[
  {
    "path": ".github/ISSUE_TEMPLATE/bug_report.md",
    "content": "---\nname: Bug report\nabout: Help us improve KeyDB by reporting a bug\ntitle: '[BUG]'\nlabels: ''\nassignees: ''\n\n---\n\n**Describe the bug**\n\nA short description of the bug.\n\n**To reproduce**\n\nSteps to reproduce the behavior and/or a minimal code sample.\n\n**Expected behavior**\n\nA description of what you expected to happen.\n\n**Additional information**\n\nAny additional information that is relevant to the problem.\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/crash_report.md",
    "content": "---\nname: Crash report\nabout: Submit a crash report\ntitle: '[CRASH]'\nlabels: ''\nassignees: ''\n\n---\n\n**Crash report**\n\nPaste the complete crash log between the quotes below. Please include a few lines from the log preceding the crash report to provide some context.\n\n```\n```\n\n**Aditional information**\n\n1. OS distribution and version\n2. Steps to reproduce (if any)\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/feature_request.md",
    "content": "---\nname: Feature request\nabout: Suggest a feature for KeyDB\ntitle: '[NEW]'\nlabels: ''\nassignees: ''\n\n---\n\n**The problem/use-case that the feature addresses**\n\nA description of the problem that the feature will solve, or the use-case with which the feature will be used.\n\n**Description of the feature**\n\nA description of what you want to happen.\n\n**Alternatives you've considered**\n\nAny alternative solutions or features you've considered, including references to existing open and closed feature requests in this repository.\n\n**Additional information**\n\nAny additional information that is relevant to the feature request.\n"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/other_stuff.md",
    "content": "---\nname: Other\nabout: Can't find the right issue type? Use this one!\ntitle: ''\nlabels: ''\nassignees: ''\n\n---\n"
  },
  {
    "path": ".github/workflows/ci.yml",
    "content": "name: CI\n\non: [push, pull_request]\n\njobs:\n\n  test-ubuntu-latest:\n    runs-on: ubuntu-latest\n    steps:\n    - uses: actions/checkout@v2\n      with:\n        submodules: recursive\n    - name: make\n      run: |\n          sudo apt-get update\n          sudo apt-get -y remove libzstd || true\n          sudo apt-get -y install uuid-dev libcurl4-openssl-dev libbz2-dev zlib1g-dev libsnappy-dev liblz4-dev libzstd-dev libgflags-dev\n          make BUILD_TLS=yes -j2 KEYDB_CFLAGS='-Werror' KEYDB_CXXFLAGS='-Werror'\n    - name: gen-cert\n      run: ./utils/gen-test-certs.sh\n    - name: test-tls\n      run: |\n        sudo apt-get -y install tcl tcl-tls\n        ./runtest --clients 1 --verbose --tls --config server-threads 3\n    - name: cluster-test\n      run: |\n        ./runtest-cluster --tls --config server-threads 3\n    - name: sentinel test\n      run: |\n          ./runtest-sentinel\n    - name: module tests\n      run: |\n          ./runtest-moduleapi\n    - name: rotation test\n      run: |\n          ./runtest-rotation\n        \n  build-ubuntu-old:\n    runs-on: ubuntu-20.04\n    steps:\n    - uses: actions/checkout@v2\n      with:\n        submodules: recursive\n    - name: make -j2\n      run: | \n        sudo apt-get update\n        sudo apt-get -y remove libzstd || true\n        sudo apt-get -y install uuid-dev libcurl4-openssl-dev libbz2-dev zlib1g-dev libsnappy-dev liblz4-dev libzstd-dev libgflags-dev\n        make -j2\n\n  build-macos-latest:\n    runs-on: macos-latest\n    steps:\n    - uses: actions/checkout@v2\n      with:\n        submodules: recursive\n    - name: make\n      run: make KEYDB_CFLAGS='-Werror' KEYDB_CXXFLAGS='-Werror' -j2\n\n  build-libc-malloc:\n    runs-on: ubuntu-latest\n    steps:\n    - uses: actions/checkout@v2\n      with:\n        submodules: recursive\n    - name: make\n      run: |\n          sudo apt-get update\n          sudo apt-get -y remove libzstd || true\n          sudo apt-get -y install uuid-dev libcurl4-openssl-dev libbz2-dev zlib1g-dev libsnappy-dev liblz4-dev libzstd-dev libgflags-dev\n          make KEYDB_CFLAGS='-Werror' KEYDB_CXXFLAGS='-Werror' MALLOC=libc -j2\n"
  },
  {
    "path": ".gitignore",
    "content": ".*.swp\ncore\n*.o\n*.xo\n*.so\n*.d\n!**/bash_completion.d\n!**/logrotate.d\n!**/keydb.service.d\n!**/keydb-sentinel.service.d\n*.log\ndump.rdb\nsrc/keydb-server\n**/bin/keydb-server\n**/app/keydb-server\n*.deb\n*.rpm\nsrc/keydb-cli\n**/bin/keydb-cli\n**/app/keydb-cli\nsrc/keydb-sentinel\n**/bin/keydb-sentinel\n**/app/keydb-sentinel\nredis-benchmark\nkeydb-benchmark\nredis-check-aof\nkeydb-check-aof\nredis-check-rdb\nkeydb-check-rdb\nredis-check-dump\nkeydb-check-dump\nkeydb-diagnostic-tool\nredis-cli\nredis-sentinel\nredis-server\ndoc-tools\nrelease\nmisc/*\nsrc/release.h\nappendonly.aof\nSHORT_TERM_TODO\nrelease.h\nsrc/transfer.sh\nsrc/configs\nredis.ds\nsrc/keydb.conf\nsrc/nodes.conf\ndeps/lua/src/lua\ndeps/lua/src/luac\ndeps/lua/src/liblua.a\ntests/tls*/*\n.make-*\n.prerequisites\n*.dSYM\nMakefile.dep\n.vscode/*\n.idea/*\n.ccls\n.ccls-cache/*\ncompile_commands.json\nkeydb.code-workspace\n"
  },
  {
    "path": ".gitmodules",
    "content": "[submodule \"deps/rocksdb\"]\n\tpath = deps/rocksdb\n\turl = https://github.com/facebook/rocksdb.git\n[submodule \"deps/depot_tools\"]\n\tpath = deps/depot_tools\n\turl = https://chromium.googlesource.com/chromium/tools/depot_tools.git\n"
  },
  {
    "path": "00-RELEASENOTES",
    "content": "Redis 6.2 release notes\n=======================\n\n--------------------------------------------------------------------------------\nUpgrade urgency levels:\n\nLOW:      No need to upgrade unless there are new features you want to use.\nMODERATE: Program an upgrade of the server, but it's not urgent.\nHIGH:     There is a critical bug that may affect a subset of users. Upgrade!\nCRITICAL: There is a critical bug affecting MOST USERS. Upgrade ASAP.\nSECURITY: There are security fixes in the release.\n--------------------------------------------------------------------------------\n\n================================================================================\nRedis 6.2.6 Released Mon Oct 4 12:00:00 IDT 2021\n================================================================================\n\nUpgrade urgency: SECURITY, contains fixes to security issues.\n\nSecurity Fixes:\n* (CVE-2021-41099) Integer to heap buffer overflow handling certain string\n  commands and network payloads, when proto-max-bulk-len is manually configured\n  to a non-default, very large value [reported by yiyuaner].\n* (CVE-2021-32762) Integer to heap buffer overflow issue in redis-cli and\n  redis-sentinel parsing large multi-bulk replies on some older and less common\n  platforms [reported by Microsoft Vulnerability Research].\n* (CVE-2021-32687) Integer to heap buffer overflow with intsets, when\n  set-max-intset-entries is manually configured to a non-default, very large\n  value [reported by Pawel Wieczorkiewicz, AWS].\n* (CVE-2021-32675) Denial Of Service when processing RESP request payloads with\n  a large number of elements on many connections.\n* (CVE-2021-32672) Random heap reading issue with Lua Debugger [reported by\n  Meir Shpilraien].\n* (CVE-2021-32628) Integer to heap buffer overflow handling ziplist-encoded\n  data types, when configuring a large, non-default value for\n  hash-max-ziplist-entries, hash-max-ziplist-value, zset-max-ziplist-entries\n  or zset-max-ziplist-value [reported by sundb].\n* (CVE-2021-32627) Integer to heap buffer overflow issue with streams, when\n  configuring a non-default, large value for proto-max-bulk-len and\n  client-query-buffer-limit [reported by sundb].\n* (CVE-2021-32626) Specially crafted Lua scripts may result with Heap buffer\n  overflow [reported by Meir Shpilraien].\n\nBug fixes that involve behavior changes:\n* GEO* STORE with empty source key deletes the destination key and return 0 (#9271)\n  Previously it would have returned an empty array like the non-STORE variant.\n* PUBSUB NUMPAT replies with number of patterns rather than number of subscriptions (#9209)\n  This actually changed in 6.2.0 but was overlooked and omitted from the release notes.\n\nBug fixes that are only applicable to previous releases of Redis 6.2:\n* Fix CLIENT PAUSE, used an old timeout from previous PAUSE (#9477)\n* Fix CLIENT PAUSE in a replica would mess the replication offset (#9448)\n* Add some missing error statistics in INFO errorstats (#9328)\n\nOther bug fixes:\n* Fix incorrect reply of COMMAND command key positions for MIGRATE command (#9455)\n* Fix appendfsync to always guarantee fsync before reply, on MacOS and FreeBSD (kqueue) (#9416)\n* Fix the wrong mis-detection of sync_file_range system call, affecting performance (#9371)\n\nCLI tools:\n* When redis-cli received ASK response, it didn't handle it (#8930)\n\nImprovements:\n* Add latency monitor sample when key is deleted via lazy expire (#9317)\n* Sanitize corrupt payload improvements (#9321, #9399)\n* Delete empty keys when loading RDB file or handling a RESTORE command (#9297, #9349)\n\n================================================================================\nRedis 6.2.5 Released Wed Jul 21 16:32:19 IDT 2021\n================================================================================\n\nUpgrade urgency: SECURITY, contains fixes to security issues that affect\nauthenticated client connections on 32-bit versions. MODERATE otherwise.\n\nFix integer overflow in BITFIELD on 32-bit versions (CVE-2021-32761).\nAn integer overflow bug in Redis version 2.2 or newer can be exploited using the\nBITFIELD command to corrupt the heap and potentially result with remote code\nexecution.\n\nBug fixes that involve behavior changes:\n* Change reply type for ZPOPMAX/MIN with count in RESP3 to nested array (#8981).\n  Was using a flat array like in RESP2 instead of a nested array like ZRANGE does.\n* Fix reply type for HRANDFIELD and ZRANDMEMBER when key is missing (#9178).\n  Was using a null array instead of an empty array.\n* Fix reply type for ZRANGESTORE when source key is missing (#9089).\n  Was using an empty array like ZRANGE instead of 0 (used in the STORE variant).\n\nBug fixes that are only applicable to previous releases of Redis 6.2:\n* ZRANDMEMBER WITHSCORES with negative COUNT may return bad score (#9162)\n* Fix crash after CLIENT UNPAUSE when threaded I/O config is enabled (#9041)\n* Fix XTRIM or XADD with LIMIT may delete more entries than the limit (#9048)\n* Fix build issue with OpenSSL 1.1.0 (#9233)\n\nOther bug fixes:\n* Fail EXEC command in case a watched key is expired (#9194)\n* Fix SMOVE not to invalidate dest key (WATCH and tracking) when member already exists (#9244)\n* Fix SINTERSTORE not to delete dest key when getting a wrong type error (#9032)\n* Fix overflows on 32-bit versions in GETBIT, SETBIT, BITCOUNT, BITPOS, and BITFIELD (#9191)\n* Improve MEMORY USAGE on stream keys (#9164)\n* Set TCP keepalive on inbound cluster bus connections (#9230)\n* Fix diskless replica loading to recover from RDB short read on module AUX data (#9199)\n* Fix race in client side tracking (#9116)\n* Fix ziplist length updates on big-endian platforms (#2080)\n\nCLI tools:\n* redis-cli cluster import command may issue wrong MIGRATE command, sending COPY instead of REPLACE (#8945)\n* redis-cli --rdb fixes when using \"-\" to write to stdout (#9136, #9135)\n* redis-cli support for RESP3 set type in CSV and RAW output (#7338)\n\nModules:\n* Module API for getting current command name (#8792)\n* Fix RM_StringTruncate when newlen is 0 (#3718)\n* Fix CLIENT UNBLOCK crashing modules without timeout callback (#9167)\n\n================================================================================\nRedis 6.2.4 Released Tue June 1 12:00:00 IST 2021\n================================================================================\n\nUpgrade urgency: SECURITY, Contains fixes to security issues that affect\nauthenticated client connections. MODERATE otherwise.\n\nFix integer overflow in STRALGO LCS (CVE-2021-32625)\nAn integer overflow bug in Redis version 6.0 or newer can be exploited using the\nSTRALGO LCS command to corrupt the heap and potentially result with remote code\nexecution. This is a result of an incomplete fix by CVE-2021-29477.\n\nBug fixes that are only applicable to previous releases of Redis 6.2:\n* Fix crash after a diskless replication fork child is terminated (#8991)\n* Fix redis-benchmark crash on unsupported configs (#8916)\n\nOther bug fixes:\n* Fix crash in UNLINK on a stream key with deleted consumer groups (#8932)\n* SINTERSTORE: Add missing keyspace del event when none of the sources exist (#8949)\n* Sentinel: Fix CONFIG SET of empty string sentinel-user/sentinel-pass configs (#8958)\n* Enforce client output buffer soft limit when no traffic (#8833)\n\nImprovements:\n* Hide AUTH passwords in MIGRATE command from slowlog (#8859)\n\n================================================================================\nRedis 6.2.3 Released Mon May 3 19:00:00 IST 2021\n================================================================================\n\nUpgrade urgency: SECURITY, Contains fixes to security issues that affect\nauthenticated client connections. LOW otherwise.\n\nInteger overflow in STRALGO LCS command (CVE-2021-29477):\nAn integer overflow bug in Redis version 6.0 or newer could be exploited using\nthe STRALGO LCS command to corrupt the heap and potentially result in remote\ncode execution. The integer overflow bug exists in all versions of Redis\nstarting with 6.0.\n\nInteger overflow in COPY command for large intsets (CVE-2021-29478):\nAn integer overflow bug in Redis 6.2 could be exploited to corrupt the heap and\npotentially result with remote code execution. The vulnerability involves\nchanging the default set-max-intset-entries configuration value, creating a\nlarge set key that consists of integer values and using the COPY command to\nduplicate it. The integer overflow bug exists in all versions of Redis starting\nwith 2.6, where it could result with a corrupted RDB or DUMP payload, but not\nexploited through COPY (which did not exist before 6.2).\n\nBug fixes that are only applicable to previous releases of Redis 6.2:\n* Fix memory leak in moduleDefragGlobals (#8853)\n* Fix memory leak when doing lazy freeing client tracking table (#8822)\n* Block abusive replicas from sending command that could assert and crash redis (#8868)\n\nOther bug fixes:\n* Use a monotonic clock to check for Lua script timeout (#8812)\n* redis-cli: Do not use unix socket when we got redirected in cluster mode (#8870)\n\nModules:\n* Fix RM_GetClusterNodeInfo() to correctly populate master id (#8846)\n\n================================================================================\nRedis 6.2.2 Released Mon April 19 19:00:00 IST 2021\n================================================================================\n\nUpgrade urgency: HIGH, if you're using ACL and pub/sub, CONFIG REWRITE, or\nsuffering from performance regression. see below.\n\nBug fixes for regressions in previous releases of Redis 6.2:\n* Fix BGSAVE, AOFRW, and replication slowdown due to child reporting CoW (#8645)\n* Fix short busy loop when timer event is about to fire (#8764)\n* Fix default user, overwritten and reset users losing pubsub channel permissions (#8723)\n* Fix config rewrite with an empty `save` config resulsing in default `save` values (#8719)\n* Fix not starting on alpine/libmusl without IPv6 (#8655)\n* Fix issues with propagation and MULTI/EXEC in modules (#8617)\n  Several issues around nested calls and thread safe contexts\n\nBug fixes that are only applicable to previous releases of Redis 6.2:\n* ACL Pub/Sub channels permission handling for save/load scenario (#8794)\n* Fix early rejection of PUBLISH inside MULTI-EXEC transaction (#8534)\n* Fix missing SLOWLOG records for blocked commands (#8632)\n* Allow RESET command during busy scripts (#8629)\n* Fix some error replies were not counted on stats (#8659)\n\nBug fixes:\n* Add a timeout mechanism for replicas stuck in fullsync (#8762)\n* Process HELLO command even if the default user has no permissions (#8633)\n* Client issuing a long running script and using a pipeline, got disconnected (#8715)\n* Fix script kill to work also on scripts that use `pcall` (#8661)\n* Fix list-compress-depth may compress more node than required (#8311)\n* Fix redis-cli handling of rediss:// URL scheme (#8705)\n* Cluster: Skip unnecessary check which may prevent failure detection (#8585)\n* Cluster: Fix hang manual failover when replica just started (#8651)\n* Sentinel: Fix info-refresh time field before sentinel get first response (#8567)\n* Sentinel: Fix possible crash on failed connection attempt (#8627)\n* Systemd: Send the readiness notification when a replica is ready to accept connections (#8409)\n\nCommand behavior changes:\n* ZADD: fix wrong reply when INCR used with GT/LT which blocked the update (#8717)\n  It was responding with the incremented value rather than nil\n* XAUTOCLAIM: fix response to return the next available id as the cursor (#8725)\n  Previous behavior was retuning the last one which was already scanned\n* XAUTOCLAIM: fix JUSTID to prevent incrementing delivery_count (#8724)\n\nNew config options:\n* Add cluster-allow-replica-migration config option (#5285)\n* Add replica-announced config option (#8653)\n* Add support for plaintext clients in TLS cluster (#8587)\n* Add support for reading encrypted keyfiles (#8644)\n\nImprovements:\n* Fix performance regression in BRPOP on Redis 6.0 (#8689)\n* Avoid adding slowlog entries for config with sensitive data (#8584)\n* Improve redis-cli non-binary safe string handling (#8566)\n* Optimize CLUSTER SLOTS reply (#8541)\n* Handle remaining fsync errors (#8419)\n\nInfo fields and introspection changes:\n* Strip % sign from current_fork_perc info field (#8628)\n* Fix RSS memory info on FreeBSD (#8620)\n* Fix client_recent_max_input/output_buffer in 'INFO CLIENTS' when all clients drop (#8588)\n* Fix invalid master_link_down_since_seconds in info replication (#8785)\n\nPlatform and deployment-related changes:\n* Fix FreeBSD <12.x builds (#8603)\n\nModules:\n* Add macros for RedisModule_log logging levels (#4246)\n* Add RedisModule_GetAbsExpire / RedisModule_SetAbsExpire (#8564)\n* Add a module type for key space notification (#8759)\n* Set module eviction context flag only in masters (#8631)\n* Fix unusable RedisModule_IsAOFClient API (#8596)\n* Fix missing EXEC on modules propagation after failed EVAL execution (#8654)\n* Fix edge-case when a module client is unblocked (#8618)\n\n================================================================================\nRedis 6.2.1 Released Mon Mar  1 17:51:36 IST 2021\n================================================================================\n\nUpgrade urgency: LOW.\n\nHere is a comprehensive list of changes in this release compared to 6.2.0,\neach one includes the PR number that added it, so you can get more details\nat https://github.com/redis/redis/pull/<number>\n\nBug fixes:\n* Fix sanitize-dump-payload for stream with deleted records (#8568)\n* Prevent client-query-buffer-limit config from being set to lower than 1mb (#8557)\n\nImprovements:\n* Make port, tls-port and bind config options modifiable at runtime (#8510)\n\nPlatform and deployment-related changes:\n* Fix compilation error on non-glibc systems if jemalloc is not used (#8533)\n* Improved memory consumption and memory usage tracking on FreeBSD (#8545)\n* Fix compilation on ARM64 MacOS with jemalloc (#8458)\n\nModules:\n* New Module API for getting user name of a client (#8508)\n* Optimize RM_Call by utilizing a shared reusable client (#8516)\n* Fix crash running CLIENT INFO via RM_Call (#8560)\n\n================================================================================\nRedis 6.2.0 GA  Released Tue Feb 22 14:00:00 IST 2021\n================================================================================\n\nUpgrade urgency: SECURITY if you use 32bit build of redis (see bellow), MODERATE\nif you used earlier versions of Redis 6.2, LOW otherwise.\n\nInteger overflow on 32-bit systems (CVE-2021-21309):\nRedis 4.0 or newer uses a configurable limit for the maximum supported bulk\ninput size. By default, it is 512MB which is a safe value for all platforms.\nIf the limit is significantly increased, receiving a large request from a client\nmay trigger several integer overflow scenarios, which would result with buffer\noverflow and heap corruption.\n\nHere is a comprehensive list of changes in this release compared to 6.2 RC3,\neach one includes the PR number that added it, so you can get more details\nat https://github.com/redis/redis/pull/<number>\n\nBug fixes:\n* Avoid 32-bit overflows when proto-max-bulk-len is set high (#8522)\n* Fix broken protocol in client tracking tracking-redir-broken message (#8456)\n* Avoid unsafe field name characters in INFO commandstats, errorstats, modules (#8492)\n* XINFO able to access expired keys during CLIENT PAUSE WRITE (#8436)\n* Fix allowed length for REPLCONF ip-address, needed due to Sentinel's support for hostnames (#8517)\n* Fix broken protocol in redis-benchmark when used with -a or --dbnum (#8486)\n* XADD counts deleted records too when considering switching to a new listpack (#8390)\n\nBug fixes that are only applicable to previous releases of Redis 6.2:\n* Fixes in GEOSEARCH bybox (accuracy and mismatch between width and height) (#8445)\n* Fix risk of OOM panic in HRANDFIELD, ZRANDMEMBER commands with huge negative count (#8429)\n* Fix duplicate replicas issue in Sentinel, needed due to hostname support (#8481)\n* Fix Sentinel configuration rewrite, an improvement of #8271 (#8480)\n\nCommand behavior changes:\n* SRANDMEMBER uses RESP3 array type instead of set type (#8504)\n* EXPIRE, EXPIREAT, SETEX, GETEX: Return error when provided expire time overflows (#8287)\n\nOther behavior changes:\n* Remove ACL subcommand validation if fully added command exists. (#8483)\n\nImprovements:\n* Optimize sorting in GEORADIUS / GEOSEARCH with COUNT (#8326)\n* Optimize HRANDFIELD and ZRANDMEMBER case 4 when ziplist encoded (#8444)\n* Optimize in-place replacement of elements in HSET, HINCRBY, LSET (#8493)\n* Remove redundant list to store pubsub patterns (#8472)\n* Add --insecure option to command line tools (#8416)\n\nInfo fields and introspection changes:\n* Add INFO fields to track progress of BGSAVE, AOFRW, replication (#8414)\n\nModules:\n* RM_ZsetRem: Delete key if empty, the bug could leave empty zset keys (#8453)\n* RM_HashSet: Add COUNT_ALL flag and set errno (#8446)\n\n================================================================================\nRedis 6.2 RC3   Released Tue Feb 1 14:00:00 IST 2021\n================================================================================\n\nUpgrade urgency LOW: This is the third Release Candidate of Redis 6.2.\n\nHere is a comprehensive list of changes in this release compared to 6.2 RC2,\neach one includes the PR number that added it, so you can get more details\nat https://github.com/redis/redis/pull/<number>\n\nNew commands / args:\n* Add HRANDFIELD and ZRANDMEMBER commands (#8297)\n* Add FAILOVER command (#8315)\n* Add GETEX, GETDEL commands (#8327)\n* Add PXAT/EXAT arguments to SET command (#8327)\n* Add SYNC arg to FLUSHALL and FLUSHDB, and ASYNC/SYNC arg to SCRIPT FLUSH (#8258)\n\nSentinel:\n* Add hostname support to Sentinel (#8282)\n* Prevent file descriptors from leaking into Sentinel scripts (#8242)\n* Fix config file line order dependency and config rewrite sequence (#8271)\n\nNew configuration options:\n* Add set-proc-title config option to disable changes to the process title (#3623)\n* Add proc-title-template option to control what's shown in the process title (#8397)\n* Add lazyfree-lazy-user-flush config option to control FLUSHALL, FLUSHDB and SCRIPT FLUSH (#8258)\n\nBug fixes:\n* AOF: recover from last write error by turning on/off appendonly config (#8030)\n* Exit on fsync error when the AOF fsync policy is 'always' (#8347)\n* Avoid assertions (on older kernels) when testing arm64 CoW bug (#8405)\n* CONFIG REWRITE should honor umask settings (#8371)\n* Fix firstkey,lastkey,step in COMMAND command for some commands (#8367)\n\nSpecial considerations:\n* Fix misleading description of the save configuration directive (#8337)\n\nImprovements:\n* A way to get RDB file via replication without excessive replication buffers (#8303)\n* Optimize performance of clusterGenNodesDescription for large clusters (#8182)\n\nInfo fields and introspection changes:\n* SLOWLOG and LATENCY monitor include unblocking time of blocked commands (#7491)\n\nModules:\n* Add modules API for streams (#8288)\n* Add event for fork child birth and termination (#8289)\n* Add RM_BlockedClientMeasureTime* etc, to track background processing in commandstats (#7491)\n* Fix bug in v6.2, wrong value passed to the new unlink callback (#8381)\n* Fix bug in v6.2, modules blocked on keys unblock on commands like LPUSH (#8356)\n\n================================================================================\nRedis 6.2 RC2   Released Tue Jan 12 16:17:20 IST 2021\n================================================================================\n\nUpgrade urgency LOW: This is the second Release Candidate of Redis 6.2.\n\nIMPORTANT: If you're running Redis on ARM64 or a big-endian system, upgrade may\nhave significant implications. Please be sure to read the notes below.\n\nHere is a comprehensive list of changes in this release compared to 6.2 RC1,\neach one includes the PR number that added it, so you can get more details\nat https://github.com/redis/redis/pull/<number>\n\nNew commands / args:\n* Add the REV, BYLEX and BYSCORE arguments to ZRANGE, and the ZRANGESTORE command (#7844)\n* Add the XAUTOCLAIM command (#7973)\n* Add the MINID trimming strategy and the LIMIT argument to XADD and XTRIM (#8169)\n* Add the ANY argument to GEOSEARCH and GEORADIUS (#8259)\n* Add the CH, NX, XX arguments to GEOADD (#8227)\n* Add the COUNT argument to LPOP and RPOP (#8179)\n* Add the WRITE argument to CLIENT PAUSE for pausing write commands exclusively (#8170)\n* Change the proto-ver argument of HELLO to optional (#7377)\n* Add the CLIENT TRACKINGINFO subcommand (#7309)\n\nCommand behavior changes:\n* CLIENT TRACKING yields an error when given overlapping BCAST prefixes (#8176)\n* SWAPDB invalidates WATCHed keys (#8239)\n* SORT command behaves differently when used on a writable replica (#8283)\n\nOther behavior changes:\n* Avoid propagating MULTI/EXEC for read-only transactions (#8216)\n* Remove the read-only flag from TIME, ECHO, ROLE, LASTSAVE (#8216)\n* Fix the command flags of PFDEBUG (#8222)\n* Tracking clients will no longer receive unnecessary key invalidation messages after FLUSHDB (#8039)\n* Sentinel: Fix missing updates to the config file after SENTINEL SET command (#8229)\n\nBug fixes with compatibility implications (bugs introduced in Redis 6.0):\n* Fix RDB CRC64 checksum on big-endian systems (#8270)\n  If you're using big-endian please consider the compatibility implications with\n  RESTORE, replication and persistence.\n* Fix wrong order of key/value in Lua's map response (#8266)\n  If your scripts use redis.setresp() or return a map (new in Redis 6.0), please\n  consider the implications.\n\nBug fixes that are only applicable to previous releases of Redis 6.2:\n* Resolve rare assertions in active defragmentation while loading (#8284, #8281)\n\nBug fixes:\n* Fix the selection of a random element from large hash tables (#8133)\n* Fix an issue where a forked process deletes the parent's pidfile (#8231)\n* Fix crashes when enabling io-threads-do-reads (#8230)\n* Fix a crash in redis-cli after executing cluster backup (#8267)\n* Fix redis-benchmark to use an IP address for the first cluster node (#8154)\n* Fix saving of strings larger than 2GB into RDB files (#8306)\n\nAdditional improvements:\n* Improve replication handshake time (#8214)\n* Release client tracking table memory asynchronously in cases where the DB is also freed asynchronously (#8039)\n* Avoid wasteful transient memory allocation in certain cases (#8286, #5954)\n* Handle binary string values by the 'requirepass' and 'masterauth' configs (#8200)\n\nPlatform and deployment-related changes:\n* Install redis-check-rdb and redis-check-aof as symlinks to redis-server (#5745)\n* Add a check for an ARM64 Linux kernel bug (#8224)\n  Due to the potential severity of this issue, Redis will refuse to run on\n  affected platforms by default.\n\nInfo fields and introspection changes:\n* Add the errorstats section to the INFO command (#8217)\n* Add the failed_calls and rejected_calls fields INFO's commandstats section (#8217)\n* Report child copy-on-write metrics continuously (#8264)\n\nModule API changes:\n* Add the RedisModule_SendChildCOWInfo API (#8264)\n* Add the may-replicate command flag (#8170)\n\n================================================================================\nRedis 6.2 RC1   Released Mon Dec 14 11:50:00 IST 2020\n================================================================================\n\nUpgrade urgency LOW: This is the first Release Candidate of Redis 6.2.\n\nIntroduction to the Redis 6.2 release\n=====================================\n\nThis release is the first significant Redis release managed by the core team\nunder the new project governance model.\n\nRedis 6.2 includes many new commands and improvements, but no big features. It\nmainly makes Redis more complete and addresses issues that have been requested\nby many users frequently or for a long time.\n\nMany of these changes were not eligible for 6.0.x for several reasons:\n\n1. They are not backward compatible, which is always the case with new or\n   extended commands (that cannot be replicated to an older replica).\n2. They require a longer release-candidate test cycle.\n\n\nHere is a comprehensive list of changes in this release compared to 6.0.9,\neach one includes the PR number that added it, so you can get more details\nat https://github.com/redis/redis/pull/<number>\n\nNew commands / args:\n* Add SMISMEMBER command that checks multiple members (#7615)\n* Add ZMSCORE command that returns an array of scores (#7593)\n* Add LMOVE and BLMOVE commands that pop and push arbitrarily (#6929)\n* Add RESET command that resets client connection state (#7982)\n* Add COPY command that copies keys (#7953)\n* Add ZDIFF and ZDIFFSTORE commands (#7961)\n* Add ZINTER and ZUNION commands (#7794)\n* Add GEOSEARCH/GEOSEARCHSTORE commands for bounding box spatial queries (#8094)\n* Add GET parameter to SET command, for more powerful GETSET (#7852)\n* Add exclusive range query to XPENDING (#8130)\n* Add exclusive range query to X[REV]RANGE (#8072)\n* Add GT and LT options to ZADD for conditional score updates (#7818)\n* Add CLIENT INFO and CLIENT LIST for specific ids (#8113)\n* Add IDLE argument to XPENDING command (#7972)\n* Add local address to CLIENT LIST, and a CLIENT KILL filter. (#7913)\n* Add NOMKSTREAM option to XADD command (#7910)\n* Add command introspection to Sentinel (#7940)\n* Add SENTINEL MYID subcommand (#7858)\n\nNew features:\n* Dump payload sanitization: prevent corrupt payload causing crashes (#7807)\n  Has flags to enable full O(N) validation (disabled by default).\n* ACL patterns for Pub/Sub channels (#7993)\n* Support ACL for Sentinel mode (#7888)\n* Support getting configuration from both stdin and file at the same time (#7893)\n  Lets you avoid storing secrets on the disk.\n\nNew features in CLI tools:\n* redis-cli RESP3 push support (#7609)\n* redis-cli cluster import support source and target that require auth (#7994)\n* redis-cli URIs able to provide user name in addition to password (#8048)\n* redis-cli/redis-benchmark allow specifying the prefered ciphers/ciphersuites (#8005)\n* redis-cli add -e option to exit with code when command execution fails (#8136)\n\nCommand behavior changes:\n* EXISTS should not alter LRU (#8016)\n  In Redis 5.0 and 6.0 it would have touched the LRU/LFU of the key.\n* OBJECT should not reveal logically expired keys (#8016)\n  Will now behave the same TYPE or any other non-DEBUG command.\n* Improve db id range check for SELECT and MOVE (#8085)\n  Changes the error message text on a wrong db index.\n* Modify AUTH / HELLO error message (#7648)\n  Changes the error message text when the user isn't found or is disabled.\n* BITOPS length limited to proto_max_bulk_len rather than 512MB (#8096)\n  The limit is now configurable like in SETRANGE, and APPEND.\n* GEORADIUS[BYMEMBER] can fail with -OOM if Redis is over the memory limit (#8107)\n\nOther behavior changes:\n* Optionally (default) fail to start if requested bind address is not available (#7936)\n  If you rely on Redis starting successfully even if one of the bind addresses\n  is not available, you'll need to tune the new config.\n* Limit the main db dictionaries expansion to prevent key eviction (#7954)\n  In the past big dictionary rehashing could result in massive data eviction.\n  Now this rehashing is delayed (up to a limit), which can result in performance\n  loss due to hash collisions.\n* CONFIG REWRITE is atomic and safer, but requires write access to the config file's folder (#7824, #8051)\n  This change was already present in 6.0.9, but was missing from the release\n  notes.\n* A new incremental eviction mechanism that reduces latency on eviction spikes (#7653)\n  In pathological cases this can cause memory to grow uncontrolled and may require\n  specific tuning.\n* Not resetting \"save\" config when Redis is started with command line arguments. (#7092)\n  In case you provide command line arguments without \"save\" and count on it\n  being disabled, Now the defaults \"save\" config will kick in.\n* Update memory metrics for INFO during loading (#7690)\n* When \"supervised\" config is enabled, it takes precedence over \"daemonize\". (#8036)\n* Assertion and panic, print crash log without generating SIGSEGV (#7585)\n* Added crash log report on SIGABRT, instead of silently exiting (#8004)\n* Disable THP (Transparent Huge Pages) if enabled (#7381)\n  If you deliberately enabled it, you'll need to config Redis to keep it.\n\nBug fixes:\n* Handle output buffer limits for module blocked clients (#8141)\n  Could result in a module sending reply to a blocked client to go beyond the\n  limit.\n* Fix setproctitle related crashes. (#8150, #8088)\n  Caused various crashes on startup, mainly on Apple M1 chips or under\n  instrumentation.\n* A module doing RM_Call could cause replicas to get nested MULTI (#8097).\n* Backup/restore cluster mode keys to slots map for repl-diskless-load=swapdb (#8108)\n  In cluster mode with repl-diskless-load, when loading failed, slot map\n  wouldn't have been restored.\n* Fix oom-score-adj-values range, and bug when used in config file (#8046)\n  Enabling setting this in the config file in a line after enabling it, would\n  have been buggy.\n* Reset average ttl when empty databases (#8106)\n  Just causing misleading metric in INFO\n* Disable rehash when Redis has child process (#8007)\n  This could have caused excessive CoW during BGSAVE, replication or AOFRW.\n* Further improved ACL algorithm for picking categories (#7966)\n  Output of ACL GETUSER is now more similar to the one provided by ACL SETUSER.\n* Fix bug with module GIL being released prematurely (#8061)\n  Could in theory (and rarely) cause multi-threaded modules to corrupt memory.\n* Fix cluster redirect for module command with no firstkey. (#7539)\n* Reduce effect of client tracking causing feedback loop in key eviction (#8100)\n* Kill disk-based fork child when all replicas drop and 'save' is not enabled (#7819)\n* Rewritten commands (modified for propagation) are logged as their original command (#8006)\n* Fix cluster access to unaligned memory (SIGBUS on old ARM) #7958\n* If diskless repl child is killed, make sure to reap the child pid (#7742)\n* Broadcast a PONG message when slot's migration is over, may reduce MOVED responses (#7571)\n\nOther improvements:\n* TLS Support in redis-benchmark (#7959)\n* Accelerate diskless master connections, and general re-connections (#6271)\n* Run active defrag while blocked / loading (#7726)\n* Performance and memory reporting improvement - sds take control of its internal fragmentation (#7875)\n* Speedup cluster failover. (#7948)\n\nPlatform / toolchain support related improvements:\n* Optionally (not by default) use H/W Monotonic clock for faster time sampling (#7644)\n* Remove the requirements for C11 and _Atomic supporting compiler (#7707)\n  This would allow to more easily build and use Redis on older systems and\n  compilers again.\n* Fix crash log registers output on ARM. (#8020)\n* Raspberry build fix. (#8095)\n* Setting process title support for Haiku. (#8060)\n* DragonFlyBSD RSS memory sampling support. (#8023)\n\nNew configuration options:\n* Enable configuring OpenSSL using the standard openssl.cnf (#8143)\n* oom-score-adj-values config can now take absolute values (besides relative ones) (#8046)\n* TLS: Add different client cert support. (#8076)\n* Note that a few other changes listed above added their config options.\n\nInfo fields and introspection changes:\n* Add INFO fields to track diskless and disk-based replication progress (#7981)\n* Add INFO field for main thread cpu time, and scrape system time. (#8132)\n* Add total_forks to INFO STATS (#8155)\n* Add maxclients and cluster_connections to INFO CLIENTS (#7979)\n* Add tracking bcast flag and client redirection in client list (#7995)\n* Fixed INFO client_recent_max_input_buffer includes argv array (#8065, see #7874)\n* Note that a few other changes listed above added their info fields.\n\nModule API changes:\n* Add CTX_FLAGS_DENY_BLOCKING as a unified the way to know if blocking is allowed (#8025)\n* Add data type callbacks for lazy free effort, and unlink (#7912)\n* Add data type callback for COPY command (#8112)\n* Add callbacks for defrag support. (#8149)\n* Add module event for repl-diskless-load swapdb (#8153)\n\nModule related fixes:\n* Moved RMAPI_FUNC_SUPPORTED so that it's usable (#8037)\n* Improve timer accuracy (#7987)\n* Allow '\\0' inside of result of RM_CreateStringPrintf (#6260)\n\n\nThanks to all the users and developers who made this release possible.\nWe'll follow up with more RC releases, until the code looks production ready\nand we don't get reports of serious issues for a while.\n\nA special thank you for the amount of work put into this release by:\n- Oran Agra\n- Yossi Gottlieb\n- Viktor Söderqvist\n- Yang Bodong\n- Filipe Oliveira\n- Guy Benoish\n- Itamar Haber\n- Madelyn Olson\n- Wang Yuan\n- Felipe Machado\n- Wen Hui\n- Tatsuya Arisawa\n- Jonah H. Harris\n- Raghav Muddur\n- Jim Brunner\n- Yaacov Hazan\n- Allen Farris\n- Chen Yang\n- Nitai Caro\n- sundb\n- Meir Shpilraien\n- maohuazhu\n- Valentino Geron\n- Zhao Zhao\n- Qu Chen\n- George Prekas\n- Tyson Andre\n- Uri Yagelnik\n- Michael Grunder\n- Huang Zw\n- alexronke-channeladvisor\n- Andy Pan\n- Wu Yunlong\n- Wei Kukey\n- Yoav Steinberg\n- Greg Femec\n- Uri Shachar\n- Nykolas Laurentino de Lima\n- xhe\n- zhenwei pi\n- David CARLIER\n\nMigrating from 6.0 to 6.2\n=========================\n\nRedis 6.2 is mostly a strict superset of 6.0, you should not have any problem\nupgrading your application from 6.0 to 6.2. However there are some small changes\nof behavior listed above, please make sure you are not badly affected by any of\nthem.\n\nSpecifically these sections:\n* Command behavior changes\n* Other behavior changes\n\n--------------------------------------------------------------------------------\n\nCheers,\nThe Redis team\n"
  },
  {
    "path": "BUGS",
    "content": "Please check https://github.com/redis/redis/issues\n"
  },
  {
    "path": "CONDUCT",
    "content": "Contributor Covenant Code of Conduct\nOur Pledge\nWe as members, contributors, and leaders pledge to make participation in our\ncommunity a harassment-free experience for everyone, regardless of age, body\nsize, visible or invisible disability, ethnicity, sex characteristics, gender\nidentity and expression, level of experience, education, socio-economic status,\nnationality, personal appearance, race, religion, or sexual identity\nand orientation.\nWe pledge to act and interact in ways that contribute to an open, welcoming,\ndiverse, inclusive, and healthy community.\nOur Standards\nExamples of behavior that contributes to a positive environment for our\ncommunity include:\n\n* Demonstrating empathy and kindness toward other people\n* Being respectful of differing opinions, viewpoints, and experiences\n* Giving and gracefully accepting constructive feedback\n* Accepting responsibility and apologizing to those affected by our mistakes,\nand learning from the experience\n* Focusing on what is best not just for us as individuals, but for the\noverall community\n\nExamples of unacceptable behavior include:\n\n* The use of sexualized language or imagery, and sexual attention or\nadvances of any kind\n* Trolling, insulting or derogatory comments, and personal or political attacks\n* Public or private harassment\n* Publishing others’ private information, such as a physical or email\naddress, without their explicit permission\n* Other conduct which could reasonably be considered inappropriate in a\nprofessional setting\n\nEnforcement Responsibilities\nCommunity leaders are responsible for clarifying and enforcing our standards of\nacceptable behavior and will take appropriate and fair corrective action in\nresponse to any behavior that they deem inappropriate, threatening, offensive,\nor harmful.\nCommunity leaders have the right and responsibility to remove, edit, or reject\ncomments, commits, code, wiki edits, issues, and other contributions that are\nnot aligned to this Code of Conduct, and will communicate reasons for moderation\ndecisions when appropriate.\nScope\nThis Code of Conduct applies within all community spaces, and also applies when\nan individual is officially representing the community in public spaces.\nExamples of representing our community include using an official e-mail address,\nposting via an official social media account, or acting as an appointed\nrepresentative at an online or offline event.\nEnforcement\nInstances of abusive, harassing, or otherwise unacceptable behavior may be\nreported to the community leaders responsible for enforcement at\nthis email address: redis@redis.io.\nAll complaints will be reviewed and investigated promptly and fairly.\nAll community leaders are obligated to respect the privacy and security of the\nreporter of any incident.\nEnforcement Guidelines\nCommunity leaders will follow these Community Impact Guidelines in determining\nthe consequences for any action they deem in violation of this Code of Conduct:\n1. Correction\nCommunity Impact: Use of inappropriate language or other behavior deemed\nunprofessional or unwelcome in the community.\nConsequence: A private, written warning from community leaders, providing\nclarity around the nature of the violation and an explanation of why the\nbehavior was inappropriate. A public apology may be requested.\n2. Warning\nCommunity Impact: A violation through a single incident or series\nof actions.\nConsequence: A warning with consequences for continued behavior. No\ninteraction with the people involved, including unsolicited interaction with\nthose enforcing the Code of Conduct, for a specified period of time. This\nincludes avoiding interactions in community spaces as well as external channels\nlike social media. Violating these terms may lead to a temporary or\npermanent ban.\n3. Temporary Ban\nCommunity Impact: A serious violation of community standards, including\nsustained inappropriate behavior.\nConsequence: A temporary ban from any sort of interaction or public\ncommunication with the community for a specified period of time. No public or\nprivate interaction with the people involved, including unsolicited interaction\nwith those enforcing the Code of Conduct, is allowed during this period.\nViolating these terms may lead to a permanent ban.\n4. Permanent Ban\nCommunity Impact: Demonstrating a pattern of violation of community\nstandards, including sustained inappropriate behavior,  harassment of an\nindividual, or aggression toward or disparagement of classes of individuals.\nConsequence: A permanent ban from any sort of public interaction within\nthe community.\nAttribution\nThis Code of Conduct is adapted from the Contributor Covenant,\nversion 2.0, available at\nhttps://www.contributor-covenant.org/version/2/0/code_of_conduct.html.\nCommunity Impact Guidelines were inspired by Mozilla’s code of conduct\nenforcement ladder.\nFor answers to common questions about this code of conduct, see the FAQ at\nhttps://www.contributor-covenant.org/faq. Translations are available at\nhttps://www.contributor-covenant.org/translations."
  },
  {
    "path": "COPYING",
    "content": "Copyright (c) 2006-2020, Salvatore Sanfilippo\nCopyright (C) 2019-2021, John Sully\nCopyright (C) 2020-2021, EQ Alpha Technology Ltd.\nCopyright (C) 2022 Snap Inc.\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:\n\n    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.\n    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.\n    * Neither the name of Redis nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
  },
  {
    "path": "INSTALL",
    "content": "See README\n"
  },
  {
    "path": "Makefile",
    "content": "# Top level makefile, this just calls into src/Makefile where the real work is done. Changes should be made there.\n\ndefault: all\n\n.DEFAULT:\n\tcd src && $(MAKE) $@\n\ninstall:\n\tcd src && $(MAKE) $@\n\n.PHONY: install\n"
  },
  {
    "path": "README.md",
    "content": "![Current Release](https://img.shields.io/github/release/JohnSully/KeyDB.svg)\n![CI](https://github.com/JohnSully/KeyDB/workflows/CI/badge.svg?branch=unstable)\n[![StackShare](http://img.shields.io/badge/tech-stack-0690fa.svg?style=flat)](https://stackshare.io/eq-alpha-technology-inc/eq-alpha-technology-inc)\n\n##### KeyDB is now a part of Snap Inc! Check out the announcement [here](https://docs.keydb.dev/news/2022/05/12/keydb-joins-snap) \n\n##### [Release v6.3.0](https://github.com/EQ-Alpha/KeyDB/releases/tag/v6.3.0) is here with major improvements as we consolidate our Open Source and Enterprise offerings into a single BSD-3 licensed project. See our [roadmap](https://docs.keydb.dev/docs/coming-soon) for details. \n\n##### Want to extend KeyDB with Javascript?  Try [ModJS](https://github.com/JohnSully/ModJS)\n\n##### Need Help? Check out our extensive [documentation](https://docs.keydb.dev).\n\n##### KeyDB is on Slack. Click [here](https://docs.keydb.dev/slack/) to learn more and join the KeyDB Community Slack workspace.\n\nWhat is KeyDB?\n--------------\n\nKeyDB is a high performance fork of Redis with a focus on multithreading, memory efficiency, and high throughput. In addition to performance improvements, KeyDB offers features such as Active Replication, FLASH Storage and Subkey Expires. KeyDB has a MVCC architecture that allows you to execute queries such as KEYS and SCAN without blocking the database and degrading performance.\n\nKeyDB maintains full compatibility with the Redis protocol, modules, and scripts.  This includes the atomicity guarantees for scripts and transactions.  Because KeyDB keeps in sync with Redis development KeyDB is a superset of Redis functionality, making KeyDB a drop in replacement for existing Redis deployments.\n\nOn the same hardware KeyDB can achieve significantly higher throughput than Redis. Active-Replication simplifies hot-spare failover allowing you to easily distribute writes over replicas and use simple TCP based load balancing/failover. KeyDB's higher performance allows you to do more on less hardware which reduces operation costs and complexity.\n\nThe chart below compares several KeyDB and Redis setups, including the latest Redis6 io-threads option, and TLS benchmarks.\n\n<img src=\"https://docs.keydb.dev/img/blog/2020-09-15/ops_comparison.png\"/>\n\nSee the full benchmark results and setup information here: https://docs.keydb.dev/blog/2020/09/29/blog-post/\n\nWhy fork Redis?\n---------------\n\nKeyDB has a different philosophy on how the codebase should evolve.  We feel that ease of use, high performance, and a \"batteries included\" approach is the best way to create a good user experience.  While we have great respect for the Redis maintainers it is our opinion that the Redis approach focuses too much on simplicity of the code base at the expense of complexity for the user.  This results in the need for external components and workarounds to solve common problems - resulting in more complexity overall.\n\nBecause of this difference of opinion features which are right for KeyDB may not be appropriate for Redis.  A fork allows us to explore this new development path and implement features which may never be a part of Redis.  KeyDB keeps in sync with upstream Redis changes, and where applicable we upstream bug fixes and changes. It is our hope that the two projects can continue to grow and learn from each other.\n\nProject Support\n-------------------\n\nThe KeyDB team maintains this project as part of Snap Inc. KeyDB is used by Snap as part of its caching infrastructure and is fully open sourced. There is no separate commercial product and no paid support options available. We really value collaborating with the open source community and welcome PRs, bug reports, and open discussion. For community support or to get involved further with the project check out our community support options [here](https://docs.keydb.dev/docs/support) (slack, forum, meetup, github issues). Our team monitors these channels regularly.\n\n\nAdditional Resources\n--------------------\n\nTry the KeyDB [Docker Image](https://hub.docker.com/r/eqalpha/keydb)\n\nJoin us on [Slack](https://docs.keydb.dev/slack/)\n\nLearn more using KeyDB's extensive [documentation](https://docs.keydb.dev)\n\nSee the [KeyDB Roadmap](https://docs.keydb.dev/docs/coming-soon) to see what's in store\n\n\nBenchmarking KeyDB\n------------------\n\nPlease note keydb-benchmark and redis-benchmark are currently single threaded and too slow to properly benchmark KeyDB.  We recommend using a redis cluster benchmark tool such as [memtier](https://github.com/RedisLabs/memtier_benchmark).  Please ensure your machine has enough cores for both KeyDB and memtier if testing locally.  KeyDB expects exclusive use of any cores assigned to it.\n\n\nNew Configuration Options\n-------------------------\n\nWith new features comes new options. All other configuration options behave as you'd expect.  Your existing configuration files should continue to work unchanged.\n\n```\n    server-threads N\n    server-thread-affinity [true/false]\n```\nThe number of threads used to serve requests.  This should be related to the number of queues available in your network hardware, *not* the number of cores on your\nmachine.  Because KeyDB uses spinlocks to reduce latency; making this too high will reduce performance.  We recommend using 4 here.  By default this is set to two.\n\n```\nmin-clients-per-thread 50\n```\nThe minimum number of clients on a thread before KeyDB assigns new connections to a different thread. Tuning this parameter is a tradeoff between locking overhead and distributing the workload over multiple cores\n\n```\nreplica-weighting-factor 2\n```\nKeyDB will attempt to balance clients across threads evenly; However, replica clients are usually much more expensive than a normal client, and so KeyDB will try to assign fewer clients to threads with a replica.  The weighting factor below is intended to help tune this behavior.  A replica weighting factor of 2 means we treat a replica as the equivalent of two normal clients.  Adjusting this value may improve performance when replication is used.  The best weighting is workload specific - e.g. read heavy workloads should set this to 1.  Very write heavy workloads may benefit from higher numbers.\n\n```\nactive-client-balancing yes\n```\nShould KeyDB make active attempts at balancing clients across threads?  This can impact performance accepting new clients.  By default this is enabled.  If disabled there is still a best effort from the kernel to distribute across threads with SO_REUSEPORT but it will not be as fair. By default this is enabled\n\n```\n    active-replica yes\n```\nIf you are using active-active replication set `active-replica` option to “yes”. This will enable both instances to accept reads and writes while remaining synced. [Click here](https://docs.keydb.dev/docs/active-rep/) to see more on active-rep in our docs section. There are also [docker examples]( https://docs.keydb.dev/docs/docker-active-rep/) on docs.\n\n```\nmulti-master-no-forward no\n```\nAvoid forwarding RREPLAY messages to other masters? WARNING: This setting is dangerous! You must be certain all masters are connected to eachother in a true mesh topology or data loss will occur! This command can be used to reduce multimaster bus traffic\n\n\n```\n    db-s3-object /path/to/bucket\n```\nIf you would like KeyDB to dump and load directly to AWS S3 this option specifies the bucket.  Using this option with the traditional RDB options will result in KeyDB backing up twice to both locations.  If both are specified KeyDB will first attempt to load from the local dump file and if that fails load from S3.  This requires the AWS CLI tools to be installed and configured which are used under the hood to transfer the data.\n\n\n```\nstorage-provider flash /path/to/flash\n```\nIf you would like to use KeyDB FLASH storage, specify the storage medium followed by the directory path on your local SSD volume. Note that this feature is still considered experimental and should be used with discretion. See [FLASH Documentation](https://docs.keydb.dev/docs/flash) for more details on configuration and setting up your FLASH volume. \n\n\nBuilding KeyDB\n--------------\n\nKeyDB can be compiled and is tested for use on Linux.  KeyDB currently relies on SO_REUSEPORT's load balancing behavior which is available only in Linux.  When we support marshalling connections across threads we plan to support other operating systems such as FreeBSD.\n\nMore on CentOS/Archlinux/Alpine/Debian/Ubuntu dependencies and builds can be found here: https://docs.keydb.dev/docs/build/\n\nInit and clone submodule dependencies:\n\n    % git submodule init && git submodule update\n\nInstall dependencies:\n\n    % sudo apt install build-essential nasm autotools-dev autoconf libjemalloc-dev tcl tcl-dev uuid-dev libcurl4-openssl-dev libbz2-dev libzstd-dev liblz4-dev libsnappy-dev libssl-dev\n\nCompiling is as simple as:\n\n    % make\n\nTo build with systemd support, you'll need systemd development libraries (such \nas libsystemd-dev on Debian/Ubuntu or systemd-devel on CentOS) and run:\n\n    % make USE_SYSTEMD=yes\n\nTo append a suffix to KeyDB program names, use:\n\n    % make PROG_SUFFIX=\"-alt\"\n\n***Note that the following dependencies may be needed: \n    % sudo apt-get install autoconf autotools-dev libnuma-dev libtool\n\nKeyDB by default is built with TLS enabled. To build without TLS support, use:\n\n    % make BUILD_TLS=no\n\nRunning the tests with TLS enabled (you will need `tcl-tls`\ninstalled):\n\n    % ./utils/gen-test-certs.sh\n    % ./runtest --tls\n\nTo build with KeyDB FLASH support, use:\n\n    % make ENABLE_FLASH=yes\n\n***Note that the KeyDB FLASH feature is considered experimental (beta) and should used with discretion\n\n\nFixing build problems with dependencies or cached build options\n---------\n\nKeyDB has some dependencies which are included in the `deps` directory.\n`make` does not automatically rebuild dependencies even if something in\nthe source code of dependencies changes.\n\nWhen you update the source code with `git pull` or when code inside the\ndependencies tree is modified in any other way, make sure to use the following\ncommand in order to really clean everything and rebuild from scratch:\n\n    make distclean\n\nThis will clean: jemalloc, lua, hiredis, linenoise.\n\nAlso if you force certain build options like 32bit target, no C compiler\noptimizations (for debugging purposes), and other similar build time options,\nthose options are cached indefinitely until you issue a `make distclean`\ncommand.\n\nFixing problems building 32 bit binaries\n---------\n\nIf after building KeyDB with a 32 bit target you need to rebuild it\nwith a 64 bit target, or the other way around, you need to perform a\n`make distclean` in the root directory of the KeyDB distribution.\n\nIn case of build errors when trying to build a 32 bit binary of KeyDB, try\nthe following steps:\n\n* Install the package libc6-dev-i386 (also try g++-multilib).\n* Try using the following command line instead of `make 32bit`:\n  `make CFLAGS=\"-m32 -march=native\" LDFLAGS=\"-m32\"`\n\nAllocator\n---------\n\nSelecting a non-default memory allocator when building KeyDB is done by setting\nthe `MALLOC` environment variable. KeyDB is compiled and linked against libc\nmalloc by default, with the exception of jemalloc being the default on Linux\nsystems. This default was picked because jemalloc has proven to have fewer\nfragmentation problems than libc malloc.\n\nTo force compiling against libc malloc, use:\n\n    % make MALLOC=libc\n\nTo compile against jemalloc on Mac OS X systems, use:\n\n    % make MALLOC=jemalloc\n\nMonotonic clock\n---------------\n\nBy default, KeyDB will build using the POSIX clock_gettime function as the\nmonotonic clock source.  On most modern systems, the internal processor clock\ncan be used to improve performance.  Cautions can be found here: \n    http://oliveryang.net/2015/09/pitfalls-of-TSC-usage/\n\nTo build with support for the processor's internal instruction clock, use:\n\n    % make CFLAGS=\"-DUSE_PROCESSOR_CLOCK\"\n\nVerbose build\n-------------\n\nKeyDB will build with a user friendly colorized output by default.\nIf you want to see a more verbose output, use the following:\n\n    % make V=1\n\nRunning KeyDB\n-------------\n\nTo run KeyDB with the default configuration, just type:\n\n    % cd src\n    % ./keydb-server\n\nIf you want to provide your keydb.conf, you have to run it using an additional\nparameter (the path of the configuration file):\n\n    % cd src\n    % ./keydb-server /path/to/keydb.conf\n\nIt is possible to alter the KeyDB configuration by passing parameters directly\nas options using the command line. Examples:\n\n    % ./keydb-server --port 9999 --replicaof 127.0.0.1 6379\n    % ./keydb-server /etc/keydb/6379.conf --loglevel debug\n\nAll the options in keydb.conf are also supported as options using the command\nline, with exactly the same name.\n\n\nRunning KeyDB with TLS:\n------------------\n\nPlease consult the [TLS.md](TLS.md) file for more information on\nhow to use KeyDB with TLS.\n\n\nPlaying with KeyDB\n------------------\n\nYou can use keydb-cli to play with KeyDB. Start a keydb-server instance,\nthen in another terminal try the following:\n\n    % cd src\n    % ./keydb-cli\n    keydb> ping\n    PONG\n    keydb> set foo bar\n    OK\n    keydb> get foo\n    \"bar\"\n    keydb> incr mycounter\n    (integer) 1\n    keydb> incr mycounter\n    (integer) 2\n    keydb>\n\nYou can find the list of all the available commands at https://docs.keydb.dev/docs/commands/\n\nInstalling KeyDB\n-----------------\n\nIn order to install KeyDB binaries into /usr/local/bin, just use:\n\n    % make install\n\nYou can use `make PREFIX=/some/other/directory install` if you wish to use a\ndifferent destination.\n\nMake install will just install binaries in your system, but will not configure\ninit scripts and configuration files in the appropriate place. This is not\nneeded if you just want to play a bit with KeyDB, but if you are installing\nit the proper way for a production system, we have a script that does this\nfor Ubuntu and Debian systems:\n\n    % cd utils\n    % ./install_server.sh\n\n_Note_: `install_server.sh` will not work on Mac OSX; it is built for Linux only.\n\nThe script will ask you a few questions and will setup everything you need\nto run KeyDB properly as a background daemon that will start again on\nsystem reboots.\n\nYou'll be able to stop and start KeyDB using the script named\n`/etc/init.d/keydb_<portnumber>`, for instance `/etc/init.d/keydb_6379`.\n\nMultithreading Architecture\n---------------------------\n\nKeyDB works by running the normal Redis event loop on multiple threads.  Network IO, and query parsing are done concurrently.  Each connection is assigned a thread on accept().  Access to the core hash table is guarded by spinlock.  Because the hashtable access is extremely fast this lock has low contention.  Transactions hold the lock for the duration of the EXEC command.  Modules work in concert with the GIL which is only acquired when all server threads are paused.  This maintains the atomicity guarantees modules expect.\n\nUnlike most databases the core data structure is the fastest part of the system.  Most of the query time comes from parsing the REPL protocol and copying data to/from the network.\n\n\nCode contributions\n-----------------\n\nNote: by contributing code to the KeyDB project in any form, including sending\na pull request via Github, a code fragment or patch via private email or\npublic discussion groups, you agree to release your code under the terms\nof the BSD license that you can find in the COPYING file included in the KeyDB\nsource distribution.\n\nPlease see the CONTRIBUTING file in this source distribution for more\ninformation.\n\n\n"
  },
  {
    "path": "TLS.md",
    "content": "TLS Support\n===========\n\nGetting Started\n---------------\n\n### Building\n\nTLS support is enabled in the default build. To build without TLS, run `make BUILD_TLS=no`.\n\n### Tests\n\nTo run KeyDB test suite with TLS, you'll need TLS support for TCL (i.e.\n`tcl-tls` package on Debian/Ubuntu).\n\n1. Run `./utils/gen-test-certs.sh` to generate a root CA and a server\n   certificate.\n\n2. Run `./runtest --tls` or `./runtest-cluster --tls` to run KeyDB and KeyDB\n   Cluster tests in TLS mode.\n\n### Running manually\n\nTo manually run a Redis server with TLS mode (assuming `gen-test-certs.sh` was\ninvoked so sample certificates/keys are available):\n\n    ./src/keydb-server --tls-port 6379 --port 0 \\\n        --tls-cert-file ./tests/tls/client.crt \\\n        --tls-key-file ./tests/tls/client.key \\\n        --tls-ca-cert-file ./tests/tls/ca.crt\n\nTo connect to this Redis server with `keydb-cli`:\n\n    ./src/keydb-cli --tls \\\n        --cert ./tests/tls/keydb.crt \\\n        --key ./tests/tls/keydb.key \\\n        --cacert ./tests/tls/ca.crt\n\nThis will disable TCP and enable TLS on port 6379. It's also possible to have\nboth TCP and TLS available, but you'll need to assign different ports.\n\nTo make a Replica connect to the master using TLS, use `--tls-replication yes`,\nand to make KeyDB Cluster use TLS across nodes use `--tls-cluster yes`.\n\nConnections\n-----------\n\nAll socket operations now go through a connection abstraction layer that hides\nI/O and read/write event handling from the caller.\n\nNote that unlike Redis, KeyDB fully supports multithreading of TLS connections.\n\nTo-Do List\n----------\n\n- [ ] keydb-benchmark support. The current implementation is a mix of using\n  hiredis for parsing and basic networking (establishing connections), but\n  directly manipulating sockets for most actions. This will need to be cleaned\n  up for proper TLS support. The best approach is probably to migrate to hiredis\n  async mode.\n- [ ] keydb-cli `--slave` and `--rdb` support.\n\nMulti-port\n----------\n\nConsider the implications of allowing TLS to be configured on a separate port,\nmaking KeyDB listening on multiple ports:\n\n1. Startup banner port notification\n2. Proctitle\n3. How slaves announce themselves\n4. Cluster bus port calculation\n"
  },
  {
    "path": "build.yaml",
    "content": "# Doc: https://wiki.sc-corp.net/pages/viewpage.action?pageId=121500284\nversion: 1\nmachamp:\n  keydb-build:\n    # Optional - build counter is linked to the build def\n    tag_template: \"0.0.%build.counter%-%sha%\"\n    # Optional - value in seconds before a build is terminated, default is 3600 seconds\n    timeout: 3600\n    # Optional - update ghe or not, default to true\n    update_ghe: true\n    code_coverage: false\n    # Required\n    steps:\n      make-build:\n        type: cmd\n        # https://github.sc-corp.net/Snapchat/img/tree/master/keydb/ubuntu-20-04\n        builder_image: us.gcr.io/snapchat-build-artifacts/prod/snapchat/img/keydb/keydb-ubuntu-20-04@sha256:cf869a3f5d1de1e1d976bb906689c37b7031938eb68661b844a38c532f27248c\n        command: ./machamp_scripts/build.sh\n      tls-test:\n        type: cmd\n        parent: make-build\n        # https://github.sc-corp.net/Snapchat/img/tree/master/keydb/ubuntu-20-04\n        builder_image: us.gcr.io/snapchat-build-artifacts/prod/snapchat/img/keydb/keydb-ubuntu-20-04@sha256:cf869a3f5d1de1e1d976bb906689c37b7031938eb68661b844a38c532f27248c\n        command: ./runtest --clients 4 --verbose --tls\n      cluster-test:\n        type: cmd\n        parent: make-build\n        # https://github.sc-corp.net/Snapchat/img/tree/master/keydb/ubuntu-20-04\n        builder_image: us.gcr.io/snapchat-build-artifacts/prod/snapchat/img/keydb/keydb-ubuntu-20-04@sha256:cf869a3f5d1de1e1d976bb906689c37b7031938eb68661b844a38c532f27248c\n        command: ./runtest-cluster --tls\n      sentinel-test:\n        type: cmd\n        parent: make-build\n        # https://github.sc-corp.net/Snapchat/img/tree/master/keydb/ubuntu-20-04\n        builder_image: us.gcr.io/snapchat-build-artifacts/prod/snapchat/img/keydb/keydb-ubuntu-20-04@sha256:cf869a3f5d1de1e1d976bb906689c37b7031938eb68661b844a38c532f27248c\n        command: ./runtest-sentinel\n      module-test:\n        type: cmd\n        parent: make-build\n        # https://github.sc-corp.net/Snapchat/img/tree/master/keydb/ubuntu-20-04\n        builder_image: us.gcr.io/snapchat-build-artifacts/prod/snapchat/img/keydb/keydb-ubuntu-20-04@sha256:cf869a3f5d1de1e1d976bb906689c37b7031938eb68661b844a38c532f27248c\n        command: ./runtest-moduleapi\n      rotation-test:\n        type: cmd\n        parent: make-build\n        # https://github.sc-corp.net/Snapchat/img/tree/master/keydb/ubuntu-20-04\n        builder_image: us.gcr.io/snapchat-build-artifacts/prod/snapchat/img/keydb/keydb-ubuntu-20-04@sha256:cf869a3f5d1de1e1d976bb906689c37b7031938eb68661b844a38c532f27248c\n        command: ./runtest-rotation\n  keydb-docker-build:\n    # Optional - build counter is linked to the build def\n    tag_template: \"%sha%\"\n    # Optional - value in seconds before a build is terminated, default is 3600 seconds\n    timeout: 3600\n    # Optional - update ghe or not, default to true\n    update_ghe: true\n    code_coverage: false\n    # Required\n    steps:\n      # to ensure a clearer docker build env\n      code-checkout:\n        type: cmd\n        command: echo checkout\n        # default machamp builder image does not work for multi arch\n        builder_image: us.gcr.io/snapchat-build-artifacts/prod/snapchat/img/ubuntu/ubuntu-23-04@sha256:bd43177a80e6ce1c3583e8ea959b88a9081c0f56b765ec9c5a157c27a637c23b\n      docker:\n        parent: code-checkout\n        type: docker # published images can be found in https://console.cloud.google.com/gcr/images/machamp-prod/global/keydb\n        dockerfile: machamp_scripts/Dockerfile\n        image_name: keydb # git commit sha will be deafult tag in the final image\n        workspace_context: ./ # This is the workspace context that your Dockerfile will use to move files around. <Root of checkout repository>/<Workspace Context>/<Dockerfile> If the workspace context is just the root of the repository, you can just use \"./\".\n"
  },
  {
    "path": "ci.yaml",
    "content": "# Doc: https://wiki.sc-corp.net/display/TOOL/ci.yaml+User+Guide\nversion: 1\non:\n  # https://wiki.sc-corp.net/display/TOOL/Onboard+Machamp+Build+By+ci.yaml+Configuration\n  # on pull_request is used for any pr build\n  pull_request:\n    - branches: ['!!main', '*'] # this branch pattern means any branch but not main branch will trigger this pr build\n      workflows:\n        # All builds that use machamp should use the defined `backend_workflow`\n        - workflow_type: backend_workflow\n          # references a build defined in build.yaml\n          build_name: keydb-build\n          arch_types: [\"amd64\", \"arm64\"]\n        - workflow_type: backend_workflow\n          # references a build defined in build.yaml\n          build_name: keydb-docker-build\n          arch_types: [\"amd64\", \"arm64\"]\n  # on push is used for release branch, meaning: trigger this build when there is commit pushed to this branch\n  push:\n    - branches: [main]\n      workflows:\n        - workflow_type: backend_workflow\n          build_name: keydb-build\n          arch_types: [\"amd64\", \"arm64\"]\n        - workflow_type: backend_workflow\n          # references a build defined in build.yaml\n          build_name: keydb-docker-build\n          arch_types: [\"amd64\", \"arm64\"]\n\n# below defines which branch is release branch / release tag\nmachamp:\n  releases:\n    # Note: machamp will only respect the ci.yaml file from default branch for \"release branch\" definition (most repositories using master/main as default branch)\n    # https://wiki.sc-corp.net/display/TOOL/Onboard+Machamp+Build+By+ci.yaml+Configuration\n    - branch_name: ^main$\n"
  },
  {
    "path": "deps/Makefile",
    "content": "# Redis dependency Makefile\n\nuname_S:= $(shell sh -c 'uname -s 2>/dev/null || echo not')\nuname_M := $(shell sh -c 'uname -m 2>/dev/null || echo not')\n\nCCCOLOR=\"\\033[34m\"\nLINKCOLOR=\"\\033[34;1m\"\nSRCCOLOR=\"\\033[33m\"\nBINCOLOR=\"\\033[37;1m\"\nMAKECOLOR=\"\\033[32;1m\"\nENDCOLOR=\"\\033[0m\"\n\ndefault:\n\t@echo \"Explicit target required\"\n\n.PHONY: default\n\n# Prerequisites target\n.make-prerequisites:\n\t@touch $@\n\n# Clean everything when CFLAGS is different\nifneq ($(shell sh -c '[ -f .make-cflags ] && cat .make-cflags || echo none'), $(CFLAGS))\n.make-cflags: distclean\n\t-(echo \"$(CFLAGS)\" > .make-cflags)\n.make-prerequisites: .make-cflags\nendif\n\n# Clean everything when LDFLAGS is different\nifneq ($(shell sh -c '[ -f .make-ldflags ] && cat .make-ldflags || echo none'), $(LDFLAGS))\n.make-ldflags: distclean\n\t-(echo \"$(LDFLAGS)\" > .make-ldflags)\n.make-prerequisites: .make-ldflags\nendif\n\ndistclean:\nifneq ($(USE_SYSTEM_HIREDIS),yes)\n\t-(cd hiredis && $(MAKE) clean) > /dev/null || true\nendif\n\t-(cd linenoise && $(MAKE) clean) > /dev/null || true\n\t-(cd lua && $(MAKE) clean) > /dev/null || true\nifneq ($(USE_SYSTEM_JEMALLOC),yes)\n\t-(cd jemalloc && [ -f Makefile ] && $(MAKE) distclean) > /dev/null || true\nendif\nifneq ($(USE_SYSTEM_ROCKSDB),yes)\n\t-(cd rocksdb && $(MAKE) clean) > /dev/null || true\nendif\n\t-(cd hdr_histogram && $(MAKE) clean) > /dev/null || true\n\t-(rm -f .make-*)\n\n.PHONY: distclean\n\nifeq ($(BUILD_TLS),yes)\n    HIREDIS_MAKE_FLAGS = USE_SSL=1\nendif\n\nhiredis: .make-prerequisites\n\t@printf '%b %b\\n' $(MAKECOLOR)MAKE$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR)\n\tcd hiredis && $(MAKE) static $(HIREDIS_MAKE_FLAGS)\n\n.PHONY: hiredis\n\nlinenoise: .make-prerequisites\n\t@printf '%b %b\\n' $(MAKECOLOR)MAKE$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR)\n\tcd linenoise && $(MAKE)\n\n.PHONY: linenoise\n\n.PHONY: memkind\nmemkind:\n\tcd memkind && $(MAKE)\n\n\nhdr_histogram: .make-prerequisites\n\t@printf '%b %b\\n' $(MAKECOLOR)MAKE$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR)\n\tcd hdr_histogram && $(MAKE)\n\n.PHONY: hdr_histogram\n\nifeq ($(uname_S),SunOS)\n\t# Make isinf() available\n\tLUA_CFLAGS= -D__C99FEATURES__=1\nendif\n\nLUA_CFLAGS+= -O2 -Wall -DLUA_ANSI -DENABLE_CJSON_GLOBAL -DREDIS_STATIC='' -DLUA_USE_MKSTEMP $(CFLAGS)\nLUA_LDFLAGS+= $(LDFLAGS)\n# lua's Makefile defines AR=\"ar rcu\", which is unusual, and makes it more\n# challenging to cross-compile lua (and redis).  These defines make it easier\n# to fit redis into cross-compilation environments, which typically set AR.\nAR=ar\nARFLAGS=rc\n\nlua: .make-prerequisites\n\t@printf '%b %b\\n' $(MAKECOLOR)MAKE$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR)\n\tcd lua/src && $(MAKE) all CFLAGS=\"$(LUA_CFLAGS)\" MYLDFLAGS=\"$(LUA_LDFLAGS)\" AR=\"$(AR) $(ARFLAGS)\"\n\n.PHONY: lua\n\nJEMALLOC_CFLAGS= -std=gnu99 -Wall -pipe -g3 -O3 -funroll-loops $(CFLAGS)\nJEMALLOC_LDFLAGS= $(LDFLAGS)\n\njemalloc: .make-prerequisites\n\t@printf '%b %b\\n' $(MAKECOLOR)MAKE$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR)\n\tcd jemalloc && ./configure --with-version=5.2.1-0-g0 --with-lg-quantum=3 --disable-cxx CFLAGS=\"$(JEMALLOC_CFLAGS)\" LDFLAGS=\"$(JEMALLOC_LDFLAGS)\"\n\tcd jemalloc && $(MAKE) CFLAGS=\"$(JEMALLOC_CFLAGS)\" LDFLAGS=\"$(JEMALLOC_LDFLAGS)\" lib/libjemalloc.a\n\n.PHONY: jemalloc\n\nrocksdb: .make-prerequisites\n\t@printf '%b %b\\n' $(MAKECOLOR)MAKE$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR)\nifeq ($(uname_M),x86_64)\n\tcd rocksdb && CFLAGS=-Wno-error PORTABLE=1 USE_SSE=1 FORCE_SSE42=1 $(MAKE) static_lib\nelse\n\tcd rocksdb && PORTABLE=1 $(MAKE) static_lib\nendif\n\n.PHONY: rocksdb\n"
  },
  {
    "path": "deps/README.md",
    "content": "This directory contains all Redis dependencies, except for the libc that\nshould be provided by the operating system.\n\n* **Jemalloc** is our memory allocator, used as replacement for libc malloc on Linux by default. It has good performances and excellent fragmentation behavior. This component is upgraded from time to time.\n* **hiredis** is the official C client library for Redis. It is used by redis-cli, redis-benchmark and Redis Sentinel. It is part of the Redis official ecosystem but is developed externally from the Redis repository, so we just upgrade it as needed.\n* **linenoise** is a readline replacement. It is developed by the same authors of Redis but is managed as a separated project and updated as needed.\n* **lua** is Lua 5.1 with minor changes for security and additional libraries.\n\nHow to upgrade the above dependencies\n===\n\nJemalloc\n---\n\nJemalloc is modified with changes that allow us to implement the Redis\nactive defragmentation logic. However this feature of Redis is not mandatory\nand Redis is able to understand if the Jemalloc version it is compiled\nagainst supports such Redis-specific modifications. So in theory, if you\nare not interested in the active defragmentation, you can replace Jemalloc\njust following these steps:\n\n1. Remove the jemalloc directory.\n2. Substitute it with the new jemalloc source tree.\n3. Edit the Makefile located in the same directory as the README you are\n   reading, and change the --with-version in the Jemalloc configure script\n   options with the version you are using. This is required because otherwise\n   Jemalloc configuration script is broken and will not work nested in another\n   git repository.\n\nHowever note that we change Jemalloc settings via the `configure` script of Jemalloc using the `--with-lg-quantum` option, setting it to the value of 3 instead of 4. This provides us with more size classes that better suit the Redis data structures, in order to gain memory efficiency.\n\nIf you want to upgrade Jemalloc while also providing support for\nactive defragmentation, in addition to the above steps you need to perform\nthe following additional steps:\n\n5. In Jemalloc tree, file `include/jemalloc/jemalloc_macros.h.in`, make sure\n   to add `#define JEMALLOC_FRAG_HINT`.\n6. Implement the function `je_get_defrag_hint()` inside `src/jemalloc.c`. You\n   can see how it is implemented in the current Jemalloc source tree shipped\n   with Redis, and rewrite it according to the new Jemalloc internals, if they\n   changed, otherwise you could just copy the old implementation if you are\n   upgrading just to a similar version of Jemalloc.\n\nHiredis\n---\n\nHiredis uses the SDS string library, that must be the same version used inside Redis itself. Hiredis is also very critical for Sentinel. Historically Redis often used forked versions of hiredis in a way or the other. In order to upgrade it is advised to take a lot of care:\n\n1. Check with diff if hiredis API changed and what impact it could have in Redis.\n2. Make sure that the SDS library inside Hiredis and inside Redis are compatible.\n3. After the upgrade, run the Redis Sentinel test.\n4. Check manually that redis-cli and redis-benchmark behave as expected, since we have no tests for CLI utilities currently.\n\nLinenoise\n---\n\nLinenoise is rarely upgraded as needed. The upgrade process is trivial since\nRedis uses a non modified version of linenoise, so to upgrade just do the\nfollowing:\n\n1. Remove the linenoise directory.\n2. Substitute it with the new linenoise source tree.\n\nLua\n---\n\nWe use Lua 5.1 and no upgrade is planned currently, since we don't want to break\nLua scripts for new Lua features: in the context of Redis Lua scripts the\ncapabilities of 5.1 are usually more than enough, the release is rock solid,\nand we definitely don't want to break old scripts.\n\nSo upgrading of Lua is up to the Redis project maintainers and should be a\nmanual procedure performed by taking a diff between the different versions.\n\nCurrently we have at least the following differences between official Lua 5.1\nand our version:\n\n1. Makefile is modified to allow a different compiler than GCC.\n2. We have the implementation source code, and directly link to the following external libraries: `lua_cjson.o`, `lua_struct.o`, `lua_cmsgpack.o` and `lua_bit.o`.\n3. There is a security fix in `ldo.c`, line 498: The check for `LUA_SIGNATURE[0]` is removed in order to avoid direct bytecode execution.\n\n\n"
  },
  {
    "path": "deps/concurrentqueue/blockingconcurrentqueue.h",
    "content": "// Provides an efficient blocking version of moodycamel::ConcurrentQueue.\n// ©2015-2020 Cameron Desrochers. Distributed under the terms of the simplified\n// BSD license, available at the top of concurrentqueue.h.\n// Also dual-licensed under the Boost Software License (see LICENSE.md)\n// Uses Jeff Preshing's semaphore implementation (under the terms of its\n// separate zlib license, see lightweightsemaphore.h).\n\n#pragma once\n\n#include \"concurrentqueue.h\"\n#include \"lightweightsemaphore.h\"\n\n#include <type_traits>\n#include <cerrno>\n#include <memory>\n#include <chrono>\n#include <ctime>\n\nnamespace moodycamel\n{\n// This is a blocking version of the queue. It has an almost identical interface to\n// the normal non-blocking version, with the addition of various wait_dequeue() methods\n// and the removal of producer-specific dequeue methods.\ntemplate<typename T, typename Traits = ConcurrentQueueDefaultTraits>\nclass BlockingConcurrentQueue\n{\nprivate:\n\ttypedef ::moodycamel::ConcurrentQueue<T, Traits> ConcurrentQueue;\n\ttypedef ::moodycamel::LightweightSemaphore LightweightSemaphore;\n\npublic:\n\ttypedef typename ConcurrentQueue::producer_token_t producer_token_t;\n\ttypedef typename ConcurrentQueue::consumer_token_t consumer_token_t;\n\t\n\ttypedef typename ConcurrentQueue::index_t index_t;\n\ttypedef typename ConcurrentQueue::size_t size_t;\n\ttypedef typename std::make_signed<size_t>::type ssize_t;\n\t\n\tstatic const size_t BLOCK_SIZE = ConcurrentQueue::BLOCK_SIZE;\n\tstatic const size_t EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD = ConcurrentQueue::EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD;\n\tstatic const size_t EXPLICIT_INITIAL_INDEX_SIZE = ConcurrentQueue::EXPLICIT_INITIAL_INDEX_SIZE;\n\tstatic const size_t IMPLICIT_INITIAL_INDEX_SIZE = ConcurrentQueue::IMPLICIT_INITIAL_INDEX_SIZE;\n\tstatic const size_t INITIAL_IMPLICIT_PRODUCER_HASH_SIZE = ConcurrentQueue::INITIAL_IMPLICIT_PRODUCER_HASH_SIZE;\n\tstatic const std::uint32_t EXPLICIT_CONSUMER_CONSUMPTION_QUOTA_BEFORE_ROTATE = ConcurrentQueue::EXPLICIT_CONSUMER_CONSUMPTION_QUOTA_BEFORE_ROTATE;\n\tstatic const size_t MAX_SUBQUEUE_SIZE = ConcurrentQueue::MAX_SUBQUEUE_SIZE;\n\t\npublic:\n\t// Creates a queue with at least `capacity` element slots; note that the\n\t// actual number of elements that can be inserted without additional memory\n\t// allocation depends on the number of producers and the block size (e.g. if\n\t// the block size is equal to `capacity`, only a single block will be allocated\n\t// up-front, which means only a single producer will be able to enqueue elements\n\t// without an extra allocation -- blocks aren't shared between producers).\n\t// This method is not thread safe -- it is up to the user to ensure that the\n\t// queue is fully constructed before it starts being used by other threads (this\n\t// includes making the memory effects of construction visible, possibly with a\n\t// memory barrier).\n\texplicit BlockingConcurrentQueue(size_t capacity = 6 * BLOCK_SIZE)\n\t\t: inner(capacity), sema(create<LightweightSemaphore, ssize_t, int>(0, (int)Traits::MAX_SEMA_SPINS), &BlockingConcurrentQueue::template destroy<LightweightSemaphore>)\n\t{\n\t\tassert(reinterpret_cast<ConcurrentQueue*>((BlockingConcurrentQueue*)1) == &((BlockingConcurrentQueue*)1)->inner && \"BlockingConcurrentQueue must have ConcurrentQueue as its first member\");\n\t\tif (!sema) {\n\t\t\tMOODYCAMEL_THROW(std::bad_alloc());\n\t\t}\n\t}\n\t\n\tBlockingConcurrentQueue(size_t minCapacity, size_t maxExplicitProducers, size_t maxImplicitProducers)\n\t\t: inner(minCapacity, maxExplicitProducers, maxImplicitProducers), sema(create<LightweightSemaphore, ssize_t, int>(0, (int)Traits::MAX_SEMA_SPINS), &BlockingConcurrentQueue::template destroy<LightweightSemaphore>)\n\t{\n\t\tassert(reinterpret_cast<ConcurrentQueue*>((BlockingConcurrentQueue*)1) == &((BlockingConcurrentQueue*)1)->inner && \"BlockingConcurrentQueue must have ConcurrentQueue as its first member\");\n\t\tif (!sema) {\n\t\t\tMOODYCAMEL_THROW(std::bad_alloc());\n\t\t}\n\t}\n\t\n\t// Disable copying and copy assignment\n\tBlockingConcurrentQueue(BlockingConcurrentQueue const&) MOODYCAMEL_DELETE_FUNCTION;\n\tBlockingConcurrentQueue& operator=(BlockingConcurrentQueue const&) MOODYCAMEL_DELETE_FUNCTION;\n\t\n\t// Moving is supported, but note that it is *not* a thread-safe operation.\n\t// Nobody can use the queue while it's being moved, and the memory effects\n\t// of that move must be propagated to other threads before they can use it.\n\t// Note: When a queue is moved, its tokens are still valid but can only be\n\t// used with the destination queue (i.e. semantically they are moved along\n\t// with the queue itself).\n\tBlockingConcurrentQueue(BlockingConcurrentQueue&& other) MOODYCAMEL_NOEXCEPT\n\t\t: inner(std::move(other.inner)), sema(std::move(other.sema))\n\t{ }\n\t\n\tinline BlockingConcurrentQueue& operator=(BlockingConcurrentQueue&& other) MOODYCAMEL_NOEXCEPT\n\t{\n\t\treturn swap_internal(other);\n\t}\n\t\n\t// Swaps this queue's state with the other's. Not thread-safe.\n\t// Swapping two queues does not invalidate their tokens, however\n\t// the tokens that were created for one queue must be used with\n\t// only the swapped queue (i.e. the tokens are tied to the\n\t// queue's movable state, not the object itself).\n\tinline void swap(BlockingConcurrentQueue& other) MOODYCAMEL_NOEXCEPT\n\t{\n\t\tswap_internal(other);\n\t}\n\t\nprivate:\n\tBlockingConcurrentQueue& swap_internal(BlockingConcurrentQueue& other)\n\t{\n\t\tif (this == &other) {\n\t\t\treturn *this;\n\t\t}\n\t\t\n\t\tinner.swap(other.inner);\n\t\tsema.swap(other.sema);\n\t\treturn *this;\n\t}\n\t\npublic:\n\t// Enqueues a single item (by copying it).\n\t// Allocates memory if required. Only fails if memory allocation fails (or implicit\n\t// production is disabled because Traits::INITIAL_IMPLICIT_PRODUCER_HASH_SIZE is 0,\n\t// or Traits::MAX_SUBQUEUE_SIZE has been defined and would be surpassed).\n\t// Thread-safe.\n\tinline bool enqueue(T const& item)\n\t{\n\t\tif ((details::likely)(inner.enqueue(item))) {\n\t\t\tsema->signal();\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n\t\n\t// Enqueues a single item (by moving it, if possible).\n\t// Allocates memory if required. Only fails if memory allocation fails (or implicit\n\t// production is disabled because Traits::INITIAL_IMPLICIT_PRODUCER_HASH_SIZE is 0,\n\t// or Traits::MAX_SUBQUEUE_SIZE has been defined and would be surpassed).\n\t// Thread-safe.\n\tinline bool enqueue(T&& item)\n\t{\n\t\tif ((details::likely)(inner.enqueue(std::move(item)))) {\n\t\t\tsema->signal();\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n\t\n\t// Enqueues a single item (by copying it) using an explicit producer token.\n\t// Allocates memory if required. Only fails if memory allocation fails (or\n\t// Traits::MAX_SUBQUEUE_SIZE has been defined and would be surpassed).\n\t// Thread-safe.\n\tinline bool enqueue(producer_token_t const& token, T const& item)\n\t{\n\t\tif ((details::likely)(inner.enqueue(token, item))) {\n\t\t\tsema->signal();\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n\t\n\t// Enqueues a single item (by moving it, if possible) using an explicit producer token.\n\t// Allocates memory if required. Only fails if memory allocation fails (or\n\t// Traits::MAX_SUBQUEUE_SIZE has been defined and would be surpassed).\n\t// Thread-safe.\n\tinline bool enqueue(producer_token_t const& token, T&& item)\n\t{\n\t\tif ((details::likely)(inner.enqueue(token, std::move(item)))) {\n\t\t\tsema->signal();\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n\t\n\t// Enqueues several items.\n\t// Allocates memory if required. Only fails if memory allocation fails (or\n\t// implicit production is disabled because Traits::INITIAL_IMPLICIT_PRODUCER_HASH_SIZE\n\t// is 0, or Traits::MAX_SUBQUEUE_SIZE has been defined and would be surpassed).\n\t// Note: Use std::make_move_iterator if the elements should be moved instead of copied.\n\t// Thread-safe.\n\ttemplate<typename It>\n\tinline bool enqueue_bulk(It itemFirst, size_t count)\n\t{\n\t\tif ((details::likely)(inner.enqueue_bulk(std::forward<It>(itemFirst), count))) {\n\t\t\tsema->signal((LightweightSemaphore::ssize_t)(ssize_t)count);\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n\t\n\t// Enqueues several items using an explicit producer token.\n\t// Allocates memory if required. Only fails if memory allocation fails\n\t// (or Traits::MAX_SUBQUEUE_SIZE has been defined and would be surpassed).\n\t// Note: Use std::make_move_iterator if the elements should be moved\n\t// instead of copied.\n\t// Thread-safe.\n\ttemplate<typename It>\n\tinline bool enqueue_bulk(producer_token_t const& token, It itemFirst, size_t count)\n\t{\n\t\tif ((details::likely)(inner.enqueue_bulk(token, std::forward<It>(itemFirst), count))) {\n\t\t\tsema->signal((LightweightSemaphore::ssize_t)(ssize_t)count);\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n\t\n\t// Enqueues a single item (by copying it).\n\t// Does not allocate memory. Fails if not enough room to enqueue (or implicit\n\t// production is disabled because Traits::INITIAL_IMPLICIT_PRODUCER_HASH_SIZE\n\t// is 0).\n\t// Thread-safe.\n\tinline bool try_enqueue(T const& item)\n\t{\n\t\tif (inner.try_enqueue(item)) {\n\t\t\tsema->signal();\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n\t\n\t// Enqueues a single item (by moving it, if possible).\n\t// Does not allocate memory (except for one-time implicit producer).\n\t// Fails if not enough room to enqueue (or implicit production is\n\t// disabled because Traits::INITIAL_IMPLICIT_PRODUCER_HASH_SIZE is 0).\n\t// Thread-safe.\n\tinline bool try_enqueue(T&& item)\n\t{\n\t\tif (inner.try_enqueue(std::move(item))) {\n\t\t\tsema->signal();\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n\t\n\t// Enqueues a single item (by copying it) using an explicit producer token.\n\t// Does not allocate memory. Fails if not enough room to enqueue.\n\t// Thread-safe.\n\tinline bool try_enqueue(producer_token_t const& token, T const& item)\n\t{\n\t\tif (inner.try_enqueue(token, item)) {\n\t\t\tsema->signal();\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n\t\n\t// Enqueues a single item (by moving it, if possible) using an explicit producer token.\n\t// Does not allocate memory. Fails if not enough room to enqueue.\n\t// Thread-safe.\n\tinline bool try_enqueue(producer_token_t const& token, T&& item)\n\t{\n\t\tif (inner.try_enqueue(token, std::move(item))) {\n\t\t\tsema->signal();\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n\t\n\t// Enqueues several items.\n\t// Does not allocate memory (except for one-time implicit producer).\n\t// Fails if not enough room to enqueue (or implicit production is\n\t// disabled because Traits::INITIAL_IMPLICIT_PRODUCER_HASH_SIZE is 0).\n\t// Note: Use std::make_move_iterator if the elements should be moved\n\t// instead of copied.\n\t// Thread-safe.\n\ttemplate<typename It>\n\tinline bool try_enqueue_bulk(It itemFirst, size_t count)\n\t{\n\t\tif (inner.try_enqueue_bulk(std::forward<It>(itemFirst), count)) {\n\t\t\tsema->signal((LightweightSemaphore::ssize_t)(ssize_t)count);\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n\t\n\t// Enqueues several items using an explicit producer token.\n\t// Does not allocate memory. Fails if not enough room to enqueue.\n\t// Note: Use std::make_move_iterator if the elements should be moved\n\t// instead of copied.\n\t// Thread-safe.\n\ttemplate<typename It>\n\tinline bool try_enqueue_bulk(producer_token_t const& token, It itemFirst, size_t count)\n\t{\n\t\tif (inner.try_enqueue_bulk(token, std::forward<It>(itemFirst), count)) {\n\t\t\tsema->signal((LightweightSemaphore::ssize_t)(ssize_t)count);\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n\t\n\t\n\t// Attempts to dequeue from the queue.\n\t// Returns false if all producer streams appeared empty at the time they\n\t// were checked (so, the queue is likely but not guaranteed to be empty).\n\t// Never allocates. Thread-safe.\n\ttemplate<typename U>\n\tinline bool try_dequeue(U& item)\n\t{\n\t\tif (sema->tryWait()) {\n\t\t\twhile (!inner.try_dequeue(item)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n\t\n\t// Attempts to dequeue from the queue using an explicit consumer token.\n\t// Returns false if all producer streams appeared empty at the time they\n\t// were checked (so, the queue is likely but not guaranteed to be empty).\n\t// Never allocates. Thread-safe.\n\ttemplate<typename U>\n\tinline bool try_dequeue(consumer_token_t& token, U& item)\n\t{\n\t\tif (sema->tryWait()) {\n\t\t\twhile (!inner.try_dequeue(token, item)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n\t\n\t// Attempts to dequeue several elements from the queue.\n\t// Returns the number of items actually dequeued.\n\t// Returns 0 if all producer streams appeared empty at the time they\n\t// were checked (so, the queue is likely but not guaranteed to be empty).\n\t// Never allocates. Thread-safe.\n\ttemplate<typename It>\n\tinline size_t try_dequeue_bulk(It itemFirst, size_t max)\n\t{\n\t\tsize_t count = 0;\n\t\tmax = (size_t)sema->tryWaitMany((LightweightSemaphore::ssize_t)(ssize_t)max);\n\t\twhile (count != max) {\n\t\t\tcount += inner.template try_dequeue_bulk<It&>(itemFirst, max - count);\n\t\t}\n\t\treturn count;\n\t}\n\t\n\t// Attempts to dequeue several elements from the queue using an explicit consumer token.\n\t// Returns the number of items actually dequeued.\n\t// Returns 0 if all producer streams appeared empty at the time they\n\t// were checked (so, the queue is likely but not guaranteed to be empty).\n\t// Never allocates. Thread-safe.\n\ttemplate<typename It>\n\tinline size_t try_dequeue_bulk(consumer_token_t& token, It itemFirst, size_t max)\n\t{\n\t\tsize_t count = 0;\n\t\tmax = (size_t)sema->tryWaitMany((LightweightSemaphore::ssize_t)(ssize_t)max);\n\t\twhile (count != max) {\n\t\t\tcount += inner.template try_dequeue_bulk<It&>(token, itemFirst, max - count);\n\t\t}\n\t\treturn count;\n\t}\n\t\n\t\n\t\n\t// Blocks the current thread until there's something to dequeue, then\n\t// dequeues it.\n\t// Never allocates. Thread-safe.\n\ttemplate<typename U>\n\tinline void wait_dequeue(U& item)\n\t{\n\t\twhile (!sema->wait()) {\n\t\t\tcontinue;\n\t\t}\n\t\twhile (!inner.try_dequeue(item)) {\n\t\t\tcontinue;\n\t\t}\n\t}\n\n\t// Blocks the current thread until either there's something to dequeue\n\t// or the timeout (specified in microseconds) expires. Returns false\n\t// without setting `item` if the timeout expires, otherwise assigns\n\t// to `item` and returns true.\n\t// Using a negative timeout indicates an indefinite timeout,\n\t// and is thus functionally equivalent to calling wait_dequeue.\n\t// Never allocates. Thread-safe.\n\ttemplate<typename U>\n\tinline bool wait_dequeue_timed(U& item, std::int64_t timeout_usecs)\n\t{\n\t\tif (!sema->wait(timeout_usecs)) {\n\t\t\treturn false;\n\t\t}\n\t\twhile (!inner.try_dequeue(item)) {\n\t\t\tcontinue;\n\t\t}\n\t\treturn true;\n\t}\n    \n    // Blocks the current thread until either there's something to dequeue\n\t// or the timeout expires. Returns false without setting `item` if the\n    // timeout expires, otherwise assigns to `item` and returns true.\n\t// Never allocates. Thread-safe.\n\ttemplate<typename U, typename Rep, typename Period>\n\tinline bool wait_dequeue_timed(U& item, std::chrono::duration<Rep, Period> const& timeout)\n    {\n        return wait_dequeue_timed(item, std::chrono::duration_cast<std::chrono::microseconds>(timeout).count());\n    }\n\t\n\t// Blocks the current thread until there's something to dequeue, then\n\t// dequeues it using an explicit consumer token.\n\t// Never allocates. Thread-safe.\n\ttemplate<typename U>\n\tinline void wait_dequeue(consumer_token_t& token, U& item)\n\t{\n\t\twhile (!sema->wait()) {\n\t\t\tcontinue;\n\t\t}\n\t\twhile (!inner.try_dequeue(token, item)) {\n\t\t\tcontinue;\n\t\t}\n\t}\n\t\n\t// Blocks the current thread until either there's something to dequeue\n\t// or the timeout (specified in microseconds) expires. Returns false\n\t// without setting `item` if the timeout expires, otherwise assigns\n\t// to `item` and returns true.\n\t// Using a negative timeout indicates an indefinite timeout,\n\t// and is thus functionally equivalent to calling wait_dequeue.\n\t// Never allocates. Thread-safe.\n\ttemplate<typename U>\n\tinline bool wait_dequeue_timed(consumer_token_t& token, U& item, std::int64_t timeout_usecs)\n\t{\n\t\tif (!sema->wait(timeout_usecs)) {\n\t\t\treturn false;\n\t\t}\n\t\twhile (!inner.try_dequeue(token, item)) {\n\t\t\tcontinue;\n\t\t}\n\t\treturn true;\n\t}\n    \n    // Blocks the current thread until either there's something to dequeue\n\t// or the timeout expires. Returns false without setting `item` if the\n    // timeout expires, otherwise assigns to `item` and returns true.\n\t// Never allocates. Thread-safe.\n\ttemplate<typename U, typename Rep, typename Period>\n\tinline bool wait_dequeue_timed(consumer_token_t& token, U& item, std::chrono::duration<Rep, Period> const& timeout)\n    {\n        return wait_dequeue_timed(token, item, std::chrono::duration_cast<std::chrono::microseconds>(timeout).count());\n    }\n\t\n\t// Attempts to dequeue several elements from the queue.\n\t// Returns the number of items actually dequeued, which will\n\t// always be at least one (this method blocks until the queue\n\t// is non-empty) and at most max.\n\t// Never allocates. Thread-safe.\n\ttemplate<typename It>\n\tinline size_t wait_dequeue_bulk(It itemFirst, size_t max)\n\t{\n\t\tsize_t count = 0;\n\t\tmax = (size_t)sema->waitMany((LightweightSemaphore::ssize_t)(ssize_t)max);\n\t\twhile (count != max) {\n\t\t\tcount += inner.template try_dequeue_bulk<It&>(itemFirst, max - count);\n\t\t}\n\t\treturn count;\n\t}\n\t\n\t// Attempts to dequeue several elements from the queue.\n\t// Returns the number of items actually dequeued, which can\n\t// be 0 if the timeout expires while waiting for elements,\n\t// and at most max.\n\t// Using a negative timeout indicates an indefinite timeout,\n\t// and is thus functionally equivalent to calling wait_dequeue_bulk.\n\t// Never allocates. Thread-safe.\n\ttemplate<typename It>\n\tinline size_t wait_dequeue_bulk_timed(It itemFirst, size_t max, std::int64_t timeout_usecs)\n\t{\n\t\tsize_t count = 0;\n\t\tmax = (size_t)sema->waitMany((LightweightSemaphore::ssize_t)(ssize_t)max, timeout_usecs);\n\t\twhile (count != max) {\n\t\t\tcount += inner.template try_dequeue_bulk<It&>(itemFirst, max - count);\n\t\t}\n\t\treturn count;\n\t}\n    \n    // Attempts to dequeue several elements from the queue.\n\t// Returns the number of items actually dequeued, which can\n\t// be 0 if the timeout expires while waiting for elements,\n\t// and at most max.\n\t// Never allocates. Thread-safe.\n\ttemplate<typename It, typename Rep, typename Period>\n\tinline size_t wait_dequeue_bulk_timed(It itemFirst, size_t max, std::chrono::duration<Rep, Period> const& timeout)\n    {\n        return wait_dequeue_bulk_timed<It&>(itemFirst, max, std::chrono::duration_cast<std::chrono::microseconds>(timeout).count());\n    }\n\t\n\t// Attempts to dequeue several elements from the queue using an explicit consumer token.\n\t// Returns the number of items actually dequeued, which will\n\t// always be at least one (this method blocks until the queue\n\t// is non-empty) and at most max.\n\t// Never allocates. Thread-safe.\n\ttemplate<typename It>\n\tinline size_t wait_dequeue_bulk(consumer_token_t& token, It itemFirst, size_t max)\n\t{\n\t\tsize_t count = 0;\n\t\tmax = (size_t)sema->waitMany((LightweightSemaphore::ssize_t)(ssize_t)max);\n\t\twhile (count != max) {\n\t\t\tcount += inner.template try_dequeue_bulk<It&>(token, itemFirst, max - count);\n\t\t}\n\t\treturn count;\n\t}\n\t\n\t// Attempts to dequeue several elements from the queue using an explicit consumer token.\n\t// Returns the number of items actually dequeued, which can\n\t// be 0 if the timeout expires while waiting for elements,\n\t// and at most max.\n\t// Using a negative timeout indicates an indefinite timeout,\n\t// and is thus functionally equivalent to calling wait_dequeue_bulk.\n\t// Never allocates. Thread-safe.\n\ttemplate<typename It>\n\tinline size_t wait_dequeue_bulk_timed(consumer_token_t& token, It itemFirst, size_t max, std::int64_t timeout_usecs)\n\t{\n\t\tsize_t count = 0;\n\t\tmax = (size_t)sema->waitMany((LightweightSemaphore::ssize_t)(ssize_t)max, timeout_usecs);\n\t\twhile (count != max) {\n\t\t\tcount += inner.template try_dequeue_bulk<It&>(token, itemFirst, max - count);\n\t\t}\n\t\treturn count;\n\t}\n\t\n\t// Attempts to dequeue several elements from the queue using an explicit consumer token.\n\t// Returns the number of items actually dequeued, which can\n\t// be 0 if the timeout expires while waiting for elements,\n\t// and at most max.\n\t// Never allocates. Thread-safe.\n\ttemplate<typename It, typename Rep, typename Period>\n\tinline size_t wait_dequeue_bulk_timed(consumer_token_t& token, It itemFirst, size_t max, std::chrono::duration<Rep, Period> const& timeout)\n    {\n        return wait_dequeue_bulk_timed<It&>(token, itemFirst, max, std::chrono::duration_cast<std::chrono::microseconds>(timeout).count());\n    }\n\t\n\t\n\t// Returns an estimate of the total number of elements currently in the queue. This\n\t// estimate is only accurate if the queue has completely stabilized before it is called\n\t// (i.e. all enqueue and dequeue operations have completed and their memory effects are\n\t// visible on the calling thread, and no further operations start while this method is\n\t// being called).\n\t// Thread-safe.\n\tinline size_t size_approx() const\n\t{\n\t\treturn (size_t)sema->availableApprox();\n\t}\n\t\n\t\n\t// Returns true if the underlying atomic variables used by\n\t// the queue are lock-free (they should be on most platforms).\n\t// Thread-safe.\n\tstatic bool is_lock_free()\n\t{\n\t\treturn ConcurrentQueue::is_lock_free();\n\t}\n\t\n\nprivate:\n\ttemplate<typename U, typename A1, typename A2>\n\tstatic inline U* create(A1&& a1, A2&& a2)\n\t{\n\t\tvoid* p = (Traits::malloc)(sizeof(U));\n\t\treturn p != nullptr ? new (p) U(std::forward<A1>(a1), std::forward<A2>(a2)) : nullptr;\n\t}\n\t\n\ttemplate<typename U>\n\tstatic inline void destroy(U* p)\n\t{\n\t\tif (p != nullptr) {\n\t\t\tp->~U();\n\t\t}\n\t\t(Traits::free)(p);\n\t}\n\t\nprivate:\n\tConcurrentQueue inner;\n\tstd::unique_ptr<LightweightSemaphore, void (*)(LightweightSemaphore*)> sema;\n};\n\n\ntemplate<typename T, typename Traits>\ninline void swap(BlockingConcurrentQueue<T, Traits>& a, BlockingConcurrentQueue<T, Traits>& b) MOODYCAMEL_NOEXCEPT\n{\n\ta.swap(b);\n}\n\n}\t// end namespace moodycamel\n\n"
  },
  {
    "path": "deps/concurrentqueue/concurrentqueue.h",
    "content": "// Provides a C++11 implementation of a multi-producer, multi-consumer lock-free queue.\n// An overview, including benchmark results, is provided here:\n//     http://moodycamel.com/blog/2014/a-fast-general-purpose-lock-free-queue-for-c++\n// The full design is also described in excruciating detail at:\n//    http://moodycamel.com/blog/2014/detailed-design-of-a-lock-free-queue\n\n// Simplified BSD license:\n// Copyright (c) 2013-2020, Cameron Desrochers.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without modification,\n// are permitted provided that the following conditions are met:\n//\n// - Redistributions of source code must retain the above copyright notice, this list of\n// conditions and the following disclaimer.\n// - Redistributions in binary form must reproduce the above copyright notice, this list of\n// conditions and the following disclaimer in the documentation and/or other materials\n// provided with the distribution.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY\n// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL\n// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT\n// OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR\n// TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,\n// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\n// Also dual-licensed under the Boost Software License (see LICENSE.md)\n\n#pragma once\n\n#if defined(__GNUC__) && !defined(__INTEL_COMPILER)\n// Disable -Wconversion warnings (spuriously triggered when Traits::size_t and\n// Traits::index_t are set to < 32 bits, causing integer promotion, causing warnings\n// upon assigning any computed values)\n#pragma GCC diagnostic push\n#pragma GCC diagnostic ignored \"-Wconversion\"\n\n#ifdef MCDBGQ_USE_RELACY\n#pragma GCC diagnostic ignored \"-Wint-to-pointer-cast\"\n#endif\n#endif\n\n#if defined(_MSC_VER) && (!defined(_HAS_CXX17) || !_HAS_CXX17)\n// VS2019 with /W4 warns about constant conditional expressions but unless /std=c++17 or higher\n// does not support `if constexpr`, so we have no choice but to simply disable the warning\n#pragma warning(push)\n#pragma warning(disable: 4127)  // conditional expression is constant\n#endif\n\n#if defined(__APPLE__)\n#include \"TargetConditionals.h\"\n#endif\n\n#ifdef MCDBGQ_USE_RELACY\n#include \"relacy/relacy_std.hpp\"\n#include \"relacy_shims.h\"\n// We only use malloc/free anyway, and the delete macro messes up `= delete` method declarations.\n// We'll override the default trait malloc ourselves without a macro.\n#undef new\n#undef delete\n#undef malloc\n#undef free\n#else\n#include <atomic>\t\t// Requires C++11. Sorry VS2010.\n#include <cassert>\n#endif\n#include <cstddef>              // for max_align_t\n#include <cstdint>\n#include <cstdlib>\n#include <type_traits>\n#include <algorithm>\n#include <utility>\n#include <limits>\n#include <climits>\t\t// for CHAR_BIT\n#include <array>\n#include <thread>\t\t// partly for __WINPTHREADS_VERSION if on MinGW-w64 w/ POSIX threading\n\n// Platform-specific definitions of a numeric thread ID type and an invalid value\nnamespace moodycamel { namespace details {\n\ttemplate<typename thread_id_t> struct thread_id_converter {\n\t\ttypedef thread_id_t thread_id_numeric_size_t;\n\t\ttypedef thread_id_t thread_id_hash_t;\n\t\tstatic thread_id_hash_t prehash(thread_id_t const& x) { return x; }\n\t};\n} }\n#if defined(MCDBGQ_USE_RELACY)\nnamespace moodycamel { namespace details {\n\ttypedef std::uint32_t thread_id_t;\n\tstatic const thread_id_t invalid_thread_id  = 0xFFFFFFFFU;\n\tstatic const thread_id_t invalid_thread_id2 = 0xFFFFFFFEU;\n\tstatic inline thread_id_t thread_id() { return rl::thread_index(); }\n} }\n#elif defined(_WIN32) || defined(__WINDOWS__) || defined(__WIN32__)\n// No sense pulling in windows.h in a header, we'll manually declare the function\n// we use and rely on backwards-compatibility for this not to break\nextern \"C\" __declspec(dllimport) unsigned long __stdcall GetCurrentThreadId(void);\nnamespace moodycamel { namespace details {\n\tstatic_assert(sizeof(unsigned long) == sizeof(std::uint32_t), \"Expected size of unsigned long to be 32 bits on Windows\");\n\ttypedef std::uint32_t thread_id_t;\n\tstatic const thread_id_t invalid_thread_id  = 0;\t\t\t// See http://blogs.msdn.com/b/oldnewthing/archive/2004/02/23/78395.aspx\n\tstatic const thread_id_t invalid_thread_id2 = 0xFFFFFFFFU;\t// Not technically guaranteed to be invalid, but is never used in practice. Note that all Win32 thread IDs are presently multiples of 4.\n\tstatic inline thread_id_t thread_id() { return static_cast<thread_id_t>(::GetCurrentThreadId()); }\n} }\n#elif defined(__arm__) || defined(_M_ARM) || defined(__aarch64__) || (defined(__APPLE__) && TARGET_OS_IPHONE) || defined(MOODYCAMEL_NO_THREAD_LOCAL)\nnamespace moodycamel { namespace details {\n\tstatic_assert(sizeof(std::thread::id) == 4 || sizeof(std::thread::id) == 8, \"std::thread::id is expected to be either 4 or 8 bytes\");\n\t\n\ttypedef std::thread::id thread_id_t;\n\tstatic const thread_id_t invalid_thread_id;         // Default ctor creates invalid ID\n\n\t// Note we don't define a invalid_thread_id2 since std::thread::id doesn't have one; it's\n\t// only used if MOODYCAMEL_CPP11_THREAD_LOCAL_SUPPORTED is defined anyway, which it won't\n\t// be.\n\tstatic inline thread_id_t thread_id() { return std::this_thread::get_id(); }\n\n\ttemplate<std::size_t> struct thread_id_size { };\n\ttemplate<> struct thread_id_size<4> { typedef std::uint32_t numeric_t; };\n\ttemplate<> struct thread_id_size<8> { typedef std::uint64_t numeric_t; };\n\n\ttemplate<> struct thread_id_converter<thread_id_t> {\n\t\ttypedef thread_id_size<sizeof(thread_id_t)>::numeric_t thread_id_numeric_size_t;\n#ifndef __APPLE__\n\t\ttypedef std::size_t thread_id_hash_t;\n#else\n\t\ttypedef thread_id_numeric_size_t thread_id_hash_t;\n#endif\n\n\t\tstatic thread_id_hash_t prehash(thread_id_t const& x)\n\t\t{\n#ifndef __APPLE__\n\t\t\treturn std::hash<std::thread::id>()(x);\n#else\n\t\t\treturn *reinterpret_cast<thread_id_hash_t const*>(&x);\n#endif\n\t\t}\n\t};\n} }\n#else\n// Use a nice trick from this answer: http://stackoverflow.com/a/8438730/21475\n// In order to get a numeric thread ID in a platform-independent way, we use a thread-local\n// static variable's address as a thread identifier :-)\n#if defined(__GNUC__) || defined(__INTEL_COMPILER)\n#define MOODYCAMEL_THREADLOCAL __thread\n#elif defined(_MSC_VER)\n#define MOODYCAMEL_THREADLOCAL __declspec(thread)\n#else\n// Assume C++11 compliant compiler\n#define MOODYCAMEL_THREADLOCAL thread_local\n#endif\nnamespace moodycamel { namespace details {\n\ttypedef std::uintptr_t thread_id_t;\n\tstatic const thread_id_t invalid_thread_id  = 0;\t\t// Address can't be nullptr\n\tstatic const thread_id_t invalid_thread_id2 = 1;\t\t// Member accesses off a null pointer are also generally invalid. Plus it's not aligned.\n\tinline thread_id_t thread_id() { static MOODYCAMEL_THREADLOCAL int x; return reinterpret_cast<thread_id_t>(&x); }\n} }\n#endif\n\n// Constexpr if\n#ifndef MOODYCAMEL_CONSTEXPR_IF\n#if (defined(_MSC_VER) && defined(_HAS_CXX17) && _HAS_CXX17) || __cplusplus > 201402L\n#define MOODYCAMEL_CONSTEXPR_IF if constexpr\n#define MOODYCAMEL_MAYBE_UNUSED [[maybe_unused]]\n#else\n#define MOODYCAMEL_CONSTEXPR_IF if\n#define MOODYCAMEL_MAYBE_UNUSED\n#endif\n#endif\n\n// Exceptions\n#ifndef MOODYCAMEL_EXCEPTIONS_ENABLED\n#if (defined(_MSC_VER) && defined(_CPPUNWIND)) || (defined(__GNUC__) && defined(__EXCEPTIONS)) || (!defined(_MSC_VER) && !defined(__GNUC__))\n#define MOODYCAMEL_EXCEPTIONS_ENABLED\n#endif\n#endif\n#ifdef MOODYCAMEL_EXCEPTIONS_ENABLED\n#define MOODYCAMEL_TRY try\n#define MOODYCAMEL_CATCH(...) catch(__VA_ARGS__)\n#define MOODYCAMEL_RETHROW throw\n#define MOODYCAMEL_THROW(expr) throw (expr)\n#else\n#define MOODYCAMEL_TRY MOODYCAMEL_CONSTEXPR_IF (true)\n#define MOODYCAMEL_CATCH(...) else MOODYCAMEL_CONSTEXPR_IF (false)\n#define MOODYCAMEL_RETHROW\n#define MOODYCAMEL_THROW(expr)\n#endif\n\n#ifndef MOODYCAMEL_NOEXCEPT\n#if !defined(MOODYCAMEL_EXCEPTIONS_ENABLED)\n#define MOODYCAMEL_NOEXCEPT\n#define MOODYCAMEL_NOEXCEPT_CTOR(type, valueType, expr) true\n#define MOODYCAMEL_NOEXCEPT_ASSIGN(type, valueType, expr) true\n#elif defined(_MSC_VER) && defined(_NOEXCEPT) && _MSC_VER < 1800\n// VS2012's std::is_nothrow_[move_]constructible is broken and returns true when it shouldn't :-(\n// We have to assume *all* non-trivial constructors may throw on VS2012!\n#define MOODYCAMEL_NOEXCEPT _NOEXCEPT\n#define MOODYCAMEL_NOEXCEPT_CTOR(type, valueType, expr) (std::is_rvalue_reference<valueType>::value && std::is_move_constructible<type>::value ? std::is_trivially_move_constructible<type>::value : std::is_trivially_copy_constructible<type>::value)\n#define MOODYCAMEL_NOEXCEPT_ASSIGN(type, valueType, expr) ((std::is_rvalue_reference<valueType>::value && std::is_move_assignable<type>::value ? std::is_trivially_move_assignable<type>::value || std::is_nothrow_move_assignable<type>::value : std::is_trivially_copy_assignable<type>::value || std::is_nothrow_copy_assignable<type>::value) && MOODYCAMEL_NOEXCEPT_CTOR(type, valueType, expr))\n#elif defined(_MSC_VER) && defined(_NOEXCEPT) && _MSC_VER < 1900\n#define MOODYCAMEL_NOEXCEPT _NOEXCEPT\n#define MOODYCAMEL_NOEXCEPT_CTOR(type, valueType, expr) (std::is_rvalue_reference<valueType>::value && std::is_move_constructible<type>::value ? std::is_trivially_move_constructible<type>::value || std::is_nothrow_move_constructible<type>::value : std::is_trivially_copy_constructible<type>::value || std::is_nothrow_copy_constructible<type>::value)\n#define MOODYCAMEL_NOEXCEPT_ASSIGN(type, valueType, expr) ((std::is_rvalue_reference<valueType>::value && std::is_move_assignable<type>::value ? std::is_trivially_move_assignable<type>::value || std::is_nothrow_move_assignable<type>::value : std::is_trivially_copy_assignable<type>::value || std::is_nothrow_copy_assignable<type>::value) && MOODYCAMEL_NOEXCEPT_CTOR(type, valueType, expr))\n#else\n#define MOODYCAMEL_NOEXCEPT noexcept\n#define MOODYCAMEL_NOEXCEPT_CTOR(type, valueType, expr) noexcept(expr)\n#define MOODYCAMEL_NOEXCEPT_ASSIGN(type, valueType, expr) noexcept(expr)\n#endif\n#endif\n\n#ifndef MOODYCAMEL_CPP11_THREAD_LOCAL_SUPPORTED\n#ifdef MCDBGQ_USE_RELACY\n#define MOODYCAMEL_CPP11_THREAD_LOCAL_SUPPORTED\n#else\n// VS2013 doesn't support `thread_local`, and MinGW-w64 w/ POSIX threading has a crippling bug: http://sourceforge.net/p/mingw-w64/bugs/445\n// g++ <=4.7 doesn't support thread_local either.\n// Finally, iOS/ARM doesn't have support for it either, and g++/ARM allows it to compile but it's unconfirmed to actually work\n#if (!defined(_MSC_VER) || _MSC_VER >= 1900) && (!defined(__MINGW32__) && !defined(__MINGW64__) || !defined(__WINPTHREADS_VERSION)) && (!defined(__GNUC__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) && (!defined(__APPLE__) || !TARGET_OS_IPHONE) && !defined(__arm__) && !defined(_M_ARM) && !defined(__aarch64__)\n// Assume `thread_local` is fully supported in all other C++11 compilers/platforms\n//#define MOODYCAMEL_CPP11_THREAD_LOCAL_SUPPORTED    // always disabled for now since several users report having problems with it on\n#endif\n#endif\n#endif\n\n// VS2012 doesn't support deleted functions. \n// In this case, we declare the function normally but don't define it. A link error will be generated if the function is called.\n#ifndef MOODYCAMEL_DELETE_FUNCTION\n#if defined(_MSC_VER) && _MSC_VER < 1800\n#define MOODYCAMEL_DELETE_FUNCTION\n#else\n#define MOODYCAMEL_DELETE_FUNCTION = delete\n#endif\n#endif\n\nnamespace moodycamel { namespace details {\n#ifndef MOODYCAMEL_ALIGNAS\n// VS2013 doesn't support alignas or alignof, and align() requires a constant literal\n#if defined(_MSC_VER) && _MSC_VER <= 1800\n#define MOODYCAMEL_ALIGNAS(alignment) __declspec(align(alignment))\n#define MOODYCAMEL_ALIGNOF(obj) __alignof(obj)\n#define MOODYCAMEL_ALIGNED_TYPE_LIKE(T, obj) typename details::Vs2013Aligned<std::alignment_of<obj>::value, T>::type\n\ttemplate<int Align, typename T> struct Vs2013Aligned { };  // default, unsupported alignment\n\ttemplate<typename T> struct Vs2013Aligned<1, T> { typedef __declspec(align(1)) T type; };\n\ttemplate<typename T> struct Vs2013Aligned<2, T> { typedef __declspec(align(2)) T type; };\n\ttemplate<typename T> struct Vs2013Aligned<4, T> { typedef __declspec(align(4)) T type; };\n\ttemplate<typename T> struct Vs2013Aligned<8, T> { typedef __declspec(align(8)) T type; };\n\ttemplate<typename T> struct Vs2013Aligned<16, T> { typedef __declspec(align(16)) T type; };\n\ttemplate<typename T> struct Vs2013Aligned<32, T> { typedef __declspec(align(32)) T type; };\n\ttemplate<typename T> struct Vs2013Aligned<64, T> { typedef __declspec(align(64)) T type; };\n\ttemplate<typename T> struct Vs2013Aligned<128, T> { typedef __declspec(align(128)) T type; };\n\ttemplate<typename T> struct Vs2013Aligned<256, T> { typedef __declspec(align(256)) T type; };\n#else\n\ttemplate<typename T> struct identity { typedef T type; };\n#define MOODYCAMEL_ALIGNAS(alignment) alignas(alignment)\n#define MOODYCAMEL_ALIGNOF(obj) alignof(obj)\n#define MOODYCAMEL_ALIGNED_TYPE_LIKE(T, obj) alignas(alignof(obj)) typename details::identity<T>::type\n#endif\n#endif\n} }\n\n\n// TSAN can false report races in lock-free code.  To enable TSAN to be used from projects that use this one,\n// we can apply per-function compile-time suppression.\n// See https://clang.llvm.org/docs/ThreadSanitizer.html#has-feature-thread-sanitizer\n#define MOODYCAMEL_NO_TSAN\n#if defined(__has_feature)\n #if __has_feature(thread_sanitizer)\n  #undef MOODYCAMEL_NO_TSAN\n  #define MOODYCAMEL_NO_TSAN __attribute__((no_sanitize(\"thread\")))\n #endif // TSAN\n#endif // TSAN\n\n// Compiler-specific likely/unlikely hints\nnamespace moodycamel { namespace details {\n#if defined(__GNUC__)\n\tstatic inline bool (likely)(bool x) { return __builtin_expect((x), true); }\n\tstatic inline bool (unlikely)(bool x) { return __builtin_expect((x), false); }\n#else\n\tstatic inline bool (likely)(bool x) { return x; }\n\tstatic inline bool (unlikely)(bool x) { return x; }\n#endif\n} }\n\n#ifdef MOODYCAMEL_QUEUE_INTERNAL_DEBUG\n#include \"internal/concurrentqueue_internal_debug.h\"\n#endif\n\nnamespace moodycamel {\nnamespace details {\n\ttemplate<typename T>\n\tstruct const_numeric_max {\n\t\tstatic_assert(std::is_integral<T>::value, \"const_numeric_max can only be used with integers\");\n\t\tstatic const T value = std::numeric_limits<T>::is_signed\n\t\t\t? (static_cast<T>(1) << (sizeof(T) * CHAR_BIT - 1)) - static_cast<T>(1)\n\t\t\t: static_cast<T>(-1);\n\t};\n\n#if defined(__GLIBCXX__)\n\ttypedef ::max_align_t std_max_align_t;      // libstdc++ forgot to add it to std:: for a while\n#else\n\ttypedef std::max_align_t std_max_align_t;   // Others (e.g. MSVC) insist it can *only* be accessed via std::\n#endif\n\n\t// Some platforms have incorrectly set max_align_t to a type with <8 bytes alignment even while supporting\n\t// 8-byte aligned scalar values (*cough* 32-bit iOS). Work around this with our own union. See issue #64.\n\ttypedef union {\n\t\tstd_max_align_t x;\n\t\tlong long y;\n\t\tvoid* z;\n\t} max_align_t;\n}\n\n// Default traits for the ConcurrentQueue. To change some of the\n// traits without re-implementing all of them, inherit from this\n// struct and shadow the declarations you wish to be different;\n// since the traits are used as a template type parameter, the\n// shadowed declarations will be used where defined, and the defaults\n// otherwise.\nstruct ConcurrentQueueDefaultTraits\n{\n\t// General-purpose size type. std::size_t is strongly recommended.\n\ttypedef std::size_t size_t;\n\t\n\t// The type used for the enqueue and dequeue indices. Must be at least as\n\t// large as size_t. Should be significantly larger than the number of elements\n\t// you expect to hold at once, especially if you have a high turnover rate;\n\t// for example, on 32-bit x86, if you expect to have over a hundred million\n\t// elements or pump several million elements through your queue in a very\n\t// short space of time, using a 32-bit type *may* trigger a race condition.\n\t// A 64-bit int type is recommended in that case, and in practice will\n\t// prevent a race condition no matter the usage of the queue. Note that\n\t// whether the queue is lock-free with a 64-int type depends on the whether\n\t// std::atomic<std::uint64_t> is lock-free, which is platform-specific.\n\ttypedef std::size_t index_t;\n\t\n\t// Internally, all elements are enqueued and dequeued from multi-element\n\t// blocks; this is the smallest controllable unit. If you expect few elements\n\t// but many producers, a smaller block size should be favoured. For few producers\n\t// and/or many elements, a larger block size is preferred. A sane default\n\t// is provided. Must be a power of 2.\n\tstatic const size_t BLOCK_SIZE = 32;\n\t\n\t// For explicit producers (i.e. when using a producer token), the block is\n\t// checked for being empty by iterating through a list of flags, one per element.\n\t// For large block sizes, this is too inefficient, and switching to an atomic\n\t// counter-based approach is faster. The switch is made for block sizes strictly\n\t// larger than this threshold.\n\tstatic const size_t EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD = 32;\n\t\n\t// How many full blocks can be expected for a single explicit producer? This should\n\t// reflect that number's maximum for optimal performance. Must be a power of 2.\n\tstatic const size_t EXPLICIT_INITIAL_INDEX_SIZE = 32;\n\t\n\t// How many full blocks can be expected for a single implicit producer? This should\n\t// reflect that number's maximum for optimal performance. Must be a power of 2.\n\tstatic const size_t IMPLICIT_INITIAL_INDEX_SIZE = 32;\n\t\n\t// The initial size of the hash table mapping thread IDs to implicit producers.\n\t// Note that the hash is resized every time it becomes half full.\n\t// Must be a power of two, and either 0 or at least 1. If 0, implicit production\n\t// (using the enqueue methods without an explicit producer token) is disabled.\n\tstatic const size_t INITIAL_IMPLICIT_PRODUCER_HASH_SIZE = 32;\n\t\n\t// Controls the number of items that an explicit consumer (i.e. one with a token)\n\t// must consume before it causes all consumers to rotate and move on to the next\n\t// internal queue.\n\tstatic const std::uint32_t EXPLICIT_CONSUMER_CONSUMPTION_QUOTA_BEFORE_ROTATE = 256;\n\t\n\t// The maximum number of elements (inclusive) that can be enqueued to a sub-queue.\n\t// Enqueue operations that would cause this limit to be surpassed will fail. Note\n\t// that this limit is enforced at the block level (for performance reasons), i.e.\n\t// it's rounded up to the nearest block size.\n\tstatic const size_t MAX_SUBQUEUE_SIZE = details::const_numeric_max<size_t>::value;\n\n\t// The number of times to spin before sleeping when waiting on a semaphore.\n\t// Recommended values are on the order of 1000-10000 unless the number of\n\t// consumer threads exceeds the number of idle cores (in which case try 0-100).\n\t// Only affects instances of the BlockingConcurrentQueue.\n\tstatic const int MAX_SEMA_SPINS = 10000;\n\t\n\t\n#ifndef MCDBGQ_USE_RELACY\n\t// Memory allocation can be customized if needed.\n\t// malloc should return nullptr on failure, and handle alignment like std::malloc.\n#if defined(malloc) || defined(free)\n\t// Gah, this is 2015, stop defining macros that break standard code already!\n\t// Work around malloc/free being special macros:\n\tstatic inline void* WORKAROUND_malloc(size_t size) { return malloc(size); }\n\tstatic inline void WORKAROUND_free(void* ptr) { return free(ptr); }\n\tstatic inline void* (malloc)(size_t size) { return WORKAROUND_malloc(size); }\n\tstatic inline void (free)(void* ptr) { return WORKAROUND_free(ptr); }\n#else\n\tstatic inline void* malloc(size_t size) { return std::malloc(size); }\n\tstatic inline void free(void* ptr) { return std::free(ptr); }\n#endif\n#else\n\t// Debug versions when running under the Relacy race detector (ignore\n\t// these in user code)\n\tstatic inline void* malloc(size_t size) { return rl::rl_malloc(size, $); }\n\tstatic inline void free(void* ptr) { return rl::rl_free(ptr, $); }\n#endif\n};\n\n\n// When producing or consuming many elements, the most efficient way is to:\n//    1) Use one of the bulk-operation methods of the queue with a token\n//    2) Failing that, use the bulk-operation methods without a token\n//    3) Failing that, create a token and use that with the single-item methods\n//    4) Failing that, use the single-parameter methods of the queue\n// Having said that, don't create tokens willy-nilly -- ideally there should be\n// a maximum of one token per thread (of each kind).\nstruct ProducerToken;\nstruct ConsumerToken;\n\ntemplate<typename T, typename Traits> class ConcurrentQueue;\ntemplate<typename T, typename Traits> class BlockingConcurrentQueue;\nclass ConcurrentQueueTests;\n\n\nnamespace details\n{\n\tstruct ConcurrentQueueProducerTypelessBase\n\t{\n\t\tConcurrentQueueProducerTypelessBase* next;\n\t\tstd::atomic<bool> inactive;\n\t\tProducerToken* token;\n\t\t\n\t\tConcurrentQueueProducerTypelessBase()\n\t\t\t: next(nullptr), inactive(false), token(nullptr)\n\t\t{\n\t\t}\n\t};\n\t\n\ttemplate<bool use32> struct _hash_32_or_64 {\n\t\tstatic inline std::uint32_t hash(std::uint32_t h)\n\t\t{\n\t\t\t// MurmurHash3 finalizer -- see https://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp\n\t\t\t// Since the thread ID is already unique, all we really want to do is propagate that\n\t\t\t// uniqueness evenly across all the bits, so that we can use a subset of the bits while\n\t\t\t// reducing collisions significantly\n\t\t\th ^= h >> 16;\n\t\t\th *= 0x85ebca6b;\n\t\t\th ^= h >> 13;\n\t\t\th *= 0xc2b2ae35;\n\t\t\treturn h ^ (h >> 16);\n\t\t}\n\t};\n\ttemplate<> struct _hash_32_or_64<1> {\n\t\tstatic inline std::uint64_t hash(std::uint64_t h)\n\t\t{\n\t\t\th ^= h >> 33;\n\t\t\th *= 0xff51afd7ed558ccd;\n\t\t\th ^= h >> 33;\n\t\t\th *= 0xc4ceb9fe1a85ec53;\n\t\t\treturn h ^ (h >> 33);\n\t\t}\n\t};\n\ttemplate<std::size_t size> struct hash_32_or_64 : public _hash_32_or_64<(size > 4)> {  };\n\t\n\tstatic inline size_t hash_thread_id(thread_id_t id)\n\t{\n\t\tstatic_assert(sizeof(thread_id_t) <= 8, \"Expected a platform where thread IDs are at most 64-bit values\");\n\t\treturn static_cast<size_t>(hash_32_or_64<sizeof(thread_id_converter<thread_id_t>::thread_id_hash_t)>::hash(\n\t\t\tthread_id_converter<thread_id_t>::prehash(id)));\n\t}\n\t\n\ttemplate<typename T>\n\tstatic inline bool circular_less_than(T a, T b)\n\t{\n#ifdef _MSC_VER\n#pragma warning(push)\n#pragma warning(disable: 4554)\n#endif\n\t\tstatic_assert(std::is_integral<T>::value && !std::numeric_limits<T>::is_signed, \"circular_less_than is intended to be used only with unsigned integer types\");\n\t\treturn static_cast<T>(a - b) > static_cast<T>(static_cast<T>(1) << static_cast<T>(sizeof(T) * CHAR_BIT - 1));\n#ifdef _MSC_VER\n#pragma warning(pop)\n#endif\n\t}\n\t\n\ttemplate<typename U>\n\tstatic inline char* align_for(char* ptr)\n\t{\n\t\tconst std::size_t alignment = std::alignment_of<U>::value;\n\t\treturn ptr + (alignment - (reinterpret_cast<std::uintptr_t>(ptr) % alignment)) % alignment;\n\t}\n\n\ttemplate<typename T>\n\tstatic inline T ceil_to_pow_2(T x)\n\t{\n\t\tstatic_assert(std::is_integral<T>::value && !std::numeric_limits<T>::is_signed, \"ceil_to_pow_2 is intended to be used only with unsigned integer types\");\n\n\t\t// Adapted from http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2\n\t\t--x;\n\t\tx |= x >> 1;\n\t\tx |= x >> 2;\n\t\tx |= x >> 4;\n\t\tfor (std::size_t i = 1; i < sizeof(T); i <<= 1) {\n\t\t\tx |= x >> (i << 3);\n\t\t}\n\t\t++x;\n\t\treturn x;\n\t}\n\t\n\ttemplate<typename T>\n\tstatic inline void swap_relaxed(std::atomic<T>& left, std::atomic<T>& right)\n\t{\n\t\tT temp = std::move(left.load(std::memory_order_relaxed));\n\t\tleft.store(std::move(right.load(std::memory_order_relaxed)), std::memory_order_relaxed);\n\t\tright.store(std::move(temp), std::memory_order_relaxed);\n\t}\n\t\n\ttemplate<typename T>\n\tstatic inline T const& nomove(T const& x)\n\t{\n\t\treturn x;\n\t}\n\t\n\ttemplate<bool Enable>\n\tstruct nomove_if\n\t{\n\t\ttemplate<typename T>\n\t\tstatic inline T const& eval(T const& x)\n\t\t{\n\t\t\treturn x;\n\t\t}\n\t};\n\t\n\ttemplate<>\n\tstruct nomove_if<false>\n\t{\n\t\ttemplate<typename U>\n\t\tstatic inline auto eval(U&& x)\n\t\t\t-> decltype(std::forward<U>(x))\n\t\t{\n\t\t\treturn std::forward<U>(x);\n\t\t}\n\t};\n\t\n\ttemplate<typename It>\n\tstatic inline auto deref_noexcept(It& it) MOODYCAMEL_NOEXCEPT -> decltype(*it)\n\t{\n\t\treturn *it;\n\t}\n\t\n#if defined(__clang__) || !defined(__GNUC__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)\n\ttemplate<typename T> struct is_trivially_destructible : std::is_trivially_destructible<T> { };\n#else\n\ttemplate<typename T> struct is_trivially_destructible : std::has_trivial_destructor<T> { };\n#endif\n\t\n#ifdef MOODYCAMEL_CPP11_THREAD_LOCAL_SUPPORTED\n#ifdef MCDBGQ_USE_RELACY\n\ttypedef RelacyThreadExitListener ThreadExitListener;\n\ttypedef RelacyThreadExitNotifier ThreadExitNotifier;\n#else\n\tstruct ThreadExitListener\n\t{\n\t\ttypedef void (*callback_t)(void*);\n\t\tcallback_t callback;\n\t\tvoid* userData;\n\t\t\n\t\tThreadExitListener* next;\t\t// reserved for use by the ThreadExitNotifier\n\t};\n\t\n\t\n\tclass ThreadExitNotifier\n\t{\n\tpublic:\n\t\tstatic void subscribe(ThreadExitListener* listener)\n\t\t{\n\t\t\tauto& tlsInst = instance();\n\t\t\tlistener->next = tlsInst.tail;\n\t\t\ttlsInst.tail = listener;\n\t\t}\n\t\t\n\t\tstatic void unsubscribe(ThreadExitListener* listener)\n\t\t{\n\t\t\tauto& tlsInst = instance();\n\t\t\tThreadExitListener** prev = &tlsInst.tail;\n\t\t\tfor (auto ptr = tlsInst.tail; ptr != nullptr; ptr = ptr->next) {\n\t\t\t\tif (ptr == listener) {\n\t\t\t\t\t*prev = ptr->next;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tprev = &ptr->next;\n\t\t\t}\n\t\t}\n\t\t\n\tprivate:\n\t\tThreadExitNotifier() : tail(nullptr) { }\n\t\tThreadExitNotifier(ThreadExitNotifier const&) MOODYCAMEL_DELETE_FUNCTION;\n\t\tThreadExitNotifier& operator=(ThreadExitNotifier const&) MOODYCAMEL_DELETE_FUNCTION;\n\t\t\n\t\t~ThreadExitNotifier()\n\t\t{\n\t\t\t// This thread is about to exit, let everyone know!\n\t\t\tassert(this == &instance() && \"If this assert fails, you likely have a buggy compiler! Change the preprocessor conditions such that MOODYCAMEL_CPP11_THREAD_LOCAL_SUPPORTED is no longer defined.\");\n\t\t\tfor (auto ptr = tail; ptr != nullptr; ptr = ptr->next) {\n\t\t\t\tptr->callback(ptr->userData);\n\t\t\t}\n\t\t}\n\t\t\n\t\t// Thread-local\n\t\tstatic inline ThreadExitNotifier& instance()\n\t\t{\n\t\t\tstatic thread_local ThreadExitNotifier notifier;\n\t\t\treturn notifier;\n\t\t}\n\t\t\n\tprivate:\n\t\tThreadExitListener* tail;\n\t};\n#endif\n#endif\n\t\n\ttemplate<typename T> struct static_is_lock_free_num { enum { value = 0 }; };\n\ttemplate<> struct static_is_lock_free_num<signed char> { enum { value = ATOMIC_CHAR_LOCK_FREE }; };\n\ttemplate<> struct static_is_lock_free_num<short> { enum { value = ATOMIC_SHORT_LOCK_FREE }; };\n\ttemplate<> struct static_is_lock_free_num<int> { enum { value = ATOMIC_INT_LOCK_FREE }; };\n\ttemplate<> struct static_is_lock_free_num<long> { enum { value = ATOMIC_LONG_LOCK_FREE }; };\n\ttemplate<> struct static_is_lock_free_num<long long> { enum { value = ATOMIC_LLONG_LOCK_FREE }; };\n\ttemplate<typename T> struct static_is_lock_free : static_is_lock_free_num<typename std::make_signed<T>::type> {  };\n\ttemplate<> struct static_is_lock_free<bool> { enum { value = ATOMIC_BOOL_LOCK_FREE }; };\n\ttemplate<typename U> struct static_is_lock_free<U*> { enum { value = ATOMIC_POINTER_LOCK_FREE }; };\n}\n\n\nstruct ProducerToken\n{\n\ttemplate<typename T, typename Traits>\n\texplicit ProducerToken(ConcurrentQueue<T, Traits>& queue);\n\t\n\ttemplate<typename T, typename Traits>\n\texplicit ProducerToken(BlockingConcurrentQueue<T, Traits>& queue);\n\t\n\tProducerToken(ProducerToken&& other) MOODYCAMEL_NOEXCEPT\n\t\t: producer(other.producer)\n\t{\n\t\tother.producer = nullptr;\n\t\tif (producer != nullptr) {\n\t\t\tproducer->token = this;\n\t\t}\n\t}\n\t\n\tinline ProducerToken& operator=(ProducerToken&& other) MOODYCAMEL_NOEXCEPT\n\t{\n\t\tswap(other);\n\t\treturn *this;\n\t}\n\t\n\tvoid swap(ProducerToken& other) MOODYCAMEL_NOEXCEPT\n\t{\n\t\tstd::swap(producer, other.producer);\n\t\tif (producer != nullptr) {\n\t\t\tproducer->token = this;\n\t\t}\n\t\tif (other.producer != nullptr) {\n\t\t\tother.producer->token = &other;\n\t\t}\n\t}\n\t\n\t// A token is always valid unless:\n\t//     1) Memory allocation failed during construction\n\t//     2) It was moved via the move constructor\n\t//        (Note: assignment does a swap, leaving both potentially valid)\n\t//     3) The associated queue was destroyed\n\t// Note that if valid() returns true, that only indicates\n\t// that the token is valid for use with a specific queue,\n\t// but not which one; that's up to the user to track.\n\tinline bool valid() const { return producer != nullptr; }\n\t\n\t~ProducerToken()\n\t{\n\t\tif (producer != nullptr) {\n\t\t\tproducer->token = nullptr;\n\t\t\tproducer->inactive.store(true, std::memory_order_release);\n\t\t}\n\t}\n\t\n\t// Disable copying and assignment\n\tProducerToken(ProducerToken const&) MOODYCAMEL_DELETE_FUNCTION;\n\tProducerToken& operator=(ProducerToken const&) MOODYCAMEL_DELETE_FUNCTION;\n\t\nprivate:\n\ttemplate<typename T, typename Traits> friend class ConcurrentQueue;\n\tfriend class ConcurrentQueueTests;\n\t\nprotected:\n\tdetails::ConcurrentQueueProducerTypelessBase* producer;\n};\n\n\nstruct ConsumerToken\n{\n\ttemplate<typename T, typename Traits>\n\texplicit ConsumerToken(ConcurrentQueue<T, Traits>& q);\n\t\n\ttemplate<typename T, typename Traits>\n\texplicit ConsumerToken(BlockingConcurrentQueue<T, Traits>& q);\n\t\n\tConsumerToken(ConsumerToken&& other) MOODYCAMEL_NOEXCEPT\n\t\t: initialOffset(other.initialOffset), lastKnownGlobalOffset(other.lastKnownGlobalOffset), itemsConsumedFromCurrent(other.itemsConsumedFromCurrent), currentProducer(other.currentProducer), desiredProducer(other.desiredProducer)\n\t{\n\t}\n\t\n\tinline ConsumerToken& operator=(ConsumerToken&& other) MOODYCAMEL_NOEXCEPT\n\t{\n\t\tswap(other);\n\t\treturn *this;\n\t}\n\t\n\tvoid swap(ConsumerToken& other) MOODYCAMEL_NOEXCEPT\n\t{\n\t\tstd::swap(initialOffset, other.initialOffset);\n\t\tstd::swap(lastKnownGlobalOffset, other.lastKnownGlobalOffset);\n\t\tstd::swap(itemsConsumedFromCurrent, other.itemsConsumedFromCurrent);\n\t\tstd::swap(currentProducer, other.currentProducer);\n\t\tstd::swap(desiredProducer, other.desiredProducer);\n\t}\n\t\n\t// Disable copying and assignment\n\tConsumerToken(ConsumerToken const&) MOODYCAMEL_DELETE_FUNCTION;\n\tConsumerToken& operator=(ConsumerToken const&) MOODYCAMEL_DELETE_FUNCTION;\n\nprivate:\n\ttemplate<typename T, typename Traits> friend class ConcurrentQueue;\n\tfriend class ConcurrentQueueTests;\n\t\nprivate: // but shared with ConcurrentQueue\n\tstd::uint32_t initialOffset;\n\tstd::uint32_t lastKnownGlobalOffset;\n\tstd::uint32_t itemsConsumedFromCurrent;\n\tdetails::ConcurrentQueueProducerTypelessBase* currentProducer;\n\tdetails::ConcurrentQueueProducerTypelessBase* desiredProducer;\n};\n\n// Need to forward-declare this swap because it's in a namespace.\n// See http://stackoverflow.com/questions/4492062/why-does-a-c-friend-class-need-a-forward-declaration-only-in-other-namespaces\ntemplate<typename T, typename Traits>\ninline void swap(typename ConcurrentQueue<T, Traits>::ImplicitProducerKVP& a, typename ConcurrentQueue<T, Traits>::ImplicitProducerKVP& b) MOODYCAMEL_NOEXCEPT;\n\n\ntemplate<typename T, typename Traits = ConcurrentQueueDefaultTraits>\nclass ConcurrentQueue\n{\npublic:\n\ttypedef ::moodycamel::ProducerToken producer_token_t;\n\ttypedef ::moodycamel::ConsumerToken consumer_token_t;\n\t\n\ttypedef typename Traits::index_t index_t;\n\ttypedef typename Traits::size_t size_t;\n\t\n\tstatic const size_t BLOCK_SIZE = static_cast<size_t>(Traits::BLOCK_SIZE);\n\tstatic const size_t EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD = static_cast<size_t>(Traits::EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD);\n\tstatic const size_t EXPLICIT_INITIAL_INDEX_SIZE = static_cast<size_t>(Traits::EXPLICIT_INITIAL_INDEX_SIZE);\n\tstatic const size_t IMPLICIT_INITIAL_INDEX_SIZE = static_cast<size_t>(Traits::IMPLICIT_INITIAL_INDEX_SIZE);\n\tstatic const size_t INITIAL_IMPLICIT_PRODUCER_HASH_SIZE = static_cast<size_t>(Traits::INITIAL_IMPLICIT_PRODUCER_HASH_SIZE);\n\tstatic const std::uint32_t EXPLICIT_CONSUMER_CONSUMPTION_QUOTA_BEFORE_ROTATE = static_cast<std::uint32_t>(Traits::EXPLICIT_CONSUMER_CONSUMPTION_QUOTA_BEFORE_ROTATE);\n#ifdef _MSC_VER\n#pragma warning(push)\n#pragma warning(disable: 4307)\t\t// + integral constant overflow (that's what the ternary expression is for!)\n#pragma warning(disable: 4309)\t\t// static_cast: Truncation of constant value\n#endif\n\tstatic const size_t MAX_SUBQUEUE_SIZE = (details::const_numeric_max<size_t>::value - static_cast<size_t>(Traits::MAX_SUBQUEUE_SIZE) < BLOCK_SIZE) ? details::const_numeric_max<size_t>::value : ((static_cast<size_t>(Traits::MAX_SUBQUEUE_SIZE) + (BLOCK_SIZE - 1)) / BLOCK_SIZE * BLOCK_SIZE);\n#ifdef _MSC_VER\n#pragma warning(pop)\n#endif\n\n\tstatic_assert(!std::numeric_limits<size_t>::is_signed && std::is_integral<size_t>::value, \"Traits::size_t must be an unsigned integral type\");\n\tstatic_assert(!std::numeric_limits<index_t>::is_signed && std::is_integral<index_t>::value, \"Traits::index_t must be an unsigned integral type\");\n\tstatic_assert(sizeof(index_t) >= sizeof(size_t), \"Traits::index_t must be at least as wide as Traits::size_t\");\n\tstatic_assert((BLOCK_SIZE > 1) && !(BLOCK_SIZE & (BLOCK_SIZE - 1)), \"Traits::BLOCK_SIZE must be a power of 2 (and at least 2)\");\n\tstatic_assert((EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD > 1) && !(EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD & (EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD - 1)), \"Traits::EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD must be a power of 2 (and greater than 1)\");\n\tstatic_assert((EXPLICIT_INITIAL_INDEX_SIZE > 1) && !(EXPLICIT_INITIAL_INDEX_SIZE & (EXPLICIT_INITIAL_INDEX_SIZE - 1)), \"Traits::EXPLICIT_INITIAL_INDEX_SIZE must be a power of 2 (and greater than 1)\");\n\tstatic_assert((IMPLICIT_INITIAL_INDEX_SIZE > 1) && !(IMPLICIT_INITIAL_INDEX_SIZE & (IMPLICIT_INITIAL_INDEX_SIZE - 1)), \"Traits::IMPLICIT_INITIAL_INDEX_SIZE must be a power of 2 (and greater than 1)\");\n\tstatic_assert((INITIAL_IMPLICIT_PRODUCER_HASH_SIZE == 0) || !(INITIAL_IMPLICIT_PRODUCER_HASH_SIZE & (INITIAL_IMPLICIT_PRODUCER_HASH_SIZE - 1)), \"Traits::INITIAL_IMPLICIT_PRODUCER_HASH_SIZE must be a power of 2\");\n\tstatic_assert(INITIAL_IMPLICIT_PRODUCER_HASH_SIZE == 0 || INITIAL_IMPLICIT_PRODUCER_HASH_SIZE >= 1, \"Traits::INITIAL_IMPLICIT_PRODUCER_HASH_SIZE must be at least 1 (or 0 to disable implicit enqueueing)\");\n\npublic:\n\t// Creates a queue with at least `capacity` element slots; note that the\n\t// actual number of elements that can be inserted without additional memory\n\t// allocation depends on the number of producers and the block size (e.g. if\n\t// the block size is equal to `capacity`, only a single block will be allocated\n\t// up-front, which means only a single producer will be able to enqueue elements\n\t// without an extra allocation -- blocks aren't shared between producers).\n\t// This method is not thread safe -- it is up to the user to ensure that the\n\t// queue is fully constructed before it starts being used by other threads (this\n\t// includes making the memory effects of construction visible, possibly with a\n\t// memory barrier).\n\texplicit ConcurrentQueue(size_t capacity = 6 * BLOCK_SIZE)\n\t\t: producerListTail(nullptr),\n\t\tproducerCount(0),\n\t\tinitialBlockPoolIndex(0),\n\t\tnextExplicitConsumerId(0),\n\t\tglobalExplicitConsumerOffset(0)\n\t{\n\t\timplicitProducerHashResizeInProgress.clear(std::memory_order_relaxed);\n\t\tpopulate_initial_implicit_producer_hash();\n\t\tpopulate_initial_block_list(capacity / BLOCK_SIZE + ((capacity & (BLOCK_SIZE - 1)) == 0 ? 0 : 1));\n\t\t\n#ifdef MOODYCAMEL_QUEUE_INTERNAL_DEBUG\n\t\t// Track all the producers using a fully-resolved typed list for\n\t\t// each kind; this makes it possible to debug them starting from\n\t\t// the root queue object (otherwise wacky casts are needed that\n\t\t// don't compile in the debugger's expression evaluator).\n\t\texplicitProducers.store(nullptr, std::memory_order_relaxed);\n\t\timplicitProducers.store(nullptr, std::memory_order_relaxed);\n#endif\n\t}\n\t\n\t// Computes the correct amount of pre-allocated blocks for you based\n\t// on the minimum number of elements you want available at any given\n\t// time, and the maximum concurrent number of each type of producer.\n\tConcurrentQueue(size_t minCapacity, size_t maxExplicitProducers, size_t maxImplicitProducers)\n\t\t: producerListTail(nullptr),\n\t\tproducerCount(0),\n\t\tinitialBlockPoolIndex(0),\n\t\tnextExplicitConsumerId(0),\n\t\tglobalExplicitConsumerOffset(0)\n\t{\n\t\timplicitProducerHashResizeInProgress.clear(std::memory_order_relaxed);\n\t\tpopulate_initial_implicit_producer_hash();\n\t\tsize_t blocks = (((minCapacity + BLOCK_SIZE - 1) / BLOCK_SIZE) - 1) * (maxExplicitProducers + 1) + 2 * (maxExplicitProducers + maxImplicitProducers);\n\t\tpopulate_initial_block_list(blocks);\n\t\t\n#ifdef MOODYCAMEL_QUEUE_INTERNAL_DEBUG\n\t\texplicitProducers.store(nullptr, std::memory_order_relaxed);\n\t\timplicitProducers.store(nullptr, std::memory_order_relaxed);\n#endif\n\t}\n\t\n\t// Note: The queue should not be accessed concurrently while it's\n\t// being deleted. It's up to the user to synchronize this.\n\t// This method is not thread safe.\n\t~ConcurrentQueue()\n\t{\n\t\t// Destroy producers\n\t\tauto ptr = producerListTail.load(std::memory_order_relaxed);\n\t\twhile (ptr != nullptr) {\n\t\t\tauto next = ptr->next_prod();\n\t\t\tif (ptr->token != nullptr) {\n\t\t\t\tptr->token->producer = nullptr;\n\t\t\t}\n\t\t\tdestroy(ptr);\n\t\t\tptr = next;\n\t\t}\n\t\t\n\t\t// Destroy implicit producer hash tables\n\t\tMOODYCAMEL_CONSTEXPR_IF (INITIAL_IMPLICIT_PRODUCER_HASH_SIZE != 0) {\n\t\t\tauto hash = implicitProducerHash.load(std::memory_order_relaxed);\n\t\t\twhile (hash != nullptr) {\n\t\t\t\tauto prev = hash->prev;\n\t\t\t\tif (prev != nullptr) {\t\t// The last hash is part of this object and was not allocated dynamically\n\t\t\t\t\tfor (size_t i = 0; i != hash->capacity; ++i) {\n\t\t\t\t\t\thash->entries[i].~ImplicitProducerKVP();\n\t\t\t\t\t}\n\t\t\t\t\thash->~ImplicitProducerHash();\n\t\t\t\t\t(Traits::free)(hash);\n\t\t\t\t}\n\t\t\t\thash = prev;\n\t\t\t}\n\t\t}\n\t\t\n\t\t// Destroy global free list\n\t\tauto block = freeList.head_unsafe();\n\t\twhile (block != nullptr) {\n\t\t\tauto next = block->freeListNext.load(std::memory_order_relaxed);\n\t\t\tif (block->dynamicallyAllocated) {\n\t\t\t\tdestroy(block);\n\t\t\t}\n\t\t\tblock = next;\n\t\t}\n\t\t\n\t\t// Destroy initial free list\n\t\tdestroy_array(initialBlockPool, initialBlockPoolSize);\n\t}\n\n\t// Disable copying and copy assignment\n\tConcurrentQueue(ConcurrentQueue const&) MOODYCAMEL_DELETE_FUNCTION;\n\tConcurrentQueue& operator=(ConcurrentQueue const&) MOODYCAMEL_DELETE_FUNCTION;\n\t\n\t// Moving is supported, but note that it is *not* a thread-safe operation.\n\t// Nobody can use the queue while it's being moved, and the memory effects\n\t// of that move must be propagated to other threads before they can use it.\n\t// Note: When a queue is moved, its tokens are still valid but can only be\n\t// used with the destination queue (i.e. semantically they are moved along\n\t// with the queue itself).\n\tConcurrentQueue(ConcurrentQueue&& other) MOODYCAMEL_NOEXCEPT\n\t\t: producerListTail(other.producerListTail.load(std::memory_order_relaxed)),\n\t\tproducerCount(other.producerCount.load(std::memory_order_relaxed)),\n\t\tinitialBlockPoolIndex(other.initialBlockPoolIndex.load(std::memory_order_relaxed)),\n\t\tinitialBlockPool(other.initialBlockPool),\n\t\tinitialBlockPoolSize(other.initialBlockPoolSize),\n\t\tfreeList(std::move(other.freeList)),\n\t\tnextExplicitConsumerId(other.nextExplicitConsumerId.load(std::memory_order_relaxed)),\n\t\tglobalExplicitConsumerOffset(other.globalExplicitConsumerOffset.load(std::memory_order_relaxed))\n\t{\n\t\t// Move the other one into this, and leave the other one as an empty queue\n\t\timplicitProducerHashResizeInProgress.clear(std::memory_order_relaxed);\n\t\tpopulate_initial_implicit_producer_hash();\n\t\tswap_implicit_producer_hashes(other);\n\t\t\n\t\tother.producerListTail.store(nullptr, std::memory_order_relaxed);\n\t\tother.producerCount.store(0, std::memory_order_relaxed);\n\t\tother.nextExplicitConsumerId.store(0, std::memory_order_relaxed);\n\t\tother.globalExplicitConsumerOffset.store(0, std::memory_order_relaxed);\n\t\t\n#ifdef MOODYCAMEL_QUEUE_INTERNAL_DEBUG\n\t\texplicitProducers.store(other.explicitProducers.load(std::memory_order_relaxed), std::memory_order_relaxed);\n\t\tother.explicitProducers.store(nullptr, std::memory_order_relaxed);\n\t\timplicitProducers.store(other.implicitProducers.load(std::memory_order_relaxed), std::memory_order_relaxed);\n\t\tother.implicitProducers.store(nullptr, std::memory_order_relaxed);\n#endif\n\t\t\n\t\tother.initialBlockPoolIndex.store(0, std::memory_order_relaxed);\n\t\tother.initialBlockPoolSize = 0;\n\t\tother.initialBlockPool = nullptr;\n\t\t\n\t\treown_producers();\n\t}\n\t\n\tinline ConcurrentQueue& operator=(ConcurrentQueue&& other) MOODYCAMEL_NOEXCEPT\n\t{\n\t\treturn swap_internal(other);\n\t}\n\t\n\t// Swaps this queue's state with the other's. Not thread-safe.\n\t// Swapping two queues does not invalidate their tokens, however\n\t// the tokens that were created for one queue must be used with\n\t// only the swapped queue (i.e. the tokens are tied to the\n\t// queue's movable state, not the object itself).\n\tinline void swap(ConcurrentQueue& other) MOODYCAMEL_NOEXCEPT\n\t{\n\t\tswap_internal(other);\n\t}\n\t\nprivate:\n\tConcurrentQueue& swap_internal(ConcurrentQueue& other)\n\t{\n\t\tif (this == &other) {\n\t\t\treturn *this;\n\t\t}\n\t\t\n\t\tdetails::swap_relaxed(producerListTail, other.producerListTail);\n\t\tdetails::swap_relaxed(producerCount, other.producerCount);\n\t\tdetails::swap_relaxed(initialBlockPoolIndex, other.initialBlockPoolIndex);\n\t\tstd::swap(initialBlockPool, other.initialBlockPool);\n\t\tstd::swap(initialBlockPoolSize, other.initialBlockPoolSize);\n\t\tfreeList.swap(other.freeList);\n\t\tdetails::swap_relaxed(nextExplicitConsumerId, other.nextExplicitConsumerId);\n\t\tdetails::swap_relaxed(globalExplicitConsumerOffset, other.globalExplicitConsumerOffset);\n\t\t\n\t\tswap_implicit_producer_hashes(other);\n\t\t\n\t\treown_producers();\n\t\tother.reown_producers();\n\t\t\n#ifdef MOODYCAMEL_QUEUE_INTERNAL_DEBUG\n\t\tdetails::swap_relaxed(explicitProducers, other.explicitProducers);\n\t\tdetails::swap_relaxed(implicitProducers, other.implicitProducers);\n#endif\n\t\t\n\t\treturn *this;\n\t}\n\t\npublic:\n\t// Enqueues a single item (by copying it).\n\t// Allocates memory if required. Only fails if memory allocation fails (or implicit\n\t// production is disabled because Traits::INITIAL_IMPLICIT_PRODUCER_HASH_SIZE is 0,\n\t// or Traits::MAX_SUBQUEUE_SIZE has been defined and would be surpassed).\n\t// Thread-safe.\n\tinline bool enqueue(T const& item)\n\t{\n\t\tMOODYCAMEL_CONSTEXPR_IF (INITIAL_IMPLICIT_PRODUCER_HASH_SIZE == 0) return false;\n\t\telse return inner_enqueue<CanAlloc>(item);\n\t}\n\t\n\t// Enqueues a single item (by moving it, if possible).\n\t// Allocates memory if required. Only fails if memory allocation fails (or implicit\n\t// production is disabled because Traits::INITIAL_IMPLICIT_PRODUCER_HASH_SIZE is 0,\n\t// or Traits::MAX_SUBQUEUE_SIZE has been defined and would be surpassed).\n\t// Thread-safe.\n\tinline bool enqueue(T&& item)\n\t{\n\t\tMOODYCAMEL_CONSTEXPR_IF (INITIAL_IMPLICIT_PRODUCER_HASH_SIZE == 0) return false;\n\t\telse return inner_enqueue<CanAlloc>(std::move(item));\n\t}\n\t\n\t// Enqueues a single item (by copying it) using an explicit producer token.\n\t// Allocates memory if required. Only fails if memory allocation fails (or\n\t// Traits::MAX_SUBQUEUE_SIZE has been defined and would be surpassed).\n\t// Thread-safe.\n\tinline bool enqueue(producer_token_t const& token, T const& item)\n\t{\n\t\treturn inner_enqueue<CanAlloc>(token, item);\n\t}\n\t\n\t// Enqueues a single item (by moving it, if possible) using an explicit producer token.\n\t// Allocates memory if required. Only fails if memory allocation fails (or\n\t// Traits::MAX_SUBQUEUE_SIZE has been defined and would be surpassed).\n\t// Thread-safe.\n\tinline bool enqueue(producer_token_t const& token, T&& item)\n\t{\n\t\treturn inner_enqueue<CanAlloc>(token, std::move(item));\n\t}\n\t\n\t// Enqueues several items.\n\t// Allocates memory if required. Only fails if memory allocation fails (or\n\t// implicit production is disabled because Traits::INITIAL_IMPLICIT_PRODUCER_HASH_SIZE\n\t// is 0, or Traits::MAX_SUBQUEUE_SIZE has been defined and would be surpassed).\n\t// Note: Use std::make_move_iterator if the elements should be moved instead of copied.\n\t// Thread-safe.\n\ttemplate<typename It>\n\tbool enqueue_bulk(It itemFirst, size_t count)\n\t{\n\t\tMOODYCAMEL_CONSTEXPR_IF (INITIAL_IMPLICIT_PRODUCER_HASH_SIZE == 0) return false;\n\t\telse return inner_enqueue_bulk<CanAlloc>(itemFirst, count);\n\t}\n\t\n\t// Enqueues several items using an explicit producer token.\n\t// Allocates memory if required. Only fails if memory allocation fails\n\t// (or Traits::MAX_SUBQUEUE_SIZE has been defined and would be surpassed).\n\t// Note: Use std::make_move_iterator if the elements should be moved\n\t// instead of copied.\n\t// Thread-safe.\n\ttemplate<typename It>\n\tbool enqueue_bulk(producer_token_t const& token, It itemFirst, size_t count)\n\t{\n\t\treturn inner_enqueue_bulk<CanAlloc>(token, itemFirst, count);\n\t}\n\t\n\t// Enqueues a single item (by copying it).\n\t// Does not allocate memory. Fails if not enough room to enqueue (or implicit\n\t// production is disabled because Traits::INITIAL_IMPLICIT_PRODUCER_HASH_SIZE\n\t// is 0).\n\t// Thread-safe.\n\tinline bool try_enqueue(T const& item)\n\t{\n\t\tMOODYCAMEL_CONSTEXPR_IF (INITIAL_IMPLICIT_PRODUCER_HASH_SIZE == 0) return false;\n\t\telse return inner_enqueue<CannotAlloc>(item);\n\t}\n\t\n\t// Enqueues a single item (by moving it, if possible).\n\t// Does not allocate memory (except for one-time implicit producer).\n\t// Fails if not enough room to enqueue (or implicit production is\n\t// disabled because Traits::INITIAL_IMPLICIT_PRODUCER_HASH_SIZE is 0).\n\t// Thread-safe.\n\tinline bool try_enqueue(T&& item)\n\t{\n\t\tMOODYCAMEL_CONSTEXPR_IF (INITIAL_IMPLICIT_PRODUCER_HASH_SIZE == 0) return false;\n\t\telse return inner_enqueue<CannotAlloc>(std::move(item));\n\t}\n\t\n\t// Enqueues a single item (by copying it) using an explicit producer token.\n\t// Does not allocate memory. Fails if not enough room to enqueue.\n\t// Thread-safe.\n\tinline bool try_enqueue(producer_token_t const& token, T const& item)\n\t{\n\t\treturn inner_enqueue<CannotAlloc>(token, item);\n\t}\n\t\n\t// Enqueues a single item (by moving it, if possible) using an explicit producer token.\n\t// Does not allocate memory. Fails if not enough room to enqueue.\n\t// Thread-safe.\n\tinline bool try_enqueue(producer_token_t const& token, T&& item)\n\t{\n\t\treturn inner_enqueue<CannotAlloc>(token, std::move(item));\n\t}\n\t\n\t// Enqueues several items.\n\t// Does not allocate memory (except for one-time implicit producer).\n\t// Fails if not enough room to enqueue (or implicit production is\n\t// disabled because Traits::INITIAL_IMPLICIT_PRODUCER_HASH_SIZE is 0).\n\t// Note: Use std::make_move_iterator if the elements should be moved\n\t// instead of copied.\n\t// Thread-safe.\n\ttemplate<typename It>\n\tbool try_enqueue_bulk(It itemFirst, size_t count)\n\t{\n\t\tMOODYCAMEL_CONSTEXPR_IF (INITIAL_IMPLICIT_PRODUCER_HASH_SIZE == 0) return false;\n\t\telse return inner_enqueue_bulk<CannotAlloc>(itemFirst, count);\n\t}\n\t\n\t// Enqueues several items using an explicit producer token.\n\t// Does not allocate memory. Fails if not enough room to enqueue.\n\t// Note: Use std::make_move_iterator if the elements should be moved\n\t// instead of copied.\n\t// Thread-safe.\n\ttemplate<typename It>\n\tbool try_enqueue_bulk(producer_token_t const& token, It itemFirst, size_t count)\n\t{\n\t\treturn inner_enqueue_bulk<CannotAlloc>(token, itemFirst, count);\n\t}\n\t\n\t\n\t\n\t// Attempts to dequeue from the queue.\n\t// Returns false if all producer streams appeared empty at the time they\n\t// were checked (so, the queue is likely but not guaranteed to be empty).\n\t// Never allocates. Thread-safe.\n\ttemplate<typename U>\n\tbool try_dequeue(U& item)\n\t{\n\t\t// Instead of simply trying each producer in turn (which could cause needless contention on the first\n\t\t// producer), we score them heuristically.\n\t\tsize_t nonEmptyCount = 0;\n\t\tProducerBase* best = nullptr;\n\t\tsize_t bestSize = 0;\n\t\tfor (auto ptr = producerListTail.load(std::memory_order_acquire); nonEmptyCount < 3 && ptr != nullptr; ptr = ptr->next_prod()) {\n\t\t\tauto size = ptr->size_approx();\n\t\t\tif (size > 0) {\n\t\t\t\tif (size > bestSize) {\n\t\t\t\t\tbestSize = size;\n\t\t\t\t\tbest = ptr;\n\t\t\t\t}\n\t\t\t\t++nonEmptyCount;\n\t\t\t}\n\t\t}\n\t\t\n\t\t// If there was at least one non-empty queue but it appears empty at the time\n\t\t// we try to dequeue from it, we need to make sure every queue's been tried\n\t\tif (nonEmptyCount > 0) {\n\t\t\tif ((details::likely)(best->dequeue(item))) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tfor (auto ptr = producerListTail.load(std::memory_order_acquire); ptr != nullptr; ptr = ptr->next_prod()) {\n\t\t\t\tif (ptr != best && ptr->dequeue(item)) {\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn false;\n\t}\n\t\n\t// Attempts to dequeue from the queue.\n\t// Returns false if all producer streams appeared empty at the time they\n\t// were checked (so, the queue is likely but not guaranteed to be empty).\n\t// This differs from the try_dequeue(item) method in that this one does\n\t// not attempt to reduce contention by interleaving the order that producer\n\t// streams are dequeued from. So, using this method can reduce overall throughput\n\t// under contention, but will give more predictable results in single-threaded\n\t// consumer scenarios. This is mostly only useful for internal unit tests.\n\t// Never allocates. Thread-safe.\n\ttemplate<typename U>\n\tbool try_dequeue_non_interleaved(U& item)\n\t{\n\t\tfor (auto ptr = producerListTail.load(std::memory_order_acquire); ptr != nullptr; ptr = ptr->next_prod()) {\n\t\t\tif (ptr->dequeue(item)) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t\treturn false;\n\t}\n\t\n\t// Attempts to dequeue from the queue using an explicit consumer token.\n\t// Returns false if all producer streams appeared empty at the time they\n\t// were checked (so, the queue is likely but not guaranteed to be empty).\n\t// Never allocates. Thread-safe.\n\ttemplate<typename U>\n\tbool try_dequeue(consumer_token_t& token, U& item)\n\t{\n\t\t// The idea is roughly as follows:\n\t\t// Every 256 items from one producer, make everyone rotate (increase the global offset) -> this means the highest efficiency consumer dictates the rotation speed of everyone else, more or less\n\t\t// If you see that the global offset has changed, you must reset your consumption counter and move to your designated place\n\t\t// If there's no items where you're supposed to be, keep moving until you find a producer with some items\n\t\t// If the global offset has not changed but you've run out of items to consume, move over from your current position until you find an producer with something in it\n\t\t\n\t\tif (token.desiredProducer == nullptr || token.lastKnownGlobalOffset != globalExplicitConsumerOffset.load(std::memory_order_relaxed)) {\n\t\t\tif (!update_current_producer_after_rotation(token)) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t\t\n\t\t// If there was at least one non-empty queue but it appears empty at the time\n\t\t// we try to dequeue from it, we need to make sure every queue's been tried\n\t\tif (static_cast<ProducerBase*>(token.currentProducer)->dequeue(item)) {\n\t\t\tif (++token.itemsConsumedFromCurrent == EXPLICIT_CONSUMER_CONSUMPTION_QUOTA_BEFORE_ROTATE) {\n\t\t\t\tglobalExplicitConsumerOffset.fetch_add(1, std::memory_order_relaxed);\n\t\t\t}\n\t\t\treturn true;\n\t\t}\n\t\t\n\t\tauto tail = producerListTail.load(std::memory_order_acquire);\n\t\tauto ptr = static_cast<ProducerBase*>(token.currentProducer)->next_prod();\n\t\tif (ptr == nullptr) {\n\t\t\tptr = tail;\n\t\t}\n\t\twhile (ptr != static_cast<ProducerBase*>(token.currentProducer)) {\n\t\t\tif (ptr->dequeue(item)) {\n\t\t\t\ttoken.currentProducer = ptr;\n\t\t\t\ttoken.itemsConsumedFromCurrent = 1;\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tptr = ptr->next_prod();\n\t\t\tif (ptr == nullptr) {\n\t\t\t\tptr = tail;\n\t\t\t}\n\t\t}\n\t\treturn false;\n\t}\n\t\n\t// Attempts to dequeue several elements from the queue.\n\t// Returns the number of items actually dequeued.\n\t// Returns 0 if all producer streams appeared empty at the time they\n\t// were checked (so, the queue is likely but not guaranteed to be empty).\n\t// Never allocates. Thread-safe.\n\ttemplate<typename It>\n\tsize_t try_dequeue_bulk(It itemFirst, size_t max)\n\t{\n\t\tsize_t count = 0;\n\t\tfor (auto ptr = producerListTail.load(std::memory_order_acquire); ptr != nullptr; ptr = ptr->next_prod()) {\n\t\t\tcount += ptr->dequeue_bulk(itemFirst, max - count);\n\t\t\tif (count == max) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\treturn count;\n\t}\n\t\n\t// Attempts to dequeue several elements from the queue using an explicit consumer token.\n\t// Returns the number of items actually dequeued.\n\t// Returns 0 if all producer streams appeared empty at the time they\n\t// were checked (so, the queue is likely but not guaranteed to be empty).\n\t// Never allocates. Thread-safe.\n\ttemplate<typename It>\n\tsize_t try_dequeue_bulk(consumer_token_t& token, It itemFirst, size_t max)\n\t{\n\t\tif (token.desiredProducer == nullptr || token.lastKnownGlobalOffset != globalExplicitConsumerOffset.load(std::memory_order_relaxed)) {\n\t\t\tif (!update_current_producer_after_rotation(token)) {\n\t\t\t\treturn 0;\n\t\t\t}\n\t\t}\n\t\t\n\t\tsize_t count = static_cast<ProducerBase*>(token.currentProducer)->dequeue_bulk(itemFirst, max);\n\t\tif (count == max) {\n\t\t\tif ((token.itemsConsumedFromCurrent += static_cast<std::uint32_t>(max)) >= EXPLICIT_CONSUMER_CONSUMPTION_QUOTA_BEFORE_ROTATE) {\n\t\t\t\tglobalExplicitConsumerOffset.fetch_add(1, std::memory_order_relaxed);\n\t\t\t}\n\t\t\treturn max;\n\t\t}\n\t\ttoken.itemsConsumedFromCurrent += static_cast<std::uint32_t>(count);\n\t\tmax -= count;\n\t\t\n\t\tauto tail = producerListTail.load(std::memory_order_acquire);\n\t\tauto ptr = static_cast<ProducerBase*>(token.currentProducer)->next_prod();\n\t\tif (ptr == nullptr) {\n\t\t\tptr = tail;\n\t\t}\n\t\twhile (ptr != static_cast<ProducerBase*>(token.currentProducer)) {\n\t\t\tauto dequeued = ptr->dequeue_bulk(itemFirst, max);\n\t\t\tcount += dequeued;\n\t\t\tif (dequeued != 0) {\n\t\t\t\ttoken.currentProducer = ptr;\n\t\t\t\ttoken.itemsConsumedFromCurrent = static_cast<std::uint32_t>(dequeued);\n\t\t\t}\n\t\t\tif (dequeued == max) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tmax -= dequeued;\n\t\t\tptr = ptr->next_prod();\n\t\t\tif (ptr == nullptr) {\n\t\t\t\tptr = tail;\n\t\t\t}\n\t\t}\n\t\treturn count;\n\t}\n\t\n\t\n\t\n\t// Attempts to dequeue from a specific producer's inner queue.\n\t// If you happen to know which producer you want to dequeue from, this\n\t// is significantly faster than using the general-case try_dequeue methods.\n\t// Returns false if the producer's queue appeared empty at the time it\n\t// was checked (so, the queue is likely but not guaranteed to be empty).\n\t// Never allocates. Thread-safe.\n\ttemplate<typename U>\n\tinline bool try_dequeue_from_producer(producer_token_t const& producer, U& item)\n\t{\n\t\treturn static_cast<ExplicitProducer*>(producer.producer)->dequeue(item);\n\t}\n\t\n\t// Attempts to dequeue several elements from a specific producer's inner queue.\n\t// Returns the number of items actually dequeued.\n\t// If you happen to know which producer you want to dequeue from, this\n\t// is significantly faster than using the general-case try_dequeue methods.\n\t// Returns 0 if the producer's queue appeared empty at the time it\n\t// was checked (so, the queue is likely but not guaranteed to be empty).\n\t// Never allocates. Thread-safe.\n\ttemplate<typename It>\n\tinline size_t try_dequeue_bulk_from_producer(producer_token_t const& producer, It itemFirst, size_t max)\n\t{\n\t\treturn static_cast<ExplicitProducer*>(producer.producer)->dequeue_bulk(itemFirst, max);\n\t}\n\t\n\t\n\t// Returns an estimate of the total number of elements currently in the queue. This\n\t// estimate is only accurate if the queue has completely stabilized before it is called\n\t// (i.e. all enqueue and dequeue operations have completed and their memory effects are\n\t// visible on the calling thread, and no further operations start while this method is\n\t// being called).\n\t// Thread-safe.\n\tsize_t size_approx() const\n\t{\n\t\tsize_t size = 0;\n\t\tfor (auto ptr = producerListTail.load(std::memory_order_acquire); ptr != nullptr; ptr = ptr->next_prod()) {\n\t\t\tsize += ptr->size_approx();\n\t\t}\n\t\treturn size;\n\t}\n\t\n\t\n\t// Returns true if the underlying atomic variables used by\n\t// the queue are lock-free (they should be on most platforms).\n\t// Thread-safe.\n\tstatic bool is_lock_free()\n\t{\n\t\treturn\n\t\t\tdetails::static_is_lock_free<bool>::value == 2 &&\n\t\t\tdetails::static_is_lock_free<size_t>::value == 2 &&\n\t\t\tdetails::static_is_lock_free<std::uint32_t>::value == 2 &&\n\t\t\tdetails::static_is_lock_free<index_t>::value == 2 &&\n\t\t\tdetails::static_is_lock_free<void*>::value == 2 &&\n\t\t\tdetails::static_is_lock_free<typename details::thread_id_converter<details::thread_id_t>::thread_id_numeric_size_t>::value == 2;\n\t}\n\n\nprivate:\n\tfriend struct ProducerToken;\n\tfriend struct ConsumerToken;\n\tstruct ExplicitProducer;\n\tfriend struct ExplicitProducer;\n\tstruct ImplicitProducer;\n\tfriend struct ImplicitProducer;\n\tfriend class ConcurrentQueueTests;\n\t\t\n\tenum AllocationMode { CanAlloc, CannotAlloc };\n\t\n\t\n\t///////////////////////////////\n\t// Queue methods\n\t///////////////////////////////\n\t\n\ttemplate<AllocationMode canAlloc, typename U>\n\tinline bool inner_enqueue(producer_token_t const& token, U&& element)\n\t{\n\t\treturn static_cast<ExplicitProducer*>(token.producer)->ConcurrentQueue::ExplicitProducer::template enqueue<canAlloc>(std::forward<U>(element));\n\t}\n\t\n\ttemplate<AllocationMode canAlloc, typename U>\n\tinline bool inner_enqueue(U&& element)\n\t{\n\t\tauto producer = get_or_add_implicit_producer();\n\t\treturn producer == nullptr ? false : producer->ConcurrentQueue::ImplicitProducer::template enqueue<canAlloc>(std::forward<U>(element));\n\t}\n\t\n\ttemplate<AllocationMode canAlloc, typename It>\n\tinline bool inner_enqueue_bulk(producer_token_t const& token, It itemFirst, size_t count)\n\t{\n\t\treturn static_cast<ExplicitProducer*>(token.producer)->ConcurrentQueue::ExplicitProducer::template enqueue_bulk<canAlloc>(itemFirst, count);\n\t}\n\t\n\ttemplate<AllocationMode canAlloc, typename It>\n\tinline bool inner_enqueue_bulk(It itemFirst, size_t count)\n\t{\n\t\tauto producer = get_or_add_implicit_producer();\n\t\treturn producer == nullptr ? false : producer->ConcurrentQueue::ImplicitProducer::template enqueue_bulk<canAlloc>(itemFirst, count);\n\t}\n\t\n\tinline bool update_current_producer_after_rotation(consumer_token_t& token)\n\t{\n\t\t// Ah, there's been a rotation, figure out where we should be!\n\t\tauto tail = producerListTail.load(std::memory_order_acquire);\n\t\tif (token.desiredProducer == nullptr && tail == nullptr) {\n\t\t\treturn false;\n\t\t}\n\t\tauto prodCount = producerCount.load(std::memory_order_relaxed);\n\t\tauto globalOffset = globalExplicitConsumerOffset.load(std::memory_order_relaxed);\n\t\tif ((details::unlikely)(token.desiredProducer == nullptr)) {\n\t\t\t// Aha, first time we're dequeueing anything.\n\t\t\t// Figure out our local position\n\t\t\t// Note: offset is from start, not end, but we're traversing from end -- subtract from count first\n\t\t\tstd::uint32_t offset = prodCount - 1 - (token.initialOffset % prodCount);\n\t\t\ttoken.desiredProducer = tail;\n\t\t\tfor (std::uint32_t i = 0; i != offset; ++i) {\n\t\t\t\ttoken.desiredProducer = static_cast<ProducerBase*>(token.desiredProducer)->next_prod();\n\t\t\t\tif (token.desiredProducer == nullptr) {\n\t\t\t\t\ttoken.desiredProducer = tail;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\tstd::uint32_t delta = globalOffset - token.lastKnownGlobalOffset;\n\t\tif (delta >= prodCount) {\n\t\t\tdelta = delta % prodCount;\n\t\t}\n\t\tfor (std::uint32_t i = 0; i != delta; ++i) {\n\t\t\ttoken.desiredProducer = static_cast<ProducerBase*>(token.desiredProducer)->next_prod();\n\t\t\tif (token.desiredProducer == nullptr) {\n\t\t\t\ttoken.desiredProducer = tail;\n\t\t\t}\n\t\t}\n\t\t\n\t\ttoken.lastKnownGlobalOffset = globalOffset;\n\t\ttoken.currentProducer = token.desiredProducer;\n\t\ttoken.itemsConsumedFromCurrent = 0;\n\t\treturn true;\n\t}\n\t\n\t\n\t///////////////////////////\n\t// Free list\n\t///////////////////////////\n\t\n\ttemplate <typename N>\n\tstruct FreeListNode\n\t{\n\t\tFreeListNode() : freeListRefs(0), freeListNext(nullptr) { }\n\t\t\n\t\tstd::atomic<std::uint32_t> freeListRefs;\n\t\tstd::atomic<N*> freeListNext;\n\t};\n\t\n\t// A simple CAS-based lock-free free list. Not the fastest thing in the world under heavy contention, but\n\t// simple and correct (assuming nodes are never freed until after the free list is destroyed), and fairly\n\t// speedy under low contention.\n\ttemplate<typename N>\t\t// N must inherit FreeListNode or have the same fields (and initialization of them)\n\tstruct FreeList\n\t{\n\t\tFreeList() : freeListHead(nullptr) { }\n\t\tFreeList(FreeList&& other) : freeListHead(other.freeListHead.load(std::memory_order_relaxed)) { other.freeListHead.store(nullptr, std::memory_order_relaxed); }\n\t\tvoid swap(FreeList& other) { details::swap_relaxed(freeListHead, other.freeListHead); }\n\t\t\n\t\tFreeList(FreeList const&) MOODYCAMEL_DELETE_FUNCTION;\n\t\tFreeList& operator=(FreeList const&) MOODYCAMEL_DELETE_FUNCTION;\n\t\t\n\t\tinline void add(N* node)\n\t\t{\n#ifdef MCDBGQ_NOLOCKFREE_FREELIST\n\t\t\tdebug::DebugLock lock(mutex);\n#endif\t\t\n\t\t\t// We know that the should-be-on-freelist bit is 0 at this point, so it's safe to\n\t\t\t// set it using a fetch_add\n\t\t\tif (node->freeListRefs.fetch_add(SHOULD_BE_ON_FREELIST, std::memory_order_acq_rel) == 0) {\n\t\t\t\t// Oh look! We were the last ones referencing this node, and we know\n\t\t\t\t// we want to add it to the free list, so let's do it!\n\t\t \t\tadd_knowing_refcount_is_zero(node);\n\t\t\t}\n\t\t}\n\t\t\n\t\tinline N* try_get()\n\t\t{\n#ifdef MCDBGQ_NOLOCKFREE_FREELIST\n\t\t\tdebug::DebugLock lock(mutex);\n#endif\t\t\n\t\t\tauto head = freeListHead.load(std::memory_order_acquire);\n\t\t\twhile (head != nullptr) {\n\t\t\t\tauto prevHead = head;\n\t\t\t\tauto refs = head->freeListRefs.load(std::memory_order_relaxed);\n\t\t\t\tif ((refs & REFS_MASK) == 0 || !head->freeListRefs.compare_exchange_strong(refs, refs + 1, std::memory_order_acquire, std::memory_order_relaxed)) {\n\t\t\t\t\thead = freeListHead.load(std::memory_order_acquire);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\t// Good, reference count has been incremented (it wasn't at zero), which means we can read the\n\t\t\t\t// next and not worry about it changing between now and the time we do the CAS\n\t\t\t\tauto next = head->freeListNext.load(std::memory_order_relaxed);\n\t\t\t\tif (freeListHead.compare_exchange_strong(head, next, std::memory_order_acquire, std::memory_order_relaxed)) {\n\t\t\t\t\t// Yay, got the node. This means it was on the list, which means shouldBeOnFreeList must be false no\n\t\t\t\t\t// matter the refcount (because nobody else knows it's been taken off yet, it can't have been put back on).\n\t\t\t\t\tassert((head->freeListRefs.load(std::memory_order_relaxed) & SHOULD_BE_ON_FREELIST) == 0);\n\t\t\t\t\t\n\t\t\t\t\t// Decrease refcount twice, once for our ref, and once for the list's ref\n\t\t\t\t\thead->freeListRefs.fetch_sub(2, std::memory_order_release);\n\t\t\t\t\treturn head;\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\t// OK, the head must have changed on us, but we still need to decrease the refcount we increased.\n\t\t\t\t// Note that we don't need to release any memory effects, but we do need to ensure that the reference\n\t\t\t\t// count decrement happens-after the CAS on the head.\n\t\t\t\trefs = prevHead->freeListRefs.fetch_sub(1, std::memory_order_acq_rel);\n\t\t\t\tif (refs == SHOULD_BE_ON_FREELIST + 1) {\n\t\t\t\t\tadd_knowing_refcount_is_zero(prevHead);\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\treturn nullptr;\n\t\t}\n\t\t\n\t\t// Useful for traversing the list when there's no contention (e.g. to destroy remaining nodes)\n\t\tN* head_unsafe() const { return freeListHead.load(std::memory_order_relaxed); }\n\t\t\n\tprivate:\n\t\tinline void add_knowing_refcount_is_zero(N* node)\n\t\t{\n\t\t\t// Since the refcount is zero, and nobody can increase it once it's zero (except us, and we run\n\t\t\t// only one copy of this method per node at a time, i.e. the single thread case), then we know\n\t\t\t// we can safely change the next pointer of the node; however, once the refcount is back above\n\t\t\t// zero, then other threads could increase it (happens under heavy contention, when the refcount\n\t\t\t// goes to zero in between a load and a refcount increment of a node in try_get, then back up to\n\t\t\t// something non-zero, then the refcount increment is done by the other thread) -- so, if the CAS\n\t\t\t// to add the node to the actual list fails, decrease the refcount and leave the add operation to\n\t\t\t// the next thread who puts the refcount back at zero (which could be us, hence the loop).\n\t\t\tauto head = freeListHead.load(std::memory_order_relaxed);\n\t\t\twhile (true) {\n\t\t\t\tnode->freeListNext.store(head, std::memory_order_relaxed);\n\t\t\t\tnode->freeListRefs.store(1, std::memory_order_release);\n\t\t\t\tif (!freeListHead.compare_exchange_strong(head, node, std::memory_order_release, std::memory_order_relaxed)) {\n\t\t\t\t\t// Hmm, the add failed, but we can only try again when the refcount goes back to zero\n\t\t\t\t\tif (node->freeListRefs.fetch_add(SHOULD_BE_ON_FREELIST - 1, std::memory_order_release) == 1) {\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t\t\n\tprivate:\n\t\t// Implemented like a stack, but where node order doesn't matter (nodes are inserted out of order under contention)\n\t\tstd::atomic<N*> freeListHead;\n\t\n\tstatic const std::uint32_t REFS_MASK = 0x7FFFFFFF;\n\tstatic const std::uint32_t SHOULD_BE_ON_FREELIST = 0x80000000;\n\t\t\n#ifdef MCDBGQ_NOLOCKFREE_FREELIST\n\t\tdebug::DebugMutex mutex;\n#endif\n\t};\n\t\n\t\n\t///////////////////////////\n\t// Block\n\t///////////////////////////\n\t\n\tenum InnerQueueContext { implicit_context = 0, explicit_context = 1 };\n\t\n\tstruct Block\n\t{\n\t\tBlock()\n\t\t\t: next(nullptr), elementsCompletelyDequeued(0), freeListRefs(0), freeListNext(nullptr), shouldBeOnFreeList(false), dynamicallyAllocated(true)\n\t\t{\n#ifdef MCDBGQ_TRACKMEM\n\t\t\towner = nullptr;\n#endif\n\t\t}\n\t\t\n\t\ttemplate<InnerQueueContext context>\n\t\tinline bool is_empty() const\n\t\t{\n\t\t\tMOODYCAMEL_CONSTEXPR_IF (context == explicit_context && BLOCK_SIZE <= EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD) {\n\t\t\t\t// Check flags\n\t\t\t\tfor (size_t i = 0; i < BLOCK_SIZE; ++i) {\n\t\t\t\t\tif (!emptyFlags[i].load(std::memory_order_relaxed)) {\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\t// Aha, empty; make sure we have all other memory effects that happened before the empty flags were set\n\t\t\t\tstd::atomic_thread_fence(std::memory_order_acquire);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\telse {\n\t\t\t\t// Check counter\n\t\t\t\tif (elementsCompletelyDequeued.load(std::memory_order_relaxed) == BLOCK_SIZE) {\n\t\t\t\t\tstd::atomic_thread_fence(std::memory_order_acquire);\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\tassert(elementsCompletelyDequeued.load(std::memory_order_relaxed) <= BLOCK_SIZE);\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t\t\n\t\t// Returns true if the block is now empty (does not apply in explicit context)\n\t\ttemplate<InnerQueueContext context>\n\t\tinline bool set_empty(MOODYCAMEL_MAYBE_UNUSED index_t i)\n\t\t{\n\t\t\tMOODYCAMEL_CONSTEXPR_IF (context == explicit_context && BLOCK_SIZE <= EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD) {\n\t\t\t\t// Set flag\n\t\t\t\tassert(!emptyFlags[BLOCK_SIZE - 1 - static_cast<size_t>(i & static_cast<index_t>(BLOCK_SIZE - 1))].load(std::memory_order_relaxed));\n\t\t\t\temptyFlags[BLOCK_SIZE - 1 - static_cast<size_t>(i & static_cast<index_t>(BLOCK_SIZE - 1))].store(true, std::memory_order_release);\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\telse {\n\t\t\t\t// Increment counter\n\t\t\t\tauto prevVal = elementsCompletelyDequeued.fetch_add(1, std::memory_order_release);\n\t\t\t\tassert(prevVal < BLOCK_SIZE);\n\t\t\t\treturn prevVal == BLOCK_SIZE - 1;\n\t\t\t}\n\t\t}\n\t\t\n\t\t// Sets multiple contiguous item statuses to 'empty' (assumes no wrapping and count > 0).\n\t\t// Returns true if the block is now empty (does not apply in explicit context).\n\t\ttemplate<InnerQueueContext context>\n\t\tinline bool set_many_empty(MOODYCAMEL_MAYBE_UNUSED index_t i, size_t count)\n\t\t{\n\t\t\tMOODYCAMEL_CONSTEXPR_IF (context == explicit_context && BLOCK_SIZE <= EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD) {\n\t\t\t\t// Set flags\n\t\t\t\tstd::atomic_thread_fence(std::memory_order_release);\n\t\t\t\ti = BLOCK_SIZE - 1 - static_cast<size_t>(i & static_cast<index_t>(BLOCK_SIZE - 1)) - count + 1;\n\t\t\t\tfor (size_t j = 0; j != count; ++j) {\n\t\t\t\t\tassert(!emptyFlags[i + j].load(std::memory_order_relaxed));\n\t\t\t\t\temptyFlags[i + j].store(true, std::memory_order_relaxed);\n\t\t\t\t}\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\telse {\n\t\t\t\t// Increment counter\n\t\t\t\tauto prevVal = elementsCompletelyDequeued.fetch_add(count, std::memory_order_release);\n\t\t\t\tassert(prevVal + count <= BLOCK_SIZE);\n\t\t\t\treturn prevVal + count == BLOCK_SIZE;\n\t\t\t}\n\t\t}\n\t\t\n\t\ttemplate<InnerQueueContext context>\n\t\tinline void set_all_empty()\n\t\t{\n\t\t\tMOODYCAMEL_CONSTEXPR_IF (context == explicit_context && BLOCK_SIZE <= EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD) {\n\t\t\t\t// Set all flags\n\t\t\t\tfor (size_t i = 0; i != BLOCK_SIZE; ++i) {\n\t\t\t\t\temptyFlags[i].store(true, std::memory_order_relaxed);\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\t// Reset counter\n\t\t\t\telementsCompletelyDequeued.store(BLOCK_SIZE, std::memory_order_relaxed);\n\t\t\t}\n\t\t}\n\t\t\n\t\ttemplate<InnerQueueContext context>\n\t\tinline void reset_empty()\n\t\t{\n\t\t\tMOODYCAMEL_CONSTEXPR_IF (context == explicit_context && BLOCK_SIZE <= EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD) {\n\t\t\t\t// Reset flags\n\t\t\t\tfor (size_t i = 0; i != BLOCK_SIZE; ++i) {\n\t\t\t\t\temptyFlags[i].store(false, std::memory_order_relaxed);\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\t// Reset counter\n\t\t\t\telementsCompletelyDequeued.store(0, std::memory_order_relaxed);\n\t\t\t}\n\t\t}\n\t\t\n\t\tinline T* operator[](index_t idx) MOODYCAMEL_NOEXCEPT { return static_cast<T*>(static_cast<void*>(elements)) + static_cast<size_t>(idx & static_cast<index_t>(BLOCK_SIZE - 1)); }\n\t\tinline T const* operator[](index_t idx) const MOODYCAMEL_NOEXCEPT { return static_cast<T const*>(static_cast<void const*>(elements)) + static_cast<size_t>(idx & static_cast<index_t>(BLOCK_SIZE - 1)); }\n\t\t\n\tprivate:\n\t\tstatic_assert(std::alignment_of<T>::value <= sizeof(T), \"The queue does not support types with an alignment greater than their size at this time\");\n\t\tMOODYCAMEL_ALIGNED_TYPE_LIKE(char[sizeof(T) * BLOCK_SIZE], T) elements;\n\tpublic:\n\t\tBlock* next;\n\t\tstd::atomic<size_t> elementsCompletelyDequeued;\n\t\tstd::atomic<bool> emptyFlags[BLOCK_SIZE <= EXPLICIT_BLOCK_EMPTY_COUNTER_THRESHOLD ? BLOCK_SIZE : 1];\n\tpublic:\n\t\tstd::atomic<std::uint32_t> freeListRefs;\n\t\tstd::atomic<Block*> freeListNext;\n\t\tstd::atomic<bool> shouldBeOnFreeList;\n\t\tbool dynamicallyAllocated;\t\t// Perhaps a better name for this would be 'isNotPartOfInitialBlockPool'\n\t\t\n#ifdef MCDBGQ_TRACKMEM\n\t\tvoid* owner;\n#endif\n\t};\n\tstatic_assert(std::alignment_of<Block>::value >= std::alignment_of<T>::value, \"Internal error: Blocks must be at least as aligned as the type they are wrapping\");\n\n\n#ifdef MCDBGQ_TRACKMEM\npublic:\n\tstruct MemStats;\nprivate:\n#endif\n\t\n\t///////////////////////////\n\t// Producer base\n\t///////////////////////////\n\t\n\tstruct ProducerBase : public details::ConcurrentQueueProducerTypelessBase\n\t{\n\t\tProducerBase(ConcurrentQueue* parent_, bool isExplicit_) :\n\t\t\ttailIndex(0),\n\t\t\theadIndex(0),\n\t\t\tdequeueOptimisticCount(0),\n\t\t\tdequeueOvercommit(0),\n\t\t\ttailBlock(nullptr),\n\t\t\tisExplicit(isExplicit_),\n\t\t\tparent(parent_)\n\t\t{\n\t\t}\n\t\t\n\t\tvirtual ~ProducerBase() { }\n\t\t\n\t\ttemplate<typename U>\n\t\tinline bool dequeue(U& element)\n\t\t{\n\t\t\tif (isExplicit) {\n\t\t\t\treturn static_cast<ExplicitProducer*>(this)->dequeue(element);\n\t\t\t}\n\t\t\telse {\n\t\t\t\treturn static_cast<ImplicitProducer*>(this)->dequeue(element);\n\t\t\t}\n\t\t}\n\t\t\n\t\ttemplate<typename It>\n\t\tinline size_t dequeue_bulk(It& itemFirst, size_t max)\n\t\t{\n\t\t\tif (isExplicit) {\n\t\t\t\treturn static_cast<ExplicitProducer*>(this)->dequeue_bulk(itemFirst, max);\n\t\t\t}\n\t\t\telse {\n\t\t\t\treturn static_cast<ImplicitProducer*>(this)->dequeue_bulk(itemFirst, max);\n\t\t\t}\n\t\t}\n\t\t\n\t\tinline ProducerBase* next_prod() const { return static_cast<ProducerBase*>(next); }\n\t\t\n\t\tinline size_t size_approx() const\n\t\t{\n\t\t\tauto tail = tailIndex.load(std::memory_order_relaxed);\n\t\t\tauto head = headIndex.load(std::memory_order_relaxed);\n\t\t\treturn details::circular_less_than(head, tail) ? static_cast<size_t>(tail - head) : 0;\n\t\t}\n\t\t\n\t\tinline index_t getTail() const { return tailIndex.load(std::memory_order_relaxed); }\n\tprotected:\n\t\tstd::atomic<index_t> tailIndex;\t\t// Where to enqueue to next\n\t\tstd::atomic<index_t> headIndex;\t\t// Where to dequeue from next\n\t\t\n\t\tstd::atomic<index_t> dequeueOptimisticCount;\n\t\tstd::atomic<index_t> dequeueOvercommit;\n\t\t\n\t\tBlock* tailBlock;\n\t\t\n\tpublic:\n\t\tbool isExplicit;\n\t\tConcurrentQueue* parent;\n\t\t\n\tprotected:\n#ifdef MCDBGQ_TRACKMEM\n\t\tfriend struct MemStats;\n#endif\n\t};\n\t\n\t\n\t///////////////////////////\n\t// Explicit queue\n\t///////////////////////////\n\t\t\n\tstruct ExplicitProducer : public ProducerBase\n\t{\n\t\texplicit ExplicitProducer(ConcurrentQueue* parent_) :\n\t\t\tProducerBase(parent_, true),\n\t\t\tblockIndex(nullptr),\n\t\t\tpr_blockIndexSlotsUsed(0),\n\t\t\tpr_blockIndexSize(EXPLICIT_INITIAL_INDEX_SIZE >> 1),\n\t\t\tpr_blockIndexFront(0),\n\t\t\tpr_blockIndexEntries(nullptr),\n\t\t\tpr_blockIndexRaw(nullptr)\n\t\t{\n\t\t\tsize_t poolBasedIndexSize = details::ceil_to_pow_2(parent_->initialBlockPoolSize) >> 1;\n\t\t\tif (poolBasedIndexSize > pr_blockIndexSize) {\n\t\t\t\tpr_blockIndexSize = poolBasedIndexSize;\n\t\t\t}\n\t\t\t\n\t\t\tnew_block_index(0);\t\t// This creates an index with double the number of current entries, i.e. EXPLICIT_INITIAL_INDEX_SIZE\n\t\t}\n\t\t\n\t\t~ExplicitProducer()\n\t\t{\n\t\t\t// Destruct any elements not yet dequeued.\n\t\t\t// Since we're in the destructor, we can assume all elements\n\t\t\t// are either completely dequeued or completely not (no halfways).\n\t\t\tif (this->tailBlock != nullptr) {\t\t// Note this means there must be a block index too\n\t\t\t\t// First find the block that's partially dequeued, if any\n\t\t\t\tBlock* halfDequeuedBlock = nullptr;\n\t\t\t\tif ((this->headIndex.load(std::memory_order_relaxed) & static_cast<index_t>(BLOCK_SIZE - 1)) != 0) {\n\t\t\t\t\t// The head's not on a block boundary, meaning a block somewhere is partially dequeued\n\t\t\t\t\t// (or the head block is the tail block and was fully dequeued, but the head/tail are still not on a boundary)\n\t\t\t\t\tsize_t i = (pr_blockIndexFront - pr_blockIndexSlotsUsed) & (pr_blockIndexSize - 1);\n\t\t\t\t\twhile (details::circular_less_than<index_t>(pr_blockIndexEntries[i].base + BLOCK_SIZE, this->headIndex.load(std::memory_order_relaxed))) {\n\t\t\t\t\t\ti = (i + 1) & (pr_blockIndexSize - 1);\n\t\t\t\t\t}\n\t\t\t\t\tassert(details::circular_less_than<index_t>(pr_blockIndexEntries[i].base, this->headIndex.load(std::memory_order_relaxed)));\n\t\t\t\t\thalfDequeuedBlock = pr_blockIndexEntries[i].block;\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\t// Start at the head block (note the first line in the loop gives us the head from the tail on the first iteration)\n\t\t\t\tauto block = this->tailBlock;\n\t\t\t\tdo {\n\t\t\t\t\tblock = block->next;\n\t\t\t\t\tif (block->ConcurrentQueue::Block::template is_empty<explicit_context>()) {\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\tsize_t i = 0;\t// Offset into block\n\t\t\t\t\tif (block == halfDequeuedBlock) {\n\t\t\t\t\t\ti = static_cast<size_t>(this->headIndex.load(std::memory_order_relaxed) & static_cast<index_t>(BLOCK_SIZE - 1));\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\t// Walk through all the items in the block; if this is the tail block, we need to stop when we reach the tail index\n\t\t\t\t\tauto lastValidIndex = (this->tailIndex.load(std::memory_order_relaxed) & static_cast<index_t>(BLOCK_SIZE - 1)) == 0 ? BLOCK_SIZE : static_cast<size_t>(this->tailIndex.load(std::memory_order_relaxed) & static_cast<index_t>(BLOCK_SIZE - 1));\n\t\t\t\t\twhile (i != BLOCK_SIZE && (block != this->tailBlock || i != lastValidIndex)) {\n\t\t\t\t\t\t(*block)[i++]->~T();\n\t\t\t\t\t}\n\t\t\t\t} while (block != this->tailBlock);\n\t\t\t}\n\t\t\t\n\t\t\t// Destroy all blocks that we own\n\t\t\tif (this->tailBlock != nullptr) {\n\t\t\t\tauto block = this->tailBlock;\n\t\t\t\tdo {\n\t\t\t\t\tauto nextBlock = block->next;\n\t\t\t\t\tif (block->dynamicallyAllocated) {\n\t\t\t\t\t\tdestroy(block);\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tthis->parent->add_block_to_free_list(block);\n\t\t\t\t\t}\n\t\t\t\t\tblock = nextBlock;\n\t\t\t\t} while (block != this->tailBlock);\n\t\t\t}\n\t\t\t\n\t\t\t// Destroy the block indices\n\t\t\tauto header = static_cast<BlockIndexHeader*>(pr_blockIndexRaw);\n\t\t\twhile (header != nullptr) {\n\t\t\t\tauto prev = static_cast<BlockIndexHeader*>(header->prev);\n\t\t\t\theader->~BlockIndexHeader();\n\t\t\t\t(Traits::free)(header);\n\t\t\t\theader = prev;\n\t\t\t}\n\t\t}\n\t\t\n\t\ttemplate<AllocationMode allocMode, typename U>\n\t\tinline bool enqueue(U&& element)\n\t\t{\n\t\t\tindex_t currentTailIndex = this->tailIndex.load(std::memory_order_relaxed);\n\t\t\tindex_t newTailIndex = 1 + currentTailIndex;\n\t\t\tif ((currentTailIndex & static_cast<index_t>(BLOCK_SIZE - 1)) == 0) {\n\t\t\t\t// We reached the end of a block, start a new one\n\t\t\t\tauto startBlock = this->tailBlock;\n\t\t\t\tauto originalBlockIndexSlotsUsed = pr_blockIndexSlotsUsed;\n\t\t\t\tif (this->tailBlock != nullptr && this->tailBlock->next->ConcurrentQueue::Block::template is_empty<explicit_context>()) {\n\t\t\t\t\t// We can re-use the block ahead of us, it's empty!\t\t\t\t\t\n\t\t\t\t\tthis->tailBlock = this->tailBlock->next;\n\t\t\t\t\tthis->tailBlock->ConcurrentQueue::Block::template reset_empty<explicit_context>();\n\t\t\t\t\t\n\t\t\t\t\t// We'll put the block on the block index (guaranteed to be room since we're conceptually removing the\n\t\t\t\t\t// last block from it first -- except instead of removing then adding, we can just overwrite).\n\t\t\t\t\t// Note that there must be a valid block index here, since even if allocation failed in the ctor,\n\t\t\t\t\t// it would have been re-attempted when adding the first block to the queue; since there is such\n\t\t\t\t\t// a block, a block index must have been successfully allocated.\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\t// Whatever head value we see here is >= the last value we saw here (relatively),\n\t\t\t\t\t// and <= its current value. Since we have the most recent tail, the head must be\n\t\t\t\t\t// <= to it.\n\t\t\t\t\tauto head = this->headIndex.load(std::memory_order_relaxed);\n\t\t\t\t\tassert(!details::circular_less_than<index_t>(currentTailIndex, head));\n\t\t\t\t\tif (!details::circular_less_than<index_t>(head, currentTailIndex + BLOCK_SIZE)\n\t\t\t\t\t\t|| (MAX_SUBQUEUE_SIZE != details::const_numeric_max<size_t>::value && (MAX_SUBQUEUE_SIZE == 0 || MAX_SUBQUEUE_SIZE - BLOCK_SIZE < currentTailIndex - head))) {\n\t\t\t\t\t\t// We can't enqueue in another block because there's not enough leeway -- the\n\t\t\t\t\t\t// tail could surpass the head by the time the block fills up! (Or we'll exceed\n\t\t\t\t\t\t// the size limit, if the second part of the condition was true.)\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\t// We're going to need a new block; check that the block index has room\n\t\t\t\t\tif (pr_blockIndexRaw == nullptr || pr_blockIndexSlotsUsed == pr_blockIndexSize) {\n\t\t\t\t\t\t// Hmm, the circular block index is already full -- we'll need\n\t\t\t\t\t\t// to allocate a new index. Note pr_blockIndexRaw can only be nullptr if\n\t\t\t\t\t\t// the initial allocation failed in the constructor.\n\t\t\t\t\t\t\n\t\t\t\t\t\tMOODYCAMEL_CONSTEXPR_IF (allocMode == CannotAlloc) {\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse if (!new_block_index(pr_blockIndexSlotsUsed)) {\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\t// Insert a new block in the circular linked list\n\t\t\t\t\tauto newBlock = this->parent->ConcurrentQueue::template requisition_block<allocMode>();\n\t\t\t\t\tif (newBlock == nullptr) {\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n#ifdef MCDBGQ_TRACKMEM\n\t\t\t\t\tnewBlock->owner = this;\n#endif\n\t\t\t\t\tnewBlock->ConcurrentQueue::Block::template reset_empty<explicit_context>();\n\t\t\t\t\tif (this->tailBlock == nullptr) {\n\t\t\t\t\t\tnewBlock->next = newBlock;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tnewBlock->next = this->tailBlock->next;\n\t\t\t\t\t\tthis->tailBlock->next = newBlock;\n\t\t\t\t\t}\n\t\t\t\t\tthis->tailBlock = newBlock;\n\t\t\t\t\t++pr_blockIndexSlotsUsed;\n\t\t\t\t}\n\n\t\t\t\tMOODYCAMEL_CONSTEXPR_IF (!MOODYCAMEL_NOEXCEPT_CTOR(T, U, new (static_cast<T*>(nullptr)) T(std::forward<U>(element)))) {\n\t\t\t\t\t// The constructor may throw. We want the element not to appear in the queue in\n\t\t\t\t\t// that case (without corrupting the queue):\n\t\t\t\t\tMOODYCAMEL_TRY {\n\t\t\t\t\t\tnew ((*this->tailBlock)[currentTailIndex]) T(std::forward<U>(element));\n\t\t\t\t\t}\n\t\t\t\t\tMOODYCAMEL_CATCH (...) {\n\t\t\t\t\t\t// Revert change to the current block, but leave the new block available\n\t\t\t\t\t\t// for next time\n\t\t\t\t\t\tpr_blockIndexSlotsUsed = originalBlockIndexSlotsUsed;\n\t\t\t\t\t\tthis->tailBlock = startBlock == nullptr ? this->tailBlock : startBlock;\n\t\t\t\t\t\tMOODYCAMEL_RETHROW;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\t(void)startBlock;\n\t\t\t\t\t(void)originalBlockIndexSlotsUsed;\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\t// Add block to block index\n\t\t\t\tauto& entry = blockIndex.load(std::memory_order_relaxed)->entries[pr_blockIndexFront];\n\t\t\t\tentry.base = currentTailIndex;\n\t\t\t\tentry.block = this->tailBlock;\n\t\t\t\tblockIndex.load(std::memory_order_relaxed)->front.store(pr_blockIndexFront, std::memory_order_release);\n\t\t\t\tpr_blockIndexFront = (pr_blockIndexFront + 1) & (pr_blockIndexSize - 1);\n\t\t\t\t\n\t\t\t\tMOODYCAMEL_CONSTEXPR_IF (!MOODYCAMEL_NOEXCEPT_CTOR(T, U, new (static_cast<T*>(nullptr)) T(std::forward<U>(element)))) {\n\t\t\t\t\tthis->tailIndex.store(newTailIndex, std::memory_order_release);\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\t// Enqueue\n\t\t\tnew ((*this->tailBlock)[currentTailIndex]) T(std::forward<U>(element));\n\t\t\t\n\t\t\tthis->tailIndex.store(newTailIndex, std::memory_order_release);\n\t\t\treturn true;\n\t\t}\n\t\t\n\t\ttemplate<typename U>\n\t\tbool dequeue(U& element)\n\t\t{\n\t\t\tauto tail = this->tailIndex.load(std::memory_order_relaxed);\n\t\t\tauto overcommit = this->dequeueOvercommit.load(std::memory_order_relaxed);\n\t\t\tif (details::circular_less_than<index_t>(this->dequeueOptimisticCount.load(std::memory_order_relaxed) - overcommit, tail)) {\n\t\t\t\t// Might be something to dequeue, let's give it a try\n\t\t\t\t\n\t\t\t\t// Note that this if is purely for performance purposes in the common case when the queue is\n\t\t\t\t// empty and the values are eventually consistent -- we may enter here spuriously.\n\t\t\t\t\n\t\t\t\t// Note that whatever the values of overcommit and tail are, they are not going to change (unless we\n\t\t\t\t// change them) and must be the same value at this point (inside the if) as when the if condition was\n\t\t\t\t// evaluated.\n\n\t\t\t\t// We insert an acquire fence here to synchronize-with the release upon incrementing dequeueOvercommit below.\n\t\t\t\t// This ensures that whatever the value we got loaded into overcommit, the load of dequeueOptisticCount in\n\t\t\t\t// the fetch_add below will result in a value at least as recent as that (and therefore at least as large).\n\t\t\t\t// Note that I believe a compiler (signal) fence here would be sufficient due to the nature of fetch_add (all\n\t\t\t\t// read-modify-write operations are guaranteed to work on the latest value in the modification order), but\n\t\t\t\t// unfortunately that can't be shown to be correct using only the C++11 standard.\n\t\t\t\t// See http://stackoverflow.com/questions/18223161/what-are-the-c11-memory-ordering-guarantees-in-this-corner-case\n\t\t\t\tstd::atomic_thread_fence(std::memory_order_acquire);\n\t\t\t\t\n\t\t\t\t// Increment optimistic counter, then check if it went over the boundary\n\t\t\t\tauto myDequeueCount = this->dequeueOptimisticCount.fetch_add(1, std::memory_order_relaxed);\n\t\t\t\t\n\t\t\t\t// Note that since dequeueOvercommit must be <= dequeueOptimisticCount (because dequeueOvercommit is only ever\n\t\t\t\t// incremented after dequeueOptimisticCount -- this is enforced in the `else` block below), and since we now\n\t\t\t\t// have a version of dequeueOptimisticCount that is at least as recent as overcommit (due to the release upon\n\t\t\t\t// incrementing dequeueOvercommit and the acquire above that synchronizes with it), overcommit <= myDequeueCount.\n\t\t\t\t// However, we can't assert this since both dequeueOptimisticCount and dequeueOvercommit may (independently)\n\t\t\t\t// overflow; in such a case, though, the logic still holds since the difference between the two is maintained.\n\t\t\t\t\n\t\t\t\t// Note that we reload tail here in case it changed; it will be the same value as before or greater, since\n\t\t\t\t// this load is sequenced after (happens after) the earlier load above. This is supported by read-read\n\t\t\t\t// coherency (as defined in the standard), explained here: http://en.cppreference.com/w/cpp/atomic/memory_order\n\t\t\t\ttail = this->tailIndex.load(std::memory_order_acquire);\n\t\t\t\tif ((details::likely)(details::circular_less_than<index_t>(myDequeueCount - overcommit, tail))) {\n\t\t\t\t\t// Guaranteed to be at least one element to dequeue!\n\t\t\t\t\t\n\t\t\t\t\t// Get the index. Note that since there's guaranteed to be at least one element, this\n\t\t\t\t\t// will never exceed tail. We need to do an acquire-release fence here since it's possible\n\t\t\t\t\t// that whatever condition got us to this point was for an earlier enqueued element (that\n\t\t\t\t\t// we already see the memory effects for), but that by the time we increment somebody else\n\t\t\t\t\t// has incremented it, and we need to see the memory effects for *that* element, which is\n\t\t\t\t\t// in such a case is necessarily visible on the thread that incremented it in the first\n\t\t\t\t\t// place with the more current condition (they must have acquired a tail that is at least\n\t\t\t\t\t// as recent).\n\t\t\t\t\tauto index = this->headIndex.fetch_add(1, std::memory_order_acq_rel);\n\t\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t\t// Determine which block the element is in\n\t\t\t\t\t\n\t\t\t\t\tauto localBlockIndex = blockIndex.load(std::memory_order_acquire);\n\t\t\t\t\tauto localBlockIndexHead = localBlockIndex->front.load(std::memory_order_acquire);\n\t\t\t\t\t\n\t\t\t\t\t// We need to be careful here about subtracting and dividing because of index wrap-around.\n\t\t\t\t\t// When an index wraps, we need to preserve the sign of the offset when dividing it by the\n\t\t\t\t\t// block size (in order to get a correct signed block count offset in all cases):\n\t\t\t\t\tauto headBase = localBlockIndex->entries[localBlockIndexHead].base;\n\t\t\t\t\tauto blockBaseIndex = index & ~static_cast<index_t>(BLOCK_SIZE - 1);\n\t\t\t\t\tauto offset = static_cast<size_t>(static_cast<typename std::make_signed<index_t>::type>(blockBaseIndex - headBase) / BLOCK_SIZE);\n\t\t\t\t\tauto block = localBlockIndex->entries[(localBlockIndexHead + offset) & (localBlockIndex->size - 1)].block;\n\t\t\t\t\t\n\t\t\t\t\t// Dequeue\n\t\t\t\t\tauto& el = *((*block)[index]);\n\t\t\t\t\tif (!MOODYCAMEL_NOEXCEPT_ASSIGN(T, T&&, element = std::move(el))) {\n\t\t\t\t\t\t// Make sure the element is still fully dequeued and destroyed even if the assignment\n\t\t\t\t\t\t// throws\n\t\t\t\t\t\tstruct Guard {\n\t\t\t\t\t\t\tBlock* block;\n\t\t\t\t\t\t\tindex_t index;\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t~Guard()\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t(*block)[index]->~T();\n\t\t\t\t\t\t\t\tblock->ConcurrentQueue::Block::template set_empty<explicit_context>(index);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} guard = { block, index };\n\n\t\t\t\t\t\telement = std::move(el); // NOLINT\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\telement = std::move(el); // NOLINT\n\t\t\t\t\t\tel.~T(); // NOLINT\n\t\t\t\t\t\tblock->ConcurrentQueue::Block::template set_empty<explicit_context>(index);\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\t// Wasn't anything to dequeue after all; make the effective dequeue count eventually consistent\n\t\t\t\t\tthis->dequeueOvercommit.fetch_add(1, std::memory_order_release);\t\t// Release so that the fetch_add on dequeueOptimisticCount is guaranteed to happen before this write\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\t\treturn false;\n\t\t}\n\t\t\n\t\ttemplate<AllocationMode allocMode, typename It>\n\t\tbool MOODYCAMEL_NO_TSAN enqueue_bulk(It itemFirst, size_t count)\n\t\t{\n\t\t\t// First, we need to make sure we have enough room to enqueue all of the elements;\n\t\t\t// this means pre-allocating blocks and putting them in the block index (but only if\n\t\t\t// all the allocations succeeded).\n\t\t\tindex_t startTailIndex = this->tailIndex.load(std::memory_order_relaxed);\n\t\t\tauto startBlock = this->tailBlock;\n\t\t\tauto originalBlockIndexFront = pr_blockIndexFront;\n\t\t\tauto originalBlockIndexSlotsUsed = pr_blockIndexSlotsUsed;\n\t\t\t\n\t\t\tBlock* firstAllocatedBlock = nullptr;\n\t\t\t\n\t\t\t// Figure out how many blocks we'll need to allocate, and do so\n\t\t\tsize_t blockBaseDiff = ((startTailIndex + count - 1) & ~static_cast<index_t>(BLOCK_SIZE - 1)) - ((startTailIndex - 1) & ~static_cast<index_t>(BLOCK_SIZE - 1));\n\t\t\tindex_t currentTailIndex = (startTailIndex - 1) & ~static_cast<index_t>(BLOCK_SIZE - 1);\n\t\t\tif (blockBaseDiff > 0) {\n\t\t\t\t// Allocate as many blocks as possible from ahead\n\t\t\t\twhile (blockBaseDiff > 0 && this->tailBlock != nullptr && this->tailBlock->next != firstAllocatedBlock && this->tailBlock->next->ConcurrentQueue::Block::template is_empty<explicit_context>()) {\n\t\t\t\t\tblockBaseDiff -= static_cast<index_t>(BLOCK_SIZE);\n\t\t\t\t\tcurrentTailIndex += static_cast<index_t>(BLOCK_SIZE);\n\t\t\t\t\t\n\t\t\t\t\tthis->tailBlock = this->tailBlock->next;\n\t\t\t\t\tfirstAllocatedBlock = firstAllocatedBlock == nullptr ? this->tailBlock : firstAllocatedBlock;\n\t\t\t\t\t\n\t\t\t\t\tauto& entry = blockIndex.load(std::memory_order_relaxed)->entries[pr_blockIndexFront];\n\t\t\t\t\tentry.base = currentTailIndex;\n\t\t\t\t\tentry.block = this->tailBlock;\n\t\t\t\t\tpr_blockIndexFront = (pr_blockIndexFront + 1) & (pr_blockIndexSize - 1);\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\t// Now allocate as many blocks as necessary from the block pool\n\t\t\t\twhile (blockBaseDiff > 0) {\n\t\t\t\t\tblockBaseDiff -= static_cast<index_t>(BLOCK_SIZE);\n\t\t\t\t\tcurrentTailIndex += static_cast<index_t>(BLOCK_SIZE);\n\t\t\t\t\t\n\t\t\t\t\tauto head = this->headIndex.load(std::memory_order_relaxed);\n\t\t\t\t\tassert(!details::circular_less_than<index_t>(currentTailIndex, head));\n\t\t\t\t\tbool full = !details::circular_less_than<index_t>(head, currentTailIndex + BLOCK_SIZE) || (MAX_SUBQUEUE_SIZE != details::const_numeric_max<size_t>::value && (MAX_SUBQUEUE_SIZE == 0 || MAX_SUBQUEUE_SIZE - BLOCK_SIZE < currentTailIndex - head));\n\t\t\t\t\tif (pr_blockIndexRaw == nullptr || pr_blockIndexSlotsUsed == pr_blockIndexSize || full) {\n\t\t\t\t\t\tMOODYCAMEL_CONSTEXPR_IF (allocMode == CannotAlloc) {\n\t\t\t\t\t\t\t// Failed to allocate, undo changes (but keep injected blocks)\n\t\t\t\t\t\t\tpr_blockIndexFront = originalBlockIndexFront;\n\t\t\t\t\t\t\tpr_blockIndexSlotsUsed = originalBlockIndexSlotsUsed;\n\t\t\t\t\t\t\tthis->tailBlock = startBlock == nullptr ? firstAllocatedBlock : startBlock;\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse if (full || !new_block_index(originalBlockIndexSlotsUsed)) {\n\t\t\t\t\t\t\t// Failed to allocate, undo changes (but keep injected blocks)\n\t\t\t\t\t\t\tpr_blockIndexFront = originalBlockIndexFront;\n\t\t\t\t\t\t\tpr_blockIndexSlotsUsed = originalBlockIndexSlotsUsed;\n\t\t\t\t\t\t\tthis->tailBlock = startBlock == nullptr ? firstAllocatedBlock : startBlock;\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t\t}\n\t\t\t\t\t\t\n\t\t\t\t\t\t// pr_blockIndexFront is updated inside new_block_index, so we need to\n\t\t\t\t\t\t// update our fallback value too (since we keep the new index even if we\n\t\t\t\t\t\t// later fail)\n\t\t\t\t\t\toriginalBlockIndexFront = originalBlockIndexSlotsUsed;\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\t// Insert a new block in the circular linked list\n\t\t\t\t\tauto newBlock = this->parent->ConcurrentQueue::template requisition_block<allocMode>();\n\t\t\t\t\tif (newBlock == nullptr) {\n\t\t\t\t\t\tpr_blockIndexFront = originalBlockIndexFront;\n\t\t\t\t\t\tpr_blockIndexSlotsUsed = originalBlockIndexSlotsUsed;\n\t\t\t\t\t\tthis->tailBlock = startBlock == nullptr ? firstAllocatedBlock : startBlock;\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\t\n#ifdef MCDBGQ_TRACKMEM\n\t\t\t\t\tnewBlock->owner = this;\n#endif\n\t\t\t\t\tnewBlock->ConcurrentQueue::Block::template set_all_empty<explicit_context>();\n\t\t\t\t\tif (this->tailBlock == nullptr) {\n\t\t\t\t\t\tnewBlock->next = newBlock;\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tnewBlock->next = this->tailBlock->next;\n\t\t\t\t\t\tthis->tailBlock->next = newBlock;\n\t\t\t\t\t}\n\t\t\t\t\tthis->tailBlock = newBlock;\n\t\t\t\t\tfirstAllocatedBlock = firstAllocatedBlock == nullptr ? this->tailBlock : firstAllocatedBlock;\n\t\t\t\t\t\n\t\t\t\t\t++pr_blockIndexSlotsUsed;\n\t\t\t\t\t\n\t\t\t\t\tauto& entry = blockIndex.load(std::memory_order_relaxed)->entries[pr_blockIndexFront];\n\t\t\t\t\tentry.base = currentTailIndex;\n\t\t\t\t\tentry.block = this->tailBlock;\n\t\t\t\t\tpr_blockIndexFront = (pr_blockIndexFront + 1) & (pr_blockIndexSize - 1);\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\t// Excellent, all allocations succeeded. Reset each block's emptiness before we fill them up, and\n\t\t\t\t// publish the new block index front\n\t\t\t\tauto block = firstAllocatedBlock;\n\t\t\t\twhile (true) {\n\t\t\t\t\tblock->ConcurrentQueue::Block::template reset_empty<explicit_context>();\n\t\t\t\t\tif (block == this->tailBlock) {\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tblock = block->next;\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tMOODYCAMEL_CONSTEXPR_IF (MOODYCAMEL_NOEXCEPT_CTOR(T, decltype(*itemFirst), new (static_cast<T*>(nullptr)) T(details::deref_noexcept(itemFirst)))) {\n\t\t\t\t\tblockIndex.load(std::memory_order_relaxed)->front.store((pr_blockIndexFront - 1) & (pr_blockIndexSize - 1), std::memory_order_release);\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\t// Enqueue, one block at a time\n\t\t\tindex_t newTailIndex = startTailIndex + static_cast<index_t>(count);\n\t\t\tcurrentTailIndex = startTailIndex;\n\t\t\tauto endBlock = this->tailBlock;\n\t\t\tthis->tailBlock = startBlock;\n\t\t\tassert((startTailIndex & static_cast<index_t>(BLOCK_SIZE - 1)) != 0 || firstAllocatedBlock != nullptr || count == 0);\n\t\t\tif ((startTailIndex & static_cast<index_t>(BLOCK_SIZE - 1)) == 0 && firstAllocatedBlock != nullptr) {\n\t\t\t\tthis->tailBlock = firstAllocatedBlock;\n\t\t\t}\n\t\t\twhile (true) {\n\t\t\t\tindex_t stopIndex = (currentTailIndex & ~static_cast<index_t>(BLOCK_SIZE - 1)) + static_cast<index_t>(BLOCK_SIZE);\n\t\t\t\tif (details::circular_less_than<index_t>(newTailIndex, stopIndex)) {\n\t\t\t\t\tstopIndex = newTailIndex;\n\t\t\t\t}\n\t\t\t\tMOODYCAMEL_CONSTEXPR_IF (MOODYCAMEL_NOEXCEPT_CTOR(T, decltype(*itemFirst), new (static_cast<T*>(nullptr)) T(details::deref_noexcept(itemFirst)))) {\n\t\t\t\t\twhile (currentTailIndex != stopIndex) {\n\t\t\t\t\t\tnew ((*this->tailBlock)[currentTailIndex++]) T(*itemFirst++);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tMOODYCAMEL_TRY {\n\t\t\t\t\t\twhile (currentTailIndex != stopIndex) {\n\t\t\t\t\t\t\t// Must use copy constructor even if move constructor is available\n\t\t\t\t\t\t\t// because we may have to revert if there's an exception.\n\t\t\t\t\t\t\t// Sorry about the horrible templated next line, but it was the only way\n\t\t\t\t\t\t\t// to disable moving *at compile time*, which is important because a type\n\t\t\t\t\t\t\t// may only define a (noexcept) move constructor, and so calls to the\n\t\t\t\t\t\t\t// cctor will not compile, even if they are in an if branch that will never\n\t\t\t\t\t\t\t// be executed\n\t\t\t\t\t\t\tnew ((*this->tailBlock)[currentTailIndex]) T(details::nomove_if<!MOODYCAMEL_NOEXCEPT_CTOR(T, decltype(*itemFirst), new (static_cast<T*>(nullptr)) T(details::deref_noexcept(itemFirst)))>::eval(*itemFirst));\n\t\t\t\t\t\t\t++currentTailIndex;\n\t\t\t\t\t\t\t++itemFirst;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tMOODYCAMEL_CATCH (...) {\n\t\t\t\t\t\t// Oh dear, an exception's been thrown -- destroy the elements that\n\t\t\t\t\t\t// were enqueued so far and revert the entire bulk operation (we'll keep\n\t\t\t\t\t\t// any allocated blocks in our linked list for later, though).\n\t\t\t\t\t\tauto constructedStopIndex = currentTailIndex;\n\t\t\t\t\t\tauto lastBlockEnqueued = this->tailBlock;\n\t\t\t\t\t\t\n\t\t\t\t\t\tpr_blockIndexFront = originalBlockIndexFront;\n\t\t\t\t\t\tpr_blockIndexSlotsUsed = originalBlockIndexSlotsUsed;\n\t\t\t\t\t\tthis->tailBlock = startBlock == nullptr ? firstAllocatedBlock : startBlock;\n\t\t\t\t\t\t\n\t\t\t\t\t\tif (!details::is_trivially_destructible<T>::value) {\n\t\t\t\t\t\t\tauto block = startBlock;\n\t\t\t\t\t\t\tif ((startTailIndex & static_cast<index_t>(BLOCK_SIZE - 1)) == 0) {\n\t\t\t\t\t\t\t\tblock = firstAllocatedBlock;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tcurrentTailIndex = startTailIndex;\n\t\t\t\t\t\t\twhile (true) {\n\t\t\t\t\t\t\t\tstopIndex = (currentTailIndex & ~static_cast<index_t>(BLOCK_SIZE - 1)) + static_cast<index_t>(BLOCK_SIZE);\n\t\t\t\t\t\t\t\tif (details::circular_less_than<index_t>(constructedStopIndex, stopIndex)) {\n\t\t\t\t\t\t\t\t\tstopIndex = constructedStopIndex;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\twhile (currentTailIndex != stopIndex) {\n\t\t\t\t\t\t\t\t\t(*block)[currentTailIndex++]->~T();\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif (block == lastBlockEnqueued) {\n\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tblock = block->next;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tMOODYCAMEL_RETHROW;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tif (this->tailBlock == endBlock) {\n\t\t\t\t\tassert(currentTailIndex == newTailIndex);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tthis->tailBlock = this->tailBlock->next;\n\t\t\t}\n\t\t\t\n\t\t\tMOODYCAMEL_CONSTEXPR_IF (!MOODYCAMEL_NOEXCEPT_CTOR(T, decltype(*itemFirst), new (static_cast<T*>(nullptr)) T(details::deref_noexcept(itemFirst)))) {\n\t\t\t\tif (firstAllocatedBlock != nullptr)\n\t\t\t\t\tblockIndex.load(std::memory_order_relaxed)->front.store((pr_blockIndexFront - 1) & (pr_blockIndexSize - 1), std::memory_order_release);\n\t\t\t}\n\t\t\t\n\t\t\tthis->tailIndex.store(newTailIndex, std::memory_order_release);\n\t\t\treturn true;\n\t\t}\n\t\t\n\t\ttemplate<typename It>\n\t\tsize_t dequeue_bulk(It& itemFirst, size_t max)\n\t\t{\n\t\t\tauto tail = this->tailIndex.load(std::memory_order_relaxed);\n\t\t\tauto overcommit = this->dequeueOvercommit.load(std::memory_order_relaxed);\n\t\t\tauto desiredCount = static_cast<size_t>(tail - (this->dequeueOptimisticCount.load(std::memory_order_relaxed) - overcommit));\n\t\t\tif (details::circular_less_than<size_t>(0, desiredCount)) {\n\t\t\t\tdesiredCount = desiredCount < max ? desiredCount : max;\n\t\t\t\tstd::atomic_thread_fence(std::memory_order_acquire);\n\t\t\t\t\n\t\t\t\tauto myDequeueCount = this->dequeueOptimisticCount.fetch_add(desiredCount, std::memory_order_relaxed);\n\t\t\t\t\n\t\t\t\ttail = this->tailIndex.load(std::memory_order_acquire);\n\t\t\t\tauto actualCount = static_cast<size_t>(tail - (myDequeueCount - overcommit));\n\t\t\t\tif (details::circular_less_than<size_t>(0, actualCount)) {\n\t\t\t\t\tactualCount = desiredCount < actualCount ? desiredCount : actualCount;\n\t\t\t\t\tif (actualCount < desiredCount) {\n\t\t\t\t\t\tthis->dequeueOvercommit.fetch_add(desiredCount - actualCount, std::memory_order_release);\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\t// Get the first index. Note that since there's guaranteed to be at least actualCount elements, this\n\t\t\t\t\t// will never exceed tail.\n\t\t\t\t\tauto firstIndex = this->headIndex.fetch_add(actualCount, std::memory_order_acq_rel);\n\t\t\t\t\t\n\t\t\t\t\t// Determine which block the first element is in\n\t\t\t\t\tauto localBlockIndex = blockIndex.load(std::memory_order_acquire);\n\t\t\t\t\tauto localBlockIndexHead = localBlockIndex->front.load(std::memory_order_acquire);\n\t\t\t\t\t\n\t\t\t\t\tauto headBase = localBlockIndex->entries[localBlockIndexHead].base;\n\t\t\t\t\tauto firstBlockBaseIndex = firstIndex & ~static_cast<index_t>(BLOCK_SIZE - 1);\n\t\t\t\t\tauto offset = static_cast<size_t>(static_cast<typename std::make_signed<index_t>::type>(firstBlockBaseIndex - headBase) / BLOCK_SIZE);\n\t\t\t\t\tauto indexIndex = (localBlockIndexHead + offset) & (localBlockIndex->size - 1);\n\t\t\t\t\t\n\t\t\t\t\t// Iterate the blocks and dequeue\n\t\t\t\t\tauto index = firstIndex;\n\t\t\t\t\tdo {\n\t\t\t\t\t\tauto firstIndexInBlock = index;\n\t\t\t\t\t\tindex_t endIndex = (index & ~static_cast<index_t>(BLOCK_SIZE - 1)) + static_cast<index_t>(BLOCK_SIZE);\n\t\t\t\t\t\tendIndex = details::circular_less_than<index_t>(firstIndex + static_cast<index_t>(actualCount), endIndex) ? firstIndex + static_cast<index_t>(actualCount) : endIndex;\n\t\t\t\t\t\tauto block = localBlockIndex->entries[indexIndex].block;\n\t\t\t\t\t\tif (MOODYCAMEL_NOEXCEPT_ASSIGN(T, T&&, details::deref_noexcept(itemFirst) = std::move((*(*block)[index])))) {\n\t\t\t\t\t\t\twhile (index != endIndex) {\n\t\t\t\t\t\t\t\tauto& el = *((*block)[index]);\n\t\t\t\t\t\t\t\t*itemFirst++ = std::move(el);\n\t\t\t\t\t\t\t\tel.~T();\n\t\t\t\t\t\t\t\t++index;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\tMOODYCAMEL_TRY {\n\t\t\t\t\t\t\t\twhile (index != endIndex) {\n\t\t\t\t\t\t\t\t\tauto& el = *((*block)[index]);\n\t\t\t\t\t\t\t\t\t*itemFirst = std::move(el);\n\t\t\t\t\t\t\t\t\t++itemFirst;\n\t\t\t\t\t\t\t\t\tel.~T();\n\t\t\t\t\t\t\t\t\t++index;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tMOODYCAMEL_CATCH (...) {\n\t\t\t\t\t\t\t\t// It's too late to revert the dequeue, but we can make sure that all\n\t\t\t\t\t\t\t\t// the dequeued objects are properly destroyed and the block index\n\t\t\t\t\t\t\t\t// (and empty count) are properly updated before we propagate the exception\n\t\t\t\t\t\t\t\tdo {\n\t\t\t\t\t\t\t\t\tblock = localBlockIndex->entries[indexIndex].block;\n\t\t\t\t\t\t\t\t\twhile (index != endIndex) {\n\t\t\t\t\t\t\t\t\t\t(*block)[index++]->~T();\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\tblock->ConcurrentQueue::Block::template set_many_empty<explicit_context>(firstIndexInBlock, static_cast<size_t>(endIndex - firstIndexInBlock));\n\t\t\t\t\t\t\t\t\tindexIndex = (indexIndex + 1) & (localBlockIndex->size - 1);\n\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\tfirstIndexInBlock = index;\n\t\t\t\t\t\t\t\t\tendIndex = (index & ~static_cast<index_t>(BLOCK_SIZE - 1)) + static_cast<index_t>(BLOCK_SIZE);\n\t\t\t\t\t\t\t\t\tendIndex = details::circular_less_than<index_t>(firstIndex + static_cast<index_t>(actualCount), endIndex) ? firstIndex + static_cast<index_t>(actualCount) : endIndex;\n\t\t\t\t\t\t\t\t} while (index != firstIndex + actualCount);\n\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\tMOODYCAMEL_RETHROW;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tblock->ConcurrentQueue::Block::template set_many_empty<explicit_context>(firstIndexInBlock, static_cast<size_t>(endIndex - firstIndexInBlock));\n\t\t\t\t\t\tindexIndex = (indexIndex + 1) & (localBlockIndex->size - 1);\n\t\t\t\t\t} while (index != firstIndex + actualCount);\n\t\t\t\t\t\n\t\t\t\t\treturn actualCount;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\t// Wasn't anything to dequeue after all; make the effective dequeue count eventually consistent\n\t\t\t\t\tthis->dequeueOvercommit.fetch_add(desiredCount, std::memory_order_release);\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\treturn 0;\n\t\t}\n\t\t\n\tprivate:\n\t\tstruct BlockIndexEntry\n\t\t{\n\t\t\tindex_t base;\n\t\t\tBlock* block;\n\t\t};\n\t\t\n\t\tstruct BlockIndexHeader\n\t\t{\n\t\t\tsize_t size;\n\t\t\tstd::atomic<size_t> front;\t\t// Current slot (not next, like pr_blockIndexFront)\n\t\t\tBlockIndexEntry* entries;\n\t\t\tvoid* prev;\n\t\t};\n\t\t\n\t\t\n\t\tbool new_block_index(size_t numberOfFilledSlotsToExpose)\n\t\t{\n\t\t\tauto prevBlockSizeMask = pr_blockIndexSize - 1;\n\t\t\t\n\t\t\t// Create the new block\n\t\t\tpr_blockIndexSize <<= 1;\n\t\t\tauto newRawPtr = static_cast<char*>((Traits::malloc)(sizeof(BlockIndexHeader) + std::alignment_of<BlockIndexEntry>::value - 1 + sizeof(BlockIndexEntry) * pr_blockIndexSize));\n\t\t\tif (newRawPtr == nullptr) {\n\t\t\t\tpr_blockIndexSize >>= 1;\t\t// Reset to allow graceful retry\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\t\n\t\t\tauto newBlockIndexEntries = reinterpret_cast<BlockIndexEntry*>(details::align_for<BlockIndexEntry>(newRawPtr + sizeof(BlockIndexHeader)));\n\t\t\t\n\t\t\t// Copy in all the old indices, if any\n\t\t\tsize_t j = 0;\n\t\t\tif (pr_blockIndexSlotsUsed != 0) {\n\t\t\t\tauto i = (pr_blockIndexFront - pr_blockIndexSlotsUsed) & prevBlockSizeMask;\n\t\t\t\tdo {\n\t\t\t\t\tnewBlockIndexEntries[j++] = pr_blockIndexEntries[i];\n\t\t\t\t\ti = (i + 1) & prevBlockSizeMask;\n\t\t\t\t} while (i != pr_blockIndexFront);\n\t\t\t}\n\t\t\t\n\t\t\t// Update everything\n\t\t\tauto header = new (newRawPtr) BlockIndexHeader;\n\t\t\theader->size = pr_blockIndexSize;\n\t\t\theader->front.store(numberOfFilledSlotsToExpose - 1, std::memory_order_relaxed);\n\t\t\theader->entries = newBlockIndexEntries;\n\t\t\theader->prev = pr_blockIndexRaw;\t\t// we link the new block to the old one so we can free it later\n\t\t\t\n\t\t\tpr_blockIndexFront = j;\n\t\t\tpr_blockIndexEntries = newBlockIndexEntries;\n\t\t\tpr_blockIndexRaw = newRawPtr;\n\t\t\tblockIndex.store(header, std::memory_order_release);\n\t\t\t\n\t\t\treturn true;\n\t\t}\n\t\t\n\tprivate:\n\t\tstd::atomic<BlockIndexHeader*> blockIndex;\n\t\t\n\t\t// To be used by producer only -- consumer must use the ones in referenced by blockIndex\n\t\tsize_t pr_blockIndexSlotsUsed;\n\t\tsize_t pr_blockIndexSize;\n\t\tsize_t pr_blockIndexFront;\t\t// Next slot (not current)\n\t\tBlockIndexEntry* pr_blockIndexEntries;\n\t\tvoid* pr_blockIndexRaw;\n\t\t\n#ifdef MOODYCAMEL_QUEUE_INTERNAL_DEBUG\n\tpublic:\n\t\tExplicitProducer* nextExplicitProducer;\n\tprivate:\n#endif\n\t\t\n#ifdef MCDBGQ_TRACKMEM\n\t\tfriend struct MemStats;\n#endif\n\t};\n\t\n\t\n\t//////////////////////////////////\n\t// Implicit queue\n\t//////////////////////////////////\n\t\n\tstruct ImplicitProducer : public ProducerBase\n\t{\t\t\t\n\t\tImplicitProducer(ConcurrentQueue* parent_) :\n\t\t\tProducerBase(parent_, false),\n\t\t\tnextBlockIndexCapacity(IMPLICIT_INITIAL_INDEX_SIZE),\n\t\t\tblockIndex(nullptr)\n\t\t{\n\t\t\tnew_block_index();\n\t\t}\n\t\t\n\t\t~ImplicitProducer()\n\t\t{\n\t\t\t// Note that since we're in the destructor we can assume that all enqueue/dequeue operations\n\t\t\t// completed already; this means that all undequeued elements are placed contiguously across\n\t\t\t// contiguous blocks, and that only the first and last remaining blocks can be only partially\n\t\t\t// empty (all other remaining blocks must be completely full).\n\t\t\t\n#ifdef MOODYCAMEL_CPP11_THREAD_LOCAL_SUPPORTED\n\t\t\t// Unregister ourselves for thread termination notification\n\t\t\tif (!this->inactive.load(std::memory_order_relaxed)) {\n\t\t\t\tdetails::ThreadExitNotifier::unsubscribe(&threadExitListener);\n\t\t\t}\n#endif\n\t\t\t\n\t\t\t// Destroy all remaining elements!\n\t\t\tauto tail = this->tailIndex.load(std::memory_order_relaxed);\n\t\t\tauto index = this->headIndex.load(std::memory_order_relaxed);\n\t\t\tBlock* block = nullptr;\n\t\t\tassert(index == tail || details::circular_less_than(index, tail));\n\t\t\tbool forceFreeLastBlock = index != tail;\t\t// If we enter the loop, then the last (tail) block will not be freed\n\t\t\twhile (index != tail) {\n\t\t\t\tif ((index & static_cast<index_t>(BLOCK_SIZE - 1)) == 0 || block == nullptr) {\n\t\t\t\t\tif (block != nullptr) {\n\t\t\t\t\t\t// Free the old block\n\t\t\t\t\t\tthis->parent->add_block_to_free_list(block);\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\tblock = get_block_index_entry_for_index(index)->value.load(std::memory_order_relaxed);\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\t((*block)[index])->~T();\n\t\t\t\t++index;\n\t\t\t}\n\t\t\t// Even if the queue is empty, there's still one block that's not on the free list\n\t\t\t// (unless the head index reached the end of it, in which case the tail will be poised\n\t\t\t// to create a new block).\n\t\t\tif (this->tailBlock != nullptr && (forceFreeLastBlock || (tail & static_cast<index_t>(BLOCK_SIZE - 1)) != 0)) {\n\t\t\t\tthis->parent->add_block_to_free_list(this->tailBlock);\n\t\t\t}\n\t\t\t\n\t\t\t// Destroy block index\n\t\t\tauto localBlockIndex = blockIndex.load(std::memory_order_relaxed);\n\t\t\tif (localBlockIndex != nullptr) {\n\t\t\t\tfor (size_t i = 0; i != localBlockIndex->capacity; ++i) {\n\t\t\t\t\tlocalBlockIndex->index[i]->~BlockIndexEntry();\n\t\t\t\t}\n\t\t\t\tdo {\n\t\t\t\t\tauto prev = localBlockIndex->prev;\n\t\t\t\t\tlocalBlockIndex->~BlockIndexHeader();\n\t\t\t\t\t(Traits::free)(localBlockIndex);\n\t\t\t\t\tlocalBlockIndex = prev;\n\t\t\t\t} while (localBlockIndex != nullptr);\n\t\t\t}\n\t\t}\n\t\t\n\t\ttemplate<AllocationMode allocMode, typename U>\n\t\tinline bool enqueue(U&& element)\n\t\t{\n\t\t\tindex_t currentTailIndex = this->tailIndex.load(std::memory_order_relaxed);\n\t\t\tindex_t newTailIndex = 1 + currentTailIndex;\n\t\t\tif ((currentTailIndex & static_cast<index_t>(BLOCK_SIZE - 1)) == 0) {\n\t\t\t\t// We reached the end of a block, start a new one\n\t\t\t\tauto head = this->headIndex.load(std::memory_order_relaxed);\n\t\t\t\tassert(!details::circular_less_than<index_t>(currentTailIndex, head));\n\t\t\t\tif (!details::circular_less_than<index_t>(head, currentTailIndex + BLOCK_SIZE) || (MAX_SUBQUEUE_SIZE != details::const_numeric_max<size_t>::value && (MAX_SUBQUEUE_SIZE == 0 || MAX_SUBQUEUE_SIZE - BLOCK_SIZE < currentTailIndex - head))) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n#ifdef MCDBGQ_NOLOCKFREE_IMPLICITPRODBLOCKINDEX\n\t\t\t\tdebug::DebugLock lock(mutex);\n#endif\n\t\t\t\t// Find out where we'll be inserting this block in the block index\n\t\t\t\tBlockIndexEntry* idxEntry;\n\t\t\t\tif (!insert_block_index_entry<allocMode>(idxEntry, currentTailIndex)) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\t// Get ahold of a new block\n\t\t\t\tauto newBlock = this->parent->ConcurrentQueue::template requisition_block<allocMode>();\n\t\t\t\tif (newBlock == nullptr) {\n\t\t\t\t\trewind_block_index_tail();\n\t\t\t\t\tidxEntry->value.store(nullptr, std::memory_order_relaxed);\n\t\t\t\t\treturn false;\n\t\t\t\t}\n#ifdef MCDBGQ_TRACKMEM\n\t\t\t\tnewBlock->owner = this;\n#endif\n\t\t\t\tnewBlock->ConcurrentQueue::Block::template reset_empty<implicit_context>();\n\n\t\t\t\tMOODYCAMEL_CONSTEXPR_IF (!MOODYCAMEL_NOEXCEPT_CTOR(T, U, new (static_cast<T*>(nullptr)) T(std::forward<U>(element)))) {\n\t\t\t\t\t// May throw, try to insert now before we publish the fact that we have this new block\n\t\t\t\t\tMOODYCAMEL_TRY {\n\t\t\t\t\t\tnew ((*newBlock)[currentTailIndex]) T(std::forward<U>(element));\n\t\t\t\t\t}\n\t\t\t\t\tMOODYCAMEL_CATCH (...) {\n\t\t\t\t\t\trewind_block_index_tail();\n\t\t\t\t\t\tidxEntry->value.store(nullptr, std::memory_order_relaxed);\n\t\t\t\t\t\tthis->parent->add_block_to_free_list(newBlock);\n\t\t\t\t\t\tMOODYCAMEL_RETHROW;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\t// Insert the new block into the index\n\t\t\t\tidxEntry->value.store(newBlock, std::memory_order_relaxed);\n\t\t\t\t\n\t\t\t\tthis->tailBlock = newBlock;\n\t\t\t\t\n\t\t\t\tMOODYCAMEL_CONSTEXPR_IF (!MOODYCAMEL_NOEXCEPT_CTOR(T, U, new (static_cast<T*>(nullptr)) T(std::forward<U>(element)))) {\n\t\t\t\t\tthis->tailIndex.store(newTailIndex, std::memory_order_release);\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\t// Enqueue\n\t\t\tnew ((*this->tailBlock)[currentTailIndex]) T(std::forward<U>(element));\n\t\t\t\n\t\t\tthis->tailIndex.store(newTailIndex, std::memory_order_release);\n\t\t\treturn true;\n\t\t}\n\t\t\n\t\ttemplate<typename U>\n\t\tbool dequeue(U& element)\n\t\t{\n\t\t\t// See ExplicitProducer::dequeue for rationale and explanation\n\t\t\tindex_t tail = this->tailIndex.load(std::memory_order_relaxed);\n\t\t\tindex_t overcommit = this->dequeueOvercommit.load(std::memory_order_relaxed);\n\t\t\tif (details::circular_less_than<index_t>(this->dequeueOptimisticCount.load(std::memory_order_relaxed) - overcommit, tail)) {\n\t\t\t\tstd::atomic_thread_fence(std::memory_order_acquire);\n\t\t\t\t\n\t\t\t\tindex_t myDequeueCount = this->dequeueOptimisticCount.fetch_add(1, std::memory_order_relaxed);\n\t\t\t\ttail = this->tailIndex.load(std::memory_order_acquire);\n\t\t\t\tif ((details::likely)(details::circular_less_than<index_t>(myDequeueCount - overcommit, tail))) {\n\t\t\t\t\tindex_t index = this->headIndex.fetch_add(1, std::memory_order_acq_rel);\n\t\t\t\t\t\n\t\t\t\t\t// Determine which block the element is in\n\t\t\t\t\tauto entry = get_block_index_entry_for_index(index);\n\t\t\t\t\t\n\t\t\t\t\t// Dequeue\n\t\t\t\t\tauto block = entry->value.load(std::memory_order_relaxed);\n\t\t\t\t\tauto& el = *((*block)[index]);\n\t\t\t\t\t\n\t\t\t\t\tif (!MOODYCAMEL_NOEXCEPT_ASSIGN(T, T&&, element = std::move(el))) {\n#ifdef MCDBGQ_NOLOCKFREE_IMPLICITPRODBLOCKINDEX\n\t\t\t\t\t\t// Note: Acquiring the mutex with every dequeue instead of only when a block\n\t\t\t\t\t\t// is released is very sub-optimal, but it is, after all, purely debug code.\n\t\t\t\t\t\tdebug::DebugLock lock(producer->mutex);\n#endif\n\t\t\t\t\t\tstruct Guard {\n\t\t\t\t\t\t\tBlock* block;\n\t\t\t\t\t\t\tindex_t index;\n\t\t\t\t\t\t\tBlockIndexEntry* entry;\n\t\t\t\t\t\t\tConcurrentQueue* parent;\n\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t~Guard()\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t(*block)[index]->~T();\n\t\t\t\t\t\t\t\tif (block->ConcurrentQueue::Block::template set_empty<implicit_context>(index)) {\n\t\t\t\t\t\t\t\t\tentry->value.store(nullptr, std::memory_order_relaxed);\n\t\t\t\t\t\t\t\t\tparent->add_block_to_free_list(block);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} guard = { block, index, entry, this->parent };\n\n\t\t\t\t\t\telement = std::move(el); // NOLINT\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\telement = std::move(el); // NOLINT\n\t\t\t\t\t\tel.~T(); // NOLINT\n\n\t\t\t\t\t\tif (block->ConcurrentQueue::Block::template set_empty<implicit_context>(index)) {\n\t\t\t\t\t\t\t{\n#ifdef MCDBGQ_NOLOCKFREE_IMPLICITPRODBLOCKINDEX\n\t\t\t\t\t\t\t\tdebug::DebugLock lock(mutex);\n#endif\n\t\t\t\t\t\t\t\t// Add the block back into the global free pool (and remove from block index)\n\t\t\t\t\t\t\t\tentry->value.store(nullptr, std::memory_order_relaxed);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tthis->parent->add_block_to_free_list(block);\t\t// releases the above store\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tthis->dequeueOvercommit.fetch_add(1, std::memory_order_release);\n\t\t\t\t}\n\t\t\t}\n\t\t\n\t\t\treturn false;\n\t\t}\n\t\t\n#ifdef _MSC_VER\n#pragma warning(push)\n#pragma warning(disable: 4706)  // assignment within conditional expression\n#endif\n\t\ttemplate<AllocationMode allocMode, typename It>\n\t\tbool enqueue_bulk(It itemFirst, size_t count)\n\t\t{\n\t\t\t// First, we need to make sure we have enough room to enqueue all of the elements;\n\t\t\t// this means pre-allocating blocks and putting them in the block index (but only if\n\t\t\t// all the allocations succeeded).\n\t\t\t\n\t\t\t// Note that the tailBlock we start off with may not be owned by us any more;\n\t\t\t// this happens if it was filled up exactly to the top (setting tailIndex to\n\t\t\t// the first index of the next block which is not yet allocated), then dequeued\n\t\t\t// completely (putting it on the free list) before we enqueue again.\n\t\t\t\n\t\t\tindex_t startTailIndex = this->tailIndex.load(std::memory_order_relaxed);\n\t\t\tauto startBlock = this->tailBlock;\n\t\t\tBlock* firstAllocatedBlock = nullptr;\n\t\t\tauto endBlock = this->tailBlock;\n\t\t\t\n\t\t\t// Figure out how many blocks we'll need to allocate, and do so\n\t\t\tsize_t blockBaseDiff = ((startTailIndex + count - 1) & ~static_cast<index_t>(BLOCK_SIZE - 1)) - ((startTailIndex - 1) & ~static_cast<index_t>(BLOCK_SIZE - 1));\n\t\t\tindex_t currentTailIndex = (startTailIndex - 1) & ~static_cast<index_t>(BLOCK_SIZE - 1);\n\t\t\tif (blockBaseDiff > 0) {\n#ifdef MCDBGQ_NOLOCKFREE_IMPLICITPRODBLOCKINDEX\n\t\t\t\tdebug::DebugLock lock(mutex);\n#endif\n\t\t\t\tdo {\n\t\t\t\t\tblockBaseDiff -= static_cast<index_t>(BLOCK_SIZE);\n\t\t\t\t\tcurrentTailIndex += static_cast<index_t>(BLOCK_SIZE);\n\t\t\t\t\t\n\t\t\t\t\t// Find out where we'll be inserting this block in the block index\n\t\t\t\t\tBlockIndexEntry* idxEntry = nullptr;  // initialization here unnecessary but compiler can't always tell\n\t\t\t\t\tBlock* newBlock;\n\t\t\t\t\tbool indexInserted = false;\n\t\t\t\t\tauto head = this->headIndex.load(std::memory_order_relaxed);\n\t\t\t\t\tassert(!details::circular_less_than<index_t>(currentTailIndex, head));\n\t\t\t\t\tbool full = !details::circular_less_than<index_t>(head, currentTailIndex + BLOCK_SIZE) || (MAX_SUBQUEUE_SIZE != details::const_numeric_max<size_t>::value && (MAX_SUBQUEUE_SIZE == 0 || MAX_SUBQUEUE_SIZE - BLOCK_SIZE < currentTailIndex - head));\n\n\t\t\t\t\tif (full || !(indexInserted = insert_block_index_entry<allocMode>(idxEntry, currentTailIndex)) || (newBlock = this->parent->ConcurrentQueue::template requisition_block<allocMode>()) == nullptr) {\n\t\t\t\t\t\t// Index allocation or block allocation failed; revert any other allocations\n\t\t\t\t\t\t// and index insertions done so far for this operation\n\t\t\t\t\t\tif (indexInserted) {\n\t\t\t\t\t\t\trewind_block_index_tail();\n\t\t\t\t\t\t\tidxEntry->value.store(nullptr, std::memory_order_relaxed);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcurrentTailIndex = (startTailIndex - 1) & ~static_cast<index_t>(BLOCK_SIZE - 1);\n\t\t\t\t\t\tfor (auto block = firstAllocatedBlock; block != nullptr; block = block->next) {\n\t\t\t\t\t\t\tcurrentTailIndex += static_cast<index_t>(BLOCK_SIZE);\n\t\t\t\t\t\t\tidxEntry = get_block_index_entry_for_index(currentTailIndex);\n\t\t\t\t\t\t\tidxEntry->value.store(nullptr, std::memory_order_relaxed);\n\t\t\t\t\t\t\trewind_block_index_tail();\n\t\t\t\t\t\t}\n\t\t\t\t\t\tthis->parent->add_blocks_to_free_list(firstAllocatedBlock);\n\t\t\t\t\t\tthis->tailBlock = startBlock;\n\t\t\t\t\t\t\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\t\n#ifdef MCDBGQ_TRACKMEM\n\t\t\t\t\tnewBlock->owner = this;\n#endif\n\t\t\t\t\tnewBlock->ConcurrentQueue::Block::template reset_empty<implicit_context>();\n\t\t\t\t\tnewBlock->next = nullptr;\n\t\t\t\t\t\n\t\t\t\t\t// Insert the new block into the index\n\t\t\t\t\tidxEntry->value.store(newBlock, std::memory_order_relaxed);\n\t\t\t\t\t\n\t\t\t\t\t// Store the chain of blocks so that we can undo if later allocations fail,\n\t\t\t\t\t// and so that we can find the blocks when we do the actual enqueueing\n\t\t\t\t\tif ((startTailIndex & static_cast<index_t>(BLOCK_SIZE - 1)) != 0 || firstAllocatedBlock != nullptr) {\n\t\t\t\t\t\tassert(this->tailBlock != nullptr);\n\t\t\t\t\t\tthis->tailBlock->next = newBlock;\n\t\t\t\t\t}\n\t\t\t\t\tthis->tailBlock = newBlock;\n\t\t\t\t\tendBlock = newBlock;\n\t\t\t\t\tfirstAllocatedBlock = firstAllocatedBlock == nullptr ? newBlock : firstAllocatedBlock;\n\t\t\t\t} while (blockBaseDiff > 0);\n\t\t\t}\n\t\t\t\n\t\t\t// Enqueue, one block at a time\n\t\t\tindex_t newTailIndex = startTailIndex + static_cast<index_t>(count);\n\t\t\tcurrentTailIndex = startTailIndex;\n\t\t\tthis->tailBlock = startBlock;\n\t\t\tassert((startTailIndex & static_cast<index_t>(BLOCK_SIZE - 1)) != 0 || firstAllocatedBlock != nullptr || count == 0);\n\t\t\tif ((startTailIndex & static_cast<index_t>(BLOCK_SIZE - 1)) == 0 && firstAllocatedBlock != nullptr) {\n\t\t\t\tthis->tailBlock = firstAllocatedBlock;\n\t\t\t}\n\t\t\twhile (true) {\n\t\t\t\tindex_t stopIndex = (currentTailIndex & ~static_cast<index_t>(BLOCK_SIZE - 1)) + static_cast<index_t>(BLOCK_SIZE);\n\t\t\t\tif (details::circular_less_than<index_t>(newTailIndex, stopIndex)) {\n\t\t\t\t\tstopIndex = newTailIndex;\n\t\t\t\t}\n\t\t\t\tMOODYCAMEL_CONSTEXPR_IF (MOODYCAMEL_NOEXCEPT_CTOR(T, decltype(*itemFirst), new (static_cast<T*>(nullptr)) T(details::deref_noexcept(itemFirst)))) {\n\t\t\t\t\twhile (currentTailIndex != stopIndex) {\n\t\t\t\t\t\tnew ((*this->tailBlock)[currentTailIndex++]) T(*itemFirst++);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tMOODYCAMEL_TRY {\n\t\t\t\t\t\twhile (currentTailIndex != stopIndex) {\n\t\t\t\t\t\t\tnew ((*this->tailBlock)[currentTailIndex]) T(details::nomove_if<!MOODYCAMEL_NOEXCEPT_CTOR(T, decltype(*itemFirst), new (static_cast<T*>(nullptr)) T(details::deref_noexcept(itemFirst)))>::eval(*itemFirst));\n\t\t\t\t\t\t\t++currentTailIndex;\n\t\t\t\t\t\t\t++itemFirst;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tMOODYCAMEL_CATCH (...) {\n\t\t\t\t\t\tauto constructedStopIndex = currentTailIndex;\n\t\t\t\t\t\tauto lastBlockEnqueued = this->tailBlock;\n\t\t\t\t\t\t\n\t\t\t\t\t\tif (!details::is_trivially_destructible<T>::value) {\n\t\t\t\t\t\t\tauto block = startBlock;\n\t\t\t\t\t\t\tif ((startTailIndex & static_cast<index_t>(BLOCK_SIZE - 1)) == 0) {\n\t\t\t\t\t\t\t\tblock = firstAllocatedBlock;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tcurrentTailIndex = startTailIndex;\n\t\t\t\t\t\t\twhile (true) {\n\t\t\t\t\t\t\t\tstopIndex = (currentTailIndex & ~static_cast<index_t>(BLOCK_SIZE - 1)) + static_cast<index_t>(BLOCK_SIZE);\n\t\t\t\t\t\t\t\tif (details::circular_less_than<index_t>(constructedStopIndex, stopIndex)) {\n\t\t\t\t\t\t\t\t\tstopIndex = constructedStopIndex;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\twhile (currentTailIndex != stopIndex) {\n\t\t\t\t\t\t\t\t\t(*block)[currentTailIndex++]->~T();\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif (block == lastBlockEnqueued) {\n\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tblock = block->next;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\t\n\t\t\t\t\t\tcurrentTailIndex = (startTailIndex - 1) & ~static_cast<index_t>(BLOCK_SIZE - 1);\n\t\t\t\t\t\tfor (auto block = firstAllocatedBlock; block != nullptr; block = block->next) {\n\t\t\t\t\t\t\tcurrentTailIndex += static_cast<index_t>(BLOCK_SIZE);\n\t\t\t\t\t\t\tauto idxEntry = get_block_index_entry_for_index(currentTailIndex);\n\t\t\t\t\t\t\tidxEntry->value.store(nullptr, std::memory_order_relaxed);\n\t\t\t\t\t\t\trewind_block_index_tail();\n\t\t\t\t\t\t}\n\t\t\t\t\t\tthis->parent->add_blocks_to_free_list(firstAllocatedBlock);\n\t\t\t\t\t\tthis->tailBlock = startBlock;\n\t\t\t\t\t\tMOODYCAMEL_RETHROW;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tif (this->tailBlock == endBlock) {\n\t\t\t\t\tassert(currentTailIndex == newTailIndex);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tthis->tailBlock = this->tailBlock->next;\n\t\t\t}\n\t\t\tthis->tailIndex.store(newTailIndex, std::memory_order_release);\n\t\t\treturn true;\n\t\t}\n#ifdef _MSC_VER\n#pragma warning(pop)\n#endif\n\t\t\n\t\ttemplate<typename It>\n\t\tsize_t dequeue_bulk(It& itemFirst, size_t max)\n\t\t{\n\t\t\tauto tail = this->tailIndex.load(std::memory_order_relaxed);\n\t\t\tauto overcommit = this->dequeueOvercommit.load(std::memory_order_relaxed);\n\t\t\tauto desiredCount = static_cast<size_t>(tail - (this->dequeueOptimisticCount.load(std::memory_order_relaxed) - overcommit));\n\t\t\tif (details::circular_less_than<size_t>(0, desiredCount)) {\n\t\t\t\tdesiredCount = desiredCount < max ? desiredCount : max;\n\t\t\t\tstd::atomic_thread_fence(std::memory_order_acquire);\n\t\t\t\t\n\t\t\t\tauto myDequeueCount = this->dequeueOptimisticCount.fetch_add(desiredCount, std::memory_order_relaxed);\n\t\t\t\t\n\t\t\t\ttail = this->tailIndex.load(std::memory_order_acquire);\n\t\t\t\tauto actualCount = static_cast<size_t>(tail - (myDequeueCount - overcommit));\n\t\t\t\tif (details::circular_less_than<size_t>(0, actualCount)) {\n\t\t\t\t\tactualCount = desiredCount < actualCount ? desiredCount : actualCount;\n\t\t\t\t\tif (actualCount < desiredCount) {\n\t\t\t\t\t\tthis->dequeueOvercommit.fetch_add(desiredCount - actualCount, std::memory_order_release);\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\t// Get the first index. Note that since there's guaranteed to be at least actualCount elements, this\n\t\t\t\t\t// will never exceed tail.\n\t\t\t\t\tauto firstIndex = this->headIndex.fetch_add(actualCount, std::memory_order_acq_rel);\n\t\t\t\t\t\n\t\t\t\t\t// Iterate the blocks and dequeue\n\t\t\t\t\tauto index = firstIndex;\n\t\t\t\t\tBlockIndexHeader* localBlockIndex;\n\t\t\t\t\tauto indexIndex = get_block_index_index_for_index(index, localBlockIndex);\n\t\t\t\t\tdo {\n\t\t\t\t\t\tauto blockStartIndex = index;\n\t\t\t\t\t\tindex_t endIndex = (index & ~static_cast<index_t>(BLOCK_SIZE - 1)) + static_cast<index_t>(BLOCK_SIZE);\n\t\t\t\t\t\tendIndex = details::circular_less_than<index_t>(firstIndex + static_cast<index_t>(actualCount), endIndex) ? firstIndex + static_cast<index_t>(actualCount) : endIndex;\n\t\t\t\t\t\t\n\t\t\t\t\t\tauto entry = localBlockIndex->index[indexIndex];\n\t\t\t\t\t\tauto block = entry->value.load(std::memory_order_relaxed);\n\t\t\t\t\t\tif (MOODYCAMEL_NOEXCEPT_ASSIGN(T, T&&, details::deref_noexcept(itemFirst) = std::move((*(*block)[index])))) {\n\t\t\t\t\t\t\twhile (index != endIndex) {\n\t\t\t\t\t\t\t\tauto& el = *((*block)[index]);\n\t\t\t\t\t\t\t\t*itemFirst++ = std::move(el);\n\t\t\t\t\t\t\t\tel.~T();\n\t\t\t\t\t\t\t\t++index;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\telse {\n\t\t\t\t\t\t\tMOODYCAMEL_TRY {\n\t\t\t\t\t\t\t\twhile (index != endIndex) {\n\t\t\t\t\t\t\t\t\tauto& el = *((*block)[index]);\n\t\t\t\t\t\t\t\t\t*itemFirst = std::move(el);\n\t\t\t\t\t\t\t\t\t++itemFirst;\n\t\t\t\t\t\t\t\t\tel.~T();\n\t\t\t\t\t\t\t\t\t++index;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tMOODYCAMEL_CATCH (...) {\n\t\t\t\t\t\t\t\tdo {\n\t\t\t\t\t\t\t\t\tentry = localBlockIndex->index[indexIndex];\n\t\t\t\t\t\t\t\t\tblock = entry->value.load(std::memory_order_relaxed);\n\t\t\t\t\t\t\t\t\twhile (index != endIndex) {\n\t\t\t\t\t\t\t\t\t\t(*block)[index++]->~T();\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\tif (block->ConcurrentQueue::Block::template set_many_empty<implicit_context>(blockStartIndex, static_cast<size_t>(endIndex - blockStartIndex))) {\n#ifdef MCDBGQ_NOLOCKFREE_IMPLICITPRODBLOCKINDEX\n\t\t\t\t\t\t\t\t\t\tdebug::DebugLock lock(mutex);\n#endif\n\t\t\t\t\t\t\t\t\t\tentry->value.store(nullptr, std::memory_order_relaxed);\n\t\t\t\t\t\t\t\t\t\tthis->parent->add_block_to_free_list(block);\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\tindexIndex = (indexIndex + 1) & (localBlockIndex->capacity - 1);\n\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\tblockStartIndex = index;\n\t\t\t\t\t\t\t\t\tendIndex = (index & ~static_cast<index_t>(BLOCK_SIZE - 1)) + static_cast<index_t>(BLOCK_SIZE);\n\t\t\t\t\t\t\t\t\tendIndex = details::circular_less_than<index_t>(firstIndex + static_cast<index_t>(actualCount), endIndex) ? firstIndex + static_cast<index_t>(actualCount) : endIndex;\n\t\t\t\t\t\t\t\t} while (index != firstIndex + actualCount);\n\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\tMOODYCAMEL_RETHROW;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (block->ConcurrentQueue::Block::template set_many_empty<implicit_context>(blockStartIndex, static_cast<size_t>(endIndex - blockStartIndex))) {\n\t\t\t\t\t\t\t{\n#ifdef MCDBGQ_NOLOCKFREE_IMPLICITPRODBLOCKINDEX\n\t\t\t\t\t\t\t\tdebug::DebugLock lock(mutex);\n#endif\n\t\t\t\t\t\t\t\t// Note that the set_many_empty above did a release, meaning that anybody who acquires the block\n\t\t\t\t\t\t\t\t// we're about to free can use it safely since our writes (and reads!) will have happened-before then.\n\t\t\t\t\t\t\t\tentry->value.store(nullptr, std::memory_order_relaxed);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tthis->parent->add_block_to_free_list(block);\t\t// releases the above store\n\t\t\t\t\t\t}\n\t\t\t\t\t\tindexIndex = (indexIndex + 1) & (localBlockIndex->capacity - 1);\n\t\t\t\t\t} while (index != firstIndex + actualCount);\n\t\t\t\t\t\n\t\t\t\t\treturn actualCount;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tthis->dequeueOvercommit.fetch_add(desiredCount, std::memory_order_release);\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\treturn 0;\n\t\t}\n\t\t\n\tprivate:\n\t\t// The block size must be > 1, so any number with the low bit set is an invalid block base index\n\t\tstatic const index_t INVALID_BLOCK_BASE = 1;\n\t\t\n\t\tstruct BlockIndexEntry\n\t\t{\n\t\t\tstd::atomic<index_t> key;\n\t\t\tstd::atomic<Block*> value;\n\t\t};\n\t\t\n\t\tstruct BlockIndexHeader\n\t\t{\n\t\t\tsize_t capacity;\n\t\t\tstd::atomic<size_t> tail;\n\t\t\tBlockIndexEntry* entries;\n\t\t\tBlockIndexEntry** index;\n\t\t\tBlockIndexHeader* prev;\n\t\t};\n\t\t\n\t\ttemplate<AllocationMode allocMode>\n\t\tinline bool insert_block_index_entry(BlockIndexEntry*& idxEntry, index_t blockStartIndex)\n\t\t{\n\t\t\tauto localBlockIndex = blockIndex.load(std::memory_order_relaxed);\t\t// We're the only writer thread, relaxed is OK\n\t\t\tif (localBlockIndex == nullptr) {\n\t\t\t\treturn false;  // this can happen if new_block_index failed in the constructor\n\t\t\t}\n\t\t\tsize_t newTail = (localBlockIndex->tail.load(std::memory_order_relaxed) + 1) & (localBlockIndex->capacity - 1);\n\t\t\tidxEntry = localBlockIndex->index[newTail];\n\t\t\tif (idxEntry->key.load(std::memory_order_relaxed) == INVALID_BLOCK_BASE ||\n\t\t\t\tidxEntry->value.load(std::memory_order_relaxed) == nullptr) {\n\t\t\t\t\n\t\t\t\tidxEntry->key.store(blockStartIndex, std::memory_order_relaxed);\n\t\t\t\tlocalBlockIndex->tail.store(newTail, std::memory_order_release);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\t\n\t\t\t// No room in the old block index, try to allocate another one!\n\t\t\tMOODYCAMEL_CONSTEXPR_IF (allocMode == CannotAlloc) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\telse if (!new_block_index()) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\tlocalBlockIndex = blockIndex.load(std::memory_order_relaxed);\n\t\t\tnewTail = (localBlockIndex->tail.load(std::memory_order_relaxed) + 1) & (localBlockIndex->capacity - 1);\n\t\t\tidxEntry = localBlockIndex->index[newTail];\n\t\t\tassert(idxEntry->key.load(std::memory_order_relaxed) == INVALID_BLOCK_BASE);\n\t\t\tidxEntry->key.store(blockStartIndex, std::memory_order_relaxed);\n\t\t\tlocalBlockIndex->tail.store(newTail, std::memory_order_release);\n\t\t\treturn true;\n\t\t}\n\t\t\n\t\tinline void rewind_block_index_tail()\n\t\t{\n\t\t\tauto localBlockIndex = blockIndex.load(std::memory_order_relaxed);\n\t\t\tlocalBlockIndex->tail.store((localBlockIndex->tail.load(std::memory_order_relaxed) - 1) & (localBlockIndex->capacity - 1), std::memory_order_relaxed);\n\t\t}\n\t\t\n\t\tinline BlockIndexEntry* get_block_index_entry_for_index(index_t index) const\n\t\t{\n\t\t\tBlockIndexHeader* localBlockIndex;\n\t\t\tauto idx = get_block_index_index_for_index(index, localBlockIndex);\n\t\t\treturn localBlockIndex->index[idx];\n\t\t}\n\t\t\n\t\tinline size_t get_block_index_index_for_index(index_t index, BlockIndexHeader*& localBlockIndex) const\n\t\t{\n#ifdef MCDBGQ_NOLOCKFREE_IMPLICITPRODBLOCKINDEX\n\t\t\tdebug::DebugLock lock(mutex);\n#endif\n\t\t\tindex &= ~static_cast<index_t>(BLOCK_SIZE - 1);\n\t\t\tlocalBlockIndex = blockIndex.load(std::memory_order_acquire);\n\t\t\tauto tail = localBlockIndex->tail.load(std::memory_order_acquire);\n\t\t\tauto tailBase = localBlockIndex->index[tail]->key.load(std::memory_order_relaxed);\n\t\t\tassert(tailBase != INVALID_BLOCK_BASE);\n\t\t\t// Note: Must use division instead of shift because the index may wrap around, causing a negative\n\t\t\t// offset, whose negativity we want to preserve\n\t\t\tauto offset = static_cast<size_t>(static_cast<typename std::make_signed<index_t>::type>(index - tailBase) / BLOCK_SIZE);\n\t\t\tsize_t idx = (tail + offset) & (localBlockIndex->capacity - 1);\n\t\t\tassert(localBlockIndex->index[idx]->key.load(std::memory_order_relaxed) == index && localBlockIndex->index[idx]->value.load(std::memory_order_relaxed) != nullptr);\n\t\t\treturn idx;\n\t\t}\n\t\t\n\t\tbool new_block_index()\n\t\t{\n\t\t\tauto prev = blockIndex.load(std::memory_order_relaxed);\n\t\t\tsize_t prevCapacity = prev == nullptr ? 0 : prev->capacity;\n\t\t\tauto entryCount = prev == nullptr ? nextBlockIndexCapacity : prevCapacity;\n\t\t\tauto raw = static_cast<char*>((Traits::malloc)(\n\t\t\t\tsizeof(BlockIndexHeader) +\n\t\t\t\tstd::alignment_of<BlockIndexEntry>::value - 1 + sizeof(BlockIndexEntry) * entryCount +\n\t\t\t\tstd::alignment_of<BlockIndexEntry*>::value - 1 + sizeof(BlockIndexEntry*) * nextBlockIndexCapacity));\n\t\t\tif (raw == nullptr) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\t\n\t\t\tauto header = new (raw) BlockIndexHeader;\n\t\t\tauto entries = reinterpret_cast<BlockIndexEntry*>(details::align_for<BlockIndexEntry>(raw + sizeof(BlockIndexHeader)));\n\t\t\tauto index = reinterpret_cast<BlockIndexEntry**>(details::align_for<BlockIndexEntry*>(reinterpret_cast<char*>(entries) + sizeof(BlockIndexEntry) * entryCount));\n\t\t\tif (prev != nullptr) {\n\t\t\t\tauto prevTail = prev->tail.load(std::memory_order_relaxed);\n\t\t\t\tauto prevPos = prevTail;\n\t\t\t\tsize_t i = 0;\n\t\t\t\tdo {\n\t\t\t\t\tprevPos = (prevPos + 1) & (prev->capacity - 1);\n\t\t\t\t\tindex[i++] = prev->index[prevPos];\n\t\t\t\t} while (prevPos != prevTail);\n\t\t\t\tassert(i == prevCapacity);\n\t\t\t}\n\t\t\tfor (size_t i = 0; i != entryCount; ++i) {\n\t\t\t\tnew (entries + i) BlockIndexEntry;\n\t\t\t\tentries[i].key.store(INVALID_BLOCK_BASE, std::memory_order_relaxed);\n\t\t\t\tindex[prevCapacity + i] = entries + i;\n\t\t\t}\n\t\t\theader->prev = prev;\n\t\t\theader->entries = entries;\n\t\t\theader->index = index;\n\t\t\theader->capacity = nextBlockIndexCapacity;\n\t\t\theader->tail.store((prevCapacity - 1) & (nextBlockIndexCapacity - 1), std::memory_order_relaxed);\n\t\t\t\n\t\t\tblockIndex.store(header, std::memory_order_release);\n\t\t\t\n\t\t\tnextBlockIndexCapacity <<= 1;\n\t\t\t\n\t\t\treturn true;\n\t\t}\n\t\t\n\tprivate:\n\t\tsize_t nextBlockIndexCapacity;\n\t\tstd::atomic<BlockIndexHeader*> blockIndex;\n\n#ifdef MOODYCAMEL_CPP11_THREAD_LOCAL_SUPPORTED\n\tpublic:\n\t\tdetails::ThreadExitListener threadExitListener;\n\tprivate:\n#endif\n\t\t\n#ifdef MOODYCAMEL_QUEUE_INTERNAL_DEBUG\n\tpublic:\n\t\tImplicitProducer* nextImplicitProducer;\n\tprivate:\n#endif\n\n#ifdef MCDBGQ_NOLOCKFREE_IMPLICITPRODBLOCKINDEX\n\t\tmutable debug::DebugMutex mutex;\n#endif\n#ifdef MCDBGQ_TRACKMEM\n\t\tfriend struct MemStats;\n#endif\n\t};\n\t\n\t\n\t//////////////////////////////////\n\t// Block pool manipulation\n\t//////////////////////////////////\n\t\n\tvoid populate_initial_block_list(size_t blockCount)\n\t{\n\t\tinitialBlockPoolSize = blockCount;\n\t\tif (initialBlockPoolSize == 0) {\n\t\t\tinitialBlockPool = nullptr;\n\t\t\treturn;\n\t\t}\n\t\t\n\t\tinitialBlockPool = create_array<Block>(blockCount);\n\t\tif (initialBlockPool == nullptr) {\n\t\t\tinitialBlockPoolSize = 0;\n\t\t}\n\t\tfor (size_t i = 0; i < initialBlockPoolSize; ++i) {\n\t\t\tinitialBlockPool[i].dynamicallyAllocated = false;\n\t\t}\n\t}\n\t\n\tinline Block* try_get_block_from_initial_pool()\n\t{\n\t\tif (initialBlockPoolIndex.load(std::memory_order_relaxed) >= initialBlockPoolSize) {\n\t\t\treturn nullptr;\n\t\t}\n\t\t\n\t\tauto index = initialBlockPoolIndex.fetch_add(1, std::memory_order_relaxed);\n\t\t\n\t\treturn index < initialBlockPoolSize ? (initialBlockPool + index) : nullptr;\n\t}\n\t\n\tinline void add_block_to_free_list(Block* block)\n\t{\n#ifdef MCDBGQ_TRACKMEM\n\t\tblock->owner = nullptr;\n#endif\n\t\tfreeList.add(block);\n\t}\n\t\n\tinline void add_blocks_to_free_list(Block* block)\n\t{\n\t\twhile (block != nullptr) {\n\t\t\tauto next = block->next;\n\t\t\tadd_block_to_free_list(block);\n\t\t\tblock = next;\n\t\t}\n\t}\n\t\n\tinline Block* try_get_block_from_free_list()\n\t{\n\t\treturn freeList.try_get();\n\t}\n\t\n\t// Gets a free block from one of the memory pools, or allocates a new one (if applicable)\n\ttemplate<AllocationMode canAlloc>\n\tBlock* requisition_block()\n\t{\n\t\tauto block = try_get_block_from_initial_pool();\n\t\tif (block != nullptr) {\n\t\t\treturn block;\n\t\t}\n\t\t\n\t\tblock = try_get_block_from_free_list();\n\t\tif (block != nullptr) {\n\t\t\treturn block;\n\t\t}\n\t\t\n\t\tMOODYCAMEL_CONSTEXPR_IF (canAlloc == CanAlloc) {\n\t\t\treturn create<Block>();\n\t\t}\n\t\telse {\n\t\t\treturn nullptr;\n\t\t}\n\t}\n\t\n\n#ifdef MCDBGQ_TRACKMEM\n\tpublic:\n\t\tstruct MemStats {\n\t\t\tsize_t allocatedBlocks;\n\t\t\tsize_t usedBlocks;\n\t\t\tsize_t freeBlocks;\n\t\t\tsize_t ownedBlocksExplicit;\n\t\t\tsize_t ownedBlocksImplicit;\n\t\t\tsize_t implicitProducers;\n\t\t\tsize_t explicitProducers;\n\t\t\tsize_t elementsEnqueued;\n\t\t\tsize_t blockClassBytes;\n\t\t\tsize_t queueClassBytes;\n\t\t\tsize_t implicitBlockIndexBytes;\n\t\t\tsize_t explicitBlockIndexBytes;\n\t\t\t\n\t\t\tfriend class ConcurrentQueue;\n\t\t\t\n\t\tprivate:\n\t\t\tstatic MemStats getFor(ConcurrentQueue* q)\n\t\t\t{\n\t\t\t\tMemStats stats = { 0 };\n\t\t\t\t\n\t\t\t\tstats.elementsEnqueued = q->size_approx();\n\t\t\t\n\t\t\t\tauto block = q->freeList.head_unsafe();\n\t\t\t\twhile (block != nullptr) {\n\t\t\t\t\t++stats.allocatedBlocks;\n\t\t\t\t\t++stats.freeBlocks;\n\t\t\t\t\tblock = block->freeListNext.load(std::memory_order_relaxed);\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tfor (auto ptr = q->producerListTail.load(std::memory_order_acquire); ptr != nullptr; ptr = ptr->next_prod()) {\n\t\t\t\t\tbool implicit = dynamic_cast<ImplicitProducer*>(ptr) != nullptr;\n\t\t\t\t\tstats.implicitProducers += implicit ? 1 : 0;\n\t\t\t\t\tstats.explicitProducers += implicit ? 0 : 1;\n\t\t\t\t\t\n\t\t\t\t\tif (implicit) {\n\t\t\t\t\t\tauto prod = static_cast<ImplicitProducer*>(ptr);\n\t\t\t\t\t\tstats.queueClassBytes += sizeof(ImplicitProducer);\n\t\t\t\t\t\tauto head = prod->headIndex.load(std::memory_order_relaxed);\n\t\t\t\t\t\tauto tail = prod->tailIndex.load(std::memory_order_relaxed);\n\t\t\t\t\t\tauto hash = prod->blockIndex.load(std::memory_order_relaxed);\n\t\t\t\t\t\tif (hash != nullptr) {\n\t\t\t\t\t\t\tfor (size_t i = 0; i != hash->capacity; ++i) {\n\t\t\t\t\t\t\t\tif (hash->index[i]->key.load(std::memory_order_relaxed) != ImplicitProducer::INVALID_BLOCK_BASE && hash->index[i]->value.load(std::memory_order_relaxed) != nullptr) {\n\t\t\t\t\t\t\t\t\t++stats.allocatedBlocks;\n\t\t\t\t\t\t\t\t\t++stats.ownedBlocksImplicit;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tstats.implicitBlockIndexBytes += hash->capacity * sizeof(typename ImplicitProducer::BlockIndexEntry);\n\t\t\t\t\t\t\tfor (; hash != nullptr; hash = hash->prev) {\n\t\t\t\t\t\t\t\tstats.implicitBlockIndexBytes += sizeof(typename ImplicitProducer::BlockIndexHeader) + hash->capacity * sizeof(typename ImplicitProducer::BlockIndexEntry*);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tfor (; details::circular_less_than<index_t>(head, tail); head += BLOCK_SIZE) {\n\t\t\t\t\t\t\t//auto block = prod->get_block_index_entry_for_index(head);\n\t\t\t\t\t\t\t++stats.usedBlocks;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tauto prod = static_cast<ExplicitProducer*>(ptr);\n\t\t\t\t\t\tstats.queueClassBytes += sizeof(ExplicitProducer);\n\t\t\t\t\t\tauto tailBlock = prod->tailBlock;\n\t\t\t\t\t\tbool wasNonEmpty = false;\n\t\t\t\t\t\tif (tailBlock != nullptr) {\n\t\t\t\t\t\t\tauto block = tailBlock;\n\t\t\t\t\t\t\tdo {\n\t\t\t\t\t\t\t\t++stats.allocatedBlocks;\n\t\t\t\t\t\t\t\tif (!block->ConcurrentQueue::Block::template is_empty<explicit_context>() || wasNonEmpty) {\n\t\t\t\t\t\t\t\t\t++stats.usedBlocks;\n\t\t\t\t\t\t\t\t\twasNonEmpty = wasNonEmpty || block != tailBlock;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t++stats.ownedBlocksExplicit;\n\t\t\t\t\t\t\t\tblock = block->next;\n\t\t\t\t\t\t\t} while (block != tailBlock);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tauto index = prod->blockIndex.load(std::memory_order_relaxed);\n\t\t\t\t\t\twhile (index != nullptr) {\n\t\t\t\t\t\t\tstats.explicitBlockIndexBytes += sizeof(typename ExplicitProducer::BlockIndexHeader) + index->size * sizeof(typename ExplicitProducer::BlockIndexEntry);\n\t\t\t\t\t\t\tindex = static_cast<typename ExplicitProducer::BlockIndexHeader*>(index->prev);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tauto freeOnInitialPool = q->initialBlockPoolIndex.load(std::memory_order_relaxed) >= q->initialBlockPoolSize ? 0 : q->initialBlockPoolSize - q->initialBlockPoolIndex.load(std::memory_order_relaxed);\n\t\t\t\tstats.allocatedBlocks += freeOnInitialPool;\n\t\t\t\tstats.freeBlocks += freeOnInitialPool;\n\t\t\t\t\n\t\t\t\tstats.blockClassBytes = sizeof(Block) * stats.allocatedBlocks;\n\t\t\t\tstats.queueClassBytes += sizeof(ConcurrentQueue);\n\t\t\t\t\n\t\t\t\treturn stats;\n\t\t\t}\n\t\t};\n\t\t\n\t\t// For debugging only. Not thread-safe.\n\t\tMemStats getMemStats()\n\t\t{\n\t\t\treturn MemStats::getFor(this);\n\t\t}\n\tprivate:\n\t\tfriend struct MemStats;\n#endif\n\t\n\t\n\t//////////////////////////////////\n\t// Producer list manipulation\n\t//////////////////////////////////\t\n\t\n\tProducerBase* recycle_or_create_producer(bool isExplicit)\n\t{\n\t\tbool recycled;\n\t\treturn recycle_or_create_producer(isExplicit, recycled);\n\t}\n\t\n\tProducerBase* recycle_or_create_producer(bool isExplicit, bool& recycled)\n\t{\n#ifdef MCDBGQ_NOLOCKFREE_IMPLICITPRODHASH\n\t\tdebug::DebugLock lock(implicitProdMutex);\n#endif\n\t\t// Try to re-use one first\n\t\tfor (auto ptr = producerListTail.load(std::memory_order_acquire); ptr != nullptr; ptr = ptr->next_prod()) {\n\t\t\tif (ptr->inactive.load(std::memory_order_relaxed) && ptr->isExplicit == isExplicit) {\n\t\t\t\tbool expected = true;\n\t\t\t\tif (ptr->inactive.compare_exchange_strong(expected, /* desired */ false, std::memory_order_acquire, std::memory_order_relaxed)) {\n\t\t\t\t\t// We caught one! It's been marked as activated, the caller can have it\n\t\t\t\t\trecycled = true;\n\t\t\t\t\treturn ptr;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\n\t\trecycled = false;\n\t\treturn add_producer(isExplicit ? static_cast<ProducerBase*>(create<ExplicitProducer>(this)) : create<ImplicitProducer>(this));\n\t}\n\t\n\tProducerBase* add_producer(ProducerBase* producer)\n\t{\n\t\t// Handle failed memory allocation\n\t\tif (producer == nullptr) {\n\t\t\treturn nullptr;\n\t\t}\n\t\t\n\t\tproducerCount.fetch_add(1, std::memory_order_relaxed);\n\t\t\n\t\t// Add it to the lock-free list\n\t\tauto prevTail = producerListTail.load(std::memory_order_relaxed);\n\t\tdo {\n\t\t\tproducer->next = prevTail;\n\t\t} while (!producerListTail.compare_exchange_weak(prevTail, producer, std::memory_order_release, std::memory_order_relaxed));\n\t\t\n#ifdef MOODYCAMEL_QUEUE_INTERNAL_DEBUG\n\t\tif (producer->isExplicit) {\n\t\t\tauto prevTailExplicit = explicitProducers.load(std::memory_order_relaxed);\n\t\t\tdo {\n\t\t\t\tstatic_cast<ExplicitProducer*>(producer)->nextExplicitProducer = prevTailExplicit;\n\t\t\t} while (!explicitProducers.compare_exchange_weak(prevTailExplicit, static_cast<ExplicitProducer*>(producer), std::memory_order_release, std::memory_order_relaxed));\n\t\t}\n\t\telse {\n\t\t\tauto prevTailImplicit = implicitProducers.load(std::memory_order_relaxed);\n\t\t\tdo {\n\t\t\t\tstatic_cast<ImplicitProducer*>(producer)->nextImplicitProducer = prevTailImplicit;\n\t\t\t} while (!implicitProducers.compare_exchange_weak(prevTailImplicit, static_cast<ImplicitProducer*>(producer), std::memory_order_release, std::memory_order_relaxed));\n\t\t}\n#endif\n\t\t\n\t\treturn producer;\n\t}\n\t\n\tvoid reown_producers()\n\t{\n\t\t// After another instance is moved-into/swapped-with this one, all the\n\t\t// producers we stole still think their parents are the other queue.\n\t\t// So fix them up!\n\t\tfor (auto ptr = producerListTail.load(std::memory_order_relaxed); ptr != nullptr; ptr = ptr->next_prod()) {\n\t\t\tptr->parent = this;\n\t\t}\n\t}\n\t\n\t\n\t//////////////////////////////////\n\t// Implicit producer hash\n\t//////////////////////////////////\n\t\n\tstruct ImplicitProducerKVP\n\t{\n\t\tstd::atomic<details::thread_id_t> key;\n\t\tImplicitProducer* value;\t\t// No need for atomicity since it's only read by the thread that sets it in the first place\n\t\t\n\t\tImplicitProducerKVP() : value(nullptr) { }\n\t\t\n\t\tImplicitProducerKVP(ImplicitProducerKVP&& other) MOODYCAMEL_NOEXCEPT\n\t\t{\n\t\t\tkey.store(other.key.load(std::memory_order_relaxed), std::memory_order_relaxed);\n\t\t\tvalue = other.value;\n\t\t}\n\t\t\n\t\tinline ImplicitProducerKVP& operator=(ImplicitProducerKVP&& other) MOODYCAMEL_NOEXCEPT\n\t\t{\n\t\t\tswap(other);\n\t\t\treturn *this;\n\t\t}\n\t\t\n\t\tinline void swap(ImplicitProducerKVP& other) MOODYCAMEL_NOEXCEPT\n\t\t{\n\t\t\tif (this != &other) {\n\t\t\t\tdetails::swap_relaxed(key, other.key);\n\t\t\t\tstd::swap(value, other.value);\n\t\t\t}\n\t\t}\n\t};\n\t\n\ttemplate<typename XT, typename XTraits>\n\tfriend void moodycamel::swap(typename ConcurrentQueue<XT, XTraits>::ImplicitProducerKVP&, typename ConcurrentQueue<XT, XTraits>::ImplicitProducerKVP&) MOODYCAMEL_NOEXCEPT;\n\t\n\tstruct ImplicitProducerHash\n\t{\n\t\tsize_t capacity;\n\t\tImplicitProducerKVP* entries;\n\t\tImplicitProducerHash* prev;\n\t};\n\t\n\tinline void populate_initial_implicit_producer_hash()\n\t{\n\t\tMOODYCAMEL_CONSTEXPR_IF (INITIAL_IMPLICIT_PRODUCER_HASH_SIZE == 0) {\n\t\t\treturn;\n\t\t}\n\t\telse {\n\t\t\timplicitProducerHashCount.store(0, std::memory_order_relaxed);\n\t\t\tauto hash = &initialImplicitProducerHash;\n\t\t\thash->capacity = INITIAL_IMPLICIT_PRODUCER_HASH_SIZE;\n\t\t\thash->entries = &initialImplicitProducerHashEntries[0];\n\t\t\tfor (size_t i = 0; i != INITIAL_IMPLICIT_PRODUCER_HASH_SIZE; ++i) {\n\t\t\t\tinitialImplicitProducerHashEntries[i].key.store(details::invalid_thread_id, std::memory_order_relaxed);\n\t\t\t}\n\t\t\thash->prev = nullptr;\n\t\t\timplicitProducerHash.store(hash, std::memory_order_relaxed);\n\t\t}\n\t}\n\t\n\tvoid swap_implicit_producer_hashes(ConcurrentQueue& other)\n\t{\n\t\tMOODYCAMEL_CONSTEXPR_IF (INITIAL_IMPLICIT_PRODUCER_HASH_SIZE == 0) {\n\t\t\treturn;\n\t\t}\n\t\telse {\n\t\t\t// Swap (assumes our implicit producer hash is initialized)\n\t\t\tinitialImplicitProducerHashEntries.swap(other.initialImplicitProducerHashEntries);\n\t\t\tinitialImplicitProducerHash.entries = &initialImplicitProducerHashEntries[0];\n\t\t\tother.initialImplicitProducerHash.entries = &other.initialImplicitProducerHashEntries[0];\n\t\t\t\n\t\t\tdetails::swap_relaxed(implicitProducerHashCount, other.implicitProducerHashCount);\n\t\t\t\n\t\t\tdetails::swap_relaxed(implicitProducerHash, other.implicitProducerHash);\n\t\t\tif (implicitProducerHash.load(std::memory_order_relaxed) == &other.initialImplicitProducerHash) {\n\t\t\t\timplicitProducerHash.store(&initialImplicitProducerHash, std::memory_order_relaxed);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tImplicitProducerHash* hash;\n\t\t\t\tfor (hash = implicitProducerHash.load(std::memory_order_relaxed); hash->prev != &other.initialImplicitProducerHash; hash = hash->prev) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\thash->prev = &initialImplicitProducerHash;\n\t\t\t}\n\t\t\tif (other.implicitProducerHash.load(std::memory_order_relaxed) == &initialImplicitProducerHash) {\n\t\t\t\tother.implicitProducerHash.store(&other.initialImplicitProducerHash, std::memory_order_relaxed);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tImplicitProducerHash* hash;\n\t\t\t\tfor (hash = other.implicitProducerHash.load(std::memory_order_relaxed); hash->prev != &initialImplicitProducerHash; hash = hash->prev) {\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\thash->prev = &other.initialImplicitProducerHash;\n\t\t\t}\n\t\t}\n\t}\n\t\n\t// Only fails (returns nullptr) if memory allocation fails\n\tImplicitProducer* get_or_add_implicit_producer()\n\t{\n\t\t// Note that since the data is essentially thread-local (key is thread ID),\n\t\t// there's a reduced need for fences (memory ordering is already consistent\n\t\t// for any individual thread), except for the current table itself.\n\t\t\n\t\t// Start by looking for the thread ID in the current and all previous hash tables.\n\t\t// If it's not found, it must not be in there yet, since this same thread would\n\t\t// have added it previously to one of the tables that we traversed.\n\t\t\n\t\t// Code and algorithm adapted from http://preshing.com/20130605/the-worlds-simplest-lock-free-hash-table\n\t\t\n#ifdef MCDBGQ_NOLOCKFREE_IMPLICITPRODHASH\n\t\tdebug::DebugLock lock(implicitProdMutex);\n#endif\n\t\t\n\t\tauto id = details::thread_id();\n\t\tauto hashedId = details::hash_thread_id(id);\n\t\t\n\t\tauto mainHash = implicitProducerHash.load(std::memory_order_acquire);\n\t\tassert(mainHash != nullptr);  // silence clang-tidy and MSVC warnings (hash cannot be null)\n\t\tfor (auto hash = mainHash; hash != nullptr; hash = hash->prev) {\n\t\t\t// Look for the id in this hash\n\t\t\tauto index = hashedId;\n\t\t\twhile (true) {\t\t// Not an infinite loop because at least one slot is free in the hash table\n\t\t\t\tindex &= hash->capacity - 1;\n\t\t\t\t\n\t\t\t\tauto probedKey = hash->entries[index].key.load(std::memory_order_relaxed);\n\t\t\t\tif (probedKey == id) {\n\t\t\t\t\t// Found it! If we had to search several hashes deep, though, we should lazily add it\n\t\t\t\t\t// to the current main hash table to avoid the extended search next time.\n\t\t\t\t\t// Note there's guaranteed to be room in the current hash table since every subsequent\n\t\t\t\t\t// table implicitly reserves space for all previous tables (there's only one\n\t\t\t\t\t// implicitProducerHashCount).\n\t\t\t\t\tauto value = hash->entries[index].value;\n\t\t\t\t\tif (hash != mainHash) {\n\t\t\t\t\t\tindex = hashedId;\n\t\t\t\t\t\twhile (true) {\n\t\t\t\t\t\t\tindex &= mainHash->capacity - 1;\n\t\t\t\t\t\t\tprobedKey = mainHash->entries[index].key.load(std::memory_order_relaxed);\n\t\t\t\t\t\t\tauto empty = details::invalid_thread_id;\n#ifdef MOODYCAMEL_CPP11_THREAD_LOCAL_SUPPORTED\n\t\t\t\t\t\t\tauto reusable = details::invalid_thread_id2;\n\t\t\t\t\t\t\tif ((probedKey == empty    && mainHash->entries[index].key.compare_exchange_strong(empty,    id, std::memory_order_relaxed, std::memory_order_relaxed)) ||\n\t\t\t\t\t\t\t\t(probedKey == reusable && mainHash->entries[index].key.compare_exchange_strong(reusable, id, std::memory_order_acquire, std::memory_order_acquire))) {\n#else\n\t\t\t\t\t\t\tif ((probedKey == empty    && mainHash->entries[index].key.compare_exchange_strong(empty,    id, std::memory_order_relaxed, std::memory_order_relaxed))) {\n#endif\n\t\t\t\t\t\t\t\tmainHash->entries[index].value = value;\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t++index;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\treturn value;\n\t\t\t\t}\n\t\t\t\tif (probedKey == details::invalid_thread_id) {\n\t\t\t\t\tbreak;\t\t// Not in this hash table\n\t\t\t\t}\n\t\t\t\t++index;\n\t\t\t}\n\t\t}\n\t\t\n\t\t// Insert!\n\t\tauto newCount = 1 + implicitProducerHashCount.fetch_add(1, std::memory_order_relaxed);\n\t\twhile (true) {\n\t\t\t// NOLINTNEXTLINE(clang-analyzer-core.NullDereference)\n\t\t\tif (newCount >= (mainHash->capacity >> 1) && !implicitProducerHashResizeInProgress.test_and_set(std::memory_order_acquire)) {\n\t\t\t\t// We've acquired the resize lock, try to allocate a bigger hash table.\n\t\t\t\t// Note the acquire fence synchronizes with the release fence at the end of this block, and hence when\n\t\t\t\t// we reload implicitProducerHash it must be the most recent version (it only gets changed within this\n\t\t\t\t// locked block).\n\t\t\t\tmainHash = implicitProducerHash.load(std::memory_order_acquire);\n\t\t\t\tif (newCount >= (mainHash->capacity >> 1)) {\n\t\t\t\t\tauto newCapacity = mainHash->capacity << 1;\n\t\t\t\t\twhile (newCount >= (newCapacity >> 1)) {\n\t\t\t\t\t\tnewCapacity <<= 1;\n\t\t\t\t\t}\n\t\t\t\t\tauto raw = static_cast<char*>((Traits::malloc)(sizeof(ImplicitProducerHash) + std::alignment_of<ImplicitProducerKVP>::value - 1 + sizeof(ImplicitProducerKVP) * newCapacity));\n\t\t\t\t\tif (raw == nullptr) {\n\t\t\t\t\t\t// Allocation failed\n\t\t\t\t\t\timplicitProducerHashCount.fetch_sub(1, std::memory_order_relaxed);\n\t\t\t\t\t\timplicitProducerHashResizeInProgress.clear(std::memory_order_relaxed);\n\t\t\t\t\t\treturn nullptr;\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\tauto newHash = new (raw) ImplicitProducerHash;\n\t\t\t\t\tnewHash->capacity = static_cast<size_t>(newCapacity);\n\t\t\t\t\tnewHash->entries = reinterpret_cast<ImplicitProducerKVP*>(details::align_for<ImplicitProducerKVP>(raw + sizeof(ImplicitProducerHash)));\n\t\t\t\t\tfor (size_t i = 0; i != newCapacity; ++i) {\n\t\t\t\t\t\tnew (newHash->entries + i) ImplicitProducerKVP;\n\t\t\t\t\t\tnewHash->entries[i].key.store(details::invalid_thread_id, std::memory_order_relaxed);\n\t\t\t\t\t}\n\t\t\t\t\tnewHash->prev = mainHash;\n\t\t\t\t\timplicitProducerHash.store(newHash, std::memory_order_release);\n\t\t\t\t\timplicitProducerHashResizeInProgress.clear(std::memory_order_release);\n\t\t\t\t\tmainHash = newHash;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\timplicitProducerHashResizeInProgress.clear(std::memory_order_release);\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\t// If it's < three-quarters full, add to the old one anyway so that we don't have to wait for the next table\n\t\t\t// to finish being allocated by another thread (and if we just finished allocating above, the condition will\n\t\t\t// always be true)\n\t\t\tif (newCount < (mainHash->capacity >> 1) + (mainHash->capacity >> 2)) {\n\t\t\t\tbool recycled;\n\t\t\t\tauto producer = static_cast<ImplicitProducer*>(recycle_or_create_producer(false, recycled));\n\t\t\t\tif (producer == nullptr) {\n\t\t\t\t\timplicitProducerHashCount.fetch_sub(1, std::memory_order_relaxed);\n\t\t\t\t\treturn nullptr;\n\t\t\t\t}\n\t\t\t\tif (recycled) {\n\t\t\t\t\timplicitProducerHashCount.fetch_sub(1, std::memory_order_relaxed);\n\t\t\t\t}\n\t\t\t\t\n#ifdef MOODYCAMEL_CPP11_THREAD_LOCAL_SUPPORTED\n\t\t\t\tproducer->threadExitListener.callback = &ConcurrentQueue::implicit_producer_thread_exited_callback;\n\t\t\t\tproducer->threadExitListener.userData = producer;\n\t\t\t\tdetails::ThreadExitNotifier::subscribe(&producer->threadExitListener);\n#endif\n\t\t\t\t\n\t\t\t\tauto index = hashedId;\n\t\t\t\twhile (true) {\n\t\t\t\t\tindex &= mainHash->capacity - 1;\n\t\t\t\t\tauto probedKey = mainHash->entries[index].key.load(std::memory_order_relaxed);\n\t\t\t\t\t\n\t\t\t\t\tauto empty = details::invalid_thread_id;\n#ifdef MOODYCAMEL_CPP11_THREAD_LOCAL_SUPPORTED\n\t\t\t\t\tauto reusable = details::invalid_thread_id2;\n\t\t\t\t\tif ((probedKey == empty    && mainHash->entries[index].key.compare_exchange_strong(empty,    id, std::memory_order_relaxed, std::memory_order_relaxed)) ||\n\t\t\t\t\t\t(probedKey == reusable && mainHash->entries[index].key.compare_exchange_strong(reusable, id, std::memory_order_acquire, std::memory_order_acquire))) {\n#else\n\t\t\t\t\tif ((probedKey == empty    && mainHash->entries[index].key.compare_exchange_strong(empty,    id, std::memory_order_relaxed, std::memory_order_relaxed))) {\n#endif\n\t\t\t\t\t\tmainHash->entries[index].value = producer;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\t++index;\n\t\t\t\t}\n\t\t\t\treturn producer;\n\t\t\t}\n\t\t\t\n\t\t\t// Hmm, the old hash is quite full and somebody else is busy allocating a new one.\n\t\t\t// We need to wait for the allocating thread to finish (if it succeeds, we add, if not,\n\t\t\t// we try to allocate ourselves).\n\t\t\tmainHash = implicitProducerHash.load(std::memory_order_acquire);\n\t\t}\n\t}\n\t\n#ifdef MOODYCAMEL_CPP11_THREAD_LOCAL_SUPPORTED\n\tvoid implicit_producer_thread_exited(ImplicitProducer* producer)\n\t{\n\t\t// Remove from thread exit listeners\n\t\tdetails::ThreadExitNotifier::unsubscribe(&producer->threadExitListener);\n\t\t\n\t\t// Remove from hash\n#ifdef MCDBGQ_NOLOCKFREE_IMPLICITPRODHASH\n\t\tdebug::DebugLock lock(implicitProdMutex);\n#endif\n\t\tauto hash = implicitProducerHash.load(std::memory_order_acquire);\n\t\tassert(hash != nullptr);\t\t// The thread exit listener is only registered if we were added to a hash in the first place\n\t\tauto id = details::thread_id();\n\t\tauto hashedId = details::hash_thread_id(id);\n\t\tdetails::thread_id_t probedKey;\n\t\t\n\t\t// We need to traverse all the hashes just in case other threads aren't on the current one yet and are\n\t\t// trying to add an entry thinking there's a free slot (because they reused a producer)\n\t\tfor (; hash != nullptr; hash = hash->prev) {\n\t\t\tauto index = hashedId;\n\t\t\tdo {\n\t\t\t\tindex &= hash->capacity - 1;\n\t\t\t\tprobedKey = hash->entries[index].key.load(std::memory_order_relaxed);\n\t\t\t\tif (probedKey == id) {\n\t\t\t\t\thash->entries[index].key.store(details::invalid_thread_id2, std::memory_order_release);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\t++index;\n\t\t\t} while (probedKey != details::invalid_thread_id);\t\t// Can happen if the hash has changed but we weren't put back in it yet, or if we weren't added to this hash in the first place\n\t\t}\n\t\t\n\t\t// Mark the queue as being recyclable\n\t\tproducer->inactive.store(true, std::memory_order_release);\n\t}\n\t\n\tstatic void implicit_producer_thread_exited_callback(void* userData)\n\t{\n\t\tauto producer = static_cast<ImplicitProducer*>(userData);\n\t\tauto queue = producer->parent;\n\t\tqueue->implicit_producer_thread_exited(producer);\n\t}\n#endif\n\t\n\t//////////////////////////////////\n\t// Utility functions\n\t//////////////////////////////////\n\n\ttemplate<typename TAlign>\n\tstatic inline void* aligned_malloc(size_t size)\n\t{\n\t\tMOODYCAMEL_CONSTEXPR_IF (std::alignment_of<TAlign>::value <= std::alignment_of<details::max_align_t>::value)\n\t\t\treturn (Traits::malloc)(size);\n\t\telse {\n\t\t\tsize_t alignment = std::alignment_of<TAlign>::value;\n\t\t\tvoid* raw = (Traits::malloc)(size + alignment - 1 + sizeof(void*));\n\t\t\tif (!raw)\n\t\t\t\treturn nullptr;\n\t\t\tchar* ptr = details::align_for<TAlign>(reinterpret_cast<char*>(raw) + sizeof(void*));\n\t\t\t*(reinterpret_cast<void**>(ptr) - 1) = raw;\n\t\t\treturn ptr;\n\t\t}\n\t}\n\n\ttemplate<typename TAlign>\n\tstatic inline void aligned_free(void* ptr)\n\t{\n\t\tMOODYCAMEL_CONSTEXPR_IF (std::alignment_of<TAlign>::value <= std::alignment_of<details::max_align_t>::value)\n\t\t\treturn (Traits::free)(ptr);\n\t\telse\n\t\t\t(Traits::free)(ptr ? *(reinterpret_cast<void**>(ptr) - 1) : nullptr);\n\t}\n\n\ttemplate<typename U>\n\tstatic inline U* create_array(size_t count)\n\t{\n\t\tassert(count > 0);\n\t\tU* p = static_cast<U*>(aligned_malloc<U>(sizeof(U) * count));\n\t\tif (p == nullptr)\n\t\t\treturn nullptr;\n\n\t\tfor (size_t i = 0; i != count; ++i)\n\t\t\tnew (p + i) U();\n\t\treturn p;\n\t}\n\n\ttemplate<typename U>\n\tstatic inline void destroy_array(U* p, size_t count)\n\t{\n\t\tif (p != nullptr) {\n\t\t\tassert(count > 0);\n\t\t\tfor (size_t i = count; i != 0; )\n\t\t\t\t(p + --i)->~U();\n\t\t}\n\t\taligned_free<U>(p);\n\t}\n\n\ttemplate<typename U>\n\tstatic inline U* create()\n\t{\n\t\tvoid* p = aligned_malloc<U>(sizeof(U));\n\t\treturn p != nullptr ? new (p) U : nullptr;\n\t}\n\n\ttemplate<typename U, typename A1>\n\tstatic inline U* create(A1&& a1)\n\t{\n\t\tvoid* p = aligned_malloc<U>(sizeof(U));\n\t\treturn p != nullptr ? new (p) U(std::forward<A1>(a1)) : nullptr;\n\t}\n\n\ttemplate<typename U>\n\tstatic inline void destroy(U* p)\n\t{\n\t\tif (p != nullptr)\n\t\t\tp->~U();\n\t\taligned_free<U>(p);\n\t}\n\nprivate:\n\tstd::atomic<ProducerBase*> producerListTail;\n\tstd::atomic<std::uint32_t> producerCount;\n\t\n\tstd::atomic<size_t> initialBlockPoolIndex;\n\tBlock* initialBlockPool;\n\tsize_t initialBlockPoolSize;\n\t\n#ifndef MCDBGQ_USEDEBUGFREELIST\n\tFreeList<Block> freeList;\n#else\n\tdebug::DebugFreeList<Block> freeList;\n#endif\n\t\n\tstd::atomic<ImplicitProducerHash*> implicitProducerHash;\n\tstd::atomic<size_t> implicitProducerHashCount;\t\t// Number of slots logically used\n\tImplicitProducerHash initialImplicitProducerHash;\n\tstd::array<ImplicitProducerKVP, INITIAL_IMPLICIT_PRODUCER_HASH_SIZE> initialImplicitProducerHashEntries;\n\tstd::atomic_flag implicitProducerHashResizeInProgress;\n\t\n\tstd::atomic<std::uint32_t> nextExplicitConsumerId;\n\tstd::atomic<std::uint32_t> globalExplicitConsumerOffset;\n\t\n#ifdef MCDBGQ_NOLOCKFREE_IMPLICITPRODHASH\n\tdebug::DebugMutex implicitProdMutex;\n#endif\n\t\n#ifdef MOODYCAMEL_QUEUE_INTERNAL_DEBUG\n\tstd::atomic<ExplicitProducer*> explicitProducers;\n\tstd::atomic<ImplicitProducer*> implicitProducers;\n#endif\n};\n\n\ntemplate<typename T, typename Traits>\nProducerToken::ProducerToken(ConcurrentQueue<T, Traits>& queue)\n\t: producer(queue.recycle_or_create_producer(true))\n{\n\tif (producer != nullptr) {\n\t\tproducer->token = this;\n\t}\n}\n\ntemplate<typename T, typename Traits>\nProducerToken::ProducerToken(BlockingConcurrentQueue<T, Traits>& queue)\n\t: producer(reinterpret_cast<ConcurrentQueue<T, Traits>*>(&queue)->recycle_or_create_producer(true))\n{\n\tif (producer != nullptr) {\n\t\tproducer->token = this;\n\t}\n}\n\ntemplate<typename T, typename Traits>\nConsumerToken::ConsumerToken(ConcurrentQueue<T, Traits>& queue)\n\t: itemsConsumedFromCurrent(0), currentProducer(nullptr), desiredProducer(nullptr)\n{\n\tinitialOffset = queue.nextExplicitConsumerId.fetch_add(1, std::memory_order_release);\n\tlastKnownGlobalOffset = static_cast<std::uint32_t>(-1);\n}\n\ntemplate<typename T, typename Traits>\nConsumerToken::ConsumerToken(BlockingConcurrentQueue<T, Traits>& queue)\n\t: itemsConsumedFromCurrent(0), currentProducer(nullptr), desiredProducer(nullptr)\n{\n\tinitialOffset = reinterpret_cast<ConcurrentQueue<T, Traits>*>(&queue)->nextExplicitConsumerId.fetch_add(1, std::memory_order_release);\n\tlastKnownGlobalOffset = static_cast<std::uint32_t>(-1);\n}\n\ntemplate<typename T, typename Traits>\ninline void swap(ConcurrentQueue<T, Traits>& a, ConcurrentQueue<T, Traits>& b) MOODYCAMEL_NOEXCEPT\n{\n\ta.swap(b);\n}\n\ninline void swap(ProducerToken& a, ProducerToken& b) MOODYCAMEL_NOEXCEPT\n{\n\ta.swap(b);\n}\n\ninline void swap(ConsumerToken& a, ConsumerToken& b) MOODYCAMEL_NOEXCEPT\n{\n\ta.swap(b);\n}\n\ntemplate<typename T, typename Traits>\ninline void swap(typename ConcurrentQueue<T, Traits>::ImplicitProducerKVP& a, typename ConcurrentQueue<T, Traits>::ImplicitProducerKVP& b) MOODYCAMEL_NOEXCEPT\n{\n\ta.swap(b);\n}\n\n}\n\n#if defined(_MSC_VER) && (!defined(_HAS_CXX17) || !_HAS_CXX17)\n#pragma warning(pop)\n#endif\n\n#if defined(__GNUC__) && !defined(__INTEL_COMPILER)\n#pragma GCC diagnostic pop\n#endif\n\n"
  },
  {
    "path": "deps/concurrentqueue/lightweightsemaphore.h",
    "content": "// Provides an efficient implementation of a semaphore (LightweightSemaphore).\n// This is an extension of Jeff Preshing's sempahore implementation (licensed \n// under the terms of its separate zlib license) that has been adapted and\n// extended by Cameron Desrochers.\n\n#pragma once\n\n#include <cstddef> // For std::size_t\n#include <atomic>\n#include <type_traits> // For std::make_signed<T>\n\n#if defined(_WIN32)\n// Avoid including windows.h in a header; we only need a handful of\n// items, so we'll redeclare them here (this is relatively safe since\n// the API generally has to remain stable between Windows versions).\n// I know this is an ugly hack but it still beats polluting the global\n// namespace with thousands of generic names or adding a .cpp for nothing.\nextern \"C\" {\n\tstruct _SECURITY_ATTRIBUTES;\n\t__declspec(dllimport) void* __stdcall CreateSemaphoreW(_SECURITY_ATTRIBUTES* lpSemaphoreAttributes, long lInitialCount, long lMaximumCount, const wchar_t* lpName);\n\t__declspec(dllimport) int __stdcall CloseHandle(void* hObject);\n\t__declspec(dllimport) unsigned long __stdcall WaitForSingleObject(void* hHandle, unsigned long dwMilliseconds);\n\t__declspec(dllimport) int __stdcall ReleaseSemaphore(void* hSemaphore, long lReleaseCount, long* lpPreviousCount);\n}\n#elif defined(__MACH__)\n#include <mach/mach.h>\n#elif defined(__unix__)\n#include <semaphore.h>\n#endif\n\nnamespace moodycamel\n{\nnamespace details\n{\n\n// Code in the mpmc_sema namespace below is an adaptation of Jeff Preshing's\n// portable + lightweight semaphore implementations, originally from\n// https://github.com/preshing/cpp11-on-multicore/blob/master/common/sema.h\n// LICENSE:\n// Copyright (c) 2015 Jeff Preshing\n//\n// This software is provided 'as-is', without any express or implied\n// warranty. In no event will the authors be held liable for any damages\n// arising from the use of this software.\n//\n// Permission is granted to anyone to use this software for any purpose,\n// including commercial applications, and to alter it and redistribute it\n// freely, subject to the following restrictions:\n//\n// 1. The origin of this software must not be misrepresented; you must not\n//\tclaim that you wrote the original software. If you use this software\n//\tin a product, an acknowledgement in the product documentation would be\n//\tappreciated but is not required.\n// 2. Altered source versions must be plainly marked as such, and must not be\n//\tmisrepresented as being the original software.\n// 3. This notice may not be removed or altered from any source distribution.\n#if defined(_WIN32)\nclass Semaphore\n{\nprivate:\n\tvoid* m_hSema;\n\t\n\tSemaphore(const Semaphore& other) MOODYCAMEL_DELETE_FUNCTION;\n\tSemaphore& operator=(const Semaphore& other) MOODYCAMEL_DELETE_FUNCTION;\n\npublic:\n\tSemaphore(int initialCount = 0)\n\t{\n\t\tassert(initialCount >= 0);\n\t\tconst long maxLong = 0x7fffffff;\n\t\tm_hSema = CreateSemaphoreW(nullptr, initialCount, maxLong, nullptr);\n\t\tassert(m_hSema);\n\t}\n\n\t~Semaphore()\n\t{\n\t\tCloseHandle(m_hSema);\n\t}\n\n\tbool wait()\n\t{\n\t\tconst unsigned long infinite = 0xffffffff;\n\t\treturn WaitForSingleObject(m_hSema, infinite) == 0;\n\t}\n\t\n\tbool try_wait()\n\t{\n\t\treturn WaitForSingleObject(m_hSema, 0) == 0;\n\t}\n\t\n\tbool timed_wait(std::uint64_t usecs)\n\t{\n\t\treturn WaitForSingleObject(m_hSema, (unsigned long)(usecs / 1000)) == 0;\n\t}\n\n\tvoid signal(int count = 1)\n\t{\n\t\twhile (!ReleaseSemaphore(m_hSema, count, nullptr));\n\t}\n};\n#elif defined(__MACH__)\n//---------------------------------------------------------\n// Semaphore (Apple iOS and OSX)\n// Can't use POSIX semaphores due to http://lists.apple.com/archives/darwin-kernel/2009/Apr/msg00010.html\n//---------------------------------------------------------\nclass Semaphore\n{\nprivate:\n\tsemaphore_t m_sema;\n\n\tSemaphore(const Semaphore& other) MOODYCAMEL_DELETE_FUNCTION;\n\tSemaphore& operator=(const Semaphore& other) MOODYCAMEL_DELETE_FUNCTION;\n\npublic:\n\tSemaphore(int initialCount = 0)\n\t{\n\t\tassert(initialCount >= 0);\n\t\tkern_return_t rc = semaphore_create(mach_task_self(), &m_sema, SYNC_POLICY_FIFO, initialCount);\n\t\tassert(rc == KERN_SUCCESS);\n\t\t(void)rc;\n\t}\n\n\t~Semaphore()\n\t{\n\t\tsemaphore_destroy(mach_task_self(), m_sema);\n\t}\n\n\tbool wait()\n\t{\n\t\treturn semaphore_wait(m_sema) == KERN_SUCCESS;\n\t}\n\t\n\tbool try_wait()\n\t{\n\t\treturn timed_wait(0);\n\t}\n\t\n\tbool timed_wait(std::uint64_t timeout_usecs)\n\t{\n\t\tmach_timespec_t ts;\n\t\tts.tv_sec = static_cast<unsigned int>(timeout_usecs / 1000000);\n\t\tts.tv_nsec = static_cast<int>((timeout_usecs % 1000000) * 1000);\n\n\t\t// added in OSX 10.10: https://developer.apple.com/library/prerelease/mac/documentation/General/Reference/APIDiffsMacOSX10_10SeedDiff/modules/Darwin.html\n\t\tkern_return_t rc = semaphore_timedwait(m_sema, ts);\n\t\treturn rc == KERN_SUCCESS;\n\t}\n\n\tvoid signal()\n\t{\n\t\twhile (semaphore_signal(m_sema) != KERN_SUCCESS);\n\t}\n\n\tvoid signal(int count)\n\t{\n\t\twhile (count-- > 0)\n\t\t{\n\t\t\twhile (semaphore_signal(m_sema) != KERN_SUCCESS);\n\t\t}\n\t}\n};\n#elif defined(__unix__)\n//---------------------------------------------------------\n// Semaphore (POSIX, Linux)\n//---------------------------------------------------------\nclass Semaphore\n{\nprivate:\n\tsem_t m_sema;\n\n\tSemaphore(const Semaphore& other) MOODYCAMEL_DELETE_FUNCTION;\n\tSemaphore& operator=(const Semaphore& other) MOODYCAMEL_DELETE_FUNCTION;\n\npublic:\n\tSemaphore(int initialCount = 0)\n\t{\n\t\tassert(initialCount >= 0);\n\t\tint rc = sem_init(&m_sema, 0, static_cast<unsigned int>(initialCount));\n\t\tassert(rc == 0);\n\t\t(void)rc;\n\t}\n\n\t~Semaphore()\n\t{\n\t\tsem_destroy(&m_sema);\n\t}\n\n\tbool wait()\n\t{\n\t\t// http://stackoverflow.com/questions/2013181/gdb-causes-sem-wait-to-fail-with-eintr-error\n\t\tint rc;\n\t\tdo {\n\t\t\trc = sem_wait(&m_sema);\n\t\t} while (rc == -1 && errno == EINTR);\n\t\treturn rc == 0;\n\t}\n\n\tbool try_wait()\n\t{\n\t\tint rc;\n\t\tdo {\n\t\t\trc = sem_trywait(&m_sema);\n\t\t} while (rc == -1 && errno == EINTR);\n\t\treturn rc == 0;\n\t}\n\n\tbool timed_wait(std::uint64_t usecs)\n\t{\n\t\tstruct timespec ts;\n\t\tconst int usecs_in_1_sec = 1000000;\n\t\tconst int nsecs_in_1_sec = 1000000000;\n\t\tclock_gettime(CLOCK_REALTIME, &ts);\n\t\tts.tv_sec += (time_t)(usecs / usecs_in_1_sec);\n\t\tts.tv_nsec += (long)(usecs % usecs_in_1_sec) * 1000;\n\t\t// sem_timedwait bombs if you have more than 1e9 in tv_nsec\n\t\t// so we have to clean things up before passing it in\n\t\tif (ts.tv_nsec >= nsecs_in_1_sec) {\n\t\t\tts.tv_nsec -= nsecs_in_1_sec;\n\t\t\t++ts.tv_sec;\n\t\t}\n\n\t\tint rc;\n\t\tdo {\n\t\t\trc = sem_timedwait(&m_sema, &ts);\n\t\t} while (rc == -1 && errno == EINTR);\n\t\treturn rc == 0;\n\t}\n\n\tvoid signal()\n\t{\n\t\twhile (sem_post(&m_sema) == -1);\n\t}\n\n\tvoid signal(int count)\n\t{\n\t\twhile (count-- > 0)\n\t\t{\n\t\t\twhile (sem_post(&m_sema) == -1);\n\t\t}\n\t}\n};\n#else\n#error Unsupported platform! (No semaphore wrapper available)\n#endif\n\n}\t// end namespace details\n\n\n//---------------------------------------------------------\n// LightweightSemaphore\n//---------------------------------------------------------\nclass LightweightSemaphore\n{\npublic:\n\ttypedef std::make_signed<std::size_t>::type ssize_t;\n\nprivate:\n\tstd::atomic<ssize_t> m_count;\n\tdetails::Semaphore m_sema;\n\tint m_maxSpins;\n\n\tbool waitWithPartialSpinning(std::int64_t timeout_usecs = -1)\n\t{\n\t\tssize_t oldCount;\n\t\tint spin = m_maxSpins;\n\t\twhile (--spin >= 0)\n\t\t{\n\t\t\toldCount = m_count.load(std::memory_order_relaxed);\n\t\t\tif ((oldCount > 0) && m_count.compare_exchange_strong(oldCount, oldCount - 1, std::memory_order_acquire, std::memory_order_relaxed))\n\t\t\t\treturn true;\n\t\t\tstd::atomic_signal_fence(std::memory_order_acquire);\t // Prevent the compiler from collapsing the loop.\n\t\t}\n\t\toldCount = m_count.fetch_sub(1, std::memory_order_acquire);\n\t\tif (oldCount > 0)\n\t\t\treturn true;\n\t\tif (timeout_usecs < 0)\n\t\t{\n\t\t\tif (m_sema.wait())\n\t\t\t\treturn true;\n\t\t}\n\t\tif (timeout_usecs > 0 && m_sema.timed_wait((std::uint64_t)timeout_usecs))\n\t\t\treturn true;\n\t\t// At this point, we've timed out waiting for the semaphore, but the\n\t\t// count is still decremented indicating we may still be waiting on\n\t\t// it. So we have to re-adjust the count, but only if the semaphore\n\t\t// wasn't signaled enough times for us too since then. If it was, we\n\t\t// need to release the semaphore too.\n\t\twhile (true)\n\t\t{\n\t\t\toldCount = m_count.load(std::memory_order_acquire);\n\t\t\tif (oldCount >= 0 && m_sema.try_wait())\n\t\t\t\treturn true;\n\t\t\tif (oldCount < 0 && m_count.compare_exchange_strong(oldCount, oldCount + 1, std::memory_order_relaxed, std::memory_order_relaxed))\n\t\t\t\treturn false;\n\t\t}\n\t}\n\n\tssize_t waitManyWithPartialSpinning(ssize_t max, std::int64_t timeout_usecs = -1)\n\t{\n\t\tassert(max > 0);\n\t\tssize_t oldCount;\n\t\tint spin = m_maxSpins;\n\t\twhile (--spin >= 0)\n\t\t{\n\t\t\toldCount = m_count.load(std::memory_order_relaxed);\n\t\t\tif (oldCount > 0)\n\t\t\t{\n\t\t\t\tssize_t newCount = oldCount > max ? oldCount - max : 0;\n\t\t\t\tif (m_count.compare_exchange_strong(oldCount, newCount, std::memory_order_acquire, std::memory_order_relaxed))\n\t\t\t\t\treturn oldCount - newCount;\n\t\t\t}\n\t\t\tstd::atomic_signal_fence(std::memory_order_acquire);\n\t\t}\n\t\toldCount = m_count.fetch_sub(1, std::memory_order_acquire);\n\t\tif (oldCount <= 0)\n\t\t{\n\t\t\tif ((timeout_usecs == 0) || (timeout_usecs < 0 && !m_sema.wait()) || (timeout_usecs > 0 && !m_sema.timed_wait((std::uint64_t)timeout_usecs)))\n\t\t\t{\n\t\t\t\twhile (true)\n\t\t\t\t{\n\t\t\t\t\toldCount = m_count.load(std::memory_order_acquire);\n\t\t\t\t\tif (oldCount >= 0 && m_sema.try_wait())\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tif (oldCount < 0 && m_count.compare_exchange_strong(oldCount, oldCount + 1, std::memory_order_relaxed, std::memory_order_relaxed))\n\t\t\t\t\t\treturn 0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (max > 1)\n\t\t\treturn 1 + tryWaitMany(max - 1);\n\t\treturn 1;\n\t}\n\npublic:\n\tLightweightSemaphore(ssize_t initialCount = 0, int maxSpins = 10000) : m_count(initialCount), m_maxSpins(maxSpins)\n\t{\n\t\tassert(initialCount >= 0);\n\t\tassert(maxSpins >= 0);\n\t}\n\n\tbool tryWait()\n\t{\n\t\tssize_t oldCount = m_count.load(std::memory_order_relaxed);\n\t\twhile (oldCount > 0)\n\t\t{\n\t\t\tif (m_count.compare_exchange_weak(oldCount, oldCount - 1, std::memory_order_acquire, std::memory_order_relaxed))\n\t\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n\n\tbool wait()\n\t{\n\t\treturn tryWait() || waitWithPartialSpinning();\n\t}\n\n\tbool wait(std::int64_t timeout_usecs)\n\t{\n\t\treturn tryWait() || waitWithPartialSpinning(timeout_usecs);\n\t}\n\n\t// Acquires between 0 and (greedily) max, inclusive\n\tssize_t tryWaitMany(ssize_t max)\n\t{\n\t\tassert(max >= 0);\n\t\tssize_t oldCount = m_count.load(std::memory_order_relaxed);\n\t\twhile (oldCount > 0)\n\t\t{\n\t\t\tssize_t newCount = oldCount > max ? oldCount - max : 0;\n\t\t\tif (m_count.compare_exchange_weak(oldCount, newCount, std::memory_order_acquire, std::memory_order_relaxed))\n\t\t\t\treturn oldCount - newCount;\n\t\t}\n\t\treturn 0;\n\t}\n\n\t// Acquires at least one, and (greedily) at most max\n\tssize_t waitMany(ssize_t max, std::int64_t timeout_usecs)\n\t{\n\t\tassert(max >= 0);\n\t\tssize_t result = tryWaitMany(max);\n\t\tif (result == 0 && max > 0)\n\t\t\tresult = waitManyWithPartialSpinning(max, timeout_usecs);\n\t\treturn result;\n\t}\n\t\n\tssize_t waitMany(ssize_t max)\n\t{\n\t\tssize_t result = waitMany(max, -1);\n\t\tassert(result > 0);\n\t\treturn result;\n\t}\n\n\tvoid signal(ssize_t count = 1)\n\t{\n\t\tassert(count >= 0);\n\t\tssize_t oldCount = m_count.fetch_add(count, std::memory_order_release);\n\t\tssize_t toRelease = -oldCount < count ? -oldCount : count;\n\t\tif (toRelease > 0)\n\t\t{\n\t\t\tm_sema.signal((int)toRelease);\n\t\t}\n\t}\n\t\n\tstd::size_t availableApprox() const\n\t{\n\t\tssize_t count = m_count.load(std::memory_order_relaxed);\n\t\treturn count > 0 ? static_cast<std::size_t>(count) : 0;\n\t}\n};\n\n}   // end namespace moodycamel\n\n"
  },
  {
    "path": "deps/cpp-statsd-client/.clang-format",
    "content": "AccessModifierOffset: -4\nAllowAllParametersOfDeclarationOnNextLine: false\nAllowShortFunctionsOnASingleLine: Empty\nBinPackArguments: false\nBinPackParameters: false\nColumnLimit:     120\nIndentCaseLabels: false\nIndentWidth:     4\n\n---\nLanguage:        Cpp\n# BasedOnStyle:  Google\n#AccessModifierOffset: -1\nAlignAfterOpenBracket: Align\nAlignConsecutiveAssignments: false\nAlignConsecutiveDeclarations: false\nAlignEscapedNewlinesLeft: true\nAlignOperands:   true\nAlignTrailingComments: true\n#AllowAllParametersOfDeclarationOnNextLine: true\nAllowShortBlocksOnASingleLine: false\nAllowShortCaseLabelsOnASingleLine: false\n#AllowShortFunctionsOnASingleLine: All\nAllowShortIfStatementsOnASingleLine: true\nAllowShortLoopsOnASingleLine: true\nAlwaysBreakAfterDefinitionReturnType: None\nAlwaysBreakAfterReturnType: None\nAlwaysBreakBeforeMultilineStrings: true\nAlwaysBreakTemplateDeclarations: true\n#BinPackArguments: true\n#BinPackParameters: true\nBraceWrapping:\n  AfterClass:      false\n  AfterControlStatement: false\n  AfterEnum:       false\n  AfterFunction:   false\n  AfterNamespace:  false\n  AfterObjCDeclaration: false\n  AfterStruct:     false\n  AfterUnion:      false\n  BeforeCatch:     false\n  BeforeElse:      false\n  IndentBraces:    false\nBreakBeforeBinaryOperators: None\nBreakBeforeBraces: Attach\nBreakBeforeTernaryOperators: true\nBreakConstructorInitializersBeforeComma: false\nBreakAfterJavaFieldAnnotations: false\nBreakStringLiterals: true\n#ColumnLimit:     80\nCommentPragmas:  '^ IWYU pragma:'\nConstructorInitializerAllOnOneLineOrOnePerLine: true\nConstructorInitializerIndentWidth: 4\nContinuationIndentWidth: 4\nCpp11BracedListStyle: true\nDerivePointerAlignment: true\nDisableFormat:   false\nExperimentalAutoDetectBinPacking: false\nForEachMacros:   [ foreach, Q_FOREACH, BOOST_FOREACH ]\nIncludeCategories:\n  - Regex:           '^<.*\\.h>'\n    Priority:        1\n  - Regex:           '^<.*'\n    Priority:        2\n  - Regex:           '.*'\n    Priority:        3\nIncludeIsMainRegex: '([-_](test|unittest))?$'\n#IndentCaseLabels: true\n#IndentWidth:     2\nIndentWrappedFunctionNames: false\nJavaScriptQuotes: Leave\nJavaScriptWrapImports: true\nKeepEmptyLinesAtTheStartOfBlocks: false\nMacroBlockBegin: ''\nMacroBlockEnd:   ''\nMaxEmptyLinesToKeep: 1\nNamespaceIndentation: None\nObjCBlockIndentWidth: 2\nObjCSpaceAfterProperty: false\nObjCSpaceBeforeProtocolList: false\nPenaltyBreakBeforeFirstCallParameter: 1\nPenaltyBreakComment: 300\nPenaltyBreakFirstLessLess: 120\nPenaltyBreakString: 1000\nPenaltyExcessCharacter: 1000000\nPenaltyReturnTypeOnItsOwnLine: 200\nPointerAlignment: Left\nReflowComments:  true\nSortIncludes:    true\nSpaceAfterCStyleCast: false\nSpaceAfterTemplateKeyword: true\nSpaceBeforeAssignmentOperators: true\nSpaceBeforeParens: ControlStatements\nSpaceInEmptyParentheses: false\nSpacesBeforeTrailingComments: 2\nSpacesInAngles:  false\nSpacesInContainerLiterals: true\nSpacesInCStyleCastParentheses: false\nSpacesInParentheses: false\nSpacesInSquareBrackets: false\nStandard:        Auto\nTabWidth:        8\nUseTab:          Never\n...\n\n"
  },
  {
    "path": "deps/cpp-statsd-client/.github/workflows/coverage.yml",
    "content": "name: Coverage\n\non: [push, pull_request]\njobs:\n  coverage:\n    runs-on: ubuntu-20.04\n    steps:\n      - uses: actions/checkout@v2\n      - name: dependencies\n        shell: bash\n        run: |\n          sudo apt-get update\n          sudo apt-get install -y -qq make cmake gcc g++ lcov bc\n      - name: build\n        shell: bash\n        run: |\n          export LD_LIBRARY_PATH=.:$(cat /etc/ld.so.conf.d/* | grep -vF \"#\" | tr \"\\\\n\" \":\" | sed -e \"s/:$//g\")\n          cmake . -DCMAKE_BUILD_TYPE=Debug -DENABLE_COVERAGE=On\n          make all -j$(nproc)\n      - name: coverage\n        shell: bash\n        run: |\n          make coverage\n          lines=$(lcov --summary coverage.info | grep -F lines | awk '{print $2}' | sed -e \"s/%//g\")\n          if (( $(echo \"${lines} < ${COVERAGE_THRESHOLD}\" | bc -l) )); then\n              echo \"Line coverage dropped below ${COVERAGE_THRESHOLD}% to ${lines}%\"\n              exit 1\n          fi\n        env:\n          COVERAGE_THRESHOLD: 85.0\n"
  },
  {
    "path": "deps/cpp-statsd-client/.github/workflows/lint.yml",
    "content": "name: Lint\n\non: [push, pull_request]\njobs:\n  lint:\n    runs-on: ubuntu-20.04\n    steps:\n      - uses: actions/checkout@v2\n      - name: lint\n        uses: DoozyX/clang-format-lint-action@v0.12\n        with:\n          clangFormatVersion: 12\n          source: './include/cpp-statsd-client ./tests'\n"
  },
  {
    "path": "deps/cpp-statsd-client/.github/workflows/linux.yml",
    "content": "name: Linux\n\non: [push, pull_request]\njobs:\n  linux:\n    runs-on: ubuntu-20.04\n    steps:\n      - uses: actions/checkout@v2\n      - name: dependencies\n        shell: bash\n        run: |\n          sudo apt-get update\n          sudo apt-get install -y -qq make cmake gcc g++\n      - name: build\n        shell: bash\n        run: |\n          export LD_LIBRARY_PATH=.:$(cat /etc/ld.so.conf.d/* | grep -vF \"#\" | tr \"\\\\n\" \":\" | sed -e \"s/:$//g\")\n          cmake . -DCMAKE_BUILD_TYPE=RelWithDebInfo -DENABLE_SANITIZERS=On\n          make all -j$(nproc)\n      - name: test\n        shell: bash\n        run: |\n          make test\n"
  },
  {
    "path": "deps/cpp-statsd-client/.github/workflows/windows.yml",
    "content": "name: Windows\n\non: [push, pull_request]\njobs:\n  windows:\n    runs-on: windows-latest\n    steps:\n      - uses: actions/checkout@v2\n      - name: dependencies\n        run: |\n          choco install cmake\n      - name: build\n        run: |\n          cmake -S . -B build -G \"Visual Studio 16 2019\" -A x64\n          cmake --build build --target ALL_BUILD --config Release\n      - name: test\n        run: |\n          cmake --build build --target RUN_TESTS --config Release\n"
  },
  {
    "path": "deps/cpp-statsd-client/.gitignore",
    "content": "bin"
  },
  {
    "path": "deps/cpp-statsd-client/CMakeLists.txt",
    "content": "# Basic project setup\ncmake_minimum_required(VERSION 3.5)\nproject(cpp-statsd-client\n        VERSION 1.0.2\n        LANGUAGES CXX\n        DESCRIPTION \"A header-only StatsD client implemented in C++\"\n        HOMEPAGE_URL \"https://github.com/vthiery/cpp-statsd-client\")\n\noption(CPP_STATSD_STANDALONE \"Allows configuration of targets for verifying library functionality\" ON)\noption(ENABLE_TESTS \"Build tests\" ON)\noption(ENABLE_COVERAGE \"Build with coverage instrumentalisation\" OFF)\n\nif(NOT CPP_STATSD_STANDALONE)\n  set(ENABLE_TESTS OFF)\n  set(ENABLE_COVERAGE OFF)\nendif()\n\ninclude(GNUInstallDirs)\ninclude(CMakePackageConfigHelpers)\nfind_package(Threads)\n\n# Optional code coverage targets\nif(ENABLE_COVERAGE)\n  set(COVERAGE_EXCLUDES /usr/*)\n  include(${PROJECT_SOURCE_DIR}/cmake/CodeCoverage.cmake)\n  APPEND_COVERAGE_COMPILER_FLAGS()\n  SETUP_TARGET_FOR_COVERAGE_LCOV(NAME coverage\n                                 EXECUTABLE testStatsdClient\n                                 DEPENDENCIES ${PROJECT_NAME}\n  )\nendif()\n\n# The library target\nadd_library(${PROJECT_NAME} INTERFACE)\ntarget_include_directories(\n  ${PROJECT_NAME}\n  INTERFACE $<BUILD_INTERFACE:${${PROJECT_NAME}_SOURCE_DIR}/include>\n            $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)\ntarget_link_libraries(${PROJECT_NAME} INTERFACE Threads::Threads)\nif(WIN32)\n  target_link_libraries(${PROJECT_NAME} INTERFACE ws2_32)\nendif()\n\n# The installation and pkg-config-like cmake config\ninstall(TARGETS ${PROJECT_NAME}\n        EXPORT ${PROJECT_NAME}_Targets\n        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}\n        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}\n        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})\nwrite_basic_package_version_file(\"${PROJECT_NAME}ConfigVersion.cmake\"\n                                 VERSION ${PROJECT_VERSION}\n                                 COMPATIBILITY SameMajorVersion)\nconfigure_package_config_file(\n  \"${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}Config.cmake.in\"\n  \"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake\"\n  INSTALL_DESTINATION\n  ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)\ninstall(EXPORT ${PROJECT_NAME}_Targets\n        FILE ${PROJECT_NAME}Targets.cmake\n        NAMESPACE ${PROJECT_NAME}::\n        DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)\ninstall(FILES \"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake\"\n              \"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake\"\n        DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)\ninstall(DIRECTORY ${PROJECT_SOURCE_DIR}/include DESTINATION include)\n\nif(ENABLE_TESTS)\n  # The test targets\n  add_executable(testStatsdClient ${CMAKE_CURRENT_SOURCE_DIR}/tests/testStatsdClient.cpp)\n  if(WIN32)\n    target_compile_options(testStatsdClient PRIVATE -W4 -WX /external:W0)\n  else()\n    target_compile_options(testStatsdClient PRIVATE -Wall -Wextra -pedantic -Werror)\n  endif()\n  target_include_directories(testStatsdClient PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/tests)\n  target_link_libraries(testStatsdClient ${PROJECT_NAME})\n\n  set_property(TARGET testStatsdClient PROPERTY CXX_STANDARD 11)\n  set_property(TARGET testStatsdClient PROPERTY CXX_EXTENSIONS OFF)\n\n  # The test suite\n  enable_testing()\n  add_test(ctestTestStatsdClient testStatsdClient)\n  add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} DEPENDS testStatsdClient)\nendif()\n"
  },
  {
    "path": "deps/cpp-statsd-client/LICENSE.md",
    "content": "MIT License\n\nCopyright (c) 2017 Vincent Thiery\n\nPermission 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:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE 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."
  },
  {
    "path": "deps/cpp-statsd-client/Makefile",
    "content": "# simple makefile to build, test and clean\n\nBUILD_MODE ?= Release\nENABLE_COVERAGE ?= On\n\nbuild: clean\n\t@echo \"Build in ${BUILD_MODE} mode\"\n\tmkdir -p bin/${BUILD_MODE}\n\t@cd bin/${BUILD_MODE}; cmake ../../ -DCMAKE_BUILD_TYPE=${BUILD_MODE} -DENABLE_COVERAGE=${ENABLE_COVERAGE}\n\t@cd bin/${BUILD_MODE}; make\n\ntest: build\n\t@cd bin/${BUILD_MODE}; make test\n\ncoverage: build\n\t@cd bin/${BUILD_MODE}; make coverage\n\ninstall: build\n\t@cd bin/${BUILD_MODE}; make install\n\nclean:\n\t@rm -rf bin\n"
  },
  {
    "path": "deps/cpp-statsd-client/README.md",
    "content": "# C++ StatsD Client\n\n![logo](https://raw.githubusercontent.com/vthiery/cpp-statsd-client/master/images/logo.svg?sanitize=true)\n\n[![Release](https://img.shields.io/github/release/vthiery/cpp-statsd-client.svg?style=for-the-badge)](https://github.com/vthiery/cpp-statsd-client/releases/latest)\n![License](https://img.shields.io/github/license/vthiery/cpp-statsd-client?style=for-the-badge)\n[![Linux status](https://img.shields.io/github/workflow/status/vthiery/cpp-statsd-client/Linux?label=Linux&style=for-the-badge)](https://github.com/vthiery/cpp-statsd-client/actions/workflows/linux.yml?query=branch%3Amaster++)\n[![Windows status](https://img.shields.io/github/workflow/status/vthiery/cpp-statsd-client/Windows?label=Windows&style=for-the-badge)](https://github.com/vthiery/cpp-statsd-client/actions/workflows/windows.yml?query=branch%3Amaster++)\n\nA header-only StatsD client implemented in C++.\nThe client allows:\n\n- batching,\n- change of configuration at runtime,\n- user-defined frequency rate.\n\n## Install and Test\n\n### Makefile\n\nIn order to install the header files and/or run the tests, simply use the Makefile and execute\n\n```sh\nmake install\n```\n\nand\n\n```sh\nmake test\n```\n\n### Conan\n\nIf you are using [Conan](https://www.conan.io/) to manage your dependencies, merely add statsdclient/x.y.z@vthiery/stable to your conanfile.py's requires, where x.y.z is the release version you want to use. Please file issues here if you experience problems with the packages. You can also directly download the latest version [here](https://bintray.com/vthiery/conan-packages/statsdclient%3Avthiery/_latestVersion).\n\n## Usage\n\n### Example\n\nA simple example of how to use the client:\n\n```cpp\n#include \"StatsdClient.hpp\"\nusing namespace Statsd;\n\nint main() {\n    // Define the client on localhost, with port 8080,\n    // using a prefix,\n    // a batching size of 20 bytes,\n    // and three points of precision for floating point gauge values\n    StatsdClient client{ \"127.0.0.1\", 8080, \"myPrefix\", 20, 3 };\n\n    // Increment the metric \"coco\"\n    client.increment(\"coco\");\n\n    // Decrement the metric \"kiki\"\n    client.decrement(\"kiki\");\n\n    // Adjusts \"toto\" by +3\n    client.count(\"toto\", 2, 0.1f);\n\n    // Record a gauge \"titi\" to 3\n    client.gauge(\"titi\", 3);\n\n    // Record a timing of 2ms for \"myTiming\"\n    client.timing(\"myTiming\", 2, 0.1f);\n\n    // Send a metric explicitly\n    client.send(\"tutu\", 4, \"c\", 2.0f);\n    exit(0);\n}\n```\n\n### Advanced Testing\n\nA simple mock StatsD server can be found at `tests/StatsdServer.hpp`. This can be used to do simple validation of your application's metrics, typically in the form of unit tests. In fact this is the primary means by which this library is tested. The mock server itself is not distributed with the library so to use it you'd need to vendor this project into your project. Once you have though, you can test your application's use of the client like so:\n\n```cpp\n#include \"StatsdClient.hpp\"\n#include \"StatsdServer.hpp\"\n\n#include <cassert>\n\nusing namespace Statsd;\n\nstruct MyApp {\n    void doWork() const {\n        m_client.count(\"bar\", 3);\n    }\nprivate:\n    StatsdClient m_client{\"localhost\", 8125, \"foo\"};\n};\n\nint main() {\n    StatsdServer mockServer;\n\n    MyApp app;\n    app.doWork();\n\n    assert(mockServer.receive() == \"foo.bar:3|c\");\n    exit(0);\n}\n```\n\n### Configuration\n\nThe configuration of the client must be input when one instantiates it. Nevertheless, the API allows the configuration ot change afterwards. For example, one can do the following:\n\n```cpp\n#include \"StatsdClient.hpp\"\nusing namespace Statsd;\n\nint main()\n{\n    // Define the client on localhost, with port 8080,\n    // using a prefix,\n    // a batching size of 20 bytes,\n    // and three points of precision for floating point gauge values\n    StatsdClient client{ \"127.0.0.1\", 8080, \"myPrefix\", 20, 3 };\n\n    client.increment(\"coco\");\n\n    // Set a new configuration, using a different port, a different prefix, and more gauge precision\n    client.setConfig(\"127.0.0.1\", 8000, \"anotherPrefix\", 6);\n\n    client.decrement(\"kiki\");\n}\n```\n\nThe batchsize is the only parameter that cannot be changed for the time being.\n\n### Batching\n\nThe client supports batching of the metrics. The batch size parameter is the number of bytes to allow in each batch (UDP datagram payload) to be sent to the statsd process. This number is not a hard limit. If appending the current stat to the current batch (separated by the `'\\n'` character) pushes the current batch over the batch size, that batch is enqueued (not sent) and a new batch is started. If batch size is 0, the default, then each stat is sent individually to the statsd process and no batches are enqueued.\n\n### Sending\n\nAs previously mentioned, if batching is disabled (by setting the batch size to 0) then every stat is sent immediately in a blocking fashion. If batching is enabled (ie non-zero) then you may also set the send interval. The send interval controls the time, in milliseconds, to wait before flushing/sending the queued stats batches to the statsd process. When the send interval is non-zero a background thread is spawned which will do the flushing/sending at the configured send interval, in other words asynchronously. The queuing mechanism in this case is *not* lock-free. If batching is enabled but the send interval is set to zero then the queued batchs of stats will not be sent automatically by a background thread but must be sent manually via the `flush` method. The `flush` method is a blocking call.\n\n\n### Frequency rate\n\nWhen sending a metric, a frequency rate can be set in order to limit the metrics' sampling. By default, the frequency rate is set to one and won't affect the sampling. If set to a value `epsilon` (0.0001 for the time being) close to one, the sampling is not affected either.\n\nIf the frequency rate is set and `epsilon` different from one, the sending will be rejected randomly (the higher the frequency rate, the lower the probability of rejection).\n\n## License\n\nThis library is under MIT license.\n"
  },
  {
    "path": "deps/cpp-statsd-client/cmake/CodeCoverage.cmake",
    "content": "# Copyright (c) 2012 - 2017, Lars Bilke\n# All rights reserved.\n#\n# Redistribution and use in source and binary forms, with or without modification,\n# are permitted provided that the following conditions are met:\n#\n# 1. Redistributions of source code must retain the above copyright notice, this\n#    list of conditions and the following disclaimer.\n#\n# 2. Redistributions in binary form must reproduce the above copyright notice,\n#    this list of conditions and the following disclaimer in the documentation\n#    and/or other materials provided with the distribution.\n#\n# 3. Neither the name of the copyright holder nor the names of its contributors\n#    may be used to endorse or promote products derived from this software without\n#    specific prior written permission.\n#\n# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND\n# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR\n# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\n# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\n# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n#\n# CHANGES:\n#\n# 2012-01-31, Lars Bilke\n# - Enable Code Coverage\n#\n# 2013-09-17, Joakim Söderberg\n# - Added support for Clang.\n# - Some additional usage instructions.\n#\n# 2016-02-03, Lars Bilke\n# - Refactored functions to use named parameters\n#\n# 2017-06-02, Lars Bilke\n# - Merged with modified version from github.com/ufz/ogs\n#\n# 2019-05-06, Anatolii Kurotych\n# - Remove unnecessary --coverage flag\n#\n# 2019-12-13, FeRD (Frank Dana)\n# - Deprecate COVERAGE_LCOVR_EXCLUDES and COVERAGE_GCOVR_EXCLUDES lists in favor\n#   of tool-agnostic COVERAGE_EXCLUDES variable, or EXCLUDE setup arguments.\n# - CMake 3.4+: All excludes can be specified relative to BASE_DIRECTORY\n# - All setup functions: accept BASE_DIRECTORY, EXCLUDE list\n# - Set lcov basedir with -b argument\n# - Add automatic --demangle-cpp in lcovr, if 'c++filt' is available (can be\n#   overridden with NO_DEMANGLE option in setup_target_for_coverage_lcovr().)\n# - Delete output dir, .info file on 'make clean'\n# - Remove Python detection, since version mismatches will break gcovr\n# - Minor cleanup (lowercase function names, update examples...)\n#\n# 2019-12-19, FeRD (Frank Dana)\n# - Rename Lcov outputs, make filtered file canonical, fix cleanup for targets\n#\n# 2020-01-19, Bob Apthorpe\n# - Added gfortran support\n#\n# 2020-02-17, FeRD (Frank Dana)\n# - Make all add_custom_target()s VERBATIM to auto-escape wildcard characters\n#   in EXCLUDEs, and remove manual escaping from gcovr targets\n#\n# 2021-01-19, Robin Mueller\n# - Add CODE_COVERAGE_VERBOSE option which will allow to print out commands which are run\n# - Added the option for users to set the GCOVR_ADDITIONAL_ARGS variable to supply additional\n#   flags to the gcovr command\n#\n# 2020-05-04, Mihchael Davis\n#     - Add -fprofile-abs-path to make gcno files contain absolute paths\n#     - Fix BASE_DIRECTORY not working when defined\n#     - Change BYPRODUCT from folder to index.html to stop ninja from complaining about double defines\n#\n# 2021-05-10, Martin Stump\n#     - Check if the generator is multi-config before warning about non-Debug builds\n#\n# USAGE:\n#\n# 1. Copy this file into your cmake modules path.\n#\n# 2. Add the following line to your CMakeLists.txt (best inside an if-condition\n#    using a CMake option() to enable it just optionally):\n#      include(CodeCoverage)\n#\n# 3. Append necessary compiler flags:\n#      append_coverage_compiler_flags()\n#\n# 3.a (OPTIONAL) Set appropriate optimization flags, e.g. -O0, -O1 or -Og\n#\n# 4. If you need to exclude additional directories from the report, specify them\n#    using full paths in the COVERAGE_EXCLUDES variable before calling\n#    setup_target_for_coverage_*().\n#    Example:\n#      set(COVERAGE_EXCLUDES\n#          '${PROJECT_SOURCE_DIR}/src/dir1/*'\n#          '/path/to/my/src/dir2/*')\n#    Or, use the EXCLUDE argument to setup_target_for_coverage_*().\n#    Example:\n#      setup_target_for_coverage_lcov(\n#          NAME coverage\n#          EXECUTABLE testrunner\n#          EXCLUDE \"${PROJECT_SOURCE_DIR}/src/dir1/*\" \"/path/to/my/src/dir2/*\")\n#\n# 4.a NOTE: With CMake 3.4+, COVERAGE_EXCLUDES or EXCLUDE can also be set\n#     relative to the BASE_DIRECTORY (default: PROJECT_SOURCE_DIR)\n#     Example:\n#       set(COVERAGE_EXCLUDES \"dir1/*\")\n#       setup_target_for_coverage_gcovr_html(\n#           NAME coverage\n#           EXECUTABLE testrunner\n#           BASE_DIRECTORY \"${PROJECT_SOURCE_DIR}/src\"\n#           EXCLUDE \"dir2/*\")\n#\n# 5. Use the functions described below to create a custom make target which\n#    runs your test executable and produces a code coverage report.\n#\n# 6. Build a Debug build:\n#      cmake -DCMAKE_BUILD_TYPE=Debug ..\n#      make\n#      make my_coverage_target\n#\n\ninclude(CMakeParseArguments)\n\noption(CODE_COVERAGE_VERBOSE \"Verbose information\" FALSE)\n\n# Check prereqs\nfind_program( GCOV_PATH gcov )\nfind_program( LCOV_PATH  NAMES lcov lcov.bat lcov.exe lcov.perl)\nfind_program( FASTCOV_PATH NAMES fastcov fastcov.py )\nfind_program( GENHTML_PATH NAMES genhtml genhtml.perl genhtml.bat )\nfind_program( GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/scripts/test)\nfind_program( CPPFILT_PATH NAMES c++filt )\n\nif(NOT GCOV_PATH)\n    message(FATAL_ERROR \"gcov not found! Aborting...\")\nendif() # NOT GCOV_PATH\n\nget_property(LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES)\nlist(GET LANGUAGES 0 LANG)\n\nif(\"${CMAKE_${LANG}_COMPILER_ID}\" MATCHES \"(Apple)?[Cc]lang\")\n    if(\"${CMAKE_${LANG}_COMPILER_VERSION}\" VERSION_LESS 3)\n        message(FATAL_ERROR \"Clang version must be 3.0.0 or greater! Aborting...\")\n    endif()\nelseif(NOT CMAKE_COMPILER_IS_GNUCXX)\n    if(\"${CMAKE_Fortran_COMPILER_ID}\" MATCHES \"[Ff]lang\")\n        # Do nothing; exit conditional without error if true\n    elseif(\"${CMAKE_Fortran_COMPILER_ID}\" MATCHES \"GNU\")\n        # Do nothing; exit conditional without error if true\n    else()\n        message(FATAL_ERROR \"Compiler is not GNU gcc! Aborting...\")\n    endif()\nendif()\n\nset(COVERAGE_COMPILER_FLAGS \"-g -fprofile-arcs -ftest-coverage\"\n    CACHE INTERNAL \"\")\nif(CMAKE_CXX_COMPILER_ID MATCHES \"(GNU|Clang)\")\n    include(CheckCXXCompilerFlag)\n    check_cxx_compiler_flag(-fprofile-abs-path HAVE_fprofile_abs_path)\n    if(HAVE_fprofile_abs_path)\n        set(COVERAGE_COMPILER_FLAGS \"${COVERAGE_COMPILER_FLAGS} -fprofile-abs-path\")\n    endif()\nendif()\n\nset(CMAKE_Fortran_FLAGS_COVERAGE\n    ${COVERAGE_COMPILER_FLAGS}\n    CACHE STRING \"Flags used by the Fortran compiler during coverage builds.\"\n    FORCE )\nset(CMAKE_CXX_FLAGS_COVERAGE\n    ${COVERAGE_COMPILER_FLAGS}\n    CACHE STRING \"Flags used by the C++ compiler during coverage builds.\"\n    FORCE )\nset(CMAKE_C_FLAGS_COVERAGE\n    ${COVERAGE_COMPILER_FLAGS}\n    CACHE STRING \"Flags used by the C compiler during coverage builds.\"\n    FORCE )\nset(CMAKE_EXE_LINKER_FLAGS_COVERAGE\n    \"\"\n    CACHE STRING \"Flags used for linking binaries during coverage builds.\"\n    FORCE )\nset(CMAKE_SHARED_LINKER_FLAGS_COVERAGE\n    \"\"\n    CACHE STRING \"Flags used by the shared libraries linker during coverage builds.\"\n    FORCE )\nmark_as_advanced(\n    CMAKE_Fortran_FLAGS_COVERAGE\n    CMAKE_CXX_FLAGS_COVERAGE\n    CMAKE_C_FLAGS_COVERAGE\n    CMAKE_EXE_LINKER_FLAGS_COVERAGE\n    CMAKE_SHARED_LINKER_FLAGS_COVERAGE )\n\nget_property(GENERATOR_IS_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)\nif(NOT (CMAKE_BUILD_TYPE STREQUAL \"Debug\" OR GENERATOR_IS_MULTI_CONFIG))\n    message(WARNING \"Code coverage results with an optimised (non-Debug) build may be misleading\")\nendif() # NOT (CMAKE_BUILD_TYPE STREQUAL \"Debug\" OR GENERATOR_IS_MULTI_CONFIG)\n\nif(CMAKE_C_COMPILER_ID STREQUAL \"GNU\" OR CMAKE_Fortran_COMPILER_ID STREQUAL \"GNU\")\n    link_libraries(gcov)\nendif()\n\n# Defines a target for running and collection code coverage information\n# Builds dependencies, runs the given executable and outputs reports.\n# NOTE! The executable should always have a ZERO as exit code otherwise\n# the coverage generation will not complete.\n#\n# setup_target_for_coverage_lcov(\n#     NAME testrunner_coverage                    # New target name\n#     EXECUTABLE testrunner -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR\n#     DEPENDENCIES testrunner                     # Dependencies to build first\n#     BASE_DIRECTORY \"../\"                        # Base directory for report\n#                                                 #  (defaults to PROJECT_SOURCE_DIR)\n#     EXCLUDE \"src/dir1/*\" \"src/dir2/*\"           # Patterns to exclude (can be relative\n#                                                 #  to BASE_DIRECTORY, with CMake 3.4+)\n#     NO_DEMANGLE                                 # Don't demangle C++ symbols\n#                                                 #  even if c++filt is found\n# )\nfunction(setup_target_for_coverage_lcov)\n\n    set(options NO_DEMANGLE)\n    set(oneValueArgs BASE_DIRECTORY NAME)\n    set(multiValueArgs EXCLUDE EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES LCOV_ARGS GENHTML_ARGS)\n    cmake_parse_arguments(Coverage \"${options}\" \"${oneValueArgs}\" \"${multiValueArgs}\" ${ARGN})\n\n    if(NOT LCOV_PATH)\n        message(FATAL_ERROR \"lcov not found! Aborting...\")\n    endif() # NOT LCOV_PATH\n\n    if(NOT GENHTML_PATH)\n        message(FATAL_ERROR \"genhtml not found! Aborting...\")\n    endif() # NOT GENHTML_PATH\n\n    # Set base directory (as absolute path), or default to PROJECT_SOURCE_DIR\n    if(DEFINED Coverage_BASE_DIRECTORY)\n        get_filename_component(BASEDIR ${Coverage_BASE_DIRECTORY} ABSOLUTE)\n    else()\n        set(BASEDIR ${PROJECT_SOURCE_DIR})\n    endif()\n\n    # Collect excludes (CMake 3.4+: Also compute absolute paths)\n    set(LCOV_EXCLUDES \"\")\n    foreach(EXCLUDE ${Coverage_EXCLUDE} ${COVERAGE_EXCLUDES} ${COVERAGE_LCOV_EXCLUDES})\n        if(CMAKE_VERSION VERSION_GREATER 3.4)\n            get_filename_component(EXCLUDE ${EXCLUDE} ABSOLUTE BASE_DIR ${BASEDIR})\n        endif()\n        list(APPEND LCOV_EXCLUDES \"${EXCLUDE}\")\n    endforeach()\n    list(REMOVE_DUPLICATES LCOV_EXCLUDES)\n\n    # Conditional arguments\n    if(CPPFILT_PATH AND NOT ${Coverage_NO_DEMANGLE})\n      set(GENHTML_EXTRA_ARGS \"--demangle-cpp\")\n    endif()\n     \n    # Setting up commands which will be run to generate coverage data.\n    # Cleanup lcov\n    set(LCOV_CLEAN_CMD \n        ${LCOV_PATH} ${Coverage_LCOV_ARGS} --gcov-tool ${GCOV_PATH} -directory . \n        -b ${BASEDIR} --zerocounters\n    )\n    # Create baseline to make sure untouched files show up in the report\n    set(LCOV_BASELINE_CMD \n        ${LCOV_PATH} ${Coverage_LCOV_ARGS} --gcov-tool ${GCOV_PATH} -c -i -d . -b \n        ${BASEDIR} -o ${Coverage_NAME}.base\n    )\n    # Run tests\n    set(LCOV_EXEC_TESTS_CMD \n        ${Coverage_EXECUTABLE} ${Coverage_EXECUTABLE_ARGS}\n    )    \n    # Capturing lcov counters and generating report\n    set(LCOV_CAPTURE_CMD \n        ${LCOV_PATH} ${Coverage_LCOV_ARGS} --gcov-tool ${GCOV_PATH} --directory . -b \n        ${BASEDIR} --capture --output-file ${Coverage_NAME}.capture\n    )\n    # add baseline counters\n    set(LCOV_BASELINE_COUNT_CMD\n        ${LCOV_PATH} ${Coverage_LCOV_ARGS} --gcov-tool ${GCOV_PATH} -a ${Coverage_NAME}.base \n        -a ${Coverage_NAME}.capture --output-file ${Coverage_NAME}.total\n    ) \n    # filter collected data to final coverage report\n    set(LCOV_FILTER_CMD \n        ${LCOV_PATH} ${Coverage_LCOV_ARGS} --gcov-tool ${GCOV_PATH} --remove \n        ${Coverage_NAME}.total ${LCOV_EXCLUDES} --output-file ${Coverage_NAME}.info\n    )    \n    # Generate HTML output\n    set(LCOV_GEN_HTML_CMD\n        ${GENHTML_PATH} ${GENHTML_EXTRA_ARGS} ${Coverage_GENHTML_ARGS} -o \n        ${Coverage_NAME} ${Coverage_NAME}.info\n    )\n    \n\n    if(CODE_COVERAGE_VERBOSE)\n        message(STATUS \"Executed command report\")\n        message(STATUS \"Command to clean up lcov: \")\n        string(REPLACE \";\" \" \" LCOV_CLEAN_CMD_SPACED \"${LCOV_CLEAN_CMD}\")\n        message(STATUS \"${LCOV_CLEAN_CMD_SPACED}\")\n\n        message(STATUS \"Command to create baseline: \")\n        string(REPLACE \";\" \" \" LCOV_BASELINE_CMD_SPACED \"${LCOV_BASELINE_CMD}\")\n        message(STATUS \"${LCOV_BASELINE_CMD_SPACED}\")\n\n        message(STATUS \"Command to run the tests: \")\n        string(REPLACE \";\" \" \" LCOV_EXEC_TESTS_CMD_SPACED \"${LCOV_EXEC_TESTS_CMD}\")\n        message(STATUS \"${LCOV_EXEC_TESTS_CMD_SPACED}\")\n\n        message(STATUS \"Command to capture counters and generate report: \")\n        string(REPLACE \";\" \" \" LCOV_CAPTURE_CMD_SPACED \"${LCOV_CAPTURE_CMD}\")\n        message(STATUS \"${LCOV_CAPTURE_CMD_SPACED}\")\n\n        message(STATUS \"Command to add baseline counters: \")\n        string(REPLACE \";\" \" \" LCOV_BASELINE_COUNT_CMD_SPACED \"${LCOV_BASELINE_COUNT_CMD}\")\n        message(STATUS \"${LCOV_BASELINE_COUNT_CMD_SPACED}\")\n\n        message(STATUS \"Command to filter collected data: \")\n        string(REPLACE \";\" \" \" LCOV_FILTER_CMD_SPACED \"${LCOV_FILTER_CMD}\")\n        message(STATUS \"${LCOV_FILTER_CMD_SPACED}\")\n\n        message(STATUS \"Command to generate lcov HTML output: \")\n        string(REPLACE \";\" \" \" LCOV_GEN_HTML_CMD_SPACED \"${LCOV_GEN_HTML_CMD}\")\n        message(STATUS \"${LCOV_GEN_HTML_CMD_SPACED}\")\n    endif()\n\n    # Setup target\n    add_custom_target(${Coverage_NAME}\n        COMMAND ${LCOV_CLEAN_CMD}\n        COMMAND ${LCOV_BASELINE_CMD} \n        COMMAND ${LCOV_EXEC_TESTS_CMD}\n        COMMAND ${LCOV_CAPTURE_CMD}\n        COMMAND ${LCOV_BASELINE_COUNT_CMD}\n        COMMAND ${LCOV_FILTER_CMD} \n        COMMAND ${LCOV_GEN_HTML_CMD}\n\n        # Set output files as GENERATED (will be removed on 'make clean')\n        BYPRODUCTS\n            ${Coverage_NAME}.base\n            ${Coverage_NAME}.capture\n            ${Coverage_NAME}.total\n            ${Coverage_NAME}.info\n            ${Coverage_NAME}/index.html\n        WORKING_DIRECTORY ${PROJECT_BINARY_DIR}\n        DEPENDS ${Coverage_DEPENDENCIES}\n        VERBATIM # Protect arguments to commands\n        COMMENT \"Resetting code coverage counters to zero.\\nProcessing code coverage counters and generating report.\"\n    )\n\n    # Show where to find the lcov info report\n    add_custom_command(TARGET ${Coverage_NAME} POST_BUILD\n        COMMAND ;\n        COMMENT \"Lcov code coverage info report saved in ${Coverage_NAME}.info.\"\n    )\n\n    # Show info where to find the report\n    add_custom_command(TARGET ${Coverage_NAME} POST_BUILD\n        COMMAND ;\n        COMMENT \"Open ./${Coverage_NAME}/index.html in your browser to view the coverage report.\"\n    )\n\nendfunction() # setup_target_for_coverage_lcov\n\n# Defines a target for running and collection code coverage information\n# Builds dependencies, runs the given executable and outputs reports.\n# NOTE! The executable should always have a ZERO as exit code otherwise\n# the coverage generation will not complete.\n#\n# setup_target_for_coverage_gcovr_xml(\n#     NAME ctest_coverage                    # New target name\n#     EXECUTABLE ctest -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR\n#     DEPENDENCIES executable_target         # Dependencies to build first\n#     BASE_DIRECTORY \"../\"                   # Base directory for report\n#                                            #  (defaults to PROJECT_SOURCE_DIR)\n#     EXCLUDE \"src/dir1/*\" \"src/dir2/*\"      # Patterns to exclude (can be relative\n#                                            #  to BASE_DIRECTORY, with CMake 3.4+)\n# )\n# The user can set the variable GCOVR_ADDITIONAL_ARGS to supply additional flags to the\n# GCVOR command.\nfunction(setup_target_for_coverage_gcovr_xml)\n\n    set(options NONE)\n    set(oneValueArgs BASE_DIRECTORY NAME)\n    set(multiValueArgs EXCLUDE EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES)\n    cmake_parse_arguments(Coverage \"${options}\" \"${oneValueArgs}\" \"${multiValueArgs}\" ${ARGN})\n\n    if(NOT GCOVR_PATH)\n        message(FATAL_ERROR \"gcovr not found! Aborting...\")\n    endif() # NOT GCOVR_PATH\n\n    # Set base directory (as absolute path), or default to PROJECT_SOURCE_DIR\n    if(DEFINED Coverage_BASE_DIRECTORY)\n        get_filename_component(BASEDIR ${Coverage_BASE_DIRECTORY} ABSOLUTE)\n    else()\n        set(BASEDIR ${PROJECT_SOURCE_DIR})\n    endif()\n\n    # Collect excludes (CMake 3.4+: Also compute absolute paths)\n    set(GCOVR_EXCLUDES \"\")\n    foreach(EXCLUDE ${Coverage_EXCLUDE} ${COVERAGE_EXCLUDES} ${COVERAGE_GCOVR_EXCLUDES})\n        if(CMAKE_VERSION VERSION_GREATER 3.4)\n            get_filename_component(EXCLUDE ${EXCLUDE} ABSOLUTE BASE_DIR ${BASEDIR})\n        endif()\n        list(APPEND GCOVR_EXCLUDES \"${EXCLUDE}\")\n    endforeach()\n    list(REMOVE_DUPLICATES GCOVR_EXCLUDES)\n\n    # Combine excludes to several -e arguments\n    set(GCOVR_EXCLUDE_ARGS \"\")\n    foreach(EXCLUDE ${GCOVR_EXCLUDES})\n        list(APPEND GCOVR_EXCLUDE_ARGS \"-e\")\n        list(APPEND GCOVR_EXCLUDE_ARGS \"${EXCLUDE}\")\n    endforeach()\n    \n    # Set up commands which will be run to generate coverage data\n    # Run tests\n    set(GCOVR_XML_EXEC_TESTS_CMD\n        ${Coverage_EXECUTABLE} ${Coverage_EXECUTABLE_ARGS}\n    )\n    # Running gcovr\n    set(GCOVR_XML_CMD\n        ${GCOVR_PATH} --xml -r ${BASEDIR} ${GCOVR_ADDITIONAL_ARGS} ${GCOVR_EXCLUDE_ARGS} \n        --object-directory=${PROJECT_BINARY_DIR} -o ${Coverage_NAME}.xml\n    )\n    \n    if(CODE_COVERAGE_VERBOSE)\n        message(STATUS \"Executed command report\")\n\n        message(STATUS \"Command to run tests: \")\n        string(REPLACE \";\" \" \" GCOVR_XML_EXEC_TESTS_CMD_SPACED \"${GCOVR_XML_EXEC_TESTS_CMD}\")\n        message(STATUS \"${GCOVR_XML_EXEC_TESTS_CMD_SPACED}\")\n\n        message(STATUS \"Command to generate gcovr XML coverage data: \")\n        string(REPLACE \";\" \" \" GCOVR_XML_CMD_SPACED \"${GCOVR_XML_CMD}\")\n        message(STATUS \"${GCOVR_XML_CMD_SPACED}\")\n    endif()\n\n    add_custom_target(${Coverage_NAME}\n        COMMAND ${GCOVR_XML_EXEC_TESTS_CMD}\n        COMMAND ${GCOVR_XML_CMD}\n        \n        BYPRODUCTS ${Coverage_NAME}.xml\n        WORKING_DIRECTORY ${PROJECT_BINARY_DIR}\n        DEPENDS ${Coverage_DEPENDENCIES}\n        VERBATIM # Protect arguments to commands\n        COMMENT \"Running gcovr to produce Cobertura code coverage report.\"\n    )\n\n    # Show info where to find the report\n    add_custom_command(TARGET ${Coverage_NAME} POST_BUILD\n        COMMAND ;\n        COMMENT \"Cobertura code coverage report saved in ${Coverage_NAME}.xml.\"\n    )\nendfunction() # setup_target_for_coverage_gcovr_xml\n\n# Defines a target for running and collection code coverage information\n# Builds dependencies, runs the given executable and outputs reports.\n# NOTE! The executable should always have a ZERO as exit code otherwise\n# the coverage generation will not complete.\n#\n# setup_target_for_coverage_gcovr_html(\n#     NAME ctest_coverage                    # New target name\n#     EXECUTABLE ctest -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR\n#     DEPENDENCIES executable_target         # Dependencies to build first\n#     BASE_DIRECTORY \"../\"                   # Base directory for report\n#                                            #  (defaults to PROJECT_SOURCE_DIR)\n#     EXCLUDE \"src/dir1/*\" \"src/dir2/*\"      # Patterns to exclude (can be relative\n#                                            #  to BASE_DIRECTORY, with CMake 3.4+)\n# )\n# The user can set the variable GCOVR_ADDITIONAL_ARGS to supply additional flags to the\n# GCVOR command.\nfunction(setup_target_for_coverage_gcovr_html)\n\n    set(options NONE)\n    set(oneValueArgs BASE_DIRECTORY NAME)\n    set(multiValueArgs EXCLUDE EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES)\n    cmake_parse_arguments(Coverage \"${options}\" \"${oneValueArgs}\" \"${multiValueArgs}\" ${ARGN})\n\n    if(NOT GCOVR_PATH)\n        message(FATAL_ERROR \"gcovr not found! Aborting...\")\n    endif() # NOT GCOVR_PATH\n\n    # Set base directory (as absolute path), or default to PROJECT_SOURCE_DIR\n    if(DEFINED Coverage_BASE_DIRECTORY)\n        get_filename_component(BASEDIR ${Coverage_BASE_DIRECTORY} ABSOLUTE)\n    else()\n        set(BASEDIR ${PROJECT_SOURCE_DIR})\n    endif()\n\n    # Collect excludes (CMake 3.4+: Also compute absolute paths)\n    set(GCOVR_EXCLUDES \"\")\n    foreach(EXCLUDE ${Coverage_EXCLUDE} ${COVERAGE_EXCLUDES} ${COVERAGE_GCOVR_EXCLUDES})\n        if(CMAKE_VERSION VERSION_GREATER 3.4)\n            get_filename_component(EXCLUDE ${EXCLUDE} ABSOLUTE BASE_DIR ${BASEDIR})\n        endif()\n        list(APPEND GCOVR_EXCLUDES \"${EXCLUDE}\")\n    endforeach()\n    list(REMOVE_DUPLICATES GCOVR_EXCLUDES)\n\n    # Combine excludes to several -e arguments\n    set(GCOVR_EXCLUDE_ARGS \"\")\n    foreach(EXCLUDE ${GCOVR_EXCLUDES})\n        list(APPEND GCOVR_EXCLUDE_ARGS \"-e\")\n        list(APPEND GCOVR_EXCLUDE_ARGS \"${EXCLUDE}\")\n    endforeach()\n\n    # Set up commands which will be run to generate coverage data\n    # Run tests\n    set(GCOVR_HTML_EXEC_TESTS_CMD\n        ${Coverage_EXECUTABLE} ${Coverage_EXECUTABLE_ARGS}\n    )\n    # Create folder\n    set(GCOVR_HTML_FOLDER_CMD\n        ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}/${Coverage_NAME}\n    )\n    # Running gcovr\n    set(GCOVR_HTML_CMD\n        ${GCOVR_PATH} --html --html-details -r ${BASEDIR} ${GCOVR_ADDITIONAL_ARGS}\n        ${GCOVR_EXCLUDE_ARGS} --object-directory=${PROJECT_BINARY_DIR} \n        -o ${Coverage_NAME}/index.html\n    )\n\n    if(CODE_COVERAGE_VERBOSE)\n        message(STATUS \"Executed command report\")\n\n        message(STATUS \"Command to run tests: \")\n        string(REPLACE \";\" \" \" GCOVR_HTML_EXEC_TESTS_CMD_SPACED \"${GCOVR_HTML_EXEC_TESTS_CMD}\")\n        message(STATUS \"${GCOVR_HTML_EXEC_TESTS_CMD_SPACED}\")\n\n        message(STATUS \"Command to create a folder: \")\n        string(REPLACE \";\" \" \" GCOVR_HTML_FOLDER_CMD_SPACED \"${GCOVR_HTML_FOLDER_CMD}\")\n        message(STATUS \"${GCOVR_HTML_FOLDER_CMD_SPACED}\")\n\n        message(STATUS \"Command to generate gcovr HTML coverage data: \")\n        string(REPLACE \";\" \" \" GCOVR_HTML_CMD_SPACED \"${GCOVR_HTML_CMD}\")\n        message(STATUS \"${GCOVR_HTML_CMD_SPACED}\")\n    endif()\n\n    add_custom_target(${Coverage_NAME}\n        COMMAND ${GCOVR_HTML_EXEC_TESTS_CMD}\n        COMMAND ${GCOVR_HTML_FOLDER_CMD}\n        COMMAND ${GCOVR_HTML_CMD}\n\n        BYPRODUCTS ${PROJECT_BINARY_DIR}/${Coverage_NAME}/index.html  # report directory\n        WORKING_DIRECTORY ${PROJECT_BINARY_DIR}\n        DEPENDS ${Coverage_DEPENDENCIES}\n        VERBATIM # Protect arguments to commands\n        COMMENT \"Running gcovr to produce HTML code coverage report.\"\n    )\n\n    # Show info where to find the report\n    add_custom_command(TARGET ${Coverage_NAME} POST_BUILD\n        COMMAND ;\n        COMMENT \"Open ./${Coverage_NAME}/index.html in your browser to view the coverage report.\"\n    )\n\nendfunction() # setup_target_for_coverage_gcovr_html\n\n# Defines a target for running and collection code coverage information\n# Builds dependencies, runs the given executable and outputs reports.\n# NOTE! The executable should always have a ZERO as exit code otherwise\n# the coverage generation will not complete.\n#\n# setup_target_for_coverage_fastcov(\n#     NAME testrunner_coverage                    # New target name\n#     EXECUTABLE testrunner -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR\n#     DEPENDENCIES testrunner                     # Dependencies to build first\n#     BASE_DIRECTORY \"../\"                        # Base directory for report\n#                                                 #  (defaults to PROJECT_SOURCE_DIR)\n#     EXCLUDE \"src/dir1/\" \"src/dir2/\"             # Patterns to exclude.\n#     NO_DEMANGLE                                 # Don't demangle C++ symbols\n#                                                 #  even if c++filt is found\n#     SKIP_HTML                                   # Don't create html report\n#     POST_CMD perl -i -pe s!${PROJECT_SOURCE_DIR}/!!g ctest_coverage.json  # E.g. for stripping source dir from file paths\n# )\nfunction(setup_target_for_coverage_fastcov)\n\n    set(options NO_DEMANGLE SKIP_HTML)\n    set(oneValueArgs BASE_DIRECTORY NAME)\n    set(multiValueArgs EXCLUDE EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES FASTCOV_ARGS GENHTML_ARGS POST_CMD)\n    cmake_parse_arguments(Coverage \"${options}\" \"${oneValueArgs}\" \"${multiValueArgs}\" ${ARGN})\n\n    if(NOT FASTCOV_PATH)\n        message(FATAL_ERROR \"fastcov not found! Aborting...\")\n    endif()\n\n    if(NOT Coverage_SKIP_HTML AND NOT GENHTML_PATH)\n        message(FATAL_ERROR \"genhtml not found! Aborting...\")\n    endif()\n\n    # Set base directory (as absolute path), or default to PROJECT_SOURCE_DIR\n    if(Coverage_BASE_DIRECTORY)\n        get_filename_component(BASEDIR ${Coverage_BASE_DIRECTORY} ABSOLUTE)\n    else()\n        set(BASEDIR ${PROJECT_SOURCE_DIR})\n    endif()\n\n    # Collect excludes (Patterns, not paths, for fastcov)\n    set(FASTCOV_EXCLUDES \"\")\n    foreach(EXCLUDE ${Coverage_EXCLUDE} ${COVERAGE_EXCLUDES} ${COVERAGE_FASTCOV_EXCLUDES})\n        list(APPEND FASTCOV_EXCLUDES \"${EXCLUDE}\")\n    endforeach()\n    list(REMOVE_DUPLICATES FASTCOV_EXCLUDES)\n\n    # Conditional arguments\n    if(CPPFILT_PATH AND NOT ${Coverage_NO_DEMANGLE})\n        set(GENHTML_EXTRA_ARGS \"--demangle-cpp\")\n    endif()\n\n    # Set up commands which will be run to generate coverage data\n    set(FASTCOV_EXEC_TESTS_CMD ${Coverage_EXECUTABLE} ${Coverage_EXECUTABLE_ARGS})\n\n    set(FASTCOV_CAPTURE_CMD ${FASTCOV_PATH} ${Coverage_FASTCOV_ARGS} --gcov ${GCOV_PATH}\n        --search-directory ${BASEDIR}\n        --process-gcno\n        --output ${Coverage_NAME}.json\n        --exclude ${FASTCOV_EXCLUDES}\n        --exclude ${FASTCOV_EXCLUDES}\n    )\n    \n    set(FASTCOV_CONVERT_CMD ${FASTCOV_PATH}\n        -C ${Coverage_NAME}.json --lcov --output ${Coverage_NAME}.info\n    )\n\n    if(Coverage_SKIP_HTML)\n        set(FASTCOV_HTML_CMD \";\")\n    else()\n        set(FASTCOV_HTML_CMD ${GENHTML_PATH} ${GENHTML_EXTRA_ARGS} ${Coverage_GENHTML_ARGS}\n            -o ${Coverage_NAME} ${Coverage_NAME}.info\n        )\n    endif()\n\n    set(FASTCOV_POST_CMD \";\")\n    if(Coverage_POST_CMD)\n        set(FASTCOV_POST_CMD ${Coverage_POST_CMD})\n    endif()\n\n    if(CODE_COVERAGE_VERBOSE)\n        message(STATUS \"Code coverage commands for target ${Coverage_NAME} (fastcov):\")\n\n        message(\"   Running tests:\")\n        string(REPLACE \";\" \" \" FASTCOV_EXEC_TESTS_CMD_SPACED \"${FASTCOV_EXEC_TESTS_CMD}\")\n        message(\"     ${FASTCOV_EXEC_TESTS_CMD_SPACED}\")\n\n        message(\"   Capturing fastcov counters and generating report:\")\n        string(REPLACE \";\" \" \" FASTCOV_CAPTURE_CMD_SPACED \"${FASTCOV_CAPTURE_CMD}\")\n        message(\"     ${FASTCOV_CAPTURE_CMD_SPACED}\")\n\n        message(\"   Converting fastcov .json to lcov .info:\")\n        string(REPLACE \";\" \" \" FASTCOV_CONVERT_CMD_SPACED \"${FASTCOV_CONVERT_CMD}\")\n        message(\"     ${FASTCOV_CONVERT_CMD_SPACED}\")\n\n        if(NOT Coverage_SKIP_HTML)\n            message(\"   Generating HTML report: \")\n            string(REPLACE \";\" \" \" FASTCOV_HTML_CMD_SPACED \"${FASTCOV_HTML_CMD}\")\n            message(\"     ${FASTCOV_HTML_CMD_SPACED}\")\n        endif()\n        if(Coverage_POST_CMD)\n            message(\"   Running post command: \")\n            string(REPLACE \";\" \" \" FASTCOV_POST_CMD_SPACED \"${FASTCOV_POST_CMD}\")\n            message(\"     ${FASTCOV_POST_CMD_SPACED}\")\n        endif()\n    endif()\n\n    # Setup target\n    add_custom_target(${Coverage_NAME}\n\n        # Cleanup fastcov\n        COMMAND ${FASTCOV_PATH} ${Coverage_FASTCOV_ARGS} --gcov ${GCOV_PATH}\n            --search-directory ${BASEDIR}\n            --zerocounters\n\n        COMMAND ${FASTCOV_EXEC_TESTS_CMD}\n        COMMAND ${FASTCOV_CAPTURE_CMD}\n        COMMAND ${FASTCOV_CONVERT_CMD}\n        COMMAND ${FASTCOV_HTML_CMD}\n        COMMAND ${FASTCOV_POST_CMD}\n\n        # Set output files as GENERATED (will be removed on 'make clean')\n        BYPRODUCTS\n             ${Coverage_NAME}.info\n             ${Coverage_NAME}.json\n             ${Coverage_NAME}/index.html  # report directory\n\n        WORKING_DIRECTORY ${PROJECT_BINARY_DIR}\n        DEPENDS ${Coverage_DEPENDENCIES}\n        VERBATIM # Protect arguments to commands\n        COMMENT \"Resetting code coverage counters to zero. Processing code coverage counters and generating report.\"\n    )\n\n    set(INFO_MSG \"fastcov code coverage info report saved in ${Coverage_NAME}.info and ${Coverage_NAME}.json.\")\n    if(NOT Coverage_SKIP_HTML)\n        string(APPEND INFO_MSG \" Open ${PROJECT_BINARY_DIR}/${Coverage_NAME}/index.html in your browser to view the coverage report.\")\n    endif()\n    # Show where to find the fastcov info report\n    add_custom_command(TARGET ${Coverage_NAME} POST_BUILD\n        COMMAND ${CMAKE_COMMAND} -E echo ${INFO_MSG}\n    )\n\nendfunction() # setup_target_for_coverage_fastcov\n\nfunction(append_coverage_compiler_flags)\n    set(CMAKE_C_FLAGS \"${CMAKE_C_FLAGS} ${COVERAGE_COMPILER_FLAGS}\" PARENT_SCOPE)\n    set(CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS} ${COVERAGE_COMPILER_FLAGS}\" PARENT_SCOPE)\n    set(CMAKE_Fortran_FLAGS \"${CMAKE_Fortran_FLAGS} ${COVERAGE_COMPILER_FLAGS}\" PARENT_SCOPE)\n    message(STATUS \"Appending code coverage compiler flags: ${COVERAGE_COMPILER_FLAGS}\")\nendfunction() # append_coverage_compiler_flags\n"
  },
  {
    "path": "deps/cpp-statsd-client/cmake/Config.cmake.in",
    "content": "# Copyright (c) 2016, Ruslan Baratov\n#\n# Licensed under the MIT License (the \"License\"); you may not use this file except\n# in compliance with the License. You may obtain a copy of the License at\n#\n# http://opensource.org/licenses/MIT\n#\n# Unless required by applicable law or agreed to in writing, software distributed\n# under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR\n# CONDITIONS OF ANY KIND, either express or implied. See the License for the\n# specific language governing permissions and limitations under the License.\n\n@PACKAGE_INIT@\n\nfind_package(Threads REQUIRED)\n\ninclude(\"${CMAKE_CURRENT_LIST_DIR}/@TARGETS_EXPORT_NAME@.cmake\")\ncheck_required_components(\"@PROJECT_NAME@\")\n"
  },
  {
    "path": "deps/cpp-statsd-client/cmake/cpp-statsd-clientConfig.cmake.in",
    "content": "@PACKAGE_INIT@\n\ninclude(\"${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake\")\ncheck_required_components(\"@PROJECT_NAME@\")\n"
  },
  {
    "path": "deps/cpp-statsd-client/include/cpp-statsd-client/StatsdClient.hpp",
    "content": "#ifndef STATSD_CLIENT_HPP\n#define STATSD_CLIENT_HPP\n\n#include <cpp-statsd-client/UDPSender.hpp>\n#include <cstdint>\n#include <cstdio>\n#include <iomanip>\n#include <memory>\n#include <random>\n#include <sstream>\n#include <string>\n#include <vector>\n\nnamespace Statsd {\n\n/*!\n *\n * Statsd client\n *\n * This is the Statsd client, exposing the classic methods\n * and relying on a UDP sender for the actual sending.\n *\n * The prefix for a stat is provided once at construction or\n * on reconfiguring the client. The separator character '.'\n * is automatically inserted between the prefix and the stats\n * key, therefore you should neither append one to the prefix\n * nor prepend one to the key\n *\n * The sampling frequency is specified per call and uses a\n * random number generator to determine whether or not the stat\n * will be recorded this time or not.\n *\n * The top level configuration includes 2 optional parameters\n * that determine how the stats are delivered to statsd. These\n * parameters are the batching size and the send interval.\n *\n * The batching size controls the number of bytes to send\n * in each UDP datagram to statsd. This is not a hard limit as\n * we continue appending to a batch of stats until the limit\n * has been reached or surpassed. When this occurs we add the\n * batch to a queue and create a new batch to appended to. A\n * value of 0 for the batching size will disable batching such\n * that each stat will be sent to the daemon individually.\n *\n * The send interval controls the rate at which queued batches\n * of stats will be sent to statsd. If batching is disabled,\n * this value is ignored and each individual stat is sent to\n * statsd immediately in a blocking fashion. If batching is\n * enabled (ie. non-zero) then the send interval is the number\n * of milliseconds to wait before flushing the queue of\n * batched stats messages to the daemon. This is done in a non-\n * blocking fashion via a background thread. If the send\n * interval is 0 then the stats messages are appended to a\n * queue until the caller manually flushes the queue via the\n * flush method.\n *\n */\nclass StatsdClient {\npublic:\n    //!@name Constructor and destructor, non-copyable\n    //!@{\n\n    //! Constructor\n    StatsdClient(const std::string& host,\n                 const uint16_t port,\n                 const std::string& prefix,\n                 const uint64_t batchsize = 0,\n                 const uint64_t sendInterval = 1000,\n                 const unsigned int gaugePrecision = 4) noexcept;\n\n    StatsdClient(const StatsdClient&) = delete;\n    StatsdClient& operator=(const StatsdClient&) = delete;\n\n    //!@}\n\n    //!@name Methods\n    //!@{\n\n    //! Sets a configuration { host, port, prefix, batchsize }\n    void setConfig(const std::string& host,\n                   const uint16_t port,\n                   const std::string& prefix,\n                   const uint64_t batchsize = 0,\n                   const uint64_t sendInterval = 1000,\n                   const unsigned int gaugePrecision = 4) noexcept;\n\n    //! Returns the error message as an std::string\n    const std::string& errorMessage() const noexcept;\n\n    //! Increments the key, at a given frequency rate\n    void increment(const std::string& key,\n                   float frequency = 1.0f,\n                   const std::vector<std::string>& tags = {}) const noexcept;\n\n    //! Increments the key, at a given frequency rate\n    void decrement(const std::string& key,\n                   float frequency = 1.0f,\n                   const std::vector<std::string>& tags = {}) const noexcept;\n\n    //! Adjusts the specified key by a given delta, at a given frequency rate\n    void count(const std::string& key,\n               const int delta,\n               float frequency = 1.0f,\n               const std::vector<std::string>& tags = {}) const noexcept;\n\n    //! Records a gauge for the key, with a given value, at a given frequency rate\n    template <typename T>\n    void gauge(const std::string& key,\n               const T value,\n               float frequency = 1.0f,\n               const std::vector<std::string>& tags = {}) const noexcept;\n\n    //! Records a timing for a key, at a given frequency\n    void timing(const std::string& key,\n                const unsigned int ms,\n                float frequency = 1.0f,\n                const std::vector<std::string>& tags = {}) const noexcept;\n\n    //! Records a count of unique occurrences for a key, at a given frequency\n    void set(const std::string& key,\n             const unsigned int sum,\n             float frequency = 1.0f,\n             const std::vector<std::string>& tags = {}) const noexcept;\n\n    //! Seed the RNG that controls sampling\n    void seed(unsigned int seed = std::random_device()()) noexcept;\n\n    //! Flush any queued stats to the daemon\n    void flush() noexcept;\n\n    //!@}\n\nprivate:\n    // @name Private methods\n    // @{\n\n    //! Send a value for a key, according to its type, at a given frequency\n    template <typename T>\n    void send(const std::string& key,\n              const T value,\n              const char* type,\n              float frequency,\n              const std::vector<std::string>& tags) const noexcept;\n\n    //!@}\n\nprivate:\n    //! The prefix to be used for metrics\n    std::string m_prefix;\n\n    //! The UDP sender to be used for actual sending\n    std::unique_ptr<UDPSender> m_sender;\n\n    //! The random number generator for handling sampling\n    mutable std::mt19937 m_randomEngine;\n\n    //! The buffer string format our stats before sending them\n    mutable std::string m_buffer;\n\n    //! Fixed floating point precision of gauges\n    unsigned int m_gaugePrecision;\n};\n\nnamespace detail {\ninline std::string sanitizePrefix(std::string prefix) {\n    // For convenience we provide the dot when generating the stat message\n    if (!prefix.empty() && prefix.back() == '.') {\n        prefix.pop_back();\n    }\n    return prefix;\n}\n\n// All supported metric types\nconstexpr char METRIC_TYPE_COUNT[] = \"c\";\nconstexpr char METRIC_TYPE_GAUGE[] = \"g\";\nconstexpr char METRIC_TYPE_TIMING[] = \"ms\";\nconstexpr char METRIC_TYPE_SET[] = \"s\";\n}  // namespace detail\n\ninline StatsdClient::StatsdClient(const std::string& host,\n                                  const uint16_t port,\n                                  const std::string& prefix,\n                                  const uint64_t batchsize,\n                                  const uint64_t sendInterval,\n                                  const unsigned int gaugePrecision) noexcept\n    : m_prefix(detail::sanitizePrefix(prefix)),\n      m_sender(new UDPSender{host, port, batchsize, sendInterval}),\n      m_gaugePrecision(gaugePrecision) {\n    // Initialize the random generator to be used for sampling\n    seed();\n    // Avoid re-allocations by reserving a generous buffer\n    m_buffer.reserve(256);\n}\n\ninline void StatsdClient::setConfig(const std::string& host,\n                                    const uint16_t port,\n                                    const std::string& prefix,\n                                    const uint64_t batchsize,\n                                    const uint64_t sendInterval,\n                                    const unsigned int gaugePrecision) noexcept {\n    m_prefix = detail::sanitizePrefix(prefix);\n    m_sender.reset(new UDPSender(host, port, batchsize, sendInterval));\n    m_gaugePrecision = gaugePrecision;\n}\n\ninline const std::string& StatsdClient::errorMessage() const noexcept {\n    return m_sender->errorMessage();\n}\n\ninline void StatsdClient::decrement(const std::string& key,\n                                    float frequency,\n                                    const std::vector<std::string>& tags) const noexcept {\n    count(key, -1, frequency, tags);\n}\n\ninline void StatsdClient::increment(const std::string& key,\n                                    float frequency,\n                                    const std::vector<std::string>& tags) const noexcept {\n    count(key, 1, frequency, tags);\n}\n\ninline void StatsdClient::count(const std::string& key,\n                                const int delta,\n                                float frequency,\n                                const std::vector<std::string>& tags) const noexcept {\n    send(key, delta, detail::METRIC_TYPE_COUNT, frequency, tags);\n}\n\ntemplate <typename T>\ninline void StatsdClient::gauge(const std::string& key,\n                                const T value,\n                                const float frequency,\n                                const std::vector<std::string>& tags) const noexcept {\n    send(key, value, detail::METRIC_TYPE_GAUGE, frequency, tags);\n}\n\ninline void StatsdClient::timing(const std::string& key,\n                                 const unsigned int ms,\n                                 float frequency,\n                                 const std::vector<std::string>& tags) const noexcept {\n    send(key, ms, detail::METRIC_TYPE_TIMING, frequency, tags);\n}\n\ninline void StatsdClient::set(const std::string& key,\n                              const unsigned int sum,\n                              float frequency,\n                              const std::vector<std::string>& tags) const noexcept {\n    send(key, sum, detail::METRIC_TYPE_SET, frequency, tags);\n}\n\ntemplate <typename T>\ninline void StatsdClient::send(const std::string& key,\n                               const T value,\n                               const char* type,\n                               float frequency,\n                               const std::vector<std::string>& tags) const noexcept {\n    // Bail if we can't send anything anyway\n    if (!m_sender->initialized()) {\n        return;\n    }\n\n    // A valid frequency is: 0 <= f <= 1\n    // At 0 you never emit the stat, at 1 you always emit the stat and with anything else you roll the dice\n    frequency = std::max(std::min(frequency, 1.f), 0.f);\n    constexpr float epsilon{0.0001f};\n    const bool isFrequencyOne = std::fabs(frequency - 1.0f) < epsilon;\n    const bool isFrequencyZero = std::fabs(frequency) < epsilon;\n    if (isFrequencyZero ||\n        (!isFrequencyOne && (frequency < std::uniform_real_distribution<float>(0.f, 1.f)(m_randomEngine)))) {\n        return;\n    }\n\n    // Format the stat message\n    std::stringstream valueStream;\n    valueStream << std::fixed << std::setprecision(m_gaugePrecision) << value;\n\n    m_buffer.clear();\n\n    m_buffer.append(m_prefix);\n    if (!m_prefix.empty() && !key.empty()) {\n        m_buffer.push_back('.');\n    }\n\n    m_buffer.append(key);\n    m_buffer.push_back(':');\n    m_buffer.append(valueStream.str());\n    m_buffer.push_back('|');\n    m_buffer.append(type);\n\n    if (frequency < 1.f) {\n        m_buffer.append(\"|@0.\");\n        m_buffer.append(std::to_string(static_cast<int>(frequency * 100)));\n    }\n\n    if (!tags.empty()) {\n        m_buffer.append(\"|#\");\n        for (const auto& tag : tags) {\n            m_buffer.append(tag);\n            m_buffer.push_back(',');\n        }\n        m_buffer.pop_back();\n    }\n\n    // Send the message via the UDP sender\n    m_sender->send(m_buffer);\n}\n\ninline void StatsdClient::seed(unsigned int seed) noexcept {\n    m_randomEngine.seed(seed);\n}\n\ninline void StatsdClient::flush() noexcept {\n    m_sender->flush();\n}\n\n}  // namespace Statsd\n\n#endif\n"
  },
  {
    "path": "deps/cpp-statsd-client/include/cpp-statsd-client/UDPSender.hpp",
    "content": "#ifndef UDP_SENDER_HPP\n#define UDP_SENDER_HPP\n\n#ifdef _WIN32\n#define NOMINMAX\n#include <io.h>\n#include <winsock2.h>\n#include <ws2tcpip.h>\n#else\n#include <arpa/inet.h>\n#include <netdb.h>\n#include <sys/socket.h>\n#include <unistd.h>\n#endif\n\n#include <atomic>\n#include <cmath>\n#include <cstdint>\n#include <cstring>\n#include <deque>\n#include <mutex>\n#include <string>\n#include <thread>\n\nnamespace Statsd {\n\n#ifdef _WIN32\nusing SOCKET_TYPE = SOCKET;\nconstexpr SOCKET_TYPE k_invalidSocket{INVALID_SOCKET};\n#define SOCKET_ERRNO WSAGetLastError()\n#define SOCKET_CLOSE closesocket\n#else\nusing SOCKET_TYPE = int;\nconstexpr SOCKET_TYPE k_invalidSocket{-1};\n#define SOCKET_ERRNO errno\n#define SOCKET_CLOSE close\n#endif\n\n/*!\n *\n * UDP sender\n *\n * A simple UDP sender handling batching.\n *\n */\nclass UDPSender final {\npublic:\n    //!@name Constructor and destructor, non-copyable\n    //!@{\n\n    //! Constructor\n    UDPSender(const std::string& host,\n              const uint16_t port,\n              const uint64_t batchsize,\n              const uint64_t sendInterval) noexcept;\n\n    //! Destructor\n    ~UDPSender();\n\n    UDPSender(const UDPSender&) = delete;\n    UDPSender& operator=(const UDPSender&) = delete;\n    UDPSender(UDPSender&&) = delete;\n\n    //!@}\n\n    //!@name Methods\n    //!@{\n\n    //! Send or enqueue a message\n    void send(const std::string& message) noexcept;\n\n    //! Returns the error message as a string\n    const std::string& errorMessage() const noexcept;\n\n    //! Returns true if the sender is initialized\n    bool initialized() const noexcept;\n\n    //! Flushes any queued messages\n    void flush() noexcept;\n\n    //!@}\n\nprivate:\n    // @name Private methods\n    // @{\n\n    //! Initialize the sender and returns true when it is initialized\n    bool initialize() noexcept;\n\n    //! Queue a message to be sent to the daemon later\n    inline void queueMessage(const std::string& message) noexcept;\n\n    //! Send a message to the daemon\n    void sendToDaemon(const std::string& message) noexcept;\n\n    //!@}\n\nprivate:\n    // @name State variables\n    // @{\n\n    //! Shall we exit?\n    std::atomic<bool> m_mustExit{false};\n\n    //!@}\n\n    // @name Network info\n    // @{\n\n    //! The hostname\n    std::string m_host;\n\n    //! The port\n    uint16_t m_port;\n\n    //! The structure holding the server\n    struct sockaddr_in m_server;\n\n    //! The socket to be used\n    SOCKET_TYPE m_socket = k_invalidSocket;\n\n    //!@}\n\n    // @name Batching info\n    // @{\n\n    //! The batching size\n    uint64_t m_batchsize;\n\n    //! The sending frequency in milliseconds\n    uint64_t m_sendInterval;\n\n    //! The queue batching the messages\n    std::deque<std::string> m_batchingMessageQueue;\n\n    //! The mutex used for batching\n    std::mutex m_batchingMutex;\n\n    //! The thread dedicated to the batching\n    std::thread m_batchingThread;\n\n    //!@}\n\n    //! Error message (optional string)\n    std::string m_errorMessage;\n};\n\nnamespace detail {\n\ninline bool isValidSocket(const SOCKET_TYPE socket) {\n    return socket != k_invalidSocket;\n}\n\n#ifdef _WIN32\nstruct WinSockSingleton {\n    inline static const WinSockSingleton& getInstance() {\n        static const WinSockSingleton instance;\n        return instance;\n    }\n    inline bool ok() const {\n        return m_ok;\n    }\n    ~WinSockSingleton() {\n        WSACleanup();\n    }\n\nprivate:\n    WinSockSingleton() {\n        WSADATA wsa;\n        m_ok = WSAStartup(MAKEWORD(2, 2), &wsa) == 0;\n    }\n    bool m_ok;\n};\n#endif\n\n}  // namespace detail\n\ninline UDPSender::UDPSender(const std::string& host,\n                            const uint16_t port,\n                            const uint64_t batchsize,\n                            const uint64_t sendInterval) noexcept\n    : m_host(host), m_port(port), m_batchsize(batchsize), m_sendInterval(sendInterval) {\n    // Initialize the socket\n    if (!initialize()) {\n        return;\n    }\n\n    // If batching is on, use a dedicated thread to send after the wait time is reached\n    if (m_batchsize != 0 && m_sendInterval > 0) {\n        // Define the batching thread\n        m_batchingThread = std::thread([this] {\n            // TODO: this will drop unsent stats, should we send all the unsent stats before we exit?\n            while (!m_mustExit.load(std::memory_order_acquire)) {\n                std::deque<std::string> stagedMessageQueue;\n\n                std::unique_lock<std::mutex> batchingLock(m_batchingMutex);\n                m_batchingMessageQueue.swap(stagedMessageQueue);\n                batchingLock.unlock();\n\n                // Flush the queue\n                while (!stagedMessageQueue.empty()) {\n                    sendToDaemon(stagedMessageQueue.front());\n                    stagedMessageQueue.pop_front();\n                }\n\n                // Wait before sending the next batch\n                std::this_thread::sleep_for(std::chrono::milliseconds(m_sendInterval));\n            }\n        });\n    }\n}\n\ninline UDPSender::~UDPSender() {\n    if (!initialized()) {\n        return;\n    }\n\n    // If we're running a background thread tell it to stop\n    if (m_batchingThread.joinable()) {\n        m_mustExit.store(true, std::memory_order_release);\n        m_batchingThread.join();\n    }\n\n    // Cleanup the socket\n    SOCKET_CLOSE(m_socket);\n}\n\ninline void UDPSender::send(const std::string& message) noexcept {\n    m_errorMessage.clear();\n\n    // If batching is on, accumulate messages in the queue\n    if (m_batchsize > 0) {\n        queueMessage(message);\n        return;\n    }\n\n    // Or send it right now\n    sendToDaemon(message);\n}\n\ninline void UDPSender::queueMessage(const std::string& message) noexcept {\n    // We aquire a lock but only if we actually need to (i.e. there is a thread also accessing the queue)\n    auto batchingLock =\n        m_batchingThread.joinable() ? std::unique_lock<std::mutex>(m_batchingMutex) : std::unique_lock<std::mutex>();\n    // Either we don't have a place to batch our message or we exceeded the batch size, so make a new batch\n    if (m_batchingMessageQueue.empty() || m_batchingMessageQueue.back().length() > m_batchsize) {\n        m_batchingMessageQueue.emplace_back();\n        m_batchingMessageQueue.back().reserve(m_batchsize + 256);\n    }  // When there is already a batch open we need a separator when its not empty\n    else if (!m_batchingMessageQueue.back().empty()) {\n        m_batchingMessageQueue.back().push_back('\\n');\n    }\n    // Add the new message to the batch\n    m_batchingMessageQueue.back().append(message);\n}\n\ninline const std::string& UDPSender::errorMessage() const noexcept {\n    return m_errorMessage;\n}\n\ninline bool UDPSender::initialize() noexcept {\n#ifdef _WIN32\n    if (!detail::WinSockSingleton::getInstance().ok()) {\n        m_errorMessage = \"WSAStartup failed: errno=\" + std::to_string(SOCKET_ERRNO);\n    }\n#endif\n\n    // Connect the socket\n    m_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);\n    if (!detail::isValidSocket(m_socket)) {\n        m_errorMessage = \"socket creation failed: errno=\" + std::to_string(SOCKET_ERRNO);\n        return false;\n    }\n\n    std::memset(&m_server, 0, sizeof(m_server));\n    m_server.sin_family = AF_INET;\n    m_server.sin_port = htons(m_port);\n\n    if (inet_pton(AF_INET, m_host.c_str(), &m_server.sin_addr) == 0) {\n        // An error code has been returned by inet_aton\n\n        // Specify the criteria for selecting the socket address structure\n        struct addrinfo hints;\n        std::memset(&hints, 0, sizeof(hints));\n        hints.ai_family = AF_INET;\n        hints.ai_socktype = SOCK_DGRAM;\n\n        // Get the address info using the hints\n        struct addrinfo* results = nullptr;\n        const int ret{getaddrinfo(m_host.c_str(), nullptr, &hints, &results)};\n        if (ret != 0) {\n            // An error code has been returned by getaddrinfo\n            SOCKET_CLOSE(m_socket);\n            m_socket = k_invalidSocket;\n            m_errorMessage = \"getaddrinfo failed: err=\" + std::to_string(ret) + \", msg=\" + gai_strerror(ret);\n            return false;\n        }\n\n        // Copy the results in m_server\n        struct sockaddr_in* host_addr = (struct sockaddr_in*)results->ai_addr;\n        std::memcpy(&m_server.sin_addr, &host_addr->sin_addr, sizeof(struct in_addr));\n\n        // Free the memory allocated\n        freeaddrinfo(results);\n    }\n\n    return true;\n}\n\ninline void UDPSender::sendToDaemon(const std::string& message) noexcept {\n    // Try sending the message\n    const auto ret = sendto(m_socket,\n                            message.data(),\n#ifdef _WIN32\n                            static_cast<int>(message.size()),\n#else\n                            message.size(),\n#endif\n                            0,\n                            (struct sockaddr*)&m_server,\n                            sizeof(m_server));\n    if (ret == -1) {\n        m_errorMessage = \"sendto server failed: host=\" + m_host + \":\" + std::to_string(m_port) +\n                         \", err=\" + std::to_string(SOCKET_ERRNO);\n    }\n}\n\ninline bool UDPSender::initialized() const noexcept {\n    return m_socket != k_invalidSocket;\n}\n\ninline void UDPSender::flush() noexcept {\n    // We aquire a lock but only if we actually need to (ie there is a thread also accessing the queue)\n    auto batchingLock =\n        m_batchingThread.joinable() ? std::unique_lock<std::mutex>(m_batchingMutex) : std::unique_lock<std::mutex>();\n    // Flush the queue\n    while (!m_batchingMessageQueue.empty()) {\n        sendToDaemon(m_batchingMessageQueue.front());\n        m_batchingMessageQueue.pop_front();\n    }\n}\n\n}  // namespace Statsd\n\n#endif\n"
  },
  {
    "path": "deps/cpp-statsd-client/tests/StatsdServer.hpp",
    "content": "#ifndef STATSD_SERVER_HPP\n#define STATSD_SERVER_HPP\n\n// It might make sense to include this test class in the UDPSender header\n// it includes most of the cross platform defines etc that we need for socket io\n#include \"cpp-statsd-client/UDPSender.hpp\"\n\n#include <algorithm>\n#include <string>\n\nnamespace Statsd {\n\nclass StatsdServer {\npublic:\n    StatsdServer(unsigned short port = 8125) noexcept {\n#ifdef _WIN32\n        if (!detail::WinSockSingleton::getInstance().ok()) {\n            m_errorMessage = \"WSAStartup failed: errno=\" + std::to_string(SOCKET_ERRNO);\n        }\n#endif\n\n        // Create the socket\n        m_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);\n        if (!detail::isValidSocket(m_socket)) {\n            m_errorMessage = \"socket creation failed: errno=\" + std::to_string(SOCKET_ERRNO);\n            return;\n        }\n\n        // Binding should be with ipv4 to all interfaces\n        struct sockaddr_in address {};\n        address.sin_family = AF_INET;\n        address.sin_port = htons(port);\n        address.sin_addr.s_addr = INADDR_ANY;\n\n        // Try to bind\n        if (bind(m_socket, reinterpret_cast<const struct sockaddr*>(&address), sizeof(address)) != 0) {\n            SOCKET_CLOSE(m_socket);\n            m_socket = k_invalidSocket;\n            m_errorMessage = \"bind failed: errno=\" + std::to_string(SOCKET_ERRNO);\n        }\n    }\n\n    ~StatsdServer() {\n        if (detail::isValidSocket(m_socket)) {\n            SOCKET_CLOSE(m_socket);\n        }\n    }\n\n    const std::string& errorMessage() const noexcept {\n        return m_errorMessage;\n    }\n\n    std::string receive() noexcept {\n        // If uninitialized then bail\n        if (!detail::isValidSocket(m_socket)) {\n            return \"\";\n        }\n\n        // Try to receive (this is blocking)\n        std::string buffer(256, '\\0');\n        int string_len;\n        if ((string_len = recv(m_socket, &buffer[0], static_cast<int>(buffer.size()), 0)) < 1) {\n            m_errorMessage = \"Could not recv on the socket file descriptor\";\n            return \"\";\n        }\n\n        // No error return the trimmed result\n        m_errorMessage.clear();\n        buffer.resize(std::min(static_cast<size_t>(string_len), buffer.size()));\n        return buffer;\n    }\n\nprivate:\n    SOCKET_TYPE m_socket;\n    std::string m_errorMessage;\n};\n\n}  // namespace Statsd\n\n#endif\n"
  },
  {
    "path": "deps/cpp-statsd-client/tests/testStatsdClient.cpp",
    "content": "#include <iostream>\n\n#include \"StatsdServer.hpp\"\n#include \"cpp-statsd-client/StatsdClient.hpp\"\n\nusing namespace Statsd;\n\n// Each test suite below spawns a thread to recv the client messages over UDP as if it were a real statsd server\n// Note that we could just synchronously recv metrics and not use a thread but doing the test async has the\n// advantage that we can test the threaded batching mode in a straightforward way. The server thread basically\n// just keeps storing metrics in an vector until it hears a special one signaling the test is over and bails\nvoid mock(StatsdServer& server, std::vector<std::string>& messages) {\n    do {\n        // Grab the messages that are waiting\n        auto recvd = server.receive();\n\n        // Split the messages on '\\n'\n        auto start = std::string::npos;\n        do {\n            // Keep this message\n            auto end = recvd.find('\\n', ++start);\n            messages.emplace_back(recvd.substr(start, end));\n            start = end;\n\n            // Bail if we found the special quit message\n            if (messages.back().find(\"DONE\") != std::string::npos) {\n                messages.pop_back();\n                return;\n            }\n        } while (start != std::string::npos);\n    } while (server.errorMessage().empty() && !messages.back().empty());\n}\n\ntemplate <typename SocketWrapper>\nvoid throwOnError(const SocketWrapper& wrapped, bool expectEmpty = true, const std::string& extraMessage = \"\") {\n    if (wrapped.errorMessage().empty() != expectEmpty) {\n        std::cerr << (expectEmpty ? wrapped.errorMessage() : extraMessage) << std::endl;\n        throw std::runtime_error(expectEmpty ? wrapped.errorMessage() : extraMessage);\n    }\n}\n\nvoid throwOnWrongMessage(StatsdServer& server, const std::string& expected) {\n    auto actual = server.receive();\n    if (actual != expected) {\n        std::cerr << \"Expected: \" << expected << \" but got: \" << actual << std::endl;\n        throw std::runtime_error(\"Incorrect stat received\");\n    }\n}\n\nvoid testErrorConditions() {\n    // Resolve a rubbish ip and make sure initialization failed\n    StatsdClient client{\"256.256.256.256\", 8125, \"myPrefix\", 20};\n    throwOnError(client, false, \"Should not be able to resolve a ridiculous ip\");\n}\n\nvoid testReconfigure() {\n    StatsdServer server;\n    throwOnError(server);\n\n    StatsdClient client(\"localhost\", 8125, \"first.\");\n    client.increment(\"foo\");\n    throwOnWrongMessage(server, \"first.foo:1|c\");\n\n    client.setConfig(\"localhost\", 8125, \"second\");\n    client.increment(\"bar\");\n    throwOnWrongMessage(server, \"second.bar:1|c\");\n\n    client.setConfig(\"localhost\", 8125, \"\");\n    client.increment(\"third.baz\");\n    throwOnWrongMessage(server, \"third.baz:1|c\");\n\n    client.increment(\"\");\n    throwOnWrongMessage(server, \":1|c\");\n\n    // TODO: test what happens with the batching after resolving the question about incomplete\n    //  batches being dropped vs sent on reconfiguring\n}\n\nvoid testSendRecv(uint64_t batchSize, uint64_t sendInterval) {\n    StatsdServer mock_server;\n    std::vector<std::string> messages, expected;\n    std::thread server(mock, std::ref(mock_server), std::ref(messages));\n\n    // Set a new config that has the client send messages to a proper address that can be resolved\n    StatsdClient client(\"localhost\", 8125, \"sendRecv.\", batchSize, sendInterval, 3);\n    throwOnError(client);\n\n    // TODO: I forget if we need to wait for the server to be ready here before sending the first stats\n    //  is there a race condition where the client sending before the server binds would drop that clients message\n\n    for (int i = 0; i < 3; ++i) {\n        // Increment \"coco\"\n        client.increment(\"coco\");\n        throwOnError(client);\n        expected.emplace_back(\"sendRecv.coco:1|c\");\n\n        // Decrement \"kiki\"\n        client.decrement(\"kiki\");\n        throwOnError(client);\n        expected.emplace_back(\"sendRecv.kiki:-1|c\");\n\n        // Adjusts \"toto\" by +2\n        client.seed(19);  // this seed gets a hit on the first call\n        client.count(\"toto\", 2, 0.1f);\n        throwOnError(client);\n        expected.emplace_back(\"sendRecv.toto:2|c|@0.10\");\n\n        // Gets \"sampled out\" by the random number generator\n        client.count(\"popo\", 9, 0.1f);\n        throwOnError(client);\n\n        // Record a gauge \"titi\" to 3\n        client.gauge(\"titi\", 3);\n        throwOnError(client);\n        expected.emplace_back(\"sendRecv.titi:3|g\");\n\n        // Record a gauge \"titifloat\" to -123.456789 with precision 3\n        client.gauge(\"titifloat\", -123.456789);\n        throwOnError(client);\n        expected.emplace_back(\"sendRecv.titifloat:-123.457|g\");\n\n        // Record a timing of 2ms for \"myTiming\"\n        client.seed(19);\n        client.timing(\"myTiming\", 2, 0.1f);\n        throwOnError(client);\n        expected.emplace_back(\"sendRecv.myTiming:2|ms|@0.10\");\n\n        // Send a set with 1227 total uniques\n        client.set(\"tutu\", 1227, 2.0f);\n        throwOnError(client);\n        expected.emplace_back(\"sendRecv.tutu:1227|s\");\n\n        // Gauge but with tags\n        client.gauge(\"dr.röstigrabe\", 333, 1.f, {\"liegt\", \"im\", \"weste\"});\n        throwOnError(client);\n        expected.emplace_back(\"sendRecv.dr.röstigrabe:333|g|#liegt,im,weste\");\n\n        // All the things\n        client.count(\"foo\", -42, .9f, {\"bar\", \"baz\"});\n        throwOnError(client);\n        expected.emplace_back(\"sendRecv.foo:-42|c|@0.90|#bar,baz\");\n    }\n\n    // Signal the mock server we are done\n    client.timing(\"DONE\", 0);\n\n    // If manual flushing do it now\n    if (sendInterval == 0) {\n        client.flush();\n    }\n\n    // Wait for the server to stop\n    server.join();\n\n    // Make sure we get the exactly correct output\n    if (messages != expected) {\n        std::cerr << \"Unexpected stats received by server, got:\" << std::endl;\n        for (const auto& message : messages) {\n            std::cerr << message << std::endl;\n        }\n        std::cerr << std::endl << \"But we expected:\" << std::endl;\n        for (const auto& message : expected) {\n            std::cerr << message << std::endl;\n        }\n        throw std::runtime_error(\"Unexpected stats\");\n    }\n}\n\nint main() {\n    // If any of these tests fail they throw an exception, not catching makes for a nonzero return code\n\n    // general things that should be errors\n    testErrorConditions();\n    // reconfiguring how you are sending\n    testReconfigure();\n    // no batching\n    testSendRecv(0, 0);\n    // background batching\n    testSendRecv(32, 1000);\n    // manual flushing of batches\n    testSendRecv(16, 0);\n\n    return EXIT_SUCCESS;\n}\n"
  },
  {
    "path": "deps/hdr_histogram/COPYING.txt",
    "content": "Creative Commons Legal Code\n\nCC0 1.0 Universal\n\n    CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE\n    LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN\n    ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS\n    INFORMATION ON AN \"AS-IS\" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES\n    REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS\n    PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM\n    THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED\n    HEREUNDER.\n\nStatement of Purpose\n\nThe laws of most jurisdictions throughout the world automatically confer\nexclusive Copyright and Related Rights (defined below) upon the creator\nand subsequent owner(s) (each and all, an \"owner\") of an original work of\nauthorship and/or a database (each, a \"Work\").\n\nCertain owners wish to permanently relinquish those rights to a Work for\nthe purpose of contributing to a commons of creative, cultural and\nscientific works (\"Commons\") that the public can reliably and without fear\nof later claims of infringement build upon, modify, incorporate in other\nworks, reuse and redistribute as freely as possible in any form whatsoever\nand for any purposes, including without limitation commercial purposes.\nThese owners may contribute to the Commons to promote the ideal of a free\nculture and the further production of creative, cultural and scientific\nworks, or to gain reputation or greater distribution for their Work in\npart through the use and efforts of others.\n\nFor these and/or other purposes and motivations, and without any\nexpectation of additional consideration or compensation, the person\nassociating CC0 with a Work (the \"Affirmer\"), to the extent that he or she\nis an owner of Copyright and Related Rights in the Work, voluntarily\nelects to apply CC0 to the Work and publicly distribute the Work under its\nterms, with knowledge of his or her Copyright and Related Rights in the\nWork and the meaning and intended legal effect of CC0 on those rights.\n\n1. Copyright and Related Rights. A Work made available under CC0 may be\nprotected by copyright and related or neighboring rights (\"Copyright and\nRelated Rights\"). Copyright and Related Rights include, but are not\nlimited to, the following:\n\n  i. the right to reproduce, adapt, distribute, perform, display,\n     communicate, and translate a Work;\n ii. moral rights retained by the original author(s) and/or performer(s);\niii. publicity and privacy rights pertaining to a person's image or\n     likeness depicted in a Work;\n iv. rights protecting against unfair competition in regards to a Work,\n     subject to the limitations in paragraph 4(a), below;\n  v. rights protecting the extraction, dissemination, use and reuse of data\n     in a Work;\n vi. database rights (such as those arising under Directive 96/9/EC of the\n     European Parliament and of the Council of 11 March 1996 on the legal\n     protection of databases, and under any national implementation\n     thereof, including any amended or successor version of such\n     directive); and\nvii. other similar, equivalent or corresponding rights throughout the\n     world based on applicable law or treaty, and any national\n     implementations thereof.\n\n2. Waiver. To the greatest extent permitted by, but not in contravention\nof, applicable law, Affirmer hereby overtly, fully, permanently,\nirrevocably and unconditionally waives, abandons, and surrenders all of\nAffirmer's Copyright and Related Rights and associated claims and causes\nof action, whether now known or unknown (including existing as well as\nfuture claims and causes of action), in the Work (i) in all territories\nworldwide, (ii) for the maximum duration provided by applicable law or\ntreaty (including future time extensions), (iii) in any current or future\nmedium and for any number of copies, and (iv) for any purpose whatsoever,\nincluding without limitation commercial, advertising or promotional\npurposes (the \"Waiver\"). Affirmer makes the Waiver for the benefit of each\nmember of the public at large and to the detriment of Affirmer's heirs and\nsuccessors, fully intending that such Waiver shall not be subject to\nrevocation, rescission, cancellation, termination, or any other legal or\nequitable action to disrupt the quiet enjoyment of the Work by the public\nas contemplated by Affirmer's express Statement of Purpose.\n\n3. Public License Fallback. Should any part of the Waiver for any reason\nbe judged legally invalid or ineffective under applicable law, then the\nWaiver shall be preserved to the maximum extent permitted taking into\naccount Affirmer's express Statement of Purpose. In addition, to the\nextent the Waiver is so judged Affirmer hereby grants to each affected\nperson a royalty-free, non transferable, non sublicensable, non exclusive,\nirrevocable and unconditional license to exercise Affirmer's Copyright and\nRelated Rights in the Work (i) in all territories worldwide, (ii) for the\nmaximum duration provided by applicable law or treaty (including future\ntime extensions), (iii) in any current or future medium and for any number\nof copies, and (iv) for any purpose whatsoever, including without\nlimitation commercial, advertising or promotional purposes (the\n\"License\"). The License shall be deemed effective as of the date CC0 was\napplied by Affirmer to the Work. Should any part of the License for any\nreason be judged legally invalid or ineffective under applicable law, such\npartial invalidity or ineffectiveness shall not invalidate the remainder\nof the License, and in such case Affirmer hereby affirms that he or she\nwill not (i) exercise any of his or her remaining Copyright and Related\nRights in the Work or (ii) assert any associated claims and causes of\naction with respect to the Work, in either case contrary to Affirmer's\nexpress Statement of Purpose.\n\n4. Limitations and Disclaimers.\n\n a. No trademark or patent rights held by Affirmer are waived, abandoned,\n    surrendered, licensed or otherwise affected by this document.\n b. Affirmer offers the Work as-is and makes no representations or\n    warranties of any kind concerning the Work, express, implied,\n    statutory or otherwise, including without limitation warranties of\n    title, merchantability, fitness for a particular purpose, non\n    infringement, or the absence of latent or other defects, accuracy, or\n    the present or absence of errors, whether or not discoverable, all to\n    the greatest extent permissible under applicable law.\n c. Affirmer disclaims responsibility for clearing rights of other persons\n    that may apply to the Work or any use thereof, including without\n    limitation any person's Copyright and Related Rights in the Work.\n    Further, Affirmer disclaims responsibility for obtaining any necessary\n    consents, permissions or other rights required for any use of the\n    Work.\n d. Affirmer understands and acknowledges that Creative Commons is not a\n    party to this document and has no duty or obligation with respect to\n    this CC0 or use of the Work.\n"
  },
  {
    "path": "deps/hdr_histogram/LICENSE.txt",
    "content": "The code in this repository code was Written by Gil Tene, Michael Barker,\nand Matt Warren, and released to the public domain, as explained at\nhttp://creativecommons.org/publicdomain/zero/1.0/\n\nFor users of this code who wish to consume it under the \"BSD\" license\nrather than under the public domain or CC0 contribution text mentioned\nabove, the code found under this directory is *also* provided under the\nfollowing license (commonly referred to as the BSD 2-Clause License). This\nlicense does not detract from the above stated release of the code into\nthe public domain, and simply represents an additional license granted by\nthe Author.\n\n-----------------------------------------------------------------------------\n** Beginning of \"BSD 2-Clause License\" text. **\n\n Copyright (c) 2012, 2013, 2014 Gil Tene\n Copyright (c) 2014 Michael Barker\n Copyright (c) 2014 Matt Warren\n All rights reserved.\n\n Redistribution and use in source and binary forms, with or without\n modification, are permitted provided that the following conditions are met:\n\n 1. Redistributions of source code must retain the above copyright notice,\n    this list of conditions and the following disclaimer.\n\n 2. Redistributions in binary form must reproduce the above copyright notice,\n    this list of conditions and the following disclaimer in the documentation\n    and/or other materials provided with the distribution.\n\n THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE\n LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF\n THE POSSIBILITY OF SUCH DAMAGE.\n"
  },
  {
    "path": "deps/hdr_histogram/Makefile",
    "content": "STD=\nWARN= -Wall\nOPT= -Os\n\nR_CFLAGS= $(STD) $(WARN) $(OPT) $(DEBUG) $(CFLAGS)\nR_LDFLAGS= $(LDFLAGS)\nDEBUG= -g\n\nR_CC=$(CC) $(R_CFLAGS)\nR_LD=$(CC) $(R_LDFLAGS)\n\nhdr_histogram.o: hdr_histogram.h hdr_histogram.c \n\n.c.o:\n\t$(R_CC) -c  $< \n\nclean:\n\trm -f *.o\n\n\n"
  },
  {
    "path": "deps/hdr_histogram/README.md",
    "content": "HdrHistogram_c v0.11.0\n\n----------------------------------------------\n\nThis port contains a subset of the 'C' version of High Dynamic Range (HDR) Histogram available at [github.com/HdrHistogram/HdrHistogram_c](https://github.com/HdrHistogram/HdrHistogram_c).\n\n\nThe code present on `hdr_histogram.c`, `hdr_histogram.h`, and `hdr_atomic.c` was Written by Gil Tene, Michael Barker,\nand Matt Warren, and released to the public domain, as explained at\nhttp://creativecommons.org/publicdomain/zero/1.0/."
  },
  {
    "path": "deps/hdr_histogram/hdr_atomic.h",
    "content": "/**\n * hdr_atomic.h\n * Written by Philip Orwig and released to the public domain,\n * as explained at http://creativecommons.org/publicdomain/zero/1.0/\n */\n\n#ifndef HDR_ATOMIC_H__\n#define HDR_ATOMIC_H__\n\n\n#if defined(_MSC_VER)\n\n#include <stdint.h>\n#include <intrin.h>\n#include <stdbool.h>\n\nstatic void __inline * hdr_atomic_load_pointer(void** pointer)\n{\n\t_ReadBarrier();\n\treturn *pointer;\n}\n\nstatic void hdr_atomic_store_pointer(void** pointer, void* value)\n{\n\t_WriteBarrier();\n\t*pointer = value;\n}\n\nstatic int64_t __inline hdr_atomic_load_64(int64_t* field)\n{ \n\t_ReadBarrier();\n\treturn *field;\n}\n\nstatic void __inline hdr_atomic_store_64(int64_t* field, int64_t value)\n{\n\t_WriteBarrier();\n\t*field = value;\n}\n\nstatic int64_t __inline hdr_atomic_exchange_64(volatile int64_t* field, int64_t value)\n{\n#if defined(_WIN64)\n    return _InterlockedExchange64(field, value);\n#else\n    int64_t comparand;\n    int64_t initial_value = *field;\n    do\n    {\n        comparand = initial_value;\n        initial_value = _InterlockedCompareExchange64(field, value, comparand);\n    }\n    while (comparand != initial_value);\n\n    return initial_value;\n#endif\n}\n\nstatic int64_t __inline hdr_atomic_add_fetch_64(volatile int64_t* field, int64_t value)\n{\n#if defined(_WIN64)\n\treturn _InterlockedExchangeAdd64(field, value) + value;\n#else\n    int64_t comparand;\n    int64_t initial_value = *field;\n    do\n    {\n        comparand = initial_value;\n        initial_value = _InterlockedCompareExchange64(field, comparand + value, comparand);\n    }\n    while (comparand != initial_value);\n\n    return initial_value + value;\n#endif\n}\n\nstatic bool __inline hdr_atomic_compare_exchange_64(volatile int64_t* field, int64_t* expected, int64_t desired)\n{\n    return *expected == _InterlockedCompareExchange64(field, desired, *expected);\n}\n\n#elif defined(__ATOMIC_SEQ_CST)\n\n#define hdr_atomic_load_pointer(x) __atomic_load_n(x, __ATOMIC_SEQ_CST)\n#define hdr_atomic_store_pointer(f,v) __atomic_store_n(f,v, __ATOMIC_SEQ_CST)\n#define hdr_atomic_load_64(x) __atomic_load_n(x, __ATOMIC_SEQ_CST)\n#define hdr_atomic_store_64(f,v) __atomic_store_n(f,v, __ATOMIC_SEQ_CST)\n#define hdr_atomic_exchange_64(f,i) __atomic_exchange_n(f,i, __ATOMIC_SEQ_CST)\n#define hdr_atomic_add_fetch_64(field, value) __atomic_add_fetch(field, value, __ATOMIC_SEQ_CST)\n#define hdr_atomic_compare_exchange_64(field, expected, desired) __atomic_compare_exchange_n(field, expected, desired, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)\n\n#elif defined(__x86_64__)\n\n#include <stdint.h>\n#include <stdbool.h>\n\nstatic inline void* hdr_atomic_load_pointer(void** pointer)\n{\n   void* p =  *pointer;\n\tasm volatile (\"\" ::: \"memory\");\n\treturn p;\n}\n\nstatic inline void hdr_atomic_store_pointer(void** pointer, void* value)\n{\n    asm volatile (\"lock; xchgq %0, %1\" : \"+q\" (value), \"+m\" (*pointer));\n}\n\nstatic inline int64_t hdr_atomic_load_64(int64_t* field)\n{\n    int64_t i = *field;\n\tasm volatile (\"\" ::: \"memory\");\n\treturn i;\n}\n\nstatic inline void hdr_atomic_store_64(int64_t* field, int64_t value)\n{\n    asm volatile (\"lock; xchgq %0, %1\" : \"+q\" (value), \"+m\" (*field));\n}\n\nstatic inline int64_t hdr_atomic_exchange_64(volatile int64_t* field, int64_t value)\n{\n    int64_t result = 0;\n    asm volatile (\"lock; xchgq %1, %2\" : \"=r\" (result), \"+q\" (value), \"+m\" (*field));\n    return result;\n}\n\nstatic inline int64_t hdr_atomic_add_fetch_64(volatile int64_t* field, int64_t value)\n{\n    return __sync_add_and_fetch(field, value);\n}\n\nstatic inline bool hdr_atomic_compare_exchange_64(volatile int64_t* field, int64_t* expected, int64_t desired)\n{\n    int64_t original;\n    asm volatile( \"lock; cmpxchgq %2, %1\" : \"=a\"(original), \"+m\"(*field) : \"q\"(desired), \"0\"(*expected));\n    return original == *expected;\n}\n\n#else\n\n#error \"Unable to determine atomic operations for your platform\"\n\n#endif\n\n#endif /* HDR_ATOMIC_H__ */\n"
  },
  {
    "path": "deps/hdr_histogram/hdr_histogram.c",
    "content": "/**\n * hdr_histogram.c\n * Written by Michael Barker and released to the public domain,\n * as explained at http://creativecommons.org/publicdomain/zero/1.0/\n */\n\n#include <stdlib.h>\n#include <stdbool.h>\n#include <math.h>\n#include <stdio.h>\n#include <string.h>\n#include <stdint.h>\n#include <errno.h>\n#include <inttypes.h>\n\n#include \"hdr_histogram.h\"\n#include \"hdr_atomic.h\"\n\n/*  ######   #######  ##     ## ##    ## ########  ######  */\n/* ##    ## ##     ## ##     ## ###   ##    ##    ##    ## */\n/* ##       ##     ## ##     ## ####  ##    ##    ##       */\n/* ##       ##     ## ##     ## ## ## ##    ##     ######  */\n/* ##       ##     ## ##     ## ##  ####    ##          ## */\n/* ##    ## ##     ## ##     ## ##   ###    ##    ##    ## */\n/*  ######   #######   #######  ##    ##    ##     ######  */\n\nstatic int32_t normalize_index(const struct hdr_histogram* h, int32_t index)\n{\n    int32_t normalized_index;\n    int32_t adjustment = 0;\n    if (h->normalizing_index_offset == 0)\n    {\n        return index;\n    }\n\n    normalized_index = index - h->normalizing_index_offset;\n\n    if (normalized_index < 0)\n    {\n        adjustment = h->counts_len;\n    }\n    else if (normalized_index >= h->counts_len)\n    {\n        adjustment = -h->counts_len;\n    }\n\n    return normalized_index + adjustment;\n}\n\nstatic int64_t counts_get_direct(const struct hdr_histogram* h, int32_t index)\n{\n    return h->counts[index];\n}\n\nstatic int64_t counts_get_normalised(const struct hdr_histogram* h, int32_t index)\n{\n    return counts_get_direct(h, normalize_index(h, index));\n}\n\nstatic void counts_inc_normalised(\n    struct hdr_histogram* h, int32_t index, int64_t value)\n{\n    int32_t normalised_index = normalize_index(h, index);\n    h->counts[normalised_index] += value;\n    h->total_count += value;\n}\n\nstatic void counts_inc_normalised_atomic(\n    struct hdr_histogram* h, int32_t index, int64_t value)\n{\n    int32_t normalised_index = normalize_index(h, index);\n\n    hdr_atomic_add_fetch_64(&h->counts[normalised_index], value);\n    hdr_atomic_add_fetch_64(&h->total_count, value);\n}\n\nstatic void update_min_max(struct hdr_histogram* h, int64_t value)\n{\n    h->min_value = (value < h->min_value && value != 0) ? value : h->min_value;\n    h->max_value = (value > h->max_value) ? value : h->max_value;\n}\n\nstatic void update_min_max_atomic(struct hdr_histogram* h, int64_t value)\n{\n    int64_t current_min_value;\n    int64_t current_max_value;\n    do\n    {\n        current_min_value = hdr_atomic_load_64(&h->min_value);\n\n        if (0 == value || current_min_value <= value)\n        {\n            break;\n        }\n    }\n    while (!hdr_atomic_compare_exchange_64(&h->min_value, &current_min_value, value));\n\n    do\n    {\n        current_max_value = hdr_atomic_load_64(&h->max_value);\n\n        if (value <= current_max_value)\n        {\n            break;\n        }\n    }\n    while (!hdr_atomic_compare_exchange_64(&h->max_value, &current_max_value, value));\n}\n\n\n/* ##     ## ######## #### ##       #### ######## ##    ## */\n/* ##     ##    ##     ##  ##        ##     ##     ##  ##  */\n/* ##     ##    ##     ##  ##        ##     ##      ####   */\n/* ##     ##    ##     ##  ##        ##     ##       ##    */\n/* ##     ##    ##     ##  ##        ##     ##       ##    */\n/* ##     ##    ##     ##  ##        ##     ##       ##    */\n/*  #######     ##    #### ######## ####    ##       ##    */\n\nstatic int64_t power(int64_t base, int64_t exp)\n{\n    int64_t result = 1;\n    while(exp)\n    {\n        result *= base; exp--;\n    }\n    return result;\n}\n\n#if defined(_MSC_VER)\n#   if defined(_WIN64)\n#       pragma intrinsic(_BitScanReverse64)\n#   else\n#       pragma intrinsic(_BitScanReverse)\n#   endif\n#endif\n\nstatic int32_t count_leading_zeros_64(int64_t value)\n{\n#if defined(_MSC_VER)\n    uint32_t leading_zero = 0;\n#if defined(_WIN64)\n    _BitScanReverse64(&leading_zero, value);\n#else\n    uint32_t high = value >> 32;\n    if  (_BitScanReverse(&leading_zero, high))\n    {\n        leading_zero += 32;\n    }\n    else\n    {\n        uint32_t low = value & 0x00000000FFFFFFFF;\n        _BitScanReverse(&leading_zero, low);\n    }\n#endif\n    return 63 - leading_zero; /* smallest power of 2 containing value */\n#else\n    return __builtin_clzll(value); /* smallest power of 2 containing value */\n#endif\n}\n\nstatic int32_t get_bucket_index(const struct hdr_histogram* h, int64_t value)\n{\n    int32_t pow2ceiling = 64 - count_leading_zeros_64(value | h->sub_bucket_mask); /* smallest power of 2 containing value */\n    return pow2ceiling - h->unit_magnitude - (h->sub_bucket_half_count_magnitude + 1);\n}\n\nstatic int32_t get_sub_bucket_index(int64_t value, int32_t bucket_index, int32_t unit_magnitude)\n{\n    return (int32_t)(value >> (bucket_index + unit_magnitude));\n}\n\nstatic int32_t counts_index(const struct hdr_histogram* h, int32_t bucket_index, int32_t sub_bucket_index)\n{\n    /* Calculate the index for the first entry in the bucket: */\n    /* (The following is the equivalent of ((bucket_index + 1) * subBucketHalfCount) ): */\n    int32_t bucket_base_index = (bucket_index + 1) << h->sub_bucket_half_count_magnitude;\n    /* Calculate the offset in the bucket: */\n    int32_t offset_in_bucket = sub_bucket_index - h->sub_bucket_half_count;\n    /* The following is the equivalent of ((sub_bucket_index  - subBucketHalfCount) + bucketBaseIndex; */\n    return bucket_base_index + offset_in_bucket;\n}\n\nstatic int64_t value_from_index(int32_t bucket_index, int32_t sub_bucket_index, int32_t unit_magnitude)\n{\n    return ((int64_t) sub_bucket_index) << (bucket_index + unit_magnitude);\n}\n\nint32_t counts_index_for(const struct hdr_histogram* h, int64_t value)\n{\n    int32_t bucket_index     = get_bucket_index(h, value);\n    int32_t sub_bucket_index = get_sub_bucket_index(value, bucket_index, h->unit_magnitude);\n\n    return counts_index(h, bucket_index, sub_bucket_index);\n}\n\nint64_t hdr_value_at_index(const struct hdr_histogram *h, int32_t index)\n{\n    int32_t bucket_index = (index >> h->sub_bucket_half_count_magnitude) - 1;\n    int32_t sub_bucket_index = (index & (h->sub_bucket_half_count - 1)) + h->sub_bucket_half_count;\n\n    if (bucket_index < 0)\n    {\n        sub_bucket_index -= h->sub_bucket_half_count;\n        bucket_index = 0;\n    }\n\n    return value_from_index(bucket_index, sub_bucket_index, h->unit_magnitude);\n}\n\nint64_t hdr_size_of_equivalent_value_range(const struct hdr_histogram* h, int64_t value)\n{\n    int32_t bucket_index     = get_bucket_index(h, value);\n    int32_t sub_bucket_index = get_sub_bucket_index(value, bucket_index, h->unit_magnitude);\n    int32_t adjusted_bucket  = (sub_bucket_index >= h->sub_bucket_count) ? (bucket_index + 1) : bucket_index;\n    return INT64_C(1) << (h->unit_magnitude + adjusted_bucket);\n}\n\nstatic int64_t lowest_equivalent_value(const struct hdr_histogram* h, int64_t value)\n{\n    int32_t bucket_index     = get_bucket_index(h, value);\n    int32_t sub_bucket_index = get_sub_bucket_index(value, bucket_index, h->unit_magnitude);\n    return value_from_index(bucket_index, sub_bucket_index, h->unit_magnitude);\n}\n\nint64_t hdr_next_non_equivalent_value(const struct hdr_histogram *h, int64_t value)\n{\n    return lowest_equivalent_value(h, value) + hdr_size_of_equivalent_value_range(h, value);\n}\n\nstatic int64_t highest_equivalent_value(const struct hdr_histogram* h, int64_t value)\n{\n    return hdr_next_non_equivalent_value(h, value) - 1;\n}\n\nint64_t hdr_median_equivalent_value(const struct hdr_histogram *h, int64_t value)\n{\n    return lowest_equivalent_value(h, value) + (hdr_size_of_equivalent_value_range(h, value) >> 1);\n}\n\nstatic int64_t non_zero_min(const struct hdr_histogram* h)\n{\n    if (INT64_MAX == h->min_value)\n    {\n        return INT64_MAX;\n    }\n\n    return lowest_equivalent_value(h, h->min_value);\n}\n\nvoid hdr_reset_internal_counters(struct hdr_histogram* h)\n{\n    int min_non_zero_index = -1;\n    int max_index = -1;\n    int64_t observed_total_count = 0;\n    int i;\n\n    for (i = 0; i < h->counts_len; i++)\n    {\n        int64_t count_at_index;\n\n        if ((count_at_index = counts_get_direct(h, i)) > 0)\n        {\n            observed_total_count += count_at_index;\n            max_index = i;\n            if (min_non_zero_index == -1 && i != 0)\n            {\n                min_non_zero_index = i;\n            }\n        }\n    }\n\n    if (max_index == -1)\n    {\n        h->max_value = 0;\n    }\n    else\n    {\n        int64_t max_value = hdr_value_at_index(h, max_index);\n        h->max_value = highest_equivalent_value(h, max_value);\n    }\n\n    if (min_non_zero_index == -1)\n    {\n        h->min_value = INT64_MAX;\n    }\n    else\n    {\n        h->min_value = hdr_value_at_index(h, min_non_zero_index);\n    }\n\n    h->total_count = observed_total_count;\n}\n\nstatic int32_t buckets_needed_to_cover_value(int64_t value, int32_t sub_bucket_count, int32_t unit_magnitude)\n{\n    int64_t smallest_untrackable_value = ((int64_t) sub_bucket_count) << unit_magnitude;\n    int32_t buckets_needed = 1;\n    while (smallest_untrackable_value <= value)\n    {\n        if (smallest_untrackable_value > INT64_MAX / 2)\n        {\n            return buckets_needed + 1;\n        }\n        smallest_untrackable_value <<= 1;\n        buckets_needed++;\n    }\n\n    return buckets_needed;\n}\n\n/* ##     ## ######## ##     ##  #######  ########  ##    ## */\n/* ###   ### ##       ###   ### ##     ## ##     ##  ##  ##  */\n/* #### #### ##       #### #### ##     ## ##     ##   ####   */\n/* ## ### ## ######   ## ### ## ##     ## ########     ##    */\n/* ##     ## ##       ##     ## ##     ## ##   ##      ##    */\n/* ##     ## ##       ##     ## ##     ## ##    ##     ##    */\n/* ##     ## ######## ##     ##  #######  ##     ##    ##    */\n\nint hdr_calculate_bucket_config(\n        int64_t lowest_trackable_value,\n        int64_t highest_trackable_value,\n        int significant_figures,\n        struct hdr_histogram_bucket_config* cfg)\n{\n    int32_t sub_bucket_count_magnitude;\n    int64_t largest_value_with_single_unit_resolution;\n\n    if (lowest_trackable_value < 1 ||\n            significant_figures < 1 || 5 < significant_figures ||\n            lowest_trackable_value * 2 > highest_trackable_value)\n    {\n        return EINVAL;\n    }\n\n    cfg->lowest_trackable_value = lowest_trackable_value;\n    cfg->significant_figures = significant_figures;\n    cfg->highest_trackable_value = highest_trackable_value;\n\n    largest_value_with_single_unit_resolution = 2 * power(10, significant_figures);\n    sub_bucket_count_magnitude = (int32_t) ceil(log((double)largest_value_with_single_unit_resolution) / log(2));\n    cfg->sub_bucket_half_count_magnitude = ((sub_bucket_count_magnitude > 1) ? sub_bucket_count_magnitude : 1) - 1;\n\n    cfg->unit_magnitude = (int32_t) floor(log((double)lowest_trackable_value) / log(2));\n\n    cfg->sub_bucket_count      = (int32_t) pow(2, (cfg->sub_bucket_half_count_magnitude + 1));\n    cfg->sub_bucket_half_count = cfg->sub_bucket_count / 2;\n    cfg->sub_bucket_mask       = ((int64_t) cfg->sub_bucket_count - 1) << cfg->unit_magnitude;\n\n    if (cfg->unit_magnitude + cfg->sub_bucket_half_count_magnitude > 61)\n    {\n        return EINVAL;\n    }\n\n    cfg->bucket_count = buckets_needed_to_cover_value(highest_trackable_value, cfg->sub_bucket_count, (int32_t)cfg->unit_magnitude);\n    cfg->counts_len = (cfg->bucket_count + 1) * (cfg->sub_bucket_count / 2);\n\n    return 0;\n}\n\nvoid hdr_init_preallocated(struct hdr_histogram* h, struct hdr_histogram_bucket_config* cfg)\n{\n    h->lowest_trackable_value          = cfg->lowest_trackable_value;\n    h->highest_trackable_value         = cfg->highest_trackable_value;\n    h->unit_magnitude                  = (int32_t)cfg->unit_magnitude;\n    h->significant_figures             = (int32_t)cfg->significant_figures;\n    h->sub_bucket_half_count_magnitude = cfg->sub_bucket_half_count_magnitude;\n    h->sub_bucket_half_count           = cfg->sub_bucket_half_count;\n    h->sub_bucket_mask                 = cfg->sub_bucket_mask;\n    h->sub_bucket_count                = cfg->sub_bucket_count;\n    h->min_value                       = INT64_MAX;\n    h->max_value                       = 0;\n    h->normalizing_index_offset        = 0;\n    h->conversion_ratio                = 1.0;\n    h->bucket_count                    = cfg->bucket_count;\n    h->counts_len                      = cfg->counts_len;\n    h->total_count                     = 0;\n}\n\nint hdr_init(\n        int64_t lowest_trackable_value,\n        int64_t highest_trackable_value,\n        int significant_figures,\n        struct hdr_histogram** result)\n{\n    int64_t* counts;\n    struct hdr_histogram_bucket_config cfg;\n    struct hdr_histogram* histogram;\n\n    int r = hdr_calculate_bucket_config(lowest_trackable_value, highest_trackable_value, significant_figures, &cfg);\n    if (r)\n    {\n        return r;\n    }\n\n    counts = (int64_t*) calloc((size_t) cfg.counts_len, sizeof(int64_t));\n    if (!counts)\n    {\n        return ENOMEM;\n    }\n\n    histogram = (struct hdr_histogram*) calloc(1, sizeof(struct hdr_histogram));\n    if (!histogram)\n    {\n        free(counts);\n        return ENOMEM;\n    }\n\n    histogram->counts = counts;\n\n    hdr_init_preallocated(histogram, &cfg);\n    *result = histogram;\n\n    return 0;\n}\n\nvoid hdr_close(struct hdr_histogram* h)\n{\n    if (h) {\n\tfree(h->counts);\n\tfree(h);\n    }\n}\n\nint hdr_alloc(int64_t highest_trackable_value, int significant_figures, struct hdr_histogram** result)\n{\n    return hdr_init(1, highest_trackable_value, significant_figures, result);\n}\n\n/* reset a histogram to zero. */\nvoid hdr_reset(struct hdr_histogram *h)\n{\n     h->total_count=0;\n     h->min_value = INT64_MAX;\n     h->max_value = 0;\n     memset(h->counts, 0, (sizeof(int64_t) * h->counts_len));\n}\n\nsize_t hdr_get_memory_size(struct hdr_histogram *h)\n{\n    return sizeof(struct hdr_histogram) + h->counts_len * sizeof(int64_t);\n}\n\n/* ##     ## ########  ########     ###    ######## ########  ######  */\n/* ##     ## ##     ## ##     ##   ## ##      ##    ##       ##    ## */\n/* ##     ## ##     ## ##     ##  ##   ##     ##    ##       ##       */\n/* ##     ## ########  ##     ## ##     ##    ##    ######    ######  */\n/* ##     ## ##        ##     ## #########    ##    ##             ## */\n/* ##     ## ##        ##     ## ##     ##    ##    ##       ##    ## */\n/*  #######  ##        ########  ##     ##    ##    ########  ######  */\n\n\nbool hdr_record_value(struct hdr_histogram* h, int64_t value)\n{\n    return hdr_record_values(h, value, 1);\n}\n\nbool hdr_record_value_atomic(struct hdr_histogram* h, int64_t value)\n{\n    return hdr_record_values_atomic(h, value, 1);\n}\n\nbool hdr_record_values(struct hdr_histogram* h, int64_t value, int64_t count)\n{\n    int32_t counts_index;\n\n    if (value < 0)\n    {\n        return false;\n    }\n\n    counts_index = counts_index_for(h, value);\n\n    if (counts_index < 0 || h->counts_len <= counts_index)\n    {\n        return false;\n    }\n\n    counts_inc_normalised(h, counts_index, count);\n    update_min_max(h, value);\n\n    return true;\n}\n\nbool hdr_record_values_atomic(struct hdr_histogram* h, int64_t value, int64_t count)\n{\n    int32_t counts_index;\n\n    if (value < 0)\n    {\n        return false;\n    }\n\n    counts_index = counts_index_for(h, value);\n\n    if (counts_index < 0 || h->counts_len <= counts_index)\n    {\n        return false;\n    }\n\n    counts_inc_normalised_atomic(h, counts_index, count);\n    update_min_max_atomic(h, value);\n\n    return true;\n}\n\nbool hdr_record_corrected_value(struct hdr_histogram* h, int64_t value, int64_t expected_interval)\n{\n    return hdr_record_corrected_values(h, value, 1, expected_interval);\n}\n\nbool hdr_record_corrected_value_atomic(struct hdr_histogram* h, int64_t value, int64_t expected_interval)\n{\n    return hdr_record_corrected_values_atomic(h, value, 1, expected_interval);\n}\n\nbool hdr_record_corrected_values(struct hdr_histogram* h, int64_t value, int64_t count, int64_t expected_interval)\n{\n    int64_t missing_value;\n\n    if (!hdr_record_values(h, value, count))\n    {\n        return false;\n    }\n\n    if (expected_interval <= 0 || value <= expected_interval)\n    {\n        return true;\n    }\n\n    missing_value = value - expected_interval;\n    for (; missing_value >= expected_interval; missing_value -= expected_interval)\n    {\n        if (!hdr_record_values(h, missing_value, count))\n        {\n            return false;\n        }\n    }\n\n    return true;\n}\n\nbool hdr_record_corrected_values_atomic(struct hdr_histogram* h, int64_t value, int64_t count, int64_t expected_interval)\n{\n    int64_t missing_value;\n\n    if (!hdr_record_values_atomic(h, value, count))\n    {\n        return false;\n    }\n\n    if (expected_interval <= 0 || value <= expected_interval)\n    {\n        return true;\n    }\n\n    missing_value = value - expected_interval;\n    for (; missing_value >= expected_interval; missing_value -= expected_interval)\n    {\n        if (!hdr_record_values_atomic(h, missing_value, count))\n        {\n            return false;\n        }\n    }\n\n    return true;\n}\n\nint64_t hdr_add(struct hdr_histogram* h, const struct hdr_histogram* from)\n{\n    struct hdr_iter iter;\n    int64_t dropped = 0;\n    hdr_iter_recorded_init(&iter, from);\n\n    while (hdr_iter_next(&iter))\n    {\n        int64_t value = iter.value;\n        int64_t count = iter.count;\n\n        if (!hdr_record_values(h, value, count))\n        {\n            dropped += count;\n        }\n    }\n\n    return dropped;\n}\n\nint64_t hdr_add_while_correcting_for_coordinated_omission(\n        struct hdr_histogram* h, struct hdr_histogram* from, int64_t expected_interval)\n{\n    struct hdr_iter iter;\n    int64_t dropped = 0;\n    hdr_iter_recorded_init(&iter, from);\n\n    while (hdr_iter_next(&iter))\n    {\n        int64_t value = iter.value;\n        int64_t count = iter.count;\n\n        if (!hdr_record_corrected_values(h, value, count, expected_interval))\n        {\n            dropped += count;\n        }\n    }\n\n    return dropped;\n}\n\n\n\n/* ##     ##    ###    ##       ##     ## ########  ######  */\n/* ##     ##   ## ##   ##       ##     ## ##       ##    ## */\n/* ##     ##  ##   ##  ##       ##     ## ##       ##       */\n/* ##     ## ##     ## ##       ##     ## ######    ######  */\n/*  ##   ##  ######### ##       ##     ## ##             ## */\n/*   ## ##   ##     ## ##       ##     ## ##       ##    ## */\n/*    ###    ##     ## ########  #######  ########  ######  */\n\n\nint64_t hdr_max(const struct hdr_histogram* h)\n{\n    if (0 == h->max_value)\n    {\n        return 0;\n    }\n\n    return highest_equivalent_value(h, h->max_value);\n}\n\nint64_t hdr_min(const struct hdr_histogram* h)\n{\n    if (0 < hdr_count_at_index(h, 0))\n    {\n        return 0;\n    }\n\n    return non_zero_min(h);\n}\n\nint64_t hdr_value_at_percentile(const struct hdr_histogram* h, double percentile)\n{\n    struct hdr_iter iter;\n    int64_t total = 0;\n    double requested_percentile = percentile < 100.0 ? percentile : 100.0;\n    int64_t count_at_percentile =\n        (int64_t) (((requested_percentile / 100) * h->total_count) + 0.5);\n    count_at_percentile = count_at_percentile > 1 ? count_at_percentile : 1;\n\n    hdr_iter_init(&iter, h);\n\n    while (hdr_iter_next(&iter))\n    {\n        total += iter.count;\n\n        if (total >= count_at_percentile)\n        {\n            int64_t value_from_index = iter.value;\n            return highest_equivalent_value(h, value_from_index);\n        }\n    }\n\n    return 0;\n}\n\ndouble hdr_mean(const struct hdr_histogram* h)\n{\n    struct hdr_iter iter;\n    int64_t total = 0;\n\n    hdr_iter_init(&iter, h);\n\n    while (hdr_iter_next(&iter))\n    {\n        if (0 != iter.count)\n        {\n            total += iter.count * hdr_median_equivalent_value(h, iter.value);\n        }\n    }\n\n    return (total * 1.0) / h->total_count;\n}\n\ndouble hdr_stddev(const struct hdr_histogram* h)\n{\n    double mean = hdr_mean(h);\n    double geometric_dev_total = 0.0;\n\n    struct hdr_iter iter;\n    hdr_iter_init(&iter, h);\n\n    while (hdr_iter_next(&iter))\n    {\n        if (0 != iter.count)\n        {\n            double dev = (hdr_median_equivalent_value(h, iter.value) * 1.0) - mean;\n            geometric_dev_total += (dev * dev) * iter.count;\n        }\n    }\n\n    return sqrt(geometric_dev_total / h->total_count);\n}\n\nbool hdr_values_are_equivalent(const struct hdr_histogram* h, int64_t a, int64_t b)\n{\n    return lowest_equivalent_value(h, a) == lowest_equivalent_value(h, b);\n}\n\nint64_t hdr_lowest_equivalent_value(const struct hdr_histogram* h, int64_t value)\n{\n    return lowest_equivalent_value(h, value);\n}\n\nint64_t hdr_count_at_value(const struct hdr_histogram* h, int64_t value)\n{\n    return counts_get_normalised(h, counts_index_for(h, value));\n}\n\nint64_t hdr_count_at_index(const struct hdr_histogram* h, int32_t index)\n{\n    return counts_get_normalised(h, index);\n}\n\n\n/* #### ######## ######## ########     ###    ########  #######  ########   ######  */\n/*  ##     ##    ##       ##     ##   ## ##      ##    ##     ## ##     ## ##    ## */\n/*  ##     ##    ##       ##     ##  ##   ##     ##    ##     ## ##     ## ##       */\n/*  ##     ##    ######   ########  ##     ##    ##    ##     ## ########   ######  */\n/*  ##     ##    ##       ##   ##   #########    ##    ##     ## ##   ##         ## */\n/*  ##     ##    ##       ##    ##  ##     ##    ##    ##     ## ##    ##  ##    ## */\n/* ####    ##    ######## ##     ## ##     ##    ##     #######  ##     ##  ######  */\n\n\nstatic bool has_buckets(struct hdr_iter* iter)\n{\n    return iter->counts_index < iter->h->counts_len;\n}\n\nstatic bool has_next(struct hdr_iter* iter)\n{\n    return iter->cumulative_count < iter->total_count;\n}\n\nstatic bool move_next(struct hdr_iter* iter)\n{\n    iter->counts_index++;\n\n    if (!has_buckets(iter))\n    {\n        return false;\n    }\n\n    iter->count = counts_get_normalised(iter->h, iter->counts_index);\n    iter->cumulative_count += iter->count;\n\n    iter->value = hdr_value_at_index(iter->h, iter->counts_index);\n    iter->highest_equivalent_value = highest_equivalent_value(iter->h, iter->value);\n    iter->lowest_equivalent_value = lowest_equivalent_value(iter->h, iter->value);\n    iter->median_equivalent_value = hdr_median_equivalent_value(iter->h, iter->value);\n\n    return true;\n}\n\nstatic int64_t peek_next_value_from_index(struct hdr_iter* iter)\n{\n    return hdr_value_at_index(iter->h, iter->counts_index + 1);\n}\n\nstatic bool next_value_greater_than_reporting_level_upper_bound(\n    struct hdr_iter *iter, int64_t reporting_level_upper_bound)\n{\n    if (iter->counts_index >= iter->h->counts_len)\n    {\n        return false;\n    }\n\n    return peek_next_value_from_index(iter) > reporting_level_upper_bound;\n}\n\nstatic bool basic_iter_next(struct hdr_iter *iter)\n{\n    if (!has_next(iter) || iter->counts_index >= iter->h->counts_len)\n    {\n        return false;\n    }\n\n    move_next(iter);\n\n    return true;\n}\n\nstatic void update_iterated_values(struct hdr_iter* iter, int64_t new_value_iterated_to)\n{\n    iter->value_iterated_from = iter->value_iterated_to;\n    iter->value_iterated_to = new_value_iterated_to;\n}\n\nstatic bool all_values_iter_next(struct hdr_iter* iter)\n{\n    bool result = move_next(iter);\n\n    if (result)\n    {\n        update_iterated_values(iter, iter->value);\n    }\n\n    return result;\n}\n\nvoid hdr_iter_init(struct hdr_iter* iter, const struct hdr_histogram* h)\n{\n    iter->h = h;\n\n    iter->counts_index = -1;\n    iter->total_count = h->total_count;\n    iter->count = 0;\n    iter->cumulative_count = 0;\n    iter->value = 0;\n    iter->highest_equivalent_value = 0;\n    iter->value_iterated_from = 0;\n    iter->value_iterated_to = 0;\n\n    iter->_next_fp = all_values_iter_next;\n}\n\nbool hdr_iter_next(struct hdr_iter* iter)\n{\n    return iter->_next_fp(iter);\n}\n\n/* ########  ######## ########   ######  ######## ##    ## ######## #### ##       ########  ######  */\n/* ##     ## ##       ##     ## ##    ## ##       ###   ##    ##     ##  ##       ##       ##    ## */\n/* ##     ## ##       ##     ## ##       ##       ####  ##    ##     ##  ##       ##       ##       */\n/* ########  ######   ########  ##       ######   ## ## ##    ##     ##  ##       ######    ######  */\n/* ##        ##       ##   ##   ##       ##       ##  ####    ##     ##  ##       ##             ## */\n/* ##        ##       ##    ##  ##    ## ##       ##   ###    ##     ##  ##       ##       ##    ## */\n/* ##        ######## ##     ##  ######  ######## ##    ##    ##    #### ######## ########  ######  */\n\nstatic bool percentile_iter_next(struct hdr_iter* iter)\n{\n    int64_t temp, half_distance, percentile_reporting_ticks;\n\n    struct hdr_iter_percentiles* percentiles = &iter->specifics.percentiles;\n\n    if (!has_next(iter))\n    {\n        if (percentiles->seen_last_value)\n        {\n            return false;\n        }\n\n        percentiles->seen_last_value = true;\n        percentiles->percentile = 100.0;\n\n        return true;\n    }\n\n    if (iter->counts_index == -1 && !basic_iter_next(iter))\n    {\n        return false;\n    }\n\n    do\n    {\n        double current_percentile = (100.0 * (double) iter->cumulative_count) / iter->h->total_count;\n        if (iter->count != 0 &&\n                percentiles->percentile_to_iterate_to <= current_percentile)\n        {\n            update_iterated_values(iter, highest_equivalent_value(iter->h, iter->value));\n\n            percentiles->percentile = percentiles->percentile_to_iterate_to;\n            temp = (int64_t)(log(100 / (100.0 - (percentiles->percentile_to_iterate_to))) / log(2)) + 1;\n            half_distance = (int64_t) pow(2, (double) temp);\n            percentile_reporting_ticks = percentiles->ticks_per_half_distance * half_distance;\n            percentiles->percentile_to_iterate_to += 100.0 / percentile_reporting_ticks;\n\n            return true;\n        }\n    }\n    while (basic_iter_next(iter));\n\n    return true;\n}\n\nvoid hdr_iter_percentile_init(struct hdr_iter* iter, const struct hdr_histogram* h, int32_t ticks_per_half_distance)\n{\n    iter->h = h;\n\n    hdr_iter_init(iter, h);\n\n    iter->specifics.percentiles.seen_last_value          = false;\n    iter->specifics.percentiles.ticks_per_half_distance  = ticks_per_half_distance;\n    iter->specifics.percentiles.percentile_to_iterate_to = 0.0;\n    iter->specifics.percentiles.percentile               = 0.0;\n\n    iter->_next_fp = percentile_iter_next;\n}\n\nstatic void format_line_string(char* str, size_t len, int significant_figures, format_type format)\n{\n#if defined(_MSC_VER)\n#define snprintf _snprintf\n#pragma warning(push)\n#pragma warning(disable: 4996)\n#endif\n    const char* format_str = \"%s%d%s\";\n\n    switch (format)\n    {\n        case CSV:\n            snprintf(str, len, format_str, \"%.\", significant_figures, \"f,%f,%d,%.2f\\n\");\n            break;\n        case CLASSIC:\n            snprintf(str, len, format_str, \"%12.\", significant_figures, \"f %12f %12d %12.2f\\n\");\n            break;\n        default:\n            snprintf(str, len, format_str, \"%12.\", significant_figures, \"f %12f %12d %12.2f\\n\");\n    }\n#if defined(_MSC_VER)\n#undef snprintf\n#pragma warning(pop)\n#endif\n}\n\n\n/* ########  ########  ######   #######  ########  ########  ######## ########   */\n/* ##     ## ##       ##    ## ##     ## ##     ## ##     ## ##       ##     ##  */\n/* ##     ## ##       ##       ##     ## ##     ## ##     ## ##       ##     ##  */\n/* ########  ######   ##       ##     ## ########  ##     ## ######   ##     ##  */\n/* ##   ##   ##       ##       ##     ## ##   ##   ##     ## ##       ##     ##  */\n/* ##    ##  ##       ##    ## ##     ## ##    ##  ##     ## ##       ##     ##  */\n/* ##     ## ########  ######   #######  ##     ## ########  ######## ########   */\n\n\nstatic bool recorded_iter_next(struct hdr_iter* iter)\n{\n    while (basic_iter_next(iter))\n    {\n        if (iter->count != 0)\n        {\n            update_iterated_values(iter, iter->value);\n\n            iter->specifics.recorded.count_added_in_this_iteration_step = iter->count;\n            return true;\n        }\n    }\n\n    return false;\n}\n\nvoid hdr_iter_recorded_init(struct hdr_iter* iter, const struct hdr_histogram* h)\n{\n    hdr_iter_init(iter, h);\n\n    iter->specifics.recorded.count_added_in_this_iteration_step = 0;\n\n    iter->_next_fp = recorded_iter_next;\n}\n\n/* ##       #### ##    ## ########    ###    ########  */\n/* ##        ##  ###   ## ##         ## ##   ##     ## */\n/* ##        ##  ####  ## ##        ##   ##  ##     ## */\n/* ##        ##  ## ## ## ######   ##     ## ########  */\n/* ##        ##  ##  #### ##       ######### ##   ##   */\n/* ##        ##  ##   ### ##       ##     ## ##    ##  */\n/* ######## #### ##    ## ######## ##     ## ##     ## */\n\n\nstatic bool iter_linear_next(struct hdr_iter* iter)\n{\n    struct hdr_iter_linear* linear = &iter->specifics.linear;\n\n    linear->count_added_in_this_iteration_step = 0;\n\n    if (has_next(iter) ||\n        next_value_greater_than_reporting_level_upper_bound(\n            iter, linear->next_value_reporting_level_lowest_equivalent))\n    {\n        do\n        {\n            if (iter->value >= linear->next_value_reporting_level_lowest_equivalent)\n            {\n                update_iterated_values(iter, linear->next_value_reporting_level);\n\n                linear->next_value_reporting_level += linear->value_units_per_bucket;\n                linear->next_value_reporting_level_lowest_equivalent =\n                    lowest_equivalent_value(iter->h, linear->next_value_reporting_level);\n\n                return true;\n            }\n\n            if (!move_next(iter))\n            {\n                return true;\n            }\n\n            linear->count_added_in_this_iteration_step += iter->count;\n        }\n        while (true);\n    }\n\n    return false;\n}\n\n\nvoid hdr_iter_linear_init(struct hdr_iter* iter, const struct hdr_histogram* h, int64_t value_units_per_bucket)\n{\n    hdr_iter_init(iter, h);\n\n    iter->specifics.linear.count_added_in_this_iteration_step = 0;\n    iter->specifics.linear.value_units_per_bucket = value_units_per_bucket;\n    iter->specifics.linear.next_value_reporting_level = value_units_per_bucket;\n    iter->specifics.linear.next_value_reporting_level_lowest_equivalent = lowest_equivalent_value(h, value_units_per_bucket);\n\n    iter->_next_fp = iter_linear_next;\n}\n\nvoid hdr_iter_linear_set_value_units_per_bucket(struct hdr_iter* iter, int64_t value_units_per_bucket)\n{\n    iter->specifics.linear.value_units_per_bucket = value_units_per_bucket;\n}\n\n/* ##        #######   ######      ###    ########  #### ######## ##     ## ##     ## ####  ######  */\n/* ##       ##     ## ##    ##    ## ##   ##     ##  ##     ##    ##     ## ###   ###  ##  ##    ## */\n/* ##       ##     ## ##         ##   ##  ##     ##  ##     ##    ##     ## #### ####  ##  ##       */\n/* ##       ##     ## ##   #### ##     ## ########   ##     ##    ######### ## ### ##  ##  ##       */\n/* ##       ##     ## ##    ##  ######### ##   ##    ##     ##    ##     ## ##     ##  ##  ##       */\n/* ##       ##     ## ##    ##  ##     ## ##    ##   ##     ##    ##     ## ##     ##  ##  ##    ## */\n/* ########  #######   ######   ##     ## ##     ## ####    ##    ##     ## ##     ## ####  ######  */\n\nstatic bool log_iter_next(struct hdr_iter *iter)\n{\n    struct hdr_iter_log* logarithmic = &iter->specifics.log;\n\n    logarithmic->count_added_in_this_iteration_step = 0;\n\n    if (has_next(iter) ||\n        next_value_greater_than_reporting_level_upper_bound(\n            iter, logarithmic->next_value_reporting_level_lowest_equivalent))\n    {\n        do\n        {\n            if (iter->value >= logarithmic->next_value_reporting_level_lowest_equivalent)\n            {\n                update_iterated_values(iter, logarithmic->next_value_reporting_level);\n\n                logarithmic->next_value_reporting_level *= (int64_t)logarithmic->log_base;\n                logarithmic->next_value_reporting_level_lowest_equivalent = lowest_equivalent_value(iter->h, logarithmic->next_value_reporting_level);\n\n                return true;\n            }\n\n            if (!move_next(iter))\n            {\n                return true;\n            }\n\n            logarithmic->count_added_in_this_iteration_step += iter->count;\n        }\n        while (true);\n    }\n\n    return false;\n}\n\nvoid hdr_iter_log_init(\n        struct hdr_iter* iter,\n        const struct hdr_histogram* h,\n        int64_t value_units_first_bucket,\n        double log_base)\n{\n    hdr_iter_init(iter, h);\n    iter->specifics.log.count_added_in_this_iteration_step = 0;\n    iter->specifics.log.log_base = log_base;\n    iter->specifics.log.next_value_reporting_level = value_units_first_bucket;\n    iter->specifics.log.next_value_reporting_level_lowest_equivalent = lowest_equivalent_value(h, value_units_first_bucket);\n\n    iter->_next_fp = log_iter_next;\n}\n\n/* Printing. */\n\nstatic const char* format_head_string(format_type format)\n{\n    switch (format)\n    {\n        case CSV:\n            return \"%s,%s,%s,%s\\n\";\n        case CLASSIC:\n        default:\n            return \"%12s %12s %12s %12s\\n\\n\";\n    }\n}\n\nstatic const char CLASSIC_FOOTER[] =\n    \"#[Mean    = %12.3f, StdDeviation   = %12.3f]\\n\"\n    \"#[Max     = %12.3f, Total count    = %12\" PRIu64 \"]\\n\"\n    \"#[Buckets = %12d, SubBuckets     = %12d]\\n\";\n\nint hdr_percentiles_print(\n        struct hdr_histogram* h, FILE* stream, int32_t ticks_per_half_distance,\n        double value_scale, format_type format)\n{\n    char line_format[25];\n    const char* head_format;\n    int rc = 0;\n    struct hdr_iter iter;\n    struct hdr_iter_percentiles * percentiles;\n\n    format_line_string(line_format, 25, h->significant_figures, format);\n    head_format = format_head_string(format);\n\n    hdr_iter_percentile_init(&iter, h, ticks_per_half_distance);\n\n    if (fprintf(\n            stream, head_format,\n            \"Value\", \"Percentile\", \"TotalCount\", \"1/(1-Percentile)\") < 0)\n    {\n        rc = EIO;\n        goto cleanup;\n    }\n\n    percentiles = &iter.specifics.percentiles;\n    while (hdr_iter_next(&iter))\n    {\n        double  value               = iter.highest_equivalent_value / value_scale;\n        double  percentile          = percentiles->percentile / 100.0;\n        int64_t total_count         = iter.cumulative_count;\n        double  inverted_percentile = (1.0 / (1.0 - percentile));\n\n        if (fprintf(\n                stream, line_format, value, percentile, total_count, inverted_percentile) < 0)\n        {\n            rc = EIO;\n            goto cleanup;\n        }\n    }\n\n    if (CLASSIC == format)\n    {\n        double mean   = hdr_mean(h)   / value_scale;\n        double stddev = hdr_stddev(h) / value_scale;\n        double max    = hdr_max(h)    / value_scale;\n\n        if (fprintf(\n                stream, CLASSIC_FOOTER,  mean, stddev, max,\n                h->total_count, h->bucket_count, h->sub_bucket_count) < 0)\n        {\n            rc = EIO;\n            goto cleanup;\n        }\n    }\n\n    cleanup:\n    return rc;\n}\n"
  },
  {
    "path": "deps/hdr_histogram/hdr_histogram.h",
    "content": "/**\n * hdr_histogram.h\n * Written by Michael Barker and released to the public domain,\n * as explained at http://creativecommons.org/publicdomain/zero/1.0/\n *\n * The source for the hdr_histogram utilises a few C99 constructs, specifically\n * the use of stdint/stdbool and inline variable declaration.\n */\n\n#ifndef HDR_HISTOGRAM_H\n#define HDR_HISTOGRAM_H 1\n\n#include <stdint.h>\n#include <stdbool.h>\n#include <stdio.h>\n\nstruct hdr_histogram\n{\n    int64_t lowest_trackable_value;\n    int64_t highest_trackable_value;\n    int32_t unit_magnitude;\n    int32_t significant_figures;\n    int32_t sub_bucket_half_count_magnitude;\n    int32_t sub_bucket_half_count;\n    int64_t sub_bucket_mask;\n    int32_t sub_bucket_count;\n    int32_t bucket_count;\n    int64_t min_value;\n    int64_t max_value;\n    int32_t normalizing_index_offset;\n    double conversion_ratio;\n    int32_t counts_len;\n    int64_t total_count;\n    int64_t* counts;\n};\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/**\n * Allocate the memory and initialise the hdr_histogram.\n *\n * Due to the size of the histogram being the result of some reasonably\n * involved math on the input parameters this function it is tricky to stack allocate.\n * The histogram should be released with hdr_close\n *\n * @param lowest_trackable_value The smallest possible value to be put into the\n * histogram.\n * @param highest_trackable_value The largest possible value to be put into the\n * histogram.\n * @param significant_figures The level of precision for this histogram, i.e. the number\n * of figures in a decimal number that will be maintained.  E.g. a value of 3 will mean\n * the results from the histogram will be accurate up to the first three digits.  Must\n * be a value between 1 and 5 (inclusive).\n * @param result Output parameter to capture allocated histogram.\n * @return 0 on success, EINVAL if lowest_trackable_value is < 1 or the\n * significant_figure value is outside of the allowed range, ENOMEM if malloc\n * failed.\n */\nint hdr_init(\n    int64_t lowest_trackable_value,\n    int64_t highest_trackable_value,\n    int significant_figures,\n    struct hdr_histogram** result);\n\n/**\n * Free the memory and close the hdr_histogram.\n *\n * @param h The histogram you want to close.\n */\nvoid hdr_close(struct hdr_histogram* h);\n\n/**\n * Allocate the memory and initialise the hdr_histogram.  This is the equivalent of calling\n * hdr_init(1, highest_trackable_value, significant_figures, result);\n *\n * @deprecated use hdr_init.\n */\nint hdr_alloc(int64_t highest_trackable_value, int significant_figures, struct hdr_histogram** result);\n\n\n/**\n * Reset a histogram to zero - empty out a histogram and re-initialise it\n *\n * If you want to re-use an existing histogram, but reset everything back to zero, this\n * is the routine to use.\n *\n * @param h The histogram you want to reset to empty.\n *\n */\nvoid hdr_reset(struct hdr_histogram* h);\n\n/**\n * Get the memory size of the hdr_histogram.\n *\n * @param h \"This\" pointer\n * @return The amount of memory used by the hdr_histogram in bytes\n */\nsize_t hdr_get_memory_size(struct hdr_histogram* h);\n\n/**\n * Records a value in the histogram, will round this value of to a precision at or better\n * than the significant_figure specified at construction time.\n *\n * @param h \"This\" pointer\n * @param value Value to add to the histogram\n * @return false if the value is larger than the highest_trackable_value and can't be recorded,\n * true otherwise.\n */\nbool hdr_record_value(struct hdr_histogram* h, int64_t value);\n\n/**\n * Records a value in the histogram, will round this value of to a precision at or better\n * than the significant_figure specified at construction time.\n *\n * Will record this value atomically, however the whole structure may appear inconsistent\n * when read concurrently with this update.  Do NOT mix calls to this method with calls\n * to non-atomic updates.\n *\n * @param h \"This\" pointer\n * @param value Value to add to the histogram\n * @return false if the value is larger than the highest_trackable_value and can't be recorded,\n * true otherwise.\n */\nbool hdr_record_value_atomic(struct hdr_histogram* h, int64_t value);\n\n/**\n * Records count values in the histogram, will round this value of to a\n * precision at or better than the significant_figure specified at construction\n * time.\n *\n * @param h \"This\" pointer\n * @param value Value to add to the histogram\n * @param count Number of 'value's to add to the histogram\n * @return false if any value is larger than the highest_trackable_value and can't be recorded,\n * true otherwise.\n */\nbool hdr_record_values(struct hdr_histogram* h, int64_t value, int64_t count);\n\n/**\n * Records count values in the histogram, will round this value of to a\n * precision at or better than the significant_figure specified at construction\n * time.\n *\n * Will record this value atomically, however the whole structure may appear inconsistent\n * when read concurrently with this update.  Do NOT mix calls to this method with calls\n * to non-atomic updates.\n *\n * @param h \"This\" pointer\n * @param value Value to add to the histogram\n * @param count Number of 'value's to add to the histogram\n * @return false if any value is larger than the highest_trackable_value and can't be recorded,\n * true otherwise.\n */\nbool hdr_record_values_atomic(struct hdr_histogram* h, int64_t value, int64_t count);\n\n/**\n * Record a value in the histogram and backfill based on an expected interval.\n *\n * Records a value in the histogram, will round this value of to a precision at or better\n * than the significant_figure specified at contruction time.  This is specifically used\n * for recording latency.  If the value is larger than the expected_interval then the\n * latency recording system has experienced co-ordinated omission.  This method fills in the\n * values that would have occured had the client providing the load not been blocked.\n\n * @param h \"This\" pointer\n * @param value Value to add to the histogram\n * @param expected_interval The delay between recording values.\n * @return false if the value is larger than the highest_trackable_value and can't be recorded,\n * true otherwise.\n */\nbool hdr_record_corrected_value(struct hdr_histogram* h, int64_t value, int64_t expexcted_interval);\n\n/**\n * Record a value in the histogram and backfill based on an expected interval.\n *\n * Records a value in the histogram, will round this value of to a precision at or better\n * than the significant_figure specified at contruction time.  This is specifically used\n * for recording latency.  If the value is larger than the expected_interval then the\n * latency recording system has experienced co-ordinated omission.  This method fills in the\n * values that would have occured had the client providing the load not been blocked.\n *\n * Will record this value atomically, however the whole structure may appear inconsistent\n * when read concurrently with this update.  Do NOT mix calls to this method with calls\n * to non-atomic updates.\n *\n * @param h \"This\" pointer\n * @param value Value to add to the histogram\n * @param expected_interval The delay between recording values.\n * @return false if the value is larger than the highest_trackable_value and can't be recorded,\n * true otherwise.\n */\nbool hdr_record_corrected_value_atomic(struct hdr_histogram* h, int64_t value, int64_t expexcted_interval);\n\n/**\n * Record a value in the histogram 'count' times.  Applies the same correcting logic\n * as 'hdr_record_corrected_value'.\n *\n * @param h \"This\" pointer\n * @param value Value to add to the histogram\n * @param count Number of 'value's to add to the histogram\n * @param expected_interval The delay between recording values.\n * @return false if the value is larger than the highest_trackable_value and can't be recorded,\n * true otherwise.\n */\nbool hdr_record_corrected_values(struct hdr_histogram* h, int64_t value, int64_t count, int64_t expected_interval);\n\n/**\n * Record a value in the histogram 'count' times.  Applies the same correcting logic\n * as 'hdr_record_corrected_value'.\n *\n * Will record this value atomically, however the whole structure may appear inconsistent\n * when read concurrently with this update.  Do NOT mix calls to this method with calls\n * to non-atomic updates.\n *\n * @param h \"This\" pointer\n * @param value Value to add to the histogram\n * @param count Number of 'value's to add to the histogram\n * @param expected_interval The delay between recording values.\n * @return false if the value is larger than the highest_trackable_value and can't be recorded,\n * true otherwise.\n */\nbool hdr_record_corrected_values_atomic(struct hdr_histogram* h, int64_t value, int64_t count, int64_t expected_interval);\n\n/**\n * Adds all of the values from 'from' to 'this' histogram.  Will return the\n * number of values that are dropped when copying.  Values will be dropped\n * if they around outside of h.lowest_trackable_value and\n * h.highest_trackable_value.\n *\n * @param h \"This\" pointer\n * @param from Histogram to copy values from.\n * @return The number of values dropped when copying.\n */\nint64_t hdr_add(struct hdr_histogram* h, const struct hdr_histogram* from);\n\n/**\n * Adds all of the values from 'from' to 'this' histogram.  Will return the\n * number of values that are dropped when copying.  Values will be dropped\n * if they around outside of h.lowest_trackable_value and\n * h.highest_trackable_value.\n *\n * @param h \"This\" pointer\n * @param from Histogram to copy values from.\n * @return The number of values dropped when copying.\n */\nint64_t hdr_add_while_correcting_for_coordinated_omission(\n    struct hdr_histogram* h, struct hdr_histogram* from, int64_t expected_interval);\n\n/**\n * Get minimum value from the histogram.  Will return 2^63-1 if the histogram\n * is empty.\n *\n * @param h \"This\" pointer\n */\nint64_t hdr_min(const struct hdr_histogram* h);\n\n/**\n * Get maximum value from the histogram.  Will return 0 if the histogram\n * is empty.\n *\n * @param h \"This\" pointer\n */\nint64_t hdr_max(const struct hdr_histogram* h);\n\n/**\n * Get the value at a specific percentile.\n *\n * @param h \"This\" pointer.\n * @param percentile The percentile to get the value for\n */\nint64_t hdr_value_at_percentile(const struct hdr_histogram* h, double percentile);\n\n/**\n * Gets the standard deviation for the values in the histogram.\n *\n * @param h \"This\" pointer\n * @return The standard deviation\n */\ndouble hdr_stddev(const struct hdr_histogram* h);\n\n/**\n * Gets the mean for the values in the histogram.\n *\n * @param h \"This\" pointer\n * @return The mean\n */\ndouble hdr_mean(const struct hdr_histogram* h);\n\n/**\n * Determine if two values are equivalent with the histogram's resolution.\n * Where \"equivalent\" means that value samples recorded for any two\n * equivalent values are counted in a common total count.\n *\n * @param h \"This\" pointer\n * @param a first value to compare\n * @param b second value to compare\n * @return 'true' if values are equivalent with the histogram's resolution.\n */\nbool hdr_values_are_equivalent(const struct hdr_histogram* h, int64_t a, int64_t b);\n\n/**\n * Get the lowest value that is equivalent to the given value within the histogram's resolution.\n * Where \"equivalent\" means that value samples recorded for any two\n * equivalent values are counted in a common total count.\n *\n * @param h \"This\" pointer\n * @param value The given value\n * @return The lowest value that is equivalent to the given value within the histogram's resolution.\n */\nint64_t hdr_lowest_equivalent_value(const struct hdr_histogram* h, int64_t value);\n\n/**\n * Get the count of recorded values at a specific value\n * (to within the histogram resolution at the value level).\n *\n * @param h \"This\" pointer\n * @param value The value for which to provide the recorded count\n * @return The total count of values recorded in the histogram within the value range that is\n * {@literal >=} lowestEquivalentValue(<i>value</i>) and {@literal <=} highestEquivalentValue(<i>value</i>)\n */\nint64_t hdr_count_at_value(const struct hdr_histogram* h, int64_t value);\n\nint64_t hdr_count_at_index(const struct hdr_histogram* h, int32_t index);\n\nint64_t hdr_value_at_index(const struct hdr_histogram* h, int32_t index);\n\nstruct hdr_iter_percentiles\n{\n    bool seen_last_value;\n    int32_t ticks_per_half_distance;\n    double percentile_to_iterate_to;\n    double percentile;\n};\n\nstruct hdr_iter_recorded\n{\n    int64_t count_added_in_this_iteration_step;\n};\n\nstruct hdr_iter_linear\n{\n    int64_t value_units_per_bucket;\n    int64_t count_added_in_this_iteration_step;\n    int64_t next_value_reporting_level;\n    int64_t next_value_reporting_level_lowest_equivalent;\n};\n\nstruct hdr_iter_log\n{\n    double log_base;\n    int64_t count_added_in_this_iteration_step;\n    int64_t next_value_reporting_level;\n    int64_t next_value_reporting_level_lowest_equivalent;\n};\n\n/**\n * The basic iterator.  This is a generic structure\n * that supports all of the types of iteration.  Use\n * the appropriate initialiser to get the desired\n * iteration.\n *\n * @\n */\nstruct hdr_iter\n{\n    const struct hdr_histogram* h;\n    /** raw index into the counts array */\n    int32_t counts_index;\n    /** snapshot of the length at the time the iterator is created */\n    int64_t total_count;\n    /** value directly from array for the current counts_index */\n    int64_t count;\n    /** sum of all of the counts up to and including the count at this index */\n    int64_t cumulative_count;\n    /** The current value based on counts_index */\n    int64_t value;\n    int64_t highest_equivalent_value;\n    int64_t lowest_equivalent_value;\n    int64_t median_equivalent_value;\n    int64_t value_iterated_from;\n    int64_t value_iterated_to;\n\n    union\n    {\n        struct hdr_iter_percentiles percentiles;\n        struct hdr_iter_recorded recorded;\n        struct hdr_iter_linear linear;\n        struct hdr_iter_log log;\n    } specifics;\n\n    bool (* _next_fp)(struct hdr_iter* iter);\n\n};\n\n/**\n * Initalises the basic iterator.\n *\n * @param itr 'This' pointer\n * @param h The histogram to iterate over\n */\nvoid hdr_iter_init(struct hdr_iter* iter, const struct hdr_histogram* h);\n\n/**\n * Initialise the iterator for use with percentiles.\n */\nvoid hdr_iter_percentile_init(struct hdr_iter* iter, const struct hdr_histogram* h, int32_t ticks_per_half_distance);\n\n/**\n * Initialise the iterator for use with recorded values.\n */\nvoid hdr_iter_recorded_init(struct hdr_iter* iter, const struct hdr_histogram* h);\n\n/**\n * Initialise the iterator for use with linear values.\n */\nvoid hdr_iter_linear_init(\n    struct hdr_iter* iter,\n    const struct hdr_histogram* h,\n    int64_t value_units_per_bucket);\n\n/**\n * Update the iterator value units per bucket\n */\nvoid hdr_iter_linear_set_value_units_per_bucket(struct hdr_iter* iter, int64_t value_units_per_bucket);\n\n/**\n * Initialise the iterator for use with logarithmic values\n */\nvoid hdr_iter_log_init(\n    struct hdr_iter* iter,\n    const struct hdr_histogram* h,\n    int64_t value_units_first_bucket,\n    double log_base);\n\n/**\n * Iterate to the next value for the iterator.  If there are no more values\n * available return faluse.\n *\n * @param itr 'This' pointer\n * @return 'false' if there are no values remaining for this iterator.\n */\nbool hdr_iter_next(struct hdr_iter* iter);\n\ntypedef enum\n{\n    CLASSIC,\n    CSV\n} format_type;\n\n/**\n * Print out a percentile based histogram to the supplied stream.  Note that\n * this call will not flush the FILE, this is left up to the user.\n *\n * @param h 'This' pointer\n * @param stream The FILE to write the output to\n * @param ticks_per_half_distance The number of iteration steps per half-distance to 100%\n * @param value_scale Scale the output values by this amount\n * @param format_type Format to use, e.g. CSV.\n * @return 0 on success, error code on failure.  EIO if an error occurs writing\n * the output.\n */\nint hdr_percentiles_print(\n    struct hdr_histogram* h, FILE* stream, int32_t ticks_per_half_distance,\n    double value_scale, format_type format);\n\n/**\n* Internal allocation methods, used by hdr_dbl_histogram.\n*/\nstruct hdr_histogram_bucket_config\n{\n    int64_t lowest_trackable_value;\n    int64_t highest_trackable_value;\n    int64_t unit_magnitude;\n    int64_t significant_figures;\n    int32_t sub_bucket_half_count_magnitude;\n    int32_t sub_bucket_half_count;\n    int64_t sub_bucket_mask;\n    int32_t sub_bucket_count;\n    int32_t bucket_count;\n    int32_t counts_len;\n};\n\nint hdr_calculate_bucket_config(\n    int64_t lowest_trackable_value,\n    int64_t highest_trackable_value,\n    int significant_figures,\n    struct hdr_histogram_bucket_config* cfg);\n\nvoid hdr_init_preallocated(struct hdr_histogram* h, struct hdr_histogram_bucket_config* cfg);\n\nint64_t hdr_size_of_equivalent_value_range(const struct hdr_histogram* h, int64_t value);\n\nint64_t hdr_next_non_equivalent_value(const struct hdr_histogram* h, int64_t value);\n\nint64_t hdr_median_equivalent_value(const struct hdr_histogram* h, int64_t value);\n\n/**\n * Used to reset counters after importing data manuallying into the histogram, used by the logging code\n * and other custom serialisation tools.\n */\nvoid hdr_reset_internal_counters(struct hdr_histogram* h);\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n"
  },
  {
    "path": "deps/hiredis/.gitignore",
    "content": "/hiredis-test\n/examples/hiredis-example*\n/*.o\n/*.so\n/*.dylib\n/*.a\n/*.pc\n*.dSYM\ntags\n"
  },
  {
    "path": "deps/hiredis/.travis.yml",
    "content": "language: c\ncompiler:\n  - gcc\n  - clang\n\nos:\n  - linux\n  - osx\n\ndist: bionic\n\nbranches:\n  only:\n    - staging\n    - trying\n    - master\n    - /^release\\/.*$/\n\ninstall:\n    - if [ \"$BITS\" == \"64\" ]; then\n        wget https://github.com/redis/redis/archive/6.0.6.tar.gz;\n        tar -xzvf 6.0.6.tar.gz;\n        pushd redis-6.0.6 && BUILD_TLS=yes make && export PATH=$PWD/src:$PATH && popd;\n      fi\n\nbefore_script:\n    - if [ \"$TRAVIS_OS_NAME\" == \"osx\" ]; then\n        curl -O https://distfiles.macports.org/MacPorts/MacPorts-2.6.2-10.13-HighSierra.pkg;\n        sudo installer -pkg MacPorts-2.6.2-10.13-HighSierra.pkg -target /;\n        export PATH=$PATH:/opt/local/bin && sudo port -v selfupdate;\n        sudo port -N install openssl redis;\n      fi;\n\naddons:\n  apt:\n    sources:\n    - sourceline: 'ppa:chris-lea/redis-server'\n    packages:\n    - libc6-dbg\n    - libc6-dev\n    - libc6:i386\n    - libc6-dev-i386\n    - libc6-dbg:i386\n    - gcc-multilib\n    - g++-multilib\n    - libssl-dev\n    - libssl-dev:i386\n    - valgrind\n    - redis\n\nenv:\n  - BITS=\"32\"\n  - BITS=\"64\"\n\nscript:\n  - EXTRA_CMAKE_OPTS=\"-DENABLE_EXAMPLES:BOOL=ON -DENABLE_SSL:BOOL=ON\";\n    if [ \"$BITS\" == \"64\" ]; then\n      EXTRA_CMAKE_OPTS=\"$EXTRA_CMAKE_OPTS -DENABLE_SSL_TESTS:BOOL=ON\";\n    fi;\n    if [ \"$TRAVIS_OS_NAME\" == \"osx\" ]; then\n      if [ \"$BITS\" == \"32\" ]; then\n        CFLAGS=\"-m32 -Werror\";\n        CXXFLAGS=\"-m32 -Werror\";\n        LDFLAGS=\"-m32\";\n        EXTRA_CMAKE_OPTS=;\n      else\n        CFLAGS=\"-Werror\";\n        CXXFLAGS=\"-Werror\";\n      fi;\n    else\n      TEST_PREFIX=\"valgrind --track-origins=yes --leak-check=full\";\n      if [ \"$BITS\" == \"32\" ]; then\n        CFLAGS=\"-m32 -Werror\";\n        CXXFLAGS=\"-m32 -Werror\";\n        LDFLAGS=\"-m32\";\n        EXTRA_CMAKE_OPTS=;\n      else\n        CFLAGS=\"-Werror\";\n        CXXFLAGS=\"-Werror\";\n      fi;\n    fi;\n    export CFLAGS CXXFLAGS LDFLAGS TEST_PREFIX EXTRA_CMAKE_OPTS\n  - make && make clean;\n    if [ \"$TRAVIS_OS_NAME\" == \"osx\" ]; then\n      if [ \"$BITS\" == \"64\" ]; then\n        OPENSSL_PREFIX=\"$(ls -d /usr/local/Cellar/openssl@1.1/*)\" USE_SSL=1 make;\n      fi;\n    else\n      USE_SSL=1 make;\n    fi;\n  - mkdir build/ && cd build/\n  - cmake .. ${EXTRA_CMAKE_OPTS}\n  - make VERBOSE=1\n  - if [ \"$BITS\" == \"64\" ]; then\n      TEST_SSL=1 SKIPS_AS_FAILS=1 ctest -V;\n    else\n      SKIPS_AS_FAILS=1 ctest -V;\n    fi;\n\njobs:\n  include:\n    # Windows MinGW cross compile on Linux\n    - os: linux\n      dist: xenial\n      compiler: mingw\n      addons:\n        apt:\n          packages:\n            - ninja-build\n            - gcc-mingw-w64-x86-64\n            - g++-mingw-w64-x86-64\n      script:\n        - mkdir build && cd build\n        - CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ cmake .. -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_BUILD_WITH_INSTALL_RPATH=on\n        - ninja -v\n\n    # Windows MSVC 2017\n    - os: windows\n      compiler: msvc\n      env:\n        - MATRIX_EVAL=\"CC=cl.exe && CXX=cl.exe\"\n      before_install:\n        - eval \"${MATRIX_EVAL}\"\n      install:\n        - choco install ninja\n        - choco install -y memurai-developer\n      script:\n        - mkdir build && cd build\n        - cmd.exe //C 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\BuildTools\\VC\\Auxiliary\\Build\\vcvarsall.bat' amd64 '&&'\n          cmake .. -G Ninja -DCMAKE_BUILD_TYPE=Release -DENABLE_EXAMPLES=ON '&&' ninja -v\n        - ./hiredis-test.exe\n"
  },
  {
    "path": "deps/hiredis/CHANGELOG.md",
    "content": "## [1.0.0](https://github.com/redis/hiredis/tree/v1.0.0) - (2020-08-03)\n\nAnnouncing Hiredis v1.0.0, which adds support for RESP3, SSL connections, allocator injection, and better Windows support! :tada:\n\n_A big thanks to everyone who helped with this release.  The following list includes everyone who contributed at least five lines, sorted by lines contributed._ :sparkling_heart:\n\n[Michael Grunder](https://github.com/michael-grunder), [Yossi Gottlieb](https://github.com/yossigo),\n[Mark Nunberg](https://github.com/mnunberg), [Marcus Geelnard](https://github.com/mbitsnbites),\n[Justin Brewer](https://github.com/justinbrewer), [Valentino Geron](https://github.com/valentinogeron),\n[Minun Dragonation](https://github.com/dragonation), [Omri Steiner](https://github.com/OmriSteiner),\n[Sangmoon Yi](https://github.com/jman-krafton), [Jinjiazh](https://github.com/jinjiazhang),\n[Odin Hultgren Van Der Horst](https://github.com/Miniwoffer), [Muhammad Zahalqa](https://github.com/tryfinally),\n[Nick Rivera](https://github.com/heronr), [Qi Yang](https://github.com/movebean),\n[kevin1018](https://github.com/kevin1018)\n\n[Full Changelog](https://github.com/redis/hiredis/compare/v0.14.1...v1.0.0)\n\n**BREAKING CHANGES**:\n\n* `redisOptions` now has two timeout fields.  One for connecting, and one for commands.  If you're presently using `options->timeout` you will need to change it to use `options->connect_timeout`. (See [example](https://github.com/redis/hiredis/commit/38b5ae543f5c99eb4ccabbe277770fc6bc81226f#diff-86ba39d37aa829c8c82624cce4f049fbL36))\n\n* Bulk and multi-bulk lengths less than -1 or greater than `LLONG_MAX` are now protocol errors. This is consistent\n  with the RESP specification. On 32-bit platforms, the upper bound is lowered to `SIZE_MAX`.\n\n* `redisReplyObjectFunctions.createArray` now takes `size_t` for its length parameter.\n\n**New features:**\n- Support for RESP3\n  [\\#697](https://github.com/redis/hiredis/pull/697),\n  [\\#805](https://github.com/redis/hiredis/pull/805),\n  [\\#819](https://github.com/redis/hiredis/pull/819),\n  [\\#841](https://github.com/redis/hiredis/pull/841)\n  ([Yossi Gottlieb](https://github.com/yossigo), [Michael Grunder](https://github.com/michael-grunder))\n- Support for SSL connections\n  [\\#645](https://github.com/redis/hiredis/pull/645),\n  [\\#699](https://github.com/redis/hiredis/pull/699),\n  [\\#702](https://github.com/redis/hiredis/pull/702),\n  [\\#708](https://github.com/redis/hiredis/pull/708),\n  [\\#711](https://github.com/redis/hiredis/pull/711),\n  [\\#821](https://github.com/redis/hiredis/pull/821),\n  [more](https://github.com/redis/hiredis/pulls?q=is%3Apr+is%3Amerged+SSL)\n  ([Mark Nunberg](https://github.com/mnunberg), [Yossi Gottlieb](https://github.com/yossigo))\n- Run-time allocator injection\n  [\\#800](https://github.com/redis/hiredis/pull/800)\n  ([Michael Grunder](https://github.com/michael-grunder))\n- Improved Windows support (including MinGW and Windows CI)\n  [\\#652](https://github.com/redis/hiredis/pull/652),\n  [\\#663](https://github.com/redis/hiredis/pull/663)\n  ([Marcus Geelnard](https://www.bitsnbites.eu/author/m/))\n- Adds support for distinct connect and command timeouts\n  [\\#839](https://github.com/redis/hiredis/pull/839),\n  [\\#829](https://github.com/redis/hiredis/pull/829)\n  ([Valentino Geron](https://github.com/valentinogeron))\n- Add generic pointer and destructor to `redisContext` that users can use for context.\n  [\\#855](https://github.com/redis/hiredis/pull/855)\n  ([Michael Grunder](https://github.com/michael-grunder))\n\n**Closed issues (that involved code changes):**\n\n- Makefile does not install TLS libraries  [\\#809](https://github.com/redis/hiredis/issues/809)\n- redisConnectWithOptions should not set command timeout [\\#722](https://github.com/redis/hiredis/issues/722), [\\#829](https://github.com/redis/hiredis/pull/829) ([valentinogeron](https://github.com/valentinogeron))\n- Fix integer overflow in `sdsrange` [\\#827](https://github.com/redis/hiredis/issues/827)\n- INFO & CLUSTER commands failed when using RESP3 [\\#802](https://github.com/redis/hiredis/issues/802)\n- Windows compatibility patches [\\#687](https://github.com/redis/hiredis/issues/687), [\\#838](https://github.com/redis/hiredis/issues/838), [\\#842](https://github.com/redis/hiredis/issues/842)\n- RESP3 PUSH messages incorrectly use pending callback [\\#825](https://github.com/redis/hiredis/issues/825)\n- Asynchronous PSUBSCRIBE command fails when using RESP3 [\\#815](https://github.com/redis/hiredis/issues/815)\n- New SSL API [\\#804](https://github.com/redis/hiredis/issues/804), [\\#813](https://github.com/redis/hiredis/issues/813)\n- Hard-coded limit of nested reply depth [\\#794](https://github.com/redis/hiredis/issues/794)\n- Fix TCP_NODELAY in Windows/OSX [\\#679](https://github.com/redis/hiredis/issues/679), [\\#690](https://github.com/redis/hiredis/issues/690), [\\#779](https://github.com/redis/hiredis/issues/779), [\\#785](https://github.com/redis/hiredis/issues/785),\n- Added timers to libev adapter.  [\\#778](https://github.com/redis/hiredis/issues/778), [\\#795](https://github.com/redis/hiredis/pull/795)\n- Initialization discards const qualifier [\\#777](https://github.com/redis/hiredis/issues/777)\n- \\[BUG\\]\\[MinGW64\\] Error setting socket timeout  [\\#775](https://github.com/redis/hiredis/issues/775)\n- undefined reference to hi_malloc [\\#769](https://github.com/redis/hiredis/issues/769)\n- hiredis pkg-config file incorrectly ignores multiarch libdir spec'n [\\#767](https://github.com/redis/hiredis/issues/767)\n- Don't use -G to build shared object on Solaris [\\#757](https://github.com/redis/hiredis/issues/757)\n- error when make USE\\_SSL=1 [\\#748](https://github.com/redis/hiredis/issues/748)\n- Allow to change SSL Mode [\\#646](https://github.com/redis/hiredis/issues/646)\n- hiredis/adapters/libevent.h memleak [\\#618](https://github.com/redis/hiredis/issues/618)\n- redisLibuvPoll crash when server closes the connetion [\\#545](https://github.com/redis/hiredis/issues/545)\n- about redisAsyncDisconnect question [\\#518](https://github.com/redis/hiredis/issues/518)\n- hiredis adapters libuv error for help [\\#508](https://github.com/redis/hiredis/issues/508)\n- API/ABI changes analysis [\\#506](https://github.com/redis/hiredis/issues/506)\n- Memory leak patch in Redis [\\#502](https://github.com/redis/hiredis/issues/502)\n- Remove the depth limitation [\\#421](https://github.com/redis/hiredis/issues/421)\n\n**Merged pull requests:**\n\n- Move SSL management to a distinct private pointer [\\#855](https://github.com/redis/hiredis/pull/855) ([michael-grunder](https://github.com/michael-grunder))\n- Move include to sockcompat.h to maintain style [\\#850](https://github.com/redis/hiredis/pull/850) ([michael-grunder](https://github.com/michael-grunder))\n- Remove erroneous tag and add license to push example [\\#849](https://github.com/redis/hiredis/pull/849) ([michael-grunder](https://github.com/michael-grunder))\n- fix windows compiling with mingw [\\#848](https://github.com/redis/hiredis/pull/848) ([rmalizia44](https://github.com/rmalizia44))\n- Some Windows quality of life improvements. [\\#846](https://github.com/redis/hiredis/pull/846) ([michael-grunder](https://github.com/michael-grunder))\n- Use \\_WIN32 define instead of WIN32 [\\#845](https://github.com/redis/hiredis/pull/845) ([michael-grunder](https://github.com/michael-grunder))\n- Non Linux CI fixes [\\#844](https://github.com/redis/hiredis/pull/844) ([michael-grunder](https://github.com/michael-grunder))\n- Resp3 oob push support [\\#841](https://github.com/redis/hiredis/pull/841) ([michael-grunder](https://github.com/michael-grunder))\n- fix \\#785: defer TCP\\_NODELAY in async tcp connections [\\#836](https://github.com/redis/hiredis/pull/836) ([OmriSteiner](https://github.com/OmriSteiner))\n- sdsrange overflow fix [\\#830](https://github.com/redis/hiredis/pull/830) ([michael-grunder](https://github.com/michael-grunder))\n- Use explicit pointer casting for c++ compatibility [\\#826](https://github.com/redis/hiredis/pull/826) ([aureus1](https://github.com/aureus1))\n- Document allocator injection and completeness fix in test.c [\\#824](https://github.com/redis/hiredis/pull/824) ([michael-grunder](https://github.com/michael-grunder))\n- Use unique names for allocator struct members [\\#823](https://github.com/redis/hiredis/pull/823) ([michael-grunder](https://github.com/michael-grunder))\n- New SSL API to replace redisSecureConnection\\(\\). [\\#821](https://github.com/redis/hiredis/pull/821) ([yossigo](https://github.com/yossigo))\n- Add logic to handle RESP3 push messages [\\#819](https://github.com/redis/hiredis/pull/819) ([michael-grunder](https://github.com/michael-grunder))\n- Use standrad isxdigit instead of custom helper function. [\\#814](https://github.com/redis/hiredis/pull/814) ([tryfinally](https://github.com/tryfinally))\n- Fix missing SSL build/install options. [\\#812](https://github.com/redis/hiredis/pull/812) ([yossigo](https://github.com/yossigo))\n- Add link to ABI tracker [\\#808](https://github.com/redis/hiredis/pull/808) ([michael-grunder](https://github.com/michael-grunder))\n- Resp3 verbatim string support [\\#805](https://github.com/redis/hiredis/pull/805) ([michael-grunder](https://github.com/michael-grunder))\n- Allow users to replace allocator and handle OOM everywhere. [\\#800](https://github.com/redis/hiredis/pull/800) ([michael-grunder](https://github.com/michael-grunder))\n- Remove nested depth limitation. [\\#797](https://github.com/redis/hiredis/pull/797) ([michael-grunder](https://github.com/michael-grunder))\n- Attempt to fix compilation on Solaris [\\#796](https://github.com/redis/hiredis/pull/796) ([michael-grunder](https://github.com/michael-grunder))\n- Support timeouts in libev adapater [\\#795](https://github.com/redis/hiredis/pull/795) ([michael-grunder](https://github.com/michael-grunder))\n- Fix pkgconfig when installing to a custom lib dir [\\#793](https://github.com/redis/hiredis/pull/793) ([michael-grunder](https://github.com/michael-grunder))\n- Fix USE\\_SSL=1 make/cmake on OSX and CMake tests [\\#789](https://github.com/redis/hiredis/pull/789) ([michael-grunder](https://github.com/michael-grunder))\n- Use correct libuv call on Windows [\\#784](https://github.com/redis/hiredis/pull/784) ([michael-grunder](https://github.com/michael-grunder))\n- Added CMake package config and fixed hiredis\\_ssl on Windows [\\#783](https://github.com/redis/hiredis/pull/783) ([michael-grunder](https://github.com/michael-grunder))\n- CMake: Set hiredis\\_ssl shared object version. [\\#780](https://github.com/redis/hiredis/pull/780) ([yossigo](https://github.com/yossigo))\n- Win32 tests and timeout fix [\\#776](https://github.com/redis/hiredis/pull/776) ([michael-grunder](https://github.com/michael-grunder))\n- Provides an optional cleanup callback for async data. [\\#768](https://github.com/redis/hiredis/pull/768) ([heronr](https://github.com/heronr))\n- Housekeeping fixes [\\#764](https://github.com/redis/hiredis/pull/764) ([michael-grunder](https://github.com/michael-grunder))\n- install alloc.h [\\#756](https://github.com/redis/hiredis/pull/756) ([ch1aki](https://github.com/ch1aki))\n- fix spelling mistakes [\\#746](https://github.com/redis/hiredis/pull/746) ([ShooterIT](https://github.com/ShooterIT))\n- Free the reply in redisGetReply when passed NULL [\\#741](https://github.com/redis/hiredis/pull/741) ([michael-grunder](https://github.com/michael-grunder))\n- Fix dead code in sslLogCallback relating to should\\_log variable. [\\#737](https://github.com/redis/hiredis/pull/737) ([natoscott](https://github.com/natoscott))\n- Fix typo in dict.c. [\\#731](https://github.com/redis/hiredis/pull/731) ([Kevin-Xi](https://github.com/Kevin-Xi))\n- Adding an option to DISABLE\\_TESTS [\\#727](https://github.com/redis/hiredis/pull/727) ([pbotros](https://github.com/pbotros))\n- Update README with SSL support. [\\#720](https://github.com/redis/hiredis/pull/720) ([yossigo](https://github.com/yossigo))\n- Fixes leaks in unit tests [\\#715](https://github.com/redis/hiredis/pull/715) ([michael-grunder](https://github.com/michael-grunder))\n- SSL Tests [\\#711](https://github.com/redis/hiredis/pull/711) ([yossigo](https://github.com/yossigo))\n- SSL Reorganization [\\#708](https://github.com/redis/hiredis/pull/708) ([yossigo](https://github.com/yossigo))\n- Fix MSVC build. [\\#706](https://github.com/redis/hiredis/pull/706) ([yossigo](https://github.com/yossigo))\n- SSL: Properly report SSL\\_connect\\(\\) errors. [\\#702](https://github.com/redis/hiredis/pull/702) ([yossigo](https://github.com/yossigo))\n- Silent SSL trace to stdout by default. [\\#699](https://github.com/redis/hiredis/pull/699) ([yossigo](https://github.com/yossigo))\n- Port RESP3 support from Redis. [\\#697](https://github.com/redis/hiredis/pull/697) ([yossigo](https://github.com/yossigo))\n- Removed whitespace before newline [\\#691](https://github.com/redis/hiredis/pull/691) ([Miniwoffer](https://github.com/Miniwoffer))\n- Add install adapters header files [\\#688](https://github.com/redis/hiredis/pull/688) ([kevin1018](https://github.com/kevin1018))\n- Remove unnecessary null check before free [\\#684](https://github.com/redis/hiredis/pull/684) ([qlyoung](https://github.com/qlyoung))\n- redisReaderGetReply leak memory [\\#671](https://github.com/redis/hiredis/pull/671) ([movebean](https://github.com/movebean))\n- fix timeout code in windows [\\#670](https://github.com/redis/hiredis/pull/670) ([jman-krafton](https://github.com/jman-krafton))\n- test: fix errstr matching for musl libc [\\#665](https://github.com/redis/hiredis/pull/665) ([ghost](https://github.com/ghost))\n- Windows: MinGW fixes and Windows Travis builders [\\#663](https://github.com/redis/hiredis/pull/663) ([mbitsnbites](https://github.com/mbitsnbites))\n- The setsockopt and getsockopt API diffs from BSD socket and WSA one [\\#662](https://github.com/redis/hiredis/pull/662) ([dragonation](https://github.com/dragonation))\n- Fix Compile Error On Windows \\(Visual Studio\\) [\\#658](https://github.com/redis/hiredis/pull/658) ([jinjiazhang](https://github.com/jinjiazhang))\n- Fix NXDOMAIN test case [\\#653](https://github.com/redis/hiredis/pull/653) ([michael-grunder](https://github.com/michael-grunder))\n- Add MinGW support [\\#652](https://github.com/redis/hiredis/pull/652) ([mbitsnbites](https://github.com/mbitsnbites))\n- SSL Support [\\#645](https://github.com/redis/hiredis/pull/645) ([mnunberg](https://github.com/mnunberg))\n- Fix Invalid argument after redisAsyncConnectUnix [\\#644](https://github.com/redis/hiredis/pull/644) ([codehz](https://github.com/codehz))\n- Makefile: use predefined AR [\\#632](https://github.com/redis/hiredis/pull/632) ([Mic92](https://github.com/Mic92))\n- FreeBSD  build fix [\\#628](https://github.com/redis/hiredis/pull/628) ([devnexen](https://github.com/devnexen))\n- Fix errors not propagating properly with libuv.h. [\\#624](https://github.com/redis/hiredis/pull/624) ([yossigo](https://github.com/yossigo))\n- Update README.md [\\#621](https://github.com/redis/hiredis/pull/621) ([Crunsher](https://github.com/Crunsher))\n- Fix redisBufferRead documentation [\\#620](https://github.com/redis/hiredis/pull/620) ([hacst](https://github.com/hacst))\n- Add CPPFLAGS to REAL\\_CFLAGS [\\#614](https://github.com/redis/hiredis/pull/614) ([thomaslee](https://github.com/thomaslee))\n- Update createArray to take size\\_t [\\#597](https://github.com/redis/hiredis/pull/597) ([justinbrewer](https://github.com/justinbrewer))\n- fix common realloc mistake and add null check more [\\#580](https://github.com/redis/hiredis/pull/580) ([charsyam](https://github.com/charsyam))\n- Proper error reporting for connect failures [\\#578](https://github.com/redis/hiredis/pull/578) ([mnunberg](https://github.com/mnunberg))\n\n\\* *This Changelog was automatically generated by [github_changelog_generator](https://github.com/github-changelog-generator/github-changelog-generator)*\n\n## [1.0.0-rc1](https://github.com/redis/hiredis/tree/v1.0.0-rc1) - (2020-07-29)\n\n_Note:  There were no changes to code between v1.0.0-rc1 and v1.0.0 so see v1.0.0 for changelog_\n\n### 0.14.1 (2020-03-13)\n\n* Adds safe allocation wrappers (CVE-2020-7105, #747, #752) (Michael Grunder)\n\n### 0.14.0 (2018-09-25)\n**BREAKING CHANGES**:\n\n* Change `redisReply.len` to `size_t`, as it denotes the the size of a string\n\n  User code should compare this to `size_t` values as well.\n  If it was used to compare to other values, casting might be necessary or can be removed, if casting was applied before.\n\n* Make string2ll static to fix conflict with Redis (Tom Lee [c3188b])\n* Use -dynamiclib instead of -shared for OSX (Ryan Schmidt [a65537])\n* Use string2ll from Redis w/added tests (Michael Grunder [7bef04, 60f622])\n* Makefile - OSX compilation fixes (Ryan Schmidt [881fcb, 0e9af8])\n* Remove redundant NULL checks (Justin Brewer [54acc8, 58e6b8])\n* Fix bulk and multi-bulk length truncation (Justin Brewer [109197])\n* Fix SIGSEGV in OpenBSD by checking for NULL before calling freeaddrinfo (Justin Brewer [546d94])\n* Several POSIX compatibility fixes (Justin Brewer [bbeab8, 49bbaa, d1c1b6])\n* Makefile - Compatibility fixes (Dimitri Vorobiev [3238cf, 12a9d1])\n* Makefile - Fix make install on FreeBSD (Zach Shipko [a2ef2b])\n* Makefile - don't assume $(INSTALL) is cp (Igor Gnatenko [725a96])\n* Separate side-effect causing function from assert and small cleanup (amallia [b46413, 3c3234])\n* Don't send negative values to `__redisAsyncCommand` (Frederik Deweerdt [706129])\n* Fix leak if setsockopt fails (Frederik Deweerdt [e21c9c])\n* Fix libevent leak (zfz [515228])\n* Clean up GCC warning (Ichito Nagata [2ec774])\n* Keep track of errno in `__redisSetErrorFromErrno()` as snprintf may use it (Jin Qing [25cd88])\n* Solaris compilation fix (Donald Whyte [41b07d])\n* Reorder linker arguments when building examples (Tustfarm-heart [06eedd])\n* Keep track of subscriptions in case of rapid subscribe/unsubscribe (Hyungjin Kim [073dc8, be76c5, d46999])\n* libuv use after free fix (Paul Scott [cbb956])\n* Properly close socket fd on reconnect attempt (WSL [64d1ec])\n* Skip valgrind in OSX tests (Jan-Erik Rediger [9deb78])\n* Various updates for Travis testing OSX (Ted Nyman [fa3774, 16a459, bc0ea5])\n* Update libevent (Chris Xin [386802])\n* Change sds.h for building in C++ projects (Ali Volkan ATLI [f5b32e])\n* Use proper format specifier in redisFormatSdsCommandArgv (Paulino Huerta, Jan-Erik Rediger [360a06, 8655a6])\n* Better handling of NULL reply in example code (Jan-Erik Rediger [1b8ed3])\n* Prevent overflow when formatting an error (Jan-Erik Rediger [0335cb])\n* Compatibility fix for strerror_r (Tom Lee [bb1747])\n* Properly detect integer parse/overflow errors (Justin Brewer [93421f])\n* Adds CI for Windows and cygwin fixes (owent, [6c53d6, 6c3e40])\n* Catch a buffer overflow when formatting the error message\n* Import latest upstream sds. This breaks applications that are linked against the old hiredis v0.13\n* Fix warnings, when compiled with -Wshadow\n* Make hiredis compile in Cygwin on Windows, now CI-tested\n* Bulk and multi-bulk lengths less than -1 or greater than `LLONG_MAX` are now\n  protocol errors. This is consistent with the RESP specification. On 32-bit\n  platforms, the upper bound is lowered to `SIZE_MAX`.\n\n* Remove backwards compatibility macro's\n\nThis removes the following old function aliases, use the new name now:\n\n| Old                         | New                    |\n| --------------------------- | ---------------------- |\n| redisReplyReaderCreate      | redisReaderCreate      |\n| redisReplyReaderCreate      | redisReaderCreate      |\n| redisReplyReaderFree        | redisReaderFree        |\n| redisReplyReaderFeed        | redisReaderFeed        |\n| redisReplyReaderGetReply    | redisReaderGetReply    |\n| redisReplyReaderSetPrivdata | redisReaderSetPrivdata |\n| redisReplyReaderGetObject   | redisReaderGetObject   |\n| redisReplyReaderGetError    | redisReaderGetError    |\n\n* The `DEBUG` variable in the Makefile was renamed to `DEBUG_FLAGS`\n\nPreviously it broke some builds for people that had `DEBUG` set to some arbitrary value,\ndue to debugging other software.\nBy renaming we avoid unintentional name clashes.\n\nSimply rename `DEBUG` to `DEBUG_FLAGS` in your environment to make it working again.\n\n### 0.13.3 (2015-09-16)\n\n* Revert \"Clear `REDIS_CONNECTED` flag when connection is closed\".\n* Make tests pass on FreeBSD (Thanks, Giacomo Olgeni)\n\n\nIf the `REDIS_CONNECTED` flag is cleared,\nthe async onDisconnect callback function will never be called.\nThis causes problems as the disconnect is never reported back to the user.\n\n### 0.13.2 (2015-08-25)\n\n* Prevent crash on pending replies in async code (Thanks, @switch-st)\n* Clear `REDIS_CONNECTED` flag when connection is closed (Thanks, Jerry Jacobs)\n* Add MacOS X addapter (Thanks, @dizzus)\n* Add Qt adapter (Thanks, Pietro Cerutti)\n* Add Ivykis adapter (Thanks, Gergely Nagy)\n\nAll adapters are provided as is and are only tested where possible.\n\n### 0.13.1 (2015-05-03)\n\nThis is a bug fix release.\nThe new `reconnect` method introduced new struct members, which clashed with pre-defined names in pre-C99 code.\nAnother commit forced C99 compilation just to make it work, but of course this is not desirable for outside projects.\nOther non-C99 code can now use hiredis as usual again.\nSorry for the inconvenience.\n\n* Fix memory leak in async reply handling (Salvatore Sanfilippo)\n* Rename struct member to avoid name clash with pre-c99 code (Alex Balashov, ncopa)\n\n### 0.13.0 (2015-04-16)\n\nThis release adds a minimal Windows compatibility layer.\nThe parser, standalone since v0.12.0, can now be compiled on Windows\n(and thus used in other client libraries as well)\n\n* Windows compatibility layer for parser code (tzickel)\n* Properly escape data printed to PKGCONF file (Dan Skorupski)\n* Fix tests when assert() undefined (Keith Bennett, Matt Stancliff)\n* Implement a reconnect method for the client context, this changes the structure of `redisContext` (Aaron Bedra)\n\n### 0.12.1 (2015-01-26)\n\n* Fix `make install`: DESTDIR support, install all required files, install PKGCONF in proper location\n* Fix `make test` as 32 bit build on 64 bit platform\n\n### 0.12.0 (2015-01-22)\n\n* Add optional KeepAlive support\n\n* Try again on EINTR errors\n\n* Add libuv adapter\n\n* Add IPv6 support\n\n* Remove possibility of multiple close on same fd\n\n* Add ability to bind source address on connect\n\n* Add redisConnectFd() and redisFreeKeepFd()\n\n* Fix getaddrinfo() memory leak\n\n* Free string if it is unused (fixes memory leak)\n\n* Improve redisAppendCommandArgv performance 2.5x\n\n* Add support for SO_REUSEADDR\n\n* Fix redisvFormatCommand format parsing\n\n* Add GLib 2.0 adapter\n\n* Refactor reading code into read.c\n\n* Fix errno error buffers to not clobber errors\n\n* Generate pkgconf during build\n\n* Silence _BSD_SOURCE warnings\n\n* Improve digit counting for multibulk creation\n\n\n### 0.11.0\n\n* Increase the maximum multi-bulk reply depth to 7.\n\n* Increase the read buffer size from 2k to 16k.\n\n* Use poll(2) instead of select(2) to support large fds (>= 1024).\n\n### 0.10.1\n\n* Makefile overhaul. Important to check out if you override one or more\n  variables using environment variables or via arguments to the \"make\" tool.\n\n* Issue #45: Fix potential memory leak for a multi bulk reply with 0 elements\n  being created by the default reply object functions.\n\n* Issue #43: Don't crash in an asynchronous context when Redis returns an error\n  reply after the connection has been made (this happens when the maximum\n  number of connections is reached).\n\n### 0.10.0\n\n* See commit log.\n"
  },
  {
    "path": "deps/hiredis/CMakeLists.txt",
    "content": "CMAKE_MINIMUM_REQUIRED(VERSION 3.4.0)\nINCLUDE(GNUInstallDirs)\nPROJECT(hiredis)\n\nOPTION(ENABLE_SSL \"Build hiredis_ssl for SSL support\" OFF)\nOPTION(DISABLE_TESTS \"If tests should be compiled or not\" OFF)\nOPTION(ENABLE_SSL_TESTS, \"Should we test SSL connections\" OFF)\n\nMACRO(getVersionBit name)\n  SET(VERSION_REGEX \"^#define ${name} (.+)$\")\n  FILE(STRINGS \"${CMAKE_CURRENT_SOURCE_DIR}/hiredis.h\"\n    VERSION_BIT REGEX ${VERSION_REGEX})\n  STRING(REGEX REPLACE ${VERSION_REGEX} \"\\\\1\" ${name} \"${VERSION_BIT}\")\nENDMACRO(getVersionBit)\n\ngetVersionBit(HIREDIS_MAJOR)\ngetVersionBit(HIREDIS_MINOR)\ngetVersionBit(HIREDIS_PATCH)\ngetVersionBit(HIREDIS_SONAME)\nSET(VERSION \"${HIREDIS_MAJOR}.${HIREDIS_MINOR}.${HIREDIS_PATCH}\")\nMESSAGE(\"Detected version: ${VERSION}\")\n\nPROJECT(hiredis VERSION \"${VERSION}\")\n\nSET(ENABLE_EXAMPLES OFF CACHE BOOL \"Enable building hiredis examples\")\n\nSET(hiredis_sources\n    alloc.c\n    async.c\n    dict.c\n    hiredis.c\n    net.c\n    read.c\n    sds.c\n    sockcompat.c)\n\nSET(hiredis_sources ${hiredis_sources})\n\nIF(WIN32)\n    ADD_COMPILE_DEFINITIONS(_CRT_SECURE_NO_WARNINGS WIN32_LEAN_AND_MEAN)\nENDIF()\n\nADD_LIBRARY(hiredis SHARED ${hiredis_sources})\n\nSET_TARGET_PROPERTIES(hiredis\n    PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS TRUE\n    VERSION \"${HIREDIS_SONAME}\")\nIF(WIN32 OR MINGW)\n    TARGET_LINK_LIBRARIES(hiredis PRIVATE ws2_32)\nENDIF()\n\nTARGET_INCLUDE_DIRECTORIES(hiredis PUBLIC $<INSTALL_INTERFACE:.> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)\n\nCONFIGURE_FILE(hiredis.pc.in hiredis.pc @ONLY)\n\nINSTALL(TARGETS hiredis\n    EXPORT hiredis-targets\n    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}\n    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}\n    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})\n\nINSTALL(FILES hiredis.h read.h sds.h async.h alloc.h\n    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/hiredis)\n    \nINSTALL(DIRECTORY adapters\n    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/hiredis)\n    \nINSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/hiredis.pc\n    DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)\n\nexport(EXPORT hiredis-targets\n    FILE \"${CMAKE_CURRENT_BINARY_DIR}/hiredis-targets.cmake\"\n    NAMESPACE hiredis::)\n\nSET(CMAKE_CONF_INSTALL_DIR share/hiredis)\nSET(INCLUDE_INSTALL_DIR include)\ninclude(CMakePackageConfigHelpers)\nconfigure_package_config_file(hiredis-config.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/hiredis-config.cmake\n                              INSTALL_DESTINATION ${CMAKE_CONF_INSTALL_DIR}\n                              PATH_VARS INCLUDE_INSTALL_DIR)\n\nINSTALL(EXPORT hiredis-targets\n        FILE hiredis-targets.cmake\n        NAMESPACE hiredis::\n        DESTINATION ${CMAKE_CONF_INSTALL_DIR})\n\nINSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/hiredis-config.cmake\n        DESTINATION ${CMAKE_CONF_INSTALL_DIR})\n\n\nIF(ENABLE_SSL)\n    IF (NOT OPENSSL_ROOT_DIR)\n        IF (APPLE)\n            SET(OPENSSL_ROOT_DIR \"/usr/local/opt/openssl\")\n        ENDIF()\n    ENDIF()\n    FIND_PACKAGE(OpenSSL REQUIRED)\n    SET(hiredis_ssl_sources \n        ssl.c)\n    ADD_LIBRARY(hiredis_ssl SHARED\n            ${hiredis_ssl_sources})\n\n    IF (APPLE)\n        SET_PROPERTY(TARGET hiredis_ssl PROPERTY LINK_FLAGS \"-Wl,-undefined -Wl,dynamic_lookup\")\n    ENDIF()\n\n    SET_TARGET_PROPERTIES(hiredis_ssl\n        PROPERTIES\n        WINDOWS_EXPORT_ALL_SYMBOLS TRUE\n        VERSION \"${HIREDIS_SONAME}\")\n\n    TARGET_INCLUDE_DIRECTORIES(hiredis_ssl PRIVATE \"${OPENSSL_INCLUDE_DIR}\")\n    TARGET_LINK_LIBRARIES(hiredis_ssl PRIVATE ${OPENSSL_LIBRARIES})\n    IF (WIN32 OR MINGW)\n        TARGET_LINK_LIBRARIES(hiredis_ssl PRIVATE hiredis)\n    ENDIF()\n    CONFIGURE_FILE(hiredis_ssl.pc.in hiredis_ssl.pc @ONLY)\n\n    INSTALL(TARGETS hiredis_ssl\n        EXPORT hiredis_ssl-targets\n        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}\n        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}\n        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})\n\n    INSTALL(FILES hiredis_ssl.h\n        DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/hiredis)\n    \n    INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/hiredis_ssl.pc\n        DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)\n\n    export(EXPORT hiredis_ssl-targets\n           FILE \"${CMAKE_CURRENT_BINARY_DIR}/hiredis_ssl-targets.cmake\"\n           NAMESPACE hiredis::)\n\n    SET(CMAKE_CONF_INSTALL_DIR share/hiredis_ssl)\n    configure_package_config_file(hiredis_ssl-config.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/hiredis_ssl-config.cmake\n                                  INSTALL_DESTINATION ${CMAKE_CONF_INSTALL_DIR}\n                                  PATH_VARS INCLUDE_INSTALL_DIR)\n\n    INSTALL(EXPORT hiredis_ssl-targets\n        FILE hiredis_ssl-targets.cmake\n        NAMESPACE hiredis::\n        DESTINATION ${CMAKE_CONF_INSTALL_DIR})\n\n    INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/hiredis_ssl-config.cmake\n        DESTINATION ${CMAKE_CONF_INSTALL_DIR})\nENDIF()\n\nIF(NOT DISABLE_TESTS)\n    ENABLE_TESTING()\n    ADD_EXECUTABLE(hiredis-test test.c)\n    IF(ENABLE_SSL_TESTS)\n        ADD_DEFINITIONS(-DHIREDIS_TEST_SSL=1)\n        TARGET_LINK_LIBRARIES(hiredis-test hiredis hiredis_ssl)\n    ELSE()\n        TARGET_LINK_LIBRARIES(hiredis-test hiredis)\n    ENDIF()\n    ADD_TEST(NAME hiredis-test\n        COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/test.sh)\nENDIF()\n\n# Add examples\nIF(ENABLE_EXAMPLES)\n  ADD_SUBDIRECTORY(examples)\nENDIF(ENABLE_EXAMPLES)\n"
  },
  {
    "path": "deps/hiredis/COPYING",
    "content": "Copyright (c) 2009-2011, Salvatore Sanfilippo <antirez at gmail dot com>\nCopyright (c) 2010-2011, Pieter Noordhuis <pcnoordhuis at gmail dot com>\n\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are met:\n\n* Redistributions of source code must retain the above copyright notice,\n  this list of conditions and the following disclaimer.\n\n* Redistributions in binary form must reproduce the above copyright notice,\n  this list of conditions and the following disclaimer in the documentation\n  and/or other materials provided with the distribution.\n\n* Neither the name of Redis nor the names of its contributors may be used\n  to endorse or promote products derived from this software without specific\n  prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND\nANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\nDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR\nANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\nANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
  },
  {
    "path": "deps/hiredis/Makefile",
    "content": "# Hiredis Makefile\n# Copyright (C) 2010-2011 Salvatore Sanfilippo <antirez at gmail dot com>\n# Copyright (C) 2010-2011 Pieter Noordhuis <pcnoordhuis at gmail dot com>\n# This file is released under the BSD license, see the COPYING file\n\nOBJ=alloc.o net.o hiredis.o sds.o async.o read.o sockcompat.o\nSSL_OBJ=ssl.o\nEXAMPLES=hiredis-example hiredis-example-libevent hiredis-example-libev hiredis-example-glib hiredis-example-push\nifeq ($(USE_SSL),1)\nEXAMPLES+=hiredis-example-ssl hiredis-example-libevent-ssl\nendif\nTESTS=hiredis-test\nLIBNAME=libhiredis\nPKGCONFNAME=hiredis.pc\nSSL_LIBNAME=libhiredis_ssl\nSSL_PKGCONFNAME=hiredis_ssl.pc\n\nHIREDIS_MAJOR=$(shell grep HIREDIS_MAJOR hiredis.h | awk '{print $$3}')\nHIREDIS_MINOR=$(shell grep HIREDIS_MINOR hiredis.h | awk '{print $$3}')\nHIREDIS_PATCH=$(shell grep HIREDIS_PATCH hiredis.h | awk '{print $$3}')\nHIREDIS_SONAME=$(shell grep HIREDIS_SONAME hiredis.h | awk '{print $$3}')\n\n# Installation related variables and target\nPREFIX?=/usr/local\nINCLUDE_PATH?=include/hiredis\nLIBRARY_PATH?=lib\nPKGCONF_PATH?=pkgconfig\nINSTALL_INCLUDE_PATH= $(DESTDIR)$(PREFIX)/$(INCLUDE_PATH)\nINSTALL_LIBRARY_PATH= $(DESTDIR)$(PREFIX)/$(LIBRARY_PATH)\nINSTALL_PKGCONF_PATH= $(INSTALL_LIBRARY_PATH)/$(PKGCONF_PATH)\n\n# keydb-server configuration used for testing\nREDIS_PORT=56379\nREDIS_SERVER=keydb-server\ndefine REDIS_TEST_CONFIG\n\tdaemonize yes\n\tpidfile /tmp/hiredis-test-redis.pid\n\tport $(REDIS_PORT)\n\tbind 127.0.0.1\n\tunixsocket /tmp/hiredis-test-redis.sock\nendef\nexport REDIS_TEST_CONFIG\n\n# Fallback to gcc when $CC is not in $PATH.\nCC:=$(shell sh -c 'type $${CC%% *} >/dev/null 2>/dev/null && echo $(CC) || echo gcc')\nCXX:=$(shell sh -c 'type $${CXX%% *} >/dev/null 2>/dev/null && echo $(CXX) || echo g++')\nOPTIMIZATION?=-O3\nWARNINGS=-Wall -W -Wstrict-prototypes -Wwrite-strings -Wno-missing-field-initializers\nDEBUG_FLAGS?= -g -ggdb\nREAL_CFLAGS=$(OPTIMIZATION) -fPIC $(CPPFLAGS) $(CFLAGS) $(WARNINGS) $(DEBUG_FLAGS)\nREAL_LDFLAGS=$(LDFLAGS)\n\nDYLIBSUFFIX=so\nSTLIBSUFFIX=a\nDYLIB_MINOR_NAME=$(LIBNAME).$(DYLIBSUFFIX).$(HIREDIS_SONAME)\nDYLIB_MAJOR_NAME=$(LIBNAME).$(DYLIBSUFFIX).$(HIREDIS_MAJOR)\nDYLIBNAME=$(LIBNAME).$(DYLIBSUFFIX)\n\nDYLIB_MAKE_CMD=$(CC) -shared -Wl,-soname,$(DYLIB_MINOR_NAME)\nSTLIBNAME=$(LIBNAME).$(STLIBSUFFIX)\nSTLIB_MAKE_CMD=$(AR) rcs\n\nSSL_DYLIB_MINOR_NAME=$(SSL_LIBNAME).$(DYLIBSUFFIX).$(HIREDIS_SONAME)\nSSL_DYLIB_MAJOR_NAME=$(SSL_LIBNAME).$(DYLIBSUFFIX).$(HIREDIS_MAJOR)\nSSL_DYLIBNAME=$(SSL_LIBNAME).$(DYLIBSUFFIX)\nSSL_STLIBNAME=$(SSL_LIBNAME).$(STLIBSUFFIX)\nSSL_DYLIB_MAKE_CMD=$(CC) -shared -Wl,-soname,$(SSL_DYLIB_MINOR_NAME)\n\n# Platform-specific overrides\nuname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')\n\nUSE_SSL?=0\n\n# This is required for test.c only\nifeq ($(USE_SSL),1)\n  CFLAGS+=-DHIREDIS_TEST_SSL\nendif\n\nifeq ($(uname_S),Linux)\n  ifdef OPENSSL_PREFIX\n    CFLAGS+=-I$(OPENSSL_PREFIX)/include\n    SSL_LDFLAGS+=-L$(OPENSSL_PREFIX)/lib -lssl -lcrypto\n  else\n    SSL_LDFLAGS=-lssl -lcrypto\n  endif\nelse\n  OPENSSL_PREFIX?=/usr/local/opt/openssl\n  CFLAGS+=-I$(OPENSSL_PREFIX)/include\n  SSL_LDFLAGS+=-L$(OPENSSL_PREFIX)/lib -lssl -lcrypto\nendif\n\nifeq ($(uname_S),SunOS)\n  IS_SUN_CC=$(shell sh -c '$(CC) -V 2>&1 |egrep -i -c \"sun|studio\"')\n  ifeq ($(IS_SUN_CC),1)\n    SUN_SHARED_FLAG=-G\n  else\n    SUN_SHARED_FLAG=-shared\n  endif\n  REAL_LDFLAGS+= -ldl -lnsl -lsocket\n  DYLIB_MAKE_CMD=$(CC) $(SUN_SHARED_FLAG) -o $(DYLIBNAME) -h $(DYLIB_MINOR_NAME) $(LDFLAGS)\n  SSL_DYLIB_MAKE_CMD=$(CC) $(SUN_SHARED_FLAG) -o $(SSL_DYLIBNAME) -h $(SSL_DYLIB_MINOR_NAME) $(LDFLAGS) $(SSL_LDFLAGS)\nendif\nifeq ($(uname_S),Darwin)\n  DYLIBSUFFIX=dylib\n  DYLIB_MINOR_NAME=$(LIBNAME).$(HIREDIS_SONAME).$(DYLIBSUFFIX)\n  DYLIB_MAKE_CMD=$(CC) -dynamiclib -Wl,-install_name,$(PREFIX)/$(LIBRARY_PATH)/$(DYLIB_MINOR_NAME) -o $(DYLIBNAME) $(LDFLAGS)\n  SSL_DYLIB_MAKE_CMD=$(CC) -dynamiclib -Wl,-install_name,$(PREFIX)/$(LIBRARY_PATH)/$(SSL_DYLIB_MINOR_NAME) -o $(SSL_DYLIBNAME) $(LDFLAGS) $(SSL_LDFLAGS)\n  DYLIB_PLUGIN=-Wl,-undefined -Wl,dynamic_lookup\nendif\n\nall: $(DYLIBNAME) $(STLIBNAME) hiredis-test $(PKGCONFNAME)\nifeq ($(USE_SSL),1)\nall: $(SSL_DYLIBNAME) $(SSL_STLIBNAME) $(SSL_PKGCONFNAME)\nendif\n\n# Deps (use make dep to generate this)\nalloc.o: alloc.c fmacros.h alloc.h\nasync.o: async.c fmacros.h alloc.h async.h hiredis.h read.h sds.h net.h dict.c dict.h win32.h async_private.h\ndict.o: dict.c fmacros.h alloc.h dict.h\nhiredis.o: hiredis.c fmacros.h hiredis.h read.h sds.h alloc.h net.h async.h win32.h\nnet.o: net.c fmacros.h net.h hiredis.h read.h sds.h alloc.h sockcompat.h win32.h\nread.o: read.c fmacros.h alloc.h read.h sds.h win32.h\nsds.o: sds.c sds.h sdsalloc.h alloc.h\nsockcompat.o: sockcompat.c sockcompat.h\nssl.o: ssl.c hiredis.h read.h sds.h alloc.h async.h win32.h async_private.h\ntest.o: test.c fmacros.h hiredis.h read.h sds.h alloc.h net.h sockcompat.h win32.h\n\n$(DYLIBNAME): $(OBJ)\n\t$(DYLIB_MAKE_CMD) -o $(DYLIBNAME) $(OBJ) $(REAL_LDFLAGS)\n\n$(STLIBNAME): $(OBJ)\n\t$(STLIB_MAKE_CMD) $(STLIBNAME) $(OBJ)\n\n$(SSL_DYLIBNAME): $(SSL_OBJ)\n\t$(SSL_DYLIB_MAKE_CMD) $(DYLIB_PLUGIN) -o $(SSL_DYLIBNAME) $(SSL_OBJ) $(REAL_LDFLAGS) $(LDFLAGS) $(SSL_LDFLAGS)\n\n$(SSL_STLIBNAME): $(SSL_OBJ)\n\t$(STLIB_MAKE_CMD) $(SSL_STLIBNAME) $(SSL_OBJ)\n\ndynamic: $(DYLIBNAME)\nstatic: $(STLIBNAME)\nifeq ($(USE_SSL),1)\ndynamic: $(SSL_DYLIBNAME)\nstatic: $(SSL_STLIBNAME)\nendif\n\n# Binaries:\nhiredis-example-libevent: examples/example-libevent.c adapters/libevent.h $(STLIBNAME)\n\t$(CC) -o examples/$@ $(REAL_CFLAGS) -I. $< -levent $(STLIBNAME) $(REAL_LDFLAGS)\n\nhiredis-example-libevent-ssl: examples/example-libevent-ssl.c adapters/libevent.h $(STLIBNAME) $(SSL_STLIBNAME)\n\t$(CC) -o examples/$@ $(REAL_CFLAGS) -I. $< -levent $(STLIBNAME) $(SSL_STLIBNAME) $(REAL_LDFLAGS) $(SSL_LDFLAGS)\n\nhiredis-example-libev: examples/example-libev.c adapters/libev.h $(STLIBNAME)\n\t$(CC) -o examples/$@ $(REAL_CFLAGS) -I. $< -lev $(STLIBNAME) $(REAL_LDFLAGS)\n\nhiredis-example-glib: examples/example-glib.c adapters/glib.h $(STLIBNAME)\n\t$(CC) -o examples/$@ $(REAL_CFLAGS) -I. $< $(shell pkg-config --cflags --libs glib-2.0) $(STLIBNAME) $(REAL_LDFLAGS)\n\nhiredis-example-ivykis: examples/example-ivykis.c adapters/ivykis.h $(STLIBNAME)\n\t$(CC) -o examples/$@ $(REAL_CFLAGS) -I. $< -livykis $(STLIBNAME) $(REAL_LDFLAGS)\n\nhiredis-example-macosx: examples/example-macosx.c adapters/macosx.h $(STLIBNAME)\n\t$(CC) -o examples/$@ $(REAL_CFLAGS) -I. $< -framework CoreFoundation $(STLIBNAME) $(REAL_LDFLAGS)\n\nhiredis-example-ssl: examples/example-ssl.c $(STLIBNAME) $(SSL_STLIBNAME)\n\t$(CC) -o examples/$@ $(REAL_CFLAGS) -I. $< $(STLIBNAME) $(SSL_STLIBNAME) $(REAL_LDFLAGS) $(SSL_LDFLAGS)\n\n\nifndef AE_DIR\nhiredis-example-ae:\n\t@echo \"Please specify AE_DIR (e.g. <redis repository>/src)\"\n\t@false\nelse\nhiredis-example-ae: examples/example-ae.c adapters/ae.h $(STLIBNAME)\n\t$(CC) -o examples/$@ $(REAL_CFLAGS) $(REAL_LDFLAGS) -I. -I$(AE_DIR) $< $(AE_DIR)/ae.o $(AE_DIR)/zmalloc.o $(AE_DIR)/../deps/jemalloc/lib/libjemalloc.a -pthread $(STLIBNAME)\nendif\n\nifndef LIBUV_DIR\nhiredis-example-libuv:\n\t@echo \"Please specify LIBUV_DIR (e.g. ../libuv/)\"\n\t@false\nelse\nhiredis-example-libuv: examples/example-libuv.c adapters/libuv.h $(STLIBNAME)\n\t$(CC) -o examples/$@ $(REAL_CFLAGS) -I. -I$(LIBUV_DIR)/include $< $(LIBUV_DIR)/.libs/libuv.a -lpthread -lrt $(STLIBNAME) $(REAL_LDFLAGS)\nendif\n\nifeq ($(and $(QT_MOC),$(QT_INCLUDE_DIR),$(QT_LIBRARY_DIR)),)\nhiredis-example-qt:\n\t@echo \"Please specify QT_MOC, QT_INCLUDE_DIR AND QT_LIBRARY_DIR\"\n\t@false\nelse\nhiredis-example-qt: examples/example-qt.cpp adapters/qt.h $(STLIBNAME)\n\t$(QT_MOC) adapters/qt.h -I. -I$(QT_INCLUDE_DIR) -I$(QT_INCLUDE_DIR)/QtCore | \\\n\t    $(CXX) -x c++ -o qt-adapter-moc.o -c - $(REAL_CFLAGS) -I. -I$(QT_INCLUDE_DIR) -I$(QT_INCLUDE_DIR)/QtCore\n\t$(QT_MOC) examples/example-qt.h -I. -I$(QT_INCLUDE_DIR) -I$(QT_INCLUDE_DIR)/QtCore | \\\n\t    $(CXX) -x c++ -o qt-example-moc.o -c - $(REAL_CFLAGS) -I. -I$(QT_INCLUDE_DIR) -I$(QT_INCLUDE_DIR)/QtCore\n\t$(CXX) -o examples/$@ $(REAL_CFLAGS) $(REAL_LDFLAGS) -I. -I$(QT_INCLUDE_DIR) -I$(QT_INCLUDE_DIR)/QtCore -L$(QT_LIBRARY_DIR) qt-adapter-moc.o qt-example-moc.o $< -pthread $(STLIBNAME) -lQtCore\nendif\n\nhiredis-example: examples/example.c $(STLIBNAME)\n\t$(CC) -o examples/$@ $(REAL_CFLAGS) -I. $< $(STLIBNAME) $(REAL_LDFLAGS)\n\nhiredis-example-push: examples/example-push.c $(STLIBNAME)\n\t$(CC) -o examples/$@ $(REAL_CFLAGS) -I. $< $(STLIBNAME) $(REAL_LDFLAGS)\n\nexamples: $(EXAMPLES)\n\nTEST_LIBS = $(STLIBNAME)\nifeq ($(USE_SSL),1)\n    TEST_LIBS += $(SSL_STLIBNAME)\n    TEST_LDFLAGS = $(SSL_LDFLAGS) -lssl -lcrypto -lpthread\nendif\n\nhiredis-test: test.o $(TEST_LIBS)\n\t$(CC) -o $@ $(REAL_CFLAGS) -I. $^ $(REAL_LDFLAGS) $(TEST_LDFLAGS)\n\nhiredis-%: %.o $(STLIBNAME)\n\t$(CC) $(REAL_CFLAGS) -o $@ $< $(TEST_LIBS) $(REAL_LDFLAGS)\n\ntest: hiredis-test\n\t./hiredis-test\n\ncheck: hiredis-test\n\tTEST_SSL=$(USE_SSL) ./test.sh\n\n.c.o:\n\t$(CC) -std=c99 -pedantic -c $(REAL_CFLAGS) $<\n\nclean:\n\trm -rf $(DYLIBNAME) $(STLIBNAME) $(SSL_DYLIBNAME) $(SSL_STLIBNAME) $(TESTS) $(PKGCONFNAME) examples/hiredis-example* *.o *.gcda *.gcno *.gcov\n\ndep:\n\t$(CC) $(CPPFLAGS) $(CFLAGS) -MM *.c\n\nINSTALL?= cp -pPR\n\n$(PKGCONFNAME): hiredis.h\n\t@echo \"Generating $@ for pkgconfig...\"\n\t@echo prefix=$(PREFIX) > $@\n\t@echo exec_prefix=\\$${prefix} >> $@\n\t@echo libdir=$(PREFIX)/$(LIBRARY_PATH) >> $@\n\t@echo includedir=$(PREFIX)/$(INCLUDE_PATH) >> $@\n\t@echo >> $@\n\t@echo Name: hiredis >> $@\n\t@echo Description: Minimalistic C client library for Redis. >> $@\n\t@echo Version: $(HIREDIS_MAJOR).$(HIREDIS_MINOR).$(HIREDIS_PATCH) >> $@\n\t@echo Libs: -L\\$${libdir} -lhiredis >> $@\n\t@echo Cflags: -I\\$${includedir} -D_FILE_OFFSET_BITS=64 >> $@\n\n$(SSL_PKGCONFNAME): hiredis_ssl.h\n\t@echo \"Generating $@ for pkgconfig...\"\n\t@echo prefix=$(PREFIX) > $@\n\t@echo exec_prefix=\\$${prefix} >> $@\n\t@echo libdir=$(PREFIX)/$(LIBRARY_PATH) >> $@\n\t@echo includedir=$(PREFIX)/$(INCLUDE_PATH) >> $@\n\t@echo >> $@\n\t@echo Name: hiredis_ssl >> $@\n\t@echo Description: SSL Support for hiredis. >> $@\n\t@echo Version: $(HIREDIS_MAJOR).$(HIREDIS_MINOR).$(HIREDIS_PATCH) >> $@\n\t@echo Requires: hiredis >> $@\n\t@echo Libs: -L\\$${libdir} -lhiredis_ssl >> $@\n\t@echo Libs.private: -lssl -lcrypto >> $@\n\ninstall: $(DYLIBNAME) $(STLIBNAME) $(PKGCONFNAME)\n\tmkdir -p $(INSTALL_INCLUDE_PATH) $(INSTALL_INCLUDE_PATH)/adapters $(INSTALL_LIBRARY_PATH)\n\t$(INSTALL) hiredis.h async.h read.h sds.h alloc.h $(INSTALL_INCLUDE_PATH)\n\t$(INSTALL) adapters/*.h $(INSTALL_INCLUDE_PATH)/adapters\n\t$(INSTALL) $(DYLIBNAME) $(INSTALL_LIBRARY_PATH)/$(DYLIB_MINOR_NAME)\n\tcd $(INSTALL_LIBRARY_PATH) && ln -sf $(DYLIB_MINOR_NAME) $(DYLIBNAME)\n\t$(INSTALL) $(STLIBNAME) $(INSTALL_LIBRARY_PATH)\n\tmkdir -p $(INSTALL_PKGCONF_PATH)\n\t$(INSTALL) $(PKGCONFNAME) $(INSTALL_PKGCONF_PATH)\n\nifeq ($(USE_SSL),1)\ninstall: install-ssl\n\ninstall-ssl: $(SSL_DYLIBNAME) $(SSL_STLIBNAME) $(SSL_PKGCONFNAME)\n\tmkdir -p $(INSTALL_INCLUDE_PATH) $(INSTALL_LIBRARY_PATH)\n\t$(INSTALL) hiredis_ssl.h $(INSTALL_INCLUDE_PATH)\n\t$(INSTALL) $(SSL_DYLIBNAME) $(INSTALL_LIBRARY_PATH)/$(SSL_DYLIB_MINOR_NAME)\n\tcd $(INSTALL_LIBRARY_PATH) && ln -sf $(SSL_DYLIB_MINOR_NAME) $(SSL_DYLIBNAME)\n\t$(INSTALL) $(SSL_STLIBNAME) $(INSTALL_LIBRARY_PATH)\n\tmkdir -p $(INSTALL_PKGCONF_PATH)\n\t$(INSTALL) $(SSL_PKGCONFNAME) $(INSTALL_PKGCONF_PATH)\nendif\n\n32bit:\n\t@echo \"\"\n\t@echo \"WARNING: if this fails under Linux you probably need to install libc6-dev-i386\"\n\t@echo \"\"\n\t$(MAKE) CFLAGS=\"-m32\" LDFLAGS=\"-m32\"\n\n32bit-vars:\n\t$(eval CFLAGS=-m32)\n\t$(eval LDFLAGS=-m32)\n\ngprof:\n\t$(MAKE) CFLAGS=\"-pg\" LDFLAGS=\"-pg\"\n\ngcov:\n\t$(MAKE) CFLAGS=\"-fprofile-arcs -ftest-coverage\" LDFLAGS=\"-fprofile-arcs\"\n\ncoverage: gcov\n\tmake check\n\tmkdir -p tmp/lcov\n\tlcov -d . -c -o tmp/lcov/hiredis.info\n\tgenhtml --legend -o tmp/lcov/report tmp/lcov/hiredis.info\n\nnoopt:\n\t$(MAKE) OPTIMIZATION=\"\"\n\n.PHONY: all test check clean dep install 32bit 32bit-vars gprof gcov noopt\n"
  },
  {
    "path": "deps/hiredis/README.md",
    "content": "[![Build Status](https://travis-ci.org/redis/hiredis.png)](https://travis-ci.org/redis/hiredis)\n\n**This Readme reflects the latest changed in the master branch. See [v1.0.0](https://github.com/redis/hiredis/tree/v1.0.0) for the Readme and documentation for the latest release ([API/ABI history](https://abi-laboratory.pro/?view=timeline&l=hiredis)).**\n\n# HIREDIS\n\nHiredis is a minimalistic C client library for the [Redis](http://redis.io/) database.\n\nIt is minimalistic because it just adds minimal support for the protocol, but\nat the same time it uses a high level printf-alike API in order to make it\nmuch higher level than otherwise suggested by its minimal code base and the\nlack of explicit bindings for every Redis command.\n\nApart from supporting sending commands and receiving replies, it comes with\na reply parser that is decoupled from the I/O layer. It\nis a stream parser designed for easy reusability, which can for instance be used\nin higher level language bindings for efficient reply parsing.\n\nHiredis only supports the binary-safe Redis protocol, so you can use it with any\nRedis version >= 1.2.0.\n\nThe library comes with multiple APIs. There is the\n*synchronous API*, the *asynchronous API* and the *reply parsing API*.\n\n## Upgrading to `1.0.0`\n\nVersion 1.0.0 marks the first stable release of Hiredis.\nIt includes some minor breaking changes, mostly to make the exposed API more uniform and self-explanatory.\nIt also bundles the updated `sds` library, to sync up with upstream and Redis.\nFor code changes see the [Changelog](CHANGELOG.md).\n\n_Note:  As described below, a few member names have been changed but most applications should be able to upgrade with minor code changes and recompiling._\n\n## IMPORTANT:  Breaking changes from `0.14.1` -> `1.0.0`\n\n* `redisContext` has two additional members (`free_privdata`, and `privctx`).\n* `redisOptions.timeout` has been renamed to `redisOptions.connect_timeout`, and we've added `redisOptions.command_timeout`.\n* `redisReplyObjectFunctions.createArray` now takes `size_t` instead of `int` for its length parameter.\n\n## IMPORTANT:  Breaking changes when upgrading from 0.13.x -> 0.14.x\n\nBulk and multi-bulk lengths less than -1 or greater than `LLONG_MAX` are now\nprotocol errors. This is consistent with the RESP specification. On 32-bit\nplatforms, the upper bound is lowered to `SIZE_MAX`.\n\nChange `redisReply.len` to `size_t`, as it denotes the the size of a string\n\nUser code should compare this to `size_t` values as well.  If it was used to\ncompare to other values, casting might be necessary or can be removed, if\ncasting was applied before.\n\n## Upgrading from `<0.9.0`\n\nVersion 0.9.0 is a major overhaul of hiredis in every aspect. However, upgrading existing\ncode using hiredis should not be a big pain. The key thing to keep in mind when\nupgrading is that hiredis >= 0.9.0 uses a `redisContext*` to keep state, in contrast to\nthe stateless 0.0.1 that only has a file descriptor to work with.\n\n## Synchronous API\n\nTo consume the synchronous API, there are only a few function calls that need to be introduced:\n\n```c\nredisContext *redisConnect(const char *ip, int port);\nvoid *redisCommand(redisContext *c, const char *format, ...);\nvoid freeReplyObject(void *reply);\n```\n\n### Connecting\n\nThe function `redisConnect` is used to create a so-called `redisContext`. The\ncontext is where Hiredis holds state for a connection. The `redisContext`\nstruct has an integer `err` field that is non-zero when the connection is in\nan error state. The field `errstr` will contain a string with a description of\nthe error. More information on errors can be found in the **Errors** section.\nAfter trying to connect to Redis using `redisConnect` you should\ncheck the `err` field to see if establishing the connection was successful:\n```c\nredisContext *c = redisConnect(\"127.0.0.1\", 6379);\nif (c == NULL || c->err) {\n    if (c) {\n        printf(\"Error: %s\\n\", c->errstr);\n        // handle error\n    } else {\n        printf(\"Can't allocate redis context\\n\");\n    }\n}\n```\n\n*Note: A `redisContext` is not thread-safe.*\n\n### Sending commands\n\nThere are several ways to issue commands to Redis. The first that will be introduced is\n`redisCommand`. This function takes a format similar to printf. In the simplest form,\nit is used like this:\n```c\nreply = redisCommand(context, \"SET foo bar\");\n```\n\nThe specifier `%s` interpolates a string in the command, and uses `strlen` to\ndetermine the length of the string:\n```c\nreply = redisCommand(context, \"SET foo %s\", value);\n```\nWhen you need to pass binary safe strings in a command, the `%b` specifier can be\nused. Together with a pointer to the string, it requires a `size_t` length argument\nof the string:\n```c\nreply = redisCommand(context, \"SET foo %b\", value, (size_t) valuelen);\n```\nInternally, Hiredis splits the command in different arguments and will\nconvert it to the protocol used to communicate with Redis.\nOne or more spaces separates arguments, so you can use the specifiers\nanywhere in an argument:\n```c\nreply = redisCommand(context, \"SET key:%s %s\", myid, value);\n```\n\n### Using replies\n\nThe return value of `redisCommand` holds a reply when the command was\nsuccessfully executed. When an error occurs, the return value is `NULL` and\nthe `err` field in the context will be set (see section on **Errors**).\nOnce an error is returned the context cannot be reused and you should set up\na new connection.\n\nThe standard replies that `redisCommand` are of the type `redisReply`. The\n`type` field in the `redisReply` should be used to test what kind of reply\nwas received:\n\n### RESP2\n\n* **`REDIS_REPLY_STATUS`**:\n    * The command replied with a status reply. The status string can be accessed using `reply->str`.\n      The length of this string can be accessed using `reply->len`.\n\n* **`REDIS_REPLY_ERROR`**:\n    *  The command replied with an error. The error string can be accessed identical to `REDIS_REPLY_STATUS`.\n\n* **`REDIS_REPLY_INTEGER`**:\n    * The command replied with an integer. The integer value can be accessed using the\n      `reply->integer` field of type `long long`.\n\n* **`REDIS_REPLY_NIL`**:\n    * The command replied with a **nil** object. There is no data to access.\n\n* **`REDIS_REPLY_STRING`**:\n    * A bulk (string) reply. The value of the reply can be accessed using `reply->str`.\n      The length of this string can be accessed using `reply->len`.\n\n* **`REDIS_REPLY_ARRAY`**:\n    * A multi bulk reply. The number of elements in the multi bulk reply is stored in\n      `reply->elements`. Every element in the multi bulk reply is a `redisReply` object as well\n      and can be accessed via `reply->element[..index..]`.\n      Redis may reply with nested arrays but this is fully supported.\n\n### RESP3\n\nHiredis also supports every new `RESP3` data type which are as follows.  For more information about the protocol see the `RESP3` [specification.](https://github.com/antirez/RESP3/blob/master/spec.md)\n\n* **`REDIS_REPLY_DOUBLE`**:\n    * The command replied with a double-precision floating point number.\n      The value is stored as a string in the `str` member, and can be converted with `strtod` or similar.\n\n* **`REDIS_REPLY_BOOL`**:\n    * A boolean true/false reply.\n      The value is stored in the `integer` member and will be either `0` or `1`.\n\n* **`REDIS_REPLY_MAP`**:\n    * An array with the added invariant that there will always be an even number of elements.\n      The MAP is functionally equivelant to `REDIS_REPLY_ARRAY` except for the previously mentioned invariant.\n\n* **`REDIS_REPLY_SET`**:\n    * An array response where each entry is unique.\n      Like the MAP type, the data is identical to an array response except there are no duplicate values.\n\n* **`REDIS_REPLY_PUSH`**:\n    * An array that can be generated spontaneously by Redis.\n      This array response will always contain at least two subelements.  The first contains the type of `PUSH` message (e.g. `message`, or `invalidate`), and the second being a sub-array with the `PUSH` payload itself.\n\n* **`REDIS_REPLY_ATTR`**:\n    * An array structurally identical to a `MAP` but intended as meta-data about a reply.\n      _As of Redis 6.0.6 this reply type is not used in Redis_\n\n* **`REDIS_REPLY_BIGNUM`**:\n    * A string representing an arbitrarily large signed or unsigned integer value.\n      The number will be encoded as a string in the `str` member of `redisReply`.\n\n* **`REDIS_REPLY_VERB`**:\n    * A verbatim string, intended to be presented to the user without modification.\n      The string payload is stored in the `str` memeber, and type data is stored in the `vtype` member (e.g. `txt` for raw text or `md` for markdown).\n\nReplies should be freed using the `freeReplyObject()` function.\nNote that this function will take care of freeing sub-reply objects\ncontained in arrays and nested arrays, so there is no need for the user to\nfree the sub replies (it is actually harmful and will corrupt the memory).\n\n**Important:** the current version of hiredis (1.0.0) frees replies when the\nasynchronous API is used. This means you should not call `freeReplyObject` when\nyou use this API. The reply is cleaned up by hiredis _after_ the callback\nreturns.  We may introduce a flag to make this configurable in future versions of the library.\n\n### Cleaning up\n\nTo disconnect and free the context the following function can be used:\n```c\nvoid redisFree(redisContext *c);\n```\nThis function immediately closes the socket and then frees the allocations done in\ncreating the context.\n\n### Sending commands (cont'd)\n\nTogether with `redisCommand`, the function `redisCommandArgv` can be used to issue commands.\nIt has the following prototype:\n```c\nvoid *redisCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);\n```\nIt takes the number of arguments `argc`, an array of strings `argv` and the lengths of the\narguments `argvlen`. For convenience, `argvlen` may be set to `NULL` and the function will\nuse `strlen(3)` on every argument to determine its length. Obviously, when any of the arguments\nneed to be binary safe, the entire array of lengths `argvlen` should be provided.\n\nThe return value has the same semantic as `redisCommand`.\n\n### Pipelining\n\nTo explain how Hiredis supports pipelining in a blocking connection, there needs to be\nunderstanding of the internal execution flow.\n\nWhen any of the functions in the `redisCommand` family is called, Hiredis first formats the\ncommand according to the Redis protocol. The formatted command is then put in the output buffer\nof the context. This output buffer is dynamic, so it can hold any number of commands.\nAfter the command is put in the output buffer, `redisGetReply` is called. This function has the\nfollowing two execution paths:\n\n1. The input buffer is non-empty:\n    * Try to parse a single reply from the input buffer and return it\n    * If no reply could be parsed, continue at *2*\n2. The input buffer is empty:\n    * Write the **entire** output buffer to the socket\n    * Read from the socket until a single reply could be parsed\n\nThe function `redisGetReply` is exported as part of the Hiredis API and can be used when a reply\nis expected on the socket. To pipeline commands, the only things that needs to be done is\nfilling up the output buffer. For this cause, two commands can be used that are identical\nto the `redisCommand` family, apart from not returning a reply:\n```c\nvoid redisAppendCommand(redisContext *c, const char *format, ...);\nvoid redisAppendCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);\n```\nAfter calling either function one or more times, `redisGetReply` can be used to receive the\nsubsequent replies. The return value for this function is either `REDIS_OK` or `REDIS_ERR`, where\nthe latter means an error occurred while reading a reply. Just as with the other commands,\nthe `err` field in the context can be used to find out what the cause of this error is.\n\nThe following examples shows a simple pipeline (resulting in only a single call to `write(2)` and\na single call to `read(2)`):\n```c\nredisReply *reply;\nredisAppendCommand(context,\"SET foo bar\");\nredisAppendCommand(context,\"GET foo\");\nredisGetReply(context,(void *)&reply); // reply for SET\nfreeReplyObject(reply);\nredisGetReply(context,(void *)&reply); // reply for GET\nfreeReplyObject(reply);\n```\nThis API can also be used to implement a blocking subscriber:\n```c\nreply = redisCommand(context,\"SUBSCRIBE foo\");\nfreeReplyObject(reply);\nwhile(redisGetReply(context,(void *)&reply) == REDIS_OK) {\n    // consume message\n    freeReplyObject(reply);\n}\n```\n### Errors\n\nWhen a function call is not successful, depending on the function either `NULL` or `REDIS_ERR` is\nreturned. The `err` field inside the context will be non-zero and set to one of the\nfollowing constants:\n\n* **`REDIS_ERR_IO`**:\n    There was an I/O error while creating the connection, trying to write\n    to the socket or read from the socket. If you included `errno.h` in your\n    application, you can use the global `errno` variable to find out what is\n    wrong.\n\n* **`REDIS_ERR_EOF`**:\n    The server closed the connection which resulted in an empty read.\n\n* **`REDIS_ERR_PROTOCOL`**:\n    There was an error while parsing the protocol.\n\n* **`REDIS_ERR_OTHER`**:\n    Any other error. Currently, it is only used when a specified hostname to connect\n    to cannot be resolved.\n\nIn every case, the `errstr` field in the context will be set to hold a string representation\nof the error.\n\n## Asynchronous API\n\nHiredis comes with an asynchronous API that works easily with any event library.\nExamples are bundled that show using Hiredis with [libev](http://software.schmorp.de/pkg/libev.html)\nand [libevent](http://monkey.org/~provos/libevent/).\n\n### Connecting\n\nThe function `redisAsyncConnect` can be used to establish a non-blocking connection to\nRedis. It returns a pointer to the newly created `redisAsyncContext` struct. The `err` field\nshould be checked after creation to see if there were errors creating the connection.\nBecause the connection that will be created is non-blocking, the kernel is not able to\ninstantly return if the specified host and port is able to accept a connection.\n\n*Note: A `redisAsyncContext` is not thread-safe.*\n\n```c\nredisAsyncContext *c = redisAsyncConnect(\"127.0.0.1\", 6379);\nif (c->err) {\n    printf(\"Error: %s\\n\", c->errstr);\n    // handle error\n}\n```\n\nThe asynchronous context can hold a disconnect callback function that is called when the\nconnection is disconnected (either because of an error or per user request). This function should\nhave the following prototype:\n```c\nvoid(const redisAsyncContext *c, int status);\n```\nOn a disconnect, the `status` argument is set to `REDIS_OK` when disconnection was initiated by the\nuser, or `REDIS_ERR` when the disconnection was caused by an error. When it is `REDIS_ERR`, the `err`\nfield in the context can be accessed to find out the cause of the error.\n\nThe context object is always freed after the disconnect callback fired. When a reconnect is needed,\nthe disconnect callback is a good point to do so.\n\nSetting the disconnect callback can only be done once per context. For subsequent calls it will\nreturn `REDIS_ERR`. The function to set the disconnect callback has the following prototype:\n```c\nint redisAsyncSetDisconnectCallback(redisAsyncContext *ac, redisDisconnectCallback *fn);\n```\n`ac->data` may be used to pass user data to this callback, the same can be done for redisConnectCallback.\n### Sending commands and their callbacks\n\nIn an asynchronous context, commands are automatically pipelined due to the nature of an event loop.\nTherefore, unlike the synchronous API, there is only a single way to send commands.\nBecause commands are sent to Redis asynchronously, issuing a command requires a callback function\nthat is called when the reply is received. Reply callbacks should have the following prototype:\n```c\nvoid(redisAsyncContext *c, void *reply, void *privdata);\n```\nThe `privdata` argument can be used to curry arbitrary data to the callback from the point where\nthe command is initially queued for execution.\n\nThe functions that can be used to issue commands in an asynchronous context are:\n```c\nint redisAsyncCommand(\n  redisAsyncContext *ac, redisCallbackFn *fn, void *privdata,\n  const char *format, ...);\nint redisAsyncCommandArgv(\n  redisAsyncContext *ac, redisCallbackFn *fn, void *privdata,\n  int argc, const char **argv, const size_t *argvlen);\n```\nBoth functions work like their blocking counterparts. The return value is `REDIS_OK` when the command\nwas successfully added to the output buffer and `REDIS_ERR` otherwise. Example: when the connection\nis being disconnected per user-request, no new commands may be added to the output buffer and `REDIS_ERR` is\nreturned on calls to the `redisAsyncCommand` family.\n\nIf the reply for a command with a `NULL` callback is read, it is immediately freed. When the callback\nfor a command is non-`NULL`, the memory is freed immediately following the callback: the reply is only\nvalid for the duration of the callback.\n\nAll pending callbacks are called with a `NULL` reply when the context encountered an error.\n\n### Disconnecting\n\nAn asynchronous connection can be terminated using:\n```c\nvoid redisAsyncDisconnect(redisAsyncContext *ac);\n```\nWhen this function is called, the connection is **not** immediately terminated. Instead, new\ncommands are no longer accepted and the connection is only terminated when all pending commands\nhave been written to the socket, their respective replies have been read and their respective\ncallbacks have been executed. After this, the disconnection callback is executed with the\n`REDIS_OK` status and the context object is freed.\n\n### Hooking it up to event library *X*\n\nThere are a few hooks that need to be set on the context object after it is created.\nSee the `adapters/` directory for bindings to *libev* and *libevent*.\n\n## Reply parsing API\n\nHiredis comes with a reply parsing API that makes it easy for writing higher\nlevel language bindings.\n\nThe reply parsing API consists of the following functions:\n```c\nredisReader *redisReaderCreate(void);\nvoid redisReaderFree(redisReader *reader);\nint redisReaderFeed(redisReader *reader, const char *buf, size_t len);\nint redisReaderGetReply(redisReader *reader, void **reply);\n```\nThe same set of functions are used internally by hiredis when creating a\nnormal Redis context, the above API just exposes it to the user for a direct\nusage.\n\n### Usage\n\nThe function `redisReaderCreate` creates a `redisReader` structure that holds a\nbuffer with unparsed data and state for the protocol parser.\n\nIncoming data -- most likely from a socket -- can be placed in the internal\nbuffer of the `redisReader` using `redisReaderFeed`. This function will make a\ncopy of the buffer pointed to by `buf` for `len` bytes. This data is parsed\nwhen `redisReaderGetReply` is called. This function returns an integer status\nand a reply object (as described above) via `void **reply`. The returned status\ncan be either `REDIS_OK` or `REDIS_ERR`, where the latter means something went\nwrong (either a protocol error, or an out of memory error).\n\nThe parser limits the level of nesting for multi bulk payloads to 7. If the\nmulti bulk nesting level is higher than this, the parser returns an error.\n\n### Customizing replies\n\nThe function `redisReaderGetReply` creates `redisReply` and makes the function\nargument `reply` point to the created `redisReply` variable. For instance, if\nthe response of type `REDIS_REPLY_STATUS` then the `str` field of `redisReply`\nwill hold the status as a vanilla C string. However, the functions that are\nresponsible for creating instances of the `redisReply` can be customized by\nsetting the `fn` field on the `redisReader` struct. This should be done\nimmediately after creating the `redisReader`.\n\nFor example, [hiredis-rb](https://github.com/pietern/hiredis-rb/blob/master/ext/hiredis_ext/reader.c)\nuses customized reply object functions to create Ruby objects.\n\n### Reader max buffer\n\nBoth when using the Reader API directly or when using it indirectly via a\nnormal Redis context, the redisReader structure uses a buffer in order to\naccumulate data from the server.\nUsually this buffer is destroyed when it is empty and is larger than 16\nKiB in order to avoid wasting memory in unused buffers\n\nHowever when working with very big payloads destroying the buffer may slow\ndown performances considerably, so it is possible to modify the max size of\nan idle buffer changing the value of the `maxbuf` field of the reader structure\nto the desired value. The special value of 0 means that there is no maximum\nvalue for an idle buffer, so the buffer will never get freed.\n\nFor instance if you have a normal Redis context you can set the maximum idle\nbuffer to zero (unlimited) just with:\n```c\ncontext->reader->maxbuf = 0;\n```\nThis should be done only in order to maximize performances when working with\nlarge payloads. The context should be set back to `REDIS_READER_MAX_BUF` again\nas soon as possible in order to prevent allocation of useless memory.\n\n### Reader max array elements\n\nBy default the hiredis reply parser sets the maximum number of multi-bulk elements\nto 2^32 - 1 or 4,294,967,295 entries.  If you need to process multi-bulk replies\nwith more than this many elements you can set the value higher or to zero, meaning\nunlimited with:\n```c\ncontext->reader->maxelements = 0;\n```\n\n## SSL/TLS Support\n\n### Building\n\nSSL/TLS support is not built by default and requires an explicit flag:\n\n    make USE_SSL=1\n\nThis requires OpenSSL development package (e.g. including header files to be\navailable.\n\nWhen enabled, SSL/TLS support is built into extra `libhiredis_ssl.a` and\n`libhiredis_ssl.so` static/dynamic libraries. This leaves the original libraries\nunaffected so no additional dependencies are introduced.\n\n### Using it\n\nFirst, you'll need to make sure you include the SSL header file:\n\n```c\n#include \"hiredis.h\"\n#include \"hiredis_ssl.h\"\n```\n\nYou will also need to link against `libhiredis_ssl`, **in addition** to\n`libhiredis` and add `-lssl -lcrypto` to satisfy its dependencies.\n\nHiredis implements SSL/TLS on top of its normal `redisContext` or\n`redisAsyncContext`, so you will need to establish a connection first and then\ninitiate an SSL/TLS handshake.\n\n#### Hiredis OpenSSL Wrappers\n\nBefore Hiredis can negotiate an SSL/TLS connection, it is necessary to\ninitialize OpenSSL and create a context. You can do that in two ways:\n\n1. Work directly with the OpenSSL API to initialize the library's global context\n   and create `SSL_CTX *` and `SSL *` contexts. With an `SSL *` object you can\n   call `redisInitiateSSL()`.\n2. Work with a set of Hiredis-provided wrappers around OpenSSL, create a\n   `redisSSLContext` object to hold configuration and use\n   `redisInitiateSSLWithContext()` to initiate the SSL/TLS handshake.\n\n```c\n/* An Hiredis SSL context. It holds SSL configuration and can be reused across\n * many contexts.\n */\nredisSSLContext *ssl;\n\n/* An error variable to indicate what went wrong, if the context fails to\n * initialize.\n */\nredisSSLContextError ssl_error;\n\n/* Initialize global OpenSSL state.\n *\n * You should call this only once when your app initializes, and only if\n * you don't explicitly or implicitly initialize OpenSSL it elsewhere.\n */\nredisInitOpenSSL();\n\n/* Create SSL context */\nssl = redisCreateSSLContext(\n    \"cacertbundle.crt\",     /* File name of trusted CA/ca bundle file, optional */\n    \"/path/to/certs\",       /* Path of trusted certificates, optional */\n    \"client_cert.pem\",      /* File name of client certificate file, optional */\n    \"client_key.pem\",       /* File name of client private key, optional */\n    \"redis.mydomain.com\",   /* Server name to request (SNI), optional */\n    &ssl_error\n    ) != REDIS_OK) {\n        printf(\"SSL error: %s\\n\", redisSSLContextGetError(ssl_error);\n        /* Abort... */\n    }\n\n/* Create Redis context and establish connection */\nc = redisConnect(\"localhost\", 6443);\nif (c == NULL || c->err) {\n    /* Handle error and abort... */\n}\n\n/* Negotiate SSL/TLS */\nif (redisInitiateSSLWithContext(c, ssl) != REDIS_OK) {\n    /* Handle error, in c->err / c->errstr */\n}\n```\n\n## RESP3 PUSH replies\nRedis 6.0 introduced PUSH replies with the reply-type `>`.  These messages are generated spontaneously and can arrive at any time, so must be handled using callbacks.\n\n### Default behavior\nHiredis installs handlers on `redisContext` and `redisAsyncContext` by default, which will intercept and free any PUSH replies detected.  This means existing code will work as-is after upgrading to Redis 6 and switching to `RESP3`.\n\n### Custom PUSH handler prototypes\nThe callback prototypes differ between `redisContext` and `redisAsyncContext`.\n\n#### redisContext\n```c\nvoid my_push_handler(void *privdata, void *reply) {\n    /* Handle the reply */\n\n    /* Note: We need to free the reply in our custom handler for\n             blocking contexts.  This lets us keep the reply if\n             we want. */\n    freeReplyObject(reply);\n}\n```\n\n#### redisAsyncContext\n```c\nvoid my_async_push_handler(redisAsyncContext *ac, void *reply) {\n    /* Handle the reply */\n\n    /* Note:  Because async hiredis always frees replies, you should\n              not call freeReplyObject in an async push callback. */\n}\n```\n\n### Installing a custom handler\nThere are two ways to set your own PUSH handlers.\n\n1. Set `push_cb` or `async_push_cb` in the `redisOptions` struct and connect with `redisConnectWithOptions` or `redisAsyncConnectWithOptions`.\n    ```c\n    redisOptions = {0};\n    REDIS_OPTIONS_SET_TCP(&options, \"127.0.0.1\", 6379);\n    options->push_cb = my_push_handler;\n    redisContext *context = redisConnectWithOptions(&options);\n    ```\n2.  Call `redisSetPushCallback` or `redisAsyncSetPushCallback` on a connected context.\n    ```c\n    redisContext *context = redisConnect(\"127.0.0.1\", 6379);\n    redisSetPushCallback(context, my_push_handler);\n    ```\n\n    _Note `redisSetPushCallback` and `redisAsyncSetPushCallback` both return any currently configured handler,  making it easy to override and then return to the old value._\n\n### Specifying no handler\nIf you have a unique use-case where you don't want hiredis to automatically intercept and free PUSH replies, you will want to configure no handler at all.  This can be done in two ways.\n1.  Set the `REDIS_OPT_NO_PUSH_AUTOFREE` flag in `redisOptions` and leave the callback function pointer `NULL`.\n    ```c\n    redisOptions = {0};\n    REDIS_OPTIONS_SET_TCP(&options, \"127.0.0.1\", 6379);\n    options->options |= REDIS_OPT_NO_PUSH_AUTOFREE;\n    redisContext *context = redisConnectWithOptions(&options);\n    ```\n3.  Call `redisSetPushCallback` with `NULL` once connected.\n    ```c\n    redisContext *context = redisConnect(\"127.0.0.1\", 6379);\n    redisSetPushCallback(context, NULL);\n    ```\n\n    _Note:  With no handler configured, calls to `redisCommand` may generate more than one reply, so this strategy is only applicable when there's some kind of blocking`redisGetReply()` loop (e.g. `MONITOR` or `SUBSCRIBE` workloads)._\n\n## Allocator injection\n\nHiredis uses a pass-thru structure of function pointers defined in [alloc.h](https://github.com/redis/hiredis/blob/f5d25850/alloc.h#L41) that contain the currently configured allocation and deallocation functions.  By default they just point to libc (`malloc`, `calloc`, `realloc`, etc).\n\n### Overriding\n\nOne can override the allocators like so:\n\n```c\nhiredisAllocFuncs myfuncs = {\n    .mallocFn = my_malloc,\n    .callocFn = my_calloc,\n    .reallocFn = my_realloc,\n    .strdupFn = my_strdup,\n    .freeFn = my_free,\n};\n\n// Override allocators (function returns current allocators if needed)\nhiredisAllocFuncs orig = hiredisSetAllocators(&myfuncs);\n```\n\nTo reset the allocators to their default libc function simply call:\n\n```c\nhiredisResetAllocators();\n```\n\n## AUTHORS\n\nSalvatore Sanfilippo (antirez at gmail),\\\nPieter Noordhuis (pcnoordhuis at gmail)\\\nMichael Grunder (michael dot grunder at gmail)\n\n_Hiredis is released under the BSD license._\n"
  },
  {
    "path": "deps/hiredis/adapters/ae.h",
    "content": "/*\n * Copyright (c) 2010-2011, Pieter Noordhuis <pcnoordhuis at gmail dot com>\n *\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __HIREDIS_AE_H__\n#define __HIREDIS_AE_H__\n#include <sys/types.h>\n#include <ae.h>\n#include \"../hiredis.h\"\n#include \"../async.h\"\n\ntypedef struct redisAeEvents {\n    redisAsyncContext *context;\n    aeEventLoop *loop;\n    int fd;\n    int reading, writing;\n} redisAeEvents;\n\nstatic void redisAeReadEvent(aeEventLoop *el, int fd, void *privdata, int mask) {\n    ((void)el); ((void)fd); ((void)mask);\n\n    redisAeEvents *e = (redisAeEvents*)privdata;\n    redisAsyncHandleRead(e->context);\n}\n\nstatic void redisAeWriteEvent(aeEventLoop *el, int fd, void *privdata, int mask) {\n    ((void)el); ((void)fd); ((void)mask);\n\n    redisAeEvents *e = (redisAeEvents*)privdata;\n    redisAsyncHandleWrite(e->context);\n}\n\nstatic void redisAeAddRead(void *privdata) {\n    redisAeEvents *e = (redisAeEvents*)privdata;\n    aeEventLoop *loop = e->loop;\n    if (!e->reading) {\n        e->reading = 1;\n        aeCreateFileEvent(loop,e->fd,AE_READABLE,redisAeReadEvent,e);\n    }\n}\n\nstatic void redisAeDelRead(void *privdata) {\n    redisAeEvents *e = (redisAeEvents*)privdata;\n    aeEventLoop *loop = e->loop;\n    if (e->reading) {\n        e->reading = 0;\n        aeDeleteFileEvent(loop,e->fd,AE_READABLE);\n    }\n}\n\nstatic void redisAeAddWrite(void *privdata) {\n    redisAeEvents *e = (redisAeEvents*)privdata;\n    aeEventLoop *loop = e->loop;\n    if (!e->writing) {\n        e->writing = 1;\n        aeCreateFileEvent(loop,e->fd,AE_WRITABLE,redisAeWriteEvent,e);\n    }\n}\n\nstatic void redisAeDelWrite(void *privdata) {\n    redisAeEvents *e = (redisAeEvents*)privdata;\n    aeEventLoop *loop = e->loop;\n    if (e->writing) {\n        e->writing = 0;\n        aeDeleteFileEvent(loop,e->fd,AE_WRITABLE);\n    }\n}\n\nstatic void redisAeCleanup(void *privdata) {\n    redisAeEvents *e = (redisAeEvents*)privdata;\n    redisAeDelRead(privdata);\n    redisAeDelWrite(privdata);\n    hi_free(e);\n}\n\nstatic int redisAeAttach(aeEventLoop *loop, redisAsyncContext *ac) {\n    redisContext *c = &(ac->c);\n    redisAeEvents *e;\n\n    /* Nothing should be attached when something is already attached */\n    if (ac->ev.data != NULL)\n        return REDIS_ERR;\n\n    /* Create container for context and r/w events */\n    e = (redisAeEvents*)hi_malloc(sizeof(*e));\n    if (e == NULL)\n        return REDIS_ERR;\n\n    e->context = ac;\n    e->loop = loop;\n    e->fd = c->fd;\n    e->reading = e->writing = 0;\n\n    /* Register functions to start/stop listening for events */\n    ac->ev.addRead = redisAeAddRead;\n    ac->ev.delRead = redisAeDelRead;\n    ac->ev.addWrite = redisAeAddWrite;\n    ac->ev.delWrite = redisAeDelWrite;\n    ac->ev.cleanup = redisAeCleanup;\n    ac->ev.data = e;\n\n    return REDIS_OK;\n}\n#endif\n"
  },
  {
    "path": "deps/hiredis/adapters/glib.h",
    "content": "#ifndef __HIREDIS_GLIB_H__\n#define __HIREDIS_GLIB_H__\n\n#include <glib.h>\n\n#include \"../hiredis.h\"\n#include \"../async.h\"\n\ntypedef struct\n{\n    GSource source;\n    redisAsyncContext *ac;\n    GPollFD poll_fd;\n} RedisSource;\n\nstatic void\nredis_source_add_read (gpointer data)\n{\n    RedisSource *source = (RedisSource *)data;\n    g_return_if_fail(source);\n    source->poll_fd.events |= G_IO_IN;\n    g_main_context_wakeup(g_source_get_context((GSource *)data));\n}\n\nstatic void\nredis_source_del_read (gpointer data)\n{\n    RedisSource *source = (RedisSource *)data;\n    g_return_if_fail(source);\n    source->poll_fd.events &= ~G_IO_IN;\n    g_main_context_wakeup(g_source_get_context((GSource *)data));\n}\n\nstatic void\nredis_source_add_write (gpointer data)\n{\n    RedisSource *source = (RedisSource *)data;\n    g_return_if_fail(source);\n    source->poll_fd.events |= G_IO_OUT;\n    g_main_context_wakeup(g_source_get_context((GSource *)data));\n}\n\nstatic void\nredis_source_del_write (gpointer data)\n{\n    RedisSource *source = (RedisSource *)data;\n    g_return_if_fail(source);\n    source->poll_fd.events &= ~G_IO_OUT;\n    g_main_context_wakeup(g_source_get_context((GSource *)data));\n}\n\nstatic void\nredis_source_cleanup (gpointer data)\n{\n    RedisSource *source = (RedisSource *)data;\n\n    g_return_if_fail(source);\n\n    redis_source_del_read(source);\n    redis_source_del_write(source);\n    /*\n     * It is not our responsibility to remove ourself from the\n     * current main loop. However, we will remove the GPollFD.\n     */\n    if (source->poll_fd.fd >= 0) {\n        g_source_remove_poll((GSource *)data, &source->poll_fd);\n        source->poll_fd.fd = -1;\n    }\n}\n\nstatic gboolean\nredis_source_prepare (GSource *source,\n                      gint    *timeout_)\n{\n    RedisSource *redis = (RedisSource *)source;\n    *timeout_ = -1;\n    return !!(redis->poll_fd.events & redis->poll_fd.revents);\n}\n\nstatic gboolean\nredis_source_check (GSource *source)\n{\n    RedisSource *redis = (RedisSource *)source;\n    return !!(redis->poll_fd.events & redis->poll_fd.revents);\n}\n\nstatic gboolean\nredis_source_dispatch (GSource      *source,\n                       GSourceFunc   callback,\n                       gpointer      user_data)\n{\n    RedisSource *redis = (RedisSource *)source;\n\n    if ((redis->poll_fd.revents & G_IO_OUT)) {\n        redisAsyncHandleWrite(redis->ac);\n        redis->poll_fd.revents &= ~G_IO_OUT;\n    }\n\n    if ((redis->poll_fd.revents & G_IO_IN)) {\n        redisAsyncHandleRead(redis->ac);\n        redis->poll_fd.revents &= ~G_IO_IN;\n    }\n\n    if (callback) {\n        return callback(user_data);\n    }\n\n    return TRUE;\n}\n\nstatic void\nredis_source_finalize (GSource *source)\n{\n    RedisSource *redis = (RedisSource *)source;\n\n    if (redis->poll_fd.fd >= 0) {\n        g_source_remove_poll(source, &redis->poll_fd);\n        redis->poll_fd.fd = -1;\n    }\n}\n\nstatic GSource *\nredis_source_new (redisAsyncContext *ac)\n{\n    static GSourceFuncs source_funcs = {\n        .prepare  = redis_source_prepare,\n        .check     = redis_source_check,\n        .dispatch = redis_source_dispatch,\n        .finalize = redis_source_finalize,\n    };\n    redisContext *c = &ac->c;\n    RedisSource *source;\n\n    g_return_val_if_fail(ac != NULL, NULL);\n\n    source = (RedisSource *)g_source_new(&source_funcs, sizeof *source);\n    if (source == NULL)\n        return NULL;\n\n    source->ac = ac;\n    source->poll_fd.fd = c->fd;\n    source->poll_fd.events = 0;\n    source->poll_fd.revents = 0;\n    g_source_add_poll((GSource *)source, &source->poll_fd);\n\n    ac->ev.addRead = redis_source_add_read;\n    ac->ev.delRead = redis_source_del_read;\n    ac->ev.addWrite = redis_source_add_write;\n    ac->ev.delWrite = redis_source_del_write;\n    ac->ev.cleanup = redis_source_cleanup;\n    ac->ev.data = source;\n\n    return (GSource *)source;\n}\n\n#endif /* __HIREDIS_GLIB_H__ */\n"
  },
  {
    "path": "deps/hiredis/adapters/ivykis.h",
    "content": "#ifndef __HIREDIS_IVYKIS_H__\n#define __HIREDIS_IVYKIS_H__\n#include <iv.h>\n#include \"../hiredis.h\"\n#include \"../async.h\"\n\ntypedef struct redisIvykisEvents {\n    redisAsyncContext *context;\n    struct iv_fd fd;\n} redisIvykisEvents;\n\nstatic void redisIvykisReadEvent(void *arg) {\n    redisAsyncContext *context = (redisAsyncContext *)arg;\n    redisAsyncHandleRead(context);\n}\n\nstatic void redisIvykisWriteEvent(void *arg) {\n    redisAsyncContext *context = (redisAsyncContext *)arg;\n    redisAsyncHandleWrite(context);\n}\n\nstatic void redisIvykisAddRead(void *privdata) {\n    redisIvykisEvents *e = (redisIvykisEvents*)privdata;\n    iv_fd_set_handler_in(&e->fd, redisIvykisReadEvent);\n}\n\nstatic void redisIvykisDelRead(void *privdata) {\n    redisIvykisEvents *e = (redisIvykisEvents*)privdata;\n    iv_fd_set_handler_in(&e->fd, NULL);\n}\n\nstatic void redisIvykisAddWrite(void *privdata) {\n    redisIvykisEvents *e = (redisIvykisEvents*)privdata;\n    iv_fd_set_handler_out(&e->fd, redisIvykisWriteEvent);\n}\n\nstatic void redisIvykisDelWrite(void *privdata) {\n    redisIvykisEvents *e = (redisIvykisEvents*)privdata;\n    iv_fd_set_handler_out(&e->fd, NULL);\n}\n\nstatic void redisIvykisCleanup(void *privdata) {\n    redisIvykisEvents *e = (redisIvykisEvents*)privdata;\n\n    iv_fd_unregister(&e->fd);\n    hi_free(e);\n}\n\nstatic int redisIvykisAttach(redisAsyncContext *ac) {\n    redisContext *c = &(ac->c);\n    redisIvykisEvents *e;\n\n    /* Nothing should be attached when something is already attached */\n    if (ac->ev.data != NULL)\n        return REDIS_ERR;\n\n    /* Create container for context and r/w events */\n    e = (redisIvykisEvents*)hi_malloc(sizeof(*e));\n    if (e == NULL)\n        return REDIS_ERR;\n\n    e->context = ac;\n\n    /* Register functions to start/stop listening for events */\n    ac->ev.addRead = redisIvykisAddRead;\n    ac->ev.delRead = redisIvykisDelRead;\n    ac->ev.addWrite = redisIvykisAddWrite;\n    ac->ev.delWrite = redisIvykisDelWrite;\n    ac->ev.cleanup = redisIvykisCleanup;\n    ac->ev.data = e;\n\n    /* Initialize and install read/write events */\n    IV_FD_INIT(&e->fd);\n    e->fd.fd = c->fd;\n    e->fd.handler_in = redisIvykisReadEvent;\n    e->fd.handler_out = redisIvykisWriteEvent;\n    e->fd.handler_err = NULL;\n    e->fd.cookie = e->context;\n\n    iv_fd_register(&e->fd);\n\n    return REDIS_OK;\n}\n#endif\n"
  },
  {
    "path": "deps/hiredis/adapters/libev.h",
    "content": "/*\n * Copyright (c) 2010-2011, Pieter Noordhuis <pcnoordhuis at gmail dot com>\n *\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __HIREDIS_LIBEV_H__\n#define __HIREDIS_LIBEV_H__\n#include <stdlib.h>\n#include <sys/types.h>\n#include <ev.h>\n#include \"../hiredis.h\"\n#include \"../async.h\"\n\ntypedef struct redisLibevEvents {\n    redisAsyncContext *context;\n    struct ev_loop *loop;\n    int reading, writing;\n    ev_io rev, wev;\n    ev_timer timer;\n} redisLibevEvents;\n\nstatic void redisLibevReadEvent(EV_P_ ev_io *watcher, int revents) {\n#if EV_MULTIPLICITY\n    ((void)loop);\n#endif\n    ((void)revents);\n\n    redisLibevEvents *e = (redisLibevEvents*)watcher->data;\n    redisAsyncHandleRead(e->context);\n}\n\nstatic void redisLibevWriteEvent(EV_P_ ev_io *watcher, int revents) {\n#if EV_MULTIPLICITY\n    ((void)loop);\n#endif\n    ((void)revents);\n\n    redisLibevEvents *e = (redisLibevEvents*)watcher->data;\n    redisAsyncHandleWrite(e->context);\n}\n\nstatic void redisLibevAddRead(void *privdata) {\n    redisLibevEvents *e = (redisLibevEvents*)privdata;\n    struct ev_loop *loop = e->loop;\n    ((void)loop);\n    if (!e->reading) {\n        e->reading = 1;\n        ev_io_start(EV_A_ &e->rev);\n    }\n}\n\nstatic void redisLibevDelRead(void *privdata) {\n    redisLibevEvents *e = (redisLibevEvents*)privdata;\n    struct ev_loop *loop = e->loop;\n    ((void)loop);\n    if (e->reading) {\n        e->reading = 0;\n        ev_io_stop(EV_A_ &e->rev);\n    }\n}\n\nstatic void redisLibevAddWrite(void *privdata) {\n    redisLibevEvents *e = (redisLibevEvents*)privdata;\n    struct ev_loop *loop = e->loop;\n    ((void)loop);\n    if (!e->writing) {\n        e->writing = 1;\n        ev_io_start(EV_A_ &e->wev);\n    }\n}\n\nstatic void redisLibevDelWrite(void *privdata) {\n    redisLibevEvents *e = (redisLibevEvents*)privdata;\n    struct ev_loop *loop = e->loop;\n    ((void)loop);\n    if (e->writing) {\n        e->writing = 0;\n        ev_io_stop(EV_A_ &e->wev);\n    }\n}\n\nstatic void redisLibevStopTimer(void *privdata) {\n    redisLibevEvents *e = (redisLibevEvents*)privdata;\n    struct ev_loop *loop = e->loop;\n    ((void)loop);\n    ev_timer_stop(EV_A_ &e->timer);\n}\n\nstatic void redisLibevCleanup(void *privdata) {\n    redisLibevEvents *e = (redisLibevEvents*)privdata;\n    redisLibevDelRead(privdata);\n    redisLibevDelWrite(privdata);\n    redisLibevStopTimer(privdata);\n    hi_free(e);\n}\n\nstatic void redisLibevTimeout(EV_P_ ev_timer *timer, int revents) {\n    ((void)revents);\n    redisLibevEvents *e = (redisLibevEvents*)timer->data;\n    redisAsyncHandleTimeout(e->context);\n}\n\nstatic void redisLibevSetTimeout(void *privdata, struct timeval tv) {\n    redisLibevEvents *e = (redisLibevEvents*)privdata;\n    struct ev_loop *loop = e->loop;\n    ((void)loop);\n\n    if (!ev_is_active(&e->timer)) {\n        ev_init(&e->timer, redisLibevTimeout);\n        e->timer.data = e;\n    }\n\n    e->timer.repeat = tv.tv_sec + tv.tv_usec / 1000000.00;\n    ev_timer_again(EV_A_ &e->timer);\n}\n\nstatic int redisLibevAttach(EV_P_ redisAsyncContext *ac) {\n    redisContext *c = &(ac->c);\n    redisLibevEvents *e;\n\n    /* Nothing should be attached when something is already attached */\n    if (ac->ev.data != NULL)\n        return REDIS_ERR;\n\n    /* Create container for context and r/w events */\n    e = (redisLibevEvents*)hi_calloc(1, sizeof(*e));\n    if (e == NULL)\n        return REDIS_ERR;\n\n    e->context = ac;\n#if EV_MULTIPLICITY\n    e->loop = loop;\n#else\n    e->loop = NULL;\n#endif\n    e->rev.data = e;\n    e->wev.data = e;\n\n    /* Register functions to start/stop listening for events */\n    ac->ev.addRead = redisLibevAddRead;\n    ac->ev.delRead = redisLibevDelRead;\n    ac->ev.addWrite = redisLibevAddWrite;\n    ac->ev.delWrite = redisLibevDelWrite;\n    ac->ev.cleanup = redisLibevCleanup;\n    ac->ev.scheduleTimer = redisLibevSetTimeout;\n    ac->ev.data = e;\n\n    /* Initialize read/write events */\n    ev_io_init(&e->rev,redisLibevReadEvent,c->fd,EV_READ);\n    ev_io_init(&e->wev,redisLibevWriteEvent,c->fd,EV_WRITE);\n    return REDIS_OK;\n}\n\n#endif\n"
  },
  {
    "path": "deps/hiredis/adapters/libevent.h",
    "content": "/*\n * Copyright (c) 2010-2011, Pieter Noordhuis <pcnoordhuis at gmail dot com>\n *\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __HIREDIS_LIBEVENT_H__\n#define __HIREDIS_LIBEVENT_H__\n#include <event2/event.h>\n#include \"../hiredis.h\"\n#include \"../async.h\"\n\n#define REDIS_LIBEVENT_DELETED 0x01\n#define REDIS_LIBEVENT_ENTERED 0x02\n\ntypedef struct redisLibeventEvents {\n    redisAsyncContext *context;\n    struct event *ev;\n    struct event_base *base;\n    struct timeval tv;\n    short flags;\n    short state;\n} redisLibeventEvents;\n\nstatic void redisLibeventDestroy(redisLibeventEvents *e) {\n    hi_free(e);\n}\n\nstatic void redisLibeventHandler(int fd, short event, void *arg) {\n    ((void)fd);\n    redisLibeventEvents *e = (redisLibeventEvents*)arg;\n    e->state |= REDIS_LIBEVENT_ENTERED;\n\n    #define CHECK_DELETED() if (e->state & REDIS_LIBEVENT_DELETED) {\\\n        redisLibeventDestroy(e);\\\n        return; \\\n    }\n\n    if ((event & EV_TIMEOUT) && (e->state & REDIS_LIBEVENT_DELETED) == 0) {\n        redisAsyncHandleTimeout(e->context);\n        CHECK_DELETED();\n    }\n\n    if ((event & EV_READ) && e->context && (e->state & REDIS_LIBEVENT_DELETED) == 0) {\n        redisAsyncHandleRead(e->context);\n        CHECK_DELETED();\n    }\n\n    if ((event & EV_WRITE) && e->context && (e->state & REDIS_LIBEVENT_DELETED) == 0) {\n        redisAsyncHandleWrite(e->context);\n        CHECK_DELETED();\n    }\n\n    e->state &= ~REDIS_LIBEVENT_ENTERED;\n    #undef CHECK_DELETED\n}\n\nstatic void redisLibeventUpdate(void *privdata, short flag, int isRemove) {\n    redisLibeventEvents *e = (redisLibeventEvents *)privdata;\n    const struct timeval *tv = e->tv.tv_sec || e->tv.tv_usec ? &e->tv : NULL;\n\n    if (isRemove) {\n        if ((e->flags & flag) == 0) {\n            return;\n        } else {\n            e->flags &= ~flag;\n        }\n    } else {\n        if (e->flags & flag) {\n            return;\n        } else {\n            e->flags |= flag;\n        }\n    }\n\n    event_del(e->ev);\n    event_assign(e->ev, e->base, e->context->c.fd, e->flags | EV_PERSIST,\n                 redisLibeventHandler, privdata);\n    event_add(e->ev, tv);\n}\n\nstatic void redisLibeventAddRead(void *privdata) {\n    redisLibeventUpdate(privdata, EV_READ, 0);\n}\n\nstatic void redisLibeventDelRead(void *privdata) {\n    redisLibeventUpdate(privdata, EV_READ, 1);\n}\n\nstatic void redisLibeventAddWrite(void *privdata) {\n    redisLibeventUpdate(privdata, EV_WRITE, 0);\n}\n\nstatic void redisLibeventDelWrite(void *privdata) {\n    redisLibeventUpdate(privdata, EV_WRITE, 1);\n}\n\nstatic void redisLibeventCleanup(void *privdata) {\n    redisLibeventEvents *e = (redisLibeventEvents*)privdata;\n    if (!e) {\n        return;\n    }\n    event_del(e->ev);\n    event_free(e->ev);\n    e->ev = NULL;\n\n    if (e->state & REDIS_LIBEVENT_ENTERED) {\n        e->state |= REDIS_LIBEVENT_DELETED;\n    } else {\n        redisLibeventDestroy(e);\n    }\n}\n\nstatic void redisLibeventSetTimeout(void *privdata, struct timeval tv) {\n    redisLibeventEvents *e = (redisLibeventEvents *)privdata;\n    short flags = e->flags;\n    e->flags = 0;\n    e->tv = tv;\n    redisLibeventUpdate(e, flags, 0);\n}\n\nstatic int redisLibeventAttach(redisAsyncContext *ac, struct event_base *base) {\n    redisContext *c = &(ac->c);\n    redisLibeventEvents *e;\n\n    /* Nothing should be attached when something is already attached */\n    if (ac->ev.data != NULL)\n        return REDIS_ERR;\n\n    /* Create container for context and r/w events */\n    e = (redisLibeventEvents*)hi_calloc(1, sizeof(*e));\n    if (e == NULL)\n        return REDIS_ERR;\n\n    e->context = ac;\n\n    /* Register functions to start/stop listening for events */\n    ac->ev.addRead = redisLibeventAddRead;\n    ac->ev.delRead = redisLibeventDelRead;\n    ac->ev.addWrite = redisLibeventAddWrite;\n    ac->ev.delWrite = redisLibeventDelWrite;\n    ac->ev.cleanup = redisLibeventCleanup;\n    ac->ev.scheduleTimer = redisLibeventSetTimeout;\n    ac->ev.data = e;\n\n    /* Initialize and install read/write events */\n    e->ev = event_new(base, c->fd, EV_READ | EV_WRITE, redisLibeventHandler, e);\n    e->base = base;\n    return REDIS_OK;\n}\n#endif\n"
  },
  {
    "path": "deps/hiredis/adapters/libuv.h",
    "content": "#ifndef __HIREDIS_LIBUV_H__\n#define __HIREDIS_LIBUV_H__\n#include <stdlib.h>\n#include <uv.h>\n#include \"../hiredis.h\"\n#include \"../async.h\"\n#include <string.h>\n\ntypedef struct redisLibuvEvents {\n  redisAsyncContext* context;\n  uv_poll_t          handle;\n  int                events;\n} redisLibuvEvents;\n\n\nstatic void redisLibuvPoll(uv_poll_t* handle, int status, int events) {\n  redisLibuvEvents* p = (redisLibuvEvents*)handle->data;\n  int ev = (status ? p->events : events);\n\n  if (p->context != NULL && (ev & UV_READABLE)) {\n    redisAsyncHandleRead(p->context);\n  }\n  if (p->context != NULL && (ev & UV_WRITABLE)) {\n    redisAsyncHandleWrite(p->context);\n  }\n}\n\n\nstatic void redisLibuvAddRead(void *privdata) {\n  redisLibuvEvents* p = (redisLibuvEvents*)privdata;\n\n  p->events |= UV_READABLE;\n\n  uv_poll_start(&p->handle, p->events, redisLibuvPoll);\n}\n\n\nstatic void redisLibuvDelRead(void *privdata) {\n  redisLibuvEvents* p = (redisLibuvEvents*)privdata;\n\n  p->events &= ~UV_READABLE;\n\n  if (p->events) {\n    uv_poll_start(&p->handle, p->events, redisLibuvPoll);\n  } else {\n    uv_poll_stop(&p->handle);\n  }\n}\n\n\nstatic void redisLibuvAddWrite(void *privdata) {\n  redisLibuvEvents* p = (redisLibuvEvents*)privdata;\n\n  p->events |= UV_WRITABLE;\n\n  uv_poll_start(&p->handle, p->events, redisLibuvPoll);\n}\n\n\nstatic void redisLibuvDelWrite(void *privdata) {\n  redisLibuvEvents* p = (redisLibuvEvents*)privdata;\n\n  p->events &= ~UV_WRITABLE;\n\n  if (p->events) {\n    uv_poll_start(&p->handle, p->events, redisLibuvPoll);\n  } else {\n    uv_poll_stop(&p->handle);\n  }\n}\n\n\nstatic void on_close(uv_handle_t* handle) {\n  redisLibuvEvents* p = (redisLibuvEvents*)handle->data;\n\n  hi_free(p);\n}\n\n\nstatic void redisLibuvCleanup(void *privdata) {\n  redisLibuvEvents* p = (redisLibuvEvents*)privdata;\n\n  p->context = NULL; // indicate that context might no longer exist\n  uv_close((uv_handle_t*)&p->handle, on_close);\n}\n\n\nstatic int redisLibuvAttach(redisAsyncContext* ac, uv_loop_t* loop) {\n  redisContext *c = &(ac->c);\n\n  if (ac->ev.data != NULL) {\n    return REDIS_ERR;\n  }\n\n  ac->ev.addRead  = redisLibuvAddRead;\n  ac->ev.delRead  = redisLibuvDelRead;\n  ac->ev.addWrite = redisLibuvAddWrite;\n  ac->ev.delWrite = redisLibuvDelWrite;\n  ac->ev.cleanup  = redisLibuvCleanup;\n\n  redisLibuvEvents* p = (redisLibuvEvents*)hi_malloc(sizeof(*p));\n  if (p == NULL)\n      return REDIS_ERR;\n\n  memset(p, 0, sizeof(*p));\n\n  if (uv_poll_init_socket(loop, &p->handle, c->fd) != 0) {\n    return REDIS_ERR;\n  }\n\n  ac->ev.data    = p;\n  p->handle.data = p;\n  p->context     = ac;\n\n  return REDIS_OK;\n}\n#endif\n"
  },
  {
    "path": "deps/hiredis/adapters/macosx.h",
    "content": "//\n//  Created by Дмитрий Бахвалов on 13.07.15.\n//  Copyright (c) 2015 Dmitry Bakhvalov. All rights reserved.\n//\n\n#ifndef __HIREDIS_MACOSX_H__\n#define __HIREDIS_MACOSX_H__\n\n#include <CoreFoundation/CoreFoundation.h>\n\n#include \"../hiredis.h\"\n#include \"../async.h\"\n\ntypedef struct {\n    redisAsyncContext *context;\n    CFSocketRef socketRef;\n    CFRunLoopSourceRef sourceRef;\n} RedisRunLoop;\n\nstatic int freeRedisRunLoop(RedisRunLoop* redisRunLoop) {\n    if( redisRunLoop != NULL ) {\n        if( redisRunLoop->sourceRef != NULL ) {\n            CFRunLoopSourceInvalidate(redisRunLoop->sourceRef);\n            CFRelease(redisRunLoop->sourceRef);\n        }\n        if( redisRunLoop->socketRef != NULL ) {\n            CFSocketInvalidate(redisRunLoop->socketRef);\n            CFRelease(redisRunLoop->socketRef);\n        }\n        hi_free(redisRunLoop);\n    }\n    return REDIS_ERR;\n}\n\nstatic void redisMacOSAddRead(void *privdata) {\n    RedisRunLoop *redisRunLoop = (RedisRunLoop*)privdata;\n    CFSocketEnableCallBacks(redisRunLoop->socketRef, kCFSocketReadCallBack);\n}\n\nstatic void redisMacOSDelRead(void *privdata) {\n    RedisRunLoop *redisRunLoop = (RedisRunLoop*)privdata;\n    CFSocketDisableCallBacks(redisRunLoop->socketRef, kCFSocketReadCallBack);\n}\n\nstatic void redisMacOSAddWrite(void *privdata) {\n    RedisRunLoop *redisRunLoop = (RedisRunLoop*)privdata;\n    CFSocketEnableCallBacks(redisRunLoop->socketRef, kCFSocketWriteCallBack);\n}\n\nstatic void redisMacOSDelWrite(void *privdata) {\n    RedisRunLoop *redisRunLoop = (RedisRunLoop*)privdata;\n    CFSocketDisableCallBacks(redisRunLoop->socketRef, kCFSocketWriteCallBack);\n}\n\nstatic void redisMacOSCleanup(void *privdata) {\n    RedisRunLoop *redisRunLoop = (RedisRunLoop*)privdata;\n    freeRedisRunLoop(redisRunLoop);\n}\n\nstatic void redisMacOSAsyncCallback(CFSocketRef __unused s, CFSocketCallBackType callbackType, CFDataRef __unused address, const void __unused *data, void *info) {\n    redisAsyncContext* context = (redisAsyncContext*) info;\n\n    switch (callbackType) {\n        case kCFSocketReadCallBack:\n            redisAsyncHandleRead(context);\n            break;\n\n        case kCFSocketWriteCallBack:\n            redisAsyncHandleWrite(context);\n            break;\n\n        default:\n            break;\n    }\n}\n\nstatic int redisMacOSAttach(redisAsyncContext *redisAsyncCtx, CFRunLoopRef runLoop) {\n    redisContext *redisCtx = &(redisAsyncCtx->c);\n\n    /* Nothing should be attached when something is already attached */\n    if( redisAsyncCtx->ev.data != NULL ) return REDIS_ERR;\n\n    RedisRunLoop* redisRunLoop = (RedisRunLoop*) hi_calloc(1, sizeof(RedisRunLoop));\n    if (redisRunLoop == NULL)\n        return REDIS_ERR;\n\n    /* Setup redis stuff */\n    redisRunLoop->context = redisAsyncCtx;\n\n    redisAsyncCtx->ev.addRead  = redisMacOSAddRead;\n    redisAsyncCtx->ev.delRead  = redisMacOSDelRead;\n    redisAsyncCtx->ev.addWrite = redisMacOSAddWrite;\n    redisAsyncCtx->ev.delWrite = redisMacOSDelWrite;\n    redisAsyncCtx->ev.cleanup  = redisMacOSCleanup;\n    redisAsyncCtx->ev.data     = redisRunLoop;\n\n    /* Initialize and install read/write events */\n    CFSocketContext socketCtx = { 0, redisAsyncCtx, NULL, NULL, NULL };\n\n    redisRunLoop->socketRef = CFSocketCreateWithNative(NULL, redisCtx->fd,\n                                                       kCFSocketReadCallBack | kCFSocketWriteCallBack,\n                                                       redisMacOSAsyncCallback,\n                                                       &socketCtx);\n    if( !redisRunLoop->socketRef ) return freeRedisRunLoop(redisRunLoop);\n\n    redisRunLoop->sourceRef = CFSocketCreateRunLoopSource(NULL, redisRunLoop->socketRef, 0);\n    if( !redisRunLoop->sourceRef ) return freeRedisRunLoop(redisRunLoop);\n\n    CFRunLoopAddSource(runLoop, redisRunLoop->sourceRef, kCFRunLoopDefaultMode);\n\n    return REDIS_OK;\n}\n\n#endif\n\n"
  },
  {
    "path": "deps/hiredis/adapters/qt.h",
    "content": "/*-\n * Copyright (C) 2014 Pietro Cerutti <gahr@gahr.ch>\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions\n * are met:\n * 1. Redistributions of source code must retain the above copyright\n *    notice, this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright\n *    notice, this list of conditions and the following disclaimer in the\n *    documentation and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND\n * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE\n * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\n * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\n * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\n * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\n * SUCH DAMAGE.\n */\n\n#ifndef __HIREDIS_QT_H__\n#define __HIREDIS_QT_H__\n#include <QSocketNotifier>\n#include \"../async.h\"\n\nstatic void RedisQtAddRead(void *);\nstatic void RedisQtDelRead(void *);\nstatic void RedisQtAddWrite(void *);\nstatic void RedisQtDelWrite(void *);\nstatic void RedisQtCleanup(void *);\n\nclass RedisQtAdapter : public QObject {\n\n    Q_OBJECT\n\n    friend\n    void RedisQtAddRead(void * adapter) {\n        RedisQtAdapter * a = static_cast<RedisQtAdapter *>(adapter);\n        a->addRead();\n    }\n\n    friend\n    void RedisQtDelRead(void * adapter) {\n        RedisQtAdapter * a = static_cast<RedisQtAdapter *>(adapter);\n        a->delRead();\n    }\n\n    friend\n    void RedisQtAddWrite(void * adapter) {\n        RedisQtAdapter * a = static_cast<RedisQtAdapter *>(adapter);\n        a->addWrite();\n    }\n\n    friend\n    void RedisQtDelWrite(void * adapter) {\n        RedisQtAdapter * a = static_cast<RedisQtAdapter *>(adapter);\n        a->delWrite();\n    }\n\n    friend\n    void RedisQtCleanup(void * adapter) {\n        RedisQtAdapter * a = static_cast<RedisQtAdapter *>(adapter);\n        a->cleanup();\n    }\n\n    public:\n        RedisQtAdapter(QObject * parent = 0)\n            : QObject(parent), m_ctx(0), m_read(0), m_write(0) { }\n\n        ~RedisQtAdapter() {\n            if (m_ctx != 0) {\n                m_ctx->ev.data = NULL;\n            }\n        }\n\n        int setContext(redisAsyncContext * ac) {\n            if (ac->ev.data != NULL) {\n                return REDIS_ERR;\n            }\n            m_ctx = ac;\n            m_ctx->ev.data = this;\n            m_ctx->ev.addRead = RedisQtAddRead;\n            m_ctx->ev.delRead = RedisQtDelRead;\n            m_ctx->ev.addWrite = RedisQtAddWrite;\n            m_ctx->ev.delWrite = RedisQtDelWrite;\n            m_ctx->ev.cleanup = RedisQtCleanup;\n            return REDIS_OK;\n        }\n\n    private:\n        void addRead() {\n            if (m_read) return;\n            m_read = new QSocketNotifier(m_ctx->c.fd, QSocketNotifier::Read, 0);\n            connect(m_read, SIGNAL(activated(int)), this, SLOT(read()));\n        }\n\n        void delRead() {\n            if (!m_read) return;\n            delete m_read;\n            m_read = 0;\n        }\n\n        void addWrite() {\n            if (m_write) return;\n            m_write = new QSocketNotifier(m_ctx->c.fd, QSocketNotifier::Write, 0);\n            connect(m_write, SIGNAL(activated(int)), this, SLOT(write()));\n        }\n\n        void delWrite() {\n            if (!m_write) return;\n            delete m_write;\n            m_write = 0;\n        }\n\n        void cleanup() {\n            delRead();\n            delWrite();\n        }\n\n    private slots:\n        void read() { redisAsyncHandleRead(m_ctx); }\n        void write() { redisAsyncHandleWrite(m_ctx); }\n\n    private:\n        redisAsyncContext * m_ctx;\n        QSocketNotifier * m_read;\n        QSocketNotifier * m_write;\n};\n\n#endif /* !__HIREDIS_QT_H__ */\n"
  },
  {
    "path": "deps/hiredis/alloc.c",
    "content": "/*\n * Copyright (c) 2020, Michael Grunder <michael dot grunder at gmail dot com>\n *\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"fmacros.h\"\n#include \"alloc.h\"\n#include <string.h>\n#include <stdlib.h>\n\nhiredisAllocFuncs hiredisAllocFns = {\n    .mallocFn = malloc,\n    .callocFn = calloc,\n    .reallocFn = realloc,\n    .strdupFn = strdup,\n    .freeFn = free,\n};\n\n/* Override hiredis' allocators with ones supplied by the user */\nhiredisAllocFuncs hiredisSetAllocators(hiredisAllocFuncs *override) {\n    hiredisAllocFuncs orig = hiredisAllocFns;\n\n    hiredisAllocFns = *override;\n\n    return orig;\n}\n\n/* Reset allocators to use libc defaults */\nvoid hiredisResetAllocators(void) {\n    hiredisAllocFns = (hiredisAllocFuncs) {\n        .mallocFn = malloc,\n        .callocFn = calloc,\n        .reallocFn = realloc,\n        .strdupFn = strdup,\n        .freeFn = free,\n    };\n}\n\n#ifdef _WIN32\n\nvoid *hi_malloc(size_t size) {\n    return hiredisAllocFns.mallocFn(size);\n}\n\nvoid *hi_calloc(size_t nmemb, size_t size) {\n    return hiredisAllocFns.callocFn(nmemb, size);\n}\n\nvoid *hi_realloc(void *ptr, size_t size) {\n    return hiredisAllocFns.reallocFn(ptr, size);\n}\n\nchar *hi_strdup(const char *str) {\n    return hiredisAllocFns.strdupFn(str);\n}\n\nvoid hi_free(void *ptr) {\n    hiredisAllocFns.freeFn(ptr);\n}\n\n#endif\n"
  },
  {
    "path": "deps/hiredis/alloc.h",
    "content": "/*\n * Copyright (c) 2020, Michael Grunder <michael dot grunder at gmail dot com>\n *\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef HIREDIS_ALLOC_H\n#define HIREDIS_ALLOC_H\n\n#include <stddef.h> /* for size_t */\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/* Structure pointing to our actually configured allocators */\ntypedef struct hiredisAllocFuncs {\n    void *(*mallocFn)(size_t);\n    void *(*callocFn)(size_t,size_t);\n    void *(*reallocFn)(void*,size_t);\n    char *(*strdupFn)(const char*);\n    void (*freeFn)(void*);\n} hiredisAllocFuncs;\n\nhiredisAllocFuncs hiredisSetAllocators(hiredisAllocFuncs *ha);\nvoid hiredisResetAllocators(void);\n\n#ifndef _WIN32\n\n/* Hiredis' configured allocator function pointer struct */\nextern hiredisAllocFuncs hiredisAllocFns;\n\nstatic inline void *hi_malloc(size_t size) {\n    return hiredisAllocFns.mallocFn(size);\n}\n\nstatic inline void *hi_calloc(size_t nmemb, size_t size) {\n    return hiredisAllocFns.callocFn(nmemb, size);\n}\n\nstatic inline void *hi_realloc(void *ptr, size_t size) {\n    return hiredisAllocFns.reallocFn(ptr, size);\n}\n\nstatic inline char *hi_strdup(const char *str) {\n    return hiredisAllocFns.strdupFn(str);\n}\n\nstatic inline void hi_free(void *ptr) {\n    hiredisAllocFns.freeFn(ptr);\n}\n\n#else\n\nvoid *hi_malloc(size_t size);\nvoid *hi_calloc(size_t nmemb, size_t size);\nvoid *hi_realloc(void *ptr, size_t size);\nchar *hi_strdup(const char *str);\nvoid hi_free(void *ptr);\n\n#endif\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif /* HIREDIS_ALLOC_H */\n"
  },
  {
    "path": "deps/hiredis/appveyor.yml",
    "content": "# Appveyor configuration file for CI build of hiredis on Windows (under Cygwin)\nenvironment:\n    matrix:\n        - CYG_BASH: C:\\cygwin64\\bin\\bash\n          CC: gcc\n        - CYG_BASH: C:\\cygwin\\bin\\bash\n          CC: gcc\n          CFLAGS: -m32\n          CXXFLAGS: -m32\n          LDFLAGS: -m32\n\nclone_depth: 1\n\n# Attempt to ensure we don't try to convert line endings to Win32 CRLF as this will cause build to fail\ninit:\n    - git config --global core.autocrlf input\n\n# Install needed build dependencies\ninstall:\n    - '%CYG_BASH% -lc \"cygcheck -dc cygwin\"'\n\nbuild_script:\n    - 'echo building...'\n    - '%CYG_BASH% -lc \"cd $APPVEYOR_BUILD_FOLDER; exec 0</dev/null; mkdir build && cd build && cmake .. -G \\\"Unix Makefiles\\\" && make VERBOSE=1\"'\n"
  },
  {
    "path": "deps/hiredis/async.c",
    "content": "/*\n * Copyright (c) 2009-2011, Salvatore Sanfilippo <antirez at gmail dot com>\n * Copyright (c) 2010-2011, Pieter Noordhuis <pcnoordhuis at gmail dot com>\n *\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"fmacros.h\"\n#include \"alloc.h\"\n#include <stdlib.h>\n#include <string.h>\n#ifndef _MSC_VER\n#include <strings.h>\n#endif\n#include <assert.h>\n#include <ctype.h>\n#include <errno.h>\n#include \"async.h\"\n#include \"net.h\"\n#include \"dict.c\"\n#include \"sds.h\"\n#include \"win32.h\"\n\n#include \"async_private.h\"\n\n/* Forward declarations of hiredis.c functions */\nint __redisAppendCommand(redisContext *c, const char *cmd, size_t len);\nvoid __redisSetError(redisContext *c, int type, const char *str);\n\n/* Functions managing dictionary of callbacks for pub/sub. */\nstatic unsigned int callbackHash(const void *key) {\n    return dictGenHashFunction((const unsigned char *)key,\n                               hi_sdslen((const hisds)key));\n}\n\nstatic void *callbackValDup(void *privdata, const void *src) {\n    ((void) privdata);\n    redisCallback *dup;\n\n    dup = hi_malloc(sizeof(*dup));\n    if (dup == NULL)\n        return NULL;\n\n    memcpy(dup,src,sizeof(*dup));\n    return dup;\n}\n\nstatic int callbackKeyCompare(void *privdata, const void *key1, const void *key2) {\n    int l1, l2;\n    ((void) privdata);\n\n    l1 = hi_sdslen((const hisds)key1);\n    l2 = hi_sdslen((const hisds)key2);\n    if (l1 != l2) return 0;\n    return memcmp(key1,key2,l1) == 0;\n}\n\nstatic void callbackKeyDestructor(void *privdata, void *key) {\n    ((void) privdata);\n    hi_sdsfree((hisds)key);\n}\n\nstatic void callbackValDestructor(void *privdata, void *val) {\n    ((void) privdata);\n    hi_free(val);\n}\n\nstatic dictType callbackDict = {\n    callbackHash,\n    NULL,\n    callbackValDup,\n    callbackKeyCompare,\n    callbackKeyDestructor,\n    callbackValDestructor\n};\n\nstatic redisAsyncContext *redisAsyncInitialize(redisContext *c) {\n    redisAsyncContext *ac;\n    dict *channels = NULL, *patterns = NULL;\n\n    channels = dictCreate(&callbackDict,NULL);\n    if (channels == NULL)\n        goto oom;\n\n    patterns = dictCreate(&callbackDict,NULL);\n    if (patterns == NULL)\n        goto oom;\n\n    ac = hi_realloc(c,sizeof(redisAsyncContext));\n    if (ac == NULL)\n        goto oom;\n\n    c = &(ac->c);\n\n    /* The regular connect functions will always set the flag REDIS_CONNECTED.\n     * For the async API, we want to wait until the first write event is\n     * received up before setting this flag, so reset it here. */\n    c->flags &= ~REDIS_CONNECTED;\n\n    ac->err = 0;\n    ac->errstr = NULL;\n    ac->data = NULL;\n    ac->dataCleanup = NULL;\n\n    ac->ev.data = NULL;\n    ac->ev.addRead = NULL;\n    ac->ev.delRead = NULL;\n    ac->ev.addWrite = NULL;\n    ac->ev.delWrite = NULL;\n    ac->ev.cleanup = NULL;\n    ac->ev.scheduleTimer = NULL;\n\n    ac->onConnect = NULL;\n    ac->onDisconnect = NULL;\n\n    ac->replies.head = NULL;\n    ac->replies.tail = NULL;\n    ac->sub.invalid.head = NULL;\n    ac->sub.invalid.tail = NULL;\n    ac->sub.channels = channels;\n    ac->sub.patterns = patterns;\n\n    return ac;\noom:\n    if (channels) dictRelease(channels);\n    if (patterns) dictRelease(patterns);\n    return NULL;\n}\n\n/* We want the error field to be accessible directly instead of requiring\n * an indirection to the redisContext struct. */\nstatic void __redisAsyncCopyError(redisAsyncContext *ac) {\n    if (!ac)\n        return;\n\n    redisContext *c = &(ac->c);\n    ac->err = c->err;\n    ac->errstr = c->errstr;\n}\n\nredisAsyncContext *redisAsyncConnectWithOptions(const redisOptions *options) {\n    redisOptions myOptions = *options;\n    redisContext *c;\n    redisAsyncContext *ac;\n\n    /* Clear any erroneously set sync callback and flag that we don't want to\n     * use freeReplyObject by default. */\n    myOptions.push_cb = NULL;\n    myOptions.options |= REDIS_OPT_NO_PUSH_AUTOFREE;\n\n    myOptions.options |= REDIS_OPT_NONBLOCK;\n    c = redisConnectWithOptions(&myOptions);\n    if (c == NULL) {\n        return NULL;\n    }\n\n    ac = redisAsyncInitialize(c);\n    if (ac == NULL) {\n        redisFree(c);\n        return NULL;\n    }\n\n    /* Set any configured async push handler */\n    redisAsyncSetPushCallback(ac, myOptions.async_push_cb);\n\n    __redisAsyncCopyError(ac);\n    return ac;\n}\n\nredisAsyncContext *redisAsyncConnect(const char *ip, int port) {\n    redisOptions options = {0};\n    REDIS_OPTIONS_SET_TCP(&options, ip, port);\n    return redisAsyncConnectWithOptions(&options);\n}\n\nredisAsyncContext *redisAsyncConnectBind(const char *ip, int port,\n                                         const char *source_addr) {\n    redisOptions options = {0};\n    REDIS_OPTIONS_SET_TCP(&options, ip, port);\n    options.endpoint.tcp.source_addr = source_addr;\n    return redisAsyncConnectWithOptions(&options);\n}\n\nredisAsyncContext *redisAsyncConnectBindWithReuse(const char *ip, int port,\n                                                  const char *source_addr) {\n    redisOptions options = {0};\n    REDIS_OPTIONS_SET_TCP(&options, ip, port);\n    options.options |= REDIS_OPT_REUSEADDR;\n    options.endpoint.tcp.source_addr = source_addr;\n    return redisAsyncConnectWithOptions(&options);\n}\n\nredisAsyncContext *redisAsyncConnectUnix(const char *path) {\n    redisOptions options = {0};\n    REDIS_OPTIONS_SET_UNIX(&options, path);\n    return redisAsyncConnectWithOptions(&options);\n}\n\nint redisAsyncSetConnectCallback(redisAsyncContext *ac, redisConnectCallback *fn) {\n    if (ac->onConnect == NULL) {\n        ac->onConnect = fn;\n\n        /* The common way to detect an established connection is to wait for\n         * the first write event to be fired. This assumes the related event\n         * library functions are already set. */\n        _EL_ADD_WRITE(ac);\n        return REDIS_OK;\n    }\n    return REDIS_ERR;\n}\n\nint redisAsyncSetDisconnectCallback(redisAsyncContext *ac, redisDisconnectCallback *fn) {\n    if (ac->onDisconnect == NULL) {\n        ac->onDisconnect = fn;\n        return REDIS_OK;\n    }\n    return REDIS_ERR;\n}\n\n/* Helper functions to push/shift callbacks */\nstatic int __redisPushCallback(redisCallbackList *list, redisCallback *source) {\n    redisCallback *cb;\n\n    /* Copy callback from stack to heap */\n    cb = hi_malloc(sizeof(*cb));\n    if (cb == NULL)\n        return REDIS_ERR_OOM;\n\n    if (source != NULL) {\n        memcpy(cb,source,sizeof(*cb));\n        cb->next = NULL;\n    }\n\n    /* Store callback in list */\n    if (list->head == NULL)\n        list->head = cb;\n    if (list->tail != NULL)\n        list->tail->next = cb;\n    list->tail = cb;\n    return REDIS_OK;\n}\n\nstatic int __redisShiftCallback(redisCallbackList *list, redisCallback *target) {\n    redisCallback *cb = list->head;\n    if (cb != NULL) {\n        list->head = cb->next;\n        if (cb == list->tail)\n            list->tail = NULL;\n\n        /* Copy callback from heap to stack */\n        if (target != NULL)\n            memcpy(target,cb,sizeof(*cb));\n        hi_free(cb);\n        return REDIS_OK;\n    }\n    return REDIS_ERR;\n}\n\nstatic void __redisRunCallback(redisAsyncContext *ac, redisCallback *cb, redisReply *reply) {\n    redisContext *c = &(ac->c);\n    if (cb->fn != NULL) {\n        c->flags |= REDIS_IN_CALLBACK;\n        cb->fn(ac,reply,cb->privdata);\n        c->flags &= ~REDIS_IN_CALLBACK;\n    }\n}\n\nstatic void __redisRunPushCallback(redisAsyncContext *ac, redisReply *reply) {\n    if (ac->push_cb != NULL) {\n        ac->c.flags |= REDIS_IN_CALLBACK;\n        ac->push_cb(ac, reply);\n        ac->c.flags &= ~REDIS_IN_CALLBACK;\n    }\n}\n\n/* Helper function to free the context. */\nstatic void __redisAsyncFree(redisAsyncContext *ac) {\n    redisContext *c = &(ac->c);\n    redisCallback cb;\n    dictIterator *it;\n    dictEntry *de;\n\n    /* Execute pending callbacks with NULL reply. */\n    while (__redisShiftCallback(&ac->replies,&cb) == REDIS_OK)\n        __redisRunCallback(ac,&cb,NULL);\n\n    /* Execute callbacks for invalid commands */\n    while (__redisShiftCallback(&ac->sub.invalid,&cb) == REDIS_OK)\n        __redisRunCallback(ac,&cb,NULL);\n\n    /* Run subscription callbacks with NULL reply */\n    if (ac->sub.channels) {\n        it = dictGetIterator(ac->sub.channels);\n        if (it != NULL) {\n            while ((de = dictNext(it)) != NULL)\n                __redisRunCallback(ac,dictGetEntryVal(de),NULL);\n            dictReleaseIterator(it);\n        }\n\n        dictRelease(ac->sub.channels);\n    }\n\n    if (ac->sub.patterns) {\n        it = dictGetIterator(ac->sub.patterns);\n        if (it != NULL) {\n            while ((de = dictNext(it)) != NULL)\n                __redisRunCallback(ac,dictGetEntryVal(de),NULL);\n            dictReleaseIterator(it);\n        }\n\n        dictRelease(ac->sub.patterns);\n    }\n\n    /* Signal event lib to clean up */\n    _EL_CLEANUP(ac);\n\n    /* Execute disconnect callback. When redisAsyncFree() initiated destroying\n     * this context, the status will always be REDIS_OK. */\n    if (ac->onDisconnect && (c->flags & REDIS_CONNECTED)) {\n        if (c->flags & REDIS_FREEING) {\n            ac->onDisconnect(ac,REDIS_OK);\n        } else {\n            ac->onDisconnect(ac,(ac->err == 0) ? REDIS_OK : REDIS_ERR);\n        }\n    }\n\n    if (ac->dataCleanup) {\n        ac->dataCleanup(ac->data);\n    }\n\n    /* Cleanup self */\n    redisFree(c);\n}\n\n/* Free the async context. When this function is called from a callback,\n * control needs to be returned to redisProcessCallbacks() before actual\n * free'ing. To do so, a flag is set on the context which is picked up by\n * redisProcessCallbacks(). Otherwise, the context is immediately free'd. */\nvoid redisAsyncFree(redisAsyncContext *ac) {\n    redisContext *c = &(ac->c);\n    c->flags |= REDIS_FREEING;\n    if (!(c->flags & REDIS_IN_CALLBACK))\n        __redisAsyncFree(ac);\n}\n\n/* Helper function to make the disconnect happen and clean up. */\nvoid __redisAsyncDisconnect(redisAsyncContext *ac) {\n    redisContext *c = &(ac->c);\n\n    /* Make sure error is accessible if there is any */\n    __redisAsyncCopyError(ac);\n\n    if (ac->err == 0) {\n        /* For clean disconnects, there should be no pending callbacks. */\n        int ret = __redisShiftCallback(&ac->replies,NULL);\n        assert(ret == REDIS_ERR);\n    } else {\n        /* Disconnection is caused by an error, make sure that pending\n         * callbacks cannot call new commands. */\n        c->flags |= REDIS_DISCONNECTING;\n    }\n\n    /* cleanup event library on disconnect.\n     * this is safe to call multiple times */\n    _EL_CLEANUP(ac);\n\n    /* For non-clean disconnects, __redisAsyncFree() will execute pending\n     * callbacks with a NULL-reply. */\n    if (!(c->flags & REDIS_NO_AUTO_FREE)) {\n      __redisAsyncFree(ac);\n    }\n}\n\n/* Tries to do a clean disconnect from Redis, meaning it stops new commands\n * from being issued, but tries to flush the output buffer and execute\n * callbacks for all remaining replies. When this function is called from a\n * callback, there might be more replies and we can safely defer disconnecting\n * to redisProcessCallbacks(). Otherwise, we can only disconnect immediately\n * when there are no pending callbacks. */\nvoid redisAsyncDisconnect(redisAsyncContext *ac) {\n    redisContext *c = &(ac->c);\n    c->flags |= REDIS_DISCONNECTING;\n\n    /** unset the auto-free flag here, because disconnect undoes this */\n    c->flags &= ~REDIS_NO_AUTO_FREE;\n    if (!(c->flags & REDIS_IN_CALLBACK) && ac->replies.head == NULL)\n        __redisAsyncDisconnect(ac);\n}\n\nstatic int __redisGetSubscribeCallback(redisAsyncContext *ac, redisReply *reply, redisCallback *dstcb) {\n    redisContext *c = &(ac->c);\n    dict *callbacks;\n    redisCallback *cb;\n    dictEntry *de;\n    int pvariant;\n    char *stype;\n    hisds sname;\n\n    /* Custom reply functions are not supported for pub/sub. This will fail\n     * very hard when they are used... */\n    if (reply->type == REDIS_REPLY_ARRAY || reply->type == REDIS_REPLY_PUSH) {\n        assert(reply->elements >= 2);\n        assert(reply->element[0]->type == REDIS_REPLY_STRING);\n        stype = reply->element[0]->str;\n        pvariant = (tolower(stype[0]) == 'p') ? 1 : 0;\n\n        if (pvariant)\n            callbacks = ac->sub.patterns;\n        else\n            callbacks = ac->sub.channels;\n\n        /* Locate the right callback */\n        assert(reply->element[1]->type == REDIS_REPLY_STRING);\n        sname = hi_sdsnewlen(reply->element[1]->str,reply->element[1]->len);\n        if (sname == NULL)\n            goto oom;\n\n        de = dictFind(callbacks,sname);\n        if (de != NULL) {\n            cb = dictGetEntryVal(de);\n\n            /* If this is an subscribe reply decrease pending counter. */\n            if (strcasecmp(stype+pvariant,\"subscribe\") == 0) {\n                cb->pending_subs -= 1;\n            }\n\n            memcpy(dstcb,cb,sizeof(*dstcb));\n\n            /* If this is an unsubscribe message, remove it. */\n            if (strcasecmp(stype+pvariant,\"unsubscribe\") == 0) {\n                if (cb->pending_subs == 0)\n                    dictDelete(callbacks,sname);\n\n                /* If this was the last unsubscribe message, revert to\n                 * non-subscribe mode. */\n                assert(reply->element[2]->type == REDIS_REPLY_INTEGER);\n\n                /* Unset subscribed flag only when no pipelined pending subscribe. */\n                if (reply->element[2]->integer == 0\n                    && dictSize(ac->sub.channels) == 0\n                    && dictSize(ac->sub.patterns) == 0)\n                    c->flags &= ~REDIS_SUBSCRIBED;\n            }\n        }\n        hi_sdsfree(sname);\n    } else {\n        /* Shift callback for invalid commands. */\n        __redisShiftCallback(&ac->sub.invalid,dstcb);\n    }\n    return REDIS_OK;\noom:\n    __redisSetError(&(ac->c), REDIS_ERR_OOM, \"Out of memory\");\n    return REDIS_ERR;\n}\n\n#define redisIsSpontaneousPushReply(r) \\\n    (redisIsPushReply(r) && !redisIsSubscribeReply(r))\n\nstatic int redisIsSubscribeReply(redisReply *reply) {\n    char *str;\n    size_t len, off;\n\n    /* We will always have at least one string with the subscribe/message type */\n    if (reply->elements < 1 || reply->element[0]->type != REDIS_REPLY_STRING ||\n        reply->element[0]->len < sizeof(\"message\") - 1)\n    {\n        return 0;\n    }\n\n    /* Get the string/len moving past 'p' if needed */\n    off = tolower(reply->element[0]->str[0]) == 'p';\n    str = reply->element[0]->str + off;\n    len = reply->element[0]->len - off;\n\n    return !strncasecmp(str, \"subscribe\", len) ||\n           !strncasecmp(str, \"message\", len);\n\n}\n\nvoid redisProcessCallbacks(redisAsyncContext *ac) {\n    redisContext *c = &(ac->c);\n    redisCallback cb = {NULL, NULL, 0, NULL};\n    void *reply = NULL;\n    int status;\n\n    while((status = redisGetReply(c,&reply)) == REDIS_OK) {\n        if (reply == NULL) {\n            /* When the connection is being disconnected and there are\n             * no more replies, this is the cue to really disconnect. */\n            if (c->flags & REDIS_DISCONNECTING && hi_sdslen(c->obuf) == 0\n                && ac->replies.head == NULL) {\n                __redisAsyncDisconnect(ac);\n                return;\n            }\n\n            /* If monitor mode, repush callback */\n            if(c->flags & REDIS_MONITORING) {\n                __redisPushCallback(&ac->replies,&cb);\n            }\n\n            /* When the connection is not being disconnected, simply stop\n             * trying to get replies and wait for the next loop tick. */\n            break;\n        }\n\n        /* Send any non-subscribe related PUSH messages to our PUSH handler\n         * while allowing subscribe related PUSH messages to pass through.\n         * This allows existing code to be backward compatible and work in\n         * either RESP2 or RESP3 mode. */\n        if (redisIsSpontaneousPushReply(reply)) {\n            __redisRunPushCallback(ac, reply);\n            c->reader->fn->freeObject(reply);\n            continue;\n        }\n\n        /* Even if the context is subscribed, pending regular\n         * callbacks will get a reply before pub/sub messages arrive. */\n        if (__redisShiftCallback(&ac->replies,&cb) != REDIS_OK) {\n            /*\n             * A spontaneous reply in a not-subscribed context can be the error\n             * reply that is sent when a new connection exceeds the maximum\n             * number of allowed connections on the server side.\n             *\n             * This is seen as an error instead of a regular reply because the\n             * server closes the connection after sending it.\n             *\n             * To prevent the error from being overwritten by an EOF error the\n             * connection is closed here. See issue #43.\n             *\n             * Another possibility is that the server is loading its dataset.\n             * In this case we also want to close the connection, and have the\n             * user wait until the server is ready to take our request.\n             */\n            if (((redisReply*)reply)->type == REDIS_REPLY_ERROR) {\n                c->err = REDIS_ERR_OTHER;\n                snprintf(c->errstr,sizeof(c->errstr),\"%s\",((redisReply*)reply)->str);\n                c->reader->fn->freeObject(reply);\n                __redisAsyncDisconnect(ac);\n                return;\n            }\n            /* No more regular callbacks and no errors, the context *must* be subscribed or monitoring. */\n            assert((c->flags & REDIS_SUBSCRIBED || c->flags & REDIS_MONITORING));\n            if(c->flags & REDIS_SUBSCRIBED)\n                __redisGetSubscribeCallback(ac,reply,&cb);\n        }\n\n        if (cb.fn != NULL) {\n            __redisRunCallback(ac,&cb,reply);\n            c->reader->fn->freeObject(reply);\n\n            /* Proceed with free'ing when redisAsyncFree() was called. */\n            if (c->flags & REDIS_FREEING) {\n                __redisAsyncFree(ac);\n                return;\n            }\n        } else {\n            /* No callback for this reply. This can either be a NULL callback,\n             * or there were no callbacks to begin with. Either way, don't\n             * abort with an error, but simply ignore it because the client\n             * doesn't know what the server will spit out over the wire. */\n            c->reader->fn->freeObject(reply);\n        }\n    }\n\n    /* Disconnect when there was an error reading the reply */\n    if (status != REDIS_OK)\n        __redisAsyncDisconnect(ac);\n}\n\nstatic void __redisAsyncHandleConnectFailure(redisAsyncContext *ac) {\n    if (ac->onConnect) ac->onConnect(ac, REDIS_ERR);\n    __redisAsyncDisconnect(ac);\n}\n\n/* Internal helper function to detect socket status the first time a read or\n * write event fires. When connecting was not successful, the connect callback\n * is called with a REDIS_ERR status and the context is free'd. */\nstatic int __redisAsyncHandleConnect(redisAsyncContext *ac) {\n    int completed = 0;\n    redisContext *c = &(ac->c);\n\n    if (redisCheckConnectDone(c, &completed) == REDIS_ERR) {\n        /* Error! */\n        redisCheckSocketError(c);\n        __redisAsyncHandleConnectFailure(ac);\n        return REDIS_ERR;\n    } else if (completed == 1) {\n        /* connected! */\n        if (c->connection_type == REDIS_CONN_TCP &&\n            redisSetTcpNoDelay(c) == REDIS_ERR) {\n            __redisAsyncHandleConnectFailure(ac);\n            return REDIS_ERR;\n        }\n\n        if (ac->onConnect) ac->onConnect(ac, REDIS_OK);\n        c->flags |= REDIS_CONNECTED;\n        return REDIS_OK;\n    } else {\n        return REDIS_OK;\n    }\n}\n\nvoid redisAsyncRead(redisAsyncContext *ac) {\n    redisContext *c = &(ac->c);\n\n    if (redisBufferRead(c) == REDIS_ERR) {\n        __redisAsyncDisconnect(ac);\n    } else {\n        /* Always re-schedule reads */\n        _EL_ADD_READ(ac);\n        redisProcessCallbacks(ac);\n    }\n}\n\n/* This function should be called when the socket is readable.\n * It processes all replies that can be read and executes their callbacks.\n */\nvoid redisAsyncHandleRead(redisAsyncContext *ac) {\n    redisContext *c = &(ac->c);\n\n    if (!(c->flags & REDIS_CONNECTED)) {\n        /* Abort connect was not successful. */\n        if (__redisAsyncHandleConnect(ac) != REDIS_OK)\n            return;\n        /* Try again later when the context is still not connected. */\n        if (!(c->flags & REDIS_CONNECTED))\n            return;\n    }\n\n    c->funcs->async_read(ac);\n}\n\nvoid redisAsyncWrite(redisAsyncContext *ac) {\n    redisContext *c = &(ac->c);\n    int done = 0;\n\n    if (redisBufferWrite(c,&done) == REDIS_ERR) {\n        __redisAsyncDisconnect(ac);\n    } else {\n        /* Continue writing when not done, stop writing otherwise */\n        if (!done)\n            _EL_ADD_WRITE(ac);\n        else\n            _EL_DEL_WRITE(ac);\n\n        /* Always schedule reads after writes */\n        _EL_ADD_READ(ac);\n    }\n}\n\nvoid redisAsyncHandleWrite(redisAsyncContext *ac) {\n    redisContext *c = &(ac->c);\n\n    if (!(c->flags & REDIS_CONNECTED)) {\n        /* Abort connect was not successful. */\n        if (__redisAsyncHandleConnect(ac) != REDIS_OK)\n            return;\n        /* Try again later when the context is still not connected. */\n        if (!(c->flags & REDIS_CONNECTED))\n            return;\n    }\n\n    c->funcs->async_write(ac);\n}\n\nvoid redisAsyncHandleTimeout(redisAsyncContext *ac) {\n    redisContext *c = &(ac->c);\n    redisCallback cb;\n\n    if ((c->flags & REDIS_CONNECTED) && ac->replies.head == NULL) {\n        /* Nothing to do - just an idle timeout */\n        return;\n    }\n\n    if (!c->err) {\n        __redisSetError(c, REDIS_ERR_TIMEOUT, \"Timeout\");\n    }\n\n    if (!(c->flags & REDIS_CONNECTED) && ac->onConnect) {\n        ac->onConnect(ac, REDIS_ERR);\n    }\n\n    while (__redisShiftCallback(&ac->replies, &cb) == REDIS_OK) {\n        __redisRunCallback(ac, &cb, NULL);\n    }\n\n    /**\n     * TODO: Don't automatically sever the connection,\n     * rather, allow to ignore <x> responses before the queue is clear\n     */\n    __redisAsyncDisconnect(ac);\n}\n\n/* Sets a pointer to the first argument and its length starting at p. Returns\n * the number of bytes to skip to get to the following argument. */\nstatic const char *nextArgument(const char *start, const char **str, size_t *len) {\n    const char *p = start;\n    if (p[0] != '$') {\n        p = strchr(p,'$');\n        if (p == NULL) return NULL;\n    }\n\n    *len = (int)strtol(p+1,NULL,10);\n    p = strchr(p,'\\r');\n    assert(p);\n    *str = p+2;\n    return p+2+(*len)+2;\n}\n\n/* Helper function for the redisAsyncCommand* family of functions. Writes a\n * formatted command to the output buffer and registers the provided callback\n * function with the context. */\nstatic int __redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *cmd, size_t len) {\n    redisContext *c = &(ac->c);\n    redisCallback cb;\n    struct dict *cbdict;\n    dictEntry *de;\n    redisCallback *existcb;\n    int pvariant, hasnext;\n    const char *cstr, *astr;\n    size_t clen, alen;\n    const char *p;\n    hisds sname;\n    int ret;\n\n    /* Don't accept new commands when the connection is about to be closed. */\n    if (c->flags & (REDIS_DISCONNECTING | REDIS_FREEING)) return REDIS_ERR;\n\n    /* Setup callback */\n    cb.fn = fn;\n    cb.privdata = privdata;\n    cb.pending_subs = 1;\n\n    /* Find out which command will be appended. */\n    p = nextArgument(cmd,&cstr,&clen);\n    assert(p != NULL);\n    hasnext = (p[0] == '$');\n    pvariant = (tolower(cstr[0]) == 'p') ? 1 : 0;\n    cstr += pvariant;\n    clen -= pvariant;\n\n    if (hasnext && strncasecmp(cstr,\"subscribe\\r\\n\",11) == 0) {\n        c->flags |= REDIS_SUBSCRIBED;\n\n        /* Add every channel/pattern to the list of subscription callbacks. */\n        while ((p = nextArgument(p,&astr,&alen)) != NULL) {\n            sname = hi_sdsnewlen(astr,alen);\n            if (sname == NULL)\n                goto oom;\n\n            if (pvariant)\n                cbdict = ac->sub.patterns;\n            else\n                cbdict = ac->sub.channels;\n\n            de = dictFind(cbdict,sname);\n\n            if (de != NULL) {\n                existcb = dictGetEntryVal(de);\n                cb.pending_subs = existcb->pending_subs + 1;\n            }\n\n            ret = dictReplace(cbdict,sname,&cb);\n\n            if (ret == 0) hi_sdsfree(sname);\n        }\n    } else if (strncasecmp(cstr,\"unsubscribe\\r\\n\",13) == 0) {\n        /* It is only useful to call (P)UNSUBSCRIBE when the context is\n         * subscribed to one or more channels or patterns. */\n        if (!(c->flags & REDIS_SUBSCRIBED)) return REDIS_ERR;\n\n        /* (P)UNSUBSCRIBE does not have its own response: every channel or\n         * pattern that is unsubscribed will receive a message. This means we\n         * should not append a callback function for this command. */\n     } else if(strncasecmp(cstr,\"monitor\\r\\n\",9) == 0) {\n         /* Set monitor flag and push callback */\n         c->flags |= REDIS_MONITORING;\n         __redisPushCallback(&ac->replies,&cb);\n    } else {\n        if (c->flags & REDIS_SUBSCRIBED)\n            /* This will likely result in an error reply, but it needs to be\n             * received and passed to the callback. */\n            __redisPushCallback(&ac->sub.invalid,&cb);\n        else\n            __redisPushCallback(&ac->replies,&cb);\n    }\n\n    __redisAppendCommand(c,cmd,len);\n\n    /* Always schedule a write when the write buffer is non-empty */\n    _EL_ADD_WRITE(ac);\n\n    return REDIS_OK;\noom:\n    __redisSetError(&(ac->c), REDIS_ERR_OOM, \"Out of memory\");\n    return REDIS_ERR;\n}\n\nint redisvAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *format, va_list ap) {\n    char *cmd;\n    int len;\n    int status;\n    len = redisvFormatCommand(&cmd,format,ap);\n\n    /* We don't want to pass -1 or -2 to future functions as a length. */\n    if (len < 0)\n        return REDIS_ERR;\n\n    status = __redisAsyncCommand(ac,fn,privdata,cmd,len);\n    hi_free(cmd);\n    return status;\n}\n\nint redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *format, ...) {\n    va_list ap;\n    int status;\n    va_start(ap,format);\n    status = redisvAsyncCommand(ac,fn,privdata,format,ap);\n    va_end(ap);\n    return status;\n}\n\nint redisAsyncCommandArgv(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, int argc, const char **argv, const size_t *argvlen) {\n    hisds cmd;\n    int len;\n    int status;\n    len = redisFormatSdsCommandArgv(&cmd,argc,argv,argvlen);\n    if (len < 0)\n        return REDIS_ERR;\n    status = __redisAsyncCommand(ac,fn,privdata,cmd,len);\n    hi_sdsfree(cmd);\n    return status;\n}\n\nint redisAsyncFormattedCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *cmd, size_t len) {\n    int status = __redisAsyncCommand(ac,fn,privdata,cmd,len);\n    return status;\n}\n\nredisAsyncPushFn *redisAsyncSetPushCallback(redisAsyncContext *ac, redisAsyncPushFn *fn) {\n    redisAsyncPushFn *old = ac->push_cb;\n    ac->push_cb = fn;\n    return old;\n}\n\nint redisAsyncSetTimeout(redisAsyncContext *ac, struct timeval tv) {\n    if (!ac->c.command_timeout) {\n        ac->c.command_timeout = hi_calloc(1, sizeof(tv));\n        if (ac->c.command_timeout == NULL) {\n            __redisSetError(&ac->c, REDIS_ERR_OOM, \"Out of memory\");\n            __redisAsyncCopyError(ac);\n            return REDIS_ERR;\n        }\n    }\n\n    if (tv.tv_sec != ac->c.command_timeout->tv_sec ||\n        tv.tv_usec != ac->c.command_timeout->tv_usec)\n    {\n        *ac->c.command_timeout = tv;\n    }\n\n    return REDIS_OK;\n}\n"
  },
  {
    "path": "deps/hiredis/async.h",
    "content": "/*\n * Copyright (c) 2009-2011, Salvatore Sanfilippo <antirez at gmail dot com>\n * Copyright (c) 2010-2011, Pieter Noordhuis <pcnoordhuis at gmail dot com>\n *\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __HIREDIS_ASYNC_H\n#define __HIREDIS_ASYNC_H\n#include \"hiredis.h\"\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\nstruct redisAsyncContext; /* need forward declaration of redisAsyncContext */\nstruct dict; /* dictionary header is included in async.c */\n\n/* Reply callback prototype and container */\ntypedef void (redisCallbackFn)(struct redisAsyncContext*, void*, void*);\ntypedef struct redisCallback {\n    struct redisCallback *next; /* simple singly linked list */\n    redisCallbackFn *fn;\n    int pending_subs;\n    void *privdata;\n} redisCallback;\n\n/* List of callbacks for either regular replies or pub/sub */\ntypedef struct redisCallbackList {\n    redisCallback *head, *tail;\n} redisCallbackList;\n\n/* Connection callback prototypes */\ntypedef void (redisDisconnectCallback)(const struct redisAsyncContext*, int status);\ntypedef void (redisConnectCallback)(const struct redisAsyncContext*, int status);\ntypedef void(redisTimerCallback)(void *timer, void *privdata);\n\n/* Context for an async connection to Redis */\ntypedef struct redisAsyncContext {\n    /* Hold the regular context, so it can be realloc'ed. */\n    redisContext c;\n\n    /* Setup error flags so they can be used directly. */\n    int err;\n    char *errstr;\n\n    /* Not used by hiredis */\n    void *data;\n    void (*dataCleanup)(void *privdata);\n\n    /* Event library data and hooks */\n    struct {\n        void *data;\n\n        /* Hooks that are called when the library expects to start\n         * reading/writing. These functions should be idempotent. */\n        void (*addRead)(void *privdata);\n        void (*delRead)(void *privdata);\n        void (*addWrite)(void *privdata);\n        void (*delWrite)(void *privdata);\n        void (*cleanup)(void *privdata);\n        void (*scheduleTimer)(void *privdata, struct timeval tv);\n    } ev;\n\n    /* Called when either the connection is terminated due to an error or per\n     * user request. The status is set accordingly (REDIS_OK, REDIS_ERR). */\n    redisDisconnectCallback *onDisconnect;\n\n    /* Called when the first write event was received. */\n    redisConnectCallback *onConnect;\n\n    /* Regular command callbacks */\n    redisCallbackList replies;\n\n    /* Address used for connect() */\n    struct sockaddr *saddr;\n    size_t addrlen;\n\n    /* Subscription callbacks */\n    struct {\n        redisCallbackList invalid;\n        struct dict *channels;\n        struct dict *patterns;\n    } sub;\n\n    /* Any configured RESP3 PUSH handler */\n    redisAsyncPushFn *push_cb;\n} redisAsyncContext;\n\n/* Functions that proxy to hiredis */\nredisAsyncContext *redisAsyncConnectWithOptions(const redisOptions *options);\nredisAsyncContext *redisAsyncConnect(const char *ip, int port);\nredisAsyncContext *redisAsyncConnectBind(const char *ip, int port, const char *source_addr);\nredisAsyncContext *redisAsyncConnectBindWithReuse(const char *ip, int port,\n                                                  const char *source_addr);\nredisAsyncContext *redisAsyncConnectUnix(const char *path);\nint redisAsyncSetConnectCallback(redisAsyncContext *ac, redisConnectCallback *fn);\nint redisAsyncSetDisconnectCallback(redisAsyncContext *ac, redisDisconnectCallback *fn);\n\nredisAsyncPushFn *redisAsyncSetPushCallback(redisAsyncContext *ac, redisAsyncPushFn *fn);\nint redisAsyncSetTimeout(redisAsyncContext *ac, struct timeval tv);\nvoid redisAsyncDisconnect(redisAsyncContext *ac);\nvoid redisAsyncFree(redisAsyncContext *ac);\n\n/* Handle read/write events */\nvoid redisAsyncHandleRead(redisAsyncContext *ac);\nvoid redisAsyncHandleWrite(redisAsyncContext *ac);\nvoid redisAsyncHandleTimeout(redisAsyncContext *ac);\nvoid redisAsyncRead(redisAsyncContext *ac);\nvoid redisAsyncWrite(redisAsyncContext *ac);\n\n/* Command functions for an async context. Write the command to the\n * output buffer and register the provided callback. */\nint redisvAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *format, va_list ap);\nint redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *format, ...);\nint redisAsyncCommandArgv(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, int argc, const char **argv, const size_t *argvlen);\nint redisAsyncFormattedCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *cmd, size_t len);\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n"
  },
  {
    "path": "deps/hiredis/async_private.h",
    "content": "/*\n * Copyright (c) 2009-2011, Salvatore Sanfilippo <antirez at gmail dot com>\n * Copyright (c) 2010-2011, Pieter Noordhuis <pcnoordhuis at gmail dot com>\n *\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __HIREDIS_ASYNC_PRIVATE_H\n#define __HIREDIS_ASYNC_PRIVATE_H\n\n#define _EL_ADD_READ(ctx)                                         \\\n    do {                                                          \\\n        refreshTimeout(ctx);                                      \\\n        if ((ctx)->ev.addRead) (ctx)->ev.addRead((ctx)->ev.data); \\\n    } while (0)\n#define _EL_DEL_READ(ctx) do { \\\n        if ((ctx)->ev.delRead) (ctx)->ev.delRead((ctx)->ev.data); \\\n    } while(0)\n#define _EL_ADD_WRITE(ctx)                                          \\\n    do {                                                            \\\n        refreshTimeout(ctx);                                        \\\n        if ((ctx)->ev.addWrite) (ctx)->ev.addWrite((ctx)->ev.data); \\\n    } while (0)\n#define _EL_DEL_WRITE(ctx) do { \\\n        if ((ctx)->ev.delWrite) (ctx)->ev.delWrite((ctx)->ev.data); \\\n    } while(0)\n#define _EL_CLEANUP(ctx) do { \\\n        if ((ctx)->ev.cleanup) (ctx)->ev.cleanup((ctx)->ev.data); \\\n        ctx->ev.cleanup = NULL; \\\n    } while(0)\n\nstatic inline void refreshTimeout(redisAsyncContext *ctx) {\n    #define REDIS_TIMER_ISSET(tvp) \\\n        (tvp && ((tvp)->tv_sec || (tvp)->tv_usec))\n\n    #define REDIS_EL_TIMER(ac, tvp) \\\n        if ((ac)->ev.scheduleTimer && REDIS_TIMER_ISSET(tvp)) { \\\n            (ac)->ev.scheduleTimer((ac)->ev.data, *(tvp)); \\\n        }\n\n    if (ctx->c.flags & REDIS_CONNECTED) {\n        REDIS_EL_TIMER(ctx, ctx->c.command_timeout);\n    } else {\n        REDIS_EL_TIMER(ctx, ctx->c.connect_timeout);\n    }\n}\n\nvoid __redisAsyncDisconnect(redisAsyncContext *ac);\nvoid redisProcessCallbacks(redisAsyncContext *ac);\n\n#endif  /* __HIREDIS_ASYNC_PRIVATE_H */\n"
  },
  {
    "path": "deps/hiredis/dict.c",
    "content": "/* Hash table implementation.\n *\n * This file implements in memory hash tables with insert/del/replace/find/\n * get-random-element operations. Hash tables will auto resize if needed\n * tables of power of two in size are used, collisions are handled by\n * chaining. See the source code for more information... :)\n *\n * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"fmacros.h\"\n#include \"alloc.h\"\n#include <stdlib.h>\n#include <assert.h>\n#include <limits.h>\n#include \"dict.h\"\n\n/* -------------------------- private prototypes ---------------------------- */\n\nstatic int _dictExpandIfNeeded(dict *ht);\nstatic unsigned long _dictNextPower(unsigned long size);\nstatic int _dictKeyIndex(dict *ht, const void *key);\nstatic int _dictInit(dict *ht, dictType *type, void *privDataPtr);\n\n/* -------------------------- hash functions -------------------------------- */\n\n/* Generic hash function (a popular one from Bernstein).\n * I tested a few and this was the best. */\nstatic unsigned int dictGenHashFunction(const unsigned char *buf, int len) {\n    unsigned int hash = 5381;\n\n    while (len--)\n        hash = ((hash << 5) + hash) + (*buf++); /* hash * 33 + c */\n    return hash;\n}\n\n/* ----------------------------- API implementation ------------------------- */\n\n/* Reset an hashtable already initialized with ht_init().\n * NOTE: This function should only called by ht_destroy(). */\nstatic void _dictReset(dict *ht) {\n    ht->table = NULL;\n    ht->size = 0;\n    ht->sizemask = 0;\n    ht->used = 0;\n}\n\n/* Create a new hash table */\nstatic dict *dictCreate(dictType *type, void *privDataPtr) {\n    dict *ht = hi_malloc(sizeof(*ht));\n    if (ht == NULL)\n        return NULL;\n\n    _dictInit(ht,type,privDataPtr);\n    return ht;\n}\n\n/* Initialize the hash table */\nstatic int _dictInit(dict *ht, dictType *type, void *privDataPtr) {\n    _dictReset(ht);\n    ht->type = type;\n    ht->privdata = privDataPtr;\n    return DICT_OK;\n}\n\n/* Expand or create the hashtable */\nstatic int dictExpand(dict *ht, unsigned long size) {\n    dict n; /* the new hashtable */\n    unsigned long realsize = _dictNextPower(size), i;\n\n    /* the size is invalid if it is smaller than the number of\n     * elements already inside the hashtable */\n    if (ht->used > size)\n        return DICT_ERR;\n\n    _dictInit(&n, ht->type, ht->privdata);\n    n.size = realsize;\n    n.sizemask = realsize-1;\n    n.table = hi_calloc(realsize,sizeof(dictEntry*));\n    if (n.table == NULL)\n        return DICT_ERR;\n\n    /* Copy all the elements from the old to the new table:\n     * note that if the old hash table is empty ht->size is zero,\n     * so dictExpand just creates an hash table. */\n    n.used = ht->used;\n    for (i = 0; i < ht->size && ht->used > 0; i++) {\n        dictEntry *he, *nextHe;\n\n        if (ht->table[i] == NULL) continue;\n\n        /* For each hash entry on this slot... */\n        he = ht->table[i];\n        while(he) {\n            unsigned int h;\n\n            nextHe = he->next;\n            /* Get the new element index */\n            h = dictHashKey(ht, he->key) & n.sizemask;\n            he->next = n.table[h];\n            n.table[h] = he;\n            ht->used--;\n            /* Pass to the next element */\n            he = nextHe;\n        }\n    }\n    assert(ht->used == 0);\n    hi_free(ht->table);\n\n    /* Remap the new hashtable in the old */\n    *ht = n;\n    return DICT_OK;\n}\n\n/* Add an element to the target hash table */\nstatic int dictAdd(dict *ht, void *key, void *val) {\n    int index;\n    dictEntry *entry;\n\n    /* Get the index of the new element, or -1 if\n     * the element already exists. */\n    if ((index = _dictKeyIndex(ht, key)) == -1)\n        return DICT_ERR;\n\n    /* Allocates the memory and stores key */\n    entry = hi_malloc(sizeof(*entry));\n    if (entry == NULL)\n        return DICT_ERR;\n\n    entry->next = ht->table[index];\n    ht->table[index] = entry;\n\n    /* Set the hash entry fields. */\n    dictSetHashKey(ht, entry, key);\n    dictSetHashVal(ht, entry, val);\n    ht->used++;\n    return DICT_OK;\n}\n\n/* Add an element, discarding the old if the key already exists.\n * Return 1 if the key was added from scratch, 0 if there was already an\n * element with such key and dictReplace() just performed a value update\n * operation. */\nstatic int dictReplace(dict *ht, void *key, void *val) {\n    dictEntry *entry, auxentry;\n\n    /* Try to add the element. If the key\n     * does not exists dictAdd will succeed. */\n    if (dictAdd(ht, key, val) == DICT_OK)\n        return 1;\n    /* It already exists, get the entry */\n    entry = dictFind(ht, key);\n    if (entry == NULL)\n        return 0;\n\n    /* Free the old value and set the new one */\n    /* Set the new value and free the old one. Note that it is important\n     * to do that in this order, as the value may just be exactly the same\n     * as the previous one. In this context, think to reference counting,\n     * you want to increment (set), and then decrement (free), and not the\n     * reverse. */\n    auxentry = *entry;\n    dictSetHashVal(ht, entry, val);\n    dictFreeEntryVal(ht, &auxentry);\n    return 0;\n}\n\n/* Search and remove an element */\nstatic int dictDelete(dict *ht, const void *key) {\n    unsigned int h;\n    dictEntry *de, *prevde;\n\n    if (ht->size == 0)\n        return DICT_ERR;\n    h = dictHashKey(ht, key) & ht->sizemask;\n    de = ht->table[h];\n\n    prevde = NULL;\n    while(de) {\n        if (dictCompareHashKeys(ht,key,de->key)) {\n            /* Unlink the element from the list */\n            if (prevde)\n                prevde->next = de->next;\n            else\n                ht->table[h] = de->next;\n\n            dictFreeEntryKey(ht,de);\n            dictFreeEntryVal(ht,de);\n            hi_free(de);\n            ht->used--;\n            return DICT_OK;\n        }\n        prevde = de;\n        de = de->next;\n    }\n    return DICT_ERR; /* not found */\n}\n\n/* Destroy an entire hash table */\nstatic int _dictClear(dict *ht) {\n    unsigned long i;\n\n    /* Free all the elements */\n    for (i = 0; i < ht->size && ht->used > 0; i++) {\n        dictEntry *he, *nextHe;\n\n        if ((he = ht->table[i]) == NULL) continue;\n        while(he) {\n            nextHe = he->next;\n            dictFreeEntryKey(ht, he);\n            dictFreeEntryVal(ht, he);\n            hi_free(he);\n            ht->used--;\n            he = nextHe;\n        }\n    }\n    /* Free the table and the allocated cache structure */\n    hi_free(ht->table);\n    /* Re-initialize the table */\n    _dictReset(ht);\n    return DICT_OK; /* never fails */\n}\n\n/* Clear & Release the hash table */\nstatic void dictRelease(dict *ht) {\n    _dictClear(ht);\n    hi_free(ht);\n}\n\nstatic dictEntry *dictFind(dict *ht, const void *key) {\n    dictEntry *he;\n    unsigned int h;\n\n    if (ht->size == 0) return NULL;\n    h = dictHashKey(ht, key) & ht->sizemask;\n    he = ht->table[h];\n    while(he) {\n        if (dictCompareHashKeys(ht, key, he->key))\n            return he;\n        he = he->next;\n    }\n    return NULL;\n}\n\nstatic dictIterator *dictGetIterator(dict *ht) {\n    dictIterator *iter = hi_malloc(sizeof(*iter));\n    if (iter == NULL)\n        return NULL;\n\n    iter->ht = ht;\n    iter->index = -1;\n    iter->entry = NULL;\n    iter->nextEntry = NULL;\n    return iter;\n}\n\nstatic dictEntry *dictNext(dictIterator *iter) {\n    while (1) {\n        if (iter->entry == NULL) {\n            iter->index++;\n            if (iter->index >=\n                    (signed)iter->ht->size) break;\n            iter->entry = iter->ht->table[iter->index];\n        } else {\n            iter->entry = iter->nextEntry;\n        }\n        if (iter->entry) {\n            /* We need to save the 'next' here, the iterator user\n             * may delete the entry we are returning. */\n            iter->nextEntry = iter->entry->next;\n            return iter->entry;\n        }\n    }\n    return NULL;\n}\n\nstatic void dictReleaseIterator(dictIterator *iter) {\n    hi_free(iter);\n}\n\n/* ------------------------- private functions ------------------------------ */\n\n/* Expand the hash table if needed */\nstatic int _dictExpandIfNeeded(dict *ht) {\n    /* If the hash table is empty expand it to the initial size,\n     * if the table is \"full\" double its size. */\n    if (ht->size == 0)\n        return dictExpand(ht, DICT_HT_INITIAL_SIZE);\n    if (ht->used == ht->size)\n        return dictExpand(ht, ht->size*2);\n    return DICT_OK;\n}\n\n/* Our hash table capability is a power of two */\nstatic unsigned long _dictNextPower(unsigned long size) {\n    unsigned long i = DICT_HT_INITIAL_SIZE;\n\n    if (size >= LONG_MAX) return LONG_MAX;\n    while(1) {\n        if (i >= size)\n            return i;\n        i *= 2;\n    }\n}\n\n/* Returns the index of a free slot that can be populated with\n * an hash entry for the given 'key'.\n * If the key already exists, -1 is returned. */\nstatic int _dictKeyIndex(dict *ht, const void *key) {\n    unsigned int h;\n    dictEntry *he;\n\n    /* Expand the hashtable if needed */\n    if (_dictExpandIfNeeded(ht) == DICT_ERR)\n        return -1;\n    /* Compute the key hash value */\n    h = dictHashKey(ht, key) & ht->sizemask;\n    /* Search if this slot does not already contain the given key */\n    he = ht->table[h];\n    while(he) {\n        if (dictCompareHashKeys(ht, key, he->key))\n            return -1;\n        he = he->next;\n    }\n    return h;\n}\n\n"
  },
  {
    "path": "deps/hiredis/dict.h",
    "content": "/* Hash table implementation.\n *\n * This file implements in memory hash tables with insert/del/replace/find/\n * get-random-element operations. Hash tables will auto resize if needed\n * tables of power of two in size are used, collisions are handled by\n * chaining. See the source code for more information... :)\n *\n * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __DICT_H\n#define __DICT_H\n\n#define DICT_OK 0\n#define DICT_ERR 1\n\n/* Unused arguments generate annoying warnings... */\n#define DICT_NOTUSED(V) ((void) V)\n\ntypedef struct dictEntry {\n    void *key;\n    void *val;\n    struct dictEntry *next;\n} dictEntry;\n\ntypedef struct dictType {\n    unsigned int (*hashFunction)(const void *key);\n    void *(*keyDup)(void *privdata, const void *key);\n    void *(*valDup)(void *privdata, const void *obj);\n    int (*keyCompare)(void *privdata, const void *key1, const void *key2);\n    void (*keyDestructor)(void *privdata, void *key);\n    void (*valDestructor)(void *privdata, void *obj);\n} dictType;\n\ntypedef struct dict {\n    dictEntry **table;\n    dictType *type;\n    unsigned long size;\n    unsigned long sizemask;\n    unsigned long used;\n    void *privdata;\n} dict;\n\ntypedef struct dictIterator {\n    dict *ht;\n    int index;\n    dictEntry *entry, *nextEntry;\n} dictIterator;\n\n/* This is the initial size of every hash table */\n#define DICT_HT_INITIAL_SIZE     4\n\n/* ------------------------------- Macros ------------------------------------*/\n#define dictFreeEntryVal(ht, entry) \\\n    if ((ht)->type->valDestructor) \\\n        (ht)->type->valDestructor((ht)->privdata, (entry)->val)\n\n#define dictSetHashVal(ht, entry, _val_) do { \\\n    if ((ht)->type->valDup) \\\n        entry->val = (ht)->type->valDup((ht)->privdata, _val_); \\\n    else \\\n        entry->val = (_val_); \\\n} while(0)\n\n#define dictFreeEntryKey(ht, entry) \\\n    if ((ht)->type->keyDestructor) \\\n        (ht)->type->keyDestructor((ht)->privdata, (entry)->key)\n\n#define dictSetHashKey(ht, entry, _key_) do { \\\n    if ((ht)->type->keyDup) \\\n        entry->key = (ht)->type->keyDup((ht)->privdata, _key_); \\\n    else \\\n        entry->key = (_key_); \\\n} while(0)\n\n#define dictCompareHashKeys(ht, key1, key2) \\\n    (((ht)->type->keyCompare) ? \\\n        (ht)->type->keyCompare((ht)->privdata, key1, key2) : \\\n        (key1) == (key2))\n\n#define dictHashKey(ht, key) (ht)->type->hashFunction(key)\n\n#define dictGetEntryKey(he) ((he)->key)\n#define dictGetEntryVal(he) ((he)->val)\n#define dictSlots(ht) ((ht)->size)\n#define dictSize(ht) ((ht)->used)\n\n/* API */\nstatic unsigned int dictGenHashFunction(const unsigned char *buf, int len);\nstatic dict *dictCreate(dictType *type, void *privDataPtr);\nstatic int dictExpand(dict *ht, unsigned long size);\nstatic int dictAdd(dict *ht, void *key, void *val);\nstatic int dictReplace(dict *ht, void *key, void *val);\nstatic int dictDelete(dict *ht, const void *key);\nstatic void dictRelease(dict *ht);\nstatic dictEntry * dictFind(dict *ht, const void *key);\nstatic dictIterator *dictGetIterator(dict *ht);\nstatic dictEntry *dictNext(dictIterator *iter);\nstatic void dictReleaseIterator(dictIterator *iter);\n\n#endif /* __DICT_H */\n"
  },
  {
    "path": "deps/hiredis/examples/CMakeLists.txt",
    "content": "INCLUDE(FindPkgConfig)\n# Check for GLib\n\nPKG_CHECK_MODULES(GLIB2 glib-2.0)\nif (GLIB2_FOUND)\n    INCLUDE_DIRECTORIES(${GLIB2_INCLUDE_DIRS})\n    LINK_DIRECTORIES(${GLIB2_LIBRARY_DIRS})\n    ADD_EXECUTABLE(example-glib example-glib.c)\n    TARGET_LINK_LIBRARIES(example-glib hiredis ${GLIB2_LIBRARIES})\nENDIF(GLIB2_FOUND)\n\nFIND_PATH(LIBEV ev.h\n    HINTS /usr/local /usr/opt/local\n    ENV LIBEV_INCLUDE_DIR)\n\nif (LIBEV)\n    # Just compile and link with libev\n    ADD_EXECUTABLE(example-libev example-libev.c)\n    TARGET_LINK_LIBRARIES(example-libev hiredis ev)\nENDIF()\n\nFIND_PATH(LIBEVENT event.h)\nif (LIBEVENT)\n    ADD_EXECUTABLE(example-libevent example-libevent)\n    TARGET_LINK_LIBRARIES(example-libevent hiredis event)\nENDIF()\n\nFIND_PATH(LIBUV uv.h)\nIF (LIBUV)\n    ADD_EXECUTABLE(example-libuv example-libuv.c)\n    TARGET_LINK_LIBRARIES(example-libuv hiredis uv)\nENDIF()\n\nIF (APPLE)\n    FIND_LIBRARY(CF CoreFoundation)\n    ADD_EXECUTABLE(example-macosx example-macosx.c)\n    TARGET_LINK_LIBRARIES(example-macosx hiredis ${CF})\nENDIF()\n\nIF (ENABLE_SSL)\n    ADD_EXECUTABLE(example-ssl example-ssl.c)\n    TARGET_LINK_LIBRARIES(example-ssl hiredis hiredis_ssl)\nENDIF()\n\nADD_EXECUTABLE(example example.c)\nTARGET_LINK_LIBRARIES(example hiredis)\n\nADD_EXECUTABLE(example-push example-push.c)\nTARGET_LINK_LIBRARIES(example-push hiredis)\n"
  },
  {
    "path": "deps/hiredis/examples/example-ae.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <signal.h>\n\n#include <hiredis.h>\n#include <async.h>\n#include <adapters/ae.h>\n\n/* Put event loop in the global scope, so it can be explicitly stopped */\nstatic aeEventLoop *loop;\n\nvoid getCallback(redisAsyncContext *c, void *r, void *privdata) {\n    redisReply *reply = r;\n    if (reply == NULL) return;\n    printf(\"argv[%s]: %s\\n\", (char*)privdata, reply->str);\n\n    /* Disconnect after receiving the reply to GET */\n    redisAsyncDisconnect(c);\n}\n\nvoid connectCallback(const redisAsyncContext *c, int status) {\n    if (status != REDIS_OK) {\n        printf(\"Error: %s\\n\", c->errstr);\n        aeStop(loop);\n        return;\n    }\n\n    printf(\"Connected...\\n\");\n}\n\nvoid disconnectCallback(const redisAsyncContext *c, int status) {\n    if (status != REDIS_OK) {\n        printf(\"Error: %s\\n\", c->errstr);\n        aeStop(loop);\n        return;\n    }\n\n    printf(\"Disconnected...\\n\");\n    aeStop(loop);\n}\n\nint main (int argc, char **argv) {\n    signal(SIGPIPE, SIG_IGN);\n\n    redisAsyncContext *c = redisAsyncConnect(\"127.0.0.1\", 6379);\n    if (c->err) {\n        /* Let *c leak for now... */\n        printf(\"Error: %s\\n\", c->errstr);\n        return 1;\n    }\n\n    loop = aeCreateEventLoop(64);\n    redisAeAttach(loop, c);\n    redisAsyncSetConnectCallback(c,connectCallback);\n    redisAsyncSetDisconnectCallback(c,disconnectCallback);\n    redisAsyncCommand(c, NULL, NULL, \"SET key %b\", argv[argc-1], strlen(argv[argc-1]));\n    redisAsyncCommand(c, getCallback, (char*)\"end-1\", \"GET key\");\n    aeMain(loop);\n    return 0;\n}\n\n"
  },
  {
    "path": "deps/hiredis/examples/example-glib.c",
    "content": "#include <stdlib.h>\n\n#include <hiredis.h>\n#include <async.h>\n#include <adapters/glib.h>\n\nstatic GMainLoop *mainloop;\n\nstatic void\nconnect_cb (const redisAsyncContext *ac G_GNUC_UNUSED,\n            int status)\n{\n    if (status != REDIS_OK) {\n        g_printerr(\"Failed to connect: %s\\n\", ac->errstr);\n        g_main_loop_quit(mainloop);\n    } else {\n        g_printerr(\"Connected...\\n\");\n    }\n}\n\nstatic void\ndisconnect_cb (const redisAsyncContext *ac G_GNUC_UNUSED,\n               int status)\n{\n    if (status != REDIS_OK) {\n        g_error(\"Failed to disconnect: %s\", ac->errstr);\n    } else {\n        g_printerr(\"Disconnected...\\n\");\n        g_main_loop_quit(mainloop);\n    }\n}\n\nstatic void\ncommand_cb(redisAsyncContext *ac,\n           gpointer r,\n           gpointer user_data G_GNUC_UNUSED)\n{\n    redisReply *reply = r;\n\n    if (reply) {\n        g_print(\"REPLY: %s\\n\", reply->str);\n    }\n\n    redisAsyncDisconnect(ac);\n}\n\ngint\nmain (gint argc     G_GNUC_UNUSED,\n      gchar *argv[] G_GNUC_UNUSED)\n{\n    redisAsyncContext *ac;\n    GMainContext *context = NULL;\n    GSource *source;\n\n    ac = redisAsyncConnect(\"127.0.0.1\", 6379);\n    if (ac->err) {\n        g_printerr(\"%s\\n\", ac->errstr);\n        exit(EXIT_FAILURE);\n    }\n\n    source = redis_source_new(ac);\n    mainloop = g_main_loop_new(context, FALSE);\n    g_source_attach(source, context);\n\n    redisAsyncSetConnectCallback(ac, connect_cb);\n    redisAsyncSetDisconnectCallback(ac, disconnect_cb);\n    redisAsyncCommand(ac, command_cb, NULL, \"SET key 1234\");\n    redisAsyncCommand(ac, command_cb, NULL, \"GET key\");\n\n    g_main_loop_run(mainloop);\n\n    return EXIT_SUCCESS;\n}\n"
  },
  {
    "path": "deps/hiredis/examples/example-ivykis.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <signal.h>\n\n#include <hiredis.h>\n#include <async.h>\n#include <adapters/ivykis.h>\n\nvoid getCallback(redisAsyncContext *c, void *r, void *privdata) {\n    redisReply *reply = r;\n    if (reply == NULL) return;\n    printf(\"argv[%s]: %s\\n\", (char*)privdata, reply->str);\n\n    /* Disconnect after receiving the reply to GET */\n    redisAsyncDisconnect(c);\n}\n\nvoid connectCallback(const redisAsyncContext *c, int status) {\n    if (status != REDIS_OK) {\n        printf(\"Error: %s\\n\", c->errstr);\n        return;\n    }\n    printf(\"Connected...\\n\");\n}\n\nvoid disconnectCallback(const redisAsyncContext *c, int status) {\n    if (status != REDIS_OK) {\n        printf(\"Error: %s\\n\", c->errstr);\n        return;\n    }\n    printf(\"Disconnected...\\n\");\n}\n\nint main (int argc, char **argv) {\n#ifndef _WIN32\n    signal(SIGPIPE, SIG_IGN);\n#endif\n\n    iv_init();\n\n    redisAsyncContext *c = redisAsyncConnect(\"127.0.0.1\", 6379);\n    if (c->err) {\n        /* Let *c leak for now... */\n        printf(\"Error: %s\\n\", c->errstr);\n        return 1;\n    }\n\n    redisIvykisAttach(c);\n    redisAsyncSetConnectCallback(c,connectCallback);\n    redisAsyncSetDisconnectCallback(c,disconnectCallback);\n    redisAsyncCommand(c, NULL, NULL, \"SET key %b\", argv[argc-1], strlen(argv[argc-1]));\n    redisAsyncCommand(c, getCallback, (char*)\"end-1\", \"GET key\");\n\n    iv_main();\n\n    iv_deinit();\n\n    return 0;\n}\n"
  },
  {
    "path": "deps/hiredis/examples/example-libev.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <signal.h>\n\n#include <hiredis.h>\n#include <async.h>\n#include <adapters/libev.h>\n\nvoid getCallback(redisAsyncContext *c, void *r, void *privdata) {\n    redisReply *reply = r;\n    if (reply == NULL) return;\n    printf(\"argv[%s]: %s\\n\", (char*)privdata, reply->str);\n\n    /* Disconnect after receiving the reply to GET */\n    redisAsyncDisconnect(c);\n}\n\nvoid connectCallback(const redisAsyncContext *c, int status) {\n    if (status != REDIS_OK) {\n        printf(\"Error: %s\\n\", c->errstr);\n        return;\n    }\n    printf(\"Connected...\\n\");\n}\n\nvoid disconnectCallback(const redisAsyncContext *c, int status) {\n    if (status != REDIS_OK) {\n        printf(\"Error: %s\\n\", c->errstr);\n        return;\n    }\n    printf(\"Disconnected...\\n\");\n}\n\nint main (int argc, char **argv) {\n#ifndef _WIN32\n    signal(SIGPIPE, SIG_IGN);\n#endif\n\n    redisAsyncContext *c = redisAsyncConnect(\"127.0.0.1\", 6379);\n    if (c->err) {\n        /* Let *c leak for now... */\n        printf(\"Error: %s\\n\", c->errstr);\n        return 1;\n    }\n\n    redisLibevAttach(EV_DEFAULT_ c);\n    redisAsyncSetConnectCallback(c,connectCallback);\n    redisAsyncSetDisconnectCallback(c,disconnectCallback);\n    redisAsyncCommand(c, NULL, NULL, \"SET key %b\", argv[argc-1], strlen(argv[argc-1]));\n    redisAsyncCommand(c, getCallback, (char*)\"end-1\", \"GET key\");\n    ev_loop(EV_DEFAULT_ 0);\n    return 0;\n}\n"
  },
  {
    "path": "deps/hiredis/examples/example-libevent-ssl.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <signal.h>\n\n#include <hiredis.h>\n#include <hiredis_ssl.h>\n#include <async.h>\n#include <adapters/libevent.h>\n\nvoid getCallback(redisAsyncContext *c, void *r, void *privdata) {\n    redisReply *reply = r;\n    if (reply == NULL) return;\n    printf(\"argv[%s]: %s\\n\", (char*)privdata, reply->str);\n\n    /* Disconnect after receiving the reply to GET */\n    redisAsyncDisconnect(c);\n}\n\nvoid connectCallback(const redisAsyncContext *c, int status) {\n    if (status != REDIS_OK) {\n        printf(\"Error: %s\\n\", c->errstr);\n        return;\n    }\n    printf(\"Connected...\\n\");\n}\n\nvoid disconnectCallback(const redisAsyncContext *c, int status) {\n    if (status != REDIS_OK) {\n        printf(\"Error: %s\\n\", c->errstr);\n        return;\n    }\n    printf(\"Disconnected...\\n\");\n}\n\nint main (int argc, char **argv) {\n#ifndef _WIN32\n    signal(SIGPIPE, SIG_IGN);\n#endif\n\n    struct event_base *base = event_base_new();\n    if (argc < 5) {\n        fprintf(stderr,\n                \"Usage: %s <key> <host> <port> <cert> <certKey> [ca]\\n\", argv[0]);\n        exit(1);\n    }\n\n    const char *value = argv[1];\n    size_t nvalue = strlen(value);\n\n    const char *hostname = argv[2];\n    int port = atoi(argv[3]);\n\n    const char *cert = argv[4];\n    const char *certKey = argv[5];\n    const char *caCert = argc > 5 ? argv[6] : NULL;\n\n    redisSSLContext *ssl;\n    redisSSLContextError ssl_error;\n\n    redisInitOpenSSL();\n\n    ssl = redisCreateSSLContext(caCert, NULL,\n            cert, certKey, NULL, &ssl_error);\n    if (!ssl) {\n        printf(\"Error: %s\\n\", redisSSLContextGetError(ssl_error));\n        return 1;\n    }\n\n    redisAsyncContext *c = redisAsyncConnect(hostname, port);\n    if (c->err) {\n        /* Let *c leak for now... */\n        printf(\"Error: %s\\n\", c->errstr);\n        return 1;\n    }\n    if (redisInitiateSSLWithContext(&c->c, ssl) != REDIS_OK) {\n        printf(\"SSL Error!\\n\");\n        exit(1);\n    }\n\n    redisLibeventAttach(c,base);\n    redisAsyncSetConnectCallback(c,connectCallback);\n    redisAsyncSetDisconnectCallback(c,disconnectCallback);\n    redisAsyncCommand(c, NULL, NULL, \"SET key %b\", value, nvalue);\n    redisAsyncCommand(c, getCallback, (char*)\"end-1\", \"GET key\");\n    event_base_dispatch(base);\n\n    redisFreeSSLContext(ssl);\n    return 0;\n}\n"
  },
  {
    "path": "deps/hiredis/examples/example-libevent.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <signal.h>\n\n#include <hiredis.h>\n#include <async.h>\n#include <adapters/libevent.h>\n\nvoid getCallback(redisAsyncContext *c, void *r, void *privdata) {\n    redisReply *reply = r;\n    if (reply == NULL) {\n        if (c->errstr) {\n            printf(\"errstr: %s\\n\", c->errstr);\n        }\n        return;\n    }\n    printf(\"argv[%s]: %s\\n\", (char*)privdata, reply->str);\n\n    /* Disconnect after receiving the reply to GET */\n    redisAsyncDisconnect(c);\n}\n\nvoid connectCallback(const redisAsyncContext *c, int status) {\n    if (status != REDIS_OK) {\n        printf(\"Error: %s\\n\", c->errstr);\n        return;\n    }\n    printf(\"Connected...\\n\");\n}\n\nvoid disconnectCallback(const redisAsyncContext *c, int status) {\n    if (status != REDIS_OK) {\n        printf(\"Error: %s\\n\", c->errstr);\n        return;\n    }\n    printf(\"Disconnected...\\n\");\n}\n\nint main (int argc, char **argv) {\n#ifndef _WIN32\n    signal(SIGPIPE, SIG_IGN);\n#endif\n\n    struct event_base *base = event_base_new();\n    redisOptions options = {0};\n    REDIS_OPTIONS_SET_TCP(&options, \"127.0.0.1\", 6379);\n    struct timeval tv = {0};\n    tv.tv_sec = 1;\n    options.connect_timeout = &tv;\n\n\n    redisAsyncContext *c = redisAsyncConnectWithOptions(&options);\n    if (c->err) {\n        /* Let *c leak for now... */\n        printf(\"Error: %s\\n\", c->errstr);\n        return 1;\n    }\n\n    redisLibeventAttach(c,base);\n    redisAsyncSetConnectCallback(c,connectCallback);\n    redisAsyncSetDisconnectCallback(c,disconnectCallback);\n    redisAsyncCommand(c, NULL, NULL, \"SET key %b\", argv[argc-1], strlen(argv[argc-1]));\n    redisAsyncCommand(c, getCallback, (char*)\"end-1\", \"GET key\");\n    event_base_dispatch(base);\n    return 0;\n}\n"
  },
  {
    "path": "deps/hiredis/examples/example-libuv.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <signal.h>\n\n#include <hiredis.h>\n#include <async.h>\n#include <adapters/libuv.h>\n\nvoid getCallback(redisAsyncContext *c, void *r, void *privdata) {\n    redisReply *reply = r;\n    if (reply == NULL) return;\n    printf(\"argv[%s]: %s\\n\", (char*)privdata, reply->str);\n\n    /* Disconnect after receiving the reply to GET */\n    redisAsyncDisconnect(c);\n}\n\nvoid connectCallback(const redisAsyncContext *c, int status) {\n    if (status != REDIS_OK) {\n        printf(\"Error: %s\\n\", c->errstr);\n        return;\n    }\n    printf(\"Connected...\\n\");\n}\n\nvoid disconnectCallback(const redisAsyncContext *c, int status) {\n    if (status != REDIS_OK) {\n        printf(\"Error: %s\\n\", c->errstr);\n        return;\n    }\n    printf(\"Disconnected...\\n\");\n}\n\nint main (int argc, char **argv) {\n#ifndef _WIN32\n    signal(SIGPIPE, SIG_IGN);\n#endif\n\n    uv_loop_t* loop = uv_default_loop();\n\n    redisAsyncContext *c = redisAsyncConnect(\"127.0.0.1\", 6379);\n    if (c->err) {\n        /* Let *c leak for now... */\n        printf(\"Error: %s\\n\", c->errstr);\n        return 1;\n    }\n\n    redisLibuvAttach(c,loop);\n    redisAsyncSetConnectCallback(c,connectCallback);\n    redisAsyncSetDisconnectCallback(c,disconnectCallback);\n    redisAsyncCommand(c, NULL, NULL, \"SET key %b\", argv[argc-1], strlen(argv[argc-1]));\n    redisAsyncCommand(c, getCallback, (char*)\"end-1\", \"GET key\");\n    uv_run(loop, UV_RUN_DEFAULT);\n    return 0;\n}\n"
  },
  {
    "path": "deps/hiredis/examples/example-macosx.c",
    "content": "//\n//  Created by Дмитрий Бахвалов on 13.07.15.\n//  Copyright (c) 2015 Dmitry Bakhvalov. All rights reserved.\n//\n\n#include <stdio.h>\n\n#include <hiredis.h>\n#include <async.h>\n#include <adapters/macosx.h>\n\nvoid getCallback(redisAsyncContext *c, void *r, void *privdata) {\n    redisReply *reply = r;\n    if (reply == NULL) return;\n    printf(\"argv[%s]: %s\\n\", (char*)privdata, reply->str);\n\n    /* Disconnect after receiving the reply to GET */\n    redisAsyncDisconnect(c);\n}\n\nvoid connectCallback(const redisAsyncContext *c, int status) {\n    if (status != REDIS_OK) {\n        printf(\"Error: %s\\n\", c->errstr);\n        return;\n    }\n    printf(\"Connected...\\n\");\n}\n\nvoid disconnectCallback(const redisAsyncContext *c, int status) {\n    if (status != REDIS_OK) {\n        printf(\"Error: %s\\n\", c->errstr);\n        return;\n    }\n    CFRunLoopStop(CFRunLoopGetCurrent());\n    printf(\"Disconnected...\\n\");\n}\n\nint main (int argc, char **argv) {\n    signal(SIGPIPE, SIG_IGN);\n\n    CFRunLoopRef loop = CFRunLoopGetCurrent();\n    if( !loop ) {\n        printf(\"Error: Cannot get current run loop\\n\");\n        return 1;\n    }\n\n    redisAsyncContext *c = redisAsyncConnect(\"127.0.0.1\", 6379);\n    if (c->err) {\n        /* Let *c leak for now... */\n        printf(\"Error: %s\\n\", c->errstr);\n        return 1;\n    }\n\n    redisMacOSAttach(c, loop);\n\n    redisAsyncSetConnectCallback(c,connectCallback);\n    redisAsyncSetDisconnectCallback(c,disconnectCallback);\n\n    redisAsyncCommand(c, NULL, NULL, \"SET key %b\", argv[argc-1], strlen(argv[argc-1]));\n    redisAsyncCommand(c, getCallback, (char*)\"end-1\", \"GET key\");\n\n    CFRunLoopRun();\n\n    return 0;\n}\n\n"
  },
  {
    "path": "deps/hiredis/examples/example-push.c",
    "content": "/*\n * Copyright (c) 2020, Michael Grunder <michael dot grunder at gmail dot com>\n *\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <hiredis.h>\n#include <win32.h>\n\n#define KEY_COUNT 5\n\n#define panicAbort(fmt, ...) \\\n    do { \\\n        fprintf(stderr, \"%s:%d:%s(): \" fmt, __FILE__, __LINE__, __func__, __VA_ARGS__); \\\n        exit(-1); \\\n    } while (0)\n\nstatic void assertReplyAndFree(redisContext *context, redisReply *reply, int type) {\n    if (reply == NULL)\n        panicAbort(\"NULL reply from server (error: %s)\", context->errstr);\n\n    if (reply->type != type) {\n        if (reply->type == REDIS_REPLY_ERROR)\n            fprintf(stderr, \"Redis Error: %s\\n\", reply->str);\n\n        panicAbort(\"Expected reply type %d but got type %d\", type, reply->type);\n    }\n\n    freeReplyObject(reply);\n}\n\n/* Switch to the RESP3 protocol and enable client tracking */\nstatic void enableClientTracking(redisContext *c) {\n    redisReply *reply = redisCommand(c, \"HELLO 3\");\n    if (reply == NULL || c->err) {\n        panicAbort(\"NULL reply or server error (error: %s)\", c->errstr);\n    }\n\n    if (reply->type != REDIS_REPLY_MAP) {\n        fprintf(stderr, \"Error: Can't send HELLO 3 command.  Are you sure you're \");\n        fprintf(stderr, \"connected to redis-server >= 6.0.0?\\nRedis error: %s\\n\",\n                        reply->type == REDIS_REPLY_ERROR ? reply->str : \"(unknown)\");\n        exit(-1);\n    }\n\n    freeReplyObject(reply);\n\n    /* Enable client tracking */\n    reply = redisCommand(c, \"CLIENT TRACKING ON\");\n    assertReplyAndFree(c, reply, REDIS_REPLY_STATUS);\n}\n\nvoid pushReplyHandler(void *privdata, void *r) {\n    redisReply *reply = r;\n    int *invalidations = privdata;\n\n    /* Sanity check on the invalidation reply */\n    if (reply->type != REDIS_REPLY_PUSH || reply->elements != 2 ||\n        reply->element[1]->type != REDIS_REPLY_ARRAY ||\n        reply->element[1]->element[0]->type != REDIS_REPLY_STRING)\n    {\n        panicAbort(\"%s\", \"Can't parse PUSH message!\");\n    }\n\n    /* Increment our invalidation count */\n    *invalidations += 1;\n\n    printf(\"pushReplyHandler(): INVALIDATE '%s' (invalidation count: %d)\\n\",\n           reply->element[1]->element[0]->str, *invalidations);\n\n    freeReplyObject(reply);\n}\n\n/* We aren't actually freeing anything here, but it is included to show that we can\n * have hiredis call our data destructor when freeing the context */\nvoid privdata_dtor(void *privdata) {\n    unsigned int *icount = privdata;\n    printf(\"privdata_dtor():  In context privdata dtor (invalidations: %u)\\n\", *icount);\n}\n\nint main(int argc, char **argv) {\n    unsigned int j, invalidations = 0;\n    redisContext *c;\n    redisReply *reply;\n\n    const char *hostname = (argc > 1) ? argv[1] : \"127.0.0.1\";\n    int port = (argc > 2) ? atoi(argv[2]) : 6379;\n\n    redisOptions o = {0};\n    REDIS_OPTIONS_SET_TCP(&o, hostname, port);\n\n    /* Set our context privdata to the address of our invalidation counter.  Each\n     * time our PUSH handler is called, hiredis will pass the privdata for context.\n     *\n     * This could also be done after we create the context like so:\n     *\n     *    c->privdata = &invalidations;\n     *    c->free_privdata = privdata_dtor;\n     */\n    REDIS_OPTIONS_SET_PRIVDATA(&o, &invalidations, privdata_dtor);\n\n    /* Set our custom PUSH message handler */\n    o.push_cb = pushReplyHandler;\n\n    c = redisConnectWithOptions(&o);\n    if (c == NULL || c->err)\n        panicAbort(\"Connection error:  %s\", c ? c->errstr : \"OOM\");\n\n    /* Enable RESP3 and turn on client tracking */\n    enableClientTracking(c);\n\n    /* Set some keys and then read them back.  Once we do that, Redis will deliver\n     * invalidation push messages whenever the key is modified */\n    for (j = 0; j < KEY_COUNT; j++) {\n        reply = redisCommand(c, \"SET key:%d initial:%d\", j, j);\n        assertReplyAndFree(c, reply, REDIS_REPLY_STATUS);\n\n        reply = redisCommand(c, \"GET key:%d\", j);\n        assertReplyAndFree(c, reply, REDIS_REPLY_STRING);\n    }\n\n    /* Trigger invalidation messages by updating keys we just read */\n    for (j = 0; j < KEY_COUNT; j++) {\n        printf(\"            main(): SET key:%d update:%d\\n\", j, j);\n        reply = redisCommand(c, \"SET key:%d update:%d\", j, j);\n        assertReplyAndFree(c, reply, REDIS_REPLY_STATUS);\n        printf(\"            main(): SET REPLY OK\\n\");\n    }\n\n    printf(\"\\nTotal detected invalidations: %d, expected: %d\\n\", invalidations, KEY_COUNT);\n\n    /* PING server */\n    redisFree(c);\n}\n"
  },
  {
    "path": "deps/hiredis/examples/example-qt.cpp",
    "content": "#include <iostream>\nusing namespace std;\n\n#include <QCoreApplication>\n#include <QTimer>\n\n#include \"example-qt.h\"\n\nvoid getCallback(redisAsyncContext *, void * r, void * privdata) {\n\n    redisReply * reply = static_cast<redisReply *>(r);\n    ExampleQt * ex = static_cast<ExampleQt *>(privdata);\n    if (reply == nullptr || ex == nullptr) return;\n\n    cout << \"key: \" << reply->str << endl;\n\n    ex->finish();\n}\n\nvoid ExampleQt::run() {\n\n    m_ctx = redisAsyncConnect(\"localhost\", 6379);\n\n    if (m_ctx->err) {\n        cerr << \"Error: \" << m_ctx->errstr << endl;\n        redisAsyncFree(m_ctx);\n        emit finished();\n    }\n\n    m_adapter.setContext(m_ctx);\n\n    redisAsyncCommand(m_ctx, NULL, NULL, \"SET key %s\", m_value);\n    redisAsyncCommand(m_ctx, getCallback, this, \"GET key\");\n}\n\nint main (int argc, char **argv) {\n\n    QCoreApplication app(argc, argv);\n\n    ExampleQt example(argv[argc-1]);\n\n    QObject::connect(&example, SIGNAL(finished()), &app, SLOT(quit()));\n    QTimer::singleShot(0, &example, SLOT(run()));\n\n    return app.exec();\n}\n"
  },
  {
    "path": "deps/hiredis/examples/example-qt.h",
    "content": "#ifndef __HIREDIS_EXAMPLE_QT_H\n#define __HIREDIS_EXAMPLE_QT_H\n\n#include <adapters/qt.h>\n\nclass ExampleQt : public QObject {\n\n    Q_OBJECT\n\n    public:\n        ExampleQt(const char * value, QObject * parent = 0)\n            : QObject(parent), m_value(value) {}\n\n    signals:\n        void finished();\n\n    public slots:\n        void run();\n\n    private:\n        void finish() { emit finished(); }\n\n    private:\n        const char * m_value;\n        redisAsyncContext * m_ctx;\n        RedisQtAdapter m_adapter;\n\n    friend\n    void getCallback(redisAsyncContext *, void *, void *);\n};\n\n#endif /* !__HIREDIS_EXAMPLE_QT_H */\n"
  },
  {
    "path": "deps/hiredis/examples/example-ssl.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#include <hiredis.h>\n#include <hiredis_ssl.h>\n#include <win32.h>\n\nint main(int argc, char **argv) {\n    unsigned int j;\n    redisSSLContext *ssl;\n    redisSSLContextError ssl_error;\n    redisContext *c;\n    redisReply *reply;\n    if (argc < 4) {\n        printf(\"Usage: %s <host> <port> <cert> <key> [ca]\\n\", argv[0]);\n        exit(1);\n    }\n    const char *hostname = (argc > 1) ? argv[1] : \"127.0.0.1\";\n    int port = atoi(argv[2]);\n    const char *cert = argv[3];\n    const char *key = argv[4];\n    const char *ca = argc > 4 ? argv[5] : NULL;\n\n    redisInitOpenSSL();\n    ssl = redisCreateSSLContext(ca, NULL, cert, key, NULL, &ssl_error);\n    if (!ssl) {\n        printf(\"SSL Context error: %s\\n\",\n                redisSSLContextGetError(ssl_error));\n        exit(1);\n    }\n\n    struct timeval tv = { 1, 500000 }; // 1.5 seconds\n    redisOptions options = {0};\n    REDIS_OPTIONS_SET_TCP(&options, hostname, port);\n    options.connect_timeout = &tv;\n    c = redisConnectWithOptions(&options);\n\n    if (c == NULL || c->err) {\n        if (c) {\n            printf(\"Connection error: %s\\n\", c->errstr);\n            redisFree(c);\n        } else {\n            printf(\"Connection error: can't allocate redis context\\n\");\n        }\n        exit(1);\n    }\n\n    if (redisInitiateSSLWithContext(c, ssl) != REDIS_OK) {\n        printf(\"Couldn't initialize SSL!\\n\");\n        printf(\"Error: %s\\n\", c->errstr);\n        redisFree(c);\n        exit(1);\n    }\n\n    /* PING server */\n    reply = redisCommand(c,\"PING\");\n    printf(\"PING: %s\\n\", reply->str);\n    freeReplyObject(reply);\n\n    /* Set a key */\n    reply = redisCommand(c,\"SET %s %s\", \"foo\", \"hello world\");\n    printf(\"SET: %s\\n\", reply->str);\n    freeReplyObject(reply);\n\n    /* Set a key using binary safe API */\n    reply = redisCommand(c,\"SET %b %b\", \"bar\", (size_t) 3, \"hello\", (size_t) 5);\n    printf(\"SET (binary API): %s\\n\", reply->str);\n    freeReplyObject(reply);\n\n    /* Try a GET and two INCR */\n    reply = redisCommand(c,\"GET foo\");\n    printf(\"GET foo: %s\\n\", reply->str);\n    freeReplyObject(reply);\n\n    reply = redisCommand(c,\"INCR counter\");\n    printf(\"INCR counter: %lld\\n\", reply->integer);\n    freeReplyObject(reply);\n    /* again ... */\n    reply = redisCommand(c,\"INCR counter\");\n    printf(\"INCR counter: %lld\\n\", reply->integer);\n    freeReplyObject(reply);\n\n    /* Create a list of numbers, from 0 to 9 */\n    reply = redisCommand(c,\"DEL mylist\");\n    freeReplyObject(reply);\n    for (j = 0; j < 10; j++) {\n        char buf[64];\n\n        snprintf(buf,64,\"%u\",j);\n        reply = redisCommand(c,\"LPUSH mylist element-%s\", buf);\n        freeReplyObject(reply);\n    }\n\n    /* Let's check what we have inside the list */\n    reply = redisCommand(c,\"LRANGE mylist 0 -1\");\n    if (reply->type == REDIS_REPLY_ARRAY) {\n        for (j = 0; j < reply->elements; j++) {\n            printf(\"%u) %s\\n\", j, reply->element[j]->str);\n        }\n    }\n    freeReplyObject(reply);\n\n    /* Disconnects and frees the context */\n    redisFree(c);\n\n    redisFreeSSLContext(ssl);\n\n    return 0;\n}\n"
  },
  {
    "path": "deps/hiredis/examples/example.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <hiredis.h>\n#include <win32.h>\n\nint main(int argc, char **argv) {\n    unsigned int j, isunix = 0;\n    redisContext *c;\n    redisReply *reply;\n    const char *hostname = (argc > 1) ? argv[1] : \"127.0.0.1\";\n\n    if (argc > 2) {\n        if (*argv[2] == 'u' || *argv[2] == 'U') {\n            isunix = 1;\n            /* in this case, host is the path to the unix socket */\n            printf(\"Will connect to unix socket @%s\\n\", hostname);\n        }\n    }\n\n    int port = (argc > 2) ? atoi(argv[2]) : 6379;\n\n    struct timeval timeout = { 1, 500000 }; // 1.5 seconds\n    if (isunix) {\n        c = redisConnectUnixWithTimeout(hostname, timeout);\n    } else {\n        c = redisConnectWithTimeout(hostname, port, timeout);\n    }\n    if (c == NULL || c->err) {\n        if (c) {\n            printf(\"Connection error: %s\\n\", c->errstr);\n            redisFree(c);\n        } else {\n            printf(\"Connection error: can't allocate redis context\\n\");\n        }\n        exit(1);\n    }\n\n    /* PING server */\n    reply = redisCommand(c,\"PING\");\n    printf(\"PING: %s\\n\", reply->str);\n    freeReplyObject(reply);\n\n    /* Set a key */\n    reply = redisCommand(c,\"SET %s %s\", \"foo\", \"hello world\");\n    printf(\"SET: %s\\n\", reply->str);\n    freeReplyObject(reply);\n\n    /* Set a key using binary safe API */\n    reply = redisCommand(c,\"SET %b %b\", \"bar\", (size_t) 3, \"hello\", (size_t) 5);\n    printf(\"SET (binary API): %s\\n\", reply->str);\n    freeReplyObject(reply);\n\n    /* Try a GET and two INCR */\n    reply = redisCommand(c,\"GET foo\");\n    printf(\"GET foo: %s\\n\", reply->str);\n    freeReplyObject(reply);\n\n    reply = redisCommand(c,\"INCR counter\");\n    printf(\"INCR counter: %lld\\n\", reply->integer);\n    freeReplyObject(reply);\n    /* again ... */\n    reply = redisCommand(c,\"INCR counter\");\n    printf(\"INCR counter: %lld\\n\", reply->integer);\n    freeReplyObject(reply);\n\n    /* Create a list of numbers, from 0 to 9 */\n    reply = redisCommand(c,\"DEL mylist\");\n    freeReplyObject(reply);\n    for (j = 0; j < 10; j++) {\n        char buf[64];\n\n        snprintf(buf,64,\"%u\",j);\n        reply = redisCommand(c,\"LPUSH mylist element-%s\", buf);\n        freeReplyObject(reply);\n    }\n\n    /* Let's check what we have inside the list */\n    reply = redisCommand(c,\"LRANGE mylist 0 -1\");\n    if (reply->type == REDIS_REPLY_ARRAY) {\n        for (j = 0; j < reply->elements; j++) {\n            printf(\"%u) %s\\n\", j, reply->element[j]->str);\n        }\n    }\n    freeReplyObject(reply);\n\n    /* Disconnects and frees the context */\n    redisFree(c);\n\n    return 0;\n}\n"
  },
  {
    "path": "deps/hiredis/fmacros.h",
    "content": "#ifndef __HIREDIS_FMACRO_H\n#define __HIREDIS_FMACRO_H\n\n#define _XOPEN_SOURCE 600\n#define _POSIX_C_SOURCE 200112L\n\n#if defined(__APPLE__) && defined(__MACH__)\n/* Enable TCP_KEEPALIVE */\n#define _DARWIN_C_SOURCE\n#endif\n\n#endif\n"
  },
  {
    "path": "deps/hiredis/hiredis-config.cmake.in",
    "content": "@PACKAGE_INIT@\n\nset_and_check(hiredis_INCLUDEDIR \"@PACKAGE_INCLUDE_INSTALL_DIR@\")\n\nIF (NOT TARGET hiredis::hiredis)\n\tINCLUDE(${CMAKE_CURRENT_LIST_DIR}/hiredis-targets.cmake)\nENDIF()\n\nSET(hiredis_LIBRARIES hiredis::hiredis)\nSET(hiredis_INCLUDE_DIRS ${hiredis_INCLUDEDIR})\n\ncheck_required_components(hiredis)\n\n"
  },
  {
    "path": "deps/hiredis/hiredis.c",
    "content": "/*\n * Copyright (c) 2009-2011, Salvatore Sanfilippo <antirez at gmail dot com>\n * Copyright (c) 2010-2014, Pieter Noordhuis <pcnoordhuis at gmail dot com>\n * Copyright (c) 2015, Matt Stancliff <matt at genges dot com>,\n *                     Jan-Erik Rediger <janerik at fnordig dot com>\n *\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"fmacros.h\"\n#include <string.h>\n#include <stdlib.h>\n#include <assert.h>\n#include <errno.h>\n#include <ctype.h>\n\n#include \"hiredis.h\"\n#include \"net.h\"\n#include \"sds.h\"\n#include \"async.h\"\n#include \"win32.h\"\n\nextern int redisContextUpdateConnectTimeout(redisContext *c, const struct timeval *timeout);\nextern int redisContextUpdateCommandTimeout(redisContext *c, const struct timeval *timeout);\n\nstatic redisContextFuncs redisContextDefaultFuncs = {\n    .free_privctx = NULL,\n    .async_read = redisAsyncRead,\n    .async_write = redisAsyncWrite,\n    .read = redisNetRead,\n    .write = redisNetWrite\n};\n\nstatic redisReply *createReplyObject(int type);\nstatic void *createStringObject(const redisReadTask *task, char *str, size_t len);\nstatic void *createArrayObject(const redisReadTask *task, size_t elements);\nstatic void *createIntegerObject(const redisReadTask *task, long long value);\nstatic void *createDoubleObject(const redisReadTask *task, double value, char *str, size_t len);\nstatic void *createNilObject(const redisReadTask *task);\nstatic void *createBoolObject(const redisReadTask *task, int bval);\n\n/* Default set of functions to build the reply. Keep in mind that such a\n * function returning NULL is interpreted as OOM. */\nstatic redisReplyObjectFunctions defaultFunctions = {\n    createStringObject,\n    createArrayObject,\n    createIntegerObject,\n    createDoubleObject,\n    createNilObject,\n    createBoolObject,\n    freeReplyObject\n};\n\n/* Create a reply object */\nstatic redisReply *createReplyObject(int type) {\n    redisReply *r = hi_calloc(1,sizeof(*r));\n\n    if (r == NULL)\n        return NULL;\n\n    r->type = type;\n    return r;\n}\n\n/* Free a reply object */\nvoid freeReplyObject(void *reply) {\n    redisReply *r = reply;\n    size_t j;\n\n    if (r == NULL)\n        return;\n\n    switch(r->type) {\n    case REDIS_REPLY_INTEGER:\n        break; /* Nothing to free */\n    case REDIS_REPLY_ARRAY:\n    case REDIS_REPLY_MAP:\n    case REDIS_REPLY_SET:\n    case REDIS_REPLY_PUSH:\n        if (r->element != NULL) {\n            for (j = 0; j < r->elements; j++)\n                freeReplyObject(r->element[j]);\n            hi_free(r->element);\n        }\n        break;\n    case REDIS_REPLY_ERROR:\n    case REDIS_REPLY_STATUS:\n    case REDIS_REPLY_STRING:\n    case REDIS_REPLY_DOUBLE:\n    case REDIS_REPLY_VERB:\n        hi_free(r->str);\n        break;\n    }\n    hi_free(r);\n}\n\nstatic void *createStringObject(const redisReadTask *task, char *str, size_t len) {\n    redisReply *r, *parent;\n    char *buf;\n\n    r = createReplyObject(task->type);\n    if (r == NULL)\n        return NULL;\n\n    assert(task->type == REDIS_REPLY_ERROR  ||\n           task->type == REDIS_REPLY_STATUS ||\n           task->type == REDIS_REPLY_STRING ||\n           task->type == REDIS_REPLY_VERB);\n\n    /* Copy string value */\n    if (task->type == REDIS_REPLY_VERB) {\n        buf = hi_malloc(len-4+1); /* Skip 4 bytes of verbatim type header. */\n        if (buf == NULL) goto oom;\n\n        memcpy(r->vtype,str,3);\n        r->vtype[3] = '\\0';\n        memcpy(buf,str+4,len-4);\n        buf[len-4] = '\\0';\n        r->len = len - 4;\n    } else {\n        buf = hi_malloc(len+1);\n        if (buf == NULL) goto oom;\n\n        memcpy(buf,str,len);\n        buf[len] = '\\0';\n        r->len = len;\n    }\n    r->str = buf;\n\n    if (task->parent) {\n        parent = task->parent->obj;\n        assert(parent->type == REDIS_REPLY_ARRAY ||\n               parent->type == REDIS_REPLY_MAP ||\n               parent->type == REDIS_REPLY_SET ||\n               parent->type == REDIS_REPLY_PUSH);\n        parent->element[task->idx] = r;\n    }\n    return r;\n\noom:\n    freeReplyObject(r);\n    return NULL;\n}\n\nstatic void *createArrayObject(const redisReadTask *task, size_t elements) {\n    redisReply *r, *parent;\n\n    r = createReplyObject(task->type);\n    if (r == NULL)\n        return NULL;\n\n    if (elements > 0) {\n        if (SIZE_MAX / sizeof(redisReply*) < elements) return NULL;  /* Don't overflow */\n        r->element = hi_calloc(elements,sizeof(redisReply*));\n        if (r->element == NULL) {\n            freeReplyObject(r);\n            return NULL;\n        }\n    }\n\n    r->elements = elements;\n\n    if (task->parent) {\n        parent = task->parent->obj;\n        assert(parent->type == REDIS_REPLY_ARRAY ||\n               parent->type == REDIS_REPLY_MAP ||\n               parent->type == REDIS_REPLY_SET ||\n               parent->type == REDIS_REPLY_PUSH);\n        parent->element[task->idx] = r;\n    }\n    return r;\n}\n\nstatic void *createIntegerObject(const redisReadTask *task, long long value) {\n    redisReply *r, *parent;\n\n    r = createReplyObject(REDIS_REPLY_INTEGER);\n    if (r == NULL)\n        return NULL;\n\n    r->integer = value;\n\n    if (task->parent) {\n        parent = task->parent->obj;\n        assert(parent->type == REDIS_REPLY_ARRAY ||\n               parent->type == REDIS_REPLY_MAP ||\n               parent->type == REDIS_REPLY_SET ||\n               parent->type == REDIS_REPLY_PUSH);\n        parent->element[task->idx] = r;\n    }\n    return r;\n}\n\nstatic void *createDoubleObject(const redisReadTask *task, double value, char *str, size_t len) {\n    redisReply *r, *parent;\n\n    r = createReplyObject(REDIS_REPLY_DOUBLE);\n    if (r == NULL)\n        return NULL;\n\n    r->dval = value;\n    r->str = hi_malloc(len+1);\n    if (r->str == NULL) {\n        freeReplyObject(r);\n        return NULL;\n    }\n\n    /* The double reply also has the original protocol string representing a\n     * double as a null terminated string. This way the caller does not need\n     * to format back for string conversion, especially since Redis does efforts\n     * to make the string more human readable avoiding the calssical double\n     * decimal string conversion artifacts. */\n    memcpy(r->str, str, len);\n    r->str[len] = '\\0';\n\n    if (task->parent) {\n        parent = task->parent->obj;\n        assert(parent->type == REDIS_REPLY_ARRAY ||\n               parent->type == REDIS_REPLY_MAP ||\n               parent->type == REDIS_REPLY_SET);\n        parent->element[task->idx] = r;\n    }\n    return r;\n}\n\nstatic void *createNilObject(const redisReadTask *task) {\n    redisReply *r, *parent;\n\n    r = createReplyObject(REDIS_REPLY_NIL);\n    if (r == NULL)\n        return NULL;\n\n    if (task->parent) {\n        parent = task->parent->obj;\n        assert(parent->type == REDIS_REPLY_ARRAY ||\n               parent->type == REDIS_REPLY_MAP ||\n               parent->type == REDIS_REPLY_SET ||\n               parent->type == REDIS_REPLY_PUSH);\n        parent->element[task->idx] = r;\n    }\n    return r;\n}\n\nstatic void *createBoolObject(const redisReadTask *task, int bval) {\n    redisReply *r, *parent;\n\n    r = createReplyObject(REDIS_REPLY_BOOL);\n    if (r == NULL)\n        return NULL;\n\n    r->integer = bval != 0;\n\n    if (task->parent) {\n        parent = task->parent->obj;\n        assert(parent->type == REDIS_REPLY_ARRAY ||\n               parent->type == REDIS_REPLY_MAP ||\n               parent->type == REDIS_REPLY_SET);\n        parent->element[task->idx] = r;\n    }\n    return r;\n}\n\n/* Return the number of digits of 'v' when converted to string in radix 10.\n * Implementation borrowed from link in redis/src/util.c:string2ll(). */\nstatic uint32_t countDigits(uint64_t v) {\n  uint32_t result = 1;\n  for (;;) {\n    if (v < 10) return result;\n    if (v < 100) return result + 1;\n    if (v < 1000) return result + 2;\n    if (v < 10000) return result + 3;\n    v /= 10000U;\n    result += 4;\n  }\n}\n\n/* Helper that calculates the bulk length given a certain string length. */\nstatic size_t bulklen(size_t len) {\n    return 1+countDigits(len)+2+len+2;\n}\n\nint redisvFormatCommand(char **target, const char *format, va_list ap) {\n    const char *c = format;\n    char *cmd = NULL; /* final command */\n    int pos; /* position in final command */\n    hisds curarg, newarg; /* current argument */\n    int touched = 0; /* was the current argument touched? */\n    char **curargv = NULL, **newargv = NULL;\n    int argc = 0;\n    int totlen = 0;\n    int error_type = 0; /* 0 = no error; -1 = memory error; -2 = format error */\n    int j;\n\n    /* Abort if there is not target to set */\n    if (target == NULL)\n        return -1;\n\n    /* Build the command string accordingly to protocol */\n    curarg = hi_sdsempty();\n    if (curarg == NULL)\n        return -1;\n\n    while(*c != '\\0') {\n        if (*c != '%' || c[1] == '\\0') {\n            if (*c == ' ') {\n                if (touched) {\n                    newargv = hi_realloc(curargv,sizeof(char*)*(argc+1));\n                    if (newargv == NULL) goto memory_err;\n                    curargv = newargv;\n                    curargv[argc++] = curarg;\n                    totlen += bulklen(hi_sdslen(curarg));\n\n                    /* curarg is put in argv so it can be overwritten. */\n                    curarg = hi_sdsempty();\n                    if (curarg == NULL) goto memory_err;\n                    touched = 0;\n                }\n            } else {\n                newarg = hi_sdscatlen(curarg,c,1);\n                if (newarg == NULL) goto memory_err;\n                curarg = newarg;\n                touched = 1;\n            }\n        } else {\n            char *arg;\n            size_t size;\n\n            /* Set newarg so it can be checked even if it is not touched. */\n            newarg = curarg;\n\n            switch(c[1]) {\n            case 's':\n                arg = va_arg(ap,char*);\n                size = strlen(arg);\n                if (size > 0)\n                    newarg = hi_sdscatlen(curarg,arg,size);\n                break;\n            case 'b':\n                arg = va_arg(ap,char*);\n                size = va_arg(ap,size_t);\n                if (size > 0)\n                    newarg = hi_sdscatlen(curarg,arg,size);\n                break;\n            case '%':\n                newarg = hi_sdscat(curarg,\"%\");\n                break;\n            default:\n                /* Try to detect printf format */\n                {\n                    static const char intfmts[] = \"diouxX\";\n                    static const char flags[] = \"#0-+ \";\n                    char _format[16];\n                    const char *_p = c+1;\n                    size_t _l = 0;\n                    va_list _cpy;\n\n                    /* Flags */\n                    while (*_p != '\\0' && strchr(flags,*_p) != NULL) _p++;\n\n                    /* Field width */\n                    while (*_p != '\\0' && isdigit(*_p)) _p++;\n\n                    /* Precision */\n                    if (*_p == '.') {\n                        _p++;\n                        while (*_p != '\\0' && isdigit(*_p)) _p++;\n                    }\n\n                    /* Copy va_list before consuming with va_arg */\n                    va_copy(_cpy,ap);\n\n                    /* Integer conversion (without modifiers) */\n                    if (strchr(intfmts,*_p) != NULL) {\n                        va_arg(ap,int);\n                        goto fmt_valid;\n                    }\n\n                    /* Double conversion (without modifiers) */\n                    if (strchr(\"eEfFgGaA\",*_p) != NULL) {\n                        va_arg(ap,double);\n                        goto fmt_valid;\n                    }\n\n                    /* Size: char */\n                    if (_p[0] == 'h' && _p[1] == 'h') {\n                        _p += 2;\n                        if (*_p != '\\0' && strchr(intfmts,*_p) != NULL) {\n                            va_arg(ap,int); /* char gets promoted to int */\n                            goto fmt_valid;\n                        }\n                        goto fmt_invalid;\n                    }\n\n                    /* Size: short */\n                    if (_p[0] == 'h') {\n                        _p += 1;\n                        if (*_p != '\\0' && strchr(intfmts,*_p) != NULL) {\n                            va_arg(ap,int); /* short gets promoted to int */\n                            goto fmt_valid;\n                        }\n                        goto fmt_invalid;\n                    }\n\n                    /* Size: long long */\n                    if (_p[0] == 'l' && _p[1] == 'l') {\n                        _p += 2;\n                        if (*_p != '\\0' && strchr(intfmts,*_p) != NULL) {\n                            va_arg(ap,long long);\n                            goto fmt_valid;\n                        }\n                        goto fmt_invalid;\n                    }\n\n                    /* Size: long */\n                    if (_p[0] == 'l') {\n                        _p += 1;\n                        if (*_p != '\\0' && strchr(intfmts,*_p) != NULL) {\n                            va_arg(ap,long);\n                            goto fmt_valid;\n                        }\n                        goto fmt_invalid;\n                    }\n\n                fmt_invalid:\n                    va_end(_cpy);\n                    goto format_err;\n\n                fmt_valid:\n                    _l = (_p+1)-c;\n                    if (_l < sizeof(_format)-2) {\n                        memcpy(_format,c,_l);\n                        _format[_l] = '\\0';\n                        newarg = hi_sdscatvprintf(curarg,_format,_cpy);\n\n                        /* Update current position (note: outer blocks\n                         * increment c twice so compensate here) */\n                        c = _p-1;\n                    }\n\n                    va_end(_cpy);\n                    break;\n                }\n            }\n\n            if (newarg == NULL) goto memory_err;\n            curarg = newarg;\n\n            touched = 1;\n            c++;\n        }\n        c++;\n    }\n\n    /* Add the last argument if needed */\n    if (touched) {\n        newargv = hi_realloc(curargv,sizeof(char*)*(argc+1));\n        if (newargv == NULL) goto memory_err;\n        curargv = newargv;\n        curargv[argc++] = curarg;\n        totlen += bulklen(hi_sdslen(curarg));\n    } else {\n        hi_sdsfree(curarg);\n    }\n\n    /* Clear curarg because it was put in curargv or was free'd. */\n    curarg = NULL;\n\n    /* Add bytes needed to hold multi bulk count */\n    totlen += 1+countDigits(argc)+2;\n\n    /* Build the command at protocol level */\n    cmd = hi_malloc(totlen+1);\n    if (cmd == NULL) goto memory_err;\n\n    pos = sprintf(cmd,\"*%d\\r\\n\",argc);\n    for (j = 0; j < argc; j++) {\n        pos += sprintf(cmd+pos,\"$%zu\\r\\n\",hi_sdslen(curargv[j]));\n        memcpy(cmd+pos,curargv[j],hi_sdslen(curargv[j]));\n        pos += hi_sdslen(curargv[j]);\n        hi_sdsfree(curargv[j]);\n        cmd[pos++] = '\\r';\n        cmd[pos++] = '\\n';\n    }\n    assert(pos == totlen);\n    cmd[pos] = '\\0';\n\n    hi_free(curargv);\n    *target = cmd;\n    return totlen;\n\nformat_err:\n    error_type = -2;\n    goto cleanup;\n\nmemory_err:\n    error_type = -1;\n    goto cleanup;\n\ncleanup:\n    if (curargv) {\n        while(argc--)\n            hi_sdsfree(curargv[argc]);\n        hi_free(curargv);\n    }\n\n    hi_sdsfree(curarg);\n    hi_free(cmd);\n\n    return error_type;\n}\n\n/* Format a command according to the Redis protocol. This function\n * takes a format similar to printf:\n *\n * %s represents a C null terminated string you want to interpolate\n * %b represents a binary safe string\n *\n * When using %b you need to provide both the pointer to the string\n * and the length in bytes as a size_t. Examples:\n *\n * len = redisFormatCommand(target, \"GET %s\", mykey);\n * len = redisFormatCommand(target, \"SET %s %b\", mykey, myval, myvallen);\n */\nint redisFormatCommand(char **target, const char *format, ...) {\n    va_list ap;\n    int len;\n    va_start(ap,format);\n    len = redisvFormatCommand(target,format,ap);\n    va_end(ap);\n\n    /* The API says \"-1\" means bad result, but we now also return \"-2\" in some\n     * cases.  Force the return value to always be -1. */\n    if (len < 0)\n        len = -1;\n\n    return len;\n}\n\n/* Format a command according to the Redis protocol using an hisds string and\n * hi_sdscatfmt for the processing of arguments. This function takes the\n * number of arguments, an array with arguments and an array with their\n * lengths. If the latter is set to NULL, strlen will be used to compute the\n * argument lengths.\n */\nint redisFormatSdsCommandArgv(hisds *target, int argc, const char **argv,\n                              const size_t *argvlen)\n{\n    hisds cmd, aux;\n    unsigned long long totlen;\n    int j;\n    size_t len;\n\n    /* Abort on a NULL target */\n    if (target == NULL)\n        return -1;\n\n    /* Calculate our total size */\n    totlen = 1+countDigits(argc)+2;\n    for (j = 0; j < argc; j++) {\n        len = argvlen ? argvlen[j] : strlen(argv[j]);\n        totlen += bulklen(len);\n    }\n\n    /* Use an SDS string for command construction */\n    cmd = hi_sdsempty();\n    if (cmd == NULL)\n        return -1;\n\n    /* We already know how much storage we need */\n    aux = hi_sdsMakeRoomFor(cmd, totlen);\n    if (aux == NULL) {\n        hi_sdsfree(cmd);\n        return -1;\n    }\n\n    cmd = aux;\n\n    /* Construct command */\n    cmd = hi_sdscatfmt(cmd, \"*%i\\r\\n\", argc);\n    for (j=0; j < argc; j++) {\n        len = argvlen ? argvlen[j] : strlen(argv[j]);\n        cmd = hi_sdscatfmt(cmd, \"$%u\\r\\n\", len);\n        cmd = hi_sdscatlen(cmd, argv[j], len);\n        cmd = hi_sdscatlen(cmd, \"\\r\\n\", sizeof(\"\\r\\n\")-1);\n    }\n\n    assert(hi_sdslen(cmd)==totlen);\n\n    *target = cmd;\n    return totlen;\n}\n\nvoid redisFreeSdsCommand(hisds cmd) {\n    hi_sdsfree(cmd);\n}\n\n/* Format a command according to the Redis protocol. This function takes the\n * number of arguments, an array with arguments and an array with their\n * lengths. If the latter is set to NULL, strlen will be used to compute the\n * argument lengths.\n */\nint redisFormatCommandArgv(char **target, int argc, const char **argv, const size_t *argvlen) {\n    char *cmd = NULL; /* final command */\n    int pos; /* position in final command */\n    size_t len;\n    int totlen, j;\n\n    /* Abort on a NULL target */\n    if (target == NULL)\n        return -1;\n\n    /* Calculate number of bytes needed for the command */\n    totlen = 1+countDigits(argc)+2;\n    for (j = 0; j < argc; j++) {\n        len = argvlen ? argvlen[j] : strlen(argv[j]);\n        totlen += bulklen(len);\n    }\n\n    /* Build the command at protocol level */\n    cmd = hi_malloc(totlen+1);\n    if (cmd == NULL)\n        return -1;\n\n    pos = sprintf(cmd,\"*%d\\r\\n\",argc);\n    for (j = 0; j < argc; j++) {\n        len = argvlen ? argvlen[j] : strlen(argv[j]);\n        pos += sprintf(cmd+pos,\"$%zu\\r\\n\",len);\n        memcpy(cmd+pos,argv[j],len);\n        pos += len;\n        cmd[pos++] = '\\r';\n        cmd[pos++] = '\\n';\n    }\n    assert(pos == totlen);\n    cmd[pos] = '\\0';\n\n    *target = cmd;\n    return totlen;\n}\n\nvoid redisFreeCommand(char *cmd) {\n    hi_free(cmd);\n}\n\nvoid __redisSetError(redisContext *c, int type, const char *str) {\n    size_t len;\n\n    c->err = type;\n    if (str != NULL) {\n        len = strlen(str);\n        len = len < (sizeof(c->errstr)-1) ? len : (sizeof(c->errstr)-1);\n        memcpy(c->errstr,str,len);\n        c->errstr[len] = '\\0';\n    } else {\n        /* Only REDIS_ERR_IO may lack a description! */\n        assert(type == REDIS_ERR_IO);\n        strerror_r(errno, c->errstr, sizeof(c->errstr));\n    }\n}\n\nredisReader *redisReaderCreate(void) {\n    return redisReaderCreateWithFunctions(&defaultFunctions);\n}\n\nstatic void redisPushAutoFree(void *privdata, void *reply) {\n    (void)privdata;\n    freeReplyObject(reply);\n}\n\nstatic redisContext *redisContextInit(void) {\n    redisContext *c;\n\n    c = hi_calloc(1, sizeof(*c));\n    if (c == NULL)\n        return NULL;\n\n    c->funcs = &redisContextDefaultFuncs;\n\n    c->obuf = hi_sdsempty();\n    c->reader = redisReaderCreate();\n    c->fd = REDIS_INVALID_FD;\n\n    if (c->obuf == NULL || c->reader == NULL) {\n        redisFree(c);\n        return NULL;\n    }\n\n    return c;\n}\n\nvoid redisFree(redisContext *c) {\n    if (c == NULL)\n        return;\n    redisNetClose(c);\n\n    hi_sdsfree(c->obuf);\n    redisReaderFree(c->reader);\n    hi_free(c->tcp.host);\n    hi_free(c->tcp.source_addr);\n    hi_free(c->unix_sock.path);\n    hi_free(c->connect_timeout);\n    hi_free(c->command_timeout);\n    hi_free(c->saddr);\n\n    if (c->privdata && c->free_privdata)\n        c->free_privdata(c->privdata);\n\n    if (c->funcs->free_privctx)\n        c->funcs->free_privctx(c->privctx);\n\n    memset(c, 0xff, sizeof(*c));\n    hi_free(c);\n}\n\nredisFD redisFreeKeepFd(redisContext *c) {\n    redisFD fd = c->fd;\n    c->fd = REDIS_INVALID_FD;\n    redisFree(c);\n    return fd;\n}\n\nint redisReconnect(redisContext *c) {\n    c->err = 0;\n    memset(c->errstr, '\\0', strlen(c->errstr));\n\n    if (c->privctx && c->funcs->free_privctx) {\n        c->funcs->free_privctx(c->privctx);\n        c->privctx = NULL;\n    }\n\n    redisNetClose(c);\n\n    hi_sdsfree(c->obuf);\n    redisReaderFree(c->reader);\n\n    c->obuf = hi_sdsempty();\n    c->reader = redisReaderCreate();\n\n    if (c->obuf == NULL || c->reader == NULL) {\n        __redisSetError(c, REDIS_ERR_OOM, \"Out of memory\");\n        return REDIS_ERR;\n    }\n\n    int ret = REDIS_ERR;\n    if (c->connection_type == REDIS_CONN_TCP) {\n        ret = redisContextConnectBindTcp(c, c->tcp.host, c->tcp.port,\n               c->connect_timeout, c->tcp.source_addr);\n    } else if (c->connection_type == REDIS_CONN_UNIX) {\n        ret = redisContextConnectUnix(c, c->unix_sock.path, c->connect_timeout);\n    } else {\n        /* Something bad happened here and shouldn't have. There isn't\n           enough information in the context to reconnect. */\n        __redisSetError(c,REDIS_ERR_OTHER,\"Not enough information to reconnect\");\n        ret = REDIS_ERR;\n    }\n\n    if (c->command_timeout != NULL && (c->flags & REDIS_BLOCK) && c->fd != REDIS_INVALID_FD) {\n        redisContextSetTimeout(c, *c->command_timeout);\n    }\n\n    return ret;\n}\n\nredisContext *redisConnectWithOptions(const redisOptions *options) {\n    redisContext *c = redisContextInit();\n    if (c == NULL) {\n        return NULL;\n    }\n    if (!(options->options & REDIS_OPT_NONBLOCK)) {\n        c->flags |= REDIS_BLOCK;\n    }\n    if (options->options & REDIS_OPT_REUSEADDR) {\n        c->flags |= REDIS_REUSEADDR;\n    }\n    if (options->options & REDIS_OPT_NOAUTOFREE) {\n        c->flags |= REDIS_NO_AUTO_FREE;\n    }\n\n    /* Set any user supplied RESP3 PUSH handler or use freeReplyObject\n     * as a default unless specifically flagged that we don't want one. */\n    if (options->push_cb != NULL)\n        redisSetPushCallback(c, options->push_cb);\n    else if (!(options->options & REDIS_OPT_NO_PUSH_AUTOFREE))\n        redisSetPushCallback(c, redisPushAutoFree);\n\n    c->privdata = options->privdata;\n    c->free_privdata = options->free_privdata;\n\n    if (redisContextUpdateConnectTimeout(c, options->connect_timeout) != REDIS_OK ||\n        redisContextUpdateCommandTimeout(c, options->command_timeout) != REDIS_OK) {\n        __redisSetError(c, REDIS_ERR_OOM, \"Out of memory\");\n        return c;\n    }\n\n    if (options->type == REDIS_CONN_TCP) {\n        redisContextConnectBindTcp(c, options->endpoint.tcp.ip,\n                                   options->endpoint.tcp.port, options->connect_timeout,\n                                   options->endpoint.tcp.source_addr);\n    } else if (options->type == REDIS_CONN_UNIX) {\n        redisContextConnectUnix(c, options->endpoint.unix_socket,\n                                options->connect_timeout);\n    } else if (options->type == REDIS_CONN_USERFD) {\n        c->fd = options->endpoint.fd;\n        c->flags |= REDIS_CONNECTED;\n    } else {\n        // Unknown type - FIXME - FREE\n        return NULL;\n    }\n\n    if (options->command_timeout != NULL && (c->flags & REDIS_BLOCK) && c->fd != REDIS_INVALID_FD) {\n        redisContextSetTimeout(c, *options->command_timeout);\n    }\n\n    return c;\n}\n\n/* Connect to a Redis instance. On error the field error in the returned\n * context will be set to the return value of the error function.\n * When no set of reply functions is given, the default set will be used. */\nredisContext *redisConnect(const char *ip, int port) {\n    redisOptions options = {0};\n    REDIS_OPTIONS_SET_TCP(&options, ip, port);\n    return redisConnectWithOptions(&options);\n}\n\nredisContext *redisConnectWithTimeout(const char *ip, int port, const struct timeval tv) {\n    redisOptions options = {0};\n    REDIS_OPTIONS_SET_TCP(&options, ip, port);\n    options.connect_timeout = &tv;\n    return redisConnectWithOptions(&options);\n}\n\nredisContext *redisConnectNonBlock(const char *ip, int port) {\n    redisOptions options = {0};\n    REDIS_OPTIONS_SET_TCP(&options, ip, port);\n    options.options |= REDIS_OPT_NONBLOCK;\n    return redisConnectWithOptions(&options);\n}\n\nredisContext *redisConnectBindNonBlock(const char *ip, int port,\n                                       const char *source_addr) {\n    redisOptions options = {0};\n    REDIS_OPTIONS_SET_TCP(&options, ip, port);\n    options.endpoint.tcp.source_addr = source_addr;\n    options.options |= REDIS_OPT_NONBLOCK;\n    return redisConnectWithOptions(&options);\n}\n\nredisContext *redisConnectBindNonBlockWithReuse(const char *ip, int port,\n                                                const char *source_addr) {\n    redisOptions options = {0};\n    REDIS_OPTIONS_SET_TCP(&options, ip, port);\n    options.endpoint.tcp.source_addr = source_addr;\n    options.options |= REDIS_OPT_NONBLOCK|REDIS_OPT_REUSEADDR;\n    return redisConnectWithOptions(&options);\n}\n\nredisContext *redisConnectUnix(const char *path) {\n    redisOptions options = {0};\n    REDIS_OPTIONS_SET_UNIX(&options, path);\n    return redisConnectWithOptions(&options);\n}\n\nredisContext *redisConnectUnixWithTimeout(const char *path, const struct timeval tv) {\n    redisOptions options = {0};\n    REDIS_OPTIONS_SET_UNIX(&options, path);\n    options.connect_timeout = &tv;\n    return redisConnectWithOptions(&options);\n}\n\nredisContext *redisConnectUnixNonBlock(const char *path) {\n    redisOptions options = {0};\n    REDIS_OPTIONS_SET_UNIX(&options, path);\n    options.options |= REDIS_OPT_NONBLOCK;\n    return redisConnectWithOptions(&options);\n}\n\nredisContext *redisConnectFd(redisFD fd) {\n    redisOptions options = {0};\n    options.type = REDIS_CONN_USERFD;\n    options.endpoint.fd = fd;\n    return redisConnectWithOptions(&options);\n}\n\n/* Set read/write timeout on a blocking socket. */\nint redisSetTimeout(redisContext *c, const struct timeval tv) {\n    if (c->flags & REDIS_BLOCK)\n        return redisContextSetTimeout(c,tv);\n    return REDIS_ERR;\n}\n\n/* Enable connection KeepAlive. */\nint redisEnableKeepAlive(redisContext *c) {\n    if (redisKeepAlive(c, REDIS_KEEPALIVE_INTERVAL) != REDIS_OK)\n        return REDIS_ERR;\n    return REDIS_OK;\n}\n\n/* Set a user provided RESP3 PUSH handler and return any old one set. */\nredisPushFn *redisSetPushCallback(redisContext *c, redisPushFn *fn) {\n    redisPushFn *old = c->push_cb;\n    c->push_cb = fn;\n    return old;\n}\n\n/* Use this function to handle a read event on the descriptor. It will try\n * and read some bytes from the socket and feed them to the reply parser.\n *\n * After this function is called, you may use redisGetReplyFromReader to\n * see if there is a reply available. */\nint redisBufferRead(redisContext *c) {\n    char buf[1024*16];\n    int nread;\n\n    /* Return early when the context has seen an error. */\n    if (c->err)\n        return REDIS_ERR;\n\n    nread = c->funcs->read(c, buf, sizeof(buf));\n    if (nread > 0) {\n        if (redisReaderFeed(c->reader, buf, nread) != REDIS_OK) {\n            __redisSetError(c, c->reader->err, c->reader->errstr);\n            return REDIS_ERR;\n        } else {\n        }\n    } else if (nread < 0) {\n        return REDIS_ERR;\n    }\n    return REDIS_OK;\n}\n\n/* Write the output buffer to the socket.\n *\n * Returns REDIS_OK when the buffer is empty, or (a part of) the buffer was\n * successfully written to the socket. When the buffer is empty after the\n * write operation, \"done\" is set to 1 (if given).\n *\n * Returns REDIS_ERR if an error occurred trying to write and sets\n * c->errstr to hold the appropriate error string.\n */\nint redisBufferWrite(redisContext *c, int *done) {\n\n    /* Return early when the context has seen an error. */\n    if (c->err)\n        return REDIS_ERR;\n\n    if (hi_sdslen(c->obuf) > 0) {\n        ssize_t nwritten = c->funcs->write(c);\n        if (nwritten < 0) {\n            return REDIS_ERR;\n        } else if (nwritten > 0) {\n            if (nwritten == (ssize_t)hi_sdslen(c->obuf)) {\n                hi_sdsfree(c->obuf);\n                c->obuf = hi_sdsempty();\n                if (c->obuf == NULL)\n                    goto oom;\n            } else {\n                if (hi_sdsrange(c->obuf,nwritten,-1) < 0) goto oom;\n            }\n        }\n    }\n    if (done != NULL) *done = (hi_sdslen(c->obuf) == 0);\n    return REDIS_OK;\n\noom:\n    __redisSetError(c, REDIS_ERR_OOM, \"Out of memory\");\n    return REDIS_ERR;\n}\n\n/* Internal helper function to try and get a reply from the reader,\n * or set an error in the context otherwise. */\nint redisGetReplyFromReader(redisContext *c, void **reply) {\n    if (redisReaderGetReply(c->reader,reply) == REDIS_ERR) {\n        __redisSetError(c,c->reader->err,c->reader->errstr);\n        return REDIS_ERR;\n    }\n\n    return REDIS_OK;\n}\n\n/* Internal helper that returns 1 if the reply was a RESP3 PUSH\n * message and we handled it with a user-provided callback. */\nstatic int redisHandledPushReply(redisContext *c, void *reply) {\n    if (reply && c->push_cb && redisIsPushReply(reply)) {\n        c->push_cb(c->privdata, reply);\n        return 1;\n    }\n\n    return 0;\n}\n\nint redisGetReply(redisContext *c, void **reply) {\n    int wdone = 0;\n    void *aux = NULL;\n\n    /* Try to read pending replies */\n    if (redisGetReplyFromReader(c,&aux) == REDIS_ERR)\n        return REDIS_ERR;\n\n    /* For the blocking context, flush output buffer and read reply */\n    if (aux == NULL && c->flags & REDIS_BLOCK) {\n        /* Write until done */\n        do {\n            if (redisBufferWrite(c,&wdone) == REDIS_ERR)\n                return REDIS_ERR;\n        } while (!wdone);\n\n        /* Read until there is a reply */\n        do {\n            if (redisBufferRead(c) == REDIS_ERR)\n                return REDIS_ERR;\n\n            /* We loop here in case the user has specified a RESP3\n             * PUSH handler (e.g. for client tracking). */\n            do {\n                if (redisGetReplyFromReader(c,&aux) == REDIS_ERR)\n                    return REDIS_ERR;\n            } while (redisHandledPushReply(c, aux));\n        } while (aux == NULL);\n    }\n\n    /* Set reply or free it if we were passed NULL */\n    if (reply != NULL) {\n        *reply = aux;\n    } else {\n        freeReplyObject(aux);\n    }\n\n    return REDIS_OK;\n}\n\n\n/* Helper function for the redisAppendCommand* family of functions.\n *\n * Write a formatted command to the output buffer. When this family\n * is used, you need to call redisGetReply yourself to retrieve\n * the reply (or replies in pub/sub).\n */\nint __redisAppendCommand(redisContext *c, const char *cmd, size_t len) {\n    hisds newbuf;\n\n    newbuf = hi_sdscatlen(c->obuf,cmd,len);\n    if (newbuf == NULL) {\n        __redisSetError(c,REDIS_ERR_OOM,\"Out of memory\");\n        return REDIS_ERR;\n    }\n\n    c->obuf = newbuf;\n    return REDIS_OK;\n}\n\nint redisAppendFormattedCommand(redisContext *c, const char *cmd, size_t len) {\n\n    if (__redisAppendCommand(c, cmd, len) != REDIS_OK) {\n        return REDIS_ERR;\n    }\n\n    return REDIS_OK;\n}\n\nint redisvAppendCommand(redisContext *c, const char *format, va_list ap) {\n    char *cmd;\n    int len;\n\n    len = redisvFormatCommand(&cmd,format,ap);\n    if (len == -1) {\n        __redisSetError(c,REDIS_ERR_OOM,\"Out of memory\");\n        return REDIS_ERR;\n    } else if (len == -2) {\n        __redisSetError(c,REDIS_ERR_OTHER,\"Invalid format string\");\n        return REDIS_ERR;\n    }\n\n    if (__redisAppendCommand(c,cmd,len) != REDIS_OK) {\n        hi_free(cmd);\n        return REDIS_ERR;\n    }\n\n    hi_free(cmd);\n    return REDIS_OK;\n}\n\nint redisAppendCommand(redisContext *c, const char *format, ...) {\n    va_list ap;\n    int ret;\n\n    va_start(ap,format);\n    ret = redisvAppendCommand(c,format,ap);\n    va_end(ap);\n    return ret;\n}\n\nint redisAppendCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen) {\n    hisds cmd;\n    int len;\n\n    len = redisFormatSdsCommandArgv(&cmd,argc,argv,argvlen);\n    if (len == -1) {\n        __redisSetError(c,REDIS_ERR_OOM,\"Out of memory\");\n        return REDIS_ERR;\n    }\n\n    if (__redisAppendCommand(c,cmd,len) != REDIS_OK) {\n        hi_sdsfree(cmd);\n        return REDIS_ERR;\n    }\n\n    hi_sdsfree(cmd);\n    return REDIS_OK;\n}\n\n/* Helper function for the redisCommand* family of functions.\n *\n * Write a formatted command to the output buffer. If the given context is\n * blocking, immediately read the reply into the \"reply\" pointer. When the\n * context is non-blocking, the \"reply\" pointer will not be used and the\n * command is simply appended to the write buffer.\n *\n * Returns the reply when a reply was successfully retrieved. Returns NULL\n * otherwise. When NULL is returned in a blocking context, the error field\n * in the context will be set.\n */\nstatic void *__redisBlockForReply(redisContext *c) {\n    void *reply;\n\n    if (c->flags & REDIS_BLOCK) {\n        if (redisGetReply(c,&reply) != REDIS_OK)\n            return NULL;\n        return reply;\n    }\n    return NULL;\n}\n\nvoid *redisvCommand(redisContext *c, const char *format, va_list ap) {\n    if (redisvAppendCommand(c,format,ap) != REDIS_OK)\n        return NULL;\n    return __redisBlockForReply(c);\n}\n\nvoid *redisCommand(redisContext *c, const char *format, ...) {\n    va_list ap;\n    va_start(ap,format);\n    void *reply = redisvCommand(c,format,ap);\n    va_end(ap);\n    return reply;\n}\n\nvoid *redisCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen) {\n    if (redisAppendCommandArgv(c,argc,argv,argvlen) != REDIS_OK)\n        return NULL;\n    return __redisBlockForReply(c);\n}\n"
  },
  {
    "path": "deps/hiredis/hiredis.h",
    "content": "/*\n * Copyright (c) 2009-2011, Salvatore Sanfilippo <antirez at gmail dot com>\n * Copyright (c) 2010-2014, Pieter Noordhuis <pcnoordhuis at gmail dot com>\n * Copyright (c) 2015, Matt Stancliff <matt at genges dot com>,\n *                     Jan-Erik Rediger <janerik at fnordig dot com>\n *\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __HIREDIS_H\n#define __HIREDIS_H\n#include \"read.h\"\n#include <stdarg.h> /* for va_list */\n#ifndef _MSC_VER\n#include <sys/time.h> /* for struct timeval */\n#else\nstruct timeval; /* forward declaration */\ntypedef long long ssize_t;\n#endif\n#include <stdint.h> /* uintXX_t, etc */\n#include \"sds.h\" /* for hisds */\n#include \"alloc.h\" /* for allocation wrappers */\n\n#define HIREDIS_MAJOR 1\n#define HIREDIS_MINOR 0\n#define HIREDIS_PATCH 0\n#define HIREDIS_SONAME 1.0.0\n\n/* Connection type can be blocking or non-blocking and is set in the\n * least significant bit of the flags field in redisContext. */\n#define REDIS_BLOCK 0x1\n\n/* Connection may be disconnected before being free'd. The second bit\n * in the flags field is set when the context is connected. */\n#define REDIS_CONNECTED 0x2\n\n/* The async API might try to disconnect cleanly and flush the output\n * buffer and read all subsequent replies before disconnecting.\n * This flag means no new commands can come in and the connection\n * should be terminated once all replies have been read. */\n#define REDIS_DISCONNECTING 0x4\n\n/* Flag specific to the async API which means that the context should be clean\n * up as soon as possible. */\n#define REDIS_FREEING 0x8\n\n/* Flag that is set when an async callback is executed. */\n#define REDIS_IN_CALLBACK 0x10\n\n/* Flag that is set when the async context has one or more subscriptions. */\n#define REDIS_SUBSCRIBED 0x20\n\n/* Flag that is set when monitor mode is active */\n#define REDIS_MONITORING 0x40\n\n/* Flag that is set when we should set SO_REUSEADDR before calling bind() */\n#define REDIS_REUSEADDR 0x80\n\n/**\n * Flag that indicates the user does not want the context to\n * be automatically freed upon error\n */\n#define REDIS_NO_AUTO_FREE 0x200\n\n#define REDIS_KEEPALIVE_INTERVAL 15 /* seconds */\n\n/* number of times we retry to connect in the case of EADDRNOTAVAIL and\n * SO_REUSEADDR is being used. */\n#define REDIS_CONNECT_RETRIES  10\n\n/* Forward declarations for structs defined elsewhere */\nstruct redisAsyncContext;\nstruct redisContext;\n\n/* RESP3 push helpers and callback prototypes */\n#define redisIsPushReply(r) (((redisReply*)(r))->type == REDIS_REPLY_PUSH)\ntypedef void (redisPushFn)(void *, void *);\ntypedef void (redisAsyncPushFn)(struct redisAsyncContext *, void *);\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/* This is the reply object returned by redisCommand() */\ntypedef struct redisReply {\n    int type; /* REDIS_REPLY_* */\n    long long integer; /* The integer when type is REDIS_REPLY_INTEGER */\n    double dval; /* The double when type is REDIS_REPLY_DOUBLE */\n    size_t len; /* Length of string */\n    char *str; /* Used for REDIS_REPLY_ERROR, REDIS_REPLY_STRING\n                  REDIS_REPLY_VERB, and REDIS_REPLY_DOUBLE (in additional to dval). */\n    char vtype[4]; /* Used for REDIS_REPLY_VERB, contains the null\n                      terminated 3 character content type, such as \"txt\". */\n    size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */\n    struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */\n} redisReply;\n\nredisReader *redisReaderCreate(void);\n\n/* Function to free the reply objects hiredis returns by default. */\nvoid freeReplyObject(void *reply);\n\n/* Functions to format a command according to the protocol. */\nint redisvFormatCommand(char **target, const char *format, va_list ap);\nint redisFormatCommand(char **target, const char *format, ...);\nint redisFormatCommandArgv(char **target, int argc, const char **argv, const size_t *argvlen);\nint redisFormatSdsCommandArgv(hisds *target, int argc, const char ** argv, const size_t *argvlen);\nvoid redisFreeCommand(char *cmd);\nvoid redisFreeSdsCommand(hisds cmd);\n\nenum redisConnectionType {\n    REDIS_CONN_TCP,\n    REDIS_CONN_UNIX,\n    REDIS_CONN_USERFD\n};\n\nstruct redisSsl;\n\n#define REDIS_OPT_NONBLOCK 0x01\n#define REDIS_OPT_REUSEADDR 0x02\n\n/**\n * Don't automatically free the async object on a connection failure,\n * or other implicit conditions. Only free on an explicit call to disconnect() or free()\n */\n#define REDIS_OPT_NOAUTOFREE 0x04\n\n/* Don't automatically intercept and free RESP3 PUSH replies. */\n#define REDIS_OPT_NO_PUSH_AUTOFREE 0x08\n\n/* In Unix systems a file descriptor is a regular signed int, with -1\n * representing an invalid descriptor. In Windows it is a SOCKET\n * (32- or 64-bit unsigned integer depending on the architecture), where\n * all bits set (~0) is INVALID_SOCKET.  */\n#ifndef _WIN32\ntypedef int redisFD;\n#define REDIS_INVALID_FD -1\n#else\n#ifdef _WIN64\ntypedef unsigned long long redisFD; /* SOCKET = 64-bit UINT_PTR */\n#else\ntypedef unsigned long redisFD;      /* SOCKET = 32-bit UINT_PTR */\n#endif\n#define REDIS_INVALID_FD ((redisFD)(~0)) /* INVALID_SOCKET */\n#endif\n\ntypedef struct {\n    /*\n     * the type of connection to use. This also indicates which\n     * `endpoint` member field to use\n     */\n    int type;\n    /* bit field of REDIS_OPT_xxx */\n    int options;\n    /* timeout value for connect operation. If NULL, no timeout is used */\n    const struct timeval *connect_timeout;\n    /* timeout value for commands. If NULL, no timeout is used.  This can be\n     * updated at runtime with redisSetTimeout/redisAsyncSetTimeout. */\n    const struct timeval *command_timeout;\n    union {\n        /** use this field for tcp/ip connections */\n        struct {\n            const char *source_addr;\n            const char *ip;\n            int port;\n        } tcp;\n        /** use this field for unix domain sockets */\n        const char *unix_socket;\n        /**\n         * use this field to have hiredis operate an already-open\n         * file descriptor */\n        redisFD fd;\n    } endpoint;\n\n    /* Optional user defined data/destructor */\n    void *privdata;\n    void (*free_privdata)(void *);\n\n    /* A user defined PUSH message callback */\n    redisPushFn *push_cb;\n    redisAsyncPushFn *async_push_cb;\n} redisOptions;\n\n/**\n * Helper macros to initialize options to their specified fields.\n */\n#define REDIS_OPTIONS_SET_TCP(opts, ip_, port_) \\\n    (opts)->type = REDIS_CONN_TCP; \\\n    (opts)->endpoint.tcp.ip = ip_; \\\n    (opts)->endpoint.tcp.port = port_;\n\n#define REDIS_OPTIONS_SET_UNIX(opts, path) \\\n    (opts)->type = REDIS_CONN_UNIX;        \\\n    (opts)->endpoint.unix_socket = path;\n\n#define REDIS_OPTIONS_SET_PRIVDATA(opts, data, dtor) \\\n    (opts)->privdata = data;                         \\\n    (opts)->free_privdata = dtor;                    \\\n\ntypedef struct redisContextFuncs {\n    void (*free_privctx)(void *);\n    void (*async_read)(struct redisAsyncContext *);\n    void (*async_write)(struct redisAsyncContext *);\n    ssize_t (*read)(struct redisContext *, char *, size_t);\n    ssize_t (*write)(struct redisContext *);\n} redisContextFuncs;\n\n/* Context for a connection to Redis */\ntypedef struct redisContext {\n    const redisContextFuncs *funcs;   /* Function table */\n\n    int err; /* Error flags, 0 when there is no error */\n    char errstr[128]; /* String representation of error when applicable */\n    redisFD fd;\n    int flags;\n    char *obuf; /* Write buffer */\n    redisReader *reader; /* Protocol reader */\n\n    enum redisConnectionType connection_type;\n    struct timeval *connect_timeout;\n    struct timeval *command_timeout;\n\n    struct {\n        char *host;\n        char *source_addr;\n        int port;\n    } tcp;\n\n    struct {\n        char *path;\n    } unix_sock;\n\n    /* For non-blocking connect */\n    struct sockadr *saddr;\n    size_t addrlen;\n\n    /* Optional data and corresponding destructor users can use to provide\n     * context to a given redisContext.  Not used by hiredis. */\n    void *privdata;\n    void (*free_privdata)(void *);\n\n    /* Internal context pointer presently used by hiredis to manage\n     * SSL connections. */\n    void *privctx;\n\n    /* An optional RESP3 PUSH handler */\n    redisPushFn *push_cb;\n} redisContext;\n\nredisContext *redisConnectWithOptions(const redisOptions *options);\nredisContext *redisConnect(const char *ip, int port);\nredisContext *redisConnectWithTimeout(const char *ip, int port, const struct timeval tv);\nredisContext *redisConnectNonBlock(const char *ip, int port);\nredisContext *redisConnectBindNonBlock(const char *ip, int port,\n                                       const char *source_addr);\nredisContext *redisConnectBindNonBlockWithReuse(const char *ip, int port,\n                                                const char *source_addr);\nredisContext *redisConnectUnix(const char *path);\nredisContext *redisConnectUnixWithTimeout(const char *path, const struct timeval tv);\nredisContext *redisConnectUnixNonBlock(const char *path);\nredisContext *redisConnectFd(redisFD fd);\n\n/**\n * Reconnect the given context using the saved information.\n *\n * This re-uses the exact same connect options as in the initial connection.\n * host, ip (or path), timeout and bind address are reused,\n * flags are used unmodified from the existing context.\n *\n * Returns REDIS_OK on successful connect or REDIS_ERR otherwise.\n */\nint redisReconnect(redisContext *c);\n\nredisPushFn *redisSetPushCallback(redisContext *c, redisPushFn *fn);\nint redisSetTimeout(redisContext *c, const struct timeval tv);\nint redisEnableKeepAlive(redisContext *c);\nvoid redisFree(redisContext *c);\nredisFD redisFreeKeepFd(redisContext *c);\nint redisBufferRead(redisContext *c);\nint redisBufferWrite(redisContext *c, int *done);\n\n/* In a blocking context, this function first checks if there are unconsumed\n * replies to return and returns one if so. Otherwise, it flushes the output\n * buffer to the socket and reads until it has a reply. In a non-blocking\n * context, it will return unconsumed replies until there are no more. */\nint redisGetReply(redisContext *c, void **reply);\nint redisGetReplyFromReader(redisContext *c, void **reply);\n\n/* Write a formatted command to the output buffer. Use these functions in blocking mode\n * to get a pipeline of commands. */\nint redisAppendFormattedCommand(redisContext *c, const char *cmd, size_t len);\n\n/* Write a command to the output buffer. Use these functions in blocking mode\n * to get a pipeline of commands. */\nint redisvAppendCommand(redisContext *c, const char *format, va_list ap);\nint redisAppendCommand(redisContext *c, const char *format, ...);\nint redisAppendCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);\n\n/* Issue a command to Redis. In a blocking context, it is identical to calling\n * redisAppendCommand, followed by redisGetReply. The function will return\n * NULL if there was an error in performing the request, otherwise it will\n * return the reply. In a non-blocking context, it is identical to calling\n * only redisAppendCommand and will always return NULL. */\nvoid *redisvCommand(redisContext *c, const char *format, va_list ap);\nvoid *redisCommand(redisContext *c, const char *format, ...);\nvoid *redisCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n"
  },
  {
    "path": "deps/hiredis/hiredis.pc.in",
    "content": "prefix=@CMAKE_INSTALL_PREFIX@\ninstall_libdir=@CMAKE_INSTALL_LIBDIR@\nexec_prefix=${prefix}\nlibdir=${exec_prefix}/${install_libdir}\nincludedir=${prefix}/include\npkgincludedir=${includedir}/hiredis\n\nName: hiredis\nDescription: Minimalistic C client library for Redis.\nVersion: @PROJECT_VERSION@\nLibs: -L${libdir} -lhiredis\nCflags: -I${pkgincludedir} -D_FILE_OFFSET_BITS=64\n"
  },
  {
    "path": "deps/hiredis/hiredis_ssl-config.cmake.in",
    "content": "@PACKAGE_INIT@\n\nset_and_check(hiredis_ssl_INCLUDEDIR \"@PACKAGE_INCLUDE_INSTALL_DIR@\")\n\nIF (NOT TARGET hiredis::hiredis_ssl)\n\tINCLUDE(${CMAKE_CURRENT_LIST_DIR}/hiredis_ssl-targets.cmake)\nENDIF()\n\nSET(hiredis_ssl_LIBRARIES hiredis::hiredis_ssl)\nSET(hiredis_ssl_INCLUDE_DIRS ${hiredis_ssl_INCLUDEDIR})\n\ncheck_required_components(hiredis_ssl)\n\n"
  },
  {
    "path": "deps/hiredis/hiredis_ssl.h",
    "content": "\n/*\n * Copyright (c) 2019, Redis Labs\n *\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __HIREDIS_SSL_H\n#define __HIREDIS_SSL_H\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/* This is the underlying struct for SSL in ssl.h, which is not included to\n * keep build dependencies short here.\n */\nstruct ssl_st;\n\n/* A wrapper around OpenSSL SSL_CTX to allow easy SSL use without directly\n * calling OpenSSL.\n */\ntypedef struct redisSSLContext redisSSLContext;\n\n/**\n * Initialization errors that redisCreateSSLContext() may return.\n */\n\ntypedef enum {\n    REDIS_SSL_CTX_NONE = 0,                     /* No Error */\n    REDIS_SSL_CTX_CREATE_FAILED,                /* Failed to create OpenSSL SSL_CTX */\n    REDIS_SSL_CTX_CERT_KEY_REQUIRED,            /* Client cert and key must both be specified or skipped */\n    REDIS_SSL_CTX_CA_CERT_LOAD_FAILED,          /* Failed to load CA Certificate or CA Path */\n    REDIS_SSL_CTX_CLIENT_CERT_LOAD_FAILED,      /* Failed to load client certificate */\n    REDIS_SSL_CTX_PRIVATE_KEY_LOAD_FAILED       /* Failed to load private key */\n} redisSSLContextError;\n\n/**\n * Return the error message corresponding with the specified error code.\n */\n\nconst char *redisSSLContextGetError(redisSSLContextError error);\n\n/**\n * Helper function to initialize the OpenSSL library.\n *\n * OpenSSL requires one-time initialization before it can be used. Callers should\n * call this function only once, and only if OpenSSL is not directly initialized\n * elsewhere.\n */\nint redisInitOpenSSL(void);\n\n/**\n * Helper function to initialize an OpenSSL context that can be used\n * to initiate SSL connections.\n *\n * cacert_filename is an optional name of a CA certificate/bundle file to load\n * and use for validation.\n *\n * capath is an optional directory path where trusted CA certificate files are\n * stored in an OpenSSL-compatible structure.\n *\n * cert_filename and private_key_filename are optional names of a client side\n * certificate and private key files to use for authentication. They need to\n * be both specified or omitted.\n *\n * server_name is an optional and will be used as a server name indication\n * (SNI) TLS extension.\n *\n * If error is non-null, it will be populated in case the context creation fails\n * (returning a NULL).\n */\n\nredisSSLContext *redisCreateSSLContext(const char *cacert_filename, const char *capath,\n        const char *cert_filename, const char *private_key_filename,\n        const char *server_name, redisSSLContextError *error);\n\n/**\n * Free a previously created OpenSSL context.\n */\nvoid redisFreeSSLContext(redisSSLContext *redis_ssl_ctx);\n\n/**\n * Initiate SSL on an existing redisContext.\n *\n * This is similar to redisInitiateSSL() but does not require the caller\n * to directly interact with OpenSSL, and instead uses a redisSSLContext\n * previously created using redisCreateSSLContext().\n */\n\nint redisInitiateSSLWithContext(redisContext *c, redisSSLContext *redis_ssl_ctx);\n\n/**\n * Initiate SSL/TLS negotiation on a provided OpenSSL SSL object.\n */\n\nint redisInitiateSSL(redisContext *c, struct ssl_st *ssl);\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif  /* __HIREDIS_SSL_H */\n"
  },
  {
    "path": "deps/hiredis/hiredis_ssl.pc.in",
    "content": "prefix=@CMAKE_INSTALL_PREFIX@\nexec_prefix=${prefix}\nlibdir=${exec_prefix}/lib\nincludedir=${prefix}/include\npkgincludedir=${includedir}/hiredis\n\nName: hiredis_ssl\nDescription: SSL Support for hiredis.\nVersion: @PROJECT_VERSION@\nRequires: hiredis\nLibs: -L${libdir} -lhiredis_ssl\nLibs.private: -lssl -lcrypto\n"
  },
  {
    "path": "deps/hiredis/net.c",
    "content": "/* Extracted from anet.c to work properly with Hiredis error reporting.\n *\n * Copyright (c) 2009-2011, Salvatore Sanfilippo <antirez at gmail dot com>\n * Copyright (c) 2010-2014, Pieter Noordhuis <pcnoordhuis at gmail dot com>\n * Copyright (c) 2015, Matt Stancliff <matt at genges dot com>,\n *                     Jan-Erik Rediger <janerik at fnordig dot com>\n *\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"fmacros.h\"\n#include <sys/types.h>\n#include <fcntl.h>\n#include <string.h>\n#include <errno.h>\n#include <stdarg.h>\n#include <stdio.h>\n#include <limits.h>\n#include <stdlib.h>\n\n#include \"net.h\"\n#include \"sds.h\"\n#include \"sockcompat.h\"\n#include \"win32.h\"\n\n/* Defined in hiredis.c */\nvoid __redisSetError(redisContext *c, int type, const char *str);\n\nvoid redisNetClose(redisContext *c) {\n    if (c && c->fd != REDIS_INVALID_FD) {\n        close(c->fd);\n        c->fd = REDIS_INVALID_FD;\n    }\n}\n\nssize_t redisNetRead(redisContext *c, char *buf, size_t bufcap) {\n    ssize_t nread = recv(c->fd, buf, bufcap, 0);\n    if (nread == -1) {\n        if ((errno == EWOULDBLOCK && !(c->flags & REDIS_BLOCK)) || (errno == EINTR)) {\n            /* Try again later */\n            return 0;\n        } else if(errno == ETIMEDOUT && (c->flags & REDIS_BLOCK)) {\n            /* especially in windows */\n            __redisSetError(c, REDIS_ERR_TIMEOUT, \"recv timeout\");\n            return -1;\n        } else {\n            __redisSetError(c, REDIS_ERR_IO, NULL);\n            return -1;\n        }\n    } else if (nread == 0) {\n        __redisSetError(c, REDIS_ERR_EOF, \"Server closed the connection\");\n        return -1;\n    } else {\n        return nread;\n    }\n}\n\nssize_t redisNetWrite(redisContext *c) {\n    ssize_t nwritten = send(c->fd, c->obuf, hi_sdslen(c->obuf), 0);\n    if (nwritten < 0) {\n        if ((errno == EWOULDBLOCK && !(c->flags & REDIS_BLOCK)) || (errno == EINTR)) {\n            /* Try again later */\n        } else {\n            __redisSetError(c, REDIS_ERR_IO, NULL);\n            return -1;\n        }\n    }\n    return nwritten;\n}\n\nstatic void __redisSetErrorFromErrno(redisContext *c, int type, const char *prefix) {\n    int errorno = errno;  /* snprintf() may change errno */\n    char buf[128] = { 0 };\n    size_t len = 0;\n\n    if (prefix != NULL)\n        len = snprintf(buf,sizeof(buf),\"%s: \",prefix);\n    strerror_r(errorno, (char *)(buf + len), sizeof(buf) - len);\n    __redisSetError(c,type,buf);\n}\n\nstatic int redisSetReuseAddr(redisContext *c) {\n    int on = 1;\n    if (setsockopt(c->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {\n        __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);\n        redisNetClose(c);\n        return REDIS_ERR;\n    }\n    return REDIS_OK;\n}\n\nstatic int redisCreateSocket(redisContext *c, int type) {\n    redisFD s;\n    if ((s = socket(type, SOCK_STREAM, 0)) == REDIS_INVALID_FD) {\n        __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);\n        return REDIS_ERR;\n    }\n    c->fd = s;\n    if (type == AF_INET) {\n        if (redisSetReuseAddr(c) == REDIS_ERR) {\n            return REDIS_ERR;\n        }\n    }\n    return REDIS_OK;\n}\n\nstatic int redisSetBlocking(redisContext *c, int blocking) {\n#ifndef _WIN32\n    int flags;\n\n    /* Set the socket nonblocking.\n     * Note that fcntl(2) for F_GETFL and F_SETFL can't be\n     * interrupted by a signal. */\n    if ((flags = fcntl(c->fd, F_GETFL)) == -1) {\n        __redisSetErrorFromErrno(c,REDIS_ERR_IO,\"fcntl(F_GETFL)\");\n        redisNetClose(c);\n        return REDIS_ERR;\n    }\n\n    if (blocking)\n        flags &= ~O_NONBLOCK;\n    else\n        flags |= O_NONBLOCK;\n\n    if (fcntl(c->fd, F_SETFL, flags) == -1) {\n        __redisSetErrorFromErrno(c,REDIS_ERR_IO,\"fcntl(F_SETFL)\");\n        redisNetClose(c);\n        return REDIS_ERR;\n    }\n#else\n    u_long mode = blocking ? 0 : 1;\n    if (ioctl(c->fd, FIONBIO, &mode) == -1) {\n        __redisSetErrorFromErrno(c, REDIS_ERR_IO, \"ioctl(FIONBIO)\");\n        redisNetClose(c);\n        return REDIS_ERR;\n    }\n#endif /* _WIN32 */\n    return REDIS_OK;\n}\n\nint redisKeepAlive(redisContext *c, int interval) {\n    int val = 1;\n    redisFD fd = c->fd;\n\n    if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) == -1){\n        __redisSetError(c,REDIS_ERR_OTHER,strerror(errno));\n        return REDIS_ERR;\n    }\n\n    val = interval;\n\n#if defined(__APPLE__) && defined(__MACH__)\n    if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &val, sizeof(val)) < 0) {\n        __redisSetError(c,REDIS_ERR_OTHER,strerror(errno));\n        return REDIS_ERR;\n    }\n#else\n#if defined(__GLIBC__) && !defined(__FreeBSD_kernel__)\n    if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof(val)) < 0) {\n        __redisSetError(c,REDIS_ERR_OTHER,strerror(errno));\n        return REDIS_ERR;\n    }\n\n    val = interval/3;\n    if (val == 0) val = 1;\n    if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof(val)) < 0) {\n        __redisSetError(c,REDIS_ERR_OTHER,strerror(errno));\n        return REDIS_ERR;\n    }\n\n    val = 3;\n    if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof(val)) < 0) {\n        __redisSetError(c,REDIS_ERR_OTHER,strerror(errno));\n        return REDIS_ERR;\n    }\n#endif\n#endif\n\n    return REDIS_OK;\n}\n\nint redisSetTcpNoDelay(redisContext *c) {\n    int yes = 1;\n    if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) == -1) {\n        __redisSetErrorFromErrno(c,REDIS_ERR_IO,\"setsockopt(TCP_NODELAY)\");\n        redisNetClose(c);\n        return REDIS_ERR;\n    }\n    return REDIS_OK;\n}\n\n#define __MAX_MSEC (((LONG_MAX) - 999) / 1000)\n\nstatic int redisContextTimeoutMsec(redisContext *c, long *result)\n{\n    const struct timeval *timeout = c->connect_timeout;\n    long msec = -1;\n\n    /* Only use timeout when not NULL. */\n    if (timeout != NULL) {\n        if (timeout->tv_usec > 1000000 || timeout->tv_sec > __MAX_MSEC) {\n            *result = msec;\n            return REDIS_ERR;\n        }\n\n        msec = (timeout->tv_sec * 1000) + ((timeout->tv_usec + 999) / 1000);\n\n        if (msec < 0 || msec > INT_MAX) {\n            msec = INT_MAX;\n        }\n    }\n\n    *result = msec;\n    return REDIS_OK;\n}\n\nstatic int redisContextWaitReady(redisContext *c, long msec) {\n    struct pollfd   wfd[1];\n\n    wfd[0].fd     = c->fd;\n    wfd[0].events = POLLOUT;\n\n    if (errno == EINPROGRESS) {\n        int res;\n\n        if ((res = poll(wfd, 1, msec)) == -1) {\n            __redisSetErrorFromErrno(c, REDIS_ERR_IO, \"poll(2)\");\n            redisNetClose(c);\n            return REDIS_ERR;\n        } else if (res == 0) {\n            errno = ETIMEDOUT;\n            __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);\n            redisNetClose(c);\n            return REDIS_ERR;\n        }\n\n        if (redisCheckConnectDone(c, &res) != REDIS_OK || res == 0) {\n            redisCheckSocketError(c);\n            return REDIS_ERR;\n        }\n\n        return REDIS_OK;\n    }\n\n    __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);\n    redisNetClose(c);\n    return REDIS_ERR;\n}\n\nint redisCheckConnectDone(redisContext *c, int *completed) {\n    int rc = connect(c->fd, (const struct sockaddr *)c->saddr, c->addrlen);\n    if (rc == 0) {\n        *completed = 1;\n        return REDIS_OK;\n    }\n    switch (errno) {\n    case EISCONN:\n        *completed = 1;\n        return REDIS_OK;\n    case EALREADY:\n    case EINPROGRESS:\n    case EWOULDBLOCK:\n        *completed = 0;\n        return REDIS_OK;\n    default:\n        return REDIS_ERR;\n    }\n}\n\nint redisCheckSocketError(redisContext *c) {\n    int err = 0, errno_saved = errno;\n    socklen_t errlen = sizeof(err);\n\n    if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) {\n        __redisSetErrorFromErrno(c,REDIS_ERR_IO,\"getsockopt(SO_ERROR)\");\n        return REDIS_ERR;\n    }\n\n    if (err == 0) {\n        err = errno_saved;\n    }\n\n    if (err) {\n        errno = err;\n        __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);\n        return REDIS_ERR;\n    }\n\n    return REDIS_OK;\n}\n\nint redisContextSetTimeout(redisContext *c, const struct timeval tv) {\n    const void *to_ptr = &tv;\n    size_t to_sz = sizeof(tv);\n\n    if (setsockopt(c->fd,SOL_SOCKET,SO_RCVTIMEO,to_ptr,to_sz) == -1) {\n        __redisSetErrorFromErrno(c,REDIS_ERR_IO,\"setsockopt(SO_RCVTIMEO)\");\n        return REDIS_ERR;\n    }\n    if (setsockopt(c->fd,SOL_SOCKET,SO_SNDTIMEO,to_ptr,to_sz) == -1) {\n        __redisSetErrorFromErrno(c,REDIS_ERR_IO,\"setsockopt(SO_SNDTIMEO)\");\n        return REDIS_ERR;\n    }\n    return REDIS_OK;\n}\n\nint redisContextUpdateConnectTimeout(redisContext *c, const struct timeval *timeout) {\n    /* Same timeval struct, short circuit */\n    if (c->connect_timeout == timeout)\n        return REDIS_OK;\n\n    /* Allocate context timeval if we need to */\n    if (c->connect_timeout == NULL) {\n        c->connect_timeout = hi_malloc(sizeof(*c->connect_timeout));\n        if (c->connect_timeout == NULL)\n            return REDIS_ERR;\n    }\n\n    memcpy(c->connect_timeout, timeout, sizeof(*c->connect_timeout));\n    return REDIS_OK;\n}\n\nint redisContextUpdateCommandTimeout(redisContext *c, const struct timeval *timeout) {\n    /* Same timeval struct, short circuit */\n    if (c->command_timeout == timeout)\n        return REDIS_OK;\n\n    /* Allocate context timeval if we need to */\n    if (c->command_timeout == NULL) {\n        c->command_timeout = hi_malloc(sizeof(*c->command_timeout));\n        if (c->command_timeout == NULL)\n            return REDIS_ERR;\n    }\n\n    memcpy(c->command_timeout, timeout, sizeof(*c->command_timeout));\n    return REDIS_OK;\n}\n\nstatic int _redisContextConnectTcp(redisContext *c, const char *addr, int port,\n                                   const struct timeval *timeout,\n                                   const char *source_addr) {\n    redisFD s;\n    int rv, n;\n    char _port[6];  /* strlen(\"65535\"); */\n    struct addrinfo hints, *servinfo, *bservinfo, *p, *b;\n    int blocking = (c->flags & REDIS_BLOCK);\n    int reuseaddr = (c->flags & REDIS_REUSEADDR);\n    int reuses = 0;\n    long timeout_msec = -1;\n\n    servinfo = NULL;\n    c->connection_type = REDIS_CONN_TCP;\n    c->tcp.port = port;\n\n    /* We need to take possession of the passed parameters\n     * to make them reusable for a reconnect.\n     * We also carefully check we don't free data we already own,\n     * as in the case of the reconnect method.\n     *\n     * This is a bit ugly, but atleast it works and doesn't leak memory.\n     **/\n    if (c->tcp.host != addr) {\n        hi_free(c->tcp.host);\n\n        c->tcp.host = hi_strdup(addr);\n        if (c->tcp.host == NULL)\n            goto oom;\n    }\n\n    if (timeout) {\n        if (redisContextUpdateConnectTimeout(c, timeout) == REDIS_ERR)\n            goto oom;\n    } else {\n        hi_free(c->connect_timeout);\n        c->connect_timeout = NULL;\n    }\n\n    if (redisContextTimeoutMsec(c, &timeout_msec) != REDIS_OK) {\n        __redisSetError(c, REDIS_ERR_IO, \"Invalid timeout specified\");\n        goto error;\n    }\n\n    if (source_addr == NULL) {\n        hi_free(c->tcp.source_addr);\n        c->tcp.source_addr = NULL;\n    } else if (c->tcp.source_addr != source_addr) {\n        hi_free(c->tcp.source_addr);\n        c->tcp.source_addr = hi_strdup(source_addr);\n    }\n\n    snprintf(_port, 6, \"%d\", port);\n    memset(&hints,0,sizeof(hints));\n    hints.ai_family = AF_INET;\n    hints.ai_socktype = SOCK_STREAM;\n\n    /* Try with IPv6 if no IPv4 address was found. We do it in this order since\n     * in a Redis client you can't afford to test if you have IPv6 connectivity\n     * as this would add latency to every connect. Otherwise a more sensible\n     * route could be: Use IPv6 if both addresses are available and there is IPv6\n     * connectivity. */\n    if ((rv = getaddrinfo(c->tcp.host,_port,&hints,&servinfo)) != 0) {\n         hints.ai_family = AF_INET6;\n         if ((rv = getaddrinfo(addr,_port,&hints,&servinfo)) != 0) {\n            __redisSetError(c,REDIS_ERR_OTHER,gai_strerror(rv));\n            return REDIS_ERR;\n        }\n    }\n    for (p = servinfo; p != NULL; p = p->ai_next) {\naddrretry:\n        if ((s = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == REDIS_INVALID_FD)\n            continue;\n\n        c->fd = s;\n        if (redisSetBlocking(c,0) != REDIS_OK)\n            goto error;\n        if (c->tcp.source_addr) {\n            int bound = 0;\n            /* Using getaddrinfo saves us from self-determining IPv4 vs IPv6 */\n            if ((rv = getaddrinfo(c->tcp.source_addr, NULL, &hints, &bservinfo)) != 0) {\n                char buf[128];\n                snprintf(buf,sizeof(buf),\"Can't get addr: %s\",gai_strerror(rv));\n                __redisSetError(c,REDIS_ERR_OTHER,buf);\n                goto error;\n            }\n\n            if (reuseaddr) {\n                n = 1;\n                if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*) &n,\n                               sizeof(n)) < 0) {\n                    freeaddrinfo(bservinfo);\n                    goto error;\n                }\n            }\n\n            for (b = bservinfo; b != NULL; b = b->ai_next) {\n                if (bind(s,b->ai_addr,b->ai_addrlen) != -1) {\n                    bound = 1;\n                    break;\n                }\n            }\n            freeaddrinfo(bservinfo);\n            if (!bound) {\n                char buf[128];\n                snprintf(buf,sizeof(buf),\"Can't bind socket: %s\",strerror(errno));\n                __redisSetError(c,REDIS_ERR_OTHER,buf);\n                goto error;\n            }\n        }\n\n        /* For repeat connection */\n        hi_free(c->saddr);\n        c->saddr = hi_malloc(p->ai_addrlen);\n        if (c->saddr == NULL)\n            goto oom;\n\n        memcpy(c->saddr, p->ai_addr, p->ai_addrlen);\n        c->addrlen = p->ai_addrlen;\n\n        if (connect(s,p->ai_addr,p->ai_addrlen) == -1) {\n            if (errno == EHOSTUNREACH) {\n                redisNetClose(c);\n                continue;\n            } else if (errno == EINPROGRESS) {\n                if (blocking) {\n                    goto wait_for_ready;\n                }\n                /* This is ok.\n                 * Note that even when it's in blocking mode, we unset blocking\n                 * for `connect()`\n                 */\n            } else if (errno == EADDRNOTAVAIL && reuseaddr) {\n                if (++reuses >= REDIS_CONNECT_RETRIES) {\n                    goto error;\n                } else {\n                    redisNetClose(c);\n                    goto addrretry;\n                }\n            } else {\n                wait_for_ready:\n                if (redisContextWaitReady(c,timeout_msec) != REDIS_OK)\n                    goto error;\n                if (redisSetTcpNoDelay(c) != REDIS_OK)\n                    goto error;\n            }\n        }\n        if (blocking && redisSetBlocking(c,1) != REDIS_OK)\n            goto error;\n\n        c->flags |= REDIS_CONNECTED;\n        rv = REDIS_OK;\n        goto end;\n    }\n    if (p == NULL) {\n        char buf[128];\n        snprintf(buf,sizeof(buf),\"Can't create socket: %s\",strerror(errno));\n        __redisSetError(c,REDIS_ERR_OTHER,buf);\n        goto error;\n    }\n\noom:\n    __redisSetError(c, REDIS_ERR_OOM, \"Out of memory\");\nerror:\n    rv = REDIS_ERR;\nend:\n    if(servinfo) {\n        freeaddrinfo(servinfo);\n    }\n\n    return rv;  // Need to return REDIS_OK if alright\n}\n\nint redisContextConnectTcp(redisContext *c, const char *addr, int port,\n                           const struct timeval *timeout) {\n    return _redisContextConnectTcp(c, addr, port, timeout, NULL);\n}\n\nint redisContextConnectBindTcp(redisContext *c, const char *addr, int port,\n                               const struct timeval *timeout,\n                               const char *source_addr) {\n    return _redisContextConnectTcp(c, addr, port, timeout, source_addr);\n}\n\nint redisContextConnectUnix(redisContext *c, const char *path, const struct timeval *timeout) {\n#ifndef _WIN32\n    int blocking = (c->flags & REDIS_BLOCK);\n    struct sockaddr_un *sa;\n    long timeout_msec = -1;\n\n    if (redisCreateSocket(c,AF_UNIX) < 0)\n        return REDIS_ERR;\n    if (redisSetBlocking(c,0) != REDIS_OK)\n        return REDIS_ERR;\n\n    c->connection_type = REDIS_CONN_UNIX;\n    if (c->unix_sock.path != path) {\n        hi_free(c->unix_sock.path);\n\n        c->unix_sock.path = hi_strdup(path);\n        if (c->unix_sock.path == NULL)\n            goto oom;\n    }\n\n    if (timeout) {\n        if (redisContextUpdateConnectTimeout(c, timeout) == REDIS_ERR)\n            goto oom;\n    } else {\n        hi_free(c->connect_timeout);\n        c->connect_timeout = NULL;\n    }\n\n    if (redisContextTimeoutMsec(c,&timeout_msec) != REDIS_OK)\n        return REDIS_ERR;\n\n    /* Don't leak sockaddr if we're reconnecting */\n    if (c->saddr) hi_free(c->saddr);\n\n    sa = (struct sockaddr_un*)(c->saddr = hi_malloc(sizeof(struct sockaddr_un)));\n    if (sa == NULL)\n        goto oom;\n\n    c->addrlen = sizeof(struct sockaddr_un);\n    sa->sun_family = AF_UNIX;\n    strncpy(sa->sun_path, path, sizeof(sa->sun_path) - 1);\n    if (connect(c->fd, (struct sockaddr*)sa, sizeof(*sa)) == -1) {\n        if (errno == EINPROGRESS && !blocking) {\n            /* This is ok. */\n        } else {\n            if (redisContextWaitReady(c,timeout_msec) != REDIS_OK)\n                return REDIS_ERR;\n        }\n    }\n\n    /* Reset socket to be blocking after connect(2). */\n    if (blocking && redisSetBlocking(c,1) != REDIS_OK)\n        return REDIS_ERR;\n\n    c->flags |= REDIS_CONNECTED;\n    return REDIS_OK;\n#else\n    /* We currently do not support Unix sockets for Windows. */\n    /* TODO(m): https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/ */\n    errno = EPROTONOSUPPORT;\n    return REDIS_ERR;\n#endif /* _WIN32 */\noom:\n    __redisSetError(c, REDIS_ERR_OOM, \"Out of memory\");\n    return REDIS_ERR;\n}\n"
  },
  {
    "path": "deps/hiredis/net.h",
    "content": "/* Extracted from anet.c to work properly with Hiredis error reporting.\n *\n * Copyright (c) 2009-2011, Salvatore Sanfilippo <antirez at gmail dot com>\n * Copyright (c) 2010-2014, Pieter Noordhuis <pcnoordhuis at gmail dot com>\n * Copyright (c) 2015, Matt Stancliff <matt at genges dot com>,\n *                     Jan-Erik Rediger <janerik at fnordig dot com>\n *\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __NET_H\n#define __NET_H\n\n#include \"hiredis.h\"\n\nvoid redisNetClose(redisContext *c);\nssize_t redisNetRead(redisContext *c, char *buf, size_t bufcap);\nssize_t redisNetWrite(redisContext *c);\n\nint redisCheckSocketError(redisContext *c);\nint redisContextSetTimeout(redisContext *c, const struct timeval tv);\nint redisContextConnectTcp(redisContext *c, const char *addr, int port, const struct timeval *timeout);\nint redisContextConnectBindTcp(redisContext *c, const char *addr, int port,\n                               const struct timeval *timeout,\n                               const char *source_addr);\nint redisContextConnectUnix(redisContext *c, const char *path, const struct timeval *timeout);\nint redisKeepAlive(redisContext *c, int interval);\nint redisCheckConnectDone(redisContext *c, int *completed);\n\nint redisSetTcpNoDelay(redisContext *c);\n\n#endif\n"
  },
  {
    "path": "deps/hiredis/read.c",
    "content": "/*\n * Copyright (c) 2009-2011, Salvatore Sanfilippo <antirez at gmail dot com>\n * Copyright (c) 2010-2011, Pieter Noordhuis <pcnoordhuis at gmail dot com>\n *\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"fmacros.h\"\n#include <string.h>\n#include <stdlib.h>\n#ifndef _MSC_VER\n#include <unistd.h>\n#include <strings.h>\n#endif\n#include <assert.h>\n#include <errno.h>\n#include <ctype.h>\n#include <limits.h>\n#include <math.h>\n\n#include \"alloc.h\"\n#include \"read.h\"\n#include \"sds.h\"\n#include \"win32.h\"\n\n/* Initial size of our nested reply stack and how much we grow it when needd */\n#define REDIS_READER_STACK_SIZE 9\n\nstatic void __redisReaderSetError(redisReader *r, int type, const char *str) {\n    size_t len;\n\n    if (r->reply != NULL && r->fn && r->fn->freeObject) {\n        r->fn->freeObject(r->reply);\n        r->reply = NULL;\n    }\n\n    /* Clear input buffer on errors. */\n    hi_sdsfree(r->buf);\n    r->buf = NULL;\n    r->pos = r->len = 0;\n\n    /* Reset task stack. */\n    r->ridx = -1;\n\n    /* Set error. */\n    r->err = type;\n    len = strlen(str);\n    len = len < (sizeof(r->errstr)-1) ? len : (sizeof(r->errstr)-1);\n    memcpy(r->errstr,str,len);\n    r->errstr[len] = '\\0';\n}\n\nstatic size_t chrtos(char *buf, size_t size, char byte) {\n    size_t len = 0;\n\n    switch(byte) {\n    case '\\\\':\n    case '\"':\n        len = snprintf(buf,size,\"\\\"\\\\%c\\\"\",byte);\n        break;\n    case '\\n': len = snprintf(buf,size,\"\\\"\\\\n\\\"\"); break;\n    case '\\r': len = snprintf(buf,size,\"\\\"\\\\r\\\"\"); break;\n    case '\\t': len = snprintf(buf,size,\"\\\"\\\\t\\\"\"); break;\n    case '\\a': len = snprintf(buf,size,\"\\\"\\\\a\\\"\"); break;\n    case '\\b': len = snprintf(buf,size,\"\\\"\\\\b\\\"\"); break;\n    default:\n        if (isprint(byte))\n            len = snprintf(buf,size,\"\\\"%c\\\"\",byte);\n        else\n            len = snprintf(buf,size,\"\\\"\\\\x%02x\\\"\",(unsigned char)byte);\n        break;\n    }\n\n    return len;\n}\n\nstatic void __redisReaderSetErrorProtocolByte(redisReader *r, char byte) {\n    char cbuf[8], sbuf[128];\n\n    chrtos(cbuf,sizeof(cbuf),byte);\n    snprintf(sbuf,sizeof(sbuf),\n        \"Protocol error, got %s as reply type byte\", cbuf);\n    __redisReaderSetError(r,REDIS_ERR_PROTOCOL,sbuf);\n}\n\nstatic void __redisReaderSetErrorOOM(redisReader *r) {\n    __redisReaderSetError(r,REDIS_ERR_OOM,\"Out of memory\");\n}\n\nstatic char *readBytes(redisReader *r, unsigned int bytes) {\n    char *p;\n    if (r->len-r->pos >= bytes) {\n        p = r->buf+r->pos;\n        r->pos += bytes;\n        return p;\n    }\n    return NULL;\n}\n\n/* Find pointer to \\r\\n. */\nstatic char *seekNewline(char *s, size_t len) {\n    int pos = 0;\n    int _len = len-1;\n\n    /* Position should be < len-1 because the character at \"pos\" should be\n     * followed by a \\n. Note that strchr cannot be used because it doesn't\n     * allow to search a limited length and the buffer that is being searched\n     * might not have a trailing NULL character. */\n    while (pos < _len) {\n        while(pos < _len && s[pos] != '\\r') pos++;\n        if (pos==_len) {\n            /* Not found. */\n            return NULL;\n        } else {\n            if (s[pos+1] == '\\n') {\n                /* Found. */\n                return s+pos;\n            } else {\n                /* Continue searching. */\n                pos++;\n            }\n        }\n    }\n    return NULL;\n}\n\n/* Convert a string into a long long. Returns REDIS_OK if the string could be\n * parsed into a (non-overflowing) long long, REDIS_ERR otherwise. The value\n * will be set to the parsed value when appropriate.\n *\n * Note that this function demands that the string strictly represents\n * a long long: no spaces or other characters before or after the string\n * representing the number are accepted, nor zeroes at the start if not\n * for the string \"0\" representing the zero number.\n *\n * Because of its strictness, it is safe to use this function to check if\n * you can convert a string into a long long, and obtain back the string\n * from the number without any loss in the string representation. */\nstatic int string2ll(const char *s, size_t slen, long long *value) {\n    const char *p = s;\n    size_t plen = 0;\n    int negative = 0;\n    unsigned long long v;\n\n    if (plen == slen)\n        return REDIS_ERR;\n\n    /* Special case: first and only digit is 0. */\n    if (slen == 1 && p[0] == '0') {\n        if (value != NULL) *value = 0;\n        return REDIS_OK;\n    }\n\n    if (p[0] == '-') {\n        negative = 1;\n        p++; plen++;\n\n        /* Abort on only a negative sign. */\n        if (plen == slen)\n            return REDIS_ERR;\n    }\n\n    /* First digit should be 1-9, otherwise the string should just be 0. */\n    if (p[0] >= '1' && p[0] <= '9') {\n        v = p[0]-'0';\n        p++; plen++;\n    } else if (p[0] == '0' && slen == 1) {\n        *value = 0;\n        return REDIS_OK;\n    } else {\n        return REDIS_ERR;\n    }\n\n    while (plen < slen && p[0] >= '0' && p[0] <= '9') {\n        if (v > (ULLONG_MAX / 10)) /* Overflow. */\n            return REDIS_ERR;\n        v *= 10;\n\n        if (v > (ULLONG_MAX - (p[0]-'0'))) /* Overflow. */\n            return REDIS_ERR;\n        v += p[0]-'0';\n\n        p++; plen++;\n    }\n\n    /* Return if not all bytes were used. */\n    if (plen < slen)\n        return REDIS_ERR;\n\n    if (negative) {\n        if (v > ((unsigned long long)(-(LLONG_MIN+1))+1)) /* Overflow. */\n            return REDIS_ERR;\n        if (value != NULL) *value = -v;\n    } else {\n        if (v > LLONG_MAX) /* Overflow. */\n            return REDIS_ERR;\n        if (value != NULL) *value = v;\n    }\n    return REDIS_OK;\n}\n\nstatic char *readLine(redisReader *r, int *_len) {\n    char *p, *s;\n    int len;\n\n    p = r->buf+r->pos;\n    s = seekNewline(p,(r->len-r->pos));\n    if (s != NULL) {\n        len = s-(r->buf+r->pos);\n        r->pos += len+2; /* skip \\r\\n */\n        if (_len) *_len = len;\n        return p;\n    }\n    return NULL;\n}\n\nstatic void moveToNextTask(redisReader *r) {\n    redisReadTask *cur, *prv;\n    while (r->ridx >= 0) {\n        /* Return a.s.a.p. when the stack is now empty. */\n        if (r->ridx == 0) {\n            r->ridx--;\n            return;\n        }\n\n        cur = r->task[r->ridx];\n        prv = r->task[r->ridx-1];\n        assert(prv->type == REDIS_REPLY_ARRAY ||\n               prv->type == REDIS_REPLY_MAP ||\n               prv->type == REDIS_REPLY_SET ||\n               prv->type == REDIS_REPLY_PUSH);\n        if (cur->idx == prv->elements-1) {\n            r->ridx--;\n        } else {\n            /* Reset the type because the next item can be anything */\n            assert(cur->idx < prv->elements);\n            cur->type = -1;\n            cur->elements = -1;\n            cur->idx++;\n            return;\n        }\n    }\n}\n\nstatic int processLineItem(redisReader *r) {\n    redisReadTask *cur = r->task[r->ridx];\n    void *obj;\n    char *p;\n    int len;\n\n    if ((p = readLine(r,&len)) != NULL) {\n        if (cur->type == REDIS_REPLY_INTEGER) {\n            if (r->fn && r->fn->createInteger) {\n                long long v;\n                if (string2ll(p, len, &v) == REDIS_ERR) {\n                    __redisReaderSetError(r,REDIS_ERR_PROTOCOL,\n                            \"Bad integer value\");\n                    return REDIS_ERR;\n                }\n                obj = r->fn->createInteger(cur,v);\n            } else {\n                obj = (void*)REDIS_REPLY_INTEGER;\n            }\n        } else if (cur->type == REDIS_REPLY_DOUBLE) {\n            if (r->fn && r->fn->createDouble) {\n                char buf[326], *eptr;\n                double d;\n\n                if ((size_t)len >= sizeof(buf)) {\n                    __redisReaderSetError(r,REDIS_ERR_PROTOCOL,\n                            \"Double value is too large\");\n                    return REDIS_ERR;\n                }\n\n                memcpy(buf,p,len);\n                buf[len] = '\\0';\n\n                if (strcasecmp(buf,\",inf\") == 0) {\n                    d = INFINITY; /* Positive infinite. */\n                } else if (strcasecmp(buf,\",-inf\") == 0) {\n                    d = -INFINITY; /* Negative infinite. */\n                } else {\n                    d = strtod((char*)buf,&eptr);\n                    if (buf[0] == '\\0' || eptr[0] != '\\0' || isnan(d)) {\n                        __redisReaderSetError(r,REDIS_ERR_PROTOCOL,\n                                \"Bad double value\");\n                        return REDIS_ERR;\n                    }\n                }\n                obj = r->fn->createDouble(cur,d,buf,len);\n            } else {\n                obj = (void*)REDIS_REPLY_DOUBLE;\n            }\n        } else if (cur->type == REDIS_REPLY_NIL) {\n            if (r->fn && r->fn->createNil)\n                obj = r->fn->createNil(cur);\n            else\n                obj = (void*)REDIS_REPLY_NIL;\n        } else if (cur->type == REDIS_REPLY_BOOL) {\n            int bval = p[0] == 't' || p[0] == 'T';\n            if (r->fn && r->fn->createBool)\n                obj = r->fn->createBool(cur,bval);\n            else\n                obj = (void*)REDIS_REPLY_BOOL;\n        } else {\n            /* Type will be error or status. */\n            if (r->fn && r->fn->createString)\n                obj = r->fn->createString(cur,p,len);\n            else\n                obj = (void*)(size_t)(cur->type);\n        }\n\n        if (obj == NULL) {\n            __redisReaderSetErrorOOM(r);\n            return REDIS_ERR;\n        }\n\n        /* Set reply if this is the root object. */\n        if (r->ridx == 0) r->reply = obj;\n        moveToNextTask(r);\n        return REDIS_OK;\n    }\n\n    return REDIS_ERR;\n}\n\nstatic int processBulkItem(redisReader *r) {\n    redisReadTask *cur = r->task[r->ridx];\n    void *obj = NULL;\n    char *p, *s;\n    long long len;\n    unsigned long bytelen;\n    int success = 0;\n\n    p = r->buf+r->pos;\n    s = seekNewline(p,r->len-r->pos);\n    if (s != NULL) {\n        p = r->buf+r->pos;\n        bytelen = s-(r->buf+r->pos)+2; /* include \\r\\n */\n\n        if (string2ll(p, bytelen - 2, &len) == REDIS_ERR) {\n            __redisReaderSetError(r,REDIS_ERR_PROTOCOL,\n                    \"Bad bulk string length\");\n            return REDIS_ERR;\n        }\n\n        if (len < -1 || (LLONG_MAX > SIZE_MAX && len > (long long)SIZE_MAX)) {\n            __redisReaderSetError(r,REDIS_ERR_PROTOCOL,\n                    \"Bulk string length out of range\");\n            return REDIS_ERR;\n        }\n\n        if (len == -1) {\n            /* The nil object can always be created. */\n            if (r->fn && r->fn->createNil)\n                obj = r->fn->createNil(cur);\n            else\n                obj = (void*)REDIS_REPLY_NIL;\n            success = 1;\n        } else {\n            /* Only continue when the buffer contains the entire bulk item. */\n            bytelen += len+2; /* include \\r\\n */\n            if (r->pos+bytelen <= r->len) {\n                if ((cur->type == REDIS_REPLY_VERB && len < 4) ||\n                    (cur->type == REDIS_REPLY_VERB && s[5] != ':'))\n                {\n                    __redisReaderSetError(r,REDIS_ERR_PROTOCOL,\n                            \"Verbatim string 4 bytes of content type are \"\n                            \"missing or incorrectly encoded.\");\n                    return REDIS_ERR;\n                }\n                if (r->fn && r->fn->createString)\n                    obj = r->fn->createString(cur,s+2,len);\n                else\n                    obj = (void*)(long)cur->type;\n                success = 1;\n            }\n        }\n\n        /* Proceed when obj was created. */\n        if (success) {\n            if (obj == NULL) {\n                __redisReaderSetErrorOOM(r);\n                return REDIS_ERR;\n            }\n\n            r->pos += bytelen;\n\n            /* Set reply if this is the root object. */\n            if (r->ridx == 0) r->reply = obj;\n            moveToNextTask(r);\n            return REDIS_OK;\n        }\n    }\n\n    return REDIS_ERR;\n}\n\nstatic int redisReaderGrow(redisReader *r) {\n    redisReadTask **aux;\n    int newlen;\n\n    /* Grow our stack size */\n    newlen = r->tasks + REDIS_READER_STACK_SIZE;\n    aux = hi_realloc(r->task, sizeof(*r->task) * newlen);\n    if (aux == NULL)\n        goto oom;\n\n    r->task = aux;\n\n    /* Allocate new tasks */\n    for (; r->tasks < newlen; r->tasks++) {\n        r->task[r->tasks] = hi_calloc(1, sizeof(**r->task));\n        if (r->task[r->tasks] == NULL)\n            goto oom;\n    }\n\n    return REDIS_OK;\noom:\n    __redisReaderSetErrorOOM(r);\n    return REDIS_ERR;\n}\n\n/* Process the array, map and set types. */\nstatic int processAggregateItem(redisReader *r) {\n    redisReadTask *cur = r->task[r->ridx];\n    void *obj;\n    char *p;\n    long long elements;\n    int root = 0, len;\n\n    /* Set error for nested multi bulks with depth > 7 */\n    if (r->ridx == r->tasks - 1) {\n        if (redisReaderGrow(r) == REDIS_ERR)\n            return REDIS_ERR;\n    }\n\n    if ((p = readLine(r,&len)) != NULL) {\n        if (string2ll(p, len, &elements) == REDIS_ERR) {\n            __redisReaderSetError(r,REDIS_ERR_PROTOCOL,\n                    \"Bad multi-bulk length\");\n            return REDIS_ERR;\n        }\n\n        root = (r->ridx == 0);\n\n        if (elements < -1 || (LLONG_MAX > SIZE_MAX && elements > SIZE_MAX) ||\n            (r->maxelements > 0 && elements > r->maxelements))\n        {\n            __redisReaderSetError(r,REDIS_ERR_PROTOCOL,\n                    \"Multi-bulk length out of range\");\n            return REDIS_ERR;\n        }\n\n        if (elements == -1) {\n            if (r->fn && r->fn->createNil)\n                obj = r->fn->createNil(cur);\n            else\n                obj = (void*)REDIS_REPLY_NIL;\n\n            if (obj == NULL) {\n                __redisReaderSetErrorOOM(r);\n                return REDIS_ERR;\n            }\n\n            moveToNextTask(r);\n        } else {\n            if (cur->type == REDIS_REPLY_MAP) elements *= 2;\n\n            if (r->fn && r->fn->createArray)\n                obj = r->fn->createArray(cur,elements);\n            else\n                obj = (void*)(long)cur->type;\n\n            if (obj == NULL) {\n                __redisReaderSetErrorOOM(r);\n                return REDIS_ERR;\n            }\n\n            /* Modify task stack when there are more than 0 elements. */\n            if (elements > 0) {\n                cur->elements = elements;\n                cur->obj = obj;\n                r->ridx++;\n                r->task[r->ridx]->type = -1;\n                r->task[r->ridx]->elements = -1;\n                r->task[r->ridx]->idx = 0;\n                r->task[r->ridx]->obj = NULL;\n                r->task[r->ridx]->parent = cur;\n                r->task[r->ridx]->privdata = r->privdata;\n            } else {\n                moveToNextTask(r);\n            }\n        }\n\n        /* Set reply if this is the root object. */\n        if (root) r->reply = obj;\n        return REDIS_OK;\n    }\n\n    return REDIS_ERR;\n}\n\nstatic int processItem(redisReader *r) {\n    redisReadTask *cur = r->task[r->ridx];\n    char *p;\n\n    /* check if we need to read type */\n    if (cur->type < 0) {\n        if ((p = readBytes(r,1)) != NULL) {\n            switch (p[0]) {\n            case '-':\n                cur->type = REDIS_REPLY_ERROR;\n                break;\n            case '+':\n                cur->type = REDIS_REPLY_STATUS;\n                break;\n            case ':':\n                cur->type = REDIS_REPLY_INTEGER;\n                break;\n            case ',':\n                cur->type = REDIS_REPLY_DOUBLE;\n                break;\n            case '_':\n                cur->type = REDIS_REPLY_NIL;\n                break;\n            case '$':\n                cur->type = REDIS_REPLY_STRING;\n                break;\n            case '*':\n                cur->type = REDIS_REPLY_ARRAY;\n                break;\n            case '%':\n                cur->type = REDIS_REPLY_MAP;\n                break;\n            case '~':\n                cur->type = REDIS_REPLY_SET;\n                break;\n            case '#':\n                cur->type = REDIS_REPLY_BOOL;\n                break;\n            case '=':\n                cur->type = REDIS_REPLY_VERB;\n                break;\n            case '>':\n                cur->type = REDIS_REPLY_PUSH;\n                break;\n            default:\n                __redisReaderSetErrorProtocolByte(r,*p);\n                return REDIS_ERR;\n            }\n        } else {\n            /* could not consume 1 byte */\n            return REDIS_ERR;\n        }\n    }\n\n    /* process typed item */\n    switch(cur->type) {\n    case REDIS_REPLY_ERROR:\n    case REDIS_REPLY_STATUS:\n    case REDIS_REPLY_INTEGER:\n    case REDIS_REPLY_DOUBLE:\n    case REDIS_REPLY_NIL:\n    case REDIS_REPLY_BOOL:\n        return processLineItem(r);\n    case REDIS_REPLY_STRING:\n    case REDIS_REPLY_VERB:\n        return processBulkItem(r);\n    case REDIS_REPLY_ARRAY:\n    case REDIS_REPLY_MAP:\n    case REDIS_REPLY_SET:\n    case REDIS_REPLY_PUSH:\n        return processAggregateItem(r);\n    default:\n        assert(NULL);\n        return REDIS_ERR; /* Avoid warning. */\n    }\n}\n\nredisReader *redisReaderCreateWithFunctions(redisReplyObjectFunctions *fn) {\n    redisReader *r;\n\n    r = hi_calloc(1,sizeof(redisReader));\n    if (r == NULL)\n        return NULL;\n\n    r->buf = hi_sdsempty();\n    if (r->buf == NULL)\n        goto oom;\n\n    r->task = hi_calloc(REDIS_READER_STACK_SIZE, sizeof(*r->task));\n    if (r->task == NULL)\n        goto oom;\n\n    for (; r->tasks < REDIS_READER_STACK_SIZE; r->tasks++) {\n        r->task[r->tasks] = hi_calloc(1, sizeof(**r->task));\n        if (r->task[r->tasks] == NULL)\n            goto oom;\n    }\n\n    r->fn = fn;\n    r->maxbuf = REDIS_READER_MAX_BUF;\n    r->maxelements = REDIS_READER_MAX_ARRAY_ELEMENTS;\n    r->ridx = -1;\n\n    return r;\noom:\n    redisReaderFree(r);\n    return NULL;\n}\n\nvoid redisReaderFree(redisReader *r) {\n    if (r == NULL)\n        return;\n\n    if (r->reply != NULL && r->fn && r->fn->freeObject)\n        r->fn->freeObject(r->reply);\n\n    if (r->task) {\n        /* We know r->task[i] is allocated if i < r->tasks */\n        for (int i = 0; i < r->tasks; i++) {\n            hi_free(r->task[i]);\n        }\n\n        hi_free(r->task);\n    }\n\n    hi_sdsfree(r->buf);\n    hi_free(r);\n}\n\nint redisReaderFeed(redisReader *r, const char *buf, size_t len) {\n    hisds newbuf;\n\n    /* Return early when this reader is in an erroneous state. */\n    if (r->err)\n        return REDIS_ERR;\n\n    /* Copy the provided buffer. */\n    if (buf != NULL && len >= 1) {\n        /* Destroy internal buffer when it is empty and is quite large. */\n        if (r->len == 0 && r->maxbuf != 0 && hi_sdsavail(r->buf) > r->maxbuf) {\n            hi_sdsfree(r->buf);\n            r->buf = hi_sdsempty();\n            if (r->buf == 0) goto oom;\n\n            r->pos = 0;\n        }\n\n        newbuf = hi_sdscatlen(r->buf,buf,len);\n        if (newbuf == NULL) goto oom;\n\n        r->buf = newbuf;\n        r->len = hi_sdslen(r->buf);\n    }\n\n    return REDIS_OK;\noom:\n    __redisReaderSetErrorOOM(r);\n    return REDIS_ERR;\n}\n\nint redisReaderGetReply(redisReader *r, void **reply) {\n    /* Default target pointer to NULL. */\n    if (reply != NULL)\n        *reply = NULL;\n\n    /* Return early when this reader is in an erroneous state. */\n    if (r->err)\n        return REDIS_ERR;\n\n    /* When the buffer is empty, there will never be a reply. */\n    if (r->len == 0)\n        return REDIS_OK;\n\n    /* Set first item to process when the stack is empty. */\n    if (r->ridx == -1) {\n        r->task[0]->type = -1;\n        r->task[0]->elements = -1;\n        r->task[0]->idx = -1;\n        r->task[0]->obj = NULL;\n        r->task[0]->parent = NULL;\n        r->task[0]->privdata = r->privdata;\n        r->ridx = 0;\n    }\n\n    /* Process items in reply. */\n    while (r->ridx >= 0)\n        if (processItem(r) != REDIS_OK)\n            break;\n\n    /* Return ASAP when an error occurred. */\n    if (r->err)\n        return REDIS_ERR;\n\n    /* Discard part of the buffer when we've consumed at least 1k, to avoid\n     * doing unnecessary calls to memmove() in sds.c. */\n    if (r->pos >= 1024) {\n        if (hi_sdsrange(r->buf,r->pos,-1) < 0) return REDIS_ERR;\n        r->pos = 0;\n        r->len = hi_sdslen(r->buf);\n    }\n\n    /* Emit a reply when there is one. */\n    if (r->ridx == -1) {\n        if (reply != NULL) {\n            *reply = r->reply;\n        } else if (r->reply != NULL && r->fn && r->fn->freeObject) {\n            r->fn->freeObject(r->reply);\n        }\n        r->reply = NULL;\n    }\n    return REDIS_OK;\n}\n"
  },
  {
    "path": "deps/hiredis/read.h",
    "content": "/*\n * Copyright (c) 2009-2011, Salvatore Sanfilippo <antirez at gmail dot com>\n * Copyright (c) 2010-2011, Pieter Noordhuis <pcnoordhuis at gmail dot com>\n *\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n\n#ifndef __HIREDIS_READ_H\n#define __HIREDIS_READ_H\n#include <stdio.h> /* for size_t */\n\n#define REDIS_ERR -1\n#define REDIS_OK 0\n\n/* When an error occurs, the err flag in a context is set to hold the type of\n * error that occurred. REDIS_ERR_IO means there was an I/O error and you\n * should use the \"errno\" variable to find out what is wrong.\n * For other values, the \"errstr\" field will hold a description. */\n#define REDIS_ERR_IO 1 /* Error in read or write */\n#define REDIS_ERR_EOF 3 /* End of file */\n#define REDIS_ERR_PROTOCOL 4 /* Protocol error */\n#define REDIS_ERR_OOM 5 /* Out of memory */\n#define REDIS_ERR_TIMEOUT 6 /* Timed out */\n#define REDIS_ERR_OTHER 2 /* Everything else... */\n\n#define REDIS_REPLY_STRING 1\n#define REDIS_REPLY_ARRAY 2\n#define REDIS_REPLY_INTEGER 3\n#define REDIS_REPLY_NIL 4\n#define REDIS_REPLY_STATUS 5\n#define REDIS_REPLY_ERROR 6\n#define REDIS_REPLY_DOUBLE 7\n#define REDIS_REPLY_BOOL 8\n#define REDIS_REPLY_MAP 9\n#define REDIS_REPLY_SET 10\n#define REDIS_REPLY_ATTR 11\n#define REDIS_REPLY_PUSH 12\n#define REDIS_REPLY_BIGNUM 13\n#define REDIS_REPLY_VERB 14\n\n/* Default max unused reader buffer. */\n#define REDIS_READER_MAX_BUF (1024*16)\n\n/* Default multi-bulk element limit */\n#define REDIS_READER_MAX_ARRAY_ELEMENTS ((1LL<<32) - 1)\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\ntypedef struct redisReadTask {\n    int type;\n    long long elements; /* number of elements in multibulk container */\n    int idx; /* index in parent (array) object */\n    void *obj; /* holds user-generated value for a read task */\n    struct redisReadTask *parent; /* parent task */\n    void *privdata; /* user-settable arbitrary field */\n} redisReadTask;\n\ntypedef struct redisReplyObjectFunctions {\n    void *(*createString)(const redisReadTask*, char*, size_t);\n    void *(*createArray)(const redisReadTask*, size_t);\n    void *(*createInteger)(const redisReadTask*, long long);\n    void *(*createDouble)(const redisReadTask*, double, char*, size_t);\n    void *(*createNil)(const redisReadTask*);\n    void *(*createBool)(const redisReadTask*, int);\n    void (*freeObject)(void*);\n} redisReplyObjectFunctions;\n\ntypedef struct redisReader {\n    int err; /* Error flags, 0 when there is no error */\n    char errstr[128]; /* String representation of error when applicable */\n\n    char *buf; /* Read buffer */\n    size_t pos; /* Buffer cursor */\n    size_t len; /* Buffer length */\n    size_t maxbuf; /* Max length of unused buffer */\n    long long maxelements; /* Max multi-bulk elements */\n\n    redisReadTask **task;\n    int tasks;\n\n    int ridx; /* Index of current read task */\n    void *reply; /* Temporary reply pointer */\n\n    redisReplyObjectFunctions *fn;\n    void *privdata;\n} redisReader;\n\n/* Public API for the protocol parser. */\nredisReader *redisReaderCreateWithFunctions(redisReplyObjectFunctions *fn);\nvoid redisReaderFree(redisReader *r);\nint redisReaderFeed(redisReader *r, const char *buf, size_t len);\nint redisReaderGetReply(redisReader *r, void **reply);\n\n#define redisReaderSetPrivdata(_r, _p) (int)(((redisReader*)(_r))->privdata = (_p))\n#define redisReaderGetObject(_r) (((redisReader*)(_r))->reply)\n#define redisReaderGetError(_r) (((redisReader*)(_r))->errstr)\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n"
  },
  {
    "path": "deps/hiredis/sds.c",
    "content": "/* SDSLib 2.0 -- A C dynamic strings library\n *\n * Copyright (c) 2006-2015, Salvatore Sanfilippo <antirez at gmail dot com>\n * Copyright (c) 2015, Oran Agra\n * Copyright (c) 2015, Redis Labs, Inc\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"fmacros.h\"\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <ctype.h>\n#include <assert.h>\n#include <limits.h>\n#include \"sds.h\"\n#include \"sdsalloc.h\"\n\nstatic inline int hi_sdsHdrSize(char type) {\n    switch(type&HI_SDS_TYPE_MASK) {\n        case HI_SDS_TYPE_5:\n            return sizeof(struct hisdshdr5);\n        case HI_SDS_TYPE_8:\n            return sizeof(struct hisdshdr8);\n        case HI_SDS_TYPE_16:\n            return sizeof(struct hisdshdr16);\n        case HI_SDS_TYPE_32:\n            return sizeof(struct hisdshdr32);\n        case HI_SDS_TYPE_64:\n            return sizeof(struct hisdshdr64);\n    }\n    return 0;\n}\n\nstatic inline char hi_sdsReqType(size_t string_size) {\n    if (string_size < 32)\n        return HI_SDS_TYPE_5;\n    if (string_size < 0xff)\n        return HI_SDS_TYPE_8;\n    if (string_size < 0xffff)\n        return HI_SDS_TYPE_16;\n    if (string_size < 0xffffffff)\n        return HI_SDS_TYPE_32;\n    return HI_SDS_TYPE_64;\n}\n\n/* Create a new hisds string with the content specified by the 'init' pointer\n * and 'initlen'.\n * If NULL is used for 'init' the string is initialized with zero bytes.\n *\n * The string is always null-termined (all the hisds strings are, always) so\n * even if you create an hisds string with:\n *\n * mystring = hi_sdsnewlen(\"abc\",3);\n *\n * You can print the string with printf() as there is an implicit \\0 at the\n * end of the string. However the string is binary safe and can contain\n * \\0 characters in the middle, as the length is stored in the hisds header. */\nhisds hi_sdsnewlen(const void *init, size_t initlen) {\n    void *sh;\n    hisds s;\n    char type = hi_sdsReqType(initlen);\n    /* Empty strings are usually created in order to append. Use type 8\n     * since type 5 is not good at this. */\n    if (type == HI_SDS_TYPE_5 && initlen == 0) type = HI_SDS_TYPE_8;\n    int hdrlen = hi_sdsHdrSize(type);\n    unsigned char *fp; /* flags pointer. */\n\n    sh = hi_s_malloc(hdrlen+initlen+1);\n    if (sh == NULL) return NULL;\n    if (!init)\n        memset(sh, 0, hdrlen+initlen+1);\n    s = (char*)sh+hdrlen;\n    fp = ((unsigned char*)s)-1;\n    switch(type) {\n        case HI_SDS_TYPE_5: {\n            *fp = type | (initlen << HI_SDS_TYPE_BITS);\n            break;\n        }\n        case HI_SDS_TYPE_8: {\n            HI_SDS_HDR_VAR(8,s);\n            sh->len = initlen;\n            sh->alloc = initlen;\n            *fp = type;\n            break;\n        }\n        case HI_SDS_TYPE_16: {\n            HI_SDS_HDR_VAR(16,s);\n            sh->len = initlen;\n            sh->alloc = initlen;\n            *fp = type;\n            break;\n        }\n        case HI_SDS_TYPE_32: {\n            HI_SDS_HDR_VAR(32,s);\n            sh->len = initlen;\n            sh->alloc = initlen;\n            *fp = type;\n            break;\n        }\n        case HI_SDS_TYPE_64: {\n            HI_SDS_HDR_VAR(64,s);\n            sh->len = initlen;\n            sh->alloc = initlen;\n            *fp = type;\n            break;\n        }\n    }\n    if (initlen && init)\n        memcpy(s, init, initlen);\n    s[initlen] = '\\0';\n    return s;\n}\n\n/* Create an empty (zero length) hisds string. Even in this case the string\n * always has an implicit null term. */\nhisds hi_sdsempty(void) {\n    return hi_sdsnewlen(\"\",0);\n}\n\n/* Create a new hisds string starting from a null terminated C string. */\nhisds hi_sdsnew(const char *init) {\n    size_t initlen = (init == NULL) ? 0 : strlen(init);\n    return hi_sdsnewlen(init, initlen);\n}\n\n/* Duplicate an hisds string. */\nhisds hi_sdsdup(const hisds s) {\n    return hi_sdsnewlen(s, hi_sdslen(s));\n}\n\n/* Free an hisds string. No operation is performed if 's' is NULL. */\nvoid hi_sdsfree(hisds s) {\n    if (s == NULL) return;\n    hi_s_free((char*)s-hi_sdsHdrSize(s[-1]));\n}\n\n/* Set the hisds string length to the length as obtained with strlen(), so\n * considering as content only up to the first null term character.\n *\n * This function is useful when the hisds string is hacked manually in some\n * way, like in the following example:\n *\n * s = hi_sdsnew(\"foobar\");\n * s[2] = '\\0';\n * hi_sdsupdatelen(s);\n * printf(\"%d\\n\", hi_sdslen(s));\n *\n * The output will be \"2\", but if we comment out the call to hi_sdsupdatelen()\n * the output will be \"6\" as the string was modified but the logical length\n * remains 6 bytes. */\nvoid hi_sdsupdatelen(hisds s) {\n    int reallen = strlen(s);\n    hi_sdssetlen(s, reallen);\n}\n\n/* Modify an hisds string in-place to make it empty (zero length).\n * However all the existing buffer is not discarded but set as free space\n * so that next append operations will not require allocations up to the\n * number of bytes previously available. */\nvoid hi_sdsclear(hisds s) {\n    hi_sdssetlen(s, 0);\n    s[0] = '\\0';\n}\n\n/* Enlarge the free space at the end of the hisds string so that the caller\n * is sure that after calling this function can overwrite up to addlen\n * bytes after the end of the string, plus one more byte for nul term.\n *\n * Note: this does not change the *length* of the hisds string as returned\n * by hi_sdslen(), but only the free buffer space we have. */\nhisds hi_sdsMakeRoomFor(hisds s, size_t addlen) {\n    void *sh, *newsh;\n    size_t avail = hi_sdsavail(s);\n    size_t len, newlen;\n    char type, oldtype = s[-1] & HI_SDS_TYPE_MASK;\n    int hdrlen;\n\n    /* Return ASAP if there is enough space left. */\n    if (avail >= addlen) return s;\n\n    len = hi_sdslen(s);\n    sh = (char*)s-hi_sdsHdrSize(oldtype);\n    newlen = (len+addlen);\n    if (newlen < HI_SDS_MAX_PREALLOC)\n        newlen *= 2;\n    else\n        newlen += HI_SDS_MAX_PREALLOC;\n\n    type = hi_sdsReqType(newlen);\n\n    /* Don't use type 5: the user is appending to the string and type 5 is\n     * not able to remember empty space, so hi_sdsMakeRoomFor() must be called\n     * at every appending operation. */\n    if (type == HI_SDS_TYPE_5) type = HI_SDS_TYPE_8;\n\n    hdrlen = hi_sdsHdrSize(type);\n    if (oldtype==type) {\n        newsh = hi_s_realloc(sh, hdrlen+newlen+1);\n        if (newsh == NULL) return NULL;\n        s = (char*)newsh+hdrlen;\n    } else {\n        /* Since the header size changes, need to move the string forward,\n         * and can't use realloc */\n        newsh = hi_s_malloc(hdrlen+newlen+1);\n        if (newsh == NULL) return NULL;\n        memcpy((char*)newsh+hdrlen, s, len+1);\n        hi_s_free(sh);\n        s = (char*)newsh+hdrlen;\n        s[-1] = type;\n        hi_sdssetlen(s, len);\n    }\n    hi_sdssetalloc(s, newlen);\n    return s;\n}\n\n/* Reallocate the hisds string so that it has no free space at the end. The\n * contained string remains not altered, but next concatenation operations\n * will require a reallocation.\n *\n * After the call, the passed hisds string is no longer valid and all the\n * references must be substituted with the new pointer returned by the call. */\nhisds hi_sdsRemoveFreeSpace(hisds s) {\n    void *sh, *newsh;\n    char type, oldtype = s[-1] & HI_SDS_TYPE_MASK;\n    int hdrlen;\n    size_t len = hi_sdslen(s);\n    sh = (char*)s-hi_sdsHdrSize(oldtype);\n\n    type = hi_sdsReqType(len);\n    hdrlen = hi_sdsHdrSize(type);\n    if (oldtype==type) {\n        newsh = hi_s_realloc(sh, hdrlen+len+1);\n        if (newsh == NULL) return NULL;\n        s = (char*)newsh+hdrlen;\n    } else {\n        newsh = hi_s_malloc(hdrlen+len+1);\n        if (newsh == NULL) return NULL;\n        memcpy((char*)newsh+hdrlen, s, len+1);\n        hi_s_free(sh);\n        s = (char*)newsh+hdrlen;\n        s[-1] = type;\n        hi_sdssetlen(s, len);\n    }\n    hi_sdssetalloc(s, len);\n    return s;\n}\n\n/* Return the total size of the allocation of the specifed hisds string,\n * including:\n * 1) The hisds header before the pointer.\n * 2) The string.\n * 3) The free buffer at the end if any.\n * 4) The implicit null term.\n */\nsize_t hi_sdsAllocSize(hisds s) {\n    size_t alloc = hi_sdsalloc(s);\n    return hi_sdsHdrSize(s[-1])+alloc+1;\n}\n\n/* Return the pointer of the actual SDS allocation (normally SDS strings\n * are referenced by the start of the string buffer). */\nvoid *hi_sdsAllocPtr(hisds s) {\n    return (void*) (s-hi_sdsHdrSize(s[-1]));\n}\n\n/* Increment the hisds length and decrements the left free space at the\n * end of the string according to 'incr'. Also set the null term\n * in the new end of the string.\n *\n * This function is used in order to fix the string length after the\n * user calls hi_sdsMakeRoomFor(), writes something after the end of\n * the current string, and finally needs to set the new length.\n *\n * Note: it is possible to use a negative increment in order to\n * right-trim the string.\n *\n * Usage example:\n *\n * Using hi_sdsIncrLen() and hi_sdsMakeRoomFor() it is possible to mount the\n * following schema, to cat bytes coming from the kernel to the end of an\n * hisds string without copying into an intermediate buffer:\n *\n * oldlen = hi_hi_sdslen(s);\n * s = hi_sdsMakeRoomFor(s, BUFFER_SIZE);\n * nread = read(fd, s+oldlen, BUFFER_SIZE);\n * ... check for nread <= 0 and handle it ...\n * hi_sdsIncrLen(s, nread);\n */\nvoid hi_sdsIncrLen(hisds s, int incr) {\n    unsigned char flags = s[-1];\n    size_t len;\n    switch(flags&HI_SDS_TYPE_MASK) {\n        case HI_SDS_TYPE_5: {\n            unsigned char *fp = ((unsigned char*)s)-1;\n            unsigned char oldlen = HI_SDS_TYPE_5_LEN(flags);\n            assert((incr > 0 && oldlen+incr < 32) || (incr < 0 && oldlen >= (unsigned int)(-incr)));\n            *fp = HI_SDS_TYPE_5 | ((oldlen+incr) << HI_SDS_TYPE_BITS);\n            len = oldlen+incr;\n            break;\n        }\n        case HI_SDS_TYPE_8: {\n            HI_SDS_HDR_VAR(8,s);\n            assert((incr >= 0 && sh->alloc-sh->len >= incr) || (incr < 0 && sh->len >= (unsigned int)(-incr)));\n            len = (sh->len += incr);\n            break;\n        }\n        case HI_SDS_TYPE_16: {\n            HI_SDS_HDR_VAR(16,s);\n            assert((incr >= 0 && sh->alloc-sh->len >= incr) || (incr < 0 && sh->len >= (unsigned int)(-incr)));\n            len = (sh->len += incr);\n            break;\n        }\n        case HI_SDS_TYPE_32: {\n            HI_SDS_HDR_VAR(32,s);\n            assert((incr >= 0 && sh->alloc-sh->len >= (unsigned int)incr) || (incr < 0 && sh->len >= (unsigned int)(-incr)));\n            len = (sh->len += incr);\n            break;\n        }\n        case HI_SDS_TYPE_64: {\n            HI_SDS_HDR_VAR(64,s);\n            assert((incr >= 0 && sh->alloc-sh->len >= (uint64_t)incr) || (incr < 0 && sh->len >= (uint64_t)(-incr)));\n            len = (sh->len += incr);\n            break;\n        }\n        default: len = 0; /* Just to avoid compilation warnings. */\n    }\n    s[len] = '\\0';\n}\n\n/* Grow the hisds to have the specified length. Bytes that were not part of\n * the original length of the hisds will be set to zero.\n *\n * if the specified length is smaller than the current length, no operation\n * is performed. */\nhisds hi_sdsgrowzero(hisds s, size_t len) {\n    size_t curlen = hi_sdslen(s);\n\n    if (len <= curlen) return s;\n    s = hi_sdsMakeRoomFor(s,len-curlen);\n    if (s == NULL) return NULL;\n\n    /* Make sure added region doesn't contain garbage */\n    memset(s+curlen,0,(len-curlen+1)); /* also set trailing \\0 byte */\n    hi_sdssetlen(s, len);\n    return s;\n}\n\n/* Append the specified binary-safe string pointed by 't' of 'len' bytes to the\n * end of the specified hisds string 's'.\n *\n * After the call, the passed hisds string is no longer valid and all the\n * references must be substituted with the new pointer returned by the call. */\nhisds hi_sdscatlen(hisds s, const void *t, size_t len) {\n    size_t curlen = hi_sdslen(s);\n\n    s = hi_sdsMakeRoomFor(s,len);\n    if (s == NULL) return NULL;\n    memcpy(s+curlen, t, len);\n    hi_sdssetlen(s, curlen+len);\n    s[curlen+len] = '\\0';\n    return s;\n}\n\n/* Append the specified null termianted C string to the hisds string 's'.\n *\n * After the call, the passed hisds string is no longer valid and all the\n * references must be substituted with the new pointer returned by the call. */\nhisds hi_sdscat(hisds s, const char *t) {\n    return hi_sdscatlen(s, t, strlen(t));\n}\n\n/* Append the specified hisds 't' to the existing hisds 's'.\n *\n * After the call, the modified hisds string is no longer valid and all the\n * references must be substituted with the new pointer returned by the call. */\nhisds hi_sdscatsds(hisds s, const hisds t) {\n    return hi_sdscatlen(s, t, hi_sdslen(t));\n}\n\n/* Destructively modify the hisds string 's' to hold the specified binary\n * safe string pointed by 't' of length 'len' bytes. */\nhisds hi_sdscpylen(hisds s, const char *t, size_t len) {\n    if (hi_sdsalloc(s) < len) {\n        s = hi_sdsMakeRoomFor(s,len-hi_sdslen(s));\n        if (s == NULL) return NULL;\n    }\n    memcpy(s, t, len);\n    s[len] = '\\0';\n    hi_sdssetlen(s, len);\n    return s;\n}\n\n/* Like hi_sdscpylen() but 't' must be a null-termined string so that the length\n * of the string is obtained with strlen(). */\nhisds hi_sdscpy(hisds s, const char *t) {\n    return hi_sdscpylen(s, t, strlen(t));\n}\n\n/* Helper for hi_sdscatlonglong() doing the actual number -> string\n * conversion. 's' must point to a string with room for at least\n * HI_SDS_LLSTR_SIZE bytes.\n *\n * The function returns the length of the null-terminated string\n * representation stored at 's'. */\n#define HI_SDS_LLSTR_SIZE 21\nint hi_sdsll2str(char *s, long long value) {\n    char *p, aux;\n    unsigned long long v;\n    size_t l;\n\n    /* Generate the string representation, this method produces\n     * an reversed string. */\n    v = (value < 0) ? -value : value;\n    p = s;\n    do {\n        *p++ = '0'+(v%10);\n        v /= 10;\n    } while(v);\n    if (value < 0) *p++ = '-';\n\n    /* Compute length and add null term. */\n    l = p-s;\n    *p = '\\0';\n\n    /* Reverse the string. */\n    p--;\n    while(s < p) {\n        aux = *s;\n        *s = *p;\n        *p = aux;\n        s++;\n        p--;\n    }\n    return l;\n}\n\n/* Identical hi_sdsll2str(), but for unsigned long long type. */\nint hi_sdsull2str(char *s, unsigned long long v) {\n    char *p, aux;\n    size_t l;\n\n    /* Generate the string representation, this method produces\n     * an reversed string. */\n    p = s;\n    do {\n        *p++ = '0'+(v%10);\n        v /= 10;\n    } while(v);\n\n    /* Compute length and add null term. */\n    l = p-s;\n    *p = '\\0';\n\n    /* Reverse the string. */\n    p--;\n    while(s < p) {\n        aux = *s;\n        *s = *p;\n        *p = aux;\n        s++;\n        p--;\n    }\n    return l;\n}\n\n/* Create an hisds string from a long long value. It is much faster than:\n *\n * hi_sdscatprintf(hi_sdsempty(),\"%lld\\n\", value);\n */\nhisds hi_sdsfromlonglong(long long value) {\n    char buf[HI_SDS_LLSTR_SIZE];\n    int len = hi_sdsll2str(buf,value);\n\n    return hi_sdsnewlen(buf,len);\n}\n\n/* Like hi_sdscatprintf() but gets va_list instead of being variadic. */\nhisds hi_sdscatvprintf(hisds s, const char *fmt, va_list ap) {\n    va_list cpy;\n    char staticbuf[1024], *buf = staticbuf, *t;\n    size_t buflen = strlen(fmt)*2;\n\n    /* We try to start using a static buffer for speed.\n     * If not possible we revert to heap allocation. */\n    if (buflen > sizeof(staticbuf)) {\n        buf = hi_s_malloc(buflen);\n        if (buf == NULL) return NULL;\n    } else {\n        buflen = sizeof(staticbuf);\n    }\n\n    /* Try with buffers two times bigger every time we fail to\n     * fit the string in the current buffer size. */\n    while(1) {\n        buf[buflen-2] = '\\0';\n        va_copy(cpy,ap);\n        vsnprintf(buf, buflen, fmt, cpy);\n        va_end(cpy);\n        if (buf[buflen-2] != '\\0') {\n            if (buf != staticbuf) hi_s_free(buf);\n            buflen *= 2;\n            buf = hi_s_malloc(buflen);\n            if (buf == NULL) return NULL;\n            continue;\n        }\n        break;\n    }\n\n    /* Finally concat the obtained string to the SDS string and return it. */\n    t = hi_sdscat(s, buf);\n    if (buf != staticbuf) hi_s_free(buf);\n    return t;\n}\n\n/* Append to the hisds string 's' a string obtained using printf-alike format\n * specifier.\n *\n * After the call, the modified hisds string is no longer valid and all the\n * references must be substituted with the new pointer returned by the call.\n *\n * Example:\n *\n * s = hi_sdsnew(\"Sum is: \");\n * s = hi_sdscatprintf(s,\"%d+%d = %d\",a,b,a+b).\n *\n * Often you need to create a string from scratch with the printf-alike\n * format. When this is the need, just use hi_sdsempty() as the target string:\n *\n * s = hi_sdscatprintf(hi_sdsempty(), \"... your format ...\", args);\n */\nhisds hi_sdscatprintf(hisds s, const char *fmt, ...) {\n    va_list ap;\n    char *t;\n    va_start(ap, fmt);\n    t = hi_sdscatvprintf(s,fmt,ap);\n    va_end(ap);\n    return t;\n}\n\n/* This function is similar to hi_sdscatprintf, but much faster as it does\n * not rely on sprintf() family functions implemented by the libc that\n * are often very slow. Moreover directly handling the hisds string as\n * new data is concatenated provides a performance improvement.\n *\n * However this function only handles an incompatible subset of printf-alike\n * format specifiers:\n *\n * %s - C String\n * %S - SDS string\n * %i - signed int\n * %I - 64 bit signed integer (long long, int64_t)\n * %u - unsigned int\n * %U - 64 bit unsigned integer (unsigned long long, uint64_t)\n * %% - Verbatim \"%\" character.\n */\nhisds hi_sdscatfmt(hisds s, char const *fmt, ...) {\n    const char *f = fmt;\n    int i;\n    va_list ap;\n\n    va_start(ap,fmt);\n    i = hi_sdslen(s); /* Position of the next byte to write to dest str. */\n    while(*f) {\n        char next, *str;\n        size_t l;\n        long long num;\n        unsigned long long unum;\n\n        /* Make sure there is always space for at least 1 char. */\n        if (hi_sdsavail(s)==0) {\n            s = hi_sdsMakeRoomFor(s,1);\n            if (s == NULL) goto fmt_error;\n        }\n\n        switch(*f) {\n        case '%':\n            next = *(f+1);\n            f++;\n            switch(next) {\n            case 's':\n            case 'S':\n                str = va_arg(ap,char*);\n                l = (next == 's') ? strlen(str) : hi_sdslen(str);\n                if (hi_sdsavail(s) < l) {\n                    s = hi_sdsMakeRoomFor(s,l);\n                    if (s == NULL) goto fmt_error;\n                }\n                memcpy(s+i,str,l);\n                hi_sdsinclen(s,l);\n                i += l;\n                break;\n            case 'i':\n            case 'I':\n                if (next == 'i')\n                    num = va_arg(ap,int);\n                else\n                    num = va_arg(ap,long long);\n                {\n                    char buf[HI_SDS_LLSTR_SIZE];\n                    l = hi_sdsll2str(buf,num);\n                    if (hi_sdsavail(s) < l) {\n                        s = hi_sdsMakeRoomFor(s,l);\n                        if (s == NULL) goto fmt_error;\n                    }\n                    memcpy(s+i,buf,l);\n                    hi_sdsinclen(s,l);\n                    i += l;\n                }\n                break;\n            case 'u':\n            case 'U':\n                if (next == 'u')\n                    unum = va_arg(ap,unsigned int);\n                else\n                    unum = va_arg(ap,unsigned long long);\n                {\n                    char buf[HI_SDS_LLSTR_SIZE];\n                    l = hi_sdsull2str(buf,unum);\n                    if (hi_sdsavail(s) < l) {\n                        s = hi_sdsMakeRoomFor(s,l);\n                        if (s == NULL) goto fmt_error;\n                    }\n                    memcpy(s+i,buf,l);\n                    hi_sdsinclen(s,l);\n                    i += l;\n                }\n                break;\n            default: /* Handle %% and generally %<unknown>. */\n                s[i++] = next;\n                hi_sdsinclen(s,1);\n                break;\n            }\n            break;\n        default:\n            s[i++] = *f;\n            hi_sdsinclen(s,1);\n            break;\n        }\n        f++;\n    }\n    va_end(ap);\n\n    /* Add null-term */\n    s[i] = '\\0';\n    return s;\n\nfmt_error:\n    va_end(ap);\n    return NULL;\n}\n\n/* Remove the part of the string from left and from right composed just of\n * contiguous characters found in 'cset', that is a null terminted C string.\n *\n * After the call, the modified hisds string is no longer valid and all the\n * references must be substituted with the new pointer returned by the call.\n *\n * Example:\n *\n * s = hi_sdsnew(\"AA...AA.a.aa.aHelloWorld     :::\");\n * s = hi_sdstrim(s,\"Aa. :\");\n * printf(\"%s\\n\", s);\n *\n * Output will be just \"Hello World\".\n */\nhisds hi_sdstrim(hisds s, const char *cset) {\n    char *start, *end, *sp, *ep;\n    size_t len;\n\n    sp = start = s;\n    ep = end = s+hi_sdslen(s)-1;\n    while(sp <= end && strchr(cset, *sp)) sp++;\n    while(ep > sp && strchr(cset, *ep)) ep--;\n    len = (sp > ep) ? 0 : ((ep-sp)+1);\n    if (s != sp) memmove(s, sp, len);\n    s[len] = '\\0';\n    hi_sdssetlen(s,len);\n    return s;\n}\n\n/* Turn the string into a smaller (or equal) string containing only the\n * substring specified by the 'start' and 'end' indexes.\n *\n * start and end can be negative, where -1 means the last character of the\n * string, -2 the penultimate character, and so forth.\n *\n * The interval is inclusive, so the start and end characters will be part\n * of the resulting string.\n *\n * The string is modified in-place.\n *\n * Return value:\n * -1 (error) if hi_sdslen(s) is larger than maximum positive ssize_t value.\n *  0 on success.\n *\n * Example:\n *\n * s = hi_sdsnew(\"Hello World\");\n * hi_sdsrange(s,1,-1); => \"ello World\"\n */\nint hi_sdsrange(hisds s, ssize_t start, ssize_t end) {\n    size_t newlen, len = hi_sdslen(s);\n    if (len > SSIZE_MAX) return -1;\n\n    if (len == 0) return 0;\n    if (start < 0) {\n        start = len+start;\n        if (start < 0) start = 0;\n    }\n    if (end < 0) {\n        end = len+end;\n        if (end < 0) end = 0;\n    }\n    newlen = (start > end) ? 0 : (end-start)+1;\n    if (newlen != 0) {\n        if (start >= (ssize_t)len) {\n            newlen = 0;\n        } else if (end >= (ssize_t)len) {\n            end = len-1;\n            newlen = (start > end) ? 0 : (end-start)+1;\n        }\n    } else {\n        start = 0;\n    }\n    if (start && newlen) memmove(s, s+start, newlen);\n    s[newlen] = 0;\n    hi_sdssetlen(s,newlen);\n    return 0;\n}\n\n/* Apply tolower() to every character of the hisds string 's'. */\nvoid hi_sdstolower(hisds s) {\n    int len = hi_sdslen(s), j;\n\n    for (j = 0; j < len; j++) s[j] = tolower(s[j]);\n}\n\n/* Apply toupper() to every character of the hisds string 's'. */\nvoid hi_sdstoupper(hisds s) {\n    int len = hi_sdslen(s), j;\n\n    for (j = 0; j < len; j++) s[j] = toupper(s[j]);\n}\n\n/* Compare two hisds strings s1 and s2 with memcmp().\n *\n * Return value:\n *\n *     positive if s1 > s2.\n *     negative if s1 < s2.\n *     0 if s1 and s2 are exactly the same binary string.\n *\n * If two strings share exactly the same prefix, but one of the two has\n * additional characters, the longer string is considered to be greater than\n * the smaller one. */\nint hi_sdscmp(const hisds s1, const hisds s2) {\n    size_t l1, l2, minlen;\n    int cmp;\n\n    l1 = hi_sdslen(s1);\n    l2 = hi_sdslen(s2);\n    minlen = (l1 < l2) ? l1 : l2;\n    cmp = memcmp(s1,s2,minlen);\n    if (cmp == 0) return l1-l2;\n    return cmp;\n}\n\n/* Split 's' with separator in 'sep'. An array\n * of hisds strings is returned. *count will be set\n * by reference to the number of tokens returned.\n *\n * On out of memory, zero length string, zero length\n * separator, NULL is returned.\n *\n * Note that 'sep' is able to split a string using\n * a multi-character separator. For example\n * hi_sdssplit(\"foo_-_bar\",\"_-_\"); will return two\n * elements \"foo\" and \"bar\".\n *\n * This version of the function is binary-safe but\n * requires length arguments. hi_sdssplit() is just the\n * same function but for zero-terminated strings.\n */\nhisds *hi_sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count) {\n    int elements = 0, slots = 5, start = 0, j;\n    hisds *tokens;\n\n    if (seplen < 1 || len < 0) return NULL;\n\n    tokens = hi_s_malloc(sizeof(hisds)*slots);\n    if (tokens == NULL) return NULL;\n\n    if (len == 0) {\n        *count = 0;\n        return tokens;\n    }\n    for (j = 0; j < (len-(seplen-1)); j++) {\n        /* make sure there is room for the next element and the final one */\n        if (slots < elements+2) {\n            hisds *newtokens;\n\n            slots *= 2;\n            newtokens = hi_s_realloc(tokens,sizeof(hisds)*slots);\n            if (newtokens == NULL) goto cleanup;\n            tokens = newtokens;\n        }\n        /* search the separator */\n        if ((seplen == 1 && *(s+j) == sep[0]) || (memcmp(s+j,sep,seplen) == 0)) {\n            tokens[elements] = hi_sdsnewlen(s+start,j-start);\n            if (tokens[elements] == NULL) goto cleanup;\n            elements++;\n            start = j+seplen;\n            j = j+seplen-1; /* skip the separator */\n        }\n    }\n    /* Add the final element. We are sure there is room in the tokens array. */\n    tokens[elements] = hi_sdsnewlen(s+start,len-start);\n    if (tokens[elements] == NULL) goto cleanup;\n    elements++;\n    *count = elements;\n    return tokens;\n\ncleanup:\n    {\n        int i;\n        for (i = 0; i < elements; i++) hi_sdsfree(tokens[i]);\n        hi_s_free(tokens);\n        *count = 0;\n        return NULL;\n    }\n}\n\n/* Free the result returned by hi_sdssplitlen(), or do nothing if 'tokens' is NULL. */\nvoid hi_sdsfreesplitres(hisds *tokens, int count) {\n    if (!tokens) return;\n    while(count--)\n        hi_sdsfree(tokens[count]);\n    hi_s_free(tokens);\n}\n\n/* Append to the hisds string \"s\" an escaped string representation where\n * all the non-printable characters (tested with isprint()) are turned into\n * escapes in the form \"\\n\\r\\a....\" or \"\\x<hex-number>\".\n *\n * After the call, the modified hisds string is no longer valid and all the\n * references must be substituted with the new pointer returned by the call. */\nhisds hi_sdscatrepr(hisds s, const char *p, size_t len) {\n    s = hi_sdscatlen(s,\"\\\"\",1);\n    while(len--) {\n        switch(*p) {\n        case '\\\\':\n        case '\"':\n            s = hi_sdscatprintf(s,\"\\\\%c\",*p);\n            break;\n        case '\\n': s = hi_sdscatlen(s,\"\\\\n\",2); break;\n        case '\\r': s = hi_sdscatlen(s,\"\\\\r\",2); break;\n        case '\\t': s = hi_sdscatlen(s,\"\\\\t\",2); break;\n        case '\\a': s = hi_sdscatlen(s,\"\\\\a\",2); break;\n        case '\\b': s = hi_sdscatlen(s,\"\\\\b\",2); break;\n        default:\n            if (isprint(*p))\n                s = hi_sdscatprintf(s,\"%c\",*p);\n            else\n                s = hi_sdscatprintf(s,\"\\\\x%02x\",(unsigned char)*p);\n            break;\n        }\n        p++;\n    }\n    return hi_sdscatlen(s,\"\\\"\",1);\n}\n\n/* Helper function for hi_sdssplitargs() that converts a hex digit into an\n * integer from 0 to 15 */\nstatic int hi_hex_digit_to_int(char c) {\n    switch(c) {\n    case '0': return 0;\n    case '1': return 1;\n    case '2': return 2;\n    case '3': return 3;\n    case '4': return 4;\n    case '5': return 5;\n    case '6': return 6;\n    case '7': return 7;\n    case '8': return 8;\n    case '9': return 9;\n    case 'a': case 'A': return 10;\n    case 'b': case 'B': return 11;\n    case 'c': case 'C': return 12;\n    case 'd': case 'D': return 13;\n    case 'e': case 'E': return 14;\n    case 'f': case 'F': return 15;\n    default: return 0;\n    }\n}\n\n/* Split a line into arguments, where every argument can be in the\n * following programming-language REPL-alike form:\n *\n * foo bar \"newline are supported\\n\" and \"\\xff\\x00otherstuff\"\n *\n * The number of arguments is stored into *argc, and an array\n * of hisds is returned.\n *\n * The caller should free the resulting array of hisds strings with\n * hi_sdsfreesplitres().\n *\n * Note that hi_sdscatrepr() is able to convert back a string into\n * a quoted string in the same format hi_sdssplitargs() is able to parse.\n *\n * The function returns the allocated tokens on success, even when the\n * input string is empty, or NULL if the input contains unbalanced\n * quotes or closed quotes followed by non space characters\n * as in: \"foo\"bar or \"foo'\n */\nhisds *hi_sdssplitargs(const char *line, int *argc) {\n    const char *p = line;\n    char *current = NULL;\n    char **vector = NULL;\n\n    *argc = 0;\n    while(1) {\n        /* skip blanks */\n        while(*p && isspace(*p)) p++;\n        if (*p) {\n            /* get a token */\n            int inq=0;  /* set to 1 if we are in \"quotes\" */\n            int insq=0; /* set to 1 if we are in 'single quotes' */\n            int done=0;\n\n            if (current == NULL) current = hi_sdsempty();\n            while(!done) {\n                if (inq) {\n                    if (*p == '\\\\' && *(p+1) == 'x' &&\n                                             isxdigit(*(p+2)) &&\n                                             isxdigit(*(p+3)))\n                    {\n                        unsigned char byte;\n\n                        byte = (hi_hex_digit_to_int(*(p+2))*16)+\n                                hi_hex_digit_to_int(*(p+3));\n                        current = hi_sdscatlen(current,(char*)&byte,1);\n                        p += 3;\n                    } else if (*p == '\\\\' && *(p+1)) {\n                        char c;\n\n                        p++;\n                        switch(*p) {\n                        case 'n': c = '\\n'; break;\n                        case 'r': c = '\\r'; break;\n                        case 't': c = '\\t'; break;\n                        case 'b': c = '\\b'; break;\n                        case 'a': c = '\\a'; break;\n                        default: c = *p; break;\n                        }\n                        current = hi_sdscatlen(current,&c,1);\n                    } else if (*p == '\"') {\n                        /* closing quote must be followed by a space or\n                         * nothing at all. */\n                        if (*(p+1) && !isspace(*(p+1))) goto err;\n                        done=1;\n                    } else if (!*p) {\n                        /* unterminated quotes */\n                        goto err;\n                    } else {\n                        current = hi_sdscatlen(current,p,1);\n                    }\n                } else if (insq) {\n                    if (*p == '\\\\' && *(p+1) == '\\'') {\n                        p++;\n                        current = hi_sdscatlen(current,\"'\",1);\n                    } else if (*p == '\\'') {\n                        /* closing quote must be followed by a space or\n                         * nothing at all. */\n                        if (*(p+1) && !isspace(*(p+1))) goto err;\n                        done=1;\n                    } else if (!*p) {\n                        /* unterminated quotes */\n                        goto err;\n                    } else {\n                        current = hi_sdscatlen(current,p,1);\n                    }\n                } else {\n                    switch(*p) {\n                    case ' ':\n                    case '\\n':\n                    case '\\r':\n                    case '\\t':\n                    case '\\0':\n                        done=1;\n                        break;\n                    case '\"':\n                        inq=1;\n                        break;\n                    case '\\'':\n                        insq=1;\n                        break;\n                    default:\n                        current = hi_sdscatlen(current,p,1);\n                        break;\n                    }\n                }\n                if (*p) p++;\n            }\n            /* add the token to the vector */\n            {\n                char **new_vector = hi_s_realloc(vector,((*argc)+1)*sizeof(char*));\n                if (new_vector == NULL) {\n                    hi_s_free(vector);\n                    return NULL;\n                }\n\n                vector = new_vector;\n                vector[*argc] = current;\n                (*argc)++;\n                current = NULL;\n            }\n        } else {\n            /* Even on empty input string return something not NULL. */\n            if (vector == NULL) vector = hi_s_malloc(sizeof(void*));\n            return vector;\n        }\n    }\n\nerr:\n    while((*argc)--)\n        hi_sdsfree(vector[*argc]);\n    hi_s_free(vector);\n    if (current) hi_sdsfree(current);\n    *argc = 0;\n    return NULL;\n}\n\n/* Modify the string substituting all the occurrences of the set of\n * characters specified in the 'from' string to the corresponding character\n * in the 'to' array.\n *\n * For instance: hi_sdsmapchars(mystring, \"ho\", \"01\", 2)\n * will have the effect of turning the string \"hello\" into \"0ell1\".\n *\n * The function returns the hisds string pointer, that is always the same\n * as the input pointer since no resize is needed. */\nhisds hi_sdsmapchars(hisds s, const char *from, const char *to, size_t setlen) {\n    size_t j, i, l = hi_sdslen(s);\n\n    for (j = 0; j < l; j++) {\n        for (i = 0; i < setlen; i++) {\n            if (s[j] == from[i]) {\n                s[j] = to[i];\n                break;\n            }\n        }\n    }\n    return s;\n}\n\n/* Join an array of C strings using the specified separator (also a C string).\n * Returns the result as an hisds string. */\nhisds hi_sdsjoin(char **argv, int argc, char *sep) {\n    hisds join = hi_sdsempty();\n    int j;\n\n    for (j = 0; j < argc; j++) {\n        join = hi_sdscat(join, argv[j]);\n        if (j != argc-1) join = hi_sdscat(join,sep);\n    }\n    return join;\n}\n\n/* Like hi_sdsjoin, but joins an array of SDS strings. */\nhisds hi_sdsjoinsds(hisds *argv, int argc, const char *sep, size_t seplen) {\n    hisds join = hi_sdsempty();\n    int j;\n\n    for (j = 0; j < argc; j++) {\n        join = hi_sdscatsds(join, argv[j]);\n        if (j != argc-1) join = hi_sdscatlen(join,sep,seplen);\n    }\n    return join;\n}\n\n/* Wrappers to the allocators used by SDS. Note that SDS will actually\n * just use the macros defined into sdsalloc.h in order to avoid to pay\n * the overhead of function calls. Here we define these wrappers only for\n * the programs SDS is linked to, if they want to touch the SDS internals\n * even if they use a different allocator. */\nvoid *hi_sds_malloc(size_t size) { return hi_s_malloc(size); }\nvoid *hi_sds_realloc(void *ptr, size_t size) { return hi_s_realloc(ptr,size); }\nvoid hi_sds_free(void *ptr) { hi_s_free(ptr); }\n\n#if defined(HI_SDS_TEST_MAIN)\n#include <stdio.h>\n#include \"testhelp.h\"\n#include \"limits.h\"\n\n#define UNUSED(x) (void)(x)\nint hi_sdsTest(void) {\n    {\n        hisds x = hi_sdsnew(\"foo\"), y;\n\n        test_cond(\"Create a string and obtain the length\",\n            hi_sdslen(x) == 3 && memcmp(x,\"foo\\0\",4) == 0)\n\n        hi_sdsfree(x);\n        x = hi_sdsnewlen(\"foo\",2);\n        test_cond(\"Create a string with specified length\",\n            hi_sdslen(x) == 2 && memcmp(x,\"fo\\0\",3) == 0)\n\n        x = hi_sdscat(x,\"bar\");\n        test_cond(\"Strings concatenation\",\n            hi_sdslen(x) == 5 && memcmp(x,\"fobar\\0\",6) == 0);\n\n        x = hi_sdscpy(x,\"a\");\n        test_cond(\"hi_sdscpy() against an originally longer string\",\n            hi_sdslen(x) == 1 && memcmp(x,\"a\\0\",2) == 0)\n\n        x = hi_sdscpy(x,\"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\");\n        test_cond(\"hi_sdscpy() against an originally shorter string\",\n            hi_sdslen(x) == 33 &&\n            memcmp(x,\"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\\0\",33) == 0)\n\n        hi_sdsfree(x);\n        x = hi_sdscatprintf(hi_sdsempty(),\"%d\",123);\n        test_cond(\"hi_sdscatprintf() seems working in the base case\",\n            hi_sdslen(x) == 3 && memcmp(x,\"123\\0\",4) == 0)\n\n        hi_sdsfree(x);\n        x = hi_sdsnew(\"--\");\n        x = hi_sdscatfmt(x, \"Hello %s World %I,%I--\", \"Hi!\", LLONG_MIN,LLONG_MAX);\n        test_cond(\"hi_sdscatfmt() seems working in the base case\",\n            hi_sdslen(x) == 60 &&\n            memcmp(x,\"--Hello Hi! World -9223372036854775808,\"\n                     \"9223372036854775807--\",60) == 0)\n        printf(\"[%s]\\n\",x);\n\n        hi_sdsfree(x);\n        x = hi_sdsnew(\"--\");\n        x = hi_sdscatfmt(x, \"%u,%U--\", UINT_MAX, ULLONG_MAX);\n        test_cond(\"hi_sdscatfmt() seems working with unsigned numbers\",\n            hi_sdslen(x) == 35 &&\n            memcmp(x,\"--4294967295,18446744073709551615--\",35) == 0)\n\n        hi_sdsfree(x);\n        x = hi_sdsnew(\" x \");\n        hi_sdstrim(x,\" x\");\n        test_cond(\"hi_sdstrim() works when all chars match\",\n            hi_sdslen(x) == 0)\n\n        hi_sdsfree(x);\n        x = hi_sdsnew(\" x \");\n        hi_sdstrim(x,\" \");\n        test_cond(\"hi_sdstrim() works when a single char remains\",\n            hi_sdslen(x) == 1 && x[0] == 'x')\n\n        hi_sdsfree(x);\n        x = hi_sdsnew(\"xxciaoyyy\");\n        hi_sdstrim(x,\"xy\");\n        test_cond(\"hi_sdstrim() correctly trims characters\",\n            hi_sdslen(x) == 4 && memcmp(x,\"ciao\\0\",5) == 0)\n\n        y = hi_sdsdup(x);\n        hi_sdsrange(y,1,1);\n        test_cond(\"hi_sdsrange(...,1,1)\",\n            hi_sdslen(y) == 1 && memcmp(y,\"i\\0\",2) == 0)\n\n        hi_sdsfree(y);\n        y = hi_sdsdup(x);\n        hi_sdsrange(y,1,-1);\n        test_cond(\"hi_sdsrange(...,1,-1)\",\n            hi_sdslen(y) == 3 && memcmp(y,\"iao\\0\",4) == 0)\n\n        hi_sdsfree(y);\n        y = hi_sdsdup(x);\n        hi_sdsrange(y,-2,-1);\n        test_cond(\"hi_sdsrange(...,-2,-1)\",\n            hi_sdslen(y) == 2 && memcmp(y,\"ao\\0\",3) == 0)\n\n        hi_sdsfree(y);\n        y = hi_sdsdup(x);\n        hi_sdsrange(y,2,1);\n        test_cond(\"hi_sdsrange(...,2,1)\",\n            hi_sdslen(y) == 0 && memcmp(y,\"\\0\",1) == 0)\n\n        hi_sdsfree(y);\n        y = hi_sdsdup(x);\n        hi_sdsrange(y,1,100);\n        test_cond(\"hi_sdsrange(...,1,100)\",\n            hi_sdslen(y) == 3 && memcmp(y,\"iao\\0\",4) == 0)\n\n        hi_sdsfree(y);\n        y = hi_sdsdup(x);\n        hi_sdsrange(y,100,100);\n        test_cond(\"hi_sdsrange(...,100,100)\",\n            hi_sdslen(y) == 0 && memcmp(y,\"\\0\",1) == 0)\n\n        hi_sdsfree(y);\n        hi_sdsfree(x);\n        x = hi_sdsnew(\"foo\");\n        y = hi_sdsnew(\"foa\");\n        test_cond(\"hi_sdscmp(foo,foa)\", hi_sdscmp(x,y) > 0)\n\n        hi_sdsfree(y);\n        hi_sdsfree(x);\n        x = hi_sdsnew(\"bar\");\n        y = hi_sdsnew(\"bar\");\n        test_cond(\"hi_sdscmp(bar,bar)\", hi_sdscmp(x,y) == 0)\n\n        hi_sdsfree(y);\n        hi_sdsfree(x);\n        x = hi_sdsnew(\"aar\");\n        y = hi_sdsnew(\"bar\");\n        test_cond(\"hi_sdscmp(bar,bar)\", hi_sdscmp(x,y) < 0)\n\n        hi_sdsfree(y);\n        hi_sdsfree(x);\n        x = hi_sdsnewlen(\"\\a\\n\\0foo\\r\",7);\n        y = hi_sdscatrepr(hi_sdsempty(),x,hi_sdslen(x));\n        test_cond(\"hi_sdscatrepr(...data...)\",\n            memcmp(y,\"\\\"\\\\a\\\\n\\\\x00foo\\\\r\\\"\",15) == 0)\n\n        {\n            unsigned int oldfree;\n            char *p;\n            int step = 10, j, i;\n\n            hi_sdsfree(x);\n            hi_sdsfree(y);\n            x = hi_sdsnew(\"0\");\n            test_cond(\"hi_sdsnew() free/len buffers\", hi_sdslen(x) == 1 && hi_sdsavail(x) == 0);\n\n            /* Run the test a few times in order to hit the first two\n             * SDS header types. */\n            for (i = 0; i < 10; i++) {\n                int oldlen = hi_sdslen(x);\n                x = hi_sdsMakeRoomFor(x,step);\n                int type = x[-1]&HI_SDS_TYPE_MASK;\n\n                test_cond(\"sdsMakeRoomFor() len\", hi_sdslen(x) == oldlen);\n                if (type != HI_SDS_TYPE_5) {\n                    test_cond(\"hi_sdsMakeRoomFor() free\", hi_sdsavail(x) >= step);\n                    oldfree = hi_sdsavail(x);\n                }\n                p = x+oldlen;\n                for (j = 0; j < step; j++) {\n                    p[j] = 'A'+j;\n                }\n                hi_sdsIncrLen(x,step);\n            }\n            test_cond(\"hi_sdsMakeRoomFor() content\",\n                memcmp(\"0ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJ\",x,101) == 0);\n            test_cond(\"sdsMakeRoomFor() final length\",hi_sdslen(x)==101);\n\n            hi_sdsfree(x);\n        }\n    }\n    test_report();\n    return 0;\n}\n#endif\n\n#ifdef HI_SDS_TEST_MAIN\nint main(void) {\n    return hi_sdsTest();\n}\n#endif\n"
  },
  {
    "path": "deps/hiredis/sds.h",
    "content": "/* SDSLib 2.0 -- A C dynamic strings library\n *\n * Copyright (c) 2006-2015, Salvatore Sanfilippo <antirez at gmail dot com>\n * Copyright (c) 2015, Oran Agra\n * Copyright (c) 2015, Redis Labs, Inc\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef HIREDIS_SDS_H\n#define HIREDIS_SDS_H\n\n#define HI_SDS_MAX_PREALLOC (1024*1024)\n#ifdef _MSC_VER\n#define __attribute__(x)\ntypedef long long ssize_t;\n#define SSIZE_MAX (LLONG_MAX >> 1)\n#endif\n\n#include <sys/types.h>\n#include <stdarg.h>\n#include <stdint.h>\n\ntypedef char *hisds;\n\n/* Note: sdshdr5 is never used, we just access the flags byte directly.\n * However is here to document the layout of type 5 SDS strings. */\nstruct __attribute__ ((__packed__)) hisdshdr5 {\n    unsigned char flags; /* 3 lsb of type, and 5 msb of string length */\n#ifndef __cplusplus\n    char buf[];\n#endif\n};\nstruct __attribute__ ((__packed__)) hisdshdr8 {\n    uint8_t len; /* used */\n    uint8_t alloc; /* excluding the header and null terminator */\n    unsigned char flags; /* 3 lsb of type, 5 unused bits */\n#ifndef __cplusplus\n    char buf[];\n#endif\n};\nstruct __attribute__ ((__packed__)) hisdshdr16 {\n    uint16_t len; /* used */\n    uint16_t alloc; /* excluding the header and null terminator */\n    unsigned char flags; /* 3 lsb of type, 5 unused bits */\n#ifndef __cplusplus\n    char buf[];\n#endif\n};\nstruct __attribute__ ((__packed__)) hisdshdr32 {\n    uint32_t len; /* used */\n    uint32_t alloc; /* excluding the header and null terminator */\n    unsigned char flags; /* 3 lsb of type, 5 unused bits */\n#ifndef __cplusplus\n    char buf[];\n#endif\n};\nstruct __attribute__ ((__packed__)) hisdshdr64 {\n    uint64_t len; /* used */\n    uint64_t alloc; /* excluding the header and null terminator */\n    unsigned char flags; /* 3 lsb of type, 5 unused bits */\n#ifndef __cplusplus\n    char buf[];\n#endif\n};\n\n#define HI_SDS_TYPE_5  0\n#define HI_SDS_TYPE_8  1\n#define HI_SDS_TYPE_16 2\n#define HI_SDS_TYPE_32 3\n#define HI_SDS_TYPE_64 4\n#define HI_SDS_TYPE_MASK 7\n#define HI_SDS_TYPE_BITS 3\n#define HI_SDS_HDR_VAR(T,s) struct hisdshdr##T *sh = (struct hisdshdr##T *)((s)-(sizeof(struct hisdshdr##T)));\n#define HI_SDS_HDR(T,s) ((struct hisdshdr##T *)((s)-(sizeof(struct hisdshdr##T))))\n#define HI_SDS_TYPE_5_LEN(f) ((f)>>HI_SDS_TYPE_BITS)\n\nstatic inline size_t hi_sdslen(const hisds s) {\n    unsigned char flags = s[-1];\n    \n    switch(__builtin_expect((flags&HI_SDS_TYPE_MASK), HI_SDS_TYPE_5)) {\n        case HI_SDS_TYPE_5:\n            return HI_SDS_TYPE_5_LEN(flags);\n        case HI_SDS_TYPE_8:\n            return HI_SDS_HDR(8,s)->len;\n        case HI_SDS_TYPE_16:\n            return HI_SDS_HDR(16,s)->len;\n        case HI_SDS_TYPE_32:\n            return HI_SDS_HDR(32,s)->len;\n        case HI_SDS_TYPE_64:\n            return HI_SDS_HDR(64,s)->len;\n    }\n    return 0;\n}\n\nstatic inline size_t hi_sdsavail(const hisds s) {\n    unsigned char flags = s[-1];\n    switch(flags&HI_SDS_TYPE_MASK) {\n        case HI_SDS_TYPE_5: {\n            return 0;\n        }\n        case HI_SDS_TYPE_8: {\n            HI_SDS_HDR_VAR(8,s);\n            return sh->alloc - sh->len;\n        }\n        case HI_SDS_TYPE_16: {\n            HI_SDS_HDR_VAR(16,s);\n            return sh->alloc - sh->len;\n        }\n        case HI_SDS_TYPE_32: {\n            HI_SDS_HDR_VAR(32,s);\n            return sh->alloc - sh->len;\n        }\n        case HI_SDS_TYPE_64: {\n            HI_SDS_HDR_VAR(64,s);\n            return sh->alloc - sh->len;\n        }\n    }\n    return 0;\n}\n\nstatic inline void hi_sdssetlen(hisds s, size_t newlen) {\n    unsigned char flags = s[-1];\n    switch(flags&HI_SDS_TYPE_MASK) {\n        case HI_SDS_TYPE_5:\n            {\n                unsigned char *fp = ((unsigned char*)s)-1;\n                *fp = (unsigned char)(HI_SDS_TYPE_5 | (newlen << HI_SDS_TYPE_BITS));\n            }\n            break;\n        case HI_SDS_TYPE_8:\n            HI_SDS_HDR(8,s)->len = (uint8_t)newlen;\n            break;\n        case HI_SDS_TYPE_16:\n            HI_SDS_HDR(16,s)->len = (uint16_t)newlen;\n            break;\n        case HI_SDS_TYPE_32:\n            HI_SDS_HDR(32,s)->len = (uint32_t)newlen;\n            break;\n        case HI_SDS_TYPE_64:\n            HI_SDS_HDR(64,s)->len = (uint64_t)newlen;\n            break;\n    }\n}\n\nstatic inline void hi_sdsinclen(hisds s, size_t inc) {\n    unsigned char flags = s[-1];\n    switch(flags&HI_SDS_TYPE_MASK) {\n        case HI_SDS_TYPE_5:\n            {\n                unsigned char *fp = ((unsigned char*)s)-1;\n                unsigned char newlen = HI_SDS_TYPE_5_LEN(flags)+(unsigned char)inc;\n                *fp = HI_SDS_TYPE_5 | (newlen << HI_SDS_TYPE_BITS);\n            }\n            break;\n        case HI_SDS_TYPE_8:\n            HI_SDS_HDR(8,s)->len += (uint8_t)inc;\n            break;\n        case HI_SDS_TYPE_16:\n            HI_SDS_HDR(16,s)->len += (uint16_t)inc;\n            break;\n        case HI_SDS_TYPE_32:\n            HI_SDS_HDR(32,s)->len += (uint32_t)inc;\n            break;\n        case HI_SDS_TYPE_64:\n            HI_SDS_HDR(64,s)->len += (uint64_t)inc;\n            break;\n    }\n}\n\n/* hi_sdsalloc() = hi_sdsavail() + hi_sdslen() */\nstatic inline size_t hi_sdsalloc(const hisds s) {\n    unsigned char flags = s[-1];\n    switch(flags & HI_SDS_TYPE_MASK) {\n        case HI_SDS_TYPE_5:\n            return HI_SDS_TYPE_5_LEN(flags);\n        case HI_SDS_TYPE_8:\n            return HI_SDS_HDR(8,s)->alloc;\n        case HI_SDS_TYPE_16:\n            return HI_SDS_HDR(16,s)->alloc;\n        case HI_SDS_TYPE_32:\n            return HI_SDS_HDR(32,s)->alloc;\n        case HI_SDS_TYPE_64:\n            return HI_SDS_HDR(64,s)->alloc;\n    }\n    return 0;\n}\n\nstatic inline void hi_sdssetalloc(hisds s, size_t newlen) {\n    unsigned char flags = s[-1];\n    switch(flags&HI_SDS_TYPE_MASK) {\n        case HI_SDS_TYPE_5:\n            /* Nothing to do, this type has no total allocation info. */\n            break;\n        case HI_SDS_TYPE_8:\n            HI_SDS_HDR(8,s)->alloc = (uint8_t)newlen;\n            break;\n        case HI_SDS_TYPE_16:\n            HI_SDS_HDR(16,s)->alloc = (uint16_t)newlen;\n            break;\n        case HI_SDS_TYPE_32:\n            HI_SDS_HDR(32,s)->alloc = (uint32_t)newlen;\n            break;\n        case HI_SDS_TYPE_64:\n            HI_SDS_HDR(64,s)->alloc = (uint64_t)newlen;\n            break;\n    }\n}\n\nhisds hi_sdsnewlen(const void *init, size_t initlen);\nhisds hi_sdsnew(const char *init);\nhisds hi_sdsempty(void);\nhisds hi_sdsdup(const hisds s);\nvoid  hi_sdsfree(hisds s);\nhisds hi_sdsgrowzero(hisds s, size_t len);\nhisds hi_sdscatlen(hisds s, const void *t, size_t len);\nhisds hi_sdscat(hisds s, const char *t);\nhisds hi_sdscatsds(hisds s, const hisds t);\nhisds hi_sdscpylen(hisds s, const char *t, size_t len);\nhisds hi_sdscpy(hisds s, const char *t);\n\nhisds hi_sdscatvprintf(hisds s, const char *fmt, va_list ap);\n#ifdef __GNUC__\nhisds hi_sdscatprintf(hisds s, const char *fmt, ...)\n    __attribute__((format(printf, 2, 3)));\n#else\nhisds hi_sdscatprintf(hisds s, const char *fmt, ...);\n#endif\n\nhisds hi_sdscatfmt(hisds s, char const *fmt, ...);\nhisds hi_sdstrim(hisds s, const char *cset);\nint hi_sdsrange(hisds s, ssize_t start, ssize_t end);\nvoid hi_sdsupdatelen(hisds s);\nvoid hi_sdsclear(hisds s);\nint hi_sdscmp(const hisds s1, const hisds s2);\nhisds *hi_sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count);\nvoid hi_sdsfreesplitres(hisds *tokens, int count);\nvoid hi_sdstolower(hisds s);\nvoid hi_sdstoupper(hisds s);\nhisds hi_sdsfromlonglong(long long value);\nhisds hi_sdscatrepr(hisds s, const char *p, size_t len);\nhisds *hi_sdssplitargs(const char *line, int *argc);\nhisds hi_sdsmapchars(hisds s, const char *from, const char *to, size_t setlen);\nhisds hi_sdsjoin(char **argv, int argc, char *sep);\nhisds hi_sdsjoinsds(hisds *argv, int argc, const char *sep, size_t seplen);\n\n/* Low level functions exposed to the user API */\nhisds hi_sdsMakeRoomFor(hisds s, size_t addlen);\nvoid hi_sdsIncrLen(hisds s, int incr);\nhisds hi_sdsRemoveFreeSpace(hisds s);\nsize_t hi_sdsAllocSize(hisds s);\nvoid *hi_sdsAllocPtr(hisds s);\n\n/* Export the allocator used by SDS to the program using SDS.\n * Sometimes the program SDS is linked to, may use a different set of\n * allocators, but may want to allocate or free things that SDS will\n * respectively free or allocate. */\nvoid *hi_sds_malloc(size_t size);\nvoid *hi_sds_realloc(void *ptr, size_t size);\nvoid hi_sds_free(void *ptr);\n\n#ifdef REDIS_TEST\nint hi_sdsTest(int argc, char *argv[]);\n#endif\n\n#endif /* HIREDIS_SDS_H */\n"
  },
  {
    "path": "deps/hiredis/sdsalloc.h",
    "content": "/* SDSLib 2.0 -- A C dynamic strings library\n *\n * Copyright (c) 2006-2015, Salvatore Sanfilippo <antirez at gmail dot com>\n * Copyright (c) 2015, Oran Agra\n * Copyright (c) 2015, Redis Labs, Inc\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n/* SDS allocator selection.\n *\n * This file is used in order to change the SDS allocator at compile time.\n * Just define the following defines to what you want to use. Also add\n * the include of your alternate allocator if needed (not needed in order\n * to use the default libc allocator). */\n\n#include \"alloc.h\"\n\n#define hi_s_malloc hi_malloc\n#define hi_s_realloc hi_realloc\n#define hi_s_free hi_free\n"
  },
  {
    "path": "deps/hiredis/sockcompat.c",
    "content": "/*\n * Copyright (c) 2019, Marcus Geelnard <m at bitsnbites dot eu>\n *\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#define REDIS_SOCKCOMPAT_IMPLEMENTATION\n#include \"sockcompat.h\"\n\n#ifdef _WIN32\nstatic int _wsaErrorToErrno(int err) {\n    switch (err) {\n        case WSAEWOULDBLOCK:\n            return EWOULDBLOCK;\n        case WSAEINPROGRESS:\n            return EINPROGRESS;\n        case WSAEALREADY:\n            return EALREADY;\n        case WSAENOTSOCK:\n            return ENOTSOCK;\n        case WSAEDESTADDRREQ:\n            return EDESTADDRREQ;\n        case WSAEMSGSIZE:\n            return EMSGSIZE;\n        case WSAEPROTOTYPE:\n            return EPROTOTYPE;\n        case WSAENOPROTOOPT:\n            return ENOPROTOOPT;\n        case WSAEPROTONOSUPPORT:\n            return EPROTONOSUPPORT;\n        case WSAEOPNOTSUPP:\n            return EOPNOTSUPP;\n        case WSAEAFNOSUPPORT:\n            return EAFNOSUPPORT;\n        case WSAEADDRINUSE:\n            return EADDRINUSE;\n        case WSAEADDRNOTAVAIL:\n            return EADDRNOTAVAIL;\n        case WSAENETDOWN:\n            return ENETDOWN;\n        case WSAENETUNREACH:\n            return ENETUNREACH;\n        case WSAENETRESET:\n            return ENETRESET;\n        case WSAECONNABORTED:\n            return ECONNABORTED;\n        case WSAECONNRESET:\n            return ECONNRESET;\n        case WSAENOBUFS:\n            return ENOBUFS;\n        case WSAEISCONN:\n            return EISCONN;\n        case WSAENOTCONN:\n            return ENOTCONN;\n        case WSAETIMEDOUT:\n            return ETIMEDOUT;\n        case WSAECONNREFUSED:\n            return ECONNREFUSED;\n        case WSAELOOP:\n            return ELOOP;\n        case WSAENAMETOOLONG:\n            return ENAMETOOLONG;\n        case WSAEHOSTUNREACH:\n            return EHOSTUNREACH;\n        case WSAENOTEMPTY:\n            return ENOTEMPTY;\n        default:\n            /* We just return a generic I/O error if we could not find a relevant error. */\n            return EIO;\n    }\n}\n\nstatic void _updateErrno(int success) {\n    errno = success ? 0 : _wsaErrorToErrno(WSAGetLastError());\n}\n\nstatic int _initWinsock() {\n    static int s_initialized = 0;\n    if (!s_initialized) {\n        static WSADATA wsadata;\n        int err = WSAStartup(MAKEWORD(2,2), &wsadata);\n        if (err != 0) {\n            errno = _wsaErrorToErrno(err);\n            return 0;\n        }\n        s_initialized = 1;\n    }\n    return 1;\n}\n\nint win32_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res) {\n    /* Note: This function is likely to be called before other functions, so run init here. */\n    if (!_initWinsock()) {\n        return EAI_FAIL;\n    }\n\n    switch (getaddrinfo(node, service, hints, res)) {\n        case 0:                     return 0;\n        case WSATRY_AGAIN:          return EAI_AGAIN;\n        case WSAEINVAL:             return EAI_BADFLAGS;\n        case WSAEAFNOSUPPORT:       return EAI_FAMILY;\n        case WSA_NOT_ENOUGH_MEMORY: return EAI_MEMORY;\n        case WSAHOST_NOT_FOUND:     return EAI_NONAME;\n        case WSATYPE_NOT_FOUND:     return EAI_SERVICE;\n        case WSAESOCKTNOSUPPORT:    return EAI_SOCKTYPE;\n        default:                    return EAI_FAIL;     /* Including WSANO_RECOVERY */\n    }\n}\n\nconst char *win32_gai_strerror(int errcode) {\n    switch (errcode) {\n        case 0:            errcode = 0;                     break;\n        case EAI_AGAIN:    errcode = WSATRY_AGAIN;          break;\n        case EAI_BADFLAGS: errcode = WSAEINVAL;             break;\n        case EAI_FAMILY:   errcode = WSAEAFNOSUPPORT;       break;\n        case EAI_MEMORY:   errcode = WSA_NOT_ENOUGH_MEMORY; break;\n        case EAI_NONAME:   errcode = WSAHOST_NOT_FOUND;     break;\n        case EAI_SERVICE:  errcode = WSATYPE_NOT_FOUND;     break;\n        case EAI_SOCKTYPE: errcode = WSAESOCKTNOSUPPORT;    break;\n        default:           errcode = WSANO_RECOVERY;        break; /* Including EAI_FAIL */\n    }\n    return gai_strerror(errcode);\n}\n\nvoid win32_freeaddrinfo(struct addrinfo *res) {\n    freeaddrinfo(res);\n}\n\nSOCKET win32_socket(int domain, int type, int protocol) {\n    SOCKET s;\n\n    /* Note: This function is likely to be called before other functions, so run init here. */\n    if (!_initWinsock()) {\n        return INVALID_SOCKET;\n    }\n\n    _updateErrno((s = socket(domain, type, protocol)) != INVALID_SOCKET);\n    return s;\n}\n\nint win32_ioctl(SOCKET fd, unsigned long request, unsigned long *argp) {\n    int ret = ioctlsocket(fd, (long)request, argp);\n    _updateErrno(ret != SOCKET_ERROR);\n    return ret != SOCKET_ERROR ? ret : -1;\n}\n\nint win32_bind(SOCKET sockfd, const struct sockaddr *addr, socklen_t addrlen) {\n    int ret = bind(sockfd, addr, addrlen);\n    _updateErrno(ret != SOCKET_ERROR);\n    return ret != SOCKET_ERROR ? ret : -1;\n}\n\nint win32_connect(SOCKET sockfd, const struct sockaddr *addr, socklen_t addrlen) {\n    int ret = connect(sockfd, addr, addrlen);\n    _updateErrno(ret != SOCKET_ERROR);\n\n    /* For Winsock connect(), the WSAEWOULDBLOCK error means the same thing as\n     * EINPROGRESS for POSIX connect(), so we do that translation to keep POSIX\n     * logic consistent. */\n    if (errno == EWOULDBLOCK) {\n        errno = EINPROGRESS;\n    }\n\n    return ret != SOCKET_ERROR ? ret : -1;\n}\n\nint win32_getsockopt(SOCKET sockfd, int level, int optname, void *optval, socklen_t *optlen) {\n    int ret = 0;\n    if ((level == SOL_SOCKET) && ((optname == SO_RCVTIMEO) || (optname == SO_SNDTIMEO))) {\n        if (*optlen >= sizeof (struct timeval)) {\n            struct timeval *tv = optval;\n            DWORD timeout = 0;\n            socklen_t dwlen = 0;\n            ret = getsockopt(sockfd, level, optname, (char *)&timeout, &dwlen);\n            tv->tv_sec = timeout / 1000;\n            tv->tv_usec = (timeout * 1000) % 1000000;\n        } else {\n            ret = WSAEFAULT;\n        }\n        *optlen = sizeof (struct timeval);\n    } else {\n        ret = getsockopt(sockfd, level, optname, (char*)optval, optlen);\n    }\n    _updateErrno(ret != SOCKET_ERROR);\n    return ret != SOCKET_ERROR ? ret : -1;\n}\n\nint win32_setsockopt(SOCKET sockfd, int level, int optname, const void *optval, socklen_t optlen) {\n    int ret = 0;\n    if ((level == SOL_SOCKET) && ((optname == SO_RCVTIMEO) || (optname == SO_SNDTIMEO))) {\n        const struct timeval *tv = optval;\n        DWORD timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;\n        ret = setsockopt(sockfd, level, optname, (const char*)&timeout, sizeof(DWORD));\n    } else {\n        ret = setsockopt(sockfd, level, optname, (const char*)optval, optlen);\n    }\n    _updateErrno(ret != SOCKET_ERROR);\n    return ret != SOCKET_ERROR ? ret : -1;\n}\n\nint win32_close(SOCKET fd) {\n    int ret = closesocket(fd);\n    _updateErrno(ret != SOCKET_ERROR);\n    return ret != SOCKET_ERROR ? ret : -1;\n}\n\nssize_t win32_recv(SOCKET sockfd, void *buf, size_t len, int flags) {\n    int ret = recv(sockfd, (char*)buf, (int)len, flags);\n    _updateErrno(ret != SOCKET_ERROR);\n    return ret != SOCKET_ERROR ? ret : -1;\n}\n\nssize_t win32_send(SOCKET sockfd, const void *buf, size_t len, int flags) {\n    int ret = send(sockfd, (const char*)buf, (int)len, flags);\n    _updateErrno(ret != SOCKET_ERROR);\n    return ret != SOCKET_ERROR ? ret : -1;\n}\n\nint win32_poll(struct pollfd *fds, nfds_t nfds, int timeout) {\n    int ret = WSAPoll(fds, nfds, timeout);\n    _updateErrno(ret != SOCKET_ERROR);\n    return ret != SOCKET_ERROR ? ret : -1;\n}\n#endif /* _WIN32 */\n"
  },
  {
    "path": "deps/hiredis/sockcompat.h",
    "content": "/*\n * Copyright (c) 2019, Marcus Geelnard <m at bitsnbites dot eu>\n *\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __SOCKCOMPAT_H\n#define __SOCKCOMPAT_H\n\n#ifndef _WIN32\n/* For POSIX systems we use the standard BSD socket API. */\n#include <unistd.h>\n#include <sys/socket.h>\n#include <sys/select.h>\n#include <sys/un.h>\n#include <netinet/in.h>\n#include <netinet/tcp.h>\n#include <arpa/inet.h>\n#include <netdb.h>\n#include <poll.h>\n#else\n/* For Windows we use winsock. */\n#undef _WIN32_WINNT\n#define _WIN32_WINNT 0x0600 /* To get WSAPoll etc. */\n#include <winsock2.h>\n#include <ws2tcpip.h>\n#include <stddef.h>\n#include <errno.h>\n\n#ifdef _MSC_VER\ntypedef long long ssize_t;\n#endif\n\n/* Emulate the parts of the BSD socket API that we need (override the winsock signatures). */\nint win32_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res);\nconst char *win32_gai_strerror(int errcode);\nvoid win32_freeaddrinfo(struct addrinfo *res);\nSOCKET win32_socket(int domain, int type, int protocol);\nint win32_ioctl(SOCKET fd, unsigned long request, unsigned long *argp);\nint win32_bind(SOCKET sockfd, const struct sockaddr *addr, socklen_t addrlen);\nint win32_connect(SOCKET sockfd, const struct sockaddr *addr, socklen_t addrlen);\nint win32_getsockopt(SOCKET sockfd, int level, int optname, void *optval, socklen_t *optlen);\nint win32_setsockopt(SOCKET sockfd, int level, int optname, const void *optval, socklen_t optlen);\nint win32_close(SOCKET fd);\nssize_t win32_recv(SOCKET sockfd, void *buf, size_t len, int flags);\nssize_t win32_send(SOCKET sockfd, const void *buf, size_t len, int flags);\ntypedef ULONG nfds_t;\nint win32_poll(struct pollfd *fds, nfds_t nfds, int timeout);\n\n#ifndef REDIS_SOCKCOMPAT_IMPLEMENTATION\n#define getaddrinfo(node, service, hints, res) win32_getaddrinfo(node, service, hints, res)\n#undef gai_strerror\n#define gai_strerror(errcode) win32_gai_strerror(errcode)\n#define freeaddrinfo(res) win32_freeaddrinfo(res)\n#define socket(domain, type, protocol) win32_socket(domain, type, protocol)\n#define ioctl(fd, request, argp) win32_ioctl(fd, request, argp)\n#define bind(sockfd, addr, addrlen) win32_bind(sockfd, addr, addrlen)\n#define connect(sockfd, addr, addrlen) win32_connect(sockfd, addr, addrlen)\n#define getsockopt(sockfd, level, optname, optval, optlen) win32_getsockopt(sockfd, level, optname, optval, optlen)\n#define setsockopt(sockfd, level, optname, optval, optlen) win32_setsockopt(sockfd, level, optname, optval, optlen)\n#define close(fd) win32_close(fd)\n#define recv(sockfd, buf, len, flags) win32_recv(sockfd, buf, len, flags)\n#define send(sockfd, buf, len, flags) win32_send(sockfd, buf, len, flags)\n#define poll(fds, nfds, timeout) win32_poll(fds, nfds, timeout)\n#endif /* REDIS_SOCKCOMPAT_IMPLEMENTATION */\n#endif /* _WIN32 */\n\n#endif /* __SOCKCOMPAT_H */\n"
  },
  {
    "path": "deps/hiredis/ssl.c",
    "content": "/*\n * Copyright (c) 2009-2011, Salvatore Sanfilippo <antirez at gmail dot com>\n * Copyright (c) 2010-2011, Pieter Noordhuis <pcnoordhuis at gmail dot com>\n * Copyright (c) 2019, Redis Labs\n *\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"hiredis.h\"\n#include \"async.h\"\n\n#include <assert.h>\n#include <errno.h>\n#include <string.h>\n#ifdef _WIN32\n#include <windows.h>\n#else\n#include <pthread.h>\n#endif\n\n#include <openssl/ssl.h>\n#include <openssl/err.h>\n\n#include \"win32.h\"\n#include \"async_private.h\"\n#include \"hiredis_ssl.h\"\n\nvoid __redisSetError(redisContext *c, int type, const char *str);\n\nstruct redisSSLContext {\n    /* Associated OpenSSL SSL_CTX as created by redisCreateSSLContext() */\n    SSL_CTX *ssl_ctx;\n\n    /* Requested SNI, or NULL */\n    char *server_name;\n};\n\n/* The SSL connection context is attached to SSL/TLS connections as a privdata. */\ntypedef struct redisSSL {\n    /**\n     * OpenSSL SSL object.\n     */\n    SSL *ssl;\n\n    /**\n     * SSL_write() requires to be called again with the same arguments it was\n     * previously called with in the event of an SSL_read/SSL_write situation\n     */\n    size_t lastLen;\n\n    /** Whether the SSL layer requires read (possibly before a write) */\n    int wantRead;\n\n    /**\n     * Whether a write was requested prior to a read. If set, the write()\n     * should resume whenever a read takes place, if possible\n     */\n    int pendingWrite;\n} redisSSL;\n\n/* Forward declaration */\nredisContextFuncs redisContextSSLFuncs;\n\n/**\n * OpenSSL global initialization and locking handling callbacks.\n * Note that this is only required for OpenSSL < 1.1.0.\n */\n\n#if OPENSSL_VERSION_NUMBER < 0x10100000L\n#define HIREDIS_USE_CRYPTO_LOCKS\n#endif\n\n#ifdef HIREDIS_USE_CRYPTO_LOCKS\n#ifdef _WIN32\ntypedef CRITICAL_SECTION sslLockType;\nstatic void sslLockInit(sslLockType* l) {\n    InitializeCriticalSection(l);\n}\nstatic void sslLockAcquire(sslLockType* l) {\n    EnterCriticalSection(l);\n}\nstatic void sslLockRelease(sslLockType* l) {\n    LeaveCriticalSection(l);\n}\n#else\ntypedef pthread_mutex_t sslLockType;\nstatic void sslLockInit(sslLockType *l) {\n    pthread_mutex_init(l, NULL);\n}\nstatic void sslLockAcquire(sslLockType *l) {\n    pthread_mutex_lock(l);\n}\nstatic void sslLockRelease(sslLockType *l) {\n    pthread_mutex_unlock(l);\n}\n#endif\n\nstatic sslLockType* ossl_locks;\n\nstatic void opensslDoLock(int mode, int lkid, const char *f, int line) {\n    sslLockType *l = ossl_locks + lkid;\n\n    if (mode & CRYPTO_LOCK) {\n        sslLockAcquire(l);\n    } else {\n        sslLockRelease(l);\n    }\n\n    (void)f;\n    (void)line;\n}\n\nstatic int initOpensslLocks(void) {\n    unsigned ii, nlocks;\n    if (CRYPTO_get_locking_callback() != NULL) {\n        /* Someone already set the callback before us. Don't destroy it! */\n        return REDIS_OK;\n    }\n    nlocks = CRYPTO_num_locks();\n    ossl_locks = hi_malloc(sizeof(*ossl_locks) * nlocks);\n    if (ossl_locks == NULL)\n        return REDIS_ERR;\n\n    for (ii = 0; ii < nlocks; ii++) {\n        sslLockInit(ossl_locks + ii);\n    }\n    CRYPTO_set_locking_callback(opensslDoLock);\n    return REDIS_OK;\n}\n#endif /* HIREDIS_USE_CRYPTO_LOCKS */\n\nint redisInitOpenSSL(void)\n{\n    SSL_library_init();\n#ifdef HIREDIS_USE_CRYPTO_LOCKS\n    initOpensslLocks();\n#endif\n\n    return REDIS_OK;\n}\n\n/**\n * redisSSLContext helper context destruction.\n */\n\nconst char *redisSSLContextGetError(redisSSLContextError error)\n{\n    switch (error) {\n        case REDIS_SSL_CTX_NONE:\n            return \"No Error\";\n        case REDIS_SSL_CTX_CREATE_FAILED:\n            return \"Failed to create OpenSSL SSL_CTX\";\n        case REDIS_SSL_CTX_CERT_KEY_REQUIRED:\n            return \"Client cert and key must both be specified or skipped\";\n        case REDIS_SSL_CTX_CA_CERT_LOAD_FAILED:\n            return \"Failed to load CA Certificate or CA Path\";\n        case REDIS_SSL_CTX_CLIENT_CERT_LOAD_FAILED:\n            return \"Failed to load client certificate\";\n        case REDIS_SSL_CTX_PRIVATE_KEY_LOAD_FAILED:\n            return \"Failed to load private key\";\n        default:\n            return \"Unknown error code\";\n    }\n}\n\nvoid redisFreeSSLContext(redisSSLContext *ctx)\n{\n    if (!ctx)\n        return;\n\n    if (ctx->server_name) {\n        hi_free(ctx->server_name);\n        ctx->server_name = NULL;\n    }\n\n    if (ctx->ssl_ctx) {\n        SSL_CTX_free(ctx->ssl_ctx);\n        ctx->ssl_ctx = NULL;\n    }\n\n    hi_free(ctx);\n}\n\n\n/**\n * redisSSLContext helper context initialization.\n */\n\nredisSSLContext *redisCreateSSLContext(const char *cacert_filename, const char *capath,\n        const char *cert_filename, const char *private_key_filename,\n        const char *server_name, redisSSLContextError *error)\n{\n    redisSSLContext *ctx = hi_calloc(1, sizeof(redisSSLContext));\n    if (ctx == NULL)\n        goto error;\n\n    ctx->ssl_ctx = SSL_CTX_new(SSLv23_client_method());\n    if (!ctx->ssl_ctx) {\n        if (error) *error = REDIS_SSL_CTX_CREATE_FAILED;\n        goto error;\n    }\n\n    SSL_CTX_set_options(ctx->ssl_ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);\n    SSL_CTX_set_verify(ctx->ssl_ctx, SSL_VERIFY_PEER, NULL);\n\n    if ((cert_filename != NULL && private_key_filename == NULL) ||\n            (private_key_filename != NULL && cert_filename == NULL)) {\n        if (error) *error = REDIS_SSL_CTX_CERT_KEY_REQUIRED;\n        goto error;\n    }\n\n    if (capath || cacert_filename) {\n        if (!SSL_CTX_load_verify_locations(ctx->ssl_ctx, cacert_filename, capath)) {\n            if (error) *error = REDIS_SSL_CTX_CA_CERT_LOAD_FAILED;\n            goto error;\n        }\n    }\n\n    if (cert_filename) {\n        if (!SSL_CTX_use_certificate_chain_file(ctx->ssl_ctx, cert_filename)) {\n            if (error) *error = REDIS_SSL_CTX_CLIENT_CERT_LOAD_FAILED;\n            goto error;\n        }\n        if (!SSL_CTX_use_PrivateKey_file(ctx->ssl_ctx, private_key_filename, SSL_FILETYPE_PEM)) {\n            if (error) *error = REDIS_SSL_CTX_PRIVATE_KEY_LOAD_FAILED;\n            goto error;\n        }\n    }\n\n    if (server_name)\n        ctx->server_name = hi_strdup(server_name);\n\n    return ctx;\n\nerror:\n    redisFreeSSLContext(ctx);\n    return NULL;\n}\n\n/**\n * SSL Connection initialization.\n */\n\n\nstatic int redisSSLConnect(redisContext *c, SSL *ssl) {\n    if (c->privctx) {\n        __redisSetError(c, REDIS_ERR_OTHER, \"redisContext was already associated\");\n        return REDIS_ERR;\n    }\n\n    redisSSL *rssl = hi_calloc(1, sizeof(redisSSL));\n    if (rssl == NULL) {\n        __redisSetError(c, REDIS_ERR_OOM, \"Out of memory\");\n        return REDIS_ERR;\n    }\n\n    c->funcs = &redisContextSSLFuncs;\n    rssl->ssl = ssl;\n\n    SSL_set_mode(rssl->ssl, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);\n    SSL_set_fd(rssl->ssl, c->fd);\n    SSL_set_connect_state(rssl->ssl);\n\n    ERR_clear_error();\n    int rv = SSL_connect(rssl->ssl);\n    if (rv == 1) {\n        c->privctx = rssl;\n        return REDIS_OK;\n    }\n\n    rv = SSL_get_error(rssl->ssl, rv);\n    if (((c->flags & REDIS_BLOCK) == 0) &&\n        (rv == SSL_ERROR_WANT_READ || rv == SSL_ERROR_WANT_WRITE)) {\n        c->privctx = rssl;\n        return REDIS_OK;\n    }\n\n    if (c->err == 0) {\n        char err[512];\n        if (rv == SSL_ERROR_SYSCALL)\n            snprintf(err,sizeof(err)-1,\"SSL_connect failed: %s\",strerror(errno));\n        else {\n            unsigned long e = ERR_peek_last_error();\n            snprintf(err,sizeof(err)-1,\"SSL_connect failed: %s\",\n                    ERR_reason_error_string(e));\n        }\n        __redisSetError(c, REDIS_ERR_IO, err);\n    }\n\n    hi_free(rssl);\n    return REDIS_ERR;\n}\n\n/**\n * A wrapper around redisSSLConnect() for users who manage their own context and\n * create their own SSL object.\n */\n\nint redisInitiateSSL(redisContext *c, SSL *ssl) {\n    return redisSSLConnect(c, ssl);\n}\n\n/**\n * A wrapper around redisSSLConnect() for users who use redisSSLContext and don't\n * manage their own SSL objects.\n */\n\nint redisInitiateSSLWithContext(redisContext *c, redisSSLContext *redis_ssl_ctx)\n{\n    if (!c || !redis_ssl_ctx)\n        return REDIS_ERR;\n\n    /* We want to verify that redisSSLConnect() won't fail on this, as it will\n     * not own the SSL object in that case and we'll end up leaking.\n     */\n    if (c->privctx)\n        return REDIS_ERR;\n\n    SSL *ssl = SSL_new(redis_ssl_ctx->ssl_ctx);\n    if (!ssl) {\n        __redisSetError(c, REDIS_ERR_OTHER, \"Couldn't create new SSL instance\");\n        goto error;\n    }\n\n    if (redis_ssl_ctx->server_name) {\n        if (!SSL_set_tlsext_host_name(ssl, redis_ssl_ctx->server_name)) {\n            __redisSetError(c, REDIS_ERR_OTHER, \"Failed to set server_name/SNI\");\n            goto error;\n        }\n    }\n\n    return redisSSLConnect(c, ssl);\n\nerror:\n    if (ssl)\n        SSL_free(ssl);\n    return REDIS_ERR;\n}\n\nstatic int maybeCheckWant(redisSSL *rssl, int rv) {\n    /**\n     * If the error is WANT_READ or WANT_WRITE, the appropriate flags are set\n     * and true is returned. False is returned otherwise\n     */\n    if (rv == SSL_ERROR_WANT_READ) {\n        rssl->wantRead = 1;\n        return 1;\n    } else if (rv == SSL_ERROR_WANT_WRITE) {\n        rssl->pendingWrite = 1;\n        return 1;\n    } else {\n        return 0;\n    }\n}\n\n/**\n * Implementation of redisContextFuncs for SSL connections.\n */\n\nstatic void redisSSLFree(void *privctx){\n    redisSSL *rsc = privctx;\n\n    if (!rsc) return;\n    if (rsc->ssl) {\n        SSL_free(rsc->ssl);\n        rsc->ssl = NULL;\n    }\n    hi_free(rsc);\n}\n\nstatic ssize_t redisSSLRead(redisContext *c, char *buf, size_t bufcap) {\n    redisSSL *rssl = c->privctx;\n\n    int nread = SSL_read(rssl->ssl, buf, bufcap);\n    if (nread > 0) {\n        return nread;\n    } else if (nread == 0) {\n        __redisSetError(c, REDIS_ERR_EOF, \"Server closed the connection\");\n        return -1;\n    } else {\n        int err = SSL_get_error(rssl->ssl, nread);\n        if (c->flags & REDIS_BLOCK) {\n            /**\n             * In blocking mode, we should never end up in a situation where\n             * we get an error without it being an actual error, except\n             * in the case of EINTR, which can be spuriously received from\n             * debuggers or whatever.\n             */\n            if (errno == EINTR) {\n                return 0;\n            } else {\n                const char *msg = NULL;\n                if (errno == EAGAIN) {\n                    msg = \"Resource temporarily unavailable\";\n                }\n                __redisSetError(c, REDIS_ERR_IO, msg);\n                return -1;\n            }\n        }\n\n        /**\n         * We can very well get an EWOULDBLOCK/EAGAIN, however\n         */\n        if (maybeCheckWant(rssl, err)) {\n            return 0;\n        } else {\n            __redisSetError(c, REDIS_ERR_IO, NULL);\n            return -1;\n        }\n    }\n}\n\nstatic ssize_t redisSSLWrite(redisContext *c) {\n    redisSSL *rssl = c->privctx;\n\n    size_t len = rssl->lastLen ? rssl->lastLen : hi_sdslen(c->obuf);\n    int rv = SSL_write(rssl->ssl, c->obuf, len);\n\n    if (rv > 0) {\n        rssl->lastLen = 0;\n    } else if (rv < 0) {\n        rssl->lastLen = len;\n\n        int err = SSL_get_error(rssl->ssl, rv);\n        if ((c->flags & REDIS_BLOCK) == 0 && maybeCheckWant(rssl, err)) {\n            return 0;\n        } else {\n            __redisSetError(c, REDIS_ERR_IO, NULL);\n            return -1;\n        }\n    }\n    return rv;\n}\n\nstatic void redisSSLAsyncRead(redisAsyncContext *ac) {\n    int rv;\n    redisSSL *rssl = ac->c.privctx;\n    redisContext *c = &ac->c;\n\n    rssl->wantRead = 0;\n\n    if (rssl->pendingWrite) {\n        int done;\n\n        /* This is probably just a write event */\n        rssl->pendingWrite = 0;\n        rv = redisBufferWrite(c, &done);\n        if (rv == REDIS_ERR) {\n            __redisAsyncDisconnect(ac);\n            return;\n        } else if (!done) {\n            _EL_ADD_WRITE(ac);\n        }\n    }\n\n    rv = redisBufferRead(c);\n    if (rv == REDIS_ERR) {\n        __redisAsyncDisconnect(ac);\n    } else {\n        _EL_ADD_READ(ac);\n        redisProcessCallbacks(ac);\n    }\n}\n\nstatic void redisSSLAsyncWrite(redisAsyncContext *ac) {\n    int rv, done = 0;\n    redisSSL *rssl = ac->c.privctx;\n    redisContext *c = &ac->c;\n\n    rssl->pendingWrite = 0;\n    rv = redisBufferWrite(c, &done);\n    if (rv == REDIS_ERR) {\n        __redisAsyncDisconnect(ac);\n        return;\n    }\n\n    if (!done) {\n        if (rssl->wantRead) {\n            /* Need to read-before-write */\n            rssl->pendingWrite = 1;\n            _EL_DEL_WRITE(ac);\n        } else {\n            /* No extra reads needed, just need to write more */\n            _EL_ADD_WRITE(ac);\n        }\n    } else {\n        /* Already done! */\n        _EL_DEL_WRITE(ac);\n    }\n\n    /* Always reschedule a read */\n    _EL_ADD_READ(ac);\n}\n\nredisContextFuncs redisContextSSLFuncs = {\n    .free_privctx = redisSSLFree,\n    .async_read = redisSSLAsyncRead,\n    .async_write = redisSSLAsyncWrite,\n    .read = redisSSLRead,\n    .write = redisSSLWrite\n};\n\n"
  },
  {
    "path": "deps/hiredis/test.c",
    "content": "#include \"fmacros.h\"\n#include \"sockcompat.h\"\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#ifndef _WIN32\n#include <strings.h>\n#include <sys/time.h>\n#endif\n#include <assert.h>\n#include <signal.h>\n#include <errno.h>\n#include <limits.h>\n\n#include \"hiredis.h\"\n#include \"async.h\"\n#ifdef HIREDIS_TEST_SSL\n#include \"hiredis_ssl.h\"\n#endif\n#include \"net.h\"\n#include \"win32.h\"\n\nenum connection_type {\n    CONN_TCP,\n    CONN_UNIX,\n    CONN_FD,\n    CONN_SSL\n};\n\nstruct config {\n    enum connection_type type;\n\n    struct {\n        const char *host;\n        int port;\n        struct timeval timeout;\n    } tcp;\n\n    struct {\n        const char *path;\n    } unix_sock;\n\n    struct {\n        const char *host;\n        int port;\n        const char *ca_cert;\n        const char *cert;\n        const char *key;\n    } ssl;\n};\n\nstruct privdata {\n    int dtor_counter;\n};\n\nstruct pushCounters {\n    int nil;\n    int str;\n};\n\n#ifdef HIREDIS_TEST_SSL\nredisSSLContext *_ssl_ctx = NULL;\n#endif\n\n/* The following lines make up our testing \"framework\" :) */\nstatic int tests = 0, fails = 0, skips = 0;\n#define test(_s) { printf(\"#%02d \", ++tests); printf(_s); }\n#define test_cond(_c) if(_c) printf(\"\\033[0;32mPASSED\\033[0;0m\\n\"); else {printf(\"\\033[0;31mFAILED\\033[0;0m\\n\"); fails++;}\n#define test_skipped() { printf(\"\\033[01;33mSKIPPED\\033[0;0m\\n\"); skips++; }\n\nstatic long long usec(void) {\n#ifndef _MSC_VER\n    struct timeval tv;\n    gettimeofday(&tv,NULL);\n    return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;\n#else\n    FILETIME ft;\n    GetSystemTimeAsFileTime(&ft);\n    return (((long long)ft.dwHighDateTime << 32) | ft.dwLowDateTime) / 10;\n#endif\n}\n\n/* The assert() calls below have side effects, so we need assert()\n * even if we are compiling without asserts (-DNDEBUG). */\n#ifdef NDEBUG\n#undef assert\n#define assert(e) (void)(e)\n#endif\n\n/* Helper to extract Redis version information.  Aborts on any failure. */\n#define REDIS_VERSION_FIELD \"redis_version:\"\nvoid get_redis_version(redisContext *c, int *majorptr, int *minorptr) {\n    redisReply *reply;\n    char *eptr, *s, *e;\n    int major, minor;\n\n    reply = redisCommand(c, \"INFO\");\n    if (reply == NULL || c->err || reply->type != REDIS_REPLY_STRING)\n        goto abort;\n    if ((s = strstr(reply->str, REDIS_VERSION_FIELD)) == NULL)\n        goto abort;\n\n    s += strlen(REDIS_VERSION_FIELD);\n\n    /* We need a field terminator and at least 'x.y.z' (5) bytes of data */\n    if ((e = strstr(s, \"\\r\\n\")) == NULL || (e - s) < 5)\n        goto abort;\n\n    /* Extract version info */\n    major = strtol(s, &eptr, 10);\n    if (*eptr != '.') goto abort;\n    minor = strtol(eptr+1, NULL, 10);\n\n    /* Push info the caller wants */\n    if (majorptr) *majorptr = major;\n    if (minorptr) *minorptr = minor;\n\n    freeReplyObject(reply);\n    return;\n\nabort:\n    freeReplyObject(reply);\n    fprintf(stderr, \"Error:  Cannot determine Redis version, aborting\\n\");\n    exit(1);\n}\n\nstatic redisContext *select_database(redisContext *c) {\n    redisReply *reply;\n\n    /* Switch to DB 9 for testing, now that we know we can chat. */\n    reply = redisCommand(c,\"SELECT 9\");\n    assert(reply != NULL);\n    freeReplyObject(reply);\n\n    /* Make sure the DB is emtpy */\n    reply = redisCommand(c,\"DBSIZE\");\n    assert(reply != NULL);\n    if (reply->type == REDIS_REPLY_INTEGER && reply->integer == 0) {\n        /* Awesome, DB 9 is empty and we can continue. */\n        freeReplyObject(reply);\n    } else {\n        printf(\"Database #9 is not empty, test can not continue\\n\");\n        exit(1);\n    }\n\n    return c;\n}\n\n/* Switch protocol */\nstatic void send_hello(redisContext *c, int version) {\n    redisReply *reply;\n    int expected;\n\n    reply = redisCommand(c, \"HELLO %d\", version);\n    expected = version == 3 ? REDIS_REPLY_MAP : REDIS_REPLY_ARRAY;\n    assert(reply != NULL && reply->type == expected);\n    freeReplyObject(reply);\n}\n\n/* Togggle client tracking */\nstatic void send_client_tracking(redisContext *c, const char *str) {\n    redisReply *reply;\n\n    reply = redisCommand(c, \"CLIENT TRACKING %s\", str);\n    assert(reply != NULL && reply->type == REDIS_REPLY_STATUS);\n    freeReplyObject(reply);\n}\n\nstatic int disconnect(redisContext *c, int keep_fd) {\n    redisReply *reply;\n\n    /* Make sure we're on DB 9. */\n    reply = redisCommand(c,\"SELECT 9\");\n    assert(reply != NULL);\n    freeReplyObject(reply);\n    reply = redisCommand(c,\"FLUSHDB\");\n    assert(reply != NULL);\n    freeReplyObject(reply);\n\n    /* Free the context as well, but keep the fd if requested. */\n    if (keep_fd)\n        return redisFreeKeepFd(c);\n    redisFree(c);\n    return -1;\n}\n\nstatic void do_ssl_handshake(redisContext *c) {\n#ifdef HIREDIS_TEST_SSL\n    redisInitiateSSLWithContext(c, _ssl_ctx);\n    if (c->err) {\n        printf(\"SSL error: %s\\n\", c->errstr);\n        redisFree(c);\n        exit(1);\n    }\n#else\n    (void) c;\n#endif\n}\n\nstatic redisContext *do_connect(struct config config) {\n    redisContext *c = NULL;\n\n    if (config.type == CONN_TCP) {\n        c = redisConnect(config.tcp.host, config.tcp.port);\n    } else if (config.type == CONN_SSL) {\n        c = redisConnect(config.ssl.host, config.ssl.port);\n    } else if (config.type == CONN_UNIX) {\n        c = redisConnectUnix(config.unix_sock.path);\n    } else if (config.type == CONN_FD) {\n        /* Create a dummy connection just to get an fd to inherit */\n        redisContext *dummy_ctx = redisConnectUnix(config.unix_sock.path);\n        if (dummy_ctx) {\n            int fd = disconnect(dummy_ctx, 1);\n            printf(\"Connecting to inherited fd %d\\n\", fd);\n            c = redisConnectFd(fd);\n        }\n    } else {\n        assert(NULL);\n    }\n\n    if (c == NULL) {\n        printf(\"Connection error: can't allocate redis context\\n\");\n        exit(1);\n    } else if (c->err) {\n        printf(\"Connection error: %s\\n\", c->errstr);\n        redisFree(c);\n        exit(1);\n    }\n\n    if (config.type == CONN_SSL) {\n        do_ssl_handshake(c);\n    }\n\n    return select_database(c);\n}\n\nstatic void do_reconnect(redisContext *c, struct config config) {\n    redisReconnect(c);\n\n    if (config.type == CONN_SSL) {\n        do_ssl_handshake(c);\n    }\n}\n\nstatic void test_format_commands(void) {\n    char *cmd;\n    int len;\n\n    test(\"Format command without interpolation: \");\n    len = redisFormatCommand(&cmd,\"SET foo bar\");\n    test_cond(strncmp(cmd,\"*3\\r\\n$3\\r\\nSET\\r\\n$3\\r\\nfoo\\r\\n$3\\r\\nbar\\r\\n\",len) == 0 &&\n        len == 4+4+(3+2)+4+(3+2)+4+(3+2));\n    hi_free(cmd);\n\n    test(\"Format command with %%s string interpolation: \");\n    len = redisFormatCommand(&cmd,\"SET %s %s\",\"foo\",\"bar\");\n    test_cond(strncmp(cmd,\"*3\\r\\n$3\\r\\nSET\\r\\n$3\\r\\nfoo\\r\\n$3\\r\\nbar\\r\\n\",len) == 0 &&\n        len == 4+4+(3+2)+4+(3+2)+4+(3+2));\n    hi_free(cmd);\n\n    test(\"Format command with %%s and an empty string: \");\n    len = redisFormatCommand(&cmd,\"SET %s %s\",\"foo\",\"\");\n    test_cond(strncmp(cmd,\"*3\\r\\n$3\\r\\nSET\\r\\n$3\\r\\nfoo\\r\\n$0\\r\\n\\r\\n\",len) == 0 &&\n        len == 4+4+(3+2)+4+(3+2)+4+(0+2));\n    hi_free(cmd);\n\n    test(\"Format command with an empty string in between proper interpolations: \");\n    len = redisFormatCommand(&cmd,\"SET %s %s\",\"\",\"foo\");\n    test_cond(strncmp(cmd,\"*3\\r\\n$3\\r\\nSET\\r\\n$0\\r\\n\\r\\n$3\\r\\nfoo\\r\\n\",len) == 0 &&\n        len == 4+4+(3+2)+4+(0+2)+4+(3+2));\n    hi_free(cmd);\n\n    test(\"Format command with %%b string interpolation: \");\n    len = redisFormatCommand(&cmd,\"SET %b %b\",\"foo\",(size_t)3,\"b\\0r\",(size_t)3);\n    test_cond(strncmp(cmd,\"*3\\r\\n$3\\r\\nSET\\r\\n$3\\r\\nfoo\\r\\n$3\\r\\nb\\0r\\r\\n\",len) == 0 &&\n        len == 4+4+(3+2)+4+(3+2)+4+(3+2));\n    hi_free(cmd);\n\n    test(\"Format command with %%b and an empty string: \");\n    len = redisFormatCommand(&cmd,\"SET %b %b\",\"foo\",(size_t)3,\"\",(size_t)0);\n    test_cond(strncmp(cmd,\"*3\\r\\n$3\\r\\nSET\\r\\n$3\\r\\nfoo\\r\\n$0\\r\\n\\r\\n\",len) == 0 &&\n        len == 4+4+(3+2)+4+(3+2)+4+(0+2));\n    hi_free(cmd);\n\n    test(\"Format command with literal %%: \");\n    len = redisFormatCommand(&cmd,\"SET %% %%\");\n    test_cond(strncmp(cmd,\"*3\\r\\n$3\\r\\nSET\\r\\n$1\\r\\n%\\r\\n$1\\r\\n%\\r\\n\",len) == 0 &&\n        len == 4+4+(3+2)+4+(1+2)+4+(1+2));\n    hi_free(cmd);\n\n    /* Vararg width depends on the type. These tests make sure that the\n     * width is correctly determined using the format and subsequent varargs\n     * can correctly be interpolated. */\n#define INTEGER_WIDTH_TEST(fmt, type) do {                                                \\\n    type value = 123;                                                                     \\\n    test(\"Format command with printf-delegation (\" #type \"): \");                          \\\n    len = redisFormatCommand(&cmd,\"key:%08\" fmt \" str:%s\", value, \"hello\");               \\\n    test_cond(strncmp(cmd,\"*2\\r\\n$12\\r\\nkey:00000123\\r\\n$9\\r\\nstr:hello\\r\\n\",len) == 0 && \\\n        len == 4+5+(12+2)+4+(9+2));                                                       \\\n    hi_free(cmd);                                                                         \\\n} while(0)\n\n#define FLOAT_WIDTH_TEST(type) do {                                                       \\\n    type value = 123.0;                                                                   \\\n    test(\"Format command with printf-delegation (\" #type \"): \");                          \\\n    len = redisFormatCommand(&cmd,\"key:%08.3f str:%s\", value, \"hello\");                   \\\n    test_cond(strncmp(cmd,\"*2\\r\\n$12\\r\\nkey:0123.000\\r\\n$9\\r\\nstr:hello\\r\\n\",len) == 0 && \\\n        len == 4+5+(12+2)+4+(9+2));                                                       \\\n    hi_free(cmd);                                                                         \\\n} while(0)\n\n    INTEGER_WIDTH_TEST(\"d\", int);\n    INTEGER_WIDTH_TEST(\"hhd\", char);\n    INTEGER_WIDTH_TEST(\"hd\", short);\n    INTEGER_WIDTH_TEST(\"ld\", long);\n    INTEGER_WIDTH_TEST(\"lld\", long long);\n    INTEGER_WIDTH_TEST(\"u\", unsigned int);\n    INTEGER_WIDTH_TEST(\"hhu\", unsigned char);\n    INTEGER_WIDTH_TEST(\"hu\", unsigned short);\n    INTEGER_WIDTH_TEST(\"lu\", unsigned long);\n    INTEGER_WIDTH_TEST(\"llu\", unsigned long long);\n    FLOAT_WIDTH_TEST(float);\n    FLOAT_WIDTH_TEST(double);\n\n    test(\"Format command with invalid printf format: \");\n    len = redisFormatCommand(&cmd,\"key:%08p %b\",(void*)1234,\"foo\",(size_t)3);\n    test_cond(len == -1);\n\n    const char *argv[3];\n    argv[0] = \"SET\";\n    argv[1] = \"foo\\0xxx\";\n    argv[2] = \"bar\";\n    size_t lens[3] = { 3, 7, 3 };\n    int argc = 3;\n\n    test(\"Format command by passing argc/argv without lengths: \");\n    len = redisFormatCommandArgv(&cmd,argc,argv,NULL);\n    test_cond(strncmp(cmd,\"*3\\r\\n$3\\r\\nSET\\r\\n$3\\r\\nfoo\\r\\n$3\\r\\nbar\\r\\n\",len) == 0 &&\n        len == 4+4+(3+2)+4+(3+2)+4+(3+2));\n    hi_free(cmd);\n\n    test(\"Format command by passing argc/argv with lengths: \");\n    len = redisFormatCommandArgv(&cmd,argc,argv,lens);\n    test_cond(strncmp(cmd,\"*3\\r\\n$3\\r\\nSET\\r\\n$7\\r\\nfoo\\0xxx\\r\\n$3\\r\\nbar\\r\\n\",len) == 0 &&\n        len == 4+4+(3+2)+4+(7+2)+4+(3+2));\n    hi_free(cmd);\n\n    hisds sds_cmd;\n\n    sds_cmd = NULL;\n    test(\"Format command into hisds by passing argc/argv without lengths: \");\n    len = redisFormatSdsCommandArgv(&sds_cmd,argc,argv,NULL);\n    test_cond(strncmp(sds_cmd,\"*3\\r\\n$3\\r\\nSET\\r\\n$3\\r\\nfoo\\r\\n$3\\r\\nbar\\r\\n\",len) == 0 &&\n        len == 4+4+(3+2)+4+(3+2)+4+(3+2));\n    hi_sdsfree(sds_cmd);\n\n    sds_cmd = NULL;\n    test(\"Format command into hisds by passing argc/argv with lengths: \");\n    len = redisFormatSdsCommandArgv(&sds_cmd,argc,argv,lens);\n    test_cond(strncmp(sds_cmd,\"*3\\r\\n$3\\r\\nSET\\r\\n$7\\r\\nfoo\\0xxx\\r\\n$3\\r\\nbar\\r\\n\",len) == 0 &&\n        len == 4+4+(3+2)+4+(7+2)+4+(3+2));\n    hi_sdsfree(sds_cmd);\n}\n\nstatic void test_append_formatted_commands(struct config config) {\n    redisContext *c;\n    redisReply *reply;\n    char *cmd;\n    int len;\n\n    c = do_connect(config);\n\n    test(\"Append format command: \");\n\n    len = redisFormatCommand(&cmd, \"SET foo bar\");\n\n    test_cond(redisAppendFormattedCommand(c, cmd, len) == REDIS_OK);\n\n    assert(redisGetReply(c, (void*)&reply) == REDIS_OK);\n\n    hi_free(cmd);\n    freeReplyObject(reply);\n\n    disconnect(c, 0);\n}\n\nstatic void test_reply_reader(void) {\n    redisReader *reader;\n    void *reply, *root;\n    int ret;\n    int i;\n\n    test(\"Error handling in reply parser: \");\n    reader = redisReaderCreate();\n    redisReaderFeed(reader,(char*)\"@foo\\r\\n\",6);\n    ret = redisReaderGetReply(reader,NULL);\n    test_cond(ret == REDIS_ERR &&\n              strcasecmp(reader->errstr,\"Protocol error, got \\\"@\\\" as reply type byte\") == 0);\n    redisReaderFree(reader);\n\n    /* when the reply already contains multiple items, they must be free'd\n     * on an error. valgrind will bark when this doesn't happen. */\n    test(\"Memory cleanup in reply parser: \");\n    reader = redisReaderCreate();\n    redisReaderFeed(reader,(char*)\"*2\\r\\n\",4);\n    redisReaderFeed(reader,(char*)\"$5\\r\\nhello\\r\\n\",11);\n    redisReaderFeed(reader,(char*)\"@foo\\r\\n\",6);\n    ret = redisReaderGetReply(reader,NULL);\n    test_cond(ret == REDIS_ERR &&\n              strcasecmp(reader->errstr,\"Protocol error, got \\\"@\\\" as reply type byte\") == 0);\n    redisReaderFree(reader);\n\n    reader = redisReaderCreate();\n    test(\"Can handle arbitrarily nested multi-bulks: \");\n    for (i = 0; i < 128; i++) {\n        redisReaderFeed(reader,(char*)\"*1\\r\\n\", 4);\n    }\n    redisReaderFeed(reader,(char*)\"$6\\r\\nLOLWUT\\r\\n\",12);\n    ret = redisReaderGetReply(reader,&reply);\n    root = reply; /* Keep track of the root reply */\n    test_cond(ret == REDIS_OK &&\n        ((redisReply*)reply)->type == REDIS_REPLY_ARRAY &&\n        ((redisReply*)reply)->elements == 1);\n\n    test(\"Can parse arbitrarily nested multi-bulks correctly: \");\n    while(i--) {\n        assert(reply != NULL && ((redisReply*)reply)->type == REDIS_REPLY_ARRAY);\n        reply = ((redisReply*)reply)->element[0];\n    }\n    test_cond(((redisReply*)reply)->type == REDIS_REPLY_STRING &&\n        !memcmp(((redisReply*)reply)->str, \"LOLWUT\", 6));\n    freeReplyObject(root);\n    redisReaderFree(reader);\n\n    test(\"Correctly parses LLONG_MAX: \");\n    reader = redisReaderCreate();\n    redisReaderFeed(reader, \":9223372036854775807\\r\\n\",22);\n    ret = redisReaderGetReply(reader,&reply);\n    test_cond(ret == REDIS_OK &&\n            ((redisReply*)reply)->type == REDIS_REPLY_INTEGER &&\n            ((redisReply*)reply)->integer == LLONG_MAX);\n    freeReplyObject(reply);\n    redisReaderFree(reader);\n\n    test(\"Set error when > LLONG_MAX: \");\n    reader = redisReaderCreate();\n    redisReaderFeed(reader, \":9223372036854775808\\r\\n\",22);\n    ret = redisReaderGetReply(reader,&reply);\n    test_cond(ret == REDIS_ERR &&\n              strcasecmp(reader->errstr,\"Bad integer value\") == 0);\n    freeReplyObject(reply);\n    redisReaderFree(reader);\n\n    test(\"Correctly parses LLONG_MIN: \");\n    reader = redisReaderCreate();\n    redisReaderFeed(reader, \":-9223372036854775808\\r\\n\",23);\n    ret = redisReaderGetReply(reader,&reply);\n    test_cond(ret == REDIS_OK &&\n            ((redisReply*)reply)->type == REDIS_REPLY_INTEGER &&\n            ((redisReply*)reply)->integer == LLONG_MIN);\n    freeReplyObject(reply);\n    redisReaderFree(reader);\n\n    test(\"Set error when < LLONG_MIN: \");\n    reader = redisReaderCreate();\n    redisReaderFeed(reader, \":-9223372036854775809\\r\\n\",23);\n    ret = redisReaderGetReply(reader,&reply);\n    test_cond(ret == REDIS_ERR &&\n              strcasecmp(reader->errstr,\"Bad integer value\") == 0);\n    freeReplyObject(reply);\n    redisReaderFree(reader);\n\n    test(\"Set error when array < -1: \");\n    reader = redisReaderCreate();\n    redisReaderFeed(reader, \"*-2\\r\\n+asdf\\r\\n\",12);\n    ret = redisReaderGetReply(reader,&reply);\n    test_cond(ret == REDIS_ERR &&\n              strcasecmp(reader->errstr,\"Multi-bulk length out of range\") == 0);\n    freeReplyObject(reply);\n    redisReaderFree(reader);\n\n    test(\"Set error when bulk < -1: \");\n    reader = redisReaderCreate();\n    redisReaderFeed(reader, \"$-2\\r\\nasdf\\r\\n\",11);\n    ret = redisReaderGetReply(reader,&reply);\n    test_cond(ret == REDIS_ERR &&\n              strcasecmp(reader->errstr,\"Bulk string length out of range\") == 0);\n    freeReplyObject(reply);\n    redisReaderFree(reader);\n\n    test(\"Can configure maximum multi-bulk elements: \");\n    reader = redisReaderCreate();\n    reader->maxelements = 1024;\n    redisReaderFeed(reader, \"*1025\\r\\n\", 7);\n    ret = redisReaderGetReply(reader,&reply);\n    test_cond(ret == REDIS_ERR &&\n              strcasecmp(reader->errstr, \"Multi-bulk length out of range\") == 0);\n    freeReplyObject(reply);\n    redisReaderFree(reader);\n\n    test(\"Multi-bulk never overflows regardless of maxelements: \");\n    size_t bad_mbulk_len = (SIZE_MAX / sizeof(void *)) + 3;\n    char bad_mbulk_reply[100];\n    snprintf(bad_mbulk_reply, sizeof(bad_mbulk_reply), \"*%llu\\r\\n+asdf\\r\\n\",\n        (unsigned long long) bad_mbulk_len);\n\n    reader = redisReaderCreate();\n    reader->maxelements = 0;    /* Don't rely on default limit */\n    redisReaderFeed(reader, bad_mbulk_reply, strlen(bad_mbulk_reply));\n    ret = redisReaderGetReply(reader,&reply);\n    test_cond(ret == REDIS_ERR && strcasecmp(reader->errstr, \"Out of memory\") == 0);\n    freeReplyObject(reply);\n    redisReaderFree(reader);\n\n#if LLONG_MAX > SIZE_MAX\n    test(\"Set error when array > SIZE_MAX: \");\n    reader = redisReaderCreate();\n    redisReaderFeed(reader, \"*9223372036854775807\\r\\n+asdf\\r\\n\",29);\n    ret = redisReaderGetReply(reader,&reply);\n    test_cond(ret == REDIS_ERR &&\n            strcasecmp(reader->errstr,\"Multi-bulk length out of range\") == 0);\n    freeReplyObject(reply);\n    redisReaderFree(reader);\n\n    test(\"Set error when bulk > SIZE_MAX: \");\n    reader = redisReaderCreate();\n    redisReaderFeed(reader, \"$9223372036854775807\\r\\nasdf\\r\\n\",28);\n    ret = redisReaderGetReply(reader,&reply);\n    test_cond(ret == REDIS_ERR &&\n            strcasecmp(reader->errstr,\"Bulk string length out of range\") == 0);\n    freeReplyObject(reply);\n    redisReaderFree(reader);\n#endif\n\n    test(\"Works with NULL functions for reply: \");\n    reader = redisReaderCreate();\n    reader->fn = NULL;\n    redisReaderFeed(reader,(char*)\"+OK\\r\\n\",5);\n    ret = redisReaderGetReply(reader,&reply);\n    test_cond(ret == REDIS_OK && reply == (void*)REDIS_REPLY_STATUS);\n    redisReaderFree(reader);\n\n    test(\"Works when a single newline (\\\\r\\\\n) covers two calls to feed: \");\n    reader = redisReaderCreate();\n    reader->fn = NULL;\n    redisReaderFeed(reader,(char*)\"+OK\\r\",4);\n    ret = redisReaderGetReply(reader,&reply);\n    assert(ret == REDIS_OK && reply == NULL);\n    redisReaderFeed(reader,(char*)\"\\n\",1);\n    ret = redisReaderGetReply(reader,&reply);\n    test_cond(ret == REDIS_OK && reply == (void*)REDIS_REPLY_STATUS);\n    redisReaderFree(reader);\n\n    test(\"Don't reset state after protocol error: \");\n    reader = redisReaderCreate();\n    reader->fn = NULL;\n    redisReaderFeed(reader,(char*)\"x\",1);\n    ret = redisReaderGetReply(reader,&reply);\n    assert(ret == REDIS_ERR);\n    ret = redisReaderGetReply(reader,&reply);\n    test_cond(ret == REDIS_ERR && reply == NULL);\n    redisReaderFree(reader);\n\n    /* Regression test for issue #45 on GitHub. */\n    test(\"Don't do empty allocation for empty multi bulk: \");\n    reader = redisReaderCreate();\n    redisReaderFeed(reader,(char*)\"*0\\r\\n\",4);\n    ret = redisReaderGetReply(reader,&reply);\n    test_cond(ret == REDIS_OK &&\n        ((redisReply*)reply)->type == REDIS_REPLY_ARRAY &&\n        ((redisReply*)reply)->elements == 0);\n    freeReplyObject(reply);\n    redisReaderFree(reader);\n\n    /* RESP3 verbatim strings (GitHub issue #802) */\n    test(\"Can parse RESP3 verbatim strings: \");\n    reader = redisReaderCreate();\n    redisReaderFeed(reader,(char*)\"=10\\r\\ntxt:LOLWUT\\r\\n\",17);\n    ret = redisReaderGetReply(reader,&reply);\n    test_cond(ret == REDIS_OK &&\n        ((redisReply*)reply)->type == REDIS_REPLY_VERB &&\n         !memcmp(((redisReply*)reply)->str,\"LOLWUT\", 6));\n    freeReplyObject(reply);\n    redisReaderFree(reader);\n\n    /* RESP3 push messages (Github issue #815) */\n    test(\"Can parse RESP3 push messages: \");\n    reader = redisReaderCreate();\n    redisReaderFeed(reader,(char*)\">2\\r\\n$6\\r\\nLOLWUT\\r\\n:42\\r\\n\",21);\n    ret = redisReaderGetReply(reader,&reply);\n    test_cond(ret == REDIS_OK &&\n        ((redisReply*)reply)->type == REDIS_REPLY_PUSH &&\n        ((redisReply*)reply)->elements == 2 &&\n        ((redisReply*)reply)->element[0]->type == REDIS_REPLY_STRING &&\n        !memcmp(((redisReply*)reply)->element[0]->str,\"LOLWUT\",6) &&\n        ((redisReply*)reply)->element[1]->type == REDIS_REPLY_INTEGER &&\n        ((redisReply*)reply)->element[1]->integer == 42);\n    freeReplyObject(reply);\n    redisReaderFree(reader);\n}\n\nstatic void test_free_null(void) {\n    void *redisCtx = NULL;\n    void *reply = NULL;\n\n    test(\"Don't fail when redisFree is passed a NULL value: \");\n    redisFree(redisCtx);\n    test_cond(redisCtx == NULL);\n\n    test(\"Don't fail when freeReplyObject is passed a NULL value: \");\n    freeReplyObject(reply);\n    test_cond(reply == NULL);\n}\n\nstatic void *hi_malloc_fail(size_t size) {\n    (void)size;\n    return NULL;\n}\n\nstatic void *hi_calloc_fail(size_t nmemb, size_t size) {\n    (void)nmemb;\n    (void)size;\n    return NULL;\n}\n\nstatic void *hi_realloc_fail(void *ptr, size_t size) {\n    (void)ptr;\n    (void)size;\n    return NULL;\n}\n\nstatic void test_allocator_injection(void) {\n    hiredisAllocFuncs ha = {\n        .mallocFn = hi_malloc_fail,\n        .callocFn = hi_calloc_fail,\n        .reallocFn = hi_realloc_fail,\n        .strdupFn = strdup,\n        .freeFn = free,\n    };\n\n    // Override hiredis allocators\n    hiredisSetAllocators(&ha);\n\n    test(\"redisContext uses injected allocators: \");\n    redisContext *c = redisConnect(\"localhost\", 6379);\n    test_cond(c == NULL);\n\n    test(\"redisReader uses injected allocators: \");\n    redisReader *reader = redisReaderCreate();\n    test_cond(reader == NULL);\n\n    // Return allocators to default\n    hiredisResetAllocators();\n}\n\n#define HIREDIS_BAD_DOMAIN \"idontexist-noreally.com\"\nstatic void test_blocking_connection_errors(void) {\n    redisContext *c;\n    struct addrinfo hints = {.ai_family = AF_INET};\n    struct addrinfo *ai_tmp = NULL;\n\n    int rv = getaddrinfo(HIREDIS_BAD_DOMAIN, \"6379\", &hints, &ai_tmp);\n    if (rv != 0) {\n        // Address does *not* exist\n        test(\"Returns error when host cannot be resolved: \");\n        // First see if this domain name *actually* resolves to NXDOMAIN\n        c = redisConnect(HIREDIS_BAD_DOMAIN, 6379);\n        test_cond(\n            c->err == REDIS_ERR_OTHER &&\n            (strcmp(c->errstr, \"Name or service not known\") == 0 ||\n             strcmp(c->errstr, \"Can't resolve: \" HIREDIS_BAD_DOMAIN) == 0 ||\n             strcmp(c->errstr, \"Name does not resolve\") == 0 ||\n             strcmp(c->errstr, \"nodename nor servname provided, or not known\") == 0 ||\n             strcmp(c->errstr, \"No address associated with hostname\") == 0 ||\n             strcmp(c->errstr, \"Temporary failure in name resolution\") == 0 ||\n             strcmp(c->errstr, \"hostname nor servname provided, or not known\") == 0 ||\n             strcmp(c->errstr, \"no address associated with name\") == 0 ||\n             strcmp(c->errstr, \"No such host is known. \") == 0));\n        redisFree(c);\n    } else {\n        printf(\"Skipping NXDOMAIN test. Found evil ISP!\\n\");\n        freeaddrinfo(ai_tmp);\n    }\n\n#ifndef _WIN32\n    test(\"Returns error when the port is not open: \");\n    c = redisConnect((char*)\"localhost\", 1);\n    test_cond(c->err == REDIS_ERR_IO &&\n        strcmp(c->errstr,\"Connection refused\") == 0);\n    redisFree(c);\n\n    test(\"Returns error when the unix_sock socket path doesn't accept connections: \");\n    c = redisConnectUnix((char*)\"/tmp/idontexist.sock\");\n    test_cond(c->err == REDIS_ERR_IO); /* Don't care about the message... */\n    redisFree(c);\n#endif\n}\n\n/* Test push handler */\nvoid push_handler(void *privdata, void *r) {\n    struct pushCounters *pcounts = privdata;\n    redisReply *reply = r, *payload;\n\n    assert(reply && reply->type == REDIS_REPLY_PUSH && reply->elements == 2);\n\n    payload = reply->element[1];\n    if (payload->type == REDIS_REPLY_ARRAY) {\n        payload = payload->element[0];\n    }\n\n    if (payload->type == REDIS_REPLY_STRING) {\n        pcounts->str++;\n    } else if (payload->type == REDIS_REPLY_NIL) {\n        pcounts->nil++;\n    }\n\n    freeReplyObject(reply);\n}\n\n/* Dummy function just to test setting a callback with redisOptions */\nvoid push_handler_async(redisAsyncContext *ac, void *reply) {\n    (void)ac;\n    (void)reply;\n}\n\nstatic void test_resp3_push_handler(redisContext *c) {\n    struct pushCounters pc = {0};\n    redisPushFn *old = NULL;\n    redisReply *reply;\n    void *privdata;\n\n    /* Switch to RESP3 and turn on client tracking */\n    send_hello(c, 3);\n    send_client_tracking(c, \"ON\");\n    privdata = c->privdata;\n    c->privdata = &pc;\n\n    reply = redisCommand(c, \"GET key:0\");\n    assert(reply != NULL);\n    freeReplyObject(reply);\n\n    test(\"RESP3 PUSH messages are handled out of band by default: \");\n    reply = redisCommand(c, \"SET key:0 val:0\");\n    test_cond(reply != NULL && reply->type == REDIS_REPLY_STATUS);\n    freeReplyObject(reply);\n\n    assert((reply = redisCommand(c, \"GET key:0\")) != NULL);\n    freeReplyObject(reply);\n\n    old = redisSetPushCallback(c, push_handler);\n    test(\"We can set a custom RESP3 PUSH handler: \");\n    reply = redisCommand(c, \"SET key:0 val:0\");\n    test_cond(reply != NULL && reply->type == REDIS_REPLY_STATUS && pc.str == 1);\n    freeReplyObject(reply);\n\n    test(\"We properly handle a NIL invalidation payload: \");\n    reply = redisCommand(c, \"FLUSHDB\");\n    test_cond(reply != NULL && reply->type == REDIS_REPLY_STATUS && pc.nil == 1);\n    freeReplyObject(reply);\n\n    /* Unset the push callback and generate an invalidate message making\n     * sure it is not handled out of band. */\n    test(\"With no handler, PUSH replies come in-band: \");\n    redisSetPushCallback(c, NULL);\n    assert((reply = redisCommand(c, \"GET key:0\")) != NULL);\n    freeReplyObject(reply);\n    assert((reply = redisCommand(c, \"SET key:0 invalid\")) != NULL);\n    test_cond(reply->type == REDIS_REPLY_PUSH);\n    freeReplyObject(reply);\n\n    test(\"With no PUSH handler, no replies are lost: \");\n    assert(redisGetReply(c, (void**)&reply) == REDIS_OK);\n    test_cond(reply != NULL && reply->type == REDIS_REPLY_STATUS);\n    freeReplyObject(reply);\n\n    /* Return to the originally set PUSH handler */\n    assert(old != NULL);\n    redisSetPushCallback(c, old);\n\n    /* Switch back to RESP2 and disable tracking */\n    c->privdata = privdata;\n    send_client_tracking(c, \"OFF\");\n    send_hello(c, 2);\n}\n\nredisOptions get_redis_tcp_options(struct config config) {\n    redisOptions options = {0};\n    REDIS_OPTIONS_SET_TCP(&options, config.tcp.host, config.tcp.port);\n    return options;\n}\n\nstatic void test_resp3_push_options(struct config config) {\n    redisAsyncContext *ac;\n    redisContext *c;\n    redisOptions options;\n\n    test(\"We set a default RESP3 handler for redisContext: \");\n    options = get_redis_tcp_options(config);\n    assert((c = redisConnectWithOptions(&options)) != NULL);\n    test_cond(c->push_cb != NULL);\n    redisFree(c);\n\n    test(\"We don't set a default RESP3 push handler for redisAsyncContext: \");\n    options = get_redis_tcp_options(config);\n    assert((ac = redisAsyncConnectWithOptions(&options)) != NULL);\n    test_cond(ac->c.push_cb == NULL);\n    redisAsyncFree(ac);\n\n    test(\"Our REDIS_OPT_NO_PUSH_AUTOFREE flag works: \");\n    options = get_redis_tcp_options(config);\n    options.options |= REDIS_OPT_NO_PUSH_AUTOFREE;\n    assert((c = redisConnectWithOptions(&options)) != NULL);\n    test_cond(c->push_cb == NULL);\n    redisFree(c);\n\n    test(\"We can use redisOptions to set a custom PUSH handler for redisContext: \");\n    options = get_redis_tcp_options(config);\n    options.push_cb = push_handler;\n    assert((c = redisConnectWithOptions(&options)) != NULL);\n    test_cond(c->push_cb == push_handler);\n    redisFree(c);\n\n    test(\"We can use redisOptions to set a custom PUSH handler for redisAsyncContext: \");\n    options = get_redis_tcp_options(config);\n    options.async_push_cb = push_handler_async;\n    assert((ac = redisAsyncConnectWithOptions(&options)) != NULL);\n    test_cond(ac->push_cb == push_handler_async);\n    redisAsyncFree(ac);\n}\n\nvoid free_privdata(void *privdata) {\n    struct privdata *data = privdata;\n    data->dtor_counter++;\n}\n\nstatic void test_privdata_hooks(struct config config) {\n    struct privdata data = {0};\n    redisOptions options;\n    redisContext *c;\n\n    test(\"We can use redisOptions to set privdata: \");\n    options = get_redis_tcp_options(config);\n    REDIS_OPTIONS_SET_PRIVDATA(&options, &data, free_privdata);\n    assert((c = redisConnectWithOptions(&options)) != NULL);\n    test_cond(c->privdata == &data);\n\n    test(\"Our privdata destructor fires when we free the context: \");\n    redisFree(c);\n    test_cond(data.dtor_counter == 1);\n}\n\nstatic void test_blocking_connection(struct config config) {\n    redisContext *c;\n    redisReply *reply;\n    int major;\n\n    c = do_connect(config);\n\n    test(\"Is able to deliver commands: \");\n    reply = redisCommand(c,\"PING\");\n    test_cond(reply->type == REDIS_REPLY_STATUS &&\n        strcasecmp(reply->str,\"pong\") == 0)\n    freeReplyObject(reply);\n\n    test(\"Is a able to send commands verbatim: \");\n    reply = redisCommand(c,\"SET foo bar\");\n    test_cond (reply->type == REDIS_REPLY_STATUS &&\n        strcasecmp(reply->str,\"ok\") == 0)\n    freeReplyObject(reply);\n\n    test(\"%%s String interpolation works: \");\n    reply = redisCommand(c,\"SET %s %s\",\"foo\",\"hello world\");\n    freeReplyObject(reply);\n    reply = redisCommand(c,\"GET foo\");\n    test_cond(reply->type == REDIS_REPLY_STRING &&\n        strcmp(reply->str,\"hello world\") == 0);\n    freeReplyObject(reply);\n\n    test(\"%%b String interpolation works: \");\n    reply = redisCommand(c,\"SET %b %b\",\"foo\",(size_t)3,\"hello\\x00world\",(size_t)11);\n    freeReplyObject(reply);\n    reply = redisCommand(c,\"GET foo\");\n    test_cond(reply->type == REDIS_REPLY_STRING &&\n        memcmp(reply->str,\"hello\\x00world\",11) == 0)\n\n    test(\"Binary reply length is correct: \");\n    test_cond(reply->len == 11)\n    freeReplyObject(reply);\n\n    test(\"Can parse nil replies: \");\n    reply = redisCommand(c,\"GET nokey\");\n    test_cond(reply->type == REDIS_REPLY_NIL)\n    freeReplyObject(reply);\n\n    /* test 7 */\n    test(\"Can parse integer replies: \");\n    reply = redisCommand(c,\"INCR mycounter\");\n    test_cond(reply->type == REDIS_REPLY_INTEGER && reply->integer == 1)\n    freeReplyObject(reply);\n\n    test(\"Can parse multi bulk replies: \");\n    freeReplyObject(redisCommand(c,\"LPUSH mylist foo\"));\n    freeReplyObject(redisCommand(c,\"LPUSH mylist bar\"));\n    reply = redisCommand(c,\"LRANGE mylist 0 -1\");\n    test_cond(reply->type == REDIS_REPLY_ARRAY &&\n              reply->elements == 2 &&\n              !memcmp(reply->element[0]->str,\"bar\",3) &&\n              !memcmp(reply->element[1]->str,\"foo\",3))\n    freeReplyObject(reply);\n\n    /* m/e with multi bulk reply *before* other reply.\n     * specifically test ordering of reply items to parse. */\n    test(\"Can handle nested multi bulk replies: \");\n    freeReplyObject(redisCommand(c,\"MULTI\"));\n    freeReplyObject(redisCommand(c,\"LRANGE mylist 0 -1\"));\n    freeReplyObject(redisCommand(c,\"PING\"));\n    reply = (redisCommand(c,\"EXEC\"));\n    test_cond(reply->type == REDIS_REPLY_ARRAY &&\n              reply->elements == 2 &&\n              reply->element[0]->type == REDIS_REPLY_ARRAY &&\n              reply->element[0]->elements == 2 &&\n              !memcmp(reply->element[0]->element[0]->str,\"bar\",3) &&\n              !memcmp(reply->element[0]->element[1]->str,\"foo\",3) &&\n              reply->element[1]->type == REDIS_REPLY_STATUS &&\n              strcasecmp(reply->element[1]->str,\"pong\") == 0);\n    freeReplyObject(reply);\n\n    /* Make sure passing NULL to redisGetReply is safe */\n    test(\"Can pass NULL to redisGetReply: \");\n    assert(redisAppendCommand(c, \"PING\") == REDIS_OK);\n    test_cond(redisGetReply(c, NULL) == REDIS_OK);\n\n    get_redis_version(c, &major, NULL);\n    if (major >= 6) test_resp3_push_handler(c);\n    test_resp3_push_options(config);\n\n    test_privdata_hooks(config);\n\n    disconnect(c, 0);\n}\n\n/* Send DEBUG SLEEP 0 to detect if we have this command */\nstatic int detect_debug_sleep(redisContext *c) {\n    int detected;\n    redisReply *reply = redisCommand(c, \"DEBUG SLEEP 0\\r\\n\");\n\n    if (reply == NULL || c->err) {\n        const char *cause = c->err ? c->errstr : \"(none)\";\n        fprintf(stderr, \"Error testing for DEBUG SLEEP (Redis error: %s), exiting\\n\", cause);\n        exit(-1);\n    }\n\n    detected = reply->type == REDIS_REPLY_STATUS;\n    freeReplyObject(reply);\n\n    return detected;\n}\n\nstatic void test_blocking_connection_timeouts(struct config config) {\n    redisContext *c;\n    redisReply *reply;\n    ssize_t s;\n    const char *sleep_cmd = \"DEBUG SLEEP 3\\r\\n\";\n    struct timeval tv;\n\n    c = do_connect(config);\n    test(\"Successfully completes a command when the timeout is not exceeded: \");\n    reply = redisCommand(c,\"SET foo fast\");\n    freeReplyObject(reply);\n    tv.tv_sec = 0;\n    tv.tv_usec = 10000;\n    redisSetTimeout(c, tv);\n    reply = redisCommand(c, \"GET foo\");\n    test_cond(reply != NULL && reply->type == REDIS_REPLY_STRING && memcmp(reply->str, \"fast\", 4) == 0);\n    freeReplyObject(reply);\n    disconnect(c, 0);\n\n    c = do_connect(config);\n    test(\"Does not return a reply when the command times out: \");\n    if (detect_debug_sleep(c)) {\n        redisAppendFormattedCommand(c, sleep_cmd, strlen(sleep_cmd));\n        s = c->funcs->write(c);\n        tv.tv_sec = 0;\n        tv.tv_usec = 10000;\n        redisSetTimeout(c, tv);\n        reply = redisCommand(c, \"GET foo\");\n#ifndef _WIN32\n        test_cond(s > 0 && reply == NULL && c->err == REDIS_ERR_IO &&\n                  strcmp(c->errstr, \"Resource temporarily unavailable\") == 0);\n#else\n        test_cond(s > 0 && reply == NULL && c->err == REDIS_ERR_TIMEOUT &&\n                  strcmp(c->errstr, \"recv timeout\") == 0);\n#endif\n        freeReplyObject(reply);\n    } else {\n        test_skipped();\n    }\n\n    test(\"Reconnect properly reconnects after a timeout: \");\n    do_reconnect(c, config);\n    reply = redisCommand(c, \"PING\");\n    test_cond(reply != NULL && reply->type == REDIS_REPLY_STATUS && strcmp(reply->str, \"PONG\") == 0);\n    freeReplyObject(reply);\n\n    test(\"Reconnect properly uses owned parameters: \");\n    config.tcp.host = \"foo\";\n    config.unix_sock.path = \"foo\";\n    do_reconnect(c, config);\n    reply = redisCommand(c, \"PING\");\n    test_cond(reply != NULL && reply->type == REDIS_REPLY_STATUS && strcmp(reply->str, \"PONG\") == 0);\n    freeReplyObject(reply);\n\n    disconnect(c, 0);\n}\n\nstatic void test_blocking_io_errors(struct config config) {\n    redisContext *c;\n    redisReply *reply;\n    void *_reply;\n    int major, minor;\n\n    /* Connect to target given by config. */\n    c = do_connect(config);\n    get_redis_version(c, &major, &minor);\n\n    test(\"Returns I/O error when the connection is lost: \");\n    reply = redisCommand(c,\"QUIT\");\n    if (major > 2 || (major == 2 && minor > 0)) {\n        /* > 2.0 returns OK on QUIT and read() should be issued once more\n         * to know the descriptor is at EOF. */\n        test_cond(strcasecmp(reply->str,\"OK\") == 0 &&\n            redisGetReply(c,&_reply) == REDIS_ERR);\n        freeReplyObject(reply);\n    } else {\n        test_cond(reply == NULL);\n    }\n\n#ifndef _WIN32\n    /* On 2.0, QUIT will cause the connection to be closed immediately and\n     * the read(2) for the reply on QUIT will set the error to EOF.\n     * On >2.0, QUIT will return with OK and another read(2) needed to be\n     * issued to find out the socket was closed by the server. In both\n     * conditions, the error will be set to EOF. */\n    assert(c->err == REDIS_ERR_EOF &&\n        strcmp(c->errstr,\"Server closed the connection\") == 0);\n#endif\n    redisFree(c);\n\n    c = do_connect(config);\n    test(\"Returns I/O error on socket timeout: \");\n    struct timeval tv = { 0, 1000 };\n    assert(redisSetTimeout(c,tv) == REDIS_OK);\n    int respcode = redisGetReply(c,&_reply);\n#ifndef _WIN32\n    test_cond(respcode == REDIS_ERR && c->err == REDIS_ERR_IO && errno == EAGAIN);\n#else\n    test_cond(respcode == REDIS_ERR && c->err == REDIS_ERR_TIMEOUT);\n#endif\n    redisFree(c);\n}\n\nstatic void test_invalid_timeout_errors(struct config config) {\n    redisContext *c;\n\n    test(\"Set error when an invalid timeout usec value is given to redisConnectWithTimeout: \");\n\n    config.tcp.timeout.tv_sec = 0;\n    config.tcp.timeout.tv_usec = 10000001;\n\n    c = redisConnectWithTimeout(config.tcp.host, config.tcp.port, config.tcp.timeout);\n\n    test_cond(c->err == REDIS_ERR_IO && strcmp(c->errstr, \"Invalid timeout specified\") == 0);\n    redisFree(c);\n\n    test(\"Set error when an invalid timeout sec value is given to redisConnectWithTimeout: \");\n\n    config.tcp.timeout.tv_sec = (((LONG_MAX) - 999) / 1000) + 1;\n    config.tcp.timeout.tv_usec = 0;\n\n    c = redisConnectWithTimeout(config.tcp.host, config.tcp.port, config.tcp.timeout);\n\n    test_cond(c->err == REDIS_ERR_IO && strcmp(c->errstr, \"Invalid timeout specified\") == 0);\n    redisFree(c);\n}\n\n/* Wrap malloc to abort on failure so OOM checks don't make the test logic\n * harder to follow. */\nvoid *hi_malloc_safe(size_t size) {\n    void *ptr = hi_malloc(size);\n    if (ptr == NULL) {\n        fprintf(stderr, \"Error:  Out of memory\\n\");\n        exit(-1);\n    }\n\n    return ptr;\n}\n\nstatic void test_throughput(struct config config) {\n    redisContext *c = do_connect(config);\n    redisReply **replies;\n    int i, num;\n    long long t1, t2;\n\n    test(\"Throughput:\\n\");\n    for (i = 0; i < 500; i++)\n        freeReplyObject(redisCommand(c,\"LPUSH mylist foo\"));\n\n    num = 1000;\n    replies = hi_malloc_safe(sizeof(redisReply*)*num);\n    t1 = usec();\n    for (i = 0; i < num; i++) {\n        replies[i] = redisCommand(c,\"PING\");\n        assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_STATUS);\n    }\n    t2 = usec();\n    for (i = 0; i < num; i++) freeReplyObject(replies[i]);\n    hi_free(replies);\n    printf(\"\\t(%dx PING: %.3fs)\\n\", num, (t2-t1)/1000000.0);\n\n    replies = hi_malloc_safe(sizeof(redisReply*)*num);\n    t1 = usec();\n    for (i = 0; i < num; i++) {\n        replies[i] = redisCommand(c,\"LRANGE mylist 0 499\");\n        assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_ARRAY);\n        assert(replies[i] != NULL && replies[i]->elements == 500);\n    }\n    t2 = usec();\n    for (i = 0; i < num; i++) freeReplyObject(replies[i]);\n    hi_free(replies);\n    printf(\"\\t(%dx LRANGE with 500 elements: %.3fs)\\n\", num, (t2-t1)/1000000.0);\n\n    replies = hi_malloc_safe(sizeof(redisReply*)*num);\n    t1 = usec();\n    for (i = 0; i < num; i++) {\n        replies[i] = redisCommand(c, \"INCRBY incrkey %d\", 1000000);\n        assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_INTEGER);\n    }\n    t2 = usec();\n    for (i = 0; i < num; i++) freeReplyObject(replies[i]);\n    hi_free(replies);\n    printf(\"\\t(%dx INCRBY: %.3fs)\\n\", num, (t2-t1)/1000000.0);\n\n    num = 10000;\n    replies = hi_malloc_safe(sizeof(redisReply*)*num);\n    for (i = 0; i < num; i++)\n        redisAppendCommand(c,\"PING\");\n    t1 = usec();\n    for (i = 0; i < num; i++) {\n        assert(redisGetReply(c, (void*)&replies[i]) == REDIS_OK);\n        assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_STATUS);\n    }\n    t2 = usec();\n    for (i = 0; i < num; i++) freeReplyObject(replies[i]);\n    hi_free(replies);\n    printf(\"\\t(%dx PING (pipelined): %.3fs)\\n\", num, (t2-t1)/1000000.0);\n\n    replies = hi_malloc_safe(sizeof(redisReply*)*num);\n    for (i = 0; i < num; i++)\n        redisAppendCommand(c,\"LRANGE mylist 0 499\");\n    t1 = usec();\n    for (i = 0; i < num; i++) {\n        assert(redisGetReply(c, (void*)&replies[i]) == REDIS_OK);\n        assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_ARRAY);\n        assert(replies[i] != NULL && replies[i]->elements == 500);\n    }\n    t2 = usec();\n    for (i = 0; i < num; i++) freeReplyObject(replies[i]);\n    hi_free(replies);\n    printf(\"\\t(%dx LRANGE with 500 elements (pipelined): %.3fs)\\n\", num, (t2-t1)/1000000.0);\n\n    replies = hi_malloc_safe(sizeof(redisReply*)*num);\n    for (i = 0; i < num; i++)\n        redisAppendCommand(c,\"INCRBY incrkey %d\", 1000000);\n    t1 = usec();\n    for (i = 0; i < num; i++) {\n        assert(redisGetReply(c, (void*)&replies[i]) == REDIS_OK);\n        assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_INTEGER);\n    }\n    t2 = usec();\n    for (i = 0; i < num; i++) freeReplyObject(replies[i]);\n    hi_free(replies);\n    printf(\"\\t(%dx INCRBY (pipelined): %.3fs)\\n\", num, (t2-t1)/1000000.0);\n\n    disconnect(c, 0);\n}\n\n// static long __test_callback_flags = 0;\n// static void __test_callback(redisContext *c, void *privdata) {\n//     ((void)c);\n//     /* Shift to detect execution order */\n//     __test_callback_flags <<= 8;\n//     __test_callback_flags |= (long)privdata;\n// }\n//\n// static void __test_reply_callback(redisContext *c, redisReply *reply, void *privdata) {\n//     ((void)c);\n//     /* Shift to detect execution order */\n//     __test_callback_flags <<= 8;\n//     __test_callback_flags |= (long)privdata;\n//     if (reply) freeReplyObject(reply);\n// }\n//\n// static redisContext *__connect_nonblock() {\n//     /* Reset callback flags */\n//     __test_callback_flags = 0;\n//     return redisConnectNonBlock(\"127.0.0.1\", port, NULL);\n// }\n//\n// static void test_nonblocking_connection() {\n//     redisContext *c;\n//     int wdone = 0;\n//\n//     test(\"Calls command callback when command is issued: \");\n//     c = __connect_nonblock();\n//     redisSetCommandCallback(c,__test_callback,(void*)1);\n//     redisCommand(c,\"PING\");\n//     test_cond(__test_callback_flags == 1);\n//     redisFree(c);\n//\n//     test(\"Calls disconnect callback on redisDisconnect: \");\n//     c = __connect_nonblock();\n//     redisSetDisconnectCallback(c,__test_callback,(void*)2);\n//     redisDisconnect(c);\n//     test_cond(__test_callback_flags == 2);\n//     redisFree(c);\n//\n//     test(\"Calls disconnect callback and free callback on redisFree: \");\n//     c = __connect_nonblock();\n//     redisSetDisconnectCallback(c,__test_callback,(void*)2);\n//     redisSetFreeCallback(c,__test_callback,(void*)4);\n//     redisFree(c);\n//     test_cond(__test_callback_flags == ((2 << 8) | 4));\n//\n//     test(\"redisBufferWrite against empty write buffer: \");\n//     c = __connect_nonblock();\n//     test_cond(redisBufferWrite(c,&wdone) == REDIS_OK && wdone == 1);\n//     redisFree(c);\n//\n//     test(\"redisBufferWrite against not yet connected fd: \");\n//     c = __connect_nonblock();\n//     redisCommand(c,\"PING\");\n//     test_cond(redisBufferWrite(c,NULL) == REDIS_ERR &&\n//               strncmp(c->error,\"write:\",6) == 0);\n//     redisFree(c);\n//\n//     test(\"redisBufferWrite against closed fd: \");\n//     c = __connect_nonblock();\n//     redisCommand(c,\"PING\");\n//     redisDisconnect(c);\n//     test_cond(redisBufferWrite(c,NULL) == REDIS_ERR &&\n//               strncmp(c->error,\"write:\",6) == 0);\n//     redisFree(c);\n//\n//     test(\"Process callbacks in the right sequence: \");\n//     c = __connect_nonblock();\n//     redisCommandWithCallback(c,__test_reply_callback,(void*)1,\"PING\");\n//     redisCommandWithCallback(c,__test_reply_callback,(void*)2,\"PING\");\n//     redisCommandWithCallback(c,__test_reply_callback,(void*)3,\"PING\");\n//\n//     /* Write output buffer */\n//     wdone = 0;\n//     while(!wdone) {\n//         usleep(500);\n//         redisBufferWrite(c,&wdone);\n//     }\n//\n//     /* Read until at least one callback is executed (the 3 replies will\n//      * arrive in a single packet, causing all callbacks to be executed in\n//      * a single pass). */\n//     while(__test_callback_flags == 0) {\n//         assert(redisBufferRead(c) == REDIS_OK);\n//         redisProcessCallbacks(c);\n//     }\n//     test_cond(__test_callback_flags == 0x010203);\n//     redisFree(c);\n//\n//     test(\"redisDisconnect executes pending callbacks with NULL reply: \");\n//     c = __connect_nonblock();\n//     redisSetDisconnectCallback(c,__test_callback,(void*)1);\n//     redisCommandWithCallback(c,__test_reply_callback,(void*)2,\"PING\");\n//     redisDisconnect(c);\n//     test_cond(__test_callback_flags == 0x0201);\n//     redisFree(c);\n// }\n\nint main(int argc, char **argv) {\n    struct config cfg = {\n        .tcp = {\n            .host = \"127.0.0.1\",\n            .port = 6379\n        },\n        .unix_sock = {\n            .path = \"/tmp/redis.sock\"\n        }\n    };\n    int throughput = 1;\n    int test_inherit_fd = 1;\n    int skips_as_fails = 0;\n    int test_unix_socket;\n\n    /* Parse command line options. */\n    argv++; argc--;\n    while (argc) {\n        if (argc >= 2 && !strcmp(argv[0],\"-h\")) {\n            argv++; argc--;\n            cfg.tcp.host = argv[0];\n        } else if (argc >= 2 && !strcmp(argv[0],\"-p\")) {\n            argv++; argc--;\n            cfg.tcp.port = atoi(argv[0]);\n        } else if (argc >= 2 && !strcmp(argv[0],\"-s\")) {\n            argv++; argc--;\n            cfg.unix_sock.path = argv[0];\n        } else if (argc >= 1 && !strcmp(argv[0],\"--skip-throughput\")) {\n            throughput = 0;\n        } else if (argc >= 1 && !strcmp(argv[0],\"--skip-inherit-fd\")) {\n            test_inherit_fd = 0;\n        } else if (argc >= 1 && !strcmp(argv[0],\"--skips-as-fails\")) {\n            skips_as_fails = 1;\n#ifdef HIREDIS_TEST_SSL\n        } else if (argc >= 2 && !strcmp(argv[0],\"--ssl-port\")) {\n            argv++; argc--;\n            cfg.ssl.port = atoi(argv[0]);\n        } else if (argc >= 2 && !strcmp(argv[0],\"--ssl-host\")) {\n            argv++; argc--;\n            cfg.ssl.host = argv[0];\n        } else if (argc >= 2 && !strcmp(argv[0],\"--ssl-ca-cert\")) {\n            argv++; argc--;\n            cfg.ssl.ca_cert  = argv[0];\n        } else if (argc >= 2 && !strcmp(argv[0],\"--ssl-cert\")) {\n            argv++; argc--;\n            cfg.ssl.cert = argv[0];\n        } else if (argc >= 2 && !strcmp(argv[0],\"--ssl-key\")) {\n            argv++; argc--;\n            cfg.ssl.key = argv[0];\n#endif\n        } else {\n            fprintf(stderr, \"Invalid argument: %s\\n\", argv[0]);\n            exit(1);\n        }\n        argv++; argc--;\n    }\n\n#ifndef _WIN32\n    /* Ignore broken pipe signal (for I/O error tests). */\n    signal(SIGPIPE, SIG_IGN);\n\n    test_unix_socket = access(cfg.unix_sock.path, F_OK) == 0;\n\n#else\n    /* Unix sockets don't exist in Windows */\n    test_unix_socket = 0;\n#endif\n\n    test_allocator_injection();\n\n    test_format_commands();\n    test_reply_reader();\n    test_blocking_connection_errors();\n    test_free_null();\n\n    printf(\"\\nTesting against TCP connection (%s:%d):\\n\", cfg.tcp.host, cfg.tcp.port);\n    cfg.type = CONN_TCP;\n    test_blocking_connection(cfg);\n    test_blocking_connection_timeouts(cfg);\n    test_blocking_io_errors(cfg);\n    test_invalid_timeout_errors(cfg);\n    test_append_formatted_commands(cfg);\n    if (throughput) test_throughput(cfg);\n\n    printf(\"\\nTesting against Unix socket connection (%s): \", cfg.unix_sock.path);\n    if (test_unix_socket) {\n        printf(\"\\n\");\n        cfg.type = CONN_UNIX;\n        test_blocking_connection(cfg);\n        test_blocking_connection_timeouts(cfg);\n        test_blocking_io_errors(cfg);\n        if (throughput) test_throughput(cfg);\n    } else {\n        test_skipped();\n    }\n\n#ifdef HIREDIS_TEST_SSL\n    if (cfg.ssl.port && cfg.ssl.host) {\n\n        redisInitOpenSSL();\n        _ssl_ctx = redisCreateSSLContext(cfg.ssl.ca_cert, NULL, cfg.ssl.cert, cfg.ssl.key, NULL, NULL);\n        assert(_ssl_ctx != NULL);\n\n        printf(\"\\nTesting against SSL connection (%s:%d):\\n\", cfg.ssl.host, cfg.ssl.port);\n        cfg.type = CONN_SSL;\n\n        test_blocking_connection(cfg);\n        test_blocking_connection_timeouts(cfg);\n        test_blocking_io_errors(cfg);\n        test_invalid_timeout_errors(cfg);\n        test_append_formatted_commands(cfg);\n        if (throughput) test_throughput(cfg);\n\n        redisFreeSSLContext(_ssl_ctx);\n        _ssl_ctx = NULL;\n    }\n#endif\n\n    if (test_inherit_fd) {\n        printf(\"\\nTesting against inherited fd (%s): \", cfg.unix_sock.path);\n        if (test_unix_socket) {\n            printf(\"\\n\");\n            cfg.type = CONN_FD;\n            test_blocking_connection(cfg);\n        } else {\n            test_skipped();\n        }\n    }\n\n    if (fails || (skips_as_fails && skips)) {\n        printf(\"*** %d TESTS FAILED ***\\n\", fails);\n        if (skips) {\n            printf(\"*** %d TESTS SKIPPED ***\\n\", skips);\n        }\n        return 1;\n    }\n\n    printf(\"ALL TESTS PASSED (%d skipped)\\n\", skips);\n    return 0;\n}\n"
  },
  {
    "path": "deps/hiredis/test.sh",
    "content": "#!/bin/sh -ue\n\nREDIS_SERVER=${REDIS_SERVER:-redis-server}\nREDIS_PORT=${REDIS_PORT:-56379}\nREDIS_SSL_PORT=${REDIS_SSL_PORT:-56443}\nTEST_SSL=${TEST_SSL:-0}\nSKIPS_AS_FAILS=${SKIPS_AS_FAILS-:0}\nSSL_TEST_ARGS=\nSKIPS_ARG=\n\ntmpdir=$(mktemp -d)\nPID_FILE=${tmpdir}/hiredis-test-redis.pid\nSOCK_FILE=${tmpdir}/hiredis-test-redis.sock\n\nif [ \"$TEST_SSL\" = \"1\" ]; then\n    SSL_CA_CERT=${tmpdir}/ca.crt\n    SSL_CA_KEY=${tmpdir}/ca.key\n    SSL_CERT=${tmpdir}/redis.crt\n    SSL_KEY=${tmpdir}/redis.key\n\n    openssl genrsa -out ${tmpdir}/ca.key 4096\n    openssl req \\\n        -x509 -new -nodes -sha256 \\\n        -key ${SSL_CA_KEY} \\\n        -days 3650 \\\n        -subj '/CN=Hiredis Test CA' \\\n        -out ${SSL_CA_CERT}\n    openssl genrsa -out ${SSL_KEY} 2048\n    openssl req \\\n        -new -sha256 \\\n        -key ${SSL_KEY} \\\n        -subj '/CN=Hiredis Test Cert' | \\\n        openssl x509 \\\n            -req -sha256 \\\n            -CA ${SSL_CA_CERT} \\\n            -CAkey ${SSL_CA_KEY} \\\n            -CAserial ${tmpdir}/ca.txt \\\n            -CAcreateserial \\\n            -days 365 \\\n            -out ${SSL_CERT}\n\n    SSL_TEST_ARGS=\"--ssl-host 127.0.0.1 --ssl-port ${REDIS_SSL_PORT} --ssl-ca-cert ${SSL_CA_CERT} --ssl-cert ${SSL_CERT} --ssl-key ${SSL_KEY}\"\nfi\n\ncleanup() {\n  set +e\n  kill $(cat ${PID_FILE})\n  rm -rf ${tmpdir}\n}\ntrap cleanup INT TERM EXIT\n\ncat > ${tmpdir}/redis.conf <<EOF\ndaemonize yes\npidfile ${PID_FILE}\nport ${REDIS_PORT}\nbind 127.0.0.1\nunixsocket ${SOCK_FILE}\nEOF\n\nif [ \"$TEST_SSL\" = \"1\" ]; then\n    cat >> ${tmpdir}/redis.conf <<EOF\ntls-port ${REDIS_SSL_PORT}\ntls-ca-cert-file ${SSL_CA_CERT}\ntls-cert-file ${SSL_CERT}\ntls-key-file ${SSL_KEY}\nEOF\nfi\n\ncat ${tmpdir}/redis.conf\n${REDIS_SERVER} ${tmpdir}/redis.conf\n\n# Wait until we detect the unix socket\nwhile [ ! -S \"${SOCK_FILE}\" ]; do sleep 1; done\n\n# Treat skips as failures if directed\n[ \"$SKIPS_AS_FAILS\" = 1 ] && SKIPS_ARG=\"--skips-as-fails\"\n\n${TEST_PREFIX:-} ./hiredis-test -h 127.0.0.1 -p ${REDIS_PORT} -s ${SOCK_FILE} ${SSL_TEST_ARGS} ${SKIPS_ARG}\n"
  },
  {
    "path": "deps/hiredis/win32.h",
    "content": "#ifndef _WIN32_HELPER_INCLUDE\n#define _WIN32_HELPER_INCLUDE\n#ifdef _MSC_VER\n\n#include <winsock2.h> /* for struct timeval */\n\n#ifndef inline\n#define inline __inline\n#endif\n\n#ifndef strcasecmp\n#define strcasecmp stricmp\n#endif\n\n#ifndef strncasecmp\n#define strncasecmp strnicmp\n#endif\n\n#ifndef va_copy\n#define va_copy(d,s) ((d) = (s))\n#endif\n\n#ifndef snprintf\n#define snprintf c99_snprintf\n\n__inline int c99_vsnprintf(char* str, size_t size, const char* format, va_list ap)\n{\n    int count = -1;\n\n    if (size != 0)\n        count = _vsnprintf_s(str, size, _TRUNCATE, format, ap);\n    if (count == -1)\n        count = _vscprintf(format, ap);\n\n    return count;\n}\n\n__inline int c99_snprintf(char* str, size_t size, const char* format, ...)\n{\n    int count;\n    va_list ap;\n\n    va_start(ap, format);\n    count = c99_vsnprintf(str, size, format, ap);\n    va_end(ap);\n\n    return count;\n}\n#endif\n#endif /* _MSC_VER */\n\n#ifdef _WIN32\n#define strerror_r(errno,buf,len) strerror_s(buf,len,errno)\n#endif /* _WIN32 */\n\n#endif /* _WIN32_HELPER_INCLUDE */\n"
  },
  {
    "path": "deps/jemalloc/.appveyor.yml",
    "content": "version: '{build}'\n\nenvironment:\n  matrix:\n  - MSYSTEM: MINGW64\n    CPU: x86_64\n    MSVC: amd64\n    CONFIG_FLAGS: --enable-debug\n  - MSYSTEM: MINGW64\n    CPU: x86_64\n    CONFIG_FLAGS: --enable-debug\n  - MSYSTEM: MINGW32\n    CPU: i686\n    MSVC: x86\n    CONFIG_FLAGS: --enable-debug\n  - MSYSTEM: MINGW32\n    CPU: i686\n    CONFIG_FLAGS: --enable-debug\n  - MSYSTEM: MINGW64\n    CPU: x86_64\n    MSVC: amd64\n  - MSYSTEM: MINGW64\n    CPU: x86_64\n  - MSYSTEM: MINGW32\n    CPU: i686\n    MSVC: x86\n  - MSYSTEM: MINGW32\n    CPU: i686\n\ninstall:\n  - set PATH=c:\\msys64\\%MSYSTEM%\\bin;c:\\msys64\\usr\\bin;%PATH%\n  - if defined MSVC call \"c:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\vcvarsall.bat\" %MSVC%\n  - if defined MSVC pacman --noconfirm -Rsc mingw-w64-%CPU%-gcc gcc\n  - pacman --noconfirm -Suy mingw-w64-%CPU%-make\n\nbuild_script:\n  - bash -c \"autoconf\"\n  - bash -c \"./configure $CONFIG_FLAGS\"\n  - mingw32-make\n  - file lib/jemalloc.dll\n  - mingw32-make tests\n  - mingw32-make -k check\n"
  },
  {
    "path": "deps/jemalloc/.autom4te.cfg",
    "content": "begin-language: \"Autoconf-without-aclocal-m4\"\nargs: --no-cache\nend-language: \"Autoconf-without-aclocal-m4\"\n"
  },
  {
    "path": "deps/jemalloc/.cirrus.yml",
    "content": "env:\n  CIRRUS_CLONE_DEPTH: 1\n  ARCH: amd64\n\ntask:\n  freebsd_instance:\n    matrix:\n      image: freebsd-12-0-release-amd64\n      image: freebsd-11-2-release-amd64\n  install_script:\n    - sed -i.bak -e 's,pkg+http://pkg.FreeBSD.org/\\${ABI}/quarterly,pkg+http://pkg.FreeBSD.org/\\${ABI}/latest,' /etc/pkg/FreeBSD.conf\n    - pkg upgrade -y\n    - pkg install -y autoconf gmake\n  script:\n    - autoconf\n    #- ./configure ${COMPILER_FLAGS:+       CC=\"$CC $COMPILER_FLAGS\"       CXX=\"$CXX $COMPILER_FLAGS\" }       $CONFIGURE_FLAGS\n    - ./configure\n    - export JFLAG=`sysctl -n kern.smp.cpus`\n    - gmake -j${JFLAG}\n    - gmake -j${JFLAG} tests\n    - gmake check\n"
  },
  {
    "path": "deps/jemalloc/.gitattributes",
    "content": "* text=auto eol=lf\n"
  },
  {
    "path": "deps/jemalloc/.gitignore",
    "content": "/bin/jemalloc-config\n/bin/jemalloc.sh\n/bin/jeprof\n\n/config.stamp\n/config.log\n/config.status\n/configure\n\n/doc/html.xsl\n/doc/manpages.xsl\n/doc/jemalloc.xml\n/doc/jemalloc.html\n/doc/jemalloc.3\n\n/jemalloc.pc\n\n/lib/\n\n/Makefile\n\n/include/jemalloc/internal/jemalloc_preamble.h\n/include/jemalloc/internal/jemalloc_internal_defs.h\n/include/jemalloc/internal/private_namespace.gen.h\n/include/jemalloc/internal/private_namespace.h\n/include/jemalloc/internal/private_namespace_jet.gen.h\n/include/jemalloc/internal/private_namespace_jet.h\n/include/jemalloc/internal/private_symbols.awk\n/include/jemalloc/internal/private_symbols_jet.awk\n/include/jemalloc/internal/public_namespace.h\n/include/jemalloc/internal/public_symbols.txt\n/include/jemalloc/internal/public_unnamespace.h\n/include/jemalloc/jemalloc.h\n/include/jemalloc/jemalloc_defs.h\n/include/jemalloc/jemalloc_macros.h\n/include/jemalloc/jemalloc_mangle.h\n/include/jemalloc/jemalloc_mangle_jet.h\n/include/jemalloc/jemalloc_protos.h\n/include/jemalloc/jemalloc_protos_jet.h\n/include/jemalloc/jemalloc_rename.h\n/include/jemalloc/jemalloc_typedefs.h\n\n/src/*.[od]\n/src/*.sym\n\n/run_tests.out/\n\n/test/test.sh\ntest/include/test/jemalloc_test.h\ntest/include/test/jemalloc_test_defs.h\n\n/test/integration/[A-Za-z]*\n!/test/integration/[A-Za-z]*.*\n/test/integration/*.[od]\n/test/integration/*.out\n\n/test/integration/cpp/[A-Za-z]*\n!/test/integration/cpp/[A-Za-z]*.*\n/test/integration/cpp/*.[od]\n/test/integration/cpp/*.out\n\n/test/src/*.[od]\n\n/test/stress/[A-Za-z]*\n!/test/stress/[A-Za-z]*.*\n/test/stress/*.[od]\n/test/stress/*.out\n\n/test/unit/[A-Za-z]*\n!/test/unit/[A-Za-z]*.*\n/test/unit/*.[od]\n/test/unit/*.out\n\n/VERSION\n\n*.pdb\n*.sdf\n*.opendb\n*.VC.db\n*.opensdf\n*.cachefile\n*.suo\n*.user\n*.sln.docstates\n*.tmp\n.vs/\n/msvc/Win32/\n/msvc/x64/\n/msvc/projects/*/*/Debug*/\n/msvc/projects/*/*/Release*/\n/msvc/projects/*/*/Win32/\n/msvc/projects/*/*/x64/\n"
  },
  {
    "path": "deps/jemalloc/.travis.yml",
    "content": "language: generic\ndist: precise\n\nmatrix:\n  include:\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: osx\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=clang CXX=clang++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"-m32\" CONFIGURE_FLAGS=\"\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n      addons: &gcc_multilib\n        apt:\n          packages:\n            - gcc-multilib\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-debug\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-prof\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--disable-stats\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--disable-libdl\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-opt-safety-checks\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=tcache:false\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=dss:primary\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=percpu_arena:percpu\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=background_thread:true\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: osx\n      env: CC=clang CXX=clang++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: osx\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"-m32\" CONFIGURE_FLAGS=\"\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: osx\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-debug\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: osx\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--disable-stats\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: osx\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--disable-libdl\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: osx\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-opt-safety-checks\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: osx\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=tcache:false\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=clang CXX=clang++ COMPILER_FLAGS=\"-m32\" CONFIGURE_FLAGS=\"\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n      addons: *gcc_multilib\n    - os: linux\n      env: CC=clang CXX=clang++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-debug\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=clang CXX=clang++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-prof\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=clang CXX=clang++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--disable-stats\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=clang CXX=clang++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--disable-libdl\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=clang CXX=clang++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-opt-safety-checks\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=clang CXX=clang++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=tcache:false\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=clang CXX=clang++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=dss:primary\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=clang CXX=clang++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=percpu_arena:percpu\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=clang CXX=clang++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=background_thread:true\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"-m32\" CONFIGURE_FLAGS=\"--enable-debug\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n      addons: *gcc_multilib\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"-m32\" CONFIGURE_FLAGS=\"--enable-prof\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n      addons: *gcc_multilib\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"-m32\" CONFIGURE_FLAGS=\"--disable-stats\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n      addons: *gcc_multilib\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"-m32\" CONFIGURE_FLAGS=\"--disable-libdl\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n      addons: *gcc_multilib\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"-m32\" CONFIGURE_FLAGS=\"--enable-opt-safety-checks\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n      addons: *gcc_multilib\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"-m32\" CONFIGURE_FLAGS=\"--with-malloc-conf=tcache:false\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n      addons: *gcc_multilib\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"-m32\" CONFIGURE_FLAGS=\"--with-malloc-conf=dss:primary\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n      addons: *gcc_multilib\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"-m32\" CONFIGURE_FLAGS=\"--with-malloc-conf=percpu_arena:percpu\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n      addons: *gcc_multilib\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"-m32\" CONFIGURE_FLAGS=\"--with-malloc-conf=background_thread:true\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n      addons: *gcc_multilib\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-debug --enable-prof\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-debug --disable-stats\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-debug --disable-libdl\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-debug --enable-opt-safety-checks\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-debug --with-malloc-conf=tcache:false\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-debug --with-malloc-conf=dss:primary\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-debug --with-malloc-conf=percpu_arena:percpu\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-debug --with-malloc-conf=background_thread:true\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-prof --disable-stats\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-prof --disable-libdl\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-prof --enable-opt-safety-checks\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-prof --with-malloc-conf=tcache:false\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-prof --with-malloc-conf=dss:primary\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-prof --with-malloc-conf=percpu_arena:percpu\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-prof --with-malloc-conf=background_thread:true\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--disable-stats --disable-libdl\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--disable-stats --enable-opt-safety-checks\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--disable-stats --with-malloc-conf=tcache:false\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--disable-stats --with-malloc-conf=dss:primary\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--disable-stats --with-malloc-conf=percpu_arena:percpu\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--disable-stats --with-malloc-conf=background_thread:true\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--disable-libdl --enable-opt-safety-checks\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--disable-libdl --with-malloc-conf=tcache:false\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--disable-libdl --with-malloc-conf=dss:primary\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--disable-libdl --with-malloc-conf=percpu_arena:percpu\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--disable-libdl --with-malloc-conf=background_thread:true\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-opt-safety-checks --with-malloc-conf=tcache:false\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-opt-safety-checks --with-malloc-conf=dss:primary\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-opt-safety-checks --with-malloc-conf=percpu_arena:percpu\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-opt-safety-checks --with-malloc-conf=background_thread:true\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=tcache:false,dss:primary\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=tcache:false,percpu_arena:percpu\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=tcache:false,background_thread:true\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=dss:primary,percpu_arena:percpu\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=dss:primary,background_thread:true\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=percpu_arena:percpu,background_thread:true\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    # Development build\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-debug --disable-cache-oblivious --enable-stats --enable-log --enable-prof\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    # --enable-expermental-smallocx:\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-debug --enable-experimental-smallocx --enable-stats --enable-prof\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n\n    # Valgrind\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\" JEMALLOC_TEST_PREFIX=\"valgrind\"\n      addons:\n        apt:\n          packages:\n            - valgrind\n\n\nbefore_script:\n  - autoconf\n  - scripts/gen_travis.py > travis_script && diff .travis.yml travis_script\n  - ./configure ${COMPILER_FLAGS:+       CC=\"$CC $COMPILER_FLAGS\"       CXX=\"$CXX $COMPILER_FLAGS\" }       $CONFIGURE_FLAGS\n  - make -j3\n  - make -j3 tests\n\nscript:\n  - make check\n\n"
  },
  {
    "path": "deps/jemalloc/COPYING",
    "content": "Unless otherwise specified, files in the jemalloc source distribution are\nsubject to the following license:\n--------------------------------------------------------------------------------\nCopyright (C) 2002-present Jason Evans <jasone@canonware.com>.\nAll rights reserved.\nCopyright (C) 2007-2012 Mozilla Foundation.  All rights reserved.\nCopyright (C) 2009-present Facebook, Inc.  All rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are met:\n1. Redistributions of source code must retain the above copyright notice(s),\n   this list of conditions and the following disclaimer.\n2. Redistributions in binary form must reproduce the above copyright notice(s),\n   this list of conditions and the following disclaimer in the documentation\n   and/or other materials provided with the distribution.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\nOR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\nMERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\nEVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\nINCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\nLIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\nPROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\nLIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\nOR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\nADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n--------------------------------------------------------------------------------\n"
  },
  {
    "path": "deps/jemalloc/ChangeLog",
    "content": "Following are change highlights associated with official releases.  Important\nbug fixes are all mentioned, but some internal enhancements are omitted here for\nbrevity.  Much more detail can be found in the git revision history:\n\n    https://github.com/jemalloc/jemalloc\n\n* 5.2.1 (August 5, 2019)\n\n  This release is primarily about Windows.  A critical virtual memory leak is\n  resolved on all Windows platforms.  The regression was present in all releases\n  since 5.0.0.\n\n  Bug fixes:\n  - Fix a severe virtual memory leak on Windows.  This regression was first\n    released in 5.0.0.  (@Ignition, @j0t, @frederik-h, @davidtgoldblatt,\n    @interwq)\n  - Fix size 0 handling in posix_memalign().  This regression was first released\n    in 5.2.0.  (@interwq)\n  - Fix the prof_log unit test which may observe unexpected backtraces from\n    compiler optimizations.  The test was first added in 5.2.0.  (@marxin,\n    @gnzlbg, @interwq)\n  - Fix the declaration of the extent_avail tree.  This regression was first\n    released in 5.1.0.  (@zoulasc)\n  - Fix an incorrect reference in jeprof.  This functionality was first released\n    in 3.0.0.  (@prehistoric-penguin)\n  - Fix an assertion on the deallocation fast-path.  This regression was first\n    released in 5.2.0.  (@yinan1048576)\n  - Fix the TLS_MODEL attribute in headers.  This regression was first released\n    in 5.0.0.  (@zoulasc, @interwq)\n\n  Optimizations and refactors:\n  - Implement opt.retain on Windows and enable by default on 64-bit.  (@interwq,\n    @davidtgoldblatt)\n  - Optimize away a branch on the operator delete[] path.  (@mgrice)\n  - Add format annotation to the format generator function.  (@zoulasc)\n  - Refactor and improve the size class header generation.  (@yinan1048576)\n  - Remove best fit.  (@djwatson)\n  - Avoid blocking on background thread locks for stats.  (@oranagra, @interwq)\n\n* 5.2.0 (April 2, 2019)\n\n  This release includes a few notable improvements, which are summarized below:\n  1) improved fast-path performance from the optimizations by @djwatson; 2)\n  reduced virtual memory fragmentation and metadata usage; and 3) bug fixes on\n  setting the number of background threads.  In addition, peak / spike memory\n  usage is improved with certain allocation patterns.  As usual, the release and\n  prior dev versions have gone through large-scale production testing.\n\n  New features:\n  - Implement oversize_threshold, which uses a dedicated arena for allocations\n    crossing the specified threshold to reduce fragmentation.  (@interwq)\n  - Add extents usage information to stats.  (@tyleretzel)\n  - Log time information for sampled allocations.  (@tyleretzel)\n  - Support 0 size in sdallocx.  (@djwatson)\n  - Output rate for certain counters in malloc_stats.  (@zinoale)\n  - Add configure option --enable-readlinkat, which allows the use of readlinkat\n    over readlink.  (@davidtgoldblatt)\n  - Add configure options --{enable,disable}-{static,shared} to allow not\n    building unwanted libraries.  (@Ericson2314)\n  - Add configure option --disable-libdl to enable fully static builds.\n    (@interwq)\n  - Add mallctl interfaces:\n\t+ opt.oversize_threshold (@interwq)\n\t+ stats.arenas.<i>.extent_avail (@tyleretzel)\n\t+ stats.arenas.<i>.extents.<j>.n{dirty,muzzy,retained} (@tyleretzel)\n\t+ stats.arenas.<i>.extents.<j>.{dirty,muzzy,retained}_bytes\n\t  (@tyleretzel)\n\n  Portability improvements:\n  - Update MSVC builds.  (@maksqwe, @rustyx)\n  - Workaround a compiler optimizer bug on s390x.  (@rkmisra)\n  - Make use of pthread_set_name_np(3) on FreeBSD.  (@trasz)\n  - Implement malloc_getcpu() to enable percpu_arena for windows.  (@santagada)\n  - Link against -pthread instead of -lpthread.  (@paravoid)\n  - Make background_thread not dependent on libdl.  (@interwq)\n  - Add stringify to fix a linker directive issue on MSVC.  (@daverigby)\n  - Detect and fall back when 8-bit atomics are unavailable.  (@interwq)\n  - Fall back to the default pthread_create if dlsym(3) fails.  (@interwq)\n\n  Optimizations and refactors:\n  - Refactor the TSD module.  (@davidtgoldblatt)\n  - Avoid taking extents_muzzy mutex when muzzy is disabled.  (@interwq)\n  - Avoid taking large_mtx for auto arenas on the tcache flush path.  (@interwq)\n  - Optimize ixalloc by avoiding a size lookup.  (@interwq)\n  - Implement opt.oversize_threshold which uses a dedicated arena for requests\n    crossing the threshold, also eagerly purges the oversize extents.  Default\n    the threshold to 8 MiB.  (@interwq)\n  - Clean compilation with -Wextra.  (@gnzlbg, @jasone)\n  - Refactor the size class module.  (@davidtgoldblatt)\n  - Refactor the stats emitter.  (@tyleretzel)\n  - Optimize pow2_ceil.  (@rkmisra)\n  - Avoid runtime detection of lazy purging on FreeBSD.  (@trasz)\n  - Optimize mmap(2) alignment handling on FreeBSD.  (@trasz)\n  - Improve error handling for THP state initialization.  (@jsteemann)\n  - Rework the malloc() fast path.  (@djwatson)\n  - Rework the free() fast path.  (@djwatson)\n  - Refactor and optimize the tcache fill / flush paths.  (@djwatson)\n  - Optimize sync / lwsync on PowerPC.  (@chmeeedalf)\n  - Bypass extent_dalloc() when retain is enabled.  (@interwq)\n  - Optimize the locking on large deallocation.  (@interwq)\n  - Reduce the number of pages committed from sanity checking in debug build.\n    (@trasz, @interwq)\n  - Deprecate OSSpinLock.  (@interwq)\n  - Lower the default number of background threads to 4 (when the feature\n    is enabled).  (@interwq)\n  - Optimize the trylock spin wait.  (@djwatson)\n  - Use arena index for arena-matching checks.  (@interwq)\n  - Avoid forced decay on thread termination when using background threads.\n    (@interwq)\n  - Disable muzzy decay by default.  (@djwatson, @interwq)\n  - Only initialize libgcc unwinder when profiling is enabled.  (@paravoid,\n    @interwq)\n\n  Bug fixes (all only relevant to jemalloc 5.x):\n  - Fix background thread index issues with max_background_threads.  (@djwatson,\n    @interwq)\n  - Fix stats output for opt.lg_extent_max_active_fit.  (@interwq)\n  - Fix opt.prof_prefix initialization.  (@davidtgoldblatt)\n  - Properly trigger decay on tcache destroy.  (@interwq, @amosbird)\n  - Fix tcache.flush.  (@interwq)\n  - Detect whether explicit extent zero out is necessary with huge pages or\n    custom extent hooks, which may change the purge semantics.  (@interwq)\n  - Fix a side effect caused by extent_max_active_fit combined with decay-based\n    purging, where freed extents can accumulate and not be reused for an\n    extended period of time.  (@interwq, @mpghf)\n  - Fix a missing unlock on extent register error handling.  (@zoulasc)\n\n  Testing:\n  - Simplify the Travis script output.  (@gnzlbg)\n  - Update the test scripts for FreeBSD.  (@devnexen)\n  - Add unit tests for the producer-consumer pattern.  (@interwq)\n  - Add Cirrus-CI config for FreeBSD builds.  (@jasone)\n  - Add size-matching sanity checks on tcache flush.  (@davidtgoldblatt,\n    @interwq)\n\n  Incompatible changes:\n  - Remove --with-lg-page-sizes.  (@davidtgoldblatt)\n\n  Documentation:\n  - Attempt to build docs by default, however skip doc building when xsltproc\n    is missing. (@interwq, @cmuellner)\n\n* 5.1.0 (May 4, 2018)\n\n  This release is primarily about fine-tuning, ranging from several new features\n  to numerous notable performance and portability enhancements.  The release and\n  prior dev versions have been running in multiple large scale applications for\n  months, and the cumulative improvements are substantial in many cases.\n\n  Given the long and successful production runs, this release is likely a good\n  candidate for applications to upgrade, from both jemalloc 5.0 and before.  For\n  performance-critical applications, the newly added TUNING.md provides\n  guidelines on jemalloc tuning.\n\n  New features:\n  - Implement transparent huge page support for internal metadata.  (@interwq)\n  - Add opt.thp to allow enabling / disabling transparent huge pages for all\n    mappings.  (@interwq)\n  - Add maximum background thread count option.  (@djwatson)\n  - Allow prof_active to control opt.lg_prof_interval and prof.gdump.\n    (@interwq)\n  - Allow arena index lookup based on allocation addresses via mallctl.\n    (@lionkov)\n  - Allow disabling initial-exec TLS model.  (@davidtgoldblatt, @KenMacD)\n  - Add opt.lg_extent_max_active_fit to set the max ratio between the size of\n    the active extent selected (to split off from) and the size of the requested\n    allocation.  (@interwq, @davidtgoldblatt)\n  - Add retain_grow_limit to set the max size when growing virtual address\n    space.  (@interwq)\n  - Add mallctl interfaces:\n    + arena.<i>.retain_grow_limit  (@interwq)\n    + arenas.lookup  (@lionkov)\n    + max_background_threads  (@djwatson)\n    + opt.lg_extent_max_active_fit  (@interwq)\n    + opt.max_background_threads  (@djwatson)\n    + opt.metadata_thp  (@interwq)\n    + opt.thp  (@interwq)\n    + stats.metadata_thp  (@interwq)\n\n  Portability improvements:\n  - Support GNU/kFreeBSD configuration.  (@paravoid)\n  - Support m68k, nios2 and SH3 architectures.  (@paravoid)\n  - Fall back to FD_CLOEXEC when O_CLOEXEC is unavailable.  (@zonyitoo)\n  - Fix symbol listing for cross-compiling.  (@tamird)\n  - Fix high bits computation on ARM.  (@davidtgoldblatt, @paravoid)\n  - Disable the CPU_SPINWAIT macro for Power.  (@davidtgoldblatt, @marxin)\n  - Fix MSVC 2015 & 2017 builds.  (@rustyx)\n  - Improve RISC-V support.  (@EdSchouten)\n  - Set name mangling script in strict mode.  (@nicolov)\n  - Avoid MADV_HUGEPAGE on ARM.  (@marxin)\n  - Modify configure to determine return value of strerror_r.\n    (@davidtgoldblatt, @cferris1000)\n  - Make sure CXXFLAGS is tested with CPP compiler.  (@nehaljwani)\n  - Fix 32-bit build on MSVC.  (@rustyx)\n  - Fix external symbol on MSVC.  (@maksqwe)\n  - Avoid a printf format specifier warning.  (@jasone)\n  - Add configure option --disable-initial-exec-tls which can allow jemalloc to\n    be dynamically loaded after program startup.  (@davidtgoldblatt, @KenMacD)\n  - AArch64: Add ILP32 support.  (@cmuellner)\n  - Add --with-lg-vaddr configure option to support cross compiling.\n    (@cmuellner, @davidtgoldblatt)\n\n  Optimizations and refactors:\n  - Improve active extent fit with extent_max_active_fit.  This considerably\n    reduces fragmentation over time and improves virtual memory and metadata\n    usage.  (@davidtgoldblatt, @interwq)\n  - Eagerly coalesce large extents to reduce fragmentation.  (@interwq)\n  - sdallocx: only read size info when page aligned (i.e. possibly sampled),\n    which speeds up the sized deallocation path significantly.  (@interwq)\n  - Avoid attempting new mappings for in place expansion with retain, since\n    it rarely succeeds in practice and causes high overhead.  (@interwq)\n  - Refactor OOM handling in newImpl.  (@wqfish)\n  - Add internal fine-grained logging functionality for debugging use.\n    (@davidtgoldblatt)\n  - Refactor arena / tcache interactions.  (@davidtgoldblatt)\n  - Refactor extent management with dumpable flag.  (@davidtgoldblatt)\n  - Add runtime detection of lazy purging.  (@interwq)\n  - Use pairing heap instead of red-black tree for extents_avail.  (@djwatson)\n  - Use sysctl on startup in FreeBSD.  (@trasz)\n  - Use thread local prng state instead of atomic.  (@djwatson)\n  - Make decay to always purge one more extent than before, because in\n    practice large extents are usually the ones that cross the decay threshold.\n    Purging the additional extent helps save memory as well as reduce VM\n    fragmentation.  (@interwq)\n  - Fast division by dynamic values.  (@davidtgoldblatt)\n  - Improve the fit for aligned allocation.  (@interwq, @edwinsmith)\n  - Refactor extent_t bitpacking.  (@rkmisra)\n  - Optimize the generated assembly for ticker operations.  (@davidtgoldblatt)\n  - Convert stats printing to use a structured text emitter.  (@davidtgoldblatt)\n  - Remove preserve_lru feature for extents management.  (@djwatson)\n  - Consolidate two memory loads into one on the fast deallocation path.\n    (@davidtgoldblatt, @interwq)\n\n  Bug fixes (most of the issues are only relevant to jemalloc 5.0):\n  - Fix deadlock with multithreaded fork in OS X.  (@davidtgoldblatt)\n  - Validate returned file descriptor before use.  (@zonyitoo)\n  - Fix a few background thread initialization and shutdown issues.  (@interwq)\n  - Fix an extent coalesce + decay race by taking both coalescing extents off\n    the LRU list.  (@interwq)\n  - Fix potentially unbound increase during decay, caused by one thread keep\n    stashing memory to purge while other threads generating new pages.  The\n    number of pages to purge is checked to prevent this.  (@interwq)\n  - Fix a FreeBSD bootstrap assertion.  (@strejda, @interwq)\n  - Handle 32 bit mutex counters.  (@rkmisra)\n  - Fix a indexing bug when creating background threads.  (@davidtgoldblatt,\n    @binliu19)\n  - Fix arguments passed to extent_init.  (@yuleniwo, @interwq)\n  - Fix addresses used for ordering mutexes.  (@rkmisra)\n  - Fix abort_conf processing during bootstrap.  (@interwq)\n  - Fix include path order for out-of-tree builds.  (@cmuellner)\n\n  Incompatible changes:\n  - Remove --disable-thp.  (@interwq)\n  - Remove mallctl interfaces:\n    + config.thp  (@interwq)\n\n  Documentation:\n  - Add TUNING.md.  (@interwq, @davidtgoldblatt, @djwatson)\n\n* 5.0.1 (July 1, 2017)\n\n  This bugfix release fixes several issues, most of which are obscure enough\n  that typical applications are not impacted.\n\n  Bug fixes:\n  - Update decay->nunpurged before purging, in order to avoid potential update\n    races and subsequent incorrect purging volume.  (@interwq)\n  - Only abort on dlsym(3) error if the failure impacts an enabled feature (lazy\n    locking and/or background threads).  This mitigates an initialization\n    failure bug for which we still do not have a clear reproduction test case.\n    (@interwq)\n  - Modify tsd management so that it neither crashes nor leaks if a thread's\n    only allocation activity is to call free() after TLS destructors have been\n    executed.  This behavior was observed when operating with GNU libc, and is\n    unlikely to be an issue with other libc implementations.  (@interwq)\n  - Mask signals during background thread creation.  This prevents signals from\n    being inadvertently delivered to background threads.  (@jasone,\n    @davidtgoldblatt, @interwq)\n  - Avoid inactivity checks within background threads, in order to prevent\n    recursive mutex acquisition.  (@interwq)\n  - Fix extent_grow_retained() to use the specified hooks when the\n    arena.<i>.extent_hooks mallctl is used to override the default hooks.\n    (@interwq)\n  - Add missing reentrancy support for custom extent hooks which allocate.\n    (@interwq)\n  - Post-fork(2), re-initialize the list of tcaches associated with each arena\n    to contain no tcaches except the forking thread's.  (@interwq)\n  - Add missing post-fork(2) mutex reinitialization for extent_grow_mtx.  This\n    fixes potential deadlocks after fork(2).  (@interwq)\n  - Enforce minimum autoconf version (currently 2.68), since 2.63 is known to\n    generate corrupt configure scripts.  (@jasone)\n  - Ensure that the configured page size (--with-lg-page) is no larger than the\n    configured huge page size (--with-lg-hugepage).  (@jasone)\n\n* 5.0.0 (June 13, 2017)\n\n  Unlike all previous jemalloc releases, this release does not use naturally\n  aligned \"chunks\" for virtual memory management, and instead uses page-aligned\n  \"extents\".  This change has few externally visible effects, but the internal\n  impacts are... extensive.  Many other internal changes combine to make this\n  the most cohesively designed version of jemalloc so far, with ample\n  opportunity for further enhancements.\n\n  Continuous integration is now an integral aspect of development thanks to the\n  efforts of @davidtgoldblatt, and the dev branch tends to remain reasonably\n  stable on the tested platforms (Linux, FreeBSD, macOS, and Windows).  As a\n  side effect the official release frequency may decrease over time.\n\n  New features:\n  - Implement optional per-CPU arena support; threads choose which arena to use\n    based on current CPU rather than on fixed thread-->arena associations.\n    (@interwq)\n  - Implement two-phase decay of unused dirty pages.  Pages transition from\n    dirty-->muzzy-->clean, where the first phase transition relies on\n    madvise(... MADV_FREE) semantics, and the second phase transition discards\n    pages such that they are replaced with demand-zeroed pages on next access.\n    (@jasone)\n  - Increase decay time resolution from seconds to milliseconds.  (@jasone)\n  - Implement opt-in per CPU background threads, and use them for asynchronous\n    decay-driven unused dirty page purging.  (@interwq)\n  - Add mutex profiling, which collects a variety of statistics useful for\n    diagnosing overhead/contention issues.  (@interwq)\n  - Add C++ new/delete operator bindings.  (@djwatson)\n  - Support manually created arena destruction, such that all data and metadata\n    are discarded.  Add MALLCTL_ARENAS_DESTROYED for accessing merged stats\n    associated with destroyed arenas.  (@jasone)\n  - Add MALLCTL_ARENAS_ALL as a fixed index for use in accessing\n    merged/destroyed arena statistics via mallctl.  (@jasone)\n  - Add opt.abort_conf to optionally abort if invalid configuration options are\n    detected during initialization.  (@interwq)\n  - Add opt.stats_print_opts, so that e.g. JSON output can be selected for the\n    stats dumped during exit if opt.stats_print is true.  (@jasone)\n  - Add --with-version=VERSION for use when embedding jemalloc into another\n    project's git repository.  (@jasone)\n  - Add --disable-thp to support cross compiling.  (@jasone)\n  - Add --with-lg-hugepage to support cross compiling.  (@jasone)\n  - Add mallctl interfaces (various authors):\n    + background_thread\n    + opt.abort_conf\n    + opt.retain\n    + opt.percpu_arena\n    + opt.background_thread\n    + opt.{dirty,muzzy}_decay_ms\n    + opt.stats_print_opts\n    + arena.<i>.initialized\n    + arena.<i>.destroy\n    + arena.<i>.{dirty,muzzy}_decay_ms\n    + arena.<i>.extent_hooks\n    + arenas.{dirty,muzzy}_decay_ms\n    + arenas.bin.<i>.slab_size\n    + arenas.nlextents\n    + arenas.lextent.<i>.size\n    + arenas.create\n    + stats.background_thread.{num_threads,num_runs,run_interval}\n    + stats.mutexes.{ctl,background_thread,prof,reset}.\n      {num_ops,num_spin_acq,num_wait,max_wait_time,total_wait_time,max_num_thds,\n      num_owner_switch}\n    + stats.arenas.<i>.{dirty,muzzy}_decay_ms\n    + stats.arenas.<i>.uptime\n    + stats.arenas.<i>.{pmuzzy,base,internal,resident}\n    + stats.arenas.<i>.{dirty,muzzy}_{npurge,nmadvise,purged}\n    + stats.arenas.<i>.bins.<j>.{nslabs,reslabs,curslabs}\n    + stats.arenas.<i>.bins.<j>.mutex.\n      {num_ops,num_spin_acq,num_wait,max_wait_time,total_wait_time,max_num_thds,\n      num_owner_switch}\n    + stats.arenas.<i>.lextents.<j>.{nmalloc,ndalloc,nrequests,curlextents}\n    + stats.arenas.i.mutexes.{large,extent_avail,extents_dirty,extents_muzzy,\n      extents_retained,decay_dirty,decay_muzzy,base,tcache_list}.\n      {num_ops,num_spin_acq,num_wait,max_wait_time,total_wait_time,max_num_thds,\n      num_owner_switch}\n\n  Portability improvements:\n  - Improve reentrant allocation support, such that deadlock is less likely if\n    e.g. a system library call in turn allocates memory.  (@davidtgoldblatt,\n    @interwq)\n  - Support static linking of jemalloc with glibc.  (@djwatson)\n\n  Optimizations and refactors:\n  - Organize virtual memory as \"extents\" of virtual memory pages, rather than as\n    naturally aligned \"chunks\", and store all metadata in arbitrarily distant\n    locations.  This reduces virtual memory external fragmentation, and will\n    interact better with huge pages (not yet explicitly supported).  (@jasone)\n  - Fold large and huge size classes together; only small and large size classes\n    remain.  (@jasone)\n  - Unify the allocation paths, and merge most fast-path branching decisions.\n    (@davidtgoldblatt, @interwq)\n  - Embed per thread automatic tcache into thread-specific data, which reduces\n    conditional branches and dereferences.  Also reorganize tcache to increase\n    fast-path data locality.  (@interwq)\n  - Rewrite atomics to closely model the C11 API, convert various\n    synchronization from mutex-based to atomic, and use the explicit memory\n    ordering control to resolve various hypothetical races without increasing\n    synchronization overhead.  (@davidtgoldblatt)\n  - Extensively optimize rtree via various methods:\n    + Add multiple layers of rtree lookup caching, since rtree lookups are now\n      part of fast-path deallocation.  (@interwq)\n    + Determine rtree layout at compile time.  (@jasone)\n    + Make the tree shallower for common configurations.  (@jasone)\n    + Embed the root node in the top-level rtree data structure, thus avoiding\n      one level of indirection.  (@jasone)\n    + Further specialize leaf elements as compared to internal node elements,\n      and directly embed extent metadata needed for fast-path deallocation.\n      (@jasone)\n    + Ignore leading always-zero address bits (architecture-specific).\n      (@jasone)\n  - Reorganize headers (ongoing work) to make them hermetic, and disentangle\n    various module dependencies.  (@davidtgoldblatt)\n  - Convert various internal data structures such as size class metadata from\n    boot-time-initialized to compile-time-initialized.  Propagate resulting data\n    structure simplifications, such as making arena metadata fixed-size.\n    (@jasone)\n  - Simplify size class lookups when constrained to size classes that are\n    multiples of the page size.  This speeds lookups, but the primary benefit is\n    complexity reduction in code that was the source of numerous regressions.\n    (@jasone)\n  - Lock individual extents when possible for localized extent operations,\n    rather than relying on a top-level arena lock.  (@davidtgoldblatt, @jasone)\n  - Use first fit layout policy instead of best fit, in order to improve\n    packing.  (@jasone)\n  - If munmap(2) is not in use, use an exponential series to grow each arena's\n    virtual memory, so that the number of disjoint virtual memory mappings\n    remains low.  (@jasone)\n  - Implement per arena base allocators, so that arenas never share any virtual\n    memory pages.  (@jasone)\n  - Automatically generate private symbol name mangling macros.  (@jasone)\n\n  Incompatible changes:\n  - Replace chunk hooks with an expanded/normalized set of extent hooks.\n    (@jasone)\n  - Remove ratio-based purging.  (@jasone)\n  - Remove --disable-tcache.  (@jasone)\n  - Remove --disable-tls.  (@jasone)\n  - Remove --enable-ivsalloc.  (@jasone)\n  - Remove --with-lg-size-class-group.  (@jasone)\n  - Remove --with-lg-tiny-min.  (@jasone)\n  - Remove --disable-cc-silence.  (@jasone)\n  - Remove --enable-code-coverage.  (@jasone)\n  - Remove --disable-munmap (replaced by opt.retain).  (@jasone)\n  - Remove Valgrind support.  (@jasone)\n  - Remove quarantine support.  (@jasone)\n  - Remove redzone support.  (@jasone)\n  - Remove mallctl interfaces (various authors):\n    + config.munmap\n    + config.tcache\n    + config.tls\n    + config.valgrind\n    + opt.lg_chunk\n    + opt.purge\n    + opt.lg_dirty_mult\n    + opt.decay_time\n    + opt.quarantine\n    + opt.redzone\n    + opt.thp\n    + arena.<i>.lg_dirty_mult\n    + arena.<i>.decay_time\n    + arena.<i>.chunk_hooks\n    + arenas.initialized\n    + arenas.lg_dirty_mult\n    + arenas.decay_time\n    + arenas.bin.<i>.run_size\n    + arenas.nlruns\n    + arenas.lrun.<i>.size\n    + arenas.nhchunks\n    + arenas.hchunk.<i>.size\n    + arenas.extend\n    + stats.cactive\n    + stats.arenas.<i>.lg_dirty_mult\n    + stats.arenas.<i>.decay_time\n    + stats.arenas.<i>.metadata.{mapped,allocated}\n    + stats.arenas.<i>.{npurge,nmadvise,purged}\n    + stats.arenas.<i>.huge.{allocated,nmalloc,ndalloc,nrequests}\n    + stats.arenas.<i>.bins.<j>.{nruns,reruns,curruns}\n    + stats.arenas.<i>.lruns.<j>.{nmalloc,ndalloc,nrequests,curruns}\n    + stats.arenas.<i>.hchunks.<j>.{nmalloc,ndalloc,nrequests,curhchunks}\n\n  Bug fixes:\n  - Improve interval-based profile dump triggering to dump only one profile when\n    a single allocation's size exceeds the interval.  (@jasone)\n  - Use prefixed function names (as controlled by --with-jemalloc-prefix) when\n    pruning backtrace frames in jeprof.  (@jasone)\n\n* 4.5.0 (February 28, 2017)\n\n  This is the first release to benefit from much broader continuous integration\n  testing, thanks to @davidtgoldblatt.  Had we had this testing infrastructure\n  in place for prior releases, it would have caught all of the most serious\n  regressions fixed by this release.\n\n  New features:\n  - Add --disable-thp and the opt.thp mallctl to provide opt-out mechanisms for\n    transparent huge page integration.  (@jasone)\n  - Update zone allocator integration to work with macOS 10.12.  (@glandium)\n  - Restructure *CFLAGS configuration, so that CFLAGS behaves typically, and\n    EXTRA_CFLAGS provides a way to specify e.g. -Werror during building, but not\n    during configuration.  (@jasone, @ronawho)\n\n  Bug fixes:\n  - Fix DSS (sbrk(2)-based) allocation.  This regression was first released in\n    4.3.0.  (@jasone)\n  - Handle race in per size class utilization computation.  This functionality\n    was first released in 4.0.0.  (@interwq)\n  - Fix lock order reversal during gdump.  (@jasone)\n  - Fix/refactor tcache synchronization.  This regression was first released in\n    4.0.0.  (@jasone)\n  - Fix various JSON-formatted malloc_stats_print() bugs.  This functionality\n    was first released in 4.3.0.  (@jasone)\n  - Fix huge-aligned allocation.  This regression was first released in 4.4.0.\n    (@jasone)\n  - When transparent huge page integration is enabled, detect what state pages\n    start in according to the kernel's current operating mode, and only convert\n    arena chunks to non-huge during purging if that is not their initial state.\n    This functionality was first released in 4.4.0.  (@jasone)\n  - Fix lg_chunk clamping for the --enable-cache-oblivious --disable-fill case.\n    This regression was first released in 4.0.0.  (@jasone, @428desmo)\n  - Properly detect sparc64 when building for Linux.  (@glaubitz)\n\n* 4.4.0 (December 3, 2016)\n\n  New features:\n  - Add configure support for *-*-linux-android.  (@cferris1000, @jasone)\n  - Add the --disable-syscall configure option, for use on systems that place\n    security-motivated limitations on syscall(2).  (@jasone)\n  - Add support for Debian GNU/kFreeBSD.  (@thesam)\n\n  Optimizations:\n  - Add extent serial numbers and use them where appropriate as a sort key that\n    is higher priority than address, so that the allocation policy prefers older\n    extents.  This tends to improve locality (decrease fragmentation) when\n    memory grows downward.  (@jasone)\n  - Refactor madvise(2) configuration so that MADV_FREE is detected and utilized\n    on Linux 4.5 and newer.  (@jasone)\n  - Mark partially purged arena chunks as non-huge-page.  This improves\n    interaction with Linux's transparent huge page functionality.  (@jasone)\n\n  Bug fixes:\n  - Fix size class computations for edge conditions involving extremely large\n    allocations.  This regression was first released in 4.0.0.  (@jasone,\n    @ingvarha)\n  - Remove overly restrictive assertions related to the cactive statistic.  This\n    regression was first released in 4.1.0.  (@jasone)\n  - Implement a more reliable detection scheme for os_unfair_lock on macOS.\n    (@jszakmeister)\n\n* 4.3.1 (November 7, 2016)\n\n  Bug fixes:\n  - Fix a severe virtual memory leak.  This regression was first released in\n    4.3.0.  (@interwq, @jasone)\n  - Refactor atomic and prng APIs to restore support for 32-bit platforms that\n    use pre-C11 toolchains, e.g. FreeBSD's mips.  (@jasone)\n\n* 4.3.0 (November 4, 2016)\n\n  This is the first release that passes the test suite for multiple Windows\n  configurations, thanks in large part to @glandium setting up continuous\n  integration via AppVeyor (and Travis CI for Linux and OS X).\n\n  New features:\n  - Add \"J\" (JSON) support to malloc_stats_print().  (@jasone)\n  - Add Cray compiler support.  (@ronawho)\n\n  Optimizations:\n  - Add/use adaptive spinning for bootstrapping and radix tree node\n    initialization.  (@jasone)\n\n  Bug fixes:\n  - Fix large allocation to search starting in the optimal size class heap,\n    which can substantially reduce virtual memory churn and fragmentation.  This\n    regression was first released in 4.0.0.  (@mjp41, @jasone)\n  - Fix stats.arenas.<i>.nthreads accounting.  (@interwq)\n  - Fix and simplify decay-based purging.  (@jasone)\n  - Make DSS (sbrk(2)-related) operations lockless, which resolves potential\n    deadlocks during thread exit.  (@jasone)\n  - Fix over-sized allocation of radix tree leaf nodes.  (@mjp41, @ogaun,\n    @jasone)\n  - Fix over-sized allocation of arena_t (plus associated stats) data\n    structures.  (@jasone, @interwq)\n  - Fix EXTRA_CFLAGS to not affect configuration.  (@jasone)\n  - Fix a Valgrind integration bug.  (@ronawho)\n  - Disallow 0x5a junk filling when running in Valgrind.  (@jasone)\n  - Fix a file descriptor leak on Linux.  This regression was first released in\n    4.2.0.  (@vsarunas, @jasone)\n  - Fix static linking of jemalloc with glibc.  (@djwatson)\n  - Use syscall(2) rather than {open,read,close}(2) during boot on Linux.  This\n    works around other libraries' system call wrappers performing reentrant\n    allocation.  (@kspinka, @Whissi, @jasone)\n  - Fix OS X default zone replacement to work with OS X 10.12.  (@glandium,\n    @jasone)\n  - Fix cached memory management to avoid needless commit/decommit operations\n    during purging, which resolves permanent virtual memory map fragmentation\n    issues on Windows.  (@mjp41, @jasone)\n  - Fix TSD fetches to avoid (recursive) allocation.  This is relevant to\n    non-TLS and Windows configurations.  (@jasone)\n  - Fix malloc_conf overriding to work on Windows.  (@jasone)\n  - Forcibly disable lazy-lock on Windows (was forcibly *enabled*).  (@jasone)\n\n* 4.2.1 (June 8, 2016)\n\n  Bug fixes:\n  - Fix bootstrapping issues for configurations that require allocation during\n    tsd initialization (e.g. --disable-tls).  (@cferris1000, @jasone)\n  - Fix gettimeofday() version of nstime_update().  (@ronawho)\n  - Fix Valgrind regressions in calloc() and chunk_alloc_wrapper().  (@ronawho)\n  - Fix potential VM map fragmentation regression.  (@jasone)\n  - Fix opt_zero-triggered in-place huge reallocation zeroing.  (@jasone)\n  - Fix heap profiling context leaks in reallocation edge cases.  (@jasone)\n\n* 4.2.0 (May 12, 2016)\n\n  New features:\n  - Add the arena.<i>.reset mallctl, which makes it possible to discard all of\n    an arena's allocations in a single operation.  (@jasone)\n  - Add the stats.retained and stats.arenas.<i>.retained statistics.  (@jasone)\n  - Add the --with-version configure option.  (@jasone)\n  - Support --with-lg-page values larger than actual page size.  (@jasone)\n\n  Optimizations:\n  - Use pairing heaps rather than red-black trees for various hot data\n    structures.  (@djwatson, @jasone)\n  - Streamline fast paths of rtree operations.  (@jasone)\n  - Optimize the fast paths of calloc() and [m,d,sd]allocx().  (@jasone)\n  - Decommit unused virtual memory if the OS does not overcommit.  (@jasone)\n  - Specify MAP_NORESERVE on Linux if [heuristic] overcommit is active, in order\n    to avoid unfortunate interactions during fork(2).  (@jasone)\n\n  Bug fixes:\n  - Fix chunk accounting related to triggering gdump profiles.  (@jasone)\n  - Link against librt for clock_gettime(2) if glibc < 2.17.  (@jasone)\n  - Scale leak report summary according to sampling probability.  (@jasone)\n\n* 4.1.1 (May 3, 2016)\n\n  This bugfix release resolves a variety of mostly minor issues, though the\n  bitmap fix is critical for 64-bit Windows.\n\n  Bug fixes:\n  - Fix the linear scan version of bitmap_sfu() to shift by the proper amount\n    even when sizeof(long) is not the same as sizeof(void *), as on 64-bit\n    Windows.  (@jasone)\n  - Fix hashing functions to avoid unaligned memory accesses (and resulting\n    crashes).  This is relevant at least to some ARM-based platforms.\n    (@rkmisra)\n  - Fix fork()-related lock rank ordering reversals.  These reversals were\n    unlikely to cause deadlocks in practice except when heap profiling was\n    enabled and active.  (@jasone)\n  - Fix various chunk leaks in OOM code paths.  (@jasone)\n  - Fix malloc_stats_print() to print opt.narenas correctly.  (@jasone)\n  - Fix MSVC-specific build/test issues.  (@rustyx, @yuslepukhin)\n  - Fix a variety of test failures that were due to test fragility rather than\n    core bugs.  (@jasone)\n\n* 4.1.0 (February 28, 2016)\n\n  This release is primarily about optimizations, but it also incorporates a lot\n  of portability-motivated refactoring and enhancements.  Many people worked on\n  this release, to an extent that even with the omission here of minor changes\n  (see git revision history), and of the people who reported and diagnosed\n  issues, so much of the work was contributed that starting with this release,\n  changes are annotated with author credits to help reflect the collaborative\n  effort involved.\n\n  New features:\n  - Implement decay-based unused dirty page purging, a major optimization with\n    mallctl API impact.  This is an alternative to the existing ratio-based\n    unused dirty page purging, and is intended to eventually become the sole\n    purging mechanism.  New mallctls:\n    + opt.purge\n    + opt.decay_time\n    + arena.<i>.decay\n    + arena.<i>.decay_time\n    + arenas.decay_time\n    + stats.arenas.<i>.decay_time\n    (@jasone, @cevans87)\n  - Add --with-malloc-conf, which makes it possible to embed a default\n    options string during configuration.  This was motivated by the desire to\n    specify --with-malloc-conf=purge:decay , since the default must remain\n    purge:ratio until the 5.0.0 release.  (@jasone)\n  - Add MS Visual Studio 2015 support.  (@rustyx, @yuslepukhin)\n  - Make *allocx() size class overflow behavior defined.  The maximum\n    size class is now less than PTRDIFF_MAX to protect applications against\n    numerical overflow, and all allocation functions are guaranteed to indicate\n    errors rather than potentially crashing if the request size exceeds the\n    maximum size class.  (@jasone)\n  - jeprof:\n    + Add raw heap profile support.  (@jasone)\n    + Add --retain and --exclude for backtrace symbol filtering.  (@jasone)\n\n  Optimizations:\n  - Optimize the fast path to combine various bootstrapping and configuration\n    checks and execute more streamlined code in the common case.  (@interwq)\n  - Use linear scan for small bitmaps (used for small object tracking).  In\n    addition to speeding up bitmap operations on 64-bit systems, this reduces\n    allocator metadata overhead by approximately 0.2%.  (@djwatson)\n  - Separate arena_avail trees, which substantially speeds up run tree\n    operations.  (@djwatson)\n  - Use memoization (boot-time-computed table) for run quantization.  Separate\n    arena_avail trees reduced the importance of this optimization.  (@jasone)\n  - Attempt mmap-based in-place huge reallocation.  This can dramatically speed\n    up incremental huge reallocation.  (@jasone)\n\n  Incompatible changes:\n  - Make opt.narenas unsigned rather than size_t.  (@jasone)\n\n  Bug fixes:\n  - Fix stats.cactive accounting regression.  (@rustyx, @jasone)\n  - Handle unaligned keys in hash().  This caused problems for some ARM systems.\n    (@jasone, @cferris1000)\n  - Refactor arenas array.  In addition to fixing a fork-related deadlock, this\n    makes arena lookups faster and simpler.  (@jasone)\n  - Move retained memory allocation out of the default chunk allocation\n    function, to a location that gets executed even if the application installs\n    a custom chunk allocation function.  This resolves a virtual memory leak.\n    (@buchgr)\n  - Fix a potential tsd cleanup leak.  (@cferris1000, @jasone)\n  - Fix run quantization.  In practice this bug had no impact unless\n    applications requested memory with alignment exceeding one page.\n    (@jasone, @djwatson)\n  - Fix LinuxThreads-specific bootstrapping deadlock.  (Cosmin Paraschiv)\n  - jeprof:\n    + Don't discard curl options if timeout is not defined.  (@djwatson)\n    + Detect failed profile fetches.  (@djwatson)\n  - Fix stats.arenas.<i>.{dss,lg_dirty_mult,decay_time,pactive,pdirty} for\n    --disable-stats case.  (@jasone)\n\n* 4.0.4 (October 24, 2015)\n\n  This bugfix release fixes another xallocx() regression.  No other regressions\n  have come to light in over a month, so this is likely a good starting point\n  for people who prefer to wait for \"dot one\" releases with all the major issues\n  shaken out.\n\n  Bug fixes:\n  - Fix xallocx(..., MALLOCX_ZERO to zero the last full trailing page of large\n    allocations that have been randomly assigned an offset of 0 when\n    --enable-cache-oblivious configure option is enabled.\n\n* 4.0.3 (September 24, 2015)\n\n  This bugfix release continues the trend of xallocx() and heap profiling fixes.\n\n  Bug fixes:\n  - Fix xallocx(..., MALLOCX_ZERO) to zero all trailing bytes of large\n    allocations when --enable-cache-oblivious configure option is enabled.\n  - Fix xallocx(..., MALLOCX_ZERO) to zero trailing bytes of huge allocations\n    when resizing from/to a size class that is not a multiple of the chunk size.\n  - Fix prof_tctx_dump_iter() to filter out nodes that were created after heap\n    profile dumping started.\n  - Work around a potentially bad thread-specific data initialization\n    interaction with NPTL (glibc's pthreads implementation).\n\n* 4.0.2 (September 21, 2015)\n\n  This bugfix release addresses a few bugs specific to heap profiling.\n\n  Bug fixes:\n  - Fix ixallocx_prof_sample() to never modify nor create sampled small\n    allocations.  xallocx() is in general incapable of moving small allocations,\n    so this fix removes buggy code without loss of generality.\n  - Fix irallocx_prof_sample() to always allocate large regions, even when\n    alignment is non-zero.\n  - Fix prof_alloc_rollback() to read tdata from thread-specific data rather\n    than dereferencing a potentially invalid tctx.\n\n* 4.0.1 (September 15, 2015)\n\n  This is a bugfix release that is somewhat high risk due to the amount of\n  refactoring required to address deep xallocx() problems.  As a side effect of\n  these fixes, xallocx() now tries harder to partially fulfill requests for\n  optional extra space.  Note that a couple of minor heap profiling\n  optimizations are included, but these are better thought of as performance\n  fixes that were integral to discovering most of the other bugs.\n\n  Optimizations:\n  - Avoid a chunk metadata read in arena_prof_tctx_set(), since it is in the\n    fast path when heap profiling is enabled.  Additionally, split a special\n    case out into arena_prof_tctx_reset(), which also avoids chunk metadata\n    reads.\n  - Optimize irallocx_prof() to optimistically update the sampler state.  The\n    prior implementation appears to have been a holdover from when\n    rallocx()/xallocx() functionality was combined as rallocm().\n\n  Bug fixes:\n  - Fix TLS configuration such that it is enabled by default for platforms on\n    which it works correctly.\n  - Fix arenas_cache_cleanup() and arena_get_hard() to handle\n    allocation/deallocation within the application's thread-specific data\n    cleanup functions even after arenas_cache is torn down.\n  - Fix xallocx() bugs related to size+extra exceeding HUGE_MAXCLASS.\n  - Fix chunk purge hook calls for in-place huge shrinking reallocation to\n    specify the old chunk size rather than the new chunk size.  This bug caused\n    no correctness issues for the default chunk purge function, but was\n    visible to custom functions set via the \"arena.<i>.chunk_hooks\" mallctl.\n  - Fix heap profiling bugs:\n    + Fix heap profiling to distinguish among otherwise identical sample sites\n      with interposed resets (triggered via the \"prof.reset\" mallctl).  This bug\n      could cause data structure corruption that would most likely result in a\n      segfault.\n    + Fix irealloc_prof() to prof_alloc_rollback() on OOM.\n    + Make one call to prof_active_get_unlocked() per allocation event, and use\n      the result throughout the relevant functions that handle an allocation\n      event.  Also add a missing check in prof_realloc().  These fixes protect\n      allocation events against concurrent prof_active changes.\n    + Fix ixallocx_prof() to pass usize_max and zero to ixallocx_prof_sample()\n      in the correct order.\n    + Fix prof_realloc() to call prof_free_sampled_object() after calling\n      prof_malloc_sample_object().  Prior to this fix, if tctx and old_tctx were\n      the same, the tctx could have been prematurely destroyed.\n  - Fix portability bugs:\n    + Don't bitshift by negative amounts when encoding/decoding run sizes in\n      chunk header maps.  This affected systems with page sizes greater than 8\n      KiB.\n    + Rename index_t to szind_t to avoid an existing type on Solaris.\n    + Add JEMALLOC_CXX_THROW to the memalign() function prototype, in order to\n      match glibc and avoid compilation errors when including both\n      jemalloc/jemalloc.h and malloc.h in C++ code.\n    + Don't assume that /bin/sh is appropriate when running size_classes.sh\n      during configuration.\n    + Consider __sparcv9 a synonym for __sparc64__ when defining LG_QUANTUM.\n    + Link tests to librt if it contains clock_gettime(2).\n\n* 4.0.0 (August 17, 2015)\n\n  This version contains many speed and space optimizations, both minor and\n  major.  The major themes are generalization, unification, and simplification.\n  Although many of these optimizations cause no visible behavior change, their\n  cumulative effect is substantial.\n\n  New features:\n  - Normalize size class spacing to be consistent across the complete size\n    range.  By default there are four size classes per size doubling, but this\n    is now configurable via the --with-lg-size-class-group option.  Also add the\n    --with-lg-page, --with-lg-page-sizes, --with-lg-quantum, and\n    --with-lg-tiny-min options, which can be used to tweak page and size class\n    settings.  Impacts:\n    + Worst case performance for incrementally growing/shrinking reallocation\n      is improved because there are far fewer size classes, and therefore\n      copying happens less often.\n    + Internal fragmentation is limited to 20% for all but the smallest size\n      classes (those less than four times the quantum).  (1B + 4 KiB)\n      and (1B + 4 MiB) previously suffered nearly 50% internal fragmentation.\n    + Chunk fragmentation tends to be lower because there are fewer distinct run\n      sizes to pack.\n  - Add support for explicit tcaches.  The \"tcache.create\", \"tcache.flush\", and\n    \"tcache.destroy\" mallctls control tcache lifetime and flushing, and the\n    MALLOCX_TCACHE(tc) and MALLOCX_TCACHE_NONE flags to the *allocx() API\n    control which tcache is used for each operation.\n  - Implement per thread heap profiling, as well as the ability to\n    enable/disable heap profiling on a per thread basis.  Add the \"prof.reset\",\n    \"prof.lg_sample\", \"thread.prof.name\", \"thread.prof.active\",\n    \"opt.prof_thread_active_init\", \"prof.thread_active_init\", and\n    \"thread.prof.active\" mallctls.\n  - Add support for per arena application-specified chunk allocators, configured\n    via the \"arena.<i>.chunk_hooks\" mallctl.\n  - Refactor huge allocation to be managed by arenas, so that arenas now\n    function as general purpose independent allocators.  This is important in\n    the context of user-specified chunk allocators, aside from the scalability\n    benefits.  Related new statistics:\n    + The \"stats.arenas.<i>.huge.allocated\", \"stats.arenas.<i>.huge.nmalloc\",\n      \"stats.arenas.<i>.huge.ndalloc\", and \"stats.arenas.<i>.huge.nrequests\"\n      mallctls provide high level per arena huge allocation statistics.\n    + The \"arenas.nhchunks\", \"arenas.hchunk.<i>.size\",\n      \"stats.arenas.<i>.hchunks.<j>.nmalloc\",\n      \"stats.arenas.<i>.hchunks.<j>.ndalloc\",\n      \"stats.arenas.<i>.hchunks.<j>.nrequests\", and\n      \"stats.arenas.<i>.hchunks.<j>.curhchunks\" mallctls provide per size class\n      statistics.\n  - Add the 'util' column to malloc_stats_print() output, which reports the\n    proportion of available regions that are currently in use for each small\n    size class.\n  - Add \"alloc\" and \"free\" modes for for junk filling (see the \"opt.junk\"\n    mallctl), so that it is possible to separately enable junk filling for\n    allocation versus deallocation.\n  - Add the jemalloc-config script, which provides information about how\n    jemalloc was configured, and how to integrate it into application builds.\n  - Add metadata statistics, which are accessible via the \"stats.metadata\",\n    \"stats.arenas.<i>.metadata.mapped\", and\n    \"stats.arenas.<i>.metadata.allocated\" mallctls.\n  - Add the \"stats.resident\" mallctl, which reports the upper limit of\n    physically resident memory mapped by the allocator.\n  - Add per arena control over unused dirty page purging, via the\n    \"arenas.lg_dirty_mult\", \"arena.<i>.lg_dirty_mult\", and\n    \"stats.arenas.<i>.lg_dirty_mult\" mallctls.\n  - Add the \"prof.gdump\" mallctl, which makes it possible to toggle the gdump\n    feature on/off during program execution.\n  - Add sdallocx(), which implements sized deallocation.  The primary\n    optimization over dallocx() is the removal of a metadata read, which often\n    suffers an L1 cache miss.\n  - Add missing header includes in jemalloc/jemalloc.h, so that applications\n    only have to #include <jemalloc/jemalloc.h>.\n  - Add support for additional platforms:\n    + Bitrig\n    + Cygwin\n    + DragonFlyBSD\n    + iOS\n    + OpenBSD\n    + OpenRISC/or1k\n\n  Optimizations:\n  - Maintain dirty runs in per arena LRUs rather than in per arena trees of\n    dirty-run-containing chunks.  In practice this change significantly reduces\n    dirty page purging volume.\n  - Integrate whole chunks into the unused dirty page purging machinery.  This\n    reduces the cost of repeated huge allocation/deallocation, because it\n    effectively introduces a cache of chunks.\n  - Split the arena chunk map into two separate arrays, in order to increase\n    cache locality for the frequently accessed bits.\n  - Move small run metadata out of runs, into arena chunk headers.  This reduces\n    run fragmentation, smaller runs reduce external fragmentation for small size\n    classes, and packed (less uniformly aligned) metadata layout improves CPU\n    cache set distribution.\n  - Randomly distribute large allocation base pointer alignment relative to page\n    boundaries in order to more uniformly utilize CPU cache sets.  This can be\n    disabled via the --disable-cache-oblivious configure option, and queried via\n    the \"config.cache_oblivious\" mallctl.\n  - Micro-optimize the fast paths for the public API functions.\n  - Refactor thread-specific data to reside in a single structure.  This assures\n    that only a single TLS read is necessary per call into the public API.\n  - Implement in-place huge allocation growing and shrinking.\n  - Refactor rtree (radix tree for chunk lookups) to be lock-free, and make\n    additional optimizations that reduce maximum lookup depth to one or two\n    levels.  This resolves what was a concurrency bottleneck for per arena huge\n    allocation, because a global data structure is critical for determining\n    which arenas own which huge allocations.\n\n  Incompatible changes:\n  - Replace --enable-cc-silence with --disable-cc-silence to suppress spurious\n    warnings by default.\n  - Assure that the constness of malloc_usable_size()'s return type matches that\n    of the system implementation.\n  - Change the heap profile dump format to support per thread heap profiling,\n    rename pprof to jeprof, and enhance it with the --thread=<n> option.  As a\n    result, the bundled jeprof must now be used rather than the upstream\n    (gperftools) pprof.\n  - Disable \"opt.prof_final\" by default, in order to avoid atexit(3), which can\n    internally deadlock on some platforms.\n  - Change the \"arenas.nlruns\" mallctl type from size_t to unsigned.\n  - Replace the \"stats.arenas.<i>.bins.<j>.allocated\" mallctl with\n    \"stats.arenas.<i>.bins.<j>.curregs\".\n  - Ignore MALLOC_CONF in set{uid,gid,cap} binaries.\n  - Ignore MALLOCX_ARENA(a) in dallocx(), in favor of using the\n    MALLOCX_TCACHE(tc) and MALLOCX_TCACHE_NONE flags to control tcache usage.\n\n  Removed features:\n  - Remove the *allocm() API, which is superseded by the *allocx() API.\n  - Remove the --enable-dss options, and make dss non-optional on all platforms\n    which support sbrk(2).\n  - Remove the \"arenas.purge\" mallctl, which was obsoleted by the\n    \"arena.<i>.purge\" mallctl in 3.1.0.\n  - Remove the unnecessary \"opt.valgrind\" mallctl; jemalloc automatically\n    detects whether it is running inside Valgrind.\n  - Remove the \"stats.huge.allocated\", \"stats.huge.nmalloc\", and\n    \"stats.huge.ndalloc\" mallctls.\n  - Remove the --enable-mremap option.\n  - Remove the \"stats.chunks.current\", \"stats.chunks.total\", and\n    \"stats.chunks.high\" mallctls.\n\n  Bug fixes:\n  - Fix the cactive statistic to decrease (rather than increase) when active\n    memory decreases.  This regression was first released in 3.5.0.\n  - Fix OOM handling in memalign() and valloc().  A variant of this bug existed\n    in all releases since 2.0.0, which introduced these functions.\n  - Fix an OOM-related regression in arena_tcache_fill_small(), which could\n    cause cache corruption on OOM.  This regression was present in all releases\n    from 2.2.0 through 3.6.0.\n  - Fix size class overflow handling for malloc(), posix_memalign(), memalign(),\n    calloc(), and realloc() when profiling is enabled.\n  - Fix the \"arena.<i>.dss\" mallctl to return an error if \"primary\" or\n    \"secondary\" precedence is specified, but sbrk(2) is not supported.\n  - Fix fallback lg_floor() implementations to handle extremely large inputs.\n  - Ensure the default purgeable zone is after the default zone on OS X.\n  - Fix latent bugs in atomic_*().\n  - Fix the \"arena.<i>.dss\" mallctl to handle read-only calls.\n  - Fix tls_model configuration to enable the initial-exec model when possible.\n  - Mark malloc_conf as a weak symbol so that the application can override it.\n  - Correctly detect glibc's adaptive pthread mutexes.\n  - Fix the --without-export configure option.\n\n* 3.6.0 (March 31, 2014)\n\n  This version contains a critical bug fix for a regression present in 3.5.0 and\n  3.5.1.\n\n  Bug fixes:\n  - Fix a regression in arena_chunk_alloc() that caused crashes during\n    small/large allocation if chunk allocation failed.  In the absence of this\n    bug, chunk allocation failure would result in allocation failure, e.g.  NULL\n    return from malloc().  This regression was introduced in 3.5.0.\n  - Fix backtracing for gcc intrinsics-based backtracing by specifying\n    -fno-omit-frame-pointer to gcc.  Note that the application (and all the\n    libraries it links to) must also be compiled with this option for\n    backtracing to be reliable.\n  - Use dss allocation precedence for huge allocations as well as small/large\n    allocations.\n  - Fix test assertion failure message formatting.  This bug did not manifest on\n    x86_64 systems because of implementation subtleties in va_list.\n  - Fix inconsequential test failures for hash and SFMT code.\n\n  New features:\n  - Support heap profiling on FreeBSD.  This feature depends on the proc\n    filesystem being mounted during heap profile dumping.\n\n* 3.5.1 (February 25, 2014)\n\n  This version primarily addresses minor bugs in test code.\n\n  Bug fixes:\n  - Configure Solaris/Illumos to use MADV_FREE.\n  - Fix junk filling for mremap(2)-based huge reallocation.  This is only\n    relevant if configuring with the --enable-mremap option specified.\n  - Avoid compilation failure if 'restrict' C99 keyword is not supported by the\n    compiler.\n  - Add a configure test for SSE2 rather than assuming it is usable on i686\n    systems.  This fixes test compilation errors, especially on 32-bit Linux\n    systems.\n  - Fix mallctl argument size mismatches (size_t vs. uint64_t) in the stats unit\n    test.\n  - Fix/remove flawed alignment-related overflow tests.\n  - Prevent compiler optimizations that could change backtraces in the\n    prof_accum unit test.\n\n* 3.5.0 (January 22, 2014)\n\n  This version focuses on refactoring and automated testing, though it also\n  includes some non-trivial heap profiling optimizations not mentioned below.\n\n  New features:\n  - Add the *allocx() API, which is a successor to the experimental *allocm()\n    API.  The *allocx() functions are slightly simpler to use because they have\n    fewer parameters, they directly return the results of primary interest, and\n    mallocx()/rallocx() avoid the strict aliasing pitfall that\n    allocm()/rallocm() share with posix_memalign().  Note that *allocm() is\n    slated for removal in the next non-bugfix release.\n  - Add support for LinuxThreads.\n\n  Bug fixes:\n  - Unless heap profiling is enabled, disable floating point code and don't link\n    with libm.  This, in combination with e.g. EXTRA_CFLAGS=-mno-sse on x64\n    systems, makes it possible to completely disable floating point register\n    use.  Some versions of glibc neglect to save/restore caller-saved floating\n    point registers during dynamic lazy symbol loading, and the symbol loading\n    code uses whatever malloc the application happens to have linked/loaded\n    with, the result being potential floating point register corruption.\n  - Report ENOMEM rather than EINVAL if an OOM occurs during heap profiling\n    backtrace creation in imemalign().  This bug impacted posix_memalign() and\n    aligned_alloc().\n  - Fix a file descriptor leak in a prof_dump_maps() error path.\n  - Fix prof_dump() to close the dump file descriptor for all relevant error\n    paths.\n  - Fix rallocm() to use the arena specified by the ALLOCM_ARENA(s) flag for\n    allocation, not just deallocation.\n  - Fix a data race for large allocation stats counters.\n  - Fix a potential infinite loop during thread exit.  This bug occurred on\n    Solaris, and could affect other platforms with similar pthreads TSD\n    implementations.\n  - Don't junk-fill reallocations unless usable size changes.  This fixes a\n    violation of the *allocx()/*allocm() semantics.\n  - Fix growing large reallocation to junk fill new space.\n  - Fix huge deallocation to junk fill when munmap is disabled.\n  - Change the default private namespace prefix from empty to je_, and change\n    --with-private-namespace-prefix so that it prepends an additional prefix\n    rather than replacing je_.  This reduces the likelihood of applications\n    which statically link jemalloc experiencing symbol name collisions.\n  - Add missing private namespace mangling (relevant when\n    --with-private-namespace is specified).\n  - Add and use JEMALLOC_INLINE_C so that static inline functions are marked as\n    static even for debug builds.\n  - Add a missing mutex unlock in a malloc_init_hard() error path.  In practice\n    this error path is never executed.\n  - Fix numerous bugs in malloc_strotumax() error handling/reporting.  These\n    bugs had no impact except for malformed inputs.\n  - Fix numerous bugs in malloc_snprintf().  These bugs were not exercised by\n    existing calls, so they had no impact.\n\n* 3.4.1 (October 20, 2013)\n\n  Bug fixes:\n  - Fix a race in the \"arenas.extend\" mallctl that could cause memory corruption\n    of internal data structures and subsequent crashes.\n  - Fix Valgrind integration flaws that caused Valgrind warnings about reads of\n    uninitialized memory in:\n    + arena chunk headers\n    + internal zero-initialized data structures (relevant to tcache and prof\n      code)\n  - Preserve errno during the first allocation.  A readlink(2) call during\n    initialization fails unless /etc/malloc.conf exists, so errno was typically\n    set during the first allocation prior to this fix.\n  - Fix compilation warnings reported by gcc 4.8.1.\n\n* 3.4.0 (June 2, 2013)\n\n  This version is essentially a small bugfix release, but the addition of\n  aarch64 support requires that the minor version be incremented.\n\n  Bug fixes:\n  - Fix race-triggered deadlocks in chunk_record().  These deadlocks were\n    typically triggered by multiple threads concurrently deallocating huge\n    objects.\n\n  New features:\n  - Add support for the aarch64 architecture.\n\n* 3.3.1 (March 6, 2013)\n\n  This version fixes bugs that are typically encountered only when utilizing\n  custom run-time options.\n\n  Bug fixes:\n  - Fix a locking order bug that could cause deadlock during fork if heap\n    profiling were enabled.\n  - Fix a chunk recycling bug that could cause the allocator to lose track of\n    whether a chunk was zeroed.  On FreeBSD, NetBSD, and OS X, it could cause\n    corruption if allocating via sbrk(2) (unlikely unless running with the\n    \"dss:primary\" option specified).  This was completely harmless on Linux\n    unless using mlockall(2) (and unlikely even then, unless the\n    --disable-munmap configure option or the \"dss:primary\" option was\n    specified).  This regression was introduced in 3.1.0 by the\n    mlockall(2)/madvise(2) interaction fix.\n  - Fix TLS-related memory corruption that could occur during thread exit if the\n    thread never allocated memory.  Only the quarantine and prof facilities were\n    susceptible.\n  - Fix two quarantine bugs:\n    + Internal reallocation of the quarantined object array leaked the old\n      array.\n    + Reallocation failure for internal reallocation of the quarantined object\n      array (very unlikely) resulted in memory corruption.\n  - Fix Valgrind integration to annotate all internally allocated memory in a\n    way that keeps Valgrind happy about internal data structure access.\n  - Fix building for s390 systems.\n\n* 3.3.0 (January 23, 2013)\n\n  This version includes a few minor performance improvements in addition to the\n  listed new features and bug fixes.\n\n  New features:\n  - Add clipping support to lg_chunk option processing.\n  - Add the --enable-ivsalloc option.\n  - Add the --without-export option.\n  - Add the --disable-zone-allocator option.\n\n  Bug fixes:\n  - Fix \"arenas.extend\" mallctl to output the number of arenas.\n  - Fix chunk_recycle() to unconditionally inform Valgrind that returned memory\n    is undefined.\n  - Fix build break on FreeBSD related to alloca.h.\n\n* 3.2.0 (November 9, 2012)\n\n  In addition to a couple of bug fixes, this version modifies page run\n  allocation and dirty page purging algorithms in order to better control\n  page-level virtual memory fragmentation.\n\n  Incompatible changes:\n  - Change the \"opt.lg_dirty_mult\" default from 5 to 3 (32:1 to 8:1).\n\n  Bug fixes:\n  - Fix dss/mmap allocation precedence code to use recyclable mmap memory only\n    after primary dss allocation fails.\n  - Fix deadlock in the \"arenas.purge\" mallctl.  This regression was introduced\n    in 3.1.0 by the addition of the \"arena.<i>.purge\" mallctl.\n\n* 3.1.0 (October 16, 2012)\n\n  New features:\n  - Auto-detect whether running inside Valgrind, thus removing the need to\n    manually specify MALLOC_CONF=valgrind:true.\n  - Add the \"arenas.extend\" mallctl, which allows applications to create\n    manually managed arenas.\n  - Add the ALLOCM_ARENA() flag for {,r,d}allocm().\n  - Add the \"opt.dss\", \"arena.<i>.dss\", and \"stats.arenas.<i>.dss\" mallctls,\n    which provide control over dss/mmap precedence.\n  - Add the \"arena.<i>.purge\" mallctl, which obsoletes \"arenas.purge\".\n  - Define LG_QUANTUM for hppa.\n\n  Incompatible changes:\n  - Disable tcache by default if running inside Valgrind, in order to avoid\n    making unallocated objects appear reachable to Valgrind.\n  - Drop const from malloc_usable_size() argument on Linux.\n\n  Bug fixes:\n  - Fix heap profiling crash if sampled object is freed via realloc(p, 0).\n  - Remove const from __*_hook variable declarations, so that glibc can modify\n    them during process forking.\n  - Fix mlockall(2)/madvise(2) interaction.\n  - Fix fork(2)-related deadlocks.\n  - Fix error return value for \"thread.tcache.enabled\" mallctl.\n\n* 3.0.0 (May 11, 2012)\n\n  Although this version adds some major new features, the primary focus is on\n  internal code cleanup that facilitates maintainability and portability, most\n  of which is not reflected in the ChangeLog.  This is the first release to\n  incorporate substantial contributions from numerous other developers, and the\n  result is a more broadly useful allocator (see the git revision history for\n  contribution details).  Note that the license has been unified, thanks to\n  Facebook granting a license under the same terms as the other copyright\n  holders (see COPYING).\n\n  New features:\n  - Implement Valgrind support, redzones, and quarantine.\n  - Add support for additional platforms:\n    + FreeBSD\n    + Mac OS X Lion\n    + MinGW\n    + Windows (no support yet for replacing the system malloc)\n  - Add support for additional architectures:\n    + MIPS\n    + SH4\n    + Tilera\n  - Add support for cross compiling.\n  - Add nallocm(), which rounds a request size up to the nearest size class\n    without actually allocating.\n  - Implement aligned_alloc() (blame C11).\n  - Add the \"thread.tcache.enabled\" mallctl.\n  - Add the \"opt.prof_final\" mallctl.\n  - Update pprof (from gperftools 2.0).\n  - Add the --with-mangling option.\n  - Add the --disable-experimental option.\n  - Add the --disable-munmap option, and make it the default on Linux.\n  - Add the --enable-mremap option, which disables use of mremap(2) by default.\n\n  Incompatible changes:\n  - Enable stats by default.\n  - Enable fill by default.\n  - Disable lazy locking by default.\n  - Rename the \"tcache.flush\" mallctl to \"thread.tcache.flush\".\n  - Rename the \"arenas.pagesize\" mallctl to \"arenas.page\".\n  - Change the \"opt.lg_prof_sample\" default from 0 to 19 (1 B to 512 KiB).\n  - Change the \"opt.prof_accum\" default from true to false.\n\n  Removed features:\n  - Remove the swap feature, including the \"config.swap\", \"swap.avail\",\n    \"swap.prezeroed\", \"swap.nfds\", and \"swap.fds\" mallctls.\n  - Remove highruns statistics, including the\n    \"stats.arenas.<i>.bins.<j>.highruns\" and\n    \"stats.arenas.<i>.lruns.<j>.highruns\" mallctls.\n  - As part of small size class refactoring, remove the \"opt.lg_[qc]space_max\",\n    \"arenas.cacheline\", \"arenas.subpage\", \"arenas.[tqcs]space_{min,max}\", and\n    \"arenas.[tqcs]bins\" mallctls.\n  - Remove the \"arenas.chunksize\" mallctl.\n  - Remove the \"opt.lg_prof_tcmax\" option.\n  - Remove the \"opt.lg_prof_bt_max\" option.\n  - Remove the \"opt.lg_tcache_gc_sweep\" option.\n  - Remove the --disable-tiny option, including the \"config.tiny\" mallctl.\n  - Remove the --enable-dynamic-page-shift configure option.\n  - Remove the --enable-sysv configure option.\n\n  Bug fixes:\n  - Fix a statistics-related bug in the \"thread.arena\" mallctl that could cause\n    invalid statistics and crashes.\n  - Work around TLS deallocation via free() on Linux.  This bug could cause\n    write-after-free memory corruption.\n  - Fix a potential deadlock that could occur during interval- and\n    growth-triggered heap profile dumps.\n  - Fix large calloc() zeroing bugs due to dropping chunk map unzeroed flags.\n  - Fix chunk_alloc_dss() to stop claiming memory is zeroed.  This bug could\n    cause memory corruption and crashes with --enable-dss specified.\n  - Fix fork-related bugs that could cause deadlock in children between fork\n    and exec.\n  - Fix malloc_stats_print() to honor 'b' and 'l' in the opts parameter.\n  - Fix realloc(p, 0) to act like free(p).\n  - Do not enforce minimum alignment in memalign().\n  - Check for NULL pointer in malloc_usable_size().\n  - Fix an off-by-one heap profile statistics bug that could be observed in\n    interval- and growth-triggered heap profiles.\n  - Fix the \"epoch\" mallctl to update cached stats even if the passed in epoch\n    is 0.\n  - Fix bin->runcur management to fix a layout policy bug.  This bug did not\n    affect correctness.\n  - Fix a bug in choose_arena_hard() that potentially caused more arenas to be\n    initialized than necessary.\n  - Add missing \"opt.lg_tcache_max\" mallctl implementation.\n  - Use glibc allocator hooks to make mixed allocator usage less likely.\n  - Fix build issues for --disable-tcache.\n  - Don't mangle pthread_create() when --with-private-namespace is specified.\n\n* 2.2.5 (November 14, 2011)\n\n  Bug fixes:\n  - Fix huge_ralloc() race when using mremap(2).  This is a serious bug that\n    could cause memory corruption and/or crashes.\n  - Fix huge_ralloc() to maintain chunk statistics.\n  - Fix malloc_stats_print(..., \"a\") output.\n\n* 2.2.4 (November 5, 2011)\n\n  Bug fixes:\n  - Initialize arenas_tsd before using it.  This bug existed for 2.2.[0-3], as\n    well as for --disable-tls builds in earlier releases.\n  - Do not assume a 4 KiB page size in test/rallocm.c.\n\n* 2.2.3 (August 31, 2011)\n\n  This version fixes numerous bugs related to heap profiling.\n\n  Bug fixes:\n  - Fix a prof-related race condition.  This bug could cause memory corruption,\n    but only occurred in non-default configurations (prof_accum:false).\n  - Fix off-by-one backtracing issues (make sure that prof_alloc_prep() is\n    excluded from backtraces).\n  - Fix a prof-related bug in realloc() (only triggered by OOM errors).\n  - Fix prof-related bugs in allocm() and rallocm().\n  - Fix prof_tdata_cleanup() for --disable-tls builds.\n  - Fix a relative include path, to fix objdir builds.\n\n* 2.2.2 (July 30, 2011)\n\n  Bug fixes:\n  - Fix a build error for --disable-tcache.\n  - Fix assertions in arena_purge() (for real this time).\n  - Add the --with-private-namespace option.  This is a workaround for symbol\n    conflicts that can inadvertently arise when using static libraries.\n\n* 2.2.1 (March 30, 2011)\n\n  Bug fixes:\n  - Implement atomic operations for x86/x64.  This fixes compilation failures\n    for versions of gcc that are still in wide use.\n  - Fix an assertion in arena_purge().\n\n* 2.2.0 (March 22, 2011)\n\n  This version incorporates several improvements to algorithms and data\n  structures that tend to reduce fragmentation and increase speed.\n\n  New features:\n  - Add the \"stats.cactive\" mallctl.\n  - Update pprof (from google-perftools 1.7).\n  - Improve backtracing-related configuration logic, and add the\n    --disable-prof-libgcc option.\n\n  Bug fixes:\n  - Change default symbol visibility from \"internal\", to \"hidden\", which\n    decreases the overhead of library-internal function calls.\n  - Fix symbol visibility so that it is also set on OS X.\n  - Fix a build dependency regression caused by the introduction of the .pic.o\n    suffix for PIC object files.\n  - Add missing checks for mutex initialization failures.\n  - Don't use libgcc-based backtracing except on x64, where it is known to work.\n  - Fix deadlocks on OS X that were due to memory allocation in\n    pthread_mutex_lock().\n  - Heap profiling-specific fixes:\n    + Fix memory corruption due to integer overflow in small region index\n      computation, when using a small enough sample interval that profiling\n      context pointers are stored in small run headers.\n    + Fix a bootstrap ordering bug that only occurred with TLS disabled.\n    + Fix a rallocm() rsize bug.\n    + Fix error detection bugs for aligned memory allocation.\n\n* 2.1.3 (March 14, 2011)\n\n  Bug fixes:\n  - Fix a cpp logic regression (due to the \"thread.{de,}allocatedp\" mallctl fix\n    for OS X in 2.1.2).\n  - Fix a \"thread.arena\" mallctl bug.\n  - Fix a thread cache stats merging bug.\n\n* 2.1.2 (March 2, 2011)\n\n  Bug fixes:\n  - Fix \"thread.{de,}allocatedp\" mallctl for OS X.\n  - Add missing jemalloc.a to build system.\n\n* 2.1.1 (January 31, 2011)\n\n  Bug fixes:\n  - Fix aligned huge reallocation (affected allocm()).\n  - Fix the ALLOCM_LG_ALIGN macro definition.\n  - Fix a heap dumping deadlock.\n  - Fix a \"thread.arena\" mallctl bug.\n\n* 2.1.0 (December 3, 2010)\n\n  This version incorporates some optimizations that can't quite be considered\n  bug fixes.\n\n  New features:\n  - Use Linux's mremap(2) for huge object reallocation when possible.\n  - Avoid locking in mallctl*() when possible.\n  - Add the \"thread.[de]allocatedp\" mallctl's.\n  - Convert the manual page source from roff to DocBook, and generate both roff\n    and HTML manuals.\n\n  Bug fixes:\n  - Fix a crash due to incorrect bootstrap ordering.  This only impacted\n    --enable-debug --enable-dss configurations.\n  - Fix a minor statistics bug for mallctl(\"swap.avail\", ...).\n\n* 2.0.1 (October 29, 2010)\n\n  Bug fixes:\n  - Fix a race condition in heap profiling that could cause undefined behavior\n    if \"opt.prof_accum\" were disabled.\n  - Add missing mutex unlocks for some OOM error paths in the heap profiling\n    code.\n  - Fix a compilation error for non-C99 builds.\n\n* 2.0.0 (October 24, 2010)\n\n  This version focuses on the experimental *allocm() API, and on improved\n  run-time configuration/introspection.  Nonetheless, numerous performance\n  improvements are also included.\n\n  New features:\n  - Implement the experimental {,r,s,d}allocm() API, which provides a superset\n    of the functionality available via malloc(), calloc(), posix_memalign(),\n    realloc(), malloc_usable_size(), and free().  These functions can be used to\n    allocate/reallocate aligned zeroed memory, ask for optional extra memory\n    during reallocation, prevent object movement during reallocation, etc.\n  - Replace JEMALLOC_OPTIONS/JEMALLOC_PROF_PREFIX with MALLOC_CONF, which is\n    more human-readable, and more flexible.  For example:\n      JEMALLOC_OPTIONS=AJP\n    is now:\n      MALLOC_CONF=abort:true,fill:true,stats_print:true\n  - Port to Apple OS X.  Sponsored by Mozilla.\n  - Make it possible for the application to control thread-->arena mappings via\n    the \"thread.arena\" mallctl.\n  - Add compile-time support for all TLS-related functionality via pthreads TSD.\n    This is mainly of interest for OS X, which does not support TLS, but has a\n    TSD implementation with similar performance.\n  - Override memalign() and valloc() if they are provided by the system.\n  - Add the \"arenas.purge\" mallctl, which can be used to synchronously purge all\n    dirty unused pages.\n  - Make cumulative heap profiling data optional, so that it is possible to\n    limit the amount of memory consumed by heap profiling data structures.\n  - Add per thread allocation counters that can be accessed via the\n    \"thread.allocated\" and \"thread.deallocated\" mallctls.\n\n  Incompatible changes:\n  - Remove JEMALLOC_OPTIONS and malloc_options (see MALLOC_CONF above).\n  - Increase default backtrace depth from 4 to 128 for heap profiling.\n  - Disable interval-based profile dumps by default.\n\n  Bug fixes:\n  - Remove bad assertions in fork handler functions.  These assertions could\n    cause aborts for some combinations of configure settings.\n  - Fix strerror_r() usage to deal with non-standard semantics in GNU libc.\n  - Fix leak context reporting.  This bug tended to cause the number of contexts\n    to be underreported (though the reported number of objects and bytes were\n    correct).\n  - Fix a realloc() bug for large in-place growing reallocation.  This bug could\n    cause memory corruption, but it was hard to trigger.\n  - Fix an allocation bug for small allocations that could be triggered if\n    multiple threads raced to create a new run of backing pages.\n  - Enhance the heap profiler to trigger samples based on usable size, rather\n    than request size.\n  - Fix a heap profiling bug due to sometimes losing track of requested object\n    size for sampled objects.\n\n* 1.0.3 (August 12, 2010)\n\n  Bug fixes:\n  - Fix the libunwind-based implementation of stack backtracing (used for heap\n    profiling).  This bug could cause zero-length backtraces to be reported.\n  - Add a missing mutex unlock in library initialization code.  If multiple\n    threads raced to initialize malloc, some of them could end up permanently\n    blocked.\n\n* 1.0.2 (May 11, 2010)\n\n  Bug fixes:\n  - Fix junk filling of large objects, which could cause memory corruption.\n  - Add MAP_NORESERVE support for chunk mapping, because otherwise virtual\n    memory limits could cause swap file configuration to fail.  Contributed by\n    Jordan DeLong.\n\n* 1.0.1 (April 14, 2010)\n\n  Bug fixes:\n  - Fix compilation when --enable-fill is specified.\n  - Fix threads-related profiling bugs that affected accuracy and caused memory\n    to be leaked during thread exit.\n  - Fix dirty page purging race conditions that could cause crashes.\n  - Fix crash in tcache flushing code during thread destruction.\n\n* 1.0.0 (April 11, 2010)\n\n  This release focuses on speed and run-time introspection.  Numerous\n  algorithmic improvements make this release substantially faster than its\n  predecessors.\n\n  New features:\n  - Implement autoconf-based configuration system.\n  - Add mallctl*(), for the purposes of introspection and run-time\n    configuration.\n  - Make it possible for the application to manually flush a thread's cache, via\n    the \"tcache.flush\" mallctl.\n  - Base maximum dirty page count on proportion of active memory.\n  - Compute various additional run-time statistics, including per size class\n    statistics for large objects.\n  - Expose malloc_stats_print(), which can be called repeatedly by the\n    application.\n  - Simplify the malloc_message() signature to only take one string argument,\n    and incorporate an opaque data pointer argument for use by the application\n    in combination with malloc_stats_print().\n  - Add support for allocation backed by one or more swap files, and allow the\n    application to disable over-commit if swap files are in use.\n  - Implement allocation profiling and leak checking.\n\n  Removed features:\n  - Remove the dynamic arena rebalancing code, since thread-specific caching\n    reduces its utility.\n\n  Bug fixes:\n  - Modify chunk allocation to work when address space layout randomization\n    (ASLR) is in use.\n  - Fix thread cleanup bugs related to TLS destruction.\n  - Handle 0-size allocation requests in posix_memalign().\n  - Fix a chunk leak.  The leaked chunks were never touched, so this impacted\n    virtual memory usage, but not physical memory usage.\n\n* linux_2008082[78]a (August 27/28, 2008)\n\n  These snapshot releases are the simple result of incorporating Linux-specific\n  support into the FreeBSD malloc sources.\n\n--------------------------------------------------------------------------------\nvim:filetype=text:textwidth=80\n"
  },
  {
    "path": "deps/jemalloc/INSTALL.md",
    "content": "Building and installing a packaged release of jemalloc can be as simple as\ntyping the following while in the root directory of the source tree:\n\n    ./configure\n    make\n    make install\n\nIf building from unpackaged developer sources, the simplest command sequence\nthat might work is:\n\n    ./autogen.sh\n    make dist\n    make\n    make install\n\nNote that documentation is not built by the default target because doing so\nwould create a dependency on xsltproc in packaged releases, hence the\nrequirement to either run 'make dist' or avoid installing docs via the various\ninstall_* targets documented below.\n\n\n## Advanced configuration\n\nThe 'configure' script supports numerous options that allow control of which\nfunctionality is enabled, where jemalloc is installed, etc.  Optionally, pass\nany of the following arguments (not a definitive list) to 'configure':\n\n* `--help`\n\n    Print a definitive list of options.\n\n* `--prefix=<install-root-dir>`\n\n    Set the base directory in which to install.  For example:\n\n        ./configure --prefix=/usr/local\n\n    will cause files to be installed into /usr/local/include, /usr/local/lib,\n    and /usr/local/man.\n\n* `--with-version=(<major>.<minor>.<bugfix>-<nrev>-g<gid>|VERSION)`\n\n    The VERSION file is mandatory for successful configuration, and the\n    following steps are taken to assure its presence:\n    1) If --with-version=<major>.<minor>.<bugfix>-<nrev>-g<gid> is specified,\n       generate VERSION using the specified value.\n    2) If --with-version is not specified in either form and the source\n       directory is inside a git repository, try to generate VERSION via 'git\n       describe' invocations that pattern-match release tags.\n    3) If VERSION is missing, generate it with a bogus version:\n       0.0.0-0-g0000000000000000000000000000000000000000\n\n    Note that --with-version=VERSION bypasses (1) and (2), which simplifies\n    VERSION configuration when embedding a jemalloc release into another\n    project's git repository.\n\n* `--with-rpath=<colon-separated-rpath>`\n\n    Embed one or more library paths, so that libjemalloc can find the libraries\n    it is linked to.  This works only on ELF-based systems.\n\n* `--with-mangling=<map>`\n\n    Mangle public symbols specified in <map> which is a comma-separated list of\n    name:mangled pairs.\n\n    For example, to use ld's --wrap option as an alternative method for\n    overriding libc's malloc implementation, specify something like:\n\n      --with-mangling=malloc:__wrap_malloc,free:__wrap_free[...]\n\n    Note that mangling happens prior to application of the prefix specified by\n    --with-jemalloc-prefix, and mangled symbols are then ignored when applying\n    the prefix.\n\n* `--with-jemalloc-prefix=<prefix>`\n\n    Prefix all public APIs with <prefix>.  For example, if <prefix> is\n    \"prefix_\", API changes like the following occur:\n\n      malloc()         --> prefix_malloc()\n      malloc_conf      --> prefix_malloc_conf\n      /etc/malloc.conf --> /etc/prefix_malloc.conf\n      MALLOC_CONF      --> PREFIX_MALLOC_CONF\n\n    This makes it possible to use jemalloc at the same time as the system\n    allocator, or even to use multiple copies of jemalloc simultaneously.\n\n    By default, the prefix is \"\", except on OS X, where it is \"je_\".  On OS X,\n    jemalloc overlays the default malloc zone, but makes no attempt to actually\n    replace the \"malloc\", \"calloc\", etc. symbols.\n\n* `--without-export`\n\n    Don't export public APIs.  This can be useful when building jemalloc as a\n    static library, or to avoid exporting public APIs when using the zone\n    allocator on OSX.\n\n* `--with-private-namespace=<prefix>`\n\n    Prefix all library-private APIs with <prefix>je_.  For shared libraries,\n    symbol visibility mechanisms prevent these symbols from being exported, but\n    for static libraries, naming collisions are a real possibility.  By\n    default, <prefix> is empty, which results in a symbol prefix of je_ .\n\n* `--with-install-suffix=<suffix>`\n\n    Append <suffix> to the base name of all installed files, such that multiple\n    versions of jemalloc can coexist in the same installation directory.  For\n    example, libjemalloc.so.0 becomes libjemalloc<suffix>.so.0.\n\n* `--with-malloc-conf=<malloc_conf>`\n\n    Embed `<malloc_conf>` as a run-time options string that is processed prior to\n    the malloc_conf global variable, the /etc/malloc.conf symlink, and the\n    MALLOC_CONF environment variable.  For example, to change the default decay\n    time to 30 seconds:\n\n      --with-malloc-conf=decay_ms:30000\n\n* `--enable-debug`\n\n    Enable assertions and validation code.  This incurs a substantial\n    performance hit, but is very useful during application development.\n\n* `--disable-stats`\n\n    Disable statistics gathering functionality.  See the \"opt.stats_print\"\n    option documentation for usage details.\n\n* `--enable-prof`\n\n    Enable heap profiling and leak detection functionality.  See the \"opt.prof\"\n    option documentation for usage details.  When enabled, there are several\n    approaches to backtracing, and the configure script chooses the first one\n    in the following list that appears to function correctly:\n\n    + libunwind      (requires --enable-prof-libunwind)\n    + libgcc         (unless --disable-prof-libgcc)\n    + gcc intrinsics (unless --disable-prof-gcc)\n\n* `--enable-prof-libunwind`\n\n    Use the libunwind library (http://www.nongnu.org/libunwind/) for stack\n    backtracing.\n\n* `--disable-prof-libgcc`\n\n    Disable the use of libgcc's backtracing functionality.\n\n* `--disable-prof-gcc`\n\n    Disable the use of gcc intrinsics for backtracing.\n\n* `--with-static-libunwind=<libunwind.a>`\n\n    Statically link against the specified libunwind.a rather than dynamically\n    linking with -lunwind.\n\n* `--disable-fill`\n\n    Disable support for junk/zero filling of memory.  See the \"opt.junk\" and\n    \"opt.zero\" option documentation for usage details.\n\n* `--disable-zone-allocator`\n\n    Disable zone allocator for Darwin.  This means jemalloc won't be hooked as\n    the default allocator on OSX/iOS.\n\n* `--enable-utrace`\n\n    Enable utrace(2)-based allocation tracing.  This feature is not broadly\n    portable (FreeBSD has it, but Linux and OS X do not).\n\n* `--enable-xmalloc`\n\n    Enable support for optional immediate termination due to out-of-memory\n    errors, as is commonly implemented by \"xmalloc\" wrapper function for malloc.\n    See the \"opt.xmalloc\" option documentation for usage details.\n\n* `--enable-lazy-lock`\n\n    Enable code that wraps pthread_create() to detect when an application\n    switches from single-threaded to multi-threaded mode, so that it can avoid\n    mutex locking/unlocking operations while in single-threaded mode.  In\n    practice, this feature usually has little impact on performance unless\n    thread-specific caching is disabled.\n\n* `--disable-cache-oblivious`\n\n    Disable cache-oblivious large allocation alignment for large allocation\n    requests with no alignment constraints.  If this feature is disabled, all\n    large allocations are page-aligned as an implementation artifact, which can\n    severely harm CPU cache utilization.  However, the cache-oblivious layout\n    comes at the cost of one extra page per large allocation, which in the\n    most extreme case increases physical memory usage for the 16 KiB size class\n    to 20 KiB.\n\n* `--disable-syscall`\n\n    Disable use of syscall(2) rather than {open,read,write,close}(2).  This is\n    intended as a workaround for systems that place security limitations on\n    syscall(2).\n\n* `--disable-cxx`\n\n    Disable C++ integration.  This will cause new and delete operator\n    implementations to be omitted.\n\n* `--with-xslroot=<path>`\n\n    Specify where to find DocBook XSL stylesheets when building the\n    documentation.\n\n* `--with-lg-page=<lg-page>`\n\n    Specify the base 2 log of the allocator page size, which must in turn be at\n    least as large as the system page size.  By default the configure script\n    determines the host's page size and sets the allocator page size equal to\n    the system page size, so this option need not be specified unless the\n    system page size may change between configuration and execution, e.g. when\n    cross compiling.\n\n* `--with-lg-hugepage=<lg-hugepage>`\n\n    Specify the base 2 log of the system huge page size.  This option is useful\n    when cross compiling, or when overriding the default for systems that do\n    not explicitly support huge pages.\n\n* `--with-lg-quantum=<lg-quantum>`\n\n    Specify the base 2 log of the minimum allocation alignment.  jemalloc needs\n    to know the minimum alignment that meets the following C standard\n    requirement (quoted from the April 12, 2011 draft of the C11 standard):\n\n    >  The pointer returned if the allocation succeeds is suitably aligned so\n      that it may be assigned to a pointer to any type of object with a\n      fundamental alignment requirement and then used to access such an object\n      or an array of such objects in the space allocated [...]\n\n    This setting is architecture-specific, and although jemalloc includes known\n    safe values for the most commonly used modern architectures, there is a\n    wrinkle related to GNU libc (glibc) that may impact your choice of\n    <lg-quantum>.  On most modern architectures, this mandates 16-byte\n    alignment (<lg-quantum>=4), but the glibc developers chose not to meet this\n    requirement for performance reasons.  An old discussion can be found at\n    <https://sourceware.org/bugzilla/show_bug.cgi?id=206> .  Unlike glibc,\n    jemalloc does follow the C standard by default (caveat: jemalloc\n    technically cheats for size classes smaller than the quantum), but the fact\n    that Linux systems already work around this allocator noncompliance means\n    that it is generally safe in practice to let jemalloc's minimum alignment\n    follow glibc's lead.  If you specify `--with-lg-quantum=3` during\n    configuration, jemalloc will provide additional size classes that are not\n    16-byte-aligned (24, 40, and 56).\n\n* `--with-lg-vaddr=<lg-vaddr>`\n\n    Specify the number of significant virtual address bits.  By default, the\n    configure script attempts to detect virtual address size on those platforms\n    where it knows how, and picks a default otherwise.  This option may be\n    useful when cross-compiling.\n\n* `--disable-initial-exec-tls`\n\n    Disable the initial-exec TLS model for jemalloc's internal thread-local\n    storage (on those platforms that support explicit settings).  This can allow\n    jemalloc to be dynamically loaded after program startup (e.g. using dlopen).\n    Note that in this case, there will be two malloc implementations operating\n    in the same process, which will almost certainly result in confusing runtime\n    crashes if pointers leak from one implementation to the other.\n\n* `--disable-libdl`\n\n    Disable the usage of libdl, namely dlsym(3) which is required by the lazy\n    lock option.  This can allow building static binaries.\n\nThe following environment variables (not a definitive list) impact configure's\nbehavior:\n\n* `CFLAGS=\"?\"`\n* `CXXFLAGS=\"?\"`\n\n    Pass these flags to the C/C++ compiler.  Any flags set by the configure\n    script are prepended, which means explicitly set flags generally take\n    precedence.  Take care when specifying flags such as -Werror, because\n    configure tests may be affected in undesirable ways.\n\n* `EXTRA_CFLAGS=\"?\"`\n* `EXTRA_CXXFLAGS=\"?\"`\n\n    Append these flags to CFLAGS/CXXFLAGS, without passing them to the\n    compiler(s) during configuration.  This makes it possible to add flags such\n    as -Werror, while allowing the configure script to determine what other\n    flags are appropriate for the specified configuration.\n\n* `CPPFLAGS=\"?\"`\n\n    Pass these flags to the C preprocessor.  Note that CFLAGS is not passed to\n    'cpp' when 'configure' is looking for include files, so you must use\n    CPPFLAGS instead if you need to help 'configure' find header files.\n\n* `LD_LIBRARY_PATH=\"?\"`\n\n    'ld' uses this colon-separated list to find libraries.\n\n* `LDFLAGS=\"?\"`\n\n    Pass these flags when linking.\n\n* `PATH=\"?\"`\n\n    'configure' uses this to find programs.\n\nIn some cases it may be necessary to work around configuration results that do\nnot match reality.  For example, Linux 4.5 added support for the MADV_FREE flag\nto madvise(2), which can cause problems if building on a host with MADV_FREE\nsupport and deploying to a target without.  To work around this, use a cache\nfile to override the relevant configuration variable defined in configure.ac,\ne.g.:\n\n    echo \"je_cv_madv_free=no\" > config.cache && ./configure -C\n\n\n## Advanced compilation\n\nTo build only parts of jemalloc, use the following targets:\n\n    build_lib_shared\n    build_lib_static\n    build_lib\n    build_doc_html\n    build_doc_man\n    build_doc\n\nTo install only parts of jemalloc, use the following targets:\n\n    install_bin\n    install_include\n    install_lib_shared\n    install_lib_static\n    install_lib_pc\n    install_lib\n    install_doc_html\n    install_doc_man\n    install_doc\n\nTo clean up build results to varying degrees, use the following make targets:\n\n    clean\n    distclean\n    relclean\n\n\n## Advanced installation\n\nOptionally, define make variables when invoking make, including (not\nexclusively):\n\n* `INCLUDEDIR=\"?\"`\n\n    Use this as the installation prefix for header files.\n\n* `LIBDIR=\"?\"`\n\n    Use this as the installation prefix for libraries.\n\n* `MANDIR=\"?\"`\n\n    Use this as the installation prefix for man pages.\n\n* `DESTDIR=\"?\"`\n\n    Prepend DESTDIR to INCLUDEDIR, LIBDIR, DATADIR, and MANDIR.  This is useful\n    when installing to a different path than was specified via --prefix.\n\n* `CC=\"?\"`\n\n    Use this to invoke the C compiler.\n\n* `CFLAGS=\"?\"`\n\n    Pass these flags to the compiler.\n\n* `CPPFLAGS=\"?\"`\n\n    Pass these flags to the C preprocessor.\n\n* `LDFLAGS=\"?\"`\n\n    Pass these flags when linking.\n\n* `PATH=\"?\"`\n\n    Use this to search for programs used during configuration and building.\n\n\n## Development\n\nIf you intend to make non-trivial changes to jemalloc, use the 'autogen.sh'\nscript rather than 'configure'.  This re-generates 'configure', enables\nconfiguration dependency rules, and enables re-generation of automatically\ngenerated source files.\n\nThe build system supports using an object directory separate from the source\ntree.  For example, you can create an 'obj' directory, and from within that\ndirectory, issue configuration and build commands:\n\n    autoconf\n    mkdir obj\n    cd obj\n    ../configure --enable-autogen\n    make\n\n\n## Documentation\n\nThe manual page is generated in both html and roff formats.  Any web browser\ncan be used to view the html manual.  The roff manual page can be formatted\nprior to installation via the following command:\n\n    nroff -man -t doc/jemalloc.3\n"
  },
  {
    "path": "deps/jemalloc/Makefile.in",
    "content": "# Clear out all vpaths, then set just one (default vpath) for the main build\n# directory.\nvpath\nvpath % .\n\n# Clear the default suffixes, so that built-in rules are not used.\n.SUFFIXES :\n\nSHELL := /bin/sh\n\nCC := @CC@\nCXX := @CXX@\n\n# Configuration parameters.\nDESTDIR =\nBINDIR := $(DESTDIR)@BINDIR@\nINCLUDEDIR := $(DESTDIR)@INCLUDEDIR@\nLIBDIR := $(DESTDIR)@LIBDIR@\nDATADIR := $(DESTDIR)@DATADIR@\nMANDIR := $(DESTDIR)@MANDIR@\nsrcroot := @srcroot@\nobjroot := @objroot@\nabs_srcroot := @abs_srcroot@\nabs_objroot := @abs_objroot@\n\n# Build parameters.\nCPPFLAGS := @CPPFLAGS@ -I$(objroot)include -I$(srcroot)include\nCONFIGURE_CFLAGS := @CONFIGURE_CFLAGS@\nSPECIFIED_CFLAGS := @SPECIFIED_CFLAGS@\nEXTRA_CFLAGS := @EXTRA_CFLAGS@\nCFLAGS := $(strip $(CONFIGURE_CFLAGS) $(SPECIFIED_CFLAGS) $(EXTRA_CFLAGS))\nCONFIGURE_CXXFLAGS := @CONFIGURE_CXXFLAGS@\nSPECIFIED_CXXFLAGS := @SPECIFIED_CXXFLAGS@\nEXTRA_CXXFLAGS := @EXTRA_CXXFLAGS@\nCXXFLAGS := $(strip $(CONFIGURE_CXXFLAGS) $(SPECIFIED_CXXFLAGS) $(EXTRA_CXXFLAGS))\nLDFLAGS := @LDFLAGS@\nEXTRA_LDFLAGS := @EXTRA_LDFLAGS@\nLIBS := @LIBS@\nRPATH_EXTRA := @RPATH_EXTRA@\nSO := @so@\nIMPORTLIB := @importlib@\nO := @o@\nA := @a@\nEXE := @exe@\nLIBPREFIX := @libprefix@\nREV := @rev@\ninstall_suffix := @install_suffix@\nABI := @abi@\nXSLTPROC := @XSLTPROC@\nXSLROOT := @XSLROOT@\nAUTOCONF := @AUTOCONF@\n_RPATH = @RPATH@\nRPATH = $(if $(1),$(call _RPATH,$(1)))\ncfghdrs_in := $(addprefix $(srcroot),@cfghdrs_in@)\ncfghdrs_out := @cfghdrs_out@\ncfgoutputs_in := $(addprefix $(srcroot),@cfgoutputs_in@)\ncfgoutputs_out := @cfgoutputs_out@\nenable_autogen := @enable_autogen@\nenable_doc := @enable_doc@\nenable_shared := @enable_shared@\nenable_static := @enable_static@\nenable_prof := @enable_prof@\nenable_zone_allocator := @enable_zone_allocator@\nenable_experimental_smallocx := @enable_experimental_smallocx@\nMALLOC_CONF := @JEMALLOC_CPREFIX@MALLOC_CONF\nlink_whole_archive := @link_whole_archive@\nDSO_LDFLAGS = @DSO_LDFLAGS@\nSOREV = @SOREV@\nPIC_CFLAGS = @PIC_CFLAGS@\nCTARGET = @CTARGET@\nLDTARGET = @LDTARGET@\nTEST_LD_MODE = @TEST_LD_MODE@\nMKLIB = @MKLIB@\nAR = @AR@\nARFLAGS = @ARFLAGS@\nDUMP_SYMS = @DUMP_SYMS@\nAWK := @AWK@\nCC_MM = @CC_MM@\nLM := @LM@\nINSTALL = @INSTALL@\n\nifeq (macho, $(ABI))\nTEST_LIBRARY_PATH := DYLD_FALLBACK_LIBRARY_PATH=\"$(objroot)lib\"\nelse\nifeq (pecoff, $(ABI))\nTEST_LIBRARY_PATH := PATH=\"$(PATH):$(objroot)lib\"\nelse\nTEST_LIBRARY_PATH :=\nendif\nendif\n\nLIBJEMALLOC := $(LIBPREFIX)jemalloc$(install_suffix)\n\n# Lists of files.\nBINS := $(objroot)bin/jemalloc-config $(objroot)bin/jemalloc.sh $(objroot)bin/jeprof\nC_HDRS := $(objroot)include/jemalloc/jemalloc$(install_suffix).h\nC_SRCS := $(srcroot)src/jemalloc.c \\\n\t$(srcroot)src/arena.c \\\n\t$(srcroot)src/background_thread.c \\\n\t$(srcroot)src/base.c \\\n\t$(srcroot)src/bin.c \\\n\t$(srcroot)src/bitmap.c \\\n\t$(srcroot)src/ckh.c \\\n\t$(srcroot)src/ctl.c \\\n\t$(srcroot)src/div.c \\\n\t$(srcroot)src/extent.c \\\n\t$(srcroot)src/extent_dss.c \\\n\t$(srcroot)src/extent_mmap.c \\\n\t$(srcroot)src/hash.c \\\n\t$(srcroot)src/hook.c \\\n\t$(srcroot)src/large.c \\\n\t$(srcroot)src/log.c \\\n\t$(srcroot)src/malloc_io.c \\\n\t$(srcroot)src/mutex.c \\\n\t$(srcroot)src/mutex_pool.c \\\n\t$(srcroot)src/nstime.c \\\n\t$(srcroot)src/pages.c \\\n\t$(srcroot)src/prng.c \\\n\t$(srcroot)src/prof.c \\\n\t$(srcroot)src/rtree.c \\\n\t$(srcroot)src/safety_check.c \\\n\t$(srcroot)src/stats.c \\\n\t$(srcroot)src/sc.c \\\n\t$(srcroot)src/sz.c \\\n\t$(srcroot)src/tcache.c \\\n\t$(srcroot)src/test_hooks.c \\\n\t$(srcroot)src/ticker.c \\\n\t$(srcroot)src/tsd.c \\\n\t$(srcroot)src/witness.c\nifeq ($(enable_zone_allocator), 1)\nC_SRCS += $(srcroot)src/zone.c\nendif\nifeq ($(IMPORTLIB),$(SO))\nSTATIC_LIBS := $(objroot)lib/$(LIBJEMALLOC).$(A)\nendif\nifdef PIC_CFLAGS\nSTATIC_LIBS += $(objroot)lib/$(LIBJEMALLOC)_pic.$(A)\nelse\nSTATIC_LIBS += $(objroot)lib/$(LIBJEMALLOC)_s.$(A)\nendif\nDSOS := $(objroot)lib/$(LIBJEMALLOC).$(SOREV)\nifneq ($(SOREV),$(SO))\nDSOS += $(objroot)lib/$(LIBJEMALLOC).$(SO)\nendif\nifeq (1, $(link_whole_archive))\nLJEMALLOC := -Wl,--whole-archive -L$(objroot)lib -l$(LIBJEMALLOC) -Wl,--no-whole-archive\nelse\nLJEMALLOC := $(objroot)lib/$(LIBJEMALLOC).$(IMPORTLIB)\nendif\nPC := $(objroot)jemalloc.pc\nMAN3 := $(objroot)doc/jemalloc$(install_suffix).3\nDOCS_XML := $(objroot)doc/jemalloc$(install_suffix).xml\nDOCS_HTML := $(DOCS_XML:$(objroot)%.xml=$(objroot)%.html)\nDOCS_MAN3 := $(DOCS_XML:$(objroot)%.xml=$(objroot)%.3)\nDOCS := $(DOCS_HTML) $(DOCS_MAN3)\nC_TESTLIB_SRCS := $(srcroot)test/src/btalloc.c $(srcroot)test/src/btalloc_0.c \\\n\t$(srcroot)test/src/btalloc_1.c $(srcroot)test/src/math.c \\\n\t$(srcroot)test/src/mtx.c $(srcroot)test/src/mq.c \\\n\t$(srcroot)test/src/SFMT.c $(srcroot)test/src/test.c \\\n\t$(srcroot)test/src/thd.c $(srcroot)test/src/timer.c\nifeq (1, $(link_whole_archive))\nC_UTIL_INTEGRATION_SRCS :=\nC_UTIL_CPP_SRCS :=\nelse\nC_UTIL_INTEGRATION_SRCS := $(srcroot)src/nstime.c $(srcroot)src/malloc_io.c\nC_UTIL_CPP_SRCS := $(srcroot)src/nstime.c $(srcroot)src/malloc_io.c\nendif\nTESTS_UNIT := \\\n\t$(srcroot)test/unit/a0.c \\\n\t$(srcroot)test/unit/arena_reset.c \\\n\t$(srcroot)test/unit/atomic.c \\\n\t$(srcroot)test/unit/background_thread.c \\\n\t$(srcroot)test/unit/background_thread_enable.c \\\n\t$(srcroot)test/unit/base.c \\\n\t$(srcroot)test/unit/bitmap.c \\\n\t$(srcroot)test/unit/bit_util.c \\\n\t$(srcroot)test/unit/binshard.c \\\n\t$(srcroot)test/unit/ckh.c \\\n\t$(srcroot)test/unit/decay.c \\\n\t$(srcroot)test/unit/div.c \\\n\t$(srcroot)test/unit/emitter.c \\\n\t$(srcroot)test/unit/extent_quantize.c \\\n\t$(srcroot)test/unit/extent_util.c \\\n\t$(srcroot)test/unit/fork.c \\\n\t$(srcroot)test/unit/hash.c \\\n\t$(srcroot)test/unit/hook.c \\\n\t$(srcroot)test/unit/huge.c \\\n\t$(srcroot)test/unit/junk.c \\\n\t$(srcroot)test/unit/junk_alloc.c \\\n\t$(srcroot)test/unit/junk_free.c \\\n\t$(srcroot)test/unit/log.c \\\n\t$(srcroot)test/unit/mallctl.c \\\n\t$(srcroot)test/unit/malloc_io.c \\\n\t$(srcroot)test/unit/math.c \\\n\t$(srcroot)test/unit/mq.c \\\n\t$(srcroot)test/unit/mtx.c \\\n\t$(srcroot)test/unit/pack.c \\\n\t$(srcroot)test/unit/pages.c \\\n\t$(srcroot)test/unit/ph.c \\\n\t$(srcroot)test/unit/prng.c \\\n\t$(srcroot)test/unit/prof_accum.c \\\n\t$(srcroot)test/unit/prof_active.c \\\n\t$(srcroot)test/unit/prof_gdump.c \\\n\t$(srcroot)test/unit/prof_idump.c \\\n\t$(srcroot)test/unit/prof_log.c \\\n\t$(srcroot)test/unit/prof_reset.c \\\n\t$(srcroot)test/unit/prof_tctx.c \\\n\t$(srcroot)test/unit/prof_thread_name.c \\\n\t$(srcroot)test/unit/ql.c \\\n\t$(srcroot)test/unit/qr.c \\\n\t$(srcroot)test/unit/rb.c \\\n\t$(srcroot)test/unit/retained.c \\\n\t$(srcroot)test/unit/rtree.c \\\n\t$(srcroot)test/unit/safety_check.c \\\n\t$(srcroot)test/unit/seq.c \\\n\t$(srcroot)test/unit/SFMT.c \\\n\t$(srcroot)test/unit/sc.c \\\n\t$(srcroot)test/unit/size_classes.c \\\n\t$(srcroot)test/unit/slab.c \\\n\t$(srcroot)test/unit/smoothstep.c \\\n\t$(srcroot)test/unit/spin.c \\\n\t$(srcroot)test/unit/stats.c \\\n\t$(srcroot)test/unit/stats_print.c \\\n\t$(srcroot)test/unit/test_hooks.c \\\n\t$(srcroot)test/unit/ticker.c \\\n\t$(srcroot)test/unit/nstime.c \\\n\t$(srcroot)test/unit/tsd.c \\\n\t$(srcroot)test/unit/witness.c \\\n\t$(srcroot)test/unit/zero.c\nifeq (@enable_prof@, 1)\nTESTS_UNIT += \\\n\t$(srcroot)test/unit/arena_reset_prof.c\nendif\nTESTS_INTEGRATION := $(srcroot)test/integration/aligned_alloc.c \\\n\t$(srcroot)test/integration/allocated.c \\\n\t$(srcroot)test/integration/extent.c \\\n\t$(srcroot)test/integration/malloc.c \\\n\t$(srcroot)test/integration/mallocx.c \\\n\t$(srcroot)test/integration/MALLOCX_ARENA.c \\\n\t$(srcroot)test/integration/overflow.c \\\n\t$(srcroot)test/integration/posix_memalign.c \\\n\t$(srcroot)test/integration/rallocx.c \\\n\t$(srcroot)test/integration/sdallocx.c \\\n\t$(srcroot)test/integration/slab_sizes.c \\\n\t$(srcroot)test/integration/thread_arena.c \\\n\t$(srcroot)test/integration/thread_tcache_enabled.c \\\n\t$(srcroot)test/integration/xallocx.c\nifeq (@enable_experimental_smallocx@, 1)\nTESTS_INTEGRATION += \\\n  $(srcroot)test/integration/smallocx.c\nendif\nifeq (@enable_cxx@, 1)\nCPP_SRCS := $(srcroot)src/jemalloc_cpp.cpp\nTESTS_INTEGRATION_CPP := $(srcroot)test/integration/cpp/basic.cpp\nelse\nCPP_SRCS :=\nTESTS_INTEGRATION_CPP :=\nendif\nTESTS_STRESS := $(srcroot)test/stress/microbench.c \\\n\t$(srcroot)test/stress/hookbench.c\n\n\nTESTS := $(TESTS_UNIT) $(TESTS_INTEGRATION) $(TESTS_INTEGRATION_CPP) $(TESTS_STRESS)\n\nPRIVATE_NAMESPACE_HDRS := $(objroot)include/jemalloc/internal/private_namespace.h $(objroot)include/jemalloc/internal/private_namespace_jet.h\nPRIVATE_NAMESPACE_GEN_HDRS := $(PRIVATE_NAMESPACE_HDRS:%.h=%.gen.h)\nC_SYM_OBJS := $(C_SRCS:$(srcroot)%.c=$(objroot)%.sym.$(O))\nC_SYMS := $(C_SRCS:$(srcroot)%.c=$(objroot)%.sym)\nC_OBJS := $(C_SRCS:$(srcroot)%.c=$(objroot)%.$(O))\nCPP_OBJS := $(CPP_SRCS:$(srcroot)%.cpp=$(objroot)%.$(O))\nC_PIC_OBJS := $(C_SRCS:$(srcroot)%.c=$(objroot)%.pic.$(O))\nCPP_PIC_OBJS := $(CPP_SRCS:$(srcroot)%.cpp=$(objroot)%.pic.$(O))\nC_JET_SYM_OBJS := $(C_SRCS:$(srcroot)%.c=$(objroot)%.jet.sym.$(O))\nC_JET_SYMS := $(C_SRCS:$(srcroot)%.c=$(objroot)%.jet.sym)\nC_JET_OBJS := $(C_SRCS:$(srcroot)%.c=$(objroot)%.jet.$(O))\nC_TESTLIB_UNIT_OBJS := $(C_TESTLIB_SRCS:$(srcroot)%.c=$(objroot)%.unit.$(O))\nC_TESTLIB_INTEGRATION_OBJS := $(C_TESTLIB_SRCS:$(srcroot)%.c=$(objroot)%.integration.$(O))\nC_UTIL_INTEGRATION_OBJS := $(C_UTIL_INTEGRATION_SRCS:$(srcroot)%.c=$(objroot)%.integration.$(O))\nC_TESTLIB_STRESS_OBJS := $(C_TESTLIB_SRCS:$(srcroot)%.c=$(objroot)%.stress.$(O))\nC_TESTLIB_OBJS := $(C_TESTLIB_UNIT_OBJS) $(C_TESTLIB_INTEGRATION_OBJS) $(C_UTIL_INTEGRATION_OBJS) $(C_TESTLIB_STRESS_OBJS)\n\nTESTS_UNIT_OBJS := $(TESTS_UNIT:$(srcroot)%.c=$(objroot)%.$(O))\nTESTS_INTEGRATION_OBJS := $(TESTS_INTEGRATION:$(srcroot)%.c=$(objroot)%.$(O))\nTESTS_INTEGRATION_CPP_OBJS := $(TESTS_INTEGRATION_CPP:$(srcroot)%.cpp=$(objroot)%.$(O))\nTESTS_STRESS_OBJS := $(TESTS_STRESS:$(srcroot)%.c=$(objroot)%.$(O))\nTESTS_OBJS := $(TESTS_UNIT_OBJS) $(TESTS_INTEGRATION_OBJS) $(TESTS_STRESS_OBJS)\nTESTS_CPP_OBJS := $(TESTS_INTEGRATION_CPP_OBJS)\n\n.PHONY: all dist build_doc_html build_doc_man build_doc\n.PHONY: install_bin install_include install_lib\n.PHONY: install_doc_html install_doc_man install_doc install\n.PHONY: tests check clean distclean relclean\n\n.SECONDARY : $(PRIVATE_NAMESPACE_GEN_HDRS) $(TESTS_OBJS) $(TESTS_CPP_OBJS)\n\n# Default target.\nall: build_lib\n\ndist: build_doc\n\n$(objroot)doc/%.html : $(objroot)doc/%.xml $(srcroot)doc/stylesheet.xsl $(objroot)doc/html.xsl\nifneq ($(XSLROOT),)\n\t$(XSLTPROC) -o $@ $(objroot)doc/html.xsl $<\nelse\nifeq ($(wildcard $(DOCS_HTML)),)\n\t@echo \"<p>Missing xsltproc.  Doc not built.</p>\" > $@\nendif\n\t@echo \"Missing xsltproc.  \"$@\" not (re)built.\"\nendif\n\n$(objroot)doc/%.3 : $(objroot)doc/%.xml $(srcroot)doc/stylesheet.xsl $(objroot)doc/manpages.xsl\nifneq ($(XSLROOT),)\n\t$(XSLTPROC) -o $@ $(objroot)doc/manpages.xsl $<\nelse\nifeq ($(wildcard $(DOCS_MAN3)),)\n\t@echo \"Missing xsltproc.  Doc not built.\" > $@\nendif\n\t@echo \"Missing xsltproc.  \"$@\" not (re)built.\"\nendif\n\nbuild_doc_html: $(DOCS_HTML)\nbuild_doc_man: $(DOCS_MAN3)\nbuild_doc: $(DOCS)\n\n#\n# Include generated dependency files.\n#\nifdef CC_MM\n-include $(C_SYM_OBJS:%.$(O)=%.d)\n-include $(C_OBJS:%.$(O)=%.d)\n-include $(CPP_OBJS:%.$(O)=%.d)\n-include $(C_PIC_OBJS:%.$(O)=%.d)\n-include $(CPP_PIC_OBJS:%.$(O)=%.d)\n-include $(C_JET_SYM_OBJS:%.$(O)=%.d)\n-include $(C_JET_OBJS:%.$(O)=%.d)\n-include $(C_TESTLIB_OBJS:%.$(O)=%.d)\n-include $(TESTS_OBJS:%.$(O)=%.d)\n-include $(TESTS_CPP_OBJS:%.$(O)=%.d)\nendif\n\n$(C_SYM_OBJS): $(objroot)src/%.sym.$(O): $(srcroot)src/%.c\n$(C_SYM_OBJS): CPPFLAGS += -DJEMALLOC_NO_PRIVATE_NAMESPACE\n$(C_SYMS): $(objroot)src/%.sym: $(objroot)src/%.sym.$(O)\n$(C_OBJS): $(objroot)src/%.$(O): $(srcroot)src/%.c\n$(CPP_OBJS): $(objroot)src/%.$(O): $(srcroot)src/%.cpp\n$(C_PIC_OBJS): $(objroot)src/%.pic.$(O): $(srcroot)src/%.c\n$(C_PIC_OBJS): CFLAGS += $(PIC_CFLAGS)\n$(CPP_PIC_OBJS): $(objroot)src/%.pic.$(O): $(srcroot)src/%.cpp\n$(CPP_PIC_OBJS): CXXFLAGS += $(PIC_CFLAGS)\n$(C_JET_SYM_OBJS): $(objroot)src/%.jet.sym.$(O): $(srcroot)src/%.c\n$(C_JET_SYM_OBJS): CPPFLAGS += -DJEMALLOC_JET -DJEMALLOC_NO_PRIVATE_NAMESPACE\n$(C_JET_SYMS): $(objroot)src/%.jet.sym: $(objroot)src/%.jet.sym.$(O)\n$(C_JET_OBJS): $(objroot)src/%.jet.$(O): $(srcroot)src/%.c\n$(C_JET_OBJS): CPPFLAGS += -DJEMALLOC_JET\n$(C_TESTLIB_UNIT_OBJS): $(objroot)test/src/%.unit.$(O): $(srcroot)test/src/%.c\n$(C_TESTLIB_UNIT_OBJS): CPPFLAGS += -DJEMALLOC_UNIT_TEST\n$(C_TESTLIB_INTEGRATION_OBJS): $(objroot)test/src/%.integration.$(O): $(srcroot)test/src/%.c\n$(C_TESTLIB_INTEGRATION_OBJS): CPPFLAGS += -DJEMALLOC_INTEGRATION_TEST\n$(C_UTIL_INTEGRATION_OBJS): $(objroot)src/%.integration.$(O): $(srcroot)src/%.c\n$(C_TESTLIB_STRESS_OBJS): $(objroot)test/src/%.stress.$(O): $(srcroot)test/src/%.c\n$(C_TESTLIB_STRESS_OBJS): CPPFLAGS += -DJEMALLOC_STRESS_TEST -DJEMALLOC_STRESS_TESTLIB\n$(C_TESTLIB_OBJS): CPPFLAGS += -I$(srcroot)test/include -I$(objroot)test/include\n$(TESTS_UNIT_OBJS): CPPFLAGS += -DJEMALLOC_UNIT_TEST\n$(TESTS_INTEGRATION_OBJS): CPPFLAGS += -DJEMALLOC_INTEGRATION_TEST\n$(TESTS_INTEGRATION_CPP_OBJS): CPPFLAGS += -DJEMALLOC_INTEGRATION_CPP_TEST\n$(TESTS_STRESS_OBJS): CPPFLAGS += -DJEMALLOC_STRESS_TEST\n$(TESTS_OBJS): $(objroot)test/%.$(O): $(srcroot)test/%.c\n$(TESTS_CPP_OBJS): $(objroot)test/%.$(O): $(srcroot)test/%.cpp\n$(TESTS_OBJS): CPPFLAGS += -I$(srcroot)test/include -I$(objroot)test/include\n$(TESTS_CPP_OBJS): CPPFLAGS += -I$(srcroot)test/include -I$(objroot)test/include\nifneq ($(IMPORTLIB),$(SO))\n$(CPP_OBJS) $(C_SYM_OBJS) $(C_OBJS) $(C_JET_SYM_OBJS) $(C_JET_OBJS): CPPFLAGS += -DDLLEXPORT\nendif\n\n# Dependencies.\nifndef CC_MM\nHEADER_DIRS = $(srcroot)include/jemalloc/internal \\\n\t$(objroot)include/jemalloc $(objroot)include/jemalloc/internal\nHEADERS = $(filter-out $(PRIVATE_NAMESPACE_HDRS),$(wildcard $(foreach dir,$(HEADER_DIRS),$(dir)/*.h)))\n$(C_SYM_OBJS) $(C_OBJS) $(CPP_OBJS) $(C_PIC_OBJS) $(CPP_PIC_OBJS) $(C_JET_SYM_OBJS) $(C_JET_OBJS) $(C_TESTLIB_OBJS) $(TESTS_OBJS) $(TESTS_CPP_OBJS): $(HEADERS)\n$(TESTS_OBJS) $(TESTS_CPP_OBJS): $(objroot)test/include/test/jemalloc_test.h\nendif\n\n$(C_OBJS) $(CPP_OBJS) $(C_PIC_OBJS) $(CPP_PIC_OBJS) $(C_TESTLIB_INTEGRATION_OBJS) $(C_UTIL_INTEGRATION_OBJS) $(TESTS_INTEGRATION_OBJS) $(TESTS_INTEGRATION_CPP_OBJS): $(objroot)include/jemalloc/internal/private_namespace.h\n$(C_JET_OBJS) $(C_TESTLIB_UNIT_OBJS) $(C_TESTLIB_STRESS_OBJS) $(TESTS_UNIT_OBJS) $(TESTS_STRESS_OBJS): $(objroot)include/jemalloc/internal/private_namespace_jet.h\n\n$(C_SYM_OBJS) $(C_OBJS) $(C_PIC_OBJS) $(C_JET_SYM_OBJS) $(C_JET_OBJS) $(C_TESTLIB_OBJS) $(TESTS_OBJS): %.$(O):\n\t@mkdir -p $(@D)\n\t$(CC) $(CFLAGS) -c $(CPPFLAGS) $(CTARGET) $<\nifdef CC_MM\n\t@$(CC) -MM $(CPPFLAGS) -MT $@ -o $(@:%.$(O)=%.d) $<\nendif\n\n$(C_SYMS): %.sym:\n\t@mkdir -p $(@D)\n\t$(DUMP_SYMS) $< | $(AWK) -f $(objroot)include/jemalloc/internal/private_symbols.awk > $@\n\n$(C_JET_SYMS): %.sym:\n\t@mkdir -p $(@D)\n\t$(DUMP_SYMS) $< | $(AWK) -f $(objroot)include/jemalloc/internal/private_symbols_jet.awk > $@\n\n$(objroot)include/jemalloc/internal/private_namespace.gen.h: $(C_SYMS)\n\t$(SHELL) $(srcroot)include/jemalloc/internal/private_namespace.sh $^ > $@\n\n$(objroot)include/jemalloc/internal/private_namespace_jet.gen.h: $(C_JET_SYMS)\n\t$(SHELL) $(srcroot)include/jemalloc/internal/private_namespace.sh $^ > $@\n\n%.h: %.gen.h\n\t@if ! `cmp -s $< $@` ; then echo \"cp $< $<\"; cp $< $@ ; fi\n\n$(CPP_OBJS) $(CPP_PIC_OBJS) $(TESTS_CPP_OBJS): %.$(O):\n\t@mkdir -p $(@D)\n\t$(CXX) $(CXXFLAGS) -c $(CPPFLAGS) $(CTARGET) $<\nifdef CC_MM\n\t@$(CXX) -MM $(CPPFLAGS) -MT $@ -o $(@:%.$(O)=%.d) $<\nendif\n\nifneq ($(SOREV),$(SO))\n%.$(SO) : %.$(SOREV)\n\t@mkdir -p $(@D)\n\tln -sf $(<F) $@\nendif\n\n$(objroot)lib/$(LIBJEMALLOC).$(SOREV) : $(if $(PIC_CFLAGS),$(C_PIC_OBJS),$(C_OBJS)) $(if $(PIC_CFLAGS),$(CPP_PIC_OBJS),$(CPP_OBJS))\n\t@mkdir -p $(@D)\n\t$(CC) $(DSO_LDFLAGS) $(call RPATH,$(RPATH_EXTRA)) $(LDTARGET) $+ $(LDFLAGS) $(LIBS) $(EXTRA_LDFLAGS)\n\n$(objroot)lib/$(LIBJEMALLOC)_pic.$(A) : $(C_PIC_OBJS) $(CPP_PIC_OBJS)\n$(objroot)lib/$(LIBJEMALLOC).$(A) : $(C_OBJS) $(CPP_OBJS)\n$(objroot)lib/$(LIBJEMALLOC)_s.$(A) : $(C_OBJS) $(CPP_OBJS)\n\n$(STATIC_LIBS):\n\t@mkdir -p $(@D)\n\t$(AR) $(ARFLAGS)@AROUT@ $+\n\n$(objroot)test/unit/%$(EXE): $(objroot)test/unit/%.$(O) $(C_JET_OBJS) $(C_TESTLIB_UNIT_OBJS)\n\t@mkdir -p $(@D)\n\t$(CC) $(LDTARGET) $(filter %.$(O),$^) $(call RPATH,$(objroot)lib) $(LDFLAGS) $(filter-out -lm,$(LIBS)) $(LM) $(EXTRA_LDFLAGS)\n\n$(objroot)test/integration/%$(EXE): $(objroot)test/integration/%.$(O) $(C_TESTLIB_INTEGRATION_OBJS) $(C_UTIL_INTEGRATION_OBJS) $(objroot)lib/$(LIBJEMALLOC).$(IMPORTLIB)\n\t@mkdir -p $(@D)\n\t$(CC) $(TEST_LD_MODE) $(LDTARGET) $(filter %.$(O),$^) $(call RPATH,$(objroot)lib) $(LJEMALLOC) $(LDFLAGS) $(filter-out -lm,$(filter -lrt -pthread -lstdc++,$(LIBS))) $(LM) $(EXTRA_LDFLAGS)\n\n$(objroot)test/integration/cpp/%$(EXE): $(objroot)test/integration/cpp/%.$(O) $(C_TESTLIB_INTEGRATION_OBJS) $(C_UTIL_INTEGRATION_OBJS) $(objroot)lib/$(LIBJEMALLOC).$(IMPORTLIB)\n\t@mkdir -p $(@D)\n\t$(CXX) $(LDTARGET) $(filter %.$(O),$^) $(call RPATH,$(objroot)lib) $(objroot)lib/$(LIBJEMALLOC).$(IMPORTLIB) $(LDFLAGS) $(filter-out -lm,$(LIBS)) -lm $(EXTRA_LDFLAGS)\n\n$(objroot)test/stress/%$(EXE): $(objroot)test/stress/%.$(O) $(C_JET_OBJS) $(C_TESTLIB_STRESS_OBJS) $(objroot)lib/$(LIBJEMALLOC).$(IMPORTLIB)\n\t@mkdir -p $(@D)\n\t$(CC) $(TEST_LD_MODE) $(LDTARGET) $(filter %.$(O),$^) $(call RPATH,$(objroot)lib) $(objroot)lib/$(LIBJEMALLOC).$(IMPORTLIB) $(LDFLAGS) $(filter-out -lm,$(LIBS)) $(LM) $(EXTRA_LDFLAGS)\n\nbuild_lib_shared: $(DSOS)\nbuild_lib_static: $(STATIC_LIBS)\nifeq ($(enable_shared), 1)\nbuild_lib: build_lib_shared\nendif\nifeq ($(enable_static), 1)\nbuild_lib: build_lib_static\nendif\n\ninstall_bin:\n\t$(INSTALL) -d $(BINDIR)\n\t@for b in $(BINS); do \\\n\techo \"$(INSTALL) -m 755 $$b $(BINDIR)\"; \\\n\t$(INSTALL) -m 755 $$b $(BINDIR); \\\ndone\n\ninstall_include:\n\t$(INSTALL) -d $(INCLUDEDIR)/jemalloc\n\t@for h in $(C_HDRS); do \\\n\techo \"$(INSTALL) -m 644 $$h $(INCLUDEDIR)/jemalloc\"; \\\n\t$(INSTALL) -m 644 $$h $(INCLUDEDIR)/jemalloc; \\\ndone\n\ninstall_lib_shared: $(DSOS)\n\t$(INSTALL) -d $(LIBDIR)\n\t$(INSTALL) -m 755 $(objroot)lib/$(LIBJEMALLOC).$(SOREV) $(LIBDIR)\nifneq ($(SOREV),$(SO))\n\tln -sf $(LIBJEMALLOC).$(SOREV) $(LIBDIR)/$(LIBJEMALLOC).$(SO)\nendif\n\ninstall_lib_static: $(STATIC_LIBS)\n\t$(INSTALL) -d $(LIBDIR)\n\t@for l in $(STATIC_LIBS); do \\\n\techo \"$(INSTALL) -m 755 $$l $(LIBDIR)\"; \\\n\t$(INSTALL) -m 755 $$l $(LIBDIR); \\\ndone\n\ninstall_lib_pc: $(PC)\n\t$(INSTALL) -d $(LIBDIR)/pkgconfig\n\t@for l in $(PC); do \\\n\techo \"$(INSTALL) -m 644 $$l $(LIBDIR)/pkgconfig\"; \\\n\t$(INSTALL) -m 644 $$l $(LIBDIR)/pkgconfig; \\\ndone\n\nifeq ($(enable_shared), 1)\ninstall_lib: install_lib_shared\nendif\nifeq ($(enable_static), 1)\ninstall_lib: install_lib_static\nendif\ninstall_lib: install_lib_pc\n\ninstall_doc_html:\n\t$(INSTALL) -d $(DATADIR)/doc/jemalloc$(install_suffix)\n\t@for d in $(DOCS_HTML); do \\\n\techo \"$(INSTALL) -m 644 $$d $(DATADIR)/doc/jemalloc$(install_suffix)\"; \\\n\t$(INSTALL) -m 644 $$d $(DATADIR)/doc/jemalloc$(install_suffix); \\\ndone\n\ninstall_doc_man:\n\t$(INSTALL) -d $(MANDIR)/man3\n\t@for d in $(DOCS_MAN3); do \\\n\techo \"$(INSTALL) -m 644 $$d $(MANDIR)/man3\"; \\\n\t$(INSTALL) -m 644 $$d $(MANDIR)/man3; \\\ndone\n\ninstall_doc: build_doc install_doc_html install_doc_man\n\ninstall: install_bin install_include install_lib\n\nifeq ($(enable_doc), 1)\ninstall: install_doc\nendif\n\ntests_unit: $(TESTS_UNIT:$(srcroot)%.c=$(objroot)%$(EXE))\ntests_integration: $(TESTS_INTEGRATION:$(srcroot)%.c=$(objroot)%$(EXE)) $(TESTS_INTEGRATION_CPP:$(srcroot)%.cpp=$(objroot)%$(EXE))\ntests_stress: $(TESTS_STRESS:$(srcroot)%.c=$(objroot)%$(EXE))\ntests: tests_unit tests_integration tests_stress\n\ncheck_unit_dir:\n\t@mkdir -p $(objroot)test/unit\ncheck_integration_dir:\n\t@mkdir -p $(objroot)test/integration\nstress_dir:\n\t@mkdir -p $(objroot)test/stress\ncheck_dir: check_unit_dir check_integration_dir\n\ncheck_unit: tests_unit check_unit_dir\n\t$(SHELL) $(objroot)test/test.sh $(TESTS_UNIT:$(srcroot)%.c=$(objroot)%)\ncheck_integration_prof: tests_integration check_integration_dir\nifeq ($(enable_prof), 1)\n\t$(MALLOC_CONF)=\"prof:true\" $(SHELL) $(objroot)test/test.sh $(TESTS_INTEGRATION:$(srcroot)%.c=$(objroot)%) $(TESTS_INTEGRATION_CPP:$(srcroot)%.cpp=$(objroot)%)\n\t$(MALLOC_CONF)=\"prof:true,prof_active:false\" $(SHELL) $(objroot)test/test.sh $(TESTS_INTEGRATION:$(srcroot)%.c=$(objroot)%) $(TESTS_INTEGRATION_CPP:$(srcroot)%.cpp=$(objroot)%)\nendif\ncheck_integration_decay: tests_integration check_integration_dir\n\t$(MALLOC_CONF)=\"dirty_decay_ms:-1,muzzy_decay_ms:-1\" $(SHELL) $(objroot)test/test.sh $(TESTS_INTEGRATION:$(srcroot)%.c=$(objroot)%) $(TESTS_INTEGRATION_CPP:$(srcroot)%.cpp=$(objroot)%)\n\t$(MALLOC_CONF)=\"dirty_decay_ms:0,muzzy_decay_ms:0\" $(SHELL) $(objroot)test/test.sh $(TESTS_INTEGRATION:$(srcroot)%.c=$(objroot)%) $(TESTS_INTEGRATION_CPP:$(srcroot)%.cpp=$(objroot)%)\ncheck_integration: tests_integration check_integration_dir\n\t$(SHELL) $(objroot)test/test.sh $(TESTS_INTEGRATION:$(srcroot)%.c=$(objroot)%) $(TESTS_INTEGRATION_CPP:$(srcroot)%.cpp=$(objroot)%)\nstress: tests_stress stress_dir\n\t$(SHELL) $(objroot)test/test.sh $(TESTS_STRESS:$(srcroot)%.c=$(objroot)%)\ncheck: check_unit check_integration check_integration_decay check_integration_prof\n\nclean:\n\trm -f $(PRIVATE_NAMESPACE_HDRS)\n\trm -f $(PRIVATE_NAMESPACE_GEN_HDRS)\n\trm -f $(C_SYM_OBJS)\n\trm -f $(C_SYMS)\n\trm -f $(C_OBJS)\n\trm -f $(CPP_OBJS)\n\trm -f $(C_PIC_OBJS)\n\trm -f $(CPP_PIC_OBJS)\n\trm -f $(C_JET_SYM_OBJS)\n\trm -f $(C_JET_SYMS)\n\trm -f $(C_JET_OBJS)\n\trm -f $(C_TESTLIB_OBJS)\n\trm -f $(C_SYM_OBJS:%.$(O)=%.d)\n\trm -f $(C_OBJS:%.$(O)=%.d)\n\trm -f $(CPP_OBJS:%.$(O)=%.d)\n\trm -f $(C_PIC_OBJS:%.$(O)=%.d)\n\trm -f $(CPP_PIC_OBJS:%.$(O)=%.d)\n\trm -f $(C_JET_SYM_OBJS:%.$(O)=%.d)\n\trm -f $(C_JET_OBJS:%.$(O)=%.d)\n\trm -f $(C_TESTLIB_OBJS:%.$(O)=%.d)\n\trm -f $(TESTS_OBJS:%.$(O)=%$(EXE))\n\trm -f $(TESTS_OBJS)\n\trm -f $(TESTS_OBJS:%.$(O)=%.d)\n\trm -f $(TESTS_OBJS:%.$(O)=%.out)\n\trm -f $(TESTS_CPP_OBJS:%.$(O)=%$(EXE))\n\trm -f $(TESTS_CPP_OBJS)\n\trm -f $(TESTS_CPP_OBJS:%.$(O)=%.d)\n\trm -f $(TESTS_CPP_OBJS:%.$(O)=%.out)\n\trm -f $(DSOS) $(STATIC_LIBS)\n\ndistclean: clean\n\trm -f $(objroot)bin/jemalloc-config\n\trm -f $(objroot)bin/jemalloc.sh\n\trm -f $(objroot)bin/jeprof\n\trm -f $(objroot)config.log\n\trm -f $(objroot)config.status\n\trm -f $(objroot)config.stamp\n\trm -f $(cfghdrs_out)\n\trm -f $(cfgoutputs_out)\n\nrelclean: distclean\n\trm -f $(objroot)configure\n\trm -f $(objroot)VERSION\n\trm -f $(DOCS_HTML)\n\trm -f $(DOCS_MAN3)\n\n#===============================================================================\n# Re-configuration rules.\n\nifeq ($(enable_autogen), 1)\n$(srcroot)configure : $(srcroot)configure.ac\n\tcd ./$(srcroot) && $(AUTOCONF)\n\n$(objroot)config.status : $(srcroot)configure\n\t./$(objroot)config.status --recheck\n\n$(srcroot)config.stamp.in : $(srcroot)configure.ac\n\techo stamp > $(srcroot)config.stamp.in\n\n$(objroot)config.stamp : $(cfgoutputs_in) $(cfghdrs_in) $(srcroot)configure\n\t./$(objroot)config.status\n\t@touch $@\n\n# There must be some action in order for make to re-read Makefile when it is\n# out of date.\n$(cfgoutputs_out) $(cfghdrs_out) : $(objroot)config.stamp\n\t@true\nendif\n"
  },
  {
    "path": "deps/jemalloc/README",
    "content": "jemalloc is a general purpose malloc(3) implementation that emphasizes\nfragmentation avoidance and scalable concurrency support.  jemalloc first came\ninto use as the FreeBSD libc allocator in 2005, and since then it has found its\nway into numerous applications that rely on its predictable behavior.  In 2010\njemalloc development efforts broadened to include developer support features\nsuch as heap profiling and extensive monitoring/tuning hooks.  Modern jemalloc\nreleases continue to be integrated back into FreeBSD, and therefore versatility\nremains critical.  Ongoing development efforts trend toward making jemalloc\namong the best allocators for a broad range of demanding applications, and\neliminating/mitigating weaknesses that have practical repercussions for real\nworld applications.\n\nThe COPYING file contains copyright and licensing information.\n\nThe INSTALL file contains information on how to configure, build, and install\njemalloc.\n\nThe ChangeLog file contains a brief summary of changes for each release.\n\nURL: http://jemalloc.net/\n"
  },
  {
    "path": "deps/jemalloc/TUNING.md",
    "content": "This document summarizes the common approaches for performance fine tuning with\njemalloc (as of 5.1.0).  The default configuration of jemalloc tends to work\nreasonably well in practice, and most applications should not have to tune any\noptions. However, in order to cover a wide range of applications and avoid\npathological cases, the default setting is sometimes kept conservative and\nsuboptimal, even for many common workloads.  When jemalloc is properly tuned for\na specific application / workload, it is common to improve system level metrics\nby a few percent, or make favorable trade-offs.\n\n\n## Notable runtime options for performance tuning\n\nRuntime options can be set via\n[malloc_conf](http://jemalloc.net/jemalloc.3.html#tuning).\n\n* [background_thread](http://jemalloc.net/jemalloc.3.html#background_thread)\n\n    Enabling jemalloc background threads generally improves the tail latency for\n    application threads, since unused memory purging is shifted to the dedicated\n    background threads.  In addition, unintended purging delay caused by\n    application inactivity is avoided with background threads.\n\n    Suggested: `background_thread:true` when jemalloc managed threads can be\n    allowed.\n\n* [metadata_thp](http://jemalloc.net/jemalloc.3.html#opt.metadata_thp)\n\n    Allowing jemalloc to utilize transparent huge pages for its internal\n    metadata usually reduces TLB misses significantly, especially for programs\n    with large memory footprint and frequent allocation / deallocation\n    activities.  Metadata memory usage may increase due to the use of huge\n    pages.\n\n    Suggested for allocation intensive programs: `metadata_thp:auto` or\n    `metadata_thp:always`, which is expected to improve CPU utilization at a\n    small memory cost.\n\n* [dirty_decay_ms](http://jemalloc.net/jemalloc.3.html#opt.dirty_decay_ms) and\n  [muzzy_decay_ms](http://jemalloc.net/jemalloc.3.html#opt.muzzy_decay_ms)\n\n    Decay time determines how fast jemalloc returns unused pages back to the\n    operating system, and therefore provides a fairly straightforward trade-off\n    between CPU and memory usage.  Shorter decay time purges unused pages faster\n    to reduces memory usage (usually at the cost of more CPU cycles spent on\n    purging), and vice versa.\n\n    Suggested: tune the values based on the desired trade-offs.\n\n* [narenas](http://jemalloc.net/jemalloc.3.html#opt.narenas)\n\n    By default jemalloc uses multiple arenas to reduce internal lock contention.\n    However high arena count may also increase overall memory fragmentation,\n    since arenas manage memory independently.  When high degree of parallelism\n    is not expected at the allocator level, lower number of arenas often\n    improves memory usage.\n\n    Suggested: if low parallelism is expected, try lower arena count while\n    monitoring CPU and memory usage.\n\n* [percpu_arena](http://jemalloc.net/jemalloc.3.html#opt.percpu_arena)\n\n    Enable dynamic thread to arena association based on running CPU.  This has\n    the potential to improve locality, e.g. when thread to CPU affinity is\n    present.\n    \n    Suggested: try `percpu_arena:percpu` or `percpu_arena:phycpu` if\n    thread migration between processors is expected to be infrequent.\n\nExamples:\n\n* High resource consumption application, prioritizing CPU utilization:\n\n    `background_thread:true,metadata_thp:auto` combined with relaxed decay time\n    (increased `dirty_decay_ms` and / or `muzzy_decay_ms`,\n    e.g. `dirty_decay_ms:30000,muzzy_decay_ms:30000`).\n\n* High resource consumption application, prioritizing memory usage:\n\n    `background_thread:true` combined with shorter decay time (decreased\n    `dirty_decay_ms` and / or `muzzy_decay_ms`,\n    e.g. `dirty_decay_ms:5000,muzzy_decay_ms:5000`), and lower arena count\n    (e.g. number of CPUs).\n\n* Low resource consumption application:\n\n    `narenas:1,lg_tcache_max:13` combined with shorter decay time (decreased\n    `dirty_decay_ms` and / or `muzzy_decay_ms`,e.g.\n    `dirty_decay_ms:1000,muzzy_decay_ms:0`).\n\n* Extremely conservative -- minimize memory usage at all costs, only suitable when\nallocation activity is very rare:\n\n    `narenas:1,tcache:false,dirty_decay_ms:0,muzzy_decay_ms:0`\n\nNote that it is recommended to combine the options with `abort_conf:true` which\naborts immediately on illegal options.\n\n## Beyond runtime options\n\nIn addition to the runtime options, there are a number of programmatic ways to\nimprove application performance with jemalloc.\n\n* [Explicit arenas](http://jemalloc.net/jemalloc.3.html#arenas.create)\n\n    Manually created arenas can help performance in various ways, e.g. by\n    managing locality and contention for specific usages.  For example,\n    applications can explicitly allocate frequently accessed objects from a\n    dedicated arena with\n    [mallocx()](http://jemalloc.net/jemalloc.3.html#MALLOCX_ARENA) to improve\n    locality.  In addition, explicit arenas often benefit from individually\n    tuned options, e.g. relaxed [decay\n    time](http://jemalloc.net/jemalloc.3.html#arena.i.dirty_decay_ms) if\n    frequent reuse is expected.\n\n* [Extent hooks](http://jemalloc.net/jemalloc.3.html#arena.i.extent_hooks)\n\n    Extent hooks allow customization for managing underlying memory.  One use\n    case for performance purpose is to utilize huge pages -- for example,\n    [HHVM](https://github.com/facebook/hhvm/blob/master/hphp/util/alloc.cpp)\n    uses explicit arenas with customized extent hooks to manage 1GB huge pages\n    for frequently accessed data, which reduces TLB misses significantly.\n\n* [Explicit thread-to-arena\n  binding](http://jemalloc.net/jemalloc.3.html#thread.arena)\n\n    It is common for some threads in an application to have different memory\n    access / allocation patterns.  Threads with heavy workloads often benefit\n    from explicit binding, e.g. binding very active threads to dedicated arenas\n    may reduce contention at the allocator level.\n"
  },
  {
    "path": "deps/jemalloc/VERSION",
    "content": "5.2.1-0-g0\n"
  },
  {
    "path": "deps/jemalloc/autogen.sh",
    "content": "#!/bin/sh\n\nfor i in autoconf; do\n    echo \"$i\"\n    $i\n    if [ $? -ne 0 ]; then\n\techo \"Error $? in $i\"\n\texit 1\n    fi\ndone\n\necho \"./configure --enable-autogen $@\"\n./configure --enable-autogen $@\nif [ $? -ne 0 ]; then\n    echo \"Error $? in ./configure\"\n    exit 1\nfi\n"
  },
  {
    "path": "deps/jemalloc/bin/jemalloc-config.in",
    "content": "#!/bin/sh\n\nusage() {\n\tcat <<EOF\nUsage:\n  @BINDIR@/jemalloc-config <option>\nOptions:\n  --help | -h  : Print usage.\n  --version    : Print jemalloc version.\n  --revision   : Print shared library revision number.\n  --config     : Print configure options used to build jemalloc.\n  --prefix     : Print installation directory prefix.\n  --bindir     : Print binary installation directory.\n  --datadir    : Print data installation directory.\n  --includedir : Print include installation directory.\n  --libdir     : Print library installation directory.\n  --mandir     : Print manual page installation directory.\n  --cc         : Print compiler used to build jemalloc.\n  --cflags     : Print compiler flags used to build jemalloc.\n  --cppflags   : Print preprocessor flags used to build jemalloc.\n  --cxxflags   : Print C++ compiler flags used to build jemalloc.\n  --ldflags    : Print library flags used to build jemalloc.\n  --libs       : Print libraries jemalloc was linked against.\nEOF\n}\n\nprefix=\"@prefix@\"\nexec_prefix=\"@exec_prefix@\"\n\ncase \"$1\" in\n--help | -h)\n\tusage\n\texit 0\n\t;;\n--version)\n\techo \"@jemalloc_version@\"\n\t;;\n--revision)\n\techo \"@rev@\"\n\t;;\n--config)\n\techo \"@CONFIG@\"\n\t;;\n--prefix)\n\techo \"@PREFIX@\"\n\t;;\n--bindir)\n\techo \"@BINDIR@\"\n\t;;\n--datadir)\n\techo \"@DATADIR@\"\n\t;;\n--includedir)\n\techo \"@INCLUDEDIR@\"\n\t;;\n--libdir)\n\techo \"@LIBDIR@\"\n\t;;\n--mandir)\n\techo \"@MANDIR@\"\n\t;;\n--cc)\n\techo \"@CC@\"\n\t;;\n--cflags)\n\techo \"@CFLAGS@\"\n\t;;\n--cppflags)\n\techo \"@CPPFLAGS@\"\n\t;;\n--cxxflags)\n\techo \"@CXXFLAGS@\"\n\t;;\n--ldflags)\n\techo \"@LDFLAGS@ @EXTRA_LDFLAGS@\"\n\t;;\n--libs)\n\techo \"@LIBS@\"\n\t;;\n*)\n\tusage\n\texit 1\nesac\n"
  },
  {
    "path": "deps/jemalloc/bin/jemalloc.sh.in",
    "content": "#!/bin/sh\n\nprefix=@prefix@\nexec_prefix=@exec_prefix@\nlibdir=@libdir@\n\n@LD_PRELOAD_VAR@=${libdir}/libjemalloc.@SOREV@\nexport @LD_PRELOAD_VAR@\nexec \"$@\"\n"
  },
  {
    "path": "deps/jemalloc/bin/jeprof.in",
    "content": "#! /usr/bin/env perl\n\n# Copyright (c) 1998-2007, Google Inc.\n# All rights reserved.\n#\n# Redistribution and use in source and binary forms, with or without\n# modification, are permitted provided that the following conditions are\n# met:\n#\n#     * Redistributions of source code must retain the above copyright\n# notice, this list of conditions and the following disclaimer.\n#     * Redistributions in binary form must reproduce the above\n# copyright notice, this list of conditions and the following disclaimer\n# in the documentation and/or other materials provided with the\n# distribution.\n#     * Neither the name of Google Inc. nor the names of its\n# contributors may be used to endorse or promote products derived from\n# this software without specific prior written permission.\n#\n# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n# \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\n# ---\n# Program for printing the profile generated by common/profiler.cc,\n# or by the heap profiler (common/debugallocation.cc)\n#\n# The profile contains a sequence of entries of the form:\n#       <count> <stack trace>\n# This program parses the profile, and generates user-readable\n# output.\n#\n# Examples:\n#\n# % tools/jeprof \"program\" \"profile\"\n#   Enters \"interactive\" mode\n#\n# % tools/jeprof --text \"program\" \"profile\"\n#   Generates one line per procedure\n#\n# % tools/jeprof --gv \"program\" \"profile\"\n#   Generates annotated call-graph and displays via \"gv\"\n#\n# % tools/jeprof --gv --focus=Mutex \"program\" \"profile\"\n#   Restrict to code paths that involve an entry that matches \"Mutex\"\n#\n# % tools/jeprof --gv --focus=Mutex --ignore=string \"program\" \"profile\"\n#   Restrict to code paths that involve an entry that matches \"Mutex\"\n#   and does not match \"string\"\n#\n# % tools/jeprof --list=IBF_CheckDocid \"program\" \"profile\"\n#   Generates disassembly listing of all routines with at least one\n#   sample that match the --list=<regexp> pattern.  The listing is\n#   annotated with the flat and cumulative sample counts at each line.\n#\n# % tools/jeprof --disasm=IBF_CheckDocid \"program\" \"profile\"\n#   Generates disassembly listing of all routines with at least one\n#   sample that match the --disasm=<regexp> pattern.  The listing is\n#   annotated with the flat and cumulative sample counts at each PC value.\n#\n# TODO: Use color to indicate files?\n\nuse strict;\nuse warnings;\nuse Getopt::Long;\nuse Cwd;\n\nmy $JEPROF_VERSION = \"@jemalloc_version@\";\nmy $PPROF_VERSION = \"2.0\";\n\n# These are the object tools we use which can come from a\n# user-specified location using --tools, from the JEPROF_TOOLS\n# environment variable, or from the environment.\nmy %obj_tool_map = (\n  \"objdump\" => \"objdump\",\n  \"nm\" => \"nm\",\n  \"addr2line\" => \"addr2line\",\n  \"c++filt\" => \"c++filt\",\n  ## ConfigureObjTools may add architecture-specific entries:\n  #\"nm_pdb\" => \"nm-pdb\",       # for reading windows (PDB-format) executables\n  #\"addr2line_pdb\" => \"addr2line-pdb\",                                # ditto\n  #\"otool\" => \"otool\",         # equivalent of objdump on OS X\n);\n# NOTE: these are lists, so you can put in commandline flags if you want.\nmy @DOT = (\"dot\");          # leave non-absolute, since it may be in /usr/local\nmy @GV = (\"gv\");\nmy @EVINCE = (\"evince\");    # could also be xpdf or perhaps acroread\nmy @KCACHEGRIND = (\"kcachegrind\");\nmy @PS2PDF = (\"ps2pdf\");\n# These are used for dynamic profiles\nmy @URL_FETCHER = (\"curl\", \"-s\", \"--fail\");\n\n# These are the web pages that servers need to support for dynamic profiles\nmy $HEAP_PAGE = \"/pprof/heap\";\nmy $PROFILE_PAGE = \"/pprof/profile\";   # must support cgi-param \"?seconds=#\"\nmy $PMUPROFILE_PAGE = \"/pprof/pmuprofile(?:\\\\?.*)?\"; # must support cgi-param\n                                                # ?seconds=#&event=x&period=n\nmy $GROWTH_PAGE = \"/pprof/growth\";\nmy $CONTENTION_PAGE = \"/pprof/contention\";\nmy $WALL_PAGE = \"/pprof/wall(?:\\\\?.*)?\";  # accepts options like namefilter\nmy $FILTEREDPROFILE_PAGE = \"/pprof/filteredprofile(?:\\\\?.*)?\";\nmy $CENSUSPROFILE_PAGE = \"/pprof/censusprofile(?:\\\\?.*)?\"; # must support cgi-param\n                                                       # \"?seconds=#\",\n                                                       # \"?tags_regexp=#\" and\n                                                       # \"?type=#\".\nmy $SYMBOL_PAGE = \"/pprof/symbol\";     # must support symbol lookup via POST\nmy $PROGRAM_NAME_PAGE = \"/pprof/cmdline\";\n\n# These are the web pages that can be named on the command line.\n# All the alternatives must begin with /.\nmy $PROFILES = \"($HEAP_PAGE|$PROFILE_PAGE|$PMUPROFILE_PAGE|\" .\n               \"$GROWTH_PAGE|$CONTENTION_PAGE|$WALL_PAGE|\" .\n               \"$FILTEREDPROFILE_PAGE|$CENSUSPROFILE_PAGE)\";\n\n# default binary name\nmy $UNKNOWN_BINARY = \"(unknown)\";\n\n# There is a pervasive dependency on the length (in hex characters,\n# i.e., nibbles) of an address, distinguishing between 32-bit and\n# 64-bit profiles.  To err on the safe size, default to 64-bit here:\nmy $address_length = 16;\n\nmy $dev_null = \"/dev/null\";\nif (! -e $dev_null && $^O =~ /MSWin/) {    # $^O is the OS perl was built for\n  $dev_null = \"nul\";\n}\n\n# A list of paths to search for shared object files\nmy @prefix_list = ();\n\n# Special routine name that should not have any symbols.\n# Used as separator to parse \"addr2line -i\" output.\nmy $sep_symbol = '_fini';\nmy $sep_address = undef;\n\n##### Argument parsing #####\n\nsub usage_string {\n  return <<EOF;\nUsage:\njeprof [options] <program> <profiles>\n   <profiles> is a space separated list of profile names.\njeprof [options] <symbolized-profiles>\n   <symbolized-profiles> is a list of profile files where each file contains\n   the necessary symbol mappings  as well as profile data (likely generated\n   with --raw).\njeprof [options] <profile>\n   <profile> is a remote form.  Symbols are obtained from host:port$SYMBOL_PAGE\n\n   Each name can be:\n   /path/to/profile        - a path to a profile file\n   host:port[/<service>]   - a location of a service to get profile from\n\n   The /<service> can be $HEAP_PAGE, $PROFILE_PAGE, /pprof/pmuprofile,\n                         $GROWTH_PAGE, $CONTENTION_PAGE, /pprof/wall,\n                         $CENSUSPROFILE_PAGE, or /pprof/filteredprofile.\n   For instance:\n     jeprof http://myserver.com:80$HEAP_PAGE\n   If /<service> is omitted, the service defaults to $PROFILE_PAGE (cpu profiling).\njeprof --symbols <program>\n   Maps addresses to symbol names.  In this mode, stdin should be a\n   list of library mappings, in the same format as is found in the heap-\n   and cpu-profile files (this loosely matches that of /proc/self/maps\n   on linux), followed by a list of hex addresses to map, one per line.\n\n   For more help with querying remote servers, including how to add the\n   necessary server-side support code, see this filename (or one like it):\n\n   /usr/doc/gperftools-$PPROF_VERSION/pprof_remote_servers.html\n\nOptions:\n   --cum               Sort by cumulative data\n   --base=<base>       Subtract <base> from <profile> before display\n   --interactive       Run in interactive mode (interactive \"help\" gives help) [default]\n   --seconds=<n>       Length of time for dynamic profiles [default=30 secs]\n   --add_lib=<file>    Read additional symbols and line info from the given library\n   --lib_prefix=<dir>  Comma separated list of library path prefixes\n\nReporting Granularity:\n   --addresses         Report at address level\n   --lines             Report at source line level\n   --functions         Report at function level [default]\n   --files             Report at source file level\n\nOutput type:\n   --text              Generate text report\n   --callgrind         Generate callgrind format to stdout\n   --gv                Generate Postscript and display\n   --evince            Generate PDF and display\n   --web               Generate SVG and display\n   --list=<regexp>     Generate source listing of matching routines\n   --disasm=<regexp>   Generate disassembly of matching routines\n   --symbols           Print demangled symbol names found at given addresses\n   --dot               Generate DOT file to stdout\n   --ps                Generate Postcript to stdout\n   --pdf               Generate PDF to stdout\n   --svg               Generate SVG to stdout\n   --gif               Generate GIF to stdout\n   --raw               Generate symbolized jeprof data (useful with remote fetch)\n\nHeap-Profile Options:\n   --inuse_space       Display in-use (mega)bytes [default]\n   --inuse_objects     Display in-use objects\n   --alloc_space       Display allocated (mega)bytes\n   --alloc_objects     Display allocated objects\n   --show_bytes        Display space in bytes\n   --drop_negative     Ignore negative differences\n\nContention-profile options:\n   --total_delay       Display total delay at each region [default]\n   --contentions       Display number of delays at each region\n   --mean_delay        Display mean delay at each region\n\nCall-graph Options:\n   --nodecount=<n>     Show at most so many nodes [default=80]\n   --nodefraction=<f>  Hide nodes below <f>*total [default=.005]\n   --edgefraction=<f>  Hide edges below <f>*total [default=.001]\n   --maxdegree=<n>     Max incoming/outgoing edges per node [default=8]\n   --focus=<regexp>    Focus on backtraces with nodes matching <regexp>\n   --thread=<n>        Show profile for thread <n>\n   --ignore=<regexp>   Ignore backtraces with nodes matching <regexp>\n   --scale=<n>         Set GV scaling [default=0]\n   --heapcheck         Make nodes with non-0 object counts\n                       (i.e. direct leak generators) more visible\n   --retain=<regexp>   Retain only nodes that match <regexp>\n   --exclude=<regexp>  Exclude all nodes that match <regexp>\n\nMiscellaneous:\n   --tools=<prefix or binary:fullpath>[,...]   \\$PATH for object tool pathnames\n   --test              Run unit tests\n   --help              This message\n   --version           Version information\n\nEnvironment Variables:\n   JEPROF_TMPDIR        Profiles directory. Defaults to \\$HOME/jeprof\n   JEPROF_TOOLS         Prefix for object tools pathnames\n\nExamples:\n\njeprof /bin/ls ls.prof\n                       Enters \"interactive\" mode\njeprof --text /bin/ls ls.prof\n                       Outputs one line per procedure\njeprof --web /bin/ls ls.prof\n                       Displays annotated call-graph in web browser\njeprof --gv /bin/ls ls.prof\n                       Displays annotated call-graph via 'gv'\njeprof --gv --focus=Mutex /bin/ls ls.prof\n                       Restricts to code paths including a .*Mutex.* entry\njeprof --gv --focus=Mutex --ignore=string /bin/ls ls.prof\n                       Code paths including Mutex but not string\njeprof --list=getdir /bin/ls ls.prof\n                       (Per-line) annotated source listing for getdir()\njeprof --disasm=getdir /bin/ls ls.prof\n                       (Per-PC) annotated disassembly for getdir()\n\njeprof http://localhost:1234/\n                       Enters \"interactive\" mode\njeprof --text localhost:1234\n                       Outputs one line per procedure for localhost:1234\njeprof --raw localhost:1234 > ./local.raw\njeprof --text ./local.raw\n                       Fetches a remote profile for later analysis and then\n                       analyzes it in text mode.\nEOF\n}\n\nsub version_string {\n  return <<EOF\njeprof (part of jemalloc $JEPROF_VERSION)\nbased on pprof (part of gperftools $PPROF_VERSION)\n\nCopyright 1998-2007 Google Inc.\n\nThis is BSD licensed software; see the source for copying conditions\nand license information.\nThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\nPARTICULAR PURPOSE.\nEOF\n}\n\nsub usage {\n  my $msg = shift;\n  print STDERR \"$msg\\n\\n\";\n  print STDERR usage_string();\n  print STDERR \"\\nFATAL ERROR: $msg\\n\";    # just as a reminder\n  exit(1);\n}\n\nsub Init() {\n  # Setup tmp-file name and handler to clean it up.\n  # We do this in the very beginning so that we can use\n  # error() and cleanup() function anytime here after.\n  $main::tmpfile_sym = \"/tmp/jeprof$$.sym\";\n  $main::tmpfile_ps = \"/tmp/jeprof$$\";\n  $main::next_tmpfile = 0;\n  $SIG{'INT'} = \\&sighandler;\n\n  # Cache from filename/linenumber to source code\n  $main::source_cache = ();\n\n  $main::opt_help = 0;\n  $main::opt_version = 0;\n\n  $main::opt_cum = 0;\n  $main::opt_base = '';\n  $main::opt_addresses = 0;\n  $main::opt_lines = 0;\n  $main::opt_functions = 0;\n  $main::opt_files = 0;\n  $main::opt_lib_prefix = \"\";\n\n  $main::opt_text = 0;\n  $main::opt_callgrind = 0;\n  $main::opt_list = \"\";\n  $main::opt_disasm = \"\";\n  $main::opt_symbols = 0;\n  $main::opt_gv = 0;\n  $main::opt_evince = 0;\n  $main::opt_web = 0;\n  $main::opt_dot = 0;\n  $main::opt_ps = 0;\n  $main::opt_pdf = 0;\n  $main::opt_gif = 0;\n  $main::opt_svg = 0;\n  $main::opt_raw = 0;\n\n  $main::opt_nodecount = 80;\n  $main::opt_nodefraction = 0.005;\n  $main::opt_edgefraction = 0.001;\n  $main::opt_maxdegree = 8;\n  $main::opt_focus = '';\n  $main::opt_thread = undef;\n  $main::opt_ignore = '';\n  $main::opt_scale = 0;\n  $main::opt_heapcheck = 0;\n  $main::opt_retain = '';\n  $main::opt_exclude = '';\n  $main::opt_seconds = 30;\n  $main::opt_lib = \"\";\n\n  $main::opt_inuse_space   = 0;\n  $main::opt_inuse_objects = 0;\n  $main::opt_alloc_space   = 0;\n  $main::opt_alloc_objects = 0;\n  $main::opt_show_bytes    = 0;\n  $main::opt_drop_negative = 0;\n  $main::opt_interactive   = 0;\n\n  $main::opt_total_delay = 0;\n  $main::opt_contentions = 0;\n  $main::opt_mean_delay = 0;\n\n  $main::opt_tools   = \"\";\n  $main::opt_debug   = 0;\n  $main::opt_test    = 0;\n\n  # These are undocumented flags used only by unittests.\n  $main::opt_test_stride = 0;\n\n  # Are we using $SYMBOL_PAGE?\n  $main::use_symbol_page = 0;\n\n  # Files returned by TempName.\n  %main::tempnames = ();\n\n  # Type of profile we are dealing with\n  # Supported types:\n  #     cpu\n  #     heap\n  #     growth\n  #     contention\n  $main::profile_type = '';     # Empty type means \"unknown\"\n\n  GetOptions(\"help!\"          => \\$main::opt_help,\n             \"version!\"       => \\$main::opt_version,\n             \"cum!\"           => \\$main::opt_cum,\n             \"base=s\"         => \\$main::opt_base,\n             \"seconds=i\"      => \\$main::opt_seconds,\n             \"add_lib=s\"      => \\$main::opt_lib,\n             \"lib_prefix=s\"   => \\$main::opt_lib_prefix,\n             \"functions!\"     => \\$main::opt_functions,\n             \"lines!\"         => \\$main::opt_lines,\n             \"addresses!\"     => \\$main::opt_addresses,\n             \"files!\"         => \\$main::opt_files,\n             \"text!\"          => \\$main::opt_text,\n             \"callgrind!\"     => \\$main::opt_callgrind,\n             \"list=s\"         => \\$main::opt_list,\n             \"disasm=s\"       => \\$main::opt_disasm,\n             \"symbols!\"       => \\$main::opt_symbols,\n             \"gv!\"            => \\$main::opt_gv,\n             \"evince!\"        => \\$main::opt_evince,\n             \"web!\"           => \\$main::opt_web,\n             \"dot!\"           => \\$main::opt_dot,\n             \"ps!\"            => \\$main::opt_ps,\n             \"pdf!\"           => \\$main::opt_pdf,\n             \"svg!\"           => \\$main::opt_svg,\n             \"gif!\"           => \\$main::opt_gif,\n             \"raw!\"           => \\$main::opt_raw,\n             \"interactive!\"   => \\$main::opt_interactive,\n             \"nodecount=i\"    => \\$main::opt_nodecount,\n             \"nodefraction=f\" => \\$main::opt_nodefraction,\n             \"edgefraction=f\" => \\$main::opt_edgefraction,\n             \"maxdegree=i\"    => \\$main::opt_maxdegree,\n             \"focus=s\"        => \\$main::opt_focus,\n             \"thread=s\"       => \\$main::opt_thread,\n             \"ignore=s\"       => \\$main::opt_ignore,\n             \"scale=i\"        => \\$main::opt_scale,\n             \"heapcheck\"      => \\$main::opt_heapcheck,\n             \"retain=s\"       => \\$main::opt_retain,\n             \"exclude=s\"      => \\$main::opt_exclude,\n             \"inuse_space!\"   => \\$main::opt_inuse_space,\n             \"inuse_objects!\" => \\$main::opt_inuse_objects,\n             \"alloc_space!\"   => \\$main::opt_alloc_space,\n             \"alloc_objects!\" => \\$main::opt_alloc_objects,\n             \"show_bytes!\"    => \\$main::opt_show_bytes,\n             \"drop_negative!\" => \\$main::opt_drop_negative,\n             \"total_delay!\"   => \\$main::opt_total_delay,\n             \"contentions!\"   => \\$main::opt_contentions,\n             \"mean_delay!\"    => \\$main::opt_mean_delay,\n             \"tools=s\"        => \\$main::opt_tools,\n             \"test!\"          => \\$main::opt_test,\n             \"debug!\"         => \\$main::opt_debug,\n             # Undocumented flags used only by unittests:\n             \"test_stride=i\"  => \\$main::opt_test_stride,\n      ) || usage(\"Invalid option(s)\");\n\n  # Deal with the standard --help and --version\n  if ($main::opt_help) {\n    print usage_string();\n    exit(0);\n  }\n\n  if ($main::opt_version) {\n    print version_string();\n    exit(0);\n  }\n\n  # Disassembly/listing/symbols mode requires address-level info\n  if ($main::opt_disasm || $main::opt_list || $main::opt_symbols) {\n    $main::opt_functions = 0;\n    $main::opt_lines = 0;\n    $main::opt_addresses = 1;\n    $main::opt_files = 0;\n  }\n\n  # Check heap-profiling flags\n  if ($main::opt_inuse_space +\n      $main::opt_inuse_objects +\n      $main::opt_alloc_space +\n      $main::opt_alloc_objects > 1) {\n    usage(\"Specify at most on of --inuse/--alloc options\");\n  }\n\n  # Check output granularities\n  my $grains =\n      $main::opt_functions +\n      $main::opt_lines +\n      $main::opt_addresses +\n      $main::opt_files +\n      0;\n  if ($grains > 1) {\n    usage(\"Only specify one output granularity option\");\n  }\n  if ($grains == 0) {\n    $main::opt_functions = 1;\n  }\n\n  # Check output modes\n  my $modes =\n      $main::opt_text +\n      $main::opt_callgrind +\n      ($main::opt_list eq '' ? 0 : 1) +\n      ($main::opt_disasm eq '' ? 0 : 1) +\n      ($main::opt_symbols == 0 ? 0 : 1) +\n      $main::opt_gv +\n      $main::opt_evince +\n      $main::opt_web +\n      $main::opt_dot +\n      $main::opt_ps +\n      $main::opt_pdf +\n      $main::opt_svg +\n      $main::opt_gif +\n      $main::opt_raw +\n      $main::opt_interactive +\n      0;\n  if ($modes > 1) {\n    usage(\"Only specify one output mode\");\n  }\n  if ($modes == 0) {\n    if (-t STDOUT) {  # If STDOUT is a tty, activate interactive mode\n      $main::opt_interactive = 1;\n    } else {\n      $main::opt_text = 1;\n    }\n  }\n\n  if ($main::opt_test) {\n    RunUnitTests();\n    # Should not return\n    exit(1);\n  }\n\n  # Binary name and profile arguments list\n  $main::prog = \"\";\n  @main::pfile_args = ();\n\n  # Remote profiling without a binary (using $SYMBOL_PAGE instead)\n  if (@ARGV > 0) {\n    if (IsProfileURL($ARGV[0])) {\n      $main::use_symbol_page = 1;\n    } elsif (IsSymbolizedProfileFile($ARGV[0])) {\n      $main::use_symbolized_profile = 1;\n      $main::prog = $UNKNOWN_BINARY;  # will be set later from the profile file\n    }\n  }\n\n  if ($main::use_symbol_page || $main::use_symbolized_profile) {\n    # We don't need a binary!\n    my %disabled = ('--lines' => $main::opt_lines,\n                    '--disasm' => $main::opt_disasm);\n    for my $option (keys %disabled) {\n      usage(\"$option cannot be used without a binary\") if $disabled{$option};\n    }\n    # Set $main::prog later...\n    scalar(@ARGV) || usage(\"Did not specify profile file\");\n  } elsif ($main::opt_symbols) {\n    # --symbols needs a binary-name (to run nm on, etc) but not profiles\n    $main::prog = shift(@ARGV) || usage(\"Did not specify program\");\n  } else {\n    $main::prog = shift(@ARGV) || usage(\"Did not specify program\");\n    scalar(@ARGV) || usage(\"Did not specify profile file\");\n  }\n\n  # Parse profile file/location arguments\n  foreach my $farg (@ARGV) {\n    if ($farg =~ m/(.*)\\@([0-9]+)(|\\/.*)$/ ) {\n      my $machine = $1;\n      my $num_machines = $2;\n      my $path = $3;\n      for (my $i = 0; $i < $num_machines; $i++) {\n        unshift(@main::pfile_args, \"$i.$machine$path\");\n      }\n    } else {\n      unshift(@main::pfile_args, $farg);\n    }\n  }\n\n  if ($main::use_symbol_page) {\n    unless (IsProfileURL($main::pfile_args[0])) {\n      error(\"The first profile should be a remote form to use $SYMBOL_PAGE\\n\");\n    }\n    CheckSymbolPage();\n    $main::prog = FetchProgramName();\n  } elsif (!$main::use_symbolized_profile) {  # may not need objtools!\n    ConfigureObjTools($main::prog)\n  }\n\n  # Break the opt_lib_prefix into the prefix_list array\n  @prefix_list = split (',', $main::opt_lib_prefix);\n\n  # Remove trailing / from the prefixes, in the list to prevent\n  # searching things like /my/path//lib/mylib.so\n  foreach (@prefix_list) {\n    s|/+$||;\n  }\n}\n\nsub FilterAndPrint {\n  my ($profile, $symbols, $libs, $thread) = @_;\n\n  # Get total data in profile\n  my $total = TotalProfile($profile);\n\n  # Remove uniniteresting stack items\n  $profile = RemoveUninterestingFrames($symbols, $profile);\n\n  # Focus?\n  if ($main::opt_focus ne '') {\n    $profile = FocusProfile($symbols, $profile, $main::opt_focus);\n  }\n\n  # Ignore?\n  if ($main::opt_ignore ne '') {\n    $profile = IgnoreProfile($symbols, $profile, $main::opt_ignore);\n  }\n\n  my $calls = ExtractCalls($symbols, $profile);\n\n  # Reduce profiles to required output granularity, and also clean\n  # each stack trace so a given entry exists at most once.\n  my $reduced = ReduceProfile($symbols, $profile);\n\n  # Get derived profiles\n  my $flat = FlatProfile($reduced);\n  my $cumulative = CumulativeProfile($reduced);\n\n  # Print\n  if (!$main::opt_interactive) {\n    if ($main::opt_disasm) {\n      PrintDisassembly($libs, $flat, $cumulative, $main::opt_disasm);\n    } elsif ($main::opt_list) {\n      PrintListing($total, $libs, $flat, $cumulative, $main::opt_list, 0);\n    } elsif ($main::opt_text) {\n      # Make sure the output is empty when have nothing to report\n      # (only matters when --heapcheck is given but we must be\n      # compatible with old branches that did not pass --heapcheck always):\n      if ($total != 0) {\n        printf(\"Total%s: %s %s\\n\",\n               (defined($thread) ? \" (t$thread)\" : \"\"),\n               Unparse($total), Units());\n      }\n      PrintText($symbols, $flat, $cumulative, -1);\n    } elsif ($main::opt_raw) {\n      PrintSymbolizedProfile($symbols, $profile, $main::prog);\n    } elsif ($main::opt_callgrind) {\n      PrintCallgrind($calls);\n    } else {\n      if (PrintDot($main::prog, $symbols, $profile, $flat, $cumulative, $total)) {\n        if ($main::opt_gv) {\n          RunGV(TempName($main::next_tmpfile, \"ps\"), \"\");\n        } elsif ($main::opt_evince) {\n          RunEvince(TempName($main::next_tmpfile, \"pdf\"), \"\");\n        } elsif ($main::opt_web) {\n          my $tmp = TempName($main::next_tmpfile, \"svg\");\n          RunWeb($tmp);\n          # The command we run might hand the file name off\n          # to an already running browser instance and then exit.\n          # Normally, we'd remove $tmp on exit (right now),\n          # but fork a child to remove $tmp a little later, so that the\n          # browser has time to load it first.\n          delete $main::tempnames{$tmp};\n          if (fork() == 0) {\n            sleep 5;\n            unlink($tmp);\n            exit(0);\n          }\n        }\n      } else {\n        cleanup();\n        exit(1);\n      }\n    }\n  } else {\n    InteractiveMode($profile, $symbols, $libs, $total);\n  }\n}\n\nsub Main() {\n  Init();\n  $main::collected_profile = undef;\n  @main::profile_files = ();\n  $main::op_time = time();\n\n  # Printing symbols is special and requires a lot less info that most.\n  if ($main::opt_symbols) {\n    PrintSymbols(*STDIN);   # Get /proc/maps and symbols output from stdin\n    return;\n  }\n\n  # Fetch all profile data\n  FetchDynamicProfiles();\n\n  # this will hold symbols that we read from the profile files\n  my $symbol_map = {};\n\n  # Read one profile, pick the last item on the list\n  my $data = ReadProfile($main::prog, pop(@main::profile_files));\n  my $profile = $data->{profile};\n  my $pcs = $data->{pcs};\n  my $libs = $data->{libs};   # Info about main program and shared libraries\n  $symbol_map = MergeSymbols($symbol_map, $data->{symbols});\n\n  # Add additional profiles, if available.\n  if (scalar(@main::profile_files) > 0) {\n    foreach my $pname (@main::profile_files) {\n      my $data2 = ReadProfile($main::prog, $pname);\n      $profile = AddProfile($profile, $data2->{profile});\n      $pcs = AddPcs($pcs, $data2->{pcs});\n      $symbol_map = MergeSymbols($symbol_map, $data2->{symbols});\n    }\n  }\n\n  # Subtract base from profile, if specified\n  if ($main::opt_base ne '') {\n    my $base = ReadProfile($main::prog, $main::opt_base);\n    $profile = SubtractProfile($profile, $base->{profile});\n    $pcs = AddPcs($pcs, $base->{pcs});\n    $symbol_map = MergeSymbols($symbol_map, $base->{symbols});\n  }\n\n  # Collect symbols\n  my $symbols;\n  if ($main::use_symbolized_profile) {\n    $symbols = FetchSymbols($pcs, $symbol_map);\n  } elsif ($main::use_symbol_page) {\n    $symbols = FetchSymbols($pcs);\n  } else {\n    # TODO(csilvers): $libs uses the /proc/self/maps data from profile1,\n    # which may differ from the data from subsequent profiles, especially\n    # if they were run on different machines.  Use appropriate libs for\n    # each pc somehow.\n    $symbols = ExtractSymbols($libs, $pcs);\n  }\n\n  if (!defined($main::opt_thread)) {\n    FilterAndPrint($profile, $symbols, $libs);\n  }\n  if (defined($data->{threads})) {\n    foreach my $thread (sort { $a <=> $b } keys(%{$data->{threads}})) {\n      if (defined($main::opt_thread) &&\n          ($main::opt_thread eq '*' || $main::opt_thread == $thread)) {\n        my $thread_profile = $data->{threads}{$thread};\n        FilterAndPrint($thread_profile, $symbols, $libs, $thread);\n      }\n    }\n  }\n\n  cleanup();\n  exit(0);\n}\n\n##### Entry Point #####\n\nMain();\n\n# Temporary code to detect if we're running on a Goobuntu system.\n# These systems don't have the right stuff installed for the special\n# Readline libraries to work, so as a temporary workaround, we default\n# to using the normal stdio code, rather than the fancier readline-based\n# code\nsub ReadlineMightFail {\n  if (-e '/lib/libtermcap.so.2') {\n    return 0;  # libtermcap exists, so readline should be okay\n  } else {\n    return 1;\n  }\n}\n\nsub RunGV {\n  my $fname = shift;\n  my $bg = shift;       # \"\" or \" &\" if we should run in background\n  if (!system(ShellEscape(@GV, \"--version\") . \" >$dev_null 2>&1\")) {\n    # Options using double dash are supported by this gv version.\n    # Also, turn on noantialias to better handle bug in gv for\n    # postscript files with large dimensions.\n    # TODO: Maybe we should not pass the --noantialias flag\n    # if the gv version is known to work properly without the flag.\n    system(ShellEscape(@GV, \"--scale=$main::opt_scale\", \"--noantialias\", $fname)\n           . $bg);\n  } else {\n    # Old gv version - only supports options that use single dash.\n    print STDERR ShellEscape(@GV, \"-scale\", $main::opt_scale) . \"\\n\";\n    system(ShellEscape(@GV, \"-scale\", \"$main::opt_scale\", $fname) . $bg);\n  }\n}\n\nsub RunEvince {\n  my $fname = shift;\n  my $bg = shift;       # \"\" or \" &\" if we should run in background\n  system(ShellEscape(@EVINCE, $fname) . $bg);\n}\n\nsub RunWeb {\n  my $fname = shift;\n  print STDERR \"Loading web page file:///$fname\\n\";\n\n  if (`uname` =~ /Darwin/) {\n    # OS X: open will use standard preference for SVG files.\n    system(\"/usr/bin/open\", $fname);\n    return;\n  }\n\n  # Some kind of Unix; try generic symlinks, then specific browsers.\n  # (Stop once we find one.)\n  # Works best if the browser is already running.\n  my @alt = (\n    \"/etc/alternatives/gnome-www-browser\",\n    \"/etc/alternatives/x-www-browser\",\n    \"google-chrome\",\n    \"firefox\",\n  );\n  foreach my $b (@alt) {\n    if (system($b, $fname) == 0) {\n      return;\n    }\n  }\n\n  print STDERR \"Could not load web browser.\\n\";\n}\n\nsub RunKcachegrind {\n  my $fname = shift;\n  my $bg = shift;       # \"\" or \" &\" if we should run in background\n  print STDERR \"Starting '@KCACHEGRIND \" . $fname . $bg . \"'\\n\";\n  system(ShellEscape(@KCACHEGRIND, $fname) . $bg);\n}\n\n\n##### Interactive helper routines #####\n\nsub InteractiveMode {\n  $| = 1;  # Make output unbuffered for interactive mode\n  my ($orig_profile, $symbols, $libs, $total) = @_;\n\n  print STDERR \"Welcome to jeprof!  For help, type 'help'.\\n\";\n\n  # Use ReadLine if it's installed and input comes from a console.\n  if ( -t STDIN &&\n       !ReadlineMightFail() &&\n       defined(eval {require Term::ReadLine}) ) {\n    my $term = new Term::ReadLine 'jeprof';\n    while ( defined ($_ = $term->readline('(jeprof) '))) {\n      $term->addhistory($_) if /\\S/;\n      if (!InteractiveCommand($orig_profile, $symbols, $libs, $total, $_)) {\n        last;    # exit when we get an interactive command to quit\n      }\n    }\n  } else {       # don't have readline\n    while (1) {\n      print STDERR \"(jeprof) \";\n      $_ = <STDIN>;\n      last if ! defined $_ ;\n      s/\\r//g;         # turn windows-looking lines into unix-looking lines\n\n      # Save some flags that might be reset by InteractiveCommand()\n      my $save_opt_lines = $main::opt_lines;\n\n      if (!InteractiveCommand($orig_profile, $symbols, $libs, $total, $_)) {\n        last;    # exit when we get an interactive command to quit\n      }\n\n      # Restore flags\n      $main::opt_lines = $save_opt_lines;\n    }\n  }\n}\n\n# Takes two args: orig profile, and command to run.\n# Returns 1 if we should keep going, or 0 if we were asked to quit\nsub InteractiveCommand {\n  my($orig_profile, $symbols, $libs, $total, $command) = @_;\n  $_ = $command;                # just to make future m//'s easier\n  if (!defined($_)) {\n    print STDERR \"\\n\";\n    return 0;\n  }\n  if (m/^\\s*quit/) {\n    return 0;\n  }\n  if (m/^\\s*help/) {\n    InteractiveHelpMessage();\n    return 1;\n  }\n  # Clear all the mode options -- mode is controlled by \"$command\"\n  $main::opt_text = 0;\n  $main::opt_callgrind = 0;\n  $main::opt_disasm = 0;\n  $main::opt_list = 0;\n  $main::opt_gv = 0;\n  $main::opt_evince = 0;\n  $main::opt_cum = 0;\n\n  if (m/^\\s*(text|top)(\\d*)\\s*(.*)/) {\n    $main::opt_text = 1;\n\n    my $line_limit = ($2 ne \"\") ? int($2) : 10;\n\n    my $routine;\n    my $ignore;\n    ($routine, $ignore) = ParseInteractiveArgs($3);\n\n    my $profile = ProcessProfile($total, $orig_profile, $symbols, \"\", $ignore);\n    my $reduced = ReduceProfile($symbols, $profile);\n\n    # Get derived profiles\n    my $flat = FlatProfile($reduced);\n    my $cumulative = CumulativeProfile($reduced);\n\n    PrintText($symbols, $flat, $cumulative, $line_limit);\n    return 1;\n  }\n  if (m/^\\s*callgrind\\s*([^ \\n]*)/) {\n    $main::opt_callgrind = 1;\n\n    # Get derived profiles\n    my $calls = ExtractCalls($symbols, $orig_profile);\n    my $filename = $1;\n    if ( $1 eq '' ) {\n      $filename = TempName($main::next_tmpfile, \"callgrind\");\n    }\n    PrintCallgrind($calls, $filename);\n    if ( $1 eq '' ) {\n      RunKcachegrind($filename, \" & \");\n      $main::next_tmpfile++;\n    }\n\n    return 1;\n  }\n  if (m/^\\s*(web)?list\\s*(.+)/) {\n    my $html = (defined($1) && ($1 eq \"web\"));\n    $main::opt_list = 1;\n\n    my $routine;\n    my $ignore;\n    ($routine, $ignore) = ParseInteractiveArgs($2);\n\n    my $profile = ProcessProfile($total, $orig_profile, $symbols, \"\", $ignore);\n    my $reduced = ReduceProfile($symbols, $profile);\n\n    # Get derived profiles\n    my $flat = FlatProfile($reduced);\n    my $cumulative = CumulativeProfile($reduced);\n\n    PrintListing($total, $libs, $flat, $cumulative, $routine, $html);\n    return 1;\n  }\n  if (m/^\\s*disasm\\s*(.+)/) {\n    $main::opt_disasm = 1;\n\n    my $routine;\n    my $ignore;\n    ($routine, $ignore) = ParseInteractiveArgs($1);\n\n    # Process current profile to account for various settings\n    my $profile = ProcessProfile($total, $orig_profile, $symbols, \"\", $ignore);\n    my $reduced = ReduceProfile($symbols, $profile);\n\n    # Get derived profiles\n    my $flat = FlatProfile($reduced);\n    my $cumulative = CumulativeProfile($reduced);\n\n    PrintDisassembly($libs, $flat, $cumulative, $routine);\n    return 1;\n  }\n  if (m/^\\s*(gv|web|evince)\\s*(.*)/) {\n    $main::opt_gv = 0;\n    $main::opt_evince = 0;\n    $main::opt_web = 0;\n    if ($1 eq \"gv\") {\n      $main::opt_gv = 1;\n    } elsif ($1 eq \"evince\") {\n      $main::opt_evince = 1;\n    } elsif ($1 eq \"web\") {\n      $main::opt_web = 1;\n    }\n\n    my $focus;\n    my $ignore;\n    ($focus, $ignore) = ParseInteractiveArgs($2);\n\n    # Process current profile to account for various settings\n    my $profile = ProcessProfile($total, $orig_profile, $symbols,\n                                 $focus, $ignore);\n    my $reduced = ReduceProfile($symbols, $profile);\n\n    # Get derived profiles\n    my $flat = FlatProfile($reduced);\n    my $cumulative = CumulativeProfile($reduced);\n\n    if (PrintDot($main::prog, $symbols, $profile, $flat, $cumulative, $total)) {\n      if ($main::opt_gv) {\n        RunGV(TempName($main::next_tmpfile, \"ps\"), \" &\");\n      } elsif ($main::opt_evince) {\n        RunEvince(TempName($main::next_tmpfile, \"pdf\"), \" &\");\n      } elsif ($main::opt_web) {\n        RunWeb(TempName($main::next_tmpfile, \"svg\"));\n      }\n      $main::next_tmpfile++;\n    }\n    return 1;\n  }\n  if (m/^\\s*$/) {\n    return 1;\n  }\n  print STDERR \"Unknown command: try 'help'.\\n\";\n  return 1;\n}\n\n\nsub ProcessProfile {\n  my $total_count = shift;\n  my $orig_profile = shift;\n  my $symbols = shift;\n  my $focus = shift;\n  my $ignore = shift;\n\n  # Process current profile to account for various settings\n  my $profile = $orig_profile;\n  printf(\"Total: %s %s\\n\", Unparse($total_count), Units());\n  if ($focus ne '') {\n    $profile = FocusProfile($symbols, $profile, $focus);\n    my $focus_count = TotalProfile($profile);\n    printf(\"After focusing on '%s': %s %s of %s (%0.1f%%)\\n\",\n           $focus,\n           Unparse($focus_count), Units(),\n           Unparse($total_count), ($focus_count*100.0) / $total_count);\n  }\n  if ($ignore ne '') {\n    $profile = IgnoreProfile($symbols, $profile, $ignore);\n    my $ignore_count = TotalProfile($profile);\n    printf(\"After ignoring '%s': %s %s of %s (%0.1f%%)\\n\",\n           $ignore,\n           Unparse($ignore_count), Units(),\n           Unparse($total_count),\n           ($ignore_count*100.0) / $total_count);\n  }\n\n  return $profile;\n}\n\nsub InteractiveHelpMessage {\n  print STDERR <<ENDOFHELP;\nInteractive jeprof mode\n\nCommands:\n  gv\n  gv [focus] [-ignore1] [-ignore2]\n      Show graphical hierarchical display of current profile.  Without\n      any arguments, shows all samples in the profile.  With the optional\n      \"focus\" argument, restricts the samples shown to just those where\n      the \"focus\" regular expression matches a routine name on the stack\n      trace.\n\n  web\n  web [focus] [-ignore1] [-ignore2]\n      Like GV, but displays profile in your web browser instead of using\n      Ghostview. Works best if your web browser is already running.\n      To change the browser that gets used:\n      On Linux, set the /etc/alternatives/gnome-www-browser symlink.\n      On OS X, change the Finder association for SVG files.\n\n  list [routine_regexp] [-ignore1] [-ignore2]\n      Show source listing of routines whose names match \"routine_regexp\"\n\n  weblist [routine_regexp] [-ignore1] [-ignore2]\n     Displays a source listing of routines whose names match \"routine_regexp\"\n     in a web browser.  You can click on source lines to view the\n     corresponding disassembly.\n\n  top [--cum] [-ignore1] [-ignore2]\n  top20 [--cum] [-ignore1] [-ignore2]\n  top37 [--cum] [-ignore1] [-ignore2]\n      Show top lines ordered by flat profile count, or cumulative count\n      if --cum is specified.  If a number is present after 'top', the\n      top K routines will be shown (defaults to showing the top 10)\n\n  disasm [routine_regexp] [-ignore1] [-ignore2]\n      Show disassembly of routines whose names match \"routine_regexp\",\n      annotated with sample counts.\n\n  callgrind\n  callgrind [filename]\n      Generates callgrind file. If no filename is given, kcachegrind is called.\n\n  help - This listing\n  quit or ^D - End jeprof\n\nFor commands that accept optional -ignore tags, samples where any routine in\nthe stack trace matches the regular expression in any of the -ignore\nparameters will be ignored.\n\nFurther pprof details are available at this location (or one similar):\n\n /usr/doc/gperftools-$PPROF_VERSION/cpu_profiler.html\n /usr/doc/gperftools-$PPROF_VERSION/heap_profiler.html\n\nENDOFHELP\n}\nsub ParseInteractiveArgs {\n  my $args = shift;\n  my $focus = \"\";\n  my $ignore = \"\";\n  my @x = split(/ +/, $args);\n  foreach $a (@x) {\n    if ($a =~ m/^(--|-)lines$/) {\n      $main::opt_lines = 1;\n    } elsif ($a =~ m/^(--|-)cum$/) {\n      $main::opt_cum = 1;\n    } elsif ($a =~ m/^-(.*)/) {\n      $ignore .= (($ignore ne \"\") ? \"|\" : \"\" ) . $1;\n    } else {\n      $focus .= (($focus ne \"\") ? \"|\" : \"\" ) . $a;\n    }\n  }\n  if ($ignore ne \"\") {\n    print STDERR \"Ignoring samples in call stacks that match '$ignore'\\n\";\n  }\n  return ($focus, $ignore);\n}\n\n##### Output code #####\n\nsub TempName {\n  my $fnum = shift;\n  my $ext = shift;\n  my $file = \"$main::tmpfile_ps.$fnum.$ext\";\n  $main::tempnames{$file} = 1;\n  return $file;\n}\n\n# Print profile data in packed binary format (64-bit) to standard out\nsub PrintProfileData {\n  my $profile = shift;\n\n  # print header (64-bit style)\n  # (zero) (header-size) (version) (sample-period) (zero)\n  print pack('L*', 0, 0, 3, 0, 0, 0, 1, 0, 0, 0);\n\n  foreach my $k (keys(%{$profile})) {\n    my $count = $profile->{$k};\n    my @addrs = split(/\\n/, $k);\n    if ($#addrs >= 0) {\n      my $depth = $#addrs + 1;\n      # int(foo / 2**32) is the only reliable way to get rid of bottom\n      # 32 bits on both 32- and 64-bit systems.\n      print pack('L*', $count & 0xFFFFFFFF, int($count / 2**32));\n      print pack('L*', $depth & 0xFFFFFFFF, int($depth / 2**32));\n\n      foreach my $full_addr (@addrs) {\n        my $addr = $full_addr;\n        $addr =~ s/0x0*//;  # strip off leading 0x, zeroes\n        if (length($addr) > 16) {\n          print STDERR \"Invalid address in profile: $full_addr\\n\";\n          next;\n        }\n        my $low_addr = substr($addr, -8);       # get last 8 hex chars\n        my $high_addr = substr($addr, -16, 8);  # get up to 8 more hex chars\n        print pack('L*', hex('0x' . $low_addr), hex('0x' . $high_addr));\n      }\n    }\n  }\n}\n\n# Print symbols and profile data\nsub PrintSymbolizedProfile {\n  my $symbols = shift;\n  my $profile = shift;\n  my $prog = shift;\n\n  $SYMBOL_PAGE =~ m,[^/]+$,;    # matches everything after the last slash\n  my $symbol_marker = $&;\n\n  print '--- ', $symbol_marker, \"\\n\";\n  if (defined($prog)) {\n    print 'binary=', $prog, \"\\n\";\n  }\n  while (my ($pc, $name) = each(%{$symbols})) {\n    my $sep = ' ';\n    print '0x', $pc;\n    # We have a list of function names, which include the inlined\n    # calls.  They are separated (and terminated) by --, which is\n    # illegal in function names.\n    for (my $j = 2; $j <= $#{$name}; $j += 3) {\n      print $sep, $name->[$j];\n      $sep = '--';\n    }\n    print \"\\n\";\n  }\n  print '---', \"\\n\";\n\n  my $profile_marker;\n  if ($main::profile_type eq 'heap') {\n    $HEAP_PAGE =~ m,[^/]+$,;    # matches everything after the last slash\n    $profile_marker = $&;\n  } elsif ($main::profile_type eq 'growth') {\n    $GROWTH_PAGE =~ m,[^/]+$,;    # matches everything after the last slash\n    $profile_marker = $&;\n  } elsif ($main::profile_type eq 'contention') {\n    $CONTENTION_PAGE =~ m,[^/]+$,;    # matches everything after the last slash\n    $profile_marker = $&;\n  } else { # elsif ($main::profile_type eq 'cpu')\n    $PROFILE_PAGE =~ m,[^/]+$,;    # matches everything after the last slash\n    $profile_marker = $&;\n  }\n\n  print '--- ', $profile_marker, \"\\n\";\n  if (defined($main::collected_profile)) {\n    # if used with remote fetch, simply dump the collected profile to output.\n    open(SRC, \"<$main::collected_profile\");\n    while (<SRC>) {\n      print $_;\n    }\n    close(SRC);\n  } else {\n    # --raw/http: For everything to work correctly for non-remote profiles, we\n    # would need to extend PrintProfileData() to handle all possible profile\n    # types, re-enable the code that is currently disabled in ReadCPUProfile()\n    # and FixCallerAddresses(), and remove the remote profile dumping code in\n    # the block above.\n    die \"--raw/http: jeprof can only dump remote profiles for --raw\\n\";\n    # dump a cpu-format profile to standard out\n    PrintProfileData($profile);\n  }\n}\n\n# Print text output\nsub PrintText {\n  my $symbols = shift;\n  my $flat = shift;\n  my $cumulative = shift;\n  my $line_limit = shift;\n\n  my $total = TotalProfile($flat);\n\n  # Which profile to sort by?\n  my $s = $main::opt_cum ? $cumulative : $flat;\n\n  my $running_sum = 0;\n  my $lines = 0;\n  foreach my $k (sort { GetEntry($s, $b) <=> GetEntry($s, $a) || $a cmp $b }\n                 keys(%{$cumulative})) {\n    my $f = GetEntry($flat, $k);\n    my $c = GetEntry($cumulative, $k);\n    $running_sum += $f;\n\n    my $sym = $k;\n    if (exists($symbols->{$k})) {\n      $sym = $symbols->{$k}->[0] . \" \" . $symbols->{$k}->[1];\n      if ($main::opt_addresses) {\n        $sym = $k . \" \" . $sym;\n      }\n    }\n\n    if ($f != 0 || $c != 0) {\n      printf(\"%8s %6s %6s %8s %6s %s\\n\",\n             Unparse($f),\n             Percent($f, $total),\n             Percent($running_sum, $total),\n             Unparse($c),\n             Percent($c, $total),\n             $sym);\n    }\n    $lines++;\n    last if ($line_limit >= 0 && $lines >= $line_limit);\n  }\n}\n\n# Callgrind format has a compression for repeated function and file\n# names.  You show the name the first time, and just use its number\n# subsequently.  This can cut down the file to about a third or a\n# quarter of its uncompressed size.  $key and $val are the key/value\n# pair that would normally be printed by callgrind; $map is a map from\n# value to number.\nsub CompressedCGName {\n  my($key, $val, $map) = @_;\n  my $idx = $map->{$val};\n  # For very short keys, providing an index hurts rather than helps.\n  if (length($val) <= 3) {\n    return \"$key=$val\\n\";\n  } elsif (defined($idx)) {\n    return \"$key=($idx)\\n\";\n  } else {\n    # scalar(keys $map) gives the number of items in the map.\n    $idx = scalar(keys(%{$map})) + 1;\n    $map->{$val} = $idx;\n    return \"$key=($idx) $val\\n\";\n  }\n}\n\n# Print the call graph in a way that's suiteable for callgrind.\nsub PrintCallgrind {\n  my $calls = shift;\n  my $filename;\n  my %filename_to_index_map;\n  my %fnname_to_index_map;\n\n  if ($main::opt_interactive) {\n    $filename = shift;\n    print STDERR \"Writing callgrind file to '$filename'.\\n\"\n  } else {\n    $filename = \"&STDOUT\";\n  }\n  open(CG, \">$filename\");\n  printf CG (\"events: Hits\\n\\n\");\n  foreach my $call ( map { $_->[0] }\n                     sort { $a->[1] cmp $b ->[1] ||\n                            $a->[2] <=> $b->[2] }\n                     map { /([^:]+):(\\d+):([^ ]+)( -> ([^:]+):(\\d+):(.+))?/;\n                           [$_, $1, $2] }\n                     keys %$calls ) {\n    my $count = int($calls->{$call});\n    $call =~ /([^:]+):(\\d+):([^ ]+)( -> ([^:]+):(\\d+):(.+))?/;\n    my ( $caller_file, $caller_line, $caller_function,\n         $callee_file, $callee_line, $callee_function ) =\n       ( $1, $2, $3, $5, $6, $7 );\n\n    # TODO(csilvers): for better compression, collect all the\n    # caller/callee_files and functions first, before printing\n    # anything, and only compress those referenced more than once.\n    printf CG CompressedCGName(\"fl\", $caller_file, \\%filename_to_index_map);\n    printf CG CompressedCGName(\"fn\", $caller_function, \\%fnname_to_index_map);\n    if (defined $6) {\n      printf CG CompressedCGName(\"cfl\", $callee_file, \\%filename_to_index_map);\n      printf CG CompressedCGName(\"cfn\", $callee_function, \\%fnname_to_index_map);\n      printf CG (\"calls=$count $callee_line\\n\");\n    }\n    printf CG (\"$caller_line $count\\n\\n\");\n  }\n}\n\n# Print disassembly for all all routines that match $main::opt_disasm\nsub PrintDisassembly {\n  my $libs = shift;\n  my $flat = shift;\n  my $cumulative = shift;\n  my $disasm_opts = shift;\n\n  my $total = TotalProfile($flat);\n\n  foreach my $lib (@{$libs}) {\n    my $symbol_table = GetProcedureBoundaries($lib->[0], $disasm_opts);\n    my $offset = AddressSub($lib->[1], $lib->[3]);\n    foreach my $routine (sort ByName keys(%{$symbol_table})) {\n      my $start_addr = $symbol_table->{$routine}->[0];\n      my $end_addr = $symbol_table->{$routine}->[1];\n      # See if there are any samples in this routine\n      my $length = hex(AddressSub($end_addr, $start_addr));\n      my $addr = AddressAdd($start_addr, $offset);\n      for (my $i = 0; $i < $length; $i++) {\n        if (defined($cumulative->{$addr})) {\n          PrintDisassembledFunction($lib->[0], $offset,\n                                    $routine, $flat, $cumulative,\n                                    $start_addr, $end_addr, $total);\n          last;\n        }\n        $addr = AddressInc($addr);\n      }\n    }\n  }\n}\n\n# Return reference to array of tuples of the form:\n#       [start_address, filename, linenumber, instruction, limit_address]\n# E.g.,\n#       [\"0x806c43d\", \"/foo/bar.cc\", 131, \"ret\", \"0x806c440\"]\nsub Disassemble {\n  my $prog = shift;\n  my $offset = shift;\n  my $start_addr = shift;\n  my $end_addr = shift;\n\n  my $objdump = $obj_tool_map{\"objdump\"};\n  my $cmd = ShellEscape($objdump, \"-C\", \"-d\", \"-l\", \"--no-show-raw-insn\",\n                        \"--start-address=0x$start_addr\",\n                        \"--stop-address=0x$end_addr\", $prog);\n  open(OBJDUMP, \"$cmd |\") || error(\"$cmd: $!\\n\");\n  my @result = ();\n  my $filename = \"\";\n  my $linenumber = -1;\n  my $last = [\"\", \"\", \"\", \"\"];\n  while (<OBJDUMP>) {\n    s/\\r//g;         # turn windows-looking lines into unix-looking lines\n    chop;\n    if (m|\\s*([^:\\s]+):(\\d+)\\s*$|) {\n      # Location line of the form:\n      #   <filename>:<linenumber>\n      $filename = $1;\n      $linenumber = $2;\n    } elsif (m/^ +([0-9a-f]+):\\s*(.*)/) {\n      # Disassembly line -- zero-extend address to full length\n      my $addr = HexExtend($1);\n      my $k = AddressAdd($addr, $offset);\n      $last->[4] = $k;   # Store ending address for previous instruction\n      $last = [$k, $filename, $linenumber, $2, $end_addr];\n      push(@result, $last);\n    }\n  }\n  close(OBJDUMP);\n  return @result;\n}\n\n# The input file should contain lines of the form /proc/maps-like\n# output (same format as expected from the profiles) or that looks\n# like hex addresses (like \"0xDEADBEEF\").  We will parse all\n# /proc/maps output, and for all the hex addresses, we will output\n# \"short\" symbol names, one per line, in the same order as the input.\nsub PrintSymbols {\n  my $maps_and_symbols_file = shift;\n\n  # ParseLibraries expects pcs to be in a set.  Fine by us...\n  my @pclist = ();   # pcs in sorted order\n  my $pcs = {};\n  my $map = \"\";\n  foreach my $line (<$maps_and_symbols_file>) {\n    $line =~ s/\\r//g;    # turn windows-looking lines into unix-looking lines\n    if ($line =~ /\\b(0x[0-9a-f]+)\\b/i) {\n      push(@pclist, HexExtend($1));\n      $pcs->{$pclist[-1]} = 1;\n    } else {\n      $map .= $line;\n    }\n  }\n\n  my $libs = ParseLibraries($main::prog, $map, $pcs);\n  my $symbols = ExtractSymbols($libs, $pcs);\n\n  foreach my $pc (@pclist) {\n    # ->[0] is the shortname, ->[2] is the full name\n    print(($symbols->{$pc}->[0] || \"??\") . \"\\n\");\n  }\n}\n\n\n# For sorting functions by name\nsub ByName {\n  return ShortFunctionName($a) cmp ShortFunctionName($b);\n}\n\n# Print source-listing for all all routines that match $list_opts\nsub PrintListing {\n  my $total = shift;\n  my $libs = shift;\n  my $flat = shift;\n  my $cumulative = shift;\n  my $list_opts = shift;\n  my $html = shift;\n\n  my $output = \\*STDOUT;\n  my $fname = \"\";\n\n  if ($html) {\n    # Arrange to write the output to a temporary file\n    $fname = TempName($main::next_tmpfile, \"html\");\n    $main::next_tmpfile++;\n    if (!open(TEMP, \">$fname\")) {\n      print STDERR \"$fname: $!\\n\";\n      return;\n    }\n    $output = \\*TEMP;\n    print $output HtmlListingHeader();\n    printf $output (\"<div class=\\\"legend\\\">%s<br>Total: %s %s</div>\\n\",\n                    $main::prog, Unparse($total), Units());\n  }\n\n  my $listed = 0;\n  foreach my $lib (@{$libs}) {\n    my $symbol_table = GetProcedureBoundaries($lib->[0], $list_opts);\n    my $offset = AddressSub($lib->[1], $lib->[3]);\n    foreach my $routine (sort ByName keys(%{$symbol_table})) {\n      # Print if there are any samples in this routine\n      my $start_addr = $symbol_table->{$routine}->[0];\n      my $end_addr = $symbol_table->{$routine}->[1];\n      my $length = hex(AddressSub($end_addr, $start_addr));\n      my $addr = AddressAdd($start_addr, $offset);\n      for (my $i = 0; $i < $length; $i++) {\n        if (defined($cumulative->{$addr})) {\n          $listed += PrintSource(\n            $lib->[0], $offset,\n            $routine, $flat, $cumulative,\n            $start_addr, $end_addr,\n            $html,\n            $output);\n          last;\n        }\n        $addr = AddressInc($addr);\n      }\n    }\n  }\n\n  if ($html) {\n    if ($listed > 0) {\n      print $output HtmlListingFooter();\n      close($output);\n      RunWeb($fname);\n    } else {\n      close($output);\n      unlink($fname);\n    }\n  }\n}\n\nsub HtmlListingHeader {\n  return <<'EOF';\n<DOCTYPE html>\n<html>\n<head>\n<title>Pprof listing</title>\n<style type=\"text/css\">\nbody {\n  font-family: sans-serif;\n}\nh1 {\n  font-size: 1.5em;\n  margin-bottom: 4px;\n}\n.legend {\n  font-size: 1.25em;\n}\n.line {\n  color: #aaaaaa;\n}\n.nop {\n  color: #aaaaaa;\n}\n.unimportant {\n  color: #cccccc;\n}\n.disasmloc {\n  color: #000000;\n}\n.deadsrc {\n  cursor: pointer;\n}\n.deadsrc:hover {\n  background-color: #eeeeee;\n}\n.livesrc {\n  color: #0000ff;\n  cursor: pointer;\n}\n.livesrc:hover {\n  background-color: #eeeeee;\n}\n.asm {\n  color: #008800;\n  display: none;\n}\n</style>\n<script type=\"text/javascript\">\nfunction jeprof_toggle_asm(e) {\n  var target;\n  if (!e) e = window.event;\n  if (e.target) target = e.target;\n  else if (e.srcElement) target = e.srcElement;\n\n  if (target) {\n    var asm = target.nextSibling;\n    if (asm && asm.className == \"asm\") {\n      asm.style.display = (asm.style.display == \"block\" ? \"\" : \"block\");\n      e.preventDefault();\n      return false;\n    }\n  }\n}\n</script>\n</head>\n<body>\nEOF\n}\n\nsub HtmlListingFooter {\n  return <<'EOF';\n</body>\n</html>\nEOF\n}\n\nsub HtmlEscape {\n  my $text = shift;\n  $text =~ s/&/&amp;/g;\n  $text =~ s/</&lt;/g;\n  $text =~ s/>/&gt;/g;\n  return $text;\n}\n\n# Returns the indentation of the line, if it has any non-whitespace\n# characters.  Otherwise, returns -1.\nsub Indentation {\n  my $line = shift;\n  if (m/^(\\s*)\\S/) {\n    return length($1);\n  } else {\n    return -1;\n  }\n}\n\n# If the symbol table contains inlining info, Disassemble() may tag an\n# instruction with a location inside an inlined function.  But for\n# source listings, we prefer to use the location in the function we\n# are listing.  So use MapToSymbols() to fetch full location\n# information for each instruction and then pick out the first\n# location from a location list (location list contains callers before\n# callees in case of inlining).\n#\n# After this routine has run, each entry in $instructions contains:\n#   [0] start address\n#   [1] filename for function we are listing\n#   [2] line number for function we are listing\n#   [3] disassembly\n#   [4] limit address\n#   [5] most specific filename (may be different from [1] due to inlining)\n#   [6] most specific line number (may be different from [2] due to inlining)\nsub GetTopLevelLineNumbers {\n  my ($lib, $offset, $instructions) = @_;\n  my $pcs = [];\n  for (my $i = 0; $i <= $#{$instructions}; $i++) {\n    push(@{$pcs}, $instructions->[$i]->[0]);\n  }\n  my $symbols = {};\n  MapToSymbols($lib, $offset, $pcs, $symbols);\n  for (my $i = 0; $i <= $#{$instructions}; $i++) {\n    my $e = $instructions->[$i];\n    push(@{$e}, $e->[1]);\n    push(@{$e}, $e->[2]);\n    my $addr = $e->[0];\n    my $sym = $symbols->{$addr};\n    if (defined($sym)) {\n      if ($#{$sym} >= 2 && $sym->[1] =~ m/^(.*):(\\d+)$/) {\n        $e->[1] = $1;  # File name\n        $e->[2] = $2;  # Line number\n      }\n    }\n  }\n}\n\n# Print source-listing for one routine\nsub PrintSource {\n  my $prog = shift;\n  my $offset = shift;\n  my $routine = shift;\n  my $flat = shift;\n  my $cumulative = shift;\n  my $start_addr = shift;\n  my $end_addr = shift;\n  my $html = shift;\n  my $output = shift;\n\n  # Disassemble all instructions (just to get line numbers)\n  my @instructions = Disassemble($prog, $offset, $start_addr, $end_addr);\n  GetTopLevelLineNumbers($prog, $offset, \\@instructions);\n\n  # Hack 1: assume that the first source file encountered in the\n  # disassembly contains the routine\n  my $filename = undef;\n  for (my $i = 0; $i <= $#instructions; $i++) {\n    if ($instructions[$i]->[2] >= 0) {\n      $filename = $instructions[$i]->[1];\n      last;\n    }\n  }\n  if (!defined($filename)) {\n    print STDERR \"no filename found in $routine\\n\";\n    return 0;\n  }\n\n  # Hack 2: assume that the largest line number from $filename is the\n  # end of the procedure.  This is typically safe since if P1 contains\n  # an inlined call to P2, then P2 usually occurs earlier in the\n  # source file.  If this does not work, we might have to compute a\n  # density profile or just print all regions we find.\n  my $lastline = 0;\n  for (my $i = 0; $i <= $#instructions; $i++) {\n    my $f = $instructions[$i]->[1];\n    my $l = $instructions[$i]->[2];\n    if (($f eq $filename) && ($l > $lastline)) {\n      $lastline = $l;\n    }\n  }\n\n  # Hack 3: assume the first source location from \"filename\" is the start of\n  # the source code.\n  my $firstline = 1;\n  for (my $i = 0; $i <= $#instructions; $i++) {\n    if ($instructions[$i]->[1] eq $filename) {\n      $firstline = $instructions[$i]->[2];\n      last;\n    }\n  }\n\n  # Hack 4: Extend last line forward until its indentation is less than\n  # the indentation we saw on $firstline\n  my $oldlastline = $lastline;\n  {\n    if (!open(FILE, \"<$filename\")) {\n      print STDERR \"$filename: $!\\n\";\n      return 0;\n    }\n    my $l = 0;\n    my $first_indentation = -1;\n    while (<FILE>) {\n      s/\\r//g;         # turn windows-looking lines into unix-looking lines\n      $l++;\n      my $indent = Indentation($_);\n      if ($l >= $firstline) {\n        if ($first_indentation < 0 && $indent >= 0) {\n          $first_indentation = $indent;\n          last if ($first_indentation == 0);\n        }\n      }\n      if ($l >= $lastline && $indent >= 0) {\n        if ($indent >= $first_indentation) {\n          $lastline = $l+1;\n        } else {\n          last;\n        }\n      }\n    }\n    close(FILE);\n  }\n\n  # Assign all samples to the range $firstline,$lastline,\n  # Hack 4: If an instruction does not occur in the range, its samples\n  # are moved to the next instruction that occurs in the range.\n  my $samples1 = {};        # Map from line number to flat count\n  my $samples2 = {};        # Map from line number to cumulative count\n  my $running1 = 0;         # Unassigned flat counts\n  my $running2 = 0;         # Unassigned cumulative counts\n  my $total1 = 0;           # Total flat counts\n  my $total2 = 0;           # Total cumulative counts\n  my %disasm = ();          # Map from line number to disassembly\n  my $running_disasm = \"\";  # Unassigned disassembly\n  my $skip_marker = \"---\\n\";\n  if ($html) {\n    $skip_marker = \"\";\n    for (my $l = $firstline; $l <= $lastline; $l++) {\n      $disasm{$l} = \"\";\n    }\n  }\n  my $last_dis_filename = '';\n  my $last_dis_linenum = -1;\n  my $last_touched_line = -1;  # To detect gaps in disassembly for a line\n  foreach my $e (@instructions) {\n    # Add up counts for all address that fall inside this instruction\n    my $c1 = 0;\n    my $c2 = 0;\n    for (my $a = $e->[0]; $a lt $e->[4]; $a = AddressInc($a)) {\n      $c1 += GetEntry($flat, $a);\n      $c2 += GetEntry($cumulative, $a);\n    }\n\n    if ($html) {\n      my $dis = sprintf(\"      %6s %6s \\t\\t%8s: %s \",\n                        HtmlPrintNumber($c1),\n                        HtmlPrintNumber($c2),\n                        UnparseAddress($offset, $e->[0]),\n                        CleanDisassembly($e->[3]));\n\n      # Append the most specific source line associated with this instruction\n      if (length($dis) < 80) { $dis .= (' ' x (80 - length($dis))) };\n      $dis = HtmlEscape($dis);\n      my $f = $e->[5];\n      my $l = $e->[6];\n      if ($f ne $last_dis_filename) {\n        $dis .= sprintf(\"<span class=disasmloc>%s:%d</span>\",\n                        HtmlEscape(CleanFileName($f)), $l);\n      } elsif ($l ne $last_dis_linenum) {\n        # De-emphasize the unchanged file name portion\n        $dis .= sprintf(\"<span class=unimportant>%s</span>\" .\n                        \"<span class=disasmloc>:%d</span>\",\n                        HtmlEscape(CleanFileName($f)), $l);\n      } else {\n        # De-emphasize the entire location\n        $dis .= sprintf(\"<span class=unimportant>%s:%d</span>\",\n                        HtmlEscape(CleanFileName($f)), $l);\n      }\n      $last_dis_filename = $f;\n      $last_dis_linenum = $l;\n      $running_disasm .= $dis;\n      $running_disasm .= \"\\n\";\n    }\n\n    $running1 += $c1;\n    $running2 += $c2;\n    $total1 += $c1;\n    $total2 += $c2;\n    my $file = $e->[1];\n    my $line = $e->[2];\n    if (($file eq $filename) &&\n        ($line >= $firstline) &&\n        ($line <= $lastline)) {\n      # Assign all accumulated samples to this line\n      AddEntry($samples1, $line, $running1);\n      AddEntry($samples2, $line, $running2);\n      $running1 = 0;\n      $running2 = 0;\n      if ($html) {\n        if ($line != $last_touched_line && $disasm{$line} ne '') {\n          $disasm{$line} .= \"\\n\";\n        }\n        $disasm{$line} .= $running_disasm;\n        $running_disasm = '';\n        $last_touched_line = $line;\n      }\n    }\n  }\n\n  # Assign any leftover samples to $lastline\n  AddEntry($samples1, $lastline, $running1);\n  AddEntry($samples2, $lastline, $running2);\n  if ($html) {\n    if ($lastline != $last_touched_line && $disasm{$lastline} ne '') {\n      $disasm{$lastline} .= \"\\n\";\n    }\n    $disasm{$lastline} .= $running_disasm;\n  }\n\n  if ($html) {\n    printf $output (\n      \"<h1>%s</h1>%s\\n<pre onClick=\\\"jeprof_toggle_asm()\\\">\\n\" .\n      \"Total:%6s %6s (flat / cumulative %s)\\n\",\n      HtmlEscape(ShortFunctionName($routine)),\n      HtmlEscape(CleanFileName($filename)),\n      Unparse($total1),\n      Unparse($total2),\n      Units());\n  } else {\n    printf $output (\n      \"ROUTINE ====================== %s in %s\\n\" .\n      \"%6s %6s Total %s (flat / cumulative)\\n\",\n      ShortFunctionName($routine),\n      CleanFileName($filename),\n      Unparse($total1),\n      Unparse($total2),\n      Units());\n  }\n  if (!open(FILE, \"<$filename\")) {\n    print STDERR \"$filename: $!\\n\";\n    return 0;\n  }\n  my $l = 0;\n  while (<FILE>) {\n    s/\\r//g;         # turn windows-looking lines into unix-looking lines\n    $l++;\n    if ($l >= $firstline - 5 &&\n        (($l <= $oldlastline + 5) || ($l <= $lastline))) {\n      chop;\n      my $text = $_;\n      if ($l == $firstline) { print $output $skip_marker; }\n      my $n1 = GetEntry($samples1, $l);\n      my $n2 = GetEntry($samples2, $l);\n      if ($html) {\n        # Emit a span that has one of the following classes:\n        #    livesrc -- has samples\n        #    deadsrc -- has disassembly, but with no samples\n        #    nop     -- has no matching disasembly\n        # Also emit an optional span containing disassembly.\n        my $dis = $disasm{$l};\n        my $asm = \"\";\n        if (defined($dis) && $dis ne '') {\n          $asm = \"<span class=\\\"asm\\\">\" . $dis . \"</span>\";\n        }\n        my $source_class = (($n1 + $n2 > 0)\n                            ? \"livesrc\"\n                            : (($asm ne \"\") ? \"deadsrc\" : \"nop\"));\n        printf $output (\n          \"<span class=\\\"line\\\">%5d</span> \" .\n          \"<span class=\\\"%s\\\">%6s %6s %s</span>%s\\n\",\n          $l, $source_class,\n          HtmlPrintNumber($n1),\n          HtmlPrintNumber($n2),\n          HtmlEscape($text),\n          $asm);\n      } else {\n        printf $output(\n          \"%6s %6s %4d: %s\\n\",\n          UnparseAlt($n1),\n          UnparseAlt($n2),\n          $l,\n          $text);\n      }\n      if ($l == $lastline)  { print $output $skip_marker; }\n    };\n  }\n  close(FILE);\n  if ($html) {\n    print $output \"</pre>\\n\";\n  }\n  return 1;\n}\n\n# Return the source line for the specified file/linenumber.\n# Returns undef if not found.\nsub SourceLine {\n  my $file = shift;\n  my $line = shift;\n\n  # Look in cache\n  if (!defined($main::source_cache{$file})) {\n    if (100 < scalar keys(%main::source_cache)) {\n      # Clear the cache when it gets too big\n      $main::source_cache = ();\n    }\n\n    # Read all lines from the file\n    if (!open(FILE, \"<$file\")) {\n      print STDERR \"$file: $!\\n\";\n      $main::source_cache{$file} = [];  # Cache the negative result\n      return undef;\n    }\n    my $lines = [];\n    push(@{$lines}, \"\");        # So we can use 1-based line numbers as indices\n    while (<FILE>) {\n      push(@{$lines}, $_);\n    }\n    close(FILE);\n\n    # Save the lines in the cache\n    $main::source_cache{$file} = $lines;\n  }\n\n  my $lines = $main::source_cache{$file};\n  if (($line < 0) || ($line > $#{$lines})) {\n    return undef;\n  } else {\n    return $lines->[$line];\n  }\n}\n\n# Print disassembly for one routine with interspersed source if available\nsub PrintDisassembledFunction {\n  my $prog = shift;\n  my $offset = shift;\n  my $routine = shift;\n  my $flat = shift;\n  my $cumulative = shift;\n  my $start_addr = shift;\n  my $end_addr = shift;\n  my $total = shift;\n\n  # Disassemble all instructions\n  my @instructions = Disassemble($prog, $offset, $start_addr, $end_addr);\n\n  # Make array of counts per instruction\n  my @flat_count = ();\n  my @cum_count = ();\n  my $flat_total = 0;\n  my $cum_total = 0;\n  foreach my $e (@instructions) {\n    # Add up counts for all address that fall inside this instruction\n    my $c1 = 0;\n    my $c2 = 0;\n    for (my $a = $e->[0]; $a lt $e->[4]; $a = AddressInc($a)) {\n      $c1 += GetEntry($flat, $a);\n      $c2 += GetEntry($cumulative, $a);\n    }\n    push(@flat_count, $c1);\n    push(@cum_count, $c2);\n    $flat_total += $c1;\n    $cum_total += $c2;\n  }\n\n  # Print header with total counts\n  printf(\"ROUTINE ====================== %s\\n\" .\n         \"%6s %6s %s (flat, cumulative) %.1f%% of total\\n\",\n         ShortFunctionName($routine),\n         Unparse($flat_total),\n         Unparse($cum_total),\n         Units(),\n         ($cum_total * 100.0) / $total);\n\n  # Process instructions in order\n  my $current_file = \"\";\n  for (my $i = 0; $i <= $#instructions; ) {\n    my $e = $instructions[$i];\n\n    # Print the new file name whenever we switch files\n    if ($e->[1] ne $current_file) {\n      $current_file = $e->[1];\n      my $fname = $current_file;\n      $fname =~ s|^\\./||;   # Trim leading \"./\"\n\n      # Shorten long file names\n      if (length($fname) >= 58) {\n        $fname = \"...\" . substr($fname, -55);\n      }\n      printf(\"-------------------- %s\\n\", $fname);\n    }\n\n    # TODO: Compute range of lines to print together to deal with\n    # small reorderings.\n    my $first_line = $e->[2];\n    my $last_line = $first_line;\n    my %flat_sum = ();\n    my %cum_sum = ();\n    for (my $l = $first_line; $l <= $last_line; $l++) {\n      $flat_sum{$l} = 0;\n      $cum_sum{$l} = 0;\n    }\n\n    # Find run of instructions for this range of source lines\n    my $first_inst = $i;\n    while (($i <= $#instructions) &&\n           ($instructions[$i]->[2] >= $first_line) &&\n           ($instructions[$i]->[2] <= $last_line)) {\n      $e = $instructions[$i];\n      $flat_sum{$e->[2]} += $flat_count[$i];\n      $cum_sum{$e->[2]} += $cum_count[$i];\n      $i++;\n    }\n    my $last_inst = $i - 1;\n\n    # Print source lines\n    for (my $l = $first_line; $l <= $last_line; $l++) {\n      my $line = SourceLine($current_file, $l);\n      if (!defined($line)) {\n        $line = \"?\\n\";\n        next;\n      } else {\n        $line =~ s/^\\s+//;\n      }\n      printf(\"%6s %6s %5d: %s\",\n             UnparseAlt($flat_sum{$l}),\n             UnparseAlt($cum_sum{$l}),\n             $l,\n             $line);\n    }\n\n    # Print disassembly\n    for (my $x = $first_inst; $x <= $last_inst; $x++) {\n      my $e = $instructions[$x];\n      printf(\"%6s %6s    %8s: %6s\\n\",\n             UnparseAlt($flat_count[$x]),\n             UnparseAlt($cum_count[$x]),\n             UnparseAddress($offset, $e->[0]),\n             CleanDisassembly($e->[3]));\n    }\n  }\n}\n\n# Print DOT graph\nsub PrintDot {\n  my $prog = shift;\n  my $symbols = shift;\n  my $raw = shift;\n  my $flat = shift;\n  my $cumulative = shift;\n  my $overall_total = shift;\n\n  # Get total\n  my $local_total = TotalProfile($flat);\n  my $nodelimit = int($main::opt_nodefraction * $local_total);\n  my $edgelimit = int($main::opt_edgefraction * $local_total);\n  my $nodecount = $main::opt_nodecount;\n\n  # Find nodes to include\n  my @list = (sort { abs(GetEntry($cumulative, $b)) <=>\n                     abs(GetEntry($cumulative, $a))\n                     || $a cmp $b }\n              keys(%{$cumulative}));\n  my $last = $nodecount - 1;\n  if ($last > $#list) {\n    $last = $#list;\n  }\n  while (($last >= 0) &&\n         (abs(GetEntry($cumulative, $list[$last])) <= $nodelimit)) {\n    $last--;\n  }\n  if ($last < 0) {\n    print STDERR \"No nodes to print\\n\";\n    return 0;\n  }\n\n  if ($nodelimit > 0 || $edgelimit > 0) {\n    printf STDERR (\"Dropping nodes with <= %s %s; edges with <= %s abs(%s)\\n\",\n                   Unparse($nodelimit), Units(),\n                   Unparse($edgelimit), Units());\n  }\n\n  # Open DOT output file\n  my $output;\n  my $escaped_dot = ShellEscape(@DOT);\n  my $escaped_ps2pdf = ShellEscape(@PS2PDF);\n  if ($main::opt_gv) {\n    my $escaped_outfile = ShellEscape(TempName($main::next_tmpfile, \"ps\"));\n    $output = \"| $escaped_dot -Tps2 >$escaped_outfile\";\n  } elsif ($main::opt_evince) {\n    my $escaped_outfile = ShellEscape(TempName($main::next_tmpfile, \"pdf\"));\n    $output = \"| $escaped_dot -Tps2 | $escaped_ps2pdf - $escaped_outfile\";\n  } elsif ($main::opt_ps) {\n    $output = \"| $escaped_dot -Tps2\";\n  } elsif ($main::opt_pdf) {\n    $output = \"| $escaped_dot -Tps2 | $escaped_ps2pdf - -\";\n  } elsif ($main::opt_web || $main::opt_svg) {\n    # We need to post-process the SVG, so write to a temporary file always.\n    my $escaped_outfile = ShellEscape(TempName($main::next_tmpfile, \"svg\"));\n    $output = \"| $escaped_dot -Tsvg >$escaped_outfile\";\n  } elsif ($main::opt_gif) {\n    $output = \"| $escaped_dot -Tgif\";\n  } else {\n    $output = \">&STDOUT\";\n  }\n  open(DOT, $output) || error(\"$output: $!\\n\");\n\n  # Title\n  printf DOT (\"digraph \\\"%s; %s %s\\\" {\\n\",\n              $prog,\n              Unparse($overall_total),\n              Units());\n  if ($main::opt_pdf) {\n    # The output is more printable if we set the page size for dot.\n    printf DOT (\"size=\\\"8,11\\\"\\n\");\n  }\n  printf DOT (\"node [width=0.375,height=0.25];\\n\");\n\n  # Print legend\n  printf DOT (\"Legend [shape=box,fontsize=24,shape=plaintext,\" .\n              \"label=\\\"%s\\\\l%s\\\\l%s\\\\l%s\\\\l%s\\\\l\\\"];\\n\",\n              $prog,\n              sprintf(\"Total %s: %s\", Units(), Unparse($overall_total)),\n              sprintf(\"Focusing on: %s\", Unparse($local_total)),\n              sprintf(\"Dropped nodes with <= %s abs(%s)\",\n                      Unparse($nodelimit), Units()),\n              sprintf(\"Dropped edges with <= %s %s\",\n                      Unparse($edgelimit), Units())\n              );\n\n  # Print nodes\n  my %node = ();\n  my $nextnode = 1;\n  foreach my $a (@list[0..$last]) {\n    # Pick font size\n    my $f = GetEntry($flat, $a);\n    my $c = GetEntry($cumulative, $a);\n\n    my $fs = 8;\n    if ($local_total > 0) {\n      $fs = 8 + (50.0 * sqrt(abs($f * 1.0 / $local_total)));\n    }\n\n    $node{$a} = $nextnode++;\n    my $sym = $a;\n    $sym =~ s/\\s+/\\\\n/g;\n    $sym =~ s/::/\\\\n/g;\n\n    # Extra cumulative info to print for non-leaves\n    my $extra = \"\";\n    if ($f != $c) {\n      $extra = sprintf(\"\\\\rof %s (%s)\",\n                       Unparse($c),\n                       Percent($c, $local_total));\n    }\n    my $style = \"\";\n    if ($main::opt_heapcheck) {\n      if ($f > 0) {\n        # make leak-causing nodes more visible (add a background)\n        $style = \",style=filled,fillcolor=gray\"\n      } elsif ($f < 0) {\n        # make anti-leak-causing nodes (which almost never occur)\n        # stand out as well (triple border)\n        $style = \",peripheries=3\"\n      }\n    }\n\n    printf DOT (\"N%d [label=\\\"%s\\\\n%s (%s)%s\\\\r\" .\n                \"\\\",shape=box,fontsize=%.1f%s];\\n\",\n                $node{$a},\n                $sym,\n                Unparse($f),\n                Percent($f, $local_total),\n                $extra,\n                $fs,\n                $style,\n               );\n  }\n\n  # Get edges and counts per edge\n  my %edge = ();\n  my $n;\n  my $fullname_to_shortname_map = {};\n  FillFullnameToShortnameMap($symbols, $fullname_to_shortname_map);\n  foreach my $k (keys(%{$raw})) {\n    # TODO: omit low %age edges\n    $n = $raw->{$k};\n    my @translated = TranslateStack($symbols, $fullname_to_shortname_map, $k);\n    for (my $i = 1; $i <= $#translated; $i++) {\n      my $src = $translated[$i];\n      my $dst = $translated[$i-1];\n      #next if ($src eq $dst);  # Avoid self-edges?\n      if (exists($node{$src}) && exists($node{$dst})) {\n        my $edge_label = \"$src\\001$dst\";\n        if (!exists($edge{$edge_label})) {\n          $edge{$edge_label} = 0;\n        }\n        $edge{$edge_label} += $n;\n      }\n    }\n  }\n\n  # Print edges (process in order of decreasing counts)\n  my %indegree = ();   # Number of incoming edges added per node so far\n  my %outdegree = ();  # Number of outgoing edges added per node so far\n  foreach my $e (sort { $edge{$b} <=> $edge{$a} } keys(%edge)) {\n    my @x = split(/\\001/, $e);\n    $n = $edge{$e};\n\n    # Initialize degree of kept incoming and outgoing edges if necessary\n    my $src = $x[0];\n    my $dst = $x[1];\n    if (!exists($outdegree{$src})) { $outdegree{$src} = 0; }\n    if (!exists($indegree{$dst})) { $indegree{$dst} = 0; }\n\n    my $keep;\n    if ($indegree{$dst} == 0) {\n      # Keep edge if needed for reachability\n      $keep = 1;\n    } elsif (abs($n) <= $edgelimit) {\n      # Drop if we are below --edgefraction\n      $keep = 0;\n    } elsif ($outdegree{$src} >= $main::opt_maxdegree ||\n             $indegree{$dst} >= $main::opt_maxdegree) {\n      # Keep limited number of in/out edges per node\n      $keep = 0;\n    } else {\n      $keep = 1;\n    }\n\n    if ($keep) {\n      $outdegree{$src}++;\n      $indegree{$dst}++;\n\n      # Compute line width based on edge count\n      my $fraction = abs($local_total ? (3 * ($n / $local_total)) : 0);\n      if ($fraction > 1) { $fraction = 1; }\n      my $w = $fraction * 2;\n      if ($w < 1 && ($main::opt_web || $main::opt_svg)) {\n        # SVG output treats line widths < 1 poorly.\n        $w = 1;\n      }\n\n      # Dot sometimes segfaults if given edge weights that are too large, so\n      # we cap the weights at a large value\n      my $edgeweight = abs($n) ** 0.7;\n      if ($edgeweight > 100000) { $edgeweight = 100000; }\n      $edgeweight = int($edgeweight);\n\n      my $style = sprintf(\"setlinewidth(%f)\", $w);\n      if ($x[1] =~ m/\\(inline\\)/) {\n        $style .= \",dashed\";\n      }\n\n      # Use a slightly squashed function of the edge count as the weight\n      printf DOT (\"N%s -> N%s [label=%s, weight=%d, style=\\\"%s\\\"];\\n\",\n                  $node{$x[0]},\n                  $node{$x[1]},\n                  Unparse($n),\n                  $edgeweight,\n                  $style);\n    }\n  }\n\n  print DOT (\"}\\n\");\n  close(DOT);\n\n  if ($main::opt_web || $main::opt_svg) {\n    # Rewrite SVG to be more usable inside web browser.\n    RewriteSvg(TempName($main::next_tmpfile, \"svg\"));\n  }\n\n  return 1;\n}\n\nsub RewriteSvg {\n  my $svgfile = shift;\n\n  open(SVG, $svgfile) || die \"open temp svg: $!\";\n  my @svg = <SVG>;\n  close(SVG);\n  unlink $svgfile;\n  my $svg = join('', @svg);\n\n  # Dot's SVG output is\n  #\n  #    <svg width=\"___\" height=\"___\"\n  #     viewBox=\"___\" xmlns=...>\n  #    <g id=\"graph0\" transform=\"...\">\n  #    ...\n  #    </g>\n  #    </svg>\n  #\n  # Change it to\n  #\n  #    <svg width=\"100%\" height=\"100%\"\n  #     xmlns=...>\n  #    $svg_javascript\n  #    <g id=\"viewport\" transform=\"translate(0,0)\">\n  #    <g id=\"graph0\" transform=\"...\">\n  #    ...\n  #    </g>\n  #    </g>\n  #    </svg>\n\n  # Fix width, height; drop viewBox.\n  $svg =~ s/(?s)<svg width=\"[^\"]+\" height=\"[^\"]+\"(.*?)viewBox=\"[^\"]+\"/<svg width=\"100%\" height=\"100%\"$1/;\n\n  # Insert script, viewport <g> above first <g>\n  my $svg_javascript = SvgJavascript();\n  my $viewport = \"<g id=\\\"viewport\\\" transform=\\\"translate(0,0)\\\">\\n\";\n  $svg =~ s/<g id=\"graph\\d\"/$svg_javascript$viewport$&/;\n\n  # Insert final </g> above </svg>.\n  $svg =~ s/(.*)(<\\/svg>)/$1<\\/g>$2/;\n  $svg =~ s/<g id=\"graph\\d\"(.*?)/<g id=\"viewport\"$1/;\n\n  if ($main::opt_svg) {\n    # --svg: write to standard output.\n    print $svg;\n  } else {\n    # Write back to temporary file.\n    open(SVG, \">$svgfile\") || die \"open $svgfile: $!\";\n    print SVG $svg;\n    close(SVG);\n  }\n}\n\nsub SvgJavascript {\n  return <<'EOF';\n<script type=\"text/ecmascript\"><![CDATA[\n// SVGPan\n// http://www.cyberz.org/blog/2009/12/08/svgpan-a-javascript-svg-panzoomdrag-library/\n// Local modification: if(true || ...) below to force panning, never moving.\n\n/**\n *  SVGPan library 1.2\n * ====================\n *\n * Given an unique existing element with id \"viewport\", including the\n * the library into any SVG adds the following capabilities:\n *\n *  - Mouse panning\n *  - Mouse zooming (using the wheel)\n *  - Object dargging\n *\n * Known issues:\n *\n *  - Zooming (while panning) on Safari has still some issues\n *\n * Releases:\n *\n * 1.2, Sat Mar 20 08:42:50 GMT 2010, Zeng Xiaohui\n *\tFixed a bug with browser mouse handler interaction\n *\n * 1.1, Wed Feb  3 17:39:33 GMT 2010, Zeng Xiaohui\n *\tUpdated the zoom code to support the mouse wheel on Safari/Chrome\n *\n * 1.0, Andrea Leofreddi\n *\tFirst release\n *\n * This code is licensed under the following BSD license:\n *\n * Copyright 2009-2010 Andrea Leofreddi <a.leofreddi@itcharm.com>. All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without modification, are\n * permitted provided that the following conditions are met:\n *\n *    1. Redistributions of source code must retain the above copyright notice, this list of\n *       conditions and the following disclaimer.\n *\n *    2. Redistributions in binary form must reproduce the above copyright notice, this list\n *       of conditions and the following disclaimer in the documentation and/or other materials\n *       provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY Andrea Leofreddi ``AS IS'' AND ANY EXPRESS OR IMPLIED\n * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND\n * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Andrea Leofreddi OR\n * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\n * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\n * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n *\n * The views and conclusions contained in the software and documentation are those of the\n * authors and should not be interpreted as representing official policies, either expressed\n * or implied, of Andrea Leofreddi.\n */\n\nvar root = document.documentElement;\n\nvar state = 'none', stateTarget, stateOrigin, stateTf;\n\nsetupHandlers(root);\n\n/**\n * Register handlers\n */\nfunction setupHandlers(root){\n\tsetAttributes(root, {\n\t\t\"onmouseup\" : \"add(evt)\",\n\t\t\"onmousedown\" : \"handleMouseDown(evt)\",\n\t\t\"onmousemove\" : \"handleMouseMove(evt)\",\n\t\t\"onmouseup\" : \"handleMouseUp(evt)\",\n\t\t//\"onmouseout\" : \"handleMouseUp(evt)\", // Decomment this to stop the pan functionality when dragging out of the SVG element\n\t});\n\n\tif(navigator.userAgent.toLowerCase().indexOf('webkit') >= 0)\n\t\twindow.addEventListener('mousewheel', handleMouseWheel, false); // Chrome/Safari\n\telse\n\t\twindow.addEventListener('DOMMouseScroll', handleMouseWheel, false); // Others\n\n\tvar g = svgDoc.getElementById(\"svg\");\n\tg.width = \"100%\";\n\tg.height = \"100%\";\n}\n\n/**\n * Instance an SVGPoint object with given event coordinates.\n */\nfunction getEventPoint(evt) {\n\tvar p = root.createSVGPoint();\n\n\tp.x = evt.clientX;\n\tp.y = evt.clientY;\n\n\treturn p;\n}\n\n/**\n * Sets the current transform matrix of an element.\n */\nfunction setCTM(element, matrix) {\n\tvar s = \"matrix(\" + matrix.a + \",\" + matrix.b + \",\" + matrix.c + \",\" + matrix.d + \",\" + matrix.e + \",\" + matrix.f + \")\";\n\n\telement.setAttribute(\"transform\", s);\n}\n\n/**\n * Dumps a matrix to a string (useful for debug).\n */\nfunction dumpMatrix(matrix) {\n\tvar s = \"[ \" + matrix.a + \", \" + matrix.c + \", \" + matrix.e + \"\\n  \" + matrix.b + \", \" + matrix.d + \", \" + matrix.f + \"\\n  0, 0, 1 ]\";\n\n\treturn s;\n}\n\n/**\n * Sets attributes of an element.\n */\nfunction setAttributes(element, attributes){\n\tfor (i in attributes)\n\t\telement.setAttributeNS(null, i, attributes[i]);\n}\n\n/**\n * Handle mouse move event.\n */\nfunction handleMouseWheel(evt) {\n\tif(evt.preventDefault)\n\t\tevt.preventDefault();\n\n\tevt.returnValue = false;\n\n\tvar svgDoc = evt.target.ownerDocument;\n\n\tvar delta;\n\n\tif(evt.wheelDelta)\n\t\tdelta = evt.wheelDelta / 3600; // Chrome/Safari\n\telse\n\t\tdelta = evt.detail / -90; // Mozilla\n\n\tvar z = 1 + delta; // Zoom factor: 0.9/1.1\n\n\tvar g = svgDoc.getElementById(\"viewport\");\n\n\tvar p = getEventPoint(evt);\n\n\tp = p.matrixTransform(g.getCTM().inverse());\n\n\t// Compute new scale matrix in current mouse position\n\tvar k = root.createSVGMatrix().translate(p.x, p.y).scale(z).translate(-p.x, -p.y);\n\n        setCTM(g, g.getCTM().multiply(k));\n\n\tstateTf = stateTf.multiply(k.inverse());\n}\n\n/**\n * Handle mouse move event.\n */\nfunction handleMouseMove(evt) {\n\tif(evt.preventDefault)\n\t\tevt.preventDefault();\n\n\tevt.returnValue = false;\n\n\tvar svgDoc = evt.target.ownerDocument;\n\n\tvar g = svgDoc.getElementById(\"viewport\");\n\n\tif(state == 'pan') {\n\t\t// Pan mode\n\t\tvar p = getEventPoint(evt).matrixTransform(stateTf);\n\n\t\tsetCTM(g, stateTf.inverse().translate(p.x - stateOrigin.x, p.y - stateOrigin.y));\n\t} else if(state == 'move') {\n\t\t// Move mode\n\t\tvar p = getEventPoint(evt).matrixTransform(g.getCTM().inverse());\n\n\t\tsetCTM(stateTarget, root.createSVGMatrix().translate(p.x - stateOrigin.x, p.y - stateOrigin.y).multiply(g.getCTM().inverse()).multiply(stateTarget.getCTM()));\n\n\t\tstateOrigin = p;\n\t}\n}\n\n/**\n * Handle click event.\n */\nfunction handleMouseDown(evt) {\n\tif(evt.preventDefault)\n\t\tevt.preventDefault();\n\n\tevt.returnValue = false;\n\n\tvar svgDoc = evt.target.ownerDocument;\n\n\tvar g = svgDoc.getElementById(\"viewport\");\n\n\tif(true || evt.target.tagName == \"svg\") {\n\t\t// Pan mode\n\t\tstate = 'pan';\n\n\t\tstateTf = g.getCTM().inverse();\n\n\t\tstateOrigin = getEventPoint(evt).matrixTransform(stateTf);\n\t} else {\n\t\t// Move mode\n\t\tstate = 'move';\n\n\t\tstateTarget = evt.target;\n\n\t\tstateTf = g.getCTM().inverse();\n\n\t\tstateOrigin = getEventPoint(evt).matrixTransform(stateTf);\n\t}\n}\n\n/**\n * Handle mouse button release event.\n */\nfunction handleMouseUp(evt) {\n\tif(evt.preventDefault)\n\t\tevt.preventDefault();\n\n\tevt.returnValue = false;\n\n\tvar svgDoc = evt.target.ownerDocument;\n\n\tif(state == 'pan' || state == 'move') {\n\t\t// Quit pan mode\n\t\tstate = '';\n\t}\n}\n\n]]></script>\nEOF\n}\n\n# Provides a map from fullname to shortname for cases where the\n# shortname is ambiguous.  The symlist has both the fullname and\n# shortname for all symbols, which is usually fine, but sometimes --\n# such as overloaded functions -- two different fullnames can map to\n# the same shortname.  In that case, we use the address of the\n# function to disambiguate the two.  This function fills in a map that\n# maps fullnames to modified shortnames in such cases.  If a fullname\n# is not present in the map, the 'normal' shortname provided by the\n# symlist is the appropriate one to use.\nsub FillFullnameToShortnameMap {\n  my $symbols = shift;\n  my $fullname_to_shortname_map = shift;\n  my $shortnames_seen_once = {};\n  my $shortnames_seen_more_than_once = {};\n\n  foreach my $symlist (values(%{$symbols})) {\n    # TODO(csilvers): deal with inlined symbols too.\n    my $shortname = $symlist->[0];\n    my $fullname = $symlist->[2];\n    if ($fullname !~ /<[0-9a-fA-F]+>$/) {  # fullname doesn't end in an address\n      next;       # the only collisions we care about are when addresses differ\n    }\n    if (defined($shortnames_seen_once->{$shortname}) &&\n        $shortnames_seen_once->{$shortname} ne $fullname) {\n      $shortnames_seen_more_than_once->{$shortname} = 1;\n    } else {\n      $shortnames_seen_once->{$shortname} = $fullname;\n    }\n  }\n\n  foreach my $symlist (values(%{$symbols})) {\n    my $shortname = $symlist->[0];\n    my $fullname = $symlist->[2];\n    # TODO(csilvers): take in a list of addresses we care about, and only\n    # store in the map if $symlist->[1] is in that list.  Saves space.\n    next if defined($fullname_to_shortname_map->{$fullname});\n    if (defined($shortnames_seen_more_than_once->{$shortname})) {\n      if ($fullname =~ /<0*([^>]*)>$/) {   # fullname has address at end of it\n        $fullname_to_shortname_map->{$fullname} = \"$shortname\\@$1\";\n      }\n    }\n  }\n}\n\n# Return a small number that identifies the argument.\n# Multiple calls with the same argument will return the same number.\n# Calls with different arguments will return different numbers.\nsub ShortIdFor {\n  my $key = shift;\n  my $id = $main::uniqueid{$key};\n  if (!defined($id)) {\n    $id = keys(%main::uniqueid) + 1;\n    $main::uniqueid{$key} = $id;\n  }\n  return $id;\n}\n\n# Translate a stack of addresses into a stack of symbols\nsub TranslateStack {\n  my $symbols = shift;\n  my $fullname_to_shortname_map = shift;\n  my $k = shift;\n\n  my @addrs = split(/\\n/, $k);\n  my @result = ();\n  for (my $i = 0; $i <= $#addrs; $i++) {\n    my $a = $addrs[$i];\n\n    # Skip large addresses since they sometimes show up as fake entries on RH9\n    if (length($a) > 8 && $a gt \"7fffffffffffffff\") {\n      next;\n    }\n\n    if ($main::opt_disasm || $main::opt_list) {\n      # We want just the address for the key\n      push(@result, $a);\n      next;\n    }\n\n    my $symlist = $symbols->{$a};\n    if (!defined($symlist)) {\n      $symlist = [$a, \"\", $a];\n    }\n\n    # We can have a sequence of symbols for a particular entry\n    # (more than one symbol in the case of inlining).  Callers\n    # come before callees in symlist, so walk backwards since\n    # the translated stack should contain callees before callers.\n    for (my $j = $#{$symlist}; $j >= 2; $j -= 3) {\n      my $func = $symlist->[$j-2];\n      my $fileline = $symlist->[$j-1];\n      my $fullfunc = $symlist->[$j];\n      if (defined($fullname_to_shortname_map->{$fullfunc})) {\n        $func = $fullname_to_shortname_map->{$fullfunc};\n      }\n      if ($j > 2) {\n        $func = \"$func (inline)\";\n      }\n\n      # Do not merge nodes corresponding to Callback::Run since that\n      # causes confusing cycles in dot display.  Instead, we synthesize\n      # a unique name for this frame per caller.\n      if ($func =~ m/Callback.*::Run$/) {\n        my $caller = ($i > 0) ? $addrs[$i-1] : 0;\n        $func = \"Run#\" . ShortIdFor($caller);\n      }\n\n      if ($main::opt_addresses) {\n        push(@result, \"$a $func $fileline\");\n      } elsif ($main::opt_lines) {\n        if ($func eq '??' && $fileline eq '??:0') {\n          push(@result, \"$a\");\n        } else {\n          push(@result, \"$func $fileline\");\n        }\n      } elsif ($main::opt_functions) {\n        if ($func eq '??') {\n          push(@result, \"$a\");\n        } else {\n          push(@result, $func);\n        }\n      } elsif ($main::opt_files) {\n        if ($fileline eq '??:0' || $fileline eq '') {\n          push(@result, \"$a\");\n        } else {\n          my $f = $fileline;\n          $f =~ s/:\\d+$//;\n          push(@result, $f);\n        }\n      } else {\n        push(@result, $a);\n        last;  # Do not print inlined info\n      }\n    }\n  }\n\n  # print join(\",\", @addrs), \" => \", join(\",\", @result), \"\\n\";\n  return @result;\n}\n\n# Generate percent string for a number and a total\nsub Percent {\n  my $num = shift;\n  my $tot = shift;\n  if ($tot != 0) {\n    return sprintf(\"%.1f%%\", $num * 100.0 / $tot);\n  } else {\n    return ($num == 0) ? \"nan\" : (($num > 0) ? \"+inf\" : \"-inf\");\n  }\n}\n\n# Generate pretty-printed form of number\nsub Unparse {\n  my $num = shift;\n  if ($main::profile_type eq 'heap' || $main::profile_type eq 'growth') {\n    if ($main::opt_inuse_objects || $main::opt_alloc_objects) {\n      return sprintf(\"%d\", $num);\n    } else {\n      if ($main::opt_show_bytes) {\n        return sprintf(\"%d\", $num);\n      } else {\n        return sprintf(\"%.1f\", $num / 1048576.0);\n      }\n    }\n  } elsif ($main::profile_type eq 'contention' && !$main::opt_contentions) {\n    return sprintf(\"%.3f\", $num / 1e9); # Convert nanoseconds to seconds\n  } else {\n    return sprintf(\"%d\", $num);\n  }\n}\n\n# Alternate pretty-printed form: 0 maps to \".\"\nsub UnparseAlt {\n  my $num = shift;\n  if ($num == 0) {\n    return \".\";\n  } else {\n    return Unparse($num);\n  }\n}\n\n# Alternate pretty-printed form: 0 maps to \"\"\nsub HtmlPrintNumber {\n  my $num = shift;\n  if ($num == 0) {\n    return \"\";\n  } else {\n    return Unparse($num);\n  }\n}\n\n# Return output units\nsub Units {\n  if ($main::profile_type eq 'heap' || $main::profile_type eq 'growth') {\n    if ($main::opt_inuse_objects || $main::opt_alloc_objects) {\n      return \"objects\";\n    } else {\n      if ($main::opt_show_bytes) {\n        return \"B\";\n      } else {\n        return \"MB\";\n      }\n    }\n  } elsif ($main::profile_type eq 'contention' && !$main::opt_contentions) {\n    return \"seconds\";\n  } else {\n    return \"samples\";\n  }\n}\n\n##### Profile manipulation code #####\n\n# Generate flattened profile:\n# If count is charged to stack [a,b,c,d], in generated profile,\n# it will be charged to [a]\nsub FlatProfile {\n  my $profile = shift;\n  my $result = {};\n  foreach my $k (keys(%{$profile})) {\n    my $count = $profile->{$k};\n    my @addrs = split(/\\n/, $k);\n    if ($#addrs >= 0) {\n      AddEntry($result, $addrs[0], $count);\n    }\n  }\n  return $result;\n}\n\n# Generate cumulative profile:\n# If count is charged to stack [a,b,c,d], in generated profile,\n# it will be charged to [a], [b], [c], [d]\nsub CumulativeProfile {\n  my $profile = shift;\n  my $result = {};\n  foreach my $k (keys(%{$profile})) {\n    my $count = $profile->{$k};\n    my @addrs = split(/\\n/, $k);\n    foreach my $a (@addrs) {\n      AddEntry($result, $a, $count);\n    }\n  }\n  return $result;\n}\n\n# If the second-youngest PC on the stack is always the same, returns\n# that pc.  Otherwise, returns undef.\nsub IsSecondPcAlwaysTheSame {\n  my $profile = shift;\n\n  my $second_pc = undef;\n  foreach my $k (keys(%{$profile})) {\n    my @addrs = split(/\\n/, $k);\n    if ($#addrs < 1) {\n      return undef;\n    }\n    if (not defined $second_pc) {\n      $second_pc = $addrs[1];\n    } else {\n      if ($second_pc ne $addrs[1]) {\n        return undef;\n      }\n    }\n  }\n  return $second_pc;\n}\n\nsub ExtractSymbolLocation {\n  my $symbols = shift;\n  my $address = shift;\n  # 'addr2line' outputs \"??:0\" for unknown locations; we do the\n  # same to be consistent.\n  my $location = \"??:0:unknown\";\n  if (exists $symbols->{$address}) {\n    my $file = $symbols->{$address}->[1];\n    if ($file eq \"?\") {\n      $file = \"??:0\"\n    }\n    $location = $file . \":\" . $symbols->{$address}->[0];\n  }\n  return $location;\n}\n\n# Extracts a graph of calls.\nsub ExtractCalls {\n  my $symbols = shift;\n  my $profile = shift;\n\n  my $calls = {};\n  while( my ($stack_trace, $count) = each %$profile ) {\n    my @address = split(/\\n/, $stack_trace);\n    my $destination = ExtractSymbolLocation($symbols, $address[0]);\n    AddEntry($calls, $destination, $count);\n    for (my $i = 1; $i <= $#address; $i++) {\n      my $source = ExtractSymbolLocation($symbols, $address[$i]);\n      my $call = \"$source -> $destination\";\n      AddEntry($calls, $call, $count);\n      $destination = $source;\n    }\n  }\n\n  return $calls;\n}\n\nsub FilterFrames {\n  my $symbols = shift;\n  my $profile = shift;\n\n  if ($main::opt_retain eq '' && $main::opt_exclude eq '') {\n    return $profile;\n  }\n\n  my $result = {};\n  foreach my $k (keys(%{$profile})) {\n    my $count = $profile->{$k};\n    my @addrs = split(/\\n/, $k);\n    my @path = ();\n    foreach my $a (@addrs) {\n      my $sym;\n      if (exists($symbols->{$a})) {\n        $sym = $symbols->{$a}->[0];\n      } else {\n        $sym = $a;\n      }\n      if ($main::opt_retain ne '' && $sym !~ m/$main::opt_retain/) {\n        next;\n      }\n      if ($main::opt_exclude ne '' && $sym =~ m/$main::opt_exclude/) {\n        next;\n      }\n      push(@path, $a);\n    }\n    if (scalar(@path) > 0) {\n      my $reduced_path = join(\"\\n\", @path);\n      AddEntry($result, $reduced_path, $count);\n    }\n  }\n\n  return $result;\n}\n\nsub RemoveUninterestingFrames {\n  my $symbols = shift;\n  my $profile = shift;\n\n  # List of function names to skip\n  my %skip = ();\n  my $skip_regexp = 'NOMATCH';\n  if ($main::profile_type eq 'heap' || $main::profile_type eq 'growth') {\n    foreach my $name ('@JEMALLOC_PREFIX@calloc',\n                      'cfree',\n                      '@JEMALLOC_PREFIX@malloc',\n                      'newImpl',\n                      'void* newImpl',\n                      '@JEMALLOC_PREFIX@free',\n                      '@JEMALLOC_PREFIX@memalign',\n                      '@JEMALLOC_PREFIX@posix_memalign',\n                      '@JEMALLOC_PREFIX@aligned_alloc',\n                      'pvalloc',\n                      '@JEMALLOC_PREFIX@valloc',\n                      '@JEMALLOC_PREFIX@realloc',\n                      '@JEMALLOC_PREFIX@mallocx',\n                      '@JEMALLOC_PREFIX@rallocx',\n                      '@JEMALLOC_PREFIX@xallocx',\n                      '@JEMALLOC_PREFIX@dallocx',\n                      '@JEMALLOC_PREFIX@sdallocx',\n                      '@JEMALLOC_PREFIX@sdallocx_noflags',\n                      'tc_calloc',\n                      'tc_cfree',\n                      'tc_malloc',\n                      'tc_free',\n                      'tc_memalign',\n                      'tc_posix_memalign',\n                      'tc_pvalloc',\n                      'tc_valloc',\n                      'tc_realloc',\n                      'tc_new',\n                      'tc_delete',\n                      'tc_newarray',\n                      'tc_deletearray',\n                      'tc_new_nothrow',\n                      'tc_newarray_nothrow',\n                      'do_malloc',\n                      '::do_malloc',   # new name -- got moved to an unnamed ns\n                      '::do_malloc_or_cpp_alloc',\n                      'DoSampledAllocation',\n                      'simple_alloc::allocate',\n                      '__malloc_alloc_template::allocate',\n                      '__builtin_delete',\n                      '__builtin_new',\n                      '__builtin_vec_delete',\n                      '__builtin_vec_new',\n                      'operator new',\n                      'operator new[]',\n                      # The entry to our memory-allocation routines on OS X\n                      'malloc_zone_malloc',\n                      'malloc_zone_calloc',\n                      'malloc_zone_valloc',\n                      'malloc_zone_realloc',\n                      'malloc_zone_memalign',\n                      'malloc_zone_free',\n                      # These mark the beginning/end of our custom sections\n                      '__start_google_malloc',\n                      '__stop_google_malloc',\n                      '__start_malloc_hook',\n                      '__stop_malloc_hook') {\n      $skip{$name} = 1;\n      $skip{\"_\" . $name} = 1;   # Mach (OS X) adds a _ prefix to everything\n    }\n    # TODO: Remove TCMalloc once everything has been\n    # moved into the tcmalloc:: namespace and we have flushed\n    # old code out of the system.\n    $skip_regexp = \"TCMalloc|^tcmalloc::\";\n  } elsif ($main::profile_type eq 'contention') {\n    foreach my $vname ('base::RecordLockProfileData',\n                       'base::SubmitMutexProfileData',\n                       'base::SubmitSpinLockProfileData',\n                       'Mutex::Unlock',\n                       'Mutex::UnlockSlow',\n                       'Mutex::ReaderUnlock',\n                       'MutexLock::~MutexLock',\n                       'SpinLock::Unlock',\n                       'SpinLock::SlowUnlock',\n                       'SpinLockHolder::~SpinLockHolder') {\n      $skip{$vname} = 1;\n    }\n  } elsif ($main::profile_type eq 'cpu') {\n    # Drop signal handlers used for CPU profile collection\n    # TODO(dpeng): this should not be necessary; it's taken\n    # care of by the general 2nd-pc mechanism below.\n    foreach my $name ('ProfileData::Add',           # historical\n                      'ProfileData::prof_handler',  # historical\n                      'CpuProfiler::prof_handler',\n                      '__FRAME_END__',\n                      '__pthread_sighandler',\n                      '__restore') {\n      $skip{$name} = 1;\n    }\n  } else {\n    # Nothing skipped for unknown types\n  }\n\n  if ($main::profile_type eq 'cpu') {\n    # If all the second-youngest program counters are the same,\n    # this STRONGLY suggests that it is an artifact of measurement,\n    # i.e., stack frames pushed by the CPU profiler signal handler.\n    # Hence, we delete them.\n    # (The topmost PC is read from the signal structure, not from\n    # the stack, so it does not get involved.)\n    while (my $second_pc = IsSecondPcAlwaysTheSame($profile)) {\n      my $result = {};\n      my $func = '';\n      if (exists($symbols->{$second_pc})) {\n        $second_pc = $symbols->{$second_pc}->[0];\n      }\n      print STDERR \"Removing $second_pc from all stack traces.\\n\";\n      foreach my $k (keys(%{$profile})) {\n        my $count = $profile->{$k};\n        my @addrs = split(/\\n/, $k);\n        splice @addrs, 1, 1;\n        my $reduced_path = join(\"\\n\", @addrs);\n        AddEntry($result, $reduced_path, $count);\n      }\n      $profile = $result;\n    }\n  }\n\n  my $result = {};\n  foreach my $k (keys(%{$profile})) {\n    my $count = $profile->{$k};\n    my @addrs = split(/\\n/, $k);\n    my @path = ();\n    foreach my $a (@addrs) {\n      if (exists($symbols->{$a})) {\n        my $func = $symbols->{$a}->[0];\n        if ($skip{$func} || ($func =~ m/$skip_regexp/)) {\n          # Throw away the portion of the backtrace seen so far, under the\n          # assumption that previous frames were for functions internal to the\n          # allocator.\n          @path = ();\n          next;\n        }\n      }\n      push(@path, $a);\n    }\n    my $reduced_path = join(\"\\n\", @path);\n    AddEntry($result, $reduced_path, $count);\n  }\n\n  $result = FilterFrames($symbols, $result);\n\n  return $result;\n}\n\n# Reduce profile to granularity given by user\nsub ReduceProfile {\n  my $symbols = shift;\n  my $profile = shift;\n  my $result = {};\n  my $fullname_to_shortname_map = {};\n  FillFullnameToShortnameMap($symbols, $fullname_to_shortname_map);\n  foreach my $k (keys(%{$profile})) {\n    my $count = $profile->{$k};\n    my @translated = TranslateStack($symbols, $fullname_to_shortname_map, $k);\n    my @path = ();\n    my %seen = ();\n    $seen{''} = 1;      # So that empty keys are skipped\n    foreach my $e (@translated) {\n      # To avoid double-counting due to recursion, skip a stack-trace\n      # entry if it has already been seen\n      if (!$seen{$e}) {\n        $seen{$e} = 1;\n        push(@path, $e);\n      }\n    }\n    my $reduced_path = join(\"\\n\", @path);\n    AddEntry($result, $reduced_path, $count);\n  }\n  return $result;\n}\n\n# Does the specified symbol array match the regexp?\nsub SymbolMatches {\n  my $sym = shift;\n  my $re = shift;\n  if (defined($sym)) {\n    for (my $i = 0; $i < $#{$sym}; $i += 3) {\n      if ($sym->[$i] =~ m/$re/ || $sym->[$i+1] =~ m/$re/) {\n        return 1;\n      }\n    }\n  }\n  return 0;\n}\n\n# Focus only on paths involving specified regexps\nsub FocusProfile {\n  my $symbols = shift;\n  my $profile = shift;\n  my $focus = shift;\n  my $result = {};\n  foreach my $k (keys(%{$profile})) {\n    my $count = $profile->{$k};\n    my @addrs = split(/\\n/, $k);\n    foreach my $a (@addrs) {\n      # Reply if it matches either the address/shortname/fileline\n      if (($a =~ m/$focus/) || SymbolMatches($symbols->{$a}, $focus)) {\n        AddEntry($result, $k, $count);\n        last;\n      }\n    }\n  }\n  return $result;\n}\n\n# Focus only on paths not involving specified regexps\nsub IgnoreProfile {\n  my $symbols = shift;\n  my $profile = shift;\n  my $ignore = shift;\n  my $result = {};\n  foreach my $k (keys(%{$profile})) {\n    my $count = $profile->{$k};\n    my @addrs = split(/\\n/, $k);\n    my $matched = 0;\n    foreach my $a (@addrs) {\n      # Reply if it matches either the address/shortname/fileline\n      if (($a =~ m/$ignore/) || SymbolMatches($symbols->{$a}, $ignore)) {\n        $matched = 1;\n        last;\n      }\n    }\n    if (!$matched) {\n      AddEntry($result, $k, $count);\n    }\n  }\n  return $result;\n}\n\n# Get total count in profile\nsub TotalProfile {\n  my $profile = shift;\n  my $result = 0;\n  foreach my $k (keys(%{$profile})) {\n    $result += $profile->{$k};\n  }\n  return $result;\n}\n\n# Add A to B\nsub AddProfile {\n  my $A = shift;\n  my $B = shift;\n\n  my $R = {};\n  # add all keys in A\n  foreach my $k (keys(%{$A})) {\n    my $v = $A->{$k};\n    AddEntry($R, $k, $v);\n  }\n  # add all keys in B\n  foreach my $k (keys(%{$B})) {\n    my $v = $B->{$k};\n    AddEntry($R, $k, $v);\n  }\n  return $R;\n}\n\n# Merges symbol maps\nsub MergeSymbols {\n  my $A = shift;\n  my $B = shift;\n\n  my $R = {};\n  foreach my $k (keys(%{$A})) {\n    $R->{$k} = $A->{$k};\n  }\n  if (defined($B)) {\n    foreach my $k (keys(%{$B})) {\n      $R->{$k} = $B->{$k};\n    }\n  }\n  return $R;\n}\n\n\n# Add A to B\nsub AddPcs {\n  my $A = shift;\n  my $B = shift;\n\n  my $R = {};\n  # add all keys in A\n  foreach my $k (keys(%{$A})) {\n    $R->{$k} = 1\n  }\n  # add all keys in B\n  foreach my $k (keys(%{$B})) {\n    $R->{$k} = 1\n  }\n  return $R;\n}\n\n# Subtract B from A\nsub SubtractProfile {\n  my $A = shift;\n  my $B = shift;\n\n  my $R = {};\n  foreach my $k (keys(%{$A})) {\n    my $v = $A->{$k} - GetEntry($B, $k);\n    if ($v < 0 && $main::opt_drop_negative) {\n      $v = 0;\n    }\n    AddEntry($R, $k, $v);\n  }\n  if (!$main::opt_drop_negative) {\n    # Take care of when subtracted profile has more entries\n    foreach my $k (keys(%{$B})) {\n      if (!exists($A->{$k})) {\n        AddEntry($R, $k, 0 - $B->{$k});\n      }\n    }\n  }\n  return $R;\n}\n\n# Get entry from profile; zero if not present\nsub GetEntry {\n  my $profile = shift;\n  my $k = shift;\n  if (exists($profile->{$k})) {\n    return $profile->{$k};\n  } else {\n    return 0;\n  }\n}\n\n# Add entry to specified profile\nsub AddEntry {\n  my $profile = shift;\n  my $k = shift;\n  my $n = shift;\n  if (!exists($profile->{$k})) {\n    $profile->{$k} = 0;\n  }\n  $profile->{$k} += $n;\n}\n\n# Add a stack of entries to specified profile, and add them to the $pcs\n# list.\nsub AddEntries {\n  my $profile = shift;\n  my $pcs = shift;\n  my $stack = shift;\n  my $count = shift;\n  my @k = ();\n\n  foreach my $e (split(/\\s+/, $stack)) {\n    my $pc = HexExtend($e);\n    $pcs->{$pc} = 1;\n    push @k, $pc;\n  }\n  AddEntry($profile, (join \"\\n\", @k), $count);\n}\n\n##### Code to profile a server dynamically #####\n\nsub CheckSymbolPage {\n  my $url = SymbolPageURL();\n  my $command = ShellEscape(@URL_FETCHER, $url);\n  open(SYMBOL, \"$command |\") or error($command);\n  my $line = <SYMBOL>;\n  $line =~ s/\\r//g;         # turn windows-looking lines into unix-looking lines\n  close(SYMBOL);\n  unless (defined($line)) {\n    error(\"$url doesn't exist\\n\");\n  }\n\n  if ($line =~ /^num_symbols:\\s+(\\d+)$/) {\n    if ($1 == 0) {\n      error(\"Stripped binary. No symbols available.\\n\");\n    }\n  } else {\n    error(\"Failed to get the number of symbols from $url\\n\");\n  }\n}\n\nsub IsProfileURL {\n  my $profile_name = shift;\n  if (-f $profile_name) {\n    printf STDERR \"Using local file $profile_name.\\n\";\n    return 0;\n  }\n  return 1;\n}\n\nsub ParseProfileURL {\n  my $profile_name = shift;\n\n  if (!defined($profile_name) || $profile_name eq \"\") {\n    return ();\n  }\n\n  # Split profile URL - matches all non-empty strings, so no test.\n  $profile_name =~ m,^(https?://)?([^/]+)(.*?)(/|$PROFILES)?$,;\n\n  my $proto = $1 || \"http://\";\n  my $hostport = $2;\n  my $prefix = $3;\n  my $profile = $4 || \"/\";\n\n  my $host = $hostport;\n  $host =~ s/:.*//;\n\n  my $baseurl = \"$proto$hostport$prefix\";\n  return ($host, $baseurl, $profile);\n}\n\n# We fetch symbols from the first profile argument.\nsub SymbolPageURL {\n  my ($host, $baseURL, $path) = ParseProfileURL($main::pfile_args[0]);\n  return \"$baseURL$SYMBOL_PAGE\";\n}\n\nsub FetchProgramName() {\n  my ($host, $baseURL, $path) = ParseProfileURL($main::pfile_args[0]);\n  my $url = \"$baseURL$PROGRAM_NAME_PAGE\";\n  my $command_line = ShellEscape(@URL_FETCHER, $url);\n  open(CMDLINE, \"$command_line |\") or error($command_line);\n  my $cmdline = <CMDLINE>;\n  $cmdline =~ s/\\r//g;   # turn windows-looking lines into unix-looking lines\n  close(CMDLINE);\n  error(\"Failed to get program name from $url\\n\") unless defined($cmdline);\n  $cmdline =~ s/\\x00.+//;  # Remove argv[1] and latters.\n  $cmdline =~ s!\\n!!g;  # Remove LFs.\n  return $cmdline;\n}\n\n# Gee, curl's -L (--location) option isn't reliable at least\n# with its 7.12.3 version.  Curl will forget to post data if\n# there is a redirection.  This function is a workaround for\n# curl.  Redirection happens on borg hosts.\nsub ResolveRedirectionForCurl {\n  my $url = shift;\n  my $command_line = ShellEscape(@URL_FETCHER, \"--head\", $url);\n  open(CMDLINE, \"$command_line |\") or error($command_line);\n  while (<CMDLINE>) {\n    s/\\r//g;         # turn windows-looking lines into unix-looking lines\n    if (/^Location: (.*)/) {\n      $url = $1;\n    }\n  }\n  close(CMDLINE);\n  return $url;\n}\n\n# Add a timeout flat to URL_FETCHER.  Returns a new list.\nsub AddFetchTimeout {\n  my $timeout = shift;\n  my @fetcher = @_;\n  if (defined($timeout)) {\n    if (join(\" \", @fetcher) =~ m/\\bcurl -s/) {\n      push(@fetcher, \"--max-time\", sprintf(\"%d\", $timeout));\n    } elsif (join(\" \", @fetcher) =~ m/\\brpcget\\b/) {\n      push(@fetcher, sprintf(\"--deadline=%d\", $timeout));\n    }\n  }\n  return @fetcher;\n}\n\n# Reads a symbol map from the file handle name given as $1, returning\n# the resulting symbol map.  Also processes variables relating to symbols.\n# Currently, the only variable processed is 'binary=<value>' which updates\n# $main::prog to have the correct program name.\nsub ReadSymbols {\n  my $in = shift;\n  my $map = {};\n  while (<$in>) {\n    s/\\r//g;         # turn windows-looking lines into unix-looking lines\n    # Removes all the leading zeroes from the symbols, see comment below.\n    if (m/^0x0*([0-9a-f]+)\\s+(.+)/) {\n      $map->{$1} = $2;\n    } elsif (m/^---/) {\n      last;\n    } elsif (m/^([a-z][^=]*)=(.*)$/ ) {\n      my ($variable, $value) = ($1, $2);\n      for ($variable, $value) {\n        s/^\\s+//;\n        s/\\s+$//;\n      }\n      if ($variable eq \"binary\") {\n        if ($main::prog ne $UNKNOWN_BINARY && $main::prog ne $value) {\n          printf STDERR (\"Warning: Mismatched binary name '%s', using '%s'.\\n\",\n                         $main::prog, $value);\n        }\n        $main::prog = $value;\n      } else {\n        printf STDERR (\"Ignoring unknown variable in symbols list: \" .\n            \"'%s' = '%s'\\n\", $variable, $value);\n      }\n    }\n  }\n  return $map;\n}\n\nsub URLEncode {\n  my $str = shift;\n  $str =~ s/([^A-Za-z0-9\\-_.!~*'()])/ sprintf \"%%%02x\", ord $1 /eg;\n  return $str;\n}\n\nsub AppendSymbolFilterParams {\n  my $url = shift;\n  my @params = ();\n  if ($main::opt_retain ne '') {\n    push(@params, sprintf(\"retain=%s\", URLEncode($main::opt_retain)));\n  }\n  if ($main::opt_exclude ne '') {\n    push(@params, sprintf(\"exclude=%s\", URLEncode($main::opt_exclude)));\n  }\n  if (scalar @params > 0) {\n    $url = sprintf(\"%s?%s\", $url, join(\"&\", @params));\n  }\n  return $url;\n}\n\n# Fetches and processes symbols to prepare them for use in the profile output\n# code.  If the optional 'symbol_map' arg is not given, fetches symbols from\n# $SYMBOL_PAGE for all PC values found in profile.  Otherwise, the raw symbols\n# are assumed to have already been fetched into 'symbol_map' and are simply\n# extracted and processed.\nsub FetchSymbols {\n  my $pcset = shift;\n  my $symbol_map = shift;\n\n  my %seen = ();\n  my @pcs = grep { !$seen{$_}++ } keys(%$pcset);  # uniq\n\n  if (!defined($symbol_map)) {\n    my $post_data = join(\"+\", sort((map {\"0x\" . \"$_\"} @pcs)));\n\n    open(POSTFILE, \">$main::tmpfile_sym\");\n    print POSTFILE $post_data;\n    close(POSTFILE);\n\n    my $url = SymbolPageURL();\n\n    my $command_line;\n    if (join(\" \", @URL_FETCHER) =~ m/\\bcurl -s/) {\n      $url = ResolveRedirectionForCurl($url);\n      $url = AppendSymbolFilterParams($url);\n      $command_line = ShellEscape(@URL_FETCHER, \"-d\", \"\\@$main::tmpfile_sym\",\n                                  $url);\n    } else {\n      $url = AppendSymbolFilterParams($url);\n      $command_line = (ShellEscape(@URL_FETCHER, \"--post\", $url)\n                       . \" < \" . ShellEscape($main::tmpfile_sym));\n    }\n    # We use c++filt in case $SYMBOL_PAGE gives us mangled symbols.\n    my $escaped_cppfilt = ShellEscape($obj_tool_map{\"c++filt\"});\n    open(SYMBOL, \"$command_line | $escaped_cppfilt |\") or error($command_line);\n    $symbol_map = ReadSymbols(*SYMBOL{IO});\n    close(SYMBOL);\n  }\n\n  my $symbols = {};\n  foreach my $pc (@pcs) {\n    my $fullname;\n    # For 64 bits binaries, symbols are extracted with 8 leading zeroes.\n    # Then /symbol reads the long symbols in as uint64, and outputs\n    # the result with a \"0x%08llx\" format which get rid of the zeroes.\n    # By removing all the leading zeroes in both $pc and the symbols from\n    # /symbol, the symbols match and are retrievable from the map.\n    my $shortpc = $pc;\n    $shortpc =~ s/^0*//;\n    # Each line may have a list of names, which includes the function\n    # and also other functions it has inlined.  They are separated (in\n    # PrintSymbolizedProfile), by --, which is illegal in function names.\n    my $fullnames;\n    if (defined($symbol_map->{$shortpc})) {\n      $fullnames = $symbol_map->{$shortpc};\n    } else {\n      $fullnames = \"0x\" . $pc;  # Just use addresses\n    }\n    my $sym = [];\n    $symbols->{$pc} = $sym;\n    foreach my $fullname (split(\"--\", $fullnames)) {\n      my $name = ShortFunctionName($fullname);\n      push(@{$sym}, $name, \"?\", $fullname);\n    }\n  }\n  return $symbols;\n}\n\nsub BaseName {\n  my $file_name = shift;\n  $file_name =~ s!^.*/!!;  # Remove directory name\n  return $file_name;\n}\n\nsub MakeProfileBaseName {\n  my ($binary_name, $profile_name) = @_;\n  my ($host, $baseURL, $path) = ParseProfileURL($profile_name);\n  my $binary_shortname = BaseName($binary_name);\n  return sprintf(\"%s.%s.%s\",\n                 $binary_shortname, $main::op_time, $host);\n}\n\nsub FetchDynamicProfile {\n  my $binary_name = shift;\n  my $profile_name = shift;\n  my $fetch_name_only = shift;\n  my $encourage_patience = shift;\n\n  if (!IsProfileURL($profile_name)) {\n    return $profile_name;\n  } else {\n    my ($host, $baseURL, $path) = ParseProfileURL($profile_name);\n    if ($path eq \"\" || $path eq \"/\") {\n      # Missing type specifier defaults to cpu-profile\n      $path = $PROFILE_PAGE;\n    }\n\n    my $profile_file = MakeProfileBaseName($binary_name, $profile_name);\n\n    my $url = \"$baseURL$path\";\n    my $fetch_timeout = undef;\n    if ($path =~ m/$PROFILE_PAGE|$PMUPROFILE_PAGE/) {\n      if ($path =~ m/[?]/) {\n        $url .= \"&\";\n      } else {\n        $url .= \"?\";\n      }\n      $url .= sprintf(\"seconds=%d\", $main::opt_seconds);\n      $fetch_timeout = $main::opt_seconds * 1.01 + 60;\n      # Set $profile_type for consumption by PrintSymbolizedProfile.\n      $main::profile_type = 'cpu';\n    } else {\n      # For non-CPU profiles, we add a type-extension to\n      # the target profile file name.\n      my $suffix = $path;\n      $suffix =~ s,/,.,g;\n      $profile_file .= $suffix;\n      # Set $profile_type for consumption by PrintSymbolizedProfile.\n      if ($path =~ m/$HEAP_PAGE/) {\n        $main::profile_type = 'heap';\n      } elsif ($path =~ m/$GROWTH_PAGE/) {\n        $main::profile_type = 'growth';\n      } elsif ($path =~ m/$CONTENTION_PAGE/) {\n        $main::profile_type = 'contention';\n      }\n    }\n\n    my $profile_dir = $ENV{\"JEPROF_TMPDIR\"} || ($ENV{HOME} . \"/jeprof\");\n    if (! -d $profile_dir) {\n      mkdir($profile_dir)\n          || die(\"Unable to create profile directory $profile_dir: $!\\n\");\n    }\n    my $tmp_profile = \"$profile_dir/.tmp.$profile_file\";\n    my $real_profile = \"$profile_dir/$profile_file\";\n\n    if ($fetch_name_only > 0) {\n      return $real_profile;\n    }\n\n    my @fetcher = AddFetchTimeout($fetch_timeout, @URL_FETCHER);\n    my $cmd = ShellEscape(@fetcher, $url) . \" > \" . ShellEscape($tmp_profile);\n    if ($path =~ m/$PROFILE_PAGE|$PMUPROFILE_PAGE|$CENSUSPROFILE_PAGE/){\n      print STDERR \"Gathering CPU profile from $url for $main::opt_seconds seconds to\\n  ${real_profile}\\n\";\n      if ($encourage_patience) {\n        print STDERR \"Be patient...\\n\";\n      }\n    } else {\n      print STDERR \"Fetching $path profile from $url to\\n  ${real_profile}\\n\";\n    }\n\n    (system($cmd) == 0) || error(\"Failed to get profile: $cmd: $!\\n\");\n    (system(\"mv\", $tmp_profile, $real_profile) == 0) || error(\"Unable to rename profile\\n\");\n    print STDERR \"Wrote profile to $real_profile\\n\";\n    $main::collected_profile = $real_profile;\n    return $main::collected_profile;\n  }\n}\n\n# Collect profiles in parallel\nsub FetchDynamicProfiles {\n  my $items = scalar(@main::pfile_args);\n  my $levels = log($items) / log(2);\n\n  if ($items == 1) {\n    $main::profile_files[0] = FetchDynamicProfile($main::prog, $main::pfile_args[0], 0, 1);\n  } else {\n    # math rounding issues\n    if ((2 ** $levels) < $items) {\n     $levels++;\n    }\n    my $count = scalar(@main::pfile_args);\n    for (my $i = 0; $i < $count; $i++) {\n      $main::profile_files[$i] = FetchDynamicProfile($main::prog, $main::pfile_args[$i], 1, 0);\n    }\n    print STDERR \"Fetching $count profiles, Be patient...\\n\";\n    FetchDynamicProfilesRecurse($levels, 0, 0);\n    $main::collected_profile = join(\" \\\\\\n    \", @main::profile_files);\n  }\n}\n\n# Recursively fork a process to get enough processes\n# collecting profiles\nsub FetchDynamicProfilesRecurse {\n  my $maxlevel = shift;\n  my $level = shift;\n  my $position = shift;\n\n  if (my $pid = fork()) {\n    $position = 0 | ($position << 1);\n    TryCollectProfile($maxlevel, $level, $position);\n    wait;\n  } else {\n    $position = 1 | ($position << 1);\n    TryCollectProfile($maxlevel, $level, $position);\n    cleanup();\n    exit(0);\n  }\n}\n\n# Collect a single profile\nsub TryCollectProfile {\n  my $maxlevel = shift;\n  my $level = shift;\n  my $position = shift;\n\n  if ($level >= ($maxlevel - 1)) {\n    if ($position < scalar(@main::pfile_args)) {\n      FetchDynamicProfile($main::prog, $main::pfile_args[$position], 0, 0);\n    }\n  } else {\n    FetchDynamicProfilesRecurse($maxlevel, $level+1, $position);\n  }\n}\n\n##### Parsing code #####\n\n# Provide a small streaming-read module to handle very large\n# cpu-profile files.  Stream in chunks along a sliding window.\n# Provides an interface to get one 'slot', correctly handling\n# endian-ness differences.  A slot is one 32-bit or 64-bit word\n# (depending on the input profile).  We tell endianness and bit-size\n# for the profile by looking at the first 8 bytes: in cpu profiles,\n# the second slot is always 3 (we'll accept anything that's not 0).\nBEGIN {\n  package CpuProfileStream;\n\n  sub new {\n    my ($class, $file, $fname) = @_;\n    my $self = { file        => $file,\n                 base        => 0,\n                 stride      => 512 * 1024,   # must be a multiple of bitsize/8\n                 slots       => [],\n                 unpack_code => \"\",           # N for big-endian, V for little\n                 perl_is_64bit => 1,          # matters if profile is 64-bit\n    };\n    bless $self, $class;\n    # Let unittests adjust the stride\n    if ($main::opt_test_stride > 0) {\n      $self->{stride} = $main::opt_test_stride;\n    }\n    # Read the first two slots to figure out bitsize and endianness.\n    my $slots = $self->{slots};\n    my $str;\n    read($self->{file}, $str, 8);\n    # Set the global $address_length based on what we see here.\n    # 8 is 32-bit (8 hexadecimal chars); 16 is 64-bit (16 hexadecimal chars).\n    $address_length = ($str eq (chr(0)x8)) ? 16 : 8;\n    if ($address_length == 8) {\n      if (substr($str, 6, 2) eq chr(0)x2) {\n        $self->{unpack_code} = 'V';  # Little-endian.\n      } elsif (substr($str, 4, 2) eq chr(0)x2) {\n        $self->{unpack_code} = 'N';  # Big-endian\n      } else {\n        ::error(\"$fname: header size >= 2**16\\n\");\n      }\n      @$slots = unpack($self->{unpack_code} . \"*\", $str);\n    } else {\n      # If we're a 64-bit profile, check if we're a 64-bit-capable\n      # perl.  Otherwise, each slot will be represented as a float\n      # instead of an int64, losing precision and making all the\n      # 64-bit addresses wrong.  We won't complain yet, but will\n      # later if we ever see a value that doesn't fit in 32 bits.\n      my $has_q = 0;\n      eval { $has_q = pack(\"Q\", \"1\") ? 1 : 1; };\n      if (!$has_q) {\n        $self->{perl_is_64bit} = 0;\n      }\n      read($self->{file}, $str, 8);\n      if (substr($str, 4, 4) eq chr(0)x4) {\n        # We'd love to use 'Q', but it's a) not universal, b) not endian-proof.\n        $self->{unpack_code} = 'V';  # Little-endian.\n      } elsif (substr($str, 0, 4) eq chr(0)x4) {\n        $self->{unpack_code} = 'N';  # Big-endian\n      } else {\n        ::error(\"$fname: header size >= 2**32\\n\");\n      }\n      my @pair = unpack($self->{unpack_code} . \"*\", $str);\n      # Since we know one of the pair is 0, it's fine to just add them.\n      @$slots = (0, $pair[0] + $pair[1]);\n    }\n    return $self;\n  }\n\n  # Load more data when we access slots->get(X) which is not yet in memory.\n  sub overflow {\n    my ($self) = @_;\n    my $slots = $self->{slots};\n    $self->{base} += $#$slots + 1;   # skip over data we're replacing\n    my $str;\n    read($self->{file}, $str, $self->{stride});\n    if ($address_length == 8) {      # the 32-bit case\n      # This is the easy case: unpack provides 32-bit unpacking primitives.\n      @$slots = unpack($self->{unpack_code} . \"*\", $str);\n    } else {\n      # We need to unpack 32 bits at a time and combine.\n      my @b32_values = unpack($self->{unpack_code} . \"*\", $str);\n      my @b64_values = ();\n      for (my $i = 0; $i < $#b32_values; $i += 2) {\n        # TODO(csilvers): if this is a 32-bit perl, the math below\n        #    could end up in a too-large int, which perl will promote\n        #    to a double, losing necessary precision.  Deal with that.\n        #    Right now, we just die.\n        my ($lo, $hi) = ($b32_values[$i], $b32_values[$i+1]);\n        if ($self->{unpack_code} eq 'N') {    # big-endian\n          ($lo, $hi) = ($hi, $lo);\n        }\n        my $value = $lo + $hi * (2**32);\n        if (!$self->{perl_is_64bit} &&   # check value is exactly represented\n            (($value % (2**32)) != $lo || int($value / (2**32)) != $hi)) {\n          ::error(\"Need a 64-bit perl to process this 64-bit profile.\\n\");\n        }\n        push(@b64_values, $value);\n      }\n      @$slots = @b64_values;\n    }\n  }\n\n  # Access the i-th long in the file (logically), or -1 at EOF.\n  sub get {\n    my ($self, $idx) = @_;\n    my $slots = $self->{slots};\n    while ($#$slots >= 0) {\n      if ($idx < $self->{base}) {\n        # The only time we expect a reference to $slots[$i - something]\n        # after referencing $slots[$i] is reading the very first header.\n        # Since $stride > |header|, that shouldn't cause any lookback\n        # errors.  And everything after the header is sequential.\n        print STDERR \"Unexpected look-back reading CPU profile\";\n        return -1;   # shrug, don't know what better to return\n      } elsif ($idx > $self->{base} + $#$slots) {\n        $self->overflow();\n      } else {\n        return $slots->[$idx - $self->{base}];\n      }\n    }\n    # If we get here, $slots is [], which means we've reached EOF\n    return -1;  # unique since slots is supposed to hold unsigned numbers\n  }\n}\n\n# Reads the top, 'header' section of a profile, and returns the last\n# line of the header, commonly called a 'header line'.  The header\n# section of a profile consists of zero or more 'command' lines that\n# are instructions to jeprof, which jeprof executes when reading the\n# header.  All 'command' lines start with a %.  After the command\n# lines is the 'header line', which is a profile-specific line that\n# indicates what type of profile it is, and perhaps other global\n# information about the profile.  For instance, here's a header line\n# for a heap profile:\n#   heap profile:     53:    38236 [  5525:  1284029] @ heapprofile\n# For historical reasons, the CPU profile does not contain a text-\n# readable header line.  If the profile looks like a CPU profile,\n# this function returns \"\".  If no header line could be found, this\n# function returns undef.\n#\n# The following commands are recognized:\n#   %warn -- emit the rest of this line to stderr, prefixed by 'WARNING:'\n#\n# The input file should be in binmode.\nsub ReadProfileHeader {\n  local *PROFILE = shift;\n  my $firstchar = \"\";\n  my $line = \"\";\n  read(PROFILE, $firstchar, 1);\n  seek(PROFILE, -1, 1);                    # unread the firstchar\n  if ($firstchar !~ /[[:print:]]/) {       # is not a text character\n    return \"\";\n  }\n  while (defined($line = <PROFILE>)) {\n    $line =~ s/\\r//g;   # turn windows-looking lines into unix-looking lines\n    if ($line =~ /^%warn\\s+(.*)/) {        # 'warn' command\n      # Note this matches both '%warn blah\\n' and '%warn\\n'.\n      print STDERR \"WARNING: $1\\n\";        # print the rest of the line\n    } elsif ($line =~ /^%/) {\n      print STDERR \"Ignoring unknown command from profile header: $line\";\n    } else {\n      # End of commands, must be the header line.\n      return $line;\n    }\n  }\n  return undef;     # got to EOF without seeing a header line\n}\n\nsub IsSymbolizedProfileFile {\n  my $file_name = shift;\n  if (!(-e $file_name) || !(-r $file_name)) {\n    return 0;\n  }\n  # Check if the file contains a symbol-section marker.\n  open(TFILE, \"<$file_name\");\n  binmode TFILE;\n  my $firstline = ReadProfileHeader(*TFILE);\n  close(TFILE);\n  if (!$firstline) {\n    return 0;\n  }\n  $SYMBOL_PAGE =~ m,[^/]+$,;    # matches everything after the last slash\n  my $symbol_marker = $&;\n  return $firstline =~ /^--- *$symbol_marker/;\n}\n\n# Parse profile generated by common/profiler.cc and return a reference\n# to a map:\n#      $result->{version}     Version number of profile file\n#      $result->{period}      Sampling period (in microseconds)\n#      $result->{profile}     Profile object\n#      $result->{threads}     Map of thread IDs to profile objects\n#      $result->{map}         Memory map info from profile\n#      $result->{pcs}         Hash of all PC values seen, key is hex address\nsub ReadProfile {\n  my $prog = shift;\n  my $fname = shift;\n  my $result;            # return value\n\n  $CONTENTION_PAGE =~ m,[^/]+$,;    # matches everything after the last slash\n  my $contention_marker = $&;\n  $GROWTH_PAGE  =~ m,[^/]+$,;    # matches everything after the last slash\n  my $growth_marker = $&;\n  $SYMBOL_PAGE =~ m,[^/]+$,;    # matches everything after the last slash\n  my $symbol_marker = $&;\n  $PROFILE_PAGE =~ m,[^/]+$,;    # matches everything after the last slash\n  my $profile_marker = $&;\n  $HEAP_PAGE =~ m,[^/]+$,;    # matches everything after the last slash\n  my $heap_marker = $&;\n\n  # Look at first line to see if it is a heap or a CPU profile.\n  # CPU profile may start with no header at all, and just binary data\n  # (starting with \\0\\0\\0\\0) -- in that case, don't try to read the\n  # whole firstline, since it may be gigabytes(!) of data.\n  open(PROFILE, \"<$fname\") || error(\"$fname: $!\\n\");\n  binmode PROFILE;      # New perls do UTF-8 processing\n  my $header = ReadProfileHeader(*PROFILE);\n  if (!defined($header)) {   # means \"at EOF\"\n    error(\"Profile is empty.\\n\");\n  }\n\n  my $symbols;\n  if ($header =~ m/^--- *$symbol_marker/o) {\n    # Verify that the user asked for a symbolized profile\n    if (!$main::use_symbolized_profile) {\n      # we have both a binary and symbolized profiles, abort\n      error(\"FATAL ERROR: Symbolized profile\\n   $fname\\ncannot be used with \" .\n            \"a binary arg. Try again without passing\\n   $prog\\n\");\n    }\n    # Read the symbol section of the symbolized profile file.\n    $symbols = ReadSymbols(*PROFILE{IO});\n    # Read the next line to get the header for the remaining profile.\n    $header = ReadProfileHeader(*PROFILE) || \"\";\n  }\n\n  if ($header =~ m/^--- *($heap_marker|$growth_marker)/o) {\n    # Skip \"--- ...\" line for profile types that have their own headers.\n    $header = ReadProfileHeader(*PROFILE) || \"\";\n  }\n\n  $main::profile_type = '';\n\n  if ($header =~ m/^heap profile:.*$growth_marker/o) {\n    $main::profile_type = 'growth';\n    $result =  ReadHeapProfile($prog, *PROFILE, $header);\n  } elsif ($header =~ m/^heap profile:/) {\n    $main::profile_type = 'heap';\n    $result =  ReadHeapProfile($prog, *PROFILE, $header);\n  } elsif ($header =~ m/^heap/) {\n    $main::profile_type = 'heap';\n    $result = ReadThreadedHeapProfile($prog, $fname, $header);\n  } elsif ($header =~ m/^--- *$contention_marker/o) {\n    $main::profile_type = 'contention';\n    $result = ReadSynchProfile($prog, *PROFILE);\n  } elsif ($header =~ m/^--- *Stacks:/) {\n    print STDERR\n      \"Old format contention profile: mistakenly reports \" .\n      \"condition variable signals as lock contentions.\\n\";\n    $main::profile_type = 'contention';\n    $result = ReadSynchProfile($prog, *PROFILE);\n  } elsif ($header =~ m/^--- *$profile_marker/) {\n    # the binary cpu profile data starts immediately after this line\n    $main::profile_type = 'cpu';\n    $result = ReadCPUProfile($prog, $fname, *PROFILE);\n  } else {\n    if (defined($symbols)) {\n      # a symbolized profile contains a format we don't recognize, bail out\n      error(\"$fname: Cannot recognize profile section after symbols.\\n\");\n    }\n    # no ascii header present -- must be a CPU profile\n    $main::profile_type = 'cpu';\n    $result = ReadCPUProfile($prog, $fname, *PROFILE);\n  }\n\n  close(PROFILE);\n\n  # if we got symbols along with the profile, return those as well\n  if (defined($symbols)) {\n    $result->{symbols} = $symbols;\n  }\n\n  return $result;\n}\n\n# Subtract one from caller pc so we map back to call instr.\n# However, don't do this if we're reading a symbolized profile\n# file, in which case the subtract-one was done when the file\n# was written.\n#\n# We apply the same logic to all readers, though ReadCPUProfile uses an\n# independent implementation.\nsub FixCallerAddresses {\n  my $stack = shift;\n  # --raw/http: Always subtract one from pc's, because PrintSymbolizedProfile()\n  # dumps unadjusted profiles.\n  {\n    $stack =~ /(\\s)/;\n    my $delimiter = $1;\n    my @addrs = split(' ', $stack);\n    my @fixedaddrs;\n    $#fixedaddrs = $#addrs;\n    if ($#addrs >= 0) {\n      $fixedaddrs[0] = $addrs[0];\n    }\n    for (my $i = 1; $i <= $#addrs; $i++) {\n      $fixedaddrs[$i] = AddressSub($addrs[$i], \"0x1\");\n    }\n    return join $delimiter, @fixedaddrs;\n  }\n}\n\n# CPU profile reader\nsub ReadCPUProfile {\n  my $prog = shift;\n  my $fname = shift;       # just used for logging\n  local *PROFILE = shift;\n  my $version;\n  my $period;\n  my $i;\n  my $profile = {};\n  my $pcs = {};\n\n  # Parse string into array of slots.\n  my $slots = CpuProfileStream->new(*PROFILE, $fname);\n\n  # Read header.  The current header version is a 5-element structure\n  # containing:\n  #   0: header count (always 0)\n  #   1: header \"words\" (after this one: 3)\n  #   2: format version (0)\n  #   3: sampling period (usec)\n  #   4: unused padding (always 0)\n  if ($slots->get(0) != 0 ) {\n    error(\"$fname: not a profile file, or old format profile file\\n\");\n  }\n  $i = 2 + $slots->get(1);\n  $version = $slots->get(2);\n  $period = $slots->get(3);\n  # Do some sanity checking on these header values.\n  if ($version > (2**32) || $period > (2**32) || $i > (2**32) || $i < 5) {\n    error(\"$fname: not a profile file, or corrupted profile file\\n\");\n  }\n\n  # Parse profile\n  while ($slots->get($i) != -1) {\n    my $n = $slots->get($i++);\n    my $d = $slots->get($i++);\n    if ($d > (2**16)) {  # TODO(csilvers): what's a reasonable max-stack-depth?\n      my $addr = sprintf(\"0%o\", $i * ($address_length == 8 ? 4 : 8));\n      print STDERR \"At index $i (address $addr):\\n\";\n      error(\"$fname: stack trace depth >= 2**32\\n\");\n    }\n    if ($slots->get($i) == 0) {\n      # End of profile data marker\n      $i += $d;\n      last;\n    }\n\n    # Make key out of the stack entries\n    my @k = ();\n    for (my $j = 0; $j < $d; $j++) {\n      my $pc = $slots->get($i+$j);\n      # Subtract one from caller pc so we map back to call instr.\n      $pc--;\n      $pc = sprintf(\"%0*x\", $address_length, $pc);\n      $pcs->{$pc} = 1;\n      push @k, $pc;\n    }\n\n    AddEntry($profile, (join \"\\n\", @k), $n);\n    $i += $d;\n  }\n\n  # Parse map\n  my $map = '';\n  seek(PROFILE, $i * 4, 0);\n  read(PROFILE, $map, (stat PROFILE)[7]);\n\n  my $r = {};\n  $r->{version} = $version;\n  $r->{period} = $period;\n  $r->{profile} = $profile;\n  $r->{libs} = ParseLibraries($prog, $map, $pcs);\n  $r->{pcs} = $pcs;\n\n  return $r;\n}\n\nsub HeapProfileIndex {\n  my $index = 1;\n  if ($main::opt_inuse_space) {\n    $index = 1;\n  } elsif ($main::opt_inuse_objects) {\n    $index = 0;\n  } elsif ($main::opt_alloc_space) {\n    $index = 3;\n  } elsif ($main::opt_alloc_objects) {\n    $index = 2;\n  }\n  return $index;\n}\n\nsub ReadMappedLibraries {\n  my $fh = shift;\n  my $map = \"\";\n  # Read the /proc/self/maps data\n  while (<$fh>) {\n    s/\\r//g;         # turn windows-looking lines into unix-looking lines\n    $map .= $_;\n  }\n  return $map;\n}\n\nsub ReadMemoryMap {\n  my $fh = shift;\n  my $map = \"\";\n  # Read /proc/self/maps data as formatted by DumpAddressMap()\n  my $buildvar = \"\";\n  while (<PROFILE>) {\n    s/\\r//g;         # turn windows-looking lines into unix-looking lines\n    # Parse \"build=<dir>\" specification if supplied\n    if (m/^\\s*build=(.*)\\n/) {\n      $buildvar = $1;\n    }\n\n    # Expand \"$build\" variable if available\n    $_ =~ s/\\$build\\b/$buildvar/g;\n\n    $map .= $_;\n  }\n  return $map;\n}\n\nsub AdjustSamples {\n  my ($sample_adjustment, $sampling_algorithm, $n1, $s1, $n2, $s2) = @_;\n  if ($sample_adjustment) {\n    if ($sampling_algorithm == 2) {\n      # Remote-heap version 2\n      # The sampling frequency is the rate of a Poisson process.\n      # This means that the probability of sampling an allocation of\n      # size X with sampling rate Y is 1 - exp(-X/Y)\n      if ($n1 != 0) {\n        my $ratio = (($s1*1.0)/$n1)/($sample_adjustment);\n        my $scale_factor = 1/(1 - exp(-$ratio));\n        $n1 *= $scale_factor;\n        $s1 *= $scale_factor;\n      }\n      if ($n2 != 0) {\n        my $ratio = (($s2*1.0)/$n2)/($sample_adjustment);\n        my $scale_factor = 1/(1 - exp(-$ratio));\n        $n2 *= $scale_factor;\n        $s2 *= $scale_factor;\n      }\n    } else {\n      # Remote-heap version 1\n      my $ratio;\n      $ratio = (($s1*1.0)/$n1)/($sample_adjustment);\n      if ($ratio < 1) {\n        $n1 /= $ratio;\n        $s1 /= $ratio;\n      }\n      $ratio = (($s2*1.0)/$n2)/($sample_adjustment);\n      if ($ratio < 1) {\n        $n2 /= $ratio;\n        $s2 /= $ratio;\n      }\n    }\n  }\n  return ($n1, $s1, $n2, $s2);\n}\n\nsub ReadHeapProfile {\n  my $prog = shift;\n  local *PROFILE = shift;\n  my $header = shift;\n\n  my $index = HeapProfileIndex();\n\n  # Find the type of this profile.  The header line looks like:\n  #    heap profile:   1246:  8800744 [  1246:  8800744] @ <heap-url>/266053\n  # There are two pairs <count: size>, the first inuse objects/space, and the\n  # second allocated objects/space.  This is followed optionally by a profile\n  # type, and if that is present, optionally by a sampling frequency.\n  # For remote heap profiles (v1):\n  # The interpretation of the sampling frequency is that the profiler, for\n  # each sample, calculates a uniformly distributed random integer less than\n  # the given value, and records the next sample after that many bytes have\n  # been allocated.  Therefore, the expected sample interval is half of the\n  # given frequency.  By default, if not specified, the expected sample\n  # interval is 128KB.  Only remote-heap-page profiles are adjusted for\n  # sample size.\n  # For remote heap profiles (v2):\n  # The sampling frequency is the rate of a Poisson process. This means that\n  # the probability of sampling an allocation of size X with sampling rate Y\n  # is 1 - exp(-X/Y)\n  # For version 2, a typical header line might look like this:\n  # heap profile:   1922: 127792360 [  1922: 127792360] @ <heap-url>_v2/524288\n  # the trailing number (524288) is the sampling rate. (Version 1 showed\n  # double the 'rate' here)\n  my $sampling_algorithm = 0;\n  my $sample_adjustment = 0;\n  chomp($header);\n  my $type = \"unknown\";\n  if ($header =~ m\"^heap profile:\\s*(\\d+):\\s+(\\d+)\\s+\\[\\s*(\\d+):\\s+(\\d+)\\](\\s*@\\s*([^/]*)(/(\\d+))?)?\") {\n    if (defined($6) && ($6 ne '')) {\n      $type = $6;\n      my $sample_period = $8;\n      # $type is \"heapprofile\" for profiles generated by the\n      # heap-profiler, and either \"heap\" or \"heap_v2\" for profiles\n      # generated by sampling directly within tcmalloc.  It can also\n      # be \"growth\" for heap-growth profiles.  The first is typically\n      # found for profiles generated locally, and the others for\n      # remote profiles.\n      if (($type eq \"heapprofile\") || ($type !~ /heap/) ) {\n        # No need to adjust for the sampling rate with heap-profiler-derived data\n        $sampling_algorithm = 0;\n      } elsif ($type =~ /_v2/) {\n        $sampling_algorithm = 2;     # version 2 sampling\n        if (defined($sample_period) && ($sample_period ne '')) {\n          $sample_adjustment = int($sample_period);\n        }\n      } else {\n        $sampling_algorithm = 1;     # version 1 sampling\n        if (defined($sample_period) && ($sample_period ne '')) {\n          $sample_adjustment = int($sample_period)/2;\n        }\n      }\n    } else {\n      # We detect whether or not this is a remote-heap profile by checking\n      # that the total-allocated stats ($n2,$s2) are exactly the\n      # same as the in-use stats ($n1,$s1).  It is remotely conceivable\n      # that a non-remote-heap profile may pass this check, but it is hard\n      # to imagine how that could happen.\n      # In this case it's so old it's guaranteed to be remote-heap version 1.\n      my ($n1, $s1, $n2, $s2) = ($1, $2, $3, $4);\n      if (($n1 == $n2) && ($s1 == $s2)) {\n        # This is likely to be a remote-heap based sample profile\n        $sampling_algorithm = 1;\n      }\n    }\n  }\n\n  if ($sampling_algorithm > 0) {\n    # For remote-heap generated profiles, adjust the counts and sizes to\n    # account for the sample rate (we sample once every 128KB by default).\n    if ($sample_adjustment == 0) {\n      # Turn on profile adjustment.\n      $sample_adjustment = 128*1024;\n      print STDERR \"Adjusting heap profiles for 1-in-128KB sampling rate\\n\";\n    } else {\n      printf STDERR (\"Adjusting heap profiles for 1-in-%d sampling rate\\n\",\n                     $sample_adjustment);\n    }\n    if ($sampling_algorithm > 1) {\n      # We don't bother printing anything for the original version (version 1)\n      printf STDERR \"Heap version $sampling_algorithm\\n\";\n    }\n  }\n\n  my $profile = {};\n  my $pcs = {};\n  my $map = \"\";\n\n  while (<PROFILE>) {\n    s/\\r//g;         # turn windows-looking lines into unix-looking lines\n    if (/^MAPPED_LIBRARIES:/) {\n      $map .= ReadMappedLibraries(*PROFILE);\n      last;\n    }\n\n    if (/^--- Memory map:/) {\n      $map .= ReadMemoryMap(*PROFILE);\n      last;\n    }\n\n    # Read entry of the form:\n    #  <count1>: <bytes1> [<count2>: <bytes2>] @ a1 a2 a3 ... an\n    s/^\\s*//;\n    s/\\s*$//;\n    if (m/^\\s*(\\d+):\\s+(\\d+)\\s+\\[\\s*(\\d+):\\s+(\\d+)\\]\\s+@\\s+(.*)$/) {\n      my $stack = $5;\n      my ($n1, $s1, $n2, $s2) = ($1, $2, $3, $4);\n      my @counts = AdjustSamples($sample_adjustment, $sampling_algorithm,\n                                 $n1, $s1, $n2, $s2);\n      AddEntries($profile, $pcs, FixCallerAddresses($stack), $counts[$index]);\n    }\n  }\n\n  my $r = {};\n  $r->{version} = \"heap\";\n  $r->{period} = 1;\n  $r->{profile} = $profile;\n  $r->{libs} = ParseLibraries($prog, $map, $pcs);\n  $r->{pcs} = $pcs;\n  return $r;\n}\n\nsub ReadThreadedHeapProfile {\n  my ($prog, $fname, $header) = @_;\n\n  my $index = HeapProfileIndex();\n  my $sampling_algorithm = 0;\n  my $sample_adjustment = 0;\n  chomp($header);\n  my $type = \"unknown\";\n  # Assuming a very specific type of header for now.\n  if ($header =~ m\"^heap_v2/(\\d+)\") {\n    $type = \"_v2\";\n    $sampling_algorithm = 2;\n    $sample_adjustment = int($1);\n  }\n  if ($type ne \"_v2\" || !defined($sample_adjustment)) {\n    die \"Threaded heap profiles require v2 sampling with a sample rate\\n\";\n  }\n\n  my $profile = {};\n  my $thread_profiles = {};\n  my $pcs = {};\n  my $map = \"\";\n  my $stack = \"\";\n\n  while (<PROFILE>) {\n    s/\\r//g;\n    if (/^MAPPED_LIBRARIES:/) {\n      $map .= ReadMappedLibraries(*PROFILE);\n      last;\n    }\n\n    if (/^--- Memory map:/) {\n      $map .= ReadMemoryMap(*PROFILE);\n      last;\n    }\n\n    # Read entry of the form:\n    # @ a1 a2 ... an\n    #   t*: <count1>: <bytes1> [<count2>: <bytes2>]\n    #   t1: <count1>: <bytes1> [<count2>: <bytes2>]\n    #     ...\n    #   tn: <count1>: <bytes1> [<count2>: <bytes2>]\n    s/^\\s*//;\n    s/\\s*$//;\n    if (m/^@\\s+(.*)$/) {\n      $stack = $1;\n    } elsif (m/^\\s*(t(\\*|\\d+)):\\s+(\\d+):\\s+(\\d+)\\s+\\[\\s*(\\d+):\\s+(\\d+)\\]$/) {\n      if ($stack eq \"\") {\n        # Still in the header, so this is just a per-thread summary.\n        next;\n      }\n      my $thread = $2;\n      my ($n1, $s1, $n2, $s2) = ($3, $4, $5, $6);\n      my @counts = AdjustSamples($sample_adjustment, $sampling_algorithm,\n                                 $n1, $s1, $n2, $s2);\n      if ($thread eq \"*\") {\n        AddEntries($profile, $pcs, FixCallerAddresses($stack), $counts[$index]);\n      } else {\n        if (!exists($thread_profiles->{$thread})) {\n          $thread_profiles->{$thread} = {};\n        }\n        AddEntries($thread_profiles->{$thread}, $pcs,\n                   FixCallerAddresses($stack), $counts[$index]);\n      }\n    }\n  }\n\n  my $r = {};\n  $r->{version} = \"heap\";\n  $r->{period} = 1;\n  $r->{profile} = $profile;\n  $r->{threads} = $thread_profiles;\n  $r->{libs} = ParseLibraries($prog, $map, $pcs);\n  $r->{pcs} = $pcs;\n  return $r;\n}\n\nsub ReadSynchProfile {\n  my $prog = shift;\n  local *PROFILE = shift;\n  my $header = shift;\n\n  my $map = '';\n  my $profile = {};\n  my $pcs = {};\n  my $sampling_period = 1;\n  my $cyclespernanosec = 2.8;   # Default assumption for old binaries\n  my $seen_clockrate = 0;\n  my $line;\n\n  my $index = 0;\n  if ($main::opt_total_delay) {\n    $index = 0;\n  } elsif ($main::opt_contentions) {\n    $index = 1;\n  } elsif ($main::opt_mean_delay) {\n    $index = 2;\n  }\n\n  while ( $line = <PROFILE> ) {\n    $line =~ s/\\r//g;      # turn windows-looking lines into unix-looking lines\n    if ( $line =~ /^\\s*(\\d+)\\s+(\\d+) \\@\\s*(.*?)\\s*$/ ) {\n      my ($cycles, $count, $stack) = ($1, $2, $3);\n\n      # Convert cycles to nanoseconds\n      $cycles /= $cyclespernanosec;\n\n      # Adjust for sampling done by application\n      $cycles *= $sampling_period;\n      $count *= $sampling_period;\n\n      my @values = ($cycles, $count, $cycles / $count);\n      AddEntries($profile, $pcs, FixCallerAddresses($stack), $values[$index]);\n\n    } elsif ( $line =~ /^(slow release).*thread \\d+  \\@\\s*(.*?)\\s*$/ ||\n              $line =~ /^\\s*(\\d+) \\@\\s*(.*?)\\s*$/ ) {\n      my ($cycles, $stack) = ($1, $2);\n      if ($cycles !~ /^\\d+$/) {\n        next;\n      }\n\n      # Convert cycles to nanoseconds\n      $cycles /= $cyclespernanosec;\n\n      # Adjust for sampling done by application\n      $cycles *= $sampling_period;\n\n      AddEntries($profile, $pcs, FixCallerAddresses($stack), $cycles);\n\n    } elsif ( $line =~ m/^([a-z][^=]*)=(.*)$/ ) {\n      my ($variable, $value) = ($1,$2);\n      for ($variable, $value) {\n        s/^\\s+//;\n        s/\\s+$//;\n      }\n      if ($variable eq \"cycles/second\") {\n        $cyclespernanosec = $value / 1e9;\n        $seen_clockrate = 1;\n      } elsif ($variable eq \"sampling period\") {\n        $sampling_period = $value;\n      } elsif ($variable eq \"ms since reset\") {\n        # Currently nothing is done with this value in jeprof\n        # So we just silently ignore it for now\n      } elsif ($variable eq \"discarded samples\") {\n        # Currently nothing is done with this value in jeprof\n        # So we just silently ignore it for now\n      } else {\n        printf STDERR (\"Ignoring unnknown variable in /contention output: \" .\n                       \"'%s' = '%s'\\n\",$variable,$value);\n      }\n    } else {\n      # Memory map entry\n      $map .= $line;\n    }\n  }\n\n  if (!$seen_clockrate) {\n    printf STDERR (\"No cycles/second entry in profile; Guessing %.1f GHz\\n\",\n                   $cyclespernanosec);\n  }\n\n  my $r = {};\n  $r->{version} = 0;\n  $r->{period} = $sampling_period;\n  $r->{profile} = $profile;\n  $r->{libs} = ParseLibraries($prog, $map, $pcs);\n  $r->{pcs} = $pcs;\n  return $r;\n}\n\n# Given a hex value in the form \"0x1abcd\" or \"1abcd\", return either\n# \"0001abcd\" or \"000000000001abcd\", depending on the current (global)\n# address length.\nsub HexExtend {\n  my $addr = shift;\n\n  $addr =~ s/^(0x)?0*//;\n  my $zeros_needed = $address_length - length($addr);\n  if ($zeros_needed < 0) {\n    printf STDERR \"Warning: address $addr is longer than address length $address_length\\n\";\n    return $addr;\n  }\n  return (\"0\" x $zeros_needed) . $addr;\n}\n\n##### Symbol extraction #####\n\n# Aggressively search the lib_prefix values for the given library\n# If all else fails, just return the name of the library unmodified.\n# If the lib_prefix is \"/my/path,/other/path\" and $file is \"/lib/dir/mylib.so\"\n# it will search the following locations in this order, until it finds a file:\n#   /my/path/lib/dir/mylib.so\n#   /other/path/lib/dir/mylib.so\n#   /my/path/dir/mylib.so\n#   /other/path/dir/mylib.so\n#   /my/path/mylib.so\n#   /other/path/mylib.so\n#   /lib/dir/mylib.so              (returned as last resort)\nsub FindLibrary {\n  my $file = shift;\n  my $suffix = $file;\n\n  # Search for the library as described above\n  do {\n    foreach my $prefix (@prefix_list) {\n      my $fullpath = $prefix . $suffix;\n      if (-e $fullpath) {\n        return $fullpath;\n      }\n    }\n  } while ($suffix =~ s|^/[^/]+/|/|);\n  return $file;\n}\n\n# Return path to library with debugging symbols.\n# For libc libraries, the copy in /usr/lib/debug contains debugging symbols\nsub DebuggingLibrary {\n  my $file = shift;\n  if ($file =~ m|^/|) {\n      if (-f \"/usr/lib/debug$file\") {\n        return \"/usr/lib/debug$file\";\n      } elsif (-f \"/usr/lib/debug$file.debug\") {\n        return \"/usr/lib/debug$file.debug\";\n      }\n  }\n  return undef;\n}\n\n# Parse text section header of a library using objdump\nsub ParseTextSectionHeaderFromObjdump {\n  my $lib = shift;\n\n  my $size = undef;\n  my $vma;\n  my $file_offset;\n  # Get objdump output from the library file to figure out how to\n  # map between mapped addresses and addresses in the library.\n  my $cmd = ShellEscape($obj_tool_map{\"objdump\"}, \"-h\", $lib);\n  open(OBJDUMP, \"$cmd |\") || error(\"$cmd: $!\\n\");\n  while (<OBJDUMP>) {\n    s/\\r//g;         # turn windows-looking lines into unix-looking lines\n    # Idx Name          Size      VMA       LMA       File off  Algn\n    #  10 .text         00104b2c  420156f0  420156f0  000156f0  2**4\n    # For 64-bit objects, VMA and LMA will be 16 hex digits, size and file\n    # offset may still be 8.  But AddressSub below will still handle that.\n    my @x = split;\n    if (($#x >= 6) && ($x[1] eq '.text')) {\n      $size = $x[2];\n      $vma = $x[3];\n      $file_offset = $x[5];\n      last;\n    }\n  }\n  close(OBJDUMP);\n\n  if (!defined($size)) {\n    return undef;\n  }\n\n  my $r = {};\n  $r->{size} = $size;\n  $r->{vma} = $vma;\n  $r->{file_offset} = $file_offset;\n\n  return $r;\n}\n\n# Parse text section header of a library using otool (on OS X)\nsub ParseTextSectionHeaderFromOtool {\n  my $lib = shift;\n\n  my $size = undef;\n  my $vma = undef;\n  my $file_offset = undef;\n  # Get otool output from the library file to figure out how to\n  # map between mapped addresses and addresses in the library.\n  my $command = ShellEscape($obj_tool_map{\"otool\"}, \"-l\", $lib);\n  open(OTOOL, \"$command |\") || error(\"$command: $!\\n\");\n  my $cmd = \"\";\n  my $sectname = \"\";\n  my $segname = \"\";\n  foreach my $line (<OTOOL>) {\n    $line =~ s/\\r//g;      # turn windows-looking lines into unix-looking lines\n    # Load command <#>\n    #       cmd LC_SEGMENT\n    # [...]\n    # Section\n    #   sectname __text\n    #    segname __TEXT\n    #       addr 0x000009f8\n    #       size 0x00018b9e\n    #     offset 2552\n    #      align 2^2 (4)\n    # We will need to strip off the leading 0x from the hex addresses,\n    # and convert the offset into hex.\n    if ($line =~ /Load command/) {\n      $cmd = \"\";\n      $sectname = \"\";\n      $segname = \"\";\n    } elsif ($line =~ /Section/) {\n      $sectname = \"\";\n      $segname = \"\";\n    } elsif ($line =~ /cmd (\\w+)/) {\n      $cmd = $1;\n    } elsif ($line =~ /sectname (\\w+)/) {\n      $sectname = $1;\n    } elsif ($line =~ /segname (\\w+)/) {\n      $segname = $1;\n    } elsif (!(($cmd eq \"LC_SEGMENT\" || $cmd eq \"LC_SEGMENT_64\") &&\n               $sectname eq \"__text\" &&\n               $segname eq \"__TEXT\")) {\n      next;\n    } elsif ($line =~ /\\baddr 0x([0-9a-fA-F]+)/) {\n      $vma = $1;\n    } elsif ($line =~ /\\bsize 0x([0-9a-fA-F]+)/) {\n      $size = $1;\n    } elsif ($line =~ /\\boffset ([0-9]+)/) {\n      $file_offset = sprintf(\"%016x\", $1);\n    }\n    if (defined($vma) && defined($size) && defined($file_offset)) {\n      last;\n    }\n  }\n  close(OTOOL);\n\n  if (!defined($vma) || !defined($size) || !defined($file_offset)) {\n     return undef;\n  }\n\n  my $r = {};\n  $r->{size} = $size;\n  $r->{vma} = $vma;\n  $r->{file_offset} = $file_offset;\n\n  return $r;\n}\n\nsub ParseTextSectionHeader {\n  # obj_tool_map(\"otool\") is only defined if we're in a Mach-O environment\n  if (defined($obj_tool_map{\"otool\"})) {\n    my $r = ParseTextSectionHeaderFromOtool(@_);\n    if (defined($r)){\n      return $r;\n    }\n  }\n  # If otool doesn't work, or we don't have it, fall back to objdump\n  return ParseTextSectionHeaderFromObjdump(@_);\n}\n\n# Split /proc/pid/maps dump into a list of libraries\nsub ParseLibraries {\n  return if $main::use_symbol_page;  # We don't need libraries info.\n  my $prog = Cwd::abs_path(shift);\n  my $map = shift;\n  my $pcs = shift;\n\n  my $result = [];\n  my $h = \"[a-f0-9]+\";\n  my $zero_offset = HexExtend(\"0\");\n\n  my $buildvar = \"\";\n  foreach my $l (split(\"\\n\", $map)) {\n    if ($l =~ m/^\\s*build=(.*)$/) {\n      $buildvar = $1;\n    }\n\n    my $start;\n    my $finish;\n    my $offset;\n    my $lib;\n    if ($l =~ /^($h)-($h)\\s+..x.\\s+($h)\\s+\\S+:\\S+\\s+\\d+\\s+(\\S+\\.(so|dll|dylib|bundle)((\\.\\d+)+\\w*(\\.\\d+){0,3})?)$/i) {\n      # Full line from /proc/self/maps.  Example:\n      #   40000000-40015000 r-xp 00000000 03:01 12845071   /lib/ld-2.3.2.so\n      $start = HexExtend($1);\n      $finish = HexExtend($2);\n      $offset = HexExtend($3);\n      $lib = $4;\n      $lib =~ s|\\\\|/|g;     # turn windows-style paths into unix-style paths\n    } elsif ($l =~ /^\\s*($h)-($h):\\s*(\\S+\\.so(\\.\\d+)*)/) {\n      # Cooked line from DumpAddressMap.  Example:\n      #   40000000-40015000: /lib/ld-2.3.2.so\n      $start = HexExtend($1);\n      $finish = HexExtend($2);\n      $offset = $zero_offset;\n      $lib = $3;\n    } elsif (($l =~ /^($h)-($h)\\s+..x.\\s+($h)\\s+\\S+:\\S+\\s+\\d+\\s+(\\S+)$/i) && ($4 eq $prog)) {\n      # PIEs and address space randomization do not play well with our\n      # default assumption that main executable is at lowest\n      # addresses. So we're detecting main executable in\n      # /proc/self/maps as well.\n      $start = HexExtend($1);\n      $finish = HexExtend($2);\n      $offset = HexExtend($3);\n      $lib = $4;\n      $lib =~ s|\\\\|/|g;     # turn windows-style paths into unix-style paths\n    }\n    # FreeBSD 10.0 virtual memory map /proc/curproc/map as defined in\n    # function procfs_doprocmap (sys/fs/procfs/procfs_map.c)\n    #\n    # Example:\n    # 0x800600000 0x80061a000 26 0 0xfffff800035a0000 r-x 75 33 0x1004 COW NC vnode /libexec/ld-elf.s\n    # o.1 NCH -1\n    elsif ($l =~ /^(0x$h)\\s(0x$h)\\s\\d+\\s\\d+\\s0x$h\\sr-x\\s\\d+\\s\\d+\\s0x\\d+\\s(COW|NCO)\\s(NC|NNC)\\svnode\\s(\\S+\\.so(\\.\\d+)*)/) {\n      $start = HexExtend($1);\n      $finish = HexExtend($2);\n      $offset = $zero_offset;\n      $lib = FindLibrary($5);\n\n    } else {\n      next;\n    }\n\n    # Expand \"$build\" variable if available\n    $lib =~ s/\\$build\\b/$buildvar/g;\n\n    $lib = FindLibrary($lib);\n\n    # Check for pre-relocated libraries, which use pre-relocated symbol tables\n    # and thus require adjusting the offset that we'll use to translate\n    # VM addresses into symbol table addresses.\n    # Only do this if we're not going to fetch the symbol table from a\n    # debugging copy of the library.\n    if (!DebuggingLibrary($lib)) {\n      my $text = ParseTextSectionHeader($lib);\n      if (defined($text)) {\n         my $vma_offset = AddressSub($text->{vma}, $text->{file_offset});\n         $offset = AddressAdd($offset, $vma_offset);\n      }\n    }\n\n    if($main::opt_debug) { printf STDERR \"$start:$finish ($offset) $lib\\n\"; }\n    push(@{$result}, [$lib, $start, $finish, $offset]);\n  }\n\n  # Append special entry for additional library (not relocated)\n  if ($main::opt_lib ne \"\") {\n    my $text = ParseTextSectionHeader($main::opt_lib);\n    if (defined($text)) {\n       my $start = $text->{vma};\n       my $finish = AddressAdd($start, $text->{size});\n\n       push(@{$result}, [$main::opt_lib, $start, $finish, $start]);\n    }\n  }\n\n  # Append special entry for the main program.  This covers\n  # 0..max_pc_value_seen, so that we assume pc values not found in one\n  # of the library ranges will be treated as coming from the main\n  # program binary.\n  my $min_pc = HexExtend(\"0\");\n  my $max_pc = $min_pc;          # find the maximal PC value in any sample\n  foreach my $pc (keys(%{$pcs})) {\n    if (HexExtend($pc) gt $max_pc) { $max_pc = HexExtend($pc); }\n  }\n  push(@{$result}, [$prog, $min_pc, $max_pc, $zero_offset]);\n\n  return $result;\n}\n\n# Add two hex addresses of length $address_length.\n# Run jeprof --test for unit test if this is changed.\nsub AddressAdd {\n  my $addr1 = shift;\n  my $addr2 = shift;\n  my $sum;\n\n  if ($address_length == 8) {\n    # Perl doesn't cope with wraparound arithmetic, so do it explicitly:\n    $sum = (hex($addr1)+hex($addr2)) % (0x10000000 * 16);\n    return sprintf(\"%08x\", $sum);\n\n  } else {\n    # Do the addition in 7-nibble chunks to trivialize carry handling.\n\n    if ($main::opt_debug and $main::opt_test) {\n      print STDERR \"AddressAdd $addr1 + $addr2 = \";\n    }\n\n    my $a1 = substr($addr1,-7);\n    $addr1 = substr($addr1,0,-7);\n    my $a2 = substr($addr2,-7);\n    $addr2 = substr($addr2,0,-7);\n    $sum = hex($a1) + hex($a2);\n    my $c = 0;\n    if ($sum > 0xfffffff) {\n      $c = 1;\n      $sum -= 0x10000000;\n    }\n    my $r = sprintf(\"%07x\", $sum);\n\n    $a1 = substr($addr1,-7);\n    $addr1 = substr($addr1,0,-7);\n    $a2 = substr($addr2,-7);\n    $addr2 = substr($addr2,0,-7);\n    $sum = hex($a1) + hex($a2) + $c;\n    $c = 0;\n    if ($sum > 0xfffffff) {\n      $c = 1;\n      $sum -= 0x10000000;\n    }\n    $r = sprintf(\"%07x\", $sum) . $r;\n\n    $sum = hex($addr1) + hex($addr2) + $c;\n    if ($sum > 0xff) { $sum -= 0x100; }\n    $r = sprintf(\"%02x\", $sum) . $r;\n\n    if ($main::opt_debug and $main::opt_test) { print STDERR \"$r\\n\"; }\n\n    return $r;\n  }\n}\n\n\n# Subtract two hex addresses of length $address_length.\n# Run jeprof --test for unit test if this is changed.\nsub AddressSub {\n  my $addr1 = shift;\n  my $addr2 = shift;\n  my $diff;\n\n  if ($address_length == 8) {\n    # Perl doesn't cope with wraparound arithmetic, so do it explicitly:\n    $diff = (hex($addr1)-hex($addr2)) % (0x10000000 * 16);\n    return sprintf(\"%08x\", $diff);\n\n  } else {\n    # Do the addition in 7-nibble chunks to trivialize borrow handling.\n    # if ($main::opt_debug) { print STDERR \"AddressSub $addr1 - $addr2 = \"; }\n\n    my $a1 = hex(substr($addr1,-7));\n    $addr1 = substr($addr1,0,-7);\n    my $a2 = hex(substr($addr2,-7));\n    $addr2 = substr($addr2,0,-7);\n    my $b = 0;\n    if ($a2 > $a1) {\n      $b = 1;\n      $a1 += 0x10000000;\n    }\n    $diff = $a1 - $a2;\n    my $r = sprintf(\"%07x\", $diff);\n\n    $a1 = hex(substr($addr1,-7));\n    $addr1 = substr($addr1,0,-7);\n    $a2 = hex(substr($addr2,-7)) + $b;\n    $addr2 = substr($addr2,0,-7);\n    $b = 0;\n    if ($a2 > $a1) {\n      $b = 1;\n      $a1 += 0x10000000;\n    }\n    $diff = $a1 - $a2;\n    $r = sprintf(\"%07x\", $diff) . $r;\n\n    $a1 = hex($addr1);\n    $a2 = hex($addr2) + $b;\n    if ($a2 > $a1) { $a1 += 0x100; }\n    $diff = $a1 - $a2;\n    $r = sprintf(\"%02x\", $diff) . $r;\n\n    # if ($main::opt_debug) { print STDERR \"$r\\n\"; }\n\n    return $r;\n  }\n}\n\n# Increment a hex addresses of length $address_length.\n# Run jeprof --test for unit test if this is changed.\nsub AddressInc {\n  my $addr = shift;\n  my $sum;\n\n  if ($address_length == 8) {\n    # Perl doesn't cope with wraparound arithmetic, so do it explicitly:\n    $sum = (hex($addr)+1) % (0x10000000 * 16);\n    return sprintf(\"%08x\", $sum);\n\n  } else {\n    # Do the addition in 7-nibble chunks to trivialize carry handling.\n    # We are always doing this to step through the addresses in a function,\n    # and will almost never overflow the first chunk, so we check for this\n    # case and exit early.\n\n    # if ($main::opt_debug) { print STDERR \"AddressInc $addr1 = \"; }\n\n    my $a1 = substr($addr,-7);\n    $addr = substr($addr,0,-7);\n    $sum = hex($a1) + 1;\n    my $r = sprintf(\"%07x\", $sum);\n    if ($sum <= 0xfffffff) {\n      $r = $addr . $r;\n      # if ($main::opt_debug) { print STDERR \"$r\\n\"; }\n      return HexExtend($r);\n    } else {\n      $r = \"0000000\";\n    }\n\n    $a1 = substr($addr,-7);\n    $addr = substr($addr,0,-7);\n    $sum = hex($a1) + 1;\n    $r = sprintf(\"%07x\", $sum) . $r;\n    if ($sum <= 0xfffffff) {\n      $r = $addr . $r;\n      # if ($main::opt_debug) { print STDERR \"$r\\n\"; }\n      return HexExtend($r);\n    } else {\n      $r = \"00000000000000\";\n    }\n\n    $sum = hex($addr) + 1;\n    if ($sum > 0xff) { $sum -= 0x100; }\n    $r = sprintf(\"%02x\", $sum) . $r;\n\n    # if ($main::opt_debug) { print STDERR \"$r\\n\"; }\n    return $r;\n  }\n}\n\n# Extract symbols for all PC values found in profile\nsub ExtractSymbols {\n  my $libs = shift;\n  my $pcset = shift;\n\n  my $symbols = {};\n\n  # Map each PC value to the containing library.  To make this faster,\n  # we sort libraries by their starting pc value (highest first), and\n  # advance through the libraries as we advance the pc.  Sometimes the\n  # addresses of libraries may overlap with the addresses of the main\n  # binary, so to make sure the libraries 'win', we iterate over the\n  # libraries in reverse order (which assumes the binary doesn't start\n  # in the middle of a library, which seems a fair assumption).\n  my @pcs = (sort { $a cmp $b } keys(%{$pcset}));  # pcset is 0-extended strings\n  foreach my $lib (sort {$b->[1] cmp $a->[1]} @{$libs}) {\n    my $libname = $lib->[0];\n    my $start = $lib->[1];\n    my $finish = $lib->[2];\n    my $offset = $lib->[3];\n\n    # Use debug library if it exists\n    my $debug_libname = DebuggingLibrary($libname);\n    if ($debug_libname) {\n        $libname = $debug_libname;\n    }\n\n    # Get list of pcs that belong in this library.\n    my $contained = [];\n    my ($start_pc_index, $finish_pc_index);\n    # Find smallest finish_pc_index such that $finish < $pc[$finish_pc_index].\n    for ($finish_pc_index = $#pcs + 1; $finish_pc_index > 0;\n         $finish_pc_index--) {\n      last if $pcs[$finish_pc_index - 1] le $finish;\n    }\n    # Find smallest start_pc_index such that $start <= $pc[$start_pc_index].\n    for ($start_pc_index = $finish_pc_index; $start_pc_index > 0;\n         $start_pc_index--) {\n      last if $pcs[$start_pc_index - 1] lt $start;\n    }\n    # This keeps PC values higher than $pc[$finish_pc_index] in @pcs,\n    # in case there are overlaps in libraries and the main binary.\n    @{$contained} = splice(@pcs, $start_pc_index,\n                           $finish_pc_index - $start_pc_index);\n    # Map to symbols\n    MapToSymbols($libname, AddressSub($start, $offset), $contained, $symbols);\n  }\n\n  return $symbols;\n}\n\n# Map list of PC values to symbols for a given image\nsub MapToSymbols {\n  my $image = shift;\n  my $offset = shift;\n  my $pclist = shift;\n  my $symbols = shift;\n\n  my $debug = 0;\n\n  # Ignore empty binaries\n  if ($#{$pclist} < 0) { return; }\n\n  # Figure out the addr2line command to use\n  my $addr2line = $obj_tool_map{\"addr2line\"};\n  my $cmd = ShellEscape($addr2line, \"-f\", \"-C\", \"-e\", $image);\n  if (exists $obj_tool_map{\"addr2line_pdb\"}) {\n    $addr2line = $obj_tool_map{\"addr2line_pdb\"};\n    $cmd = ShellEscape($addr2line, \"--demangle\", \"-f\", \"-C\", \"-e\", $image);\n  }\n\n  # If \"addr2line\" isn't installed on the system at all, just use\n  # nm to get what info we can (function names, but not line numbers).\n  if (system(ShellEscape($addr2line, \"--help\") . \" >$dev_null 2>&1\") != 0) {\n    MapSymbolsWithNM($image, $offset, $pclist, $symbols);\n    return;\n  }\n\n  # \"addr2line -i\" can produce a variable number of lines per input\n  # address, with no separator that allows us to tell when data for\n  # the next address starts.  So we find the address for a special\n  # symbol (_fini) and interleave this address between all real\n  # addresses passed to addr2line.  The name of this special symbol\n  # can then be used as a separator.\n  $sep_address = undef;  # May be filled in by MapSymbolsWithNM()\n  my $nm_symbols = {};\n  MapSymbolsWithNM($image, $offset, $pclist, $nm_symbols);\n  if (defined($sep_address)) {\n    # Only add \" -i\" to addr2line if the binary supports it.\n    # addr2line --help returns 0, but not if it sees an unknown flag first.\n    if (system(\"$cmd -i --help >$dev_null 2>&1\") == 0) {\n      $cmd .= \" -i\";\n    } else {\n      $sep_address = undef;   # no need for sep_address if we don't support -i\n    }\n  }\n\n  # Make file with all PC values with intervening 'sep_address' so\n  # that we can reliably detect the end of inlined function list\n  open(ADDRESSES, \">$main::tmpfile_sym\") || error(\"$main::tmpfile_sym: $!\\n\");\n  if ($debug) { print(\"---- $image ---\\n\"); }\n  for (my $i = 0; $i <= $#{$pclist}; $i++) {\n    # addr2line always reads hex addresses, and does not need '0x' prefix.\n    if ($debug) { printf STDERR (\"%s\\n\", $pclist->[$i]); }\n    printf ADDRESSES (\"%s\\n\", AddressSub($pclist->[$i], $offset));\n    if (defined($sep_address)) {\n      printf ADDRESSES (\"%s\\n\", $sep_address);\n    }\n  }\n  close(ADDRESSES);\n  if ($debug) {\n    print(\"----\\n\");\n    system(\"cat\", $main::tmpfile_sym);\n    print(\"----\\n\");\n    system(\"$cmd < \" . ShellEscape($main::tmpfile_sym));\n    print(\"----\\n\");\n  }\n\n  open(SYMBOLS, \"$cmd <\" . ShellEscape($main::tmpfile_sym) . \" |\")\n      || error(\"$cmd: $!\\n\");\n  my $count = 0;   # Index in pclist\n  while (<SYMBOLS>) {\n    # Read fullfunction and filelineinfo from next pair of lines\n    s/\\r?\\n$//g;\n    my $fullfunction = $_;\n    $_ = <SYMBOLS>;\n    s/\\r?\\n$//g;\n    my $filelinenum = $_;\n\n    if (defined($sep_address) && $fullfunction eq $sep_symbol) {\n      # Terminating marker for data for this address\n      $count++;\n      next;\n    }\n\n    $filelinenum =~ s|\\\\|/|g; # turn windows-style paths into unix-style paths\n\n    my $pcstr = $pclist->[$count];\n    my $function = ShortFunctionName($fullfunction);\n    my $nms = $nm_symbols->{$pcstr};\n    if (defined($nms)) {\n      if ($fullfunction eq '??') {\n        # nm found a symbol for us.\n        $function = $nms->[0];\n        $fullfunction = $nms->[2];\n      } else {\n\t# MapSymbolsWithNM tags each routine with its starting address,\n\t# useful in case the image has multiple occurrences of this\n\t# routine.  (It uses a syntax that resembles template paramters,\n\t# that are automatically stripped out by ShortFunctionName().)\n\t# addr2line does not provide the same information.  So we check\n\t# if nm disambiguated our symbol, and if so take the annotated\n\t# (nm) version of the routine-name.  TODO(csilvers): this won't\n\t# catch overloaded, inlined symbols, which nm doesn't see.\n\t# Better would be to do a check similar to nm's, in this fn.\n\tif ($nms->[2] =~ m/^\\Q$function\\E/) {  # sanity check it's the right fn\n\t  $function = $nms->[0];\n\t  $fullfunction = $nms->[2];\n\t}\n      }\n    }\n\n    # Prepend to accumulated symbols for pcstr\n    # (so that caller comes before callee)\n    my $sym = $symbols->{$pcstr};\n    if (!defined($sym)) {\n      $sym = [];\n      $symbols->{$pcstr} = $sym;\n    }\n    unshift(@{$sym}, $function, $filelinenum, $fullfunction);\n    if ($debug) { printf STDERR (\"%s => [%s]\\n\", $pcstr, join(\" \", @{$sym})); }\n    if (!defined($sep_address)) {\n      # Inlining is off, so this entry ends immediately\n      $count++;\n    }\n  }\n  close(SYMBOLS);\n}\n\n# Use nm to map the list of referenced PCs to symbols.  Return true iff we\n# are able to read procedure information via nm.\nsub MapSymbolsWithNM {\n  my $image = shift;\n  my $offset = shift;\n  my $pclist = shift;\n  my $symbols = shift;\n\n  # Get nm output sorted by increasing address\n  my $symbol_table = GetProcedureBoundaries($image, \".\");\n  if (!%{$symbol_table}) {\n    return 0;\n  }\n  # Start addresses are already the right length (8 or 16 hex digits).\n  my @names = sort { $symbol_table->{$a}->[0] cmp $symbol_table->{$b}->[0] }\n    keys(%{$symbol_table});\n\n  if ($#names < 0) {\n    # No symbols: just use addresses\n    foreach my $pc (@{$pclist}) {\n      my $pcstr = \"0x\" . $pc;\n      $symbols->{$pc} = [$pcstr, \"?\", $pcstr];\n    }\n    return 0;\n  }\n\n  # Sort addresses so we can do a join against nm output\n  my $index = 0;\n  my $fullname = $names[0];\n  my $name = ShortFunctionName($fullname);\n  foreach my $pc (sort { $a cmp $b } @{$pclist}) {\n    # Adjust for mapped offset\n    my $mpc = AddressSub($pc, $offset);\n    while (($index < $#names) && ($mpc ge $symbol_table->{$fullname}->[1])){\n      $index++;\n      $fullname = $names[$index];\n      $name = ShortFunctionName($fullname);\n    }\n    if ($mpc lt $symbol_table->{$fullname}->[1]) {\n      $symbols->{$pc} = [$name, \"?\", $fullname];\n    } else {\n      my $pcstr = \"0x\" . $pc;\n      $symbols->{$pc} = [$pcstr, \"?\", $pcstr];\n    }\n  }\n  return 1;\n}\n\nsub ShortFunctionName {\n  my $function = shift;\n  while ($function =~ s/\\([^()]*\\)(\\s*const)?//g) { }   # Argument types\n  while ($function =~ s/<[^<>]*>//g)  { }    # Remove template arguments\n  $function =~ s/^.*\\s+(\\w+::)/$1/;          # Remove leading type\n  return $function;\n}\n\n# Trim overly long symbols found in disassembler output\nsub CleanDisassembly {\n  my $d = shift;\n  while ($d =~ s/\\([^()%]*\\)(\\s*const)?//g) { } # Argument types, not (%rax)\n  while ($d =~ s/(\\w+)<[^<>]*>/$1/g)  { }       # Remove template arguments\n  return $d;\n}\n\n# Clean file name for display\nsub CleanFileName {\n  my ($f) = @_;\n  $f =~ s|^/proc/self/cwd/||;\n  $f =~ s|^\\./||;\n  return $f;\n}\n\n# Make address relative to section and clean up for display\nsub UnparseAddress {\n  my ($offset, $address) = @_;\n  $address = AddressSub($address, $offset);\n  $address =~ s/^0x//;\n  $address =~ s/^0*//;\n  return $address;\n}\n\n##### Miscellaneous #####\n\n# Find the right versions of the above object tools to use.  The\n# argument is the program file being analyzed, and should be an ELF\n# 32-bit or ELF 64-bit executable file.  The location of the tools\n# is determined by considering the following options in this order:\n#   1) --tools option, if set\n#   2) JEPROF_TOOLS environment variable, if set\n#   3) the environment\nsub ConfigureObjTools {\n  my $prog_file = shift;\n\n  # Check for the existence of $prog_file because /usr/bin/file does not\n  # predictably return error status in prod.\n  (-e $prog_file)  || error(\"$prog_file does not exist.\\n\");\n\n  my $file_type = undef;\n  if (-e \"/usr/bin/file\") {\n    # Follow symlinks (at least for systems where \"file\" supports that).\n    my $escaped_prog_file = ShellEscape($prog_file);\n    $file_type = `/usr/bin/file -L $escaped_prog_file 2>$dev_null ||\n                  /usr/bin/file $escaped_prog_file`;\n  } elsif ($^O == \"MSWin32\") {\n    $file_type = \"MS Windows\";\n  } else {\n    print STDERR \"WARNING: Can't determine the file type of $prog_file\";\n  }\n\n  if ($file_type =~ /64-bit/) {\n    # Change $address_length to 16 if the program file is ELF 64-bit.\n    # We can't detect this from many (most?) heap or lock contention\n    # profiles, since the actual addresses referenced are generally in low\n    # memory even for 64-bit programs.\n    $address_length = 16;\n  }\n\n  if ($file_type =~ /MS Windows/) {\n    # For windows, we provide a version of nm and addr2line as part of\n    # the opensource release, which is capable of parsing\n    # Windows-style PDB executables.  It should live in the path, or\n    # in the same directory as jeprof.\n    $obj_tool_map{\"nm_pdb\"} = \"nm-pdb\";\n    $obj_tool_map{\"addr2line_pdb\"} = \"addr2line-pdb\";\n  }\n\n  if ($file_type =~ /Mach-O/) {\n    # OS X uses otool to examine Mach-O files, rather than objdump.\n    $obj_tool_map{\"otool\"} = \"otool\";\n    $obj_tool_map{\"addr2line\"} = \"false\";  # no addr2line\n    $obj_tool_map{\"objdump\"} = \"false\";  # no objdump\n  }\n\n  # Go fill in %obj_tool_map with the pathnames to use:\n  foreach my $tool (keys %obj_tool_map) {\n    $obj_tool_map{$tool} = ConfigureTool($obj_tool_map{$tool});\n  }\n}\n\n# Returns the path of a caller-specified object tool.  If --tools or\n# JEPROF_TOOLS are specified, then returns the full path to the tool\n# with that prefix.  Otherwise, returns the path unmodified (which\n# means we will look for it on PATH).\nsub ConfigureTool {\n  my $tool = shift;\n  my $path;\n\n  # --tools (or $JEPROF_TOOLS) is a comma separated list, where each\n  # item is either a) a pathname prefix, or b) a map of the form\n  # <tool>:<path>.  First we look for an entry of type (b) for our\n  # tool.  If one is found, we use it.  Otherwise, we consider all the\n  # pathname prefixes in turn, until one yields an existing file.  If\n  # none does, we use a default path.\n  my $tools = $main::opt_tools || $ENV{\"JEPROF_TOOLS\"} || \"\";\n  if ($tools =~ m/(,|^)\\Q$tool\\E:([^,]*)/) {\n    $path = $2;\n    # TODO(csilvers): sanity-check that $path exists?  Hard if it's relative.\n  } elsif ($tools ne '') {\n    foreach my $prefix (split(',', $tools)) {\n      next if ($prefix =~ /:/);    # ignore \"tool:fullpath\" entries in the list\n      if (-x $prefix . $tool) {\n        $path = $prefix . $tool;\n        last;\n      }\n    }\n    if (!$path) {\n      error(\"No '$tool' found with prefix specified by \" .\n            \"--tools (or \\$JEPROF_TOOLS) '$tools'\\n\");\n    }\n  } else {\n    # ... otherwise use the version that exists in the same directory as\n    # jeprof.  If there's nothing there, use $PATH.\n    $0 =~ m,[^/]*$,;     # this is everything after the last slash\n    my $dirname = $`;    # this is everything up to and including the last slash\n    if (-x \"$dirname$tool\") {\n      $path = \"$dirname$tool\";\n    } else {\n      $path = $tool;\n    }\n  }\n  if ($main::opt_debug) { print STDERR \"Using '$path' for '$tool'.\\n\"; }\n  return $path;\n}\n\nsub ShellEscape {\n  my @escaped_words = ();\n  foreach my $word (@_) {\n    my $escaped_word = $word;\n    if ($word =~ m![^a-zA-Z0-9/.,_=-]!) {  # check for anything not in whitelist\n      $escaped_word =~ s/'/'\\\\''/;\n      $escaped_word = \"'$escaped_word'\";\n    }\n    push(@escaped_words, $escaped_word);\n  }\n  return join(\" \", @escaped_words);\n}\n\nsub cleanup {\n  unlink($main::tmpfile_sym);\n  unlink(keys %main::tempnames);\n\n  # We leave any collected profiles in $HOME/jeprof in case the user wants\n  # to look at them later.  We print a message informing them of this.\n  if ((scalar(@main::profile_files) > 0) &&\n      defined($main::collected_profile)) {\n    if (scalar(@main::profile_files) == 1) {\n      print STDERR \"Dynamically gathered profile is in $main::collected_profile\\n\";\n    }\n    print STDERR \"If you want to investigate this profile further, you can do:\\n\";\n    print STDERR \"\\n\";\n    print STDERR \"  jeprof \\\\\\n\";\n    print STDERR \"    $main::prog \\\\\\n\";\n    print STDERR \"    $main::collected_profile\\n\";\n    print STDERR \"\\n\";\n  }\n}\n\nsub sighandler {\n  cleanup();\n  exit(1);\n}\n\nsub error {\n  my $msg = shift;\n  print STDERR $msg;\n  cleanup();\n  exit(1);\n}\n\n\n# Run $nm_command and get all the resulting procedure boundaries whose\n# names match \"$regexp\" and returns them in a hashtable mapping from\n# procedure name to a two-element vector of [start address, end address]\nsub GetProcedureBoundariesViaNm {\n  my $escaped_nm_command = shift;    # shell-escaped\n  my $regexp = shift;\n\n  my $symbol_table = {};\n  open(NM, \"$escaped_nm_command |\") || error(\"$escaped_nm_command: $!\\n\");\n  my $last_start = \"0\";\n  my $routine = \"\";\n  while (<NM>) {\n    s/\\r//g;         # turn windows-looking lines into unix-looking lines\n    if (m/^\\s*([0-9a-f]+) (.) (..*)/) {\n      my $start_val = $1;\n      my $type = $2;\n      my $this_routine = $3;\n\n      # It's possible for two symbols to share the same address, if\n      # one is a zero-length variable (like __start_google_malloc) or\n      # one symbol is a weak alias to another (like __libc_malloc).\n      # In such cases, we want to ignore all values except for the\n      # actual symbol, which in nm-speak has type \"T\".  The logic\n      # below does this, though it's a bit tricky: what happens when\n      # we have a series of lines with the same address, is the first\n      # one gets queued up to be processed.  However, it won't\n      # *actually* be processed until later, when we read a line with\n      # a different address.  That means that as long as we're reading\n      # lines with the same address, we have a chance to replace that\n      # item in the queue, which we do whenever we see a 'T' entry --\n      # that is, a line with type 'T'.  If we never see a 'T' entry,\n      # we'll just go ahead and process the first entry (which never\n      # got touched in the queue), and ignore the others.\n      if ($start_val eq $last_start && $type =~ /t/i) {\n        # We are the 'T' symbol at this address, replace previous symbol.\n        $routine = $this_routine;\n        next;\n      } elsif ($start_val eq $last_start) {\n        # We're not the 'T' symbol at this address, so ignore us.\n        next;\n      }\n\n      if ($this_routine eq $sep_symbol) {\n        $sep_address = HexExtend($start_val);\n      }\n\n      # Tag this routine with the starting address in case the image\n      # has multiple occurrences of this routine.  We use a syntax\n      # that resembles template parameters that are automatically\n      # stripped out by ShortFunctionName()\n      $this_routine .= \"<$start_val>\";\n\n      if (defined($routine) && $routine =~ m/$regexp/) {\n        $symbol_table->{$routine} = [HexExtend($last_start),\n                                     HexExtend($start_val)];\n      }\n      $last_start = $start_val;\n      $routine = $this_routine;\n    } elsif (m/^Loaded image name: (.+)/) {\n      # The win32 nm workalike emits information about the binary it is using.\n      if ($main::opt_debug) { print STDERR \"Using Image $1\\n\"; }\n    } elsif (m/^PDB file name: (.+)/) {\n      # The win32 nm workalike emits information about the pdb it is using.\n      if ($main::opt_debug) { print STDERR \"Using PDB $1\\n\"; }\n    }\n  }\n  close(NM);\n  # Handle the last line in the nm output.  Unfortunately, we don't know\n  # how big this last symbol is, because we don't know how big the file\n  # is.  For now, we just give it a size of 0.\n  # TODO(csilvers): do better here.\n  if (defined($routine) && $routine =~ m/$regexp/) {\n    $symbol_table->{$routine} = [HexExtend($last_start),\n                                 HexExtend($last_start)];\n  }\n  return $symbol_table;\n}\n\n# Gets the procedure boundaries for all routines in \"$image\" whose names\n# match \"$regexp\" and returns them in a hashtable mapping from procedure\n# name to a two-element vector of [start address, end address].\n# Will return an empty map if nm is not installed or not working properly.\nsub GetProcedureBoundaries {\n  my $image = shift;\n  my $regexp = shift;\n\n  # If $image doesn't start with /, then put ./ in front of it.  This works\n  # around an obnoxious bug in our probing of nm -f behavior.\n  # \"nm -f $image\" is supposed to fail on GNU nm, but if:\n  #\n  # a. $image starts with [BbSsPp] (for example, bin/foo/bar), AND\n  # b. you have a.out in your current directory (a not uncommon occurence)\n  #\n  # then \"nm -f $image\" succeeds because -f only looks at the first letter of\n  # the argument, which looks valid because it's [BbSsPp], and then since\n  # there's no image provided, it looks for a.out and finds it.\n  #\n  # This regex makes sure that $image starts with . or /, forcing the -f\n  # parsing to fail since . and / are not valid formats.\n  $image =~ s#^[^/]#./$&#;\n\n  # For libc libraries, the copy in /usr/lib/debug contains debugging symbols\n  my $debugging = DebuggingLibrary($image);\n  if ($debugging) {\n    $image = $debugging;\n  }\n\n  my $nm = $obj_tool_map{\"nm\"};\n  my $cppfilt = $obj_tool_map{\"c++filt\"};\n\n  # nm can fail for two reasons: 1) $image isn't a debug library; 2) nm\n  # binary doesn't support --demangle.  In addition, for OS X we need\n  # to use the -f flag to get 'flat' nm output (otherwise we don't sort\n  # properly and get incorrect results).  Unfortunately, GNU nm uses -f\n  # in an incompatible way.  So first we test whether our nm supports\n  # --demangle and -f.\n  my $demangle_flag = \"\";\n  my $cppfilt_flag = \"\";\n  my $to_devnull = \">$dev_null 2>&1\";\n  if (system(ShellEscape($nm, \"--demangle\", $image) . $to_devnull) == 0) {\n    # In this mode, we do \"nm --demangle <foo>\"\n    $demangle_flag = \"--demangle\";\n    $cppfilt_flag = \"\";\n  } elsif (system(ShellEscape($cppfilt, $image) . $to_devnull) == 0) {\n    # In this mode, we do \"nm <foo> | c++filt\"\n    $cppfilt_flag = \" | \" . ShellEscape($cppfilt);\n  };\n  my $flatten_flag = \"\";\n  if (system(ShellEscape($nm, \"-f\", $image) . $to_devnull) == 0) {\n    $flatten_flag = \"-f\";\n  }\n\n  # Finally, in the case $imagie isn't a debug library, we try again with\n  # -D to at least get *exported* symbols.  If we can't use --demangle,\n  # we use c++filt instead, if it exists on this system.\n  my @nm_commands = (ShellEscape($nm, \"-n\", $flatten_flag, $demangle_flag,\n                                 $image) . \" 2>$dev_null $cppfilt_flag\",\n                     ShellEscape($nm, \"-D\", \"-n\", $flatten_flag, $demangle_flag,\n                                 $image) . \" 2>$dev_null $cppfilt_flag\",\n                     # 6nm is for Go binaries\n                     ShellEscape(\"6nm\", \"$image\") . \" 2>$dev_null | sort\",\n                     );\n\n  # If the executable is an MS Windows PDB-format executable, we'll\n  # have set up obj_tool_map(\"nm_pdb\").  In this case, we actually\n  # want to use both unix nm and windows-specific nm_pdb, since\n  # PDB-format executables can apparently include dwarf .o files.\n  if (exists $obj_tool_map{\"nm_pdb\"}) {\n    push(@nm_commands,\n         ShellEscape($obj_tool_map{\"nm_pdb\"}, \"--demangle\", $image)\n         . \" 2>$dev_null\");\n  }\n\n  foreach my $nm_command (@nm_commands) {\n    my $symbol_table = GetProcedureBoundariesViaNm($nm_command, $regexp);\n    return $symbol_table if (%{$symbol_table});\n  }\n  my $symbol_table = {};\n  return $symbol_table;\n}\n\n\n# The test vectors for AddressAdd/Sub/Inc are 8-16-nibble hex strings.\n# To make them more readable, we add underscores at interesting places.\n# This routine removes the underscores, producing the canonical representation\n# used by jeprof to represent addresses, particularly in the tested routines.\nsub CanonicalHex {\n  my $arg = shift;\n  return join '', (split '_',$arg);\n}\n\n\n# Unit test for AddressAdd:\nsub AddressAddUnitTest {\n  my $test_data_8 = shift;\n  my $test_data_16 = shift;\n  my $error_count = 0;\n  my $fail_count = 0;\n  my $pass_count = 0;\n  # print STDERR \"AddressAddUnitTest: \", 1+$#{$test_data_8}, \" tests\\n\";\n\n  # First a few 8-nibble addresses.  Note that this implementation uses\n  # plain old arithmetic, so a quick sanity check along with verifying what\n  # happens to overflow (we want it to wrap):\n  $address_length = 8;\n  foreach my $row (@{$test_data_8}) {\n    if ($main::opt_debug and $main::opt_test) { print STDERR \"@{$row}\\n\"; }\n    my $sum = AddressAdd ($row->[0], $row->[1]);\n    if ($sum ne $row->[2]) {\n      printf STDERR \"ERROR: %s != %s + %s = %s\\n\", $sum,\n             $row->[0], $row->[1], $row->[2];\n      ++$fail_count;\n    } else {\n      ++$pass_count;\n    }\n  }\n  printf STDERR \"AddressAdd 32-bit tests: %d passes, %d failures\\n\",\n         $pass_count, $fail_count;\n  $error_count = $fail_count;\n  $fail_count = 0;\n  $pass_count = 0;\n\n  # Now 16-nibble addresses.\n  $address_length = 16;\n  foreach my $row (@{$test_data_16}) {\n    if ($main::opt_debug and $main::opt_test) { print STDERR \"@{$row}\\n\"; }\n    my $sum = AddressAdd (CanonicalHex($row->[0]), CanonicalHex($row->[1]));\n    my $expected = join '', (split '_',$row->[2]);\n    if ($sum ne CanonicalHex($row->[2])) {\n      printf STDERR \"ERROR: %s != %s + %s = %s\\n\", $sum,\n             $row->[0], $row->[1], $row->[2];\n      ++$fail_count;\n    } else {\n      ++$pass_count;\n    }\n  }\n  printf STDERR \"AddressAdd 64-bit tests: %d passes, %d failures\\n\",\n         $pass_count, $fail_count;\n  $error_count += $fail_count;\n\n  return $error_count;\n}\n\n\n# Unit test for AddressSub:\nsub AddressSubUnitTest {\n  my $test_data_8 = shift;\n  my $test_data_16 = shift;\n  my $error_count = 0;\n  my $fail_count = 0;\n  my $pass_count = 0;\n  # print STDERR \"AddressSubUnitTest: \", 1+$#{$test_data_8}, \" tests\\n\";\n\n  # First a few 8-nibble addresses.  Note that this implementation uses\n  # plain old arithmetic, so a quick sanity check along with verifying what\n  # happens to overflow (we want it to wrap):\n  $address_length = 8;\n  foreach my $row (@{$test_data_8}) {\n    if ($main::opt_debug and $main::opt_test) { print STDERR \"@{$row}\\n\"; }\n    my $sum = AddressSub ($row->[0], $row->[1]);\n    if ($sum ne $row->[3]) {\n      printf STDERR \"ERROR: %s != %s - %s = %s\\n\", $sum,\n             $row->[0], $row->[1], $row->[3];\n      ++$fail_count;\n    } else {\n      ++$pass_count;\n    }\n  }\n  printf STDERR \"AddressSub 32-bit tests: %d passes, %d failures\\n\",\n         $pass_count, $fail_count;\n  $error_count = $fail_count;\n  $fail_count = 0;\n  $pass_count = 0;\n\n  # Now 16-nibble addresses.\n  $address_length = 16;\n  foreach my $row (@{$test_data_16}) {\n    if ($main::opt_debug and $main::opt_test) { print STDERR \"@{$row}\\n\"; }\n    my $sum = AddressSub (CanonicalHex($row->[0]), CanonicalHex($row->[1]));\n    if ($sum ne CanonicalHex($row->[3])) {\n      printf STDERR \"ERROR: %s != %s - %s = %s\\n\", $sum,\n             $row->[0], $row->[1], $row->[3];\n      ++$fail_count;\n    } else {\n      ++$pass_count;\n    }\n  }\n  printf STDERR \"AddressSub 64-bit tests: %d passes, %d failures\\n\",\n         $pass_count, $fail_count;\n  $error_count += $fail_count;\n\n  return $error_count;\n}\n\n\n# Unit test for AddressInc:\nsub AddressIncUnitTest {\n  my $test_data_8 = shift;\n  my $test_data_16 = shift;\n  my $error_count = 0;\n  my $fail_count = 0;\n  my $pass_count = 0;\n  # print STDERR \"AddressIncUnitTest: \", 1+$#{$test_data_8}, \" tests\\n\";\n\n  # First a few 8-nibble addresses.  Note that this implementation uses\n  # plain old arithmetic, so a quick sanity check along with verifying what\n  # happens to overflow (we want it to wrap):\n  $address_length = 8;\n  foreach my $row (@{$test_data_8}) {\n    if ($main::opt_debug and $main::opt_test) { print STDERR \"@{$row}\\n\"; }\n    my $sum = AddressInc ($row->[0]);\n    if ($sum ne $row->[4]) {\n      printf STDERR \"ERROR: %s != %s + 1 = %s\\n\", $sum,\n             $row->[0], $row->[4];\n      ++$fail_count;\n    } else {\n      ++$pass_count;\n    }\n  }\n  printf STDERR \"AddressInc 32-bit tests: %d passes, %d failures\\n\",\n         $pass_count, $fail_count;\n  $error_count = $fail_count;\n  $fail_count = 0;\n  $pass_count = 0;\n\n  # Now 16-nibble addresses.\n  $address_length = 16;\n  foreach my $row (@{$test_data_16}) {\n    if ($main::opt_debug and $main::opt_test) { print STDERR \"@{$row}\\n\"; }\n    my $sum = AddressInc (CanonicalHex($row->[0]));\n    if ($sum ne CanonicalHex($row->[4])) {\n      printf STDERR \"ERROR: %s != %s + 1 = %s\\n\", $sum,\n             $row->[0], $row->[4];\n      ++$fail_count;\n    } else {\n      ++$pass_count;\n    }\n  }\n  printf STDERR \"AddressInc 64-bit tests: %d passes, %d failures\\n\",\n         $pass_count, $fail_count;\n  $error_count += $fail_count;\n\n  return $error_count;\n}\n\n\n# Driver for unit tests.\n# Currently just the address add/subtract/increment routines for 64-bit.\nsub RunUnitTests {\n  my $error_count = 0;\n\n  # This is a list of tuples [a, b, a+b, a-b, a+1]\n  my $unit_test_data_8 = [\n    [qw(aaaaaaaa 50505050 fafafafa 5a5a5a5a aaaaaaab)],\n    [qw(50505050 aaaaaaaa fafafafa a5a5a5a6 50505051)],\n    [qw(ffffffff aaaaaaaa aaaaaaa9 55555555 00000000)],\n    [qw(00000001 ffffffff 00000000 00000002 00000002)],\n    [qw(00000001 fffffff0 fffffff1 00000011 00000002)],\n  ];\n  my $unit_test_data_16 = [\n    # The implementation handles data in 7-nibble chunks, so those are the\n    # interesting boundaries.\n    [qw(aaaaaaaa 50505050\n        00_000000f_afafafa 00_0000005_a5a5a5a 00_000000a_aaaaaab)],\n    [qw(50505050 aaaaaaaa\n        00_000000f_afafafa ff_ffffffa_5a5a5a6 00_0000005_0505051)],\n    [qw(ffffffff aaaaaaaa\n        00_000001a_aaaaaa9 00_0000005_5555555 00_0000010_0000000)],\n    [qw(00000001 ffffffff\n        00_0000010_0000000 ff_ffffff0_0000002 00_0000000_0000002)],\n    [qw(00000001 fffffff0\n        00_000000f_ffffff1 ff_ffffff0_0000011 00_0000000_0000002)],\n\n    [qw(00_a00000a_aaaaaaa 50505050\n        00_a00000f_afafafa 00_a000005_a5a5a5a 00_a00000a_aaaaaab)],\n    [qw(0f_fff0005_0505050 aaaaaaaa\n        0f_fff000f_afafafa 0f_ffefffa_5a5a5a6 0f_fff0005_0505051)],\n    [qw(00_000000f_fffffff 01_800000a_aaaaaaa\n        01_800001a_aaaaaa9 fe_8000005_5555555 00_0000010_0000000)],\n    [qw(00_0000000_0000001 ff_fffffff_fffffff\n        00_0000000_0000000 00_0000000_0000002 00_0000000_0000002)],\n    [qw(00_0000000_0000001 ff_fffffff_ffffff0\n        ff_fffffff_ffffff1 00_0000000_0000011 00_0000000_0000002)],\n  ];\n\n  $error_count += AddressAddUnitTest($unit_test_data_8, $unit_test_data_16);\n  $error_count += AddressSubUnitTest($unit_test_data_8, $unit_test_data_16);\n  $error_count += AddressIncUnitTest($unit_test_data_8, $unit_test_data_16);\n  if ($error_count > 0) {\n    print STDERR $error_count, \" errors: FAILED\\n\";\n  } else {\n    print STDERR \"PASS\\n\";\n  }\n  exit ($error_count);\n}\n"
  },
  {
    "path": "deps/jemalloc/build-aux/config.guess",
    "content": "#! /bin/sh\n# Attempt to guess a canonical system name.\n#   Copyright 1992-2016 Free Software Foundation, Inc.\n\ntimestamp='2016-10-02'\n\n# This file is free software; you can redistribute it and/or modify it\n# under the terms of the GNU General Public License as published by\n# the Free Software Foundation; either version 3 of the License, or\n# (at your option) any later version.\n#\n# This program is distributed in the hope that it will be useful, but\n# WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n# General Public License for more details.\n#\n# You should have received a copy of the GNU General Public License\n# along with this program; if not, see <http://www.gnu.org/licenses/>.\n#\n# As a special exception to the GNU General Public License, if you\n# distribute this file as part of a program that contains a\n# configuration script generated by Autoconf, you may include it under\n# the same distribution terms that you use for the rest of that\n# program.  This Exception is an additional permission under section 7\n# of the GNU General Public License, version 3 (\"GPLv3\").\n#\n# Originally written by Per Bothner; maintained since 2000 by Ben Elliston.\n#\n# You can get the latest version of this script from:\n# http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess\n#\n# Please send patches to <config-patches@gnu.org>.\n\n\nme=`echo \"$0\" | sed -e 's,.*/,,'`\n\nusage=\"\\\nUsage: $0 [OPTION]\n\nOutput the configuration name of the system \\`$me' is run on.\n\nOperation modes:\n  -h, --help         print this help, then exit\n  -t, --time-stamp   print date of last modification, then exit\n  -v, --version      print version number, then exit\n\nReport bugs and patches to <config-patches@gnu.org>.\"\n\nversion=\"\\\nGNU config.guess ($timestamp)\n\nOriginally written by Per Bothner.\nCopyright 1992-2016 Free Software Foundation, Inc.\n\nThis is free software; see the source for copying conditions.  There is NO\nwarranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\"\n\nhelp=\"\nTry \\`$me --help' for more information.\"\n\n# Parse command line\nwhile test $# -gt 0 ; do\n  case $1 in\n    --time-stamp | --time* | -t )\n       echo \"$timestamp\" ; exit ;;\n    --version | -v )\n       echo \"$version\" ; exit ;;\n    --help | --h* | -h )\n       echo \"$usage\"; exit ;;\n    -- )     # Stop option processing\n       shift; break ;;\n    - )\t# Use stdin as input.\n       break ;;\n    -* )\n       echo \"$me: invalid option $1$help\" >&2\n       exit 1 ;;\n    * )\n       break ;;\n  esac\ndone\n\nif test $# != 0; then\n  echo \"$me: too many arguments$help\" >&2\n  exit 1\nfi\n\ntrap 'exit 1' 1 2 15\n\n# CC_FOR_BUILD -- compiler used by this script. Note that the use of a\n# compiler to aid in system detection is discouraged as it requires\n# temporary files to be created and, as you can see below, it is a\n# headache to deal with in a portable fashion.\n\n# Historically, `CC_FOR_BUILD' used to be named `HOST_CC'. We still\n# use `HOST_CC' if defined, but it is deprecated.\n\n# Portable tmp directory creation inspired by the Autoconf team.\n\nset_cc_for_build='\ntrap \"exitcode=\\$?; (rm -f \\$tmpfiles 2>/dev/null; rmdir \\$tmp 2>/dev/null) && exit \\$exitcode\" 0 ;\ntrap \"rm -f \\$tmpfiles 2>/dev/null; rmdir \\$tmp 2>/dev/null; exit 1\" 1 2 13 15 ;\n: ${TMPDIR=/tmp} ;\n { tmp=`(umask 077 && mktemp -d \"$TMPDIR/cgXXXXXX\") 2>/dev/null` && test -n \"$tmp\" && test -d \"$tmp\" ; } ||\n { test -n \"$RANDOM\" && tmp=$TMPDIR/cg$$-$RANDOM && (umask 077 && mkdir $tmp) ; } ||\n { tmp=$TMPDIR/cg-$$ && (umask 077 && mkdir $tmp) && echo \"Warning: creating insecure temp directory\" >&2 ; } ||\n { echo \"$me: cannot create a temporary directory in $TMPDIR\" >&2 ; exit 1 ; } ;\ndummy=$tmp/dummy ;\ntmpfiles=\"$dummy.c $dummy.o $dummy.rel $dummy\" ;\ncase $CC_FOR_BUILD,$HOST_CC,$CC in\n ,,)    echo \"int x;\" > $dummy.c ;\n\tfor c in cc gcc c89 c99 ; do\n\t  if ($c -c -o $dummy.o $dummy.c) >/dev/null 2>&1 ; then\n\t     CC_FOR_BUILD=\"$c\"; break ;\n\t  fi ;\n\tdone ;\n\tif test x\"$CC_FOR_BUILD\" = x ; then\n\t  CC_FOR_BUILD=no_compiler_found ;\n\tfi\n\t;;\n ,,*)   CC_FOR_BUILD=$CC ;;\n ,*,*)  CC_FOR_BUILD=$HOST_CC ;;\nesac ; set_cc_for_build= ;'\n\n# This is needed to find uname on a Pyramid OSx when run in the BSD universe.\n# (ghazi@noc.rutgers.edu 1994-08-24)\nif (test -f /.attbin/uname) >/dev/null 2>&1 ; then\n\tPATH=$PATH:/.attbin ; export PATH\nfi\n\nUNAME_MACHINE=`(uname -m) 2>/dev/null` || UNAME_MACHINE=unknown\nUNAME_RELEASE=`(uname -r) 2>/dev/null` || UNAME_RELEASE=unknown\nUNAME_SYSTEM=`(uname -s) 2>/dev/null`  || UNAME_SYSTEM=unknown\nUNAME_VERSION=`(uname -v) 2>/dev/null` || UNAME_VERSION=unknown\n\ncase \"${UNAME_SYSTEM}\" in\nLinux|GNU|GNU/*)\n\t# If the system lacks a compiler, then just pick glibc.\n\t# We could probably try harder.\n\tLIBC=gnu\n\n\teval $set_cc_for_build\n\tcat <<-EOF > $dummy.c\n\t#include <features.h>\n\t#if defined(__UCLIBC__)\n\tLIBC=uclibc\n\t#elif defined(__dietlibc__)\n\tLIBC=dietlibc\n\t#else\n\tLIBC=gnu\n\t#endif\n\tEOF\n\teval `$CC_FOR_BUILD -E $dummy.c 2>/dev/null | grep '^LIBC' | sed 's, ,,g'`\n\t;;\nesac\n\n# Note: order is significant - the case branches are not exclusive.\n\ncase \"${UNAME_MACHINE}:${UNAME_SYSTEM}:${UNAME_RELEASE}:${UNAME_VERSION}\" in\n    *:NetBSD:*:*)\n\t# NetBSD (nbsd) targets should (where applicable) match one or\n\t# more of the tuples: *-*-netbsdelf*, *-*-netbsdaout*,\n\t# *-*-netbsdecoff* and *-*-netbsd*.  For targets that recently\n\t# switched to ELF, *-*-netbsd* would select the old\n\t# object file format.  This provides both forward\n\t# compatibility and a consistent mechanism for selecting the\n\t# object file format.\n\t#\n\t# Note: NetBSD doesn't particularly care about the vendor\n\t# portion of the name.  We always set it to \"unknown\".\n\tsysctl=\"sysctl -n hw.machine_arch\"\n\tUNAME_MACHINE_ARCH=`(uname -p 2>/dev/null || \\\n\t    /sbin/$sysctl 2>/dev/null || \\\n\t    /usr/sbin/$sysctl 2>/dev/null || \\\n\t    echo unknown)`\n\tcase \"${UNAME_MACHINE_ARCH}\" in\n\t    armeb) machine=armeb-unknown ;;\n\t    arm*) machine=arm-unknown ;;\n\t    sh3el) machine=shl-unknown ;;\n\t    sh3eb) machine=sh-unknown ;;\n\t    sh5el) machine=sh5le-unknown ;;\n\t    earmv*)\n\t\tarch=`echo ${UNAME_MACHINE_ARCH} | sed -e 's,^e\\(armv[0-9]\\).*$,\\1,'`\n\t\tendian=`echo ${UNAME_MACHINE_ARCH} | sed -ne 's,^.*\\(eb\\)$,\\1,p'`\n\t\tmachine=${arch}${endian}-unknown\n\t\t;;\n\t    *) machine=${UNAME_MACHINE_ARCH}-unknown ;;\n\tesac\n\t# The Operating System including object format, if it has switched\n\t# to ELF recently (or will in the future) and ABI.\n\tcase \"${UNAME_MACHINE_ARCH}\" in\n\t    earm*)\n\t\tos=netbsdelf\n\t\t;;\n\t    arm*|i386|m68k|ns32k|sh3*|sparc|vax)\n\t\teval $set_cc_for_build\n\t\tif echo __ELF__ | $CC_FOR_BUILD -E - 2>/dev/null \\\n\t\t\t| grep -q __ELF__\n\t\tthen\n\t\t    # Once all utilities can be ECOFF (netbsdecoff) or a.out (netbsdaout).\n\t\t    # Return netbsd for either.  FIX?\n\t\t    os=netbsd\n\t\telse\n\t\t    os=netbsdelf\n\t\tfi\n\t\t;;\n\t    *)\n\t\tos=netbsd\n\t\t;;\n\tesac\n\t# Determine ABI tags.\n\tcase \"${UNAME_MACHINE_ARCH}\" in\n\t    earm*)\n\t\texpr='s/^earmv[0-9]/-eabi/;s/eb$//'\n\t\tabi=`echo ${UNAME_MACHINE_ARCH} | sed -e \"$expr\"`\n\t\t;;\n\tesac\n\t# The OS release\n\t# Debian GNU/NetBSD machines have a different userland, and\n\t# thus, need a distinct triplet. However, they do not need\n\t# kernel version information, so it can be replaced with a\n\t# suitable tag, in the style of linux-gnu.\n\tcase \"${UNAME_VERSION}\" in\n\t    Debian*)\n\t\trelease='-gnu'\n\t\t;;\n\t    *)\n\t\trelease=`echo ${UNAME_RELEASE} | sed -e 's/[-_].*//' | cut -d. -f1,2`\n\t\t;;\n\tesac\n\t# Since CPU_TYPE-MANUFACTURER-KERNEL-OPERATING_SYSTEM:\n\t# contains redundant information, the shorter form:\n\t# CPU_TYPE-MANUFACTURER-OPERATING_SYSTEM is used.\n\techo \"${machine}-${os}${release}${abi}\"\n\texit ;;\n    *:Bitrig:*:*)\n\tUNAME_MACHINE_ARCH=`arch | sed 's/Bitrig.//'`\n\techo ${UNAME_MACHINE_ARCH}-unknown-bitrig${UNAME_RELEASE}\n\texit ;;\n    *:OpenBSD:*:*)\n\tUNAME_MACHINE_ARCH=`arch | sed 's/OpenBSD.//'`\n\techo ${UNAME_MACHINE_ARCH}-unknown-openbsd${UNAME_RELEASE}\n\texit ;;\n    *:LibertyBSD:*:*)\n\tUNAME_MACHINE_ARCH=`arch | sed 's/^.*BSD\\.//'`\n\techo ${UNAME_MACHINE_ARCH}-unknown-libertybsd${UNAME_RELEASE}\n\texit ;;\n    *:ekkoBSD:*:*)\n\techo ${UNAME_MACHINE}-unknown-ekkobsd${UNAME_RELEASE}\n\texit ;;\n    *:SolidBSD:*:*)\n\techo ${UNAME_MACHINE}-unknown-solidbsd${UNAME_RELEASE}\n\texit ;;\n    macppc:MirBSD:*:*)\n\techo powerpc-unknown-mirbsd${UNAME_RELEASE}\n\texit ;;\n    *:MirBSD:*:*)\n\techo ${UNAME_MACHINE}-unknown-mirbsd${UNAME_RELEASE}\n\texit ;;\n    *:Sortix:*:*)\n\techo ${UNAME_MACHINE}-unknown-sortix\n\texit ;;\n    alpha:OSF1:*:*)\n\tcase $UNAME_RELEASE in\n\t*4.0)\n\t\tUNAME_RELEASE=`/usr/sbin/sizer -v | awk '{print $3}'`\n\t\t;;\n\t*5.*)\n\t\tUNAME_RELEASE=`/usr/sbin/sizer -v | awk '{print $4}'`\n\t\t;;\n\tesac\n\t# According to Compaq, /usr/sbin/psrinfo has been available on\n\t# OSF/1 and Tru64 systems produced since 1995.  I hope that\n\t# covers most systems running today.  This code pipes the CPU\n\t# types through head -n 1, so we only detect the type of CPU 0.\n\tALPHA_CPU_TYPE=`/usr/sbin/psrinfo -v | sed -n -e 's/^  The alpha \\(.*\\) processor.*$/\\1/p' | head -n 1`\n\tcase \"$ALPHA_CPU_TYPE\" in\n\t    \"EV4 (21064)\")\n\t\tUNAME_MACHINE=alpha ;;\n\t    \"EV4.5 (21064)\")\n\t\tUNAME_MACHINE=alpha ;;\n\t    \"LCA4 (21066/21068)\")\n\t\tUNAME_MACHINE=alpha ;;\n\t    \"EV5 (21164)\")\n\t\tUNAME_MACHINE=alphaev5 ;;\n\t    \"EV5.6 (21164A)\")\n\t\tUNAME_MACHINE=alphaev56 ;;\n\t    \"EV5.6 (21164PC)\")\n\t\tUNAME_MACHINE=alphapca56 ;;\n\t    \"EV5.7 (21164PC)\")\n\t\tUNAME_MACHINE=alphapca57 ;;\n\t    \"EV6 (21264)\")\n\t\tUNAME_MACHINE=alphaev6 ;;\n\t    \"EV6.7 (21264A)\")\n\t\tUNAME_MACHINE=alphaev67 ;;\n\t    \"EV6.8CB (21264C)\")\n\t\tUNAME_MACHINE=alphaev68 ;;\n\t    \"EV6.8AL (21264B)\")\n\t\tUNAME_MACHINE=alphaev68 ;;\n\t    \"EV6.8CX (21264D)\")\n\t\tUNAME_MACHINE=alphaev68 ;;\n\t    \"EV6.9A (21264/EV69A)\")\n\t\tUNAME_MACHINE=alphaev69 ;;\n\t    \"EV7 (21364)\")\n\t\tUNAME_MACHINE=alphaev7 ;;\n\t    \"EV7.9 (21364A)\")\n\t\tUNAME_MACHINE=alphaev79 ;;\n\tesac\n\t# A Pn.n version is a patched version.\n\t# A Vn.n version is a released version.\n\t# A Tn.n version is a released field test version.\n\t# A Xn.n version is an unreleased experimental baselevel.\n\t# 1.2 uses \"1.2\" for uname -r.\n\techo ${UNAME_MACHINE}-dec-osf`echo ${UNAME_RELEASE} | sed -e 's/^[PVTX]//' | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz`\n\t# Reset EXIT trap before exiting to avoid spurious non-zero exit code.\n\texitcode=$?\n\ttrap '' 0\n\texit $exitcode ;;\n    Alpha\\ *:Windows_NT*:*)\n\t# How do we know it's Interix rather than the generic POSIX subsystem?\n\t# Should we change UNAME_MACHINE based on the output of uname instead\n\t# of the specific Alpha model?\n\techo alpha-pc-interix\n\texit ;;\n    21064:Windows_NT:50:3)\n\techo alpha-dec-winnt3.5\n\texit ;;\n    Amiga*:UNIX_System_V:4.0:*)\n\techo m68k-unknown-sysv4\n\texit ;;\n    *:[Aa]miga[Oo][Ss]:*:*)\n\techo ${UNAME_MACHINE}-unknown-amigaos\n\texit ;;\n    *:[Mm]orph[Oo][Ss]:*:*)\n\techo ${UNAME_MACHINE}-unknown-morphos\n\texit ;;\n    *:OS/390:*:*)\n\techo i370-ibm-openedition\n\texit ;;\n    *:z/VM:*:*)\n\techo s390-ibm-zvmoe\n\texit ;;\n    *:OS400:*:*)\n\techo powerpc-ibm-os400\n\texit ;;\n    arm:RISC*:1.[012]*:*|arm:riscix:1.[012]*:*)\n\techo arm-acorn-riscix${UNAME_RELEASE}\n\texit ;;\n    arm*:riscos:*:*|arm*:RISCOS:*:*)\n\techo arm-unknown-riscos\n\texit ;;\n    SR2?01:HI-UX/MPP:*:* | SR8000:HI-UX/MPP:*:*)\n\techo hppa1.1-hitachi-hiuxmpp\n\texit ;;\n    Pyramid*:OSx*:*:* | MIS*:OSx*:*:* | MIS*:SMP_DC-OSx*:*:*)\n\t# akee@wpdis03.wpafb.af.mil (Earle F. Ake) contributed MIS and NILE.\n\tif test \"`(/bin/universe) 2>/dev/null`\" = att ; then\n\t\techo pyramid-pyramid-sysv3\n\telse\n\t\techo pyramid-pyramid-bsd\n\tfi\n\texit ;;\n    NILE*:*:*:dcosx)\n\techo pyramid-pyramid-svr4\n\texit ;;\n    DRS?6000:unix:4.0:6*)\n\techo sparc-icl-nx6\n\texit ;;\n    DRS?6000:UNIX_SV:4.2*:7* | DRS?6000:isis:4.2*:7*)\n\tcase `/usr/bin/uname -p` in\n\t    sparc) echo sparc-icl-nx7; exit ;;\n\tesac ;;\n    s390x:SunOS:*:*)\n\techo ${UNAME_MACHINE}-ibm-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`\n\texit ;;\n    sun4H:SunOS:5.*:*)\n\techo sparc-hal-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`\n\texit ;;\n    sun4*:SunOS:5.*:* | tadpole*:SunOS:5.*:*)\n\techo sparc-sun-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`\n\texit ;;\n    i86pc:AuroraUX:5.*:* | i86xen:AuroraUX:5.*:*)\n\techo i386-pc-auroraux${UNAME_RELEASE}\n\texit ;;\n    i86pc:SunOS:5.*:* | i86xen:SunOS:5.*:*)\n\teval $set_cc_for_build\n\tSUN_ARCH=i386\n\t# If there is a compiler, see if it is configured for 64-bit objects.\n\t# Note that the Sun cc does not turn __LP64__ into 1 like gcc does.\n\t# This test works for both compilers.\n\tif [ \"$CC_FOR_BUILD\" != no_compiler_found ]; then\n\t    if (echo '#ifdef __amd64'; echo IS_64BIT_ARCH; echo '#endif') | \\\n\t\t(CCOPTS=\"\" $CC_FOR_BUILD -E - 2>/dev/null) | \\\n\t\tgrep IS_64BIT_ARCH >/dev/null\n\t    then\n\t\tSUN_ARCH=x86_64\n\t    fi\n\tfi\n\techo ${SUN_ARCH}-pc-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`\n\texit ;;\n    sun4*:SunOS:6*:*)\n\t# According to config.sub, this is the proper way to canonicalize\n\t# SunOS6.  Hard to guess exactly what SunOS6 will be like, but\n\t# it's likely to be more like Solaris than SunOS4.\n\techo sparc-sun-solaris3`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`\n\texit ;;\n    sun4*:SunOS:*:*)\n\tcase \"`/usr/bin/arch -k`\" in\n\t    Series*|S4*)\n\t\tUNAME_RELEASE=`uname -v`\n\t\t;;\n\tesac\n\t# Japanese Language versions have a version number like `4.1.3-JL'.\n\techo sparc-sun-sunos`echo ${UNAME_RELEASE}|sed -e 's/-/_/'`\n\texit ;;\n    sun3*:SunOS:*:*)\n\techo m68k-sun-sunos${UNAME_RELEASE}\n\texit ;;\n    sun*:*:4.2BSD:*)\n\tUNAME_RELEASE=`(sed 1q /etc/motd | awk '{print substr($5,1,3)}') 2>/dev/null`\n\ttest \"x${UNAME_RELEASE}\" = x && UNAME_RELEASE=3\n\tcase \"`/bin/arch`\" in\n\t    sun3)\n\t\techo m68k-sun-sunos${UNAME_RELEASE}\n\t\t;;\n\t    sun4)\n\t\techo sparc-sun-sunos${UNAME_RELEASE}\n\t\t;;\n\tesac\n\texit ;;\n    aushp:SunOS:*:*)\n\techo sparc-auspex-sunos${UNAME_RELEASE}\n\texit ;;\n    # The situation for MiNT is a little confusing.  The machine name\n    # can be virtually everything (everything which is not\n    # \"atarist\" or \"atariste\" at least should have a processor\n    # > m68000).  The system name ranges from \"MiNT\" over \"FreeMiNT\"\n    # to the lowercase version \"mint\" (or \"freemint\").  Finally\n    # the system name \"TOS\" denotes a system which is actually not\n    # MiNT.  But MiNT is downward compatible to TOS, so this should\n    # be no problem.\n    atarist[e]:*MiNT:*:* | atarist[e]:*mint:*:* | atarist[e]:*TOS:*:*)\n\techo m68k-atari-mint${UNAME_RELEASE}\n\texit ;;\n    atari*:*MiNT:*:* | atari*:*mint:*:* | atarist[e]:*TOS:*:*)\n\techo m68k-atari-mint${UNAME_RELEASE}\n\texit ;;\n    *falcon*:*MiNT:*:* | *falcon*:*mint:*:* | *falcon*:*TOS:*:*)\n\techo m68k-atari-mint${UNAME_RELEASE}\n\texit ;;\n    milan*:*MiNT:*:* | milan*:*mint:*:* | *milan*:*TOS:*:*)\n\techo m68k-milan-mint${UNAME_RELEASE}\n\texit ;;\n    hades*:*MiNT:*:* | hades*:*mint:*:* | *hades*:*TOS:*:*)\n\techo m68k-hades-mint${UNAME_RELEASE}\n\texit ;;\n    *:*MiNT:*:* | *:*mint:*:* | *:*TOS:*:*)\n\techo m68k-unknown-mint${UNAME_RELEASE}\n\texit ;;\n    m68k:machten:*:*)\n\techo m68k-apple-machten${UNAME_RELEASE}\n\texit ;;\n    powerpc:machten:*:*)\n\techo powerpc-apple-machten${UNAME_RELEASE}\n\texit ;;\n    RISC*:Mach:*:*)\n\techo mips-dec-mach_bsd4.3\n\texit ;;\n    RISC*:ULTRIX:*:*)\n\techo mips-dec-ultrix${UNAME_RELEASE}\n\texit ;;\n    VAX*:ULTRIX*:*:*)\n\techo vax-dec-ultrix${UNAME_RELEASE}\n\texit ;;\n    2020:CLIX:*:* | 2430:CLIX:*:*)\n\techo clipper-intergraph-clix${UNAME_RELEASE}\n\texit ;;\n    mips:*:*:UMIPS | mips:*:*:RISCos)\n\teval $set_cc_for_build\n\tsed 's/^\t//' << EOF >$dummy.c\n#ifdef __cplusplus\n#include <stdio.h>  /* for printf() prototype */\n\tint main (int argc, char *argv[]) {\n#else\n\tint main (argc, argv) int argc; char *argv[]; {\n#endif\n\t#if defined (host_mips) && defined (MIPSEB)\n\t#if defined (SYSTYPE_SYSV)\n\t  printf (\"mips-mips-riscos%ssysv\\n\", argv[1]); exit (0);\n\t#endif\n\t#if defined (SYSTYPE_SVR4)\n\t  printf (\"mips-mips-riscos%ssvr4\\n\", argv[1]); exit (0);\n\t#endif\n\t#if defined (SYSTYPE_BSD43) || defined(SYSTYPE_BSD)\n\t  printf (\"mips-mips-riscos%sbsd\\n\", argv[1]); exit (0);\n\t#endif\n\t#endif\n\t  exit (-1);\n\t}\nEOF\n\t$CC_FOR_BUILD -o $dummy $dummy.c &&\n\t  dummyarg=`echo \"${UNAME_RELEASE}\" | sed -n 's/\\([0-9]*\\).*/\\1/p'` &&\n\t  SYSTEM_NAME=`$dummy $dummyarg` &&\n\t    { echo \"$SYSTEM_NAME\"; exit; }\n\techo mips-mips-riscos${UNAME_RELEASE}\n\texit ;;\n    Motorola:PowerMAX_OS:*:*)\n\techo powerpc-motorola-powermax\n\texit ;;\n    Motorola:*:4.3:PL8-*)\n\techo powerpc-harris-powermax\n\texit ;;\n    Night_Hawk:*:*:PowerMAX_OS | Synergy:PowerMAX_OS:*:*)\n\techo powerpc-harris-powermax\n\texit ;;\n    Night_Hawk:Power_UNIX:*:*)\n\techo powerpc-harris-powerunix\n\texit ;;\n    m88k:CX/UX:7*:*)\n\techo m88k-harris-cxux7\n\texit ;;\n    m88k:*:4*:R4*)\n\techo m88k-motorola-sysv4\n\texit ;;\n    m88k:*:3*:R3*)\n\techo m88k-motorola-sysv3\n\texit ;;\n    AViiON:dgux:*:*)\n\t# DG/UX returns AViiON for all architectures\n\tUNAME_PROCESSOR=`/usr/bin/uname -p`\n\tif [ $UNAME_PROCESSOR = mc88100 ] || [ $UNAME_PROCESSOR = mc88110 ]\n\tthen\n\t    if [ ${TARGET_BINARY_INTERFACE}x = m88kdguxelfx ] || \\\n\t       [ ${TARGET_BINARY_INTERFACE}x = x ]\n\t    then\n\t\techo m88k-dg-dgux${UNAME_RELEASE}\n\t    else\n\t\techo m88k-dg-dguxbcs${UNAME_RELEASE}\n\t    fi\n\telse\n\t    echo i586-dg-dgux${UNAME_RELEASE}\n\tfi\n\texit ;;\n    M88*:DolphinOS:*:*)\t# DolphinOS (SVR3)\n\techo m88k-dolphin-sysv3\n\texit ;;\n    M88*:*:R3*:*)\n\t# Delta 88k system running SVR3\n\techo m88k-motorola-sysv3\n\texit ;;\n    XD88*:*:*:*) # Tektronix XD88 system running UTekV (SVR3)\n\techo m88k-tektronix-sysv3\n\texit ;;\n    Tek43[0-9][0-9]:UTek:*:*) # Tektronix 4300 system running UTek (BSD)\n\techo m68k-tektronix-bsd\n\texit ;;\n    *:IRIX*:*:*)\n\techo mips-sgi-irix`echo ${UNAME_RELEASE}|sed -e 's/-/_/g'`\n\texit ;;\n    ????????:AIX?:[12].1:2)   # AIX 2.2.1 or AIX 2.1.1 is RT/PC AIX.\n\techo romp-ibm-aix     # uname -m gives an 8 hex-code CPU id\n\texit ;;               # Note that: echo \"'`uname -s`'\" gives 'AIX '\n    i*86:AIX:*:*)\n\techo i386-ibm-aix\n\texit ;;\n    ia64:AIX:*:*)\n\tif [ -x /usr/bin/oslevel ] ; then\n\t\tIBM_REV=`/usr/bin/oslevel`\n\telse\n\t\tIBM_REV=${UNAME_VERSION}.${UNAME_RELEASE}\n\tfi\n\techo ${UNAME_MACHINE}-ibm-aix${IBM_REV}\n\texit ;;\n    *:AIX:2:3)\n\tif grep bos325 /usr/include/stdio.h >/dev/null 2>&1; then\n\t\teval $set_cc_for_build\n\t\tsed 's/^\t\t//' << EOF >$dummy.c\n\t\t#include <sys/systemcfg.h>\n\n\t\tmain()\n\t\t\t{\n\t\t\tif (!__power_pc())\n\t\t\t\texit(1);\n\t\t\tputs(\"powerpc-ibm-aix3.2.5\");\n\t\t\texit(0);\n\t\t\t}\nEOF\n\t\tif $CC_FOR_BUILD -o $dummy $dummy.c && SYSTEM_NAME=`$dummy`\n\t\tthen\n\t\t\techo \"$SYSTEM_NAME\"\n\t\telse\n\t\t\techo rs6000-ibm-aix3.2.5\n\t\tfi\n\telif grep bos324 /usr/include/stdio.h >/dev/null 2>&1; then\n\t\techo rs6000-ibm-aix3.2.4\n\telse\n\t\techo rs6000-ibm-aix3.2\n\tfi\n\texit ;;\n    *:AIX:*:[4567])\n\tIBM_CPU_ID=`/usr/sbin/lsdev -C -c processor -S available | sed 1q | awk '{ print $1 }'`\n\tif /usr/sbin/lsattr -El ${IBM_CPU_ID} | grep ' POWER' >/dev/null 2>&1; then\n\t\tIBM_ARCH=rs6000\n\telse\n\t\tIBM_ARCH=powerpc\n\tfi\n\tif [ -x /usr/bin/lslpp ] ; then\n\t\tIBM_REV=`/usr/bin/lslpp -Lqc bos.rte.libc |\n\t\t\t   awk -F: '{ print $3 }' | sed s/[0-9]*$/0/`\n\telse\n\t\tIBM_REV=${UNAME_VERSION}.${UNAME_RELEASE}\n\tfi\n\techo ${IBM_ARCH}-ibm-aix${IBM_REV}\n\texit ;;\n    *:AIX:*:*)\n\techo rs6000-ibm-aix\n\texit ;;\n    ibmrt:4.4BSD:*|romp-ibm:BSD:*)\n\techo romp-ibm-bsd4.4\n\texit ;;\n    ibmrt:*BSD:*|romp-ibm:BSD:*)            # covers RT/PC BSD and\n\techo romp-ibm-bsd${UNAME_RELEASE}   # 4.3 with uname added to\n\texit ;;                             # report: romp-ibm BSD 4.3\n    *:BOSX:*:*)\n\techo rs6000-bull-bosx\n\texit ;;\n    DPX/2?00:B.O.S.:*:*)\n\techo m68k-bull-sysv3\n\texit ;;\n    9000/[34]??:4.3bsd:1.*:*)\n\techo m68k-hp-bsd\n\texit ;;\n    hp300:4.4BSD:*:* | 9000/[34]??:4.3bsd:2.*:*)\n\techo m68k-hp-bsd4.4\n\texit ;;\n    9000/[34678]??:HP-UX:*:*)\n\tHPUX_REV=`echo ${UNAME_RELEASE}|sed -e 's/[^.]*.[0B]*//'`\n\tcase \"${UNAME_MACHINE}\" in\n\t    9000/31? )            HP_ARCH=m68000 ;;\n\t    9000/[34]?? )         HP_ARCH=m68k ;;\n\t    9000/[678][0-9][0-9])\n\t\tif [ -x /usr/bin/getconf ]; then\n\t\t    sc_cpu_version=`/usr/bin/getconf SC_CPU_VERSION 2>/dev/null`\n\t\t    sc_kernel_bits=`/usr/bin/getconf SC_KERNEL_BITS 2>/dev/null`\n\t\t    case \"${sc_cpu_version}\" in\n\t\t      523) HP_ARCH=hppa1.0 ;; # CPU_PA_RISC1_0\n\t\t      528) HP_ARCH=hppa1.1 ;; # CPU_PA_RISC1_1\n\t\t      532)                      # CPU_PA_RISC2_0\n\t\t\tcase \"${sc_kernel_bits}\" in\n\t\t\t  32) HP_ARCH=hppa2.0n ;;\n\t\t\t  64) HP_ARCH=hppa2.0w ;;\n\t\t\t  '') HP_ARCH=hppa2.0 ;;   # HP-UX 10.20\n\t\t\tesac ;;\n\t\t    esac\n\t\tfi\n\t\tif [ \"${HP_ARCH}\" = \"\" ]; then\n\t\t    eval $set_cc_for_build\n\t\t    sed 's/^\t\t//' << EOF >$dummy.c\n\n\t\t#define _HPUX_SOURCE\n\t\t#include <stdlib.h>\n\t\t#include <unistd.h>\n\n\t\tint main ()\n\t\t{\n\t\t#if defined(_SC_KERNEL_BITS)\n\t\t    long bits = sysconf(_SC_KERNEL_BITS);\n\t\t#endif\n\t\t    long cpu  = sysconf (_SC_CPU_VERSION);\n\n\t\t    switch (cpu)\n\t\t\t{\n\t\t\tcase CPU_PA_RISC1_0: puts (\"hppa1.0\"); break;\n\t\t\tcase CPU_PA_RISC1_1: puts (\"hppa1.1\"); break;\n\t\t\tcase CPU_PA_RISC2_0:\n\t\t#if defined(_SC_KERNEL_BITS)\n\t\t\t    switch (bits)\n\t\t\t\t{\n\t\t\t\tcase 64: puts (\"hppa2.0w\"); break;\n\t\t\t\tcase 32: puts (\"hppa2.0n\"); break;\n\t\t\t\tdefault: puts (\"hppa2.0\"); break;\n\t\t\t\t} break;\n\t\t#else  /* !defined(_SC_KERNEL_BITS) */\n\t\t\t    puts (\"hppa2.0\"); break;\n\t\t#endif\n\t\t\tdefault: puts (\"hppa1.0\"); break;\n\t\t\t}\n\t\t    exit (0);\n\t\t}\nEOF\n\t\t    (CCOPTS=\"\" $CC_FOR_BUILD -o $dummy $dummy.c 2>/dev/null) && HP_ARCH=`$dummy`\n\t\t    test -z \"$HP_ARCH\" && HP_ARCH=hppa\n\t\tfi ;;\n\tesac\n\tif [ ${HP_ARCH} = hppa2.0w ]\n\tthen\n\t    eval $set_cc_for_build\n\n\t    # hppa2.0w-hp-hpux* has a 64-bit kernel and a compiler generating\n\t    # 32-bit code.  hppa64-hp-hpux* has the same kernel and a compiler\n\t    # generating 64-bit code.  GNU and HP use different nomenclature:\n\t    #\n\t    # $ CC_FOR_BUILD=cc ./config.guess\n\t    # => hppa2.0w-hp-hpux11.23\n\t    # $ CC_FOR_BUILD=\"cc +DA2.0w\" ./config.guess\n\t    # => hppa64-hp-hpux11.23\n\n\t    if echo __LP64__ | (CCOPTS=\"\" $CC_FOR_BUILD -E - 2>/dev/null) |\n\t\tgrep -q __LP64__\n\t    then\n\t\tHP_ARCH=hppa2.0w\n\t    else\n\t\tHP_ARCH=hppa64\n\t    fi\n\tfi\n\techo ${HP_ARCH}-hp-hpux${HPUX_REV}\n\texit ;;\n    ia64:HP-UX:*:*)\n\tHPUX_REV=`echo ${UNAME_RELEASE}|sed -e 's/[^.]*.[0B]*//'`\n\techo ia64-hp-hpux${HPUX_REV}\n\texit ;;\n    3050*:HI-UX:*:*)\n\teval $set_cc_for_build\n\tsed 's/^\t//' << EOF >$dummy.c\n\t#include <unistd.h>\n\tint\n\tmain ()\n\t{\n\t  long cpu = sysconf (_SC_CPU_VERSION);\n\t  /* The order matters, because CPU_IS_HP_MC68K erroneously returns\n\t     true for CPU_PA_RISC1_0.  CPU_IS_PA_RISC returns correct\n\t     results, however.  */\n\t  if (CPU_IS_PA_RISC (cpu))\n\t    {\n\t      switch (cpu)\n\t\t{\n\t\t  case CPU_PA_RISC1_0: puts (\"hppa1.0-hitachi-hiuxwe2\"); break;\n\t\t  case CPU_PA_RISC1_1: puts (\"hppa1.1-hitachi-hiuxwe2\"); break;\n\t\t  case CPU_PA_RISC2_0: puts (\"hppa2.0-hitachi-hiuxwe2\"); break;\n\t\t  default: puts (\"hppa-hitachi-hiuxwe2\"); break;\n\t\t}\n\t    }\n\t  else if (CPU_IS_HP_MC68K (cpu))\n\t    puts (\"m68k-hitachi-hiuxwe2\");\n\t  else puts (\"unknown-hitachi-hiuxwe2\");\n\t  exit (0);\n\t}\nEOF\n\t$CC_FOR_BUILD -o $dummy $dummy.c && SYSTEM_NAME=`$dummy` &&\n\t\t{ echo \"$SYSTEM_NAME\"; exit; }\n\techo unknown-hitachi-hiuxwe2\n\texit ;;\n    9000/7??:4.3bsd:*:* | 9000/8?[79]:4.3bsd:*:* )\n\techo hppa1.1-hp-bsd\n\texit ;;\n    9000/8??:4.3bsd:*:*)\n\techo hppa1.0-hp-bsd\n\texit ;;\n    *9??*:MPE/iX:*:* | *3000*:MPE/iX:*:*)\n\techo hppa1.0-hp-mpeix\n\texit ;;\n    hp7??:OSF1:*:* | hp8?[79]:OSF1:*:* )\n\techo hppa1.1-hp-osf\n\texit ;;\n    hp8??:OSF1:*:*)\n\techo hppa1.0-hp-osf\n\texit ;;\n    i*86:OSF1:*:*)\n\tif [ -x /usr/sbin/sysversion ] ; then\n\t    echo ${UNAME_MACHINE}-unknown-osf1mk\n\telse\n\t    echo ${UNAME_MACHINE}-unknown-osf1\n\tfi\n\texit ;;\n    parisc*:Lites*:*:*)\n\techo hppa1.1-hp-lites\n\texit ;;\n    C1*:ConvexOS:*:* | convex:ConvexOS:C1*:*)\n\techo c1-convex-bsd\n\texit ;;\n    C2*:ConvexOS:*:* | convex:ConvexOS:C2*:*)\n\tif getsysinfo -f scalar_acc\n\tthen echo c32-convex-bsd\n\telse echo c2-convex-bsd\n\tfi\n\texit ;;\n    C34*:ConvexOS:*:* | convex:ConvexOS:C34*:*)\n\techo c34-convex-bsd\n\texit ;;\n    C38*:ConvexOS:*:* | convex:ConvexOS:C38*:*)\n\techo c38-convex-bsd\n\texit ;;\n    C4*:ConvexOS:*:* | convex:ConvexOS:C4*:*)\n\techo c4-convex-bsd\n\texit ;;\n    CRAY*Y-MP:*:*:*)\n\techo ymp-cray-unicos${UNAME_RELEASE} | sed -e 's/\\.[^.]*$/.X/'\n\texit ;;\n    CRAY*[A-Z]90:*:*:*)\n\techo ${UNAME_MACHINE}-cray-unicos${UNAME_RELEASE} \\\n\t| sed -e 's/CRAY.*\\([A-Z]90\\)/\\1/' \\\n\t      -e y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/ \\\n\t      -e 's/\\.[^.]*$/.X/'\n\texit ;;\n    CRAY*TS:*:*:*)\n\techo t90-cray-unicos${UNAME_RELEASE} | sed -e 's/\\.[^.]*$/.X/'\n\texit ;;\n    CRAY*T3E:*:*:*)\n\techo alphaev5-cray-unicosmk${UNAME_RELEASE} | sed -e 's/\\.[^.]*$/.X/'\n\texit ;;\n    CRAY*SV1:*:*:*)\n\techo sv1-cray-unicos${UNAME_RELEASE} | sed -e 's/\\.[^.]*$/.X/'\n\texit ;;\n    *:UNICOS/mp:*:*)\n\techo craynv-cray-unicosmp${UNAME_RELEASE} | sed -e 's/\\.[^.]*$/.X/'\n\texit ;;\n    F30[01]:UNIX_System_V:*:* | F700:UNIX_System_V:*:*)\n\tFUJITSU_PROC=`uname -m | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz`\n\tFUJITSU_SYS=`uname -p | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz | sed -e 's/\\///'`\n\tFUJITSU_REL=`echo ${UNAME_RELEASE} | sed -e 's/ /_/'`\n\techo \"${FUJITSU_PROC}-fujitsu-${FUJITSU_SYS}${FUJITSU_REL}\"\n\texit ;;\n    5000:UNIX_System_V:4.*:*)\n\tFUJITSU_SYS=`uname -p | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz | sed -e 's/\\///'`\n\tFUJITSU_REL=`echo ${UNAME_RELEASE} | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz | sed -e 's/ /_/'`\n\techo \"sparc-fujitsu-${FUJITSU_SYS}${FUJITSU_REL}\"\n\texit ;;\n    i*86:BSD/386:*:* | i*86:BSD/OS:*:* | *:Ascend\\ Embedded/OS:*:*)\n\techo ${UNAME_MACHINE}-pc-bsdi${UNAME_RELEASE}\n\texit ;;\n    sparc*:BSD/OS:*:*)\n\techo sparc-unknown-bsdi${UNAME_RELEASE}\n\texit ;;\n    *:BSD/OS:*:*)\n\techo ${UNAME_MACHINE}-unknown-bsdi${UNAME_RELEASE}\n\texit ;;\n    *:FreeBSD:*:*)\n\tUNAME_PROCESSOR=`/usr/bin/uname -p`\n\tcase ${UNAME_PROCESSOR} in\n\t    amd64)\n\t\techo x86_64-unknown-freebsd`echo ${UNAME_RELEASE}|sed -e 's/[-(].*//'` ;;\n\t    *)\n\t\techo ${UNAME_PROCESSOR}-unknown-freebsd`echo ${UNAME_RELEASE}|sed -e 's/[-(].*//'` ;;\n\tesac\n\texit ;;\n    i*:CYGWIN*:*)\n\techo ${UNAME_MACHINE}-pc-cygwin\n\texit ;;\n    *:MINGW64*:*)\n\techo ${UNAME_MACHINE}-pc-mingw64\n\texit ;;\n    *:MINGW*:*)\n\techo ${UNAME_MACHINE}-pc-mingw32\n\texit ;;\n    *:MSYS*:*)\n\techo ${UNAME_MACHINE}-pc-msys\n\texit ;;\n    i*:windows32*:*)\n\t# uname -m includes \"-pc\" on this system.\n\techo ${UNAME_MACHINE}-mingw32\n\texit ;;\n    i*:PW*:*)\n\techo ${UNAME_MACHINE}-pc-pw32\n\texit ;;\n    *:Interix*:*)\n\tcase ${UNAME_MACHINE} in\n\t    x86)\n\t\techo i586-pc-interix${UNAME_RELEASE}\n\t\texit ;;\n\t    authenticamd | genuineintel | EM64T)\n\t\techo x86_64-unknown-interix${UNAME_RELEASE}\n\t\texit ;;\n\t    IA64)\n\t\techo ia64-unknown-interix${UNAME_RELEASE}\n\t\texit ;;\n\tesac ;;\n    [345]86:Windows_95:* | [345]86:Windows_98:* | [345]86:Windows_NT:*)\n\techo i${UNAME_MACHINE}-pc-mks\n\texit ;;\n    8664:Windows_NT:*)\n\techo x86_64-pc-mks\n\texit ;;\n    i*:Windows_NT*:* | Pentium*:Windows_NT*:*)\n\t# How do we know it's Interix rather than the generic POSIX subsystem?\n\t# It also conflicts with pre-2.0 versions of AT&T UWIN. Should we\n\t# UNAME_MACHINE based on the output of uname instead of i386?\n\techo i586-pc-interix\n\texit ;;\n    i*:UWIN*:*)\n\techo ${UNAME_MACHINE}-pc-uwin\n\texit ;;\n    amd64:CYGWIN*:*:* | x86_64:CYGWIN*:*:*)\n\techo x86_64-unknown-cygwin\n\texit ;;\n    p*:CYGWIN*:*)\n\techo powerpcle-unknown-cygwin\n\texit ;;\n    prep*:SunOS:5.*:*)\n\techo powerpcle-unknown-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`\n\texit ;;\n    *:GNU:*:*)\n\t# the GNU system\n\techo `echo ${UNAME_MACHINE}|sed -e 's,[-/].*$,,'`-unknown-${LIBC}`echo ${UNAME_RELEASE}|sed -e 's,/.*$,,'`\n\texit ;;\n    *:GNU/*:*:*)\n\t# other systems with GNU libc and userland\n\techo ${UNAME_MACHINE}-unknown-`echo ${UNAME_SYSTEM} | sed 's,^[^/]*/,,' | tr \"[:upper:]\" \"[:lower:]\"``echo ${UNAME_RELEASE}|sed -e 's/[-(].*//'`-${LIBC}\n\texit ;;\n    i*86:Minix:*:*)\n\techo ${UNAME_MACHINE}-pc-minix\n\texit ;;\n    aarch64:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    aarch64_be:Linux:*:*)\n\tUNAME_MACHINE=aarch64_be\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    alpha:Linux:*:*)\n\tcase `sed -n '/^cpu model/s/^.*: \\(.*\\)/\\1/p' < /proc/cpuinfo` in\n\t  EV5)   UNAME_MACHINE=alphaev5 ;;\n\t  EV56)  UNAME_MACHINE=alphaev56 ;;\n\t  PCA56) UNAME_MACHINE=alphapca56 ;;\n\t  PCA57) UNAME_MACHINE=alphapca56 ;;\n\t  EV6)   UNAME_MACHINE=alphaev6 ;;\n\t  EV67)  UNAME_MACHINE=alphaev67 ;;\n\t  EV68*) UNAME_MACHINE=alphaev68 ;;\n\tesac\n\tobjdump --private-headers /bin/sh | grep -q ld.so.1\n\tif test \"$?\" = 0 ; then LIBC=gnulibc1 ; fi\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    arc:Linux:*:* | arceb:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    arm*:Linux:*:*)\n\teval $set_cc_for_build\n\tif echo __ARM_EABI__ | $CC_FOR_BUILD -E - 2>/dev/null \\\n\t    | grep -q __ARM_EABI__\n\tthen\n\t    echo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\telse\n\t    if echo __ARM_PCS_VFP | $CC_FOR_BUILD -E - 2>/dev/null \\\n\t\t| grep -q __ARM_PCS_VFP\n\t    then\n\t\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}eabi\n\t    else\n\t\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}eabihf\n\t    fi\n\tfi\n\texit ;;\n    avr32*:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    cris:Linux:*:*)\n\techo ${UNAME_MACHINE}-axis-linux-${LIBC}\n\texit ;;\n    crisv32:Linux:*:*)\n\techo ${UNAME_MACHINE}-axis-linux-${LIBC}\n\texit ;;\n    e2k:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    frv:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    hexagon:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    i*86:Linux:*:*)\n\techo ${UNAME_MACHINE}-pc-linux-${LIBC}\n\texit ;;\n    ia64:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    k1om:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    m32r*:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    m68*:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    mips:Linux:*:* | mips64:Linux:*:*)\n\teval $set_cc_for_build\n\tsed 's/^\t//' << EOF >$dummy.c\n\t#undef CPU\n\t#undef ${UNAME_MACHINE}\n\t#undef ${UNAME_MACHINE}el\n\t#if defined(__MIPSEL__) || defined(__MIPSEL) || defined(_MIPSEL) || defined(MIPSEL)\n\tCPU=${UNAME_MACHINE}el\n\t#else\n\t#if defined(__MIPSEB__) || defined(__MIPSEB) || defined(_MIPSEB) || defined(MIPSEB)\n\tCPU=${UNAME_MACHINE}\n\t#else\n\tCPU=\n\t#endif\n\t#endif\nEOF\n\teval `$CC_FOR_BUILD -E $dummy.c 2>/dev/null | grep '^CPU'`\n\ttest x\"${CPU}\" != x && { echo \"${CPU}-unknown-linux-${LIBC}\"; exit; }\n\t;;\n    mips64el:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    openrisc*:Linux:*:*)\n\techo or1k-unknown-linux-${LIBC}\n\texit ;;\n    or32:Linux:*:* | or1k*:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    padre:Linux:*:*)\n\techo sparc-unknown-linux-${LIBC}\n\texit ;;\n    parisc64:Linux:*:* | hppa64:Linux:*:*)\n\techo hppa64-unknown-linux-${LIBC}\n\texit ;;\n    parisc:Linux:*:* | hppa:Linux:*:*)\n\t# Look for CPU level\n\tcase `grep '^cpu[^a-z]*:' /proc/cpuinfo 2>/dev/null | cut -d' ' -f2` in\n\t  PA7*) echo hppa1.1-unknown-linux-${LIBC} ;;\n\t  PA8*) echo hppa2.0-unknown-linux-${LIBC} ;;\n\t  *)    echo hppa-unknown-linux-${LIBC} ;;\n\tesac\n\texit ;;\n    ppc64:Linux:*:*)\n\techo powerpc64-unknown-linux-${LIBC}\n\texit ;;\n    ppc:Linux:*:*)\n\techo powerpc-unknown-linux-${LIBC}\n\texit ;;\n    ppc64le:Linux:*:*)\n\techo powerpc64le-unknown-linux-${LIBC}\n\texit ;;\n    ppcle:Linux:*:*)\n\techo powerpcle-unknown-linux-${LIBC}\n\texit ;;\n    riscv32:Linux:*:* | riscv64:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    s390:Linux:*:* | s390x:Linux:*:*)\n\techo ${UNAME_MACHINE}-ibm-linux-${LIBC}\n\texit ;;\n    sh64*:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    sh*:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    sparc:Linux:*:* | sparc64:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    tile*:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    vax:Linux:*:*)\n\techo ${UNAME_MACHINE}-dec-linux-${LIBC}\n\texit ;;\n    x86_64:Linux:*:*)\n\techo ${UNAME_MACHINE}-pc-linux-${LIBC}\n\texit ;;\n    xtensa*:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    i*86:DYNIX/ptx:4*:*)\n\t# ptx 4.0 does uname -s correctly, with DYNIX/ptx in there.\n\t# earlier versions are messed up and put the nodename in both\n\t# sysname and nodename.\n\techo i386-sequent-sysv4\n\texit ;;\n    i*86:UNIX_SV:4.2MP:2.*)\n\t# Unixware is an offshoot of SVR4, but it has its own version\n\t# number series starting with 2...\n\t# I am not positive that other SVR4 systems won't match this,\n\t# I just have to hope.  -- rms.\n\t# Use sysv4.2uw... so that sysv4* matches it.\n\techo ${UNAME_MACHINE}-pc-sysv4.2uw${UNAME_VERSION}\n\texit ;;\n    i*86:OS/2:*:*)\n\t# If we were able to find `uname', then EMX Unix compatibility\n\t# is probably installed.\n\techo ${UNAME_MACHINE}-pc-os2-emx\n\texit ;;\n    i*86:XTS-300:*:STOP)\n\techo ${UNAME_MACHINE}-unknown-stop\n\texit ;;\n    i*86:atheos:*:*)\n\techo ${UNAME_MACHINE}-unknown-atheos\n\texit ;;\n    i*86:syllable:*:*)\n\techo ${UNAME_MACHINE}-pc-syllable\n\texit ;;\n    i*86:LynxOS:2.*:* | i*86:LynxOS:3.[01]*:* | i*86:LynxOS:4.[02]*:*)\n\techo i386-unknown-lynxos${UNAME_RELEASE}\n\texit ;;\n    i*86:*DOS:*:*)\n\techo ${UNAME_MACHINE}-pc-msdosdjgpp\n\texit ;;\n    i*86:*:4.*:* | i*86:SYSTEM_V:4.*:*)\n\tUNAME_REL=`echo ${UNAME_RELEASE} | sed 's/\\/MP$//'`\n\tif grep Novell /usr/include/link.h >/dev/null 2>/dev/null; then\n\t\techo ${UNAME_MACHINE}-univel-sysv${UNAME_REL}\n\telse\n\t\techo ${UNAME_MACHINE}-pc-sysv${UNAME_REL}\n\tfi\n\texit ;;\n    i*86:*:5:[678]*)\n\t# UnixWare 7.x, OpenUNIX and OpenServer 6.\n\tcase `/bin/uname -X | grep \"^Machine\"` in\n\t    *486*)\t     UNAME_MACHINE=i486 ;;\n\t    *Pentium)\t     UNAME_MACHINE=i586 ;;\n\t    *Pent*|*Celeron) UNAME_MACHINE=i686 ;;\n\tesac\n\techo ${UNAME_MACHINE}-unknown-sysv${UNAME_RELEASE}${UNAME_SYSTEM}${UNAME_VERSION}\n\texit ;;\n    i*86:*:3.2:*)\n\tif test -f /usr/options/cb.name; then\n\t\tUNAME_REL=`sed -n 's/.*Version //p' </usr/options/cb.name`\n\t\techo ${UNAME_MACHINE}-pc-isc$UNAME_REL\n\telif /bin/uname -X 2>/dev/null >/dev/null ; then\n\t\tUNAME_REL=`(/bin/uname -X|grep Release|sed -e 's/.*= //')`\n\t\t(/bin/uname -X|grep i80486 >/dev/null) && UNAME_MACHINE=i486\n\t\t(/bin/uname -X|grep '^Machine.*Pentium' >/dev/null) \\\n\t\t\t&& UNAME_MACHINE=i586\n\t\t(/bin/uname -X|grep '^Machine.*Pent *II' >/dev/null) \\\n\t\t\t&& UNAME_MACHINE=i686\n\t\t(/bin/uname -X|grep '^Machine.*Pentium Pro' >/dev/null) \\\n\t\t\t&& UNAME_MACHINE=i686\n\t\techo ${UNAME_MACHINE}-pc-sco$UNAME_REL\n\telse\n\t\techo ${UNAME_MACHINE}-pc-sysv32\n\tfi\n\texit ;;\n    pc:*:*:*)\n\t# Left here for compatibility:\n\t# uname -m prints for DJGPP always 'pc', but it prints nothing about\n\t# the processor, so we play safe by assuming i586.\n\t# Note: whatever this is, it MUST be the same as what config.sub\n\t# prints for the \"djgpp\" host, or else GDB configure will decide that\n\t# this is a cross-build.\n\techo i586-pc-msdosdjgpp\n\texit ;;\n    Intel:Mach:3*:*)\n\techo i386-pc-mach3\n\texit ;;\n    paragon:*:*:*)\n\techo i860-intel-osf1\n\texit ;;\n    i860:*:4.*:*) # i860-SVR4\n\tif grep Stardent /usr/include/sys/uadmin.h >/dev/null 2>&1 ; then\n\t  echo i860-stardent-sysv${UNAME_RELEASE} # Stardent Vistra i860-SVR4\n\telse # Add other i860-SVR4 vendors below as they are discovered.\n\t  echo i860-unknown-sysv${UNAME_RELEASE}  # Unknown i860-SVR4\n\tfi\n\texit ;;\n    mini*:CTIX:SYS*5:*)\n\t# \"miniframe\"\n\techo m68010-convergent-sysv\n\texit ;;\n    mc68k:UNIX:SYSTEM5:3.51m)\n\techo m68k-convergent-sysv\n\texit ;;\n    M680?0:D-NIX:5.3:*)\n\techo m68k-diab-dnix\n\texit ;;\n    M68*:*:R3V[5678]*:*)\n\ttest -r /sysV68 && { echo 'm68k-motorola-sysv'; exit; } ;;\n    3[345]??:*:4.0:3.0 | 3[34]??A:*:4.0:3.0 | 3[34]??,*:*:4.0:3.0 | 3[34]??/*:*:4.0:3.0 | 4400:*:4.0:3.0 | 4850:*:4.0:3.0 | SKA40:*:4.0:3.0 | SDS2:*:4.0:3.0 | SHG2:*:4.0:3.0 | S7501*:*:4.0:3.0)\n\tOS_REL=''\n\ttest -r /etc/.relid \\\n\t&& OS_REL=.`sed -n 's/[^ ]* [^ ]* \\([0-9][0-9]\\).*/\\1/p' < /etc/.relid`\n\t/bin/uname -p 2>/dev/null | grep 86 >/dev/null \\\n\t  && { echo i486-ncr-sysv4.3${OS_REL}; exit; }\n\t/bin/uname -p 2>/dev/null | /bin/grep entium >/dev/null \\\n\t  && { echo i586-ncr-sysv4.3${OS_REL}; exit; } ;;\n    3[34]??:*:4.0:* | 3[34]??,*:*:4.0:*)\n\t/bin/uname -p 2>/dev/null | grep 86 >/dev/null \\\n\t  && { echo i486-ncr-sysv4; exit; } ;;\n    NCR*:*:4.2:* | MPRAS*:*:4.2:*)\n\tOS_REL='.3'\n\ttest -r /etc/.relid \\\n\t    && OS_REL=.`sed -n 's/[^ ]* [^ ]* \\([0-9][0-9]\\).*/\\1/p' < /etc/.relid`\n\t/bin/uname -p 2>/dev/null | grep 86 >/dev/null \\\n\t    && { echo i486-ncr-sysv4.3${OS_REL}; exit; }\n\t/bin/uname -p 2>/dev/null | /bin/grep entium >/dev/null \\\n\t    && { echo i586-ncr-sysv4.3${OS_REL}; exit; }\n\t/bin/uname -p 2>/dev/null | /bin/grep pteron >/dev/null \\\n\t    && { echo i586-ncr-sysv4.3${OS_REL}; exit; } ;;\n    m68*:LynxOS:2.*:* | m68*:LynxOS:3.0*:*)\n\techo m68k-unknown-lynxos${UNAME_RELEASE}\n\texit ;;\n    mc68030:UNIX_System_V:4.*:*)\n\techo m68k-atari-sysv4\n\texit ;;\n    TSUNAMI:LynxOS:2.*:*)\n\techo sparc-unknown-lynxos${UNAME_RELEASE}\n\texit ;;\n    rs6000:LynxOS:2.*:*)\n\techo rs6000-unknown-lynxos${UNAME_RELEASE}\n\texit ;;\n    PowerPC:LynxOS:2.*:* | PowerPC:LynxOS:3.[01]*:* | PowerPC:LynxOS:4.[02]*:*)\n\techo powerpc-unknown-lynxos${UNAME_RELEASE}\n\texit ;;\n    SM[BE]S:UNIX_SV:*:*)\n\techo mips-dde-sysv${UNAME_RELEASE}\n\texit ;;\n    RM*:ReliantUNIX-*:*:*)\n\techo mips-sni-sysv4\n\texit ;;\n    RM*:SINIX-*:*:*)\n\techo mips-sni-sysv4\n\texit ;;\n    *:SINIX-*:*:*)\n\tif uname -p 2>/dev/null >/dev/null ; then\n\t\tUNAME_MACHINE=`(uname -p) 2>/dev/null`\n\t\techo ${UNAME_MACHINE}-sni-sysv4\n\telse\n\t\techo ns32k-sni-sysv\n\tfi\n\texit ;;\n    PENTIUM:*:4.0*:*)\t# Unisys `ClearPath HMP IX 4000' SVR4/MP effort\n\t\t\t# says <Richard.M.Bartel@ccMail.Census.GOV>\n\techo i586-unisys-sysv4\n\texit ;;\n    *:UNIX_System_V:4*:FTX*)\n\t# From Gerald Hewes <hewes@openmarket.com>.\n\t# How about differentiating between stratus architectures? -djm\n\techo hppa1.1-stratus-sysv4\n\texit ;;\n    *:*:*:FTX*)\n\t# From seanf@swdc.stratus.com.\n\techo i860-stratus-sysv4\n\texit ;;\n    i*86:VOS:*:*)\n\t# From Paul.Green@stratus.com.\n\techo ${UNAME_MACHINE}-stratus-vos\n\texit ;;\n    *:VOS:*:*)\n\t# From Paul.Green@stratus.com.\n\techo hppa1.1-stratus-vos\n\texit ;;\n    mc68*:A/UX:*:*)\n\techo m68k-apple-aux${UNAME_RELEASE}\n\texit ;;\n    news*:NEWS-OS:6*:*)\n\techo mips-sony-newsos6\n\texit ;;\n    R[34]000:*System_V*:*:* | R4000:UNIX_SYSV:*:* | R*000:UNIX_SV:*:*)\n\tif [ -d /usr/nec ]; then\n\t\techo mips-nec-sysv${UNAME_RELEASE}\n\telse\n\t\techo mips-unknown-sysv${UNAME_RELEASE}\n\tfi\n\texit ;;\n    BeBox:BeOS:*:*)\t# BeOS running on hardware made by Be, PPC only.\n\techo powerpc-be-beos\n\texit ;;\n    BeMac:BeOS:*:*)\t# BeOS running on Mac or Mac clone, PPC only.\n\techo powerpc-apple-beos\n\texit ;;\n    BePC:BeOS:*:*)\t# BeOS running on Intel PC compatible.\n\techo i586-pc-beos\n\texit ;;\n    BePC:Haiku:*:*)\t# Haiku running on Intel PC compatible.\n\techo i586-pc-haiku\n\texit ;;\n    x86_64:Haiku:*:*)\n\techo x86_64-unknown-haiku\n\texit ;;\n    SX-4:SUPER-UX:*:*)\n\techo sx4-nec-superux${UNAME_RELEASE}\n\texit ;;\n    SX-5:SUPER-UX:*:*)\n\techo sx5-nec-superux${UNAME_RELEASE}\n\texit ;;\n    SX-6:SUPER-UX:*:*)\n\techo sx6-nec-superux${UNAME_RELEASE}\n\texit ;;\n    SX-7:SUPER-UX:*:*)\n\techo sx7-nec-superux${UNAME_RELEASE}\n\texit ;;\n    SX-8:SUPER-UX:*:*)\n\techo sx8-nec-superux${UNAME_RELEASE}\n\texit ;;\n    SX-8R:SUPER-UX:*:*)\n\techo sx8r-nec-superux${UNAME_RELEASE}\n\texit ;;\n    SX-ACE:SUPER-UX:*:*)\n\techo sxace-nec-superux${UNAME_RELEASE}\n\texit ;;\n    Power*:Rhapsody:*:*)\n\techo powerpc-apple-rhapsody${UNAME_RELEASE}\n\texit ;;\n    *:Rhapsody:*:*)\n\techo ${UNAME_MACHINE}-apple-rhapsody${UNAME_RELEASE}\n\texit ;;\n    *:Darwin:*:*)\n\tUNAME_PROCESSOR=`uname -p` || UNAME_PROCESSOR=unknown\n\teval $set_cc_for_build\n\tif test \"$UNAME_PROCESSOR\" = unknown ; then\n\t    UNAME_PROCESSOR=powerpc\n\tfi\n\tif test `echo \"$UNAME_RELEASE\" | sed -e 's/\\..*//'` -le 10 ; then\n\t    if [ \"$CC_FOR_BUILD\" != no_compiler_found ]; then\n\t\tif (echo '#ifdef __LP64__'; echo IS_64BIT_ARCH; echo '#endif') | \\\n\t\t    (CCOPTS=\"\" $CC_FOR_BUILD -E - 2>/dev/null) | \\\n\t\t    grep IS_64BIT_ARCH >/dev/null\n\t\tthen\n\t\t    case $UNAME_PROCESSOR in\n\t\t\ti386) UNAME_PROCESSOR=x86_64 ;;\n\t\t\tpowerpc) UNAME_PROCESSOR=powerpc64 ;;\n\t\t    esac\n\t\tfi\n\t    fi\n\telif test \"$UNAME_PROCESSOR\" = i386 ; then\n\t    # Avoid executing cc on OS X 10.9, as it ships with a stub\n\t    # that puts up a graphical alert prompting to install\n\t    # developer tools.  Any system running Mac OS X 10.7 or\n\t    # later (Darwin 11 and later) is required to have a 64-bit\n\t    # processor. This is not true of the ARM version of Darwin\n\t    # that Apple uses in portable devices.\n\t    UNAME_PROCESSOR=x86_64\n\tfi\n\techo ${UNAME_PROCESSOR}-apple-darwin${UNAME_RELEASE}\n\texit ;;\n    *:procnto*:*:* | *:QNX:[0123456789]*:*)\n\tUNAME_PROCESSOR=`uname -p`\n\tif test \"$UNAME_PROCESSOR\" = x86; then\n\t\tUNAME_PROCESSOR=i386\n\t\tUNAME_MACHINE=pc\n\tfi\n\techo ${UNAME_PROCESSOR}-${UNAME_MACHINE}-nto-qnx${UNAME_RELEASE}\n\texit ;;\n    *:QNX:*:4*)\n\techo i386-pc-qnx\n\texit ;;\n    NEO-?:NONSTOP_KERNEL:*:*)\n\techo neo-tandem-nsk${UNAME_RELEASE}\n\texit ;;\n    NSE-*:NONSTOP_KERNEL:*:*)\n\techo nse-tandem-nsk${UNAME_RELEASE}\n\texit ;;\n    NSR-?:NONSTOP_KERNEL:*:*)\n\techo nsr-tandem-nsk${UNAME_RELEASE}\n\texit ;;\n    *:NonStop-UX:*:*)\n\techo mips-compaq-nonstopux\n\texit ;;\n    BS2000:POSIX*:*:*)\n\techo bs2000-siemens-sysv\n\texit ;;\n    DS/*:UNIX_System_V:*:*)\n\techo ${UNAME_MACHINE}-${UNAME_SYSTEM}-${UNAME_RELEASE}\n\texit ;;\n    *:Plan9:*:*)\n\t# \"uname -m\" is not consistent, so use $cputype instead. 386\n\t# is converted to i386 for consistency with other x86\n\t# operating systems.\n\tif test \"$cputype\" = 386; then\n\t    UNAME_MACHINE=i386\n\telse\n\t    UNAME_MACHINE=\"$cputype\"\n\tfi\n\techo ${UNAME_MACHINE}-unknown-plan9\n\texit ;;\n    *:TOPS-10:*:*)\n\techo pdp10-unknown-tops10\n\texit ;;\n    *:TENEX:*:*)\n\techo pdp10-unknown-tenex\n\texit ;;\n    KS10:TOPS-20:*:* | KL10:TOPS-20:*:* | TYPE4:TOPS-20:*:*)\n\techo pdp10-dec-tops20\n\texit ;;\n    XKL-1:TOPS-20:*:* | TYPE5:TOPS-20:*:*)\n\techo pdp10-xkl-tops20\n\texit ;;\n    *:TOPS-20:*:*)\n\techo pdp10-unknown-tops20\n\texit ;;\n    *:ITS:*:*)\n\techo pdp10-unknown-its\n\texit ;;\n    SEI:*:*:SEIUX)\n\techo mips-sei-seiux${UNAME_RELEASE}\n\texit ;;\n    *:DragonFly:*:*)\n\techo ${UNAME_MACHINE}-unknown-dragonfly`echo ${UNAME_RELEASE}|sed -e 's/[-(].*//'`\n\texit ;;\n    *:*VMS:*:*)\n\tUNAME_MACHINE=`(uname -p) 2>/dev/null`\n\tcase \"${UNAME_MACHINE}\" in\n\t    A*) echo alpha-dec-vms ; exit ;;\n\t    I*) echo ia64-dec-vms ; exit ;;\n\t    V*) echo vax-dec-vms ; exit ;;\n\tesac ;;\n    *:XENIX:*:SysV)\n\techo i386-pc-xenix\n\texit ;;\n    i*86:skyos:*:*)\n\techo ${UNAME_MACHINE}-pc-skyos`echo ${UNAME_RELEASE} | sed -e 's/ .*$//'`\n\texit ;;\n    i*86:rdos:*:*)\n\techo ${UNAME_MACHINE}-pc-rdos\n\texit ;;\n    i*86:AROS:*:*)\n\techo ${UNAME_MACHINE}-pc-aros\n\texit ;;\n    x86_64:VMkernel:*:*)\n\techo ${UNAME_MACHINE}-unknown-esx\n\texit ;;\n    amd64:Isilon\\ OneFS:*:*)\n\techo x86_64-unknown-onefs\n\texit ;;\nesac\n\ncat >&2 <<EOF\n$0: unable to guess system type\n\nThis script (version $timestamp), has failed to recognize the\noperating system you are using. If your script is old, overwrite\nconfig.guess and config.sub with the latest versions from:\n\n  http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess\nand\n  http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub\n\nIf $0 has already been updated, send the following data and any\ninformation you think might be pertinent to config-patches@gnu.org to\nprovide the necessary information to handle your system.\n\nconfig.guess timestamp = $timestamp\n\nuname -m = `(uname -m) 2>/dev/null || echo unknown`\nuname -r = `(uname -r) 2>/dev/null || echo unknown`\nuname -s = `(uname -s) 2>/dev/null || echo unknown`\nuname -v = `(uname -v) 2>/dev/null || echo unknown`\n\n/usr/bin/uname -p = `(/usr/bin/uname -p) 2>/dev/null`\n/bin/uname -X     = `(/bin/uname -X) 2>/dev/null`\n\nhostinfo               = `(hostinfo) 2>/dev/null`\n/bin/universe          = `(/bin/universe) 2>/dev/null`\n/usr/bin/arch -k       = `(/usr/bin/arch -k) 2>/dev/null`\n/bin/arch              = `(/bin/arch) 2>/dev/null`\n/usr/bin/oslevel       = `(/usr/bin/oslevel) 2>/dev/null`\n/usr/convex/getsysinfo = `(/usr/convex/getsysinfo) 2>/dev/null`\n\nUNAME_MACHINE = ${UNAME_MACHINE}\nUNAME_RELEASE = ${UNAME_RELEASE}\nUNAME_SYSTEM  = ${UNAME_SYSTEM}\nUNAME_VERSION = ${UNAME_VERSION}\nEOF\n\nexit 1\n\n# Local variables:\n# eval: (add-hook 'write-file-hooks 'time-stamp)\n# time-stamp-start: \"timestamp='\"\n# time-stamp-format: \"%:y-%02m-%02d\"\n# time-stamp-end: \"'\"\n# End:\n"
  },
  {
    "path": "deps/jemalloc/build-aux/config.sub",
    "content": "#! /bin/sh\n# Configuration validation subroutine script.\n#   Copyright 1992-2016 Free Software Foundation, Inc.\n\ntimestamp='2016-11-04'\n\n# This file is free software; you can redistribute it and/or modify it\n# under the terms of the GNU General Public License as published by\n# the Free Software Foundation; either version 3 of the License, or\n# (at your option) any later version.\n#\n# This program is distributed in the hope that it will be useful, but\n# WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n# General Public License for more details.\n#\n# You should have received a copy of the GNU General Public License\n# along with this program; if not, see <http://www.gnu.org/licenses/>.\n#\n# As a special exception to the GNU General Public License, if you\n# distribute this file as part of a program that contains a\n# configuration script generated by Autoconf, you may include it under\n# the same distribution terms that you use for the rest of that\n# program.  This Exception is an additional permission under section 7\n# of the GNU General Public License, version 3 (\"GPLv3\").\n\n\n# Please send patches to <config-patches@gnu.org>.\n#\n# Configuration subroutine to validate and canonicalize a configuration type.\n# Supply the specified configuration type as an argument.\n# If it is invalid, we print an error message on stderr and exit with code 1.\n# Otherwise, we print the canonical config type on stdout and succeed.\n\n# You can get the latest version of this script from:\n# http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub\n\n# This file is supposed to be the same for all GNU packages\n# and recognize all the CPU types, system types and aliases\n# that are meaningful with *any* GNU software.\n# Each package is responsible for reporting which valid configurations\n# it does not support.  The user should be able to distinguish\n# a failure to support a valid configuration from a meaningless\n# configuration.\n\n# The goal of this file is to map all the various variations of a given\n# machine specification into a single specification in the form:\n#\tCPU_TYPE-MANUFACTURER-OPERATING_SYSTEM\n# or in some cases, the newer four-part form:\n#\tCPU_TYPE-MANUFACTURER-KERNEL-OPERATING_SYSTEM\n# It is wrong to echo any other type of specification.\n\nme=`echo \"$0\" | sed -e 's,.*/,,'`\n\nusage=\"\\\nUsage: $0 [OPTION] CPU-MFR-OPSYS or ALIAS\n\nCanonicalize a configuration name.\n\nOperation modes:\n  -h, --help         print this help, then exit\n  -t, --time-stamp   print date of last modification, then exit\n  -v, --version      print version number, then exit\n\nReport bugs and patches to <config-patches@gnu.org>.\"\n\nversion=\"\\\nGNU config.sub ($timestamp)\n\nCopyright 1992-2016 Free Software Foundation, Inc.\n\nThis is free software; see the source for copying conditions.  There is NO\nwarranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\"\n\nhelp=\"\nTry \\`$me --help' for more information.\"\n\n# Parse command line\nwhile test $# -gt 0 ; do\n  case $1 in\n    --time-stamp | --time* | -t )\n       echo \"$timestamp\" ; exit ;;\n    --version | -v )\n       echo \"$version\" ; exit ;;\n    --help | --h* | -h )\n       echo \"$usage\"; exit ;;\n    -- )     # Stop option processing\n       shift; break ;;\n    - )\t# Use stdin as input.\n       break ;;\n    -* )\n       echo \"$me: invalid option $1$help\"\n       exit 1 ;;\n\n    *local*)\n       # First pass through any local machine types.\n       echo $1\n       exit ;;\n\n    * )\n       break ;;\n  esac\ndone\n\ncase $# in\n 0) echo \"$me: missing argument$help\" >&2\n    exit 1;;\n 1) ;;\n *) echo \"$me: too many arguments$help\" >&2\n    exit 1;;\nesac\n\n# Separate what the user gave into CPU-COMPANY and OS or KERNEL-OS (if any).\n# Here we must recognize all the valid KERNEL-OS combinations.\nmaybe_os=`echo $1 | sed 's/^\\(.*\\)-\\([^-]*-[^-]*\\)$/\\2/'`\ncase $maybe_os in\n  nto-qnx* | linux-gnu* | linux-android* | linux-dietlibc | linux-newlib* | \\\n  linux-musl* | linux-uclibc* | uclinux-uclibc* | uclinux-gnu* | kfreebsd*-gnu* | \\\n  knetbsd*-gnu* | netbsd*-gnu* | netbsd*-eabi* | \\\n  kopensolaris*-gnu* | cloudabi*-eabi* | \\\n  storm-chaos* | os2-emx* | rtmk-nova*)\n    os=-$maybe_os\n    basic_machine=`echo $1 | sed 's/^\\(.*\\)-\\([^-]*-[^-]*\\)$/\\1/'`\n    ;;\n  android-linux)\n    os=-linux-android\n    basic_machine=`echo $1 | sed 's/^\\(.*\\)-\\([^-]*-[^-]*\\)$/\\1/'`-unknown\n    ;;\n  *)\n    basic_machine=`echo $1 | sed 's/-[^-]*$//'`\n    if [ $basic_machine != $1 ]\n    then os=`echo $1 | sed 's/.*-/-/'`\n    else os=; fi\n    ;;\nesac\n\n### Let's recognize common machines as not being operating systems so\n### that things like config.sub decstation-3100 work.  We also\n### recognize some manufacturers as not being operating systems, so we\n### can provide default operating systems below.\ncase $os in\n\t-sun*os*)\n\t\t# Prevent following clause from handling this invalid input.\n\t\t;;\n\t-dec* | -mips* | -sequent* | -encore* | -pc532* | -sgi* | -sony* | \\\n\t-att* | -7300* | -3300* | -delta* | -motorola* | -sun[234]* | \\\n\t-unicom* | -ibm* | -next | -hp | -isi* | -apollo | -altos* | \\\n\t-convergent* | -ncr* | -news | -32* | -3600* | -3100* | -hitachi* |\\\n\t-c[123]* | -convex* | -sun | -crds | -omron* | -dg | -ultra | -tti* | \\\n\t-harris | -dolphin | -highlevel | -gould | -cbm | -ns | -masscomp | \\\n\t-apple | -axis | -knuth | -cray | -microblaze*)\n\t\tos=\n\t\tbasic_machine=$1\n\t\t;;\n\t-bluegene*)\n\t\tos=-cnk\n\t\t;;\n\t-sim | -cisco | -oki | -wec | -winbond)\n\t\tos=\n\t\tbasic_machine=$1\n\t\t;;\n\t-scout)\n\t\t;;\n\t-wrs)\n\t\tos=-vxworks\n\t\tbasic_machine=$1\n\t\t;;\n\t-chorusos*)\n\t\tos=-chorusos\n\t\tbasic_machine=$1\n\t\t;;\n\t-chorusrdb)\n\t\tos=-chorusrdb\n\t\tbasic_machine=$1\n\t\t;;\n\t-hiux*)\n\t\tos=-hiuxwe2\n\t\t;;\n\t-sco6)\n\t\tos=-sco5v6\n\t\tbasic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`\n\t\t;;\n\t-sco5)\n\t\tos=-sco3.2v5\n\t\tbasic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`\n\t\t;;\n\t-sco4)\n\t\tos=-sco3.2v4\n\t\tbasic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`\n\t\t;;\n\t-sco3.2.[4-9]*)\n\t\tos=`echo $os | sed -e 's/sco3.2./sco3.2v/'`\n\t\tbasic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`\n\t\t;;\n\t-sco3.2v[4-9]*)\n\t\t# Don't forget version if it is 3.2v4 or newer.\n\t\tbasic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`\n\t\t;;\n\t-sco5v6*)\n\t\t# Don't forget version if it is 3.2v4 or newer.\n\t\tbasic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`\n\t\t;;\n\t-sco*)\n\t\tos=-sco3.2v2\n\t\tbasic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`\n\t\t;;\n\t-udk*)\n\t\tbasic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`\n\t\t;;\n\t-isc)\n\t\tos=-isc2.2\n\t\tbasic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`\n\t\t;;\n\t-clix*)\n\t\tbasic_machine=clipper-intergraph\n\t\t;;\n\t-isc*)\n\t\tbasic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`\n\t\t;;\n\t-lynx*178)\n\t\tos=-lynxos178\n\t\t;;\n\t-lynx*5)\n\t\tos=-lynxos5\n\t\t;;\n\t-lynx*)\n\t\tos=-lynxos\n\t\t;;\n\t-ptx*)\n\t\tbasic_machine=`echo $1 | sed -e 's/86-.*/86-sequent/'`\n\t\t;;\n\t-windowsnt*)\n\t\tos=`echo $os | sed -e 's/windowsnt/winnt/'`\n\t\t;;\n\t-psos*)\n\t\tos=-psos\n\t\t;;\n\t-mint | -mint[0-9]*)\n\t\tbasic_machine=m68k-atari\n\t\tos=-mint\n\t\t;;\nesac\n\n# Decode aliases for certain CPU-COMPANY combinations.\ncase $basic_machine in\n\t# Recognize the basic CPU types without company name.\n\t# Some are omitted here because they have special meanings below.\n\t1750a | 580 \\\n\t| a29k \\\n\t| aarch64 | aarch64_be \\\n\t| alpha | alphaev[4-8] | alphaev56 | alphaev6[78] | alphapca5[67] \\\n\t| alpha64 | alpha64ev[4-8] | alpha64ev56 | alpha64ev6[78] | alpha64pca5[67] \\\n\t| am33_2.0 \\\n\t| arc | arceb \\\n\t| arm | arm[bl]e | arme[lb] | armv[2-8] | armv[3-8][lb] | armv7[arm] \\\n\t| avr | avr32 \\\n\t| ba \\\n\t| be32 | be64 \\\n\t| bfin \\\n\t| c4x | c8051 | clipper \\\n\t| d10v | d30v | dlx | dsp16xx \\\n\t| e2k | epiphany \\\n\t| fido | fr30 | frv | ft32 \\\n\t| h8300 | h8500 | hppa | hppa1.[01] | hppa2.0 | hppa2.0[nw] | hppa64 \\\n\t| hexagon \\\n\t| i370 | i860 | i960 | ia64 \\\n\t| ip2k | iq2000 \\\n\t| k1om \\\n\t| le32 | le64 \\\n\t| lm32 \\\n\t| m32c | m32r | m32rle | m68000 | m68k | m88k \\\n\t| maxq | mb | microblaze | microblazeel | mcore | mep | metag \\\n\t| mips | mipsbe | mipseb | mipsel | mipsle \\\n\t| mips16 \\\n\t| mips64 | mips64el \\\n\t| mips64octeon | mips64octeonel \\\n\t| mips64orion | mips64orionel \\\n\t| mips64r5900 | mips64r5900el \\\n\t| mips64vr | mips64vrel \\\n\t| mips64vr4100 | mips64vr4100el \\\n\t| mips64vr4300 | mips64vr4300el \\\n\t| mips64vr5000 | mips64vr5000el \\\n\t| mips64vr5900 | mips64vr5900el \\\n\t| mipsisa32 | mipsisa32el \\\n\t| mipsisa32r2 | mipsisa32r2el \\\n\t| mipsisa32r6 | mipsisa32r6el \\\n\t| mipsisa64 | mipsisa64el \\\n\t| mipsisa64r2 | mipsisa64r2el \\\n\t| mipsisa64r6 | mipsisa64r6el \\\n\t| mipsisa64sb1 | mipsisa64sb1el \\\n\t| mipsisa64sr71k | mipsisa64sr71kel \\\n\t| mipsr5900 | mipsr5900el \\\n\t| mipstx39 | mipstx39el \\\n\t| mn10200 | mn10300 \\\n\t| moxie \\\n\t| mt \\\n\t| msp430 \\\n\t| nds32 | nds32le | nds32be \\\n\t| nios | nios2 | nios2eb | nios2el \\\n\t| ns16k | ns32k \\\n\t| open8 | or1k | or1knd | or32 \\\n\t| pdp10 | pdp11 | pj | pjl \\\n\t| powerpc | powerpc64 | powerpc64le | powerpcle \\\n\t| pru \\\n\t| pyramid \\\n\t| riscv32 | riscv64 \\\n\t| rl78 | rx \\\n\t| score \\\n\t| sh | sh[1234] | sh[24]a | sh[24]aeb | sh[23]e | sh[234]eb | sheb | shbe | shle | sh[1234]le | sh3ele \\\n\t| sh64 | sh64le \\\n\t| sparc | sparc64 | sparc64b | sparc64v | sparc86x | sparclet | sparclite \\\n\t| sparcv8 | sparcv9 | sparcv9b | sparcv9v \\\n\t| spu \\\n\t| tahoe | tic4x | tic54x | tic55x | tic6x | tic80 | tron \\\n\t| ubicom32 \\\n\t| v850 | v850e | v850e1 | v850e2 | v850es | v850e2v3 \\\n\t| visium \\\n\t| we32k \\\n\t| x86 | xc16x | xstormy16 | xtensa \\\n\t| z8k | z80)\n\t\tbasic_machine=$basic_machine-unknown\n\t\t;;\n\tc54x)\n\t\tbasic_machine=tic54x-unknown\n\t\t;;\n\tc55x)\n\t\tbasic_machine=tic55x-unknown\n\t\t;;\n\tc6x)\n\t\tbasic_machine=tic6x-unknown\n\t\t;;\n\tleon|leon[3-9])\n\t\tbasic_machine=sparc-$basic_machine\n\t\t;;\n\tm6811 | m68hc11 | m6812 | m68hc12 | m68hcs12x | nvptx | picochip)\n\t\tbasic_machine=$basic_machine-unknown\n\t\tos=-none\n\t\t;;\n\tm88110 | m680[12346]0 | m683?2 | m68360 | m5200 | v70 | w65 | z8k)\n\t\t;;\n\tms1)\n\t\tbasic_machine=mt-unknown\n\t\t;;\n\n\tstrongarm | thumb | xscale)\n\t\tbasic_machine=arm-unknown\n\t\t;;\n\txgate)\n\t\tbasic_machine=$basic_machine-unknown\n\t\tos=-none\n\t\t;;\n\txscaleeb)\n\t\tbasic_machine=armeb-unknown\n\t\t;;\n\n\txscaleel)\n\t\tbasic_machine=armel-unknown\n\t\t;;\n\n\t# We use `pc' rather than `unknown'\n\t# because (1) that's what they normally are, and\n\t# (2) the word \"unknown\" tends to confuse beginning users.\n\ti*86 | x86_64)\n\t  basic_machine=$basic_machine-pc\n\t  ;;\n\t# Object if more than one company name word.\n\t*-*-*)\n\t\techo Invalid configuration \\`$1\\': machine \\`$basic_machine\\' not recognized 1>&2\n\t\texit 1\n\t\t;;\n\t# Recognize the basic CPU types with company name.\n\t580-* \\\n\t| a29k-* \\\n\t| aarch64-* | aarch64_be-* \\\n\t| alpha-* | alphaev[4-8]-* | alphaev56-* | alphaev6[78]-* \\\n\t| alpha64-* | alpha64ev[4-8]-* | alpha64ev56-* | alpha64ev6[78]-* \\\n\t| alphapca5[67]-* | alpha64pca5[67]-* | arc-* | arceb-* \\\n\t| arm-*  | armbe-* | armle-* | armeb-* | armv*-* \\\n\t| avr-* | avr32-* \\\n\t| ba-* \\\n\t| be32-* | be64-* \\\n\t| bfin-* | bs2000-* \\\n\t| c[123]* | c30-* | [cjt]90-* | c4x-* \\\n\t| c8051-* | clipper-* | craynv-* | cydra-* \\\n\t| d10v-* | d30v-* | dlx-* \\\n\t| e2k-* | elxsi-* \\\n\t| f30[01]-* | f700-* | fido-* | fr30-* | frv-* | fx80-* \\\n\t| h8300-* | h8500-* \\\n\t| hppa-* | hppa1.[01]-* | hppa2.0-* | hppa2.0[nw]-* | hppa64-* \\\n\t| hexagon-* \\\n\t| i*86-* | i860-* | i960-* | ia64-* \\\n\t| ip2k-* | iq2000-* \\\n\t| k1om-* \\\n\t| le32-* | le64-* \\\n\t| lm32-* \\\n\t| m32c-* | m32r-* | m32rle-* \\\n\t| m68000-* | m680[012346]0-* | m68360-* | m683?2-* | m68k-* \\\n\t| m88110-* | m88k-* | maxq-* | mcore-* | metag-* \\\n\t| microblaze-* | microblazeel-* \\\n\t| mips-* | mipsbe-* | mipseb-* | mipsel-* | mipsle-* \\\n\t| mips16-* \\\n\t| mips64-* | mips64el-* \\\n\t| mips64octeon-* | mips64octeonel-* \\\n\t| mips64orion-* | mips64orionel-* \\\n\t| mips64r5900-* | mips64r5900el-* \\\n\t| mips64vr-* | mips64vrel-* \\\n\t| mips64vr4100-* | mips64vr4100el-* \\\n\t| mips64vr4300-* | mips64vr4300el-* \\\n\t| mips64vr5000-* | mips64vr5000el-* \\\n\t| mips64vr5900-* | mips64vr5900el-* \\\n\t| mipsisa32-* | mipsisa32el-* \\\n\t| mipsisa32r2-* | mipsisa32r2el-* \\\n\t| mipsisa32r6-* | mipsisa32r6el-* \\\n\t| mipsisa64-* | mipsisa64el-* \\\n\t| mipsisa64r2-* | mipsisa64r2el-* \\\n\t| mipsisa64r6-* | mipsisa64r6el-* \\\n\t| mipsisa64sb1-* | mipsisa64sb1el-* \\\n\t| mipsisa64sr71k-* | mipsisa64sr71kel-* \\\n\t| mipsr5900-* | mipsr5900el-* \\\n\t| mipstx39-* | mipstx39el-* \\\n\t| mmix-* \\\n\t| mt-* \\\n\t| msp430-* \\\n\t| nds32-* | nds32le-* | nds32be-* \\\n\t| nios-* | nios2-* | nios2eb-* | nios2el-* \\\n\t| none-* | np1-* | ns16k-* | ns32k-* \\\n\t| open8-* \\\n\t| or1k*-* \\\n\t| orion-* \\\n\t| pdp10-* | pdp11-* | pj-* | pjl-* | pn-* | power-* \\\n\t| powerpc-* | powerpc64-* | powerpc64le-* | powerpcle-* \\\n\t| pru-* \\\n\t| pyramid-* \\\n\t| riscv32-* | riscv64-* \\\n\t| rl78-* | romp-* | rs6000-* | rx-* \\\n\t| sh-* | sh[1234]-* | sh[24]a-* | sh[24]aeb-* | sh[23]e-* | sh[34]eb-* | sheb-* | shbe-* \\\n\t| shle-* | sh[1234]le-* | sh3ele-* | sh64-* | sh64le-* \\\n\t| sparc-* | sparc64-* | sparc64b-* | sparc64v-* | sparc86x-* | sparclet-* \\\n\t| sparclite-* \\\n\t| sparcv8-* | sparcv9-* | sparcv9b-* | sparcv9v-* | sv1-* | sx*-* \\\n\t| tahoe-* \\\n\t| tic30-* | tic4x-* | tic54x-* | tic55x-* | tic6x-* | tic80-* \\\n\t| tile*-* \\\n\t| tron-* \\\n\t| ubicom32-* \\\n\t| v850-* | v850e-* | v850e1-* | v850es-* | v850e2-* | v850e2v3-* \\\n\t| vax-* \\\n\t| visium-* \\\n\t| we32k-* \\\n\t| x86-* | x86_64-* | xc16x-* | xps100-* \\\n\t| xstormy16-* | xtensa*-* \\\n\t| ymp-* \\\n\t| z8k-* | z80-*)\n\t\t;;\n\t# Recognize the basic CPU types without company name, with glob match.\n\txtensa*)\n\t\tbasic_machine=$basic_machine-unknown\n\t\t;;\n\t# Recognize the various machine names and aliases which stand\n\t# for a CPU type and a company and sometimes even an OS.\n\t386bsd)\n\t\tbasic_machine=i386-unknown\n\t\tos=-bsd\n\t\t;;\n\t3b1 | 7300 | 7300-att | att-7300 | pc7300 | safari | unixpc)\n\t\tbasic_machine=m68000-att\n\t\t;;\n\t3b*)\n\t\tbasic_machine=we32k-att\n\t\t;;\n\ta29khif)\n\t\tbasic_machine=a29k-amd\n\t\tos=-udi\n\t\t;;\n\tabacus)\n\t\tbasic_machine=abacus-unknown\n\t\t;;\n\tadobe68k)\n\t\tbasic_machine=m68010-adobe\n\t\tos=-scout\n\t\t;;\n\talliant | fx80)\n\t\tbasic_machine=fx80-alliant\n\t\t;;\n\taltos | altos3068)\n\t\tbasic_machine=m68k-altos\n\t\t;;\n\tam29k)\n\t\tbasic_machine=a29k-none\n\t\tos=-bsd\n\t\t;;\n\tamd64)\n\t\tbasic_machine=x86_64-pc\n\t\t;;\n\tamd64-*)\n\t\tbasic_machine=x86_64-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tamdahl)\n\t\tbasic_machine=580-amdahl\n\t\tos=-sysv\n\t\t;;\n\tamiga | amiga-*)\n\t\tbasic_machine=m68k-unknown\n\t\t;;\n\tamigaos | amigados)\n\t\tbasic_machine=m68k-unknown\n\t\tos=-amigaos\n\t\t;;\n\tamigaunix | amix)\n\t\tbasic_machine=m68k-unknown\n\t\tos=-sysv4\n\t\t;;\n\tapollo68)\n\t\tbasic_machine=m68k-apollo\n\t\tos=-sysv\n\t\t;;\n\tapollo68bsd)\n\t\tbasic_machine=m68k-apollo\n\t\tos=-bsd\n\t\t;;\n\taros)\n\t\tbasic_machine=i386-pc\n\t\tos=-aros\n\t\t;;\n\tasmjs)\n\t\tbasic_machine=asmjs-unknown\n\t\t;;\n\taux)\n\t\tbasic_machine=m68k-apple\n\t\tos=-aux\n\t\t;;\n\tbalance)\n\t\tbasic_machine=ns32k-sequent\n\t\tos=-dynix\n\t\t;;\n\tblackfin)\n\t\tbasic_machine=bfin-unknown\n\t\tos=-linux\n\t\t;;\n\tblackfin-*)\n\t\tbasic_machine=bfin-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\tos=-linux\n\t\t;;\n\tbluegene*)\n\t\tbasic_machine=powerpc-ibm\n\t\tos=-cnk\n\t\t;;\n\tc54x-*)\n\t\tbasic_machine=tic54x-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tc55x-*)\n\t\tbasic_machine=tic55x-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tc6x-*)\n\t\tbasic_machine=tic6x-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tc90)\n\t\tbasic_machine=c90-cray\n\t\tos=-unicos\n\t\t;;\n\tcegcc)\n\t\tbasic_machine=arm-unknown\n\t\tos=-cegcc\n\t\t;;\n\tconvex-c1)\n\t\tbasic_machine=c1-convex\n\t\tos=-bsd\n\t\t;;\n\tconvex-c2)\n\t\tbasic_machine=c2-convex\n\t\tos=-bsd\n\t\t;;\n\tconvex-c32)\n\t\tbasic_machine=c32-convex\n\t\tos=-bsd\n\t\t;;\n\tconvex-c34)\n\t\tbasic_machine=c34-convex\n\t\tos=-bsd\n\t\t;;\n\tconvex-c38)\n\t\tbasic_machine=c38-convex\n\t\tos=-bsd\n\t\t;;\n\tcray | j90)\n\t\tbasic_machine=j90-cray\n\t\tos=-unicos\n\t\t;;\n\tcraynv)\n\t\tbasic_machine=craynv-cray\n\t\tos=-unicosmp\n\t\t;;\n\tcr16 | cr16-*)\n\t\tbasic_machine=cr16-unknown\n\t\tos=-elf\n\t\t;;\n\tcrds | unos)\n\t\tbasic_machine=m68k-crds\n\t\t;;\n\tcrisv32 | crisv32-* | etraxfs*)\n\t\tbasic_machine=crisv32-axis\n\t\t;;\n\tcris | cris-* | etrax*)\n\t\tbasic_machine=cris-axis\n\t\t;;\n\tcrx)\n\t\tbasic_machine=crx-unknown\n\t\tos=-elf\n\t\t;;\n\tda30 | da30-*)\n\t\tbasic_machine=m68k-da30\n\t\t;;\n\tdecstation | decstation-3100 | pmax | pmax-* | pmin | dec3100 | decstatn)\n\t\tbasic_machine=mips-dec\n\t\t;;\n\tdecsystem10* | dec10*)\n\t\tbasic_machine=pdp10-dec\n\t\tos=-tops10\n\t\t;;\n\tdecsystem20* | dec20*)\n\t\tbasic_machine=pdp10-dec\n\t\tos=-tops20\n\t\t;;\n\tdelta | 3300 | motorola-3300 | motorola-delta \\\n\t      | 3300-motorola | delta-motorola)\n\t\tbasic_machine=m68k-motorola\n\t\t;;\n\tdelta88)\n\t\tbasic_machine=m88k-motorola\n\t\tos=-sysv3\n\t\t;;\n\tdicos)\n\t\tbasic_machine=i686-pc\n\t\tos=-dicos\n\t\t;;\n\tdjgpp)\n\t\tbasic_machine=i586-pc\n\t\tos=-msdosdjgpp\n\t\t;;\n\tdpx20 | dpx20-*)\n\t\tbasic_machine=rs6000-bull\n\t\tos=-bosx\n\t\t;;\n\tdpx2* | dpx2*-bull)\n\t\tbasic_machine=m68k-bull\n\t\tos=-sysv3\n\t\t;;\n\te500v[12])\n\t\tbasic_machine=powerpc-unknown\n\t\tos=$os\"spe\"\n\t\t;;\n\te500v[12]-*)\n\t\tbasic_machine=powerpc-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\tos=$os\"spe\"\n\t\t;;\n\tebmon29k)\n\t\tbasic_machine=a29k-amd\n\t\tos=-ebmon\n\t\t;;\n\telxsi)\n\t\tbasic_machine=elxsi-elxsi\n\t\tos=-bsd\n\t\t;;\n\tencore | umax | mmax)\n\t\tbasic_machine=ns32k-encore\n\t\t;;\n\tes1800 | OSE68k | ose68k | ose | OSE)\n\t\tbasic_machine=m68k-ericsson\n\t\tos=-ose\n\t\t;;\n\tfx2800)\n\t\tbasic_machine=i860-alliant\n\t\t;;\n\tgenix)\n\t\tbasic_machine=ns32k-ns\n\t\t;;\n\tgmicro)\n\t\tbasic_machine=tron-gmicro\n\t\tos=-sysv\n\t\t;;\n\tgo32)\n\t\tbasic_machine=i386-pc\n\t\tos=-go32\n\t\t;;\n\th3050r* | hiux*)\n\t\tbasic_machine=hppa1.1-hitachi\n\t\tos=-hiuxwe2\n\t\t;;\n\th8300hms)\n\t\tbasic_machine=h8300-hitachi\n\t\tos=-hms\n\t\t;;\n\th8300xray)\n\t\tbasic_machine=h8300-hitachi\n\t\tos=-xray\n\t\t;;\n\th8500hms)\n\t\tbasic_machine=h8500-hitachi\n\t\tos=-hms\n\t\t;;\n\tharris)\n\t\tbasic_machine=m88k-harris\n\t\tos=-sysv3\n\t\t;;\n\thp300-*)\n\t\tbasic_machine=m68k-hp\n\t\t;;\n\thp300bsd)\n\t\tbasic_machine=m68k-hp\n\t\tos=-bsd\n\t\t;;\n\thp300hpux)\n\t\tbasic_machine=m68k-hp\n\t\tos=-hpux\n\t\t;;\n\thp3k9[0-9][0-9] | hp9[0-9][0-9])\n\t\tbasic_machine=hppa1.0-hp\n\t\t;;\n\thp9k2[0-9][0-9] | hp9k31[0-9])\n\t\tbasic_machine=m68000-hp\n\t\t;;\n\thp9k3[2-9][0-9])\n\t\tbasic_machine=m68k-hp\n\t\t;;\n\thp9k6[0-9][0-9] | hp6[0-9][0-9])\n\t\tbasic_machine=hppa1.0-hp\n\t\t;;\n\thp9k7[0-79][0-9] | hp7[0-79][0-9])\n\t\tbasic_machine=hppa1.1-hp\n\t\t;;\n\thp9k78[0-9] | hp78[0-9])\n\t\t# FIXME: really hppa2.0-hp\n\t\tbasic_machine=hppa1.1-hp\n\t\t;;\n\thp9k8[67]1 | hp8[67]1 | hp9k80[24] | hp80[24] | hp9k8[78]9 | hp8[78]9 | hp9k893 | hp893)\n\t\t# FIXME: really hppa2.0-hp\n\t\tbasic_machine=hppa1.1-hp\n\t\t;;\n\thp9k8[0-9][13679] | hp8[0-9][13679])\n\t\tbasic_machine=hppa1.1-hp\n\t\t;;\n\thp9k8[0-9][0-9] | hp8[0-9][0-9])\n\t\tbasic_machine=hppa1.0-hp\n\t\t;;\n\thppa-next)\n\t\tos=-nextstep3\n\t\t;;\n\thppaosf)\n\t\tbasic_machine=hppa1.1-hp\n\t\tos=-osf\n\t\t;;\n\thppro)\n\t\tbasic_machine=hppa1.1-hp\n\t\tos=-proelf\n\t\t;;\n\ti370-ibm* | ibm*)\n\t\tbasic_machine=i370-ibm\n\t\t;;\n\ti*86v32)\n\t\tbasic_machine=`echo $1 | sed -e 's/86.*/86-pc/'`\n\t\tos=-sysv32\n\t\t;;\n\ti*86v4*)\n\t\tbasic_machine=`echo $1 | sed -e 's/86.*/86-pc/'`\n\t\tos=-sysv4\n\t\t;;\n\ti*86v)\n\t\tbasic_machine=`echo $1 | sed -e 's/86.*/86-pc/'`\n\t\tos=-sysv\n\t\t;;\n\ti*86sol2)\n\t\tbasic_machine=`echo $1 | sed -e 's/86.*/86-pc/'`\n\t\tos=-solaris2\n\t\t;;\n\ti386mach)\n\t\tbasic_machine=i386-mach\n\t\tos=-mach\n\t\t;;\n\ti386-vsta | vsta)\n\t\tbasic_machine=i386-unknown\n\t\tos=-vsta\n\t\t;;\n\tiris | iris4d)\n\t\tbasic_machine=mips-sgi\n\t\tcase $os in\n\t\t    -irix*)\n\t\t\t;;\n\t\t    *)\n\t\t\tos=-irix4\n\t\t\t;;\n\t\tesac\n\t\t;;\n\tisi68 | isi)\n\t\tbasic_machine=m68k-isi\n\t\tos=-sysv\n\t\t;;\n\tleon-*|leon[3-9]-*)\n\t\tbasic_machine=sparc-`echo $basic_machine | sed 's/-.*//'`\n\t\t;;\n\tm68knommu)\n\t\tbasic_machine=m68k-unknown\n\t\tos=-linux\n\t\t;;\n\tm68knommu-*)\n\t\tbasic_machine=m68k-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\tos=-linux\n\t\t;;\n\tm88k-omron*)\n\t\tbasic_machine=m88k-omron\n\t\t;;\n\tmagnum | m3230)\n\t\tbasic_machine=mips-mips\n\t\tos=-sysv\n\t\t;;\n\tmerlin)\n\t\tbasic_machine=ns32k-utek\n\t\tos=-sysv\n\t\t;;\n\tmicroblaze*)\n\t\tbasic_machine=microblaze-xilinx\n\t\t;;\n\tmingw64)\n\t\tbasic_machine=x86_64-pc\n\t\tos=-mingw64\n\t\t;;\n\tmingw32)\n\t\tbasic_machine=i686-pc\n\t\tos=-mingw32\n\t\t;;\n\tmingw32ce)\n\t\tbasic_machine=arm-unknown\n\t\tos=-mingw32ce\n\t\t;;\n\tminiframe)\n\t\tbasic_machine=m68000-convergent\n\t\t;;\n\t*mint | -mint[0-9]* | *MiNT | *MiNT[0-9]*)\n\t\tbasic_machine=m68k-atari\n\t\tos=-mint\n\t\t;;\n\tmips3*-*)\n\t\tbasic_machine=`echo $basic_machine | sed -e 's/mips3/mips64/'`\n\t\t;;\n\tmips3*)\n\t\tbasic_machine=`echo $basic_machine | sed -e 's/mips3/mips64/'`-unknown\n\t\t;;\n\tmonitor)\n\t\tbasic_machine=m68k-rom68k\n\t\tos=-coff\n\t\t;;\n\tmorphos)\n\t\tbasic_machine=powerpc-unknown\n\t\tos=-morphos\n\t\t;;\n\tmoxiebox)\n\t\tbasic_machine=moxie-unknown\n\t\tos=-moxiebox\n\t\t;;\n\tmsdos)\n\t\tbasic_machine=i386-pc\n\t\tos=-msdos\n\t\t;;\n\tms1-*)\n\t\tbasic_machine=`echo $basic_machine | sed -e 's/ms1-/mt-/'`\n\t\t;;\n\tmsys)\n\t\tbasic_machine=i686-pc\n\t\tos=-msys\n\t\t;;\n\tmvs)\n\t\tbasic_machine=i370-ibm\n\t\tos=-mvs\n\t\t;;\n\tnacl)\n\t\tbasic_machine=le32-unknown\n\t\tos=-nacl\n\t\t;;\n\tncr3000)\n\t\tbasic_machine=i486-ncr\n\t\tos=-sysv4\n\t\t;;\n\tnetbsd386)\n\t\tbasic_machine=i386-unknown\n\t\tos=-netbsd\n\t\t;;\n\tnetwinder)\n\t\tbasic_machine=armv4l-rebel\n\t\tos=-linux\n\t\t;;\n\tnews | news700 | news800 | news900)\n\t\tbasic_machine=m68k-sony\n\t\tos=-newsos\n\t\t;;\n\tnews1000)\n\t\tbasic_machine=m68030-sony\n\t\tos=-newsos\n\t\t;;\n\tnews-3600 | risc-news)\n\t\tbasic_machine=mips-sony\n\t\tos=-newsos\n\t\t;;\n\tnecv70)\n\t\tbasic_machine=v70-nec\n\t\tos=-sysv\n\t\t;;\n\tnext | m*-next )\n\t\tbasic_machine=m68k-next\n\t\tcase $os in\n\t\t    -nextstep* )\n\t\t\t;;\n\t\t    -ns2*)\n\t\t      os=-nextstep2\n\t\t\t;;\n\t\t    *)\n\t\t      os=-nextstep3\n\t\t\t;;\n\t\tesac\n\t\t;;\n\tnh3000)\n\t\tbasic_machine=m68k-harris\n\t\tos=-cxux\n\t\t;;\n\tnh[45]000)\n\t\tbasic_machine=m88k-harris\n\t\tos=-cxux\n\t\t;;\n\tnindy960)\n\t\tbasic_machine=i960-intel\n\t\tos=-nindy\n\t\t;;\n\tmon960)\n\t\tbasic_machine=i960-intel\n\t\tos=-mon960\n\t\t;;\n\tnonstopux)\n\t\tbasic_machine=mips-compaq\n\t\tos=-nonstopux\n\t\t;;\n\tnp1)\n\t\tbasic_machine=np1-gould\n\t\t;;\n\tneo-tandem)\n\t\tbasic_machine=neo-tandem\n\t\t;;\n\tnse-tandem)\n\t\tbasic_machine=nse-tandem\n\t\t;;\n\tnsr-tandem)\n\t\tbasic_machine=nsr-tandem\n\t\t;;\n\top50n-* | op60c-*)\n\t\tbasic_machine=hppa1.1-oki\n\t\tos=-proelf\n\t\t;;\n\topenrisc | openrisc-*)\n\t\tbasic_machine=or32-unknown\n\t\t;;\n\tos400)\n\t\tbasic_machine=powerpc-ibm\n\t\tos=-os400\n\t\t;;\n\tOSE68000 | ose68000)\n\t\tbasic_machine=m68000-ericsson\n\t\tos=-ose\n\t\t;;\n\tos68k)\n\t\tbasic_machine=m68k-none\n\t\tos=-os68k\n\t\t;;\n\tpa-hitachi)\n\t\tbasic_machine=hppa1.1-hitachi\n\t\tos=-hiuxwe2\n\t\t;;\n\tparagon)\n\t\tbasic_machine=i860-intel\n\t\tos=-osf\n\t\t;;\n\tparisc)\n\t\tbasic_machine=hppa-unknown\n\t\tos=-linux\n\t\t;;\n\tparisc-*)\n\t\tbasic_machine=hppa-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\tos=-linux\n\t\t;;\n\tpbd)\n\t\tbasic_machine=sparc-tti\n\t\t;;\n\tpbb)\n\t\tbasic_machine=m68k-tti\n\t\t;;\n\tpc532 | pc532-*)\n\t\tbasic_machine=ns32k-pc532\n\t\t;;\n\tpc98)\n\t\tbasic_machine=i386-pc\n\t\t;;\n\tpc98-*)\n\t\tbasic_machine=i386-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tpentium | p5 | k5 | k6 | nexgen | viac3)\n\t\tbasic_machine=i586-pc\n\t\t;;\n\tpentiumpro | p6 | 6x86 | athlon | athlon_*)\n\t\tbasic_machine=i686-pc\n\t\t;;\n\tpentiumii | pentium2 | pentiumiii | pentium3)\n\t\tbasic_machine=i686-pc\n\t\t;;\n\tpentium4)\n\t\tbasic_machine=i786-pc\n\t\t;;\n\tpentium-* | p5-* | k5-* | k6-* | nexgen-* | viac3-*)\n\t\tbasic_machine=i586-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tpentiumpro-* | p6-* | 6x86-* | athlon-*)\n\t\tbasic_machine=i686-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tpentiumii-* | pentium2-* | pentiumiii-* | pentium3-*)\n\t\tbasic_machine=i686-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tpentium4-*)\n\t\tbasic_machine=i786-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tpn)\n\t\tbasic_machine=pn-gould\n\t\t;;\n\tpower)\tbasic_machine=power-ibm\n\t\t;;\n\tppc | ppcbe)\tbasic_machine=powerpc-unknown\n\t\t;;\n\tppc-* | ppcbe-*)\n\t\tbasic_machine=powerpc-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tppcle | powerpclittle)\n\t\tbasic_machine=powerpcle-unknown\n\t\t;;\n\tppcle-* | powerpclittle-*)\n\t\tbasic_machine=powerpcle-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tppc64)\tbasic_machine=powerpc64-unknown\n\t\t;;\n\tppc64-*) basic_machine=powerpc64-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tppc64le | powerpc64little)\n\t\tbasic_machine=powerpc64le-unknown\n\t\t;;\n\tppc64le-* | powerpc64little-*)\n\t\tbasic_machine=powerpc64le-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tps2)\n\t\tbasic_machine=i386-ibm\n\t\t;;\n\tpw32)\n\t\tbasic_machine=i586-unknown\n\t\tos=-pw32\n\t\t;;\n\trdos | rdos64)\n\t\tbasic_machine=x86_64-pc\n\t\tos=-rdos\n\t\t;;\n\trdos32)\n\t\tbasic_machine=i386-pc\n\t\tos=-rdos\n\t\t;;\n\trom68k)\n\t\tbasic_machine=m68k-rom68k\n\t\tos=-coff\n\t\t;;\n\trm[46]00)\n\t\tbasic_machine=mips-siemens\n\t\t;;\n\trtpc | rtpc-*)\n\t\tbasic_machine=romp-ibm\n\t\t;;\n\ts390 | s390-*)\n\t\tbasic_machine=s390-ibm\n\t\t;;\n\ts390x | s390x-*)\n\t\tbasic_machine=s390x-ibm\n\t\t;;\n\tsa29200)\n\t\tbasic_machine=a29k-amd\n\t\tos=-udi\n\t\t;;\n\tsb1)\n\t\tbasic_machine=mipsisa64sb1-unknown\n\t\t;;\n\tsb1el)\n\t\tbasic_machine=mipsisa64sb1el-unknown\n\t\t;;\n\tsde)\n\t\tbasic_machine=mipsisa32-sde\n\t\tos=-elf\n\t\t;;\n\tsei)\n\t\tbasic_machine=mips-sei\n\t\tos=-seiux\n\t\t;;\n\tsequent)\n\t\tbasic_machine=i386-sequent\n\t\t;;\n\tsh)\n\t\tbasic_machine=sh-hitachi\n\t\tos=-hms\n\t\t;;\n\tsh5el)\n\t\tbasic_machine=sh5le-unknown\n\t\t;;\n\tsh64)\n\t\tbasic_machine=sh64-unknown\n\t\t;;\n\tsparclite-wrs | simso-wrs)\n\t\tbasic_machine=sparclite-wrs\n\t\tos=-vxworks\n\t\t;;\n\tsps7)\n\t\tbasic_machine=m68k-bull\n\t\tos=-sysv2\n\t\t;;\n\tspur)\n\t\tbasic_machine=spur-unknown\n\t\t;;\n\tst2000)\n\t\tbasic_machine=m68k-tandem\n\t\t;;\n\tstratus)\n\t\tbasic_machine=i860-stratus\n\t\tos=-sysv4\n\t\t;;\n\tstrongarm-* | thumb-*)\n\t\tbasic_machine=arm-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tsun2)\n\t\tbasic_machine=m68000-sun\n\t\t;;\n\tsun2os3)\n\t\tbasic_machine=m68000-sun\n\t\tos=-sunos3\n\t\t;;\n\tsun2os4)\n\t\tbasic_machine=m68000-sun\n\t\tos=-sunos4\n\t\t;;\n\tsun3os3)\n\t\tbasic_machine=m68k-sun\n\t\tos=-sunos3\n\t\t;;\n\tsun3os4)\n\t\tbasic_machine=m68k-sun\n\t\tos=-sunos4\n\t\t;;\n\tsun4os3)\n\t\tbasic_machine=sparc-sun\n\t\tos=-sunos3\n\t\t;;\n\tsun4os4)\n\t\tbasic_machine=sparc-sun\n\t\tos=-sunos4\n\t\t;;\n\tsun4sol2)\n\t\tbasic_machine=sparc-sun\n\t\tos=-solaris2\n\t\t;;\n\tsun3 | sun3-*)\n\t\tbasic_machine=m68k-sun\n\t\t;;\n\tsun4)\n\t\tbasic_machine=sparc-sun\n\t\t;;\n\tsun386 | sun386i | roadrunner)\n\t\tbasic_machine=i386-sun\n\t\t;;\n\tsv1)\n\t\tbasic_machine=sv1-cray\n\t\tos=-unicos\n\t\t;;\n\tsymmetry)\n\t\tbasic_machine=i386-sequent\n\t\tos=-dynix\n\t\t;;\n\tt3e)\n\t\tbasic_machine=alphaev5-cray\n\t\tos=-unicos\n\t\t;;\n\tt90)\n\t\tbasic_machine=t90-cray\n\t\tos=-unicos\n\t\t;;\n\ttile*)\n\t\tbasic_machine=$basic_machine-unknown\n\t\tos=-linux-gnu\n\t\t;;\n\ttx39)\n\t\tbasic_machine=mipstx39-unknown\n\t\t;;\n\ttx39el)\n\t\tbasic_machine=mipstx39el-unknown\n\t\t;;\n\ttoad1)\n\t\tbasic_machine=pdp10-xkl\n\t\tos=-tops20\n\t\t;;\n\ttower | tower-32)\n\t\tbasic_machine=m68k-ncr\n\t\t;;\n\ttpf)\n\t\tbasic_machine=s390x-ibm\n\t\tos=-tpf\n\t\t;;\n\tudi29k)\n\t\tbasic_machine=a29k-amd\n\t\tos=-udi\n\t\t;;\n\tultra3)\n\t\tbasic_machine=a29k-nyu\n\t\tos=-sym1\n\t\t;;\n\tv810 | necv810)\n\t\tbasic_machine=v810-nec\n\t\tos=-none\n\t\t;;\n\tvaxv)\n\t\tbasic_machine=vax-dec\n\t\tos=-sysv\n\t\t;;\n\tvms)\n\t\tbasic_machine=vax-dec\n\t\tos=-vms\n\t\t;;\n\tvpp*|vx|vx-*)\n\t\tbasic_machine=f301-fujitsu\n\t\t;;\n\tvxworks960)\n\t\tbasic_machine=i960-wrs\n\t\tos=-vxworks\n\t\t;;\n\tvxworks68)\n\t\tbasic_machine=m68k-wrs\n\t\tos=-vxworks\n\t\t;;\n\tvxworks29k)\n\t\tbasic_machine=a29k-wrs\n\t\tos=-vxworks\n\t\t;;\n\tw65*)\n\t\tbasic_machine=w65-wdc\n\t\tos=-none\n\t\t;;\n\tw89k-*)\n\t\tbasic_machine=hppa1.1-winbond\n\t\tos=-proelf\n\t\t;;\n\txbox)\n\t\tbasic_machine=i686-pc\n\t\tos=-mingw32\n\t\t;;\n\txps | xps100)\n\t\tbasic_machine=xps100-honeywell\n\t\t;;\n\txscale-* | xscalee[bl]-*)\n\t\tbasic_machine=`echo $basic_machine | sed 's/^xscale/arm/'`\n\t\t;;\n\tymp)\n\t\tbasic_machine=ymp-cray\n\t\tos=-unicos\n\t\t;;\n\tz8k-*-coff)\n\t\tbasic_machine=z8k-unknown\n\t\tos=-sim\n\t\t;;\n\tz80-*-coff)\n\t\tbasic_machine=z80-unknown\n\t\tos=-sim\n\t\t;;\n\tnone)\n\t\tbasic_machine=none-none\n\t\tos=-none\n\t\t;;\n\n# Here we handle the default manufacturer of certain CPU types.  It is in\n# some cases the only manufacturer, in others, it is the most popular.\n\tw89k)\n\t\tbasic_machine=hppa1.1-winbond\n\t\t;;\n\top50n)\n\t\tbasic_machine=hppa1.1-oki\n\t\t;;\n\top60c)\n\t\tbasic_machine=hppa1.1-oki\n\t\t;;\n\tromp)\n\t\tbasic_machine=romp-ibm\n\t\t;;\n\tmmix)\n\t\tbasic_machine=mmix-knuth\n\t\t;;\n\trs6000)\n\t\tbasic_machine=rs6000-ibm\n\t\t;;\n\tvax)\n\t\tbasic_machine=vax-dec\n\t\t;;\n\tpdp10)\n\t\t# there are many clones, so DEC is not a safe bet\n\t\tbasic_machine=pdp10-unknown\n\t\t;;\n\tpdp11)\n\t\tbasic_machine=pdp11-dec\n\t\t;;\n\twe32k)\n\t\tbasic_machine=we32k-att\n\t\t;;\n\tsh[1234] | sh[24]a | sh[24]aeb | sh[34]eb | sh[1234]le | sh[23]ele)\n\t\tbasic_machine=sh-unknown\n\t\t;;\n\tsparc | sparcv8 | sparcv9 | sparcv9b | sparcv9v)\n\t\tbasic_machine=sparc-sun\n\t\t;;\n\tcydra)\n\t\tbasic_machine=cydra-cydrome\n\t\t;;\n\torion)\n\t\tbasic_machine=orion-highlevel\n\t\t;;\n\torion105)\n\t\tbasic_machine=clipper-highlevel\n\t\t;;\n\tmac | mpw | mac-mpw)\n\t\tbasic_machine=m68k-apple\n\t\t;;\n\tpmac | pmac-mpw)\n\t\tbasic_machine=powerpc-apple\n\t\t;;\n\t*-unknown)\n\t\t# Make sure to match an already-canonicalized machine name.\n\t\t;;\n\t*)\n\t\techo Invalid configuration \\`$1\\': machine \\`$basic_machine\\' not recognized 1>&2\n\t\texit 1\n\t\t;;\nesac\n\n# Here we canonicalize certain aliases for manufacturers.\ncase $basic_machine in\n\t*-digital*)\n\t\tbasic_machine=`echo $basic_machine | sed 's/digital.*/dec/'`\n\t\t;;\n\t*-commodore*)\n\t\tbasic_machine=`echo $basic_machine | sed 's/commodore.*/cbm/'`\n\t\t;;\n\t*)\n\t\t;;\nesac\n\n# Decode manufacturer-specific aliases for certain operating systems.\n\nif [ x\"$os\" != x\"\" ]\nthen\ncase $os in\n\t# First match some system type aliases\n\t# that might get confused with valid system types.\n\t# -solaris* is a basic system type, with this one exception.\n\t-auroraux)\n\t\tos=-auroraux\n\t\t;;\n\t-solaris1 | -solaris1.*)\n\t\tos=`echo $os | sed -e 's|solaris1|sunos4|'`\n\t\t;;\n\t-solaris)\n\t\tos=-solaris2\n\t\t;;\n\t-svr4*)\n\t\tos=-sysv4\n\t\t;;\n\t-unixware*)\n\t\tos=-sysv4.2uw\n\t\t;;\n\t-gnu/linux*)\n\t\tos=`echo $os | sed -e 's|gnu/linux|linux-gnu|'`\n\t\t;;\n\t# First accept the basic system types.\n\t# The portable systems comes first.\n\t# Each alternative MUST END IN A *, to match a version number.\n\t# -sysv* is not here because it comes later, after sysvr4.\n\t-gnu* | -bsd* | -mach* | -minix* | -genix* | -ultrix* | -irix* \\\n\t      | -*vms* | -sco* | -esix* | -isc* | -aix* | -cnk* | -sunos | -sunos[34]*\\\n\t      | -hpux* | -unos* | -osf* | -luna* | -dgux* | -auroraux* | -solaris* \\\n\t      | -sym* | -kopensolaris* | -plan9* \\\n\t      | -amigaos* | -amigados* | -msdos* | -newsos* | -unicos* | -aof* \\\n\t      | -aos* | -aros* | -cloudabi* | -sortix* \\\n\t      | -nindy* | -vxsim* | -vxworks* | -ebmon* | -hms* | -mvs* \\\n\t      | -clix* | -riscos* | -uniplus* | -iris* | -rtu* | -xenix* \\\n\t      | -hiux* | -386bsd* | -knetbsd* | -mirbsd* | -netbsd* \\\n\t      | -bitrig* | -openbsd* | -solidbsd* | -libertybsd* \\\n\t      | -ekkobsd* | -kfreebsd* | -freebsd* | -riscix* | -lynxos* \\\n\t      | -bosx* | -nextstep* | -cxux* | -aout* | -elf* | -oabi* \\\n\t      | -ptx* | -coff* | -ecoff* | -winnt* | -domain* | -vsta* \\\n\t      | -udi* | -eabi* | -lites* | -ieee* | -go32* | -aux* \\\n\t      | -chorusos* | -chorusrdb* | -cegcc* \\\n\t      | -cygwin* | -msys* | -pe* | -psos* | -moss* | -proelf* | -rtems* \\\n\t      | -midipix* | -mingw32* | -mingw64* | -linux-gnu* | -linux-android* \\\n\t      | -linux-newlib* | -linux-musl* | -linux-uclibc* \\\n\t      | -uxpv* | -beos* | -mpeix* | -udk* | -moxiebox* \\\n\t      | -interix* | -uwin* | -mks* | -rhapsody* | -darwin* | -opened* \\\n\t      | -openstep* | -oskit* | -conix* | -pw32* | -nonstopux* \\\n\t      | -storm-chaos* | -tops10* | -tenex* | -tops20* | -its* \\\n\t      | -os2* | -vos* | -palmos* | -uclinux* | -nucleus* \\\n\t      | -morphos* | -superux* | -rtmk* | -rtmk-nova* | -windiss* \\\n\t      | -powermax* | -dnix* | -nx6 | -nx7 | -sei* | -dragonfly* \\\n\t      | -skyos* | -haiku* | -rdos* | -toppers* | -drops* | -es* \\\n\t      | -onefs* | -tirtos* | -phoenix* | -fuchsia*)\n\t# Remember, each alternative MUST END IN *, to match a version number.\n\t\t;;\n\t-qnx*)\n\t\tcase $basic_machine in\n\t\t    x86-* | i*86-*)\n\t\t\t;;\n\t\t    *)\n\t\t\tos=-nto$os\n\t\t\t;;\n\t\tesac\n\t\t;;\n\t-nto-qnx*)\n\t\t;;\n\t-nto*)\n\t\tos=`echo $os | sed -e 's|nto|nto-qnx|'`\n\t\t;;\n\t-sim | -es1800* | -hms* | -xray | -os68k* | -none* | -v88r* \\\n\t      | -windows* | -osx | -abug | -netware* | -os9* | -beos* | -haiku* \\\n\t      | -macos* | -mpw* | -magic* | -mmixware* | -mon960* | -lnews*)\n\t\t;;\n\t-mac*)\n\t\tos=`echo $os | sed -e 's|mac|macos|'`\n\t\t;;\n\t-linux-dietlibc)\n\t\tos=-linux-dietlibc\n\t\t;;\n\t-linux*)\n\t\tos=`echo $os | sed -e 's|linux|linux-gnu|'`\n\t\t;;\n\t-sunos5*)\n\t\tos=`echo $os | sed -e 's|sunos5|solaris2|'`\n\t\t;;\n\t-sunos6*)\n\t\tos=`echo $os | sed -e 's|sunos6|solaris3|'`\n\t\t;;\n\t-opened*)\n\t\tos=-openedition\n\t\t;;\n\t-os400*)\n\t\tos=-os400\n\t\t;;\n\t-wince*)\n\t\tos=-wince\n\t\t;;\n\t-osfrose*)\n\t\tos=-osfrose\n\t\t;;\n\t-osf*)\n\t\tos=-osf\n\t\t;;\n\t-utek*)\n\t\tos=-bsd\n\t\t;;\n\t-dynix*)\n\t\tos=-bsd\n\t\t;;\n\t-acis*)\n\t\tos=-aos\n\t\t;;\n\t-atheos*)\n\t\tos=-atheos\n\t\t;;\n\t-syllable*)\n\t\tos=-syllable\n\t\t;;\n\t-386bsd)\n\t\tos=-bsd\n\t\t;;\n\t-ctix* | -uts*)\n\t\tos=-sysv\n\t\t;;\n\t-nova*)\n\t\tos=-rtmk-nova\n\t\t;;\n\t-ns2 )\n\t\tos=-nextstep2\n\t\t;;\n\t-nsk*)\n\t\tos=-nsk\n\t\t;;\n\t# Preserve the version number of sinix5.\n\t-sinix5.*)\n\t\tos=`echo $os | sed -e 's|sinix|sysv|'`\n\t\t;;\n\t-sinix*)\n\t\tos=-sysv4\n\t\t;;\n\t-tpf*)\n\t\tos=-tpf\n\t\t;;\n\t-triton*)\n\t\tos=-sysv3\n\t\t;;\n\t-oss*)\n\t\tos=-sysv3\n\t\t;;\n\t-svr4)\n\t\tos=-sysv4\n\t\t;;\n\t-svr3)\n\t\tos=-sysv3\n\t\t;;\n\t-sysvr4)\n\t\tos=-sysv4\n\t\t;;\n\t# This must come after -sysvr4.\n\t-sysv*)\n\t\t;;\n\t-ose*)\n\t\tos=-ose\n\t\t;;\n\t-es1800*)\n\t\tos=-ose\n\t\t;;\n\t-xenix)\n\t\tos=-xenix\n\t\t;;\n\t-*mint | -mint[0-9]* | -*MiNT | -MiNT[0-9]*)\n\t\tos=-mint\n\t\t;;\n\t-aros*)\n\t\tos=-aros\n\t\t;;\n\t-zvmoe)\n\t\tos=-zvmoe\n\t\t;;\n\t-dicos*)\n\t\tos=-dicos\n\t\t;;\n\t-nacl*)\n\t\t;;\n\t-ios)\n\t\t;;\n\t-none)\n\t\t;;\n\t*)\n\t\t# Get rid of the `-' at the beginning of $os.\n\t\tos=`echo $os | sed 's/[^-]*-//'`\n\t\techo Invalid configuration \\`$1\\': system \\`$os\\' not recognized 1>&2\n\t\texit 1\n\t\t;;\nesac\nelse\n\n# Here we handle the default operating systems that come with various machines.\n# The value should be what the vendor currently ships out the door with their\n# machine or put another way, the most popular os provided with the machine.\n\n# Note that if you're going to try to match \"-MANUFACTURER\" here (say,\n# \"-sun\"), then you have to tell the case statement up towards the top\n# that MANUFACTURER isn't an operating system.  Otherwise, code above\n# will signal an error saying that MANUFACTURER isn't an operating\n# system, and we'll never get to this point.\n\ncase $basic_machine in\n\tscore-*)\n\t\tos=-elf\n\t\t;;\n\tspu-*)\n\t\tos=-elf\n\t\t;;\n\t*-acorn)\n\t\tos=-riscix1.2\n\t\t;;\n\tarm*-rebel)\n\t\tos=-linux\n\t\t;;\n\tarm*-semi)\n\t\tos=-aout\n\t\t;;\n\tc4x-* | tic4x-*)\n\t\tos=-coff\n\t\t;;\n\tc8051-*)\n\t\tos=-elf\n\t\t;;\n\thexagon-*)\n\t\tos=-elf\n\t\t;;\n\ttic54x-*)\n\t\tos=-coff\n\t\t;;\n\ttic55x-*)\n\t\tos=-coff\n\t\t;;\n\ttic6x-*)\n\t\tos=-coff\n\t\t;;\n\t# This must come before the *-dec entry.\n\tpdp10-*)\n\t\tos=-tops20\n\t\t;;\n\tpdp11-*)\n\t\tos=-none\n\t\t;;\n\t*-dec | vax-*)\n\t\tos=-ultrix4.2\n\t\t;;\n\tm68*-apollo)\n\t\tos=-domain\n\t\t;;\n\ti386-sun)\n\t\tos=-sunos4.0.2\n\t\t;;\n\tm68000-sun)\n\t\tos=-sunos3\n\t\t;;\n\tm68*-cisco)\n\t\tos=-aout\n\t\t;;\n\tmep-*)\n\t\tos=-elf\n\t\t;;\n\tmips*-cisco)\n\t\tos=-elf\n\t\t;;\n\tmips*-*)\n\t\tos=-elf\n\t\t;;\n\tor32-*)\n\t\tos=-coff\n\t\t;;\n\t*-tti)\t# must be before sparc entry or we get the wrong os.\n\t\tos=-sysv3\n\t\t;;\n\tsparc-* | *-sun)\n\t\tos=-sunos4.1.1\n\t\t;;\n\t*-be)\n\t\tos=-beos\n\t\t;;\n\t*-haiku)\n\t\tos=-haiku\n\t\t;;\n\t*-ibm)\n\t\tos=-aix\n\t\t;;\n\t*-knuth)\n\t\tos=-mmixware\n\t\t;;\n\t*-wec)\n\t\tos=-proelf\n\t\t;;\n\t*-winbond)\n\t\tos=-proelf\n\t\t;;\n\t*-oki)\n\t\tos=-proelf\n\t\t;;\n\t*-hp)\n\t\tos=-hpux\n\t\t;;\n\t*-hitachi)\n\t\tos=-hiux\n\t\t;;\n\ti860-* | *-att | *-ncr | *-altos | *-motorola | *-convergent)\n\t\tos=-sysv\n\t\t;;\n\t*-cbm)\n\t\tos=-amigaos\n\t\t;;\n\t*-dg)\n\t\tos=-dgux\n\t\t;;\n\t*-dolphin)\n\t\tos=-sysv3\n\t\t;;\n\tm68k-ccur)\n\t\tos=-rtu\n\t\t;;\n\tm88k-omron*)\n\t\tos=-luna\n\t\t;;\n\t*-next )\n\t\tos=-nextstep\n\t\t;;\n\t*-sequent)\n\t\tos=-ptx\n\t\t;;\n\t*-crds)\n\t\tos=-unos\n\t\t;;\n\t*-ns)\n\t\tos=-genix\n\t\t;;\n\ti370-*)\n\t\tos=-mvs\n\t\t;;\n\t*-next)\n\t\tos=-nextstep3\n\t\t;;\n\t*-gould)\n\t\tos=-sysv\n\t\t;;\n\t*-highlevel)\n\t\tos=-bsd\n\t\t;;\n\t*-encore)\n\t\tos=-bsd\n\t\t;;\n\t*-sgi)\n\t\tos=-irix\n\t\t;;\n\t*-siemens)\n\t\tos=-sysv4\n\t\t;;\n\t*-masscomp)\n\t\tos=-rtu\n\t\t;;\n\tf30[01]-fujitsu | f700-fujitsu)\n\t\tos=-uxpv\n\t\t;;\n\t*-rom68k)\n\t\tos=-coff\n\t\t;;\n\t*-*bug)\n\t\tos=-coff\n\t\t;;\n\t*-apple)\n\t\tos=-macos\n\t\t;;\n\t*-atari*)\n\t\tos=-mint\n\t\t;;\n\t*)\n\t\tos=-none\n\t\t;;\nesac\nfi\n\n# Here we handle the case where we know the os, and the CPU type, but not the\n# manufacturer.  We pick the logical manufacturer.\nvendor=unknown\ncase $basic_machine in\n\t*-unknown)\n\t\tcase $os in\n\t\t\t-riscix*)\n\t\t\t\tvendor=acorn\n\t\t\t\t;;\n\t\t\t-sunos*)\n\t\t\t\tvendor=sun\n\t\t\t\t;;\n\t\t\t-cnk*|-aix*)\n\t\t\t\tvendor=ibm\n\t\t\t\t;;\n\t\t\t-beos*)\n\t\t\t\tvendor=be\n\t\t\t\t;;\n\t\t\t-hpux*)\n\t\t\t\tvendor=hp\n\t\t\t\t;;\n\t\t\t-mpeix*)\n\t\t\t\tvendor=hp\n\t\t\t\t;;\n\t\t\t-hiux*)\n\t\t\t\tvendor=hitachi\n\t\t\t\t;;\n\t\t\t-unos*)\n\t\t\t\tvendor=crds\n\t\t\t\t;;\n\t\t\t-dgux*)\n\t\t\t\tvendor=dg\n\t\t\t\t;;\n\t\t\t-luna*)\n\t\t\t\tvendor=omron\n\t\t\t\t;;\n\t\t\t-genix*)\n\t\t\t\tvendor=ns\n\t\t\t\t;;\n\t\t\t-mvs* | -opened*)\n\t\t\t\tvendor=ibm\n\t\t\t\t;;\n\t\t\t-os400*)\n\t\t\t\tvendor=ibm\n\t\t\t\t;;\n\t\t\t-ptx*)\n\t\t\t\tvendor=sequent\n\t\t\t\t;;\n\t\t\t-tpf*)\n\t\t\t\tvendor=ibm\n\t\t\t\t;;\n\t\t\t-vxsim* | -vxworks* | -windiss*)\n\t\t\t\tvendor=wrs\n\t\t\t\t;;\n\t\t\t-aux*)\n\t\t\t\tvendor=apple\n\t\t\t\t;;\n\t\t\t-hms*)\n\t\t\t\tvendor=hitachi\n\t\t\t\t;;\n\t\t\t-mpw* | -macos*)\n\t\t\t\tvendor=apple\n\t\t\t\t;;\n\t\t\t-*mint | -mint[0-9]* | -*MiNT | -MiNT[0-9]*)\n\t\t\t\tvendor=atari\n\t\t\t\t;;\n\t\t\t-vos*)\n\t\t\t\tvendor=stratus\n\t\t\t\t;;\n\t\tesac\n\t\tbasic_machine=`echo $basic_machine | sed \"s/unknown/$vendor/\"`\n\t\t;;\nesac\n\necho $basic_machine$os\nexit\n\n# Local variables:\n# eval: (add-hook 'write-file-hooks 'time-stamp)\n# time-stamp-start: \"timestamp='\"\n# time-stamp-format: \"%:y-%02m-%02d\"\n# time-stamp-end: \"'\"\n# End:\n"
  },
  {
    "path": "deps/jemalloc/build-aux/install-sh",
    "content": "#! /bin/sh\n#\n# install - install a program, script, or datafile\n# This comes from X11R5 (mit/util/scripts/install.sh).\n#\n# Copyright 1991 by the Massachusetts Institute of Technology\n#\n# Permission to use, copy, modify, distribute, and sell this software and its\n# documentation for any purpose is hereby granted without fee, provided that\n# the above copyright notice appear in all copies and that both that\n# copyright notice and this permission notice appear in supporting\n# documentation, and that the name of M.I.T. not be used in advertising or\n# publicity pertaining to distribution of the software without specific,\n# written prior permission.  M.I.T. makes no representations about the\n# suitability of this software for any purpose.  It is provided \"as is\"\n# without express or implied warranty.\n#\n# Calling this script install-sh is preferred over install.sh, to prevent\n# `make' implicit rules from creating a file called install from it\n# when there is no Makefile.\n#\n# This script is compatible with the BSD install script, but was written\n# from scratch.  It can only install one file at a time, a restriction\n# shared with many OS's install programs.\n\n\n# set DOITPROG to echo to test this script\n\n# Don't use :- since 4.3BSD and earlier shells don't like it.\ndoit=\"${DOITPROG-}\"\n\n\n# put in absolute paths if you don't have them in your path; or use env. vars.\n\nmvprog=\"${MVPROG-mv}\"\ncpprog=\"${CPPROG-cp}\"\nchmodprog=\"${CHMODPROG-chmod}\"\nchownprog=\"${CHOWNPROG-chown}\"\nchgrpprog=\"${CHGRPPROG-chgrp}\"\nstripprog=\"${STRIPPROG-strip}\"\nrmprog=\"${RMPROG-rm}\"\nmkdirprog=\"${MKDIRPROG-mkdir}\"\n\ntransformbasename=\"\"\ntransform_arg=\"\"\ninstcmd=\"$mvprog\"\nchmodcmd=\"$chmodprog 0755\"\nchowncmd=\"\"\nchgrpcmd=\"\"\nstripcmd=\"\"\nrmcmd=\"$rmprog -f\"\nmvcmd=\"$mvprog\"\nsrc=\"\"\ndst=\"\"\ndir_arg=\"\"\n\nwhile [ x\"$1\" != x ]; do\n    case $1 in\n\t-c) instcmd=\"$cpprog\"\n\t    shift\n\t    continue;;\n\n\t-d) dir_arg=true\n\t    shift\n\t    continue;;\n\n\t-m) chmodcmd=\"$chmodprog $2\"\n\t    shift\n\t    shift\n\t    continue;;\n\n\t-o) chowncmd=\"$chownprog $2\"\n\t    shift\n\t    shift\n\t    continue;;\n\n\t-g) chgrpcmd=\"$chgrpprog $2\"\n\t    shift\n\t    shift\n\t    continue;;\n\n\t-s) stripcmd=\"$stripprog\"\n\t    shift\n\t    continue;;\n\n\t-t=*) transformarg=`echo $1 | sed 's/-t=//'`\n\t    shift\n\t    continue;;\n\n\t-b=*) transformbasename=`echo $1 | sed 's/-b=//'`\n\t    shift\n\t    continue;;\n\n\t*)  if [ x\"$src\" = x ]\n\t    then\n\t\tsrc=$1\n\t    else\n\t\t# this colon is to work around a 386BSD /bin/sh bug\n\t\t:\n\t\tdst=$1\n\t    fi\n\t    shift\n\t    continue;;\n    esac\ndone\n\nif [ x\"$src\" = x ]\nthen\n\techo \"install:\tno input file specified\"\n\texit 1\nelse\n\ttrue\nfi\n\nif [ x\"$dir_arg\" != x ]; then\n\tdst=$src\n\tsrc=\"\"\n\t\n\tif [ -d $dst ]; then\n\t\tinstcmd=:\n\telse\n\t\tinstcmd=mkdir\n\tfi\nelse\n\n# Waiting for this to be detected by the \"$instcmd $src $dsttmp\" command\n# might cause directories to be created, which would be especially bad \n# if $src (and thus $dsttmp) contains '*'.\n\n\tif [ -f $src -o -d $src ]\n\tthen\n\t\ttrue\n\telse\n\t\techo \"install:  $src does not exist\"\n\t\texit 1\n\tfi\n\t\n\tif [ x\"$dst\" = x ]\n\tthen\n\t\techo \"install:\tno destination specified\"\n\t\texit 1\n\telse\n\t\ttrue\n\tfi\n\n# If destination is a directory, append the input filename; if your system\n# does not like double slashes in filenames, you may need to add some logic\n\n\tif [ -d $dst ]\n\tthen\n\t\tdst=\"$dst\"/`basename $src`\n\telse\n\t\ttrue\n\tfi\nfi\n\n## this sed command emulates the dirname command\ndstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`\n\n# Make sure that the destination directory exists.\n#  this part is taken from Noah Friedman's mkinstalldirs script\n\n# Skip lots of stat calls in the usual case.\nif [ ! -d \"$dstdir\" ]; then\ndefaultIFS='\t\n'\nIFS=\"${IFS-${defaultIFS}}\"\n\noIFS=\"${IFS}\"\n# Some sh's can't handle IFS=/ for some reason.\nIFS='%'\nset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`\nIFS=\"${oIFS}\"\n\npathcomp=''\n\nwhile [ $# -ne 0 ] ; do\n\tpathcomp=\"${pathcomp}${1}\"\n\tshift\n\n\tif [ ! -d \"${pathcomp}\" ] ;\n        then\n\t\t$mkdirprog \"${pathcomp}\"\n\telse\n\t\ttrue\n\tfi\n\n\tpathcomp=\"${pathcomp}/\"\ndone\nfi\n\nif [ x\"$dir_arg\" != x ]\nthen\n\t$doit $instcmd $dst &&\n\n\tif [ x\"$chowncmd\" != x ]; then $doit $chowncmd $dst; else true ; fi &&\n\tif [ x\"$chgrpcmd\" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&\n\tif [ x\"$stripcmd\" != x ]; then $doit $stripcmd $dst; else true ; fi &&\n\tif [ x\"$chmodcmd\" != x ]; then $doit $chmodcmd $dst; else true ; fi\nelse\n\n# If we're going to rename the final executable, determine the name now.\n\n\tif [ x\"$transformarg\" = x ] \n\tthen\n\t\tdstfile=`basename $dst`\n\telse\n\t\tdstfile=`basename $dst $transformbasename | \n\t\t\tsed $transformarg`$transformbasename\n\tfi\n\n# don't allow the sed command to completely eliminate the filename\n\n\tif [ x\"$dstfile\" = x ] \n\tthen\n\t\tdstfile=`basename $dst`\n\telse\n\t\ttrue\n\tfi\n\n# Make a temp file name in the proper directory.\n\n\tdsttmp=$dstdir/#inst.$$#\n\n# Move or copy the file name to the temp name\n\n\t$doit $instcmd $src $dsttmp &&\n\n\ttrap \"rm -f ${dsttmp}\" 0 &&\n\n# and set any options; do chmod last to preserve setuid bits\n\n# If any of these fail, we abort the whole thing.  If we want to\n# ignore errors from any of these, just make sure not to ignore\n# errors from the above \"$doit $instcmd $src $dsttmp\" command.\n\n\tif [ x\"$chowncmd\" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&\n\tif [ x\"$chgrpcmd\" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&\n\tif [ x\"$stripcmd\" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&\n\tif [ x\"$chmodcmd\" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&\n\n# Now rename the file to the real destination.\n\n\t$doit $rmcmd -f $dstdir/$dstfile &&\n\t$doit $mvcmd $dsttmp $dstdir/$dstfile \n\nfi &&\n\n\nexit 0\n"
  },
  {
    "path": "deps/jemalloc/config.stamp.in",
    "content": ""
  },
  {
    "path": "deps/jemalloc/configure",
    "content": "#! /bin/sh\n# Guess values for system-dependent variables and create Makefiles.\n# Generated by GNU Autoconf 2.69.\n#\n#\n# Copyright (C) 1992-1996, 1998-2012 Free Software Foundation, Inc.\n#\n#\n# This configure script is free software; the Free Software Foundation\n# gives unlimited permission to copy, distribute and modify it.\n## -------------------- ##\n## M4sh Initialization. ##\n## -------------------- ##\n\n# Be more Bourne compatible\nDUALCASE=1; export DUALCASE # for MKS sh\nif test -n \"${ZSH_VERSION+set}\" && (emulate sh) >/dev/null 2>&1; then :\n  emulate sh\n  NULLCMD=:\n  # Pre-4.2 versions of Zsh do word splitting on ${1+\"$@\"}, which\n  # is contrary to our usage.  Disable this feature.\n  alias -g '${1+\"$@\"}'='\"$@\"'\n  setopt NO_GLOB_SUBST\nelse\n  case `(set -o) 2>/dev/null` in #(\n  *posix*) :\n    set -o posix ;; #(\n  *) :\n     ;;\nesac\nfi\n\n\nas_nl='\n'\nexport as_nl\n# Printing a long string crashes Solaris 7 /usr/bin/printf.\nas_echo='\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'\nas_echo=$as_echo$as_echo$as_echo$as_echo$as_echo\nas_echo=$as_echo$as_echo$as_echo$as_echo$as_echo$as_echo\n# Prefer a ksh shell builtin over an external printf program on Solaris,\n# but without wasting forks for bash or zsh.\nif test -z \"$BASH_VERSION$ZSH_VERSION\" \\\n    && (test \"X`print -r -- $as_echo`\" = \"X$as_echo\") 2>/dev/null; then\n  as_echo='print -r --'\n  as_echo_n='print -rn --'\nelif (test \"X`printf %s $as_echo`\" = \"X$as_echo\") 2>/dev/null; then\n  as_echo='printf %s\\n'\n  as_echo_n='printf %s'\nelse\n  if test \"X`(/usr/ucb/echo -n -n $as_echo) 2>/dev/null`\" = \"X-n $as_echo\"; then\n    as_echo_body='eval /usr/ucb/echo -n \"$1$as_nl\"'\n    as_echo_n='/usr/ucb/echo -n'\n  else\n    as_echo_body='eval expr \"X$1\" : \"X\\\\(.*\\\\)\"'\n    as_echo_n_body='eval\n      arg=$1;\n      case $arg in #(\n      *\"$as_nl\"*)\n\texpr \"X$arg\" : \"X\\\\(.*\\\\)$as_nl\";\n\targ=`expr \"X$arg\" : \".*$as_nl\\\\(.*\\\\)\"`;;\n      esac;\n      expr \"X$arg\" : \"X\\\\(.*\\\\)\" | tr -d \"$as_nl\"\n    '\n    export as_echo_n_body\n    as_echo_n='sh -c $as_echo_n_body as_echo'\n  fi\n  export as_echo_body\n  as_echo='sh -c $as_echo_body as_echo'\nfi\n\n# The user is always right.\nif test \"${PATH_SEPARATOR+set}\" != set; then\n  PATH_SEPARATOR=:\n  (PATH='/bin;/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 && {\n    (PATH='/bin:/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 ||\n      PATH_SEPARATOR=';'\n  }\nfi\n\n\n# IFS\n# We need space, tab and new line, in precisely that order.  Quoting is\n# there to prevent editors from complaining about space-tab.\n# (If _AS_PATH_WALK were called with IFS unset, it would disable word\n# splitting by setting IFS to empty value.)\nIFS=\" \"\"\t$as_nl\"\n\n# Find who we are.  Look in the path if we contain no directory separator.\nas_myself=\ncase $0 in #((\n  *[\\\\/]* ) as_myself=$0 ;;\n  *) as_save_IFS=$IFS; IFS=$PATH_SEPARATOR\nfor as_dir in $PATH\ndo\n  IFS=$as_save_IFS\n  test -z \"$as_dir\" && as_dir=.\n    test -r \"$as_dir/$0\" && as_myself=$as_dir/$0 && break\n  done\nIFS=$as_save_IFS\n\n     ;;\nesac\n# We did not find ourselves, most probably we were run as `sh COMMAND'\n# in which case we are not to be found in the path.\nif test \"x$as_myself\" = x; then\n  as_myself=$0\nfi\nif test ! -f \"$as_myself\"; then\n  $as_echo \"$as_myself: error: cannot find myself; rerun with an absolute file name\" >&2\n  exit 1\nfi\n\n# Unset variables that we do not need and which cause bugs (e.g. in\n# pre-3.0 UWIN ksh).  But do not cause bugs in bash 2.01; the \"|| exit 1\"\n# suppresses any \"Segmentation fault\" message there.  '((' could\n# trigger a bug in pdksh 5.2.14.\nfor as_var in BASH_ENV ENV MAIL MAILPATH\ndo eval test x\\${$as_var+set} = xset \\\n  && ( (unset $as_var) || exit 1) >/dev/null 2>&1 && unset $as_var || :\ndone\nPS1='$ '\nPS2='> '\nPS4='+ '\n\n# NLS nuisances.\nLC_ALL=C\nexport LC_ALL\nLANGUAGE=C\nexport LANGUAGE\n\n# CDPATH.\n(unset CDPATH) >/dev/null 2>&1 && unset CDPATH\n\n# Use a proper internal environment variable to ensure we don't fall\n  # into an infinite loop, continuously re-executing ourselves.\n  if test x\"${_as_can_reexec}\" != xno && test \"x$CONFIG_SHELL\" != x; then\n    _as_can_reexec=no; export _as_can_reexec;\n    # We cannot yet assume a decent shell, so we have to provide a\n# neutralization value for shells without unset; and this also\n# works around shells that cannot unset nonexistent variables.\n# Preserve -v and -x to the replacement shell.\nBASH_ENV=/dev/null\nENV=/dev/null\n(unset BASH_ENV) >/dev/null 2>&1 && unset BASH_ENV ENV\ncase $- in # ((((\n  *v*x* | *x*v* ) as_opts=-vx ;;\n  *v* ) as_opts=-v ;;\n  *x* ) as_opts=-x ;;\n  * ) as_opts= ;;\nesac\nexec $CONFIG_SHELL $as_opts \"$as_myself\" ${1+\"$@\"}\n# Admittedly, this is quite paranoid, since all the known shells bail\n# out after a failed `exec'.\n$as_echo \"$0: could not re-execute with $CONFIG_SHELL\" >&2\nas_fn_exit 255\n  fi\n  # We don't want this to propagate to other subprocesses.\n          { _as_can_reexec=; unset _as_can_reexec;}\nif test \"x$CONFIG_SHELL\" = x; then\n  as_bourne_compatible=\"if test -n \\\"\\${ZSH_VERSION+set}\\\" && (emulate sh) >/dev/null 2>&1; then :\n  emulate sh\n  NULLCMD=:\n  # Pre-4.2 versions of Zsh do word splitting on \\${1+\\\"\\$@\\\"}, which\n  # is contrary to our usage.  Disable this feature.\n  alias -g '\\${1+\\\"\\$@\\\"}'='\\\"\\$@\\\"'\n  setopt NO_GLOB_SUBST\nelse\n  case \\`(set -o) 2>/dev/null\\` in #(\n  *posix*) :\n    set -o posix ;; #(\n  *) :\n     ;;\nesac\nfi\n\"\n  as_required=\"as_fn_return () { (exit \\$1); }\nas_fn_success () { as_fn_return 0; }\nas_fn_failure () { as_fn_return 1; }\nas_fn_ret_success () { return 0; }\nas_fn_ret_failure () { return 1; }\n\nexitcode=0\nas_fn_success || { exitcode=1; echo as_fn_success failed.; }\nas_fn_failure && { exitcode=1; echo as_fn_failure succeeded.; }\nas_fn_ret_success || { exitcode=1; echo as_fn_ret_success failed.; }\nas_fn_ret_failure && { exitcode=1; echo as_fn_ret_failure succeeded.; }\nif ( set x; as_fn_ret_success y && test x = \\\"\\$1\\\" ); then :\n\nelse\n  exitcode=1; echo positional parameters were not saved.\nfi\ntest x\\$exitcode = x0 || exit 1\ntest -x / || exit 1\"\n  as_suggested=\"  as_lineno_1=\";as_suggested=$as_suggested$LINENO;as_suggested=$as_suggested\" as_lineno_1a=\\$LINENO\n  as_lineno_2=\";as_suggested=$as_suggested$LINENO;as_suggested=$as_suggested\" as_lineno_2a=\\$LINENO\n  eval 'test \\\"x\\$as_lineno_1'\\$as_run'\\\" != \\\"x\\$as_lineno_2'\\$as_run'\\\" &&\n  test \\\"x\\`expr \\$as_lineno_1'\\$as_run' + 1\\`\\\" = \\\"x\\$as_lineno_2'\\$as_run'\\\"' || exit 1\ntest \\$(( 1 + 1 )) = 2 || exit 1\"\n  if (eval \"$as_required\") 2>/dev/null; then :\n  as_have_required=yes\nelse\n  as_have_required=no\nfi\n  if test x$as_have_required = xyes && (eval \"$as_suggested\") 2>/dev/null; then :\n\nelse\n  as_save_IFS=$IFS; IFS=$PATH_SEPARATOR\nas_found=false\nfor as_dir in /bin$PATH_SEPARATOR/usr/bin$PATH_SEPARATOR$PATH\ndo\n  IFS=$as_save_IFS\n  test -z \"$as_dir\" && as_dir=.\n  as_found=:\n  case $as_dir in #(\n\t /*)\n\t   for as_base in sh bash ksh sh5; do\n\t     # Try only shells that exist, to save several forks.\n\t     as_shell=$as_dir/$as_base\n\t     if { test -f \"$as_shell\" || test -f \"$as_shell.exe\"; } &&\n\t\t    { $as_echo \"$as_bourne_compatible\"\"$as_required\" | as_run=a \"$as_shell\"; } 2>/dev/null; then :\n  CONFIG_SHELL=$as_shell as_have_required=yes\n\t\t   if { $as_echo \"$as_bourne_compatible\"\"$as_suggested\" | as_run=a \"$as_shell\"; } 2>/dev/null; then :\n  break 2\nfi\nfi\n\t   done;;\n       esac\n  as_found=false\ndone\n$as_found || { if { test -f \"$SHELL\" || test -f \"$SHELL.exe\"; } &&\n\t      { $as_echo \"$as_bourne_compatible\"\"$as_required\" | as_run=a \"$SHELL\"; } 2>/dev/null; then :\n  CONFIG_SHELL=$SHELL as_have_required=yes\nfi; }\nIFS=$as_save_IFS\n\n\n      if test \"x$CONFIG_SHELL\" != x; then :\n  export CONFIG_SHELL\n             # We cannot yet assume a decent shell, so we have to provide a\n# neutralization value for shells without unset; and this also\n# works around shells that cannot unset nonexistent variables.\n# Preserve -v and -x to the replacement shell.\nBASH_ENV=/dev/null\nENV=/dev/null\n(unset BASH_ENV) >/dev/null 2>&1 && unset BASH_ENV ENV\ncase $- in # ((((\n  *v*x* | *x*v* ) as_opts=-vx ;;\n  *v* ) as_opts=-v ;;\n  *x* ) as_opts=-x ;;\n  * ) as_opts= ;;\nesac\nexec $CONFIG_SHELL $as_opts \"$as_myself\" ${1+\"$@\"}\n# Admittedly, this is quite paranoid, since all the known shells bail\n# out after a failed `exec'.\n$as_echo \"$0: could not re-execute with $CONFIG_SHELL\" >&2\nexit 255\nfi\n\n    if test x$as_have_required = xno; then :\n  $as_echo \"$0: This script requires a shell more modern than all\"\n  $as_echo \"$0: the shells that I found on your system.\"\n  if test x${ZSH_VERSION+set} = xset ; then\n    $as_echo \"$0: In particular, zsh $ZSH_VERSION has bugs and should\"\n    $as_echo \"$0: be upgraded to zsh 4.3.4 or later.\"\n  else\n    $as_echo \"$0: Please tell bug-autoconf@gnu.org about your system,\n$0: including any error possibly output before this\n$0: message. Then install a modern shell, or manually run\n$0: the script under such a shell if you do have one.\"\n  fi\n  exit 1\nfi\nfi\nfi\nSHELL=${CONFIG_SHELL-/bin/sh}\nexport SHELL\n# Unset more variables known to interfere with behavior of common tools.\nCLICOLOR_FORCE= GREP_OPTIONS=\nunset CLICOLOR_FORCE GREP_OPTIONS\n\n## --------------------- ##\n## M4sh Shell Functions. ##\n## --------------------- ##\n# as_fn_unset VAR\n# ---------------\n# Portably unset VAR.\nas_fn_unset ()\n{\n  { eval $1=; unset $1;}\n}\nas_unset=as_fn_unset\n\n# as_fn_set_status STATUS\n# -----------------------\n# Set $? to STATUS, without forking.\nas_fn_set_status ()\n{\n  return $1\n} # as_fn_set_status\n\n# as_fn_exit STATUS\n# -----------------\n# Exit the shell with STATUS, even in a \"trap 0\" or \"set -e\" context.\nas_fn_exit ()\n{\n  set +e\n  as_fn_set_status $1\n  exit $1\n} # as_fn_exit\n\n# as_fn_mkdir_p\n# -------------\n# Create \"$as_dir\" as a directory, including parents if necessary.\nas_fn_mkdir_p ()\n{\n\n  case $as_dir in #(\n  -*) as_dir=./$as_dir;;\n  esac\n  test -d \"$as_dir\" || eval $as_mkdir_p || {\n    as_dirs=\n    while :; do\n      case $as_dir in #(\n      *\\'*) as_qdir=`$as_echo \"$as_dir\" | sed \"s/'/'\\\\\\\\\\\\\\\\''/g\"`;; #'(\n      *) as_qdir=$as_dir;;\n      esac\n      as_dirs=\"'$as_qdir' $as_dirs\"\n      as_dir=`$as_dirname -- \"$as_dir\" ||\n$as_expr X\"$as_dir\" : 'X\\(.*[^/]\\)//*[^/][^/]*/*$' \\| \\\n\t X\"$as_dir\" : 'X\\(//\\)[^/]' \\| \\\n\t X\"$as_dir\" : 'X\\(//\\)$' \\| \\\n\t X\"$as_dir\" : 'X\\(/\\)' \\| . 2>/dev/null ||\n$as_echo X\"$as_dir\" |\n    sed '/^X\\(.*[^/]\\)\\/\\/*[^/][^/]*\\/*$/{\n\t    s//\\1/\n\t    q\n\t  }\n\t  /^X\\(\\/\\/\\)[^/].*/{\n\t    s//\\1/\n\t    q\n\t  }\n\t  /^X\\(\\/\\/\\)$/{\n\t    s//\\1/\n\t    q\n\t  }\n\t  /^X\\(\\/\\).*/{\n\t    s//\\1/\n\t    q\n\t  }\n\t  s/.*/./; q'`\n      test -d \"$as_dir\" && break\n    done\n    test -z \"$as_dirs\" || eval \"mkdir $as_dirs\"\n  } || test -d \"$as_dir\" || as_fn_error $? \"cannot create directory $as_dir\"\n\n\n} # as_fn_mkdir_p\n\n# as_fn_executable_p FILE\n# -----------------------\n# Test if FILE is an executable regular file.\nas_fn_executable_p ()\n{\n  test -f \"$1\" && test -x \"$1\"\n} # as_fn_executable_p\n# as_fn_append VAR VALUE\n# ----------------------\n# Append the text in VALUE to the end of the definition contained in VAR. Take\n# advantage of any shell optimizations that allow amortized linear growth over\n# repeated appends, instead of the typical quadratic growth present in naive\n# implementations.\nif (eval \"as_var=1; as_var+=2; test x\\$as_var = x12\") 2>/dev/null; then :\n  eval 'as_fn_append ()\n  {\n    eval $1+=\\$2\n  }'\nelse\n  as_fn_append ()\n  {\n    eval $1=\\$$1\\$2\n  }\nfi # as_fn_append\n\n# as_fn_arith ARG...\n# ------------------\n# Perform arithmetic evaluation on the ARGs, and store the result in the\n# global $as_val. Take advantage of shells that can avoid forks. The arguments\n# must be portable across $(()) and expr.\nif (eval \"test \\$(( 1 + 1 )) = 2\") 2>/dev/null; then :\n  eval 'as_fn_arith ()\n  {\n    as_val=$(( $* ))\n  }'\nelse\n  as_fn_arith ()\n  {\n    as_val=`expr \"$@\" || test $? -eq 1`\n  }\nfi # as_fn_arith\n\n\n# as_fn_error STATUS ERROR [LINENO LOG_FD]\n# ----------------------------------------\n# Output \"`basename $0`: error: ERROR\" to stderr. If LINENO and LOG_FD are\n# provided, also output the error to LOG_FD, referencing LINENO. Then exit the\n# script with STATUS, using 1 if that was 0.\nas_fn_error ()\n{\n  as_status=$1; test $as_status -eq 0 && as_status=1\n  if test \"$4\"; then\n    as_lineno=${as_lineno-\"$3\"} as_lineno_stack=as_lineno_stack=$as_lineno_stack\n    $as_echo \"$as_me:${as_lineno-$LINENO}: error: $2\" >&$4\n  fi\n  $as_echo \"$as_me: error: $2\" >&2\n  as_fn_exit $as_status\n} # as_fn_error\n\nif expr a : '\\(a\\)' >/dev/null 2>&1 &&\n   test \"X`expr 00001 : '.*\\(...\\)'`\" = X001; then\n  as_expr=expr\nelse\n  as_expr=false\nfi\n\nif (basename -- /) >/dev/null 2>&1 && test \"X`basename -- / 2>&1`\" = \"X/\"; then\n  as_basename=basename\nelse\n  as_basename=false\nfi\n\nif (as_dir=`dirname -- /` && test \"X$as_dir\" = X/) >/dev/null 2>&1; then\n  as_dirname=dirname\nelse\n  as_dirname=false\nfi\n\nas_me=`$as_basename -- \"$0\" ||\n$as_expr X/\"$0\" : '.*/\\([^/][^/]*\\)/*$' \\| \\\n\t X\"$0\" : 'X\\(//\\)$' \\| \\\n\t X\"$0\" : 'X\\(/\\)' \\| . 2>/dev/null ||\n$as_echo X/\"$0\" |\n    sed '/^.*\\/\\([^/][^/]*\\)\\/*$/{\n\t    s//\\1/\n\t    q\n\t  }\n\t  /^X\\/\\(\\/\\/\\)$/{\n\t    s//\\1/\n\t    q\n\t  }\n\t  /^X\\/\\(\\/\\).*/{\n\t    s//\\1/\n\t    q\n\t  }\n\t  s/.*/./; q'`\n\n# Avoid depending upon Character Ranges.\nas_cr_letters='abcdefghijklmnopqrstuvwxyz'\nas_cr_LETTERS='ABCDEFGHIJKLMNOPQRSTUVWXYZ'\nas_cr_Letters=$as_cr_letters$as_cr_LETTERS\nas_cr_digits='0123456789'\nas_cr_alnum=$as_cr_Letters$as_cr_digits\n\n\n  as_lineno_1=$LINENO as_lineno_1a=$LINENO\n  as_lineno_2=$LINENO as_lineno_2a=$LINENO\n  eval 'test \"x$as_lineno_1'$as_run'\" != \"x$as_lineno_2'$as_run'\" &&\n  test \"x`expr $as_lineno_1'$as_run' + 1`\" = \"x$as_lineno_2'$as_run'\"' || {\n  # Blame Lee E. McMahon (1931-1989) for sed's syntax.  :-)\n  sed -n '\n    p\n    /[$]LINENO/=\n  ' <$as_myself |\n    sed '\n      s/[$]LINENO.*/&-/\n      t lineno\n      b\n      :lineno\n      N\n      :loop\n      s/[$]LINENO\\([^'$as_cr_alnum'_].*\\n\\)\\(.*\\)/\\2\\1\\2/\n      t loop\n      s/-\\n.*//\n    ' >$as_me.lineno &&\n  chmod +x \"$as_me.lineno\" ||\n    { $as_echo \"$as_me: error: cannot create $as_me.lineno; rerun with a POSIX shell\" >&2; as_fn_exit 1; }\n\n  # If we had to re-execute with $CONFIG_SHELL, we're ensured to have\n  # already done that, so ensure we don't try to do so again and fall\n  # in an infinite loop.  This has already happened in practice.\n  _as_can_reexec=no; export _as_can_reexec\n  # Don't try to exec as it changes $[0], causing all sort of problems\n  # (the dirname of $[0] is not the place where we might find the\n  # original and so on.  Autoconf is especially sensitive to this).\n  . \"./$as_me.lineno\"\n  # Exit status is that of the last command.\n  exit\n}\n\nECHO_C= ECHO_N= ECHO_T=\ncase `echo -n x` in #(((((\n-n*)\n  case `echo 'xy\\c'` in\n  *c*) ECHO_T='\t';;\t# ECHO_T is single tab character.\n  xy)  ECHO_C='\\c';;\n  *)   echo `echo ksh88 bug on AIX 6.1` > /dev/null\n       ECHO_T='\t';;\n  esac;;\n*)\n  ECHO_N='-n';;\nesac\n\nrm -f conf$$ conf$$.exe conf$$.file\nif test -d conf$$.dir; then\n  rm -f conf$$.dir/conf$$.file\nelse\n  rm -f conf$$.dir\n  mkdir conf$$.dir 2>/dev/null\nfi\nif (echo >conf$$.file) 2>/dev/null; then\n  if ln -s conf$$.file conf$$ 2>/dev/null; then\n    as_ln_s='ln -s'\n    # ... but there are two gotchas:\n    # 1) On MSYS, both `ln -s file dir' and `ln file dir' fail.\n    # 2) DJGPP < 2.04 has no symlinks; `ln -s' creates a wrapper executable.\n    # In both cases, we have to default to `cp -pR'.\n    ln -s conf$$.file conf$$.dir 2>/dev/null && test ! -f conf$$.exe ||\n      as_ln_s='cp -pR'\n  elif ln conf$$.file conf$$ 2>/dev/null; then\n    as_ln_s=ln\n  else\n    as_ln_s='cp -pR'\n  fi\nelse\n  as_ln_s='cp -pR'\nfi\nrm -f conf$$ conf$$.exe conf$$.dir/conf$$.file conf$$.file\nrmdir conf$$.dir 2>/dev/null\n\nif mkdir -p . 2>/dev/null; then\n  as_mkdir_p='mkdir -p \"$as_dir\"'\nelse\n  test -d ./-p && rmdir ./-p\n  as_mkdir_p=false\nfi\n\nas_test_x='test -x'\nas_executable_p=as_fn_executable_p\n\n# Sed expression to map a string onto a valid CPP name.\nas_tr_cpp=\"eval sed 'y%*$as_cr_letters%P$as_cr_LETTERS%;s%[^_$as_cr_alnum]%_%g'\"\n\n# Sed expression to map a string onto a valid variable name.\nas_tr_sh=\"eval sed 'y%*+%pp%;s%[^_$as_cr_alnum]%_%g'\"\n\n\ntest -n \"$DJDIR\" || exec 7<&0 </dev/null\nexec 6>&1\n\n# Name of the host.\n# hostname on some systems (SVR3.2, old GNU/Linux) returns a bogus exit status,\n# so uname gets run too.\nac_hostname=`(hostname || uname -n) 2>/dev/null | sed 1q`\n\n#\n# Initializations.\n#\nac_default_prefix=/usr/local\nac_clean_files=\nac_config_libobj_dir=.\nLIBOBJS=\ncross_compiling=no\nsubdirs=\nMFLAGS=\nMAKEFLAGS=\n\n# Identity of this package.\nPACKAGE_NAME=\nPACKAGE_TARNAME=\nPACKAGE_VERSION=\nPACKAGE_STRING=\nPACKAGE_BUGREPORT=\nPACKAGE_URL=\n\nac_unique_file=\"Makefile.in\"\n# Factoring default headers for most tests.\nac_includes_default=\"\\\n#include <stdio.h>\n#ifdef HAVE_SYS_TYPES_H\n# include <sys/types.h>\n#endif\n#ifdef HAVE_SYS_STAT_H\n# include <sys/stat.h>\n#endif\n#ifdef STDC_HEADERS\n# include <stdlib.h>\n# include <stddef.h>\n#else\n# ifdef HAVE_STDLIB_H\n#  include <stdlib.h>\n# endif\n#endif\n#ifdef HAVE_STRING_H\n# if !defined STDC_HEADERS && defined HAVE_MEMORY_H\n#  include <memory.h>\n# endif\n# include <string.h>\n#endif\n#ifdef HAVE_STRINGS_H\n# include <strings.h>\n#endif\n#ifdef HAVE_INTTYPES_H\n# include <inttypes.h>\n#endif\n#ifdef HAVE_STDINT_H\n# include <stdint.h>\n#endif\n#ifdef HAVE_UNISTD_H\n# include <unistd.h>\n#endif\"\n\nac_subst_vars='LTLIBOBJS\nLIBOBJS\ncfgoutputs_out\ncfgoutputs_in\ncfghdrs_out\ncfghdrs_in\nenable_initial_exec_tls\nenable_zone_allocator\nenable_tls\nenable_lazy_lock\nlibdl\nenable_opt_safety_checks\nenable_readlinkat\nenable_log\nenable_cache_oblivious\nenable_xmalloc\nenable_utrace\nenable_fill\nenable_prof\nenable_experimental_smallocx\nenable_stats\nenable_debug\nje_\ninstall_suffix\nprivate_namespace\nJEMALLOC_CPREFIX\nJEMALLOC_PREFIX\nenable_static\nenable_shared\nenable_doc\nAUTOCONF\nLD\nRANLIB\nINSTALL_DATA\nINSTALL_SCRIPT\nINSTALL_PROGRAM\nenable_autogen\nRPATH_EXTRA\nLM\nCC_MM\nDUMP_SYMS\nAROUT\nARFLAGS\nMKLIB\nTEST_LD_MODE\nLDTARGET\nCTARGET\nPIC_CFLAGS\nSOREV\nEXTRA_LDFLAGS\nDSO_LDFLAGS\nlink_whole_archive\nlibprefix\nexe\na\no\nimportlib\nso\nLD_PRELOAD_VAR\nRPATH\nabi\njemalloc_version_gid\njemalloc_version_nrev\njemalloc_version_bugfix\njemalloc_version_minor\njemalloc_version_major\njemalloc_version\nAWK\nNM\nAR\nhost_os\nhost_vendor\nhost_cpu\nhost\nbuild_os\nbuild_vendor\nbuild_cpu\nbuild\nEGREP\nGREP\nEXTRA_CXXFLAGS\nSPECIFIED_CXXFLAGS\nCONFIGURE_CXXFLAGS\nenable_cxx\nHAVE_CXX14\nac_ct_CXX\nCXXFLAGS\nCXX\nCPP\nEXTRA_CFLAGS\nSPECIFIED_CFLAGS\nCONFIGURE_CFLAGS\nOBJEXT\nEXEEXT\nac_ct_CC\nCPPFLAGS\nLDFLAGS\nCFLAGS\nCC\nXSLROOT\nXSLTPROC\nMANDIR\nDATADIR\nLIBDIR\nINCLUDEDIR\nBINDIR\nPREFIX\nabs_objroot\nobjroot\nabs_srcroot\nsrcroot\nrev\nCONFIG\ntarget_alias\nhost_alias\nbuild_alias\nLIBS\nECHO_T\nECHO_N\nECHO_C\nDEFS\nmandir\nlocaledir\nlibdir\npsdir\npdfdir\ndvidir\nhtmldir\ninfodir\ndocdir\noldincludedir\nincludedir\nrunstatedir\nlocalstatedir\nsharedstatedir\nsysconfdir\ndatadir\ndatarootdir\nlibexecdir\nsbindir\nbindir\nprogram_transform_name\nprefix\nexec_prefix\nPACKAGE_URL\nPACKAGE_BUGREPORT\nPACKAGE_STRING\nPACKAGE_VERSION\nPACKAGE_TARNAME\nPACKAGE_NAME\nPATH_SEPARATOR\nSHELL'\nac_subst_files=''\nac_user_opts='\nenable_option_checking\nwith_xslroot\nenable_cxx\nwith_lg_vaddr\nwith_version\nwith_rpath\nenable_autogen\nenable_doc\nenable_shared\nenable_static\nwith_mangling\nwith_jemalloc_prefix\nwith_export\nwith_private_namespace\nwith_install_suffix\nwith_malloc_conf\nenable_debug\nenable_stats\nenable_experimental_smallocx\nenable_prof\nenable_prof_libunwind\nwith_static_libunwind\nenable_prof_libgcc\nenable_prof_gcc\nenable_fill\nenable_utrace\nenable_xmalloc\nenable_cache_oblivious\nenable_log\nenable_readlinkat\nenable_opt_safety_checks\nwith_lg_quantum\nwith_lg_page\nwith_lg_hugepage\nenable_libdl\nenable_syscall\nenable_lazy_lock\nenable_zone_allocator\nenable_initial_exec_tls\n'\n      ac_precious_vars='build_alias\nhost_alias\ntarget_alias\nCC\nCFLAGS\nLDFLAGS\nLIBS\nCPPFLAGS\nCPP\nCXX\nCXXFLAGS\nCCC'\n\n\n# Initialize some variables set by options.\nac_init_help=\nac_init_version=false\nac_unrecognized_opts=\nac_unrecognized_sep=\n# The variables have the same names as the options, with\n# dashes changed to underlines.\ncache_file=/dev/null\nexec_prefix=NONE\nno_create=\nno_recursion=\nprefix=NONE\nprogram_prefix=NONE\nprogram_suffix=NONE\nprogram_transform_name=s,x,x,\nsilent=\nsite=\nsrcdir=\nverbose=\nx_includes=NONE\nx_libraries=NONE\n\n# Installation directory options.\n# These are left unexpanded so users can \"make install exec_prefix=/foo\"\n# and all the variables that are supposed to be based on exec_prefix\n# by default will actually change.\n# Use braces instead of parens because sh, perl, etc. also accept them.\n# (The list follows the same order as the GNU Coding Standards.)\nbindir='${exec_prefix}/bin'\nsbindir='${exec_prefix}/sbin'\nlibexecdir='${exec_prefix}/libexec'\ndatarootdir='${prefix}/share'\ndatadir='${datarootdir}'\nsysconfdir='${prefix}/etc'\nsharedstatedir='${prefix}/com'\nlocalstatedir='${prefix}/var'\nrunstatedir='${localstatedir}/run'\nincludedir='${prefix}/include'\noldincludedir='/usr/include'\ndocdir='${datarootdir}/doc/${PACKAGE}'\ninfodir='${datarootdir}/info'\nhtmldir='${docdir}'\ndvidir='${docdir}'\npdfdir='${docdir}'\npsdir='${docdir}'\nlibdir='${exec_prefix}/lib'\nlocaledir='${datarootdir}/locale'\nmandir='${datarootdir}/man'\n\nac_prev=\nac_dashdash=\nfor ac_option\ndo\n  # If the previous option needs an argument, assign it.\n  if test -n \"$ac_prev\"; then\n    eval $ac_prev=\\$ac_option\n    ac_prev=\n    continue\n  fi\n\n  case $ac_option in\n  *=?*) ac_optarg=`expr \"X$ac_option\" : '[^=]*=\\(.*\\)'` ;;\n  *=)   ac_optarg= ;;\n  *)    ac_optarg=yes ;;\n  esac\n\n  # Accept the important Cygnus configure options, so we can diagnose typos.\n\n  case $ac_dashdash$ac_option in\n  --)\n    ac_dashdash=yes ;;\n\n  -bindir | --bindir | --bindi | --bind | --bin | --bi)\n    ac_prev=bindir ;;\n  -bindir=* | --bindir=* | --bindi=* | --bind=* | --bin=* | --bi=*)\n    bindir=$ac_optarg ;;\n\n  -build | --build | --buil | --bui | --bu)\n    ac_prev=build_alias ;;\n  -build=* | --build=* | --buil=* | --bui=* | --bu=*)\n    build_alias=$ac_optarg ;;\n\n  -cache-file | --cache-file | --cache-fil | --cache-fi \\\n  | --cache-f | --cache- | --cache | --cach | --cac | --ca | --c)\n    ac_prev=cache_file ;;\n  -cache-file=* | --cache-file=* | --cache-fil=* | --cache-fi=* \\\n  | --cache-f=* | --cache-=* | --cache=* | --cach=* | --cac=* | --ca=* | --c=*)\n    cache_file=$ac_optarg ;;\n\n  --config-cache | -C)\n    cache_file=config.cache ;;\n\n  -datadir | --datadir | --datadi | --datad)\n    ac_prev=datadir ;;\n  -datadir=* | --datadir=* | --datadi=* | --datad=*)\n    datadir=$ac_optarg ;;\n\n  -datarootdir | --datarootdir | --datarootdi | --datarootd | --dataroot \\\n  | --dataroo | --dataro | --datar)\n    ac_prev=datarootdir ;;\n  -datarootdir=* | --datarootdir=* | --datarootdi=* | --datarootd=* \\\n  | --dataroot=* | --dataroo=* | --dataro=* | --datar=*)\n    datarootdir=$ac_optarg ;;\n\n  -disable-* | --disable-*)\n    ac_useropt=`expr \"x$ac_option\" : 'x-*disable-\\(.*\\)'`\n    # Reject names that are not valid shell variable names.\n    expr \"x$ac_useropt\" : \".*[^-+._$as_cr_alnum]\" >/dev/null &&\n      as_fn_error $? \"invalid feature name: $ac_useropt\"\n    ac_useropt_orig=$ac_useropt\n    ac_useropt=`$as_echo \"$ac_useropt\" | sed 's/[-+.]/_/g'`\n    case $ac_user_opts in\n      *\"\n\"enable_$ac_useropt\"\n\"*) ;;\n      *) ac_unrecognized_opts=\"$ac_unrecognized_opts$ac_unrecognized_sep--disable-$ac_useropt_orig\"\n\t ac_unrecognized_sep=', ';;\n    esac\n    eval enable_$ac_useropt=no ;;\n\n  -docdir | --docdir | --docdi | --doc | --do)\n    ac_prev=docdir ;;\n  -docdir=* | --docdir=* | --docdi=* | --doc=* | --do=*)\n    docdir=$ac_optarg ;;\n\n  -dvidir | --dvidir | --dvidi | --dvid | --dvi | --dv)\n    ac_prev=dvidir ;;\n  -dvidir=* | --dvidir=* | --dvidi=* | --dvid=* | --dvi=* | --dv=*)\n    dvidir=$ac_optarg ;;\n\n  -enable-* | --enable-*)\n    ac_useropt=`expr \"x$ac_option\" : 'x-*enable-\\([^=]*\\)'`\n    # Reject names that are not valid shell variable names.\n    expr \"x$ac_useropt\" : \".*[^-+._$as_cr_alnum]\" >/dev/null &&\n      as_fn_error $? \"invalid feature name: $ac_useropt\"\n    ac_useropt_orig=$ac_useropt\n    ac_useropt=`$as_echo \"$ac_useropt\" | sed 's/[-+.]/_/g'`\n    case $ac_user_opts in\n      *\"\n\"enable_$ac_useropt\"\n\"*) ;;\n      *) ac_unrecognized_opts=\"$ac_unrecognized_opts$ac_unrecognized_sep--enable-$ac_useropt_orig\"\n\t ac_unrecognized_sep=', ';;\n    esac\n    eval enable_$ac_useropt=\\$ac_optarg ;;\n\n  -exec-prefix | --exec_prefix | --exec-prefix | --exec-prefi \\\n  | --exec-pref | --exec-pre | --exec-pr | --exec-p | --exec- \\\n  | --exec | --exe | --ex)\n    ac_prev=exec_prefix ;;\n  -exec-prefix=* | --exec_prefix=* | --exec-prefix=* | --exec-prefi=* \\\n  | --exec-pref=* | --exec-pre=* | --exec-pr=* | --exec-p=* | --exec-=* \\\n  | --exec=* | --exe=* | --ex=*)\n    exec_prefix=$ac_optarg ;;\n\n  -gas | --gas | --ga | --g)\n    # Obsolete; use --with-gas.\n    with_gas=yes ;;\n\n  -help | --help | --hel | --he | -h)\n    ac_init_help=long ;;\n  -help=r* | --help=r* | --hel=r* | --he=r* | -hr*)\n    ac_init_help=recursive ;;\n  -help=s* | --help=s* | --hel=s* | --he=s* | -hs*)\n    ac_init_help=short ;;\n\n  -host | --host | --hos | --ho)\n    ac_prev=host_alias ;;\n  -host=* | --host=* | --hos=* | --ho=*)\n    host_alias=$ac_optarg ;;\n\n  -htmldir | --htmldir | --htmldi | --htmld | --html | --htm | --ht)\n    ac_prev=htmldir ;;\n  -htmldir=* | --htmldir=* | --htmldi=* | --htmld=* | --html=* | --htm=* \\\n  | --ht=*)\n    htmldir=$ac_optarg ;;\n\n  -includedir | --includedir | --includedi | --included | --include \\\n  | --includ | --inclu | --incl | --inc)\n    ac_prev=includedir ;;\n  -includedir=* | --includedir=* | --includedi=* | --included=* | --include=* \\\n  | --includ=* | --inclu=* | --incl=* | --inc=*)\n    includedir=$ac_optarg ;;\n\n  -infodir | --infodir | --infodi | --infod | --info | --inf)\n    ac_prev=infodir ;;\n  -infodir=* | --infodir=* | --infodi=* | --infod=* | --info=* | --inf=*)\n    infodir=$ac_optarg ;;\n\n  -libdir | --libdir | --libdi | --libd)\n    ac_prev=libdir ;;\n  -libdir=* | --libdir=* | --libdi=* | --libd=*)\n    libdir=$ac_optarg ;;\n\n  -libexecdir | --libexecdir | --libexecdi | --libexecd | --libexec \\\n  | --libexe | --libex | --libe)\n    ac_prev=libexecdir ;;\n  -libexecdir=* | --libexecdir=* | --libexecdi=* | --libexecd=* | --libexec=* \\\n  | --libexe=* | --libex=* | --libe=*)\n    libexecdir=$ac_optarg ;;\n\n  -localedir | --localedir | --localedi | --localed | --locale)\n    ac_prev=localedir ;;\n  -localedir=* | --localedir=* | --localedi=* | --localed=* | --locale=*)\n    localedir=$ac_optarg ;;\n\n  -localstatedir | --localstatedir | --localstatedi | --localstated \\\n  | --localstate | --localstat | --localsta | --localst | --locals)\n    ac_prev=localstatedir ;;\n  -localstatedir=* | --localstatedir=* | --localstatedi=* | --localstated=* \\\n  | --localstate=* | --localstat=* | --localsta=* | --localst=* | --locals=*)\n    localstatedir=$ac_optarg ;;\n\n  -mandir | --mandir | --mandi | --mand | --man | --ma | --m)\n    ac_prev=mandir ;;\n  -mandir=* | --mandir=* | --mandi=* | --mand=* | --man=* | --ma=* | --m=*)\n    mandir=$ac_optarg ;;\n\n  -nfp | --nfp | --nf)\n    # Obsolete; use --without-fp.\n    with_fp=no ;;\n\n  -no-create | --no-create | --no-creat | --no-crea | --no-cre \\\n  | --no-cr | --no-c | -n)\n    no_create=yes ;;\n\n  -no-recursion | --no-recursion | --no-recursio | --no-recursi \\\n  | --no-recurs | --no-recur | --no-recu | --no-rec | --no-re | --no-r)\n    no_recursion=yes ;;\n\n  -oldincludedir | --oldincludedir | --oldincludedi | --oldincluded \\\n  | --oldinclude | --oldinclud | --oldinclu | --oldincl | --oldinc \\\n  | --oldin | --oldi | --old | --ol | --o)\n    ac_prev=oldincludedir ;;\n  -oldincludedir=* | --oldincludedir=* | --oldincludedi=* | --oldincluded=* \\\n  | --oldinclude=* | --oldinclud=* | --oldinclu=* | --oldincl=* | --oldinc=* \\\n  | --oldin=* | --oldi=* | --old=* | --ol=* | --o=*)\n    oldincludedir=$ac_optarg ;;\n\n  -prefix | --prefix | --prefi | --pref | --pre | --pr | --p)\n    ac_prev=prefix ;;\n  -prefix=* | --prefix=* | --prefi=* | --pref=* | --pre=* | --pr=* | --p=*)\n    prefix=$ac_optarg ;;\n\n  -program-prefix | --program-prefix | --program-prefi | --program-pref \\\n  | --program-pre | --program-pr | --program-p)\n    ac_prev=program_prefix ;;\n  -program-prefix=* | --program-prefix=* | --program-prefi=* \\\n  | --program-pref=* | --program-pre=* | --program-pr=* | --program-p=*)\n    program_prefix=$ac_optarg ;;\n\n  -program-suffix | --program-suffix | --program-suffi | --program-suff \\\n  | --program-suf | --program-su | --program-s)\n    ac_prev=program_suffix ;;\n  -program-suffix=* | --program-suffix=* | --program-suffi=* \\\n  | --program-suff=* | --program-suf=* | --program-su=* | --program-s=*)\n    program_suffix=$ac_optarg ;;\n\n  -program-transform-name | --program-transform-name \\\n  | --program-transform-nam | --program-transform-na \\\n  | --program-transform-n | --program-transform- \\\n  | --program-transform | --program-transfor \\\n  | --program-transfo | --program-transf \\\n  | --program-trans | --program-tran \\\n  | --progr-tra | --program-tr | --program-t)\n    ac_prev=program_transform_name ;;\n  -program-transform-name=* | --program-transform-name=* \\\n  | --program-transform-nam=* | --program-transform-na=* \\\n  | --program-transform-n=* | --program-transform-=* \\\n  | --program-transform=* | --program-transfor=* \\\n  | --program-transfo=* | --program-transf=* \\\n  | --program-trans=* | --program-tran=* \\\n  | --progr-tra=* | --program-tr=* | --program-t=*)\n    program_transform_name=$ac_optarg ;;\n\n  -pdfdir | --pdfdir | --pdfdi | --pdfd | --pdf | --pd)\n    ac_prev=pdfdir ;;\n  -pdfdir=* | --pdfdir=* | --pdfdi=* | --pdfd=* | --pdf=* | --pd=*)\n    pdfdir=$ac_optarg ;;\n\n  -psdir | --psdir | --psdi | --psd | --ps)\n    ac_prev=psdir ;;\n  -psdir=* | --psdir=* | --psdi=* | --psd=* | --ps=*)\n    psdir=$ac_optarg ;;\n\n  -q | -quiet | --quiet | --quie | --qui | --qu | --q \\\n  | -silent | --silent | --silen | --sile | --sil)\n    silent=yes ;;\n\n  -runstatedir | --runstatedir | --runstatedi | --runstated \\\n  | --runstate | --runstat | --runsta | --runst | --runs \\\n  | --run | --ru | --r)\n    ac_prev=runstatedir ;;\n  -runstatedir=* | --runstatedir=* | --runstatedi=* | --runstated=* \\\n  | --runstate=* | --runstat=* | --runsta=* | --runst=* | --runs=* \\\n  | --run=* | --ru=* | --r=*)\n    runstatedir=$ac_optarg ;;\n\n  -sbindir | --sbindir | --sbindi | --sbind | --sbin | --sbi | --sb)\n    ac_prev=sbindir ;;\n  -sbindir=* | --sbindir=* | --sbindi=* | --sbind=* | --sbin=* \\\n  | --sbi=* | --sb=*)\n    sbindir=$ac_optarg ;;\n\n  -sharedstatedir | --sharedstatedir | --sharedstatedi \\\n  | --sharedstated | --sharedstate | --sharedstat | --sharedsta \\\n  | --sharedst | --shareds | --shared | --share | --shar \\\n  | --sha | --sh)\n    ac_prev=sharedstatedir ;;\n  -sharedstatedir=* | --sharedstatedir=* | --sharedstatedi=* \\\n  | --sharedstated=* | --sharedstate=* | --sharedstat=* | --sharedsta=* \\\n  | --sharedst=* | --shareds=* | --shared=* | --share=* | --shar=* \\\n  | --sha=* | --sh=*)\n    sharedstatedir=$ac_optarg ;;\n\n  -site | --site | --sit)\n    ac_prev=site ;;\n  -site=* | --site=* | --sit=*)\n    site=$ac_optarg ;;\n\n  -srcdir | --srcdir | --srcdi | --srcd | --src | --sr)\n    ac_prev=srcdir ;;\n  -srcdir=* | --srcdir=* | --srcdi=* | --srcd=* | --src=* | --sr=*)\n    srcdir=$ac_optarg ;;\n\n  -sysconfdir | --sysconfdir | --sysconfdi | --sysconfd | --sysconf \\\n  | --syscon | --sysco | --sysc | --sys | --sy)\n    ac_prev=sysconfdir ;;\n  -sysconfdir=* | --sysconfdir=* | --sysconfdi=* | --sysconfd=* | --sysconf=* \\\n  | --syscon=* | --sysco=* | --sysc=* | --sys=* | --sy=*)\n    sysconfdir=$ac_optarg ;;\n\n  -target | --target | --targe | --targ | --tar | --ta | --t)\n    ac_prev=target_alias ;;\n  -target=* | --target=* | --targe=* | --targ=* | --tar=* | --ta=* | --t=*)\n    target_alias=$ac_optarg ;;\n\n  -v | -verbose | --verbose | --verbos | --verbo | --verb)\n    verbose=yes ;;\n\n  -version | --version | --versio | --versi | --vers | -V)\n    ac_init_version=: ;;\n\n  -with-* | --with-*)\n    ac_useropt=`expr \"x$ac_option\" : 'x-*with-\\([^=]*\\)'`\n    # Reject names that are not valid shell variable names.\n    expr \"x$ac_useropt\" : \".*[^-+._$as_cr_alnum]\" >/dev/null &&\n      as_fn_error $? \"invalid package name: $ac_useropt\"\n    ac_useropt_orig=$ac_useropt\n    ac_useropt=`$as_echo \"$ac_useropt\" | sed 's/[-+.]/_/g'`\n    case $ac_user_opts in\n      *\"\n\"with_$ac_useropt\"\n\"*) ;;\n      *) ac_unrecognized_opts=\"$ac_unrecognized_opts$ac_unrecognized_sep--with-$ac_useropt_orig\"\n\t ac_unrecognized_sep=', ';;\n    esac\n    eval with_$ac_useropt=\\$ac_optarg ;;\n\n  -without-* | --without-*)\n    ac_useropt=`expr \"x$ac_option\" : 'x-*without-\\(.*\\)'`\n    # Reject names that are not valid shell variable names.\n    expr \"x$ac_useropt\" : \".*[^-+._$as_cr_alnum]\" >/dev/null &&\n      as_fn_error $? \"invalid package name: $ac_useropt\"\n    ac_useropt_orig=$ac_useropt\n    ac_useropt=`$as_echo \"$ac_useropt\" | sed 's/[-+.]/_/g'`\n    case $ac_user_opts in\n      *\"\n\"with_$ac_useropt\"\n\"*) ;;\n      *) ac_unrecognized_opts=\"$ac_unrecognized_opts$ac_unrecognized_sep--without-$ac_useropt_orig\"\n\t ac_unrecognized_sep=', ';;\n    esac\n    eval with_$ac_useropt=no ;;\n\n  --x)\n    # Obsolete; use --with-x.\n    with_x=yes ;;\n\n  -x-includes | --x-includes | --x-include | --x-includ | --x-inclu \\\n  | --x-incl | --x-inc | --x-in | --x-i)\n    ac_prev=x_includes ;;\n  -x-includes=* | --x-includes=* | --x-include=* | --x-includ=* | --x-inclu=* \\\n  | --x-incl=* | --x-inc=* | --x-in=* | --x-i=*)\n    x_includes=$ac_optarg ;;\n\n  -x-libraries | --x-libraries | --x-librarie | --x-librari \\\n  | --x-librar | --x-libra | --x-libr | --x-lib | --x-li | --x-l)\n    ac_prev=x_libraries ;;\n  -x-libraries=* | --x-libraries=* | --x-librarie=* | --x-librari=* \\\n  | --x-librar=* | --x-libra=* | --x-libr=* | --x-lib=* | --x-li=* | --x-l=*)\n    x_libraries=$ac_optarg ;;\n\n  -*) as_fn_error $? \"unrecognized option: \\`$ac_option'\nTry \\`$0 --help' for more information\"\n    ;;\n\n  *=*)\n    ac_envvar=`expr \"x$ac_option\" : 'x\\([^=]*\\)='`\n    # Reject names that are not valid shell variable names.\n    case $ac_envvar in #(\n      '' | [0-9]* | *[!_$as_cr_alnum]* )\n      as_fn_error $? \"invalid variable name: \\`$ac_envvar'\" ;;\n    esac\n    eval $ac_envvar=\\$ac_optarg\n    export $ac_envvar ;;\n\n  *)\n    # FIXME: should be removed in autoconf 3.0.\n    $as_echo \"$as_me: WARNING: you should use --build, --host, --target\" >&2\n    expr \"x$ac_option\" : \".*[^-._$as_cr_alnum]\" >/dev/null &&\n      $as_echo \"$as_me: WARNING: invalid host type: $ac_option\" >&2\n    : \"${build_alias=$ac_option} ${host_alias=$ac_option} ${target_alias=$ac_option}\"\n    ;;\n\n  esac\ndone\n\nif test -n \"$ac_prev\"; then\n  ac_option=--`echo $ac_prev | sed 's/_/-/g'`\n  as_fn_error $? \"missing argument to $ac_option\"\nfi\n\nif test -n \"$ac_unrecognized_opts\"; then\n  case $enable_option_checking in\n    no) ;;\n    fatal) as_fn_error $? \"unrecognized options: $ac_unrecognized_opts\" ;;\n    *)     $as_echo \"$as_me: WARNING: unrecognized options: $ac_unrecognized_opts\" >&2 ;;\n  esac\nfi\n\n# Check all directory arguments for consistency.\nfor ac_var in\texec_prefix prefix bindir sbindir libexecdir datarootdir \\\n\t\tdatadir sysconfdir sharedstatedir localstatedir includedir \\\n\t\toldincludedir docdir infodir htmldir dvidir pdfdir psdir \\\n\t\tlibdir localedir mandir runstatedir\ndo\n  eval ac_val=\\$$ac_var\n  # Remove trailing slashes.\n  case $ac_val in\n    */ )\n      ac_val=`expr \"X$ac_val\" : 'X\\(.*[^/]\\)' \\| \"X$ac_val\" : 'X\\(.*\\)'`\n      eval $ac_var=\\$ac_val;;\n  esac\n  # Be sure to have absolute directory names.\n  case $ac_val in\n    [\\\\/$]* | ?:[\\\\/]* )  continue;;\n    NONE | '' ) case $ac_var in *prefix ) continue;; esac;;\n  esac\n  as_fn_error $? \"expected an absolute directory name for --$ac_var: $ac_val\"\ndone\n\n# There might be people who depend on the old broken behavior: `$host'\n# used to hold the argument of --host etc.\n# FIXME: To remove some day.\nbuild=$build_alias\nhost=$host_alias\ntarget=$target_alias\n\n# FIXME: To remove some day.\nif test \"x$host_alias\" != x; then\n  if test \"x$build_alias\" = x; then\n    cross_compiling=maybe\n  elif test \"x$build_alias\" != \"x$host_alias\"; then\n    cross_compiling=yes\n  fi\nfi\n\nac_tool_prefix=\ntest -n \"$host_alias\" && ac_tool_prefix=$host_alias-\n\ntest \"$silent\" = yes && exec 6>/dev/null\n\n\nac_pwd=`pwd` && test -n \"$ac_pwd\" &&\nac_ls_di=`ls -di .` &&\nac_pwd_ls_di=`cd \"$ac_pwd\" && ls -di .` ||\n  as_fn_error $? \"working directory cannot be determined\"\ntest \"X$ac_ls_di\" = \"X$ac_pwd_ls_di\" ||\n  as_fn_error $? \"pwd does not report name of working directory\"\n\n\n# Find the source files, if location was not specified.\nif test -z \"$srcdir\"; then\n  ac_srcdir_defaulted=yes\n  # Try the directory containing this script, then the parent directory.\n  ac_confdir=`$as_dirname -- \"$as_myself\" ||\n$as_expr X\"$as_myself\" : 'X\\(.*[^/]\\)//*[^/][^/]*/*$' \\| \\\n\t X\"$as_myself\" : 'X\\(//\\)[^/]' \\| \\\n\t X\"$as_myself\" : 'X\\(//\\)$' \\| \\\n\t X\"$as_myself\" : 'X\\(/\\)' \\| . 2>/dev/null ||\n$as_echo X\"$as_myself\" |\n    sed '/^X\\(.*[^/]\\)\\/\\/*[^/][^/]*\\/*$/{\n\t    s//\\1/\n\t    q\n\t  }\n\t  /^X\\(\\/\\/\\)[^/].*/{\n\t    s//\\1/\n\t    q\n\t  }\n\t  /^X\\(\\/\\/\\)$/{\n\t    s//\\1/\n\t    q\n\t  }\n\t  /^X\\(\\/\\).*/{\n\t    s//\\1/\n\t    q\n\t  }\n\t  s/.*/./; q'`\n  srcdir=$ac_confdir\n  if test ! -r \"$srcdir/$ac_unique_file\"; then\n    srcdir=..\n  fi\nelse\n  ac_srcdir_defaulted=no\nfi\nif test ! -r \"$srcdir/$ac_unique_file\"; then\n  test \"$ac_srcdir_defaulted\" = yes && srcdir=\"$ac_confdir or ..\"\n  as_fn_error $? \"cannot find sources ($ac_unique_file) in $srcdir\"\nfi\nac_msg=\"sources are in $srcdir, but \\`cd $srcdir' does not work\"\nac_abs_confdir=`(\n\tcd \"$srcdir\" && test -r \"./$ac_unique_file\" || as_fn_error $? \"$ac_msg\"\n\tpwd)`\n# When building in place, set srcdir=.\nif test \"$ac_abs_confdir\" = \"$ac_pwd\"; then\n  srcdir=.\nfi\n# Remove unnecessary trailing slashes from srcdir.\n# Double slashes in file names in object file debugging info\n# mess up M-x gdb in Emacs.\ncase $srcdir in\n*/) srcdir=`expr \"X$srcdir\" : 'X\\(.*[^/]\\)' \\| \"X$srcdir\" : 'X\\(.*\\)'`;;\nesac\nfor ac_var in $ac_precious_vars; do\n  eval ac_env_${ac_var}_set=\\${${ac_var}+set}\n  eval ac_env_${ac_var}_value=\\$${ac_var}\n  eval ac_cv_env_${ac_var}_set=\\${${ac_var}+set}\n  eval ac_cv_env_${ac_var}_value=\\$${ac_var}\ndone\n\n#\n# Report the --help message.\n#\nif test \"$ac_init_help\" = \"long\"; then\n  # Omit some internal or obsolete options to make the list less imposing.\n  # This message is too long to be a string in the A/UX 3.1 sh.\n  cat <<_ACEOF\n\\`configure' configures this package to adapt to many kinds of systems.\n\nUsage: $0 [OPTION]... [VAR=VALUE]...\n\nTo assign environment variables (e.g., CC, CFLAGS...), specify them as\nVAR=VALUE.  See below for descriptions of some of the useful variables.\n\nDefaults for the options are specified in brackets.\n\nConfiguration:\n  -h, --help              display this help and exit\n      --help=short        display options specific to this package\n      --help=recursive    display the short help of all the included packages\n  -V, --version           display version information and exit\n  -q, --quiet, --silent   do not print \\`checking ...' messages\n      --cache-file=FILE   cache test results in FILE [disabled]\n  -C, --config-cache      alias for \\`--cache-file=config.cache'\n  -n, --no-create         do not create output files\n      --srcdir=DIR        find the sources in DIR [configure dir or \\`..']\n\nInstallation directories:\n  --prefix=PREFIX         install architecture-independent files in PREFIX\n                          [$ac_default_prefix]\n  --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX\n                          [PREFIX]\n\nBy default, \\`make install' will install all the files in\n\\`$ac_default_prefix/bin', \\`$ac_default_prefix/lib' etc.  You can specify\nan installation prefix other than \\`$ac_default_prefix' using \\`--prefix',\nfor instance \\`--prefix=\\$HOME'.\n\nFor better control, use the options below.\n\nFine tuning of the installation directories:\n  --bindir=DIR            user executables [EPREFIX/bin]\n  --sbindir=DIR           system admin executables [EPREFIX/sbin]\n  --libexecdir=DIR        program executables [EPREFIX/libexec]\n  --sysconfdir=DIR        read-only single-machine data [PREFIX/etc]\n  --sharedstatedir=DIR    modifiable architecture-independent data [PREFIX/com]\n  --localstatedir=DIR     modifiable single-machine data [PREFIX/var]\n  --runstatedir=DIR       modifiable per-process data [LOCALSTATEDIR/run]\n  --libdir=DIR            object code libraries [EPREFIX/lib]\n  --includedir=DIR        C header files [PREFIX/include]\n  --oldincludedir=DIR     C header files for non-gcc [/usr/include]\n  --datarootdir=DIR       read-only arch.-independent data root [PREFIX/share]\n  --datadir=DIR           read-only architecture-independent data [DATAROOTDIR]\n  --infodir=DIR           info documentation [DATAROOTDIR/info]\n  --localedir=DIR         locale-dependent data [DATAROOTDIR/locale]\n  --mandir=DIR            man documentation [DATAROOTDIR/man]\n  --docdir=DIR            documentation root [DATAROOTDIR/doc/PACKAGE]\n  --htmldir=DIR           html documentation [DOCDIR]\n  --dvidir=DIR            dvi documentation [DOCDIR]\n  --pdfdir=DIR            pdf documentation [DOCDIR]\n  --psdir=DIR             ps documentation [DOCDIR]\n_ACEOF\n\n  cat <<\\_ACEOF\n\nSystem types:\n  --build=BUILD     configure for building on BUILD [guessed]\n  --host=HOST       cross-compile to build programs to run on HOST [BUILD]\n_ACEOF\nfi\n\nif test -n \"$ac_init_help\"; then\n\n  cat <<\\_ACEOF\n\nOptional Features:\n  --disable-option-checking  ignore unrecognized --enable/--with options\n  --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)\n  --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]\n  --disable-cxx           Disable C++ integration\n  --enable-autogen        Automatically regenerate configure output\n  --enable-documentation  Build documentation\n  --enable-shared         Build shared libaries\n  --enable-static         Build static libaries\n  --enable-debug          Build debugging code\n  --disable-stats         Disable statistics calculation/reporting\n  --enable-experimental-smallocx\n                          Enable experimental smallocx API\n  --enable-prof           Enable allocation profiling\n  --enable-prof-libunwind Use libunwind for backtracing\n  --disable-prof-libgcc   Do not use libgcc for backtracing\n  --disable-prof-gcc      Do not use gcc intrinsics for backtracing\n  --disable-fill          Disable support for junk/zero filling\n  --enable-utrace         Enable utrace(2)-based tracing\n  --enable-xmalloc        Support xmalloc option\n  --disable-cache-oblivious\n                          Disable support for cache-oblivious allocation\n                          alignment\n  --enable-log            Support debug logging\n  --enable-readlinkat     Use readlinkat over readlink\n  --enable-opt-safety-checks\n                          Perform certain low-overhead checks, even in opt\n                          mode\n  --disable-libdl         Do not use libdl\n  --disable-syscall       Disable use of syscall(2)\n  --enable-lazy-lock      Enable lazy locking (only lock when multi-threaded)\n  --disable-zone-allocator\n                          Disable zone allocator for Darwin\n  --disable-initial-exec-tls\n                          Disable the initial-exec tls model\n\nOptional Packages:\n  --with-PACKAGE[=ARG]    use PACKAGE [ARG=yes]\n  --without-PACKAGE       do not use PACKAGE (same as --with-PACKAGE=no)\n  --with-xslroot=<path>   XSL stylesheet root path\n  --with-lg-vaddr=<lg-vaddr>\n                          Number of significant virtual address bits\n  --with-version=<major>.<minor>.<bugfix>-<nrev>-g<gid>\n                          Version string\n  --with-rpath=<rpath>    Colon-separated rpath (ELF systems only)\n  --with-mangling=<map>   Mangle symbols in <map>\n  --with-jemalloc-prefix=<prefix>\n                          Prefix to prepend to all public APIs\n  --without-export        disable exporting jemalloc public APIs\n  --with-private-namespace=<prefix>\n                          Prefix to prepend to all library-private APIs\n  --with-install-suffix=<suffix>\n                          Suffix to append to all installed files\n  --with-malloc-conf=<malloc_conf>\n                          config.malloc_conf options string\n  --with-static-libunwind=<libunwind.a>\n                          Path to static libunwind library; use rather than\n                          dynamically linking\n  --with-lg-quantum=<lg-quantum>\n                          Base 2 log of minimum allocation alignment\n  --with-lg-page=<lg-page>\n                          Base 2 log of system page size\n  --with-lg-hugepage=<lg-hugepage>\n                          Base 2 log of system huge page size\n\nSome influential environment variables:\n  CC          C compiler command\n  CFLAGS      C compiler flags\n  LDFLAGS     linker flags, e.g. -L<lib dir> if you have libraries in a\n              nonstandard directory <lib dir>\n  LIBS        libraries to pass to the linker, e.g. -l<library>\n  CPPFLAGS    (Objective) C/C++ preprocessor flags, e.g. -I<include dir> if\n              you have headers in a nonstandard directory <include dir>\n  CPP         C preprocessor\n  CXX         C++ compiler command\n  CXXFLAGS    C++ compiler flags\n\nUse these variables to override the choices made by `configure' or to help\nit to find libraries and programs with nonstandard names/locations.\n\nReport bugs to the package provider.\n_ACEOF\nac_status=$?\nfi\n\nif test \"$ac_init_help\" = \"recursive\"; then\n  # If there are subdirs, report their specific --help.\n  for ac_dir in : $ac_subdirs_all; do test \"x$ac_dir\" = x: && continue\n    test -d \"$ac_dir\" ||\n      { cd \"$srcdir\" && ac_pwd=`pwd` && srcdir=. && test -d \"$ac_dir\"; } ||\n      continue\n    ac_builddir=.\n\ncase \"$ac_dir\" in\n.) ac_dir_suffix= ac_top_builddir_sub=. ac_top_build_prefix= ;;\n*)\n  ac_dir_suffix=/`$as_echo \"$ac_dir\" | sed 's|^\\.[\\\\/]||'`\n  # A \"..\" for each directory in $ac_dir_suffix.\n  ac_top_builddir_sub=`$as_echo \"$ac_dir_suffix\" | sed 's|/[^\\\\/]*|/..|g;s|/||'`\n  case $ac_top_builddir_sub in\n  \"\") ac_top_builddir_sub=. ac_top_build_prefix= ;;\n  *)  ac_top_build_prefix=$ac_top_builddir_sub/ ;;\n  esac ;;\nesac\nac_abs_top_builddir=$ac_pwd\nac_abs_builddir=$ac_pwd$ac_dir_suffix\n# for backward compatibility:\nac_top_builddir=$ac_top_build_prefix\n\ncase $srcdir in\n  .)  # We are building in place.\n    ac_srcdir=.\n    ac_top_srcdir=$ac_top_builddir_sub\n    ac_abs_top_srcdir=$ac_pwd ;;\n  [\\\\/]* | ?:[\\\\/]* )  # Absolute name.\n    ac_srcdir=$srcdir$ac_dir_suffix;\n    ac_top_srcdir=$srcdir\n    ac_abs_top_srcdir=$srcdir ;;\n  *) # Relative name.\n    ac_srcdir=$ac_top_build_prefix$srcdir$ac_dir_suffix\n    ac_top_srcdir=$ac_top_build_prefix$srcdir\n    ac_abs_top_srcdir=$ac_pwd/$srcdir ;;\nesac\nac_abs_srcdir=$ac_abs_top_srcdir$ac_dir_suffix\n\n    cd \"$ac_dir\" || { ac_status=$?; continue; }\n    # Check for guested configure.\n    if test -f \"$ac_srcdir/configure.gnu\"; then\n      echo &&\n      $SHELL \"$ac_srcdir/configure.gnu\" --help=recursive\n    elif test -f \"$ac_srcdir/configure\"; then\n      echo &&\n      $SHELL \"$ac_srcdir/configure\" --help=recursive\n    else\n      $as_echo \"$as_me: WARNING: no configuration information is in $ac_dir\" >&2\n    fi || ac_status=$?\n    cd \"$ac_pwd\" || { ac_status=$?; break; }\n  done\nfi\n\ntest -n \"$ac_init_help\" && exit $ac_status\nif $ac_init_version; then\n  cat <<\\_ACEOF\nconfigure\ngenerated by GNU Autoconf 2.69\n\nCopyright (C) 2012 Free Software Foundation, Inc.\nThis configure script is free software; the Free Software Foundation\ngives unlimited permission to copy, distribute and modify it.\n_ACEOF\n  exit\nfi\n\n## ------------------------ ##\n## Autoconf initialization. ##\n## ------------------------ ##\n\n# ac_fn_c_try_compile LINENO\n# --------------------------\n# Try to compile conftest.$ac_ext, and return whether this succeeded.\nac_fn_c_try_compile ()\n{\n  as_lineno=${as_lineno-\"$1\"} as_lineno_stack=as_lineno_stack=$as_lineno_stack\n  rm -f conftest.$ac_objext\n  if { { ac_try=\"$ac_compile\"\ncase \"(($ac_try\" in\n  *\\\"* | *\\`* | *\\\\*) ac_try_echo=\\$ac_try;;\n  *) ac_try_echo=$ac_try;;\nesac\neval ac_try_echo=\"\\\"\\$as_me:${as_lineno-$LINENO}: $ac_try_echo\\\"\"\n$as_echo \"$ac_try_echo\"; } >&5\n  (eval \"$ac_compile\") 2>conftest.err\n  ac_status=$?\n  if test -s conftest.err; then\n    grep -v '^ *+' conftest.err >conftest.er1\n    cat conftest.er1 >&5\n    mv -f conftest.er1 conftest.err\n  fi\n  $as_echo \"$as_me:${as_lineno-$LINENO}: \\$? = $ac_status\" >&5\n  test $ac_status = 0; } && {\n\t test -z \"$ac_c_werror_flag\" ||\n\t test ! -s conftest.err\n       } && test -s conftest.$ac_objext; then :\n  ac_retval=0\nelse\n  $as_echo \"$as_me: failed program was:\" >&5\nsed 's/^/| /' conftest.$ac_ext >&5\n\n\tac_retval=1\nfi\n  eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno\n  as_fn_set_status $ac_retval\n\n} # ac_fn_c_try_compile\n\n# ac_fn_c_try_cpp LINENO\n# ----------------------\n# Try to preprocess conftest.$ac_ext, and return whether this succeeded.\nac_fn_c_try_cpp ()\n{\n  as_lineno=${as_lineno-\"$1\"} as_lineno_stack=as_lineno_stack=$as_lineno_stack\n  if { { ac_try=\"$ac_cpp conftest.$ac_ext\"\ncase \"(($ac_try\" in\n  *\\\"* | *\\`* | *\\\\*) ac_try_echo=\\$ac_try;;\n  *) ac_try_echo=$ac_try;;\nesac\neval ac_try_echo=\"\\\"\\$as_me:${as_lineno-$LINENO}: $ac_try_echo\\\"\"\n$as_echo \"$ac_try_echo\"; } >&5\n  (eval \"$ac_cpp conftest.$ac_ext\") 2>conftest.err\n  ac_status=$?\n  if test -s conftest.err; then\n    grep -v '^ *+' conftest.err >conftest.er1\n    cat conftest.er1 >&5\n    mv -f conftest.er1 conftest.err\n  fi\n  $as_echo \"$as_me:${as_lineno-$LINENO}: \\$? = $ac_status\" >&5\n  test $ac_status = 0; } > conftest.i && {\n\t test -z \"$ac_c_preproc_warn_flag$ac_c_werror_flag\" ||\n\t test ! -s conftest.err\n       }; then :\n  ac_retval=0\nelse\n  $as_echo \"$as_me: failed program was:\" >&5\nsed 's/^/| /' conftest.$ac_ext >&5\n\n    ac_retval=1\nfi\n  eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno\n  as_fn_set_status $ac_retval\n\n} # ac_fn_c_try_cpp\n\n# ac_fn_cxx_try_compile LINENO\n# ----------------------------\n# Try to compile conftest.$ac_ext, and return whether this succeeded.\nac_fn_cxx_try_compile ()\n{\n  as_lineno=${as_lineno-\"$1\"} as_lineno_stack=as_lineno_stack=$as_lineno_stack\n  rm -f conftest.$ac_objext\n  if { { ac_try=\"$ac_compile\"\ncase \"(($ac_try\" in\n  *\\\"* | *\\`* | *\\\\*) ac_try_echo=\\$ac_try;;\n  *) ac_try_echo=$ac_try;;\nesac\neval ac_try_echo=\"\\\"\\$as_me:${as_lineno-$LINENO}: $ac_try_echo\\\"\"\n$as_echo \"$ac_try_echo\"; } >&5\n  (eval \"$ac_compile\") 2>conftest.err\n  ac_status=$?\n  if test -s conftest.err; then\n    grep -v '^ *+' conftest.err >conftest.er1\n    cat conftest.er1 >&5\n    mv -f conftest.er1 conftest.err\n  fi\n  $as_echo \"$as_me:${as_lineno-$LINENO}: \\$? = $ac_status\" >&5\n  test $ac_status = 0; } && {\n\t test -z \"$ac_cxx_werror_flag\" ||\n\t test ! -s conftest.err\n       } && test -s conftest.$ac_objext; then :\n  ac_retval=0\nelse\n  $as_echo \"$as_me: failed program was:\" >&5\nsed 's/^/| /' conftest.$ac_ext >&5\n\n\tac_retval=1\nfi\n  eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno\n  as_fn_set_status $ac_retval\n\n} # ac_fn_cxx_try_compile\n\n# ac_fn_c_try_link LINENO\n# -----------------------\n# Try to link conftest.$ac_ext, and return whether this succeeded.\nac_fn_c_try_link ()\n{\n  as_lineno=${as_lineno-\"$1\"} as_lineno_stack=as_lineno_stack=$as_lineno_stack\n  rm -f conftest.$ac_objext conftest$ac_exeext\n  if { { ac_try=\"$ac_link\"\ncase \"(($ac_try\" in\n  *\\\"* | *\\`* | *\\\\*) ac_try_echo=\\$ac_try;;\n  *) ac_try_echo=$ac_try;;\nesac\neval ac_try_echo=\"\\\"\\$as_me:${as_lineno-$LINENO}: $ac_try_echo\\\"\"\n$as_echo \"$ac_try_echo\"; } >&5\n  (eval \"$ac_link\") 2>conftest.err\n  ac_status=$?\n  if test -s conftest.err; then\n    grep -v '^ *+' conftest.err >conftest.er1\n    cat conftest.er1 >&5\n    mv -f conftest.er1 conftest.err\n  fi\n  $as_echo \"$as_me:${as_lineno-$LINENO}: \\$? = $ac_status\" >&5\n  test $ac_status = 0; } && {\n\t test -z \"$ac_c_werror_flag\" ||\n\t test ! -s conftest.err\n       } && test -s conftest$ac_exeext && {\n\t test \"$cross_compiling\" = yes ||\n\t test -x conftest$ac_exeext\n       }; then :\n  ac_retval=0\nelse\n  $as_echo \"$as_me: failed program was:\" >&5\nsed 's/^/| /' conftest.$ac_ext >&5\n\n\tac_retval=1\nfi\n  # Delete the IPA/IPO (Inter Procedural Analysis/Optimization) information\n  # created by the PGI compiler (conftest_ipa8_conftest.oo), as it would\n  # interfere with the next link command; also delete a directory that is\n  # left behind by Apple's compiler.  We do this before executing the actions.\n  rm -rf conftest.dSYM conftest_ipa8_conftest.oo\n  eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno\n  as_fn_set_status $ac_retval\n\n} # ac_fn_c_try_link\n\n# ac_fn_c_try_run LINENO\n# ----------------------\n# Try to link conftest.$ac_ext, and return whether this succeeded. Assumes\n# that executables *can* be run.\nac_fn_c_try_run ()\n{\n  as_lineno=${as_lineno-\"$1\"} as_lineno_stack=as_lineno_stack=$as_lineno_stack\n  if { { ac_try=\"$ac_link\"\ncase \"(($ac_try\" in\n  *\\\"* | *\\`* | *\\\\*) ac_try_echo=\\$ac_try;;\n  *) ac_try_echo=$ac_try;;\nesac\neval ac_try_echo=\"\\\"\\$as_me:${as_lineno-$LINENO}: $ac_try_echo\\\"\"\n$as_echo \"$ac_try_echo\"; } >&5\n  (eval \"$ac_link\") 2>&5\n  ac_status=$?\n  $as_echo \"$as_me:${as_lineno-$LINENO}: \\$? = $ac_status\" >&5\n  test $ac_status = 0; } && { ac_try='./conftest$ac_exeext'\n  { { case \"(($ac_try\" in\n  *\\\"* | *\\`* | *\\\\*) ac_try_echo=\\$ac_try;;\n  *) ac_try_echo=$ac_try;;\nesac\neval ac_try_echo=\"\\\"\\$as_me:${as_lineno-$LINENO}: $ac_try_echo\\\"\"\n$as_echo \"$ac_try_echo\"; } >&5\n  (eval \"$ac_try\") 2>&5\n  ac_status=$?\n  $as_echo \"$as_me:${as_lineno-$LINENO}: \\$? = $ac_status\" >&5\n  test $ac_status = 0; }; }; then :\n  ac_retval=0\nelse\n  $as_echo \"$as_me: program exited with status $ac_status\" >&5\n       $as_echo \"$as_me: failed program was:\" >&5\nsed 's/^/| /' conftest.$ac_ext >&5\n\n       ac_retval=$ac_status\nfi\n  rm -rf conftest.dSYM conftest_ipa8_conftest.oo\n  eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno\n  as_fn_set_status $ac_retval\n\n} # ac_fn_c_try_run\n\n# ac_fn_c_check_header_compile LINENO HEADER VAR INCLUDES\n# -------------------------------------------------------\n# Tests whether HEADER exists and can be compiled using the include files in\n# INCLUDES, setting the cache variable VAR accordingly.\nac_fn_c_check_header_compile ()\n{\n  as_lineno=${as_lineno-\"$1\"} as_lineno_stack=as_lineno_stack=$as_lineno_stack\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: checking for $2\" >&5\n$as_echo_n \"checking for $2... \" >&6; }\nif eval \\${$3+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n$4\n#include <$2>\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  eval \"$3=yes\"\nelse\n  eval \"$3=no\"\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nfi\neval ac_res=\\$$3\n\t       { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_res\" >&5\n$as_echo \"$ac_res\" >&6; }\n  eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno\n\n} # ac_fn_c_check_header_compile\n\n# ac_fn_c_compute_int LINENO EXPR VAR INCLUDES\n# --------------------------------------------\n# Tries to find the compile-time value of EXPR in a program that includes\n# INCLUDES, setting VAR accordingly. Returns whether the value could be\n# computed\nac_fn_c_compute_int ()\n{\n  as_lineno=${as_lineno-\"$1\"} as_lineno_stack=as_lineno_stack=$as_lineno_stack\n  if test \"$cross_compiling\" = yes; then\n    # Depending upon the size, compute the lo and hi bounds.\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n$4\nint\nmain ()\n{\nstatic int test_array [1 - 2 * !(($2) >= 0)];\ntest_array [0] = 0;\nreturn test_array [0];\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  ac_lo=0 ac_mid=0\n  while :; do\n    cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n$4\nint\nmain ()\n{\nstatic int test_array [1 - 2 * !(($2) <= $ac_mid)];\ntest_array [0] = 0;\nreturn test_array [0];\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  ac_hi=$ac_mid; break\nelse\n  as_fn_arith $ac_mid + 1 && ac_lo=$as_val\n\t\t\tif test $ac_lo -le $ac_mid; then\n\t\t\t  ac_lo= ac_hi=\n\t\t\t  break\n\t\t\tfi\n\t\t\tas_fn_arith 2 '*' $ac_mid + 1 && ac_mid=$as_val\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\n  done\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n$4\nint\nmain ()\n{\nstatic int test_array [1 - 2 * !(($2) < 0)];\ntest_array [0] = 0;\nreturn test_array [0];\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  ac_hi=-1 ac_mid=-1\n  while :; do\n    cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n$4\nint\nmain ()\n{\nstatic int test_array [1 - 2 * !(($2) >= $ac_mid)];\ntest_array [0] = 0;\nreturn test_array [0];\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  ac_lo=$ac_mid; break\nelse\n  as_fn_arith '(' $ac_mid ')' - 1 && ac_hi=$as_val\n\t\t\tif test $ac_mid -le $ac_hi; then\n\t\t\t  ac_lo= ac_hi=\n\t\t\t  break\n\t\t\tfi\n\t\t\tas_fn_arith 2 '*' $ac_mid && ac_mid=$as_val\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\n  done\nelse\n  ac_lo= ac_hi=\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\n# Binary search between lo and hi bounds.\nwhile test \"x$ac_lo\" != \"x$ac_hi\"; do\n  as_fn_arith '(' $ac_hi - $ac_lo ')' / 2 + $ac_lo && ac_mid=$as_val\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n$4\nint\nmain ()\n{\nstatic int test_array [1 - 2 * !(($2) <= $ac_mid)];\ntest_array [0] = 0;\nreturn test_array [0];\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  ac_hi=$ac_mid\nelse\n  as_fn_arith '(' $ac_mid ')' + 1 && ac_lo=$as_val\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\ndone\ncase $ac_lo in #((\n?*) eval \"$3=\\$ac_lo\"; ac_retval=0 ;;\n'') ac_retval=1 ;;\nesac\n  else\n    cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n$4\nstatic long int longval () { return $2; }\nstatic unsigned long int ulongval () { return $2; }\n#include <stdio.h>\n#include <stdlib.h>\nint\nmain ()\n{\n\n  FILE *f = fopen (\"conftest.val\", \"w\");\n  if (! f)\n    return 1;\n  if (($2) < 0)\n    {\n      long int i = longval ();\n      if (i != ($2))\n\treturn 1;\n      fprintf (f, \"%ld\", i);\n    }\n  else\n    {\n      unsigned long int i = ulongval ();\n      if (i != ($2))\n\treturn 1;\n      fprintf (f, \"%lu\", i);\n    }\n  /* Do not output a trailing newline, as this causes \\r\\n confusion\n     on some platforms.  */\n  return ferror (f) || fclose (f) != 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_run \"$LINENO\"; then :\n  echo >>conftest.val; read $3 <conftest.val; ac_retval=0\nelse\n  ac_retval=1\nfi\nrm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext \\\n  conftest.$ac_objext conftest.beam conftest.$ac_ext\nrm -f conftest.val\n\n  fi\n  eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno\n  as_fn_set_status $ac_retval\n\n} # ac_fn_c_compute_int\n\n# ac_fn_c_check_header_mongrel LINENO HEADER VAR INCLUDES\n# -------------------------------------------------------\n# Tests whether HEADER exists, giving a warning if it cannot be compiled using\n# the include files in INCLUDES and setting the cache variable VAR\n# accordingly.\nac_fn_c_check_header_mongrel ()\n{\n  as_lineno=${as_lineno-\"$1\"} as_lineno_stack=as_lineno_stack=$as_lineno_stack\n  if eval \\${$3+:} false; then :\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: checking for $2\" >&5\n$as_echo_n \"checking for $2... \" >&6; }\nif eval \\${$3+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nfi\neval ac_res=\\$$3\n\t       { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_res\" >&5\n$as_echo \"$ac_res\" >&6; }\nelse\n  # Is the header compilable?\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking $2 usability\" >&5\n$as_echo_n \"checking $2 usability... \" >&6; }\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n$4\n#include <$2>\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  ac_header_compiler=yes\nelse\n  ac_header_compiler=no\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_header_compiler\" >&5\n$as_echo \"$ac_header_compiler\" >&6; }\n\n# Is the header present?\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking $2 presence\" >&5\n$as_echo_n \"checking $2 presence... \" >&6; }\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n#include <$2>\n_ACEOF\nif ac_fn_c_try_cpp \"$LINENO\"; then :\n  ac_header_preproc=yes\nelse\n  ac_header_preproc=no\nfi\nrm -f conftest.err conftest.i conftest.$ac_ext\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_header_preproc\" >&5\n$as_echo \"$ac_header_preproc\" >&6; }\n\n# So?  What about this header?\ncase $ac_header_compiler:$ac_header_preproc:$ac_c_preproc_warn_flag in #((\n  yes:no: )\n    { $as_echo \"$as_me:${as_lineno-$LINENO}: WARNING: $2: accepted by the compiler, rejected by the preprocessor!\" >&5\n$as_echo \"$as_me: WARNING: $2: accepted by the compiler, rejected by the preprocessor!\" >&2;}\n    { $as_echo \"$as_me:${as_lineno-$LINENO}: WARNING: $2: proceeding with the compiler's result\" >&5\n$as_echo \"$as_me: WARNING: $2: proceeding with the compiler's result\" >&2;}\n    ;;\n  no:yes:* )\n    { $as_echo \"$as_me:${as_lineno-$LINENO}: WARNING: $2: present but cannot be compiled\" >&5\n$as_echo \"$as_me: WARNING: $2: present but cannot be compiled\" >&2;}\n    { $as_echo \"$as_me:${as_lineno-$LINENO}: WARNING: $2:     check for missing prerequisite headers?\" >&5\n$as_echo \"$as_me: WARNING: $2:     check for missing prerequisite headers?\" >&2;}\n    { $as_echo \"$as_me:${as_lineno-$LINENO}: WARNING: $2: see the Autoconf documentation\" >&5\n$as_echo \"$as_me: WARNING: $2: see the Autoconf documentation\" >&2;}\n    { $as_echo \"$as_me:${as_lineno-$LINENO}: WARNING: $2:     section \\\"Present But Cannot Be Compiled\\\"\" >&5\n$as_echo \"$as_me: WARNING: $2:     section \\\"Present But Cannot Be Compiled\\\"\" >&2;}\n    { $as_echo \"$as_me:${as_lineno-$LINENO}: WARNING: $2: proceeding with the compiler's result\" >&5\n$as_echo \"$as_me: WARNING: $2: proceeding with the compiler's result\" >&2;}\n    ;;\nesac\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: checking for $2\" >&5\n$as_echo_n \"checking for $2... \" >&6; }\nif eval \\${$3+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  eval \"$3=\\$ac_header_compiler\"\nfi\neval ac_res=\\$$3\n\t       { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_res\" >&5\n$as_echo \"$ac_res\" >&6; }\nfi\n  eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno\n\n} # ac_fn_c_check_header_mongrel\n\n# ac_fn_c_check_func LINENO FUNC VAR\n# ----------------------------------\n# Tests whether FUNC exists, setting the cache variable VAR accordingly\nac_fn_c_check_func ()\n{\n  as_lineno=${as_lineno-\"$1\"} as_lineno_stack=as_lineno_stack=$as_lineno_stack\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: checking for $2\" >&5\n$as_echo_n \"checking for $2... \" >&6; }\nif eval \\${$3+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n/* Define $2 to an innocuous variant, in case <limits.h> declares $2.\n   For example, HP-UX 11i <limits.h> declares gettimeofday.  */\n#define $2 innocuous_$2\n\n/* System header to define __stub macros and hopefully few prototypes,\n    which can conflict with char $2 (); below.\n    Prefer <limits.h> to <assert.h> if __STDC__ is defined, since\n    <limits.h> exists even on freestanding compilers.  */\n\n#ifdef __STDC__\n# include <limits.h>\n#else\n# include <assert.h>\n#endif\n\n#undef $2\n\n/* Override any GCC internal prototype to avoid an error.\n   Use char because int might match the return type of a GCC\n   builtin and then its argument prototype would still apply.  */\n#ifdef __cplusplus\nextern \"C\"\n#endif\nchar $2 ();\n/* The GNU C library defines this for functions which it implements\n    to always fail with ENOSYS.  Some functions are actually named\n    something starting with __ and the normal name is an alias.  */\n#if defined __stub_$2 || defined __stub___$2\nchoke me\n#endif\n\nint\nmain ()\n{\nreturn $2 ();\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  eval \"$3=yes\"\nelse\n  eval \"$3=no\"\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\neval ac_res=\\$$3\n\t       { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_res\" >&5\n$as_echo \"$ac_res\" >&6; }\n  eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno\n\n} # ac_fn_c_check_func\n\n# ac_fn_c_check_type LINENO TYPE VAR INCLUDES\n# -------------------------------------------\n# Tests whether TYPE exists after having included INCLUDES, setting cache\n# variable VAR accordingly.\nac_fn_c_check_type ()\n{\n  as_lineno=${as_lineno-\"$1\"} as_lineno_stack=as_lineno_stack=$as_lineno_stack\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: checking for $2\" >&5\n$as_echo_n \"checking for $2... \" >&6; }\nif eval \\${$3+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  eval \"$3=no\"\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n$4\nint\nmain ()\n{\nif (sizeof ($2))\n\t return 0;\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n$4\nint\nmain ()\n{\nif (sizeof (($2)))\n\t    return 0;\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n\nelse\n  eval \"$3=yes\"\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nfi\neval ac_res=\\$$3\n\t       { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_res\" >&5\n$as_echo \"$ac_res\" >&6; }\n  eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno\n\n} # ac_fn_c_check_type\ncat >config.log <<_ACEOF\nThis file contains any messages produced by compilers while\nrunning configure, to aid debugging if configure makes a mistake.\n\nIt was created by $as_me, which was\ngenerated by GNU Autoconf 2.69.  Invocation command line was\n\n  $ $0 $@\n\n_ACEOF\nexec 5>>config.log\n{\ncat <<_ASUNAME\n## --------- ##\n## Platform. ##\n## --------- ##\n\nhostname = `(hostname || uname -n) 2>/dev/null | sed 1q`\nuname -m = `(uname -m) 2>/dev/null || echo unknown`\nuname -r = `(uname -r) 2>/dev/null || echo unknown`\nuname -s = `(uname -s) 2>/dev/null || echo unknown`\nuname -v = `(uname -v) 2>/dev/null || echo unknown`\n\n/usr/bin/uname -p = `(/usr/bin/uname -p) 2>/dev/null || echo unknown`\n/bin/uname -X     = `(/bin/uname -X) 2>/dev/null     || echo unknown`\n\n/bin/arch              = `(/bin/arch) 2>/dev/null              || echo unknown`\n/usr/bin/arch -k       = `(/usr/bin/arch -k) 2>/dev/null       || echo unknown`\n/usr/convex/getsysinfo = `(/usr/convex/getsysinfo) 2>/dev/null || echo unknown`\n/usr/bin/hostinfo      = `(/usr/bin/hostinfo) 2>/dev/null      || echo unknown`\n/bin/machine           = `(/bin/machine) 2>/dev/null           || echo unknown`\n/usr/bin/oslevel       = `(/usr/bin/oslevel) 2>/dev/null       || echo unknown`\n/bin/universe          = `(/bin/universe) 2>/dev/null          || echo unknown`\n\n_ASUNAME\n\nas_save_IFS=$IFS; IFS=$PATH_SEPARATOR\nfor as_dir in $PATH\ndo\n  IFS=$as_save_IFS\n  test -z \"$as_dir\" && as_dir=.\n    $as_echo \"PATH: $as_dir\"\n  done\nIFS=$as_save_IFS\n\n} >&5\n\ncat >&5 <<_ACEOF\n\n\n## ----------- ##\n## Core tests. ##\n## ----------- ##\n\n_ACEOF\n\n\n# Keep a trace of the command line.\n# Strip out --no-create and --no-recursion so they do not pile up.\n# Strip out --silent because we don't want to record it for future runs.\n# Also quote any args containing shell meta-characters.\n# Make two passes to allow for proper duplicate-argument suppression.\nac_configure_args=\nac_configure_args0=\nac_configure_args1=\nac_must_keep_next=false\nfor ac_pass in 1 2\ndo\n  for ac_arg\n  do\n    case $ac_arg in\n    -no-create | --no-c* | -n | -no-recursion | --no-r*) continue ;;\n    -q | -quiet | --quiet | --quie | --qui | --qu | --q \\\n    | -silent | --silent | --silen | --sile | --sil)\n      continue ;;\n    *\\'*)\n      ac_arg=`$as_echo \"$ac_arg\" | sed \"s/'/'\\\\\\\\\\\\\\\\''/g\"` ;;\n    esac\n    case $ac_pass in\n    1) as_fn_append ac_configure_args0 \" '$ac_arg'\" ;;\n    2)\n      as_fn_append ac_configure_args1 \" '$ac_arg'\"\n      if test $ac_must_keep_next = true; then\n\tac_must_keep_next=false # Got value, back to normal.\n      else\n\tcase $ac_arg in\n\t  *=* | --config-cache | -C | -disable-* | --disable-* \\\n\t  | -enable-* | --enable-* | -gas | --g* | -nfp | --nf* \\\n\t  | -q | -quiet | --q* | -silent | --sil* | -v | -verb* \\\n\t  | -with-* | --with-* | -without-* | --without-* | --x)\n\t    case \"$ac_configure_args0 \" in\n\t      \"$ac_configure_args1\"*\" '$ac_arg' \"* ) continue ;;\n\t    esac\n\t    ;;\n\t  -* ) ac_must_keep_next=true ;;\n\tesac\n      fi\n      as_fn_append ac_configure_args \" '$ac_arg'\"\n      ;;\n    esac\n  done\ndone\n{ ac_configure_args0=; unset ac_configure_args0;}\n{ ac_configure_args1=; unset ac_configure_args1;}\n\n# When interrupted or exit'd, cleanup temporary files, and complete\n# config.log.  We remove comments because anyway the quotes in there\n# would cause problems or look ugly.\n# WARNING: Use '\\'' to represent an apostrophe within the trap.\n# WARNING: Do not start the trap code with a newline, due to a FreeBSD 4.0 bug.\ntrap 'exit_status=$?\n  # Save into config.log some information that might help in debugging.\n  {\n    echo\n\n    $as_echo \"## ---------------- ##\n## Cache variables. ##\n## ---------------- ##\"\n    echo\n    # The following way of writing the cache mishandles newlines in values,\n(\n  for ac_var in `(set) 2>&1 | sed -n '\\''s/^\\([a-zA-Z_][a-zA-Z0-9_]*\\)=.*/\\1/p'\\''`; do\n    eval ac_val=\\$$ac_var\n    case $ac_val in #(\n    *${as_nl}*)\n      case $ac_var in #(\n      *_cv_*) { $as_echo \"$as_me:${as_lineno-$LINENO}: WARNING: cache variable $ac_var contains a newline\" >&5\n$as_echo \"$as_me: WARNING: cache variable $ac_var contains a newline\" >&2;} ;;\n      esac\n      case $ac_var in #(\n      _ | IFS | as_nl) ;; #(\n      BASH_ARGV | BASH_SOURCE) eval $ac_var= ;; #(\n      *) { eval $ac_var=; unset $ac_var;} ;;\n      esac ;;\n    esac\n  done\n  (set) 2>&1 |\n    case $as_nl`(ac_space='\\'' '\\''; set) 2>&1` in #(\n    *${as_nl}ac_space=\\ *)\n      sed -n \\\n\t\"s/'\\''/'\\''\\\\\\\\'\\'''\\''/g;\n\t  s/^\\\\([_$as_cr_alnum]*_cv_[_$as_cr_alnum]*\\\\)=\\\\(.*\\\\)/\\\\1='\\''\\\\2'\\''/p\"\n      ;; #(\n    *)\n      sed -n \"/^[_$as_cr_alnum]*_cv_[_$as_cr_alnum]*=/p\"\n      ;;\n    esac |\n    sort\n)\n    echo\n\n    $as_echo \"## ----------------- ##\n## Output variables. ##\n## ----------------- ##\"\n    echo\n    for ac_var in $ac_subst_vars\n    do\n      eval ac_val=\\$$ac_var\n      case $ac_val in\n      *\\'\\''*) ac_val=`$as_echo \"$ac_val\" | sed \"s/'\\''/'\\''\\\\\\\\\\\\\\\\'\\'''\\''/g\"`;;\n      esac\n      $as_echo \"$ac_var='\\''$ac_val'\\''\"\n    done | sort\n    echo\n\n    if test -n \"$ac_subst_files\"; then\n      $as_echo \"## ------------------- ##\n## File substitutions. ##\n## ------------------- ##\"\n      echo\n      for ac_var in $ac_subst_files\n      do\n\teval ac_val=\\$$ac_var\n\tcase $ac_val in\n\t*\\'\\''*) ac_val=`$as_echo \"$ac_val\" | sed \"s/'\\''/'\\''\\\\\\\\\\\\\\\\'\\'''\\''/g\"`;;\n\tesac\n\t$as_echo \"$ac_var='\\''$ac_val'\\''\"\n      done | sort\n      echo\n    fi\n\n    if test -s confdefs.h; then\n      $as_echo \"## ----------- ##\n## confdefs.h. ##\n## ----------- ##\"\n      echo\n      cat confdefs.h\n      echo\n    fi\n    test \"$ac_signal\" != 0 &&\n      $as_echo \"$as_me: caught signal $ac_signal\"\n    $as_echo \"$as_me: exit $exit_status\"\n  } >&5\n  rm -f core *.core core.conftest.* &&\n    rm -f -r conftest* confdefs* conf$$* $ac_clean_files &&\n    exit $exit_status\n' 0\nfor ac_signal in 1 2 13 15; do\n  trap 'ac_signal='$ac_signal'; as_fn_exit 1' $ac_signal\ndone\nac_signal=0\n\n# confdefs.h avoids OS command line length limits that DEFS can exceed.\nrm -f -r conftest* confdefs.h\n\n$as_echo \"/* confdefs.h */\" > confdefs.h\n\n# Predefined preprocessor variables.\n\ncat >>confdefs.h <<_ACEOF\n#define PACKAGE_NAME \"$PACKAGE_NAME\"\n_ACEOF\n\ncat >>confdefs.h <<_ACEOF\n#define PACKAGE_TARNAME \"$PACKAGE_TARNAME\"\n_ACEOF\n\ncat >>confdefs.h <<_ACEOF\n#define PACKAGE_VERSION \"$PACKAGE_VERSION\"\n_ACEOF\n\ncat >>confdefs.h <<_ACEOF\n#define PACKAGE_STRING \"$PACKAGE_STRING\"\n_ACEOF\n\ncat >>confdefs.h <<_ACEOF\n#define PACKAGE_BUGREPORT \"$PACKAGE_BUGREPORT\"\n_ACEOF\n\ncat >>confdefs.h <<_ACEOF\n#define PACKAGE_URL \"$PACKAGE_URL\"\n_ACEOF\n\n\n# Let the site file select an alternate cache file if it wants to.\n# Prefer an explicitly selected file to automatically selected ones.\nac_site_file1=NONE\nac_site_file2=NONE\nif test -n \"$CONFIG_SITE\"; then\n  # We do not want a PATH search for config.site.\n  case $CONFIG_SITE in #((\n    -*)  ac_site_file1=./$CONFIG_SITE;;\n    */*) ac_site_file1=$CONFIG_SITE;;\n    *)   ac_site_file1=./$CONFIG_SITE;;\n  esac\nelif test \"x$prefix\" != xNONE; then\n  ac_site_file1=$prefix/share/config.site\n  ac_site_file2=$prefix/etc/config.site\nelse\n  ac_site_file1=$ac_default_prefix/share/config.site\n  ac_site_file2=$ac_default_prefix/etc/config.site\nfi\nfor ac_site_file in \"$ac_site_file1\" \"$ac_site_file2\"\ndo\n  test \"x$ac_site_file\" = xNONE && continue\n  if test /dev/null != \"$ac_site_file\" && test -r \"$ac_site_file\"; then\n    { $as_echo \"$as_me:${as_lineno-$LINENO}: loading site script $ac_site_file\" >&5\n$as_echo \"$as_me: loading site script $ac_site_file\" >&6;}\n    sed 's/^/| /' \"$ac_site_file\" >&5\n    . \"$ac_site_file\" \\\n      || { { $as_echo \"$as_me:${as_lineno-$LINENO}: error: in \\`$ac_pwd':\" >&5\n$as_echo \"$as_me: error: in \\`$ac_pwd':\" >&2;}\nas_fn_error $? \"failed to load site script $ac_site_file\nSee \\`config.log' for more details\" \"$LINENO\" 5; }\n  fi\ndone\n\nif test -r \"$cache_file\"; then\n  # Some versions of bash will fail to source /dev/null (special files\n  # actually), so we avoid doing that.  DJGPP emulates it as a regular file.\n  if test /dev/null != \"$cache_file\" && test -f \"$cache_file\"; then\n    { $as_echo \"$as_me:${as_lineno-$LINENO}: loading cache $cache_file\" >&5\n$as_echo \"$as_me: loading cache $cache_file\" >&6;}\n    case $cache_file in\n      [\\\\/]* | ?:[\\\\/]* ) . \"$cache_file\";;\n      *)                      . \"./$cache_file\";;\n    esac\n  fi\nelse\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: creating cache $cache_file\" >&5\n$as_echo \"$as_me: creating cache $cache_file\" >&6;}\n  >$cache_file\nfi\n\n# Check that the precious variables saved in the cache have kept the same\n# value.\nac_cache_corrupted=false\nfor ac_var in $ac_precious_vars; do\n  eval ac_old_set=\\$ac_cv_env_${ac_var}_set\n  eval ac_new_set=\\$ac_env_${ac_var}_set\n  eval ac_old_val=\\$ac_cv_env_${ac_var}_value\n  eval ac_new_val=\\$ac_env_${ac_var}_value\n  case $ac_old_set,$ac_new_set in\n    set,)\n      { $as_echo \"$as_me:${as_lineno-$LINENO}: error: \\`$ac_var' was set to \\`$ac_old_val' in the previous run\" >&5\n$as_echo \"$as_me: error: \\`$ac_var' was set to \\`$ac_old_val' in the previous run\" >&2;}\n      ac_cache_corrupted=: ;;\n    ,set)\n      { $as_echo \"$as_me:${as_lineno-$LINENO}: error: \\`$ac_var' was not set in the previous run\" >&5\n$as_echo \"$as_me: error: \\`$ac_var' was not set in the previous run\" >&2;}\n      ac_cache_corrupted=: ;;\n    ,);;\n    *)\n      if test \"x$ac_old_val\" != \"x$ac_new_val\"; then\n\t# differences in whitespace do not lead to failure.\n\tac_old_val_w=`echo x $ac_old_val`\n\tac_new_val_w=`echo x $ac_new_val`\n\tif test \"$ac_old_val_w\" != \"$ac_new_val_w\"; then\n\t  { $as_echo \"$as_me:${as_lineno-$LINENO}: error: \\`$ac_var' has changed since the previous run:\" >&5\n$as_echo \"$as_me: error: \\`$ac_var' has changed since the previous run:\" >&2;}\n\t  ac_cache_corrupted=:\n\telse\n\t  { $as_echo \"$as_me:${as_lineno-$LINENO}: warning: ignoring whitespace changes in \\`$ac_var' since the previous run:\" >&5\n$as_echo \"$as_me: warning: ignoring whitespace changes in \\`$ac_var' since the previous run:\" >&2;}\n\t  eval $ac_var=\\$ac_old_val\n\tfi\n\t{ $as_echo \"$as_me:${as_lineno-$LINENO}:   former value:  \\`$ac_old_val'\" >&5\n$as_echo \"$as_me:   former value:  \\`$ac_old_val'\" >&2;}\n\t{ $as_echo \"$as_me:${as_lineno-$LINENO}:   current value: \\`$ac_new_val'\" >&5\n$as_echo \"$as_me:   current value: \\`$ac_new_val'\" >&2;}\n      fi;;\n  esac\n  # Pass precious variables to config.status.\n  if test \"$ac_new_set\" = set; then\n    case $ac_new_val in\n    *\\'*) ac_arg=$ac_var=`$as_echo \"$ac_new_val\" | sed \"s/'/'\\\\\\\\\\\\\\\\''/g\"` ;;\n    *) ac_arg=$ac_var=$ac_new_val ;;\n    esac\n    case \" $ac_configure_args \" in\n      *\" '$ac_arg' \"*) ;; # Avoid dups.  Use of quotes ensures accuracy.\n      *) as_fn_append ac_configure_args \" '$ac_arg'\" ;;\n    esac\n  fi\ndone\nif $ac_cache_corrupted; then\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: error: in \\`$ac_pwd':\" >&5\n$as_echo \"$as_me: error: in \\`$ac_pwd':\" >&2;}\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: error: changes in the environment can compromise the build\" >&5\n$as_echo \"$as_me: error: changes in the environment can compromise the build\" >&2;}\n  as_fn_error $? \"run \\`make distclean' and/or \\`rm $cache_file' and start over\" \"$LINENO\" 5\nfi\n## -------------------- ##\n## Main body of script. ##\n## -------------------- ##\n\nac_ext=c\nac_cpp='$CPP $CPPFLAGS'\nac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'\nac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'\nac_compiler_gnu=$ac_cv_c_compiler_gnu\n\n\n\nac_aux_dir=\nfor ac_dir in build-aux \"$srcdir\"/build-aux; do\n  if test -f \"$ac_dir/install-sh\"; then\n    ac_aux_dir=$ac_dir\n    ac_install_sh=\"$ac_aux_dir/install-sh -c\"\n    break\n  elif test -f \"$ac_dir/install.sh\"; then\n    ac_aux_dir=$ac_dir\n    ac_install_sh=\"$ac_aux_dir/install.sh -c\"\n    break\n  elif test -f \"$ac_dir/shtool\"; then\n    ac_aux_dir=$ac_dir\n    ac_install_sh=\"$ac_aux_dir/shtool install -c\"\n    break\n  fi\ndone\nif test -z \"$ac_aux_dir\"; then\n  as_fn_error $? \"cannot find install-sh, install.sh, or shtool in build-aux \\\"$srcdir\\\"/build-aux\" \"$LINENO\" 5\nfi\n\n# These three variables are undocumented and unsupported,\n# and are intended to be withdrawn in a future Autoconf release.\n# They can cause serious problems if a builder's source tree is in a directory\n# whose full name contains unusual characters.\nac_config_guess=\"$SHELL $ac_aux_dir/config.guess\"  # Please don't use this var.\nac_config_sub=\"$SHELL $ac_aux_dir/config.sub\"  # Please don't use this var.\nac_configure=\"$SHELL $ac_aux_dir/configure\"  # Please don't use this var.\n\n\n\n\n\n\n\n\nCONFIGURE_CFLAGS=\nSPECIFIED_CFLAGS=\"${CFLAGS}\"\n\n\n\n\n\nCONFIGURE_CXXFLAGS=\nSPECIFIED_CXXFLAGS=\"${CXXFLAGS}\"\n\n\n\n\n\nCONFIG=`echo ${ac_configure_args} | sed -e 's#'\"'\"'\\([^ ]*\\)'\"'\"'#\\1#g'`\n\n\nrev=2\n\n\nsrcroot=$srcdir\nif test \"x${srcroot}\" = \"x.\" ; then\n  srcroot=\"\"\nelse\n  srcroot=\"${srcroot}/\"\nfi\n\nabs_srcroot=\"`cd \\\"${srcdir}\\\"; pwd`/\"\n\n\nobjroot=\"\"\n\nabs_objroot=\"`pwd`/\"\n\n\nif test \"x$prefix\" = \"xNONE\" ; then\n  prefix=\"/usr/local\"\nfi\nif test \"x$exec_prefix\" = \"xNONE\" ; then\n  exec_prefix=$prefix\nfi\nPREFIX=$prefix\n\nBINDIR=`eval echo $bindir`\nBINDIR=`eval echo $BINDIR`\n\nINCLUDEDIR=`eval echo $includedir`\nINCLUDEDIR=`eval echo $INCLUDEDIR`\n\nLIBDIR=`eval echo $libdir`\nLIBDIR=`eval echo $LIBDIR`\n\nDATADIR=`eval echo $datadir`\nDATADIR=`eval echo $DATADIR`\n\nMANDIR=`eval echo $mandir`\nMANDIR=`eval echo $MANDIR`\n\n\n# Extract the first word of \"xsltproc\", so it can be a program name with args.\nset dummy xsltproc; ac_word=$2\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for $ac_word\" >&5\n$as_echo_n \"checking for $ac_word... \" >&6; }\nif ${ac_cv_path_XSLTPROC+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  case $XSLTPROC in\n  [\\\\/]* | ?:[\\\\/]*)\n  ac_cv_path_XSLTPROC=\"$XSLTPROC\" # Let the user override the test with a path.\n  ;;\n  *)\n  as_save_IFS=$IFS; IFS=$PATH_SEPARATOR\nfor as_dir in $PATH\ndo\n  IFS=$as_save_IFS\n  test -z \"$as_dir\" && as_dir=.\n    for ac_exec_ext in '' $ac_executable_extensions; do\n  if as_fn_executable_p \"$as_dir/$ac_word$ac_exec_ext\"; then\n    ac_cv_path_XSLTPROC=\"$as_dir/$ac_word$ac_exec_ext\"\n    $as_echo \"$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext\" >&5\n    break 2\n  fi\ndone\n  done\nIFS=$as_save_IFS\n\n  test -z \"$ac_cv_path_XSLTPROC\" && ac_cv_path_XSLTPROC=\"false\"\n  ;;\nesac\nfi\nXSLTPROC=$ac_cv_path_XSLTPROC\nif test -n \"$XSLTPROC\"; then\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $XSLTPROC\" >&5\n$as_echo \"$XSLTPROC\" >&6; }\nelse\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\nfi\n\n\nif test -d \"/usr/share/xml/docbook/stylesheet/docbook-xsl\" ; then\n  DEFAULT_XSLROOT=\"/usr/share/xml/docbook/stylesheet/docbook-xsl\"\nelif test -d \"/usr/share/sgml/docbook/xsl-stylesheets\" ; then\n  DEFAULT_XSLROOT=\"/usr/share/sgml/docbook/xsl-stylesheets\"\nelse\n    DEFAULT_XSLROOT=\"\"\nfi\n\n# Check whether --with-xslroot was given.\nif test \"${with_xslroot+set}\" = set; then :\n  withval=$with_xslroot;\nif test \"x$with_xslroot\" = \"xno\" ; then\n  XSLROOT=\"${DEFAULT_XSLROOT}\"\nelse\n  XSLROOT=\"${with_xslroot}\"\nfi\n\nelse\n  XSLROOT=\"${DEFAULT_XSLROOT}\"\n\nfi\n\nif test \"x$XSLTPROC\" = \"xfalse\" ; then\n  XSLROOT=\"\"\nfi\n\n\nCFLAGS=$CFLAGS\nac_ext=c\nac_cpp='$CPP $CPPFLAGS'\nac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'\nac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'\nac_compiler_gnu=$ac_cv_c_compiler_gnu\nif test -n \"$ac_tool_prefix\"; then\n  # Extract the first word of \"${ac_tool_prefix}gcc\", so it can be a program name with args.\nset dummy ${ac_tool_prefix}gcc; ac_word=$2\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for $ac_word\" >&5\n$as_echo_n \"checking for $ac_word... \" >&6; }\nif ${ac_cv_prog_CC+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  if test -n \"$CC\"; then\n  ac_cv_prog_CC=\"$CC\" # Let the user override the test.\nelse\nas_save_IFS=$IFS; IFS=$PATH_SEPARATOR\nfor as_dir in $PATH\ndo\n  IFS=$as_save_IFS\n  test -z \"$as_dir\" && as_dir=.\n    for ac_exec_ext in '' $ac_executable_extensions; do\n  if as_fn_executable_p \"$as_dir/$ac_word$ac_exec_ext\"; then\n    ac_cv_prog_CC=\"${ac_tool_prefix}gcc\"\n    $as_echo \"$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext\" >&5\n    break 2\n  fi\ndone\n  done\nIFS=$as_save_IFS\n\nfi\nfi\nCC=$ac_cv_prog_CC\nif test -n \"$CC\"; then\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $CC\" >&5\n$as_echo \"$CC\" >&6; }\nelse\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\nfi\n\n\nfi\nif test -z \"$ac_cv_prog_CC\"; then\n  ac_ct_CC=$CC\n  # Extract the first word of \"gcc\", so it can be a program name with args.\nset dummy gcc; ac_word=$2\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for $ac_word\" >&5\n$as_echo_n \"checking for $ac_word... \" >&6; }\nif ${ac_cv_prog_ac_ct_CC+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  if test -n \"$ac_ct_CC\"; then\n  ac_cv_prog_ac_ct_CC=\"$ac_ct_CC\" # Let the user override the test.\nelse\nas_save_IFS=$IFS; IFS=$PATH_SEPARATOR\nfor as_dir in $PATH\ndo\n  IFS=$as_save_IFS\n  test -z \"$as_dir\" && as_dir=.\n    for ac_exec_ext in '' $ac_executable_extensions; do\n  if as_fn_executable_p \"$as_dir/$ac_word$ac_exec_ext\"; then\n    ac_cv_prog_ac_ct_CC=\"gcc\"\n    $as_echo \"$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext\" >&5\n    break 2\n  fi\ndone\n  done\nIFS=$as_save_IFS\n\nfi\nfi\nac_ct_CC=$ac_cv_prog_ac_ct_CC\nif test -n \"$ac_ct_CC\"; then\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_ct_CC\" >&5\n$as_echo \"$ac_ct_CC\" >&6; }\nelse\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\nfi\n\n  if test \"x$ac_ct_CC\" = x; then\n    CC=\"\"\n  else\n    case $cross_compiling:$ac_tool_warned in\nyes:)\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not prefixed with host triplet\" >&5\n$as_echo \"$as_me: WARNING: using cross tools not prefixed with host triplet\" >&2;}\nac_tool_warned=yes ;;\nesac\n    CC=$ac_ct_CC\n  fi\nelse\n  CC=\"$ac_cv_prog_CC\"\nfi\n\nif test -z \"$CC\"; then\n          if test -n \"$ac_tool_prefix\"; then\n    # Extract the first word of \"${ac_tool_prefix}cc\", so it can be a program name with args.\nset dummy ${ac_tool_prefix}cc; ac_word=$2\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for $ac_word\" >&5\n$as_echo_n \"checking for $ac_word... \" >&6; }\nif ${ac_cv_prog_CC+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  if test -n \"$CC\"; then\n  ac_cv_prog_CC=\"$CC\" # Let the user override the test.\nelse\nas_save_IFS=$IFS; IFS=$PATH_SEPARATOR\nfor as_dir in $PATH\ndo\n  IFS=$as_save_IFS\n  test -z \"$as_dir\" && as_dir=.\n    for ac_exec_ext in '' $ac_executable_extensions; do\n  if as_fn_executable_p \"$as_dir/$ac_word$ac_exec_ext\"; then\n    ac_cv_prog_CC=\"${ac_tool_prefix}cc\"\n    $as_echo \"$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext\" >&5\n    break 2\n  fi\ndone\n  done\nIFS=$as_save_IFS\n\nfi\nfi\nCC=$ac_cv_prog_CC\nif test -n \"$CC\"; then\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $CC\" >&5\n$as_echo \"$CC\" >&6; }\nelse\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\nfi\n\n\n  fi\nfi\nif test -z \"$CC\"; then\n  # Extract the first word of \"cc\", so it can be a program name with args.\nset dummy cc; ac_word=$2\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for $ac_word\" >&5\n$as_echo_n \"checking for $ac_word... \" >&6; }\nif ${ac_cv_prog_CC+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  if test -n \"$CC\"; then\n  ac_cv_prog_CC=\"$CC\" # Let the user override the test.\nelse\n  ac_prog_rejected=no\nas_save_IFS=$IFS; IFS=$PATH_SEPARATOR\nfor as_dir in $PATH\ndo\n  IFS=$as_save_IFS\n  test -z \"$as_dir\" && as_dir=.\n    for ac_exec_ext in '' $ac_executable_extensions; do\n  if as_fn_executable_p \"$as_dir/$ac_word$ac_exec_ext\"; then\n    if test \"$as_dir/$ac_word$ac_exec_ext\" = \"/usr/ucb/cc\"; then\n       ac_prog_rejected=yes\n       continue\n     fi\n    ac_cv_prog_CC=\"cc\"\n    $as_echo \"$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext\" >&5\n    break 2\n  fi\ndone\n  done\nIFS=$as_save_IFS\n\nif test $ac_prog_rejected = yes; then\n  # We found a bogon in the path, so make sure we never use it.\n  set dummy $ac_cv_prog_CC\n  shift\n  if test $# != 0; then\n    # We chose a different compiler from the bogus one.\n    # However, it has the same basename, so the bogon will be chosen\n    # first if we set CC to just the basename; use the full file name.\n    shift\n    ac_cv_prog_CC=\"$as_dir/$ac_word${1+' '}$@\"\n  fi\nfi\nfi\nfi\nCC=$ac_cv_prog_CC\nif test -n \"$CC\"; then\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $CC\" >&5\n$as_echo \"$CC\" >&6; }\nelse\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\nfi\n\n\nfi\nif test -z \"$CC\"; then\n  if test -n \"$ac_tool_prefix\"; then\n  for ac_prog in cl.exe\n  do\n    # Extract the first word of \"$ac_tool_prefix$ac_prog\", so it can be a program name with args.\nset dummy $ac_tool_prefix$ac_prog; ac_word=$2\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for $ac_word\" >&5\n$as_echo_n \"checking for $ac_word... \" >&6; }\nif ${ac_cv_prog_CC+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  if test -n \"$CC\"; then\n  ac_cv_prog_CC=\"$CC\" # Let the user override the test.\nelse\nas_save_IFS=$IFS; IFS=$PATH_SEPARATOR\nfor as_dir in $PATH\ndo\n  IFS=$as_save_IFS\n  test -z \"$as_dir\" && as_dir=.\n    for ac_exec_ext in '' $ac_executable_extensions; do\n  if as_fn_executable_p \"$as_dir/$ac_word$ac_exec_ext\"; then\n    ac_cv_prog_CC=\"$ac_tool_prefix$ac_prog\"\n    $as_echo \"$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext\" >&5\n    break 2\n  fi\ndone\n  done\nIFS=$as_save_IFS\n\nfi\nfi\nCC=$ac_cv_prog_CC\nif test -n \"$CC\"; then\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $CC\" >&5\n$as_echo \"$CC\" >&6; }\nelse\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\nfi\n\n\n    test -n \"$CC\" && break\n  done\nfi\nif test -z \"$CC\"; then\n  ac_ct_CC=$CC\n  for ac_prog in cl.exe\ndo\n  # Extract the first word of \"$ac_prog\", so it can be a program name with args.\nset dummy $ac_prog; ac_word=$2\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for $ac_word\" >&5\n$as_echo_n \"checking for $ac_word... \" >&6; }\nif ${ac_cv_prog_ac_ct_CC+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  if test -n \"$ac_ct_CC\"; then\n  ac_cv_prog_ac_ct_CC=\"$ac_ct_CC\" # Let the user override the test.\nelse\nas_save_IFS=$IFS; IFS=$PATH_SEPARATOR\nfor as_dir in $PATH\ndo\n  IFS=$as_save_IFS\n  test -z \"$as_dir\" && as_dir=.\n    for ac_exec_ext in '' $ac_executable_extensions; do\n  if as_fn_executable_p \"$as_dir/$ac_word$ac_exec_ext\"; then\n    ac_cv_prog_ac_ct_CC=\"$ac_prog\"\n    $as_echo \"$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext\" >&5\n    break 2\n  fi\ndone\n  done\nIFS=$as_save_IFS\n\nfi\nfi\nac_ct_CC=$ac_cv_prog_ac_ct_CC\nif test -n \"$ac_ct_CC\"; then\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_ct_CC\" >&5\n$as_echo \"$ac_ct_CC\" >&6; }\nelse\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\nfi\n\n\n  test -n \"$ac_ct_CC\" && break\ndone\n\n  if test \"x$ac_ct_CC\" = x; then\n    CC=\"\"\n  else\n    case $cross_compiling:$ac_tool_warned in\nyes:)\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not prefixed with host triplet\" >&5\n$as_echo \"$as_me: WARNING: using cross tools not prefixed with host triplet\" >&2;}\nac_tool_warned=yes ;;\nesac\n    CC=$ac_ct_CC\n  fi\nfi\n\nfi\n\n\ntest -z \"$CC\" && { { $as_echo \"$as_me:${as_lineno-$LINENO}: error: in \\`$ac_pwd':\" >&5\n$as_echo \"$as_me: error: in \\`$ac_pwd':\" >&2;}\nas_fn_error $? \"no acceptable C compiler found in \\$PATH\nSee \\`config.log' for more details\" \"$LINENO\" 5; }\n\n# Provide some information about the compiler.\n$as_echo \"$as_me:${as_lineno-$LINENO}: checking for C compiler version\" >&5\nset X $ac_compile\nac_compiler=$2\nfor ac_option in --version -v -V -qversion; do\n  { { ac_try=\"$ac_compiler $ac_option >&5\"\ncase \"(($ac_try\" in\n  *\\\"* | *\\`* | *\\\\*) ac_try_echo=\\$ac_try;;\n  *) ac_try_echo=$ac_try;;\nesac\neval ac_try_echo=\"\\\"\\$as_me:${as_lineno-$LINENO}: $ac_try_echo\\\"\"\n$as_echo \"$ac_try_echo\"; } >&5\n  (eval \"$ac_compiler $ac_option >&5\") 2>conftest.err\n  ac_status=$?\n  if test -s conftest.err; then\n    sed '10a\\\n... rest of stderr output deleted ...\n         10q' conftest.err >conftest.er1\n    cat conftest.er1 >&5\n  fi\n  rm -f conftest.er1 conftest.err\n  $as_echo \"$as_me:${as_lineno-$LINENO}: \\$? = $ac_status\" >&5\n  test $ac_status = 0; }\ndone\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\nint\nmain ()\n{\n\n  ;\n  return 0;\n}\n_ACEOF\nac_clean_files_save=$ac_clean_files\nac_clean_files=\"$ac_clean_files a.out a.out.dSYM a.exe b.out\"\n# Try to create an executable without -o first, disregard a.out.\n# It will help us diagnose broken compilers, and finding out an intuition\n# of exeext.\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether the C compiler works\" >&5\n$as_echo_n \"checking whether the C compiler works... \" >&6; }\nac_link_default=`$as_echo \"$ac_link\" | sed 's/ -o *conftest[^ ]*//'`\n\n# The possible output files:\nac_files=\"a.out conftest.exe conftest a.exe a_out.exe b.out conftest.*\"\n\nac_rmfiles=\nfor ac_file in $ac_files\ndo\n  case $ac_file in\n    *.$ac_ext | *.xcoff | *.tds | *.d | *.pdb | *.xSYM | *.bb | *.bbg | *.map | *.inf | *.dSYM | *.o | *.obj ) ;;\n    * ) ac_rmfiles=\"$ac_rmfiles $ac_file\";;\n  esac\ndone\nrm -f $ac_rmfiles\n\nif { { ac_try=\"$ac_link_default\"\ncase \"(($ac_try\" in\n  *\\\"* | *\\`* | *\\\\*) ac_try_echo=\\$ac_try;;\n  *) ac_try_echo=$ac_try;;\nesac\neval ac_try_echo=\"\\\"\\$as_me:${as_lineno-$LINENO}: $ac_try_echo\\\"\"\n$as_echo \"$ac_try_echo\"; } >&5\n  (eval \"$ac_link_default\") 2>&5\n  ac_status=$?\n  $as_echo \"$as_me:${as_lineno-$LINENO}: \\$? = $ac_status\" >&5\n  test $ac_status = 0; }; then :\n  # Autoconf-2.13 could set the ac_cv_exeext variable to `no'.\n# So ignore a value of `no', otherwise this would lead to `EXEEXT = no'\n# in a Makefile.  We should not override ac_cv_exeext if it was cached,\n# so that the user can short-circuit this test for compilers unknown to\n# Autoconf.\nfor ac_file in $ac_files ''\ndo\n  test -f \"$ac_file\" || continue\n  case $ac_file in\n    *.$ac_ext | *.xcoff | *.tds | *.d | *.pdb | *.xSYM | *.bb | *.bbg | *.map | *.inf | *.dSYM | *.o | *.obj )\n\t;;\n    [ab].out )\n\t# We found the default executable, but exeext='' is most\n\t# certainly right.\n\tbreak;;\n    *.* )\n\tif test \"${ac_cv_exeext+set}\" = set && test \"$ac_cv_exeext\" != no;\n\tthen :; else\n\t   ac_cv_exeext=`expr \"$ac_file\" : '[^.]*\\(\\..*\\)'`\n\tfi\n\t# We set ac_cv_exeext here because the later test for it is not\n\t# safe: cross compilers may not add the suffix if given an `-o'\n\t# argument, so we may need to know it at that point already.\n\t# Even if this section looks crufty: it has the advantage of\n\t# actually working.\n\tbreak;;\n    * )\n\tbreak;;\n  esac\ndone\ntest \"$ac_cv_exeext\" = no && ac_cv_exeext=\n\nelse\n  ac_file=''\nfi\nif test -z \"$ac_file\"; then :\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n$as_echo \"$as_me: failed program was:\" >&5\nsed 's/^/| /' conftest.$ac_ext >&5\n\n{ { $as_echo \"$as_me:${as_lineno-$LINENO}: error: in \\`$ac_pwd':\" >&5\n$as_echo \"$as_me: error: in \\`$ac_pwd':\" >&2;}\nas_fn_error 77 \"C compiler cannot create executables\nSee \\`config.log' for more details\" \"$LINENO\" 5; }\nelse\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for C compiler default output file name\" >&5\n$as_echo_n \"checking for C compiler default output file name... \" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_file\" >&5\n$as_echo \"$ac_file\" >&6; }\nac_exeext=$ac_cv_exeext\n\nrm -f -r a.out a.out.dSYM a.exe conftest$ac_cv_exeext b.out\nac_clean_files=$ac_clean_files_save\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for suffix of executables\" >&5\n$as_echo_n \"checking for suffix of executables... \" >&6; }\nif { { ac_try=\"$ac_link\"\ncase \"(($ac_try\" in\n  *\\\"* | *\\`* | *\\\\*) ac_try_echo=\\$ac_try;;\n  *) ac_try_echo=$ac_try;;\nesac\neval ac_try_echo=\"\\\"\\$as_me:${as_lineno-$LINENO}: $ac_try_echo\\\"\"\n$as_echo \"$ac_try_echo\"; } >&5\n  (eval \"$ac_link\") 2>&5\n  ac_status=$?\n  $as_echo \"$as_me:${as_lineno-$LINENO}: \\$? = $ac_status\" >&5\n  test $ac_status = 0; }; then :\n  # If both `conftest.exe' and `conftest' are `present' (well, observable)\n# catch `conftest.exe'.  For instance with Cygwin, `ls conftest' will\n# work properly (i.e., refer to `conftest.exe'), while it won't with\n# `rm'.\nfor ac_file in conftest.exe conftest conftest.*; do\n  test -f \"$ac_file\" || continue\n  case $ac_file in\n    *.$ac_ext | *.xcoff | *.tds | *.d | *.pdb | *.xSYM | *.bb | *.bbg | *.map | *.inf | *.dSYM | *.o | *.obj ) ;;\n    *.* ) ac_cv_exeext=`expr \"$ac_file\" : '[^.]*\\(\\..*\\)'`\n\t  break;;\n    * ) break;;\n  esac\ndone\nelse\n  { { $as_echo \"$as_me:${as_lineno-$LINENO}: error: in \\`$ac_pwd':\" >&5\n$as_echo \"$as_me: error: in \\`$ac_pwd':\" >&2;}\nas_fn_error $? \"cannot compute suffix of executables: cannot compile and link\nSee \\`config.log' for more details\" \"$LINENO\" 5; }\nfi\nrm -f conftest conftest$ac_cv_exeext\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_exeext\" >&5\n$as_echo \"$ac_cv_exeext\" >&6; }\n\nrm -f conftest.$ac_ext\nEXEEXT=$ac_cv_exeext\nac_exeext=$EXEEXT\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n#include <stdio.h>\nint\nmain ()\n{\nFILE *f = fopen (\"conftest.out\", \"w\");\n return ferror (f) || fclose (f) != 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nac_clean_files=\"$ac_clean_files conftest.out\"\n# Check that the compiler produces executables we can run.  If not, either\n# the compiler is broken, or we cross compile.\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether we are cross compiling\" >&5\n$as_echo_n \"checking whether we are cross compiling... \" >&6; }\nif test \"$cross_compiling\" != yes; then\n  { { ac_try=\"$ac_link\"\ncase \"(($ac_try\" in\n  *\\\"* | *\\`* | *\\\\*) ac_try_echo=\\$ac_try;;\n  *) ac_try_echo=$ac_try;;\nesac\neval ac_try_echo=\"\\\"\\$as_me:${as_lineno-$LINENO}: $ac_try_echo\\\"\"\n$as_echo \"$ac_try_echo\"; } >&5\n  (eval \"$ac_link\") 2>&5\n  ac_status=$?\n  $as_echo \"$as_me:${as_lineno-$LINENO}: \\$? = $ac_status\" >&5\n  test $ac_status = 0; }\n  if { ac_try='./conftest$ac_cv_exeext'\n  { { case \"(($ac_try\" in\n  *\\\"* | *\\`* | *\\\\*) ac_try_echo=\\$ac_try;;\n  *) ac_try_echo=$ac_try;;\nesac\neval ac_try_echo=\"\\\"\\$as_me:${as_lineno-$LINENO}: $ac_try_echo\\\"\"\n$as_echo \"$ac_try_echo\"; } >&5\n  (eval \"$ac_try\") 2>&5\n  ac_status=$?\n  $as_echo \"$as_me:${as_lineno-$LINENO}: \\$? = $ac_status\" >&5\n  test $ac_status = 0; }; }; then\n    cross_compiling=no\n  else\n    if test \"$cross_compiling\" = maybe; then\n\tcross_compiling=yes\n    else\n\t{ { $as_echo \"$as_me:${as_lineno-$LINENO}: error: in \\`$ac_pwd':\" >&5\n$as_echo \"$as_me: error: in \\`$ac_pwd':\" >&2;}\nas_fn_error $? \"cannot run C compiled programs.\nIf you meant to cross compile, use \\`--host'.\nSee \\`config.log' for more details\" \"$LINENO\" 5; }\n    fi\n  fi\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $cross_compiling\" >&5\n$as_echo \"$cross_compiling\" >&6; }\n\nrm -f conftest.$ac_ext conftest$ac_cv_exeext conftest.out\nac_clean_files=$ac_clean_files_save\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for suffix of object files\" >&5\n$as_echo_n \"checking for suffix of object files... \" >&6; }\nif ${ac_cv_objext+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\nint\nmain ()\n{\n\n  ;\n  return 0;\n}\n_ACEOF\nrm -f conftest.o conftest.obj\nif { { ac_try=\"$ac_compile\"\ncase \"(($ac_try\" in\n  *\\\"* | *\\`* | *\\\\*) ac_try_echo=\\$ac_try;;\n  *) ac_try_echo=$ac_try;;\nesac\neval ac_try_echo=\"\\\"\\$as_me:${as_lineno-$LINENO}: $ac_try_echo\\\"\"\n$as_echo \"$ac_try_echo\"; } >&5\n  (eval \"$ac_compile\") 2>&5\n  ac_status=$?\n  $as_echo \"$as_me:${as_lineno-$LINENO}: \\$? = $ac_status\" >&5\n  test $ac_status = 0; }; then :\n  for ac_file in conftest.o conftest.obj conftest.*; do\n  test -f \"$ac_file\" || continue;\n  case $ac_file in\n    *.$ac_ext | *.xcoff | *.tds | *.d | *.pdb | *.xSYM | *.bb | *.bbg | *.map | *.inf | *.dSYM ) ;;\n    *) ac_cv_objext=`expr \"$ac_file\" : '.*\\.\\(.*\\)'`\n       break;;\n  esac\ndone\nelse\n  $as_echo \"$as_me: failed program was:\" >&5\nsed 's/^/| /' conftest.$ac_ext >&5\n\n{ { $as_echo \"$as_me:${as_lineno-$LINENO}: error: in \\`$ac_pwd':\" >&5\n$as_echo \"$as_me: error: in \\`$ac_pwd':\" >&2;}\nas_fn_error $? \"cannot compute suffix of object files: cannot compile\nSee \\`config.log' for more details\" \"$LINENO\" 5; }\nfi\nrm -f conftest.$ac_cv_objext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_objext\" >&5\n$as_echo \"$ac_cv_objext\" >&6; }\nOBJEXT=$ac_cv_objext\nac_objext=$OBJEXT\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether we are using the GNU C compiler\" >&5\n$as_echo_n \"checking whether we are using the GNU C compiler... \" >&6; }\nif ${ac_cv_c_compiler_gnu+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\nint\nmain ()\n{\n#ifndef __GNUC__\n       choke me\n#endif\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  ac_compiler_gnu=yes\nelse\n  ac_compiler_gnu=no\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nac_cv_c_compiler_gnu=$ac_compiler_gnu\n\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_c_compiler_gnu\" >&5\n$as_echo \"$ac_cv_c_compiler_gnu\" >&6; }\nif test $ac_compiler_gnu = yes; then\n  GCC=yes\nelse\n  GCC=\nfi\nac_test_CFLAGS=${CFLAGS+set}\nac_save_CFLAGS=$CFLAGS\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether $CC accepts -g\" >&5\n$as_echo_n \"checking whether $CC accepts -g... \" >&6; }\nif ${ac_cv_prog_cc_g+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  ac_save_c_werror_flag=$ac_c_werror_flag\n   ac_c_werror_flag=yes\n   ac_cv_prog_cc_g=no\n   CFLAGS=\"-g\"\n   cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\nint\nmain ()\n{\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  ac_cv_prog_cc_g=yes\nelse\n  CFLAGS=\"\"\n      cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\nint\nmain ()\n{\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n\nelse\n  ac_c_werror_flag=$ac_save_c_werror_flag\n\t CFLAGS=\"-g\"\n\t cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\nint\nmain ()\n{\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  ac_cv_prog_cc_g=yes\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\n   ac_c_werror_flag=$ac_save_c_werror_flag\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_prog_cc_g\" >&5\n$as_echo \"$ac_cv_prog_cc_g\" >&6; }\nif test \"$ac_test_CFLAGS\" = set; then\n  CFLAGS=$ac_save_CFLAGS\nelif test $ac_cv_prog_cc_g = yes; then\n  if test \"$GCC\" = yes; then\n    CFLAGS=\"-g -O2\"\n  else\n    CFLAGS=\"-g\"\n  fi\nelse\n  if test \"$GCC\" = yes; then\n    CFLAGS=\"-O2\"\n  else\n    CFLAGS=\n  fi\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for $CC option to accept ISO C89\" >&5\n$as_echo_n \"checking for $CC option to accept ISO C89... \" >&6; }\nif ${ac_cv_prog_cc_c89+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  ac_cv_prog_cc_c89=no\nac_save_CC=$CC\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n#include <stdarg.h>\n#include <stdio.h>\nstruct stat;\n/* Most of the following tests are stolen from RCS 5.7's src/conf.sh.  */\nstruct buf { int x; };\nFILE * (*rcsopen) (struct buf *, struct stat *, int);\nstatic char *e (p, i)\n     char **p;\n     int i;\n{\n  return p[i];\n}\nstatic char *f (char * (*g) (char **, int), char **p, ...)\n{\n  char *s;\n  va_list v;\n  va_start (v,p);\n  s = g (p, va_arg (v,int));\n  va_end (v);\n  return s;\n}\n\n/* OSF 4.0 Compaq cc is some sort of almost-ANSI by default.  It has\n   function prototypes and stuff, but not '\\xHH' hex character constants.\n   These don't provoke an error unfortunately, instead are silently treated\n   as 'x'.  The following induces an error, until -std is added to get\n   proper ANSI mode.  Curiously '\\x00'!='x' always comes out true, for an\n   array size at least.  It's necessary to write '\\x00'==0 to get something\n   that's true only with -std.  */\nint osf4_cc_array ['\\x00' == 0 ? 1 : -1];\n\n/* IBM C 6 for AIX is almost-ANSI by default, but it replaces macro parameters\n   inside strings and character constants.  */\n#define FOO(x) 'x'\nint xlc6_cc_array[FOO(a) == 'x' ? 1 : -1];\n\nint test (int i, double x);\nstruct s1 {int (*f) (int a);};\nstruct s2 {int (*f) (double a);};\nint pairnames (int, char **, FILE *(*)(struct buf *, struct stat *, int), int, int);\nint argc;\nchar **argv;\nint\nmain ()\n{\nreturn f (e, argv, 0) != argv[0]  ||  f (e, argv, 1) != argv[1];\n  ;\n  return 0;\n}\n_ACEOF\nfor ac_arg in '' -qlanglvl=extc89 -qlanglvl=ansi -std \\\n\t-Ae \"-Aa -D_HPUX_SOURCE\" \"-Xc -D__EXTENSIONS__\"\ndo\n  CC=\"$ac_save_CC $ac_arg\"\n  if ac_fn_c_try_compile \"$LINENO\"; then :\n  ac_cv_prog_cc_c89=$ac_arg\nfi\nrm -f core conftest.err conftest.$ac_objext\n  test \"x$ac_cv_prog_cc_c89\" != \"xno\" && break\ndone\nrm -f conftest.$ac_ext\nCC=$ac_save_CC\n\nfi\n# AC_CACHE_VAL\ncase \"x$ac_cv_prog_cc_c89\" in\n  x)\n    { $as_echo \"$as_me:${as_lineno-$LINENO}: result: none needed\" >&5\n$as_echo \"none needed\" >&6; } ;;\n  xno)\n    { $as_echo \"$as_me:${as_lineno-$LINENO}: result: unsupported\" >&5\n$as_echo \"unsupported\" >&6; } ;;\n  *)\n    CC=\"$CC $ac_cv_prog_cc_c89\"\n    { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_prog_cc_c89\" >&5\n$as_echo \"$ac_cv_prog_cc_c89\" >&6; } ;;\nesac\nif test \"x$ac_cv_prog_cc_c89\" != xno; then :\n\nfi\n\nac_ext=c\nac_cpp='$CPP $CPPFLAGS'\nac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'\nac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'\nac_compiler_gnu=$ac_cv_c_compiler_gnu\n\n\nif test \"x$GCC\" != \"xyes\" ; then\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler is MSVC\" >&5\n$as_echo_n \"checking whether compiler is MSVC... \" >&6; }\nif ${je_cv_msvc+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\nint\nmain ()\n{\n\n#ifndef _MSC_VER\n  int fail-1;\n#endif\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_msvc=yes\nelse\n  je_cv_msvc=no\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_msvc\" >&5\n$as_echo \"$je_cv_msvc\" >&6; }\nfi\n\nje_cv_cray_prgenv_wrapper=\"\"\nif test \"x${PE_ENV}\" != \"x\" ; then\n  case \"${CC}\" in\n    CC|cc)\n\tje_cv_cray_prgenv_wrapper=\"yes\"\n\t;;\n    *)\n       ;;\n  esac\nfi\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler is cray\" >&5\n$as_echo_n \"checking whether compiler is cray... \" >&6; }\nif ${je_cv_cray+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\nint\nmain ()\n{\n\n#ifndef _CRAYC\n  int fail-1;\n#endif\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cray=yes\nelse\n  je_cv_cray=no\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_cray\" >&5\n$as_echo \"$je_cv_cray\" >&6; }\n\nif test \"x${je_cv_cray}\" = \"xyes\" ; then\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether cray compiler version is 8.4\" >&5\n$as_echo_n \"checking whether cray compiler version is 8.4... \" >&6; }\nif ${je_cv_cray_84+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\nint\nmain ()\n{\n\n#if !(_RELEASE_MAJOR == 8 && _RELEASE_MINOR == 4)\n  int fail-1;\n#endif\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cray_84=yes\nelse\n  je_cv_cray_84=no\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_cray_84\" >&5\n$as_echo \"$je_cv_cray_84\" >&6; }\nfi\n\nif test \"x$GCC\" = \"xyes\" ; then\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -std=gnu11\" >&5\n$as_echo_n \"checking whether compiler supports -std=gnu11... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-std=gnu11\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-std=gnu11\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n  if test \"x$je_cv_cflags_added\" = \"x-std=gnu11\" ; then\n    cat >>confdefs.h <<_ACEOF\n#define JEMALLOC_HAS_RESTRICT 1\n_ACEOF\n\n  else\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -std=gnu99\" >&5\n$as_echo_n \"checking whether compiler supports -std=gnu99... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-std=gnu99\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-std=gnu99\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n    if test \"x$je_cv_cflags_added\" = \"x-std=gnu99\" ; then\n      cat >>confdefs.h <<_ACEOF\n#define JEMALLOC_HAS_RESTRICT 1\n_ACEOF\n\n    fi\n  fi\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -Wall\" >&5\n$as_echo_n \"checking whether compiler supports -Wall... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-Wall\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-Wall\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -Wextra\" >&5\n$as_echo_n \"checking whether compiler supports -Wextra... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-Wextra\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-Wextra\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -Wshorten-64-to-32\" >&5\n$as_echo_n \"checking whether compiler supports -Wshorten-64-to-32... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-Wshorten-64-to-32\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-Wshorten-64-to-32\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -Wsign-compare\" >&5\n$as_echo_n \"checking whether compiler supports -Wsign-compare... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-Wsign-compare\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-Wsign-compare\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -Wundef\" >&5\n$as_echo_n \"checking whether compiler supports -Wundef... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-Wundef\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-Wundef\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -Wno-format-zero-length\" >&5\n$as_echo_n \"checking whether compiler supports -Wno-format-zero-length... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-Wno-format-zero-length\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-Wno-format-zero-length\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -pipe\" >&5\n$as_echo_n \"checking whether compiler supports -pipe... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-pipe\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-pipe\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -g3\" >&5\n$as_echo_n \"checking whether compiler supports -g3... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-g3\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-g3\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\nelif test \"x$je_cv_msvc\" = \"xyes\" ; then\n  CC=\"$CC -nologo\"\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -Zi\" >&5\n$as_echo_n \"checking whether compiler supports -Zi... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-Zi\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-Zi\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -MT\" >&5\n$as_echo_n \"checking whether compiler supports -MT... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-MT\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-MT\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -W3\" >&5\n$as_echo_n \"checking whether compiler supports -W3... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-W3\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-W3\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -FS\" >&5\n$as_echo_n \"checking whether compiler supports -FS... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-FS\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-FS\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n  T_APPEND_V=-I${srcdir}/include/msvc_compat\n  if test \"x${CPPFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CPPFLAGS=\"${CPPFLAGS}${T_APPEND_V}\"\nelse\n  CPPFLAGS=\"${CPPFLAGS} ${T_APPEND_V}\"\nfi\n\n\nfi\nif test \"x$je_cv_cray\" = \"xyes\" ; then\n    if test \"x$je_cv_cray_84\" = \"xyes\" ; then\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -hipa2\" >&5\n$as_echo_n \"checking whether compiler supports -hipa2... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-hipa2\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-hipa2\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -hnognu\" >&5\n$as_echo_n \"checking whether compiler supports -hnognu... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-hnognu\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-hnognu\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n  fi\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -hnomessage=128\" >&5\n$as_echo_n \"checking whether compiler supports -hnomessage=128... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-hnomessage=128\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-hnomessage=128\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -hnomessage=1357\" >&5\n$as_echo_n \"checking whether compiler supports -hnomessage=1357... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-hnomessage=1357\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-hnomessage=1357\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\nfi\n\n\n\nac_ext=c\nac_cpp='$CPP $CPPFLAGS'\nac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'\nac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'\nac_compiler_gnu=$ac_cv_c_compiler_gnu\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking how to run the C preprocessor\" >&5\n$as_echo_n \"checking how to run the C preprocessor... \" >&6; }\n# On Suns, sometimes $CPP names a directory.\nif test -n \"$CPP\" && test -d \"$CPP\"; then\n  CPP=\nfi\nif test -z \"$CPP\"; then\n  if ${ac_cv_prog_CPP+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n      # Double quotes because CPP needs to be expanded\n    for CPP in \"$CC -E\" \"$CC -E -traditional-cpp\" \"/lib/cpp\"\n    do\n      ac_preproc_ok=false\nfor ac_c_preproc_warn_flag in '' yes\ndo\n  # Use a header file that comes with gcc, so configuring glibc\n  # with a fresh cross-compiler works.\n  # Prefer <limits.h> to <assert.h> if __STDC__ is defined, since\n  # <limits.h> exists even on freestanding compilers.\n  # On the NeXT, cc -E runs the code through the compiler's parser,\n  # not just through cpp. \"Syntax error\" is here to catch this case.\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n#ifdef __STDC__\n# include <limits.h>\n#else\n# include <assert.h>\n#endif\n\t\t     Syntax error\n_ACEOF\nif ac_fn_c_try_cpp \"$LINENO\"; then :\n\nelse\n  # Broken: fails on valid input.\ncontinue\nfi\nrm -f conftest.err conftest.i conftest.$ac_ext\n\n  # OK, works on sane cases.  Now check whether nonexistent headers\n  # can be detected and how.\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n#include <ac_nonexistent.h>\n_ACEOF\nif ac_fn_c_try_cpp \"$LINENO\"; then :\n  # Broken: success on invalid input.\ncontinue\nelse\n  # Passes both tests.\nac_preproc_ok=:\nbreak\nfi\nrm -f conftest.err conftest.i conftest.$ac_ext\n\ndone\n# Because of `break', _AC_PREPROC_IFELSE's cleaning code was skipped.\nrm -f conftest.i conftest.err conftest.$ac_ext\nif $ac_preproc_ok; then :\n  break\nfi\n\n    done\n    ac_cv_prog_CPP=$CPP\n\nfi\n  CPP=$ac_cv_prog_CPP\nelse\n  ac_cv_prog_CPP=$CPP\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $CPP\" >&5\n$as_echo \"$CPP\" >&6; }\nac_preproc_ok=false\nfor ac_c_preproc_warn_flag in '' yes\ndo\n  # Use a header file that comes with gcc, so configuring glibc\n  # with a fresh cross-compiler works.\n  # Prefer <limits.h> to <assert.h> if __STDC__ is defined, since\n  # <limits.h> exists even on freestanding compilers.\n  # On the NeXT, cc -E runs the code through the compiler's parser,\n  # not just through cpp. \"Syntax error\" is here to catch this case.\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n#ifdef __STDC__\n# include <limits.h>\n#else\n# include <assert.h>\n#endif\n\t\t     Syntax error\n_ACEOF\nif ac_fn_c_try_cpp \"$LINENO\"; then :\n\nelse\n  # Broken: fails on valid input.\ncontinue\nfi\nrm -f conftest.err conftest.i conftest.$ac_ext\n\n  # OK, works on sane cases.  Now check whether nonexistent headers\n  # can be detected and how.\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n#include <ac_nonexistent.h>\n_ACEOF\nif ac_fn_c_try_cpp \"$LINENO\"; then :\n  # Broken: success on invalid input.\ncontinue\nelse\n  # Passes both tests.\nac_preproc_ok=:\nbreak\nfi\nrm -f conftest.err conftest.i conftest.$ac_ext\n\ndone\n# Because of `break', _AC_PREPROC_IFELSE's cleaning code was skipped.\nrm -f conftest.i conftest.err conftest.$ac_ext\nif $ac_preproc_ok; then :\n\nelse\n  { { $as_echo \"$as_me:${as_lineno-$LINENO}: error: in \\`$ac_pwd':\" >&5\n$as_echo \"$as_me: error: in \\`$ac_pwd':\" >&2;}\nas_fn_error $? \"C preprocessor \\\"$CPP\\\" fails sanity check\nSee \\`config.log' for more details\" \"$LINENO\" 5; }\nfi\n\nac_ext=c\nac_cpp='$CPP $CPPFLAGS'\nac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'\nac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'\nac_compiler_gnu=$ac_cv_c_compiler_gnu\n\n\n# Check whether --enable-cxx was given.\nif test \"${enable_cxx+set}\" = set; then :\n  enableval=$enable_cxx; if test \"x$enable_cxx\" = \"xno\" ; then\n  enable_cxx=\"0\"\nelse\n  enable_cxx=\"1\"\nfi\n\nelse\n  enable_cxx=\"1\"\n\nfi\n\nif test \"x$enable_cxx\" = \"x1\" ; then\n      # ===========================================================================\n#   http://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx.html\n# ===========================================================================\n#\n# SYNOPSIS\n#\n#   AX_CXX_COMPILE_STDCXX(VERSION, [ext|noext], [mandatory|optional])\n#\n# DESCRIPTION\n#\n#   Check for baseline language coverage in the compiler for the specified\n#   version of the C++ standard.  If necessary, add switches to CXX and\n#   CXXCPP to enable support.  VERSION may be '11' (for the C++11 standard)\n#   or '14' (for the C++14 standard).\n#\n#   The second argument, if specified, indicates whether you insist on an\n#   extended mode (e.g. -std=gnu++11) or a strict conformance mode (e.g.\n#   -std=c++11).  If neither is specified, you get whatever works, with\n#   preference for an extended mode.\n#\n#   The third argument, if specified 'mandatory' or if left unspecified,\n#   indicates that baseline support for the specified C++ standard is\n#   required and that the macro should error out if no mode with that\n#   support is found.  If specified 'optional', then configuration proceeds\n#   regardless, after defining HAVE_CXX${VERSION} if and only if a\n#   supporting mode is found.\n#\n# LICENSE\n#\n#   Copyright (c) 2008 Benjamin Kosnik <bkoz@redhat.com>\n#   Copyright (c) 2012 Zack Weinberg <zackw@panix.com>\n#   Copyright (c) 2013 Roy Stogner <roystgnr@ices.utexas.edu>\n#   Copyright (c) 2014, 2015 Google Inc.; contributed by Alexey Sokolov <sokolov@google.com>\n#   Copyright (c) 2015 Paul Norman <penorman@mac.com>\n#   Copyright (c) 2015 Moritz Klammler <moritz@klammler.eu>\n#\n#   Copying and distribution of this file, with or without modification, are\n#   permitted in any medium without royalty provided the copyright notice\n#   and this notice are preserved.  This file is offered as-is, without any\n#   warranty.\n\n#serial 4\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n  ac_ext=cpp\nac_cpp='$CXXCPP $CPPFLAGS'\nac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'\nac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'\nac_compiler_gnu=$ac_cv_cxx_compiler_gnu\nif test -z \"$CXX\"; then\n  if test -n \"$CCC\"; then\n    CXX=$CCC\n  else\n    if test -n \"$ac_tool_prefix\"; then\n  for ac_prog in g++ c++ gpp aCC CC cxx cc++ cl.exe FCC KCC RCC xlC_r xlC\n  do\n    # Extract the first word of \"$ac_tool_prefix$ac_prog\", so it can be a program name with args.\nset dummy $ac_tool_prefix$ac_prog; ac_word=$2\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for $ac_word\" >&5\n$as_echo_n \"checking for $ac_word... \" >&6; }\nif ${ac_cv_prog_CXX+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  if test -n \"$CXX\"; then\n  ac_cv_prog_CXX=\"$CXX\" # Let the user override the test.\nelse\nas_save_IFS=$IFS; IFS=$PATH_SEPARATOR\nfor as_dir in $PATH\ndo\n  IFS=$as_save_IFS\n  test -z \"$as_dir\" && as_dir=.\n    for ac_exec_ext in '' $ac_executable_extensions; do\n  if as_fn_executable_p \"$as_dir/$ac_word$ac_exec_ext\"; then\n    ac_cv_prog_CXX=\"$ac_tool_prefix$ac_prog\"\n    $as_echo \"$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext\" >&5\n    break 2\n  fi\ndone\n  done\nIFS=$as_save_IFS\n\nfi\nfi\nCXX=$ac_cv_prog_CXX\nif test -n \"$CXX\"; then\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $CXX\" >&5\n$as_echo \"$CXX\" >&6; }\nelse\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\nfi\n\n\n    test -n \"$CXX\" && break\n  done\nfi\nif test -z \"$CXX\"; then\n  ac_ct_CXX=$CXX\n  for ac_prog in g++ c++ gpp aCC CC cxx cc++ cl.exe FCC KCC RCC xlC_r xlC\ndo\n  # Extract the first word of \"$ac_prog\", so it can be a program name with args.\nset dummy $ac_prog; ac_word=$2\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for $ac_word\" >&5\n$as_echo_n \"checking for $ac_word... \" >&6; }\nif ${ac_cv_prog_ac_ct_CXX+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  if test -n \"$ac_ct_CXX\"; then\n  ac_cv_prog_ac_ct_CXX=\"$ac_ct_CXX\" # Let the user override the test.\nelse\nas_save_IFS=$IFS; IFS=$PATH_SEPARATOR\nfor as_dir in $PATH\ndo\n  IFS=$as_save_IFS\n  test -z \"$as_dir\" && as_dir=.\n    for ac_exec_ext in '' $ac_executable_extensions; do\n  if as_fn_executable_p \"$as_dir/$ac_word$ac_exec_ext\"; then\n    ac_cv_prog_ac_ct_CXX=\"$ac_prog\"\n    $as_echo \"$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext\" >&5\n    break 2\n  fi\ndone\n  done\nIFS=$as_save_IFS\n\nfi\nfi\nac_ct_CXX=$ac_cv_prog_ac_ct_CXX\nif test -n \"$ac_ct_CXX\"; then\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_ct_CXX\" >&5\n$as_echo \"$ac_ct_CXX\" >&6; }\nelse\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\nfi\n\n\n  test -n \"$ac_ct_CXX\" && break\ndone\n\n  if test \"x$ac_ct_CXX\" = x; then\n    CXX=\"g++\"\n  else\n    case $cross_compiling:$ac_tool_warned in\nyes:)\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not prefixed with host triplet\" >&5\n$as_echo \"$as_me: WARNING: using cross tools not prefixed with host triplet\" >&2;}\nac_tool_warned=yes ;;\nesac\n    CXX=$ac_ct_CXX\n  fi\nfi\n\n  fi\nfi\n# Provide some information about the compiler.\n$as_echo \"$as_me:${as_lineno-$LINENO}: checking for C++ compiler version\" >&5\nset X $ac_compile\nac_compiler=$2\nfor ac_option in --version -v -V -qversion; do\n  { { ac_try=\"$ac_compiler $ac_option >&5\"\ncase \"(($ac_try\" in\n  *\\\"* | *\\`* | *\\\\*) ac_try_echo=\\$ac_try;;\n  *) ac_try_echo=$ac_try;;\nesac\neval ac_try_echo=\"\\\"\\$as_me:${as_lineno-$LINENO}: $ac_try_echo\\\"\"\n$as_echo \"$ac_try_echo\"; } >&5\n  (eval \"$ac_compiler $ac_option >&5\") 2>conftest.err\n  ac_status=$?\n  if test -s conftest.err; then\n    sed '10a\\\n... rest of stderr output deleted ...\n         10q' conftest.err >conftest.er1\n    cat conftest.er1 >&5\n  fi\n  rm -f conftest.er1 conftest.err\n  $as_echo \"$as_me:${as_lineno-$LINENO}: \\$? = $ac_status\" >&5\n  test $ac_status = 0; }\ndone\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether we are using the GNU C++ compiler\" >&5\n$as_echo_n \"checking whether we are using the GNU C++ compiler... \" >&6; }\nif ${ac_cv_cxx_compiler_gnu+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\nint\nmain ()\n{\n#ifndef __GNUC__\n       choke me\n#endif\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_cxx_try_compile \"$LINENO\"; then :\n  ac_compiler_gnu=yes\nelse\n  ac_compiler_gnu=no\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nac_cv_cxx_compiler_gnu=$ac_compiler_gnu\n\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_cxx_compiler_gnu\" >&5\n$as_echo \"$ac_cv_cxx_compiler_gnu\" >&6; }\nif test $ac_compiler_gnu = yes; then\n  GXX=yes\nelse\n  GXX=\nfi\nac_test_CXXFLAGS=${CXXFLAGS+set}\nac_save_CXXFLAGS=$CXXFLAGS\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether $CXX accepts -g\" >&5\n$as_echo_n \"checking whether $CXX accepts -g... \" >&6; }\nif ${ac_cv_prog_cxx_g+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  ac_save_cxx_werror_flag=$ac_cxx_werror_flag\n   ac_cxx_werror_flag=yes\n   ac_cv_prog_cxx_g=no\n   CXXFLAGS=\"-g\"\n   cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\nint\nmain ()\n{\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_cxx_try_compile \"$LINENO\"; then :\n  ac_cv_prog_cxx_g=yes\nelse\n  CXXFLAGS=\"\"\n      cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\nint\nmain ()\n{\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_cxx_try_compile \"$LINENO\"; then :\n\nelse\n  ac_cxx_werror_flag=$ac_save_cxx_werror_flag\n\t CXXFLAGS=\"-g\"\n\t cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\nint\nmain ()\n{\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_cxx_try_compile \"$LINENO\"; then :\n  ac_cv_prog_cxx_g=yes\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\n   ac_cxx_werror_flag=$ac_save_cxx_werror_flag\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_prog_cxx_g\" >&5\n$as_echo \"$ac_cv_prog_cxx_g\" >&6; }\nif test \"$ac_test_CXXFLAGS\" = set; then\n  CXXFLAGS=$ac_save_CXXFLAGS\nelif test $ac_cv_prog_cxx_g = yes; then\n  if test \"$GXX\" = yes; then\n    CXXFLAGS=\"-g -O2\"\n  else\n    CXXFLAGS=\"-g\"\n  fi\nelse\n  if test \"$GXX\" = yes; then\n    CXXFLAGS=\"-O2\"\n  else\n    CXXFLAGS=\n  fi\nfi\nac_ext=cpp\nac_cpp='$CXXCPP $CPPFLAGS'\nac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'\nac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'\nac_compiler_gnu=$ac_cv_cxx_compiler_gnu\n\n\n      ax_cxx_compile_cxx14_required=false\n  ac_ext=cpp\nac_cpp='$CXXCPP $CPPFLAGS'\nac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'\nac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'\nac_compiler_gnu=$ac_cv_cxx_compiler_gnu\n  ac_success=no\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether $CXX supports C++14 features by default\" >&5\n$as_echo_n \"checking whether $CXX supports C++14 features by default... \" >&6; }\nif ${ax_cv_cxx_compile_cxx14+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\n// If the compiler admits that it is not ready for C++11, why torture it?\n// Hopefully, this will speed up the test.\n\n#ifndef __cplusplus\n\n#error \"This is not a C++ compiler\"\n\n#elif __cplusplus < 201103L\n\n#error \"This is not a C++11 compiler\"\n\n#else\n\nnamespace cxx11\n{\n\n  namespace test_static_assert\n  {\n\n    template <typename T>\n    struct check\n    {\n      static_assert(sizeof(int) <= sizeof(T), \"not big enough\");\n    };\n\n  }\n\n  namespace test_final_override\n  {\n\n    struct Base\n    {\n      virtual void f() {}\n    };\n\n    struct Derived : public Base\n    {\n      virtual void f() override {}\n    };\n\n  }\n\n  namespace test_double_right_angle_brackets\n  {\n\n    template < typename T >\n    struct check {};\n\n    typedef check<void> single_type;\n    typedef check<check<void>> double_type;\n    typedef check<check<check<void>>> triple_type;\n    typedef check<check<check<check<void>>>> quadruple_type;\n\n  }\n\n  namespace test_decltype\n  {\n\n    int\n    f()\n    {\n      int a = 1;\n      decltype(a) b = 2;\n      return a + b;\n    }\n\n  }\n\n  namespace test_type_deduction\n  {\n\n    template < typename T1, typename T2 >\n    struct is_same\n    {\n      static const bool value = false;\n    };\n\n    template < typename T >\n    struct is_same<T, T>\n    {\n      static const bool value = true;\n    };\n\n    template < typename T1, typename T2 >\n    auto\n    add(T1 a1, T2 a2) -> decltype(a1 + a2)\n    {\n      return a1 + a2;\n    }\n\n    int\n    test(const int c, volatile int v)\n    {\n      static_assert(is_same<int, decltype(0)>::value == true, \"\");\n      static_assert(is_same<int, decltype(c)>::value == false, \"\");\n      static_assert(is_same<int, decltype(v)>::value == false, \"\");\n      auto ac = c;\n      auto av = v;\n      auto sumi = ac + av + 'x';\n      auto sumf = ac + av + 1.0;\n      static_assert(is_same<int, decltype(ac)>::value == true, \"\");\n      static_assert(is_same<int, decltype(av)>::value == true, \"\");\n      static_assert(is_same<int, decltype(sumi)>::value == true, \"\");\n      static_assert(is_same<int, decltype(sumf)>::value == false, \"\");\n      static_assert(is_same<int, decltype(add(c, v))>::value == true, \"\");\n      return (sumf > 0.0) ? sumi : add(c, v);\n    }\n\n  }\n\n  namespace test_noexcept\n  {\n\n    int f() { return 0; }\n    int g() noexcept { return 0; }\n\n    static_assert(noexcept(f()) == false, \"\");\n    static_assert(noexcept(g()) == true, \"\");\n\n  }\n\n  namespace test_constexpr\n  {\n\n    template < typename CharT >\n    unsigned long constexpr\n    strlen_c_r(const CharT *const s, const unsigned long acc) noexcept\n    {\n      return *s ? strlen_c_r(s + 1, acc + 1) : acc;\n    }\n\n    template < typename CharT >\n    unsigned long constexpr\n    strlen_c(const CharT *const s) noexcept\n    {\n      return strlen_c_r(s, 0UL);\n    }\n\n    static_assert(strlen_c(\"\") == 0UL, \"\");\n    static_assert(strlen_c(\"1\") == 1UL, \"\");\n    static_assert(strlen_c(\"example\") == 7UL, \"\");\n    static_assert(strlen_c(\"another\\0example\") == 7UL, \"\");\n\n  }\n\n  namespace test_rvalue_references\n  {\n\n    template < int N >\n    struct answer\n    {\n      static constexpr int value = N;\n    };\n\n    answer<1> f(int&)       { return answer<1>(); }\n    answer<2> f(const int&) { return answer<2>(); }\n    answer<3> f(int&&)      { return answer<3>(); }\n\n    void\n    test()\n    {\n      int i = 0;\n      const int c = 0;\n      static_assert(decltype(f(i))::value == 1, \"\");\n      static_assert(decltype(f(c))::value == 2, \"\");\n      static_assert(decltype(f(0))::value == 3, \"\");\n    }\n\n  }\n\n  namespace test_uniform_initialization\n  {\n\n    struct test\n    {\n      static const int zero {};\n      static const int one {1};\n    };\n\n    static_assert(test::zero == 0, \"\");\n    static_assert(test::one == 1, \"\");\n\n  }\n\n  namespace test_lambdas\n  {\n\n    void\n    test1()\n    {\n      auto lambda1 = [](){};\n      auto lambda2 = lambda1;\n      lambda1();\n      lambda2();\n    }\n\n    int\n    test2()\n    {\n      auto a = [](int i, int j){ return i + j; }(1, 2);\n      auto b = []() -> int { return '0'; }();\n      auto c = [=](){ return a + b; }();\n      auto d = [&](){ return c; }();\n      auto e = [a, &b](int x) mutable {\n        const auto identity = [](int y){ return y; };\n        for (auto i = 0; i < a; ++i)\n          a += b--;\n        return x + identity(a + b);\n      }(0);\n      return a + b + c + d + e;\n    }\n\n    int\n    test3()\n    {\n      const auto nullary = [](){ return 0; };\n      const auto unary = [](int x){ return x; };\n      using nullary_t = decltype(nullary);\n      using unary_t = decltype(unary);\n      const auto higher1st = [](nullary_t f){ return f(); };\n      const auto higher2nd = [unary](nullary_t f1){\n        return [unary, f1](unary_t f2){ return f2(unary(f1())); };\n      };\n      return higher1st(nullary) + higher2nd(nullary)(unary);\n    }\n\n  }\n\n  namespace test_variadic_templates\n  {\n\n    template <int...>\n    struct sum;\n\n    template <int N0, int... N1toN>\n    struct sum<N0, N1toN...>\n    {\n      static constexpr auto value = N0 + sum<N1toN...>::value;\n    };\n\n    template <>\n    struct sum<>\n    {\n      static constexpr auto value = 0;\n    };\n\n    static_assert(sum<>::value == 0, \"\");\n    static_assert(sum<1>::value == 1, \"\");\n    static_assert(sum<23>::value == 23, \"\");\n    static_assert(sum<1, 2>::value == 3, \"\");\n    static_assert(sum<5, 5, 11>::value == 21, \"\");\n    static_assert(sum<2, 3, 5, 7, 11, 13>::value == 41, \"\");\n\n  }\n\n  // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae\n  // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function\n  // because of this.\n  namespace test_template_alias_sfinae\n  {\n\n    struct foo {};\n\n    template<typename T>\n    using member = typename T::member_type;\n\n    template<typename T>\n    void func(...) {}\n\n    template<typename T>\n    void func(member<T>*) {}\n\n    void test();\n\n    void test() { func<foo>(0); }\n\n  }\n\n}  // namespace cxx11\n\n#endif  // __cplusplus >= 201103L\n\n\n\n\n// If the compiler admits that it is not ready for C++14, why torture it?\n// Hopefully, this will speed up the test.\n\n#ifndef __cplusplus\n\n#error \"This is not a C++ compiler\"\n\n#elif __cplusplus < 201402L\n\n#error \"This is not a C++14 compiler\"\n\n#else\n\nnamespace cxx14\n{\n\n  namespace test_polymorphic_lambdas\n  {\n\n    int\n    test()\n    {\n      const auto lambda = [](auto&&... args){\n        const auto istiny = [](auto x){\n          return (sizeof(x) == 1UL) ? 1 : 0;\n        };\n        const int aretiny[] = { istiny(args)... };\n        return aretiny[0];\n      };\n      return lambda(1, 1L, 1.0f, '1');\n    }\n\n  }\n\n  namespace test_binary_literals\n  {\n\n    constexpr auto ivii = 0b0000000000101010;\n    static_assert(ivii == 42, \"wrong value\");\n\n  }\n\n  namespace test_generalized_constexpr\n  {\n\n    template < typename CharT >\n    constexpr unsigned long\n    strlen_c(const CharT *const s) noexcept\n    {\n      auto length = 0UL;\n      for (auto p = s; *p; ++p)\n        ++length;\n      return length;\n    }\n\n    static_assert(strlen_c(\"\") == 0UL, \"\");\n    static_assert(strlen_c(\"x\") == 1UL, \"\");\n    static_assert(strlen_c(\"test\") == 4UL, \"\");\n    static_assert(strlen_c(\"another\\0test\") == 7UL, \"\");\n\n  }\n\n  namespace test_lambda_init_capture\n  {\n\n    int\n    test()\n    {\n      auto x = 0;\n      const auto lambda1 = [a = x](int b){ return a + b; };\n      const auto lambda2 = [a = lambda1(x)](){ return a; };\n      return lambda2();\n    }\n\n  }\n\n  namespace test_digit_seperators\n  {\n\n    constexpr auto ten_million = 100'000'000;\n    static_assert(ten_million == 100000000, \"\");\n\n  }\n\n  namespace test_return_type_deduction\n  {\n\n    auto f(int& x) { return x; }\n    decltype(auto) g(int& x) { return x; }\n\n    template < typename T1, typename T2 >\n    struct is_same\n    {\n      static constexpr auto value = false;\n    };\n\n    template < typename T >\n    struct is_same<T, T>\n    {\n      static constexpr auto value = true;\n    };\n\n    int\n    test()\n    {\n      auto x = 0;\n      static_assert(is_same<int, decltype(f(x))>::value, \"\");\n      static_assert(is_same<int&, decltype(g(x))>::value, \"\");\n      return x;\n    }\n\n  }\n\n}  // namespace cxx14\n\n#endif  // __cplusplus >= 201402L\n\n\n\n_ACEOF\nif ac_fn_cxx_try_compile \"$LINENO\"; then :\n  ax_cv_cxx_compile_cxx14=yes\nelse\n  ax_cv_cxx_compile_cxx14=no\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ax_cv_cxx_compile_cxx14\" >&5\n$as_echo \"$ax_cv_cxx_compile_cxx14\" >&6; }\n  if test x$ax_cv_cxx_compile_cxx14 = xyes; then\n    ac_success=yes\n  fi\n\n\n\n    if test x$ac_success = xno; then\n                for switch in -std=c++14 -std=c++0x +std=c++14 \"-h std=c++14\"; do\n      cachevar=`$as_echo \"ax_cv_cxx_compile_cxx14_$switch\" | $as_tr_sh`\n      { $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether $CXX supports C++14 features with $switch\" >&5\n$as_echo_n \"checking whether $CXX supports C++14 features with $switch... \" >&6; }\nif eval \\${$cachevar+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  ac_save_CXX=\"$CXX\"\n         CXX=\"$CXX $switch\"\n         cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\n// If the compiler admits that it is not ready for C++11, why torture it?\n// Hopefully, this will speed up the test.\n\n#ifndef __cplusplus\n\n#error \"This is not a C++ compiler\"\n\n#elif __cplusplus < 201103L\n\n#error \"This is not a C++11 compiler\"\n\n#else\n\nnamespace cxx11\n{\n\n  namespace test_static_assert\n  {\n\n    template <typename T>\n    struct check\n    {\n      static_assert(sizeof(int) <= sizeof(T), \"not big enough\");\n    };\n\n  }\n\n  namespace test_final_override\n  {\n\n    struct Base\n    {\n      virtual void f() {}\n    };\n\n    struct Derived : public Base\n    {\n      virtual void f() override {}\n    };\n\n  }\n\n  namespace test_double_right_angle_brackets\n  {\n\n    template < typename T >\n    struct check {};\n\n    typedef check<void> single_type;\n    typedef check<check<void>> double_type;\n    typedef check<check<check<void>>> triple_type;\n    typedef check<check<check<check<void>>>> quadruple_type;\n\n  }\n\n  namespace test_decltype\n  {\n\n    int\n    f()\n    {\n      int a = 1;\n      decltype(a) b = 2;\n      return a + b;\n    }\n\n  }\n\n  namespace test_type_deduction\n  {\n\n    template < typename T1, typename T2 >\n    struct is_same\n    {\n      static const bool value = false;\n    };\n\n    template < typename T >\n    struct is_same<T, T>\n    {\n      static const bool value = true;\n    };\n\n    template < typename T1, typename T2 >\n    auto\n    add(T1 a1, T2 a2) -> decltype(a1 + a2)\n    {\n      return a1 + a2;\n    }\n\n    int\n    test(const int c, volatile int v)\n    {\n      static_assert(is_same<int, decltype(0)>::value == true, \"\");\n      static_assert(is_same<int, decltype(c)>::value == false, \"\");\n      static_assert(is_same<int, decltype(v)>::value == false, \"\");\n      auto ac = c;\n      auto av = v;\n      auto sumi = ac + av + 'x';\n      auto sumf = ac + av + 1.0;\n      static_assert(is_same<int, decltype(ac)>::value == true, \"\");\n      static_assert(is_same<int, decltype(av)>::value == true, \"\");\n      static_assert(is_same<int, decltype(sumi)>::value == true, \"\");\n      static_assert(is_same<int, decltype(sumf)>::value == false, \"\");\n      static_assert(is_same<int, decltype(add(c, v))>::value == true, \"\");\n      return (sumf > 0.0) ? sumi : add(c, v);\n    }\n\n  }\n\n  namespace test_noexcept\n  {\n\n    int f() { return 0; }\n    int g() noexcept { return 0; }\n\n    static_assert(noexcept(f()) == false, \"\");\n    static_assert(noexcept(g()) == true, \"\");\n\n  }\n\n  namespace test_constexpr\n  {\n\n    template < typename CharT >\n    unsigned long constexpr\n    strlen_c_r(const CharT *const s, const unsigned long acc) noexcept\n    {\n      return *s ? strlen_c_r(s + 1, acc + 1) : acc;\n    }\n\n    template < typename CharT >\n    unsigned long constexpr\n    strlen_c(const CharT *const s) noexcept\n    {\n      return strlen_c_r(s, 0UL);\n    }\n\n    static_assert(strlen_c(\"\") == 0UL, \"\");\n    static_assert(strlen_c(\"1\") == 1UL, \"\");\n    static_assert(strlen_c(\"example\") == 7UL, \"\");\n    static_assert(strlen_c(\"another\\0example\") == 7UL, \"\");\n\n  }\n\n  namespace test_rvalue_references\n  {\n\n    template < int N >\n    struct answer\n    {\n      static constexpr int value = N;\n    };\n\n    answer<1> f(int&)       { return answer<1>(); }\n    answer<2> f(const int&) { return answer<2>(); }\n    answer<3> f(int&&)      { return answer<3>(); }\n\n    void\n    test()\n    {\n      int i = 0;\n      const int c = 0;\n      static_assert(decltype(f(i))::value == 1, \"\");\n      static_assert(decltype(f(c))::value == 2, \"\");\n      static_assert(decltype(f(0))::value == 3, \"\");\n    }\n\n  }\n\n  namespace test_uniform_initialization\n  {\n\n    struct test\n    {\n      static const int zero {};\n      static const int one {1};\n    };\n\n    static_assert(test::zero == 0, \"\");\n    static_assert(test::one == 1, \"\");\n\n  }\n\n  namespace test_lambdas\n  {\n\n    void\n    test1()\n    {\n      auto lambda1 = [](){};\n      auto lambda2 = lambda1;\n      lambda1();\n      lambda2();\n    }\n\n    int\n    test2()\n    {\n      auto a = [](int i, int j){ return i + j; }(1, 2);\n      auto b = []() -> int { return '0'; }();\n      auto c = [=](){ return a + b; }();\n      auto d = [&](){ return c; }();\n      auto e = [a, &b](int x) mutable {\n        const auto identity = [](int y){ return y; };\n        for (auto i = 0; i < a; ++i)\n          a += b--;\n        return x + identity(a + b);\n      }(0);\n      return a + b + c + d + e;\n    }\n\n    int\n    test3()\n    {\n      const auto nullary = [](){ return 0; };\n      const auto unary = [](int x){ return x; };\n      using nullary_t = decltype(nullary);\n      using unary_t = decltype(unary);\n      const auto higher1st = [](nullary_t f){ return f(); };\n      const auto higher2nd = [unary](nullary_t f1){\n        return [unary, f1](unary_t f2){ return f2(unary(f1())); };\n      };\n      return higher1st(nullary) + higher2nd(nullary)(unary);\n    }\n\n  }\n\n  namespace test_variadic_templates\n  {\n\n    template <int...>\n    struct sum;\n\n    template <int N0, int... N1toN>\n    struct sum<N0, N1toN...>\n    {\n      static constexpr auto value = N0 + sum<N1toN...>::value;\n    };\n\n    template <>\n    struct sum<>\n    {\n      static constexpr auto value = 0;\n    };\n\n    static_assert(sum<>::value == 0, \"\");\n    static_assert(sum<1>::value == 1, \"\");\n    static_assert(sum<23>::value == 23, \"\");\n    static_assert(sum<1, 2>::value == 3, \"\");\n    static_assert(sum<5, 5, 11>::value == 21, \"\");\n    static_assert(sum<2, 3, 5, 7, 11, 13>::value == 41, \"\");\n\n  }\n\n  // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae\n  // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function\n  // because of this.\n  namespace test_template_alias_sfinae\n  {\n\n    struct foo {};\n\n    template<typename T>\n    using member = typename T::member_type;\n\n    template<typename T>\n    void func(...) {}\n\n    template<typename T>\n    void func(member<T>*) {}\n\n    void test();\n\n    void test() { func<foo>(0); }\n\n  }\n\n}  // namespace cxx11\n\n#endif  // __cplusplus >= 201103L\n\n\n\n\n// If the compiler admits that it is not ready for C++14, why torture it?\n// Hopefully, this will speed up the test.\n\n#ifndef __cplusplus\n\n#error \"This is not a C++ compiler\"\n\n#elif __cplusplus < 201402L\n\n#error \"This is not a C++14 compiler\"\n\n#else\n\nnamespace cxx14\n{\n\n  namespace test_polymorphic_lambdas\n  {\n\n    int\n    test()\n    {\n      const auto lambda = [](auto&&... args){\n        const auto istiny = [](auto x){\n          return (sizeof(x) == 1UL) ? 1 : 0;\n        };\n        const int aretiny[] = { istiny(args)... };\n        return aretiny[0];\n      };\n      return lambda(1, 1L, 1.0f, '1');\n    }\n\n  }\n\n  namespace test_binary_literals\n  {\n\n    constexpr auto ivii = 0b0000000000101010;\n    static_assert(ivii == 42, \"wrong value\");\n\n  }\n\n  namespace test_generalized_constexpr\n  {\n\n    template < typename CharT >\n    constexpr unsigned long\n    strlen_c(const CharT *const s) noexcept\n    {\n      auto length = 0UL;\n      for (auto p = s; *p; ++p)\n        ++length;\n      return length;\n    }\n\n    static_assert(strlen_c(\"\") == 0UL, \"\");\n    static_assert(strlen_c(\"x\") == 1UL, \"\");\n    static_assert(strlen_c(\"test\") == 4UL, \"\");\n    static_assert(strlen_c(\"another\\0test\") == 7UL, \"\");\n\n  }\n\n  namespace test_lambda_init_capture\n  {\n\n    int\n    test()\n    {\n      auto x = 0;\n      const auto lambda1 = [a = x](int b){ return a + b; };\n      const auto lambda2 = [a = lambda1(x)](){ return a; };\n      return lambda2();\n    }\n\n  }\n\n  namespace test_digit_seperators\n  {\n\n    constexpr auto ten_million = 100'000'000;\n    static_assert(ten_million == 100000000, \"\");\n\n  }\n\n  namespace test_return_type_deduction\n  {\n\n    auto f(int& x) { return x; }\n    decltype(auto) g(int& x) { return x; }\n\n    template < typename T1, typename T2 >\n    struct is_same\n    {\n      static constexpr auto value = false;\n    };\n\n    template < typename T >\n    struct is_same<T, T>\n    {\n      static constexpr auto value = true;\n    };\n\n    int\n    test()\n    {\n      auto x = 0;\n      static_assert(is_same<int, decltype(f(x))>::value, \"\");\n      static_assert(is_same<int&, decltype(g(x))>::value, \"\");\n      return x;\n    }\n\n  }\n\n}  // namespace cxx14\n\n#endif  // __cplusplus >= 201402L\n\n\n\n_ACEOF\nif ac_fn_cxx_try_compile \"$LINENO\"; then :\n  eval $cachevar=yes\nelse\n  eval $cachevar=no\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\n         CXX=\"$ac_save_CXX\"\nfi\neval ac_res=\\$$cachevar\n\t       { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_res\" >&5\n$as_echo \"$ac_res\" >&6; }\n      if eval test x\\$$cachevar = xyes; then\n        CXX=\"$CXX $switch\"\n        if test -n \"$CXXCPP\" ; then\n          CXXCPP=\"$CXXCPP $switch\"\n        fi\n        ac_success=yes\n        break\n      fi\n    done\n  fi\n  ac_ext=c\nac_cpp='$CPP $CPPFLAGS'\nac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'\nac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'\nac_compiler_gnu=$ac_cv_c_compiler_gnu\n\n  if test x$ax_cxx_compile_cxx14_required = xtrue; then\n    if test x$ac_success = xno; then\n      as_fn_error $? \"*** A compiler with support for C++14 language features is required.\" \"$LINENO\" 5\n    fi\n  fi\n  if test x$ac_success = xno; then\n    HAVE_CXX14=0\n    { $as_echo \"$as_me:${as_lineno-$LINENO}: No compiler with C++14 support was found\" >&5\n$as_echo \"$as_me: No compiler with C++14 support was found\" >&6;}\n  else\n    HAVE_CXX14=1\n\n$as_echo \"#define HAVE_CXX14 1\" >>confdefs.h\n\n  fi\n\n\n  if test \"x${HAVE_CXX14}\" = \"x1\" ; then\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -Wall\" >&5\n$as_echo_n \"checking whether compiler supports -Wall... \" >&6; }\nT_CONFIGURE_CXXFLAGS=\"${CONFIGURE_CXXFLAGS}\"\nT_APPEND_V=-Wall\n  if test \"x${CONFIGURE_CXXFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CXXFLAGS=\"${CONFIGURE_CXXFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CXXFLAGS=\"${CONFIGURE_CXXFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CXXFLAGS}\" = \"x\" -o \"x${SPECIFIED_CXXFLAGS}\" = \"x\" ; then\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS}${SPECIFIED_CXXFLAGS}\"\nelse\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS} ${SPECIFIED_CXXFLAGS}\"\nfi\n\nac_ext=cpp\nac_cpp='$CXXCPP $CPPFLAGS'\nac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'\nac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'\nac_compiler_gnu=$ac_cv_cxx_compiler_gnu\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_cxx_try_compile \"$LINENO\"; then :\n  je_cv_cxxflags_added=-Wall\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cxxflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CXXFLAGS=\"${T_CONFIGURE_CXXFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nac_ext=c\nac_cpp='$CPP $CPPFLAGS'\nac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'\nac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'\nac_compiler_gnu=$ac_cv_c_compiler_gnu\n\nif test \"x${CONFIGURE_CXXFLAGS}\" = \"x\" -o \"x${SPECIFIED_CXXFLAGS}\" = \"x\" ; then\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS}${SPECIFIED_CXXFLAGS}\"\nelse\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS} ${SPECIFIED_CXXFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -Wextra\" >&5\n$as_echo_n \"checking whether compiler supports -Wextra... \" >&6; }\nT_CONFIGURE_CXXFLAGS=\"${CONFIGURE_CXXFLAGS}\"\nT_APPEND_V=-Wextra\n  if test \"x${CONFIGURE_CXXFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CXXFLAGS=\"${CONFIGURE_CXXFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CXXFLAGS=\"${CONFIGURE_CXXFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CXXFLAGS}\" = \"x\" -o \"x${SPECIFIED_CXXFLAGS}\" = \"x\" ; then\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS}${SPECIFIED_CXXFLAGS}\"\nelse\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS} ${SPECIFIED_CXXFLAGS}\"\nfi\n\nac_ext=cpp\nac_cpp='$CXXCPP $CPPFLAGS'\nac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'\nac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'\nac_compiler_gnu=$ac_cv_cxx_compiler_gnu\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_cxx_try_compile \"$LINENO\"; then :\n  je_cv_cxxflags_added=-Wextra\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cxxflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CXXFLAGS=\"${T_CONFIGURE_CXXFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nac_ext=c\nac_cpp='$CPP $CPPFLAGS'\nac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'\nac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'\nac_compiler_gnu=$ac_cv_c_compiler_gnu\n\nif test \"x${CONFIGURE_CXXFLAGS}\" = \"x\" -o \"x${SPECIFIED_CXXFLAGS}\" = \"x\" ; then\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS}${SPECIFIED_CXXFLAGS}\"\nelse\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS} ${SPECIFIED_CXXFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -g3\" >&5\n$as_echo_n \"checking whether compiler supports -g3... \" >&6; }\nT_CONFIGURE_CXXFLAGS=\"${CONFIGURE_CXXFLAGS}\"\nT_APPEND_V=-g3\n  if test \"x${CONFIGURE_CXXFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CXXFLAGS=\"${CONFIGURE_CXXFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CXXFLAGS=\"${CONFIGURE_CXXFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CXXFLAGS}\" = \"x\" -o \"x${SPECIFIED_CXXFLAGS}\" = \"x\" ; then\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS}${SPECIFIED_CXXFLAGS}\"\nelse\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS} ${SPECIFIED_CXXFLAGS}\"\nfi\n\nac_ext=cpp\nac_cpp='$CXXCPP $CPPFLAGS'\nac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'\nac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'\nac_compiler_gnu=$ac_cv_cxx_compiler_gnu\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_cxx_try_compile \"$LINENO\"; then :\n  je_cv_cxxflags_added=-g3\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cxxflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CXXFLAGS=\"${T_CONFIGURE_CXXFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nac_ext=c\nac_cpp='$CPP $CPPFLAGS'\nac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'\nac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'\nac_compiler_gnu=$ac_cv_c_compiler_gnu\n\nif test \"x${CONFIGURE_CXXFLAGS}\" = \"x\" -o \"x${SPECIFIED_CXXFLAGS}\" = \"x\" ; then\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS}${SPECIFIED_CXXFLAGS}\"\nelse\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS} ${SPECIFIED_CXXFLAGS}\"\nfi\n\n\n\n    SAVED_LIBS=\"${LIBS}\"\n    T_APPEND_V=-lstdc++\n  if test \"x${LIBS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  LIBS=\"${LIBS}${T_APPEND_V}\"\nelse\n  LIBS=\"${LIBS} ${T_APPEND_V}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether libstdc++ linkage is compilable\" >&5\n$as_echo_n \"checking whether libstdc++ linkage is compilable... \" >&6; }\nif ${je_cv_libstdcxx+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n#include <stdlib.h>\n\nint\nmain ()\n{\n\n\tint *arr = (int *)malloc(sizeof(int) * 42);\n\tif (arr == NULL)\n\t\treturn 1;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_libstdcxx=yes\nelse\n  je_cv_libstdcxx=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_libstdcxx\" >&5\n$as_echo \"$je_cv_libstdcxx\" >&6; }\n\n    if test \"x${je_cv_libstdcxx}\" = \"xno\" ; then\n      LIBS=\"${SAVED_LIBS}\"\n    fi\n  else\n    enable_cxx=\"0\"\n  fi\nfi\n\n\n\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for grep that handles long lines and -e\" >&5\n$as_echo_n \"checking for grep that handles long lines and -e... \" >&6; }\nif ${ac_cv_path_GREP+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  if test -z \"$GREP\"; then\n  ac_path_GREP_found=false\n  # Loop through the user's path and test for each of PROGNAME-LIST\n  as_save_IFS=$IFS; IFS=$PATH_SEPARATOR\nfor as_dir in $PATH$PATH_SEPARATOR/usr/xpg4/bin\ndo\n  IFS=$as_save_IFS\n  test -z \"$as_dir\" && as_dir=.\n    for ac_prog in grep ggrep; do\n    for ac_exec_ext in '' $ac_executable_extensions; do\n      ac_path_GREP=\"$as_dir/$ac_prog$ac_exec_ext\"\n      as_fn_executable_p \"$ac_path_GREP\" || continue\n# Check for GNU ac_path_GREP and select it if it is found.\n  # Check for GNU $ac_path_GREP\ncase `\"$ac_path_GREP\" --version 2>&1` in\n*GNU*)\n  ac_cv_path_GREP=\"$ac_path_GREP\" ac_path_GREP_found=:;;\n*)\n  ac_count=0\n  $as_echo_n 0123456789 >\"conftest.in\"\n  while :\n  do\n    cat \"conftest.in\" \"conftest.in\" >\"conftest.tmp\"\n    mv \"conftest.tmp\" \"conftest.in\"\n    cp \"conftest.in\" \"conftest.nl\"\n    $as_echo 'GREP' >> \"conftest.nl\"\n    \"$ac_path_GREP\" -e 'GREP$' -e '-(cannot match)-' < \"conftest.nl\" >\"conftest.out\" 2>/dev/null || break\n    diff \"conftest.out\" \"conftest.nl\" >/dev/null 2>&1 || break\n    as_fn_arith $ac_count + 1 && ac_count=$as_val\n    if test $ac_count -gt ${ac_path_GREP_max-0}; then\n      # Best one so far, save it but keep looking for a better one\n      ac_cv_path_GREP=\"$ac_path_GREP\"\n      ac_path_GREP_max=$ac_count\n    fi\n    # 10*(2^10) chars as input seems more than enough\n    test $ac_count -gt 10 && break\n  done\n  rm -f conftest.in conftest.tmp conftest.nl conftest.out;;\nesac\n\n      $ac_path_GREP_found && break 3\n    done\n  done\n  done\nIFS=$as_save_IFS\n  if test -z \"$ac_cv_path_GREP\"; then\n    as_fn_error $? \"no acceptable grep could be found in $PATH$PATH_SEPARATOR/usr/xpg4/bin\" \"$LINENO\" 5\n  fi\nelse\n  ac_cv_path_GREP=$GREP\nfi\n\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_path_GREP\" >&5\n$as_echo \"$ac_cv_path_GREP\" >&6; }\n GREP=\"$ac_cv_path_GREP\"\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for egrep\" >&5\n$as_echo_n \"checking for egrep... \" >&6; }\nif ${ac_cv_path_EGREP+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  if echo a | $GREP -E '(a|b)' >/dev/null 2>&1\n   then ac_cv_path_EGREP=\"$GREP -E\"\n   else\n     if test -z \"$EGREP\"; then\n  ac_path_EGREP_found=false\n  # Loop through the user's path and test for each of PROGNAME-LIST\n  as_save_IFS=$IFS; IFS=$PATH_SEPARATOR\nfor as_dir in $PATH$PATH_SEPARATOR/usr/xpg4/bin\ndo\n  IFS=$as_save_IFS\n  test -z \"$as_dir\" && as_dir=.\n    for ac_prog in egrep; do\n    for ac_exec_ext in '' $ac_executable_extensions; do\n      ac_path_EGREP=\"$as_dir/$ac_prog$ac_exec_ext\"\n      as_fn_executable_p \"$ac_path_EGREP\" || continue\n# Check for GNU ac_path_EGREP and select it if it is found.\n  # Check for GNU $ac_path_EGREP\ncase `\"$ac_path_EGREP\" --version 2>&1` in\n*GNU*)\n  ac_cv_path_EGREP=\"$ac_path_EGREP\" ac_path_EGREP_found=:;;\n*)\n  ac_count=0\n  $as_echo_n 0123456789 >\"conftest.in\"\n  while :\n  do\n    cat \"conftest.in\" \"conftest.in\" >\"conftest.tmp\"\n    mv \"conftest.tmp\" \"conftest.in\"\n    cp \"conftest.in\" \"conftest.nl\"\n    $as_echo 'EGREP' >> \"conftest.nl\"\n    \"$ac_path_EGREP\" 'EGREP$' < \"conftest.nl\" >\"conftest.out\" 2>/dev/null || break\n    diff \"conftest.out\" \"conftest.nl\" >/dev/null 2>&1 || break\n    as_fn_arith $ac_count + 1 && ac_count=$as_val\n    if test $ac_count -gt ${ac_path_EGREP_max-0}; then\n      # Best one so far, save it but keep looking for a better one\n      ac_cv_path_EGREP=\"$ac_path_EGREP\"\n      ac_path_EGREP_max=$ac_count\n    fi\n    # 10*(2^10) chars as input seems more than enough\n    test $ac_count -gt 10 && break\n  done\n  rm -f conftest.in conftest.tmp conftest.nl conftest.out;;\nesac\n\n      $ac_path_EGREP_found && break 3\n    done\n  done\n  done\nIFS=$as_save_IFS\n  if test -z \"$ac_cv_path_EGREP\"; then\n    as_fn_error $? \"no acceptable egrep could be found in $PATH$PATH_SEPARATOR/usr/xpg4/bin\" \"$LINENO\" 5\n  fi\nelse\n  ac_cv_path_EGREP=$EGREP\nfi\n\n   fi\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_path_EGREP\" >&5\n$as_echo \"$ac_cv_path_EGREP\" >&6; }\n EGREP=\"$ac_cv_path_EGREP\"\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for ANSI C header files\" >&5\n$as_echo_n \"checking for ANSI C header files... \" >&6; }\nif ${ac_cv_header_stdc+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n#include <stdlib.h>\n#include <stdarg.h>\n#include <string.h>\n#include <float.h>\n\nint\nmain ()\n{\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  ac_cv_header_stdc=yes\nelse\n  ac_cv_header_stdc=no\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\n\nif test $ac_cv_header_stdc = yes; then\n  # SunOS 4.x string.h does not declare mem*, contrary to ANSI.\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n#include <string.h>\n\n_ACEOF\nif (eval \"$ac_cpp conftest.$ac_ext\") 2>&5 |\n  $EGREP \"memchr\" >/dev/null 2>&1; then :\n\nelse\n  ac_cv_header_stdc=no\nfi\nrm -f conftest*\n\nfi\n\nif test $ac_cv_header_stdc = yes; then\n  # ISC 2.0.2 stdlib.h does not declare free, contrary to ANSI.\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n#include <stdlib.h>\n\n_ACEOF\nif (eval \"$ac_cpp conftest.$ac_ext\") 2>&5 |\n  $EGREP \"free\" >/dev/null 2>&1; then :\n\nelse\n  ac_cv_header_stdc=no\nfi\nrm -f conftest*\n\nfi\n\nif test $ac_cv_header_stdc = yes; then\n  # /bin/cc in Irix-4.0.5 gets non-ANSI ctype macros unless using -ansi.\n  if test \"$cross_compiling\" = yes; then :\n  :\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n#include <ctype.h>\n#include <stdlib.h>\n#if ((' ' & 0x0FF) == 0x020)\n# define ISLOWER(c) ('a' <= (c) && (c) <= 'z')\n# define TOUPPER(c) (ISLOWER(c) ? 'A' + ((c) - 'a') : (c))\n#else\n# define ISLOWER(c) \\\n\t\t   (('a' <= (c) && (c) <= 'i') \\\n\t\t     || ('j' <= (c) && (c) <= 'r') \\\n\t\t     || ('s' <= (c) && (c) <= 'z'))\n# define TOUPPER(c) (ISLOWER(c) ? ((c) | 0x40) : (c))\n#endif\n\n#define XOR(e, f) (((e) && !(f)) || (!(e) && (f)))\nint\nmain ()\n{\n  int i;\n  for (i = 0; i < 256; i++)\n    if (XOR (islower (i), ISLOWER (i))\n\t|| toupper (i) != TOUPPER (i))\n      return 2;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_run \"$LINENO\"; then :\n\nelse\n  ac_cv_header_stdc=no\nfi\nrm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext \\\n  conftest.$ac_objext conftest.beam conftest.$ac_ext\nfi\n\nfi\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_header_stdc\" >&5\n$as_echo \"$ac_cv_header_stdc\" >&6; }\nif test $ac_cv_header_stdc = yes; then\n\n$as_echo \"#define STDC_HEADERS 1\" >>confdefs.h\n\nfi\n\n# On IRIX 5.3, sys/types and inttypes.h are conflicting.\nfor ac_header in sys/types.h sys/stat.h stdlib.h string.h memory.h strings.h \\\n\t\t  inttypes.h stdint.h unistd.h\ndo :\n  as_ac_Header=`$as_echo \"ac_cv_header_$ac_header\" | $as_tr_sh`\nac_fn_c_check_header_compile \"$LINENO\" \"$ac_header\" \"$as_ac_Header\" \"$ac_includes_default\n\"\nif eval test \\\"x\\$\"$as_ac_Header\"\\\" = x\"yes\"; then :\n  cat >>confdefs.h <<_ACEOF\n#define `$as_echo \"HAVE_$ac_header\" | $as_tr_cpp` 1\n_ACEOF\n\nfi\n\ndone\n\n\n { $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether byte ordering is bigendian\" >&5\n$as_echo_n \"checking whether byte ordering is bigendian... \" >&6; }\nif ${ac_cv_c_bigendian+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  ac_cv_c_bigendian=unknown\n    # See if we're dealing with a universal compiler.\n    cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n#ifndef __APPLE_CC__\n\t       not a universal capable compiler\n\t     #endif\n\t     typedef int dummy;\n\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n\n\t# Check for potential -arch flags.  It is not universal unless\n\t# there are at least two -arch flags with different values.\n\tac_arch=\n\tac_prev=\n\tfor ac_word in $CC $CFLAGS $CPPFLAGS $LDFLAGS; do\n\t if test -n \"$ac_prev\"; then\n\t   case $ac_word in\n\t     i?86 | x86_64 | ppc | ppc64)\n\t       if test -z \"$ac_arch\" || test \"$ac_arch\" = \"$ac_word\"; then\n\t\t ac_arch=$ac_word\n\t       else\n\t\t ac_cv_c_bigendian=universal\n\t\t break\n\t       fi\n\t       ;;\n\t   esac\n\t   ac_prev=\n\t elif test \"x$ac_word\" = \"x-arch\"; then\n\t   ac_prev=arch\n\t fi\n       done\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\n    if test $ac_cv_c_bigendian = unknown; then\n      # See if sys/param.h defines the BYTE_ORDER macro.\n      cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n#include <sys/types.h>\n\t     #include <sys/param.h>\n\nint\nmain ()\n{\n#if ! (defined BYTE_ORDER && defined BIG_ENDIAN \\\n\t\t     && defined LITTLE_ENDIAN && BYTE_ORDER && BIG_ENDIAN \\\n\t\t     && LITTLE_ENDIAN)\n\t      bogus endian macros\n\t     #endif\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  # It does; now see whether it defined to BIG_ENDIAN or not.\n\t cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n#include <sys/types.h>\n\t\t#include <sys/param.h>\n\nint\nmain ()\n{\n#if BYTE_ORDER != BIG_ENDIAN\n\t\t not big endian\n\t\t#endif\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  ac_cv_c_bigendian=yes\nelse\n  ac_cv_c_bigendian=no\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\n    fi\n    if test $ac_cv_c_bigendian = unknown; then\n      # See if <limits.h> defines _LITTLE_ENDIAN or _BIG_ENDIAN (e.g., Solaris).\n      cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n#include <limits.h>\n\nint\nmain ()\n{\n#if ! (defined _LITTLE_ENDIAN || defined _BIG_ENDIAN)\n\t      bogus endian macros\n\t     #endif\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  # It does; now see whether it defined to _BIG_ENDIAN or not.\n\t cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n#include <limits.h>\n\nint\nmain ()\n{\n#ifndef _BIG_ENDIAN\n\t\t not big endian\n\t\t#endif\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  ac_cv_c_bigendian=yes\nelse\n  ac_cv_c_bigendian=no\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\n    fi\n    if test $ac_cv_c_bigendian = unknown; then\n      # Compile a test program.\n      if test \"$cross_compiling\" = yes; then :\n  # Try to guess by grepping values from an object file.\n\t cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\nshort int ascii_mm[] =\n\t\t  { 0x4249, 0x4765, 0x6E44, 0x6961, 0x6E53, 0x7953, 0 };\n\t\tshort int ascii_ii[] =\n\t\t  { 0x694C, 0x5454, 0x656C, 0x6E45, 0x6944, 0x6E61, 0 };\n\t\tint use_ascii (int i) {\n\t\t  return ascii_mm[i] + ascii_ii[i];\n\t\t}\n\t\tshort int ebcdic_ii[] =\n\t\t  { 0x89D3, 0xE3E3, 0x8593, 0x95C5, 0x89C4, 0x9581, 0 };\n\t\tshort int ebcdic_mm[] =\n\t\t  { 0xC2C9, 0xC785, 0x95C4, 0x8981, 0x95E2, 0xA8E2, 0 };\n\t\tint use_ebcdic (int i) {\n\t\t  return ebcdic_mm[i] + ebcdic_ii[i];\n\t\t}\n\t\textern int foo;\n\nint\nmain ()\n{\nreturn use_ascii (foo) == use_ebcdic (foo);\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  if grep BIGenDianSyS conftest.$ac_objext >/dev/null; then\n\t      ac_cv_c_bigendian=yes\n\t    fi\n\t    if grep LiTTleEnDian conftest.$ac_objext >/dev/null ; then\n\t      if test \"$ac_cv_c_bigendian\" = unknown; then\n\t\tac_cv_c_bigendian=no\n\t      else\n\t\t# finding both strings is unlikely to happen, but who knows?\n\t\tac_cv_c_bigendian=unknown\n\t      fi\n\t    fi\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n$ac_includes_default\nint\nmain ()\n{\n\n\t     /* Are we little or big endian?  From Harbison&Steele.  */\n\t     union\n\t     {\n\t       long int l;\n\t       char c[sizeof (long int)];\n\t     } u;\n\t     u.l = 1;\n\t     return u.c[sizeof (long int) - 1] == 1;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_run \"$LINENO\"; then :\n  ac_cv_c_bigendian=no\nelse\n  ac_cv_c_bigendian=yes\nfi\nrm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext \\\n  conftest.$ac_objext conftest.beam conftest.$ac_ext\nfi\n\n    fi\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_c_bigendian\" >&5\n$as_echo \"$ac_cv_c_bigendian\" >&6; }\n case $ac_cv_c_bigendian in #(\n   yes)\n     ac_cv_big_endian=1;; #(\n   no)\n     ac_cv_big_endian=0 ;; #(\n   universal)\n\n$as_echo \"#define AC_APPLE_UNIVERSAL_BUILD 1\" >>confdefs.h\n\n     ;; #(\n   *)\n     as_fn_error $? \"unknown endianness\n presetting ac_cv_c_bigendian=no (or yes) will help\" \"$LINENO\" 5 ;;\n esac\n\nif test \"x${ac_cv_big_endian}\" = \"x1\" ; then\n  cat >>confdefs.h <<_ACEOF\n#define JEMALLOC_BIG_ENDIAN\n_ACEOF\n\nfi\n\nif test \"x${je_cv_msvc}\" = \"xyes\" -a \"x${ac_cv_header_inttypes_h}\" = \"xno\"; then\n  T_APPEND_V=-I${srcdir}/include/msvc_compat/C99\n  if test \"x${CPPFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CPPFLAGS=\"${CPPFLAGS}${T_APPEND_V}\"\nelse\n  CPPFLAGS=\"${CPPFLAGS} ${T_APPEND_V}\"\nfi\n\n\nfi\n\nif test \"x${je_cv_msvc}\" = \"xyes\" ; then\n  LG_SIZEOF_PTR=LG_SIZEOF_PTR_WIN\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: Using a predefined value for sizeof(void *): 4 for 32-bit, 8 for 64-bit\" >&5\n$as_echo \"Using a predefined value for sizeof(void *): 4 for 32-bit, 8 for 64-bit\" >&6; }\nelse\n  # The cast to long int works around a bug in the HP C Compiler\n# version HP92453-01 B.11.11.23709.GP, which incorrectly rejects\n# declarations like `int a3[[(sizeof (unsigned char)) >= 0]];'.\n# This bug is HP SR number 8606223364.\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking size of void *\" >&5\n$as_echo_n \"checking size of void *... \" >&6; }\nif ${ac_cv_sizeof_void_p+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  if ac_fn_c_compute_int \"$LINENO\" \"(long int) (sizeof (void *))\" \"ac_cv_sizeof_void_p\"        \"$ac_includes_default\"; then :\n\nelse\n  if test \"$ac_cv_type_void_p\" = yes; then\n     { { $as_echo \"$as_me:${as_lineno-$LINENO}: error: in \\`$ac_pwd':\" >&5\n$as_echo \"$as_me: error: in \\`$ac_pwd':\" >&2;}\nas_fn_error 77 \"cannot compute sizeof (void *)\nSee \\`config.log' for more details\" \"$LINENO\" 5; }\n   else\n     ac_cv_sizeof_void_p=0\n   fi\nfi\n\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_sizeof_void_p\" >&5\n$as_echo \"$ac_cv_sizeof_void_p\" >&6; }\n\n\n\ncat >>confdefs.h <<_ACEOF\n#define SIZEOF_VOID_P $ac_cv_sizeof_void_p\n_ACEOF\n\n\n  if test \"x${ac_cv_sizeof_void_p}\" = \"x8\" ; then\n    LG_SIZEOF_PTR=3\n  elif test \"x${ac_cv_sizeof_void_p}\" = \"x4\" ; then\n    LG_SIZEOF_PTR=2\n  else\n    as_fn_error $? \"Unsupported pointer size: ${ac_cv_sizeof_void_p}\" \"$LINENO\" 5\n  fi\nfi\ncat >>confdefs.h <<_ACEOF\n#define LG_SIZEOF_PTR $LG_SIZEOF_PTR\n_ACEOF\n\n\n# The cast to long int works around a bug in the HP C Compiler\n# version HP92453-01 B.11.11.23709.GP, which incorrectly rejects\n# declarations like `int a3[[(sizeof (unsigned char)) >= 0]];'.\n# This bug is HP SR number 8606223364.\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking size of int\" >&5\n$as_echo_n \"checking size of int... \" >&6; }\nif ${ac_cv_sizeof_int+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  if ac_fn_c_compute_int \"$LINENO\" \"(long int) (sizeof (int))\" \"ac_cv_sizeof_int\"        \"$ac_includes_default\"; then :\n\nelse\n  if test \"$ac_cv_type_int\" = yes; then\n     { { $as_echo \"$as_me:${as_lineno-$LINENO}: error: in \\`$ac_pwd':\" >&5\n$as_echo \"$as_me: error: in \\`$ac_pwd':\" >&2;}\nas_fn_error 77 \"cannot compute sizeof (int)\nSee \\`config.log' for more details\" \"$LINENO\" 5; }\n   else\n     ac_cv_sizeof_int=0\n   fi\nfi\n\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_sizeof_int\" >&5\n$as_echo \"$ac_cv_sizeof_int\" >&6; }\n\n\n\ncat >>confdefs.h <<_ACEOF\n#define SIZEOF_INT $ac_cv_sizeof_int\n_ACEOF\n\n\nif test \"x${ac_cv_sizeof_int}\" = \"x8\" ; then\n  LG_SIZEOF_INT=3\nelif test \"x${ac_cv_sizeof_int}\" = \"x4\" ; then\n  LG_SIZEOF_INT=2\nelse\n  as_fn_error $? \"Unsupported int size: ${ac_cv_sizeof_int}\" \"$LINENO\" 5\nfi\ncat >>confdefs.h <<_ACEOF\n#define LG_SIZEOF_INT $LG_SIZEOF_INT\n_ACEOF\n\n\n# The cast to long int works around a bug in the HP C Compiler\n# version HP92453-01 B.11.11.23709.GP, which incorrectly rejects\n# declarations like `int a3[[(sizeof (unsigned char)) >= 0]];'.\n# This bug is HP SR number 8606223364.\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking size of long\" >&5\n$as_echo_n \"checking size of long... \" >&6; }\nif ${ac_cv_sizeof_long+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  if ac_fn_c_compute_int \"$LINENO\" \"(long int) (sizeof (long))\" \"ac_cv_sizeof_long\"        \"$ac_includes_default\"; then :\n\nelse\n  if test \"$ac_cv_type_long\" = yes; then\n     { { $as_echo \"$as_me:${as_lineno-$LINENO}: error: in \\`$ac_pwd':\" >&5\n$as_echo \"$as_me: error: in \\`$ac_pwd':\" >&2;}\nas_fn_error 77 \"cannot compute sizeof (long)\nSee \\`config.log' for more details\" \"$LINENO\" 5; }\n   else\n     ac_cv_sizeof_long=0\n   fi\nfi\n\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_sizeof_long\" >&5\n$as_echo \"$ac_cv_sizeof_long\" >&6; }\n\n\n\ncat >>confdefs.h <<_ACEOF\n#define SIZEOF_LONG $ac_cv_sizeof_long\n_ACEOF\n\n\nif test \"x${ac_cv_sizeof_long}\" = \"x8\" ; then\n  LG_SIZEOF_LONG=3\nelif test \"x${ac_cv_sizeof_long}\" = \"x4\" ; then\n  LG_SIZEOF_LONG=2\nelse\n  as_fn_error $? \"Unsupported long size: ${ac_cv_sizeof_long}\" \"$LINENO\" 5\nfi\ncat >>confdefs.h <<_ACEOF\n#define LG_SIZEOF_LONG $LG_SIZEOF_LONG\n_ACEOF\n\n\n# The cast to long int works around a bug in the HP C Compiler\n# version HP92453-01 B.11.11.23709.GP, which incorrectly rejects\n# declarations like `int a3[[(sizeof (unsigned char)) >= 0]];'.\n# This bug is HP SR number 8606223364.\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking size of long long\" >&5\n$as_echo_n \"checking size of long long... \" >&6; }\nif ${ac_cv_sizeof_long_long+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  if ac_fn_c_compute_int \"$LINENO\" \"(long int) (sizeof (long long))\" \"ac_cv_sizeof_long_long\"        \"$ac_includes_default\"; then :\n\nelse\n  if test \"$ac_cv_type_long_long\" = yes; then\n     { { $as_echo \"$as_me:${as_lineno-$LINENO}: error: in \\`$ac_pwd':\" >&5\n$as_echo \"$as_me: error: in \\`$ac_pwd':\" >&2;}\nas_fn_error 77 \"cannot compute sizeof (long long)\nSee \\`config.log' for more details\" \"$LINENO\" 5; }\n   else\n     ac_cv_sizeof_long_long=0\n   fi\nfi\n\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_sizeof_long_long\" >&5\n$as_echo \"$ac_cv_sizeof_long_long\" >&6; }\n\n\n\ncat >>confdefs.h <<_ACEOF\n#define SIZEOF_LONG_LONG $ac_cv_sizeof_long_long\n_ACEOF\n\n\nif test \"x${ac_cv_sizeof_long_long}\" = \"x8\" ; then\n  LG_SIZEOF_LONG_LONG=3\nelif test \"x${ac_cv_sizeof_long_long}\" = \"x4\" ; then\n  LG_SIZEOF_LONG_LONG=2\nelse\n  as_fn_error $? \"Unsupported long long size: ${ac_cv_sizeof_long_long}\" \"$LINENO\" 5\nfi\ncat >>confdefs.h <<_ACEOF\n#define LG_SIZEOF_LONG_LONG $LG_SIZEOF_LONG_LONG\n_ACEOF\n\n\n# The cast to long int works around a bug in the HP C Compiler\n# version HP92453-01 B.11.11.23709.GP, which incorrectly rejects\n# declarations like `int a3[[(sizeof (unsigned char)) >= 0]];'.\n# This bug is HP SR number 8606223364.\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking size of intmax_t\" >&5\n$as_echo_n \"checking size of intmax_t... \" >&6; }\nif ${ac_cv_sizeof_intmax_t+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  if ac_fn_c_compute_int \"$LINENO\" \"(long int) (sizeof (intmax_t))\" \"ac_cv_sizeof_intmax_t\"        \"$ac_includes_default\"; then :\n\nelse\n  if test \"$ac_cv_type_intmax_t\" = yes; then\n     { { $as_echo \"$as_me:${as_lineno-$LINENO}: error: in \\`$ac_pwd':\" >&5\n$as_echo \"$as_me: error: in \\`$ac_pwd':\" >&2;}\nas_fn_error 77 \"cannot compute sizeof (intmax_t)\nSee \\`config.log' for more details\" \"$LINENO\" 5; }\n   else\n     ac_cv_sizeof_intmax_t=0\n   fi\nfi\n\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_sizeof_intmax_t\" >&5\n$as_echo \"$ac_cv_sizeof_intmax_t\" >&6; }\n\n\n\ncat >>confdefs.h <<_ACEOF\n#define SIZEOF_INTMAX_T $ac_cv_sizeof_intmax_t\n_ACEOF\n\n\nif test \"x${ac_cv_sizeof_intmax_t}\" = \"x16\" ; then\n  LG_SIZEOF_INTMAX_T=4\nelif test \"x${ac_cv_sizeof_intmax_t}\" = \"x8\" ; then\n  LG_SIZEOF_INTMAX_T=3\nelif test \"x${ac_cv_sizeof_intmax_t}\" = \"x4\" ; then\n  LG_SIZEOF_INTMAX_T=2\nelse\n  as_fn_error $? \"Unsupported intmax_t size: ${ac_cv_sizeof_intmax_t}\" \"$LINENO\" 5\nfi\ncat >>confdefs.h <<_ACEOF\n#define LG_SIZEOF_INTMAX_T $LG_SIZEOF_INTMAX_T\n_ACEOF\n\n\n# Make sure we can run config.sub.\n$SHELL \"$ac_aux_dir/config.sub\" sun4 >/dev/null 2>&1 ||\n  as_fn_error $? \"cannot run $SHELL $ac_aux_dir/config.sub\" \"$LINENO\" 5\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking build system type\" >&5\n$as_echo_n \"checking build system type... \" >&6; }\nif ${ac_cv_build+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  ac_build_alias=$build_alias\ntest \"x$ac_build_alias\" = x &&\n  ac_build_alias=`$SHELL \"$ac_aux_dir/config.guess\"`\ntest \"x$ac_build_alias\" = x &&\n  as_fn_error $? \"cannot guess build type; you must specify one\" \"$LINENO\" 5\nac_cv_build=`$SHELL \"$ac_aux_dir/config.sub\" $ac_build_alias` ||\n  as_fn_error $? \"$SHELL $ac_aux_dir/config.sub $ac_build_alias failed\" \"$LINENO\" 5\n\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_build\" >&5\n$as_echo \"$ac_cv_build\" >&6; }\ncase $ac_cv_build in\n*-*-*) ;;\n*) as_fn_error $? \"invalid value of canonical build\" \"$LINENO\" 5;;\nesac\nbuild=$ac_cv_build\nac_save_IFS=$IFS; IFS='-'\nset x $ac_cv_build\nshift\nbuild_cpu=$1\nbuild_vendor=$2\nshift; shift\n# Remember, the first character of IFS is used to create $*,\n# except with old shells:\nbuild_os=$*\nIFS=$ac_save_IFS\ncase $build_os in *\\ *) build_os=`echo \"$build_os\" | sed 's/ /-/g'`;; esac\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking host system type\" >&5\n$as_echo_n \"checking host system type... \" >&6; }\nif ${ac_cv_host+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  if test \"x$host_alias\" = x; then\n  ac_cv_host=$ac_cv_build\nelse\n  ac_cv_host=`$SHELL \"$ac_aux_dir/config.sub\" $host_alias` ||\n    as_fn_error $? \"$SHELL $ac_aux_dir/config.sub $host_alias failed\" \"$LINENO\" 5\nfi\n\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_host\" >&5\n$as_echo \"$ac_cv_host\" >&6; }\ncase $ac_cv_host in\n*-*-*) ;;\n*) as_fn_error $? \"invalid value of canonical host\" \"$LINENO\" 5;;\nesac\nhost=$ac_cv_host\nac_save_IFS=$IFS; IFS='-'\nset x $ac_cv_host\nshift\nhost_cpu=$1\nhost_vendor=$2\nshift; shift\n# Remember, the first character of IFS is used to create $*,\n# except with old shells:\nhost_os=$*\nIFS=$ac_save_IFS\ncase $host_os in *\\ *) host_os=`echo \"$host_os\" | sed 's/ /-/g'`;; esac\n\n\nCPU_SPINWAIT=\"\"\ncase \"${host_cpu}\" in\n  i686|x86_64)\n\tHAVE_CPU_SPINWAIT=1\n\tif test \"x${je_cv_msvc}\" = \"xyes\" ; then\n\t    if ${je_cv_pause_msvc+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether pause instruction MSVC is compilable\" >&5\n$as_echo_n \"checking whether pause instruction MSVC is compilable... \" >&6; }\nif ${je_cv_pause_msvc+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\nint\nmain ()\n{\n_mm_pause(); return 0;\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_pause_msvc=yes\nelse\n  je_cv_pause_msvc=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_pause_msvc\" >&5\n$as_echo \"$je_cv_pause_msvc\" >&6; }\n\nfi\n\n\t    if test \"x${je_cv_pause_msvc}\" = \"xyes\" ; then\n\t\tCPU_SPINWAIT='_mm_pause()'\n\t    fi\n\telse\n\t    if ${je_cv_pause+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether pause instruction is compilable\" >&5\n$as_echo_n \"checking whether pause instruction is compilable... \" >&6; }\nif ${je_cv_pause+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\nint\nmain ()\n{\n__asm__ volatile(\"pause\"); return 0;\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_pause=yes\nelse\n  je_cv_pause=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_pause\" >&5\n$as_echo \"$je_cv_pause\" >&6; }\n\nfi\n\n\t    if test \"x${je_cv_pause}\" = \"xyes\" ; then\n\t\tCPU_SPINWAIT='__asm__ volatile(\"pause\")'\n\t    fi\n\tfi\n\t;;\n  *)\n\tHAVE_CPU_SPINWAIT=0\n\t;;\nesac\ncat >>confdefs.h <<_ACEOF\n#define HAVE_CPU_SPINWAIT $HAVE_CPU_SPINWAIT\n_ACEOF\n\ncat >>confdefs.h <<_ACEOF\n#define CPU_SPINWAIT $CPU_SPINWAIT\n_ACEOF\n\n\n\n# Check whether --with-lg_vaddr was given.\nif test \"${with_lg_vaddr+set}\" = set; then :\n  withval=$with_lg_vaddr; LG_VADDR=\"$with_lg_vaddr\"\nelse\n  LG_VADDR=\"detect\"\nfi\n\n\ncase \"${host_cpu}\" in\n  aarch64)\n    if test \"x$LG_VADDR\" = \"xdetect\"; then\n      { $as_echo \"$as_me:${as_lineno-$LINENO}: checking number of significant virtual address bits\" >&5\n$as_echo_n \"checking number of significant virtual address bits... \" >&6; }\n      if test \"x${LG_SIZEOF_PTR}\" = \"x2\" ; then\n        #aarch64 ILP32\n        LG_VADDR=32\n      else\n        #aarch64 LP64\n        LG_VADDR=48\n      fi\n      { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $LG_VADDR\" >&5\n$as_echo \"$LG_VADDR\" >&6; }\n    fi\n    ;;\n  x86_64)\n    if test \"x$LG_VADDR\" = \"xdetect\"; then\n      { $as_echo \"$as_me:${as_lineno-$LINENO}: checking number of significant virtual address bits\" >&5\n$as_echo_n \"checking number of significant virtual address bits... \" >&6; }\nif ${je_cv_lg_vaddr+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  if test \"$cross_compiling\" = yes; then :\n  je_cv_lg_vaddr=57\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n#include <stdio.h>\n#ifdef _WIN32\n#include <limits.h>\n#include <intrin.h>\ntypedef unsigned __int32 uint32_t;\n#else\n#include <stdint.h>\n#endif\n\nint\nmain ()\n{\n\n\tuint32_t r[4];\n\tuint32_t eax_in = 0x80000008U;\n#ifdef _WIN32\n\t__cpuid((int *)r, (int)eax_in);\n#else\n\tasm volatile (\"cpuid\"\n\t    : \"=a\" (r[0]), \"=b\" (r[1]), \"=c\" (r[2]), \"=d\" (r[3])\n\t    : \"a\" (eax_in), \"c\" (0)\n\t);\n#endif\n\tuint32_t eax_out = r[0];\n\tuint32_t vaddr = ((eax_out & 0x0000ff00U) >> 8);\n\tFILE *f = fopen(\"conftest.out\", \"w\");\n\tif (f == NULL) {\n\t\treturn 1;\n\t}\n\tif (vaddr > (sizeof(void *) << 3)) {\n\t\tvaddr = sizeof(void *) << 3;\n\t}\n\tfprintf(f, \"%u\", vaddr);\n\tfclose(f);\n\treturn 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_run \"$LINENO\"; then :\n  je_cv_lg_vaddr=`cat conftest.out`\nelse\n  je_cv_lg_vaddr=error\nfi\nrm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext \\\n  conftest.$ac_objext conftest.beam conftest.$ac_ext\nfi\n\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_lg_vaddr\" >&5\n$as_echo \"$je_cv_lg_vaddr\" >&6; }\n      if test \"x${je_cv_lg_vaddr}\" != \"x\" ; then\n        LG_VADDR=\"${je_cv_lg_vaddr}\"\n      fi\n      if test \"x${LG_VADDR}\" != \"xerror\" ; then\n        cat >>confdefs.h <<_ACEOF\n#define LG_VADDR $LG_VADDR\n_ACEOF\n\n      else\n        as_fn_error $? \"cannot determine number of significant virtual address bits\" \"$LINENO\" 5\n      fi\n    fi\n    ;;\n  *)\n    if test \"x$LG_VADDR\" = \"xdetect\"; then\n      { $as_echo \"$as_me:${as_lineno-$LINENO}: checking number of significant virtual address bits\" >&5\n$as_echo_n \"checking number of significant virtual address bits... \" >&6; }\n      if test \"x${LG_SIZEOF_PTR}\" = \"x3\" ; then\n        LG_VADDR=64\n      elif test \"x${LG_SIZEOF_PTR}\" = \"x2\" ; then\n        LG_VADDR=32\n      elif test \"x${LG_SIZEOF_PTR}\" = \"xLG_SIZEOF_PTR_WIN\" ; then\n        LG_VADDR=\"(1U << (LG_SIZEOF_PTR_WIN+3))\"\n      else\n        as_fn_error $? \"Unsupported lg(pointer size): ${LG_SIZEOF_PTR}\" \"$LINENO\" 5\n      fi\n      { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $LG_VADDR\" >&5\n$as_echo \"$LG_VADDR\" >&6; }\n    fi\n    ;;\nesac\ncat >>confdefs.h <<_ACEOF\n#define LG_VADDR $LG_VADDR\n_ACEOF\n\n\nLD_PRELOAD_VAR=\"LD_PRELOAD\"\nso=\"so\"\nimportlib=\"${so}\"\no=\"$ac_objext\"\na=\"a\"\nexe=\"$ac_exeext\"\nlibprefix=\"lib\"\nlink_whole_archive=\"0\"\nDSO_LDFLAGS='-shared -Wl,-soname,$(@F)'\nRPATH='-Wl,-rpath,$(1)'\nSOREV=\"${so}.${rev}\"\nPIC_CFLAGS='-fPIC -DPIC'\nCTARGET='-o $@'\nLDTARGET='-o $@'\nTEST_LD_MODE=\nEXTRA_LDFLAGS=\nARFLAGS='crus'\nAROUT=' $@'\nCC_MM=1\n\nif test \"x$je_cv_cray_prgenv_wrapper\" = \"xyes\" ; then\n  TEST_LD_MODE='-dynamic'\nfi\n\nif test \"x${je_cv_cray}\" = \"xyes\" ; then\n  CC_MM=\nfi\n\n\n\n\nif test -n \"$ac_tool_prefix\"; then\n  # Extract the first word of \"${ac_tool_prefix}ar\", so it can be a program name with args.\nset dummy ${ac_tool_prefix}ar; ac_word=$2\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for $ac_word\" >&5\n$as_echo_n \"checking for $ac_word... \" >&6; }\nif ${ac_cv_prog_AR+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  if test -n \"$AR\"; then\n  ac_cv_prog_AR=\"$AR\" # Let the user override the test.\nelse\nas_save_IFS=$IFS; IFS=$PATH_SEPARATOR\nfor as_dir in $PATH\ndo\n  IFS=$as_save_IFS\n  test -z \"$as_dir\" && as_dir=.\n    for ac_exec_ext in '' $ac_executable_extensions; do\n  if as_fn_executable_p \"$as_dir/$ac_word$ac_exec_ext\"; then\n    ac_cv_prog_AR=\"${ac_tool_prefix}ar\"\n    $as_echo \"$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext\" >&5\n    break 2\n  fi\ndone\n  done\nIFS=$as_save_IFS\n\nfi\nfi\nAR=$ac_cv_prog_AR\nif test -n \"$AR\"; then\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $AR\" >&5\n$as_echo \"$AR\" >&6; }\nelse\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\nfi\n\n\nfi\nif test -z \"$ac_cv_prog_AR\"; then\n  ac_ct_AR=$AR\n  # Extract the first word of \"ar\", so it can be a program name with args.\nset dummy ar; ac_word=$2\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for $ac_word\" >&5\n$as_echo_n \"checking for $ac_word... \" >&6; }\nif ${ac_cv_prog_ac_ct_AR+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  if test -n \"$ac_ct_AR\"; then\n  ac_cv_prog_ac_ct_AR=\"$ac_ct_AR\" # Let the user override the test.\nelse\nas_save_IFS=$IFS; IFS=$PATH_SEPARATOR\nfor as_dir in $PATH\ndo\n  IFS=$as_save_IFS\n  test -z \"$as_dir\" && as_dir=.\n    for ac_exec_ext in '' $ac_executable_extensions; do\n  if as_fn_executable_p \"$as_dir/$ac_word$ac_exec_ext\"; then\n    ac_cv_prog_ac_ct_AR=\"ar\"\n    $as_echo \"$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext\" >&5\n    break 2\n  fi\ndone\n  done\nIFS=$as_save_IFS\n\nfi\nfi\nac_ct_AR=$ac_cv_prog_ac_ct_AR\nif test -n \"$ac_ct_AR\"; then\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_ct_AR\" >&5\n$as_echo \"$ac_ct_AR\" >&6; }\nelse\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\nfi\n\n  if test \"x$ac_ct_AR\" = x; then\n    AR=\":\"\n  else\n    case $cross_compiling:$ac_tool_warned in\nyes:)\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not prefixed with host triplet\" >&5\n$as_echo \"$as_me: WARNING: using cross tools not prefixed with host triplet\" >&2;}\nac_tool_warned=yes ;;\nesac\n    AR=$ac_ct_AR\n  fi\nelse\n  AR=\"$ac_cv_prog_AR\"\nfi\n\n\n\n\n\nif test -n \"$ac_tool_prefix\"; then\n  # Extract the first word of \"${ac_tool_prefix}nm\", so it can be a program name with args.\nset dummy ${ac_tool_prefix}nm; ac_word=$2\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for $ac_word\" >&5\n$as_echo_n \"checking for $ac_word... \" >&6; }\nif ${ac_cv_prog_NM+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  if test -n \"$NM\"; then\n  ac_cv_prog_NM=\"$NM\" # Let the user override the test.\nelse\nas_save_IFS=$IFS; IFS=$PATH_SEPARATOR\nfor as_dir in $PATH\ndo\n  IFS=$as_save_IFS\n  test -z \"$as_dir\" && as_dir=.\n    for ac_exec_ext in '' $ac_executable_extensions; do\n  if as_fn_executable_p \"$as_dir/$ac_word$ac_exec_ext\"; then\n    ac_cv_prog_NM=\"${ac_tool_prefix}nm\"\n    $as_echo \"$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext\" >&5\n    break 2\n  fi\ndone\n  done\nIFS=$as_save_IFS\n\nfi\nfi\nNM=$ac_cv_prog_NM\nif test -n \"$NM\"; then\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $NM\" >&5\n$as_echo \"$NM\" >&6; }\nelse\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\nfi\n\n\nfi\nif test -z \"$ac_cv_prog_NM\"; then\n  ac_ct_NM=$NM\n  # Extract the first word of \"nm\", so it can be a program name with args.\nset dummy nm; ac_word=$2\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for $ac_word\" >&5\n$as_echo_n \"checking for $ac_word... \" >&6; }\nif ${ac_cv_prog_ac_ct_NM+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  if test -n \"$ac_ct_NM\"; then\n  ac_cv_prog_ac_ct_NM=\"$ac_ct_NM\" # Let the user override the test.\nelse\nas_save_IFS=$IFS; IFS=$PATH_SEPARATOR\nfor as_dir in $PATH\ndo\n  IFS=$as_save_IFS\n  test -z \"$as_dir\" && as_dir=.\n    for ac_exec_ext in '' $ac_executable_extensions; do\n  if as_fn_executable_p \"$as_dir/$ac_word$ac_exec_ext\"; then\n    ac_cv_prog_ac_ct_NM=\"nm\"\n    $as_echo \"$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext\" >&5\n    break 2\n  fi\ndone\n  done\nIFS=$as_save_IFS\n\nfi\nfi\nac_ct_NM=$ac_cv_prog_ac_ct_NM\nif test -n \"$ac_ct_NM\"; then\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_ct_NM\" >&5\n$as_echo \"$ac_ct_NM\" >&6; }\nelse\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\nfi\n\n  if test \"x$ac_ct_NM\" = x; then\n    NM=\":\"\n  else\n    case $cross_compiling:$ac_tool_warned in\nyes:)\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not prefixed with host triplet\" >&5\n$as_echo \"$as_me: WARNING: using cross tools not prefixed with host triplet\" >&2;}\nac_tool_warned=yes ;;\nesac\n    NM=$ac_ct_NM\n  fi\nelse\n  NM=\"$ac_cv_prog_NM\"\nfi\n\n\nfor ac_prog in gawk mawk nawk awk\ndo\n  # Extract the first word of \"$ac_prog\", so it can be a program name with args.\nset dummy $ac_prog; ac_word=$2\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for $ac_word\" >&5\n$as_echo_n \"checking for $ac_word... \" >&6; }\nif ${ac_cv_prog_AWK+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  if test -n \"$AWK\"; then\n  ac_cv_prog_AWK=\"$AWK\" # Let the user override the test.\nelse\nas_save_IFS=$IFS; IFS=$PATH_SEPARATOR\nfor as_dir in $PATH\ndo\n  IFS=$as_save_IFS\n  test -z \"$as_dir\" && as_dir=.\n    for ac_exec_ext in '' $ac_executable_extensions; do\n  if as_fn_executable_p \"$as_dir/$ac_word$ac_exec_ext\"; then\n    ac_cv_prog_AWK=\"$ac_prog\"\n    $as_echo \"$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext\" >&5\n    break 2\n  fi\ndone\n  done\nIFS=$as_save_IFS\n\nfi\nfi\nAWK=$ac_cv_prog_AWK\nif test -n \"$AWK\"; then\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $AWK\" >&5\n$as_echo \"$AWK\" >&6; }\nelse\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\nfi\n\n\n  test -n \"$AWK\" && break\ndone\n\n\n\n\n# Check whether --with-version was given.\nif test \"${with_version+set}\" = set; then :\n  withval=$with_version;\n    echo \"${with_version}\" | grep '^[0-9]\\+\\.[0-9]\\+\\.[0-9]\\+-[0-9]\\+-g[0-9a-f]\\+$' 2>&1 1>/dev/null\n    if test $? -eq 0 ; then\n      echo \"$with_version\" > \"${objroot}VERSION\"\n    else\n      echo \"${with_version}\" | grep '^VERSION$' 2>&1 1>/dev/null\n      if test $? -ne 0 ; then\n        as_fn_error $? \"${with_version} does not match <major>.<minor>.<bugfix>-<nrev>-g<gid> or VERSION\" \"$LINENO\" 5\n      fi\n    fi\n\nelse\n\n        if test \"x`test ! \\\"${srcroot}\\\" && cd \\\"${srcroot}\\\"; git rev-parse --is-inside-work-tree 2>/dev/null`\" = \"xtrue\" ; then\n                        for pattern in '[0-9].[0-9].[0-9]' '[0-9].[0-9].[0-9][0-9]' \\\n                     '[0-9].[0-9][0-9].[0-9]' '[0-9].[0-9][0-9].[0-9][0-9]' \\\n                     '[0-9][0-9].[0-9].[0-9]' '[0-9][0-9].[0-9].[0-9][0-9]' \\\n                     '[0-9][0-9].[0-9][0-9].[0-9]' \\\n                     '[0-9][0-9].[0-9][0-9].[0-9][0-9]'; do\n        (test ! \"${srcroot}\" && cd \"${srcroot}\"; git describe --long --abbrev=40 --match=\"${pattern}\") > \"${objroot}VERSION.tmp\" 2>/dev/null\n        if test $? -eq 0 ; then\n          mv \"${objroot}VERSION.tmp\" \"${objroot}VERSION\"\n          break\n        fi\n      done\n    fi\n    rm -f \"${objroot}VERSION.tmp\"\n\nfi\n\n\nif test ! -e \"${objroot}VERSION\" ; then\n  if test ! -e \"${srcroot}VERSION\" ; then\n    { $as_echo \"$as_me:${as_lineno-$LINENO}: result: Missing VERSION file, and unable to generate it; creating bogus VERSION\" >&5\n$as_echo \"Missing VERSION file, and unable to generate it; creating bogus VERSION\" >&6; }\n    echo \"0.0.0-0-g0000000000000000000000000000000000000000\" > \"${objroot}VERSION\"\n  else\n    cp ${srcroot}VERSION ${objroot}VERSION\n  fi\nfi\njemalloc_version=`cat \"${objroot}VERSION\"`\njemalloc_version_major=`echo ${jemalloc_version} | tr \".g-\" \" \" | awk '{print $1}'`\njemalloc_version_minor=`echo ${jemalloc_version} | tr \".g-\" \" \" | awk '{print $2}'`\njemalloc_version_bugfix=`echo ${jemalloc_version} | tr \".g-\" \" \" | awk '{print $3}'`\njemalloc_version_nrev=`echo ${jemalloc_version} | tr \".g-\" \" \" | awk '{print $4}'`\njemalloc_version_gid=`echo ${jemalloc_version} | tr \".g-\" \" \" | awk '{print $5}'`\n\n\n\n\n\n\n\ndefault_retain=\"0\"\nmaps_coalesce=\"1\"\nDUMP_SYMS=\"${NM} -a\"\nSYM_PREFIX=\"\"\ncase \"${host}\" in\n  *-*-darwin* | *-*-ios*)\n\tabi=\"macho\"\n\tRPATH=\"\"\n\tLD_PRELOAD_VAR=\"DYLD_INSERT_LIBRARIES\"\n\tso=\"dylib\"\n\timportlib=\"${so}\"\n\tforce_tls=\"0\"\n\tDSO_LDFLAGS='-shared -Wl,-install_name,$(LIBDIR)/$(@F)'\n\tSOREV=\"${rev}.${so}\"\n\tsbrk_deprecated=\"1\"\n\tSYM_PREFIX=\"_\"\n\t;;\n  *-*-freebsd*)\n\tabi=\"elf\"\n\t$as_echo \"#define JEMALLOC_SYSCTL_VM_OVERCOMMIT  \" >>confdefs.h\n\n\tforce_lazy_lock=\"1\"\n\t;;\n  *-*-dragonfly*)\n\tabi=\"elf\"\n\t;;\n  *-*-openbsd*)\n\tabi=\"elf\"\n\tforce_tls=\"0\"\n\t;;\n  *-*-bitrig*)\n\tabi=\"elf\"\n\t;;\n  *-*-linux-android)\n\t\tT_APPEND_V=-D_GNU_SOURCE\n  if test \"x${CPPFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CPPFLAGS=\"${CPPFLAGS}${T_APPEND_V}\"\nelse\n  CPPFLAGS=\"${CPPFLAGS} ${T_APPEND_V}\"\nfi\n\n\n\tabi=\"elf\"\n\t$as_echo \"#define JEMALLOC_PURGE_MADVISE_DONTNEED_ZEROS  \" >>confdefs.h\n\n\t$as_echo \"#define JEMALLOC_HAS_ALLOCA_H 1\" >>confdefs.h\n\n\t$as_echo \"#define JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY  \" >>confdefs.h\n\n\t$as_echo \"#define JEMALLOC_THREADED_INIT  \" >>confdefs.h\n\n\t$as_echo \"#define JEMALLOC_C11_ATOMICS 1\" >>confdefs.h\n\n\tforce_tls=\"0\"\n\tif test \"${LG_SIZEOF_PTR}\" = \"3\"; then\n\t  default_retain=\"1\"\n\tfi\n\t;;\n  *-*-linux*)\n\t\tT_APPEND_V=-D_GNU_SOURCE\n  if test \"x${CPPFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CPPFLAGS=\"${CPPFLAGS}${T_APPEND_V}\"\nelse\n  CPPFLAGS=\"${CPPFLAGS} ${T_APPEND_V}\"\nfi\n\n\n\tabi=\"elf\"\n\t$as_echo \"#define JEMALLOC_PURGE_MADVISE_DONTNEED_ZEROS  \" >>confdefs.h\n\n\t$as_echo \"#define JEMALLOC_HAS_ALLOCA_H 1\" >>confdefs.h\n\n\t$as_echo \"#define JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY  \" >>confdefs.h\n\n\t$as_echo \"#define JEMALLOC_THREADED_INIT  \" >>confdefs.h\n\n\t$as_echo \"#define JEMALLOC_USE_CXX_THROW  \" >>confdefs.h\n\n\tif test \"${LG_SIZEOF_PTR}\" = \"3\"; then\n\t  default_retain=\"1\"\n\tfi\n\t;;\n  *-*-kfreebsd*)\n\t\tT_APPEND_V=-D_GNU_SOURCE\n  if test \"x${CPPFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CPPFLAGS=\"${CPPFLAGS}${T_APPEND_V}\"\nelse\n  CPPFLAGS=\"${CPPFLAGS} ${T_APPEND_V}\"\nfi\n\n\n\tabi=\"elf\"\n\t$as_echo \"#define JEMALLOC_HAS_ALLOCA_H 1\" >>confdefs.h\n\n\t$as_echo \"#define JEMALLOC_SYSCTL_VM_OVERCOMMIT  \" >>confdefs.h\n\n\t$as_echo \"#define JEMALLOC_THREADED_INIT  \" >>confdefs.h\n\n\t$as_echo \"#define JEMALLOC_USE_CXX_THROW  \" >>confdefs.h\n\n\t;;\n  *-*-netbsd*)\n\t{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking ABI\" >&5\n$as_echo_n \"checking ABI... \" >&6; }\n        cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n#ifdef __ELF__\n/* ELF */\n#else\n#error aout\n#endif\n\nint\nmain ()\n{\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  abi=\"elf\"\nelse\n  abi=\"aout\"\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\n\t{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $abi\" >&5\n$as_echo \"$abi\" >&6; }\n\t;;\n  *-*-solaris2*)\n\tabi=\"elf\"\n\tRPATH='-Wl,-R,$(1)'\n\t\tT_APPEND_V=-D_POSIX_PTHREAD_SEMANTICS\n  if test \"x${CPPFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CPPFLAGS=\"${CPPFLAGS}${T_APPEND_V}\"\nelse\n  CPPFLAGS=\"${CPPFLAGS} ${T_APPEND_V}\"\nfi\n\n\n\tT_APPEND_V=-lposix4 -lsocket -lnsl\n  if test \"x${LIBS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  LIBS=\"${LIBS}${T_APPEND_V}\"\nelse\n  LIBS=\"${LIBS} ${T_APPEND_V}\"\nfi\n\n\n\t;;\n  *-ibm-aix*)\n\tif test \"${LG_SIZEOF_PTR}\" = \"3\"; then\n\t  \t  LD_PRELOAD_VAR=\"LDR_PRELOAD64\"\n\telse\n\t  \t  LD_PRELOAD_VAR=\"LDR_PRELOAD\"\n\tfi\n\tabi=\"xcoff\"\n\t;;\n  *-*-mingw* | *-*-cygwin*)\n\tabi=\"pecoff\"\n\tforce_tls=\"0\"\n\tmaps_coalesce=\"0\"\n\tRPATH=\"\"\n\tso=\"dll\"\n\tif test \"x$je_cv_msvc\" = \"xyes\" ; then\n\t  importlib=\"lib\"\n\t  DSO_LDFLAGS=\"-LD\"\n\t  EXTRA_LDFLAGS=\"-link -DEBUG\"\n\t  CTARGET='-Fo$@'\n\t  LDTARGET='-Fe$@'\n\t  AR='lib'\n\t  ARFLAGS='-nologo -out:'\n\t  AROUT='$@'\n\t  CC_MM=\n        else\n\t  importlib=\"${so}\"\n\t  DSO_LDFLAGS=\"-shared\"\n\t  link_whole_archive=\"1\"\n\tfi\n\tcase \"${host}\" in\n\t  *-*-cygwin*)\n\t    DUMP_SYMS=\"dumpbin /SYMBOLS\"\n\t    ;;\n\t  *)\n\t    ;;\n\tesac\n\ta=\"lib\"\n\tlibprefix=\"\"\n\tSOREV=\"${so}\"\n\tPIC_CFLAGS=\"\"\n\tif test \"${LG_SIZEOF_PTR}\" = \"3\"; then\n\t  default_retain=\"1\"\n\tfi\n\t;;\n  *)\n\t{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: Unsupported operating system: ${host}\" >&5\n$as_echo \"Unsupported operating system: ${host}\" >&6; }\n\tabi=\"elf\"\n\t;;\nesac\n\nJEMALLOC_USABLE_SIZE_CONST=const\nfor ac_header in malloc.h\ndo :\n  ac_fn_c_check_header_mongrel \"$LINENO\" \"malloc.h\" \"ac_cv_header_malloc_h\" \"$ac_includes_default\"\nif test \"x$ac_cv_header_malloc_h\" = xyes; then :\n  cat >>confdefs.h <<_ACEOF\n#define HAVE_MALLOC_H 1\n_ACEOF\n\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether malloc_usable_size definition can use const argument\" >&5\n$as_echo_n \"checking whether malloc_usable_size definition can use const argument... \" >&6; }\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n#include <malloc.h>\n     #include <stddef.h>\n    size_t malloc_usable_size(const void *ptr);\n\nint\nmain ()\n{\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n\n                { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\n\nelse\n\n                JEMALLOC_USABLE_SIZE_CONST=\n                { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\n\nfi\n\ndone\n\ncat >>confdefs.h <<_ACEOF\n#define JEMALLOC_USABLE_SIZE_CONST $JEMALLOC_USABLE_SIZE_CONST\n_ACEOF\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for library containing log\" >&5\n$as_echo_n \"checking for library containing log... \" >&6; }\nif ${ac_cv_search_log+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  ac_func_search_save_LIBS=$LIBS\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n/* Override any GCC internal prototype to avoid an error.\n   Use char because int might match the return type of a GCC\n   builtin and then its argument prototype would still apply.  */\n#ifdef __cplusplus\nextern \"C\"\n#endif\nchar log ();\nint\nmain ()\n{\nreturn log ();\n  ;\n  return 0;\n}\n_ACEOF\nfor ac_lib in '' m; do\n  if test -z \"$ac_lib\"; then\n    ac_res=\"none required\"\n  else\n    ac_res=-l$ac_lib\n    LIBS=\"-l$ac_lib  $ac_func_search_save_LIBS\"\n  fi\n  if ac_fn_c_try_link \"$LINENO\"; then :\n  ac_cv_search_log=$ac_res\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext\n  if ${ac_cv_search_log+:} false; then :\n  break\nfi\ndone\nif ${ac_cv_search_log+:} false; then :\n\nelse\n  ac_cv_search_log=no\nfi\nrm conftest.$ac_ext\nLIBS=$ac_func_search_save_LIBS\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_log\" >&5\n$as_echo \"$ac_cv_search_log\" >&6; }\nac_res=$ac_cv_search_log\nif test \"$ac_res\" != no; then :\n  test \"$ac_res\" = \"none required\" || LIBS=\"$ac_res $LIBS\"\n\nelse\n  as_fn_error $? \"Missing math functions\" \"$LINENO\" 5\nfi\n\nif test \"x$ac_cv_search_log\" != \"xnone required\" ; then\n  LM=\"$ac_cv_search_log\"\nelse\n  LM=\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether __attribute__ syntax is compilable\" >&5\n$as_echo_n \"checking whether __attribute__ syntax is compilable... \" >&6; }\nif ${je_cv_attribute+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\nstatic __attribute__((unused)) void foo(void){}\nint\nmain ()\n{\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_attribute=yes\nelse\n  je_cv_attribute=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_attribute\" >&5\n$as_echo \"$je_cv_attribute\" >&6; }\n\nif test \"x${je_cv_attribute}\" = \"xyes\" ; then\n  $as_echo \"#define JEMALLOC_HAVE_ATTR  \" >>confdefs.h\n\n  if test \"x${GCC}\" = \"xyes\" -a \"x${abi}\" = \"xelf\"; then\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -fvisibility=hidden\" >&5\n$as_echo_n \"checking whether compiler supports -fvisibility=hidden... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-fvisibility=hidden\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-fvisibility=hidden\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -fvisibility=hidden\" >&5\n$as_echo_n \"checking whether compiler supports -fvisibility=hidden... \" >&6; }\nT_CONFIGURE_CXXFLAGS=\"${CONFIGURE_CXXFLAGS}\"\nT_APPEND_V=-fvisibility=hidden\n  if test \"x${CONFIGURE_CXXFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CXXFLAGS=\"${CONFIGURE_CXXFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CXXFLAGS=\"${CONFIGURE_CXXFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CXXFLAGS}\" = \"x\" -o \"x${SPECIFIED_CXXFLAGS}\" = \"x\" ; then\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS}${SPECIFIED_CXXFLAGS}\"\nelse\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS} ${SPECIFIED_CXXFLAGS}\"\nfi\n\nac_ext=cpp\nac_cpp='$CXXCPP $CPPFLAGS'\nac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'\nac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'\nac_compiler_gnu=$ac_cv_cxx_compiler_gnu\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_cxx_try_compile \"$LINENO\"; then :\n  je_cv_cxxflags_added=-fvisibility=hidden\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cxxflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CXXFLAGS=\"${T_CONFIGURE_CXXFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nac_ext=c\nac_cpp='$CPP $CPPFLAGS'\nac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'\nac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'\nac_compiler_gnu=$ac_cv_c_compiler_gnu\n\nif test \"x${CONFIGURE_CXXFLAGS}\" = \"x\" -o \"x${SPECIFIED_CXXFLAGS}\" = \"x\" ; then\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS}${SPECIFIED_CXXFLAGS}\"\nelse\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS} ${SPECIFIED_CXXFLAGS}\"\nfi\n\n\n  fi\nfi\nSAVED_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -Werror\" >&5\n$as_echo_n \"checking whether compiler supports -Werror... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-Werror\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-Werror\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -herror_on_warning\" >&5\n$as_echo_n \"checking whether compiler supports -herror_on_warning... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-herror_on_warning\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-herror_on_warning\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether tls_model attribute is compilable\" >&5\n$as_echo_n \"checking whether tls_model attribute is compilable... \" >&6; }\nif ${je_cv_tls_model+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\nint\nmain ()\n{\nstatic __thread int\n               __attribute__((tls_model(\"initial-exec\"), unused)) foo;\n               foo = 0;\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_tls_model=yes\nelse\n  je_cv_tls_model=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_tls_model\" >&5\n$as_echo \"$je_cv_tls_model\" >&6; }\n\nCONFIGURE_CFLAGS=\"${SAVED_CONFIGURE_CFLAGS}\"\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\nSAVED_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -Werror\" >&5\n$as_echo_n \"checking whether compiler supports -Werror... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-Werror\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-Werror\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -herror_on_warning\" >&5\n$as_echo_n \"checking whether compiler supports -herror_on_warning... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-herror_on_warning\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-herror_on_warning\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether alloc_size attribute is compilable\" >&5\n$as_echo_n \"checking whether alloc_size attribute is compilable... \" >&6; }\nif ${je_cv_alloc_size+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n#include <stdlib.h>\nint\nmain ()\n{\nvoid *foo(size_t size) __attribute__((alloc_size(1)));\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_alloc_size=yes\nelse\n  je_cv_alloc_size=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_alloc_size\" >&5\n$as_echo \"$je_cv_alloc_size\" >&6; }\n\nCONFIGURE_CFLAGS=\"${SAVED_CONFIGURE_CFLAGS}\"\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\nif test \"x${je_cv_alloc_size}\" = \"xyes\" ; then\n  $as_echo \"#define JEMALLOC_HAVE_ATTR_ALLOC_SIZE  \" >>confdefs.h\n\nfi\nSAVED_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -Werror\" >&5\n$as_echo_n \"checking whether compiler supports -Werror... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-Werror\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-Werror\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -herror_on_warning\" >&5\n$as_echo_n \"checking whether compiler supports -herror_on_warning... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-herror_on_warning\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-herror_on_warning\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether format(gnu_printf, ...) attribute is compilable\" >&5\n$as_echo_n \"checking whether format(gnu_printf, ...) attribute is compilable... \" >&6; }\nif ${je_cv_format_gnu_printf+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n#include <stdlib.h>\nint\nmain ()\n{\nvoid *foo(const char *format, ...) __attribute__((format(gnu_printf, 1, 2)));\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_format_gnu_printf=yes\nelse\n  je_cv_format_gnu_printf=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_format_gnu_printf\" >&5\n$as_echo \"$je_cv_format_gnu_printf\" >&6; }\n\nCONFIGURE_CFLAGS=\"${SAVED_CONFIGURE_CFLAGS}\"\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\nif test \"x${je_cv_format_gnu_printf}\" = \"xyes\" ; then\n  $as_echo \"#define JEMALLOC_HAVE_ATTR_FORMAT_GNU_PRINTF  \" >>confdefs.h\n\nfi\nSAVED_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -Werror\" >&5\n$as_echo_n \"checking whether compiler supports -Werror... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-Werror\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-Werror\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -herror_on_warning\" >&5\n$as_echo_n \"checking whether compiler supports -herror_on_warning... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-herror_on_warning\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-herror_on_warning\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether format(printf, ...) attribute is compilable\" >&5\n$as_echo_n \"checking whether format(printf, ...) attribute is compilable... \" >&6; }\nif ${je_cv_format_printf+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n#include <stdlib.h>\nint\nmain ()\n{\nvoid *foo(const char *format, ...) __attribute__((format(printf, 1, 2)));\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_format_printf=yes\nelse\n  je_cv_format_printf=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_format_printf\" >&5\n$as_echo \"$je_cv_format_printf\" >&6; }\n\nCONFIGURE_CFLAGS=\"${SAVED_CONFIGURE_CFLAGS}\"\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\nif test \"x${je_cv_format_printf}\" = \"xyes\" ; then\n  $as_echo \"#define JEMALLOC_HAVE_ATTR_FORMAT_PRINTF  \" >>confdefs.h\n\nfi\n\nSAVED_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -Werror\" >&5\n$as_echo_n \"checking whether compiler supports -Werror... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-Werror\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-Werror\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -herror_on_warning\" >&5\n$as_echo_n \"checking whether compiler supports -herror_on_warning... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-herror_on_warning\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-herror_on_warning\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether format(printf, ...) attribute is compilable\" >&5\n$as_echo_n \"checking whether format(printf, ...) attribute is compilable... \" >&6; }\nif ${je_cv_format_arg+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n#include <stdlib.h>\nint\nmain ()\n{\nconst char * __attribute__((__format_arg__(1))) foo(const char *format);\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_format_arg=yes\nelse\n  je_cv_format_arg=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_format_arg\" >&5\n$as_echo \"$je_cv_format_arg\" >&6; }\n\nCONFIGURE_CFLAGS=\"${SAVED_CONFIGURE_CFLAGS}\"\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\nif test \"x${je_cv_format_arg}\" = \"xyes\" ; then\n  $as_echo \"#define JEMALLOC_HAVE_ATTR_FORMAT_ARG  \" >>confdefs.h\n\nfi\n\n\n# Check whether --with-rpath was given.\nif test \"${with_rpath+set}\" = set; then :\n  withval=$with_rpath; if test \"x$with_rpath\" = \"xno\" ; then\n  RPATH_EXTRA=\nelse\n  RPATH_EXTRA=\"`echo $with_rpath | tr \\\":\\\" \\\" \\\"`\"\nfi\nelse\n  RPATH_EXTRA=\n\nfi\n\n\n\n# Check whether --enable-autogen was given.\nif test \"${enable_autogen+set}\" = set; then :\n  enableval=$enable_autogen; if test \"x$enable_autogen\" = \"xno\" ; then\n  enable_autogen=\"0\"\nelse\n  enable_autogen=\"1\"\nfi\n\nelse\n  enable_autogen=\"0\"\n\nfi\n\n\n\n# Find a good install program.  We prefer a C program (faster),\n# so one script is as good as another.  But avoid the broken or\n# incompatible versions:\n# SysV /etc/install, /usr/sbin/install\n# SunOS /usr/etc/install\n# IRIX /sbin/install\n# AIX /bin/install\n# AmigaOS /C/install, which installs bootblocks on floppy discs\n# AIX 4 /usr/bin/installbsd, which doesn't work without a -g flag\n# AFS /usr/afsws/bin/install, which mishandles nonexistent args\n# SVR4 /usr/ucb/install, which tries to use the nonexistent group \"staff\"\n# OS/2's system install, which has a completely different semantic\n# ./install, which can be erroneously created by make from ./install.sh.\n# Reject install programs that cannot install multiple files.\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for a BSD-compatible install\" >&5\n$as_echo_n \"checking for a BSD-compatible install... \" >&6; }\nif test -z \"$INSTALL\"; then\nif ${ac_cv_path_install+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  as_save_IFS=$IFS; IFS=$PATH_SEPARATOR\nfor as_dir in $PATH\ndo\n  IFS=$as_save_IFS\n  test -z \"$as_dir\" && as_dir=.\n    # Account for people who put trailing slashes in PATH elements.\ncase $as_dir/ in #((\n  ./ | .// | /[cC]/* | \\\n  /etc/* | /usr/sbin/* | /usr/etc/* | /sbin/* | /usr/afsws/bin/* | \\\n  ?:[\\\\/]os2[\\\\/]install[\\\\/]* | ?:[\\\\/]OS2[\\\\/]INSTALL[\\\\/]* | \\\n  /usr/ucb/* ) ;;\n  *)\n    # OSF1 and SCO ODT 3.0 have their own names for install.\n    # Don't use installbsd from OSF since it installs stuff as root\n    # by default.\n    for ac_prog in ginstall scoinst install; do\n      for ac_exec_ext in '' $ac_executable_extensions; do\n\tif as_fn_executable_p \"$as_dir/$ac_prog$ac_exec_ext\"; then\n\t  if test $ac_prog = install &&\n\t    grep dspmsg \"$as_dir/$ac_prog$ac_exec_ext\" >/dev/null 2>&1; then\n\t    # AIX install.  It has an incompatible calling convention.\n\t    :\n\t  elif test $ac_prog = install &&\n\t    grep pwplus \"$as_dir/$ac_prog$ac_exec_ext\" >/dev/null 2>&1; then\n\t    # program-specific install script used by HP pwplus--don't use.\n\t    :\n\t  else\n\t    rm -rf conftest.one conftest.two conftest.dir\n\t    echo one > conftest.one\n\t    echo two > conftest.two\n\t    mkdir conftest.dir\n\t    if \"$as_dir/$ac_prog$ac_exec_ext\" -c conftest.one conftest.two \"`pwd`/conftest.dir\" &&\n\t      test -s conftest.one && test -s conftest.two &&\n\t      test -s conftest.dir/conftest.one &&\n\t      test -s conftest.dir/conftest.two\n\t    then\n\t      ac_cv_path_install=\"$as_dir/$ac_prog$ac_exec_ext -c\"\n\t      break 3\n\t    fi\n\t  fi\n\tfi\n      done\n    done\n    ;;\nesac\n\n  done\nIFS=$as_save_IFS\n\nrm -rf conftest.one conftest.two conftest.dir\n\nfi\n  if test \"${ac_cv_path_install+set}\" = set; then\n    INSTALL=$ac_cv_path_install\n  else\n    # As a last resort, use the slow shell script.  Don't cache a\n    # value for INSTALL within a source directory, because that will\n    # break other packages using the cache if that directory is\n    # removed, or if the value is a relative name.\n    INSTALL=$ac_install_sh\n  fi\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $INSTALL\" >&5\n$as_echo \"$INSTALL\" >&6; }\n\n# Use test -z because SunOS4 sh mishandles braces in ${var-val}.\n# It thinks the first close brace ends the variable substitution.\ntest -z \"$INSTALL_PROGRAM\" && INSTALL_PROGRAM='${INSTALL}'\n\ntest -z \"$INSTALL_SCRIPT\" && INSTALL_SCRIPT='${INSTALL}'\n\ntest -z \"$INSTALL_DATA\" && INSTALL_DATA='${INSTALL} -m 644'\n\nif test -n \"$ac_tool_prefix\"; then\n  # Extract the first word of \"${ac_tool_prefix}ranlib\", so it can be a program name with args.\nset dummy ${ac_tool_prefix}ranlib; ac_word=$2\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for $ac_word\" >&5\n$as_echo_n \"checking for $ac_word... \" >&6; }\nif ${ac_cv_prog_RANLIB+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  if test -n \"$RANLIB\"; then\n  ac_cv_prog_RANLIB=\"$RANLIB\" # Let the user override the test.\nelse\nas_save_IFS=$IFS; IFS=$PATH_SEPARATOR\nfor as_dir in $PATH\ndo\n  IFS=$as_save_IFS\n  test -z \"$as_dir\" && as_dir=.\n    for ac_exec_ext in '' $ac_executable_extensions; do\n  if as_fn_executable_p \"$as_dir/$ac_word$ac_exec_ext\"; then\n    ac_cv_prog_RANLIB=\"${ac_tool_prefix}ranlib\"\n    $as_echo \"$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext\" >&5\n    break 2\n  fi\ndone\n  done\nIFS=$as_save_IFS\n\nfi\nfi\nRANLIB=$ac_cv_prog_RANLIB\nif test -n \"$RANLIB\"; then\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $RANLIB\" >&5\n$as_echo \"$RANLIB\" >&6; }\nelse\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\nfi\n\n\nfi\nif test -z \"$ac_cv_prog_RANLIB\"; then\n  ac_ct_RANLIB=$RANLIB\n  # Extract the first word of \"ranlib\", so it can be a program name with args.\nset dummy ranlib; ac_word=$2\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for $ac_word\" >&5\n$as_echo_n \"checking for $ac_word... \" >&6; }\nif ${ac_cv_prog_ac_ct_RANLIB+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  if test -n \"$ac_ct_RANLIB\"; then\n  ac_cv_prog_ac_ct_RANLIB=\"$ac_ct_RANLIB\" # Let the user override the test.\nelse\nas_save_IFS=$IFS; IFS=$PATH_SEPARATOR\nfor as_dir in $PATH\ndo\n  IFS=$as_save_IFS\n  test -z \"$as_dir\" && as_dir=.\n    for ac_exec_ext in '' $ac_executable_extensions; do\n  if as_fn_executable_p \"$as_dir/$ac_word$ac_exec_ext\"; then\n    ac_cv_prog_ac_ct_RANLIB=\"ranlib\"\n    $as_echo \"$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext\" >&5\n    break 2\n  fi\ndone\n  done\nIFS=$as_save_IFS\n\nfi\nfi\nac_ct_RANLIB=$ac_cv_prog_ac_ct_RANLIB\nif test -n \"$ac_ct_RANLIB\"; then\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_ct_RANLIB\" >&5\n$as_echo \"$ac_ct_RANLIB\" >&6; }\nelse\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\nfi\n\n  if test \"x$ac_ct_RANLIB\" = x; then\n    RANLIB=\":\"\n  else\n    case $cross_compiling:$ac_tool_warned in\nyes:)\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not prefixed with host triplet\" >&5\n$as_echo \"$as_me: WARNING: using cross tools not prefixed with host triplet\" >&2;}\nac_tool_warned=yes ;;\nesac\n    RANLIB=$ac_ct_RANLIB\n  fi\nelse\n  RANLIB=\"$ac_cv_prog_RANLIB\"\nfi\n\n# Extract the first word of \"ld\", so it can be a program name with args.\nset dummy ld; ac_word=$2\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for $ac_word\" >&5\n$as_echo_n \"checking for $ac_word... \" >&6; }\nif ${ac_cv_path_LD+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  case $LD in\n  [\\\\/]* | ?:[\\\\/]*)\n  ac_cv_path_LD=\"$LD\" # Let the user override the test with a path.\n  ;;\n  *)\n  as_save_IFS=$IFS; IFS=$PATH_SEPARATOR\nfor as_dir in $PATH\ndo\n  IFS=$as_save_IFS\n  test -z \"$as_dir\" && as_dir=.\n    for ac_exec_ext in '' $ac_executable_extensions; do\n  if as_fn_executable_p \"$as_dir/$ac_word$ac_exec_ext\"; then\n    ac_cv_path_LD=\"$as_dir/$ac_word$ac_exec_ext\"\n    $as_echo \"$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext\" >&5\n    break 2\n  fi\ndone\n  done\nIFS=$as_save_IFS\n\n  test -z \"$ac_cv_path_LD\" && ac_cv_path_LD=\"false\"\n  ;;\nesac\nfi\nLD=$ac_cv_path_LD\nif test -n \"$LD\"; then\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $LD\" >&5\n$as_echo \"$LD\" >&6; }\nelse\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\nfi\n\n\n# Extract the first word of \"autoconf\", so it can be a program name with args.\nset dummy autoconf; ac_word=$2\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for $ac_word\" >&5\n$as_echo_n \"checking for $ac_word... \" >&6; }\nif ${ac_cv_path_AUTOCONF+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  case $AUTOCONF in\n  [\\\\/]* | ?:[\\\\/]*)\n  ac_cv_path_AUTOCONF=\"$AUTOCONF\" # Let the user override the test with a path.\n  ;;\n  *)\n  as_save_IFS=$IFS; IFS=$PATH_SEPARATOR\nfor as_dir in $PATH\ndo\n  IFS=$as_save_IFS\n  test -z \"$as_dir\" && as_dir=.\n    for ac_exec_ext in '' $ac_executable_extensions; do\n  if as_fn_executable_p \"$as_dir/$ac_word$ac_exec_ext\"; then\n    ac_cv_path_AUTOCONF=\"$as_dir/$ac_word$ac_exec_ext\"\n    $as_echo \"$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext\" >&5\n    break 2\n  fi\ndone\n  done\nIFS=$as_save_IFS\n\n  test -z \"$ac_cv_path_AUTOCONF\" && ac_cv_path_AUTOCONF=\"false\"\n  ;;\nesac\nfi\nAUTOCONF=$ac_cv_path_AUTOCONF\nif test -n \"$AUTOCONF\"; then\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: $AUTOCONF\" >&5\n$as_echo \"$AUTOCONF\" >&6; }\nelse\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\nfi\n\n\n\n# Check whether --enable-doc was given.\nif test \"${enable_doc+set}\" = set; then :\n  enableval=$enable_doc; if test \"x$enable_doc\" = \"xno\" ; then\n  enable_doc=\"0\"\nelse\n  enable_doc=\"1\"\nfi\n\nelse\n  enable_doc=\"1\"\n\nfi\n\n\n\n# Check whether --enable-shared was given.\nif test \"${enable_shared+set}\" = set; then :\n  enableval=$enable_shared; if test \"x$enable_shared\" = \"xno\" ; then\n  enable_shared=\"0\"\nelse\n  enable_shared=\"1\"\nfi\n\nelse\n  enable_shared=\"1\"\n\nfi\n\n\n\n# Check whether --enable-static was given.\nif test \"${enable_static+set}\" = set; then :\n  enableval=$enable_static; if test \"x$enable_static\" = \"xno\" ; then\n  enable_static=\"0\"\nelse\n  enable_static=\"1\"\nfi\n\nelse\n  enable_static=\"1\"\n\nfi\n\n\n\nif test \"$enable_shared$enable_static\" = \"00\" ; then\n  as_fn_error $? \"Please enable one of shared or static builds\" \"$LINENO\" 5\nfi\n\n\n# Check whether --with-mangling was given.\nif test \"${with_mangling+set}\" = set; then :\n  withval=$with_mangling; mangling_map=\"$with_mangling\"\nelse\n  mangling_map=\"\"\nfi\n\n\n\n# Check whether --with-jemalloc_prefix was given.\nif test \"${with_jemalloc_prefix+set}\" = set; then :\n  withval=$with_jemalloc_prefix; JEMALLOC_PREFIX=\"$with_jemalloc_prefix\"\nelse\n  if test \"x$abi\" != \"xmacho\" -a \"x$abi\" != \"xpecoff\"; then\n  JEMALLOC_PREFIX=\"\"\nelse\n  JEMALLOC_PREFIX=\"je_\"\nfi\n\nfi\n\nif test \"x$JEMALLOC_PREFIX\" = \"x\" ; then\n  $as_echo \"#define JEMALLOC_IS_MALLOC 1\" >>confdefs.h\n\nelse\n  JEMALLOC_CPREFIX=`echo ${JEMALLOC_PREFIX} | tr \"a-z\" \"A-Z\"`\n  cat >>confdefs.h <<_ACEOF\n#define JEMALLOC_PREFIX \"$JEMALLOC_PREFIX\"\n_ACEOF\n\n  cat >>confdefs.h <<_ACEOF\n#define JEMALLOC_CPREFIX \"$JEMALLOC_CPREFIX\"\n_ACEOF\n\nfi\n\n\n\n\n# Check whether --with-export was given.\nif test \"${with_export+set}\" = set; then :\n  withval=$with_export; if test \"x$with_export\" = \"xno\"; then\n  $as_echo \"#define JEMALLOC_EXPORT /**/\" >>confdefs.h\n\nfi\n\nfi\n\n\npublic_syms=\"aligned_alloc calloc dallocx free mallctl mallctlbymib mallctlnametomib malloc malloc_conf malloc_message malloc_stats_print malloc_usable_size mallocx smallocx_${jemalloc_version_gid} nallocx posix_memalign rallocx realloc sallocx sdallocx xallocx\"\nac_fn_c_check_func \"$LINENO\" \"memalign\" \"ac_cv_func_memalign\"\nif test \"x$ac_cv_func_memalign\" = xyes; then :\n  $as_echo \"#define JEMALLOC_OVERRIDE_MEMALIGN  \" >>confdefs.h\n\n\t       public_syms=\"${public_syms} memalign\"\nfi\n\nac_fn_c_check_func \"$LINENO\" \"valloc\" \"ac_cv_func_valloc\"\nif test \"x$ac_cv_func_valloc\" = xyes; then :\n  $as_echo \"#define JEMALLOC_OVERRIDE_VALLOC  \" >>confdefs.h\n\n\t       public_syms=\"${public_syms} valloc\"\nfi\n\n\nwrap_syms=\nif test \"x${JEMALLOC_PREFIX}\" = \"x\" ; then\n  ac_fn_c_check_func \"$LINENO\" \"__libc_calloc\" \"ac_cv_func___libc_calloc\"\nif test \"x$ac_cv_func___libc_calloc\" = xyes; then :\n  $as_echo \"#define JEMALLOC_OVERRIDE___LIBC_CALLOC  \" >>confdefs.h\n\n\t\t wrap_syms=\"${wrap_syms} __libc_calloc\"\nfi\n\n  ac_fn_c_check_func \"$LINENO\" \"__libc_free\" \"ac_cv_func___libc_free\"\nif test \"x$ac_cv_func___libc_free\" = xyes; then :\n  $as_echo \"#define JEMALLOC_OVERRIDE___LIBC_FREE  \" >>confdefs.h\n\n\t\t wrap_syms=\"${wrap_syms} __libc_free\"\nfi\n\n  ac_fn_c_check_func \"$LINENO\" \"__libc_malloc\" \"ac_cv_func___libc_malloc\"\nif test \"x$ac_cv_func___libc_malloc\" = xyes; then :\n  $as_echo \"#define JEMALLOC_OVERRIDE___LIBC_MALLOC  \" >>confdefs.h\n\n\t\t wrap_syms=\"${wrap_syms} __libc_malloc\"\nfi\n\n  ac_fn_c_check_func \"$LINENO\" \"__libc_memalign\" \"ac_cv_func___libc_memalign\"\nif test \"x$ac_cv_func___libc_memalign\" = xyes; then :\n  $as_echo \"#define JEMALLOC_OVERRIDE___LIBC_MEMALIGN  \" >>confdefs.h\n\n\t\t wrap_syms=\"${wrap_syms} __libc_memalign\"\nfi\n\n  ac_fn_c_check_func \"$LINENO\" \"__libc_realloc\" \"ac_cv_func___libc_realloc\"\nif test \"x$ac_cv_func___libc_realloc\" = xyes; then :\n  $as_echo \"#define JEMALLOC_OVERRIDE___LIBC_REALLOC  \" >>confdefs.h\n\n\t\t wrap_syms=\"${wrap_syms} __libc_realloc\"\nfi\n\n  ac_fn_c_check_func \"$LINENO\" \"__libc_valloc\" \"ac_cv_func___libc_valloc\"\nif test \"x$ac_cv_func___libc_valloc\" = xyes; then :\n  $as_echo \"#define JEMALLOC_OVERRIDE___LIBC_VALLOC  \" >>confdefs.h\n\n\t\t wrap_syms=\"${wrap_syms} __libc_valloc\"\nfi\n\n  ac_fn_c_check_func \"$LINENO\" \"__posix_memalign\" \"ac_cv_func___posix_memalign\"\nif test \"x$ac_cv_func___posix_memalign\" = xyes; then :\n  $as_echo \"#define JEMALLOC_OVERRIDE___POSIX_MEMALIGN  \" >>confdefs.h\n\n\t\t wrap_syms=\"${wrap_syms} __posix_memalign\"\nfi\n\nfi\n\ncase \"${host}\" in\n  *-*-mingw* | *-*-cygwin*)\n    wrap_syms=\"${wrap_syms} tls_callback\"\n    ;;\n  *)\n    ;;\nesac\n\n\n# Check whether --with-private_namespace was given.\nif test \"${with_private_namespace+set}\" = set; then :\n  withval=$with_private_namespace; JEMALLOC_PRIVATE_NAMESPACE=\"${with_private_namespace}je_\"\nelse\n  JEMALLOC_PRIVATE_NAMESPACE=\"je_\"\n\nfi\n\ncat >>confdefs.h <<_ACEOF\n#define JEMALLOC_PRIVATE_NAMESPACE $JEMALLOC_PRIVATE_NAMESPACE\n_ACEOF\n\nprivate_namespace=\"$JEMALLOC_PRIVATE_NAMESPACE\"\n\n\n\n# Check whether --with-install_suffix was given.\nif test \"${with_install_suffix+set}\" = set; then :\n  withval=$with_install_suffix; INSTALL_SUFFIX=\"$with_install_suffix\"\nelse\n  INSTALL_SUFFIX=\n\nfi\n\ninstall_suffix=\"$INSTALL_SUFFIX\"\n\n\n\n# Check whether --with-malloc_conf was given.\nif test \"${with_malloc_conf+set}\" = set; then :\n  withval=$with_malloc_conf; JEMALLOC_CONFIG_MALLOC_CONF=\"$with_malloc_conf\"\nelse\n  JEMALLOC_CONFIG_MALLOC_CONF=\"\"\n\nfi\n\nconfig_malloc_conf=\"$JEMALLOC_CONFIG_MALLOC_CONF\"\ncat >>confdefs.h <<_ACEOF\n#define JEMALLOC_CONFIG_MALLOC_CONF \"$config_malloc_conf\"\n_ACEOF\n\n\nje_=\"je_\"\n\n\ncfgoutputs_in=\"Makefile.in\"\ncfgoutputs_in=\"${cfgoutputs_in} jemalloc.pc.in\"\ncfgoutputs_in=\"${cfgoutputs_in} doc/html.xsl.in\"\ncfgoutputs_in=\"${cfgoutputs_in} doc/manpages.xsl.in\"\ncfgoutputs_in=\"${cfgoutputs_in} doc/jemalloc.xml.in\"\ncfgoutputs_in=\"${cfgoutputs_in} include/jemalloc/jemalloc_macros.h.in\"\ncfgoutputs_in=\"${cfgoutputs_in} include/jemalloc/jemalloc_protos.h.in\"\ncfgoutputs_in=\"${cfgoutputs_in} include/jemalloc/jemalloc_typedefs.h.in\"\ncfgoutputs_in=\"${cfgoutputs_in} include/jemalloc/internal/jemalloc_preamble.h.in\"\ncfgoutputs_in=\"${cfgoutputs_in} test/test.sh.in\"\ncfgoutputs_in=\"${cfgoutputs_in} test/include/test/jemalloc_test.h.in\"\n\ncfgoutputs_out=\"Makefile\"\ncfgoutputs_out=\"${cfgoutputs_out} jemalloc.pc\"\ncfgoutputs_out=\"${cfgoutputs_out} doc/html.xsl\"\ncfgoutputs_out=\"${cfgoutputs_out} doc/manpages.xsl\"\ncfgoutputs_out=\"${cfgoutputs_out} doc/jemalloc.xml\"\ncfgoutputs_out=\"${cfgoutputs_out} include/jemalloc/jemalloc_macros.h\"\ncfgoutputs_out=\"${cfgoutputs_out} include/jemalloc/jemalloc_protos.h\"\ncfgoutputs_out=\"${cfgoutputs_out} include/jemalloc/jemalloc_typedefs.h\"\ncfgoutputs_out=\"${cfgoutputs_out} include/jemalloc/internal/jemalloc_preamble.h\"\ncfgoutputs_out=\"${cfgoutputs_out} test/test.sh\"\ncfgoutputs_out=\"${cfgoutputs_out} test/include/test/jemalloc_test.h\"\n\ncfgoutputs_tup=\"Makefile\"\ncfgoutputs_tup=\"${cfgoutputs_tup} jemalloc.pc:jemalloc.pc.in\"\ncfgoutputs_tup=\"${cfgoutputs_tup} doc/html.xsl:doc/html.xsl.in\"\ncfgoutputs_tup=\"${cfgoutputs_tup} doc/manpages.xsl:doc/manpages.xsl.in\"\ncfgoutputs_tup=\"${cfgoutputs_tup} doc/jemalloc.xml:doc/jemalloc.xml.in\"\ncfgoutputs_tup=\"${cfgoutputs_tup} include/jemalloc/jemalloc_macros.h:include/jemalloc/jemalloc_macros.h.in\"\ncfgoutputs_tup=\"${cfgoutputs_tup} include/jemalloc/jemalloc_protos.h:include/jemalloc/jemalloc_protos.h.in\"\ncfgoutputs_tup=\"${cfgoutputs_tup} include/jemalloc/jemalloc_typedefs.h:include/jemalloc/jemalloc_typedefs.h.in\"\ncfgoutputs_tup=\"${cfgoutputs_tup} include/jemalloc/internal/jemalloc_preamble.h\"\ncfgoutputs_tup=\"${cfgoutputs_tup} test/test.sh:test/test.sh.in\"\ncfgoutputs_tup=\"${cfgoutputs_tup} test/include/test/jemalloc_test.h:test/include/test/jemalloc_test.h.in\"\n\ncfghdrs_in=\"include/jemalloc/jemalloc_defs.h.in\"\ncfghdrs_in=\"${cfghdrs_in} include/jemalloc/internal/jemalloc_internal_defs.h.in\"\ncfghdrs_in=\"${cfghdrs_in} include/jemalloc/internal/private_symbols.sh\"\ncfghdrs_in=\"${cfghdrs_in} include/jemalloc/internal/private_namespace.sh\"\ncfghdrs_in=\"${cfghdrs_in} include/jemalloc/internal/public_namespace.sh\"\ncfghdrs_in=\"${cfghdrs_in} include/jemalloc/internal/public_unnamespace.sh\"\ncfghdrs_in=\"${cfghdrs_in} include/jemalloc/jemalloc_rename.sh\"\ncfghdrs_in=\"${cfghdrs_in} include/jemalloc/jemalloc_mangle.sh\"\ncfghdrs_in=\"${cfghdrs_in} include/jemalloc/jemalloc.sh\"\ncfghdrs_in=\"${cfghdrs_in} test/include/test/jemalloc_test_defs.h.in\"\n\ncfghdrs_out=\"include/jemalloc/jemalloc_defs.h\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/jemalloc${install_suffix}.h\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/internal/private_symbols.awk\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/internal/private_symbols_jet.awk\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/internal/public_symbols.txt\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/internal/public_namespace.h\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/internal/public_unnamespace.h\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/jemalloc_protos_jet.h\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/jemalloc_rename.h\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/jemalloc_mangle.h\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/jemalloc_mangle_jet.h\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/internal/jemalloc_internal_defs.h\"\ncfghdrs_out=\"${cfghdrs_out} test/include/test/jemalloc_test_defs.h\"\n\ncfghdrs_tup=\"include/jemalloc/jemalloc_defs.h:include/jemalloc/jemalloc_defs.h.in\"\ncfghdrs_tup=\"${cfghdrs_tup} include/jemalloc/internal/jemalloc_internal_defs.h:include/jemalloc/internal/jemalloc_internal_defs.h.in\"\ncfghdrs_tup=\"${cfghdrs_tup} test/include/test/jemalloc_test_defs.h:test/include/test/jemalloc_test_defs.h.in\"\n\n\n# Check whether --enable-debug was given.\nif test \"${enable_debug+set}\" = set; then :\n  enableval=$enable_debug; if test \"x$enable_debug\" = \"xno\" ; then\n  enable_debug=\"0\"\nelse\n  enable_debug=\"1\"\nfi\n\nelse\n  enable_debug=\"0\"\n\nfi\n\nif test \"x$enable_debug\" = \"x1\" ; then\n  $as_echo \"#define JEMALLOC_DEBUG  \" >>confdefs.h\n\nfi\nif test \"x$enable_debug\" = \"x1\" ; then\n  $as_echo \"#define JEMALLOC_DEBUG  \" >>confdefs.h\n\nfi\n\n\nif test \"x$enable_debug\" = \"x0\" ; then\n  if test \"x$GCC\" = \"xyes\" ; then\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -O3\" >&5\n$as_echo_n \"checking whether compiler supports -O3... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-O3\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-O3\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -O3\" >&5\n$as_echo_n \"checking whether compiler supports -O3... \" >&6; }\nT_CONFIGURE_CXXFLAGS=\"${CONFIGURE_CXXFLAGS}\"\nT_APPEND_V=-O3\n  if test \"x${CONFIGURE_CXXFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CXXFLAGS=\"${CONFIGURE_CXXFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CXXFLAGS=\"${CONFIGURE_CXXFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CXXFLAGS}\" = \"x\" -o \"x${SPECIFIED_CXXFLAGS}\" = \"x\" ; then\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS}${SPECIFIED_CXXFLAGS}\"\nelse\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS} ${SPECIFIED_CXXFLAGS}\"\nfi\n\nac_ext=cpp\nac_cpp='$CXXCPP $CPPFLAGS'\nac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'\nac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'\nac_compiler_gnu=$ac_cv_cxx_compiler_gnu\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_cxx_try_compile \"$LINENO\"; then :\n  je_cv_cxxflags_added=-O3\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cxxflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CXXFLAGS=\"${T_CONFIGURE_CXXFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nac_ext=c\nac_cpp='$CPP $CPPFLAGS'\nac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'\nac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'\nac_compiler_gnu=$ac_cv_c_compiler_gnu\n\nif test \"x${CONFIGURE_CXXFLAGS}\" = \"x\" -o \"x${SPECIFIED_CXXFLAGS}\" = \"x\" ; then\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS}${SPECIFIED_CXXFLAGS}\"\nelse\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS} ${SPECIFIED_CXXFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -funroll-loops\" >&5\n$as_echo_n \"checking whether compiler supports -funroll-loops... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-funroll-loops\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-funroll-loops\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n  elif test \"x$je_cv_msvc\" = \"xyes\" ; then\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -O2\" >&5\n$as_echo_n \"checking whether compiler supports -O2... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-O2\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-O2\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -O2\" >&5\n$as_echo_n \"checking whether compiler supports -O2... \" >&6; }\nT_CONFIGURE_CXXFLAGS=\"${CONFIGURE_CXXFLAGS}\"\nT_APPEND_V=-O2\n  if test \"x${CONFIGURE_CXXFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CXXFLAGS=\"${CONFIGURE_CXXFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CXXFLAGS=\"${CONFIGURE_CXXFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CXXFLAGS}\" = \"x\" -o \"x${SPECIFIED_CXXFLAGS}\" = \"x\" ; then\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS}${SPECIFIED_CXXFLAGS}\"\nelse\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS} ${SPECIFIED_CXXFLAGS}\"\nfi\n\nac_ext=cpp\nac_cpp='$CXXCPP $CPPFLAGS'\nac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'\nac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'\nac_compiler_gnu=$ac_cv_cxx_compiler_gnu\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_cxx_try_compile \"$LINENO\"; then :\n  je_cv_cxxflags_added=-O2\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cxxflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CXXFLAGS=\"${T_CONFIGURE_CXXFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nac_ext=c\nac_cpp='$CPP $CPPFLAGS'\nac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'\nac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'\nac_compiler_gnu=$ac_cv_c_compiler_gnu\n\nif test \"x${CONFIGURE_CXXFLAGS}\" = \"x\" -o \"x${SPECIFIED_CXXFLAGS}\" = \"x\" ; then\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS}${SPECIFIED_CXXFLAGS}\"\nelse\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS} ${SPECIFIED_CXXFLAGS}\"\nfi\n\n\n  else\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -O\" >&5\n$as_echo_n \"checking whether compiler supports -O... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-O\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-O\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -O\" >&5\n$as_echo_n \"checking whether compiler supports -O... \" >&6; }\nT_CONFIGURE_CXXFLAGS=\"${CONFIGURE_CXXFLAGS}\"\nT_APPEND_V=-O\n  if test \"x${CONFIGURE_CXXFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CXXFLAGS=\"${CONFIGURE_CXXFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CXXFLAGS=\"${CONFIGURE_CXXFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CXXFLAGS}\" = \"x\" -o \"x${SPECIFIED_CXXFLAGS}\" = \"x\" ; then\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS}${SPECIFIED_CXXFLAGS}\"\nelse\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS} ${SPECIFIED_CXXFLAGS}\"\nfi\n\nac_ext=cpp\nac_cpp='$CXXCPP $CPPFLAGS'\nac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'\nac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'\nac_compiler_gnu=$ac_cv_cxx_compiler_gnu\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_cxx_try_compile \"$LINENO\"; then :\n  je_cv_cxxflags_added=-O\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cxxflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CXXFLAGS=\"${T_CONFIGURE_CXXFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nac_ext=c\nac_cpp='$CPP $CPPFLAGS'\nac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'\nac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'\nac_compiler_gnu=$ac_cv_c_compiler_gnu\n\nif test \"x${CONFIGURE_CXXFLAGS}\" = \"x\" -o \"x${SPECIFIED_CXXFLAGS}\" = \"x\" ; then\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS}${SPECIFIED_CXXFLAGS}\"\nelse\n  CXXFLAGS=\"${CONFIGURE_CXXFLAGS} ${SPECIFIED_CXXFLAGS}\"\nfi\n\n\n  fi\nfi\n\n# Check whether --enable-stats was given.\nif test \"${enable_stats+set}\" = set; then :\n  enableval=$enable_stats; if test \"x$enable_stats\" = \"xno\" ; then\n  enable_stats=\"0\"\nelse\n  enable_stats=\"1\"\nfi\n\nelse\n  enable_stats=\"1\"\n\nfi\n\nif test \"x$enable_stats\" = \"x1\" ; then\n  $as_echo \"#define JEMALLOC_STATS  \" >>confdefs.h\n\nfi\n\n\n# Check whether --enable-experimental_smallocx was given.\nif test \"${enable_experimental_smallocx+set}\" = set; then :\n  enableval=$enable_experimental_smallocx; if test \"x$enable_experimental_smallocx\" = \"xno\" ; then\nenable_experimental_smallocx=\"0\"\nelse\nenable_experimental_smallocx=\"1\"\nfi\n\nelse\n  enable_experimental_smallocx=\"0\"\n\nfi\n\nif test \"x$enable_experimental_smallocx\" = \"x1\" ; then\n  $as_echo \"#define JEMALLOC_EXPERIMENTAL_SMALLOCX_API 1\" >>confdefs.h\n\nfi\n\n\n# Check whether --enable-prof was given.\nif test \"${enable_prof+set}\" = set; then :\n  enableval=$enable_prof; if test \"x$enable_prof\" = \"xno\" ; then\n  enable_prof=\"0\"\nelse\n  enable_prof=\"1\"\nfi\n\nelse\n  enable_prof=\"0\"\n\nfi\n\nif test \"x$enable_prof\" = \"x1\" ; then\n  backtrace_method=\"\"\nelse\n  backtrace_method=\"N/A\"\nfi\n\n# Check whether --enable-prof-libunwind was given.\nif test \"${enable_prof_libunwind+set}\" = set; then :\n  enableval=$enable_prof_libunwind; if test \"x$enable_prof_libunwind\" = \"xno\" ; then\n  enable_prof_libunwind=\"0\"\nelse\n  enable_prof_libunwind=\"1\"\nfi\n\nelse\n  enable_prof_libunwind=\"0\"\n\nfi\n\n\n# Check whether --with-static_libunwind was given.\nif test \"${with_static_libunwind+set}\" = set; then :\n  withval=$with_static_libunwind; if test \"x$with_static_libunwind\" = \"xno\" ; then\n  LUNWIND=\"-lunwind\"\nelse\n  if test ! -f \"$with_static_libunwind\" ; then\n    as_fn_error $? \"Static libunwind not found: $with_static_libunwind\" \"$LINENO\" 5\n  fi\n  LUNWIND=\"$with_static_libunwind\"\nfi\nelse\n  LUNWIND=\"-lunwind\"\n\nfi\n\nif test \"x$backtrace_method\" = \"x\" -a \"x$enable_prof_libunwind\" = \"x1\" ; then\n  for ac_header in libunwind.h\ndo :\n  ac_fn_c_check_header_mongrel \"$LINENO\" \"libunwind.h\" \"ac_cv_header_libunwind_h\" \"$ac_includes_default\"\nif test \"x$ac_cv_header_libunwind_h\" = xyes; then :\n  cat >>confdefs.h <<_ACEOF\n#define HAVE_LIBUNWIND_H 1\n_ACEOF\n\nelse\n  enable_prof_libunwind=\"0\"\nfi\n\ndone\n\n  if test \"x$LUNWIND\" = \"x-lunwind\" ; then\n    { $as_echo \"$as_me:${as_lineno-$LINENO}: checking for unw_backtrace in -lunwind\" >&5\n$as_echo_n \"checking for unw_backtrace in -lunwind... \" >&6; }\nif ${ac_cv_lib_unwind_unw_backtrace+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  ac_check_lib_save_LIBS=$LIBS\nLIBS=\"-lunwind  $LIBS\"\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n/* Override any GCC internal prototype to avoid an error.\n   Use char because int might match the return type of a GCC\n   builtin and then its argument prototype would still apply.  */\n#ifdef __cplusplus\nextern \"C\"\n#endif\nchar unw_backtrace ();\nint\nmain ()\n{\nreturn unw_backtrace ();\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  ac_cv_lib_unwind_unw_backtrace=yes\nelse\n  ac_cv_lib_unwind_unw_backtrace=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nLIBS=$ac_check_lib_save_LIBS\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_unwind_unw_backtrace\" >&5\n$as_echo \"$ac_cv_lib_unwind_unw_backtrace\" >&6; }\nif test \"x$ac_cv_lib_unwind_unw_backtrace\" = xyes; then :\n  T_APPEND_V=$LUNWIND\n  if test \"x${LIBS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  LIBS=\"${LIBS}${T_APPEND_V}\"\nelse\n  LIBS=\"${LIBS} ${T_APPEND_V}\"\nfi\n\n\nelse\n  enable_prof_libunwind=\"0\"\nfi\n\n  else\n    T_APPEND_V=$LUNWIND\n  if test \"x${LIBS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  LIBS=\"${LIBS}${T_APPEND_V}\"\nelse\n  LIBS=\"${LIBS} ${T_APPEND_V}\"\nfi\n\n\n  fi\n  if test \"x${enable_prof_libunwind}\" = \"x1\" ; then\n    backtrace_method=\"libunwind\"\n    $as_echo \"#define JEMALLOC_PROF_LIBUNWIND  \" >>confdefs.h\n\n  fi\nfi\n\n# Check whether --enable-prof-libgcc was given.\nif test \"${enable_prof_libgcc+set}\" = set; then :\n  enableval=$enable_prof_libgcc; if test \"x$enable_prof_libgcc\" = \"xno\" ; then\n  enable_prof_libgcc=\"0\"\nelse\n  enable_prof_libgcc=\"1\"\nfi\n\nelse\n  enable_prof_libgcc=\"1\"\n\nfi\n\nif test \"x$backtrace_method\" = \"x\" -a \"x$enable_prof_libgcc\" = \"x1\" \\\n     -a \"x$GCC\" = \"xyes\" ; then\n  for ac_header in unwind.h\ndo :\n  ac_fn_c_check_header_mongrel \"$LINENO\" \"unwind.h\" \"ac_cv_header_unwind_h\" \"$ac_includes_default\"\nif test \"x$ac_cv_header_unwind_h\" = xyes; then :\n  cat >>confdefs.h <<_ACEOF\n#define HAVE_UNWIND_H 1\n_ACEOF\n\nelse\n  enable_prof_libgcc=\"0\"\nfi\n\ndone\n\n  if test \"x${enable_prof_libgcc}\" = \"x1\" ; then\n    { $as_echo \"$as_me:${as_lineno-$LINENO}: checking for _Unwind_Backtrace in -lgcc\" >&5\n$as_echo_n \"checking for _Unwind_Backtrace in -lgcc... \" >&6; }\nif ${ac_cv_lib_gcc__Unwind_Backtrace+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  ac_check_lib_save_LIBS=$LIBS\nLIBS=\"-lgcc  $LIBS\"\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n/* Override any GCC internal prototype to avoid an error.\n   Use char because int might match the return type of a GCC\n   builtin and then its argument prototype would still apply.  */\n#ifdef __cplusplus\nextern \"C\"\n#endif\nchar _Unwind_Backtrace ();\nint\nmain ()\n{\nreturn _Unwind_Backtrace ();\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  ac_cv_lib_gcc__Unwind_Backtrace=yes\nelse\n  ac_cv_lib_gcc__Unwind_Backtrace=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nLIBS=$ac_check_lib_save_LIBS\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_gcc__Unwind_Backtrace\" >&5\n$as_echo \"$ac_cv_lib_gcc__Unwind_Backtrace\" >&6; }\nif test \"x$ac_cv_lib_gcc__Unwind_Backtrace\" = xyes; then :\n  T_APPEND_V=-lgcc\n  if test \"x${LIBS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  LIBS=\"${LIBS}${T_APPEND_V}\"\nelse\n  LIBS=\"${LIBS} ${T_APPEND_V}\"\nfi\n\n\nelse\n  enable_prof_libgcc=\"0\"\nfi\n\n  fi\n  if test \"x${enable_prof_libgcc}\" = \"x1\" ; then\n    backtrace_method=\"libgcc\"\n    $as_echo \"#define JEMALLOC_PROF_LIBGCC  \" >>confdefs.h\n\n  fi\nelse\n  enable_prof_libgcc=\"0\"\nfi\n\n# Check whether --enable-prof-gcc was given.\nif test \"${enable_prof_gcc+set}\" = set; then :\n  enableval=$enable_prof_gcc; if test \"x$enable_prof_gcc\" = \"xno\" ; then\n  enable_prof_gcc=\"0\"\nelse\n  enable_prof_gcc=\"1\"\nfi\n\nelse\n  enable_prof_gcc=\"1\"\n\nfi\n\nif test \"x$backtrace_method\" = \"x\" -a \"x$enable_prof_gcc\" = \"x1\" \\\n     -a \"x$GCC\" = \"xyes\" ; then\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -fno-omit-frame-pointer\" >&5\n$as_echo_n \"checking whether compiler supports -fno-omit-frame-pointer... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-fno-omit-frame-pointer\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-fno-omit-frame-pointer\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n  backtrace_method=\"gcc intrinsics\"\n  $as_echo \"#define JEMALLOC_PROF_GCC  \" >>confdefs.h\n\nelse\n  enable_prof_gcc=\"0\"\nfi\n\nif test \"x$backtrace_method\" = \"x\" ; then\n  backtrace_method=\"none (disabling profiling)\"\n  enable_prof=\"0\"\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking configured backtracing method\" >&5\n$as_echo_n \"checking configured backtracing method... \" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $backtrace_method\" >&5\n$as_echo \"$backtrace_method\" >&6; }\nif test \"x$enable_prof\" = \"x1\" ; then\n    T_APPEND_V=$LM\n  if test \"x${LIBS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  LIBS=\"${LIBS}${T_APPEND_V}\"\nelse\n  LIBS=\"${LIBS} ${T_APPEND_V}\"\nfi\n\n\n\n  $as_echo \"#define JEMALLOC_PROF  \" >>confdefs.h\n\nfi\n\n\nif test \"x${maps_coalesce}\" = \"x1\" ; then\n  $as_echo \"#define JEMALLOC_MAPS_COALESCE  \" >>confdefs.h\n\nfi\n\nif test \"x$default_retain\" = \"x1\" ; then\n  $as_echo \"#define JEMALLOC_RETAIN  \" >>confdefs.h\n\nfi\n\nhave_dss=\"1\"\nac_fn_c_check_func \"$LINENO\" \"sbrk\" \"ac_cv_func_sbrk\"\nif test \"x$ac_cv_func_sbrk\" = xyes; then :\n  have_sbrk=\"1\"\nelse\n  have_sbrk=\"0\"\nfi\n\nif test \"x$have_sbrk\" = \"x1\" ; then\n  if test \"x$sbrk_deprecated\" = \"x1\" ; then\n    { $as_echo \"$as_me:${as_lineno-$LINENO}: result: Disabling dss allocation because sbrk is deprecated\" >&5\n$as_echo \"Disabling dss allocation because sbrk is deprecated\" >&6; }\n    have_dss=\"0\"\n  fi\nelse\n  have_dss=\"0\"\nfi\n\nif test \"x$have_dss\" = \"x1\" ; then\n  $as_echo \"#define JEMALLOC_DSS  \" >>confdefs.h\n\nfi\n\n# Check whether --enable-fill was given.\nif test \"${enable_fill+set}\" = set; then :\n  enableval=$enable_fill; if test \"x$enable_fill\" = \"xno\" ; then\n  enable_fill=\"0\"\nelse\n  enable_fill=\"1\"\nfi\n\nelse\n  enable_fill=\"1\"\n\nfi\n\nif test \"x$enable_fill\" = \"x1\" ; then\n  $as_echo \"#define JEMALLOC_FILL  \" >>confdefs.h\n\nfi\n\n\n# Check whether --enable-utrace was given.\nif test \"${enable_utrace+set}\" = set; then :\n  enableval=$enable_utrace; if test \"x$enable_utrace\" = \"xno\" ; then\n  enable_utrace=\"0\"\nelse\n  enable_utrace=\"1\"\nfi\n\nelse\n  enable_utrace=\"0\"\n\nfi\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether utrace(2) is compilable\" >&5\n$as_echo_n \"checking whether utrace(2) is compilable... \" >&6; }\nif ${je_cv_utrace+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n#include <sys/types.h>\n#include <sys/param.h>\n#include <sys/time.h>\n#include <sys/uio.h>\n#include <sys/ktrace.h>\n\nint\nmain ()\n{\n\n\tutrace((void *)0, 0);\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_utrace=yes\nelse\n  je_cv_utrace=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_utrace\" >&5\n$as_echo \"$je_cv_utrace\" >&6; }\n\nif test \"x${je_cv_utrace}\" = \"xno\" ; then\n  enable_utrace=\"0\"\nfi\nif test \"x$enable_utrace\" = \"x1\" ; then\n  $as_echo \"#define JEMALLOC_UTRACE  \" >>confdefs.h\n\nfi\n\n\n# Check whether --enable-xmalloc was given.\nif test \"${enable_xmalloc+set}\" = set; then :\n  enableval=$enable_xmalloc; if test \"x$enable_xmalloc\" = \"xno\" ; then\n  enable_xmalloc=\"0\"\nelse\n  enable_xmalloc=\"1\"\nfi\n\nelse\n  enable_xmalloc=\"0\"\n\nfi\n\nif test \"x$enable_xmalloc\" = \"x1\" ; then\n  $as_echo \"#define JEMALLOC_XMALLOC  \" >>confdefs.h\n\nfi\n\n\n# Check whether --enable-cache-oblivious was given.\nif test \"${enable_cache_oblivious+set}\" = set; then :\n  enableval=$enable_cache_oblivious; if test \"x$enable_cache_oblivious\" = \"xno\" ; then\n  enable_cache_oblivious=\"0\"\nelse\n  enable_cache_oblivious=\"1\"\nfi\n\nelse\n  enable_cache_oblivious=\"1\"\n\nfi\n\nif test \"x$enable_cache_oblivious\" = \"x1\" ; then\n  $as_echo \"#define JEMALLOC_CACHE_OBLIVIOUS  \" >>confdefs.h\n\nfi\n\n\n# Check whether --enable-log was given.\nif test \"${enable_log+set}\" = set; then :\n  enableval=$enable_log; if test \"x$enable_log\" = \"xno\" ; then\n  enable_log=\"0\"\nelse\n  enable_log=\"1\"\nfi\n\nelse\n  enable_log=\"0\"\n\nfi\n\nif test \"x$enable_log\" = \"x1\" ; then\n  $as_echo \"#define JEMALLOC_LOG  \" >>confdefs.h\n\nfi\n\n\n# Check whether --enable-readlinkat was given.\nif test \"${enable_readlinkat+set}\" = set; then :\n  enableval=$enable_readlinkat; if test \"x$enable_readlinkat\" = \"xno\" ; then\n  enable_readlinkat=\"0\"\nelse\n  enable_readlinkat=\"1\"\nfi\n\nelse\n  enable_readlinkat=\"0\"\n\nfi\n\nif test \"x$enable_readlinkat\" = \"x1\" ; then\n  $as_echo \"#define JEMALLOC_READLINKAT  \" >>confdefs.h\n\nfi\n\n\n# Check whether --enable-opt-safety-checks was given.\nif test \"${enable_opt_safety_checks+set}\" = set; then :\n  enableval=$enable_opt_safety_checks; if test \"x$enable_opt_safety_checks\" = \"xno\" ; then\n  enable_opt_safety_checks=\"0\"\nelse\n  enable_opt_safety_checks=\"1\"\nfi\n\nelse\n  enable_opt_safety_checks=\"0\"\n\nfi\n\nif test \"x$enable_opt_safety_checks\" = \"x1\" ; then\n  $as_echo \"#define JEMALLOC_OPT_SAFETY_CHECKS  \" >>confdefs.h\n\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether a program using __builtin_unreachable is compilable\" >&5\n$as_echo_n \"checking whether a program using __builtin_unreachable is compilable... \" >&6; }\nif ${je_cv_gcc_builtin_unreachable+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\nvoid foo (void) {\n  __builtin_unreachable();\n}\n\nint\nmain ()\n{\n\n\t{\n\t\tfoo();\n\t}\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_gcc_builtin_unreachable=yes\nelse\n  je_cv_gcc_builtin_unreachable=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_gcc_builtin_unreachable\" >&5\n$as_echo \"$je_cv_gcc_builtin_unreachable\" >&6; }\n\nif test \"x${je_cv_gcc_builtin_unreachable}\" = \"xyes\" ; then\n  $as_echo \"#define JEMALLOC_INTERNAL_UNREACHABLE __builtin_unreachable\" >>confdefs.h\n\nelse\n  $as_echo \"#define JEMALLOC_INTERNAL_UNREACHABLE abort\" >>confdefs.h\n\nfi\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether a program using __builtin_ffsl is compilable\" >&5\n$as_echo_n \"checking whether a program using __builtin_ffsl is compilable... \" >&6; }\nif ${je_cv_gcc_builtin_ffsl+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n#include <stdio.h>\n#include <strings.h>\n#include <string.h>\n\nint\nmain ()\n{\n\n\t{\n\t\tint rv = __builtin_ffsl(0x08);\n\t\tprintf(\"%d\\n\", rv);\n\t}\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_gcc_builtin_ffsl=yes\nelse\n  je_cv_gcc_builtin_ffsl=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_gcc_builtin_ffsl\" >&5\n$as_echo \"$je_cv_gcc_builtin_ffsl\" >&6; }\n\nif test \"x${je_cv_gcc_builtin_ffsl}\" = \"xyes\" ; then\n  $as_echo \"#define JEMALLOC_INTERNAL_FFSLL __builtin_ffsll\" >>confdefs.h\n\n  $as_echo \"#define JEMALLOC_INTERNAL_FFSL __builtin_ffsl\" >>confdefs.h\n\n  $as_echo \"#define JEMALLOC_INTERNAL_FFS __builtin_ffs\" >>confdefs.h\n\nelse\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether a program using ffsl is compilable\" >&5\n$as_echo_n \"checking whether a program using ffsl is compilable... \" >&6; }\nif ${je_cv_function_ffsl+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n  #include <stdio.h>\n  #include <strings.h>\n  #include <string.h>\n\nint\nmain ()\n{\n\n\t{\n\t\tint rv = ffsl(0x08);\n\t\tprintf(\"%d\\n\", rv);\n\t}\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_function_ffsl=yes\nelse\n  je_cv_function_ffsl=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_function_ffsl\" >&5\n$as_echo \"$je_cv_function_ffsl\" >&6; }\n\n  if test \"x${je_cv_function_ffsl}\" = \"xyes\" ; then\n    $as_echo \"#define JEMALLOC_INTERNAL_FFSLL ffsll\" >>confdefs.h\n\n    $as_echo \"#define JEMALLOC_INTERNAL_FFSL ffsl\" >>confdefs.h\n\n    $as_echo \"#define JEMALLOC_INTERNAL_FFS ffs\" >>confdefs.h\n\n  else\n    as_fn_error $? \"Cannot build without ffsl(3) or __builtin_ffsl()\" \"$LINENO\" 5\n  fi\nfi\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether a program using __builtin_popcountl is compilable\" >&5\n$as_echo_n \"checking whether a program using __builtin_popcountl is compilable... \" >&6; }\nif ${je_cv_gcc_builtin_popcountl+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n#include <stdio.h>\n#include <strings.h>\n#include <string.h>\n\nint\nmain ()\n{\n\n\t{\n\t\tint rv = __builtin_popcountl(0x08);\n\t\tprintf(\"%d\\n\", rv);\n\t}\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_gcc_builtin_popcountl=yes\nelse\n  je_cv_gcc_builtin_popcountl=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_gcc_builtin_popcountl\" >&5\n$as_echo \"$je_cv_gcc_builtin_popcountl\" >&6; }\n\nif test \"x${je_cv_gcc_builtin_popcountl}\" = \"xyes\" ; then\n  $as_echo \"#define JEMALLOC_INTERNAL_POPCOUNT __builtin_popcount\" >>confdefs.h\n\n  $as_echo \"#define JEMALLOC_INTERNAL_POPCOUNTL __builtin_popcountl\" >>confdefs.h\n\nfi\n\n\n# Check whether --with-lg_quantum was given.\nif test \"${with_lg_quantum+set}\" = set; then :\n  withval=$with_lg_quantum; LG_QUANTA=\"$with_lg_quantum\"\nelse\n  LG_QUANTA=\"3 4\"\nfi\n\nif test \"x$with_lg_quantum\" != \"x\" ; then\n  cat >>confdefs.h <<_ACEOF\n#define LG_QUANTUM $with_lg_quantum\n_ACEOF\n\nfi\n\n\n# Check whether --with-lg_page was given.\nif test \"${with_lg_page+set}\" = set; then :\n  withval=$with_lg_page; LG_PAGE=\"$with_lg_page\"\nelse\n  LG_PAGE=\"detect\"\nfi\n\nif test \"x$LG_PAGE\" = \"xdetect\"; then\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: checking LG_PAGE\" >&5\n$as_echo_n \"checking LG_PAGE... \" >&6; }\nif ${je_cv_lg_page+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  if test \"$cross_compiling\" = yes; then :\n  je_cv_lg_page=12\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n#include <strings.h>\n#ifdef _WIN32\n#include <windows.h>\n#else\n#include <unistd.h>\n#endif\n#include <stdio.h>\n\nint\nmain ()\n{\n\n    int result;\n    FILE *f;\n\n#ifdef _WIN32\n    SYSTEM_INFO si;\n    GetSystemInfo(&si);\n    result = si.dwPageSize;\n#else\n    result = sysconf(_SC_PAGESIZE);\n#endif\n    if (result == -1) {\n\treturn 1;\n    }\n    result = JEMALLOC_INTERNAL_FFSL(result) - 1;\n\n    f = fopen(\"conftest.out\", \"w\");\n    if (f == NULL) {\n\treturn 1;\n    }\n    fprintf(f, \"%d\", result);\n    fclose(f);\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_run \"$LINENO\"; then :\n  je_cv_lg_page=`cat conftest.out`\nelse\n  je_cv_lg_page=undefined\nfi\nrm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext \\\n  conftest.$ac_objext conftest.beam conftest.$ac_ext\nfi\n\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_lg_page\" >&5\n$as_echo \"$je_cv_lg_page\" >&6; }\nfi\nif test \"x${je_cv_lg_page}\" != \"x\" ; then\n  LG_PAGE=\"${je_cv_lg_page}\"\nfi\nif test \"x${LG_PAGE}\" != \"xundefined\" ; then\n   cat >>confdefs.h <<_ACEOF\n#define LG_PAGE $LG_PAGE\n_ACEOF\n\nelse\n   as_fn_error $? \"cannot determine value for LG_PAGE\" \"$LINENO\" 5\nfi\n\n\n# Check whether --with-lg_hugepage was given.\nif test \"${with_lg_hugepage+set}\" = set; then :\n  withval=$with_lg_hugepage; je_cv_lg_hugepage=\"${with_lg_hugepage}\"\nelse\n  je_cv_lg_hugepage=\"\"\nfi\n\nif test \"x${je_cv_lg_hugepage}\" = \"x\" ; then\n          if test -e \"/proc/meminfo\" ; then\n    hpsk=`cat /proc/meminfo 2>/dev/null | \\\n          grep -e '^Hugepagesize:[[:space:]]\\+[0-9]\\+[[:space:]]kB$' | \\\n          awk '{print $2}'`\n    if test \"x${hpsk}\" != \"x\" ; then\n      je_cv_lg_hugepage=10\n      while test \"${hpsk}\" -gt 1 ; do\n        hpsk=\"$((hpsk / 2))\"\n        je_cv_lg_hugepage=\"$((je_cv_lg_hugepage + 1))\"\n      done\n    fi\n  fi\n\n    if test \"x${je_cv_lg_hugepage}\" = \"x\" ; then\n    je_cv_lg_hugepage=21\n  fi\nfi\nif test \"x${LG_PAGE}\" != \"xundefined\" -a \\\n        \"${je_cv_lg_hugepage}\" -lt \"${LG_PAGE}\" ; then\n  as_fn_error $? \"Huge page size (2^${je_cv_lg_hugepage}) must be at least page size (2^${LG_PAGE})\" \"$LINENO\" 5\nfi\ncat >>confdefs.h <<_ACEOF\n#define LG_HUGEPAGE ${je_cv_lg_hugepage}\n_ACEOF\n\n\n# Check whether --enable-libdl was given.\nif test \"${enable_libdl+set}\" = set; then :\n  enableval=$enable_libdl; if test \"x$enable_libdl\" = \"xno\" ; then\n  enable_libdl=\"0\"\nelse\n  enable_libdl=\"1\"\nfi\n\nelse\n  enable_libdl=\"1\"\n\nfi\n\n\n\n\nif test \"x$abi\" != \"xpecoff\" ; then\n  $as_echo \"#define JEMALLOC_HAVE_PTHREAD  \" >>confdefs.h\n\n  for ac_header in pthread.h\ndo :\n  ac_fn_c_check_header_mongrel \"$LINENO\" \"pthread.h\" \"ac_cv_header_pthread_h\" \"$ac_includes_default\"\nif test \"x$ac_cv_header_pthread_h\" = xyes; then :\n  cat >>confdefs.h <<_ACEOF\n#define HAVE_PTHREAD_H 1\n_ACEOF\n\nelse\n  as_fn_error $? \"pthread.h is missing\" \"$LINENO\" 5\nfi\n\ndone\n\n      { $as_echo \"$as_me:${as_lineno-$LINENO}: checking for pthread_create in -lpthread\" >&5\n$as_echo_n \"checking for pthread_create in -lpthread... \" >&6; }\nif ${ac_cv_lib_pthread_pthread_create+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  ac_check_lib_save_LIBS=$LIBS\nLIBS=\"-lpthread  $LIBS\"\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n/* Override any GCC internal prototype to avoid an error.\n   Use char because int might match the return type of a GCC\n   builtin and then its argument prototype would still apply.  */\n#ifdef __cplusplus\nextern \"C\"\n#endif\nchar pthread_create ();\nint\nmain ()\n{\nreturn pthread_create ();\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  ac_cv_lib_pthread_pthread_create=yes\nelse\n  ac_cv_lib_pthread_pthread_create=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nLIBS=$ac_check_lib_save_LIBS\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_pthread_pthread_create\" >&5\n$as_echo \"$ac_cv_lib_pthread_pthread_create\" >&6; }\nif test \"x$ac_cv_lib_pthread_pthread_create\" = xyes; then :\n  T_APPEND_V=-pthread\n  if test \"x${LIBS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  LIBS=\"${LIBS}${T_APPEND_V}\"\nelse\n  LIBS=\"${LIBS} ${T_APPEND_V}\"\nfi\n\n\nelse\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: checking for library containing pthread_create\" >&5\n$as_echo_n \"checking for library containing pthread_create... \" >&6; }\nif ${ac_cv_search_pthread_create+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  ac_func_search_save_LIBS=$LIBS\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n/* Override any GCC internal prototype to avoid an error.\n   Use char because int might match the return type of a GCC\n   builtin and then its argument prototype would still apply.  */\n#ifdef __cplusplus\nextern \"C\"\n#endif\nchar pthread_create ();\nint\nmain ()\n{\nreturn pthread_create ();\n  ;\n  return 0;\n}\n_ACEOF\nfor ac_lib in '' ; do\n  if test -z \"$ac_lib\"; then\n    ac_res=\"none required\"\n  else\n    ac_res=-l$ac_lib\n    LIBS=\"-l$ac_lib  $ac_func_search_save_LIBS\"\n  fi\n  if ac_fn_c_try_link \"$LINENO\"; then :\n  ac_cv_search_pthread_create=$ac_res\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext\n  if ${ac_cv_search_pthread_create+:} false; then :\n  break\nfi\ndone\nif ${ac_cv_search_pthread_create+:} false; then :\n\nelse\n  ac_cv_search_pthread_create=no\nfi\nrm conftest.$ac_ext\nLIBS=$ac_func_search_save_LIBS\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_pthread_create\" >&5\n$as_echo \"$ac_cv_search_pthread_create\" >&6; }\nac_res=$ac_cv_search_pthread_create\nif test \"$ac_res\" != no; then :\n  test \"$ac_res\" = \"none required\" || LIBS=\"$ac_res $LIBS\"\n\nelse\n  as_fn_error $? \"libpthread is missing\" \"$LINENO\" 5\nfi\n\nfi\n\n  wrap_syms=\"${wrap_syms} pthread_create\"\n  have_pthread=\"1\"\n\n  if test \"x$enable_libdl\" = \"x1\" ; then\n    have_dlsym=\"1\"\n    for ac_header in dlfcn.h\ndo :\n  ac_fn_c_check_header_mongrel \"$LINENO\" \"dlfcn.h\" \"ac_cv_header_dlfcn_h\" \"$ac_includes_default\"\nif test \"x$ac_cv_header_dlfcn_h\" = xyes; then :\n  cat >>confdefs.h <<_ACEOF\n#define HAVE_DLFCN_H 1\n_ACEOF\n ac_fn_c_check_func \"$LINENO\" \"dlsym\" \"ac_cv_func_dlsym\"\nif test \"x$ac_cv_func_dlsym\" = xyes; then :\n\nelse\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: checking for dlsym in -ldl\" >&5\n$as_echo_n \"checking for dlsym in -ldl... \" >&6; }\nif ${ac_cv_lib_dl_dlsym+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  ac_check_lib_save_LIBS=$LIBS\nLIBS=\"-ldl  $LIBS\"\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n/* Override any GCC internal prototype to avoid an error.\n   Use char because int might match the return type of a GCC\n   builtin and then its argument prototype would still apply.  */\n#ifdef __cplusplus\nextern \"C\"\n#endif\nchar dlsym ();\nint\nmain ()\n{\nreturn dlsym ();\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  ac_cv_lib_dl_dlsym=yes\nelse\n  ac_cv_lib_dl_dlsym=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nLIBS=$ac_check_lib_save_LIBS\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_dl_dlsym\" >&5\n$as_echo \"$ac_cv_lib_dl_dlsym\" >&6; }\nif test \"x$ac_cv_lib_dl_dlsym\" = xyes; then :\n  LIBS=\"$LIBS -ldl\"\nelse\n  have_dlsym=\"0\"\nfi\n\nfi\n\nelse\n  have_dlsym=\"0\"\nfi\n\ndone\n\n    if test \"x$have_dlsym\" = \"x1\" ; then\n      $as_echo \"#define JEMALLOC_HAVE_DLSYM  \" >>confdefs.h\n\n    fi\n  else\n    have_dlsym=\"0\"\n  fi\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether pthread_atfork(3) is compilable\" >&5\n$as_echo_n \"checking whether pthread_atfork(3) is compilable... \" >&6; }\nif ${je_cv_pthread_atfork+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n#include <pthread.h>\n\nint\nmain ()\n{\n\n  pthread_atfork((void *)0, (void *)0, (void *)0);\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_pthread_atfork=yes\nelse\n  je_cv_pthread_atfork=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_pthread_atfork\" >&5\n$as_echo \"$je_cv_pthread_atfork\" >&6; }\n\n  if test \"x${je_cv_pthread_atfork}\" = \"xyes\" ; then\n    $as_echo \"#define JEMALLOC_HAVE_PTHREAD_ATFORK  \" >>confdefs.h\n\n  fi\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether pthread_setname_np(3) is compilable\" >&5\n$as_echo_n \"checking whether pthread_setname_np(3) is compilable... \" >&6; }\nif ${je_cv_pthread_setname_np+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n#include <pthread.h>\n\nint\nmain ()\n{\n\n  pthread_setname_np(pthread_self(), \"setname_test\");\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_pthread_setname_np=yes\nelse\n  je_cv_pthread_setname_np=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_pthread_setname_np\" >&5\n$as_echo \"$je_cv_pthread_setname_np\" >&6; }\n\n  if test \"x${je_cv_pthread_setname_np}\" = \"xyes\" ; then\n    $as_echo \"#define JEMALLOC_HAVE_PTHREAD_SETNAME_NP  \" >>confdefs.h\n\n  fi\nfi\n\nT_APPEND_V=-D_REENTRANT\n  if test \"x${CPPFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CPPFLAGS=\"${CPPFLAGS}${T_APPEND_V}\"\nelse\n  CPPFLAGS=\"${CPPFLAGS} ${T_APPEND_V}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for library containing clock_gettime\" >&5\n$as_echo_n \"checking for library containing clock_gettime... \" >&6; }\nif ${ac_cv_search_clock_gettime+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  ac_func_search_save_LIBS=$LIBS\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n/* Override any GCC internal prototype to avoid an error.\n   Use char because int might match the return type of a GCC\n   builtin and then its argument prototype would still apply.  */\n#ifdef __cplusplus\nextern \"C\"\n#endif\nchar clock_gettime ();\nint\nmain ()\n{\nreturn clock_gettime ();\n  ;\n  return 0;\n}\n_ACEOF\nfor ac_lib in '' rt; do\n  if test -z \"$ac_lib\"; then\n    ac_res=\"none required\"\n  else\n    ac_res=-l$ac_lib\n    LIBS=\"-l$ac_lib  $ac_func_search_save_LIBS\"\n  fi\n  if ac_fn_c_try_link \"$LINENO\"; then :\n  ac_cv_search_clock_gettime=$ac_res\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext\n  if ${ac_cv_search_clock_gettime+:} false; then :\n  break\nfi\ndone\nif ${ac_cv_search_clock_gettime+:} false; then :\n\nelse\n  ac_cv_search_clock_gettime=no\nfi\nrm conftest.$ac_ext\nLIBS=$ac_func_search_save_LIBS\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_clock_gettime\" >&5\n$as_echo \"$ac_cv_search_clock_gettime\" >&6; }\nac_res=$ac_cv_search_clock_gettime\nif test \"$ac_res\" != no; then :\n  test \"$ac_res\" = \"none required\" || LIBS=\"$ac_res $LIBS\"\n\nfi\n\n\nif test \"x$je_cv_cray_prgenv_wrapper\" = \"xyes\" ; then\n  if test \"$ac_cv_search_clock_gettime\" != \"-lrt\"; then\n    SAVED_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\n\n\n    unset ac_cv_search_clock_gettime\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -dynamic\" >&5\n$as_echo_n \"checking whether compiler supports -dynamic... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-dynamic\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-dynamic\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n    { $as_echo \"$as_me:${as_lineno-$LINENO}: checking for library containing clock_gettime\" >&5\n$as_echo_n \"checking for library containing clock_gettime... \" >&6; }\nif ${ac_cv_search_clock_gettime+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  ac_func_search_save_LIBS=$LIBS\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n/* Override any GCC internal prototype to avoid an error.\n   Use char because int might match the return type of a GCC\n   builtin and then its argument prototype would still apply.  */\n#ifdef __cplusplus\nextern \"C\"\n#endif\nchar clock_gettime ();\nint\nmain ()\n{\nreturn clock_gettime ();\n  ;\n  return 0;\n}\n_ACEOF\nfor ac_lib in '' rt; do\n  if test -z \"$ac_lib\"; then\n    ac_res=\"none required\"\n  else\n    ac_res=-l$ac_lib\n    LIBS=\"-l$ac_lib  $ac_func_search_save_LIBS\"\n  fi\n  if ac_fn_c_try_link \"$LINENO\"; then :\n  ac_cv_search_clock_gettime=$ac_res\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext\n  if ${ac_cv_search_clock_gettime+:} false; then :\n  break\nfi\ndone\nif ${ac_cv_search_clock_gettime+:} false; then :\n\nelse\n  ac_cv_search_clock_gettime=no\nfi\nrm conftest.$ac_ext\nLIBS=$ac_func_search_save_LIBS\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_clock_gettime\" >&5\n$as_echo \"$ac_cv_search_clock_gettime\" >&6; }\nac_res=$ac_cv_search_clock_gettime\nif test \"$ac_res\" != no; then :\n  test \"$ac_res\" = \"none required\" || LIBS=\"$ac_res $LIBS\"\n\nfi\n\n\n    CONFIGURE_CFLAGS=\"${SAVED_CONFIGURE_CFLAGS}\"\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n  fi\nfi\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether clock_gettime(CLOCK_MONOTONIC_COARSE, ...) is compilable\" >&5\n$as_echo_n \"checking whether clock_gettime(CLOCK_MONOTONIC_COARSE, ...) is compilable... \" >&6; }\nif ${je_cv_clock_monotonic_coarse+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n#include <time.h>\n\nint\nmain ()\n{\n\n\tstruct timespec ts;\n\n\tclock_gettime(CLOCK_MONOTONIC_COARSE, &ts);\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_clock_monotonic_coarse=yes\nelse\n  je_cv_clock_monotonic_coarse=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_clock_monotonic_coarse\" >&5\n$as_echo \"$je_cv_clock_monotonic_coarse\" >&6; }\n\nif test \"x${je_cv_clock_monotonic_coarse}\" = \"xyes\" ; then\n  $as_echo \"#define JEMALLOC_HAVE_CLOCK_MONOTONIC_COARSE 1\" >>confdefs.h\n\nfi\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether clock_gettime(CLOCK_MONOTONIC, ...) is compilable\" >&5\n$as_echo_n \"checking whether clock_gettime(CLOCK_MONOTONIC, ...) is compilable... \" >&6; }\nif ${je_cv_clock_monotonic+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n#include <unistd.h>\n#include <time.h>\n\nint\nmain ()\n{\n\n\tstruct timespec ts;\n\n\tclock_gettime(CLOCK_MONOTONIC, &ts);\n#if !defined(_POSIX_MONOTONIC_CLOCK) || _POSIX_MONOTONIC_CLOCK < 0\n#  error _POSIX_MONOTONIC_CLOCK missing/invalid\n#endif\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_clock_monotonic=yes\nelse\n  je_cv_clock_monotonic=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_clock_monotonic\" >&5\n$as_echo \"$je_cv_clock_monotonic\" >&6; }\n\nif test \"x${je_cv_clock_monotonic}\" = \"xyes\" ; then\n  $as_echo \"#define JEMALLOC_HAVE_CLOCK_MONOTONIC 1\" >>confdefs.h\n\nfi\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether mach_absolute_time() is compilable\" >&5\n$as_echo_n \"checking whether mach_absolute_time() is compilable... \" >&6; }\nif ${je_cv_mach_absolute_time+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n#include <mach/mach_time.h>\n\nint\nmain ()\n{\n\n\tmach_absolute_time();\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_mach_absolute_time=yes\nelse\n  je_cv_mach_absolute_time=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_mach_absolute_time\" >&5\n$as_echo \"$je_cv_mach_absolute_time\" >&6; }\n\nif test \"x${je_cv_mach_absolute_time}\" = \"xyes\" ; then\n  $as_echo \"#define JEMALLOC_HAVE_MACH_ABSOLUTE_TIME 1\" >>confdefs.h\n\nfi\n\n# Check whether --enable-syscall was given.\nif test \"${enable_syscall+set}\" = set; then :\n  enableval=$enable_syscall; if test \"x$enable_syscall\" = \"xno\" ; then\n  enable_syscall=\"0\"\nelse\n  enable_syscall=\"1\"\nfi\n\nelse\n  enable_syscall=\"1\"\n\nfi\n\nif test \"x$enable_syscall\" = \"x1\" ; then\n      SAVED_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -Werror\" >&5\n$as_echo_n \"checking whether compiler supports -Werror... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-Werror\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-Werror\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether syscall(2) is compilable\" >&5\n$as_echo_n \"checking whether syscall(2) is compilable... \" >&6; }\nif ${je_cv_syscall+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n#include <sys/syscall.h>\n#include <unistd.h>\n\nint\nmain ()\n{\n\n\tsyscall(SYS_write, 2, \"hello\", 5);\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_syscall=yes\nelse\n  je_cv_syscall=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_syscall\" >&5\n$as_echo \"$je_cv_syscall\" >&6; }\n\n  CONFIGURE_CFLAGS=\"${SAVED_CONFIGURE_CFLAGS}\"\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n  if test \"x$je_cv_syscall\" = \"xyes\" ; then\n    $as_echo \"#define JEMALLOC_USE_SYSCALL  \" >>confdefs.h\n\n  fi\nfi\n\nac_fn_c_check_func \"$LINENO\" \"secure_getenv\" \"ac_cv_func_secure_getenv\"\nif test \"x$ac_cv_func_secure_getenv\" = xyes; then :\n  have_secure_getenv=\"1\"\nelse\n  have_secure_getenv=\"0\"\n\nfi\n\nif test \"x$have_secure_getenv\" = \"x1\" ; then\n  $as_echo \"#define JEMALLOC_HAVE_SECURE_GETENV  \" >>confdefs.h\n\nfi\n\nac_fn_c_check_func \"$LINENO\" \"sched_getcpu\" \"ac_cv_func_sched_getcpu\"\nif test \"x$ac_cv_func_sched_getcpu\" = xyes; then :\n  have_sched_getcpu=\"1\"\nelse\n  have_sched_getcpu=\"0\"\n\nfi\n\nif test \"x$have_sched_getcpu\" = \"x1\" ; then\n  $as_echo \"#define JEMALLOC_HAVE_SCHED_GETCPU  \" >>confdefs.h\n\nfi\n\nac_fn_c_check_func \"$LINENO\" \"sched_setaffinity\" \"ac_cv_func_sched_setaffinity\"\nif test \"x$ac_cv_func_sched_setaffinity\" = xyes; then :\n  have_sched_setaffinity=\"1\"\nelse\n  have_sched_setaffinity=\"0\"\n\nfi\n\nif test \"x$have_sched_setaffinity\" = \"x1\" ; then\n  $as_echo \"#define JEMALLOC_HAVE_SCHED_SETAFFINITY  \" >>confdefs.h\n\nfi\n\nac_fn_c_check_func \"$LINENO\" \"issetugid\" \"ac_cv_func_issetugid\"\nif test \"x$ac_cv_func_issetugid\" = xyes; then :\n  have_issetugid=\"1\"\nelse\n  have_issetugid=\"0\"\n\nfi\n\nif test \"x$have_issetugid\" = \"x1\" ; then\n  $as_echo \"#define JEMALLOC_HAVE_ISSETUGID  \" >>confdefs.h\n\nfi\n\nac_fn_c_check_func \"$LINENO\" \"_malloc_thread_cleanup\" \"ac_cv_func__malloc_thread_cleanup\"\nif test \"x$ac_cv_func__malloc_thread_cleanup\" = xyes; then :\n  have__malloc_thread_cleanup=\"1\"\nelse\n  have__malloc_thread_cleanup=\"0\"\n\nfi\n\nif test \"x$have__malloc_thread_cleanup\" = \"x1\" ; then\n  $as_echo \"#define JEMALLOC_MALLOC_THREAD_CLEANUP  \" >>confdefs.h\n\n  wrap_syms=\"${wrap_syms} _malloc_thread_cleanup\"\n  force_tls=\"1\"\nfi\n\nac_fn_c_check_func \"$LINENO\" \"_pthread_mutex_init_calloc_cb\" \"ac_cv_func__pthread_mutex_init_calloc_cb\"\nif test \"x$ac_cv_func__pthread_mutex_init_calloc_cb\" = xyes; then :\n  have__pthread_mutex_init_calloc_cb=\"1\"\nelse\n  have__pthread_mutex_init_calloc_cb=\"0\"\n\nfi\n\nif test \"x$have__pthread_mutex_init_calloc_cb\" = \"x1\" ; then\n  $as_echo \"#define JEMALLOC_MUTEX_INIT_CB 1\" >>confdefs.h\n\n  wrap_syms=\"${wrap_syms} _malloc_prefork _malloc_postfork\"\nfi\n\n# Check whether --enable-lazy_lock was given.\nif test \"${enable_lazy_lock+set}\" = set; then :\n  enableval=$enable_lazy_lock; if test \"x$enable_lazy_lock\" = \"xno\" ; then\n  enable_lazy_lock=\"0\"\nelse\n  enable_lazy_lock=\"1\"\nfi\n\nelse\n  enable_lazy_lock=\"\"\n\nfi\n\nif test \"x${enable_lazy_lock}\" = \"x\" ; then\n  if test \"x${force_lazy_lock}\" = \"x1\" ; then\n    { $as_echo \"$as_me:${as_lineno-$LINENO}: result: Forcing lazy-lock to avoid allocator/threading bootstrap issues\" >&5\n$as_echo \"Forcing lazy-lock to avoid allocator/threading bootstrap issues\" >&6; }\n    enable_lazy_lock=\"1\"\n  else\n    enable_lazy_lock=\"0\"\n  fi\nfi\nif test \"x${enable_lazy_lock}\" = \"x1\" -a \"x${abi}\" = \"xpecoff\" ; then\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: Forcing no lazy-lock because thread creation monitoring is unimplemented\" >&5\n$as_echo \"Forcing no lazy-lock because thread creation monitoring is unimplemented\" >&6; }\n  enable_lazy_lock=\"0\"\nfi\nif test \"x$enable_lazy_lock\" = \"x1\" ; then\n  if test \"x$have_dlsym\" = \"x1\" ; then\n    $as_echo \"#define JEMALLOC_LAZY_LOCK  \" >>confdefs.h\n\n  else\n    as_fn_error $? \"Missing dlsym support: lazy-lock cannot be enabled.\" \"$LINENO\" 5\n  fi\nfi\n\n\nif test \"x${force_tls}\" = \"x1\" ; then\n  enable_tls=\"1\"\nelif test \"x${force_tls}\" = \"x0\" ; then\n  enable_tls=\"0\"\nelse\n  enable_tls=\"1\"\nfi\nif test \"x${enable_tls}\" = \"x1\" ; then\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for TLS\" >&5\n$as_echo_n \"checking for TLS... \" >&6; }\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n    __thread int x;\n\nint\nmain ()\n{\n\n    x = 42;\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              enable_tls=\"0\"\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nelse\n  enable_tls=\"0\"\nfi\n\nif test \"x${enable_tls}\" = \"x1\" ; then\n  cat >>confdefs.h <<_ACEOF\n#define JEMALLOC_TLS\n_ACEOF\n\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether C11 atomics is compilable\" >&5\n$as_echo_n \"checking whether C11 atomics is compilable... \" >&6; }\nif ${je_cv_c11_atomics+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n#include <stdint.h>\n#if (__STDC_VERSION__ >= 201112L) && !defined(__STDC_NO_ATOMICS__)\n#include <stdatomic.h>\n#else\n#error Atomics not available\n#endif\n\nint\nmain ()\n{\n\n    uint64_t *p = (uint64_t *)0;\n    uint64_t x = 1;\n    volatile atomic_uint_least64_t *a = (volatile atomic_uint_least64_t *)p;\n    uint64_t r = atomic_fetch_add(a, x) + x;\n    return r == 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_c11_atomics=yes\nelse\n  je_cv_c11_atomics=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_c11_atomics\" >&5\n$as_echo \"$je_cv_c11_atomics\" >&6; }\n\nif test \"x${je_cv_c11_atomics}\" = \"xyes\" ; then\n  $as_echo \"#define JEMALLOC_C11_ATOMICS 1\" >>confdefs.h\n\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether GCC __atomic atomics is compilable\" >&5\n$as_echo_n \"checking whether GCC __atomic atomics is compilable... \" >&6; }\nif ${je_cv_gcc_atomic_atomics+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    int x = 0;\n    int val = 1;\n    int y = __atomic_fetch_add(&x, val, __ATOMIC_RELAXED);\n    int after_add = x;\n    return after_add == 1;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_gcc_atomic_atomics=yes\nelse\n  je_cv_gcc_atomic_atomics=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_gcc_atomic_atomics\" >&5\n$as_echo \"$je_cv_gcc_atomic_atomics\" >&6; }\n\nif test \"x${je_cv_gcc_atomic_atomics}\" = \"xyes\" ; then\n  $as_echo \"#define JEMALLOC_GCC_ATOMIC_ATOMICS 1\" >>confdefs.h\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether GCC 8-bit __atomic atomics is compilable\" >&5\n$as_echo_n \"checking whether GCC 8-bit __atomic atomics is compilable... \" >&6; }\nif ${je_cv_gcc_u8_atomic_atomics+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n      unsigned char x = 0;\n      int val = 1;\n      int y = __atomic_fetch_add(&x, val, __ATOMIC_RELAXED);\n      int after_add = (int)x;\n      return after_add == 1;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_gcc_u8_atomic_atomics=yes\nelse\n  je_cv_gcc_u8_atomic_atomics=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_gcc_u8_atomic_atomics\" >&5\n$as_echo \"$je_cv_gcc_u8_atomic_atomics\" >&6; }\n\n  if test \"x${je_cv_gcc_u8_atomic_atomics}\" = \"xyes\" ; then\n    $as_echo \"#define JEMALLOC_GCC_U8_ATOMIC_ATOMICS 1\" >>confdefs.h\n\n  fi\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether GCC __sync atomics is compilable\" >&5\n$as_echo_n \"checking whether GCC __sync atomics is compilable... \" >&6; }\nif ${je_cv_gcc_sync_atomics+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    int x = 0;\n    int before_add = __sync_fetch_and_add(&x, 1);\n    int after_add = x;\n    return (before_add == 0) && (after_add == 1);\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_gcc_sync_atomics=yes\nelse\n  je_cv_gcc_sync_atomics=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_gcc_sync_atomics\" >&5\n$as_echo \"$je_cv_gcc_sync_atomics\" >&6; }\n\nif test \"x${je_cv_gcc_sync_atomics}\" = \"xyes\" ; then\n  $as_echo \"#define JEMALLOC_GCC_SYNC_ATOMICS 1\" >>confdefs.h\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether GCC 8-bit __sync atomics is compilable\" >&5\n$as_echo_n \"checking whether GCC 8-bit __sync atomics is compilable... \" >&6; }\nif ${je_cv_gcc_u8_sync_atomics+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n      unsigned char x = 0;\n      int before_add = __sync_fetch_and_add(&x, 1);\n      int after_add = (int)x;\n      return (before_add == 0) && (after_add == 1);\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_gcc_u8_sync_atomics=yes\nelse\n  je_cv_gcc_u8_sync_atomics=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_gcc_u8_sync_atomics\" >&5\n$as_echo \"$je_cv_gcc_u8_sync_atomics\" >&6; }\n\n  if test \"x${je_cv_gcc_u8_sync_atomics}\" = \"xyes\" ; then\n    $as_echo \"#define JEMALLOC_GCC_U8_SYNC_ATOMICS 1\" >>confdefs.h\n\n  fi\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether Darwin OSAtomic*() is compilable\" >&5\n$as_echo_n \"checking whether Darwin OSAtomic*() is compilable... \" >&6; }\nif ${je_cv_osatomic+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n#include <libkern/OSAtomic.h>\n#include <inttypes.h>\n\nint\nmain ()\n{\n\n\t{\n\t\tint32_t x32 = 0;\n\t\tvolatile int32_t *x32p = &x32;\n\t\tOSAtomicAdd32(1, x32p);\n\t}\n\t{\n\t\tint64_t x64 = 0;\n\t\tvolatile int64_t *x64p = &x64;\n\t\tOSAtomicAdd64(1, x64p);\n\t}\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_osatomic=yes\nelse\n  je_cv_osatomic=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_osatomic\" >&5\n$as_echo \"$je_cv_osatomic\" >&6; }\n\nif test \"x${je_cv_osatomic}\" = \"xyes\" ; then\n  $as_echo \"#define JEMALLOC_OSATOMIC  \" >>confdefs.h\n\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether madvise(2) is compilable\" >&5\n$as_echo_n \"checking whether madvise(2) is compilable... \" >&6; }\nif ${je_cv_madvise+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n#include <sys/mman.h>\n\nint\nmain ()\n{\n\n\tmadvise((void *)0, 0, 0);\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_madvise=yes\nelse\n  je_cv_madvise=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_madvise\" >&5\n$as_echo \"$je_cv_madvise\" >&6; }\n\nif test \"x${je_cv_madvise}\" = \"xyes\" ; then\n  $as_echo \"#define JEMALLOC_HAVE_MADVISE  \" >>confdefs.h\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether madvise(..., MADV_FREE) is compilable\" >&5\n$as_echo_n \"checking whether madvise(..., MADV_FREE) is compilable... \" >&6; }\nif ${je_cv_madv_free+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n#include <sys/mman.h>\n\nint\nmain ()\n{\n\n\tmadvise((void *)0, 0, MADV_FREE);\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_madv_free=yes\nelse\n  je_cv_madv_free=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_madv_free\" >&5\n$as_echo \"$je_cv_madv_free\" >&6; }\n\n  if test \"x${je_cv_madv_free}\" = \"xyes\" ; then\n    $as_echo \"#define JEMALLOC_PURGE_MADVISE_FREE  \" >>confdefs.h\n\n  elif test \"x${je_cv_madvise}\" = \"xyes\" ; then\n    case \"${host_cpu}\" in i686|x86_64)\n        case \"${host}\" in *-*-linux*)\n            $as_echo \"#define JEMALLOC_PURGE_MADVISE_FREE  \" >>confdefs.h\n\n            $as_echo \"#define JEMALLOC_DEFINE_MADVISE_FREE  \" >>confdefs.h\n\n\t    ;;\n        esac\n        ;;\n    esac\n  fi\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether madvise(..., MADV_DONTNEED) is compilable\" >&5\n$as_echo_n \"checking whether madvise(..., MADV_DONTNEED) is compilable... \" >&6; }\nif ${je_cv_madv_dontneed+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n#include <sys/mman.h>\n\nint\nmain ()\n{\n\n\tmadvise((void *)0, 0, MADV_DONTNEED);\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_madv_dontneed=yes\nelse\n  je_cv_madv_dontneed=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_madv_dontneed\" >&5\n$as_echo \"$je_cv_madv_dontneed\" >&6; }\n\n  if test \"x${je_cv_madv_dontneed}\" = \"xyes\" ; then\n    $as_echo \"#define JEMALLOC_PURGE_MADVISE_DONTNEED  \" >>confdefs.h\n\n  fi\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether madvise(..., MADV_DO[NT]DUMP) is compilable\" >&5\n$as_echo_n \"checking whether madvise(..., MADV_DO[NT]DUMP) is compilable... \" >&6; }\nif ${je_cv_madv_dontdump+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n#include <sys/mman.h>\n\nint\nmain ()\n{\n\n\tmadvise((void *)0, 0, MADV_DONTDUMP);\n\tmadvise((void *)0, 0, MADV_DODUMP);\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_madv_dontdump=yes\nelse\n  je_cv_madv_dontdump=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_madv_dontdump\" >&5\n$as_echo \"$je_cv_madv_dontdump\" >&6; }\n\n  if test \"x${je_cv_madv_dontdump}\" = \"xyes\" ; then\n    $as_echo \"#define JEMALLOC_MADVISE_DONTDUMP  \" >>confdefs.h\n\n  fi\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether madvise(..., MADV_[NO]HUGEPAGE) is compilable\" >&5\n$as_echo_n \"checking whether madvise(..., MADV_[NO]HUGEPAGE) is compilable... \" >&6; }\nif ${je_cv_thp+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n#include <sys/mman.h>\n\nint\nmain ()\n{\n\n\tmadvise((void *)0, 0, MADV_HUGEPAGE);\n\tmadvise((void *)0, 0, MADV_NOHUGEPAGE);\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_thp=yes\nelse\n  je_cv_thp=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_thp\" >&5\n$as_echo \"$je_cv_thp\" >&6; }\n\ncase \"${host_cpu}\" in\n  arm*)\n    ;;\n  *)\n  if test \"x${je_cv_thp}\" = \"xyes\" ; then\n    $as_echo \"#define JEMALLOC_HAVE_MADVISE_HUGE  \" >>confdefs.h\n\n  fi\n  ;;\nesac\nfi\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for __builtin_clz\" >&5\n$as_echo_n \"checking for __builtin_clz... \" >&6; }\nif ${je_cv_builtin_clz+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\nint\nmain ()\n{\n\n                                                {\n                                                        unsigned x = 0;\n                                                        int y = __builtin_clz(x);\n                                                }\n                                                {\n                                                        unsigned long x = 0;\n                                                        int y = __builtin_clzl(x);\n                                                }\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_builtin_clz=yes\nelse\n  je_cv_builtin_clz=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_builtin_clz\" >&5\n$as_echo \"$je_cv_builtin_clz\" >&6; }\n\nif test \"x${je_cv_builtin_clz}\" = \"xyes\" ; then\n  $as_echo \"#define JEMALLOC_HAVE_BUILTIN_CLZ  \" >>confdefs.h\n\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether Darwin os_unfair_lock_*() is compilable\" >&5\n$as_echo_n \"checking whether Darwin os_unfair_lock_*() is compilable... \" >&6; }\nif ${je_cv_os_unfair_lock+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n#include <os/lock.h>\n#include <AvailabilityMacros.h>\n\nint\nmain ()\n{\n\n\t#if MAC_OS_X_VERSION_MIN_REQUIRED < 101200\n\t#error \"os_unfair_lock is not supported\"\n\t#else\n\tos_unfair_lock lock = OS_UNFAIR_LOCK_INIT;\n\tos_unfair_lock_lock(&lock);\n\tos_unfair_lock_unlock(&lock);\n\t#endif\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_os_unfair_lock=yes\nelse\n  je_cv_os_unfair_lock=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_os_unfair_lock\" >&5\n$as_echo \"$je_cv_os_unfair_lock\" >&6; }\n\nif test \"x${je_cv_os_unfair_lock}\" = \"xyes\" ; then\n  $as_echo \"#define JEMALLOC_OS_UNFAIR_LOCK  \" >>confdefs.h\n\nfi\n\n\n# Check whether --enable-zone-allocator was given.\nif test \"${enable_zone_allocator+set}\" = set; then :\n  enableval=$enable_zone_allocator; if test \"x$enable_zone_allocator\" = \"xno\" ; then\n  enable_zone_allocator=\"0\"\nelse\n  enable_zone_allocator=\"1\"\nfi\n\nelse\n  if test \"x${abi}\" = \"xmacho\"; then\n  enable_zone_allocator=\"1\"\nfi\n\n\nfi\n\n\n\nif test \"x${enable_zone_allocator}\" = \"x1\" ; then\n  if test \"x${abi}\" != \"xmacho\"; then\n    as_fn_error $? \"--enable-zone-allocator is only supported on Darwin\" \"$LINENO\" 5\n  fi\n  $as_echo \"#define JEMALLOC_ZONE  \" >>confdefs.h\n\nfi\n\n# Check whether --enable-initial-exec-tls was given.\nif test \"${enable_initial_exec_tls+set}\" = set; then :\n  enableval=$enable_initial_exec_tls; if test \"x$enable_initial_exec_tls\" = \"xno\" ; then\n  enable_initial_exec_tls=\"0\"\nelse\n  enable_initial_exec_tls=\"1\"\nfi\n\nelse\n  enable_initial_exec_tls=\"1\"\n\nfi\n\n\n\nif test \"x${je_cv_tls_model}\" = \"xyes\" -a \\\n       \"x${enable_initial_exec_tls}\" = \"x1\" ; then\n  $as_echo \"#define JEMALLOC_TLS_MODEL __attribute__((tls_model(\\\"initial-exec\\\")))\" >>confdefs.h\n\nelse\n  $as_echo \"#define JEMALLOC_TLS_MODEL  \" >>confdefs.h\n\nfi\n\n\nif test \"x${have_pthread}\" = \"x1\" -a \"x${je_cv_os_unfair_lock}\" != \"xyes\" ; then\n  $as_echo \"#define JEMALLOC_BACKGROUND_THREAD 1\" >>confdefs.h\n\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether glibc malloc hook is compilable\" >&5\n$as_echo_n \"checking whether glibc malloc hook is compilable... \" >&6; }\nif ${je_cv_glibc_malloc_hook+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n#include <stddef.h>\n\nextern void (* __free_hook)(void *ptr);\nextern void *(* __malloc_hook)(size_t size);\nextern void *(* __realloc_hook)(void *ptr, size_t size);\n\nint\nmain ()\n{\n\n  void *ptr = 0L;\n  if (__malloc_hook) ptr = __malloc_hook(1);\n  if (__realloc_hook) ptr = __realloc_hook(ptr, 2);\n  if (__free_hook && ptr) __free_hook(ptr);\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_glibc_malloc_hook=yes\nelse\n  je_cv_glibc_malloc_hook=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_glibc_malloc_hook\" >&5\n$as_echo \"$je_cv_glibc_malloc_hook\" >&6; }\n\nif test \"x${je_cv_glibc_malloc_hook}\" = \"xyes\" ; then\n  if test \"x${JEMALLOC_PREFIX}\" = \"x\" ; then\n    $as_echo \"#define JEMALLOC_GLIBC_MALLOC_HOOK  \" >>confdefs.h\n\n    wrap_syms=\"${wrap_syms} __free_hook __malloc_hook __realloc_hook\"\n  fi\nfi\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether glibc memalign hook is compilable\" >&5\n$as_echo_n \"checking whether glibc memalign hook is compilable... \" >&6; }\nif ${je_cv_glibc_memalign_hook+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n#include <stddef.h>\n\nextern void *(* __memalign_hook)(size_t alignment, size_t size);\n\nint\nmain ()\n{\n\n  void *ptr = 0L;\n  if (__memalign_hook) ptr = __memalign_hook(16, 7);\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_glibc_memalign_hook=yes\nelse\n  je_cv_glibc_memalign_hook=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_glibc_memalign_hook\" >&5\n$as_echo \"$je_cv_glibc_memalign_hook\" >&6; }\n\nif test \"x${je_cv_glibc_memalign_hook}\" = \"xyes\" ; then\n  if test \"x${JEMALLOC_PREFIX}\" = \"x\" ; then\n    $as_echo \"#define JEMALLOC_GLIBC_MEMALIGN_HOOK  \" >>confdefs.h\n\n    wrap_syms=\"${wrap_syms} __memalign_hook\"\n  fi\nfi\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether pthreads adaptive mutexes is compilable\" >&5\n$as_echo_n \"checking whether pthreads adaptive mutexes is compilable... \" >&6; }\nif ${je_cv_pthread_mutex_adaptive_np+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n#include <pthread.h>\n\nint\nmain ()\n{\n\n  pthread_mutexattr_t attr;\n  pthread_mutexattr_init(&attr);\n  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);\n  pthread_mutexattr_destroy(&attr);\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_pthread_mutex_adaptive_np=yes\nelse\n  je_cv_pthread_mutex_adaptive_np=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_pthread_mutex_adaptive_np\" >&5\n$as_echo \"$je_cv_pthread_mutex_adaptive_np\" >&6; }\n\nif test \"x${je_cv_pthread_mutex_adaptive_np}\" = \"xyes\" ; then\n  $as_echo \"#define JEMALLOC_HAVE_PTHREAD_MUTEX_ADAPTIVE_NP  \" >>confdefs.h\n\nfi\n\nSAVED_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -D_GNU_SOURCE\" >&5\n$as_echo_n \"checking whether compiler supports -D_GNU_SOURCE... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-D_GNU_SOURCE\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-D_GNU_SOURCE\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -Werror\" >&5\n$as_echo_n \"checking whether compiler supports -Werror... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-Werror\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-Werror\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether compiler supports -herror_on_warning\" >&5\n$as_echo_n \"checking whether compiler supports -herror_on_warning... \" >&6; }\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nT_APPEND_V=-herror_on_warning\n  if test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${T_APPEND_V}\" = \"x\" ; then\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}${T_APPEND_V}\"\nelse\n  CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS} ${T_APPEND_V}\"\nfi\n\n\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\ncat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n\nint\nmain ()\n{\n\n    return 0;\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  je_cv_cflags_added=-herror_on_warning\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: yes\" >&5\n$as_echo \"yes\" >&6; }\nelse\n  je_cv_cflags_added=\n              { $as_echo \"$as_me:${as_lineno-$LINENO}: result: no\" >&5\n$as_echo \"no\" >&6; }\n              CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"\n\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking whether strerror_r returns char with gnu source is compilable\" >&5\n$as_echo_n \"checking whether strerror_r returns char with gnu source is compilable... \" >&6; }\nif ${je_cv_strerror_r_returns_char_with_gnu_source+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n#include <errno.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\nint\nmain ()\n{\n\n  char *buffer = (char *) malloc(100);\n  char *error = strerror_r(EINVAL, buffer, 100);\n  printf(\"%s\\n\", error);\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_link \"$LINENO\"; then :\n  je_cv_strerror_r_returns_char_with_gnu_source=yes\nelse\n  je_cv_strerror_r_returns_char_with_gnu_source=no\nfi\nrm -f core conftest.err conftest.$ac_objext \\\n    conftest$ac_exeext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $je_cv_strerror_r_returns_char_with_gnu_source\" >&5\n$as_echo \"$je_cv_strerror_r_returns_char_with_gnu_source\" >&6; }\n\nCONFIGURE_CFLAGS=\"${SAVED_CONFIGURE_CFLAGS}\"\nif test \"x${CONFIGURE_CFLAGS}\" = \"x\" -o \"x${SPECIFIED_CFLAGS}\" = \"x\" ; then\n  CFLAGS=\"${CONFIGURE_CFLAGS}${SPECIFIED_CFLAGS}\"\nelse\n  CFLAGS=\"${CONFIGURE_CFLAGS} ${SPECIFIED_CFLAGS}\"\nfi\n\n\nif test \"x${je_cv_strerror_r_returns_char_with_gnu_source}\" = \"xyes\" ; then\n  $as_echo \"#define JEMALLOC_STRERROR_R_RETURNS_CHAR_WITH_GNU_SOURCE  \" >>confdefs.h\n\nfi\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: checking for stdbool.h that conforms to C99\" >&5\n$as_echo_n \"checking for stdbool.h that conforms to C99... \" >&6; }\nif ${ac_cv_header_stdbool_h+:} false; then :\n  $as_echo_n \"(cached) \" >&6\nelse\n  cat confdefs.h - <<_ACEOF >conftest.$ac_ext\n/* end confdefs.h.  */\n\n             #include <stdbool.h>\n             #ifndef bool\n              \"error: bool is not defined\"\n             #endif\n             #ifndef false\n              \"error: false is not defined\"\n             #endif\n             #if false\n              \"error: false is not 0\"\n             #endif\n             #ifndef true\n              \"error: true is not defined\"\n             #endif\n             #if true != 1\n              \"error: true is not 1\"\n             #endif\n             #ifndef __bool_true_false_are_defined\n              \"error: __bool_true_false_are_defined is not defined\"\n             #endif\n\n             struct s { _Bool s: 1; _Bool t; } s;\n\n             char a[true == 1 ? 1 : -1];\n             char b[false == 0 ? 1 : -1];\n             char c[__bool_true_false_are_defined == 1 ? 1 : -1];\n             char d[(bool) 0.5 == true ? 1 : -1];\n             /* See body of main program for 'e'.  */\n             char f[(_Bool) 0.0 == false ? 1 : -1];\n             char g[true];\n             char h[sizeof (_Bool)];\n             char i[sizeof s.t];\n             enum { j = false, k = true, l = false * true, m = true * 256 };\n             /* The following fails for\n                HP aC++/ANSI C B3910B A.05.55 [Dec 04 2003]. */\n             _Bool n[m];\n             char o[sizeof n == m * sizeof n[0] ? 1 : -1];\n             char p[-1 - (_Bool) 0 < 0 && -1 - (bool) 0 < 0 ? 1 : -1];\n             /* Catch a bug in an HP-UX C compiler.  See\n                http://gcc.gnu.org/ml/gcc-patches/2003-12/msg02303.html\n                http://lists.gnu.org/archive/html/bug-coreutils/2005-11/msg00161.html\n              */\n             _Bool q = true;\n             _Bool *pq = &q;\n\nint\nmain ()\n{\n\n             bool e = &s;\n             *pq |= q;\n             *pq |= ! q;\n             /* Refer to every declared value, to avoid compiler optimizations.  */\n             return (!a + !b + !c + !d + !e + !f + !g + !h + !i + !!j + !k + !!l\n                     + !m + !n + !o + !p + !q + !pq);\n\n  ;\n  return 0;\n}\n_ACEOF\nif ac_fn_c_try_compile \"$LINENO\"; then :\n  ac_cv_header_stdbool_h=yes\nelse\n  ac_cv_header_stdbool_h=no\nfi\nrm -f core conftest.err conftest.$ac_objext conftest.$ac_ext\nfi\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: $ac_cv_header_stdbool_h\" >&5\n$as_echo \"$ac_cv_header_stdbool_h\" >&6; }\n   ac_fn_c_check_type \"$LINENO\" \"_Bool\" \"ac_cv_type__Bool\" \"$ac_includes_default\"\nif test \"x$ac_cv_type__Bool\" = xyes; then :\n\ncat >>confdefs.h <<_ACEOF\n#define HAVE__BOOL 1\n_ACEOF\n\n\nfi\n\n\nif test $ac_cv_header_stdbool_h = yes; then\n\n$as_echo \"#define HAVE_STDBOOL_H 1\" >>confdefs.h\n\nfi\n\n\n\nac_config_commands=\"$ac_config_commands include/jemalloc/internal/public_symbols.txt\"\n\nac_config_commands=\"$ac_config_commands include/jemalloc/internal/private_symbols.awk\"\n\nac_config_commands=\"$ac_config_commands include/jemalloc/internal/private_symbols_jet.awk\"\n\nac_config_commands=\"$ac_config_commands include/jemalloc/internal/public_namespace.h\"\n\nac_config_commands=\"$ac_config_commands include/jemalloc/internal/public_unnamespace.h\"\n\nac_config_commands=\"$ac_config_commands include/jemalloc/jemalloc_protos_jet.h\"\n\nac_config_commands=\"$ac_config_commands include/jemalloc/jemalloc_rename.h\"\n\nac_config_commands=\"$ac_config_commands include/jemalloc/jemalloc_mangle.h\"\n\nac_config_commands=\"$ac_config_commands include/jemalloc/jemalloc_mangle_jet.h\"\n\nac_config_commands=\"$ac_config_commands include/jemalloc/jemalloc.h\"\n\n\n\n\nac_config_headers=\"$ac_config_headers $cfghdrs_tup\"\n\n\n\nac_config_files=\"$ac_config_files $cfgoutputs_tup config.stamp bin/jemalloc-config bin/jemalloc.sh bin/jeprof\"\n\n\n\ncat >confcache <<\\_ACEOF\n# This file is a shell script that caches the results of configure\n# tests run on this system so they can be shared between configure\n# scripts and configure runs, see configure's option --config-cache.\n# It is not useful on other systems.  If it contains results you don't\n# want to keep, you may remove or edit it.\n#\n# config.status only pays attention to the cache file if you give it\n# the --recheck option to rerun configure.\n#\n# `ac_cv_env_foo' variables (set or unset) will be overridden when\n# loading this file, other *unset* `ac_cv_foo' will be assigned the\n# following values.\n\n_ACEOF\n\n# The following way of writing the cache mishandles newlines in values,\n# but we know of no workaround that is simple, portable, and efficient.\n# So, we kill variables containing newlines.\n# Ultrix sh set writes to stderr and can't be redirected directly,\n# and sets the high bit in the cache file unless we assign to the vars.\n(\n  for ac_var in `(set) 2>&1 | sed -n 's/^\\([a-zA-Z_][a-zA-Z0-9_]*\\)=.*/\\1/p'`; do\n    eval ac_val=\\$$ac_var\n    case $ac_val in #(\n    *${as_nl}*)\n      case $ac_var in #(\n      *_cv_*) { $as_echo \"$as_me:${as_lineno-$LINENO}: WARNING: cache variable $ac_var contains a newline\" >&5\n$as_echo \"$as_me: WARNING: cache variable $ac_var contains a newline\" >&2;} ;;\n      esac\n      case $ac_var in #(\n      _ | IFS | as_nl) ;; #(\n      BASH_ARGV | BASH_SOURCE) eval $ac_var= ;; #(\n      *) { eval $ac_var=; unset $ac_var;} ;;\n      esac ;;\n    esac\n  done\n\n  (set) 2>&1 |\n    case $as_nl`(ac_space=' '; set) 2>&1` in #(\n    *${as_nl}ac_space=\\ *)\n      # `set' does not quote correctly, so add quotes: double-quote\n      # substitution turns \\\\\\\\ into \\\\, and sed turns \\\\ into \\.\n      sed -n \\\n\t\"s/'/'\\\\\\\\''/g;\n\t  s/^\\\\([_$as_cr_alnum]*_cv_[_$as_cr_alnum]*\\\\)=\\\\(.*\\\\)/\\\\1='\\\\2'/p\"\n      ;; #(\n    *)\n      # `set' quotes correctly as required by POSIX, so do not add quotes.\n      sed -n \"/^[_$as_cr_alnum]*_cv_[_$as_cr_alnum]*=/p\"\n      ;;\n    esac |\n    sort\n) |\n  sed '\n     /^ac_cv_env_/b end\n     t clear\n     :clear\n     s/^\\([^=]*\\)=\\(.*[{}].*\\)$/test \"${\\1+set}\" = set || &/\n     t end\n     s/^\\([^=]*\\)=\\(.*\\)$/\\1=${\\1=\\2}/\n     :end' >>confcache\nif diff \"$cache_file\" confcache >/dev/null 2>&1; then :; else\n  if test -w \"$cache_file\"; then\n    if test \"x$cache_file\" != \"x/dev/null\"; then\n      { $as_echo \"$as_me:${as_lineno-$LINENO}: updating cache $cache_file\" >&5\n$as_echo \"$as_me: updating cache $cache_file\" >&6;}\n      if test ! -f \"$cache_file\" || test -h \"$cache_file\"; then\n\tcat confcache >\"$cache_file\"\n      else\n        case $cache_file in #(\n        */* | ?:*)\n\t  mv -f confcache \"$cache_file\"$$ &&\n\t  mv -f \"$cache_file\"$$ \"$cache_file\" ;; #(\n        *)\n\t  mv -f confcache \"$cache_file\" ;;\n\tesac\n      fi\n    fi\n  else\n    { $as_echo \"$as_me:${as_lineno-$LINENO}: not updating unwritable cache $cache_file\" >&5\n$as_echo \"$as_me: not updating unwritable cache $cache_file\" >&6;}\n  fi\nfi\nrm -f confcache\n\ntest \"x$prefix\" = xNONE && prefix=$ac_default_prefix\n# Let make expand exec_prefix.\ntest \"x$exec_prefix\" = xNONE && exec_prefix='${prefix}'\n\nDEFS=-DHAVE_CONFIG_H\n\nac_libobjs=\nac_ltlibobjs=\nU=\nfor ac_i in : $LIBOBJS; do test \"x$ac_i\" = x: && continue\n  # 1. Remove the extension, and $U if already installed.\n  ac_script='s/\\$U\\././;s/\\.o$//;s/\\.obj$//'\n  ac_i=`$as_echo \"$ac_i\" | sed \"$ac_script\"`\n  # 2. Prepend LIBOBJDIR.  When used with automake>=1.10 LIBOBJDIR\n  #    will be set to the directory where LIBOBJS objects are built.\n  as_fn_append ac_libobjs \" \\${LIBOBJDIR}$ac_i\\$U.$ac_objext\"\n  as_fn_append ac_ltlibobjs \" \\${LIBOBJDIR}$ac_i\"'$U.lo'\ndone\nLIBOBJS=$ac_libobjs\n\nLTLIBOBJS=$ac_ltlibobjs\n\n\n\n\n: \"${CONFIG_STATUS=./config.status}\"\nac_write_fail=0\nac_clean_files_save=$ac_clean_files\nac_clean_files=\"$ac_clean_files $CONFIG_STATUS\"\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: creating $CONFIG_STATUS\" >&5\n$as_echo \"$as_me: creating $CONFIG_STATUS\" >&6;}\nas_write_fail=0\ncat >$CONFIG_STATUS <<_ASEOF || as_write_fail=1\n#! $SHELL\n# Generated by $as_me.\n# Run this file to recreate the current configuration.\n# Compiler output produced by configure, useful for debugging\n# configure, is in config.log if it exists.\n\ndebug=false\nac_cs_recheck=false\nac_cs_silent=false\n\nSHELL=\\${CONFIG_SHELL-$SHELL}\nexport SHELL\n_ASEOF\ncat >>$CONFIG_STATUS <<\\_ASEOF || as_write_fail=1\n## -------------------- ##\n## M4sh Initialization. ##\n## -------------------- ##\n\n# Be more Bourne compatible\nDUALCASE=1; export DUALCASE # for MKS sh\nif test -n \"${ZSH_VERSION+set}\" && (emulate sh) >/dev/null 2>&1; then :\n  emulate sh\n  NULLCMD=:\n  # Pre-4.2 versions of Zsh do word splitting on ${1+\"$@\"}, which\n  # is contrary to our usage.  Disable this feature.\n  alias -g '${1+\"$@\"}'='\"$@\"'\n  setopt NO_GLOB_SUBST\nelse\n  case `(set -o) 2>/dev/null` in #(\n  *posix*) :\n    set -o posix ;; #(\n  *) :\n     ;;\nesac\nfi\n\n\nas_nl='\n'\nexport as_nl\n# Printing a long string crashes Solaris 7 /usr/bin/printf.\nas_echo='\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'\nas_echo=$as_echo$as_echo$as_echo$as_echo$as_echo\nas_echo=$as_echo$as_echo$as_echo$as_echo$as_echo$as_echo\n# Prefer a ksh shell builtin over an external printf program on Solaris,\n# but without wasting forks for bash or zsh.\nif test -z \"$BASH_VERSION$ZSH_VERSION\" \\\n    && (test \"X`print -r -- $as_echo`\" = \"X$as_echo\") 2>/dev/null; then\n  as_echo='print -r --'\n  as_echo_n='print -rn --'\nelif (test \"X`printf %s $as_echo`\" = \"X$as_echo\") 2>/dev/null; then\n  as_echo='printf %s\\n'\n  as_echo_n='printf %s'\nelse\n  if test \"X`(/usr/ucb/echo -n -n $as_echo) 2>/dev/null`\" = \"X-n $as_echo\"; then\n    as_echo_body='eval /usr/ucb/echo -n \"$1$as_nl\"'\n    as_echo_n='/usr/ucb/echo -n'\n  else\n    as_echo_body='eval expr \"X$1\" : \"X\\\\(.*\\\\)\"'\n    as_echo_n_body='eval\n      arg=$1;\n      case $arg in #(\n      *\"$as_nl\"*)\n\texpr \"X$arg\" : \"X\\\\(.*\\\\)$as_nl\";\n\targ=`expr \"X$arg\" : \".*$as_nl\\\\(.*\\\\)\"`;;\n      esac;\n      expr \"X$arg\" : \"X\\\\(.*\\\\)\" | tr -d \"$as_nl\"\n    '\n    export as_echo_n_body\n    as_echo_n='sh -c $as_echo_n_body as_echo'\n  fi\n  export as_echo_body\n  as_echo='sh -c $as_echo_body as_echo'\nfi\n\n# The user is always right.\nif test \"${PATH_SEPARATOR+set}\" != set; then\n  PATH_SEPARATOR=:\n  (PATH='/bin;/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 && {\n    (PATH='/bin:/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 ||\n      PATH_SEPARATOR=';'\n  }\nfi\n\n\n# IFS\n# We need space, tab and new line, in precisely that order.  Quoting is\n# there to prevent editors from complaining about space-tab.\n# (If _AS_PATH_WALK were called with IFS unset, it would disable word\n# splitting by setting IFS to empty value.)\nIFS=\" \"\"\t$as_nl\"\n\n# Find who we are.  Look in the path if we contain no directory separator.\nas_myself=\ncase $0 in #((\n  *[\\\\/]* ) as_myself=$0 ;;\n  *) as_save_IFS=$IFS; IFS=$PATH_SEPARATOR\nfor as_dir in $PATH\ndo\n  IFS=$as_save_IFS\n  test -z \"$as_dir\" && as_dir=.\n    test -r \"$as_dir/$0\" && as_myself=$as_dir/$0 && break\n  done\nIFS=$as_save_IFS\n\n     ;;\nesac\n# We did not find ourselves, most probably we were run as `sh COMMAND'\n# in which case we are not to be found in the path.\nif test \"x$as_myself\" = x; then\n  as_myself=$0\nfi\nif test ! -f \"$as_myself\"; then\n  $as_echo \"$as_myself: error: cannot find myself; rerun with an absolute file name\" >&2\n  exit 1\nfi\n\n# Unset variables that we do not need and which cause bugs (e.g. in\n# pre-3.0 UWIN ksh).  But do not cause bugs in bash 2.01; the \"|| exit 1\"\n# suppresses any \"Segmentation fault\" message there.  '((' could\n# trigger a bug in pdksh 5.2.14.\nfor as_var in BASH_ENV ENV MAIL MAILPATH\ndo eval test x\\${$as_var+set} = xset \\\n  && ( (unset $as_var) || exit 1) >/dev/null 2>&1 && unset $as_var || :\ndone\nPS1='$ '\nPS2='> '\nPS4='+ '\n\n# NLS nuisances.\nLC_ALL=C\nexport LC_ALL\nLANGUAGE=C\nexport LANGUAGE\n\n# CDPATH.\n(unset CDPATH) >/dev/null 2>&1 && unset CDPATH\n\n\n# as_fn_error STATUS ERROR [LINENO LOG_FD]\n# ----------------------------------------\n# Output \"`basename $0`: error: ERROR\" to stderr. If LINENO and LOG_FD are\n# provided, also output the error to LOG_FD, referencing LINENO. Then exit the\n# script with STATUS, using 1 if that was 0.\nas_fn_error ()\n{\n  as_status=$1; test $as_status -eq 0 && as_status=1\n  if test \"$4\"; then\n    as_lineno=${as_lineno-\"$3\"} as_lineno_stack=as_lineno_stack=$as_lineno_stack\n    $as_echo \"$as_me:${as_lineno-$LINENO}: error: $2\" >&$4\n  fi\n  $as_echo \"$as_me: error: $2\" >&2\n  as_fn_exit $as_status\n} # as_fn_error\n\n\n# as_fn_set_status STATUS\n# -----------------------\n# Set $? to STATUS, without forking.\nas_fn_set_status ()\n{\n  return $1\n} # as_fn_set_status\n\n# as_fn_exit STATUS\n# -----------------\n# Exit the shell with STATUS, even in a \"trap 0\" or \"set -e\" context.\nas_fn_exit ()\n{\n  set +e\n  as_fn_set_status $1\n  exit $1\n} # as_fn_exit\n\n# as_fn_unset VAR\n# ---------------\n# Portably unset VAR.\nas_fn_unset ()\n{\n  { eval $1=; unset $1;}\n}\nas_unset=as_fn_unset\n# as_fn_append VAR VALUE\n# ----------------------\n# Append the text in VALUE to the end of the definition contained in VAR. Take\n# advantage of any shell optimizations that allow amortized linear growth over\n# repeated appends, instead of the typical quadratic growth present in naive\n# implementations.\nif (eval \"as_var=1; as_var+=2; test x\\$as_var = x12\") 2>/dev/null; then :\n  eval 'as_fn_append ()\n  {\n    eval $1+=\\$2\n  }'\nelse\n  as_fn_append ()\n  {\n    eval $1=\\$$1\\$2\n  }\nfi # as_fn_append\n\n# as_fn_arith ARG...\n# ------------------\n# Perform arithmetic evaluation on the ARGs, and store the result in the\n# global $as_val. Take advantage of shells that can avoid forks. The arguments\n# must be portable across $(()) and expr.\nif (eval \"test \\$(( 1 + 1 )) = 2\") 2>/dev/null; then :\n  eval 'as_fn_arith ()\n  {\n    as_val=$(( $* ))\n  }'\nelse\n  as_fn_arith ()\n  {\n    as_val=`expr \"$@\" || test $? -eq 1`\n  }\nfi # as_fn_arith\n\n\nif expr a : '\\(a\\)' >/dev/null 2>&1 &&\n   test \"X`expr 00001 : '.*\\(...\\)'`\" = X001; then\n  as_expr=expr\nelse\n  as_expr=false\nfi\n\nif (basename -- /) >/dev/null 2>&1 && test \"X`basename -- / 2>&1`\" = \"X/\"; then\n  as_basename=basename\nelse\n  as_basename=false\nfi\n\nif (as_dir=`dirname -- /` && test \"X$as_dir\" = X/) >/dev/null 2>&1; then\n  as_dirname=dirname\nelse\n  as_dirname=false\nfi\n\nas_me=`$as_basename -- \"$0\" ||\n$as_expr X/\"$0\" : '.*/\\([^/][^/]*\\)/*$' \\| \\\n\t X\"$0\" : 'X\\(//\\)$' \\| \\\n\t X\"$0\" : 'X\\(/\\)' \\| . 2>/dev/null ||\n$as_echo X/\"$0\" |\n    sed '/^.*\\/\\([^/][^/]*\\)\\/*$/{\n\t    s//\\1/\n\t    q\n\t  }\n\t  /^X\\/\\(\\/\\/\\)$/{\n\t    s//\\1/\n\t    q\n\t  }\n\t  /^X\\/\\(\\/\\).*/{\n\t    s//\\1/\n\t    q\n\t  }\n\t  s/.*/./; q'`\n\n# Avoid depending upon Character Ranges.\nas_cr_letters='abcdefghijklmnopqrstuvwxyz'\nas_cr_LETTERS='ABCDEFGHIJKLMNOPQRSTUVWXYZ'\nas_cr_Letters=$as_cr_letters$as_cr_LETTERS\nas_cr_digits='0123456789'\nas_cr_alnum=$as_cr_Letters$as_cr_digits\n\nECHO_C= ECHO_N= ECHO_T=\ncase `echo -n x` in #(((((\n-n*)\n  case `echo 'xy\\c'` in\n  *c*) ECHO_T='\t';;\t# ECHO_T is single tab character.\n  xy)  ECHO_C='\\c';;\n  *)   echo `echo ksh88 bug on AIX 6.1` > /dev/null\n       ECHO_T='\t';;\n  esac;;\n*)\n  ECHO_N='-n';;\nesac\n\nrm -f conf$$ conf$$.exe conf$$.file\nif test -d conf$$.dir; then\n  rm -f conf$$.dir/conf$$.file\nelse\n  rm -f conf$$.dir\n  mkdir conf$$.dir 2>/dev/null\nfi\nif (echo >conf$$.file) 2>/dev/null; then\n  if ln -s conf$$.file conf$$ 2>/dev/null; then\n    as_ln_s='ln -s'\n    # ... but there are two gotchas:\n    # 1) On MSYS, both `ln -s file dir' and `ln file dir' fail.\n    # 2) DJGPP < 2.04 has no symlinks; `ln -s' creates a wrapper executable.\n    # In both cases, we have to default to `cp -pR'.\n    ln -s conf$$.file conf$$.dir 2>/dev/null && test ! -f conf$$.exe ||\n      as_ln_s='cp -pR'\n  elif ln conf$$.file conf$$ 2>/dev/null; then\n    as_ln_s=ln\n  else\n    as_ln_s='cp -pR'\n  fi\nelse\n  as_ln_s='cp -pR'\nfi\nrm -f conf$$ conf$$.exe conf$$.dir/conf$$.file conf$$.file\nrmdir conf$$.dir 2>/dev/null\n\n\n# as_fn_mkdir_p\n# -------------\n# Create \"$as_dir\" as a directory, including parents if necessary.\nas_fn_mkdir_p ()\n{\n\n  case $as_dir in #(\n  -*) as_dir=./$as_dir;;\n  esac\n  test -d \"$as_dir\" || eval $as_mkdir_p || {\n    as_dirs=\n    while :; do\n      case $as_dir in #(\n      *\\'*) as_qdir=`$as_echo \"$as_dir\" | sed \"s/'/'\\\\\\\\\\\\\\\\''/g\"`;; #'(\n      *) as_qdir=$as_dir;;\n      esac\n      as_dirs=\"'$as_qdir' $as_dirs\"\n      as_dir=`$as_dirname -- \"$as_dir\" ||\n$as_expr X\"$as_dir\" : 'X\\(.*[^/]\\)//*[^/][^/]*/*$' \\| \\\n\t X\"$as_dir\" : 'X\\(//\\)[^/]' \\| \\\n\t X\"$as_dir\" : 'X\\(//\\)$' \\| \\\n\t X\"$as_dir\" : 'X\\(/\\)' \\| . 2>/dev/null ||\n$as_echo X\"$as_dir\" |\n    sed '/^X\\(.*[^/]\\)\\/\\/*[^/][^/]*\\/*$/{\n\t    s//\\1/\n\t    q\n\t  }\n\t  /^X\\(\\/\\/\\)[^/].*/{\n\t    s//\\1/\n\t    q\n\t  }\n\t  /^X\\(\\/\\/\\)$/{\n\t    s//\\1/\n\t    q\n\t  }\n\t  /^X\\(\\/\\).*/{\n\t    s//\\1/\n\t    q\n\t  }\n\t  s/.*/./; q'`\n      test -d \"$as_dir\" && break\n    done\n    test -z \"$as_dirs\" || eval \"mkdir $as_dirs\"\n  } || test -d \"$as_dir\" || as_fn_error $? \"cannot create directory $as_dir\"\n\n\n} # as_fn_mkdir_p\nif mkdir -p . 2>/dev/null; then\n  as_mkdir_p='mkdir -p \"$as_dir\"'\nelse\n  test -d ./-p && rmdir ./-p\n  as_mkdir_p=false\nfi\n\n\n# as_fn_executable_p FILE\n# -----------------------\n# Test if FILE is an executable regular file.\nas_fn_executable_p ()\n{\n  test -f \"$1\" && test -x \"$1\"\n} # as_fn_executable_p\nas_test_x='test -x'\nas_executable_p=as_fn_executable_p\n\n# Sed expression to map a string onto a valid CPP name.\nas_tr_cpp=\"eval sed 'y%*$as_cr_letters%P$as_cr_LETTERS%;s%[^_$as_cr_alnum]%_%g'\"\n\n# Sed expression to map a string onto a valid variable name.\nas_tr_sh=\"eval sed 'y%*+%pp%;s%[^_$as_cr_alnum]%_%g'\"\n\n\nexec 6>&1\n## ----------------------------------- ##\n## Main body of $CONFIG_STATUS script. ##\n## ----------------------------------- ##\n_ASEOF\ntest $as_write_fail = 0 && chmod +x $CONFIG_STATUS || ac_write_fail=1\n\ncat >>$CONFIG_STATUS <<\\_ACEOF || ac_write_fail=1\n# Save the log message, to keep $0 and so on meaningful, and to\n# report actual input values of CONFIG_FILES etc. instead of their\n# values after options handling.\nac_log=\"\nThis file was extended by $as_me, which was\ngenerated by GNU Autoconf 2.69.  Invocation command line was\n\n  CONFIG_FILES    = $CONFIG_FILES\n  CONFIG_HEADERS  = $CONFIG_HEADERS\n  CONFIG_LINKS    = $CONFIG_LINKS\n  CONFIG_COMMANDS = $CONFIG_COMMANDS\n  $ $0 $@\n\non `(hostname || uname -n) 2>/dev/null | sed 1q`\n\"\n\n_ACEOF\n\ncase $ac_config_files in *\"\n\"*) set x $ac_config_files; shift; ac_config_files=$*;;\nesac\n\ncase $ac_config_headers in *\"\n\"*) set x $ac_config_headers; shift; ac_config_headers=$*;;\nesac\n\n\ncat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1\n# Files that config.status was made for.\nconfig_files=\"$ac_config_files\"\nconfig_headers=\"$ac_config_headers\"\nconfig_commands=\"$ac_config_commands\"\n\n_ACEOF\n\ncat >>$CONFIG_STATUS <<\\_ACEOF || ac_write_fail=1\nac_cs_usage=\"\\\n\\`$as_me' instantiates files and other configuration actions\nfrom templates according to the current configuration.  Unless the files\nand actions are specified as TAGs, all are instantiated by default.\n\nUsage: $0 [OPTION]... [TAG]...\n\n  -h, --help       print this help, then exit\n  -V, --version    print version number and configuration settings, then exit\n      --config     print configuration, then exit\n  -q, --quiet, --silent\n                   do not print progress messages\n  -d, --debug      don't remove temporary files\n      --recheck    update $as_me by reconfiguring in the same conditions\n      --file=FILE[:TEMPLATE]\n                   instantiate the configuration file FILE\n      --header=FILE[:TEMPLATE]\n                   instantiate the configuration header FILE\n\nConfiguration files:\n$config_files\n\nConfiguration headers:\n$config_headers\n\nConfiguration commands:\n$config_commands\n\nReport bugs to the package provider.\"\n\n_ACEOF\ncat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1\nac_cs_config=\"`$as_echo \"$ac_configure_args\" | sed 's/^ //; s/[\\\\\"\"\\`\\$]/\\\\\\\\&/g'`\"\nac_cs_version=\"\\\\\nconfig.status\nconfigured by $0, generated by GNU Autoconf 2.69,\n  with options \\\\\"\\$ac_cs_config\\\\\"\n\nCopyright (C) 2012 Free Software Foundation, Inc.\nThis config.status script is free software; the Free Software Foundation\ngives unlimited permission to copy, distribute and modify it.\"\n\nac_pwd='$ac_pwd'\nsrcdir='$srcdir'\nINSTALL='$INSTALL'\nAWK='$AWK'\ntest -n \"\\$AWK\" || AWK=awk\n_ACEOF\n\ncat >>$CONFIG_STATUS <<\\_ACEOF || ac_write_fail=1\n# The default lists apply if the user does not specify any file.\nac_need_defaults=:\nwhile test $# != 0\ndo\n  case $1 in\n  --*=?*)\n    ac_option=`expr \"X$1\" : 'X\\([^=]*\\)='`\n    ac_optarg=`expr \"X$1\" : 'X[^=]*=\\(.*\\)'`\n    ac_shift=:\n    ;;\n  --*=)\n    ac_option=`expr \"X$1\" : 'X\\([^=]*\\)='`\n    ac_optarg=\n    ac_shift=:\n    ;;\n  *)\n    ac_option=$1\n    ac_optarg=$2\n    ac_shift=shift\n    ;;\n  esac\n\n  case $ac_option in\n  # Handling of the options.\n  -recheck | --recheck | --rechec | --reche | --rech | --rec | --re | --r)\n    ac_cs_recheck=: ;;\n  --version | --versio | --versi | --vers | --ver | --ve | --v | -V )\n    $as_echo \"$ac_cs_version\"; exit ;;\n  --config | --confi | --conf | --con | --co | --c )\n    $as_echo \"$ac_cs_config\"; exit ;;\n  --debug | --debu | --deb | --de | --d | -d )\n    debug=: ;;\n  --file | --fil | --fi | --f )\n    $ac_shift\n    case $ac_optarg in\n    *\\'*) ac_optarg=`$as_echo \"$ac_optarg\" | sed \"s/'/'\\\\\\\\\\\\\\\\''/g\"` ;;\n    '') as_fn_error $? \"missing file argument\" ;;\n    esac\n    as_fn_append CONFIG_FILES \" '$ac_optarg'\"\n    ac_need_defaults=false;;\n  --header | --heade | --head | --hea )\n    $ac_shift\n    case $ac_optarg in\n    *\\'*) ac_optarg=`$as_echo \"$ac_optarg\" | sed \"s/'/'\\\\\\\\\\\\\\\\''/g\"` ;;\n    esac\n    as_fn_append CONFIG_HEADERS \" '$ac_optarg'\"\n    ac_need_defaults=false;;\n  --he | --h)\n    # Conflict between --help and --header\n    as_fn_error $? \"ambiguous option: \\`$1'\nTry \\`$0 --help' for more information.\";;\n  --help | --hel | -h )\n    $as_echo \"$ac_cs_usage\"; exit ;;\n  -q | -quiet | --quiet | --quie | --qui | --qu | --q \\\n  | -silent | --silent | --silen | --sile | --sil | --si | --s)\n    ac_cs_silent=: ;;\n\n  # This is an error.\n  -*) as_fn_error $? \"unrecognized option: \\`$1'\nTry \\`$0 --help' for more information.\" ;;\n\n  *) as_fn_append ac_config_targets \" $1\"\n     ac_need_defaults=false ;;\n\n  esac\n  shift\ndone\n\nac_configure_extra_args=\n\nif $ac_cs_silent; then\n  exec 6>/dev/null\n  ac_configure_extra_args=\"$ac_configure_extra_args --silent\"\nfi\n\n_ACEOF\ncat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1\nif \\$ac_cs_recheck; then\n  set X $SHELL '$0' $ac_configure_args \\$ac_configure_extra_args --no-create --no-recursion\n  shift\n  \\$as_echo \"running CONFIG_SHELL=$SHELL \\$*\" >&6\n  CONFIG_SHELL='$SHELL'\n  export CONFIG_SHELL\n  exec \"\\$@\"\nfi\n\n_ACEOF\ncat >>$CONFIG_STATUS <<\\_ACEOF || ac_write_fail=1\nexec 5>>config.log\n{\n  echo\n  sed 'h;s/./-/g;s/^.../## /;s/...$/ ##/;p;x;p;x' <<_ASBOX\n## Running $as_me. ##\n_ASBOX\n  $as_echo \"$ac_log\"\n} >&5\n\n_ACEOF\ncat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1\n#\n# INIT-COMMANDS\n#\n\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n  mangling_map=\"${mangling_map}\"\n  public_syms=\"${public_syms}\"\n  JEMALLOC_PREFIX=\"${JEMALLOC_PREFIX}\"\n\n\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n  public_syms=\"${public_syms}\"\n  wrap_syms=\"${wrap_syms}\"\n  SYM_PREFIX=\"${SYM_PREFIX}\"\n  JEMALLOC_PREFIX=\"${JEMALLOC_PREFIX}\"\n\n\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n  public_syms=\"${public_syms}\"\n  wrap_syms=\"${wrap_syms}\"\n  SYM_PREFIX=\"${SYM_PREFIX}\"\n\n\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n\n\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n\n\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n\n\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n\n\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n\n\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n\n\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n  install_suffix=\"${install_suffix}\"\n\n\n_ACEOF\n\ncat >>$CONFIG_STATUS <<\\_ACEOF || ac_write_fail=1\n\n# Handling of arguments.\nfor ac_config_target in $ac_config_targets\ndo\n  case $ac_config_target in\n    \"include/jemalloc/internal/public_symbols.txt\") CONFIG_COMMANDS=\"$CONFIG_COMMANDS include/jemalloc/internal/public_symbols.txt\" ;;\n    \"include/jemalloc/internal/private_symbols.awk\") CONFIG_COMMANDS=\"$CONFIG_COMMANDS include/jemalloc/internal/private_symbols.awk\" ;;\n    \"include/jemalloc/internal/private_symbols_jet.awk\") CONFIG_COMMANDS=\"$CONFIG_COMMANDS include/jemalloc/internal/private_symbols_jet.awk\" ;;\n    \"include/jemalloc/internal/public_namespace.h\") CONFIG_COMMANDS=\"$CONFIG_COMMANDS include/jemalloc/internal/public_namespace.h\" ;;\n    \"include/jemalloc/internal/public_unnamespace.h\") CONFIG_COMMANDS=\"$CONFIG_COMMANDS include/jemalloc/internal/public_unnamespace.h\" ;;\n    \"include/jemalloc/jemalloc_protos_jet.h\") CONFIG_COMMANDS=\"$CONFIG_COMMANDS include/jemalloc/jemalloc_protos_jet.h\" ;;\n    \"include/jemalloc/jemalloc_rename.h\") CONFIG_COMMANDS=\"$CONFIG_COMMANDS include/jemalloc/jemalloc_rename.h\" ;;\n    \"include/jemalloc/jemalloc_mangle.h\") CONFIG_COMMANDS=\"$CONFIG_COMMANDS include/jemalloc/jemalloc_mangle.h\" ;;\n    \"include/jemalloc/jemalloc_mangle_jet.h\") CONFIG_COMMANDS=\"$CONFIG_COMMANDS include/jemalloc/jemalloc_mangle_jet.h\" ;;\n    \"include/jemalloc/jemalloc.h\") CONFIG_COMMANDS=\"$CONFIG_COMMANDS include/jemalloc/jemalloc.h\" ;;\n    \"$cfghdrs_tup\") CONFIG_HEADERS=\"$CONFIG_HEADERS $cfghdrs_tup\" ;;\n    \"$cfgoutputs_tup\") CONFIG_FILES=\"$CONFIG_FILES $cfgoutputs_tup\" ;;\n    \"config.stamp\") CONFIG_FILES=\"$CONFIG_FILES config.stamp\" ;;\n    \"bin/jemalloc-config\") CONFIG_FILES=\"$CONFIG_FILES bin/jemalloc-config\" ;;\n    \"bin/jemalloc.sh\") CONFIG_FILES=\"$CONFIG_FILES bin/jemalloc.sh\" ;;\n    \"bin/jeprof\") CONFIG_FILES=\"$CONFIG_FILES bin/jeprof\" ;;\n\n  *) as_fn_error $? \"invalid argument: \\`$ac_config_target'\" \"$LINENO\" 5;;\n  esac\ndone\n\n\n# If the user did not use the arguments to specify the items to instantiate,\n# then the envvar interface is used.  Set only those that are not.\n# We use the long form for the default assignment because of an extremely\n# bizarre bug on SunOS 4.1.3.\nif $ac_need_defaults; then\n  test \"${CONFIG_FILES+set}\" = set || CONFIG_FILES=$config_files\n  test \"${CONFIG_HEADERS+set}\" = set || CONFIG_HEADERS=$config_headers\n  test \"${CONFIG_COMMANDS+set}\" = set || CONFIG_COMMANDS=$config_commands\nfi\n\n# Have a temporary directory for convenience.  Make it in the build tree\n# simply because there is no reason against having it here, and in addition,\n# creating and moving files from /tmp can sometimes cause problems.\n# Hook for its removal unless debugging.\n# Note that there is a small window in which the directory will not be cleaned:\n# after its creation but before its name has been assigned to `$tmp'.\n$debug ||\n{\n  tmp= ac_tmp=\n  trap 'exit_status=$?\n  : \"${ac_tmp:=$tmp}\"\n  { test ! -d \"$ac_tmp\" || rm -fr \"$ac_tmp\"; } && exit $exit_status\n' 0\n  trap 'as_fn_exit 1' 1 2 13 15\n}\n# Create a (secure) tmp directory for tmp files.\n\n{\n  tmp=`(umask 077 && mktemp -d \"./confXXXXXX\") 2>/dev/null` &&\n  test -d \"$tmp\"\n}  ||\n{\n  tmp=./conf$$-$RANDOM\n  (umask 077 && mkdir \"$tmp\")\n} || as_fn_error $? \"cannot create a temporary directory in .\" \"$LINENO\" 5\nac_tmp=$tmp\n\n# Set up the scripts for CONFIG_FILES section.\n# No need to generate them if there are no CONFIG_FILES.\n# This happens for instance with `./config.status config.h'.\nif test -n \"$CONFIG_FILES\"; then\n\n\nac_cr=`echo X | tr X '\\015'`\n# On cygwin, bash can eat \\r inside `` if the user requested igncr.\n# But we know of no other shell where ac_cr would be empty at this\n# point, so we can use a bashism as a fallback.\nif test \"x$ac_cr\" = x; then\n  eval ac_cr=\\$\\'\\\\r\\'\nfi\nac_cs_awk_cr=`$AWK 'BEGIN { print \"a\\rb\" }' </dev/null 2>/dev/null`\nif test \"$ac_cs_awk_cr\" = \"a${ac_cr}b\"; then\n  ac_cs_awk_cr='\\\\r'\nelse\n  ac_cs_awk_cr=$ac_cr\nfi\n\necho 'BEGIN {' >\"$ac_tmp/subs1.awk\" &&\n_ACEOF\n\n\n{\n  echo \"cat >conf$$subs.awk <<_ACEOF\" &&\n  echo \"$ac_subst_vars\" | sed 's/.*/&!$&$ac_delim/' &&\n  echo \"_ACEOF\"\n} >conf$$subs.sh ||\n  as_fn_error $? \"could not make $CONFIG_STATUS\" \"$LINENO\" 5\nac_delim_num=`echo \"$ac_subst_vars\" | grep -c '^'`\nac_delim='%!_!# '\nfor ac_last_try in false false false false false :; do\n  . ./conf$$subs.sh ||\n    as_fn_error $? \"could not make $CONFIG_STATUS\" \"$LINENO\" 5\n\n  ac_delim_n=`sed -n \"s/.*$ac_delim\\$/X/p\" conf$$subs.awk | grep -c X`\n  if test $ac_delim_n = $ac_delim_num; then\n    break\n  elif $ac_last_try; then\n    as_fn_error $? \"could not make $CONFIG_STATUS\" \"$LINENO\" 5\n  else\n    ac_delim=\"$ac_delim!$ac_delim _$ac_delim!! \"\n  fi\ndone\nrm -f conf$$subs.sh\n\ncat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1\ncat >>\"\\$ac_tmp/subs1.awk\" <<\\\\_ACAWK &&\n_ACEOF\nsed -n '\nh\ns/^/S[\"/; s/!.*/\"]=/\np\ng\ns/^[^!]*!//\n:repl\nt repl\ns/'\"$ac_delim\"'$//\nt delim\n:nl\nh\ns/\\(.\\{148\\}\\)..*/\\1/\nt more1\ns/[\"\\\\]/\\\\&/g; s/^/\"/; s/$/\\\\n\"\\\\/\np\nn\nb repl\n:more1\ns/[\"\\\\]/\\\\&/g; s/^/\"/; s/$/\"\\\\/\np\ng\ns/.\\{148\\}//\nt nl\n:delim\nh\ns/\\(.\\{148\\}\\)..*/\\1/\nt more2\ns/[\"\\\\]/\\\\&/g; s/^/\"/; s/$/\"/\np\nb\n:more2\ns/[\"\\\\]/\\\\&/g; s/^/\"/; s/$/\"\\\\/\np\ng\ns/.\\{148\\}//\nt delim\n' <conf$$subs.awk | sed '\n/^[^\"\"]/{\n  N\n  s/\\n//\n}\n' >>$CONFIG_STATUS || ac_write_fail=1\nrm -f conf$$subs.awk\ncat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1\n_ACAWK\ncat >>\"\\$ac_tmp/subs1.awk\" <<_ACAWK &&\n  for (key in S) S_is_set[key] = 1\n  FS = \"\u0007\"\n\n}\n{\n  line = $ 0\n  nfields = split(line, field, \"@\")\n  substed = 0\n  len = length(field[1])\n  for (i = 2; i < nfields; i++) {\n    key = field[i]\n    keylen = length(key)\n    if (S_is_set[key]) {\n      value = S[key]\n      line = substr(line, 1, len) \"\" value \"\" substr(line, len + keylen + 3)\n      len += length(value) + length(field[++i])\n      substed = 1\n    } else\n      len += 1 + keylen\n  }\n\n  print line\n}\n\n_ACAWK\n_ACEOF\ncat >>$CONFIG_STATUS <<\\_ACEOF || ac_write_fail=1\nif sed \"s/$ac_cr//\" < /dev/null > /dev/null 2>&1; then\n  sed \"s/$ac_cr\\$//; s/$ac_cr/$ac_cs_awk_cr/g\"\nelse\n  cat\nfi < \"$ac_tmp/subs1.awk\" > \"$ac_tmp/subs.awk\" \\\n  || as_fn_error $? \"could not setup config files machinery\" \"$LINENO\" 5\n_ACEOF\n\n# VPATH may cause trouble with some makes, so we remove sole $(srcdir),\n# ${srcdir} and @srcdir@ entries from VPATH if srcdir is \".\", strip leading and\n# trailing colons and then remove the whole line if VPATH becomes empty\n# (actually we leave an empty line to preserve line numbers).\nif test \"x$srcdir\" = x.; then\n  ac_vpsub='/^[\t ]*VPATH[\t ]*=[\t ]*/{\nh\ns///\ns/^/:/\ns/[\t ]*$/:/\ns/:\\$(srcdir):/:/g\ns/:\\${srcdir}:/:/g\ns/:@srcdir@:/:/g\ns/^:*//\ns/:*$//\nx\ns/\\(=[\t ]*\\).*/\\1/\nG\ns/\\n//\ns/^[^=]*=[\t ]*$//\n}'\nfi\n\ncat >>$CONFIG_STATUS <<\\_ACEOF || ac_write_fail=1\nfi # test -n \"$CONFIG_FILES\"\n\n# Set up the scripts for CONFIG_HEADERS section.\n# No need to generate them if there are no CONFIG_HEADERS.\n# This happens for instance with `./config.status Makefile'.\nif test -n \"$CONFIG_HEADERS\"; then\ncat >\"$ac_tmp/defines.awk\" <<\\_ACAWK ||\nBEGIN {\n_ACEOF\n\n# Transform confdefs.h into an awk script `defines.awk', embedded as\n# here-document in config.status, that substitutes the proper values into\n# config.h.in to produce config.h.\n\n# Create a delimiter string that does not exist in confdefs.h, to ease\n# handling of long lines.\nac_delim='%!_!# '\nfor ac_last_try in false false :; do\n  ac_tt=`sed -n \"/$ac_delim/p\" confdefs.h`\n  if test -z \"$ac_tt\"; then\n    break\n  elif $ac_last_try; then\n    as_fn_error $? \"could not make $CONFIG_HEADERS\" \"$LINENO\" 5\n  else\n    ac_delim=\"$ac_delim!$ac_delim _$ac_delim!! \"\n  fi\ndone\n\n# For the awk script, D is an array of macro values keyed by name,\n# likewise P contains macro parameters if any.  Preserve backslash\n# newline sequences.\n\nac_word_re=[_$as_cr_Letters][_$as_cr_alnum]*\nsed -n '\ns/.\\{148\\}/&'\"$ac_delim\"'/g\nt rset\n:rset\ns/^[\t ]*#[\t ]*define[\t ][\t ]*/ /\nt def\nd\n:def\ns/\\\\$//\nt bsnl\ns/[\"\\\\]/\\\\&/g\ns/^ \\('\"$ac_word_re\"'\\)\\(([^()]*)\\)[\t ]*\\(.*\\)/P[\"\\1\"]=\"\\2\"\\\nD[\"\\1\"]=\" \\3\"/p\ns/^ \\('\"$ac_word_re\"'\\)[\t ]*\\(.*\\)/D[\"\\1\"]=\" \\2\"/p\nd\n:bsnl\ns/[\"\\\\]/\\\\&/g\ns/^ \\('\"$ac_word_re\"'\\)\\(([^()]*)\\)[\t ]*\\(.*\\)/P[\"\\1\"]=\"\\2\"\\\nD[\"\\1\"]=\" \\3\\\\\\\\\\\\n\"\\\\/p\nt cont\ns/^ \\('\"$ac_word_re\"'\\)[\t ]*\\(.*\\)/D[\"\\1\"]=\" \\2\\\\\\\\\\\\n\"\\\\/p\nt cont\nd\n:cont\nn\ns/.\\{148\\}/&'\"$ac_delim\"'/g\nt clear\n:clear\ns/\\\\$//\nt bsnlc\ns/[\"\\\\]/\\\\&/g; s/^/\"/; s/$/\"/p\nd\n:bsnlc\ns/[\"\\\\]/\\\\&/g; s/^/\"/; s/$/\\\\\\\\\\\\n\"\\\\/p\nb cont\n' <confdefs.h | sed '\ns/'\"$ac_delim\"'/\"\\\\\\\n\"/g' >>$CONFIG_STATUS || ac_write_fail=1\n\ncat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1\n  for (key in D) D_is_set[key] = 1\n  FS = \"\u0007\"\n}\n/^[\\t ]*#[\\t ]*(define|undef)[\\t ]+$ac_word_re([\\t (]|\\$)/ {\n  line = \\$ 0\n  split(line, arg, \" \")\n  if (arg[1] == \"#\") {\n    defundef = arg[2]\n    mac1 = arg[3]\n  } else {\n    defundef = substr(arg[1], 2)\n    mac1 = arg[2]\n  }\n  split(mac1, mac2, \"(\") #)\n  macro = mac2[1]\n  prefix = substr(line, 1, index(line, defundef) - 1)\n  if (D_is_set[macro]) {\n    # Preserve the white space surrounding the \"#\".\n    print prefix \"define\", macro P[macro] D[macro]\n    next\n  } else {\n    # Replace #undef with comments.  This is necessary, for example,\n    # in the case of _POSIX_SOURCE, which is predefined and required\n    # on some systems where configure will not decide to define it.\n    if (defundef == \"undef\") {\n      print \"/*\", prefix defundef, macro, \"*/\"\n      next\n    }\n  }\n}\n{ print }\n_ACAWK\n_ACEOF\ncat >>$CONFIG_STATUS <<\\_ACEOF || ac_write_fail=1\n  as_fn_error $? \"could not setup config headers machinery\" \"$LINENO\" 5\nfi # test -n \"$CONFIG_HEADERS\"\n\n\neval set X \"  :F $CONFIG_FILES  :H $CONFIG_HEADERS    :C $CONFIG_COMMANDS\"\nshift\nfor ac_tag\ndo\n  case $ac_tag in\n  :[FHLC]) ac_mode=$ac_tag; continue;;\n  esac\n  case $ac_mode$ac_tag in\n  :[FHL]*:*);;\n  :L* | :C*:*) as_fn_error $? \"invalid tag \\`$ac_tag'\" \"$LINENO\" 5;;\n  :[FH]-) ac_tag=-:-;;\n  :[FH]*) ac_tag=$ac_tag:$ac_tag.in;;\n  esac\n  ac_save_IFS=$IFS\n  IFS=:\n  set x $ac_tag\n  IFS=$ac_save_IFS\n  shift\n  ac_file=$1\n  shift\n\n  case $ac_mode in\n  :L) ac_source=$1;;\n  :[FH])\n    ac_file_inputs=\n    for ac_f\n    do\n      case $ac_f in\n      -) ac_f=\"$ac_tmp/stdin\";;\n      *) # Look for the file first in the build tree, then in the source tree\n\t # (if the path is not absolute).  The absolute path cannot be DOS-style,\n\t # because $ac_f cannot contain `:'.\n\t test -f \"$ac_f\" ||\n\t   case $ac_f in\n\t   [\\\\/$]*) false;;\n\t   *) test -f \"$srcdir/$ac_f\" && ac_f=\"$srcdir/$ac_f\";;\n\t   esac ||\n\t   as_fn_error 1 \"cannot find input file: \\`$ac_f'\" \"$LINENO\" 5;;\n      esac\n      case $ac_f in *\\'*) ac_f=`$as_echo \"$ac_f\" | sed \"s/'/'\\\\\\\\\\\\\\\\''/g\"`;; esac\n      as_fn_append ac_file_inputs \" '$ac_f'\"\n    done\n\n    # Let's still pretend it is `configure' which instantiates (i.e., don't\n    # use $as_me), people would be surprised to read:\n    #    /* config.h.  Generated by config.status.  */\n    configure_input='Generated from '`\n\t  $as_echo \"$*\" | sed 's|^[^:]*/||;s|:[^:]*/|, |g'\n\t`' by configure.'\n    if test x\"$ac_file\" != x-; then\n      configure_input=\"$ac_file.  $configure_input\"\n      { $as_echo \"$as_me:${as_lineno-$LINENO}: creating $ac_file\" >&5\n$as_echo \"$as_me: creating $ac_file\" >&6;}\n    fi\n    # Neutralize special characters interpreted by sed in replacement strings.\n    case $configure_input in #(\n    *\\&* | *\\|* | *\\\\* )\n       ac_sed_conf_input=`$as_echo \"$configure_input\" |\n       sed 's/[\\\\\\\\&|]/\\\\\\\\&/g'`;; #(\n    *) ac_sed_conf_input=$configure_input;;\n    esac\n\n    case $ac_tag in\n    *:-:* | *:-) cat >\"$ac_tmp/stdin\" \\\n      || as_fn_error $? \"could not create $ac_file\" \"$LINENO\" 5 ;;\n    esac\n    ;;\n  esac\n\n  ac_dir=`$as_dirname -- \"$ac_file\" ||\n$as_expr X\"$ac_file\" : 'X\\(.*[^/]\\)//*[^/][^/]*/*$' \\| \\\n\t X\"$ac_file\" : 'X\\(//\\)[^/]' \\| \\\n\t X\"$ac_file\" : 'X\\(//\\)$' \\| \\\n\t X\"$ac_file\" : 'X\\(/\\)' \\| . 2>/dev/null ||\n$as_echo X\"$ac_file\" |\n    sed '/^X\\(.*[^/]\\)\\/\\/*[^/][^/]*\\/*$/{\n\t    s//\\1/\n\t    q\n\t  }\n\t  /^X\\(\\/\\/\\)[^/].*/{\n\t    s//\\1/\n\t    q\n\t  }\n\t  /^X\\(\\/\\/\\)$/{\n\t    s//\\1/\n\t    q\n\t  }\n\t  /^X\\(\\/\\).*/{\n\t    s//\\1/\n\t    q\n\t  }\n\t  s/.*/./; q'`\n  as_dir=\"$ac_dir\"; as_fn_mkdir_p\n  ac_builddir=.\n\ncase \"$ac_dir\" in\n.) ac_dir_suffix= ac_top_builddir_sub=. ac_top_build_prefix= ;;\n*)\n  ac_dir_suffix=/`$as_echo \"$ac_dir\" | sed 's|^\\.[\\\\/]||'`\n  # A \"..\" for each directory in $ac_dir_suffix.\n  ac_top_builddir_sub=`$as_echo \"$ac_dir_suffix\" | sed 's|/[^\\\\/]*|/..|g;s|/||'`\n  case $ac_top_builddir_sub in\n  \"\") ac_top_builddir_sub=. ac_top_build_prefix= ;;\n  *)  ac_top_build_prefix=$ac_top_builddir_sub/ ;;\n  esac ;;\nesac\nac_abs_top_builddir=$ac_pwd\nac_abs_builddir=$ac_pwd$ac_dir_suffix\n# for backward compatibility:\nac_top_builddir=$ac_top_build_prefix\n\ncase $srcdir in\n  .)  # We are building in place.\n    ac_srcdir=.\n    ac_top_srcdir=$ac_top_builddir_sub\n    ac_abs_top_srcdir=$ac_pwd ;;\n  [\\\\/]* | ?:[\\\\/]* )  # Absolute name.\n    ac_srcdir=$srcdir$ac_dir_suffix;\n    ac_top_srcdir=$srcdir\n    ac_abs_top_srcdir=$srcdir ;;\n  *) # Relative name.\n    ac_srcdir=$ac_top_build_prefix$srcdir$ac_dir_suffix\n    ac_top_srcdir=$ac_top_build_prefix$srcdir\n    ac_abs_top_srcdir=$ac_pwd/$srcdir ;;\nesac\nac_abs_srcdir=$ac_abs_top_srcdir$ac_dir_suffix\n\n\n  case $ac_mode in\n  :F)\n  #\n  # CONFIG_FILE\n  #\n\n  case $INSTALL in\n  [\\\\/$]* | ?:[\\\\/]* ) ac_INSTALL=$INSTALL ;;\n  *) ac_INSTALL=$ac_top_build_prefix$INSTALL ;;\n  esac\n_ACEOF\n\ncat >>$CONFIG_STATUS <<\\_ACEOF || ac_write_fail=1\n# If the template does not know about datarootdir, expand it.\n# FIXME: This hack should be removed a few years after 2.60.\nac_datarootdir_hack=; ac_datarootdir_seen=\nac_sed_dataroot='\n/datarootdir/ {\n  p\n  q\n}\n/@datadir@/p\n/@docdir@/p\n/@infodir@/p\n/@localedir@/p\n/@mandir@/p'\ncase `eval \"sed -n \\\"\\$ac_sed_dataroot\\\" $ac_file_inputs\"` in\n*datarootdir*) ac_datarootdir_seen=yes;;\n*@datadir@*|*@docdir@*|*@infodir@*|*@localedir@*|*@mandir@*)\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: WARNING: $ac_file_inputs seems to ignore the --datarootdir setting\" >&5\n$as_echo \"$as_me: WARNING: $ac_file_inputs seems to ignore the --datarootdir setting\" >&2;}\n_ACEOF\ncat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1\n  ac_datarootdir_hack='\n  s&@datadir@&$datadir&g\n  s&@docdir@&$docdir&g\n  s&@infodir@&$infodir&g\n  s&@localedir@&$localedir&g\n  s&@mandir@&$mandir&g\n  s&\\\\\\${datarootdir}&$datarootdir&g' ;;\nesac\n_ACEOF\n\n# Neutralize VPATH when `$srcdir' = `.'.\n# Shell code in configure.ac might set extrasub.\n# FIXME: do we really want to maintain this feature?\ncat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1\nac_sed_extra=\"$ac_vpsub\n$extrasub\n_ACEOF\ncat >>$CONFIG_STATUS <<\\_ACEOF || ac_write_fail=1\n:t\n/@[a-zA-Z_][a-zA-Z_0-9]*@/!b\ns|@configure_input@|$ac_sed_conf_input|;t t\ns&@top_builddir@&$ac_top_builddir_sub&;t t\ns&@top_build_prefix@&$ac_top_build_prefix&;t t\ns&@srcdir@&$ac_srcdir&;t t\ns&@abs_srcdir@&$ac_abs_srcdir&;t t\ns&@top_srcdir@&$ac_top_srcdir&;t t\ns&@abs_top_srcdir@&$ac_abs_top_srcdir&;t t\ns&@builddir@&$ac_builddir&;t t\ns&@abs_builddir@&$ac_abs_builddir&;t t\ns&@abs_top_builddir@&$ac_abs_top_builddir&;t t\ns&@INSTALL@&$ac_INSTALL&;t t\n$ac_datarootdir_hack\n\"\neval sed \\\"\\$ac_sed_extra\\\" \"$ac_file_inputs\" | $AWK -f \"$ac_tmp/subs.awk\" \\\n  >$ac_tmp/out || as_fn_error $? \"could not create $ac_file\" \"$LINENO\" 5\n\ntest -z \"$ac_datarootdir_hack$ac_datarootdir_seen\" &&\n  { ac_out=`sed -n '/\\${datarootdir}/p' \"$ac_tmp/out\"`; test -n \"$ac_out\"; } &&\n  { ac_out=`sed -n '/^[\t ]*datarootdir[\t ]*:*=/p' \\\n      \"$ac_tmp/out\"`; test -z \"$ac_out\"; } &&\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: WARNING: $ac_file contains a reference to the variable \\`datarootdir'\nwhich seems to be undefined.  Please make sure it is defined\" >&5\n$as_echo \"$as_me: WARNING: $ac_file contains a reference to the variable \\`datarootdir'\nwhich seems to be undefined.  Please make sure it is defined\" >&2;}\n\n  rm -f \"$ac_tmp/stdin\"\n  case $ac_file in\n  -) cat \"$ac_tmp/out\" && rm -f \"$ac_tmp/out\";;\n  *) rm -f \"$ac_file\" && mv \"$ac_tmp/out\" \"$ac_file\";;\n  esac \\\n  || as_fn_error $? \"could not create $ac_file\" \"$LINENO\" 5\n ;;\n  :H)\n  #\n  # CONFIG_HEADER\n  #\n  if test x\"$ac_file\" != x-; then\n    {\n      $as_echo \"/* $configure_input  */\" \\\n      && eval '$AWK -f \"$ac_tmp/defines.awk\"' \"$ac_file_inputs\"\n    } >\"$ac_tmp/config.h\" \\\n      || as_fn_error $? \"could not create $ac_file\" \"$LINENO\" 5\n    if diff \"$ac_file\" \"$ac_tmp/config.h\" >/dev/null 2>&1; then\n      { $as_echo \"$as_me:${as_lineno-$LINENO}: $ac_file is unchanged\" >&5\n$as_echo \"$as_me: $ac_file is unchanged\" >&6;}\n    else\n      rm -f \"$ac_file\"\n      mv \"$ac_tmp/config.h\" \"$ac_file\" \\\n\t|| as_fn_error $? \"could not create $ac_file\" \"$LINENO\" 5\n    fi\n  else\n    $as_echo \"/* $configure_input  */\" \\\n      && eval '$AWK -f \"$ac_tmp/defines.awk\"' \"$ac_file_inputs\" \\\n      || as_fn_error $? \"could not create -\" \"$LINENO\" 5\n  fi\n ;;\n\n  :C)  { $as_echo \"$as_me:${as_lineno-$LINENO}: executing $ac_file commands\" >&5\n$as_echo \"$as_me: executing $ac_file commands\" >&6;}\n ;;\n  esac\n\n\n  case $ac_file$ac_mode in\n    \"include/jemalloc/internal/public_symbols.txt\":C)\n  f=\"${objroot}include/jemalloc/internal/public_symbols.txt\"\n  mkdir -p \"${objroot}include/jemalloc/internal\"\n  cp /dev/null \"${f}\"\n  for nm in `echo ${mangling_map} |tr ',' ' '` ; do\n    n=`echo ${nm} |tr ':' ' ' |awk '{print $1}'`\n    m=`echo ${nm} |tr ':' ' ' |awk '{print $2}'`\n    echo \"${n}:${m}\" >> \"${f}\"\n        public_syms=`for sym in ${public_syms}; do echo \"${sym}\"; done |grep -v \"^${n}\\$\" |tr '\\n' ' '`\n  done\n  for sym in ${public_syms} ; do\n    n=\"${sym}\"\n    m=\"${JEMALLOC_PREFIX}${sym}\"\n    echo \"${n}:${m}\" >> \"${f}\"\n  done\n ;;\n    \"include/jemalloc/internal/private_symbols.awk\":C)\n  f=\"${objroot}include/jemalloc/internal/private_symbols.awk\"\n  mkdir -p \"${objroot}include/jemalloc/internal\"\n  export_syms=`for sym in ${public_syms}; do echo \"${JEMALLOC_PREFIX}${sym}\"; done; for sym in ${wrap_syms}; do echo \"${sym}\"; done;`\n  \"${srcdir}/include/jemalloc/internal/private_symbols.sh\" \"${SYM_PREFIX}\" ${export_syms} > \"${objroot}include/jemalloc/internal/private_symbols.awk\"\n ;;\n    \"include/jemalloc/internal/private_symbols_jet.awk\":C)\n  f=\"${objroot}include/jemalloc/internal/private_symbols_jet.awk\"\n  mkdir -p \"${objroot}include/jemalloc/internal\"\n  export_syms=`for sym in ${public_syms}; do echo \"jet_${sym}\"; done; for sym in ${wrap_syms}; do echo \"${sym}\"; done;`\n  \"${srcdir}/include/jemalloc/internal/private_symbols.sh\" \"${SYM_PREFIX}\" ${export_syms} > \"${objroot}include/jemalloc/internal/private_symbols_jet.awk\"\n ;;\n    \"include/jemalloc/internal/public_namespace.h\":C)\n  mkdir -p \"${objroot}include/jemalloc/internal\"\n  \"${srcdir}/include/jemalloc/internal/public_namespace.sh\" \"${objroot}include/jemalloc/internal/public_symbols.txt\" > \"${objroot}include/jemalloc/internal/public_namespace.h\"\n ;;\n    \"include/jemalloc/internal/public_unnamespace.h\":C)\n  mkdir -p \"${objroot}include/jemalloc/internal\"\n  \"${srcdir}/include/jemalloc/internal/public_unnamespace.sh\" \"${objroot}include/jemalloc/internal/public_symbols.txt\" > \"${objroot}include/jemalloc/internal/public_unnamespace.h\"\n ;;\n    \"include/jemalloc/jemalloc_protos_jet.h\":C)\n  mkdir -p \"${objroot}include/jemalloc\"\n  cat \"${srcdir}/include/jemalloc/jemalloc_protos.h.in\" | sed -e 's/@je_@/jet_/g' > \"${objroot}include/jemalloc/jemalloc_protos_jet.h\"\n ;;\n    \"include/jemalloc/jemalloc_rename.h\":C)\n  mkdir -p \"${objroot}include/jemalloc\"\n  \"${srcdir}/include/jemalloc/jemalloc_rename.sh\" \"${objroot}include/jemalloc/internal/public_symbols.txt\" > \"${objroot}include/jemalloc/jemalloc_rename.h\"\n ;;\n    \"include/jemalloc/jemalloc_mangle.h\":C)\n  mkdir -p \"${objroot}include/jemalloc\"\n  \"${srcdir}/include/jemalloc/jemalloc_mangle.sh\" \"${objroot}include/jemalloc/internal/public_symbols.txt\" je_ > \"${objroot}include/jemalloc/jemalloc_mangle.h\"\n ;;\n    \"include/jemalloc/jemalloc_mangle_jet.h\":C)\n  mkdir -p \"${objroot}include/jemalloc\"\n  \"${srcdir}/include/jemalloc/jemalloc_mangle.sh\" \"${objroot}include/jemalloc/internal/public_symbols.txt\" jet_ > \"${objroot}include/jemalloc/jemalloc_mangle_jet.h\"\n ;;\n    \"include/jemalloc/jemalloc.h\":C)\n  mkdir -p \"${objroot}include/jemalloc\"\n  \"${srcdir}/include/jemalloc/jemalloc.sh\" \"${objroot}\" > \"${objroot}include/jemalloc/jemalloc${install_suffix}.h\"\n ;;\n\n  esac\ndone # for ac_tag\n\n\nas_fn_exit 0\n_ACEOF\nac_clean_files=$ac_clean_files_save\n\ntest $ac_write_fail = 0 ||\n  as_fn_error $? \"write failure creating $CONFIG_STATUS\" \"$LINENO\" 5\n\n\n# configure is writing to config.log, and then calls config.status.\n# config.status does its own redirection, appending to config.log.\n# Unfortunately, on DOS this fails, as config.log is still kept open\n# by configure, so config.status won't be able to write to it; its\n# output is simply discarded.  So we exec the FD to /dev/null,\n# effectively closing config.log, so it can be properly (re)opened and\n# appended to by config.status.  When coming back to configure, we\n# need to make the FD available again.\nif test \"$no_create\" != yes; then\n  ac_cs_success=:\n  ac_config_status_args=\n  test \"$silent\" = yes &&\n    ac_config_status_args=\"$ac_config_status_args --quiet\"\n  exec 5>/dev/null\n  $SHELL $CONFIG_STATUS $ac_config_status_args || ac_cs_success=false\n  exec 5>>config.log\n  # Use ||, not &&, to avoid exiting from the if with $? = 1, which\n  # would make configure fail if this is the last instruction.\n  $ac_cs_success || as_fn_exit 1\nfi\nif test -n \"$ac_unrecognized_opts\" && test \"$enable_option_checking\" != no; then\n  { $as_echo \"$as_me:${as_lineno-$LINENO}: WARNING: unrecognized options: $ac_unrecognized_opts\" >&5\n$as_echo \"$as_me: WARNING: unrecognized options: $ac_unrecognized_opts\" >&2;}\nfi\n\n\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: ===============================================================================\" >&5\n$as_echo \"===============================================================================\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: jemalloc version   : ${jemalloc_version}\" >&5\n$as_echo \"jemalloc version   : ${jemalloc_version}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: library revision   : ${rev}\" >&5\n$as_echo \"library revision   : ${rev}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: \" >&5\n$as_echo \"\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: CONFIG             : ${CONFIG}\" >&5\n$as_echo \"CONFIG             : ${CONFIG}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: CC                 : ${CC}\" >&5\n$as_echo \"CC                 : ${CC}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: CONFIGURE_CFLAGS   : ${CONFIGURE_CFLAGS}\" >&5\n$as_echo \"CONFIGURE_CFLAGS   : ${CONFIGURE_CFLAGS}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: SPECIFIED_CFLAGS   : ${SPECIFIED_CFLAGS}\" >&5\n$as_echo \"SPECIFIED_CFLAGS   : ${SPECIFIED_CFLAGS}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: EXTRA_CFLAGS       : ${EXTRA_CFLAGS}\" >&5\n$as_echo \"EXTRA_CFLAGS       : ${EXTRA_CFLAGS}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: CPPFLAGS           : ${CPPFLAGS}\" >&5\n$as_echo \"CPPFLAGS           : ${CPPFLAGS}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: CXX                : ${CXX}\" >&5\n$as_echo \"CXX                : ${CXX}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: CONFIGURE_CXXFLAGS : ${CONFIGURE_CXXFLAGS}\" >&5\n$as_echo \"CONFIGURE_CXXFLAGS : ${CONFIGURE_CXXFLAGS}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: SPECIFIED_CXXFLAGS : ${SPECIFIED_CXXFLAGS}\" >&5\n$as_echo \"SPECIFIED_CXXFLAGS : ${SPECIFIED_CXXFLAGS}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: EXTRA_CXXFLAGS     : ${EXTRA_CXXFLAGS}\" >&5\n$as_echo \"EXTRA_CXXFLAGS     : ${EXTRA_CXXFLAGS}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: LDFLAGS            : ${LDFLAGS}\" >&5\n$as_echo \"LDFLAGS            : ${LDFLAGS}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: EXTRA_LDFLAGS      : ${EXTRA_LDFLAGS}\" >&5\n$as_echo \"EXTRA_LDFLAGS      : ${EXTRA_LDFLAGS}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: DSO_LDFLAGS        : ${DSO_LDFLAGS}\" >&5\n$as_echo \"DSO_LDFLAGS        : ${DSO_LDFLAGS}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: LIBS               : ${LIBS}\" >&5\n$as_echo \"LIBS               : ${LIBS}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: RPATH_EXTRA        : ${RPATH_EXTRA}\" >&5\n$as_echo \"RPATH_EXTRA        : ${RPATH_EXTRA}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: \" >&5\n$as_echo \"\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: XSLTPROC           : ${XSLTPROC}\" >&5\n$as_echo \"XSLTPROC           : ${XSLTPROC}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: XSLROOT            : ${XSLROOT}\" >&5\n$as_echo \"XSLROOT            : ${XSLROOT}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: \" >&5\n$as_echo \"\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: PREFIX             : ${PREFIX}\" >&5\n$as_echo \"PREFIX             : ${PREFIX}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: BINDIR             : ${BINDIR}\" >&5\n$as_echo \"BINDIR             : ${BINDIR}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: DATADIR            : ${DATADIR}\" >&5\n$as_echo \"DATADIR            : ${DATADIR}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: INCLUDEDIR         : ${INCLUDEDIR}\" >&5\n$as_echo \"INCLUDEDIR         : ${INCLUDEDIR}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: LIBDIR             : ${LIBDIR}\" >&5\n$as_echo \"LIBDIR             : ${LIBDIR}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: MANDIR             : ${MANDIR}\" >&5\n$as_echo \"MANDIR             : ${MANDIR}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: \" >&5\n$as_echo \"\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: srcroot            : ${srcroot}\" >&5\n$as_echo \"srcroot            : ${srcroot}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: abs_srcroot        : ${abs_srcroot}\" >&5\n$as_echo \"abs_srcroot        : ${abs_srcroot}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: objroot            : ${objroot}\" >&5\n$as_echo \"objroot            : ${objroot}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: abs_objroot        : ${abs_objroot}\" >&5\n$as_echo \"abs_objroot        : ${abs_objroot}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: \" >&5\n$as_echo \"\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: JEMALLOC_PREFIX    : ${JEMALLOC_PREFIX}\" >&5\n$as_echo \"JEMALLOC_PREFIX    : ${JEMALLOC_PREFIX}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: JEMALLOC_PRIVATE_NAMESPACE\" >&5\n$as_echo \"JEMALLOC_PRIVATE_NAMESPACE\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result:                    : ${JEMALLOC_PRIVATE_NAMESPACE}\" >&5\n$as_echo \"                   : ${JEMALLOC_PRIVATE_NAMESPACE}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: install_suffix     : ${install_suffix}\" >&5\n$as_echo \"install_suffix     : ${install_suffix}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: malloc_conf        : ${config_malloc_conf}\" >&5\n$as_echo \"malloc_conf        : ${config_malloc_conf}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: documentation      : ${enable_doc}\" >&5\n$as_echo \"documentation      : ${enable_doc}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: shared libs        : ${enable_shared}\" >&5\n$as_echo \"shared libs        : ${enable_shared}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: static libs        : ${enable_static}\" >&5\n$as_echo \"static libs        : ${enable_static}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: autogen            : ${enable_autogen}\" >&5\n$as_echo \"autogen            : ${enable_autogen}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: debug              : ${enable_debug}\" >&5\n$as_echo \"debug              : ${enable_debug}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: stats              : ${enable_stats}\" >&5\n$as_echo \"stats              : ${enable_stats}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: experimetal_smallocx : ${enable_experimental_smallocx}\" >&5\n$as_echo \"experimetal_smallocx : ${enable_experimental_smallocx}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: prof               : ${enable_prof}\" >&5\n$as_echo \"prof               : ${enable_prof}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: prof-libunwind     : ${enable_prof_libunwind}\" >&5\n$as_echo \"prof-libunwind     : ${enable_prof_libunwind}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: prof-libgcc        : ${enable_prof_libgcc}\" >&5\n$as_echo \"prof-libgcc        : ${enable_prof_libgcc}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: prof-gcc           : ${enable_prof_gcc}\" >&5\n$as_echo \"prof-gcc           : ${enable_prof_gcc}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: fill               : ${enable_fill}\" >&5\n$as_echo \"fill               : ${enable_fill}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: utrace             : ${enable_utrace}\" >&5\n$as_echo \"utrace             : ${enable_utrace}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: xmalloc            : ${enable_xmalloc}\" >&5\n$as_echo \"xmalloc            : ${enable_xmalloc}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: log                : ${enable_log}\" >&5\n$as_echo \"log                : ${enable_log}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: lazy_lock          : ${enable_lazy_lock}\" >&5\n$as_echo \"lazy_lock          : ${enable_lazy_lock}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: cache-oblivious    : ${enable_cache_oblivious}\" >&5\n$as_echo \"cache-oblivious    : ${enable_cache_oblivious}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: cxx                : ${enable_cxx}\" >&5\n$as_echo \"cxx                : ${enable_cxx}\" >&6; }\n{ $as_echo \"$as_me:${as_lineno-$LINENO}: result: ===============================================================================\" >&5\n$as_echo \"===============================================================================\" >&6; }\n"
  },
  {
    "path": "deps/jemalloc/configure.ac",
    "content": "dnl Process this file with autoconf to produce a configure script.\nAC_PREREQ(2.68)\nAC_INIT([Makefile.in])\n\nAC_CONFIG_AUX_DIR([build-aux])\n\ndnl ============================================================================\ndnl Custom macro definitions.\n\ndnl JE_CONCAT_VVV(r, a, b)\ndnl\ndnl Set $r to the concatenation of $a and $b, with a space separating them iff\ndnl both $a and $b are non-empty.\nAC_DEFUN([JE_CONCAT_VVV],\nif test \"x[$]{$2}\" = \"x\" -o \"x[$]{$3}\" = \"x\" ; then\n  $1=\"[$]{$2}[$]{$3}\"\nelse\n  $1=\"[$]{$2} [$]{$3}\"\nfi\n)\n\ndnl JE_APPEND_VS(a, b)\ndnl\ndnl Set $a to the concatenation of $a and b, with a space separating them iff\ndnl both $a and b are non-empty.\nAC_DEFUN([JE_APPEND_VS],\n  T_APPEND_V=$2\n  JE_CONCAT_VVV($1, $1, T_APPEND_V)\n)\n\nCONFIGURE_CFLAGS=\nSPECIFIED_CFLAGS=\"${CFLAGS}\"\ndnl JE_CFLAGS_ADD(cflag)\ndnl\ndnl CFLAGS is the concatenation of CONFIGURE_CFLAGS and SPECIFIED_CFLAGS\ndnl (ignoring EXTRA_CFLAGS, which does not impact configure tests.  This macro\ndnl appends to CONFIGURE_CFLAGS and regenerates CFLAGS.\nAC_DEFUN([JE_CFLAGS_ADD],\n[\nAC_MSG_CHECKING([whether compiler supports $1])\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nJE_APPEND_VS(CONFIGURE_CFLAGS, $1)\nJE_CONCAT_VVV(CFLAGS, CONFIGURE_CFLAGS, SPECIFIED_CFLAGS)\nAC_COMPILE_IFELSE([AC_LANG_PROGRAM(\n[[\n]], [[\n    return 0;\n]])],\n              [je_cv_cflags_added=$1]\n              AC_MSG_RESULT([yes]),\n              [je_cv_cflags_added=]\n              AC_MSG_RESULT([no])\n              [CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"]\n)\nJE_CONCAT_VVV(CFLAGS, CONFIGURE_CFLAGS, SPECIFIED_CFLAGS)\n])\n\ndnl JE_CFLAGS_SAVE()\ndnl JE_CFLAGS_RESTORE()\ndnl\ndnl Save/restore CFLAGS.  Nesting is not supported.\nAC_DEFUN([JE_CFLAGS_SAVE],\nSAVED_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\n)\nAC_DEFUN([JE_CFLAGS_RESTORE],\nCONFIGURE_CFLAGS=\"${SAVED_CONFIGURE_CFLAGS}\"\nJE_CONCAT_VVV(CFLAGS, CONFIGURE_CFLAGS, SPECIFIED_CFLAGS)\n)\n\nCONFIGURE_CXXFLAGS=\nSPECIFIED_CXXFLAGS=\"${CXXFLAGS}\"\ndnl JE_CXXFLAGS_ADD(cxxflag)\nAC_DEFUN([JE_CXXFLAGS_ADD],\n[\nAC_MSG_CHECKING([whether compiler supports $1])\nT_CONFIGURE_CXXFLAGS=\"${CONFIGURE_CXXFLAGS}\"\nJE_APPEND_VS(CONFIGURE_CXXFLAGS, $1)\nJE_CONCAT_VVV(CXXFLAGS, CONFIGURE_CXXFLAGS, SPECIFIED_CXXFLAGS)\nAC_LANG_PUSH([C++])\nAC_COMPILE_IFELSE([AC_LANG_PROGRAM(\n[[\n]], [[\n    return 0;\n]])],\n              [je_cv_cxxflags_added=$1]\n              AC_MSG_RESULT([yes]),\n              [je_cv_cxxflags_added=]\n              AC_MSG_RESULT([no])\n              [CONFIGURE_CXXFLAGS=\"${T_CONFIGURE_CXXFLAGS}\"]\n)\nAC_LANG_POP([C++])\nJE_CONCAT_VVV(CXXFLAGS, CONFIGURE_CXXFLAGS, SPECIFIED_CXXFLAGS)\n])\n\ndnl JE_COMPILABLE(label, hcode, mcode, rvar)\ndnl\ndnl Use AC_LINK_IFELSE() rather than AC_COMPILE_IFELSE() so that linker errors\ndnl cause failure.\nAC_DEFUN([JE_COMPILABLE],\n[\nAC_CACHE_CHECK([whether $1 is compilable],\n               [$4],\n               [AC_LINK_IFELSE([AC_LANG_PROGRAM([$2],\n                                                [$3])],\n                               [$4=yes],\n                               [$4=no])])\n])\n\ndnl ============================================================================\n\nCONFIG=`echo ${ac_configure_args} | sed -e 's#'\"'\"'\\([^ ]*\\)'\"'\"'#\\1#g'`\nAC_SUBST([CONFIG])\n\ndnl Library revision.\nrev=2\nAC_SUBST([rev])\n\nsrcroot=$srcdir\nif test \"x${srcroot}\" = \"x.\" ; then\n  srcroot=\"\"\nelse\n  srcroot=\"${srcroot}/\"\nfi\nAC_SUBST([srcroot])\nabs_srcroot=\"`cd \\\"${srcdir}\\\"; pwd`/\"\nAC_SUBST([abs_srcroot])\n\nobjroot=\"\"\nAC_SUBST([objroot])\nabs_objroot=\"`pwd`/\"\nAC_SUBST([abs_objroot])\n\ndnl Munge install path variables.\nif test \"x$prefix\" = \"xNONE\" ; then\n  prefix=\"/usr/local\"\nfi\nif test \"x$exec_prefix\" = \"xNONE\" ; then\n  exec_prefix=$prefix\nfi\nPREFIX=$prefix\nAC_SUBST([PREFIX])\nBINDIR=`eval echo $bindir`\nBINDIR=`eval echo $BINDIR`\nAC_SUBST([BINDIR])\nINCLUDEDIR=`eval echo $includedir`\nINCLUDEDIR=`eval echo $INCLUDEDIR`\nAC_SUBST([INCLUDEDIR])\nLIBDIR=`eval echo $libdir`\nLIBDIR=`eval echo $LIBDIR`\nAC_SUBST([LIBDIR])\nDATADIR=`eval echo $datadir`\nDATADIR=`eval echo $DATADIR`\nAC_SUBST([DATADIR])\nMANDIR=`eval echo $mandir`\nMANDIR=`eval echo $MANDIR`\nAC_SUBST([MANDIR])\n\ndnl Support for building documentation.\nAC_PATH_PROG([XSLTPROC], [xsltproc], [false], [$PATH])\nif test -d \"/usr/share/xml/docbook/stylesheet/docbook-xsl\" ; then\n  DEFAULT_XSLROOT=\"/usr/share/xml/docbook/stylesheet/docbook-xsl\"\nelif test -d \"/usr/share/sgml/docbook/xsl-stylesheets\" ; then\n  DEFAULT_XSLROOT=\"/usr/share/sgml/docbook/xsl-stylesheets\"\nelse\n  dnl Documentation building will fail if this default gets used.\n  DEFAULT_XSLROOT=\"\"\nfi\nAC_ARG_WITH([xslroot],\n  [AS_HELP_STRING([--with-xslroot=<path>], [XSL stylesheet root path])], [\nif test \"x$with_xslroot\" = \"xno\" ; then\n  XSLROOT=\"${DEFAULT_XSLROOT}\"\nelse\n  XSLROOT=\"${with_xslroot}\"\nfi\n],\n  XSLROOT=\"${DEFAULT_XSLROOT}\"\n)\nif test \"x$XSLTPROC\" = \"xfalse\" ; then\n  XSLROOT=\"\"\nfi\nAC_SUBST([XSLROOT])\n\ndnl If CFLAGS isn't defined, set CFLAGS to something reasonable.  Otherwise,\ndnl just prevent autoconf from molesting CFLAGS.\nCFLAGS=$CFLAGS\nAC_PROG_CC\n\nif test \"x$GCC\" != \"xyes\" ; then\n  AC_CACHE_CHECK([whether compiler is MSVC],\n                 [je_cv_msvc],\n                 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],\n                                                     [\n#ifndef _MSC_VER\n  int fail[-1];\n#endif\n])],\n                               [je_cv_msvc=yes],\n                               [je_cv_msvc=no])])\nfi\n\ndnl check if a cray prgenv wrapper compiler is being used\nje_cv_cray_prgenv_wrapper=\"\"\nif test \"x${PE_ENV}\" != \"x\" ; then\n  case \"${CC}\" in\n    CC|cc)\n\tje_cv_cray_prgenv_wrapper=\"yes\"\n\t;;\n    *)\n       ;;\n  esac\nfi\n\nAC_CACHE_CHECK([whether compiler is cray],\n              [je_cv_cray],\n              [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],\n                                                  [\n#ifndef _CRAYC\n  int fail[-1];\n#endif\n])],\n                            [je_cv_cray=yes],\n                            [je_cv_cray=no])])\n\nif test \"x${je_cv_cray}\" = \"xyes\" ; then\n  AC_CACHE_CHECK([whether cray compiler version is 8.4],\n                [je_cv_cray_84],\n                [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],\n                                                      [\n#if !(_RELEASE_MAJOR == 8 && _RELEASE_MINOR == 4)\n  int fail[-1];\n#endif\n])],\n                              [je_cv_cray_84=yes],\n                              [je_cv_cray_84=no])])\nfi\n\nif test \"x$GCC\" = \"xyes\" ; then\n  JE_CFLAGS_ADD([-std=gnu11])\n  if test \"x$je_cv_cflags_added\" = \"x-std=gnu11\" ; then\n    AC_DEFINE_UNQUOTED([JEMALLOC_HAS_RESTRICT])\n  else\n    JE_CFLAGS_ADD([-std=gnu99])\n    if test \"x$je_cv_cflags_added\" = \"x-std=gnu99\" ; then\n      AC_DEFINE_UNQUOTED([JEMALLOC_HAS_RESTRICT])\n    fi\n  fi\n  JE_CFLAGS_ADD([-Wall])\n  JE_CFLAGS_ADD([-Wextra])\n  JE_CFLAGS_ADD([-Wshorten-64-to-32])\n  JE_CFLAGS_ADD([-Wsign-compare])\n  JE_CFLAGS_ADD([-Wundef])\n  JE_CFLAGS_ADD([-Wno-format-zero-length])\n  JE_CFLAGS_ADD([-pipe])\n  JE_CFLAGS_ADD([-g3])\nelif test \"x$je_cv_msvc\" = \"xyes\" ; then\n  CC=\"$CC -nologo\"\n  JE_CFLAGS_ADD([-Zi])\n  JE_CFLAGS_ADD([-MT])\n  JE_CFLAGS_ADD([-W3])\n  JE_CFLAGS_ADD([-FS])\n  JE_APPEND_VS(CPPFLAGS, -I${srcdir}/include/msvc_compat)\nfi\nif test \"x$je_cv_cray\" = \"xyes\" ; then\n  dnl cray compiler 8.4 has an inlining bug\n  if test \"x$je_cv_cray_84\" = \"xyes\" ; then\n    JE_CFLAGS_ADD([-hipa2])\n    JE_CFLAGS_ADD([-hnognu])\n  fi\n  dnl ignore unreachable code warning\n  JE_CFLAGS_ADD([-hnomessage=128])\n  dnl ignore redefinition of \"malloc\", \"free\", etc warning\n  JE_CFLAGS_ADD([-hnomessage=1357])\nfi\nAC_SUBST([CONFIGURE_CFLAGS])\nAC_SUBST([SPECIFIED_CFLAGS])\nAC_SUBST([EXTRA_CFLAGS])\nAC_PROG_CPP\n\nAC_ARG_ENABLE([cxx],\n  [AS_HELP_STRING([--disable-cxx], [Disable C++ integration])],\nif test \"x$enable_cxx\" = \"xno\" ; then\n  enable_cxx=\"0\"\nelse\n  enable_cxx=\"1\"\nfi\n,\nenable_cxx=\"1\"\n)\nif test \"x$enable_cxx\" = \"x1\" ; then\n  dnl Require at least c++14, which is the first version to support sized\n  dnl deallocation.  C++ support is not compiled otherwise.\n  m4_include([m4/ax_cxx_compile_stdcxx.m4])\n  AX_CXX_COMPILE_STDCXX([14], [noext], [optional])\n  if test \"x${HAVE_CXX14}\" = \"x1\" ; then\n    JE_CXXFLAGS_ADD([-Wall])\n    JE_CXXFLAGS_ADD([-Wextra])\n    JE_CXXFLAGS_ADD([-g3])\n\n    SAVED_LIBS=\"${LIBS}\"\n    JE_APPEND_VS(LIBS, -lstdc++)\n    JE_COMPILABLE([libstdc++ linkage], [\n#include <stdlib.h>\n], [[\n\tint *arr = (int *)malloc(sizeof(int) * 42);\n\tif (arr == NULL)\n\t\treturn 1;\n]], [je_cv_libstdcxx])\n    if test \"x${je_cv_libstdcxx}\" = \"xno\" ; then\n      LIBS=\"${SAVED_LIBS}\"\n    fi\n  else\n    enable_cxx=\"0\"\n  fi\nfi\nAC_SUBST([enable_cxx])\nAC_SUBST([CONFIGURE_CXXFLAGS])\nAC_SUBST([SPECIFIED_CXXFLAGS])\nAC_SUBST([EXTRA_CXXFLAGS])\n\nAC_C_BIGENDIAN([ac_cv_big_endian=1], [ac_cv_big_endian=0])\nif test \"x${ac_cv_big_endian}\" = \"x1\" ; then\n  AC_DEFINE_UNQUOTED([JEMALLOC_BIG_ENDIAN], [ ])\nfi\n\nif test \"x${je_cv_msvc}\" = \"xyes\" -a \"x${ac_cv_header_inttypes_h}\" = \"xno\"; then\n  JE_APPEND_VS(CPPFLAGS, -I${srcdir}/include/msvc_compat/C99)\nfi\n\nif test \"x${je_cv_msvc}\" = \"xyes\" ; then\n  LG_SIZEOF_PTR=LG_SIZEOF_PTR_WIN\n  AC_MSG_RESULT([Using a predefined value for sizeof(void *): 4 for 32-bit, 8 for 64-bit])\nelse\n  AC_CHECK_SIZEOF([void *])\n  if test \"x${ac_cv_sizeof_void_p}\" = \"x8\" ; then\n    LG_SIZEOF_PTR=3\n  elif test \"x${ac_cv_sizeof_void_p}\" = \"x4\" ; then\n    LG_SIZEOF_PTR=2\n  else\n    AC_MSG_ERROR([Unsupported pointer size: ${ac_cv_sizeof_void_p}])\n  fi\nfi\nAC_DEFINE_UNQUOTED([LG_SIZEOF_PTR], [$LG_SIZEOF_PTR])\n\nAC_CHECK_SIZEOF([int])\nif test \"x${ac_cv_sizeof_int}\" = \"x8\" ; then\n  LG_SIZEOF_INT=3\nelif test \"x${ac_cv_sizeof_int}\" = \"x4\" ; then\n  LG_SIZEOF_INT=2\nelse\n  AC_MSG_ERROR([Unsupported int size: ${ac_cv_sizeof_int}])\nfi\nAC_DEFINE_UNQUOTED([LG_SIZEOF_INT], [$LG_SIZEOF_INT])\n\nAC_CHECK_SIZEOF([long])\nif test \"x${ac_cv_sizeof_long}\" = \"x8\" ; then\n  LG_SIZEOF_LONG=3\nelif test \"x${ac_cv_sizeof_long}\" = \"x4\" ; then\n  LG_SIZEOF_LONG=2\nelse\n  AC_MSG_ERROR([Unsupported long size: ${ac_cv_sizeof_long}])\nfi\nAC_DEFINE_UNQUOTED([LG_SIZEOF_LONG], [$LG_SIZEOF_LONG])\n\nAC_CHECK_SIZEOF([long long])\nif test \"x${ac_cv_sizeof_long_long}\" = \"x8\" ; then\n  LG_SIZEOF_LONG_LONG=3\nelif test \"x${ac_cv_sizeof_long_long}\" = \"x4\" ; then\n  LG_SIZEOF_LONG_LONG=2\nelse\n  AC_MSG_ERROR([Unsupported long long size: ${ac_cv_sizeof_long_long}])\nfi\nAC_DEFINE_UNQUOTED([LG_SIZEOF_LONG_LONG], [$LG_SIZEOF_LONG_LONG])\n\nAC_CHECK_SIZEOF([intmax_t])\nif test \"x${ac_cv_sizeof_intmax_t}\" = \"x16\" ; then\n  LG_SIZEOF_INTMAX_T=4\nelif test \"x${ac_cv_sizeof_intmax_t}\" = \"x8\" ; then\n  LG_SIZEOF_INTMAX_T=3\nelif test \"x${ac_cv_sizeof_intmax_t}\" = \"x4\" ; then\n  LG_SIZEOF_INTMAX_T=2\nelse\n  AC_MSG_ERROR([Unsupported intmax_t size: ${ac_cv_sizeof_intmax_t}])\nfi\nAC_DEFINE_UNQUOTED([LG_SIZEOF_INTMAX_T], [$LG_SIZEOF_INTMAX_T])\n\nAC_CANONICAL_HOST\ndnl CPU-specific settings.\nCPU_SPINWAIT=\"\"\ncase \"${host_cpu}\" in\n  i686|x86_64)\n\tHAVE_CPU_SPINWAIT=1\n\tif test \"x${je_cv_msvc}\" = \"xyes\" ; then\n\t    AC_CACHE_VAL([je_cv_pause_msvc],\n\t      [JE_COMPILABLE([pause instruction MSVC], [],\n\t\t\t\t\t[[_mm_pause(); return 0;]],\n\t\t\t\t\t[je_cv_pause_msvc])])\n\t    if test \"x${je_cv_pause_msvc}\" = \"xyes\" ; then\n\t\tCPU_SPINWAIT='_mm_pause()'\n\t    fi\n\telse\n\t    AC_CACHE_VAL([je_cv_pause],\n\t      [JE_COMPILABLE([pause instruction], [],\n\t\t\t\t\t[[__asm__ volatile(\"pause\"); return 0;]],\n\t\t\t\t\t[je_cv_pause])])\n\t    if test \"x${je_cv_pause}\" = \"xyes\" ; then\n\t\tCPU_SPINWAIT='__asm__ volatile(\"pause\")'\n\t    fi\n\tfi\n\t;;\n  *)\n\tHAVE_CPU_SPINWAIT=0\n\t;;\nesac\nAC_DEFINE_UNQUOTED([HAVE_CPU_SPINWAIT], [$HAVE_CPU_SPINWAIT])\nAC_DEFINE_UNQUOTED([CPU_SPINWAIT], [$CPU_SPINWAIT])\n\nAC_ARG_WITH([lg_vaddr],\n  [AS_HELP_STRING([--with-lg-vaddr=<lg-vaddr>], [Number of significant virtual address bits])],\n  [LG_VADDR=\"$with_lg_vaddr\"], [LG_VADDR=\"detect\"])\n\ncase \"${host_cpu}\" in\n  aarch64)\n    if test \"x$LG_VADDR\" = \"xdetect\"; then\n      AC_MSG_CHECKING([number of significant virtual address bits])\n      if test \"x${LG_SIZEOF_PTR}\" = \"x2\" ; then\n        #aarch64 ILP32\n        LG_VADDR=32\n      else\n        #aarch64 LP64\n        LG_VADDR=48\n      fi\n      AC_MSG_RESULT([$LG_VADDR])\n    fi\n    ;;\n  x86_64)\n    if test \"x$LG_VADDR\" = \"xdetect\"; then\n      AC_CACHE_CHECK([number of significant virtual address bits],\n                     [je_cv_lg_vaddr],\n                     AC_RUN_IFELSE([AC_LANG_PROGRAM(\n[[\n#include <stdio.h>\n#ifdef _WIN32\n#include <limits.h>\n#include <intrin.h>\ntypedef unsigned __int32 uint32_t;\n#else\n#include <stdint.h>\n#endif\n]], [[\n\tuint32_t r[[4]];\n\tuint32_t eax_in = 0x80000008U;\n#ifdef _WIN32\n\t__cpuid((int *)r, (int)eax_in);\n#else\n\tasm volatile (\"cpuid\"\n\t    : \"=a\" (r[[0]]), \"=b\" (r[[1]]), \"=c\" (r[[2]]), \"=d\" (r[[3]])\n\t    : \"a\" (eax_in), \"c\" (0)\n\t);\n#endif\n\tuint32_t eax_out = r[[0]];\n\tuint32_t vaddr = ((eax_out & 0x0000ff00U) >> 8);\n\tFILE *f = fopen(\"conftest.out\", \"w\");\n\tif (f == NULL) {\n\t\treturn 1;\n\t}\n\tif (vaddr > (sizeof(void *) << 3)) {\n\t\tvaddr = sizeof(void *) << 3;\n\t}\n\tfprintf(f, \"%u\", vaddr);\n\tfclose(f);\n\treturn 0;\n]])],\n                   [je_cv_lg_vaddr=`cat conftest.out`],\n                   [je_cv_lg_vaddr=error],\n                   [je_cv_lg_vaddr=57]))\n      if test \"x${je_cv_lg_vaddr}\" != \"x\" ; then\n        LG_VADDR=\"${je_cv_lg_vaddr}\"\n      fi\n      if test \"x${LG_VADDR}\" != \"xerror\" ; then\n        AC_DEFINE_UNQUOTED([LG_VADDR], [$LG_VADDR])\n      else\n        AC_MSG_ERROR([cannot determine number of significant virtual address bits])\n      fi\n    fi\n    ;;\n  *)\n    if test \"x$LG_VADDR\" = \"xdetect\"; then\n      AC_MSG_CHECKING([number of significant virtual address bits])\n      if test \"x${LG_SIZEOF_PTR}\" = \"x3\" ; then\n        LG_VADDR=64\n      elif test \"x${LG_SIZEOF_PTR}\" = \"x2\" ; then\n        LG_VADDR=32\n      elif test \"x${LG_SIZEOF_PTR}\" = \"xLG_SIZEOF_PTR_WIN\" ; then\n        LG_VADDR=\"(1U << (LG_SIZEOF_PTR_WIN+3))\"\n      else\n        AC_MSG_ERROR([Unsupported lg(pointer size): ${LG_SIZEOF_PTR}])\n      fi\n      AC_MSG_RESULT([$LG_VADDR])\n    fi\n    ;;\nesac\nAC_DEFINE_UNQUOTED([LG_VADDR], [$LG_VADDR])\n\nLD_PRELOAD_VAR=\"LD_PRELOAD\"\nso=\"so\"\nimportlib=\"${so}\"\no=\"$ac_objext\"\na=\"a\"\nexe=\"$ac_exeext\"\nlibprefix=\"lib\"\nlink_whole_archive=\"0\"\nDSO_LDFLAGS='-shared -Wl,-soname,$(@F)'\nRPATH='-Wl,-rpath,$(1)'\nSOREV=\"${so}.${rev}\"\nPIC_CFLAGS='-fPIC -DPIC'\nCTARGET='-o $@'\nLDTARGET='-o $@'\nTEST_LD_MODE=\nEXTRA_LDFLAGS=\nARFLAGS='crs'\nAROUT=' $@'\nCC_MM=1\n\nif test \"x$je_cv_cray_prgenv_wrapper\" = \"xyes\" ; then\n  TEST_LD_MODE='-dynamic'\nfi\n\nif test \"x${je_cv_cray}\" = \"xyes\" ; then\n  CC_MM=\nfi\n\nAN_MAKEVAR([AR], [AC_PROG_AR])\nAN_PROGRAM([ar], [AC_PROG_AR])\nAC_DEFUN([AC_PROG_AR], [AC_CHECK_TOOL(AR, ar, :)])\nAC_PROG_AR\n\nAN_MAKEVAR([NM], [AC_PROG_NM])\nAN_PROGRAM([nm], [AC_PROG_NM])\nAC_DEFUN([AC_PROG_NM], [AC_CHECK_TOOL(NM, nm, :)])\nAC_PROG_NM\n\nAC_PROG_AWK\n\ndnl ============================================================================\ndnl jemalloc version.\ndnl\n\nAC_ARG_WITH([version],\n  [AS_HELP_STRING([--with-version=<major>.<minor>.<bugfix>-<nrev>-g<gid>],\n   [Version string])],\n  [\n    echo \"${with_version}\" | grep ['^[0-9]\\+\\.[0-9]\\+\\.[0-9]\\+-[0-9]\\+-g[0-9a-f]\\+$'] 2>&1 1>/dev/null\n    if test $? -eq 0 ; then\n      echo \"$with_version\" > \"${objroot}VERSION\"\n    else\n      echo \"${with_version}\" | grep ['^VERSION$'] 2>&1 1>/dev/null\n      if test $? -ne 0 ; then\n        AC_MSG_ERROR([${with_version} does not match <major>.<minor>.<bugfix>-<nrev>-g<gid> or VERSION])\n      fi\n    fi\n  ], [\n    dnl Set VERSION if source directory is inside a git repository.\n    if test \"x`test ! \\\"${srcroot}\\\" && cd \\\"${srcroot}\\\"; git rev-parse --is-inside-work-tree 2>/dev/null`\" = \"xtrue\" ; then\n      dnl Pattern globs aren't powerful enough to match both single- and\n      dnl double-digit version numbers, so iterate over patterns to support up\n      dnl to version 99.99.99 without any accidental matches.\n      for pattern in ['[0-9].[0-9].[0-9]' '[0-9].[0-9].[0-9][0-9]' \\\n                     '[0-9].[0-9][0-9].[0-9]' '[0-9].[0-9][0-9].[0-9][0-9]' \\\n                     '[0-9][0-9].[0-9].[0-9]' '[0-9][0-9].[0-9].[0-9][0-9]' \\\n                     '[0-9][0-9].[0-9][0-9].[0-9]' \\\n                     '[0-9][0-9].[0-9][0-9].[0-9][0-9]']; do\n        (test ! \"${srcroot}\" && cd \"${srcroot}\"; git describe --long --abbrev=40 --match=\"${pattern}\") > \"${objroot}VERSION.tmp\" 2>/dev/null\n        if test $? -eq 0 ; then\n          mv \"${objroot}VERSION.tmp\" \"${objroot}VERSION\"\n          break\n        fi\n      done\n    fi\n    rm -f \"${objroot}VERSION.tmp\"\n  ])\n\nif test ! -e \"${objroot}VERSION\" ; then\n  if test ! -e \"${srcroot}VERSION\" ; then\n    AC_MSG_RESULT(\n      [Missing VERSION file, and unable to generate it; creating bogus VERSION])\n    echo \"0.0.0-0-g0000000000000000000000000000000000000000\" > \"${objroot}VERSION\"\n  else\n    cp ${srcroot}VERSION ${objroot}VERSION\n  fi\nfi\njemalloc_version=`cat \"${objroot}VERSION\"`\njemalloc_version_major=`echo ${jemalloc_version} | tr \".g-\" \" \" | awk '{print [$]1}'`\njemalloc_version_minor=`echo ${jemalloc_version} | tr \".g-\" \" \" | awk '{print [$]2}'`\njemalloc_version_bugfix=`echo ${jemalloc_version} | tr \".g-\" \" \" | awk '{print [$]3}'`\njemalloc_version_nrev=`echo ${jemalloc_version} | tr \".g-\" \" \" | awk '{print [$]4}'`\njemalloc_version_gid=`echo ${jemalloc_version} | tr \".g-\" \" \" | awk '{print [$]5}'`\nAC_SUBST([jemalloc_version])\nAC_SUBST([jemalloc_version_major])\nAC_SUBST([jemalloc_version_minor])\nAC_SUBST([jemalloc_version_bugfix])\nAC_SUBST([jemalloc_version_nrev])\nAC_SUBST([jemalloc_version_gid])\n\ndnl Platform-specific settings.  abi and RPATH can probably be determined\ndnl programmatically, but doing so is error-prone, which makes it generally\ndnl not worth the trouble.\ndnl\ndnl Define cpp macros in CPPFLAGS, rather than doing AC_DEFINE(macro), since the\ndnl definitions need to be seen before any headers are included, which is a pain\ndnl to make happen otherwise.\ndefault_retain=\"0\"\nmaps_coalesce=\"1\"\nDUMP_SYMS=\"${NM} -a\"\nSYM_PREFIX=\"\"\ncase \"${host}\" in\n  *-*-darwin* | *-*-ios*)\n\tabi=\"macho\"\n\tRPATH=\"\"\n\tLD_PRELOAD_VAR=\"DYLD_INSERT_LIBRARIES\"\n\tso=\"dylib\"\n\timportlib=\"${so}\"\n\tforce_tls=\"0\"\n\tDSO_LDFLAGS='-shared -Wl,-install_name,$(LIBDIR)/$(@F)'\n\tSOREV=\"${rev}.${so}\"\n\tsbrk_deprecated=\"1\"\n\tSYM_PREFIX=\"_\"\n\t;;\n  *-*-freebsd*)\n\tabi=\"elf\"\n\tAC_DEFINE([JEMALLOC_SYSCTL_VM_OVERCOMMIT], [ ])\n\tforce_lazy_lock=\"1\"\n\t;;\n  *-*-dragonfly*)\n\tabi=\"elf\"\n\t;;\n  *-*-openbsd*)\n\tabi=\"elf\"\n\tforce_tls=\"0\"\n\t;;\n  *-*-bitrig*)\n\tabi=\"elf\"\n\t;;\n  *-*-linux-android)\n\tdnl syscall(2) and secure_getenv(3) are exposed by _GNU_SOURCE.\n\tJE_APPEND_VS(CPPFLAGS, -D_GNU_SOURCE)\n\tabi=\"elf\"\n\tAC_DEFINE([JEMALLOC_PURGE_MADVISE_DONTNEED_ZEROS], [ ])\n\tAC_DEFINE([JEMALLOC_HAS_ALLOCA_H])\n\tAC_DEFINE([JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY], [ ])\n\tAC_DEFINE([JEMALLOC_THREADED_INIT], [ ])\n\tAC_DEFINE([JEMALLOC_C11_ATOMICS])\n\tforce_tls=\"0\"\n\tif test \"${LG_SIZEOF_PTR}\" = \"3\"; then\n\t  default_retain=\"1\"\n\tfi\n\t;;\n  *-*-linux*)\n\tdnl syscall(2) and secure_getenv(3) are exposed by _GNU_SOURCE.\n\tJE_APPEND_VS(CPPFLAGS, -D_GNU_SOURCE)\n\tabi=\"elf\"\n\tAC_DEFINE([JEMALLOC_PURGE_MADVISE_DONTNEED_ZEROS], [ ])\n\tAC_DEFINE([JEMALLOC_HAS_ALLOCA_H])\n\tAC_DEFINE([JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY], [ ])\n\tAC_DEFINE([JEMALLOC_THREADED_INIT], [ ])\n\tAC_DEFINE([JEMALLOC_USE_CXX_THROW], [ ])\n\tif test \"${LG_SIZEOF_PTR}\" = \"3\"; then\n\t  default_retain=\"1\"\n\tfi\n\t;;\n  *-*-kfreebsd*)\n\tdnl syscall(2) and secure_getenv(3) are exposed by _GNU_SOURCE.\n\tJE_APPEND_VS(CPPFLAGS, -D_GNU_SOURCE)\n\tabi=\"elf\"\n\tAC_DEFINE([JEMALLOC_HAS_ALLOCA_H])\n\tAC_DEFINE([JEMALLOC_SYSCTL_VM_OVERCOMMIT], [ ])\n\tAC_DEFINE([JEMALLOC_THREADED_INIT], [ ])\n\tAC_DEFINE([JEMALLOC_USE_CXX_THROW], [ ])\n\t;;\n  *-*-netbsd*)\n\tAC_MSG_CHECKING([ABI])\n        AC_COMPILE_IFELSE([AC_LANG_PROGRAM(\n[[#ifdef __ELF__\n/* ELF */\n#else\n#error aout\n#endif\n]])],\n                          [abi=\"elf\"],\n                          [abi=\"aout\"])\n\tAC_MSG_RESULT([$abi])\n\t;;\n  *-*-solaris2*)\n\tabi=\"elf\"\n\tRPATH='-Wl,-R,$(1)'\n\tdnl Solaris needs this for sigwait().\n\tJE_APPEND_VS(CPPFLAGS, -D_POSIX_PTHREAD_SEMANTICS)\n\tJE_APPEND_VS(LIBS, -lposix4 -lsocket -lnsl)\n\t;;\n  *-ibm-aix*)\n\tif test \"${LG_SIZEOF_PTR}\" = \"3\"; then\n\t  dnl 64bit AIX\n\t  LD_PRELOAD_VAR=\"LDR_PRELOAD64\"\n\telse\n\t  dnl 32bit AIX\n\t  LD_PRELOAD_VAR=\"LDR_PRELOAD\"\n\tfi\n\tabi=\"xcoff\"\n\t;;\n  *-*-mingw* | *-*-cygwin*)\n\tabi=\"pecoff\"\n\tforce_tls=\"0\"\n\tmaps_coalesce=\"0\"\n\tRPATH=\"\"\n\tso=\"dll\"\n\tif test \"x$je_cv_msvc\" = \"xyes\" ; then\n\t  importlib=\"lib\"\n\t  DSO_LDFLAGS=\"-LD\"\n\t  EXTRA_LDFLAGS=\"-link -DEBUG\"\n\t  CTARGET='-Fo$@'\n\t  LDTARGET='-Fe$@'\n\t  AR='lib'\n\t  ARFLAGS='-nologo -out:'\n\t  AROUT='$@'\n\t  CC_MM=\n        else\n\t  importlib=\"${so}\"\n\t  DSO_LDFLAGS=\"-shared\"\n\t  link_whole_archive=\"1\"\n\tfi\n\tcase \"${host}\" in\n\t  *-*-cygwin*)\n\t    DUMP_SYMS=\"dumpbin /SYMBOLS\"\n\t    ;;\n\t  *)\n\t    ;;\n\tesac\n\ta=\"lib\"\n\tlibprefix=\"\"\n\tSOREV=\"${so}\"\n\tPIC_CFLAGS=\"\"\n\tif test \"${LG_SIZEOF_PTR}\" = \"3\"; then\n\t  default_retain=\"1\"\n\tfi\n\t;;\n  *)\n\tAC_MSG_RESULT([Unsupported operating system: ${host}])\n\tabi=\"elf\"\n\t;;\nesac\n\nJEMALLOC_USABLE_SIZE_CONST=const\nAC_CHECK_HEADERS([malloc.h], [\n  AC_MSG_CHECKING([whether malloc_usable_size definition can use const argument])\n  AC_COMPILE_IFELSE([AC_LANG_PROGRAM(\n    [#include <malloc.h>\n     #include <stddef.h>\n    size_t malloc_usable_size(const void *ptr);\n    ],\n    [])],[\n                AC_MSG_RESULT([yes])\n         ],[\n                JEMALLOC_USABLE_SIZE_CONST=\n                AC_MSG_RESULT([no])\n         ])\n])\nAC_DEFINE_UNQUOTED([JEMALLOC_USABLE_SIZE_CONST], [$JEMALLOC_USABLE_SIZE_CONST])\nAC_SUBST([abi])\nAC_SUBST([RPATH])\nAC_SUBST([LD_PRELOAD_VAR])\nAC_SUBST([so])\nAC_SUBST([importlib])\nAC_SUBST([o])\nAC_SUBST([a])\nAC_SUBST([exe])\nAC_SUBST([libprefix])\nAC_SUBST([link_whole_archive])\nAC_SUBST([DSO_LDFLAGS])\nAC_SUBST([EXTRA_LDFLAGS])\nAC_SUBST([SOREV])\nAC_SUBST([PIC_CFLAGS])\nAC_SUBST([CTARGET])\nAC_SUBST([LDTARGET])\nAC_SUBST([TEST_LD_MODE])\nAC_SUBST([MKLIB])\nAC_SUBST([ARFLAGS])\nAC_SUBST([AROUT])\nAC_SUBST([DUMP_SYMS])\nAC_SUBST([CC_MM])\n\ndnl Determine whether libm must be linked to use e.g. log(3).\nAC_SEARCH_LIBS([log], [m], , [AC_MSG_ERROR([Missing math functions])])\nif test \"x$ac_cv_search_log\" != \"xnone required\" ; then\n  LM=\"$ac_cv_search_log\"\nelse\n  LM=\nfi\nAC_SUBST(LM)\n\nJE_COMPILABLE([__attribute__ syntax],\n              [static __attribute__((unused)) void foo(void){}],\n              [],\n              [je_cv_attribute])\nif test \"x${je_cv_attribute}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_ATTR], [ ])\n  if test \"x${GCC}\" = \"xyes\" -a \"x${abi}\" = \"xelf\"; then\n    JE_CFLAGS_ADD([-fvisibility=hidden])\n    JE_CXXFLAGS_ADD([-fvisibility=hidden])\n  fi\nfi\ndnl Check for tls_model attribute support (clang 3.0 still lacks support).\nJE_CFLAGS_SAVE()\nJE_CFLAGS_ADD([-Werror])\nJE_CFLAGS_ADD([-herror_on_warning])\nJE_COMPILABLE([tls_model attribute], [],\n              [static __thread int\n               __attribute__((tls_model(\"initial-exec\"), unused)) foo;\n               foo = 0;],\n              [je_cv_tls_model])\nJE_CFLAGS_RESTORE()\ndnl (Setting of JEMALLOC_TLS_MODEL is done later, after we've checked for\ndnl --disable-initial-exec-tls)\n\ndnl Check for alloc_size attribute support.\nJE_CFLAGS_SAVE()\nJE_CFLAGS_ADD([-Werror])\nJE_CFLAGS_ADD([-herror_on_warning])\nJE_COMPILABLE([alloc_size attribute], [#include <stdlib.h>],\n              [void *foo(size_t size) __attribute__((alloc_size(1)));],\n              [je_cv_alloc_size])\nJE_CFLAGS_RESTORE()\nif test \"x${je_cv_alloc_size}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_ATTR_ALLOC_SIZE], [ ])\nfi\ndnl Check for format(gnu_printf, ...) attribute support.\nJE_CFLAGS_SAVE()\nJE_CFLAGS_ADD([-Werror])\nJE_CFLAGS_ADD([-herror_on_warning])\nJE_COMPILABLE([format(gnu_printf, ...) attribute], [#include <stdlib.h>],\n              [void *foo(const char *format, ...) __attribute__((format(gnu_printf, 1, 2)));],\n              [je_cv_format_gnu_printf])\nJE_CFLAGS_RESTORE()\nif test \"x${je_cv_format_gnu_printf}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_ATTR_FORMAT_GNU_PRINTF], [ ])\nfi\ndnl Check for format(printf, ...) attribute support.\nJE_CFLAGS_SAVE()\nJE_CFLAGS_ADD([-Werror])\nJE_CFLAGS_ADD([-herror_on_warning])\nJE_COMPILABLE([format(printf, ...) attribute], [#include <stdlib.h>],\n              [void *foo(const char *format, ...) __attribute__((format(printf, 1, 2)));],\n              [je_cv_format_printf])\nJE_CFLAGS_RESTORE()\nif test \"x${je_cv_format_printf}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_ATTR_FORMAT_PRINTF], [ ])\nfi\n\ndnl Check for format_arg(...) attribute support.\nJE_CFLAGS_SAVE()\nJE_CFLAGS_ADD([-Werror])\nJE_CFLAGS_ADD([-herror_on_warning])\nJE_COMPILABLE([format(printf, ...) attribute], [#include <stdlib.h>],\n              [const char * __attribute__((__format_arg__(1))) foo(const char *format);],\n              [je_cv_format_arg])\nJE_CFLAGS_RESTORE()\nif test \"x${je_cv_format_arg}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_ATTR_FORMAT_ARG], [ ])\nfi\n\ndnl Support optional additions to rpath.\nAC_ARG_WITH([rpath],\n  [AS_HELP_STRING([--with-rpath=<rpath>], [Colon-separated rpath (ELF systems only)])],\nif test \"x$with_rpath\" = \"xno\" ; then\n  RPATH_EXTRA=\nelse\n  RPATH_EXTRA=\"`echo $with_rpath | tr \\\":\\\" \\\" \\\"`\"\nfi,\n  RPATH_EXTRA=\n)\nAC_SUBST([RPATH_EXTRA])\n\ndnl Disable rules that do automatic regeneration of configure output by default.\nAC_ARG_ENABLE([autogen],\n  [AS_HELP_STRING([--enable-autogen], [Automatically regenerate configure output])],\nif test \"x$enable_autogen\" = \"xno\" ; then\n  enable_autogen=\"0\"\nelse\n  enable_autogen=\"1\"\nfi\n,\nenable_autogen=\"0\"\n)\nAC_SUBST([enable_autogen])\n\nAC_PROG_INSTALL\nAC_PROG_RANLIB\nAC_PATH_PROG([LD], [ld], [false], [$PATH])\nAC_PATH_PROG([AUTOCONF], [autoconf], [false], [$PATH])\n\ndnl Enable documentation\nAC_ARG_ENABLE([doc],\n\t      [AS_HELP_STRING([--enable-documentation], [Build documentation])],\nif test \"x$enable_doc\" = \"xno\" ; then\n  enable_doc=\"0\"\nelse\n  enable_doc=\"1\"\nfi\n,\nenable_doc=\"1\"\n)\nAC_SUBST([enable_doc])\n\ndnl Enable shared libs\nAC_ARG_ENABLE([shared],\n  [AS_HELP_STRING([--enable-shared], [Build shared libaries])],\nif test \"x$enable_shared\" = \"xno\" ; then\n  enable_shared=\"0\"\nelse\n  enable_shared=\"1\"\nfi\n,\nenable_shared=\"1\"\n)\nAC_SUBST([enable_shared])\n\ndnl Enable static libs\nAC_ARG_ENABLE([static],\n  [AS_HELP_STRING([--enable-static], [Build static libaries])],\nif test \"x$enable_static\" = \"xno\" ; then\n  enable_static=\"0\"\nelse\n  enable_static=\"1\"\nfi\n,\nenable_static=\"1\"\n)\nAC_SUBST([enable_static])\n\nif test \"$enable_shared$enable_static\" = \"00\" ; then\n  AC_MSG_ERROR([Please enable one of shared or static builds])\nfi\n\ndnl Perform no name mangling by default.\nAC_ARG_WITH([mangling],\n  [AS_HELP_STRING([--with-mangling=<map>], [Mangle symbols in <map>])],\n  [mangling_map=\"$with_mangling\"], [mangling_map=\"\"])\n\ndnl Do not prefix public APIs by default.\nAC_ARG_WITH([jemalloc_prefix],\n  [AS_HELP_STRING([--with-jemalloc-prefix=<prefix>], [Prefix to prepend to all public APIs])],\n  [JEMALLOC_PREFIX=\"$with_jemalloc_prefix\"],\n  [if test \"x$abi\" != \"xmacho\" -a \"x$abi\" != \"xpecoff\"; then\n  JEMALLOC_PREFIX=\"\"\nelse\n  JEMALLOC_PREFIX=\"je_\"\nfi]\n)\nif test \"x$JEMALLOC_PREFIX\" = \"x\" ; then\n  AC_DEFINE([JEMALLOC_IS_MALLOC])\nelse\n  JEMALLOC_CPREFIX=`echo ${JEMALLOC_PREFIX} | tr \"a-z\" \"A-Z\"`\n  AC_DEFINE_UNQUOTED([JEMALLOC_PREFIX], [\"$JEMALLOC_PREFIX\"])\n  AC_DEFINE_UNQUOTED([JEMALLOC_CPREFIX], [\"$JEMALLOC_CPREFIX\"])\nfi\nAC_SUBST([JEMALLOC_PREFIX])\nAC_SUBST([JEMALLOC_CPREFIX])\n\nAC_ARG_WITH([export],\n  [AS_HELP_STRING([--without-export], [disable exporting jemalloc public APIs])],\n  [if test \"x$with_export\" = \"xno\"; then\n  AC_DEFINE([JEMALLOC_EXPORT],[])\nfi]\n)\n\npublic_syms=\"aligned_alloc calloc dallocx free mallctl mallctlbymib mallctlnametomib malloc malloc_conf malloc_message malloc_stats_print malloc_usable_size mallocx smallocx_${jemalloc_version_gid} nallocx posix_memalign rallocx realloc sallocx sdallocx xallocx\"\ndnl Check for additional platform-specific public API functions.\nAC_CHECK_FUNC([memalign],\n\t      [AC_DEFINE([JEMALLOC_OVERRIDE_MEMALIGN], [ ])\n\t       public_syms=\"${public_syms} memalign\"])\nAC_CHECK_FUNC([valloc],\n\t      [AC_DEFINE([JEMALLOC_OVERRIDE_VALLOC], [ ])\n\t       public_syms=\"${public_syms} valloc\"])\n\ndnl Check for allocator-related functions that should be wrapped.\nwrap_syms=\nif test \"x${JEMALLOC_PREFIX}\" = \"x\" ; then\n  AC_CHECK_FUNC([__libc_calloc],\n\t\t[AC_DEFINE([JEMALLOC_OVERRIDE___LIBC_CALLOC], [ ])\n\t\t wrap_syms=\"${wrap_syms} __libc_calloc\"])\n  AC_CHECK_FUNC([__libc_free],\n\t\t[AC_DEFINE([JEMALLOC_OVERRIDE___LIBC_FREE], [ ])\n\t\t wrap_syms=\"${wrap_syms} __libc_free\"])\n  AC_CHECK_FUNC([__libc_malloc],\n\t\t[AC_DEFINE([JEMALLOC_OVERRIDE___LIBC_MALLOC], [ ])\n\t\t wrap_syms=\"${wrap_syms} __libc_malloc\"])\n  AC_CHECK_FUNC([__libc_memalign],\n\t\t[AC_DEFINE([JEMALLOC_OVERRIDE___LIBC_MEMALIGN], [ ])\n\t\t wrap_syms=\"${wrap_syms} __libc_memalign\"])\n  AC_CHECK_FUNC([__libc_realloc],\n\t\t[AC_DEFINE([JEMALLOC_OVERRIDE___LIBC_REALLOC], [ ])\n\t\t wrap_syms=\"${wrap_syms} __libc_realloc\"])\n  AC_CHECK_FUNC([__libc_valloc],\n\t\t[AC_DEFINE([JEMALLOC_OVERRIDE___LIBC_VALLOC], [ ])\n\t\t wrap_syms=\"${wrap_syms} __libc_valloc\"])\n  AC_CHECK_FUNC([__posix_memalign],\n\t\t[AC_DEFINE([JEMALLOC_OVERRIDE___POSIX_MEMALIGN], [ ])\n\t\t wrap_syms=\"${wrap_syms} __posix_memalign\"])\nfi\n\ncase \"${host}\" in\n  *-*-mingw* | *-*-cygwin*)\n    wrap_syms=\"${wrap_syms} tls_callback\"\n    ;;\n  *)\n    ;;\nesac\n\ndnl Mangle library-private APIs.\nAC_ARG_WITH([private_namespace],\n  [AS_HELP_STRING([--with-private-namespace=<prefix>], [Prefix to prepend to all library-private APIs])],\n  [JEMALLOC_PRIVATE_NAMESPACE=\"${with_private_namespace}je_\"],\n  [JEMALLOC_PRIVATE_NAMESPACE=\"je_\"]\n)\nAC_DEFINE_UNQUOTED([JEMALLOC_PRIVATE_NAMESPACE], [$JEMALLOC_PRIVATE_NAMESPACE])\nprivate_namespace=\"$JEMALLOC_PRIVATE_NAMESPACE\"\nAC_SUBST([private_namespace])\n\ndnl Do not add suffix to installed files by default.\nAC_ARG_WITH([install_suffix],\n  [AS_HELP_STRING([--with-install-suffix=<suffix>], [Suffix to append to all installed files])],\n  [INSTALL_SUFFIX=\"$with_install_suffix\"],\n  [INSTALL_SUFFIX=]\n)\ninstall_suffix=\"$INSTALL_SUFFIX\"\nAC_SUBST([install_suffix])\n\ndnl Specify default malloc_conf.\nAC_ARG_WITH([malloc_conf],\n  [AS_HELP_STRING([--with-malloc-conf=<malloc_conf>], [config.malloc_conf options string])],\n  [JEMALLOC_CONFIG_MALLOC_CONF=\"$with_malloc_conf\"],\n  [JEMALLOC_CONFIG_MALLOC_CONF=\"\"]\n)\nconfig_malloc_conf=\"$JEMALLOC_CONFIG_MALLOC_CONF\"\nAC_DEFINE_UNQUOTED([JEMALLOC_CONFIG_MALLOC_CONF], [\"$config_malloc_conf\"])\n\ndnl Substitute @je_@ in jemalloc_protos.h.in, primarily to make generation of\ndnl jemalloc_protos_jet.h easy.\nje_=\"je_\"\nAC_SUBST([je_])\n\ncfgoutputs_in=\"Makefile.in\"\ncfgoutputs_in=\"${cfgoutputs_in} jemalloc.pc.in\"\ncfgoutputs_in=\"${cfgoutputs_in} doc/html.xsl.in\"\ncfgoutputs_in=\"${cfgoutputs_in} doc/manpages.xsl.in\"\ncfgoutputs_in=\"${cfgoutputs_in} doc/jemalloc.xml.in\"\ncfgoutputs_in=\"${cfgoutputs_in} include/jemalloc/jemalloc_macros.h.in\"\ncfgoutputs_in=\"${cfgoutputs_in} include/jemalloc/jemalloc_protos.h.in\"\ncfgoutputs_in=\"${cfgoutputs_in} include/jemalloc/jemalloc_typedefs.h.in\"\ncfgoutputs_in=\"${cfgoutputs_in} include/jemalloc/internal/jemalloc_preamble.h.in\"\ncfgoutputs_in=\"${cfgoutputs_in} test/test.sh.in\"\ncfgoutputs_in=\"${cfgoutputs_in} test/include/test/jemalloc_test.h.in\"\n\ncfgoutputs_out=\"Makefile\"\ncfgoutputs_out=\"${cfgoutputs_out} jemalloc.pc\"\ncfgoutputs_out=\"${cfgoutputs_out} doc/html.xsl\"\ncfgoutputs_out=\"${cfgoutputs_out} doc/manpages.xsl\"\ncfgoutputs_out=\"${cfgoutputs_out} doc/jemalloc.xml\"\ncfgoutputs_out=\"${cfgoutputs_out} include/jemalloc/jemalloc_macros.h\"\ncfgoutputs_out=\"${cfgoutputs_out} include/jemalloc/jemalloc_protos.h\"\ncfgoutputs_out=\"${cfgoutputs_out} include/jemalloc/jemalloc_typedefs.h\"\ncfgoutputs_out=\"${cfgoutputs_out} include/jemalloc/internal/jemalloc_preamble.h\"\ncfgoutputs_out=\"${cfgoutputs_out} test/test.sh\"\ncfgoutputs_out=\"${cfgoutputs_out} test/include/test/jemalloc_test.h\"\n\ncfgoutputs_tup=\"Makefile\"\ncfgoutputs_tup=\"${cfgoutputs_tup} jemalloc.pc:jemalloc.pc.in\"\ncfgoutputs_tup=\"${cfgoutputs_tup} doc/html.xsl:doc/html.xsl.in\"\ncfgoutputs_tup=\"${cfgoutputs_tup} doc/manpages.xsl:doc/manpages.xsl.in\"\ncfgoutputs_tup=\"${cfgoutputs_tup} doc/jemalloc.xml:doc/jemalloc.xml.in\"\ncfgoutputs_tup=\"${cfgoutputs_tup} include/jemalloc/jemalloc_macros.h:include/jemalloc/jemalloc_macros.h.in\"\ncfgoutputs_tup=\"${cfgoutputs_tup} include/jemalloc/jemalloc_protos.h:include/jemalloc/jemalloc_protos.h.in\"\ncfgoutputs_tup=\"${cfgoutputs_tup} include/jemalloc/jemalloc_typedefs.h:include/jemalloc/jemalloc_typedefs.h.in\"\ncfgoutputs_tup=\"${cfgoutputs_tup} include/jemalloc/internal/jemalloc_preamble.h\"\ncfgoutputs_tup=\"${cfgoutputs_tup} test/test.sh:test/test.sh.in\"\ncfgoutputs_tup=\"${cfgoutputs_tup} test/include/test/jemalloc_test.h:test/include/test/jemalloc_test.h.in\"\n\ncfghdrs_in=\"include/jemalloc/jemalloc_defs.h.in\"\ncfghdrs_in=\"${cfghdrs_in} include/jemalloc/internal/jemalloc_internal_defs.h.in\"\ncfghdrs_in=\"${cfghdrs_in} include/jemalloc/internal/private_symbols.sh\"\ncfghdrs_in=\"${cfghdrs_in} include/jemalloc/internal/private_namespace.sh\"\ncfghdrs_in=\"${cfghdrs_in} include/jemalloc/internal/public_namespace.sh\"\ncfghdrs_in=\"${cfghdrs_in} include/jemalloc/internal/public_unnamespace.sh\"\ncfghdrs_in=\"${cfghdrs_in} include/jemalloc/jemalloc_rename.sh\"\ncfghdrs_in=\"${cfghdrs_in} include/jemalloc/jemalloc_mangle.sh\"\ncfghdrs_in=\"${cfghdrs_in} include/jemalloc/jemalloc.sh\"\ncfghdrs_in=\"${cfghdrs_in} test/include/test/jemalloc_test_defs.h.in\"\n\ncfghdrs_out=\"include/jemalloc/jemalloc_defs.h\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/jemalloc${install_suffix}.h\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/internal/private_symbols.awk\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/internal/private_symbols_jet.awk\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/internal/public_symbols.txt\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/internal/public_namespace.h\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/internal/public_unnamespace.h\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/jemalloc_protos_jet.h\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/jemalloc_rename.h\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/jemalloc_mangle.h\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/jemalloc_mangle_jet.h\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/internal/jemalloc_internal_defs.h\"\ncfghdrs_out=\"${cfghdrs_out} test/include/test/jemalloc_test_defs.h\"\n\ncfghdrs_tup=\"include/jemalloc/jemalloc_defs.h:include/jemalloc/jemalloc_defs.h.in\"\ncfghdrs_tup=\"${cfghdrs_tup} include/jemalloc/internal/jemalloc_internal_defs.h:include/jemalloc/internal/jemalloc_internal_defs.h.in\"\ncfghdrs_tup=\"${cfghdrs_tup} test/include/test/jemalloc_test_defs.h:test/include/test/jemalloc_test_defs.h.in\"\n\ndnl ============================================================================\ndnl jemalloc build options.\ndnl\n\ndnl Do not compile with debugging by default.\nAC_ARG_ENABLE([debug],\n  [AS_HELP_STRING([--enable-debug],\n                  [Build debugging code])],\n[if test \"x$enable_debug\" = \"xno\" ; then\n  enable_debug=\"0\"\nelse\n  enable_debug=\"1\"\nfi\n],\n[enable_debug=\"0\"]\n)\nif test \"x$enable_debug\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_DEBUG], [ ])\nfi\nif test \"x$enable_debug\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_DEBUG], [ ])\nfi\nAC_SUBST([enable_debug])\n\ndnl Only optimize if not debugging.\nif test \"x$enable_debug\" = \"x0\" ; then\n  if test \"x$GCC\" = \"xyes\" ; then\n    JE_CFLAGS_ADD([-O3])\n    JE_CXXFLAGS_ADD([-O3])\n    JE_CFLAGS_ADD([-funroll-loops])\n  elif test \"x$je_cv_msvc\" = \"xyes\" ; then\n    JE_CFLAGS_ADD([-O2])\n    JE_CXXFLAGS_ADD([-O2])\n  else\n    JE_CFLAGS_ADD([-O])\n    JE_CXXFLAGS_ADD([-O])\n  fi\nfi\n\ndnl Enable statistics calculation by default.\nAC_ARG_ENABLE([stats],\n  [AS_HELP_STRING([--disable-stats],\n                  [Disable statistics calculation/reporting])],\n[if test \"x$enable_stats\" = \"xno\" ; then\n  enable_stats=\"0\"\nelse\n  enable_stats=\"1\"\nfi\n],\n[enable_stats=\"1\"]\n)\nif test \"x$enable_stats\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_STATS], [ ])\nfi\nAC_SUBST([enable_stats])\n\ndnl Do not enable smallocx by default.\nAC_ARG_ENABLE([experimental_smallocx],\n  [AS_HELP_STRING([--enable-experimental-smallocx], [Enable experimental smallocx API])],\n[if test \"x$enable_experimental_smallocx\" = \"xno\" ; then\nenable_experimental_smallocx=\"0\"\nelse\nenable_experimental_smallocx=\"1\"\nfi\n],\n[enable_experimental_smallocx=\"0\"]\n)\nif test \"x$enable_experimental_smallocx\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_EXPERIMENTAL_SMALLOCX_API])\nfi\nAC_SUBST([enable_experimental_smallocx])\n\ndnl Do not enable profiling by default.\nAC_ARG_ENABLE([prof],\n  [AS_HELP_STRING([--enable-prof], [Enable allocation profiling])],\n[if test \"x$enable_prof\" = \"xno\" ; then\n  enable_prof=\"0\"\nelse\n  enable_prof=\"1\"\nfi\n],\n[enable_prof=\"0\"]\n)\nif test \"x$enable_prof\" = \"x1\" ; then\n  backtrace_method=\"\"\nelse\n  backtrace_method=\"N/A\"\nfi\n\nAC_ARG_ENABLE([prof-libunwind],\n  [AS_HELP_STRING([--enable-prof-libunwind], [Use libunwind for backtracing])],\n[if test \"x$enable_prof_libunwind\" = \"xno\" ; then\n  enable_prof_libunwind=\"0\"\nelse\n  enable_prof_libunwind=\"1\"\nfi\n],\n[enable_prof_libunwind=\"0\"]\n)\nAC_ARG_WITH([static_libunwind],\n  [AS_HELP_STRING([--with-static-libunwind=<libunwind.a>],\n  [Path to static libunwind library; use rather than dynamically linking])],\nif test \"x$with_static_libunwind\" = \"xno\" ; then\n  LUNWIND=\"-lunwind\"\nelse\n  if test ! -f \"$with_static_libunwind\" ; then\n    AC_MSG_ERROR([Static libunwind not found: $with_static_libunwind])\n  fi\n  LUNWIND=\"$with_static_libunwind\"\nfi,\n  LUNWIND=\"-lunwind\"\n)\nif test \"x$backtrace_method\" = \"x\" -a \"x$enable_prof_libunwind\" = \"x1\" ; then\n  AC_CHECK_HEADERS([libunwind.h], , [enable_prof_libunwind=\"0\"])\n  if test \"x$LUNWIND\" = \"x-lunwind\" ; then\n    AC_CHECK_LIB([unwind], [unw_backtrace], [JE_APPEND_VS(LIBS, $LUNWIND)],\n                 [enable_prof_libunwind=\"0\"])\n  else\n    JE_APPEND_VS(LIBS, $LUNWIND)\n  fi\n  if test \"x${enable_prof_libunwind}\" = \"x1\" ; then\n    backtrace_method=\"libunwind\"\n    AC_DEFINE([JEMALLOC_PROF_LIBUNWIND], [ ])\n  fi\nfi\n\nAC_ARG_ENABLE([prof-libgcc],\n  [AS_HELP_STRING([--disable-prof-libgcc],\n  [Do not use libgcc for backtracing])],\n[if test \"x$enable_prof_libgcc\" = \"xno\" ; then\n  enable_prof_libgcc=\"0\"\nelse\n  enable_prof_libgcc=\"1\"\nfi\n],\n[enable_prof_libgcc=\"1\"]\n)\nif test \"x$backtrace_method\" = \"x\" -a \"x$enable_prof_libgcc\" = \"x1\" \\\n     -a \"x$GCC\" = \"xyes\" ; then\n  AC_CHECK_HEADERS([unwind.h], , [enable_prof_libgcc=\"0\"])\n  if test \"x${enable_prof_libgcc}\" = \"x1\" ; then\n    AC_CHECK_LIB([gcc], [_Unwind_Backtrace], [JE_APPEND_VS(LIBS, -lgcc)], [enable_prof_libgcc=\"0\"])\n  fi\n  if test \"x${enable_prof_libgcc}\" = \"x1\" ; then\n    backtrace_method=\"libgcc\"\n    AC_DEFINE([JEMALLOC_PROF_LIBGCC], [ ])\n  fi\nelse\n  enable_prof_libgcc=\"0\"\nfi\n\nAC_ARG_ENABLE([prof-gcc],\n  [AS_HELP_STRING([--disable-prof-gcc],\n  [Do not use gcc intrinsics for backtracing])],\n[if test \"x$enable_prof_gcc\" = \"xno\" ; then\n  enable_prof_gcc=\"0\"\nelse\n  enable_prof_gcc=\"1\"\nfi\n],\n[enable_prof_gcc=\"1\"]\n)\nif test \"x$backtrace_method\" = \"x\" -a \"x$enable_prof_gcc\" = \"x1\" \\\n     -a \"x$GCC\" = \"xyes\" ; then\n  JE_CFLAGS_ADD([-fno-omit-frame-pointer])\n  backtrace_method=\"gcc intrinsics\"\n  AC_DEFINE([JEMALLOC_PROF_GCC], [ ])\nelse\n  enable_prof_gcc=\"0\"\nfi\n\nif test \"x$backtrace_method\" = \"x\" ; then\n  backtrace_method=\"none (disabling profiling)\"\n  enable_prof=\"0\"\nfi\nAC_MSG_CHECKING([configured backtracing method])\nAC_MSG_RESULT([$backtrace_method])\nif test \"x$enable_prof\" = \"x1\" ; then\n  dnl Heap profiling uses the log(3) function.\n  JE_APPEND_VS(LIBS, $LM)\n\n  AC_DEFINE([JEMALLOC_PROF], [ ])\nfi\nAC_SUBST([enable_prof])\n\ndnl Indicate whether adjacent virtual memory mappings automatically coalesce\ndnl (and fragment on demand).\nif test \"x${maps_coalesce}\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_MAPS_COALESCE], [ ])\nfi\n\ndnl Indicate whether to retain memory (rather than using munmap()) by default.\nif test \"x$default_retain\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_RETAIN], [ ])\nfi\n\ndnl Enable allocation from DSS if supported by the OS.\nhave_dss=\"1\"\ndnl Check whether the BSD/SUSv1 sbrk() exists.  If not, disable DSS support.\nAC_CHECK_FUNC([sbrk], [have_sbrk=\"1\"], [have_sbrk=\"0\"])\nif test \"x$have_sbrk\" = \"x1\" ; then\n  if test \"x$sbrk_deprecated\" = \"x1\" ; then\n    AC_MSG_RESULT([Disabling dss allocation because sbrk is deprecated])\n    have_dss=\"0\"\n  fi\nelse\n  have_dss=\"0\"\nfi\n\nif test \"x$have_dss\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_DSS], [ ])\nfi\n\ndnl Support the junk/zero filling option by default.\nAC_ARG_ENABLE([fill],\n  [AS_HELP_STRING([--disable-fill], [Disable support for junk/zero filling])],\n[if test \"x$enable_fill\" = \"xno\" ; then\n  enable_fill=\"0\"\nelse\n  enable_fill=\"1\"\nfi\n],\n[enable_fill=\"1\"]\n)\nif test \"x$enable_fill\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_FILL], [ ])\nfi\nAC_SUBST([enable_fill])\n\ndnl Disable utrace(2)-based tracing by default.\nAC_ARG_ENABLE([utrace],\n  [AS_HELP_STRING([--enable-utrace], [Enable utrace(2)-based tracing])],\n[if test \"x$enable_utrace\" = \"xno\" ; then\n  enable_utrace=\"0\"\nelse\n  enable_utrace=\"1\"\nfi\n],\n[enable_utrace=\"0\"]\n)\nJE_COMPILABLE([utrace(2)], [\n#include <sys/types.h>\n#include <sys/param.h>\n#include <sys/time.h>\n#include <sys/uio.h>\n#include <sys/ktrace.h>\n], [\n\tutrace((void *)0, 0);\n], [je_cv_utrace])\nif test \"x${je_cv_utrace}\" = \"xno\" ; then\n  enable_utrace=\"0\"\nfi\nif test \"x$enable_utrace\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_UTRACE], [ ])\nfi\nAC_SUBST([enable_utrace])\n\ndnl Do not support the xmalloc option by default.\nAC_ARG_ENABLE([xmalloc],\n  [AS_HELP_STRING([--enable-xmalloc], [Support xmalloc option])],\n[if test \"x$enable_xmalloc\" = \"xno\" ; then\n  enable_xmalloc=\"0\"\nelse\n  enable_xmalloc=\"1\"\nfi\n],\n[enable_xmalloc=\"0\"]\n)\nif test \"x$enable_xmalloc\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_XMALLOC], [ ])\nfi\nAC_SUBST([enable_xmalloc])\n\ndnl Support cache-oblivious allocation alignment by default.\nAC_ARG_ENABLE([cache-oblivious],\n  [AS_HELP_STRING([--disable-cache-oblivious],\n                  [Disable support for cache-oblivious allocation alignment])],\n[if test \"x$enable_cache_oblivious\" = \"xno\" ; then\n  enable_cache_oblivious=\"0\"\nelse\n  enable_cache_oblivious=\"1\"\nfi\n],\n[enable_cache_oblivious=\"1\"]\n)\nif test \"x$enable_cache_oblivious\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_CACHE_OBLIVIOUS], [ ])\nfi\nAC_SUBST([enable_cache_oblivious])\n\ndnl Do not log by default.\nAC_ARG_ENABLE([log],\n  [AS_HELP_STRING([--enable-log], [Support debug logging])],\n[if test \"x$enable_log\" = \"xno\" ; then\n  enable_log=\"0\"\nelse\n  enable_log=\"1\"\nfi\n],\n[enable_log=\"0\"]\n)\nif test \"x$enable_log\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_LOG], [ ])\nfi\nAC_SUBST([enable_log])\n\ndnl Do not use readlinkat by default\nAC_ARG_ENABLE([readlinkat],\n  [AS_HELP_STRING([--enable-readlinkat], [Use readlinkat over readlink])],\n[if test \"x$enable_readlinkat\" = \"xno\" ; then\n  enable_readlinkat=\"0\"\nelse\n  enable_readlinkat=\"1\"\nfi\n],\n[enable_readlinkat=\"0\"]\n)\nif test \"x$enable_readlinkat\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_READLINKAT], [ ])\nfi\nAC_SUBST([enable_readlinkat])\n\ndnl Avoid extra safety checks by default\nAC_ARG_ENABLE([opt-safety-checks],\n  [AS_HELP_STRING([--enable-opt-safety-checks],\n  [Perform certain low-overhead checks, even in opt mode])],\n[if test \"x$enable_opt_safety_checks\" = \"xno\" ; then\n  enable_opt_safety_checks=\"0\"\nelse\n  enable_opt_safety_checks=\"1\"\nfi\n],\n[enable_opt_safety_checks=\"0\"]\n)\nif test \"x$enable_opt_safety_checks\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_OPT_SAFETY_CHECKS], [ ])\nfi\nAC_SUBST([enable_opt_safety_checks])\n\nJE_COMPILABLE([a program using __builtin_unreachable], [\nvoid foo (void) {\n  __builtin_unreachable();\n}\n], [\n\t{\n\t\tfoo();\n\t}\n], [je_cv_gcc_builtin_unreachable])\nif test \"x${je_cv_gcc_builtin_unreachable}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_INTERNAL_UNREACHABLE], [__builtin_unreachable])\nelse\n  AC_DEFINE([JEMALLOC_INTERNAL_UNREACHABLE], [abort])\nfi\n\ndnl ============================================================================\ndnl Check for  __builtin_ffsl(), then ffsl(3), and fail if neither are found.\ndnl One of those two functions should (theoretically) exist on all platforms\ndnl that jemalloc currently has a chance of functioning on without modification.\ndnl We additionally assume ffs[ll]() or __builtin_ffs[ll]() are defined if\ndnl ffsl() or __builtin_ffsl() are defined, respectively.\nJE_COMPILABLE([a program using __builtin_ffsl], [\n#include <stdio.h>\n#include <strings.h>\n#include <string.h>\n], [\n\t{\n\t\tint rv = __builtin_ffsl(0x08);\n\t\tprintf(\"%d\\n\", rv);\n\t}\n], [je_cv_gcc_builtin_ffsl])\nif test \"x${je_cv_gcc_builtin_ffsl}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_INTERNAL_FFSLL], [__builtin_ffsll])\n  AC_DEFINE([JEMALLOC_INTERNAL_FFSL], [__builtin_ffsl])\n  AC_DEFINE([JEMALLOC_INTERNAL_FFS], [__builtin_ffs])\nelse\n  JE_COMPILABLE([a program using ffsl], [\n  #include <stdio.h>\n  #include <strings.h>\n  #include <string.h>\n  ], [\n\t{\n\t\tint rv = ffsl(0x08);\n\t\tprintf(\"%d\\n\", rv);\n\t}\n  ], [je_cv_function_ffsl])\n  if test \"x${je_cv_function_ffsl}\" = \"xyes\" ; then\n    AC_DEFINE([JEMALLOC_INTERNAL_FFSLL], [ffsll])\n    AC_DEFINE([JEMALLOC_INTERNAL_FFSL], [ffsl])\n    AC_DEFINE([JEMALLOC_INTERNAL_FFS], [ffs])\n  else\n    AC_MSG_ERROR([Cannot build without ffsl(3) or __builtin_ffsl()])\n  fi\nfi\n\nJE_COMPILABLE([a program using __builtin_popcountl], [\n#include <stdio.h>\n#include <strings.h>\n#include <string.h>\n], [\n\t{\n\t\tint rv = __builtin_popcountl(0x08);\n\t\tprintf(\"%d\\n\", rv);\n\t}\n], [je_cv_gcc_builtin_popcountl])\nif test \"x${je_cv_gcc_builtin_popcountl}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_INTERNAL_POPCOUNT], [__builtin_popcount])\n  AC_DEFINE([JEMALLOC_INTERNAL_POPCOUNTL], [__builtin_popcountl])\nfi\n\nAC_ARG_WITH([lg_quantum],\n  [AS_HELP_STRING([--with-lg-quantum=<lg-quantum>],\n   [Base 2 log of minimum allocation alignment])],\n  [LG_QUANTA=\"$with_lg_quantum\"],\n  [LG_QUANTA=\"3 4\"])\nif test \"x$with_lg_quantum\" != \"x\" ; then\n  AC_DEFINE_UNQUOTED([LG_QUANTUM], [$with_lg_quantum])\nfi\n\nAC_ARG_WITH([lg_page],\n  [AS_HELP_STRING([--with-lg-page=<lg-page>], [Base 2 log of system page size])],\n  [LG_PAGE=\"$with_lg_page\"], [LG_PAGE=\"detect\"])\nif test \"x$LG_PAGE\" = \"xdetect\"; then\n  AC_CACHE_CHECK([LG_PAGE],\n               [je_cv_lg_page],\n               AC_RUN_IFELSE([AC_LANG_PROGRAM(\n[[\n#include <strings.h>\n#ifdef _WIN32\n#include <windows.h>\n#else\n#include <unistd.h>\n#endif\n#include <stdio.h>\n]],\n[[\n    int result;\n    FILE *f;\n\n#ifdef _WIN32\n    SYSTEM_INFO si;\n    GetSystemInfo(&si);\n    result = si.dwPageSize;\n#else\n    result = sysconf(_SC_PAGESIZE);\n#endif\n    if (result == -1) {\n\treturn 1;\n    }\n    result = JEMALLOC_INTERNAL_FFSL(result) - 1;\n\n    f = fopen(\"conftest.out\", \"w\");\n    if (f == NULL) {\n\treturn 1;\n    }\n    fprintf(f, \"%d\", result);\n    fclose(f);\n\n    return 0;\n]])],\n                             [je_cv_lg_page=`cat conftest.out`],\n                             [je_cv_lg_page=undefined],\n                             [je_cv_lg_page=12]))\nfi\nif test \"x${je_cv_lg_page}\" != \"x\" ; then\n  LG_PAGE=\"${je_cv_lg_page}\"\nfi\nif test \"x${LG_PAGE}\" != \"xundefined\" ; then\n   AC_DEFINE_UNQUOTED([LG_PAGE], [$LG_PAGE])\nelse\n   AC_MSG_ERROR([cannot determine value for LG_PAGE])\nfi\n\nAC_ARG_WITH([lg_hugepage],\n  [AS_HELP_STRING([--with-lg-hugepage=<lg-hugepage>],\n   [Base 2 log of system huge page size])],\n  [je_cv_lg_hugepage=\"${with_lg_hugepage}\"],\n  [je_cv_lg_hugepage=\"\"])\nif test \"x${je_cv_lg_hugepage}\" = \"x\" ; then\n  dnl Look in /proc/meminfo (Linux-specific) for information on the default huge\n  dnl page size, if any.  The relevant line looks like:\n  dnl\n  dnl   Hugepagesize:       2048 kB\n  if test -e \"/proc/meminfo\" ; then\n    hpsk=[`cat /proc/meminfo 2>/dev/null | \\\n          grep -e '^Hugepagesize:[[:space:]]\\+[0-9]\\+[[:space:]]kB$' | \\\n          awk '{print $2}'`]\n    if test \"x${hpsk}\" != \"x\" ; then\n      je_cv_lg_hugepage=10\n      while test \"${hpsk}\" -gt 1 ; do\n        hpsk=\"$((hpsk / 2))\"\n        je_cv_lg_hugepage=\"$((je_cv_lg_hugepage + 1))\"\n      done\n    fi\n  fi\n\n  dnl Set default if unable to automatically configure.\n  if test \"x${je_cv_lg_hugepage}\" = \"x\" ; then\n    je_cv_lg_hugepage=21\n  fi\nfi\nif test \"x${LG_PAGE}\" != \"xundefined\" -a \\\n        \"${je_cv_lg_hugepage}\" -lt \"${LG_PAGE}\" ; then\n  AC_MSG_ERROR([Huge page size (2^${je_cv_lg_hugepage}) must be at least page size (2^${LG_PAGE})])\nfi\nAC_DEFINE_UNQUOTED([LG_HUGEPAGE], [${je_cv_lg_hugepage}])\n\ndnl ============================================================================\ndnl Enable libdl by default.\nAC_ARG_ENABLE([libdl],\n  [AS_HELP_STRING([--disable-libdl],\n  [Do not use libdl])],\n[if test \"x$enable_libdl\" = \"xno\" ; then\n  enable_libdl=\"0\"\nelse\n  enable_libdl=\"1\"\nfi\n],\n[enable_libdl=\"1\"]\n)\nAC_SUBST([libdl])\n\ndnl ============================================================================\ndnl Configure pthreads.\n\nif test \"x$abi\" != \"xpecoff\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_PTHREAD], [ ])\n  AC_CHECK_HEADERS([pthread.h], , [AC_MSG_ERROR([pthread.h is missing])])\n  dnl Some systems may embed pthreads functionality in libc; check for libpthread\n  dnl first, but try libc too before failing.\n  AC_CHECK_LIB([pthread], [pthread_create], [JE_APPEND_VS(LIBS, -pthread)],\n               [AC_SEARCH_LIBS([pthread_create], , ,\n                               AC_MSG_ERROR([libpthread is missing]))])\n  wrap_syms=\"${wrap_syms} pthread_create\"\n  have_pthread=\"1\"\n\ndnl Check if we have dlsym support.\n  if test \"x$enable_libdl\" = \"x1\" ; then\n    have_dlsym=\"1\"\n    AC_CHECK_HEADERS([dlfcn.h],\n      AC_CHECK_FUNC([dlsym], [],\n        [AC_CHECK_LIB([dl], [dlsym], [LIBS=\"$LIBS -ldl\"], [have_dlsym=\"0\"])]),\n      [have_dlsym=\"0\"])\n    if test \"x$have_dlsym\" = \"x1\" ; then\n      AC_DEFINE([JEMALLOC_HAVE_DLSYM], [ ])\n    fi\n  else\n    have_dlsym=\"0\"\n  fi\n\n  JE_COMPILABLE([pthread_atfork(3)], [\n#include <pthread.h>\n], [\n  pthread_atfork((void *)0, (void *)0, (void *)0);\n], [je_cv_pthread_atfork])\n  if test \"x${je_cv_pthread_atfork}\" = \"xyes\" ; then\n    AC_DEFINE([JEMALLOC_HAVE_PTHREAD_ATFORK], [ ])\n  fi\n  dnl Check if pthread_setname_np is available with the expected API.\n  JE_COMPILABLE([pthread_setname_np(3)], [\n#include <pthread.h>\n], [\n  pthread_setname_np(pthread_self(), \"setname_test\");\n], [je_cv_pthread_setname_np])\n  if test \"x${je_cv_pthread_setname_np}\" = \"xyes\" ; then\n    AC_DEFINE([JEMALLOC_HAVE_PTHREAD_SETNAME_NP], [ ])\n  fi\nfi\n\nJE_APPEND_VS(CPPFLAGS, -D_REENTRANT)\n\ndnl Check whether clock_gettime(2) is in libc or librt.\nAC_SEARCH_LIBS([clock_gettime], [rt])\n\ndnl Cray wrapper compiler often adds `-lrt` when using `-static`. Check with\ndnl `-dynamic` as well in case a user tries to dynamically link in jemalloc\nif test \"x$je_cv_cray_prgenv_wrapper\" = \"xyes\" ; then\n  if test \"$ac_cv_search_clock_gettime\" != \"-lrt\"; then\n    JE_CFLAGS_SAVE()\n\n    unset ac_cv_search_clock_gettime\n    JE_CFLAGS_ADD([-dynamic])\n    AC_SEARCH_LIBS([clock_gettime], [rt])\n\n    JE_CFLAGS_RESTORE()\n  fi\nfi\n\ndnl check for CLOCK_MONOTONIC_COARSE (Linux-specific).\nJE_COMPILABLE([clock_gettime(CLOCK_MONOTONIC_COARSE, ...)], [\n#include <time.h>\n], [\n\tstruct timespec ts;\n\n\tclock_gettime(CLOCK_MONOTONIC_COARSE, &ts);\n], [je_cv_clock_monotonic_coarse])\nif test \"x${je_cv_clock_monotonic_coarse}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_CLOCK_MONOTONIC_COARSE])\nfi\n\ndnl check for CLOCK_MONOTONIC.\nJE_COMPILABLE([clock_gettime(CLOCK_MONOTONIC, ...)], [\n#include <unistd.h>\n#include <time.h>\n], [\n\tstruct timespec ts;\n\n\tclock_gettime(CLOCK_MONOTONIC, &ts);\n#if !defined(_POSIX_MONOTONIC_CLOCK) || _POSIX_MONOTONIC_CLOCK < 0\n#  error _POSIX_MONOTONIC_CLOCK missing/invalid\n#endif\n], [je_cv_clock_monotonic])\nif test \"x${je_cv_clock_monotonic}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_CLOCK_MONOTONIC])\nfi\n\ndnl Check for mach_absolute_time().\nJE_COMPILABLE([mach_absolute_time()], [\n#include <mach/mach_time.h>\n], [\n\tmach_absolute_time();\n], [je_cv_mach_absolute_time])\nif test \"x${je_cv_mach_absolute_time}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_MACH_ABSOLUTE_TIME])\nfi\n\ndnl Use syscall(2) (if available) by default.\nAC_ARG_ENABLE([syscall],\n  [AS_HELP_STRING([--disable-syscall], [Disable use of syscall(2)])],\n[if test \"x$enable_syscall\" = \"xno\" ; then\n  enable_syscall=\"0\"\nelse\n  enable_syscall=\"1\"\nfi\n],\n[enable_syscall=\"1\"]\n)\nif test \"x$enable_syscall\" = \"x1\" ; then\n  dnl Check if syscall(2) is usable.  Treat warnings as errors, so that e.g. OS\n  dnl X 10.12's deprecation warning prevents use.\n  JE_CFLAGS_SAVE()\n  JE_CFLAGS_ADD([-Werror])\n  JE_COMPILABLE([syscall(2)], [\n#include <sys/syscall.h>\n#include <unistd.h>\n], [\n\tsyscall(SYS_write, 2, \"hello\", 5);\n],\n                [je_cv_syscall])\n  JE_CFLAGS_RESTORE()\n  if test \"x$je_cv_syscall\" = \"xyes\" ; then\n    AC_DEFINE([JEMALLOC_USE_SYSCALL], [ ])\n  fi\nfi\n\ndnl Check if the GNU-specific secure_getenv function exists.\nAC_CHECK_FUNC([secure_getenv],\n              [have_secure_getenv=\"1\"],\n              [have_secure_getenv=\"0\"]\n             )\nif test \"x$have_secure_getenv\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_SECURE_GETENV], [ ])\nfi\n\ndnl Check if the GNU-specific sched_getcpu function exists.\nAC_CHECK_FUNC([sched_getcpu],\n              [have_sched_getcpu=\"1\"],\n              [have_sched_getcpu=\"0\"]\n             )\nif test \"x$have_sched_getcpu\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_SCHED_GETCPU], [ ])\nfi\n\ndnl Check if the GNU-specific sched_setaffinity function exists.\nAC_CHECK_FUNC([sched_setaffinity],\n              [have_sched_setaffinity=\"1\"],\n              [have_sched_setaffinity=\"0\"]\n             )\nif test \"x$have_sched_setaffinity\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_SCHED_SETAFFINITY], [ ])\nfi\n\ndnl Check if the Solaris/BSD issetugid function exists.\nAC_CHECK_FUNC([issetugid],\n              [have_issetugid=\"1\"],\n              [have_issetugid=\"0\"]\n             )\nif test \"x$have_issetugid\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_ISSETUGID], [ ])\nfi\n\ndnl Check whether the BSD-specific _malloc_thread_cleanup() exists.  If so, use\ndnl it rather than pthreads TSD cleanup functions to support cleanup during\ndnl thread exit, in order to avoid pthreads library recursion during\ndnl bootstrapping.\nAC_CHECK_FUNC([_malloc_thread_cleanup],\n              [have__malloc_thread_cleanup=\"1\"],\n              [have__malloc_thread_cleanup=\"0\"]\n             )\nif test \"x$have__malloc_thread_cleanup\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_MALLOC_THREAD_CLEANUP], [ ])\n  wrap_syms=\"${wrap_syms} _malloc_thread_cleanup\"\n  force_tls=\"1\"\nfi\n\ndnl Check whether the BSD-specific _pthread_mutex_init_calloc_cb() exists.  If\ndnl so, mutex initialization causes allocation, and we need to implement this\ndnl callback function in order to prevent recursive allocation.\nAC_CHECK_FUNC([_pthread_mutex_init_calloc_cb],\n              [have__pthread_mutex_init_calloc_cb=\"1\"],\n              [have__pthread_mutex_init_calloc_cb=\"0\"]\n             )\nif test \"x$have__pthread_mutex_init_calloc_cb\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_MUTEX_INIT_CB])\n  wrap_syms=\"${wrap_syms} _malloc_prefork _malloc_postfork\"\nfi\n\ndnl Disable lazy locking by default.\nAC_ARG_ENABLE([lazy_lock],\n  [AS_HELP_STRING([--enable-lazy-lock],\n  [Enable lazy locking (only lock when multi-threaded)])],\n[if test \"x$enable_lazy_lock\" = \"xno\" ; then\n  enable_lazy_lock=\"0\"\nelse\n  enable_lazy_lock=\"1\"\nfi\n],\n[enable_lazy_lock=\"\"]\n)\nif test \"x${enable_lazy_lock}\" = \"x\" ; then\n  if test \"x${force_lazy_lock}\" = \"x1\" ; then\n    AC_MSG_RESULT([Forcing lazy-lock to avoid allocator/threading bootstrap issues])\n    enable_lazy_lock=\"1\"\n  else\n    enable_lazy_lock=\"0\"\n  fi\nfi\nif test \"x${enable_lazy_lock}\" = \"x1\" -a \"x${abi}\" = \"xpecoff\" ; then\n  AC_MSG_RESULT([Forcing no lazy-lock because thread creation monitoring is unimplemented])\n  enable_lazy_lock=\"0\"\nfi\nif test \"x$enable_lazy_lock\" = \"x1\" ; then\n  if test \"x$have_dlsym\" = \"x1\" ; then\n    AC_DEFINE([JEMALLOC_LAZY_LOCK], [ ])\n  else\n    AC_MSG_ERROR([Missing dlsym support: lazy-lock cannot be enabled.])\n  fi\nfi\nAC_SUBST([enable_lazy_lock])\n\ndnl Automatically configure TLS.\nif test \"x${force_tls}\" = \"x1\" ; then\n  enable_tls=\"1\"\nelif test \"x${force_tls}\" = \"x0\" ; then\n  enable_tls=\"0\"\nelse\n  enable_tls=\"1\"\nfi\nif test \"x${enable_tls}\" = \"x1\" ; then\nAC_MSG_CHECKING([for TLS])\nAC_COMPILE_IFELSE([AC_LANG_PROGRAM(\n[[\n    __thread int x;\n]], [[\n    x = 42;\n\n    return 0;\n]])],\n              AC_MSG_RESULT([yes]),\n              AC_MSG_RESULT([no])\n              enable_tls=\"0\")\nelse\n  enable_tls=\"0\"\nfi\nAC_SUBST([enable_tls])\nif test \"x${enable_tls}\" = \"x1\" ; then\n  AC_DEFINE_UNQUOTED([JEMALLOC_TLS], [ ])\nfi\n\ndnl ============================================================================\ndnl Check for C11 atomics.\n\nJE_COMPILABLE([C11 atomics], [\n#include <stdint.h>\n#if (__STDC_VERSION__ >= 201112L) && !defined(__STDC_NO_ATOMICS__)\n#include <stdatomic.h>\n#else\n#error Atomics not available\n#endif\n], [\n    uint64_t *p = (uint64_t *)0;\n    uint64_t x = 1;\n    volatile atomic_uint_least64_t *a = (volatile atomic_uint_least64_t *)p;\n    uint64_t r = atomic_fetch_add(a, x) + x;\n    return r == 0;\n], [je_cv_c11_atomics])\nif test \"x${je_cv_c11_atomics}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_C11_ATOMICS])\nfi\n\ndnl ============================================================================\ndnl Check for GCC-style __atomic atomics.\n\nJE_COMPILABLE([GCC __atomic atomics], [\n], [\n    int x = 0;\n    int val = 1;\n    int y = __atomic_fetch_add(&x, val, __ATOMIC_RELAXED);\n    int after_add = x;\n    return after_add == 1;\n], [je_cv_gcc_atomic_atomics])\nif test \"x${je_cv_gcc_atomic_atomics}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_GCC_ATOMIC_ATOMICS])\n\n  dnl check for 8-bit atomic support\n  JE_COMPILABLE([GCC 8-bit __atomic atomics], [\n  ], [\n      unsigned char x = 0;\n      int val = 1;\n      int y = __atomic_fetch_add(&x, val, __ATOMIC_RELAXED);\n      int after_add = (int)x;\n      return after_add == 1;\n  ], [je_cv_gcc_u8_atomic_atomics])\n  if test \"x${je_cv_gcc_u8_atomic_atomics}\" = \"xyes\" ; then\n    AC_DEFINE([JEMALLOC_GCC_U8_ATOMIC_ATOMICS])\n  fi\nfi\n\ndnl ============================================================================\ndnl Check for GCC-style __sync atomics.\n\nJE_COMPILABLE([GCC __sync atomics], [\n], [\n    int x = 0;\n    int before_add = __sync_fetch_and_add(&x, 1);\n    int after_add = x;\n    return (before_add == 0) && (after_add == 1);\n], [je_cv_gcc_sync_atomics])\nif test \"x${je_cv_gcc_sync_atomics}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_GCC_SYNC_ATOMICS])\n\n  dnl check for 8-bit atomic support\n  JE_COMPILABLE([GCC 8-bit __sync atomics], [\n  ], [\n      unsigned char x = 0;\n      int before_add = __sync_fetch_and_add(&x, 1);\n      int after_add = (int)x;\n      return (before_add == 0) && (after_add == 1);\n  ], [je_cv_gcc_u8_sync_atomics])\n  if test \"x${je_cv_gcc_u8_sync_atomics}\" = \"xyes\" ; then\n    AC_DEFINE([JEMALLOC_GCC_U8_SYNC_ATOMICS])\n  fi\nfi\n\ndnl ============================================================================\ndnl Check for atomic(3) operations as provided on Darwin.\ndnl We need this not for the atomic operations (which are provided above), but\ndnl rather for the OS_unfair_lock type it exposes.\n\nJE_COMPILABLE([Darwin OSAtomic*()], [\n#include <libkern/OSAtomic.h>\n#include <inttypes.h>\n], [\n\t{\n\t\tint32_t x32 = 0;\n\t\tvolatile int32_t *x32p = &x32;\n\t\tOSAtomicAdd32(1, x32p);\n\t}\n\t{\n\t\tint64_t x64 = 0;\n\t\tvolatile int64_t *x64p = &x64;\n\t\tOSAtomicAdd64(1, x64p);\n\t}\n], [je_cv_osatomic])\nif test \"x${je_cv_osatomic}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_OSATOMIC], [ ])\nfi\n\ndnl ============================================================================\ndnl Check for madvise(2).\n\nJE_COMPILABLE([madvise(2)], [\n#include <sys/mman.h>\n], [\n\tmadvise((void *)0, 0, 0);\n], [je_cv_madvise])\nif test \"x${je_cv_madvise}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_MADVISE], [ ])\n\n  dnl Check for madvise(..., MADV_FREE).\n  JE_COMPILABLE([madvise(..., MADV_FREE)], [\n#include <sys/mman.h>\n], [\n\tmadvise((void *)0, 0, MADV_FREE);\n], [je_cv_madv_free])\n  if test \"x${je_cv_madv_free}\" = \"xyes\" ; then\n    AC_DEFINE([JEMALLOC_PURGE_MADVISE_FREE], [ ])\n  elif test \"x${je_cv_madvise}\" = \"xyes\" ; then\n    case \"${host_cpu}\" in i686|x86_64)\n        case \"${host}\" in *-*-linux*)\n            AC_DEFINE([JEMALLOC_PURGE_MADVISE_FREE], [ ])\n            AC_DEFINE([JEMALLOC_DEFINE_MADVISE_FREE], [ ])\n\t    ;;\n        esac\n        ;;\n    esac\n  fi\n\n  dnl Check for madvise(..., MADV_DONTNEED).\n  JE_COMPILABLE([madvise(..., MADV_DONTNEED)], [\n#include <sys/mman.h>\n], [\n\tmadvise((void *)0, 0, MADV_DONTNEED);\n], [je_cv_madv_dontneed])\n  if test \"x${je_cv_madv_dontneed}\" = \"xyes\" ; then\n    AC_DEFINE([JEMALLOC_PURGE_MADVISE_DONTNEED], [ ])\n  fi\n\n  dnl Check for madvise(..., MADV_DO[NT]DUMP).\n  JE_COMPILABLE([madvise(..., MADV_DO[[NT]]DUMP)], [\n#include <sys/mman.h>\n], [\n\tmadvise((void *)0, 0, MADV_DONTDUMP);\n\tmadvise((void *)0, 0, MADV_DODUMP);\n], [je_cv_madv_dontdump])\n  if test \"x${je_cv_madv_dontdump}\" = \"xyes\" ; then\n    AC_DEFINE([JEMALLOC_MADVISE_DONTDUMP], [ ])\n  fi\n\n  dnl Check for madvise(..., MADV_[NO]HUGEPAGE).\n  JE_COMPILABLE([madvise(..., MADV_[[NO]]HUGEPAGE)], [\n#include <sys/mman.h>\n], [\n\tmadvise((void *)0, 0, MADV_HUGEPAGE);\n\tmadvise((void *)0, 0, MADV_NOHUGEPAGE);\n], [je_cv_thp])\ncase \"${host_cpu}\" in\n  arm*)\n    ;;\n  *)\n  if test \"x${je_cv_thp}\" = \"xyes\" ; then\n    AC_DEFINE([JEMALLOC_HAVE_MADVISE_HUGE], [ ])\n  fi\n  ;;\nesac\nfi\n\ndnl ============================================================================\ndnl Check for __builtin_clz() and __builtin_clzl().\n\nAC_CACHE_CHECK([for __builtin_clz],\n               [je_cv_builtin_clz],\n               [AC_LINK_IFELSE([AC_LANG_PROGRAM([],\n                                                [\n                                                {\n                                                        unsigned x = 0;\n                                                        int y = __builtin_clz(x);\n                                                }\n                                                {\n                                                        unsigned long x = 0;\n                                                        int y = __builtin_clzl(x);\n                                                }\n                                                ])],\n                               [je_cv_builtin_clz=yes],\n                               [je_cv_builtin_clz=no])])\n\nif test \"x${je_cv_builtin_clz}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_BUILTIN_CLZ], [ ])\nfi\n\ndnl ============================================================================\ndnl Check for os_unfair_lock operations as provided on Darwin.\n\nJE_COMPILABLE([Darwin os_unfair_lock_*()], [\n#include <os/lock.h>\n#include <AvailabilityMacros.h>\n], [\n\t#if MAC_OS_X_VERSION_MIN_REQUIRED < 101200\n\t#error \"os_unfair_lock is not supported\"\n\t#else\n\tos_unfair_lock lock = OS_UNFAIR_LOCK_INIT;\n\tos_unfair_lock_lock(&lock);\n\tos_unfair_lock_unlock(&lock);\n\t#endif\n], [je_cv_os_unfair_lock])\nif test \"x${je_cv_os_unfair_lock}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_OS_UNFAIR_LOCK], [ ])\nfi\n\ndnl ============================================================================\ndnl Darwin-related configuration.\n\nAC_ARG_ENABLE([zone-allocator],\n  [AS_HELP_STRING([--disable-zone-allocator],\n                  [Disable zone allocator for Darwin])],\n[if test \"x$enable_zone_allocator\" = \"xno\" ; then\n  enable_zone_allocator=\"0\"\nelse\n  enable_zone_allocator=\"1\"\nfi\n],\n[if test \"x${abi}\" = \"xmacho\"; then\n  enable_zone_allocator=\"1\"\nfi\n]\n)\nAC_SUBST([enable_zone_allocator])\n\nif test \"x${enable_zone_allocator}\" = \"x1\" ; then\n  if test \"x${abi}\" != \"xmacho\"; then\n    AC_MSG_ERROR([--enable-zone-allocator is only supported on Darwin])\n  fi\n  AC_DEFINE([JEMALLOC_ZONE], [ ])\nfi\n\ndnl ============================================================================\ndnl Use initial-exec TLS by default.\nAC_ARG_ENABLE([initial-exec-tls],\n  [AS_HELP_STRING([--disable-initial-exec-tls],\n                  [Disable the initial-exec tls model])],\n[if test \"x$enable_initial_exec_tls\" = \"xno\" ; then\n  enable_initial_exec_tls=\"0\"\nelse\n  enable_initial_exec_tls=\"1\"\nfi\n],\n[enable_initial_exec_tls=\"1\"]\n)\nAC_SUBST([enable_initial_exec_tls])\n\nif test \"x${je_cv_tls_model}\" = \"xyes\" -a \\\n       \"x${enable_initial_exec_tls}\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_TLS_MODEL],\n            [__attribute__((tls_model(\"initial-exec\")))])\nelse\n  AC_DEFINE([JEMALLOC_TLS_MODEL], [ ])\nfi\n\ndnl ============================================================================\ndnl Enable background threads if possible.\n\nif test \"x${have_pthread}\" = \"x1\" -a \"x${je_cv_os_unfair_lock}\" != \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_BACKGROUND_THREAD])\nfi\n\ndnl ============================================================================\ndnl Check for glibc malloc hooks\n\nJE_COMPILABLE([glibc malloc hook], [\n#include <stddef.h>\n\nextern void (* __free_hook)(void *ptr);\nextern void *(* __malloc_hook)(size_t size);\nextern void *(* __realloc_hook)(void *ptr, size_t size);\n], [\n  void *ptr = 0L;\n  if (__malloc_hook) ptr = __malloc_hook(1);\n  if (__realloc_hook) ptr = __realloc_hook(ptr, 2);\n  if (__free_hook && ptr) __free_hook(ptr);\n], [je_cv_glibc_malloc_hook])\nif test \"x${je_cv_glibc_malloc_hook}\" = \"xyes\" ; then\n  if test \"x${JEMALLOC_PREFIX}\" = \"x\" ; then\n    AC_DEFINE([JEMALLOC_GLIBC_MALLOC_HOOK], [ ])\n    wrap_syms=\"${wrap_syms} __free_hook __malloc_hook __realloc_hook\"\n  fi\nfi\n\nJE_COMPILABLE([glibc memalign hook], [\n#include <stddef.h>\n\nextern void *(* __memalign_hook)(size_t alignment, size_t size);\n], [\n  void *ptr = 0L;\n  if (__memalign_hook) ptr = __memalign_hook(16, 7);\n], [je_cv_glibc_memalign_hook])\nif test \"x${je_cv_glibc_memalign_hook}\" = \"xyes\" ; then\n  if test \"x${JEMALLOC_PREFIX}\" = \"x\" ; then\n    AC_DEFINE([JEMALLOC_GLIBC_MEMALIGN_HOOK], [ ])\n    wrap_syms=\"${wrap_syms} __memalign_hook\"\n  fi\nfi\n\nJE_COMPILABLE([pthreads adaptive mutexes], [\n#include <pthread.h>\n], [\n  pthread_mutexattr_t attr;\n  pthread_mutexattr_init(&attr);\n  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);\n  pthread_mutexattr_destroy(&attr);\n], [je_cv_pthread_mutex_adaptive_np])\nif test \"x${je_cv_pthread_mutex_adaptive_np}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_PTHREAD_MUTEX_ADAPTIVE_NP], [ ])\nfi\n\nJE_CFLAGS_SAVE()\nJE_CFLAGS_ADD([-D_GNU_SOURCE])\nJE_CFLAGS_ADD([-Werror])\nJE_CFLAGS_ADD([-herror_on_warning])\nJE_COMPILABLE([strerror_r returns char with gnu source], [\n#include <errno.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n], [\n  char *buffer = (char *) malloc(100);\n  char *error = strerror_r(EINVAL, buffer, 100);\n  printf(\"%s\\n\", error);\n], [je_cv_strerror_r_returns_char_with_gnu_source])\nJE_CFLAGS_RESTORE()\nif test \"x${je_cv_strerror_r_returns_char_with_gnu_source}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_STRERROR_R_RETURNS_CHAR_WITH_GNU_SOURCE], [ ])\nfi\n\ndnl ============================================================================\ndnl Check for typedefs, structures, and compiler characteristics.\nAC_HEADER_STDBOOL\n\ndnl ============================================================================\ndnl Define commands that generate output files.\n\nAC_CONFIG_COMMANDS([include/jemalloc/internal/public_symbols.txt], [\n  f=\"${objroot}include/jemalloc/internal/public_symbols.txt\"\n  mkdir -p \"${objroot}include/jemalloc/internal\"\n  cp /dev/null \"${f}\"\n  for nm in `echo ${mangling_map} |tr ',' ' '` ; do\n    n=`echo ${nm} |tr ':' ' ' |awk '{print $[]1}'`\n    m=`echo ${nm} |tr ':' ' ' |awk '{print $[]2}'`\n    echo \"${n}:${m}\" >> \"${f}\"\n    dnl Remove name from public_syms so that it isn't redefined later.\n    public_syms=`for sym in ${public_syms}; do echo \"${sym}\"; done |grep -v \"^${n}\\$\" |tr '\\n' ' '`\n  done\n  for sym in ${public_syms} ; do\n    n=\"${sym}\"\n    m=\"${JEMALLOC_PREFIX}${sym}\"\n    echo \"${n}:${m}\" >> \"${f}\"\n  done\n], [\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n  mangling_map=\"${mangling_map}\"\n  public_syms=\"${public_syms}\"\n  JEMALLOC_PREFIX=\"${JEMALLOC_PREFIX}\"\n])\nAC_CONFIG_COMMANDS([include/jemalloc/internal/private_symbols.awk], [\n  f=\"${objroot}include/jemalloc/internal/private_symbols.awk\"\n  mkdir -p \"${objroot}include/jemalloc/internal\"\n  export_syms=`for sym in ${public_syms}; do echo \"${JEMALLOC_PREFIX}${sym}\"; done; for sym in ${wrap_syms}; do echo \"${sym}\"; done;`\n  \"${srcdir}/include/jemalloc/internal/private_symbols.sh\" \"${SYM_PREFIX}\" ${export_syms} > \"${objroot}include/jemalloc/internal/private_symbols.awk\"\n], [\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n  public_syms=\"${public_syms}\"\n  wrap_syms=\"${wrap_syms}\"\n  SYM_PREFIX=\"${SYM_PREFIX}\"\n  JEMALLOC_PREFIX=\"${JEMALLOC_PREFIX}\"\n])\nAC_CONFIG_COMMANDS([include/jemalloc/internal/private_symbols_jet.awk], [\n  f=\"${objroot}include/jemalloc/internal/private_symbols_jet.awk\"\n  mkdir -p \"${objroot}include/jemalloc/internal\"\n  export_syms=`for sym in ${public_syms}; do echo \"jet_${sym}\"; done; for sym in ${wrap_syms}; do echo \"${sym}\"; done;`\n  \"${srcdir}/include/jemalloc/internal/private_symbols.sh\" \"${SYM_PREFIX}\" ${export_syms} > \"${objroot}include/jemalloc/internal/private_symbols_jet.awk\"\n], [\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n  public_syms=\"${public_syms}\"\n  wrap_syms=\"${wrap_syms}\"\n  SYM_PREFIX=\"${SYM_PREFIX}\"\n])\nAC_CONFIG_COMMANDS([include/jemalloc/internal/public_namespace.h], [\n  mkdir -p \"${objroot}include/jemalloc/internal\"\n  \"${srcdir}/include/jemalloc/internal/public_namespace.sh\" \"${objroot}include/jemalloc/internal/public_symbols.txt\" > \"${objroot}include/jemalloc/internal/public_namespace.h\"\n], [\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n])\nAC_CONFIG_COMMANDS([include/jemalloc/internal/public_unnamespace.h], [\n  mkdir -p \"${objroot}include/jemalloc/internal\"\n  \"${srcdir}/include/jemalloc/internal/public_unnamespace.sh\" \"${objroot}include/jemalloc/internal/public_symbols.txt\" > \"${objroot}include/jemalloc/internal/public_unnamespace.h\"\n], [\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n])\nAC_CONFIG_COMMANDS([include/jemalloc/jemalloc_protos_jet.h], [\n  mkdir -p \"${objroot}include/jemalloc\"\n  cat \"${srcdir}/include/jemalloc/jemalloc_protos.h.in\" | sed -e 's/@je_@/jet_/g' > \"${objroot}include/jemalloc/jemalloc_protos_jet.h\"\n], [\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n])\nAC_CONFIG_COMMANDS([include/jemalloc/jemalloc_rename.h], [\n  mkdir -p \"${objroot}include/jemalloc\"\n  \"${srcdir}/include/jemalloc/jemalloc_rename.sh\" \"${objroot}include/jemalloc/internal/public_symbols.txt\" > \"${objroot}include/jemalloc/jemalloc_rename.h\"\n], [\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n])\nAC_CONFIG_COMMANDS([include/jemalloc/jemalloc_mangle.h], [\n  mkdir -p \"${objroot}include/jemalloc\"\n  \"${srcdir}/include/jemalloc/jemalloc_mangle.sh\" \"${objroot}include/jemalloc/internal/public_symbols.txt\" je_ > \"${objroot}include/jemalloc/jemalloc_mangle.h\"\n], [\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n])\nAC_CONFIG_COMMANDS([include/jemalloc/jemalloc_mangle_jet.h], [\n  mkdir -p \"${objroot}include/jemalloc\"\n  \"${srcdir}/include/jemalloc/jemalloc_mangle.sh\" \"${objroot}include/jemalloc/internal/public_symbols.txt\" jet_ > \"${objroot}include/jemalloc/jemalloc_mangle_jet.h\"\n], [\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n])\nAC_CONFIG_COMMANDS([include/jemalloc/jemalloc.h], [\n  mkdir -p \"${objroot}include/jemalloc\"\n  \"${srcdir}/include/jemalloc/jemalloc.sh\" \"${objroot}\" > \"${objroot}include/jemalloc/jemalloc${install_suffix}.h\"\n], [\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n  install_suffix=\"${install_suffix}\"\n])\n\ndnl Process .in files.\nAC_SUBST([cfghdrs_in])\nAC_SUBST([cfghdrs_out])\nAC_CONFIG_HEADERS([$cfghdrs_tup])\n\ndnl ============================================================================\ndnl Generate outputs.\n\nAC_CONFIG_FILES([$cfgoutputs_tup config.stamp bin/jemalloc-config bin/jemalloc.sh bin/jeprof])\nAC_SUBST([cfgoutputs_in])\nAC_SUBST([cfgoutputs_out])\nAC_OUTPUT\n\ndnl ============================================================================\ndnl Print out the results of configuration.\nAC_MSG_RESULT([===============================================================================])\nAC_MSG_RESULT([jemalloc version   : ${jemalloc_version}])\nAC_MSG_RESULT([library revision   : ${rev}])\nAC_MSG_RESULT([])\nAC_MSG_RESULT([CONFIG             : ${CONFIG}])\nAC_MSG_RESULT([CC                 : ${CC}])\nAC_MSG_RESULT([CONFIGURE_CFLAGS   : ${CONFIGURE_CFLAGS}])\nAC_MSG_RESULT([SPECIFIED_CFLAGS   : ${SPECIFIED_CFLAGS}])\nAC_MSG_RESULT([EXTRA_CFLAGS       : ${EXTRA_CFLAGS}])\nAC_MSG_RESULT([CPPFLAGS           : ${CPPFLAGS}])\nAC_MSG_RESULT([CXX                : ${CXX}])\nAC_MSG_RESULT([CONFIGURE_CXXFLAGS : ${CONFIGURE_CXXFLAGS}])\nAC_MSG_RESULT([SPECIFIED_CXXFLAGS : ${SPECIFIED_CXXFLAGS}])\nAC_MSG_RESULT([EXTRA_CXXFLAGS     : ${EXTRA_CXXFLAGS}])\nAC_MSG_RESULT([LDFLAGS            : ${LDFLAGS}])\nAC_MSG_RESULT([EXTRA_LDFLAGS      : ${EXTRA_LDFLAGS}])\nAC_MSG_RESULT([DSO_LDFLAGS        : ${DSO_LDFLAGS}])\nAC_MSG_RESULT([LIBS               : ${LIBS}])\nAC_MSG_RESULT([RPATH_EXTRA        : ${RPATH_EXTRA}])\nAC_MSG_RESULT([])\nAC_MSG_RESULT([XSLTPROC           : ${XSLTPROC}])\nAC_MSG_RESULT([XSLROOT            : ${XSLROOT}])\nAC_MSG_RESULT([])\nAC_MSG_RESULT([PREFIX             : ${PREFIX}])\nAC_MSG_RESULT([BINDIR             : ${BINDIR}])\nAC_MSG_RESULT([DATADIR            : ${DATADIR}])\nAC_MSG_RESULT([INCLUDEDIR         : ${INCLUDEDIR}])\nAC_MSG_RESULT([LIBDIR             : ${LIBDIR}])\nAC_MSG_RESULT([MANDIR             : ${MANDIR}])\nAC_MSG_RESULT([])\nAC_MSG_RESULT([srcroot            : ${srcroot}])\nAC_MSG_RESULT([abs_srcroot        : ${abs_srcroot}])\nAC_MSG_RESULT([objroot            : ${objroot}])\nAC_MSG_RESULT([abs_objroot        : ${abs_objroot}])\nAC_MSG_RESULT([])\nAC_MSG_RESULT([JEMALLOC_PREFIX    : ${JEMALLOC_PREFIX}])\nAC_MSG_RESULT([JEMALLOC_PRIVATE_NAMESPACE])\nAC_MSG_RESULT([                   : ${JEMALLOC_PRIVATE_NAMESPACE}])\nAC_MSG_RESULT([install_suffix     : ${install_suffix}])\nAC_MSG_RESULT([malloc_conf        : ${config_malloc_conf}])\nAC_MSG_RESULT([documentation      : ${enable_doc}])\nAC_MSG_RESULT([shared libs        : ${enable_shared}])\nAC_MSG_RESULT([static libs        : ${enable_static}])\nAC_MSG_RESULT([autogen            : ${enable_autogen}])\nAC_MSG_RESULT([debug              : ${enable_debug}])\nAC_MSG_RESULT([stats              : ${enable_stats}])\nAC_MSG_RESULT([experimetal_smallocx : ${enable_experimental_smallocx}])\nAC_MSG_RESULT([prof               : ${enable_prof}])\nAC_MSG_RESULT([prof-libunwind     : ${enable_prof_libunwind}])\nAC_MSG_RESULT([prof-libgcc        : ${enable_prof_libgcc}])\nAC_MSG_RESULT([prof-gcc           : ${enable_prof_gcc}])\nAC_MSG_RESULT([fill               : ${enable_fill}])\nAC_MSG_RESULT([utrace             : ${enable_utrace}])\nAC_MSG_RESULT([xmalloc            : ${enable_xmalloc}])\nAC_MSG_RESULT([log                : ${enable_log}])\nAC_MSG_RESULT([lazy_lock          : ${enable_lazy_lock}])\nAC_MSG_RESULT([cache-oblivious    : ${enable_cache_oblivious}])\nAC_MSG_RESULT([cxx                : ${enable_cxx}])\nAC_MSG_RESULT([===============================================================================])\n"
  },
  {
    "path": "deps/jemalloc/doc/html.xsl.in",
    "content": "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">\n  <xsl:import href=\"@XSLROOT@/html/docbook.xsl\"/>\n  <xsl:import href=\"@abs_srcroot@doc/stylesheet.xsl\"/>\n  <xsl:output method=\"xml\" encoding=\"utf-8\"/>\n</xsl:stylesheet>\n"
  },
  {
    "path": "deps/jemalloc/doc/jemalloc.xml.in",
    "content": "<?xml version='1.0' encoding='UTF-8'?>\n<?xml-stylesheet type=\"text/xsl\"\n        href=\"http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl\"?>\n<!DOCTYPE refentry PUBLIC \"-//OASIS//DTD DocBook XML V4.4//EN\"\n        \"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd\" [\n]>\n\n<refentry>\n  <refentryinfo>\n    <title>User Manual</title>\n    <productname>jemalloc</productname>\n    <releaseinfo role=\"version\">@jemalloc_version@</releaseinfo>\n    <authorgroup>\n      <author>\n        <firstname>Jason</firstname>\n        <surname>Evans</surname>\n        <personblurb>Author</personblurb>\n      </author>\n    </authorgroup>\n  </refentryinfo>\n  <refmeta>\n    <refentrytitle>JEMALLOC</refentrytitle>\n    <manvolnum>3</manvolnum>\n  </refmeta>\n  <refnamediv>\n    <refdescriptor>jemalloc</refdescriptor>\n    <refname>jemalloc</refname>\n    <!-- Each refname causes a man page file to be created.  Only if this were\n         the system malloc(3) implementation would these files be appropriate.\n    <refname>malloc</refname>\n    <refname>calloc</refname>\n    <refname>posix_memalign</refname>\n    <refname>aligned_alloc</refname>\n    <refname>realloc</refname>\n    <refname>free</refname>\n    <refname>mallocx</refname>\n    <refname>rallocx</refname>\n    <refname>xallocx</refname>\n    <refname>sallocx</refname>\n    <refname>dallocx</refname>\n    <refname>sdallocx</refname>\n    <refname>nallocx</refname>\n    <refname>mallctl</refname>\n    <refname>mallctlnametomib</refname>\n    <refname>mallctlbymib</refname>\n    <refname>malloc_stats_print</refname>\n    <refname>malloc_usable_size</refname>\n    -->\n    <refpurpose>general purpose memory allocation functions</refpurpose>\n  </refnamediv>\n  <refsect1 id=\"library\">\n    <title>LIBRARY</title>\n    <para>This manual describes jemalloc @jemalloc_version@.  More information\n    can be found at the <ulink\n    url=\"http://jemalloc.net/\">jemalloc website</ulink>.</para>\n  </refsect1>\n  <refsynopsisdiv>\n    <title>SYNOPSIS</title>\n    <funcsynopsis>\n      <funcsynopsisinfo>#include &lt;<filename class=\"headerfile\">jemalloc/jemalloc.h</filename>&gt;</funcsynopsisinfo>\n      <refsect2>\n        <title>Standard API</title>\n        <funcprototype>\n          <funcdef>void *<function>malloc</function></funcdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>void *<function>calloc</function></funcdef>\n          <paramdef>size_t <parameter>number</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>int <function>posix_memalign</function></funcdef>\n          <paramdef>void **<parameter>ptr</parameter></paramdef>\n          <paramdef>size_t <parameter>alignment</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>void *<function>aligned_alloc</function></funcdef>\n          <paramdef>size_t <parameter>alignment</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>void *<function>realloc</function></funcdef>\n          <paramdef>void *<parameter>ptr</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>void <function>free</function></funcdef>\n          <paramdef>void *<parameter>ptr</parameter></paramdef>\n        </funcprototype>\n      </refsect2>\n      <refsect2>\n        <title>Non-standard API</title>\n        <funcprototype>\n          <funcdef>void *<function>mallocx</function></funcdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n          <paramdef>int <parameter>flags</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>void *<function>rallocx</function></funcdef>\n          <paramdef>void *<parameter>ptr</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n          <paramdef>int <parameter>flags</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>size_t <function>xallocx</function></funcdef>\n          <paramdef>void *<parameter>ptr</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n          <paramdef>size_t <parameter>extra</parameter></paramdef>\n          <paramdef>int <parameter>flags</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>size_t <function>sallocx</function></funcdef>\n          <paramdef>void *<parameter>ptr</parameter></paramdef>\n          <paramdef>int <parameter>flags</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>void <function>dallocx</function></funcdef>\n          <paramdef>void *<parameter>ptr</parameter></paramdef>\n          <paramdef>int <parameter>flags</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>void <function>sdallocx</function></funcdef>\n          <paramdef>void *<parameter>ptr</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n          <paramdef>int <parameter>flags</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>size_t <function>nallocx</function></funcdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n          <paramdef>int <parameter>flags</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>int <function>mallctl</function></funcdef>\n          <paramdef>const char *<parameter>name</parameter></paramdef>\n          <paramdef>void *<parameter>oldp</parameter></paramdef>\n          <paramdef>size_t *<parameter>oldlenp</parameter></paramdef>\n          <paramdef>void *<parameter>newp</parameter></paramdef>\n          <paramdef>size_t <parameter>newlen</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>int <function>mallctlnametomib</function></funcdef>\n          <paramdef>const char *<parameter>name</parameter></paramdef>\n          <paramdef>size_t *<parameter>mibp</parameter></paramdef>\n          <paramdef>size_t *<parameter>miblenp</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>int <function>mallctlbymib</function></funcdef>\n          <paramdef>const size_t *<parameter>mib</parameter></paramdef>\n          <paramdef>size_t <parameter>miblen</parameter></paramdef>\n          <paramdef>void *<parameter>oldp</parameter></paramdef>\n          <paramdef>size_t *<parameter>oldlenp</parameter></paramdef>\n          <paramdef>void *<parameter>newp</parameter></paramdef>\n          <paramdef>size_t <parameter>newlen</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>void <function>malloc_stats_print</function></funcdef>\n          <paramdef>void <parameter>(*write_cb)</parameter>\n            <funcparams>void *, const char *</funcparams>\n          </paramdef>\n          <paramdef>void *<parameter>cbopaque</parameter></paramdef>\n          <paramdef>const char *<parameter>opts</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>size_t <function>malloc_usable_size</function></funcdef>\n          <paramdef>const void *<parameter>ptr</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>void <function>(*malloc_message)</function></funcdef>\n          <paramdef>void *<parameter>cbopaque</parameter></paramdef>\n          <paramdef>const char *<parameter>s</parameter></paramdef>\n        </funcprototype>\n        <para><type>const char *</type><varname>malloc_conf</varname>;</para>\n      </refsect2>\n    </funcsynopsis>\n  </refsynopsisdiv>\n  <refsect1 id=\"description\">\n    <title>DESCRIPTION</title>\n    <refsect2>\n      <title>Standard API</title>\n\n      <para>The <function>malloc()</function> function allocates\n      <parameter>size</parameter> bytes of uninitialized memory.  The allocated\n      space is suitably aligned (after possible pointer coercion) for storage\n      of any type of object.</para>\n\n      <para>The <function>calloc()</function> function allocates\n      space for <parameter>number</parameter> objects, each\n      <parameter>size</parameter> bytes in length.  The result is identical to\n      calling <function>malloc()</function> with an argument of\n      <parameter>number</parameter> * <parameter>size</parameter>, with the\n      exception that the allocated memory is explicitly initialized to zero\n      bytes.</para>\n\n      <para>The <function>posix_memalign()</function> function\n      allocates <parameter>size</parameter> bytes of memory such that the\n      allocation's base address is a multiple of\n      <parameter>alignment</parameter>, and returns the allocation in the value\n      pointed to by <parameter>ptr</parameter>.  The requested\n      <parameter>alignment</parameter> must be a power of 2 at least as large as\n      <code language=\"C\">sizeof(<type>void *</type>)</code>.</para>\n\n      <para>The <function>aligned_alloc()</function> function\n      allocates <parameter>size</parameter> bytes of memory such that the\n      allocation's base address is a multiple of\n      <parameter>alignment</parameter>.  The requested\n      <parameter>alignment</parameter> must be a power of 2.  Behavior is\n      undefined if <parameter>size</parameter> is not an integral multiple of\n      <parameter>alignment</parameter>.</para>\n\n      <para>The <function>realloc()</function> function changes the\n      size of the previously allocated memory referenced by\n      <parameter>ptr</parameter> to <parameter>size</parameter> bytes.  The\n      contents of the memory are unchanged up to the lesser of the new and old\n      sizes.  If the new size is larger, the contents of the newly allocated\n      portion of the memory are undefined.  Upon success, the memory referenced\n      by <parameter>ptr</parameter> is freed and a pointer to the newly\n      allocated memory is returned.  Note that\n      <function>realloc()</function> may move the memory allocation,\n      resulting in a different return value than <parameter>ptr</parameter>.\n      If <parameter>ptr</parameter> is <constant>NULL</constant>, the\n      <function>realloc()</function> function behaves identically to\n      <function>malloc()</function> for the specified size.</para>\n\n      <para>The <function>free()</function> function causes the\n      allocated memory referenced by <parameter>ptr</parameter> to be made\n      available for future allocations.  If <parameter>ptr</parameter> is\n      <constant>NULL</constant>, no action occurs.</para>\n    </refsect2>\n    <refsect2>\n      <title>Non-standard API</title>\n      <para>The <function>mallocx()</function>,\n      <function>rallocx()</function>,\n      <function>xallocx()</function>,\n      <function>sallocx()</function>,\n      <function>dallocx()</function>,\n      <function>sdallocx()</function>, and\n      <function>nallocx()</function> functions all have a\n      <parameter>flags</parameter> argument that can be used to specify\n      options.  The functions only check the options that are contextually\n      relevant.  Use bitwise or (<code language=\"C\">|</code>) operations to\n      specify one or more of the following:\n        <variablelist>\n          <varlistentry id=\"MALLOCX_LG_ALIGN\">\n            <term><constant>MALLOCX_LG_ALIGN(<parameter>la</parameter>)\n            </constant></term>\n\n            <listitem><para>Align the memory allocation to start at an address\n            that is a multiple of <code language=\"C\">(1 &lt;&lt;\n            <parameter>la</parameter>)</code>.  This macro does not validate\n            that <parameter>la</parameter> is within the valid\n            range.</para></listitem>\n          </varlistentry>\n          <varlistentry id=\"MALLOCX_ALIGN\">\n            <term><constant>MALLOCX_ALIGN(<parameter>a</parameter>)\n            </constant></term>\n\n            <listitem><para>Align the memory allocation to start at an address\n            that is a multiple of <parameter>a</parameter>, where\n            <parameter>a</parameter> is a power of two.  This macro does not\n            validate that <parameter>a</parameter> is a power of 2.\n            </para></listitem>\n          </varlistentry>\n          <varlistentry id=\"MALLOCX_ZERO\">\n            <term><constant>MALLOCX_ZERO</constant></term>\n\n            <listitem><para>Initialize newly allocated memory to contain zero\n            bytes.  In the growing reallocation case, the real size prior to\n            reallocation defines the boundary between untouched bytes and those\n            that are initialized to contain zero bytes.  If this macro is\n            absent, newly allocated memory is uninitialized.</para></listitem>\n          </varlistentry>\n          <varlistentry id=\"MALLOCX_TCACHE\">\n            <term><constant>MALLOCX_TCACHE(<parameter>tc</parameter>)\n            </constant></term>\n\n            <listitem><para>Use the thread-specific cache (tcache) specified by\n            the identifier <parameter>tc</parameter>, which must have been\n            acquired via the <link\n            linkend=\"tcache.create\"><mallctl>tcache.create</mallctl></link>\n            mallctl.  This macro does not validate that\n            <parameter>tc</parameter> specifies a valid\n            identifier.</para></listitem>\n          </varlistentry>\n          <varlistentry id=\"MALLOC_TCACHE_NONE\">\n            <term><constant>MALLOCX_TCACHE_NONE</constant></term>\n\n            <listitem><para>Do not use a thread-specific cache (tcache).  Unless\n            <constant>MALLOCX_TCACHE(<parameter>tc</parameter>)</constant> or\n            <constant>MALLOCX_TCACHE_NONE</constant> is specified, an\n            automatically managed tcache will be used under many circumstances.\n            This macro cannot be used in the same <parameter>flags</parameter>\n            argument as\n            <constant>MALLOCX_TCACHE(<parameter>tc</parameter>)</constant>.</para></listitem>\n          </varlistentry>\n          <varlistentry id=\"MALLOCX_ARENA\">\n            <term><constant>MALLOCX_ARENA(<parameter>a</parameter>)\n            </constant></term>\n\n            <listitem><para>Use the arena specified by the index\n            <parameter>a</parameter>.  This macro has no effect for regions that\n            were allocated via an arena other than the one specified.  This\n            macro does not validate that <parameter>a</parameter> specifies an\n            arena index in the valid range.</para></listitem>\n          </varlistentry>\n        </variablelist>\n      </para>\n\n      <para>The <function>mallocx()</function> function allocates at\n      least <parameter>size</parameter> bytes of memory, and returns a pointer\n      to the base address of the allocation.  Behavior is undefined if\n      <parameter>size</parameter> is <constant>0</constant>.</para>\n\n      <para>The <function>rallocx()</function> function resizes the\n      allocation at <parameter>ptr</parameter> to be at least\n      <parameter>size</parameter> bytes, and returns a pointer to the base\n      address of the resulting allocation, which may or may not have moved from\n      its original location.  Behavior is undefined if\n      <parameter>size</parameter> is <constant>0</constant>.</para>\n\n      <para>The <function>xallocx()</function> function resizes the\n      allocation at <parameter>ptr</parameter> in place to be at least\n      <parameter>size</parameter> bytes, and returns the real size of the\n      allocation.  If <parameter>extra</parameter> is non-zero, an attempt is\n      made to resize the allocation to be at least <code\n      language=\"C\">(<parameter>size</parameter> +\n      <parameter>extra</parameter>)</code> bytes, though inability to allocate\n      the extra byte(s) will not by itself result in failure to resize.\n      Behavior is undefined if <parameter>size</parameter> is\n      <constant>0</constant>, or if <code\n      language=\"C\">(<parameter>size</parameter> + <parameter>extra</parameter>\n      &gt; <constant>SIZE_T_MAX</constant>)</code>.</para>\n\n      <para>The <function>sallocx()</function> function returns the\n      real size of the allocation at <parameter>ptr</parameter>.</para>\n\n      <para>The <function>dallocx()</function> function causes the\n      memory referenced by <parameter>ptr</parameter> to be made available for\n      future allocations.</para>\n\n      <para>The <function>sdallocx()</function> function is an\n      extension of <function>dallocx()</function> with a\n      <parameter>size</parameter> parameter to allow the caller to pass in the\n      allocation size as an optimization.  The minimum valid input size is the\n      original requested size of the allocation, and the maximum valid input\n      size is the corresponding value returned by\n      <function>nallocx()</function> or\n      <function>sallocx()</function>.</para>\n\n      <para>The <function>nallocx()</function> function allocates no\n      memory, but it performs the same size computation as the\n      <function>mallocx()</function> function, and returns the real\n      size of the allocation that would result from the equivalent\n      <function>mallocx()</function> function call, or\n      <constant>0</constant> if the inputs exceed the maximum supported size\n      class and/or alignment.  Behavior is undefined if\n      <parameter>size</parameter> is <constant>0</constant>.</para>\n\n      <para>The <function>mallctl()</function> function provides a\n      general interface for introspecting the memory allocator, as well as\n      setting modifiable parameters and triggering actions.  The\n      period-separated <parameter>name</parameter> argument specifies a\n      location in a tree-structured namespace; see the <xref\n      linkend=\"mallctl_namespace\" xrefstyle=\"template:%t\"/> section for\n      documentation on the tree contents.  To read a value, pass a pointer via\n      <parameter>oldp</parameter> to adequate space to contain the value, and a\n      pointer to its length via <parameter>oldlenp</parameter>; otherwise pass\n      <constant>NULL</constant> and <constant>NULL</constant>.  Similarly, to\n      write a value, pass a pointer to the value via\n      <parameter>newp</parameter>, and its length via\n      <parameter>newlen</parameter>; otherwise pass <constant>NULL</constant>\n      and <constant>0</constant>.</para>\n\n      <para>The <function>mallctlnametomib()</function> function\n      provides a way to avoid repeated name lookups for applications that\n      repeatedly query the same portion of the namespace, by translating a name\n      to a <quote>Management Information Base</quote> (MIB) that can be passed\n      repeatedly to <function>mallctlbymib()</function>.  Upon\n      successful return from <function>mallctlnametomib()</function>,\n      <parameter>mibp</parameter> contains an array of\n      <parameter>*miblenp</parameter> integers, where\n      <parameter>*miblenp</parameter> is the lesser of the number of components\n      in <parameter>name</parameter> and the input value of\n      <parameter>*miblenp</parameter>.  Thus it is possible to pass a\n      <parameter>*miblenp</parameter> that is smaller than the number of\n      period-separated name components, which results in a partial MIB that can\n      be used as the basis for constructing a complete MIB.  For name\n      components that are integers (e.g. the 2 in\n      <link\n      linkend=\"arenas.bin.i.size\"><mallctl>arenas.bin.2.size</mallctl></link>),\n      the corresponding MIB component will always be that integer.  Therefore,\n      it is legitimate to construct code like the following: <programlisting\n      language=\"C\"><![CDATA[\nunsigned nbins, i;\nsize_t mib[4];\nsize_t len, miblen;\n\nlen = sizeof(nbins);\nmallctl(\"arenas.nbins\", &nbins, &len, NULL, 0);\n\nmiblen = 4;\nmallctlnametomib(\"arenas.bin.0.size\", mib, &miblen);\nfor (i = 0; i < nbins; i++) {\n\tsize_t bin_size;\n\n\tmib[2] = i;\n\tlen = sizeof(bin_size);\n\tmallctlbymib(mib, miblen, (void *)&bin_size, &len, NULL, 0);\n\t/* Do something with bin_size... */\n}]]></programlisting></para>\n\n      <varlistentry id=\"malloc_stats_print_opts\">\n      </varlistentry>\n      <para>The <function>malloc_stats_print()</function> function writes\n      summary statistics via the <parameter>write_cb</parameter> callback\n      function pointer and <parameter>cbopaque</parameter> data passed to\n      <parameter>write_cb</parameter>, or <function>malloc_message()</function>\n      if <parameter>write_cb</parameter> is <constant>NULL</constant>.  The\n      statistics are presented in human-readable form unless <quote>J</quote> is\n      specified as a character within the <parameter>opts</parameter> string, in\n      which case the statistics are presented in <ulink\n      url=\"http://www.json.org/\">JSON format</ulink>.  This function can be\n      called repeatedly.  General information that never changes during\n      execution can be omitted by specifying <quote>g</quote> as a character\n      within the <parameter>opts</parameter> string.  Note that\n      <function>malloc_stats_print()</function> uses the\n      <function>mallctl*()</function> functions internally, so inconsistent\n      statistics can be reported if multiple threads use these functions\n      simultaneously.  If <option>--enable-stats</option> is specified during\n      configuration, <quote>m</quote>, <quote>d</quote>, and <quote>a</quote>\n      can be specified to omit merged arena, destroyed merged arena, and per\n      arena statistics, respectively; <quote>b</quote> and <quote>l</quote> can\n      be specified to omit per size class statistics for bins and large objects,\n      respectively; <quote>x</quote> can be specified to omit all mutex\n      statistics; <quote>e</quote> can be used to omit extent statistics.\n      Unrecognized characters are silently ignored.  Note that thread caching\n      may prevent some statistics from being completely up to date, since extra\n      locking would be required to merge counters that track thread cache\n      operations.</para>\n\n      <para>The <function>malloc_usable_size()</function> function\n      returns the usable size of the allocation pointed to by\n      <parameter>ptr</parameter>.  The return value may be larger than the size\n      that was requested during allocation.  The\n      <function>malloc_usable_size()</function> function is not a\n      mechanism for in-place <function>realloc()</function>; rather\n      it is provided solely as a tool for introspection purposes.  Any\n      discrepancy between the requested allocation size and the size reported\n      by <function>malloc_usable_size()</function> should not be\n      depended on, since such behavior is entirely implementation-dependent.\n      </para>\n    </refsect2>\n  </refsect1>\n  <refsect1 id=\"tuning\">\n    <title>TUNING</title>\n    <para>Once, when the first call is made to one of the memory allocation\n    routines, the allocator initializes its internals based in part on various\n    options that can be specified at compile- or run-time.</para>\n\n    <para>The string specified via <option>--with-malloc-conf</option>, the\n    string pointed to by the global variable <varname>malloc_conf</varname>, the\n    <quote>name</quote> of the file referenced by the symbolic link named\n    <filename class=\"symlink\">/etc/malloc.conf</filename>, and the value of the\n    environment variable <envar>MALLOC_CONF</envar>, will be interpreted, in\n    that order, from left to right as options.  Note that\n    <varname>malloc_conf</varname> may be read before\n    <function>main()</function> is entered, so the declaration of\n    <varname>malloc_conf</varname> should specify an initializer that contains\n    the final value to be read by jemalloc.  <option>--with-malloc-conf</option>\n    and <varname>malloc_conf</varname> are compile-time mechanisms, whereas\n    <filename class=\"symlink\">/etc/malloc.conf</filename> and\n    <envar>MALLOC_CONF</envar> can be safely set any time prior to program\n    invocation.</para>\n\n    <para>An options string is a comma-separated list of option:value pairs.\n    There is one key corresponding to each <link\n    linkend=\"opt.abort\"><mallctl>opt.*</mallctl></link> mallctl (see the <xref\n    linkend=\"mallctl_namespace\" xrefstyle=\"template:%t\"/> section for options\n    documentation).  For example, <literal>abort:true,narenas:1</literal> sets\n    the <link linkend=\"opt.abort\"><mallctl>opt.abort</mallctl></link> and <link\n    linkend=\"opt.narenas\"><mallctl>opt.narenas</mallctl></link> options.  Some\n    options have boolean values (true/false), others have integer values (base\n    8, 10, or 16, depending on prefix), and yet others have raw string\n    values.</para>\n  </refsect1>\n  <refsect1 id=\"implementation_notes\">\n    <title>IMPLEMENTATION NOTES</title>\n    <para>Traditionally, allocators have used\n    <citerefentry><refentrytitle>sbrk</refentrytitle>\n    <manvolnum>2</manvolnum></citerefentry> to obtain memory, which is\n    suboptimal for several reasons, including race conditions, increased\n    fragmentation, and artificial limitations on maximum usable memory.  If\n    <citerefentry><refentrytitle>sbrk</refentrytitle>\n    <manvolnum>2</manvolnum></citerefentry> is supported by the operating\n    system, this allocator uses both\n    <citerefentry><refentrytitle>mmap</refentrytitle>\n    <manvolnum>2</manvolnum></citerefentry> and\n    <citerefentry><refentrytitle>sbrk</refentrytitle>\n    <manvolnum>2</manvolnum></citerefentry>, in that order of preference;\n    otherwise only <citerefentry><refentrytitle>mmap</refentrytitle>\n    <manvolnum>2</manvolnum></citerefentry> is used.</para>\n\n    <para>This allocator uses multiple arenas in order to reduce lock\n    contention for threaded programs on multi-processor systems.  This works\n    well with regard to threading scalability, but incurs some costs.  There is\n    a small fixed per-arena overhead, and additionally, arenas manage memory\n    completely independently of each other, which means a small fixed increase\n    in overall memory fragmentation.  These overheads are not generally an\n    issue, given the number of arenas normally used.  Note that using\n    substantially more arenas than the default is not likely to improve\n    performance, mainly due to reduced cache performance.  However, it may make\n    sense to reduce the number of arenas if an application does not make much\n    use of the allocation functions.</para>\n\n    <para>In addition to multiple arenas, this allocator supports\n    thread-specific caching, in order to make it possible to completely avoid\n    synchronization for most allocation requests.  Such caching allows very fast\n    allocation in the common case, but it increases memory usage and\n    fragmentation, since a bounded number of objects can remain allocated in\n    each thread cache.</para>\n\n    <para>Memory is conceptually broken into extents.  Extents are always\n    aligned to multiples of the page size.  This alignment makes it possible to\n    find metadata for user objects quickly.  User objects are broken into two\n    categories according to size: small and large.  Contiguous small objects\n    comprise a slab, which resides within a single extent, whereas large objects\n    each have their own extents backing them.</para>\n\n    <para>Small objects are managed in groups by slabs.  Each slab maintains\n    a bitmap to track which regions are in use.  Allocation requests that are no\n    more than half the quantum (8 or 16, depending on architecture) are rounded\n    up to the nearest power of two that is at least <code\n    language=\"C\">sizeof(<type>double</type>)</code>.  All other object size\n    classes are multiples of the quantum, spaced such that there are four size\n    classes for each doubling in size, which limits internal fragmentation to\n    approximately 20% for all but the smallest size classes.  Small size classes\n    are smaller than four times the page size, and large size classes extend\n    from four times the page size up to the largest size class that does not\n    exceed <constant>PTRDIFF_MAX</constant>.</para>\n\n    <para>Allocations are packed tightly together, which can be an issue for\n    multi-threaded applications.  If you need to assure that allocations do not\n    suffer from cacheline sharing, round your allocation requests up to the\n    nearest multiple of the cacheline size, or specify cacheline alignment when\n    allocating.</para>\n\n    <para>The <function>realloc()</function>,\n    <function>rallocx()</function>, and\n    <function>xallocx()</function> functions may resize allocations\n    without moving them under limited circumstances.  Unlike the\n    <function>*allocx()</function> API, the standard API does not\n    officially round up the usable size of an allocation to the nearest size\n    class, so technically it is necessary to call\n    <function>realloc()</function> to grow e.g. a 9-byte allocation to\n    16 bytes, or shrink a 16-byte allocation to 9 bytes.  Growth and shrinkage\n    trivially succeeds in place as long as the pre-size and post-size both round\n    up to the same size class.  No other API guarantees are made regarding\n    in-place resizing, but the current implementation also tries to resize large\n    allocations in place, as long as the pre-size and post-size are both large.\n    For shrinkage to succeed, the extent allocator must support splitting (see\n    <link\n    linkend=\"arena.i.extent_hooks\"><mallctl>arena.&lt;i&gt;.extent_hooks</mallctl></link>).\n    Growth only succeeds if the trailing memory is currently available, and the\n    extent allocator supports merging.</para>\n\n    <para>Assuming 4 KiB pages and a 16-byte quantum on a 64-bit system, the\n    size classes in each category are as shown in <xref linkend=\"size_classes\"\n    xrefstyle=\"template:Table %n\"/>.</para>\n\n    <table xml:id=\"size_classes\" frame=\"all\">\n      <title>Size classes</title>\n      <tgroup cols=\"3\" colsep=\"1\" rowsep=\"1\">\n      <colspec colname=\"c1\" align=\"left\"/>\n      <colspec colname=\"c2\" align=\"right\"/>\n      <colspec colname=\"c3\" align=\"left\"/>\n      <thead>\n        <row>\n          <entry>Category</entry>\n          <entry>Spacing</entry>\n          <entry>Size</entry>\n        </row>\n      </thead>\n      <tbody>\n        <row>\n          <entry morerows=\"8\">Small</entry>\n          <entry>lg</entry>\n          <entry>[8]</entry>\n        </row>\n        <row>\n          <entry>16</entry>\n          <entry>[16, 32, 48, 64, 80, 96, 112, 128]</entry>\n        </row>\n        <row>\n          <entry>32</entry>\n          <entry>[160, 192, 224, 256]</entry>\n        </row>\n        <row>\n          <entry>64</entry>\n          <entry>[320, 384, 448, 512]</entry>\n        </row>\n        <row>\n          <entry>128</entry>\n          <entry>[640, 768, 896, 1024]</entry>\n        </row>\n        <row>\n          <entry>256</entry>\n          <entry>[1280, 1536, 1792, 2048]</entry>\n        </row>\n        <row>\n          <entry>512</entry>\n          <entry>[2560, 3072, 3584, 4096]</entry>\n        </row>\n        <row>\n          <entry>1 KiB</entry>\n          <entry>[5 KiB, 6 KiB, 7 KiB, 8 KiB]</entry>\n        </row>\n        <row>\n          <entry>2 KiB</entry>\n          <entry>[10 KiB, 12 KiB, 14 KiB]</entry>\n        </row>\n        <row>\n          <entry morerows=\"15\">Large</entry>\n          <entry>2 KiB</entry>\n          <entry>[16 KiB]</entry>\n        </row>\n        <row>\n          <entry>4 KiB</entry>\n          <entry>[20 KiB, 24 KiB, 28 KiB, 32 KiB]</entry>\n        </row>\n        <row>\n          <entry>8 KiB</entry>\n          <entry>[40 KiB, 48 KiB, 54 KiB, 64 KiB]</entry>\n        </row>\n        <row>\n          <entry>16 KiB</entry>\n          <entry>[80 KiB, 96 KiB, 112 KiB, 128 KiB]</entry>\n        </row>\n        <row>\n          <entry>32 KiB</entry>\n          <entry>[160 KiB, 192 KiB, 224 KiB, 256 KiB]</entry>\n        </row>\n        <row>\n          <entry>64 KiB</entry>\n          <entry>[320 KiB, 384 KiB, 448 KiB, 512 KiB]</entry>\n        </row>\n        <row>\n          <entry>128 KiB</entry>\n          <entry>[640 KiB, 768 KiB, 896 KiB, 1 MiB]</entry>\n        </row>\n        <row>\n          <entry>256 KiB</entry>\n          <entry>[1280 KiB, 1536 KiB, 1792 KiB, 2 MiB]</entry>\n        </row>\n        <row>\n          <entry>512 KiB</entry>\n          <entry>[2560 KiB, 3 MiB, 3584 KiB, 4 MiB]</entry>\n        </row>\n        <row>\n          <entry>1 MiB</entry>\n          <entry>[5 MiB, 6 MiB, 7 MiB, 8 MiB]</entry>\n        </row>\n        <row>\n          <entry>2 MiB</entry>\n          <entry>[10 MiB, 12 MiB, 14 MiB, 16 MiB]</entry>\n        </row>\n        <row>\n          <entry>4 MiB</entry>\n          <entry>[20 MiB, 24 MiB, 28 MiB, 32 MiB]</entry>\n        </row>\n        <row>\n          <entry>8 MiB</entry>\n          <entry>[40 MiB, 48 MiB, 56 MiB, 64 MiB]</entry>\n        </row>\n        <row>\n          <entry>...</entry>\n          <entry>...</entry>\n        </row>\n        <row>\n          <entry>512 PiB</entry>\n          <entry>[2560 PiB, 3 EiB, 3584 PiB, 4 EiB]</entry>\n        </row>\n        <row>\n          <entry>1 EiB</entry>\n          <entry>[5 EiB, 6 EiB, 7 EiB]</entry>\n        </row>\n      </tbody>\n      </tgroup>\n    </table>\n  </refsect1>\n  <refsect1 id=\"mallctl_namespace\">\n    <title>MALLCTL NAMESPACE</title>\n    <para>The following names are defined in the namespace accessible via the\n    <function>mallctl*()</function> functions.  Value types are specified in\n    parentheses, their readable/writable statuses are encoded as\n    <literal>rw</literal>, <literal>r-</literal>, <literal>-w</literal>, or\n    <literal>--</literal>, and required build configuration flags follow, if\n    any.  A name element encoded as <literal>&lt;i&gt;</literal> or\n    <literal>&lt;j&gt;</literal> indicates an integer component, where the\n    integer varies from 0 to some upper value that must be determined via\n    introspection.  In the case of <mallctl>stats.arenas.&lt;i&gt;.*</mallctl>\n    and <mallctl>arena.&lt;i&gt;.{initialized,purge,decay,dss}</mallctl>,\n    <literal>&lt;i&gt;</literal> equal to\n    <constant>MALLCTL_ARENAS_ALL</constant> can be used to operate on all arenas\n    or access the summation of statistics from all arenas; similarly\n    <literal>&lt;i&gt;</literal> equal to\n    <constant>MALLCTL_ARENAS_DESTROYED</constant> can be used to access the\n    summation of statistics from all destroyed arenas.  These constants can be\n    utilized either via <function>mallctlnametomib()</function> followed by\n    <function>mallctlbymib()</function>, or via code such as the following:\n    <programlisting language=\"C\"><![CDATA[\n#define STRINGIFY_HELPER(x) #x\n#define STRINGIFY(x) STRINGIFY_HELPER(x)\n\nmallctl(\"arena.\" STRINGIFY(MALLCTL_ARENAS_ALL) \".decay\",\n    NULL, NULL, NULL, 0);]]></programlisting>\n    Take special note of the <link\n    linkend=\"epoch\"><mallctl>epoch</mallctl></link> mallctl, which controls\n    refreshing of cached dynamic statistics.</para>\n\n    <variablelist>\n      <varlistentry id=\"version\">\n        <term>\n          <mallctl>version</mallctl>\n          (<type>const char *</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Return the jemalloc version string.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"epoch\">\n        <term>\n          <mallctl>epoch</mallctl>\n          (<type>uint64_t</type>)\n          <literal>rw</literal>\n        </term>\n        <listitem><para>If a value is passed in, refresh the data from which\n        the <function>mallctl*()</function> functions report values,\n        and increment the epoch.  Return the current epoch.  This is useful for\n        detecting whether another thread caused a refresh.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"background_thread\">\n        <term>\n          <mallctl>background_thread</mallctl>\n          (<type>bool</type>)\n          <literal>rw</literal>\n        </term>\n        <listitem><para>Enable/disable internal background worker threads.  When\n        set to true, background threads are created on demand (the number of\n        background threads will be no more than the number of CPUs or active\n        arenas).  Threads run periodically, and handle <link\n        linkend=\"arena.i.decay\">purging</link> asynchronously.  When switching\n        off, background threads are terminated synchronously.  Note that after\n        <citerefentry><refentrytitle>fork</refentrytitle><manvolnum>2</manvolnum></citerefentry>\n        function, the state in the child process will be disabled regardless\n        the state in parent process. See <link\n        linkend=\"stats.background_thread.num_threads\"><mallctl>stats.background_thread</mallctl></link>\n        for related stats.  <link\n        linkend=\"opt.background_thread\"><mallctl>opt.background_thread</mallctl></link>\n        can be used to set the default option.  This option is only available on\n        selected pthread-based platforms.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"max_background_threads\">\n        <term>\n          <mallctl>max_background_threads</mallctl>\n          (<type>size_t</type>)\n          <literal>rw</literal>\n        </term>\n        <listitem><para>Maximum number of background worker threads that will\n        be created.  This value is capped at <link\n        linkend=\"opt.max_background_threads\"><mallctl>opt.max_background_threads</mallctl></link> at\n        startup.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"config.cache_oblivious\">\n        <term>\n          <mallctl>config.cache_oblivious</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para><option>--enable-cache-oblivious</option> was specified\n        during build configuration.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"config.debug\">\n        <term>\n          <mallctl>config.debug</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para><option>--enable-debug</option> was specified during\n        build configuration.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"config.fill\">\n        <term>\n          <mallctl>config.fill</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para><option>--enable-fill</option> was specified during\n        build configuration.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"config.lazy_lock\">\n        <term>\n          <mallctl>config.lazy_lock</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para><option>--enable-lazy-lock</option> was specified\n        during build configuration.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"config.malloc_conf\">\n        <term>\n          <mallctl>config.malloc_conf</mallctl>\n          (<type>const char *</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Embedded configure-time-specified run-time options\n        string, empty unless <option>--with-malloc-conf</option> was specified\n        during build configuration.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"config.prof\">\n        <term>\n          <mallctl>config.prof</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para><option>--enable-prof</option> was specified during\n        build configuration.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"config.prof_libgcc\">\n        <term>\n          <mallctl>config.prof_libgcc</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para><option>--disable-prof-libgcc</option> was not\n        specified during build configuration.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"config.prof_libunwind\">\n        <term>\n          <mallctl>config.prof_libunwind</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para><option>--enable-prof-libunwind</option> was specified\n        during build configuration.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"config.stats\">\n        <term>\n          <mallctl>config.stats</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para><option>--enable-stats</option> was specified during\n        build configuration.</para></listitem>\n      </varlistentry>\n\n\n      <varlistentry id=\"config.utrace\">\n        <term>\n          <mallctl>config.utrace</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para><option>--enable-utrace</option> was specified during\n        build configuration.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"config.xmalloc\">\n        <term>\n          <mallctl>config.xmalloc</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para><option>--enable-xmalloc</option> was specified during\n        build configuration.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.abort\">\n        <term>\n          <mallctl>opt.abort</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Abort-on-warning enabled/disabled.  If true, most\n        warnings are fatal.  Note that runtime option warnings are not included\n        (see <link\n        linkend=\"opt.abort_conf\"><mallctl>opt.abort_conf</mallctl></link> for\n        that). The process will call\n        <citerefentry><refentrytitle>abort</refentrytitle>\n        <manvolnum>3</manvolnum></citerefentry> in these cases.  This option is\n        disabled by default unless <option>--enable-debug</option> is\n        specified during configuration, in which case it is enabled by default.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.confirm_conf\">\n        <term>\n          <mallctl>opt.confirm_conf</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n\t<listitem><para>Confirm-runtime-options-when-program-starts\n\tenabled/disabled.  If true, the string specified via\n\t<option>--with-malloc-conf</option>, the string pointed to by the\n\tglobal variable <varname>malloc_conf</varname>, the <quote>name</quote>\n\tof the file referenced by the symbolic link named\n\t<filename class=\"symlink\">/etc/malloc.conf</filename>, and the value of\n\tthe environment variable <envar>MALLOC_CONF</envar>, will be printed in\n\torder.  Then, each option being set will be individually printed.  This\n\toption is disabled by default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.abort_conf\">\n        <term>\n          <mallctl>opt.abort_conf</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Abort-on-invalid-configuration enabled/disabled.  If\n        true, invalid runtime options are fatal.  The process will call\n        <citerefentry><refentrytitle>abort</refentrytitle>\n        <manvolnum>3</manvolnum></citerefentry> in these cases.  This option is\n        disabled by default unless <option>--enable-debug</option> is\n        specified during configuration, in which case it is enabled by default.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.metadata_thp\">\n        <term>\n          <mallctl>opt.metadata_thp</mallctl>\n          (<type>const char *</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Controls whether to allow jemalloc to use transparent\n        huge page (THP) for internal metadata (see <link\n        linkend=\"stats.metadata\">stats.metadata</link>).  <quote>always</quote>\n        allows such usage.  <quote>auto</quote> uses no THP initially, but may\n        begin to do so when metadata usage reaches certain level.  The default\n        is <quote>disabled</quote>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.retain\">\n        <term>\n          <mallctl>opt.retain</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>If true, retain unused virtual memory for later reuse\n        rather than discarding it by calling\n        <citerefentry><refentrytitle>munmap</refentrytitle>\n        <manvolnum>2</manvolnum></citerefentry> or equivalent (see <link\n        linkend=\"stats.retained\">stats.retained</link> for related details).\n        It also makes jemalloc use <citerefentry>\n        <refentrytitle>mmap</refentrytitle><manvolnum>2</manvolnum>\n        </citerefentry> or equivalent in a more greedy way, mapping larger\n        chunks in one go.  This option is disabled by default unless discarding\n        virtual memory is known to trigger platform-specific performance\n        problems, namely 1) for [64-bit] Linux, which has a quirk in its virtual\n        memory allocation algorithm that causes semi-permanent VM map holes\n        under normal jemalloc operation; and 2) for [64-bit] Windows, which\n        disallows split / merged regions with\n        <parameter><constant>MEM_RELEASE</constant></parameter>.  Although the\n        same issues may present on 32-bit platforms as well, retaining virtual\n        memory for 32-bit Linux and Windows is disabled by default due to the\n        practical possibility of address space exhaustion.  </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.dss\">\n        <term>\n          <mallctl>opt.dss</mallctl>\n          (<type>const char *</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>dss (<citerefentry><refentrytitle>sbrk</refentrytitle>\n        <manvolnum>2</manvolnum></citerefentry>) allocation precedence as\n        related to <citerefentry><refentrytitle>mmap</refentrytitle>\n        <manvolnum>2</manvolnum></citerefentry> allocation.  The following\n        settings are supported if\n        <citerefentry><refentrytitle>sbrk</refentrytitle>\n        <manvolnum>2</manvolnum></citerefentry> is supported by the operating\n        system: <quote>disabled</quote>, <quote>primary</quote>, and\n        <quote>secondary</quote>; otherwise only <quote>disabled</quote> is\n        supported.  The default is <quote>secondary</quote> if\n        <citerefentry><refentrytitle>sbrk</refentrytitle>\n        <manvolnum>2</manvolnum></citerefentry> is supported by the operating\n        system; <quote>disabled</quote> otherwise.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.narenas\">\n        <term>\n          <mallctl>opt.narenas</mallctl>\n          (<type>unsigned</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Maximum number of arenas to use for automatic\n        multiplexing of threads and arenas.  The default is four times the\n        number of CPUs, or one if there is a single CPU.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.oversize_threshold\">\n        <term>\n          <mallctl>opt.oversize_threshold</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>The threshold in bytes of which requests are considered\n        oversize.  Allocation requests with greater sizes are fulfilled from a\n        dedicated arena (automatically managed, however not within\n        <literal>narenas</literal>), in order to reduce fragmentation by not\n        mixing huge allocations with small ones.  In addition, the decay API\n        guarantees on the extents greater than the specified threshold may be\n        overridden.  Note that requests with arena index specified via\n        <constant>MALLOCX_ARENA</constant>, or threads associated with explicit\n        arenas will not be considered.  The default threshold is 8MiB.  Values\n        not within large size classes disables this feature.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.percpu_arena\">\n        <term>\n          <mallctl>opt.percpu_arena</mallctl>\n          (<type>const char *</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Per CPU arena mode.  Use the <quote>percpu</quote>\n        setting to enable this feature, which uses number of CPUs to determine\n        number of arenas, and bind threads to arenas dynamically based on the\n        CPU the thread runs on currently.  <quote>phycpu</quote> setting uses\n        one arena per physical CPU, which means the two hyper threads on the\n        same CPU share one arena.  Note that no runtime checking regarding the\n        availability of hyper threading is done at the moment.  When set to\n        <quote>disabled</quote>, narenas and thread to arena association will\n        not be impacted by this option.  The default is <quote>disabled</quote>.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.background_thread\">\n        <term>\n          <mallctl>opt.background_thread</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Internal background worker threads enabled/disabled.\n        Because of potential circular dependencies, enabling background thread\n        using this option may cause crash or deadlock during initialization. For\n        a reliable way to use this feature, see <link\n        linkend=\"background_thread\">background_thread</link> for dynamic control\n        options and details.  This option is disabled by\n        default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.max_background_threads\">\n        <term>\n          <mallctl>opt.max_background_threads</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Maximum number of background threads that will be created\n        if <link linkend=\"background_thread\">background_thread</link> is set.\n        Defaults to number of cpus.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.dirty_decay_ms\">\n        <term>\n          <mallctl>opt.dirty_decay_ms</mallctl>\n          (<type>ssize_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Approximate time in milliseconds from the creation of a\n        set of unused dirty pages until an equivalent set of unused dirty pages\n        is purged (i.e. converted to muzzy via e.g.\n        <function>madvise(<parameter>...</parameter><parameter><constant>MADV_FREE</constant></parameter>)</function>\n        if supported by the operating system, or converted to clean otherwise)\n        and/or reused.  Dirty pages are defined as previously having been\n        potentially written to by the application, and therefore consuming\n        physical memory, yet having no current use.  The pages are incrementally\n        purged according to a sigmoidal decay curve that starts and ends with\n        zero purge rate.  A decay time of 0 causes all unused dirty pages to be\n        purged immediately upon creation.  A decay time of -1 disables purging.\n        The default decay time is 10 seconds.  See <link\n        linkend=\"arenas.dirty_decay_ms\"><mallctl>arenas.dirty_decay_ms</mallctl></link>\n        and <link\n        linkend=\"arena.i.dirty_decay_ms\"><mallctl>arena.&lt;i&gt;.dirty_decay_ms</mallctl></link>\n        for related dynamic control options.  See <link\n        linkend=\"opt.muzzy_decay_ms\"><mallctl>opt.muzzy_decay_ms</mallctl></link>\n        for a description of muzzy pages.for a description of muzzy pages.  Note\n        that when the <link\n        linkend=\"opt.oversize_threshold\"><mallctl>oversize_threshold</mallctl></link>\n        feature is enabled, the arenas reserved for oversize requests may have\n        its own default decay settings.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.muzzy_decay_ms\">\n        <term>\n          <mallctl>opt.muzzy_decay_ms</mallctl>\n          (<type>ssize_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Approximate time in milliseconds from the creation of a\n        set of unused muzzy pages until an equivalent set of unused muzzy pages\n        is purged (i.e. converted to clean) and/or reused.  Muzzy pages are\n        defined as previously having been unused dirty pages that were\n        subsequently purged in a manner that left them subject to the\n        reclamation whims of the operating system (e.g.\n        <function>madvise(<parameter>...</parameter><parameter><constant>MADV_FREE</constant></parameter>)</function>),\n        and therefore in an indeterminate state.  The pages are incrementally\n        purged according to a sigmoidal decay curve that starts and ends with\n        zero purge rate.  A decay time of 0 causes all unused muzzy pages to be\n        purged immediately upon creation.  A decay time of -1 disables purging.\n        The default decay time is 10 seconds.  See <link\n        linkend=\"arenas.muzzy_decay_ms\"><mallctl>arenas.muzzy_decay_ms</mallctl></link>\n        and <link\n        linkend=\"arena.i.muzzy_decay_ms\"><mallctl>arena.&lt;i&gt;.muzzy_decay_ms</mallctl></link>\n        for related dynamic control options.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.lg_extent_max_active_fit\">\n        <term>\n          <mallctl>opt.lg_extent_max_active_fit</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>When reusing dirty extents, this determines the (log\n        base 2 of the) maximum ratio between the size of the active extent\n        selected (to split off from) and the size of the requested allocation.\n        This prevents the splitting of large active extents for smaller\n        allocations, which can reduce fragmentation over the long run\n        (especially for non-active extents).  Lower value may reduce\n        fragmentation, at the cost of extra active extents.  The default value\n        is 6, which gives a maximum ratio of 64 (2^6).</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.stats_print\">\n        <term>\n          <mallctl>opt.stats_print</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Enable/disable statistics printing at exit.  If\n        enabled, the <function>malloc_stats_print()</function>\n        function is called at program exit via an\n        <citerefentry><refentrytitle>atexit</refentrytitle>\n        <manvolnum>3</manvolnum></citerefentry> function.  <link\n        linkend=\"opt.stats_print_opts\"><mallctl>opt.stats_print_opts</mallctl></link>\n        can be combined to specify output options. If\n        <option>--enable-stats</option> is specified during configuration, this\n        has the potential to cause deadlock for a multi-threaded process that\n        exits while one or more threads are executing in the memory allocation\n        functions.  Furthermore, <function>atexit()</function> may\n        allocate memory during application initialization and then deadlock\n        internally when jemalloc in turn calls\n        <function>atexit()</function>, so this option is not\n        universally usable (though the application can register its own\n        <function>atexit()</function> function with equivalent\n        functionality).  Therefore, this option should only be used with care;\n        it is primarily intended as a performance tuning aid during application\n        development.  This option is disabled by default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.stats_print_opts\">\n        <term>\n          <mallctl>opt.stats_print_opts</mallctl>\n          (<type>const char *</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Options (the <parameter>opts</parameter> string) to pass\n        to the <function>malloc_stats_print()</function> at exit (enabled\n        through <link\n        linkend=\"opt.stats_print\"><mallctl>opt.stats_print</mallctl></link>). See\n        available options in <link\n        linkend=\"malloc_stats_print_opts\"><function>malloc_stats_print()</function></link>.\n        Has no effect unless <link\n        linkend=\"opt.stats_print\"><mallctl>opt.stats_print</mallctl></link> is\n        enabled.  The default is <quote></quote>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.junk\">\n        <term>\n          <mallctl>opt.junk</mallctl>\n          (<type>const char *</type>)\n          <literal>r-</literal>\n          [<option>--enable-fill</option>]\n        </term>\n        <listitem><para>Junk filling.  If set to <quote>alloc</quote>, each byte\n        of uninitialized allocated memory will be initialized to\n        <literal>0xa5</literal>.  If set to <quote>free</quote>, all deallocated\n        memory will be initialized to <literal>0x5a</literal>.  If set to\n        <quote>true</quote>, both allocated and deallocated memory will be\n        initialized, and if set to <quote>false</quote>, junk filling be\n        disabled entirely.  This is intended for debugging and will impact\n        performance negatively.  This option is <quote>false</quote> by default\n        unless <option>--enable-debug</option> is specified during\n        configuration, in which case it is <quote>true</quote> by\n        default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.zero\">\n        <term>\n          <mallctl>opt.zero</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n          [<option>--enable-fill</option>]\n        </term>\n        <listitem><para>Zero filling enabled/disabled.  If enabled, each byte\n        of uninitialized allocated memory will be initialized to 0.  Note that\n        this initialization only happens once for each byte, so\n        <function>realloc()</function> and\n        <function>rallocx()</function> calls do not zero memory that\n        was previously allocated.  This is intended for debugging and will\n        impact performance negatively.  This option is disabled by default.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.utrace\">\n        <term>\n          <mallctl>opt.utrace</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n          [<option>--enable-utrace</option>]\n        </term>\n        <listitem><para>Allocation tracing based on\n        <citerefentry><refentrytitle>utrace</refentrytitle>\n        <manvolnum>2</manvolnum></citerefentry> enabled/disabled.  This option\n        is disabled by default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.xmalloc\">\n        <term>\n          <mallctl>opt.xmalloc</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n          [<option>--enable-xmalloc</option>]\n        </term>\n        <listitem><para>Abort-on-out-of-memory enabled/disabled.  If enabled,\n        rather than returning failure for any allocation function, display a\n        diagnostic message on <constant>STDERR_FILENO</constant> and cause the\n        program to drop core (using\n        <citerefentry><refentrytitle>abort</refentrytitle>\n        <manvolnum>3</manvolnum></citerefentry>).  If an application is\n        designed to depend on this behavior, set the option at compile time by\n        including the following in the source code:\n        <programlisting language=\"C\"><![CDATA[\nmalloc_conf = \"xmalloc:true\";]]></programlisting>\n        This option is disabled by default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.tcache\">\n        <term>\n          <mallctl>opt.tcache</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Thread-specific caching (tcache) enabled/disabled.  When\n        there are multiple threads, each thread uses a tcache for objects up to\n        a certain size.  Thread-specific caching allows many allocations to be\n        satisfied without performing any thread synchronization, at the cost of\n        increased memory use.  See the <link\n        linkend=\"opt.lg_tcache_max\"><mallctl>opt.lg_tcache_max</mallctl></link>\n        option for related tuning information.  This option is enabled by\n        default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.lg_tcache_max\">\n        <term>\n          <mallctl>opt.lg_tcache_max</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Maximum size class (log base 2) to cache in the\n        thread-specific cache (tcache).  At a minimum, all small size classes\n        are cached, and at a maximum all large size classes are cached.  The\n        default maximum is 32 KiB (2^15).</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.thp\">\n        <term>\n          <mallctl>opt.thp</mallctl>\n          (<type>const char *</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Transparent hugepage (THP) mode. Settings \"always\",\n        \"never\" and \"default\" are available if THP is supported by the operating\n        system.  The \"always\" setting enables transparent hugepage for all user\n        memory mappings with\n        <parameter><constant>MADV_HUGEPAGE</constant></parameter>; \"never\"\n        ensures no transparent hugepage with\n        <parameter><constant>MADV_NOHUGEPAGE</constant></parameter>; the default\n        setting \"default\" makes no changes.  Note that: this option does not\n        affect THP for jemalloc internal metadata (see <link\n        linkend=\"opt.metadata_thp\"><mallctl>opt.metadata_thp</mallctl></link>);\n        in addition, for arenas with customized <link\n        linkend=\"arena.i.extent_hooks\"><mallctl>extent_hooks</mallctl></link>,\n        this option is bypassed as it is implemented as part of the default\n        extent hooks.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.prof\">\n        <term>\n          <mallctl>opt.prof</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Memory profiling enabled/disabled.  If enabled, profile\n        memory allocation activity.  See the <link\n        linkend=\"opt.prof_active\"><mallctl>opt.prof_active</mallctl></link>\n        option for on-the-fly activation/deactivation.  See the <link\n        linkend=\"opt.lg_prof_sample\"><mallctl>opt.lg_prof_sample</mallctl></link>\n        option for probabilistic sampling control.  See the <link\n        linkend=\"opt.prof_accum\"><mallctl>opt.prof_accum</mallctl></link>\n        option for control of cumulative sample reporting.  See the <link\n        linkend=\"opt.lg_prof_interval\"><mallctl>opt.lg_prof_interval</mallctl></link>\n        option for information on interval-triggered profile dumping, the <link\n        linkend=\"opt.prof_gdump\"><mallctl>opt.prof_gdump</mallctl></link>\n        option for information on high-water-triggered profile dumping, and the\n        <link linkend=\"opt.prof_final\"><mallctl>opt.prof_final</mallctl></link>\n        option for final profile dumping.  Profile output is compatible with\n        the <command>jeprof</command> command, which is based on the\n        <command>pprof</command> that is developed as part of the <ulink\n        url=\"http://code.google.com/p/gperftools/\">gperftools\n        package</ulink>.  See <link linkend=\"heap_profile_format\">HEAP PROFILE\n        FORMAT</link> for heap profile format documentation.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.prof_prefix\">\n        <term>\n          <mallctl>opt.prof_prefix</mallctl>\n          (<type>const char *</type>)\n          <literal>r-</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Filename prefix for profile dumps.  If the prefix is\n        set to the empty string, no automatic dumps will occur; this is\n        primarily useful for disabling the automatic final heap dump (which\n        also disables leak reporting, if enabled).  The default prefix is\n        <filename>jeprof</filename>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.prof_active\">\n        <term>\n          <mallctl>opt.prof_active</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Profiling activated/deactivated.  This is a secondary\n        control mechanism that makes it possible to start the application with\n        profiling enabled (see the <link\n        linkend=\"opt.prof\"><mallctl>opt.prof</mallctl></link> option) but\n        inactive, then toggle profiling at any time during program execution\n        with the <link\n        linkend=\"prof.active\"><mallctl>prof.active</mallctl></link> mallctl.\n        This option is enabled by default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.prof_thread_active_init\">\n        <term>\n          <mallctl>opt.prof_thread_active_init</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Initial setting for <link\n        linkend=\"thread.prof.active\"><mallctl>thread.prof.active</mallctl></link>\n        in newly created threads.  The initial setting for newly created threads\n        can also be changed during execution via the <link\n        linkend=\"prof.thread_active_init\"><mallctl>prof.thread_active_init</mallctl></link>\n        mallctl.  This option is enabled by default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.lg_prof_sample\">\n        <term>\n          <mallctl>opt.lg_prof_sample</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Average interval (log base 2) between allocation\n        samples, as measured in bytes of allocation activity.  Increasing the\n        sampling interval decreases profile fidelity, but also decreases the\n        computational overhead.  The default sample interval is 512 KiB (2^19\n        B).</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.prof_accum\">\n        <term>\n          <mallctl>opt.prof_accum</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Reporting of cumulative object/byte counts in profile\n        dumps enabled/disabled.  If this option is enabled, every unique\n        backtrace must be stored for the duration of execution.  Depending on\n        the application, this can impose a large memory overhead, and the\n        cumulative counts are not always of interest.  This option is disabled\n        by default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.lg_prof_interval\">\n        <term>\n          <mallctl>opt.lg_prof_interval</mallctl>\n          (<type>ssize_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Average interval (log base 2) between memory profile\n        dumps, as measured in bytes of allocation activity.  The actual\n        interval between dumps may be sporadic because decentralized allocation\n        counters are used to avoid synchronization bottlenecks.  Profiles are\n        dumped to files named according to the pattern\n        <filename>&lt;prefix&gt;.&lt;pid&gt;.&lt;seq&gt;.i&lt;iseq&gt;.heap</filename>,\n        where <literal>&lt;prefix&gt;</literal> is controlled by the\n        <link\n        linkend=\"opt.prof_prefix\"><mallctl>opt.prof_prefix</mallctl></link>\n        option.  By default, interval-triggered profile dumping is disabled\n        (encoded as -1).\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.prof_gdump\">\n        <term>\n          <mallctl>opt.prof_gdump</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Set the initial state of <link\n        linkend=\"prof.gdump\"><mallctl>prof.gdump</mallctl></link>, which when\n        enabled triggers a memory profile dump every time the total virtual\n        memory exceeds the previous maximum.  This option is disabled by\n        default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.prof_final\">\n        <term>\n          <mallctl>opt.prof_final</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Use an\n        <citerefentry><refentrytitle>atexit</refentrytitle>\n        <manvolnum>3</manvolnum></citerefentry> function to dump final memory\n        usage to a file named according to the pattern\n        <filename>&lt;prefix&gt;.&lt;pid&gt;.&lt;seq&gt;.f.heap</filename>,\n        where <literal>&lt;prefix&gt;</literal> is controlled by the <link\n        linkend=\"opt.prof_prefix\"><mallctl>opt.prof_prefix</mallctl></link>\n        option.  Note that <function>atexit()</function> may allocate\n        memory during application initialization and then deadlock internally\n        when jemalloc in turn calls <function>atexit()</function>, so\n        this option is not universally usable (though the application can\n        register its own <function>atexit()</function> function with\n        equivalent functionality).  This option is disabled by\n        default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.prof_leak\">\n        <term>\n          <mallctl>opt.prof_leak</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Leak reporting enabled/disabled.  If enabled, use an\n        <citerefentry><refentrytitle>atexit</refentrytitle>\n        <manvolnum>3</manvolnum></citerefentry> function to report memory leaks\n        detected by allocation sampling.  See the\n        <link linkend=\"opt.prof\"><mallctl>opt.prof</mallctl></link> option for\n        information on analyzing heap profile output.  This option is disabled\n        by default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"thread.arena\">\n        <term>\n          <mallctl>thread.arena</mallctl>\n          (<type>unsigned</type>)\n          <literal>rw</literal>\n        </term>\n        <listitem><para>Get or set the arena associated with the calling\n        thread.  If the specified arena was not initialized beforehand (see the\n        <link\n        linkend=\"arena.i.initialized\"><mallctl>arena.i.initialized</mallctl></link>\n        mallctl), it will be automatically initialized as a side effect of\n        calling this interface.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"thread.allocated\">\n        <term>\n          <mallctl>thread.allocated</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Get the total number of bytes ever allocated by the\n        calling thread.  This counter has the potential to wrap around; it is\n        up to the application to appropriately interpret the counter in such\n        cases.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"thread.allocatedp\">\n        <term>\n          <mallctl>thread.allocatedp</mallctl>\n          (<type>uint64_t *</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Get a pointer to the the value that is returned by the\n        <link\n        linkend=\"thread.allocated\"><mallctl>thread.allocated</mallctl></link>\n        mallctl.  This is useful for avoiding the overhead of repeated\n        <function>mallctl*()</function> calls.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"thread.deallocated\">\n        <term>\n          <mallctl>thread.deallocated</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Get the total number of bytes ever deallocated by the\n        calling thread.  This counter has the potential to wrap around; it is\n        up to the application to appropriately interpret the counter in such\n        cases.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"thread.deallocatedp\">\n        <term>\n          <mallctl>thread.deallocatedp</mallctl>\n          (<type>uint64_t *</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Get a pointer to the the value that is returned by the\n        <link\n        linkend=\"thread.deallocated\"><mallctl>thread.deallocated</mallctl></link>\n        mallctl.  This is useful for avoiding the overhead of repeated\n        <function>mallctl*()</function> calls.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"thread.tcache.enabled\">\n        <term>\n          <mallctl>thread.tcache.enabled</mallctl>\n          (<type>bool</type>)\n          <literal>rw</literal>\n        </term>\n        <listitem><para>Enable/disable calling thread's tcache.  The tcache is\n        implicitly flushed as a side effect of becoming\n        disabled (see <link\n        linkend=\"thread.tcache.flush\"><mallctl>thread.tcache.flush</mallctl></link>).\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"thread.tcache.flush\">\n        <term>\n          <mallctl>thread.tcache.flush</mallctl>\n          (<type>void</type>)\n          <literal>--</literal>\n        </term>\n        <listitem><para>Flush calling thread's thread-specific cache (tcache).\n        This interface releases all cached objects and internal data structures\n        associated with the calling thread's tcache.  Ordinarily, this interface\n        need not be called, since automatic periodic incremental garbage\n        collection occurs, and the thread cache is automatically discarded when\n        a thread exits.  However, garbage collection is triggered by allocation\n        activity, so it is possible for a thread that stops\n        allocating/deallocating to retain its cache indefinitely, in which case\n        the developer may find manual flushing useful.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"thread.prof.name\">\n        <term>\n          <mallctl>thread.prof.name</mallctl>\n          (<type>const char *</type>)\n          <literal>r-</literal> or\n          <literal>-w</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Get/set the descriptive name associated with the calling\n        thread in memory profile dumps.  An internal copy of the name string is\n        created, so the input string need not be maintained after this interface\n        completes execution.  The output string of this interface should be\n        copied for non-ephemeral uses, because multiple implementation details\n        can cause asynchronous string deallocation.  Furthermore, each\n        invocation of this interface can only read or write; simultaneous\n        read/write is not supported due to string lifetime limitations.  The\n        name string must be nil-terminated and comprised only of characters in\n        the sets recognized\n        by <citerefentry><refentrytitle>isgraph</refentrytitle>\n        <manvolnum>3</manvolnum></citerefentry> and\n        <citerefentry><refentrytitle>isblank</refentrytitle>\n        <manvolnum>3</manvolnum></citerefentry>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"thread.prof.active\">\n        <term>\n          <mallctl>thread.prof.active</mallctl>\n          (<type>bool</type>)\n          <literal>rw</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Control whether sampling is currently active for the\n        calling thread.  This is an activation mechanism in addition to <link\n        linkend=\"prof.active\"><mallctl>prof.active</mallctl></link>; both must\n        be active for the calling thread to sample.  This flag is enabled by\n        default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"tcache.create\">\n        <term>\n          <mallctl>tcache.create</mallctl>\n          (<type>unsigned</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Create an explicit thread-specific cache (tcache) and\n        return an identifier that can be passed to the <link\n        linkend=\"MALLOCX_TCACHE\"><constant>MALLOCX_TCACHE(<parameter>tc</parameter>)</constant></link>\n        macro to explicitly use the specified cache rather than the\n        automatically managed one that is used by default.  Each explicit cache\n        can be used by only one thread at a time; the application must assure\n        that this constraint holds.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"tcache.flush\">\n        <term>\n          <mallctl>tcache.flush</mallctl>\n          (<type>unsigned</type>)\n          <literal>-w</literal>\n        </term>\n        <listitem><para>Flush the specified thread-specific cache (tcache).  The\n        same considerations apply to this interface as to <link\n        linkend=\"thread.tcache.flush\"><mallctl>thread.tcache.flush</mallctl></link>,\n        except that the tcache will never be automatically discarded.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"tcache.destroy\">\n        <term>\n          <mallctl>tcache.destroy</mallctl>\n          (<type>unsigned</type>)\n          <literal>-w</literal>\n        </term>\n        <listitem><para>Flush the specified thread-specific cache (tcache) and\n        make the identifier available for use during a future tcache creation.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arena.i.initialized\">\n        <term>\n          <mallctl>arena.&lt;i&gt;.initialized</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Get whether the specified arena's statistics are\n        initialized (i.e. the arena was initialized prior to the current epoch).\n        This interface can also be nominally used to query whether the merged\n        statistics corresponding to <constant>MALLCTL_ARENAS_ALL</constant> are\n        initialized (always true).</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arena.i.decay\">\n        <term>\n          <mallctl>arena.&lt;i&gt;.decay</mallctl>\n          (<type>void</type>)\n          <literal>--</literal>\n        </term>\n        <listitem><para>Trigger decay-based purging of unused dirty/muzzy pages\n        for arena &lt;i&gt;, or for all arenas if &lt;i&gt; equals\n        <constant>MALLCTL_ARENAS_ALL</constant>.  The proportion of unused\n        dirty/muzzy pages to be purged depends on the current time; see <link\n        linkend=\"opt.dirty_decay_ms\"><mallctl>opt.dirty_decay_ms</mallctl></link>\n        and <link\n        linkend=\"opt.muzzy_decay_ms\"><mallctl>opt.muzy_decay_ms</mallctl></link>\n        for details.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arena.i.purge\">\n        <term>\n          <mallctl>arena.&lt;i&gt;.purge</mallctl>\n          (<type>void</type>)\n          <literal>--</literal>\n        </term>\n        <listitem><para>Purge all unused dirty pages for arena &lt;i&gt;, or for\n        all arenas if &lt;i&gt; equals <constant>MALLCTL_ARENAS_ALL</constant>.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arena.i.reset\">\n        <term>\n          <mallctl>arena.&lt;i&gt;.reset</mallctl>\n          (<type>void</type>)\n          <literal>--</literal>\n        </term>\n        <listitem><para>Discard all of the arena's extant allocations.  This\n        interface can only be used with arenas explicitly created via <link\n        linkend=\"arenas.create\"><mallctl>arenas.create</mallctl></link>.  None\n        of the arena's discarded/cached allocations may accessed afterward.  As\n        part of this requirement, all thread caches which were used to\n        allocate/deallocate in conjunction with the arena must be flushed\n        beforehand.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arena.i.destroy\">\n        <term>\n          <mallctl>arena.&lt;i&gt;.destroy</mallctl>\n          (<type>void</type>)\n          <literal>--</literal>\n        </term>\n        <listitem><para>Destroy the arena.  Discard all of the arena's extant\n        allocations using the same mechanism as for <link\n        linkend=\"arena.i.reset\"><mallctl>arena.&lt;i&gt;.reset</mallctl></link>\n        (with all the same constraints and side effects), merge the arena stats\n        into those accessible at arena index\n        <constant>MALLCTL_ARENAS_DESTROYED</constant>, and then completely\n        discard all metadata associated with the arena.  Future calls to <link\n        linkend=\"arenas.create\"><mallctl>arenas.create</mallctl></link> may\n        recycle the arena index.  Destruction will fail if any threads are\n        currently associated with the arena as a result of calls to <link\n        linkend=\"thread.arena\"><mallctl>thread.arena</mallctl></link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arena.i.dss\">\n        <term>\n          <mallctl>arena.&lt;i&gt;.dss</mallctl>\n          (<type>const char *</type>)\n          <literal>rw</literal>\n        </term>\n        <listitem><para>Set the precedence of dss allocation as related to mmap\n        allocation for arena &lt;i&gt;, or for all arenas if &lt;i&gt; equals\n        <constant>MALLCTL_ARENAS_ALL</constant>.  See <link\n        linkend=\"opt.dss\"><mallctl>opt.dss</mallctl></link> for supported\n        settings.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arena.i.dirty_decay_ms\">\n        <term>\n          <mallctl>arena.&lt;i&gt;.dirty_decay_ms</mallctl>\n          (<type>ssize_t</type>)\n          <literal>rw</literal>\n        </term>\n        <listitem><para>Current per-arena approximate time in milliseconds from\n        the creation of a set of unused dirty pages until an equivalent set of\n        unused dirty pages is purged and/or reused.  Each time this interface is\n        set, all currently unused dirty pages are considered to have fully\n        decayed, which causes immediate purging of all unused dirty pages unless\n        the decay time is set to -1 (i.e. purging disabled).  See <link\n        linkend=\"opt.dirty_decay_ms\"><mallctl>opt.dirty_decay_ms</mallctl></link>\n        for additional information.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arena.i.muzzy_decay_ms\">\n        <term>\n          <mallctl>arena.&lt;i&gt;.muzzy_decay_ms</mallctl>\n          (<type>ssize_t</type>)\n          <literal>rw</literal>\n        </term>\n        <listitem><para>Current per-arena approximate time in milliseconds from\n        the creation of a set of unused muzzy pages until an equivalent set of\n        unused muzzy pages is purged and/or reused.  Each time this interface is\n        set, all currently unused muzzy pages are considered to have fully\n        decayed, which causes immediate purging of all unused muzzy pages unless\n        the decay time is set to -1 (i.e. purging disabled).  See <link\n        linkend=\"opt.muzzy_decay_ms\"><mallctl>opt.muzzy_decay_ms</mallctl></link>\n        for additional information.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arena.i.retain_grow_limit\">\n        <term>\n          <mallctl>arena.&lt;i&gt;.retain_grow_limit</mallctl>\n          (<type>size_t</type>)\n          <literal>rw</literal>\n        </term>\n        <listitem><para>Maximum size to grow retained region (only relevant when\n        <link linkend=\"opt.retain\"><mallctl>opt.retain</mallctl></link> is\n        enabled).  This controls the maximum increment to expand virtual memory,\n        or allocation through <link\n        linkend=\"arena.i.extent_hooks\"><mallctl>arena.&lt;i&gt;extent_hooks</mallctl></link>.\n        In particular, if customized extent hooks reserve physical memory\n        (e.g. 1G huge pages), this is useful to control the allocation hook's\n        input size.  The default is no limit.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arena.i.extent_hooks\">\n        <term>\n          <mallctl>arena.&lt;i&gt;.extent_hooks</mallctl>\n          (<type>extent_hooks_t *</type>)\n          <literal>rw</literal>\n        </term>\n        <listitem><para>Get or set the extent management hook functions for\n        arena &lt;i&gt;.  The functions must be capable of operating on all\n        extant extents associated with arena &lt;i&gt;, usually by passing\n        unknown extents to the replaced functions.  In practice, it is feasible\n        to control allocation for arenas explicitly created via <link\n        linkend=\"arenas.create\"><mallctl>arenas.create</mallctl></link> such\n        that all extents originate from an application-supplied extent allocator\n        (by specifying the custom extent hook functions during arena creation).\n        However, the API guarantees for the automatically created arenas may be\n        relaxed -- hooks set there may be called in a \"best effort\" fashion; in\n        addition there may be extents created prior to the application having an\n        opportunity to take over extent allocation.</para>\n\n        <programlisting language=\"C\"><![CDATA[\ntypedef extent_hooks_s extent_hooks_t;\nstruct extent_hooks_s {\n\textent_alloc_t\t\t*alloc;\n\textent_dalloc_t\t\t*dalloc;\n\textent_destroy_t\t*destroy;\n\textent_commit_t\t\t*commit;\n\textent_decommit_t\t*decommit;\n\textent_purge_t\t\t*purge_lazy;\n\textent_purge_t\t\t*purge_forced;\n\textent_split_t\t\t*split;\n\textent_merge_t\t\t*merge;\n};]]></programlisting>\n        <para>The <type>extent_hooks_t</type> structure comprises function\n        pointers which are described individually below.  jemalloc uses these\n        functions to manage extent lifetime, which starts off with allocation of\n        mapped committed memory, in the simplest case followed by deallocation.\n        However, there are performance and platform reasons to retain extents\n        for later reuse.  Cleanup attempts cascade from deallocation to decommit\n        to forced purging to lazy purging, which gives the extent management\n        functions opportunities to reject the most permanent cleanup operations\n        in favor of less permanent (and often less costly) operations.  All\n        operations except allocation can be universally opted out of by setting\n        the hook pointers to <constant>NULL</constant>, or selectively opted out\n        of by returning failure.  Note that once the extent hook is set, the\n        structure is accessed directly by the associated arenas, so it must\n        remain valid for the entire lifetime of the arenas.</para>\n\n        <funcsynopsis><funcprototype>\n          <funcdef>typedef void *<function>(extent_alloc_t)</function></funcdef>\n          <paramdef>extent_hooks_t *<parameter>extent_hooks</parameter></paramdef>\n          <paramdef>void *<parameter>new_addr</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n          <paramdef>size_t <parameter>alignment</parameter></paramdef>\n          <paramdef>bool *<parameter>zero</parameter></paramdef>\n          <paramdef>bool *<parameter>commit</parameter></paramdef>\n          <paramdef>unsigned <parameter>arena_ind</parameter></paramdef>\n        </funcprototype></funcsynopsis>\n        <literallayout></literallayout>\n        <para>An extent allocation function conforms to the\n        <type>extent_alloc_t</type> type and upon success returns a pointer to\n        <parameter>size</parameter> bytes of mapped memory on behalf of arena\n        <parameter>arena_ind</parameter> such that the extent's base address is\n        a multiple of <parameter>alignment</parameter>, as well as setting\n        <parameter>*zero</parameter> to indicate whether the extent is zeroed\n        and <parameter>*commit</parameter> to indicate whether the extent is\n        committed.  Upon error the function returns <constant>NULL</constant>\n        and leaves <parameter>*zero</parameter> and\n        <parameter>*commit</parameter> unmodified.  The\n        <parameter>size</parameter> parameter is always a multiple of the page\n        size.  The <parameter>alignment</parameter> parameter is always a power\n        of two at least as large as the page size.  Zeroing is mandatory if\n        <parameter>*zero</parameter> is true upon function entry.  Committing is\n        mandatory if <parameter>*commit</parameter> is true upon function entry.\n        If <parameter>new_addr</parameter> is not <constant>NULL</constant>, the\n        returned pointer must be <parameter>new_addr</parameter> on success or\n        <constant>NULL</constant> on error.  Committed memory may be committed\n        in absolute terms as on a system that does not overcommit, or in\n        implicit terms as on a system that overcommits and satisfies physical\n        memory needs on demand via soft page faults.  Note that replacing the\n        default extent allocation function makes the arena's <link\n        linkend=\"arena.i.dss\"><mallctl>arena.&lt;i&gt;.dss</mallctl></link>\n        setting irrelevant.</para>\n\n        <funcsynopsis><funcprototype>\n          <funcdef>typedef bool <function>(extent_dalloc_t)</function></funcdef>\n          <paramdef>extent_hooks_t *<parameter>extent_hooks</parameter></paramdef>\n          <paramdef>void *<parameter>addr</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n          <paramdef>bool <parameter>committed</parameter></paramdef>\n          <paramdef>unsigned <parameter>arena_ind</parameter></paramdef>\n        </funcprototype></funcsynopsis>\n        <literallayout></literallayout>\n        <para>\n        An extent deallocation function conforms to the\n        <type>extent_dalloc_t</type> type and deallocates an extent at given\n        <parameter>addr</parameter> and <parameter>size</parameter> with\n        <parameter>committed</parameter>/decommited memory as indicated, on\n        behalf of arena <parameter>arena_ind</parameter>, returning false upon\n        success.  If the function returns true, this indicates opt-out from\n        deallocation; the virtual memory mapping associated with the extent\n        remains mapped, in the same commit state, and available for future use,\n        in which case it will be automatically retained for later reuse.</para>\n\n        <funcsynopsis><funcprototype>\n          <funcdef>typedef void <function>(extent_destroy_t)</function></funcdef>\n          <paramdef>extent_hooks_t *<parameter>extent_hooks</parameter></paramdef>\n          <paramdef>void *<parameter>addr</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n          <paramdef>bool <parameter>committed</parameter></paramdef>\n          <paramdef>unsigned <parameter>arena_ind</parameter></paramdef>\n        </funcprototype></funcsynopsis>\n        <literallayout></literallayout>\n        <para>\n        An extent destruction function conforms to the\n        <type>extent_destroy_t</type> type and unconditionally destroys an\n        extent at given <parameter>addr</parameter> and\n        <parameter>size</parameter> with\n        <parameter>committed</parameter>/decommited memory as indicated, on\n        behalf of arena <parameter>arena_ind</parameter>.  This function may be\n        called to destroy retained extents during arena destruction (see <link\n        linkend=\"arena.i.destroy\"><mallctl>arena.&lt;i&gt;.destroy</mallctl></link>).</para>\n\n        <funcsynopsis><funcprototype>\n          <funcdef>typedef bool <function>(extent_commit_t)</function></funcdef>\n          <paramdef>extent_hooks_t *<parameter>extent_hooks</parameter></paramdef>\n          <paramdef>void *<parameter>addr</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n          <paramdef>size_t <parameter>offset</parameter></paramdef>\n          <paramdef>size_t <parameter>length</parameter></paramdef>\n          <paramdef>unsigned <parameter>arena_ind</parameter></paramdef>\n        </funcprototype></funcsynopsis>\n        <literallayout></literallayout>\n        <para>An extent commit function conforms to the\n        <type>extent_commit_t</type> type and commits zeroed physical memory to\n        back pages within an extent at given <parameter>addr</parameter> and\n        <parameter>size</parameter> at <parameter>offset</parameter> bytes,\n        extending for <parameter>length</parameter> on behalf of arena\n        <parameter>arena_ind</parameter>, returning false upon success.\n        Committed memory may be committed in absolute terms as on a system that\n        does not overcommit, or in implicit terms as on a system that\n        overcommits and satisfies physical memory needs on demand via soft page\n        faults. If the function returns true, this indicates insufficient\n        physical memory to satisfy the request.</para>\n\n        <funcsynopsis><funcprototype>\n          <funcdef>typedef bool <function>(extent_decommit_t)</function></funcdef>\n          <paramdef>extent_hooks_t *<parameter>extent_hooks</parameter></paramdef>\n          <paramdef>void *<parameter>addr</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n          <paramdef>size_t <parameter>offset</parameter></paramdef>\n          <paramdef>size_t <parameter>length</parameter></paramdef>\n          <paramdef>unsigned <parameter>arena_ind</parameter></paramdef>\n        </funcprototype></funcsynopsis>\n        <literallayout></literallayout>\n        <para>An extent decommit function conforms to the\n        <type>extent_decommit_t</type> type and decommits any physical memory\n        that is backing pages within an extent at given\n        <parameter>addr</parameter> and <parameter>size</parameter> at\n        <parameter>offset</parameter> bytes, extending for\n        <parameter>length</parameter> on behalf of arena\n        <parameter>arena_ind</parameter>, returning false upon success, in which\n        case the pages will be committed via the extent commit function before\n        being reused.  If the function returns true, this indicates opt-out from\n        decommit; the memory remains committed and available for future use, in\n        which case it will be automatically retained for later reuse.</para>\n\n        <funcsynopsis><funcprototype>\n          <funcdef>typedef bool <function>(extent_purge_t)</function></funcdef>\n          <paramdef>extent_hooks_t *<parameter>extent_hooks</parameter></paramdef>\n          <paramdef>void *<parameter>addr</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n          <paramdef>size_t <parameter>offset</parameter></paramdef>\n          <paramdef>size_t <parameter>length</parameter></paramdef>\n          <paramdef>unsigned <parameter>arena_ind</parameter></paramdef>\n        </funcprototype></funcsynopsis>\n        <literallayout></literallayout>\n        <para>An extent purge function conforms to the\n        <type>extent_purge_t</type> type and discards physical pages\n        within the virtual memory mapping associated with an extent at given\n        <parameter>addr</parameter> and <parameter>size</parameter> at\n        <parameter>offset</parameter> bytes, extending for\n        <parameter>length</parameter> on behalf of arena\n        <parameter>arena_ind</parameter>.  A lazy extent purge function (e.g.\n        implemented via\n        <function>madvise(<parameter>...</parameter><parameter><constant>MADV_FREE</constant></parameter>)</function>)\n        can delay purging indefinitely and leave the pages within the purged\n        virtual memory range in an indeterminite state, whereas a forced extent\n        purge function immediately purges, and the pages within the virtual\n        memory range will be zero-filled the next time they are accessed.  If\n        the function returns true, this indicates failure to purge.</para>\n\n        <funcsynopsis><funcprototype>\n          <funcdef>typedef bool <function>(extent_split_t)</function></funcdef>\n          <paramdef>extent_hooks_t *<parameter>extent_hooks</parameter></paramdef>\n          <paramdef>void *<parameter>addr</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n          <paramdef>size_t <parameter>size_a</parameter></paramdef>\n          <paramdef>size_t <parameter>size_b</parameter></paramdef>\n          <paramdef>bool <parameter>committed</parameter></paramdef>\n          <paramdef>unsigned <parameter>arena_ind</parameter></paramdef>\n        </funcprototype></funcsynopsis>\n        <literallayout></literallayout>\n        <para>An extent split function conforms to the\n        <type>extent_split_t</type> type and optionally splits an extent at\n        given <parameter>addr</parameter> and <parameter>size</parameter> into\n        two adjacent extents, the first of <parameter>size_a</parameter> bytes,\n        and the second of <parameter>size_b</parameter> bytes, operating on\n        <parameter>committed</parameter>/decommitted memory as indicated, on\n        behalf of arena <parameter>arena_ind</parameter>, returning false upon\n        success.  If the function returns true, this indicates that the extent\n        remains unsplit and therefore should continue to be operated on as a\n        whole.</para>\n\n        <funcsynopsis><funcprototype>\n          <funcdef>typedef bool <function>(extent_merge_t)</function></funcdef>\n          <paramdef>extent_hooks_t *<parameter>extent_hooks</parameter></paramdef>\n          <paramdef>void *<parameter>addr_a</parameter></paramdef>\n          <paramdef>size_t <parameter>size_a</parameter></paramdef>\n          <paramdef>void *<parameter>addr_b</parameter></paramdef>\n          <paramdef>size_t <parameter>size_b</parameter></paramdef>\n          <paramdef>bool <parameter>committed</parameter></paramdef>\n          <paramdef>unsigned <parameter>arena_ind</parameter></paramdef>\n        </funcprototype></funcsynopsis>\n        <literallayout></literallayout>\n        <para>An extent merge function conforms to the\n        <type>extent_merge_t</type> type and optionally merges adjacent extents,\n        at given <parameter>addr_a</parameter> and <parameter>size_a</parameter>\n        with given <parameter>addr_b</parameter> and\n        <parameter>size_b</parameter> into one contiguous extent, operating on\n        <parameter>committed</parameter>/decommitted memory as indicated, on\n        behalf of arena <parameter>arena_ind</parameter>, returning false upon\n        success.  If the function returns true, this indicates that the extents\n        remain distinct mappings and therefore should continue to be operated on\n        independently.</para>\n        </listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.narenas\">\n        <term>\n          <mallctl>arenas.narenas</mallctl>\n          (<type>unsigned</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Current limit on number of arenas.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.dirty_decay_ms\">\n        <term>\n          <mallctl>arenas.dirty_decay_ms</mallctl>\n          (<type>ssize_t</type>)\n          <literal>rw</literal>\n        </term>\n        <listitem><para>Current default per-arena approximate time in\n        milliseconds from the creation of a set of unused dirty pages until an\n        equivalent set of unused dirty pages is purged and/or reused, used to\n        initialize <link\n        linkend=\"arena.i.dirty_decay_ms\"><mallctl>arena.&lt;i&gt;.dirty_decay_ms</mallctl></link>\n        during arena creation.  See <link\n        linkend=\"opt.dirty_decay_ms\"><mallctl>opt.dirty_decay_ms</mallctl></link>\n        for additional information.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.muzzy_decay_ms\">\n        <term>\n          <mallctl>arenas.muzzy_decay_ms</mallctl>\n          (<type>ssize_t</type>)\n          <literal>rw</literal>\n        </term>\n        <listitem><para>Current default per-arena approximate time in\n        milliseconds from the creation of a set of unused muzzy pages until an\n        equivalent set of unused muzzy pages is purged and/or reused, used to\n        initialize <link\n        linkend=\"arena.i.muzzy_decay_ms\"><mallctl>arena.&lt;i&gt;.muzzy_decay_ms</mallctl></link>\n        during arena creation.  See <link\n        linkend=\"opt.muzzy_decay_ms\"><mallctl>opt.muzzy_decay_ms</mallctl></link>\n        for additional information.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.quantum\">\n        <term>\n          <mallctl>arenas.quantum</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Quantum size.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.page\">\n        <term>\n          <mallctl>arenas.page</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Page size.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.tcache_max\">\n        <term>\n          <mallctl>arenas.tcache_max</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Maximum thread-cached size class.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.nbins\">\n        <term>\n          <mallctl>arenas.nbins</mallctl>\n          (<type>unsigned</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Number of bin size classes.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.nhbins\">\n        <term>\n          <mallctl>arenas.nhbins</mallctl>\n          (<type>unsigned</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Total number of thread cache bin size\n        classes.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.bin.i.size\">\n        <term>\n          <mallctl>arenas.bin.&lt;i&gt;.size</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Maximum size supported by size class.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.bin.i.nregs\">\n        <term>\n          <mallctl>arenas.bin.&lt;i&gt;.nregs</mallctl>\n          (<type>uint32_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Number of regions per slab.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.bin.i.slab_size\">\n        <term>\n          <mallctl>arenas.bin.&lt;i&gt;.slab_size</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Number of bytes per slab.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.nlextents\">\n        <term>\n          <mallctl>arenas.nlextents</mallctl>\n          (<type>unsigned</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Total number of large size classes.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.lextent.i.size\">\n        <term>\n          <mallctl>arenas.lextent.&lt;i&gt;.size</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Maximum size supported by this large size\n        class.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.create\">\n        <term>\n          <mallctl>arenas.create</mallctl>\n          (<type>unsigned</type>, <type>extent_hooks_t *</type>)\n          <literal>rw</literal>\n        </term>\n        <listitem><para>Explicitly create a new arena outside the range of\n        automatically managed arenas, with optionally specified extent hooks,\n        and return the new arena index.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.lookup\">\n        <term>\n          <mallctl>arenas.lookup</mallctl>\n          (<type>unsigned</type>, <type>void*</type>)\n          <literal>rw</literal>\n        </term>\n        <listitem><para>Index of the arena to which an allocation belongs to.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"prof.thread_active_init\">\n        <term>\n          <mallctl>prof.thread_active_init</mallctl>\n          (<type>bool</type>)\n          <literal>rw</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Control the initial setting for <link\n        linkend=\"thread.prof.active\"><mallctl>thread.prof.active</mallctl></link>\n        in newly created threads.  See the <link\n        linkend=\"opt.prof_thread_active_init\"><mallctl>opt.prof_thread_active_init</mallctl></link>\n        option for additional information.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"prof.active\">\n        <term>\n          <mallctl>prof.active</mallctl>\n          (<type>bool</type>)\n          <literal>rw</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Control whether sampling is currently active.  See the\n        <link\n        linkend=\"opt.prof_active\"><mallctl>opt.prof_active</mallctl></link>\n        option for additional information, as well as the interrelated <link\n        linkend=\"thread.prof.active\"><mallctl>thread.prof.active</mallctl></link>\n        mallctl.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"prof.dump\">\n        <term>\n          <mallctl>prof.dump</mallctl>\n          (<type>const char *</type>)\n          <literal>-w</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Dump a memory profile to the specified file, or if NULL\n        is specified, to a file according to the pattern\n        <filename>&lt;prefix&gt;.&lt;pid&gt;.&lt;seq&gt;.m&lt;mseq&gt;.heap</filename>,\n        where <literal>&lt;prefix&gt;</literal> is controlled by the\n        <link\n        linkend=\"opt.prof_prefix\"><mallctl>opt.prof_prefix</mallctl></link>\n        option.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"prof.gdump\">\n        <term>\n          <mallctl>prof.gdump</mallctl>\n          (<type>bool</type>)\n          <literal>rw</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>When enabled, trigger a memory profile dump every time\n        the total virtual memory exceeds the previous maximum.  Profiles are\n        dumped to files named according to the pattern\n        <filename>&lt;prefix&gt;.&lt;pid&gt;.&lt;seq&gt;.u&lt;useq&gt;.heap</filename>,\n        where <literal>&lt;prefix&gt;</literal> is controlled by the <link\n        linkend=\"opt.prof_prefix\"><mallctl>opt.prof_prefix</mallctl></link>\n        option.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"prof.reset\">\n        <term>\n          <mallctl>prof.reset</mallctl>\n          (<type>size_t</type>)\n          <literal>-w</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Reset all memory profile statistics, and optionally\n        update the sample rate (see <link\n        linkend=\"opt.lg_prof_sample\"><mallctl>opt.lg_prof_sample</mallctl></link>\n        and <link\n        linkend=\"prof.lg_sample\"><mallctl>prof.lg_sample</mallctl></link>).\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"prof.lg_sample\">\n        <term>\n          <mallctl>prof.lg_sample</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Get the current sample rate (see <link\n        linkend=\"opt.lg_prof_sample\"><mallctl>opt.lg_prof_sample</mallctl></link>).\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"prof.interval\">\n        <term>\n          <mallctl>prof.interval</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Average number of bytes allocated between\n        interval-based profile dumps.  See the\n        <link\n        linkend=\"opt.lg_prof_interval\"><mallctl>opt.lg_prof_interval</mallctl></link>\n        option for additional information.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.allocated\">\n        <term>\n          <mallctl>stats.allocated</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Total number of bytes allocated by the\n        application.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.active\">\n        <term>\n          <mallctl>stats.active</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Total number of bytes in active pages allocated by the\n        application.  This is a multiple of the page size, and greater than or\n        equal to <link\n        linkend=\"stats.allocated\"><mallctl>stats.allocated</mallctl></link>.\n        This does not include <link linkend=\"stats.arenas.i.pdirty\">\n        <mallctl>stats.arenas.&lt;i&gt;.pdirty</mallctl></link>,\n        <link linkend=\"stats.arenas.i.pmuzzy\">\n        <mallctl>stats.arenas.&lt;i&gt;.pmuzzy</mallctl></link>, nor pages\n        entirely devoted to allocator metadata.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.metadata\">\n        <term>\n          <mallctl>stats.metadata</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Total number of bytes dedicated to metadata, which\n        comprise base allocations used for bootstrap-sensitive allocator\n        metadata structures (see <link\n        linkend=\"stats.arenas.i.base\"><mallctl>stats.arenas.&lt;i&gt;.base</mallctl></link>)\n        and internal allocations (see <link\n        linkend=\"stats.arenas.i.internal\"><mallctl>stats.arenas.&lt;i&gt;.internal</mallctl></link>).\n        Transparent huge page (enabled with <link\n        linkend=\"opt.metadata_thp\">opt.metadata_thp</link>) usage is not\n        considered.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.metadata_thp\">\n        <term>\n          <mallctl>stats.metadata_thp</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Number of transparent huge pages (THP) used for\n        metadata.  See <link\n        linkend=\"stats.metadata\"><mallctl>stats.metadata</mallctl></link> and\n        <link linkend=\"opt.metadata_thp\">opt.metadata_thp</link>) for\n        details.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.resident\">\n        <term>\n          <mallctl>stats.resident</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Maximum number of bytes in physically resident data\n        pages mapped by the allocator, comprising all pages dedicated to\n        allocator metadata, pages backing active allocations, and unused dirty\n        pages.  This is a maximum rather than precise because pages may not\n        actually be physically resident if they correspond to demand-zeroed\n        virtual memory that has not yet been touched.  This is a multiple of the\n        page size, and is larger than <link\n        linkend=\"stats.active\"><mallctl>stats.active</mallctl></link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.mapped\">\n        <term>\n          <mallctl>stats.mapped</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Total number of bytes in active extents mapped by the\n        allocator.  This is larger than <link\n        linkend=\"stats.active\"><mallctl>stats.active</mallctl></link>.  This\n        does not include inactive extents, even those that contain unused dirty\n        pages, which means that there is no strict ordering between this and\n        <link\n        linkend=\"stats.resident\"><mallctl>stats.resident</mallctl></link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.retained\">\n        <term>\n          <mallctl>stats.retained</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Total number of bytes in virtual memory mappings that\n        were retained rather than being returned to the operating system via\n        e.g. <citerefentry><refentrytitle>munmap</refentrytitle>\n        <manvolnum>2</manvolnum></citerefentry> or similar.  Retained virtual\n        memory is typically untouched, decommitted, or purged, so it has no\n        strongly associated physical memory (see <link\n        linkend=\"arena.i.extent_hooks\">extent hooks</link> for details).\n        Retained memory is excluded from mapped memory statistics, e.g. <link\n        linkend=\"stats.mapped\"><mallctl>stats.mapped</mallctl></link>.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.background_thread.num_threads\">\n        <term>\n          <mallctl>stats.background_thread.num_threads</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para> Number of <link linkend=\"background_thread\">background\n        threads</link> running currently.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.background_thread.num_runs\">\n        <term>\n          <mallctl>stats.background_thread.num_runs</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para> Total number of runs from all <link\n        linkend=\"background_thread\">background threads</link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.background_thread.run_interval\">\n        <term>\n          <mallctl>stats.background_thread.run_interval</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para> Average run interval in nanoseconds of <link\n        linkend=\"background_thread\">background threads</link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.mutexes.ctl\">\n        <term>\n          <mallctl>stats.mutexes.ctl.{counter};</mallctl>\n          (<type>counter specific type</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Statistics on <varname>ctl</varname> mutex (global\n        scope; mallctl related).  <mallctl>{counter}</mallctl> is one of the\n        counters below:</para>\n        <varlistentry id=\"mutex_counters\">\n          <listitem><para><varname>num_ops</varname> (<type>uint64_t</type>):\n          Total number of lock acquisition operations on this mutex.</para>\n\n\t  <para><varname>num_spin_acq</varname> (<type>uint64_t</type>): Number\n\t  of times the mutex was spin-acquired.  When the mutex is currently\n\t  locked and cannot be acquired immediately, a short period of\n\t  spin-retry within jemalloc will be performed.  Acquired through spin\n\t  generally means the contention was lightweight and not causing context\n\t  switches.</para>\n\n\t  <para><varname>num_wait</varname> (<type>uint64_t</type>): Number of\n\t  times the mutex was wait-acquired, which means the mutex contention\n\t  was not solved by spin-retry, and blocking operation was likely\n\t  involved in order to acquire the mutex.  This event generally implies\n\t  higher cost / longer delay, and should be investigated if it happens\n\t  often.</para>\n\n\t  <para><varname>max_wait_time</varname> (<type>uint64_t</type>):\n\t  Maximum length of time in nanoseconds spent on a single wait-acquired\n\t  lock operation.  Note that to avoid profiling overhead on the common\n\t  path, this does not consider spin-acquired cases.</para>\n\n\t  <para><varname>total_wait_time</varname> (<type>uint64_t</type>):\n\t  Cumulative time in nanoseconds spent on wait-acquired lock operations.\n\t  Similarly, spin-acquired cases are not considered.</para>\n\n\t  <para><varname>max_num_thds</varname> (<type>uint32_t</type>): Maximum\n\t  number of threads waiting on this mutex simultaneously.  Similarly,\n\t  spin-acquired cases are not considered.</para>\n\n\t  <para><varname>num_owner_switch</varname> (<type>uint64_t</type>):\n\t  Number of times the current mutex owner is different from the previous\n\t  one.  This event does not generally imply an issue; rather it is an\n\t  indicator of how often the protected data are accessed by different\n\t  threads.\n\t  </para>\n\t  </listitem>\n\t</varlistentry>\n\t</listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.mutexes.background_thread\">\n        <term>\n          <mallctl>stats.mutexes.background_thread.{counter}</mallctl>\n\t  (<type>counter specific type</type>) <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Statistics on <varname>background_thread</varname> mutex\n        (global scope; <link\n        linkend=\"background_thread\"><mallctl>background_thread</mallctl></link>\n        related).  <mallctl>{counter}</mallctl> is one of the counters in <link\n        linkend=\"mutex_counters\">mutex profiling\n        counters</link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.mutexes.prof\">\n        <term>\n          <mallctl>stats.mutexes.prof.{counter}</mallctl>\n\t  (<type>counter specific type</type>) <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Statistics on <varname>prof</varname> mutex (global\n        scope; profiling related).  <mallctl>{counter}</mallctl> is one of the\n        counters in <link linkend=\"mutex_counters\">mutex profiling\n        counters</link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.mutexes.reset\">\n        <term>\n          <mallctl>stats.mutexes.reset</mallctl>\n\t  (<type>void</type>) <literal>--</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Reset all mutex profile statistics, including global\n        mutexes, arena mutexes and bin mutexes.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.dss\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.dss</mallctl>\n          (<type>const char *</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>dss (<citerefentry><refentrytitle>sbrk</refentrytitle>\n        <manvolnum>2</manvolnum></citerefentry>) allocation precedence as\n        related to <citerefentry><refentrytitle>mmap</refentrytitle>\n        <manvolnum>2</manvolnum></citerefentry> allocation.  See <link\n        linkend=\"opt.dss\"><mallctl>opt.dss</mallctl></link> for details.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.dirty_decay_ms\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.dirty_decay_ms</mallctl>\n          (<type>ssize_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Approximate time in milliseconds from the creation of a\n        set of unused dirty pages until an equivalent set of unused dirty pages\n        is purged and/or reused.  See <link\n        linkend=\"opt.dirty_decay_ms\"><mallctl>opt.dirty_decay_ms</mallctl></link>\n        for details.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.muzzy_decay_ms\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.muzzy_decay_ms</mallctl>\n          (<type>ssize_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Approximate time in milliseconds from the creation of a\n        set of unused muzzy pages until an equivalent set of unused muzzy pages\n        is purged and/or reused.  See <link\n        linkend=\"opt.muzzy_decay_ms\"><mallctl>opt.muzzy_decay_ms</mallctl></link>\n        for details.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.nthreads\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.nthreads</mallctl>\n          (<type>unsigned</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Number of threads currently assigned to\n        arena.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.uptime\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.uptime</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Time elapsed (in nanoseconds) since the arena was\n        created.  If &lt;i&gt; equals <constant>0</constant> or\n        <constant>MALLCTL_ARENAS_ALL</constant>, this is the uptime since malloc\n        initialization.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.pactive\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.pactive</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Number of pages in active extents.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.pdirty\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.pdirty</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Number of pages within unused extents that are\n        potentially dirty, and for which <function>madvise()</function> or\n        similar has not been called.  See <link\n        linkend=\"opt.dirty_decay_ms\"><mallctl>opt.dirty_decay_ms</mallctl></link>\n        for a description of dirty pages.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.pmuzzy\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.pmuzzy</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Number of pages within unused extents that are muzzy.\n        See <link\n        linkend=\"opt.muzzy_decay_ms\"><mallctl>opt.muzzy_decay_ms</mallctl></link>\n        for a description of muzzy pages.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.mapped\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.mapped</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Number of mapped bytes.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.retained\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.retained</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Number of retained bytes.  See <link\n        linkend=\"stats.retained\"><mallctl>stats.retained</mallctl></link> for\n        details.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.extent_avail\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.extent_avail</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Number of allocated (but unused) extent structs in this\n\tarena.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.base\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.base</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>\n        Number of bytes dedicated to bootstrap-sensitive allocator metadata\n        structures.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.internal\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.internal</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Number of bytes dedicated to internal allocations.\n        Internal allocations differ from application-originated allocations in\n        that they are for internal use, and that they are omitted from heap\n        profiles.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.metadata_thp\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.metadata_thp</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Number of transparent huge pages (THP) used for\n        metadata.  See <link linkend=\"opt.metadata_thp\">opt.metadata_thp</link>\n        for details.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.resident\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.resident</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Maximum number of bytes in physically resident data\n        pages mapped by the arena, comprising all pages dedicated to allocator\n        metadata, pages backing active allocations, and unused dirty pages.\n        This is a maximum rather than precise because pages may not actually be\n        physically resident if they correspond to demand-zeroed virtual memory\n        that has not yet been touched.  This is a multiple of the page\n        size.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.dirty_npurge\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.dirty_npurge</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Number of dirty page purge sweeps performed.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.dirty_nmadvise\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.dirty_nmadvise</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Number of <function>madvise()</function> or similar\n        calls made to purge dirty pages.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.dirty_purged\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.dirty_purged</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Number of dirty pages purged.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.muzzy_npurge\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.muzzy_npurge</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Number of muzzy page purge sweeps performed.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.muzzy_nmadvise\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.muzzy_nmadvise</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Number of <function>madvise()</function> or similar\n        calls made to purge muzzy pages.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.muzzy_purged\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.muzzy_purged</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Number of muzzy pages purged.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.small.allocated\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.small.allocated</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Number of bytes currently allocated by small objects.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.small.nmalloc\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.small.nmalloc</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of times a small allocation was\n        requested from the arena's bins, whether to fill the relevant tcache if\n        <link linkend=\"opt.tcache\"><mallctl>opt.tcache</mallctl></link> is\n        enabled, or to directly satisfy an allocation request\n        otherwise.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.small.ndalloc\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.small.ndalloc</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of times a small allocation was\n        returned to the arena's bins, whether to flush the relevant tcache if\n        <link linkend=\"opt.tcache\"><mallctl>opt.tcache</mallctl></link> is\n        enabled, or to directly deallocate an allocation\n        otherwise.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.small.nrequests\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.small.nrequests</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of allocation requests satisfied by\n        all bin size classes.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.small.nfills\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.small.nfills</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of tcache fills by all small size\n\tclasses.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.small.nflushes\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.small.nflushes</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of tcache flushes by all small size\n        classes.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.large.allocated\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.large.allocated</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Number of bytes currently allocated by large objects.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.large.nmalloc\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.large.nmalloc</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of times a large extent was allocated\n        from the arena, whether to fill the relevant tcache if <link\n        linkend=\"opt.tcache\"><mallctl>opt.tcache</mallctl></link> is enabled and\n        the size class is within the range being cached, or to directly satisfy\n        an allocation request otherwise.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.large.ndalloc\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.large.ndalloc</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of times a large extent was returned\n        to the arena, whether to flush the relevant tcache if <link\n        linkend=\"opt.tcache\"><mallctl>opt.tcache</mallctl></link> is enabled and\n        the size class is within the range being cached, or to directly\n        deallocate an allocation otherwise.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.large.nrequests\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.large.nrequests</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of allocation requests satisfied by\n        all large size classes.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.large.nfills\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.large.nfills</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of tcache fills by all large size\n\tclasses.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.large.nflushes\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.large.nflushes</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of tcache flushes by all large size\n        classes.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.bins.j.nmalloc\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.bins.&lt;j&gt;.nmalloc</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of times a bin region of the\n        corresponding size class was allocated from the arena, whether to fill\n        the relevant tcache if <link\n        linkend=\"opt.tcache\"><mallctl>opt.tcache</mallctl></link> is enabled, or\n        to directly satisfy an allocation request otherwise.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.bins.j.ndalloc\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.bins.&lt;j&gt;.ndalloc</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of times a bin region of the\n        corresponding size class was returned to the arena, whether to flush the\n        relevant tcache if <link\n        linkend=\"opt.tcache\"><mallctl>opt.tcache</mallctl></link> is enabled, or\n        to directly deallocate an allocation otherwise.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.bins.j.nrequests\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.bins.&lt;j&gt;.nrequests</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of allocation requests satisfied by\n        bin regions of the corresponding size class.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.bins.j.curregs\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.bins.&lt;j&gt;.curregs</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Current number of regions for this size\n        class.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.bins.j.nfills\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.bins.&lt;j&gt;.nfills</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Cumulative number of tcache fills.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.bins.j.nflushes\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.bins.&lt;j&gt;.nflushes</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Cumulative number of tcache flushes.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.bins.j.nslabs\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.bins.&lt;j&gt;.nslabs</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of slabs created.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.bins.j.nreslabs\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.bins.&lt;j&gt;.nreslabs</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of times the current slab from which\n        to allocate changed.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.bins.j.curslabs\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.bins.&lt;j&gt;.curslabs</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Current number of slabs.</para></listitem>\n      </varlistentry>\n\n\n      <varlistentry id=\"stats.arenas.i.bins.j.nonfull_slabs\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.bins.&lt;j&gt;.nonfull_slabs</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Current number of nonfull slabs.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.bins.mutex\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.bins.&lt;j&gt;.mutex.{counter}</mallctl>\n          (<type>counter specific type</type>) <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Statistics on\n        <varname>arena.&lt;i&gt;.bins.&lt;j&gt;</varname> mutex (arena bin\n        scope; bin operation related).  <mallctl>{counter}</mallctl> is one of\n        the counters in <link linkend=\"mutex_counters\">mutex profiling\n        counters</link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.extents.n\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.extents.&lt;j&gt;.n{extent_type}</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para> Number of extents of the given type in this arena in\n\tthe bucket corresponding to page size index &lt;j&gt;. The extent type\n\tis one of dirty, muzzy, or retained.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.extents.bytes\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.extents.&lt;j&gt;.{extent_type}_bytes</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n\t<listitem><para> Sum of the bytes managed by extents of the given type\n\tin this arena in the bucket corresponding to page size index &lt;j&gt;.\n\tThe extent type is one of dirty, muzzy, or retained.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.lextents.j.nmalloc\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.lextents.&lt;j&gt;.nmalloc</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of times a large extent of the\n        corresponding size class was allocated from the arena, whether to fill\n        the relevant tcache if <link\n        linkend=\"opt.tcache\"><mallctl>opt.tcache</mallctl></link> is enabled and\n        the size class is within the range being cached, or to directly satisfy\n        an allocation request otherwise.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.lextents.j.ndalloc\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.lextents.&lt;j&gt;.ndalloc</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of times a large extent of the\n        corresponding size class was returned to the arena, whether to flush the\n        relevant tcache if <link\n        linkend=\"opt.tcache\"><mallctl>opt.tcache</mallctl></link> is enabled and\n        the size class is within the range being cached, or to directly\n        deallocate an allocation otherwise.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.lextents.j.nrequests\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.lextents.&lt;j&gt;.nrequests</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of allocation requests satisfied by\n        large extents of the corresponding size class.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.lextents.j.curlextents\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.lextents.&lt;j&gt;.curlextents</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Current number of large allocations for this size class.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.mutexes.large\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.mutexes.large.{counter}</mallctl>\n          (<type>counter specific type</type>) <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Statistics on <varname>arena.&lt;i&gt;.large</varname>\n        mutex (arena scope; large allocation related).\n        <mallctl>{counter}</mallctl> is one of the counters in <link\n        linkend=\"mutex_counters\">mutex profiling\n        counters</link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.mutexes.extent_avail\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.mutexes.extent_avail.{counter}</mallctl>\n          (<type>counter specific type</type>) <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Statistics on <varname>arena.&lt;i&gt;.extent_avail\n        </varname> mutex (arena scope; extent avail related).\n        <mallctl>{counter}</mallctl> is one of the counters in <link\n        linkend=\"mutex_counters\">mutex profiling\n        counters</link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.mutexes.extents_dirty\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.mutexes.extents_dirty.{counter}</mallctl>\n          (<type>counter specific type</type>) <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Statistics on <varname>arena.&lt;i&gt;.extents_dirty\n        </varname> mutex (arena scope; dirty extents related).\n        <mallctl>{counter}</mallctl> is one of the counters in <link\n        linkend=\"mutex_counters\">mutex profiling\n        counters</link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.mutexes.extents_muzzy\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.mutexes.extents_muzzy.{counter}</mallctl>\n          (<type>counter specific type</type>) <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Statistics on <varname>arena.&lt;i&gt;.extents_muzzy\n        </varname> mutex (arena scope; muzzy extents related).\n        <mallctl>{counter}</mallctl> is one of the counters in <link\n        linkend=\"mutex_counters\">mutex profiling\n        counters</link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.mutexes.extents_retained\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.mutexes.extents_retained.{counter}</mallctl>\n          (<type>counter specific type</type>) <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Statistics on <varname>arena.&lt;i&gt;.extents_retained\n        </varname> mutex (arena scope; retained extents related).\n        <mallctl>{counter}</mallctl> is one of the counters in <link\n        linkend=\"mutex_counters\">mutex profiling\n        counters</link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.mutexes.decay_dirty\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.mutexes.decay_dirty.{counter}</mallctl>\n          (<type>counter specific type</type>) <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Statistics on <varname>arena.&lt;i&gt;.decay_dirty\n        </varname> mutex (arena scope; decay for dirty pages related).\n        <mallctl>{counter}</mallctl> is one of the counters in <link\n        linkend=\"mutex_counters\">mutex profiling\n        counters</link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.mutexes.decay_muzzy\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.mutexes.decay_muzzy.{counter}</mallctl>\n          (<type>counter specific type</type>) <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Statistics on <varname>arena.&lt;i&gt;.decay_muzzy\n        </varname> mutex (arena scope; decay for muzzy pages related).\n        <mallctl>{counter}</mallctl> is one of the counters in <link\n        linkend=\"mutex_counters\">mutex profiling\n        counters</link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.mutexes.base\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.mutexes.base.{counter}</mallctl>\n          (<type>counter specific type</type>) <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Statistics on <varname>arena.&lt;i&gt;.base</varname>\n        mutex (arena scope; base allocator related).\n        <mallctl>{counter}</mallctl> is one of the counters in <link\n        linkend=\"mutex_counters\">mutex profiling\n        counters</link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.mutexes.tcache_list\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.mutexes.tcache_list.{counter}</mallctl>\n          (<type>counter specific type</type>) <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Statistics on\n        <varname>arena.&lt;i&gt;.tcache_list</varname> mutex (arena scope;\n        tcache to arena association related).  This mutex is expected to be\n        accessed less often.  <mallctl>{counter}</mallctl> is one of the\n        counters in <link linkend=\"mutex_counters\">mutex profiling\n        counters</link>.</para></listitem>\n      </varlistentry>\n\n    </variablelist>\n  </refsect1>\n  <refsect1 id=\"heap_profile_format\">\n    <title>HEAP PROFILE FORMAT</title>\n    <para>Although the heap profiling functionality was originally designed to\n    be compatible with the\n    <command>pprof</command> command that is developed as part of the <ulink\n    url=\"http://code.google.com/p/gperftools/\">gperftools\n    package</ulink>, the addition of per thread heap profiling functionality\n    required a different heap profile format.  The <command>jeprof</command>\n    command is derived from <command>pprof</command>, with enhancements to\n    support the heap profile format described here.</para>\n\n    <para>In the following hypothetical heap profile, <constant>[...]</constant>\n    indicates elision for the sake of compactness.  <programlisting><![CDATA[\nheap_v2/524288\n  t*: 28106: 56637512 [0: 0]\n  [...]\n  t3: 352: 16777344 [0: 0]\n  [...]\n  t99: 17754: 29341640 [0: 0]\n  [...]\n@ 0x5f86da8 0x5f5a1dc [...] 0x29e4d4e 0xa200316 0xabb2988 [...]\n  t*: 13: 6688 [0: 0]\n  t3: 12: 6496 [0: ]\n  t99: 1: 192 [0: 0]\n[...]\n\nMAPPED_LIBRARIES:\n[...]]]></programlisting> The following matches the above heap profile, but most\ntokens are replaced with <constant>&lt;description&gt;</constant> to indicate\ndescriptions of the corresponding fields.  <programlisting><![CDATA[\n<heap_profile_format_version>/<mean_sample_interval>\n  <aggregate>: <curobjs>: <curbytes> [<cumobjs>: <cumbytes>]\n  [...]\n  <thread_3_aggregate>: <curobjs>: <curbytes>[<cumobjs>: <cumbytes>]\n  [...]\n  <thread_99_aggregate>: <curobjs>: <curbytes>[<cumobjs>: <cumbytes>]\n  [...]\n@ <top_frame> <frame> [...] <frame> <frame> <frame> [...]\n  <backtrace_aggregate>: <curobjs>: <curbytes> [<cumobjs>: <cumbytes>]\n  <backtrace_thread_3>: <curobjs>: <curbytes> [<cumobjs>: <cumbytes>]\n  <backtrace_thread_99>: <curobjs>: <curbytes> [<cumobjs>: <cumbytes>]\n[...]\n\nMAPPED_LIBRARIES:\n</proc/<pid>/maps>]]></programlisting></para>\n  </refsect1>\n\n  <refsect1 id=\"debugging_malloc_problems\">\n    <title>DEBUGGING MALLOC PROBLEMS</title>\n    <para>When debugging, it is a good idea to configure/build jemalloc with\n    the <option>--enable-debug</option> and <option>--enable-fill</option>\n    options, and recompile the program with suitable options and symbols for\n    debugger support.  When so configured, jemalloc incorporates a wide variety\n    of run-time assertions that catch application errors such as double-free,\n    write-after-free, etc.</para>\n\n    <para>Programs often accidentally depend on <quote>uninitialized</quote>\n    memory actually being filled with zero bytes.  Junk filling\n    (see the <link linkend=\"opt.junk\"><mallctl>opt.junk</mallctl></link>\n    option) tends to expose such bugs in the form of obviously incorrect\n    results and/or coredumps.  Conversely, zero\n    filling (see the <link\n    linkend=\"opt.zero\"><mallctl>opt.zero</mallctl></link> option) eliminates\n    the symptoms of such bugs.  Between these two options, it is usually\n    possible to quickly detect, diagnose, and eliminate such bugs.</para>\n\n    <para>This implementation does not provide much detail about the problems\n    it detects, because the performance impact for storing such information\n    would be prohibitive.</para>\n  </refsect1>\n  <refsect1 id=\"diagnostic_messages\">\n    <title>DIAGNOSTIC MESSAGES</title>\n    <para>If any of the memory allocation/deallocation functions detect an\n    error or warning condition, a message will be printed to file descriptor\n    <constant>STDERR_FILENO</constant>.  Errors will result in the process\n    dumping core.  If the <link\n    linkend=\"opt.abort\"><mallctl>opt.abort</mallctl></link> option is set, most\n    warnings are treated as errors.</para>\n\n    <para>The <varname>malloc_message</varname> variable allows the programmer\n    to override the function which emits the text strings forming the errors\n    and warnings if for some reason the <constant>STDERR_FILENO</constant> file\n    descriptor is not suitable for this.\n    <function>malloc_message()</function> takes the\n    <parameter>cbopaque</parameter> pointer argument that is\n    <constant>NULL</constant> unless overridden by the arguments in a call to\n    <function>malloc_stats_print()</function>, followed by a string\n    pointer.  Please note that doing anything which tries to allocate memory in\n    this function is likely to result in a crash or deadlock.</para>\n\n    <para>All messages are prefixed by\n    <quote><computeroutput>&lt;jemalloc&gt;: </computeroutput></quote>.</para>\n  </refsect1>\n  <refsect1 id=\"return_values\">\n    <title>RETURN VALUES</title>\n    <refsect2>\n      <title>Standard API</title>\n      <para>The <function>malloc()</function> and\n      <function>calloc()</function> functions return a pointer to the\n      allocated memory if successful; otherwise a <constant>NULL</constant>\n      pointer is returned and <varname>errno</varname> is set to\n      <errorname>ENOMEM</errorname>.</para>\n\n      <para>The <function>posix_memalign()</function> function\n      returns the value 0 if successful; otherwise it returns an error value.\n      The <function>posix_memalign()</function> function will fail\n      if:\n        <variablelist>\n          <varlistentry>\n            <term><errorname>EINVAL</errorname></term>\n\n            <listitem><para>The <parameter>alignment</parameter> parameter is\n            not a power of 2 at least as large as\n            <code language=\"C\">sizeof(<type>void *</type>)</code>.\n            </para></listitem>\n          </varlistentry>\n          <varlistentry>\n            <term><errorname>ENOMEM</errorname></term>\n\n            <listitem><para>Memory allocation error.</para></listitem>\n          </varlistentry>\n        </variablelist>\n      </para>\n\n      <para>The <function>aligned_alloc()</function> function returns\n      a pointer to the allocated memory if successful; otherwise a\n      <constant>NULL</constant> pointer is returned and\n      <varname>errno</varname> is set.  The\n      <function>aligned_alloc()</function> function will fail if:\n        <variablelist>\n          <varlistentry>\n            <term><errorname>EINVAL</errorname></term>\n\n            <listitem><para>The <parameter>alignment</parameter> parameter is\n            not a power of 2.\n            </para></listitem>\n          </varlistentry>\n          <varlistentry>\n            <term><errorname>ENOMEM</errorname></term>\n\n            <listitem><para>Memory allocation error.</para></listitem>\n          </varlistentry>\n        </variablelist>\n      </para>\n\n      <para>The <function>realloc()</function> function returns a\n      pointer, possibly identical to <parameter>ptr</parameter>, to the\n      allocated memory if successful; otherwise a <constant>NULL</constant>\n      pointer is returned, and <varname>errno</varname> is set to\n      <errorname>ENOMEM</errorname> if the error was the result of an\n      allocation failure.  The <function>realloc()</function>\n      function always leaves the original buffer intact when an error occurs.\n      </para>\n\n      <para>The <function>free()</function> function returns no\n      value.</para>\n    </refsect2>\n    <refsect2>\n      <title>Non-standard API</title>\n      <para>The <function>mallocx()</function> and\n      <function>rallocx()</function> functions return a pointer to\n      the allocated memory if successful; otherwise a <constant>NULL</constant>\n      pointer is returned to indicate insufficient contiguous memory was\n      available to service the allocation request.  </para>\n\n      <para>The <function>xallocx()</function> function returns the\n      real size of the resulting resized allocation pointed to by\n      <parameter>ptr</parameter>, which is a value less than\n      <parameter>size</parameter> if the allocation could not be adequately\n      grown in place.  </para>\n\n      <para>The <function>sallocx()</function> function returns the\n      real size of the allocation pointed to by <parameter>ptr</parameter>.\n      </para>\n\n      <para>The <function>nallocx()</function> returns the real size\n      that would result from a successful equivalent\n      <function>mallocx()</function> function call, or zero if\n      insufficient memory is available to perform the size computation.  </para>\n\n      <para>The <function>mallctl()</function>,\n      <function>mallctlnametomib()</function>, and\n      <function>mallctlbymib()</function> functions return 0 on\n      success; otherwise they return an error value.  The functions will fail\n      if:\n        <variablelist>\n          <varlistentry>\n            <term><errorname>EINVAL</errorname></term>\n\n            <listitem><para><parameter>newp</parameter> is not\n            <constant>NULL</constant>, and <parameter>newlen</parameter> is too\n            large or too small.  Alternatively, <parameter>*oldlenp</parameter>\n            is too large or too small; in this case as much data as possible\n            are read despite the error.</para></listitem>\n          </varlistentry>\n          <varlistentry>\n            <term><errorname>ENOENT</errorname></term>\n\n            <listitem><para><parameter>name</parameter> or\n            <parameter>mib</parameter> specifies an unknown/invalid\n            value.</para></listitem>\n          </varlistentry>\n          <varlistentry>\n            <term><errorname>EPERM</errorname></term>\n\n            <listitem><para>Attempt to read or write void value, or attempt to\n            write read-only value.</para></listitem>\n          </varlistentry>\n          <varlistentry>\n            <term><errorname>EAGAIN</errorname></term>\n\n            <listitem><para>A memory allocation failure\n            occurred.</para></listitem>\n          </varlistentry>\n          <varlistentry>\n            <term><errorname>EFAULT</errorname></term>\n\n            <listitem><para>An interface with side effects failed in some way\n            not directly related to <function>mallctl*()</function>\n            read/write processing.</para></listitem>\n          </varlistentry>\n        </variablelist>\n      </para>\n\n      <para>The <function>malloc_usable_size()</function> function\n      returns the usable size of the allocation pointed to by\n      <parameter>ptr</parameter>.  </para>\n    </refsect2>\n  </refsect1>\n  <refsect1 id=\"environment\">\n    <title>ENVIRONMENT</title>\n    <para>The following environment variable affects the execution of the\n    allocation functions:\n      <variablelist>\n        <varlistentry>\n          <term><envar>MALLOC_CONF</envar></term>\n\n          <listitem><para>If the environment variable\n          <envar>MALLOC_CONF</envar> is set, the characters it contains\n          will be interpreted as options.</para></listitem>\n        </varlistentry>\n      </variablelist>\n    </para>\n  </refsect1>\n  <refsect1 id=\"examples\">\n    <title>EXAMPLES</title>\n    <para>To dump core whenever a problem occurs:\n      <screen>ln -s 'abort:true' /etc/malloc.conf</screen>\n    </para>\n    <para>To specify in the source that only one arena should be automatically\n    created:\n      <programlisting language=\"C\"><![CDATA[\nmalloc_conf = \"narenas:1\";]]></programlisting></para>\n  </refsect1>\n  <refsect1 id=\"see_also\">\n    <title>SEE ALSO</title>\n    <para><citerefentry><refentrytitle>madvise</refentrytitle>\n    <manvolnum>2</manvolnum></citerefentry>,\n    <citerefentry><refentrytitle>mmap</refentrytitle>\n    <manvolnum>2</manvolnum></citerefentry>,\n    <citerefentry><refentrytitle>sbrk</refentrytitle>\n    <manvolnum>2</manvolnum></citerefentry>,\n    <citerefentry><refentrytitle>utrace</refentrytitle>\n    <manvolnum>2</manvolnum></citerefentry>,\n    <citerefentry><refentrytitle>alloca</refentrytitle>\n    <manvolnum>3</manvolnum></citerefentry>,\n    <citerefentry><refentrytitle>atexit</refentrytitle>\n    <manvolnum>3</manvolnum></citerefentry>,\n    <citerefentry><refentrytitle>getpagesize</refentrytitle>\n    <manvolnum>3</manvolnum></citerefentry></para>\n  </refsect1>\n  <refsect1 id=\"standards\">\n    <title>STANDARDS</title>\n    <para>The <function>malloc()</function>,\n    <function>calloc()</function>,\n    <function>realloc()</function>, and\n    <function>free()</function> functions conform to ISO/IEC\n    9899:1990 (<quote>ISO C90</quote>).</para>\n\n    <para>The <function>posix_memalign()</function> function conforms\n    to IEEE Std 1003.1-2001 (<quote>POSIX.1</quote>).</para>\n  </refsect1>\n</refentry>\n"
  },
  {
    "path": "deps/jemalloc/doc/manpages.xsl.in",
    "content": "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">\n  <xsl:import href=\"@XSLROOT@/manpages/docbook.xsl\"/>\n  <xsl:import href=\"@abs_srcroot@doc/stylesheet.xsl\"/>\n</xsl:stylesheet>\n"
  },
  {
    "path": "deps/jemalloc/doc/stylesheet.xsl",
    "content": "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">\n  <xsl:param name=\"funcsynopsis.style\">ansi</xsl:param>\n  <xsl:param name=\"function.parens\" select=\"0\"/>\n  <xsl:template match=\"function\">\n    <xsl:call-template name=\"inline.monoseq\"/>\n  </xsl:template>\n  <xsl:template match=\"mallctl\">\n    <quote><xsl:call-template name=\"inline.monoseq\"/></quote>\n  </xsl:template>\n</xsl:stylesheet>\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/arena_externs.h",
    "content": "#ifndef JEMALLOC_INTERNAL_ARENA_EXTERNS_H\n#define JEMALLOC_INTERNAL_ARENA_EXTERNS_H\n\n#include \"jemalloc/internal/bin.h\"\n#include \"jemalloc/internal/extent_dss.h\"\n#include \"jemalloc/internal/hook.h\"\n#include \"jemalloc/internal/pages.h\"\n#include \"jemalloc/internal/stats.h\"\n\nextern ssize_t opt_dirty_decay_ms;\nextern ssize_t opt_muzzy_decay_ms;\n\nextern percpu_arena_mode_t opt_percpu_arena;\nextern const char *percpu_arena_mode_names[];\n\nextern const uint64_t h_steps[SMOOTHSTEP_NSTEPS];\nextern malloc_mutex_t arenas_lock;\n\nextern size_t opt_oversize_threshold;\nextern size_t oversize_threshold;\n\nvoid arena_basic_stats_merge(tsdn_t *tsdn, arena_t *arena,\n    unsigned *nthreads, const char **dss, ssize_t *dirty_decay_ms,\n    ssize_t *muzzy_decay_ms, size_t *nactive, size_t *ndirty, size_t *nmuzzy);\nvoid arena_stats_merge(tsdn_t *tsdn, arena_t *arena, unsigned *nthreads,\n    const char **dss, ssize_t *dirty_decay_ms, ssize_t *muzzy_decay_ms,\n    size_t *nactive, size_t *ndirty, size_t *nmuzzy, arena_stats_t *astats,\n    bin_stats_t *bstats, arena_stats_large_t *lstats,\n    arena_stats_extents_t *estats);\nvoid arena_extents_dirty_dalloc(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent);\n#ifdef JEMALLOC_JET\nsize_t arena_slab_regind(extent_t *slab, szind_t binind, const void *ptr);\n#endif\nextent_t *arena_extent_alloc_large(tsdn_t *tsdn, arena_t *arena,\n    size_t usize, size_t alignment, bool *zero);\nvoid arena_extent_dalloc_large_prep(tsdn_t *tsdn, arena_t *arena,\n    extent_t *extent);\nvoid arena_extent_ralloc_large_shrink(tsdn_t *tsdn, arena_t *arena,\n    extent_t *extent, size_t oldsize);\nvoid arena_extent_ralloc_large_expand(tsdn_t *tsdn, arena_t *arena,\n    extent_t *extent, size_t oldsize);\nssize_t arena_dirty_decay_ms_get(arena_t *arena);\nbool arena_dirty_decay_ms_set(tsdn_t *tsdn, arena_t *arena, ssize_t decay_ms);\nssize_t arena_muzzy_decay_ms_get(arena_t *arena);\nbool arena_muzzy_decay_ms_set(tsdn_t *tsdn, arena_t *arena, ssize_t decay_ms);\nvoid arena_decay(tsdn_t *tsdn, arena_t *arena, bool is_background_thread,\n    bool all);\nvoid arena_reset(tsd_t *tsd, arena_t *arena);\nvoid arena_destroy(tsd_t *tsd, arena_t *arena);\nvoid arena_tcache_fill_small(tsdn_t *tsdn, arena_t *arena, tcache_t *tcache,\n    cache_bin_t *tbin, szind_t binind, uint64_t prof_accumbytes);\nvoid arena_alloc_junk_small(void *ptr, const bin_info_t *bin_info,\n    bool zero);\n\ntypedef void (arena_dalloc_junk_small_t)(void *, const bin_info_t *);\nextern arena_dalloc_junk_small_t *JET_MUTABLE arena_dalloc_junk_small;\n\nvoid *arena_malloc_hard(tsdn_t *tsdn, arena_t *arena, size_t size,\n    szind_t ind, bool zero);\nvoid *arena_palloc(tsdn_t *tsdn, arena_t *arena, size_t usize,\n    size_t alignment, bool zero, tcache_t *tcache);\nvoid arena_prof_promote(tsdn_t *tsdn, void *ptr, size_t usize);\nvoid arena_dalloc_promoted(tsdn_t *tsdn, void *ptr, tcache_t *tcache,\n    bool slow_path);\nvoid arena_dalloc_bin_junked_locked(tsdn_t *tsdn, arena_t *arena, bin_t *bin,\n    szind_t binind, extent_t *extent, void *ptr);\nvoid arena_dalloc_small(tsdn_t *tsdn, void *ptr);\nbool arena_ralloc_no_move(tsdn_t *tsdn, void *ptr, size_t oldsize, size_t size,\n    size_t extra, bool zero, size_t *newsize);\nvoid *arena_ralloc(tsdn_t *tsdn, arena_t *arena, void *ptr, size_t oldsize,\n    size_t size, size_t alignment, bool zero, tcache_t *tcache,\n    hook_ralloc_args_t *hook_args);\ndss_prec_t arena_dss_prec_get(arena_t *arena);\nbool arena_dss_prec_set(arena_t *arena, dss_prec_t dss_prec);\nssize_t arena_dirty_decay_ms_default_get(void);\nbool arena_dirty_decay_ms_default_set(ssize_t decay_ms);\nssize_t arena_muzzy_decay_ms_default_get(void);\nbool arena_muzzy_decay_ms_default_set(ssize_t decay_ms);\nbool arena_retain_grow_limit_get_set(tsd_t *tsd, arena_t *arena,\n    size_t *old_limit, size_t *new_limit);\nunsigned arena_nthreads_get(arena_t *arena, bool internal);\nvoid arena_nthreads_inc(arena_t *arena, bool internal);\nvoid arena_nthreads_dec(arena_t *arena, bool internal);\nsize_t arena_extent_sn_next(arena_t *arena);\narena_t *arena_new(tsdn_t *tsdn, unsigned ind, extent_hooks_t *extent_hooks);\nbool arena_init_huge(void);\nbool arena_is_huge(unsigned arena_ind);\narena_t *arena_choose_huge(tsd_t *tsd);\nbin_t *arena_bin_choose_lock(tsdn_t *tsdn, arena_t *arena, szind_t binind,\n    unsigned *binshard);\nvoid arena_boot(sc_data_t *sc_data);\nvoid arena_prefork0(tsdn_t *tsdn, arena_t *arena);\nvoid arena_prefork1(tsdn_t *tsdn, arena_t *arena);\nvoid arena_prefork2(tsdn_t *tsdn, arena_t *arena);\nvoid arena_prefork3(tsdn_t *tsdn, arena_t *arena);\nvoid arena_prefork4(tsdn_t *tsdn, arena_t *arena);\nvoid arena_prefork5(tsdn_t *tsdn, arena_t *arena);\nvoid arena_prefork6(tsdn_t *tsdn, arena_t *arena);\nvoid arena_prefork7(tsdn_t *tsdn, arena_t *arena);\nvoid arena_postfork_parent(tsdn_t *tsdn, arena_t *arena);\nvoid arena_postfork_child(tsdn_t *tsdn, arena_t *arena);\n\n#endif /* JEMALLOC_INTERNAL_ARENA_EXTERNS_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/arena_inlines_a.h",
    "content": "#ifndef JEMALLOC_INTERNAL_ARENA_INLINES_A_H\n#define JEMALLOC_INTERNAL_ARENA_INLINES_A_H\n\nstatic inline unsigned\narena_ind_get(const arena_t *arena) {\n\treturn base_ind_get(arena->base);\n}\n\nstatic inline void\narena_internal_add(arena_t *arena, size_t size) {\n\tatomic_fetch_add_zu(&arena->stats.internal, size, ATOMIC_RELAXED);\n}\n\nstatic inline void\narena_internal_sub(arena_t *arena, size_t size) {\n\tatomic_fetch_sub_zu(&arena->stats.internal, size, ATOMIC_RELAXED);\n}\n\nstatic inline size_t\narena_internal_get(arena_t *arena) {\n\treturn atomic_load_zu(&arena->stats.internal, ATOMIC_RELAXED);\n}\n\nstatic inline bool\narena_prof_accum(tsdn_t *tsdn, arena_t *arena, uint64_t accumbytes) {\n\tcassert(config_prof);\n\n\tif (likely(prof_interval == 0 || !prof_active_get_unlocked())) {\n\t\treturn false;\n\t}\n\n\treturn prof_accum_add(tsdn, &arena->prof_accum, accumbytes);\n}\n\nstatic inline void\npercpu_arena_update(tsd_t *tsd, unsigned cpu) {\n\tassert(have_percpu_arena);\n\tarena_t *oldarena = tsd_arena_get(tsd);\n\tassert(oldarena != NULL);\n\tunsigned oldind = arena_ind_get(oldarena);\n\n\tif (oldind != cpu) {\n\t\tunsigned newind = cpu;\n\t\tarena_t *newarena = arena_get(tsd_tsdn(tsd), newind, true);\n\t\tassert(newarena != NULL);\n\n\t\t/* Set new arena/tcache associations. */\n\t\tarena_migrate(tsd, oldind, newind);\n\t\ttcache_t *tcache = tcache_get(tsd);\n\t\tif (tcache != NULL) {\n\t\t\ttcache_arena_reassociate(tsd_tsdn(tsd), tcache,\n\t\t\t    newarena);\n\t\t}\n\t}\n}\n\n#endif /* JEMALLOC_INTERNAL_ARENA_INLINES_A_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/arena_inlines_b.h",
    "content": "#ifndef JEMALLOC_INTERNAL_ARENA_INLINES_B_H\n#define JEMALLOC_INTERNAL_ARENA_INLINES_B_H\n\n#include \"jemalloc/internal/jemalloc_internal_types.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/rtree.h\"\n#include \"jemalloc/internal/sc.h\"\n#include \"jemalloc/internal/sz.h\"\n#include \"jemalloc/internal/ticker.h\"\n\nJEMALLOC_ALWAYS_INLINE bool\narena_has_default_hooks(arena_t *arena) {\n\treturn (extent_hooks_get(arena) == &extent_hooks_default);\n}\n\nJEMALLOC_ALWAYS_INLINE arena_t *\narena_choose_maybe_huge(tsd_t *tsd, arena_t *arena, size_t size) {\n\tif (arena != NULL) {\n\t\treturn arena;\n\t}\n\n\t/*\n\t * For huge allocations, use the dedicated huge arena if both are true:\n\t * 1) is using auto arena selection (i.e. arena == NULL), and 2) the\n\t * thread is not assigned to a manual arena.\n\t */\n\tif (unlikely(size >= oversize_threshold)) {\n\t\tarena_t *tsd_arena = tsd_arena_get(tsd);\n\t\tif (tsd_arena == NULL || arena_is_auto(tsd_arena)) {\n\t\t\treturn arena_choose_huge(tsd);\n\t\t}\n\t}\n\n\treturn arena_choose(tsd, NULL);\n}\n\nJEMALLOC_ALWAYS_INLINE prof_tctx_t *\narena_prof_tctx_get(tsdn_t *tsdn, const void *ptr, alloc_ctx_t *alloc_ctx) {\n\tcassert(config_prof);\n\tassert(ptr != NULL);\n\n\t/* Static check. */\n\tif (alloc_ctx == NULL) {\n\t\tconst extent_t *extent = iealloc(tsdn, ptr);\n\t\tif (unlikely(!extent_slab_get(extent))) {\n\t\t\treturn large_prof_tctx_get(tsdn, extent);\n\t\t}\n\t} else {\n\t\tif (unlikely(!alloc_ctx->slab)) {\n\t\t\treturn large_prof_tctx_get(tsdn, iealloc(tsdn, ptr));\n\t\t}\n\t}\n\treturn (prof_tctx_t *)(uintptr_t)1U;\n}\n\nJEMALLOC_ALWAYS_INLINE void\narena_prof_tctx_set(tsdn_t *tsdn, const void *ptr, size_t usize,\n    alloc_ctx_t *alloc_ctx, prof_tctx_t *tctx) {\n\tcassert(config_prof);\n\tassert(ptr != NULL);\n\n\t/* Static check. */\n\tif (alloc_ctx == NULL) {\n\t\textent_t *extent = iealloc(tsdn, ptr);\n\t\tif (unlikely(!extent_slab_get(extent))) {\n\t\t\tlarge_prof_tctx_set(tsdn, extent, tctx);\n\t\t}\n\t} else {\n\t\tif (unlikely(!alloc_ctx->slab)) {\n\t\t\tlarge_prof_tctx_set(tsdn, iealloc(tsdn, ptr), tctx);\n\t\t}\n\t}\n}\n\nstatic inline void\narena_prof_tctx_reset(tsdn_t *tsdn, const void *ptr, prof_tctx_t *tctx) {\n\tcassert(config_prof);\n\tassert(ptr != NULL);\n\n\textent_t *extent = iealloc(tsdn, ptr);\n\tassert(!extent_slab_get(extent));\n\n\tlarge_prof_tctx_reset(tsdn, extent);\n}\n\nJEMALLOC_ALWAYS_INLINE nstime_t\narena_prof_alloc_time_get(tsdn_t *tsdn, const void *ptr,\n    alloc_ctx_t *alloc_ctx) {\n\tcassert(config_prof);\n\tassert(ptr != NULL);\n\n\textent_t *extent = iealloc(tsdn, ptr);\n\t/*\n\t * Unlike arena_prof_prof_tctx_{get, set}, we only call this once we're\n\t * sure we have a sampled allocation.\n\t */\n\tassert(!extent_slab_get(extent));\n\treturn large_prof_alloc_time_get(extent);\n}\n\nJEMALLOC_ALWAYS_INLINE void\narena_prof_alloc_time_set(tsdn_t *tsdn, const void *ptr, alloc_ctx_t *alloc_ctx,\n    nstime_t t) {\n\tcassert(config_prof);\n\tassert(ptr != NULL);\n\n\textent_t *extent = iealloc(tsdn, ptr);\n\tassert(!extent_slab_get(extent));\n\tlarge_prof_alloc_time_set(extent, t);\n}\n\nJEMALLOC_ALWAYS_INLINE void\narena_decay_ticks(tsdn_t *tsdn, arena_t *arena, unsigned nticks) {\n\ttsd_t *tsd;\n\tticker_t *decay_ticker;\n\n\tif (unlikely(tsdn_null(tsdn))) {\n\t\treturn;\n\t}\n\ttsd = tsdn_tsd(tsdn);\n\tdecay_ticker = decay_ticker_get(tsd, arena_ind_get(arena));\n\tif (unlikely(decay_ticker == NULL)) {\n\t\treturn;\n\t}\n\tif (unlikely(ticker_ticks(decay_ticker, nticks))) {\n\t\tarena_decay(tsdn, arena, false, false);\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE void\narena_decay_tick(tsdn_t *tsdn, arena_t *arena) {\n\tmalloc_mutex_assert_not_owner(tsdn, &arena->decay_dirty.mtx);\n\tmalloc_mutex_assert_not_owner(tsdn, &arena->decay_muzzy.mtx);\n\n\tarena_decay_ticks(tsdn, arena, 1);\n}\n\n/* Purge a single extent to retained / unmapped directly. */\nJEMALLOC_ALWAYS_INLINE void\narena_decay_extent(tsdn_t *tsdn,arena_t *arena, extent_hooks_t **r_extent_hooks,\n    extent_t *extent) {\n\tsize_t extent_size = extent_size_get(extent);\n\textent_dalloc_wrapper(tsdn, arena,\n\t    r_extent_hooks, extent);\n\tif (config_stats) {\n\t\t/* Update stats accordingly. */\n\t\tarena_stats_lock(tsdn, &arena->stats);\n\t\tarena_stats_add_u64(tsdn, &arena->stats,\n\t\t    &arena->decay_dirty.stats->nmadvise, 1);\n\t\tarena_stats_add_u64(tsdn, &arena->stats,\n\t\t    &arena->decay_dirty.stats->purged, extent_size >> LG_PAGE);\n\t\tarena_stats_sub_zu(tsdn, &arena->stats, &arena->stats.mapped,\n\t\t    extent_size);\n\t\tarena_stats_unlock(tsdn, &arena->stats);\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE void *\narena_malloc(tsdn_t *tsdn, arena_t *arena, size_t size, szind_t ind, bool zero,\n    tcache_t *tcache, bool slow_path) {\n\tassert(!tsdn_null(tsdn) || tcache == NULL);\n\n\tif (likely(tcache != NULL)) {\n\t\tif (likely(size <= SC_SMALL_MAXCLASS)) {\n\t\t\treturn tcache_alloc_small(tsdn_tsd(tsdn), arena,\n\t\t\t    tcache, size, ind, zero, slow_path);\n\t\t}\n\t\tif (likely(size <= tcache_maxclass)) {\n\t\t\treturn tcache_alloc_large(tsdn_tsd(tsdn), arena,\n\t\t\t    tcache, size, ind, zero, slow_path);\n\t\t}\n\t\t/* (size > tcache_maxclass) case falls through. */\n\t\tassert(size > tcache_maxclass);\n\t}\n\n\treturn arena_malloc_hard(tsdn, arena, size, ind, zero);\n}\n\nJEMALLOC_ALWAYS_INLINE arena_t *\narena_aalloc(tsdn_t *tsdn, const void *ptr) {\n\treturn extent_arena_get(iealloc(tsdn, ptr));\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\narena_salloc(tsdn_t *tsdn, const void *ptr) {\n\tassert(ptr != NULL);\n\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\n\tszind_t szind = rtree_szind_read(tsdn, &extents_rtree, rtree_ctx,\n\t    (uintptr_t)ptr, true);\n\tassert(szind != SC_NSIZES);\n\n\treturn sz_index2size(szind);\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\narena_vsalloc(tsdn_t *tsdn, const void *ptr) {\n\t/*\n\t * Return 0 if ptr is not within an extent managed by jemalloc.  This\n\t * function has two extra costs relative to isalloc():\n\t * - The rtree calls cannot claim to be dependent lookups, which induces\n\t *   rtree lookup load dependencies.\n\t * - The lookup may fail, so there is an extra branch to check for\n\t *   failure.\n\t */\n\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\n\textent_t *extent;\n\tszind_t szind;\n\tif (rtree_extent_szind_read(tsdn, &extents_rtree, rtree_ctx,\n\t    (uintptr_t)ptr, false, &extent, &szind)) {\n\t\treturn 0;\n\t}\n\n\tif (extent == NULL) {\n\t\treturn 0;\n\t}\n\tassert(extent_state_get(extent) == extent_state_active);\n\t/* Only slab members should be looked up via interior pointers. */\n\tassert(extent_addr_get(extent) == ptr || extent_slab_get(extent));\n\n\tassert(szind != SC_NSIZES);\n\n\treturn sz_index2size(szind);\n}\n\nstatic inline void\narena_dalloc_large_no_tcache(tsdn_t *tsdn, void *ptr, szind_t szind) {\n\tif (config_prof && unlikely(szind < SC_NBINS)) {\n\t\tarena_dalloc_promoted(tsdn, ptr, NULL, true);\n\t} else {\n\t\textent_t *extent = iealloc(tsdn, ptr);\n\t\tlarge_dalloc(tsdn, extent);\n\t}\n}\n\nstatic inline void\narena_dalloc_no_tcache(tsdn_t *tsdn, void *ptr) {\n\tassert(ptr != NULL);\n\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\n\tszind_t szind;\n\tbool slab;\n\trtree_szind_slab_read(tsdn, &extents_rtree, rtree_ctx, (uintptr_t)ptr,\n\t    true, &szind, &slab);\n\n\tif (config_debug) {\n\t\textent_t *extent = rtree_extent_read(tsdn, &extents_rtree,\n\t\t    rtree_ctx, (uintptr_t)ptr, true);\n\t\tassert(szind == extent_szind_get(extent));\n\t\tassert(szind < SC_NSIZES);\n\t\tassert(slab == extent_slab_get(extent));\n\t}\n\n\tif (likely(slab)) {\n\t\t/* Small allocation. */\n\t\tarena_dalloc_small(tsdn, ptr);\n\t} else {\n\t\tarena_dalloc_large_no_tcache(tsdn, ptr, szind);\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE void\narena_dalloc_large(tsdn_t *tsdn, void *ptr, tcache_t *tcache, szind_t szind,\n    bool slow_path) {\n\tif (szind < nhbins) {\n\t\tif (config_prof && unlikely(szind < SC_NBINS)) {\n\t\t\tarena_dalloc_promoted(tsdn, ptr, tcache, slow_path);\n\t\t} else {\n\t\t\ttcache_dalloc_large(tsdn_tsd(tsdn), tcache, ptr, szind,\n\t\t\t    slow_path);\n\t\t}\n\t} else {\n\t\textent_t *extent = iealloc(tsdn, ptr);\n\t\tlarge_dalloc(tsdn, extent);\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE void\narena_dalloc(tsdn_t *tsdn, void *ptr, tcache_t *tcache,\n    alloc_ctx_t *alloc_ctx, bool slow_path) {\n\tassert(!tsdn_null(tsdn) || tcache == NULL);\n\tassert(ptr != NULL);\n\n\tif (unlikely(tcache == NULL)) {\n\t\tarena_dalloc_no_tcache(tsdn, ptr);\n\t\treturn;\n\t}\n\n\tszind_t szind;\n\tbool slab;\n\trtree_ctx_t *rtree_ctx;\n\tif (alloc_ctx != NULL) {\n\t\tszind = alloc_ctx->szind;\n\t\tslab = alloc_ctx->slab;\n\t\tassert(szind != SC_NSIZES);\n\t} else {\n\t\trtree_ctx = tsd_rtree_ctx(tsdn_tsd(tsdn));\n\t\trtree_szind_slab_read(tsdn, &extents_rtree, rtree_ctx,\n\t\t    (uintptr_t)ptr, true, &szind, &slab);\n\t}\n\n\tif (config_debug) {\n\t\trtree_ctx = tsd_rtree_ctx(tsdn_tsd(tsdn));\n\t\textent_t *extent = rtree_extent_read(tsdn, &extents_rtree,\n\t\t    rtree_ctx, (uintptr_t)ptr, true);\n\t\tassert(szind == extent_szind_get(extent));\n\t\tassert(szind < SC_NSIZES);\n\t\tassert(slab == extent_slab_get(extent));\n\t}\n\n\tif (likely(slab)) {\n\t\t/* Small allocation. */\n\t\ttcache_dalloc_small(tsdn_tsd(tsdn), tcache, ptr, szind,\n\t\t    slow_path);\n\t} else {\n\t\tarena_dalloc_large(tsdn, ptr, tcache, szind, slow_path);\n\t}\n}\n\nstatic inline void\narena_sdalloc_no_tcache(tsdn_t *tsdn, void *ptr, size_t size) {\n\tassert(ptr != NULL);\n\tassert(size <= SC_LARGE_MAXCLASS);\n\n\tszind_t szind;\n\tbool slab;\n\tif (!config_prof || !opt_prof) {\n\t\t/*\n\t\t * There is no risk of being confused by a promoted sampled\n\t\t * object, so base szind and slab on the given size.\n\t\t */\n\t\tszind = sz_size2index(size);\n\t\tslab = (szind < SC_NBINS);\n\t}\n\n\tif ((config_prof && opt_prof) || config_debug) {\n\t\trtree_ctx_t rtree_ctx_fallback;\n\t\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn,\n\t\t    &rtree_ctx_fallback);\n\n\t\trtree_szind_slab_read(tsdn, &extents_rtree, rtree_ctx,\n\t\t    (uintptr_t)ptr, true, &szind, &slab);\n\n\t\tassert(szind == sz_size2index(size));\n\t\tassert((config_prof && opt_prof) || slab == (szind < SC_NBINS));\n\n\t\tif (config_debug) {\n\t\t\textent_t *extent = rtree_extent_read(tsdn,\n\t\t\t    &extents_rtree, rtree_ctx, (uintptr_t)ptr, true);\n\t\t\tassert(szind == extent_szind_get(extent));\n\t\t\tassert(slab == extent_slab_get(extent));\n\t\t}\n\t}\n\n\tif (likely(slab)) {\n\t\t/* Small allocation. */\n\t\tarena_dalloc_small(tsdn, ptr);\n\t} else {\n\t\tarena_dalloc_large_no_tcache(tsdn, ptr, szind);\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE void\narena_sdalloc(tsdn_t *tsdn, void *ptr, size_t size, tcache_t *tcache,\n    alloc_ctx_t *alloc_ctx, bool slow_path) {\n\tassert(!tsdn_null(tsdn) || tcache == NULL);\n\tassert(ptr != NULL);\n\tassert(size <= SC_LARGE_MAXCLASS);\n\n\tif (unlikely(tcache == NULL)) {\n\t\tarena_sdalloc_no_tcache(tsdn, ptr, size);\n\t\treturn;\n\t}\n\n\tszind_t szind;\n\tbool slab;\n\talloc_ctx_t local_ctx;\n\tif (config_prof && opt_prof) {\n\t\tif (alloc_ctx == NULL) {\n\t\t\t/* Uncommon case and should be a static check. */\n\t\t\trtree_ctx_t rtree_ctx_fallback;\n\t\t\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn,\n\t\t\t    &rtree_ctx_fallback);\n\t\t\trtree_szind_slab_read(tsdn, &extents_rtree, rtree_ctx,\n\t\t\t    (uintptr_t)ptr, true, &local_ctx.szind,\n\t\t\t    &local_ctx.slab);\n\t\t\tassert(local_ctx.szind == sz_size2index(size));\n\t\t\talloc_ctx = &local_ctx;\n\t\t}\n\t\tslab = alloc_ctx->slab;\n\t\tszind = alloc_ctx->szind;\n\t} else {\n\t\t/*\n\t\t * There is no risk of being confused by a promoted sampled\n\t\t * object, so base szind and slab on the given size.\n\t\t */\n\t\tszind = sz_size2index(size);\n\t\tslab = (szind < SC_NBINS);\n\t}\n\n\tif (config_debug) {\n\t\trtree_ctx_t *rtree_ctx = tsd_rtree_ctx(tsdn_tsd(tsdn));\n\t\trtree_szind_slab_read(tsdn, &extents_rtree, rtree_ctx,\n\t\t    (uintptr_t)ptr, true, &szind, &slab);\n\t\textent_t *extent = rtree_extent_read(tsdn,\n\t\t    &extents_rtree, rtree_ctx, (uintptr_t)ptr, true);\n\t\tassert(szind == extent_szind_get(extent));\n\t\tassert(slab == extent_slab_get(extent));\n\t}\n\n\tif (likely(slab)) {\n\t\t/* Small allocation. */\n\t\ttcache_dalloc_small(tsdn_tsd(tsdn), tcache, ptr, szind,\n\t\t    slow_path);\n\t} else {\n\t\tarena_dalloc_large(tsdn, ptr, tcache, szind, slow_path);\n\t}\n}\n\n#endif /* JEMALLOC_INTERNAL_ARENA_INLINES_B_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/arena_stats.h",
    "content": "#ifndef JEMALLOC_INTERNAL_ARENA_STATS_H\n#define JEMALLOC_INTERNAL_ARENA_STATS_H\n\n#include \"jemalloc/internal/atomic.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/mutex_prof.h\"\n#include \"jemalloc/internal/sc.h\"\n\nJEMALLOC_DIAGNOSTIC_DISABLE_SPURIOUS\n\n/*\n * In those architectures that support 64-bit atomics, we use atomic updates for\n * our 64-bit values.  Otherwise, we use a plain uint64_t and synchronize\n * externally.\n */\n#ifdef JEMALLOC_ATOMIC_U64\ntypedef atomic_u64_t arena_stats_u64_t;\n#else\n/* Must hold the arena stats mutex while reading atomically. */\ntypedef uint64_t arena_stats_u64_t;\n#endif\n\ntypedef struct arena_stats_large_s arena_stats_large_t;\nstruct arena_stats_large_s {\n\t/*\n\t * Total number of allocation/deallocation requests served directly by\n\t * the arena.\n\t */\n\tarena_stats_u64_t\tnmalloc;\n\tarena_stats_u64_t\tndalloc;\n\n\t/*\n\t * Number of allocation requests that correspond to this size class.\n\t * This includes requests served by tcache, though tcache only\n\t * periodically merges into this counter.\n\t */\n\tarena_stats_u64_t\tnrequests; /* Partially derived. */\n\t/*\n\t * Number of tcache fills / flushes for large (similarly, periodically\n\t * merged).  Note that there is no large tcache batch-fill currently\n\t * (i.e. only fill 1 at a time); however flush may be batched.\n\t */\n\tarena_stats_u64_t\tnfills; /* Partially derived. */\n\tarena_stats_u64_t\tnflushes; /* Partially derived. */\n\n\t/* Current number of allocations of this size class. */\n\tsize_t\t\tcurlextents; /* Derived. */\n};\n\ntypedef struct arena_stats_decay_s arena_stats_decay_t;\nstruct arena_stats_decay_s {\n\t/* Total number of purge sweeps. */\n\tarena_stats_u64_t\tnpurge;\n\t/* Total number of madvise calls made. */\n\tarena_stats_u64_t\tnmadvise;\n\t/* Total number of pages purged. */\n\tarena_stats_u64_t\tpurged;\n};\n\ntypedef struct arena_stats_extents_s arena_stats_extents_t;\nstruct arena_stats_extents_s {\n\t/*\n\t * Stats for a given index in the range [0, SC_NPSIZES] in an extents_t.\n\t * We track both bytes and # of extents: two extents in the same bucket\n\t * may have different sizes if adjacent size classes differ by more than\n\t * a page, so bytes cannot always be derived from # of extents.\n\t */\n\tatomic_zu_t ndirty;\n\tatomic_zu_t dirty_bytes;\n\tatomic_zu_t nmuzzy;\n\tatomic_zu_t muzzy_bytes;\n\tatomic_zu_t nretained;\n\tatomic_zu_t retained_bytes;\n};\n\n/*\n * Arena stats.  Note that fields marked \"derived\" are not directly maintained\n * within the arena code; rather their values are derived during stats merge\n * requests.\n */\ntypedef struct arena_stats_s arena_stats_t;\nstruct arena_stats_s {\n#ifndef JEMALLOC_ATOMIC_U64\n\tmalloc_mutex_t\t\tmtx;\n#endif\n\n\t/* Number of bytes currently mapped, excluding retained memory. */\n\tatomic_zu_t\t\tmapped; /* Partially derived. */\n\n\t/*\n\t * Number of unused virtual memory bytes currently retained.  Retained\n\t * bytes are technically mapped (though always decommitted or purged),\n\t * but they are excluded from the mapped statistic (above).\n\t */\n\tatomic_zu_t\t\tretained; /* Derived. */\n\n\t/* Number of extent_t structs allocated by base, but not being used. */\n\tatomic_zu_t\t\textent_avail;\n\n\tarena_stats_decay_t\tdecay_dirty;\n\tarena_stats_decay_t\tdecay_muzzy;\n\n\tatomic_zu_t\t\tbase; /* Derived. */\n\tatomic_zu_t\t\tinternal;\n\tatomic_zu_t\t\tresident; /* Derived. */\n\tatomic_zu_t\t\tmetadata_thp;\n\n\tatomic_zu_t\t\tallocated_large; /* Derived. */\n\tarena_stats_u64_t\tnmalloc_large; /* Derived. */\n\tarena_stats_u64_t\tndalloc_large; /* Derived. */\n\tarena_stats_u64_t\tnfills_large; /* Derived. */\n\tarena_stats_u64_t\tnflushes_large; /* Derived. */\n\tarena_stats_u64_t\tnrequests_large; /* Derived. */\n\n\t/* VM space had to be leaked (undocumented).  Normally 0. */\n\tatomic_zu_t\t\tabandoned_vm;\n\n\t/* Number of bytes cached in tcache associated with this arena. */\n\tatomic_zu_t\t\ttcache_bytes; /* Derived. */\n\n\tmutex_prof_data_t mutex_prof_data[mutex_prof_num_arena_mutexes];\n\n\t/* One element for each large size class. */\n\tarena_stats_large_t\tlstats[SC_NSIZES - SC_NBINS];\n\n\t/* Arena uptime. */\n\tnstime_t\t\tuptime;\n};\n\nstatic inline bool\narena_stats_init(tsdn_t *tsdn, arena_stats_t *arena_stats) {\n\tif (config_debug) {\n\t\tfor (size_t i = 0; i < sizeof(arena_stats_t); i++) {\n\t\t\tassert(((char *)arena_stats)[i] == 0);\n\t\t}\n\t}\n#ifndef JEMALLOC_ATOMIC_U64\n\tif (malloc_mutex_init(&arena_stats->mtx, \"arena_stats\",\n\t    WITNESS_RANK_ARENA_STATS, malloc_mutex_rank_exclusive)) {\n\t\treturn true;\n\t}\n#endif\n\t/* Memory is zeroed, so there is no need to clear stats. */\n\treturn false;\n}\n\nstatic inline void\narena_stats_lock(tsdn_t *tsdn, arena_stats_t *arena_stats) {\n#ifndef JEMALLOC_ATOMIC_U64\n\tmalloc_mutex_lock(tsdn, &arena_stats->mtx);\n#endif\n}\n\nstatic inline void\narena_stats_unlock(tsdn_t *tsdn, arena_stats_t *arena_stats) {\n#ifndef JEMALLOC_ATOMIC_U64\n\tmalloc_mutex_unlock(tsdn, &arena_stats->mtx);\n#endif\n}\n\nstatic inline uint64_t\narena_stats_read_u64(tsdn_t *tsdn, arena_stats_t *arena_stats,\n    arena_stats_u64_t *p) {\n#ifdef JEMALLOC_ATOMIC_U64\n\treturn atomic_load_u64(p, ATOMIC_RELAXED);\n#else\n\tmalloc_mutex_assert_owner(tsdn, &arena_stats->mtx);\n\treturn *p;\n#endif\n}\n\nstatic inline void\narena_stats_add_u64(tsdn_t *tsdn, arena_stats_t *arena_stats,\n    arena_stats_u64_t *p, uint64_t x) {\n#ifdef JEMALLOC_ATOMIC_U64\n\tatomic_fetch_add_u64(p, x, ATOMIC_RELAXED);\n#else\n\tmalloc_mutex_assert_owner(tsdn, &arena_stats->mtx);\n\t*p += x;\n#endif\n}\n\nstatic inline void\narena_stats_sub_u64(tsdn_t *tsdn, arena_stats_t *arena_stats,\n    arena_stats_u64_t *p, uint64_t x) {\n#ifdef JEMALLOC_ATOMIC_U64\n\tuint64_t r = atomic_fetch_sub_u64(p, x, ATOMIC_RELAXED);\n\tassert(r - x <= r);\n#else\n\tmalloc_mutex_assert_owner(tsdn, &arena_stats->mtx);\n\t*p -= x;\n\tassert(*p + x >= *p);\n#endif\n}\n\n/*\n * Non-atomically sets *dst += src.  *dst needs external synchronization.\n * This lets us avoid the cost of a fetch_add when its unnecessary (note that\n * the types here are atomic).\n */\nstatic inline void\narena_stats_accum_u64(arena_stats_u64_t *dst, uint64_t src) {\n#ifdef JEMALLOC_ATOMIC_U64\n\tuint64_t cur_dst = atomic_load_u64(dst, ATOMIC_RELAXED);\n\tatomic_store_u64(dst, src + cur_dst, ATOMIC_RELAXED);\n#else\n\t*dst += src;\n#endif\n}\n\nstatic inline size_t\narena_stats_read_zu(tsdn_t *tsdn, arena_stats_t *arena_stats,\n    atomic_zu_t *p) {\n#ifdef JEMALLOC_ATOMIC_U64\n\treturn atomic_load_zu(p, ATOMIC_RELAXED);\n#else\n\tmalloc_mutex_assert_owner(tsdn, &arena_stats->mtx);\n\treturn atomic_load_zu(p, ATOMIC_RELAXED);\n#endif\n}\n\nstatic inline void\narena_stats_add_zu(tsdn_t *tsdn, arena_stats_t *arena_stats,\n    atomic_zu_t *p, size_t x) {\n#ifdef JEMALLOC_ATOMIC_U64\n\tatomic_fetch_add_zu(p, x, ATOMIC_RELAXED);\n#else\n\tmalloc_mutex_assert_owner(tsdn, &arena_stats->mtx);\n\tsize_t cur = atomic_load_zu(p, ATOMIC_RELAXED);\n\tatomic_store_zu(p, cur + x, ATOMIC_RELAXED);\n#endif\n}\n\nstatic inline void\narena_stats_sub_zu(tsdn_t *tsdn, arena_stats_t *arena_stats,\n    atomic_zu_t *p, size_t x) {\n#ifdef JEMALLOC_ATOMIC_U64\n\tsize_t r = atomic_fetch_sub_zu(p, x, ATOMIC_RELAXED);\n\tassert(r - x <= r);\n#else\n\tmalloc_mutex_assert_owner(tsdn, &arena_stats->mtx);\n\tsize_t cur = atomic_load_zu(p, ATOMIC_RELAXED);\n\tatomic_store_zu(p, cur - x, ATOMIC_RELAXED);\n#endif\n}\n\n/* Like the _u64 variant, needs an externally synchronized *dst. */\nstatic inline void\narena_stats_accum_zu(atomic_zu_t *dst, size_t src) {\n\tsize_t cur_dst = atomic_load_zu(dst, ATOMIC_RELAXED);\n\tatomic_store_zu(dst, src + cur_dst, ATOMIC_RELAXED);\n}\n\nstatic inline void\narena_stats_large_flush_nrequests_add(tsdn_t *tsdn, arena_stats_t *arena_stats,\n    szind_t szind, uint64_t nrequests) {\n\tarena_stats_lock(tsdn, arena_stats);\n\tarena_stats_large_t *lstats = &arena_stats->lstats[szind - SC_NBINS];\n\tarena_stats_add_u64(tsdn, arena_stats, &lstats->nrequests, nrequests);\n\tarena_stats_add_u64(tsdn, arena_stats, &lstats->nflushes, 1);\n\tarena_stats_unlock(tsdn, arena_stats);\n}\n\nstatic inline void\narena_stats_mapped_add(tsdn_t *tsdn, arena_stats_t *arena_stats, size_t size) {\n\tarena_stats_lock(tsdn, arena_stats);\n\tarena_stats_add_zu(tsdn, arena_stats, &arena_stats->mapped, size);\n\tarena_stats_unlock(tsdn, arena_stats);\n}\n\n#endif /* JEMALLOC_INTERNAL_ARENA_STATS_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/arena_structs_a.h",
    "content": "#ifndef JEMALLOC_INTERNAL_ARENA_STRUCTS_A_H\n#define JEMALLOC_INTERNAL_ARENA_STRUCTS_A_H\n\n#include \"jemalloc/internal/bitmap.h\"\n\nstruct arena_slab_data_s {\n\t/* Per region allocated/deallocated bitmap. */\n\tbitmap_t\tbitmap[BITMAP_GROUPS_MAX];\n};\n\n#endif /* JEMALLOC_INTERNAL_ARENA_STRUCTS_A_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/arena_structs_b.h",
    "content": "#ifndef JEMALLOC_INTERNAL_ARENA_STRUCTS_B_H\n#define JEMALLOC_INTERNAL_ARENA_STRUCTS_B_H\n\n#include \"jemalloc/internal/arena_stats.h\"\n#include \"jemalloc/internal/atomic.h\"\n#include \"jemalloc/internal/bin.h\"\n#include \"jemalloc/internal/bitmap.h\"\n#include \"jemalloc/internal/extent_dss.h\"\n#include \"jemalloc/internal/jemalloc_internal_types.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/nstime.h\"\n#include \"jemalloc/internal/ql.h\"\n#include \"jemalloc/internal/sc.h\"\n#include \"jemalloc/internal/smoothstep.h\"\n#include \"jemalloc/internal/ticker.h\"\n\nstruct arena_decay_s {\n\t/* Synchronizes all non-atomic fields. */\n\tmalloc_mutex_t\t\tmtx;\n\t/*\n\t * True if a thread is currently purging the extents associated with\n\t * this decay structure.\n\t */\n\tbool\t\t\tpurging;\n\t/*\n\t * Approximate time in milliseconds from the creation of a set of unused\n\t * dirty pages until an equivalent set of unused dirty pages is purged\n\t * and/or reused.\n\t */\n\tatomic_zd_t\t\ttime_ms;\n\t/* time / SMOOTHSTEP_NSTEPS. */\n\tnstime_t\t\tinterval;\n\t/*\n\t * Time at which the current decay interval logically started.  We do\n\t * not actually advance to a new epoch until sometime after it starts\n\t * because of scheduling and computation delays, and it is even possible\n\t * to completely skip epochs.  In all cases, during epoch advancement we\n\t * merge all relevant activity into the most recently recorded epoch.\n\t */\n\tnstime_t\t\tepoch;\n\t/* Deadline randomness generator. */\n\tuint64_t\t\tjitter_state;\n\t/*\n\t * Deadline for current epoch.  This is the sum of interval and per\n\t * epoch jitter which is a uniform random variable in [0..interval).\n\t * Epochs always advance by precise multiples of interval, but we\n\t * randomize the deadline to reduce the likelihood of arenas purging in\n\t * lockstep.\n\t */\n\tnstime_t\t\tdeadline;\n\t/*\n\t * Number of unpurged pages at beginning of current epoch.  During epoch\n\t * advancement we use the delta between arena->decay_*.nunpurged and\n\t * extents_npages_get(&arena->extents_*) to determine how many dirty\n\t * pages, if any, were generated.\n\t */\n\tsize_t\t\t\tnunpurged;\n\t/*\n\t * Trailing log of how many unused dirty pages were generated during\n\t * each of the past SMOOTHSTEP_NSTEPS decay epochs, where the last\n\t * element is the most recent epoch.  Corresponding epoch times are\n\t * relative to epoch.\n\t */\n\tsize_t\t\t\tbacklog[SMOOTHSTEP_NSTEPS];\n\n\t/*\n\t * Pointer to associated stats.  These stats are embedded directly in\n\t * the arena's stats due to how stats structures are shared between the\n\t * arena and ctl code.\n\t *\n\t * Synchronization: Same as associated arena's stats field. */\n\tarena_stats_decay_t\t*stats;\n\t/* Peak number of pages in associated extents.  Used for debug only. */\n\tuint64_t\t\tceil_npages;\n};\n\nstruct arena_s {\n\t/*\n\t * Number of threads currently assigned to this arena.  Each thread has\n\t * two distinct assignments, one for application-serving allocation, and\n\t * the other for internal metadata allocation.  Internal metadata must\n\t * not be allocated from arenas explicitly created via the arenas.create\n\t * mallctl, because the arena.<i>.reset mallctl indiscriminately\n\t * discards all allocations for the affected arena.\n\t *\n\t *   0: Application allocation.\n\t *   1: Internal metadata allocation.\n\t *\n\t * Synchronization: atomic.\n\t */\n\tatomic_u_t\t\tnthreads[2];\n\n\t/* Next bin shard for binding new threads. Synchronization: atomic. */\n\tatomic_u_t\t\tbinshard_next;\n\n\t/*\n\t * When percpu_arena is enabled, to amortize the cost of reading /\n\t * updating the current CPU id, track the most recent thread accessing\n\t * this arena, and only read CPU if there is a mismatch.\n\t */\n\ttsdn_t\t\t*last_thd;\n\n\t/* Synchronization: internal. */\n\tarena_stats_t\t\tstats;\n\n\t/*\n\t * Lists of tcaches and cache_bin_array_descriptors for extant threads\n\t * associated with this arena.  Stats from these are merged\n\t * incrementally, and at exit if opt_stats_print is enabled.\n\t *\n\t * Synchronization: tcache_ql_mtx.\n\t */\n\tql_head(tcache_t)\t\t\ttcache_ql;\n\tql_head(cache_bin_array_descriptor_t)\tcache_bin_array_descriptor_ql;\n\tmalloc_mutex_t\t\t\t\ttcache_ql_mtx;\n\n\t/* Synchronization: internal. */\n\tprof_accum_t\t\tprof_accum;\n\n\t/*\n\t * PRNG state for cache index randomization of large allocation base\n\t * pointers.\n\t *\n\t * Synchronization: atomic.\n\t */\n\tatomic_zu_t\t\toffset_state;\n\n\t/*\n\t * Extent serial number generator state.\n\t *\n\t * Synchronization: atomic.\n\t */\n\tatomic_zu_t\t\textent_sn_next;\n\n\t/*\n\t * Represents a dss_prec_t, but atomically.\n\t *\n\t * Synchronization: atomic.\n\t */\n\tatomic_u_t\t\tdss_prec;\n\n\t/*\n\t * Number of pages in active extents.\n\t *\n\t * Synchronization: atomic.\n\t */\n\tatomic_zu_t\t\tnactive;\n\n\t/*\n\t * Extant large allocations.\n\t *\n\t * Synchronization: large_mtx.\n\t */\n\textent_list_t\t\tlarge;\n\t/* Synchronizes all large allocation/update/deallocation. */\n\tmalloc_mutex_t\t\tlarge_mtx;\n\n\t/*\n\t * Collections of extents that were previously allocated.  These are\n\t * used when allocating extents, in an attempt to re-use address space.\n\t *\n\t * Synchronization: internal.\n\t */\n\textents_t\t\textents_dirty;\n\textents_t\t\textents_muzzy;\n\textents_t\t\textents_retained;\n\n\t/*\n\t * Decay-based purging state, responsible for scheduling extent state\n\t * transitions.\n\t *\n\t * Synchronization: internal.\n\t */\n\tarena_decay_t\t\tdecay_dirty; /* dirty --> muzzy */\n\tarena_decay_t\t\tdecay_muzzy; /* muzzy --> retained */\n\n\t/*\n\t * Next extent size class in a growing series to use when satisfying a\n\t * request via the extent hooks (only if opt_retain).  This limits the\n\t * number of disjoint virtual memory ranges so that extent merging can\n\t * be effective even if multiple arenas' extent allocation requests are\n\t * highly interleaved.\n\t *\n\t * retain_grow_limit is the max allowed size ind to expand (unless the\n\t * required size is greater).  Default is no limit, and controlled\n\t * through mallctl only.\n\t *\n\t * Synchronization: extent_grow_mtx\n\t */\n\tpszind_t\t\textent_grow_next;\n\tpszind_t\t\tretain_grow_limit;\n\tmalloc_mutex_t\t\textent_grow_mtx;\n\n\t/*\n\t * Available extent structures that were allocated via\n\t * base_alloc_extent().\n\t *\n\t * Synchronization: extent_avail_mtx.\n\t */\n\textent_tree_t\t\textent_avail;\n\tatomic_zu_t\t\textent_avail_cnt;\n\tmalloc_mutex_t\t\textent_avail_mtx;\n\n\t/*\n\t * bins is used to store heaps of free regions.\n\t *\n\t * Synchronization: internal.\n\t */\n\tbins_t\t\t\tbins[SC_NBINS];\n\n\t/*\n\t * Base allocator, from which arena metadata are allocated.\n\t *\n\t * Synchronization: internal.\n\t */\n\tbase_t\t\t\t*base;\n\t/* Used to determine uptime.  Read-only after initialization. */\n\tnstime_t\t\tcreate_time;\n};\n\n/* Used in conjunction with tsd for fast arena-related context lookup. */\nstruct arena_tdata_s {\n\tticker_t\t\tdecay_ticker;\n};\n\n/* Used to pass rtree lookup context down the path. */\nstruct alloc_ctx_s {\n\tszind_t szind;\n\tbool slab;\n};\n\n#endif /* JEMALLOC_INTERNAL_ARENA_STRUCTS_B_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/arena_types.h",
    "content": "#ifndef JEMALLOC_INTERNAL_ARENA_TYPES_H\n#define JEMALLOC_INTERNAL_ARENA_TYPES_H\n\n#include \"jemalloc/internal/sc.h\"\n\n/* Maximum number of regions in one slab. */\n#define LG_SLAB_MAXREGS\t\t(LG_PAGE - SC_LG_TINY_MIN)\n#define SLAB_MAXREGS\t\t(1U << LG_SLAB_MAXREGS)\n\n/* Default decay times in milliseconds. */\n#define DIRTY_DECAY_MS_DEFAULT\tZD(10 * 1000)\n#define MUZZY_DECAY_MS_DEFAULT\t(0)\n/* Number of event ticks between time checks. */\n#define DECAY_NTICKS_PER_UPDATE\t1000\n\ntypedef struct arena_slab_data_s arena_slab_data_t;\ntypedef struct arena_decay_s arena_decay_t;\ntypedef struct arena_s arena_t;\ntypedef struct arena_tdata_s arena_tdata_t;\ntypedef struct alloc_ctx_s alloc_ctx_t;\n\ntypedef enum {\n\tpercpu_arena_mode_names_base   = 0, /* Used for options processing. */\n\n\t/*\n\t * *_uninit are used only during bootstrapping, and must correspond\n\t * to initialized variant plus percpu_arena_mode_enabled_base.\n\t */\n\tpercpu_arena_uninit            = 0,\n\tper_phycpu_arena_uninit        = 1,\n\n\t/* All non-disabled modes must come after percpu_arena_disabled. */\n\tpercpu_arena_disabled          = 2,\n\n\tpercpu_arena_mode_names_limit  = 3, /* Used for options processing. */\n\tpercpu_arena_mode_enabled_base = 3,\n\n\tpercpu_arena                   = 3,\n\tper_phycpu_arena               = 4  /* Hyper threads share arena. */\n} percpu_arena_mode_t;\n\n#define PERCPU_ARENA_ENABLED(m)\t((m) >= percpu_arena_mode_enabled_base)\n#define PERCPU_ARENA_DEFAULT\tpercpu_arena_disabled\n\n/*\n * When allocation_size >= oversize_threshold, use the dedicated huge arena\n * (unless have explicitly spicified arena index).  0 disables the feature.\n */\n#define OVERSIZE_THRESHOLD_DEFAULT (8 << 20)\n\n#endif /* JEMALLOC_INTERNAL_ARENA_TYPES_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/assert.h",
    "content": "#include \"jemalloc/internal/malloc_io.h\"\n#include \"jemalloc/internal/util.h\"\n\n/*\n * Define a custom assert() in order to reduce the chances of deadlock during\n * assertion failure.\n */\n#ifndef assert\n#define assert(e) do {\t\t\t\t\t\t\t\\\n\tif (unlikely(config_debug && !(e))) {\t\t\t\t\\\n\t\tmalloc_printf(\t\t\t\t\t\t\\\n\t\t    \"<jemalloc>: %s:%d: Failed assertion: \\\"%s\\\"\\n\",\t\\\n\t\t    __FILE__, __LINE__, #e);\t\t\t\t\\\n\t\tabort();\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n#endif\n\n#ifndef not_reached\n#define not_reached() do {\t\t\t\t\t\t\\\n\tif (config_debug) {\t\t\t\t\t\t\\\n\t\tmalloc_printf(\t\t\t\t\t\t\\\n\t\t    \"<jemalloc>: %s:%d: Unreachable code reached\\n\",\t\\\n\t\t    __FILE__, __LINE__);\t\t\t\t\\\n\t\tabort();\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tunreachable();\t\t\t\t\t\t\t\\\n} while (0)\n#endif\n\n#ifndef not_implemented\n#define not_implemented() do {\t\t\t\t\t\t\\\n\tif (config_debug) {\t\t\t\t\t\t\\\n\t\tmalloc_printf(\"<jemalloc>: %s:%d: Not implemented\\n\",\t\\\n\t\t    __FILE__, __LINE__);\t\t\t\t\\\n\t\tabort();\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n#endif\n\n#ifndef assert_not_implemented\n#define assert_not_implemented(e) do {\t\t\t\t\t\\\n\tif (unlikely(config_debug && !(e))) {\t\t\t\t\\\n\t\tnot_implemented();\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n#endif\n\n/* Use to assert a particular configuration, e.g., cassert(config_debug). */\n#ifndef cassert\n#define cassert(c) do {\t\t\t\t\t\t\t\\\n\tif (unlikely(!(c))) {\t\t\t\t\t\t\\\n\t\tnot_reached();\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n#endif\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/atomic.h",
    "content": "#ifndef JEMALLOC_INTERNAL_ATOMIC_H\n#define JEMALLOC_INTERNAL_ATOMIC_H\n\n#define ATOMIC_INLINE JEMALLOC_ALWAYS_INLINE\n\n#define JEMALLOC_U8_ATOMICS\n#if defined(JEMALLOC_GCC_ATOMIC_ATOMICS)\n#  include \"jemalloc/internal/atomic_gcc_atomic.h\"\n#  if !defined(JEMALLOC_GCC_U8_ATOMIC_ATOMICS)\n#    undef JEMALLOC_U8_ATOMICS\n#  endif\n#elif defined(JEMALLOC_GCC_SYNC_ATOMICS)\n#  include \"jemalloc/internal/atomic_gcc_sync.h\"\n#  if !defined(JEMALLOC_GCC_U8_SYNC_ATOMICS)\n#    undef JEMALLOC_U8_ATOMICS\n#  endif\n#elif defined(_MSC_VER)\n#  include \"jemalloc/internal/atomic_msvc.h\"\n#elif defined(JEMALLOC_C11_ATOMICS)\n#  include \"jemalloc/internal/atomic_c11.h\"\n#else\n#  error \"Don't have atomics implemented on this platform.\"\n#endif\n\n/*\n * This header gives more or less a backport of C11 atomics. The user can write\n * JEMALLOC_GENERATE_ATOMICS(type, short_type, lg_sizeof_type); to generate\n * counterparts of the C11 atomic functions for type, as so:\n *   JEMALLOC_GENERATE_ATOMICS(int *, pi, 3);\n * and then write things like:\n *   int *some_ptr;\n *   atomic_pi_t atomic_ptr_to_int;\n *   atomic_store_pi(&atomic_ptr_to_int, some_ptr, ATOMIC_RELAXED);\n *   int *prev_value = atomic_exchange_pi(&ptr_to_int, NULL, ATOMIC_ACQ_REL);\n *   assert(some_ptr == prev_value);\n * and expect things to work in the obvious way.\n *\n * Also included (with naming differences to avoid conflicts with the standard\n * library):\n *   atomic_fence(atomic_memory_order_t) (mimics C11's atomic_thread_fence).\n *   ATOMIC_INIT (mimics C11's ATOMIC_VAR_INIT).\n */\n\n/*\n * Pure convenience, so that we don't have to type \"atomic_memory_order_\"\n * quite so often.\n */\n#define ATOMIC_RELAXED atomic_memory_order_relaxed\n#define ATOMIC_ACQUIRE atomic_memory_order_acquire\n#define ATOMIC_RELEASE atomic_memory_order_release\n#define ATOMIC_ACQ_REL atomic_memory_order_acq_rel\n#define ATOMIC_SEQ_CST atomic_memory_order_seq_cst\n\n/*\n * Not all platforms have 64-bit atomics.  If we do, this #define exposes that\n * fact.\n */\n#if (LG_SIZEOF_PTR == 3 || LG_SIZEOF_INT == 3)\n#  define JEMALLOC_ATOMIC_U64\n#endif\n\nJEMALLOC_GENERATE_ATOMICS(void *, p, LG_SIZEOF_PTR)\n\n/*\n * There's no actual guarantee that sizeof(bool) == 1, but it's true on the only\n * platform that actually needs to know the size, MSVC.\n */\nJEMALLOC_GENERATE_ATOMICS(bool, b, 0)\n\nJEMALLOC_GENERATE_INT_ATOMICS(unsigned, u, LG_SIZEOF_INT)\n\nJEMALLOC_GENERATE_INT_ATOMICS(size_t, zu, LG_SIZEOF_PTR)\n\nJEMALLOC_GENERATE_INT_ATOMICS(ssize_t, zd, LG_SIZEOF_PTR)\n\nJEMALLOC_GENERATE_INT_ATOMICS(uint8_t, u8, 0)\n\nJEMALLOC_GENERATE_INT_ATOMICS(uint32_t, u32, 2)\n\n#ifdef JEMALLOC_ATOMIC_U64\nJEMALLOC_GENERATE_INT_ATOMICS(uint64_t, u64, 3)\n#endif\n\n#undef ATOMIC_INLINE\n\n#endif /* JEMALLOC_INTERNAL_ATOMIC_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/atomic_c11.h",
    "content": "#ifndef JEMALLOC_INTERNAL_ATOMIC_C11_H\n#define JEMALLOC_INTERNAL_ATOMIC_C11_H\n\n#include <stdatomic.h>\n\n#define ATOMIC_INIT(...) ATOMIC_VAR_INIT(__VA_ARGS__)\n\n#define atomic_memory_order_t memory_order\n#define atomic_memory_order_relaxed memory_order_relaxed\n#define atomic_memory_order_acquire memory_order_acquire\n#define atomic_memory_order_release memory_order_release\n#define atomic_memory_order_acq_rel memory_order_acq_rel\n#define atomic_memory_order_seq_cst memory_order_seq_cst\n\n#define atomic_fence atomic_thread_fence\n\n#define JEMALLOC_GENERATE_ATOMICS(type, short_type,\t\t\t\\\n    /* unused */ lg_size)\t\t\t\t\t\t\\\ntypedef _Atomic(type) atomic_##short_type##_t;\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_load_##short_type(const atomic_##short_type##_t *a,\t\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\t/*\t\t\t\t\t\t\t\t\\\n\t * A strict interpretation of the C standard prevents\t\t\\\n\t * atomic_load from taking a const argument, but it's\t\t\\\n\t * convenient for our purposes. This cast is a workaround.\t\\\n\t */\t\t\t\t\t\t\t\t\\\n\tatomic_##short_type##_t* a_nonconst =\t\t\t\t\\\n\t    (atomic_##short_type##_t*)a;\t\t\t\t\\\n\treturn atomic_load_explicit(a_nonconst, mo);\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE void\t\t\t\t\t\t\t\\\natomic_store_##short_type(atomic_##short_type##_t *a,\t\t\t\\\n    type val, atomic_memory_order_t mo) {\t\t\t\t\\\n\tatomic_store_explicit(a, val, mo);\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_exchange_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\treturn atomic_exchange_explicit(a, val, mo);\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE bool\t\t\t\t\t\t\t\\\natomic_compare_exchange_weak_##short_type(atomic_##short_type##_t *a,\t\\\n    type *expected, type desired, atomic_memory_order_t success_mo,\t\\\n    atomic_memory_order_t failure_mo) {\t\t\t\t\t\\\n\treturn atomic_compare_exchange_weak_explicit(a, expected,\t\\\n\t    desired, success_mo, failure_mo);\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE bool\t\t\t\t\t\t\t\\\natomic_compare_exchange_strong_##short_type(atomic_##short_type##_t *a,\t\\\n    type *expected, type desired, atomic_memory_order_t success_mo,\t\\\n    atomic_memory_order_t failure_mo) {\t\t\t\t\t\\\n\treturn atomic_compare_exchange_strong_explicit(a, expected,\t\\\n\t    desired, success_mo, failure_mo);\t\t\t\t\\\n}\n\n/*\n * Integral types have some special operations available that non-integral ones\n * lack.\n */\n#define JEMALLOC_GENERATE_INT_ATOMICS(type, short_type, \t\t\\\n    /* unused */ lg_size)\t\t\t\t\t\t\\\nJEMALLOC_GENERATE_ATOMICS(type, short_type, /* unused */ lg_size)\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_add_##short_type(atomic_##short_type##_t *a,\t\t\\\n    type val, atomic_memory_order_t mo) {\t\t\t\t\\\n\treturn atomic_fetch_add_explicit(a, val, mo);\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_sub_##short_type(atomic_##short_type##_t *a,\t\t\\\n    type val, atomic_memory_order_t mo) {\t\t\t\t\\\n\treturn atomic_fetch_sub_explicit(a, val, mo);\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_and_##short_type(atomic_##short_type##_t *a,\t\t\\\n    type val, atomic_memory_order_t mo) {\t\t\t\t\\\n\treturn atomic_fetch_and_explicit(a, val, mo);\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_or_##short_type(atomic_##short_type##_t *a,\t\t\\\n    type val, atomic_memory_order_t mo) {\t\t\t\t\\\n\treturn atomic_fetch_or_explicit(a, val, mo);\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_xor_##short_type(atomic_##short_type##_t *a,\t\t\\\n    type val, atomic_memory_order_t mo) {\t\t\t\t\\\n\treturn atomic_fetch_xor_explicit(a, val, mo);\t\t\t\\\n}\n\n#endif /* JEMALLOC_INTERNAL_ATOMIC_C11_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/atomic_gcc_atomic.h",
    "content": "#ifndef JEMALLOC_INTERNAL_ATOMIC_GCC_ATOMIC_H\n#define JEMALLOC_INTERNAL_ATOMIC_GCC_ATOMIC_H\n\n#include \"jemalloc/internal/assert.h\"\n\n#define ATOMIC_INIT(...) {__VA_ARGS__}\n\ntypedef enum {\n\tatomic_memory_order_relaxed,\n\tatomic_memory_order_acquire,\n\tatomic_memory_order_release,\n\tatomic_memory_order_acq_rel,\n\tatomic_memory_order_seq_cst\n} atomic_memory_order_t;\n\nATOMIC_INLINE int\natomic_enum_to_builtin(atomic_memory_order_t mo) {\n\tswitch (mo) {\n\tcase atomic_memory_order_relaxed:\n\t\treturn __ATOMIC_RELAXED;\n\tcase atomic_memory_order_acquire:\n\t\treturn __ATOMIC_ACQUIRE;\n\tcase atomic_memory_order_release:\n\t\treturn __ATOMIC_RELEASE;\n\tcase atomic_memory_order_acq_rel:\n\t\treturn __ATOMIC_ACQ_REL;\n\tcase atomic_memory_order_seq_cst:\n\t\treturn __ATOMIC_SEQ_CST;\n\t}\n\t/* Can't happen; the switch is exhaustive. */\n\tnot_reached();\n}\n\nATOMIC_INLINE void\natomic_fence(atomic_memory_order_t mo) {\n\t__atomic_thread_fence(atomic_enum_to_builtin(mo));\n}\n\n#define JEMALLOC_GENERATE_ATOMICS(type, short_type,\t\t\t\\\n    /* unused */ lg_size)\t\t\t\t\t\t\\\ntypedef struct {\t\t\t\t\t\t\t\\\n\ttype repr;\t\t\t\t\t\t\t\\\n} atomic_##short_type##_t;\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_load_##short_type(const atomic_##short_type##_t *a,\t\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\ttype result;\t\t\t\t\t\t\t\\\n\t__atomic_load(&a->repr, &result, atomic_enum_to_builtin(mo));\t\\\n\treturn result;\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE void\t\t\t\t\t\t\t\\\natomic_store_##short_type(atomic_##short_type##_t *a, type val,\t\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\t__atomic_store(&a->repr, &val, atomic_enum_to_builtin(mo));\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_exchange_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\ttype result;\t\t\t\t\t\t\t\\\n\t__atomic_exchange(&a->repr, &val, &result,\t\t\t\\\n\t    atomic_enum_to_builtin(mo));\t\t\t\t\\\n\treturn result;\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE bool\t\t\t\t\t\t\t\\\natomic_compare_exchange_weak_##short_type(atomic_##short_type##_t *a,\t\\\n    UNUSED type *expected, type desired,\t\t\t\t\\\n    atomic_memory_order_t success_mo,\t\t\t\t\t\\\n    atomic_memory_order_t failure_mo) {\t\t\t\t\t\\\n\treturn __atomic_compare_exchange(&a->repr, expected, &desired,\t\\\n\t    true, atomic_enum_to_builtin(success_mo),\t\t\t\\\n\t    atomic_enum_to_builtin(failure_mo));\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE bool\t\t\t\t\t\t\t\\\natomic_compare_exchange_strong_##short_type(atomic_##short_type##_t *a,\t\\\n    UNUSED type *expected, type desired,\t\t\t\t\\\n    atomic_memory_order_t success_mo,\t\t\t\t\t\\\n    atomic_memory_order_t failure_mo) {\t\t\t\t\t\\\n\treturn __atomic_compare_exchange(&a->repr, expected, &desired,\t\\\n\t    false,\t\t\t\t\t\t\t\\\n\t    atomic_enum_to_builtin(success_mo),\t\t\t\t\\\n\t    atomic_enum_to_builtin(failure_mo));\t\t\t\\\n}\n\n\n#define JEMALLOC_GENERATE_INT_ATOMICS(type, short_type,\t\t\t\\\n    /* unused */ lg_size)\t\t\t\t\t\t\\\nJEMALLOC_GENERATE_ATOMICS(type, short_type, /* unused */ lg_size)\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_add_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\treturn __atomic_fetch_add(&a->repr, val,\t\t\t\\\n\t    atomic_enum_to_builtin(mo));\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_sub_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\treturn __atomic_fetch_sub(&a->repr, val,\t\t\t\\\n\t    atomic_enum_to_builtin(mo));\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_and_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\treturn __atomic_fetch_and(&a->repr, val,\t\t\t\\\n\t    atomic_enum_to_builtin(mo));\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_or_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\treturn __atomic_fetch_or(&a->repr, val,\t\t\t\t\\\n\t    atomic_enum_to_builtin(mo));\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_xor_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\treturn __atomic_fetch_xor(&a->repr, val,\t\t\t\\\n\t    atomic_enum_to_builtin(mo));\t\t\t\t\\\n}\n\n#endif /* JEMALLOC_INTERNAL_ATOMIC_GCC_ATOMIC_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/atomic_gcc_sync.h",
    "content": "#ifndef JEMALLOC_INTERNAL_ATOMIC_GCC_SYNC_H\n#define JEMALLOC_INTERNAL_ATOMIC_GCC_SYNC_H\n\n#define ATOMIC_INIT(...) {__VA_ARGS__}\n\ntypedef enum {\n\tatomic_memory_order_relaxed,\n\tatomic_memory_order_acquire,\n\tatomic_memory_order_release,\n\tatomic_memory_order_acq_rel,\n\tatomic_memory_order_seq_cst\n} atomic_memory_order_t;\n\nATOMIC_INLINE void\natomic_fence(atomic_memory_order_t mo) {\n\t/* Easy cases first: no barrier, and full barrier. */\n\tif (mo == atomic_memory_order_relaxed) {\n\t\tasm volatile(\"\" ::: \"memory\");\n\t\treturn;\n\t}\n\tif (mo == atomic_memory_order_seq_cst) {\n\t\tasm volatile(\"\" ::: \"memory\");\n\t\t__sync_synchronize();\n\t\tasm volatile(\"\" ::: \"memory\");\n\t\treturn;\n\t}\n\tasm volatile(\"\" ::: \"memory\");\n#  if defined(__i386__) || defined(__x86_64__)\n\t/* This is implicit on x86. */\n#  elif defined(__ppc64__)\n\tasm volatile(\"lwsync\");\n#  elif defined(__ppc__)\n\tasm volatile(\"sync\");\n#  elif defined(__sparc__) && defined(__arch64__)\n\tif (mo == atomic_memory_order_acquire) {\n\t\tasm volatile(\"membar #LoadLoad | #LoadStore\");\n\t} else if (mo == atomic_memory_order_release) {\n\t\tasm volatile(\"membar #LoadStore | #StoreStore\");\n\t} else {\n\t\tasm volatile(\"membar #LoadLoad | #LoadStore | #StoreStore\");\n\t}\n#  else\n\t__sync_synchronize();\n#  endif\n\tasm volatile(\"\" ::: \"memory\");\n}\n\n/*\n * A correct implementation of seq_cst loads and stores on weakly ordered\n * architectures could do either of the following:\n *   1. store() is weak-fence -> store -> strong fence, load() is load ->\n *      strong-fence.\n *   2. store() is strong-fence -> store, load() is strong-fence -> load ->\n *      weak-fence.\n * The tricky thing is, load() and store() above can be the load or store\n * portions of a gcc __sync builtin, so we have to follow GCC's lead, which\n * means going with strategy 2.\n * On strongly ordered architectures, the natural strategy is to stick a strong\n * fence after seq_cst stores, and have naked loads.  So we want the strong\n * fences in different places on different architectures.\n * atomic_pre_sc_load_fence and atomic_post_sc_store_fence allow us to\n * accomplish this.\n */\n\nATOMIC_INLINE void\natomic_pre_sc_load_fence() {\n#  if defined(__i386__) || defined(__x86_64__) ||\t\t\t\\\n    (defined(__sparc__) && defined(__arch64__))\n\tatomic_fence(atomic_memory_order_relaxed);\n#  else\n\tatomic_fence(atomic_memory_order_seq_cst);\n#  endif\n}\n\nATOMIC_INLINE void\natomic_post_sc_store_fence() {\n#  if defined(__i386__) || defined(__x86_64__) ||\t\t\t\\\n    (defined(__sparc__) && defined(__arch64__))\n\tatomic_fence(atomic_memory_order_seq_cst);\n#  else\n\tatomic_fence(atomic_memory_order_relaxed);\n#  endif\n\n}\n\n#define JEMALLOC_GENERATE_ATOMICS(type, short_type,\t\t\t\\\n    /* unused */ lg_size)\t\t\t\t\t\t\\\ntypedef struct {\t\t\t\t\t\t\t\\\n\ttype volatile repr;\t\t\t\t\t\t\\\n} atomic_##short_type##_t;\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_load_##short_type(const atomic_##short_type##_t *a,\t\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\tif (mo == atomic_memory_order_seq_cst) {\t\t\t\\\n\t\tatomic_pre_sc_load_fence();\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\ttype result = a->repr;\t\t\t\t\t\t\\\n\tif (mo != atomic_memory_order_relaxed) {\t\t\t\\\n\t\tatomic_fence(atomic_memory_order_acquire);\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\treturn result;\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE void\t\t\t\t\t\t\t\\\natomic_store_##short_type(atomic_##short_type##_t *a,\t\t\t\\\n    type val, atomic_memory_order_t mo) {\t\t\t\t\\\n\tif (mo != atomic_memory_order_relaxed) {\t\t\t\\\n\t\tatomic_fence(atomic_memory_order_release);\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\ta->repr = val;\t\t\t\t\t\t\t\\\n\tif (mo == atomic_memory_order_seq_cst) {\t\t\t\\\n\t\tatomic_post_sc_store_fence();\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_exchange_##short_type(atomic_##short_type##_t *a, type val, \\\n    atomic_memory_order_t mo) {                  \t\t\t\t\t \\\n\t/*\t\t\t\t\t\t\t\t\\\n\t * Because of FreeBSD, we care about gcc 4.2, which doesn't have\\\n\t * an atomic exchange builtin.  We fake it with a CAS loop.\t\\\n\t */\t\t\t\t\t\t\t\t\\\n\twhile (true) {\t\t\t\t\t\t\t\\\n\t\ttype old = a->repr;\t\t\t\t\t\\\n\t\tif (__sync_bool_compare_and_swap(&a->repr, old, val)) {\t\\\n\t\t\treturn old;\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE bool\t\t\t\t\t\t\t\\\natomic_compare_exchange_weak_##short_type(atomic_##short_type##_t *a,\t\\\n    type *expected, type desired,                                     \\\n    atomic_memory_order_t success_mo,                          \\\n    atomic_memory_order_t failure_mo) {\t\t\t\t                \\\n\ttype prev = __sync_val_compare_and_swap(&a->repr, *expected,\t\\\n\t    desired);\t\t\t\t\t\t\t\\\n\tif (prev == *expected) {\t\t\t\t\t\\\n\t\treturn true;\t\t\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t\t*expected = prev;\t\t\t\t\t\\\n\t\treturn false;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE bool\t\t\t\t\t\t\t\\\natomic_compare_exchange_strong_##short_type(atomic_##short_type##_t *a,\t\\\n    type *expected, type desired,                                       \\\n    atomic_memory_order_t success_mo,                            \\\n    atomic_memory_order_t failure_mo) {                          \\\n\ttype prev = __sync_val_compare_and_swap(&a->repr, *expected,\t\\\n\t    desired);\t\t\t\t\t\t\t\\\n\tif (prev == *expected) {\t\t\t\t\t\\\n\t\treturn true;\t\t\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t\t*expected = prev;\t\t\t\t\t\\\n\t\treturn false;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n}\n\n#define JEMALLOC_GENERATE_INT_ATOMICS(type, short_type,\t\t\t\\\n    /* unused */ lg_size)\t\t\t\t\t\t\\\nJEMALLOC_GENERATE_ATOMICS(type, short_type, /* unused */ lg_size)\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_add_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\treturn __sync_fetch_and_add(&a->repr, val);\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_sub_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\treturn __sync_fetch_and_sub(&a->repr, val);\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_and_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\treturn __sync_fetch_and_and(&a->repr, val);\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_or_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\treturn __sync_fetch_and_or(&a->repr, val);\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_xor_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\treturn __sync_fetch_and_xor(&a->repr, val);\t\t\t\\\n}\n\n#endif /* JEMALLOC_INTERNAL_ATOMIC_GCC_SYNC_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/atomic_msvc.h",
    "content": "#ifndef JEMALLOC_INTERNAL_ATOMIC_MSVC_H\n#define JEMALLOC_INTERNAL_ATOMIC_MSVC_H\n\n#define ATOMIC_INIT(...) {__VA_ARGS__}\n\ntypedef enum {\n\tatomic_memory_order_relaxed,\n\tatomic_memory_order_acquire,\n\tatomic_memory_order_release,\n\tatomic_memory_order_acq_rel,\n\tatomic_memory_order_seq_cst\n} atomic_memory_order_t;\n\ntypedef char atomic_repr_0_t;\ntypedef short atomic_repr_1_t;\ntypedef long atomic_repr_2_t;\ntypedef __int64 atomic_repr_3_t;\n\nATOMIC_INLINE void\natomic_fence(atomic_memory_order_t mo) {\n\t_ReadWriteBarrier();\n#  if defined(_M_ARM) || defined(_M_ARM64)\n\t/* ARM needs a barrier for everything but relaxed. */\n\tif (mo != atomic_memory_order_relaxed) {\n\t\tMemoryBarrier();\n\t}\n#  elif defined(_M_IX86) || defined (_M_X64)\n\t/* x86 needs a barrier only for seq_cst. */\n\tif (mo == atomic_memory_order_seq_cst) {\n\t\tMemoryBarrier();\n\t}\n#  else\n#  error \"Don't know how to create atomics for this platform for MSVC.\"\n#  endif\n\t_ReadWriteBarrier();\n}\n\n#define ATOMIC_INTERLOCKED_REPR(lg_size) atomic_repr_ ## lg_size ## _t\n\n#define ATOMIC_CONCAT(a, b) ATOMIC_RAW_CONCAT(a, b)\n#define ATOMIC_RAW_CONCAT(a, b) a ## b\n\n#define ATOMIC_INTERLOCKED_NAME(base_name, lg_size) ATOMIC_CONCAT(\t\\\n    base_name, ATOMIC_INTERLOCKED_SUFFIX(lg_size))\n\n#define ATOMIC_INTERLOCKED_SUFFIX(lg_size)\t\t\t\t\\\n    ATOMIC_CONCAT(ATOMIC_INTERLOCKED_SUFFIX_, lg_size)\n\n#define ATOMIC_INTERLOCKED_SUFFIX_0 8\n#define ATOMIC_INTERLOCKED_SUFFIX_1 16\n#define ATOMIC_INTERLOCKED_SUFFIX_2\n#define ATOMIC_INTERLOCKED_SUFFIX_3 64\n\n#define JEMALLOC_GENERATE_ATOMICS(type, short_type, lg_size)\t\t\\\ntypedef struct {\t\t\t\t\t\t\t\\\n\tATOMIC_INTERLOCKED_REPR(lg_size) repr;\t\t\t\t\\\n} atomic_##short_type##_t;\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_load_##short_type(const atomic_##short_type##_t *a,\t\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\tATOMIC_INTERLOCKED_REPR(lg_size) ret = a->repr;\t\t\t\\\n\tif (mo != atomic_memory_order_relaxed) {\t\t\t\\\n\t\tatomic_fence(atomic_memory_order_acquire);\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\treturn (type) ret;\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE void\t\t\t\t\t\t\t\\\natomic_store_##short_type(atomic_##short_type##_t *a,\t\t\t\\\n    type val, atomic_memory_order_t mo) {\t\t\t\t\\\n\tif (mo != atomic_memory_order_relaxed) {\t\t\t\\\n\t\tatomic_fence(atomic_memory_order_release);\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\ta->repr = (ATOMIC_INTERLOCKED_REPR(lg_size)) val;\t\t\\\n\tif (mo == atomic_memory_order_seq_cst) {\t\t\t\\\n\t\tatomic_fence(atomic_memory_order_seq_cst);\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_exchange_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\treturn (type)ATOMIC_INTERLOCKED_NAME(_InterlockedExchange,\t\\\n\t    lg_size)(&a->repr, (ATOMIC_INTERLOCKED_REPR(lg_size))val);\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE bool\t\t\t\t\t\t\t\\\natomic_compare_exchange_weak_##short_type(atomic_##short_type##_t *a,\t\\\n    type *expected, type desired, atomic_memory_order_t success_mo,\t\\\n    atomic_memory_order_t failure_mo) {\t\t\t\t\t\\\n\tATOMIC_INTERLOCKED_REPR(lg_size) e =\t\t\t\t\\\n\t    (ATOMIC_INTERLOCKED_REPR(lg_size))*expected;\t\t\\\n\tATOMIC_INTERLOCKED_REPR(lg_size) d =\t\t\t\t\\\n\t    (ATOMIC_INTERLOCKED_REPR(lg_size))desired;\t\t\t\\\n\tATOMIC_INTERLOCKED_REPR(lg_size) old =\t\t\t\t\\\n\t    ATOMIC_INTERLOCKED_NAME(_InterlockedCompareExchange, \t\\\n\t\tlg_size)(&a->repr, d, e);\t\t\t\t\\\n\tif (old == e) {\t\t\t\t\t\t\t\\\n\t\treturn true;\t\t\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t\t*expected = (type)old;\t\t\t\t\t\\\n\t\treturn false;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE bool\t\t\t\t\t\t\t\\\natomic_compare_exchange_strong_##short_type(atomic_##short_type##_t *a,\t\\\n    type *expected, type desired, atomic_memory_order_t success_mo,\t\\\n    atomic_memory_order_t failure_mo) {\t\t\t\t\t\\\n\t/* We implement the weak version with strong semantics. */\t\\\n\treturn atomic_compare_exchange_weak_##short_type(a, expected,\t\\\n\t    desired, success_mo, failure_mo);\t\t\t\t\\\n}\n\n\n#define JEMALLOC_GENERATE_INT_ATOMICS(type, short_type, lg_size)\t\\\nJEMALLOC_GENERATE_ATOMICS(type, short_type, lg_size)\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_add_##short_type(atomic_##short_type##_t *a,\t\t\\\n    type val, atomic_memory_order_t mo) {\t\t\t\t\\\n\treturn (type)ATOMIC_INTERLOCKED_NAME(_InterlockedExchangeAdd,\t\\\n\t    lg_size)(&a->repr, (ATOMIC_INTERLOCKED_REPR(lg_size))val);\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_sub_##short_type(atomic_##short_type##_t *a,\t\t\\\n    type val, atomic_memory_order_t mo) {\t\t\t\t\\\n\t/*\t\t\t\t\t\t\t\t\\\n\t * MSVC warns on negation of unsigned operands, but for us it\t\\\n\t * gives exactly the right semantics (MAX_TYPE + 1 - operand).\t\\\n\t */\t\t\t\t\t\t\t\t\\\n\t__pragma(warning(push))\t\t\t\t\t\t\\\n\t__pragma(warning(disable: 4146))\t\t\t\t\\\n\treturn atomic_fetch_add_##short_type(a, -val, mo);\t\t\\\n\t__pragma(warning(pop))\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_and_##short_type(atomic_##short_type##_t *a,\t\t\\\n    type val, atomic_memory_order_t mo) {\t\t\t\t\\\n\treturn (type)ATOMIC_INTERLOCKED_NAME(_InterlockedAnd, lg_size)(\t\\\n\t    &a->repr, (ATOMIC_INTERLOCKED_REPR(lg_size))val);\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_or_##short_type(atomic_##short_type##_t *a,\t\t\\\n    type val, atomic_memory_order_t mo) {\t\t\t\t\\\n\treturn (type)ATOMIC_INTERLOCKED_NAME(_InterlockedOr, lg_size)(\t\\\n\t    &a->repr, (ATOMIC_INTERLOCKED_REPR(lg_size))val);\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_xor_##short_type(atomic_##short_type##_t *a,\t\t\\\n    type val, atomic_memory_order_t mo) {\t\t\t\t\\\n\treturn (type)ATOMIC_INTERLOCKED_NAME(_InterlockedXor, lg_size)(\t\\\n\t    &a->repr, (ATOMIC_INTERLOCKED_REPR(lg_size))val);\t\t\\\n}\n\n#endif /* JEMALLOC_INTERNAL_ATOMIC_MSVC_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/background_thread_externs.h",
    "content": "#ifndef JEMALLOC_INTERNAL_BACKGROUND_THREAD_EXTERNS_H\n#define JEMALLOC_INTERNAL_BACKGROUND_THREAD_EXTERNS_H\n\nextern bool opt_background_thread;\nextern size_t opt_max_background_threads;\nextern malloc_mutex_t background_thread_lock;\nextern atomic_b_t background_thread_enabled_state;\nextern size_t n_background_threads;\nextern size_t max_background_threads;\nextern background_thread_info_t *background_thread_info;\n\nbool background_thread_create(tsd_t *tsd, unsigned arena_ind);\nbool background_threads_enable(tsd_t *tsd);\nbool background_threads_disable(tsd_t *tsd);\nvoid background_thread_interval_check(tsdn_t *tsdn, arena_t *arena,\n    arena_decay_t *decay, size_t npages_new);\nvoid background_thread_prefork0(tsdn_t *tsdn);\nvoid background_thread_prefork1(tsdn_t *tsdn);\nvoid background_thread_postfork_parent(tsdn_t *tsdn);\nvoid background_thread_postfork_child(tsdn_t *tsdn);\nbool background_thread_stats_read(tsdn_t *tsdn,\n    background_thread_stats_t *stats);\nvoid background_thread_ctl_init(tsdn_t *tsdn);\n\n#ifdef JEMALLOC_PTHREAD_CREATE_WRAPPER\nextern int pthread_create_wrapper(pthread_t *__restrict, const pthread_attr_t *,\n    void *(*)(void *), void *__restrict);\n#endif\nbool background_thread_boot0(void);\nbool background_thread_boot1(tsdn_t *tsdn);\n\n#endif /* JEMALLOC_INTERNAL_BACKGROUND_THREAD_EXTERNS_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/background_thread_inlines.h",
    "content": "#ifndef JEMALLOC_INTERNAL_BACKGROUND_THREAD_INLINES_H\n#define JEMALLOC_INTERNAL_BACKGROUND_THREAD_INLINES_H\n\nJEMALLOC_ALWAYS_INLINE bool\nbackground_thread_enabled(void) {\n\treturn atomic_load_b(&background_thread_enabled_state, ATOMIC_RELAXED);\n}\n\nJEMALLOC_ALWAYS_INLINE void\nbackground_thread_enabled_set(tsdn_t *tsdn, bool state) {\n\tmalloc_mutex_assert_owner(tsdn, &background_thread_lock);\n\tatomic_store_b(&background_thread_enabled_state, state, ATOMIC_RELAXED);\n}\n\nJEMALLOC_ALWAYS_INLINE background_thread_info_t *\narena_background_thread_info_get(arena_t *arena) {\n\tunsigned arena_ind = arena_ind_get(arena);\n\treturn &background_thread_info[arena_ind % max_background_threads];\n}\n\nJEMALLOC_ALWAYS_INLINE background_thread_info_t *\nbackground_thread_info_get(size_t ind) {\n\treturn &background_thread_info[ind % max_background_threads];\n}\n\nJEMALLOC_ALWAYS_INLINE uint64_t\nbackground_thread_wakeup_time_get(background_thread_info_t *info) {\n\tuint64_t next_wakeup = nstime_ns(&info->next_wakeup);\n\tassert(atomic_load_b(&info->indefinite_sleep, ATOMIC_ACQUIRE) ==\n\t    (next_wakeup == BACKGROUND_THREAD_INDEFINITE_SLEEP));\n\treturn next_wakeup;\n}\n\nJEMALLOC_ALWAYS_INLINE void\nbackground_thread_wakeup_time_set(tsdn_t *tsdn, background_thread_info_t *info,\n    uint64_t wakeup_time) {\n\tmalloc_mutex_assert_owner(tsdn, &info->mtx);\n\tatomic_store_b(&info->indefinite_sleep,\n\t    wakeup_time == BACKGROUND_THREAD_INDEFINITE_SLEEP, ATOMIC_RELEASE);\n\tnstime_init(&info->next_wakeup, wakeup_time);\n}\n\nJEMALLOC_ALWAYS_INLINE bool\nbackground_thread_indefinite_sleep(background_thread_info_t *info) {\n\treturn atomic_load_b(&info->indefinite_sleep, ATOMIC_ACQUIRE);\n}\n\nJEMALLOC_ALWAYS_INLINE void\narena_background_thread_inactivity_check(tsdn_t *tsdn, arena_t *arena,\n    bool is_background_thread) {\n\tif (!background_thread_enabled() || is_background_thread) {\n\t\treturn;\n\t}\n\tbackground_thread_info_t *info =\n\t    arena_background_thread_info_get(arena);\n\tif (background_thread_indefinite_sleep(info)) {\n\t\tbackground_thread_interval_check(tsdn, arena,\n\t\t    &arena->decay_dirty, 0);\n\t}\n}\n\n#endif /* JEMALLOC_INTERNAL_BACKGROUND_THREAD_INLINES_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/background_thread_structs.h",
    "content": "#ifndef JEMALLOC_INTERNAL_BACKGROUND_THREAD_STRUCTS_H\n#define JEMALLOC_INTERNAL_BACKGROUND_THREAD_STRUCTS_H\n\n/* This file really combines \"structs\" and \"types\", but only transitionally. */\n\n#if defined(JEMALLOC_BACKGROUND_THREAD) || defined(JEMALLOC_LAZY_LOCK)\n#  define JEMALLOC_PTHREAD_CREATE_WRAPPER\n#endif\n\n#define BACKGROUND_THREAD_INDEFINITE_SLEEP UINT64_MAX\n#define MAX_BACKGROUND_THREAD_LIMIT MALLOCX_ARENA_LIMIT\n#define DEFAULT_NUM_BACKGROUND_THREAD 4\n\ntypedef enum {\n\tbackground_thread_stopped,\n\tbackground_thread_started,\n\t/* Thread waits on the global lock when paused (for arena_reset). */\n\tbackground_thread_paused,\n} background_thread_state_t;\n\nstruct background_thread_info_s {\n#ifdef JEMALLOC_BACKGROUND_THREAD\n\t/* Background thread is pthread specific. */\n\tpthread_t\t\tthread;\n\tpthread_cond_t\t\tcond;\n#endif\n\tmalloc_mutex_t\t\tmtx;\n\tbackground_thread_state_t\tstate;\n\t/* When true, it means no wakeup scheduled. */\n\tatomic_b_t\t\tindefinite_sleep;\n\t/* Next scheduled wakeup time (absolute time in ns). */\n\tnstime_t\t\tnext_wakeup;\n\t/*\n\t *  Since the last background thread run, newly added number of pages\n\t *  that need to be purged by the next wakeup.  This is adjusted on\n\t *  epoch advance, and is used to determine whether we should signal the\n\t *  background thread to wake up earlier.\n\t */\n\tsize_t\t\t\tnpages_to_purge_new;\n\t/* Stats: total number of runs since started. */\n\tuint64_t\t\ttot_n_runs;\n\t/* Stats: total sleep time since started. */\n\tnstime_t\t\ttot_sleep_time;\n};\ntypedef struct background_thread_info_s background_thread_info_t;\n\nstruct background_thread_stats_s {\n\tsize_t num_threads;\n\tuint64_t num_runs;\n\tnstime_t run_interval;\n};\ntypedef struct background_thread_stats_s background_thread_stats_t;\n\n#endif /* JEMALLOC_INTERNAL_BACKGROUND_THREAD_STRUCTS_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/base_externs.h",
    "content": "#ifndef JEMALLOC_INTERNAL_BASE_EXTERNS_H\n#define JEMALLOC_INTERNAL_BASE_EXTERNS_H\n\nextern metadata_thp_mode_t opt_metadata_thp;\nextern const char *metadata_thp_mode_names[];\n\nbase_t *b0get(void);\nbase_t *base_new(tsdn_t *tsdn, unsigned ind, extent_hooks_t *extent_hooks);\nvoid base_delete(tsdn_t *tsdn, base_t *base);\nextent_hooks_t *base_extent_hooks_get(base_t *base);\nextent_hooks_t *base_extent_hooks_set(base_t *base,\n    extent_hooks_t *extent_hooks);\nvoid *base_alloc(tsdn_t *tsdn, base_t *base, size_t size, size_t alignment);\nextent_t *base_alloc_extent(tsdn_t *tsdn, base_t *base);\nvoid base_stats_get(tsdn_t *tsdn, base_t *base, size_t *allocated,\n    size_t *resident, size_t *mapped, size_t *n_thp);\nvoid base_prefork(tsdn_t *tsdn, base_t *base);\nvoid base_postfork_parent(tsdn_t *tsdn, base_t *base);\nvoid base_postfork_child(tsdn_t *tsdn, base_t *base);\nbool base_boot(tsdn_t *tsdn);\n\n#endif /* JEMALLOC_INTERNAL_BASE_EXTERNS_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/base_inlines.h",
    "content": "#ifndef JEMALLOC_INTERNAL_BASE_INLINES_H\n#define JEMALLOC_INTERNAL_BASE_INLINES_H\n\nstatic inline unsigned\nbase_ind_get(const base_t *base) {\n\treturn base->ind;\n}\n\nstatic inline bool\nmetadata_thp_enabled(void) {\n\treturn (opt_metadata_thp != metadata_thp_disabled);\n}\n#endif /* JEMALLOC_INTERNAL_BASE_INLINES_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/base_structs.h",
    "content": "#ifndef JEMALLOC_INTERNAL_BASE_STRUCTS_H\n#define JEMALLOC_INTERNAL_BASE_STRUCTS_H\n\n#include \"jemalloc/internal/jemalloc_internal_types.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/sc.h\"\n\n/* Embedded at the beginning of every block of base-managed virtual memory. */\nstruct base_block_s {\n\t/* Total size of block's virtual memory mapping. */\n\tsize_t\t\tsize;\n\n\t/* Next block in list of base's blocks. */\n\tbase_block_t\t*next;\n\n\t/* Tracks unused trailing space. */\n\textent_t\textent;\n};\n\nstruct base_s {\n\t/* Associated arena's index within the arenas array. */\n\tunsigned\tind;\n\n\t/*\n\t * User-configurable extent hook functions.  Points to an\n\t * extent_hooks_t.\n\t */\n\tatomic_p_t\textent_hooks;\n\n\t/* Protects base_alloc() and base_stats_get() operations. */\n\tmalloc_mutex_t\tmtx;\n\n\t/* Using THP when true (metadata_thp auto mode). */\n\tbool\t\tauto_thp_switched;\n\t/*\n\t * Most recent size class in the series of increasingly large base\n\t * extents.  Logarithmic spacing between subsequent allocations ensures\n\t * that the total number of distinct mappings remains small.\n\t */\n\tpszind_t\tpind_last;\n\n\t/* Serial number generation state. */\n\tsize_t\t\textent_sn_next;\n\n\t/* Chain of all blocks associated with base. */\n\tbase_block_t\t*blocks;\n\n\t/* Heap of extents that track unused trailing space within blocks. */\n\textent_heap_t\tavail[SC_NSIZES];\n\n\t/* Stats, only maintained if config_stats. */\n\tsize_t\t\tallocated;\n\tsize_t\t\tresident;\n\tsize_t\t\tmapped;\n\t/* Number of THP regions touched. */\n\tsize_t\t\tn_thp;\n};\n\n#endif /* JEMALLOC_INTERNAL_BASE_STRUCTS_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/base_types.h",
    "content": "#ifndef JEMALLOC_INTERNAL_BASE_TYPES_H\n#define JEMALLOC_INTERNAL_BASE_TYPES_H\n\ntypedef struct base_block_s base_block_t;\ntypedef struct base_s base_t;\n\n#define METADATA_THP_DEFAULT metadata_thp_disabled\n\n/*\n * In auto mode, arenas switch to huge pages for the base allocator on the\n * second base block.  a0 switches to thp on the 5th block (after 20 megabytes\n * of metadata), since more metadata (e.g. rtree nodes) come from a0's base.\n */\n\n#define BASE_AUTO_THP_THRESHOLD    2\n#define BASE_AUTO_THP_THRESHOLD_A0 5\n\ntypedef enum {\n\tmetadata_thp_disabled   = 0,\n\t/*\n\t * Lazily enable hugepage for metadata. To avoid high RSS caused by THP\n\t * + low usage arena (i.e. THP becomes a significant percentage), the\n\t * \"auto\" option only starts using THP after a base allocator used up\n\t * the first THP region.  Starting from the second hugepage (in a single\n\t * arena), \"auto\" behaves the same as \"always\", i.e. madvise hugepage\n\t * right away.\n\t */\n\tmetadata_thp_auto       = 1,\n\tmetadata_thp_always     = 2,\n\tmetadata_thp_mode_limit = 3\n} metadata_thp_mode_t;\n\n#endif /* JEMALLOC_INTERNAL_BASE_TYPES_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/bin.h",
    "content": "#ifndef JEMALLOC_INTERNAL_BIN_H\n#define JEMALLOC_INTERNAL_BIN_H\n\n#include \"jemalloc/internal/bin_stats.h\"\n#include \"jemalloc/internal/bin_types.h\"\n#include \"jemalloc/internal/extent_types.h\"\n#include \"jemalloc/internal/extent_structs.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/sc.h\"\n\n/*\n * A bin contains a set of extents that are currently being used for slab\n * allocations.\n */\n\n/*\n * Read-only information associated with each element of arena_t's bins array\n * is stored separately, partly to reduce memory usage (only one copy, rather\n * than one per arena), but mainly to avoid false cacheline sharing.\n *\n * Each slab has the following layout:\n *\n *   /--------------------\\\n *   | region 0           |\n *   |--------------------|\n *   | region 1           |\n *   |--------------------|\n *   | ...                |\n *   | ...                |\n *   | ...                |\n *   |--------------------|\n *   | region nregs-1     |\n *   \\--------------------/\n */\ntypedef struct bin_info_s bin_info_t;\nstruct bin_info_s {\n\t/* Size of regions in a slab for this bin's size class. */\n\tsize_t\t\t\treg_size;\n\n\t/* Total size of a slab for this bin's size class. */\n\tsize_t\t\t\tslab_size;\n\n\t/* Total number of regions in a slab for this bin's size class. */\n\tuint32_t\t\tnregs;\n\n\t/* Number of sharded bins in each arena for this size class. */\n\tuint32_t\t\tn_shards;\n\n\t/*\n\t * Metadata used to manipulate bitmaps for slabs associated with this\n\t * bin.\n\t */\n\tbitmap_info_t\t\tbitmap_info;\n};\n\nextern bin_info_t bin_infos[SC_NBINS];\n\ntypedef struct bin_s bin_t;\nstruct bin_s {\n\t/* All operations on bin_t fields require lock ownership. */\n\tmalloc_mutex_t\t\tlock;\n\n\t/*\n\t * Current slab being used to service allocations of this bin's size\n\t * class.  slabcur is independent of slabs_{nonfull,full}; whenever\n\t * slabcur is reassigned, the previous slab must be deallocated or\n\t * inserted into slabs_{nonfull,full}.\n\t */\n\textent_t\t\t*slabcur;\n\n\t/*\n\t * Heap of non-full slabs.  This heap is used to assure that new\n\t * allocations come from the non-full slab that is oldest/lowest in\n\t * memory.\n\t */\n\textent_heap_t\t\tslabs_nonfull;\n\n\t/* List used to track full slabs. */\n\textent_list_t\t\tslabs_full;\n\n\t/* Bin statistics. */\n\tbin_stats_t\tstats;\n};\n\n/* A set of sharded bins of the same size class. */\ntypedef struct bins_s bins_t;\nstruct bins_s {\n\t/* Sharded bins.  Dynamically sized. */\n\tbin_t *bin_shards;\n};\n\nvoid bin_shard_sizes_boot(unsigned bin_shards[SC_NBINS]);\nbool bin_update_shard_size(unsigned bin_shards[SC_NBINS], size_t start_size,\n    size_t end_size, size_t nshards);\nvoid bin_boot(sc_data_t *sc_data, unsigned bin_shard_sizes[SC_NBINS]);\n\n/* Initializes a bin to empty.  Returns true on error. */\nbool bin_init(bin_t *bin);\n\n/* Forking. */\nvoid bin_prefork(tsdn_t *tsdn, bin_t *bin);\nvoid bin_postfork_parent(tsdn_t *tsdn, bin_t *bin);\nvoid bin_postfork_child(tsdn_t *tsdn, bin_t *bin);\n\n/* Stats. */\nstatic inline void\nbin_stats_merge(tsdn_t *tsdn, bin_stats_t *dst_bin_stats, bin_t *bin) {\n\tmalloc_mutex_lock(tsdn, &bin->lock);\n\tmalloc_mutex_prof_accum(tsdn, &dst_bin_stats->mutex_data, &bin->lock);\n\tdst_bin_stats->nmalloc += bin->stats.nmalloc;\n\tdst_bin_stats->ndalloc += bin->stats.ndalloc;\n\tdst_bin_stats->nrequests += bin->stats.nrequests;\n\tdst_bin_stats->curregs += bin->stats.curregs;\n\tdst_bin_stats->nfills += bin->stats.nfills;\n\tdst_bin_stats->nflushes += bin->stats.nflushes;\n\tdst_bin_stats->nslabs += bin->stats.nslabs;\n\tdst_bin_stats->reslabs += bin->stats.reslabs;\n\tdst_bin_stats->curslabs += bin->stats.curslabs;\n\tdst_bin_stats->nonfull_slabs += bin->stats.nonfull_slabs;\n\tmalloc_mutex_unlock(tsdn, &bin->lock);\n}\n\n#endif /* JEMALLOC_INTERNAL_BIN_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/bin_stats.h",
    "content": "#ifndef JEMALLOC_INTERNAL_BIN_STATS_H\n#define JEMALLOC_INTERNAL_BIN_STATS_H\n\n#include \"jemalloc/internal/mutex_prof.h\"\n\ntypedef struct bin_stats_s bin_stats_t;\nstruct bin_stats_s {\n\t/*\n\t * Total number of allocation/deallocation requests served directly by\n\t * the bin.  Note that tcache may allocate an object, then recycle it\n\t * many times, resulting many increments to nrequests, but only one\n\t * each to nmalloc and ndalloc.\n\t */\n\tuint64_t\tnmalloc;\n\tuint64_t\tndalloc;\n\n\t/*\n\t * Number of allocation requests that correspond to the size of this\n\t * bin.  This includes requests served by tcache, though tcache only\n\t * periodically merges into this counter.\n\t */\n\tuint64_t\tnrequests;\n\n\t/*\n\t * Current number of regions of this size class, including regions\n\t * currently cached by tcache.\n\t */\n\tsize_t\t\tcurregs;\n\n\t/* Number of tcache fills from this bin. */\n\tuint64_t\tnfills;\n\n\t/* Number of tcache flushes to this bin. */\n\tuint64_t\tnflushes;\n\n\t/* Total number of slabs created for this bin's size class. */\n\tuint64_t\tnslabs;\n\n\t/*\n\t * Total number of slabs reused by extracting them from the slabs heap\n\t * for this bin's size class.\n\t */\n\tuint64_t\treslabs;\n\n\t/* Current number of slabs in this bin. */\n\tsize_t\t\tcurslabs;\n\n\t/* Current size of nonfull slabs heap in this bin. */\n\tsize_t\t\tnonfull_slabs;\n\n\tmutex_prof_data_t mutex_data;\n};\n\n#endif /* JEMALLOC_INTERNAL_BIN_STATS_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/bin_types.h",
    "content": "#ifndef JEMALLOC_INTERNAL_BIN_TYPES_H\n#define JEMALLOC_INTERNAL_BIN_TYPES_H\n\n#include \"jemalloc/internal/sc.h\"\n\n#define BIN_SHARDS_MAX (1 << EXTENT_BITS_BINSHARD_WIDTH)\n#define N_BIN_SHARDS_DEFAULT 1\n\n/* Used in TSD static initializer only. Real init in arena_bind(). */\n#define TSD_BINSHARDS_ZERO_INITIALIZER {{UINT8_MAX}}\n\ntypedef struct tsd_binshards_s tsd_binshards_t;\nstruct tsd_binshards_s {\n\tuint8_t binshard[SC_NBINS];\n};\n\n#endif /* JEMALLOC_INTERNAL_BIN_TYPES_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/bit_util.h",
    "content": "#ifndef JEMALLOC_INTERNAL_BIT_UTIL_H\n#define JEMALLOC_INTERNAL_BIT_UTIL_H\n\n#include \"jemalloc/internal/assert.h\"\n\n#define BIT_UTIL_INLINE static inline\n\n/* Sanity check. */\n#if !defined(JEMALLOC_INTERNAL_FFSLL) || !defined(JEMALLOC_INTERNAL_FFSL) \\\n    || !defined(JEMALLOC_INTERNAL_FFS)\n#  error JEMALLOC_INTERNAL_FFS{,L,LL} should have been defined by configure\n#endif\n\n\nBIT_UTIL_INLINE unsigned\nffs_llu(unsigned long long bitmap) {\n\treturn JEMALLOC_INTERNAL_FFSLL(bitmap);\n}\n\nBIT_UTIL_INLINE unsigned\nffs_lu(unsigned long bitmap) {\n\treturn JEMALLOC_INTERNAL_FFSL(bitmap);\n}\n\nBIT_UTIL_INLINE unsigned\nffs_u(unsigned bitmap) {\n\treturn JEMALLOC_INTERNAL_FFS(bitmap);\n}\n\n#ifdef JEMALLOC_INTERNAL_POPCOUNTL\nBIT_UTIL_INLINE unsigned\npopcount_lu(unsigned long bitmap) {\n  return JEMALLOC_INTERNAL_POPCOUNTL(bitmap);\n}\n#endif\n\n/*\n * Clears first unset bit in bitmap, and returns\n * place of bit.  bitmap *must not* be 0.\n */\n\nBIT_UTIL_INLINE size_t\ncfs_lu(unsigned long* bitmap) {\n\tsize_t bit = ffs_lu(*bitmap) - 1;\n\t*bitmap ^= ZU(1) << bit;\n\treturn bit;\n}\n\nBIT_UTIL_INLINE unsigned\nffs_zu(size_t bitmap) {\n#if LG_SIZEOF_PTR == LG_SIZEOF_INT\n\treturn ffs_u(bitmap);\n#elif LG_SIZEOF_PTR == LG_SIZEOF_LONG\n\treturn ffs_lu(bitmap);\n#elif LG_SIZEOF_PTR == LG_SIZEOF_LONG_LONG\n\treturn ffs_llu(bitmap);\n#else\n#error No implementation for size_t ffs()\n#endif\n}\n\nBIT_UTIL_INLINE unsigned\nffs_u64(uint64_t bitmap) {\n#if LG_SIZEOF_LONG == 3\n\treturn ffs_lu(bitmap);\n#elif LG_SIZEOF_LONG_LONG == 3\n\treturn ffs_llu(bitmap);\n#else\n#error No implementation for 64-bit ffs()\n#endif\n}\n\nBIT_UTIL_INLINE unsigned\nffs_u32(uint32_t bitmap) {\n#if LG_SIZEOF_INT == 2\n\treturn ffs_u(bitmap);\n#else\n#error No implementation for 32-bit ffs()\n#endif\n\treturn ffs_u(bitmap);\n}\n\nBIT_UTIL_INLINE uint64_t\npow2_ceil_u64(uint64_t x) {\n#if (defined(__amd64__) || defined(__x86_64__) || defined(JEMALLOC_HAVE_BUILTIN_CLZ))\n\tif(unlikely(x <= 1)) {\n\t\treturn x;\n\t}\n\tsize_t msb_on_index;\n#if (defined(__amd64__) || defined(__x86_64__))\n\tasm (\"bsrq %1, %0\"\n\t\t\t: \"=r\"(msb_on_index) // Outputs.\n\t\t\t: \"r\"(x-1)           // Inputs.\n\t\t);\n#elif (defined(JEMALLOC_HAVE_BUILTIN_CLZ))\n\tmsb_on_index = (63 ^ __builtin_clzll(x - 1));\n#endif\n\tassert(msb_on_index < 63);\n\treturn 1ULL << (msb_on_index + 1);\n#else\n\tx--;\n\tx |= x >> 1;\n\tx |= x >> 2;\n\tx |= x >> 4;\n\tx |= x >> 8;\n\tx |= x >> 16;\n\tx |= x >> 32;\n\tx++;\n\treturn x;\n#endif\n}\n\nBIT_UTIL_INLINE uint32_t\npow2_ceil_u32(uint32_t x) {\n#if ((defined(__i386__) || defined(JEMALLOC_HAVE_BUILTIN_CLZ)) && (!defined(__s390__)))\n\tif(unlikely(x <= 1)) {\n\t\treturn x;\n\t}\n\tsize_t msb_on_index;\n#if (defined(__i386__))\n\tasm (\"bsr %1, %0\"\n\t\t\t: \"=r\"(msb_on_index) // Outputs.\n\t\t\t: \"r\"(x-1)           // Inputs.\n\t\t);\n#elif (defined(JEMALLOC_HAVE_BUILTIN_CLZ))\n\tmsb_on_index = (31 ^ __builtin_clz(x - 1));\n#endif\n\tassert(msb_on_index < 31);\n\treturn 1U << (msb_on_index + 1);\n#else\n\tx--;\n\tx |= x >> 1;\n\tx |= x >> 2;\n\tx |= x >> 4;\n\tx |= x >> 8;\n\tx |= x >> 16;\n\tx++;\n\treturn x;\n#endif\n}\n\n/* Compute the smallest power of 2 that is >= x. */\nBIT_UTIL_INLINE size_t\npow2_ceil_zu(size_t x) {\n#if (LG_SIZEOF_PTR == 3)\n\treturn pow2_ceil_u64(x);\n#else\n\treturn pow2_ceil_u32(x);\n#endif\n}\n\n#if (defined(__i386__) || defined(__amd64__) || defined(__x86_64__))\nBIT_UTIL_INLINE unsigned\nlg_floor(size_t x) {\n\tsize_t ret;\n\tassert(x != 0);\n\n\tasm (\"bsr %1, %0\"\n\t    : \"=r\"(ret) // Outputs.\n\t    : \"r\"(x)    // Inputs.\n\t    );\n\tassert(ret < UINT_MAX);\n\treturn (unsigned)ret;\n}\n#elif (defined(_MSC_VER))\nBIT_UTIL_INLINE unsigned\nlg_floor(size_t x) {\n\tunsigned long ret;\n\n\tassert(x != 0);\n\n#if (LG_SIZEOF_PTR == 3)\n\t_BitScanReverse64(&ret, x);\n#elif (LG_SIZEOF_PTR == 2)\n\t_BitScanReverse(&ret, x);\n#else\n#  error \"Unsupported type size for lg_floor()\"\n#endif\n\tassert(ret < UINT_MAX);\n\treturn (unsigned)ret;\n}\n#elif (defined(JEMALLOC_HAVE_BUILTIN_CLZ))\nBIT_UTIL_INLINE unsigned\nlg_floor(size_t x) {\n\tassert(x != 0);\n\n#if (LG_SIZEOF_PTR == LG_SIZEOF_INT)\n\treturn ((8 << LG_SIZEOF_PTR) - 1) - __builtin_clz(x);\n#elif (LG_SIZEOF_PTR == LG_SIZEOF_LONG)\n\treturn ((8 << LG_SIZEOF_PTR) - 1) - __builtin_clzl(x);\n#else\n#  error \"Unsupported type size for lg_floor()\"\n#endif\n}\n#else\nBIT_UTIL_INLINE unsigned\nlg_floor(size_t x) {\n\tassert(x != 0);\n\n\tx |= (x >> 1);\n\tx |= (x >> 2);\n\tx |= (x >> 4);\n\tx |= (x >> 8);\n\tx |= (x >> 16);\n#if (LG_SIZEOF_PTR == 3)\n\tx |= (x >> 32);\n#endif\n\tif (x == SIZE_T_MAX) {\n\t\treturn (8 << LG_SIZEOF_PTR) - 1;\n\t}\n\tx++;\n\treturn ffs_zu(x) - 2;\n}\n#endif\n\nBIT_UTIL_INLINE unsigned\nlg_ceil(size_t x) {\n\treturn lg_floor(x) + ((x & (x - 1)) == 0 ? 0 : 1);\n}\n\n#undef BIT_UTIL_INLINE\n\n/* A compile-time version of lg_floor and lg_ceil. */\n#define LG_FLOOR_1(x) 0\n#define LG_FLOOR_2(x) (x < (1ULL << 1) ? LG_FLOOR_1(x) : 1 + LG_FLOOR_1(x >> 1))\n#define LG_FLOOR_4(x) (x < (1ULL << 2) ? LG_FLOOR_2(x) : 2 + LG_FLOOR_2(x >> 2))\n#define LG_FLOOR_8(x) (x < (1ULL << 4) ? LG_FLOOR_4(x) : 4 + LG_FLOOR_4(x >> 4))\n#define LG_FLOOR_16(x) (x < (1ULL << 8) ? LG_FLOOR_8(x) : 8 + LG_FLOOR_8(x >> 8))\n#define LG_FLOOR_32(x) (x < (1ULL << 16) ? LG_FLOOR_16(x) : 16 + LG_FLOOR_16(x >> 16))\n#define LG_FLOOR_64(x) (x < (1ULL << 32) ? LG_FLOOR_32(x) : 32 + LG_FLOOR_32(x >> 32))\n#if LG_SIZEOF_PTR == 2\n#  define LG_FLOOR(x) LG_FLOOR_32((x))\n#else\n#  define LG_FLOOR(x) LG_FLOOR_64((x))\n#endif\n\n#define LG_CEIL(x) (LG_FLOOR(x) + (((x) & ((x) - 1)) == 0 ? 0 : 1))\n\n#endif /* JEMALLOC_INTERNAL_BIT_UTIL_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/bitmap.h",
    "content": "#ifndef JEMALLOC_INTERNAL_BITMAP_H\n#define JEMALLOC_INTERNAL_BITMAP_H\n\n#include \"jemalloc/internal/arena_types.h\"\n#include \"jemalloc/internal/bit_util.h\"\n#include \"jemalloc/internal/sc.h\"\n\ntypedef unsigned long bitmap_t;\n#define LG_SIZEOF_BITMAP\tLG_SIZEOF_LONG\n\n/* Maximum bitmap bit count is 2^LG_BITMAP_MAXBITS. */\n#if LG_SLAB_MAXREGS > LG_CEIL(SC_NSIZES)\n/* Maximum bitmap bit count is determined by maximum regions per slab. */\n#  define LG_BITMAP_MAXBITS\tLG_SLAB_MAXREGS\n#else\n/* Maximum bitmap bit count is determined by number of extent size classes. */\n#  define LG_BITMAP_MAXBITS\tLG_CEIL(SC_NSIZES)\n#endif\n#define BITMAP_MAXBITS\t\t(ZU(1) << LG_BITMAP_MAXBITS)\n\n/* Number of bits per group. */\n#define LG_BITMAP_GROUP_NBITS\t\t(LG_SIZEOF_BITMAP + 3)\n#define BITMAP_GROUP_NBITS\t\t(1U << LG_BITMAP_GROUP_NBITS)\n#define BITMAP_GROUP_NBITS_MASK\t\t(BITMAP_GROUP_NBITS-1)\n\n/*\n * Do some analysis on how big the bitmap is before we use a tree.  For a brute\n * force linear search, if we would have to call ffs_lu() more than 2^3 times,\n * use a tree instead.\n */\n#if LG_BITMAP_MAXBITS - LG_BITMAP_GROUP_NBITS > 3\n#  define BITMAP_USE_TREE\n#endif\n\n/* Number of groups required to store a given number of bits. */\n#define BITMAP_BITS2GROUPS(nbits)\t\t\t\t\t\\\n    (((nbits) + BITMAP_GROUP_NBITS_MASK) >> LG_BITMAP_GROUP_NBITS)\n\n/*\n * Number of groups required at a particular level for a given number of bits.\n */\n#define BITMAP_GROUPS_L0(nbits)\t\t\t\t\t\t\\\n    BITMAP_BITS2GROUPS(nbits)\n#define BITMAP_GROUPS_L1(nbits)\t\t\t\t\t\t\\\n    BITMAP_BITS2GROUPS(BITMAP_BITS2GROUPS(nbits))\n#define BITMAP_GROUPS_L2(nbits)\t\t\t\t\t\t\\\n    BITMAP_BITS2GROUPS(BITMAP_BITS2GROUPS(BITMAP_BITS2GROUPS((nbits))))\n#define BITMAP_GROUPS_L3(nbits)\t\t\t\t\t\t\\\n    BITMAP_BITS2GROUPS(BITMAP_BITS2GROUPS(BITMAP_BITS2GROUPS(\t\t\\\n\tBITMAP_BITS2GROUPS((nbits)))))\n#define BITMAP_GROUPS_L4(nbits)\t\t\t\t\t\t\\\n    BITMAP_BITS2GROUPS(BITMAP_BITS2GROUPS(BITMAP_BITS2GROUPS(\t\t\\\n\tBITMAP_BITS2GROUPS(BITMAP_BITS2GROUPS((nbits))))))\n\n/*\n * Assuming the number of levels, number of groups required for a given number\n * of bits.\n */\n#define BITMAP_GROUPS_1_LEVEL(nbits)\t\t\t\t\t\\\n    BITMAP_GROUPS_L0(nbits)\n#define BITMAP_GROUPS_2_LEVEL(nbits)\t\t\t\t\t\\\n    (BITMAP_GROUPS_1_LEVEL(nbits) + BITMAP_GROUPS_L1(nbits))\n#define BITMAP_GROUPS_3_LEVEL(nbits)\t\t\t\t\t\\\n    (BITMAP_GROUPS_2_LEVEL(nbits) + BITMAP_GROUPS_L2(nbits))\n#define BITMAP_GROUPS_4_LEVEL(nbits)\t\t\t\t\t\\\n    (BITMAP_GROUPS_3_LEVEL(nbits) + BITMAP_GROUPS_L3(nbits))\n#define BITMAP_GROUPS_5_LEVEL(nbits)\t\t\t\t\t\\\n    (BITMAP_GROUPS_4_LEVEL(nbits) + BITMAP_GROUPS_L4(nbits))\n\n/*\n * Maximum number of groups required to support LG_BITMAP_MAXBITS.\n */\n#ifdef BITMAP_USE_TREE\n\n#if LG_BITMAP_MAXBITS <= LG_BITMAP_GROUP_NBITS\n#  define BITMAP_GROUPS(nbits)\tBITMAP_GROUPS_1_LEVEL(nbits)\n#  define BITMAP_GROUPS_MAX\tBITMAP_GROUPS_1_LEVEL(BITMAP_MAXBITS)\n#elif LG_BITMAP_MAXBITS <= LG_BITMAP_GROUP_NBITS * 2\n#  define BITMAP_GROUPS(nbits)\tBITMAP_GROUPS_2_LEVEL(nbits)\n#  define BITMAP_GROUPS_MAX\tBITMAP_GROUPS_2_LEVEL(BITMAP_MAXBITS)\n#elif LG_BITMAP_MAXBITS <= LG_BITMAP_GROUP_NBITS * 3\n#  define BITMAP_GROUPS(nbits)\tBITMAP_GROUPS_3_LEVEL(nbits)\n#  define BITMAP_GROUPS_MAX\tBITMAP_GROUPS_3_LEVEL(BITMAP_MAXBITS)\n#elif LG_BITMAP_MAXBITS <= LG_BITMAP_GROUP_NBITS * 4\n#  define BITMAP_GROUPS(nbits)\tBITMAP_GROUPS_4_LEVEL(nbits)\n#  define BITMAP_GROUPS_MAX\tBITMAP_GROUPS_4_LEVEL(BITMAP_MAXBITS)\n#elif LG_BITMAP_MAXBITS <= LG_BITMAP_GROUP_NBITS * 5\n#  define BITMAP_GROUPS(nbits)\tBITMAP_GROUPS_5_LEVEL(nbits)\n#  define BITMAP_GROUPS_MAX\tBITMAP_GROUPS_5_LEVEL(BITMAP_MAXBITS)\n#else\n#  error \"Unsupported bitmap size\"\n#endif\n\n/*\n * Maximum number of levels possible.  This could be statically computed based\n * on LG_BITMAP_MAXBITS:\n *\n * #define BITMAP_MAX_LEVELS \\\n *     (LG_BITMAP_MAXBITS / LG_SIZEOF_BITMAP) \\\n *     + !!(LG_BITMAP_MAXBITS % LG_SIZEOF_BITMAP)\n *\n * However, that would not allow the generic BITMAP_INFO_INITIALIZER() macro, so\n * instead hardcode BITMAP_MAX_LEVELS to the largest number supported by the\n * various cascading macros.  The only additional cost this incurs is some\n * unused trailing entries in bitmap_info_t structures; the bitmaps themselves\n * are not impacted.\n */\n#define BITMAP_MAX_LEVELS\t5\n\n#define BITMAP_INFO_INITIALIZER(nbits) {\t\t\t\t\\\n\t/* nbits. */\t\t\t\t\t\t\t\\\n\tnbits,\t\t\t\t\t\t\t\t\\\n\t/* nlevels. */\t\t\t\t\t\t\t\\\n\t(BITMAP_GROUPS_L0(nbits) > BITMAP_GROUPS_L1(nbits)) +\t\t\\\n\t    (BITMAP_GROUPS_L1(nbits) > BITMAP_GROUPS_L2(nbits)) +\t\\\n\t    (BITMAP_GROUPS_L2(nbits) > BITMAP_GROUPS_L3(nbits)) +\t\\\n\t    (BITMAP_GROUPS_L3(nbits) > BITMAP_GROUPS_L4(nbits)) + 1,\t\\\n\t/* levels. */\t\t\t\t\t\t\t\\\n\t{\t\t\t\t\t\t\t\t\\\n\t\t{0},\t\t\t\t\t\t\t\\\n\t\t{BITMAP_GROUPS_L0(nbits)},\t\t\t\t\\\n\t\t{BITMAP_GROUPS_L1(nbits) + BITMAP_GROUPS_L0(nbits)},\t\\\n\t\t{BITMAP_GROUPS_L2(nbits) + BITMAP_GROUPS_L1(nbits) +\t\\\n\t\t    BITMAP_GROUPS_L0(nbits)},\t\t\t\t\\\n\t\t{BITMAP_GROUPS_L3(nbits) + BITMAP_GROUPS_L2(nbits) +\t\\\n\t\t    BITMAP_GROUPS_L1(nbits) + BITMAP_GROUPS_L0(nbits)},\t\\\n\t\t{BITMAP_GROUPS_L4(nbits) + BITMAP_GROUPS_L3(nbits) +\t\\\n\t\t     BITMAP_GROUPS_L2(nbits) + BITMAP_GROUPS_L1(nbits)\t\\\n\t\t     + BITMAP_GROUPS_L0(nbits)}\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n}\n\n#else /* BITMAP_USE_TREE */\n\n#define BITMAP_GROUPS(nbits)\tBITMAP_BITS2GROUPS(nbits)\n#define BITMAP_GROUPS_MAX\tBITMAP_BITS2GROUPS(BITMAP_MAXBITS)\n\n#define BITMAP_INFO_INITIALIZER(nbits) {\t\t\t\t\\\n\t/* nbits. */\t\t\t\t\t\t\t\\\n\tnbits,\t\t\t\t\t\t\t\t\\\n\t/* ngroups. */\t\t\t\t\t\t\t\\\n\tBITMAP_BITS2GROUPS(nbits)\t\t\t\t\t\\\n}\n\n#endif /* BITMAP_USE_TREE */\n\ntypedef struct bitmap_level_s {\n\t/* Offset of this level's groups within the array of groups. */\n\tsize_t group_offset;\n} bitmap_level_t;\n\ntypedef struct bitmap_info_s {\n\t/* Logical number of bits in bitmap (stored at bottom level). */\n\tsize_t nbits;\n\n#ifdef BITMAP_USE_TREE\n\t/* Number of levels necessary for nbits. */\n\tunsigned nlevels;\n\n\t/*\n\t * Only the first (nlevels+1) elements are used, and levels are ordered\n\t * bottom to top (e.g. the bottom level is stored in levels[0]).\n\t */\n\tbitmap_level_t levels[BITMAP_MAX_LEVELS+1];\n#else /* BITMAP_USE_TREE */\n\t/* Number of groups necessary for nbits. */\n\tsize_t ngroups;\n#endif /* BITMAP_USE_TREE */\n} bitmap_info_t;\n\nvoid bitmap_info_init(bitmap_info_t *binfo, size_t nbits);\nvoid bitmap_init(bitmap_t *bitmap, const bitmap_info_t *binfo, bool fill);\nsize_t bitmap_size(const bitmap_info_t *binfo);\n\nstatic inline bool\nbitmap_full(bitmap_t *bitmap, const bitmap_info_t *binfo) {\n#ifdef BITMAP_USE_TREE\n\tsize_t rgoff = binfo->levels[binfo->nlevels].group_offset - 1;\n\tbitmap_t rg = bitmap[rgoff];\n\t/* The bitmap is full iff the root group is 0. */\n\treturn (rg == 0);\n#else\n\tsize_t i;\n\n\tfor (i = 0; i < binfo->ngroups; i++) {\n\t\tif (bitmap[i] != 0) {\n\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n#endif\n}\n\nstatic inline bool\nbitmap_get(bitmap_t *bitmap, const bitmap_info_t *binfo, size_t bit) {\n\tsize_t goff;\n\tbitmap_t g;\n\n\tassert(bit < binfo->nbits);\n\tgoff = bit >> LG_BITMAP_GROUP_NBITS;\n\tg = bitmap[goff];\n\treturn !(g & (ZU(1) << (bit & BITMAP_GROUP_NBITS_MASK)));\n}\n\nstatic inline void\nbitmap_set(bitmap_t *bitmap, const bitmap_info_t *binfo, size_t bit) {\n\tsize_t goff;\n\tbitmap_t *gp;\n\tbitmap_t g;\n\n\tassert(bit < binfo->nbits);\n\tassert(!bitmap_get(bitmap, binfo, bit));\n\tgoff = bit >> LG_BITMAP_GROUP_NBITS;\n\tgp = &bitmap[goff];\n\tg = *gp;\n\tassert(g & (ZU(1) << (bit & BITMAP_GROUP_NBITS_MASK)));\n\tg ^= ZU(1) << (bit & BITMAP_GROUP_NBITS_MASK);\n\t*gp = g;\n\tassert(bitmap_get(bitmap, binfo, bit));\n#ifdef BITMAP_USE_TREE\n\t/* Propagate group state transitions up the tree. */\n\tif (g == 0) {\n\t\tunsigned i;\n\t\tfor (i = 1; i < binfo->nlevels; i++) {\n\t\t\tbit = goff;\n\t\t\tgoff = bit >> LG_BITMAP_GROUP_NBITS;\n\t\t\tgp = &bitmap[binfo->levels[i].group_offset + goff];\n\t\t\tg = *gp;\n\t\t\tassert(g & (ZU(1) << (bit & BITMAP_GROUP_NBITS_MASK)));\n\t\t\tg ^= ZU(1) << (bit & BITMAP_GROUP_NBITS_MASK);\n\t\t\t*gp = g;\n\t\t\tif (g != 0) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n#endif\n}\n\n/* ffu: find first unset >= bit. */\nstatic inline size_t\nbitmap_ffu(const bitmap_t *bitmap, const bitmap_info_t *binfo, size_t min_bit) {\n\tassert(min_bit < binfo->nbits);\n\n#ifdef BITMAP_USE_TREE\n\tsize_t bit = 0;\n\tfor (unsigned level = binfo->nlevels; level--;) {\n\t\tsize_t lg_bits_per_group = (LG_BITMAP_GROUP_NBITS * (level +\n\t\t    1));\n\t\tbitmap_t group = bitmap[binfo->levels[level].group_offset + (bit\n\t\t    >> lg_bits_per_group)];\n\t\tunsigned group_nmask = (unsigned)(((min_bit > bit) ? (min_bit -\n\t\t    bit) : 0) >> (lg_bits_per_group - LG_BITMAP_GROUP_NBITS));\n\t\tassert(group_nmask <= BITMAP_GROUP_NBITS);\n\t\tbitmap_t group_mask = ~((1LU << group_nmask) - 1);\n\t\tbitmap_t group_masked = group & group_mask;\n\t\tif (group_masked == 0LU) {\n\t\t\tif (group == 0LU) {\n\t\t\t\treturn binfo->nbits;\n\t\t\t}\n\t\t\t/*\n\t\t\t * min_bit was preceded by one or more unset bits in\n\t\t\t * this group, but there are no other unset bits in this\n\t\t\t * group.  Try again starting at the first bit of the\n\t\t\t * next sibling.  This will recurse at most once per\n\t\t\t * non-root level.\n\t\t\t */\n\t\t\tsize_t sib_base = bit + (ZU(1) << lg_bits_per_group);\n\t\t\tassert(sib_base > min_bit);\n\t\t\tassert(sib_base > bit);\n\t\t\tif (sib_base >= binfo->nbits) {\n\t\t\t\treturn binfo->nbits;\n\t\t\t}\n\t\t\treturn bitmap_ffu(bitmap, binfo, sib_base);\n\t\t}\n\t\tbit += ((size_t)(ffs_lu(group_masked) - 1)) <<\n\t\t    (lg_bits_per_group - LG_BITMAP_GROUP_NBITS);\n\t}\n\tassert(bit >= min_bit);\n\tassert(bit < binfo->nbits);\n\treturn bit;\n#else\n\tsize_t i = min_bit >> LG_BITMAP_GROUP_NBITS;\n\tbitmap_t g = bitmap[i] & ~((1LU << (min_bit & BITMAP_GROUP_NBITS_MASK))\n\t    - 1);\n\tsize_t bit;\n\tdo {\n\t\tbit = ffs_lu(g);\n\t\tif (bit != 0) {\n\t\t\treturn (i << LG_BITMAP_GROUP_NBITS) + (bit - 1);\n\t\t}\n\t\ti++;\n\t\tg = bitmap[i];\n\t} while (i < binfo->ngroups);\n\treturn binfo->nbits;\n#endif\n}\n\n/* sfu: set first unset. */\nstatic inline size_t\nbitmap_sfu(bitmap_t *bitmap, const bitmap_info_t *binfo) {\n\tsize_t bit;\n\tbitmap_t g;\n\tunsigned i;\n\n\tassert(!bitmap_full(bitmap, binfo));\n\n#ifdef BITMAP_USE_TREE\n\ti = binfo->nlevels - 1;\n\tg = bitmap[binfo->levels[i].group_offset];\n\tbit = ffs_lu(g) - 1;\n\twhile (i > 0) {\n\t\ti--;\n\t\tg = bitmap[binfo->levels[i].group_offset + bit];\n\t\tbit = (bit << LG_BITMAP_GROUP_NBITS) + (ffs_lu(g) - 1);\n\t}\n#else\n\ti = 0;\n\tg = bitmap[0];\n\twhile ((bit = ffs_lu(g)) == 0) {\n\t\ti++;\n\t\tg = bitmap[i];\n\t}\n\tbit = (i << LG_BITMAP_GROUP_NBITS) + (bit - 1);\n#endif\n\tbitmap_set(bitmap, binfo, bit);\n\treturn bit;\n}\n\nstatic inline void\nbitmap_unset(bitmap_t *bitmap, const bitmap_info_t *binfo, size_t bit) {\n\tsize_t goff;\n\tbitmap_t *gp;\n\tbitmap_t g;\n\tUNUSED bool propagate;\n\n\tassert(bit < binfo->nbits);\n\tassert(bitmap_get(bitmap, binfo, bit));\n\tgoff = bit >> LG_BITMAP_GROUP_NBITS;\n\tgp = &bitmap[goff];\n\tg = *gp;\n\tpropagate = (g == 0);\n\tassert((g & (ZU(1) << (bit & BITMAP_GROUP_NBITS_MASK))) == 0);\n\tg ^= ZU(1) << (bit & BITMAP_GROUP_NBITS_MASK);\n\t*gp = g;\n\tassert(!bitmap_get(bitmap, binfo, bit));\n#ifdef BITMAP_USE_TREE\n\t/* Propagate group state transitions up the tree. */\n\tif (propagate) {\n\t\tunsigned i;\n\t\tfor (i = 1; i < binfo->nlevels; i++) {\n\t\t\tbit = goff;\n\t\t\tgoff = bit >> LG_BITMAP_GROUP_NBITS;\n\t\t\tgp = &bitmap[binfo->levels[i].group_offset + goff];\n\t\t\tg = *gp;\n\t\t\tpropagate = (g == 0);\n\t\t\tassert((g & (ZU(1) << (bit & BITMAP_GROUP_NBITS_MASK)))\n\t\t\t    == 0);\n\t\t\tg ^= ZU(1) << (bit & BITMAP_GROUP_NBITS_MASK);\n\t\t\t*gp = g;\n\t\t\tif (!propagate) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n#endif /* BITMAP_USE_TREE */\n}\n\n#endif /* JEMALLOC_INTERNAL_BITMAP_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/cache_bin.h",
    "content": "#ifndef JEMALLOC_INTERNAL_CACHE_BIN_H\n#define JEMALLOC_INTERNAL_CACHE_BIN_H\n\n#include \"jemalloc/internal/ql.h\"\n\n/*\n * The cache_bins are the mechanism that the tcache and the arena use to\n * communicate.  The tcache fills from and flushes to the arena by passing a\n * cache_bin_t to fill/flush.  When the arena needs to pull stats from the\n * tcaches associated with it, it does so by iterating over its\n * cache_bin_array_descriptor_t objects and reading out per-bin stats it\n * contains.  This makes it so that the arena need not know about the existence\n * of the tcache at all.\n */\n\n\n/*\n * The count of the number of cached allocations in a bin.  We make this signed\n * so that negative numbers can encode \"invalid\" states (e.g. a low water mark\n * of -1 for a cache that has been depleted).\n */\ntypedef int32_t cache_bin_sz_t;\n\ntypedef struct cache_bin_stats_s cache_bin_stats_t;\nstruct cache_bin_stats_s {\n\t/*\n\t * Number of allocation requests that corresponded to the size of this\n\t * bin.\n\t */\n\tuint64_t nrequests;\n};\n\n/*\n * Read-only information associated with each element of tcache_t's tbins array\n * is stored separately, mainly to reduce memory usage.\n */\ntypedef struct cache_bin_info_s cache_bin_info_t;\nstruct cache_bin_info_s {\n\t/* Upper limit on ncached. */\n\tcache_bin_sz_t ncached_max;\n};\n\ntypedef struct cache_bin_s cache_bin_t;\nstruct cache_bin_s {\n\t/* Min # cached since last GC. */\n\tcache_bin_sz_t low_water;\n\t/* # of cached objects. */\n\tcache_bin_sz_t ncached;\n\t/*\n\t * ncached and stats are both modified frequently.  Let's keep them\n\t * close so that they have a higher chance of being on the same\n\t * cacheline, thus less write-backs.\n\t */\n\tcache_bin_stats_t tstats;\n\t/*\n\t * Stack of available objects.\n\t *\n\t * To make use of adjacent cacheline prefetch, the items in the avail\n\t * stack goes to higher address for newer allocations.  avail points\n\t * just above the available space, which means that\n\t * avail[-ncached, ... -1] are available items and the lowest item will\n\t * be allocated first.\n\t */\n\tvoid **avail;\n};\n\ntypedef struct cache_bin_array_descriptor_s cache_bin_array_descriptor_t;\nstruct cache_bin_array_descriptor_s {\n\t/*\n\t * The arena keeps a list of the cache bins associated with it, for\n\t * stats collection.\n\t */\n\tql_elm(cache_bin_array_descriptor_t) link;\n\t/* Pointers to the tcache bins. */\n\tcache_bin_t *bins_small;\n\tcache_bin_t *bins_large;\n};\n\nstatic inline void\ncache_bin_array_descriptor_init(cache_bin_array_descriptor_t *descriptor,\n    cache_bin_t *bins_small, cache_bin_t *bins_large) {\n\tql_elm_new(descriptor, link);\n\tdescriptor->bins_small = bins_small;\n\tdescriptor->bins_large = bins_large;\n}\n\nJEMALLOC_ALWAYS_INLINE void *\ncache_bin_alloc_easy(cache_bin_t *bin, bool *success) {\n\tvoid *ret;\n\n\tbin->ncached--;\n\n\t/*\n\t * Check for both bin->ncached == 0 and ncached < low_water\n\t * in a single branch.\n\t */\n\tif (unlikely(bin->ncached <= bin->low_water)) {\n\t\tbin->low_water = bin->ncached;\n\t\tif (bin->ncached == -1) {\n\t\t\tbin->ncached = 0;\n\t\t\t*success = false;\n\t\t\treturn NULL;\n\t\t}\n\t}\n\n\t/*\n\t * success (instead of ret) should be checked upon the return of this\n\t * function.  We avoid checking (ret == NULL) because there is never a\n\t * null stored on the avail stack (which is unknown to the compiler),\n\t * and eagerly checking ret would cause pipeline stall (waiting for the\n\t * cacheline).\n\t */\n\t*success = true;\n\tret = *(bin->avail - (bin->ncached + 1));\n\n\treturn ret;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ncache_bin_dalloc_easy(cache_bin_t *bin, cache_bin_info_t *bin_info, void *ptr) {\n\tif (unlikely(bin->ncached == bin_info->ncached_max)) {\n\t\treturn false;\n\t}\n\tassert(bin->ncached < bin_info->ncached_max);\n\tbin->ncached++;\n\t*(bin->avail - bin->ncached) = ptr;\n\n\treturn true;\n}\n\n#endif /* JEMALLOC_INTERNAL_CACHE_BIN_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/ckh.h",
    "content": "#ifndef JEMALLOC_INTERNAL_CKH_H\n#define JEMALLOC_INTERNAL_CKH_H\n\n#include \"jemalloc/internal/tsd.h\"\n\n/* Cuckoo hashing implementation.  Skip to the end for the interface. */\n\n/******************************************************************************/\n/* INTERNAL DEFINITIONS -- IGNORE */\n/******************************************************************************/\n\n/* Maintain counters used to get an idea of performance. */\n/* #define CKH_COUNT */\n/* Print counter values in ckh_delete() (requires CKH_COUNT). */\n/* #define CKH_VERBOSE */\n\n/*\n * There are 2^LG_CKH_BUCKET_CELLS cells in each hash table bucket.  Try to fit\n * one bucket per L1 cache line.\n */\n#define LG_CKH_BUCKET_CELLS (LG_CACHELINE - LG_SIZEOF_PTR - 1)\n\n/* Typedefs to allow easy function pointer passing. */\ntypedef void ckh_hash_t (const void *, size_t[2]);\ntypedef bool ckh_keycomp_t (const void *, const void *);\n\n/* Hash table cell. */\ntypedef struct {\n\tconst void *key;\n\tconst void *data;\n} ckhc_t;\n\n/* The hash table itself. */\ntypedef struct {\n#ifdef CKH_COUNT\n\t/* Counters used to get an idea of performance. */\n\tuint64_t ngrows;\n\tuint64_t nshrinks;\n\tuint64_t nshrinkfails;\n\tuint64_t ninserts;\n\tuint64_t nrelocs;\n#endif\n\n\t/* Used for pseudo-random number generation. */\n\tuint64_t prng_state;\n\n\t/* Total number of items. */\n\tsize_t count;\n\n\t/*\n\t * Minimum and current number of hash table buckets.  There are\n\t * 2^LG_CKH_BUCKET_CELLS cells per bucket.\n\t */\n\tunsigned lg_minbuckets;\n\tunsigned lg_curbuckets;\n\n\t/* Hash and comparison functions. */\n\tckh_hash_t *hash;\n\tckh_keycomp_t *keycomp;\n\n\t/* Hash table with 2^lg_curbuckets buckets. */\n\tckhc_t *tab;\n} ckh_t;\n\n/******************************************************************************/\n/* BEGIN PUBLIC API */\n/******************************************************************************/\n\n/* Lifetime management.  Minitems is the initial capacity. */\nbool ckh_new(tsd_t *tsd, ckh_t *ckh, size_t minitems, ckh_hash_t *hash,\n    ckh_keycomp_t *keycomp);\nvoid ckh_delete(tsd_t *tsd, ckh_t *ckh);\n\n/* Get the number of elements in the set. */\nsize_t ckh_count(ckh_t *ckh);\n\n/*\n * To iterate over the elements in the table, initialize *tabind to 0 and call\n * this function until it returns true.  Each call that returns false will\n * update *key and *data to the next element in the table, assuming the pointers\n * are non-NULL.\n */\nbool ckh_iter(ckh_t *ckh, size_t *tabind, void **key, void **data);\n\n/*\n * Basic hash table operations -- insert, removal, lookup.  For ckh_remove and\n * ckh_search, key or data can be NULL.  The hash-table only stores pointers to\n * the key and value, and doesn't do any lifetime management.\n */\nbool ckh_insert(tsd_t *tsd, ckh_t *ckh, const void *key, const void *data);\nbool ckh_remove(tsd_t *tsd, ckh_t *ckh, const void *searchkey, void **key,\n    void **data);\nbool ckh_search(ckh_t *ckh, const void *searchkey, void **key, void **data);\n\n/* Some useful hash and comparison functions for strings and pointers. */\nvoid ckh_string_hash(const void *key, size_t r_hash[2]);\nbool ckh_string_keycomp(const void *k1, const void *k2);\nvoid ckh_pointer_hash(const void *key, size_t r_hash[2]);\nbool ckh_pointer_keycomp(const void *k1, const void *k2);\n\n#endif /* JEMALLOC_INTERNAL_CKH_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/ctl.h",
    "content": "#ifndef JEMALLOC_INTERNAL_CTL_H\n#define JEMALLOC_INTERNAL_CTL_H\n\n#include \"jemalloc/internal/jemalloc_internal_types.h\"\n#include \"jemalloc/internal/malloc_io.h\"\n#include \"jemalloc/internal/mutex_prof.h\"\n#include \"jemalloc/internal/ql.h\"\n#include \"jemalloc/internal/sc.h\"\n#include \"jemalloc/internal/stats.h\"\n\n/* Maximum ctl tree depth. */\n#define CTL_MAX_DEPTH\t7\n\ntypedef struct ctl_node_s {\n\tbool named;\n} ctl_node_t;\n\ntypedef struct ctl_named_node_s {\n\tctl_node_t node;\n\tconst char *name;\n\t/* If (nchildren == 0), this is a terminal node. */\n\tsize_t nchildren;\n\tconst ctl_node_t *children;\n\tint (*ctl)(tsd_t *, const size_t *, size_t, void *, size_t *, void *,\n\t    size_t);\n} ctl_named_node_t;\n\ntypedef struct ctl_indexed_node_s {\n\tstruct ctl_node_s node;\n\tconst ctl_named_node_t *(*index)(tsdn_t *, const size_t *, size_t,\n\t    size_t);\n} ctl_indexed_node_t;\n\ntypedef struct ctl_arena_stats_s {\n\tarena_stats_t astats;\n\n\t/* Aggregate stats for small size classes, based on bin stats. */\n\tsize_t allocated_small;\n\tuint64_t nmalloc_small;\n\tuint64_t ndalloc_small;\n\tuint64_t nrequests_small;\n\tuint64_t nfills_small;\n\tuint64_t nflushes_small;\n\n\tbin_stats_t bstats[SC_NBINS];\n\tarena_stats_large_t lstats[SC_NSIZES - SC_NBINS];\n\tarena_stats_extents_t estats[SC_NPSIZES];\n} ctl_arena_stats_t;\n\ntypedef struct ctl_stats_s {\n\tsize_t allocated;\n\tsize_t active;\n\tsize_t metadata;\n\tsize_t metadata_thp;\n\tsize_t resident;\n\tsize_t mapped;\n\tsize_t retained;\n\n\tbackground_thread_stats_t background_thread;\n\tmutex_prof_data_t mutex_prof_data[mutex_prof_num_global_mutexes];\n} ctl_stats_t;\n\ntypedef struct ctl_arena_s ctl_arena_t;\nstruct ctl_arena_s {\n\tunsigned arena_ind;\n\tbool initialized;\n\tql_elm(ctl_arena_t) destroyed_link;\n\n\t/* Basic stats, supported even if !config_stats. */\n\tunsigned nthreads;\n\tconst char *dss;\n\tssize_t dirty_decay_ms;\n\tssize_t muzzy_decay_ms;\n\tsize_t pactive;\n\tsize_t pdirty;\n\tsize_t pmuzzy;\n\n\t/* NULL if !config_stats. */\n\tctl_arena_stats_t *astats;\n};\n\ntypedef struct ctl_arenas_s {\n\tuint64_t epoch;\n\tunsigned narenas;\n\tql_head(ctl_arena_t) destroyed;\n\n\t/*\n\t * Element 0 corresponds to merged stats for extant arenas (accessed via\n\t * MALLCTL_ARENAS_ALL), element 1 corresponds to merged stats for\n\t * destroyed arenas (accessed via MALLCTL_ARENAS_DESTROYED), and the\n\t * remaining MALLOCX_ARENA_LIMIT elements correspond to arenas.\n\t */\n\tctl_arena_t *arenas[2 + MALLOCX_ARENA_LIMIT];\n} ctl_arenas_t;\n\nint ctl_byname(tsd_t *tsd, const char *name, void *oldp, size_t *oldlenp,\n    void *newp, size_t newlen);\nint ctl_nametomib(tsd_t *tsd, const char *name, size_t *mibp, size_t *miblenp);\n\nint ctl_bymib(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen);\nbool ctl_boot(void);\nvoid ctl_prefork(tsdn_t *tsdn);\nvoid ctl_postfork_parent(tsdn_t *tsdn);\nvoid ctl_postfork_child(tsdn_t *tsdn);\n\n#define xmallctl(name, oldp, oldlenp, newp, newlen) do {\t\t\\\n\tif (je_mallctl(name, oldp, oldlenp, newp, newlen)\t\t\\\n\t    != 0) {\t\t\t\t\t\t\t\\\n\t\tmalloc_printf(\t\t\t\t\t\t\\\n\t\t    \"<jemalloc>: Failure in xmallctl(\\\"%s\\\", ...)\\n\",\t\\\n\t\t    name);\t\t\t\t\t\t\\\n\t\tabort();\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define xmallctlnametomib(name, mibp, miblenp) do {\t\t\t\\\n\tif (je_mallctlnametomib(name, mibp, miblenp) != 0) {\t\t\\\n\t\tmalloc_printf(\"<jemalloc>: Failure in \"\t\t\t\\\n\t\t    \"xmallctlnametomib(\\\"%s\\\", ...)\\n\", name);\t\t\\\n\t\tabort();\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define xmallctlbymib(mib, miblen, oldp, oldlenp, newp, newlen) do {\t\\\n\tif (je_mallctlbymib(mib, miblen, oldp, oldlenp, newp,\t\t\\\n\t    newlen) != 0) {\t\t\t\t\t\t\\\n\t\tmalloc_write(\t\t\t\t\t\t\\\n\t\t    \"<jemalloc>: Failure in xmallctlbymib()\\n\");\t\\\n\t\tabort();\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#endif /* JEMALLOC_INTERNAL_CTL_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/div.h",
    "content": "#ifndef JEMALLOC_INTERNAL_DIV_H\n#define JEMALLOC_INTERNAL_DIV_H\n\n#include \"jemalloc/internal/assert.h\"\n\n/*\n * This module does the division that computes the index of a region in a slab,\n * given its offset relative to the base.\n * That is, given a divisor d, an n = i * d (all integers), we'll return i.\n * We do some pre-computation to do this more quickly than a CPU division\n * instruction.\n * We bound n < 2^32, and don't support dividing by one.\n */\n\ntypedef struct div_info_s div_info_t;\nstruct div_info_s {\n\tuint32_t magic;\n#ifdef JEMALLOC_DEBUG\n\tsize_t d;\n#endif\n};\n\nvoid div_init(div_info_t *div_info, size_t divisor);\n\nstatic inline size_t\ndiv_compute(div_info_t *div_info, size_t n) {\n\tassert(n <= (uint32_t)-1);\n\t/*\n\t * This generates, e.g. mov; imul; shr on x86-64. On a 32-bit machine,\n\t * the compilers I tried were all smart enough to turn this into the\n\t * appropriate \"get the high 32 bits of the result of a multiply\" (e.g.\n\t * mul; mov edx eax; on x86, umull on arm, etc.).\n\t */\n\tsize_t i = ((uint64_t)n * (uint64_t)div_info->magic) >> 32;\n#ifdef JEMALLOC_DEBUG\n\tassert(i * div_info->d == n);\n#endif\n\treturn i;\n}\n\n#endif /* JEMALLOC_INTERNAL_DIV_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/emitter.h",
    "content": "#ifndef JEMALLOC_INTERNAL_EMITTER_H\n#define JEMALLOC_INTERNAL_EMITTER_H\n\n#include \"jemalloc/internal/ql.h\"\n\ntypedef enum emitter_output_e emitter_output_t;\nenum emitter_output_e {\n\temitter_output_json,\n\temitter_output_table\n};\n\ntypedef enum emitter_justify_e emitter_justify_t;\nenum emitter_justify_e {\n\temitter_justify_left,\n\temitter_justify_right,\n\t/* Not for users; just to pass to internal functions. */\n\temitter_justify_none\n};\n\ntypedef enum emitter_type_e emitter_type_t;\nenum emitter_type_e {\n\temitter_type_bool,\n\temitter_type_int,\n\temitter_type_unsigned,\n\temitter_type_uint32,\n\temitter_type_uint64,\n\temitter_type_size,\n\temitter_type_ssize,\n\temitter_type_string,\n\t/*\n\t * A title is a column title in a table; it's just a string, but it's\n\t * not quoted.\n\t */\n\temitter_type_title,\n};\n\ntypedef struct emitter_col_s emitter_col_t;\nstruct emitter_col_s {\n\t/* Filled in by the user. */\n\temitter_justify_t justify;\n\tint width;\n\temitter_type_t type;\n\tunion {\n\t\tbool bool_val;\n\t\tint int_val;\n\t\tunsigned unsigned_val;\n\t\tuint32_t uint32_val;\n\t\tuint32_t uint32_t_val;\n\t\tuint64_t uint64_val;\n\t\tuint64_t uint64_t_val;\n\t\tsize_t size_val;\n\t\tssize_t ssize_val;\n\t\tconst char *str_val;\n\t};\n\n\t/* Filled in by initialization. */\n\tql_elm(emitter_col_t) link;\n};\n\ntypedef struct emitter_row_s emitter_row_t;\nstruct emitter_row_s {\n\tql_head(emitter_col_t) cols;\n};\n\ntypedef struct emitter_s emitter_t;\nstruct emitter_s {\n\temitter_output_t output;\n\t/* The output information. */\n\tvoid (*write_cb)(void *, const char *);\n\tvoid *cbopaque;\n\tint nesting_depth;\n\t/* True if we've already emitted a value at the given depth. */\n\tbool item_at_depth;\n\t/* True if we emitted a key and will emit corresponding value next. */\n\tbool emitted_key;\n};\n\n/* Internal convenience function.  Write to the emitter the given string. */\nJEMALLOC_FORMAT_PRINTF(2, 3)\nstatic inline void\nemitter_printf(emitter_t *emitter, const char *format, ...) {\n\tva_list ap;\n\n\tva_start(ap, format);\n\tmalloc_vcprintf(emitter->write_cb, emitter->cbopaque, format, ap);\n\tva_end(ap);\n}\n\nstatic inline const char * JEMALLOC_FORMAT_ARG(3)\nemitter_gen_fmt(char *out_fmt, size_t out_size, const char *fmt_specifier,\n    emitter_justify_t justify, int width) {\n\tsize_t written;\n\tfmt_specifier++;\n\tif (justify == emitter_justify_none) {\n\t\twritten = malloc_snprintf(out_fmt, out_size,\n\t\t    \"%%%s\", fmt_specifier);\n\t} else if (justify == emitter_justify_left) {\n\t\twritten = malloc_snprintf(out_fmt, out_size,\n\t\t    \"%%-%d%s\", width, fmt_specifier);\n\t} else {\n\t\twritten = malloc_snprintf(out_fmt, out_size,\n\t\t    \"%%%d%s\", width, fmt_specifier);\n\t}\n\t/* Only happens in case of bad format string, which *we* choose. */\n\tassert(written <  out_size);\n\treturn out_fmt;\n}\n\n/*\n * Internal.  Emit the given value type in the relevant encoding (so that the\n * bool true gets mapped to json \"true\", but the string \"true\" gets mapped to\n * json \"\\\"true\\\"\", for instance.\n *\n * Width is ignored if justify is emitter_justify_none.\n */\nstatic inline void\nemitter_print_value(emitter_t *emitter, emitter_justify_t justify, int width,\n    emitter_type_t value_type, const void *value) {\n\tsize_t str_written;\n#define BUF_SIZE 256\n#define FMT_SIZE 10\n\t/*\n\t * We dynamically generate a format string to emit, to let us use the\n\t * snprintf machinery.  This is kinda hacky, but gets the job done\n\t * quickly without having to think about the various snprintf edge\n\t * cases.\n\t */\n\tchar fmt[FMT_SIZE];\n\tchar buf[BUF_SIZE];\n\n#define EMIT_SIMPLE(type, format)\t\t\t\t\t\\\n\temitter_printf(emitter,\t\t\t\t\t\t\\\n\t    emitter_gen_fmt(fmt, FMT_SIZE, format, justify, width),\t\\\n\t    *(const type *)value);\n\n\tswitch (value_type) {\n\tcase emitter_type_bool:\n\t\temitter_printf(emitter, \n\t\t    emitter_gen_fmt(fmt, FMT_SIZE, \"%s\", justify, width),\n\t\t    *(const bool *)value ?  \"true\" : \"false\");\n\t\tbreak;\n\tcase emitter_type_int:\n\t\tEMIT_SIMPLE(int, \"%d\")\n\t\tbreak;\n\tcase emitter_type_unsigned:\n\t\tEMIT_SIMPLE(unsigned, \"%u\")\n\t\tbreak;\n\tcase emitter_type_ssize:\n\t\tEMIT_SIMPLE(ssize_t, \"%zd\")\n\t\tbreak;\n\tcase emitter_type_size:\n\t\tEMIT_SIMPLE(size_t, \"%zu\")\n\t\tbreak;\n\tcase emitter_type_string:\n\t\tstr_written = malloc_snprintf(buf, BUF_SIZE, \"\\\"%s\\\"\",\n\t\t    *(const char *const *)value);\n\t\t/*\n\t\t * We control the strings we output; we shouldn't get anything\n\t\t * anywhere near the fmt size.\n\t\t */\n\t\tassert(str_written < BUF_SIZE);\n\t\temitter_printf(emitter, \n\t\t    emitter_gen_fmt(fmt, FMT_SIZE, \"%s\", justify, width), buf);\n\t\tbreak;\n\tcase emitter_type_uint32:\n\t\tEMIT_SIMPLE(uint32_t, \"%\" FMTu32)\n\t\tbreak;\n\tcase emitter_type_uint64:\n\t\tEMIT_SIMPLE(uint64_t, \"%\" FMTu64)\n\t\tbreak;\n\tcase emitter_type_title:\n\t\tEMIT_SIMPLE(char *const, \"%s\");\n\t\tbreak;\n\tdefault:\n\t\tunreachable();\n\t}\n#undef BUF_SIZE\n#undef FMT_SIZE\n}\n\n\n/* Internal functions.  In json mode, tracks nesting state. */\nstatic inline void\nemitter_nest_inc(emitter_t *emitter) {\n\temitter->nesting_depth++;\n\temitter->item_at_depth = false;\n}\n\nstatic inline void\nemitter_nest_dec(emitter_t *emitter) {\n\temitter->nesting_depth--;\n\temitter->item_at_depth = true;\n}\n\nstatic inline void\nemitter_indent(emitter_t *emitter) {\n\tint amount = emitter->nesting_depth;\n\tconst char *indent_str;\n\tif (emitter->output == emitter_output_json) {\n\t\tindent_str = \"\\t\";\n\t} else {\n\t\tamount *= 2;\n\t\tindent_str = \" \";\n\t}\n\tfor (int i = 0; i < amount; i++) {\n\t\temitter_printf(emitter, \"%s\", indent_str);\n\t}\n}\n\nstatic inline void\nemitter_json_key_prefix(emitter_t *emitter) {\n\tif (emitter->emitted_key) {\n\t\temitter->emitted_key = false;\n\t\treturn;\n\t}\n\temitter_printf(emitter, \"%s\\n\", emitter->item_at_depth ? \",\" : \"\");\n\temitter_indent(emitter);\n}\n\n/******************************************************************************/\n/* Public functions for emitter_t. */\n\nstatic inline void\nemitter_init(emitter_t *emitter, emitter_output_t emitter_output,\n    void (*write_cb)(void *, const char *), void *cbopaque) {\n\temitter->output = emitter_output;\n\temitter->write_cb = write_cb;\n\temitter->cbopaque = cbopaque;\n\temitter->item_at_depth = false;\n\temitter->emitted_key = false; \n\temitter->nesting_depth = 0;\n}\n\n/******************************************************************************/\n/* JSON public API. */\n\n/* \n * Emits a key (e.g. as appears in an object). The next json entity emitted will\n * be the corresponding value.\n */\nstatic inline void\nemitter_json_key(emitter_t *emitter, const char *json_key) {\n\tif (emitter->output == emitter_output_json) {\n\t\temitter_json_key_prefix(emitter);\n\t\temitter_printf(emitter, \"\\\"%s\\\": \", json_key);\n\t\temitter->emitted_key = true;\n\t}\n}\n\nstatic inline void\nemitter_json_value(emitter_t *emitter, emitter_type_t value_type,\n    const void *value) {\n\tif (emitter->output == emitter_output_json) {\n\t\temitter_json_key_prefix(emitter);\n\t\temitter_print_value(emitter, emitter_justify_none, -1,\n\t\t    value_type, value);\n\t\temitter->item_at_depth = true;\n\t}\n}\n\n/* Shorthand for calling emitter_json_key and then emitter_json_value. */\nstatic inline void\nemitter_json_kv(emitter_t *emitter, const char *json_key,\n    emitter_type_t value_type, const void *value) {\n\temitter_json_key(emitter, json_key);\n\temitter_json_value(emitter, value_type, value);\n}\n\nstatic inline void\nemitter_json_array_begin(emitter_t *emitter) {\n\tif (emitter->output == emitter_output_json) {\n\t\temitter_json_key_prefix(emitter);\n\t\temitter_printf(emitter, \"[\");\n\t\temitter_nest_inc(emitter);\n\t}\n}\n\n/* Shorthand for calling emitter_json_key and then emitter_json_array_begin. */\nstatic inline void\nemitter_json_array_kv_begin(emitter_t *emitter, const char *json_key) {\n\temitter_json_key(emitter, json_key);\n\temitter_json_array_begin(emitter);\n}\n\nstatic inline void\nemitter_json_array_end(emitter_t *emitter) {\n\tif (emitter->output == emitter_output_json) {\n\t\tassert(emitter->nesting_depth > 0);\n\t\temitter_nest_dec(emitter);\n\t\temitter_printf(emitter, \"\\n\");\n\t\temitter_indent(emitter);\n\t\temitter_printf(emitter, \"]\");\n\t}\n}\n\nstatic inline void\nemitter_json_object_begin(emitter_t *emitter) {\n\tif (emitter->output == emitter_output_json) {\n\t\temitter_json_key_prefix(emitter);\n\t\temitter_printf(emitter, \"{\");\n\t\temitter_nest_inc(emitter);\n\t}\n}\n\n/* Shorthand for calling emitter_json_key and then emitter_json_object_begin. */\nstatic inline void\nemitter_json_object_kv_begin(emitter_t *emitter, const char *json_key) {\n\temitter_json_key(emitter, json_key);\n\temitter_json_object_begin(emitter);\n}\n\nstatic inline void\nemitter_json_object_end(emitter_t *emitter) {\n\tif (emitter->output == emitter_output_json) {\n\t\tassert(emitter->nesting_depth > 0);\n\t\temitter_nest_dec(emitter);\n\t\temitter_printf(emitter, \"\\n\");\n\t\temitter_indent(emitter);\n\t\temitter_printf(emitter, \"}\");\n\t}\n}\n\n\n/******************************************************************************/\n/* Table public API. */\n\nstatic inline void\nemitter_table_dict_begin(emitter_t *emitter, const char *table_key) {\n\tif (emitter->output == emitter_output_table) {\n\t\temitter_indent(emitter);\n\t\temitter_printf(emitter, \"%s\\n\", table_key);\n\t\temitter_nest_inc(emitter);\n\t}\n}\n\nstatic inline void\nemitter_table_dict_end(emitter_t *emitter) {\n\tif (emitter->output == emitter_output_table) {\n\t\temitter_nest_dec(emitter);\n\t}\n}\n\nstatic inline void\nemitter_table_kv_note(emitter_t *emitter, const char *table_key,\n    emitter_type_t value_type, const void *value,\n    const char *table_note_key, emitter_type_t table_note_value_type,\n    const void *table_note_value) {\n\tif (emitter->output == emitter_output_table) {\n\t\temitter_indent(emitter);\n\t\temitter_printf(emitter, \"%s: \", table_key);\n\t\temitter_print_value(emitter, emitter_justify_none, -1,\n\t\t    value_type, value);\n\t\tif (table_note_key != NULL) {\n\t\t\temitter_printf(emitter, \" (%s: \", table_note_key);\n\t\t\temitter_print_value(emitter, emitter_justify_none, -1,\n\t\t\t    table_note_value_type, table_note_value);\n\t\t\temitter_printf(emitter, \")\");\n\t\t}\n\t\temitter_printf(emitter, \"\\n\");\n\t}\n\temitter->item_at_depth = true;\n}\n\nstatic inline void\nemitter_table_kv(emitter_t *emitter, const char *table_key,\n    emitter_type_t value_type, const void *value) {\n\temitter_table_kv_note(emitter, table_key, value_type, value, NULL,\n\t    emitter_type_bool, NULL);\n}\n\n\n/* Write to the emitter the given string, but only in table mode. */\nJEMALLOC_FORMAT_PRINTF(2, 3)\nstatic inline void\nemitter_table_printf(emitter_t *emitter, const char *format, ...) {\n\tif (emitter->output == emitter_output_table) {\n\t\tva_list ap;\n\t\tva_start(ap, format);\n\t\tmalloc_vcprintf(emitter->write_cb, emitter->cbopaque, format, ap);\n\t\tva_end(ap);\n\t}\n}\n\nstatic inline void\nemitter_table_row(emitter_t *emitter, emitter_row_t *row) {\n\tif (emitter->output != emitter_output_table) {\n\t\treturn;\n\t}\n\temitter_col_t *col;\n\tql_foreach(col, &row->cols, link) {\n\t\temitter_print_value(emitter, col->justify, col->width,\n\t\t    col->type, (const void *)&col->bool_val);\n\t}\n\temitter_table_printf(emitter, \"\\n\");\n}\n\nstatic inline void\nemitter_row_init(emitter_row_t *row) {\n\tql_new(&row->cols);\n}\n\nstatic inline void\nemitter_col_init(emitter_col_t *col, emitter_row_t *row) {\n\tql_elm_new(col, link);\n\tql_tail_insert(&row->cols, col, link);\n}\n\n\n/******************************************************************************/\n/*\n * Generalized public API. Emits using either JSON or table, according to\n * settings in the emitter_t. */\n\n/*\n * Note emits a different kv pair as well, but only in table mode.  Omits the\n * note if table_note_key is NULL.\n */\nstatic inline void\nemitter_kv_note(emitter_t *emitter, const char *json_key, const char *table_key,\n    emitter_type_t value_type, const void *value,\n    const char *table_note_key, emitter_type_t table_note_value_type,\n    const void *table_note_value) {\n\tif (emitter->output == emitter_output_json) {\n\t\temitter_json_key(emitter, json_key);\n\t\temitter_json_value(emitter, value_type, value);\n\t} else {\n\t\temitter_table_kv_note(emitter, table_key, value_type, value,\n\t\t    table_note_key, table_note_value_type, table_note_value);\n\t}\n\temitter->item_at_depth = true;\n}\n\nstatic inline void\nemitter_kv(emitter_t *emitter, const char *json_key, const char *table_key,\n    emitter_type_t value_type, const void *value) {\n\temitter_kv_note(emitter, json_key, table_key, value_type, value, NULL,\n\t    emitter_type_bool, NULL);\n}\n\nstatic inline void\nemitter_dict_begin(emitter_t *emitter, const char *json_key,\n    const char *table_header) {\n\tif (emitter->output == emitter_output_json) {\n\t\temitter_json_key(emitter, json_key);\n\t\temitter_json_object_begin(emitter);\n\t} else {\n\t\temitter_table_dict_begin(emitter, table_header);\n\t}\n}\n\nstatic inline void\nemitter_dict_end(emitter_t *emitter) {\n\tif (emitter->output == emitter_output_json) {\n\t\temitter_json_object_end(emitter);\n\t} else {\n\t\temitter_table_dict_end(emitter);\n\t}\n}\n\nstatic inline void\nemitter_begin(emitter_t *emitter) {\n\tif (emitter->output == emitter_output_json) {\n\t\tassert(emitter->nesting_depth == 0);\n\t\temitter_printf(emitter, \"{\");\n\t\temitter_nest_inc(emitter);\n\t} else {\n\t\t/*\n\t\t * This guarantees that we always call write_cb at least once.\n\t\t * This is useful if some invariant is established by each call\n\t\t * to write_cb, but doesn't hold initially: e.g., some buffer\n\t\t * holds a null-terminated string.\n\t\t */\n\t\temitter_printf(emitter, \"%s\", \"\");\n\t}\n}\n\nstatic inline void\nemitter_end(emitter_t *emitter) {\n\tif (emitter->output == emitter_output_json) {\n\t\tassert(emitter->nesting_depth == 1);\n\t\temitter_nest_dec(emitter);\n\t\temitter_printf(emitter, \"\\n}\\n\");\n\t}\n}\n\n#endif /* JEMALLOC_INTERNAL_EMITTER_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/extent_dss.h",
    "content": "#ifndef JEMALLOC_INTERNAL_EXTENT_DSS_H\n#define JEMALLOC_INTERNAL_EXTENT_DSS_H\n\ntypedef enum {\n\tdss_prec_disabled  = 0,\n\tdss_prec_primary   = 1,\n\tdss_prec_secondary = 2,\n\n\tdss_prec_limit     = 3\n} dss_prec_t;\n#define DSS_PREC_DEFAULT dss_prec_secondary\n#define DSS_DEFAULT \"secondary\"\n\nextern const char *dss_prec_names[];\n\nextern const char *opt_dss;\n\ndss_prec_t extent_dss_prec_get(void);\nbool extent_dss_prec_set(dss_prec_t dss_prec);\nvoid *extent_alloc_dss(tsdn_t *tsdn, arena_t *arena, void *new_addr,\n    size_t size, size_t alignment, bool *zero, bool *commit);\nbool extent_in_dss(void *addr);\nbool extent_dss_mergeable(void *addr_a, void *addr_b);\nvoid extent_dss_boot(void);\n\n#endif /* JEMALLOC_INTERNAL_EXTENT_DSS_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/extent_externs.h",
    "content": "#ifndef JEMALLOC_INTERNAL_EXTENT_EXTERNS_H\n#define JEMALLOC_INTERNAL_EXTENT_EXTERNS_H\n\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/mutex_pool.h\"\n#include \"jemalloc/internal/ph.h\"\n#include \"jemalloc/internal/rtree.h\"\n\nextern size_t opt_lg_extent_max_active_fit;\n\nextern rtree_t extents_rtree;\nextern const extent_hooks_t extent_hooks_default;\nextern mutex_pool_t extent_mutex_pool;\n\nextent_t *extent_alloc(tsdn_t *tsdn, arena_t *arena);\nvoid extent_dalloc(tsdn_t *tsdn, arena_t *arena, extent_t *extent);\n\nextent_hooks_t *extent_hooks_get(arena_t *arena);\nextent_hooks_t *extent_hooks_set(tsd_t *tsd, arena_t *arena,\n    extent_hooks_t *extent_hooks);\n\n#ifdef JEMALLOC_JET\nsize_t extent_size_quantize_floor(size_t size);\nsize_t extent_size_quantize_ceil(size_t size);\n#endif\n\nph_proto(, extent_avail_, extent_tree_t, extent_t)\nph_proto(, extent_heap_, extent_heap_t, extent_t)\n\nbool extents_init(tsdn_t *tsdn, extents_t *extents, extent_state_t state,\n    bool delay_coalesce);\nextent_state_t extents_state_get(const extents_t *extents);\nsize_t extents_npages_get(extents_t *extents);\n/* Get the number of extents in the given page size index. */\nsize_t extents_nextents_get(extents_t *extents, pszind_t ind);\n/* Get the sum total bytes of the extents in the given page size index. */\nsize_t extents_nbytes_get(extents_t *extents, pszind_t ind);\nextent_t *extents_alloc(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extents_t *extents, void *new_addr,\n    size_t size, size_t pad, size_t alignment, bool slab, szind_t szind,\n    bool *zero, bool *commit);\nvoid extents_dalloc(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extents_t *extents, extent_t *extent);\nextent_t *extents_evict(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extents_t *extents, size_t npages_min);\nvoid extents_prefork(tsdn_t *tsdn, extents_t *extents);\nvoid extents_postfork_parent(tsdn_t *tsdn, extents_t *extents);\nvoid extents_postfork_child(tsdn_t *tsdn, extents_t *extents);\nextent_t *extent_alloc_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, void *new_addr, size_t size, size_t pad,\n    size_t alignment, bool slab, szind_t szind, bool *zero, bool *commit);\nvoid extent_dalloc_gap(tsdn_t *tsdn, arena_t *arena, extent_t *extent);\nvoid extent_dalloc_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent);\nvoid extent_destroy_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent);\nbool extent_commit_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length);\nbool extent_decommit_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length);\nbool extent_purge_lazy_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length);\nbool extent_purge_forced_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length);\nextent_t *extent_split_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t size_a,\n    szind_t szind_a, bool slab_a, size_t size_b, szind_t szind_b, bool slab_b);\nbool extent_merge_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *a, extent_t *b);\n\nbool extent_boot(void);\n\nvoid extent_util_stats_get(tsdn_t *tsdn, const void *ptr,\n    size_t *nfree, size_t *nregs, size_t *size);\nvoid extent_util_stats_verbose_get(tsdn_t *tsdn, const void *ptr,\n    size_t *nfree, size_t *nregs, size_t *size,\n    size_t *bin_nfree, size_t *bin_nregs, void **slabcur_addr);\n\n#endif /* JEMALLOC_INTERNAL_EXTENT_EXTERNS_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/extent_inlines.h",
    "content": "#ifndef JEMALLOC_INTERNAL_EXTENT_INLINES_H\n#define JEMALLOC_INTERNAL_EXTENT_INLINES_H\n\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/mutex_pool.h\"\n#include \"jemalloc/internal/pages.h\"\n#include \"jemalloc/internal/prng.h\"\n#include \"jemalloc/internal/ql.h\"\n#include \"jemalloc/internal/sc.h\"\n#include \"jemalloc/internal/sz.h\"\n\nstatic inline void\nextent_lock(tsdn_t *tsdn, extent_t *extent) {\n\tassert(extent != NULL);\n\tmutex_pool_lock(tsdn, &extent_mutex_pool, (uintptr_t)extent);\n}\n\nstatic inline void\nextent_unlock(tsdn_t *tsdn, extent_t *extent) {\n\tassert(extent != NULL);\n\tmutex_pool_unlock(tsdn, &extent_mutex_pool, (uintptr_t)extent);\n}\n\nstatic inline void\nextent_lock2(tsdn_t *tsdn, extent_t *extent1, extent_t *extent2) {\n\tassert(extent1 != NULL && extent2 != NULL);\n\tmutex_pool_lock2(tsdn, &extent_mutex_pool, (uintptr_t)extent1,\n\t    (uintptr_t)extent2);\n}\n\nstatic inline void\nextent_unlock2(tsdn_t *tsdn, extent_t *extent1, extent_t *extent2) {\n\tassert(extent1 != NULL && extent2 != NULL);\n\tmutex_pool_unlock2(tsdn, &extent_mutex_pool, (uintptr_t)extent1,\n\t    (uintptr_t)extent2);\n}\n\nstatic inline unsigned\nextent_arena_ind_get(const extent_t *extent) {\n\tunsigned arena_ind = (unsigned)((extent->e_bits &\n\t    EXTENT_BITS_ARENA_MASK) >> EXTENT_BITS_ARENA_SHIFT);\n\tassert(arena_ind < MALLOCX_ARENA_LIMIT);\n\n\treturn arena_ind;\n}\n\nstatic inline arena_t *\nextent_arena_get(const extent_t *extent) {\n\tunsigned arena_ind = extent_arena_ind_get(extent);\n\n\treturn (arena_t *)atomic_load_p(&arenas[arena_ind], ATOMIC_ACQUIRE);\n}\n\nstatic inline szind_t\nextent_szind_get_maybe_invalid(const extent_t *extent) {\n\tszind_t szind = (szind_t)((extent->e_bits & EXTENT_BITS_SZIND_MASK) >>\n\t    EXTENT_BITS_SZIND_SHIFT);\n\tassert(szind <= SC_NSIZES);\n\treturn szind;\n}\n\nstatic inline szind_t\nextent_szind_get(const extent_t *extent) {\n\tszind_t szind = extent_szind_get_maybe_invalid(extent);\n\tassert(szind < SC_NSIZES); /* Never call when \"invalid\". */\n\treturn szind;\n}\n\nstatic inline size_t\nextent_usize_get(const extent_t *extent) {\n\treturn sz_index2size(extent_szind_get(extent));\n}\n\nstatic inline unsigned\nextent_binshard_get(const extent_t *extent) {\n\tunsigned binshard = (unsigned)((extent->e_bits &\n\t    EXTENT_BITS_BINSHARD_MASK) >> EXTENT_BITS_BINSHARD_SHIFT);\n\tassert(binshard < bin_infos[extent_szind_get(extent)].n_shards);\n\treturn binshard;\n}\n\nstatic inline size_t\nextent_sn_get(const extent_t *extent) {\n\treturn (size_t)((extent->e_bits & EXTENT_BITS_SN_MASK) >>\n\t    EXTENT_BITS_SN_SHIFT);\n}\n\nstatic inline extent_state_t\nextent_state_get(const extent_t *extent) {\n\treturn (extent_state_t)((extent->e_bits & EXTENT_BITS_STATE_MASK) >>\n\t    EXTENT_BITS_STATE_SHIFT);\n}\n\nstatic inline bool\nextent_zeroed_get(const extent_t *extent) {\n\treturn (bool)((extent->e_bits & EXTENT_BITS_ZEROED_MASK) >>\n\t    EXTENT_BITS_ZEROED_SHIFT);\n}\n\nstatic inline bool\nextent_committed_get(const extent_t *extent) {\n\treturn (bool)((extent->e_bits & EXTENT_BITS_COMMITTED_MASK) >>\n\t    EXTENT_BITS_COMMITTED_SHIFT);\n}\n\nstatic inline bool\nextent_dumpable_get(const extent_t *extent) {\n\treturn (bool)((extent->e_bits & EXTENT_BITS_DUMPABLE_MASK) >>\n\t    EXTENT_BITS_DUMPABLE_SHIFT);\n}\n\nstatic inline bool\nextent_slab_get(const extent_t *extent) {\n\treturn (bool)((extent->e_bits & EXTENT_BITS_SLAB_MASK) >>\n\t    EXTENT_BITS_SLAB_SHIFT);\n}\n\nstatic inline unsigned\nextent_nfree_get(const extent_t *extent) {\n\tassert(extent_slab_get(extent));\n\treturn (unsigned)((extent->e_bits & EXTENT_BITS_NFREE_MASK) >>\n\t    EXTENT_BITS_NFREE_SHIFT);\n}\n\nstatic inline void *\nextent_base_get(const extent_t *extent) {\n\tassert(extent->e_addr == PAGE_ADDR2BASE(extent->e_addr) ||\n\t    !extent_slab_get(extent));\n\treturn PAGE_ADDR2BASE(extent->e_addr);\n}\n\nstatic inline void *\nextent_addr_get(const extent_t *extent) {\n\tassert(extent->e_addr == PAGE_ADDR2BASE(extent->e_addr) ||\n\t    !extent_slab_get(extent));\n\treturn extent->e_addr;\n}\n\nstatic inline size_t\nextent_size_get(const extent_t *extent) {\n\treturn (extent->e_size_esn & EXTENT_SIZE_MASK);\n}\n\nstatic inline size_t\nextent_esn_get(const extent_t *extent) {\n\treturn (extent->e_size_esn & EXTENT_ESN_MASK);\n}\n\nstatic inline size_t\nextent_bsize_get(const extent_t *extent) {\n\treturn extent->e_bsize;\n}\n\nstatic inline void *\nextent_before_get(const extent_t *extent) {\n\treturn (void *)((uintptr_t)extent_base_get(extent) - PAGE);\n}\n\nstatic inline void *\nextent_last_get(const extent_t *extent) {\n\treturn (void *)((uintptr_t)extent_base_get(extent) +\n\t    extent_size_get(extent) - PAGE);\n}\n\nstatic inline void *\nextent_past_get(const extent_t *extent) {\n\treturn (void *)((uintptr_t)extent_base_get(extent) +\n\t    extent_size_get(extent));\n}\n\nstatic inline arena_slab_data_t *\nextent_slab_data_get(extent_t *extent) {\n\tassert(extent_slab_get(extent));\n\treturn &extent->e_slab_data;\n}\n\nstatic inline const arena_slab_data_t *\nextent_slab_data_get_const(const extent_t *extent) {\n\tassert(extent_slab_get(extent));\n\treturn &extent->e_slab_data;\n}\n\nstatic inline prof_tctx_t *\nextent_prof_tctx_get(const extent_t *extent) {\n\treturn (prof_tctx_t *)atomic_load_p(&extent->e_prof_tctx,\n\t    ATOMIC_ACQUIRE);\n}\n\nstatic inline nstime_t\nextent_prof_alloc_time_get(const extent_t *extent) {\n\treturn extent->e_alloc_time;\n}\n\nstatic inline void\nextent_arena_set(extent_t *extent, arena_t *arena) {\n\tunsigned arena_ind = (arena != NULL) ? arena_ind_get(arena) : ((1U <<\n\t    MALLOCX_ARENA_BITS) - 1);\n\textent->e_bits = (extent->e_bits & ~EXTENT_BITS_ARENA_MASK) |\n\t    ((uint64_t)arena_ind << EXTENT_BITS_ARENA_SHIFT);\n}\n\nstatic inline void\nextent_binshard_set(extent_t *extent, unsigned binshard) {\n\t/* The assertion assumes szind is set already. */\n\tassert(binshard < bin_infos[extent_szind_get(extent)].n_shards);\n\textent->e_bits = (extent->e_bits & ~EXTENT_BITS_BINSHARD_MASK) |\n\t    ((uint64_t)binshard << EXTENT_BITS_BINSHARD_SHIFT);\n}\n\nstatic inline void\nextent_addr_set(extent_t *extent, void *addr) {\n\textent->e_addr = addr;\n}\n\nstatic inline void\nextent_addr_randomize(tsdn_t *tsdn, extent_t *extent, size_t alignment) {\n\tassert(extent_base_get(extent) == extent_addr_get(extent));\n\n\tif (alignment < PAGE) {\n\t\tunsigned lg_range = LG_PAGE -\n\t\t    lg_floor(CACHELINE_CEILING(alignment));\n\t\tsize_t r;\n\t\tif (!tsdn_null(tsdn)) {\n\t\t\ttsd_t *tsd = tsdn_tsd(tsdn);\n\t\t\tr = (size_t)prng_lg_range_u64(\n\t\t\t    tsd_offset_statep_get(tsd), lg_range);\n\t\t} else {\n\t\t\tr = prng_lg_range_zu(\n\t\t\t    &extent_arena_get(extent)->offset_state,\n\t\t\t    lg_range, true);\n\t\t}\n\t\tuintptr_t random_offset = ((uintptr_t)r) << (LG_PAGE -\n\t\t    lg_range);\n\t\textent->e_addr = (void *)((uintptr_t)extent->e_addr +\n\t\t    random_offset);\n\t\tassert(ALIGNMENT_ADDR2BASE(extent->e_addr, alignment) ==\n\t\t    extent->e_addr);\n\t}\n}\n\nstatic inline void\nextent_size_set(extent_t *extent, size_t size) {\n\tassert((size & ~EXTENT_SIZE_MASK) == 0);\n\textent->e_size_esn = size | (extent->e_size_esn & ~EXTENT_SIZE_MASK);\n}\n\nstatic inline void\nextent_esn_set(extent_t *extent, size_t esn) {\n\textent->e_size_esn = (extent->e_size_esn & ~EXTENT_ESN_MASK) | (esn &\n\t    EXTENT_ESN_MASK);\n}\n\nstatic inline void\nextent_bsize_set(extent_t *extent, size_t bsize) {\n\textent->e_bsize = bsize;\n}\n\nstatic inline void\nextent_szind_set(extent_t *extent, szind_t szind) {\n\tassert(szind <= SC_NSIZES); /* SC_NSIZES means \"invalid\". */\n\textent->e_bits = (extent->e_bits & ~EXTENT_BITS_SZIND_MASK) |\n\t    ((uint64_t)szind << EXTENT_BITS_SZIND_SHIFT);\n}\n\nstatic inline void\nextent_nfree_set(extent_t *extent, unsigned nfree) {\n\tassert(extent_slab_get(extent));\n\textent->e_bits = (extent->e_bits & ~EXTENT_BITS_NFREE_MASK) |\n\t    ((uint64_t)nfree << EXTENT_BITS_NFREE_SHIFT);\n}\n\nstatic inline void\nextent_nfree_binshard_set(extent_t *extent, unsigned nfree, unsigned binshard) {\n\t/* The assertion assumes szind is set already. */\n\tassert(binshard < bin_infos[extent_szind_get(extent)].n_shards);\n\textent->e_bits = (extent->e_bits &\n\t    (~EXTENT_BITS_NFREE_MASK & ~EXTENT_BITS_BINSHARD_MASK)) |\n\t    ((uint64_t)binshard << EXTENT_BITS_BINSHARD_SHIFT) |\n\t    ((uint64_t)nfree << EXTENT_BITS_NFREE_SHIFT);\n}\n\nstatic inline void\nextent_nfree_inc(extent_t *extent) {\n\tassert(extent_slab_get(extent));\n\textent->e_bits += ((uint64_t)1U << EXTENT_BITS_NFREE_SHIFT);\n}\n\nstatic inline void\nextent_nfree_dec(extent_t *extent) {\n\tassert(extent_slab_get(extent));\n\textent->e_bits -= ((uint64_t)1U << EXTENT_BITS_NFREE_SHIFT);\n}\n\nstatic inline void\nextent_nfree_sub(extent_t *extent, uint64_t n) {\n\tassert(extent_slab_get(extent));\n\textent->e_bits -= (n << EXTENT_BITS_NFREE_SHIFT);\n}\n\nstatic inline void\nextent_sn_set(extent_t *extent, size_t sn) {\n\textent->e_bits = (extent->e_bits & ~EXTENT_BITS_SN_MASK) |\n\t    ((uint64_t)sn << EXTENT_BITS_SN_SHIFT);\n}\n\nstatic inline void\nextent_state_set(extent_t *extent, extent_state_t state) {\n\textent->e_bits = (extent->e_bits & ~EXTENT_BITS_STATE_MASK) |\n\t    ((uint64_t)state << EXTENT_BITS_STATE_SHIFT);\n}\n\nstatic inline void\nextent_zeroed_set(extent_t *extent, bool zeroed) {\n\textent->e_bits = (extent->e_bits & ~EXTENT_BITS_ZEROED_MASK) |\n\t    ((uint64_t)zeroed << EXTENT_BITS_ZEROED_SHIFT);\n}\n\nstatic inline void\nextent_committed_set(extent_t *extent, bool committed) {\n\textent->e_bits = (extent->e_bits & ~EXTENT_BITS_COMMITTED_MASK) |\n\t    ((uint64_t)committed << EXTENT_BITS_COMMITTED_SHIFT);\n}\n\nstatic inline void\nextent_dumpable_set(extent_t *extent, bool dumpable) {\n\textent->e_bits = (extent->e_bits & ~EXTENT_BITS_DUMPABLE_MASK) |\n\t    ((uint64_t)dumpable << EXTENT_BITS_DUMPABLE_SHIFT);\n}\n\nstatic inline void\nextent_slab_set(extent_t *extent, bool slab) {\n\textent->e_bits = (extent->e_bits & ~EXTENT_BITS_SLAB_MASK) |\n\t    ((uint64_t)slab << EXTENT_BITS_SLAB_SHIFT);\n}\n\nstatic inline void\nextent_prof_tctx_set(extent_t *extent, prof_tctx_t *tctx) {\n\tatomic_store_p(&extent->e_prof_tctx, tctx, ATOMIC_RELEASE);\n}\n\nstatic inline void\nextent_prof_alloc_time_set(extent_t *extent, nstime_t t) {\n\tnstime_copy(&extent->e_alloc_time, &t);\n}\n\nstatic inline bool\nextent_is_head_get(extent_t *extent) {\n\tif (maps_coalesce) {\n\t\tnot_reached();\n\t}\n\n\treturn (bool)((extent->e_bits & EXTENT_BITS_IS_HEAD_MASK) >>\n\t    EXTENT_BITS_IS_HEAD_SHIFT);\n}\n\nstatic inline void\nextent_is_head_set(extent_t *extent, bool is_head) {\n\tif (maps_coalesce) {\n\t\tnot_reached();\n\t}\n\n\textent->e_bits = (extent->e_bits & ~EXTENT_BITS_IS_HEAD_MASK) |\n\t    ((uint64_t)is_head << EXTENT_BITS_IS_HEAD_SHIFT);\n}\n\nstatic inline void\nextent_init(extent_t *extent, arena_t *arena, void *addr, size_t size,\n    bool slab, szind_t szind, size_t sn, extent_state_t state, bool zeroed,\n    bool committed, bool dumpable, extent_head_state_t is_head) {\n\tassert(addr == PAGE_ADDR2BASE(addr) || !slab);\n\n\textent_arena_set(extent, arena);\n\textent_addr_set(extent, addr);\n\textent_size_set(extent, size);\n\textent_slab_set(extent, slab);\n\textent_szind_set(extent, szind);\n\textent_sn_set(extent, sn);\n\textent_state_set(extent, state);\n\textent_zeroed_set(extent, zeroed);\n\textent_committed_set(extent, committed);\n\textent_dumpable_set(extent, dumpable);\n\tql_elm_new(extent, ql_link);\n\tif (!maps_coalesce) {\n\t\textent_is_head_set(extent, (is_head == EXTENT_IS_HEAD) ? true :\n\t\t    false);\n\t}\n\tif (config_prof) {\n\t\textent_prof_tctx_set(extent, NULL);\n\t}\n}\n\nstatic inline void\nextent_binit(extent_t *extent, void *addr, size_t bsize, size_t sn) {\n\textent_arena_set(extent, NULL);\n\textent_addr_set(extent, addr);\n\textent_bsize_set(extent, bsize);\n\textent_slab_set(extent, false);\n\textent_szind_set(extent, SC_NSIZES);\n\textent_sn_set(extent, sn);\n\textent_state_set(extent, extent_state_active);\n\textent_zeroed_set(extent, true);\n\textent_committed_set(extent, true);\n\textent_dumpable_set(extent, true);\n}\n\nstatic inline void\nextent_list_init(extent_list_t *list) {\n\tql_new(list);\n}\n\nstatic inline extent_t *\nextent_list_first(const extent_list_t *list) {\n\treturn ql_first(list);\n}\n\nstatic inline extent_t *\nextent_list_last(const extent_list_t *list) {\n\treturn ql_last(list, ql_link);\n}\n\nstatic inline void\nextent_list_append(extent_list_t *list, extent_t *extent) {\n\tql_tail_insert(list, extent, ql_link);\n}\n\nstatic inline void\nextent_list_prepend(extent_list_t *list, extent_t *extent) {\n\tql_head_insert(list, extent, ql_link);\n}\n\nstatic inline void\nextent_list_replace(extent_list_t *list, extent_t *to_remove,\n    extent_t *to_insert) {\n\tql_after_insert(to_remove, to_insert, ql_link);\n\tql_remove(list, to_remove, ql_link);\n}\n\nstatic inline void\nextent_list_remove(extent_list_t *list, extent_t *extent) {\n\tql_remove(list, extent, ql_link);\n}\n\nstatic inline int\nextent_sn_comp(const extent_t *a, const extent_t *b) {\n\tsize_t a_sn = extent_sn_get(a);\n\tsize_t b_sn = extent_sn_get(b);\n\n\treturn (a_sn > b_sn) - (a_sn < b_sn);\n}\n\nstatic inline int\nextent_esn_comp(const extent_t *a, const extent_t *b) {\n\tsize_t a_esn = extent_esn_get(a);\n\tsize_t b_esn = extent_esn_get(b);\n\n\treturn (a_esn > b_esn) - (a_esn < b_esn);\n}\n\nstatic inline int\nextent_ad_comp(const extent_t *a, const extent_t *b) {\n\tuintptr_t a_addr = (uintptr_t)extent_addr_get(a);\n\tuintptr_t b_addr = (uintptr_t)extent_addr_get(b);\n\n\treturn (a_addr > b_addr) - (a_addr < b_addr);\n}\n\nstatic inline int\nextent_ead_comp(const extent_t *a, const extent_t *b) {\n\tuintptr_t a_eaddr = (uintptr_t)a;\n\tuintptr_t b_eaddr = (uintptr_t)b;\n\n\treturn (a_eaddr > b_eaddr) - (a_eaddr < b_eaddr);\n}\n\nstatic inline int\nextent_snad_comp(const extent_t *a, const extent_t *b) {\n\tint ret;\n\n\tret = extent_sn_comp(a, b);\n\tif (ret != 0) {\n\t\treturn ret;\n\t}\n\n\tret = extent_ad_comp(a, b);\n\treturn ret;\n}\n\nstatic inline int\nextent_esnead_comp(const extent_t *a, const extent_t *b) {\n\tint ret;\n\n\tret = extent_esn_comp(a, b);\n\tif (ret != 0) {\n\t\treturn ret;\n\t}\n\n\tret = extent_ead_comp(a, b);\n\treturn ret;\n}\n\n#endif /* JEMALLOC_INTERNAL_EXTENT_INLINES_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/extent_mmap.h",
    "content": "#ifndef JEMALLOC_INTERNAL_EXTENT_MMAP_EXTERNS_H\n#define JEMALLOC_INTERNAL_EXTENT_MMAP_EXTERNS_H\n\nextern bool opt_retain;\n\nvoid *extent_alloc_mmap(void *new_addr, size_t size, size_t alignment,\n    bool *zero, bool *commit);\nbool extent_dalloc_mmap(void *addr, size_t size);\n\n#endif /* JEMALLOC_INTERNAL_EXTENT_MMAP_EXTERNS_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/extent_structs.h",
    "content": "#ifndef JEMALLOC_INTERNAL_EXTENT_STRUCTS_H\n#define JEMALLOC_INTERNAL_EXTENT_STRUCTS_H\n\n#include \"jemalloc/internal/atomic.h\"\n#include \"jemalloc/internal/bit_util.h\"\n#include \"jemalloc/internal/bitmap.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/ql.h\"\n#include \"jemalloc/internal/ph.h\"\n#include \"jemalloc/internal/sc.h\"\n\ntypedef enum {\n\textent_state_active   = 0,\n\textent_state_dirty    = 1,\n\textent_state_muzzy    = 2,\n\textent_state_retained = 3\n} extent_state_t;\n\n/* Extent (span of pages).  Use accessor functions for e_* fields. */\nstruct extent_s {\n\t/*\n\t * Bitfield containing several fields:\n\t *\n\t * a: arena_ind\n\t * b: slab\n\t * c: committed\n\t * d: dumpable\n\t * z: zeroed\n\t * t: state\n\t * i: szind\n\t * f: nfree\n\t * s: bin_shard\n\t * n: sn\n\t *\n\t * nnnnnnnn ... nnnnnnss ssssffff ffffffii iiiiiitt zdcbaaaa aaaaaaaa\n\t *\n\t * arena_ind: Arena from which this extent came, or all 1 bits if\n\t *            unassociated.\n\t *\n\t * slab: The slab flag indicates whether the extent is used for a slab\n\t *       of small regions.  This helps differentiate small size classes,\n\t *       and it indicates whether interior pointers can be looked up via\n\t *       iealloc().\n\t *\n\t * committed: The committed flag indicates whether physical memory is\n\t *            committed to the extent, whether explicitly or implicitly\n\t *            as on a system that overcommits and satisfies physical\n\t *            memory needs on demand via soft page faults.\n\t *\n\t * dumpable: The dumpable flag indicates whether or not we've set the\n\t *           memory in question to be dumpable.  Note that this\n\t *           interacts somewhat subtly with user-specified extent hooks,\n\t *           since we don't know if *they* are fiddling with\n\t *           dumpability (in which case, we don't want to undo whatever\n\t *           they're doing).  To deal with this scenario, we:\n\t *             - Make dumpable false only for memory allocated with the\n\t *               default hooks.\n\t *             - Only allow memory to go from non-dumpable to dumpable,\n\t *               and only once.\n\t *             - Never make the OS call to allow dumping when the\n\t *               dumpable bit is already set.\n\t *           These three constraints mean that we will never\n\t *           accidentally dump user memory that the user meant to set\n\t *           nondumpable with their extent hooks.\n\t *\n\t *\n\t * zeroed: The zeroed flag is used by extent recycling code to track\n\t *         whether memory is zero-filled.\n\t *\n\t * state: The state flag is an extent_state_t.\n\t *\n\t * szind: The szind flag indicates usable size class index for\n\t *        allocations residing in this extent, regardless of whether the\n\t *        extent is a slab.  Extent size and usable size often differ\n\t *        even for non-slabs, either due to sz_large_pad or promotion of\n\t *        sampled small regions.\n\t *\n\t * nfree: Number of free regions in slab.\n\t *\n\t * bin_shard: the shard of the bin from which this extent came.\n\t *\n\t * sn: Serial number (potentially non-unique).\n\t *\n\t *     Serial numbers may wrap around if !opt_retain, but as long as\n\t *     comparison functions fall back on address comparison for equal\n\t *     serial numbers, stable (if imperfect) ordering is maintained.\n\t *\n\t *     Serial numbers may not be unique even in the absence of\n\t *     wrap-around, e.g. when splitting an extent and assigning the same\n\t *     serial number to both resulting adjacent extents.\n\t */\n\tuint64_t\t\te_bits;\n#define MASK(CURRENT_FIELD_WIDTH, CURRENT_FIELD_SHIFT) ((((((uint64_t)0x1U) << (CURRENT_FIELD_WIDTH)) - 1)) << (CURRENT_FIELD_SHIFT))\n\n#define EXTENT_BITS_ARENA_WIDTH  MALLOCX_ARENA_BITS\n#define EXTENT_BITS_ARENA_SHIFT  0\n#define EXTENT_BITS_ARENA_MASK  MASK(EXTENT_BITS_ARENA_WIDTH, EXTENT_BITS_ARENA_SHIFT)\n\n#define EXTENT_BITS_SLAB_WIDTH  1\n#define EXTENT_BITS_SLAB_SHIFT  (EXTENT_BITS_ARENA_WIDTH + EXTENT_BITS_ARENA_SHIFT)\n#define EXTENT_BITS_SLAB_MASK  MASK(EXTENT_BITS_SLAB_WIDTH, EXTENT_BITS_SLAB_SHIFT)\n\n#define EXTENT_BITS_COMMITTED_WIDTH  1\n#define EXTENT_BITS_COMMITTED_SHIFT  (EXTENT_BITS_SLAB_WIDTH + EXTENT_BITS_SLAB_SHIFT)\n#define EXTENT_BITS_COMMITTED_MASK  MASK(EXTENT_BITS_COMMITTED_WIDTH, EXTENT_BITS_COMMITTED_SHIFT)\n\n#define EXTENT_BITS_DUMPABLE_WIDTH  1\n#define EXTENT_BITS_DUMPABLE_SHIFT  (EXTENT_BITS_COMMITTED_WIDTH + EXTENT_BITS_COMMITTED_SHIFT)\n#define EXTENT_BITS_DUMPABLE_MASK  MASK(EXTENT_BITS_DUMPABLE_WIDTH, EXTENT_BITS_DUMPABLE_SHIFT)\n\n#define EXTENT_BITS_ZEROED_WIDTH  1\n#define EXTENT_BITS_ZEROED_SHIFT  (EXTENT_BITS_DUMPABLE_WIDTH + EXTENT_BITS_DUMPABLE_SHIFT)\n#define EXTENT_BITS_ZEROED_MASK  MASK(EXTENT_BITS_ZEROED_WIDTH, EXTENT_BITS_ZEROED_SHIFT)\n\n#define EXTENT_BITS_STATE_WIDTH  2\n#define EXTENT_BITS_STATE_SHIFT  (EXTENT_BITS_ZEROED_WIDTH + EXTENT_BITS_ZEROED_SHIFT)\n#define EXTENT_BITS_STATE_MASK  MASK(EXTENT_BITS_STATE_WIDTH, EXTENT_BITS_STATE_SHIFT)\n\n#define EXTENT_BITS_SZIND_WIDTH  LG_CEIL(SC_NSIZES)\n#define EXTENT_BITS_SZIND_SHIFT  (EXTENT_BITS_STATE_WIDTH + EXTENT_BITS_STATE_SHIFT)\n#define EXTENT_BITS_SZIND_MASK  MASK(EXTENT_BITS_SZIND_WIDTH, EXTENT_BITS_SZIND_SHIFT)\n\n#define EXTENT_BITS_NFREE_WIDTH  (LG_SLAB_MAXREGS + 1)\n#define EXTENT_BITS_NFREE_SHIFT  (EXTENT_BITS_SZIND_WIDTH + EXTENT_BITS_SZIND_SHIFT)\n#define EXTENT_BITS_NFREE_MASK  MASK(EXTENT_BITS_NFREE_WIDTH, EXTENT_BITS_NFREE_SHIFT)\n\n#define EXTENT_BITS_BINSHARD_WIDTH  6\n#define EXTENT_BITS_BINSHARD_SHIFT  (EXTENT_BITS_NFREE_WIDTH + EXTENT_BITS_NFREE_SHIFT)\n#define EXTENT_BITS_BINSHARD_MASK  MASK(EXTENT_BITS_BINSHARD_WIDTH, EXTENT_BITS_BINSHARD_SHIFT)\n\n#define EXTENT_BITS_IS_HEAD_WIDTH 1\n#define EXTENT_BITS_IS_HEAD_SHIFT  (EXTENT_BITS_BINSHARD_WIDTH + EXTENT_BITS_BINSHARD_SHIFT)\n#define EXTENT_BITS_IS_HEAD_MASK  MASK(EXTENT_BITS_IS_HEAD_WIDTH, EXTENT_BITS_IS_HEAD_SHIFT)\n\n#define EXTENT_BITS_SN_SHIFT   (EXTENT_BITS_IS_HEAD_WIDTH + EXTENT_BITS_IS_HEAD_SHIFT)\n#define EXTENT_BITS_SN_MASK  (UINT64_MAX << EXTENT_BITS_SN_SHIFT)\n\n\t/* Pointer to the extent that this structure is responsible for. */\n\tvoid\t\t\t*e_addr;\n\n\tunion {\n\t\t/*\n\t\t * Extent size and serial number associated with the extent\n\t\t * structure (different than the serial number for the extent at\n\t\t * e_addr).\n\t\t *\n\t\t * ssssssss [...] ssssssss ssssnnnn nnnnnnnn\n\t\t */\n\t\tsize_t\t\t\te_size_esn;\n\t#define EXTENT_SIZE_MASK\t((size_t)~(PAGE-1))\n\t#define EXTENT_ESN_MASK\t\t((size_t)PAGE-1)\n\t\t/* Base extent size, which may not be a multiple of PAGE. */\n\t\tsize_t\t\t\te_bsize;\n\t};\n\n\t/*\n\t * List linkage, used by a variety of lists:\n\t * - bin_t's slabs_full\n\t * - extents_t's LRU\n\t * - stashed dirty extents\n\t * - arena's large allocations\n\t */\n\tql_elm(extent_t)\tql_link;\n\n\t/*\n\t * Linkage for per size class sn/address-ordered heaps, and\n\t * for extent_avail\n\t */\n\tphn(extent_t)\t\tph_link;\n\n\tunion {\n\t\t/* Small region slab metadata. */\n\t\tarena_slab_data_t\te_slab_data;\n\n\t\t/* Profiling data, used for large objects. */\n\t\tstruct {\n\t\t\t/* Time when this was allocated. */\n\t\t\tnstime_t\t\te_alloc_time;\n\t\t\t/* Points to a prof_tctx_t. */\n\t\t\tatomic_p_t\t\te_prof_tctx;\n\t\t};\n\t};\n};\ntypedef ql_head(extent_t) extent_list_t;\ntypedef ph(extent_t) extent_tree_t;\ntypedef ph(extent_t) extent_heap_t;\n\n/* Quantized collection of extents, with built-in LRU queue. */\nstruct extents_s {\n\tmalloc_mutex_t\t\tmtx;\n\n\t/*\n\t * Quantized per size class heaps of extents.\n\t *\n\t * Synchronization: mtx.\n\t */\n\textent_heap_t\t\theaps[SC_NPSIZES + 1];\n\tatomic_zu_t\t\tnextents[SC_NPSIZES + 1];\n\tatomic_zu_t\t\tnbytes[SC_NPSIZES + 1];\n\n\t/*\n\t * Bitmap for which set bits correspond to non-empty heaps.\n\t *\n\t * Synchronization: mtx.\n\t */\n\tbitmap_t\t\tbitmap[BITMAP_GROUPS(SC_NPSIZES + 1)];\n\n\t/*\n\t * LRU of all extents in heaps.\n\t *\n\t * Synchronization: mtx.\n\t */\n\textent_list_t\t\tlru;\n\n\t/*\n\t * Page sum for all extents in heaps.\n\t *\n\t * The synchronization here is a little tricky.  Modifications to npages\n\t * must hold mtx, but reads need not (though, a reader who sees npages\n\t * without holding the mutex can't assume anything about the rest of the\n\t * state of the extents_t).\n\t */\n\tatomic_zu_t\t\tnpages;\n\n\t/* All stored extents must be in the same state. */\n\textent_state_t\t\tstate;\n\n\t/*\n\t * If true, delay coalescing until eviction; otherwise coalesce during\n\t * deallocation.\n\t */\n\tbool\t\t\tdelay_coalesce;\n};\n\n/*\n * The following two structs are for experimental purposes. See\n * experimental_utilization_query_ctl and\n * experimental_utilization_batch_query_ctl in src/ctl.c.\n */\n\nstruct extent_util_stats_s {\n\tsize_t nfree;\n\tsize_t nregs;\n\tsize_t size;\n};\n\nstruct extent_util_stats_verbose_s {\n\tvoid *slabcur_addr;\n\tsize_t nfree;\n\tsize_t nregs;\n\tsize_t size;\n\tsize_t bin_nfree;\n\tsize_t bin_nregs;\n};\n\n#endif /* JEMALLOC_INTERNAL_EXTENT_STRUCTS_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/extent_types.h",
    "content": "#ifndef JEMALLOC_INTERNAL_EXTENT_TYPES_H\n#define JEMALLOC_INTERNAL_EXTENT_TYPES_H\n\ntypedef struct extent_s extent_t;\ntypedef struct extents_s extents_t;\n\ntypedef struct extent_util_stats_s extent_util_stats_t;\ntypedef struct extent_util_stats_verbose_s extent_util_stats_verbose_t;\n\n#define EXTENT_HOOKS_INITIALIZER\tNULL\n\n/*\n * When reuse (and split) an active extent, (1U << opt_lg_extent_max_active_fit)\n * is the max ratio between the size of the active extent and the new extent.\n */\n#define LG_EXTENT_MAX_ACTIVE_FIT_DEFAULT 6\n\ntypedef enum {\n\tEXTENT_NOT_HEAD,\n\tEXTENT_IS_HEAD   /* Only relevant for Windows && opt.retain. */\n} extent_head_state_t;\n\n#endif /* JEMALLOC_INTERNAL_EXTENT_TYPES_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/hash.h",
    "content": "#ifndef JEMALLOC_INTERNAL_HASH_H\n#define JEMALLOC_INTERNAL_HASH_H\n\n#include \"jemalloc/internal/assert.h\"\n\n/*\n * The following hash function is based on MurmurHash3, placed into the public\n * domain by Austin Appleby.  See https://github.com/aappleby/smhasher for\n * details.\n */\n\n/******************************************************************************/\n/* Internal implementation. */\nstatic inline uint32_t\nhash_rotl_32(uint32_t x, int8_t r) {\n\treturn ((x << r) | (x >> (32 - r)));\n}\n\nstatic inline uint64_t\nhash_rotl_64(uint64_t x, int8_t r) {\n\treturn ((x << r) | (x >> (64 - r)));\n}\n\nstatic inline uint32_t\nhash_get_block_32(const uint32_t *p, int i) {\n\t/* Handle unaligned read. */\n\tif (unlikely((uintptr_t)p & (sizeof(uint32_t)-1)) != 0) {\n\t\tuint32_t ret;\n\n\t\tmemcpy(&ret, (uint8_t *)(p + i), sizeof(uint32_t));\n\t\treturn ret;\n\t}\n\n\treturn p[i];\n}\n\nstatic inline uint64_t\nhash_get_block_64(const uint64_t *p, int i) {\n\t/* Handle unaligned read. */\n\tif (unlikely((uintptr_t)p & (sizeof(uint64_t)-1)) != 0) {\n\t\tuint64_t ret;\n\n\t\tmemcpy(&ret, (uint8_t *)(p + i), sizeof(uint64_t));\n\t\treturn ret;\n\t}\n\n\treturn p[i];\n}\n\nstatic inline uint32_t\nhash_fmix_32(uint32_t h) {\n\th ^= h >> 16;\n\th *= 0x85ebca6b;\n\th ^= h >> 13;\n\th *= 0xc2b2ae35;\n\th ^= h >> 16;\n\n\treturn h;\n}\n\nstatic inline uint64_t\nhash_fmix_64(uint64_t k) {\n\tk ^= k >> 33;\n\tk *= KQU(0xff51afd7ed558ccd);\n\tk ^= k >> 33;\n\tk *= KQU(0xc4ceb9fe1a85ec53);\n\tk ^= k >> 33;\n\n\treturn k;\n}\n\nstatic inline uint32_t\nhash_x86_32(const void *key, int len, uint32_t seed) {\n\tconst uint8_t *data = (const uint8_t *) key;\n\tconst int nblocks = len / 4;\n\n\tuint32_t h1 = seed;\n\n\tconst uint32_t c1 = 0xcc9e2d51;\n\tconst uint32_t c2 = 0x1b873593;\n\n\t/* body */\n\t{\n\t\tconst uint32_t *blocks = (const uint32_t *) (data + nblocks*4);\n\t\tint i;\n\n\t\tfor (i = -nblocks; i; i++) {\n\t\t\tuint32_t k1 = hash_get_block_32(blocks, i);\n\n\t\t\tk1 *= c1;\n\t\t\tk1 = hash_rotl_32(k1, 15);\n\t\t\tk1 *= c2;\n\n\t\t\th1 ^= k1;\n\t\t\th1 = hash_rotl_32(h1, 13);\n\t\t\th1 = h1*5 + 0xe6546b64;\n\t\t}\n\t}\n\n\t/* tail */\n\t{\n\t\tconst uint8_t *tail = (const uint8_t *) (data + nblocks*4);\n\n\t\tuint32_t k1 = 0;\n\n\t\tswitch (len & 3) {\n\t\tcase 3: k1 ^= tail[2] << 16; JEMALLOC_FALLTHROUGH\n\t\tcase 2: k1 ^= tail[1] << 8; JEMALLOC_FALLTHROUGH\n\t\tcase 1: k1 ^= tail[0]; k1 *= c1; k1 = hash_rotl_32(k1, 15);\n\t\t\tk1 *= c2; h1 ^= k1;\n\t\t}\n\t}\n\n\t/* finalization */\n\th1 ^= len;\n\n\th1 = hash_fmix_32(h1);\n\n\treturn h1;\n}\n\nstatic inline void\nhash_x86_128(const void *key, const int len, uint32_t seed,\n    uint64_t r_out[2]) {\n\tconst uint8_t * data = (const uint8_t *) key;\n\tconst int nblocks = len / 16;\n\n\tuint32_t h1 = seed;\n\tuint32_t h2 = seed;\n\tuint32_t h3 = seed;\n\tuint32_t h4 = seed;\n\n\tconst uint32_t c1 = 0x239b961b;\n\tconst uint32_t c2 = 0xab0e9789;\n\tconst uint32_t c3 = 0x38b34ae5;\n\tconst uint32_t c4 = 0xa1e38b93;\n\n\t/* body */\n\t{\n\t\tconst uint32_t *blocks = (const uint32_t *) (data + nblocks*16);\n\t\tint i;\n\n\t\tfor (i = -nblocks; i; i++) {\n\t\t\tuint32_t k1 = hash_get_block_32(blocks, i*4 + 0);\n\t\t\tuint32_t k2 = hash_get_block_32(blocks, i*4 + 1);\n\t\t\tuint32_t k3 = hash_get_block_32(blocks, i*4 + 2);\n\t\t\tuint32_t k4 = hash_get_block_32(blocks, i*4 + 3);\n\n\t\t\tk1 *= c1; k1 = hash_rotl_32(k1, 15); k1 *= c2; h1 ^= k1;\n\n\t\t\th1 = hash_rotl_32(h1, 19); h1 += h2;\n\t\t\th1 = h1*5 + 0x561ccd1b;\n\n\t\t\tk2 *= c2; k2 = hash_rotl_32(k2, 16); k2 *= c3; h2 ^= k2;\n\n\t\t\th2 = hash_rotl_32(h2, 17); h2 += h3;\n\t\t\th2 = h2*5 + 0x0bcaa747;\n\n\t\t\tk3 *= c3; k3 = hash_rotl_32(k3, 17); k3 *= c4; h3 ^= k3;\n\n\t\t\th3 = hash_rotl_32(h3, 15); h3 += h4;\n\t\t\th3 = h3*5 + 0x96cd1c35;\n\n\t\t\tk4 *= c4; k4 = hash_rotl_32(k4, 18); k4 *= c1; h4 ^= k4;\n\n\t\t\th4 = hash_rotl_32(h4, 13); h4 += h1;\n\t\t\th4 = h4*5 + 0x32ac3b17;\n\t\t}\n\t}\n\n\t/* tail */\n\t{\n\t\tconst uint8_t *tail = (const uint8_t *) (data + nblocks*16);\n\t\tuint32_t k1 = 0;\n\t\tuint32_t k2 = 0;\n\t\tuint32_t k3 = 0;\n\t\tuint32_t k4 = 0;\n\n\t\tswitch (len & 15) {\n\t\tcase 15: k4 ^= tail[14] << 16; JEMALLOC_FALLTHROUGH\n\t\tcase 14: k4 ^= tail[13] << 8; JEMALLOC_FALLTHROUGH\n\t\tcase 13: k4 ^= tail[12] << 0;\n\t\t\tk4 *= c4; k4 = hash_rotl_32(k4, 18); k4 *= c1; h4 ^= k4;\n      JEMALLOC_FALLTHROUGH\n\t\tcase 12: k3 ^= tail[11] << 24; JEMALLOC_FALLTHROUGH\n\t\tcase 11: k3 ^= tail[10] << 16; JEMALLOC_FALLTHROUGH\n\t\tcase 10: k3 ^= tail[ 9] << 8; JEMALLOC_FALLTHROUGH\n\t\tcase  9: k3 ^= tail[ 8] << 0;\n\t\t     k3 *= c3; k3 = hash_rotl_32(k3, 17); k3 *= c4; h3 ^= k3;\n         JEMALLOC_FALLTHROUGH\n\t\tcase  8: k2 ^= tail[ 7] << 24; JEMALLOC_FALLTHROUGH\n\t\tcase  7: k2 ^= tail[ 6] << 16; JEMALLOC_FALLTHROUGH\n\t\tcase  6: k2 ^= tail[ 5] << 8; JEMALLOC_FALLTHROUGH\n\t\tcase  5: k2 ^= tail[ 4] << 0;\n\t\t\tk2 *= c2; k2 = hash_rotl_32(k2, 16); k2 *= c3; h2 ^= k2;\n      JEMALLOC_FALLTHROUGH\n\t\tcase  4: k1 ^= tail[ 3] << 24; JEMALLOC_FALLTHROUGH\n\t\tcase  3: k1 ^= tail[ 2] << 16; JEMALLOC_FALLTHROUGH\n\t\tcase  2: k1 ^= tail[ 1] << 8; JEMALLOC_FALLTHROUGH\n\t\tcase  1: k1 ^= tail[ 0] << 0;\n\t\t\tk1 *= c1; k1 = hash_rotl_32(k1, 15); k1 *= c2; h1 ^= k1;\n      JEMALLOC_FALLTHROUGH\n\t\t}\n\t}\n\n\t/* finalization */\n\th1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len;\n\n\th1 += h2; h1 += h3; h1 += h4;\n\th2 += h1; h3 += h1; h4 += h1;\n\n\th1 = hash_fmix_32(h1);\n\th2 = hash_fmix_32(h2);\n\th3 = hash_fmix_32(h3);\n\th4 = hash_fmix_32(h4);\n\n\th1 += h2; h1 += h3; h1 += h4;\n\th2 += h1; h3 += h1; h4 += h1;\n\n\tr_out[0] = (((uint64_t) h2) << 32) | h1;\n\tr_out[1] = (((uint64_t) h4) << 32) | h3;\n}\n\nstatic inline void\nhash_x64_128(const void *key, const int len, const uint32_t seed,\n    uint64_t r_out[2]) {\n\tconst uint8_t *data = (const uint8_t *) key;\n\tconst int nblocks = len / 16;\n\n\tuint64_t h1 = seed;\n\tuint64_t h2 = seed;\n\n\tconst uint64_t c1 = KQU(0x87c37b91114253d5);\n\tconst uint64_t c2 = KQU(0x4cf5ad432745937f);\n\n\t/* body */\n\t{\n\t\tconst uint64_t *blocks = (const uint64_t *) (data);\n\t\tint i;\n\n\t\tfor (i = 0; i < nblocks; i++) {\n\t\t\tuint64_t k1 = hash_get_block_64(blocks, i*2 + 0);\n\t\t\tuint64_t k2 = hash_get_block_64(blocks, i*2 + 1);\n\n\t\t\tk1 *= c1; k1 = hash_rotl_64(k1, 31); k1 *= c2; h1 ^= k1;\n\n\t\t\th1 = hash_rotl_64(h1, 27); h1 += h2;\n\t\t\th1 = h1*5 + 0x52dce729;\n\n\t\t\tk2 *= c2; k2 = hash_rotl_64(k2, 33); k2 *= c1; h2 ^= k2;\n\n\t\t\th2 = hash_rotl_64(h2, 31); h2 += h1;\n\t\t\th2 = h2*5 + 0x38495ab5;\n\t\t}\n\t}\n\n\t/* tail */\n\t{\n\t\tconst uint8_t *tail = (const uint8_t*)(data + nblocks*16);\n\t\tuint64_t k1 = 0;\n\t\tuint64_t k2 = 0;\n\n\t\tswitch (len & 15) {\n\t\tcase 15: k2 ^= ((uint64_t)(tail[14])) << 48; JEMALLOC_FALLTHROUGH\n\t\tcase 14: k2 ^= ((uint64_t)(tail[13])) << 40; JEMALLOC_FALLTHROUGH\n\t\tcase 13: k2 ^= ((uint64_t)(tail[12])) << 32; JEMALLOC_FALLTHROUGH\n\t\tcase 12: k2 ^= ((uint64_t)(tail[11])) << 24; JEMALLOC_FALLTHROUGH\n\t\tcase 11: k2 ^= ((uint64_t)(tail[10])) << 16; JEMALLOC_FALLTHROUGH\n\t\tcase 10: k2 ^= ((uint64_t)(tail[ 9])) << 8;  JEMALLOC_FALLTHROUGH\n\t\tcase  9: k2 ^= ((uint64_t)(tail[ 8])) << 0;\n\t\t\tk2 *= c2; k2 = hash_rotl_64(k2, 33); k2 *= c1; h2 ^= k2;\n\t\t\tJEMALLOC_FALLTHROUGH\n\t\tcase  8: k1 ^= ((uint64_t)(tail[ 7])) << 56; JEMALLOC_FALLTHROUGH\n\t\tcase  7: k1 ^= ((uint64_t)(tail[ 6])) << 48; JEMALLOC_FALLTHROUGH\n\t\tcase  6: k1 ^= ((uint64_t)(tail[ 5])) << 40; JEMALLOC_FALLTHROUGH\n\t\tcase  5: k1 ^= ((uint64_t)(tail[ 4])) << 32; JEMALLOC_FALLTHROUGH\n\t\tcase  4: k1 ^= ((uint64_t)(tail[ 3])) << 24; JEMALLOC_FALLTHROUGH\n\t\tcase  3: k1 ^= ((uint64_t)(tail[ 2])) << 16; JEMALLOC_FALLTHROUGH\n\t\tcase  2: k1 ^= ((uint64_t)(tail[ 1])) << 8;  JEMALLOC_FALLTHROUGH\n\t\tcase  1: k1 ^= ((uint64_t)(tail[ 0])) << 0;\n\t\t\tk1 *= c1; k1 = hash_rotl_64(k1, 31); k1 *= c2; h1 ^= k1;\n\t\t}\n\t}\n\n\t/* finalization */\n\th1 ^= len; h2 ^= len;\n\n\th1 += h2;\n\th2 += h1;\n\n\th1 = hash_fmix_64(h1);\n\th2 = hash_fmix_64(h2);\n\n\th1 += h2;\n\th2 += h1;\n\n\tr_out[0] = h1;\n\tr_out[1] = h2;\n}\n\n/******************************************************************************/\n/* API. */\nstatic inline void\nhash(const void *key, size_t len, const uint32_t seed, size_t r_hash[2]) {\n\tassert(len <= INT_MAX); /* Unfortunate implementation limitation. */\n\n#if (LG_SIZEOF_PTR == 3 && !defined(JEMALLOC_BIG_ENDIAN))\n\thash_x64_128(key, (int)len, seed, (uint64_t *)r_hash);\n#else\n\t{\n\t\tuint64_t hashes[2];\n\t\thash_x86_128(key, (int)len, seed, hashes);\n\t\tr_hash[0] = (size_t)hashes[0];\n\t\tr_hash[1] = (size_t)hashes[1];\n\t}\n#endif\n}\n\n#endif /* JEMALLOC_INTERNAL_HASH_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/hook.h",
    "content": "#ifndef JEMALLOC_INTERNAL_HOOK_H\n#define JEMALLOC_INTERNAL_HOOK_H\n\n#include \"jemalloc/internal/tsd.h\"\n\n/*\n * This API is *extremely* experimental, and may get ripped out, changed in API-\n * and ABI-incompatible ways, be insufficiently or incorrectly documented, etc.\n *\n * It allows hooking the stateful parts of the API to see changes as they\n * happen.\n *\n * Allocation hooks are called after the allocation is done, free hooks are\n * called before the free is done, and expand hooks are called after the\n * allocation is expanded.\n *\n * For realloc and rallocx, if the expansion happens in place, the expansion\n * hook is called.  If it is moved, then the alloc hook is called on the new\n * location, and then the free hook is called on the old location (i.e. both\n * hooks are invoked in between the alloc and the dalloc).\n *\n * If we return NULL from OOM, then usize might not be trustworthy.  Calling\n * realloc(NULL, size) only calls the alloc hook, and calling realloc(ptr, 0)\n * only calls the free hook.  (Calling realloc(NULL, 0) is treated as malloc(0),\n * and only calls the alloc hook).\n *\n * Reentrancy:\n *   Reentrancy is guarded against from within the hook implementation.  If you\n *   call allocator functions from within a hook, the hooks will not be invoked\n *   again.\n * Threading:\n *   The installation of a hook synchronizes with all its uses.  If you can\n *   prove the installation of a hook happens-before a jemalloc entry point,\n *   then the hook will get invoked (unless there's a racing removal).\n *\n *   Hook insertion appears to be atomic at a per-thread level (i.e. if a thread\n *   allocates and has the alloc hook invoked, then a subsequent free on the\n *   same thread will also have the free hook invoked).\n *\n *   The *removal* of a hook does *not* block until all threads are done with\n *   the hook.  Hook authors have to be resilient to this, and need some\n *   out-of-band mechanism for cleaning up any dynamically allocated memory\n *   associated with their hook.\n * Ordering:\n *   Order of hook execution is unspecified, and may be different than insertion\n *   order.\n */\n\n#define HOOK_MAX 4\n\nenum hook_alloc_e {\n\thook_alloc_malloc,\n\thook_alloc_posix_memalign,\n\thook_alloc_aligned_alloc,\n\thook_alloc_calloc,\n\thook_alloc_memalign,\n\thook_alloc_valloc,\n\thook_alloc_mallocx,\n\n\t/* The reallocating functions have both alloc and dalloc variants */\n\thook_alloc_realloc,\n\thook_alloc_rallocx,\n};\n/*\n * We put the enum typedef after the enum, since this file may get included by\n * jemalloc_cpp.cpp, and C++ disallows enum forward declarations.\n */\ntypedef enum hook_alloc_e hook_alloc_t;\n\nenum hook_dalloc_e {\n\thook_dalloc_free,\n\thook_dalloc_dallocx,\n\thook_dalloc_sdallocx,\n\n\t/*\n\t * The dalloc halves of reallocation (not called if in-place expansion\n\t * happens).\n\t */\n\thook_dalloc_realloc,\n\thook_dalloc_rallocx,\n};\ntypedef enum hook_dalloc_e hook_dalloc_t;\n\n\nenum hook_expand_e {\n\thook_expand_realloc,\n\thook_expand_rallocx,\n\thook_expand_xallocx,\n};\ntypedef enum hook_expand_e hook_expand_t;\n\ntypedef void (*hook_alloc)(\n    void *extra, hook_alloc_t type, void *result, uintptr_t result_raw,\n    uintptr_t args_raw[3]);\n\ntypedef void (*hook_dalloc)(\n    void *extra, hook_dalloc_t type, void *address, uintptr_t args_raw[3]);\n\ntypedef void (*hook_expand)(\n    void *extra, hook_expand_t type, void *address, size_t old_usize,\n    size_t new_usize, uintptr_t result_raw, uintptr_t args_raw[4]);\n\ntypedef struct hooks_s hooks_t;\nstruct hooks_s {\n\thook_alloc alloc_hook;\n\thook_dalloc dalloc_hook;\n\thook_expand expand_hook;\n\tvoid *extra;\n};\n\n/*\n * Begin implementation details; everything above this point might one day live\n * in a public API.  Everything below this point never will.\n */\n\n/*\n * The realloc pathways haven't gotten any refactoring love in a while, and it's\n * fairly difficult to pass information from the entry point to the hooks.  We\n * put the informaiton the hooks will need into a struct to encapsulate\n * everything.\n *\n * Much of these pathways are force-inlined, so that the compiler can avoid\n * materializing this struct until we hit an extern arena function.  For fairly\n * goofy reasons, *many* of the realloc paths hit an extern arena function.\n * These paths are cold enough that it doesn't matter; eventually, we should\n * rewrite the realloc code to make the expand-in-place and the\n * free-then-realloc paths more orthogonal, at which point we don't need to\n * spread the hook logic all over the place.\n */\ntypedef struct hook_ralloc_args_s hook_ralloc_args_t;\nstruct hook_ralloc_args_s {\n\t/* I.e. as opposed to rallocx. */\n\tbool is_realloc;\n\t/*\n\t * The expand hook takes 4 arguments, even if only 3 are actually used;\n\t * we add an extra one in case the user decides to memcpy without\n\t * looking too closely at the hooked function.\n\t */\n\tuintptr_t args[4];\n};\n\n/*\n * Returns an opaque handle to be used when removing the hook.  NULL means that\n * we couldn't install the hook.\n */\nbool hook_boot();\n\nvoid *hook_install(tsdn_t *tsdn, hooks_t *hooks);\n/* Uninstalls the hook with the handle previously returned from hook_install. */\nvoid hook_remove(tsdn_t *tsdn, void *opaque);\n\n/* Hooks */\n\nvoid hook_invoke_alloc(hook_alloc_t type, void *result, uintptr_t result_raw,\n    uintptr_t args_raw[3]);\n\nvoid hook_invoke_dalloc(hook_dalloc_t type, void *address,\n    uintptr_t args_raw[3]);\n\nvoid hook_invoke_expand(hook_expand_t type, void *address, size_t old_usize,\n    size_t new_usize, uintptr_t result_raw, uintptr_t args_raw[4]);\n\n#endif /* JEMALLOC_INTERNAL_HOOK_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/jemalloc_internal_decls.h",
    "content": "#ifndef JEMALLOC_INTERNAL_DECLS_H\n#define JEMALLOC_INTERNAL_DECLS_H\n\n#include <math.h>\n#ifdef _WIN32\n#  include <windows.h>\n#  include \"msvc_compat/windows_extra.h\"\n#  ifdef _WIN64\n#    if LG_VADDR <= 32\n#      error Generate the headers using x64 vcargs\n#    endif\n#  else\n#    if LG_VADDR > 32\n#      undef LG_VADDR\n#      define LG_VADDR 32\n#    endif\n#  endif\n#else\n#  include <sys/param.h>\n#  include <sys/mman.h>\n#  if !defined(__pnacl__) && !defined(__native_client__)\n#    include <sys/syscall.h>\n#    if !defined(SYS_write) && defined(__NR_write)\n#      define SYS_write __NR_write\n#    endif\n#    if defined(SYS_open) && defined(__aarch64__)\n       /* Android headers may define SYS_open to __NR_open even though\n        * __NR_open may not exist on AArch64 (superseded by __NR_openat). */\n#      undef SYS_open\n#    endif\n#    include <sys/uio.h>\n#  endif\n#  include <pthread.h>\n#  ifdef __FreeBSD__\n#  include <pthread_np.h>\n#  endif\n#  include <signal.h>\n#  ifdef JEMALLOC_OS_UNFAIR_LOCK\n#    include <os/lock.h>\n#  endif\n#  ifdef JEMALLOC_GLIBC_MALLOC_HOOK\n#    include <sched.h>\n#  endif\n#  include <errno.h>\n#  include <sys/time.h>\n#  include <time.h>\n#  ifdef JEMALLOC_HAVE_MACH_ABSOLUTE_TIME\n#    include <mach/mach_time.h>\n#  endif\n#endif\n#include <sys/types.h>\n\n#include <limits.h>\n#ifndef SIZE_T_MAX\n#  define SIZE_T_MAX\tSIZE_MAX\n#endif\n#ifndef SSIZE_MAX\n#  define SSIZE_MAX\t((ssize_t)(SIZE_T_MAX >> 1))\n#endif\n#include <stdarg.h>\n#include <stdbool.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <stdint.h>\n#include <stddef.h>\n#ifndef offsetof\n#  define offsetof(type, member)\t((size_t)&(((type *)NULL)->member))\n#endif\n#include <string.h>\n#include <strings.h>\n#include <ctype.h>\n#ifdef _MSC_VER\n#  include <io.h>\ntypedef intptr_t ssize_t;\n#  define PATH_MAX 1024\n#  define STDERR_FILENO 2\n#  define __func__ __FUNCTION__\n#  ifdef JEMALLOC_HAS_RESTRICT\n#    define restrict __restrict\n#  endif\n/* Disable warnings about deprecated system functions. */\n#  pragma warning(disable: 4996)\n#if _MSC_VER < 1800\nstatic int\nisblank(int c) {\n\treturn (c == '\\t' || c == ' ');\n}\n#endif\n#else\n#  include <unistd.h>\n#endif\n#include <fcntl.h>\n\n#endif /* JEMALLOC_INTERNAL_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/jemalloc_internal_defs.h.in",
    "content": "#ifndef JEMALLOC_INTERNAL_DEFS_H_\n#define JEMALLOC_INTERNAL_DEFS_H_\n/*\n * If JEMALLOC_PREFIX is defined via --with-jemalloc-prefix, it will cause all\n * public APIs to be prefixed.  This makes it possible, with some care, to use\n * multiple allocators simultaneously.\n */\n#undef JEMALLOC_PREFIX\n#undef JEMALLOC_CPREFIX\n\n/*\n * Define overrides for non-standard allocator-related functions if they are\n * present on the system.\n */\n#undef JEMALLOC_OVERRIDE___LIBC_CALLOC\n#undef JEMALLOC_OVERRIDE___LIBC_FREE\n#undef JEMALLOC_OVERRIDE___LIBC_MALLOC\n#undef JEMALLOC_OVERRIDE___LIBC_MEMALIGN\n#undef JEMALLOC_OVERRIDE___LIBC_REALLOC\n#undef JEMALLOC_OVERRIDE___LIBC_VALLOC\n#undef JEMALLOC_OVERRIDE___POSIX_MEMALIGN\n\n/*\n * JEMALLOC_PRIVATE_NAMESPACE is used as a prefix for all library-private APIs.\n * For shared libraries, symbol visibility mechanisms prevent these symbols\n * from being exported, but for static libraries, naming collisions are a real\n * possibility.\n */\n#undef JEMALLOC_PRIVATE_NAMESPACE\n\n/*\n * Hyper-threaded CPUs may need a special instruction inside spin loops in\n * order to yield to another virtual CPU.\n */\n#undef CPU_SPINWAIT\n/* 1 if CPU_SPINWAIT is defined, 0 otherwise. */\n#undef HAVE_CPU_SPINWAIT\n\n/*\n * Number of significant bits in virtual addresses.  This may be less than the\n * total number of bits in a pointer, e.g. on x64, for which the uppermost 16\n * bits are the same as bit 47.\n */\n#undef LG_VADDR\n\n/* Defined if C11 atomics are available. */\n#undef JEMALLOC_C11_ATOMICS\n\n/* Defined if GCC __atomic atomics are available. */\n#undef JEMALLOC_GCC_ATOMIC_ATOMICS\n/* and the 8-bit variant support. */\n#undef JEMALLOC_GCC_U8_ATOMIC_ATOMICS\n\n/* Defined if GCC __sync atomics are available. */\n#undef JEMALLOC_GCC_SYNC_ATOMICS\n/* and the 8-bit variant support. */\n#undef JEMALLOC_GCC_U8_SYNC_ATOMICS\n\n/*\n * Defined if __builtin_clz() and __builtin_clzl() are available.\n */\n#undef JEMALLOC_HAVE_BUILTIN_CLZ\n\n/*\n * Defined if os_unfair_lock_*() functions are available, as provided by Darwin.\n */\n#undef JEMALLOC_OS_UNFAIR_LOCK\n\n/* Defined if syscall(2) is usable. */\n#undef JEMALLOC_USE_SYSCALL\n\n/*\n * Defined if secure_getenv(3) is available.\n */\n#undef JEMALLOC_HAVE_SECURE_GETENV\n\n/*\n * Defined if issetugid(2) is available.\n */\n#undef JEMALLOC_HAVE_ISSETUGID\n\n/* Defined if pthread_atfork(3) is available. */\n#undef JEMALLOC_HAVE_PTHREAD_ATFORK\n\n/* Defined if pthread_setname_np(3) is available. */\n#undef JEMALLOC_HAVE_PTHREAD_SETNAME_NP\n\n/*\n * Defined if clock_gettime(CLOCK_MONOTONIC_COARSE, ...) is available.\n */\n#undef JEMALLOC_HAVE_CLOCK_MONOTONIC_COARSE\n\n/*\n * Defined if clock_gettime(CLOCK_MONOTONIC, ...) is available.\n */\n#undef JEMALLOC_HAVE_CLOCK_MONOTONIC\n\n/*\n * Defined if mach_absolute_time() is available.\n */\n#undef JEMALLOC_HAVE_MACH_ABSOLUTE_TIME\n\n/*\n * Defined if _malloc_thread_cleanup() exists.  At least in the case of\n * FreeBSD, pthread_key_create() allocates, which if used during malloc\n * bootstrapping will cause recursion into the pthreads library.  Therefore, if\n * _malloc_thread_cleanup() exists, use it as the basis for thread cleanup in\n * malloc_tsd.\n */\n#undef JEMALLOC_MALLOC_THREAD_CLEANUP\n\n/*\n * Defined if threaded initialization is known to be safe on this platform.\n * Among other things, it must be possible to initialize a mutex without\n * triggering allocation in order for threaded allocation to be safe.\n */\n#undef JEMALLOC_THREADED_INIT\n\n/*\n * Defined if the pthreads implementation defines\n * _pthread_mutex_init_calloc_cb(), in which case the function is used in order\n * to avoid recursive allocation during mutex initialization.\n */\n#undef JEMALLOC_MUTEX_INIT_CB\n\n/* Non-empty if the tls_model attribute is supported. */\n#undef JEMALLOC_TLS_MODEL\n\n/*\n * JEMALLOC_DEBUG enables assertions and other sanity checks, and disables\n * inline functions.\n */\n#undef JEMALLOC_DEBUG\n\n/* JEMALLOC_STATS enables statistics calculation. */\n#undef JEMALLOC_STATS\n\n/* JEMALLOC_EXPERIMENTAL_SMALLOCX_API enables experimental smallocx API. */\n#undef JEMALLOC_EXPERIMENTAL_SMALLOCX_API\n\n/* JEMALLOC_PROF enables allocation profiling. */\n#undef JEMALLOC_PROF\n\n/* Use libunwind for profile backtracing if defined. */\n#undef JEMALLOC_PROF_LIBUNWIND\n\n/* Use libgcc for profile backtracing if defined. */\n#undef JEMALLOC_PROF_LIBGCC\n\n/* Use gcc intrinsics for profile backtracing if defined. */\n#undef JEMALLOC_PROF_GCC\n\n/*\n * JEMALLOC_DSS enables use of sbrk(2) to allocate extents from the data storage\n * segment (DSS).\n */\n#undef JEMALLOC_DSS\n\n/* Support memory filling (junk/zero). */\n#undef JEMALLOC_FILL\n\n/* Support utrace(2)-based tracing. */\n#undef JEMALLOC_UTRACE\n\n/* Support optional abort() on OOM. */\n#undef JEMALLOC_XMALLOC\n\n/* Support lazy locking (avoid locking unless a second thread is launched). */\n#undef JEMALLOC_LAZY_LOCK\n\n/*\n * Minimum allocation alignment is 2^LG_QUANTUM bytes (ignoring tiny size\n * classes).\n */\n#undef LG_QUANTUM\n\n/* One page is 2^LG_PAGE bytes. */\n#undef LG_PAGE\n\n/*\n * One huge page is 2^LG_HUGEPAGE bytes.  Note that this is defined even if the\n * system does not explicitly support huge pages; system calls that require\n * explicit huge page support are separately configured.\n */\n#undef LG_HUGEPAGE\n\n/*\n * If defined, adjacent virtual memory mappings with identical attributes\n * automatically coalesce, and they fragment when changes are made to subranges.\n * This is the normal order of things for mmap()/munmap(), but on Windows\n * VirtualAlloc()/VirtualFree() operations must be precisely matched, i.e.\n * mappings do *not* coalesce/fragment.\n */\n#undef JEMALLOC_MAPS_COALESCE\n\n/*\n * If defined, retain memory for later reuse by default rather than using e.g.\n * munmap() to unmap freed extents.  This is enabled on 64-bit Linux because\n * common sequences of mmap()/munmap() calls will cause virtual memory map\n * holes.\n */\n#undef JEMALLOC_RETAIN\n\n/* TLS is used to map arenas and magazine caches to threads. */\n#undef JEMALLOC_TLS\n\n/*\n * Used to mark unreachable code to quiet \"end of non-void\" compiler warnings.\n * Don't use this directly; instead use unreachable() from util.h\n */\n#undef JEMALLOC_INTERNAL_UNREACHABLE\n\n/*\n * ffs*() functions to use for bitmapping.  Don't use these directly; instead,\n * use ffs_*() from util.h.\n */\n#undef JEMALLOC_INTERNAL_FFSLL\n#undef JEMALLOC_INTERNAL_FFSL\n#undef JEMALLOC_INTERNAL_FFS\n\n/*\n * popcount*() functions to use for bitmapping.\n */\n#undef JEMALLOC_INTERNAL_POPCOUNTL\n#undef JEMALLOC_INTERNAL_POPCOUNT\n\n/*\n * If defined, explicitly attempt to more uniformly distribute large allocation\n * pointer alignments across all cache indices.\n */\n#undef JEMALLOC_CACHE_OBLIVIOUS\n\n/*\n * If defined, enable logging facilities.  We make this a configure option to\n * avoid taking extra branches everywhere.\n */\n#undef JEMALLOC_LOG\n\n/*\n * If defined, use readlinkat() (instead of readlink()) to follow\n * /etc/malloc_conf.\n */\n#undef JEMALLOC_READLINKAT\n\n/*\n * Darwin (OS X) uses zones to work around Mach-O symbol override shortcomings.\n */\n#undef JEMALLOC_ZONE\n\n/*\n * Methods for determining whether the OS overcommits.\n * JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY: Linux's\n *                                         /proc/sys/vm.overcommit_memory file.\n * JEMALLOC_SYSCTL_VM_OVERCOMMIT: FreeBSD's vm.overcommit sysctl.\n */\n#undef JEMALLOC_SYSCTL_VM_OVERCOMMIT\n#undef JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY\n\n/* Defined if madvise(2) is available. */\n#undef JEMALLOC_HAVE_MADVISE\n\n/*\n * Defined if transparent huge pages are supported via the MADV_[NO]HUGEPAGE\n * arguments to madvise(2).\n */\n#undef JEMALLOC_HAVE_MADVISE_HUGE\n\n/*\n * Methods for purging unused pages differ between operating systems.\n *\n *   madvise(..., MADV_FREE) : This marks pages as being unused, such that they\n *                             will be discarded rather than swapped out.\n *   madvise(..., MADV_DONTNEED) : If JEMALLOC_PURGE_MADVISE_DONTNEED_ZEROS is\n *                                 defined, this immediately discards pages,\n *                                 such that new pages will be demand-zeroed if\n *                                 the address region is later touched;\n *                                 otherwise this behaves similarly to\n *                                 MADV_FREE, though typically with higher\n *                                 system overhead.\n */\n#undef JEMALLOC_PURGE_MADVISE_FREE\n#undef JEMALLOC_PURGE_MADVISE_DONTNEED\n#undef JEMALLOC_PURGE_MADVISE_DONTNEED_ZEROS\n\n/* Defined if madvise(2) is available but MADV_FREE is not (x86 Linux only). */\n#undef JEMALLOC_DEFINE_MADVISE_FREE\n\n/*\n * Defined if MADV_DO[NT]DUMP is supported as an argument to madvise.\n */\n#undef JEMALLOC_MADVISE_DONTDUMP\n\n/*\n * Defined if transparent huge pages (THPs) are supported via the\n * MADV_[NO]HUGEPAGE arguments to madvise(2), and THP support is enabled.\n */\n#undef JEMALLOC_THP\n\n/* Define if operating system has alloca.h header. */\n#undef JEMALLOC_HAS_ALLOCA_H\n\n/* C99 restrict keyword supported. */\n#undef JEMALLOC_HAS_RESTRICT\n\n/* For use by hash code. */\n#undef JEMALLOC_BIG_ENDIAN\n\n/* sizeof(int) == 2^LG_SIZEOF_INT. */\n#undef LG_SIZEOF_INT\n\n/* sizeof(long) == 2^LG_SIZEOF_LONG. */\n#undef LG_SIZEOF_LONG\n\n/* sizeof(long long) == 2^LG_SIZEOF_LONG_LONG. */\n#undef LG_SIZEOF_LONG_LONG\n\n/* sizeof(intmax_t) == 2^LG_SIZEOF_INTMAX_T. */\n#undef LG_SIZEOF_INTMAX_T\n\n/* glibc malloc hooks (__malloc_hook, __realloc_hook, __free_hook). */\n#undef JEMALLOC_GLIBC_MALLOC_HOOK\n\n/* glibc memalign hook. */\n#undef JEMALLOC_GLIBC_MEMALIGN_HOOK\n\n/* pthread support */\n#undef JEMALLOC_HAVE_PTHREAD\n\n/* dlsym() support */\n#undef JEMALLOC_HAVE_DLSYM\n\n/* Adaptive mutex support in pthreads. */\n#undef JEMALLOC_HAVE_PTHREAD_MUTEX_ADAPTIVE_NP\n\n/* GNU specific sched_getcpu support */\n#undef JEMALLOC_HAVE_SCHED_GETCPU\n\n/* GNU specific sched_setaffinity support */\n#undef JEMALLOC_HAVE_SCHED_SETAFFINITY\n\n/*\n * If defined, all the features necessary for background threads are present.\n */\n#undef JEMALLOC_BACKGROUND_THREAD\n\n/*\n * If defined, jemalloc symbols are not exported (doesn't work when\n * JEMALLOC_PREFIX is not defined).\n */\n#undef JEMALLOC_EXPORT\n\n/* config.malloc_conf options string. */\n#undef JEMALLOC_CONFIG_MALLOC_CONF\n\n/* If defined, jemalloc takes the malloc/free/etc. symbol names. */\n#undef JEMALLOC_IS_MALLOC\n\n/*\n * Defined if strerror_r returns char * if _GNU_SOURCE is defined.\n */\n#undef JEMALLOC_STRERROR_R_RETURNS_CHAR_WITH_GNU_SOURCE\n\n/* Performs additional safety checks when defined. */\n#undef JEMALLOC_OPT_SAFETY_CHECKS\n\n#endif /* JEMALLOC_INTERNAL_DEFS_H_ */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/jemalloc_internal_externs.h",
    "content": "#ifndef JEMALLOC_INTERNAL_EXTERNS_H\n#define JEMALLOC_INTERNAL_EXTERNS_H\n\n#include \"jemalloc/internal/atomic.h\"\n#include \"jemalloc/internal/tsd_types.h\"\n\n/* TSD checks this to set thread local slow state accordingly. */\nextern bool malloc_slow;\n\n/* Run-time options. */\nextern bool opt_abort;\nextern bool opt_abort_conf;\nextern bool opt_confirm_conf;\nextern const char *opt_junk;\nextern bool opt_junk_alloc;\nextern bool opt_junk_free;\nextern bool opt_utrace;\nextern bool opt_xmalloc;\nextern bool opt_zero;\nextern unsigned opt_narenas;\n\n/* Number of CPUs. */\nextern unsigned ncpus;\n\n/* Number of arenas used for automatic multiplexing of threads and arenas. */\nextern unsigned narenas_auto;\n\n/* Base index for manual arenas. */\nextern unsigned manual_arena_base;\n\n/*\n * Arenas that are used to service external requests.  Not all elements of the\n * arenas array are necessarily used; arenas are created lazily as needed.\n */\nextern atomic_p_t arenas[];\n\nvoid *a0malloc(size_t size);\nvoid a0dalloc(void *ptr);\nvoid *bootstrap_malloc(size_t size);\nvoid *bootstrap_calloc(size_t num, size_t size);\nvoid bootstrap_free(void *ptr);\nvoid arena_set(unsigned ind, arena_t *arena);\nunsigned narenas_total_get(void);\narena_t *arena_init(tsdn_t *tsdn, unsigned ind, extent_hooks_t *extent_hooks);\narena_tdata_t *arena_tdata_get_hard(tsd_t *tsd, unsigned ind);\narena_t *arena_choose_hard(tsd_t *tsd, bool internal);\nvoid arena_migrate(tsd_t *tsd, unsigned oldind, unsigned newind);\nvoid iarena_cleanup(tsd_t *tsd);\nvoid arena_cleanup(tsd_t *tsd);\nvoid arenas_tdata_cleanup(tsd_t *tsd);\nvoid jemalloc_prefork(void);\nvoid jemalloc_postfork_parent(void);\nvoid jemalloc_postfork_child(void);\nbool malloc_initialized(void);\nvoid je_sdallocx_noflags(void *ptr, size_t size);\n\n#endif /* JEMALLOC_INTERNAL_EXTERNS_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/jemalloc_internal_includes.h",
    "content": "#ifndef JEMALLOC_INTERNAL_INCLUDES_H\n#define JEMALLOC_INTERNAL_INCLUDES_H\n\n/*\n * jemalloc can conceptually be broken into components (arena, tcache, etc.),\n * but there are circular dependencies that cannot be broken without\n * substantial performance degradation.\n *\n * Historically, we dealt with this by each header into four sections (types,\n * structs, externs, and inlines), and included each header file multiple times\n * in this file, picking out the portion we want on each pass using the\n * following #defines:\n *   JEMALLOC_H_TYPES   : Preprocessor-defined constants and psuedo-opaque data\n *                        types.\n *   JEMALLOC_H_STRUCTS : Data structures.\n *   JEMALLOC_H_EXTERNS : Extern data declarations and function prototypes.\n *   JEMALLOC_H_INLINES : Inline functions.\n *\n * We're moving toward a world in which the dependencies are explicit; each file\n * will #include the headers it depends on (rather than relying on them being\n * implicitly available via this file including every header file in the\n * project).\n *\n * We're now in an intermediate state: we've broken up the header files to avoid\n * having to include each one multiple times, but have not yet moved the\n * dependency information into the header files (i.e. we still rely on the\n * ordering in this file to ensure all a header's dependencies are available in\n * its translation unit).  Each component is now broken up into multiple header\n * files, corresponding to the sections above (e.g. instead of \"foo.h\", we now\n * have \"foo_types.h\", \"foo_structs.h\", \"foo_externs.h\", \"foo_inlines.h\").\n *\n * Those files which have been converted to explicitly include their\n * inter-component dependencies are now in the initial HERMETIC HEADERS\n * section.  All headers may still rely on jemalloc_preamble.h (which, by fiat,\n * must be included first in every translation unit) for system headers and\n * global jemalloc definitions, however.\n */\n\n/******************************************************************************/\n/* TYPES */\n/******************************************************************************/\n\n#include \"jemalloc/internal/extent_types.h\"\n#include \"jemalloc/internal/base_types.h\"\n#include \"jemalloc/internal/arena_types.h\"\n#include \"jemalloc/internal/tcache_types.h\"\n#include \"jemalloc/internal/prof_types.h\"\n\n/******************************************************************************/\n/* STRUCTS */\n/******************************************************************************/\n\n#include \"jemalloc/internal/arena_structs_a.h\"\n#include \"jemalloc/internal/extent_structs.h\"\n#include \"jemalloc/internal/base_structs.h\"\n#include \"jemalloc/internal/prof_structs.h\"\n#include \"jemalloc/internal/arena_structs_b.h\"\n#include \"jemalloc/internal/tcache_structs.h\"\n#include \"jemalloc/internal/background_thread_structs.h\"\n\n/******************************************************************************/\n/* EXTERNS */\n/******************************************************************************/\n\n#include \"jemalloc/internal/jemalloc_internal_externs.h\"\n#include \"jemalloc/internal/extent_externs.h\"\n#include \"jemalloc/internal/base_externs.h\"\n#include \"jemalloc/internal/arena_externs.h\"\n#include \"jemalloc/internal/large_externs.h\"\n#include \"jemalloc/internal/tcache_externs.h\"\n#include \"jemalloc/internal/prof_externs.h\"\n#include \"jemalloc/internal/background_thread_externs.h\"\n\n/******************************************************************************/\n/* INLINES */\n/******************************************************************************/\n\n#include \"jemalloc/internal/jemalloc_internal_inlines_a.h\"\n#include \"jemalloc/internal/base_inlines.h\"\n/*\n * Include portions of arena code interleaved with tcache code in order to\n * resolve circular dependencies.\n */\n#include \"jemalloc/internal/prof_inlines_a.h\"\n#include \"jemalloc/internal/arena_inlines_a.h\"\n#include \"jemalloc/internal/extent_inlines.h\"\n#include \"jemalloc/internal/jemalloc_internal_inlines_b.h\"\n#include \"jemalloc/internal/tcache_inlines.h\"\n#include \"jemalloc/internal/arena_inlines_b.h\"\n#include \"jemalloc/internal/jemalloc_internal_inlines_c.h\"\n#include \"jemalloc/internal/prof_inlines_b.h\"\n#include \"jemalloc/internal/background_thread_inlines.h\"\n\n#endif /* JEMALLOC_INTERNAL_INCLUDES_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/jemalloc_internal_inlines_a.h",
    "content": "#ifndef JEMALLOC_INTERNAL_INLINES_A_H\n#define JEMALLOC_INTERNAL_INLINES_A_H\n\n#include \"jemalloc/internal/atomic.h\"\n#include \"jemalloc/internal/bit_util.h\"\n#include \"jemalloc/internal/jemalloc_internal_types.h\"\n#include \"jemalloc/internal/sc.h\"\n#include \"jemalloc/internal/ticker.h\"\n\nJEMALLOC_ALWAYS_INLINE malloc_cpuid_t\nmalloc_getcpu(void) {\n\tassert(have_percpu_arena);\n#if defined(_WIN32)\n\treturn GetCurrentProcessorNumber();\n#elif defined(JEMALLOC_HAVE_SCHED_GETCPU)\n\treturn (malloc_cpuid_t)sched_getcpu();\n#else\n\tnot_reached();\n\treturn -1;\n#endif\n}\n\n/* Return the chosen arena index based on current cpu. */\nJEMALLOC_ALWAYS_INLINE unsigned\npercpu_arena_choose(void) {\n\tassert(have_percpu_arena && PERCPU_ARENA_ENABLED(opt_percpu_arena));\n\n\tmalloc_cpuid_t cpuid = malloc_getcpu();\n\tassert(cpuid >= 0);\n\n\tunsigned arena_ind;\n\tif ((opt_percpu_arena == percpu_arena) || ((unsigned)cpuid < ncpus /\n\t    2)) {\n\t\tarena_ind = cpuid;\n\t} else {\n\t\tassert(opt_percpu_arena == per_phycpu_arena);\n\t\t/* Hyper threads on the same physical CPU share arena. */\n\t\tarena_ind = cpuid - ncpus / 2;\n\t}\n\n\treturn arena_ind;\n}\n\n/* Return the limit of percpu auto arena range, i.e. arenas[0...ind_limit). */\nJEMALLOC_ALWAYS_INLINE unsigned\npercpu_arena_ind_limit(percpu_arena_mode_t mode) {\n\tassert(have_percpu_arena && PERCPU_ARENA_ENABLED(mode));\n\tif (mode == per_phycpu_arena && ncpus > 1) {\n\t\tif (ncpus % 2) {\n\t\t\t/* This likely means a misconfig. */\n\t\t\treturn ncpus / 2 + 1;\n\t\t}\n\t\treturn ncpus / 2;\n\t} else {\n\t\treturn ncpus;\n\t}\n}\n\nstatic inline arena_tdata_t *\narena_tdata_get(tsd_t *tsd, unsigned ind, bool refresh_if_missing) {\n\tarena_tdata_t *tdata;\n\tarena_tdata_t *arenas_tdata = tsd_arenas_tdata_get(tsd);\n\n\tif (unlikely(arenas_tdata == NULL)) {\n\t\t/* arenas_tdata hasn't been initialized yet. */\n\t\treturn arena_tdata_get_hard(tsd, ind);\n\t}\n\tif (unlikely(ind >= tsd_narenas_tdata_get(tsd))) {\n\t\t/*\n\t\t * ind is invalid, cache is old (too small), or tdata to be\n\t\t * initialized.\n\t\t */\n\t\treturn (refresh_if_missing ? arena_tdata_get_hard(tsd, ind) :\n\t\t    NULL);\n\t}\n\n\ttdata = &arenas_tdata[ind];\n\tif (likely(tdata != NULL) || !refresh_if_missing) {\n\t\treturn tdata;\n\t}\n\treturn arena_tdata_get_hard(tsd, ind);\n}\n\nstatic inline arena_t *\narena_get(tsdn_t *tsdn, unsigned ind, bool init_if_missing) {\n\tarena_t *ret;\n\n\tassert(ind < MALLOCX_ARENA_LIMIT);\n\n\tret = (arena_t *)atomic_load_p(&arenas[ind], ATOMIC_ACQUIRE);\n\tif (unlikely(ret == NULL)) {\n\t\tif (init_if_missing) {\n\t\t\tret = arena_init(tsdn, ind,\n\t\t\t    (extent_hooks_t *)&extent_hooks_default);\n\t\t}\n\t}\n\treturn ret;\n}\n\nstatic inline ticker_t *\ndecay_ticker_get(tsd_t *tsd, unsigned ind) {\n\tarena_tdata_t *tdata;\n\n\ttdata = arena_tdata_get(tsd, ind, true);\n\tif (unlikely(tdata == NULL)) {\n\t\treturn NULL;\n\t}\n\treturn &tdata->decay_ticker;\n}\n\nJEMALLOC_ALWAYS_INLINE cache_bin_t *\ntcache_small_bin_get(tcache_t *tcache, szind_t binind) {\n\tassert(binind < SC_NBINS);\n\treturn &tcache->bins_small[binind];\n}\n\nJEMALLOC_ALWAYS_INLINE cache_bin_t *\ntcache_large_bin_get(tcache_t *tcache, szind_t binind) {\n\tassert(binind >= SC_NBINS &&binind < nhbins);\n\treturn &tcache->bins_large[binind - SC_NBINS];\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntcache_available(tsd_t *tsd) {\n\t/*\n\t * Thread specific auto tcache might be unavailable if: 1) during tcache\n\t * initialization, or 2) disabled through thread.tcache.enabled mallctl\n\t * or config options.  This check covers all cases.\n\t */\n\tif (likely(tsd_tcache_enabled_get(tsd))) {\n\t\t/* Associated arena == NULL implies tcache init in progress. */\n\t\tassert(tsd_tcachep_get(tsd)->arena == NULL ||\n\t\t    tcache_small_bin_get(tsd_tcachep_get(tsd), 0)->avail !=\n\t\t    NULL);\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\nJEMALLOC_ALWAYS_INLINE tcache_t *\ntcache_get(tsd_t *tsd) {\n\tif (!tcache_available(tsd)) {\n\t\treturn NULL;\n\t}\n\n\treturn tsd_tcachep_get(tsd);\n}\n\nstatic inline void\npre_reentrancy(tsd_t *tsd, arena_t *arena) {\n\t/* arena is the current context.  Reentry from a0 is not allowed. */\n\tassert(arena != arena_get(tsd_tsdn(tsd), 0, false));\n\n\tbool fast = tsd_fast(tsd);\n\tassert(tsd_reentrancy_level_get(tsd) < INT8_MAX);\n\t++*tsd_reentrancy_levelp_get(tsd);\n\tif (fast) {\n\t\t/* Prepare slow path for reentrancy. */\n\t\ttsd_slow_update(tsd);\n\t\tassert(tsd_state_get(tsd) == tsd_state_nominal_slow);\n\t}\n}\n\nstatic inline void\npost_reentrancy(tsd_t *tsd) {\n\tint8_t *reentrancy_level = tsd_reentrancy_levelp_get(tsd);\n\tassert(*reentrancy_level > 0);\n\tif (--*reentrancy_level == 0) {\n\t\ttsd_slow_update(tsd);\n\t}\n}\n\n#endif /* JEMALLOC_INTERNAL_INLINES_A_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/jemalloc_internal_inlines_b.h",
    "content": "#ifndef JEMALLOC_INTERNAL_INLINES_B_H\n#define JEMALLOC_INTERNAL_INLINES_B_H\n\n#include \"jemalloc/internal/rtree.h\"\n\n/* Choose an arena based on a per-thread value. */\nstatic inline arena_t *\narena_choose_impl(tsd_t *tsd, arena_t *arena, bool internal) {\n\tarena_t *ret;\n\n\tif (arena != NULL) {\n\t\treturn arena;\n\t}\n\n\t/* During reentrancy, arena 0 is the safest bet. */\n\tif (unlikely(tsd_reentrancy_level_get(tsd) > 0)) {\n\t\treturn arena_get(tsd_tsdn(tsd), 0, true);\n\t}\n\n\tret = internal ? tsd_iarena_get(tsd) : tsd_arena_get(tsd);\n\tif (unlikely(ret == NULL)) {\n\t\tret = arena_choose_hard(tsd, internal);\n\t\tassert(ret);\n\t\tif (tcache_available(tsd)) {\n\t\t\ttcache_t *tcache = tcache_get(tsd);\n\t\t\tif (tcache->arena != NULL) {\n\t\t\t\t/* See comments in tcache_data_init().*/\n\t\t\t\tassert(tcache->arena ==\n\t\t\t\t    arena_get(tsd_tsdn(tsd), 0, false));\n\t\t\t\tif (tcache->arena != ret) {\n\t\t\t\t\ttcache_arena_reassociate(tsd_tsdn(tsd),\n\t\t\t\t\t    tcache, ret);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\ttcache_arena_associate(tsd_tsdn(tsd), tcache,\n\t\t\t\t    ret);\n\t\t\t}\n\t\t}\n\t}\n\n\t/*\n\t * Note that for percpu arena, if the current arena is outside of the\n\t * auto percpu arena range, (i.e. thread is assigned to a manually\n\t * managed arena), then percpu arena is skipped.\n\t */\n\tif (have_percpu_arena && PERCPU_ARENA_ENABLED(opt_percpu_arena) &&\n\t    !internal && (arena_ind_get(ret) <\n\t    percpu_arena_ind_limit(opt_percpu_arena)) && (ret->last_thd !=\n\t    tsd_tsdn(tsd))) {\n\t\tunsigned ind = percpu_arena_choose();\n\t\tif (arena_ind_get(ret) != ind) {\n\t\t\tpercpu_arena_update(tsd, ind);\n\t\t\tret = tsd_arena_get(tsd);\n\t\t}\n\t\tret->last_thd = tsd_tsdn(tsd);\n\t}\n\n\treturn ret;\n}\n\nstatic inline arena_t *\narena_choose(tsd_t *tsd, arena_t *arena) {\n\treturn arena_choose_impl(tsd, arena, false);\n}\n\nstatic inline arena_t *\narena_ichoose(tsd_t *tsd, arena_t *arena) {\n\treturn arena_choose_impl(tsd, arena, true);\n}\n\nstatic inline bool\narena_is_auto(arena_t *arena) {\n\tassert(narenas_auto > 0);\n\n\treturn (arena_ind_get(arena) < manual_arena_base);\n}\n\nJEMALLOC_ALWAYS_INLINE extent_t *\niealloc(tsdn_t *tsdn, const void *ptr) {\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\n\treturn rtree_extent_read(tsdn, &extents_rtree, rtree_ctx,\n\t    (uintptr_t)ptr, true);\n}\n\n#endif /* JEMALLOC_INTERNAL_INLINES_B_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/jemalloc_internal_inlines_c.h",
    "content": "#ifndef JEMALLOC_INTERNAL_INLINES_C_H\n#define JEMALLOC_INTERNAL_INLINES_C_H\n\n#include \"jemalloc/internal/hook.h\"\n#include \"jemalloc/internal/jemalloc_internal_types.h\"\n#include \"jemalloc/internal/sz.h\"\n#include \"jemalloc/internal/witness.h\"\n\n/*\n * Translating the names of the 'i' functions:\n *   Abbreviations used in the first part of the function name (before\n *   alloc/dalloc) describe what that function accomplishes:\n *     a: arena (query)\n *     s: size (query, or sized deallocation)\n *     e: extent (query)\n *     p: aligned (allocates)\n *     vs: size (query, without knowing that the pointer is into the heap)\n *     r: rallocx implementation\n *     x: xallocx implementation\n *   Abbreviations used in the second part of the function name (after\n *   alloc/dalloc) describe the arguments it takes\n *     z: whether to return zeroed memory\n *     t: accepts a tcache_t * parameter\n *     m: accepts an arena_t * parameter\n */\n\nJEMALLOC_ALWAYS_INLINE arena_t *\niaalloc(tsdn_t *tsdn, const void *ptr) {\n\tassert(ptr != NULL);\n\n\treturn arena_aalloc(tsdn, ptr);\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\nisalloc(tsdn_t *tsdn, const void *ptr) {\n\tassert(ptr != NULL);\n\n\treturn arena_salloc(tsdn, ptr);\n}\n\nJEMALLOC_ALWAYS_INLINE void *\niallocztm(tsdn_t *tsdn, size_t size, szind_t ind, bool zero, tcache_t *tcache,\n    bool is_internal, arena_t *arena, bool slow_path) {\n\tvoid *ret;\n\n\tassert(!is_internal || tcache == NULL);\n\tassert(!is_internal || arena == NULL || arena_is_auto(arena));\n\tif (!tsdn_null(tsdn) && tsd_reentrancy_level_get(tsdn_tsd(tsdn)) == 0) {\n\t\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t\t    WITNESS_RANK_CORE, 0);\n\t}\n\n\tret = arena_malloc(tsdn, arena, size, ind, zero, tcache, slow_path);\n\tif (config_stats && is_internal && likely(ret != NULL)) {\n\t\tarena_internal_add(iaalloc(tsdn, ret), isalloc(tsdn, ret));\n\t}\n\treturn ret;\n}\n\nJEMALLOC_ALWAYS_INLINE void *\nialloc(tsd_t *tsd, size_t size, szind_t ind, bool zero, bool slow_path) {\n\treturn iallocztm(tsd_tsdn(tsd), size, ind, zero, tcache_get(tsd), false,\n\t    NULL, slow_path);\n}\n\nJEMALLOC_ALWAYS_INLINE void *\nipallocztm(tsdn_t *tsdn, size_t usize, size_t alignment, bool zero,\n    tcache_t *tcache, bool is_internal, arena_t *arena) {\n\tvoid *ret;\n\n\tassert(usize != 0);\n\tassert(usize == sz_sa2u(usize, alignment));\n\tassert(!is_internal || tcache == NULL);\n\tassert(!is_internal || arena == NULL || arena_is_auto(arena));\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\tret = arena_palloc(tsdn, arena, usize, alignment, zero, tcache);\n\tassert(ALIGNMENT_ADDR2BASE(ret, alignment) == ret);\n\tif (config_stats && is_internal && likely(ret != NULL)) {\n\t\tarena_internal_add(iaalloc(tsdn, ret), isalloc(tsdn, ret));\n\t}\n\treturn ret;\n}\n\nJEMALLOC_ALWAYS_INLINE void *\nipalloct(tsdn_t *tsdn, size_t usize, size_t alignment, bool zero,\n    tcache_t *tcache, arena_t *arena) {\n\treturn ipallocztm(tsdn, usize, alignment, zero, tcache, false, arena);\n}\n\nJEMALLOC_ALWAYS_INLINE void *\nipalloc(tsd_t *tsd, size_t usize, size_t alignment, bool zero) {\n\treturn ipallocztm(tsd_tsdn(tsd), usize, alignment, zero,\n\t    tcache_get(tsd), false, NULL);\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\nivsalloc(tsdn_t *tsdn, const void *ptr) {\n\treturn arena_vsalloc(tsdn, ptr);\n}\n\nJEMALLOC_ALWAYS_INLINE void\nidalloctm(tsdn_t *tsdn, void *ptr, tcache_t *tcache, alloc_ctx_t *alloc_ctx,\n    bool is_internal, bool slow_path) {\n\tassert(ptr != NULL);\n\tassert(!is_internal || tcache == NULL);\n\tassert(!is_internal || arena_is_auto(iaalloc(tsdn, ptr)));\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\tif (config_stats && is_internal) {\n\t\tarena_internal_sub(iaalloc(tsdn, ptr), isalloc(tsdn, ptr));\n\t}\n\tif (!is_internal && !tsdn_null(tsdn) &&\n\t    tsd_reentrancy_level_get(tsdn_tsd(tsdn)) != 0) {\n\t\tassert(tcache == NULL);\n\t}\n\tarena_dalloc(tsdn, ptr, tcache, alloc_ctx, slow_path);\n}\n\nJEMALLOC_ALWAYS_INLINE void\nidalloc(tsd_t *tsd, void *ptr) {\n\tidalloctm(tsd_tsdn(tsd), ptr, tcache_get(tsd), NULL, false, true);\n}\n\nJEMALLOC_ALWAYS_INLINE void\nisdalloct(tsdn_t *tsdn, void *ptr, size_t size, tcache_t *tcache,\n    alloc_ctx_t *alloc_ctx, bool slow_path) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\tarena_sdalloc(tsdn, ptr, size, tcache, alloc_ctx, slow_path);\n}\n\nJEMALLOC_ALWAYS_INLINE void *\niralloct_realign(tsdn_t *tsdn, void *ptr, size_t oldsize, size_t size,\n    size_t alignment, bool zero, tcache_t *tcache, arena_t *arena,\n    hook_ralloc_args_t *hook_args) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\tvoid *p;\n\tsize_t usize, copysize;\n\n\tusize = sz_sa2u(size, alignment);\n\tif (unlikely(usize == 0 || usize > SC_LARGE_MAXCLASS)) {\n\t\treturn NULL;\n\t}\n\tp = ipalloct(tsdn, usize, alignment, zero, tcache, arena);\n\tif (p == NULL) {\n\t\treturn NULL;\n\t}\n\t/*\n\t * Copy at most size bytes (not size+extra), since the caller has no\n\t * expectation that the extra bytes will be reliably preserved.\n\t */\n\tcopysize = (size < oldsize) ? size : oldsize;\n\tmemcpy(p, ptr, copysize);\n\thook_invoke_alloc(hook_args->is_realloc\n\t    ? hook_alloc_realloc : hook_alloc_rallocx, p, (uintptr_t)p,\n\t    hook_args->args);\n\thook_invoke_dalloc(hook_args->is_realloc\n\t    ? hook_dalloc_realloc : hook_dalloc_rallocx, ptr, hook_args->args);\n\tisdalloct(tsdn, ptr, oldsize, tcache, NULL, true);\n\treturn p;\n}\n\n/*\n * is_realloc threads through the knowledge of whether or not this call comes\n * from je_realloc (as opposed to je_rallocx); this ensures that we pass the\n * correct entry point into any hooks.\n * Note that these functions are all force-inlined, so no actual bool gets\n * passed-around anywhere.\n */\nJEMALLOC_ALWAYS_INLINE void *\niralloct(tsdn_t *tsdn, void *ptr, size_t oldsize, size_t size, size_t alignment,\n    bool zero, tcache_t *tcache, arena_t *arena, hook_ralloc_args_t *hook_args)\n{\n\tassert(ptr != NULL);\n\tassert(size != 0);\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\tif (alignment != 0 && ((uintptr_t)ptr & ((uintptr_t)alignment-1))\n\t    != 0) {\n\t\t/*\n\t\t * Existing object alignment is inadequate; allocate new space\n\t\t * and copy.\n\t\t */\n\t\treturn iralloct_realign(tsdn, ptr, oldsize, size, alignment,\n\t\t    zero, tcache, arena, hook_args);\n\t}\n\n\treturn arena_ralloc(tsdn, arena, ptr, oldsize, size, alignment, zero,\n\t    tcache, hook_args);\n}\n\nJEMALLOC_ALWAYS_INLINE void *\niralloc(tsd_t *tsd, void *ptr, size_t oldsize, size_t size, size_t alignment,\n    bool zero, hook_ralloc_args_t *hook_args) {\n\treturn iralloct(tsd_tsdn(tsd), ptr, oldsize, size, alignment, zero,\n\t    tcache_get(tsd), NULL, hook_args);\n}\n\nJEMALLOC_ALWAYS_INLINE bool\nixalloc(tsdn_t *tsdn, void *ptr, size_t oldsize, size_t size, size_t extra,\n    size_t alignment, bool zero, size_t *newsize) {\n\tassert(ptr != NULL);\n\tassert(size != 0);\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\tif (alignment != 0 && ((uintptr_t)ptr & ((uintptr_t)alignment-1))\n\t    != 0) {\n\t\t/* Existing object alignment is inadequate. */\n\t\t*newsize = oldsize;\n\t\treturn true;\n\t}\n\n\treturn arena_ralloc_no_move(tsdn, ptr, oldsize, size, extra, zero,\n\t    newsize);\n}\n\nJEMALLOC_ALWAYS_INLINE int\niget_defrag_hint(tsdn_t *tsdn, void* ptr) {\n\tint defrag = 0;\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\tszind_t szind;\n\tbool is_slab;\n\trtree_szind_slab_read(tsdn, &extents_rtree, rtree_ctx, (uintptr_t)ptr, true, &szind, &is_slab);\n\tif (likely(is_slab)) {\n\t\t/* Small allocation. */\n\t\textent_t *slab = iealloc(tsdn, ptr);\n\t\tarena_t *arena = extent_arena_get(slab);\n\t\tszind_t binind = extent_szind_get(slab);\n\t\tunsigned shardind = extent_binshard_get(slab);\n\t\tbins_t *bin = &arena->bins[binind];\n\t\tbin_t* binshard = &bin->bin_shards[shardind];\n\t\tmalloc_mutex_lock(tsdn, &binshard->lock);\n\t\t/* don't bother moving allocations from the slab currently used for new allocations */\n\t\tif (slab != binshard->slabcur) {\n\t\t\tint free_in_slab = extent_nfree_get(slab);\n\t\t\tif (free_in_slab) {\n\t\t\t\tconst bin_info_t *bin_info = &bin_infos[binind];\n\t\t\t\tssize_t curslabs = binshard->stats.curslabs;\n\t\t\t\tsize_t curregs = binshard->stats.curregs;\n\t\t\t\tif (binshard->slabcur) {\n\t\t\t\t\t/* remove slabcur from the overall utilization */\n\t\t\t\t\tcurregs -= bin_info->nregs - extent_nfree_get(binshard->slabcur);\n\t\t\t\t\tcurslabs -= 1;\n\t\t\t\t}\n\t\t\t\t/* Compare the utilization ratio of the slab in question to the total average,\n\t\t\t\t * to avoid precision lost and division, we do that by extrapolating the usage\n\t\t\t\t * of the slab as if all slabs have the same usage. If this slab is less used \n\t\t\t\t * than the average, we'll prefer to evict the data to hopefully more used ones */\n\t\t\t\tdefrag = (bin_info->nregs - free_in_slab) * curslabs <= curregs;\n\t\t\t}\n\t\t}\n\t\tmalloc_mutex_unlock(tsdn, &binshard->lock);\n\t}\n\treturn defrag;\n}\n\n#endif /* JEMALLOC_INTERNAL_INLINES_C_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/jemalloc_internal_macros.h",
    "content": "#ifndef JEMALLOC_INTERNAL_MACROS_H\n#define JEMALLOC_INTERNAL_MACROS_H\n\n#ifdef JEMALLOC_DEBUG\n#  define JEMALLOC_ALWAYS_INLINE static inline\n#else\n#  define JEMALLOC_ALWAYS_INLINE JEMALLOC_ATTR(always_inline) static inline\n#endif\n#ifdef _MSC_VER\n#  define inline _inline\n#endif\n\n#define UNUSED JEMALLOC_ATTR(unused)\n\n#define ZU(z)\t((size_t)z)\n#define ZD(z)\t((ssize_t)z)\n#define QU(q)\t((uint64_t)q)\n#define QD(q)\t((int64_t)q)\n\n#define KZU(z)\tZU(z##ULL)\n#define KZD(z)\tZD(z##LL)\n#define KQU(q)\tQU(q##ULL)\n#define KQD(q)\tQI(q##LL)\n\n#ifndef __DECONST\n#  define\t__DECONST(type, var)\t((type)(uintptr_t)(const void *)(var))\n#endif\n\n#if !defined(JEMALLOC_HAS_RESTRICT) || defined(__cplusplus)\n#  define restrict\n#endif\n\n/* Various function pointers are static and immutable except during testing. */\n#ifdef JEMALLOC_JET\n#  define JET_MUTABLE\n#else\n#  define JET_MUTABLE const\n#endif\n\n#define JEMALLOC_VA_ARGS_HEAD(head, ...) head\n#define JEMALLOC_VA_ARGS_TAIL(head, ...) __VA_ARGS__\n\n#if (defined(__GNUC__) || defined(__GNUG__)) && !defined(__clang__) \\\n  && defined(JEMALLOC_HAVE_ATTR) && (__GNUC__ >= 7)\n#define JEMALLOC_FALLTHROUGH JEMALLOC_ATTR(fallthrough);\n#else\n#define JEMALLOC_FALLTHROUGH /* falls through */\n#endif\n\n/* Diagnostic suppression macros */\n#if defined(_MSC_VER) && !defined(__clang__)\n#  define JEMALLOC_DIAGNOSTIC_PUSH __pragma(warning(push))\n#  define JEMALLOC_DIAGNOSTIC_POP __pragma(warning(pop))\n#  define JEMALLOC_DIAGNOSTIC_IGNORE(W) __pragma(warning(disable:W))\n#  define JEMALLOC_DIAGNOSTIC_IGNORE_MISSING_STRUCT_FIELD_INITIALIZERS\n#  define JEMALLOC_DIAGNOSTIC_IGNORE_TYPE_LIMITS\n#  define JEMALLOC_DIAGNOSTIC_IGNORE_ALLOC_SIZE_LARGER_THAN\n#  define JEMALLOC_DIAGNOSTIC_DISABLE_SPURIOUS\n/* #pragma GCC diagnostic first appeared in gcc 4.6. */\n#elif (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && \\\n  (__GNUC_MINOR__ > 5)))) || defined(__clang__)\n/*\n * The JEMALLOC_PRAGMA__ macro is an implementation detail of the GCC and Clang\n * diagnostic suppression macros and should not be used anywhere else.\n */\n#  define JEMALLOC_PRAGMA__(X) _Pragma(#X)\n#  define JEMALLOC_DIAGNOSTIC_PUSH JEMALLOC_PRAGMA__(GCC diagnostic push)\n#  define JEMALLOC_DIAGNOSTIC_POP JEMALLOC_PRAGMA__(GCC diagnostic pop)\n#  define JEMALLOC_DIAGNOSTIC_IGNORE(W) \\\n     JEMALLOC_PRAGMA__(GCC diagnostic ignored W)\n\n/*\n * The -Wmissing-field-initializers warning is buggy in GCC versions < 5.1 and\n * all clang versions up to version 7 (currently trunk, unreleased).  This macro\n * suppresses the warning for the affected compiler versions only.\n */\n#  if ((defined(__GNUC__) && !defined(__clang__)) && (__GNUC__ < 5)) || \\\n     defined(__clang__)\n#    define JEMALLOC_DIAGNOSTIC_IGNORE_MISSING_STRUCT_FIELD_INITIALIZERS  \\\n          JEMALLOC_DIAGNOSTIC_IGNORE(\"-Wmissing-field-initializers\")\n#  else\n#    define JEMALLOC_DIAGNOSTIC_IGNORE_MISSING_STRUCT_FIELD_INITIALIZERS\n#  endif\n\n#  define JEMALLOC_DIAGNOSTIC_IGNORE_TYPE_LIMITS  \\\n     JEMALLOC_DIAGNOSTIC_IGNORE(\"-Wtype-limits\")\n#  define JEMALLOC_DIAGNOSTIC_IGNORE_UNUSED_PARAMETER \\\n     JEMALLOC_DIAGNOSTIC_IGNORE(\"-Wunused-parameter\")\n#  if defined(__GNUC__) && !defined(__clang__) && (__GNUC__ >= 7)\n#    define JEMALLOC_DIAGNOSTIC_IGNORE_ALLOC_SIZE_LARGER_THAN \\\n       JEMALLOC_DIAGNOSTIC_IGNORE(\"-Walloc-size-larger-than=\")\n#  else\n#    define JEMALLOC_DIAGNOSTIC_IGNORE_ALLOC_SIZE_LARGER_THAN\n#  endif\n#  define JEMALLOC_DIAGNOSTIC_DISABLE_SPURIOUS \\\n  JEMALLOC_DIAGNOSTIC_PUSH \\\n  JEMALLOC_DIAGNOSTIC_IGNORE_UNUSED_PARAMETER\n#else\n#  define JEMALLOC_DIAGNOSTIC_PUSH\n#  define JEMALLOC_DIAGNOSTIC_POP\n#  define JEMALLOC_DIAGNOSTIC_IGNORE(W)\n#  define JEMALLOC_DIAGNOSTIC_IGNORE_MISSING_STRUCT_FIELD_INITIALIZERS\n#  define JEMALLOC_DIAGNOSTIC_IGNORE_TYPE_LIMITS\n#  define JEMALLOC_DIAGNOSTIC_IGNORE_ALLOC_SIZE_LARGER_THAN\n#  define JEMALLOC_DIAGNOSTIC_DISABLE_SPURIOUS\n#endif\n\n/*\n * Disables spurious diagnostics for all headers.  Since these headers are not\n * included by users directly, it does not affect their diagnostic settings.\n */\nJEMALLOC_DIAGNOSTIC_DISABLE_SPURIOUS\n\n#endif /* JEMALLOC_INTERNAL_MACROS_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/jemalloc_internal_types.h",
    "content": "#ifndef JEMALLOC_INTERNAL_TYPES_H\n#define JEMALLOC_INTERNAL_TYPES_H\n\n#include \"jemalloc/internal/quantum.h\"\n\n/* Page size index type. */\ntypedef unsigned pszind_t;\n\n/* Size class index type. */\ntypedef unsigned szind_t;\n\n/* Processor / core id type. */\ntypedef int malloc_cpuid_t;\n\n/*\n * Flags bits:\n *\n * a: arena\n * t: tcache\n * 0: unused\n * z: zero\n * n: alignment\n *\n * aaaaaaaa aaaatttt tttttttt 0znnnnnn\n */\n#define MALLOCX_ARENA_BITS\t12\n#define MALLOCX_TCACHE_BITS\t12\n#define MALLOCX_LG_ALIGN_BITS\t6\n#define MALLOCX_ARENA_SHIFT\t20\n#define MALLOCX_TCACHE_SHIFT\t8\n#define MALLOCX_ARENA_MASK \\\n    (((1 << MALLOCX_ARENA_BITS) - 1) << MALLOCX_ARENA_SHIFT)\n/* NB: Arena index bias decreases the maximum number of arenas by 1. */\n#define MALLOCX_ARENA_LIMIT\t((1 << MALLOCX_ARENA_BITS) - 1)\n#define MALLOCX_TCACHE_MASK \\\n    (((1 << MALLOCX_TCACHE_BITS) - 1) << MALLOCX_TCACHE_SHIFT)\n#define MALLOCX_TCACHE_MAX\t((1 << MALLOCX_TCACHE_BITS) - 3)\n#define MALLOCX_LG_ALIGN_MASK\t((1 << MALLOCX_LG_ALIGN_BITS) - 1)\n/* Use MALLOCX_ALIGN_GET() if alignment may not be specified in flags. */\n#define MALLOCX_ALIGN_GET_SPECIFIED(flags)\t\t\t\t\\\n    (ZU(1) << (flags & MALLOCX_LG_ALIGN_MASK))\n#define MALLOCX_ALIGN_GET(flags)\t\t\t\t\t\\\n    (MALLOCX_ALIGN_GET_SPECIFIED(flags) & (SIZE_T_MAX-1))\n#define MALLOCX_ZERO_GET(flags)\t\t\t\t\t\t\\\n    ((bool)(flags & MALLOCX_ZERO))\n\n#define MALLOCX_TCACHE_GET(flags)\t\t\t\t\t\\\n    (((unsigned)((flags & MALLOCX_TCACHE_MASK) >> MALLOCX_TCACHE_SHIFT)) - 2)\n#define MALLOCX_ARENA_GET(flags)\t\t\t\t\t\\\n    (((unsigned)(((unsigned)flags) >> MALLOCX_ARENA_SHIFT)) - 1)\n\n/* Smallest size class to support. */\n#define TINY_MIN\t\t(1U << LG_TINY_MIN)\n\n#define LONG\t\t\t((size_t)(1U << LG_SIZEOF_LONG))\n#define LONG_MASK\t\t(LONG - 1)\n\n/* Return the smallest long multiple that is >= a. */\n#define LONG_CEILING(a)\t\t\t\t\t\t\t\\\n\t(((a) + LONG_MASK) & ~LONG_MASK)\n\n#define SIZEOF_PTR\t\t(1U << LG_SIZEOF_PTR)\n#define PTR_MASK\t\t(SIZEOF_PTR - 1)\n\n/* Return the smallest (void *) multiple that is >= a. */\n#define PTR_CEILING(a)\t\t\t\t\t\t\t\\\n\t(((a) + PTR_MASK) & ~PTR_MASK)\n\n/*\n * Maximum size of L1 cache line.  This is used to avoid cache line aliasing.\n * In addition, this controls the spacing of cacheline-spaced size classes.\n *\n * CACHELINE cannot be based on LG_CACHELINE because __declspec(align()) can\n * only handle raw constants.\n */\n#define LG_CACHELINE\t\t6\n#define CACHELINE\t\t64\n#define CACHELINE_MASK\t\t(CACHELINE - 1)\n\n/* Return the smallest cacheline multiple that is >= s. */\n#define CACHELINE_CEILING(s)\t\t\t\t\t\t\\\n\t(((s) + CACHELINE_MASK) & ~CACHELINE_MASK)\n\n/* Return the nearest aligned address at or below a. */\n#define ALIGNMENT_ADDR2BASE(a, alignment)\t\t\t\t\\\n\t((void *)((uintptr_t)(a) & ((~(alignment)) + 1)))\n\n/* Return the offset between a and the nearest aligned address at or below a. */\n#define ALIGNMENT_ADDR2OFFSET(a, alignment)\t\t\t\t\\\n\t((size_t)((uintptr_t)(a) & (alignment - 1)))\n\n/* Return the smallest alignment multiple that is >= s. */\n#define ALIGNMENT_CEILING(s, alignment)\t\t\t\t\t\\\n\t(((s) + (alignment - 1)) & ((~(alignment)) + 1))\n\n/* Declare a variable-length array. */\n#if __STDC_VERSION__ < 199901L\n#  ifdef _MSC_VER\n#    include <malloc.h>\n#    define alloca _alloca\n#  else\n#    ifdef JEMALLOC_HAS_ALLOCA_H\n#      include <alloca.h>\n#    else\n#      include <stdlib.h>\n#    endif\n#  endif\n#  define VARIABLE_ARRAY(type, name, count) \\\n\ttype *name = alloca(sizeof(type) * (count))\n#else\n#  define VARIABLE_ARRAY(type, name, count) type name[(count)]\n#endif\n\n#endif /* JEMALLOC_INTERNAL_TYPES_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/jemalloc_preamble.h.in",
    "content": "#ifndef JEMALLOC_PREAMBLE_H\n#define JEMALLOC_PREAMBLE_H\n\n#include \"jemalloc_internal_defs.h\"\n#include \"jemalloc/internal/jemalloc_internal_decls.h\"\n\n#ifdef JEMALLOC_UTRACE\n#include <sys/ktrace.h>\n#endif\n\n#define JEMALLOC_NO_DEMANGLE\n#ifdef JEMALLOC_JET\n#  undef JEMALLOC_IS_MALLOC\n#  define JEMALLOC_N(n) jet_##n\n#  include \"jemalloc/internal/public_namespace.h\"\n#  define JEMALLOC_NO_RENAME\n#  include \"../jemalloc@install_suffix@.h\"\n#  undef JEMALLOC_NO_RENAME\n#else\n#  define JEMALLOC_N(n) @private_namespace@##n\n#  include \"../jemalloc@install_suffix@.h\"\n#endif\n\n#if defined(JEMALLOC_OSATOMIC)\n#include <libkern/OSAtomic.h>\n#endif\n\n#ifdef JEMALLOC_ZONE\n#include <mach/mach_error.h>\n#include <mach/mach_init.h>\n#include <mach/vm_map.h>\n#endif\n\n#include \"jemalloc/internal/jemalloc_internal_macros.h\"\n\n/*\n * Note that the ordering matters here; the hook itself is name-mangled.  We\n * want the inclusion of hooks to happen early, so that we hook as much as\n * possible.\n */\n#ifndef JEMALLOC_NO_PRIVATE_NAMESPACE\n#  ifndef JEMALLOC_JET\n#    include \"jemalloc/internal/private_namespace.h\"\n#  else\n#    include \"jemalloc/internal/private_namespace_jet.h\"\n#  endif\n#endif\n#include \"jemalloc/internal/test_hooks.h\"\n\n#ifdef JEMALLOC_DEFINE_MADVISE_FREE\n#  define JEMALLOC_MADV_FREE 8\n#endif\n\nstatic const bool config_debug =\n#ifdef JEMALLOC_DEBUG\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool have_dss =\n#ifdef JEMALLOC_DSS\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool have_madvise_huge =\n#ifdef JEMALLOC_HAVE_MADVISE_HUGE\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool config_fill =\n#ifdef JEMALLOC_FILL\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool config_lazy_lock =\n#ifdef JEMALLOC_LAZY_LOCK\n    true\n#else\n    false\n#endif\n    ;\nstatic const char * const config_malloc_conf = JEMALLOC_CONFIG_MALLOC_CONF;\nstatic const bool config_prof =\n#ifdef JEMALLOC_PROF\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool config_prof_libgcc =\n#ifdef JEMALLOC_PROF_LIBGCC\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool config_prof_libunwind =\n#ifdef JEMALLOC_PROF_LIBUNWIND\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool maps_coalesce =\n#ifdef JEMALLOC_MAPS_COALESCE\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool config_stats =\n#ifdef JEMALLOC_STATS\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool config_tls =\n#ifdef JEMALLOC_TLS\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool config_utrace =\n#ifdef JEMALLOC_UTRACE\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool config_xmalloc =\n#ifdef JEMALLOC_XMALLOC\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool config_cache_oblivious =\n#ifdef JEMALLOC_CACHE_OBLIVIOUS\n    true\n#else\n    false\n#endif\n    ;\n/*\n * Undocumented, for jemalloc development use only at the moment.  See the note\n * in jemalloc/internal/log.h.\n */\nstatic const bool config_log =\n#ifdef JEMALLOC_LOG\n    true\n#else\n    false\n#endif\n    ;\n/*\n * Are extra safety checks enabled; things like checking the size of sized\n * deallocations, double-frees, etc.\n */\nstatic const bool config_opt_safety_checks =\n#ifdef JEMALLOC_OPT_SAFETY_CHECKS\n    true\n#elif defined(JEMALLOC_DEBUG)\n    /*\n     * This lets us only guard safety checks by one flag instead of two; fast\n     * checks can guard solely by config_opt_safety_checks and run in debug mode\n     * too.\n     */\n    true\n#else\n    false\n#endif\n    ;\n\n#if defined(_WIN32) || defined(JEMALLOC_HAVE_SCHED_GETCPU)\n/* Currently percpu_arena depends on sched_getcpu. */\n#define JEMALLOC_PERCPU_ARENA\n#endif\nstatic const bool have_percpu_arena =\n#ifdef JEMALLOC_PERCPU_ARENA\n    true\n#else\n    false\n#endif\n    ;\n/*\n * Undocumented, and not recommended; the application should take full\n * responsibility for tracking provenance.\n */\nstatic const bool force_ivsalloc =\n#ifdef JEMALLOC_FORCE_IVSALLOC\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool have_background_thread =\n#ifdef JEMALLOC_BACKGROUND_THREAD\n    true\n#else\n    false\n#endif\n    ;\n\n#endif /* JEMALLOC_PREAMBLE_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/large_externs.h",
    "content": "#ifndef JEMALLOC_INTERNAL_LARGE_EXTERNS_H\n#define JEMALLOC_INTERNAL_LARGE_EXTERNS_H\n\n#include \"jemalloc/internal/hook.h\"\n\nvoid *large_malloc(tsdn_t *tsdn, arena_t *arena, size_t usize, bool zero);\nvoid *large_palloc(tsdn_t *tsdn, arena_t *arena, size_t usize, size_t alignment,\n    bool zero);\nbool large_ralloc_no_move(tsdn_t *tsdn, extent_t *extent, size_t usize_min,\n    size_t usize_max, bool zero);\nvoid *large_ralloc(tsdn_t *tsdn, arena_t *arena, void *ptr, size_t usize,\n    size_t alignment, bool zero, tcache_t *tcache,\n    hook_ralloc_args_t *hook_args);\n\ntypedef void (large_dalloc_junk_t)(void *, size_t);\nextern large_dalloc_junk_t *JET_MUTABLE large_dalloc_junk;\n\ntypedef void (large_dalloc_maybe_junk_t)(void *, size_t);\nextern large_dalloc_maybe_junk_t *JET_MUTABLE large_dalloc_maybe_junk;\n\nvoid large_dalloc_prep_junked_locked(tsdn_t *tsdn, extent_t *extent);\nvoid large_dalloc_finish(tsdn_t *tsdn, extent_t *extent);\nvoid large_dalloc(tsdn_t *tsdn, extent_t *extent);\nsize_t large_salloc(tsdn_t *tsdn, const extent_t *extent);\nprof_tctx_t *large_prof_tctx_get(tsdn_t *tsdn, const extent_t *extent);\nvoid large_prof_tctx_set(tsdn_t *tsdn, extent_t *extent, prof_tctx_t *tctx);\nvoid large_prof_tctx_reset(tsdn_t *tsdn, extent_t *extent);\n\nnstime_t large_prof_alloc_time_get(const extent_t *extent);\nvoid large_prof_alloc_time_set(extent_t *extent, nstime_t time);\n\n#endif /* JEMALLOC_INTERNAL_LARGE_EXTERNS_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/log.h",
    "content": "#ifndef JEMALLOC_INTERNAL_LOG_H\n#define JEMALLOC_INTERNAL_LOG_H\n\n#include \"jemalloc/internal/atomic.h\"\n#include \"jemalloc/internal/malloc_io.h\"\n#include \"jemalloc/internal/mutex.h\"\n\n#ifdef JEMALLOC_LOG\n#  define JEMALLOC_LOG_VAR_BUFSIZE 1000\n#else\n#  define JEMALLOC_LOG_VAR_BUFSIZE 1\n#endif\n\n#define JEMALLOC_LOG_BUFSIZE 4096\n\n/*\n * The log malloc_conf option is a '|'-delimited list of log_var name segments\n * which should be logged.  The names are themselves hierarchical, with '.' as\n * the delimiter (a \"segment\" is just a prefix in the log namespace).  So, if\n * you have:\n *\n * log(\"arena\", \"log msg for arena\"); // 1\n * log(\"arena.a\", \"log msg for arena.a\"); // 2\n * log(\"arena.b\", \"log msg for arena.b\"); // 3\n * log(\"arena.a.a\", \"log msg for arena.a.a\"); // 4\n * log(\"extent.a\", \"log msg for extent.a\"); // 5\n * log(\"extent.b\", \"log msg for extent.b\"); // 6\n *\n * And your malloc_conf option is \"log=arena.a|extent\", then lines 2, 4, 5, and\n * 6 will print at runtime.  You can enable logging from all log vars by\n * writing \"log=.\".\n *\n * None of this should be regarded as a stable API for right now.  It's intended\n * as a debugging interface, to let us keep around some of our printf-debugging\n * statements.\n */\n\nextern char log_var_names[JEMALLOC_LOG_VAR_BUFSIZE];\nextern atomic_b_t log_init_done;\n\ntypedef struct log_var_s log_var_t;\nstruct log_var_s {\n\t/*\n\t * Lowest bit is \"inited\", second lowest is \"enabled\".  Putting them in\n\t * a single word lets us avoid any fences on weak architectures.\n\t */\n\tatomic_u_t state;\n\tconst char *name;\n};\n\n#define LOG_NOT_INITIALIZED 0U\n#define LOG_INITIALIZED_NOT_ENABLED 1U\n#define LOG_ENABLED 2U\n\n#define LOG_VAR_INIT(name_str) {ATOMIC_INIT(LOG_NOT_INITIALIZED), name_str}\n\n/*\n * Returns the value we should assume for state (which is not necessarily\n * accurate; if logging is done before logging has finished initializing, then\n * we default to doing the safe thing by logging everything).\n */\nunsigned log_var_update_state(log_var_t *log_var);\n\n/* We factor out the metadata management to allow us to test more easily. */\n#define log_do_begin(log_var)\t\t\t\t\t\t\\\nif (config_log) {\t\t\t\t\t\t\t\\\n\tunsigned log_state = atomic_load_u(&(log_var).state,\t\t\\\n\t    ATOMIC_RELAXED);\t\t\t\t\t\t\\\n\tif (unlikely(log_state == LOG_NOT_INITIALIZED)) {\t\t\\\n\t\tlog_state = log_var_update_state(&(log_var));\t\t\\\n\t\tassert(log_state != LOG_NOT_INITIALIZED);\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tif (log_state == LOG_ENABLED) {\t\t\t\t\t\\\n\t\t{\n\t\t\t/* User code executes here. */\n#define log_do_end(log_var)\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n}\n\n/*\n * MSVC has some preprocessor bugs in its expansion of __VA_ARGS__ during\n * preprocessing.  To work around this, we take all potential extra arguments in\n * a var-args functions.  Since a varargs macro needs at least one argument in\n * the \"...\", we accept the format string there, and require that the first\n * argument in this \"...\" is a const char *.\n */\nstatic inline void\nlog_impl_varargs(const char *name, ...) {\n\tchar buf[JEMALLOC_LOG_BUFSIZE];\n\tva_list ap;\n\n\tva_start(ap, name);\n\tconst char *format = va_arg(ap, const char *);\n\tsize_t dst_offset = 0;\n\tdst_offset += malloc_snprintf(buf, JEMALLOC_LOG_BUFSIZE, \"%s: \", name);\n\tdst_offset += malloc_vsnprintf(buf + dst_offset,\n\t    JEMALLOC_LOG_BUFSIZE - dst_offset, format, ap);\n\tdst_offset += malloc_snprintf(buf + dst_offset,\n\t    JEMALLOC_LOG_BUFSIZE - dst_offset, \"\\n\");\n\tva_end(ap);\n\n\tmalloc_write(buf);\n}\n\n/* Call as log(\"log.var.str\", \"format_string %d\", arg_for_format_string); */\n#define LOG(log_var_str, ...)\t\t\t\t\t\t\\\ndo {\t\t\t\t\t\t\t\t\t\\\n\tstatic log_var_t log_var = LOG_VAR_INIT(log_var_str);\t\t\\\n\tlog_do_begin(log_var)\t\t\t\t\t\t\\\n\t\tlog_impl_varargs((log_var).name, __VA_ARGS__);\t\t\\\n\tlog_do_end(log_var)\t\t\t\t\t\t\\\n} while (0)\n\n#endif /* JEMALLOC_INTERNAL_LOG_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/malloc_io.h",
    "content": "#ifndef JEMALLOC_INTERNAL_MALLOC_IO_H\n#define JEMALLOC_INTERNAL_MALLOC_IO_H\n\n#ifdef _WIN32\n#  ifdef _WIN64\n#    define FMT64_PREFIX \"ll\"\n#    define FMTPTR_PREFIX \"ll\"\n#  else\n#    define FMT64_PREFIX \"ll\"\n#    define FMTPTR_PREFIX \"\"\n#  endif\n#  define FMTd32 \"d\"\n#  define FMTu32 \"u\"\n#  define FMTx32 \"x\"\n#  define FMTd64 FMT64_PREFIX \"d\"\n#  define FMTu64 FMT64_PREFIX \"u\"\n#  define FMTx64 FMT64_PREFIX \"x\"\n#  define FMTdPTR FMTPTR_PREFIX \"d\"\n#  define FMTuPTR FMTPTR_PREFIX \"u\"\n#  define FMTxPTR FMTPTR_PREFIX \"x\"\n#else\n#  include <inttypes.h>\n#  define FMTd32 PRId32\n#  define FMTu32 PRIu32\n#  define FMTx32 PRIx32\n#  define FMTd64 PRId64\n#  define FMTu64 PRIu64\n#  define FMTx64 PRIx64\n#  define FMTdPTR PRIdPTR\n#  define FMTuPTR PRIuPTR\n#  define FMTxPTR PRIxPTR\n#endif\n\n/* Size of stack-allocated buffer passed to buferror(). */\n#define BUFERROR_BUF\t\t64\n\n/*\n * Size of stack-allocated buffer used by malloc_{,v,vc}printf().  This must be\n * large enough for all possible uses within jemalloc.\n */\n#define MALLOC_PRINTF_BUFSIZE\t4096\n\nint buferror(int err, char *buf, size_t buflen);\nuintmax_t malloc_strtoumax(const char *restrict nptr, char **restrict endptr,\n    int base);\nvoid malloc_write(const char *s);\n\n/*\n * malloc_vsnprintf() supports a subset of snprintf(3) that avoids floating\n * point math.\n */\nsize_t malloc_vsnprintf(char *str, size_t size, const char *format,\n    va_list ap);\nsize_t malloc_snprintf(char *str, size_t size, const char *format, ...)\n    JEMALLOC_FORMAT_PRINTF(3, 4);\n/*\n * The caller can set write_cb to null to choose to print with the\n * je_malloc_message hook.\n */\nvoid malloc_vcprintf(void (*write_cb)(void *, const char *), void *cbopaque,\n    const char *format, va_list ap);\nvoid malloc_cprintf(void (*write_cb)(void *, const char *), void *cbopaque,\n    const char *format, ...) JEMALLOC_FORMAT_PRINTF(3, 4);\nvoid malloc_printf(const char *format, ...) JEMALLOC_FORMAT_PRINTF(1, 2);\n\nstatic inline ssize_t\nmalloc_write_fd(int fd, const void *buf, size_t count) {\n#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_write)\n\t/*\n\t * Use syscall(2) rather than write(2) when possible in order to avoid\n\t * the possibility of memory allocation within libc.  This is necessary\n\t * on FreeBSD; most operating systems do not have this problem though.\n\t *\n\t * syscall() returns long or int, depending on platform, so capture the\n\t * result in the widest plausible type to avoid compiler warnings.\n\t */\n\tlong result = syscall(SYS_write, fd, buf, count);\n#else\n\tssize_t result = (ssize_t)write(fd, buf,\n#ifdef _WIN32\n\t    (unsigned int)\n#endif\n\t    count);\n#endif\n\treturn (ssize_t)result;\n}\n\nstatic inline ssize_t\nmalloc_read_fd(int fd, void *buf, size_t count) {\n#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_read)\n\tlong result = syscall(SYS_read, fd, buf, count);\n#else\n\tssize_t result = read(fd, buf,\n#ifdef _WIN32\n\t    (unsigned int)\n#endif\n\t    count);\n#endif\n\treturn (ssize_t)result;\n}\n\n#endif /* JEMALLOC_INTERNAL_MALLOC_IO_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/mutex.h",
    "content": "#ifndef JEMALLOC_INTERNAL_MUTEX_H\n#define JEMALLOC_INTERNAL_MUTEX_H\n\n#include \"jemalloc/internal/atomic.h\"\n#include \"jemalloc/internal/mutex_prof.h\"\n#include \"jemalloc/internal/tsd.h\"\n#include \"jemalloc/internal/witness.h\"\n\ntypedef enum {\n\t/* Can only acquire one mutex of a given witness rank at a time. */\n\tmalloc_mutex_rank_exclusive,\n\t/*\n\t * Can acquire multiple mutexes of the same witness rank, but in\n\t * address-ascending order only.\n\t */\n\tmalloc_mutex_address_ordered\n} malloc_mutex_lock_order_t;\n\ntypedef struct malloc_mutex_s malloc_mutex_t;\nstruct malloc_mutex_s {\n\tunion {\n\t\tstruct {\n\t\t\t/*\n\t\t\t * prof_data is defined first to reduce cacheline\n\t\t\t * bouncing: the data is not touched by the mutex holder\n\t\t\t * during unlocking, while might be modified by\n\t\t\t * contenders.  Having it before the mutex itself could\n\t\t\t * avoid prefetching a modified cacheline (for the\n\t\t\t * unlocking thread).\n\t\t\t */\n\t\t\tmutex_prof_data_t\tprof_data;\n#ifdef _WIN32\n#  if _WIN32_WINNT >= 0x0600\n\t\t\tSRWLOCK         \tlock;\n#  else\n\t\t\tCRITICAL_SECTION\tlock;\n#  endif\n#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))\n\t\t\tos_unfair_lock\t\tlock;\n#elif (defined(JEMALLOC_MUTEX_INIT_CB))\n\t\t\tpthread_mutex_t\t\tlock;\n\t\t\tmalloc_mutex_t\t\t*postponed_next;\n#else\n\t\t\tpthread_mutex_t\t\tlock;\n#endif\n\t\t\t/* \n\t\t\t * Hint flag to avoid exclusive cache line contention\n\t\t\t * during spin waiting\n\t\t\t */\n\t\t\tatomic_b_t\t\tlocked;\n\t\t};\n\t\t/*\n\t\t * We only touch witness when configured w/ debug.  However we\n\t\t * keep the field in a union when !debug so that we don't have\n\t\t * to pollute the code base with #ifdefs, while avoid paying the\n\t\t * memory cost.\n\t\t */\n#if !defined(JEMALLOC_DEBUG)\n\t\twitness_t\t\t\twitness;\n\t\tmalloc_mutex_lock_order_t\tlock_order;\n#endif\n\t};\n\n#if defined(JEMALLOC_DEBUG)\n\twitness_t\t\t\twitness;\n\tmalloc_mutex_lock_order_t\tlock_order;\n#endif\n};\n\n/*\n * Based on benchmark results, a fixed spin with this amount of retries works\n * well for our critical sections.\n */\n#define MALLOC_MUTEX_MAX_SPIN 250\n\n#ifdef _WIN32\n#  if _WIN32_WINNT >= 0x0600\n#    define MALLOC_MUTEX_LOCK(m)    AcquireSRWLockExclusive(&(m)->lock)\n#    define MALLOC_MUTEX_UNLOCK(m)  ReleaseSRWLockExclusive(&(m)->lock)\n#    define MALLOC_MUTEX_TRYLOCK(m) (!TryAcquireSRWLockExclusive(&(m)->lock))\n#  else\n#    define MALLOC_MUTEX_LOCK(m)    EnterCriticalSection(&(m)->lock)\n#    define MALLOC_MUTEX_UNLOCK(m)  LeaveCriticalSection(&(m)->lock)\n#    define MALLOC_MUTEX_TRYLOCK(m) (!TryEnterCriticalSection(&(m)->lock))\n#  endif\n#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))\n#    define MALLOC_MUTEX_LOCK(m)    os_unfair_lock_lock(&(m)->lock)\n#    define MALLOC_MUTEX_UNLOCK(m)  os_unfair_lock_unlock(&(m)->lock)\n#    define MALLOC_MUTEX_TRYLOCK(m) (!os_unfair_lock_trylock(&(m)->lock))\n#else\n#    define MALLOC_MUTEX_LOCK(m)    pthread_mutex_lock(&(m)->lock)\n#    define MALLOC_MUTEX_UNLOCK(m)  pthread_mutex_unlock(&(m)->lock)\n#    define MALLOC_MUTEX_TRYLOCK(m) (pthread_mutex_trylock(&(m)->lock) != 0)\n#endif\n\n#define LOCK_PROF_DATA_INITIALIZER\t\t\t\t\t\\\n    {NSTIME_ZERO_INITIALIZER, NSTIME_ZERO_INITIALIZER, 0, 0, 0,\t\t\\\n\t    ATOMIC_INIT(0), 0, NULL, 0}\n\n#ifdef _WIN32\n#  define MALLOC_MUTEX_INITIALIZER\n#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))\n#  if defined(JEMALLOC_DEBUG)\n#    define MALLOC_MUTEX_INITIALIZER\t\t\t\t\t\\\n  {{{LOCK_PROF_DATA_INITIALIZER, OS_UNFAIR_LOCK_INIT, ATOMIC_INIT(false)}}, \\\n         WITNESS_INITIALIZER(\"mutex\", WITNESS_RANK_OMIT), 0}\n#  else\n#    define MALLOC_MUTEX_INITIALIZER                      \\\n  {{{LOCK_PROF_DATA_INITIALIZER, OS_UNFAIR_LOCK_INIT, ATOMIC_INIT(false)}},  \\\n      WITNESS_INITIALIZER(\"mutex\", WITNESS_RANK_OMIT)}\n#  endif\n#elif (defined(JEMALLOC_MUTEX_INIT_CB))\n#  if (defined(JEMALLOC_DEBUG))\n#     define MALLOC_MUTEX_INITIALIZER\t\t\t\t\t\\\n      {{{LOCK_PROF_DATA_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, NULL, ATOMIC_INIT(false)}},\t\\\n           WITNESS_INITIALIZER(\"mutex\", WITNESS_RANK_OMIT), 0}\n#  else\n#     define MALLOC_MUTEX_INITIALIZER\t\t\t\t\t\\\n      {{{LOCK_PROF_DATA_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, NULL, ATOMIC_INIT(false)}},\t\\\n           WITNESS_INITIALIZER(\"mutex\", WITNESS_RANK_OMIT)}\n#  endif\n\n#else\n#    define MALLOC_MUTEX_TYPE PTHREAD_MUTEX_DEFAULT\n#  if defined(JEMALLOC_DEBUG)\n#    define MALLOC_MUTEX_INITIALIZER\t\t\t\t\t\\\n     {{{LOCK_PROF_DATA_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, ATOMIC_INIT(false)}}, \\\n           WITNESS_INITIALIZER(\"mutex\", WITNESS_RANK_OMIT), 0}\n#  else\n#    define MALLOC_MUTEX_INITIALIZER                          \\\n     {{{LOCK_PROF_DATA_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, ATOMIC_INIT(false)}},\t\\\n      WITNESS_INITIALIZER(\"mutex\", WITNESS_RANK_OMIT)}\n#  endif\n#endif\n\n#ifdef JEMALLOC_LAZY_LOCK\nextern bool isthreaded;\n#else\n#  undef isthreaded /* Undo private_namespace.h definition. */\n#  define isthreaded true\n#endif\n\nbool malloc_mutex_init(malloc_mutex_t *mutex, const char *name,\n    witness_rank_t rank, malloc_mutex_lock_order_t lock_order);\nvoid malloc_mutex_prefork(tsdn_t *tsdn, malloc_mutex_t *mutex);\nvoid malloc_mutex_postfork_parent(tsdn_t *tsdn, malloc_mutex_t *mutex);\nvoid malloc_mutex_postfork_child(tsdn_t *tsdn, malloc_mutex_t *mutex);\nbool malloc_mutex_boot(void);\nvoid malloc_mutex_prof_data_reset(tsdn_t *tsdn, malloc_mutex_t *mutex);\n\nvoid malloc_mutex_lock_slow(malloc_mutex_t *mutex);\n\nstatic inline void\nmalloc_mutex_lock_final(malloc_mutex_t *mutex) {\n\tMALLOC_MUTEX_LOCK(mutex);\n\tatomic_store_b(&mutex->locked, true, ATOMIC_RELAXED);\n}\n\nstatic inline bool\nmalloc_mutex_trylock_final(malloc_mutex_t *mutex) {\n\treturn MALLOC_MUTEX_TRYLOCK(mutex);\n}\n\nstatic inline void\nmutex_owner_stats_update(tsdn_t *tsdn, malloc_mutex_t *mutex) {\n\tif (config_stats) {\n\t\tmutex_prof_data_t *data = &mutex->prof_data;\n\t\tdata->n_lock_ops++;\n\t\tif (data->prev_owner != tsdn) {\n\t\t\tdata->prev_owner = tsdn;\n\t\t\tdata->n_owner_switches++;\n\t\t}\n\t}\n}\n\n/* Trylock: return false if the lock is successfully acquired. */\nstatic inline bool\nmalloc_mutex_trylock(tsdn_t *tsdn, malloc_mutex_t *mutex) {\n\twitness_assert_not_owner(tsdn_witness_tsdp_get(tsdn), &mutex->witness);\n\tif (isthreaded) {\n\t\tif (malloc_mutex_trylock_final(mutex)) {\n\t\t\tatomic_store_b(&mutex->locked, true, ATOMIC_RELAXED);\n\t\t\treturn true;\n\t\t}\n\t\tmutex_owner_stats_update(tsdn, mutex);\n\t}\n\twitness_lock(tsdn_witness_tsdp_get(tsdn), &mutex->witness);\n\n\treturn false;\n}\n\n/* Aggregate lock prof data. */\nstatic inline void\nmalloc_mutex_prof_merge(mutex_prof_data_t *sum, mutex_prof_data_t *data) {\n\tnstime_add(&sum->tot_wait_time, &data->tot_wait_time);\n\tif (nstime_compare(&sum->max_wait_time, &data->max_wait_time) < 0) {\n\t\tnstime_copy(&sum->max_wait_time, &data->max_wait_time);\n\t}\n\n\tsum->n_wait_times += data->n_wait_times;\n\tsum->n_spin_acquired += data->n_spin_acquired;\n\n\tif (sum->max_n_thds < data->max_n_thds) {\n\t\tsum->max_n_thds = data->max_n_thds;\n\t}\n\tuint32_t cur_n_waiting_thds = atomic_load_u32(&sum->n_waiting_thds,\n\t    ATOMIC_RELAXED);\n\tuint32_t new_n_waiting_thds = cur_n_waiting_thds + atomic_load_u32(\n\t    &data->n_waiting_thds, ATOMIC_RELAXED);\n\tatomic_store_u32(&sum->n_waiting_thds, new_n_waiting_thds,\n\t    ATOMIC_RELAXED);\n\tsum->n_owner_switches += data->n_owner_switches;\n\tsum->n_lock_ops += data->n_lock_ops;\n}\n\nstatic inline void\nmalloc_mutex_lock(tsdn_t *tsdn, malloc_mutex_t *mutex) {\n\twitness_assert_not_owner(tsdn_witness_tsdp_get(tsdn), &mutex->witness);\n\tif (isthreaded) {\n\t\tif (malloc_mutex_trylock_final(mutex)) {\n\t\t\tmalloc_mutex_lock_slow(mutex);\n\t\t\tatomic_store_b(&mutex->locked, true, ATOMIC_RELAXED);\n\t\t}\n\t\tmutex_owner_stats_update(tsdn, mutex);\n\t}\n\twitness_lock(tsdn_witness_tsdp_get(tsdn), &mutex->witness);\n}\n\nstatic inline void\nmalloc_mutex_unlock(tsdn_t *tsdn, malloc_mutex_t *mutex) {\n\tatomic_store_b(&mutex->locked, false, ATOMIC_RELAXED);\n\twitness_unlock(tsdn_witness_tsdp_get(tsdn), &mutex->witness);\n\tif (isthreaded) {\n\t\tMALLOC_MUTEX_UNLOCK(mutex);\n\t}\n}\n\nstatic inline void\nmalloc_mutex_assert_owner(tsdn_t *tsdn, malloc_mutex_t *mutex) {\n\twitness_assert_owner(tsdn_witness_tsdp_get(tsdn), &mutex->witness);\n}\n\nstatic inline void\nmalloc_mutex_assert_not_owner(tsdn_t *tsdn, malloc_mutex_t *mutex) {\n\twitness_assert_not_owner(tsdn_witness_tsdp_get(tsdn), &mutex->witness);\n}\n\n/* Copy the prof data from mutex for processing. */\nstatic inline void\nmalloc_mutex_prof_read(tsdn_t *tsdn, mutex_prof_data_t *data,\n    malloc_mutex_t *mutex) {\n\tmutex_prof_data_t *source = &mutex->prof_data;\n\t/* Can only read holding the mutex. */\n\tmalloc_mutex_assert_owner(tsdn, mutex);\n\n\t/*\n\t * Not *really* allowed (we shouldn't be doing non-atomic loads of\n\t * atomic data), but the mutex protection makes this safe, and writing\n\t * a member-for-member copy is tedious for this situation.\n\t */\n\t*data = *source;\n\t/* n_wait_thds is not reported (modified w/o locking). */\n\tatomic_store_u32(&data->n_waiting_thds, 0, ATOMIC_RELAXED);\n}\n\nstatic inline void\nmalloc_mutex_prof_accum(tsdn_t *tsdn, mutex_prof_data_t *data,\n    malloc_mutex_t *mutex) {\n\tmutex_prof_data_t *source = &mutex->prof_data;\n\t/* Can only read holding the mutex. */\n\tmalloc_mutex_assert_owner(tsdn, mutex);\n\n\tnstime_add(&data->tot_wait_time, &source->tot_wait_time);\n\tif (nstime_compare(&source->max_wait_time, &data->max_wait_time) > 0) {\n\t\tnstime_copy(&data->max_wait_time, &source->max_wait_time);\n\t}\n\tdata->n_wait_times += source->n_wait_times;\n\tdata->n_spin_acquired += source->n_spin_acquired;\n\tif (data->max_n_thds < source->max_n_thds) {\n\t\tdata->max_n_thds = source->max_n_thds;\n\t}\n\t/* n_wait_thds is not reported. */\n\tatomic_store_u32(&data->n_waiting_thds, 0, ATOMIC_RELAXED);\n\tdata->n_owner_switches += source->n_owner_switches;\n\tdata->n_lock_ops += source->n_lock_ops;\n}\n\n#endif /* JEMALLOC_INTERNAL_MUTEX_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/mutex_pool.h",
    "content": "#ifndef JEMALLOC_INTERNAL_MUTEX_POOL_H\n#define JEMALLOC_INTERNAL_MUTEX_POOL_H\n\n#include \"jemalloc/internal/hash.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/witness.h\"\n\n/* We do mod reductions by this value, so it should be kept a power of 2. */\n#define MUTEX_POOL_SIZE 256\n\ntypedef struct mutex_pool_s mutex_pool_t;\nstruct mutex_pool_s {\n\tmalloc_mutex_t mutexes[MUTEX_POOL_SIZE];\n};\n\nbool mutex_pool_init(mutex_pool_t *pool, const char *name, witness_rank_t rank);\n\n/* Internal helper - not meant to be called outside this module. */\nstatic inline malloc_mutex_t *\nmutex_pool_mutex(mutex_pool_t *pool, uintptr_t key) {\n\tsize_t hash_result[2];\n\thash(&key, sizeof(key), 0xd50dcc1b, hash_result);\n\treturn &pool->mutexes[hash_result[0] % MUTEX_POOL_SIZE];\n}\n\nstatic inline void\nmutex_pool_assert_not_held(tsdn_t *tsdn, mutex_pool_t *pool) {\n\tfor (int i = 0; i < MUTEX_POOL_SIZE; i++) {\n\t\tmalloc_mutex_assert_not_owner(tsdn, &pool->mutexes[i]);\n\t}\n}\n\n/*\n * Note that a mutex pool doesn't work exactly the way an embdedded mutex would.\n * You're not allowed to acquire mutexes in the pool one at a time.  You have to\n * acquire all the mutexes you'll need in a single function call, and then\n * release them all in a single function call.\n */\n\nstatic inline void\nmutex_pool_lock(tsdn_t *tsdn, mutex_pool_t *pool, uintptr_t key) {\n\tmutex_pool_assert_not_held(tsdn, pool);\n\n\tmalloc_mutex_t *mutex = mutex_pool_mutex(pool, key);\n\tmalloc_mutex_lock(tsdn, mutex);\n}\n\nstatic inline void\nmutex_pool_unlock(tsdn_t *tsdn, mutex_pool_t *pool, uintptr_t key) {\n\tmalloc_mutex_t *mutex = mutex_pool_mutex(pool, key);\n\tmalloc_mutex_unlock(tsdn, mutex);\n\n\tmutex_pool_assert_not_held(tsdn, pool);\n}\n\nstatic inline void\nmutex_pool_lock2(tsdn_t *tsdn, mutex_pool_t *pool, uintptr_t key1,\n    uintptr_t key2) {\n\tmutex_pool_assert_not_held(tsdn, pool);\n\n\tmalloc_mutex_t *mutex1 = mutex_pool_mutex(pool, key1);\n\tmalloc_mutex_t *mutex2 = mutex_pool_mutex(pool, key2);\n\tif ((uintptr_t)mutex1 < (uintptr_t)mutex2) {\n\t\tmalloc_mutex_lock(tsdn, mutex1);\n\t\tmalloc_mutex_lock(tsdn, mutex2);\n\t} else if ((uintptr_t)mutex1 == (uintptr_t)mutex2) {\n\t\tmalloc_mutex_lock(tsdn, mutex1);\n\t} else {\n\t\tmalloc_mutex_lock(tsdn, mutex2);\n\t\tmalloc_mutex_lock(tsdn, mutex1);\n\t}\n}\n\nstatic inline void\nmutex_pool_unlock2(tsdn_t *tsdn, mutex_pool_t *pool, uintptr_t key1,\n    uintptr_t key2) {\n\tmalloc_mutex_t *mutex1 = mutex_pool_mutex(pool, key1);\n\tmalloc_mutex_t *mutex2 = mutex_pool_mutex(pool, key2);\n\tif (mutex1 == mutex2) {\n\t\tmalloc_mutex_unlock(tsdn, mutex1);\n\t} else {\n\t\tmalloc_mutex_unlock(tsdn, mutex1);\n\t\tmalloc_mutex_unlock(tsdn, mutex2);\n\t}\n\n\tmutex_pool_assert_not_held(tsdn, pool);\n}\n\nstatic inline void\nmutex_pool_assert_owner(tsdn_t *tsdn, mutex_pool_t *pool, uintptr_t key) {\n\tmalloc_mutex_assert_owner(tsdn, mutex_pool_mutex(pool, key));\n}\n\n#endif /* JEMALLOC_INTERNAL_MUTEX_POOL_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/mutex_prof.h",
    "content": "#ifndef JEMALLOC_INTERNAL_MUTEX_PROF_H\n#define JEMALLOC_INTERNAL_MUTEX_PROF_H\n\n#include \"jemalloc/internal/atomic.h\"\n#include \"jemalloc/internal/nstime.h\"\n#include \"jemalloc/internal/tsd_types.h\"\n\n#define MUTEX_PROF_GLOBAL_MUTEXES\t\t\t\t\t\\\n    OP(background_thread)\t\t\t\t\t\t\\\n    OP(ctl)\t\t\t\t\t\t\t\t\\\n    OP(prof)\n\ntypedef enum {\n#define OP(mtx) global_prof_mutex_##mtx,\n\tMUTEX_PROF_GLOBAL_MUTEXES\n#undef OP\n\tmutex_prof_num_global_mutexes\n} mutex_prof_global_ind_t;\n\n#define MUTEX_PROF_ARENA_MUTEXES\t\t\t\t\t\\\n    OP(large)\t\t\t\t\t\t\t\t\\\n    OP(extent_avail)\t\t\t\t\t\t\t\\\n    OP(extents_dirty)\t\t\t\t\t\t\t\\\n    OP(extents_muzzy)\t\t\t\t\t\t\t\\\n    OP(extents_retained)\t\t\t\t\t\t\\\n    OP(decay_dirty)\t\t\t\t\t\t\t\\\n    OP(decay_muzzy)\t\t\t\t\t\t\t\\\n    OP(base)\t\t\t\t\t\t\t\t\\\n    OP(tcache_list)\n\ntypedef enum {\n#define OP(mtx) arena_prof_mutex_##mtx,\n\tMUTEX_PROF_ARENA_MUTEXES\n#undef OP\n\tmutex_prof_num_arena_mutexes\n} mutex_prof_arena_ind_t;\n\n/*\n * The forth parameter is a boolean value that is true for derived rate counters\n * and false for real ones.\n */\n#define MUTEX_PROF_UINT64_COUNTERS\t\t\t\t\t\\\n    OP(num_ops, uint64_t, \"n_lock_ops\", false, num_ops)\t\t\t\t\t\\\n    OP(num_ops_ps, uint64_t, \"(#/sec)\", true, num_ops)\t\t\t\t\\\n    OP(num_wait, uint64_t, \"n_waiting\", false, num_wait)\t\t\t\t\\\n    OP(num_wait_ps, uint64_t, \"(#/sec)\", true, num_wait)\t\t\t\t\\\n    OP(num_spin_acq, uint64_t, \"n_spin_acq\", false, num_spin_acq)\t\t\t\\\n    OP(num_spin_acq_ps, uint64_t, \"(#/sec)\", true, num_spin_acq)\t\t\t\\\n    OP(num_owner_switch, uint64_t, \"n_owner_switch\", false, num_owner_switch)\t\t\\\n    OP(num_owner_switch_ps, uint64_t, \"(#/sec)\", true, num_owner_switch)\t\\\n    OP(total_wait_time, uint64_t, \"total_wait_ns\", false, total_wait_time)\t\t\\\n    OP(total_wait_time_ps, uint64_t, \"(#/sec)\", true, total_wait_time)\t\t\\\n    OP(max_wait_time, uint64_t, \"max_wait_ns\", false, max_wait_time)\n\n#define MUTEX_PROF_UINT32_COUNTERS\t\t\t\t\t\\\n    OP(max_num_thds, uint32_t, \"max_n_thds\", false, max_num_thds)\n\n#define MUTEX_PROF_COUNTERS\t\t\t\t\t\t\\\n\t\tMUTEX_PROF_UINT64_COUNTERS\t\t\t\t\\\n\t\tMUTEX_PROF_UINT32_COUNTERS\n\n#define OP(counter, type, human, derived, base_counter) mutex_counter_##counter,\n\n#define COUNTER_ENUM(counter_list, t)\t\t\t\t\t\\\n\t\ttypedef enum {\t\t\t\t\t\t\\\n\t\t\tcounter_list\t\t\t\t\t\\\n\t\t\tmutex_prof_num_##t##_counters\t\t\t\\\n\t\t} mutex_prof_##t##_counter_ind_t;\n\nCOUNTER_ENUM(MUTEX_PROF_UINT64_COUNTERS, uint64_t)\nCOUNTER_ENUM(MUTEX_PROF_UINT32_COUNTERS, uint32_t)\n\n#undef COUNTER_ENUM\n#undef OP\n\ntypedef struct {\n\t/*\n\t * Counters touched on the slow path, i.e. when there is lock\n\t * contention.  We update them once we have the lock.\n\t */\n\t/* Total time (in nano seconds) spent waiting on this mutex. */\n\tnstime_t\t\ttot_wait_time;\n\t/* Max time (in nano seconds) spent on a single lock operation. */\n\tnstime_t\t\tmax_wait_time;\n\t/* # of times have to wait for this mutex (after spinning). */\n\tuint64_t\t\tn_wait_times;\n\t/* # of times acquired the mutex through local spinning. */\n\tuint64_t\t\tn_spin_acquired;\n\t/* Max # of threads waiting for the mutex at the same time. */\n\tuint32_t\t\tmax_n_thds;\n\t/* Current # of threads waiting on the lock.  Atomic synced. */\n\tatomic_u32_t\t\tn_waiting_thds;\n\n\t/*\n\t * Data touched on the fast path.  These are modified right after we\n\t * grab the lock, so it's placed closest to the end (i.e. right before\n\t * the lock) so that we have a higher chance of them being on the same\n\t * cacheline.\n\t */\n\t/* # of times the mutex holder is different than the previous one. */\n\tuint64_t\t\tn_owner_switches;\n\t/* Previous mutex holder, to facilitate n_owner_switches. */\n\ttsdn_t\t\t\t*prev_owner;\n\t/* # of lock() operations in total. */\n\tuint64_t\t\tn_lock_ops;\n} mutex_prof_data_t;\n\n#endif /* JEMALLOC_INTERNAL_MUTEX_PROF_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/nstime.h",
    "content": "#ifndef JEMALLOC_INTERNAL_NSTIME_H\n#define JEMALLOC_INTERNAL_NSTIME_H\n\n/* Maximum supported number of seconds (~584 years). */\n#define NSTIME_SEC_MAX KQU(18446744072)\n#define NSTIME_ZERO_INITIALIZER {0}\n\ntypedef struct {\n\tuint64_t ns;\n} nstime_t;\n\nvoid nstime_init(nstime_t *time, uint64_t ns);\nvoid nstime_init2(nstime_t *time, uint64_t sec, uint64_t nsec);\nuint64_t nstime_ns(const nstime_t *time);\nuint64_t nstime_sec(const nstime_t *time);\nuint64_t nstime_msec(const nstime_t *time);\nuint64_t nstime_nsec(const nstime_t *time);\nvoid nstime_copy(nstime_t *time, const nstime_t *source);\nint nstime_compare(const nstime_t *a, const nstime_t *b);\nvoid nstime_add(nstime_t *time, const nstime_t *addend);\nvoid nstime_iadd(nstime_t *time, uint64_t addend);\nvoid nstime_subtract(nstime_t *time, const nstime_t *subtrahend);\nvoid nstime_isubtract(nstime_t *time, uint64_t subtrahend);\nvoid nstime_imultiply(nstime_t *time, uint64_t multiplier);\nvoid nstime_idivide(nstime_t *time, uint64_t divisor);\nuint64_t nstime_divide(const nstime_t *time, const nstime_t *divisor);\n\ntypedef bool (nstime_monotonic_t)(void);\nextern nstime_monotonic_t *JET_MUTABLE nstime_monotonic;\n\ntypedef bool (nstime_update_t)(nstime_t *);\nextern nstime_update_t *JET_MUTABLE nstime_update;\n\n#endif /* JEMALLOC_INTERNAL_NSTIME_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/pages.h",
    "content": "#ifndef JEMALLOC_INTERNAL_PAGES_EXTERNS_H\n#define JEMALLOC_INTERNAL_PAGES_EXTERNS_H\n\n/* Page size.  LG_PAGE is determined by the configure script. */\n#ifdef PAGE_MASK\n#  undef PAGE_MASK\n#endif\n#define PAGE\t\t((size_t)(1U << LG_PAGE))\n#define PAGE_MASK\t((size_t)(PAGE - 1))\n/* Return the page base address for the page containing address a. */\n#define PAGE_ADDR2BASE(a)\t\t\t\t\t\t\\\n\t((void *)((uintptr_t)(a) & ~PAGE_MASK))\n/* Return the smallest pagesize multiple that is >= s. */\n#define PAGE_CEILING(s)\t\t\t\t\t\t\t\\\n\t(((s) + PAGE_MASK) & ~PAGE_MASK)\n\n/* Huge page size.  LG_HUGEPAGE is determined by the configure script. */\n#define HUGEPAGE\t((size_t)(1U << LG_HUGEPAGE))\n#define HUGEPAGE_MASK\t((size_t)(HUGEPAGE - 1))\n/* Return the huge page base address for the huge page containing address a. */\n#define HUGEPAGE_ADDR2BASE(a)\t\t\t\t\t\t\\\n\t((void *)((uintptr_t)(a) & ~HUGEPAGE_MASK))\n/* Return the smallest pagesize multiple that is >= s. */\n#define HUGEPAGE_CEILING(s)\t\t\t\t\t\t\\\n\t(((s) + HUGEPAGE_MASK) & ~HUGEPAGE_MASK)\n\n/* PAGES_CAN_PURGE_LAZY is defined if lazy purging is supported. */\n#if defined(_WIN32) || defined(JEMALLOC_PURGE_MADVISE_FREE)\n#  define PAGES_CAN_PURGE_LAZY\n#endif\n/*\n * PAGES_CAN_PURGE_FORCED is defined if forced purging is supported.\n *\n * The only supported way to hard-purge on Windows is to decommit and then\n * re-commit, but doing so is racy, and if re-commit fails it's a pain to\n * propagate the \"poisoned\" memory state.  Since we typically decommit as the\n * next step after purging on Windows anyway, there's no point in adding such\n * complexity.\n */\n#if !defined(_WIN32) && ((defined(JEMALLOC_PURGE_MADVISE_DONTNEED) && \\\n    defined(JEMALLOC_PURGE_MADVISE_DONTNEED_ZEROS)) || \\\n    defined(JEMALLOC_MAPS_COALESCE))\n#  define PAGES_CAN_PURGE_FORCED\n#endif\n\nstatic const bool pages_can_purge_lazy =\n#ifdef PAGES_CAN_PURGE_LAZY\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool pages_can_purge_forced =\n#ifdef PAGES_CAN_PURGE_FORCED\n    true\n#else\n    false\n#endif\n    ;\n\ntypedef enum {\n\tthp_mode_default       = 0, /* Do not change hugepage settings. */\n\tthp_mode_always        = 1, /* Always set MADV_HUGEPAGE. */\n\tthp_mode_never         = 2, /* Always set MADV_NOHUGEPAGE. */\n\n\tthp_mode_names_limit   = 3, /* Used for option processing. */\n\tthp_mode_not_supported = 3  /* No THP support detected. */\n} thp_mode_t;\n\n#define THP_MODE_DEFAULT thp_mode_default\nextern thp_mode_t opt_thp;\nextern thp_mode_t init_system_thp_mode; /* Initial system wide state. */\nextern const char *thp_mode_names[];\n\nvoid *pages_map(void *addr, size_t size, size_t alignment, bool *commit);\nvoid pages_unmap(void *addr, size_t size);\nbool pages_commit(void *addr, size_t size);\nbool pages_decommit(void *addr, size_t size);\nbool pages_purge_lazy(void *addr, size_t size);\nbool pages_purge_forced(void *addr, size_t size);\nbool pages_huge(void *addr, size_t size);\nbool pages_nohuge(void *addr, size_t size);\nbool pages_dontdump(void *addr, size_t size);\nbool pages_dodump(void *addr, size_t size);\nbool pages_boot(void);\nvoid pages_set_thp_state (void *ptr, size_t size);\n\n#endif /* JEMALLOC_INTERNAL_PAGES_EXTERNS_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/ph.h",
    "content": "/*\n * A Pairing Heap implementation.\n *\n * \"The Pairing Heap: A New Form of Self-Adjusting Heap\"\n * https://www.cs.cmu.edu/~sleator/papers/pairing-heaps.pdf\n *\n * With auxiliary twopass list, described in a follow on paper.\n *\n * \"Pairing Heaps: Experiments and Analysis\"\n * http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.106.2988&rep=rep1&type=pdf\n *\n *******************************************************************************\n */\n\n#ifndef PH_H_\n#define PH_H_\n\n/* Node structure. */\n#define phn(a_type)\t\t\t\t\t\t\t\\\nstruct {\t\t\t\t\t\t\t\t\\\n\ta_type\t*phn_prev;\t\t\t\t\t\t\\\n\ta_type\t*phn_next;\t\t\t\t\t\t\\\n\ta_type\t*phn_lchild;\t\t\t\t\t\t\\\n}\n\n/* Root structure. */\n#define ph(a_type)\t\t\t\t\t\t\t\\\nstruct {\t\t\t\t\t\t\t\t\\\n\ta_type\t*ph_root;\t\t\t\t\t\t\\\n}\n\n/* Internal utility macros. */\n#define phn_lchild_get(a_type, a_field, a_phn)\t\t\t\t\\\n\t(a_phn->a_field.phn_lchild)\n#define phn_lchild_set(a_type, a_field, a_phn, a_lchild) do {\t\t\\\n\ta_phn->a_field.phn_lchild = a_lchild;\t\t\t\t\\\n} while (0)\n\n#define phn_next_get(a_type, a_field, a_phn)\t\t\t\t\\\n\t(a_phn->a_field.phn_next)\n#define phn_prev_set(a_type, a_field, a_phn, a_prev) do {\t\t\\\n\ta_phn->a_field.phn_prev = a_prev;\t\t\t\t\\\n} while (0)\n\n#define phn_prev_get(a_type, a_field, a_phn)\t\t\t\t\\\n\t(a_phn->a_field.phn_prev)\n#define phn_next_set(a_type, a_field, a_phn, a_next) do {\t\t\\\n\ta_phn->a_field.phn_next = a_next;\t\t\t\t\\\n} while (0)\n\n#define phn_merge_ordered(a_type, a_field, a_phn0, a_phn1, a_cmp) do {\t\\\n\ta_type *phn0child;\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tassert(a_phn0 != NULL);\t\t\t\t\t\t\\\n\tassert(a_phn1 != NULL);\t\t\t\t\t\t\\\n\tassert(a_cmp(a_phn0, a_phn1) <= 0);\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tphn_prev_set(a_type, a_field, a_phn1, a_phn0);\t\t\t\\\n\tphn0child = phn_lchild_get(a_type, a_field, a_phn0);\t\t\\\n\tphn_next_set(a_type, a_field, a_phn1, phn0child);\t\t\\\n\tif (phn0child != NULL) {\t\t\t\t\t\\\n\t\tphn_prev_set(a_type, a_field, phn0child, a_phn1);\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tphn_lchild_set(a_type, a_field, a_phn0, a_phn1);\t\t\\\n} while (0)\n\n#define phn_merge(a_type, a_field, a_phn0, a_phn1, a_cmp, r_phn) do {\t\\\n\tif (a_phn0 == NULL) {\t\t\t\t\t\t\\\n\t\tr_phn = a_phn1;\t\t\t\t\t\t\\\n\t} else if (a_phn1 == NULL) {\t\t\t\t\t\\\n\t\tr_phn = a_phn0;\t\t\t\t\t\t\\\n\t} else if (a_cmp(a_phn0, a_phn1) < 0) {\t\t\t\t\\\n\t\tphn_merge_ordered(a_type, a_field, a_phn0, a_phn1,\t\\\n\t\t    a_cmp);\t\t\t\t\t\t\\\n\t\tr_phn = a_phn0;\t\t\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t\tphn_merge_ordered(a_type, a_field, a_phn1, a_phn0,\t\\\n\t\t    a_cmp);\t\t\t\t\t\t\\\n\t\tr_phn = a_phn1;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define ph_merge_siblings(a_type, a_field, a_phn, a_cmp, r_phn) do {\t\\\n\ta_type *head = NULL;\t\t\t\t\t\t\\\n\ta_type *tail = NULL;\t\t\t\t\t\t\\\n\ta_type *phn0 = a_phn;\t\t\t\t\t\t\\\n\ta_type *phn1 = phn_next_get(a_type, a_field, phn0);\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t/*\t\t\t\t\t\t\t\t\\\n\t * Multipass merge, wherein the first two elements of a FIFO\t\\\n\t * are repeatedly merged, and each result is appended to the\t\\\n\t * singly linked FIFO, until the FIFO contains only a single\t\\\n\t * element.  We start with a sibling list but no reference to\t\\\n\t * its tail, so we do a single pass over the sibling list to\t\\\n\t * populate the FIFO.\t\t\t\t\t\t\\\n\t */\t\t\t\t\t\t\t\t\\\n\tif (phn1 != NULL) {\t\t\t\t\t\t\\\n\t\ta_type *phnrest = phn_next_get(a_type, a_field, phn1);\t\\\n\t\tif (phnrest != NULL) {\t\t\t\t\t\\\n\t\t\tphn_prev_set(a_type, a_field, phnrest, NULL);\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\tphn_prev_set(a_type, a_field, phn0, NULL);\t\t\\\n\t\tphn_next_set(a_type, a_field, phn0, NULL);\t\t\\\n\t\tphn_prev_set(a_type, a_field, phn1, NULL);\t\t\\\n\t\tphn_next_set(a_type, a_field, phn1, NULL);\t\t\\\n\t\tphn_merge(a_type, a_field, phn0, phn1, a_cmp, phn0);\t\\\n\t\thead = tail = phn0;\t\t\t\t\t\\\n\t\tphn0 = phnrest;\t\t\t\t\t\t\\\n\t\twhile (phn0 != NULL) {\t\t\t\t\t\\\n\t\t\tphn1 = phn_next_get(a_type, a_field, phn0);\t\\\n\t\t\tif (phn1 != NULL) {\t\t\t\t\\\n\t\t\t\tphnrest = phn_next_get(a_type, a_field,\t\\\n\t\t\t\t    phn1);\t\t\t\t\\\n\t\t\t\tif (phnrest != NULL) {\t\t\t\\\n\t\t\t\t\tphn_prev_set(a_type, a_field,\t\\\n\t\t\t\t\t    phnrest, NULL);\t\t\\\n\t\t\t\t}\t\t\t\t\t\\\n\t\t\t\tphn_prev_set(a_type, a_field, phn0,\t\\\n\t\t\t\t    NULL);\t\t\t\t\\\n\t\t\t\tphn_next_set(a_type, a_field, phn0,\t\\\n\t\t\t\t    NULL);\t\t\t\t\\\n\t\t\t\tphn_prev_set(a_type, a_field, phn1,\t\\\n\t\t\t\t    NULL);\t\t\t\t\\\n\t\t\t\tphn_next_set(a_type, a_field, phn1,\t\\\n\t\t\t\t    NULL);\t\t\t\t\\\n\t\t\t\tphn_merge(a_type, a_field, phn0, phn1,\t\\\n\t\t\t\t    a_cmp, phn0);\t\t\t\\\n\t\t\t\tphn_next_set(a_type, a_field, tail,\t\\\n\t\t\t\t    phn0);\t\t\t\t\\\n\t\t\t\ttail = phn0;\t\t\t\t\\\n\t\t\t\tphn0 = phnrest;\t\t\t\t\\\n\t\t\t} else {\t\t\t\t\t\\\n\t\t\t\tphn_next_set(a_type, a_field, tail,\t\\\n\t\t\t\t    phn0);\t\t\t\t\\\n\t\t\t\ttail = phn0;\t\t\t\t\\\n\t\t\t\tphn0 = NULL;\t\t\t\t\\\n\t\t\t}\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\tphn0 = head;\t\t\t\t\t\t\\\n\t\tphn1 = phn_next_get(a_type, a_field, phn0);\t\t\\\n\t\tif (phn1 != NULL) {\t\t\t\t\t\\\n\t\t\twhile (true) {\t\t\t\t\t\\\n\t\t\t\thead = phn_next_get(a_type, a_field,\t\\\n\t\t\t\t    phn1);\t\t\t\t\\\n\t\t\t\tassert(phn_prev_get(a_type, a_field,\t\\\n\t\t\t\t    phn0) == NULL);\t\t\t\\\n\t\t\t\tphn_next_set(a_type, a_field, phn0,\t\\\n\t\t\t\t    NULL);\t\t\t\t\\\n\t\t\t\tassert(phn_prev_get(a_type, a_field,\t\\\n\t\t\t\t    phn1) == NULL);\t\t\t\\\n\t\t\t\tphn_next_set(a_type, a_field, phn1,\t\\\n\t\t\t\t    NULL);\t\t\t\t\\\n\t\t\t\tphn_merge(a_type, a_field, phn0, phn1,\t\\\n\t\t\t\t    a_cmp, phn0);\t\t\t\\\n\t\t\t\tif (head == NULL) {\t\t\t\\\n\t\t\t\t\tbreak;\t\t\t\t\\\n\t\t\t\t}\t\t\t\t\t\\\n\t\t\t\tphn_next_set(a_type, a_field, tail,\t\\\n\t\t\t\t    phn0);\t\t\t\t\\\n\t\t\t\ttail = phn0;\t\t\t\t\\\n\t\t\t\tphn0 = head;\t\t\t\t\\\n\t\t\t\tphn1 = phn_next_get(a_type, a_field,\t\\\n\t\t\t\t    phn0);\t\t\t\t\\\n\t\t\t}\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tr_phn = phn0;\t\t\t\t\t\t\t\\\n} while (0)\n\n#define ph_merge_aux(a_type, a_field, a_ph, a_cmp) do {\t\t\t\\\n\ta_type *phn = phn_next_get(a_type, a_field, a_ph->ph_root);\t\\\n\tif (phn != NULL) {\t\t\t\t\t\t\\\n\t\tphn_prev_set(a_type, a_field, a_ph->ph_root, NULL);\t\\\n\t\tphn_next_set(a_type, a_field, a_ph->ph_root, NULL);\t\\\n\t\tphn_prev_set(a_type, a_field, phn, NULL);\t\t\\\n\t\tph_merge_siblings(a_type, a_field, phn, a_cmp, phn);\t\\\n\t\tassert(phn_next_get(a_type, a_field, phn) == NULL);\t\\\n\t\tphn_merge(a_type, a_field, a_ph->ph_root, phn, a_cmp,\t\\\n\t\t    a_ph->ph_root);\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define ph_merge_children(a_type, a_field, a_phn, a_cmp, r_phn) do {\t\\\n\ta_type *lchild = phn_lchild_get(a_type, a_field, a_phn);\t\\\n\tif (lchild == NULL) {\t\t\t\t\t\t\\\n\t\tr_phn = NULL;\t\t\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t\tph_merge_siblings(a_type, a_field, lchild, a_cmp,\t\\\n\t\t    r_phn);\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n/*\n * The ph_proto() macro generates function prototypes that correspond to the\n * functions generated by an equivalently parameterized call to ph_gen().\n */\n#define ph_proto(a_attr, a_prefix, a_ph_type, a_type)\t\t\t\\\na_attr void\ta_prefix##new(a_ph_type *ph);\t\t\t\t\\\na_attr bool\ta_prefix##empty(a_ph_type *ph);\t\t\t\t\\\na_attr a_type\t*a_prefix##first(a_ph_type *ph);\t\t\t\\\na_attr a_type\t*a_prefix##any(a_ph_type *ph);\t\t\t\t\\\na_attr void\ta_prefix##insert(a_ph_type *ph, a_type *phn);\t\t\\\na_attr a_type\t*a_prefix##remove_first(a_ph_type *ph);\t\t\t\\\na_attr a_type\t*a_prefix##remove_any(a_ph_type *ph);\t\t\t\\\na_attr void\ta_prefix##remove(a_ph_type *ph, a_type *phn);\n\n/*\n * The ph_gen() macro generates a type-specific pairing heap implementation,\n * based on the above cpp macros.\n */\n#define ph_gen(a_attr, a_prefix, a_ph_type, a_type, a_field, a_cmp)\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##new(a_ph_type *ph) {\t\t\t\t\t\t\\\n\tmemset(ph, 0, sizeof(ph(a_type)));\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr bool\t\t\t\t\t\t\t\t\\\na_prefix##empty(a_ph_type *ph) {\t\t\t\t\t\\\n\treturn (ph->ph_root == NULL);\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##first(a_ph_type *ph) {\t\t\t\t\t\\\n\tif (ph->ph_root == NULL) {\t\t\t\t\t\\\n\t\treturn NULL;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tph_merge_aux(a_type, a_field, ph, a_cmp);\t\t\t\\\n\treturn ph->ph_root;\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##any(a_ph_type *ph) {\t\t\t\t\t\t\\\n\tif (ph->ph_root == NULL) {\t\t\t\t\t\\\n\t\treturn NULL;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\ta_type *aux = phn_next_get(a_type, a_field, ph->ph_root);\t\\\n\tif (aux != NULL) {\t\t\t\t\t\t\\\n\t\treturn aux;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\treturn ph->ph_root;\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##insert(a_ph_type *ph, a_type *phn) {\t\t\t\t\\\n\tmemset(&phn->a_field, 0, sizeof(phn(a_type)));\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t/*\t\t\t\t\t\t\t\t\\\n\t * Treat the root as an aux list during insertion, and lazily\t\\\n\t * merge during a_prefix##remove_first().  For elements that\t\\\n\t * are inserted, then removed via a_prefix##remove() before the\t\\\n\t * aux list is ever processed, this makes insert/remove\t\t\\\n\t * constant-time, whereas eager merging would make insert\t\\\n\t * O(log n).\t\t\t\t\t\t\t\\\n\t */\t\t\t\t\t\t\t\t\\\n\tif (ph->ph_root == NULL) {\t\t\t\t\t\\\n\t\tph->ph_root = phn;\t\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t\tphn_next_set(a_type, a_field, phn, phn_next_get(a_type,\t\\\n\t\t    a_field, ph->ph_root));\t\t\t\t\\\n\t\tif (phn_next_get(a_type, a_field, ph->ph_root) !=\t\\\n\t\t    NULL) {\t\t\t\t\t\t\\\n\t\t\tphn_prev_set(a_type, a_field,\t\t\t\\\n\t\t\t    phn_next_get(a_type, a_field, ph->ph_root),\t\\\n\t\t\t    phn);\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\tphn_prev_set(a_type, a_field, phn, ph->ph_root);\t\\\n\t\tphn_next_set(a_type, a_field, ph->ph_root, phn);\t\\\n\t}\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##remove_first(a_ph_type *ph) {\t\t\t\t\t\\\n\ta_type *ret;\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tif (ph->ph_root == NULL) {\t\t\t\t\t\\\n\t\treturn NULL;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tph_merge_aux(a_type, a_field, ph, a_cmp);\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tret = ph->ph_root;\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tph_merge_children(a_type, a_field, ph->ph_root, a_cmp,\t\t\\\n\t    ph->ph_root);\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\treturn ret;\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##remove_any(a_ph_type *ph) {\t\t\t\t\t\\\n\t/*\t\t\t\t\t\t\t\t\\\n\t * Remove the most recently inserted aux list element, or the\t\\\n\t * root if the aux list is empty.  This has the effect of\t\\\n\t * behaving as a LIFO (and insertion/removal is therefore\t\\\n\t * constant-time) if a_prefix##[remove_]first() are never\t\\\n\t * called.\t\t\t\t\t\t\t\\\n\t */\t\t\t\t\t\t\t\t\\\n\tif (ph->ph_root == NULL) {\t\t\t\t\t\\\n\t\treturn NULL;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\ta_type *ret = phn_next_get(a_type, a_field, ph->ph_root);\t\\\n\tif (ret != NULL) {\t\t\t\t\t\t\\\n\t\ta_type *aux = phn_next_get(a_type, a_field, ret);\t\\\n\t\tphn_next_set(a_type, a_field, ph->ph_root, aux);\t\\\n\t\tif (aux != NULL) {\t\t\t\t\t\\\n\t\t\tphn_prev_set(a_type, a_field, aux,\t\t\\\n\t\t\t    ph->ph_root);\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\treturn ret;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tret = ph->ph_root;\t\t\t\t\t\t\\\n\tph_merge_children(a_type, a_field, ph->ph_root, a_cmp,\t\t\\\n\t    ph->ph_root);\t\t\t\t\t\t\\\n\treturn ret;\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##remove(a_ph_type *ph, a_type *phn) {\t\t\t\t\\\n\ta_type *replace, *parent;\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tif (ph->ph_root == phn) {\t\t\t\t\t\\\n\t\t/*\t\t\t\t\t\t\t\\\n\t\t * We can delete from aux list without merging it, but\t\\\n\t\t * we need to merge if we are dealing with the root\t\\\n\t\t * node and it has children.\t\t\t\t\\\n\t\t */\t\t\t\t\t\t\t\\\n\t\tif (phn_lchild_get(a_type, a_field, phn) == NULL) {\t\\\n\t\t\tph->ph_root = phn_next_get(a_type, a_field,\t\\\n\t\t\t    phn);\t\t\t\t\t\\\n\t\t\tif (ph->ph_root != NULL) {\t\t\t\\\n\t\t\t\tphn_prev_set(a_type, a_field,\t\t\\\n\t\t\t\t    ph->ph_root, NULL);\t\t\t\\\n\t\t\t}\t\t\t\t\t\t\\\n\t\t\treturn;\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\tph_merge_aux(a_type, a_field, ph, a_cmp);\t\t\\\n\t\tif (ph->ph_root == phn) {\t\t\t\t\\\n\t\t\tph_merge_children(a_type, a_field, ph->ph_root,\t\\\n\t\t\t    a_cmp, ph->ph_root);\t\t\t\\\n\t\t\treturn;\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t/* Get parent (if phn is leftmost child) before mutating. */\t\\\n\tif ((parent = phn_prev_get(a_type, a_field, phn)) != NULL) {\t\\\n\t\tif (phn_lchild_get(a_type, a_field, parent) != phn) {\t\\\n\t\t\tparent = NULL;\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\t/* Find a possible replacement node, and link to parent. */\t\\\n\tph_merge_children(a_type, a_field, phn, a_cmp, replace);\t\\\n\t/* Set next/prev for sibling linked list. */\t\t\t\\\n\tif (replace != NULL) {\t\t\t\t\t\t\\\n\t\tif (parent != NULL) {\t\t\t\t\t\\\n\t\t\tphn_prev_set(a_type, a_field, replace, parent);\t\\\n\t\t\tphn_lchild_set(a_type, a_field, parent,\t\t\\\n\t\t\t    replace);\t\t\t\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t\tphn_prev_set(a_type, a_field, replace,\t\t\\\n\t\t\t    phn_prev_get(a_type, a_field, phn));\t\\\n\t\t\tif (phn_prev_get(a_type, a_field, phn) !=\t\\\n\t\t\t    NULL) {\t\t\t\t\t\\\n\t\t\t\tphn_next_set(a_type, a_field,\t\t\\\n\t\t\t\t    phn_prev_get(a_type, a_field, phn),\t\\\n\t\t\t\t    replace);\t\t\t\t\\\n\t\t\t}\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\tphn_next_set(a_type, a_field, replace,\t\t\t\\\n\t\t    phn_next_get(a_type, a_field, phn));\t\t\\\n\t\tif (phn_next_get(a_type, a_field, phn) != NULL) {\t\\\n\t\t\tphn_prev_set(a_type, a_field,\t\t\t\\\n\t\t\t    phn_next_get(a_type, a_field, phn),\t\t\\\n\t\t\t    replace);\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t\tif (parent != NULL) {\t\t\t\t\t\\\n\t\t\ta_type *next = phn_next_get(a_type, a_field,\t\\\n\t\t\t    phn);\t\t\t\t\t\\\n\t\t\tphn_lchild_set(a_type, a_field, parent, next);\t\\\n\t\t\tif (next != NULL) {\t\t\t\t\\\n\t\t\t\tphn_prev_set(a_type, a_field, next,\t\\\n\t\t\t\t    parent);\t\t\t\t\\\n\t\t\t}\t\t\t\t\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t\tassert(phn_prev_get(a_type, a_field, phn) !=\t\\\n\t\t\t    NULL);\t\t\t\t\t\\\n\t\t\tphn_next_set(a_type, a_field,\t\t\t\\\n\t\t\t    phn_prev_get(a_type, a_field, phn),\t\t\\\n\t\t\t    phn_next_get(a_type, a_field, phn));\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\tif (phn_next_get(a_type, a_field, phn) != NULL) {\t\\\n\t\t\tphn_prev_set(a_type, a_field,\t\t\t\\\n\t\t\t    phn_next_get(a_type, a_field, phn),\t\t\\\n\t\t\t    phn_prev_get(a_type, a_field, phn));\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n}\n\n#endif /* PH_H_ */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/private_namespace.sh",
    "content": "#!/bin/sh\n\nfor symbol in `cat \"$@\"` ; do\n  echo \"#define ${symbol} JEMALLOC_N(${symbol})\"\ndone\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/private_symbols.sh",
    "content": "#!/bin/sh\n#\n# Generate private_symbols[_jet].awk.\n#\n# Usage: private_symbols.sh <sym_prefix> <sym>*\n#\n# <sym_prefix> is typically \"\" or \"_\".\n\nsym_prefix=$1\nshift\n\ncat <<EOF\n#!/usr/bin/env awk -f\n\nBEGIN {\n  sym_prefix = \"${sym_prefix}\"\n  split(\"\\\\\nEOF\n\nfor public_sym in \"$@\" ; do\n  cat <<EOF\n        ${sym_prefix}${public_sym} \\\\\nEOF\ndone\n\ncat <<\"EOF\"\n        \", exported_symbol_names)\n  # Store exported symbol names as keys in exported_symbols.\n  for (i in exported_symbol_names) {\n    exported_symbols[exported_symbol_names[i]] = 1\n  }\n}\n\n# Process 'nm -a <c_source.o>' output.\n#\n# Handle lines like:\n#   0000000000000008 D opt_junk\n#   0000000000007574 T malloc_initialized\n(NF == 3 && $2 ~ /^[ABCDGRSTVW]$/ && !($3 in exported_symbols) && $3 ~ /^[A-Za-z0-9_]+$/) {\n  print substr($3, 1+length(sym_prefix), length($3)-length(sym_prefix))\n}\n\n# Process 'dumpbin /SYMBOLS <c_source.obj>' output.\n#\n# Handle lines like:\n#   353 00008098 SECT4  notype       External     | opt_junk\n#   3F1 00000000 SECT7  notype ()    External     | malloc_initialized\n($3 ~ /^SECT[0-9]+/ && $(NF-2) == \"External\" && !($NF in exported_symbols)) {\n  print $NF\n}\nEOF\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/prng.h",
    "content": "#ifndef JEMALLOC_INTERNAL_PRNG_H\n#define JEMALLOC_INTERNAL_PRNG_H\n\n#include \"jemalloc/internal/atomic.h\"\n#include \"jemalloc/internal/bit_util.h\"\n\n/*\n * Simple linear congruential pseudo-random number generator:\n *\n *   prng(y) = (a*x + c) % m\n *\n * where the following constants ensure maximal period:\n *\n *   a == Odd number (relatively prime to 2^n), and (a-1) is a multiple of 4.\n *   c == Odd number (relatively prime to 2^n).\n *   m == 2^32\n *\n * See Knuth's TAOCP 3rd Ed., Vol. 2, pg. 17 for details on these constraints.\n *\n * This choice of m has the disadvantage that the quality of the bits is\n * proportional to bit position.  For example, the lowest bit has a cycle of 2,\n * the next has a cycle of 4, etc.  For this reason, we prefer to use the upper\n * bits.\n */\n\n/******************************************************************************/\n/* INTERNAL DEFINITIONS -- IGNORE */\n/******************************************************************************/\n#define PRNG_A_32\tUINT32_C(1103515241)\n#define PRNG_C_32\tUINT32_C(12347)\n\n#define PRNG_A_64\tUINT64_C(6364136223846793005)\n#define PRNG_C_64\tUINT64_C(1442695040888963407)\n\nJEMALLOC_ALWAYS_INLINE uint32_t\nprng_state_next_u32(uint32_t state) {\n\treturn (state * PRNG_A_32) + PRNG_C_32;\n}\n\nJEMALLOC_ALWAYS_INLINE uint64_t\nprng_state_next_u64(uint64_t state) {\n\treturn (state * PRNG_A_64) + PRNG_C_64;\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\nprng_state_next_zu(size_t state) {\n#if LG_SIZEOF_PTR == 2\n\treturn (state * PRNG_A_32) + PRNG_C_32;\n#elif LG_SIZEOF_PTR == 3\n\treturn (state * PRNG_A_64) + PRNG_C_64;\n#else\n#error Unsupported pointer size\n#endif\n}\n\n/******************************************************************************/\n/* BEGIN PUBLIC API */\n/******************************************************************************/\n\n/*\n * The prng_lg_range functions give a uniform int in the half-open range [0,\n * 2**lg_range).  If atomic is true, they do so safely from multiple threads.\n * Multithreaded 64-bit prngs aren't supported.\n */\n\nJEMALLOC_ALWAYS_INLINE uint32_t\nprng_lg_range_u32(atomic_u32_t *state, unsigned lg_range, bool atomic) {\n\tuint32_t ret, state0, state1;\n\n\tassert(lg_range > 0);\n\tassert(lg_range <= 32);\n\n\tstate0 = atomic_load_u32(state, ATOMIC_RELAXED);\n\n\tif (atomic) {\n\t\tdo {\n\t\t\tstate1 = prng_state_next_u32(state0);\n\t\t} while (!atomic_compare_exchange_weak_u32(state, &state0,\n\t\t    state1, ATOMIC_RELAXED, ATOMIC_RELAXED));\n\t} else {\n\t\tstate1 = prng_state_next_u32(state0);\n\t\tatomic_store_u32(state, state1, ATOMIC_RELAXED);\n\t}\n\tret = state1 >> (32 - lg_range);\n\n\treturn ret;\n}\n\nJEMALLOC_ALWAYS_INLINE uint64_t\nprng_lg_range_u64(uint64_t *state, unsigned lg_range) {\n\tuint64_t ret, state1;\n\n\tassert(lg_range > 0);\n\tassert(lg_range <= 64);\n\n\tstate1 = prng_state_next_u64(*state);\n\t*state = state1;\n\tret = state1 >> (64 - lg_range);\n\n\treturn ret;\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\nprng_lg_range_zu(atomic_zu_t *state, unsigned lg_range, bool atomic) {\n\tsize_t ret, state0, state1;\n\n\tassert(lg_range > 0);\n\tassert(lg_range <= ZU(1) << (3 + LG_SIZEOF_PTR));\n\n\tstate0 = atomic_load_zu(state, ATOMIC_RELAXED);\n\n\tif (atomic) {\n\t\tdo {\n\t\t\tstate1 = prng_state_next_zu(state0);\n\t\t} while (atomic_compare_exchange_weak_zu(state, &state0,\n\t\t    state1, ATOMIC_RELAXED, ATOMIC_RELAXED));\n\t} else {\n\t\tstate1 = prng_state_next_zu(state0);\n\t\tatomic_store_zu(state, state1, ATOMIC_RELAXED);\n\t}\n\tret = state1 >> ((ZU(1) << (3 + LG_SIZEOF_PTR)) - lg_range);\n\n\treturn ret;\n}\n\n/*\n * The prng_range functions behave like the prng_lg_range, but return a result\n * in [0, range) instead of [0, 2**lg_range).\n */\n\nJEMALLOC_ALWAYS_INLINE uint32_t\nprng_range_u32(atomic_u32_t *state, uint32_t range, bool atomic) {\n\tuint32_t ret;\n\tunsigned lg_range;\n\n\tassert(range > 1);\n\n\t/* Compute the ceiling of lg(range). */\n\tlg_range = ffs_u32(pow2_ceil_u32(range)) - 1;\n\n\t/* Generate a result in [0..range) via repeated trial. */\n\tdo {\n\t\tret = prng_lg_range_u32(state, lg_range, atomic);\n\t} while (ret >= range);\n\n\treturn ret;\n}\n\nJEMALLOC_ALWAYS_INLINE uint64_t\nprng_range_u64(uint64_t *state, uint64_t range) {\n\tuint64_t ret;\n\tunsigned lg_range;\n\n\tassert(range > 1);\n\n\t/* Compute the ceiling of lg(range). */\n\tlg_range = ffs_u64(pow2_ceil_u64(range)) - 1;\n\n\t/* Generate a result in [0..range) via repeated trial. */\n\tdo {\n\t\tret = prng_lg_range_u64(state, lg_range);\n\t} while (ret >= range);\n\n\treturn ret;\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\nprng_range_zu(atomic_zu_t *state, size_t range, bool atomic) {\n\tsize_t ret;\n\tunsigned lg_range;\n\n\tassert(range > 1);\n\n\t/* Compute the ceiling of lg(range). */\n\tlg_range = ffs_u64(pow2_ceil_u64(range)) - 1;\n\n\t/* Generate a result in [0..range) via repeated trial. */\n\tdo {\n\t\tret = prng_lg_range_zu(state, lg_range, atomic);\n\t} while (ret >= range);\n\n\treturn ret;\n}\n\n#endif /* JEMALLOC_INTERNAL_PRNG_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/prof_externs.h",
    "content": "#ifndef JEMALLOC_INTERNAL_PROF_EXTERNS_H\n#define JEMALLOC_INTERNAL_PROF_EXTERNS_H\n\n#include \"jemalloc/internal/mutex.h\"\n\nextern malloc_mutex_t\tbt2gctx_mtx;\n\nextern bool\topt_prof;\nextern bool\topt_prof_active;\nextern bool\topt_prof_thread_active_init;\nextern size_t\topt_lg_prof_sample;   /* Mean bytes between samples. */\nextern ssize_t\topt_lg_prof_interval; /* lg(prof_interval). */\nextern bool\topt_prof_gdump;       /* High-water memory dumping. */\nextern bool\topt_prof_final;       /* Final profile dumping. */\nextern bool\topt_prof_leak;        /* Dump leak summary at exit. */\nextern bool\topt_prof_accum;       /* Report cumulative bytes. */\nextern bool\topt_prof_log;\t      /* Turn logging on at boot. */\nextern char\topt_prof_prefix[\n    /* Minimize memory bloat for non-prof builds. */\n#ifdef JEMALLOC_PROF\n    PATH_MAX +\n#endif\n    1];\n\n/* Accessed via prof_active_[gs]et{_unlocked,}(). */\nextern bool\tprof_active;\n\n/* Accessed via prof_gdump_[gs]et{_unlocked,}(). */\nextern bool\tprof_gdump_val;\n\n/*\n * Profile dump interval, measured in bytes allocated.  Each arena triggers a\n * profile dump when it reaches this threshold.  The effect is that the\n * interval between profile dumps averages prof_interval, though the actual\n * interval between dumps will tend to be sporadic, and the interval will be a\n * maximum of approximately (prof_interval * narenas).\n */\nextern uint64_t\tprof_interval;\n\n/*\n * Initialized as opt_lg_prof_sample, and potentially modified during profiling\n * resets.\n */\nextern size_t\tlg_prof_sample;\n\nvoid prof_alloc_rollback(tsd_t *tsd, prof_tctx_t *tctx, bool updated);\nvoid prof_malloc_sample_object(tsdn_t *tsdn, const void *ptr, size_t usize,\n    prof_tctx_t *tctx);\nvoid prof_free_sampled_object(tsd_t *tsd, const void *ptr, size_t usize,\n    prof_tctx_t *tctx);\nvoid bt_init(prof_bt_t *bt, void **vec);\nvoid prof_backtrace(prof_bt_t *bt);\nprof_tctx_t *prof_lookup(tsd_t *tsd, prof_bt_t *bt);\n#ifdef JEMALLOC_JET\nsize_t prof_tdata_count(void);\nsize_t prof_bt_count(void);\n#endif\ntypedef int (prof_dump_open_t)(bool, const char *);\nextern prof_dump_open_t *JET_MUTABLE prof_dump_open;\n\ntypedef bool (prof_dump_header_t)(tsdn_t *, bool, const prof_cnt_t *);\nextern prof_dump_header_t *JET_MUTABLE prof_dump_header;\n#ifdef JEMALLOC_JET\nvoid prof_cnt_all(uint64_t *curobjs, uint64_t *curbytes, uint64_t *accumobjs,\n    uint64_t *accumbytes);\n#endif\nbool prof_accum_init(tsdn_t *tsdn, prof_accum_t *prof_accum);\nvoid prof_idump(tsdn_t *tsdn);\nbool prof_mdump(tsd_t *tsd, const char *filename);\nvoid prof_gdump(tsdn_t *tsdn);\nprof_tdata_t *prof_tdata_init(tsd_t *tsd);\nprof_tdata_t *prof_tdata_reinit(tsd_t *tsd, prof_tdata_t *tdata);\nvoid prof_reset(tsd_t *tsd, size_t lg_sample);\nvoid prof_tdata_cleanup(tsd_t *tsd);\nbool prof_active_get(tsdn_t *tsdn);\nbool prof_active_set(tsdn_t *tsdn, bool active);\nconst char *prof_thread_name_get(tsd_t *tsd);\nint prof_thread_name_set(tsd_t *tsd, const char *thread_name);\nbool prof_thread_active_get(tsd_t *tsd);\nbool prof_thread_active_set(tsd_t *tsd, bool active);\nbool prof_thread_active_init_get(tsdn_t *tsdn);\nbool prof_thread_active_init_set(tsdn_t *tsdn, bool active_init);\nbool prof_gdump_get(tsdn_t *tsdn);\nbool prof_gdump_set(tsdn_t *tsdn, bool active);\nvoid prof_boot0(void);\nvoid prof_boot1(void);\nbool prof_boot2(tsd_t *tsd);\nvoid prof_prefork0(tsdn_t *tsdn);\nvoid prof_prefork1(tsdn_t *tsdn);\nvoid prof_postfork_parent(tsdn_t *tsdn);\nvoid prof_postfork_child(tsdn_t *tsdn);\nvoid prof_sample_threshold_update(prof_tdata_t *tdata);\n\nbool prof_log_start(tsdn_t *tsdn, const char *filename);\nbool prof_log_stop(tsdn_t *tsdn);\n#ifdef JEMALLOC_JET\nsize_t prof_log_bt_count(void);\nsize_t prof_log_alloc_count(void);\nsize_t prof_log_thr_count(void);\nbool prof_log_is_logging(void);\nbool prof_log_rep_check(void);\nvoid prof_log_dummy_set(bool new_value);\n#endif\n\n#endif /* JEMALLOC_INTERNAL_PROF_EXTERNS_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/prof_inlines_a.h",
    "content": "#ifndef JEMALLOC_INTERNAL_PROF_INLINES_A_H\n#define JEMALLOC_INTERNAL_PROF_INLINES_A_H\n\n#include \"jemalloc/internal/mutex.h\"\n\nstatic inline bool\nprof_accum_add(tsdn_t *tsdn, prof_accum_t *prof_accum,\n    uint64_t accumbytes) {\n\tcassert(config_prof);\n\n\tbool overflow;\n\tuint64_t a0, a1;\n\n\t/*\n\t * If the application allocates fast enough (and/or if idump is slow\n\t * enough), extreme overflow here (a1 >= prof_interval * 2) can cause\n\t * idump trigger coalescing.  This is an intentional mechanism that\n\t * avoids rate-limiting allocation.\n\t */\n#ifdef JEMALLOC_ATOMIC_U64\n\ta0 = atomic_load_u64(&prof_accum->accumbytes, ATOMIC_RELAXED);\n\tdo {\n\t\ta1 = a0 + accumbytes;\n\t\tassert(a1 >= a0);\n\t\toverflow = (a1 >= prof_interval);\n\t\tif (overflow) {\n\t\t\ta1 %= prof_interval;\n\t\t}\n\t} while (!atomic_compare_exchange_weak_u64(&prof_accum->accumbytes, &a0,\n\t    a1, ATOMIC_RELAXED, ATOMIC_RELAXED));\n#else\n\tmalloc_mutex_lock(tsdn, &prof_accum->mtx);\n\ta0 = prof_accum->accumbytes;\n\ta1 = a0 + accumbytes;\n\toverflow = (a1 >= prof_interval);\n\tif (overflow) {\n\t\ta1 %= prof_interval;\n\t}\n\tprof_accum->accumbytes = a1;\n\tmalloc_mutex_unlock(tsdn, &prof_accum->mtx);\n#endif\n\treturn overflow;\n}\n\nstatic inline void\nprof_accum_cancel(tsdn_t *tsdn, prof_accum_t *prof_accum,\n    size_t usize) {\n\tcassert(config_prof);\n\n\t/*\n\t * Cancel out as much of the excessive prof_accumbytes increase as\n\t * possible without underflowing.  Interval-triggered dumps occur\n\t * slightly more often than intended as a result of incomplete\n\t * canceling.\n\t */\n\tuint64_t a0, a1;\n#ifdef JEMALLOC_ATOMIC_U64\n\ta0 = atomic_load_u64(&prof_accum->accumbytes, ATOMIC_RELAXED);\n\tdo {\n\t\ta1 = (a0 >= SC_LARGE_MINCLASS - usize)\n\t\t    ? a0 - (SC_LARGE_MINCLASS - usize) : 0;\n\t} while (!atomic_compare_exchange_weak_u64(&prof_accum->accumbytes, &a0,\n\t    a1, ATOMIC_RELAXED, ATOMIC_RELAXED));\n#else\n\tmalloc_mutex_lock(tsdn, &prof_accum->mtx);\n\ta0 = prof_accum->accumbytes;\n\ta1 = (a0 >= SC_LARGE_MINCLASS - usize)\n\t    ?  a0 - (SC_LARGE_MINCLASS - usize) : 0;\n\tprof_accum->accumbytes = a1;\n\tmalloc_mutex_unlock(tsdn, &prof_accum->mtx);\n#endif\n}\n\nJEMALLOC_ALWAYS_INLINE bool\nprof_active_get_unlocked(void) {\n\t/*\n\t * Even if opt_prof is true, sampling can be temporarily disabled by\n\t * setting prof_active to false.  No locking is used when reading\n\t * prof_active in the fast path, so there are no guarantees regarding\n\t * how long it will take for all threads to notice state changes.\n\t */\n\treturn prof_active;\n}\n\n#endif /* JEMALLOC_INTERNAL_PROF_INLINES_A_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/prof_inlines_b.h",
    "content": "#ifndef JEMALLOC_INTERNAL_PROF_INLINES_B_H\n#define JEMALLOC_INTERNAL_PROF_INLINES_B_H\n\n#include \"jemalloc/internal/safety_check.h\"\n#include \"jemalloc/internal/sz.h\"\n\nJEMALLOC_ALWAYS_INLINE bool\nprof_gdump_get_unlocked(void) {\n\t/*\n\t * No locking is used when reading prof_gdump_val in the fast path, so\n\t * there are no guarantees regarding how long it will take for all\n\t * threads to notice state changes.\n\t */\n\treturn prof_gdump_val;\n}\n\nJEMALLOC_ALWAYS_INLINE prof_tdata_t *\nprof_tdata_get(tsd_t *tsd, bool create) {\n\tprof_tdata_t *tdata;\n\n\tcassert(config_prof);\n\n\ttdata = tsd_prof_tdata_get(tsd);\n\tif (create) {\n\t\tif (unlikely(tdata == NULL)) {\n\t\t\tif (tsd_nominal(tsd)) {\n\t\t\t\ttdata = prof_tdata_init(tsd);\n\t\t\t\ttsd_prof_tdata_set(tsd, tdata);\n\t\t\t}\n\t\t} else if (unlikely(tdata->expired)) {\n\t\t\ttdata = prof_tdata_reinit(tsd, tdata);\n\t\t\ttsd_prof_tdata_set(tsd, tdata);\n\t\t}\n\t\tassert(tdata == NULL || tdata->attached);\n\t}\n\n\treturn tdata;\n}\n\nJEMALLOC_ALWAYS_INLINE prof_tctx_t *\nprof_tctx_get(tsdn_t *tsdn, const void *ptr, alloc_ctx_t *alloc_ctx) {\n\tcassert(config_prof);\n\tassert(ptr != NULL);\n\n\treturn arena_prof_tctx_get(tsdn, ptr, alloc_ctx);\n}\n\nJEMALLOC_ALWAYS_INLINE void\nprof_tctx_set(tsdn_t *tsdn, const void *ptr, size_t usize,\n    alloc_ctx_t *alloc_ctx, prof_tctx_t *tctx) {\n\tcassert(config_prof);\n\tassert(ptr != NULL);\n\n\tarena_prof_tctx_set(tsdn, ptr, usize, alloc_ctx, tctx);\n}\n\nJEMALLOC_ALWAYS_INLINE void\nprof_tctx_reset(tsdn_t *tsdn, const void *ptr, prof_tctx_t *tctx) {\n\tcassert(config_prof);\n\tassert(ptr != NULL);\n\n\tarena_prof_tctx_reset(tsdn, ptr, tctx);\n}\n\nJEMALLOC_ALWAYS_INLINE nstime_t\nprof_alloc_time_get(tsdn_t *tsdn, const void *ptr, alloc_ctx_t *alloc_ctx) {\n\tcassert(config_prof);\n\tassert(ptr != NULL);\n\n\treturn arena_prof_alloc_time_get(tsdn, ptr, alloc_ctx);\n}\n\nJEMALLOC_ALWAYS_INLINE void\nprof_alloc_time_set(tsdn_t *tsdn, const void *ptr, alloc_ctx_t *alloc_ctx,\n    nstime_t t) {\n\tcassert(config_prof);\n\tassert(ptr != NULL);\n\n\tarena_prof_alloc_time_set(tsdn, ptr, alloc_ctx, t);\n}\n\nJEMALLOC_ALWAYS_INLINE bool\nprof_sample_check(tsd_t *tsd, size_t usize, bool update) {\n\tssize_t check = update ? 0 : usize;\n\n\tint64_t bytes_until_sample = tsd_bytes_until_sample_get(tsd);\n\tif (update) {\n\t\tbytes_until_sample -= usize;\n\t\tif (tsd_nominal(tsd)) {\n\t\t\ttsd_bytes_until_sample_set(tsd, bytes_until_sample);\n\t\t}\n\t}\n\tif (likely(bytes_until_sample >= check)) {\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\nprof_sample_accum_update(tsd_t *tsd, size_t usize, bool update,\n\t\t\t prof_tdata_t **tdata_out) {\n\tprof_tdata_t *tdata;\n\n\tcassert(config_prof);\n\n\t/* Fastpath: no need to load tdata */\n\tif (likely(prof_sample_check(tsd, usize, update))) {\n\t\treturn true;\n\t}\n\n\tbool booted = tsd_prof_tdata_get(tsd);\n\ttdata = prof_tdata_get(tsd, true);\n\tif (unlikely((uintptr_t)tdata <= (uintptr_t)PROF_TDATA_STATE_MAX)) {\n\t\ttdata = NULL;\n\t}\n\n\tif (tdata_out != NULL) {\n\t\t*tdata_out = tdata;\n\t}\n\n\tif (unlikely(tdata == NULL)) {\n\t\treturn true;\n\t}\n\n\t/*\n\t * If this was the first creation of tdata, then\n\t * prof_tdata_get() reset bytes_until_sample, so decrement and\n\t * check it again\n\t */\n\tif (!booted && prof_sample_check(tsd, usize, update)) {\n\t\treturn true;\n\t}\n\n\tif (tsd_reentrancy_level_get(tsd) > 0) {\n\t\treturn true;\n\t}\n\t/* Compute new sample threshold. */\n\tif (update) {\n\t\tprof_sample_threshold_update(tdata);\n\t}\n\treturn !tdata->active;\n}\n\nJEMALLOC_ALWAYS_INLINE prof_tctx_t *\nprof_alloc_prep(tsd_t *tsd, size_t usize, bool prof_active, bool update) {\n\tprof_tctx_t *ret;\n\tprof_tdata_t *tdata;\n\tprof_bt_t bt;\n\n\tassert(usize == sz_s2u(usize));\n\n\tif (!prof_active || likely(prof_sample_accum_update(tsd, usize, update,\n\t    &tdata))) {\n\t\tret = (prof_tctx_t *)(uintptr_t)1U;\n\t} else {\n\t\tbt_init(&bt, tdata->vec);\n\t\tprof_backtrace(&bt);\n\t\tret = prof_lookup(tsd, &bt);\n\t}\n\n\treturn ret;\n}\n\nJEMALLOC_ALWAYS_INLINE void\nprof_malloc(tsdn_t *tsdn, const void *ptr, size_t usize, alloc_ctx_t *alloc_ctx,\n    prof_tctx_t *tctx) {\n\tcassert(config_prof);\n\tassert(ptr != NULL);\n\tassert(usize == isalloc(tsdn, ptr));\n\n\tif (unlikely((uintptr_t)tctx > (uintptr_t)1U)) {\n\t\tprof_malloc_sample_object(tsdn, ptr, usize, tctx);\n\t} else {\n\t\tprof_tctx_set(tsdn, ptr, usize, alloc_ctx,\n\t\t    (prof_tctx_t *)(uintptr_t)1U);\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE void\nprof_realloc(tsd_t *tsd, const void *ptr, size_t usize, prof_tctx_t *tctx,\n    bool prof_active, bool updated, const void *old_ptr, size_t old_usize,\n    prof_tctx_t *old_tctx) {\n\tbool sampled, old_sampled, moved;\n\n\tcassert(config_prof);\n\tassert(ptr != NULL || (uintptr_t)tctx <= (uintptr_t)1U);\n\n\tif (prof_active && !updated && ptr != NULL) {\n\t\tassert(usize == isalloc(tsd_tsdn(tsd), ptr));\n\t\tif (prof_sample_accum_update(tsd, usize, true, NULL)) {\n\t\t\t/*\n\t\t\t * Don't sample.  The usize passed to prof_alloc_prep()\n\t\t\t * was larger than what actually got allocated, so a\n\t\t\t * backtrace was captured for this allocation, even\n\t\t\t * though its actual usize was insufficient to cross the\n\t\t\t * sample threshold.\n\t\t\t */\n\t\t\tprof_alloc_rollback(tsd, tctx, true);\n\t\t\ttctx = (prof_tctx_t *)(uintptr_t)1U;\n\t\t}\n\t}\n\n\tsampled = ((uintptr_t)tctx > (uintptr_t)1U);\n\told_sampled = ((uintptr_t)old_tctx > (uintptr_t)1U);\n\tmoved = (ptr != old_ptr);\n\n\tif (unlikely(sampled)) {\n\t\tprof_malloc_sample_object(tsd_tsdn(tsd), ptr, usize, tctx);\n\t} else if (moved) {\n\t\tprof_tctx_set(tsd_tsdn(tsd), ptr, usize, NULL,\n\t\t    (prof_tctx_t *)(uintptr_t)1U);\n\t} else if (unlikely(old_sampled)) {\n\t\t/*\n\t\t * prof_tctx_set() would work for the !moved case as well, but\n\t\t * prof_tctx_reset() is slightly cheaper, and the proper thing\n\t\t * to do here in the presence of explicit knowledge re: moved\n\t\t * state.\n\t\t */\n\t\tprof_tctx_reset(tsd_tsdn(tsd), ptr, tctx);\n\t} else {\n\t\tassert((uintptr_t)prof_tctx_get(tsd_tsdn(tsd), ptr, NULL) ==\n\t\t    (uintptr_t)1U);\n\t}\n\n\t/*\n\t * The prof_free_sampled_object() call must come after the\n\t * prof_malloc_sample_object() call, because tctx and old_tctx may be\n\t * the same, in which case reversing the call order could cause the tctx\n\t * to be prematurely destroyed as a side effect of momentarily zeroed\n\t * counters.\n\t */\n\tif (unlikely(old_sampled)) {\n\t\tprof_free_sampled_object(tsd, ptr, old_usize, old_tctx);\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE void\nprof_free(tsd_t *tsd, const void *ptr, size_t usize, alloc_ctx_t *alloc_ctx) {\n\tprof_tctx_t *tctx = prof_tctx_get(tsd_tsdn(tsd), ptr, alloc_ctx);\n\n\tcassert(config_prof);\n\tassert(usize == isalloc(tsd_tsdn(tsd), ptr));\n\n\tif (unlikely((uintptr_t)tctx > (uintptr_t)1U)) {\n\t\tprof_free_sampled_object(tsd, ptr, usize, tctx);\n\t}\n}\n\n#endif /* JEMALLOC_INTERNAL_PROF_INLINES_B_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/prof_structs.h",
    "content": "#ifndef JEMALLOC_INTERNAL_PROF_STRUCTS_H\n#define JEMALLOC_INTERNAL_PROF_STRUCTS_H\n\n#include \"jemalloc/internal/ckh.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/prng.h\"\n#include \"jemalloc/internal/rb.h\"\n\nstruct prof_bt_s {\n\t/* Backtrace, stored as len program counters. */\n\tvoid\t\t**vec;\n\tunsigned\tlen;\n};\n\n#ifdef JEMALLOC_PROF_LIBGCC\n/* Data structure passed to libgcc _Unwind_Backtrace() callback functions. */\ntypedef struct {\n\tprof_bt_t\t*bt;\n\tunsigned\tmax;\n} prof_unwind_data_t;\n#endif\n\nstruct prof_accum_s {\n#ifndef JEMALLOC_ATOMIC_U64\n\tmalloc_mutex_t\tmtx;\n\tuint64_t\taccumbytes;\n#else\n\tatomic_u64_t\taccumbytes;\n#endif\n};\n\nstruct prof_cnt_s {\n\t/* Profiling counters. */\n\tuint64_t\tcurobjs;\n\tuint64_t\tcurbytes;\n\tuint64_t\taccumobjs;\n\tuint64_t\taccumbytes;\n};\n\ntypedef enum {\n\tprof_tctx_state_initializing,\n\tprof_tctx_state_nominal,\n\tprof_tctx_state_dumping,\n\tprof_tctx_state_purgatory /* Dumper must finish destroying. */\n} prof_tctx_state_t;\n\nstruct prof_tctx_s {\n\t/* Thread data for thread that performed the allocation. */\n\tprof_tdata_t\t\t*tdata;\n\n\t/*\n\t * Copy of tdata->thr_{uid,discrim}, necessary because tdata may be\n\t * defunct during teardown.\n\t */\n\tuint64_t\t\tthr_uid;\n\tuint64_t\t\tthr_discrim;\n\n\t/* Profiling counters, protected by tdata->lock. */\n\tprof_cnt_t\t\tcnts;\n\n\t/* Associated global context. */\n\tprof_gctx_t\t\t*gctx;\n\n\t/*\n\t * UID that distinguishes multiple tctx's created by the same thread,\n\t * but coexisting in gctx->tctxs.  There are two ways that such\n\t * coexistence can occur:\n\t * - A dumper thread can cause a tctx to be retained in the purgatory\n\t *   state.\n\t * - Although a single \"producer\" thread must create all tctx's which\n\t *   share the same thr_uid, multiple \"consumers\" can each concurrently\n\t *   execute portions of prof_tctx_destroy().  prof_tctx_destroy() only\n\t *   gets called once each time cnts.cur{objs,bytes} drop to 0, but this\n\t *   threshold can be hit again before the first consumer finishes\n\t *   executing prof_tctx_destroy().\n\t */\n\tuint64_t\t\ttctx_uid;\n\n\t/* Linkage into gctx's tctxs. */\n\trb_node(prof_tctx_t)\ttctx_link;\n\n\t/*\n\t * True during prof_alloc_prep()..prof_malloc_sample_object(), prevents\n\t * sample vs destroy race.\n\t */\n\tbool\t\t\tprepared;\n\n\t/* Current dump-related state, protected by gctx->lock. */\n\tprof_tctx_state_t\tstate;\n\n\t/*\n\t * Copy of cnts snapshotted during early dump phase, protected by\n\t * dump_mtx.\n\t */\n\tprof_cnt_t\t\tdump_cnts;\n};\ntypedef rb_tree(prof_tctx_t) prof_tctx_tree_t;\n\nstruct prof_gctx_s {\n\t/* Protects nlimbo, cnt_summed, and tctxs. */\n\tmalloc_mutex_t\t\t*lock;\n\n\t/*\n\t * Number of threads that currently cause this gctx to be in a state of\n\t * limbo due to one of:\n\t *   - Initializing this gctx.\n\t *   - Initializing per thread counters associated with this gctx.\n\t *   - Preparing to destroy this gctx.\n\t *   - Dumping a heap profile that includes this gctx.\n\t * nlimbo must be 1 (single destroyer) in order to safely destroy the\n\t * gctx.\n\t */\n\tunsigned\t\tnlimbo;\n\n\t/*\n\t * Tree of profile counters, one for each thread that has allocated in\n\t * this context.\n\t */\n\tprof_tctx_tree_t\ttctxs;\n\n\t/* Linkage for tree of contexts to be dumped. */\n\trb_node(prof_gctx_t)\tdump_link;\n\n\t/* Temporary storage for summation during dump. */\n\tprof_cnt_t\t\tcnt_summed;\n\n\t/* Associated backtrace. */\n\tprof_bt_t\t\tbt;\n\n\t/* Backtrace vector, variable size, referred to by bt. */\n\tvoid\t\t\t*vec[1];\n};\ntypedef rb_tree(prof_gctx_t) prof_gctx_tree_t;\n\nstruct prof_tdata_s {\n\tmalloc_mutex_t\t\t*lock;\n\n\t/* Monotonically increasing unique thread identifier. */\n\tuint64_t\t\tthr_uid;\n\n\t/*\n\t * Monotonically increasing discriminator among tdata structures\n\t * associated with the same thr_uid.\n\t */\n\tuint64_t\t\tthr_discrim;\n\n\t/* Included in heap profile dumps if non-NULL. */\n\tchar\t\t\t*thread_name;\n\n\tbool\t\t\tattached;\n\tbool\t\t\texpired;\n\n\trb_node(prof_tdata_t)\ttdata_link;\n\n\t/*\n\t * Counter used to initialize prof_tctx_t's tctx_uid.  No locking is\n\t * necessary when incrementing this field, because only one thread ever\n\t * does so.\n\t */\n\tuint64_t\t\ttctx_uid_next;\n\n\t/*\n\t * Hash of (prof_bt_t *)-->(prof_tctx_t *).  Each thread tracks\n\t * backtraces for which it has non-zero allocation/deallocation counters\n\t * associated with thread-specific prof_tctx_t objects.  Other threads\n\t * may write to prof_tctx_t contents when freeing associated objects.\n\t */\n\tckh_t\t\t\tbt2tctx;\n\n\t/* Sampling state. */\n\tuint64_t\t\tprng_state;\n\n\t/* State used to avoid dumping while operating on prof internals. */\n\tbool\t\t\tenq;\n\tbool\t\t\tenq_idump;\n\tbool\t\t\tenq_gdump;\n\n\t/*\n\t * Set to true during an early dump phase for tdata's which are\n\t * currently being dumped.  New threads' tdata's have this initialized\n\t * to false so that they aren't accidentally included in later dump\n\t * phases.\n\t */\n\tbool\t\t\tdumping;\n\n\t/*\n\t * True if profiling is active for this tdata's thread\n\t * (thread.prof.active mallctl).\n\t */\n\tbool\t\t\tactive;\n\n\t/* Temporary storage for summation during dump. */\n\tprof_cnt_t\t\tcnt_summed;\n\n\t/* Backtrace vector, used for calls to prof_backtrace(). */\n\tvoid\t\t\t*vec[PROF_BT_MAX];\n};\ntypedef rb_tree(prof_tdata_t) prof_tdata_tree_t;\n\n#endif /* JEMALLOC_INTERNAL_PROF_STRUCTS_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/prof_types.h",
    "content": "#ifndef JEMALLOC_INTERNAL_PROF_TYPES_H\n#define JEMALLOC_INTERNAL_PROF_TYPES_H\n\ntypedef struct prof_bt_s prof_bt_t;\ntypedef struct prof_accum_s prof_accum_t;\ntypedef struct prof_cnt_s prof_cnt_t;\ntypedef struct prof_tctx_s prof_tctx_t;\ntypedef struct prof_gctx_s prof_gctx_t;\ntypedef struct prof_tdata_s prof_tdata_t;\n\n/* Option defaults. */\n#ifdef JEMALLOC_PROF\n#  define PROF_PREFIX_DEFAULT\t\t\"jeprof\"\n#else\n#  define PROF_PREFIX_DEFAULT\t\t\"\"\n#endif\n#define LG_PROF_SAMPLE_DEFAULT\t\t19\n#define LG_PROF_INTERVAL_DEFAULT\t-1\n\n/*\n * Hard limit on stack backtrace depth.  The version of prof_backtrace() that\n * is based on __builtin_return_address() necessarily has a hard-coded number\n * of backtrace frame handlers, and should be kept in sync with this setting.\n */\n#define PROF_BT_MAX\t\t\t128\n\n/* Initial hash table size. */\n#define PROF_CKH_MINITEMS\t\t64\n\n/* Size of memory buffer to use when writing dump files. */\n#define PROF_DUMP_BUFSIZE\t\t65536\n\n/* Size of stack-allocated buffer used by prof_printf(). */\n#define PROF_PRINTF_BUFSIZE\t\t128\n\n/*\n * Number of mutexes shared among all gctx's.  No space is allocated for these\n * unless profiling is enabled, so it's okay to over-provision.\n */\n#define PROF_NCTX_LOCKS\t\t\t1024\n\n/*\n * Number of mutexes shared among all tdata's.  No space is allocated for these\n * unless profiling is enabled, so it's okay to over-provision.\n */\n#define PROF_NTDATA_LOCKS\t\t256\n\n/*\n * prof_tdata pointers close to NULL are used to encode state information that\n * is used for cleaning up during thread shutdown.\n */\n#define PROF_TDATA_STATE_REINCARNATED\t((prof_tdata_t *)(uintptr_t)1)\n#define PROF_TDATA_STATE_PURGATORY\t((prof_tdata_t *)(uintptr_t)2)\n#define PROF_TDATA_STATE_MAX\t\tPROF_TDATA_STATE_PURGATORY\n\n#endif /* JEMALLOC_INTERNAL_PROF_TYPES_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/public_namespace.sh",
    "content": "#!/bin/sh\n\nfor nm in `cat $1` ; do\n  n=`echo ${nm} |tr ':' ' ' |awk '{print $1}'`\n  echo \"#define je_${n} JEMALLOC_N(${n})\"\ndone\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/public_unnamespace.sh",
    "content": "#!/bin/sh\n\nfor nm in `cat $1` ; do\n  n=`echo ${nm} |tr ':' ' ' |awk '{print $1}'`\n  echo \"#undef je_${n}\"\ndone\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/ql.h",
    "content": "#ifndef JEMALLOC_INTERNAL_QL_H\n#define JEMALLOC_INTERNAL_QL_H\n\n#include \"jemalloc/internal/qr.h\"\n\n/* List definitions. */\n#define ql_head(a_type)\t\t\t\t\t\t\t\\\nstruct {\t\t\t\t\t\t\t\t\\\n\ta_type *qlh_first;\t\t\t\t\t\t\\\n}\n\n#define ql_head_initializer(a_head) {NULL}\n\n#define ql_elm(a_type)\tqr(a_type)\n\n/* List functions. */\n#define ql_new(a_head) do {\t\t\t\t\t\t\\\n\t(a_head)->qlh_first = NULL;\t\t\t\t\t\\\n} while (0)\n\n#define ql_elm_new(a_elm, a_field) qr_new((a_elm), a_field)\n\n#define ql_first(a_head) ((a_head)->qlh_first)\n\n#define ql_last(a_head, a_field)\t\t\t\t\t\\\n\t((ql_first(a_head) != NULL)\t\t\t\t\t\\\n\t    ? qr_prev(ql_first(a_head), a_field) : NULL)\n\n#define ql_next(a_head, a_elm, a_field)\t\t\t\t\t\\\n\t((ql_last(a_head, a_field) != (a_elm))\t\t\t\t\\\n\t    ? qr_next((a_elm), a_field)\t: NULL)\n\n#define ql_prev(a_head, a_elm, a_field)\t\t\t\t\t\\\n\t((ql_first(a_head) != (a_elm)) ? qr_prev((a_elm), a_field)\t\\\n\t\t\t\t       : NULL)\n\n#define ql_before_insert(a_head, a_qlelm, a_elm, a_field) do {\t\t\\\n\tqr_before_insert((a_qlelm), (a_elm), a_field);\t\t\t\\\n\tif (ql_first(a_head) == (a_qlelm)) {\t\t\t\t\\\n\t\tql_first(a_head) = (a_elm);\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define ql_after_insert(a_qlelm, a_elm, a_field)\t\t\t\\\n\tqr_after_insert((a_qlelm), (a_elm), a_field)\n\n#define ql_head_insert(a_head, a_elm, a_field) do {\t\t\t\\\n\tif (ql_first(a_head) != NULL) {\t\t\t\t\t\\\n\t\tqr_before_insert(ql_first(a_head), (a_elm), a_field);\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tql_first(a_head) = (a_elm);\t\t\t\t\t\\\n} while (0)\n\n#define ql_tail_insert(a_head, a_elm, a_field) do {\t\t\t\\\n\tif (ql_first(a_head) != NULL) {\t\t\t\t\t\\\n\t\tqr_before_insert(ql_first(a_head), (a_elm), a_field);\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tql_first(a_head) = qr_next((a_elm), a_field);\t\t\t\\\n} while (0)\n\n#define ql_remove(a_head, a_elm, a_field) do {\t\t\t\t\\\n\tif (ql_first(a_head) == (a_elm)) {\t\t\t\t\\\n\t\tql_first(a_head) = qr_next(ql_first(a_head), a_field);\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tif (ql_first(a_head) != (a_elm)) {\t\t\t\t\\\n\t\tqr_remove((a_elm), a_field);\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t\tql_first(a_head) = NULL;\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define ql_head_remove(a_head, a_type, a_field) do {\t\t\t\\\n\ta_type *t = ql_first(a_head);\t\t\t\t\t\\\n\tql_remove((a_head), t, a_field);\t\t\t\t\\\n} while (0)\n\n#define ql_tail_remove(a_head, a_type, a_field) do {\t\t\t\\\n\ta_type *t = ql_last(a_head, a_field);\t\t\t\t\\\n\tql_remove((a_head), t, a_field);\t\t\t\t\\\n} while (0)\n\n#define ql_foreach(a_var, a_head, a_field)\t\t\t\t\\\n\tqr_foreach((a_var), ql_first(a_head), a_field)\n\n#define ql_reverse_foreach(a_var, a_head, a_field)\t\t\t\\\n\tqr_reverse_foreach((a_var), ql_first(a_head), a_field)\n\n#endif /* JEMALLOC_INTERNAL_QL_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/qr.h",
    "content": "#ifndef JEMALLOC_INTERNAL_QR_H\n#define JEMALLOC_INTERNAL_QR_H\n\n/* Ring definitions. */\n#define qr(a_type)\t\t\t\t\t\t\t\\\nstruct {\t\t\t\t\t\t\t\t\\\n\ta_type\t*qre_next;\t\t\t\t\t\t\\\n\ta_type\t*qre_prev;\t\t\t\t\t\t\\\n}\n\n/* Ring functions. */\n#define qr_new(a_qr, a_field) do {\t\t\t\t\t\\\n\t(a_qr)->a_field.qre_next = (a_qr);\t\t\t\t\\\n\t(a_qr)->a_field.qre_prev = (a_qr);\t\t\t\t\\\n} while (0)\n\n#define qr_next(a_qr, a_field) ((a_qr)->a_field.qre_next)\n\n#define qr_prev(a_qr, a_field) ((a_qr)->a_field.qre_prev)\n\n#define qr_before_insert(a_qrelm, a_qr, a_field) do {\t\t\t\\\n\t(a_qr)->a_field.qre_prev = (a_qrelm)->a_field.qre_prev;\t\t\\\n\t(a_qr)->a_field.qre_next = (a_qrelm);\t\t\t\t\\\n\t(a_qr)->a_field.qre_prev->a_field.qre_next = (a_qr);\t\t\\\n\t(a_qrelm)->a_field.qre_prev = (a_qr);\t\t\t\t\\\n} while (0)\n\n#define qr_after_insert(a_qrelm, a_qr, a_field) do {\t\t\t\\\n\t(a_qr)->a_field.qre_next = (a_qrelm)->a_field.qre_next;\t\t\\\n\t(a_qr)->a_field.qre_prev = (a_qrelm);\t\t\t\t\\\n\t(a_qr)->a_field.qre_next->a_field.qre_prev = (a_qr);\t\t\\\n\t(a_qrelm)->a_field.qre_next = (a_qr);\t\t\t\t\\\n} while (0)\n\n#define qr_meld(a_qr_a, a_qr_b, a_type, a_field) do {\t\t\t\\\n\ta_type *t;\t\t\t\t\t\t\t\\\n\t(a_qr_a)->a_field.qre_prev->a_field.qre_next = (a_qr_b);\t\\\n\t(a_qr_b)->a_field.qre_prev->a_field.qre_next = (a_qr_a);\t\\\n\tt = (a_qr_a)->a_field.qre_prev;\t\t\t\t\t\\\n\t(a_qr_a)->a_field.qre_prev = (a_qr_b)->a_field.qre_prev;\t\\\n\t(a_qr_b)->a_field.qre_prev = t;\t\t\t\t\t\\\n} while (0)\n\n/*\n * qr_meld() and qr_split() are functionally equivalent, so there's no need to\n * have two copies of the code.\n */\n#define qr_split(a_qr_a, a_qr_b, a_type, a_field)\t\t\t\\\n\tqr_meld((a_qr_a), (a_qr_b), a_type, a_field)\n\n#define qr_remove(a_qr, a_field) do {\t\t\t\t\t\\\n\t(a_qr)->a_field.qre_prev->a_field.qre_next\t\t\t\\\n\t    = (a_qr)->a_field.qre_next;\t\t\t\t\t\\\n\t(a_qr)->a_field.qre_next->a_field.qre_prev\t\t\t\\\n\t    = (a_qr)->a_field.qre_prev;\t\t\t\t\t\\\n\t(a_qr)->a_field.qre_next = (a_qr);\t\t\t\t\\\n\t(a_qr)->a_field.qre_prev = (a_qr);\t\t\t\t\\\n} while (0)\n\n#define qr_foreach(var, a_qr, a_field)\t\t\t\t\t\\\n\tfor ((var) = (a_qr);\t\t\t\t\t\t\\\n\t    (var) != NULL;\t\t\t\t\t\t\\\n\t    (var) = (((var)->a_field.qre_next != (a_qr))\t\t\\\n\t    ? (var)->a_field.qre_next : NULL))\n\n#define qr_reverse_foreach(var, a_qr, a_field)\t\t\t\t\\\n\tfor ((var) = ((a_qr) != NULL) ? qr_prev(a_qr, a_field) : NULL;\t\\\n\t    (var) != NULL;\t\t\t\t\t\t\\\n\t    (var) = (((var) != (a_qr))\t\t\t\t\t\\\n\t    ? (var)->a_field.qre_prev : NULL))\n\n#endif /* JEMALLOC_INTERNAL_QR_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/quantum.h",
    "content": "#ifndef JEMALLOC_INTERNAL_QUANTUM_H\n#define JEMALLOC_INTERNAL_QUANTUM_H\n\n/*\n * Minimum allocation alignment is 2^LG_QUANTUM bytes (ignoring tiny size\n * classes).\n */\n#ifndef LG_QUANTUM\n#  if (defined(__i386__) || defined(_M_IX86))\n#    define LG_QUANTUM\t\t4\n#  endif\n#  ifdef __ia64__\n#    define LG_QUANTUM\t\t4\n#  endif\n#  ifdef __alpha__\n#    define LG_QUANTUM\t\t4\n#  endif\n#  if (defined(__sparc64__) || defined(__sparcv9) || defined(__sparc_v9__))\n#    define LG_QUANTUM\t\t4\n#  endif\n#  if (defined(__amd64__) || defined(__x86_64__) || defined(_M_X64))\n#    define LG_QUANTUM\t\t4\n#  endif\n#  ifdef __arm__\n#    define LG_QUANTUM\t\t3\n#  endif\n#  ifdef __aarch64__\n#    define LG_QUANTUM\t\t4\n#  endif\n#  ifdef __hppa__\n#    define LG_QUANTUM\t\t4\n#  endif\n#  ifdef __m68k__\n#    define LG_QUANTUM\t\t3\n#  endif\n#  ifdef __mips__\n#    define LG_QUANTUM\t\t3\n#  endif\n#  ifdef __nios2__\n#    define LG_QUANTUM\t\t3\n#  endif\n#  ifdef __or1k__\n#    define LG_QUANTUM\t\t3\n#  endif\n#  ifdef __powerpc__\n#    define LG_QUANTUM\t\t4\n#  endif\n#  if defined(__riscv) || defined(__riscv__)\n#    define LG_QUANTUM\t\t4\n#  endif\n#  ifdef __s390__\n#    define LG_QUANTUM\t\t4\n#  endif\n#  if (defined (__SH3E__) || defined(__SH4_SINGLE__) || defined(__SH4__) || \\\n\tdefined(__SH4_SINGLE_ONLY__))\n#    define LG_QUANTUM\t\t4\n#  endif\n#  ifdef __tile__\n#    define LG_QUANTUM\t\t4\n#  endif\n#  ifdef __le32__\n#    define LG_QUANTUM\t\t4\n#  endif\n#  ifndef LG_QUANTUM\n#    error \"Unknown minimum alignment for architecture; specify via \"\n\t \"--with-lg-quantum\"\n#  endif\n#endif\n\n#define QUANTUM\t\t\t((size_t)(1U << LG_QUANTUM))\n#define QUANTUM_MASK\t\t(QUANTUM - 1)\n\n/* Return the smallest quantum multiple that is >= a. */\n#define QUANTUM_CEILING(a)\t\t\t\t\t\t\\\n\t(((a) + QUANTUM_MASK) & ~QUANTUM_MASK)\n\n#endif /* JEMALLOC_INTERNAL_QUANTUM_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/rb.h",
    "content": "/*-\n *******************************************************************************\n *\n * cpp macro implementation of left-leaning 2-3 red-black trees.  Parent\n * pointers are not used, and color bits are stored in the least significant\n * bit of right-child pointers (if RB_COMPACT is defined), thus making node\n * linkage as compact as is possible for red-black trees.\n *\n * Usage:\n *\n *   #include <stdint.h>\n *   #include <stdbool.h>\n *   #define NDEBUG // (Optional, see assert(3).)\n *   #include <assert.h>\n *   #define RB_COMPACT // (Optional, embed color bits in right-child pointers.)\n *   #include <rb.h>\n *   ...\n *\n *******************************************************************************\n */\n\n#ifndef RB_H_\n#define RB_H_\n\n#ifndef __PGI\n#define RB_COMPACT\n#endif\n\n#ifdef RB_COMPACT\n/* Node structure. */\n#define rb_node(a_type)\t\t\t\t\t\t\t\\\nstruct {\t\t\t\t\t\t\t\t\\\n    a_type *rbn_left;\t\t\t\t\t\t\t\\\n    a_type *rbn_right_red;\t\t\t\t\t\t\\\n}\n#else\n#define rb_node(a_type)\t\t\t\t\t\t\t\\\nstruct {\t\t\t\t\t\t\t\t\\\n    a_type *rbn_left;\t\t\t\t\t\t\t\\\n    a_type *rbn_right;\t\t\t\t\t\t\t\\\n    bool rbn_red;\t\t\t\t\t\t\t\\\n}\n#endif\n\n/* Root structure. */\n#define rb_tree(a_type)\t\t\t\t\t\t\t\\\nstruct {\t\t\t\t\t\t\t\t\\\n    a_type *rbt_root;\t\t\t\t\t\t\t\\\n}\n\n/* Left accessors. */\n#define rbtn_left_get(a_type, a_field, a_node)\t\t\t\t\\\n    ((a_node)->a_field.rbn_left)\n#define rbtn_left_set(a_type, a_field, a_node, a_left) do {\t\t\\\n    (a_node)->a_field.rbn_left = a_left;\t\t\t\t\\\n} while (0)\n\n#ifdef RB_COMPACT\n/* Right accessors. */\n#define rbtn_right_get(a_type, a_field, a_node)\t\t\t\t\\\n    ((a_type *) (((intptr_t) (a_node)->a_field.rbn_right_red)\t\t\\\n      & ((ssize_t)-2)))\n#define rbtn_right_set(a_type, a_field, a_node, a_right) do {\t\t\\\n    (a_node)->a_field.rbn_right_red = (a_type *) (((uintptr_t) a_right)\t\\\n      | (((uintptr_t) (a_node)->a_field.rbn_right_red) & ((size_t)1)));\t\\\n} while (0)\n\n/* Color accessors. */\n#define rbtn_red_get(a_type, a_field, a_node)\t\t\t\t\\\n    ((bool) (((uintptr_t) (a_node)->a_field.rbn_right_red)\t\t\\\n      & ((size_t)1)))\n#define rbtn_color_set(a_type, a_field, a_node, a_red) do {\t\t\\\n    (a_node)->a_field.rbn_right_red = (a_type *) ((((intptr_t)\t\t\\\n      (a_node)->a_field.rbn_right_red) & ((ssize_t)-2))\t\t\t\\\n      | ((ssize_t)a_red));\t\t\t\t\t\t\\\n} while (0)\n#define rbtn_red_set(a_type, a_field, a_node) do {\t\t\t\\\n    (a_node)->a_field.rbn_right_red = (a_type *) (((uintptr_t)\t\t\\\n      (a_node)->a_field.rbn_right_red) | ((size_t)1));\t\t\t\\\n} while (0)\n#define rbtn_black_set(a_type, a_field, a_node) do {\t\t\t\\\n    (a_node)->a_field.rbn_right_red = (a_type *) (((intptr_t)\t\t\\\n      (a_node)->a_field.rbn_right_red) & ((ssize_t)-2));\t\t\\\n} while (0)\n\n/* Node initializer. */\n#define rbt_node_new(a_type, a_field, a_rbt, a_node) do {\t\t\\\n    /* Bookkeeping bit cannot be used by node pointer. */\t\t\\\n    assert(((uintptr_t)(a_node) & 0x1) == 0);\t\t\t\t\\\n    rbtn_left_set(a_type, a_field, (a_node), NULL);\t\\\n    rbtn_right_set(a_type, a_field, (a_node), NULL);\t\\\n    rbtn_red_set(a_type, a_field, (a_node));\t\t\t\t\\\n} while (0)\n#else\n/* Right accessors. */\n#define rbtn_right_get(a_type, a_field, a_node)\t\t\t\t\\\n    ((a_node)->a_field.rbn_right)\n#define rbtn_right_set(a_type, a_field, a_node, a_right) do {\t\t\\\n    (a_node)->a_field.rbn_right = a_right;\t\t\t\t\\\n} while (0)\n\n/* Color accessors. */\n#define rbtn_red_get(a_type, a_field, a_node)\t\t\t\t\\\n    ((a_node)->a_field.rbn_red)\n#define rbtn_color_set(a_type, a_field, a_node, a_red) do {\t\t\\\n    (a_node)->a_field.rbn_red = (a_red);\t\t\t\t\\\n} while (0)\n#define rbtn_red_set(a_type, a_field, a_node) do {\t\t\t\\\n    (a_node)->a_field.rbn_red = true;\t\t\t\t\t\\\n} while (0)\n#define rbtn_black_set(a_type, a_field, a_node) do {\t\t\t\\\n    (a_node)->a_field.rbn_red = false;\t\t\t\t\t\\\n} while (0)\n\n/* Node initializer. */\n#define rbt_node_new(a_type, a_field, a_rbt, a_node) do {\t\t\\\n    rbtn_left_set(a_type, a_field, (a_node), NULL);\t\\\n    rbtn_right_set(a_type, a_field, (a_node), NULL);\t\\\n    rbtn_red_set(a_type, a_field, (a_node));\t\t\t\t\\\n} while (0)\n#endif\n\n/* Tree initializer. */\n#define rb_new(a_type, a_field, a_rbt) do {\t\t\t\t\\\n    (a_rbt)->rbt_root = NULL;\t\t\t\t\t\t\\\n} while (0)\n\n/* Internal utility macros. */\n#define rbtn_first(a_type, a_field, a_rbt, a_root, r_node) do {\t\t\\\n    (r_node) = (a_root);\t\t\t\t\t\t\\\n    if ((r_node) != NULL) {\t\t\t\t\t\t\\\n\tfor (;\t\t\t\t\t\t\t\t\\\n\t  rbtn_left_get(a_type, a_field, (r_node)) != NULL;\t\t\\\n\t  (r_node) = rbtn_left_get(a_type, a_field, (r_node))) {\t\\\n\t}\t\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define rbtn_last(a_type, a_field, a_rbt, a_root, r_node) do {\t\t\\\n    (r_node) = (a_root);\t\t\t\t\t\t\\\n    if ((r_node) != NULL) {\t\t\t\t\t\t\\\n\tfor (; rbtn_right_get(a_type, a_field, (r_node)) != NULL;\t\\\n\t  (r_node) = rbtn_right_get(a_type, a_field, (r_node))) {\t\\\n\t}\t\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define rbtn_rotate_left(a_type, a_field, a_node, r_node) do {\t\t\\\n    (r_node) = rbtn_right_get(a_type, a_field, (a_node));\t\t\\\n    rbtn_right_set(a_type, a_field, (a_node),\t\t\t\t\\\n      rbtn_left_get(a_type, a_field, (r_node)));\t\t\t\\\n    rbtn_left_set(a_type, a_field, (r_node), (a_node));\t\t\t\\\n} while (0)\n\n#define rbtn_rotate_right(a_type, a_field, a_node, r_node) do {\t\t\\\n    (r_node) = rbtn_left_get(a_type, a_field, (a_node));\t\t\\\n    rbtn_left_set(a_type, a_field, (a_node),\t\t\t\t\\\n      rbtn_right_get(a_type, a_field, (r_node)));\t\t\t\\\n    rbtn_right_set(a_type, a_field, (r_node), (a_node));\t\t\\\n} while (0)\n\n/*\n * The rb_proto() macro generates function prototypes that correspond to the\n * functions generated by an equivalently parameterized call to rb_gen().\n */\n\n#define rb_proto(a_attr, a_prefix, a_rbt_type, a_type)\t\t\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##new(a_rbt_type *rbtree);\t\t\t\t\t\\\na_attr bool\t\t\t\t\t\t\t\t\\\na_prefix##empty(a_rbt_type *rbtree);\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##first(a_rbt_type *rbtree);\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##last(a_rbt_type *rbtree);\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##next(a_rbt_type *rbtree, a_type *node);\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##prev(a_rbt_type *rbtree, a_type *node);\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##search(a_rbt_type *rbtree, const a_type *key);\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##nsearch(a_rbt_type *rbtree, const a_type *key);\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##psearch(a_rbt_type *rbtree, const a_type *key);\t\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##insert(a_rbt_type *rbtree, a_type *node);\t\t\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##remove(a_rbt_type *rbtree, a_type *node);\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##iter(a_rbt_type *rbtree, a_type *start, a_type *(*cb)(\t\\\n  a_rbt_type *, a_type *, void *), void *arg);\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##reverse_iter(a_rbt_type *rbtree, a_type *start,\t\t\\\n  a_type *(*cb)(a_rbt_type *, a_type *, void *), void *arg);\t\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##destroy(a_rbt_type *rbtree, void (*cb)(a_type *, void *),\t\\\n  void *arg);\n\n/*\n * The rb_gen() macro generates a type-specific red-black tree implementation,\n * based on the above cpp macros.\n *\n * Arguments:\n *\n *   a_attr    : Function attribute for generated functions (ex: static).\n *   a_prefix  : Prefix for generated functions (ex: ex_).\n *   a_rb_type : Type for red-black tree data structure (ex: ex_t).\n *   a_type    : Type for red-black tree node data structure (ex: ex_node_t).\n *   a_field   : Name of red-black tree node linkage (ex: ex_link).\n *   a_cmp     : Node comparison function name, with the following prototype:\n *                 int (a_cmp *)(a_type *a_node, a_type *a_other);\n *                                       ^^^^^^\n *                                    or a_key\n *               Interpretation of comparison function return values:\n *                 -1 : a_node <  a_other\n *                  0 : a_node == a_other\n *                  1 : a_node >  a_other\n *               In all cases, the a_node or a_key macro argument is the first\n *               argument to the comparison function, which makes it possible\n *               to write comparison functions that treat the first argument\n *               specially.\n *\n * Assuming the following setup:\n *\n *   typedef struct ex_node_s ex_node_t;\n *   struct ex_node_s {\n *       rb_node(ex_node_t) ex_link;\n *   };\n *   typedef rb_tree(ex_node_t) ex_t;\n *   rb_gen(static, ex_, ex_t, ex_node_t, ex_link, ex_cmp)\n *\n * The following API is generated:\n *\n *   static void\n *   ex_new(ex_t *tree);\n *       Description: Initialize a red-black tree structure.\n *       Args:\n *         tree: Pointer to an uninitialized red-black tree object.\n *\n *   static bool\n *   ex_empty(ex_t *tree);\n *       Description: Determine whether tree is empty.\n *       Args:\n *         tree: Pointer to an initialized red-black tree object.\n *       Ret: True if tree is empty, false otherwise.\n *\n *   static ex_node_t *\n *   ex_first(ex_t *tree);\n *   static ex_node_t *\n *   ex_last(ex_t *tree);\n *       Description: Get the first/last node in tree.\n *       Args:\n *         tree: Pointer to an initialized red-black tree object.\n *       Ret: First/last node in tree, or NULL if tree is empty.\n *\n *   static ex_node_t *\n *   ex_next(ex_t *tree, ex_node_t *node);\n *   static ex_node_t *\n *   ex_prev(ex_t *tree, ex_node_t *node);\n *       Description: Get node's successor/predecessor.\n *       Args:\n *         tree: Pointer to an initialized red-black tree object.\n *         node: A node in tree.\n *       Ret: node's successor/predecessor in tree, or NULL if node is\n *            last/first.\n *\n *   static ex_node_t *\n *   ex_search(ex_t *tree, const ex_node_t *key);\n *       Description: Search for node that matches key.\n *       Args:\n *         tree: Pointer to an initialized red-black tree object.\n *         key : Search key.\n *       Ret: Node in tree that matches key, or NULL if no match.\n *\n *   static ex_node_t *\n *   ex_nsearch(ex_t *tree, const ex_node_t *key);\n *   static ex_node_t *\n *   ex_psearch(ex_t *tree, const ex_node_t *key);\n *       Description: Search for node that matches key.  If no match is found,\n *                    return what would be key's successor/predecessor, were\n *                    key in tree.\n *       Args:\n *         tree: Pointer to an initialized red-black tree object.\n *         key : Search key.\n *       Ret: Node in tree that matches key, or if no match, hypothetical node's\n *            successor/predecessor (NULL if no successor/predecessor).\n *\n *   static void\n *   ex_insert(ex_t *tree, ex_node_t *node);\n *       Description: Insert node into tree.\n *       Args:\n *         tree: Pointer to an initialized red-black tree object.\n *         node: Node to be inserted into tree.\n *\n *   static void\n *   ex_remove(ex_t *tree, ex_node_t *node);\n *       Description: Remove node from tree.\n *       Args:\n *         tree: Pointer to an initialized red-black tree object.\n *         node: Node in tree to be removed.\n *\n *   static ex_node_t *\n *   ex_iter(ex_t *tree, ex_node_t *start, ex_node_t *(*cb)(ex_t *,\n *     ex_node_t *, void *), void *arg);\n *   static ex_node_t *\n *   ex_reverse_iter(ex_t *tree, ex_node_t *start, ex_node *(*cb)(ex_t *,\n *     ex_node_t *, void *), void *arg);\n *       Description: Iterate forward/backward over tree, starting at node.  If\n *                    tree is modified, iteration must be immediately\n *                    terminated by the callback function that causes the\n *                    modification.\n *       Args:\n *         tree : Pointer to an initialized red-black tree object.\n *         start: Node at which to start iteration, or NULL to start at\n *                first/last node.\n *         cb   : Callback function, which is called for each node during\n *                iteration.  Under normal circumstances the callback function\n *                should return NULL, which causes iteration to continue.  If a\n *                callback function returns non-NULL, iteration is immediately\n *                terminated and the non-NULL return value is returned by the\n *                iterator.  This is useful for re-starting iteration after\n *                modifying tree.\n *         arg  : Opaque pointer passed to cb().\n *       Ret: NULL if iteration completed, or the non-NULL callback return value\n *            that caused termination of the iteration.\n *\n *   static void\n *   ex_destroy(ex_t *tree, void (*cb)(ex_node_t *, void *), void *arg);\n *       Description: Iterate over the tree with post-order traversal, remove\n *                    each node, and run the callback if non-null.  This is\n *                    used for destroying a tree without paying the cost to\n *                    rebalance it.  The tree must not be otherwise altered\n *                    during traversal.\n *       Args:\n *         tree: Pointer to an initialized red-black tree object.\n *         cb  : Callback function, which, if non-null, is called for each node\n *               during iteration.  There is no way to stop iteration once it\n *               has begun.\n *         arg : Opaque pointer passed to cb().\n */\n#define rb_gen(a_attr, a_prefix, a_rbt_type, a_type, a_field, a_cmp)\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##new(a_rbt_type *rbtree) {\t\t\t\t\t\\\n    rb_new(a_type, a_field, rbtree);\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr bool\t\t\t\t\t\t\t\t\\\na_prefix##empty(a_rbt_type *rbtree) {\t\t\t\t\t\\\n    return (rbtree->rbt_root == NULL);\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##first(a_rbt_type *rbtree) {\t\t\t\t\t\\\n    a_type *ret;\t\t\t\t\t\t\t\\\n    rbtn_first(a_type, a_field, rbtree, rbtree->rbt_root, ret);\t\t\\\n    return ret;\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##last(a_rbt_type *rbtree) {\t\t\t\t\t\\\n    a_type *ret;\t\t\t\t\t\t\t\\\n    rbtn_last(a_type, a_field, rbtree, rbtree->rbt_root, ret);\t\t\\\n    return ret;\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##next(a_rbt_type *rbtree, a_type *node) {\t\t\t\\\n    a_type *ret;\t\t\t\t\t\t\t\\\n    if (rbtn_right_get(a_type, a_field, node) != NULL) {\t\t\\\n\trbtn_first(a_type, a_field, rbtree, rbtn_right_get(a_type,\t\\\n\t  a_field, node), ret);\t\t\t\t\t\t\\\n    } else {\t\t\t\t\t\t\t\t\\\n\ta_type *tnode = rbtree->rbt_root;\t\t\t\t\\\n\tassert(tnode != NULL);\t\t\t\t\t\t\\\n\tret = NULL;\t\t\t\t\t\t\t\\\n\twhile (true) {\t\t\t\t\t\t\t\\\n\t    int cmp = (a_cmp)(node, tnode);\t\t\t\t\\\n\t    if (cmp < 0) {\t\t\t\t\t\t\\\n\t\tret = tnode;\t\t\t\t\t\t\\\n\t\ttnode = rbtn_left_get(a_type, a_field, tnode);\t\t\\\n\t    } else if (cmp > 0) {\t\t\t\t\t\\\n\t\ttnode = rbtn_right_get(a_type, a_field, tnode);\t\t\\\n\t    } else {\t\t\t\t\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\t    }\t\t\t\t\t\t\t\t\\\n\t    assert(tnode != NULL);\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n    return ret;\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##prev(a_rbt_type *rbtree, a_type *node) {\t\t\t\\\n    a_type *ret;\t\t\t\t\t\t\t\\\n    if (rbtn_left_get(a_type, a_field, node) != NULL) {\t\t\t\\\n\trbtn_last(a_type, a_field, rbtree, rbtn_left_get(a_type,\t\\\n\t  a_field, node), ret);\t\t\t\t\t\t\\\n    } else {\t\t\t\t\t\t\t\t\\\n\ta_type *tnode = rbtree->rbt_root;\t\t\t\t\\\n\tassert(tnode != NULL);\t\t\t\t\t\t\\\n\tret = NULL;\t\t\t\t\t\t\t\\\n\twhile (true) {\t\t\t\t\t\t\t\\\n\t    int cmp = (a_cmp)(node, tnode);\t\t\t\t\\\n\t    if (cmp < 0) {\t\t\t\t\t\t\\\n\t\ttnode = rbtn_left_get(a_type, a_field, tnode);\t\t\\\n\t    } else if (cmp > 0) {\t\t\t\t\t\\\n\t\tret = tnode;\t\t\t\t\t\t\\\n\t\ttnode = rbtn_right_get(a_type, a_field, tnode);\t\t\\\n\t    } else {\t\t\t\t\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\t    }\t\t\t\t\t\t\t\t\\\n\t    assert(tnode != NULL);\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n    return ret;\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##search(a_rbt_type *rbtree, const a_type *key) {\t\t\\\n    a_type *ret;\t\t\t\t\t\t\t\\\n    int cmp;\t\t\t\t\t\t\t\t\\\n    ret = rbtree->rbt_root;\t\t\t\t\t\t\\\n    while (ret != NULL\t\t\t\t\t\t\t\\\n      && (cmp = (a_cmp)(key, ret)) != 0) {\t\t\t\t\\\n\tif (cmp < 0) {\t\t\t\t\t\t\t\\\n\t    ret = rbtn_left_get(a_type, a_field, ret);\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t    ret = rbtn_right_get(a_type, a_field, ret);\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n    return ret;\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##nsearch(a_rbt_type *rbtree, const a_type *key) {\t\t\\\n    a_type *ret;\t\t\t\t\t\t\t\\\n    a_type *tnode = rbtree->rbt_root;\t\t\t\t\t\\\n    ret = NULL;\t\t\t\t\t\t\t\t\\\n    while (tnode != NULL) {\t\t\t\t\t\t\\\n\tint cmp = (a_cmp)(key, tnode);\t\t\t\t\t\\\n\tif (cmp < 0) {\t\t\t\t\t\t\t\\\n\t    ret = tnode;\t\t\t\t\t\t\\\n\t    tnode = rbtn_left_get(a_type, a_field, tnode);\t\t\\\n\t} else if (cmp > 0) {\t\t\t\t\t\t\\\n\t    tnode = rbtn_right_get(a_type, a_field, tnode);\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t    ret = tnode;\t\t\t\t\t\t\\\n\t    break;\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n    return ret;\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##psearch(a_rbt_type *rbtree, const a_type *key) {\t\t\\\n    a_type *ret;\t\t\t\t\t\t\t\\\n    a_type *tnode = rbtree->rbt_root;\t\t\t\t\t\\\n    ret = NULL;\t\t\t\t\t\t\t\t\\\n    while (tnode != NULL) {\t\t\t\t\t\t\\\n\tint cmp = (a_cmp)(key, tnode);\t\t\t\t\t\\\n\tif (cmp < 0) {\t\t\t\t\t\t\t\\\n\t    tnode = rbtn_left_get(a_type, a_field, tnode);\t\t\\\n\t} else if (cmp > 0) {\t\t\t\t\t\t\\\n\t    ret = tnode;\t\t\t\t\t\t\\\n\t    tnode = rbtn_right_get(a_type, a_field, tnode);\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t    ret = tnode;\t\t\t\t\t\t\\\n\t    break;\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n    return ret;\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##insert(a_rbt_type *rbtree, a_type *node) {\t\t\t\\\n    struct {\t\t\t\t\t\t\t\t\\\n\ta_type *node;\t\t\t\t\t\t\t\\\n\tint cmp;\t\t\t\t\t\t\t\\\n    } path[sizeof(void *) << 4], *pathp;\t\t\t\t\\\n    rbt_node_new(a_type, a_field, rbtree, node);\t\t\t\\\n    /* Wind. */\t\t\t\t\t\t\t\t\\\n    path->node = rbtree->rbt_root;\t\t\t\t\t\\\n    for (pathp = path; pathp->node != NULL; pathp++) {\t\t\t\\\n\tint cmp = pathp->cmp = a_cmp(node, pathp->node);\t\t\\\n\tassert(cmp != 0);\t\t\t\t\t\t\\\n\tif (cmp < 0) {\t\t\t\t\t\t\t\\\n\t    pathp[1].node = rbtn_left_get(a_type, a_field,\t\t\\\n\t      pathp->node);\t\t\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t    pathp[1].node = rbtn_right_get(a_type, a_field,\t\t\\\n\t      pathp->node);\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n    pathp->node = node;\t\t\t\t\t\t\t\\\n    /* Unwind. */\t\t\t\t\t\t\t\\\n    for (pathp--; (uintptr_t)pathp >= (uintptr_t)path; pathp--) {\t\\\n\ta_type *cnode = pathp->node;\t\t\t\t\t\\\n\tif (pathp->cmp < 0) {\t\t\t\t\t\t\\\n\t    a_type *left = pathp[1].node;\t\t\t\t\\\n\t    rbtn_left_set(a_type, a_field, cnode, left);\t\t\\\n\t    if (rbtn_red_get(a_type, a_field, left)) {\t\t\t\\\n\t\ta_type *leftleft = rbtn_left_get(a_type, a_field, left);\\\n\t\tif (leftleft != NULL && rbtn_red_get(a_type, a_field,\t\\\n\t\t  leftleft)) {\t\t\t\t\t\t\\\n\t\t    /* Fix up 4-node. */\t\t\t\t\\\n\t\t    a_type *tnode;\t\t\t\t\t\\\n\t\t    rbtn_black_set(a_type, a_field, leftleft);\t\t\\\n\t\t    rbtn_rotate_right(a_type, a_field, cnode, tnode);\t\\\n\t\t    cnode = tnode;\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t    } else {\t\t\t\t\t\t\t\\\n\t\treturn;\t\t\t\t\t\t\t\\\n\t    }\t\t\t\t\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t    a_type *right = pathp[1].node;\t\t\t\t\\\n\t    rbtn_right_set(a_type, a_field, cnode, right);\t\t\\\n\t    if (rbtn_red_get(a_type, a_field, right)) {\t\t\t\\\n\t\ta_type *left = rbtn_left_get(a_type, a_field, cnode);\t\\\n\t\tif (left != NULL && rbtn_red_get(a_type, a_field,\t\\\n\t\t  left)) {\t\t\t\t\t\t\\\n\t\t    /* Split 4-node. */\t\t\t\t\t\\\n\t\t    rbtn_black_set(a_type, a_field, left);\t\t\\\n\t\t    rbtn_black_set(a_type, a_field, right);\t\t\\\n\t\t    rbtn_red_set(a_type, a_field, cnode);\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t    /* Lean left. */\t\t\t\t\t\\\n\t\t    a_type *tnode;\t\t\t\t\t\\\n\t\t    bool tred = rbtn_red_get(a_type, a_field, cnode);\t\\\n\t\t    rbtn_rotate_left(a_type, a_field, cnode, tnode);\t\\\n\t\t    rbtn_color_set(a_type, a_field, tnode, tred);\t\\\n\t\t    rbtn_red_set(a_type, a_field, cnode);\t\t\\\n\t\t    cnode = tnode;\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t    } else {\t\t\t\t\t\t\t\\\n\t\treturn;\t\t\t\t\t\t\t\\\n\t    }\t\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tpathp->node = cnode;\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n    /* Set root, and make it black. */\t\t\t\t\t\\\n    rbtree->rbt_root = path->node;\t\t\t\t\t\\\n    rbtn_black_set(a_type, a_field, rbtree->rbt_root);\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##remove(a_rbt_type *rbtree, a_type *node) {\t\t\t\\\n    struct {\t\t\t\t\t\t\t\t\\\n\ta_type *node;\t\t\t\t\t\t\t\\\n\tint cmp;\t\t\t\t\t\t\t\\\n    } *pathp, *nodep, path[sizeof(void *) << 4];\t\t\t\\\n    /* Wind. */\t\t\t\t\t\t\t\t\\\n    nodep = NULL; /* Silence compiler warning. */\t\t\t\\\n    path->node = rbtree->rbt_root;\t\t\t\t\t\\\n    for (pathp = path; pathp->node != NULL; pathp++) {\t\t\t\\\n\tint cmp = pathp->cmp = a_cmp(node, pathp->node);\t\t\\\n\tif (cmp < 0) {\t\t\t\t\t\t\t\\\n\t    pathp[1].node = rbtn_left_get(a_type, a_field,\t\t\\\n\t      pathp->node);\t\t\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t    pathp[1].node = rbtn_right_get(a_type, a_field,\t\t\\\n\t      pathp->node);\t\t\t\t\t\t\\\n\t    if (cmp == 0) {\t\t\t\t\t\t\\\n\t        /* Find node's successor, in preparation for swap. */\t\\\n\t\tpathp->cmp = 1;\t\t\t\t\t\t\\\n\t\tnodep = pathp;\t\t\t\t\t\t\\\n\t\tfor (pathp++; pathp->node != NULL; pathp++) {\t\t\\\n\t\t    pathp->cmp = -1;\t\t\t\t\t\\\n\t\t    pathp[1].node = rbtn_left_get(a_type, a_field,\t\\\n\t\t      pathp->node);\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\t    }\t\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n    assert(nodep->node == node);\t\t\t\t\t\\\n    pathp--;\t\t\t\t\t\t\t\t\\\n    if (pathp->node != node) {\t\t\t\t\t\t\\\n\t/* Swap node with its successor. */\t\t\t\t\\\n\tbool tred = rbtn_red_get(a_type, a_field, pathp->node);\t\t\\\n\trbtn_color_set(a_type, a_field, pathp->node,\t\t\t\\\n\t  rbtn_red_get(a_type, a_field, node));\t\t\t\t\\\n\trbtn_left_set(a_type, a_field, pathp->node,\t\t\t\\\n\t  rbtn_left_get(a_type, a_field, node));\t\t\t\\\n\t/* If node's successor is its right child, the following code */\\\n\t/* will do the wrong thing for the right child pointer.       */\\\n\t/* However, it doesn't matter, because the pointer will be    */\\\n\t/* properly set when the successor is pruned.                 */\\\n\trbtn_right_set(a_type, a_field, pathp->node,\t\t\t\\\n\t  rbtn_right_get(a_type, a_field, node));\t\t\t\\\n\trbtn_color_set(a_type, a_field, node, tred);\t\t\t\\\n\t/* The pruned leaf node's child pointers are never accessed   */\\\n\t/* again, so don't bother setting them to nil.                */\\\n\tnodep->node = pathp->node;\t\t\t\t\t\\\n\tpathp->node = node;\t\t\t\t\t\t\\\n\tif (nodep == path) {\t\t\t\t\t\t\\\n\t    rbtree->rbt_root = nodep->node;\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t    if (nodep[-1].cmp < 0) {\t\t\t\t\t\\\n\t\trbtn_left_set(a_type, a_field, nodep[-1].node,\t\t\\\n\t\t  nodep->node);\t\t\t\t\t\t\\\n\t    } else {\t\t\t\t\t\t\t\\\n\t\trbtn_right_set(a_type, a_field, nodep[-1].node,\t\t\\\n\t\t  nodep->node);\t\t\t\t\t\t\\\n\t    }\t\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n    } else {\t\t\t\t\t\t\t\t\\\n\ta_type *left = rbtn_left_get(a_type, a_field, node);\t\t\\\n\tif (left != NULL) {\t\t\t\t\t\t\\\n\t    /* node has no successor, but it has a left child.        */\\\n\t    /* Splice node out, without losing the left child.        */\\\n\t    assert(!rbtn_red_get(a_type, a_field, node));\t\t\\\n\t    assert(rbtn_red_get(a_type, a_field, left));\t\t\\\n\t    rbtn_black_set(a_type, a_field, left);\t\t\t\\\n\t    if (pathp == path) {\t\t\t\t\t\\\n\t\trbtree->rbt_root = left;\t\t\t\t\\\n\t    } else {\t\t\t\t\t\t\t\\\n\t\tif (pathp[-1].cmp < 0) {\t\t\t\t\\\n\t\t    rbtn_left_set(a_type, a_field, pathp[-1].node,\t\\\n\t\t      left);\t\t\t\t\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t    rbtn_right_set(a_type, a_field, pathp[-1].node,\t\\\n\t\t      left);\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t    }\t\t\t\t\t\t\t\t\\\n\t    return;\t\t\t\t\t\t\t\\\n\t} else if (pathp == path) {\t\t\t\t\t\\\n\t    /* The tree only contained one node. */\t\t\t\\\n\t    rbtree->rbt_root = NULL;\t\t\t\t\t\\\n\t    return;\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n    if (rbtn_red_get(a_type, a_field, pathp->node)) {\t\t\t\\\n\t/* Prune red node, which requires no fixup. */\t\t\t\\\n\tassert(pathp[-1].cmp < 0);\t\t\t\t\t\\\n\trbtn_left_set(a_type, a_field, pathp[-1].node, NULL);\t\t\\\n\treturn;\t\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n    /* The node to be pruned is black, so unwind until balance is     */\\\n    /* restored.                                                      */\\\n    pathp->node = NULL;\t\t\t\t\t\t\t\\\n    for (pathp--; (uintptr_t)pathp >= (uintptr_t)path; pathp--) {\t\\\n\tassert(pathp->cmp != 0);\t\t\t\t\t\\\n\tif (pathp->cmp < 0) {\t\t\t\t\t\t\\\n\t    rbtn_left_set(a_type, a_field, pathp->node,\t\t\t\\\n\t      pathp[1].node);\t\t\t\t\t\t\\\n\t    if (rbtn_red_get(a_type, a_field, pathp->node)) {\t\t\\\n\t\ta_type *right = rbtn_right_get(a_type, a_field,\t\t\\\n\t\t  pathp->node);\t\t\t\t\t\t\\\n\t\ta_type *rightleft = rbtn_left_get(a_type, a_field,\t\\\n\t\t  right);\t\t\t\t\t\t\\\n\t\ta_type *tnode;\t\t\t\t\t\t\\\n\t\tif (rightleft != NULL && rbtn_red_get(a_type, a_field,\t\\\n\t\t  rightleft)) {\t\t\t\t\t\t\\\n\t\t    /* In the following diagrams, ||, //, and \\\\      */\\\n\t\t    /* indicate the path to the removed node.         */\\\n\t\t    /*                                                */\\\n\t\t    /*      ||                                        */\\\n\t\t    /*    pathp(r)                                    */\\\n\t\t    /*  //        \\                                   */\\\n\t\t    /* (b)        (b)                                 */\\\n\t\t    /*           /                                    */\\\n\t\t    /*          (r)                                   */\\\n\t\t    /*                                                */\\\n\t\t    rbtn_black_set(a_type, a_field, pathp->node);\t\\\n\t\t    rbtn_rotate_right(a_type, a_field, right, tnode);\t\\\n\t\t    rbtn_right_set(a_type, a_field, pathp->node, tnode);\\\n\t\t    rbtn_rotate_left(a_type, a_field, pathp->node,\t\\\n\t\t      tnode);\t\t\t\t\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t    /*      ||                                        */\\\n\t\t    /*    pathp(r)                                    */\\\n\t\t    /*  //        \\                                   */\\\n\t\t    /* (b)        (b)                                 */\\\n\t\t    /*           /                                    */\\\n\t\t    /*          (b)                                   */\\\n\t\t    /*                                                */\\\n\t\t    rbtn_rotate_left(a_type, a_field, pathp->node,\t\\\n\t\t      tnode);\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\t/* Balance restored, but rotation modified subtree    */\\\n\t\t/* root.                                              */\\\n\t\tassert((uintptr_t)pathp > (uintptr_t)path);\t\t\\\n\t\tif (pathp[-1].cmp < 0) {\t\t\t\t\\\n\t\t    rbtn_left_set(a_type, a_field, pathp[-1].node,\t\\\n\t\t      tnode);\t\t\t\t\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t    rbtn_right_set(a_type, a_field, pathp[-1].node,\t\\\n\t\t      tnode);\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\treturn;\t\t\t\t\t\t\t\\\n\t    } else {\t\t\t\t\t\t\t\\\n\t\ta_type *right = rbtn_right_get(a_type, a_field,\t\t\\\n\t\t  pathp->node);\t\t\t\t\t\t\\\n\t\ta_type *rightleft = rbtn_left_get(a_type, a_field,\t\\\n\t\t  right);\t\t\t\t\t\t\\\n\t\tif (rightleft != NULL && rbtn_red_get(a_type, a_field,\t\\\n\t\t  rightleft)) {\t\t\t\t\t\t\\\n\t\t    /*      ||                                        */\\\n\t\t    /*    pathp(b)                                    */\\\n\t\t    /*  //        \\                                   */\\\n\t\t    /* (b)        (b)                                 */\\\n\t\t    /*           /                                    */\\\n\t\t    /*          (r)                                   */\\\n\t\t    a_type *tnode;\t\t\t\t\t\\\n\t\t    rbtn_black_set(a_type, a_field, rightleft);\t\t\\\n\t\t    rbtn_rotate_right(a_type, a_field, right, tnode);\t\\\n\t\t    rbtn_right_set(a_type, a_field, pathp->node, tnode);\\\n\t\t    rbtn_rotate_left(a_type, a_field, pathp->node,\t\\\n\t\t      tnode);\t\t\t\t\t\t\\\n\t\t    /* Balance restored, but rotation modified        */\\\n\t\t    /* subtree root, which may actually be the tree   */\\\n\t\t    /* root.                                          */\\\n\t\t    if (pathp == path) {\t\t\t\t\\\n\t\t\t/* Set root. */\t\t\t\t\t\\\n\t\t\trbtree->rbt_root = tnode;\t\t\t\\\n\t\t    } else {\t\t\t\t\t\t\\\n\t\t\tif (pathp[-1].cmp < 0) {\t\t\t\\\n\t\t\t    rbtn_left_set(a_type, a_field,\t\t\\\n\t\t\t      pathp[-1].node, tnode);\t\t\t\\\n\t\t\t} else {\t\t\t\t\t\\\n\t\t\t    rbtn_right_set(a_type, a_field,\t\t\\\n\t\t\t      pathp[-1].node, tnode);\t\t\t\\\n\t\t\t}\t\t\t\t\t\t\\\n\t\t    }\t\t\t\t\t\t\t\\\n\t\t    return;\t\t\t\t\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t    /*      ||                                        */\\\n\t\t    /*    pathp(b)                                    */\\\n\t\t    /*  //        \\                                   */\\\n\t\t    /* (b)        (b)                                 */\\\n\t\t    /*           /                                    */\\\n\t\t    /*          (b)                                   */\\\n\t\t    a_type *tnode;\t\t\t\t\t\\\n\t\t    rbtn_red_set(a_type, a_field, pathp->node);\t\t\\\n\t\t    rbtn_rotate_left(a_type, a_field, pathp->node,\t\\\n\t\t      tnode);\t\t\t\t\t\t\\\n\t\t    pathp->node = tnode;\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t    }\t\t\t\t\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t    a_type *left;\t\t\t\t\t\t\\\n\t    rbtn_right_set(a_type, a_field, pathp->node,\t\t\\\n\t      pathp[1].node);\t\t\t\t\t\t\\\n\t    left = rbtn_left_get(a_type, a_field, pathp->node);\t\t\\\n\t    if (rbtn_red_get(a_type, a_field, left)) {\t\t\t\\\n\t\ta_type *tnode;\t\t\t\t\t\t\\\n\t\ta_type *leftright = rbtn_right_get(a_type, a_field,\t\\\n\t\t  left);\t\t\t\t\t\t\\\n\t\ta_type *leftrightleft = rbtn_left_get(a_type, a_field,\t\\\n\t\t  leftright);\t\t\t\t\t\t\\\n\t\tif (leftrightleft != NULL && rbtn_red_get(a_type,\t\\\n\t\t  a_field, leftrightleft)) {\t\t\t\t\\\n\t\t    /*      ||                                        */\\\n\t\t    /*    pathp(b)                                    */\\\n\t\t    /*   /        \\\\                                  */\\\n\t\t    /* (r)        (b)                                 */\\\n\t\t    /*   \\                                            */\\\n\t\t    /*   (b)                                          */\\\n\t\t    /*   /                                            */\\\n\t\t    /* (r)                                            */\\\n\t\t    a_type *unode;\t\t\t\t\t\\\n\t\t    rbtn_black_set(a_type, a_field, leftrightleft);\t\\\n\t\t    rbtn_rotate_right(a_type, a_field, pathp->node,\t\\\n\t\t      unode);\t\t\t\t\t\t\\\n\t\t    rbtn_rotate_right(a_type, a_field, pathp->node,\t\\\n\t\t      tnode);\t\t\t\t\t\t\\\n\t\t    rbtn_right_set(a_type, a_field, unode, tnode);\t\\\n\t\t    rbtn_rotate_left(a_type, a_field, unode, tnode);\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t    /*      ||                                        */\\\n\t\t    /*    pathp(b)                                    */\\\n\t\t    /*   /        \\\\                                  */\\\n\t\t    /* (r)        (b)                                 */\\\n\t\t    /*   \\                                            */\\\n\t\t    /*   (b)                                          */\\\n\t\t    /*   /                                            */\\\n\t\t    /* (b)                                            */\\\n\t\t    assert(leftright != NULL);\t\t\t\t\\\n\t\t    rbtn_red_set(a_type, a_field, leftright);\t\t\\\n\t\t    rbtn_rotate_right(a_type, a_field, pathp->node,\t\\\n\t\t      tnode);\t\t\t\t\t\t\\\n\t\t    rbtn_black_set(a_type, a_field, tnode);\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\t/* Balance restored, but rotation modified subtree    */\\\n\t\t/* root, which may actually be the tree root.         */\\\n\t\tif (pathp == path) {\t\t\t\t\t\\\n\t\t    /* Set root. */\t\t\t\t\t\\\n\t\t    rbtree->rbt_root = tnode;\t\t\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t    if (pathp[-1].cmp < 0) {\t\t\t\t\\\n\t\t\trbtn_left_set(a_type, a_field, pathp[-1].node,\t\\\n\t\t\t  tnode);\t\t\t\t\t\\\n\t\t    } else {\t\t\t\t\t\t\\\n\t\t\trbtn_right_set(a_type, a_field, pathp[-1].node,\t\\\n\t\t\t  tnode);\t\t\t\t\t\\\n\t\t    }\t\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\treturn;\t\t\t\t\t\t\t\\\n\t    } else if (rbtn_red_get(a_type, a_field, pathp->node)) {\t\\\n\t\ta_type *leftleft = rbtn_left_get(a_type, a_field, left);\\\n\t\tif (leftleft != NULL && rbtn_red_get(a_type, a_field,\t\\\n\t\t  leftleft)) {\t\t\t\t\t\t\\\n\t\t    /*        ||                                      */\\\n\t\t    /*      pathp(r)                                  */\\\n\t\t    /*     /        \\\\                                */\\\n\t\t    /*   (b)        (b)                               */\\\n\t\t    /*   /                                            */\\\n\t\t    /* (r)                                            */\\\n\t\t    a_type *tnode;\t\t\t\t\t\\\n\t\t    rbtn_black_set(a_type, a_field, pathp->node);\t\\\n\t\t    rbtn_red_set(a_type, a_field, left);\t\t\\\n\t\t    rbtn_black_set(a_type, a_field, leftleft);\t\t\\\n\t\t    rbtn_rotate_right(a_type, a_field, pathp->node,\t\\\n\t\t      tnode);\t\t\t\t\t\t\\\n\t\t    /* Balance restored, but rotation modified        */\\\n\t\t    /* subtree root.                                  */\\\n\t\t    assert((uintptr_t)pathp > (uintptr_t)path);\t\t\\\n\t\t    if (pathp[-1].cmp < 0) {\t\t\t\t\\\n\t\t\trbtn_left_set(a_type, a_field, pathp[-1].node,\t\\\n\t\t\t  tnode);\t\t\t\t\t\\\n\t\t    } else {\t\t\t\t\t\t\\\n\t\t\trbtn_right_set(a_type, a_field, pathp[-1].node,\t\\\n\t\t\t  tnode);\t\t\t\t\t\\\n\t\t    }\t\t\t\t\t\t\t\\\n\t\t    return;\t\t\t\t\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t    /*        ||                                      */\\\n\t\t    /*      pathp(r)                                  */\\\n\t\t    /*     /        \\\\                                */\\\n\t\t    /*   (b)        (b)                               */\\\n\t\t    /*   /                                            */\\\n\t\t    /* (b)                                            */\\\n\t\t    rbtn_red_set(a_type, a_field, left);\t\t\\\n\t\t    rbtn_black_set(a_type, a_field, pathp->node);\t\\\n\t\t    /* Balance restored. */\t\t\t\t\\\n\t\t    return;\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t    } else {\t\t\t\t\t\t\t\\\n\t\ta_type *leftleft = rbtn_left_get(a_type, a_field, left);\\\n\t\tif (leftleft != NULL && rbtn_red_get(a_type, a_field,\t\\\n\t\t  leftleft)) {\t\t\t\t\t\t\\\n\t\t    /*               ||                               */\\\n\t\t    /*             pathp(b)                           */\\\n\t\t    /*            /        \\\\                         */\\\n\t\t    /*          (b)        (b)                        */\\\n\t\t    /*          /                                     */\\\n\t\t    /*        (r)                                     */\\\n\t\t    a_type *tnode;\t\t\t\t\t\\\n\t\t    rbtn_black_set(a_type, a_field, leftleft);\t\t\\\n\t\t    rbtn_rotate_right(a_type, a_field, pathp->node,\t\\\n\t\t      tnode);\t\t\t\t\t\t\\\n\t\t    /* Balance restored, but rotation modified        */\\\n\t\t    /* subtree root, which may actually be the tree   */\\\n\t\t    /* root.                                          */\\\n\t\t    if (pathp == path) {\t\t\t\t\\\n\t\t\t/* Set root. */\t\t\t\t\t\\\n\t\t\trbtree->rbt_root = tnode;\t\t\t\\\n\t\t    } else {\t\t\t\t\t\t\\\n\t\t\tif (pathp[-1].cmp < 0) {\t\t\t\\\n\t\t\t    rbtn_left_set(a_type, a_field,\t\t\\\n\t\t\t      pathp[-1].node, tnode);\t\t\t\\\n\t\t\t} else {\t\t\t\t\t\\\n\t\t\t    rbtn_right_set(a_type, a_field,\t\t\\\n\t\t\t      pathp[-1].node, tnode);\t\t\t\\\n\t\t\t}\t\t\t\t\t\t\\\n\t\t    }\t\t\t\t\t\t\t\\\n\t\t    return;\t\t\t\t\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t    /*               ||                               */\\\n\t\t    /*             pathp(b)                           */\\\n\t\t    /*            /        \\\\                         */\\\n\t\t    /*          (b)        (b)                        */\\\n\t\t    /*          /                                     */\\\n\t\t    /*        (b)                                     */\\\n\t\t    rbtn_red_set(a_type, a_field, left);\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t    }\t\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n    /* Set root. */\t\t\t\t\t\t\t\\\n    rbtree->rbt_root = path->node;\t\t\t\t\t\\\n    assert(!rbtn_red_get(a_type, a_field, rbtree->rbt_root));\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##iter_recurse(a_rbt_type *rbtree, a_type *node,\t\t\\\n  a_type *(*cb)(a_rbt_type *, a_type *, void *), void *arg) {\t\t\\\n    if (node == NULL) {\t\t\t\t\t\t\t\\\n\treturn NULL;\t\t\t\t\t\t\t\\\n    } else {\t\t\t\t\t\t\t\t\\\n\ta_type *ret;\t\t\t\t\t\t\t\\\n\tif ((ret = a_prefix##iter_recurse(rbtree, rbtn_left_get(a_type,\t\\\n\t  a_field, node), cb, arg)) != NULL || (ret = cb(rbtree, node,\t\\\n\t  arg)) != NULL) {\t\t\t\t\t\t\\\n\t    return ret;\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\treturn a_prefix##iter_recurse(rbtree, rbtn_right_get(a_type,\t\\\n\t  a_field, node), cb, arg);\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##iter_start(a_rbt_type *rbtree, a_type *start, a_type *node,\t\\\n  a_type *(*cb)(a_rbt_type *, a_type *, void *), void *arg) {\t\t\\\n    int cmp = a_cmp(start, node);\t\t\t\t\t\\\n    if (cmp < 0) {\t\t\t\t\t\t\t\\\n\ta_type *ret;\t\t\t\t\t\t\t\\\n\tif ((ret = a_prefix##iter_start(rbtree, start,\t\t\t\\\n\t  rbtn_left_get(a_type, a_field, node), cb, arg)) != NULL ||\t\\\n\t  (ret = cb(rbtree, node, arg)) != NULL) {\t\t\t\\\n\t    return ret;\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\treturn a_prefix##iter_recurse(rbtree, rbtn_right_get(a_type,\t\\\n\t  a_field, node), cb, arg);\t\t\t\t\t\\\n    } else if (cmp > 0) {\t\t\t\t\t\t\\\n\treturn a_prefix##iter_start(rbtree, start,\t\t\t\\\n\t  rbtn_right_get(a_type, a_field, node), cb, arg);\t\t\\\n    } else {\t\t\t\t\t\t\t\t\\\n\ta_type *ret;\t\t\t\t\t\t\t\\\n\tif ((ret = cb(rbtree, node, arg)) != NULL) {\t\t\t\\\n\t    return ret;\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\treturn a_prefix##iter_recurse(rbtree, rbtn_right_get(a_type,\t\\\n\t  a_field, node), cb, arg);\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##iter(a_rbt_type *rbtree, a_type *start, a_type *(*cb)(\t\\\n  a_rbt_type *, a_type *, void *), void *arg) {\t\t\t\t\\\n    a_type *ret;\t\t\t\t\t\t\t\\\n    if (start != NULL) {\t\t\t\t\t\t\\\n\tret = a_prefix##iter_start(rbtree, start, rbtree->rbt_root,\t\\\n\t  cb, arg);\t\t\t\t\t\t\t\\\n    } else {\t\t\t\t\t\t\t\t\\\n\tret = a_prefix##iter_recurse(rbtree, rbtree->rbt_root, cb, arg);\\\n    }\t\t\t\t\t\t\t\t\t\\\n    return ret;\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##reverse_iter_recurse(a_rbt_type *rbtree, a_type *node,\t\\\n  a_type *(*cb)(a_rbt_type *, a_type *, void *), void *arg) {\t\t\\\n    if (node == NULL) {\t\t\t\t\t\t\t\\\n\treturn NULL;\t\t\t\t\t\t\t\\\n    } else {\t\t\t\t\t\t\t\t\\\n\ta_type *ret;\t\t\t\t\t\t\t\\\n\tif ((ret = a_prefix##reverse_iter_recurse(rbtree,\t\t\\\n\t  rbtn_right_get(a_type, a_field, node), cb, arg)) != NULL ||\t\\\n\t  (ret = cb(rbtree, node, arg)) != NULL) {\t\t\t\\\n\t    return ret;\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\treturn a_prefix##reverse_iter_recurse(rbtree,\t\t\t\\\n\t  rbtn_left_get(a_type, a_field, node), cb, arg);\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##reverse_iter_start(a_rbt_type *rbtree, a_type *start,\t\t\\\n  a_type *node, a_type *(*cb)(a_rbt_type *, a_type *, void *),\t\t\\\n  void *arg) {\t\t\t\t\t\t\t\t\\\n    int cmp = a_cmp(start, node);\t\t\t\t\t\\\n    if (cmp > 0) {\t\t\t\t\t\t\t\\\n\ta_type *ret;\t\t\t\t\t\t\t\\\n\tif ((ret = a_prefix##reverse_iter_start(rbtree, start,\t\t\\\n\t  rbtn_right_get(a_type, a_field, node), cb, arg)) != NULL ||\t\\\n\t  (ret = cb(rbtree, node, arg)) != NULL) {\t\t\t\\\n\t    return ret;\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\treturn a_prefix##reverse_iter_recurse(rbtree,\t\t\t\\\n\t  rbtn_left_get(a_type, a_field, node), cb, arg);\t\t\\\n    } else if (cmp < 0) {\t\t\t\t\t\t\\\n\treturn a_prefix##reverse_iter_start(rbtree, start,\t\t\\\n\t  rbtn_left_get(a_type, a_field, node), cb, arg);\t\t\\\n    } else {\t\t\t\t\t\t\t\t\\\n\ta_type *ret;\t\t\t\t\t\t\t\\\n\tif ((ret = cb(rbtree, node, arg)) != NULL) {\t\t\t\\\n\t    return ret;\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\treturn a_prefix##reverse_iter_recurse(rbtree,\t\t\t\\\n\t  rbtn_left_get(a_type, a_field, node), cb, arg);\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##reverse_iter(a_rbt_type *rbtree, a_type *start,\t\t\\\n  a_type *(*cb)(a_rbt_type *, a_type *, void *), void *arg) {\t\t\\\n    a_type *ret;\t\t\t\t\t\t\t\\\n    if (start != NULL) {\t\t\t\t\t\t\\\n\tret = a_prefix##reverse_iter_start(rbtree, start,\t\t\\\n\t  rbtree->rbt_root, cb, arg);\t\t\t\t\t\\\n    } else {\t\t\t\t\t\t\t\t\\\n\tret = a_prefix##reverse_iter_recurse(rbtree, rbtree->rbt_root,\t\\\n\t  cb, arg);\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n    return ret;\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##destroy_recurse(a_rbt_type *rbtree, a_type *node, void (*cb)(\t\\\n  a_type *, void *), void *arg) {\t\t\t\t\t\\\n    if (node == NULL) {\t\t\t\t\t\t\t\\\n\treturn;\t\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n    a_prefix##destroy_recurse(rbtree, rbtn_left_get(a_type, a_field,\t\\\n      node), cb, arg);\t\t\t\t\t\t\t\\\n    rbtn_left_set(a_type, a_field, (node), NULL);\t\t\t\\\n    a_prefix##destroy_recurse(rbtree, rbtn_right_get(a_type, a_field,\t\\\n      node), cb, arg);\t\t\t\t\t\t\t\\\n    rbtn_right_set(a_type, a_field, (node), NULL);\t\t\t\\\n    if (cb) {\t\t\t\t\t\t\t\t\\\n\tcb(node, arg);\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##destroy(a_rbt_type *rbtree, void (*cb)(a_type *, void *),\t\\\n  void *arg) {\t\t\t\t\t\t\t\t\\\n    a_prefix##destroy_recurse(rbtree, rbtree->rbt_root, cb, arg);\t\\\n    rbtree->rbt_root = NULL;\t\t\t\t\t\t\\\n}\n\n#endif /* RB_H_ */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/rtree.h",
    "content": "#ifndef JEMALLOC_INTERNAL_RTREE_H\n#define JEMALLOC_INTERNAL_RTREE_H\n\n#include \"jemalloc/internal/atomic.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/rtree_tsd.h\"\n#include \"jemalloc/internal/sc.h\"\n#include \"jemalloc/internal/tsd.h\"\n\n/*\n * This radix tree implementation is tailored to the singular purpose of\n * associating metadata with extents that are currently owned by jemalloc.\n *\n *******************************************************************************\n */\n\n/* Number of high insignificant bits. */\n#define RTREE_NHIB ((1U << (LG_SIZEOF_PTR+3)) - LG_VADDR)\n/* Number of low insigificant bits. */\n#define RTREE_NLIB LG_PAGE\n/* Number of significant bits. */\n#define RTREE_NSB (LG_VADDR - RTREE_NLIB)\n/* Number of levels in radix tree. */\n#if RTREE_NSB <= 10\n#  define RTREE_HEIGHT 1\n#elif RTREE_NSB <= 36\n#  define RTREE_HEIGHT 2\n#elif RTREE_NSB <= 52\n#  define RTREE_HEIGHT 3\n#else\n#  error Unsupported number of significant virtual address bits\n#endif\n/* Use compact leaf representation if virtual address encoding allows. */\n#if RTREE_NHIB >= LG_CEIL(SC_NSIZES)\n#  define RTREE_LEAF_COMPACT\n#endif\n\n/* Needed for initialization only. */\n#define RTREE_LEAFKEY_INVALID ((uintptr_t)1)\n\ntypedef struct rtree_node_elm_s rtree_node_elm_t;\nstruct rtree_node_elm_s {\n\tatomic_p_t\tchild; /* (rtree_{node,leaf}_elm_t *) */\n};\n\nstruct rtree_leaf_elm_s {\n#ifdef RTREE_LEAF_COMPACT\n\t/*\n\t * Single pointer-width field containing all three leaf element fields.\n\t * For example, on a 64-bit x64 system with 48 significant virtual\n\t * memory address bits, the index, extent, and slab fields are packed as\n\t * such:\n\t *\n\t * x: index\n\t * e: extent\n\t * b: slab\n\t *\n\t *   00000000 xxxxxxxx eeeeeeee [...] eeeeeeee eeee000b\n\t */\n\tatomic_p_t\tle_bits;\n#else\n\tatomic_p_t\tle_extent; /* (extent_t *) */\n\tatomic_u_t\tle_szind; /* (szind_t) */\n\tatomic_b_t\tle_slab; /* (bool) */\n#endif\n};\n\ntypedef struct rtree_level_s rtree_level_t;\nstruct rtree_level_s {\n\t/* Number of key bits distinguished by this level. */\n\tunsigned\t\tbits;\n\t/*\n\t * Cumulative number of key bits distinguished by traversing to\n\t * corresponding tree level.\n\t */\n\tunsigned\t\tcumbits;\n};\n\ntypedef struct rtree_s rtree_t;\nstruct rtree_s {\n\tmalloc_mutex_t\t\tinit_lock;\n\t/* Number of elements based on rtree_levels[0].bits. */\n#if RTREE_HEIGHT > 1\n\trtree_node_elm_t\troot[1U << (RTREE_NSB/RTREE_HEIGHT)];\n#else\n\trtree_leaf_elm_t\troot[1U << (RTREE_NSB/RTREE_HEIGHT)];\n#endif\n};\n\n/*\n * Split the bits into one to three partitions depending on number of\n * significant bits.  It the number of bits does not divide evenly into the\n * number of levels, place one remainder bit per level starting at the leaf\n * level.\n */\nstatic const rtree_level_t rtree_levels[] = {\n#if RTREE_HEIGHT == 1\n\t{RTREE_NSB, RTREE_NHIB + RTREE_NSB}\n#elif RTREE_HEIGHT == 2\n\t{RTREE_NSB/2, RTREE_NHIB + RTREE_NSB/2},\n\t{RTREE_NSB/2 + RTREE_NSB%2, RTREE_NHIB + RTREE_NSB}\n#elif RTREE_HEIGHT == 3\n\t{RTREE_NSB/3, RTREE_NHIB + RTREE_NSB/3},\n\t{RTREE_NSB/3 + RTREE_NSB%3/2,\n\t    RTREE_NHIB + RTREE_NSB/3*2 + RTREE_NSB%3/2},\n\t{RTREE_NSB/3 + RTREE_NSB%3 - RTREE_NSB%3/2, RTREE_NHIB + RTREE_NSB}\n#else\n#  error Unsupported rtree height\n#endif\n};\n\nbool rtree_new(rtree_t *rtree, bool zeroed);\n\ntypedef rtree_node_elm_t *(rtree_node_alloc_t)(tsdn_t *, rtree_t *, size_t);\nextern rtree_node_alloc_t *JET_MUTABLE rtree_node_alloc;\n\ntypedef rtree_leaf_elm_t *(rtree_leaf_alloc_t)(tsdn_t *, rtree_t *, size_t);\nextern rtree_leaf_alloc_t *JET_MUTABLE rtree_leaf_alloc;\n\ntypedef void (rtree_node_dalloc_t)(tsdn_t *, rtree_t *, rtree_node_elm_t *);\nextern rtree_node_dalloc_t *JET_MUTABLE rtree_node_dalloc;\n\ntypedef void (rtree_leaf_dalloc_t)(tsdn_t *, rtree_t *, rtree_leaf_elm_t *);\nextern rtree_leaf_dalloc_t *JET_MUTABLE rtree_leaf_dalloc;\n#ifdef JEMALLOC_JET\nvoid rtree_delete(tsdn_t *tsdn, rtree_t *rtree);\n#endif\nrtree_leaf_elm_t *rtree_leaf_elm_lookup_hard(tsdn_t *tsdn, rtree_t *rtree,\n    rtree_ctx_t *rtree_ctx, uintptr_t key, bool dependent, bool init_missing);\n\nJEMALLOC_ALWAYS_INLINE uintptr_t\nrtree_leafkey(uintptr_t key) {\n\tunsigned ptrbits = ZU(1) << (LG_SIZEOF_PTR+3);\n\tunsigned cumbits = (rtree_levels[RTREE_HEIGHT-1].cumbits -\n\t    rtree_levels[RTREE_HEIGHT-1].bits);\n\tunsigned maskbits = ptrbits - cumbits;\n\tuintptr_t mask = ~((ZU(1) << maskbits) - 1);\n\treturn (key & mask);\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\nrtree_cache_direct_map(uintptr_t key) {\n\tunsigned ptrbits = ZU(1) << (LG_SIZEOF_PTR+3);\n\tunsigned cumbits = (rtree_levels[RTREE_HEIGHT-1].cumbits -\n\t    rtree_levels[RTREE_HEIGHT-1].bits);\n\tunsigned maskbits = ptrbits - cumbits;\n\treturn (size_t)((key >> maskbits) & (RTREE_CTX_NCACHE - 1));\n}\n\nJEMALLOC_ALWAYS_INLINE uintptr_t\nrtree_subkey(uintptr_t key, unsigned level) {\n\tunsigned ptrbits = ZU(1) << (LG_SIZEOF_PTR+3);\n\tunsigned cumbits = rtree_levels[level].cumbits;\n\tunsigned shiftbits = ptrbits - cumbits;\n\tunsigned maskbits = rtree_levels[level].bits;\n\tuintptr_t mask = (ZU(1) << maskbits) - 1;\n\treturn ((key >> shiftbits) & mask);\n}\n\n/*\n * Atomic getters.\n *\n * dependent: Reading a value on behalf of a pointer to a valid allocation\n *            is guaranteed to be a clean read even without synchronization,\n *            because the rtree update became visible in memory before the\n *            pointer came into existence.\n * !dependent: An arbitrary read, e.g. on behalf of ivsalloc(), may not be\n *             dependent on a previous rtree write, which means a stale read\n *             could result if synchronization were omitted here.\n */\n#  ifdef RTREE_LEAF_COMPACT\nJEMALLOC_ALWAYS_INLINE uintptr_t\nrtree_leaf_elm_bits_read(tsdn_t *tsdn, rtree_t *rtree,\n    rtree_leaf_elm_t *elm, bool dependent) {\n\treturn (uintptr_t)atomic_load_p(&elm->le_bits, dependent\n\t    ? ATOMIC_RELAXED : ATOMIC_ACQUIRE);\n}\n\nJEMALLOC_ALWAYS_INLINE extent_t *\nrtree_leaf_elm_bits_extent_get(uintptr_t bits) {\n#    ifdef __aarch64__\n\t/*\n\t * aarch64 doesn't sign extend the highest virtual address bit to set\n\t * the higher ones.  Instead, the high bits gets zeroed.\n\t */\n\tuintptr_t high_bit_mask = ((uintptr_t)1 << LG_VADDR) - 1;\n\t/* Mask off the slab bit. */\n\tuintptr_t low_bit_mask = ~(uintptr_t)1;\n\tuintptr_t mask = high_bit_mask & low_bit_mask;\n\treturn (extent_t *)(bits & mask);\n#    else\n\t/* Restore sign-extended high bits, mask slab bit. */\n\treturn (extent_t *)((uintptr_t)((intptr_t)(bits << RTREE_NHIB) >>\n\t    RTREE_NHIB) & ~((uintptr_t)0x1));\n#    endif\n}\n\nJEMALLOC_ALWAYS_INLINE szind_t\nrtree_leaf_elm_bits_szind_get(uintptr_t bits) {\n\treturn (szind_t)(bits >> LG_VADDR);\n}\n\nJEMALLOC_ALWAYS_INLINE bool\nrtree_leaf_elm_bits_slab_get(uintptr_t bits) {\n\treturn (bool)(bits & (uintptr_t)0x1);\n}\n\n#  endif\n\nJEMALLOC_ALWAYS_INLINE extent_t *\nrtree_leaf_elm_extent_read(tsdn_t *tsdn, rtree_t *rtree,\n    rtree_leaf_elm_t *elm, bool dependent) {\n#ifdef RTREE_LEAF_COMPACT\n\tuintptr_t bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm, dependent);\n\treturn rtree_leaf_elm_bits_extent_get(bits);\n#else\n\textent_t *extent = (extent_t *)atomic_load_p(&elm->le_extent, dependent\n\t    ? ATOMIC_RELAXED : ATOMIC_ACQUIRE);\n\treturn extent;\n#endif\n}\n\nJEMALLOC_ALWAYS_INLINE szind_t\nrtree_leaf_elm_szind_read(tsdn_t *tsdn, rtree_t *rtree,\n    rtree_leaf_elm_t *elm, bool dependent) {\n#ifdef RTREE_LEAF_COMPACT\n\tuintptr_t bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm, dependent);\n\treturn rtree_leaf_elm_bits_szind_get(bits);\n#else\n\treturn (szind_t)atomic_load_u(&elm->le_szind, dependent ? ATOMIC_RELAXED\n\t    : ATOMIC_ACQUIRE);\n#endif\n}\n\nJEMALLOC_ALWAYS_INLINE bool\nrtree_leaf_elm_slab_read(tsdn_t *tsdn, rtree_t *rtree,\n    rtree_leaf_elm_t *elm, bool dependent) {\n#ifdef RTREE_LEAF_COMPACT\n\tuintptr_t bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm, dependent);\n\treturn rtree_leaf_elm_bits_slab_get(bits);\n#else\n\treturn atomic_load_b(&elm->le_slab, dependent ? ATOMIC_RELAXED :\n\t    ATOMIC_ACQUIRE);\n#endif\n}\n\nstatic inline void\nrtree_leaf_elm_extent_write(tsdn_t *tsdn, rtree_t *rtree,\n    rtree_leaf_elm_t *elm, extent_t *extent) {\n#ifdef RTREE_LEAF_COMPACT\n\tuintptr_t old_bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm, true);\n\tuintptr_t bits = ((uintptr_t)rtree_leaf_elm_bits_szind_get(old_bits) <<\n\t    LG_VADDR) | ((uintptr_t)extent & (((uintptr_t)0x1 << LG_VADDR) - 1))\n\t    | ((uintptr_t)rtree_leaf_elm_bits_slab_get(old_bits));\n\tatomic_store_p(&elm->le_bits, (void *)bits, ATOMIC_RELEASE);\n#else\n\tatomic_store_p(&elm->le_extent, extent, ATOMIC_RELEASE);\n#endif\n}\n\nstatic inline void\nrtree_leaf_elm_szind_write(tsdn_t *tsdn, rtree_t *rtree,\n    rtree_leaf_elm_t *elm, szind_t szind) {\n\tassert(szind <= SC_NSIZES);\n\n#ifdef RTREE_LEAF_COMPACT\n\tuintptr_t old_bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm,\n\t    true);\n\tuintptr_t bits = ((uintptr_t)szind << LG_VADDR) |\n\t    ((uintptr_t)rtree_leaf_elm_bits_extent_get(old_bits) &\n\t    (((uintptr_t)0x1 << LG_VADDR) - 1)) |\n\t    ((uintptr_t)rtree_leaf_elm_bits_slab_get(old_bits));\n\tatomic_store_p(&elm->le_bits, (void *)bits, ATOMIC_RELEASE);\n#else\n\tatomic_store_u(&elm->le_szind, szind, ATOMIC_RELEASE);\n#endif\n}\n\nstatic inline void\nrtree_leaf_elm_slab_write(tsdn_t *tsdn, rtree_t *rtree,\n    rtree_leaf_elm_t *elm, bool slab) {\n#ifdef RTREE_LEAF_COMPACT\n\tuintptr_t old_bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm,\n\t    true);\n\tuintptr_t bits = ((uintptr_t)rtree_leaf_elm_bits_szind_get(old_bits) <<\n\t    LG_VADDR) | ((uintptr_t)rtree_leaf_elm_bits_extent_get(old_bits) &\n\t    (((uintptr_t)0x1 << LG_VADDR) - 1)) | ((uintptr_t)slab);\n\tatomic_store_p(&elm->le_bits, (void *)bits, ATOMIC_RELEASE);\n#else\n\tatomic_store_b(&elm->le_slab, slab, ATOMIC_RELEASE);\n#endif\n}\n\nstatic inline void\nrtree_leaf_elm_write(tsdn_t *tsdn, rtree_t *rtree,\n    rtree_leaf_elm_t *elm, extent_t *extent, szind_t szind, bool slab) {\n#ifdef RTREE_LEAF_COMPACT\n\tuintptr_t bits = ((uintptr_t)szind << LG_VADDR) |\n\t    ((uintptr_t)extent & (((uintptr_t)0x1 << LG_VADDR) - 1)) |\n\t    ((uintptr_t)slab);\n\tatomic_store_p(&elm->le_bits, (void *)bits, ATOMIC_RELEASE);\n#else\n\trtree_leaf_elm_slab_write(tsdn, rtree, elm, slab);\n\trtree_leaf_elm_szind_write(tsdn, rtree, elm, szind);\n\t/*\n\t * Write extent last, since the element is atomically considered valid\n\t * as soon as the extent field is non-NULL.\n\t */\n\trtree_leaf_elm_extent_write(tsdn, rtree, elm, extent);\n#endif\n}\n\nstatic inline void\nrtree_leaf_elm_szind_slab_update(tsdn_t *tsdn, rtree_t *rtree,\n    rtree_leaf_elm_t *elm, szind_t szind, bool slab) {\n\tassert(!slab || szind < SC_NBINS);\n\n\t/*\n\t * The caller implicitly assures that it is the only writer to the szind\n\t * and slab fields, and that the extent field cannot currently change.\n\t */\n\trtree_leaf_elm_slab_write(tsdn, rtree, elm, slab);\n\trtree_leaf_elm_szind_write(tsdn, rtree, elm, szind);\n}\n\nJEMALLOC_ALWAYS_INLINE rtree_leaf_elm_t *\nrtree_leaf_elm_lookup(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,\n    uintptr_t key, bool dependent, bool init_missing) {\n\tassert(key != 0);\n\tassert(!dependent || !init_missing);\n\n\tsize_t slot = rtree_cache_direct_map(key);\n\tuintptr_t leafkey = rtree_leafkey(key);\n\tassert(leafkey != RTREE_LEAFKEY_INVALID);\n\n\t/* Fast path: L1 direct mapped cache. */\n\tif (likely(rtree_ctx->cache[slot].leafkey == leafkey)) {\n\t\trtree_leaf_elm_t *leaf = rtree_ctx->cache[slot].leaf;\n\t\tassert(leaf != NULL);\n\t\tuintptr_t subkey = rtree_subkey(key, RTREE_HEIGHT-1);\n\t\treturn &leaf[subkey];\n\t}\n\t/*\n\t * Search the L2 LRU cache.  On hit, swap the matching element into the\n\t * slot in L1 cache, and move the position in L2 up by 1.\n\t */\n#define RTREE_CACHE_CHECK_L2(i) do {\t\t\t\t\t\\\n\tif (likely(rtree_ctx->l2_cache[i].leafkey == leafkey)) {\t\\\n\t\trtree_leaf_elm_t *leaf = rtree_ctx->l2_cache[i].leaf;\t\\\n\t\tassert(leaf != NULL);\t\t\t\t\t\\\n\t\tif (i > 0) {\t\t\t\t\t\t\\\n\t\t\t/* Bubble up by one. */\t\t\t\t\\\n\t\t\trtree_ctx->l2_cache[i].leafkey =\t\t\\\n\t\t\t\trtree_ctx->l2_cache[i - 1].leafkey;\t\\\n\t\t\trtree_ctx->l2_cache[i].leaf =\t\t\t\\\n\t\t\t\trtree_ctx->l2_cache[i - 1].leaf;\t\\\n\t\t\trtree_ctx->l2_cache[i - 1].leafkey =\t\t\\\n\t\t\t    rtree_ctx->cache[slot].leafkey;\t\t\\\n\t\t\trtree_ctx->l2_cache[i - 1].leaf =\t\t\\\n\t\t\t    rtree_ctx->cache[slot].leaf;\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t\trtree_ctx->l2_cache[0].leafkey =\t\t\\\n\t\t\t    rtree_ctx->cache[slot].leafkey;\t\t\\\n\t\t\trtree_ctx->l2_cache[0].leaf =\t\t\t\\\n\t\t\t    rtree_ctx->cache[slot].leaf;\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\trtree_ctx->cache[slot].leafkey = leafkey;\t\t\\\n\t\trtree_ctx->cache[slot].leaf = leaf;\t\t\t\\\n\t\tuintptr_t subkey = rtree_subkey(key, RTREE_HEIGHT-1);\t\\\n\t\treturn &leaf[subkey];\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\t/* Check the first cache entry. */\n\tRTREE_CACHE_CHECK_L2(0);\n\t/* Search the remaining cache elements. */\n\tfor (unsigned i = 1; i < RTREE_CTX_NCACHE_L2; i++) {\n\t\tRTREE_CACHE_CHECK_L2(i);\n\t}\n#undef RTREE_CACHE_CHECK_L2\n\n\treturn rtree_leaf_elm_lookup_hard(tsdn, rtree, rtree_ctx, key,\n\t    dependent, init_missing);\n}\n\nstatic inline bool\nrtree_write(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx, uintptr_t key,\n    extent_t *extent, szind_t szind, bool slab) {\n\t/* Use rtree_clear() to set the extent to NULL. */\n\tassert(extent != NULL);\n\n\trtree_leaf_elm_t *elm = rtree_leaf_elm_lookup(tsdn, rtree, rtree_ctx,\n\t    key, false, true);\n\tif (elm == NULL) {\n\t\treturn true;\n\t}\n\n\tassert(rtree_leaf_elm_extent_read(tsdn, rtree, elm, false) == NULL);\n\trtree_leaf_elm_write(tsdn, rtree, elm, extent, szind, slab);\n\n\treturn false;\n}\n\nJEMALLOC_ALWAYS_INLINE rtree_leaf_elm_t *\nrtree_read(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx, uintptr_t key,\n    bool dependent) {\n\trtree_leaf_elm_t *elm = rtree_leaf_elm_lookup(tsdn, rtree, rtree_ctx,\n\t    key, dependent, false);\n\tif (!dependent && elm == NULL) {\n\t\treturn NULL;\n\t}\n\tassert(elm != NULL);\n\treturn elm;\n}\n\nJEMALLOC_ALWAYS_INLINE extent_t *\nrtree_extent_read(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,\n    uintptr_t key, bool dependent) {\n\trtree_leaf_elm_t *elm = rtree_read(tsdn, rtree, rtree_ctx, key,\n\t    dependent);\n\tif (!dependent && elm == NULL) {\n\t\treturn NULL;\n\t}\n\treturn rtree_leaf_elm_extent_read(tsdn, rtree, elm, dependent);\n}\n\nJEMALLOC_ALWAYS_INLINE szind_t\nrtree_szind_read(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,\n    uintptr_t key, bool dependent) {\n\trtree_leaf_elm_t *elm = rtree_read(tsdn, rtree, rtree_ctx, key,\n\t    dependent);\n\tif (!dependent && elm == NULL) {\n\t\treturn SC_NSIZES;\n\t}\n\treturn rtree_leaf_elm_szind_read(tsdn, rtree, elm, dependent);\n}\n\n/*\n * rtree_slab_read() is intentionally omitted because slab is always read in\n * conjunction with szind, which makes rtree_szind_slab_read() a better choice.\n */\n\nJEMALLOC_ALWAYS_INLINE bool\nrtree_extent_szind_read(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,\n    uintptr_t key, bool dependent, extent_t **r_extent, szind_t *r_szind) {\n\trtree_leaf_elm_t *elm = rtree_read(tsdn, rtree, rtree_ctx, key,\n\t    dependent);\n\tif (!dependent && elm == NULL) {\n\t\treturn true;\n\t}\n\t*r_extent = rtree_leaf_elm_extent_read(tsdn, rtree, elm, dependent);\n\t*r_szind = rtree_leaf_elm_szind_read(tsdn, rtree, elm, dependent);\n\treturn false;\n}\n\n/*\n * Try to read szind_slab from the L1 cache.  Returns true on a hit,\n * and fills in r_szind and r_slab.  Otherwise returns false.\n *\n * Key is allowed to be NULL in order to save an extra branch on the\n * fastpath.  returns false in this case.\n */\nJEMALLOC_ALWAYS_INLINE bool\nrtree_szind_slab_read_fast(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,\n\t\t\t    uintptr_t key, szind_t *r_szind, bool *r_slab) {\n\trtree_leaf_elm_t *elm;\n\n\tsize_t slot = rtree_cache_direct_map(key);\n\tuintptr_t leafkey = rtree_leafkey(key);\n\tassert(leafkey != RTREE_LEAFKEY_INVALID);\n\n\tif (likely(rtree_ctx->cache[slot].leafkey == leafkey)) {\n\t\trtree_leaf_elm_t *leaf = rtree_ctx->cache[slot].leaf;\n\t\tassert(leaf != NULL);\n\t\tuintptr_t subkey = rtree_subkey(key, RTREE_HEIGHT-1);\n\t\telm = &leaf[subkey];\n\n#ifdef RTREE_LEAF_COMPACT\n\t\tuintptr_t bits = rtree_leaf_elm_bits_read(tsdn, rtree,\n\t\t\t\t\t\t\t  elm, true);\n\t\t*r_szind = rtree_leaf_elm_bits_szind_get(bits);\n\t\t*r_slab = rtree_leaf_elm_bits_slab_get(bits);\n#else\n\t\t*r_szind = rtree_leaf_elm_szind_read(tsdn, rtree, elm, true);\n\t\t*r_slab = rtree_leaf_elm_slab_read(tsdn, rtree, elm, true);\n#endif\n\t\treturn true;\n\t} else {\n\t\treturn false;\n\t}\n}\nJEMALLOC_ALWAYS_INLINE bool\nrtree_szind_slab_read(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,\n    uintptr_t key, bool dependent, szind_t *r_szind, bool *r_slab) {\n\trtree_leaf_elm_t *elm = rtree_read(tsdn, rtree, rtree_ctx, key,\n\t    dependent);\n\tif (!dependent && elm == NULL) {\n\t\treturn true;\n\t}\n#ifdef RTREE_LEAF_COMPACT\n\tuintptr_t bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm, dependent);\n\t*r_szind = rtree_leaf_elm_bits_szind_get(bits);\n\t*r_slab = rtree_leaf_elm_bits_slab_get(bits);\n#else\n\t*r_szind = rtree_leaf_elm_szind_read(tsdn, rtree, elm, dependent);\n\t*r_slab = rtree_leaf_elm_slab_read(tsdn, rtree, elm, dependent);\n#endif\n\treturn false;\n}\n\nstatic inline void\nrtree_szind_slab_update(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,\n    uintptr_t key, szind_t szind, bool slab) {\n\tassert(!slab || szind < SC_NBINS);\n\n\trtree_leaf_elm_t *elm = rtree_read(tsdn, rtree, rtree_ctx, key, true);\n\trtree_leaf_elm_szind_slab_update(tsdn, rtree, elm, szind, slab);\n}\n\nstatic inline void\nrtree_clear(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,\n    uintptr_t key) {\n\trtree_leaf_elm_t *elm = rtree_read(tsdn, rtree, rtree_ctx, key, true);\n\tassert(rtree_leaf_elm_extent_read(tsdn, rtree, elm, false) !=\n\t    NULL);\n\trtree_leaf_elm_write(tsdn, rtree, elm, NULL, SC_NSIZES, false);\n}\n\n#endif /* JEMALLOC_INTERNAL_RTREE_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/rtree_tsd.h",
    "content": "#ifndef JEMALLOC_INTERNAL_RTREE_CTX_H\n#define JEMALLOC_INTERNAL_RTREE_CTX_H\n\n/*\n * Number of leafkey/leaf pairs to cache in L1 and L2 level respectively.  Each\n * entry supports an entire leaf, so the cache hit rate is typically high even\n * with a small number of entries.  In rare cases extent activity will straddle\n * the boundary between two leaf nodes.  Furthermore, an arena may use a\n * combination of dss and mmap.  Note that as memory usage grows past the amount\n * that this cache can directly cover, the cache will become less effective if\n * locality of reference is low, but the consequence is merely cache misses\n * while traversing the tree nodes.\n *\n * The L1 direct mapped cache offers consistent and low cost on cache hit.\n * However collision could affect hit rate negatively.  This is resolved by\n * combining with a L2 LRU cache, which requires linear search and re-ordering\n * on access but suffers no collision.  Note that, the cache will itself suffer\n * cache misses if made overly large, plus the cost of linear search in the LRU\n * cache.\n */\n#define RTREE_CTX_LG_NCACHE 4\n#define RTREE_CTX_NCACHE (1 << RTREE_CTX_LG_NCACHE)\n#define RTREE_CTX_NCACHE_L2 8\n\n/*\n * Zero initializer required for tsd initialization only.  Proper initialization\n * done via rtree_ctx_data_init().\n */\n#define RTREE_CTX_ZERO_INITIALIZER {{{0, 0}}, {{0, 0}}}\n\n\ntypedef struct rtree_leaf_elm_s rtree_leaf_elm_t;\n\ntypedef struct rtree_ctx_cache_elm_s rtree_ctx_cache_elm_t;\nstruct rtree_ctx_cache_elm_s {\n\tuintptr_t\t\tleafkey;\n\trtree_leaf_elm_t\t*leaf;\n};\n\ntypedef struct rtree_ctx_s rtree_ctx_t;\nstruct rtree_ctx_s {\n\t/* Direct mapped cache. */\n\trtree_ctx_cache_elm_t\tcache[RTREE_CTX_NCACHE];\n\t/* L2 LRU cache. */\n\trtree_ctx_cache_elm_t\tl2_cache[RTREE_CTX_NCACHE_L2];\n};\n\nvoid rtree_ctx_data_init(rtree_ctx_t *ctx);\n\n#endif /* JEMALLOC_INTERNAL_RTREE_CTX_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/safety_check.h",
    "content": "#ifndef JEMALLOC_INTERNAL_SAFETY_CHECK_H\n#define JEMALLOC_INTERNAL_SAFETY_CHECK_H\n\nvoid safety_check_fail(const char *format, ...);\n/* Can set to NULL for a default. */\nvoid safety_check_set_abort(void (*abort_fn)());\n\nJEMALLOC_ALWAYS_INLINE void\nsafety_check_set_redzone(void *ptr, size_t usize, size_t bumped_usize) {\n\tassert(usize < bumped_usize);\n\tfor (size_t i = usize; i < bumped_usize && i < usize + 32; ++i) {\n\t\t*((unsigned char *)ptr + i) = 0xBC;\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE void\nsafety_check_verify_redzone(const void *ptr, size_t usize, size_t bumped_usize)\n{\n\tfor (size_t i = usize; i < bumped_usize && i < usize + 32; ++i) {\n\t\tif (unlikely(*((unsigned char *)ptr + i) != 0xBC)) {\n\t\t\tsafety_check_fail(\"Use after free error\\n\");\n\t\t}\n\t}\n}\n\n#endif /*JEMALLOC_INTERNAL_SAFETY_CHECK_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/sc.h",
    "content": "#ifndef JEMALLOC_INTERNAL_SC_H\n#define JEMALLOC_INTERNAL_SC_H\n\n#include \"jemalloc/internal/jemalloc_internal_types.h\"\n\n/*\n * Size class computations:\n *\n * These are a little tricky; we'll first start by describing how things\n * generally work, and then describe some of the details.\n *\n * Ignore the first few size classes for a moment. We can then split all the\n * remaining size classes into groups. The size classes in a group are spaced\n * such that they cover allocation request sizes in a power-of-2 range. The\n * power of two is called the base of the group, and the size classes in it\n * satisfy allocations in the half-open range (base, base * 2]. There are\n * SC_NGROUP size classes in each group, equally spaced in the range, so that\n * each one covers allocations for base / SC_NGROUP possible allocation sizes.\n * We call that value (base / SC_NGROUP) the delta of the group. Each size class\n * is delta larger than the one before it (including the initial size class in a\n * group, which is delta larger than base, the largest size class in the\n * previous group).\n * To make the math all work out nicely, we require that SC_NGROUP is a power of\n * two, and define it in terms of SC_LG_NGROUP. We'll often talk in terms of\n * lg_base and lg_delta. For each of these groups then, we have that\n * lg_delta == lg_base - SC_LG_NGROUP.\n * The size classes in a group with a given lg_base and lg_delta (which, recall,\n * can be computed from lg_base for these groups) are therefore:\n *   base + 1 * delta\n *     which covers allocations in (base, base + 1 * delta]\n *   base + 2 * delta\n *     which covers allocations in (base + 1 * delta, base + 2 * delta].\n *   base + 3 * delta\n *     which covers allocations in (base + 2 * delta, base + 3 * delta].\n *   ...\n *   base + SC_NGROUP * delta ( == 2 * base)\n *     which covers allocations in (base + (SC_NGROUP - 1) * delta, 2 * base].\n * (Note that currently SC_NGROUP is always 4, so the \"...\" is empty in\n * practice.)\n * Note that the last size class in the group is the next power of two (after\n * base), so that we've set up the induction correctly for the next group's\n * selection of delta.\n *\n * Now, let's start considering the first few size classes. Two extra constants\n * come into play here: LG_QUANTUM and SC_LG_TINY_MIN. LG_QUANTUM ensures\n * correct platform alignment; all objects of size (1 << LG_QUANTUM) or larger\n * are at least (1 << LG_QUANTUM) aligned; this can be used to ensure that we\n * never return improperly aligned memory, by making (1 << LG_QUANTUM) equal the\n * highest required alignment of a platform. For allocation sizes smaller than\n * (1 << LG_QUANTUM) though, we can be more relaxed (since we don't support\n * platforms with types with alignment larger than their size). To allow such\n * allocations (without wasting space unnecessarily), we introduce tiny size\n * classes; one per power of two, up until we hit the quantum size. There are\n * therefore LG_QUANTUM - SC_LG_TINY_MIN such size classes.\n *\n * Next, we have a size class of size (1 << LG_QUANTUM).  This can't be the\n * start of a group in the sense we described above (covering a power of two\n * range) since, if we divided into it to pick a value of delta, we'd get a\n * delta smaller than (1 << LG_QUANTUM) for sizes >= (1 << LG_QUANTUM), which\n * is against the rules.\n *\n * The first base we can divide by SC_NGROUP while still being at least\n * (1 << LG_QUANTUM) is SC_NGROUP * (1 << LG_QUANTUM). We can get there by\n * having SC_NGROUP size classes, spaced (1 << LG_QUANTUM) apart. These size\n * classes are:\n *   1 * (1 << LG_QUANTUM)\n *   2 * (1 << LG_QUANTUM)\n *   3 * (1 << LG_QUANTUM)\n *   ... (although, as above, this \"...\" is empty in practice)\n *   SC_NGROUP * (1 << LG_QUANTUM).\n *\n * There are SC_NGROUP of these size classes, so we can regard it as a sort of\n * pseudo-group, even though it spans multiple powers of 2, is divided\n * differently, and both starts and ends on a power of 2 (as opposed to just\n * ending). SC_NGROUP is itself a power of two, so the first group after the\n * pseudo-group has the power-of-two base SC_NGROUP * (1 << LG_QUANTUM), for a\n * lg_base of LG_QUANTUM + SC_LG_NGROUP. We can divide this base into SC_NGROUP\n * sizes without violating our LG_QUANTUM requirements, so we can safely set\n * lg_delta = lg_base - SC_LG_GROUP (== LG_QUANTUM).\n *\n * So, in order, the size classes are:\n *\n * Tiny size classes:\n * - Count: LG_QUANTUM - SC_LG_TINY_MIN.\n * - Sizes:\n *     1 << SC_LG_TINY_MIN\n *     1 << (SC_LG_TINY_MIN + 1)\n *     1 << (SC_LG_TINY_MIN + 2)\n *     ...\n *     1 << (LG_QUANTUM - 1)\n *\n * Initial pseudo-group:\n * - Count: SC_NGROUP\n * - Sizes:\n *     1 * (1 << LG_QUANTUM)\n *     2 * (1 << LG_QUANTUM)\n *     3 * (1 << LG_QUANTUM)\n *     ...\n *     SC_NGROUP * (1 << LG_QUANTUM)\n *\n * Regular group 0:\n * - Count: SC_NGROUP\n * - Sizes:\n *   (relative to lg_base of LG_QUANTUM + SC_LG_NGROUP and lg_delta of\n *   lg_base - SC_LG_NGROUP)\n *     (1 << lg_base) + 1 * (1 << lg_delta)\n *     (1 << lg_base) + 2 * (1 << lg_delta)\n *     (1 << lg_base) + 3 * (1 << lg_delta)\n *     ...\n *     (1 << lg_base) + SC_NGROUP * (1 << lg_delta) [ == (1 << (lg_base + 1)) ]\n *\n * Regular group 1:\n * - Count: SC_NGROUP\n * - Sizes:\n *   (relative to lg_base of LG_QUANTUM + SC_LG_NGROUP + 1 and lg_delta of\n *   lg_base - SC_LG_NGROUP)\n *     (1 << lg_base) + 1 * (1 << lg_delta)\n *     (1 << lg_base) + 2 * (1 << lg_delta)\n *     (1 << lg_base) + 3 * (1 << lg_delta)\n *     ...\n *     (1 << lg_base) + SC_NGROUP * (1 << lg_delta) [ == (1 << (lg_base + 1)) ]\n *\n * ...\n *\n * Regular group N:\n * - Count: SC_NGROUP\n * - Sizes:\n *   (relative to lg_base of LG_QUANTUM + SC_LG_NGROUP + N and lg_delta of\n *   lg_base - SC_LG_NGROUP)\n *     (1 << lg_base) + 1 * (1 << lg_delta)\n *     (1 << lg_base) + 2 * (1 << lg_delta)\n *     (1 << lg_base) + 3 * (1 << lg_delta)\n *     ...\n *     (1 << lg_base) + SC_NGROUP * (1 << lg_delta) [ == (1 << (lg_base + 1)) ]\n *\n *\n * Representation of metadata:\n * To make the math easy, we'll mostly work in lg quantities. We record lg_base,\n * lg_delta, and ndelta (i.e. number of deltas above the base) on a\n * per-size-class basis, and maintain the invariant that, across all size\n * classes, size == (1 << lg_base) + ndelta * (1 << lg_delta).\n *\n * For regular groups (i.e. those with lg_base >= LG_QUANTUM + SC_LG_NGROUP),\n * lg_delta is lg_base - SC_LG_NGROUP, and ndelta goes from 1 to SC_NGROUP.\n *\n * For the initial tiny size classes (if any), lg_base is lg(size class size).\n * lg_delta is lg_base for the first size class, and lg_base - 1 for all\n * subsequent ones. ndelta is always 0.\n *\n * For the pseudo-group, if there are no tiny size classes, then we set\n * lg_base == LG_QUANTUM, lg_delta == LG_QUANTUM, and have ndelta range from 0\n * to SC_NGROUP - 1. (Note that delta == base, so base + (SC_NGROUP - 1) * delta\n * is just SC_NGROUP * base, or (1 << (SC_LG_NGROUP + LG_QUANTUM)), so we do\n * indeed get a power of two that way). If there *are* tiny size classes, then\n * the first size class needs to have lg_delta relative to the largest tiny size\n * class. We therefore set lg_base == LG_QUANTUM - 1,\n * lg_delta == LG_QUANTUM - 1, and ndelta == 1, keeping the rest of the\n * pseudo-group the same.\n *\n *\n * Other terminology:\n * \"Small\" size classes mean those that are allocated out of bins, which is the\n * same as those that are slab allocated.\n * \"Large\" size classes are those that are not small. The cutoff for counting as\n * large is page size * group size.\n */\n\n/*\n * Size class N + (1 << SC_LG_NGROUP) twice the size of size class N.\n */\n#define SC_LG_NGROUP 2\n#define SC_LG_TINY_MIN 3\n\n#if SC_LG_TINY_MIN == 0\n/* The div module doesn't support division by 1, which this would require. */\n#error \"Unsupported LG_TINY_MIN\"\n#endif\n\n/*\n * The definitions below are all determined by the above settings and system\n * characteristics.\n */\n#define SC_NGROUP (1ULL << SC_LG_NGROUP)\n#define SC_PTR_BITS ((1ULL << LG_SIZEOF_PTR) * 8)\n#define SC_NTINY (LG_QUANTUM - SC_LG_TINY_MIN)\n#define SC_LG_TINY_MAXCLASS (LG_QUANTUM > SC_LG_TINY_MIN ? LG_QUANTUM - 1 : -1)\n#define SC_NPSEUDO SC_NGROUP\n#define SC_LG_FIRST_REGULAR_BASE (LG_QUANTUM + SC_LG_NGROUP)\n/*\n * We cap allocations to be less than 2 ** (ptr_bits - 1), so the highest base\n * we need is 2 ** (ptr_bits - 2). (This also means that the last group is 1\n * size class shorter than the others).\n * We could probably save some space in arenas by capping this at LG_VADDR size.\n */\n#define SC_LG_BASE_MAX (SC_PTR_BITS - 2)\n#define SC_NREGULAR (SC_NGROUP * \t\t\t\t\t\\\n    (SC_LG_BASE_MAX - SC_LG_FIRST_REGULAR_BASE + 1) - 1)\n#define SC_NSIZES (SC_NTINY + SC_NPSEUDO + SC_NREGULAR)\n\n/* The number of size classes that are a multiple of the page size. */\n#define SC_NPSIZES (\t\t\t\t\t\t\t\\\n    /* Start with all the size classes. */\t\t\t\t\\\n    SC_NSIZES\t\t\t\t\t\t\t\t\\\n    /* Subtract out those groups with too small a base. */\t\t\\\n    - (LG_PAGE - 1 - SC_LG_FIRST_REGULAR_BASE) * SC_NGROUP\t\t\\\n    /* And the pseudo-group. */\t\t\t\t\t\t\\\n    - SC_NPSEUDO\t\t\t\t\t\t\t\\\n    /* And the tiny group. */\t\t\t\t\t\t\\\n    - SC_NTINY\t\t\t\t\t\t\t\t\\\n    /* Sizes where ndelta*delta is not a multiple of the page size. */\t\\\n    - (SC_LG_NGROUP * SC_NGROUP))\n/*\n * Note that the last line is computed as the sum of the second column in the\n * following table:\n *                      lg(base) | count of sizes to exclude\n * ------------------------------|-----------------------------\n *                   LG_PAGE - 1 | SC_NGROUP - 1\n *                       LG_PAGE | SC_NGROUP - 1\n *                   LG_PAGE + 1 | SC_NGROUP - 2\n *                   LG_PAGE + 2 | SC_NGROUP - 4\n *                           ... | ...\n *  LG_PAGE + (SC_LG_NGROUP - 1) | SC_NGROUP - (SC_NGROUP / 2)\n */\n\n/*\n * We declare a size class is binnable if size < page size * group. Or, in other\n * words, lg(size) < lg(page size) + lg(group size).\n */\n#define SC_NBINS (\t\t\t\t\t\t\t\\\n    /* Sub-regular size classes. */\t\t\t\t\t\\\n    SC_NTINY + SC_NPSEUDO\t\t\t\t\t\t\\\n    /* Groups with lg_regular_min_base <= lg_base <= lg_base_max */\t\\\n    + SC_NGROUP * (LG_PAGE + SC_LG_NGROUP - SC_LG_FIRST_REGULAR_BASE)\t\\\n    /* Last SC of the last group hits the bound exactly; exclude it. */\t\\\n    - 1)\n\n/*\n * The size2index_tab lookup table uses uint8_t to encode each bin index, so we\n * cannot support more than 256 small size classes.\n */\n#if (SC_NBINS > 256)\n#  error \"Too many small size classes\"\n#endif\n\n/* The largest size class in the lookup table. */\n#define SC_LOOKUP_MAXCLASS ((size_t)1 << 12)\n\n/* Internal, only used for the definition of SC_SMALL_MAXCLASS. */\n#define SC_SMALL_MAX_BASE ((size_t)1 << (LG_PAGE + SC_LG_NGROUP - 1))\n#define SC_SMALL_MAX_DELTA ((size_t)1 << (LG_PAGE - 1))\n\n/* The largest size class allocated out of a slab. */\n#define SC_SMALL_MAXCLASS (SC_SMALL_MAX_BASE\t\t\t\t\\\n    + (SC_NGROUP - 1) * SC_SMALL_MAX_DELTA)\n\n/* The smallest size class not allocated out of a slab. */\n#define SC_LARGE_MINCLASS ((size_t)1ULL << (LG_PAGE + SC_LG_NGROUP))\n#define SC_LG_LARGE_MINCLASS (LG_PAGE + SC_LG_NGROUP)\n\n/* Internal; only used for the definition of SC_LARGE_MAXCLASS. */\n#define SC_MAX_BASE ((size_t)1 << (SC_PTR_BITS - 2))\n#define SC_MAX_DELTA ((size_t)1 << (SC_PTR_BITS - 2 - SC_LG_NGROUP))\n\n/* The largest size class supported. */\n#define SC_LARGE_MAXCLASS (SC_MAX_BASE + (SC_NGROUP - 1) * SC_MAX_DELTA)\n\ntypedef struct sc_s sc_t;\nstruct sc_s {\n\t/* Size class index, or -1 if not a valid size class. */\n\tint index;\n\t/* Lg group base size (no deltas added). */\n\tint lg_base;\n\t/* Lg delta to previous size class. */\n\tint lg_delta;\n\t/* Delta multiplier.  size == 1<<lg_base + ndelta<<lg_delta */\n\tint ndelta;\n\t/*\n\t * True if the size class is a multiple of the page size, false\n\t * otherwise.\n\t */\n\tbool psz;\n\t/*\n\t * True if the size class is a small, bin, size class. False otherwise.\n\t */\n\tbool bin;\n\t/* The slab page count if a small bin size class, 0 otherwise. */\n\tint pgs;\n\t/* Same as lg_delta if a lookup table size class, 0 otherwise. */\n\tint lg_delta_lookup;\n};\n\ntypedef struct sc_data_s sc_data_t;\nstruct sc_data_s {\n\t/* Number of tiny size classes. */\n\tunsigned ntiny;\n\t/* Number of bins supported by the lookup table. */\n\tint nlbins;\n\t/* Number of small size class bins. */\n\tint nbins;\n\t/* Number of size classes. */\n\tint nsizes;\n\t/* Number of bits required to store NSIZES. */\n\tint lg_ceil_nsizes;\n\t/* Number of size classes that are a multiple of (1U << LG_PAGE). */\n\tunsigned npsizes;\n\t/* Lg of maximum tiny size class (or -1, if none). */\n\tint lg_tiny_maxclass;\n\t/* Maximum size class included in lookup table. */\n\tsize_t lookup_maxclass;\n\t/* Maximum small size class. */\n\tsize_t small_maxclass;\n\t/* Lg of minimum large size class. */\n\tint lg_large_minclass;\n\t/* The minimum large size class. */\n\tsize_t large_minclass;\n\t/* Maximum (large) size class. */\n\tsize_t large_maxclass;\n\t/* True if the sc_data_t has been initialized (for debugging only). */\n\tbool initialized;\n\n\tsc_t sc[SC_NSIZES];\n};\n\nvoid sc_data_init(sc_data_t *data);\n/*\n * Updates slab sizes in [begin, end] to be pgs pages in length, if possible.\n * Otherwise, does its best to accomodate the request.\n */\nvoid sc_data_update_slab_size(sc_data_t *data, size_t begin, size_t end,\n    int pgs);\nvoid sc_boot(sc_data_t *data);\n\n#endif /* JEMALLOC_INTERNAL_SC_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/seq.h",
    "content": "#ifndef JEMALLOC_INTERNAL_SEQ_H\n#define JEMALLOC_INTERNAL_SEQ_H\n\n#include \"jemalloc/internal/atomic.h\"\n\n/*\n * A simple seqlock implementation.\n */\n\n#define seq_define(type, short_type)\t\t\t\t\t\\\ntypedef struct {\t\t\t\t\t\t\t\\\n\tatomic_zu_t seq;\t\t\t\t\t\t\\\n\tatomic_zu_t data[\t\t\t\t\t\t\\\n\t    (sizeof(type) + sizeof(size_t) - 1) / sizeof(size_t)];\t\\\n} seq_##short_type##_t;\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n/*\t\t\t\t\t\t\t\t\t\\\n * No internal synchronization -- the caller must ensure that there's\t\\\n * only a single writer at a time.\t\t\t\t\t\\\n */\t\t\t\t\t\t\t\t\t\\\nstatic inline void\t\t\t\t\t\t\t\\\nseq_store_##short_type(seq_##short_type##_t *dst, type *src) {\t\t\\\n\tsize_t buf[sizeof(dst->data) / sizeof(size_t)];\t\t\t\\\n\tbuf[sizeof(buf) / sizeof(size_t) - 1] = 0;\t\t\t\\\n\tmemcpy(buf, src, sizeof(type));\t\t\t\t\t\\\n\tsize_t old_seq = atomic_load_zu(&dst->seq, ATOMIC_RELAXED);\t\\\n\tatomic_store_zu(&dst->seq, old_seq + 1, ATOMIC_RELAXED);\t\\\n\tatomic_fence(ATOMIC_RELEASE);\t\t\t\t\t\\\n\tfor (size_t i = 0; i < sizeof(buf) / sizeof(size_t); i++) {\t\\\n\t\tatomic_store_zu(&dst->data[i], buf[i], ATOMIC_RELAXED);\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tatomic_store_zu(&dst->seq, old_seq + 2, ATOMIC_RELEASE);\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n/* Returns whether or not the read was consistent. */\t\t\t\\\nstatic inline bool\t\t\t\t\t\t\t\\\nseq_try_load_##short_type(type *dst, seq_##short_type##_t *src) {\t\\\n\tsize_t buf[sizeof(src->data) / sizeof(size_t)];\t\t\t\\\n\tsize_t seq1 = atomic_load_zu(&src->seq, ATOMIC_ACQUIRE);\t\\\n\tif (seq1 % 2 != 0) {\t\t\t\t\t\t\\\n\t\treturn false;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tfor (size_t i = 0; i < sizeof(buf) / sizeof(size_t); i++) {\t\\\n\t\tbuf[i] = atomic_load_zu(&src->data[i], ATOMIC_RELAXED);\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tatomic_fence(ATOMIC_ACQUIRE);\t\t\t\t\t\\\n\tsize_t seq2 = atomic_load_zu(&src->seq, ATOMIC_RELAXED);\t\\\n\tif (seq1 != seq2) {\t\t\t\t\t\t\\\n\t\treturn false;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tmemcpy(dst, buf, sizeof(type));\t\t\t\t\t\\\n\treturn true;\t\t\t\t\t\t\t\\\n}\n\n#endif /* JEMALLOC_INTERNAL_SEQ_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/smoothstep.h",
    "content": "#ifndef JEMALLOC_INTERNAL_SMOOTHSTEP_H\n#define JEMALLOC_INTERNAL_SMOOTHSTEP_H\n\n/*\n * This file was generated by the following command:\n *   sh smoothstep.sh smoother 200 24 3 15\n */\n/******************************************************************************/\n\n/*\n * This header defines a precomputed table based on the smoothstep family of\n * sigmoidal curves (https://en.wikipedia.org/wiki/Smoothstep) that grow from 0\n * to 1 in 0 <= x <= 1.  The table is stored as integer fixed point values so\n * that floating point math can be avoided.\n *\n *                      3     2\n *   smoothstep(x) = -2x  + 3x\n *\n *                       5      4      3\n *   smootherstep(x) = 6x  - 15x  + 10x\n *\n *                          7      6      5      4\n *   smootheststep(x) = -20x  + 70x  - 84x  + 35x\n */\n\n#define SMOOTHSTEP_VARIANT\t\"smoother\"\n#define SMOOTHSTEP_NSTEPS\t200\n#define SMOOTHSTEP_BFP\t\t24\n#define SMOOTHSTEP \\\n /* STEP(step, h,                            x,     y) */ \\\n    STEP(   1, UINT64_C(0x0000000000000014), 0.005, 0.000001240643750) \\\n    STEP(   2, UINT64_C(0x00000000000000a5), 0.010, 0.000009850600000) \\\n    STEP(   3, UINT64_C(0x0000000000000229), 0.015, 0.000032995181250) \\\n    STEP(   4, UINT64_C(0x0000000000000516), 0.020, 0.000077619200000) \\\n    STEP(   5, UINT64_C(0x00000000000009dc), 0.025, 0.000150449218750) \\\n    STEP(   6, UINT64_C(0x00000000000010e8), 0.030, 0.000257995800000) \\\n    STEP(   7, UINT64_C(0x0000000000001aa4), 0.035, 0.000406555756250) \\\n    STEP(   8, UINT64_C(0x0000000000002777), 0.040, 0.000602214400000) \\\n    STEP(   9, UINT64_C(0x00000000000037c2), 0.045, 0.000850847793750) \\\n    STEP(  10, UINT64_C(0x0000000000004be6), 0.050, 0.001158125000000) \\\n    STEP(  11, UINT64_C(0x000000000000643c), 0.055, 0.001529510331250) \\\n    STEP(  12, UINT64_C(0x000000000000811f), 0.060, 0.001970265600000) \\\n    STEP(  13, UINT64_C(0x000000000000a2e2), 0.065, 0.002485452368750) \\\n    STEP(  14, UINT64_C(0x000000000000c9d8), 0.070, 0.003079934200000) \\\n    STEP(  15, UINT64_C(0x000000000000f64f), 0.075, 0.003758378906250) \\\n    STEP(  16, UINT64_C(0x0000000000012891), 0.080, 0.004525260800000) \\\n    STEP(  17, UINT64_C(0x00000000000160e7), 0.085, 0.005384862943750) \\\n    STEP(  18, UINT64_C(0x0000000000019f95), 0.090, 0.006341279400000) \\\n    STEP(  19, UINT64_C(0x000000000001e4dc), 0.095, 0.007398417481250) \\\n    STEP(  20, UINT64_C(0x00000000000230fc), 0.100, 0.008560000000000) \\\n    STEP(  21, UINT64_C(0x0000000000028430), 0.105, 0.009829567518750) \\\n    STEP(  22, UINT64_C(0x000000000002deb0), 0.110, 0.011210480600000) \\\n    STEP(  23, UINT64_C(0x00000000000340b1), 0.115, 0.012705922056250) \\\n    STEP(  24, UINT64_C(0x000000000003aa67), 0.120, 0.014318899200000) \\\n    STEP(  25, UINT64_C(0x0000000000041c00), 0.125, 0.016052246093750) \\\n    STEP(  26, UINT64_C(0x00000000000495a8), 0.130, 0.017908625800000) \\\n    STEP(  27, UINT64_C(0x000000000005178b), 0.135, 0.019890532631250) \\\n    STEP(  28, UINT64_C(0x000000000005a1cf), 0.140, 0.022000294400000) \\\n    STEP(  29, UINT64_C(0x0000000000063498), 0.145, 0.024240074668750) \\\n    STEP(  30, UINT64_C(0x000000000006d009), 0.150, 0.026611875000000) \\\n    STEP(  31, UINT64_C(0x000000000007743f), 0.155, 0.029117537206250) \\\n    STEP(  32, UINT64_C(0x0000000000082157), 0.160, 0.031758745600000) \\\n    STEP(  33, UINT64_C(0x000000000008d76b), 0.165, 0.034537029243750) \\\n    STEP(  34, UINT64_C(0x0000000000099691), 0.170, 0.037453764200000) \\\n    STEP(  35, UINT64_C(0x00000000000a5edf), 0.175, 0.040510175781250) \\\n    STEP(  36, UINT64_C(0x00000000000b3067), 0.180, 0.043707340800000) \\\n    STEP(  37, UINT64_C(0x00000000000c0b38), 0.185, 0.047046189818750) \\\n    STEP(  38, UINT64_C(0x00000000000cef5e), 0.190, 0.050527509400000) \\\n    STEP(  39, UINT64_C(0x00000000000ddce6), 0.195, 0.054151944356250) \\\n    STEP(  40, UINT64_C(0x00000000000ed3d8), 0.200, 0.057920000000000) \\\n    STEP(  41, UINT64_C(0x00000000000fd439), 0.205, 0.061832044393750) \\\n    STEP(  42, UINT64_C(0x000000000010de0e), 0.210, 0.065888310600000) \\\n    STEP(  43, UINT64_C(0x000000000011f158), 0.215, 0.070088898931250) \\\n    STEP(  44, UINT64_C(0x0000000000130e17), 0.220, 0.074433779200000) \\\n    STEP(  45, UINT64_C(0x0000000000143448), 0.225, 0.078922792968750) \\\n    STEP(  46, UINT64_C(0x00000000001563e7), 0.230, 0.083555655800000) \\\n    STEP(  47, UINT64_C(0x0000000000169cec), 0.235, 0.088331959506250) \\\n    STEP(  48, UINT64_C(0x000000000017df4f), 0.240, 0.093251174400000) \\\n    STEP(  49, UINT64_C(0x0000000000192b04), 0.245, 0.098312651543750) \\\n    STEP(  50, UINT64_C(0x00000000001a8000), 0.250, 0.103515625000000) \\\n    STEP(  51, UINT64_C(0x00000000001bde32), 0.255, 0.108859214081250) \\\n    STEP(  52, UINT64_C(0x00000000001d458b), 0.260, 0.114342425600000) \\\n    STEP(  53, UINT64_C(0x00000000001eb5f8), 0.265, 0.119964156118750) \\\n    STEP(  54, UINT64_C(0x0000000000202f65), 0.270, 0.125723194200000) \\\n    STEP(  55, UINT64_C(0x000000000021b1bb), 0.275, 0.131618222656250) \\\n    STEP(  56, UINT64_C(0x0000000000233ce3), 0.280, 0.137647820800000) \\\n    STEP(  57, UINT64_C(0x000000000024d0c3), 0.285, 0.143810466693750) \\\n    STEP(  58, UINT64_C(0x0000000000266d40), 0.290, 0.150104539400000) \\\n    STEP(  59, UINT64_C(0x000000000028123d), 0.295, 0.156528321231250) \\\n    STEP(  60, UINT64_C(0x000000000029bf9c), 0.300, 0.163080000000000) \\\n    STEP(  61, UINT64_C(0x00000000002b753d), 0.305, 0.169757671268750) \\\n    STEP(  62, UINT64_C(0x00000000002d32fe), 0.310, 0.176559340600000) \\\n    STEP(  63, UINT64_C(0x00000000002ef8bc), 0.315, 0.183482925806250) \\\n    STEP(  64, UINT64_C(0x000000000030c654), 0.320, 0.190526259200000) \\\n    STEP(  65, UINT64_C(0x0000000000329b9f), 0.325, 0.197687089843750) \\\n    STEP(  66, UINT64_C(0x0000000000347875), 0.330, 0.204963085800000) \\\n    STEP(  67, UINT64_C(0x0000000000365cb0), 0.335, 0.212351836381250) \\\n    STEP(  68, UINT64_C(0x0000000000384825), 0.340, 0.219850854400000) \\\n    STEP(  69, UINT64_C(0x00000000003a3aa8), 0.345, 0.227457578418750) \\\n    STEP(  70, UINT64_C(0x00000000003c340f), 0.350, 0.235169375000000) \\\n    STEP(  71, UINT64_C(0x00000000003e342b), 0.355, 0.242983540956250) \\\n    STEP(  72, UINT64_C(0x0000000000403ace), 0.360, 0.250897305600000) \\\n    STEP(  73, UINT64_C(0x00000000004247c8), 0.365, 0.258907832993750) \\\n    STEP(  74, UINT64_C(0x0000000000445ae9), 0.370, 0.267012224200000) \\\n    STEP(  75, UINT64_C(0x0000000000467400), 0.375, 0.275207519531250) \\\n    STEP(  76, UINT64_C(0x00000000004892d8), 0.380, 0.283490700800000) \\\n    STEP(  77, UINT64_C(0x00000000004ab740), 0.385, 0.291858693568750) \\\n    STEP(  78, UINT64_C(0x00000000004ce102), 0.390, 0.300308369400000) \\\n    STEP(  79, UINT64_C(0x00000000004f0fe9), 0.395, 0.308836548106250) \\\n    STEP(  80, UINT64_C(0x00000000005143bf), 0.400, 0.317440000000000) \\\n    STEP(  81, UINT64_C(0x0000000000537c4d), 0.405, 0.326115448143750) \\\n    STEP(  82, UINT64_C(0x000000000055b95b), 0.410, 0.334859570600000) \\\n    STEP(  83, UINT64_C(0x000000000057fab1), 0.415, 0.343669002681250) \\\n    STEP(  84, UINT64_C(0x00000000005a4015), 0.420, 0.352540339200000) \\\n    STEP(  85, UINT64_C(0x00000000005c894e), 0.425, 0.361470136718750) \\\n    STEP(  86, UINT64_C(0x00000000005ed622), 0.430, 0.370454915800000) \\\n    STEP(  87, UINT64_C(0x0000000000612655), 0.435, 0.379491163256250) \\\n    STEP(  88, UINT64_C(0x00000000006379ac), 0.440, 0.388575334400000) \\\n    STEP(  89, UINT64_C(0x000000000065cfeb), 0.445, 0.397703855293750) \\\n    STEP(  90, UINT64_C(0x00000000006828d6), 0.450, 0.406873125000000) \\\n    STEP(  91, UINT64_C(0x00000000006a842f), 0.455, 0.416079517831250) \\\n    STEP(  92, UINT64_C(0x00000000006ce1bb), 0.460, 0.425319385600000) \\\n    STEP(  93, UINT64_C(0x00000000006f413a), 0.465, 0.434589059868750) \\\n    STEP(  94, UINT64_C(0x000000000071a270), 0.470, 0.443884854200000) \\\n    STEP(  95, UINT64_C(0x000000000074051d), 0.475, 0.453203066406250) \\\n    STEP(  96, UINT64_C(0x0000000000766905), 0.480, 0.462539980800000) \\\n    STEP(  97, UINT64_C(0x000000000078cde7), 0.485, 0.471891870443750) \\\n    STEP(  98, UINT64_C(0x00000000007b3387), 0.490, 0.481254999400000) \\\n    STEP(  99, UINT64_C(0x00000000007d99a4), 0.495, 0.490625624981250) \\\n    STEP( 100, UINT64_C(0x0000000000800000), 0.500, 0.500000000000000) \\\n    STEP( 101, UINT64_C(0x000000000082665b), 0.505, 0.509374375018750) \\\n    STEP( 102, UINT64_C(0x000000000084cc78), 0.510, 0.518745000600000) \\\n    STEP( 103, UINT64_C(0x0000000000873218), 0.515, 0.528108129556250) \\\n    STEP( 104, UINT64_C(0x00000000008996fa), 0.520, 0.537460019200000) \\\n    STEP( 105, UINT64_C(0x00000000008bfae2), 0.525, 0.546796933593750) \\\n    STEP( 106, UINT64_C(0x00000000008e5d8f), 0.530, 0.556115145800000) \\\n    STEP( 107, UINT64_C(0x000000000090bec5), 0.535, 0.565410940131250) \\\n    STEP( 108, UINT64_C(0x0000000000931e44), 0.540, 0.574680614400000) \\\n    STEP( 109, UINT64_C(0x0000000000957bd0), 0.545, 0.583920482168750) \\\n    STEP( 110, UINT64_C(0x000000000097d729), 0.550, 0.593126875000000) \\\n    STEP( 111, UINT64_C(0x00000000009a3014), 0.555, 0.602296144706250) \\\n    STEP( 112, UINT64_C(0x00000000009c8653), 0.560, 0.611424665600000) \\\n    STEP( 113, UINT64_C(0x00000000009ed9aa), 0.565, 0.620508836743750) \\\n    STEP( 114, UINT64_C(0x0000000000a129dd), 0.570, 0.629545084200000) \\\n    STEP( 115, UINT64_C(0x0000000000a376b1), 0.575, 0.638529863281250) \\\n    STEP( 116, UINT64_C(0x0000000000a5bfea), 0.580, 0.647459660800000) \\\n    STEP( 117, UINT64_C(0x0000000000a8054e), 0.585, 0.656330997318750) \\\n    STEP( 118, UINT64_C(0x0000000000aa46a4), 0.590, 0.665140429400000) \\\n    STEP( 119, UINT64_C(0x0000000000ac83b2), 0.595, 0.673884551856250) \\\n    STEP( 120, UINT64_C(0x0000000000aebc40), 0.600, 0.682560000000000) \\\n    STEP( 121, UINT64_C(0x0000000000b0f016), 0.605, 0.691163451893750) \\\n    STEP( 122, UINT64_C(0x0000000000b31efd), 0.610, 0.699691630600000) \\\n    STEP( 123, UINT64_C(0x0000000000b548bf), 0.615, 0.708141306431250) \\\n    STEP( 124, UINT64_C(0x0000000000b76d27), 0.620, 0.716509299200000) \\\n    STEP( 125, UINT64_C(0x0000000000b98c00), 0.625, 0.724792480468750) \\\n    STEP( 126, UINT64_C(0x0000000000bba516), 0.630, 0.732987775800000) \\\n    STEP( 127, UINT64_C(0x0000000000bdb837), 0.635, 0.741092167006250) \\\n    STEP( 128, UINT64_C(0x0000000000bfc531), 0.640, 0.749102694400000) \\\n    STEP( 129, UINT64_C(0x0000000000c1cbd4), 0.645, 0.757016459043750) \\\n    STEP( 130, UINT64_C(0x0000000000c3cbf0), 0.650, 0.764830625000000) \\\n    STEP( 131, UINT64_C(0x0000000000c5c557), 0.655, 0.772542421581250) \\\n    STEP( 132, UINT64_C(0x0000000000c7b7da), 0.660, 0.780149145600000) \\\n    STEP( 133, UINT64_C(0x0000000000c9a34f), 0.665, 0.787648163618750) \\\n    STEP( 134, UINT64_C(0x0000000000cb878a), 0.670, 0.795036914200000) \\\n    STEP( 135, UINT64_C(0x0000000000cd6460), 0.675, 0.802312910156250) \\\n    STEP( 136, UINT64_C(0x0000000000cf39ab), 0.680, 0.809473740800000) \\\n    STEP( 137, UINT64_C(0x0000000000d10743), 0.685, 0.816517074193750) \\\n    STEP( 138, UINT64_C(0x0000000000d2cd01), 0.690, 0.823440659400000) \\\n    STEP( 139, UINT64_C(0x0000000000d48ac2), 0.695, 0.830242328731250) \\\n    STEP( 140, UINT64_C(0x0000000000d64063), 0.700, 0.836920000000000) \\\n    STEP( 141, UINT64_C(0x0000000000d7edc2), 0.705, 0.843471678768750) \\\n    STEP( 142, UINT64_C(0x0000000000d992bf), 0.710, 0.849895460600000) \\\n    STEP( 143, UINT64_C(0x0000000000db2f3c), 0.715, 0.856189533306250) \\\n    STEP( 144, UINT64_C(0x0000000000dcc31c), 0.720, 0.862352179200000) \\\n    STEP( 145, UINT64_C(0x0000000000de4e44), 0.725, 0.868381777343750) \\\n    STEP( 146, UINT64_C(0x0000000000dfd09a), 0.730, 0.874276805800000) \\\n    STEP( 147, UINT64_C(0x0000000000e14a07), 0.735, 0.880035843881250) \\\n    STEP( 148, UINT64_C(0x0000000000e2ba74), 0.740, 0.885657574400000) \\\n    STEP( 149, UINT64_C(0x0000000000e421cd), 0.745, 0.891140785918750) \\\n    STEP( 150, UINT64_C(0x0000000000e58000), 0.750, 0.896484375000000) \\\n    STEP( 151, UINT64_C(0x0000000000e6d4fb), 0.755, 0.901687348456250) \\\n    STEP( 152, UINT64_C(0x0000000000e820b0), 0.760, 0.906748825600000) \\\n    STEP( 153, UINT64_C(0x0000000000e96313), 0.765, 0.911668040493750) \\\n    STEP( 154, UINT64_C(0x0000000000ea9c18), 0.770, 0.916444344200000) \\\n    STEP( 155, UINT64_C(0x0000000000ebcbb7), 0.775, 0.921077207031250) \\\n    STEP( 156, UINT64_C(0x0000000000ecf1e8), 0.780, 0.925566220800000) \\\n    STEP( 157, UINT64_C(0x0000000000ee0ea7), 0.785, 0.929911101068750) \\\n    STEP( 158, UINT64_C(0x0000000000ef21f1), 0.790, 0.934111689400000) \\\n    STEP( 159, UINT64_C(0x0000000000f02bc6), 0.795, 0.938167955606250) \\\n    STEP( 160, UINT64_C(0x0000000000f12c27), 0.800, 0.942080000000000) \\\n    STEP( 161, UINT64_C(0x0000000000f22319), 0.805, 0.945848055643750) \\\n    STEP( 162, UINT64_C(0x0000000000f310a1), 0.810, 0.949472490600000) \\\n    STEP( 163, UINT64_C(0x0000000000f3f4c7), 0.815, 0.952953810181250) \\\n    STEP( 164, UINT64_C(0x0000000000f4cf98), 0.820, 0.956292659200000) \\\n    STEP( 165, UINT64_C(0x0000000000f5a120), 0.825, 0.959489824218750) \\\n    STEP( 166, UINT64_C(0x0000000000f6696e), 0.830, 0.962546235800000) \\\n    STEP( 167, UINT64_C(0x0000000000f72894), 0.835, 0.965462970756250) \\\n    STEP( 168, UINT64_C(0x0000000000f7dea8), 0.840, 0.968241254400000) \\\n    STEP( 169, UINT64_C(0x0000000000f88bc0), 0.845, 0.970882462793750) \\\n    STEP( 170, UINT64_C(0x0000000000f92ff6), 0.850, 0.973388125000000) \\\n    STEP( 171, UINT64_C(0x0000000000f9cb67), 0.855, 0.975759925331250) \\\n    STEP( 172, UINT64_C(0x0000000000fa5e30), 0.860, 0.977999705600000) \\\n    STEP( 173, UINT64_C(0x0000000000fae874), 0.865, 0.980109467368750) \\\n    STEP( 174, UINT64_C(0x0000000000fb6a57), 0.870, 0.982091374200000) \\\n    STEP( 175, UINT64_C(0x0000000000fbe400), 0.875, 0.983947753906250) \\\n    STEP( 176, UINT64_C(0x0000000000fc5598), 0.880, 0.985681100800000) \\\n    STEP( 177, UINT64_C(0x0000000000fcbf4e), 0.885, 0.987294077943750) \\\n    STEP( 178, UINT64_C(0x0000000000fd214f), 0.890, 0.988789519400000) \\\n    STEP( 179, UINT64_C(0x0000000000fd7bcf), 0.895, 0.990170432481250) \\\n    STEP( 180, UINT64_C(0x0000000000fdcf03), 0.900, 0.991440000000000) \\\n    STEP( 181, UINT64_C(0x0000000000fe1b23), 0.905, 0.992601582518750) \\\n    STEP( 182, UINT64_C(0x0000000000fe606a), 0.910, 0.993658720600000) \\\n    STEP( 183, UINT64_C(0x0000000000fe9f18), 0.915, 0.994615137056250) \\\n    STEP( 184, UINT64_C(0x0000000000fed76e), 0.920, 0.995474739200000) \\\n    STEP( 185, UINT64_C(0x0000000000ff09b0), 0.925, 0.996241621093750) \\\n    STEP( 186, UINT64_C(0x0000000000ff3627), 0.930, 0.996920065800000) \\\n    STEP( 187, UINT64_C(0x0000000000ff5d1d), 0.935, 0.997514547631250) \\\n    STEP( 188, UINT64_C(0x0000000000ff7ee0), 0.940, 0.998029734400000) \\\n    STEP( 189, UINT64_C(0x0000000000ff9bc3), 0.945, 0.998470489668750) \\\n    STEP( 190, UINT64_C(0x0000000000ffb419), 0.950, 0.998841875000000) \\\n    STEP( 191, UINT64_C(0x0000000000ffc83d), 0.955, 0.999149152206250) \\\n    STEP( 192, UINT64_C(0x0000000000ffd888), 0.960, 0.999397785600000) \\\n    STEP( 193, UINT64_C(0x0000000000ffe55b), 0.965, 0.999593444243750) \\\n    STEP( 194, UINT64_C(0x0000000000ffef17), 0.970, 0.999742004200000) \\\n    STEP( 195, UINT64_C(0x0000000000fff623), 0.975, 0.999849550781250) \\\n    STEP( 196, UINT64_C(0x0000000000fffae9), 0.980, 0.999922380800000) \\\n    STEP( 197, UINT64_C(0x0000000000fffdd6), 0.985, 0.999967004818750) \\\n    STEP( 198, UINT64_C(0x0000000000ffff5a), 0.990, 0.999990149400000) \\\n    STEP( 199, UINT64_C(0x0000000000ffffeb), 0.995, 0.999998759356250) \\\n    STEP( 200, UINT64_C(0x0000000001000000), 1.000, 1.000000000000000) \\\n\n#endif /* JEMALLOC_INTERNAL_SMOOTHSTEP_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/smoothstep.sh",
    "content": "#!/bin/sh\n#\n# Generate a discrete lookup table for a sigmoid function in the smoothstep\n# family (https://en.wikipedia.org/wiki/Smoothstep), where the lookup table\n# entries correspond to x in [1/nsteps, 2/nsteps, ..., nsteps/nsteps].  Encode\n# the entries using a binary fixed point representation.\n#\n# Usage: smoothstep.sh <variant> <nsteps> <bfp> <xprec> <yprec>\n#\n#        <variant> is in {smooth, smoother, smoothest}.\n#        <nsteps> must be greater than zero.\n#        <bfp> must be in [0..62]; reasonable values are roughly [10..30].\n#        <xprec> is x decimal precision.\n#        <yprec> is y decimal precision.\n\n#set -x\n\ncmd=\"sh smoothstep.sh $*\"\nvariant=$1\nnsteps=$2\nbfp=$3\nxprec=$4\nyprec=$5\n\ncase \"${variant}\" in\n  smooth)\n    ;;\n  smoother)\n    ;;\n  smoothest)\n    ;;\n  *)\n    echo \"Unsupported variant\"\n    exit 1\n    ;;\nesac\n\nsmooth() {\n  step=$1\n  y=`echo ${yprec} k ${step} ${nsteps} / sx _2 lx 3 ^ '*' 3 lx 2 ^ '*' + p | dc | tr -d '\\\\\\\\\\n' | sed -e 's#^\\.#0.#g'`\n  h=`echo ${yprec} k 2 ${bfp} ^ ${y} '*' p | dc | tr -d '\\\\\\\\\\n' | sed -e 's#^\\.#0.#g' | tr '.' ' ' | awk '{print $1}' `\n}\n\nsmoother() {\n  step=$1\n  y=`echo ${yprec} k ${step} ${nsteps} / sx 6 lx 5 ^ '*' _15 lx 4 ^ '*' + 10 lx 3 ^ '*' + p | dc | tr -d '\\\\\\\\\\n' | sed -e 's#^\\.#0.#g'`\n  h=`echo ${yprec} k 2 ${bfp} ^ ${y} '*' p | dc | tr -d '\\\\\\\\\\n' | sed -e 's#^\\.#0.#g' | tr '.' ' ' | awk '{print $1}' `\n}\n\nsmoothest() {\n  step=$1\n  y=`echo ${yprec} k ${step} ${nsteps} / sx _20 lx 7 ^ '*' 70 lx 6 ^ '*' + _84 lx 5 ^ '*' + 35 lx 4 ^ '*' + p | dc | tr -d '\\\\\\\\\\n' | sed -e 's#^\\.#0.#g'`\n  h=`echo ${yprec} k 2 ${bfp} ^ ${y} '*' p | dc | tr -d '\\\\\\\\\\n' | sed -e 's#^\\.#0.#g' | tr '.' ' ' | awk '{print $1}' `\n}\n\ncat <<EOF\n#ifndef JEMALLOC_INTERNAL_SMOOTHSTEP_H\n#define JEMALLOC_INTERNAL_SMOOTHSTEP_H\n\n/*\n * This file was generated by the following command:\n *   $cmd\n */\n/******************************************************************************/\n\n/*\n * This header defines a precomputed table based on the smoothstep family of\n * sigmoidal curves (https://en.wikipedia.org/wiki/Smoothstep) that grow from 0\n * to 1 in 0 <= x <= 1.  The table is stored as integer fixed point values so\n * that floating point math can be avoided.\n *\n *                      3     2\n *   smoothstep(x) = -2x  + 3x\n *\n *                       5      4      3\n *   smootherstep(x) = 6x  - 15x  + 10x\n *\n *                          7      6      5      4\n *   smootheststep(x) = -20x  + 70x  - 84x  + 35x\n */\n\n#define SMOOTHSTEP_VARIANT\t\"${variant}\"\n#define SMOOTHSTEP_NSTEPS\t${nsteps}\n#define SMOOTHSTEP_BFP\t\t${bfp}\n#define SMOOTHSTEP \\\\\n /* STEP(step, h,                            x,     y) */ \\\\\nEOF\n\ns=1\nwhile [ $s -le $nsteps ] ; do\n  $variant ${s}\n  x=`echo ${xprec} k ${s} ${nsteps} / p | dc | tr -d '\\\\\\\\\\n' | sed -e 's#^\\.#0.#g'`\n  printf '    STEP(%4d, UINT64_C(0x%016x), %s, %s) \\\\\\n' ${s} ${h} ${x} ${y}\n\n  s=$((s+1))\ndone\necho\n\ncat <<EOF\n#endif /* JEMALLOC_INTERNAL_SMOOTHSTEP_H */\nEOF\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/spin.h",
    "content": "#ifndef JEMALLOC_INTERNAL_SPIN_H\n#define JEMALLOC_INTERNAL_SPIN_H\n\n#define SPIN_INITIALIZER {0U}\n\ntypedef struct {\n\tunsigned iteration;\n} spin_t;\n\nstatic inline void\nspin_cpu_spinwait() {\n#  if HAVE_CPU_SPINWAIT\n\tCPU_SPINWAIT;\n#  else\n\tvolatile int x = 0;\n\tx = x;\n#  endif\n}\n\nstatic inline void\nspin_adaptive(spin_t *spin) {\n\tvolatile uint32_t i;\n\n\tif (spin->iteration < 5) {\n\t\tfor (i = 0; i < (1U << spin->iteration); i++) {\n\t\t\tspin_cpu_spinwait();\n\t\t}\n\t\tspin->iteration++;\n\t} else {\n#ifdef _WIN32\n\t\tSwitchToThread();\n#else\n\t\tsched_yield();\n#endif\n\t}\n}\n\n#undef SPIN_INLINE\n\n#endif /* JEMALLOC_INTERNAL_SPIN_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/stats.h",
    "content": "#ifndef JEMALLOC_INTERNAL_STATS_H\n#define JEMALLOC_INTERNAL_STATS_H\n\n/*  OPTION(opt,\t\tvar_name,\tdefault,\tset_value_to) */\n#define STATS_PRINT_OPTIONS\t\t\t\t\t\t\\\n    OPTION('J',\t\tjson,\t\tfalse,\t\ttrue)\t\t\\\n    OPTION('g',\t\tgeneral,\ttrue,\t\tfalse)\t\t\\\n    OPTION('m',\t\tmerged,\t\tconfig_stats,\tfalse)\t\t\\\n    OPTION('d',\t\tdestroyed,\tconfig_stats,\tfalse)\t\t\\\n    OPTION('a',\t\tunmerged,\tconfig_stats,\tfalse)\t\t\\\n    OPTION('b',\t\tbins,\t\ttrue,\t\tfalse)\t\t\\\n    OPTION('l',\t\tlarge,\t\ttrue,\t\tfalse)\t\t\\\n    OPTION('x',\t\tmutex,\t\ttrue,\t\tfalse)\t\t\\\n    OPTION('e',\t\textents,\ttrue,\t\tfalse)\n\nenum {\n#define OPTION(o, v, d, s) stats_print_option_num_##v,\n    STATS_PRINT_OPTIONS\n#undef OPTION\n    stats_print_tot_num_options\n};\n\n/* Options for stats_print. */\nextern bool opt_stats_print;\nextern char opt_stats_print_opts[stats_print_tot_num_options+1];\n\n/* Implements je_malloc_stats_print. */\nvoid stats_print(void (*write_cb)(void *, const char *), void *cbopaque,\n    const char *opts);\n\n#endif /* JEMALLOC_INTERNAL_STATS_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/sz.h",
    "content": "#ifndef JEMALLOC_INTERNAL_SIZE_H\n#define JEMALLOC_INTERNAL_SIZE_H\n\n#include \"jemalloc/internal/bit_util.h\"\n#include \"jemalloc/internal/pages.h\"\n#include \"jemalloc/internal/sc.h\"\n#include \"jemalloc/internal/util.h\"\n\n/*\n * sz module: Size computations.\n *\n * Some abbreviations used here:\n *   p: Page\n *   ind: Index\n *   s, sz: Size\n *   u: Usable size\n *   a: Aligned\n *\n * These are not always used completely consistently, but should be enough to\n * interpret function names.  E.g. sz_psz2ind converts page size to page size\n * index; sz_sa2u converts a (size, alignment) allocation request to the usable\n * size that would result from such an allocation.\n */\n\n/*\n * sz_pind2sz_tab encodes the same information as could be computed by\n * sz_pind2sz_compute().\n */\nextern size_t sz_pind2sz_tab[SC_NPSIZES + 1];\n/*\n * sz_index2size_tab encodes the same information as could be computed (at\n * unacceptable cost in some code paths) by sz_index2size_compute().\n */\nextern size_t sz_index2size_tab[SC_NSIZES];\n/*\n * sz_size2index_tab is a compact lookup table that rounds request sizes up to\n * size classes.  In order to reduce cache footprint, the table is compressed,\n * and all accesses are via sz_size2index().\n */\nextern uint8_t sz_size2index_tab[];\n\nstatic const size_t sz_large_pad =\n#ifdef JEMALLOC_CACHE_OBLIVIOUS\n    PAGE\n#else\n    0\n#endif\n    ;\n\nextern void sz_boot(const sc_data_t *sc_data);\n\nJEMALLOC_ALWAYS_INLINE pszind_t\nsz_psz2ind(size_t psz) {\n\tif (unlikely(psz > SC_LARGE_MAXCLASS)) {\n\t\treturn SC_NPSIZES;\n\t}\n\tpszind_t x = lg_floor((psz<<1)-1);\n\tpszind_t shift = (x < SC_LG_NGROUP + LG_PAGE) ?\n\t    0 : x - (SC_LG_NGROUP + LG_PAGE);\n\tpszind_t grp = shift << SC_LG_NGROUP;\n\n\tpszind_t lg_delta = (x < SC_LG_NGROUP + LG_PAGE + 1) ?\n\t    LG_PAGE : x - SC_LG_NGROUP - 1;\n\n\tsize_t delta_inverse_mask = ZU(-1) << lg_delta;\n\tpszind_t mod = ((((psz-1) & delta_inverse_mask) >> lg_delta)) &\n\t    ((ZU(1) << SC_LG_NGROUP) - 1);\n\n\tpszind_t ind = grp + mod;\n\treturn ind;\n}\n\nstatic inline size_t\nsz_pind2sz_compute(pszind_t pind) {\n\tif (unlikely(pind == SC_NPSIZES)) {\n\t\treturn SC_LARGE_MAXCLASS + PAGE;\n\t}\n\tsize_t grp = pind >> SC_LG_NGROUP;\n\tsize_t mod = pind & ((ZU(1) << SC_LG_NGROUP) - 1);\n\n\tsize_t grp_size_mask = ~((!!grp)-1);\n\tsize_t grp_size = ((ZU(1) << (LG_PAGE + (SC_LG_NGROUP-1))) << grp)\n\t    & grp_size_mask;\n\n\tsize_t shift = (grp == 0) ? 1 : grp;\n\tsize_t lg_delta = shift + (LG_PAGE-1);\n\tsize_t mod_size = (mod+1) << lg_delta;\n\n\tsize_t sz = grp_size + mod_size;\n\treturn sz;\n}\n\nstatic inline size_t\nsz_pind2sz_lookup(pszind_t pind) {\n\tsize_t ret = (size_t)sz_pind2sz_tab[pind];\n\tassert(ret == sz_pind2sz_compute(pind));\n\treturn ret;\n}\n\nstatic inline size_t\nsz_pind2sz(pszind_t pind) {\n\tassert(pind < SC_NPSIZES + 1);\n\treturn sz_pind2sz_lookup(pind);\n}\n\nstatic inline size_t\nsz_psz2u(size_t psz) {\n\tif (unlikely(psz > SC_LARGE_MAXCLASS)) {\n\t\treturn SC_LARGE_MAXCLASS + PAGE;\n\t}\n\tsize_t x = lg_floor((psz<<1)-1);\n\tsize_t lg_delta = (x < SC_LG_NGROUP + LG_PAGE + 1) ?\n\t    LG_PAGE : x - SC_LG_NGROUP - 1;\n\tsize_t delta = ZU(1) << lg_delta;\n\tsize_t delta_mask = delta - 1;\n\tsize_t usize = (psz + delta_mask) & ~delta_mask;\n\treturn usize;\n}\n\nstatic inline szind_t\nsz_size2index_compute(size_t size) {\n\tif (unlikely(size > SC_LARGE_MAXCLASS)) {\n\t\treturn SC_NSIZES;\n\t}\n\n\tif (size == 0) {\n\t\treturn 0;\n\t}\n#if (SC_NTINY != 0)\n\tif (size <= (ZU(1) << SC_LG_TINY_MAXCLASS)) {\n\t\tszind_t lg_tmin = SC_LG_TINY_MAXCLASS - SC_NTINY + 1;\n\t\tszind_t lg_ceil = lg_floor(pow2_ceil_zu(size));\n\t\treturn (lg_ceil < lg_tmin ? 0 : lg_ceil - lg_tmin);\n\t}\n#endif\n\t{\n\t\tszind_t x = lg_floor((size<<1)-1);\n\t\tszind_t shift = (x < SC_LG_NGROUP + LG_QUANTUM) ? 0 :\n\t\t    x - (SC_LG_NGROUP + LG_QUANTUM);\n\t\tszind_t grp = shift << SC_LG_NGROUP;\n\n\t\tszind_t lg_delta = (x < SC_LG_NGROUP + LG_QUANTUM + 1)\n\t\t    ? LG_QUANTUM : x - SC_LG_NGROUP - 1;\n\n\t\tsize_t delta_inverse_mask = ZU(-1) << lg_delta;\n\t\tszind_t mod = ((((size-1) & delta_inverse_mask) >> lg_delta)) &\n\t\t    ((ZU(1) << SC_LG_NGROUP) - 1);\n\n\t\tszind_t index = SC_NTINY + grp + mod;\n\t\treturn index;\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE szind_t\nsz_size2index_lookup(size_t size) {\n\tassert(size <= SC_LOOKUP_MAXCLASS);\n\tszind_t ret = (sz_size2index_tab[(size + (ZU(1) << SC_LG_TINY_MIN) - 1)\n\t\t\t\t\t >> SC_LG_TINY_MIN]);\n\tassert(ret == sz_size2index_compute(size));\n\treturn ret;\n}\n\nJEMALLOC_ALWAYS_INLINE szind_t\nsz_size2index(size_t size) {\n\tif (likely(size <= SC_LOOKUP_MAXCLASS)) {\n\t\treturn sz_size2index_lookup(size);\n\t}\n\treturn sz_size2index_compute(size);\n}\n\nstatic inline size_t\nsz_index2size_compute(szind_t index) {\n#if (SC_NTINY > 0)\n\tif (index < SC_NTINY) {\n\t\treturn (ZU(1) << (SC_LG_TINY_MAXCLASS - SC_NTINY + 1 + index));\n\t}\n#endif\n\t{\n\t\tsize_t reduced_index = index - SC_NTINY;\n\t\tsize_t grp = reduced_index >> SC_LG_NGROUP;\n\t\tsize_t mod = reduced_index & ((ZU(1) << SC_LG_NGROUP) -\n\t\t    1);\n\n\t\tsize_t grp_size_mask = ~((!!grp)-1);\n\t\tsize_t grp_size = ((ZU(1) << (LG_QUANTUM +\n\t\t    (SC_LG_NGROUP-1))) << grp) & grp_size_mask;\n\n\t\tsize_t shift = (grp == 0) ? 1 : grp;\n\t\tsize_t lg_delta = shift + (LG_QUANTUM-1);\n\t\tsize_t mod_size = (mod+1) << lg_delta;\n\n\t\tsize_t usize = grp_size + mod_size;\n\t\treturn usize;\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\nsz_index2size_lookup(szind_t index) {\n\tsize_t ret = (size_t)sz_index2size_tab[index];\n\tassert(ret == sz_index2size_compute(index));\n\treturn ret;\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\nsz_index2size(szind_t index) {\n\tassert(index < SC_NSIZES);\n\treturn sz_index2size_lookup(index);\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\nsz_s2u_compute(size_t size) {\n\tif (unlikely(size > SC_LARGE_MAXCLASS)) {\n\t\treturn 0;\n\t}\n\n\tif (size == 0) {\n\t\tsize++;\n\t}\n#if (SC_NTINY > 0)\n\tif (size <= (ZU(1) << SC_LG_TINY_MAXCLASS)) {\n\t\tsize_t lg_tmin = SC_LG_TINY_MAXCLASS - SC_NTINY + 1;\n\t\tsize_t lg_ceil = lg_floor(pow2_ceil_zu(size));\n\t\treturn (lg_ceil < lg_tmin ? (ZU(1) << lg_tmin) :\n\t\t    (ZU(1) << lg_ceil));\n\t}\n#endif\n\t{\n\t\tsize_t x = lg_floor((size<<1)-1);\n\t\tsize_t lg_delta = (x < SC_LG_NGROUP + LG_QUANTUM + 1)\n\t\t    ?  LG_QUANTUM : x - SC_LG_NGROUP - 1;\n\t\tsize_t delta = ZU(1) << lg_delta;\n\t\tsize_t delta_mask = delta - 1;\n\t\tsize_t usize = (size + delta_mask) & ~delta_mask;\n\t\treturn usize;\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\nsz_s2u_lookup(size_t size) {\n\tsize_t ret = sz_index2size_lookup(sz_size2index_lookup(size));\n\n\tassert(ret == sz_s2u_compute(size));\n\treturn ret;\n}\n\n/*\n * Compute usable size that would result from allocating an object with the\n * specified size.\n */\nJEMALLOC_ALWAYS_INLINE size_t\nsz_s2u(size_t size) {\n\tif (likely(size <= SC_LOOKUP_MAXCLASS)) {\n\t\treturn sz_s2u_lookup(size);\n\t}\n\treturn sz_s2u_compute(size);\n}\n\n/*\n * Compute usable size that would result from allocating an object with the\n * specified size and alignment.\n */\nJEMALLOC_ALWAYS_INLINE size_t\nsz_sa2u(size_t size, size_t alignment) {\n\tsize_t usize;\n\n\tassert(alignment != 0 && ((alignment - 1) & alignment) == 0);\n\n\t/* Try for a small size class. */\n\tif (size <= SC_SMALL_MAXCLASS && alignment < PAGE) {\n\t\t/*\n\t\t * Round size up to the nearest multiple of alignment.\n\t\t *\n\t\t * This done, we can take advantage of the fact that for each\n\t\t * small size class, every object is aligned at the smallest\n\t\t * power of two that is non-zero in the base two representation\n\t\t * of the size.  For example:\n\t\t *\n\t\t *   Size |   Base 2 | Minimum alignment\n\t\t *   -----+----------+------------------\n\t\t *     96 |  1100000 |  32\n\t\t *    144 | 10100000 |  32\n\t\t *    192 | 11000000 |  64\n\t\t */\n\t\tusize = sz_s2u(ALIGNMENT_CEILING(size, alignment));\n\t\tif (usize < SC_LARGE_MINCLASS) {\n\t\t\treturn usize;\n\t\t}\n\t}\n\n\t/* Large size class.  Beware of overflow. */\n\n\tif (unlikely(alignment > SC_LARGE_MAXCLASS)) {\n\t\treturn 0;\n\t}\n\n\t/* Make sure result is a large size class. */\n\tif (size <= SC_LARGE_MINCLASS) {\n\t\tusize = SC_LARGE_MINCLASS;\n\t} else {\n\t\tusize = sz_s2u(size);\n\t\tif (usize < size) {\n\t\t\t/* size_t overflow. */\n\t\t\treturn 0;\n\t\t}\n\t}\n\n\t/*\n\t * Calculate the multi-page mapping that large_palloc() would need in\n\t * order to guarantee the alignment.\n\t */\n\tif (usize + sz_large_pad + PAGE_CEILING(alignment) - PAGE < usize) {\n\t\t/* size_t overflow. */\n\t\treturn 0;\n\t}\n\treturn usize;\n}\n\n#endif /* JEMALLOC_INTERNAL_SIZE_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/tcache_externs.h",
    "content": "#ifndef JEMALLOC_INTERNAL_TCACHE_EXTERNS_H\n#define JEMALLOC_INTERNAL_TCACHE_EXTERNS_H\n\nextern bool\topt_tcache;\nextern ssize_t\topt_lg_tcache_max;\n\nextern cache_bin_info_t\t*tcache_bin_info;\n\n/*\n * Number of tcache bins.  There are SC_NBINS small-object bins, plus 0 or more\n * large-object bins.\n */\nextern unsigned\tnhbins;\n\n/* Maximum cached size class. */\nextern size_t\ttcache_maxclass;\n\n/*\n * Explicit tcaches, managed via the tcache.{create,flush,destroy} mallctls and\n * usable via the MALLOCX_TCACHE() flag.  The automatic per thread tcaches are\n * completely disjoint from this data structure.  tcaches starts off as a sparse\n * array, so it has no physical memory footprint until individual pages are\n * touched.  This allows the entire array to be allocated the first time an\n * explicit tcache is created without a disproportionate impact on memory usage.\n */\nextern tcaches_t\t*tcaches;\n\nsize_t\ttcache_salloc(tsdn_t *tsdn, const void *ptr);\nvoid\ttcache_event_hard(tsd_t *tsd, tcache_t *tcache);\nvoid\t*tcache_alloc_small_hard(tsdn_t *tsdn, arena_t *arena, tcache_t *tcache,\n    cache_bin_t *tbin, szind_t binind, bool *tcache_success);\nvoid\ttcache_bin_flush_small(tsd_t *tsd, tcache_t *tcache, cache_bin_t *tbin,\n    szind_t binind, unsigned rem);\nvoid\ttcache_bin_flush_large(tsd_t *tsd, cache_bin_t *tbin, szind_t binind,\n    unsigned rem, tcache_t *tcache);\nvoid\ttcache_arena_reassociate(tsdn_t *tsdn, tcache_t *tcache,\n    arena_t *arena);\ntcache_t *tcache_create_explicit(tsd_t *tsd);\nvoid\ttcache_cleanup(tsd_t *tsd);\nvoid\ttcache_stats_merge(tsdn_t *tsdn, tcache_t *tcache, arena_t *arena);\nbool\ttcaches_create(tsd_t *tsd, unsigned *r_ind);\nvoid\ttcaches_flush(tsd_t *tsd, unsigned ind);\nvoid\ttcaches_destroy(tsd_t *tsd, unsigned ind);\nbool\ttcache_boot(tsdn_t *tsdn);\nvoid tcache_arena_associate(tsdn_t *tsdn, tcache_t *tcache, arena_t *arena);\nvoid tcache_prefork(tsdn_t *tsdn);\nvoid tcache_postfork_parent(tsdn_t *tsdn);\nvoid tcache_postfork_child(tsdn_t *tsdn);\nvoid tcache_flush(tsd_t *tsd);\nbool tsd_tcache_data_init(tsd_t *tsd);\nbool tsd_tcache_enabled_data_init(tsd_t *tsd);\n\n#endif /* JEMALLOC_INTERNAL_TCACHE_EXTERNS_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/tcache_inlines.h",
    "content": "#ifndef JEMALLOC_INTERNAL_TCACHE_INLINES_H\n#define JEMALLOC_INTERNAL_TCACHE_INLINES_H\n\n#include \"jemalloc/internal/bin.h\"\n#include \"jemalloc/internal/jemalloc_internal_types.h\"\n#include \"jemalloc/internal/sc.h\"\n#include \"jemalloc/internal/sz.h\"\n#include \"jemalloc/internal/ticker.h\"\n#include \"jemalloc/internal/util.h\"\n\nstatic inline bool\ntcache_enabled_get(tsd_t *tsd) {\n\treturn tsd_tcache_enabled_get(tsd);\n}\n\nstatic inline void\ntcache_enabled_set(tsd_t *tsd, bool enabled) {\n\tbool was_enabled = tsd_tcache_enabled_get(tsd);\n\n\tif (!was_enabled && enabled) {\n\t\ttsd_tcache_data_init(tsd);\n\t} else if (was_enabled && !enabled) {\n\t\ttcache_cleanup(tsd);\n\t}\n\t/* Commit the state last.  Above calls check current state. */\n\ttsd_tcache_enabled_set(tsd, enabled);\n\ttsd_slow_update(tsd);\n}\n\nJEMALLOC_ALWAYS_INLINE void\ntcache_event(tsd_t *tsd, tcache_t *tcache) {\n\tif (TCACHE_GC_INCR == 0) {\n\t\treturn;\n\t}\n\n\tif (unlikely(ticker_tick(&tcache->gc_ticker))) {\n\t\ttcache_event_hard(tsd, tcache);\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE void *\ntcache_alloc_small(tsd_t *tsd, arena_t *arena, tcache_t *tcache,\n    size_t size, szind_t binind, bool zero, bool slow_path) {\n\tvoid *ret;\n\tcache_bin_t *bin;\n\tbool tcache_success;\n\tsize_t usize JEMALLOC_CC_SILENCE_INIT(0);\n\n\tassert(binind < SC_NBINS);\n\tbin = tcache_small_bin_get(tcache, binind);\n\tret = cache_bin_alloc_easy(bin, &tcache_success);\n\tassert(tcache_success == (ret != NULL));\n\tif (unlikely(!tcache_success)) {\n\t\tbool tcache_hard_success;\n\t\tarena = arena_choose(tsd, arena);\n\t\tif (unlikely(arena == NULL)) {\n\t\t\treturn NULL;\n\t\t}\n\n\t\tret = tcache_alloc_small_hard(tsd_tsdn(tsd), arena, tcache,\n\t\t    bin, binind, &tcache_hard_success);\n\t\tif (tcache_hard_success == false) {\n\t\t\treturn NULL;\n\t\t}\n\t}\n\n\tassert(ret);\n\t/*\n\t * Only compute usize if required.  The checks in the following if\n\t * statement are all static.\n\t */\n\tif (config_prof || (slow_path && config_fill) || unlikely(zero)) {\n\t\tusize = sz_index2size(binind);\n\t\tassert(tcache_salloc(tsd_tsdn(tsd), ret) == usize);\n\t}\n\n\tif (likely(!zero)) {\n\t\tif (slow_path && config_fill) {\n\t\t\tif (unlikely(opt_junk_alloc)) {\n\t\t\t\tarena_alloc_junk_small(ret, &bin_infos[binind],\n\t\t\t\t    false);\n\t\t\t} else if (unlikely(opt_zero)) {\n\t\t\t\tmemset(ret, 0, usize);\n\t\t\t}\n\t\t}\n\t} else {\n\t\tif (slow_path && config_fill && unlikely(opt_junk_alloc)) {\n\t\t\tarena_alloc_junk_small(ret, &bin_infos[binind], true);\n\t\t}\n\t\tmemset(ret, 0, usize);\n\t}\n\n\tif (config_stats) {\n\t\tbin->tstats.nrequests++;\n\t}\n\tif (config_prof) {\n\t\ttcache->prof_accumbytes += usize;\n\t}\n\ttcache_event(tsd, tcache);\n\treturn ret;\n}\n\nJEMALLOC_ALWAYS_INLINE void *\ntcache_alloc_large(tsd_t *tsd, arena_t *arena, tcache_t *tcache, size_t size,\n    szind_t binind, bool zero, bool slow_path) {\n\tvoid *ret;\n\tcache_bin_t *bin;\n\tbool tcache_success;\n\n\tassert(binind >= SC_NBINS &&binind < nhbins);\n\tbin = tcache_large_bin_get(tcache, binind);\n\tret = cache_bin_alloc_easy(bin, &tcache_success);\n\tassert(tcache_success == (ret != NULL));\n\tif (unlikely(!tcache_success)) {\n\t\t/*\n\t\t * Only allocate one large object at a time, because it's quite\n\t\t * expensive to create one and not use it.\n\t\t */\n\t\tarena = arena_choose(tsd, arena);\n\t\tif (unlikely(arena == NULL)) {\n\t\t\treturn NULL;\n\t\t}\n\n\t\tret = large_malloc(tsd_tsdn(tsd), arena, sz_s2u(size), zero);\n\t\tif (ret == NULL) {\n\t\t\treturn NULL;\n\t\t}\n\t} else {\n\t\tsize_t usize JEMALLOC_CC_SILENCE_INIT(0);\n\n\t\t/* Only compute usize on demand */\n\t\tif (config_prof || (slow_path && config_fill) ||\n\t\t    unlikely(zero)) {\n\t\t\tusize = sz_index2size(binind);\n\t\t\tassert(usize <= tcache_maxclass);\n\t\t}\n\n\t\tif (likely(!zero)) {\n\t\t\tif (slow_path && config_fill) {\n\t\t\t\tif (unlikely(opt_junk_alloc)) {\n\t\t\t\t\tmemset(ret, JEMALLOC_ALLOC_JUNK,\n\t\t\t\t\t    usize);\n\t\t\t\t} else if (unlikely(opt_zero)) {\n\t\t\t\t\tmemset(ret, 0, usize);\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tmemset(ret, 0, usize);\n\t\t}\n\n\t\tif (config_stats) {\n\t\t\tbin->tstats.nrequests++;\n\t\t}\n\t\tif (config_prof) {\n\t\t\ttcache->prof_accumbytes += usize;\n\t\t}\n\t}\n\n\ttcache_event(tsd, tcache);\n\treturn ret;\n}\n\nJEMALLOC_ALWAYS_INLINE void\ntcache_dalloc_small(tsd_t *tsd, tcache_t *tcache, void *ptr, szind_t binind,\n    bool slow_path) {\n\tcache_bin_t *bin;\n\tcache_bin_info_t *bin_info;\n\n\tassert(tcache_salloc(tsd_tsdn(tsd), ptr)\n\t    <= SC_SMALL_MAXCLASS);\n\n\tif (slow_path && config_fill && unlikely(opt_junk_free)) {\n\t\tarena_dalloc_junk_small(ptr, &bin_infos[binind]);\n\t}\n\n\tbin = tcache_small_bin_get(tcache, binind);\n\tbin_info = &tcache_bin_info[binind];\n\tif (unlikely(!cache_bin_dalloc_easy(bin, bin_info, ptr))) {\n\t\ttcache_bin_flush_small(tsd, tcache, bin, binind,\n\t\t    (bin_info->ncached_max >> 1));\n\t\tbool ret = cache_bin_dalloc_easy(bin, bin_info, ptr);\n\t\tassert(ret);\n\t}\n\n\ttcache_event(tsd, tcache);\n}\n\nJEMALLOC_ALWAYS_INLINE void\ntcache_dalloc_large(tsd_t *tsd, tcache_t *tcache, void *ptr, szind_t binind,\n    bool slow_path) {\n\tcache_bin_t *bin;\n\tcache_bin_info_t *bin_info;\n\n\tassert(tcache_salloc(tsd_tsdn(tsd), ptr)\n\t    > SC_SMALL_MAXCLASS);\n\tassert(tcache_salloc(tsd_tsdn(tsd), ptr) <= tcache_maxclass);\n\n\tif (slow_path && config_fill && unlikely(opt_junk_free)) {\n\t\tlarge_dalloc_junk(ptr, sz_index2size(binind));\n\t}\n\n\tbin = tcache_large_bin_get(tcache, binind);\n\tbin_info = &tcache_bin_info[binind];\n\tif (unlikely(bin->ncached == bin_info->ncached_max)) {\n\t\ttcache_bin_flush_large(tsd, bin, binind,\n\t\t    (bin_info->ncached_max >> 1), tcache);\n\t}\n\tassert(bin->ncached < bin_info->ncached_max);\n\tbin->ncached++;\n\t*(bin->avail - bin->ncached) = ptr;\n\n\ttcache_event(tsd, tcache);\n}\n\nJEMALLOC_ALWAYS_INLINE tcache_t *\ntcaches_get(tsd_t *tsd, unsigned ind) {\n\ttcaches_t *elm = &tcaches[ind];\n\tif (unlikely(elm->tcache == NULL)) {\n\t\tmalloc_printf(\"<jemalloc>: invalid tcache id (%u).\\n\", ind);\n\t\tabort();\n\t} else if (unlikely(elm->tcache == TCACHES_ELM_NEED_REINIT)) {\n\t\telm->tcache = tcache_create_explicit(tsd);\n\t}\n\treturn elm->tcache;\n}\n\n#endif /* JEMALLOC_INTERNAL_TCACHE_INLINES_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/tcache_structs.h",
    "content": "#ifndef JEMALLOC_INTERNAL_TCACHE_STRUCTS_H\n#define JEMALLOC_INTERNAL_TCACHE_STRUCTS_H\n\n#include \"jemalloc/internal/cache_bin.h\"\n#include \"jemalloc/internal/ql.h\"\n#include \"jemalloc/internal/sc.h\"\n#include \"jemalloc/internal/ticker.h\"\n#include \"jemalloc/internal/tsd_types.h\"\n\n/* Various uses of this struct need it to be a named type. */\ntypedef ql_elm(tsd_t) tsd_link_t;\n\nstruct tcache_s {\n\t/*\n\t * To minimize our cache-footprint, we put the frequently accessed data\n\t * together at the start of this struct.\n\t */\n\n\t/* Cleared after arena_prof_accum(). */\n\tuint64_t\tprof_accumbytes;\n\t/* Drives incremental GC. */\n\tticker_t\tgc_ticker;\n\t/*\n\t * The pointer stacks associated with bins follow as a contiguous array.\n\t * During tcache initialization, the avail pointer in each element of\n\t * tbins is initialized to point to the proper offset within this array.\n\t */\n\tcache_bin_t\tbins_small[SC_NBINS];\n\n\t/*\n\t * This data is less hot; we can be a little less careful with our\n\t * footprint here.\n\t */\n\t/* Lets us track all the tcaches in an arena. */\n\tql_elm(tcache_t) link;\n\n\t/* Logically scoped to tsd, but put here for cache layout reasons. */\n\tql_elm(tsd_t) tsd_link;\n\tbool in_hook;\n\n\t/*\n\t * The descriptor lets the arena find our cache bins without seeing the\n\t * tcache definition.  This enables arenas to aggregate stats across\n\t * tcaches without having a tcache dependency.\n\t */\n\tcache_bin_array_descriptor_t cache_bin_array_descriptor;\n\n\t/* The arena this tcache is associated with. */\n\tarena_t\t\t*arena;\n\t/* Next bin to GC. */\n\tszind_t\t\tnext_gc_bin;\n\t/* For small bins, fill (ncached_max >> lg_fill_div). */\n\tuint8_t\t\tlg_fill_div[SC_NBINS];\n\t/*\n\t * We put the cache bins for large size classes at the end of the\n\t * struct, since some of them might not get used.  This might end up\n\t * letting us avoid touching an extra page if we don't have to.\n\t */\n\tcache_bin_t\tbins_large[SC_NSIZES-SC_NBINS];\n};\n\n/* Linkage for list of available (previously used) explicit tcache IDs. */\nstruct tcaches_s {\n\tunion {\n\t\ttcache_t\t*tcache;\n\t\ttcaches_t\t*next;\n\t};\n};\n\n#endif /* JEMALLOC_INTERNAL_TCACHE_STRUCTS_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/tcache_types.h",
    "content": "#ifndef JEMALLOC_INTERNAL_TCACHE_TYPES_H\n#define JEMALLOC_INTERNAL_TCACHE_TYPES_H\n\n#include \"jemalloc/internal/sc.h\"\n\ntypedef struct tcache_s tcache_t;\ntypedef struct tcaches_s tcaches_t;\n\n/*\n * tcache pointers close to NULL are used to encode state information that is\n * used for two purposes: preventing thread caching on a per thread basis and\n * cleaning up during thread shutdown.\n */\n#define TCACHE_STATE_DISABLED\t\t((tcache_t *)(uintptr_t)1)\n#define TCACHE_STATE_REINCARNATED\t((tcache_t *)(uintptr_t)2)\n#define TCACHE_STATE_PURGATORY\t\t((tcache_t *)(uintptr_t)3)\n#define TCACHE_STATE_MAX\t\tTCACHE_STATE_PURGATORY\n\n/*\n * Absolute minimum number of cache slots for each small bin.\n */\n#define TCACHE_NSLOTS_SMALL_MIN\t\t20\n\n/*\n * Absolute maximum number of cache slots for each small bin in the thread\n * cache.  This is an additional constraint beyond that imposed as: twice the\n * number of regions per slab for this size class.\n *\n * This constant must be an even number.\n */\n#define TCACHE_NSLOTS_SMALL_MAX\t\t200\n\n/* Number of cache slots for large size classes. */\n#define TCACHE_NSLOTS_LARGE\t\t20\n\n/* (1U << opt_lg_tcache_max) is used to compute tcache_maxclass. */\n#define LG_TCACHE_MAXCLASS_DEFAULT\t15\n\n/*\n * TCACHE_GC_SWEEP is the approximate number of allocation events between\n * full GC sweeps.  Integer rounding may cause the actual number to be\n * slightly higher, since GC is performed incrementally.\n */\n#define TCACHE_GC_SWEEP\t\t\t8192\n\n/* Number of tcache allocation/deallocation events between incremental GCs. */\n#define TCACHE_GC_INCR\t\t\t\t\t\t\t\\\n    ((TCACHE_GC_SWEEP / SC_NBINS) + ((TCACHE_GC_SWEEP / SC_NBINS == 0) ? 0 : 1))\n\n/* Used in TSD static initializer only. Real init in tcache_data_init(). */\n#define TCACHE_ZERO_INITIALIZER {0}\n\n/* Used in TSD static initializer only. Will be initialized to opt_tcache. */\n#define TCACHE_ENABLED_ZERO_INITIALIZER false\n\n/* Used for explicit tcache only. Means flushed but not destroyed. */\n#define TCACHES_ELM_NEED_REINIT ((tcache_t *)(uintptr_t)1)\n\n#endif /* JEMALLOC_INTERNAL_TCACHE_TYPES_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/test_hooks.h",
    "content": "#ifndef JEMALLOC_INTERNAL_TEST_HOOKS_H\n#define JEMALLOC_INTERNAL_TEST_HOOKS_H\n\nextern JEMALLOC_EXPORT void (*test_hooks_arena_new_hook)();\nextern JEMALLOC_EXPORT void (*test_hooks_libc_hook)();\n\n#define JEMALLOC_HOOK(fn, hook) ((void)(hook != NULL && (hook(), 0)), fn)\n\n#define open JEMALLOC_HOOK(open, test_hooks_libc_hook)\n#define read JEMALLOC_HOOK(read, test_hooks_libc_hook)\n#define write JEMALLOC_HOOK(write, test_hooks_libc_hook)\n#define readlink JEMALLOC_HOOK(readlink, test_hooks_libc_hook)\n#define close JEMALLOC_HOOK(close, test_hooks_libc_hook)\n#define creat JEMALLOC_HOOK(creat, test_hooks_libc_hook)\n#define secure_getenv JEMALLOC_HOOK(secure_getenv, test_hooks_libc_hook)\n/* Note that this is undef'd and re-define'd in src/prof.c. */\n#define _Unwind_Backtrace JEMALLOC_HOOK(_Unwind_Backtrace, test_hooks_libc_hook)\n\n#endif /* JEMALLOC_INTERNAL_TEST_HOOKS_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/ticker.h",
    "content": "#ifndef JEMALLOC_INTERNAL_TICKER_H\n#define JEMALLOC_INTERNAL_TICKER_H\n\n#include \"jemalloc/internal/util.h\"\n\n/**\n * A ticker makes it easy to count-down events until some limit.  You\n * ticker_init the ticker to trigger every nticks events.  You then notify it\n * that an event has occurred with calls to ticker_tick (or that nticks events\n * have occurred with a call to ticker_ticks), which will return true (and reset\n * the counter) if the countdown hit zero.\n */\n\ntypedef struct {\n\tint32_t tick;\n\tint32_t nticks;\n} ticker_t;\n\nstatic inline void\nticker_init(ticker_t *ticker, int32_t nticks) {\n\tticker->tick = nticks;\n\tticker->nticks = nticks;\n}\n\nstatic inline void\nticker_copy(ticker_t *ticker, const ticker_t *other) {\n\t*ticker = *other;\n}\n\nstatic inline int32_t\nticker_read(const ticker_t *ticker) {\n\treturn ticker->tick;\n}\n\n/*\n * Not intended to be a public API.  Unfortunately, on x86, neither gcc nor\n * clang seems smart enough to turn\n *   ticker->tick -= nticks;\n *   if (unlikely(ticker->tick < 0)) {\n *     fixup ticker\n *     return true;\n *   }\n *   return false;\n * into\n *   subq %nticks_reg, (%ticker_reg)\n *   js fixup ticker\n *\n * unless we force \"fixup ticker\" out of line.  In that case, gcc gets it right,\n * but clang now does worse than before.  So, on x86 with gcc, we force it out\n * of line, but otherwise let the inlining occur.  Ordinarily this wouldn't be\n * worth the hassle, but this is on the fast path of both malloc and free (via\n * tcache_event).\n */\n#if defined(__GNUC__) && !defined(__clang__)\t\t\t\t\\\n    && (defined(__x86_64__) || defined(__i386__))\nJEMALLOC_NOINLINE\n#endif\nstatic bool\nticker_fixup(ticker_t *ticker) {\n\tticker->tick = ticker->nticks;\n\treturn true;\n}\n\nstatic inline bool\nticker_ticks(ticker_t *ticker, int32_t nticks) {\n\tticker->tick -= nticks;\n\tif (unlikely(ticker->tick < 0)) {\n\t\treturn ticker_fixup(ticker);\n\t}\n\treturn false;\n}\n\nstatic inline bool\nticker_tick(ticker_t *ticker) {\n\treturn ticker_ticks(ticker, 1);\n}\n\n/* \n * Try to tick.  If ticker would fire, return true, but rely on\n * slowpath to reset ticker.\n */\nstatic inline bool\nticker_trytick(ticker_t *ticker) {\n\t--ticker->tick;\n\tif (unlikely(ticker->tick < 0)) {\n\t\treturn true;\n\t}\n\treturn false;\n}\n\n#endif /* JEMALLOC_INTERNAL_TICKER_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/tsd.h",
    "content": "#ifndef JEMALLOC_INTERNAL_TSD_H\n#define JEMALLOC_INTERNAL_TSD_H\n\n#include \"jemalloc/internal/arena_types.h\"\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/bin_types.h\"\n#include \"jemalloc/internal/jemalloc_internal_externs.h\"\n#include \"jemalloc/internal/prof_types.h\"\n#include \"jemalloc/internal/ql.h\"\n#include \"jemalloc/internal/rtree_tsd.h\"\n#include \"jemalloc/internal/tcache_types.h\"\n#include \"jemalloc/internal/tcache_structs.h\"\n#include \"jemalloc/internal/util.h\"\n#include \"jemalloc/internal/witness.h\"\n\n/*\n * Thread-Specific-Data layout\n * --- data accessed on tcache fast path: state, rtree_ctx, stats, prof ---\n * s: state\n * e: tcache_enabled\n * m: thread_allocated (config_stats)\n * f: thread_deallocated (config_stats)\n * p: prof_tdata (config_prof)\n * c: rtree_ctx (rtree cache accessed on deallocation)\n * t: tcache\n * --- data not accessed on tcache fast path: arena-related fields ---\n * d: arenas_tdata_bypass\n * r: reentrancy_level\n * x: narenas_tdata\n * i: iarena\n * a: arena\n * o: arenas_tdata\n * Loading TSD data is on the critical path of basically all malloc operations.\n * In particular, tcache and rtree_ctx rely on hot CPU cache to be effective.\n * Use a compact layout to reduce cache footprint.\n * +--- 64-bit and 64B cacheline; 1B each letter; First byte on the left. ---+\n * |----------------------------  1st cacheline  ----------------------------|\n * | sedrxxxx mmmmmmmm ffffffff pppppppp [c * 32  ........ ........ .......] |\n * |----------------------------  2nd cacheline  ----------------------------|\n * | [c * 64  ........ ........ ........ ........ ........ ........ .......] |\n * |----------------------------  3nd cacheline  ----------------------------|\n * | [c * 32  ........ ........ .......] iiiiiiii aaaaaaaa oooooooo [t...... |\n * +-------------------------------------------------------------------------+\n * Note: the entire tcache is embedded into TSD and spans multiple cachelines.\n *\n * The last 3 members (i, a and o) before tcache isn't really needed on tcache\n * fast path.  However we have a number of unused tcache bins and witnesses\n * (never touched unless config_debug) at the end of tcache, so we place them\n * there to avoid breaking the cachelines and possibly paging in an extra page.\n */\n#ifdef JEMALLOC_JET\ntypedef void (*test_callback_t)(int *);\n#  define MALLOC_TSD_TEST_DATA_INIT 0x72b65c10\n#  define MALLOC_TEST_TSD \\\n    O(test_data,\t\tint,\t\t\tint)\t\t\\\n    O(test_callback,\t\ttest_callback_t,\tint)\n#  define MALLOC_TEST_TSD_INITIALIZER , MALLOC_TSD_TEST_DATA_INIT, NULL\n#else\n#  define MALLOC_TEST_TSD\n#  define MALLOC_TEST_TSD_INITIALIZER\n#endif\n\n/*  O(name,\t\t\ttype,\t\t\tnullable type */\n#define MALLOC_TSD\t\t\t\t\t\t\t\\\n    O(tcache_enabled,\t\tbool,\t\t\tbool)\t\t\\\n    O(arenas_tdata_bypass,\tbool,\t\t\tbool)\t\t\\\n    O(reentrancy_level,\t\tint8_t,\t\t\tint8_t)\t\t\\\n    O(narenas_tdata,\t\tuint32_t,\t\tuint32_t)\t\\\n    O(offset_state,\t\tuint64_t,\t\tuint64_t)\t\\\n    O(thread_allocated,\t\tuint64_t,\t\tuint64_t)\t\\\n    O(thread_deallocated,\tuint64_t,\t\tuint64_t)\t\\\n    O(bytes_until_sample,\tint64_t,\t\tint64_t)\t\\\n    O(prof_tdata,\t\tprof_tdata_t *,\t\tprof_tdata_t *)\t\\\n    O(rtree_ctx,\t\trtree_ctx_t,\t\trtree_ctx_t)\t\\\n    O(iarena,\t\t\tarena_t *,\t\tarena_t *)\t\\\n    O(arena,\t\t\tarena_t *,\t\tarena_t *)\t\\\n    O(arenas_tdata,\t\tarena_tdata_t *,\tarena_tdata_t *)\\\n    O(binshards,\t\ttsd_binshards_t,\ttsd_binshards_t)\\\n    O(tcache,\t\t\ttcache_t,\t\ttcache_t)\t\\\n    O(witness_tsd,              witness_tsd_t,\t\twitness_tsdn_t)\t\\\n    MALLOC_TEST_TSD\n\n#define TSD_INITIALIZER {\t\t\t\t\t\t\\\n    ATOMIC_INIT(tsd_state_uninitialized),\t\t\t\t\\\n    TCACHE_ENABLED_ZERO_INITIALIZER,\t\t\t\t\t\\\n    false,\t\t\t\t\t\t\t\t\\\n    0,\t\t\t\t\t\t\t\t\t\\\n    0,\t\t\t\t\t\t\t\t\t\\\n    0,\t\t\t\t\t\t\t\t\t\\\n    0,\t\t\t\t\t\t\t\t\t\\\n    0,\t\t\t\t\t\t\t\t\t\\\n    0,\t\t\t\t\t\t\t\t\t\\\n    NULL,\t\t\t\t\t\t\t\t\\\n    RTREE_CTX_ZERO_INITIALIZER,\t\t\t\t\t\t\\\n    NULL,\t\t\t\t\t\t\t\t\\\n    NULL,\t\t\t\t\t\t\t\t\\\n    NULL,\t\t\t\t\t\t\t\t\\\n    TSD_BINSHARDS_ZERO_INITIALIZER,\t\t\t\t\t\\\n    TCACHE_ZERO_INITIALIZER,\t\t\t\t\t\t\\\n    WITNESS_TSD_INITIALIZER\t\t\t\t\t\t\\\n    MALLOC_TEST_TSD_INITIALIZER\t\t\t\t\t\t\\\n}\n\nvoid *malloc_tsd_malloc(size_t size);\nvoid malloc_tsd_dalloc(void *wrapper);\nvoid malloc_tsd_cleanup_register(bool (*f)(void));\ntsd_t *malloc_tsd_boot0(void);\nvoid malloc_tsd_boot1(void);\nvoid tsd_cleanup(void *arg);\ntsd_t *tsd_fetch_slow(tsd_t *tsd, bool internal);\nvoid tsd_state_set(tsd_t *tsd, uint8_t new_state);\nvoid tsd_slow_update(tsd_t *tsd);\nvoid tsd_prefork(tsd_t *tsd);\nvoid tsd_postfork_parent(tsd_t *tsd);\nvoid tsd_postfork_child(tsd_t *tsd);\n\n/*\n * Call ..._inc when your module wants to take all threads down the slow paths,\n * and ..._dec when it no longer needs to.\n */\nvoid tsd_global_slow_inc(tsdn_t *tsdn);\nvoid tsd_global_slow_dec(tsdn_t *tsdn);\nbool tsd_global_slow();\n\nenum {\n\t/* Common case --> jnz. */\n\ttsd_state_nominal = 0,\n\t/* Initialized but on slow path. */\n\ttsd_state_nominal_slow = 1,\n\t/*\n\t * Some thread has changed global state in such a way that all nominal\n\t * threads need to recompute their fast / slow status the next time they\n\t * get a chance.\n\t *\n\t * Any thread can change another thread's status *to* recompute, but\n\t * threads are the only ones who can change their status *from*\n\t * recompute.\n\t */\n\ttsd_state_nominal_recompute = 2,\n\t/*\n\t * The above nominal states should be lower values.  We use\n\t * tsd_nominal_max to separate nominal states from threads in the\n\t * process of being born / dying.\n\t */\n\ttsd_state_nominal_max = 2,\n\n\t/*\n\t * A thread might free() during its death as its only allocator action;\n\t * in such scenarios, we need tsd, but set up in such a way that no\n\t * cleanup is necessary.\n\t */\n\ttsd_state_minimal_initialized = 3,\n\t/* States during which we know we're in thread death. */\n\ttsd_state_purgatory = 4,\n\ttsd_state_reincarnated = 5,\n\t/*\n\t * What it says on the tin; tsd that hasn't been initialized.  Note\n\t * that even when the tsd struct lives in TLS, when need to keep track\n\t * of stuff like whether or not our pthread destructors have been\n\t * scheduled, so this really truly is different than the nominal state.\n\t */\n\ttsd_state_uninitialized = 6\n};\n\n/*\n * Some TSD accesses can only be done in a nominal state.  To enforce this, we\n * wrap TSD member access in a function that asserts on TSD state, and mangle\n * field names to prevent touching them accidentally.\n */\n#define TSD_MANGLE(n) cant_access_tsd_items_directly_use_a_getter_or_setter_##n\n\n#ifdef JEMALLOC_U8_ATOMICS\n#  define tsd_state_t atomic_u8_t\n#  define tsd_atomic_load atomic_load_u8\n#  define tsd_atomic_store atomic_store_u8\n#  define tsd_atomic_exchange atomic_exchange_u8\n#else\n#  define tsd_state_t atomic_u32_t\n#  define tsd_atomic_load atomic_load_u32\n#  define tsd_atomic_store atomic_store_u32\n#  define tsd_atomic_exchange atomic_exchange_u32\n#endif\n\n/* The actual tsd. */\nstruct tsd_s {\n\t/*\n\t * The contents should be treated as totally opaque outside the tsd\n\t * module.  Access any thread-local state through the getters and\n\t * setters below.\n\t */\n\n\t/*\n\t * We manually limit the state to just a single byte.  Unless the 8-bit\n\t * atomics are unavailable (which is rare).\n\t */\n\ttsd_state_t state;\n#define O(n, t, nt)\t\t\t\t\t\t\t\\\n\tt TSD_MANGLE(n);\nMALLOC_TSD\n#undef O\n};\n\nJEMALLOC_ALWAYS_INLINE uint8_t\ntsd_state_get(tsd_t *tsd) {\n\t/*\n\t * This should be atomic.  Unfortunately, compilers right now can't tell\n\t * that this can be done as a memory comparison, and forces a load into\n\t * a register that hurts fast-path performance.\n\t */\n\t/* return atomic_load_u8(&tsd->state, ATOMIC_RELAXED); */\n\treturn *(uint8_t *)&tsd->state;\n}\n\n/*\n * Wrapper around tsd_t that makes it possible to avoid implicit conversion\n * between tsd_t and tsdn_t, where tsdn_t is \"nullable\" and has to be\n * explicitly converted to tsd_t, which is non-nullable.\n */\nstruct tsdn_s {\n\ttsd_t tsd;\n};\n#define TSDN_NULL ((tsdn_t *)0)\nJEMALLOC_ALWAYS_INLINE tsdn_t *\ntsd_tsdn(tsd_t *tsd) {\n\treturn (tsdn_t *)tsd;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsdn_null(const tsdn_t *tsdn) {\n\treturn tsdn == NULL;\n}\n\nJEMALLOC_ALWAYS_INLINE tsd_t *\ntsdn_tsd(tsdn_t *tsdn) {\n\tassert(!tsdn_null(tsdn));\n\n\treturn &tsdn->tsd;\n}\n\n/*\n * We put the platform-specific data declarations and inlines into their own\n * header files to avoid cluttering this file.  They define tsd_boot0,\n * tsd_boot1, tsd_boot, tsd_booted_get, tsd_get_allocates, tsd_get, and tsd_set.\n */\n#ifdef JEMALLOC_MALLOC_THREAD_CLEANUP\n#include \"jemalloc/internal/tsd_malloc_thread_cleanup.h\"\n#elif (defined(JEMALLOC_TLS))\n#include \"jemalloc/internal/tsd_tls.h\"\n#elif (defined(_WIN32))\n#include \"jemalloc/internal/tsd_win.h\"\n#else\n#include \"jemalloc/internal/tsd_generic.h\"\n#endif\n\n/*\n * tsd_foop_get_unsafe(tsd) returns a pointer to the thread-local instance of\n * foo.  This omits some safety checks, and so can be used during tsd\n * initialization and cleanup.\n */\n#define O(n, t, nt)\t\t\t\t\t\t\t\\\nJEMALLOC_ALWAYS_INLINE t *\t\t\t\t\t\t\\\ntsd_##n##p_get_unsafe(tsd_t *tsd) {\t\t\t\t\t\\\n\treturn &tsd->TSD_MANGLE(n);\t\t\t\t\t\\\n}\nMALLOC_TSD\n#undef O\n\n/* tsd_foop_get(tsd) returns a pointer to the thread-local instance of foo. */\n#define O(n, t, nt)\t\t\t\t\t\t\t\\\nJEMALLOC_ALWAYS_INLINE t *\t\t\t\t\t\t\\\ntsd_##n##p_get(tsd_t *tsd) {\t\t\t\t\t\t\\\n\t/*\t\t\t\t\t\t\t\t\\\n\t * Because the state might change asynchronously if it's\t\\\n\t * nominal, we need to make sure that we only read it once.\t\\\n\t */\t\t\t\t\t\t\t\t\\\n\tuint8_t state = tsd_state_get(tsd);\t\t\t\t\\\n\tassert(state == tsd_state_nominal ||\t\t\t\t\\\n\t    state == tsd_state_nominal_slow ||\t\t\t\t\\\n\t    state == tsd_state_nominal_recompute ||\t\t\t\\\n\t    state == tsd_state_reincarnated ||\t\t\t\t\\\n\t    state == tsd_state_minimal_initialized);\t\t\t\\\n\treturn tsd_##n##p_get_unsafe(tsd);\t\t\t\t\\\n}\nMALLOC_TSD\n#undef O\n\n/*\n * tsdn_foop_get(tsdn) returns either the thread-local instance of foo (if tsdn\n * isn't NULL), or NULL (if tsdn is NULL), cast to the nullable pointer type.\n */\n#define O(n, t, nt)\t\t\t\t\t\t\t\\\nJEMALLOC_ALWAYS_INLINE nt *\t\t\t\t\t\t\\\ntsdn_##n##p_get(tsdn_t *tsdn) {\t\t\t\t\t\t\\\n\tif (tsdn_null(tsdn)) {\t\t\t\t\t\t\\\n\t\treturn NULL;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\ttsd_t *tsd = tsdn_tsd(tsdn);\t\t\t\t\t\\\n\treturn (nt *)tsd_##n##p_get(tsd);\t\t\t\t\\\n}\nMALLOC_TSD\n#undef O\n\n/* tsd_foo_get(tsd) returns the value of the thread-local instance of foo. */\n#define O(n, t, nt)\t\t\t\t\t\t\t\\\nJEMALLOC_ALWAYS_INLINE t\t\t\t\t\t\t\\\ntsd_##n##_get(tsd_t *tsd) {\t\t\t\t\t\t\\\n\treturn *tsd_##n##p_get(tsd);\t\t\t\t\t\\\n}\nMALLOC_TSD\n#undef O\n\n/* tsd_foo_set(tsd, val) updates the thread-local instance of foo to be val. */\n#define O(n, t, nt)\t\t\t\t\t\t\t\\\nJEMALLOC_ALWAYS_INLINE void\t\t\t\t\t\t\\\ntsd_##n##_set(tsd_t *tsd, t val) {\t\t\t\t\t\\\n\tassert(tsd_state_get(tsd) != tsd_state_reincarnated &&\t\t\\\n\t    tsd_state_get(tsd) != tsd_state_minimal_initialized);\t\\\n\t*tsd_##n##p_get(tsd) = val;\t\t\t\t\t\\\n}\nMALLOC_TSD\n#undef O\n\nJEMALLOC_ALWAYS_INLINE void\ntsd_assert_fast(tsd_t *tsd) {\n\t/*\n\t * Note that our fastness assertion does *not* include global slowness\n\t * counters; it's not in general possible to ensure that they won't\n\t * change asynchronously from underneath us.\n\t */\n\tassert(!malloc_slow && tsd_tcache_enabled_get(tsd) &&\n\t    tsd_reentrancy_level_get(tsd) == 0);\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_fast(tsd_t *tsd) {\n\tbool fast = (tsd_state_get(tsd) == tsd_state_nominal);\n\tif (fast) {\n\t\ttsd_assert_fast(tsd);\n\t}\n\n\treturn fast;\n}\n\nJEMALLOC_ALWAYS_INLINE tsd_t *\ntsd_fetch_impl(bool init, bool minimal) {\n\ttsd_t *tsd = tsd_get(init);\n\n\tif (!init && tsd_get_allocates() && tsd == NULL) {\n\t\treturn NULL;\n\t}\n\tassert(tsd != NULL);\n\n\tif (unlikely(tsd_state_get(tsd) != tsd_state_nominal)) {\n\t\treturn tsd_fetch_slow(tsd, minimal);\n\t}\n\tassert(tsd_fast(tsd));\n\ttsd_assert_fast(tsd);\n\n\treturn tsd;\n}\n\n/* Get a minimal TSD that requires no cleanup.  See comments in free(). */\nJEMALLOC_ALWAYS_INLINE tsd_t *\ntsd_fetch_min(void) {\n\treturn tsd_fetch_impl(true, true);\n}\n\n/* For internal background threads use only. */\nJEMALLOC_ALWAYS_INLINE tsd_t *\ntsd_internal_fetch(void) {\n\ttsd_t *tsd = tsd_fetch_min();\n\t/* Use reincarnated state to prevent full initialization. */\n\ttsd_state_set(tsd, tsd_state_reincarnated);\n\n\treturn tsd;\n}\n\nJEMALLOC_ALWAYS_INLINE tsd_t *\ntsd_fetch(void) {\n\treturn tsd_fetch_impl(true, false);\n}\n\nstatic inline bool\ntsd_nominal(tsd_t *tsd) {\n\treturn (tsd_state_get(tsd) <= tsd_state_nominal_max);\n}\n\nJEMALLOC_ALWAYS_INLINE tsdn_t *\ntsdn_fetch(void) {\n\tif (!tsd_booted_get()) {\n\t\treturn NULL;\n\t}\n\n\treturn tsd_tsdn(tsd_fetch_impl(false, false));\n}\n\nJEMALLOC_ALWAYS_INLINE rtree_ctx_t *\ntsd_rtree_ctx(tsd_t *tsd) {\n\treturn tsd_rtree_ctxp_get(tsd);\n}\n\nJEMALLOC_ALWAYS_INLINE rtree_ctx_t *\ntsdn_rtree_ctx(tsdn_t *tsdn, rtree_ctx_t *fallback) {\n\t/*\n\t * If tsd cannot be accessed, initialize the fallback rtree_ctx and\n\t * return a pointer to it.\n\t */\n\tif (unlikely(tsdn_null(tsdn))) {\n\t\trtree_ctx_data_init(fallback);\n\t\treturn fallback;\n\t}\n\treturn tsd_rtree_ctx(tsdn_tsd(tsdn));\n}\n\n#endif /* JEMALLOC_INTERNAL_TSD_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/tsd_generic.h",
    "content": "#ifdef JEMALLOC_INTERNAL_TSD_GENERIC_H\n#error This file should be included only once, by tsd.h.\n#endif\n#define JEMALLOC_INTERNAL_TSD_GENERIC_H\n\ntypedef struct tsd_init_block_s tsd_init_block_t;\nstruct tsd_init_block_s {\n\tql_elm(tsd_init_block_t) link;\n\tpthread_t thread;\n\tvoid *data;\n};\n\n/* Defined in tsd.c, to allow the mutex headers to have tsd dependencies. */\ntypedef struct tsd_init_head_s tsd_init_head_t;\n\ntypedef struct {\n\tbool initialized;\n\ttsd_t val;\n} tsd_wrapper_t;\n\nvoid *tsd_init_check_recursion(tsd_init_head_t *head,\n    tsd_init_block_t *block);\nvoid tsd_init_finish(tsd_init_head_t *head, tsd_init_block_t *block);\n\nextern pthread_key_t tsd_tsd;\nextern tsd_init_head_t tsd_init_head;\nextern tsd_wrapper_t tsd_boot_wrapper;\nextern bool tsd_booted;\n\n/* Initialization/cleanup. */\nJEMALLOC_ALWAYS_INLINE void\ntsd_cleanup_wrapper(void *arg) {\n\ttsd_wrapper_t *wrapper = (tsd_wrapper_t *)arg;\n\n\tif (wrapper->initialized) {\n\t\twrapper->initialized = false;\n\t\ttsd_cleanup(&wrapper->val);\n\t\tif (wrapper->initialized) {\n\t\t\t/* Trigger another cleanup round. */\n\t\t\tif (pthread_setspecific(tsd_tsd, (void *)wrapper) != 0)\n\t\t\t{\n\t\t\t\tmalloc_write(\"<jemalloc>: Error setting TSD\\n\");\n\t\t\t\tif (opt_abort) {\n\t\t\t\t\tabort();\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t}\n\tmalloc_tsd_dalloc(wrapper);\n}\n\nJEMALLOC_ALWAYS_INLINE void\ntsd_wrapper_set(tsd_wrapper_t *wrapper) {\n\tif (pthread_setspecific(tsd_tsd, (void *)wrapper) != 0) {\n\t\tmalloc_write(\"<jemalloc>: Error setting TSD\\n\");\n\t\tabort();\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE tsd_wrapper_t *\ntsd_wrapper_get(bool init) {\n\ttsd_wrapper_t *wrapper = (tsd_wrapper_t *)pthread_getspecific(tsd_tsd);\n\n\tif (init && unlikely(wrapper == NULL)) {\n\t\ttsd_init_block_t block;\n\t\twrapper = (tsd_wrapper_t *)\n\t\t    tsd_init_check_recursion(&tsd_init_head, &block);\n\t\tif (wrapper) {\n\t\t\treturn wrapper;\n\t\t}\n\t\twrapper = (tsd_wrapper_t *)\n\t\t    malloc_tsd_malloc(sizeof(tsd_wrapper_t));\n\t\tblock.data = (void *)wrapper;\n\t\tif (wrapper == NULL) {\n\t\t\tmalloc_write(\"<jemalloc>: Error allocating TSD\\n\");\n\t\t\tabort();\n\t\t} else {\n\t\t\twrapper->initialized = false;\n      JEMALLOC_DIAGNOSTIC_PUSH\n      JEMALLOC_DIAGNOSTIC_IGNORE_MISSING_STRUCT_FIELD_INITIALIZERS\n\t\t\ttsd_t initializer = TSD_INITIALIZER;\n      JEMALLOC_DIAGNOSTIC_POP\n\t\t\twrapper->val = initializer;\n\t\t}\n\t\ttsd_wrapper_set(wrapper);\n\t\ttsd_init_finish(&tsd_init_head, &block);\n\t}\n\treturn wrapper;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_boot0(void) {\n\tif (pthread_key_create(&tsd_tsd, tsd_cleanup_wrapper) != 0) {\n\t\treturn true;\n\t}\n\ttsd_wrapper_set(&tsd_boot_wrapper);\n\ttsd_booted = true;\n\treturn false;\n}\n\nJEMALLOC_ALWAYS_INLINE void\ntsd_boot1(void) {\n\ttsd_wrapper_t *wrapper;\n\twrapper = (tsd_wrapper_t *)malloc_tsd_malloc(sizeof(tsd_wrapper_t));\n\tif (wrapper == NULL) {\n\t\tmalloc_write(\"<jemalloc>: Error allocating TSD\\n\");\n\t\tabort();\n\t}\n\ttsd_boot_wrapper.initialized = false;\n\ttsd_cleanup(&tsd_boot_wrapper.val);\n\twrapper->initialized = false;\n  JEMALLOC_DIAGNOSTIC_PUSH\n  JEMALLOC_DIAGNOSTIC_IGNORE_MISSING_STRUCT_FIELD_INITIALIZERS\n\ttsd_t initializer = TSD_INITIALIZER;\n  JEMALLOC_DIAGNOSTIC_POP\n\twrapper->val = initializer;\n\ttsd_wrapper_set(wrapper);\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_boot(void) {\n\tif (tsd_boot0()) {\n\t\treturn true;\n\t}\n\ttsd_boot1();\n\treturn false;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_booted_get(void) {\n\treturn tsd_booted;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_get_allocates(void) {\n\treturn true;\n}\n\n/* Get/set. */\nJEMALLOC_ALWAYS_INLINE tsd_t *\ntsd_get(bool init) {\n\ttsd_wrapper_t *wrapper;\n\n\tassert(tsd_booted);\n\twrapper = tsd_wrapper_get(init);\n\tif (tsd_get_allocates() && !init && wrapper == NULL) {\n\t\treturn NULL;\n\t}\n\treturn &wrapper->val;\n}\n\nJEMALLOC_ALWAYS_INLINE void\ntsd_set(tsd_t *val) {\n\ttsd_wrapper_t *wrapper;\n\n\tassert(tsd_booted);\n\twrapper = tsd_wrapper_get(true);\n\tif (likely(&wrapper->val != val)) {\n\t\twrapper->val = *(val);\n\t}\n\twrapper->initialized = true;\n}\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/tsd_malloc_thread_cleanup.h",
    "content": "#ifdef JEMALLOC_INTERNAL_TSD_MALLOC_THREAD_CLEANUP_H\n#error This file should be included only once, by tsd.h.\n#endif\n#define JEMALLOC_INTERNAL_TSD_MALLOC_THREAD_CLEANUP_H\n\n#define JEMALLOC_TSD_TYPE_ATTR(type) __thread type JEMALLOC_TLS_MODEL\n\nextern JEMALLOC_TSD_TYPE_ATTR(tsd_t) tsd_tls;\nextern JEMALLOC_TSD_TYPE_ATTR(bool) tsd_initialized;\nextern bool tsd_booted;\n\n/* Initialization/cleanup. */\nJEMALLOC_ALWAYS_INLINE bool\ntsd_cleanup_wrapper(void) {\n\tif (tsd_initialized) {\n\t\ttsd_initialized = false;\n\t\ttsd_cleanup(&tsd_tls);\n\t}\n\treturn tsd_initialized;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_boot0(void) {\n\tmalloc_tsd_cleanup_register(&tsd_cleanup_wrapper);\n\ttsd_booted = true;\n\treturn false;\n}\n\nJEMALLOC_ALWAYS_INLINE void\ntsd_boot1(void) {\n\t/* Do nothing. */\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_boot(void) {\n\treturn tsd_boot0();\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_booted_get(void) {\n\treturn tsd_booted;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_get_allocates(void) {\n\treturn false;\n}\n\n/* Get/set. */\nJEMALLOC_ALWAYS_INLINE tsd_t *\ntsd_get(bool init) {\n\treturn &tsd_tls;\n}\nJEMALLOC_ALWAYS_INLINE void\ntsd_set(tsd_t *val) {\n\tassert(tsd_booted);\n\tif (likely(&tsd_tls != val)) {\n\t\ttsd_tls = (*val);\n\t}\n\ttsd_initialized = true;\n}\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/tsd_tls.h",
    "content": "#ifdef JEMALLOC_INTERNAL_TSD_TLS_H\n#error This file should be included only once, by tsd.h.\n#endif\n#define JEMALLOC_INTERNAL_TSD_TLS_H\n\n#define JEMALLOC_TSD_TYPE_ATTR(type) __thread type JEMALLOC_TLS_MODEL\n\nextern JEMALLOC_TSD_TYPE_ATTR(tsd_t) tsd_tls;\nextern pthread_key_t tsd_tsd;\nextern bool tsd_booted;\n\n/* Initialization/cleanup. */\nJEMALLOC_ALWAYS_INLINE bool\ntsd_boot0(void) {\n\tif (pthread_key_create(&tsd_tsd, &tsd_cleanup) != 0) {\n\t\treturn true;\n\t}\n\ttsd_booted = true;\n\treturn false;\n}\n\nJEMALLOC_ALWAYS_INLINE void\ntsd_boot1(void) {\n\t/* Do nothing. */\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_boot(void) {\n\treturn tsd_boot0();\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_booted_get(void) {\n\treturn tsd_booted;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_get_allocates(void) {\n\treturn false;\n}\n\n/* Get/set. */\nJEMALLOC_ALWAYS_INLINE tsd_t *\ntsd_get(bool init) {\n\treturn &tsd_tls;\n}\n\nJEMALLOC_ALWAYS_INLINE void\ntsd_set(tsd_t *val) {\n\tassert(tsd_booted);\n\tif (likely(&tsd_tls != val)) {\n\t\ttsd_tls = (*val);\n\t}\n\tif (pthread_setspecific(tsd_tsd, (void *)(&tsd_tls)) != 0) {\n\t\tmalloc_write(\"<jemalloc>: Error setting tsd.\\n\");\n\t\tif (opt_abort) {\n\t\t\tabort();\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/tsd_types.h",
    "content": "#ifndef JEMALLOC_INTERNAL_TSD_TYPES_H\n#define JEMALLOC_INTERNAL_TSD_TYPES_H\n\n#define MALLOC_TSD_CLEANUPS_MAX\t2\n\ntypedef struct tsd_s tsd_t;\ntypedef struct tsdn_s tsdn_t;\ntypedef bool (*malloc_tsd_cleanup_t)(void);\n\n#endif /* JEMALLOC_INTERNAL_TSD_TYPES_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/tsd_win.h",
    "content": "#ifdef JEMALLOC_INTERNAL_TSD_WIN_H\n#error This file should be included only once, by tsd.h.\n#endif\n#define JEMALLOC_INTERNAL_TSD_WIN_H\n\ntypedef struct {\n\tbool initialized;\n\ttsd_t val;\n} tsd_wrapper_t;\n\nextern DWORD tsd_tsd;\nextern tsd_wrapper_t tsd_boot_wrapper;\nextern bool tsd_booted;\n\n/* Initialization/cleanup. */\nJEMALLOC_ALWAYS_INLINE bool\ntsd_cleanup_wrapper(void) {\n\tDWORD error = GetLastError();\n\ttsd_wrapper_t *wrapper = (tsd_wrapper_t *)TlsGetValue(tsd_tsd);\n\tSetLastError(error);\n\n\tif (wrapper == NULL) {\n\t\treturn false;\n\t}\n\n\tif (wrapper->initialized) {\n\t\twrapper->initialized = false;\n\t\ttsd_cleanup(&wrapper->val);\n\t\tif (wrapper->initialized) {\n\t\t\t/* Trigger another cleanup round. */\n\t\t\treturn true;\n\t\t}\n\t}\n\tmalloc_tsd_dalloc(wrapper);\n\treturn false;\n}\n\nJEMALLOC_ALWAYS_INLINE void\ntsd_wrapper_set(tsd_wrapper_t *wrapper) {\n\tif (!TlsSetValue(tsd_tsd, (void *)wrapper)) {\n\t\tmalloc_write(\"<jemalloc>: Error setting TSD\\n\");\n\t\tabort();\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE tsd_wrapper_t *\ntsd_wrapper_get(bool init) {\n\tDWORD error = GetLastError();\n\ttsd_wrapper_t *wrapper = (tsd_wrapper_t *) TlsGetValue(tsd_tsd);\n\tSetLastError(error);\n\n\tif (init && unlikely(wrapper == NULL)) {\n\t\twrapper = (tsd_wrapper_t *)\n\t\t    malloc_tsd_malloc(sizeof(tsd_wrapper_t));\n\t\tif (wrapper == NULL) {\n\t\t\tmalloc_write(\"<jemalloc>: Error allocating TSD\\n\");\n\t\t\tabort();\n\t\t} else {\n\t\t\twrapper->initialized = false;\n\t\t\t/* MSVC is finicky about aggregate initialization. */\n\t\t\ttsd_t tsd_initializer = TSD_INITIALIZER;\n\t\t\twrapper->val = tsd_initializer;\n\t\t}\n\t\ttsd_wrapper_set(wrapper);\n\t}\n\treturn wrapper;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_boot0(void) {\n\ttsd_tsd = TlsAlloc();\n\tif (tsd_tsd == TLS_OUT_OF_INDEXES) {\n\t\treturn true;\n\t}\n\tmalloc_tsd_cleanup_register(&tsd_cleanup_wrapper);\n\ttsd_wrapper_set(&tsd_boot_wrapper);\n\ttsd_booted = true;\n\treturn false;\n}\n\nJEMALLOC_ALWAYS_INLINE void\ntsd_boot1(void) {\n\ttsd_wrapper_t *wrapper;\n\twrapper = (tsd_wrapper_t *)\n\t    malloc_tsd_malloc(sizeof(tsd_wrapper_t));\n\tif (wrapper == NULL) {\n\t\tmalloc_write(\"<jemalloc>: Error allocating TSD\\n\");\n\t\tabort();\n\t}\n\ttsd_boot_wrapper.initialized = false;\n\ttsd_cleanup(&tsd_boot_wrapper.val);\n\twrapper->initialized = false;\n\ttsd_t initializer = TSD_INITIALIZER;\n\twrapper->val = initializer;\n\ttsd_wrapper_set(wrapper);\n}\nJEMALLOC_ALWAYS_INLINE bool\ntsd_boot(void) {\n\tif (tsd_boot0()) {\n\t\treturn true;\n\t}\n\ttsd_boot1();\n\treturn false;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_booted_get(void) {\n\treturn tsd_booted;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_get_allocates(void) {\n\treturn true;\n}\n\n/* Get/set. */\nJEMALLOC_ALWAYS_INLINE tsd_t *\ntsd_get(bool init) {\n\ttsd_wrapper_t *wrapper;\n\n\tassert(tsd_booted);\n\twrapper = tsd_wrapper_get(init);\n\tif (tsd_get_allocates() && !init && wrapper == NULL) {\n\t\treturn NULL;\n\t}\n\treturn &wrapper->val;\n}\n\nJEMALLOC_ALWAYS_INLINE void\ntsd_set(tsd_t *val) {\n\ttsd_wrapper_t *wrapper;\n\n\tassert(tsd_booted);\n\twrapper = tsd_wrapper_get(true);\n\tif (likely(&wrapper->val != val)) {\n\t\twrapper->val = *(val);\n\t}\n\twrapper->initialized = true;\n}\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/util.h",
    "content": "#ifndef JEMALLOC_INTERNAL_UTIL_H\n#define JEMALLOC_INTERNAL_UTIL_H\n\n#define UTIL_INLINE static inline\n\n/* Junk fill patterns. */\n#ifndef JEMALLOC_ALLOC_JUNK\n#  define JEMALLOC_ALLOC_JUNK\t((uint8_t)0xa5)\n#endif\n#ifndef JEMALLOC_FREE_JUNK\n#  define JEMALLOC_FREE_JUNK\t((uint8_t)0x5a)\n#endif\n\n/*\n * Wrap a cpp argument that contains commas such that it isn't broken up into\n * multiple arguments.\n */\n#define JEMALLOC_ARG_CONCAT(...) __VA_ARGS__\n\n/* cpp macro definition stringification. */\n#define STRINGIFY_HELPER(x) #x\n#define STRINGIFY(x) STRINGIFY_HELPER(x)\n\n/*\n * Silence compiler warnings due to uninitialized values.  This is used\n * wherever the compiler fails to recognize that the variable is never used\n * uninitialized.\n */\n#define JEMALLOC_CC_SILENCE_INIT(v) = v\n\n#ifdef __GNUC__\n#  define likely(x)   __builtin_expect(!!(x), 1)\n#  define unlikely(x) __builtin_expect(!!(x), 0)\n#else\n#  define likely(x)   !!(x)\n#  define unlikely(x) !!(x)\n#endif\n\n#if !defined(JEMALLOC_INTERNAL_UNREACHABLE)\n#  error JEMALLOC_INTERNAL_UNREACHABLE should have been defined by configure\n#endif\n\n#define unreachable() JEMALLOC_INTERNAL_UNREACHABLE()\n\n/* Set error code. */\nUTIL_INLINE void\nset_errno(int errnum) {\n#ifdef _WIN32\n\tSetLastError(errnum);\n#else\n\terrno = errnum;\n#endif\n}\n\n/* Get last error code. */\nUTIL_INLINE int\nget_errno(void) {\n#ifdef _WIN32\n\treturn GetLastError();\n#else\n\treturn errno;\n#endif\n}\n\n#undef UTIL_INLINE\n\n#endif /* JEMALLOC_INTERNAL_UTIL_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/internal/witness.h",
    "content": "#ifndef JEMALLOC_INTERNAL_WITNESS_H\n#define JEMALLOC_INTERNAL_WITNESS_H\n\n#include \"jemalloc/internal/ql.h\"\n\n/******************************************************************************/\n/* LOCK RANKS */\n/******************************************************************************/\n\n/*\n * Witnesses with rank WITNESS_RANK_OMIT are completely ignored by the witness\n * machinery.\n */\n\n#define WITNESS_RANK_OMIT\t\t0U\n\n#define WITNESS_RANK_MIN\t\t1U\n\n#define WITNESS_RANK_INIT\t\t1U\n#define WITNESS_RANK_CTL\t\t1U\n#define WITNESS_RANK_TCACHES\t\t2U\n#define WITNESS_RANK_ARENAS\t\t3U\n\n#define WITNESS_RANK_BACKGROUND_THREAD_GLOBAL\t4U\n\n#define WITNESS_RANK_PROF_DUMP\t\t5U\n#define WITNESS_RANK_PROF_BT2GCTX\t6U\n#define WITNESS_RANK_PROF_TDATAS\t7U\n#define WITNESS_RANK_PROF_TDATA\t\t8U\n#define WITNESS_RANK_PROF_LOG\t\t9U\n#define WITNESS_RANK_PROF_GCTX\t\t10U\n#define WITNESS_RANK_BACKGROUND_THREAD\t11U\n\n/*\n * Used as an argument to witness_assert_depth_to_rank() in order to validate\n * depth excluding non-core locks with lower ranks.  Since the rank argument to\n * witness_assert_depth_to_rank() is inclusive rather than exclusive, this\n * definition can have the same value as the minimally ranked core lock.\n */\n#define WITNESS_RANK_CORE\t\t12U\n\n#define WITNESS_RANK_DECAY\t\t12U\n#define WITNESS_RANK_TCACHE_QL\t\t13U\n#define WITNESS_RANK_EXTENT_GROW\t14U\n#define WITNESS_RANK_EXTENTS\t\t15U\n#define WITNESS_RANK_EXTENT_AVAIL\t16U\n\n#define WITNESS_RANK_EXTENT_POOL\t17U\n#define WITNESS_RANK_RTREE\t\t18U\n#define WITNESS_RANK_BASE\t\t19U\n#define WITNESS_RANK_ARENA_LARGE\t20U\n#define WITNESS_RANK_HOOK\t\t21U\n\n#define WITNESS_RANK_LEAF\t\t0xffffffffU\n#define WITNESS_RANK_BIN\t\tWITNESS_RANK_LEAF\n#define WITNESS_RANK_ARENA_STATS\tWITNESS_RANK_LEAF\n#define WITNESS_RANK_DSS\t\tWITNESS_RANK_LEAF\n#define WITNESS_RANK_PROF_ACTIVE\tWITNESS_RANK_LEAF\n#define WITNESS_RANK_PROF_ACCUM\t\tWITNESS_RANK_LEAF\n#define WITNESS_RANK_PROF_DUMP_SEQ\tWITNESS_RANK_LEAF\n#define WITNESS_RANK_PROF_GDUMP\t\tWITNESS_RANK_LEAF\n#define WITNESS_RANK_PROF_NEXT_THR_UID\tWITNESS_RANK_LEAF\n#define WITNESS_RANK_PROF_THREAD_ACTIVE_INIT\tWITNESS_RANK_LEAF\n\n/******************************************************************************/\n/* PER-WITNESS DATA */\n/******************************************************************************/\n#if defined(JEMALLOC_DEBUG)\n#  define WITNESS_INITIALIZER(name, rank) {name, rank, NULL, NULL, {NULL, NULL}}\n#else\n#  define WITNESS_INITIALIZER(name, rank)\n#endif\n\ntypedef struct witness_s witness_t;\ntypedef unsigned witness_rank_t;\ntypedef ql_head(witness_t) witness_list_t;\ntypedef int witness_comp_t (const witness_t *, void *, const witness_t *,\n    void *);\n\nstruct witness_s {\n\t/* Name, used for printing lock order reversal messages. */\n\tconst char\t\t*name;\n\n\t/*\n\t * Witness rank, where 0 is lowest and UINT_MAX is highest.  Witnesses\n\t * must be acquired in order of increasing rank.\n\t */\n\twitness_rank_t\t\trank;\n\n\t/*\n\t * If two witnesses are of equal rank and they have the samp comp\n\t * function pointer, it is called as a last attempt to differentiate\n\t * between witnesses of equal rank.\n\t */\n\twitness_comp_t\t\t*comp;\n\n\t/* Opaque data, passed to comp(). */\n\tvoid\t\t\t*opaque;\n\n\t/* Linkage for thread's currently owned locks. */\n\tql_elm(witness_t)\tlink;\n};\n\n/******************************************************************************/\n/* PER-THREAD DATA */\n/******************************************************************************/\ntypedef struct witness_tsd_s witness_tsd_t;\nstruct witness_tsd_s {\n\twitness_list_t witnesses;\n\tbool forking;\n};\n\n#define WITNESS_TSD_INITIALIZER { ql_head_initializer(witnesses), false }\n#define WITNESS_TSDN_NULL ((witness_tsdn_t *)0)\n\n/******************************************************************************/\n/* (PER-THREAD) NULLABILITY HELPERS */\n/******************************************************************************/\ntypedef struct witness_tsdn_s witness_tsdn_t;\nstruct witness_tsdn_s {\n\twitness_tsd_t witness_tsd;\n};\n\nJEMALLOC_ALWAYS_INLINE witness_tsdn_t *\nwitness_tsd_tsdn(witness_tsd_t *witness_tsd) {\n\treturn (witness_tsdn_t *)witness_tsd;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\nwitness_tsdn_null(witness_tsdn_t *witness_tsdn) {\n\treturn witness_tsdn == NULL;\n}\n\nJEMALLOC_ALWAYS_INLINE witness_tsd_t *\nwitness_tsdn_tsd(witness_tsdn_t *witness_tsdn) {\n\tassert(!witness_tsdn_null(witness_tsdn));\n\treturn &witness_tsdn->witness_tsd;\n}\n\n/******************************************************************************/\n/* API */\n/******************************************************************************/\nvoid witness_init(witness_t *witness, const char *name, witness_rank_t rank,\n    witness_comp_t *comp, void *opaque);\n\ntypedef void (witness_lock_error_t)(const witness_list_t *, const witness_t *);\nextern witness_lock_error_t *JET_MUTABLE witness_lock_error;\n\ntypedef void (witness_owner_error_t)(const witness_t *);\nextern witness_owner_error_t *JET_MUTABLE witness_owner_error;\n\ntypedef void (witness_not_owner_error_t)(const witness_t *);\nextern witness_not_owner_error_t *JET_MUTABLE witness_not_owner_error;\n\ntypedef void (witness_depth_error_t)(const witness_list_t *,\n    witness_rank_t rank_inclusive, unsigned depth);\nextern witness_depth_error_t *JET_MUTABLE witness_depth_error;\n\nvoid witnesses_cleanup(witness_tsd_t *witness_tsd);\nvoid witness_prefork(witness_tsd_t *witness_tsd);\nvoid witness_postfork_parent(witness_tsd_t *witness_tsd);\nvoid witness_postfork_child(witness_tsd_t *witness_tsd);\n\n/* Helper, not intended for direct use. */\nstatic inline bool\nwitness_owner(witness_tsd_t *witness_tsd, const witness_t *witness) {\n\twitness_list_t *witnesses;\n\twitness_t *w;\n\n\tcassert(config_debug);\n\n\twitnesses = &witness_tsd->witnesses;\n\tql_foreach(w, witnesses, link) {\n\t\tif (w == witness) {\n\t\t\treturn true;\n\t\t}\n\t}\n\n\treturn false;\n}\n\nstatic inline void\nwitness_assert_owner(witness_tsdn_t *witness_tsdn, const witness_t *witness) {\n\twitness_tsd_t *witness_tsd;\n\n\tif (!config_debug) {\n\t\treturn;\n\t}\n\n\tif (witness_tsdn_null(witness_tsdn)) {\n\t\treturn;\n\t}\n\twitness_tsd = witness_tsdn_tsd(witness_tsdn);\n\tif (witness->rank == WITNESS_RANK_OMIT) {\n\t\treturn;\n\t}\n\n\tif (witness_owner(witness_tsd, witness)) {\n\t\treturn;\n\t}\n\twitness_owner_error(witness);\n}\n\nstatic inline void\nwitness_assert_not_owner(witness_tsdn_t *witness_tsdn,\n    const witness_t *witness) {\n\twitness_tsd_t *witness_tsd;\n\twitness_list_t *witnesses;\n\twitness_t *w;\n\n\tif (!config_debug) {\n\t\treturn;\n\t}\n\n\tif (witness_tsdn_null(witness_tsdn)) {\n\t\treturn;\n\t}\n\twitness_tsd = witness_tsdn_tsd(witness_tsdn);\n\tif (witness->rank == WITNESS_RANK_OMIT) {\n\t\treturn;\n\t}\n\n\twitnesses = &witness_tsd->witnesses;\n\tql_foreach(w, witnesses, link) {\n\t\tif (w == witness) {\n\t\t\twitness_not_owner_error(witness);\n\t\t}\n\t}\n}\n\nstatic inline void\nwitness_assert_depth_to_rank(witness_tsdn_t *witness_tsdn,\n    witness_rank_t rank_inclusive, unsigned depth) {\n\twitness_tsd_t *witness_tsd;\n\tunsigned d;\n\twitness_list_t *witnesses;\n\twitness_t *w;\n\n\tif (!config_debug) {\n\t\treturn;\n\t}\n\n\tif (witness_tsdn_null(witness_tsdn)) {\n\t\treturn;\n\t}\n\twitness_tsd = witness_tsdn_tsd(witness_tsdn);\n\n\td = 0;\n\twitnesses = &witness_tsd->witnesses;\n\tw = ql_last(witnesses, link);\n\tif (w != NULL) {\n\t\tql_reverse_foreach(w, witnesses, link) {\n\t\t\tif (w->rank < rank_inclusive) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\td++;\n\t\t}\n\t}\n\tif (d != depth) {\n\t\twitness_depth_error(witnesses, rank_inclusive, depth);\n\t}\n}\n\nstatic inline void\nwitness_assert_depth(witness_tsdn_t *witness_tsdn, unsigned depth) {\n\twitness_assert_depth_to_rank(witness_tsdn, WITNESS_RANK_MIN, depth);\n}\n\nstatic inline void\nwitness_assert_lockless(witness_tsdn_t *witness_tsdn) {\n\twitness_assert_depth(witness_tsdn, 0);\n}\n\nstatic inline void\nwitness_lock(witness_tsdn_t *witness_tsdn, witness_t *witness) {\n\twitness_tsd_t *witness_tsd;\n\twitness_list_t *witnesses;\n\twitness_t *w;\n\n\tif (!config_debug) {\n\t\treturn;\n\t}\n\n\tif (witness_tsdn_null(witness_tsdn)) {\n\t\treturn;\n\t}\n\twitness_tsd = witness_tsdn_tsd(witness_tsdn);\n\tif (witness->rank == WITNESS_RANK_OMIT) {\n\t\treturn;\n\t}\n\n\twitness_assert_not_owner(witness_tsdn, witness);\n\n\twitnesses = &witness_tsd->witnesses;\n\tw = ql_last(witnesses, link);\n\tif (w == NULL) {\n\t\t/* No other locks; do nothing. */\n\t} else if (witness_tsd->forking && w->rank <= witness->rank) {\n\t\t/* Forking, and relaxed ranking satisfied. */\n\t} else if (w->rank > witness->rank) {\n\t\t/* Not forking, rank order reversal. */\n\t\twitness_lock_error(witnesses, witness);\n\t} else if (w->rank == witness->rank && (w->comp == NULL || w->comp !=\n\t    witness->comp || w->comp(w, w->opaque, witness, witness->opaque) >\n\t    0)) {\n\t\t/*\n\t\t * Missing/incompatible comparison function, or comparison\n\t\t * function indicates rank order reversal.\n\t\t */\n\t\twitness_lock_error(witnesses, witness);\n\t}\n\n\tql_elm_new(witness, link);\n\tql_tail_insert(witnesses, witness, link);\n}\n\nstatic inline void\nwitness_unlock(witness_tsdn_t *witness_tsdn, witness_t *witness) {\n\twitness_tsd_t *witness_tsd;\n\twitness_list_t *witnesses;\n\n\tif (!config_debug) {\n\t\treturn;\n\t}\n\n\tif (witness_tsdn_null(witness_tsdn)) {\n\t\treturn;\n\t}\n\twitness_tsd = witness_tsdn_tsd(witness_tsdn);\n\tif (witness->rank == WITNESS_RANK_OMIT) {\n\t\treturn;\n\t}\n\n\t/*\n\t * Check whether owner before removal, rather than relying on\n\t * witness_assert_owner() to abort, so that unit tests can test this\n\t * function's failure mode without causing undefined behavior.\n\t */\n\tif (witness_owner(witness_tsd, witness)) {\n\t\twitnesses = &witness_tsd->witnesses;\n\t\tql_remove(witnesses, witness, link);\n\t} else {\n\t\twitness_assert_owner(witness_tsdn, witness);\n\t}\n}\n\n#endif /* JEMALLOC_INTERNAL_WITNESS_H */\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/jemalloc.sh",
    "content": "#!/bin/sh\n\nobjroot=$1\n\ncat <<EOF\n#ifndef JEMALLOC_H_\n#define JEMALLOC_H_\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\nEOF\n\nfor hdr in jemalloc_defs.h jemalloc_rename.h jemalloc_macros.h \\\n           jemalloc_protos.h jemalloc_typedefs.h jemalloc_mangle.h ; do\n  cat \"${objroot}include/jemalloc/${hdr}\" \\\n      | grep -v 'Generated from .* by configure\\.' \\\n      | sed -e 's/ $//g'\n  echo\ndone\n\ncat <<EOF\n#ifdef __cplusplus\n}\n#endif\n#endif /* JEMALLOC_H_ */\nEOF\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/jemalloc_defs.h.in",
    "content": "/* Defined if __attribute__((...)) syntax is supported. */\n#undef JEMALLOC_HAVE_ATTR\n\n/* Defined if alloc_size attribute is supported. */\n#undef JEMALLOC_HAVE_ATTR_ALLOC_SIZE\n\n/* Defined if format_arg(...) attribute is supported. */\n#undef JEMALLOC_HAVE_ATTR_FORMAT_ARG\n\n/* Defined if format(gnu_printf, ...) attribute is supported. */\n#undef JEMALLOC_HAVE_ATTR_FORMAT_GNU_PRINTF\n\n/* Defined if format(printf, ...) attribute is supported. */\n#undef JEMALLOC_HAVE_ATTR_FORMAT_PRINTF\n\n/*\n * Define overrides for non-standard allocator-related functions if they are\n * present on the system.\n */\n#undef JEMALLOC_OVERRIDE_MEMALIGN\n#undef JEMALLOC_OVERRIDE_VALLOC\n\n/*\n * At least Linux omits the \"const\" in:\n *\n *   size_t malloc_usable_size(const void *ptr);\n *\n * Match the operating system's prototype.\n */\n#undef JEMALLOC_USABLE_SIZE_CONST\n\n/*\n * If defined, specify throw() for the public function prototypes when compiling\n * with C++.  The only justification for this is to match the prototypes that\n * glibc defines.\n */\n#undef JEMALLOC_USE_CXX_THROW\n\n#ifdef _MSC_VER\n#  ifdef _WIN64\n#    define LG_SIZEOF_PTR_WIN 3\n#  else\n#    define LG_SIZEOF_PTR_WIN 2\n#  endif\n#endif\n\n/* sizeof(void *) == 2^LG_SIZEOF_PTR. */\n#undef LG_SIZEOF_PTR\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/jemalloc_macros.h.in",
    "content": "#include <stdlib.h>\n#include <stdbool.h>\n#include <stdint.h>\n#include <limits.h>\n#include <strings.h>\n\n#define JEMALLOC_VERSION \"@jemalloc_version@\"\n#define JEMALLOC_VERSION_MAJOR @jemalloc_version_major@\n#define JEMALLOC_VERSION_MINOR @jemalloc_version_minor@\n#define JEMALLOC_VERSION_BUGFIX @jemalloc_version_bugfix@\n#define JEMALLOC_VERSION_NREV @jemalloc_version_nrev@\n#define JEMALLOC_VERSION_GID \"@jemalloc_version_gid@\"\n#define JEMALLOC_VERSION_GID_IDENT @jemalloc_version_gid@\n\n#define MALLOCX_LG_ALIGN(la)\t((int)(la))\n#if LG_SIZEOF_PTR == 2\n#  define MALLOCX_ALIGN(a)\t((int)(ffs((int)(a))-1))\n#else\n#  define MALLOCX_ALIGN(a)\t\t\t\t\t\t\\\n     ((int)(((size_t)(a) < (size_t)INT_MAX) ? ffs((int)(a))-1 :\t\\\n     ffs((int)(((size_t)(a))>>32))+31))\n#endif\n#define MALLOCX_ZERO\t((int)0x40)\n/*\n * Bias tcache index bits so that 0 encodes \"automatic tcache management\", and 1\n * encodes MALLOCX_TCACHE_NONE.\n */\n#define MALLOCX_TCACHE(tc)\t((int)(((tc)+2) << 8))\n#define MALLOCX_TCACHE_NONE\tMALLOCX_TCACHE(-1)\n/*\n * Bias arena index bits so that 0 encodes \"use an automatically chosen arena\".\n */\n#define MALLOCX_ARENA(a)\t((((int)(a))+1) << 20)\n\n/*\n * Use as arena index in \"arena.<i>.{purge,decay,dss}\" and\n * \"stats.arenas.<i>.*\" mallctl interfaces to select all arenas.  This\n * definition is intentionally specified in raw decimal format to support\n * cpp-based string concatenation, e.g.\n *\n *   #define STRINGIFY_HELPER(x) #x\n *   #define STRINGIFY(x) STRINGIFY_HELPER(x)\n *\n *   mallctl(\"arena.\" STRINGIFY(MALLCTL_ARENAS_ALL) \".purge\", NULL, NULL, NULL,\n *       0);\n */\n#define MALLCTL_ARENAS_ALL\t4096\n/*\n * Use as arena index in \"stats.arenas.<i>.*\" mallctl interfaces to select\n * destroyed arenas.\n */\n#define MALLCTL_ARENAS_DESTROYED\t4097\n\n#if defined(__cplusplus) && defined(JEMALLOC_USE_CXX_THROW)\n#  define JEMALLOC_CXX_THROW throw()\n#else\n#  define JEMALLOC_CXX_THROW\n#endif\n\n#if defined(_MSC_VER)\n#  define JEMALLOC_ATTR(s)\n#  define JEMALLOC_ALIGNED(s) __declspec(align(s))\n#  define JEMALLOC_ALLOC_SIZE(s)\n#  define JEMALLOC_ALLOC_SIZE2(s1, s2)\n#  ifndef JEMALLOC_EXPORT\n#    ifdef DLLEXPORT\n#      define JEMALLOC_EXPORT __declspec(dllexport)\n#    else\n#      define JEMALLOC_EXPORT __declspec(dllimport)\n#    endif\n#  endif\n#  define JEMALLOC_FORMAT_ARG(i)\n#  define JEMALLOC_FORMAT_PRINTF(s, i)\n#  define JEMALLOC_NOINLINE __declspec(noinline)\n#  ifdef __cplusplus\n#    define JEMALLOC_NOTHROW __declspec(nothrow)\n#  else\n#    define JEMALLOC_NOTHROW\n#  endif\n#  define JEMALLOC_SECTION(s) __declspec(allocate(s))\n#  define JEMALLOC_RESTRICT_RETURN __declspec(restrict)\n#  if _MSC_VER >= 1900 && !defined(__EDG__)\n#    define JEMALLOC_ALLOCATOR __declspec(allocator)\n#  else\n#    define JEMALLOC_ALLOCATOR\n#  endif\n#elif defined(JEMALLOC_HAVE_ATTR)\n#  define JEMALLOC_ATTR(s) __attribute__((s))\n#  define JEMALLOC_ALIGNED(s) JEMALLOC_ATTR(aligned(s))\n#  ifdef JEMALLOC_HAVE_ATTR_ALLOC_SIZE\n#    define JEMALLOC_ALLOC_SIZE(s) JEMALLOC_ATTR(alloc_size(s))\n#    define JEMALLOC_ALLOC_SIZE2(s1, s2) JEMALLOC_ATTR(alloc_size(s1, s2))\n#  else\n#    define JEMALLOC_ALLOC_SIZE(s)\n#    define JEMALLOC_ALLOC_SIZE2(s1, s2)\n#  endif\n#  ifndef JEMALLOC_EXPORT\n#    define JEMALLOC_EXPORT JEMALLOC_ATTR(visibility(\"default\"))\n#  endif\n#  ifdef JEMALLOC_HAVE_ATTR_FORMAT_ARG\n#    define JEMALLOC_FORMAT_ARG(i) JEMALLOC_ATTR(__format_arg__(3))\n#  else\n#    define JEMALLOC_FORMAT_ARG(i)\n#  endif\n#  ifdef JEMALLOC_HAVE_ATTR_FORMAT_GNU_PRINTF\n#    define JEMALLOC_FORMAT_PRINTF(s, i) JEMALLOC_ATTR(format(gnu_printf, s, i))\n#  elif defined(JEMALLOC_HAVE_ATTR_FORMAT_PRINTF)\n#    define JEMALLOC_FORMAT_PRINTF(s, i) JEMALLOC_ATTR(format(printf, s, i))\n#  else\n#    define JEMALLOC_FORMAT_PRINTF(s, i)\n#  endif\n#  define JEMALLOC_NOINLINE JEMALLOC_ATTR(noinline)\n#  define JEMALLOC_NOTHROW JEMALLOC_ATTR(nothrow)\n#  define JEMALLOC_SECTION(s) JEMALLOC_ATTR(section(s))\n#  define JEMALLOC_RESTRICT_RETURN\n#  define JEMALLOC_ALLOCATOR\n#else\n#  define JEMALLOC_ATTR(s)\n#  define JEMALLOC_ALIGNED(s)\n#  define JEMALLOC_ALLOC_SIZE(s)\n#  define JEMALLOC_ALLOC_SIZE2(s1, s2)\n#  define JEMALLOC_EXPORT\n#  define JEMALLOC_FORMAT_PRINTF(s, i)\n#  define JEMALLOC_NOINLINE\n#  define JEMALLOC_NOTHROW\n#  define JEMALLOC_SECTION(s)\n#  define JEMALLOC_RESTRICT_RETURN\n#  define JEMALLOC_ALLOCATOR\n#endif\n\n/* This version of Jemalloc, modified for Redis, has the je_get_defrag_hint()\n * function. */\n#define JEMALLOC_FRAG_HINT"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/jemalloc_mangle.sh",
    "content": "#!/bin/sh -eu\n\npublic_symbols_txt=$1\nsymbol_prefix=$2\n\ncat <<EOF\n/*\n * By default application code must explicitly refer to mangled symbol names,\n * so that it is possible to use jemalloc in conjunction with another allocator\n * in the same application.  Define JEMALLOC_MANGLE in order to cause automatic\n * name mangling that matches the API prefixing that happened as a result of\n * --with-mangling and/or --with-jemalloc-prefix configuration settings.\n */\n#ifdef JEMALLOC_MANGLE\n#  ifndef JEMALLOC_NO_DEMANGLE\n#    define JEMALLOC_NO_DEMANGLE\n#  endif\nEOF\n\nfor nm in `cat ${public_symbols_txt}` ; do\n  n=`echo ${nm} |tr ':' ' ' |awk '{print $1}'`\n  echo \"#  define ${n} ${symbol_prefix}${n}\"\ndone\n\ncat <<EOF\n#endif\n\n/*\n * The ${symbol_prefix}* macros can be used as stable alternative names for the\n * public jemalloc API if JEMALLOC_NO_DEMANGLE is defined.  This is primarily\n * meant for use in jemalloc itself, but it can be used by application code to\n * provide isolation from the name mangling specified via --with-mangling\n * and/or --with-jemalloc-prefix.\n */\n#ifndef JEMALLOC_NO_DEMANGLE\nEOF\n\nfor nm in `cat ${public_symbols_txt}` ; do\n  n=`echo ${nm} |tr ':' ' ' |awk '{print $1}'`\n  echo \"#  undef ${symbol_prefix}${n}\"\ndone\n\ncat <<EOF\n#endif\nEOF\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/jemalloc_protos.h.in",
    "content": "/*\n * The @je_@ prefix on the following public symbol declarations is an artifact\n * of namespace management, and should be omitted in application code unless\n * JEMALLOC_NO_DEMANGLE is defined (see jemalloc_mangle@install_suffix@.h).\n */\nextern JEMALLOC_EXPORT const char\t*@je_@malloc_conf;\nextern JEMALLOC_EXPORT void\t\t(*@je_@malloc_message)(void *cbopaque,\n    const char *s);\n\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\n    void JEMALLOC_NOTHROW\t*@je_@malloc(size_t size)\n    JEMALLOC_CXX_THROW JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE(1);\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\n    void JEMALLOC_NOTHROW\t*@je_@calloc(size_t num, size_t size)\n    JEMALLOC_CXX_THROW JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE2(1, 2);\nJEMALLOC_EXPORT int JEMALLOC_NOTHROW\t@je_@posix_memalign(void **memptr,\n    size_t alignment, size_t size) JEMALLOC_CXX_THROW JEMALLOC_ATTR(nonnull(1));\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\n    void JEMALLOC_NOTHROW\t*@je_@aligned_alloc(size_t alignment,\n    size_t size) JEMALLOC_CXX_THROW JEMALLOC_ATTR(malloc)\n    JEMALLOC_ALLOC_SIZE(2);\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\n    void JEMALLOC_NOTHROW\t*@je_@realloc(void *ptr, size_t size)\n    JEMALLOC_CXX_THROW JEMALLOC_ALLOC_SIZE(2);\nJEMALLOC_EXPORT void JEMALLOC_NOTHROW\t@je_@free(void *ptr)\n    JEMALLOC_CXX_THROW;\n\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\n    void JEMALLOC_NOTHROW\t*@je_@mallocx(size_t size, int flags)\n    JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE(1);\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\n    void JEMALLOC_NOTHROW\t*@je_@rallocx(void *ptr, size_t size,\n    int flags) JEMALLOC_ALLOC_SIZE(2);\nJEMALLOC_EXPORT size_t JEMALLOC_NOTHROW\t@je_@xallocx(void *ptr, size_t size,\n    size_t extra, int flags);\nJEMALLOC_EXPORT size_t JEMALLOC_NOTHROW\t@je_@sallocx(const void *ptr,\n    int flags) JEMALLOC_ATTR(pure);\nJEMALLOC_EXPORT void JEMALLOC_NOTHROW\t@je_@dallocx(void *ptr, int flags);\nJEMALLOC_EXPORT void JEMALLOC_NOTHROW\t@je_@sdallocx(void *ptr, size_t size,\n    int flags);\nJEMALLOC_EXPORT size_t JEMALLOC_NOTHROW\t@je_@nallocx(size_t size, int flags)\n    JEMALLOC_ATTR(pure);\n\nJEMALLOC_EXPORT int JEMALLOC_NOTHROW\t@je_@mallctl(const char *name,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen);\nJEMALLOC_EXPORT int JEMALLOC_NOTHROW\t@je_@mallctlnametomib(const char *name,\n    size_t *mibp, size_t *miblenp);\nJEMALLOC_EXPORT int JEMALLOC_NOTHROW\t@je_@mallctlbymib(const size_t *mib,\n    size_t miblen, void *oldp, size_t *oldlenp, void *newp, size_t newlen);\nJEMALLOC_EXPORT void JEMALLOC_NOTHROW\t@je_@malloc_stats_print(\n    void (*write_cb)(void *, const char *), void *@je_@cbopaque,\n    const char *opts);\nJEMALLOC_EXPORT size_t JEMALLOC_NOTHROW\t@je_@malloc_usable_size(\n    JEMALLOC_USABLE_SIZE_CONST void *ptr) JEMALLOC_CXX_THROW;\n\n#ifdef JEMALLOC_OVERRIDE_MEMALIGN\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\n    void JEMALLOC_NOTHROW\t*@je_@memalign(size_t alignment, size_t size)\n    JEMALLOC_CXX_THROW JEMALLOC_ATTR(malloc);\n#endif\n\n#ifdef JEMALLOC_OVERRIDE_VALLOC\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\n    void JEMALLOC_NOTHROW\t*@je_@valloc(size_t size) JEMALLOC_CXX_THROW\n    JEMALLOC_ATTR(malloc);\n#endif\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/jemalloc_rename.sh",
    "content": "#!/bin/sh\n\npublic_symbols_txt=$1\n\ncat <<EOF\n/*\n * Name mangling for public symbols is controlled by --with-mangling and\n * --with-jemalloc-prefix.  With default settings the je_ prefix is stripped by\n * these macro definitions.\n */\n#ifndef JEMALLOC_NO_RENAME\nEOF\n\nfor nm in `cat ${public_symbols_txt}` ; do\n  n=`echo ${nm} |tr ':' ' ' |awk '{print $1}'`\n  m=`echo ${nm} |tr ':' ' ' |awk '{print $2}'`\n  echo \"#  define je_${n} ${m}\"\ndone\n\ncat <<EOF\n#endif\nEOF\n"
  },
  {
    "path": "deps/jemalloc/include/jemalloc/jemalloc_typedefs.h.in",
    "content": "typedef struct extent_hooks_s extent_hooks_t;\n\n/*\n * void *\n * extent_alloc(extent_hooks_t *extent_hooks, void *new_addr, size_t size,\n *     size_t alignment, bool *zero, bool *commit, unsigned arena_ind);\n */\ntypedef void *(extent_alloc_t)(extent_hooks_t *, void *, size_t, size_t, bool *,\n    bool *, unsigned);\n\n/*\n * bool\n * extent_dalloc(extent_hooks_t *extent_hooks, void *addr, size_t size,\n *     bool committed, unsigned arena_ind);\n */\ntypedef bool (extent_dalloc_t)(extent_hooks_t *, void *, size_t, bool,\n    unsigned);\n\n/*\n * void\n * extent_destroy(extent_hooks_t *extent_hooks, void *addr, size_t size,\n *     bool committed, unsigned arena_ind);\n */\ntypedef void (extent_destroy_t)(extent_hooks_t *, void *, size_t, bool,\n    unsigned);\n\n/*\n * bool\n * extent_commit(extent_hooks_t *extent_hooks, void *addr, size_t size,\n *     size_t offset, size_t length, unsigned arena_ind);\n */\ntypedef bool (extent_commit_t)(extent_hooks_t *, void *, size_t, size_t, size_t,\n    unsigned);\n\n/*\n * bool\n * extent_decommit(extent_hooks_t *extent_hooks, void *addr, size_t size,\n *     size_t offset, size_t length, unsigned arena_ind);\n */\ntypedef bool (extent_decommit_t)(extent_hooks_t *, void *, size_t, size_t,\n    size_t, unsigned);\n\n/*\n * bool\n * extent_purge(extent_hooks_t *extent_hooks, void *addr, size_t size,\n *     size_t offset, size_t length, unsigned arena_ind);\n */\ntypedef bool (extent_purge_t)(extent_hooks_t *, void *, size_t, size_t, size_t,\n    unsigned);\n\n/*\n * bool\n * extent_split(extent_hooks_t *extent_hooks, void *addr, size_t size,\n *     size_t size_a, size_t size_b, bool committed, unsigned arena_ind);\n */\ntypedef bool (extent_split_t)(extent_hooks_t *, void *, size_t, size_t, size_t,\n    bool, unsigned);\n\n/*\n * bool\n * extent_merge(extent_hooks_t *extent_hooks, void *addr_a, size_t size_a,\n *     void *addr_b, size_t size_b, bool committed, unsigned arena_ind);\n */\ntypedef bool (extent_merge_t)(extent_hooks_t *, void *, size_t, void *, size_t,\n    bool, unsigned);\n\nstruct extent_hooks_s {\n\textent_alloc_t\t\t*alloc;\n\textent_dalloc_t\t\t*dalloc;\n\textent_destroy_t\t*destroy;\n\textent_commit_t\t\t*commit;\n\textent_decommit_t\t*decommit;\n\textent_purge_t\t\t*purge_lazy;\n\textent_purge_t\t\t*purge_forced;\n\textent_split_t\t\t*split;\n\textent_merge_t\t\t*merge;\n};\n"
  },
  {
    "path": "deps/jemalloc/include/msvc_compat/C99/stdbool.h",
    "content": "#ifndef stdbool_h\n#define stdbool_h\n\n#include <wtypes.h>\n\n/* MSVC doesn't define _Bool or bool in C, but does have BOOL */\n/* Note this doesn't pass autoconf's test because (bool) 0.5 != true */\n/* Clang-cl uses MSVC headers, so needs msvc_compat, but has _Bool as\n * a built-in type. */\n#ifndef __clang__\ntypedef BOOL _Bool;\n#endif\n\n#define bool _Bool\n#define true 1\n#define false 0\n\n#define __bool_true_false_are_defined 1\n\n#endif /* stdbool_h */\n"
  },
  {
    "path": "deps/jemalloc/include/msvc_compat/C99/stdint.h",
    "content": "// ISO C9x  compliant stdint.h for Microsoft Visual Studio\n// Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124 \n// \n//  Copyright (c) 2006-2008 Alexander Chemeris\n// \n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are met:\n// \n//   1. Redistributions of source code must retain the above copyright notice,\n//      this list of conditions and the following disclaimer.\n// \n//   2. Redistributions in binary form must reproduce the above copyright\n//      notice, this list of conditions and the following disclaimer in the\n//      documentation and/or other materials provided with the distribution.\n// \n//   3. The name of the author may be used to endorse or promote products\n//      derived from this software without specific prior written permission.\n// \n// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED\n// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO\n// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\n// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;\n// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, \n// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR\n// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n// \n///////////////////////////////////////////////////////////////////////////////\n\n#ifndef _MSC_VER // [\n#error \"Use this header only with Microsoft Visual C++ compilers!\"\n#endif // _MSC_VER ]\n\n#ifndef _MSC_STDINT_H_ // [\n#define _MSC_STDINT_H_\n\n#if _MSC_VER > 1000\n#pragma once\n#endif\n\n#include <limits.h>\n\n// For Visual Studio 6 in C++ mode and for many Visual Studio versions when\n// compiling for ARM we should wrap <wchar.h> include with 'extern \"C++\" {}'\n// or compiler give many errors like this:\n//   error C2733: second C linkage of overloaded function 'wmemchr' not allowed\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n#  include <wchar.h>\n#ifdef __cplusplus\n}\n#endif\n\n// Define _W64 macros to mark types changing their size, like intptr_t.\n#ifndef _W64\n#  if !defined(__midl) && (defined(_X86_) || defined(_M_IX86)) && _MSC_VER >= 1300\n#     define _W64 __w64\n#  else\n#     define _W64\n#  endif\n#endif\n\n\n// 7.18.1 Integer types\n\n// 7.18.1.1 Exact-width integer types\n\n// Visual Studio 6 and Embedded Visual C++ 4 doesn't\n// realize that, e.g. char has the same size as __int8\n// so we give up on __intX for them.\n#if (_MSC_VER < 1300)\n   typedef signed char       int8_t;\n   typedef signed short      int16_t;\n   typedef signed int        int32_t;\n   typedef unsigned char     uint8_t;\n   typedef unsigned short    uint16_t;\n   typedef unsigned int      uint32_t;\n#else\n   typedef signed __int8     int8_t;\n   typedef signed __int16    int16_t;\n   typedef signed __int32    int32_t;\n   typedef unsigned __int8   uint8_t;\n   typedef unsigned __int16  uint16_t;\n   typedef unsigned __int32  uint32_t;\n#endif\ntypedef signed __int64       int64_t;\ntypedef unsigned __int64     uint64_t;\n\n\n// 7.18.1.2 Minimum-width integer types\ntypedef int8_t    int_least8_t;\ntypedef int16_t   int_least16_t;\ntypedef int32_t   int_least32_t;\ntypedef int64_t   int_least64_t;\ntypedef uint8_t   uint_least8_t;\ntypedef uint16_t  uint_least16_t;\ntypedef uint32_t  uint_least32_t;\ntypedef uint64_t  uint_least64_t;\n\n// 7.18.1.3 Fastest minimum-width integer types\ntypedef int8_t    int_fast8_t;\ntypedef int16_t   int_fast16_t;\ntypedef int32_t   int_fast32_t;\ntypedef int64_t   int_fast64_t;\ntypedef uint8_t   uint_fast8_t;\ntypedef uint16_t  uint_fast16_t;\ntypedef uint32_t  uint_fast32_t;\ntypedef uint64_t  uint_fast64_t;\n\n// 7.18.1.4 Integer types capable of holding object pointers\n#ifdef _WIN64 // [\n   typedef signed __int64    intptr_t;\n   typedef unsigned __int64  uintptr_t;\n#else // _WIN64 ][\n   typedef _W64 signed int   intptr_t;\n   typedef _W64 unsigned int uintptr_t;\n#endif // _WIN64 ]\n\n// 7.18.1.5 Greatest-width integer types\ntypedef int64_t   intmax_t;\ntypedef uint64_t  uintmax_t;\n\n\n// 7.18.2 Limits of specified-width integer types\n\n#if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS) // [   See footnote 220 at page 257 and footnote 221 at page 259\n\n// 7.18.2.1 Limits of exact-width integer types\n#define INT8_MIN     ((int8_t)_I8_MIN)\n#define INT8_MAX     _I8_MAX\n#define INT16_MIN    ((int16_t)_I16_MIN)\n#define INT16_MAX    _I16_MAX\n#define INT32_MIN    ((int32_t)_I32_MIN)\n#define INT32_MAX    _I32_MAX\n#define INT64_MIN    ((int64_t)_I64_MIN)\n#define INT64_MAX    _I64_MAX\n#define UINT8_MAX    _UI8_MAX\n#define UINT16_MAX   _UI16_MAX\n#define UINT32_MAX   _UI32_MAX\n#define UINT64_MAX   _UI64_MAX\n\n// 7.18.2.2 Limits of minimum-width integer types\n#define INT_LEAST8_MIN    INT8_MIN\n#define INT_LEAST8_MAX    INT8_MAX\n#define INT_LEAST16_MIN   INT16_MIN\n#define INT_LEAST16_MAX   INT16_MAX\n#define INT_LEAST32_MIN   INT32_MIN\n#define INT_LEAST32_MAX   INT32_MAX\n#define INT_LEAST64_MIN   INT64_MIN\n#define INT_LEAST64_MAX   INT64_MAX\n#define UINT_LEAST8_MAX   UINT8_MAX\n#define UINT_LEAST16_MAX  UINT16_MAX\n#define UINT_LEAST32_MAX  UINT32_MAX\n#define UINT_LEAST64_MAX  UINT64_MAX\n\n// 7.18.2.3 Limits of fastest minimum-width integer types\n#define INT_FAST8_MIN    INT8_MIN\n#define INT_FAST8_MAX    INT8_MAX\n#define INT_FAST16_MIN   INT16_MIN\n#define INT_FAST16_MAX   INT16_MAX\n#define INT_FAST32_MIN   INT32_MIN\n#define INT_FAST32_MAX   INT32_MAX\n#define INT_FAST64_MIN   INT64_MIN\n#define INT_FAST64_MAX   INT64_MAX\n#define UINT_FAST8_MAX   UINT8_MAX\n#define UINT_FAST16_MAX  UINT16_MAX\n#define UINT_FAST32_MAX  UINT32_MAX\n#define UINT_FAST64_MAX  UINT64_MAX\n\n// 7.18.2.4 Limits of integer types capable of holding object pointers\n#ifdef _WIN64 // [\n#  define INTPTR_MIN   INT64_MIN\n#  define INTPTR_MAX   INT64_MAX\n#  define UINTPTR_MAX  UINT64_MAX\n#else // _WIN64 ][\n#  define INTPTR_MIN   INT32_MIN\n#  define INTPTR_MAX   INT32_MAX\n#  define UINTPTR_MAX  UINT32_MAX\n#endif // _WIN64 ]\n\n// 7.18.2.5 Limits of greatest-width integer types\n#define INTMAX_MIN   INT64_MIN\n#define INTMAX_MAX   INT64_MAX\n#define UINTMAX_MAX  UINT64_MAX\n\n// 7.18.3 Limits of other integer types\n\n#ifdef _WIN64 // [\n#  define PTRDIFF_MIN  _I64_MIN\n#  define PTRDIFF_MAX  _I64_MAX\n#else  // _WIN64 ][\n#  define PTRDIFF_MIN  _I32_MIN\n#  define PTRDIFF_MAX  _I32_MAX\n#endif  // _WIN64 ]\n\n#define SIG_ATOMIC_MIN  INT_MIN\n#define SIG_ATOMIC_MAX  INT_MAX\n\n#ifndef SIZE_MAX // [\n#  ifdef _WIN64 // [\n#     define SIZE_MAX  _UI64_MAX\n#  else // _WIN64 ][\n#     define SIZE_MAX  _UI32_MAX\n#  endif // _WIN64 ]\n#endif // SIZE_MAX ]\n\n// WCHAR_MIN and WCHAR_MAX are also defined in <wchar.h>\n#ifndef WCHAR_MIN // [\n#  define WCHAR_MIN  0\n#endif  // WCHAR_MIN ]\n#ifndef WCHAR_MAX // [\n#  define WCHAR_MAX  _UI16_MAX\n#endif  // WCHAR_MAX ]\n\n#define WINT_MIN  0\n#define WINT_MAX  _UI16_MAX\n\n#endif // __STDC_LIMIT_MACROS ]\n\n\n// 7.18.4 Limits of other integer types\n\n#if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) // [   See footnote 224 at page 260\n\n// 7.18.4.1 Macros for minimum-width integer constants\n\n#define INT8_C(val)  val##i8\n#define INT16_C(val) val##i16\n#define INT32_C(val) val##i32\n#define INT64_C(val) val##i64\n\n#define UINT8_C(val)  val##ui8\n#define UINT16_C(val) val##ui16\n#define UINT32_C(val) val##ui32\n#define UINT64_C(val) val##ui64\n\n// 7.18.4.2 Macros for greatest-width integer constants\n#define INTMAX_C   INT64_C\n#define UINTMAX_C  UINT64_C\n\n#endif // __STDC_CONSTANT_MACROS ]\n\n\n#endif // _MSC_STDINT_H_ ]\n"
  },
  {
    "path": "deps/jemalloc/include/msvc_compat/strings.h",
    "content": "#ifndef strings_h\n#define strings_h\n\n/* MSVC doesn't define ffs/ffsl. This dummy strings.h header is provided\n * for both */\n#ifdef _MSC_VER\n#  include <intrin.h>\n#  pragma intrinsic(_BitScanForward)\nstatic __forceinline int ffsl(long x) {\n\tunsigned long i;\n\n\tif (_BitScanForward(&i, x)) {\n\t\treturn i + 1;\n\t}\n\treturn 0;\n}\n\nstatic __forceinline int ffs(int x) {\n\treturn ffsl(x);\n}\n\n#  ifdef  _M_X64\n#    pragma intrinsic(_BitScanForward64)\n#  endif\n\nstatic __forceinline int ffsll(unsigned __int64 x) {\n\tunsigned long i;\n#ifdef  _M_X64\n\tif (_BitScanForward64(&i, x)) {\n\t\treturn i + 1;\n\t}\n\treturn 0;\n#else\n// Fallback for 32-bit build where 64-bit version not available\n// assuming little endian\n\tunion {\n\t\tunsigned __int64 ll;\n\t\tunsigned   long l[2];\n\t} s;\n\n\ts.ll = x;\n\n\tif (_BitScanForward(&i, s.l[0])) {\n\t\treturn i + 1;\n\t} else if(_BitScanForward(&i, s.l[1])) {\n\t\treturn i + 33;\n\t}\n\treturn 0;\n#endif\n}\n\n#else\n#  define ffsll(x) __builtin_ffsll(x)\n#  define ffsl(x) __builtin_ffsl(x)\n#  define ffs(x) __builtin_ffs(x)\n#endif\n\n#endif /* strings_h */\n"
  },
  {
    "path": "deps/jemalloc/include/msvc_compat/windows_extra.h",
    "content": "#ifndef MSVC_COMPAT_WINDOWS_EXTRA_H\n#define MSVC_COMPAT_WINDOWS_EXTRA_H\n\n#include <errno.h>\n\n#endif /* MSVC_COMPAT_WINDOWS_EXTRA_H */\n"
  },
  {
    "path": "deps/jemalloc/jemalloc.pc.in",
    "content": "prefix=@prefix@\nexec_prefix=@exec_prefix@\nlibdir=@libdir@\nincludedir=@includedir@\ninstall_suffix=@install_suffix@\n\nName: jemalloc\nDescription: A general purpose malloc(3) implementation that emphasizes fragmentation avoidance and scalable concurrency support.\nURL: http://jemalloc.net/\nVersion: @jemalloc_version_major@.@jemalloc_version_minor@.@jemalloc_version_bugfix@_@jemalloc_version_nrev@\nCflags: -I${includedir}\nLibs: -L${libdir} -ljemalloc${install_suffix}\n"
  },
  {
    "path": "deps/jemalloc/m4/ax_cxx_compile_stdcxx.m4",
    "content": "# ===========================================================================\n#   http://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx.html\n# ===========================================================================\n#\n# SYNOPSIS\n#\n#   AX_CXX_COMPILE_STDCXX(VERSION, [ext|noext], [mandatory|optional])\n#\n# DESCRIPTION\n#\n#   Check for baseline language coverage in the compiler for the specified\n#   version of the C++ standard.  If necessary, add switches to CXX and\n#   CXXCPP to enable support.  VERSION may be '11' (for the C++11 standard)\n#   or '14' (for the C++14 standard).\n#\n#   The second argument, if specified, indicates whether you insist on an\n#   extended mode (e.g. -std=gnu++11) or a strict conformance mode (e.g.\n#   -std=c++11).  If neither is specified, you get whatever works, with\n#   preference for an extended mode.\n#\n#   The third argument, if specified 'mandatory' or if left unspecified,\n#   indicates that baseline support for the specified C++ standard is\n#   required and that the macro should error out if no mode with that\n#   support is found.  If specified 'optional', then configuration proceeds\n#   regardless, after defining HAVE_CXX${VERSION} if and only if a\n#   supporting mode is found.\n#\n# LICENSE\n#\n#   Copyright (c) 2008 Benjamin Kosnik <bkoz@redhat.com>\n#   Copyright (c) 2012 Zack Weinberg <zackw@panix.com>\n#   Copyright (c) 2013 Roy Stogner <roystgnr@ices.utexas.edu>\n#   Copyright (c) 2014, 2015 Google Inc.; contributed by Alexey Sokolov <sokolov@google.com>\n#   Copyright (c) 2015 Paul Norman <penorman@mac.com>\n#   Copyright (c) 2015 Moritz Klammler <moritz@klammler.eu>\n#\n#   Copying and distribution of this file, with or without modification, are\n#   permitted in any medium without royalty provided the copyright notice\n#   and this notice are preserved.  This file is offered as-is, without any\n#   warranty.\n\n#serial 4\n\ndnl  This macro is based on the code from the AX_CXX_COMPILE_STDCXX_11 macro\ndnl  (serial version number 13).\n\nAC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl\n  m4_if([$1], [11], [],\n        [$1], [14], [],\n        [$1], [17], [m4_fatal([support for C++17 not yet implemented in AX_CXX_COMPILE_STDCXX])],\n        [m4_fatal([invalid first argument `$1' to AX_CXX_COMPILE_STDCXX])])dnl\n  m4_if([$2], [], [],\n        [$2], [ext], [],\n        [$2], [noext], [],\n        [m4_fatal([invalid second argument `$2' to AX_CXX_COMPILE_STDCXX])])dnl\n  m4_if([$3], [], [ax_cxx_compile_cxx$1_required=true],\n        [$3], [mandatory], [ax_cxx_compile_cxx$1_required=true],\n        [$3], [optional], [ax_cxx_compile_cxx$1_required=false],\n        [m4_fatal([invalid third argument `$3' to AX_CXX_COMPILE_STDCXX])])\n  AC_LANG_PUSH([C++])dnl\n  ac_success=no\n  AC_CACHE_CHECK(whether $CXX supports C++$1 features by default,\n  ax_cv_cxx_compile_cxx$1,\n  [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],\n    [ax_cv_cxx_compile_cxx$1=yes],\n    [ax_cv_cxx_compile_cxx$1=no])])\n  if test x$ax_cv_cxx_compile_cxx$1 = xyes; then\n    ac_success=yes\n  fi\n\n  m4_if([$2], [noext], [], [dnl\n  if test x$ac_success = xno; then\n    for switch in -std=gnu++$1 -std=gnu++0x; do\n      cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])\n      AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,\n                     $cachevar,\n        [ac_save_CXX=\"$CXX\"\n         CXX=\"$CXX $switch\"\n         AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],\n          [eval $cachevar=yes],\n          [eval $cachevar=no])\n         CXX=\"$ac_save_CXX\"])\n      if eval test x\\$$cachevar = xyes; then\n        CXX=\"$CXX $switch\"\n        if test -n \"$CXXCPP\" ; then\n          CXXCPP=\"$CXXCPP $switch\"\n        fi\n        ac_success=yes\n        break\n      fi\n    done\n  fi])\n\n  m4_if([$2], [ext], [], [dnl\n  if test x$ac_success = xno; then\n    dnl HP's aCC needs +std=c++11 according to:\n    dnl http://h21007.www2.hp.com/portal/download/files/unprot/aCxx/PDF_Release_Notes/769149-001.pdf\n    dnl Cray's crayCC needs \"-h std=c++11\"\n    for switch in -std=c++$1 -std=c++0x +std=c++$1 \"-h std=c++$1\"; do\n      cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])\n      AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,\n                     $cachevar,\n        [ac_save_CXX=\"$CXX\"\n         CXX=\"$CXX $switch\"\n         AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],\n          [eval $cachevar=yes],\n          [eval $cachevar=no])\n         CXX=\"$ac_save_CXX\"])\n      if eval test x\\$$cachevar = xyes; then\n        CXX=\"$CXX $switch\"\n        if test -n \"$CXXCPP\" ; then\n          CXXCPP=\"$CXXCPP $switch\"\n        fi\n        ac_success=yes\n        break\n      fi\n    done\n  fi])\n  AC_LANG_POP([C++])\n  if test x$ax_cxx_compile_cxx$1_required = xtrue; then\n    if test x$ac_success = xno; then\n      AC_MSG_ERROR([*** A compiler with support for C++$1 language features is required.])\n    fi\n  fi\n  if test x$ac_success = xno; then\n    HAVE_CXX$1=0\n    AC_MSG_NOTICE([No compiler with C++$1 support was found])\n  else\n    HAVE_CXX$1=1\n    AC_DEFINE(HAVE_CXX$1,1,\n              [define if the compiler supports basic C++$1 syntax])\n  fi\n  AC_SUBST(HAVE_CXX$1)\n])\n\n\ndnl  Test body for checking C++11 support\n\nm4_define([_AX_CXX_COMPILE_STDCXX_testbody_11],\n  _AX_CXX_COMPILE_STDCXX_testbody_new_in_11\n)\n\n\ndnl  Test body for checking C++14 support\n\nm4_define([_AX_CXX_COMPILE_STDCXX_testbody_14],\n  _AX_CXX_COMPILE_STDCXX_testbody_new_in_11\n  _AX_CXX_COMPILE_STDCXX_testbody_new_in_14\n)\n\n\ndnl  Tests for new features in C++11\n\nm4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_11], [[\n\n// If the compiler admits that it is not ready for C++11, why torture it?\n// Hopefully, this will speed up the test.\n\n#ifndef __cplusplus\n\n#error \"This is not a C++ compiler\"\n\n#elif __cplusplus < 201103L\n\n#error \"This is not a C++11 compiler\"\n\n#else\n\nnamespace cxx11\n{\n\n  namespace test_static_assert\n  {\n\n    template <typename T>\n    struct check\n    {\n      static_assert(sizeof(int) <= sizeof(T), \"not big enough\");\n    };\n\n  }\n\n  namespace test_final_override\n  {\n\n    struct Base\n    {\n      virtual void f() {}\n    };\n\n    struct Derived : public Base\n    {\n      virtual void f() override {}\n    };\n\n  }\n\n  namespace test_double_right_angle_brackets\n  {\n\n    template < typename T >\n    struct check {};\n\n    typedef check<void> single_type;\n    typedef check<check<void>> double_type;\n    typedef check<check<check<void>>> triple_type;\n    typedef check<check<check<check<void>>>> quadruple_type;\n\n  }\n\n  namespace test_decltype\n  {\n\n    int\n    f()\n    {\n      int a = 1;\n      decltype(a) b = 2;\n      return a + b;\n    }\n\n  }\n\n  namespace test_type_deduction\n  {\n\n    template < typename T1, typename T2 >\n    struct is_same\n    {\n      static const bool value = false;\n    };\n\n    template < typename T >\n    struct is_same<T, T>\n    {\n      static const bool value = true;\n    };\n\n    template < typename T1, typename T2 >\n    auto\n    add(T1 a1, T2 a2) -> decltype(a1 + a2)\n    {\n      return a1 + a2;\n    }\n\n    int\n    test(const int c, volatile int v)\n    {\n      static_assert(is_same<int, decltype(0)>::value == true, \"\");\n      static_assert(is_same<int, decltype(c)>::value == false, \"\");\n      static_assert(is_same<int, decltype(v)>::value == false, \"\");\n      auto ac = c;\n      auto av = v;\n      auto sumi = ac + av + 'x';\n      auto sumf = ac + av + 1.0;\n      static_assert(is_same<int, decltype(ac)>::value == true, \"\");\n      static_assert(is_same<int, decltype(av)>::value == true, \"\");\n      static_assert(is_same<int, decltype(sumi)>::value == true, \"\");\n      static_assert(is_same<int, decltype(sumf)>::value == false, \"\");\n      static_assert(is_same<int, decltype(add(c, v))>::value == true, \"\");\n      return (sumf > 0.0) ? sumi : add(c, v);\n    }\n\n  }\n\n  namespace test_noexcept\n  {\n\n    int f() { return 0; }\n    int g() noexcept { return 0; }\n\n    static_assert(noexcept(f()) == false, \"\");\n    static_assert(noexcept(g()) == true, \"\");\n\n  }\n\n  namespace test_constexpr\n  {\n\n    template < typename CharT >\n    unsigned long constexpr\n    strlen_c_r(const CharT *const s, const unsigned long acc) noexcept\n    {\n      return *s ? strlen_c_r(s + 1, acc + 1) : acc;\n    }\n\n    template < typename CharT >\n    unsigned long constexpr\n    strlen_c(const CharT *const s) noexcept\n    {\n      return strlen_c_r(s, 0UL);\n    }\n\n    static_assert(strlen_c(\"\") == 0UL, \"\");\n    static_assert(strlen_c(\"1\") == 1UL, \"\");\n    static_assert(strlen_c(\"example\") == 7UL, \"\");\n    static_assert(strlen_c(\"another\\0example\") == 7UL, \"\");\n\n  }\n\n  namespace test_rvalue_references\n  {\n\n    template < int N >\n    struct answer\n    {\n      static constexpr int value = N;\n    };\n\n    answer<1> f(int&)       { return answer<1>(); }\n    answer<2> f(const int&) { return answer<2>(); }\n    answer<3> f(int&&)      { return answer<3>(); }\n\n    void\n    test()\n    {\n      int i = 0;\n      const int c = 0;\n      static_assert(decltype(f(i))::value == 1, \"\");\n      static_assert(decltype(f(c))::value == 2, \"\");\n      static_assert(decltype(f(0))::value == 3, \"\");\n    }\n\n  }\n\n  namespace test_uniform_initialization\n  {\n\n    struct test\n    {\n      static const int zero {};\n      static const int one {1};\n    };\n\n    static_assert(test::zero == 0, \"\");\n    static_assert(test::one == 1, \"\");\n\n  }\n\n  namespace test_lambdas\n  {\n\n    void\n    test1()\n    {\n      auto lambda1 = [](){};\n      auto lambda2 = lambda1;\n      lambda1();\n      lambda2();\n    }\n\n    int\n    test2()\n    {\n      auto a = [](int i, int j){ return i + j; }(1, 2);\n      auto b = []() -> int { return '0'; }();\n      auto c = [=](){ return a + b; }();\n      auto d = [&](){ return c; }();\n      auto e = [a, &b](int x) mutable {\n        const auto identity = [](int y){ return y; };\n        for (auto i = 0; i < a; ++i)\n          a += b--;\n        return x + identity(a + b);\n      }(0);\n      return a + b + c + d + e;\n    }\n\n    int\n    test3()\n    {\n      const auto nullary = [](){ return 0; };\n      const auto unary = [](int x){ return x; };\n      using nullary_t = decltype(nullary);\n      using unary_t = decltype(unary);\n      const auto higher1st = [](nullary_t f){ return f(); };\n      const auto higher2nd = [unary](nullary_t f1){\n        return [unary, f1](unary_t f2){ return f2(unary(f1())); };\n      };\n      return higher1st(nullary) + higher2nd(nullary)(unary);\n    }\n\n  }\n\n  namespace test_variadic_templates\n  {\n\n    template <int...>\n    struct sum;\n\n    template <int N0, int... N1toN>\n    struct sum<N0, N1toN...>\n    {\n      static constexpr auto value = N0 + sum<N1toN...>::value;\n    };\n\n    template <>\n    struct sum<>\n    {\n      static constexpr auto value = 0;\n    };\n\n    static_assert(sum<>::value == 0, \"\");\n    static_assert(sum<1>::value == 1, \"\");\n    static_assert(sum<23>::value == 23, \"\");\n    static_assert(sum<1, 2>::value == 3, \"\");\n    static_assert(sum<5, 5, 11>::value == 21, \"\");\n    static_assert(sum<2, 3, 5, 7, 11, 13>::value == 41, \"\");\n\n  }\n\n  // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae\n  // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function\n  // because of this.\n  namespace test_template_alias_sfinae\n  {\n\n    struct foo {};\n\n    template<typename T>\n    using member = typename T::member_type;\n\n    template<typename T>\n    void func(...) {}\n\n    template<typename T>\n    void func(member<T>*) {}\n\n    void test();\n\n    void test() { func<foo>(0); }\n\n  }\n\n}  // namespace cxx11\n\n#endif  // __cplusplus >= 201103L\n\n]])\n\n\ndnl  Tests for new features in C++14\n\nm4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_14], [[\n\n// If the compiler admits that it is not ready for C++14, why torture it?\n// Hopefully, this will speed up the test.\n\n#ifndef __cplusplus\n\n#error \"This is not a C++ compiler\"\n\n#elif __cplusplus < 201402L\n\n#error \"This is not a C++14 compiler\"\n\n#else\n\nnamespace cxx14\n{\n\n  namespace test_polymorphic_lambdas\n  {\n\n    int\n    test()\n    {\n      const auto lambda = [](auto&&... args){\n        const auto istiny = [](auto x){\n          return (sizeof(x) == 1UL) ? 1 : 0;\n        };\n        const int aretiny[] = { istiny(args)... };\n        return aretiny[0];\n      };\n      return lambda(1, 1L, 1.0f, '1');\n    }\n\n  }\n\n  namespace test_binary_literals\n  {\n\n    constexpr auto ivii = 0b0000000000101010;\n    static_assert(ivii == 42, \"wrong value\");\n\n  }\n\n  namespace test_generalized_constexpr\n  {\n\n    template < typename CharT >\n    constexpr unsigned long\n    strlen_c(const CharT *const s) noexcept\n    {\n      auto length = 0UL;\n      for (auto p = s; *p; ++p)\n        ++length;\n      return length;\n    }\n\n    static_assert(strlen_c(\"\") == 0UL, \"\");\n    static_assert(strlen_c(\"x\") == 1UL, \"\");\n    static_assert(strlen_c(\"test\") == 4UL, \"\");\n    static_assert(strlen_c(\"another\\0test\") == 7UL, \"\");\n\n  }\n\n  namespace test_lambda_init_capture\n  {\n\n    int\n    test()\n    {\n      auto x = 0;\n      const auto lambda1 = [a = x](int b){ return a + b; };\n      const auto lambda2 = [a = lambda1(x)](){ return a; };\n      return lambda2();\n    }\n\n  }\n\n  namespace test_digit_seperators\n  {\n\n    constexpr auto ten_million = 100'000'000;\n    static_assert(ten_million == 100000000, \"\");\n\n  }\n\n  namespace test_return_type_deduction\n  {\n\n    auto f(int& x) { return x; }\n    decltype(auto) g(int& x) { return x; }\n\n    template < typename T1, typename T2 >\n    struct is_same\n    {\n      static constexpr auto value = false;\n    };\n\n    template < typename T >\n    struct is_same<T, T>\n    {\n      static constexpr auto value = true;\n    };\n\n    int\n    test()\n    {\n      auto x = 0;\n      static_assert(is_same<int, decltype(f(x))>::value, \"\");\n      static_assert(is_same<int&, decltype(g(x))>::value, \"\");\n      return x;\n    }\n\n  }\n\n}  // namespace cxx14\n\n#endif  // __cplusplus >= 201402L\n\n]])\n"
  },
  {
    "path": "deps/jemalloc/msvc/ReadMe.txt",
    "content": "\nHow to build jemalloc for Windows\n=================================\n\n1. Install Cygwin with at least the following packages:\n   * autoconf\n   * autogen\n   * gawk\n   * grep\n   * sed\n\n2. Install Visual Studio 2015 or 2017 with Visual C++\n\n3. Add Cygwin\\bin to the PATH environment variable\n\n4. Open \"x64 Native Tools Command Prompt for VS 2017\"\n   (note: x86/x64 doesn't matter at this point)\n\n5. Generate header files:\n   sh -c \"CC=cl ./autogen.sh\"\n\n6. Now the project can be opened and built in Visual Studio:\n   msvc\\jemalloc_vc2017.sln\n"
  },
  {
    "path": "deps/jemalloc/msvc/jemalloc_vc2015.sln",
    "content": "﻿\nMicrosoft Visual Studio Solution File, Format Version 12.00\n# Visual Studio 14\nVisualStudioVersion = 14.0.24720.0\nMinimumVisualStudioVersion = 10.0.40219.1\nProject(\"{2150E333-8FDC-42A3-9474-1A3956D46DE8}\") = \"Solution Items\", \"Solution Items\", \"{70A99006-6DE9-472B-8F83-4CEE6C616DF3}\"\n\tProjectSection(SolutionItems) = preProject\n\t\tReadMe.txt = ReadMe.txt\n\tEndProjectSection\nEndProject\nProject(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \"jemalloc\", \"projects\\vc2015\\jemalloc\\jemalloc.vcxproj\", \"{8D6BB292-9E1C-413D-9F98-4864BDC1514A}\"\nEndProject\nProject(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \"test_threads\", \"projects\\vc2015\\test_threads\\test_threads.vcxproj\", \"{09028CFD-4EB7-491D-869C-0708DB97ED44}\"\nEndProject\nGlobal\n\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\n\t\tDebug|x64 = Debug|x64\n\t\tDebug|x86 = Debug|x86\n\t\tDebug-static|x64 = Debug-static|x64\n\t\tDebug-static|x86 = Debug-static|x86\n\t\tRelease|x64 = Release|x64\n\t\tRelease|x86 = Release|x86\n\t\tRelease-static|x64 = Release-static|x64\n\t\tRelease-static|x86 = Release-static|x86\n\tEndGlobalSection\n\tGlobalSection(ProjectConfigurationPlatforms) = postSolution\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Debug|x64.ActiveCfg = Debug|x64\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Debug|x64.Build.0 = Debug|x64\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Debug|x86.ActiveCfg = Debug|Win32\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Debug|x86.Build.0 = Debug|Win32\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Debug-static|x64.ActiveCfg = Debug-static|x64\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Debug-static|x64.Build.0 = Debug-static|x64\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Debug-static|x86.ActiveCfg = Debug-static|Win32\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Debug-static|x86.Build.0 = Debug-static|Win32\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Release|x64.ActiveCfg = Release|x64\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Release|x64.Build.0 = Release|x64\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Release|x86.ActiveCfg = Release|Win32\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Release|x86.Build.0 = Release|Win32\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Release-static|x64.ActiveCfg = Release-static|x64\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Release-static|x64.Build.0 = Release-static|x64\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Release-static|x86.ActiveCfg = Release-static|Win32\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Release-static|x86.Build.0 = Release-static|Win32\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Debug|x64.ActiveCfg = Debug|x64\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Debug|x64.Build.0 = Debug|x64\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Debug|x86.ActiveCfg = Debug|Win32\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Debug|x86.Build.0 = Debug|Win32\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Debug-static|x64.ActiveCfg = Debug-static|x64\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Debug-static|x64.Build.0 = Debug-static|x64\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Debug-static|x86.ActiveCfg = Debug-static|Win32\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Debug-static|x86.Build.0 = Debug-static|Win32\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Release|x64.ActiveCfg = Release|x64\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Release|x64.Build.0 = Release|x64\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Release|x86.ActiveCfg = Release|Win32\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Release|x86.Build.0 = Release|Win32\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Release-static|x64.ActiveCfg = Release-static|x64\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Release-static|x64.Build.0 = Release-static|x64\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Release-static|x86.ActiveCfg = Release-static|Win32\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Release-static|x86.Build.0 = Release-static|Win32\n\tEndGlobalSection\n\tGlobalSection(SolutionProperties) = preSolution\n\t\tHideSolutionNode = FALSE\n\tEndGlobalSection\nEndGlobal\n"
  },
  {
    "path": "deps/jemalloc/msvc/jemalloc_vc2017.sln",
    "content": "﻿\nMicrosoft Visual Studio Solution File, Format Version 12.00\n# Visual Studio 14\nVisualStudioVersion = 14.0.24720.0\nMinimumVisualStudioVersion = 10.0.40219.1\nProject(\"{2150E333-8FDC-42A3-9474-1A3956D46DE8}\") = \"Solution Items\", \"Solution Items\", \"{70A99006-6DE9-472B-8F83-4CEE6C616DF3}\"\n\tProjectSection(SolutionItems) = preProject\n\t\tReadMe.txt = ReadMe.txt\n\tEndProjectSection\nEndProject\nProject(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \"jemalloc\", \"projects\\vc2017\\jemalloc\\jemalloc.vcxproj\", \"{8D6BB292-9E1C-413D-9F98-4864BDC1514A}\"\nEndProject\nProject(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \"test_threads\", \"projects\\vc2017\\test_threads\\test_threads.vcxproj\", \"{09028CFD-4EB7-491D-869C-0708DB97ED44}\"\nEndProject\nGlobal\n\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\n\t\tDebug|x64 = Debug|x64\n\t\tDebug|x86 = Debug|x86\n\t\tDebug-static|x64 = Debug-static|x64\n\t\tDebug-static|x86 = Debug-static|x86\n\t\tRelease|x64 = Release|x64\n\t\tRelease|x86 = Release|x86\n\t\tRelease-static|x64 = Release-static|x64\n\t\tRelease-static|x86 = Release-static|x86\n\tEndGlobalSection\n\tGlobalSection(ProjectConfigurationPlatforms) = postSolution\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Debug|x64.ActiveCfg = Debug|x64\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Debug|x64.Build.0 = Debug|x64\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Debug|x86.ActiveCfg = Debug|Win32\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Debug|x86.Build.0 = Debug|Win32\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Debug-static|x64.ActiveCfg = Debug-static|x64\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Debug-static|x64.Build.0 = Debug-static|x64\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Debug-static|x86.ActiveCfg = Debug-static|Win32\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Debug-static|x86.Build.0 = Debug-static|Win32\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Release|x64.ActiveCfg = Release|x64\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Release|x64.Build.0 = Release|x64\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Release|x86.ActiveCfg = Release|Win32\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Release|x86.Build.0 = Release|Win32\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Release-static|x64.ActiveCfg = Release-static|x64\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Release-static|x64.Build.0 = Release-static|x64\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Release-static|x86.ActiveCfg = Release-static|Win32\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Release-static|x86.Build.0 = Release-static|Win32\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Debug|x64.ActiveCfg = Debug|x64\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Debug|x64.Build.0 = Debug|x64\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Debug|x86.ActiveCfg = Debug|Win32\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Debug|x86.Build.0 = Debug|Win32\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Debug-static|x64.ActiveCfg = Debug-static|x64\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Debug-static|x64.Build.0 = Debug-static|x64\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Debug-static|x86.ActiveCfg = Debug-static|Win32\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Debug-static|x86.Build.0 = Debug-static|Win32\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Release|x64.ActiveCfg = Release|x64\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Release|x64.Build.0 = Release|x64\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Release|x86.ActiveCfg = Release|Win32\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Release|x86.Build.0 = Release|Win32\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Release-static|x64.ActiveCfg = Release-static|x64\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Release-static|x64.Build.0 = Release-static|x64\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Release-static|x86.ActiveCfg = Release-static|Win32\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Release-static|x86.Build.0 = Release-static|Win32\n\tEndGlobalSection\n\tGlobalSection(SolutionProperties) = preSolution\n\t\tHideSolutionNode = FALSE\n\tEndGlobalSection\nEndGlobal\n"
  },
  {
    "path": "deps/jemalloc/msvc/projects/vc2015/jemalloc/jemalloc.vcxproj",
    "content": "﻿<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<Project DefaultTargets=\"Build\" ToolsVersion=\"14.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n  <ItemGroup Label=\"ProjectConfigurations\">\n    <ProjectConfiguration Include=\"Debug-static|Win32\">\n      <Configuration>Debug-static</Configuration>\n      <Platform>Win32</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Debug-static|x64\">\n      <Configuration>Debug-static</Configuration>\n      <Platform>x64</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Debug|Win32\">\n      <Configuration>Debug</Configuration>\n      <Platform>Win32</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Release-static|Win32\">\n      <Configuration>Release-static</Configuration>\n      <Platform>Win32</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Release-static|x64\">\n      <Configuration>Release-static</Configuration>\n      <Platform>x64</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Release|Win32\">\n      <Configuration>Release</Configuration>\n      <Platform>Win32</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Debug|x64\">\n      <Configuration>Debug</Configuration>\n      <Platform>x64</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Release|x64\">\n      <Configuration>Release</Configuration>\n      <Platform>x64</Platform>\n    </ProjectConfiguration>\n  </ItemGroup>\n  <ItemGroup>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\arena.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\background_thread.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\base.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\bin.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\bitmap.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\ckh.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\ctl.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\div.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\extent.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\extent_dss.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\extent_mmap.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\hash.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\hook.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\jemalloc.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\large.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\log.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\malloc_io.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\mutex.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\mutex_pool.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\nstime.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\pages.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\prng.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\prof.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\rtree.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\sc.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\stats.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\sz.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\tcache.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\ticker.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\tsd.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\witness.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\safety_check.c\" />\n  </ItemGroup>\n  <PropertyGroup Label=\"Globals\">\n    <ProjectGuid>{8D6BB292-9E1C-413D-9F98-4864BDC1514A}</ProjectGuid>\n    <Keyword>Win32Proj</Keyword>\n    <RootNamespace>jemalloc</RootNamespace>\n    <WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>\n  </PropertyGroup>\n  <Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\" Label=\"Configuration\">\n    <ConfigurationType>DynamicLibrary</ConfigurationType>\n    <UseDebugLibraries>true</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|Win32'\" Label=\"Configuration\">\n    <ConfigurationType>StaticLibrary</ConfigurationType>\n    <UseDebugLibraries>true</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\" Label=\"Configuration\">\n    <ConfigurationType>DynamicLibrary</ConfigurationType>\n    <UseDebugLibraries>false</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <WholeProgramOptimization>true</WholeProgramOptimization>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|Win32'\" Label=\"Configuration\">\n    <ConfigurationType>StaticLibrary</ConfigurationType>\n    <UseDebugLibraries>false</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <WholeProgramOptimization>true</WholeProgramOptimization>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\" Label=\"Configuration\">\n    <ConfigurationType>DynamicLibrary</ConfigurationType>\n    <UseDebugLibraries>true</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|x64'\" Label=\"Configuration\">\n    <ConfigurationType>StaticLibrary</ConfigurationType>\n    <UseDebugLibraries>true</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\" Label=\"Configuration\">\n    <ConfigurationType>DynamicLibrary</ConfigurationType>\n    <UseDebugLibraries>false</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <WholeProgramOptimization>true</WholeProgramOptimization>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|x64'\" Label=\"Configuration\">\n    <ConfigurationType>StaticLibrary</ConfigurationType>\n    <UseDebugLibraries>false</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <WholeProgramOptimization>true</WholeProgramOptimization>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.props\" />\n  <ImportGroup Label=\"ExtensionSettings\">\n  </ImportGroup>\n  <ImportGroup Label=\"Shared\">\n  </ImportGroup>\n  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|Win32'\" Label=\"PropertySheets\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|Win32'\" Label=\"PropertySheets\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|x64'\" Label=\"PropertySheets\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|x64'\" Label=\"PropertySheets\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <PropertyGroup Label=\"UserMacros\" />\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <TargetName>$(ProjectName)d</TargetName>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|Win32'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <TargetName>$(ProjectName)-$(PlatformToolset)-$(Configuration)</TargetName>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|Win32'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <TargetName>$(ProjectName)-$(PlatformToolset)-$(Configuration)</TargetName>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <TargetName>$(ProjectName)d</TargetName>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|x64'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <TargetName>$(ProjectName)-vc$(PlatformToolsetVersion)-$(Configuration)</TargetName>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|x64'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <TargetName>$(ProjectName)-vc$(PlatformToolsetVersion)-$(Configuration)</TargetName>\n  </PropertyGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\">\n    <ClCompile>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <WarningLevel>Level3</WarningLevel>\n      <Optimization>Disabled</Optimization>\n      <PreprocessorDefinitions>JEMALLOC_NO_PRIVATE_NAMESPACE;_REENTRANT;_WINDLL;DLLEXPORT;JEMALLOC_DEBUG;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <DisableSpecificWarnings>4090;4146;4267;4334</DisableSpecificWarnings>\n      <ProgramDataBaseFileName>$(OutputPath)$(TargetName).pdb</ProgramDataBaseFileName>\n    </ClCompile>\n    <Link>\n      <SubSystem>Windows</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|Win32'\">\n    <ClCompile>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <WarningLevel>Level3</WarningLevel>\n      <Optimization>Disabled</Optimization>\n      <PreprocessorDefinitions>JEMALLOC_NO_PRIVATE_NAMESPACE;JEMALLOC_DEBUG;_REENTRANT;JEMALLOC_EXPORT=;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>\n      <DisableSpecificWarnings>4090;4146;4267;4334</DisableSpecificWarnings>\n      <ProgramDataBaseFileName>$(OutputPath)$(TargetName).pdb</ProgramDataBaseFileName>\n    </ClCompile>\n    <Link>\n      <SubSystem>Windows</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\">\n    <ClCompile>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <WarningLevel>Level3</WarningLevel>\n      <Optimization>Disabled</Optimization>\n      <PreprocessorDefinitions>JEMALLOC_NO_PRIVATE_NAMESPACE;_REENTRANT;_WINDLL;DLLEXPORT;JEMALLOC_DEBUG;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <DisableSpecificWarnings>4090;4146;4267;4334</DisableSpecificWarnings>\n      <ProgramDataBaseFileName>$(OutputPath)$(TargetName).pdb</ProgramDataBaseFileName>\n    </ClCompile>\n    <Link>\n      <SubSystem>Windows</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|x64'\">\n    <ClCompile>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <WarningLevel>Level3</WarningLevel>\n      <Optimization>Disabled</Optimization>\n      <PreprocessorDefinitions>JEMALLOC_NO_PRIVATE_NAMESPACE;JEMALLOC_DEBUG;_REENTRANT;JEMALLOC_EXPORT=;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>\n      <DisableSpecificWarnings>4090;4146;4267;4334</DisableSpecificWarnings>\n      <DebugInformationFormat>OldStyle</DebugInformationFormat>\n      <MinimalRebuild>false</MinimalRebuild>\n    </ClCompile>\n    <Link>\n      <SubSystem>Windows</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\">\n    <ClCompile>\n      <WarningLevel>Level3</WarningLevel>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <Optimization>MaxSpeed</Optimization>\n      <FunctionLevelLinking>true</FunctionLevelLinking>\n      <IntrinsicFunctions>true</IntrinsicFunctions>\n      <PreprocessorDefinitions>JEMALLOC_NO_PRIVATE_NAMESPACE;_REENTRANT;_WINDLL;DLLEXPORT;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <DisableSpecificWarnings>4090;4146;4267;4334</DisableSpecificWarnings>\n      <ProgramDataBaseFileName>$(OutputPath)$(TargetName).pdb</ProgramDataBaseFileName>\n    </ClCompile>\n    <Link>\n      <SubSystem>Windows</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <EnableCOMDATFolding>true</EnableCOMDATFolding>\n      <OptimizeReferences>true</OptimizeReferences>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|Win32'\">\n    <ClCompile>\n      <WarningLevel>Level3</WarningLevel>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <Optimization>MaxSpeed</Optimization>\n      <FunctionLevelLinking>true</FunctionLevelLinking>\n      <IntrinsicFunctions>true</IntrinsicFunctions>\n      <PreprocessorDefinitions>JEMALLOC_NO_PRIVATE_NAMESPACE;_REENTRANT;JEMALLOC_EXPORT=;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <RuntimeLibrary>MultiThreaded</RuntimeLibrary>\n      <DisableSpecificWarnings>4090;4146;4267;4334</DisableSpecificWarnings>\n      <ProgramDataBaseFileName>$(OutputPath)$(TargetName).pdb</ProgramDataBaseFileName>\n    </ClCompile>\n    <Link>\n      <SubSystem>Windows</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <EnableCOMDATFolding>true</EnableCOMDATFolding>\n      <OptimizeReferences>true</OptimizeReferences>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\">\n    <ClCompile>\n      <WarningLevel>Level3</WarningLevel>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <Optimization>MaxSpeed</Optimization>\n      <FunctionLevelLinking>true</FunctionLevelLinking>\n      <IntrinsicFunctions>true</IntrinsicFunctions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <PreprocessorDefinitions>JEMALLOC_NO_PRIVATE_NAMESPACE;_REENTRANT;_WINDLL;DLLEXPORT;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <DisableSpecificWarnings>4090;4146;4267;4334</DisableSpecificWarnings>\n      <ProgramDataBaseFileName>$(OutputPath)$(TargetName).pdb</ProgramDataBaseFileName>\n    </ClCompile>\n    <Link>\n      <SubSystem>Windows</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <EnableCOMDATFolding>true</EnableCOMDATFolding>\n      <OptimizeReferences>true</OptimizeReferences>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|x64'\">\n    <ClCompile>\n      <WarningLevel>Level3</WarningLevel>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <Optimization>MaxSpeed</Optimization>\n      <FunctionLevelLinking>true</FunctionLevelLinking>\n      <IntrinsicFunctions>true</IntrinsicFunctions>\n      <PreprocessorDefinitions>JEMALLOC_NO_PRIVATE_NAMESPACE;_REENTRANT;JEMALLOC_EXPORT=;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <RuntimeLibrary>MultiThreaded</RuntimeLibrary>\n      <DisableSpecificWarnings>4090;4146;4267;4334</DisableSpecificWarnings>\n      <DebugInformationFormat>OldStyle</DebugInformationFormat>\n    </ClCompile>\n    <Link>\n      <SubSystem>Windows</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <EnableCOMDATFolding>true</EnableCOMDATFolding>\n      <OptimizeReferences>true</OptimizeReferences>\n    </Link>\n  </ItemDefinitionGroup>\n  <Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.targets\" />\n  <ImportGroup Label=\"ExtensionTargets\">\n  </ImportGroup>\n</Project>\n"
  },
  {
    "path": "deps/jemalloc/msvc/projects/vc2015/jemalloc/jemalloc.vcxproj.filters",
    "content": "﻿<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<Project ToolsVersion=\"4.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n  <ItemGroup>\n    <Filter Include=\"Source Files\">\n      <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>\n      <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>\n    </Filter>\n  </ItemGroup>\n  <ItemGroup>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\arena.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\background_thread.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\base.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\bitmap.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\ckh.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\ctl.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\extent.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\extent_dss.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\extent_mmap.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\hash.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\hook.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\jemalloc.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\large.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\malloc_io.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\mutex.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\mutex_pool.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\nstime.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\pages.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\prng.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\prof.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\rtree.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\sc.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\stats.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\sz.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\tcache.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\ticker.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\tsd.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\witness.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\log.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\bin.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\div.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\safety_check.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n  </ItemGroup>\n</Project>\n"
  },
  {
    "path": "deps/jemalloc/msvc/projects/vc2015/test_threads/test_threads.vcxproj",
    "content": "﻿<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<Project DefaultTargets=\"Build\" ToolsVersion=\"14.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n  <ItemGroup Label=\"ProjectConfigurations\">\n    <ProjectConfiguration Include=\"Debug-static|Win32\">\n      <Configuration>Debug-static</Configuration>\n      <Platform>Win32</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Debug-static|x64\">\n      <Configuration>Debug-static</Configuration>\n      <Platform>x64</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Debug|Win32\">\n      <Configuration>Debug</Configuration>\n      <Platform>Win32</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Release-static|Win32\">\n      <Configuration>Release-static</Configuration>\n      <Platform>Win32</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Release-static|x64\">\n      <Configuration>Release-static</Configuration>\n      <Platform>x64</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Release|Win32\">\n      <Configuration>Release</Configuration>\n      <Platform>Win32</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Debug|x64\">\n      <Configuration>Debug</Configuration>\n      <Platform>x64</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Release|x64\">\n      <Configuration>Release</Configuration>\n      <Platform>x64</Platform>\n    </ProjectConfiguration>\n  </ItemGroup>\n  <PropertyGroup Label=\"Globals\">\n    <ProjectGuid>{09028CFD-4EB7-491D-869C-0708DB97ED44}</ProjectGuid>\n    <Keyword>Win32Proj</Keyword>\n    <RootNamespace>test_threads</RootNamespace>\n    <WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>\n  </PropertyGroup>\n  <Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\" Label=\"Configuration\">\n    <ConfigurationType>Application</ConfigurationType>\n    <UseDebugLibraries>true</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|Win32'\" Label=\"Configuration\">\n    <ConfigurationType>Application</ConfigurationType>\n    <UseDebugLibraries>true</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\" Label=\"Configuration\">\n    <ConfigurationType>Application</ConfigurationType>\n    <UseDebugLibraries>false</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <WholeProgramOptimization>true</WholeProgramOptimization>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|Win32'\" Label=\"Configuration\">\n    <ConfigurationType>Application</ConfigurationType>\n    <UseDebugLibraries>false</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <WholeProgramOptimization>true</WholeProgramOptimization>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\" Label=\"Configuration\">\n    <ConfigurationType>Application</ConfigurationType>\n    <UseDebugLibraries>true</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|x64'\" Label=\"Configuration\">\n    <ConfigurationType>Application</ConfigurationType>\n    <UseDebugLibraries>true</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\" Label=\"Configuration\">\n    <ConfigurationType>Application</ConfigurationType>\n    <UseDebugLibraries>false</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <WholeProgramOptimization>true</WholeProgramOptimization>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|x64'\" Label=\"Configuration\">\n    <ConfigurationType>Application</ConfigurationType>\n    <UseDebugLibraries>false</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <WholeProgramOptimization>true</WholeProgramOptimization>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.props\" />\n  <ImportGroup Label=\"ExtensionSettings\">\n  </ImportGroup>\n  <ImportGroup Label=\"Shared\">\n  </ImportGroup>\n  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|Win32'\" Label=\"PropertySheets\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|Win32'\" Label=\"PropertySheets\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|x64'\" Label=\"PropertySheets\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|x64'\" Label=\"PropertySheets\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <PropertyGroup Label=\"UserMacros\" />\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <LinkIncremental>true</LinkIncremental>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|Win32'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <LinkIncremental>true</LinkIncremental>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\">\n    <LinkIncremental>true</LinkIncremental>\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|x64'\">\n    <LinkIncremental>true</LinkIncremental>\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <LinkIncremental>false</LinkIncremental>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|Win32'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <LinkIncremental>false</LinkIncremental>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <LinkIncremental>false</LinkIncremental>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|x64'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <LinkIncremental>false</LinkIncremental>\n  </PropertyGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\">\n    <ClCompile>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <WarningLevel>Level3</WarningLevel>\n      <Optimization>Disabled</Optimization>\n      <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\test\\include;..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n    </ClCompile>\n    <Link>\n      <SubSystem>Console</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <AdditionalLibraryDirectories>$(SolutionDir)$(Platform)\\$(Configuration)</AdditionalLibraryDirectories>\n      <AdditionalDependencies>jemallocd.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|Win32'\">\n    <ClCompile>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <WarningLevel>Level3</WarningLevel>\n      <Optimization>Disabled</Optimization>\n      <PreprocessorDefinitions>JEMALLOC_EXPORT=;JEMALLOC_STATIC;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\test\\include;..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>\n    </ClCompile>\n    <Link>\n      <SubSystem>Console</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <AdditionalLibraryDirectories>$(SolutionDir)$(Platform)\\$(Configuration)</AdditionalLibraryDirectories>\n      <AdditionalDependencies>jemalloc-$(PlatformToolset)-$(Configuration).lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\">\n    <ClCompile>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <WarningLevel>Level3</WarningLevel>\n      <Optimization>Disabled</Optimization>\n      <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\test\\include;..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n    </ClCompile>\n    <Link>\n      <SubSystem>Console</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <AdditionalDependencies>jemallocd.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>\n      <AdditionalLibraryDirectories>$(SolutionDir)$(Platform)\\$(Configuration)</AdditionalLibraryDirectories>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|x64'\">\n    <ClCompile>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <WarningLevel>Level3</WarningLevel>\n      <Optimization>Disabled</Optimization>\n      <PreprocessorDefinitions>JEMALLOC_EXPORT=;JEMALLOC_STATIC;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\test\\include;..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>\n    </ClCompile>\n    <Link>\n      <SubSystem>Console</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <AdditionalDependencies>jemalloc-vc$(PlatformToolsetVersion)-$(Configuration).lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>\n      <AdditionalLibraryDirectories>$(SolutionDir)$(Platform)\\$(Configuration)</AdditionalLibraryDirectories>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\">\n    <ClCompile>\n      <WarningLevel>Level3</WarningLevel>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <Optimization>MaxSpeed</Optimization>\n      <FunctionLevelLinking>true</FunctionLevelLinking>\n      <IntrinsicFunctions>true</IntrinsicFunctions>\n      <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\test\\include;..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n    </ClCompile>\n    <Link>\n      <SubSystem>Console</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <EnableCOMDATFolding>true</EnableCOMDATFolding>\n      <OptimizeReferences>true</OptimizeReferences>\n      <AdditionalLibraryDirectories>$(SolutionDir)$(Platform)\\$(Configuration)</AdditionalLibraryDirectories>\n      <AdditionalDependencies>jemalloc.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|Win32'\">\n    <ClCompile>\n      <WarningLevel>Level3</WarningLevel>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <Optimization>MaxSpeed</Optimization>\n      <FunctionLevelLinking>true</FunctionLevelLinking>\n      <IntrinsicFunctions>true</IntrinsicFunctions>\n      <PreprocessorDefinitions>JEMALLOC_EXPORT=;JEMALLOC_STATIC;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\test\\include;..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <RuntimeLibrary>MultiThreaded</RuntimeLibrary>\n    </ClCompile>\n    <Link>\n      <SubSystem>Console</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <EnableCOMDATFolding>true</EnableCOMDATFolding>\n      <OptimizeReferences>true</OptimizeReferences>\n      <AdditionalLibraryDirectories>$(SolutionDir)$(Platform)\\$(Configuration)</AdditionalLibraryDirectories>\n      <AdditionalDependencies>jemalloc-$(PlatformToolset)-$(Configuration).lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\">\n    <ClCompile>\n      <WarningLevel>Level3</WarningLevel>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <Optimization>MaxSpeed</Optimization>\n      <FunctionLevelLinking>true</FunctionLevelLinking>\n      <IntrinsicFunctions>true</IntrinsicFunctions>\n      <PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\test\\include;..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n    </ClCompile>\n    <Link>\n      <SubSystem>Console</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <EnableCOMDATFolding>true</EnableCOMDATFolding>\n      <OptimizeReferences>true</OptimizeReferences>\n      <AdditionalLibraryDirectories>$(SolutionDir)$(Platform)\\$(Configuration)</AdditionalLibraryDirectories>\n      <AdditionalDependencies>jemalloc.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|x64'\">\n    <ClCompile>\n      <WarningLevel>Level3</WarningLevel>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <Optimization>MaxSpeed</Optimization>\n      <FunctionLevelLinking>true</FunctionLevelLinking>\n      <IntrinsicFunctions>true</IntrinsicFunctions>\n      <PreprocessorDefinitions>JEMALLOC_EXPORT=;JEMALLOC_STATIC;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\test\\include;..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <RuntimeLibrary>MultiThreaded</RuntimeLibrary>\n    </ClCompile>\n    <Link>\n      <SubSystem>Console</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <EnableCOMDATFolding>true</EnableCOMDATFolding>\n      <OptimizeReferences>true</OptimizeReferences>\n      <AdditionalLibraryDirectories>$(SolutionDir)$(Platform)\\$(Configuration)</AdditionalLibraryDirectories>\n      <AdditionalDependencies>jemalloc-vc$(PlatformToolsetVersion)-$(Configuration).lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemGroup>\n    <ClCompile Include=\"..\\..\\..\\test_threads\\test_threads.cpp\" />\n    <ClCompile Include=\"..\\..\\..\\test_threads\\test_threads_main.cpp\" />\n  </ItemGroup>\n  <ItemGroup>\n    <ProjectReference Include=\"..\\jemalloc\\jemalloc.vcxproj\">\n      <Project>{8d6bb292-9e1c-413d-9f98-4864bdc1514a}</Project>\n    </ProjectReference>\n  </ItemGroup>\n  <ItemGroup>\n    <ClInclude Include=\"..\\..\\..\\test_threads\\test_threads.h\" />\n  </ItemGroup>\n  <Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.targets\" />\n  <ImportGroup Label=\"ExtensionTargets\">\n  </ImportGroup>\n</Project>"
  },
  {
    "path": "deps/jemalloc/msvc/projects/vc2015/test_threads/test_threads.vcxproj.filters",
    "content": "﻿<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<Project ToolsVersion=\"4.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n  <ItemGroup>\n    <Filter Include=\"Source Files\">\n      <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>\n      <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>\n    </Filter>\n    <Filter Include=\"Header Files\">\n      <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>\n      <Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>\n    </Filter>\n  </ItemGroup>\n  <ItemGroup>\n    <ClCompile Include=\"..\\..\\..\\test_threads\\test_threads.cpp\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\test_threads\\test_threads_main.cpp\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n  </ItemGroup>\n  <ItemGroup>\n    <ClInclude Include=\"..\\..\\..\\test_threads\\test_threads.h\">\n      <Filter>Header Files</Filter>\n    </ClInclude>\n  </ItemGroup>\n</Project>"
  },
  {
    "path": "deps/jemalloc/msvc/projects/vc2017/jemalloc/jemalloc.vcxproj",
    "content": "﻿<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<Project DefaultTargets=\"Build\" ToolsVersion=\"15.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n  <ItemGroup Label=\"ProjectConfigurations\">\n    <ProjectConfiguration Include=\"Debug-static|Win32\">\n      <Configuration>Debug-static</Configuration>\n      <Platform>Win32</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Debug-static|x64\">\n      <Configuration>Debug-static</Configuration>\n      <Platform>x64</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Debug|Win32\">\n      <Configuration>Debug</Configuration>\n      <Platform>Win32</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Release-static|Win32\">\n      <Configuration>Release-static</Configuration>\n      <Platform>Win32</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Release-static|x64\">\n      <Configuration>Release-static</Configuration>\n      <Platform>x64</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Release|Win32\">\n      <Configuration>Release</Configuration>\n      <Platform>Win32</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Debug|x64\">\n      <Configuration>Debug</Configuration>\n      <Platform>x64</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Release|x64\">\n      <Configuration>Release</Configuration>\n      <Platform>x64</Platform>\n    </ProjectConfiguration>\n  </ItemGroup>\n  <ItemGroup>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\arena.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\background_thread.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\base.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\bin.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\bitmap.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\ckh.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\ctl.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\div.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\extent.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\extent_dss.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\extent_mmap.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\hash.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\hook.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\jemalloc.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\large.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\log.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\malloc_io.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\mutex.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\mutex_pool.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\nstime.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\pages.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\prng.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\prof.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\rtree.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\sc.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\stats.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\sz.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\tcache.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\test_hooks.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\ticker.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\tsd.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\witness.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\safety_check.c\" />\n  </ItemGroup>\n  <PropertyGroup Label=\"Globals\">\n    <ProjectGuid>{8D6BB292-9E1C-413D-9F98-4864BDC1514A}</ProjectGuid>\n    <Keyword>Win32Proj</Keyword>\n    <RootNamespace>jemalloc</RootNamespace>\n  </PropertyGroup>\n  <Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\" Label=\"Configuration\">\n    <ConfigurationType>DynamicLibrary</ConfigurationType>\n    <UseDebugLibraries>true</UseDebugLibraries>\n    <PlatformToolset>v141</PlatformToolset>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|Win32'\" Label=\"Configuration\">\n    <ConfigurationType>StaticLibrary</ConfigurationType>\n    <UseDebugLibraries>true</UseDebugLibraries>\n    <PlatformToolset>v141</PlatformToolset>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\" Label=\"Configuration\">\n    <ConfigurationType>DynamicLibrary</ConfigurationType>\n    <UseDebugLibraries>false</UseDebugLibraries>\n    <PlatformToolset>v141</PlatformToolset>\n    <WholeProgramOptimization>true</WholeProgramOptimization>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|Win32'\" Label=\"Configuration\">\n    <ConfigurationType>StaticLibrary</ConfigurationType>\n    <UseDebugLibraries>false</UseDebugLibraries>\n    <PlatformToolset>v141</PlatformToolset>\n    <WholeProgramOptimization>true</WholeProgramOptimization>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\" Label=\"Configuration\">\n    <ConfigurationType>DynamicLibrary</ConfigurationType>\n    <UseDebugLibraries>true</UseDebugLibraries>\n    <PlatformToolset>v141</PlatformToolset>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|x64'\" Label=\"Configuration\">\n    <ConfigurationType>StaticLibrary</ConfigurationType>\n    <UseDebugLibraries>true</UseDebugLibraries>\n    <PlatformToolset>v141</PlatformToolset>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\" Label=\"Configuration\">\n    <ConfigurationType>DynamicLibrary</ConfigurationType>\n    <UseDebugLibraries>false</UseDebugLibraries>\n    <PlatformToolset>v141</PlatformToolset>\n    <WholeProgramOptimization>true</WholeProgramOptimization>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|x64'\" Label=\"Configuration\">\n    <ConfigurationType>StaticLibrary</ConfigurationType>\n    <UseDebugLibraries>false</UseDebugLibraries>\n    <PlatformToolset>v141</PlatformToolset>\n    <WholeProgramOptimization>true</WholeProgramOptimization>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.props\" />\n  <ImportGroup Label=\"ExtensionSettings\">\n  </ImportGroup>\n  <ImportGroup Label=\"Shared\">\n  </ImportGroup>\n  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|Win32'\" Label=\"PropertySheets\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|Win32'\" Label=\"PropertySheets\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|x64'\" Label=\"PropertySheets\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|x64'\" Label=\"PropertySheets\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <PropertyGroup Label=\"UserMacros\" />\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <TargetName>$(ProjectName)d</TargetName>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|Win32'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <TargetName>$(ProjectName)-$(PlatformToolset)-$(Configuration)</TargetName>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|Win32'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <TargetName>$(ProjectName)-$(PlatformToolset)-$(Configuration)</TargetName>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <TargetName>$(ProjectName)d</TargetName>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|x64'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <TargetName>$(ProjectName)-vc$(PlatformToolsetVersion)-$(Configuration)</TargetName>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|x64'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <TargetName>$(ProjectName)-vc$(PlatformToolsetVersion)-$(Configuration)</TargetName>\n  </PropertyGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\">\n    <ClCompile>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <WarningLevel>Level3</WarningLevel>\n      <Optimization>Disabled</Optimization>\n      <PreprocessorDefinitions>_REENTRANT;_WINDLL;DLLEXPORT;JEMALLOC_DEBUG;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <DisableSpecificWarnings>4090;4146;4267;4334</DisableSpecificWarnings>\n      <ProgramDataBaseFileName>$(OutputPath)$(TargetName).pdb</ProgramDataBaseFileName>\n    </ClCompile>\n    <Link>\n      <SubSystem>Windows</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|Win32'\">\n    <ClCompile>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <WarningLevel>Level3</WarningLevel>\n      <Optimization>Disabled</Optimization>\n      <PreprocessorDefinitions>JEMALLOC_DEBUG;_REENTRANT;JEMALLOC_EXPORT=;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>\n      <DisableSpecificWarnings>4090;4146;4267;4334</DisableSpecificWarnings>\n      <ProgramDataBaseFileName>$(OutputPath)$(TargetName).pdb</ProgramDataBaseFileName>\n    </ClCompile>\n    <Link>\n      <SubSystem>Windows</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\">\n    <ClCompile>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <WarningLevel>Level3</WarningLevel>\n      <Optimization>Disabled</Optimization>\n      <PreprocessorDefinitions>JEMALLOC_NO_PRIVATE_NAMESPACE;_REENTRANT;_WINDLL;DLLEXPORT;JEMALLOC_DEBUG;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <DisableSpecificWarnings>4090;4146;4267;4334</DisableSpecificWarnings>\n      <ProgramDataBaseFileName>$(OutputPath)$(TargetName).pdb</ProgramDataBaseFileName>\n    </ClCompile>\n    <Link>\n      <SubSystem>Windows</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|x64'\">\n    <ClCompile>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <WarningLevel>Level3</WarningLevel>\n      <Optimization>Disabled</Optimization>\n      <PreprocessorDefinitions>JEMALLOC_NO_PRIVATE_NAMESPACE;JEMALLOC_DEBUG;_REENTRANT;JEMALLOC_EXPORT=;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>\n      <DisableSpecificWarnings>4090;4146;4267;4334</DisableSpecificWarnings>\n      <DebugInformationFormat>OldStyle</DebugInformationFormat>\n      <MinimalRebuild>false</MinimalRebuild>\n    </ClCompile>\n    <Link>\n      <SubSystem>Windows</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\">\n    <ClCompile>\n      <WarningLevel>Level3</WarningLevel>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <Optimization>MaxSpeed</Optimization>\n      <FunctionLevelLinking>true</FunctionLevelLinking>\n      <IntrinsicFunctions>true</IntrinsicFunctions>\n      <PreprocessorDefinitions>_REENTRANT;_WINDLL;DLLEXPORT;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <DisableSpecificWarnings>4090;4146;4267;4334</DisableSpecificWarnings>\n      <ProgramDataBaseFileName>$(OutputPath)$(TargetName).pdb</ProgramDataBaseFileName>\n    </ClCompile>\n    <Link>\n      <SubSystem>Windows</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <EnableCOMDATFolding>true</EnableCOMDATFolding>\n      <OptimizeReferences>true</OptimizeReferences>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|Win32'\">\n    <ClCompile>\n      <WarningLevel>Level3</WarningLevel>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <Optimization>MaxSpeed</Optimization>\n      <FunctionLevelLinking>true</FunctionLevelLinking>\n      <IntrinsicFunctions>true</IntrinsicFunctions>\n      <PreprocessorDefinitions>_REENTRANT;JEMALLOC_EXPORT=;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <RuntimeLibrary>MultiThreaded</RuntimeLibrary>\n      <DisableSpecificWarnings>4090;4146;4267;4334</DisableSpecificWarnings>\n      <ProgramDataBaseFileName>$(OutputPath)$(TargetName).pdb</ProgramDataBaseFileName>\n    </ClCompile>\n    <Link>\n      <SubSystem>Windows</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <EnableCOMDATFolding>true</EnableCOMDATFolding>\n      <OptimizeReferences>true</OptimizeReferences>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\">\n    <ClCompile>\n      <WarningLevel>Level3</WarningLevel>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <Optimization>MaxSpeed</Optimization>\n      <FunctionLevelLinking>true</FunctionLevelLinking>\n      <IntrinsicFunctions>true</IntrinsicFunctions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <PreprocessorDefinitions>JEMALLOC_NO_PRIVATE_NAMESPACE;_REENTRANT;_WINDLL;DLLEXPORT;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <DisableSpecificWarnings>4090;4146;4267;4334</DisableSpecificWarnings>\n      <ProgramDataBaseFileName>$(OutputPath)$(TargetName).pdb</ProgramDataBaseFileName>\n    </ClCompile>\n    <Link>\n      <SubSystem>Windows</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <EnableCOMDATFolding>true</EnableCOMDATFolding>\n      <OptimizeReferences>true</OptimizeReferences>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|x64'\">\n    <ClCompile>\n      <WarningLevel>Level3</WarningLevel>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <Optimization>MaxSpeed</Optimization>\n      <FunctionLevelLinking>true</FunctionLevelLinking>\n      <IntrinsicFunctions>true</IntrinsicFunctions>\n      <PreprocessorDefinitions>JEMALLOC_NO_PRIVATE_NAMESPACE;_REENTRANT;JEMALLOC_EXPORT=;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <RuntimeLibrary>MultiThreaded</RuntimeLibrary>\n      <DisableSpecificWarnings>4090;4146;4267;4334</DisableSpecificWarnings>\n      <DebugInformationFormat>OldStyle</DebugInformationFormat>\n    </ClCompile>\n    <Link>\n      <SubSystem>Windows</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <EnableCOMDATFolding>true</EnableCOMDATFolding>\n      <OptimizeReferences>true</OptimizeReferences>\n    </Link>\n  </ItemDefinitionGroup>\n  <Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.targets\" />\n  <ImportGroup Label=\"ExtensionTargets\">\n  </ImportGroup>\n</Project>\n"
  },
  {
    "path": "deps/jemalloc/msvc/projects/vc2017/jemalloc/jemalloc.vcxproj.filters",
    "content": "﻿<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<Project ToolsVersion=\"4.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n  <ItemGroup>\n    <Filter Include=\"Source Files\">\n      <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>\n      <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>\n    </Filter>\n  </ItemGroup>\n  <ItemGroup>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\arena.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\background_thread.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\base.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\bitmap.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\ckh.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\ctl.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\extent.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\extent_dss.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\extent_mmap.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\hash.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\hook.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\jemalloc.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\large.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\malloc_io.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\mutex.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\mutex_pool.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\nstime.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\pages.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\prng.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\prof.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\rtree.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\sc.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\stats.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\sz.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\tcache.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\ticker.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\tsd.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\witness.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\log.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\bin.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\div.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\test_hooks.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\safety_check.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n  </ItemGroup>\n</Project>\n"
  },
  {
    "path": "deps/jemalloc/msvc/projects/vc2017/test_threads/test_threads.vcxproj",
    "content": "﻿<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<Project DefaultTargets=\"Build\" ToolsVersion=\"15.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n  <ItemGroup Label=\"ProjectConfigurations\">\n    <ProjectConfiguration Include=\"Debug-static|Win32\">\n      <Configuration>Debug-static</Configuration>\n      <Platform>Win32</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Debug-static|x64\">\n      <Configuration>Debug-static</Configuration>\n      <Platform>x64</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Debug|Win32\">\n      <Configuration>Debug</Configuration>\n      <Platform>Win32</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Release-static|Win32\">\n      <Configuration>Release-static</Configuration>\n      <Platform>Win32</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Release-static|x64\">\n      <Configuration>Release-static</Configuration>\n      <Platform>x64</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Release|Win32\">\n      <Configuration>Release</Configuration>\n      <Platform>Win32</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Debug|x64\">\n      <Configuration>Debug</Configuration>\n      <Platform>x64</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Release|x64\">\n      <Configuration>Release</Configuration>\n      <Platform>x64</Platform>\n    </ProjectConfiguration>\n  </ItemGroup>\n  <PropertyGroup Label=\"Globals\">\n    <ProjectGuid>{09028CFD-4EB7-491D-869C-0708DB97ED44}</ProjectGuid>\n    <Keyword>Win32Proj</Keyword>\n    <RootNamespace>test_threads</RootNamespace>\n  </PropertyGroup>\n  <Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\" Label=\"Configuration\">\n    <ConfigurationType>Application</ConfigurationType>\n    <UseDebugLibraries>true</UseDebugLibraries>\n    <PlatformToolset>v141</PlatformToolset>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|Win32'\" Label=\"Configuration\">\n    <ConfigurationType>Application</ConfigurationType>\n    <UseDebugLibraries>true</UseDebugLibraries>\n    <PlatformToolset>v141</PlatformToolset>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\" Label=\"Configuration\">\n    <ConfigurationType>Application</ConfigurationType>\n    <UseDebugLibraries>false</UseDebugLibraries>\n    <PlatformToolset>v141</PlatformToolset>\n    <WholeProgramOptimization>true</WholeProgramOptimization>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|Win32'\" Label=\"Configuration\">\n    <ConfigurationType>Application</ConfigurationType>\n    <UseDebugLibraries>false</UseDebugLibraries>\n    <PlatformToolset>v141</PlatformToolset>\n    <WholeProgramOptimization>true</WholeProgramOptimization>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\" Label=\"Configuration\">\n    <ConfigurationType>Application</ConfigurationType>\n    <UseDebugLibraries>true</UseDebugLibraries>\n    <PlatformToolset>v141</PlatformToolset>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|x64'\" Label=\"Configuration\">\n    <ConfigurationType>Application</ConfigurationType>\n    <UseDebugLibraries>true</UseDebugLibraries>\n    <PlatformToolset>v141</PlatformToolset>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\" Label=\"Configuration\">\n    <ConfigurationType>Application</ConfigurationType>\n    <UseDebugLibraries>false</UseDebugLibraries>\n    <PlatformToolset>v141</PlatformToolset>\n    <WholeProgramOptimization>true</WholeProgramOptimization>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|x64'\" Label=\"Configuration\">\n    <ConfigurationType>Application</ConfigurationType>\n    <UseDebugLibraries>false</UseDebugLibraries>\n    <PlatformToolset>v141</PlatformToolset>\n    <WholeProgramOptimization>true</WholeProgramOptimization>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.props\" />\n  <ImportGroup Label=\"ExtensionSettings\">\n  </ImportGroup>\n  <ImportGroup Label=\"Shared\">\n  </ImportGroup>\n  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|Win32'\" Label=\"PropertySheets\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|Win32'\" Label=\"PropertySheets\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|x64'\" Label=\"PropertySheets\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|x64'\" Label=\"PropertySheets\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <PropertyGroup Label=\"UserMacros\" />\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <LinkIncremental>true</LinkIncremental>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|Win32'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <LinkIncremental>true</LinkIncremental>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\">\n    <LinkIncremental>true</LinkIncremental>\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|x64'\">\n    <LinkIncremental>true</LinkIncremental>\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <LinkIncremental>false</LinkIncremental>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|Win32'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <LinkIncremental>false</LinkIncremental>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <LinkIncremental>false</LinkIncremental>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|x64'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <LinkIncremental>false</LinkIncremental>\n  </PropertyGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\">\n    <ClCompile>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <WarningLevel>Level3</WarningLevel>\n      <Optimization>Disabled</Optimization>\n      <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\test\\include;..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n    </ClCompile>\n    <Link>\n      <SubSystem>Console</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <AdditionalLibraryDirectories>$(SolutionDir)$(Platform)\\$(Configuration)</AdditionalLibraryDirectories>\n      <AdditionalDependencies>jemallocd.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|Win32'\">\n    <ClCompile>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <WarningLevel>Level3</WarningLevel>\n      <Optimization>Disabled</Optimization>\n      <PreprocessorDefinitions>JEMALLOC_EXPORT=;JEMALLOC_STATIC;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\test\\include;..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>\n    </ClCompile>\n    <Link>\n      <SubSystem>Console</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <AdditionalLibraryDirectories>$(SolutionDir)$(Platform)\\$(Configuration)</AdditionalLibraryDirectories>\n      <AdditionalDependencies>jemalloc-$(PlatformToolset)-$(Configuration).lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\">\n    <ClCompile>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <WarningLevel>Level3</WarningLevel>\n      <Optimization>Disabled</Optimization>\n      <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\test\\include;..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n    </ClCompile>\n    <Link>\n      <SubSystem>Console</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <AdditionalDependencies>jemallocd.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>\n      <AdditionalLibraryDirectories>$(SolutionDir)$(Platform)\\$(Configuration)</AdditionalLibraryDirectories>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|x64'\">\n    <ClCompile>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <WarningLevel>Level3</WarningLevel>\n      <Optimization>Disabled</Optimization>\n      <PreprocessorDefinitions>JEMALLOC_EXPORT=;JEMALLOC_STATIC;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\test\\include;..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>\n    </ClCompile>\n    <Link>\n      <SubSystem>Console</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <AdditionalDependencies>jemalloc-vc$(PlatformToolsetVersion)-$(Configuration).lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>\n      <AdditionalLibraryDirectories>$(SolutionDir)$(Platform)\\$(Configuration)</AdditionalLibraryDirectories>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\">\n    <ClCompile>\n      <WarningLevel>Level3</WarningLevel>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <Optimization>MaxSpeed</Optimization>\n      <FunctionLevelLinking>true</FunctionLevelLinking>\n      <IntrinsicFunctions>true</IntrinsicFunctions>\n      <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\test\\include;..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n    </ClCompile>\n    <Link>\n      <SubSystem>Console</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <EnableCOMDATFolding>true</EnableCOMDATFolding>\n      <OptimizeReferences>true</OptimizeReferences>\n      <AdditionalLibraryDirectories>$(SolutionDir)$(Platform)\\$(Configuration)</AdditionalLibraryDirectories>\n      <AdditionalDependencies>jemalloc.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|Win32'\">\n    <ClCompile>\n      <WarningLevel>Level3</WarningLevel>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <Optimization>MaxSpeed</Optimization>\n      <FunctionLevelLinking>true</FunctionLevelLinking>\n      <IntrinsicFunctions>true</IntrinsicFunctions>\n      <PreprocessorDefinitions>JEMALLOC_EXPORT=;JEMALLOC_STATIC;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\test\\include;..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <RuntimeLibrary>MultiThreaded</RuntimeLibrary>\n    </ClCompile>\n    <Link>\n      <SubSystem>Console</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <EnableCOMDATFolding>true</EnableCOMDATFolding>\n      <OptimizeReferences>true</OptimizeReferences>\n      <AdditionalLibraryDirectories>$(SolutionDir)$(Platform)\\$(Configuration)</AdditionalLibraryDirectories>\n      <AdditionalDependencies>jemalloc-$(PlatformToolset)-$(Configuration).lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\">\n    <ClCompile>\n      <WarningLevel>Level3</WarningLevel>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <Optimization>MaxSpeed</Optimization>\n      <FunctionLevelLinking>true</FunctionLevelLinking>\n      <IntrinsicFunctions>true</IntrinsicFunctions>\n      <PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\test\\include;..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n    </ClCompile>\n    <Link>\n      <SubSystem>Console</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <EnableCOMDATFolding>true</EnableCOMDATFolding>\n      <OptimizeReferences>true</OptimizeReferences>\n      <AdditionalLibraryDirectories>$(SolutionDir)$(Platform)\\$(Configuration)</AdditionalLibraryDirectories>\n      <AdditionalDependencies>jemalloc.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|x64'\">\n    <ClCompile>\n      <WarningLevel>Level3</WarningLevel>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <Optimization>MaxSpeed</Optimization>\n      <FunctionLevelLinking>true</FunctionLevelLinking>\n      <IntrinsicFunctions>true</IntrinsicFunctions>\n      <PreprocessorDefinitions>JEMALLOC_EXPORT=;JEMALLOC_STATIC;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\test\\include;..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <RuntimeLibrary>MultiThreaded</RuntimeLibrary>\n    </ClCompile>\n    <Link>\n      <SubSystem>Console</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <EnableCOMDATFolding>true</EnableCOMDATFolding>\n      <OptimizeReferences>true</OptimizeReferences>\n      <AdditionalLibraryDirectories>$(SolutionDir)$(Platform)\\$(Configuration)</AdditionalLibraryDirectories>\n      <AdditionalDependencies>jemalloc-vc$(PlatformToolsetVersion)-$(Configuration).lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemGroup>\n    <ClCompile Include=\"..\\..\\..\\test_threads\\test_threads.cpp\" />\n    <ClCompile Include=\"..\\..\\..\\test_threads\\test_threads_main.cpp\" />\n  </ItemGroup>\n  <ItemGroup>\n    <ProjectReference Include=\"..\\jemalloc\\jemalloc.vcxproj\">\n      <Project>{8d6bb292-9e1c-413d-9f98-4864bdc1514a}</Project>\n    </ProjectReference>\n  </ItemGroup>\n  <ItemGroup>\n    <ClInclude Include=\"..\\..\\..\\test_threads\\test_threads.h\" />\n  </ItemGroup>\n  <Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.targets\" />\n  <ImportGroup Label=\"ExtensionTargets\">\n  </ImportGroup>\n</Project>"
  },
  {
    "path": "deps/jemalloc/msvc/projects/vc2017/test_threads/test_threads.vcxproj.filters",
    "content": "﻿<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<Project ToolsVersion=\"4.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n  <ItemGroup>\n    <Filter Include=\"Source Files\">\n      <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>\n      <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>\n    </Filter>\n    <Filter Include=\"Header Files\">\n      <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>\n      <Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>\n    </Filter>\n  </ItemGroup>\n  <ItemGroup>\n    <ClCompile Include=\"..\\..\\..\\test_threads\\test_threads.cpp\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\test_threads\\test_threads_main.cpp\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n  </ItemGroup>\n  <ItemGroup>\n    <ClInclude Include=\"..\\..\\..\\test_threads\\test_threads.h\">\n      <Filter>Header Files</Filter>\n    </ClInclude>\n  </ItemGroup>\n</Project>"
  },
  {
    "path": "deps/jemalloc/msvc/test_threads/test_threads.cpp",
    "content": "// jemalloc C++ threaded test\n// Author: Rustam Abdullaev\n// Public Domain\n\n#include <atomic>\n#include <functional>\n#include <future>\n#include <random>\n#include <thread>\n#include <vector>\n#include <stdio.h>\n#include <jemalloc/jemalloc.h>\n\nusing std::vector;\nusing std::thread;\nusing std::uniform_int_distribution;\nusing std::minstd_rand;\n\nint test_threads() {\n  je_malloc_conf = \"narenas:3\";\n  int narenas = 0;\n  size_t sz = sizeof(narenas);\n  je_mallctl(\"opt.narenas\", (void *)&narenas, &sz, NULL, 0);\n  if (narenas != 3) {\n    printf(\"Error: unexpected number of arenas: %d\\n\", narenas);\n    return 1;\n  }\n  static const int sizes[] = { 7, 16, 32, 60, 91, 100, 120, 144, 169, 199, 255, 400, 670, 900, 917, 1025, 3333, 5190, 13131, 49192, 99999, 123123, 255265, 2333111 };\n  static const int numSizes = (int)(sizeof(sizes) / sizeof(sizes[0]));\n  vector<thread> workers;\n  static const int numThreads = narenas + 1, numAllocsMax = 25, numIter1 = 50, numIter2 = 50;\n  je_malloc_stats_print(NULL, NULL, NULL);\n  size_t allocated1;\n  size_t sz1 = sizeof(allocated1);\n  je_mallctl(\"stats.active\", (void *)&allocated1, &sz1, NULL, 0);\n  printf(\"\\nPress Enter to start threads...\\n\");\n  getchar();\n  printf(\"Starting %d threads x %d x %d iterations...\\n\", numThreads, numIter1, numIter2);\n  for (int i = 0; i < numThreads; i++) {\n    workers.emplace_back([tid=i]() {\n      uniform_int_distribution<int> sizeDist(0, numSizes - 1);\n      minstd_rand rnd(tid * 17);\n      uint8_t* ptrs[numAllocsMax];\n      int ptrsz[numAllocsMax];\n      for (int i = 0; i < numIter1; ++i) {\n        thread t([&]() {\n          for (int i = 0; i < numIter2; ++i) {\n            const int numAllocs = numAllocsMax - sizeDist(rnd);\n            for (int j = 0; j < numAllocs; j += 64) {\n              const int x = sizeDist(rnd);\n              const int sz = sizes[x];\n              ptrsz[j] = sz;\n              ptrs[j] = (uint8_t*)je_malloc(sz);\n              if (!ptrs[j]) {\n                printf(\"Unable to allocate %d bytes in thread %d, iter %d, alloc %d. %d\\n\", sz, tid, i, j, x);\n                exit(1);\n              }\n              for (int k = 0; k < sz; k++)\n                ptrs[j][k] = tid + k;\n            }\n            for (int j = 0; j < numAllocs; j += 64) {\n              for (int k = 0, sz = ptrsz[j]; k < sz; k++)\n                if (ptrs[j][k] != (uint8_t)(tid + k)) {\n                  printf(\"Memory error in thread %d, iter %d, alloc %d @ %d : %02X!=%02X\\n\", tid, i, j, k, ptrs[j][k], (uint8_t)(tid + k));\n                  exit(1);\n                }\n              je_free(ptrs[j]);\n            }\n          }\n        });\n        t.join();\n      }\n    });\n  }\n  for (thread& t : workers) {\n    t.join();\n  }\n  je_malloc_stats_print(NULL, NULL, NULL);\n  size_t allocated2;\n  je_mallctl(\"stats.active\", (void *)&allocated2, &sz1, NULL, 0);\n  size_t leaked = allocated2 - allocated1;\n  printf(\"\\nDone. Leaked: %zd bytes\\n\", leaked);\n  bool failed = leaked > 65536; // in case C++ runtime allocated something (e.g. iostream locale or facet)\n  printf(\"\\nTest %s!\\n\", (failed ? \"FAILED\" : \"successful\"));\n  printf(\"\\nPress Enter to continue...\\n\");\n  getchar();\n  return failed ? 1 : 0;\n}\n"
  },
  {
    "path": "deps/jemalloc/msvc/test_threads/test_threads.h",
    "content": "#pragma once\n\nint test_threads();\n"
  },
  {
    "path": "deps/jemalloc/msvc/test_threads/test_threads_main.cpp",
    "content": "#include \"test_threads.h\"\n#include <future>\n#include <functional>\n#include <chrono>\n\nusing namespace std::chrono_literals;\n\nint main(int argc, char** argv) {\n  int rc = test_threads();\n  return rc;\n}\n"
  },
  {
    "path": "deps/jemalloc/run_tests.sh",
    "content": "$(dirname \"$)\")/scripts/gen_run_tests.py | bash\n"
  },
  {
    "path": "deps/jemalloc/scripts/gen_run_tests.py",
    "content": "#!/usr/bin/env python\n\nimport sys\nfrom itertools import combinations\nfrom os import uname\nfrom multiprocessing import cpu_count\nfrom subprocess import call\n\n# Later, we want to test extended vaddr support.  Apparently, the \"real\" way of\n# checking this is flaky on OS X.\nbits_64 = sys.maxsize > 2**32\n\nnparallel = cpu_count() * 2\n\nuname = uname()[0]\n\nif \"BSD\" in uname:\n    make_cmd = 'gmake'\nelse:\n    make_cmd = 'make'\n\ndef powerset(items):\n    result = []\n    for i in xrange(len(items) + 1):\n        result += combinations(items, i)\n    return result\n\npossible_compilers = []\nfor cc, cxx in (['gcc', 'g++'], ['clang', 'clang++']):\n    try:\n        cmd_ret = call([cc, \"-v\"])\n        if cmd_ret == 0:\n            possible_compilers.append((cc, cxx))\n    except:\n        pass\npossible_compiler_opts = [\n    '-m32',\n]\npossible_config_opts = [\n    '--enable-debug',\n    '--enable-prof',\n    '--disable-stats',\n    '--enable-opt-safety-checks',\n]\nif bits_64:\n    possible_config_opts.append('--with-lg-vaddr=56')\n\npossible_malloc_conf_opts = [\n    'tcache:false',\n    'dss:primary',\n    'percpu_arena:percpu',\n    'background_thread:true',\n]\n\nprint 'set -e'\nprint 'if [ -f Makefile ] ; then %(make_cmd)s relclean ; fi' % {'make_cmd': make_cmd}\nprint 'autoconf'\nprint 'rm -rf run_tests.out'\nprint 'mkdir run_tests.out'\nprint 'cd run_tests.out'\n\nind = 0\nfor cc, cxx in possible_compilers:\n    for compiler_opts in powerset(possible_compiler_opts):\n        for config_opts in powerset(possible_config_opts):\n            for malloc_conf_opts in powerset(possible_malloc_conf_opts):\n                if cc is 'clang' \\\n                  and '-m32' in possible_compiler_opts \\\n                  and '--enable-prof' in config_opts:\n                    continue\n                config_line = (\n                    'EXTRA_CFLAGS=-Werror EXTRA_CXXFLAGS=-Werror '\n                    + 'CC=\"{} {}\" '.format(cc, \" \".join(compiler_opts))\n                    + 'CXX=\"{} {}\" '.format(cxx, \" \".join(compiler_opts))\n                    + '../../configure '\n                    + \" \".join(config_opts) + (' --with-malloc-conf=' +\n                    \",\".join(malloc_conf_opts) if len(malloc_conf_opts) > 0\n                    else '')\n                )\n\n                # We don't want to test large vaddr spaces in 32-bit mode.\n\t\tif ('-m32' in compiler_opts and '--with-lg-vaddr=56' in\n                  config_opts):\n\t\t    continue\n\n                # Per CPU arenas are only supported on Linux.\n                linux_supported = ('percpu_arena:percpu' in malloc_conf_opts \\\n                  or 'background_thread:true' in malloc_conf_opts)\n                # Heap profiling and dss are not supported on OS X.\n                darwin_unsupported = ('--enable-prof' in config_opts or \\\n                  'dss:primary' in malloc_conf_opts)\n                if (uname == 'Linux' and linux_supported) \\\n                  or (not linux_supported and (uname != 'Darwin' or \\\n                  not darwin_unsupported)):\n                    print \"\"\"cat <<EOF > run_test_%(ind)d.sh\n#!/bin/sh\n\nset -e\n\nabort() {\n    echo \"==> Error\" >> run_test.log\n    echo \"Error; see run_tests.out/run_test_%(ind)d.out/run_test.log\"\n    exit 255 # Special exit code tells xargs to terminate.\n}\n\n# Environment variables are not supported.\nrun_cmd() {\n    echo \"==> \\$@\" >> run_test.log\n    \\$@ >> run_test.log 2>&1 || abort\n}\n\necho \"=> run_test_%(ind)d: %(config_line)s\"\nmkdir run_test_%(ind)d.out\ncd run_test_%(ind)d.out\n\necho \"==> %(config_line)s\" >> run_test.log\n%(config_line)s >> run_test.log 2>&1 || abort\n\nrun_cmd %(make_cmd)s all tests\nrun_cmd %(make_cmd)s check\nrun_cmd %(make_cmd)s distclean\nEOF\nchmod 755 run_test_%(ind)d.sh\"\"\" % {'ind': ind, 'config_line': config_line, 'make_cmd': make_cmd}\n                    ind += 1\n\nprint 'for i in `seq 0 %(last_ind)d` ; do echo run_test_${i}.sh ; done | xargs -P %(nparallel)d -n 1 sh' % {'last_ind': ind-1, 'nparallel': nparallel}\n"
  },
  {
    "path": "deps/jemalloc/scripts/gen_travis.py",
    "content": "#!/usr/bin/env python\n\nfrom itertools import combinations\n\ntravis_template = \"\"\"\\\nlanguage: generic\ndist: precise\n\nmatrix:\n  include:\n%s\n\nbefore_script:\n  - autoconf\n  - scripts/gen_travis.py > travis_script && diff .travis.yml travis_script\n  - ./configure ${COMPILER_FLAGS:+ \\\n      CC=\"$CC $COMPILER_FLAGS\" \\\n      CXX=\"$CXX $COMPILER_FLAGS\" } \\\n      $CONFIGURE_FLAGS\n  - make -j3\n  - make -j3 tests\n\nscript:\n  - make check\n\"\"\"\n\n# The 'default' configuration is gcc, on linux, with no compiler or configure\n# flags.  We also test with clang, -m32, --enable-debug, --enable-prof,\n# --disable-stats, and --with-malloc-conf=tcache:false.  To avoid abusing\n# travis though, we don't test all 2**7 = 128 possible combinations of these;\n# instead, we only test combinations of up to 2 'unusual' settings, under the\n# hope that bugs involving interactions of such settings are rare.\n# Things at once, for C(7, 0) + C(7, 1) + C(7, 2) = 29\nMAX_UNUSUAL_OPTIONS = 2\n\nos_default = 'linux'\nos_unusual = 'osx'\n\ncompilers_default = 'CC=gcc CXX=g++'\ncompilers_unusual = 'CC=clang CXX=clang++'\n\ncompiler_flag_unusuals = ['-m32']\n\nconfigure_flag_unusuals = [\n    '--enable-debug',\n    '--enable-prof',\n    '--disable-stats',\n    '--disable-libdl',\n    '--enable-opt-safety-checks',\n]\n\nmalloc_conf_unusuals = [\n    'tcache:false',\n    'dss:primary',\n    'percpu_arena:percpu',\n    'background_thread:true',\n]\n\nall_unusuals = (\n    [os_unusual] + [compilers_unusual] + compiler_flag_unusuals\n    + configure_flag_unusuals + malloc_conf_unusuals\n)\n\nunusual_combinations_to_test = []\nfor i in xrange(MAX_UNUSUAL_OPTIONS + 1):\n    unusual_combinations_to_test += combinations(all_unusuals, i)\n\ngcc_multilib_set = False\n# Formats a job from a combination of flags\ndef format_job(combination):\n    global gcc_multilib_set\n\n    os = os_unusual if os_unusual in combination else os_default\n    compilers = compilers_unusual if compilers_unusual in combination else compilers_default\n\n    compiler_flags = [x for x in combination if x in compiler_flag_unusuals]\n    configure_flags = [x for x in combination if x in configure_flag_unusuals]\n    malloc_conf = [x for x in combination if x in malloc_conf_unusuals]\n\n    # Filter out unsupported configurations on OS X.\n    if os == 'osx' and ('dss:primary' in malloc_conf or \\\n      'percpu_arena:percpu' in malloc_conf or 'background_thread:true' \\\n      in malloc_conf):\n        return \"\"\n    if len(malloc_conf) > 0:\n        configure_flags.append('--with-malloc-conf=' + \",\".join(malloc_conf))\n\n    # Filter out an unsupported configuration - heap profiling on OS X.\n    if os == 'osx' and '--enable-prof' in configure_flags:\n        return \"\"\n\n    # We get some spurious errors when -Warray-bounds is enabled.\n    env_string = ('{} COMPILER_FLAGS=\"{}\" CONFIGURE_FLAGS=\"{}\" '\n\t'EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"').format(\n        compilers, \" \".join(compiler_flags), \" \".join(configure_flags))\n\n    job = \"\"\n    job += '    - os: %s\\n' % os\n    job += '      env: %s\\n' % env_string\n    if '-m32' in combination and os == 'linux':\n        job += '      addons:'\n        if gcc_multilib_set:\n            job += ' *gcc_multilib\\n'\n        else:\n            job += ' &gcc_multilib\\n'\n            job += '        apt:\\n'\n            job += '          packages:\\n'\n            job += '            - gcc-multilib\\n'\n            gcc_multilib_set = True\n    return job\n\ninclude_rows = \"\"\nfor combination in unusual_combinations_to_test:\n    include_rows += format_job(combination)\n\n# Development build\ninclude_rows += '''\\\n    # Development build\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-debug --disable-cache-oblivious --enable-stats --enable-log --enable-prof\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n'''\n\n# Enable-expermental-smallocx\ninclude_rows += '''\\\n    # --enable-expermental-smallocx:\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-debug --enable-experimental-smallocx --enable-stats --enable-prof\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n'''\n\n# Valgrind build bots\ninclude_rows += '''\n    # Valgrind\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\" JEMALLOC_TEST_PREFIX=\"valgrind\"\n      addons:\n        apt:\n          packages:\n            - valgrind\n'''\n\n# To enable valgrind on macosx add:\n#\n#  - os: osx\n#    env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\" JEMALLOC_TEST_PREFIX=\"valgrind\"\n#    install: brew install valgrind\n#\n# It currently fails due to: https://github.com/jemalloc/jemalloc/issues/1274\n\nprint travis_template % include_rows\n"
  },
  {
    "path": "deps/jemalloc/src/arena.c",
    "content": "#define JEMALLOC_ARENA_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/div.h\"\n#include \"jemalloc/internal/extent_dss.h\"\n#include \"jemalloc/internal/extent_mmap.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/rtree.h\"\n#include \"jemalloc/internal/safety_check.h\"\n#include \"jemalloc/internal/util.h\"\n\nJEMALLOC_DIAGNOSTIC_DISABLE_SPURIOUS\n\n/******************************************************************************/\n/* Data. */\n\n/*\n * Define names for both unininitialized and initialized phases, so that\n * options and mallctl processing are straightforward.\n */\nconst char *percpu_arena_mode_names[] = {\n\t\"percpu\",\n\t\"phycpu\",\n\t\"disabled\",\n\t\"percpu\",\n\t\"phycpu\"\n};\npercpu_arena_mode_t opt_percpu_arena = PERCPU_ARENA_DEFAULT;\n\nssize_t opt_dirty_decay_ms = DIRTY_DECAY_MS_DEFAULT;\nssize_t opt_muzzy_decay_ms = MUZZY_DECAY_MS_DEFAULT;\n\nstatic atomic_zd_t dirty_decay_ms_default;\nstatic atomic_zd_t muzzy_decay_ms_default;\n\nconst uint64_t h_steps[SMOOTHSTEP_NSTEPS] = {\n#define STEP(step, h, x, y)\t\t\t\\\n\t\th,\n\t\tSMOOTHSTEP\n#undef STEP\n};\n\nstatic div_info_t arena_binind_div_info[SC_NBINS];\n\nsize_t opt_oversize_threshold = OVERSIZE_THRESHOLD_DEFAULT;\nsize_t oversize_threshold = OVERSIZE_THRESHOLD_DEFAULT;\nstatic unsigned huge_arena_ind;\n\n/******************************************************************************/\n/*\n * Function prototypes for static functions that are referenced prior to\n * definition.\n */\n\nstatic void arena_decay_to_limit(tsdn_t *tsdn, arena_t *arena,\n    arena_decay_t *decay, extents_t *extents, bool all, size_t npages_limit,\n    size_t npages_decay_max, bool is_background_thread);\nstatic bool arena_decay_dirty(tsdn_t *tsdn, arena_t *arena,\n    bool is_background_thread, bool all);\nstatic void arena_dalloc_bin_slab(tsdn_t *tsdn, arena_t *arena, extent_t *slab,\n    bin_t *bin);\nstatic void arena_bin_lower_slab(tsdn_t *tsdn, arena_t *arena, extent_t *slab,\n    bin_t *bin);\n\n/******************************************************************************/\n\nvoid\narena_basic_stats_merge(tsdn_t *tsdn, arena_t *arena, unsigned *nthreads,\n    const char **dss, ssize_t *dirty_decay_ms, ssize_t *muzzy_decay_ms,\n    size_t *nactive, size_t *ndirty, size_t *nmuzzy) {\n\t*nthreads += arena_nthreads_get(arena, false);\n\t*dss = dss_prec_names[arena_dss_prec_get(arena)];\n\t*dirty_decay_ms = arena_dirty_decay_ms_get(arena);\n\t*muzzy_decay_ms = arena_muzzy_decay_ms_get(arena);\n\t*nactive += atomic_load_zu(&arena->nactive, ATOMIC_RELAXED);\n\t*ndirty += extents_npages_get(&arena->extents_dirty);\n\t*nmuzzy += extents_npages_get(&arena->extents_muzzy);\n}\n\nvoid\narena_stats_merge(tsdn_t *tsdn, arena_t *arena, unsigned *nthreads,\n    const char **dss, ssize_t *dirty_decay_ms, ssize_t *muzzy_decay_ms,\n    size_t *nactive, size_t *ndirty, size_t *nmuzzy, arena_stats_t *astats,\n    bin_stats_t *bstats, arena_stats_large_t *lstats,\n    arena_stats_extents_t *estats) {\n\tcassert(config_stats);\n\n\tarena_basic_stats_merge(tsdn, arena, nthreads, dss, dirty_decay_ms,\n\t    muzzy_decay_ms, nactive, ndirty, nmuzzy);\n\n\tsize_t base_allocated, base_resident, base_mapped, metadata_thp;\n\tbase_stats_get(tsdn, arena->base, &base_allocated, &base_resident,\n\t    &base_mapped, &metadata_thp);\n\n\tarena_stats_lock(tsdn, &arena->stats);\n\n\tarena_stats_accum_zu(&astats->mapped, base_mapped\n\t    + arena_stats_read_zu(tsdn, &arena->stats, &arena->stats.mapped));\n\tarena_stats_accum_zu(&astats->retained,\n\t    extents_npages_get(&arena->extents_retained) << LG_PAGE);\n\n\tatomic_store_zu(&astats->extent_avail,\n\t    atomic_load_zu(&arena->extent_avail_cnt, ATOMIC_RELAXED),\n\t    ATOMIC_RELAXED);\n\n\tarena_stats_accum_u64(&astats->decay_dirty.npurge,\n\t    arena_stats_read_u64(tsdn, &arena->stats,\n\t    &arena->stats.decay_dirty.npurge));\n\tarena_stats_accum_u64(&astats->decay_dirty.nmadvise,\n\t    arena_stats_read_u64(tsdn, &arena->stats,\n\t    &arena->stats.decay_dirty.nmadvise));\n\tarena_stats_accum_u64(&astats->decay_dirty.purged,\n\t    arena_stats_read_u64(tsdn, &arena->stats,\n\t    &arena->stats.decay_dirty.purged));\n\n\tarena_stats_accum_u64(&astats->decay_muzzy.npurge,\n\t    arena_stats_read_u64(tsdn, &arena->stats,\n\t    &arena->stats.decay_muzzy.npurge));\n\tarena_stats_accum_u64(&astats->decay_muzzy.nmadvise,\n\t    arena_stats_read_u64(tsdn, &arena->stats,\n\t    &arena->stats.decay_muzzy.nmadvise));\n\tarena_stats_accum_u64(&astats->decay_muzzy.purged,\n\t    arena_stats_read_u64(tsdn, &arena->stats,\n\t    &arena->stats.decay_muzzy.purged));\n\n\tarena_stats_accum_zu(&astats->base, base_allocated);\n\tarena_stats_accum_zu(&astats->internal, arena_internal_get(arena));\n\tarena_stats_accum_zu(&astats->metadata_thp, metadata_thp);\n\tarena_stats_accum_zu(&astats->resident, base_resident +\n\t    (((atomic_load_zu(&arena->nactive, ATOMIC_RELAXED) +\n\t    extents_npages_get(&arena->extents_dirty) +\n\t    extents_npages_get(&arena->extents_muzzy)) << LG_PAGE)));\n\tarena_stats_accum_zu(&astats->abandoned_vm, atomic_load_zu(\n\t    &arena->stats.abandoned_vm, ATOMIC_RELAXED));\n\n\tfor (szind_t i = 0; i < SC_NSIZES - SC_NBINS; i++) {\n\t\tuint64_t nmalloc = arena_stats_read_u64(tsdn, &arena->stats,\n\t\t    &arena->stats.lstats[i].nmalloc);\n\t\tarena_stats_accum_u64(&lstats[i].nmalloc, nmalloc);\n\t\tarena_stats_accum_u64(&astats->nmalloc_large, nmalloc);\n\n\t\tuint64_t ndalloc = arena_stats_read_u64(tsdn, &arena->stats,\n\t\t    &arena->stats.lstats[i].ndalloc);\n\t\tarena_stats_accum_u64(&lstats[i].ndalloc, ndalloc);\n\t\tarena_stats_accum_u64(&astats->ndalloc_large, ndalloc);\n\n\t\tuint64_t nrequests = arena_stats_read_u64(tsdn, &arena->stats,\n\t\t    &arena->stats.lstats[i].nrequests);\n\t\tarena_stats_accum_u64(&lstats[i].nrequests,\n\t\t    nmalloc + nrequests);\n\t\tarena_stats_accum_u64(&astats->nrequests_large,\n\t\t    nmalloc + nrequests);\n\n\t\t/* nfill == nmalloc for large currently. */\n\t\tarena_stats_accum_u64(&lstats[i].nfills, nmalloc);\n\t\tarena_stats_accum_u64(&astats->nfills_large, nmalloc);\n\n\t\tuint64_t nflush = arena_stats_read_u64(tsdn, &arena->stats,\n\t\t    &arena->stats.lstats[i].nflushes);\n\t\tarena_stats_accum_u64(&lstats[i].nflushes, nflush);\n\t\tarena_stats_accum_u64(&astats->nflushes_large, nflush);\n\n\t\tassert(nmalloc >= ndalloc);\n\t\tassert(nmalloc - ndalloc <= SIZE_T_MAX);\n\t\tsize_t curlextents = (size_t)(nmalloc - ndalloc);\n\t\tlstats[i].curlextents += curlextents;\n\t\tarena_stats_accum_zu(&astats->allocated_large,\n\t\t    curlextents * sz_index2size(SC_NBINS + i));\n\t}\n\n\tfor (pszind_t i = 0; i < SC_NPSIZES; i++) {\n\t\tsize_t dirty, muzzy, retained, dirty_bytes, muzzy_bytes,\n\t\t    retained_bytes;\n\t\tdirty = extents_nextents_get(&arena->extents_dirty, i);\n\t\tmuzzy = extents_nextents_get(&arena->extents_muzzy, i);\n\t\tretained = extents_nextents_get(&arena->extents_retained, i);\n\t\tdirty_bytes = extents_nbytes_get(&arena->extents_dirty, i);\n\t\tmuzzy_bytes = extents_nbytes_get(&arena->extents_muzzy, i);\n\t\tretained_bytes =\n\t\t    extents_nbytes_get(&arena->extents_retained, i);\n\n\t\tatomic_store_zu(&estats[i].ndirty, dirty, ATOMIC_RELAXED);\n\t\tatomic_store_zu(&estats[i].nmuzzy, muzzy, ATOMIC_RELAXED);\n\t\tatomic_store_zu(&estats[i].nretained, retained, ATOMIC_RELAXED);\n\t\tatomic_store_zu(&estats[i].dirty_bytes, dirty_bytes,\n\t\t    ATOMIC_RELAXED);\n\t\tatomic_store_zu(&estats[i].muzzy_bytes, muzzy_bytes,\n\t\t    ATOMIC_RELAXED);\n\t\tatomic_store_zu(&estats[i].retained_bytes, retained_bytes,\n\t\t    ATOMIC_RELAXED);\n\t}\n\n\tarena_stats_unlock(tsdn, &arena->stats);\n\n\t/* tcache_bytes counts currently cached bytes. */\n\tatomic_store_zu(&astats->tcache_bytes, 0, ATOMIC_RELAXED);\n\tmalloc_mutex_lock(tsdn, &arena->tcache_ql_mtx);\n\tcache_bin_array_descriptor_t *descriptor;\n\tql_foreach(descriptor, &arena->cache_bin_array_descriptor_ql, link) {\n\t\tszind_t i = 0;\n\t\tfor (; i < SC_NBINS; i++) {\n\t\t\tcache_bin_t *tbin = &descriptor->bins_small[i];\n\t\t\tarena_stats_accum_zu(&astats->tcache_bytes,\n\t\t\t    tbin->ncached * sz_index2size(i));\n\t\t}\n\t\tfor (; i < nhbins; i++) {\n\t\t\tcache_bin_t *tbin = &descriptor->bins_large[i];\n\t\t\tarena_stats_accum_zu(&astats->tcache_bytes,\n\t\t\t    tbin->ncached * sz_index2size(i));\n\t\t}\n\t}\n\tmalloc_mutex_prof_read(tsdn,\n\t    &astats->mutex_prof_data[arena_prof_mutex_tcache_list],\n\t    &arena->tcache_ql_mtx);\n\tmalloc_mutex_unlock(tsdn, &arena->tcache_ql_mtx);\n\n#define READ_ARENA_MUTEX_PROF_DATA(mtx, ind)\t\t\t\t\\\n    malloc_mutex_lock(tsdn, &arena->mtx);\t\t\t\t\\\n    malloc_mutex_prof_read(tsdn, &astats->mutex_prof_data[ind],\t\t\\\n        &arena->mtx);\t\t\t\t\t\t\t\\\n    malloc_mutex_unlock(tsdn, &arena->mtx);\n\n\t/* Gather per arena mutex profiling data. */\n\tREAD_ARENA_MUTEX_PROF_DATA(large_mtx, arena_prof_mutex_large);\n\tREAD_ARENA_MUTEX_PROF_DATA(extent_avail_mtx,\n\t    arena_prof_mutex_extent_avail)\n\tREAD_ARENA_MUTEX_PROF_DATA(extents_dirty.mtx,\n\t    arena_prof_mutex_extents_dirty)\n\tREAD_ARENA_MUTEX_PROF_DATA(extents_muzzy.mtx,\n\t    arena_prof_mutex_extents_muzzy)\n\tREAD_ARENA_MUTEX_PROF_DATA(extents_retained.mtx,\n\t    arena_prof_mutex_extents_retained)\n\tREAD_ARENA_MUTEX_PROF_DATA(decay_dirty.mtx,\n\t    arena_prof_mutex_decay_dirty)\n\tREAD_ARENA_MUTEX_PROF_DATA(decay_muzzy.mtx,\n\t    arena_prof_mutex_decay_muzzy)\n\tREAD_ARENA_MUTEX_PROF_DATA(base->mtx,\n\t    arena_prof_mutex_base)\n#undef READ_ARENA_MUTEX_PROF_DATA\n\n\tnstime_copy(&astats->uptime, &arena->create_time);\n\tnstime_update(&astats->uptime);\n\tnstime_subtract(&astats->uptime, &arena->create_time);\n\n\tfor (szind_t i = 0; i < SC_NBINS; i++) {\n\t\tfor (unsigned j = 0; j < bin_infos[i].n_shards; j++) {\n\t\t\tbin_stats_merge(tsdn, &bstats[i],\n\t\t\t    &arena->bins[i].bin_shards[j]);\n\t\t}\n\t}\n}\n\nvoid\narena_extents_dirty_dalloc(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\textents_dalloc(tsdn, arena, r_extent_hooks, &arena->extents_dirty,\n\t    extent);\n\tif (arena_dirty_decay_ms_get(arena) == 0) {\n\t\tarena_decay_dirty(tsdn, arena, false, true);\n\t} else {\n\t\tarena_background_thread_inactivity_check(tsdn, arena, false);\n\t}\n}\n\nstatic void *\narena_slab_reg_alloc(extent_t *slab, const bin_info_t *bin_info) {\n\tvoid *ret;\n\tarena_slab_data_t *slab_data = extent_slab_data_get(slab);\n\tsize_t regind;\n\n\tassert(extent_nfree_get(slab) > 0);\n\tassert(!bitmap_full(slab_data->bitmap, &bin_info->bitmap_info));\n\n\tregind = bitmap_sfu(slab_data->bitmap, &bin_info->bitmap_info);\n\tret = (void *)((uintptr_t)extent_addr_get(slab) +\n\t    (uintptr_t)(bin_info->reg_size * regind));\n\textent_nfree_dec(slab);\n\treturn ret;\n}\n\nstatic void\narena_slab_reg_alloc_batch(extent_t *slab, const bin_info_t *bin_info,\n\t\t\t   unsigned cnt, void** ptrs) {\n\tarena_slab_data_t *slab_data = extent_slab_data_get(slab);\n\n\tassert(extent_nfree_get(slab) >= cnt);\n\tassert(!bitmap_full(slab_data->bitmap, &bin_info->bitmap_info));\n\n#if (! defined JEMALLOC_INTERNAL_POPCOUNTL) || (defined BITMAP_USE_TREE)\n\tfor (unsigned i = 0; i < cnt; i++) {\n\t\tsize_t regind = bitmap_sfu(slab_data->bitmap,\n\t\t\t\t\t   &bin_info->bitmap_info);\n\t\t*(ptrs + i) = (void *)((uintptr_t)extent_addr_get(slab) +\n\t\t    (uintptr_t)(bin_info->reg_size * regind));\n\t}\n#else\n\tunsigned group = 0;\n\tbitmap_t g = slab_data->bitmap[group];\n\tunsigned i = 0;\n\twhile (i < cnt) {\n\t\twhile (g == 0) {\n\t\t\tg = slab_data->bitmap[++group];\n\t\t}\n\t\tsize_t shift = group << LG_BITMAP_GROUP_NBITS;\n\t\tsize_t pop = popcount_lu(g);\n\t\tif (pop > (cnt - i)) {\n\t\t\tpop = cnt - i;\n\t\t}\n\n\t\t/*\n\t\t * Load from memory locations only once, outside the\n\t\t * hot loop below.\n\t\t */\n\t\tuintptr_t base = (uintptr_t)extent_addr_get(slab);\n\t\tuintptr_t regsize = (uintptr_t)bin_info->reg_size;\n\t\twhile (pop--) {\n\t\t\tsize_t bit = cfs_lu(&g);\n\t\t\tsize_t regind = shift + bit;\n\t\t\t*(ptrs + i) = (void *)(base + regsize * regind);\n\n\t\t\ti++;\n\t\t}\n\t\tslab_data->bitmap[group] = g;\n\t}\n#endif\n\textent_nfree_sub(slab, cnt);\n}\n\n#ifndef JEMALLOC_JET\nstatic\n#endif\nsize_t\narena_slab_regind(extent_t *slab, szind_t binind, const void *ptr) {\n\tsize_t diff, regind;\n\n\t/* Freeing a pointer outside the slab can cause assertion failure. */\n\tassert((uintptr_t)ptr >= (uintptr_t)extent_addr_get(slab));\n\tassert((uintptr_t)ptr < (uintptr_t)extent_past_get(slab));\n\t/* Freeing an interior pointer can cause assertion failure. */\n\tassert(((uintptr_t)ptr - (uintptr_t)extent_addr_get(slab)) %\n\t    (uintptr_t)bin_infos[binind].reg_size == 0);\n\n\tdiff = (size_t)((uintptr_t)ptr - (uintptr_t)extent_addr_get(slab));\n\n\t/* Avoid doing division with a variable divisor. */\n\tregind = div_compute(&arena_binind_div_info[binind], diff);\n\n\tassert(regind < bin_infos[binind].nregs);\n\n\treturn regind;\n}\n\nstatic void\narena_slab_reg_dalloc(extent_t *slab, arena_slab_data_t *slab_data, void *ptr) {\n\tszind_t binind = extent_szind_get(slab);\n\tconst bin_info_t *bin_info = &bin_infos[binind];\n\tsize_t regind = arena_slab_regind(slab, binind, ptr);\n\n\tassert(extent_nfree_get(slab) < bin_info->nregs);\n\t/* Freeing an unallocated pointer can cause assertion failure. */\n\tassert(bitmap_get(slab_data->bitmap, &bin_info->bitmap_info, regind));\n\n\tbitmap_unset(slab_data->bitmap, &bin_info->bitmap_info, regind);\n\textent_nfree_inc(slab);\n}\n\nstatic void\narena_nactive_add(arena_t *arena, size_t add_pages) {\n\tatomic_fetch_add_zu(&arena->nactive, add_pages, ATOMIC_RELAXED);\n}\n\nstatic void\narena_nactive_sub(arena_t *arena, size_t sub_pages) {\n\tassert(atomic_load_zu(&arena->nactive, ATOMIC_RELAXED) >= sub_pages);\n\tatomic_fetch_sub_zu(&arena->nactive, sub_pages, ATOMIC_RELAXED);\n}\n\nstatic void\narena_large_malloc_stats_update(tsdn_t *tsdn, arena_t *arena, size_t usize) {\n\tszind_t index, hindex;\n\n\tcassert(config_stats);\n\n\tif (usize < SC_LARGE_MINCLASS) {\n\t\tusize = SC_LARGE_MINCLASS;\n\t}\n\tindex = sz_size2index(usize);\n\thindex = (index >= SC_NBINS) ? index - SC_NBINS : 0;\n\n\tarena_stats_add_u64(tsdn, &arena->stats,\n\t    &arena->stats.lstats[hindex].nmalloc, 1);\n}\n\nstatic void\narena_large_dalloc_stats_update(tsdn_t *tsdn, arena_t *arena, size_t usize) {\n\tszind_t index, hindex;\n\n\tcassert(config_stats);\n\n\tif (usize < SC_LARGE_MINCLASS) {\n\t\tusize = SC_LARGE_MINCLASS;\n\t}\n\tindex = sz_size2index(usize);\n\thindex = (index >= SC_NBINS) ? index - SC_NBINS : 0;\n\n\tarena_stats_add_u64(tsdn, &arena->stats,\n\t    &arena->stats.lstats[hindex].ndalloc, 1);\n}\n\nstatic void\narena_large_ralloc_stats_update(tsdn_t *tsdn, arena_t *arena, size_t oldusize,\n    size_t usize) {\n\tarena_large_dalloc_stats_update(tsdn, arena, oldusize);\n\tarena_large_malloc_stats_update(tsdn, arena, usize);\n}\n\nstatic bool\narena_may_have_muzzy(arena_t *arena) {\n\treturn (pages_can_purge_lazy && (arena_muzzy_decay_ms_get(arena) != 0));\n}\n\nextent_t *\narena_extent_alloc_large(tsdn_t *tsdn, arena_t *arena, size_t usize,\n    size_t alignment, bool *zero) {\n\textent_hooks_t *extent_hooks = EXTENT_HOOKS_INITIALIZER;\n\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\tszind_t szind = sz_size2index(usize);\n\tsize_t mapped_add;\n\tbool commit = true;\n\textent_t *extent = extents_alloc(tsdn, arena, &extent_hooks,\n\t    &arena->extents_dirty, NULL, usize, sz_large_pad, alignment, false,\n\t    szind, zero, &commit);\n\tif (extent == NULL && arena_may_have_muzzy(arena)) {\n\t\textent = extents_alloc(tsdn, arena, &extent_hooks,\n\t\t    &arena->extents_muzzy, NULL, usize, sz_large_pad, alignment,\n\t\t    false, szind, zero, &commit);\n\t}\n\tsize_t size = usize + sz_large_pad;\n\tif (extent == NULL) {\n\t\textent = extent_alloc_wrapper(tsdn, arena, &extent_hooks, NULL,\n\t\t    usize, sz_large_pad, alignment, false, szind, zero,\n\t\t    &commit);\n\t\tif (config_stats) {\n\t\t\t/*\n\t\t\t * extent may be NULL on OOM, but in that case\n\t\t\t * mapped_add isn't used below, so there's no need to\n\t\t\t * conditionlly set it to 0 here.\n\t\t\t */\n\t\t\tmapped_add = size;\n\t\t}\n\t} else if (config_stats) {\n\t\tmapped_add = 0;\n\t}\n\n\tif (extent != NULL) {\n\t\tif (config_stats) {\n\t\t\tarena_stats_lock(tsdn, &arena->stats);\n\t\t\tarena_large_malloc_stats_update(tsdn, arena, usize);\n\t\t\tif (mapped_add != 0) {\n\t\t\t\tarena_stats_add_zu(tsdn, &arena->stats,\n\t\t\t\t    &arena->stats.mapped, mapped_add);\n\t\t\t}\n\t\t\tarena_stats_unlock(tsdn, &arena->stats);\n\t\t}\n\t\tarena_nactive_add(arena, size >> LG_PAGE);\n\t}\n\n\treturn extent;\n}\n\nvoid\narena_extent_dalloc_large_prep(tsdn_t *tsdn, arena_t *arena, extent_t *extent) {\n\tif (config_stats) {\n\t\tarena_stats_lock(tsdn, &arena->stats);\n\t\tarena_large_dalloc_stats_update(tsdn, arena,\n\t\t    extent_usize_get(extent));\n\t\tarena_stats_unlock(tsdn, &arena->stats);\n\t}\n\tarena_nactive_sub(arena, extent_size_get(extent) >> LG_PAGE);\n}\n\nvoid\narena_extent_ralloc_large_shrink(tsdn_t *tsdn, arena_t *arena, extent_t *extent,\n    size_t oldusize) {\n\tsize_t usize = extent_usize_get(extent);\n\tsize_t udiff = oldusize - usize;\n\n\tif (config_stats) {\n\t\tarena_stats_lock(tsdn, &arena->stats);\n\t\tarena_large_ralloc_stats_update(tsdn, arena, oldusize, usize);\n\t\tarena_stats_unlock(tsdn, &arena->stats);\n\t}\n\tarena_nactive_sub(arena, udiff >> LG_PAGE);\n}\n\nvoid\narena_extent_ralloc_large_expand(tsdn_t *tsdn, arena_t *arena, extent_t *extent,\n    size_t oldusize) {\n\tsize_t usize = extent_usize_get(extent);\n\tsize_t udiff = usize - oldusize;\n\n\tif (config_stats) {\n\t\tarena_stats_lock(tsdn, &arena->stats);\n\t\tarena_large_ralloc_stats_update(tsdn, arena, oldusize, usize);\n\t\tarena_stats_unlock(tsdn, &arena->stats);\n\t}\n\tarena_nactive_add(arena, udiff >> LG_PAGE);\n}\n\nstatic ssize_t\narena_decay_ms_read(arena_decay_t *decay) {\n\treturn atomic_load_zd(&decay->time_ms, ATOMIC_RELAXED);\n}\n\nstatic void\narena_decay_ms_write(arena_decay_t *decay, ssize_t decay_ms) {\n\tatomic_store_zd(&decay->time_ms, decay_ms, ATOMIC_RELAXED);\n}\n\nstatic void\narena_decay_deadline_init(arena_decay_t *decay) {\n\t/*\n\t * Generate a new deadline that is uniformly random within the next\n\t * epoch after the current one.\n\t */\n\tnstime_copy(&decay->deadline, &decay->epoch);\n\tnstime_add(&decay->deadline, &decay->interval);\n\tif (arena_decay_ms_read(decay) > 0) {\n\t\tnstime_t jitter;\n\n\t\tnstime_init(&jitter, prng_range_u64(&decay->jitter_state,\n\t\t    nstime_ns(&decay->interval)));\n\t\tnstime_add(&decay->deadline, &jitter);\n\t}\n}\n\nstatic bool\narena_decay_deadline_reached(const arena_decay_t *decay, const nstime_t *time) {\n\treturn (nstime_compare(&decay->deadline, time) <= 0);\n}\n\nstatic size_t\narena_decay_backlog_npages_limit(const arena_decay_t *decay) {\n\tuint64_t sum;\n\tsize_t npages_limit_backlog;\n\tunsigned i;\n\n\t/*\n\t * For each element of decay_backlog, multiply by the corresponding\n\t * fixed-point smoothstep decay factor.  Sum the products, then divide\n\t * to round down to the nearest whole number of pages.\n\t */\n\tsum = 0;\n\tfor (i = 0; i < SMOOTHSTEP_NSTEPS; i++) {\n\t\tsum += decay->backlog[i] * h_steps[i];\n\t}\n\tnpages_limit_backlog = (size_t)(sum >> SMOOTHSTEP_BFP);\n\n\treturn npages_limit_backlog;\n}\n\nstatic void\narena_decay_backlog_update_last(arena_decay_t *decay, size_t current_npages) {\n\tsize_t npages_delta = (current_npages > decay->nunpurged) ?\n\t    current_npages - decay->nunpurged : 0;\n\tdecay->backlog[SMOOTHSTEP_NSTEPS-1] = npages_delta;\n\n\tif (config_debug) {\n\t\tif (current_npages > decay->ceil_npages) {\n\t\t\tdecay->ceil_npages = current_npages;\n\t\t}\n\t\tsize_t npages_limit = arena_decay_backlog_npages_limit(decay);\n\t\tassert(decay->ceil_npages >= npages_limit);\n\t\tif (decay->ceil_npages > npages_limit) {\n\t\t\tdecay->ceil_npages = npages_limit;\n\t\t}\n\t}\n}\n\nstatic void\narena_decay_backlog_update(arena_decay_t *decay, uint64_t nadvance_u64,\n    size_t current_npages) {\n\tif (nadvance_u64 >= SMOOTHSTEP_NSTEPS) {\n\t\tmemset(decay->backlog, 0, (SMOOTHSTEP_NSTEPS-1) *\n\t\t    sizeof(size_t));\n\t} else {\n\t\tsize_t nadvance_z = (size_t)nadvance_u64;\n\n\t\tassert((uint64_t)nadvance_z == nadvance_u64);\n\n\t\tmemmove(decay->backlog, &decay->backlog[nadvance_z],\n\t\t    (SMOOTHSTEP_NSTEPS - nadvance_z) * sizeof(size_t));\n\t\tif (nadvance_z > 1) {\n\t\t\tmemset(&decay->backlog[SMOOTHSTEP_NSTEPS -\n\t\t\t    nadvance_z], 0, (nadvance_z-1) * sizeof(size_t));\n\t\t}\n\t}\n\n\tarena_decay_backlog_update_last(decay, current_npages);\n}\n\nstatic void\narena_decay_try_purge(tsdn_t *tsdn, arena_t *arena, arena_decay_t *decay,\n    extents_t *extents, size_t current_npages, size_t npages_limit,\n    bool is_background_thread) {\n\tif (current_npages > npages_limit) {\n\t\tarena_decay_to_limit(tsdn, arena, decay, extents, false,\n\t\t    npages_limit, current_npages - npages_limit,\n\t\t    is_background_thread);\n\t}\n}\n\nstatic void\narena_decay_epoch_advance_helper(arena_decay_t *decay, const nstime_t *time,\n    size_t current_npages) {\n\tassert(arena_decay_deadline_reached(decay, time));\n\n\tnstime_t delta;\n\tnstime_copy(&delta, time);\n\tnstime_subtract(&delta, &decay->epoch);\n\n\tuint64_t nadvance_u64 = nstime_divide(&delta, &decay->interval);\n\tassert(nadvance_u64 > 0);\n\n\t/* Add nadvance_u64 decay intervals to epoch. */\n\tnstime_copy(&delta, &decay->interval);\n\tnstime_imultiply(&delta, nadvance_u64);\n\tnstime_add(&decay->epoch, &delta);\n\n\t/* Set a new deadline. */\n\tarena_decay_deadline_init(decay);\n\n\t/* Update the backlog. */\n\tarena_decay_backlog_update(decay, nadvance_u64, current_npages);\n}\n\nstatic void\narena_decay_epoch_advance(tsdn_t *tsdn, arena_t *arena, arena_decay_t *decay,\n    extents_t *extents, const nstime_t *time, bool is_background_thread) {\n\tsize_t current_npages = extents_npages_get(extents);\n\tarena_decay_epoch_advance_helper(decay, time, current_npages);\n\n\tsize_t npages_limit = arena_decay_backlog_npages_limit(decay);\n\t/* We may unlock decay->mtx when try_purge(). Finish logging first. */\n\tdecay->nunpurged = (npages_limit > current_npages) ? npages_limit :\n\t    current_npages;\n\n\tif (!background_thread_enabled() || is_background_thread) {\n\t\tarena_decay_try_purge(tsdn, arena, decay, extents,\n\t\t    current_npages, npages_limit, is_background_thread);\n\t}\n}\n\nstatic void\narena_decay_reinit(arena_decay_t *decay, ssize_t decay_ms) {\n\tarena_decay_ms_write(decay, decay_ms);\n\tif (decay_ms > 0) {\n\t\tnstime_init(&decay->interval, (uint64_t)decay_ms *\n\t\t    KQU(1000000));\n\t\tnstime_idivide(&decay->interval, SMOOTHSTEP_NSTEPS);\n\t}\n\n\tnstime_init(&decay->epoch, 0);\n\tnstime_update(&decay->epoch);\n\tdecay->jitter_state = (uint64_t)(uintptr_t)decay;\n\tarena_decay_deadline_init(decay);\n\tdecay->nunpurged = 0;\n\tmemset(decay->backlog, 0, SMOOTHSTEP_NSTEPS * sizeof(size_t));\n}\n\nstatic bool\narena_decay_init(arena_decay_t *decay, ssize_t decay_ms,\n    arena_stats_decay_t *stats) {\n\tif (config_debug) {\n\t\tfor (size_t i = 0; i < sizeof(arena_decay_t); i++) {\n\t\t\tassert(((char *)decay)[i] == 0);\n\t\t}\n\t\tdecay->ceil_npages = 0;\n\t}\n\tif (malloc_mutex_init(&decay->mtx, \"decay\", WITNESS_RANK_DECAY,\n\t    malloc_mutex_rank_exclusive)) {\n\t\treturn true;\n\t}\n\tdecay->purging = false;\n\tarena_decay_reinit(decay, decay_ms);\n\t/* Memory is zeroed, so there is no need to clear stats. */\n\tif (config_stats) {\n\t\tdecay->stats = stats;\n\t}\n\treturn false;\n}\n\nstatic bool\narena_decay_ms_valid(ssize_t decay_ms) {\n\tif (decay_ms < -1) {\n\t\treturn false;\n\t}\n\tif (decay_ms == -1 || (uint64_t)decay_ms <= NSTIME_SEC_MAX *\n\t    KQU(1000)) {\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nstatic bool\narena_maybe_decay(tsdn_t *tsdn, arena_t *arena, arena_decay_t *decay,\n    extents_t *extents, bool is_background_thread) {\n\tmalloc_mutex_assert_owner(tsdn, &decay->mtx);\n\n\t/* Purge all or nothing if the option is disabled. */\n\tssize_t decay_ms = arena_decay_ms_read(decay);\n\tif (decay_ms <= 0) {\n\t\tif (decay_ms == 0) {\n\t\t\tarena_decay_to_limit(tsdn, arena, decay, extents, false,\n\t\t\t    0, extents_npages_get(extents),\n\t\t\t    is_background_thread);\n\t\t}\n\t\treturn false;\n\t}\n\n\tnstime_t time;\n\tnstime_init(&time, 0);\n\tnstime_update(&time);\n\tif (unlikely(!nstime_monotonic() && nstime_compare(&decay->epoch, &time)\n\t    > 0)) {\n\t\t/*\n\t\t * Time went backwards.  Move the epoch back in time and\n\t\t * generate a new deadline, with the expectation that time\n\t\t * typically flows forward for long enough periods of time that\n\t\t * epochs complete.  Unfortunately, this strategy is susceptible\n\t\t * to clock jitter triggering premature epoch advances, but\n\t\t * clock jitter estimation and compensation isn't feasible here\n\t\t * because calls into this code are event-driven.\n\t\t */\n\t\tnstime_copy(&decay->epoch, &time);\n\t\tarena_decay_deadline_init(decay);\n\t} else {\n\t\t/* Verify that time does not go backwards. */\n\t\tassert(nstime_compare(&decay->epoch, &time) <= 0);\n\t}\n\n\t/*\n\t * If the deadline has been reached, advance to the current epoch and\n\t * purge to the new limit if necessary.  Note that dirty pages created\n\t * during the current epoch are not subject to purge until a future\n\t * epoch, so as a result purging only happens during epoch advances, or\n\t * being triggered by background threads (scheduled event).\n\t */\n\tbool advance_epoch = arena_decay_deadline_reached(decay, &time);\n\tif (advance_epoch) {\n\t\tarena_decay_epoch_advance(tsdn, arena, decay, extents, &time,\n\t\t    is_background_thread);\n\t} else if (is_background_thread) {\n\t\tarena_decay_try_purge(tsdn, arena, decay, extents,\n\t\t    extents_npages_get(extents),\n\t\t    arena_decay_backlog_npages_limit(decay),\n\t\t    is_background_thread);\n\t}\n\n\treturn advance_epoch;\n}\n\nstatic ssize_t\narena_decay_ms_get(arena_decay_t *decay) {\n\treturn arena_decay_ms_read(decay);\n}\n\nssize_t\narena_dirty_decay_ms_get(arena_t *arena) {\n\treturn arena_decay_ms_get(&arena->decay_dirty);\n}\n\nssize_t\narena_muzzy_decay_ms_get(arena_t *arena) {\n\treturn arena_decay_ms_get(&arena->decay_muzzy);\n}\n\nstatic bool\narena_decay_ms_set(tsdn_t *tsdn, arena_t *arena, arena_decay_t *decay,\n    extents_t *extents, ssize_t decay_ms) {\n\tif (!arena_decay_ms_valid(decay_ms)) {\n\t\treturn true;\n\t}\n\n\tmalloc_mutex_lock(tsdn, &decay->mtx);\n\t/*\n\t * Restart decay backlog from scratch, which may cause many dirty pages\n\t * to be immediately purged.  It would conceptually be possible to map\n\t * the old backlog onto the new backlog, but there is no justification\n\t * for such complexity since decay_ms changes are intended to be\n\t * infrequent, either between the {-1, 0, >0} states, or a one-time\n\t * arbitrary change during initial arena configuration.\n\t */\n\tarena_decay_reinit(decay, decay_ms);\n\tarena_maybe_decay(tsdn, arena, decay, extents, false);\n\tmalloc_mutex_unlock(tsdn, &decay->mtx);\n\n\treturn false;\n}\n\nbool\narena_dirty_decay_ms_set(tsdn_t *tsdn, arena_t *arena,\n    ssize_t decay_ms) {\n\treturn arena_decay_ms_set(tsdn, arena, &arena->decay_dirty,\n\t    &arena->extents_dirty, decay_ms);\n}\n\nbool\narena_muzzy_decay_ms_set(tsdn_t *tsdn, arena_t *arena,\n    ssize_t decay_ms) {\n\treturn arena_decay_ms_set(tsdn, arena, &arena->decay_muzzy,\n\t    &arena->extents_muzzy, decay_ms);\n}\n\nstatic size_t\narena_stash_decayed(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extents_t *extents, size_t npages_limit,\n\tsize_t npages_decay_max, extent_list_t *decay_extents) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\t/* Stash extents according to npages_limit. */\n\tsize_t nstashed = 0;\n\textent_t *extent;\n\twhile (nstashed < npages_decay_max &&\n\t    (extent = extents_evict(tsdn, arena, r_extent_hooks, extents,\n\t    npages_limit)) != NULL) {\n\t\textent_list_append(decay_extents, extent);\n\t\tnstashed += extent_size_get(extent) >> LG_PAGE;\n\t}\n\treturn nstashed;\n}\n\nstatic size_t\narena_decay_stashed(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, arena_decay_t *decay, extents_t *extents,\n    bool all, extent_list_t *decay_extents, bool is_background_thread) {\n\tsize_t nmadvise, nunmapped;\n\tsize_t npurged;\n\n\tif (config_stats) {\n\t\tnmadvise = 0;\n\t\tnunmapped = 0;\n\t}\n\tnpurged = 0;\n\n\tssize_t muzzy_decay_ms = arena_muzzy_decay_ms_get(arena);\n\tfor (extent_t *extent = extent_list_first(decay_extents); extent !=\n\t    NULL; extent = extent_list_first(decay_extents)) {\n\t\tif (config_stats) {\n\t\t\tnmadvise++;\n\t\t}\n\t\tsize_t npages = extent_size_get(extent) >> LG_PAGE;\n\t\tnpurged += npages;\n\t\textent_list_remove(decay_extents, extent);\n\t\tswitch (extents_state_get(extents)) {\n\t\tcase extent_state_active:\n\t\t\tnot_reached();\n\t\tcase extent_state_dirty:\n\t\t\tif (!all && muzzy_decay_ms != 0 &&\n\t\t\t    !extent_purge_lazy_wrapper(tsdn, arena,\n\t\t\t    r_extent_hooks, extent, 0,\n\t\t\t    extent_size_get(extent))) {\n\t\t\t\textents_dalloc(tsdn, arena, r_extent_hooks,\n\t\t\t\t    &arena->extents_muzzy, extent);\n\t\t\t\tarena_background_thread_inactivity_check(tsdn,\n\t\t\t\t    arena, is_background_thread);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\t/* Fall through. */\n\t\tcase extent_state_muzzy:\n\t\t\textent_dalloc_wrapper(tsdn, arena, r_extent_hooks,\n\t\t\t    extent);\n\t\t\tif (config_stats) {\n\t\t\t\tnunmapped += npages;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase extent_state_retained:\n\t\tdefault:\n\t\t\tnot_reached();\n\t\t}\n\t}\n\n\tif (config_stats) {\n\t\tarena_stats_lock(tsdn, &arena->stats);\n\t\tarena_stats_add_u64(tsdn, &arena->stats, &decay->stats->npurge,\n\t\t    1);\n\t\tarena_stats_add_u64(tsdn, &arena->stats,\n\t\t    &decay->stats->nmadvise, nmadvise);\n\t\tarena_stats_add_u64(tsdn, &arena->stats, &decay->stats->purged,\n\t\t    npurged);\n\t\tarena_stats_sub_zu(tsdn, &arena->stats, &arena->stats.mapped,\n\t\t    nunmapped << LG_PAGE);\n\t\tarena_stats_unlock(tsdn, &arena->stats);\n\t}\n\n\treturn npurged;\n}\n\n/*\n * npages_limit: Decay at most npages_decay_max pages without violating the\n * invariant: (extents_npages_get(extents) >= npages_limit).  We need an upper\n * bound on number of pages in order to prevent unbounded growth (namely in\n * stashed), otherwise unbounded new pages could be added to extents during the\n * current decay run, so that the purging thread never finishes.\n */\nstatic void\narena_decay_to_limit(tsdn_t *tsdn, arena_t *arena, arena_decay_t *decay,\n    extents_t *extents, bool all, size_t npages_limit, size_t npages_decay_max,\n    bool is_background_thread) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 1);\n\tmalloc_mutex_assert_owner(tsdn, &decay->mtx);\n\n\tif (decay->purging) {\n\t\treturn;\n\t}\n\tdecay->purging = true;\n\tmalloc_mutex_unlock(tsdn, &decay->mtx);\n\n\textent_hooks_t *extent_hooks = extent_hooks_get(arena);\n\n\textent_list_t decay_extents;\n\textent_list_init(&decay_extents);\n\n\tsize_t npurge = arena_stash_decayed(tsdn, arena, &extent_hooks, extents,\n\t    npages_limit, npages_decay_max, &decay_extents);\n\tif (npurge != 0) {\n\t\tsize_t npurged = arena_decay_stashed(tsdn, arena,\n\t\t    &extent_hooks, decay, extents, all, &decay_extents,\n\t\t    is_background_thread);\n\t\tassert(npurged == npurge);\n\t}\n\n\tmalloc_mutex_lock(tsdn, &decay->mtx);\n\tdecay->purging = false;\n}\n\nstatic bool\narena_decay_impl(tsdn_t *tsdn, arena_t *arena, arena_decay_t *decay,\n    extents_t *extents, bool is_background_thread, bool all) {\n\tif (all) {\n\t\tmalloc_mutex_lock(tsdn, &decay->mtx);\n\t\tarena_decay_to_limit(tsdn, arena, decay, extents, all, 0,\n\t\t    extents_npages_get(extents), is_background_thread);\n\t\tmalloc_mutex_unlock(tsdn, &decay->mtx);\n\n\t\treturn false;\n\t}\n\n\tif (malloc_mutex_trylock(tsdn, &decay->mtx)) {\n\t\t/* No need to wait if another thread is in progress. */\n\t\treturn true;\n\t}\n\n\tbool epoch_advanced = arena_maybe_decay(tsdn, arena, decay, extents,\n\t    is_background_thread);\n\tsize_t npages_new;\n\tif (epoch_advanced) {\n\t\t/* Backlog is updated on epoch advance. */\n\t\tnpages_new = decay->backlog[SMOOTHSTEP_NSTEPS-1];\n\t}\n\tmalloc_mutex_unlock(tsdn, &decay->mtx);\n\n\tif (have_background_thread && background_thread_enabled() &&\n\t    epoch_advanced && !is_background_thread) {\n\t\tbackground_thread_interval_check(tsdn, arena, decay,\n\t\t    npages_new);\n\t}\n\n\treturn false;\n}\n\nstatic bool\narena_decay_dirty(tsdn_t *tsdn, arena_t *arena, bool is_background_thread,\n    bool all) {\n\treturn arena_decay_impl(tsdn, arena, &arena->decay_dirty,\n\t    &arena->extents_dirty, is_background_thread, all);\n}\n\nstatic bool\narena_decay_muzzy(tsdn_t *tsdn, arena_t *arena, bool is_background_thread,\n    bool all) {\n\treturn arena_decay_impl(tsdn, arena, &arena->decay_muzzy,\n\t    &arena->extents_muzzy, is_background_thread, all);\n}\n\nvoid\narena_decay(tsdn_t *tsdn, arena_t *arena, bool is_background_thread, bool all) {\n\tif (arena_decay_dirty(tsdn, arena, is_background_thread, all)) {\n\t\treturn;\n\t}\n\tarena_decay_muzzy(tsdn, arena, is_background_thread, all);\n}\n\nstatic void\narena_slab_dalloc(tsdn_t *tsdn, arena_t *arena, extent_t *slab) {\n\tarena_nactive_sub(arena, extent_size_get(slab) >> LG_PAGE);\n\n\textent_hooks_t *extent_hooks = EXTENT_HOOKS_INITIALIZER;\n\tarena_extents_dirty_dalloc(tsdn, arena, &extent_hooks, slab);\n}\n\nstatic void\narena_bin_slabs_nonfull_insert(bin_t *bin, extent_t *slab) {\n\tassert(extent_nfree_get(slab) > 0);\n\textent_heap_insert(&bin->slabs_nonfull, slab);\n\tif (config_stats) {\n\t\tbin->stats.nonfull_slabs++;\n\t}\n}\n\nstatic void\narena_bin_slabs_nonfull_remove(bin_t *bin, extent_t *slab) {\n\textent_heap_remove(&bin->slabs_nonfull, slab);\n\tif (config_stats) {\n\t\tbin->stats.nonfull_slabs--;\n\t}\n}\n\nstatic extent_t *\narena_bin_slabs_nonfull_tryget(bin_t *bin) {\n\textent_t *slab = extent_heap_remove_first(&bin->slabs_nonfull);\n\tif (slab == NULL) {\n\t\treturn NULL;\n\t}\n\tif (config_stats) {\n\t\tbin->stats.reslabs++;\n\t\tbin->stats.nonfull_slabs--;\n\t}\n\treturn slab;\n}\n\nstatic void\narena_bin_slabs_full_insert(arena_t *arena, bin_t *bin, extent_t *slab) {\n\tassert(extent_nfree_get(slab) == 0);\n\t/*\n\t *  Tracking extents is required by arena_reset, which is not allowed\n\t *  for auto arenas.  Bypass this step to avoid touching the extent\n\t *  linkage (often results in cache misses) for auto arenas.\n\t */\n\tif (arena_is_auto(arena)) {\n\t\treturn;\n\t}\n\textent_list_append(&bin->slabs_full, slab);\n}\n\nstatic void\narena_bin_slabs_full_remove(arena_t *arena, bin_t *bin, extent_t *slab) {\n\tif (arena_is_auto(arena)) {\n\t\treturn;\n\t}\n\textent_list_remove(&bin->slabs_full, slab);\n}\n\nstatic void\narena_bin_reset(tsd_t *tsd, arena_t *arena, bin_t *bin) {\n\textent_t *slab;\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &bin->lock);\n\tif (bin->slabcur != NULL) {\n\t\tslab = bin->slabcur;\n\t\tbin->slabcur = NULL;\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &bin->lock);\n\t\tarena_slab_dalloc(tsd_tsdn(tsd), arena, slab);\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &bin->lock);\n\t}\n\twhile ((slab = extent_heap_remove_first(&bin->slabs_nonfull)) != NULL) {\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &bin->lock);\n\t\tarena_slab_dalloc(tsd_tsdn(tsd), arena, slab);\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &bin->lock);\n\t}\n\tfor (slab = extent_list_first(&bin->slabs_full); slab != NULL;\n\t     slab = extent_list_first(&bin->slabs_full)) {\n\t\tarena_bin_slabs_full_remove(arena, bin, slab);\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &bin->lock);\n\t\tarena_slab_dalloc(tsd_tsdn(tsd), arena, slab);\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &bin->lock);\n\t}\n\tif (config_stats) {\n\t\tbin->stats.curregs = 0;\n\t\tbin->stats.curslabs = 0;\n\t}\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &bin->lock);\n}\n\nvoid\narena_reset(tsd_t *tsd, arena_t *arena) {\n\t/*\n\t * Locking in this function is unintuitive.  The caller guarantees that\n\t * no concurrent operations are happening in this arena, but there are\n\t * still reasons that some locking is necessary:\n\t *\n\t * - Some of the functions in the transitive closure of calls assume\n\t *   appropriate locks are held, and in some cases these locks are\n\t *   temporarily dropped to avoid lock order reversal or deadlock due to\n\t *   reentry.\n\t * - mallctl(\"epoch\", ...) may concurrently refresh stats.  While\n\t *   strictly speaking this is a \"concurrent operation\", disallowing\n\t *   stats refreshes would impose an inconvenient burden.\n\t */\n\n\t/* Large allocations. */\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &arena->large_mtx);\n\n\tfor (extent_t *extent = extent_list_first(&arena->large); extent !=\n\t    NULL; extent = extent_list_first(&arena->large)) {\n\t\tvoid *ptr = extent_base_get(extent);\n\t\tsize_t usize;\n\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &arena->large_mtx);\n\t\talloc_ctx_t alloc_ctx;\n\t\trtree_ctx_t *rtree_ctx = tsd_rtree_ctx(tsd);\n\t\trtree_szind_slab_read(tsd_tsdn(tsd), &extents_rtree, rtree_ctx,\n\t\t    (uintptr_t)ptr, true, &alloc_ctx.szind, &alloc_ctx.slab);\n\t\tassert(alloc_ctx.szind != SC_NSIZES);\n\n\t\tif (config_stats || (config_prof && opt_prof)) {\n\t\t\tusize = sz_index2size(alloc_ctx.szind);\n\t\t\tassert(usize == isalloc(tsd_tsdn(tsd), ptr));\n\t\t}\n\t\t/* Remove large allocation from prof sample set. */\n\t\tif (config_prof && opt_prof) {\n\t\t\tprof_free(tsd, ptr, usize, &alloc_ctx);\n\t\t}\n\t\tlarge_dalloc(tsd_tsdn(tsd), extent);\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &arena->large_mtx);\n\t}\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &arena->large_mtx);\n\n\t/* Bins. */\n\tfor (unsigned i = 0; i < SC_NBINS; i++) {\n\t\tfor (unsigned j = 0; j < bin_infos[i].n_shards; j++) {\n\t\t\tarena_bin_reset(tsd, arena,\n\t\t\t    &arena->bins[i].bin_shards[j]);\n\t\t}\n\t}\n\n\tatomic_store_zu(&arena->nactive, 0, ATOMIC_RELAXED);\n}\n\nstatic void\narena_destroy_retained(tsdn_t *tsdn, arena_t *arena) {\n\t/*\n\t * Iterate over the retained extents and destroy them.  This gives the\n\t * extent allocator underlying the extent hooks an opportunity to unmap\n\t * all retained memory without having to keep its own metadata\n\t * structures.  In practice, virtual memory for dss-allocated extents is\n\t * leaked here, so best practice is to avoid dss for arenas to be\n\t * destroyed, or provide custom extent hooks that track retained\n\t * dss-based extents for later reuse.\n\t */\n\textent_hooks_t *extent_hooks = extent_hooks_get(arena);\n\textent_t *extent;\n\twhile ((extent = extents_evict(tsdn, arena, &extent_hooks,\n\t    &arena->extents_retained, 0)) != NULL) {\n\t\textent_destroy_wrapper(tsdn, arena, &extent_hooks, extent);\n\t}\n}\n\nvoid\narena_destroy(tsd_t *tsd, arena_t *arena) {\n\tassert(base_ind_get(arena->base) >= narenas_auto);\n\tassert(arena_nthreads_get(arena, false) == 0);\n\tassert(arena_nthreads_get(arena, true) == 0);\n\n\t/*\n\t * No allocations have occurred since arena_reset() was called.\n\t * Furthermore, the caller (arena_i_destroy_ctl()) purged all cached\n\t * extents, so only retained extents may remain.\n\t */\n\tassert(extents_npages_get(&arena->extents_dirty) == 0);\n\tassert(extents_npages_get(&arena->extents_muzzy) == 0);\n\n\t/* Deallocate retained memory. */\n\tarena_destroy_retained(tsd_tsdn(tsd), arena);\n\n\t/*\n\t * Remove the arena pointer from the arenas array.  We rely on the fact\n\t * that there is no way for the application to get a dirty read from the\n\t * arenas array unless there is an inherent race in the application\n\t * involving access of an arena being concurrently destroyed.  The\n\t * application must synchronize knowledge of the arena's validity, so as\n\t * long as we use an atomic write to update the arenas array, the\n\t * application will get a clean read any time after it synchronizes\n\t * knowledge that the arena is no longer valid.\n\t */\n\tarena_set(base_ind_get(arena->base), NULL);\n\n\t/*\n\t * Destroy the base allocator, which manages all metadata ever mapped by\n\t * this arena.\n\t */\n\tbase_delete(tsd_tsdn(tsd), arena->base);\n}\n\nstatic extent_t *\narena_slab_alloc_hard(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, const bin_info_t *bin_info,\n    szind_t szind) {\n\textent_t *slab;\n\tbool zero, commit;\n\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\tzero = false;\n\tcommit = true;\n\tslab = extent_alloc_wrapper(tsdn, arena, r_extent_hooks, NULL,\n\t    bin_info->slab_size, 0, PAGE, true, szind, &zero, &commit);\n\n\tif (config_stats && slab != NULL) {\n\t\tarena_stats_mapped_add(tsdn, &arena->stats,\n\t\t    bin_info->slab_size);\n\t}\n\n\treturn slab;\n}\n\nstatic extent_t *\narena_slab_alloc(tsdn_t *tsdn, arena_t *arena, szind_t binind, unsigned binshard,\n    const bin_info_t *bin_info) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\textent_hooks_t *extent_hooks = EXTENT_HOOKS_INITIALIZER;\n\tszind_t szind = sz_size2index(bin_info->reg_size);\n\tbool zero = false;\n\tbool commit = true;\n\textent_t *slab = extents_alloc(tsdn, arena, &extent_hooks,\n\t    &arena->extents_dirty, NULL, bin_info->slab_size, 0, PAGE, true,\n\t    binind, &zero, &commit);\n\tif (slab == NULL && arena_may_have_muzzy(arena)) {\n\t\tslab = extents_alloc(tsdn, arena, &extent_hooks,\n\t\t    &arena->extents_muzzy, NULL, bin_info->slab_size, 0, PAGE,\n\t\t    true, binind, &zero, &commit);\n\t}\n\tif (slab == NULL) {\n\t\tslab = arena_slab_alloc_hard(tsdn, arena, &extent_hooks,\n\t\t    bin_info, szind);\n\t\tif (slab == NULL) {\n\t\t\treturn NULL;\n\t\t}\n\t}\n\tassert(extent_slab_get(slab));\n\n\t/* Initialize slab internals. */\n\tarena_slab_data_t *slab_data = extent_slab_data_get(slab);\n\textent_nfree_binshard_set(slab, bin_info->nregs, binshard);\n\tbitmap_init(slab_data->bitmap, &bin_info->bitmap_info, false);\n\n\tarena_nactive_add(arena, extent_size_get(slab) >> LG_PAGE);\n\n\treturn slab;\n}\n\nstatic extent_t *\narena_bin_nonfull_slab_get(tsdn_t *tsdn, arena_t *arena, bin_t *bin,\n    szind_t binind, unsigned binshard) {\n\textent_t *slab;\n\tconst bin_info_t *bin_info;\n\n\t/* Look for a usable slab. */\n\tslab = arena_bin_slabs_nonfull_tryget(bin);\n\tif (slab != NULL) {\n\t\treturn slab;\n\t}\n\t/* No existing slabs have any space available. */\n\n\tbin_info = &bin_infos[binind];\n\n\t/* Allocate a new slab. */\n\tmalloc_mutex_unlock(tsdn, &bin->lock);\n\t/******************************/\n\tslab = arena_slab_alloc(tsdn, arena, binind, binshard, bin_info);\n\t/********************************/\n\tmalloc_mutex_lock(tsdn, &bin->lock);\n\tif (slab != NULL) {\n\t\tif (config_stats) {\n\t\t\tbin->stats.nslabs++;\n\t\t\tbin->stats.curslabs++;\n\t\t}\n\t\treturn slab;\n\t}\n\n\t/*\n\t * arena_slab_alloc() failed, but another thread may have made\n\t * sufficient memory available while this one dropped bin->lock above,\n\t * so search one more time.\n\t */\n\tslab = arena_bin_slabs_nonfull_tryget(bin);\n\tif (slab != NULL) {\n\t\treturn slab;\n\t}\n\n\treturn NULL;\n}\n\n/* Re-fill bin->slabcur, then call arena_slab_reg_alloc(). */\nstatic void *\narena_bin_malloc_hard(tsdn_t *tsdn, arena_t *arena, bin_t *bin,\n    szind_t binind, unsigned binshard) {\n\tconst bin_info_t *bin_info;\n\textent_t *slab;\n\n\tbin_info = &bin_infos[binind];\n\tif (!arena_is_auto(arena) && bin->slabcur != NULL) {\n\t\tarena_bin_slabs_full_insert(arena, bin, bin->slabcur);\n\t\tbin->slabcur = NULL;\n\t}\n\tslab = arena_bin_nonfull_slab_get(tsdn, arena, bin, binind, binshard);\n\tif (bin->slabcur != NULL) {\n\t\t/*\n\t\t * Another thread updated slabcur while this one ran without the\n\t\t * bin lock in arena_bin_nonfull_slab_get().\n\t\t */\n\t\tif (extent_nfree_get(bin->slabcur) > 0) {\n\t\t\tvoid *ret = arena_slab_reg_alloc(bin->slabcur,\n\t\t\t    bin_info);\n\t\t\tif (slab != NULL) {\n\t\t\t\t/*\n\t\t\t\t * arena_slab_alloc() may have allocated slab,\n\t\t\t\t * or it may have been pulled from\n\t\t\t\t * slabs_nonfull.  Therefore it is unsafe to\n\t\t\t\t * make any assumptions about how slab has\n\t\t\t\t * previously been used, and\n\t\t\t\t * arena_bin_lower_slab() must be called, as if\n\t\t\t\t * a region were just deallocated from the slab.\n\t\t\t\t */\n\t\t\t\tif (extent_nfree_get(slab) == bin_info->nregs) {\n\t\t\t\t\tarena_dalloc_bin_slab(tsdn, arena, slab,\n\t\t\t\t\t    bin);\n\t\t\t\t} else {\n\t\t\t\t\tarena_bin_lower_slab(tsdn, arena, slab,\n\t\t\t\t\t    bin);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn ret;\n\t\t}\n\n\t\tarena_bin_slabs_full_insert(arena, bin, bin->slabcur);\n\t\tbin->slabcur = NULL;\n\t}\n\n\tif (slab == NULL) {\n\t\treturn NULL;\n\t}\n\tbin->slabcur = slab;\n\n\tassert(extent_nfree_get(bin->slabcur) > 0);\n\n\treturn arena_slab_reg_alloc(slab, bin_info);\n}\n\n/* Choose a bin shard and return the locked bin. */\nbin_t *\narena_bin_choose_lock(tsdn_t *tsdn, arena_t *arena, szind_t binind,\n    unsigned *binshard) {\n\tbin_t *bin;\n\tif (tsdn_null(tsdn) || tsd_arena_get(tsdn_tsd(tsdn)) == NULL) {\n\t\t*binshard = 0;\n\t} else {\n\t\t*binshard = tsd_binshardsp_get(tsdn_tsd(tsdn))->binshard[binind];\n\t}\n\tassert(*binshard < bin_infos[binind].n_shards);\n\tbin = &arena->bins[binind].bin_shards[*binshard];\n\tmalloc_mutex_lock(tsdn, &bin->lock);\n\n\treturn bin;\n}\n\nvoid\narena_tcache_fill_small(tsdn_t *tsdn, arena_t *arena, tcache_t *tcache,\n    cache_bin_t *tbin, szind_t binind, uint64_t prof_accumbytes) {\n\tunsigned i, nfill, cnt;\n\n\tassert(tbin->ncached == 0);\n\n\tif (config_prof && arena_prof_accum(tsdn, arena, prof_accumbytes)) {\n\t\tprof_idump(tsdn);\n\t}\n\n\tunsigned binshard;\n\tbin_t *bin = arena_bin_choose_lock(tsdn, arena, binind, &binshard);\n\n\tfor (i = 0, nfill = (tcache_bin_info[binind].ncached_max >>\n\t    tcache->lg_fill_div[binind]); i < nfill; i += cnt) {\n\t\textent_t *slab;\n\t\tif ((slab = bin->slabcur) != NULL && extent_nfree_get(slab) >\n\t\t    0) {\n\t\t\tunsigned tofill = nfill - i;\n\t\t\tcnt = tofill < extent_nfree_get(slab) ?\n\t\t\t\ttofill : extent_nfree_get(slab);\n\t\t\tarena_slab_reg_alloc_batch(\n\t\t\t   slab, &bin_infos[binind], cnt,\n\t\t\t   tbin->avail - nfill + i);\n\t\t} else {\n\t\t\tcnt = 1;\n\t\t\tvoid *ptr = arena_bin_malloc_hard(tsdn, arena, bin,\n\t\t\t    binind, binshard);\n\t\t\t/*\n\t\t\t * OOM.  tbin->avail isn't yet filled down to its first\n\t\t\t * element, so the successful allocations (if any) must\n\t\t\t * be moved just before tbin->avail before bailing out.\n\t\t\t */\n\t\t\tif (ptr == NULL) {\n\t\t\t\tif (i > 0) {\n\t\t\t\t\tmemmove(tbin->avail - i,\n\t\t\t\t\t\ttbin->avail - nfill,\n\t\t\t\t\t\ti * sizeof(void *));\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\t/* Insert such that low regions get used first. */\n\t\t\t*(tbin->avail - nfill + i) = ptr;\n\t\t}\n\t\tif (config_fill && unlikely(opt_junk_alloc)) {\n\t\t\tfor (unsigned j = 0; j < cnt; j++) {\n\t\t\t\tvoid* ptr = *(tbin->avail - nfill + i + j);\n\t\t\t\tarena_alloc_junk_small(ptr, &bin_infos[binind],\n\t\t\t\t\t\t\ttrue);\n\t\t\t}\n\t\t}\n\t}\n\tif (config_stats) {\n\t\tbin->stats.nmalloc += i;\n\t\tbin->stats.nrequests += tbin->tstats.nrequests;\n\t\tbin->stats.curregs += i;\n\t\tbin->stats.nfills++;\n\t\ttbin->tstats.nrequests = 0;\n\t}\n\tmalloc_mutex_unlock(tsdn, &bin->lock);\n\ttbin->ncached = i;\n\tarena_decay_tick(tsdn, arena);\n}\n\nvoid\narena_alloc_junk_small(void *ptr, const bin_info_t *bin_info, bool zero) {\n\tif (!zero) {\n\t\tmemset(ptr, JEMALLOC_ALLOC_JUNK, bin_info->reg_size);\n\t}\n}\n\nstatic void\narena_dalloc_junk_small_impl(void *ptr, const bin_info_t *bin_info) {\n\tmemset(ptr, JEMALLOC_FREE_JUNK, bin_info->reg_size);\n}\narena_dalloc_junk_small_t *JET_MUTABLE arena_dalloc_junk_small =\n    arena_dalloc_junk_small_impl;\n\nstatic void *\narena_malloc_small(tsdn_t *tsdn, arena_t *arena, szind_t binind, bool zero) {\n\tvoid *ret;\n\tbin_t *bin;\n\tsize_t usize;\n\textent_t *slab;\n\n\tassert(binind < SC_NBINS);\n\tusize = sz_index2size(binind);\n\tunsigned binshard;\n\tbin = arena_bin_choose_lock(tsdn, arena, binind, &binshard);\n\n\tif ((slab = bin->slabcur) != NULL && extent_nfree_get(slab) > 0) {\n\t\tret = arena_slab_reg_alloc(slab, &bin_infos[binind]);\n\t} else {\n\t\tret = arena_bin_malloc_hard(tsdn, arena, bin, binind, binshard);\n\t}\n\n\tif (ret == NULL) {\n\t\tmalloc_mutex_unlock(tsdn, &bin->lock);\n\t\treturn NULL;\n\t}\n\n\tif (config_stats) {\n\t\tbin->stats.nmalloc++;\n\t\tbin->stats.nrequests++;\n\t\tbin->stats.curregs++;\n\t}\n\tmalloc_mutex_unlock(tsdn, &bin->lock);\n\tif (config_prof && arena_prof_accum(tsdn, arena, usize)) {\n\t\tprof_idump(tsdn);\n\t}\n\n\tif (!zero) {\n\t\tif (config_fill) {\n\t\t\tif (unlikely(opt_junk_alloc)) {\n\t\t\t\tarena_alloc_junk_small(ret,\n\t\t\t\t    &bin_infos[binind], false);\n\t\t\t} else if (unlikely(opt_zero)) {\n\t\t\t\tmemset(ret, 0, usize);\n\t\t\t}\n\t\t}\n\t} else {\n\t\tif (config_fill && unlikely(opt_junk_alloc)) {\n\t\t\tarena_alloc_junk_small(ret, &bin_infos[binind],\n\t\t\t    true);\n\t\t}\n\t\tmemset(ret, 0, usize);\n\t}\n\n\tarena_decay_tick(tsdn, arena);\n\treturn ret;\n}\n\nvoid *\narena_malloc_hard(tsdn_t *tsdn, arena_t *arena, size_t size, szind_t ind,\n    bool zero) {\n\tassert(!tsdn_null(tsdn) || arena != NULL);\n\n\tif (likely(!tsdn_null(tsdn))) {\n\t\tarena = arena_choose_maybe_huge(tsdn_tsd(tsdn), arena, size);\n\t}\n\tif (unlikely(arena == NULL)) {\n\t\treturn NULL;\n\t}\n\n\tif (likely(size <= SC_SMALL_MAXCLASS)) {\n\t\treturn arena_malloc_small(tsdn, arena, ind, zero);\n\t}\n\treturn large_malloc(tsdn, arena, sz_index2size(ind), zero);\n}\n\nvoid *\narena_palloc(tsdn_t *tsdn, arena_t *arena, size_t usize, size_t alignment,\n    bool zero, tcache_t *tcache) {\n\tvoid *ret;\n\n\tif (usize <= SC_SMALL_MAXCLASS\n\t    && (alignment < PAGE\n\t    || (alignment == PAGE && (usize & PAGE_MASK) == 0))) {\n\t\t/* Small; alignment doesn't require special slab placement. */\n\t\tret = arena_malloc(tsdn, arena, usize, sz_size2index(usize),\n\t\t    zero, tcache, true);\n\t} else {\n\t\tif (likely(alignment <= CACHELINE)) {\n\t\t\tret = large_malloc(tsdn, arena, usize, zero);\n\t\t} else {\n\t\t\tret = large_palloc(tsdn, arena, usize, alignment, zero);\n\t\t}\n\t}\n\treturn ret;\n}\n\nvoid\narena_prof_promote(tsdn_t *tsdn, void *ptr, size_t usize) {\n\tcassert(config_prof);\n\tassert(ptr != NULL);\n\tassert(isalloc(tsdn, ptr) == SC_LARGE_MINCLASS);\n\tassert(usize <= SC_SMALL_MAXCLASS);\n\n\tif (config_opt_safety_checks) {\n\t\tsafety_check_set_redzone(ptr, usize, SC_LARGE_MINCLASS);\n\t}\n\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\n\textent_t *extent = rtree_extent_read(tsdn, &extents_rtree, rtree_ctx,\n\t    (uintptr_t)ptr, true);\n\tarena_t *arena = extent_arena_get(extent);\n\n\tszind_t szind = sz_size2index(usize);\n\textent_szind_set(extent, szind);\n\trtree_szind_slab_update(tsdn, &extents_rtree, rtree_ctx, (uintptr_t)ptr,\n\t    szind, false);\n\n\tprof_accum_cancel(tsdn, &arena->prof_accum, usize);\n\n\tassert(isalloc(tsdn, ptr) == usize);\n}\n\nstatic size_t\narena_prof_demote(tsdn_t *tsdn, extent_t *extent, const void *ptr) {\n\tcassert(config_prof);\n\tassert(ptr != NULL);\n\n\textent_szind_set(extent, SC_NBINS);\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\trtree_szind_slab_update(tsdn, &extents_rtree, rtree_ctx, (uintptr_t)ptr,\n\t    SC_NBINS, false);\n\n\tassert(isalloc(tsdn, ptr) == SC_LARGE_MINCLASS);\n\n\treturn SC_LARGE_MINCLASS;\n}\n\nvoid\narena_dalloc_promoted(tsdn_t *tsdn, void *ptr, tcache_t *tcache,\n    bool slow_path) {\n\tcassert(config_prof);\n\tassert(opt_prof);\n\n\textent_t *extent = iealloc(tsdn, ptr);\n\tsize_t usize = extent_usize_get(extent);\n\tsize_t bumped_usize = arena_prof_demote(tsdn, extent, ptr);\n\tif (config_opt_safety_checks && usize < SC_LARGE_MINCLASS) {\n\t\t/*\n\t\t * Currently, we only do redzoning for small sampled\n\t\t * allocations.\n\t\t */\n\t\tassert(bumped_usize == SC_LARGE_MINCLASS);\n\t\tsafety_check_verify_redzone(ptr, usize, bumped_usize);\n\t}\n\tif (bumped_usize <= tcache_maxclass && tcache != NULL) {\n\t\ttcache_dalloc_large(tsdn_tsd(tsdn), tcache, ptr,\n\t\t    sz_size2index(bumped_usize), slow_path);\n\t} else {\n\t\tlarge_dalloc(tsdn, extent);\n\t}\n}\n\nstatic void\narena_dissociate_bin_slab(arena_t *arena, extent_t *slab, bin_t *bin) {\n\t/* Dissociate slab from bin. */\n\tif (slab == bin->slabcur) {\n\t\tbin->slabcur = NULL;\n\t} else {\n\t\tszind_t binind = extent_szind_get(slab);\n\t\tconst bin_info_t *bin_info = &bin_infos[binind];\n\n\t\t/*\n\t\t * The following block's conditional is necessary because if the\n\t\t * slab only contains one region, then it never gets inserted\n\t\t * into the non-full slabs heap.\n\t\t */\n\t\tif (bin_info->nregs == 1) {\n\t\t\tarena_bin_slabs_full_remove(arena, bin, slab);\n\t\t} else {\n\t\t\tarena_bin_slabs_nonfull_remove(bin, slab);\n\t\t}\n\t}\n}\n\nstatic void\narena_dalloc_bin_slab(tsdn_t *tsdn, arena_t *arena, extent_t *slab,\n    bin_t *bin) {\n\tassert(slab != bin->slabcur);\n\n\tmalloc_mutex_unlock(tsdn, &bin->lock);\n\t/******************************/\n\tarena_slab_dalloc(tsdn, arena, slab);\n\t/****************************/\n\tmalloc_mutex_lock(tsdn, &bin->lock);\n\tif (config_stats) {\n\t\tbin->stats.curslabs--;\n\t}\n}\n\nstatic void\narena_bin_lower_slab(tsdn_t *tsdn, arena_t *arena, extent_t *slab,\n    bin_t *bin) {\n\tassert(extent_nfree_get(slab) > 0);\n\n\t/*\n\t * Make sure that if bin->slabcur is non-NULL, it refers to the\n\t * oldest/lowest non-full slab.  It is okay to NULL slabcur out rather\n\t * than proactively keeping it pointing at the oldest/lowest non-full\n\t * slab.\n\t */\n\tif (bin->slabcur != NULL && extent_snad_comp(bin->slabcur, slab) > 0) {\n\t\t/* Switch slabcur. */\n\t\tif (extent_nfree_get(bin->slabcur) > 0) {\n\t\t\tarena_bin_slabs_nonfull_insert(bin, bin->slabcur);\n\t\t} else {\n\t\t\tarena_bin_slabs_full_insert(arena, bin, bin->slabcur);\n\t\t}\n\t\tbin->slabcur = slab;\n\t\tif (config_stats) {\n\t\t\tbin->stats.reslabs++;\n\t\t}\n\t} else {\n\t\tarena_bin_slabs_nonfull_insert(bin, slab);\n\t}\n}\n\nstatic void\narena_dalloc_bin_locked_impl(tsdn_t *tsdn, arena_t *arena, bin_t *bin,\n    szind_t binind, extent_t *slab, void *ptr, bool junked) {\n\tarena_slab_data_t *slab_data = extent_slab_data_get(slab);\n\tconst bin_info_t *bin_info = &bin_infos[binind];\n\n\tif (!junked && config_fill && unlikely(opt_junk_free)) {\n\t\tarena_dalloc_junk_small(ptr, bin_info);\n\t}\n\n\tarena_slab_reg_dalloc(slab, slab_data, ptr);\n\tunsigned nfree = extent_nfree_get(slab);\n\tif (nfree == bin_info->nregs) {\n\t\tarena_dissociate_bin_slab(arena, slab, bin);\n\t\tarena_dalloc_bin_slab(tsdn, arena, slab, bin);\n\t} else if (nfree == 1 && slab != bin->slabcur) {\n\t\tarena_bin_slabs_full_remove(arena, bin, slab);\n\t\tarena_bin_lower_slab(tsdn, arena, slab, bin);\n\t}\n\n\tif (config_stats) {\n\t\tbin->stats.ndalloc++;\n\t\tbin->stats.curregs--;\n\t}\n}\n\nvoid\narena_dalloc_bin_junked_locked(tsdn_t *tsdn, arena_t *arena, bin_t *bin,\n    szind_t binind, extent_t *extent, void *ptr) {\n\tarena_dalloc_bin_locked_impl(tsdn, arena, bin, binind, extent, ptr,\n\t    true);\n}\n\nstatic void\narena_dalloc_bin(tsdn_t *tsdn, arena_t *arena, extent_t *extent, void *ptr) {\n\tszind_t binind = extent_szind_get(extent);\n\tunsigned binshard = extent_binshard_get(extent);\n\tbin_t *bin = &arena->bins[binind].bin_shards[binshard];\n\n\tmalloc_mutex_lock(tsdn, &bin->lock);\n\tarena_dalloc_bin_locked_impl(tsdn, arena, bin, binind, extent, ptr,\n\t    false);\n\tmalloc_mutex_unlock(tsdn, &bin->lock);\n}\n\nvoid\narena_dalloc_small(tsdn_t *tsdn, void *ptr) {\n\textent_t *extent = iealloc(tsdn, ptr);\n\tarena_t *arena = extent_arena_get(extent);\n\n\tarena_dalloc_bin(tsdn, arena, extent, ptr);\n\tarena_decay_tick(tsdn, arena);\n}\n\nbool\narena_ralloc_no_move(tsdn_t *tsdn, void *ptr, size_t oldsize, size_t size,\n    size_t extra, bool zero, size_t *newsize) {\n\tbool ret;\n\t/* Calls with non-zero extra had to clamp extra. */\n\tassert(extra == 0 || size + extra <= SC_LARGE_MAXCLASS);\n\n\textent_t *extent = iealloc(tsdn, ptr);\n\tif (unlikely(size > SC_LARGE_MAXCLASS)) {\n\t\tret = true;\n\t\tgoto done;\n\t}\n\n\tsize_t usize_min = sz_s2u(size);\n\tsize_t usize_max = sz_s2u(size + extra);\n\tif (likely(oldsize <= SC_SMALL_MAXCLASS && usize_min\n\t    <= SC_SMALL_MAXCLASS)) {\n\t\t/*\n\t\t * Avoid moving the allocation if the size class can be left the\n\t\t * same.\n\t\t */\n\t\tassert(bin_infos[sz_size2index(oldsize)].reg_size ==\n\t\t    oldsize);\n\t\tif ((usize_max > SC_SMALL_MAXCLASS\n\t\t    || sz_size2index(usize_max) != sz_size2index(oldsize))\n\t\t    && (size > oldsize || usize_max < oldsize)) {\n\t\t\tret = true;\n\t\t\tgoto done;\n\t\t}\n\n\t\tarena_decay_tick(tsdn, extent_arena_get(extent));\n\t\tret = false;\n\t} else if (oldsize >= SC_LARGE_MINCLASS\n\t    && usize_max >= SC_LARGE_MINCLASS) {\n\t\tret = large_ralloc_no_move(tsdn, extent, usize_min, usize_max,\n\t\t    zero);\n\t} else {\n\t\tret = true;\n\t}\ndone:\n\tassert(extent == iealloc(tsdn, ptr));\n\t*newsize = extent_usize_get(extent);\n\n\treturn ret;\n}\n\nstatic void *\narena_ralloc_move_helper(tsdn_t *tsdn, arena_t *arena, size_t usize,\n    size_t alignment, bool zero, tcache_t *tcache) {\n\tif (alignment == 0) {\n\t\treturn arena_malloc(tsdn, arena, usize, sz_size2index(usize),\n\t\t    zero, tcache, true);\n\t}\n\tusize = sz_sa2u(usize, alignment);\n\tif (unlikely(usize == 0 || usize > SC_LARGE_MAXCLASS)) {\n\t\treturn NULL;\n\t}\n\treturn ipalloct(tsdn, usize, alignment, zero, tcache, arena);\n}\n\nvoid *\narena_ralloc(tsdn_t *tsdn, arena_t *arena, void *ptr, size_t oldsize,\n    size_t size, size_t alignment, bool zero, tcache_t *tcache,\n    hook_ralloc_args_t *hook_args) {\n\tsize_t usize = sz_s2u(size);\n\tif (unlikely(usize == 0 || size > SC_LARGE_MAXCLASS)) {\n\t\treturn NULL;\n\t}\n\n\tif (likely(usize <= SC_SMALL_MAXCLASS)) {\n\t\t/* Try to avoid moving the allocation. */\n\t\tUNUSED size_t newsize;\n\t\tif (!arena_ralloc_no_move(tsdn, ptr, oldsize, usize, 0, zero,\n\t\t    &newsize)) {\n\t\t\thook_invoke_expand(hook_args->is_realloc\n\t\t\t    ? hook_expand_realloc : hook_expand_rallocx,\n\t\t\t    ptr, oldsize, usize, (uintptr_t)ptr,\n\t\t\t    hook_args->args);\n\t\t\treturn ptr;\n\t\t}\n\t}\n\n\tif (oldsize >= SC_LARGE_MINCLASS\n\t    && usize >= SC_LARGE_MINCLASS) {\n\t\treturn large_ralloc(tsdn, arena, ptr, usize,\n\t\t    alignment, zero, tcache, hook_args);\n\t}\n\n\t/*\n\t * size and oldsize are different enough that we need to move the\n\t * object.  In that case, fall back to allocating new space and copying.\n\t */\n\tvoid *ret = arena_ralloc_move_helper(tsdn, arena, usize, alignment,\n\t    zero, tcache);\n\tif (ret == NULL) {\n\t\treturn NULL;\n\t}\n\n\thook_invoke_alloc(hook_args->is_realloc\n\t    ? hook_alloc_realloc : hook_alloc_rallocx, ret, (uintptr_t)ret,\n\t    hook_args->args);\n\thook_invoke_dalloc(hook_args->is_realloc\n\t    ? hook_dalloc_realloc : hook_dalloc_rallocx, ptr, hook_args->args);\n\n\t/*\n\t * Junk/zero-filling were already done by\n\t * ipalloc()/arena_malloc().\n\t */\n\tsize_t copysize = (usize < oldsize) ? usize : oldsize;\n\tmemcpy(ret, ptr, copysize);\n\tisdalloct(tsdn, ptr, oldsize, tcache, NULL, true);\n\treturn ret;\n}\n\ndss_prec_t\narena_dss_prec_get(arena_t *arena) {\n\treturn (dss_prec_t)atomic_load_u(&arena->dss_prec, ATOMIC_ACQUIRE);\n}\n\nbool\narena_dss_prec_set(arena_t *arena, dss_prec_t dss_prec) {\n\tif (!have_dss) {\n\t\treturn (dss_prec != dss_prec_disabled);\n\t}\n\tatomic_store_u(&arena->dss_prec, (unsigned)dss_prec, ATOMIC_RELEASE);\n\treturn false;\n}\n\nssize_t\narena_dirty_decay_ms_default_get(void) {\n\treturn atomic_load_zd(&dirty_decay_ms_default, ATOMIC_RELAXED);\n}\n\nbool\narena_dirty_decay_ms_default_set(ssize_t decay_ms) {\n\tif (!arena_decay_ms_valid(decay_ms)) {\n\t\treturn true;\n\t}\n\tatomic_store_zd(&dirty_decay_ms_default, decay_ms, ATOMIC_RELAXED);\n\treturn false;\n}\n\nssize_t\narena_muzzy_decay_ms_default_get(void) {\n\treturn atomic_load_zd(&muzzy_decay_ms_default, ATOMIC_RELAXED);\n}\n\nbool\narena_muzzy_decay_ms_default_set(ssize_t decay_ms) {\n\tif (!arena_decay_ms_valid(decay_ms)) {\n\t\treturn true;\n\t}\n\tatomic_store_zd(&muzzy_decay_ms_default, decay_ms, ATOMIC_RELAXED);\n\treturn false;\n}\n\nbool\narena_retain_grow_limit_get_set(tsd_t *tsd, arena_t *arena, size_t *old_limit,\n    size_t *new_limit) {\n\tassert(opt_retain);\n\n\tpszind_t new_ind JEMALLOC_CC_SILENCE_INIT(0);\n\tif (new_limit != NULL) {\n\t\tsize_t limit = *new_limit;\n\t\t/* Grow no more than the new limit. */\n\t\tif ((new_ind = sz_psz2ind(limit + 1) - 1) >= SC_NPSIZES) {\n\t\t\treturn true;\n\t\t}\n\t}\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &arena->extent_grow_mtx);\n\tif (old_limit != NULL) {\n\t\t*old_limit = sz_pind2sz(arena->retain_grow_limit);\n\t}\n\tif (new_limit != NULL) {\n\t\tarena->retain_grow_limit = new_ind;\n\t}\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &arena->extent_grow_mtx);\n\n\treturn false;\n}\n\nunsigned\narena_nthreads_get(arena_t *arena, bool internal) {\n\treturn atomic_load_u(&arena->nthreads[internal], ATOMIC_RELAXED);\n}\n\nvoid\narena_nthreads_inc(arena_t *arena, bool internal) {\n\tatomic_fetch_add_u(&arena->nthreads[internal], 1, ATOMIC_RELAXED);\n}\n\nvoid\narena_nthreads_dec(arena_t *arena, bool internal) {\n\tatomic_fetch_sub_u(&arena->nthreads[internal], 1, ATOMIC_RELAXED);\n}\n\nsize_t\narena_extent_sn_next(arena_t *arena) {\n\treturn atomic_fetch_add_zu(&arena->extent_sn_next, 1, ATOMIC_RELAXED);\n}\n\narena_t *\narena_new(tsdn_t *tsdn, unsigned ind, extent_hooks_t *extent_hooks) {\n\tarena_t *arena;\n\tbase_t *base;\n\tunsigned i;\n\n\tif (ind == 0) {\n\t\tbase = b0get();\n\t} else {\n\t\tbase = base_new(tsdn, ind, extent_hooks);\n\t\tif (base == NULL) {\n\t\t\treturn NULL;\n\t\t}\n\t}\n\n\tunsigned nbins_total = 0;\n\tfor (i = 0; i < SC_NBINS; i++) {\n\t\tnbins_total += bin_infos[i].n_shards;\n\t}\n\tsize_t arena_size = sizeof(arena_t) + sizeof(bin_t) * nbins_total;\n\tarena = (arena_t *)base_alloc(tsdn, base, arena_size, CACHELINE);\n\tif (arena == NULL) {\n\t\tgoto label_error;\n\t}\n\n\tatomic_store_u(&arena->nthreads[0], 0, ATOMIC_RELAXED);\n\tatomic_store_u(&arena->nthreads[1], 0, ATOMIC_RELAXED);\n\tarena->last_thd = NULL;\n\n\tif (config_stats) {\n\t\tif (arena_stats_init(tsdn, &arena->stats)) {\n\t\t\tgoto label_error;\n\t\t}\n\n\t\tql_new(&arena->tcache_ql);\n\t\tql_new(&arena->cache_bin_array_descriptor_ql);\n\t\tif (malloc_mutex_init(&arena->tcache_ql_mtx, \"tcache_ql\",\n\t\t    WITNESS_RANK_TCACHE_QL, malloc_mutex_rank_exclusive)) {\n\t\t\tgoto label_error;\n\t\t}\n\t}\n\n\tif (config_prof) {\n\t\tif (prof_accum_init(tsdn, &arena->prof_accum)) {\n\t\t\tgoto label_error;\n\t\t}\n\t}\n\n\tif (config_cache_oblivious) {\n\t\t/*\n\t\t * A nondeterministic seed based on the address of arena reduces\n\t\t * the likelihood of lockstep non-uniform cache index\n\t\t * utilization among identical concurrent processes, but at the\n\t\t * cost of test repeatability.  For debug builds, instead use a\n\t\t * deterministic seed.\n\t\t */\n\t\tatomic_store_zu(&arena->offset_state, config_debug ? ind :\n\t\t    (size_t)(uintptr_t)arena, ATOMIC_RELAXED);\n\t}\n\n\tatomic_store_zu(&arena->extent_sn_next, 0, ATOMIC_RELAXED);\n\n\tatomic_store_u(&arena->dss_prec, (unsigned)extent_dss_prec_get(),\n\t    ATOMIC_RELAXED);\n\n\tatomic_store_zu(&arena->nactive, 0, ATOMIC_RELAXED);\n\n\textent_list_init(&arena->large);\n\tif (malloc_mutex_init(&arena->large_mtx, \"arena_large\",\n\t    WITNESS_RANK_ARENA_LARGE, malloc_mutex_rank_exclusive)) {\n\t\tgoto label_error;\n\t}\n\n\t/*\n\t * Delay coalescing for dirty extents despite the disruptive effect on\n\t * memory layout for best-fit extent allocation, since cached extents\n\t * are likely to be reused soon after deallocation, and the cost of\n\t * merging/splitting extents is non-trivial.\n\t */\n\tif (extents_init(tsdn, &arena->extents_dirty, extent_state_dirty,\n\t    true)) {\n\t\tgoto label_error;\n\t}\n\t/*\n\t * Coalesce muzzy extents immediately, because operations on them are in\n\t * the critical path much less often than for dirty extents.\n\t */\n\tif (extents_init(tsdn, &arena->extents_muzzy, extent_state_muzzy,\n\t    false)) {\n\t\tgoto label_error;\n\t}\n\t/*\n\t * Coalesce retained extents immediately, in part because they will\n\t * never be evicted (and therefore there's no opportunity for delayed\n\t * coalescing), but also because operations on retained extents are not\n\t * in the critical path.\n\t */\n\tif (extents_init(tsdn, &arena->extents_retained, extent_state_retained,\n\t    false)) {\n\t\tgoto label_error;\n\t}\n\n\tif (arena_decay_init(&arena->decay_dirty,\n\t    arena_dirty_decay_ms_default_get(), &arena->stats.decay_dirty)) {\n\t\tgoto label_error;\n\t}\n\tif (arena_decay_init(&arena->decay_muzzy,\n\t    arena_muzzy_decay_ms_default_get(), &arena->stats.decay_muzzy)) {\n\t\tgoto label_error;\n\t}\n\n\tarena->extent_grow_next = sz_psz2ind(HUGEPAGE);\n\tarena->retain_grow_limit = sz_psz2ind(SC_LARGE_MAXCLASS);\n\tif (malloc_mutex_init(&arena->extent_grow_mtx, \"extent_grow\",\n\t    WITNESS_RANK_EXTENT_GROW, malloc_mutex_rank_exclusive)) {\n\t\tgoto label_error;\n\t}\n\n\textent_avail_new(&arena->extent_avail);\n\tif (malloc_mutex_init(&arena->extent_avail_mtx, \"extent_avail\",\n\t    WITNESS_RANK_EXTENT_AVAIL, malloc_mutex_rank_exclusive)) {\n\t\tgoto label_error;\n\t}\n\n\t/* Initialize bins. */\n\tuintptr_t bin_addr = (uintptr_t)arena + sizeof(arena_t);\n\tatomic_store_u(&arena->binshard_next, 0, ATOMIC_RELEASE);\n\tfor (i = 0; i < SC_NBINS; i++) {\n\t\tunsigned nshards = bin_infos[i].n_shards;\n\t\tarena->bins[i].bin_shards = (bin_t *)bin_addr;\n\t\tbin_addr += nshards * sizeof(bin_t);\n\t\tfor (unsigned j = 0; j < nshards; j++) {\n\t\t\tbool err = bin_init(&arena->bins[i].bin_shards[j]);\n\t\t\tif (err) {\n\t\t\t\tgoto label_error;\n\t\t\t}\n\t\t}\n\t}\n\tassert(bin_addr == (uintptr_t)arena + arena_size);\n\n\tarena->base = base;\n\t/* Set arena before creating background threads. */\n\tarena_set(ind, arena);\n\n\tnstime_init(&arena->create_time, 0);\n\tnstime_update(&arena->create_time);\n\n\t/* We don't support reentrancy for arena 0 bootstrapping. */\n\tif (ind != 0) {\n\t\t/*\n\t\t * If we're here, then arena 0 already exists, so bootstrapping\n\t\t * is done enough that we should have tsd.\n\t\t */\n\t\tassert(!tsdn_null(tsdn));\n\t\tpre_reentrancy(tsdn_tsd(tsdn), arena);\n\t\tif (test_hooks_arena_new_hook) {\n\t\t\ttest_hooks_arena_new_hook();\n\t\t}\n\t\tpost_reentrancy(tsdn_tsd(tsdn));\n\t}\n\n\treturn arena;\nlabel_error:\n\tif (ind != 0) {\n\t\tbase_delete(tsdn, base);\n\t}\n\treturn NULL;\n}\n\narena_t *\narena_choose_huge(tsd_t *tsd) {\n\t/* huge_arena_ind can be 0 during init (will use a0). */\n\tif (huge_arena_ind == 0) {\n\t\tassert(!malloc_initialized());\n\t}\n\n\tarena_t *huge_arena = arena_get(tsd_tsdn(tsd), huge_arena_ind, false);\n\tif (huge_arena == NULL) {\n\t\t/* Create the huge arena on demand. */\n\t\tassert(huge_arena_ind != 0);\n\t\thuge_arena = arena_get(tsd_tsdn(tsd), huge_arena_ind, true);\n\t\tif (huge_arena == NULL) {\n\t\t\treturn NULL;\n\t\t}\n\t\t/*\n\t\t * Purge eagerly for huge allocations, because: 1) number of\n\t\t * huge allocations is usually small, which means ticker based\n\t\t * decay is not reliable; and 2) less immediate reuse is\n\t\t * expected for huge allocations.\n\t\t */\n\t\tif (arena_dirty_decay_ms_default_get() > 0) {\n\t\t\tarena_dirty_decay_ms_set(tsd_tsdn(tsd), huge_arena, 0);\n\t\t}\n\t\tif (arena_muzzy_decay_ms_default_get() > 0) {\n\t\t\tarena_muzzy_decay_ms_set(tsd_tsdn(tsd), huge_arena, 0);\n\t\t}\n\t}\n\n\treturn huge_arena;\n}\n\nbool\narena_init_huge(void) {\n\tbool huge_enabled;\n\n\t/* The threshold should be large size class. */\n\tif (opt_oversize_threshold > SC_LARGE_MAXCLASS ||\n\t    opt_oversize_threshold < SC_LARGE_MINCLASS) {\n\t\topt_oversize_threshold = 0;\n\t\toversize_threshold = SC_LARGE_MAXCLASS + PAGE;\n\t\thuge_enabled = false;\n\t} else {\n\t\t/* Reserve the index for the huge arena. */\n\t\thuge_arena_ind = narenas_total_get();\n\t\toversize_threshold = opt_oversize_threshold;\n\t\thuge_enabled = true;\n\t}\n\n\treturn huge_enabled;\n}\n\nbool\narena_is_huge(unsigned arena_ind) {\n\tif (huge_arena_ind == 0) {\n\t\treturn false;\n\t}\n\treturn (arena_ind == huge_arena_ind);\n}\n\nvoid\narena_boot(sc_data_t *sc_data) {\n\tarena_dirty_decay_ms_default_set(opt_dirty_decay_ms);\n\tarena_muzzy_decay_ms_default_set(opt_muzzy_decay_ms);\n\tfor (unsigned i = 0; i < SC_NBINS; i++) {\n\t\tsc_t *sc = &sc_data->sc[i];\n\t\tdiv_init(&arena_binind_div_info[i],\n\t\t    (1U << sc->lg_base) + (sc->ndelta << sc->lg_delta));\n\t}\n}\n\nvoid\narena_prefork0(tsdn_t *tsdn, arena_t *arena) {\n\tmalloc_mutex_prefork(tsdn, &arena->decay_dirty.mtx);\n\tmalloc_mutex_prefork(tsdn, &arena->decay_muzzy.mtx);\n}\n\nvoid\narena_prefork1(tsdn_t *tsdn, arena_t *arena) {\n\tif (config_stats) {\n\t\tmalloc_mutex_prefork(tsdn, &arena->tcache_ql_mtx);\n\t}\n}\n\nvoid\narena_prefork2(tsdn_t *tsdn, arena_t *arena) {\n\tmalloc_mutex_prefork(tsdn, &arena->extent_grow_mtx);\n}\n\nvoid\narena_prefork3(tsdn_t *tsdn, arena_t *arena) {\n\textents_prefork(tsdn, &arena->extents_dirty);\n\textents_prefork(tsdn, &arena->extents_muzzy);\n\textents_prefork(tsdn, &arena->extents_retained);\n}\n\nvoid\narena_prefork4(tsdn_t *tsdn, arena_t *arena) {\n\tmalloc_mutex_prefork(tsdn, &arena->extent_avail_mtx);\n}\n\nvoid\narena_prefork5(tsdn_t *tsdn, arena_t *arena) {\n\tbase_prefork(tsdn, arena->base);\n}\n\nvoid\narena_prefork6(tsdn_t *tsdn, arena_t *arena) {\n\tmalloc_mutex_prefork(tsdn, &arena->large_mtx);\n}\n\nvoid\narena_prefork7(tsdn_t *tsdn, arena_t *arena) {\n\tfor (unsigned i = 0; i < SC_NBINS; i++) {\n\t\tfor (unsigned j = 0; j < bin_infos[i].n_shards; j++) {\n\t\t\tbin_prefork(tsdn, &arena->bins[i].bin_shards[j]);\n\t\t}\n\t}\n}\n\nvoid\narena_postfork_parent(tsdn_t *tsdn, arena_t *arena) {\n\tunsigned i;\n\n\tfor (i = 0; i < SC_NBINS; i++) {\n\t\tfor (unsigned j = 0; j < bin_infos[i].n_shards; j++) {\n\t\t\tbin_postfork_parent(tsdn,\n\t\t\t    &arena->bins[i].bin_shards[j]);\n\t\t}\n\t}\n\tmalloc_mutex_postfork_parent(tsdn, &arena->large_mtx);\n\tbase_postfork_parent(tsdn, arena->base);\n\tmalloc_mutex_postfork_parent(tsdn, &arena->extent_avail_mtx);\n\textents_postfork_parent(tsdn, &arena->extents_dirty);\n\textents_postfork_parent(tsdn, &arena->extents_muzzy);\n\textents_postfork_parent(tsdn, &arena->extents_retained);\n\tmalloc_mutex_postfork_parent(tsdn, &arena->extent_grow_mtx);\n\tmalloc_mutex_postfork_parent(tsdn, &arena->decay_dirty.mtx);\n\tmalloc_mutex_postfork_parent(tsdn, &arena->decay_muzzy.mtx);\n\tif (config_stats) {\n\t\tmalloc_mutex_postfork_parent(tsdn, &arena->tcache_ql_mtx);\n\t}\n}\n\nvoid\narena_postfork_child(tsdn_t *tsdn, arena_t *arena) {\n\tunsigned i;\n\n\tatomic_store_u(&arena->nthreads[0], 0, ATOMIC_RELAXED);\n\tatomic_store_u(&arena->nthreads[1], 0, ATOMIC_RELAXED);\n\tif (tsd_arena_get(tsdn_tsd(tsdn)) == arena) {\n\t\tarena_nthreads_inc(arena, false);\n\t}\n\tif (tsd_iarena_get(tsdn_tsd(tsdn)) == arena) {\n\t\tarena_nthreads_inc(arena, true);\n\t}\n\tif (config_stats) {\n\t\tql_new(&arena->tcache_ql);\n\t\tql_new(&arena->cache_bin_array_descriptor_ql);\n\t\ttcache_t *tcache = tcache_get(tsdn_tsd(tsdn));\n\t\tif (tcache != NULL && tcache->arena == arena) {\n\t\t\tql_elm_new(tcache, link);\n\t\t\tql_tail_insert(&arena->tcache_ql, tcache, link);\n\t\t\tcache_bin_array_descriptor_init(\n\t\t\t    &tcache->cache_bin_array_descriptor,\n\t\t\t    tcache->bins_small, tcache->bins_large);\n\t\t\tql_tail_insert(&arena->cache_bin_array_descriptor_ql,\n\t\t\t    &tcache->cache_bin_array_descriptor, link);\n\t\t}\n\t}\n\n\tfor (i = 0; i < SC_NBINS; i++) {\n\t\tfor (unsigned j = 0; j < bin_infos[i].n_shards; j++) {\n\t\t\tbin_postfork_child(tsdn, &arena->bins[i].bin_shards[j]);\n\t\t}\n\t}\n\tmalloc_mutex_postfork_child(tsdn, &arena->large_mtx);\n\tbase_postfork_child(tsdn, arena->base);\n\tmalloc_mutex_postfork_child(tsdn, &arena->extent_avail_mtx);\n\textents_postfork_child(tsdn, &arena->extents_dirty);\n\textents_postfork_child(tsdn, &arena->extents_muzzy);\n\textents_postfork_child(tsdn, &arena->extents_retained);\n\tmalloc_mutex_postfork_child(tsdn, &arena->extent_grow_mtx);\n\tmalloc_mutex_postfork_child(tsdn, &arena->decay_dirty.mtx);\n\tmalloc_mutex_postfork_child(tsdn, &arena->decay_muzzy.mtx);\n\tif (config_stats) {\n\t\tmalloc_mutex_postfork_child(tsdn, &arena->tcache_ql_mtx);\n\t}\n}\n"
  },
  {
    "path": "deps/jemalloc/src/background_thread.c",
    "content": "#define JEMALLOC_BACKGROUND_THREAD_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n\nJEMALLOC_DIAGNOSTIC_DISABLE_SPURIOUS\n\n/******************************************************************************/\n/* Data. */\n\n/* This option should be opt-in only. */\n#define BACKGROUND_THREAD_DEFAULT false\n/* Read-only after initialization. */\nbool opt_background_thread = BACKGROUND_THREAD_DEFAULT;\nsize_t opt_max_background_threads = MAX_BACKGROUND_THREAD_LIMIT + 1;\n\n/* Used for thread creation, termination and stats. */\nmalloc_mutex_t background_thread_lock;\n/* Indicates global state.  Atomic because decay reads this w/o locking. */\natomic_b_t background_thread_enabled_state;\nsize_t n_background_threads;\nsize_t max_background_threads;\n/* Thread info per-index. */\nbackground_thread_info_t *background_thread_info;\n\n/******************************************************************************/\n\n#ifdef JEMALLOC_PTHREAD_CREATE_WRAPPER\n\nstatic int (*pthread_create_fptr)(pthread_t *__restrict, const pthread_attr_t *,\n    void *(*)(void *), void *__restrict);\n\nstatic void\npthread_create_wrapper_init(void) {\n#ifdef JEMALLOC_LAZY_LOCK\n\tif (!isthreaded) {\n\t\tisthreaded = true;\n\t}\n#endif\n}\n\nint\npthread_create_wrapper(pthread_t *__restrict thread, const pthread_attr_t *attr,\n    void *(*start_routine)(void *), void *__restrict arg) {\n\tpthread_create_wrapper_init();\n\n\treturn pthread_create_fptr(thread, attr, start_routine, arg);\n}\n#endif /* JEMALLOC_PTHREAD_CREATE_WRAPPER */\n\n#ifndef JEMALLOC_BACKGROUND_THREAD\n#define NOT_REACHED { not_reached(); }\nbool background_thread_create(tsd_t *tsd, unsigned arena_ind) NOT_REACHED\nbool background_threads_enable(tsd_t *tsd) NOT_REACHED\nbool background_threads_disable(tsd_t *tsd) NOT_REACHED\nvoid background_thread_interval_check(tsdn_t *tsdn, arena_t *arena,\n    arena_decay_t *decay, size_t npages_new) NOT_REACHED\nvoid background_thread_prefork0(tsdn_t *tsdn) NOT_REACHED\nvoid background_thread_prefork1(tsdn_t *tsdn) NOT_REACHED\nvoid background_thread_postfork_parent(tsdn_t *tsdn) NOT_REACHED\nvoid background_thread_postfork_child(tsdn_t *tsdn) NOT_REACHED\nbool background_thread_stats_read(tsdn_t *tsdn,\n    background_thread_stats_t *stats) NOT_REACHED\nvoid background_thread_ctl_init(tsdn_t *tsdn) NOT_REACHED\n#undef NOT_REACHED\n#else\n\nstatic bool background_thread_enabled_at_fork;\n\nstatic void\nbackground_thread_info_init(tsdn_t *tsdn, background_thread_info_t *info) {\n\tbackground_thread_wakeup_time_set(tsdn, info, 0);\n\tinfo->npages_to_purge_new = 0;\n\tif (config_stats) {\n\t\tinfo->tot_n_runs = 0;\n\t\tnstime_init(&info->tot_sleep_time, 0);\n\t}\n}\n\nstatic inline bool\nset_current_thread_affinity(int cpu) {\n#if defined(JEMALLOC_HAVE_SCHED_SETAFFINITY)\n\tcpu_set_t cpuset;\n\tCPU_ZERO(&cpuset);\n\tCPU_SET(cpu, &cpuset);\n\tint ret = sched_setaffinity(0, sizeof(cpu_set_t), &cpuset);\n\n\treturn (ret != 0);\n#else\n\treturn false;\n#endif\n}\n\n/* Threshold for determining when to wake up the background thread. */\n#define BACKGROUND_THREAD_NPAGES_THRESHOLD UINT64_C(1024)\n#define BILLION UINT64_C(1000000000)\n/* Minimal sleep interval 100 ms. */\n#define BACKGROUND_THREAD_MIN_INTERVAL_NS (BILLION / 10)\n\nstatic inline size_t\ndecay_npurge_after_interval(arena_decay_t *decay, size_t interval) {\n\tsize_t i;\n\tuint64_t sum = 0;\n\tfor (i = 0; i < interval; i++) {\n\t\tsum += decay->backlog[i] * h_steps[i];\n\t}\n\tfor (; i < SMOOTHSTEP_NSTEPS; i++) {\n\t\tsum += decay->backlog[i] * (h_steps[i] - h_steps[i - interval]);\n\t}\n\n\treturn (size_t)(sum >> SMOOTHSTEP_BFP);\n}\n\nstatic uint64_t\narena_decay_compute_purge_interval_impl(tsdn_t *tsdn, arena_decay_t *decay,\n    extents_t *extents) {\n\tif (malloc_mutex_trylock(tsdn, &decay->mtx)) {\n\t\t/* Use minimal interval if decay is contended. */\n\t\treturn BACKGROUND_THREAD_MIN_INTERVAL_NS;\n\t}\n\n\tuint64_t interval;\n\tssize_t decay_time = atomic_load_zd(&decay->time_ms, ATOMIC_RELAXED);\n\tif (decay_time <= 0) {\n\t\t/* Purging is eagerly done or disabled currently. */\n\t\tinterval = BACKGROUND_THREAD_INDEFINITE_SLEEP;\n\t\tgoto label_done;\n\t}\n\n\tuint64_t decay_interval_ns = nstime_ns(&decay->interval);\n\tassert(decay_interval_ns > 0);\n\tsize_t npages = extents_npages_get(extents);\n\tif (npages == 0) {\n\t\tunsigned i;\n\t\tfor (i = 0; i < SMOOTHSTEP_NSTEPS; i++) {\n\t\t\tif (decay->backlog[i] > 0) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (i == SMOOTHSTEP_NSTEPS) {\n\t\t\t/* No dirty pages recorded.  Sleep indefinitely. */\n\t\t\tinterval = BACKGROUND_THREAD_INDEFINITE_SLEEP;\n\t\t\tgoto label_done;\n\t\t}\n\t}\n\tif (npages <= BACKGROUND_THREAD_NPAGES_THRESHOLD) {\n\t\t/* Use max interval. */\n\t\tinterval = decay_interval_ns * SMOOTHSTEP_NSTEPS;\n\t\tgoto label_done;\n\t}\n\n\tsize_t lb = BACKGROUND_THREAD_MIN_INTERVAL_NS / decay_interval_ns;\n\tsize_t ub = SMOOTHSTEP_NSTEPS;\n\t/* Minimal 2 intervals to ensure reaching next epoch deadline. */\n\tlb = (lb < 2) ? 2 : lb;\n\tif ((decay_interval_ns * ub <= BACKGROUND_THREAD_MIN_INTERVAL_NS) ||\n\t    (lb + 2 > ub)) {\n\t\tinterval = BACKGROUND_THREAD_MIN_INTERVAL_NS;\n\t\tgoto label_done;\n\t}\n\n\tassert(lb + 2 <= ub);\n\tsize_t npurge_lb, npurge_ub;\n\tnpurge_lb = decay_npurge_after_interval(decay, lb);\n\tif (npurge_lb > BACKGROUND_THREAD_NPAGES_THRESHOLD) {\n\t\tinterval = decay_interval_ns * lb;\n\t\tgoto label_done;\n\t}\n\tnpurge_ub = decay_npurge_after_interval(decay, ub);\n\tif (npurge_ub < BACKGROUND_THREAD_NPAGES_THRESHOLD) {\n\t\tinterval = decay_interval_ns * ub;\n\t\tgoto label_done;\n\t}\n\n\tunsigned n_search = 0;\n\tsize_t target, npurge;\n\twhile ((npurge_lb + BACKGROUND_THREAD_NPAGES_THRESHOLD < npurge_ub)\n\t    && (lb + 2 < ub)) {\n\t\ttarget = (lb + ub) / 2;\n\t\tnpurge = decay_npurge_after_interval(decay, target);\n\t\tif (npurge > BACKGROUND_THREAD_NPAGES_THRESHOLD) {\n\t\t\tub = target;\n\t\t\tnpurge_ub = npurge;\n\t\t} else {\n\t\t\tlb = target;\n\t\t\tnpurge_lb = npurge;\n\t\t}\n\t\tassert(n_search++ < lg_floor(SMOOTHSTEP_NSTEPS) + 1);\n\t}\n\tinterval = decay_interval_ns * (ub + lb) / 2;\nlabel_done:\n\tinterval = (interval < BACKGROUND_THREAD_MIN_INTERVAL_NS) ?\n\t    BACKGROUND_THREAD_MIN_INTERVAL_NS : interval;\n\tmalloc_mutex_unlock(tsdn, &decay->mtx);\n\n\treturn interval;\n}\n\n/* Compute purge interval for background threads. */\nstatic uint64_t\narena_decay_compute_purge_interval(tsdn_t *tsdn, arena_t *arena) {\n\tuint64_t i1, i2;\n\ti1 = arena_decay_compute_purge_interval_impl(tsdn, &arena->decay_dirty,\n\t    &arena->extents_dirty);\n\tif (i1 == BACKGROUND_THREAD_MIN_INTERVAL_NS) {\n\t\treturn i1;\n\t}\n\ti2 = arena_decay_compute_purge_interval_impl(tsdn, &arena->decay_muzzy,\n\t    &arena->extents_muzzy);\n\n\treturn i1 < i2 ? i1 : i2;\n}\n\nstatic void\nbackground_thread_sleep(tsdn_t *tsdn, background_thread_info_t *info,\n    uint64_t interval) {\n\tif (config_stats) {\n\t\tinfo->tot_n_runs++;\n\t}\n\tinfo->npages_to_purge_new = 0;\n\n\tstruct timeval tv;\n\t/* Specific clock required by timedwait. */\n\tgettimeofday(&tv, NULL);\n\tnstime_t before_sleep;\n\tnstime_init2(&before_sleep, tv.tv_sec, tv.tv_usec * 1000);\n\n\tint ret;\n\tif (interval == BACKGROUND_THREAD_INDEFINITE_SLEEP) {\n\t\tassert(background_thread_indefinite_sleep(info));\n\t\tret = pthread_cond_wait(&info->cond, &info->mtx.lock);\n\t\tassert(ret == 0);\n\t} else {\n\t\tassert(interval >= BACKGROUND_THREAD_MIN_INTERVAL_NS &&\n\t\t    interval <= BACKGROUND_THREAD_INDEFINITE_SLEEP);\n\t\t/* We need malloc clock (can be different from tv). */\n\t\tnstime_t next_wakeup;\n\t\tnstime_init(&next_wakeup, 0);\n\t\tnstime_update(&next_wakeup);\n\t\tnstime_iadd(&next_wakeup, interval);\n\t\tassert(nstime_ns(&next_wakeup) <\n\t\t    BACKGROUND_THREAD_INDEFINITE_SLEEP);\n\t\tbackground_thread_wakeup_time_set(tsdn, info,\n\t\t    nstime_ns(&next_wakeup));\n\n\t\tnstime_t ts_wakeup;\n\t\tnstime_copy(&ts_wakeup, &before_sleep);\n\t\tnstime_iadd(&ts_wakeup, interval);\n\t\tstruct timespec ts;\n\t\tts.tv_sec = (size_t)nstime_sec(&ts_wakeup);\n\t\tts.tv_nsec = (size_t)nstime_nsec(&ts_wakeup);\n\n\t\tassert(!background_thread_indefinite_sleep(info));\n\t\tret = pthread_cond_timedwait(&info->cond, &info->mtx.lock, &ts);\n\t\tassert(ret == ETIMEDOUT || ret == 0);\n\t\tbackground_thread_wakeup_time_set(tsdn, info,\n\t\t    BACKGROUND_THREAD_INDEFINITE_SLEEP);\n\t}\n\tif (config_stats) {\n\t\tgettimeofday(&tv, NULL);\n\t\tnstime_t after_sleep;\n\t\tnstime_init2(&after_sleep, tv.tv_sec, tv.tv_usec * 1000);\n\t\tif (nstime_compare(&after_sleep, &before_sleep) > 0) {\n\t\t\tnstime_subtract(&after_sleep, &before_sleep);\n\t\t\tnstime_add(&info->tot_sleep_time, &after_sleep);\n\t\t}\n\t}\n}\n\nstatic bool\nbackground_thread_pause_check(tsdn_t *tsdn, background_thread_info_t *info) {\n\tif (unlikely(info->state == background_thread_paused)) {\n\t\tmalloc_mutex_unlock(tsdn, &info->mtx);\n\t\t/* Wait on global lock to update status. */\n\t\tmalloc_mutex_lock(tsdn, &background_thread_lock);\n\t\tmalloc_mutex_unlock(tsdn, &background_thread_lock);\n\t\tmalloc_mutex_lock(tsdn, &info->mtx);\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\nstatic inline void\nbackground_work_sleep_once(tsdn_t *tsdn, background_thread_info_t *info, unsigned ind) {\n\tuint64_t min_interval = BACKGROUND_THREAD_INDEFINITE_SLEEP;\n\tunsigned narenas = narenas_total_get();\n\n\tfor (unsigned i = ind; i < narenas; i += max_background_threads) {\n\t\tarena_t *arena = arena_get(tsdn, i, false);\n\t\tif (!arena) {\n\t\t\tcontinue;\n\t\t}\n\t\tarena_decay(tsdn, arena, true, false);\n\t\tif (min_interval == BACKGROUND_THREAD_MIN_INTERVAL_NS) {\n\t\t\t/* Min interval will be used. */\n\t\t\tcontinue;\n\t\t}\n\t\tuint64_t interval = arena_decay_compute_purge_interval(tsdn,\n\t\t    arena);\n\t\tassert(interval >= BACKGROUND_THREAD_MIN_INTERVAL_NS);\n\t\tif (min_interval > interval) {\n\t\t\tmin_interval = interval;\n\t\t}\n\t}\n\tbackground_thread_sleep(tsdn, info, min_interval);\n}\n\nstatic bool\nbackground_threads_disable_single(tsd_t *tsd, background_thread_info_t *info) {\n\tif (info == &background_thread_info[0]) {\n\t\tmalloc_mutex_assert_owner(tsd_tsdn(tsd),\n\t\t    &background_thread_lock);\n\t} else {\n\t\tmalloc_mutex_assert_not_owner(tsd_tsdn(tsd),\n\t\t    &background_thread_lock);\n\t}\n\n\tpre_reentrancy(tsd, NULL);\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);\n\tbool has_thread;\n\tassert(info->state != background_thread_paused);\n\tif (info->state == background_thread_started) {\n\t\thas_thread = true;\n\t\tinfo->state = background_thread_stopped;\n\t\tpthread_cond_signal(&info->cond);\n\t} else {\n\t\thas_thread = false;\n\t}\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);\n\n\tif (!has_thread) {\n\t\tpost_reentrancy(tsd);\n\t\treturn false;\n\t}\n\tvoid *ret;\n\tif (pthread_join(info->thread, &ret)) {\n\t\tpost_reentrancy(tsd);\n\t\treturn true;\n\t}\n\tassert(ret == NULL);\n\tn_background_threads--;\n\tpost_reentrancy(tsd);\n\n\treturn false;\n}\n\nstatic void *background_thread_entry(void *ind_arg);\n\nstatic int\nbackground_thread_create_signals_masked(pthread_t *thread,\n    const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg) {\n\t/*\n\t * Mask signals during thread creation so that the thread inherits\n\t * an empty signal set.\n\t */\n\tsigset_t set;\n\tsigfillset(&set);\n\tsigset_t oldset;\n\tint mask_err = pthread_sigmask(SIG_SETMASK, &set, &oldset);\n\tif (mask_err != 0) {\n\t\treturn mask_err;\n\t}\n\tint create_err = pthread_create_wrapper(thread, attr, start_routine,\n\t    arg);\n\t/*\n\t * Restore the signal mask.  Failure to restore the signal mask here\n\t * changes program behavior.\n\t */\n\tint restore_err = pthread_sigmask(SIG_SETMASK, &oldset, NULL);\n\tif (restore_err != 0) {\n\t\tmalloc_printf(\"<jemalloc>: background thread creation \"\n\t\t    \"failed (%d), and signal mask restoration failed \"\n\t\t    \"(%d)\\n\", create_err, restore_err);\n\t\tif (opt_abort) {\n\t\t\tabort();\n\t\t}\n\t}\n\treturn create_err;\n}\n\nstatic bool\ncheck_background_thread_creation(tsd_t *tsd, unsigned *n_created,\n    bool *created_threads) {\n\tbool ret = false;\n\tif (likely(*n_created == n_background_threads)) {\n\t\treturn ret;\n\t}\n\n\ttsdn_t *tsdn = tsd_tsdn(tsd);\n\tmalloc_mutex_unlock(tsdn, &background_thread_info[0].mtx);\n\tfor (unsigned i = 1; i < max_background_threads; i++) {\n\t\tif (created_threads[i]) {\n\t\t\tcontinue;\n\t\t}\n\t\tbackground_thread_info_t *info = &background_thread_info[i];\n\t\tmalloc_mutex_lock(tsdn, &info->mtx);\n\t\t/*\n\t\t * In case of the background_thread_paused state because of\n\t\t * arena reset, delay the creation.\n\t\t */\n\t\tbool create = (info->state == background_thread_started);\n\t\tmalloc_mutex_unlock(tsdn, &info->mtx);\n\t\tif (!create) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tpre_reentrancy(tsd, NULL);\n\t\tint err = background_thread_create_signals_masked(&info->thread,\n\t\t    NULL, background_thread_entry, (void *)(uintptr_t)i);\n\t\tpost_reentrancy(tsd);\n\n\t\tif (err == 0) {\n\t\t\t(*n_created)++;\n\t\t\tcreated_threads[i] = true;\n\t\t} else {\n\t\t\tmalloc_printf(\"<jemalloc>: background thread \"\n\t\t\t    \"creation failed (%d)\\n\", err);\n\t\t\tif (opt_abort) {\n\t\t\t\tabort();\n\t\t\t}\n\t\t}\n\t\t/* Return to restart the loop since we unlocked. */\n\t\tret = true;\n\t\tbreak;\n\t}\n\tmalloc_mutex_lock(tsdn, &background_thread_info[0].mtx);\n\n\treturn ret;\n}\n\nstatic void\nbackground_thread0_work(tsd_t *tsd) {\n\t/* Thread0 is also responsible for launching / terminating threads. */\n\tVARIABLE_ARRAY(bool, created_threads, max_background_threads);\n\tunsigned i;\n\tfor (i = 1; i < max_background_threads; i++) {\n\t\tcreated_threads[i] = false;\n\t}\n\t/* Start working, and create more threads when asked. */\n\tunsigned n_created = 1;\n\twhile (background_thread_info[0].state != background_thread_stopped) {\n\t\tif (background_thread_pause_check(tsd_tsdn(tsd),\n\t\t    &background_thread_info[0])) {\n\t\t\tcontinue;\n\t\t}\n\t\tif (check_background_thread_creation(tsd, &n_created,\n\t\t    (bool *)&created_threads)) {\n\t\t\tcontinue;\n\t\t}\n\t\tbackground_work_sleep_once(tsd_tsdn(tsd),\n\t\t    &background_thread_info[0], 0);\n\t}\n\n\t/*\n\t * Shut down other threads at exit.  Note that the ctl thread is holding\n\t * the global background_thread mutex (and is waiting) for us.\n\t */\n\tassert(!background_thread_enabled());\n\tfor (i = 1; i < max_background_threads; i++) {\n\t\tbackground_thread_info_t *info = &background_thread_info[i];\n\t\tassert(info->state != background_thread_paused);\n\t\tif (created_threads[i]) {\n\t\t\tbackground_threads_disable_single(tsd, info);\n\t\t} else {\n\t\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);\n\t\t\tif (info->state != background_thread_stopped) {\n\t\t\t\t/* The thread was not created. */\n\t\t\t\tassert(info->state ==\n\t\t\t\t    background_thread_started);\n\t\t\t\tn_background_threads--;\n\t\t\t\tinfo->state = background_thread_stopped;\n\t\t\t}\n\t\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);\n\t\t}\n\t}\n\tbackground_thread_info[0].state = background_thread_stopped;\n\tassert(n_background_threads == 1);\n}\n\nstatic void\nbackground_work(tsd_t *tsd, unsigned ind) {\n\tbackground_thread_info_t *info = &background_thread_info[ind];\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);\n\tbackground_thread_wakeup_time_set(tsd_tsdn(tsd), info,\n\t    BACKGROUND_THREAD_INDEFINITE_SLEEP);\n\tif (ind == 0) {\n\t\tbackground_thread0_work(tsd);\n\t} else {\n\t\twhile (info->state != background_thread_stopped) {\n\t\t\tif (background_thread_pause_check(tsd_tsdn(tsd),\n\t\t\t    info)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tbackground_work_sleep_once(tsd_tsdn(tsd), info, ind);\n\t\t}\n\t}\n\tassert(info->state == background_thread_stopped);\n\tbackground_thread_wakeup_time_set(tsd_tsdn(tsd), info, 0);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);\n}\n\nstatic void *\nbackground_thread_entry(void *ind_arg) {\n\tunsigned thread_ind = (unsigned)(uintptr_t)ind_arg;\n\tassert(thread_ind < max_background_threads);\n#ifdef JEMALLOC_HAVE_PTHREAD_SETNAME_NP\n\tpthread_setname_np(pthread_self(), \"jemalloc_bg_thd\");\n#elif defined(__FreeBSD__)\n\tpthread_set_name_np(pthread_self(), \"jemalloc_bg_thd\");\n#endif\n\tif (opt_percpu_arena != percpu_arena_disabled) {\n\t\tset_current_thread_affinity((int)thread_ind);\n\t}\n\t/*\n\t * Start periodic background work.  We use internal tsd which avoids\n\t * side effects, for example triggering new arena creation (which in\n\t * turn triggers another background thread creation).\n\t */\n\tbackground_work(tsd_internal_fetch(), thread_ind);\n\tassert(pthread_equal(pthread_self(),\n\t    background_thread_info[thread_ind].thread));\n\n\treturn NULL;\n}\n\nstatic void\nbackground_thread_init(tsd_t *tsd, background_thread_info_t *info) {\n\tmalloc_mutex_assert_owner(tsd_tsdn(tsd), &background_thread_lock);\n\tinfo->state = background_thread_started;\n\tbackground_thread_info_init(tsd_tsdn(tsd), info);\n\tn_background_threads++;\n}\n\nstatic bool\nbackground_thread_create_locked(tsd_t *tsd, unsigned arena_ind) {\n\tassert(have_background_thread);\n\tmalloc_mutex_assert_owner(tsd_tsdn(tsd), &background_thread_lock);\n\n\t/* We create at most NCPUs threads. */\n\tsize_t thread_ind = arena_ind % max_background_threads;\n\tbackground_thread_info_t *info = &background_thread_info[thread_ind];\n\n\tbool need_new_thread;\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);\n\tneed_new_thread = background_thread_enabled() &&\n\t    (info->state == background_thread_stopped);\n\tif (need_new_thread) {\n\t\tbackground_thread_init(tsd, info);\n\t}\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);\n\tif (!need_new_thread) {\n\t\treturn false;\n\t}\n\tif (arena_ind != 0) {\n\t\t/* Threads are created asynchronously by Thread 0. */\n\t\tbackground_thread_info_t *t0 = &background_thread_info[0];\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &t0->mtx);\n\t\tassert(t0->state == background_thread_started);\n\t\tpthread_cond_signal(&t0->cond);\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &t0->mtx);\n\n\t\treturn false;\n\t}\n\n\tpre_reentrancy(tsd, NULL);\n\t/*\n\t * To avoid complications (besides reentrancy), create internal\n\t * background threads with the underlying pthread_create.\n\t */\n\tint err = background_thread_create_signals_masked(&info->thread, NULL,\n\t    background_thread_entry, (void *)thread_ind);\n\tpost_reentrancy(tsd);\n\n\tif (err != 0) {\n\t\tmalloc_printf(\"<jemalloc>: arena 0 background thread creation \"\n\t\t    \"failed (%d)\\n\", err);\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);\n\t\tinfo->state = background_thread_stopped;\n\t\tn_background_threads--;\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);\n\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\n/* Create a new background thread if needed. */\nbool\nbackground_thread_create(tsd_t *tsd, unsigned arena_ind) {\n\tassert(have_background_thread);\n\n\tbool ret;\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &background_thread_lock);\n\tret = background_thread_create_locked(tsd, arena_ind);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &background_thread_lock);\n\n\treturn ret;\n}\n\nbool\nbackground_threads_enable(tsd_t *tsd) {\n\tassert(n_background_threads == 0);\n\tassert(background_thread_enabled());\n\tmalloc_mutex_assert_owner(tsd_tsdn(tsd), &background_thread_lock);\n\n\tVARIABLE_ARRAY(bool, marked, max_background_threads);\n\tunsigned i, nmarked;\n\tfor (i = 0; i < max_background_threads; i++) {\n\t\tmarked[i] = false;\n\t}\n\tnmarked = 0;\n\t/* Thread 0 is required and created at the end. */\n\tmarked[0] = true;\n\t/* Mark the threads we need to create for thread 0. */\n\tunsigned n = narenas_total_get();\n\tfor (i = 1; i < n; i++) {\n\t\tif (marked[i % max_background_threads] ||\n\t\t    arena_get(tsd_tsdn(tsd), i, false) == NULL) {\n\t\t\tcontinue;\n\t\t}\n\t\tbackground_thread_info_t *info = &background_thread_info[\n\t\t    i % max_background_threads];\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);\n\t\tassert(info->state == background_thread_stopped);\n\t\tbackground_thread_init(tsd, info);\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);\n\t\tmarked[i % max_background_threads] = true;\n\t\tif (++nmarked == max_background_threads) {\n\t\t\tbreak;\n\t\t}\n\t}\n\n\treturn background_thread_create_locked(tsd, 0);\n}\n\nbool\nbackground_threads_disable(tsd_t *tsd) {\n\tassert(!background_thread_enabled());\n\tmalloc_mutex_assert_owner(tsd_tsdn(tsd), &background_thread_lock);\n\n\t/* Thread 0 will be responsible for terminating other threads. */\n\tif (background_threads_disable_single(tsd,\n\t    &background_thread_info[0])) {\n\t\treturn true;\n\t}\n\tassert(n_background_threads == 0);\n\n\treturn false;\n}\n\n/* Check if we need to signal the background thread early. */\nvoid\nbackground_thread_interval_check(tsdn_t *tsdn, arena_t *arena,\n    arena_decay_t *decay, size_t npages_new) {\n\tbackground_thread_info_t *info = arena_background_thread_info_get(\n\t    arena);\n\tif (malloc_mutex_trylock(tsdn, &info->mtx)) {\n\t\t/*\n\t\t * Background thread may hold the mutex for a long period of\n\t\t * time.  We'd like to avoid the variance on application\n\t\t * threads.  So keep this non-blocking, and leave the work to a\n\t\t * future epoch.\n\t\t */\n\t\treturn;\n\t}\n\n\tif (info->state != background_thread_started) {\n\t\tgoto label_done;\n\t}\n\tif (malloc_mutex_trylock(tsdn, &decay->mtx)) {\n\t\tgoto label_done;\n\t}\n\n\tssize_t decay_time = atomic_load_zd(&decay->time_ms, ATOMIC_RELAXED);\n\tif (decay_time <= 0) {\n\t\t/* Purging is eagerly done or disabled currently. */\n\t\tgoto label_done_unlock2;\n\t}\n\tuint64_t decay_interval_ns = nstime_ns(&decay->interval);\n\tassert(decay_interval_ns > 0);\n\n\tnstime_t diff;\n\tnstime_init(&diff, background_thread_wakeup_time_get(info));\n\tif (nstime_compare(&diff, &decay->epoch) <= 0) {\n\t\tgoto label_done_unlock2;\n\t}\n\tnstime_subtract(&diff, &decay->epoch);\n\tif (nstime_ns(&diff) < BACKGROUND_THREAD_MIN_INTERVAL_NS) {\n\t\tgoto label_done_unlock2;\n\t}\n\n\tif (npages_new > 0) {\n\t\tsize_t n_epoch = (size_t)(nstime_ns(&diff) / decay_interval_ns);\n\t\t/*\n\t\t * Compute how many new pages we would need to purge by the next\n\t\t * wakeup, which is used to determine if we should signal the\n\t\t * background thread.\n\t\t */\n\t\tuint64_t npurge_new;\n\t\tif (n_epoch >= SMOOTHSTEP_NSTEPS) {\n\t\t\tnpurge_new = npages_new;\n\t\t} else {\n\t\t\tuint64_t h_steps_max = h_steps[SMOOTHSTEP_NSTEPS - 1];\n\t\t\tassert(h_steps_max >=\n\t\t\t    h_steps[SMOOTHSTEP_NSTEPS - 1 - n_epoch]);\n\t\t\tnpurge_new = npages_new * (h_steps_max -\n\t\t\t    h_steps[SMOOTHSTEP_NSTEPS - 1 - n_epoch]);\n\t\t\tnpurge_new >>= SMOOTHSTEP_BFP;\n\t\t}\n\t\tinfo->npages_to_purge_new += npurge_new;\n\t}\n\n\tbool should_signal;\n\tif (info->npages_to_purge_new > BACKGROUND_THREAD_NPAGES_THRESHOLD) {\n\t\tshould_signal = true;\n\t} else if (unlikely(background_thread_indefinite_sleep(info)) &&\n\t    (extents_npages_get(&arena->extents_dirty) > 0 ||\n\t    extents_npages_get(&arena->extents_muzzy) > 0 ||\n\t    info->npages_to_purge_new > 0)) {\n\t\tshould_signal = true;\n\t} else {\n\t\tshould_signal = false;\n\t}\n\n\tif (should_signal) {\n\t\tinfo->npages_to_purge_new = 0;\n\t\tpthread_cond_signal(&info->cond);\n\t}\nlabel_done_unlock2:\n\tmalloc_mutex_unlock(tsdn, &decay->mtx);\nlabel_done:\n\tmalloc_mutex_unlock(tsdn, &info->mtx);\n}\n\nvoid\nbackground_thread_prefork0(tsdn_t *tsdn) {\n\tmalloc_mutex_prefork(tsdn, &background_thread_lock);\n\tbackground_thread_enabled_at_fork = background_thread_enabled();\n}\n\nvoid\nbackground_thread_prefork1(tsdn_t *tsdn) {\n\tfor (unsigned i = 0; i < max_background_threads; i++) {\n\t\tmalloc_mutex_prefork(tsdn, &background_thread_info[i].mtx);\n\t}\n}\n\nvoid\nbackground_thread_postfork_parent(tsdn_t *tsdn) {\n\tfor (unsigned i = 0; i < max_background_threads; i++) {\n\t\tmalloc_mutex_postfork_parent(tsdn,\n\t\t    &background_thread_info[i].mtx);\n\t}\n\tmalloc_mutex_postfork_parent(tsdn, &background_thread_lock);\n}\n\nvoid\nbackground_thread_postfork_child(tsdn_t *tsdn) {\n\tfor (unsigned i = 0; i < max_background_threads; i++) {\n\t\tmalloc_mutex_postfork_child(tsdn,\n\t\t    &background_thread_info[i].mtx);\n\t}\n\tmalloc_mutex_postfork_child(tsdn, &background_thread_lock);\n\tif (!background_thread_enabled_at_fork) {\n\t\treturn;\n\t}\n\n\t/* Clear background_thread state (reset to disabled for child). */\n\tmalloc_mutex_lock(tsdn, &background_thread_lock);\n\tn_background_threads = 0;\n\tbackground_thread_enabled_set(tsdn, false);\n\tfor (unsigned i = 0; i < max_background_threads; i++) {\n\t\tbackground_thread_info_t *info = &background_thread_info[i];\n\t\tmalloc_mutex_lock(tsdn, &info->mtx);\n\t\tinfo->state = background_thread_stopped;\n\t\tint ret = pthread_cond_init(&info->cond, NULL);\n\t\tassert(ret == 0);\n\t\tbackground_thread_info_init(tsdn, info);\n\t\tmalloc_mutex_unlock(tsdn, &info->mtx);\n\t}\n\tmalloc_mutex_unlock(tsdn, &background_thread_lock);\n}\n\nbool\nbackground_thread_stats_read(tsdn_t *tsdn, background_thread_stats_t *stats) {\n\tassert(config_stats);\n\tmalloc_mutex_lock(tsdn, &background_thread_lock);\n\tif (!background_thread_enabled()) {\n\t\tmalloc_mutex_unlock(tsdn, &background_thread_lock);\n\t\treturn true;\n\t}\n\n\tstats->num_threads = n_background_threads;\n\tuint64_t num_runs = 0;\n\tnstime_init(&stats->run_interval, 0);\n\tfor (unsigned i = 0; i < max_background_threads; i++) {\n\t\tbackground_thread_info_t *info = &background_thread_info[i];\n\t\tif (malloc_mutex_trylock(tsdn, &info->mtx)) {\n\t\t\t/*\n\t\t\t * Each background thread run may take a long time;\n\t\t\t * avoid waiting on the stats if the thread is active.\n\t\t\t */\n\t\t\tcontinue;\n\t\t}\n\t\tif (info->state != background_thread_stopped) {\n\t\t\tnum_runs += info->tot_n_runs;\n\t\t\tnstime_add(&stats->run_interval, &info->tot_sleep_time);\n\t\t}\n\t\tmalloc_mutex_unlock(tsdn, &info->mtx);\n\t}\n\tstats->num_runs = num_runs;\n\tif (num_runs > 0) {\n\t\tnstime_idivide(&stats->run_interval, num_runs);\n\t}\n\tmalloc_mutex_unlock(tsdn, &background_thread_lock);\n\n\treturn false;\n}\n\n#undef BACKGROUND_THREAD_NPAGES_THRESHOLD\n#undef BILLION\n#undef BACKGROUND_THREAD_MIN_INTERVAL_NS\n\n#ifdef JEMALLOC_HAVE_DLSYM\n#include <dlfcn.h>\n#endif\n\nstatic bool\npthread_create_fptr_init(void) {\n\tif (pthread_create_fptr != NULL) {\n\t\treturn false;\n\t}\n\t/*\n\t * Try the next symbol first, because 1) when use lazy_lock we have a\n\t * wrapper for pthread_create; and 2) application may define its own\n\t * wrapper as well (and can call malloc within the wrapper).\n\t */\n#ifdef JEMALLOC_HAVE_DLSYM\n\tpthread_create_fptr = dlsym(RTLD_NEXT, \"pthread_create\");\n#else\n\tpthread_create_fptr = NULL;\n#endif\n\tif (pthread_create_fptr == NULL) {\n\t\tif (config_lazy_lock) {\n\t\t\tmalloc_write(\"<jemalloc>: Error in dlsym(RTLD_NEXT, \"\n\t\t\t    \"\\\"pthread_create\\\")\\n\");\n\t\t\tabort();\n\t\t} else {\n\t\t\t/* Fall back to the default symbol. */\n\t\t\tpthread_create_fptr = pthread_create;\n\t\t}\n\t}\n\n\treturn false;\n}\n\n/*\n * When lazy lock is enabled, we need to make sure setting isthreaded before\n * taking any background_thread locks.  This is called early in ctl (instead of\n * wait for the pthread_create calls to trigger) because the mutex is required\n * before creating background threads.\n */\nvoid\nbackground_thread_ctl_init(tsdn_t *tsdn) {\n\tmalloc_mutex_assert_not_owner(tsdn, &background_thread_lock);\n#ifdef JEMALLOC_PTHREAD_CREATE_WRAPPER\n\tpthread_create_fptr_init();\n\tpthread_create_wrapper_init();\n#endif\n}\n\n#endif /* defined(JEMALLOC_BACKGROUND_THREAD) */\n\nbool\nbackground_thread_boot0(void) {\n\tif (!have_background_thread && opt_background_thread) {\n\t\tmalloc_printf(\"<jemalloc>: option background_thread currently \"\n\t\t    \"supports pthread only\\n\");\n\t\treturn true;\n\t}\n#ifdef JEMALLOC_PTHREAD_CREATE_WRAPPER\n\tif ((config_lazy_lock || opt_background_thread) &&\n\t    pthread_create_fptr_init()) {\n\t\treturn true;\n\t}\n#endif\n\treturn false;\n}\n\nbool\nbackground_thread_boot1(tsdn_t *tsdn) {\n#ifdef JEMALLOC_BACKGROUND_THREAD\n\tassert(have_background_thread);\n\tassert(narenas_total_get() > 0);\n\n\tif (opt_max_background_threads > MAX_BACKGROUND_THREAD_LIMIT) {\n\t\topt_max_background_threads = DEFAULT_NUM_BACKGROUND_THREAD;\n\t}\n\tmax_background_threads = opt_max_background_threads;\n\n\tbackground_thread_enabled_set(tsdn, opt_background_thread);\n\tif (malloc_mutex_init(&background_thread_lock,\n\t    \"background_thread_global\",\n\t    WITNESS_RANK_BACKGROUND_THREAD_GLOBAL,\n\t    malloc_mutex_rank_exclusive)) {\n\t\treturn true;\n\t}\n\n\tbackground_thread_info = (background_thread_info_t *)base_alloc(tsdn,\n\t    b0get(), opt_max_background_threads *\n\t    sizeof(background_thread_info_t), CACHELINE);\n\tif (background_thread_info == NULL) {\n\t\treturn true;\n\t}\n\n\tfor (unsigned i = 0; i < max_background_threads; i++) {\n\t\tbackground_thread_info_t *info = &background_thread_info[i];\n\t\t/* Thread mutex is rank_inclusive because of thread0. */\n\t\tif (malloc_mutex_init(&info->mtx, \"background_thread\",\n\t\t    WITNESS_RANK_BACKGROUND_THREAD,\n\t\t    malloc_mutex_address_ordered)) {\n\t\t\treturn true;\n\t\t}\n\t\tif (pthread_cond_init(&info->cond, NULL)) {\n\t\t\treturn true;\n\t\t}\n\t\tmalloc_mutex_lock(tsdn, &info->mtx);\n\t\tinfo->state = background_thread_stopped;\n\t\tbackground_thread_info_init(tsdn, info);\n\t\tmalloc_mutex_unlock(tsdn, &info->mtx);\n\t}\n#endif\n\n\treturn false;\n}\n"
  },
  {
    "path": "deps/jemalloc/src/base.c",
    "content": "#define JEMALLOC_BASE_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/extent_mmap.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/sz.h\"\n\n/******************************************************************************/\n/* Data. */\n\nstatic base_t *b0;\n\nmetadata_thp_mode_t opt_metadata_thp = METADATA_THP_DEFAULT;\n\nconst char *metadata_thp_mode_names[] = {\n\t\"disabled\",\n\t\"auto\",\n\t\"always\"\n};\n\n/******************************************************************************/\n\nstatic inline bool\nmetadata_thp_madvise(void) {\n\treturn (metadata_thp_enabled() &&\n\t    (init_system_thp_mode == thp_mode_default));\n}\n\nstatic void *\nbase_map(tsdn_t *tsdn, extent_hooks_t *extent_hooks, unsigned ind, size_t size) {\n\tvoid *addr;\n\tbool zero = true;\n\tbool commit = true;\n\n\t/* Use huge page sizes and alignment regardless of opt_metadata_thp. */\n\tassert(size == HUGEPAGE_CEILING(size));\n\tsize_t alignment = HUGEPAGE;\n\tif (extent_hooks == &extent_hooks_default) {\n\t\taddr = extent_alloc_mmap(NULL, size, alignment, &zero, &commit);\n\t} else {\n\t\t/* No arena context as we are creating new arenas. */\n\t\ttsd_t *tsd = tsdn_null(tsdn) ? tsd_fetch() : tsdn_tsd(tsdn);\n\t\tpre_reentrancy(tsd, NULL);\n\t\taddr = extent_hooks->alloc(extent_hooks, NULL, size, alignment,\n\t\t    &zero, &commit, ind);\n\t\tpost_reentrancy(tsd);\n\t}\n\n\treturn addr;\n}\n\nstatic void\nbase_unmap(tsdn_t *tsdn, extent_hooks_t *extent_hooks, unsigned ind, void *addr,\n    size_t size) {\n\t/*\n\t * Cascade through dalloc, decommit, purge_forced, and purge_lazy,\n\t * stopping at first success.  This cascade is performed for consistency\n\t * with the cascade in extent_dalloc_wrapper() because an application's\n\t * custom hooks may not support e.g. dalloc.  This function is only ever\n\t * called as a side effect of arena destruction, so although it might\n\t * seem pointless to do anything besides dalloc here, the application\n\t * may in fact want the end state of all associated virtual memory to be\n\t * in some consistent-but-allocated state.\n\t */\n\tif (extent_hooks == &extent_hooks_default) {\n\t\tif (!extent_dalloc_mmap(addr, size)) {\n\t\t\tgoto label_done;\n\t\t}\n\t\tif (!pages_decommit(addr, size)) {\n\t\t\tgoto label_done;\n\t\t}\n\t\tif (!pages_purge_forced(addr, size)) {\n\t\t\tgoto label_done;\n\t\t}\n\t\tif (!pages_purge_lazy(addr, size)) {\n\t\t\tgoto label_done;\n\t\t}\n\t\t/* Nothing worked.  This should never happen. */\n\t\tnot_reached();\n\t} else {\n\t\ttsd_t *tsd = tsdn_null(tsdn) ? tsd_fetch() : tsdn_tsd(tsdn);\n\t\tpre_reentrancy(tsd, NULL);\n\t\tif (extent_hooks->dalloc != NULL &&\n\t\t    !extent_hooks->dalloc(extent_hooks, addr, size, true,\n\t\t    ind)) {\n\t\t\tgoto label_post_reentrancy;\n\t\t}\n\t\tif (extent_hooks->decommit != NULL &&\n\t\t    !extent_hooks->decommit(extent_hooks, addr, size, 0, size,\n\t\t    ind)) {\n\t\t\tgoto label_post_reentrancy;\n\t\t}\n\t\tif (extent_hooks->purge_forced != NULL &&\n\t\t    !extent_hooks->purge_forced(extent_hooks, addr, size, 0,\n\t\t    size, ind)) {\n\t\t\tgoto label_post_reentrancy;\n\t\t}\n\t\tif (extent_hooks->purge_lazy != NULL &&\n\t\t    !extent_hooks->purge_lazy(extent_hooks, addr, size, 0, size,\n\t\t    ind)) {\n\t\t\tgoto label_post_reentrancy;\n\t\t}\n\t\t/* Nothing worked.  That's the application's problem. */\n\tlabel_post_reentrancy:\n\t\tpost_reentrancy(tsd);\n\t}\nlabel_done:\n\tif (metadata_thp_madvise()) {\n\t\t/* Set NOHUGEPAGE after unmap to avoid kernel defrag. */\n\t\tassert(((uintptr_t)addr & HUGEPAGE_MASK) == 0 &&\n\t\t    (size & HUGEPAGE_MASK) == 0);\n\t\tpages_nohuge(addr, size);\n\t}\n}\n\nstatic void\nbase_extent_init(size_t *extent_sn_next, extent_t *extent, void *addr,\n    size_t size) {\n\tsize_t sn;\n\n\tsn = *extent_sn_next;\n\t(*extent_sn_next)++;\n\n\textent_binit(extent, addr, size, sn);\n}\n\nstatic size_t\nbase_get_num_blocks(base_t *base, bool with_new_block) {\n\tbase_block_t *b = base->blocks;\n\tassert(b != NULL);\n\n\tsize_t n_blocks = with_new_block ? 2 : 1;\n\twhile (b->next != NULL) {\n\t\tn_blocks++;\n\t\tb = b->next;\n\t}\n\n\treturn n_blocks;\n}\n\nstatic void\nbase_auto_thp_switch(tsdn_t *tsdn, base_t *base) {\n\tassert(opt_metadata_thp == metadata_thp_auto);\n\tmalloc_mutex_assert_owner(tsdn, &base->mtx);\n\tif (base->auto_thp_switched) {\n\t\treturn;\n\t}\n\t/* Called when adding a new block. */\n\tbool should_switch;\n\tif (base_ind_get(base) != 0) {\n\t\tshould_switch = (base_get_num_blocks(base, true) ==\n\t\t    BASE_AUTO_THP_THRESHOLD);\n\t} else {\n\t\tshould_switch = (base_get_num_blocks(base, true) ==\n\t\t    BASE_AUTO_THP_THRESHOLD_A0);\n\t}\n\tif (!should_switch) {\n\t\treturn;\n\t}\n\n\tbase->auto_thp_switched = true;\n\tassert(!config_stats || base->n_thp == 0);\n\t/* Make the initial blocks THP lazily. */\n\tbase_block_t *block = base->blocks;\n\twhile (block != NULL) {\n\t\tassert((block->size & HUGEPAGE_MASK) == 0);\n\t\tpages_huge(block, block->size);\n\t\tif (config_stats) {\n\t\t\tbase->n_thp += HUGEPAGE_CEILING(block->size -\n\t\t\t    extent_bsize_get(&block->extent)) >> LG_HUGEPAGE;\n\t\t}\n\t\tblock = block->next;\n\t\tassert(block == NULL || (base_ind_get(base) == 0));\n\t}\n}\n\nstatic void *\nbase_extent_bump_alloc_helper(extent_t *extent, size_t *gap_size, size_t size,\n    size_t alignment) {\n\tvoid *ret;\n\n\tassert(alignment == ALIGNMENT_CEILING(alignment, QUANTUM));\n\tassert(size == ALIGNMENT_CEILING(size, alignment));\n\n\t*gap_size = ALIGNMENT_CEILING((uintptr_t)extent_addr_get(extent),\n\t    alignment) - (uintptr_t)extent_addr_get(extent);\n\tret = (void *)((uintptr_t)extent_addr_get(extent) + *gap_size);\n\tassert(extent_bsize_get(extent) >= *gap_size + size);\n\textent_binit(extent, (void *)((uintptr_t)extent_addr_get(extent) +\n\t    *gap_size + size), extent_bsize_get(extent) - *gap_size - size,\n\t    extent_sn_get(extent));\n\treturn ret;\n}\n\nstatic void\nbase_extent_bump_alloc_post(base_t *base, extent_t *extent, size_t gap_size,\n    void *addr, size_t size) {\n\tif (extent_bsize_get(extent) > 0) {\n\t\t/*\n\t\t * Compute the index for the largest size class that does not\n\t\t * exceed extent's size.\n\t\t */\n\t\tszind_t index_floor =\n\t\t    sz_size2index(extent_bsize_get(extent) + 1) - 1;\n\t\textent_heap_insert(&base->avail[index_floor], extent);\n\t}\n\n\tif (config_stats) {\n\t\tbase->allocated += size;\n\t\t/*\n\t\t * Add one PAGE to base_resident for every page boundary that is\n\t\t * crossed by the new allocation. Adjust n_thp similarly when\n\t\t * metadata_thp is enabled.\n\t\t */\n\t\tbase->resident += PAGE_CEILING((uintptr_t)addr + size) -\n\t\t    PAGE_CEILING((uintptr_t)addr - gap_size);\n\t\tassert(base->allocated <= base->resident);\n\t\tassert(base->resident <= base->mapped);\n\t\tif (metadata_thp_madvise() && (opt_metadata_thp ==\n\t\t    metadata_thp_always || base->auto_thp_switched)) {\n\t\t\tbase->n_thp += (HUGEPAGE_CEILING((uintptr_t)addr + size)\n\t\t\t    - HUGEPAGE_CEILING((uintptr_t)addr - gap_size)) >>\n\t\t\t    LG_HUGEPAGE;\n\t\t\tassert(base->mapped >= base->n_thp << LG_HUGEPAGE);\n\t\t}\n\t}\n}\n\nstatic void *\nbase_extent_bump_alloc(base_t *base, extent_t *extent, size_t size,\n    size_t alignment) {\n\tvoid *ret;\n\tsize_t gap_size;\n\n\tret = base_extent_bump_alloc_helper(extent, &gap_size, size, alignment);\n\tbase_extent_bump_alloc_post(base, extent, gap_size, ret, size);\n\treturn ret;\n}\n\n/*\n * Allocate a block of virtual memory that is large enough to start with a\n * base_block_t header, followed by an object of specified size and alignment.\n * On success a pointer to the initialized base_block_t header is returned.\n */\nstatic base_block_t *\nbase_block_alloc(tsdn_t *tsdn, base_t *base, extent_hooks_t *extent_hooks,\n    unsigned ind, pszind_t *pind_last, size_t *extent_sn_next, size_t size,\n    size_t alignment) {\n\talignment = ALIGNMENT_CEILING(alignment, QUANTUM);\n\tsize_t usize = ALIGNMENT_CEILING(size, alignment);\n\tsize_t header_size = sizeof(base_block_t);\n\tsize_t gap_size = ALIGNMENT_CEILING(header_size, alignment) -\n\t    header_size;\n\t/*\n\t * Create increasingly larger blocks in order to limit the total number\n\t * of disjoint virtual memory ranges.  Choose the next size in the page\n\t * size class series (skipping size classes that are not a multiple of\n\t * HUGEPAGE), or a size large enough to satisfy the requested size and\n\t * alignment, whichever is larger.\n\t */\n\tsize_t min_block_size = HUGEPAGE_CEILING(sz_psz2u(header_size + gap_size\n\t    + usize));\n\tpszind_t pind_next = (*pind_last + 1 < sz_psz2ind(SC_LARGE_MAXCLASS)) ?\n\t    *pind_last + 1 : *pind_last;\n\tsize_t next_block_size = HUGEPAGE_CEILING(sz_pind2sz(pind_next));\n\tsize_t block_size = (min_block_size > next_block_size) ? min_block_size\n\t    : next_block_size;\n\tbase_block_t *block = (base_block_t *)base_map(tsdn, extent_hooks, ind,\n\t    block_size);\n\tif (block == NULL) {\n\t\treturn NULL;\n\t}\n\n\tif (metadata_thp_madvise()) {\n\t\tvoid *addr = (void *)block;\n\t\tassert(((uintptr_t)addr & HUGEPAGE_MASK) == 0 &&\n\t\t    (block_size & HUGEPAGE_MASK) == 0);\n\t\tif (opt_metadata_thp == metadata_thp_always) {\n\t\t\tpages_huge(addr, block_size);\n\t\t} else if (opt_metadata_thp == metadata_thp_auto &&\n\t\t    base != NULL) {\n\t\t\t/* base != NULL indicates this is not a new base. */\n\t\t\tmalloc_mutex_lock(tsdn, &base->mtx);\n\t\t\tbase_auto_thp_switch(tsdn, base);\n\t\t\tif (base->auto_thp_switched) {\n\t\t\t\tpages_huge(addr, block_size);\n\t\t\t}\n\t\t\tmalloc_mutex_unlock(tsdn, &base->mtx);\n\t\t}\n\t}\n\n\t*pind_last = sz_psz2ind(block_size);\n\tblock->size = block_size;\n\tblock->next = NULL;\n\tassert(block_size >= header_size);\n\tbase_extent_init(extent_sn_next, &block->extent,\n\t    (void *)((uintptr_t)block + header_size), block_size - header_size);\n\treturn block;\n}\n\n/*\n * Allocate an extent that is at least as large as specified size, with\n * specified alignment.\n */\nstatic extent_t *\nbase_extent_alloc(tsdn_t *tsdn, base_t *base, size_t size, size_t alignment) {\n\tmalloc_mutex_assert_owner(tsdn, &base->mtx);\n\n\textent_hooks_t *extent_hooks = base_extent_hooks_get(base);\n\t/*\n\t * Drop mutex during base_block_alloc(), because an extent hook will be\n\t * called.\n\t */\n\tmalloc_mutex_unlock(tsdn, &base->mtx);\n\tbase_block_t *block = base_block_alloc(tsdn, base, extent_hooks,\n\t    base_ind_get(base), &base->pind_last, &base->extent_sn_next, size,\n\t    alignment);\n\tmalloc_mutex_lock(tsdn, &base->mtx);\n\tif (block == NULL) {\n\t\treturn NULL;\n\t}\n\tblock->next = base->blocks;\n\tbase->blocks = block;\n\tif (config_stats) {\n\t\tbase->allocated += sizeof(base_block_t);\n\t\tbase->resident += PAGE_CEILING(sizeof(base_block_t));\n\t\tbase->mapped += block->size;\n\t\tif (metadata_thp_madvise() &&\n\t\t    !(opt_metadata_thp == metadata_thp_auto\n\t\t      && !base->auto_thp_switched)) {\n\t\t\tassert(base->n_thp > 0);\n\t\t\tbase->n_thp += HUGEPAGE_CEILING(sizeof(base_block_t)) >>\n\t\t\t    LG_HUGEPAGE;\n\t\t}\n\t\tassert(base->allocated <= base->resident);\n\t\tassert(base->resident <= base->mapped);\n\t\tassert(base->n_thp << LG_HUGEPAGE <= base->mapped);\n\t}\n\treturn &block->extent;\n}\n\nbase_t *\nb0get(void) {\n\treturn b0;\n}\n\nbase_t *\nbase_new(tsdn_t *tsdn, unsigned ind, extent_hooks_t *extent_hooks) {\n\tpszind_t pind_last = 0;\n\tsize_t extent_sn_next = 0;\n\tbase_block_t *block = base_block_alloc(tsdn, NULL, extent_hooks, ind,\n\t    &pind_last, &extent_sn_next, sizeof(base_t), QUANTUM);\n\tif (block == NULL) {\n\t\treturn NULL;\n\t}\n\n\tsize_t gap_size;\n\tsize_t base_alignment = CACHELINE;\n\tsize_t base_size = ALIGNMENT_CEILING(sizeof(base_t), base_alignment);\n\tbase_t *base = (base_t *)base_extent_bump_alloc_helper(&block->extent,\n\t    &gap_size, base_size, base_alignment);\n\tbase->ind = ind;\n\tatomic_store_p(&base->extent_hooks, extent_hooks, ATOMIC_RELAXED);\n\tif (malloc_mutex_init(&base->mtx, \"base\", WITNESS_RANK_BASE,\n\t    malloc_mutex_rank_exclusive)) {\n\t\tbase_unmap(tsdn, extent_hooks, ind, block, block->size);\n\t\treturn NULL;\n\t}\n\tbase->pind_last = pind_last;\n\tbase->extent_sn_next = extent_sn_next;\n\tbase->blocks = block;\n\tbase->auto_thp_switched = false;\n\tfor (szind_t i = 0; i < SC_NSIZES; i++) {\n\t\textent_heap_new(&base->avail[i]);\n\t}\n\tif (config_stats) {\n\t\tbase->allocated = sizeof(base_block_t);\n\t\tbase->resident = PAGE_CEILING(sizeof(base_block_t));\n\t\tbase->mapped = block->size;\n\t\tbase->n_thp = (opt_metadata_thp == metadata_thp_always) &&\n\t\t    metadata_thp_madvise() ? HUGEPAGE_CEILING(sizeof(base_block_t))\n\t\t    >> LG_HUGEPAGE : 0;\n\t\tassert(base->allocated <= base->resident);\n\t\tassert(base->resident <= base->mapped);\n\t\tassert(base->n_thp << LG_HUGEPAGE <= base->mapped);\n\t}\n\tbase_extent_bump_alloc_post(base, &block->extent, gap_size, base,\n\t    base_size);\n\n\treturn base;\n}\n\nvoid\nbase_delete(tsdn_t *tsdn, base_t *base) {\n\textent_hooks_t *extent_hooks = base_extent_hooks_get(base);\n\tbase_block_t *next = base->blocks;\n\tdo {\n\t\tbase_block_t *block = next;\n\t\tnext = block->next;\n\t\tbase_unmap(tsdn, extent_hooks, base_ind_get(base), block,\n\t\t    block->size);\n\t} while (next != NULL);\n}\n\nextent_hooks_t *\nbase_extent_hooks_get(base_t *base) {\n\treturn (extent_hooks_t *)atomic_load_p(&base->extent_hooks,\n\t    ATOMIC_ACQUIRE);\n}\n\nextent_hooks_t *\nbase_extent_hooks_set(base_t *base, extent_hooks_t *extent_hooks) {\n\textent_hooks_t *old_extent_hooks = base_extent_hooks_get(base);\n\tatomic_store_p(&base->extent_hooks, extent_hooks, ATOMIC_RELEASE);\n\treturn old_extent_hooks;\n}\n\nstatic void *\nbase_alloc_impl(tsdn_t *tsdn, base_t *base, size_t size, size_t alignment,\n    size_t *esn) {\n\talignment = QUANTUM_CEILING(alignment);\n\tsize_t usize = ALIGNMENT_CEILING(size, alignment);\n\tsize_t asize = usize + alignment - QUANTUM;\n\n\textent_t *extent = NULL;\n\tmalloc_mutex_lock(tsdn, &base->mtx);\n\tfor (szind_t i = sz_size2index(asize); i < SC_NSIZES; i++) {\n\t\textent = extent_heap_remove_first(&base->avail[i]);\n\t\tif (extent != NULL) {\n\t\t\t/* Use existing space. */\n\t\t\tbreak;\n\t\t}\n\t}\n\tif (extent == NULL) {\n\t\t/* Try to allocate more space. */\n\t\textent = base_extent_alloc(tsdn, base, usize, alignment);\n\t}\n\tvoid *ret;\n\tif (extent == NULL) {\n\t\tret = NULL;\n\t\tgoto label_return;\n\t}\n\n\tret = base_extent_bump_alloc(base, extent, usize, alignment);\n\tif (esn != NULL) {\n\t\t*esn = extent_sn_get(extent);\n\t}\nlabel_return:\n\tmalloc_mutex_unlock(tsdn, &base->mtx);\n\treturn ret;\n}\n\n/*\n * base_alloc() returns zeroed memory, which is always demand-zeroed for the\n * auto arenas, in order to make multi-page sparse data structures such as radix\n * tree nodes efficient with respect to physical memory usage.  Upon success a\n * pointer to at least size bytes with specified alignment is returned.  Note\n * that size is rounded up to the nearest multiple of alignment to avoid false\n * sharing.\n */\nvoid *\nbase_alloc(tsdn_t *tsdn, base_t *base, size_t size, size_t alignment) {\n\treturn base_alloc_impl(tsdn, base, size, alignment, NULL);\n}\n\nextent_t *\nbase_alloc_extent(tsdn_t *tsdn, base_t *base) {\n\tsize_t esn;\n\textent_t *extent = base_alloc_impl(tsdn, base, sizeof(extent_t),\n\t    CACHELINE, &esn);\n\tif (extent == NULL) {\n\t\treturn NULL;\n\t}\n\textent_esn_set(extent, esn);\n\treturn extent;\n}\n\nvoid\nbase_stats_get(tsdn_t *tsdn, base_t *base, size_t *allocated, size_t *resident,\n    size_t *mapped, size_t *n_thp) {\n\tcassert(config_stats);\n\n\tmalloc_mutex_lock(tsdn, &base->mtx);\n\tassert(base->allocated <= base->resident);\n\tassert(base->resident <= base->mapped);\n\t*allocated = base->allocated;\n\t*resident = base->resident;\n\t*mapped = base->mapped;\n\t*n_thp = base->n_thp;\n\tmalloc_mutex_unlock(tsdn, &base->mtx);\n}\n\nvoid\nbase_prefork(tsdn_t *tsdn, base_t *base) {\n\tmalloc_mutex_prefork(tsdn, &base->mtx);\n}\n\nvoid\nbase_postfork_parent(tsdn_t *tsdn, base_t *base) {\n\tmalloc_mutex_postfork_parent(tsdn, &base->mtx);\n}\n\nvoid\nbase_postfork_child(tsdn_t *tsdn, base_t *base) {\n\tmalloc_mutex_postfork_child(tsdn, &base->mtx);\n}\n\nbool\nbase_boot(tsdn_t *tsdn) {\n\tb0 = base_new(tsdn, 0, (extent_hooks_t *)&extent_hooks_default);\n\treturn (b0 == NULL);\n}\n"
  },
  {
    "path": "deps/jemalloc/src/bin.c",
    "content": "#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/bin.h\"\n#include \"jemalloc/internal/sc.h\"\n#include \"jemalloc/internal/witness.h\"\n\nbin_info_t bin_infos[SC_NBINS];\n\nstatic void\nbin_infos_init(sc_data_t *sc_data, unsigned bin_shard_sizes[SC_NBINS],\n    bin_info_t bin_infos[SC_NBINS]) {\n\tfor (unsigned i = 0; i < SC_NBINS; i++) {\n\t\tbin_info_t *bin_info = &bin_infos[i];\n\t\tsc_t *sc = &sc_data->sc[i];\n\t\tbin_info->reg_size = ((size_t)1U << sc->lg_base)\n\t\t    + ((size_t)sc->ndelta << sc->lg_delta);\n\t\tbin_info->slab_size = (sc->pgs << LG_PAGE);\n\t\tbin_info->nregs =\n\t\t    (uint32_t)(bin_info->slab_size / bin_info->reg_size);\n\t\tbin_info->n_shards = bin_shard_sizes[i];\n\t\tbitmap_info_t bitmap_info = BITMAP_INFO_INITIALIZER(\n\t\t    bin_info->nregs);\n\t\tbin_info->bitmap_info = bitmap_info;\n\t}\n}\n\nbool\nbin_update_shard_size(unsigned bin_shard_sizes[SC_NBINS], size_t start_size,\n    size_t end_size, size_t nshards) {\n\tif (nshards > BIN_SHARDS_MAX || nshards == 0) {\n\t\treturn true;\n\t}\n\n\tif (start_size > SC_SMALL_MAXCLASS) {\n\t\treturn false;\n\t}\n\tif (end_size > SC_SMALL_MAXCLASS) {\n\t\tend_size = SC_SMALL_MAXCLASS;\n\t}\n\n\t/* Compute the index since this may happen before sz init. */\n\tszind_t ind1 = sz_size2index_compute(start_size);\n\tszind_t ind2 = sz_size2index_compute(end_size);\n\tfor (unsigned i = ind1; i <= ind2; i++) {\n\t\tbin_shard_sizes[i] = (unsigned)nshards;\n\t}\n\n\treturn false;\n}\n\nvoid\nbin_shard_sizes_boot(unsigned bin_shard_sizes[SC_NBINS]) {\n\t/* Load the default number of shards. */\n\tfor (unsigned i = 0; i < SC_NBINS; i++) {\n\t\tbin_shard_sizes[i] = N_BIN_SHARDS_DEFAULT;\n\t}\n}\n\nvoid\nbin_boot(sc_data_t *sc_data, unsigned bin_shard_sizes[SC_NBINS]) {\n\tassert(sc_data->initialized);\n\tbin_infos_init(sc_data, bin_shard_sizes, bin_infos);\n}\n\nbool\nbin_init(bin_t *bin) {\n\tif (malloc_mutex_init(&bin->lock, \"bin\", WITNESS_RANK_BIN,\n\t    malloc_mutex_rank_exclusive)) {\n\t\treturn true;\n\t}\n\tbin->slabcur = NULL;\n\textent_heap_new(&bin->slabs_nonfull);\n\textent_list_init(&bin->slabs_full);\n\tif (config_stats) {\n\t\tmemset(&bin->stats, 0, sizeof(bin_stats_t));\n\t}\n\treturn false;\n}\n\nvoid\nbin_prefork(tsdn_t *tsdn, bin_t *bin) {\n\tmalloc_mutex_prefork(tsdn, &bin->lock);\n}\n\nvoid\nbin_postfork_parent(tsdn_t *tsdn, bin_t *bin) {\n\tmalloc_mutex_postfork_parent(tsdn, &bin->lock);\n}\n\nvoid\nbin_postfork_child(tsdn_t *tsdn, bin_t *bin) {\n\tmalloc_mutex_postfork_child(tsdn, &bin->lock);\n}\n"
  },
  {
    "path": "deps/jemalloc/src/bitmap.c",
    "content": "#define JEMALLOC_BITMAP_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n\n/******************************************************************************/\n\n#ifdef BITMAP_USE_TREE\n\nvoid\nbitmap_info_init(bitmap_info_t *binfo, size_t nbits) {\n\tunsigned i;\n\tsize_t group_count;\n\n\tassert(nbits > 0);\n\tassert(nbits <= (ZU(1) << LG_BITMAP_MAXBITS));\n\n\t/*\n\t * Compute the number of groups necessary to store nbits bits, and\n\t * progressively work upward through the levels until reaching a level\n\t * that requires only one group.\n\t */\n\tbinfo->levels[0].group_offset = 0;\n\tgroup_count = BITMAP_BITS2GROUPS(nbits);\n\tfor (i = 1; group_count > 1; i++) {\n\t\tassert(i < BITMAP_MAX_LEVELS);\n\t\tbinfo->levels[i].group_offset = binfo->levels[i-1].group_offset\n\t\t    + group_count;\n\t\tgroup_count = BITMAP_BITS2GROUPS(group_count);\n\t}\n\tbinfo->levels[i].group_offset = binfo->levels[i-1].group_offset\n\t    + group_count;\n\tassert(binfo->levels[i].group_offset <= BITMAP_GROUPS_MAX);\n\tbinfo->nlevels = i;\n\tbinfo->nbits = nbits;\n}\n\nstatic size_t\nbitmap_info_ngroups(const bitmap_info_t *binfo) {\n\treturn binfo->levels[binfo->nlevels].group_offset;\n}\n\nvoid\nbitmap_init(bitmap_t *bitmap, const bitmap_info_t *binfo, bool fill) {\n\tsize_t extra;\n\tunsigned i;\n\n\t/*\n\t * Bits are actually inverted with regard to the external bitmap\n\t * interface.\n\t */\n\n\tif (fill) {\n\t\t/* The \"filled\" bitmap starts out with all 0 bits. */\n\t\tmemset(bitmap, 0, bitmap_size(binfo));\n\t\treturn;\n\t}\n\n\t/*\n\t * The \"empty\" bitmap starts out with all 1 bits, except for trailing\n\t * unused bits (if any).  Note that each group uses bit 0 to correspond\n\t * to the first logical bit in the group, so extra bits are the most\n\t * significant bits of the last group.\n\t */\n\tmemset(bitmap, 0xffU, bitmap_size(binfo));\n\textra = (BITMAP_GROUP_NBITS - (binfo->nbits & BITMAP_GROUP_NBITS_MASK))\n\t    & BITMAP_GROUP_NBITS_MASK;\n\tif (extra != 0) {\n\t\tbitmap[binfo->levels[1].group_offset - 1] >>= extra;\n\t}\n\tfor (i = 1; i < binfo->nlevels; i++) {\n\t\tsize_t group_count = binfo->levels[i].group_offset -\n\t\t    binfo->levels[i-1].group_offset;\n\t\textra = (BITMAP_GROUP_NBITS - (group_count &\n\t\t    BITMAP_GROUP_NBITS_MASK)) & BITMAP_GROUP_NBITS_MASK;\n\t\tif (extra != 0) {\n\t\t\tbitmap[binfo->levels[i+1].group_offset - 1] >>= extra;\n\t\t}\n\t}\n}\n\n#else /* BITMAP_USE_TREE */\n\nvoid\nbitmap_info_init(bitmap_info_t *binfo, size_t nbits) {\n\tassert(nbits > 0);\n\tassert(nbits <= (ZU(1) << LG_BITMAP_MAXBITS));\n\n\tbinfo->ngroups = BITMAP_BITS2GROUPS(nbits);\n\tbinfo->nbits = nbits;\n}\n\nstatic size_t\nbitmap_info_ngroups(const bitmap_info_t *binfo) {\n\treturn binfo->ngroups;\n}\n\nvoid\nbitmap_init(bitmap_t *bitmap, const bitmap_info_t *binfo, bool fill) {\n\tsize_t extra;\n\n\tif (fill) {\n\t\tmemset(bitmap, 0, bitmap_size(binfo));\n\t\treturn;\n\t}\n\n\tmemset(bitmap, 0xffU, bitmap_size(binfo));\n\textra = (BITMAP_GROUP_NBITS - (binfo->nbits & BITMAP_GROUP_NBITS_MASK))\n\t    & BITMAP_GROUP_NBITS_MASK;\n\tif (extra != 0) {\n\t\tbitmap[binfo->ngroups - 1] >>= extra;\n\t}\n}\n\n#endif /* BITMAP_USE_TREE */\n\nsize_t\nbitmap_size(const bitmap_info_t *binfo) {\n\treturn (bitmap_info_ngroups(binfo) << LG_SIZEOF_BITMAP);\n}\n"
  },
  {
    "path": "deps/jemalloc/src/ckh.c",
    "content": "/*\n *******************************************************************************\n * Implementation of (2^1+,2) cuckoo hashing, where 2^1+ indicates that each\n * hash bucket contains 2^n cells, for n >= 1, and 2 indicates that two hash\n * functions are employed.  The original cuckoo hashing algorithm was described\n * in:\n *\n *   Pagh, R., F.F. Rodler (2004) Cuckoo Hashing.  Journal of Algorithms\n *     51(2):122-144.\n *\n * Generalization of cuckoo hashing was discussed in:\n *\n *   Erlingsson, U., M. Manasse, F. McSherry (2006) A cool and practical\n *     alternative to traditional hash tables.  In Proceedings of the 7th\n *     Workshop on Distributed Data and Structures (WDAS'06), Santa Clara, CA,\n *     January 2006.\n *\n * This implementation uses precisely two hash functions because that is the\n * fewest that can work, and supporting multiple hashes is an implementation\n * burden.  Here is a reproduction of Figure 1 from Erlingsson et al. (2006)\n * that shows approximate expected maximum load factors for various\n * configurations:\n *\n *           |         #cells/bucket         |\n *   #hashes |   1   |   2   |   4   |   8   |\n *   --------+-------+-------+-------+-------+\n *         1 | 0.006 | 0.006 | 0.03  | 0.12  |\n *         2 | 0.49  | 0.86  |>0.93< |>0.96< |\n *         3 | 0.91  | 0.97  | 0.98  | 0.999 |\n *         4 | 0.97  | 0.99  | 0.999 |       |\n *\n * The number of cells per bucket is chosen such that a bucket fits in one cache\n * line.  So, on 32- and 64-bit systems, we use (8,2) and (4,2) cuckoo hashing,\n * respectively.\n *\n ******************************************************************************/\n#define JEMALLOC_CKH_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n\n#include \"jemalloc/internal/ckh.h\"\n\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/hash.h\"\n#include \"jemalloc/internal/malloc_io.h\"\n#include \"jemalloc/internal/prng.h\"\n#include \"jemalloc/internal/util.h\"\n\n/******************************************************************************/\n/* Function prototypes for non-inline static functions. */\n\nstatic bool\tckh_grow(tsd_t *tsd, ckh_t *ckh);\nstatic void\tckh_shrink(tsd_t *tsd, ckh_t *ckh);\n\n/******************************************************************************/\n\n/*\n * Search bucket for key and return the cell number if found; SIZE_T_MAX\n * otherwise.\n */\nstatic size_t\nckh_bucket_search(ckh_t *ckh, size_t bucket, const void *key) {\n\tckhc_t *cell;\n\tunsigned i;\n\n\tfor (i = 0; i < (ZU(1) << LG_CKH_BUCKET_CELLS); i++) {\n\t\tcell = &ckh->tab[(bucket << LG_CKH_BUCKET_CELLS) + i];\n\t\tif (cell->key != NULL && ckh->keycomp(key, cell->key)) {\n\t\t\treturn (bucket << LG_CKH_BUCKET_CELLS) + i;\n\t\t}\n\t}\n\n\treturn SIZE_T_MAX;\n}\n\n/*\n * Search table for key and return cell number if found; SIZE_T_MAX otherwise.\n */\nstatic size_t\nckh_isearch(ckh_t *ckh, const void *key) {\n\tsize_t hashes[2], bucket, cell;\n\n\tassert(ckh != NULL);\n\n\tckh->hash(key, hashes);\n\n\t/* Search primary bucket. */\n\tbucket = hashes[0] & ((ZU(1) << ckh->lg_curbuckets) - 1);\n\tcell = ckh_bucket_search(ckh, bucket, key);\n\tif (cell != SIZE_T_MAX) {\n\t\treturn cell;\n\t}\n\n\t/* Search secondary bucket. */\n\tbucket = hashes[1] & ((ZU(1) << ckh->lg_curbuckets) - 1);\n\tcell = ckh_bucket_search(ckh, bucket, key);\n\treturn cell;\n}\n\nstatic bool\nckh_try_bucket_insert(ckh_t *ckh, size_t bucket, const void *key,\n    const void *data) {\n\tckhc_t *cell;\n\tunsigned offset, i;\n\n\t/*\n\t * Cycle through the cells in the bucket, starting at a random position.\n\t * The randomness avoids worst-case search overhead as buckets fill up.\n\t */\n\toffset = (unsigned)prng_lg_range_u64(&ckh->prng_state,\n\t    LG_CKH_BUCKET_CELLS);\n\tfor (i = 0; i < (ZU(1) << LG_CKH_BUCKET_CELLS); i++) {\n\t\tcell = &ckh->tab[(bucket << LG_CKH_BUCKET_CELLS) +\n\t\t    ((i + offset) & ((ZU(1) << LG_CKH_BUCKET_CELLS) - 1))];\n\t\tif (cell->key == NULL) {\n\t\t\tcell->key = key;\n\t\t\tcell->data = data;\n\t\t\tckh->count++;\n\t\t\treturn false;\n\t\t}\n\t}\n\n\treturn true;\n}\n\n/*\n * No space is available in bucket.  Randomly evict an item, then try to find an\n * alternate location for that item.  Iteratively repeat this\n * eviction/relocation procedure until either success or detection of an\n * eviction/relocation bucket cycle.\n */\nstatic bool\nckh_evict_reloc_insert(ckh_t *ckh, size_t argbucket, void const **argkey,\n    void const **argdata) {\n\tconst void *key, *data, *tkey, *tdata;\n\tckhc_t *cell;\n\tsize_t hashes[2], bucket, tbucket;\n\tunsigned i;\n\n\tbucket = argbucket;\n\tkey = *argkey;\n\tdata = *argdata;\n\twhile (true) {\n\t\t/*\n\t\t * Choose a random item within the bucket to evict.  This is\n\t\t * critical to correct function, because without (eventually)\n\t\t * evicting all items within a bucket during iteration, it\n\t\t * would be possible to get stuck in an infinite loop if there\n\t\t * were an item for which both hashes indicated the same\n\t\t * bucket.\n\t\t */\n\t\ti = (unsigned)prng_lg_range_u64(&ckh->prng_state,\n\t\t    LG_CKH_BUCKET_CELLS);\n\t\tcell = &ckh->tab[(bucket << LG_CKH_BUCKET_CELLS) + i];\n\t\tassert(cell->key != NULL);\n\n\t\t/* Swap cell->{key,data} and {key,data} (evict). */\n\t\ttkey = cell->key; tdata = cell->data;\n\t\tcell->key = key; cell->data = data;\n\t\tkey = tkey; data = tdata;\n\n#ifdef CKH_COUNT\n\t\tckh->nrelocs++;\n#endif\n\n\t\t/* Find the alternate bucket for the evicted item. */\n\t\tckh->hash(key, hashes);\n\t\ttbucket = hashes[1] & ((ZU(1) << ckh->lg_curbuckets) - 1);\n\t\tif (tbucket == bucket) {\n\t\t\ttbucket = hashes[0] & ((ZU(1) << ckh->lg_curbuckets)\n\t\t\t    - 1);\n\t\t\t/*\n\t\t\t * It may be that (tbucket == bucket) still, if the\n\t\t\t * item's hashes both indicate this bucket.  However,\n\t\t\t * we are guaranteed to eventually escape this bucket\n\t\t\t * during iteration, assuming pseudo-random item\n\t\t\t * selection (true randomness would make infinite\n\t\t\t * looping a remote possibility).  The reason we can\n\t\t\t * never get trapped forever is that there are two\n\t\t\t * cases:\n\t\t\t *\n\t\t\t * 1) This bucket == argbucket, so we will quickly\n\t\t\t *    detect an eviction cycle and terminate.\n\t\t\t * 2) An item was evicted to this bucket from another,\n\t\t\t *    which means that at least one item in this bucket\n\t\t\t *    has hashes that indicate distinct buckets.\n\t\t\t */\n\t\t}\n\t\t/* Check for a cycle. */\n\t\tif (tbucket == argbucket) {\n\t\t\t*argkey = key;\n\t\t\t*argdata = data;\n\t\t\treturn true;\n\t\t}\n\n\t\tbucket = tbucket;\n\t\tif (!ckh_try_bucket_insert(ckh, bucket, key, data)) {\n\t\t\treturn false;\n\t\t}\n\t}\n}\n\nstatic bool\nckh_try_insert(ckh_t *ckh, void const**argkey, void const**argdata) {\n\tsize_t hashes[2], bucket;\n\tconst void *key = *argkey;\n\tconst void *data = *argdata;\n\n\tckh->hash(key, hashes);\n\n\t/* Try to insert in primary bucket. */\n\tbucket = hashes[0] & ((ZU(1) << ckh->lg_curbuckets) - 1);\n\tif (!ckh_try_bucket_insert(ckh, bucket, key, data)) {\n\t\treturn false;\n\t}\n\n\t/* Try to insert in secondary bucket. */\n\tbucket = hashes[1] & ((ZU(1) << ckh->lg_curbuckets) - 1);\n\tif (!ckh_try_bucket_insert(ckh, bucket, key, data)) {\n\t\treturn false;\n\t}\n\n\t/*\n\t * Try to find a place for this item via iterative eviction/relocation.\n\t */\n\treturn ckh_evict_reloc_insert(ckh, bucket, argkey, argdata);\n}\n\n/*\n * Try to rebuild the hash table from scratch by inserting all items from the\n * old table into the new.\n */\nstatic bool\nckh_rebuild(ckh_t *ckh, ckhc_t *aTab) {\n\tsize_t count, i, nins;\n\tconst void *key, *data;\n\n\tcount = ckh->count;\n\tckh->count = 0;\n\tfor (i = nins = 0; nins < count; i++) {\n\t\tif (aTab[i].key != NULL) {\n\t\t\tkey = aTab[i].key;\n\t\t\tdata = aTab[i].data;\n\t\t\tif (ckh_try_insert(ckh, &key, &data)) {\n\t\t\t\tckh->count = count;\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tnins++;\n\t\t}\n\t}\n\n\treturn false;\n}\n\nstatic bool\nckh_grow(tsd_t *tsd, ckh_t *ckh) {\n\tbool ret;\n\tckhc_t *tab, *ttab;\n\tunsigned lg_prevbuckets, lg_curcells;\n\n#ifdef CKH_COUNT\n\tckh->ngrows++;\n#endif\n\n\t/*\n\t * It is possible (though unlikely, given well behaved hashes) that the\n\t * table will have to be doubled more than once in order to create a\n\t * usable table.\n\t */\n\tlg_prevbuckets = ckh->lg_curbuckets;\n\tlg_curcells = ckh->lg_curbuckets + LG_CKH_BUCKET_CELLS;\n\twhile (true) {\n\t\tsize_t usize;\n\n\t\tlg_curcells++;\n\t\tusize = sz_sa2u(sizeof(ckhc_t) << lg_curcells, CACHELINE);\n\t\tif (unlikely(usize == 0\n\t\t    || usize > SC_LARGE_MAXCLASS)) {\n\t\t\tret = true;\n\t\t\tgoto label_return;\n\t\t}\n\t\ttab = (ckhc_t *)ipallocztm(tsd_tsdn(tsd), usize, CACHELINE,\n\t\t    true, NULL, true, arena_ichoose(tsd, NULL));\n\t\tif (tab == NULL) {\n\t\t\tret = true;\n\t\t\tgoto label_return;\n\t\t}\n\t\t/* Swap in new table. */\n\t\tttab = ckh->tab;\n\t\tckh->tab = tab;\n\t\ttab = ttab;\n\t\tckh->lg_curbuckets = lg_curcells - LG_CKH_BUCKET_CELLS;\n\n\t\tif (!ckh_rebuild(ckh, tab)) {\n\t\t\tidalloctm(tsd_tsdn(tsd), tab, NULL, NULL, true, true);\n\t\t\tbreak;\n\t\t}\n\n\t\t/* Rebuilding failed, so back out partially rebuilt table. */\n\t\tidalloctm(tsd_tsdn(tsd), ckh->tab, NULL, NULL, true, true);\n\t\tckh->tab = tab;\n\t\tckh->lg_curbuckets = lg_prevbuckets;\n\t}\n\n\tret = false;\nlabel_return:\n\treturn ret;\n}\n\nstatic void\nckh_shrink(tsd_t *tsd, ckh_t *ckh) {\n\tckhc_t *tab, *ttab;\n\tsize_t usize;\n\tunsigned lg_prevbuckets, lg_curcells;\n\n\t/*\n\t * It is possible (though unlikely, given well behaved hashes) that the\n\t * table rebuild will fail.\n\t */\n\tlg_prevbuckets = ckh->lg_curbuckets;\n\tlg_curcells = ckh->lg_curbuckets + LG_CKH_BUCKET_CELLS - 1;\n\tusize = sz_sa2u(sizeof(ckhc_t) << lg_curcells, CACHELINE);\n\tif (unlikely(usize == 0 || usize > SC_LARGE_MAXCLASS)) {\n\t\treturn;\n\t}\n\ttab = (ckhc_t *)ipallocztm(tsd_tsdn(tsd), usize, CACHELINE, true, NULL,\n\t    true, arena_ichoose(tsd, NULL));\n\tif (tab == NULL) {\n\t\t/*\n\t\t * An OOM error isn't worth propagating, since it doesn't\n\t\t * prevent this or future operations from proceeding.\n\t\t */\n\t\treturn;\n\t}\n\t/* Swap in new table. */\n\tttab = ckh->tab;\n\tckh->tab = tab;\n\ttab = ttab;\n\tckh->lg_curbuckets = lg_curcells - LG_CKH_BUCKET_CELLS;\n\n\tif (!ckh_rebuild(ckh, tab)) {\n\t\tidalloctm(tsd_tsdn(tsd), tab, NULL, NULL, true, true);\n#ifdef CKH_COUNT\n\t\tckh->nshrinks++;\n#endif\n\t\treturn;\n\t}\n\n\t/* Rebuilding failed, so back out partially rebuilt table. */\n\tidalloctm(tsd_tsdn(tsd), ckh->tab, NULL, NULL, true, true);\n\tckh->tab = tab;\n\tckh->lg_curbuckets = lg_prevbuckets;\n#ifdef CKH_COUNT\n\tckh->nshrinkfails++;\n#endif\n}\n\nbool\nckh_new(tsd_t *tsd, ckh_t *ckh, size_t minitems, ckh_hash_t *hash,\n    ckh_keycomp_t *keycomp) {\n\tbool ret;\n\tsize_t mincells, usize;\n\tunsigned lg_mincells;\n\n\tassert(minitems > 0);\n\tassert(hash != NULL);\n\tassert(keycomp != NULL);\n\n#ifdef CKH_COUNT\n\tckh->ngrows = 0;\n\tckh->nshrinks = 0;\n\tckh->nshrinkfails = 0;\n\tckh->ninserts = 0;\n\tckh->nrelocs = 0;\n#endif\n\tckh->prng_state = 42; /* Value doesn't really matter. */\n\tckh->count = 0;\n\n\t/*\n\t * Find the minimum power of 2 that is large enough to fit minitems\n\t * entries.  We are using (2+,2) cuckoo hashing, which has an expected\n\t * maximum load factor of at least ~0.86, so 0.75 is a conservative load\n\t * factor that will typically allow mincells items to fit without ever\n\t * growing the table.\n\t */\n\tassert(LG_CKH_BUCKET_CELLS > 0);\n\tmincells = ((minitems + (3 - (minitems % 3))) / 3) << 2;\n\tfor (lg_mincells = LG_CKH_BUCKET_CELLS;\n\t    (ZU(1) << lg_mincells) < mincells;\n\t    lg_mincells++) {\n\t\t/* Do nothing. */\n\t}\n\tckh->lg_minbuckets = lg_mincells - LG_CKH_BUCKET_CELLS;\n\tckh->lg_curbuckets = lg_mincells - LG_CKH_BUCKET_CELLS;\n\tckh->hash = hash;\n\tckh->keycomp = keycomp;\n\n\tusize = sz_sa2u(sizeof(ckhc_t) << lg_mincells, CACHELINE);\n\tif (unlikely(usize == 0 || usize > SC_LARGE_MAXCLASS)) {\n\t\tret = true;\n\t\tgoto label_return;\n\t}\n\tckh->tab = (ckhc_t *)ipallocztm(tsd_tsdn(tsd), usize, CACHELINE, true,\n\t    NULL, true, arena_ichoose(tsd, NULL));\n\tif (ckh->tab == NULL) {\n\t\tret = true;\n\t\tgoto label_return;\n\t}\n\n\tret = false;\nlabel_return:\n\treturn ret;\n}\n\nvoid\nckh_delete(tsd_t *tsd, ckh_t *ckh) {\n\tassert(ckh != NULL);\n\n#ifdef CKH_VERBOSE\n\tmalloc_printf(\n\t    \"%s(%p): ngrows: %\"FMTu64\", nshrinks: %\"FMTu64\",\"\n\t    \" nshrinkfails: %\"FMTu64\", ninserts: %\"FMTu64\",\"\n\t    \" nrelocs: %\"FMTu64\"\\n\", __func__, ckh,\n\t    (unsigned long long)ckh->ngrows,\n\t    (unsigned long long)ckh->nshrinks,\n\t    (unsigned long long)ckh->nshrinkfails,\n\t    (unsigned long long)ckh->ninserts,\n\t    (unsigned long long)ckh->nrelocs);\n#endif\n\n\tidalloctm(tsd_tsdn(tsd), ckh->tab, NULL, NULL, true, true);\n\tif (config_debug) {\n\t\tmemset(ckh, JEMALLOC_FREE_JUNK, sizeof(ckh_t));\n\t}\n}\n\nsize_t\nckh_count(ckh_t *ckh) {\n\tassert(ckh != NULL);\n\n\treturn ckh->count;\n}\n\nbool\nckh_iter(ckh_t *ckh, size_t *tabind, void **key, void **data) {\n\tsize_t i, ncells;\n\n\tfor (i = *tabind, ncells = (ZU(1) << (ckh->lg_curbuckets +\n\t    LG_CKH_BUCKET_CELLS)); i < ncells; i++) {\n\t\tif (ckh->tab[i].key != NULL) {\n\t\t\tif (key != NULL) {\n\t\t\t\t*key = (void *)ckh->tab[i].key;\n\t\t\t}\n\t\t\tif (data != NULL) {\n\t\t\t\t*data = (void *)ckh->tab[i].data;\n\t\t\t}\n\t\t\t*tabind = i + 1;\n\t\t\treturn false;\n\t\t}\n\t}\n\n\treturn true;\n}\n\nbool\nckh_insert(tsd_t *tsd, ckh_t *ckh, const void *key, const void *data) {\n\tbool ret;\n\n\tassert(ckh != NULL);\n\tassert(ckh_search(ckh, key, NULL, NULL));\n\n#ifdef CKH_COUNT\n\tckh->ninserts++;\n#endif\n\n\twhile (ckh_try_insert(ckh, &key, &data)) {\n\t\tif (ckh_grow(tsd, ckh)) {\n\t\t\tret = true;\n\t\t\tgoto label_return;\n\t\t}\n\t}\n\n\tret = false;\nlabel_return:\n\treturn ret;\n}\n\nbool\nckh_remove(tsd_t *tsd, ckh_t *ckh, const void *searchkey, void **key,\n    void **data) {\n\tsize_t cell;\n\n\tassert(ckh != NULL);\n\n\tcell = ckh_isearch(ckh, searchkey);\n\tif (cell != SIZE_T_MAX) {\n\t\tif (key != NULL) {\n\t\t\t*key = (void *)ckh->tab[cell].key;\n\t\t}\n\t\tif (data != NULL) {\n\t\t\t*data = (void *)ckh->tab[cell].data;\n\t\t}\n\t\tckh->tab[cell].key = NULL;\n\t\tckh->tab[cell].data = NULL; /* Not necessary. */\n\n\t\tckh->count--;\n\t\t/* Try to halve the table if it is less than 1/4 full. */\n\t\tif (ckh->count < (ZU(1) << (ckh->lg_curbuckets\n\t\t    + LG_CKH_BUCKET_CELLS - 2)) && ckh->lg_curbuckets\n\t\t    > ckh->lg_minbuckets) {\n\t\t\t/* Ignore error due to OOM. */\n\t\t\tckh_shrink(tsd, ckh);\n\t\t}\n\n\t\treturn false;\n\t}\n\n\treturn true;\n}\n\nbool\nckh_search(ckh_t *ckh, const void *searchkey, void **key, void **data) {\n\tsize_t cell;\n\n\tassert(ckh != NULL);\n\n\tcell = ckh_isearch(ckh, searchkey);\n\tif (cell != SIZE_T_MAX) {\n\t\tif (key != NULL) {\n\t\t\t*key = (void *)ckh->tab[cell].key;\n\t\t}\n\t\tif (data != NULL) {\n\t\t\t*data = (void *)ckh->tab[cell].data;\n\t\t}\n\t\treturn false;\n\t}\n\n\treturn true;\n}\n\nvoid\nckh_string_hash(const void *key, size_t r_hash[2]) {\n\thash(key, strlen((const char *)key), 0x94122f33U, r_hash);\n}\n\nbool\nckh_string_keycomp(const void *k1, const void *k2) {\n\tassert(k1 != NULL);\n\tassert(k2 != NULL);\n\n\treturn !strcmp((char *)k1, (char *)k2);\n}\n\nvoid\nckh_pointer_hash(const void *key, size_t r_hash[2]) {\n\tunion {\n\t\tconst void\t*v;\n\t\tsize_t\t\ti;\n\t} u;\n\n\tassert(sizeof(u.v) == sizeof(u.i));\n\tu.v = key;\n\thash(&u.i, sizeof(u.i), 0xd983396eU, r_hash);\n}\n\nbool\nckh_pointer_keycomp(const void *k1, const void *k2) {\n\treturn (k1 == k2);\n}\n"
  },
  {
    "path": "deps/jemalloc/src/ctl.c",
    "content": "#define JEMALLOC_CTL_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/ctl.h\"\n#include \"jemalloc/internal/extent_dss.h\"\n#include \"jemalloc/internal/extent_mmap.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/nstime.h\"\n#include \"jemalloc/internal/sc.h\"\n#include \"jemalloc/internal/util.h\"\n\n/******************************************************************************/\n/* Data. */\n\n/*\n * ctl_mtx protects the following:\n * - ctl_stats->*\n */\nstatic malloc_mutex_t\tctl_mtx;\nstatic bool\t\tctl_initialized;\nstatic ctl_stats_t\t*ctl_stats;\nstatic ctl_arenas_t\t*ctl_arenas;\n\n/******************************************************************************/\n/* Helpers for named and indexed nodes. */\n\nstatic const ctl_named_node_t *\nctl_named_node(const ctl_node_t *node) {\n\treturn ((node->named) ? (const ctl_named_node_t *)node : NULL);\n}\n\nstatic const ctl_named_node_t *\nctl_named_children(const ctl_named_node_t *node, size_t index) {\n\tconst ctl_named_node_t *children = ctl_named_node(node->children);\n\n\treturn (children ? &children[index] : NULL);\n}\n\nstatic const ctl_indexed_node_t *\nctl_indexed_node(const ctl_node_t *node) {\n\treturn (!node->named ? (const ctl_indexed_node_t *)node : NULL);\n}\n\n/******************************************************************************/\n/* Function prototypes for non-inline static functions. */\n\n#define CTL_PROTO(n)\t\t\t\t\t\t\t\\\nstatic int\tn##_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\t\\\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen);\n\n#define INDEX_PROTO(n)\t\t\t\t\t\t\t\\\nstatic const ctl_named_node_t\t*n##_index(tsdn_t *tsdn,\t\t\\\n    const size_t *mib, size_t miblen, size_t i);\n\nCTL_PROTO(version)\nCTL_PROTO(epoch)\nCTL_PROTO(background_thread)\nCTL_PROTO(max_background_threads)\nCTL_PROTO(thread_tcache_enabled)\nCTL_PROTO(thread_tcache_flush)\nCTL_PROTO(thread_prof_name)\nCTL_PROTO(thread_prof_active)\nCTL_PROTO(thread_arena)\nCTL_PROTO(thread_allocated)\nCTL_PROTO(thread_allocatedp)\nCTL_PROTO(thread_deallocated)\nCTL_PROTO(thread_deallocatedp)\nCTL_PROTO(config_cache_oblivious)\nCTL_PROTO(config_debug)\nCTL_PROTO(config_fill)\nCTL_PROTO(config_lazy_lock)\nCTL_PROTO(config_malloc_conf)\nCTL_PROTO(config_opt_safety_checks)\nCTL_PROTO(config_prof)\nCTL_PROTO(config_prof_libgcc)\nCTL_PROTO(config_prof_libunwind)\nCTL_PROTO(config_stats)\nCTL_PROTO(config_utrace)\nCTL_PROTO(config_xmalloc)\nCTL_PROTO(opt_abort)\nCTL_PROTO(opt_abort_conf)\nCTL_PROTO(opt_confirm_conf)\nCTL_PROTO(opt_metadata_thp)\nCTL_PROTO(opt_retain)\nCTL_PROTO(opt_dss)\nCTL_PROTO(opt_narenas)\nCTL_PROTO(opt_percpu_arena)\nCTL_PROTO(opt_oversize_threshold)\nCTL_PROTO(opt_background_thread)\nCTL_PROTO(opt_max_background_threads)\nCTL_PROTO(opt_dirty_decay_ms)\nCTL_PROTO(opt_muzzy_decay_ms)\nCTL_PROTO(opt_stats_print)\nCTL_PROTO(opt_stats_print_opts)\nCTL_PROTO(opt_junk)\nCTL_PROTO(opt_zero)\nCTL_PROTO(opt_utrace)\nCTL_PROTO(opt_xmalloc)\nCTL_PROTO(opt_tcache)\nCTL_PROTO(opt_thp)\nCTL_PROTO(opt_lg_extent_max_active_fit)\nCTL_PROTO(opt_lg_tcache_max)\nCTL_PROTO(opt_prof)\nCTL_PROTO(opt_prof_prefix)\nCTL_PROTO(opt_prof_active)\nCTL_PROTO(opt_prof_thread_active_init)\nCTL_PROTO(opt_lg_prof_sample)\nCTL_PROTO(opt_lg_prof_interval)\nCTL_PROTO(opt_prof_gdump)\nCTL_PROTO(opt_prof_final)\nCTL_PROTO(opt_prof_leak)\nCTL_PROTO(opt_prof_accum)\nCTL_PROTO(tcache_create)\nCTL_PROTO(tcache_flush)\nCTL_PROTO(tcache_destroy)\nCTL_PROTO(arena_i_initialized)\nCTL_PROTO(arena_i_decay)\nCTL_PROTO(arena_i_purge)\nCTL_PROTO(arena_i_reset)\nCTL_PROTO(arena_i_destroy)\nCTL_PROTO(arena_i_dss)\nCTL_PROTO(arena_i_dirty_decay_ms)\nCTL_PROTO(arena_i_muzzy_decay_ms)\nCTL_PROTO(arena_i_extent_hooks)\nCTL_PROTO(arena_i_retain_grow_limit)\nINDEX_PROTO(arena_i)\nCTL_PROTO(arenas_bin_i_size)\nCTL_PROTO(arenas_bin_i_nregs)\nCTL_PROTO(arenas_bin_i_slab_size)\nCTL_PROTO(arenas_bin_i_nshards)\nINDEX_PROTO(arenas_bin_i)\nCTL_PROTO(arenas_lextent_i_size)\nINDEX_PROTO(arenas_lextent_i)\nCTL_PROTO(arenas_narenas)\nCTL_PROTO(arenas_dirty_decay_ms)\nCTL_PROTO(arenas_muzzy_decay_ms)\nCTL_PROTO(arenas_quantum)\nCTL_PROTO(arenas_page)\nCTL_PROTO(arenas_tcache_max)\nCTL_PROTO(arenas_nbins)\nCTL_PROTO(arenas_nhbins)\nCTL_PROTO(arenas_nlextents)\nCTL_PROTO(arenas_create)\nCTL_PROTO(arenas_lookup)\nCTL_PROTO(prof_thread_active_init)\nCTL_PROTO(prof_active)\nCTL_PROTO(prof_dump)\nCTL_PROTO(prof_gdump)\nCTL_PROTO(prof_reset)\nCTL_PROTO(prof_interval)\nCTL_PROTO(lg_prof_sample)\nCTL_PROTO(prof_log_start)\nCTL_PROTO(prof_log_stop)\nCTL_PROTO(stats_arenas_i_small_allocated)\nCTL_PROTO(stats_arenas_i_small_nmalloc)\nCTL_PROTO(stats_arenas_i_small_ndalloc)\nCTL_PROTO(stats_arenas_i_small_nrequests)\nCTL_PROTO(stats_arenas_i_small_nfills)\nCTL_PROTO(stats_arenas_i_small_nflushes)\nCTL_PROTO(stats_arenas_i_large_allocated)\nCTL_PROTO(stats_arenas_i_large_nmalloc)\nCTL_PROTO(stats_arenas_i_large_ndalloc)\nCTL_PROTO(stats_arenas_i_large_nrequests)\nCTL_PROTO(stats_arenas_i_large_nfills)\nCTL_PROTO(stats_arenas_i_large_nflushes)\nCTL_PROTO(stats_arenas_i_bins_j_nmalloc)\nCTL_PROTO(stats_arenas_i_bins_j_ndalloc)\nCTL_PROTO(stats_arenas_i_bins_j_nrequests)\nCTL_PROTO(stats_arenas_i_bins_j_curregs)\nCTL_PROTO(stats_arenas_i_bins_j_nfills)\nCTL_PROTO(stats_arenas_i_bins_j_nflushes)\nCTL_PROTO(stats_arenas_i_bins_j_nslabs)\nCTL_PROTO(stats_arenas_i_bins_j_nreslabs)\nCTL_PROTO(stats_arenas_i_bins_j_curslabs)\nCTL_PROTO(stats_arenas_i_bins_j_nonfull_slabs)\nINDEX_PROTO(stats_arenas_i_bins_j)\nCTL_PROTO(stats_arenas_i_lextents_j_nmalloc)\nCTL_PROTO(stats_arenas_i_lextents_j_ndalloc)\nCTL_PROTO(stats_arenas_i_lextents_j_nrequests)\nCTL_PROTO(stats_arenas_i_lextents_j_curlextents)\nINDEX_PROTO(stats_arenas_i_lextents_j)\nCTL_PROTO(stats_arenas_i_extents_j_ndirty)\nCTL_PROTO(stats_arenas_i_extents_j_nmuzzy)\nCTL_PROTO(stats_arenas_i_extents_j_nretained)\nCTL_PROTO(stats_arenas_i_extents_j_dirty_bytes)\nCTL_PROTO(stats_arenas_i_extents_j_muzzy_bytes)\nCTL_PROTO(stats_arenas_i_extents_j_retained_bytes)\nINDEX_PROTO(stats_arenas_i_extents_j)\nCTL_PROTO(stats_arenas_i_nthreads)\nCTL_PROTO(stats_arenas_i_uptime)\nCTL_PROTO(stats_arenas_i_dss)\nCTL_PROTO(stats_arenas_i_dirty_decay_ms)\nCTL_PROTO(stats_arenas_i_muzzy_decay_ms)\nCTL_PROTO(stats_arenas_i_pactive)\nCTL_PROTO(stats_arenas_i_pdirty)\nCTL_PROTO(stats_arenas_i_pmuzzy)\nCTL_PROTO(stats_arenas_i_mapped)\nCTL_PROTO(stats_arenas_i_retained)\nCTL_PROTO(stats_arenas_i_extent_avail)\nCTL_PROTO(stats_arenas_i_dirty_npurge)\nCTL_PROTO(stats_arenas_i_dirty_nmadvise)\nCTL_PROTO(stats_arenas_i_dirty_purged)\nCTL_PROTO(stats_arenas_i_muzzy_npurge)\nCTL_PROTO(stats_arenas_i_muzzy_nmadvise)\nCTL_PROTO(stats_arenas_i_muzzy_purged)\nCTL_PROTO(stats_arenas_i_base)\nCTL_PROTO(stats_arenas_i_internal)\nCTL_PROTO(stats_arenas_i_metadata_thp)\nCTL_PROTO(stats_arenas_i_tcache_bytes)\nCTL_PROTO(stats_arenas_i_resident)\nCTL_PROTO(stats_arenas_i_abandoned_vm)\nINDEX_PROTO(stats_arenas_i)\nCTL_PROTO(stats_allocated)\nCTL_PROTO(stats_active)\nCTL_PROTO(stats_background_thread_num_threads)\nCTL_PROTO(stats_background_thread_num_runs)\nCTL_PROTO(stats_background_thread_run_interval)\nCTL_PROTO(stats_metadata)\nCTL_PROTO(stats_metadata_thp)\nCTL_PROTO(stats_resident)\nCTL_PROTO(stats_mapped)\nCTL_PROTO(stats_retained)\nCTL_PROTO(experimental_hooks_install)\nCTL_PROTO(experimental_hooks_remove)\nCTL_PROTO(experimental_utilization_query)\nCTL_PROTO(experimental_utilization_batch_query)\nCTL_PROTO(experimental_arenas_i_pactivep)\nINDEX_PROTO(experimental_arenas_i)\n\n#define MUTEX_STATS_CTL_PROTO_GEN(n)\t\t\t\t\t\\\nCTL_PROTO(stats_##n##_num_ops)\t\t\t\t\t\t\\\nCTL_PROTO(stats_##n##_num_wait)\t\t\t\t\t\t\\\nCTL_PROTO(stats_##n##_num_spin_acq)\t\t\t\t\t\\\nCTL_PROTO(stats_##n##_num_owner_switch)\t\t\t\t\t\\\nCTL_PROTO(stats_##n##_total_wait_time)\t\t\t\t\t\\\nCTL_PROTO(stats_##n##_max_wait_time)\t\t\t\t\t\\\nCTL_PROTO(stats_##n##_max_num_thds)\n\n/* Global mutexes. */\n#define OP(mtx) MUTEX_STATS_CTL_PROTO_GEN(mutexes_##mtx)\nMUTEX_PROF_GLOBAL_MUTEXES\n#undef OP\n\n/* Per arena mutexes. */\n#define OP(mtx) MUTEX_STATS_CTL_PROTO_GEN(arenas_i_mutexes_##mtx)\nMUTEX_PROF_ARENA_MUTEXES\n#undef OP\n\n/* Arena bin mutexes. */\nMUTEX_STATS_CTL_PROTO_GEN(arenas_i_bins_j_mutex)\n#undef MUTEX_STATS_CTL_PROTO_GEN\n\nCTL_PROTO(stats_mutexes_reset)\n\n/******************************************************************************/\n/* mallctl tree. */\n\n#define NAME(n)\t{true},\tn\n#define CHILD(t, c)\t\t\t\t\t\t\t\\\n\tsizeof(c##_node) / sizeof(ctl_##t##_node_t),\t\t\t\\\n\t(ctl_node_t *)c##_node,\t\t\t\t\t\t\\\n\tNULL\n#define CTL(c)\t0, NULL, c##_ctl\n\n/*\n * Only handles internal indexed nodes, since there are currently no external\n * ones.\n */\n#define INDEX(i)\t{false},\ti##_index\n\nstatic const ctl_named_node_t\tthread_tcache_node[] = {\n\t{NAME(\"enabled\"),\tCTL(thread_tcache_enabled)},\n\t{NAME(\"flush\"),\t\tCTL(thread_tcache_flush)}\n};\n\nstatic const ctl_named_node_t\tthread_prof_node[] = {\n\t{NAME(\"name\"),\t\tCTL(thread_prof_name)},\n\t{NAME(\"active\"),\tCTL(thread_prof_active)}\n};\n\nstatic const ctl_named_node_t\tthread_node[] = {\n\t{NAME(\"arena\"),\t\tCTL(thread_arena)},\n\t{NAME(\"allocated\"),\tCTL(thread_allocated)},\n\t{NAME(\"allocatedp\"),\tCTL(thread_allocatedp)},\n\t{NAME(\"deallocated\"),\tCTL(thread_deallocated)},\n\t{NAME(\"deallocatedp\"),\tCTL(thread_deallocatedp)},\n\t{NAME(\"tcache\"),\tCHILD(named, thread_tcache)},\n\t{NAME(\"prof\"),\t\tCHILD(named, thread_prof)}\n};\n\nstatic const ctl_named_node_t\tconfig_node[] = {\n\t{NAME(\"cache_oblivious\"), CTL(config_cache_oblivious)},\n\t{NAME(\"debug\"),\t\tCTL(config_debug)},\n\t{NAME(\"fill\"),\t\tCTL(config_fill)},\n\t{NAME(\"lazy_lock\"),\tCTL(config_lazy_lock)},\n\t{NAME(\"malloc_conf\"),\tCTL(config_malloc_conf)},\n\t{NAME(\"opt_safety_checks\"),\tCTL(config_opt_safety_checks)},\n\t{NAME(\"prof\"),\t\tCTL(config_prof)},\n\t{NAME(\"prof_libgcc\"),\tCTL(config_prof_libgcc)},\n\t{NAME(\"prof_libunwind\"), CTL(config_prof_libunwind)},\n\t{NAME(\"stats\"),\t\tCTL(config_stats)},\n\t{NAME(\"utrace\"),\tCTL(config_utrace)},\n\t{NAME(\"xmalloc\"),\tCTL(config_xmalloc)}\n};\n\nstatic const ctl_named_node_t opt_node[] = {\n\t{NAME(\"abort\"),\t\tCTL(opt_abort)},\n\t{NAME(\"abort_conf\"),\tCTL(opt_abort_conf)},\n\t{NAME(\"confirm_conf\"),\tCTL(opt_confirm_conf)},\n\t{NAME(\"metadata_thp\"),\tCTL(opt_metadata_thp)},\n\t{NAME(\"retain\"),\tCTL(opt_retain)},\n\t{NAME(\"dss\"),\t\tCTL(opt_dss)},\n\t{NAME(\"narenas\"),\tCTL(opt_narenas)},\n\t{NAME(\"percpu_arena\"),\tCTL(opt_percpu_arena)},\n\t{NAME(\"oversize_threshold\"),\tCTL(opt_oversize_threshold)},\n\t{NAME(\"background_thread\"),\tCTL(opt_background_thread)},\n\t{NAME(\"max_background_threads\"),\tCTL(opt_max_background_threads)},\n\t{NAME(\"dirty_decay_ms\"), CTL(opt_dirty_decay_ms)},\n\t{NAME(\"muzzy_decay_ms\"), CTL(opt_muzzy_decay_ms)},\n\t{NAME(\"stats_print\"),\tCTL(opt_stats_print)},\n\t{NAME(\"stats_print_opts\"),\tCTL(opt_stats_print_opts)},\n\t{NAME(\"junk\"),\t\tCTL(opt_junk)},\n\t{NAME(\"zero\"),\t\tCTL(opt_zero)},\n\t{NAME(\"utrace\"),\tCTL(opt_utrace)},\n\t{NAME(\"xmalloc\"),\tCTL(opt_xmalloc)},\n\t{NAME(\"tcache\"),\tCTL(opt_tcache)},\n\t{NAME(\"thp\"),\t\tCTL(opt_thp)},\n\t{NAME(\"lg_extent_max_active_fit\"), CTL(opt_lg_extent_max_active_fit)},\n\t{NAME(\"lg_tcache_max\"),\tCTL(opt_lg_tcache_max)},\n\t{NAME(\"prof\"),\t\tCTL(opt_prof)},\n\t{NAME(\"prof_prefix\"),\tCTL(opt_prof_prefix)},\n\t{NAME(\"prof_active\"),\tCTL(opt_prof_active)},\n\t{NAME(\"prof_thread_active_init\"), CTL(opt_prof_thread_active_init)},\n\t{NAME(\"lg_prof_sample\"), CTL(opt_lg_prof_sample)},\n\t{NAME(\"lg_prof_interval\"), CTL(opt_lg_prof_interval)},\n\t{NAME(\"prof_gdump\"),\tCTL(opt_prof_gdump)},\n\t{NAME(\"prof_final\"),\tCTL(opt_prof_final)},\n\t{NAME(\"prof_leak\"),\tCTL(opt_prof_leak)},\n\t{NAME(\"prof_accum\"),\tCTL(opt_prof_accum)}\n};\n\nstatic const ctl_named_node_t\ttcache_node[] = {\n\t{NAME(\"create\"),\tCTL(tcache_create)},\n\t{NAME(\"flush\"),\t\tCTL(tcache_flush)},\n\t{NAME(\"destroy\"),\tCTL(tcache_destroy)}\n};\n\nstatic const ctl_named_node_t arena_i_node[] = {\n\t{NAME(\"initialized\"),\tCTL(arena_i_initialized)},\n\t{NAME(\"decay\"),\t\tCTL(arena_i_decay)},\n\t{NAME(\"purge\"),\t\tCTL(arena_i_purge)},\n\t{NAME(\"reset\"),\t\tCTL(arena_i_reset)},\n\t{NAME(\"destroy\"),\tCTL(arena_i_destroy)},\n\t{NAME(\"dss\"),\t\tCTL(arena_i_dss)},\n\t{NAME(\"dirty_decay_ms\"), CTL(arena_i_dirty_decay_ms)},\n\t{NAME(\"muzzy_decay_ms\"), CTL(arena_i_muzzy_decay_ms)},\n\t{NAME(\"extent_hooks\"),\tCTL(arena_i_extent_hooks)},\n\t{NAME(\"retain_grow_limit\"),\tCTL(arena_i_retain_grow_limit)}\n};\nstatic const ctl_named_node_t super_arena_i_node[] = {\n\t{NAME(\"\"),\t\tCHILD(named, arena_i)}\n};\n\nstatic const ctl_indexed_node_t arena_node[] = {\n\t{INDEX(arena_i)}\n};\n\nstatic const ctl_named_node_t arenas_bin_i_node[] = {\n\t{NAME(\"size\"),\t\tCTL(arenas_bin_i_size)},\n\t{NAME(\"nregs\"),\t\tCTL(arenas_bin_i_nregs)},\n\t{NAME(\"slab_size\"),\tCTL(arenas_bin_i_slab_size)},\n\t{NAME(\"nshards\"),\tCTL(arenas_bin_i_nshards)}\n};\nstatic const ctl_named_node_t super_arenas_bin_i_node[] = {\n\t{NAME(\"\"),\t\tCHILD(named, arenas_bin_i)}\n};\n\nstatic const ctl_indexed_node_t arenas_bin_node[] = {\n\t{INDEX(arenas_bin_i)}\n};\n\nstatic const ctl_named_node_t arenas_lextent_i_node[] = {\n\t{NAME(\"size\"),\t\tCTL(arenas_lextent_i_size)}\n};\nstatic const ctl_named_node_t super_arenas_lextent_i_node[] = {\n\t{NAME(\"\"),\t\tCHILD(named, arenas_lextent_i)}\n};\n\nstatic const ctl_indexed_node_t arenas_lextent_node[] = {\n\t{INDEX(arenas_lextent_i)}\n};\n\nstatic const ctl_named_node_t arenas_node[] = {\n\t{NAME(\"narenas\"),\tCTL(arenas_narenas)},\n\t{NAME(\"dirty_decay_ms\"), CTL(arenas_dirty_decay_ms)},\n\t{NAME(\"muzzy_decay_ms\"), CTL(arenas_muzzy_decay_ms)},\n\t{NAME(\"quantum\"),\tCTL(arenas_quantum)},\n\t{NAME(\"page\"),\t\tCTL(arenas_page)},\n\t{NAME(\"tcache_max\"),\tCTL(arenas_tcache_max)},\n\t{NAME(\"nbins\"),\t\tCTL(arenas_nbins)},\n\t{NAME(\"nhbins\"),\tCTL(arenas_nhbins)},\n\t{NAME(\"bin\"),\t\tCHILD(indexed, arenas_bin)},\n\t{NAME(\"nlextents\"),\tCTL(arenas_nlextents)},\n\t{NAME(\"lextent\"),\tCHILD(indexed, arenas_lextent)},\n\t{NAME(\"create\"),\tCTL(arenas_create)},\n\t{NAME(\"lookup\"),\tCTL(arenas_lookup)}\n};\n\nstatic const ctl_named_node_t\tprof_node[] = {\n\t{NAME(\"thread_active_init\"), CTL(prof_thread_active_init)},\n\t{NAME(\"active\"),\tCTL(prof_active)},\n\t{NAME(\"dump\"),\t\tCTL(prof_dump)},\n\t{NAME(\"gdump\"),\t\tCTL(prof_gdump)},\n\t{NAME(\"reset\"),\t\tCTL(prof_reset)},\n\t{NAME(\"interval\"),\tCTL(prof_interval)},\n\t{NAME(\"lg_sample\"),\tCTL(lg_prof_sample)},\n\t{NAME(\"log_start\"),\tCTL(prof_log_start)},\n\t{NAME(\"log_stop\"),\tCTL(prof_log_stop)}\n};\nstatic const ctl_named_node_t stats_arenas_i_small_node[] = {\n\t{NAME(\"allocated\"),\tCTL(stats_arenas_i_small_allocated)},\n\t{NAME(\"nmalloc\"),\tCTL(stats_arenas_i_small_nmalloc)},\n\t{NAME(\"ndalloc\"),\tCTL(stats_arenas_i_small_ndalloc)},\n\t{NAME(\"nrequests\"),\tCTL(stats_arenas_i_small_nrequests)},\n\t{NAME(\"nfills\"),\tCTL(stats_arenas_i_small_nfills)},\n\t{NAME(\"nflushes\"),\tCTL(stats_arenas_i_small_nflushes)}\n};\n\nstatic const ctl_named_node_t stats_arenas_i_large_node[] = {\n\t{NAME(\"allocated\"),\tCTL(stats_arenas_i_large_allocated)},\n\t{NAME(\"nmalloc\"),\tCTL(stats_arenas_i_large_nmalloc)},\n\t{NAME(\"ndalloc\"),\tCTL(stats_arenas_i_large_ndalloc)},\n\t{NAME(\"nrequests\"),\tCTL(stats_arenas_i_large_nrequests)},\n\t{NAME(\"nfills\"),\tCTL(stats_arenas_i_large_nfills)},\n\t{NAME(\"nflushes\"),\tCTL(stats_arenas_i_large_nflushes)}\n};\n\n#define MUTEX_PROF_DATA_NODE(prefix)\t\t\t\t\t\\\nstatic const ctl_named_node_t stats_##prefix##_node[] = {\t\t\\\n\t{NAME(\"num_ops\"),\t\t\t\t\t\t\\\n\t CTL(stats_##prefix##_num_ops)},\t\t\t\t\\\n\t{NAME(\"num_wait\"),\t\t\t\t\t\t\\\n\t CTL(stats_##prefix##_num_wait)},\t\t\t\t\\\n\t{NAME(\"num_spin_acq\"),\t\t\t\t\t\t\\\n\t CTL(stats_##prefix##_num_spin_acq)},\t\t\t\t\\\n\t{NAME(\"num_owner_switch\"),\t\t\t\t\t\\\n\t CTL(stats_##prefix##_num_owner_switch)},\t\t\t\\\n\t{NAME(\"total_wait_time\"),\t\t\t\t\t\\\n\t CTL(stats_##prefix##_total_wait_time)},\t\t\t\\\n\t{NAME(\"max_wait_time\"),\t\t\t\t\t\t\\\n\t CTL(stats_##prefix##_max_wait_time)},\t\t\t\t\\\n\t{NAME(\"max_num_thds\"),\t\t\t\t\t\t\\\n\t CTL(stats_##prefix##_max_num_thds)}\t\t\t\t\\\n\t/* Note that # of current waiting thread not provided. */\t\\\n};\n\nMUTEX_PROF_DATA_NODE(arenas_i_bins_j_mutex)\n\nstatic const ctl_named_node_t stats_arenas_i_bins_j_node[] = {\n\t{NAME(\"nmalloc\"),\tCTL(stats_arenas_i_bins_j_nmalloc)},\n\t{NAME(\"ndalloc\"),\tCTL(stats_arenas_i_bins_j_ndalloc)},\n\t{NAME(\"nrequests\"),\tCTL(stats_arenas_i_bins_j_nrequests)},\n\t{NAME(\"curregs\"),\tCTL(stats_arenas_i_bins_j_curregs)},\n\t{NAME(\"nfills\"),\tCTL(stats_arenas_i_bins_j_nfills)},\n\t{NAME(\"nflushes\"),\tCTL(stats_arenas_i_bins_j_nflushes)},\n\t{NAME(\"nslabs\"),\tCTL(stats_arenas_i_bins_j_nslabs)},\n\t{NAME(\"nreslabs\"),\tCTL(stats_arenas_i_bins_j_nreslabs)},\n\t{NAME(\"curslabs\"),\tCTL(stats_arenas_i_bins_j_curslabs)},\n\t{NAME(\"nonfull_slabs\"),\tCTL(stats_arenas_i_bins_j_nonfull_slabs)},\n\t{NAME(\"mutex\"),\t\tCHILD(named, stats_arenas_i_bins_j_mutex)}\n};\n\nstatic const ctl_named_node_t super_stats_arenas_i_bins_j_node[] = {\n\t{NAME(\"\"),\t\tCHILD(named, stats_arenas_i_bins_j)}\n};\n\nstatic const ctl_indexed_node_t stats_arenas_i_bins_node[] = {\n\t{INDEX(stats_arenas_i_bins_j)}\n};\n\nstatic const ctl_named_node_t stats_arenas_i_lextents_j_node[] = {\n\t{NAME(\"nmalloc\"),\tCTL(stats_arenas_i_lextents_j_nmalloc)},\n\t{NAME(\"ndalloc\"),\tCTL(stats_arenas_i_lextents_j_ndalloc)},\n\t{NAME(\"nrequests\"),\tCTL(stats_arenas_i_lextents_j_nrequests)},\n\t{NAME(\"curlextents\"),\tCTL(stats_arenas_i_lextents_j_curlextents)}\n};\nstatic const ctl_named_node_t super_stats_arenas_i_lextents_j_node[] = {\n\t{NAME(\"\"),\t\tCHILD(named, stats_arenas_i_lextents_j)}\n};\n\nstatic const ctl_indexed_node_t stats_arenas_i_lextents_node[] = {\n\t{INDEX(stats_arenas_i_lextents_j)}\n};\n\nstatic const ctl_named_node_t stats_arenas_i_extents_j_node[] = {\n\t{NAME(\"ndirty\"),\tCTL(stats_arenas_i_extents_j_ndirty)},\n\t{NAME(\"nmuzzy\"),\tCTL(stats_arenas_i_extents_j_nmuzzy)},\n\t{NAME(\"nretained\"),\tCTL(stats_arenas_i_extents_j_nretained)},\n\t{NAME(\"dirty_bytes\"),\tCTL(stats_arenas_i_extents_j_dirty_bytes)},\n\t{NAME(\"muzzy_bytes\"),\tCTL(stats_arenas_i_extents_j_muzzy_bytes)},\n\t{NAME(\"retained_bytes\"), CTL(stats_arenas_i_extents_j_retained_bytes)}\n};\n\nstatic const ctl_named_node_t super_stats_arenas_i_extents_j_node[] = {\n\t{NAME(\"\"),\t\tCHILD(named, stats_arenas_i_extents_j)}\n};\n\nstatic const ctl_indexed_node_t stats_arenas_i_extents_node[] = {\n\t{INDEX(stats_arenas_i_extents_j)}\n};\n\n#define OP(mtx)  MUTEX_PROF_DATA_NODE(arenas_i_mutexes_##mtx)\nMUTEX_PROF_ARENA_MUTEXES\n#undef OP\n\nstatic const ctl_named_node_t stats_arenas_i_mutexes_node[] = {\n#define OP(mtx) {NAME(#mtx), CHILD(named, stats_arenas_i_mutexes_##mtx)},\nMUTEX_PROF_ARENA_MUTEXES\n#undef OP\n};\n\nstatic const ctl_named_node_t stats_arenas_i_node[] = {\n\t{NAME(\"nthreads\"),\tCTL(stats_arenas_i_nthreads)},\n\t{NAME(\"uptime\"),\tCTL(stats_arenas_i_uptime)},\n\t{NAME(\"dss\"),\t\tCTL(stats_arenas_i_dss)},\n\t{NAME(\"dirty_decay_ms\"), CTL(stats_arenas_i_dirty_decay_ms)},\n\t{NAME(\"muzzy_decay_ms\"), CTL(stats_arenas_i_muzzy_decay_ms)},\n\t{NAME(\"pactive\"),\tCTL(stats_arenas_i_pactive)},\n\t{NAME(\"pdirty\"),\tCTL(stats_arenas_i_pdirty)},\n\t{NAME(\"pmuzzy\"),\tCTL(stats_arenas_i_pmuzzy)},\n\t{NAME(\"mapped\"),\tCTL(stats_arenas_i_mapped)},\n\t{NAME(\"retained\"),\tCTL(stats_arenas_i_retained)},\n\t{NAME(\"extent_avail\"),\tCTL(stats_arenas_i_extent_avail)},\n\t{NAME(\"dirty_npurge\"),\tCTL(stats_arenas_i_dirty_npurge)},\n\t{NAME(\"dirty_nmadvise\"), CTL(stats_arenas_i_dirty_nmadvise)},\n\t{NAME(\"dirty_purged\"),\tCTL(stats_arenas_i_dirty_purged)},\n\t{NAME(\"muzzy_npurge\"),\tCTL(stats_arenas_i_muzzy_npurge)},\n\t{NAME(\"muzzy_nmadvise\"), CTL(stats_arenas_i_muzzy_nmadvise)},\n\t{NAME(\"muzzy_purged\"),\tCTL(stats_arenas_i_muzzy_purged)},\n\t{NAME(\"base\"),\t\tCTL(stats_arenas_i_base)},\n\t{NAME(\"internal\"),\tCTL(stats_arenas_i_internal)},\n\t{NAME(\"metadata_thp\"),\tCTL(stats_arenas_i_metadata_thp)},\n\t{NAME(\"tcache_bytes\"),\tCTL(stats_arenas_i_tcache_bytes)},\n\t{NAME(\"resident\"),\tCTL(stats_arenas_i_resident)},\n\t{NAME(\"abandoned_vm\"),\tCTL(stats_arenas_i_abandoned_vm)},\n\t{NAME(\"small\"),\t\tCHILD(named, stats_arenas_i_small)},\n\t{NAME(\"large\"),\t\tCHILD(named, stats_arenas_i_large)},\n\t{NAME(\"bins\"),\t\tCHILD(indexed, stats_arenas_i_bins)},\n\t{NAME(\"lextents\"),\tCHILD(indexed, stats_arenas_i_lextents)},\n\t{NAME(\"extents\"),\tCHILD(indexed, stats_arenas_i_extents)},\n\t{NAME(\"mutexes\"),\tCHILD(named, stats_arenas_i_mutexes)}\n};\nstatic const ctl_named_node_t super_stats_arenas_i_node[] = {\n\t{NAME(\"\"),\t\tCHILD(named, stats_arenas_i)}\n};\n\nstatic const ctl_indexed_node_t stats_arenas_node[] = {\n\t{INDEX(stats_arenas_i)}\n};\n\nstatic const ctl_named_node_t stats_background_thread_node[] = {\n\t{NAME(\"num_threads\"),\tCTL(stats_background_thread_num_threads)},\n\t{NAME(\"num_runs\"),\tCTL(stats_background_thread_num_runs)},\n\t{NAME(\"run_interval\"),\tCTL(stats_background_thread_run_interval)}\n};\n\n#define OP(mtx) MUTEX_PROF_DATA_NODE(mutexes_##mtx)\nMUTEX_PROF_GLOBAL_MUTEXES\n#undef OP\n\nstatic const ctl_named_node_t stats_mutexes_node[] = {\n#define OP(mtx) {NAME(#mtx), CHILD(named, stats_mutexes_##mtx)},\nMUTEX_PROF_GLOBAL_MUTEXES\n#undef OP\n\t{NAME(\"reset\"),\t\tCTL(stats_mutexes_reset)}\n};\n#undef MUTEX_PROF_DATA_NODE\n\nstatic const ctl_named_node_t stats_node[] = {\n\t{NAME(\"allocated\"),\tCTL(stats_allocated)},\n\t{NAME(\"active\"),\tCTL(stats_active)},\n\t{NAME(\"metadata\"),\tCTL(stats_metadata)},\n\t{NAME(\"metadata_thp\"),\tCTL(stats_metadata_thp)},\n\t{NAME(\"resident\"),\tCTL(stats_resident)},\n\t{NAME(\"mapped\"),\tCTL(stats_mapped)},\n\t{NAME(\"retained\"),\tCTL(stats_retained)},\n\t{NAME(\"background_thread\"),\n\t CHILD(named, stats_background_thread)},\n\t{NAME(\"mutexes\"),\tCHILD(named, stats_mutexes)},\n\t{NAME(\"arenas\"),\tCHILD(indexed, stats_arenas)}\n};\n\nstatic const ctl_named_node_t experimental_hooks_node[] = {\n\t{NAME(\"install\"),\tCTL(experimental_hooks_install)},\n\t{NAME(\"remove\"),\tCTL(experimental_hooks_remove)}\n};\n\nstatic const ctl_named_node_t experimental_utilization_node[] = {\n\t{NAME(\"query\"),\t\tCTL(experimental_utilization_query)},\n\t{NAME(\"batch_query\"),\tCTL(experimental_utilization_batch_query)}\n};\n\nstatic const ctl_named_node_t experimental_arenas_i_node[] = {\n\t{NAME(\"pactivep\"),\tCTL(experimental_arenas_i_pactivep)}\n};\nstatic const ctl_named_node_t super_experimental_arenas_i_node[] = {\n\t{NAME(\"\"),\t\tCHILD(named, experimental_arenas_i)}\n};\n\nstatic const ctl_indexed_node_t experimental_arenas_node[] = {\n\t{INDEX(experimental_arenas_i)}\n};\n\nstatic const ctl_named_node_t experimental_node[] = {\n\t{NAME(\"hooks\"),\t\tCHILD(named, experimental_hooks)},\n\t{NAME(\"utilization\"),\tCHILD(named, experimental_utilization)},\n\t{NAME(\"arenas\"),\tCHILD(indexed, experimental_arenas)}\n};\n\nstatic const ctl_named_node_t\troot_node[] = {\n\t{NAME(\"version\"),\tCTL(version)},\n\t{NAME(\"epoch\"),\t\tCTL(epoch)},\n\t{NAME(\"background_thread\"),\tCTL(background_thread)},\n\t{NAME(\"max_background_threads\"),\tCTL(max_background_threads)},\n\t{NAME(\"thread\"),\tCHILD(named, thread)},\n\t{NAME(\"config\"),\tCHILD(named, config)},\n\t{NAME(\"opt\"),\t\tCHILD(named, opt)},\n\t{NAME(\"tcache\"),\tCHILD(named, tcache)},\n\t{NAME(\"arena\"),\t\tCHILD(indexed, arena)},\n\t{NAME(\"arenas\"),\tCHILD(named, arenas)},\n\t{NAME(\"prof\"),\t\tCHILD(named, prof)},\n\t{NAME(\"stats\"),\t\tCHILD(named, stats)},\n\t{NAME(\"experimental\"),\tCHILD(named, experimental)}\n};\nstatic const ctl_named_node_t super_root_node[] = {\n\t{NAME(\"\"),\t\tCHILD(named, root)}\n};\n\n#undef NAME\n#undef CHILD\n#undef CTL\n#undef INDEX\n\n/******************************************************************************/\n\n/*\n * Sets *dst + *src non-atomically.  This is safe, since everything is\n * synchronized by the ctl mutex.\n */\nstatic void\nctl_accum_arena_stats_u64(arena_stats_u64_t *dst, arena_stats_u64_t *src) {\n#ifdef JEMALLOC_ATOMIC_U64\n\tuint64_t cur_dst = atomic_load_u64(dst, ATOMIC_RELAXED);\n\tuint64_t cur_src = atomic_load_u64(src, ATOMIC_RELAXED);\n\tatomic_store_u64(dst, cur_dst + cur_src, ATOMIC_RELAXED);\n#else\n\t*dst += *src;\n#endif\n}\n\n/* Likewise: with ctl mutex synchronization, reading is simple. */\nstatic uint64_t\nctl_arena_stats_read_u64(arena_stats_u64_t *p) {\n#ifdef JEMALLOC_ATOMIC_U64\n\treturn atomic_load_u64(p, ATOMIC_RELAXED);\n#else\n\treturn *p;\n#endif\n}\n\nstatic void\naccum_atomic_zu(atomic_zu_t *dst, atomic_zu_t *src) {\n\tsize_t cur_dst = atomic_load_zu(dst, ATOMIC_RELAXED);\n\tsize_t cur_src = atomic_load_zu(src, ATOMIC_RELAXED);\n\tatomic_store_zu(dst, cur_dst + cur_src, ATOMIC_RELAXED);\n}\n\n/******************************************************************************/\n\nstatic unsigned\narenas_i2a_impl(size_t i, bool compat, bool validate) {\n\tunsigned a;\n\n\tswitch (i) {\n\tcase MALLCTL_ARENAS_ALL:\n\t\ta = 0;\n\t\tbreak;\n\tcase MALLCTL_ARENAS_DESTROYED:\n\t\ta = 1;\n\t\tbreak;\n\tdefault:\n\t\tif (compat && i == ctl_arenas->narenas) {\n\t\t\t/*\n\t\t\t * Provide deprecated backward compatibility for\n\t\t\t * accessing the merged stats at index narenas rather\n\t\t\t * than via MALLCTL_ARENAS_ALL.  This is scheduled for\n\t\t\t * removal in 6.0.0.\n\t\t\t */\n\t\t\ta = 0;\n\t\t} else if (validate && i >= ctl_arenas->narenas) {\n\t\t\ta = UINT_MAX;\n\t\t} else {\n\t\t\t/*\n\t\t\t * This function should never be called for an index\n\t\t\t * more than one past the range of indices that have\n\t\t\t * initialized ctl data.\n\t\t\t */\n\t\t\tassert(i < ctl_arenas->narenas || (!validate && i ==\n\t\t\t    ctl_arenas->narenas));\n\t\t\ta = (unsigned)i + 2;\n\t\t}\n\t\tbreak;\n\t}\n\n\treturn a;\n}\n\nstatic unsigned\narenas_i2a(size_t i) {\n\treturn arenas_i2a_impl(i, true, false);\n}\n\nstatic ctl_arena_t *\narenas_i_impl(tsd_t *tsd, size_t i, bool compat, bool init) {\n\tctl_arena_t *ret;\n\n\tassert(!compat || !init);\n\n\tret = ctl_arenas->arenas[arenas_i2a_impl(i, compat, false)];\n\tif (init && ret == NULL) {\n\t\tif (config_stats) {\n\t\t\tstruct container_s {\n\t\t\t\tctl_arena_t\t\tctl_arena;\n\t\t\t\tctl_arena_stats_t\tastats;\n\t\t\t};\n\t\t\tstruct container_s *cont =\n\t\t\t    (struct container_s *)base_alloc(tsd_tsdn(tsd),\n\t\t\t    b0get(), sizeof(struct container_s), QUANTUM);\n\t\t\tif (cont == NULL) {\n\t\t\t\treturn NULL;\n\t\t\t}\n\t\t\tret = &cont->ctl_arena;\n\t\t\tret->astats = &cont->astats;\n\t\t} else {\n\t\t\tret = (ctl_arena_t *)base_alloc(tsd_tsdn(tsd), b0get(),\n\t\t\t    sizeof(ctl_arena_t), QUANTUM);\n\t\t\tif (ret == NULL) {\n\t\t\t\treturn NULL;\n\t\t\t}\n\t\t}\n\t\tret->arena_ind = (unsigned)i;\n\t\tctl_arenas->arenas[arenas_i2a_impl(i, compat, false)] = ret;\n\t}\n\n\tassert(ret == NULL || arenas_i2a(ret->arena_ind) == arenas_i2a(i));\n\treturn ret;\n}\n\nstatic ctl_arena_t *\narenas_i(size_t i) {\n\tctl_arena_t *ret = arenas_i_impl(tsd_fetch(), i, true, false);\n\tassert(ret != NULL);\n\treturn ret;\n}\n\nstatic void\nctl_arena_clear(ctl_arena_t *ctl_arena) {\n\tctl_arena->nthreads = 0;\n\tctl_arena->dss = dss_prec_names[dss_prec_limit];\n\tctl_arena->dirty_decay_ms = -1;\n\tctl_arena->muzzy_decay_ms = -1;\n\tctl_arena->pactive = 0;\n\tctl_arena->pdirty = 0;\n\tctl_arena->pmuzzy = 0;\n\tif (config_stats) {\n\t\tmemset(&ctl_arena->astats->astats, 0, sizeof(arena_stats_t));\n\t\tctl_arena->astats->allocated_small = 0;\n\t\tctl_arena->astats->nmalloc_small = 0;\n\t\tctl_arena->astats->ndalloc_small = 0;\n\t\tctl_arena->astats->nrequests_small = 0;\n\t\tctl_arena->astats->nfills_small = 0;\n\t\tctl_arena->astats->nflushes_small = 0;\n\t\tmemset(ctl_arena->astats->bstats, 0, SC_NBINS *\n\t\t    sizeof(bin_stats_t));\n\t\tmemset(ctl_arena->astats->lstats, 0, (SC_NSIZES - SC_NBINS) *\n\t\t    sizeof(arena_stats_large_t));\n\t\tmemset(ctl_arena->astats->estats, 0, SC_NPSIZES *\n\t\t    sizeof(arena_stats_extents_t));\n\t}\n}\n\nstatic void\nctl_arena_stats_amerge(tsdn_t *tsdn, ctl_arena_t *ctl_arena, arena_t *arena) {\n\tunsigned i;\n\n\tif (config_stats) {\n\t\tarena_stats_merge(tsdn, arena, &ctl_arena->nthreads,\n\t\t    &ctl_arena->dss, &ctl_arena->dirty_decay_ms,\n\t\t    &ctl_arena->muzzy_decay_ms, &ctl_arena->pactive,\n\t\t    &ctl_arena->pdirty, &ctl_arena->pmuzzy,\n\t\t    &ctl_arena->astats->astats, ctl_arena->astats->bstats,\n\t\t    ctl_arena->astats->lstats, ctl_arena->astats->estats);\n\n\t\tfor (i = 0; i < SC_NBINS; i++) {\n\t\t\tctl_arena->astats->allocated_small +=\n\t\t\t    ctl_arena->astats->bstats[i].curregs *\n\t\t\t    sz_index2size(i);\n\t\t\tctl_arena->astats->nmalloc_small +=\n\t\t\t    ctl_arena->astats->bstats[i].nmalloc;\n\t\t\tctl_arena->astats->ndalloc_small +=\n\t\t\t    ctl_arena->astats->bstats[i].ndalloc;\n\t\t\tctl_arena->astats->nrequests_small +=\n\t\t\t    ctl_arena->astats->bstats[i].nrequests;\n\t\t\tctl_arena->astats->nfills_small +=\n\t\t\t    ctl_arena->astats->bstats[i].nfills;\n\t\t\tctl_arena->astats->nflushes_small +=\n\t\t\t    ctl_arena->astats->bstats[i].nflushes;\n\t\t}\n\t} else {\n\t\tarena_basic_stats_merge(tsdn, arena, &ctl_arena->nthreads,\n\t\t    &ctl_arena->dss, &ctl_arena->dirty_decay_ms,\n\t\t    &ctl_arena->muzzy_decay_ms, &ctl_arena->pactive,\n\t\t    &ctl_arena->pdirty, &ctl_arena->pmuzzy);\n\t}\n}\n\nstatic void\nctl_arena_stats_sdmerge(ctl_arena_t *ctl_sdarena, ctl_arena_t *ctl_arena,\n    bool destroyed) {\n\tunsigned i;\n\n\tif (!destroyed) {\n\t\tctl_sdarena->nthreads += ctl_arena->nthreads;\n\t\tctl_sdarena->pactive += ctl_arena->pactive;\n\t\tctl_sdarena->pdirty += ctl_arena->pdirty;\n\t\tctl_sdarena->pmuzzy += ctl_arena->pmuzzy;\n\t} else {\n\t\tassert(ctl_arena->nthreads == 0);\n\t\tassert(ctl_arena->pactive == 0);\n\t\tassert(ctl_arena->pdirty == 0);\n\t\tassert(ctl_arena->pmuzzy == 0);\n\t}\n\n\tif (config_stats) {\n\t\tctl_arena_stats_t *sdstats = ctl_sdarena->astats;\n\t\tctl_arena_stats_t *astats = ctl_arena->astats;\n\n\t\tif (!destroyed) {\n\t\t\taccum_atomic_zu(&sdstats->astats.mapped,\n\t\t\t    &astats->astats.mapped);\n\t\t\taccum_atomic_zu(&sdstats->astats.retained,\n\t\t\t    &astats->astats.retained);\n\t\t\taccum_atomic_zu(&sdstats->astats.extent_avail,\n\t\t\t    &astats->astats.extent_avail);\n\t\t}\n\n\t\tctl_accum_arena_stats_u64(&sdstats->astats.decay_dirty.npurge,\n\t\t    &astats->astats.decay_dirty.npurge);\n\t\tctl_accum_arena_stats_u64(&sdstats->astats.decay_dirty.nmadvise,\n\t\t    &astats->astats.decay_dirty.nmadvise);\n\t\tctl_accum_arena_stats_u64(&sdstats->astats.decay_dirty.purged,\n\t\t    &astats->astats.decay_dirty.purged);\n\n\t\tctl_accum_arena_stats_u64(&sdstats->astats.decay_muzzy.npurge,\n\t\t    &astats->astats.decay_muzzy.npurge);\n\t\tctl_accum_arena_stats_u64(&sdstats->astats.decay_muzzy.nmadvise,\n\t\t    &astats->astats.decay_muzzy.nmadvise);\n\t\tctl_accum_arena_stats_u64(&sdstats->astats.decay_muzzy.purged,\n\t\t    &astats->astats.decay_muzzy.purged);\n\n#define OP(mtx) malloc_mutex_prof_merge(\t\t\t\t\\\n\t\t    &(sdstats->astats.mutex_prof_data[\t\t\t\\\n\t\t        arena_prof_mutex_##mtx]),\t\t\t\\\n\t\t    &(astats->astats.mutex_prof_data[\t\t\t\\\n\t\t        arena_prof_mutex_##mtx]));\nMUTEX_PROF_ARENA_MUTEXES\n#undef OP\n\t\tif (!destroyed) {\n\t\t\taccum_atomic_zu(&sdstats->astats.base,\n\t\t\t    &astats->astats.base);\n\t\t\taccum_atomic_zu(&sdstats->astats.internal,\n\t\t\t    &astats->astats.internal);\n\t\t\taccum_atomic_zu(&sdstats->astats.resident,\n\t\t\t    &astats->astats.resident);\n\t\t\taccum_atomic_zu(&sdstats->astats.metadata_thp,\n\t\t\t    &astats->astats.metadata_thp);\n\t\t} else {\n\t\t\tassert(atomic_load_zu(\n\t\t\t    &astats->astats.internal, ATOMIC_RELAXED) == 0);\n\t\t}\n\n\t\tif (!destroyed) {\n\t\t\tsdstats->allocated_small += astats->allocated_small;\n\t\t} else {\n\t\t\tassert(astats->allocated_small == 0);\n\t\t}\n\t\tsdstats->nmalloc_small += astats->nmalloc_small;\n\t\tsdstats->ndalloc_small += astats->ndalloc_small;\n\t\tsdstats->nrequests_small += astats->nrequests_small;\n\t\tsdstats->nfills_small += astats->nfills_small;\n\t\tsdstats->nflushes_small += astats->nflushes_small;\n\n\t\tif (!destroyed) {\n\t\t\taccum_atomic_zu(&sdstats->astats.allocated_large,\n\t\t\t    &astats->astats.allocated_large);\n\t\t} else {\n\t\t\tassert(atomic_load_zu(&astats->astats.allocated_large,\n\t\t\t    ATOMIC_RELAXED) == 0);\n\t\t}\n\t\tctl_accum_arena_stats_u64(&sdstats->astats.nmalloc_large,\n\t\t    &astats->astats.nmalloc_large);\n\t\tctl_accum_arena_stats_u64(&sdstats->astats.ndalloc_large,\n\t\t    &astats->astats.ndalloc_large);\n\t\tctl_accum_arena_stats_u64(&sdstats->astats.nrequests_large,\n\t\t    &astats->astats.nrequests_large);\n\t\taccum_atomic_zu(&sdstats->astats.abandoned_vm,\n\t\t    &astats->astats.abandoned_vm);\n\n\t\taccum_atomic_zu(&sdstats->astats.tcache_bytes,\n\t\t    &astats->astats.tcache_bytes);\n\n\t\tif (ctl_arena->arena_ind == 0) {\n\t\t\tsdstats->astats.uptime = astats->astats.uptime;\n\t\t}\n\n\t\t/* Merge bin stats. */\n\t\tfor (i = 0; i < SC_NBINS; i++) {\n\t\t\tsdstats->bstats[i].nmalloc += astats->bstats[i].nmalloc;\n\t\t\tsdstats->bstats[i].ndalloc += astats->bstats[i].ndalloc;\n\t\t\tsdstats->bstats[i].nrequests +=\n\t\t\t    astats->bstats[i].nrequests;\n\t\t\tif (!destroyed) {\n\t\t\t\tsdstats->bstats[i].curregs +=\n\t\t\t\t    astats->bstats[i].curregs;\n\t\t\t} else {\n\t\t\t\tassert(astats->bstats[i].curregs == 0);\n\t\t\t}\n\t\t\tsdstats->bstats[i].nfills += astats->bstats[i].nfills;\n\t\t\tsdstats->bstats[i].nflushes +=\n\t\t\t    astats->bstats[i].nflushes;\n\t\t\tsdstats->bstats[i].nslabs += astats->bstats[i].nslabs;\n\t\t\tsdstats->bstats[i].reslabs += astats->bstats[i].reslabs;\n\t\t\tif (!destroyed) {\n\t\t\t\tsdstats->bstats[i].curslabs +=\n\t\t\t\t    astats->bstats[i].curslabs;\n\t\t\t\tsdstats->bstats[i].nonfull_slabs +=\n\t\t\t\t    astats->bstats[i].nonfull_slabs;\n\t\t\t} else {\n\t\t\t\tassert(astats->bstats[i].curslabs == 0);\n\t\t\t\tassert(astats->bstats[i].nonfull_slabs == 0);\n\t\t\t}\n\t\t\tmalloc_mutex_prof_merge(&sdstats->bstats[i].mutex_data,\n\t\t\t    &astats->bstats[i].mutex_data);\n\t\t}\n\n\t\t/* Merge stats for large allocations. */\n\t\tfor (i = 0; i < SC_NSIZES - SC_NBINS; i++) {\n\t\t\tctl_accum_arena_stats_u64(&sdstats->lstats[i].nmalloc,\n\t\t\t    &astats->lstats[i].nmalloc);\n\t\t\tctl_accum_arena_stats_u64(&sdstats->lstats[i].ndalloc,\n\t\t\t    &astats->lstats[i].ndalloc);\n\t\t\tctl_accum_arena_stats_u64(&sdstats->lstats[i].nrequests,\n\t\t\t    &astats->lstats[i].nrequests);\n\t\t\tif (!destroyed) {\n\t\t\t\tsdstats->lstats[i].curlextents +=\n\t\t\t\t    astats->lstats[i].curlextents;\n\t\t\t} else {\n\t\t\t\tassert(astats->lstats[i].curlextents == 0);\n\t\t\t}\n\t\t}\n\n\t\t/* Merge extents stats. */\n\t\tfor (i = 0; i < SC_NPSIZES; i++) {\n\t\t\taccum_atomic_zu(&sdstats->estats[i].ndirty,\n\t\t\t    &astats->estats[i].ndirty);\n\t\t\taccum_atomic_zu(&sdstats->estats[i].nmuzzy,\n\t\t\t    &astats->estats[i].nmuzzy);\n\t\t\taccum_atomic_zu(&sdstats->estats[i].nretained,\n\t\t\t    &astats->estats[i].nretained);\n\t\t\taccum_atomic_zu(&sdstats->estats[i].dirty_bytes,\n\t\t\t    &astats->estats[i].dirty_bytes);\n\t\t\taccum_atomic_zu(&sdstats->estats[i].muzzy_bytes,\n\t\t\t    &astats->estats[i].muzzy_bytes);\n\t\t\taccum_atomic_zu(&sdstats->estats[i].retained_bytes,\n\t\t\t    &astats->estats[i].retained_bytes);\n\t\t}\n\t}\n}\n\nstatic void\nctl_arena_refresh(tsdn_t *tsdn, arena_t *arena, ctl_arena_t *ctl_sdarena,\n    unsigned i, bool destroyed) {\n\tctl_arena_t *ctl_arena = arenas_i(i);\n\n\tctl_arena_clear(ctl_arena);\n\tctl_arena_stats_amerge(tsdn, ctl_arena, arena);\n\t/* Merge into sum stats as well. */\n\tctl_arena_stats_sdmerge(ctl_sdarena, ctl_arena, destroyed);\n}\n\nstatic unsigned\nctl_arena_init(tsd_t *tsd, extent_hooks_t *extent_hooks) {\n\tunsigned arena_ind;\n\tctl_arena_t *ctl_arena;\n\n\tif ((ctl_arena = ql_last(&ctl_arenas->destroyed, destroyed_link)) !=\n\t    NULL) {\n\t\tql_remove(&ctl_arenas->destroyed, ctl_arena, destroyed_link);\n\t\tarena_ind = ctl_arena->arena_ind;\n\t} else {\n\t\tarena_ind = ctl_arenas->narenas;\n\t}\n\n\t/* Trigger stats allocation. */\n\tif (arenas_i_impl(tsd, arena_ind, false, true) == NULL) {\n\t\treturn UINT_MAX;\n\t}\n\n\t/* Initialize new arena. */\n\tif (arena_init(tsd_tsdn(tsd), arena_ind, extent_hooks) == NULL) {\n\t\treturn UINT_MAX;\n\t}\n\n\tif (arena_ind == ctl_arenas->narenas) {\n\t\tctl_arenas->narenas++;\n\t}\n\n\treturn arena_ind;\n}\n\nstatic void\nctl_background_thread_stats_read(tsdn_t *tsdn) {\n\tbackground_thread_stats_t *stats = &ctl_stats->background_thread;\n\tif (!have_background_thread ||\n\t    background_thread_stats_read(tsdn, stats)) {\n\t\tmemset(stats, 0, sizeof(background_thread_stats_t));\n\t\tnstime_init(&stats->run_interval, 0);\n\t}\n}\n\nstatic void\nctl_refresh(tsdn_t *tsdn) {\n\tunsigned i;\n\tctl_arena_t *ctl_sarena = arenas_i(MALLCTL_ARENAS_ALL);\n\tVARIABLE_ARRAY(arena_t *, tarenas, ctl_arenas->narenas);\n\n\t/*\n\t * Clear sum stats, since they will be merged into by\n\t * ctl_arena_refresh().\n\t */\n\tctl_arena_clear(ctl_sarena);\n\n\tfor (i = 0; i < ctl_arenas->narenas; i++) {\n\t\ttarenas[i] = arena_get(tsdn, i, false);\n\t}\n\n\tfor (i = 0; i < ctl_arenas->narenas; i++) {\n\t\tctl_arena_t *ctl_arena = arenas_i(i);\n\t\tbool initialized = (tarenas[i] != NULL);\n\n\t\tctl_arena->initialized = initialized;\n\t\tif (initialized) {\n\t\t\tctl_arena_refresh(tsdn, tarenas[i], ctl_sarena, i,\n\t\t\t    false);\n\t\t}\n\t}\n\n\tif (config_stats) {\n\t\tctl_stats->allocated = ctl_sarena->astats->allocated_small +\n\t\t    atomic_load_zu(&ctl_sarena->astats->astats.allocated_large,\n\t\t\tATOMIC_RELAXED);\n\t\tctl_stats->active = (ctl_sarena->pactive << LG_PAGE);\n\t\tctl_stats->metadata = atomic_load_zu(\n\t\t    &ctl_sarena->astats->astats.base, ATOMIC_RELAXED) +\n\t\t    atomic_load_zu(&ctl_sarena->astats->astats.internal,\n\t\t\tATOMIC_RELAXED);\n\t\tctl_stats->metadata_thp = atomic_load_zu(\n\t\t    &ctl_sarena->astats->astats.metadata_thp, ATOMIC_RELAXED);\n\t\tctl_stats->resident = atomic_load_zu(\n\t\t    &ctl_sarena->astats->astats.resident, ATOMIC_RELAXED);\n\t\tctl_stats->mapped = atomic_load_zu(\n\t\t    &ctl_sarena->astats->astats.mapped, ATOMIC_RELAXED);\n\t\tctl_stats->retained = atomic_load_zu(\n\t\t    &ctl_sarena->astats->astats.retained, ATOMIC_RELAXED);\n\n\t\tctl_background_thread_stats_read(tsdn);\n\n#define READ_GLOBAL_MUTEX_PROF_DATA(i, mtx)\t\t\t\t\\\n    malloc_mutex_lock(tsdn, &mtx);\t\t\t\t\t\\\n    malloc_mutex_prof_read(tsdn, &ctl_stats->mutex_prof_data[i], &mtx);\t\\\n    malloc_mutex_unlock(tsdn, &mtx);\n\n\t\tif (config_prof && opt_prof) {\n\t\t\tREAD_GLOBAL_MUTEX_PROF_DATA(global_prof_mutex_prof,\n\t\t\t    bt2gctx_mtx);\n\t\t}\n\t\tif (have_background_thread) {\n\t\t\tREAD_GLOBAL_MUTEX_PROF_DATA(\n\t\t\t    global_prof_mutex_background_thread,\n\t\t\t    background_thread_lock);\n\t\t} else {\n\t\t\tmemset(&ctl_stats->mutex_prof_data[\n\t\t\t    global_prof_mutex_background_thread], 0,\n\t\t\t    sizeof(mutex_prof_data_t));\n\t\t}\n\t\t/* We own ctl mutex already. */\n\t\tmalloc_mutex_prof_read(tsdn,\n\t\t    &ctl_stats->mutex_prof_data[global_prof_mutex_ctl],\n\t\t    &ctl_mtx);\n#undef READ_GLOBAL_MUTEX_PROF_DATA\n\t}\n\tctl_arenas->epoch++;\n}\n\nstatic bool\nctl_init(tsd_t *tsd) {\n\tbool ret;\n\ttsdn_t *tsdn = tsd_tsdn(tsd);\n\n\tmalloc_mutex_lock(tsdn, &ctl_mtx);\n\tif (!ctl_initialized) {\n\t\tctl_arena_t *ctl_sarena, *ctl_darena;\n\t\tunsigned i;\n\n\t\t/*\n\t\t * Allocate demand-zeroed space for pointers to the full\n\t\t * range of supported arena indices.\n\t\t */\n\t\tif (ctl_arenas == NULL) {\n\t\t\tctl_arenas = (ctl_arenas_t *)base_alloc(tsdn,\n\t\t\t    b0get(), sizeof(ctl_arenas_t), QUANTUM);\n\t\t\tif (ctl_arenas == NULL) {\n\t\t\t\tret = true;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\t\t}\n\n\t\tif (config_stats && ctl_stats == NULL) {\n\t\t\tctl_stats = (ctl_stats_t *)base_alloc(tsdn, b0get(),\n\t\t\t    sizeof(ctl_stats_t), QUANTUM);\n\t\t\tif (ctl_stats == NULL) {\n\t\t\t\tret = true;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\t\t}\n\n\t\t/*\n\t\t * Allocate space for the current full range of arenas\n\t\t * here rather than doing it lazily elsewhere, in order\n\t\t * to limit when OOM-caused errors can occur.\n\t\t */\n\t\tif ((ctl_sarena = arenas_i_impl(tsd, MALLCTL_ARENAS_ALL, false,\n\t\t    true)) == NULL) {\n\t\t\tret = true;\n\t\t\tgoto label_return;\n\t\t}\n\t\tctl_sarena->initialized = true;\n\n\t\tif ((ctl_darena = arenas_i_impl(tsd, MALLCTL_ARENAS_DESTROYED,\n\t\t    false, true)) == NULL) {\n\t\t\tret = true;\n\t\t\tgoto label_return;\n\t\t}\n\t\tctl_arena_clear(ctl_darena);\n\t\t/*\n\t\t * Don't toggle ctl_darena to initialized until an arena is\n\t\t * actually destroyed, so that arena.<i>.initialized can be used\n\t\t * to query whether the stats are relevant.\n\t\t */\n\n\t\tctl_arenas->narenas = narenas_total_get();\n\t\tfor (i = 0; i < ctl_arenas->narenas; i++) {\n\t\t\tif (arenas_i_impl(tsd, i, false, true) == NULL) {\n\t\t\t\tret = true;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\t\t}\n\n\t\tql_new(&ctl_arenas->destroyed);\n\t\tctl_refresh(tsdn);\n\n\t\tctl_initialized = true;\n\t}\n\n\tret = false;\nlabel_return:\n\tmalloc_mutex_unlock(tsdn, &ctl_mtx);\n\treturn ret;\n}\n\nstatic int\nctl_lookup(tsdn_t *tsdn, const char *name, ctl_node_t const **nodesp,\n    size_t *mibp, size_t *depthp) {\n\tint ret;\n\tconst char *elm, *tdot, *dot;\n\tsize_t elen, i, j;\n\tconst ctl_named_node_t *node;\n\n\telm = name;\n\t/* Equivalent to strchrnul(). */\n\tdot = ((tdot = strchr(elm, '.')) != NULL) ? tdot : strchr(elm, '\\0');\n\telen = (size_t)((uintptr_t)dot - (uintptr_t)elm);\n\tif (elen == 0) {\n\t\tret = ENOENT;\n\t\tgoto label_return;\n\t}\n\tnode = super_root_node;\n\tfor (i = 0; i < *depthp; i++) {\n\t\tassert(node);\n\t\tassert(node->nchildren > 0);\n\t\tif (ctl_named_node(node->children) != NULL) {\n\t\t\tconst ctl_named_node_t *pnode = node;\n\n\t\t\t/* Children are named. */\n\t\t\tfor (j = 0; j < node->nchildren; j++) {\n\t\t\t\tconst ctl_named_node_t *child =\n\t\t\t\t    ctl_named_children(node, j);\n\t\t\t\tif (strlen(child->name) == elen &&\n\t\t\t\t    strncmp(elm, child->name, elen) == 0) {\n\t\t\t\t\tnode = child;\n\t\t\t\t\tif (nodesp != NULL) {\n\t\t\t\t\t\tnodesp[i] =\n\t\t\t\t\t\t    (const ctl_node_t *)node;\n\t\t\t\t\t}\n\t\t\t\t\tmibp[i] = j;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (node == pnode) {\n\t\t\t\tret = ENOENT;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\t\t} else {\n\t\t\tuintmax_t index;\n\t\t\tconst ctl_indexed_node_t *inode;\n\n\t\t\t/* Children are indexed. */\n\t\t\tindex = malloc_strtoumax(elm, NULL, 10);\n\t\t\tif (index == UINTMAX_MAX || index > SIZE_T_MAX) {\n\t\t\t\tret = ENOENT;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\n\t\t\tinode = ctl_indexed_node(node->children);\n\t\t\tnode = inode->index(tsdn, mibp, *depthp, (size_t)index);\n\t\t\tif (node == NULL) {\n\t\t\t\tret = ENOENT;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\n\t\t\tif (nodesp != NULL) {\n\t\t\t\tnodesp[i] = (const ctl_node_t *)node;\n\t\t\t}\n\t\t\tmibp[i] = (size_t)index;\n\t\t}\n\n\t\tif (node->ctl != NULL) {\n\t\t\t/* Terminal node. */\n\t\t\tif (*dot != '\\0') {\n\t\t\t\t/*\n\t\t\t\t * The name contains more elements than are\n\t\t\t\t * in this path through the tree.\n\t\t\t\t */\n\t\t\t\tret = ENOENT;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\t\t\t/* Complete lookup successful. */\n\t\t\t*depthp = i + 1;\n\t\t\tbreak;\n\t\t}\n\n\t\t/* Update elm. */\n\t\tif (*dot == '\\0') {\n\t\t\t/* No more elements. */\n\t\t\tret = ENOENT;\n\t\t\tgoto label_return;\n\t\t}\n\t\telm = &dot[1];\n\t\tdot = ((tdot = strchr(elm, '.')) != NULL) ? tdot :\n\t\t    strchr(elm, '\\0');\n\t\telen = (size_t)((uintptr_t)dot - (uintptr_t)elm);\n\t}\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nint\nctl_byname(tsd_t *tsd, const char *name, void *oldp, size_t *oldlenp,\n    void *newp, size_t newlen) {\n\tint ret;\n\tsize_t depth;\n\tctl_node_t const *nodes[CTL_MAX_DEPTH];\n\tsize_t mib[CTL_MAX_DEPTH];\n\tconst ctl_named_node_t *node;\n\n\tif (!ctl_initialized && ctl_init(tsd)) {\n\t\tret = EAGAIN;\n\t\tgoto label_return;\n\t}\n\n\tdepth = CTL_MAX_DEPTH;\n\tret = ctl_lookup(tsd_tsdn(tsd), name, nodes, mib, &depth);\n\tif (ret != 0) {\n\t\tgoto label_return;\n\t}\n\n\tnode = ctl_named_node(nodes[depth-1]);\n\tif (node != NULL && node->ctl) {\n\t\tret = node->ctl(tsd, mib, depth, oldp, oldlenp, newp, newlen);\n\t} else {\n\t\t/* The name refers to a partial path through the ctl tree. */\n\t\tret = ENOENT;\n\t}\n\nlabel_return:\n\treturn(ret);\n}\n\nint\nctl_nametomib(tsd_t *tsd, const char *name, size_t *mibp, size_t *miblenp) {\n\tint ret;\n\n\tif (!ctl_initialized && ctl_init(tsd)) {\n\t\tret = EAGAIN;\n\t\tgoto label_return;\n\t}\n\n\tret = ctl_lookup(tsd_tsdn(tsd), name, NULL, mibp, miblenp);\nlabel_return:\n\treturn(ret);\n}\n\nint\nctl_bymib(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tconst ctl_named_node_t *node;\n\tsize_t i;\n\n\tif (!ctl_initialized && ctl_init(tsd)) {\n\t\tret = EAGAIN;\n\t\tgoto label_return;\n\t}\n\n\t/* Iterate down the tree. */\n\tnode = super_root_node;\n\tfor (i = 0; i < miblen; i++) {\n\t\tassert(node);\n\t\tassert(node->nchildren > 0);\n\t\tif (ctl_named_node(node->children) != NULL) {\n\t\t\t/* Children are named. */\n\t\t\tif (node->nchildren <= mib[i]) {\n\t\t\t\tret = ENOENT;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\t\t\tnode = ctl_named_children(node, mib[i]);\n\t\t} else {\n\t\t\tconst ctl_indexed_node_t *inode;\n\n\t\t\t/* Indexed element. */\n\t\t\tinode = ctl_indexed_node(node->children);\n\t\t\tnode = inode->index(tsd_tsdn(tsd), mib, miblen, mib[i]);\n\t\t\tif (node == NULL) {\n\t\t\t\tret = ENOENT;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\t\t}\n\t}\n\n\t/* Call the ctl function. */\n\tif (node && node->ctl) {\n\t\tret = node->ctl(tsd, mib, miblen, oldp, oldlenp, newp, newlen);\n\t} else {\n\t\t/* Partial MIB. */\n\t\tret = ENOENT;\n\t}\n\nlabel_return:\n\treturn(ret);\n}\n\nbool\nctl_boot(void) {\n\tif (malloc_mutex_init(&ctl_mtx, \"ctl\", WITNESS_RANK_CTL,\n\t    malloc_mutex_rank_exclusive)) {\n\t\treturn true;\n\t}\n\n\tctl_initialized = false;\n\n\treturn false;\n}\n\nvoid\nctl_prefork(tsdn_t *tsdn) {\n\tmalloc_mutex_prefork(tsdn, &ctl_mtx);\n}\n\nvoid\nctl_postfork_parent(tsdn_t *tsdn) {\n\tmalloc_mutex_postfork_parent(tsdn, &ctl_mtx);\n}\n\nvoid\nctl_postfork_child(tsdn_t *tsdn) {\n\tmalloc_mutex_postfork_child(tsdn, &ctl_mtx);\n}\n\n/******************************************************************************/\n/* *_ctl() functions. */\n\n#define READONLY()\tdo {\t\t\t\t\t\t\\\n\tif (newp != NULL || newlen != 0) {\t\t\t\t\\\n\t\tret = EPERM;\t\t\t\t\t\t\\\n\t\tgoto label_return;\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define WRITEONLY()\tdo {\t\t\t\t\t\t\\\n\tif (oldp != NULL || oldlenp != NULL) {\t\t\t\t\\\n\t\tret = EPERM;\t\t\t\t\t\t\\\n\t\tgoto label_return;\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define READ_XOR_WRITE()\tdo {\t\t\t\t\t\\\n\tif ((oldp != NULL && oldlenp != NULL) && (newp != NULL ||\t\\\n\t    newlen != 0)) {\t\t\t\t\t\t\\\n\t\tret = EPERM;\t\t\t\t\t\t\\\n\t\tgoto label_return;\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define READ(v, t)\tdo {\t\t\t\t\t\t\\\n\tif (oldp != NULL && oldlenp != NULL) {\t\t\t\t\\\n\t\tif (*oldlenp != sizeof(t)) {\t\t\t\t\\\n\t\t\tsize_t\tcopylen = (sizeof(t) <= *oldlenp)\t\\\n\t\t\t    ? sizeof(t) : *oldlenp;\t\t\t\\\n\t\t\tmemcpy(oldp, (void *)&(v), copylen);\t\t\\\n\t\t\tret = EINVAL;\t\t\t\t\t\\\n\t\t\tgoto label_return;\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\t*(t *)oldp = (v);\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define WRITE(v, t)\tdo {\t\t\t\t\t\t\\\n\tif (newp != NULL) {\t\t\t\t\t\t\\\n\t\tif (newlen != sizeof(t)) {\t\t\t\t\\\n\t\t\tret = EINVAL;\t\t\t\t\t\\\n\t\t\tgoto label_return;\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\t(v) = *(t *)newp;\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define MIB_UNSIGNED(v, i) do {\t\t\t\t\t\t\\\n\tif (mib[i] > UINT_MAX) {\t\t\t\t\t\\\n\t\tret = EFAULT;\t\t\t\t\t\t\\\n\t\tgoto label_return;\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tv = (unsigned)mib[i];\t\t\t\t\t\t\\\n} while (0)\n\n/*\n * There's a lot of code duplication in the following macros due to limitations\n * in how nested cpp macros are expanded.\n */\n#define CTL_RO_CLGEN(c, l, n, v, t)\t\t\t\t\t\\\nstatic int\t\t\t\t\t\t\t\t\\\nn##_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\t\\\n    size_t *oldlenp, void *newp, size_t newlen) {\t\t\t\\\n\tint ret;\t\t\t\t\t\t\t\\\n\tt oldval;\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tif (!(c)) {\t\t\t\t\t\t\t\\\n\t\treturn ENOENT;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tif (l) {\t\t\t\t\t\t\t\\\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &ctl_mtx);\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tREADONLY();\t\t\t\t\t\t\t\\\n\toldval = (v);\t\t\t\t\t\t\t\\\n\tREAD(oldval, t);\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tret = 0;\t\t\t\t\t\t\t\\\nlabel_return:\t\t\t\t\t\t\t\t\\\n\tif (l) {\t\t\t\t\t\t\t\\\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &ctl_mtx);\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\treturn ret;\t\t\t\t\t\t\t\\\n}\n\n#define CTL_RO_CGEN(c, n, v, t)\t\t\t\t\t\t\\\nstatic int\t\t\t\t\t\t\t\t\\\nn##_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, \\\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\t\t\t\\\n\tint ret;\t\t\t\t\t\t\t\\\n\tt oldval;\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tif (!(c)) {\t\t\t\t\t\t\t\\\n\t\treturn ENOENT;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &ctl_mtx);\t\t\t\\\n\tREADONLY();\t\t\t\t\t\t\t\\\n\toldval = (v);\t\t\t\t\t\t\t\\\n\tREAD(oldval, t);\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tret = 0;\t\t\t\t\t\t\t\\\nlabel_return:\t\t\t\t\t\t\t\t\\\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &ctl_mtx);\t\t\t\\\n\treturn ret;\t\t\t\t\t\t\t\\\n}\n\n#define CTL_RO_GEN(n, v, t)\t\t\t\t\t\t\\\nstatic int\t\t\t\t\t\t\t\t\\\nn##_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\t\\\n    size_t *oldlenp, void *newp, size_t newlen) {\t\t\t\\\n\tint ret;\t\t\t\t\t\t\t\\\n\tt oldval;\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &ctl_mtx);\t\t\t\\\n\tREADONLY();\t\t\t\t\t\t\t\\\n\toldval = (v);\t\t\t\t\t\t\t\\\n\tREAD(oldval, t);\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tret = 0;\t\t\t\t\t\t\t\\\nlabel_return:\t\t\t\t\t\t\t\t\\\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &ctl_mtx);\t\t\t\\\n\treturn ret;\t\t\t\t\t\t\t\\\n}\n\n/*\n * ctl_mtx is not acquired, under the assumption that no pertinent data will\n * mutate during the call.\n */\n#define CTL_RO_NL_CGEN(c, n, v, t)\t\t\t\t\t\\\nstatic int\t\t\t\t\t\t\t\t\\\nn##_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, \\\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\t\t\t\\\n\tint ret;\t\t\t\t\t\t\t\\\n\tt oldval;\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tif (!(c)) {\t\t\t\t\t\t\t\\\n\t\treturn ENOENT;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tREADONLY();\t\t\t\t\t\t\t\\\n\toldval = (v);\t\t\t\t\t\t\t\\\n\tREAD(oldval, t);\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tret = 0;\t\t\t\t\t\t\t\\\nlabel_return:\t\t\t\t\t\t\t\t\\\n\treturn ret;\t\t\t\t\t\t\t\\\n}\n\n#define CTL_RO_NL_GEN(n, v, t)\t\t\t\t\t\t\\\nstatic int\t\t\t\t\t\t\t\t\\\nn##_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, \\\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\t\t\t\\\n\tint ret;\t\t\t\t\t\t\t\\\n\tt oldval;\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tREADONLY();\t\t\t\t\t\t\t\\\n\toldval = (v);\t\t\t\t\t\t\t\\\n\tREAD(oldval, t);\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tret = 0;\t\t\t\t\t\t\t\\\nlabel_return:\t\t\t\t\t\t\t\t\\\n\treturn ret;\t\t\t\t\t\t\t\\\n}\n\n#define CTL_TSD_RO_NL_CGEN(c, n, m, t)\t\t\t\t\t\\\nstatic int\t\t\t\t\t\t\t\t\\\nn##_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\t\\\n    size_t *oldlenp, void *newp, size_t newlen) {\t\t\t\\\n\tint ret;\t\t\t\t\t\t\t\\\n\tt oldval;\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tif (!(c)) {\t\t\t\t\t\t\t\\\n\t\treturn ENOENT;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tREADONLY();\t\t\t\t\t\t\t\\\n\toldval = (m(tsd));\t\t\t\t\t\t\\\n\tREAD(oldval, t);\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tret = 0;\t\t\t\t\t\t\t\\\nlabel_return:\t\t\t\t\t\t\t\t\\\n\treturn ret;\t\t\t\t\t\t\t\\\n}\n\n#define CTL_RO_CONFIG_GEN(n, t)\t\t\t\t\t\t\\\nstatic int\t\t\t\t\t\t\t\t\\\nn##_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, \\\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\t\t\t\\\n\tint ret;\t\t\t\t\t\t\t\\\n\tt oldval;\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tREADONLY();\t\t\t\t\t\t\t\\\n\toldval = n;\t\t\t\t\t\t\t\\\n\tREAD(oldval, t);\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tret = 0;\t\t\t\t\t\t\t\\\nlabel_return:\t\t\t\t\t\t\t\t\\\n\treturn ret;\t\t\t\t\t\t\t\\\n}\n\n/******************************************************************************/\n\nCTL_RO_NL_GEN(version, JEMALLOC_VERSION, const char *)\n\nstatic int\nepoch_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tUNUSED uint64_t newval;\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &ctl_mtx);\n\tWRITE(newval, uint64_t);\n\tif (newp != NULL) {\n\t\tctl_refresh(tsd_tsdn(tsd));\n\t}\n\tREAD(ctl_arenas->epoch, uint64_t);\n\n\tret = 0;\nlabel_return:\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &ctl_mtx);\n\treturn ret;\n}\n\nstatic int\nbackground_thread_ctl(tsd_t *tsd, const size_t *mib,\n    size_t miblen, void *oldp, size_t *oldlenp,\n    void *newp, size_t newlen) {\n\tint ret;\n\tbool oldval;\n\n\tif (!have_background_thread) {\n\t\treturn ENOENT;\n\t}\n\tbackground_thread_ctl_init(tsd_tsdn(tsd));\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &ctl_mtx);\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &background_thread_lock);\n\tif (newp == NULL) {\n\t\toldval = background_thread_enabled();\n\t\tREAD(oldval, bool);\n\t} else {\n\t\tif (newlen != sizeof(bool)) {\n\t\t\tret = EINVAL;\n\t\t\tgoto label_return;\n\t\t}\n\t\toldval = background_thread_enabled();\n\t\tREAD(oldval, bool);\n\n\t\tbool newval = *(bool *)newp;\n\t\tif (newval == oldval) {\n\t\t\tret = 0;\n\t\t\tgoto label_return;\n\t\t}\n\n\t\tbackground_thread_enabled_set(tsd_tsdn(tsd), newval);\n\t\tif (newval) {\n\t\t\tif (background_threads_enable(tsd)) {\n\t\t\t\tret = EFAULT;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\t\t} else {\n\t\t\tif (background_threads_disable(tsd)) {\n\t\t\t\tret = EFAULT;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\t\t}\n\t}\n\tret = 0;\nlabel_return:\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &background_thread_lock);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &ctl_mtx);\n\n\treturn ret;\n}\n\nstatic int\nmax_background_threads_ctl(tsd_t *tsd, const size_t *mib,\n    size_t miblen, void *oldp, size_t *oldlenp, void *newp,\n    size_t newlen) {\n\tint ret;\n\tsize_t oldval;\n\n\tif (!have_background_thread) {\n\t\treturn ENOENT;\n\t}\n\tbackground_thread_ctl_init(tsd_tsdn(tsd));\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &ctl_mtx);\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &background_thread_lock);\n\tif (newp == NULL) {\n\t\toldval = max_background_threads;\n\t\tREAD(oldval, size_t);\n\t} else {\n\t\tif (newlen != sizeof(size_t)) {\n\t\t\tret = EINVAL;\n\t\t\tgoto label_return;\n\t\t}\n\t\toldval = max_background_threads;\n\t\tREAD(oldval, size_t);\n\n\t\tsize_t newval = *(size_t *)newp;\n\t\tif (newval == oldval) {\n\t\t\tret = 0;\n\t\t\tgoto label_return;\n\t\t}\n\t\tif (newval > opt_max_background_threads) {\n\t\t\tret = EINVAL;\n\t\t\tgoto label_return;\n\t\t}\n\n\t\tif (background_thread_enabled()) {\n\t\t\tbackground_thread_enabled_set(tsd_tsdn(tsd), false);\n\t\t\tif (background_threads_disable(tsd)) {\n\t\t\t\tret = EFAULT;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\t\t\tmax_background_threads = newval;\n\t\t\tbackground_thread_enabled_set(tsd_tsdn(tsd), true);\n\t\t\tif (background_threads_enable(tsd)) {\n\t\t\t\tret = EFAULT;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\t\t} else {\n\t\t\tmax_background_threads = newval;\n\t\t}\n\t}\n\tret = 0;\nlabel_return:\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &background_thread_lock);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &ctl_mtx);\n\n\treturn ret;\n}\n\n/******************************************************************************/\n\nCTL_RO_CONFIG_GEN(config_cache_oblivious, bool)\nCTL_RO_CONFIG_GEN(config_debug, bool)\nCTL_RO_CONFIG_GEN(config_fill, bool)\nCTL_RO_CONFIG_GEN(config_lazy_lock, bool)\nCTL_RO_CONFIG_GEN(config_malloc_conf, const char *)\nCTL_RO_CONFIG_GEN(config_opt_safety_checks, bool)\nCTL_RO_CONFIG_GEN(config_prof, bool)\nCTL_RO_CONFIG_GEN(config_prof_libgcc, bool)\nCTL_RO_CONFIG_GEN(config_prof_libunwind, bool)\nCTL_RO_CONFIG_GEN(config_stats, bool)\nCTL_RO_CONFIG_GEN(config_utrace, bool)\nCTL_RO_CONFIG_GEN(config_xmalloc, bool)\n\n/******************************************************************************/\n\nCTL_RO_NL_GEN(opt_abort, opt_abort, bool)\nCTL_RO_NL_GEN(opt_abort_conf, opt_abort_conf, bool)\nCTL_RO_NL_GEN(opt_confirm_conf, opt_confirm_conf, bool)\nCTL_RO_NL_GEN(opt_metadata_thp, metadata_thp_mode_names[opt_metadata_thp],\n    const char *)\nCTL_RO_NL_GEN(opt_retain, opt_retain, bool)\nCTL_RO_NL_GEN(opt_dss, opt_dss, const char *)\nCTL_RO_NL_GEN(opt_narenas, opt_narenas, unsigned)\nCTL_RO_NL_GEN(opt_percpu_arena, percpu_arena_mode_names[opt_percpu_arena],\n    const char *)\nCTL_RO_NL_GEN(opt_oversize_threshold, opt_oversize_threshold, size_t)\nCTL_RO_NL_GEN(opt_background_thread, opt_background_thread, bool)\nCTL_RO_NL_GEN(opt_max_background_threads, opt_max_background_threads, size_t)\nCTL_RO_NL_GEN(opt_dirty_decay_ms, opt_dirty_decay_ms, ssize_t)\nCTL_RO_NL_GEN(opt_muzzy_decay_ms, opt_muzzy_decay_ms, ssize_t)\nCTL_RO_NL_GEN(opt_stats_print, opt_stats_print, bool)\nCTL_RO_NL_GEN(opt_stats_print_opts, opt_stats_print_opts, const char *)\nCTL_RO_NL_CGEN(config_fill, opt_junk, opt_junk, const char *)\nCTL_RO_NL_CGEN(config_fill, opt_zero, opt_zero, bool)\nCTL_RO_NL_CGEN(config_utrace, opt_utrace, opt_utrace, bool)\nCTL_RO_NL_CGEN(config_xmalloc, opt_xmalloc, opt_xmalloc, bool)\nCTL_RO_NL_GEN(opt_tcache, opt_tcache, bool)\nCTL_RO_NL_GEN(opt_thp, thp_mode_names[opt_thp], const char *)\nCTL_RO_NL_GEN(opt_lg_extent_max_active_fit, opt_lg_extent_max_active_fit,\n    size_t)\nCTL_RO_NL_GEN(opt_lg_tcache_max, opt_lg_tcache_max, ssize_t)\nCTL_RO_NL_CGEN(config_prof, opt_prof, opt_prof, bool)\nCTL_RO_NL_CGEN(config_prof, opt_prof_prefix, opt_prof_prefix, const char *)\nCTL_RO_NL_CGEN(config_prof, opt_prof_active, opt_prof_active, bool)\nCTL_RO_NL_CGEN(config_prof, opt_prof_thread_active_init,\n    opt_prof_thread_active_init, bool)\nCTL_RO_NL_CGEN(config_prof, opt_lg_prof_sample, opt_lg_prof_sample, size_t)\nCTL_RO_NL_CGEN(config_prof, opt_prof_accum, opt_prof_accum, bool)\nCTL_RO_NL_CGEN(config_prof, opt_lg_prof_interval, opt_lg_prof_interval, ssize_t)\nCTL_RO_NL_CGEN(config_prof, opt_prof_gdump, opt_prof_gdump, bool)\nCTL_RO_NL_CGEN(config_prof, opt_prof_final, opt_prof_final, bool)\nCTL_RO_NL_CGEN(config_prof, opt_prof_leak, opt_prof_leak, bool)\n\n/******************************************************************************/\n\nstatic int\nthread_arena_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tarena_t *oldarena;\n\tunsigned newind, oldind;\n\n\toldarena = arena_choose(tsd, NULL);\n\tif (oldarena == NULL) {\n\t\treturn EAGAIN;\n\t}\n\tnewind = oldind = arena_ind_get(oldarena);\n\tWRITE(newind, unsigned);\n\tREAD(oldind, unsigned);\n\n\tif (newind != oldind) {\n\t\tarena_t *newarena;\n\n\t\tif (newind >= narenas_total_get()) {\n\t\t\t/* New arena index is out of range. */\n\t\t\tret = EFAULT;\n\t\t\tgoto label_return;\n\t\t}\n\n\t\tif (have_percpu_arena &&\n\t\t    PERCPU_ARENA_ENABLED(opt_percpu_arena)) {\n\t\t\tif (newind < percpu_arena_ind_limit(opt_percpu_arena)) {\n\t\t\t\t/*\n\t\t\t\t * If perCPU arena is enabled, thread_arena\n\t\t\t\t * control is not allowed for the auto arena\n\t\t\t\t * range.\n\t\t\t\t */\n\t\t\t\tret = EPERM;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\t\t}\n\n\t\t/* Initialize arena if necessary. */\n\t\tnewarena = arena_get(tsd_tsdn(tsd), newind, true);\n\t\tif (newarena == NULL) {\n\t\t\tret = EAGAIN;\n\t\t\tgoto label_return;\n\t\t}\n\t\t/* Set new arena/tcache associations. */\n\t\tarena_migrate(tsd, oldind, newind);\n\t\tif (tcache_available(tsd)) {\n\t\t\ttcache_arena_reassociate(tsd_tsdn(tsd),\n\t\t\t    tsd_tcachep_get(tsd), newarena);\n\t\t}\n\t}\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nCTL_TSD_RO_NL_CGEN(config_stats, thread_allocated, tsd_thread_allocated_get,\n    uint64_t)\nCTL_TSD_RO_NL_CGEN(config_stats, thread_allocatedp, tsd_thread_allocatedp_get,\n    uint64_t *)\nCTL_TSD_RO_NL_CGEN(config_stats, thread_deallocated, tsd_thread_deallocated_get,\n    uint64_t)\nCTL_TSD_RO_NL_CGEN(config_stats, thread_deallocatedp,\n    tsd_thread_deallocatedp_get, uint64_t *)\n\nstatic int\nthread_tcache_enabled_ctl(tsd_t *tsd, const size_t *mib,\n    size_t miblen, void *oldp, size_t *oldlenp, void *newp,\n    size_t newlen) {\n\tint ret;\n\tbool oldval;\n\n\toldval = tcache_enabled_get(tsd);\n\tif (newp != NULL) {\n\t\tif (newlen != sizeof(bool)) {\n\t\t\tret = EINVAL;\n\t\t\tgoto label_return;\n\t\t}\n\t\ttcache_enabled_set(tsd, *(bool *)newp);\n\t}\n\tREAD(oldval, bool);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\nthread_tcache_flush_ctl(tsd_t *tsd, const size_t *mib,\n    size_t miblen, void *oldp, size_t *oldlenp, void *newp,\n    size_t newlen) {\n\tint ret;\n\n\tif (!tcache_available(tsd)) {\n\t\tret = EFAULT;\n\t\tgoto label_return;\n\t}\n\n\tREADONLY();\n\tWRITEONLY();\n\n\ttcache_flush(tsd);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\nthread_prof_name_ctl(tsd_t *tsd, const size_t *mib,\n    size_t miblen, void *oldp, size_t *oldlenp, void *newp,\n    size_t newlen) {\n\tint ret;\n\n\tif (!config_prof) {\n\t\treturn ENOENT;\n\t}\n\n\tREAD_XOR_WRITE();\n\n\tif (newp != NULL) {\n\t\tif (newlen != sizeof(const char *)) {\n\t\t\tret = EINVAL;\n\t\t\tgoto label_return;\n\t\t}\n\n\t\tif ((ret = prof_thread_name_set(tsd, *(const char **)newp)) !=\n\t\t    0) {\n\t\t\tgoto label_return;\n\t\t}\n\t} else {\n\t\tconst char *oldname = prof_thread_name_get(tsd);\n\t\tREAD(oldname, const char *);\n\t}\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\nthread_prof_active_ctl(tsd_t *tsd, const size_t *mib,\n    size_t miblen, void *oldp, size_t *oldlenp, void *newp,\n    size_t newlen) {\n\tint ret;\n\tbool oldval;\n\n\tif (!config_prof) {\n\t\treturn ENOENT;\n\t}\n\n\toldval = prof_thread_active_get(tsd);\n\tif (newp != NULL) {\n\t\tif (newlen != sizeof(bool)) {\n\t\t\tret = EINVAL;\n\t\t\tgoto label_return;\n\t\t}\n\t\tif (prof_thread_active_set(tsd, *(bool *)newp)) {\n\t\t\tret = EAGAIN;\n\t\t\tgoto label_return;\n\t\t}\n\t}\n\tREAD(oldval, bool);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\n/******************************************************************************/\n\nstatic int\ntcache_create_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tunsigned tcache_ind;\n\n\tREADONLY();\n\tif (tcaches_create(tsd, &tcache_ind)) {\n\t\tret = EFAULT;\n\t\tgoto label_return;\n\t}\n\tREAD(tcache_ind, unsigned);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\ntcache_flush_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tunsigned tcache_ind;\n\n\tWRITEONLY();\n\ttcache_ind = UINT_MAX;\n\tWRITE(tcache_ind, unsigned);\n\tif (tcache_ind == UINT_MAX) {\n\t\tret = EFAULT;\n\t\tgoto label_return;\n\t}\n\ttcaches_flush(tsd, tcache_ind);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\ntcache_destroy_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tunsigned tcache_ind;\n\n\tWRITEONLY();\n\ttcache_ind = UINT_MAX;\n\tWRITE(tcache_ind, unsigned);\n\tif (tcache_ind == UINT_MAX) {\n\t\tret = EFAULT;\n\t\tgoto label_return;\n\t}\n\ttcaches_destroy(tsd, tcache_ind);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\n/******************************************************************************/\n\nstatic int\narena_i_initialized_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\ttsdn_t *tsdn = tsd_tsdn(tsd);\n\tunsigned arena_ind;\n\tbool initialized;\n\n\tREADONLY();\n\tMIB_UNSIGNED(arena_ind, 1);\n\n\tmalloc_mutex_lock(tsdn, &ctl_mtx);\n\tinitialized = arenas_i(arena_ind)->initialized;\n\tmalloc_mutex_unlock(tsdn, &ctl_mtx);\n\n\tREAD(initialized, bool);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic void\narena_i_decay(tsdn_t *tsdn, unsigned arena_ind, bool all) {\n\tmalloc_mutex_lock(tsdn, &ctl_mtx);\n\t{\n\t\tunsigned narenas = ctl_arenas->narenas;\n\n\t\t/*\n\t\t * Access via index narenas is deprecated, and scheduled for\n\t\t * removal in 6.0.0.\n\t\t */\n\t\tif (arena_ind == MALLCTL_ARENAS_ALL || arena_ind == narenas) {\n\t\t\tunsigned i;\n\t\t\tVARIABLE_ARRAY(arena_t *, tarenas, narenas);\n\n\t\t\tfor (i = 0; i < narenas; i++) {\n\t\t\t\ttarenas[i] = arena_get(tsdn, i, false);\n\t\t\t}\n\n\t\t\t/*\n\t\t\t * No further need to hold ctl_mtx, since narenas and\n\t\t\t * tarenas contain everything needed below.\n\t\t\t */\n\t\t\tmalloc_mutex_unlock(tsdn, &ctl_mtx);\n\n\t\t\tfor (i = 0; i < narenas; i++) {\n\t\t\t\tif (tarenas[i] != NULL) {\n\t\t\t\t\tarena_decay(tsdn, tarenas[i], false,\n\t\t\t\t\t    all);\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tarena_t *tarena;\n\n\t\t\tassert(arena_ind < narenas);\n\n\t\t\ttarena = arena_get(tsdn, arena_ind, false);\n\n\t\t\t/* No further need to hold ctl_mtx. */\n\t\t\tmalloc_mutex_unlock(tsdn, &ctl_mtx);\n\n\t\t\tif (tarena != NULL) {\n\t\t\t\tarena_decay(tsdn, tarena, false, all);\n\t\t\t}\n\t\t}\n\t}\n}\n\nstatic int\narena_i_decay_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tunsigned arena_ind;\n\n\tREADONLY();\n\tWRITEONLY();\n\tMIB_UNSIGNED(arena_ind, 1);\n\tarena_i_decay(tsd_tsdn(tsd), arena_ind, false);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\narena_i_purge_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tunsigned arena_ind;\n\n\tREADONLY();\n\tWRITEONLY();\n\tMIB_UNSIGNED(arena_ind, 1);\n\tarena_i_decay(tsd_tsdn(tsd), arena_ind, true);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\narena_i_reset_destroy_helper(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen, unsigned *arena_ind,\n    arena_t **arena) {\n\tint ret;\n\n\tREADONLY();\n\tWRITEONLY();\n\tMIB_UNSIGNED(*arena_ind, 1);\n\n\t*arena = arena_get(tsd_tsdn(tsd), *arena_ind, false);\n\tif (*arena == NULL || arena_is_auto(*arena)) {\n\t\tret = EFAULT;\n\t\tgoto label_return;\n\t}\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic void\narena_reset_prepare_background_thread(tsd_t *tsd, unsigned arena_ind) {\n\t/* Temporarily disable the background thread during arena reset. */\n\tif (have_background_thread) {\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &background_thread_lock);\n\t\tif (background_thread_enabled()) {\n\t\t\tbackground_thread_info_t *info =\n\t\t\t    background_thread_info_get(arena_ind);\n\t\t\tassert(info->state == background_thread_started);\n\t\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);\n\t\t\tinfo->state = background_thread_paused;\n\t\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);\n\t\t}\n\t}\n}\n\nstatic void\narena_reset_finish_background_thread(tsd_t *tsd, unsigned arena_ind) {\n\tif (have_background_thread) {\n\t\tif (background_thread_enabled()) {\n\t\t\tbackground_thread_info_t *info =\n\t\t\t    background_thread_info_get(arena_ind);\n\t\t\tassert(info->state == background_thread_paused);\n\t\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);\n\t\t\tinfo->state = background_thread_started;\n\t\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);\n\t\t}\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &background_thread_lock);\n\t}\n}\n\nstatic int\narena_i_reset_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tunsigned arena_ind;\n\tarena_t *arena;\n\n\tret = arena_i_reset_destroy_helper(tsd, mib, miblen, oldp, oldlenp,\n\t    newp, newlen, &arena_ind, &arena);\n\tif (ret != 0) {\n\t\treturn ret;\n\t}\n\n\tarena_reset_prepare_background_thread(tsd, arena_ind);\n\tarena_reset(tsd, arena);\n\tarena_reset_finish_background_thread(tsd, arena_ind);\n\n\treturn ret;\n}\n\nstatic int\narena_i_destroy_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tunsigned arena_ind;\n\tarena_t *arena;\n\tctl_arena_t *ctl_darena, *ctl_arena;\n\n\tret = arena_i_reset_destroy_helper(tsd, mib, miblen, oldp, oldlenp,\n\t    newp, newlen, &arena_ind, &arena);\n\tif (ret != 0) {\n\t\tgoto label_return;\n\t}\n\n\tif (arena_nthreads_get(arena, false) != 0 || arena_nthreads_get(arena,\n\t    true) != 0) {\n\t\tret = EFAULT;\n\t\tgoto label_return;\n\t}\n\n\tarena_reset_prepare_background_thread(tsd, arena_ind);\n\t/* Merge stats after resetting and purging arena. */\n\tarena_reset(tsd, arena);\n\tarena_decay(tsd_tsdn(tsd), arena, false, true);\n\tctl_darena = arenas_i(MALLCTL_ARENAS_DESTROYED);\n\tctl_darena->initialized = true;\n\tctl_arena_refresh(tsd_tsdn(tsd), arena, ctl_darena, arena_ind, true);\n\t/* Destroy arena. */\n\tarena_destroy(tsd, arena);\n\tctl_arena = arenas_i(arena_ind);\n\tctl_arena->initialized = false;\n\t/* Record arena index for later recycling via arenas.create. */\n\tql_elm_new(ctl_arena, destroyed_link);\n\tql_tail_insert(&ctl_arenas->destroyed, ctl_arena, destroyed_link);\n\tarena_reset_finish_background_thread(tsd, arena_ind);\n\n\tassert(ret == 0);\nlabel_return:\n\treturn ret;\n}\n\nstatic int\narena_i_dss_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tconst char *dss = NULL;\n\tunsigned arena_ind;\n\tdss_prec_t dss_prec_old = dss_prec_limit;\n\tdss_prec_t dss_prec = dss_prec_limit;\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &ctl_mtx);\n\tWRITE(dss, const char *);\n\tMIB_UNSIGNED(arena_ind, 1);\n\tif (dss != NULL) {\n\t\tint i;\n\t\tbool match = false;\n\n\t\tfor (i = 0; i < dss_prec_limit; i++) {\n\t\t\tif (strcmp(dss_prec_names[i], dss) == 0) {\n\t\t\t\tdss_prec = i;\n\t\t\t\tmatch = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif (!match) {\n\t\t\tret = EINVAL;\n\t\t\tgoto label_return;\n\t\t}\n\t}\n\n\t/*\n\t * Access via index narenas is deprecated, and scheduled for removal in\n\t * 6.0.0.\n\t */\n\tif (arena_ind == MALLCTL_ARENAS_ALL || arena_ind ==\n\t    ctl_arenas->narenas) {\n\t\tif (dss_prec != dss_prec_limit &&\n\t\t    extent_dss_prec_set(dss_prec)) {\n\t\t\tret = EFAULT;\n\t\t\tgoto label_return;\n\t\t}\n\t\tdss_prec_old = extent_dss_prec_get();\n\t} else {\n\t\tarena_t *arena = arena_get(tsd_tsdn(tsd), arena_ind, false);\n\t\tif (arena == NULL || (dss_prec != dss_prec_limit &&\n\t\t    arena_dss_prec_set(arena, dss_prec))) {\n\t\t\tret = EFAULT;\n\t\t\tgoto label_return;\n\t\t}\n\t\tdss_prec_old = arena_dss_prec_get(arena);\n\t}\n\n\tdss = dss_prec_names[dss_prec_old];\n\tREAD(dss, const char *);\n\n\tret = 0;\nlabel_return:\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &ctl_mtx);\n\treturn ret;\n}\n\nstatic int\narena_i_decay_ms_ctl_impl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen, bool dirty) {\n\tint ret;\n\tunsigned arena_ind;\n\tarena_t *arena;\n\n\tMIB_UNSIGNED(arena_ind, 1);\n\tarena = arena_get(tsd_tsdn(tsd), arena_ind, false);\n\tif (arena == NULL) {\n\t\tret = EFAULT;\n\t\tgoto label_return;\n\t}\n\n\tif (oldp != NULL && oldlenp != NULL) {\n\t\tsize_t oldval = dirty ? arena_dirty_decay_ms_get(arena) :\n\t\t    arena_muzzy_decay_ms_get(arena);\n\t\tREAD(oldval, ssize_t);\n\t}\n\tif (newp != NULL) {\n\t\tif (newlen != sizeof(ssize_t)) {\n\t\t\tret = EINVAL;\n\t\t\tgoto label_return;\n\t\t}\n\t\tif (arena_is_huge(arena_ind) && *(ssize_t *)newp > 0) {\n\t\t\t/*\n\t\t\t * By default the huge arena purges eagerly.  If it is\n\t\t\t * set to non-zero decay time afterwards, background\n\t\t\t * thread might be needed.\n\t\t\t */\n\t\t\tif (background_thread_create(tsd, arena_ind)) {\n\t\t\t\tret = EFAULT;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\t\t}\n\t\tif (dirty ? arena_dirty_decay_ms_set(tsd_tsdn(tsd), arena,\n\t\t    *(ssize_t *)newp) : arena_muzzy_decay_ms_set(tsd_tsdn(tsd),\n\t\t    arena, *(ssize_t *)newp)) {\n\t\t\tret = EFAULT;\n\t\t\tgoto label_return;\n\t\t}\n\t}\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\narena_i_dirty_decay_ms_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\treturn arena_i_decay_ms_ctl_impl(tsd, mib, miblen, oldp, oldlenp, newp,\n\t    newlen, true);\n}\n\nstatic int\narena_i_muzzy_decay_ms_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\treturn arena_i_decay_ms_ctl_impl(tsd, mib, miblen, oldp, oldlenp, newp,\n\t    newlen, false);\n}\n\nstatic int\narena_i_extent_hooks_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tunsigned arena_ind;\n\tarena_t *arena;\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &ctl_mtx);\n\tMIB_UNSIGNED(arena_ind, 1);\n\tif (arena_ind < narenas_total_get()) {\n\t\textent_hooks_t *old_extent_hooks;\n\t\tarena = arena_get(tsd_tsdn(tsd), arena_ind, false);\n\t\tif (arena == NULL) {\n\t\t\tif (arena_ind >= narenas_auto) {\n\t\t\t\tret = EFAULT;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\t\t\told_extent_hooks =\n\t\t\t    (extent_hooks_t *)&extent_hooks_default;\n\t\t\tREAD(old_extent_hooks, extent_hooks_t *);\n\t\t\tif (newp != NULL) {\n\t\t\t\t/* Initialize a new arena as a side effect. */\n\t\t\t\textent_hooks_t *new_extent_hooks\n\t\t\t\t    JEMALLOC_CC_SILENCE_INIT(NULL);\n\t\t\t\tWRITE(new_extent_hooks, extent_hooks_t *);\n\t\t\t\tarena = arena_init(tsd_tsdn(tsd), arena_ind,\n\t\t\t\t    new_extent_hooks);\n\t\t\t\tif (arena == NULL) {\n\t\t\t\t\tret = EFAULT;\n\t\t\t\t\tgoto label_return;\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tif (newp != NULL) {\n\t\t\t\textent_hooks_t *new_extent_hooks\n\t\t\t\t    JEMALLOC_CC_SILENCE_INIT(NULL);\n\t\t\t\tWRITE(new_extent_hooks, extent_hooks_t *);\n\t\t\t\told_extent_hooks = extent_hooks_set(tsd, arena,\n\t\t\t\t    new_extent_hooks);\n\t\t\t\tREAD(old_extent_hooks, extent_hooks_t *);\n\t\t\t} else {\n\t\t\t\told_extent_hooks = extent_hooks_get(arena);\n\t\t\t\tREAD(old_extent_hooks, extent_hooks_t *);\n\t\t\t}\n\t\t}\n\t} else {\n\t\tret = EFAULT;\n\t\tgoto label_return;\n\t}\n\tret = 0;\nlabel_return:\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &ctl_mtx);\n\treturn ret;\n}\n\nstatic int\narena_i_retain_grow_limit_ctl(tsd_t *tsd, const size_t *mib,\n    size_t miblen, void *oldp, size_t *oldlenp, void *newp,\n    size_t newlen) {\n\tint ret;\n\tunsigned arena_ind;\n\tarena_t *arena;\n\n\tif (!opt_retain) {\n\t\t/* Only relevant when retain is enabled. */\n\t\treturn ENOENT;\n\t}\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &ctl_mtx);\n\tMIB_UNSIGNED(arena_ind, 1);\n\tif (arena_ind < narenas_total_get() && (arena =\n\t    arena_get(tsd_tsdn(tsd), arena_ind, false)) != NULL) {\n\t\tsize_t old_limit, new_limit;\n\t\tif (newp != NULL) {\n\t\t\tWRITE(new_limit, size_t);\n\t\t}\n\t\tbool err = arena_retain_grow_limit_get_set(tsd, arena,\n\t\t    &old_limit, newp != NULL ? &new_limit : NULL);\n\t\tif (!err) {\n\t\t\tREAD(old_limit, size_t);\n\t\t\tret = 0;\n\t\t} else {\n\t\t\tret = EFAULT;\n\t\t}\n\t} else {\n\t\tret = EFAULT;\n\t}\nlabel_return:\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &ctl_mtx);\n\treturn ret;\n}\n\nstatic const ctl_named_node_t *\narena_i_index(tsdn_t *tsdn, const size_t *mib, size_t miblen,\n    size_t i) {\n\tconst ctl_named_node_t *ret;\n\n\tmalloc_mutex_lock(tsdn, &ctl_mtx);\n\tswitch (i) {\n\tcase MALLCTL_ARENAS_ALL:\n\tcase MALLCTL_ARENAS_DESTROYED:\n\t\tbreak;\n\tdefault:\n\t\tif (i > ctl_arenas->narenas) {\n\t\t\tret = NULL;\n\t\t\tgoto label_return;\n\t\t}\n\t\tbreak;\n\t}\n\n\tret = super_arena_i_node;\nlabel_return:\n\tmalloc_mutex_unlock(tsdn, &ctl_mtx);\n\treturn ret;\n}\n\n/******************************************************************************/\n\nstatic int\narenas_narenas_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tunsigned narenas;\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &ctl_mtx);\n\tREADONLY();\n\tif (*oldlenp != sizeof(unsigned)) {\n\t\tret = EINVAL;\n\t\tgoto label_return;\n\t}\n\tnarenas = ctl_arenas->narenas;\n\tREAD(narenas, unsigned);\n\n\tret = 0;\nlabel_return:\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &ctl_mtx);\n\treturn ret;\n}\n\nstatic int\narenas_decay_ms_ctl_impl(tsd_t *tsd, const size_t *mib,\n    size_t miblen, void *oldp, size_t *oldlenp, void *newp,\n    size_t newlen, bool dirty) {\n\tint ret;\n\n\tif (oldp != NULL && oldlenp != NULL) {\n\t\tsize_t oldval = (dirty ? arena_dirty_decay_ms_default_get() :\n\t\t    arena_muzzy_decay_ms_default_get());\n\t\tREAD(oldval, ssize_t);\n\t}\n\tif (newp != NULL) {\n\t\tif (newlen != sizeof(ssize_t)) {\n\t\t\tret = EINVAL;\n\t\t\tgoto label_return;\n\t\t}\n\t\tif (dirty ? arena_dirty_decay_ms_default_set(*(ssize_t *)newp)\n\t\t    : arena_muzzy_decay_ms_default_set(*(ssize_t *)newp)) {\n\t\t\tret = EFAULT;\n\t\t\tgoto label_return;\n\t\t}\n\t}\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\narenas_dirty_decay_ms_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\treturn arenas_decay_ms_ctl_impl(tsd, mib, miblen, oldp, oldlenp, newp,\n\t    newlen, true);\n}\n\nstatic int\narenas_muzzy_decay_ms_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\treturn arenas_decay_ms_ctl_impl(tsd, mib, miblen, oldp, oldlenp, newp,\n\t    newlen, false);\n}\n\nCTL_RO_NL_GEN(arenas_quantum, QUANTUM, size_t)\nCTL_RO_NL_GEN(arenas_page, PAGE, size_t)\nCTL_RO_NL_GEN(arenas_tcache_max, tcache_maxclass, size_t)\nCTL_RO_NL_GEN(arenas_nbins, SC_NBINS, unsigned)\nCTL_RO_NL_GEN(arenas_nhbins, nhbins, unsigned)\nCTL_RO_NL_GEN(arenas_bin_i_size, bin_infos[mib[2]].reg_size, size_t)\nCTL_RO_NL_GEN(arenas_bin_i_nregs, bin_infos[mib[2]].nregs, uint32_t)\nCTL_RO_NL_GEN(arenas_bin_i_slab_size, bin_infos[mib[2]].slab_size, size_t)\nCTL_RO_NL_GEN(arenas_bin_i_nshards, bin_infos[mib[2]].n_shards, uint32_t)\nstatic const ctl_named_node_t *\narenas_bin_i_index(tsdn_t *tsdn, const size_t *mib,\n    size_t miblen, size_t i) {\n\tif (i > SC_NBINS) {\n\t\treturn NULL;\n\t}\n\treturn super_arenas_bin_i_node;\n}\n\nCTL_RO_NL_GEN(arenas_nlextents, SC_NSIZES - SC_NBINS, unsigned)\nCTL_RO_NL_GEN(arenas_lextent_i_size, sz_index2size(SC_NBINS+(szind_t)mib[2]),\n    size_t)\nstatic const ctl_named_node_t *\narenas_lextent_i_index(tsdn_t *tsdn, const size_t *mib,\n    size_t miblen, size_t i) {\n\tif (i > SC_NSIZES - SC_NBINS) {\n\t\treturn NULL;\n\t}\n\treturn super_arenas_lextent_i_node;\n}\n\nstatic int\narenas_create_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\textent_hooks_t *extent_hooks;\n\tunsigned arena_ind;\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &ctl_mtx);\n\n\textent_hooks = (extent_hooks_t *)&extent_hooks_default;\n\tWRITE(extent_hooks, extent_hooks_t *);\n\tif ((arena_ind = ctl_arena_init(tsd, extent_hooks)) == UINT_MAX) {\n\t\tret = EAGAIN;\n\t\tgoto label_return;\n\t}\n\tREAD(arena_ind, unsigned);\n\n\tret = 0;\nlabel_return:\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &ctl_mtx);\n\treturn ret;\n}\n\nstatic int\narenas_lookup_ctl(tsd_t *tsd, const size_t *mib,\n    size_t miblen, void *oldp, size_t *oldlenp, void *newp,\n    size_t newlen) {\n\tint ret;\n\tunsigned arena_ind;\n\tvoid *ptr;\n\textent_t *extent;\n\tarena_t *arena;\n\n\tptr = NULL;\n\tret = EINVAL;\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &ctl_mtx);\n\tWRITE(ptr, void *);\n\textent = iealloc(tsd_tsdn(tsd), ptr);\n\tif (extent == NULL)\n\t\tgoto label_return;\n\n\tarena = extent_arena_get(extent);\n\tif (arena == NULL)\n\t\tgoto label_return;\n\n\tarena_ind = arena_ind_get(arena);\n\tREAD(arena_ind, unsigned);\n\n\tret = 0;\nlabel_return:\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &ctl_mtx);\n\treturn ret;\n}\n\n/******************************************************************************/\n\nstatic int\nprof_thread_active_init_ctl(tsd_t *tsd, const size_t *mib,\n    size_t miblen, void *oldp, size_t *oldlenp, void *newp,\n    size_t newlen) {\n\tint ret;\n\tbool oldval;\n\n\tif (!config_prof) {\n\t\treturn ENOENT;\n\t}\n\n\tif (newp != NULL) {\n\t\tif (newlen != sizeof(bool)) {\n\t\t\tret = EINVAL;\n\t\t\tgoto label_return;\n\t\t}\n\t\toldval = prof_thread_active_init_set(tsd_tsdn(tsd),\n\t\t    *(bool *)newp);\n\t} else {\n\t\toldval = prof_thread_active_init_get(tsd_tsdn(tsd));\n\t}\n\tREAD(oldval, bool);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\nprof_active_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tbool oldval;\n\n\tif (!config_prof) {\n\t\treturn ENOENT;\n\t}\n\n\tif (newp != NULL) {\n\t\tif (newlen != sizeof(bool)) {\n\t\t\tret = EINVAL;\n\t\t\tgoto label_return;\n\t\t}\n\t\toldval = prof_active_set(tsd_tsdn(tsd), *(bool *)newp);\n\t} else {\n\t\toldval = prof_active_get(tsd_tsdn(tsd));\n\t}\n\tREAD(oldval, bool);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\nprof_dump_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tconst char *filename = NULL;\n\n\tif (!config_prof) {\n\t\treturn ENOENT;\n\t}\n\n\tWRITEONLY();\n\tWRITE(filename, const char *);\n\n\tif (prof_mdump(tsd, filename)) {\n\t\tret = EFAULT;\n\t\tgoto label_return;\n\t}\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\nprof_gdump_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tbool oldval;\n\n\tif (!config_prof) {\n\t\treturn ENOENT;\n\t}\n\n\tif (newp != NULL) {\n\t\tif (newlen != sizeof(bool)) {\n\t\t\tret = EINVAL;\n\t\t\tgoto label_return;\n\t\t}\n\t\toldval = prof_gdump_set(tsd_tsdn(tsd), *(bool *)newp);\n\t} else {\n\t\toldval = prof_gdump_get(tsd_tsdn(tsd));\n\t}\n\tREAD(oldval, bool);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\nprof_reset_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tsize_t lg_sample = lg_prof_sample;\n\n\tif (!config_prof) {\n\t\treturn ENOENT;\n\t}\n\n\tWRITEONLY();\n\tWRITE(lg_sample, size_t);\n\tif (lg_sample >= (sizeof(uint64_t) << 3)) {\n\t\tlg_sample = (sizeof(uint64_t) << 3) - 1;\n\t}\n\n\tprof_reset(tsd, lg_sample);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nCTL_RO_NL_CGEN(config_prof, prof_interval, prof_interval, uint64_t)\nCTL_RO_NL_CGEN(config_prof, lg_prof_sample, lg_prof_sample, size_t)\n\nstatic int\nprof_log_start_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\n\tconst char *filename = NULL;\n\n\tif (!config_prof) {\n\t\treturn ENOENT;\n\t}\n\n\tWRITEONLY();\n\tWRITE(filename, const char *);\n\n\tif (prof_log_start(tsd_tsdn(tsd), filename)) {\n\t\tret = EFAULT;\n\t\tgoto label_return;\n\t}\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\nprof_log_stop_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tif (!config_prof) {\n\t\treturn ENOENT;\n\t}\n\n\tif (prof_log_stop(tsd_tsdn(tsd))) {\n\t\treturn EFAULT;\n\t}\n\n\treturn 0;\n}\n\n/******************************************************************************/\n\nCTL_RO_CGEN(config_stats, stats_allocated, ctl_stats->allocated, size_t)\nCTL_RO_CGEN(config_stats, stats_active, ctl_stats->active, size_t)\nCTL_RO_CGEN(config_stats, stats_metadata, ctl_stats->metadata, size_t)\nCTL_RO_CGEN(config_stats, stats_metadata_thp, ctl_stats->metadata_thp, size_t)\nCTL_RO_CGEN(config_stats, stats_resident, ctl_stats->resident, size_t)\nCTL_RO_CGEN(config_stats, stats_mapped, ctl_stats->mapped, size_t)\nCTL_RO_CGEN(config_stats, stats_retained, ctl_stats->retained, size_t)\n\nCTL_RO_CGEN(config_stats, stats_background_thread_num_threads,\n    ctl_stats->background_thread.num_threads, size_t)\nCTL_RO_CGEN(config_stats, stats_background_thread_num_runs,\n    ctl_stats->background_thread.num_runs, uint64_t)\nCTL_RO_CGEN(config_stats, stats_background_thread_run_interval,\n    nstime_ns(&ctl_stats->background_thread.run_interval), uint64_t)\n\nCTL_RO_GEN(stats_arenas_i_dss, arenas_i(mib[2])->dss, const char *)\nCTL_RO_GEN(stats_arenas_i_dirty_decay_ms, arenas_i(mib[2])->dirty_decay_ms,\n    ssize_t)\nCTL_RO_GEN(stats_arenas_i_muzzy_decay_ms, arenas_i(mib[2])->muzzy_decay_ms,\n    ssize_t)\nCTL_RO_GEN(stats_arenas_i_nthreads, arenas_i(mib[2])->nthreads, unsigned)\nCTL_RO_GEN(stats_arenas_i_uptime,\n    nstime_ns(&arenas_i(mib[2])->astats->astats.uptime), uint64_t)\nCTL_RO_GEN(stats_arenas_i_pactive, arenas_i(mib[2])->pactive, size_t)\nCTL_RO_GEN(stats_arenas_i_pdirty, arenas_i(mib[2])->pdirty, size_t)\nCTL_RO_GEN(stats_arenas_i_pmuzzy, arenas_i(mib[2])->pmuzzy, size_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_mapped,\n    atomic_load_zu(&arenas_i(mib[2])->astats->astats.mapped, ATOMIC_RELAXED),\n    size_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_retained,\n    atomic_load_zu(&arenas_i(mib[2])->astats->astats.retained, ATOMIC_RELAXED),\n    size_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_extent_avail,\n    atomic_load_zu(&arenas_i(mib[2])->astats->astats.extent_avail,\n        ATOMIC_RELAXED),\n    size_t)\n\nCTL_RO_CGEN(config_stats, stats_arenas_i_dirty_npurge,\n    ctl_arena_stats_read_u64(\n    &arenas_i(mib[2])->astats->astats.decay_dirty.npurge), uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_dirty_nmadvise,\n    ctl_arena_stats_read_u64(\n    &arenas_i(mib[2])->astats->astats.decay_dirty.nmadvise), uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_dirty_purged,\n    ctl_arena_stats_read_u64(\n    &arenas_i(mib[2])->astats->astats.decay_dirty.purged), uint64_t)\n\nCTL_RO_CGEN(config_stats, stats_arenas_i_muzzy_npurge,\n    ctl_arena_stats_read_u64(\n    &arenas_i(mib[2])->astats->astats.decay_muzzy.npurge), uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_muzzy_nmadvise,\n    ctl_arena_stats_read_u64(\n    &arenas_i(mib[2])->astats->astats.decay_muzzy.nmadvise), uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_muzzy_purged,\n    ctl_arena_stats_read_u64(\n    &arenas_i(mib[2])->astats->astats.decay_muzzy.purged), uint64_t)\n\nCTL_RO_CGEN(config_stats, stats_arenas_i_base,\n    atomic_load_zu(&arenas_i(mib[2])->astats->astats.base, ATOMIC_RELAXED),\n    size_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_internal,\n    atomic_load_zu(&arenas_i(mib[2])->astats->astats.internal, ATOMIC_RELAXED),\n    size_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_metadata_thp,\n    atomic_load_zu(&arenas_i(mib[2])->astats->astats.metadata_thp,\n    ATOMIC_RELAXED), size_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_tcache_bytes,\n    atomic_load_zu(&arenas_i(mib[2])->astats->astats.tcache_bytes,\n    ATOMIC_RELAXED), size_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_resident,\n    atomic_load_zu(&arenas_i(mib[2])->astats->astats.resident, ATOMIC_RELAXED),\n    size_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_abandoned_vm,\n    atomic_load_zu(&arenas_i(mib[2])->astats->astats.abandoned_vm,\n    ATOMIC_RELAXED), size_t)\n\nCTL_RO_CGEN(config_stats, stats_arenas_i_small_allocated,\n    arenas_i(mib[2])->astats->allocated_small, size_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_small_nmalloc,\n    arenas_i(mib[2])->astats->nmalloc_small, uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_small_ndalloc,\n    arenas_i(mib[2])->astats->ndalloc_small, uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_small_nrequests,\n    arenas_i(mib[2])->astats->nrequests_small, uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_small_nfills,\n    arenas_i(mib[2])->astats->nfills_small, uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_small_nflushes,\n    arenas_i(mib[2])->astats->nflushes_small, uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_large_allocated,\n    atomic_load_zu(&arenas_i(mib[2])->astats->astats.allocated_large,\n    ATOMIC_RELAXED), size_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_large_nmalloc,\n    ctl_arena_stats_read_u64(\n    &arenas_i(mib[2])->astats->astats.nmalloc_large), uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_large_ndalloc,\n    ctl_arena_stats_read_u64(\n    &arenas_i(mib[2])->astats->astats.ndalloc_large), uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_large_nrequests,\n    ctl_arena_stats_read_u64(\n    &arenas_i(mib[2])->astats->astats.nrequests_large), uint64_t)\n/*\n * Note: \"nmalloc_large\" here instead of \"nfills\" in the read.  This is\n * intentional (large has no batch fill).\n */\nCTL_RO_CGEN(config_stats, stats_arenas_i_large_nfills,\n    ctl_arena_stats_read_u64(\n    &arenas_i(mib[2])->astats->astats.nmalloc_large), uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_large_nflushes,\n    ctl_arena_stats_read_u64(\n    &arenas_i(mib[2])->astats->astats.nflushes_large), uint64_t)\n\n/* Lock profiling related APIs below. */\n#define RO_MUTEX_CTL_GEN(n, l)\t\t\t\t\t\t\\\nCTL_RO_CGEN(config_stats, stats_##n##_num_ops,\t\t\t\t\\\n    l.n_lock_ops, uint64_t)\t\t\t\t\t\t\\\nCTL_RO_CGEN(config_stats, stats_##n##_num_wait,\t\t\t\t\\\n    l.n_wait_times, uint64_t)\t\t\t\t\t\t\\\nCTL_RO_CGEN(config_stats, stats_##n##_num_spin_acq,\t\t\t\\\n    l.n_spin_acquired, uint64_t)\t\t\t\t\t\\\nCTL_RO_CGEN(config_stats, stats_##n##_num_owner_switch,\t\t\t\\\n    l.n_owner_switches, uint64_t) \t\t\t\t\t\\\nCTL_RO_CGEN(config_stats, stats_##n##_total_wait_time,\t\t\t\\\n    nstime_ns(&l.tot_wait_time), uint64_t)\t\t\t\t\\\nCTL_RO_CGEN(config_stats, stats_##n##_max_wait_time,\t\t\t\\\n    nstime_ns(&l.max_wait_time), uint64_t)\t\t\t\t\\\nCTL_RO_CGEN(config_stats, stats_##n##_max_num_thds,\t\t\t\\\n    l.max_n_thds, uint32_t)\n\n/* Global mutexes. */\n#define OP(mtx)\t\t\t\t\t\t\t\t\\\n    RO_MUTEX_CTL_GEN(mutexes_##mtx,\t\t\t\t\t\\\n        ctl_stats->mutex_prof_data[global_prof_mutex_##mtx])\nMUTEX_PROF_GLOBAL_MUTEXES\n#undef OP\n\n/* Per arena mutexes */\n#define OP(mtx) RO_MUTEX_CTL_GEN(arenas_i_mutexes_##mtx,\t\t\\\n    arenas_i(mib[2])->astats->astats.mutex_prof_data[arena_prof_mutex_##mtx])\nMUTEX_PROF_ARENA_MUTEXES\n#undef OP\n\n/* tcache bin mutex */\nRO_MUTEX_CTL_GEN(arenas_i_bins_j_mutex,\n    arenas_i(mib[2])->astats->bstats[mib[4]].mutex_data)\n#undef RO_MUTEX_CTL_GEN\n\n/* Resets all mutex stats, including global, arena and bin mutexes. */\nstatic int\nstats_mutexes_reset_ctl(tsd_t *tsd, const size_t *mib,\n    size_t miblen, void *oldp, size_t *oldlenp,\n    void *newp, size_t newlen) {\n\tif (!config_stats) {\n\t\treturn ENOENT;\n\t}\n\n\ttsdn_t *tsdn = tsd_tsdn(tsd);\n\n#define MUTEX_PROF_RESET(mtx)\t\t\t\t\t\t\\\n    malloc_mutex_lock(tsdn, &mtx);\t\t\t\t\t\\\n    malloc_mutex_prof_data_reset(tsdn, &mtx);\t\t\t\t\\\n    malloc_mutex_unlock(tsdn, &mtx);\n\n\t/* Global mutexes: ctl and prof. */\n\tMUTEX_PROF_RESET(ctl_mtx);\n\tif (have_background_thread) {\n\t\tMUTEX_PROF_RESET(background_thread_lock);\n\t}\n\tif (config_prof && opt_prof) {\n\t\tMUTEX_PROF_RESET(bt2gctx_mtx);\n\t}\n\n\n\t/* Per arena mutexes. */\n\tunsigned n = narenas_total_get();\n\n\tfor (unsigned i = 0; i < n; i++) {\n\t\tarena_t *arena = arena_get(tsdn, i, false);\n\t\tif (!arena) {\n\t\t\tcontinue;\n\t\t}\n\t\tMUTEX_PROF_RESET(arena->large_mtx);\n\t\tMUTEX_PROF_RESET(arena->extent_avail_mtx);\n\t\tMUTEX_PROF_RESET(arena->extents_dirty.mtx);\n\t\tMUTEX_PROF_RESET(arena->extents_muzzy.mtx);\n\t\tMUTEX_PROF_RESET(arena->extents_retained.mtx);\n\t\tMUTEX_PROF_RESET(arena->decay_dirty.mtx);\n\t\tMUTEX_PROF_RESET(arena->decay_muzzy.mtx);\n\t\tMUTEX_PROF_RESET(arena->tcache_ql_mtx);\n\t\tMUTEX_PROF_RESET(arena->base->mtx);\n\n\t\tfor (szind_t i = 0; i < SC_NBINS; i++) {\n\t\t\tfor (unsigned j = 0; j < bin_infos[i].n_shards; j++) {\n\t\t\t\tbin_t *bin = &arena->bins[i].bin_shards[j];\n\t\t\t\tMUTEX_PROF_RESET(bin->lock);\n\t\t\t}\n\t\t}\n\t}\n#undef MUTEX_PROF_RESET\n\treturn 0;\n}\n\nCTL_RO_CGEN(config_stats, stats_arenas_i_bins_j_nmalloc,\n    arenas_i(mib[2])->astats->bstats[mib[4]].nmalloc, uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_bins_j_ndalloc,\n    arenas_i(mib[2])->astats->bstats[mib[4]].ndalloc, uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_bins_j_nrequests,\n    arenas_i(mib[2])->astats->bstats[mib[4]].nrequests, uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_bins_j_curregs,\n    arenas_i(mib[2])->astats->bstats[mib[4]].curregs, size_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_bins_j_nfills,\n    arenas_i(mib[2])->astats->bstats[mib[4]].nfills, uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_bins_j_nflushes,\n    arenas_i(mib[2])->astats->bstats[mib[4]].nflushes, uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_bins_j_nslabs,\n    arenas_i(mib[2])->astats->bstats[mib[4]].nslabs, uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_bins_j_nreslabs,\n    arenas_i(mib[2])->astats->bstats[mib[4]].reslabs, uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_bins_j_curslabs,\n    arenas_i(mib[2])->astats->bstats[mib[4]].curslabs, size_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_bins_j_nonfull_slabs,\n    arenas_i(mib[2])->astats->bstats[mib[4]].nonfull_slabs, size_t)\n\nstatic const ctl_named_node_t *\nstats_arenas_i_bins_j_index(tsdn_t *tsdn, const size_t *mib,\n    size_t miblen, size_t j) {\n\tif (j > SC_NBINS) {\n\t\treturn NULL;\n\t}\n\treturn super_stats_arenas_i_bins_j_node;\n}\n\nCTL_RO_CGEN(config_stats, stats_arenas_i_lextents_j_nmalloc,\n    ctl_arena_stats_read_u64(\n    &arenas_i(mib[2])->astats->lstats[mib[4]].nmalloc), uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_lextents_j_ndalloc,\n    ctl_arena_stats_read_u64(\n    &arenas_i(mib[2])->astats->lstats[mib[4]].ndalloc), uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_lextents_j_nrequests,\n    ctl_arena_stats_read_u64(\n    &arenas_i(mib[2])->astats->lstats[mib[4]].nrequests), uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_lextents_j_curlextents,\n    arenas_i(mib[2])->astats->lstats[mib[4]].curlextents, size_t)\n\nstatic const ctl_named_node_t *\nstats_arenas_i_lextents_j_index(tsdn_t *tsdn, const size_t *mib,\n    size_t miblen, size_t j) {\n\tif (j > SC_NSIZES - SC_NBINS) {\n\t\treturn NULL;\n\t}\n\treturn super_stats_arenas_i_lextents_j_node;\n}\n\nCTL_RO_CGEN(config_stats, stats_arenas_i_extents_j_ndirty,\n    atomic_load_zu(\n        &arenas_i(mib[2])->astats->estats[mib[4]].ndirty,\n\tATOMIC_RELAXED), size_t);\nCTL_RO_CGEN(config_stats, stats_arenas_i_extents_j_nmuzzy,\n    atomic_load_zu(\n        &arenas_i(mib[2])->astats->estats[mib[4]].nmuzzy,\n\tATOMIC_RELAXED), size_t);\nCTL_RO_CGEN(config_stats, stats_arenas_i_extents_j_nretained,\n    atomic_load_zu(\n        &arenas_i(mib[2])->astats->estats[mib[4]].nretained,\n\tATOMIC_RELAXED), size_t);\nCTL_RO_CGEN(config_stats, stats_arenas_i_extents_j_dirty_bytes,\n    atomic_load_zu(\n        &arenas_i(mib[2])->astats->estats[mib[4]].dirty_bytes,\n\tATOMIC_RELAXED), size_t);\nCTL_RO_CGEN(config_stats, stats_arenas_i_extents_j_muzzy_bytes,\n    atomic_load_zu(\n        &arenas_i(mib[2])->astats->estats[mib[4]].muzzy_bytes,\n\tATOMIC_RELAXED), size_t);\nCTL_RO_CGEN(config_stats, stats_arenas_i_extents_j_retained_bytes,\n    atomic_load_zu(\n        &arenas_i(mib[2])->astats->estats[mib[4]].retained_bytes,\n\tATOMIC_RELAXED), size_t);\n\nstatic const ctl_named_node_t *\nstats_arenas_i_extents_j_index(tsdn_t *tsdn, const size_t *mib,\n    size_t miblen, size_t j) {\n\tif (j >= SC_NPSIZES) {\n\t\treturn NULL;\n\t}\n\treturn super_stats_arenas_i_extents_j_node;\n}\n\nstatic bool\nctl_arenas_i_verify(size_t i) {\n\tsize_t a = arenas_i2a_impl(i, true, true);\n\tif (a == UINT_MAX || !ctl_arenas->arenas[a]->initialized) {\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\nstatic const ctl_named_node_t *\nstats_arenas_i_index(tsdn_t *tsdn, const size_t *mib,\n    size_t miblen, size_t i) {\n\tconst ctl_named_node_t *ret;\n\n\tmalloc_mutex_lock(tsdn, &ctl_mtx);\n\tif (ctl_arenas_i_verify(i)) {\n\t\tret = NULL;\n\t\tgoto label_return;\n\t}\n\n\tret = super_stats_arenas_i_node;\nlabel_return:\n\tmalloc_mutex_unlock(tsdn, &ctl_mtx);\n\treturn ret;\n}\n\nstatic int\nexperimental_hooks_install_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tif (oldp == NULL || oldlenp == NULL|| newp == NULL) {\n\t\tret = EINVAL;\n\t\tgoto label_return;\n\t}\n\t/*\n\t * Note: this is a *private* struct.  This is an experimental interface;\n\t * forcing the user to know the jemalloc internals well enough to\n\t * extract the ABI hopefully ensures nobody gets too comfortable with\n\t * this API, which can change at a moment's notice.\n\t */\n\thooks_t hooks;\n\tWRITE(hooks, hooks_t);\n\tvoid *handle = hook_install(tsd_tsdn(tsd), &hooks);\n\tif (handle == NULL) {\n\t\tret = EAGAIN;\n\t\tgoto label_return;\n\t}\n\tREAD(handle, void *);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\nexperimental_hooks_remove_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tWRITEONLY();\n\tvoid *handle = NULL;\n\tWRITE(handle, void *);\n\tif (handle == NULL) {\n\t\tret = EINVAL;\n\t\tgoto label_return;\n\t}\n\thook_remove(tsd_tsdn(tsd), handle);\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\n/*\n * Output six memory utilization entries for an input pointer, the first one of\n * type (void *) and the remaining five of type size_t, describing the following\n * (in the same order):\n *\n * (a) memory address of the extent a potential reallocation would go into,\n * == the five fields below describe about the extent the pointer resides in ==\n * (b) number of free regions in the extent,\n * (c) number of regions in the extent,\n * (d) size of the extent in terms of bytes,\n * (e) total number of free regions in the bin the extent belongs to, and\n * (f) total number of regions in the bin the extent belongs to.\n *\n * Note that \"(e)\" and \"(f)\" are only available when stats are enabled;\n * otherwise their values are undefined.\n *\n * This API is mainly intended for small class allocations, where extents are\n * used as slab.\n *\n * In case of large class allocations, \"(a)\" will be NULL, and \"(e)\" and \"(f)\"\n * will be zero (if stats are enabled; otherwise undefined).  The other three\n * fields will be properly set though the values are trivial: \"(b)\" will be 0,\n * \"(c)\" will be 1, and \"(d)\" will be the usable size.\n *\n * The input pointer and size are respectively passed in by newp and newlen,\n * and the output fields and size are respectively oldp and *oldlenp.\n *\n * It can be beneficial to define the following macros to make it easier to\n * access the output:\n *\n * #define SLABCUR_READ(out) (*(void **)out)\n * #define COUNTS(out) ((size_t *)((void **)out + 1))\n * #define NFREE_READ(out) COUNTS(out)[0]\n * #define NREGS_READ(out) COUNTS(out)[1]\n * #define SIZE_READ(out) COUNTS(out)[2]\n * #define BIN_NFREE_READ(out) COUNTS(out)[3]\n * #define BIN_NREGS_READ(out) COUNTS(out)[4]\n *\n * and then write e.g. NFREE_READ(oldp) to fetch the output.  See the unit test\n * test_query in test/unit/extent_util.c for an example.\n *\n * For a typical defragmentation workflow making use of this API for\n * understanding the fragmentation level, please refer to the comment for\n * experimental_utilization_batch_query_ctl.\n *\n * It's up to the application how to determine the significance of\n * fragmentation relying on the outputs returned.  Possible choices are:\n *\n * (a) if extent utilization ratio is below certain threshold,\n * (b) if extent memory consumption is above certain threshold,\n * (c) if extent utilization ratio is significantly below bin utilization ratio,\n * (d) if input pointer deviates a lot from potential reallocation address, or\n * (e) some selection/combination of the above.\n *\n * The caller needs to make sure that the input/output arguments are valid,\n * in particular, that the size of the output is correct, i.e.:\n *\n *     *oldlenp = sizeof(void *) + sizeof(size_t) * 5\n *\n * Otherwise, the function immediately returns EINVAL without touching anything.\n *\n * In the rare case where there's no associated extent found for the input\n * pointer, the function zeros out all output fields and return.  Please refer\n * to the comment for experimental_utilization_batch_query_ctl to understand the\n * motivation from C++.\n */\nstatic int\nexperimental_utilization_query_ctl(tsd_t *tsd, const size_t *mib,\n    size_t miblen, void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\n\tassert(sizeof(extent_util_stats_verbose_t)\n\t    == sizeof(void *) + sizeof(size_t) * 5);\n\n\tif (oldp == NULL || oldlenp == NULL\n\t    || *oldlenp != sizeof(extent_util_stats_verbose_t)\n\t    || newp == NULL) {\n\t\tret = EINVAL;\n\t\tgoto label_return;\n\t}\n\n\tvoid *ptr = NULL;\n\tWRITE(ptr, void *);\n\textent_util_stats_verbose_t *util_stats\n\t    = (extent_util_stats_verbose_t *)oldp;\n\textent_util_stats_verbose_get(tsd_tsdn(tsd), ptr,\n\t    &util_stats->nfree, &util_stats->nregs, &util_stats->size,\n\t    &util_stats->bin_nfree, &util_stats->bin_nregs,\n\t    &util_stats->slabcur_addr);\n\tret = 0;\n\nlabel_return:\n\treturn ret;\n}\n\n/*\n * Given an input array of pointers, output three memory utilization entries of\n * type size_t for each input pointer about the extent it resides in:\n *\n * (a) number of free regions in the extent,\n * (b) number of regions in the extent, and\n * (c) size of the extent in terms of bytes.\n *\n * This API is mainly intended for small class allocations, where extents are\n * used as slab.  In case of large class allocations, the outputs are trivial:\n * \"(a)\" will be 0, \"(b)\" will be 1, and \"(c)\" will be the usable size.\n *\n * Note that multiple input pointers may reside on a same extent so the output\n * fields may contain duplicates.\n *\n * The format of the input/output looks like:\n *\n * input[0]:  1st_pointer_to_query\t|  output[0]: 1st_extent_n_free_regions\n *\t\t\t\t\t|  output[1]: 1st_extent_n_regions\n *\t\t\t\t\t|  output[2]: 1st_extent_size\n * input[1]:  2nd_pointer_to_query\t|  output[3]: 2nd_extent_n_free_regions\n *\t\t\t\t\t|  output[4]: 2nd_extent_n_regions\n *\t\t\t\t\t|  output[5]: 2nd_extent_size\n * ...\t\t\t\t\t|  ...\n *\n * The input array and size are respectively passed in by newp and newlen, and\n * the output array and size are respectively oldp and *oldlenp.\n *\n * It can be beneficial to define the following macros to make it easier to\n * access the output:\n *\n * #define NFREE_READ(out, i) out[(i) * 3]\n * #define NREGS_READ(out, i) out[(i) * 3 + 1]\n * #define SIZE_READ(out, i) out[(i) * 3 + 2]\n *\n * and then write e.g. NFREE_READ(oldp, i) to fetch the output.  See the unit\n * test test_batch in test/unit/extent_util.c for a concrete example.\n *\n * A typical workflow would be composed of the following steps:\n *\n * (1) flush tcache: mallctl(\"thread.tcache.flush\", ...)\n * (2) initialize input array of pointers to query fragmentation\n * (3) allocate output array to hold utilization statistics\n * (4) query utilization: mallctl(\"experimental.utilization.batch_query\", ...)\n * (5) (optional) decide if it's worthwhile to defragment; otherwise stop here\n * (6) disable tcache: mallctl(\"thread.tcache.enabled\", ...)\n * (7) defragment allocations with significant fragmentation, e.g.:\n *         for each allocation {\n *             if it's fragmented {\n *                 malloc(...);\n *                 memcpy(...);\n *                 free(...);\n *             }\n *         }\n * (8) enable tcache: mallctl(\"thread.tcache.enabled\", ...)\n *\n * The application can determine the significance of fragmentation themselves\n * relying on the statistics returned, both at the overall level i.e. step \"(5)\"\n * and at individual allocation level i.e. within step \"(7)\".  Possible choices\n * are:\n *\n * (a) whether memory utilization ratio is below certain threshold,\n * (b) whether memory consumption is above certain threshold, or\n * (c) some combination of the two.\n *\n * The caller needs to make sure that the input/output arrays are valid and\n * their sizes are proper as well as matched, meaning:\n *\n * (a) newlen = n_pointers * sizeof(const void *)\n * (b) *oldlenp = n_pointers * sizeof(size_t) * 3\n * (c) n_pointers > 0\n *\n * Otherwise, the function immediately returns EINVAL without touching anything.\n *\n * In the rare case where there's no associated extent found for some pointers,\n * rather than immediately terminating the computation and raising an error,\n * the function simply zeros out the corresponding output fields and continues\n * the computation until all input pointers are handled.  The motivations of\n * such a design are as follows:\n *\n * (a) The function always either processes nothing or processes everything, and\n * never leaves the output half touched and half untouched.\n *\n * (b) It facilitates usage needs especially common in C++.  A vast variety of\n * C++ objects are instantiated with multiple dynamic memory allocations.  For\n * example, std::string and std::vector typically use at least two allocations,\n * one for the metadata and one for the actual content.  Other types may use\n * even more allocations.  When inquiring about utilization statistics, the\n * caller often wants to examine into all such allocations, especially internal\n * one(s), rather than just the topmost one.  The issue comes when some\n * implementations do certain optimizations to reduce/aggregate some internal\n * allocations, e.g. putting short strings directly into the metadata, and such\n * decisions are not known to the caller.  Therefore, we permit pointers to\n * memory usages that may not be returned by previous malloc calls, and we\n * provide the caller a convenient way to identify such cases.\n */\nstatic int\nexperimental_utilization_batch_query_ctl(tsd_t *tsd, const size_t *mib,\n    size_t miblen, void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\n\tassert(sizeof(extent_util_stats_t) == sizeof(size_t) * 3);\n\n\tconst size_t len = newlen / sizeof(const void *);\n\tif (oldp == NULL || oldlenp == NULL || newp == NULL || newlen == 0\n\t    || newlen != len * sizeof(const void *)\n\t    || *oldlenp != len * sizeof(extent_util_stats_t)) {\n\t\tret = EINVAL;\n\t\tgoto label_return;\n\t}\n\n\tvoid **ptrs = (void **)newp;\n\textent_util_stats_t *util_stats = (extent_util_stats_t *)oldp;\n\tsize_t i;\n\tfor (i = 0; i < len; ++i) {\n\t\textent_util_stats_get(tsd_tsdn(tsd), ptrs[i],\n\t\t    &util_stats[i].nfree, &util_stats[i].nregs,\n\t\t    &util_stats[i].size);\n\t}\n\tret = 0;\n\nlabel_return:\n\treturn ret;\n}\n\nstatic const ctl_named_node_t *\nexperimental_arenas_i_index(tsdn_t *tsdn, const size_t *mib,\n    size_t miblen, size_t i) {\n\tconst ctl_named_node_t *ret;\n\n\tmalloc_mutex_lock(tsdn, &ctl_mtx);\n\tif (ctl_arenas_i_verify(i)) {\n\t\tret = NULL;\n\t\tgoto label_return;\n\t}\n\tret = super_experimental_arenas_i_node;\nlabel_return:\n\tmalloc_mutex_unlock(tsdn, &ctl_mtx);\n\treturn ret;\n}\n\nstatic int\nexperimental_arenas_i_pactivep_ctl(tsd_t *tsd, const size_t *mib,\n    size_t miblen, void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\tif (!config_stats) {\n\t\treturn ENOENT;\n\t}\n\tif (oldp == NULL || oldlenp == NULL || *oldlenp != sizeof(size_t *)) {\n\t\treturn EINVAL;\n\t}\n\n\tunsigned arena_ind;\n\tarena_t *arena;\n\tint ret;\n\tsize_t *pactivep;\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &ctl_mtx);\n\tREADONLY();\n\tMIB_UNSIGNED(arena_ind, 2);\n\tif (arena_ind < narenas_total_get() && (arena =\n\t    arena_get(tsd_tsdn(tsd), arena_ind, false)) != NULL) {\n#if defined(JEMALLOC_GCC_ATOMIC_ATOMICS) ||\t\t\t\t\\\n    defined(JEMALLOC_GCC_SYNC_ATOMICS) || defined(_MSC_VER)\n\t\t/* Expose the underlying counter for fast read. */\n\t\tpactivep = (size_t *)&(arena->nactive.repr);\n\t\tREAD(pactivep, size_t *);\n\t\tret = 0;\n#else\n\t\tret = EFAULT;\n#endif\n\t} else {\n\t\tret = EFAULT;\n\t}\nlabel_return:\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &ctl_mtx);\n\treturn ret;\n}\n"
  },
  {
    "path": "deps/jemalloc/src/div.c",
    "content": "#include \"jemalloc/internal/jemalloc_preamble.h\"\n\n#include \"jemalloc/internal/div.h\"\n\n#include \"jemalloc/internal/assert.h\"\n\n/*\n * Suppose we have n = q * d, all integers. We know n and d, and want q = n / d.\n *\n * For any k, we have (here, all division is exact; not C-style rounding):\n * floor(ceil(2^k / d) * n / 2^k) = floor((2^k + r) / d * n / 2^k), where\n * r = (-2^k) mod d.\n *\n * Expanding this out:\n * ... = floor(2^k / d * n / 2^k + r / d * n / 2^k)\n *     = floor(n / d + (r / d) * (n / 2^k)).\n *\n * The fractional part of n / d is 0 (because of the assumption that d divides n\n * exactly), so we have:\n * ... = n / d + floor((r / d) * (n / 2^k))\n *\n * So that our initial expression is equal to the quantity we seek, so long as\n * (r / d) * (n / 2^k) < 1.\n *\n * r is a remainder mod d, so r < d and r / d < 1 always. We can make\n * n / 2 ^ k < 1 by setting k = 32. This gets us a value of magic that works.\n */\n\nvoid\ndiv_init(div_info_t *div_info, size_t d) {\n\t/* Nonsensical. */\n\tassert(d != 0);\n\t/*\n\t * This would make the value of magic too high to fit into a uint32_t\n\t * (we would want magic = 2^32 exactly). This would mess with code gen\n\t * on 32-bit machines.\n\t */\n\tassert(d != 1);\n\n\tuint64_t two_to_k = ((uint64_t)1 << 32);\n\tuint32_t magic = (uint32_t)(two_to_k / d);\n\n\t/*\n\t * We want magic = ceil(2^k / d), but C gives us floor. We have to\n\t * increment it unless the result was exact (i.e. unless d is a power of\n\t * two).\n\t */\n\tif (two_to_k % d != 0) {\n\t\tmagic++;\n\t}\n\tdiv_info->magic = magic;\n#ifdef JEMALLOC_DEBUG\n\tdiv_info->d = d;\n#endif\n}\n"
  },
  {
    "path": "deps/jemalloc/src/extent.c",
    "content": "#define JEMALLOC_EXTENT_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/extent_dss.h\"\n#include \"jemalloc/internal/extent_mmap.h\"\n#include \"jemalloc/internal/ph.h\"\n#include \"jemalloc/internal/rtree.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/mutex_pool.h\"\n\n/******************************************************************************/\n/* Data. */\n\nrtree_t\t\textents_rtree;\n/* Keyed by the address of the extent_t being protected. */\nmutex_pool_t\textent_mutex_pool;\n\nsize_t opt_lg_extent_max_active_fit = LG_EXTENT_MAX_ACTIVE_FIT_DEFAULT;\n\nstatic const bitmap_info_t extents_bitmap_info =\n    BITMAP_INFO_INITIALIZER(SC_NPSIZES+1);\n\nstatic void *extent_alloc_default(extent_hooks_t *extent_hooks, void *new_addr,\n    size_t size, size_t alignment, bool *zero, bool *commit,\n    unsigned arena_ind);\nstatic bool extent_dalloc_default(extent_hooks_t *extent_hooks, void *addr,\n    size_t size, bool committed, unsigned arena_ind);\nstatic void extent_destroy_default(extent_hooks_t *extent_hooks, void *addr,\n    size_t size, bool committed, unsigned arena_ind);\nstatic bool extent_commit_default(extent_hooks_t *extent_hooks, void *addr,\n    size_t size, size_t offset, size_t length, unsigned arena_ind);\nstatic bool extent_commit_impl(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length, bool growing_retained);\nstatic bool extent_decommit_default(extent_hooks_t *extent_hooks,\n    void *addr, size_t size, size_t offset, size_t length, unsigned arena_ind);\n#ifdef PAGES_CAN_PURGE_LAZY\nstatic bool extent_purge_lazy_default(extent_hooks_t *extent_hooks, void *addr,\n    size_t size, size_t offset, size_t length, unsigned arena_ind);\n#endif\nstatic bool extent_purge_lazy_impl(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length, bool growing_retained);\n#ifdef PAGES_CAN_PURGE_FORCED\nstatic bool extent_purge_forced_default(extent_hooks_t *extent_hooks,\n    void *addr, size_t size, size_t offset, size_t length, unsigned arena_ind);\n#endif\nstatic bool extent_purge_forced_impl(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length, bool growing_retained);\nstatic bool extent_split_default(extent_hooks_t *extent_hooks, void *addr,\n    size_t size, size_t size_a, size_t size_b, bool committed,\n    unsigned arena_ind);\nstatic extent_t *extent_split_impl(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t size_a,\n    szind_t szind_a, bool slab_a, size_t size_b, szind_t szind_b, bool slab_b,\n    bool growing_retained);\nstatic bool extent_merge_default(extent_hooks_t *extent_hooks, void *addr_a,\n    size_t size_a, void *addr_b, size_t size_b, bool committed,\n    unsigned arena_ind);\nstatic bool extent_merge_impl(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *a, extent_t *b,\n    bool growing_retained);\n\nconst extent_hooks_t\textent_hooks_default = {\n\textent_alloc_default,\n\textent_dalloc_default,\n\textent_destroy_default,\n\textent_commit_default,\n\textent_decommit_default\n#ifdef PAGES_CAN_PURGE_LAZY\n\t,\n\textent_purge_lazy_default\n#else\n\t,\n\tNULL\n#endif\n#ifdef PAGES_CAN_PURGE_FORCED\n\t,\n\textent_purge_forced_default\n#else\n\t,\n\tNULL\n#endif\n\t,\n\textent_split_default,\n\textent_merge_default\n};\n\n/* Used exclusively for gdump triggering. */\nstatic atomic_zu_t curpages;\nstatic atomic_zu_t highpages;\n\n/******************************************************************************/\n/*\n * Function prototypes for static functions that are referenced prior to\n * definition.\n */\n\nstatic void extent_deregister(tsdn_t *tsdn, extent_t *extent);\nstatic extent_t *extent_recycle(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extents_t *extents, void *new_addr,\n    size_t usize, size_t pad, size_t alignment, bool slab, szind_t szind,\n    bool *zero, bool *commit, bool growing_retained);\nstatic extent_t *extent_try_coalesce(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, rtree_ctx_t *rtree_ctx, extents_t *extents,\n    extent_t *extent, bool *coalesced, bool growing_retained);\nstatic void extent_record(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extents_t *extents, extent_t *extent,\n    bool growing_retained);\n\n/******************************************************************************/\n\n#define ATTR_NONE /* does nothing */\n\nph_gen(ATTR_NONE, extent_avail_, extent_tree_t, extent_t, ph_link,\n    extent_esnead_comp)\n\n#undef ATTR_NONE\n\ntypedef enum {\n\tlock_result_success,\n\tlock_result_failure,\n\tlock_result_no_extent\n} lock_result_t;\n\nstatic lock_result_t\nextent_rtree_leaf_elm_try_lock(tsdn_t *tsdn, rtree_leaf_elm_t *elm,\n    extent_t **result, bool inactive_only) {\n\textent_t *extent1 = rtree_leaf_elm_extent_read(tsdn, &extents_rtree,\n\t    elm, true);\n\n\t/* Slab implies active extents and should be skipped. */\n\tif (extent1 == NULL || (inactive_only && rtree_leaf_elm_slab_read(tsdn,\n\t    &extents_rtree, elm, true))) {\n\t\treturn lock_result_no_extent;\n\t}\n\n\t/*\n\t * It's possible that the extent changed out from under us, and with it\n\t * the leaf->extent mapping.  We have to recheck while holding the lock.\n\t */\n\textent_lock(tsdn, extent1);\n\textent_t *extent2 = rtree_leaf_elm_extent_read(tsdn,\n\t    &extents_rtree, elm, true);\n\n\tif (extent1 == extent2) {\n\t\t*result = extent1;\n\t\treturn lock_result_success;\n\t} else {\n\t\textent_unlock(tsdn, extent1);\n\t\treturn lock_result_failure;\n\t}\n}\n\n/*\n * Returns a pool-locked extent_t * if there's one associated with the given\n * address, and NULL otherwise.\n */\nstatic extent_t *\nextent_lock_from_addr(tsdn_t *tsdn, rtree_ctx_t *rtree_ctx, void *addr,\n    bool inactive_only) {\n\textent_t *ret = NULL;\n\trtree_leaf_elm_t *elm = rtree_leaf_elm_lookup(tsdn, &extents_rtree,\n\t    rtree_ctx, (uintptr_t)addr, false, false);\n\tif (elm == NULL) {\n\t\treturn NULL;\n\t}\n\tlock_result_t lock_result;\n\tdo {\n\t\tlock_result = extent_rtree_leaf_elm_try_lock(tsdn, elm, &ret,\n\t\t    inactive_only);\n\t} while (lock_result == lock_result_failure);\n\treturn ret;\n}\n\nextent_t *\nextent_alloc(tsdn_t *tsdn, arena_t *arena) {\n\tmalloc_mutex_lock(tsdn, &arena->extent_avail_mtx);\n\textent_t *extent = extent_avail_first(&arena->extent_avail);\n\tif (extent == NULL) {\n\t\tmalloc_mutex_unlock(tsdn, &arena->extent_avail_mtx);\n\t\treturn base_alloc_extent(tsdn, arena->base);\n\t}\n\textent_avail_remove(&arena->extent_avail, extent);\n\tatomic_fetch_sub_zu(&arena->extent_avail_cnt, 1, ATOMIC_RELAXED);\n\tmalloc_mutex_unlock(tsdn, &arena->extent_avail_mtx);\n\treturn extent;\n}\n\nvoid\nextent_dalloc(tsdn_t *tsdn, arena_t *arena, extent_t *extent) {\n\tmalloc_mutex_lock(tsdn, &arena->extent_avail_mtx);\n\textent_avail_insert(&arena->extent_avail, extent);\n\tatomic_fetch_add_zu(&arena->extent_avail_cnt, 1, ATOMIC_RELAXED);\n\tmalloc_mutex_unlock(tsdn, &arena->extent_avail_mtx);\n}\n\nextent_hooks_t *\nextent_hooks_get(arena_t *arena) {\n\treturn base_extent_hooks_get(arena->base);\n}\n\nextent_hooks_t *\nextent_hooks_set(tsd_t *tsd, arena_t *arena, extent_hooks_t *extent_hooks) {\n\tbackground_thread_info_t *info;\n\tif (have_background_thread) {\n\t\tinfo = arena_background_thread_info_get(arena);\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);\n\t}\n\textent_hooks_t *ret = base_extent_hooks_set(arena->base, extent_hooks);\n\tif (have_background_thread) {\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);\n\t}\n\n\treturn ret;\n}\n\nstatic void\nextent_hooks_assure_initialized(arena_t *arena,\n    extent_hooks_t **r_extent_hooks) {\n\tif (*r_extent_hooks == EXTENT_HOOKS_INITIALIZER) {\n\t\t*r_extent_hooks = extent_hooks_get(arena);\n\t}\n}\n\n#ifndef JEMALLOC_JET\nstatic\n#endif\nsize_t\nextent_size_quantize_floor(size_t size) {\n\tsize_t ret;\n\tpszind_t pind;\n\n\tassert(size > 0);\n\tassert((size & PAGE_MASK) == 0);\n\n\tpind = sz_psz2ind(size - sz_large_pad + 1);\n\tif (pind == 0) {\n\t\t/*\n\t\t * Avoid underflow.  This short-circuit would also do the right\n\t\t * thing for all sizes in the range for which there are\n\t\t * PAGE-spaced size classes, but it's simplest to just handle\n\t\t * the one case that would cause erroneous results.\n\t\t */\n\t\treturn size;\n\t}\n\tret = sz_pind2sz(pind - 1) + sz_large_pad;\n\tassert(ret <= size);\n\treturn ret;\n}\n\n#ifndef JEMALLOC_JET\nstatic\n#endif\nsize_t\nextent_size_quantize_ceil(size_t size) {\n\tsize_t ret;\n\n\tassert(size > 0);\n\tassert(size - sz_large_pad <= SC_LARGE_MAXCLASS);\n\tassert((size & PAGE_MASK) == 0);\n\n\tret = extent_size_quantize_floor(size);\n\tif (ret < size) {\n\t\t/*\n\t\t * Skip a quantization that may have an adequately large extent,\n\t\t * because under-sized extents may be mixed in.  This only\n\t\t * happens when an unusual size is requested, i.e. for aligned\n\t\t * allocation, and is just one of several places where linear\n\t\t * search would potentially find sufficiently aligned available\n\t\t * memory somewhere lower.\n\t\t */\n\t\tret = sz_pind2sz(sz_psz2ind(ret - sz_large_pad + 1)) +\n\t\t    sz_large_pad;\n\t}\n\treturn ret;\n}\n\n/* Generate pairing heap functions. */\nph_gen(, extent_heap_, extent_heap_t, extent_t, ph_link, extent_snad_comp)\n\nbool\nextents_init(tsdn_t *tsdn, extents_t *extents, extent_state_t state,\n    bool delay_coalesce) {\n\tif (malloc_mutex_init(&extents->mtx, \"extents\", WITNESS_RANK_EXTENTS,\n\t    malloc_mutex_rank_exclusive)) {\n\t\treturn true;\n\t}\n\tfor (unsigned i = 0; i < SC_NPSIZES + 1; i++) {\n\t\textent_heap_new(&extents->heaps[i]);\n\t}\n\tbitmap_init(extents->bitmap, &extents_bitmap_info, true);\n\textent_list_init(&extents->lru);\n\tatomic_store_zu(&extents->npages, 0, ATOMIC_RELAXED);\n\textents->state = state;\n\textents->delay_coalesce = delay_coalesce;\n\treturn false;\n}\n\nextent_state_t\nextents_state_get(const extents_t *extents) {\n\treturn extents->state;\n}\n\nsize_t\nextents_npages_get(extents_t *extents) {\n\treturn atomic_load_zu(&extents->npages, ATOMIC_RELAXED);\n}\n\nsize_t\nextents_nextents_get(extents_t *extents, pszind_t pind) {\n\treturn atomic_load_zu(&extents->nextents[pind], ATOMIC_RELAXED);\n}\n\nsize_t\nextents_nbytes_get(extents_t *extents, pszind_t pind) {\n\treturn atomic_load_zu(&extents->nbytes[pind], ATOMIC_RELAXED);\n}\n\nstatic void\nextents_stats_add(extents_t *extent, pszind_t pind, size_t sz) {\n\tsize_t cur = atomic_load_zu(&extent->nextents[pind], ATOMIC_RELAXED);\n\tatomic_store_zu(&extent->nextents[pind], cur + 1, ATOMIC_RELAXED);\n\tcur = atomic_load_zu(&extent->nbytes[pind], ATOMIC_RELAXED);\n\tatomic_store_zu(&extent->nbytes[pind], cur + sz, ATOMIC_RELAXED);\n}\n\nstatic void\nextents_stats_sub(extents_t *extent, pszind_t pind, size_t sz) {\n\tsize_t cur = atomic_load_zu(&extent->nextents[pind], ATOMIC_RELAXED);\n\tatomic_store_zu(&extent->nextents[pind], cur - 1, ATOMIC_RELAXED);\n\tcur = atomic_load_zu(&extent->nbytes[pind], ATOMIC_RELAXED);\n\tatomic_store_zu(&extent->nbytes[pind], cur - sz, ATOMIC_RELAXED);\n}\n\nstatic void\nextents_insert_locked(tsdn_t *tsdn, extents_t *extents, extent_t *extent) {\n\tmalloc_mutex_assert_owner(tsdn, &extents->mtx);\n\tassert(extent_state_get(extent) == extents->state);\n\n\tsize_t size = extent_size_get(extent);\n\tsize_t psz = extent_size_quantize_floor(size);\n\tpszind_t pind = sz_psz2ind(psz);\n\tif (extent_heap_empty(&extents->heaps[pind])) {\n\t\tbitmap_unset(extents->bitmap, &extents_bitmap_info,\n\t\t    (size_t)pind);\n\t}\n\textent_heap_insert(&extents->heaps[pind], extent);\n\n\tif (config_stats) {\n\t\textents_stats_add(extents, pind, size);\n\t}\n\n\textent_list_append(&extents->lru, extent);\n\tsize_t npages = size >> LG_PAGE;\n\t/*\n\t * All modifications to npages hold the mutex (as asserted above), so we\n\t * don't need an atomic fetch-add; we can get by with a load followed by\n\t * a store.\n\t */\n\tsize_t cur_extents_npages =\n\t    atomic_load_zu(&extents->npages, ATOMIC_RELAXED);\n\tatomic_store_zu(&extents->npages, cur_extents_npages + npages,\n\t    ATOMIC_RELAXED);\n}\n\nstatic void\nextents_remove_locked(tsdn_t *tsdn, extents_t *extents, extent_t *extent) {\n\tmalloc_mutex_assert_owner(tsdn, &extents->mtx);\n\tassert(extent_state_get(extent) == extents->state);\n\n\tsize_t size = extent_size_get(extent);\n\tsize_t psz = extent_size_quantize_floor(size);\n\tpszind_t pind = sz_psz2ind(psz);\n\textent_heap_remove(&extents->heaps[pind], extent);\n\n\tif (config_stats) {\n\t\textents_stats_sub(extents, pind, size);\n\t}\n\n\tif (extent_heap_empty(&extents->heaps[pind])) {\n\t\tbitmap_set(extents->bitmap, &extents_bitmap_info,\n\t\t    (size_t)pind);\n\t}\n\textent_list_remove(&extents->lru, extent);\n\tsize_t npages = size >> LG_PAGE;\n\t/*\n\t * As in extents_insert_locked, we hold extents->mtx and so don't need\n\t * atomic operations for updating extents->npages.\n\t */\n\tsize_t cur_extents_npages =\n\t    atomic_load_zu(&extents->npages, ATOMIC_RELAXED);\n\tassert(cur_extents_npages >= npages);\n\tatomic_store_zu(&extents->npages,\n\t    cur_extents_npages - (size >> LG_PAGE), ATOMIC_RELAXED);\n}\n\n/*\n * Find an extent with size [min_size, max_size) to satisfy the alignment\n * requirement.  For each size, try only the first extent in the heap.\n */\nstatic extent_t *\nextents_fit_alignment(extents_t *extents, size_t min_size, size_t max_size,\n    size_t alignment) {\n        pszind_t pind = sz_psz2ind(extent_size_quantize_ceil(min_size));\n        pszind_t pind_max = sz_psz2ind(extent_size_quantize_ceil(max_size));\n\n\tfor (pszind_t i = (pszind_t)bitmap_ffu(extents->bitmap,\n\t    &extents_bitmap_info, (size_t)pind); i < pind_max; i =\n\t    (pszind_t)bitmap_ffu(extents->bitmap, &extents_bitmap_info,\n\t    (size_t)i+1)) {\n\t\tassert(i < SC_NPSIZES);\n\t\tassert(!extent_heap_empty(&extents->heaps[i]));\n\t\textent_t *extent = extent_heap_first(&extents->heaps[i]);\n\t\tuintptr_t base = (uintptr_t)extent_base_get(extent);\n\t\tsize_t candidate_size = extent_size_get(extent);\n\t\tassert(candidate_size >= min_size);\n\n\t\tuintptr_t next_align = ALIGNMENT_CEILING((uintptr_t)base,\n\t\t    PAGE_CEILING(alignment));\n\t\tif (base > next_align || base + candidate_size <= next_align) {\n\t\t\t/* Overflow or not crossing the next alignment. */\n\t\t\tcontinue;\n\t\t}\n\n\t\tsize_t leadsize = next_align - base;\n\t\tif (candidate_size - leadsize >= min_size) {\n\t\t\treturn extent;\n\t\t}\n\t}\n\n\treturn NULL;\n}\n\n/*\n * Do first-fit extent selection, i.e. select the oldest/lowest extent that is\n * large enough.\n */\nstatic extent_t *\nextents_first_fit_locked(tsdn_t *tsdn, arena_t *arena, extents_t *extents,\n    size_t size) {\n\textent_t *ret = NULL;\n\n\tpszind_t pind = sz_psz2ind(extent_size_quantize_ceil(size));\n\n\tif (!maps_coalesce && !opt_retain) {\n\t\t/*\n\t\t * No split / merge allowed (Windows w/o retain). Try exact fit\n\t\t * only.\n\t\t */\n\t\treturn extent_heap_empty(&extents->heaps[pind]) ? NULL :\n\t\t    extent_heap_first(&extents->heaps[pind]);\n\t}\n\n\tfor (pszind_t i = (pszind_t)bitmap_ffu(extents->bitmap,\n\t    &extents_bitmap_info, (size_t)pind);\n\t    i < SC_NPSIZES + 1;\n\t    i = (pszind_t)bitmap_ffu(extents->bitmap, &extents_bitmap_info,\n\t    (size_t)i+1)) {\n\t\tassert(!extent_heap_empty(&extents->heaps[i]));\n\t\textent_t *extent = extent_heap_first(&extents->heaps[i]);\n\t\tassert(extent_size_get(extent) >= size);\n\t\t/*\n\t\t * In order to reduce fragmentation, avoid reusing and splitting\n\t\t * large extents for much smaller sizes.\n\t\t *\n\t\t * Only do check for dirty extents (delay_coalesce).\n\t\t */\n\t\tif (extents->delay_coalesce &&\n\t\t    (sz_pind2sz(i) >> opt_lg_extent_max_active_fit) > size) {\n\t\t\tbreak;\n\t\t}\n\t\tif (ret == NULL || extent_snad_comp(extent, ret) < 0) {\n\t\t\tret = extent;\n\t\t}\n\t\tif (i == SC_NPSIZES) {\n\t\t\tbreak;\n\t\t}\n\t\tassert(i < SC_NPSIZES);\n\t}\n\n\treturn ret;\n}\n\n/*\n * Do first-fit extent selection, where the selection policy choice is\n * based on extents->delay_coalesce.\n */\nstatic extent_t *\nextents_fit_locked(tsdn_t *tsdn, arena_t *arena, extents_t *extents,\n    size_t esize, size_t alignment) {\n\tmalloc_mutex_assert_owner(tsdn, &extents->mtx);\n\n\tsize_t max_size = esize + PAGE_CEILING(alignment) - PAGE;\n\t/* Beware size_t wrap-around. */\n\tif (max_size < esize) {\n\t\treturn NULL;\n\t}\n\n\textent_t *extent =\n\t    extents_first_fit_locked(tsdn, arena, extents, max_size);\n\n\tif (alignment > PAGE && extent == NULL) {\n\t\t/*\n\t\t * max_size guarantees the alignment requirement but is rather\n\t\t * pessimistic.  Next we try to satisfy the aligned allocation\n\t\t * with sizes in [esize, max_size).\n\t\t */\n\t\textent = extents_fit_alignment(extents, esize, max_size,\n\t\t    alignment);\n\t}\n\n\treturn extent;\n}\n\nstatic bool\nextent_try_delayed_coalesce(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, rtree_ctx_t *rtree_ctx, extents_t *extents,\n    extent_t *extent) {\n\textent_state_set(extent, extent_state_active);\n\tbool coalesced;\n\textent = extent_try_coalesce(tsdn, arena, r_extent_hooks, rtree_ctx,\n\t    extents, extent, &coalesced, false);\n\textent_state_set(extent, extents_state_get(extents));\n\n\tif (!coalesced) {\n\t\treturn true;\n\t}\n\textents_insert_locked(tsdn, extents, extent);\n\treturn false;\n}\n\nextent_t *\nextents_alloc(tsdn_t *tsdn, arena_t *arena, extent_hooks_t **r_extent_hooks,\n    extents_t *extents, void *new_addr, size_t size, size_t pad,\n    size_t alignment, bool slab, szind_t szind, bool *zero, bool *commit) {\n\tassert(size + pad != 0);\n\tassert(alignment != 0);\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\textent_t *extent = extent_recycle(tsdn, arena, r_extent_hooks, extents,\n\t    new_addr, size, pad, alignment, slab, szind, zero, commit, false);\n\tassert(extent == NULL || extent_dumpable_get(extent));\n\treturn extent;\n}\n\nvoid\nextents_dalloc(tsdn_t *tsdn, arena_t *arena, extent_hooks_t **r_extent_hooks,\n    extents_t *extents, extent_t *extent) {\n\tassert(extent_base_get(extent) != NULL);\n\tassert(extent_size_get(extent) != 0);\n\tassert(extent_dumpable_get(extent));\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\textent_addr_set(extent, extent_base_get(extent));\n\textent_zeroed_set(extent, false);\n\n\textent_record(tsdn, arena, r_extent_hooks, extents, extent, false);\n}\n\nextent_t *\nextents_evict(tsdn_t *tsdn, arena_t *arena, extent_hooks_t **r_extent_hooks,\n    extents_t *extents, size_t npages_min) {\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\n\tmalloc_mutex_lock(tsdn, &extents->mtx);\n\n\t/*\n\t * Get the LRU coalesced extent, if any.  If coalescing was delayed,\n\t * the loop will iterate until the LRU extent is fully coalesced.\n\t */\n\textent_t *extent;\n\twhile (true) {\n\t\t/* Get the LRU extent, if any. */\n\t\textent = extent_list_first(&extents->lru);\n\t\tif (extent == NULL) {\n\t\t\tgoto label_return;\n\t\t}\n\t\t/* Check the eviction limit. */\n\t\tsize_t extents_npages = atomic_load_zu(&extents->npages,\n\t\t    ATOMIC_RELAXED);\n\t\tif (extents_npages <= npages_min) {\n\t\t\textent = NULL;\n\t\t\tgoto label_return;\n\t\t}\n\t\textents_remove_locked(tsdn, extents, extent);\n\t\tif (!extents->delay_coalesce) {\n\t\t\tbreak;\n\t\t}\n\t\t/* Try to coalesce. */\n\t\tif (extent_try_delayed_coalesce(tsdn, arena, r_extent_hooks,\n\t\t    rtree_ctx, extents, extent)) {\n\t\t\tbreak;\n\t\t}\n\t\t/*\n\t\t * The LRU extent was just coalesced and the result placed in\n\t\t * the LRU at its neighbor's position.  Start over.\n\t\t */\n\t}\n\n\t/*\n\t * Either mark the extent active or deregister it to protect against\n\t * concurrent operations.\n\t */\n\tswitch (extents_state_get(extents)) {\n\tcase extent_state_active:\n\t\tnot_reached();\n\tcase extent_state_dirty:\n\tcase extent_state_muzzy:\n\t\textent_state_set(extent, extent_state_active);\n\t\tbreak;\n\tcase extent_state_retained:\n\t\textent_deregister(tsdn, extent);\n\t\tbreak;\n\tdefault:\n\t\tnot_reached();\n\t}\n\nlabel_return:\n\tmalloc_mutex_unlock(tsdn, &extents->mtx);\n\treturn extent;\n}\n\n/*\n * This can only happen when we fail to allocate a new extent struct (which\n * indicates OOM), e.g. when trying to split an existing extent.\n */\nstatic void\nextents_abandon_vm(tsdn_t *tsdn, arena_t *arena, extent_hooks_t **r_extent_hooks,\n    extents_t *extents, extent_t *extent, bool growing_retained) {\n\tsize_t sz = extent_size_get(extent);\n\tif (config_stats) {\n\t\tarena_stats_accum_zu(&arena->stats.abandoned_vm, sz);\n\t}\n\t/*\n\t * Leak extent after making sure its pages have already been purged, so\n\t * that this is only a virtual memory leak.\n\t */\n\tif (extents_state_get(extents) == extent_state_dirty) {\n\t\tif (extent_purge_lazy_impl(tsdn, arena, r_extent_hooks,\n\t\t    extent, 0, sz, growing_retained)) {\n\t\t\textent_purge_forced_impl(tsdn, arena, r_extent_hooks,\n\t\t\t    extent, 0, extent_size_get(extent),\n\t\t\t    growing_retained);\n\t\t}\n\t}\n\textent_dalloc(tsdn, arena, extent);\n}\n\nvoid\nextents_prefork(tsdn_t *tsdn, extents_t *extents) {\n\tmalloc_mutex_prefork(tsdn, &extents->mtx);\n}\n\nvoid\nextents_postfork_parent(tsdn_t *tsdn, extents_t *extents) {\n\tmalloc_mutex_postfork_parent(tsdn, &extents->mtx);\n}\n\nvoid\nextents_postfork_child(tsdn_t *tsdn, extents_t *extents) {\n\tmalloc_mutex_postfork_child(tsdn, &extents->mtx);\n}\n\nstatic void\nextent_deactivate_locked(tsdn_t *tsdn, arena_t *arena, extents_t *extents,\n    extent_t *extent) {\n\tassert(extent_arena_get(extent) == arena);\n\tassert(extent_state_get(extent) == extent_state_active);\n\n\textent_state_set(extent, extents_state_get(extents));\n\textents_insert_locked(tsdn, extents, extent);\n}\n\nstatic void\nextent_deactivate(tsdn_t *tsdn, arena_t *arena, extents_t *extents,\n    extent_t *extent) {\n\tmalloc_mutex_lock(tsdn, &extents->mtx);\n\textent_deactivate_locked(tsdn, arena, extents, extent);\n\tmalloc_mutex_unlock(tsdn, &extents->mtx);\n}\n\nstatic void\nextent_activate_locked(tsdn_t *tsdn, arena_t *arena, extents_t *extents,\n    extent_t *extent) {\n\tassert(extent_arena_get(extent) == arena);\n\tassert(extent_state_get(extent) == extents_state_get(extents));\n\n\textents_remove_locked(tsdn, extents, extent);\n\textent_state_set(extent, extent_state_active);\n}\n\nstatic bool\nextent_rtree_leaf_elms_lookup(tsdn_t *tsdn, rtree_ctx_t *rtree_ctx,\n    const extent_t *extent, bool dependent, bool init_missing,\n    rtree_leaf_elm_t **r_elm_a, rtree_leaf_elm_t **r_elm_b) {\n\t*r_elm_a = rtree_leaf_elm_lookup(tsdn, &extents_rtree, rtree_ctx,\n\t    (uintptr_t)extent_base_get(extent), dependent, init_missing);\n\tif (!dependent && *r_elm_a == NULL) {\n\t\treturn true;\n\t}\n\tassert(*r_elm_a != NULL);\n\n\t*r_elm_b = rtree_leaf_elm_lookup(tsdn, &extents_rtree, rtree_ctx,\n\t    (uintptr_t)extent_last_get(extent), dependent, init_missing);\n\tif (!dependent && *r_elm_b == NULL) {\n\t\treturn true;\n\t}\n\tassert(*r_elm_b != NULL);\n\n\treturn false;\n}\n\nstatic void\nextent_rtree_write_acquired(tsdn_t *tsdn, rtree_leaf_elm_t *elm_a,\n    rtree_leaf_elm_t *elm_b, extent_t *extent, szind_t szind, bool slab) {\n\trtree_leaf_elm_write(tsdn, &extents_rtree, elm_a, extent, szind, slab);\n\tif (elm_b != NULL) {\n\t\trtree_leaf_elm_write(tsdn, &extents_rtree, elm_b, extent, szind,\n\t\t    slab);\n\t}\n}\n\nstatic void\nextent_interior_register(tsdn_t *tsdn, rtree_ctx_t *rtree_ctx, extent_t *extent,\n    szind_t szind) {\n\tassert(extent_slab_get(extent));\n\n\t/* Register interior. */\n\tfor (size_t i = 1; i < (extent_size_get(extent) >> LG_PAGE) - 1; i++) {\n\t\trtree_write(tsdn, &extents_rtree, rtree_ctx,\n\t\t    (uintptr_t)extent_base_get(extent) + (uintptr_t)(i <<\n\t\t    LG_PAGE), extent, szind, true);\n\t}\n}\n\nstatic void\nextent_gdump_add(tsdn_t *tsdn, const extent_t *extent) {\n\tcassert(config_prof);\n\t/* prof_gdump() requirement. */\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\tif (opt_prof && extent_state_get(extent) == extent_state_active) {\n\t\tsize_t nadd = extent_size_get(extent) >> LG_PAGE;\n\t\tsize_t cur = atomic_fetch_add_zu(&curpages, nadd,\n\t\t    ATOMIC_RELAXED) + nadd;\n\t\tsize_t high = atomic_load_zu(&highpages, ATOMIC_RELAXED);\n\t\twhile (cur > high && !atomic_compare_exchange_weak_zu(\n\t\t    &highpages, &high, cur, ATOMIC_RELAXED, ATOMIC_RELAXED)) {\n\t\t\t/*\n\t\t\t * Don't refresh cur, because it may have decreased\n\t\t\t * since this thread lost the highpages update race.\n\t\t\t * Note that high is updated in case of CAS failure.\n\t\t\t */\n\t\t}\n\t\tif (cur > high && prof_gdump_get_unlocked()) {\n\t\t\tprof_gdump(tsdn);\n\t\t}\n\t}\n}\n\nstatic void\nextent_gdump_sub(tsdn_t *tsdn, const extent_t *extent) {\n\tcassert(config_prof);\n\n\tif (opt_prof && extent_state_get(extent) == extent_state_active) {\n\t\tsize_t nsub = extent_size_get(extent) >> LG_PAGE;\n\t\tassert(atomic_load_zu(&curpages, ATOMIC_RELAXED) >= nsub);\n\t\tatomic_fetch_sub_zu(&curpages, nsub, ATOMIC_RELAXED);\n\t}\n}\n\nstatic bool\nextent_register_impl(tsdn_t *tsdn, extent_t *extent, bool gdump_add) {\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\trtree_leaf_elm_t *elm_a, *elm_b;\n\n\t/*\n\t * We need to hold the lock to protect against a concurrent coalesce\n\t * operation that sees us in a partial state.\n\t */\n\textent_lock(tsdn, extent);\n\n\tif (extent_rtree_leaf_elms_lookup(tsdn, rtree_ctx, extent, false, true,\n\t    &elm_a, &elm_b)) {\n\t\textent_unlock(tsdn, extent);\n\t\treturn true;\n\t}\n\n\tszind_t szind = extent_szind_get_maybe_invalid(extent);\n\tbool slab = extent_slab_get(extent);\n\textent_rtree_write_acquired(tsdn, elm_a, elm_b, extent, szind, slab);\n\tif (slab) {\n\t\textent_interior_register(tsdn, rtree_ctx, extent, szind);\n\t}\n\n\textent_unlock(tsdn, extent);\n\n\tif (config_prof && gdump_add) {\n\t\textent_gdump_add(tsdn, extent);\n\t}\n\n\treturn false;\n}\n\nstatic bool\nextent_register(tsdn_t *tsdn, extent_t *extent) {\n\treturn extent_register_impl(tsdn, extent, true);\n}\n\nstatic bool\nextent_register_no_gdump_add(tsdn_t *tsdn, extent_t *extent) {\n\treturn extent_register_impl(tsdn, extent, false);\n}\n\nstatic void\nextent_reregister(tsdn_t *tsdn, extent_t *extent) {\n\tbool err = extent_register(tsdn, extent);\n\tassert(!err);\n}\n\n/*\n * Removes all pointers to the given extent from the global rtree indices for\n * its interior.  This is relevant for slab extents, for which we need to do\n * metadata lookups at places other than the head of the extent.  We deregister\n * on the interior, then, when an extent moves from being an active slab to an\n * inactive state.\n */\nstatic void\nextent_interior_deregister(tsdn_t *tsdn, rtree_ctx_t *rtree_ctx,\n    extent_t *extent) {\n\tsize_t i;\n\n\tassert(extent_slab_get(extent));\n\n\tfor (i = 1; i < (extent_size_get(extent) >> LG_PAGE) - 1; i++) {\n\t\trtree_clear(tsdn, &extents_rtree, rtree_ctx,\n\t\t    (uintptr_t)extent_base_get(extent) + (uintptr_t)(i <<\n\t\t    LG_PAGE));\n\t}\n}\n\n/*\n * Removes all pointers to the given extent from the global rtree.\n */\nstatic void\nextent_deregister_impl(tsdn_t *tsdn, extent_t *extent, bool gdump) {\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\trtree_leaf_elm_t *elm_a, *elm_b;\n\textent_rtree_leaf_elms_lookup(tsdn, rtree_ctx, extent, true, false,\n\t    &elm_a, &elm_b);\n\n\textent_lock(tsdn, extent);\n\n\textent_rtree_write_acquired(tsdn, elm_a, elm_b, NULL, SC_NSIZES, false);\n\tif (extent_slab_get(extent)) {\n\t\textent_interior_deregister(tsdn, rtree_ctx, extent);\n\t\textent_slab_set(extent, false);\n\t}\n\n\textent_unlock(tsdn, extent);\n\n\tif (config_prof && gdump) {\n\t\textent_gdump_sub(tsdn, extent);\n\t}\n}\n\nstatic void\nextent_deregister(tsdn_t *tsdn, extent_t *extent) {\n\textent_deregister_impl(tsdn, extent, true);\n}\n\nstatic void\nextent_deregister_no_gdump_sub(tsdn_t *tsdn, extent_t *extent) {\n\textent_deregister_impl(tsdn, extent, false);\n}\n\n/*\n * Tries to find and remove an extent from extents that can be used for the\n * given allocation request.\n */\nstatic extent_t *\nextent_recycle_extract(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, rtree_ctx_t *rtree_ctx, extents_t *extents,\n    void *new_addr, size_t size, size_t pad, size_t alignment, bool slab,\n    bool growing_retained) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, growing_retained ? 1 : 0);\n\tassert(alignment > 0);\n\tif (config_debug && new_addr != NULL) {\n\t\t/*\n\t\t * Non-NULL new_addr has two use cases:\n\t\t *\n\t\t *   1) Recycle a known-extant extent, e.g. during purging.\n\t\t *   2) Perform in-place expanding reallocation.\n\t\t *\n\t\t * Regardless of use case, new_addr must either refer to a\n\t\t * non-existing extent, or to the base of an extant extent,\n\t\t * since only active slabs support interior lookups (which of\n\t\t * course cannot be recycled).\n\t\t */\n\t\tassert(PAGE_ADDR2BASE(new_addr) == new_addr);\n\t\tassert(pad == 0);\n\t\tassert(alignment <= PAGE);\n\t}\n\n\tsize_t esize = size + pad;\n\tmalloc_mutex_lock(tsdn, &extents->mtx);\n\textent_hooks_assure_initialized(arena, r_extent_hooks);\n\textent_t *extent;\n\tif (new_addr != NULL) {\n\t\textent = extent_lock_from_addr(tsdn, rtree_ctx, new_addr,\n\t\t    false);\n\t\tif (extent != NULL) {\n\t\t\t/*\n\t\t\t * We might null-out extent to report an error, but we\n\t\t\t * still need to unlock the associated mutex after.\n\t\t\t */\n\t\t\textent_t *unlock_extent = extent;\n\t\t\tassert(extent_base_get(extent) == new_addr);\n\t\t\tif (extent_arena_get(extent) != arena ||\n\t\t\t    extent_size_get(extent) < esize ||\n\t\t\t    extent_state_get(extent) !=\n\t\t\t    extents_state_get(extents)) {\n\t\t\t\textent = NULL;\n\t\t\t}\n\t\t\textent_unlock(tsdn, unlock_extent);\n\t\t}\n\t} else {\n\t\textent = extents_fit_locked(tsdn, arena, extents, esize,\n\t\t    alignment);\n\t}\n\tif (extent == NULL) {\n\t\tmalloc_mutex_unlock(tsdn, &extents->mtx);\n\t\treturn NULL;\n\t}\n\n\textent_activate_locked(tsdn, arena, extents, extent);\n\tmalloc_mutex_unlock(tsdn, &extents->mtx);\n\n\treturn extent;\n}\n\n/*\n * Given an allocation request and an extent guaranteed to be able to satisfy\n * it, this splits off lead and trail extents, leaving extent pointing to an\n * extent satisfying the allocation.\n * This function doesn't put lead or trail into any extents_t; it's the caller's\n * job to ensure that they can be reused.\n */\ntypedef enum {\n\t/*\n\t * Split successfully.  lead, extent, and trail, are modified to extents\n\t * describing the ranges before, in, and after the given allocation.\n\t */\n\textent_split_interior_ok,\n\t/*\n\t * The extent can't satisfy the given allocation request.  None of the\n\t * input extent_t *s are touched.\n\t */\n\textent_split_interior_cant_alloc,\n\t/*\n\t * In a potentially invalid state.  Must leak (if *to_leak is non-NULL),\n\t * and salvage what's still salvageable (if *to_salvage is non-NULL).\n\t * None of lead, extent, or trail are valid.\n\t */\n\textent_split_interior_error\n} extent_split_interior_result_t;\n\nstatic extent_split_interior_result_t\nextent_split_interior(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, rtree_ctx_t *rtree_ctx,\n    /* The result of splitting, in case of success. */\n    extent_t **extent, extent_t **lead, extent_t **trail,\n    /* The mess to clean up, in case of error. */\n    extent_t **to_leak, extent_t **to_salvage,\n    void *new_addr, size_t size, size_t pad, size_t alignment, bool slab,\n    szind_t szind, bool growing_retained) {\n\tsize_t esize = size + pad;\n\tsize_t leadsize = ALIGNMENT_CEILING((uintptr_t)extent_base_get(*extent),\n\t    PAGE_CEILING(alignment)) - (uintptr_t)extent_base_get(*extent);\n\tassert(new_addr == NULL || leadsize == 0);\n\tif (extent_size_get(*extent) < leadsize + esize) {\n\t\treturn extent_split_interior_cant_alloc;\n\t}\n\tsize_t trailsize = extent_size_get(*extent) - leadsize - esize;\n\n\t*lead = NULL;\n\t*trail = NULL;\n\t*to_leak = NULL;\n\t*to_salvage = NULL;\n\n\t/* Split the lead. */\n\tif (leadsize != 0) {\n\t\t*lead = *extent;\n\t\t*extent = extent_split_impl(tsdn, arena, r_extent_hooks,\n\t\t    *lead, leadsize, SC_NSIZES, false, esize + trailsize, szind,\n\t\t    slab, growing_retained);\n\t\tif (*extent == NULL) {\n\t\t\t*to_leak = *lead;\n\t\t\t*lead = NULL;\n\t\t\treturn extent_split_interior_error;\n\t\t}\n\t}\n\n\t/* Split the trail. */\n\tif (trailsize != 0) {\n\t\t*trail = extent_split_impl(tsdn, arena, r_extent_hooks, *extent,\n\t\t    esize, szind, slab, trailsize, SC_NSIZES, false,\n\t\t    growing_retained);\n\t\tif (*trail == NULL) {\n\t\t\t*to_leak = *extent;\n\t\t\t*to_salvage = *lead;\n\t\t\t*lead = NULL;\n\t\t\t*extent = NULL;\n\t\t\treturn extent_split_interior_error;\n\t\t}\n\t}\n\n\tif (leadsize == 0 && trailsize == 0) {\n\t\t/*\n\t\t * Splitting causes szind to be set as a side effect, but no\n\t\t * splitting occurred.\n\t\t */\n\t\textent_szind_set(*extent, szind);\n\t\tif (szind != SC_NSIZES) {\n\t\t\trtree_szind_slab_update(tsdn, &extents_rtree, rtree_ctx,\n\t\t\t    (uintptr_t)extent_addr_get(*extent), szind, slab);\n\t\t\tif (slab && extent_size_get(*extent) > PAGE) {\n\t\t\t\trtree_szind_slab_update(tsdn, &extents_rtree,\n\t\t\t\t    rtree_ctx,\n\t\t\t\t    (uintptr_t)extent_past_get(*extent) -\n\t\t\t\t    (uintptr_t)PAGE, szind, slab);\n\t\t\t}\n\t\t}\n\t}\n\n\treturn extent_split_interior_ok;\n}\n\n/*\n * This fulfills the indicated allocation request out of the given extent (which\n * the caller should have ensured was big enough).  If there's any unused space\n * before or after the resulting allocation, that space is given its own extent\n * and put back into extents.\n */\nstatic extent_t *\nextent_recycle_split(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, rtree_ctx_t *rtree_ctx, extents_t *extents,\n    void *new_addr, size_t size, size_t pad, size_t alignment, bool slab,\n    szind_t szind, extent_t *extent, bool growing_retained) {\n\textent_t *lead;\n\textent_t *trail;\n\textent_t *to_leak;\n\textent_t *to_salvage;\n\n\textent_split_interior_result_t result = extent_split_interior(\n\t    tsdn, arena, r_extent_hooks, rtree_ctx, &extent, &lead, &trail,\n\t    &to_leak, &to_salvage, new_addr, size, pad, alignment, slab, szind,\n\t    growing_retained);\n\n\tif (!maps_coalesce && result != extent_split_interior_ok\n\t    && !opt_retain) {\n\t\t/*\n\t\t * Split isn't supported (implies Windows w/o retain).  Avoid\n\t\t * leaking the extents.\n\t\t */\n\t\tassert(to_leak != NULL && lead == NULL && trail == NULL);\n\t\textent_deactivate(tsdn, arena, extents, to_leak);\n\t\treturn NULL;\n\t}\n\n\tif (result == extent_split_interior_ok) {\n\t\tif (lead != NULL) {\n\t\t\textent_deactivate(tsdn, arena, extents, lead);\n\t\t}\n\t\tif (trail != NULL) {\n\t\t\textent_deactivate(tsdn, arena, extents, trail);\n\t\t}\n\t\treturn extent;\n\t} else {\n\t\t/*\n\t\t * We should have picked an extent that was large enough to\n\t\t * fulfill our allocation request.\n\t\t */\n\t\tassert(result == extent_split_interior_error);\n\t\tif (to_salvage != NULL) {\n\t\t\textent_deregister(tsdn, to_salvage);\n\t\t}\n\t\tif (to_leak != NULL) {\n\t\t\tvoid *leak = extent_base_get(to_leak);\n\t\t\textent_deregister_no_gdump_sub(tsdn, to_leak);\n\t\t\textents_abandon_vm(tsdn, arena, r_extent_hooks, extents,\n\t\t\t    to_leak, growing_retained);\n\t\t\tassert(extent_lock_from_addr(tsdn, rtree_ctx, leak,\n\t\t\t    false) == NULL);\n\t\t}\n\t\treturn NULL;\n\t}\n\tunreachable();\n}\n\nstatic bool\nextent_need_manual_zero(arena_t *arena) {\n\t/*\n\t * Need to manually zero the extent on repopulating if either; 1) non\n\t * default extent hooks installed (in which case the purge semantics may\n\t * change); or 2) transparent huge pages enabled.\n\t */\n\treturn (!arena_has_default_hooks(arena) ||\n\t\t(opt_thp == thp_mode_always));\n}\n\n/*\n * Tries to satisfy the given allocation request by reusing one of the extents\n * in the given extents_t.\n */\nstatic extent_t *\nextent_recycle(tsdn_t *tsdn, arena_t *arena, extent_hooks_t **r_extent_hooks,\n    extents_t *extents, void *new_addr, size_t size, size_t pad,\n    size_t alignment, bool slab, szind_t szind, bool *zero, bool *commit,\n    bool growing_retained) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, growing_retained ? 1 : 0);\n\tassert(new_addr == NULL || !slab);\n\tassert(pad == 0 || !slab);\n\tassert(!*zero || !slab);\n\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\n\textent_t *extent = extent_recycle_extract(tsdn, arena, r_extent_hooks,\n\t    rtree_ctx, extents, new_addr, size, pad, alignment, slab,\n\t    growing_retained);\n\tif (extent == NULL) {\n\t\treturn NULL;\n\t}\n\n\textent = extent_recycle_split(tsdn, arena, r_extent_hooks, rtree_ctx,\n\t    extents, new_addr, size, pad, alignment, slab, szind, extent,\n\t    growing_retained);\n\tif (extent == NULL) {\n\t\treturn NULL;\n\t}\n\n\tif (*commit && !extent_committed_get(extent)) {\n\t\tif (extent_commit_impl(tsdn, arena, r_extent_hooks, extent,\n\t\t    0, extent_size_get(extent), growing_retained)) {\n\t\t\textent_record(tsdn, arena, r_extent_hooks, extents,\n\t\t\t    extent, growing_retained);\n\t\t\treturn NULL;\n\t\t}\n\t\tif (!extent_need_manual_zero(arena)) {\n\t\t\textent_zeroed_set(extent, true);\n\t\t}\n\t}\n\n\tif (extent_committed_get(extent)) {\n\t\t*commit = true;\n\t}\n\tif (extent_zeroed_get(extent)) {\n\t\t*zero = true;\n\t}\n\n\tif (pad != 0) {\n\t\textent_addr_randomize(tsdn, extent, alignment);\n\t}\n\tassert(extent_state_get(extent) == extent_state_active);\n\tif (slab) {\n\t\textent_slab_set(extent, slab);\n\t\textent_interior_register(tsdn, rtree_ctx, extent, szind);\n\t}\n\n\tif (*zero) {\n\t\tvoid *addr = extent_base_get(extent);\n\t\tif (!extent_zeroed_get(extent)) {\n\t\t\tsize_t size = extent_size_get(extent);\n\t\t\tif (extent_need_manual_zero(arena) ||\n\t\t\t    pages_purge_forced(addr, size)) {\n\t\t\t\tmemset(addr, 0, size);\n\t\t\t}\n\t\t} else if (config_debug) {\n\t\t\tsize_t *p = (size_t *)(uintptr_t)addr;\n\t\t\t/* Check the first page only. */\n\t\t\tfor (size_t i = 0; i < PAGE / sizeof(size_t); i++) {\n\t\t\t\tassert(p[i] == 0);\n\t\t\t}\n\t\t}\n\t}\n\treturn extent;\n}\n\n/*\n * If the caller specifies (!*zero), it is still possible to receive zeroed\n * memory, in which case *zero is toggled to true.  arena_extent_alloc() takes\n * advantage of this to avoid demanding zeroed extents, but taking advantage of\n * them if they are returned.\n */\nstatic void *\nextent_alloc_core(tsdn_t *tsdn, arena_t *arena, void *new_addr, size_t size,\n    size_t alignment, bool *zero, bool *commit, dss_prec_t dss_prec) {\n\tvoid *ret;\n\n\tassert(size != 0);\n\tassert(alignment != 0);\n\n\t/* \"primary\" dss. */\n\tif (have_dss && dss_prec == dss_prec_primary && (ret =\n\t    extent_alloc_dss(tsdn, arena, new_addr, size, alignment, zero,\n\t    commit)) != NULL) {\n\t\treturn ret;\n\t}\n\t/* mmap. */\n\tif ((ret = extent_alloc_mmap(new_addr, size, alignment, zero, commit))\n\t    != NULL) {\n\t\treturn ret;\n\t}\n\t/* \"secondary\" dss. */\n\tif (have_dss && dss_prec == dss_prec_secondary && (ret =\n\t    extent_alloc_dss(tsdn, arena, new_addr, size, alignment, zero,\n\t    commit)) != NULL) {\n\t\treturn ret;\n\t}\n\n\t/* All strategies for allocation failed. */\n\treturn NULL;\n}\n\nstatic void *\nextent_alloc_default_impl(tsdn_t *tsdn, arena_t *arena, void *new_addr,\n    size_t size, size_t alignment, bool *zero, bool *commit) {\n\tvoid *ret = extent_alloc_core(tsdn, arena, new_addr, size, alignment, zero,\n\t    commit, (dss_prec_t)atomic_load_u(&arena->dss_prec,\n\t    ATOMIC_RELAXED));\n\tif (have_madvise_huge && ret) {\n\t\tpages_set_thp_state(ret, size);\n\t}\n\treturn ret;\n}\n\nstatic void *\nextent_alloc_default(extent_hooks_t *extent_hooks, void *new_addr, size_t size,\n    size_t alignment, bool *zero, bool *commit, unsigned arena_ind) {\n\ttsdn_t *tsdn;\n\tarena_t *arena;\n\n\ttsdn = tsdn_fetch();\n\tarena = arena_get(tsdn, arena_ind, false);\n\t/*\n\t * The arena we're allocating on behalf of must have been initialized\n\t * already.\n\t */\n\tassert(arena != NULL);\n\n\treturn extent_alloc_default_impl(tsdn, arena, new_addr, size,\n\t    ALIGNMENT_CEILING(alignment, PAGE), zero, commit);\n}\n\nstatic void\nextent_hook_pre_reentrancy(tsdn_t *tsdn, arena_t *arena) {\n\ttsd_t *tsd = tsdn_null(tsdn) ? tsd_fetch() : tsdn_tsd(tsdn);\n\tif (arena == arena_get(tsd_tsdn(tsd), 0, false)) {\n\t\t/*\n\t\t * The only legitimate case of customized extent hooks for a0 is\n\t\t * hooks with no allocation activities.  One such example is to\n\t\t * place metadata on pre-allocated resources such as huge pages.\n\t\t * In that case, rely on reentrancy_level checks to catch\n\t\t * infinite recursions.\n\t\t */\n\t\tpre_reentrancy(tsd, NULL);\n\t} else {\n\t\tpre_reentrancy(tsd, arena);\n\t}\n}\n\nstatic void\nextent_hook_post_reentrancy(tsdn_t *tsdn) {\n\ttsd_t *tsd = tsdn_null(tsdn) ? tsd_fetch() : tsdn_tsd(tsdn);\n\tpost_reentrancy(tsd);\n}\n\n/*\n * If virtual memory is retained, create increasingly larger extents from which\n * to split requested extents in order to limit the total number of disjoint\n * virtual memory ranges retained by each arena.\n */\nstatic extent_t *\nextent_grow_retained(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, size_t size, size_t pad, size_t alignment,\n    bool slab, szind_t szind, bool *zero, bool *commit) {\n\tmalloc_mutex_assert_owner(tsdn, &arena->extent_grow_mtx);\n\tassert(pad == 0 || !slab);\n\tassert(!*zero || !slab);\n\n\tsize_t esize = size + pad;\n\tsize_t alloc_size_min = esize + PAGE_CEILING(alignment) - PAGE;\n\t/* Beware size_t wrap-around. */\n\tif (alloc_size_min < esize) {\n\t\tgoto label_err;\n\t}\n\t/*\n\t * Find the next extent size in the series that would be large enough to\n\t * satisfy this request.\n\t */\n\tpszind_t egn_skip = 0;\n\tsize_t alloc_size = sz_pind2sz(arena->extent_grow_next + egn_skip);\n\twhile (alloc_size < alloc_size_min) {\n\t\tegn_skip++;\n\t\tif (arena->extent_grow_next + egn_skip >=\n\t\t    sz_psz2ind(SC_LARGE_MAXCLASS)) {\n\t\t\t/* Outside legal range. */\n\t\t\tgoto label_err;\n\t\t}\n\t\talloc_size = sz_pind2sz(arena->extent_grow_next + egn_skip);\n\t}\n\n\textent_t *extent = extent_alloc(tsdn, arena);\n\tif (extent == NULL) {\n\t\tgoto label_err;\n\t}\n\tbool zeroed = false;\n\tbool committed = false;\n\n\tvoid *ptr;\n\tif (*r_extent_hooks == &extent_hooks_default) {\n\t\tptr = extent_alloc_default_impl(tsdn, arena, NULL,\n\t\t    alloc_size, PAGE, &zeroed, &committed);\n\t} else {\n\t\textent_hook_pre_reentrancy(tsdn, arena);\n\t\tptr = (*r_extent_hooks)->alloc(*r_extent_hooks, NULL,\n\t\t    alloc_size, PAGE, &zeroed, &committed,\n\t\t    arena_ind_get(arena));\n\t\textent_hook_post_reentrancy(tsdn);\n\t}\n\n\textent_init(extent, arena, ptr, alloc_size, false, SC_NSIZES,\n\t    arena_extent_sn_next(arena), extent_state_active, zeroed,\n\t    committed, true, EXTENT_IS_HEAD);\n\tif (ptr == NULL) {\n\t\textent_dalloc(tsdn, arena, extent);\n\t\tgoto label_err;\n\t}\n\n\tif (extent_register_no_gdump_add(tsdn, extent)) {\n\t\textent_dalloc(tsdn, arena, extent);\n\t\tgoto label_err;\n\t}\n\n\tif (extent_zeroed_get(extent) && extent_committed_get(extent)) {\n\t\t*zero = true;\n\t}\n\tif (extent_committed_get(extent)) {\n\t\t*commit = true;\n\t}\n\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\n\textent_t *lead;\n\textent_t *trail;\n\textent_t *to_leak;\n\textent_t *to_salvage;\n\textent_split_interior_result_t result = extent_split_interior(\n\t    tsdn, arena, r_extent_hooks, rtree_ctx, &extent, &lead, &trail,\n\t    &to_leak, &to_salvage, NULL, size, pad, alignment, slab, szind,\n\t    true);\n\n\tif (result == extent_split_interior_ok) {\n\t\tif (lead != NULL) {\n\t\t\textent_record(tsdn, arena, r_extent_hooks,\n\t\t\t    &arena->extents_retained, lead, true);\n\t\t}\n\t\tif (trail != NULL) {\n\t\t\textent_record(tsdn, arena, r_extent_hooks,\n\t\t\t    &arena->extents_retained, trail, true);\n\t\t}\n\t} else {\n\t\t/*\n\t\t * We should have allocated a sufficiently large extent; the\n\t\t * cant_alloc case should not occur.\n\t\t */\n\t\tassert(result == extent_split_interior_error);\n\t\tif (to_salvage != NULL) {\n\t\t\tif (config_prof) {\n\t\t\t\textent_gdump_add(tsdn, to_salvage);\n\t\t\t}\n\t\t\textent_record(tsdn, arena, r_extent_hooks,\n\t\t\t    &arena->extents_retained, to_salvage, true);\n\t\t}\n\t\tif (to_leak != NULL) {\n\t\t\textent_deregister_no_gdump_sub(tsdn, to_leak);\n\t\t\textents_abandon_vm(tsdn, arena, r_extent_hooks,\n\t\t\t    &arena->extents_retained, to_leak, true);\n\t\t}\n\t\tgoto label_err;\n\t}\n\n\tif (*commit && !extent_committed_get(extent)) {\n\t\tif (extent_commit_impl(tsdn, arena, r_extent_hooks, extent, 0,\n\t\t    extent_size_get(extent), true)) {\n\t\t\textent_record(tsdn, arena, r_extent_hooks,\n\t\t\t    &arena->extents_retained, extent, true);\n\t\t\tgoto label_err;\n\t\t}\n\t\tif (!extent_need_manual_zero(arena)) {\n\t\t\textent_zeroed_set(extent, true);\n\t\t}\n\t}\n\n\t/*\n\t * Increment extent_grow_next if doing so wouldn't exceed the allowed\n\t * range.\n\t */\n\tif (arena->extent_grow_next + egn_skip + 1 <=\n\t    arena->retain_grow_limit) {\n\t\tarena->extent_grow_next += egn_skip + 1;\n\t} else {\n\t\tarena->extent_grow_next = arena->retain_grow_limit;\n\t}\n\t/* All opportunities for failure are past. */\n\tmalloc_mutex_unlock(tsdn, &arena->extent_grow_mtx);\n\n\tif (config_prof) {\n\t\t/* Adjust gdump stats now that extent is final size. */\n\t\textent_gdump_add(tsdn, extent);\n\t}\n\tif (pad != 0) {\n\t\textent_addr_randomize(tsdn, extent, alignment);\n\t}\n\tif (slab) {\n\t\trtree_ctx_t rtree_ctx_fallback;\n\t\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn,\n\t\t    &rtree_ctx_fallback);\n\n\t\textent_slab_set(extent, true);\n\t\textent_interior_register(tsdn, rtree_ctx, extent, szind);\n\t}\n\tif (*zero && !extent_zeroed_get(extent)) {\n\t\tvoid *addr = extent_base_get(extent);\n\t\tsize_t size = extent_size_get(extent);\n\t\tif (extent_need_manual_zero(arena) ||\n\t\t    pages_purge_forced(addr, size)) {\n\t\t\tmemset(addr, 0, size);\n\t\t}\n\t}\n\n\treturn extent;\nlabel_err:\n\tmalloc_mutex_unlock(tsdn, &arena->extent_grow_mtx);\n\treturn NULL;\n}\n\nstatic extent_t *\nextent_alloc_retained(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, void *new_addr, size_t size, size_t pad,\n    size_t alignment, bool slab, szind_t szind, bool *zero, bool *commit) {\n\tassert(size != 0);\n\tassert(alignment != 0);\n\n\tmalloc_mutex_lock(tsdn, &arena->extent_grow_mtx);\n\n\textent_t *extent = extent_recycle(tsdn, arena, r_extent_hooks,\n\t    &arena->extents_retained, new_addr, size, pad, alignment, slab,\n\t    szind, zero, commit, true);\n\tif (extent != NULL) {\n\t\tmalloc_mutex_unlock(tsdn, &arena->extent_grow_mtx);\n\t\tif (config_prof) {\n\t\t\textent_gdump_add(tsdn, extent);\n\t\t}\n\t} else if (opt_retain && new_addr == NULL) {\n\t\textent = extent_grow_retained(tsdn, arena, r_extent_hooks, size,\n\t\t    pad, alignment, slab, szind, zero, commit);\n\t\t/* extent_grow_retained() always releases extent_grow_mtx. */\n\t} else {\n\t\tmalloc_mutex_unlock(tsdn, &arena->extent_grow_mtx);\n\t}\n\tmalloc_mutex_assert_not_owner(tsdn, &arena->extent_grow_mtx);\n\n\treturn extent;\n}\n\nstatic extent_t *\nextent_alloc_wrapper_hard(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, void *new_addr, size_t size, size_t pad,\n    size_t alignment, bool slab, szind_t szind, bool *zero, bool *commit) {\n\tsize_t esize = size + pad;\n\textent_t *extent = extent_alloc(tsdn, arena);\n\tif (extent == NULL) {\n\t\treturn NULL;\n\t}\n\tvoid *addr;\n\tsize_t palignment = ALIGNMENT_CEILING(alignment, PAGE);\n\tif (*r_extent_hooks == &extent_hooks_default) {\n\t\t/* Call directly to propagate tsdn. */\n\t\taddr = extent_alloc_default_impl(tsdn, arena, new_addr, esize,\n\t\t    palignment, zero, commit);\n\t} else {\n\t\textent_hook_pre_reentrancy(tsdn, arena);\n\t\taddr = (*r_extent_hooks)->alloc(*r_extent_hooks, new_addr,\n\t\t    esize, palignment, zero, commit, arena_ind_get(arena));\n\t\textent_hook_post_reentrancy(tsdn);\n\t}\n\tif (addr == NULL) {\n\t\textent_dalloc(tsdn, arena, extent);\n\t\treturn NULL;\n\t}\n\textent_init(extent, arena, addr, esize, slab, szind,\n\t    arena_extent_sn_next(arena), extent_state_active, *zero, *commit,\n\t    true, EXTENT_NOT_HEAD);\n\tif (pad != 0) {\n\t\textent_addr_randomize(tsdn, extent, alignment);\n\t}\n\tif (extent_register(tsdn, extent)) {\n\t\textent_dalloc(tsdn, arena, extent);\n\t\treturn NULL;\n\t}\n\n\treturn extent;\n}\n\nextent_t *\nextent_alloc_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, void *new_addr, size_t size, size_t pad,\n    size_t alignment, bool slab, szind_t szind, bool *zero, bool *commit) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\textent_hooks_assure_initialized(arena, r_extent_hooks);\n\n\textent_t *extent = extent_alloc_retained(tsdn, arena, r_extent_hooks,\n\t    new_addr, size, pad, alignment, slab, szind, zero, commit);\n\tif (extent == NULL) {\n\t\tif (opt_retain && new_addr != NULL) {\n\t\t\t/*\n\t\t\t * When retain is enabled and new_addr is set, we do not\n\t\t\t * attempt extent_alloc_wrapper_hard which does mmap\n\t\t\t * that is very unlikely to succeed (unless it happens\n\t\t\t * to be at the end).\n\t\t\t */\n\t\t\treturn NULL;\n\t\t}\n\t\textent = extent_alloc_wrapper_hard(tsdn, arena, r_extent_hooks,\n\t\t    new_addr, size, pad, alignment, slab, szind, zero, commit);\n\t}\n\n\tassert(extent == NULL || extent_dumpable_get(extent));\n\treturn extent;\n}\n\nstatic bool\nextent_can_coalesce(arena_t *arena, extents_t *extents, const extent_t *inner,\n    const extent_t *outer) {\n\tassert(extent_arena_get(inner) == arena);\n\tif (extent_arena_get(outer) != arena) {\n\t\treturn false;\n\t}\n\n\tassert(extent_state_get(inner) == extent_state_active);\n\tif (extent_state_get(outer) != extents->state) {\n\t\treturn false;\n\t}\n\n\tif (extent_committed_get(inner) != extent_committed_get(outer)) {\n\t\treturn false;\n\t}\n\n\treturn true;\n}\n\nstatic bool\nextent_coalesce(tsdn_t *tsdn, arena_t *arena, extent_hooks_t **r_extent_hooks,\n    extents_t *extents, extent_t *inner, extent_t *outer, bool forward,\n    bool growing_retained) {\n\tassert(extent_can_coalesce(arena, extents, inner, outer));\n\n\textent_activate_locked(tsdn, arena, extents, outer);\n\n\tmalloc_mutex_unlock(tsdn, &extents->mtx);\n\tbool err = extent_merge_impl(tsdn, arena, r_extent_hooks,\n\t    forward ? inner : outer, forward ? outer : inner, growing_retained);\n\tmalloc_mutex_lock(tsdn, &extents->mtx);\n\n\tif (err) {\n\t\textent_deactivate_locked(tsdn, arena, extents, outer);\n\t}\n\n\treturn err;\n}\n\nstatic extent_t *\nextent_try_coalesce_impl(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, rtree_ctx_t *rtree_ctx, extents_t *extents,\n    extent_t *extent, bool *coalesced, bool growing_retained,\n    bool inactive_only) {\n\t/*\n\t * We avoid checking / locking inactive neighbors for large size\n\t * classes, since they are eagerly coalesced on deallocation which can\n\t * cause lock contention.\n\t */\n\t/*\n\t * Continue attempting to coalesce until failure, to protect against\n\t * races with other threads that are thwarted by this one.\n\t */\n\tbool again;\n\tdo {\n\t\tagain = false;\n\n\t\t/* Try to coalesce forward. */\n\t\textent_t *next = extent_lock_from_addr(tsdn, rtree_ctx,\n\t\t    extent_past_get(extent), inactive_only);\n\t\tif (next != NULL) {\n\t\t\t/*\n\t\t\t * extents->mtx only protects against races for\n\t\t\t * like-state extents, so call extent_can_coalesce()\n\t\t\t * before releasing next's pool lock.\n\t\t\t */\n\t\t\tbool can_coalesce = extent_can_coalesce(arena, extents,\n\t\t\t    extent, next);\n\n\t\t\textent_unlock(tsdn, next);\n\n\t\t\tif (can_coalesce && !extent_coalesce(tsdn, arena,\n\t\t\t    r_extent_hooks, extents, extent, next, true,\n\t\t\t    growing_retained)) {\n\t\t\t\tif (extents->delay_coalesce) {\n\t\t\t\t\t/* Do minimal coalescing. */\n\t\t\t\t\t*coalesced = true;\n\t\t\t\t\treturn extent;\n\t\t\t\t}\n\t\t\t\tagain = true;\n\t\t\t}\n\t\t}\n\n\t\t/* Try to coalesce backward. */\n\t\textent_t *prev = extent_lock_from_addr(tsdn, rtree_ctx,\n\t\t    extent_before_get(extent), inactive_only);\n\t\tif (prev != NULL) {\n\t\t\tbool can_coalesce = extent_can_coalesce(arena, extents,\n\t\t\t    extent, prev);\n\t\t\textent_unlock(tsdn, prev);\n\n\t\t\tif (can_coalesce && !extent_coalesce(tsdn, arena,\n\t\t\t    r_extent_hooks, extents, extent, prev, false,\n\t\t\t    growing_retained)) {\n\t\t\t\textent = prev;\n\t\t\t\tif (extents->delay_coalesce) {\n\t\t\t\t\t/* Do minimal coalescing. */\n\t\t\t\t\t*coalesced = true;\n\t\t\t\t\treturn extent;\n\t\t\t\t}\n\t\t\t\tagain = true;\n\t\t\t}\n\t\t}\n\t} while (again);\n\n\tif (extents->delay_coalesce) {\n\t\t*coalesced = false;\n\t}\n\treturn extent;\n}\n\nstatic extent_t *\nextent_try_coalesce(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, rtree_ctx_t *rtree_ctx, extents_t *extents,\n    extent_t *extent, bool *coalesced, bool growing_retained) {\n\treturn extent_try_coalesce_impl(tsdn, arena, r_extent_hooks, rtree_ctx,\n\t    extents, extent, coalesced, growing_retained, false);\n}\n\nstatic extent_t *\nextent_try_coalesce_large(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, rtree_ctx_t *rtree_ctx, extents_t *extents,\n    extent_t *extent, bool *coalesced, bool growing_retained) {\n\treturn extent_try_coalesce_impl(tsdn, arena, r_extent_hooks, rtree_ctx,\n\t    extents, extent, coalesced, growing_retained, true);\n}\n\n/*\n * Does the metadata management portions of putting an unused extent into the\n * given extents_t (coalesces, deregisters slab interiors, the heap operations).\n */\nstatic void\nextent_record(tsdn_t *tsdn, arena_t *arena, extent_hooks_t **r_extent_hooks,\n    extents_t *extents, extent_t *extent, bool growing_retained) {\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\n\tassert((extents_state_get(extents) != extent_state_dirty &&\n\t    extents_state_get(extents) != extent_state_muzzy) ||\n\t    !extent_zeroed_get(extent));\n\n\tmalloc_mutex_lock(tsdn, &extents->mtx);\n\textent_hooks_assure_initialized(arena, r_extent_hooks);\n\n\textent_szind_set(extent, SC_NSIZES);\n\tif (extent_slab_get(extent)) {\n\t\textent_interior_deregister(tsdn, rtree_ctx, extent);\n\t\textent_slab_set(extent, false);\n\t}\n\n\tassert(rtree_extent_read(tsdn, &extents_rtree, rtree_ctx,\n\t    (uintptr_t)extent_base_get(extent), true) == extent);\n\n\tif (!extents->delay_coalesce) {\n\t\textent = extent_try_coalesce(tsdn, arena, r_extent_hooks,\n\t\t    rtree_ctx, extents, extent, NULL, growing_retained);\n\t} else if (extent_size_get(extent) >= SC_LARGE_MINCLASS) {\n\t\tassert(extents == &arena->extents_dirty);\n\t\t/* Always coalesce large extents eagerly. */\n\t\tbool coalesced;\n\t\tdo {\n\t\t\tassert(extent_state_get(extent) == extent_state_active);\n\t\t\textent = extent_try_coalesce_large(tsdn, arena,\n\t\t\t    r_extent_hooks, rtree_ctx, extents, extent,\n\t\t\t    &coalesced, growing_retained);\n\t\t} while (coalesced);\n\t\tif (extent_size_get(extent) >= oversize_threshold) {\n\t\t\t/* Shortcut to purge the oversize extent eagerly. */\n\t\t\tmalloc_mutex_unlock(tsdn, &extents->mtx);\n\t\t\tarena_decay_extent(tsdn, arena, r_extent_hooks, extent);\n\t\t\treturn;\n\t\t}\n\t}\n\textent_deactivate_locked(tsdn, arena, extents, extent);\n\n\tmalloc_mutex_unlock(tsdn, &extents->mtx);\n}\n\nvoid\nextent_dalloc_gap(tsdn_t *tsdn, arena_t *arena, extent_t *extent) {\n\textent_hooks_t *extent_hooks = EXTENT_HOOKS_INITIALIZER;\n\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\tif (extent_register(tsdn, extent)) {\n\t\textent_dalloc(tsdn, arena, extent);\n\t\treturn;\n\t}\n\textent_dalloc_wrapper(tsdn, arena, &extent_hooks, extent);\n}\n\nstatic bool\nextent_may_dalloc(void) {\n\t/* With retain enabled, the default dalloc always fails. */\n\treturn !opt_retain;\n}\n\nstatic bool\nextent_dalloc_default_impl(void *addr, size_t size) {\n\tif (!have_dss || !extent_in_dss(addr)) {\n\t\treturn extent_dalloc_mmap(addr, size);\n\t}\n\treturn true;\n}\n\nstatic bool\nextent_dalloc_default(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    bool committed, unsigned arena_ind) {\n\treturn extent_dalloc_default_impl(addr, size);\n}\n\nstatic bool\nextent_dalloc_wrapper_try(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent) {\n\tbool err;\n\n\tassert(extent_base_get(extent) != NULL);\n\tassert(extent_size_get(extent) != 0);\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\textent_addr_set(extent, extent_base_get(extent));\n\n\textent_hooks_assure_initialized(arena, r_extent_hooks);\n\t/* Try to deallocate. */\n\tif (*r_extent_hooks == &extent_hooks_default) {\n\t\t/* Call directly to propagate tsdn. */\n\t\terr = extent_dalloc_default_impl(extent_base_get(extent),\n\t\t    extent_size_get(extent));\n\t} else {\n\t\textent_hook_pre_reentrancy(tsdn, arena);\n\t\terr = ((*r_extent_hooks)->dalloc == NULL ||\n\t\t    (*r_extent_hooks)->dalloc(*r_extent_hooks,\n\t\t    extent_base_get(extent), extent_size_get(extent),\n\t\t    extent_committed_get(extent), arena_ind_get(arena)));\n\t\textent_hook_post_reentrancy(tsdn);\n\t}\n\n\tif (!err) {\n\t\textent_dalloc(tsdn, arena, extent);\n\t}\n\n\treturn err;\n}\n\nvoid\nextent_dalloc_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent) {\n\tassert(extent_dumpable_get(extent));\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\t/* Avoid calling the default extent_dalloc unless have to. */\n\tif (*r_extent_hooks != &extent_hooks_default || extent_may_dalloc()) {\n\t\t/*\n\t\t * Deregister first to avoid a race with other allocating\n\t\t * threads, and reregister if deallocation fails.\n\t\t */\n\t\textent_deregister(tsdn, extent);\n\t\tif (!extent_dalloc_wrapper_try(tsdn, arena, r_extent_hooks,\n\t\t    extent)) {\n\t\t\treturn;\n\t\t}\n\t\textent_reregister(tsdn, extent);\n\t}\n\n\tif (*r_extent_hooks != &extent_hooks_default) {\n\t\textent_hook_pre_reentrancy(tsdn, arena);\n\t}\n\t/* Try to decommit; purge if that fails. */\n\tbool zeroed;\n\tif (!extent_committed_get(extent)) {\n\t\tzeroed = true;\n\t} else if (!extent_decommit_wrapper(tsdn, arena, r_extent_hooks, extent,\n\t    0, extent_size_get(extent))) {\n\t\tzeroed = true;\n\t} else if ((*r_extent_hooks)->purge_forced != NULL &&\n\t    !(*r_extent_hooks)->purge_forced(*r_extent_hooks,\n\t    extent_base_get(extent), extent_size_get(extent), 0,\n\t    extent_size_get(extent), arena_ind_get(arena))) {\n\t\tzeroed = true;\n\t} else if (extent_state_get(extent) == extent_state_muzzy ||\n\t    ((*r_extent_hooks)->purge_lazy != NULL &&\n\t    !(*r_extent_hooks)->purge_lazy(*r_extent_hooks,\n\t    extent_base_get(extent), extent_size_get(extent), 0,\n\t    extent_size_get(extent), arena_ind_get(arena)))) {\n\t\tzeroed = false;\n\t} else {\n\t\tzeroed = false;\n\t}\n\tif (*r_extent_hooks != &extent_hooks_default) {\n\t\textent_hook_post_reentrancy(tsdn);\n\t}\n\textent_zeroed_set(extent, zeroed);\n\n\tif (config_prof) {\n\t\textent_gdump_sub(tsdn, extent);\n\t}\n\n\textent_record(tsdn, arena, r_extent_hooks, &arena->extents_retained,\n\t    extent, false);\n}\n\nstatic void\nextent_destroy_default_impl(void *addr, size_t size) {\n\tif (!have_dss || !extent_in_dss(addr)) {\n\t\tpages_unmap(addr, size);\n\t}\n}\n\nstatic void\nextent_destroy_default(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    bool committed, unsigned arena_ind) {\n\textent_destroy_default_impl(addr, size);\n}\n\nvoid\nextent_destroy_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent) {\n\tassert(extent_base_get(extent) != NULL);\n\tassert(extent_size_get(extent) != 0);\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\t/* Deregister first to avoid a race with other allocating threads. */\n\textent_deregister(tsdn, extent);\n\n\textent_addr_set(extent, extent_base_get(extent));\n\n\textent_hooks_assure_initialized(arena, r_extent_hooks);\n\t/* Try to destroy; silently fail otherwise. */\n\tif (*r_extent_hooks == &extent_hooks_default) {\n\t\t/* Call directly to propagate tsdn. */\n\t\textent_destroy_default_impl(extent_base_get(extent),\n\t\t    extent_size_get(extent));\n\t} else if ((*r_extent_hooks)->destroy != NULL) {\n\t\textent_hook_pre_reentrancy(tsdn, arena);\n\t\t(*r_extent_hooks)->destroy(*r_extent_hooks,\n\t\t    extent_base_get(extent), extent_size_get(extent),\n\t\t    extent_committed_get(extent), arena_ind_get(arena));\n\t\textent_hook_post_reentrancy(tsdn);\n\t}\n\n\textent_dalloc(tsdn, arena, extent);\n}\n\nstatic bool\nextent_commit_default(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    size_t offset, size_t length, unsigned arena_ind) {\n\treturn pages_commit((void *)((uintptr_t)addr + (uintptr_t)offset),\n\t    length);\n}\n\nstatic bool\nextent_commit_impl(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length, bool growing_retained) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, growing_retained ? 1 : 0);\n\n\textent_hooks_assure_initialized(arena, r_extent_hooks);\n\tif (*r_extent_hooks != &extent_hooks_default) {\n\t\textent_hook_pre_reentrancy(tsdn, arena);\n\t}\n\tbool err = ((*r_extent_hooks)->commit == NULL ||\n\t    (*r_extent_hooks)->commit(*r_extent_hooks, extent_base_get(extent),\n\t    extent_size_get(extent), offset, length, arena_ind_get(arena)));\n\tif (*r_extent_hooks != &extent_hooks_default) {\n\t\textent_hook_post_reentrancy(tsdn);\n\t}\n\textent_committed_set(extent, extent_committed_get(extent) || !err);\n\treturn err;\n}\n\nbool\nextent_commit_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length) {\n\treturn extent_commit_impl(tsdn, arena, r_extent_hooks, extent, offset,\n\t    length, false);\n}\n\nstatic bool\nextent_decommit_default(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    size_t offset, size_t length, unsigned arena_ind) {\n\treturn pages_decommit((void *)((uintptr_t)addr + (uintptr_t)offset),\n\t    length);\n}\n\nbool\nextent_decommit_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\textent_hooks_assure_initialized(arena, r_extent_hooks);\n\n\tif (*r_extent_hooks != &extent_hooks_default) {\n\t\textent_hook_pre_reentrancy(tsdn, arena);\n\t}\n\tbool err = ((*r_extent_hooks)->decommit == NULL ||\n\t    (*r_extent_hooks)->decommit(*r_extent_hooks,\n\t    extent_base_get(extent), extent_size_get(extent), offset, length,\n\t    arena_ind_get(arena)));\n\tif (*r_extent_hooks != &extent_hooks_default) {\n\t\textent_hook_post_reentrancy(tsdn);\n\t}\n\textent_committed_set(extent, extent_committed_get(extent) && err);\n\treturn err;\n}\n\n#ifdef PAGES_CAN_PURGE_LAZY\nstatic bool\nextent_purge_lazy_default(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    size_t offset, size_t length, unsigned arena_ind) {\n\tassert(addr != NULL);\n\tassert((offset & PAGE_MASK) == 0);\n\tassert(length != 0);\n\tassert((length & PAGE_MASK) == 0);\n\n\treturn pages_purge_lazy((void *)((uintptr_t)addr + (uintptr_t)offset),\n\t    length);\n}\n#endif\n\nstatic bool\nextent_purge_lazy_impl(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length, bool growing_retained) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, growing_retained ? 1 : 0);\n\n\textent_hooks_assure_initialized(arena, r_extent_hooks);\n\n\tif ((*r_extent_hooks)->purge_lazy == NULL) {\n\t\treturn true;\n\t}\n\tif (*r_extent_hooks != &extent_hooks_default) {\n\t\textent_hook_pre_reentrancy(tsdn, arena);\n\t}\n\tbool err = (*r_extent_hooks)->purge_lazy(*r_extent_hooks,\n\t    extent_base_get(extent), extent_size_get(extent), offset, length,\n\t    arena_ind_get(arena));\n\tif (*r_extent_hooks != &extent_hooks_default) {\n\t\textent_hook_post_reentrancy(tsdn);\n\t}\n\n\treturn err;\n}\n\nbool\nextent_purge_lazy_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length) {\n\treturn extent_purge_lazy_impl(tsdn, arena, r_extent_hooks, extent,\n\t    offset, length, false);\n}\n\n#ifdef PAGES_CAN_PURGE_FORCED\nstatic bool\nextent_purge_forced_default(extent_hooks_t *extent_hooks, void *addr,\n    size_t size, size_t offset, size_t length, unsigned arena_ind) {\n\tassert(addr != NULL);\n\tassert((offset & PAGE_MASK) == 0);\n\tassert(length != 0);\n\tassert((length & PAGE_MASK) == 0);\n\n\treturn pages_purge_forced((void *)((uintptr_t)addr +\n\t    (uintptr_t)offset), length);\n}\n#endif\n\nstatic bool\nextent_purge_forced_impl(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length, bool growing_retained) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, growing_retained ? 1 : 0);\n\n\textent_hooks_assure_initialized(arena, r_extent_hooks);\n\n\tif ((*r_extent_hooks)->purge_forced == NULL) {\n\t\treturn true;\n\t}\n\tif (*r_extent_hooks != &extent_hooks_default) {\n\t\textent_hook_pre_reentrancy(tsdn, arena);\n\t}\n\tbool err = (*r_extent_hooks)->purge_forced(*r_extent_hooks,\n\t    extent_base_get(extent), extent_size_get(extent), offset, length,\n\t    arena_ind_get(arena));\n\tif (*r_extent_hooks != &extent_hooks_default) {\n\t\textent_hook_post_reentrancy(tsdn);\n\t}\n\treturn err;\n}\n\nbool\nextent_purge_forced_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length) {\n\treturn extent_purge_forced_impl(tsdn, arena, r_extent_hooks, extent,\n\t    offset, length, false);\n}\n\nstatic bool\nextent_split_default(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    size_t size_a, size_t size_b, bool committed, unsigned arena_ind) {\n\tif (!maps_coalesce) {\n\t\t/*\n\t\t * Without retain, only whole regions can be purged (required by\n\t\t * MEM_RELEASE on Windows) -- therefore disallow splitting.  See\n\t\t * comments in extent_head_no_merge().\n\t\t */\n\t\treturn !opt_retain;\n\t}\n\n\treturn false;\n}\n\n/*\n * Accepts the extent to split, and the characteristics of each side of the\n * split.  The 'a' parameters go with the 'lead' of the resulting pair of\n * extents (the lower addressed portion of the split), and the 'b' parameters go\n * with the trail (the higher addressed portion).  This makes 'extent' the lead,\n * and returns the trail (except in case of error).\n */\nstatic extent_t *\nextent_split_impl(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t size_a,\n    szind_t szind_a, bool slab_a, size_t size_b, szind_t szind_b, bool slab_b,\n    bool growing_retained) {\n\tassert(extent_size_get(extent) == size_a + size_b);\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, growing_retained ? 1 : 0);\n\n\textent_hooks_assure_initialized(arena, r_extent_hooks);\n\n\tif ((*r_extent_hooks)->split == NULL) {\n\t\treturn NULL;\n\t}\n\n\textent_t *trail = extent_alloc(tsdn, arena);\n\tif (trail == NULL) {\n\t\tgoto label_error_a;\n\t}\n\n\textent_init(trail, arena, (void *)((uintptr_t)extent_base_get(extent) +\n\t    size_a), size_b, slab_b, szind_b, extent_sn_get(extent),\n\t    extent_state_get(extent), extent_zeroed_get(extent),\n\t    extent_committed_get(extent), extent_dumpable_get(extent),\n\t    EXTENT_NOT_HEAD);\n\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\trtree_leaf_elm_t *lead_elm_a, *lead_elm_b;\n\t{\n\t\textent_t lead;\n\n\t\textent_init(&lead, arena, extent_addr_get(extent), size_a,\n\t\t    slab_a, szind_a, extent_sn_get(extent),\n\t\t    extent_state_get(extent), extent_zeroed_get(extent),\n\t\t    extent_committed_get(extent), extent_dumpable_get(extent),\n\t\t    EXTENT_NOT_HEAD);\n\n\t\textent_rtree_leaf_elms_lookup(tsdn, rtree_ctx, &lead, false,\n\t\t    true, &lead_elm_a, &lead_elm_b);\n\t}\n\trtree_leaf_elm_t *trail_elm_a, *trail_elm_b;\n\textent_rtree_leaf_elms_lookup(tsdn, rtree_ctx, trail, false, true,\n\t    &trail_elm_a, &trail_elm_b);\n\n\tif (lead_elm_a == NULL || lead_elm_b == NULL || trail_elm_a == NULL\n\t    || trail_elm_b == NULL) {\n\t\tgoto label_error_b;\n\t}\n\n\textent_lock2(tsdn, extent, trail);\n\n\tif (*r_extent_hooks != &extent_hooks_default) {\n\t\textent_hook_pre_reentrancy(tsdn, arena);\n\t}\n\tbool err = (*r_extent_hooks)->split(*r_extent_hooks, extent_base_get(extent),\n\t    size_a + size_b, size_a, size_b, extent_committed_get(extent),\n\t    arena_ind_get(arena));\n\tif (*r_extent_hooks != &extent_hooks_default) {\n\t\textent_hook_post_reentrancy(tsdn);\n\t}\n\tif (err) {\n\t\tgoto label_error_c;\n\t}\n\n\textent_size_set(extent, size_a);\n\textent_szind_set(extent, szind_a);\n\n\textent_rtree_write_acquired(tsdn, lead_elm_a, lead_elm_b, extent,\n\t    szind_a, slab_a);\n\textent_rtree_write_acquired(tsdn, trail_elm_a, trail_elm_b, trail,\n\t    szind_b, slab_b);\n\n\textent_unlock2(tsdn, extent, trail);\n\n\treturn trail;\nlabel_error_c:\n\textent_unlock2(tsdn, extent, trail);\nlabel_error_b:\n\textent_dalloc(tsdn, arena, trail);\nlabel_error_a:\n\treturn NULL;\n}\n\nextent_t *\nextent_split_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t size_a,\n    szind_t szind_a, bool slab_a, size_t size_b, szind_t szind_b, bool slab_b) {\n\treturn extent_split_impl(tsdn, arena, r_extent_hooks, extent, size_a,\n\t    szind_a, slab_a, size_b, szind_b, slab_b, false);\n}\n\nstatic bool\nextent_merge_default_impl(void *addr_a, void *addr_b) {\n\tif (!maps_coalesce && !opt_retain) {\n\t\treturn true;\n\t}\n\tif (have_dss && !extent_dss_mergeable(addr_a, addr_b)) {\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\n/*\n * Returns true if the given extents can't be merged because of their head bit\n * settings.  Assumes the second extent has the higher address.\n */\nstatic bool\nextent_head_no_merge(extent_t *a, extent_t *b) {\n\tassert(extent_base_get(a) < extent_base_get(b));\n\t/*\n\t * When coalesce is not always allowed (Windows), only merge extents\n\t * from the same VirtualAlloc region under opt.retain (in which case\n\t * MEM_DECOMMIT is utilized for purging).\n\t */\n\tif (maps_coalesce) {\n\t\treturn false;\n\t}\n\tif (!opt_retain) {\n\t\treturn true;\n\t}\n\t/* If b is a head extent, disallow the cross-region merge. */\n\tif (extent_is_head_get(b)) {\n\t\t/*\n\t\t * Additionally, sn should not overflow with retain; sanity\n\t\t * check that different regions have unique sn.\n\t\t */\n\t\tassert(extent_sn_comp(a, b) != 0);\n\t\treturn true;\n\t}\n\tassert(extent_sn_comp(a, b) == 0);\n\n\treturn false;\n}\n\nstatic bool\nextent_merge_default(extent_hooks_t *extent_hooks, void *addr_a, size_t size_a,\n    void *addr_b, size_t size_b, bool committed, unsigned arena_ind) {\n\tif (!maps_coalesce) {\n\t\ttsdn_t *tsdn = tsdn_fetch();\n\t\textent_t *a = iealloc(tsdn, addr_a);\n\t\textent_t *b = iealloc(tsdn, addr_b);\n\t\tif (extent_head_no_merge(a, b)) {\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn extent_merge_default_impl(addr_a, addr_b);\n}\n\nstatic bool\nextent_merge_impl(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *a, extent_t *b,\n    bool growing_retained) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, growing_retained ? 1 : 0);\n\tassert(extent_base_get(a) < extent_base_get(b));\n\n\textent_hooks_assure_initialized(arena, r_extent_hooks);\n\n\tif ((*r_extent_hooks)->merge == NULL || extent_head_no_merge(a, b)) {\n\t\treturn true;\n\t}\n\n\tbool err;\n\tif (*r_extent_hooks == &extent_hooks_default) {\n\t\t/* Call directly to propagate tsdn. */\n\t\terr = extent_merge_default_impl(extent_base_get(a),\n\t\t    extent_base_get(b));\n\t} else {\n\t\textent_hook_pre_reentrancy(tsdn, arena);\n\t\terr = (*r_extent_hooks)->merge(*r_extent_hooks,\n\t\t    extent_base_get(a), extent_size_get(a), extent_base_get(b),\n\t\t    extent_size_get(b), extent_committed_get(a),\n\t\t    arena_ind_get(arena));\n\t\textent_hook_post_reentrancy(tsdn);\n\t}\n\n\tif (err) {\n\t\treturn true;\n\t}\n\n\t/*\n\t * The rtree writes must happen while all the relevant elements are\n\t * owned, so the following code uses decomposed helper functions rather\n\t * than extent_{,de}register() to do things in the right order.\n\t */\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\trtree_leaf_elm_t *a_elm_a, *a_elm_b, *b_elm_a, *b_elm_b;\n\textent_rtree_leaf_elms_lookup(tsdn, rtree_ctx, a, true, false, &a_elm_a,\n\t    &a_elm_b);\n\textent_rtree_leaf_elms_lookup(tsdn, rtree_ctx, b, true, false, &b_elm_a,\n\t    &b_elm_b);\n\n\textent_lock2(tsdn, a, b);\n\n\tif (a_elm_b != NULL) {\n\t\trtree_leaf_elm_write(tsdn, &extents_rtree, a_elm_b, NULL,\n\t\t    SC_NSIZES, false);\n\t}\n\tif (b_elm_b != NULL) {\n\t\trtree_leaf_elm_write(tsdn, &extents_rtree, b_elm_a, NULL,\n\t\t    SC_NSIZES, false);\n\t} else {\n\t\tb_elm_b = b_elm_a;\n\t}\n\n\textent_size_set(a, extent_size_get(a) + extent_size_get(b));\n\textent_szind_set(a, SC_NSIZES);\n\textent_sn_set(a, (extent_sn_get(a) < extent_sn_get(b)) ?\n\t    extent_sn_get(a) : extent_sn_get(b));\n\textent_zeroed_set(a, extent_zeroed_get(a) && extent_zeroed_get(b));\n\n\textent_rtree_write_acquired(tsdn, a_elm_a, b_elm_b, a, SC_NSIZES,\n\t    false);\n\n\textent_unlock2(tsdn, a, b);\n\n\textent_dalloc(tsdn, extent_arena_get(b), b);\n\n\treturn false;\n}\n\nbool\nextent_merge_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *a, extent_t *b) {\n\treturn extent_merge_impl(tsdn, arena, r_extent_hooks, a, b, false);\n}\n\nbool\nextent_boot(void) {\n\tif (rtree_new(&extents_rtree, true)) {\n\t\treturn true;\n\t}\n\n\tif (mutex_pool_init(&extent_mutex_pool, \"extent_mutex_pool\",\n\t    WITNESS_RANK_EXTENT_POOL)) {\n\t\treturn true;\n\t}\n\n\tif (have_dss) {\n\t\textent_dss_boot();\n\t}\n\n\treturn false;\n}\n\nvoid\nextent_util_stats_get(tsdn_t *tsdn, const void *ptr,\n    size_t *nfree, size_t *nregs, size_t *size) {\n\tassert(ptr != NULL && nfree != NULL && nregs != NULL && size != NULL);\n\n\tconst extent_t *extent = iealloc(tsdn, ptr);\n\tif (unlikely(extent == NULL)) {\n\t\t*nfree = *nregs = *size = 0;\n\t\treturn;\n\t}\n\n\t*size = extent_size_get(extent);\n\tif (!extent_slab_get(extent)) {\n\t\t*nfree = 0;\n\t\t*nregs = 1;\n\t} else {\n\t\t*nfree = extent_nfree_get(extent);\n\t\t*nregs = bin_infos[extent_szind_get(extent)].nregs;\n\t\tassert(*nfree <= *nregs);\n\t\tassert(*nfree * extent_usize_get(extent) <= *size);\n\t}\n}\n\nvoid\nextent_util_stats_verbose_get(tsdn_t *tsdn, const void *ptr,\n    size_t *nfree, size_t *nregs, size_t *size,\n    size_t *bin_nfree, size_t *bin_nregs, void **slabcur_addr) {\n\tassert(ptr != NULL && nfree != NULL && nregs != NULL && size != NULL\n\t    && bin_nfree != NULL && bin_nregs != NULL && slabcur_addr != NULL);\n\n\tconst extent_t *extent = iealloc(tsdn, ptr);\n\tif (unlikely(extent == NULL)) {\n\t\t*nfree = *nregs = *size = *bin_nfree = *bin_nregs = 0;\n\t\t*slabcur_addr = NULL;\n\t\treturn;\n\t}\n\n\t*size = extent_size_get(extent);\n\tif (!extent_slab_get(extent)) {\n\t\t*nfree = *bin_nfree = *bin_nregs = 0;\n\t\t*nregs = 1;\n\t\t*slabcur_addr = NULL;\n\t\treturn;\n\t}\n\n\t*nfree = extent_nfree_get(extent);\n\tconst szind_t szind = extent_szind_get(extent);\n\t*nregs = bin_infos[szind].nregs;\n\tassert(*nfree <= *nregs);\n\tassert(*nfree * extent_usize_get(extent) <= *size);\n\n\tconst arena_t *arena = extent_arena_get(extent);\n\tassert(arena != NULL);\n\tconst unsigned binshard = extent_binshard_get(extent);\n\tbin_t *bin = &arena->bins[szind].bin_shards[binshard];\n\n\tmalloc_mutex_lock(tsdn, &bin->lock);\n\tif (config_stats) {\n\t\t*bin_nregs = *nregs * bin->stats.curslabs;\n\t\tassert(*bin_nregs >= bin->stats.curregs);\n\t\t*bin_nfree = *bin_nregs - bin->stats.curregs;\n\t} else {\n\t\t*bin_nfree = *bin_nregs = 0;\n\t}\n\t*slabcur_addr = extent_addr_get(bin->slabcur);\n\tassert(*slabcur_addr != NULL);\n\tmalloc_mutex_unlock(tsdn, &bin->lock);\n}\n"
  },
  {
    "path": "deps/jemalloc/src/extent_dss.c",
    "content": "#define JEMALLOC_EXTENT_DSS_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/extent_dss.h\"\n#include \"jemalloc/internal/spin.h\"\n\n/******************************************************************************/\n/* Data. */\n\nconst char\t*opt_dss = DSS_DEFAULT;\n\nconst char\t*dss_prec_names[] = {\n\t\"disabled\",\n\t\"primary\",\n\t\"secondary\",\n\t\"N/A\"\n};\n\n/*\n * Current dss precedence default, used when creating new arenas.  NB: This is\n * stored as unsigned rather than dss_prec_t because in principle there's no\n * guarantee that sizeof(dss_prec_t) is the same as sizeof(unsigned), and we use\n * atomic operations to synchronize the setting.\n */\nstatic atomic_u_t\tdss_prec_default = ATOMIC_INIT(\n    (unsigned)DSS_PREC_DEFAULT);\n\n/* Base address of the DSS. */\nstatic void\t\t*dss_base;\n/* Atomic boolean indicating whether a thread is currently extending DSS. */\nstatic atomic_b_t\tdss_extending;\n/* Atomic boolean indicating whether the DSS is exhausted. */\nstatic atomic_b_t\tdss_exhausted;\n/* Atomic current upper limit on DSS addresses. */\nstatic atomic_p_t\tdss_max;\n\n/******************************************************************************/\n\nstatic void *\nextent_dss_sbrk(intptr_t increment) {\n#ifdef JEMALLOC_DSS\n\treturn sbrk(increment);\n#else\n\tnot_implemented();\n\treturn NULL;\n#endif\n}\n\ndss_prec_t\nextent_dss_prec_get(void) {\n\tdss_prec_t ret;\n\n\tif (!have_dss) {\n\t\treturn dss_prec_disabled;\n\t}\n\tret = (dss_prec_t)atomic_load_u(&dss_prec_default, ATOMIC_ACQUIRE);\n\treturn ret;\n}\n\nbool\nextent_dss_prec_set(dss_prec_t dss_prec) {\n\tif (!have_dss) {\n\t\treturn (dss_prec != dss_prec_disabled);\n\t}\n\tatomic_store_u(&dss_prec_default, (unsigned)dss_prec, ATOMIC_RELEASE);\n\treturn false;\n}\n\nstatic void\nextent_dss_extending_start(void) {\n\tspin_t spinner = SPIN_INITIALIZER;\n\twhile (true) {\n\t\tbool expected = false;\n\t\tif (atomic_compare_exchange_weak_b(&dss_extending, &expected,\n\t\t    true, ATOMIC_ACQ_REL, ATOMIC_RELAXED)) {\n\t\t\tbreak;\n\t\t}\n\t\tspin_adaptive(&spinner);\n\t}\n}\n\nstatic void\nextent_dss_extending_finish(void) {\n\tassert(atomic_load_b(&dss_extending, ATOMIC_RELAXED));\n\n\tatomic_store_b(&dss_extending, false, ATOMIC_RELEASE);\n}\n\nstatic void *\nextent_dss_max_update(void *new_addr) {\n\t/*\n\t * Get the current end of the DSS as max_cur and assure that dss_max is\n\t * up to date.\n\t */\n\tvoid *max_cur = extent_dss_sbrk(0);\n\tif (max_cur == (void *)-1) {\n\t\treturn NULL;\n\t}\n\tatomic_store_p(&dss_max, max_cur, ATOMIC_RELEASE);\n\t/* Fixed new_addr can only be supported if it is at the edge of DSS. */\n\tif (new_addr != NULL && max_cur != new_addr) {\n\t\treturn NULL;\n\t}\n\treturn max_cur;\n}\n\nvoid *\nextent_alloc_dss(tsdn_t *tsdn, arena_t *arena, void *new_addr, size_t size,\n    size_t alignment, bool *zero, bool *commit) {\n\textent_t *gap;\n\n\tcassert(have_dss);\n\tassert(size > 0);\n\tassert(alignment == ALIGNMENT_CEILING(alignment, PAGE));\n\n\t/*\n\t * sbrk() uses a signed increment argument, so take care not to\n\t * interpret a large allocation request as a negative increment.\n\t */\n\tif ((intptr_t)size < 0) {\n\t\treturn NULL;\n\t}\n\n\tgap = extent_alloc(tsdn, arena);\n\tif (gap == NULL) {\n\t\treturn NULL;\n\t}\n\n\textent_dss_extending_start();\n\tif (!atomic_load_b(&dss_exhausted, ATOMIC_ACQUIRE)) {\n\t\t/*\n\t\t * The loop is necessary to recover from races with other\n\t\t * threads that are using the DSS for something other than\n\t\t * malloc.\n\t\t */\n\t\twhile (true) {\n\t\t\tvoid *max_cur = extent_dss_max_update(new_addr);\n\t\t\tif (max_cur == NULL) {\n\t\t\t\tgoto label_oom;\n\t\t\t}\n\n\t\t\t/*\n\t\t\t * Compute how much page-aligned gap space (if any) is\n\t\t\t * necessary to satisfy alignment.  This space can be\n\t\t\t * recycled for later use.\n\t\t\t */\n\t\t\tvoid *gap_addr_page = (void *)(PAGE_CEILING(\n\t\t\t    (uintptr_t)max_cur));\n\t\t\tvoid *ret = (void *)ALIGNMENT_CEILING(\n\t\t\t    (uintptr_t)gap_addr_page, alignment);\n\t\t\tsize_t gap_size_page = (uintptr_t)ret -\n\t\t\t    (uintptr_t)gap_addr_page;\n\t\t\tif (gap_size_page != 0) {\n\t\t\t\textent_init(gap, arena, gap_addr_page,\n\t\t\t\t    gap_size_page, false, SC_NSIZES,\n\t\t\t\t    arena_extent_sn_next(arena),\n\t\t\t\t    extent_state_active, false, true, true,\n\t\t\t\t    EXTENT_NOT_HEAD);\n\t\t\t}\n\t\t\t/*\n\t\t\t * Compute the address just past the end of the desired\n\t\t\t * allocation space.\n\t\t\t */\n\t\t\tvoid *dss_next = (void *)((uintptr_t)ret + size);\n\t\t\tif ((uintptr_t)ret < (uintptr_t)max_cur ||\n\t\t\t    (uintptr_t)dss_next < (uintptr_t)max_cur) {\n\t\t\t\tgoto label_oom; /* Wrap-around. */\n\t\t\t}\n\t\t\t/* Compute the increment, including subpage bytes. */\n\t\t\tvoid *gap_addr_subpage = max_cur;\n\t\t\tsize_t gap_size_subpage = (uintptr_t)ret -\n\t\t\t    (uintptr_t)gap_addr_subpage;\n\t\t\tintptr_t incr = gap_size_subpage + size;\n\n\t\t\tassert((uintptr_t)max_cur + incr == (uintptr_t)ret +\n\t\t\t    size);\n\n\t\t\t/* Try to allocate. */\n\t\t\tvoid *dss_prev = extent_dss_sbrk(incr);\n\t\t\tif (dss_prev == max_cur) {\n\t\t\t\t/* Success. */\n\t\t\t\tatomic_store_p(&dss_max, dss_next,\n\t\t\t\t    ATOMIC_RELEASE);\n\t\t\t\textent_dss_extending_finish();\n\n\t\t\t\tif (gap_size_page != 0) {\n\t\t\t\t\textent_dalloc_gap(tsdn, arena, gap);\n\t\t\t\t} else {\n\t\t\t\t\textent_dalloc(tsdn, arena, gap);\n\t\t\t\t}\n\t\t\t\tif (!*commit) {\n\t\t\t\t\t*commit = pages_decommit(ret, size);\n\t\t\t\t}\n\t\t\t\tif (*zero && *commit) {\n\t\t\t\t\textent_hooks_t *extent_hooks =\n\t\t\t\t\t    EXTENT_HOOKS_INITIALIZER;\n\t\t\t\t\textent_t extent;\n\n\t\t\t\t\textent_init(&extent, arena, ret, size,\n\t\t\t\t\t    size, false, SC_NSIZES,\n\t\t\t\t\t    extent_state_active, false, true,\n\t\t\t\t\t    true, EXTENT_NOT_HEAD);\n\t\t\t\t\tif (extent_purge_forced_wrapper(tsdn,\n\t\t\t\t\t    arena, &extent_hooks, &extent, 0,\n\t\t\t\t\t    size)) {\n\t\t\t\t\t\tmemset(ret, 0, size);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn ret;\n\t\t\t}\n\t\t\t/*\n\t\t\t * Failure, whether due to OOM or a race with a raw\n\t\t\t * sbrk() call from outside the allocator.\n\t\t\t */\n\t\t\tif (dss_prev == (void *)-1) {\n\t\t\t\t/* OOM. */\n\t\t\t\tatomic_store_b(&dss_exhausted, true,\n\t\t\t\t    ATOMIC_RELEASE);\n\t\t\t\tgoto label_oom;\n\t\t\t}\n\t\t}\n\t}\nlabel_oom:\n\textent_dss_extending_finish();\n\textent_dalloc(tsdn, arena, gap);\n\treturn NULL;\n}\n\nstatic bool\nextent_in_dss_helper(void *addr, void *max) {\n\treturn ((uintptr_t)addr >= (uintptr_t)dss_base && (uintptr_t)addr <\n\t    (uintptr_t)max);\n}\n\nbool\nextent_in_dss(void *addr) {\n\tcassert(have_dss);\n\n\treturn extent_in_dss_helper(addr, atomic_load_p(&dss_max,\n\t    ATOMIC_ACQUIRE));\n}\n\nbool\nextent_dss_mergeable(void *addr_a, void *addr_b) {\n\tvoid *max;\n\n\tcassert(have_dss);\n\n\tif ((uintptr_t)addr_a < (uintptr_t)dss_base && (uintptr_t)addr_b <\n\t    (uintptr_t)dss_base) {\n\t\treturn true;\n\t}\n\n\tmax = atomic_load_p(&dss_max, ATOMIC_ACQUIRE);\n\treturn (extent_in_dss_helper(addr_a, max) ==\n\t    extent_in_dss_helper(addr_b, max));\n}\n\nvoid\nextent_dss_boot(void) {\n\tcassert(have_dss);\n\n\tdss_base = extent_dss_sbrk(0);\n\tatomic_store_b(&dss_extending, false, ATOMIC_RELAXED);\n\tatomic_store_b(&dss_exhausted, dss_base == (void *)-1, ATOMIC_RELAXED);\n\tatomic_store_p(&dss_max, dss_base, ATOMIC_RELAXED);\n}\n\n/******************************************************************************/\n"
  },
  {
    "path": "deps/jemalloc/src/extent_mmap.c",
    "content": "#define JEMALLOC_EXTENT_MMAP_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/extent_mmap.h\"\n\n/******************************************************************************/\n/* Data. */\n\nbool\topt_retain =\n#ifdef JEMALLOC_RETAIN\n    true\n#else\n    false\n#endif\n    ;\n\n/******************************************************************************/\n\nvoid *\nextent_alloc_mmap(void *new_addr, size_t size, size_t alignment, bool *zero,\n    bool *commit) {\n\tassert(alignment == ALIGNMENT_CEILING(alignment, PAGE));\n\tvoid *ret = pages_map(new_addr, size, alignment, commit);\n\tif (ret == NULL) {\n\t\treturn NULL;\n\t}\n\tassert(ret != NULL);\n\tif (*commit) {\n\t\t*zero = true;\n\t}\n\treturn ret;\n}\n\nbool\nextent_dalloc_mmap(void *addr, size_t size) {\n\tif (!opt_retain) {\n\t\tpages_unmap(addr, size);\n\t}\n\treturn opt_retain;\n}\n"
  },
  {
    "path": "deps/jemalloc/src/hash.c",
    "content": "#define JEMALLOC_HASH_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n"
  },
  {
    "path": "deps/jemalloc/src/hook.c",
    "content": "#include \"jemalloc/internal/jemalloc_preamble.h\"\n\n#include \"jemalloc/internal/hook.h\"\n\n#include \"jemalloc/internal/atomic.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/seq.h\"\n\ntypedef struct hooks_internal_s hooks_internal_t;\nstruct hooks_internal_s {\n\thooks_t hooks;\n\tbool in_use;\n};\n\nseq_define(hooks_internal_t, hooks)\n\nstatic atomic_u_t nhooks = ATOMIC_INIT(0);\nstatic seq_hooks_t hooks[HOOK_MAX];\nstatic malloc_mutex_t hooks_mu;\n\nbool\nhook_boot() {\n\treturn malloc_mutex_init(&hooks_mu, \"hooks\", WITNESS_RANK_HOOK,\n\t    malloc_mutex_rank_exclusive);\n}\n\nstatic void *\nhook_install_locked(hooks_t *to_install) {\n\thooks_internal_t hooks_internal;\n\tfor (int i = 0; i < HOOK_MAX; i++) {\n\t\tbool success = seq_try_load_hooks(&hooks_internal, &hooks[i]);\n\t\t/* We hold mu; no concurrent access. */\n\t\tassert(success);\n\t\tif (!hooks_internal.in_use) {\n\t\t\thooks_internal.hooks = *to_install;\n\t\t\thooks_internal.in_use = true;\n\t\t\tseq_store_hooks(&hooks[i], &hooks_internal);\n\t\t\tatomic_store_u(&nhooks,\n\t\t\t    atomic_load_u(&nhooks, ATOMIC_RELAXED) + 1,\n\t\t\t    ATOMIC_RELAXED);\n\t\t\treturn &hooks[i];\n\t\t}\n\t}\n\treturn NULL;\n}\n\nvoid *\nhook_install(tsdn_t *tsdn, hooks_t *to_install) {\n\tmalloc_mutex_lock(tsdn, &hooks_mu);\n\tvoid *ret = hook_install_locked(to_install);\n\tif (ret != NULL) {\n\t\ttsd_global_slow_inc(tsdn);\n\t}\n\tmalloc_mutex_unlock(tsdn, &hooks_mu);\n\treturn ret;\n}\n\nstatic void\nhook_remove_locked(seq_hooks_t *to_remove) {\n\thooks_internal_t hooks_internal;\n\tbool success = seq_try_load_hooks(&hooks_internal, to_remove);\n\t/* We hold mu; no concurrent access. */\n\tassert(success);\n\t/* Should only remove hooks that were added. */\n\tassert(hooks_internal.in_use);\n\thooks_internal.in_use = false;\n\tseq_store_hooks(to_remove, &hooks_internal);\n\tatomic_store_u(&nhooks, atomic_load_u(&nhooks, ATOMIC_RELAXED) - 1,\n\t    ATOMIC_RELAXED);\n}\n\nvoid\nhook_remove(tsdn_t *tsdn, void *opaque) {\n\tif (config_debug) {\n\t\tchar *hooks_begin = (char *)&hooks[0];\n\t\tchar *hooks_end = (char *)&hooks[HOOK_MAX];\n\t\tchar *hook = (char *)opaque;\n\t\tassert(hooks_begin <= hook && hook < hooks_end\n\t\t    && (hook - hooks_begin) % sizeof(seq_hooks_t) == 0);\n\t}\n\tmalloc_mutex_lock(tsdn, &hooks_mu);\n\thook_remove_locked((seq_hooks_t *)opaque);\n\ttsd_global_slow_dec(tsdn);\n\tmalloc_mutex_unlock(tsdn, &hooks_mu);\n}\n\n#define FOR_EACH_HOOK_BEGIN(hooks_internal_ptr)\t\t\t\t\\\nfor (int for_each_hook_counter = 0;\t\t\t\t\t\\\n    for_each_hook_counter < HOOK_MAX;\t\t\t\t\t\\\n    for_each_hook_counter++) {\t\t\t\t\t\t\\\n\tbool for_each_hook_success = seq_try_load_hooks(\t\t\\\n\t    (hooks_internal_ptr), &hooks[for_each_hook_counter]);\t\\\n\tif (!for_each_hook_success) {\t\t\t\t\t\\\n\t\tcontinue;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tif (!(hooks_internal_ptr)->in_use) {\t\t\t\t\\\n\t\tcontinue;\t\t\t\t\t\t\\\n\t}\n#define FOR_EACH_HOOK_END\t\t\t\t\t\t\\\n}\n\nstatic bool *\nhook_reentrantp() {\n\t/*\n\t * We prevent user reentrancy within hooks.  This is basically just a\n\t * thread-local bool that triggers an early-exit.\n\t *\n\t * We don't fold in_hook into reentrancy.  There are two reasons for\n\t * this:\n\t * - Right now, we turn on reentrancy during things like extent hook\n\t *   execution.  Allocating during extent hooks is not officially\n\t *   supported, but we don't want to break it for the time being.  These\n\t *   sorts of allocations should probably still be hooked, though.\n\t * - If a hook allocates, we may want it to be relatively fast (after\n\t *   all, it executes on every allocator operation).  Turning on\n\t *   reentrancy is a fairly heavyweight mode (disabling tcache,\n\t *   redirecting to arena 0, etc.).  It's possible we may one day want\n\t *   to turn on reentrant mode here, if it proves too difficult to keep\n\t *   this working.  But that's fairly easy for us to see; OTOH, people\n\t *   not using hooks because they're too slow is easy for us to miss.\n\t *\n\t * The tricky part is\n\t * that this code might get invoked even if we don't have access to tsd.\n\t * This function mimics getting a pointer to thread-local data, except\n\t * that it might secretly return a pointer to some global data if we\n\t * know that the caller will take the early-exit path.\n\t * If we return a bool that indicates that we are reentrant, then the\n\t * caller will go down the early exit path, leaving the global\n\t * untouched.\n\t */\n\tstatic bool in_hook_global = true;\n\ttsdn_t *tsdn = tsdn_fetch();\n\ttcache_t *tcache = tsdn_tcachep_get(tsdn);\n\tif (tcache != NULL) {\n\t\treturn &tcache->in_hook;\n\t}\n\treturn &in_hook_global;\n}\n\n#define HOOK_PROLOGUE\t\t\t\t\t\t\t\\\n\tif (likely(atomic_load_u(&nhooks, ATOMIC_RELAXED) == 0)) {\t\\\n\t\treturn;\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tbool *in_hook = hook_reentrantp();\t\t\t\t\\\n\tif (*in_hook) {\t\t\t\t\t\t\t\\\n\t\treturn;\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\t*in_hook = true;\n\n#define HOOK_EPILOGUE\t\t\t\t\t\t\t\\\n\t*in_hook = false;\n\nvoid\nhook_invoke_alloc(hook_alloc_t type, void *result, uintptr_t result_raw,\n    uintptr_t args_raw[3]) {\n\tHOOK_PROLOGUE\n\n\thooks_internal_t hook;\n\tFOR_EACH_HOOK_BEGIN(&hook)\n\t\thook_alloc h = hook.hooks.alloc_hook;\n\t\tif (h != NULL) {\n\t\t\th(hook.hooks.extra, type, result, result_raw, args_raw);\n\t\t}\n\tFOR_EACH_HOOK_END\n\n\tHOOK_EPILOGUE\n}\n\nvoid\nhook_invoke_dalloc(hook_dalloc_t type, void *address, uintptr_t args_raw[3]) {\n\tHOOK_PROLOGUE\n\thooks_internal_t hook;\n\tFOR_EACH_HOOK_BEGIN(&hook)\n\t\thook_dalloc h = hook.hooks.dalloc_hook;\n\t\tif (h != NULL) {\n\t\t\th(hook.hooks.extra, type, address, args_raw);\n\t\t}\n\tFOR_EACH_HOOK_END\n\tHOOK_EPILOGUE\n}\n\nvoid\nhook_invoke_expand(hook_expand_t type, void *address, size_t old_usize,\n    size_t new_usize, uintptr_t result_raw, uintptr_t args_raw[4]) {\n\tHOOK_PROLOGUE\n\thooks_internal_t hook;\n\tFOR_EACH_HOOK_BEGIN(&hook)\n\t\thook_expand h = hook.hooks.expand_hook;\n\t\tif (h != NULL) {\n\t\t\th(hook.hooks.extra, type, address, old_usize, new_usize,\n\t\t\t    result_raw, args_raw);\n\t\t}\n\tFOR_EACH_HOOK_END\n\tHOOK_EPILOGUE\n}\n"
  },
  {
    "path": "deps/jemalloc/src/jemalloc.c",
    "content": "#define JEMALLOC_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/atomic.h\"\n#include \"jemalloc/internal/ctl.h\"\n#include \"jemalloc/internal/extent_dss.h\"\n#include \"jemalloc/internal/extent_mmap.h\"\n#include \"jemalloc/internal/hook.h\"\n#include \"jemalloc/internal/jemalloc_internal_types.h\"\n#include \"jemalloc/internal/log.h\"\n#include \"jemalloc/internal/malloc_io.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/rtree.h\"\n#include \"jemalloc/internal/safety_check.h\"\n#include \"jemalloc/internal/sc.h\"\n#include \"jemalloc/internal/spin.h\"\n#include \"jemalloc/internal/sz.h\"\n#include \"jemalloc/internal/ticker.h\"\n#include \"jemalloc/internal/util.h\"\n\n/******************************************************************************/\n/* Data. */\n\n/* Runtime configuration options. */\nconst char\t*je_malloc_conf\n#ifndef _WIN32\n    JEMALLOC_ATTR(weak)\n#endif\n    ;\nbool\topt_abort =\n#ifdef JEMALLOC_DEBUG\n    true\n#else\n    false\n#endif\n    ;\nbool\topt_abort_conf =\n#ifdef JEMALLOC_DEBUG\n    true\n#else\n    false\n#endif\n    ;\n/* Intentionally default off, even with debug builds. */\nbool\topt_confirm_conf = false;\nconst char\t*opt_junk =\n#if (defined(JEMALLOC_DEBUG) && defined(JEMALLOC_FILL))\n    \"true\"\n#else\n    \"false\"\n#endif\n    ;\nbool\topt_junk_alloc =\n#if (defined(JEMALLOC_DEBUG) && defined(JEMALLOC_FILL))\n    true\n#else\n    false\n#endif\n    ;\nbool\topt_junk_free =\n#if (defined(JEMALLOC_DEBUG) && defined(JEMALLOC_FILL))\n    true\n#else\n    false\n#endif\n    ;\n\nbool\topt_utrace = false;\nbool\topt_xmalloc = false;\nbool\topt_zero = false;\nunsigned\topt_narenas = 0;\n\nunsigned\tncpus;\n\n/* Protects arenas initialization. */\nmalloc_mutex_t arenas_lock;\n/*\n * Arenas that are used to service external requests.  Not all elements of the\n * arenas array are necessarily used; arenas are created lazily as needed.\n *\n * arenas[0..narenas_auto) are used for automatic multiplexing of threads and\n * arenas.  arenas[narenas_auto..narenas_total) are only used if the application\n * takes some action to create them and allocate from them.\n *\n * Points to an arena_t.\n */\nJEMALLOC_ALIGNED(CACHELINE)\natomic_p_t\t\tarenas[MALLOCX_ARENA_LIMIT];\nstatic atomic_u_t\tnarenas_total; /* Use narenas_total_*(). */\n/* Below three are read-only after initialization. */\nstatic arena_t\t\t*a0; /* arenas[0]. */\nunsigned\t\tnarenas_auto;\nunsigned\t\tmanual_arena_base;\n\ntypedef enum {\n\tmalloc_init_uninitialized\t= 3,\n\tmalloc_init_a0_initialized\t= 2,\n\tmalloc_init_recursible\t\t= 1,\n\tmalloc_init_initialized\t\t= 0 /* Common case --> jnz. */\n} malloc_init_t;\nstatic malloc_init_t\tmalloc_init_state = malloc_init_uninitialized;\n\n/* False should be the common case.  Set to true to trigger initialization. */\nbool\t\t\tmalloc_slow = true;\n\n/* When malloc_slow is true, set the corresponding bits for sanity check. */\nenum {\n\tflag_opt_junk_alloc\t= (1U),\n\tflag_opt_junk_free\t= (1U << 1),\n\tflag_opt_zero\t\t= (1U << 2),\n\tflag_opt_utrace\t\t= (1U << 3),\n\tflag_opt_xmalloc\t= (1U << 4)\n};\nstatic uint8_t\tmalloc_slow_flags;\n\n#ifdef JEMALLOC_THREADED_INIT\n/* Used to let the initializing thread recursively allocate. */\n#  define NO_INITIALIZER\t((unsigned long)0)\n#  define INITIALIZER\t\tpthread_self()\n#  define IS_INITIALIZER\t(malloc_initializer == pthread_self())\nstatic pthread_t\t\tmalloc_initializer = NO_INITIALIZER;\n#else\n#  define NO_INITIALIZER\tfalse\n#  define INITIALIZER\t\ttrue\n#  define IS_INITIALIZER\tmalloc_initializer\nstatic bool\t\t\tmalloc_initializer = NO_INITIALIZER;\n#endif\n\n/* Used to avoid initialization races. */\n#ifdef _WIN32\n#if _WIN32_WINNT >= 0x0600\nstatic malloc_mutex_t\tinit_lock = SRWLOCK_INIT;\n#else\nstatic malloc_mutex_t\tinit_lock;\nstatic bool init_lock_initialized = false;\n\nJEMALLOC_ATTR(constructor)\nstatic void WINAPI\n_init_init_lock(void) {\n\t/*\n\t * If another constructor in the same binary is using mallctl to e.g.\n\t * set up extent hooks, it may end up running before this one, and\n\t * malloc_init_hard will crash trying to lock the uninitialized lock. So\n\t * we force an initialization of the lock in malloc_init_hard as well.\n\t * We don't try to care about atomicity of the accessed to the\n\t * init_lock_initialized boolean, since it really only matters early in\n\t * the process creation, before any separate thread normally starts\n\t * doing anything.\n\t */\n\tif (!init_lock_initialized) {\n\t\tmalloc_mutex_init(&init_lock, \"init\", WITNESS_RANK_INIT,\n\t\t    malloc_mutex_rank_exclusive);\n\t}\n\tinit_lock_initialized = true;\n}\n\n#ifdef _MSC_VER\n#  pragma section(\".CRT$XCU\", read)\nJEMALLOC_SECTION(\".CRT$XCU\") JEMALLOC_ATTR(used)\nstatic const void (WINAPI *init_init_lock)(void) = _init_init_lock;\n#endif\n#endif\n#else\nstatic malloc_mutex_t\tinit_lock = MALLOC_MUTEX_INITIALIZER;\n#endif\n\ntypedef struct {\n\tvoid\t*p;\t/* Input pointer (as in realloc(p, s)). */\n\tsize_t\ts;\t/* Request size. */\n\tvoid\t*r;\t/* Result pointer. */\n} malloc_utrace_t;\n\n#ifdef JEMALLOC_UTRACE\n#  define UTRACE(a, b, c) do {\t\t\t\t\t\t\\\n\tif (unlikely(opt_utrace)) {\t\t\t\t\t\\\n\t\tint utrace_serrno = errno;\t\t\t\t\\\n\t\tmalloc_utrace_t ut;\t\t\t\t\t\\\n\t\tut.p = (a);\t\t\t\t\t\t\\\n\t\tut.s = (b);\t\t\t\t\t\t\\\n\t\tut.r = (c);\t\t\t\t\t\t\\\n\t\tutrace(&ut, sizeof(ut));\t\t\t\t\\\n\t\terrno = utrace_serrno;\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n#else\n#  define UTRACE(a, b, c)\n#endif\n\n/* Whether encountered any invalid config options. */\nstatic bool had_conf_error = false;\n\n/******************************************************************************/\n/*\n * Function prototypes for static functions that are referenced prior to\n * definition.\n */\n\nstatic bool\tmalloc_init_hard_a0(void);\nstatic bool\tmalloc_init_hard(void);\n\n/******************************************************************************/\n/*\n * Begin miscellaneous support functions.\n */\n\nbool\nmalloc_initialized(void) {\n\treturn (malloc_init_state == malloc_init_initialized);\n}\n\nJEMALLOC_ALWAYS_INLINE bool\nmalloc_init_a0(void) {\n\tif (unlikely(malloc_init_state == malloc_init_uninitialized)) {\n\t\treturn malloc_init_hard_a0();\n\t}\n\treturn false;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\nmalloc_init(void) {\n\tif (unlikely(!malloc_initialized()) && malloc_init_hard()) {\n\t\treturn true;\n\t}\n\treturn false;\n}\n\n/*\n * The a0*() functions are used instead of i{d,}alloc() in situations that\n * cannot tolerate TLS variable access.\n */\n\nstatic void *\na0ialloc(size_t size, bool zero, bool is_internal) {\n\tif (unlikely(malloc_init_a0())) {\n\t\treturn NULL;\n\t}\n\n\treturn iallocztm(TSDN_NULL, size, sz_size2index(size), zero, NULL,\n\t    is_internal, arena_get(TSDN_NULL, 0, true), true);\n}\n\nstatic void\na0idalloc(void *ptr, bool is_internal) {\n\tidalloctm(TSDN_NULL, ptr, NULL, NULL, is_internal, true);\n}\n\nvoid *\na0malloc(size_t size) {\n\treturn a0ialloc(size, false, true);\n}\n\nvoid\na0dalloc(void *ptr) {\n\ta0idalloc(ptr, true);\n}\n\n/*\n * FreeBSD's libc uses the bootstrap_*() functions in bootstrap-senstive\n * situations that cannot tolerate TLS variable access (TLS allocation and very\n * early internal data structure initialization).\n */\n\nvoid *\nbootstrap_malloc(size_t size) {\n\tif (unlikely(size == 0)) {\n\t\tsize = 1;\n\t}\n\n\treturn a0ialloc(size, false, false);\n}\n\nvoid *\nbootstrap_calloc(size_t num, size_t size) {\n\tsize_t num_size;\n\n\tnum_size = num * size;\n\tif (unlikely(num_size == 0)) {\n\t\tassert(num == 0 || size == 0);\n\t\tnum_size = 1;\n\t}\n\n\treturn a0ialloc(num_size, true, false);\n}\n\nvoid\nbootstrap_free(void *ptr) {\n\tif (unlikely(ptr == NULL)) {\n\t\treturn;\n\t}\n\n\ta0idalloc(ptr, false);\n}\n\nvoid\narena_set(unsigned ind, arena_t *arena) {\n\tatomic_store_p(&arenas[ind], arena, ATOMIC_RELEASE);\n}\n\nstatic void\nnarenas_total_set(unsigned narenas) {\n\tatomic_store_u(&narenas_total, narenas, ATOMIC_RELEASE);\n}\n\nstatic void\nnarenas_total_inc(void) {\n\tatomic_fetch_add_u(&narenas_total, 1, ATOMIC_RELEASE);\n}\n\nunsigned\nnarenas_total_get(void) {\n\treturn atomic_load_u(&narenas_total, ATOMIC_ACQUIRE);\n}\n\n/* Create a new arena and insert it into the arenas array at index ind. */\nstatic arena_t *\narena_init_locked(tsdn_t *tsdn, unsigned ind, extent_hooks_t *extent_hooks) {\n\tarena_t *arena;\n\n\tassert(ind <= narenas_total_get());\n\tif (ind >= MALLOCX_ARENA_LIMIT) {\n\t\treturn NULL;\n\t}\n\tif (ind == narenas_total_get()) {\n\t\tnarenas_total_inc();\n\t}\n\n\t/*\n\t * Another thread may have already initialized arenas[ind] if it's an\n\t * auto arena.\n\t */\n\tarena = arena_get(tsdn, ind, false);\n\tif (arena != NULL) {\n\t\tassert(arena_is_auto(arena));\n\t\treturn arena;\n\t}\n\n\t/* Actually initialize the arena. */\n\tarena = arena_new(tsdn, ind, extent_hooks);\n\n\treturn arena;\n}\n\nstatic void\narena_new_create_background_thread(tsdn_t *tsdn, unsigned ind) {\n\tif (ind == 0) {\n\t\treturn;\n\t}\n\t/*\n\t * Avoid creating a new background thread just for the huge arena, which\n\t * purges eagerly by default.\n\t */\n\tif (have_background_thread && !arena_is_huge(ind)) {\n\t\tif (background_thread_create(tsdn_tsd(tsdn), ind)) {\n\t\t\tmalloc_printf(\"<jemalloc>: error in background thread \"\n\t\t\t\t      \"creation for arena %u. Abort.\\n\", ind);\n\t\t\tabort();\n\t\t}\n\t}\n}\n\narena_t *\narena_init(tsdn_t *tsdn, unsigned ind, extent_hooks_t *extent_hooks) {\n\tarena_t *arena;\n\n\tmalloc_mutex_lock(tsdn, &arenas_lock);\n\tarena = arena_init_locked(tsdn, ind, extent_hooks);\n\tmalloc_mutex_unlock(tsdn, &arenas_lock);\n\n\tarena_new_create_background_thread(tsdn, ind);\n\n\treturn arena;\n}\n\nstatic void\narena_bind(tsd_t *tsd, unsigned ind, bool internal) {\n\tarena_t *arena = arena_get(tsd_tsdn(tsd), ind, false);\n\tarena_nthreads_inc(arena, internal);\n\n\tif (internal) {\n\t\ttsd_iarena_set(tsd, arena);\n\t} else {\n\t\ttsd_arena_set(tsd, arena);\n\t\tunsigned shard = atomic_fetch_add_u(&arena->binshard_next, 1,\n\t\t    ATOMIC_RELAXED);\n\t\ttsd_binshards_t *bins = tsd_binshardsp_get(tsd);\n\t\tfor (unsigned i = 0; i < SC_NBINS; i++) {\n\t\t\tassert(bin_infos[i].n_shards > 0 &&\n\t\t\t    bin_infos[i].n_shards <= BIN_SHARDS_MAX);\n\t\t\tbins->binshard[i] = shard % bin_infos[i].n_shards;\n\t\t}\n\t}\n}\n\nvoid\narena_migrate(tsd_t *tsd, unsigned oldind, unsigned newind) {\n\tarena_t *oldarena, *newarena;\n\n\toldarena = arena_get(tsd_tsdn(tsd), oldind, false);\n\tnewarena = arena_get(tsd_tsdn(tsd), newind, false);\n\tarena_nthreads_dec(oldarena, false);\n\tarena_nthreads_inc(newarena, false);\n\ttsd_arena_set(tsd, newarena);\n}\n\nstatic void\narena_unbind(tsd_t *tsd, unsigned ind, bool internal) {\n\tarena_t *arena;\n\n\tarena = arena_get(tsd_tsdn(tsd), ind, false);\n\tarena_nthreads_dec(arena, internal);\n\n\tif (internal) {\n\t\ttsd_iarena_set(tsd, NULL);\n\t} else {\n\t\ttsd_arena_set(tsd, NULL);\n\t}\n}\n\narena_tdata_t *\narena_tdata_get_hard(tsd_t *tsd, unsigned ind) {\n\tarena_tdata_t *tdata, *arenas_tdata_old;\n\tarena_tdata_t *arenas_tdata = tsd_arenas_tdata_get(tsd);\n\tunsigned narenas_tdata_old, i;\n\tunsigned narenas_tdata = tsd_narenas_tdata_get(tsd);\n\tunsigned narenas_actual = narenas_total_get();\n\n\t/*\n\t * Dissociate old tdata array (and set up for deallocation upon return)\n\t * if it's too small.\n\t */\n\tif (arenas_tdata != NULL && narenas_tdata < narenas_actual) {\n\t\tarenas_tdata_old = arenas_tdata;\n\t\tnarenas_tdata_old = narenas_tdata;\n\t\tarenas_tdata = NULL;\n\t\tnarenas_tdata = 0;\n\t\ttsd_arenas_tdata_set(tsd, arenas_tdata);\n\t\ttsd_narenas_tdata_set(tsd, narenas_tdata);\n\t} else {\n\t\tarenas_tdata_old = NULL;\n\t\tnarenas_tdata_old = 0;\n\t}\n\n\t/* Allocate tdata array if it's missing. */\n\tif (arenas_tdata == NULL) {\n\t\tbool *arenas_tdata_bypassp = tsd_arenas_tdata_bypassp_get(tsd);\n\t\tnarenas_tdata = (ind < narenas_actual) ? narenas_actual : ind+1;\n\n\t\tif (tsd_nominal(tsd) && !*arenas_tdata_bypassp) {\n\t\t\t*arenas_tdata_bypassp = true;\n\t\t\tarenas_tdata = (arena_tdata_t *)a0malloc(\n\t\t\t    sizeof(arena_tdata_t) * narenas_tdata);\n\t\t\t*arenas_tdata_bypassp = false;\n\t\t}\n\t\tif (arenas_tdata == NULL) {\n\t\t\ttdata = NULL;\n\t\t\tgoto label_return;\n\t\t}\n\t\tassert(tsd_nominal(tsd) && !*arenas_tdata_bypassp);\n\t\ttsd_arenas_tdata_set(tsd, arenas_tdata);\n\t\ttsd_narenas_tdata_set(tsd, narenas_tdata);\n\t}\n\n\t/*\n\t * Copy to tdata array.  It's possible that the actual number of arenas\n\t * has increased since narenas_total_get() was called above, but that\n\t * causes no correctness issues unless two threads concurrently execute\n\t * the arenas.create mallctl, which we trust mallctl synchronization to\n\t * prevent.\n\t */\n\n\t/* Copy/initialize tickers. */\n\tfor (i = 0; i < narenas_actual; i++) {\n\t\tif (i < narenas_tdata_old) {\n\t\t\tticker_copy(&arenas_tdata[i].decay_ticker,\n\t\t\t    &arenas_tdata_old[i].decay_ticker);\n\t\t} else {\n\t\t\tticker_init(&arenas_tdata[i].decay_ticker,\n\t\t\t    DECAY_NTICKS_PER_UPDATE);\n\t\t}\n\t}\n\tif (narenas_tdata > narenas_actual) {\n\t\tmemset(&arenas_tdata[narenas_actual], 0, sizeof(arena_tdata_t)\n\t\t    * (narenas_tdata - narenas_actual));\n\t}\n\n\t/* Read the refreshed tdata array. */\n\ttdata = &arenas_tdata[ind];\nlabel_return:\n\tif (arenas_tdata_old != NULL) {\n\t\ta0dalloc(arenas_tdata_old);\n\t}\n\treturn tdata;\n}\n\n/* Slow path, called only by arena_choose(). */\narena_t *\narena_choose_hard(tsd_t *tsd, bool internal) {\n\tarena_t *ret JEMALLOC_CC_SILENCE_INIT(NULL);\n\n\tif (have_percpu_arena && PERCPU_ARENA_ENABLED(opt_percpu_arena)) {\n\t\tunsigned choose = percpu_arena_choose();\n\t\tret = arena_get(tsd_tsdn(tsd), choose, true);\n\t\tassert(ret != NULL);\n\t\tarena_bind(tsd, arena_ind_get(ret), false);\n\t\tarena_bind(tsd, arena_ind_get(ret), true);\n\n\t\treturn ret;\n\t}\n\n\tif (narenas_auto > 1) {\n\t\tunsigned i, j, choose[2], first_null;\n\t\tbool is_new_arena[2];\n\n\t\t/*\n\t\t * Determine binding for both non-internal and internal\n\t\t * allocation.\n\t\t *\n\t\t *   choose[0]: For application allocation.\n\t\t *   choose[1]: For internal metadata allocation.\n\t\t */\n\n\t\tfor (j = 0; j < 2; j++) {\n\t\t\tchoose[j] = 0;\n\t\t\tis_new_arena[j] = false;\n\t\t}\n\n\t\tfirst_null = narenas_auto;\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &arenas_lock);\n\t\tassert(arena_get(tsd_tsdn(tsd), 0, false) != NULL);\n\t\tfor (i = 1; i < narenas_auto; i++) {\n\t\t\tif (arena_get(tsd_tsdn(tsd), i, false) != NULL) {\n\t\t\t\t/*\n\t\t\t\t * Choose the first arena that has the lowest\n\t\t\t\t * number of threads assigned to it.\n\t\t\t\t */\n\t\t\t\tfor (j = 0; j < 2; j++) {\n\t\t\t\t\tif (arena_nthreads_get(arena_get(\n\t\t\t\t\t    tsd_tsdn(tsd), i, false), !!j) <\n\t\t\t\t\t    arena_nthreads_get(arena_get(\n\t\t\t\t\t    tsd_tsdn(tsd), choose[j], false),\n\t\t\t\t\t    !!j)) {\n\t\t\t\t\t\tchoose[j] = i;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if (first_null == narenas_auto) {\n\t\t\t\t/*\n\t\t\t\t * Record the index of the first uninitialized\n\t\t\t\t * arena, in case all extant arenas are in use.\n\t\t\t\t *\n\t\t\t\t * NB: It is possible for there to be\n\t\t\t\t * discontinuities in terms of initialized\n\t\t\t\t * versus uninitialized arenas, due to the\n\t\t\t\t * \"thread.arena\" mallctl.\n\t\t\t\t */\n\t\t\t\tfirst_null = i;\n\t\t\t}\n\t\t}\n\n\t\tfor (j = 0; j < 2; j++) {\n\t\t\tif (arena_nthreads_get(arena_get(tsd_tsdn(tsd),\n\t\t\t    choose[j], false), !!j) == 0 || first_null ==\n\t\t\t    narenas_auto) {\n\t\t\t\t/*\n\t\t\t\t * Use an unloaded arena, or the least loaded\n\t\t\t\t * arena if all arenas are already initialized.\n\t\t\t\t */\n\t\t\t\tif (!!j == internal) {\n\t\t\t\t\tret = arena_get(tsd_tsdn(tsd),\n\t\t\t\t\t    choose[j], false);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tarena_t *arena;\n\n\t\t\t\t/* Initialize a new arena. */\n\t\t\t\tchoose[j] = first_null;\n\t\t\t\tarena = arena_init_locked(tsd_tsdn(tsd),\n\t\t\t\t    choose[j],\n\t\t\t\t    (extent_hooks_t *)&extent_hooks_default);\n\t\t\t\tif (arena == NULL) {\n\t\t\t\t\tmalloc_mutex_unlock(tsd_tsdn(tsd),\n\t\t\t\t\t    &arenas_lock);\n\t\t\t\t\treturn NULL;\n\t\t\t\t}\n\t\t\t\tis_new_arena[j] = true;\n\t\t\t\tif (!!j == internal) {\n\t\t\t\t\tret = arena;\n\t\t\t\t}\n\t\t\t}\n\t\t\tarena_bind(tsd, choose[j], !!j);\n\t\t}\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &arenas_lock);\n\n\t\tfor (j = 0; j < 2; j++) {\n\t\t\tif (is_new_arena[j]) {\n\t\t\t\tassert(choose[j] > 0);\n\t\t\t\tarena_new_create_background_thread(\n\t\t\t\t    tsd_tsdn(tsd), choose[j]);\n\t\t\t}\n\t\t}\n\n\t} else {\n\t\tret = arena_get(tsd_tsdn(tsd), 0, false);\n\t\tarena_bind(tsd, 0, false);\n\t\tarena_bind(tsd, 0, true);\n\t}\n\n\treturn ret;\n}\n\nvoid\niarena_cleanup(tsd_t *tsd) {\n\tarena_t *iarena;\n\n\tiarena = tsd_iarena_get(tsd);\n\tif (iarena != NULL) {\n\t\tarena_unbind(tsd, arena_ind_get(iarena), true);\n\t}\n}\n\nvoid\narena_cleanup(tsd_t *tsd) {\n\tarena_t *arena;\n\n\tarena = tsd_arena_get(tsd);\n\tif (arena != NULL) {\n\t\tarena_unbind(tsd, arena_ind_get(arena), false);\n\t}\n}\n\nvoid\narenas_tdata_cleanup(tsd_t *tsd) {\n\tarena_tdata_t *arenas_tdata;\n\n\t/* Prevent tsd->arenas_tdata from being (re)created. */\n\t*tsd_arenas_tdata_bypassp_get(tsd) = true;\n\n\tarenas_tdata = tsd_arenas_tdata_get(tsd);\n\tif (arenas_tdata != NULL) {\n\t\ttsd_arenas_tdata_set(tsd, NULL);\n\t\ta0dalloc(arenas_tdata);\n\t}\n}\n\nstatic void\nstats_print_atexit(void) {\n\tif (config_stats) {\n\t\ttsdn_t *tsdn;\n\t\tunsigned narenas, i;\n\n\t\ttsdn = tsdn_fetch();\n\n\t\t/*\n\t\t * Merge stats from extant threads.  This is racy, since\n\t\t * individual threads do not lock when recording tcache stats\n\t\t * events.  As a consequence, the final stats may be slightly\n\t\t * out of date by the time they are reported, if other threads\n\t\t * continue to allocate.\n\t\t */\n\t\tfor (i = 0, narenas = narenas_total_get(); i < narenas; i++) {\n\t\t\tarena_t *arena = arena_get(tsdn, i, false);\n\t\t\tif (arena != NULL) {\n\t\t\t\ttcache_t *tcache;\n\n\t\t\t\tmalloc_mutex_lock(tsdn, &arena->tcache_ql_mtx);\n\t\t\t\tql_foreach(tcache, &arena->tcache_ql, link) {\n\t\t\t\t\ttcache_stats_merge(tsdn, tcache, arena);\n\t\t\t\t}\n\t\t\t\tmalloc_mutex_unlock(tsdn,\n\t\t\t\t    &arena->tcache_ql_mtx);\n\t\t\t}\n\t\t}\n\t}\n\tje_malloc_stats_print(NULL, NULL, opt_stats_print_opts);\n}\n\n/*\n * Ensure that we don't hold any locks upon entry to or exit from allocator\n * code (in a \"broad\" sense that doesn't count a reentrant allocation as an\n * entrance or exit).\n */\nJEMALLOC_ALWAYS_INLINE void\ncheck_entry_exit_locking(tsdn_t *tsdn) {\n\tif (!config_debug) {\n\t\treturn;\n\t}\n\tif (tsdn_null(tsdn)) {\n\t\treturn;\n\t}\n\ttsd_t *tsd = tsdn_tsd(tsdn);\n\t/*\n\t * It's possible we hold locks at entry/exit if we're in a nested\n\t * allocation.\n\t */\n\tint8_t reentrancy_level = tsd_reentrancy_level_get(tsd);\n\tif (reentrancy_level != 0) {\n\t\treturn;\n\t}\n\twitness_assert_lockless(tsdn_witness_tsdp_get(tsdn));\n}\n\n/*\n * End miscellaneous support functions.\n */\n/******************************************************************************/\n/*\n * Begin initialization functions.\n */\n\nstatic char *\njemalloc_secure_getenv(const char *name) {\n#ifdef JEMALLOC_HAVE_SECURE_GETENV\n\treturn secure_getenv(name);\n#else\n#  ifdef JEMALLOC_HAVE_ISSETUGID\n\tif (issetugid() != 0) {\n\t\treturn NULL;\n\t}\n#  endif\n\treturn getenv(name);\n#endif\n}\n\nstatic unsigned\nmalloc_ncpus(void) {\n\tlong result;\n\n#ifdef _WIN32\n\tSYSTEM_INFO si;\n\tGetSystemInfo(&si);\n\tresult = si.dwNumberOfProcessors;\n#elif defined(JEMALLOC_GLIBC_MALLOC_HOOK) && defined(CPU_COUNT)\n\t/*\n\t * glibc >= 2.6 has the CPU_COUNT macro.\n\t *\n\t * glibc's sysconf() uses isspace().  glibc allocates for the first time\n\t * *before* setting up the isspace tables.  Therefore we need a\n\t * different method to get the number of CPUs.\n\t */\n\t{\n\t\tcpu_set_t set;\n\n\t\tpthread_getaffinity_np(pthread_self(), sizeof(set), &set);\n\t\tresult = CPU_COUNT(&set);\n\t}\n#else\n\tresult = sysconf(_SC_NPROCESSORS_ONLN);\n#endif\n\treturn ((result == -1) ? 1 : (unsigned)result);\n}\n\nstatic void\ninit_opt_stats_print_opts(const char *v, size_t vlen) {\n\tsize_t opts_len = strlen(opt_stats_print_opts);\n\tassert(opts_len <= stats_print_tot_num_options);\n\n\tfor (size_t i = 0; i < vlen; i++) {\n\t\tswitch (v[i]) {\n#define OPTION(o, v, d, s) case o: break;\n\t\t\tSTATS_PRINT_OPTIONS\n#undef OPTION\n\t\tdefault: continue;\n\t\t}\n\n\t\tif (strchr(opt_stats_print_opts, v[i]) != NULL) {\n\t\t\t/* Ignore repeated. */\n\t\t\tcontinue;\n\t\t}\n\n\t\topt_stats_print_opts[opts_len++] = v[i];\n\t\topt_stats_print_opts[opts_len] = '\\0';\n\t\tassert(opts_len <= stats_print_tot_num_options);\n\t}\n\tassert(opts_len == strlen(opt_stats_print_opts));\n}\n\n/* Reads the next size pair in a multi-sized option. */\nstatic bool\nmalloc_conf_multi_sizes_next(const char **slab_size_segment_cur,\n    size_t *vlen_left, size_t *slab_start, size_t *slab_end, size_t *new_size) {\n\tconst char *cur = *slab_size_segment_cur;\n\tchar *end;\n\tuintmax_t um;\n\n\tset_errno(0);\n\n\t/* First number, then '-' */\n\tum = malloc_strtoumax(cur, &end, 0);\n\tif (get_errno() != 0 || *end != '-') {\n\t\treturn true;\n\t}\n\t*slab_start = (size_t)um;\n\tcur = end + 1;\n\n\t/* Second number, then ':' */\n\tum = malloc_strtoumax(cur, &end, 0);\n\tif (get_errno() != 0 || *end != ':') {\n\t\treturn true;\n\t}\n\t*slab_end = (size_t)um;\n\tcur = end + 1;\n\n\t/* Last number */\n\tum = malloc_strtoumax(cur, &end, 0);\n\tif (get_errno() != 0) {\n\t\treturn true;\n\t}\n\t*new_size = (size_t)um;\n\n\t/* Consume the separator if there is one. */\n\tif (*end == '|') {\n\t\tend++;\n\t}\n\n\t*vlen_left -= end - *slab_size_segment_cur;\n\t*slab_size_segment_cur = end;\n\n\treturn false;\n}\n\nstatic bool\nmalloc_conf_next(char const **opts_p, char const **k_p, size_t *klen_p,\n    char const **v_p, size_t *vlen_p) {\n\tbool accept;\n\tconst char *opts = *opts_p;\n\n\t*k_p = opts;\n\n\tfor (accept = false; !accept;) {\n\t\tswitch (*opts) {\n\t\tcase 'A': case 'B': case 'C': case 'D': case 'E': case 'F':\n\t\tcase 'G': case 'H': case 'I': case 'J': case 'K': case 'L':\n\t\tcase 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':\n\t\tcase 'S': case 'T': case 'U': case 'V': case 'W': case 'X':\n\t\tcase 'Y': case 'Z':\n\t\tcase 'a': case 'b': case 'c': case 'd': case 'e': case 'f':\n\t\tcase 'g': case 'h': case 'i': case 'j': case 'k': case 'l':\n\t\tcase 'm': case 'n': case 'o': case 'p': case 'q': case 'r':\n\t\tcase 's': case 't': case 'u': case 'v': case 'w': case 'x':\n\t\tcase 'y': case 'z':\n\t\tcase '0': case '1': case '2': case '3': case '4': case '5':\n\t\tcase '6': case '7': case '8': case '9':\n\t\tcase '_':\n\t\t\topts++;\n\t\t\tbreak;\n\t\tcase ':':\n\t\t\topts++;\n\t\t\t*klen_p = (uintptr_t)opts - 1 - (uintptr_t)*k_p;\n\t\t\t*v_p = opts;\n\t\t\taccept = true;\n\t\t\tbreak;\n\t\tcase '\\0':\n\t\t\tif (opts != *opts_p) {\n\t\t\t\tmalloc_write(\"<jemalloc>: Conf string ends \"\n\t\t\t\t    \"with key\\n\");\n\t\t\t}\n\t\t\treturn true;\n\t\tdefault:\n\t\t\tmalloc_write(\"<jemalloc>: Malformed conf string\\n\");\n\t\t\treturn true;\n\t\t}\n\t}\n\n\tfor (accept = false; !accept;) {\n\t\tswitch (*opts) {\n\t\tcase ',':\n\t\t\topts++;\n\t\t\t/*\n\t\t\t * Look ahead one character here, because the next time\n\t\t\t * this function is called, it will assume that end of\n\t\t\t * input has been cleanly reached if no input remains,\n\t\t\t * but we have optimistically already consumed the\n\t\t\t * comma if one exists.\n\t\t\t */\n\t\t\tif (*opts == '\\0') {\n\t\t\t\tmalloc_write(\"<jemalloc>: Conf string ends \"\n\t\t\t\t    \"with comma\\n\");\n\t\t\t}\n\t\t\t*vlen_p = (uintptr_t)opts - 1 - (uintptr_t)*v_p;\n\t\t\taccept = true;\n\t\t\tbreak;\n\t\tcase '\\0':\n\t\t\t*vlen_p = (uintptr_t)opts - (uintptr_t)*v_p;\n\t\t\taccept = true;\n\t\t\tbreak;\n\t\tdefault:\n\t\t\topts++;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\t*opts_p = opts;\n\treturn false;\n}\n\nstatic void\nmalloc_abort_invalid_conf(void) {\n\tassert(opt_abort_conf);\n\tmalloc_printf(\"<jemalloc>: Abort (abort_conf:true) on invalid conf \"\n\t    \"value (see above).\\n\");\n\tabort();\n}\n\nstatic void\nmalloc_conf_error(const char *msg, const char *k, size_t klen, const char *v,\n    size_t vlen) {\n\tmalloc_printf(\"<jemalloc>: %s: %.*s:%.*s\\n\", msg, (int)klen, k,\n\t    (int)vlen, v);\n\t/* If abort_conf is set, error out after processing all options. */\n\tconst char *experimental = \"experimental_\";\n\tif (strncmp(k, experimental, strlen(experimental)) == 0) {\n\t\t/* However, tolerate experimental features. */\n\t\treturn;\n\t}\n\thad_conf_error = true;\n}\n\nstatic void\nmalloc_slow_flag_init(void) {\n\t/*\n\t * Combine the runtime options into malloc_slow for fast path.  Called\n\t * after processing all the options.\n\t */\n\tmalloc_slow_flags |= (opt_junk_alloc ? flag_opt_junk_alloc : 0)\n\t    | (opt_junk_free ? flag_opt_junk_free : 0)\n\t    | (opt_zero ? flag_opt_zero : 0)\n\t    | (opt_utrace ? flag_opt_utrace : 0)\n\t    | (opt_xmalloc ? flag_opt_xmalloc : 0);\n\n\tmalloc_slow = (malloc_slow_flags != 0);\n}\n\n/* Number of sources for initializing malloc_conf */\n#define MALLOC_CONF_NSOURCES 4\n\nstatic const char *\nobtain_malloc_conf(unsigned which_source, char buf[PATH_MAX + 1]) {\n\tif (config_debug) {\n\t\tstatic unsigned read_source = 0;\n\t\t/*\n\t\t * Each source should only be read once, to minimize # of\n\t\t * syscalls on init.\n\t\t */\n\t\tassert(read_source++ == which_source);\n\t}\n\tassert(which_source < MALLOC_CONF_NSOURCES);\n\n\tconst char *ret;\n\tswitch (which_source) {\n\tcase 0:\n\t\tret = config_malloc_conf;\n\t\tbreak;\n\tcase 1:\n\t\tif (je_malloc_conf != NULL) {\n\t\t\t/* Use options that were compiled into the program. */\n\t\t\tret = je_malloc_conf;\n\t\t} else {\n\t\t\t/* No configuration specified. */\n\t\t\tret = NULL;\n\t\t}\n\t\tbreak;\n\tcase 2: {\n\t\tssize_t linklen = 0;\n#ifndef _WIN32\n\t\tint saved_errno = errno;\n\t\tconst char *linkname =\n#  ifdef JEMALLOC_PREFIX\n\t\t    \"/etc/\"JEMALLOC_PREFIX\"malloc.conf\"\n#  else\n\t\t    \"/etc/malloc.conf\"\n#  endif\n\t\t    ;\n\n\t\t/*\n\t\t * Try to use the contents of the \"/etc/malloc.conf\" symbolic\n\t\t * link's name.\n\t\t */\n#ifndef JEMALLOC_READLINKAT\n\t\tlinklen = readlink(linkname, buf, PATH_MAX);\n#else\n\t\tlinklen = readlinkat(AT_FDCWD, linkname, buf, PATH_MAX);\n#endif\n\t\tif (linklen == -1) {\n\t\t\t/* No configuration specified. */\n\t\t\tlinklen = 0;\n\t\t\t/* Restore errno. */\n\t\t\tset_errno(saved_errno);\n\t\t}\n#endif\n\t\tbuf[linklen] = '\\0';\n\t\tret = buf;\n\t\tbreak;\n\t} case 3: {\n\t\tconst char *envname =\n#ifdef JEMALLOC_PREFIX\n\t\t    JEMALLOC_CPREFIX\"MALLOC_CONF\"\n#else\n\t\t    \"MALLOC_CONF\"\n#endif\n\t\t    ;\n\n\t\tif ((ret = jemalloc_secure_getenv(envname)) != NULL) {\n\t\t\t/*\n\t\t\t * Do nothing; opts is already initialized to the value\n\t\t\t * of the MALLOC_CONF environment variable.\n\t\t\t */\n\t\t} else {\n\t\t\t/* No configuration specified. */\n\t\t\tret = NULL;\n\t\t}\n\t\tbreak;\n\t} default:\n\t\tnot_reached();\n\t\tret = NULL;\n\t}\n\treturn ret;\n}\n\nstatic void\nmalloc_conf_init_helper(sc_data_t *sc_data, unsigned bin_shard_sizes[SC_NBINS],\n    bool initial_call, const char *opts_cache[MALLOC_CONF_NSOURCES],\n    char buf[PATH_MAX + 1]) {\n\tstatic const char *opts_explain[MALLOC_CONF_NSOURCES] = {\n\t\t\"string specified via --with-malloc-conf\",\n\t\t\"string pointed to by the global variable malloc_conf\",\n\t\t\"\\\"name\\\" of the file referenced by the symbolic link named \"\n\t\t    \"/etc/malloc.conf\",\n\t\t\"value of the environment variable MALLOC_CONF\"\n\t};\n\tunsigned i;\n\tconst char *opts, *k, *v;\n\tsize_t klen, vlen;\n\n\tfor (i = 0; i < MALLOC_CONF_NSOURCES; i++) {\n\t\t/* Get runtime configuration. */\n\t\tif (initial_call) {\n\t\t\topts_cache[i] = obtain_malloc_conf(i, buf);\n\t\t}\n\t\topts = opts_cache[i];\n\t\tif (!initial_call && opt_confirm_conf) {\n\t\t\tmalloc_printf(\n\t\t\t    \"<jemalloc>: malloc_conf #%u (%s): \\\"%s\\\"\\n\",\n\t\t\t    i + 1, opts_explain[i], opts != NULL ? opts : \"\");\n\t\t}\n\t\tif (opts == NULL) {\n\t\t\tcontinue;\n\t\t}\n\n\t\twhile (*opts != '\\0' && !malloc_conf_next(&opts, &k, &klen, &v,\n\t\t    &vlen)) {\n\n#define CONF_ERROR(msg, k, klen, v, vlen)\t\t\t\t\\\n\t\t\tif (!initial_call) {\t\t\t\t\\\n\t\t\t\tmalloc_conf_error(\t\t\t\\\n\t\t\t\t    msg, k, klen, v, vlen);\t\t\\\n\t\t\t\tcur_opt_valid = false;\t\t\t\\\n\t\t\t}\n#define CONF_CONTINUE\t{\t\t\t\t\t\t\\\n\t\t\t\tif (!initial_call && opt_confirm_conf\t\\\n\t\t\t\t    && cur_opt_valid) {\t\t\t\\\n\t\t\t\t\tmalloc_printf(\"<jemalloc>: -- \"\t\\\n\t\t\t\t\t    \"Set conf value: %.*s:%.*s\"\t\\\n\t\t\t\t\t    \"\\n\", (int)klen, k,\t\t\\\n\t\t\t\t\t    (int)vlen, v);\t\t\\\n\t\t\t\t}\t\t\t\t\t\\\n\t\t\t\tcontinue;\t\t\t\t\\\n\t\t\t}\n#define CONF_MATCH(n)\t\t\t\t\t\t\t\\\n\t(sizeof(n)-1 == klen && strncmp(n, k, klen) == 0)\n#define CONF_MATCH_VALUE(n)\t\t\t\t\t\t\\\n\t(sizeof(n)-1 == vlen && strncmp(n, v, vlen) == 0)\n#define CONF_HANDLE_BOOL(o, n)\t\t\t\t\t\t\\\n\t\t\tif (CONF_MATCH(n)) {\t\t\t\t\\\n\t\t\t\tif (CONF_MATCH_VALUE(\"true\")) {\t\t\\\n\t\t\t\t\to = true;\t\t\t\\\n\t\t\t\t} else if (CONF_MATCH_VALUE(\"false\")) {\t\\\n\t\t\t\t\to = false;\t\t\t\\\n\t\t\t\t} else {\t\t\t\t\\\n\t\t\t\t\tCONF_ERROR(\"Invalid conf value\",\\\n\t\t\t\t\t    k, klen, v, vlen);\t\t\\\n\t\t\t\t}\t\t\t\t\t\\\n\t\t\t\tCONF_CONTINUE;\t\t\t\t\\\n\t\t\t}\n      /*\n       * One of the CONF_MIN macros below expands, in one of the use points,\n       * to \"unsigned integer < 0\", which is always false, triggering the\n       * GCC -Wtype-limits warning, which we disable here and re-enable below.\n       */\n      JEMALLOC_DIAGNOSTIC_PUSH\n      JEMALLOC_DIAGNOSTIC_IGNORE_TYPE_LIMITS\n\n#define CONF_DONT_CHECK_MIN(um, min)\tfalse\n#define CONF_CHECK_MIN(um, min)\t((um) < (min))\n#define CONF_DONT_CHECK_MAX(um, max)\tfalse\n#define CONF_CHECK_MAX(um, max)\t((um) > (max))\n#define CONF_HANDLE_T_U(t, o, n, min, max, check_min, check_max, clip)\t\\\n\t\t\tif (CONF_MATCH(n)) {\t\t\t\t\\\n\t\t\t\tuintmax_t um;\t\t\t\t\\\n\t\t\t\tchar *end;\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t\t\t\tset_errno(0);\t\t\t\t\\\n\t\t\t\tum = malloc_strtoumax(v, &end, 0);\t\\\n\t\t\t\tif (get_errno() != 0 || (uintptr_t)end -\\\n\t\t\t\t    (uintptr_t)v != vlen) {\t\t\\\n\t\t\t\t\tCONF_ERROR(\"Invalid conf value\",\\\n\t\t\t\t\t    k, klen, v, vlen);\t\t\\\n\t\t\t\t} else if (clip) {\t\t\t\\\n\t\t\t\t\tif (check_min(um, (t)(min))) {\t\\\n\t\t\t\t\t\to = (t)(min);\t\t\\\n\t\t\t\t\t} else if (\t\t\t\\\n\t\t\t\t\t    check_max(um, (t)(max))) {\t\\\n\t\t\t\t\t\to = (t)(max);\t\t\\\n\t\t\t\t\t} else {\t\t\t\\\n\t\t\t\t\t\to = (t)um;\t\t\\\n\t\t\t\t\t}\t\t\t\t\\\n\t\t\t\t} else {\t\t\t\t\\\n\t\t\t\t\tif (check_min(um, (t)(min)) ||\t\\\n\t\t\t\t\t    check_max(um, (t)(max))) {\t\\\n\t\t\t\t\t\tCONF_ERROR(\t\t\\\n\t\t\t\t\t\t    \"Out-of-range \"\t\\\n\t\t\t\t\t\t    \"conf value\",\t\\\n\t\t\t\t\t\t    k, klen, v, vlen);\t\\\n\t\t\t\t\t} else {\t\t\t\\\n\t\t\t\t\t\to = (t)um;\t\t\\\n\t\t\t\t\t}\t\t\t\t\\\n\t\t\t\t}\t\t\t\t\t\\\n\t\t\t\tCONF_CONTINUE;\t\t\t\t\\\n\t\t\t}\n#define CONF_HANDLE_UNSIGNED(o, n, min, max, check_min, check_max,\t\\\n    clip)\t\t\t\t\t\t\t\t\\\n\t\t\tCONF_HANDLE_T_U(unsigned, o, n, min, max,\t\\\n\t\t\t    check_min, check_max, clip)\n#define CONF_HANDLE_SIZE_T(o, n, min, max, check_min, check_max, clip)\t\\\n\t\t\tCONF_HANDLE_T_U(size_t, o, n, min, max,\t\t\\\n\t\t\t    check_min, check_max, clip)\n#define CONF_HANDLE_SSIZE_T(o, n, min, max)\t\t\t\t\\\n\t\t\tif (CONF_MATCH(n)) {\t\t\t\t\\\n\t\t\t\tlong l;\t\t\t\t\t\\\n\t\t\t\tchar *end;\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t\t\t\tset_errno(0);\t\t\t\t\\\n\t\t\t\tl = strtol(v, &end, 0);\t\t\t\\\n\t\t\t\tif (get_errno() != 0 || (uintptr_t)end -\\\n\t\t\t\t    (uintptr_t)v != vlen) {\t\t\\\n\t\t\t\t\tCONF_ERROR(\"Invalid conf value\",\\\n\t\t\t\t\t    k, klen, v, vlen);\t\t\\\n\t\t\t\t} else if (l < (ssize_t)(min) || l >\t\\\n\t\t\t\t    (ssize_t)(max)) {\t\t\t\\\n\t\t\t\t\tCONF_ERROR(\t\t\t\\\n\t\t\t\t\t    \"Out-of-range conf value\",\t\\\n\t\t\t\t\t    k, klen, v, vlen);\t\t\\\n\t\t\t\t} else {\t\t\t\t\\\n\t\t\t\t\to = l;\t\t\t\t\\\n\t\t\t\t}\t\t\t\t\t\\\n\t\t\t\tCONF_CONTINUE;\t\t\t\t\\\n\t\t\t}\n#define CONF_HANDLE_CHAR_P(o, n, d)\t\t\t\t\t\\\n\t\t\tif (CONF_MATCH(n)) {\t\t\t\t\\\n\t\t\t\tsize_t cpylen = (vlen <=\t\t\\\n\t\t\t\t    sizeof(o)-1) ? vlen :\t\t\\\n\t\t\t\t    sizeof(o)-1;\t\t\t\\\n\t\t\t\tstrncpy(o, v, cpylen);\t\t\t\\\n\t\t\t\to[cpylen] = '\\0';\t\t\t\\\n\t\t\t\tCONF_CONTINUE;\t\t\t\t\\\n\t\t\t}\n\n\t\t\tbool cur_opt_valid = true;\n\n\t\t\tCONF_HANDLE_BOOL(opt_confirm_conf, \"confirm_conf\")\n\t\t\tif (initial_call) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tCONF_HANDLE_BOOL(opt_abort, \"abort\")\n\t\t\tCONF_HANDLE_BOOL(opt_abort_conf, \"abort_conf\")\n\t\t\tif (strncmp(\"metadata_thp\", k, klen) == 0) {\n\t\t\t\tint i;\n\t\t\t\tbool match = false;\n\t\t\t\tfor (i = 0; i < metadata_thp_mode_limit; i++) {\n\t\t\t\t\tif (strncmp(metadata_thp_mode_names[i],\n\t\t\t\t\t    v, vlen) == 0) {\n\t\t\t\t\t\topt_metadata_thp = i;\n\t\t\t\t\t\tmatch = true;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (!match) {\n\t\t\t\t\tCONF_ERROR(\"Invalid conf value\",\n\t\t\t\t\t    k, klen, v, vlen);\n\t\t\t\t}\n\t\t\t\tCONF_CONTINUE;\n\t\t\t}\n\t\t\tCONF_HANDLE_BOOL(opt_retain, \"retain\")\n\t\t\tif (strncmp(\"dss\", k, klen) == 0) {\n\t\t\t\tint i;\n\t\t\t\tbool match = false;\n\t\t\t\tfor (i = 0; i < dss_prec_limit; i++) {\n\t\t\t\t\tif (strncmp(dss_prec_names[i], v, vlen)\n\t\t\t\t\t    == 0) {\n\t\t\t\t\t\tif (extent_dss_prec_set(i)) {\n\t\t\t\t\t\t\tCONF_ERROR(\n\t\t\t\t\t\t\t    \"Error setting dss\",\n\t\t\t\t\t\t\t    k, klen, v, vlen);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\topt_dss =\n\t\t\t\t\t\t\t    dss_prec_names[i];\n\t\t\t\t\t\t\tmatch = true;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (!match) {\n\t\t\t\t\tCONF_ERROR(\"Invalid conf value\",\n\t\t\t\t\t    k, klen, v, vlen);\n\t\t\t\t}\n\t\t\t\tCONF_CONTINUE;\n\t\t\t}\n\t\t\tCONF_HANDLE_UNSIGNED(opt_narenas, \"narenas\", 1,\n\t\t\t    UINT_MAX, CONF_CHECK_MIN, CONF_DONT_CHECK_MAX,\n\t\t\t    false)\n\t\t\tif (CONF_MATCH(\"bin_shards\")) {\n\t\t\t\tconst char *bin_shards_segment_cur = v;\n\t\t\t\tsize_t vlen_left = vlen;\n\t\t\t\tdo {\n\t\t\t\t\tsize_t size_start;\n\t\t\t\t\tsize_t size_end;\n\t\t\t\t\tsize_t nshards;\n\t\t\t\t\tbool err = malloc_conf_multi_sizes_next(\n\t\t\t\t\t    &bin_shards_segment_cur, &vlen_left,\n\t\t\t\t\t    &size_start, &size_end, &nshards);\n\t\t\t\t\tif (err || bin_update_shard_size(\n\t\t\t\t\t    bin_shard_sizes, size_start,\n\t\t\t\t\t    size_end, nshards)) {\n\t\t\t\t\t\tCONF_ERROR(\n\t\t\t\t\t\t    \"Invalid settings for \"\n\t\t\t\t\t\t    \"bin_shards\", k, klen, v,\n\t\t\t\t\t\t    vlen);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t} while (vlen_left > 0);\n\t\t\t\tCONF_CONTINUE;\n\t\t\t}\n\t\t\tCONF_HANDLE_SSIZE_T(opt_dirty_decay_ms,\n\t\t\t    \"dirty_decay_ms\", -1, NSTIME_SEC_MAX * KQU(1000) <\n\t\t\t    QU(SSIZE_MAX) ? NSTIME_SEC_MAX * KQU(1000) :\n\t\t\t    SSIZE_MAX);\n\t\t\tCONF_HANDLE_SSIZE_T(opt_muzzy_decay_ms,\n\t\t\t    \"muzzy_decay_ms\", -1, NSTIME_SEC_MAX * KQU(1000) <\n\t\t\t    QU(SSIZE_MAX) ? NSTIME_SEC_MAX * KQU(1000) :\n\t\t\t    SSIZE_MAX);\n\t\t\tCONF_HANDLE_BOOL(opt_stats_print, \"stats_print\")\n\t\t\tif (CONF_MATCH(\"stats_print_opts\")) {\n\t\t\t\tinit_opt_stats_print_opts(v, vlen);\n\t\t\t\tCONF_CONTINUE;\n\t\t\t}\n\t\t\tif (config_fill) {\n\t\t\t\tif (CONF_MATCH(\"junk\")) {\n\t\t\t\t\tif (CONF_MATCH_VALUE(\"true\")) {\n\t\t\t\t\t\topt_junk = \"true\";\n\t\t\t\t\t\topt_junk_alloc = opt_junk_free =\n\t\t\t\t\t\t    true;\n\t\t\t\t\t} else if (CONF_MATCH_VALUE(\"false\")) {\n\t\t\t\t\t\topt_junk = \"false\";\n\t\t\t\t\t\topt_junk_alloc = opt_junk_free =\n\t\t\t\t\t\t    false;\n\t\t\t\t\t} else if (CONF_MATCH_VALUE(\"alloc\")) {\n\t\t\t\t\t\topt_junk = \"alloc\";\n\t\t\t\t\t\topt_junk_alloc = true;\n\t\t\t\t\t\topt_junk_free = false;\n\t\t\t\t\t} else if (CONF_MATCH_VALUE(\"free\")) {\n\t\t\t\t\t\topt_junk = \"free\";\n\t\t\t\t\t\topt_junk_alloc = false;\n\t\t\t\t\t\topt_junk_free = true;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tCONF_ERROR(\n\t\t\t\t\t\t    \"Invalid conf value\",\n\t\t\t\t\t\t    k, klen, v, vlen);\n\t\t\t\t\t}\n\t\t\t\t\tCONF_CONTINUE;\n\t\t\t\t}\n\t\t\t\tCONF_HANDLE_BOOL(opt_zero, \"zero\")\n\t\t\t}\n\t\t\tif (config_utrace) {\n\t\t\t\tCONF_HANDLE_BOOL(opt_utrace, \"utrace\")\n\t\t\t}\n\t\t\tif (config_xmalloc) {\n\t\t\t\tCONF_HANDLE_BOOL(opt_xmalloc, \"xmalloc\")\n\t\t\t}\n\t\t\tCONF_HANDLE_BOOL(opt_tcache, \"tcache\")\n\t\t\tCONF_HANDLE_SSIZE_T(opt_lg_tcache_max, \"lg_tcache_max\",\n\t\t\t    -1, (sizeof(size_t) << 3) - 1)\n\n\t\t\t/*\n\t\t\t * The runtime option of oversize_threshold remains\n\t\t\t * undocumented.  It may be tweaked in the next major\n\t\t\t * release (6.0).  The default value 8M is rather\n\t\t\t * conservative / safe.  Tuning it further down may\n\t\t\t * improve fragmentation a bit more, but may also cause\n\t\t\t * contention on the huge arena.\n\t\t\t */\n\t\t\tCONF_HANDLE_SIZE_T(opt_oversize_threshold,\n\t\t\t    \"oversize_threshold\", 0, SC_LARGE_MAXCLASS,\n\t\t\t    CONF_DONT_CHECK_MIN, CONF_CHECK_MAX, false)\n\t\t\tCONF_HANDLE_SIZE_T(opt_lg_extent_max_active_fit,\n\t\t\t    \"lg_extent_max_active_fit\", 0,\n\t\t\t    (sizeof(size_t) << 3), CONF_DONT_CHECK_MIN,\n\t\t\t    CONF_CHECK_MAX, false)\n\n\t\t\tif (strncmp(\"percpu_arena\", k, klen) == 0) {\n\t\t\t\tbool match = false;\n\t\t\t\tfor (int i = percpu_arena_mode_names_base; i <\n\t\t\t\t    percpu_arena_mode_names_limit; i++) {\n\t\t\t\t\tif (strncmp(percpu_arena_mode_names[i],\n\t\t\t\t\t    v, vlen) == 0) {\n\t\t\t\t\t\tif (!have_percpu_arena) {\n\t\t\t\t\t\t\tCONF_ERROR(\n\t\t\t\t\t\t\t    \"No getcpu support\",\n\t\t\t\t\t\t\t    k, klen, v, vlen);\n\t\t\t\t\t\t}\n\t\t\t\t\t\topt_percpu_arena = i;\n\t\t\t\t\t\tmatch = true;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (!match) {\n\t\t\t\t\tCONF_ERROR(\"Invalid conf value\",\n\t\t\t\t\t    k, klen, v, vlen);\n\t\t\t\t}\n\t\t\t\tCONF_CONTINUE;\n\t\t\t}\n\t\t\tCONF_HANDLE_BOOL(opt_background_thread,\n\t\t\t    \"background_thread\");\n\t\t\tCONF_HANDLE_SIZE_T(opt_max_background_threads,\n\t\t\t\t\t   \"max_background_threads\", 1,\n\t\t\t\t\t   opt_max_background_threads,\n\t\t\t\t\t   CONF_CHECK_MIN, CONF_CHECK_MAX,\n\t\t\t\t\t   true);\n\t\t\tif (CONF_MATCH(\"slab_sizes\")) {\n\t\t\t\tbool err;\n\t\t\t\tconst char *slab_size_segment_cur = v;\n\t\t\t\tsize_t vlen_left = vlen;\n\t\t\t\tdo {\n\t\t\t\t\tsize_t slab_start;\n\t\t\t\t\tsize_t slab_end;\n\t\t\t\t\tsize_t pgs;\n\t\t\t\t\terr = malloc_conf_multi_sizes_next(\n\t\t\t\t\t    &slab_size_segment_cur,\n\t\t\t\t\t    &vlen_left, &slab_start, &slab_end,\n\t\t\t\t\t    &pgs);\n\t\t\t\t\tif (!err) {\n\t\t\t\t\t\tsc_data_update_slab_size(\n\t\t\t\t\t\t    sc_data, slab_start,\n\t\t\t\t\t\t    slab_end, (int)pgs);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tCONF_ERROR(\"Invalid settings \"\n\t\t\t\t\t\t    \"for slab_sizes\",\n\t\t\t\t\t\t    k, klen, v, vlen);\n\t\t\t\t\t}\n\t\t\t\t} while (!err && vlen_left > 0);\n\t\t\t\tCONF_CONTINUE;\n\t\t\t}\n\t\t\tif (config_prof) {\n\t\t\t\tCONF_HANDLE_BOOL(opt_prof, \"prof\")\n\t\t\t\tCONF_HANDLE_CHAR_P(opt_prof_prefix,\n\t\t\t\t    \"prof_prefix\", \"jeprof\")\n\t\t\t\tCONF_HANDLE_BOOL(opt_prof_active, \"prof_active\")\n\t\t\t\tCONF_HANDLE_BOOL(opt_prof_thread_active_init,\n\t\t\t\t    \"prof_thread_active_init\")\n\t\t\t\tCONF_HANDLE_SIZE_T(opt_lg_prof_sample,\n\t\t\t\t    \"lg_prof_sample\", 0, (sizeof(uint64_t) << 3)\n\t\t\t\t    - 1, CONF_DONT_CHECK_MIN, CONF_CHECK_MAX,\n\t\t\t\t    true)\n\t\t\t\tCONF_HANDLE_BOOL(opt_prof_accum, \"prof_accum\")\n\t\t\t\tCONF_HANDLE_SSIZE_T(opt_lg_prof_interval,\n\t\t\t\t    \"lg_prof_interval\", -1,\n\t\t\t\t    (sizeof(uint64_t) << 3) - 1)\n\t\t\t\tCONF_HANDLE_BOOL(opt_prof_gdump, \"prof_gdump\")\n\t\t\t\tCONF_HANDLE_BOOL(opt_prof_final, \"prof_final\")\n\t\t\t\tCONF_HANDLE_BOOL(opt_prof_leak, \"prof_leak\")\n\t\t\t\tCONF_HANDLE_BOOL(opt_prof_log, \"prof_log\")\n\t\t\t}\n\t\t\tif (config_log) {\n\t\t\t\tif (CONF_MATCH(\"log\")) {\n\t\t\t\t\tsize_t cpylen = (\n\t\t\t\t\t    vlen <= sizeof(log_var_names) ?\n\t\t\t\t\t    vlen : sizeof(log_var_names) - 1);\n\t\t\t\t\tstrncpy(log_var_names, v, cpylen);\n\t\t\t\t\tlog_var_names[cpylen] = '\\0';\n\t\t\t\t\tCONF_CONTINUE;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (CONF_MATCH(\"thp\")) {\n\t\t\t\tbool match = false;\n\t\t\t\tfor (int i = 0; i < thp_mode_names_limit; i++) {\n\t\t\t\t\tif (strncmp(thp_mode_names[i],v, vlen)\n\t\t\t\t\t    == 0) {\n\t\t\t\t\t\tif (!have_madvise_huge) {\n\t\t\t\t\t\t\tCONF_ERROR(\n\t\t\t\t\t\t\t    \"No THP support\",\n\t\t\t\t\t\t\t    k, klen, v, vlen);\n\t\t\t\t\t\t}\n\t\t\t\t\t\topt_thp = i;\n\t\t\t\t\t\tmatch = true;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (!match) {\n\t\t\t\t\tCONF_ERROR(\"Invalid conf value\",\n\t\t\t\t\t    k, klen, v, vlen);\n\t\t\t\t}\n\t\t\t\tCONF_CONTINUE;\n\t\t\t}\n\t\t\tCONF_ERROR(\"Invalid conf pair\", k, klen, v, vlen);\n#undef CONF_ERROR\n#undef CONF_CONTINUE\n#undef CONF_MATCH\n#undef CONF_MATCH_VALUE\n#undef CONF_HANDLE_BOOL\n#undef CONF_DONT_CHECK_MIN\n#undef CONF_CHECK_MIN\n#undef CONF_DONT_CHECK_MAX\n#undef CONF_CHECK_MAX\n#undef CONF_HANDLE_T_U\n#undef CONF_HANDLE_UNSIGNED\n#undef CONF_HANDLE_SIZE_T\n#undef CONF_HANDLE_SSIZE_T\n#undef CONF_HANDLE_CHAR_P\n    /* Re-enable diagnostic \"-Wtype-limits\" */\n    JEMALLOC_DIAGNOSTIC_POP\n\t\t}\n\t\tif (opt_abort_conf && had_conf_error) {\n\t\t\tmalloc_abort_invalid_conf();\n\t\t}\n\t}\n\tatomic_store_b(&log_init_done, true, ATOMIC_RELEASE);\n}\n\nstatic void\nmalloc_conf_init(sc_data_t *sc_data, unsigned bin_shard_sizes[SC_NBINS]) {\n\tconst char *opts_cache[MALLOC_CONF_NSOURCES] = {NULL, NULL, NULL, NULL};\n\tchar buf[PATH_MAX + 1];\n\n\t/* The first call only set the confirm_conf option and opts_cache */\n\tmalloc_conf_init_helper(NULL, NULL, true, opts_cache, buf);\n\tmalloc_conf_init_helper(sc_data, bin_shard_sizes, false, opts_cache,\n\t    NULL);\n}\n\n#undef MALLOC_CONF_NSOURCES\n\nstatic bool\nmalloc_init_hard_needed(void) {\n\tif (malloc_initialized() || (IS_INITIALIZER && malloc_init_state ==\n\t    malloc_init_recursible)) {\n\t\t/*\n\t\t * Another thread initialized the allocator before this one\n\t\t * acquired init_lock, or this thread is the initializing\n\t\t * thread, and it is recursively allocating.\n\t\t */\n\t\treturn false;\n\t}\n#ifdef JEMALLOC_THREADED_INIT\n\tif (malloc_initializer != NO_INITIALIZER && !IS_INITIALIZER) {\n\t\t/* Busy-wait until the initializing thread completes. */\n\t\tspin_t spinner = SPIN_INITIALIZER;\n\t\tdo {\n\t\t\tmalloc_mutex_unlock(TSDN_NULL, &init_lock);\n\t\t\tspin_adaptive(&spinner);\n\t\t\tmalloc_mutex_lock(TSDN_NULL, &init_lock);\n\t\t} while (!malloc_initialized());\n\t\treturn false;\n\t}\n#endif\n\treturn true;\n}\n\nstatic bool\nmalloc_init_hard_a0_locked() {\n\tmalloc_initializer = INITIALIZER;\n\n\tJEMALLOC_DIAGNOSTIC_PUSH\n\tJEMALLOC_DIAGNOSTIC_IGNORE_MISSING_STRUCT_FIELD_INITIALIZERS\n\tsc_data_t sc_data = {0};\n\tJEMALLOC_DIAGNOSTIC_POP\n\n\t/*\n\t * Ordering here is somewhat tricky; we need sc_boot() first, since that\n\t * determines what the size classes will be, and then\n\t * malloc_conf_init(), since any slab size tweaking will need to be done\n\t * before sz_boot and bin_boot, which assume that the values they read\n\t * out of sc_data_global are final.\n\t */\n\tsc_boot(&sc_data);\n\tunsigned bin_shard_sizes[SC_NBINS];\n\tbin_shard_sizes_boot(bin_shard_sizes);\n\t/*\n\t * prof_boot0 only initializes opt_prof_prefix.  We need to do it before\n\t * we parse malloc_conf options, in case malloc_conf parsing overwrites\n\t * it.\n\t */\n\tif (config_prof) {\n\t\tprof_boot0();\n\t}\n\tmalloc_conf_init(&sc_data, bin_shard_sizes);\n\tsz_boot(&sc_data);\n\tbin_boot(&sc_data, bin_shard_sizes);\n\n\tif (opt_stats_print) {\n\t\t/* Print statistics at exit. */\n\t\tif (atexit(stats_print_atexit) != 0) {\n\t\t\tmalloc_write(\"<jemalloc>: Error in atexit()\\n\");\n\t\t\tif (opt_abort) {\n\t\t\t\tabort();\n\t\t\t}\n\t\t}\n\t}\n\tif (pages_boot()) {\n\t\treturn true;\n\t}\n\tif (base_boot(TSDN_NULL)) {\n\t\treturn true;\n\t}\n\tif (extent_boot()) {\n\t\treturn true;\n\t}\n\tif (ctl_boot()) {\n\t\treturn true;\n\t}\n\tif (config_prof) {\n\t\tprof_boot1();\n\t}\n\tarena_boot(&sc_data);\n\tif (tcache_boot(TSDN_NULL)) {\n\t\treturn true;\n\t}\n\tif (malloc_mutex_init(&arenas_lock, \"arenas\", WITNESS_RANK_ARENAS,\n\t    malloc_mutex_rank_exclusive)) {\n\t\treturn true;\n\t}\n\thook_boot();\n\t/*\n\t * Create enough scaffolding to allow recursive allocation in\n\t * malloc_ncpus().\n\t */\n\tnarenas_auto = 1;\n\tmanual_arena_base = narenas_auto + 1;\n\tmemset(arenas, 0, sizeof(arena_t *) * narenas_auto);\n\t/*\n\t * Initialize one arena here.  The rest are lazily created in\n\t * arena_choose_hard().\n\t */\n\tif (arena_init(TSDN_NULL, 0, (extent_hooks_t *)&extent_hooks_default)\n\t    == NULL) {\n\t\treturn true;\n\t}\n\ta0 = arena_get(TSDN_NULL, 0, false);\n\tmalloc_init_state = malloc_init_a0_initialized;\n\n\treturn false;\n}\n\nstatic bool\nmalloc_init_hard_a0(void) {\n\tbool ret;\n\n\tmalloc_mutex_lock(TSDN_NULL, &init_lock);\n\tret = malloc_init_hard_a0_locked();\n\tmalloc_mutex_unlock(TSDN_NULL, &init_lock);\n\treturn ret;\n}\n\n/* Initialize data structures which may trigger recursive allocation. */\nstatic bool\nmalloc_init_hard_recursible(void) {\n\tmalloc_init_state = malloc_init_recursible;\n\n\tncpus = malloc_ncpus();\n\n#if (defined(JEMALLOC_HAVE_PTHREAD_ATFORK) && !defined(JEMALLOC_MUTEX_INIT_CB) \\\n    && !defined(JEMALLOC_ZONE) && !defined(_WIN32) && \\\n    !defined(__native_client__))\n\t/* LinuxThreads' pthread_atfork() allocates. */\n\tif (pthread_atfork(jemalloc_prefork, jemalloc_postfork_parent,\n\t    jemalloc_postfork_child) != 0) {\n\t\tmalloc_write(\"<jemalloc>: Error in pthread_atfork()\\n\");\n\t\tif (opt_abort) {\n\t\t\tabort();\n\t\t}\n\t\treturn true;\n\t}\n#endif\n\n\tif (background_thread_boot0()) {\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\nstatic unsigned\nmalloc_narenas_default(void) {\n\tassert(ncpus > 0);\n\t/*\n\t * For SMP systems, create more than one arena per CPU by\n\t * default.\n\t */\n\tif (ncpus > 1) {\n\t\treturn ncpus << 2;\n\t} else {\n\t\treturn 1;\n\t}\n}\n\nstatic percpu_arena_mode_t\npercpu_arena_as_initialized(percpu_arena_mode_t mode) {\n\tassert(!malloc_initialized());\n\tassert(mode <= percpu_arena_disabled);\n\n\tif (mode != percpu_arena_disabled) {\n\t\tmode += percpu_arena_mode_enabled_base;\n\t}\n\n\treturn mode;\n}\n\nstatic bool\nmalloc_init_narenas(void) {\n\tassert(ncpus > 0);\n\n\tif (opt_percpu_arena != percpu_arena_disabled) {\n\t\tif (!have_percpu_arena || malloc_getcpu() < 0) {\n\t\t\topt_percpu_arena = percpu_arena_disabled;\n\t\t\tmalloc_printf(\"<jemalloc>: perCPU arena getcpu() not \"\n\t\t\t    \"available. Setting narenas to %u.\\n\", opt_narenas ?\n\t\t\t    opt_narenas : malloc_narenas_default());\n\t\t\tif (opt_abort) {\n\t\t\t\tabort();\n\t\t\t}\n\t\t} else {\n\t\t\tif (ncpus >= MALLOCX_ARENA_LIMIT) {\n\t\t\t\tmalloc_printf(\"<jemalloc>: narenas w/ percpu\"\n\t\t\t\t    \"arena beyond limit (%d)\\n\", ncpus);\n\t\t\t\tif (opt_abort) {\n\t\t\t\t\tabort();\n\t\t\t\t}\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\t/* NB: opt_percpu_arena isn't fully initialized yet. */\n\t\t\tif (percpu_arena_as_initialized(opt_percpu_arena) ==\n\t\t\t    per_phycpu_arena && ncpus % 2 != 0) {\n\t\t\t\tmalloc_printf(\"<jemalloc>: invalid \"\n\t\t\t\t    \"configuration -- per physical CPU arena \"\n\t\t\t\t    \"with odd number (%u) of CPUs (no hyper \"\n\t\t\t\t    \"threading?).\\n\", ncpus);\n\t\t\t\tif (opt_abort)\n\t\t\t\t\tabort();\n\t\t\t}\n\t\t\tunsigned n = percpu_arena_ind_limit(\n\t\t\t    percpu_arena_as_initialized(opt_percpu_arena));\n\t\t\tif (opt_narenas < n) {\n\t\t\t\t/*\n\t\t\t\t * If narenas is specified with percpu_arena\n\t\t\t\t * enabled, actual narenas is set as the greater\n\t\t\t\t * of the two. percpu_arena_choose will be free\n\t\t\t\t * to use any of the arenas based on CPU\n\t\t\t\t * id. This is conservative (at a small cost)\n\t\t\t\t * but ensures correctness.\n\t\t\t\t *\n\t\t\t\t * If for some reason the ncpus determined at\n\t\t\t\t * boot is not the actual number (e.g. because\n\t\t\t\t * of affinity setting from numactl), reserving\n\t\t\t\t * narenas this way provides a workaround for\n\t\t\t\t * percpu_arena.\n\t\t\t\t */\n\t\t\t\topt_narenas = n;\n\t\t\t}\n\t\t}\n\t}\n\tif (opt_narenas == 0) {\n\t\topt_narenas = malloc_narenas_default();\n\t}\n\tassert(opt_narenas > 0);\n\n\tnarenas_auto = opt_narenas;\n\t/*\n\t * Limit the number of arenas to the indexing range of MALLOCX_ARENA().\n\t */\n\tif (narenas_auto >= MALLOCX_ARENA_LIMIT) {\n\t\tnarenas_auto = MALLOCX_ARENA_LIMIT - 1;\n\t\tmalloc_printf(\"<jemalloc>: Reducing narenas to limit (%d)\\n\",\n\t\t    narenas_auto);\n\t}\n\tnarenas_total_set(narenas_auto);\n\tif (arena_init_huge()) {\n\t\tnarenas_total_inc();\n\t}\n\tmanual_arena_base = narenas_total_get();\n\n\treturn false;\n}\n\nstatic void\nmalloc_init_percpu(void) {\n\topt_percpu_arena = percpu_arena_as_initialized(opt_percpu_arena);\n}\n\nstatic bool\nmalloc_init_hard_finish(void) {\n\tif (malloc_mutex_boot()) {\n\t\treturn true;\n\t}\n\n\tmalloc_init_state = malloc_init_initialized;\n\tmalloc_slow_flag_init();\n\n\treturn false;\n}\n\nstatic void\nmalloc_init_hard_cleanup(tsdn_t *tsdn, bool reentrancy_set) {\n\tmalloc_mutex_assert_owner(tsdn, &init_lock);\n\tmalloc_mutex_unlock(tsdn, &init_lock);\n\tif (reentrancy_set) {\n\t\tassert(!tsdn_null(tsdn));\n\t\ttsd_t *tsd = tsdn_tsd(tsdn);\n\t\tassert(tsd_reentrancy_level_get(tsd) > 0);\n\t\tpost_reentrancy(tsd);\n\t}\n}\n\nstatic bool\nmalloc_init_hard(void) {\n\ttsd_t *tsd;\n\n#if defined(_WIN32) && _WIN32_WINNT < 0x0600\n\t_init_init_lock();\n#endif\n\tmalloc_mutex_lock(TSDN_NULL, &init_lock);\n\n#define UNLOCK_RETURN(tsdn, ret, reentrancy)\t\t\\\n\tmalloc_init_hard_cleanup(tsdn, reentrancy);\t\\\n\treturn ret;\n\n\tif (!malloc_init_hard_needed()) {\n\t\tUNLOCK_RETURN(TSDN_NULL, false, false)\n\t}\n\n\tif (malloc_init_state != malloc_init_a0_initialized &&\n\t    malloc_init_hard_a0_locked()) {\n\t\tUNLOCK_RETURN(TSDN_NULL, true, false)\n\t}\n\n\tmalloc_mutex_unlock(TSDN_NULL, &init_lock);\n\t/* Recursive allocation relies on functional tsd. */\n\ttsd = malloc_tsd_boot0();\n\tif (tsd == NULL) {\n\t\treturn true;\n\t}\n\tif (malloc_init_hard_recursible()) {\n\t\treturn true;\n\t}\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &init_lock);\n\t/* Set reentrancy level to 1 during init. */\n\tpre_reentrancy(tsd, NULL);\n\t/* Initialize narenas before prof_boot2 (for allocation). */\n\tif (malloc_init_narenas() || background_thread_boot1(tsd_tsdn(tsd))) {\n\t\tUNLOCK_RETURN(tsd_tsdn(tsd), true, true)\n\t}\n\tif (config_prof && prof_boot2(tsd)) {\n\t\tUNLOCK_RETURN(tsd_tsdn(tsd), true, true)\n\t}\n\n\tmalloc_init_percpu();\n\n\tif (malloc_init_hard_finish()) {\n\t\tUNLOCK_RETURN(tsd_tsdn(tsd), true, true)\n\t}\n\tpost_reentrancy(tsd);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &init_lock);\n\n\twitness_assert_lockless(witness_tsd_tsdn(\n\t    tsd_witness_tsdp_get_unsafe(tsd)));\n\tmalloc_tsd_boot1();\n\t/* Update TSD after tsd_boot1. */\n\ttsd = tsd_fetch();\n\tif (opt_background_thread) {\n\t\tassert(have_background_thread);\n\t\t/*\n\t\t * Need to finish init & unlock first before creating background\n\t\t * threads (pthread_create depends on malloc).  ctl_init (which\n\t\t * sets isthreaded) needs to be called without holding any lock.\n\t\t */\n\t\tbackground_thread_ctl_init(tsd_tsdn(tsd));\n\t\tif (background_thread_create(tsd, 0)) {\n\t\t\treturn true;\n\t\t}\n\t}\n#undef UNLOCK_RETURN\n\treturn false;\n}\n\n/*\n * End initialization functions.\n */\n/******************************************************************************/\n/*\n * Begin allocation-path internal functions and data structures.\n */\n\n/*\n * Settings determined by the documented behavior of the allocation functions.\n */\ntypedef struct static_opts_s static_opts_t;\nstruct static_opts_s {\n\t/* Whether or not allocation size may overflow. */\n\tbool may_overflow;\n\n\t/*\n\t * Whether or not allocations (with alignment) of size 0 should be\n\t * treated as size 1.\n\t */\n\tbool bump_empty_aligned_alloc;\n\t/*\n\t * Whether to assert that allocations are not of size 0 (after any\n\t * bumping).\n\t */\n\tbool assert_nonempty_alloc;\n\n\t/*\n\t * Whether or not to modify the 'result' argument to malloc in case of\n\t * error.\n\t */\n\tbool null_out_result_on_error;\n\t/* Whether to set errno when we encounter an error condition. */\n\tbool set_errno_on_error;\n\n\t/*\n\t * The minimum valid alignment for functions requesting aligned storage.\n\t */\n\tsize_t min_alignment;\n\n\t/* The error string to use if we oom. */\n\tconst char *oom_string;\n\t/* The error string to use if the passed-in alignment is invalid. */\n\tconst char *invalid_alignment_string;\n\n\t/*\n\t * False if we're configured to skip some time-consuming operations.\n\t *\n\t * This isn't really a malloc \"behavior\", but it acts as a useful\n\t * summary of several other static (or at least, static after program\n\t * initialization) options.\n\t */\n\tbool slow;\n\t/*\n\t * Return size.\n\t */\n\tbool usize;\n};\n\nJEMALLOC_ALWAYS_INLINE void\nstatic_opts_init(static_opts_t *static_opts) {\n\tstatic_opts->may_overflow = false;\n\tstatic_opts->bump_empty_aligned_alloc = false;\n\tstatic_opts->assert_nonempty_alloc = false;\n\tstatic_opts->null_out_result_on_error = false;\n\tstatic_opts->set_errno_on_error = false;\n\tstatic_opts->min_alignment = 0;\n\tstatic_opts->oom_string = \"\";\n\tstatic_opts->invalid_alignment_string = \"\";\n\tstatic_opts->slow = false;\n\tstatic_opts->usize = false;\n}\n\n/*\n * These correspond to the macros in jemalloc/jemalloc_macros.h.  Broadly, we\n * should have one constant here per magic value there.  Note however that the\n * representations need not be related.\n */\n#define TCACHE_IND_NONE ((unsigned)-1)\n#define TCACHE_IND_AUTOMATIC ((unsigned)-2)\n#define ARENA_IND_AUTOMATIC ((unsigned)-1)\n\ntypedef struct dynamic_opts_s dynamic_opts_t;\nstruct dynamic_opts_s {\n\tvoid **result;\n\tsize_t usize;\n\tsize_t num_items;\n\tsize_t item_size;\n\tsize_t alignment;\n\tbool zero;\n\tunsigned tcache_ind;\n\tunsigned arena_ind;\n};\n\nJEMALLOC_ALWAYS_INLINE void\ndynamic_opts_init(dynamic_opts_t *dynamic_opts) {\n\tdynamic_opts->result = NULL;\n\tdynamic_opts->usize = 0;\n\tdynamic_opts->num_items = 0;\n\tdynamic_opts->item_size = 0;\n\tdynamic_opts->alignment = 0;\n\tdynamic_opts->zero = false;\n\tdynamic_opts->tcache_ind = TCACHE_IND_AUTOMATIC;\n\tdynamic_opts->arena_ind = ARENA_IND_AUTOMATIC;\n}\n\n/* ind is ignored if dopts->alignment > 0. */\nJEMALLOC_ALWAYS_INLINE void *\nimalloc_no_sample(static_opts_t *sopts, dynamic_opts_t *dopts, tsd_t *tsd,\n    size_t size, size_t usize, szind_t ind) {\n\ttcache_t *tcache;\n\tarena_t *arena;\n\n\t/* Fill in the tcache. */\n\tif (dopts->tcache_ind == TCACHE_IND_AUTOMATIC) {\n\t\tif (likely(!sopts->slow)) {\n\t\t\t/* Getting tcache ptr unconditionally. */\n\t\t\ttcache = tsd_tcachep_get(tsd);\n\t\t\tassert(tcache == tcache_get(tsd));\n\t\t} else {\n\t\t\ttcache = tcache_get(tsd);\n\t\t}\n\t} else if (dopts->tcache_ind == TCACHE_IND_NONE) {\n\t\ttcache = NULL;\n\t} else {\n\t\ttcache = tcaches_get(tsd, dopts->tcache_ind);\n\t}\n\n\t/* Fill in the arena. */\n\tif (dopts->arena_ind == ARENA_IND_AUTOMATIC) {\n\t\t/*\n\t\t * In case of automatic arena management, we defer arena\n\t\t * computation until as late as we can, hoping to fill the\n\t\t * allocation out of the tcache.\n\t\t */\n\t\tarena = NULL;\n\t} else {\n\t\tarena = arena_get(tsd_tsdn(tsd), dopts->arena_ind, true);\n\t}\n\n\tif (unlikely(dopts->alignment != 0)) {\n\t\treturn ipalloct(tsd_tsdn(tsd), usize, dopts->alignment,\n\t\t    dopts->zero, tcache, arena);\n\t}\n\n\treturn iallocztm(tsd_tsdn(tsd), size, ind, dopts->zero, tcache, false,\n\t    arena, sopts->slow);\n}\n\nJEMALLOC_ALWAYS_INLINE void *\nimalloc_sample(static_opts_t *sopts, dynamic_opts_t *dopts, tsd_t *tsd,\n    size_t usize, szind_t ind) {\n\tvoid *ret;\n\n\t/*\n\t * For small allocations, sampling bumps the usize.  If so, we allocate\n\t * from the ind_large bucket.\n\t */\n\tszind_t ind_large;\n\tsize_t bumped_usize = usize;\n\n\tif (usize <= SC_SMALL_MAXCLASS) {\n\t\tassert(((dopts->alignment == 0) ?\n\t\t    sz_s2u(SC_LARGE_MINCLASS) :\n\t\t    sz_sa2u(SC_LARGE_MINCLASS, dopts->alignment))\n\t\t\t== SC_LARGE_MINCLASS);\n\t\tind_large = sz_size2index(SC_LARGE_MINCLASS);\n\t\tbumped_usize = sz_s2u(SC_LARGE_MINCLASS);\n\t\tret = imalloc_no_sample(sopts, dopts, tsd, bumped_usize,\n\t\t    bumped_usize, ind_large);\n\t\tif (unlikely(ret == NULL)) {\n\t\t\treturn NULL;\n\t\t}\n\t\tarena_prof_promote(tsd_tsdn(tsd), ret, usize);\n\t} else {\n\t\tret = imalloc_no_sample(sopts, dopts, tsd, usize, usize, ind);\n\t}\n\n\treturn ret;\n}\n\n/*\n * Returns true if the allocation will overflow, and false otherwise.  Sets\n * *size to the product either way.\n */\nJEMALLOC_ALWAYS_INLINE bool\ncompute_size_with_overflow(bool may_overflow, dynamic_opts_t *dopts,\n    size_t *size) {\n\t/*\n\t * This function is just num_items * item_size, except that we may have\n\t * to check for overflow.\n\t */\n\n\tif (!may_overflow) {\n\t\tassert(dopts->num_items == 1);\n\t\t*size = dopts->item_size;\n\t\treturn false;\n\t}\n\n\t/* A size_t with its high-half bits all set to 1. */\n\tstatic const size_t high_bits = SIZE_T_MAX << (sizeof(size_t) * 8 / 2);\n\n\t*size = dopts->item_size * dopts->num_items;\n\n\tif (unlikely(*size == 0)) {\n\t\treturn (dopts->num_items != 0 && dopts->item_size != 0);\n\t}\n\n\t/*\n\t * We got a non-zero size, but we don't know if we overflowed to get\n\t * there.  To avoid having to do a divide, we'll be clever and note that\n\t * if both A and B can be represented in N/2 bits, then their product\n\t * can be represented in N bits (without the possibility of overflow).\n\t */\n\tif (likely((high_bits & (dopts->num_items | dopts->item_size)) == 0)) {\n\t\treturn false;\n\t}\n\tif (likely(*size / dopts->item_size == dopts->num_items)) {\n\t\treturn false;\n\t}\n\treturn true;\n}\n\nJEMALLOC_ALWAYS_INLINE int\nimalloc_body(static_opts_t *sopts, dynamic_opts_t *dopts, tsd_t *tsd) {\n\t/* Where the actual allocated memory will live. */\n\tvoid *allocation = NULL;\n\t/* Filled in by compute_size_with_overflow below. */\n\tsize_t size = 0;\n\t/*\n\t * For unaligned allocations, we need only ind.  For aligned\n\t * allocations, or in case of stats or profiling we need usize.\n\t *\n\t * These are actually dead stores, in that their values are reset before\n\t * any branch on their value is taken.  Sometimes though, it's\n\t * convenient to pass them as arguments before this point.  To avoid\n\t * undefined behavior then, we initialize them with dummy stores.\n\t */\n\tszind_t ind = 0;\n\tsize_t usize = 0;\n\n\t/* Reentrancy is only checked on slow path. */\n\tint8_t reentrancy_level;\n\n\t/* Compute the amount of memory the user wants. */\n\tif (unlikely(compute_size_with_overflow(sopts->may_overflow, dopts,\n\t    &size))) {\n\t\tgoto label_oom;\n\t}\n\n\tif (unlikely(dopts->alignment < sopts->min_alignment\n\t    || (dopts->alignment & (dopts->alignment - 1)) != 0)) {\n\t\tgoto label_invalid_alignment;\n\t}\n\n\t/* This is the beginning of the \"core\" algorithm. */\n\n\tif (dopts->alignment == 0) {\n\t\tind = sz_size2index(size);\n\t\tif (unlikely(ind >= SC_NSIZES)) {\n\t\t\tgoto label_oom;\n\t\t}\n\t\tif (config_stats || (config_prof && opt_prof) || sopts->usize) {\n\t\t\tusize = sz_index2size(ind);\n\t\t\tdopts->usize = usize;\n\t\t\tassert(usize > 0 && usize\n\t\t\t    <= SC_LARGE_MAXCLASS);\n\t\t}\n\t} else {\n\t\tif (sopts->bump_empty_aligned_alloc) {\n\t\t\tif (unlikely(size == 0)) {\n\t\t\t\tsize = 1;\n\t\t\t}\n\t\t}\n\t\tusize = sz_sa2u(size, dopts->alignment);\n\t\tdopts->usize = usize;\n\t\tif (unlikely(usize == 0\n\t\t    || usize > SC_LARGE_MAXCLASS)) {\n\t\t\tgoto label_oom;\n\t\t}\n\t}\n\t/* Validate the user input. */\n\tif (sopts->assert_nonempty_alloc) {\n\t\tassert (size != 0);\n\t}\n\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\n\t/*\n\t * If we need to handle reentrancy, we can do it out of a\n\t * known-initialized arena (i.e. arena 0).\n\t */\n\treentrancy_level = tsd_reentrancy_level_get(tsd);\n\tif (sopts->slow && unlikely(reentrancy_level > 0)) {\n\t\t/*\n\t\t * We should never specify particular arenas or tcaches from\n\t\t * within our internal allocations.\n\t\t */\n\t\tassert(dopts->tcache_ind == TCACHE_IND_AUTOMATIC ||\n\t\t    dopts->tcache_ind == TCACHE_IND_NONE);\n\t\tassert(dopts->arena_ind == ARENA_IND_AUTOMATIC);\n\t\tdopts->tcache_ind = TCACHE_IND_NONE;\n\t\t/* We know that arena 0 has already been initialized. */\n\t\tdopts->arena_ind = 0;\n\t}\n\n\t/* If profiling is on, get our profiling context. */\n\tif (config_prof && opt_prof) {\n\t\t/*\n\t\t * Note that if we're going down this path, usize must have been\n\t\t * initialized in the previous if statement.\n\t\t */\n\t\tprof_tctx_t *tctx = prof_alloc_prep(\n\t\t    tsd, usize, prof_active_get_unlocked(), true);\n\n\t\talloc_ctx_t alloc_ctx;\n\t\tif (likely((uintptr_t)tctx == (uintptr_t)1U)) {\n\t\t\talloc_ctx.slab = (usize\n\t\t\t    <= SC_SMALL_MAXCLASS);\n\t\t\tallocation = imalloc_no_sample(\n\t\t\t    sopts, dopts, tsd, usize, usize, ind);\n\t\t} else if ((uintptr_t)tctx > (uintptr_t)1U) {\n\t\t\t/*\n\t\t\t * Note that ind might still be 0 here.  This is fine;\n\t\t\t * imalloc_sample ignores ind if dopts->alignment > 0.\n\t\t\t */\n\t\t\tallocation = imalloc_sample(\n\t\t\t    sopts, dopts, tsd, usize, ind);\n\t\t\talloc_ctx.slab = false;\n\t\t} else {\n\t\t\tallocation = NULL;\n\t\t}\n\n\t\tif (unlikely(allocation == NULL)) {\n\t\t\tprof_alloc_rollback(tsd, tctx, true);\n\t\t\tgoto label_oom;\n\t\t}\n\t\tprof_malloc(tsd_tsdn(tsd), allocation, usize, &alloc_ctx, tctx);\n\t} else {\n\t\t/*\n\t\t * If dopts->alignment > 0, then ind is still 0, but usize was\n\t\t * computed in the previous if statement.  Down the positive\n\t\t * alignment path, imalloc_no_sample ignores ind and size\n\t\t * (relying only on usize).\n\t\t */\n\t\tallocation = imalloc_no_sample(sopts, dopts, tsd, size, usize,\n\t\t    ind);\n\t\tif (unlikely(allocation == NULL)) {\n\t\t\tgoto label_oom;\n\t\t}\n\t}\n\n\t/*\n\t * Allocation has been done at this point.  We still have some\n\t * post-allocation work to do though.\n\t */\n\tassert(dopts->alignment == 0\n\t    || ((uintptr_t)allocation & (dopts->alignment - 1)) == ZU(0));\n\n\tif (config_stats) {\n\t\tassert(usize == isalloc(tsd_tsdn(tsd), allocation));\n\t\t*tsd_thread_allocatedp_get(tsd) += usize;\n\t}\n\n\tif (sopts->slow) {\n\t\tUTRACE(0, size, allocation);\n\t}\n\n\t/* Success! */\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\t*dopts->result = allocation;\n\treturn 0;\n\nlabel_oom:\n\tif (unlikely(sopts->slow) && config_xmalloc && unlikely(opt_xmalloc)) {\n\t\tmalloc_write(sopts->oom_string);\n\t\tabort();\n\t}\n\n\tif (sopts->slow) {\n\t\tUTRACE(NULL, size, NULL);\n\t}\n\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\n\tif (sopts->set_errno_on_error) {\n\t\tset_errno(ENOMEM);\n\t}\n\n\tif (sopts->null_out_result_on_error) {\n\t\t*dopts->result = NULL;\n\t}\n\n\treturn ENOMEM;\n\n\t/*\n\t * This label is only jumped to by one goto; we move it out of line\n\t * anyways to avoid obscuring the non-error paths, and for symmetry with\n\t * the oom case.\n\t */\nlabel_invalid_alignment:\n\tif (config_xmalloc && unlikely(opt_xmalloc)) {\n\t\tmalloc_write(sopts->invalid_alignment_string);\n\t\tabort();\n\t}\n\n\tif (sopts->set_errno_on_error) {\n\t\tset_errno(EINVAL);\n\t}\n\n\tif (sopts->slow) {\n\t\tUTRACE(NULL, size, NULL);\n\t}\n\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\n\tif (sopts->null_out_result_on_error) {\n\t\t*dopts->result = NULL;\n\t}\n\n\treturn EINVAL;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\nimalloc_init_check(static_opts_t *sopts, dynamic_opts_t *dopts) {\n\tif (unlikely(!malloc_initialized()) && unlikely(malloc_init())) {\n\t\tif (config_xmalloc && unlikely(opt_xmalloc)) {\n\t\t\tmalloc_write(sopts->oom_string);\n\t\t\tabort();\n\t\t}\n\t\tUTRACE(NULL, dopts->num_items * dopts->item_size, NULL);\n\t\tset_errno(ENOMEM);\n\t\t*dopts->result = NULL;\n\n\t\treturn false;\n\t}\n\n\treturn true;\n}\n\n/* Returns the errno-style error code of the allocation. */\nJEMALLOC_ALWAYS_INLINE int\nimalloc(static_opts_t *sopts, dynamic_opts_t *dopts) {\n\tif (tsd_get_allocates() && !imalloc_init_check(sopts, dopts)) {\n\t\treturn ENOMEM;\n\t}\n\n\t/* We always need the tsd.  Let's grab it right away. */\n\ttsd_t *tsd = tsd_fetch();\n\tassert(tsd);\n\tif (likely(tsd_fast(tsd))) {\n\t\t/* Fast and common path. */\n\t\ttsd_assert_fast(tsd);\n\t\tsopts->slow = false;\n\t\treturn imalloc_body(sopts, dopts, tsd);\n\t} else {\n\t\tif (!tsd_get_allocates() && !imalloc_init_check(sopts, dopts)) {\n\t\t\treturn ENOMEM;\n\t\t}\n\n\t\tsopts->slow = true;\n\t\treturn imalloc_body(sopts, dopts, tsd);\n\t}\n}\n\nJEMALLOC_NOINLINE\nvoid *\nmalloc_default(size_t size) {\n\tvoid *ret;\n\tstatic_opts_t sopts;\n\tdynamic_opts_t dopts;\n\n\tLOG(\"core.malloc.entry\", \"size: %zu\", size);\n\n\tstatic_opts_init(&sopts);\n\tdynamic_opts_init(&dopts);\n\n\tsopts.null_out_result_on_error = true;\n\tsopts.set_errno_on_error = true;\n\tsopts.oom_string = \"<jemalloc>: Error in malloc(): out of memory\\n\";\n\n\tdopts.result = &ret;\n\tdopts.num_items = 1;\n\tdopts.item_size = size;\n\n\timalloc(&sopts, &dopts);\n\t/*\n\t * Note that this branch gets optimized away -- it immediately follows\n\t * the check on tsd_fast that sets sopts.slow.\n\t */\n\tif (sopts.slow) {\n\t\tuintptr_t args[3] = {size};\n\t\thook_invoke_alloc(hook_alloc_malloc, ret, (uintptr_t)ret, args);\n\t}\n\n\tLOG(\"core.malloc.exit\", \"result: %p\", ret);\n\n\treturn ret;\n}\n\n/******************************************************************************/\n/*\n * Begin malloc(3)-compatible functions.\n */\n\n/*\n * malloc() fastpath.\n *\n * Fastpath assumes size <= SC_LOOKUP_MAXCLASS, and that we hit\n * tcache.  If either of these is false, we tail-call to the slowpath,\n * malloc_default().  Tail-calling is used to avoid any caller-saved\n * registers.\n *\n * fastpath supports ticker and profiling, both of which will also\n * tail-call to the slowpath if they fire.\n */\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\nvoid JEMALLOC_NOTHROW *\nJEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE(1)\nje_malloc(size_t size) {\n\tLOG(\"core.malloc.entry\", \"size: %zu\", size);\n\n\tif (tsd_get_allocates() && unlikely(!malloc_initialized())) {\n\t\treturn malloc_default(size);\n\t}\n\n\ttsd_t *tsd = tsd_get(false);\n\tif (unlikely(!tsd || !tsd_fast(tsd) || (size > SC_LOOKUP_MAXCLASS))) {\n\t\treturn malloc_default(size);\n\t}\n\n\ttcache_t *tcache = tsd_tcachep_get(tsd);\n\n\tif (unlikely(ticker_trytick(&tcache->gc_ticker))) {\n\t\treturn malloc_default(size);\n\t}\n\n\tszind_t ind = sz_size2index_lookup(size);\n\tsize_t usize;\n\tif (config_stats || config_prof) {\n\t\tusize = sz_index2size(ind);\n\t}\n\t/* Fast path relies on size being a bin. I.e. SC_LOOKUP_MAXCLASS < SC_SMALL_MAXCLASS */\n\tassert(ind < SC_NBINS);\n\tassert(size <= SC_SMALL_MAXCLASS);\n\n\tif (config_prof) {\n\t\tint64_t bytes_until_sample = tsd_bytes_until_sample_get(tsd);\n\t\tbytes_until_sample -= usize;\n\t\ttsd_bytes_until_sample_set(tsd, bytes_until_sample);\n\n\t\tif (unlikely(bytes_until_sample < 0)) {\n\t\t\t/*\n\t\t\t * Avoid a prof_active check on the fastpath.\n\t\t\t * If prof_active is false, set bytes_until_sample to\n\t\t\t * a large value.  If prof_active is set to true,\n\t\t\t * bytes_until_sample will be reset.\n\t\t\t */\n\t\t\tif (!prof_active) {\n\t\t\t\ttsd_bytes_until_sample_set(tsd, SSIZE_MAX);\n\t\t\t}\n\t\t\treturn malloc_default(size);\n\t\t}\n\t}\n\n\tcache_bin_t *bin = tcache_small_bin_get(tcache, ind);\n\tbool tcache_success;\n\tvoid* ret = cache_bin_alloc_easy(bin, &tcache_success);\n\n\tif (tcache_success) {\n\t\tif (config_stats) {\n\t\t\t*tsd_thread_allocatedp_get(tsd) += usize;\n\t\t\tbin->tstats.nrequests++;\n\t\t}\n\t\tif (config_prof) {\n\t\t\ttcache->prof_accumbytes += usize;\n\t\t}\n\n\t\tLOG(\"core.malloc.exit\", \"result: %p\", ret);\n\n\t\t/* Fastpath success */\n\t\treturn ret;\n\t}\n\n\treturn malloc_default(size);\n}\n\nJEMALLOC_EXPORT int JEMALLOC_NOTHROW\nJEMALLOC_ATTR(nonnull(1))\nje_posix_memalign(void **memptr, size_t alignment, size_t size) {\n\tint ret;\n\tstatic_opts_t sopts;\n\tdynamic_opts_t dopts;\n\n\tLOG(\"core.posix_memalign.entry\", \"mem ptr: %p, alignment: %zu, \"\n\t    \"size: %zu\", memptr, alignment, size);\n\n\tstatic_opts_init(&sopts);\n\tdynamic_opts_init(&dopts);\n\n\tsopts.bump_empty_aligned_alloc = true;\n\tsopts.min_alignment = sizeof(void *);\n\tsopts.oom_string =\n\t    \"<jemalloc>: Error allocating aligned memory: out of memory\\n\";\n\tsopts.invalid_alignment_string =\n\t    \"<jemalloc>: Error allocating aligned memory: invalid alignment\\n\";\n\n\tdopts.result = memptr;\n\tdopts.num_items = 1;\n\tdopts.item_size = size;\n\tdopts.alignment = alignment;\n\n\tret = imalloc(&sopts, &dopts);\n\tif (sopts.slow) {\n\t\tuintptr_t args[3] = {(uintptr_t)memptr, (uintptr_t)alignment,\n\t\t\t(uintptr_t)size};\n\t\thook_invoke_alloc(hook_alloc_posix_memalign, *memptr,\n\t\t    (uintptr_t)ret, args);\n\t}\n\n\tLOG(\"core.posix_memalign.exit\", \"result: %d, alloc ptr: %p\", ret,\n\t    *memptr);\n\n\treturn ret;\n}\n\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\nvoid JEMALLOC_NOTHROW *\nJEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE(2)\nje_aligned_alloc(size_t alignment, size_t size) {\n\tvoid *ret;\n\n\tstatic_opts_t sopts;\n\tdynamic_opts_t dopts;\n\n\tLOG(\"core.aligned_alloc.entry\", \"alignment: %zu, size: %zu\\n\",\n\t    alignment, size);\n\n\tstatic_opts_init(&sopts);\n\tdynamic_opts_init(&dopts);\n\n\tsopts.bump_empty_aligned_alloc = true;\n\tsopts.null_out_result_on_error = true;\n\tsopts.set_errno_on_error = true;\n\tsopts.min_alignment = 1;\n\tsopts.oom_string =\n\t    \"<jemalloc>: Error allocating aligned memory: out of memory\\n\";\n\tsopts.invalid_alignment_string =\n\t    \"<jemalloc>: Error allocating aligned memory: invalid alignment\\n\";\n\n\tdopts.result = &ret;\n\tdopts.num_items = 1;\n\tdopts.item_size = size;\n\tdopts.alignment = alignment;\n\n\timalloc(&sopts, &dopts);\n\tif (sopts.slow) {\n\t\tuintptr_t args[3] = {(uintptr_t)alignment, (uintptr_t)size};\n\t\thook_invoke_alloc(hook_alloc_aligned_alloc, ret,\n\t\t    (uintptr_t)ret, args);\n\t}\n\n\tLOG(\"core.aligned_alloc.exit\", \"result: %p\", ret);\n\n\treturn ret;\n}\n\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\nvoid JEMALLOC_NOTHROW *\nJEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE2(1, 2)\nje_calloc(size_t num, size_t size) {\n\tvoid *ret;\n\tstatic_opts_t sopts;\n\tdynamic_opts_t dopts;\n\n\tLOG(\"core.calloc.entry\", \"num: %zu, size: %zu\\n\", num, size);\n\n\tstatic_opts_init(&sopts);\n\tdynamic_opts_init(&dopts);\n\n\tsopts.may_overflow = true;\n\tsopts.null_out_result_on_error = true;\n\tsopts.set_errno_on_error = true;\n\tsopts.oom_string = \"<jemalloc>: Error in calloc(): out of memory\\n\";\n\n\tdopts.result = &ret;\n\tdopts.num_items = num;\n\tdopts.item_size = size;\n\tdopts.zero = true;\n\n\timalloc(&sopts, &dopts);\n\tif (sopts.slow) {\n\t\tuintptr_t args[3] = {(uintptr_t)num, (uintptr_t)size};\n\t\thook_invoke_alloc(hook_alloc_calloc, ret, (uintptr_t)ret, args);\n\t}\n\n\tLOG(\"core.calloc.exit\", \"result: %p\", ret);\n\n\treturn ret;\n}\n\nstatic void *\nirealloc_prof_sample(tsd_t *tsd, void *old_ptr, size_t old_usize, size_t usize,\n    prof_tctx_t *tctx, hook_ralloc_args_t *hook_args) {\n\tvoid *p;\n\n\tif (tctx == NULL) {\n\t\treturn NULL;\n\t}\n\tif (usize <= SC_SMALL_MAXCLASS) {\n\t\tp = iralloc(tsd, old_ptr, old_usize,\n\t\t    SC_LARGE_MINCLASS, 0, false, hook_args);\n\t\tif (p == NULL) {\n\t\t\treturn NULL;\n\t\t}\n\t\tarena_prof_promote(tsd_tsdn(tsd), p, usize);\n\t} else {\n\t\tp = iralloc(tsd, old_ptr, old_usize, usize, 0, false,\n\t\t    hook_args);\n\t}\n\n\treturn p;\n}\n\nJEMALLOC_ALWAYS_INLINE void *\nirealloc_prof(tsd_t *tsd, void *old_ptr, size_t old_usize, size_t usize,\n   alloc_ctx_t *alloc_ctx, hook_ralloc_args_t *hook_args) {\n\tvoid *p;\n\tbool prof_active;\n\tprof_tctx_t *old_tctx, *tctx;\n\n\tprof_active = prof_active_get_unlocked();\n\told_tctx = prof_tctx_get(tsd_tsdn(tsd), old_ptr, alloc_ctx);\n\ttctx = prof_alloc_prep(tsd, usize, prof_active, true);\n\tif (unlikely((uintptr_t)tctx != (uintptr_t)1U)) {\n\t\tp = irealloc_prof_sample(tsd, old_ptr, old_usize, usize, tctx,\n\t\t    hook_args);\n\t} else {\n\t\tp = iralloc(tsd, old_ptr, old_usize, usize, 0, false,\n\t\t    hook_args);\n\t}\n\tif (unlikely(p == NULL)) {\n\t\tprof_alloc_rollback(tsd, tctx, true);\n\t\treturn NULL;\n\t}\n\tprof_realloc(tsd, p, usize, tctx, prof_active, true, old_ptr, old_usize,\n\t    old_tctx);\n\n\treturn p;\n}\n\nJEMALLOC_ALWAYS_INLINE void\nifree(tsd_t *tsd, void *ptr, tcache_t *tcache, bool slow_path) {\n\tif (!slow_path) {\n\t\ttsd_assert_fast(tsd);\n\t}\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\tif (tsd_reentrancy_level_get(tsd) != 0) {\n\t\tassert(slow_path);\n\t}\n\n\tassert(ptr != NULL);\n\tassert(malloc_initialized() || IS_INITIALIZER);\n\n\talloc_ctx_t alloc_ctx;\n\trtree_ctx_t *rtree_ctx = tsd_rtree_ctx(tsd);\n\trtree_szind_slab_read(tsd_tsdn(tsd), &extents_rtree, rtree_ctx,\n\t    (uintptr_t)ptr, true, &alloc_ctx.szind, &alloc_ctx.slab);\n\tassert(alloc_ctx.szind != SC_NSIZES);\n\n\tsize_t usize;\n\tif (config_prof && opt_prof) {\n\t\tusize = sz_index2size(alloc_ctx.szind);\n\t\tprof_free(tsd, ptr, usize, &alloc_ctx);\n\t} else if (config_stats) {\n\t\tusize = sz_index2size(alloc_ctx.szind);\n\t}\n\tif (config_stats) {\n\t\t*tsd_thread_deallocatedp_get(tsd) += usize;\n\t}\n\n\tif (likely(!slow_path)) {\n\t\tidalloctm(tsd_tsdn(tsd), ptr, tcache, &alloc_ctx, false,\n\t\t    false);\n\t} else {\n\t\tidalloctm(tsd_tsdn(tsd), ptr, tcache, &alloc_ctx, false,\n\t\t    true);\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE void\nisfree(tsd_t *tsd, void *ptr, size_t usize, tcache_t *tcache, bool slow_path) {\n\tif (!slow_path) {\n\t\ttsd_assert_fast(tsd);\n\t}\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\tif (tsd_reentrancy_level_get(tsd) != 0) {\n\t\tassert(slow_path);\n\t}\n\n\tassert(ptr != NULL);\n\tassert(malloc_initialized() || IS_INITIALIZER);\n\n\talloc_ctx_t alloc_ctx, *ctx;\n\tif (!config_cache_oblivious && ((uintptr_t)ptr & PAGE_MASK) != 0) {\n\t\t/*\n\t\t * When cache_oblivious is disabled and ptr is not page aligned,\n\t\t * the allocation was not sampled -- usize can be used to\n\t\t * determine szind directly.\n\t\t */\n\t\talloc_ctx.szind = sz_size2index(usize);\n\t\talloc_ctx.slab = true;\n\t\tctx = &alloc_ctx;\n\t\tif (config_debug) {\n\t\t\talloc_ctx_t dbg_ctx;\n\t\t\trtree_ctx_t *rtree_ctx = tsd_rtree_ctx(tsd);\n\t\t\trtree_szind_slab_read(tsd_tsdn(tsd), &extents_rtree,\n\t\t\t    rtree_ctx, (uintptr_t)ptr, true, &dbg_ctx.szind,\n\t\t\t    &dbg_ctx.slab);\n\t\t\tassert(dbg_ctx.szind == alloc_ctx.szind);\n\t\t\tassert(dbg_ctx.slab == alloc_ctx.slab);\n\t\t}\n\t} else if (config_prof && opt_prof) {\n\t\trtree_ctx_t *rtree_ctx = tsd_rtree_ctx(tsd);\n\t\trtree_szind_slab_read(tsd_tsdn(tsd), &extents_rtree, rtree_ctx,\n\t\t    (uintptr_t)ptr, true, &alloc_ctx.szind, &alloc_ctx.slab);\n\t\tassert(alloc_ctx.szind == sz_size2index(usize));\n\t\tctx = &alloc_ctx;\n\t} else {\n\t\tctx = NULL;\n\t}\n\n\tif (config_prof && opt_prof) {\n\t\tprof_free(tsd, ptr, usize, ctx);\n\t}\n\tif (config_stats) {\n\t\t*tsd_thread_deallocatedp_get(tsd) += usize;\n\t}\n\n\tif (likely(!slow_path)) {\n\t\tisdalloct(tsd_tsdn(tsd), ptr, usize, tcache, ctx, false);\n\t} else {\n\t\tisdalloct(tsd_tsdn(tsd), ptr, usize, tcache, ctx, true);\n\t}\n}\n\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\nvoid JEMALLOC_NOTHROW *\nJEMALLOC_ALLOC_SIZE(2)\nje_realloc(void *ptr, size_t arg_size) {\n\tvoid *ret;\n\ttsdn_t *tsdn JEMALLOC_CC_SILENCE_INIT(NULL);\n\tsize_t usize JEMALLOC_CC_SILENCE_INIT(0);\n\tsize_t old_usize = 0;\n\tsize_t size = arg_size;\n\n\tLOG(\"core.realloc.entry\", \"ptr: %p, size: %zu\\n\", ptr, size);\n\n\tif (unlikely(size == 0)) {\n\t\tif (ptr != NULL) {\n\t\t\t/* realloc(ptr, 0) is equivalent to free(ptr). */\n\t\t\tUTRACE(ptr, 0, 0);\n\t\t\ttcache_t *tcache;\n\t\t\ttsd_t *tsd = tsd_fetch();\n\t\t\tif (tsd_reentrancy_level_get(tsd) == 0) {\n\t\t\t\ttcache = tcache_get(tsd);\n\t\t\t} else {\n\t\t\t\ttcache = NULL;\n\t\t\t}\n\n\t\t\tuintptr_t args[3] = {(uintptr_t)ptr, size};\n\t\t\thook_invoke_dalloc(hook_dalloc_realloc, ptr, args);\n\n\t\t\tifree(tsd, ptr, tcache, true);\n\n\t\t\tLOG(\"core.realloc.exit\", \"result: %p\", NULL);\n\t\t\treturn NULL;\n\t\t}\n\t\tsize = 1;\n\t}\n\n\tif (likely(ptr != NULL)) {\n\t\tassert(malloc_initialized() || IS_INITIALIZER);\n\t\ttsd_t *tsd = tsd_fetch();\n\n\t\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\n\n\t\thook_ralloc_args_t hook_args = {true, {(uintptr_t)ptr,\n\t\t\t(uintptr_t)arg_size, 0, 0}};\n\n\t\talloc_ctx_t alloc_ctx;\n\t\trtree_ctx_t *rtree_ctx = tsd_rtree_ctx(tsd);\n\t\trtree_szind_slab_read(tsd_tsdn(tsd), &extents_rtree, rtree_ctx,\n\t\t    (uintptr_t)ptr, true, &alloc_ctx.szind, &alloc_ctx.slab);\n\t\tassert(alloc_ctx.szind != SC_NSIZES);\n\t\told_usize = sz_index2size(alloc_ctx.szind);\n\t\tassert(old_usize == isalloc(tsd_tsdn(tsd), ptr));\n\t\tif (config_prof && opt_prof) {\n\t\t\tusize = sz_s2u(size);\n\t\t\tif (unlikely(usize == 0\n\t\t\t    || usize > SC_LARGE_MAXCLASS)) {\n\t\t\t\tret = NULL;\n\t\t\t} else {\n\t\t\t\tret = irealloc_prof(tsd, ptr, old_usize, usize,\n\t\t\t\t    &alloc_ctx, &hook_args);\n\t\t\t}\n\t\t} else {\n\t\t\tif (config_stats) {\n\t\t\t\tusize = sz_s2u(size);\n\t\t\t}\n\t\t\tret = iralloc(tsd, ptr, old_usize, size, 0, false,\n\t\t\t    &hook_args);\n\t\t}\n\t\ttsdn = tsd_tsdn(tsd);\n\t} else {\n\t\t/* realloc(NULL, size) is equivalent to malloc(size). */\n\t\tstatic_opts_t sopts;\n\t\tdynamic_opts_t dopts;\n\n\t\tstatic_opts_init(&sopts);\n\t\tdynamic_opts_init(&dopts);\n\n\t\tsopts.null_out_result_on_error = true;\n\t\tsopts.set_errno_on_error = true;\n\t\tsopts.oom_string =\n\t\t    \"<jemalloc>: Error in realloc(): out of memory\\n\";\n\n\t\tdopts.result = &ret;\n\t\tdopts.num_items = 1;\n\t\tdopts.item_size = size;\n\n\t\timalloc(&sopts, &dopts);\n\t\tif (sopts.slow) {\n\t\t\tuintptr_t args[3] = {(uintptr_t)ptr, arg_size};\n\t\t\thook_invoke_alloc(hook_alloc_realloc, ret,\n\t\t\t    (uintptr_t)ret, args);\n\t\t}\n\n\t\treturn ret;\n\t}\n\n\tif (unlikely(ret == NULL)) {\n\t\tif (config_xmalloc && unlikely(opt_xmalloc)) {\n\t\t\tmalloc_write(\"<jemalloc>: Error in realloc(): \"\n\t\t\t    \"out of memory\\n\");\n\t\t\tabort();\n\t\t}\n\t\tset_errno(ENOMEM);\n\t}\n\tif (config_stats && likely(ret != NULL)) {\n\t\ttsd_t *tsd;\n\n\t\tassert(usize == isalloc(tsdn, ret));\n\t\ttsd = tsdn_tsd(tsdn);\n\t\t*tsd_thread_allocatedp_get(tsd) += usize;\n\t\t*tsd_thread_deallocatedp_get(tsd) += old_usize;\n\t}\n\tUTRACE(ptr, size, ret);\n\tcheck_entry_exit_locking(tsdn);\n\n\tLOG(\"core.realloc.exit\", \"result: %p\", ret);\n\treturn ret;\n}\n\nJEMALLOC_NOINLINE\nvoid\nfree_default(void *ptr) {\n\tUTRACE(ptr, 0, 0);\n\tif (likely(ptr != NULL)) {\n\t\t/*\n\t\t * We avoid setting up tsd fully (e.g. tcache, arena binding)\n\t\t * based on only free() calls -- other activities trigger the\n\t\t * minimal to full transition.  This is because free() may\n\t\t * happen during thread shutdown after tls deallocation: if a\n\t\t * thread never had any malloc activities until then, a\n\t\t * fully-setup tsd won't be destructed properly.\n\t\t */\n\t\ttsd_t *tsd = tsd_fetch_min();\n\t\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\n\t\ttcache_t *tcache;\n\t\tif (likely(tsd_fast(tsd))) {\n\t\t\ttsd_assert_fast(tsd);\n\t\t\t/* Unconditionally get tcache ptr on fast path. */\n\t\t\ttcache = tsd_tcachep_get(tsd);\n\t\t\tifree(tsd, ptr, tcache, false);\n\t\t} else {\n\t\t\tif (likely(tsd_reentrancy_level_get(tsd) == 0)) {\n\t\t\t\ttcache = tcache_get(tsd);\n\t\t\t} else {\n\t\t\t\ttcache = NULL;\n\t\t\t}\n\t\t\tuintptr_t args_raw[3] = {(uintptr_t)ptr};\n\t\t\thook_invoke_dalloc(hook_dalloc_free, ptr, args_raw);\n\t\t\tifree(tsd, ptr, tcache, true);\n\t\t}\n\t\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE\nbool free_fastpath(void *ptr, size_t size, bool size_hint) {\n\ttsd_t *tsd = tsd_get(false);\n\tif (unlikely(!tsd || !tsd_fast(tsd))) {\n\t\treturn false;\n\t}\n\n\ttcache_t *tcache = tsd_tcachep_get(tsd);\n\n\talloc_ctx_t alloc_ctx;\n\t/*\n\t * If !config_cache_oblivious, we can check PAGE alignment to\n\t * detect sampled objects.  Otherwise addresses are\n\t * randomized, and we have to look it up in the rtree anyway.\n\t * See also isfree().\n\t */\n\tif (!size_hint || config_cache_oblivious) {\n\t\trtree_ctx_t *rtree_ctx = tsd_rtree_ctx(tsd);\n\t\tbool res = rtree_szind_slab_read_fast(tsd_tsdn(tsd), &extents_rtree,\n\t\t\t\t\t\t      rtree_ctx, (uintptr_t)ptr,\n\t\t\t\t\t\t      &alloc_ctx.szind, &alloc_ctx.slab);\n\n\t\t/* Note: profiled objects will have alloc_ctx.slab set */\n\t\tif (!res || !alloc_ctx.slab) {\n\t\t\treturn false;\n\t\t}\n\t\tassert(alloc_ctx.szind != SC_NSIZES);\n\t} else {\n\t\t/*\n\t\t * Check for both sizes that are too large, and for sampled objects.\n\t\t * Sampled objects are always page-aligned.  The sampled object check\n\t\t * will also check for null ptr.\n\t\t */\n\t\tif (size > SC_LOOKUP_MAXCLASS || (((uintptr_t)ptr & PAGE_MASK) == 0)) {\n\t\t\treturn false;\n\t\t}\n\t\talloc_ctx.szind = sz_size2index_lookup(size);\n\t}\n\n\tif (unlikely(ticker_trytick(&tcache->gc_ticker))) {\n\t\treturn false;\n\t}\n\n\tcache_bin_t *bin = tcache_small_bin_get(tcache, alloc_ctx.szind);\n\tcache_bin_info_t *bin_info = &tcache_bin_info[alloc_ctx.szind];\n\tif (!cache_bin_dalloc_easy(bin, bin_info, ptr)) {\n\t\treturn false;\n\t}\n\n\tif (config_stats) {\n\t\tsize_t usize = sz_index2size(alloc_ctx.szind);\n\t\t*tsd_thread_deallocatedp_get(tsd) += usize;\n\t}\n\n\treturn true;\n}\n\nJEMALLOC_EXPORT void JEMALLOC_NOTHROW\nje_free(void *ptr) {\n\tLOG(\"core.free.entry\", \"ptr: %p\", ptr);\n\n\tif (!free_fastpath(ptr, 0, false)) {\n\t\tfree_default(ptr);\n\t}\n\n\tLOG(\"core.free.exit\", \"\");\n}\n\n/*\n * End malloc(3)-compatible functions.\n */\n/******************************************************************************/\n/*\n * Begin non-standard override functions.\n */\n\n#ifdef JEMALLOC_OVERRIDE_MEMALIGN\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\nvoid JEMALLOC_NOTHROW *\nJEMALLOC_ATTR(malloc)\nje_memalign(size_t alignment, size_t size) {\n\tvoid *ret;\n\tstatic_opts_t sopts;\n\tdynamic_opts_t dopts;\n\n\tLOG(\"core.memalign.entry\", \"alignment: %zu, size: %zu\\n\", alignment,\n\t    size);\n\n\tstatic_opts_init(&sopts);\n\tdynamic_opts_init(&dopts);\n\n\tsopts.min_alignment = 1;\n\tsopts.oom_string =\n\t    \"<jemalloc>: Error allocating aligned memory: out of memory\\n\";\n\tsopts.invalid_alignment_string =\n\t    \"<jemalloc>: Error allocating aligned memory: invalid alignment\\n\";\n\tsopts.null_out_result_on_error = true;\n\n\tdopts.result = &ret;\n\tdopts.num_items = 1;\n\tdopts.item_size = size;\n\tdopts.alignment = alignment;\n\n\timalloc(&sopts, &dopts);\n\tif (sopts.slow) {\n\t\tuintptr_t args[3] = {alignment, size};\n\t\thook_invoke_alloc(hook_alloc_memalign, ret, (uintptr_t)ret,\n\t\t    args);\n\t}\n\n\tLOG(\"core.memalign.exit\", \"result: %p\", ret);\n\treturn ret;\n}\n#endif\n\n#ifdef JEMALLOC_OVERRIDE_VALLOC\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\nvoid JEMALLOC_NOTHROW *\nJEMALLOC_ATTR(malloc)\nje_valloc(size_t size) {\n\tvoid *ret;\n\n\tstatic_opts_t sopts;\n\tdynamic_opts_t dopts;\n\n\tLOG(\"core.valloc.entry\", \"size: %zu\\n\", size);\n\n\tstatic_opts_init(&sopts);\n\tdynamic_opts_init(&dopts);\n\n\tsopts.null_out_result_on_error = true;\n\tsopts.min_alignment = PAGE;\n\tsopts.oom_string =\n\t    \"<jemalloc>: Error allocating aligned memory: out of memory\\n\";\n\tsopts.invalid_alignment_string =\n\t    \"<jemalloc>: Error allocating aligned memory: invalid alignment\\n\";\n\n\tdopts.result = &ret;\n\tdopts.num_items = 1;\n\tdopts.item_size = size;\n\tdopts.alignment = PAGE;\n\n\timalloc(&sopts, &dopts);\n\tif (sopts.slow) {\n\t\tuintptr_t args[3] = {size};\n\t\thook_invoke_alloc(hook_alloc_valloc, ret, (uintptr_t)ret, args);\n\t}\n\n\tLOG(\"core.valloc.exit\", \"result: %p\\n\", ret);\n\treturn ret;\n}\n#endif\n\n#if defined(JEMALLOC_IS_MALLOC) && defined(JEMALLOC_GLIBC_MALLOC_HOOK)\n/*\n * glibc provides the RTLD_DEEPBIND flag for dlopen which can make it possible\n * to inconsistently reference libc's malloc(3)-compatible functions\n * (https://bugzilla.mozilla.org/show_bug.cgi?id=493541).\n *\n * These definitions interpose hooks in glibc.  The functions are actually\n * passed an extra argument for the caller return address, which will be\n * ignored.\n */\nJEMALLOC_EXPORT void (*__free_hook)(void *ptr) = je_free;\nJEMALLOC_EXPORT void *(*__malloc_hook)(size_t size) = je_malloc;\nJEMALLOC_EXPORT void *(*__realloc_hook)(void *ptr, size_t size) = je_realloc;\n#  ifdef JEMALLOC_GLIBC_MEMALIGN_HOOK\nJEMALLOC_EXPORT void *(*__memalign_hook)(size_t alignment, size_t size) =\n    je_memalign;\n#  endif\n\n#  ifdef CPU_COUNT\n/*\n * To enable static linking with glibc, the libc specific malloc interface must\n * be implemented also, so none of glibc's malloc.o functions are added to the\n * link.\n */\n#    define ALIAS(je_fn)\t__attribute__((alias (#je_fn), used))\n/* To force macro expansion of je_ prefix before stringification. */\n#    define PREALIAS(je_fn)\tALIAS(je_fn)\n#    ifdef JEMALLOC_OVERRIDE___LIBC_CALLOC\nvoid *__libc_calloc(size_t n, size_t size) PREALIAS(je_calloc);\n#    endif\n#    ifdef JEMALLOC_OVERRIDE___LIBC_FREE\nvoid __libc_free(void* ptr) PREALIAS(je_free);\n#    endif\n#    ifdef JEMALLOC_OVERRIDE___LIBC_MALLOC\nvoid *__libc_malloc(size_t size) PREALIAS(je_malloc);\n#    endif\n#    ifdef JEMALLOC_OVERRIDE___LIBC_MEMALIGN\nvoid *__libc_memalign(size_t align, size_t s) PREALIAS(je_memalign);\n#    endif\n#    ifdef JEMALLOC_OVERRIDE___LIBC_REALLOC\nvoid *__libc_realloc(void* ptr, size_t size) PREALIAS(je_realloc);\n#    endif\n#    ifdef JEMALLOC_OVERRIDE___LIBC_VALLOC\nvoid *__libc_valloc(size_t size) PREALIAS(je_valloc);\n#    endif\n#    ifdef JEMALLOC_OVERRIDE___POSIX_MEMALIGN\nint __posix_memalign(void** r, size_t a, size_t s) PREALIAS(je_posix_memalign);\n#    endif\n#    undef PREALIAS\n#    undef ALIAS\n#  endif\n#endif\n\n/*\n * End non-standard override functions.\n */\n/******************************************************************************/\n/*\n * Begin non-standard functions.\n */\n\n#ifdef JEMALLOC_EXPERIMENTAL_SMALLOCX_API\n\n#define JEMALLOC_SMALLOCX_CONCAT_HELPER(x, y) x ## y\n#define JEMALLOC_SMALLOCX_CONCAT_HELPER2(x, y)  \\\n  JEMALLOC_SMALLOCX_CONCAT_HELPER(x, y)\n\ntypedef struct {\n\tvoid *ptr;\n\tsize_t size;\n} smallocx_return_t;\n\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\nsmallocx_return_t JEMALLOC_NOTHROW\n/*\n * The attribute JEMALLOC_ATTR(malloc) cannot be used due to:\n *  - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86488\n */\nJEMALLOC_SMALLOCX_CONCAT_HELPER2(je_smallocx_, JEMALLOC_VERSION_GID_IDENT)\n  (size_t size, int flags) {\n\t/*\n\t * Note: the attribute JEMALLOC_ALLOC_SIZE(1) cannot be\n\t * used here because it makes writing beyond the `size`\n\t * of the `ptr` undefined behavior, but the objective\n\t * of this function is to allow writing beyond `size`\n\t * up to `smallocx_return_t::size`.\n\t */\n\tsmallocx_return_t ret;\n\tstatic_opts_t sopts;\n\tdynamic_opts_t dopts;\n\n\tLOG(\"core.smallocx.entry\", \"size: %zu, flags: %d\", size, flags);\n\n\tstatic_opts_init(&sopts);\n\tdynamic_opts_init(&dopts);\n\n\tsopts.assert_nonempty_alloc = true;\n\tsopts.null_out_result_on_error = true;\n\tsopts.oom_string = \"<jemalloc>: Error in mallocx(): out of memory\\n\";\n\tsopts.usize = true;\n\n\tdopts.result = &ret.ptr;\n\tdopts.num_items = 1;\n\tdopts.item_size = size;\n\tif (unlikely(flags != 0)) {\n\t\tif ((flags & MALLOCX_LG_ALIGN_MASK) != 0) {\n\t\t\tdopts.alignment = MALLOCX_ALIGN_GET_SPECIFIED(flags);\n\t\t}\n\n\t\tdopts.zero = MALLOCX_ZERO_GET(flags);\n\n\t\tif ((flags & MALLOCX_TCACHE_MASK) != 0) {\n\t\t\tif ((flags & MALLOCX_TCACHE_MASK)\n\t\t\t    == MALLOCX_TCACHE_NONE) {\n\t\t\t\tdopts.tcache_ind = TCACHE_IND_NONE;\n\t\t\t} else {\n\t\t\t\tdopts.tcache_ind = MALLOCX_TCACHE_GET(flags);\n\t\t\t}\n\t\t} else {\n\t\t\tdopts.tcache_ind = TCACHE_IND_AUTOMATIC;\n\t\t}\n\n\t\tif ((flags & MALLOCX_ARENA_MASK) != 0)\n\t\t\tdopts.arena_ind = MALLOCX_ARENA_GET(flags);\n\t}\n\n\timalloc(&sopts, &dopts);\n\tassert(dopts.usize == je_nallocx(size, flags));\n\tret.size = dopts.usize;\n\n\tLOG(\"core.smallocx.exit\", \"result: %p, size: %zu\", ret.ptr, ret.size);\n\treturn ret;\n}\n#undef JEMALLOC_SMALLOCX_CONCAT_HELPER\n#undef JEMALLOC_SMALLOCX_CONCAT_HELPER2\n#endif\n\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\nvoid JEMALLOC_NOTHROW *\nJEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE(1)\nje_mallocx(size_t size, int flags) {\n\tvoid *ret;\n\tstatic_opts_t sopts;\n\tdynamic_opts_t dopts;\n\n\tLOG(\"core.mallocx.entry\", \"size: %zu, flags: %d\", size, flags);\n\n\tstatic_opts_init(&sopts);\n\tdynamic_opts_init(&dopts);\n\n\tsopts.assert_nonempty_alloc = true;\n\tsopts.null_out_result_on_error = true;\n\tsopts.oom_string = \"<jemalloc>: Error in mallocx(): out of memory\\n\";\n\n\tdopts.result = &ret;\n\tdopts.num_items = 1;\n\tdopts.item_size = size;\n\tif (unlikely(flags != 0)) {\n\t\tif ((flags & MALLOCX_LG_ALIGN_MASK) != 0) {\n\t\t\tdopts.alignment = MALLOCX_ALIGN_GET_SPECIFIED(flags);\n\t\t}\n\n\t\tdopts.zero = MALLOCX_ZERO_GET(flags);\n\n\t\tif ((flags & MALLOCX_TCACHE_MASK) != 0) {\n\t\t\tif ((flags & MALLOCX_TCACHE_MASK)\n\t\t\t    == MALLOCX_TCACHE_NONE) {\n\t\t\t\tdopts.tcache_ind = TCACHE_IND_NONE;\n\t\t\t} else {\n\t\t\t\tdopts.tcache_ind = MALLOCX_TCACHE_GET(flags);\n\t\t\t}\n\t\t} else {\n\t\t\tdopts.tcache_ind = TCACHE_IND_AUTOMATIC;\n\t\t}\n\n\t\tif ((flags & MALLOCX_ARENA_MASK) != 0)\n\t\t\tdopts.arena_ind = MALLOCX_ARENA_GET(flags);\n\t}\n\n\timalloc(&sopts, &dopts);\n\tif (sopts.slow) {\n\t\tuintptr_t args[3] = {size, flags};\n\t\thook_invoke_alloc(hook_alloc_mallocx, ret, (uintptr_t)ret,\n\t\t    args);\n\t}\n\n\tLOG(\"core.mallocx.exit\", \"result: %p\", ret);\n\treturn ret;\n}\n\nstatic void *\nirallocx_prof_sample(tsdn_t *tsdn, void *old_ptr, size_t old_usize,\n    size_t usize, size_t alignment, bool zero, tcache_t *tcache, arena_t *arena,\n    prof_tctx_t *tctx, hook_ralloc_args_t *hook_args) {\n\tvoid *p;\n\n\tif (tctx == NULL) {\n\t\treturn NULL;\n\t}\n\tif (usize <= SC_SMALL_MAXCLASS) {\n\t\tp = iralloct(tsdn, old_ptr, old_usize,\n\t\t    SC_LARGE_MINCLASS, alignment, zero, tcache,\n\t\t    arena, hook_args);\n\t\tif (p == NULL) {\n\t\t\treturn NULL;\n\t\t}\n\t\tarena_prof_promote(tsdn, p, usize);\n\t} else {\n\t\tp = iralloct(tsdn, old_ptr, old_usize, usize, alignment, zero,\n\t\t    tcache, arena, hook_args);\n\t}\n\n\treturn p;\n}\n\nJEMALLOC_ALWAYS_INLINE void *\nirallocx_prof(tsd_t *tsd, void *old_ptr, size_t old_usize, size_t size,\n    size_t alignment, size_t *usize, bool zero, tcache_t *tcache,\n    arena_t *arena, alloc_ctx_t *alloc_ctx, hook_ralloc_args_t *hook_args) {\n\tvoid *p;\n\tbool prof_active;\n\tprof_tctx_t *old_tctx, *tctx;\n\n\tprof_active = prof_active_get_unlocked();\n\told_tctx = prof_tctx_get(tsd_tsdn(tsd), old_ptr, alloc_ctx);\n\ttctx = prof_alloc_prep(tsd, *usize, prof_active, false);\n\tif (unlikely((uintptr_t)tctx != (uintptr_t)1U)) {\n\t\tp = irallocx_prof_sample(tsd_tsdn(tsd), old_ptr, old_usize,\n\t\t    *usize, alignment, zero, tcache, arena, tctx, hook_args);\n\t} else {\n\t\tp = iralloct(tsd_tsdn(tsd), old_ptr, old_usize, size, alignment,\n\t\t    zero, tcache, arena, hook_args);\n\t}\n\tif (unlikely(p == NULL)) {\n\t\tprof_alloc_rollback(tsd, tctx, false);\n\t\treturn NULL;\n\t}\n\n\tif (p == old_ptr && alignment != 0) {\n\t\t/*\n\t\t * The allocation did not move, so it is possible that the size\n\t\t * class is smaller than would guarantee the requested\n\t\t * alignment, and that the alignment constraint was\n\t\t * serendipitously satisfied.  Additionally, old_usize may not\n\t\t * be the same as the current usize because of in-place large\n\t\t * reallocation.  Therefore, query the actual value of usize.\n\t\t */\n\t\t*usize = isalloc(tsd_tsdn(tsd), p);\n\t}\n\tprof_realloc(tsd, p, *usize, tctx, prof_active, false, old_ptr,\n\t    old_usize, old_tctx);\n\n\treturn p;\n}\n\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\nvoid JEMALLOC_NOTHROW *\nJEMALLOC_ALLOC_SIZE(2)\nje_rallocx(void *ptr, size_t size, int flags) {\n\tvoid *p;\n\ttsd_t *tsd;\n\tsize_t usize;\n\tsize_t old_usize;\n\tsize_t alignment = MALLOCX_ALIGN_GET(flags);\n\tbool zero = flags & MALLOCX_ZERO;\n\tarena_t *arena;\n\ttcache_t *tcache;\n\n\tLOG(\"core.rallocx.entry\", \"ptr: %p, size: %zu, flags: %d\", ptr,\n\t    size, flags);\n\n\n\tassert(ptr != NULL);\n\tassert(size != 0);\n\tassert(malloc_initialized() || IS_INITIALIZER);\n\ttsd = tsd_fetch();\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\n\tif (unlikely((flags & MALLOCX_ARENA_MASK) != 0)) {\n\t\tunsigned arena_ind = MALLOCX_ARENA_GET(flags);\n\t\tarena = arena_get(tsd_tsdn(tsd), arena_ind, true);\n\t\tif (unlikely(arena == NULL)) {\n\t\t\tgoto label_oom;\n\t\t}\n\t} else {\n\t\tarena = NULL;\n\t}\n\n\tif (unlikely((flags & MALLOCX_TCACHE_MASK) != 0)) {\n\t\tif ((flags & MALLOCX_TCACHE_MASK) == MALLOCX_TCACHE_NONE) {\n\t\t\ttcache = NULL;\n\t\t} else {\n\t\t\ttcache = tcaches_get(tsd, MALLOCX_TCACHE_GET(flags));\n\t\t}\n\t} else {\n\t\ttcache = tcache_get(tsd);\n\t}\n\n\talloc_ctx_t alloc_ctx;\n\trtree_ctx_t *rtree_ctx = tsd_rtree_ctx(tsd);\n\trtree_szind_slab_read(tsd_tsdn(tsd), &extents_rtree, rtree_ctx,\n\t    (uintptr_t)ptr, true, &alloc_ctx.szind, &alloc_ctx.slab);\n\tassert(alloc_ctx.szind != SC_NSIZES);\n\told_usize = sz_index2size(alloc_ctx.szind);\n\tassert(old_usize == isalloc(tsd_tsdn(tsd), ptr));\n\n\thook_ralloc_args_t hook_args = {false, {(uintptr_t)ptr, size, flags,\n\t\t0}};\n\tif (config_prof && opt_prof) {\n\t\tusize = (alignment == 0) ?\n\t\t    sz_s2u(size) : sz_sa2u(size, alignment);\n\t\tif (unlikely(usize == 0\n\t\t    || usize > SC_LARGE_MAXCLASS)) {\n\t\t\tgoto label_oom;\n\t\t}\n\t\tp = irallocx_prof(tsd, ptr, old_usize, size, alignment, &usize,\n\t\t    zero, tcache, arena, &alloc_ctx, &hook_args);\n\t\tif (unlikely(p == NULL)) {\n\t\t\tgoto label_oom;\n\t\t}\n\t} else {\n\t\tp = iralloct(tsd_tsdn(tsd), ptr, old_usize, size, alignment,\n\t\t    zero, tcache, arena, &hook_args);\n\t\tif (unlikely(p == NULL)) {\n\t\t\tgoto label_oom;\n\t\t}\n\t\tif (config_stats) {\n\t\t\tusize = isalloc(tsd_tsdn(tsd), p);\n\t\t}\n\t}\n\tassert(alignment == 0 || ((uintptr_t)p & (alignment - 1)) == ZU(0));\n\n\tif (config_stats) {\n\t\t*tsd_thread_allocatedp_get(tsd) += usize;\n\t\t*tsd_thread_deallocatedp_get(tsd) += old_usize;\n\t}\n\tUTRACE(ptr, size, p);\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\n\tLOG(\"core.rallocx.exit\", \"result: %p\", p);\n\treturn p;\nlabel_oom:\n\tif (config_xmalloc && unlikely(opt_xmalloc)) {\n\t\tmalloc_write(\"<jemalloc>: Error in rallocx(): out of memory\\n\");\n\t\tabort();\n\t}\n\tUTRACE(ptr, size, 0);\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\n\tLOG(\"core.rallocx.exit\", \"result: %p\", NULL);\n\treturn NULL;\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\nixallocx_helper(tsdn_t *tsdn, void *ptr, size_t old_usize, size_t size,\n    size_t extra, size_t alignment, bool zero) {\n\tsize_t newsize;\n\n\tif (ixalloc(tsdn, ptr, old_usize, size, extra, alignment, zero,\n\t    &newsize)) {\n\t\treturn old_usize;\n\t}\n\n\treturn newsize;\n}\n\nstatic size_t\nixallocx_prof_sample(tsdn_t *tsdn, void *ptr, size_t old_usize, size_t size,\n    size_t extra, size_t alignment, bool zero, prof_tctx_t *tctx) {\n\tsize_t usize;\n\n\tif (tctx == NULL) {\n\t\treturn old_usize;\n\t}\n\tusize = ixallocx_helper(tsdn, ptr, old_usize, size, extra, alignment,\n\t    zero);\n\n\treturn usize;\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\nixallocx_prof(tsd_t *tsd, void *ptr, size_t old_usize, size_t size,\n    size_t extra, size_t alignment, bool zero, alloc_ctx_t *alloc_ctx) {\n\tsize_t usize_max, usize;\n\tbool prof_active;\n\tprof_tctx_t *old_tctx, *tctx;\n\n\tprof_active = prof_active_get_unlocked();\n\told_tctx = prof_tctx_get(tsd_tsdn(tsd), ptr, alloc_ctx);\n\t/*\n\t * usize isn't knowable before ixalloc() returns when extra is non-zero.\n\t * Therefore, compute its maximum possible value and use that in\n\t * prof_alloc_prep() to decide whether to capture a backtrace.\n\t * prof_realloc() will use the actual usize to decide whether to sample.\n\t */\n\tif (alignment == 0) {\n\t\tusize_max = sz_s2u(size+extra);\n\t\tassert(usize_max > 0\n\t\t    && usize_max <= SC_LARGE_MAXCLASS);\n\t} else {\n\t\tusize_max = sz_sa2u(size+extra, alignment);\n\t\tif (unlikely(usize_max == 0\n\t\t    || usize_max > SC_LARGE_MAXCLASS)) {\n\t\t\t/*\n\t\t\t * usize_max is out of range, and chances are that\n\t\t\t * allocation will fail, but use the maximum possible\n\t\t\t * value and carry on with prof_alloc_prep(), just in\n\t\t\t * case allocation succeeds.\n\t\t\t */\n\t\t\tusize_max = SC_LARGE_MAXCLASS;\n\t\t}\n\t}\n\ttctx = prof_alloc_prep(tsd, usize_max, prof_active, false);\n\n\tif (unlikely((uintptr_t)tctx != (uintptr_t)1U)) {\n\t\tusize = ixallocx_prof_sample(tsd_tsdn(tsd), ptr, old_usize,\n\t\t    size, extra, alignment, zero, tctx);\n\t} else {\n\t\tusize = ixallocx_helper(tsd_tsdn(tsd), ptr, old_usize, size,\n\t\t    extra, alignment, zero);\n\t}\n\tif (usize == old_usize) {\n\t\tprof_alloc_rollback(tsd, tctx, false);\n\t\treturn usize;\n\t}\n\tprof_realloc(tsd, ptr, usize, tctx, prof_active, false, ptr, old_usize,\n\t    old_tctx);\n\n\treturn usize;\n}\n\nJEMALLOC_EXPORT size_t JEMALLOC_NOTHROW\nje_xallocx(void *ptr, size_t size, size_t extra, int flags) {\n\ttsd_t *tsd;\n\tsize_t usize, old_usize;\n\tsize_t alignment = MALLOCX_ALIGN_GET(flags);\n\tbool zero = flags & MALLOCX_ZERO;\n\n\tLOG(\"core.xallocx.entry\", \"ptr: %p, size: %zu, extra: %zu, \"\n\t    \"flags: %d\", ptr, size, extra, flags);\n\n\tassert(ptr != NULL);\n\tassert(size != 0);\n\tassert(SIZE_T_MAX - size >= extra);\n\tassert(malloc_initialized() || IS_INITIALIZER);\n\ttsd = tsd_fetch();\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\n\talloc_ctx_t alloc_ctx;\n\trtree_ctx_t *rtree_ctx = tsd_rtree_ctx(tsd);\n\trtree_szind_slab_read(tsd_tsdn(tsd), &extents_rtree, rtree_ctx,\n\t    (uintptr_t)ptr, true, &alloc_ctx.szind, &alloc_ctx.slab);\n\tassert(alloc_ctx.szind != SC_NSIZES);\n\told_usize = sz_index2size(alloc_ctx.szind);\n\tassert(old_usize == isalloc(tsd_tsdn(tsd), ptr));\n\t/*\n\t * The API explicitly absolves itself of protecting against (size +\n\t * extra) numerical overflow, but we may need to clamp extra to avoid\n\t * exceeding SC_LARGE_MAXCLASS.\n\t *\n\t * Ordinarily, size limit checking is handled deeper down, but here we\n\t * have to check as part of (size + extra) clamping, since we need the\n\t * clamped value in the above helper functions.\n\t */\n\tif (unlikely(size > SC_LARGE_MAXCLASS)) {\n\t\tusize = old_usize;\n\t\tgoto label_not_resized;\n\t}\n\tif (unlikely(SC_LARGE_MAXCLASS - size < extra)) {\n\t\textra = SC_LARGE_MAXCLASS - size;\n\t}\n\n\tif (config_prof && opt_prof) {\n\t\tusize = ixallocx_prof(tsd, ptr, old_usize, size, extra,\n\t\t    alignment, zero, &alloc_ctx);\n\t} else {\n\t\tusize = ixallocx_helper(tsd_tsdn(tsd), ptr, old_usize, size,\n\t\t    extra, alignment, zero);\n\t}\n\tif (unlikely(usize == old_usize)) {\n\t\tgoto label_not_resized;\n\t}\n\n\tif (config_stats) {\n\t\t*tsd_thread_allocatedp_get(tsd) += usize;\n\t\t*tsd_thread_deallocatedp_get(tsd) += old_usize;\n\t}\nlabel_not_resized:\n\tif (unlikely(!tsd_fast(tsd))) {\n\t\tuintptr_t args[4] = {(uintptr_t)ptr, size, extra, flags};\n\t\thook_invoke_expand(hook_expand_xallocx, ptr, old_usize,\n\t\t    usize, (uintptr_t)usize, args);\n\t}\n\n\tUTRACE(ptr, size, ptr);\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\n\tLOG(\"core.xallocx.exit\", \"result: %zu\", usize);\n\treturn usize;\n}\n\nJEMALLOC_EXPORT size_t JEMALLOC_NOTHROW\nJEMALLOC_ATTR(pure)\nje_sallocx(const void *ptr, int flags) {\n\tsize_t usize;\n\ttsdn_t *tsdn;\n\n\tLOG(\"core.sallocx.entry\", \"ptr: %p, flags: %d\", ptr, flags);\n\n\tassert(malloc_initialized() || IS_INITIALIZER);\n\tassert(ptr != NULL);\n\n\ttsdn = tsdn_fetch();\n\tcheck_entry_exit_locking(tsdn);\n\n\tif (config_debug || force_ivsalloc) {\n\t\tusize = ivsalloc(tsdn, ptr);\n\t\tassert(force_ivsalloc || usize != 0);\n\t} else {\n\t\tusize = isalloc(tsdn, ptr);\n\t}\n\n\tcheck_entry_exit_locking(tsdn);\n\n\tLOG(\"core.sallocx.exit\", \"result: %zu\", usize);\n\treturn usize;\n}\n\nJEMALLOC_EXPORT void JEMALLOC_NOTHROW\nje_dallocx(void *ptr, int flags) {\n\tLOG(\"core.dallocx.entry\", \"ptr: %p, flags: %d\", ptr, flags);\n\n\tassert(ptr != NULL);\n\tassert(malloc_initialized() || IS_INITIALIZER);\n\n\ttsd_t *tsd = tsd_fetch();\n\tbool fast = tsd_fast(tsd);\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\n\ttcache_t *tcache;\n\tif (unlikely((flags & MALLOCX_TCACHE_MASK) != 0)) {\n\t\t/* Not allowed to be reentrant and specify a custom tcache. */\n\t\tassert(tsd_reentrancy_level_get(tsd) == 0);\n\t\tif ((flags & MALLOCX_TCACHE_MASK) == MALLOCX_TCACHE_NONE) {\n\t\t\ttcache = NULL;\n\t\t} else {\n\t\t\ttcache = tcaches_get(tsd, MALLOCX_TCACHE_GET(flags));\n\t\t}\n\t} else {\n\t\tif (likely(fast)) {\n\t\t\ttcache = tsd_tcachep_get(tsd);\n\t\t\tassert(tcache == tcache_get(tsd));\n\t\t} else {\n\t\t\tif (likely(tsd_reentrancy_level_get(tsd) == 0)) {\n\t\t\t\ttcache = tcache_get(tsd);\n\t\t\t}  else {\n\t\t\t\ttcache = NULL;\n\t\t\t}\n\t\t}\n\t}\n\n\tUTRACE(ptr, 0, 0);\n\tif (likely(fast)) {\n\t\ttsd_assert_fast(tsd);\n\t\tifree(tsd, ptr, tcache, false);\n\t} else {\n\t\tuintptr_t args_raw[3] = {(uintptr_t)ptr, flags};\n\t\thook_invoke_dalloc(hook_dalloc_dallocx, ptr, args_raw);\n\t\tifree(tsd, ptr, tcache, true);\n\t}\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\n\tLOG(\"core.dallocx.exit\", \"\");\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\ninallocx(tsdn_t *tsdn, size_t size, int flags) {\n\tcheck_entry_exit_locking(tsdn);\n\n\tsize_t usize;\n\tif (likely((flags & MALLOCX_LG_ALIGN_MASK) == 0)) {\n\t\tusize = sz_s2u(size);\n\t} else {\n\t\tusize = sz_sa2u(size, MALLOCX_ALIGN_GET_SPECIFIED(flags));\n\t}\n\tcheck_entry_exit_locking(tsdn);\n\treturn usize;\n}\n\nJEMALLOC_NOINLINE void\nsdallocx_default(void *ptr, size_t size, int flags) {\n\tassert(ptr != NULL);\n\tassert(malloc_initialized() || IS_INITIALIZER);\n\n\ttsd_t *tsd = tsd_fetch();\n\tbool fast = tsd_fast(tsd);\n\tsize_t usize = inallocx(tsd_tsdn(tsd), size, flags);\n\tassert(usize == isalloc(tsd_tsdn(tsd), ptr));\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\n\ttcache_t *tcache;\n\tif (unlikely((flags & MALLOCX_TCACHE_MASK) != 0)) {\n\t\t/* Not allowed to be reentrant and specify a custom tcache. */\n\t\tassert(tsd_reentrancy_level_get(tsd) == 0);\n\t\tif ((flags & MALLOCX_TCACHE_MASK) == MALLOCX_TCACHE_NONE) {\n\t\t\ttcache = NULL;\n\t\t} else {\n\t\t\ttcache = tcaches_get(tsd, MALLOCX_TCACHE_GET(flags));\n\t\t}\n\t} else {\n\t\tif (likely(fast)) {\n\t\t\ttcache = tsd_tcachep_get(tsd);\n\t\t\tassert(tcache == tcache_get(tsd));\n\t\t} else {\n\t\t\tif (likely(tsd_reentrancy_level_get(tsd) == 0)) {\n\t\t\t\ttcache = tcache_get(tsd);\n\t\t\t} else {\n\t\t\t\ttcache = NULL;\n\t\t\t}\n\t\t}\n\t}\n\n\tUTRACE(ptr, 0, 0);\n\tif (likely(fast)) {\n\t\ttsd_assert_fast(tsd);\n\t\tisfree(tsd, ptr, usize, tcache, false);\n\t} else {\n\t\tuintptr_t args_raw[3] = {(uintptr_t)ptr, size, flags};\n\t\thook_invoke_dalloc(hook_dalloc_sdallocx, ptr, args_raw);\n\t\tisfree(tsd, ptr, usize, tcache, true);\n\t}\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\n}\n\nJEMALLOC_EXPORT void JEMALLOC_NOTHROW\nje_sdallocx(void *ptr, size_t size, int flags) {\n\tLOG(\"core.sdallocx.entry\", \"ptr: %p, size: %zu, flags: %d\", ptr,\n\t\tsize, flags);\n\n\tif (flags !=0 || !free_fastpath(ptr, size, true)) {\n\t\tsdallocx_default(ptr, size, flags);\n\t}\n\n\tLOG(\"core.sdallocx.exit\", \"\");\n}\n\nvoid JEMALLOC_NOTHROW\nje_sdallocx_noflags(void *ptr, size_t size) {\n\tLOG(\"core.sdallocx.entry\", \"ptr: %p, size: %zu, flags: 0\", ptr,\n\t\tsize);\n\n\tif (!free_fastpath(ptr, size, true)) {\n\t\tsdallocx_default(ptr, size, 0);\n\t}\n\n\tLOG(\"core.sdallocx.exit\", \"\");\n}\n\nJEMALLOC_EXPORT size_t JEMALLOC_NOTHROW\nJEMALLOC_ATTR(pure)\nje_nallocx(size_t size, int flags) {\n\tsize_t usize;\n\ttsdn_t *tsdn;\n\n\tassert(size != 0);\n\n\tif (unlikely(malloc_init())) {\n\t\tLOG(\"core.nallocx.exit\", \"result: %zu\", ZU(0));\n\t\treturn 0;\n\t}\n\n\ttsdn = tsdn_fetch();\n\tcheck_entry_exit_locking(tsdn);\n\n\tusize = inallocx(tsdn, size, flags);\n\tif (unlikely(usize > SC_LARGE_MAXCLASS)) {\n\t\tLOG(\"core.nallocx.exit\", \"result: %zu\", ZU(0));\n\t\treturn 0;\n\t}\n\n\tcheck_entry_exit_locking(tsdn);\n\tLOG(\"core.nallocx.exit\", \"result: %zu\", usize);\n\treturn usize;\n}\n\nJEMALLOC_EXPORT int JEMALLOC_NOTHROW\nje_mallctl(const char *name, void *oldp, size_t *oldlenp, void *newp,\n    size_t newlen) {\n\tint ret;\n\ttsd_t *tsd;\n\n\tLOG(\"core.mallctl.entry\", \"name: %s\", name);\n\n\tif (unlikely(malloc_init())) {\n\t\tLOG(\"core.mallctl.exit\", \"result: %d\", EAGAIN);\n\t\treturn EAGAIN;\n\t}\n\n\ttsd = tsd_fetch();\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\tret = ctl_byname(tsd, name, oldp, oldlenp, newp, newlen);\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\n\tLOG(\"core.mallctl.exit\", \"result: %d\", ret);\n\treturn ret;\n}\n\nJEMALLOC_EXPORT int JEMALLOC_NOTHROW\nje_mallctlnametomib(const char *name, size_t *mibp, size_t *miblenp) {\n\tint ret;\n\n\tLOG(\"core.mallctlnametomib.entry\", \"name: %s\", name);\n\n\tif (unlikely(malloc_init())) {\n\t\tLOG(\"core.mallctlnametomib.exit\", \"result: %d\", EAGAIN);\n\t\treturn EAGAIN;\n\t}\n\n\ttsd_t *tsd = tsd_fetch();\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\tret = ctl_nametomib(tsd, name, mibp, miblenp);\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\n\tLOG(\"core.mallctlnametomib.exit\", \"result: %d\", ret);\n\treturn ret;\n}\n\nJEMALLOC_EXPORT int JEMALLOC_NOTHROW\nje_mallctlbymib(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp,\n  void *newp, size_t newlen) {\n\tint ret;\n\ttsd_t *tsd;\n\n\tLOG(\"core.mallctlbymib.entry\", \"\");\n\n\tif (unlikely(malloc_init())) {\n\t\tLOG(\"core.mallctlbymib.exit\", \"result: %d\", EAGAIN);\n\t\treturn EAGAIN;\n\t}\n\n\ttsd = tsd_fetch();\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\tret = ctl_bymib(tsd, mib, miblen, oldp, oldlenp, newp, newlen);\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\tLOG(\"core.mallctlbymib.exit\", \"result: %d\", ret);\n\treturn ret;\n}\n\nJEMALLOC_EXPORT void JEMALLOC_NOTHROW\nje_malloc_stats_print(void (*write_cb)(void *, const char *), void *cbopaque,\n    const char *opts) {\n\ttsdn_t *tsdn;\n\n\tLOG(\"core.malloc_stats_print.entry\", \"\");\n\n\ttsdn = tsdn_fetch();\n\tcheck_entry_exit_locking(tsdn);\n\tstats_print(write_cb, cbopaque, opts);\n\tcheck_entry_exit_locking(tsdn);\n\tLOG(\"core.malloc_stats_print.exit\", \"\");\n}\n\nJEMALLOC_EXPORT size_t JEMALLOC_NOTHROW\nje_malloc_usable_size(JEMALLOC_USABLE_SIZE_CONST void *ptr) {\n\tsize_t ret;\n\ttsdn_t *tsdn;\n\n\tLOG(\"core.malloc_usable_size.entry\", \"ptr: %p\", ptr);\n\n\tassert(malloc_initialized() || IS_INITIALIZER);\n\n\ttsdn = tsdn_fetch();\n\tcheck_entry_exit_locking(tsdn);\n\n\tif (unlikely(ptr == NULL)) {\n\t\tret = 0;\n\t} else {\n\t\tif (config_debug || force_ivsalloc) {\n\t\t\tret = ivsalloc(tsdn, ptr);\n\t\t\tassert(force_ivsalloc || ret != 0);\n\t\t} else {\n\t\t\tret = isalloc(tsdn, ptr);\n\t\t}\n\t}\n\n\tcheck_entry_exit_locking(tsdn);\n\tLOG(\"core.malloc_usable_size.exit\", \"result: %zu\", ret);\n\treturn ret;\n}\n\n/*\n * End non-standard functions.\n */\n/******************************************************************************/\n/*\n * The following functions are used by threading libraries for protection of\n * malloc during fork().\n */\n\n/*\n * If an application creates a thread before doing any allocation in the main\n * thread, then calls fork(2) in the main thread followed by memory allocation\n * in the child process, a race can occur that results in deadlock within the\n * child: the main thread may have forked while the created thread had\n * partially initialized the allocator.  Ordinarily jemalloc prevents\n * fork/malloc races via the following functions it registers during\n * initialization using pthread_atfork(), but of course that does no good if\n * the allocator isn't fully initialized at fork time.  The following library\n * constructor is a partial solution to this problem.  It may still be possible\n * to trigger the deadlock described above, but doing so would involve forking\n * via a library constructor that runs before jemalloc's runs.\n */\n#ifndef JEMALLOC_JET\nJEMALLOC_ATTR(constructor)\nstatic void\njemalloc_constructor(void) {\n\tmalloc_init();\n}\n#endif\n\n#ifndef JEMALLOC_MUTEX_INIT_CB\nvoid\njemalloc_prefork(void)\n#else\nJEMALLOC_EXPORT void\n_malloc_prefork(void)\n#endif\n{\n\ttsd_t *tsd;\n\tunsigned i, j, narenas;\n\tarena_t *arena;\n\n#ifdef JEMALLOC_MUTEX_INIT_CB\n\tif (!malloc_initialized()) {\n\t\treturn;\n\t}\n#endif\n\tassert(malloc_initialized());\n\n\ttsd = tsd_fetch();\n\n\tnarenas = narenas_total_get();\n\n\twitness_prefork(tsd_witness_tsdp_get(tsd));\n\t/* Acquire all mutexes in a safe order. */\n\tctl_prefork(tsd_tsdn(tsd));\n\ttcache_prefork(tsd_tsdn(tsd));\n\tmalloc_mutex_prefork(tsd_tsdn(tsd), &arenas_lock);\n\tif (have_background_thread) {\n\t\tbackground_thread_prefork0(tsd_tsdn(tsd));\n\t}\n\tprof_prefork0(tsd_tsdn(tsd));\n\tif (have_background_thread) {\n\t\tbackground_thread_prefork1(tsd_tsdn(tsd));\n\t}\n\t/* Break arena prefork into stages to preserve lock order. */\n\tfor (i = 0; i < 8; i++) {\n\t\tfor (j = 0; j < narenas; j++) {\n\t\t\tif ((arena = arena_get(tsd_tsdn(tsd), j, false)) !=\n\t\t\t    NULL) {\n\t\t\t\tswitch (i) {\n\t\t\t\tcase 0:\n\t\t\t\t\tarena_prefork0(tsd_tsdn(tsd), arena);\n\t\t\t\t\tbreak;\n\t\t\t\tcase 1:\n\t\t\t\t\tarena_prefork1(tsd_tsdn(tsd), arena);\n\t\t\t\t\tbreak;\n\t\t\t\tcase 2:\n\t\t\t\t\tarena_prefork2(tsd_tsdn(tsd), arena);\n\t\t\t\t\tbreak;\n\t\t\t\tcase 3:\n\t\t\t\t\tarena_prefork3(tsd_tsdn(tsd), arena);\n\t\t\t\t\tbreak;\n\t\t\t\tcase 4:\n\t\t\t\t\tarena_prefork4(tsd_tsdn(tsd), arena);\n\t\t\t\t\tbreak;\n\t\t\t\tcase 5:\n\t\t\t\t\tarena_prefork5(tsd_tsdn(tsd), arena);\n\t\t\t\t\tbreak;\n\t\t\t\tcase 6:\n\t\t\t\t\tarena_prefork6(tsd_tsdn(tsd), arena);\n\t\t\t\t\tbreak;\n\t\t\t\tcase 7:\n\t\t\t\t\tarena_prefork7(tsd_tsdn(tsd), arena);\n\t\t\t\t\tbreak;\n\t\t\t\tdefault: not_reached();\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tprof_prefork1(tsd_tsdn(tsd));\n\ttsd_prefork(tsd);\n}\n\n#ifndef JEMALLOC_MUTEX_INIT_CB\nvoid\njemalloc_postfork_parent(void)\n#else\nJEMALLOC_EXPORT void\n_malloc_postfork(void)\n#endif\n{\n\ttsd_t *tsd;\n\tunsigned i, narenas;\n\n#ifdef JEMALLOC_MUTEX_INIT_CB\n\tif (!malloc_initialized()) {\n\t\treturn;\n\t}\n#endif\n\tassert(malloc_initialized());\n\n\ttsd = tsd_fetch();\n\n\ttsd_postfork_parent(tsd);\n\n\twitness_postfork_parent(tsd_witness_tsdp_get(tsd));\n\t/* Release all mutexes, now that fork() has completed. */\n\tfor (i = 0, narenas = narenas_total_get(); i < narenas; i++) {\n\t\tarena_t *arena;\n\n\t\tif ((arena = arena_get(tsd_tsdn(tsd), i, false)) != NULL) {\n\t\t\tarena_postfork_parent(tsd_tsdn(tsd), arena);\n\t\t}\n\t}\n\tprof_postfork_parent(tsd_tsdn(tsd));\n\tif (have_background_thread) {\n\t\tbackground_thread_postfork_parent(tsd_tsdn(tsd));\n\t}\n\tmalloc_mutex_postfork_parent(tsd_tsdn(tsd), &arenas_lock);\n\ttcache_postfork_parent(tsd_tsdn(tsd));\n\tctl_postfork_parent(tsd_tsdn(tsd));\n}\n\nvoid\njemalloc_postfork_child(void) {\n\ttsd_t *tsd;\n\tunsigned i, narenas;\n\n\tassert(malloc_initialized());\n\n\ttsd = tsd_fetch();\n\n\ttsd_postfork_child(tsd);\n\n\twitness_postfork_child(tsd_witness_tsdp_get(tsd));\n\t/* Release all mutexes, now that fork() has completed. */\n\tfor (i = 0, narenas = narenas_total_get(); i < narenas; i++) {\n\t\tarena_t *arena;\n\n\t\tif ((arena = arena_get(tsd_tsdn(tsd), i, false)) != NULL) {\n\t\t\tarena_postfork_child(tsd_tsdn(tsd), arena);\n\t\t}\n\t}\n\tprof_postfork_child(tsd_tsdn(tsd));\n\tif (have_background_thread) {\n\t\tbackground_thread_postfork_child(tsd_tsdn(tsd));\n\t}\n\tmalloc_mutex_postfork_child(tsd_tsdn(tsd), &arenas_lock);\n\ttcache_postfork_child(tsd_tsdn(tsd));\n\tctl_postfork_child(tsd_tsdn(tsd));\n}\n\n/******************************************************************************/\n\n/* Helps the application decide if a pointer is worth re-allocating in order to reduce fragmentation.\n * returns 1 if the allocation should be moved, and 0 if the allocation be kept.\n * If the application decides to re-allocate it should use MALLOCX_TCACHE_NONE when doing so. */\nJEMALLOC_EXPORT int JEMALLOC_NOTHROW\nget_defrag_hint(void* ptr) {\n\tassert(ptr != NULL);\n\treturn iget_defrag_hint(TSDN_NULL, ptr);\n}\n"
  },
  {
    "path": "deps/jemalloc/src/jemalloc_cpp.cpp",
    "content": "#include <mutex>\n#include <new>\n\n#define JEMALLOC_CPP_CPP_\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#ifdef __cplusplus\n}\n#endif\n\n// All operators in this file are exported.\n\n// Possibly alias hidden versions of malloc and sdallocx to avoid an extra plt\n// thunk?\n//\n// extern __typeof (sdallocx) sdallocx_int\n//  __attribute ((alias (\"sdallocx\"),\n//\t\tvisibility (\"hidden\")));\n//\n// ... but it needs to work with jemalloc namespaces.\n\nvoid\t*operator new(std::size_t size);\nvoid\t*operator new[](std::size_t size);\nvoid\t*operator new(std::size_t size, const std::nothrow_t &) noexcept;\nvoid\t*operator new[](std::size_t size, const std::nothrow_t &) noexcept;\nvoid\toperator delete(void *ptr) noexcept;\nvoid\toperator delete[](void *ptr) noexcept;\nvoid\toperator delete(void *ptr, const std::nothrow_t &) noexcept;\nvoid\toperator delete[](void *ptr, const std::nothrow_t &) noexcept;\n\n#if __cpp_sized_deallocation >= 201309\n/* C++14's sized-delete operators. */\nvoid\toperator delete(void *ptr, std::size_t size) noexcept;\nvoid\toperator delete[](void *ptr, std::size_t size) noexcept;\n#endif\n\nJEMALLOC_NOINLINE\nstatic void *\nhandleOOM(std::size_t size, bool nothrow) {\n\tvoid *ptr = nullptr;\n\n\twhile (ptr == nullptr) {\n\t\tstd::new_handler handler;\n\t\t// GCC-4.8 and clang 4.0 do not have std::get_new_handler.\n\t\t{\n\t\t\tstatic std::mutex mtx;\n\t\t\tstd::lock_guard<std::mutex> lock(mtx);\n\n\t\t\thandler = std::set_new_handler(nullptr);\n\t\t\tstd::set_new_handler(handler);\n\t\t}\n\t\tif (handler == nullptr)\n\t\t\tbreak;\n\n\t\ttry {\n\t\t\thandler();\n\t\t} catch (const std::bad_alloc &) {\n\t\t\tbreak;\n\t\t}\n\n\t\tptr = je_malloc(size);\n\t}\n\n\tif (ptr == nullptr && !nothrow)\n\t\tstd::__throw_bad_alloc();\n\treturn ptr;\n}\n\ntemplate <bool IsNoExcept>\nJEMALLOC_ALWAYS_INLINE\nvoid *\nnewImpl(std::size_t size) noexcept(IsNoExcept) {\n\tvoid *ptr = je_malloc(size);\n\tif (likely(ptr != nullptr))\n\t\treturn ptr;\n\n\treturn handleOOM(size, IsNoExcept);\n}\n\nvoid *\noperator new(std::size_t size) {\n\treturn newImpl<false>(size);\n}\n\nvoid *\noperator new[](std::size_t size) {\n\treturn newImpl<false>(size);\n}\n\nvoid *\noperator new(std::size_t size, const std::nothrow_t &) noexcept {\n\treturn newImpl<true>(size);\n}\n\nvoid *\noperator new[](std::size_t size, const std::nothrow_t &) noexcept {\n\treturn newImpl<true>(size);\n}\n\nvoid\noperator delete(void *ptr) noexcept {\n\tje_free(ptr);\n}\n\nvoid\noperator delete[](void *ptr) noexcept {\n\tje_free(ptr);\n}\n\nvoid\noperator delete(void *ptr, const std::nothrow_t &) noexcept {\n\tje_free(ptr);\n}\n\nvoid operator delete[](void *ptr, const std::nothrow_t &) noexcept {\n\tje_free(ptr);\n}\n\n#if __cpp_sized_deallocation >= 201309\n\nvoid\noperator delete(void *ptr, std::size_t size) noexcept {\n\tif (unlikely(ptr == nullptr)) {\n\t\treturn;\n\t}\n\tje_sdallocx_noflags(ptr, size);\n}\n\nvoid operator delete[](void *ptr, std::size_t size) noexcept {\n\tif (unlikely(ptr == nullptr)) {\n\t\treturn;\n\t}\n\tje_sdallocx_noflags(ptr, size);\n}\n\n#endif  // __cpp_sized_deallocation\n"
  },
  {
    "path": "deps/jemalloc/src/large.c",
    "content": "#define JEMALLOC_LARGE_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/extent_mmap.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/rtree.h\"\n#include \"jemalloc/internal/util.h\"\n\n/******************************************************************************/\n\nvoid *\nlarge_malloc(tsdn_t *tsdn, arena_t *arena, size_t usize, bool zero) {\n\tassert(usize == sz_s2u(usize));\n\n\treturn large_palloc(tsdn, arena, usize, CACHELINE, zero);\n}\n\nvoid *\nlarge_palloc(tsdn_t *tsdn, arena_t *arena, size_t usize, size_t alignment,\n    bool zero) {\n\tsize_t ausize;\n\textent_t *extent;\n\tbool is_zeroed;\n\tUNUSED bool idump JEMALLOC_CC_SILENCE_INIT(false);\n\n\tassert(!tsdn_null(tsdn) || arena != NULL);\n\n\tausize = sz_sa2u(usize, alignment);\n\tif (unlikely(ausize == 0 || ausize > SC_LARGE_MAXCLASS)) {\n\t\treturn NULL;\n\t}\n\n\tif (config_fill && unlikely(opt_zero)) {\n\t\tzero = true;\n\t}\n\t/*\n\t * Copy zero into is_zeroed and pass the copy when allocating the\n\t * extent, so that it is possible to make correct junk/zero fill\n\t * decisions below, even if is_zeroed ends up true when zero is false.\n\t */\n\tis_zeroed = zero;\n\tif (likely(!tsdn_null(tsdn))) {\n\t\tarena = arena_choose_maybe_huge(tsdn_tsd(tsdn), arena, usize);\n\t}\n\tif (unlikely(arena == NULL) || (extent = arena_extent_alloc_large(tsdn,\n\t    arena, usize, alignment, &is_zeroed)) == NULL) {\n\t\treturn NULL;\n\t}\n\n\t/* See comments in arena_bin_slabs_full_insert(). */\n\tif (!arena_is_auto(arena)) {\n\t\t/* Insert extent into large. */\n\t\tmalloc_mutex_lock(tsdn, &arena->large_mtx);\n\t\textent_list_append(&arena->large, extent);\n\t\tmalloc_mutex_unlock(tsdn, &arena->large_mtx);\n\t}\n\tif (config_prof && arena_prof_accum(tsdn, arena, usize)) {\n\t\tprof_idump(tsdn);\n\t}\n\n\tif (zero) {\n\t\tassert(is_zeroed);\n\t} else if (config_fill && unlikely(opt_junk_alloc)) {\n\t\tmemset(extent_addr_get(extent), JEMALLOC_ALLOC_JUNK,\n\t\t    extent_usize_get(extent));\n\t}\n\n\tarena_decay_tick(tsdn, arena);\n\treturn extent_addr_get(extent);\n}\n\nstatic void\nlarge_dalloc_junk_impl(void *ptr, size_t size) {\n\tmemset(ptr, JEMALLOC_FREE_JUNK, size);\n}\nlarge_dalloc_junk_t *JET_MUTABLE large_dalloc_junk = large_dalloc_junk_impl;\n\nstatic void\nlarge_dalloc_maybe_junk_impl(void *ptr, size_t size) {\n\tif (config_fill && have_dss && unlikely(opt_junk_free)) {\n\t\t/*\n\t\t * Only bother junk filling if the extent isn't about to be\n\t\t * unmapped.\n\t\t */\n\t\tif (opt_retain || (have_dss && extent_in_dss(ptr))) {\n\t\t\tlarge_dalloc_junk(ptr, size);\n\t\t}\n\t}\n}\nlarge_dalloc_maybe_junk_t *JET_MUTABLE large_dalloc_maybe_junk =\n    large_dalloc_maybe_junk_impl;\n\nstatic bool\nlarge_ralloc_no_move_shrink(tsdn_t *tsdn, extent_t *extent, size_t usize) {\n\tarena_t *arena = extent_arena_get(extent);\n\tsize_t oldusize = extent_usize_get(extent);\n\textent_hooks_t *extent_hooks = extent_hooks_get(arena);\n\tsize_t diff = extent_size_get(extent) - (usize + sz_large_pad);\n\n\tassert(oldusize > usize);\n\n\tif (extent_hooks->split == NULL) {\n\t\treturn true;\n\t}\n\n\t/* Split excess pages. */\n\tif (diff != 0) {\n\t\textent_t *trail = extent_split_wrapper(tsdn, arena,\n\t\t    &extent_hooks, extent, usize + sz_large_pad,\n\t\t    sz_size2index(usize), false, diff, SC_NSIZES, false);\n\t\tif (trail == NULL) {\n\t\t\treturn true;\n\t\t}\n\n\t\tif (config_fill && unlikely(opt_junk_free)) {\n\t\t\tlarge_dalloc_maybe_junk(extent_addr_get(trail),\n\t\t\t    extent_size_get(trail));\n\t\t}\n\n\t\tarena_extents_dirty_dalloc(tsdn, arena, &extent_hooks, trail);\n\t}\n\n\tarena_extent_ralloc_large_shrink(tsdn, arena, extent, oldusize);\n\n\treturn false;\n}\n\nstatic bool\nlarge_ralloc_no_move_expand(tsdn_t *tsdn, extent_t *extent, size_t usize,\n    bool zero) {\n\tarena_t *arena = extent_arena_get(extent);\n\tsize_t oldusize = extent_usize_get(extent);\n\textent_hooks_t *extent_hooks = extent_hooks_get(arena);\n\tsize_t trailsize = usize - oldusize;\n\n\tif (extent_hooks->merge == NULL) {\n\t\treturn true;\n\t}\n\n\tif (config_fill && unlikely(opt_zero)) {\n\t\tzero = true;\n\t}\n\t/*\n\t * Copy zero into is_zeroed_trail and pass the copy when allocating the\n\t * extent, so that it is possible to make correct junk/zero fill\n\t * decisions below, even if is_zeroed_trail ends up true when zero is\n\t * false.\n\t */\n\tbool is_zeroed_trail = zero;\n\tbool commit = true;\n\textent_t *trail;\n\tbool new_mapping;\n\tif ((trail = extents_alloc(tsdn, arena, &extent_hooks,\n\t    &arena->extents_dirty, extent_past_get(extent), trailsize, 0,\n\t    CACHELINE, false, SC_NSIZES, &is_zeroed_trail, &commit)) != NULL\n\t    || (trail = extents_alloc(tsdn, arena, &extent_hooks,\n\t    &arena->extents_muzzy, extent_past_get(extent), trailsize, 0,\n\t    CACHELINE, false, SC_NSIZES, &is_zeroed_trail, &commit)) != NULL) {\n\t\tif (config_stats) {\n\t\t\tnew_mapping = false;\n\t\t}\n\t} else {\n\t\tif ((trail = extent_alloc_wrapper(tsdn, arena, &extent_hooks,\n\t\t    extent_past_get(extent), trailsize, 0, CACHELINE, false,\n\t\t    SC_NSIZES, &is_zeroed_trail, &commit)) == NULL) {\n\t\t\treturn true;\n\t\t}\n\t\tif (config_stats) {\n\t\t\tnew_mapping = true;\n\t\t}\n\t}\n\n\tif (extent_merge_wrapper(tsdn, arena, &extent_hooks, extent, trail)) {\n\t\textent_dalloc_wrapper(tsdn, arena, &extent_hooks, trail);\n\t\treturn true;\n\t}\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\tszind_t szind = sz_size2index(usize);\n\textent_szind_set(extent, szind);\n\trtree_szind_slab_update(tsdn, &extents_rtree, rtree_ctx,\n\t    (uintptr_t)extent_addr_get(extent), szind, false);\n\n\tif (config_stats && new_mapping) {\n\t\tarena_stats_mapped_add(tsdn, &arena->stats, trailsize);\n\t}\n\n\tif (zero) {\n\t\tif (config_cache_oblivious) {\n\t\t\t/*\n\t\t\t * Zero the trailing bytes of the original allocation's\n\t\t\t * last page, since they are in an indeterminate state.\n\t\t\t * There will always be trailing bytes, because ptr's\n\t\t\t * offset from the beginning of the extent is a multiple\n\t\t\t * of CACHELINE in [0 .. PAGE).\n\t\t\t */\n\t\t\tvoid *zbase = (void *)\n\t\t\t    ((uintptr_t)extent_addr_get(extent) + oldusize);\n\t\t\tvoid *zpast = PAGE_ADDR2BASE((void *)((uintptr_t)zbase +\n\t\t\t    PAGE));\n\t\t\tsize_t nzero = (uintptr_t)zpast - (uintptr_t)zbase;\n\t\t\tassert(nzero > 0);\n\t\t\tmemset(zbase, 0, nzero);\n\t\t}\n\t\tassert(is_zeroed_trail);\n\t} else if (config_fill && unlikely(opt_junk_alloc)) {\n\t\tmemset((void *)((uintptr_t)extent_addr_get(extent) + oldusize),\n\t\t    JEMALLOC_ALLOC_JUNK, usize - oldusize);\n\t}\n\n\tarena_extent_ralloc_large_expand(tsdn, arena, extent, oldusize);\n\n\treturn false;\n}\n\nbool\nlarge_ralloc_no_move(tsdn_t *tsdn, extent_t *extent, size_t usize_min,\n    size_t usize_max, bool zero) {\n\tsize_t oldusize = extent_usize_get(extent);\n\n\t/* The following should have been caught by callers. */\n\tassert(usize_min > 0 && usize_max <= SC_LARGE_MAXCLASS);\n\t/* Both allocation sizes must be large to avoid a move. */\n\tassert(oldusize >= SC_LARGE_MINCLASS\n\t    && usize_max >= SC_LARGE_MINCLASS);\n\n\tif (usize_max > oldusize) {\n\t\t/* Attempt to expand the allocation in-place. */\n\t\tif (!large_ralloc_no_move_expand(tsdn, extent, usize_max,\n\t\t    zero)) {\n\t\t\tarena_decay_tick(tsdn, extent_arena_get(extent));\n\t\t\treturn false;\n\t\t}\n\t\t/* Try again, this time with usize_min. */\n\t\tif (usize_min < usize_max && usize_min > oldusize &&\n\t\t    large_ralloc_no_move_expand(tsdn, extent, usize_min,\n\t\t    zero)) {\n\t\t\tarena_decay_tick(tsdn, extent_arena_get(extent));\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t/*\n\t * Avoid moving the allocation if the existing extent size accommodates\n\t * the new size.\n\t */\n\tif (oldusize >= usize_min && oldusize <= usize_max) {\n\t\tarena_decay_tick(tsdn, extent_arena_get(extent));\n\t\treturn false;\n\t}\n\n\t/* Attempt to shrink the allocation in-place. */\n\tif (oldusize > usize_max) {\n\t\tif (!large_ralloc_no_move_shrink(tsdn, extent, usize_max)) {\n\t\t\tarena_decay_tick(tsdn, extent_arena_get(extent));\n\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n}\n\nstatic void *\nlarge_ralloc_move_helper(tsdn_t *tsdn, arena_t *arena, size_t usize,\n    size_t alignment, bool zero) {\n\tif (alignment <= CACHELINE) {\n\t\treturn large_malloc(tsdn, arena, usize, zero);\n\t}\n\treturn large_palloc(tsdn, arena, usize, alignment, zero);\n}\n\nvoid *\nlarge_ralloc(tsdn_t *tsdn, arena_t *arena, void *ptr, size_t usize,\n    size_t alignment, bool zero, tcache_t *tcache,\n    hook_ralloc_args_t *hook_args) {\n\textent_t *extent = iealloc(tsdn, ptr);\n\n\tsize_t oldusize = extent_usize_get(extent);\n\t/* The following should have been caught by callers. */\n\tassert(usize > 0 && usize <= SC_LARGE_MAXCLASS);\n\t/* Both allocation sizes must be large to avoid a move. */\n\tassert(oldusize >= SC_LARGE_MINCLASS\n\t    && usize >= SC_LARGE_MINCLASS);\n\n\t/* Try to avoid moving the allocation. */\n\tif (!large_ralloc_no_move(tsdn, extent, usize, usize, zero)) {\n\t\thook_invoke_expand(hook_args->is_realloc\n\t\t    ? hook_expand_realloc : hook_expand_rallocx, ptr, oldusize,\n\t\t    usize, (uintptr_t)ptr, hook_args->args);\n\t\treturn extent_addr_get(extent);\n\t}\n\n\t/*\n\t * usize and old size are different enough that we need to use a\n\t * different size class.  In that case, fall back to allocating new\n\t * space and copying.\n\t */\n\tvoid *ret = large_ralloc_move_helper(tsdn, arena, usize, alignment,\n\t    zero);\n\tif (ret == NULL) {\n\t\treturn NULL;\n\t}\n\n\thook_invoke_alloc(hook_args->is_realloc\n\t    ? hook_alloc_realloc : hook_alloc_rallocx, ret, (uintptr_t)ret,\n\t    hook_args->args);\n\thook_invoke_dalloc(hook_args->is_realloc\n\t    ? hook_dalloc_realloc : hook_dalloc_rallocx, ptr, hook_args->args);\n\n\tsize_t copysize = (usize < oldusize) ? usize : oldusize;\n\tmemcpy(ret, extent_addr_get(extent), copysize);\n\tisdalloct(tsdn, extent_addr_get(extent), oldusize, tcache, NULL, true);\n\treturn ret;\n}\n\n/*\n * junked_locked indicates whether the extent's data have been junk-filled, and\n * whether the arena's large_mtx is currently held.\n */\nstatic void\nlarge_dalloc_prep_impl(tsdn_t *tsdn, arena_t *arena, extent_t *extent,\n    bool junked_locked) {\n\tif (!junked_locked) {\n\t\t/* See comments in arena_bin_slabs_full_insert(). */\n\t\tif (!arena_is_auto(arena)) {\n\t\t\tmalloc_mutex_lock(tsdn, &arena->large_mtx);\n\t\t\textent_list_remove(&arena->large, extent);\n\t\t\tmalloc_mutex_unlock(tsdn, &arena->large_mtx);\n\t\t}\n\t\tlarge_dalloc_maybe_junk(extent_addr_get(extent),\n\t\t    extent_usize_get(extent));\n\t} else {\n\t\t/* Only hold the large_mtx if necessary. */\n\t\tif (!arena_is_auto(arena)) {\n\t\t\tmalloc_mutex_assert_owner(tsdn, &arena->large_mtx);\n\t\t\textent_list_remove(&arena->large, extent);\n\t\t}\n\t}\n\tarena_extent_dalloc_large_prep(tsdn, arena, extent);\n}\n\nstatic void\nlarge_dalloc_finish_impl(tsdn_t *tsdn, arena_t *arena, extent_t *extent) {\n\textent_hooks_t *extent_hooks = EXTENT_HOOKS_INITIALIZER;\n\tarena_extents_dirty_dalloc(tsdn, arena, &extent_hooks, extent);\n}\n\nvoid\nlarge_dalloc_prep_junked_locked(tsdn_t *tsdn, extent_t *extent) {\n\tlarge_dalloc_prep_impl(tsdn, extent_arena_get(extent), extent, true);\n}\n\nvoid\nlarge_dalloc_finish(tsdn_t *tsdn, extent_t *extent) {\n\tlarge_dalloc_finish_impl(tsdn, extent_arena_get(extent), extent);\n}\n\nvoid\nlarge_dalloc(tsdn_t *tsdn, extent_t *extent) {\n\tarena_t *arena = extent_arena_get(extent);\n\tlarge_dalloc_prep_impl(tsdn, arena, extent, false);\n\tlarge_dalloc_finish_impl(tsdn, arena, extent);\n\tarena_decay_tick(tsdn, arena);\n}\n\nsize_t\nlarge_salloc(tsdn_t *tsdn, const extent_t *extent) {\n\treturn extent_usize_get(extent);\n}\n\nprof_tctx_t *\nlarge_prof_tctx_get(tsdn_t *tsdn, const extent_t *extent) {\n\treturn extent_prof_tctx_get(extent);\n}\n\nvoid\nlarge_prof_tctx_set(tsdn_t *tsdn, extent_t *extent, prof_tctx_t *tctx) {\n\textent_prof_tctx_set(extent, tctx);\n}\n\nvoid\nlarge_prof_tctx_reset(tsdn_t *tsdn, extent_t *extent) {\n\tlarge_prof_tctx_set(tsdn, extent, (prof_tctx_t *)(uintptr_t)1U);\n}\n\nnstime_t\nlarge_prof_alloc_time_get(const extent_t *extent) {\n\treturn extent_prof_alloc_time_get(extent);\n}\n\nvoid\nlarge_prof_alloc_time_set(extent_t *extent, nstime_t t) {\n\textent_prof_alloc_time_set(extent, t);\n}\n"
  },
  {
    "path": "deps/jemalloc/src/log.c",
    "content": "#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/log.h\"\n\nchar log_var_names[JEMALLOC_LOG_VAR_BUFSIZE];\natomic_b_t log_init_done = ATOMIC_INIT(false);\n\n/*\n * Returns true if we were able to pick out a segment.  Fills in r_segment_end\n * with a pointer to the first character after the end of the string.\n */\nstatic const char *\nlog_var_extract_segment(const char* segment_begin) {\n\tconst char *end;\n\tfor (end = segment_begin; *end != '\\0' && *end != '|'; end++) {\n\t}\n\treturn end;\n}\n\nstatic bool\nlog_var_matches_segment(const char *segment_begin, const char *segment_end,\n    const char *log_var_begin, const char *log_var_end) {\n\tassert(segment_begin <= segment_end);\n\tassert(log_var_begin < log_var_end);\n\n\tptrdiff_t segment_len = segment_end - segment_begin;\n\tptrdiff_t log_var_len = log_var_end - log_var_begin;\n\t/* The special '.' segment matches everything. */\n\tif (segment_len == 1 && *segment_begin == '.') {\n\t\treturn true;\n\t}\n        if (segment_len == log_var_len) {\n\t\treturn strncmp(segment_begin, log_var_begin, segment_len) == 0;\n\t} else if (segment_len < log_var_len) {\n\t\treturn strncmp(segment_begin, log_var_begin, segment_len) == 0\n\t\t    && log_var_begin[segment_len] == '.';\n        } else {\n\t\treturn false;\n\t}\n}\n\nunsigned\nlog_var_update_state(log_var_t *log_var) {\n\tconst char *log_var_begin = log_var->name;\n\tconst char *log_var_end = log_var->name + strlen(log_var->name);\n\n\t/* Pointer to one before the beginning of the current segment. */\n\tconst char *segment_begin = log_var_names;\n\n\t/*\n\t * If log_init done is false, we haven't parsed the malloc conf yet.  To\n\t * avoid log-spew, we default to not displaying anything.\n\t */\n\tif (!atomic_load_b(&log_init_done, ATOMIC_ACQUIRE)) {\n\t\treturn LOG_INITIALIZED_NOT_ENABLED;\n\t}\n\n\twhile (true) {\n\t\tconst char *segment_end = log_var_extract_segment(\n\t\t    segment_begin);\n\t\tassert(segment_end < log_var_names + JEMALLOC_LOG_VAR_BUFSIZE);\n\t\tif (log_var_matches_segment(segment_begin, segment_end,\n\t\t    log_var_begin, log_var_end)) {\n\t\t\tatomic_store_u(&log_var->state, LOG_ENABLED,\n\t\t\t    ATOMIC_RELAXED);\n\t\t\treturn LOG_ENABLED;\n\t\t}\n\t\tif (*segment_end == '\\0') {\n\t\t\t/* Hit the end of the segment string with no match. */\n\t\t\tatomic_store_u(&log_var->state,\n\t\t\t    LOG_INITIALIZED_NOT_ENABLED, ATOMIC_RELAXED);\n\t\t\treturn LOG_INITIALIZED_NOT_ENABLED;\n\t\t}\n\t\t/* Otherwise, skip the delimiter and continue. */\n\t\tsegment_begin = segment_end + 1;\n\t}\n}\n"
  },
  {
    "path": "deps/jemalloc/src/malloc_io.c",
    "content": "#define JEMALLOC_MALLOC_IO_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/malloc_io.h\"\n#include \"jemalloc/internal/util.h\"\n\n#ifdef assert\n#  undef assert\n#endif\n#ifdef not_reached\n#  undef not_reached\n#endif\n#ifdef not_implemented\n#  undef not_implemented\n#endif\n#ifdef assert_not_implemented\n#  undef assert_not_implemented\n#endif\n\n/*\n * Define simple versions of assertion macros that won't recurse in case\n * of assertion failures in malloc_*printf().\n */\n#define assert(e) do {\t\t\t\t\t\t\t\\\n\tif (config_debug && !(e)) {\t\t\t\t\t\\\n\t\tmalloc_write(\"<jemalloc>: Failed assertion\\n\");\t\t\\\n\t\tabort();\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define not_reached() do {\t\t\t\t\t\t\\\n\tif (config_debug) {\t\t\t\t\t\t\\\n\t\tmalloc_write(\"<jemalloc>: Unreachable code reached\\n\");\t\\\n\t\tabort();\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tunreachable();\t\t\t\t\t\t\t\\\n} while (0)\n\n#define not_implemented() do {\t\t\t\t\t\t\\\n\tif (config_debug) {\t\t\t\t\t\t\\\n\t\tmalloc_write(\"<jemalloc>: Not implemented\\n\");\t\t\\\n\t\tabort();\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define assert_not_implemented(e) do {\t\t\t\t\t\\\n\tif (unlikely(config_debug && !(e))) {\t\t\t\t\\\n\t\tnot_implemented();\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n/******************************************************************************/\n/* Function prototypes for non-inline static functions. */\n\nstatic void wrtmessage(void *cbopaque, const char *s);\n#define U2S_BUFSIZE ((1U << (LG_SIZEOF_INTMAX_T + 3)) + 1)\nstatic char *u2s(uintmax_t x, unsigned base, bool uppercase, char *s,\n    size_t *slen_p);\n#define D2S_BUFSIZE (1 + U2S_BUFSIZE)\nstatic char *d2s(intmax_t x, char sign, char *s, size_t *slen_p);\n#define O2S_BUFSIZE (1 + U2S_BUFSIZE)\nstatic char *o2s(uintmax_t x, bool alt_form, char *s, size_t *slen_p);\n#define X2S_BUFSIZE (2 + U2S_BUFSIZE)\nstatic char *x2s(uintmax_t x, bool alt_form, bool uppercase, char *s,\n    size_t *slen_p);\n\n/******************************************************************************/\n\n/* malloc_message() setup. */\nstatic void\nwrtmessage(void *cbopaque, const char *s) {\n\tmalloc_write_fd(STDERR_FILENO, s, strlen(s));\n}\n\nJEMALLOC_EXPORT void\t(*je_malloc_message)(void *, const char *s);\n\n/*\n * Wrapper around malloc_message() that avoids the need for\n * je_malloc_message(...) throughout the code.\n */\nvoid\nmalloc_write(const char *s) {\n\tif (je_malloc_message != NULL) {\n\t\tje_malloc_message(NULL, s);\n\t} else {\n\t\twrtmessage(NULL, s);\n\t}\n}\n\n/*\n * glibc provides a non-standard strerror_r() when _GNU_SOURCE is defined, so\n * provide a wrapper.\n */\nint\nbuferror(int err, char *buf, size_t buflen) {\n#ifdef _WIN32\n\tFormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, 0,\n\t    (LPSTR)buf, (DWORD)buflen, NULL);\n\treturn 0;\n#elif defined(JEMALLOC_STRERROR_R_RETURNS_CHAR_WITH_GNU_SOURCE) && defined(_GNU_SOURCE)\n\tchar *b = strerror_r(err, buf, buflen);\n\tif (b != buf) {\n\t\tstrncpy(buf, b, buflen);\n\t\tbuf[buflen-1] = '\\0';\n\t}\n\treturn 0;\n#else\n\treturn strerror_r(err, buf, buflen);\n#endif\n}\n\nuintmax_t\nmalloc_strtoumax(const char *restrict nptr, char **restrict endptr, int base) {\n\tuintmax_t ret, digit;\n\tunsigned b;\n\tbool neg;\n\tconst char *p, *ns;\n\n\tp = nptr;\n\tif (base < 0 || base == 1 || base > 36) {\n\t\tns = p;\n\t\tset_errno(EINVAL);\n\t\tret = UINTMAX_MAX;\n\t\tgoto label_return;\n\t}\n\tb = base;\n\n\t/* Swallow leading whitespace and get sign, if any. */\n\tneg = false;\n\twhile (true) {\n\t\tswitch (*p) {\n\t\tcase '\\t': case '\\n': case '\\v': case '\\f': case '\\r': case ' ':\n\t\t\tp++;\n\t\t\tbreak;\n\t\tcase '-':\n\t\t\tneg = true;\n\t\t\t/* Fall through. */\n\t\tcase '+':\n\t\t\tp++;\n\t\t\t/* Fall through. */\n\t\tdefault:\n\t\t\tgoto label_prefix;\n\t\t}\n\t}\n\n\t/* Get prefix, if any. */\n\tlabel_prefix:\n\t/*\n\t * Note where the first non-whitespace/sign character is so that it is\n\t * possible to tell whether any digits are consumed (e.g., \"  0\" vs.\n\t * \"  -x\").\n\t */\n\tns = p;\n\tif (*p == '0') {\n\t\tswitch (p[1]) {\n\t\tcase '0': case '1': case '2': case '3': case '4': case '5':\n\t\tcase '6': case '7':\n\t\t\tif (b == 0) {\n\t\t\t\tb = 8;\n\t\t\t}\n\t\t\tif (b == 8) {\n\t\t\t\tp++;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase 'X': case 'x':\n\t\t\tswitch (p[2]) {\n\t\t\tcase '0': case '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9':\n\t\t\tcase 'A': case 'B': case 'C': case 'D': case 'E':\n\t\t\tcase 'F':\n\t\t\tcase 'a': case 'b': case 'c': case 'd': case 'e':\n\t\t\tcase 'f':\n\t\t\t\tif (b == 0) {\n\t\t\t\t\tb = 16;\n\t\t\t\t}\n\t\t\t\tif (b == 16) {\n\t\t\t\t\tp += 2;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tp++;\n\t\t\tret = 0;\n\t\t\tgoto label_return;\n\t\t}\n\t}\n\tif (b == 0) {\n\t\tb = 10;\n\t}\n\n\t/* Convert. */\n\tret = 0;\n\twhile ((*p >= '0' && *p <= '9' && (digit = *p - '0') < b)\n\t    || (*p >= 'A' && *p <= 'Z' && (digit = 10 + *p - 'A') < b)\n\t    || (*p >= 'a' && *p <= 'z' && (digit = 10 + *p - 'a') < b)) {\n\t\tuintmax_t pret = ret;\n\t\tret *= b;\n\t\tret += digit;\n\t\tif (ret < pret) {\n\t\t\t/* Overflow. */\n\t\t\tset_errno(ERANGE);\n\t\t\tret = UINTMAX_MAX;\n\t\t\tgoto label_return;\n\t\t}\n\t\tp++;\n\t}\n\tif (neg) {\n\t\tret = (uintmax_t)(-((intmax_t)ret));\n\t}\n\n\tif (p == ns) {\n\t\t/* No conversion performed. */\n\t\tset_errno(EINVAL);\n\t\tret = UINTMAX_MAX;\n\t\tgoto label_return;\n\t}\n\nlabel_return:\n\tif (endptr != NULL) {\n\t\tif (p == ns) {\n\t\t\t/* No characters were converted. */\n\t\t\t*endptr = (char *)nptr;\n\t\t} else {\n\t\t\t*endptr = (char *)p;\n\t\t}\n\t}\n\treturn ret;\n}\n\nstatic char *\nu2s(uintmax_t x, unsigned base, bool uppercase, char *s, size_t *slen_p) {\n\tunsigned i;\n\n\ti = U2S_BUFSIZE - 1;\n\ts[i] = '\\0';\n\tswitch (base) {\n\tcase 10:\n\t\tdo {\n\t\t\ti--;\n\t\t\ts[i] = \"0123456789\"[x % (uint64_t)10];\n\t\t\tx /= (uint64_t)10;\n\t\t} while (x > 0);\n\t\tbreak;\n\tcase 16: {\n\t\tconst char *digits = (uppercase)\n\t\t    ? \"0123456789ABCDEF\"\n\t\t    : \"0123456789abcdef\";\n\n\t\tdo {\n\t\t\ti--;\n\t\t\ts[i] = digits[x & 0xf];\n\t\t\tx >>= 4;\n\t\t} while (x > 0);\n\t\tbreak;\n\t} default: {\n\t\tconst char *digits = (uppercase)\n\t\t    ? \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n\t\t    : \"0123456789abcdefghijklmnopqrstuvwxyz\";\n\n\t\tassert(base >= 2 && base <= 36);\n\t\tdo {\n\t\t\ti--;\n\t\t\ts[i] = digits[x % (uint64_t)base];\n\t\t\tx /= (uint64_t)base;\n\t\t} while (x > 0);\n\t}}\n\n\t*slen_p = U2S_BUFSIZE - 1 - i;\n\treturn &s[i];\n}\n\nstatic char *\nd2s(intmax_t x, char sign, char *s, size_t *slen_p) {\n\tbool neg;\n\n\tif ((neg = (x < 0))) {\n\t\tx = -x;\n\t}\n\ts = u2s(x, 10, false, s, slen_p);\n\tif (neg) {\n\t\tsign = '-';\n\t}\n\tswitch (sign) {\n\tcase '-':\n\t\tif (!neg) {\n\t\t\tbreak;\n\t\t}\n\t\t/* Fall through. */\n\tcase ' ':\n\tcase '+':\n\t\ts--;\n\t\t(*slen_p)++;\n\t\t*s = sign;\n\t\tbreak;\n\tdefault: not_reached();\n\t}\n\treturn s;\n}\n\nstatic char *\no2s(uintmax_t x, bool alt_form, char *s, size_t *slen_p) {\n\ts = u2s(x, 8, false, s, slen_p);\n\tif (alt_form && *s != '0') {\n\t\ts--;\n\t\t(*slen_p)++;\n\t\t*s = '0';\n\t}\n\treturn s;\n}\n\nstatic char *\nx2s(uintmax_t x, bool alt_form, bool uppercase, char *s, size_t *slen_p) {\n\ts = u2s(x, 16, uppercase, s, slen_p);\n\tif (alt_form) {\n\t\ts -= 2;\n\t\t(*slen_p) += 2;\n\t\tmemcpy(s, uppercase ? \"0X\" : \"0x\", 2);\n\t}\n\treturn s;\n}\n\nsize_t\nmalloc_vsnprintf(char *str, size_t size, const char *format, va_list ap) {\n\tsize_t i;\n\tconst char *f;\n\n#define APPEND_C(c) do {\t\t\t\t\t\t\\\n\tif (i < size) {\t\t\t\t\t\t\t\\\n\t\tstr[i] = (c);\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\ti++;\t\t\t\t\t\t\t\t\\\n} while (0)\n#define APPEND_S(s, slen) do {\t\t\t\t\t\t\\\n\tif (i < size) {\t\t\t\t\t\t\t\\\n\t\tsize_t cpylen = (slen <= size - i) ? slen : size - i;\t\\\n\t\tmemcpy(&str[i], s, cpylen);\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\ti += slen;\t\t\t\t\t\t\t\\\n} while (0)\n#define APPEND_PADDED_S(s, slen, width, left_justify) do {\t\t\\\n\t/* Left padding. */\t\t\t\t\t\t\\\n\tsize_t pad_len = (width == -1) ? 0 : ((slen < (size_t)width) ?\t\\\n\t    (size_t)width - slen : 0);\t\t\t\t\t\\\n\tif (!left_justify && pad_len != 0) {\t\t\t\t\\\n\t\tsize_t j;\t\t\t\t\t\t\\\n\t\tfor (j = 0; j < pad_len; j++) {\t\t\t\t\\\n\t\t\tAPPEND_C(' ');\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\t/* Value. */\t\t\t\t\t\t\t\\\n\tAPPEND_S(s, slen);\t\t\t\t\t\t\\\n\t/* Right padding. */\t\t\t\t\t\t\\\n\tif (left_justify && pad_len != 0) {\t\t\t\t\\\n\t\tsize_t j;\t\t\t\t\t\t\\\n\t\tfor (j = 0; j < pad_len; j++) {\t\t\t\t\\\n\t\t\tAPPEND_C(' ');\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n#define GET_ARG_NUMERIC(val, len) do {\t\t\t\t\t\\\n\tswitch ((unsigned char)len) {\t\t\t\t\t\\\n\tcase '?':\t\t\t\t\t\t\t\\\n\t\tval = va_arg(ap, int);\t\t\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\tcase '?' | 0x80:\t\t\t\t\t\t\\\n\t\tval = va_arg(ap, unsigned int);\t\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\tcase 'l':\t\t\t\t\t\t\t\\\n\t\tval = va_arg(ap, long);\t\t\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\tcase 'l' | 0x80:\t\t\t\t\t\t\\\n\t\tval = va_arg(ap, unsigned long);\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\tcase 'q':\t\t\t\t\t\t\t\\\n\t\tval = va_arg(ap, long long);\t\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\tcase 'q' | 0x80:\t\t\t\t\t\t\\\n\t\tval = va_arg(ap, unsigned long long);\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\tcase 'j':\t\t\t\t\t\t\t\\\n\t\tval = va_arg(ap, intmax_t);\t\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\tcase 'j' | 0x80:\t\t\t\t\t\t\\\n\t\tval = va_arg(ap, uintmax_t);\t\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\tcase 't':\t\t\t\t\t\t\t\\\n\t\tval = va_arg(ap, ptrdiff_t);\t\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\tcase 'z':\t\t\t\t\t\t\t\\\n\t\tval = va_arg(ap, ssize_t);\t\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\tcase 'z' | 0x80:\t\t\t\t\t\t\\\n\t\tval = va_arg(ap, size_t);\t\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\tcase 'p': /* Synthetic; used for %p. */\t\t\t\t\\\n\t\tval = va_arg(ap, uintptr_t);\t\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\tdefault:\t\t\t\t\t\t\t\\\n\t\tnot_reached();\t\t\t\t\t\t\\\n\t\tval = 0;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n\ti = 0;\n\tf = format;\n\twhile (true) {\n\t\tswitch (*f) {\n\t\tcase '\\0': goto label_out;\n\t\tcase '%': {\n\t\t\tbool alt_form = false;\n\t\t\tbool left_justify = false;\n\t\t\tbool plus_space = false;\n\t\t\tbool plus_plus = false;\n\t\t\tint prec = -1;\n\t\t\tint width = -1;\n\t\t\tunsigned char len = '?';\n\t\t\tchar *s;\n\t\t\tsize_t slen;\n\n\t\t\tf++;\n\t\t\t/* Flags. */\n\t\t\twhile (true) {\n\t\t\t\tswitch (*f) {\n\t\t\t\tcase '#':\n\t\t\t\t\tassert(!alt_form);\n\t\t\t\t\talt_form = true;\n\t\t\t\t\tbreak;\n\t\t\t\tcase '-':\n\t\t\t\t\tassert(!left_justify);\n\t\t\t\t\tleft_justify = true;\n\t\t\t\t\tbreak;\n\t\t\t\tcase ' ':\n\t\t\t\t\tassert(!plus_space);\n\t\t\t\t\tplus_space = true;\n\t\t\t\t\tbreak;\n\t\t\t\tcase '+':\n\t\t\t\t\tassert(!plus_plus);\n\t\t\t\t\tplus_plus = true;\n\t\t\t\t\tbreak;\n\t\t\t\tdefault: goto label_width;\n\t\t\t\t}\n\t\t\t\tf++;\n\t\t\t}\n\t\t\t/* Width. */\n\t\t\tlabel_width:\n\t\t\tswitch (*f) {\n\t\t\tcase '*':\n\t\t\t\twidth = va_arg(ap, int);\n\t\t\t\tf++;\n\t\t\t\tif (width < 0) {\n\t\t\t\t\tleft_justify = true;\n\t\t\t\t\twidth = -width;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase '0': case '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9': {\n\t\t\t\tuintmax_t uwidth;\n\t\t\t\tset_errno(0);\n\t\t\t\tuwidth = malloc_strtoumax(f, (char **)&f, 10);\n\t\t\t\tassert(uwidth != UINTMAX_MAX || get_errno() !=\n\t\t\t\t    ERANGE);\n\t\t\t\twidth = (int)uwidth;\n\t\t\t\tbreak;\n\t\t\t} default:\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\t/* Width/precision separator. */\n\t\t\tif (*f == '.') {\n\t\t\t\tf++;\n\t\t\t} else {\n\t\t\t\tgoto label_length;\n\t\t\t}\n\t\t\t/* Precision. */\n\t\t\tswitch (*f) {\n\t\t\tcase '*':\n\t\t\t\tprec = va_arg(ap, int);\n\t\t\t\tf++;\n\t\t\t\tbreak;\n\t\t\tcase '0': case '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9': {\n\t\t\t\tuintmax_t uprec;\n\t\t\t\tset_errno(0);\n\t\t\t\tuprec = malloc_strtoumax(f, (char **)&f, 10);\n\t\t\t\tassert(uprec != UINTMAX_MAX || get_errno() !=\n\t\t\t\t    ERANGE);\n\t\t\t\tprec = (int)uprec;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tdefault: break;\n\t\t\t}\n\t\t\t/* Length. */\n\t\t\tlabel_length:\n\t\t\tswitch (*f) {\n\t\t\tcase 'l':\n\t\t\t\tf++;\n\t\t\t\tif (*f == 'l') {\n\t\t\t\t\tlen = 'q';\n\t\t\t\t\tf++;\n\t\t\t\t} else {\n\t\t\t\t\tlen = 'l';\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase 'q': case 'j': case 't': case 'z':\n\t\t\t\tlen = *f;\n\t\t\t\tf++;\n\t\t\t\tbreak;\n\t\t\tdefault: break;\n\t\t\t}\n\t\t\t/* Conversion specifier. */\n\t\t\tswitch (*f) {\n\t\t\tcase '%':\n\t\t\t\t/* %% */\n\t\t\t\tAPPEND_C(*f);\n\t\t\t\tf++;\n\t\t\t\tbreak;\n\t\t\tcase 'd': case 'i': {\n\t\t\t\tintmax_t val JEMALLOC_CC_SILENCE_INIT(0);\n\t\t\t\tchar buf[D2S_BUFSIZE];\n\n\t\t\t\tGET_ARG_NUMERIC(val, len);\n\t\t\t\ts = d2s(val, (plus_plus ? '+' : (plus_space ?\n\t\t\t\t    ' ' : '-')), buf, &slen);\n\t\t\t\tAPPEND_PADDED_S(s, slen, width, left_justify);\n\t\t\t\tf++;\n\t\t\t\tbreak;\n\t\t\t} case 'o': {\n\t\t\t\tuintmax_t val JEMALLOC_CC_SILENCE_INIT(0);\n\t\t\t\tchar buf[O2S_BUFSIZE];\n\n\t\t\t\tGET_ARG_NUMERIC(val, len | 0x80);\n\t\t\t\ts = o2s(val, alt_form, buf, &slen);\n\t\t\t\tAPPEND_PADDED_S(s, slen, width, left_justify);\n\t\t\t\tf++;\n\t\t\t\tbreak;\n\t\t\t} case 'u': {\n\t\t\t\tuintmax_t val JEMALLOC_CC_SILENCE_INIT(0);\n\t\t\t\tchar buf[U2S_BUFSIZE];\n\n\t\t\t\tGET_ARG_NUMERIC(val, len | 0x80);\n\t\t\t\ts = u2s(val, 10, false, buf, &slen);\n\t\t\t\tAPPEND_PADDED_S(s, slen, width, left_justify);\n\t\t\t\tf++;\n\t\t\t\tbreak;\n\t\t\t} case 'x': case 'X': {\n\t\t\t\tuintmax_t val JEMALLOC_CC_SILENCE_INIT(0);\n\t\t\t\tchar buf[X2S_BUFSIZE];\n\n\t\t\t\tGET_ARG_NUMERIC(val, len | 0x80);\n\t\t\t\ts = x2s(val, alt_form, *f == 'X', buf, &slen);\n\t\t\t\tAPPEND_PADDED_S(s, slen, width, left_justify);\n\t\t\t\tf++;\n\t\t\t\tbreak;\n\t\t\t} case 'c': {\n\t\t\t\tunsigned char val;\n\t\t\t\tchar buf[2];\n\n\t\t\t\tassert(len == '?' || len == 'l');\n\t\t\t\tassert_not_implemented(len != 'l');\n\t\t\t\tval = va_arg(ap, int);\n\t\t\t\tbuf[0] = val;\n\t\t\t\tbuf[1] = '\\0';\n\t\t\t\tAPPEND_PADDED_S(buf, 1, width, left_justify);\n\t\t\t\tf++;\n\t\t\t\tbreak;\n\t\t\t} case 's':\n\t\t\t\tassert(len == '?' || len == 'l');\n\t\t\t\tassert_not_implemented(len != 'l');\n\t\t\t\ts = va_arg(ap, char *);\n\t\t\t\tslen = (prec < 0) ? strlen(s) : (size_t)prec;\n\t\t\t\tAPPEND_PADDED_S(s, slen, width, left_justify);\n\t\t\t\tf++;\n\t\t\t\tbreak;\n\t\t\tcase 'p': {\n\t\t\t\tuintmax_t val;\n\t\t\t\tchar buf[X2S_BUFSIZE];\n\n\t\t\t\tGET_ARG_NUMERIC(val, 'p');\n\t\t\t\ts = x2s(val, true, false, buf, &slen);\n\t\t\t\tAPPEND_PADDED_S(s, slen, width, left_justify);\n\t\t\t\tf++;\n\t\t\t\tbreak;\n\t\t\t} default: not_reached();\n\t\t\t}\n\t\t\tbreak;\n\t\t} default: {\n\t\t\tAPPEND_C(*f);\n\t\t\tf++;\n\t\t\tbreak;\n\t\t}}\n\t}\n\tlabel_out:\n\tif (i < size) {\n\t\tstr[i] = '\\0';\n\t} else {\n\t\tstr[size - 1] = '\\0';\n\t}\n\n#undef APPEND_C\n#undef APPEND_S\n#undef APPEND_PADDED_S\n#undef GET_ARG_NUMERIC\n\treturn i;\n}\n\nJEMALLOC_FORMAT_PRINTF(3, 4)\nsize_t\nmalloc_snprintf(char *str, size_t size, const char *format, ...) {\n\tsize_t ret;\n\tva_list ap;\n\n\tva_start(ap, format);\n\tret = malloc_vsnprintf(str, size, format, ap);\n\tva_end(ap);\n\n\treturn ret;\n}\n\nvoid\nmalloc_vcprintf(void (*write_cb)(void *, const char *), void *cbopaque,\n    const char *format, va_list ap) {\n\tchar buf[MALLOC_PRINTF_BUFSIZE];\n\n\tif (write_cb == NULL) {\n\t\t/*\n\t\t * The caller did not provide an alternate write_cb callback\n\t\t * function, so use the default one.  malloc_write() is an\n\t\t * inline function, so use malloc_message() directly here.\n\t\t */\n\t\twrite_cb = (je_malloc_message != NULL) ? je_malloc_message :\n\t\t    wrtmessage;\n\t}\n\n\tmalloc_vsnprintf(buf, sizeof(buf), format, ap);\n\twrite_cb(cbopaque, buf);\n}\n\n/*\n * Print to a callback function in such a way as to (hopefully) avoid memory\n * allocation.\n */\nJEMALLOC_FORMAT_PRINTF(3, 4)\nvoid\nmalloc_cprintf(void (*write_cb)(void *, const char *), void *cbopaque,\n    const char *format, ...) {\n\tva_list ap;\n\n\tva_start(ap, format);\n\tmalloc_vcprintf(write_cb, cbopaque, format, ap);\n\tva_end(ap);\n}\n\n/* Print to stderr in such a way as to avoid memory allocation. */\nJEMALLOC_FORMAT_PRINTF(1, 2)\nvoid\nmalloc_printf(const char *format, ...) {\n\tva_list ap;\n\n\tva_start(ap, format);\n\tmalloc_vcprintf(NULL, NULL, format, ap);\n\tva_end(ap);\n}\n\n/*\n * Restore normal assertion macros, in order to make it possible to compile all\n * C files as a single concatenation.\n */\n#undef assert\n#undef not_reached\n#undef not_implemented\n#undef assert_not_implemented\n#include \"jemalloc/internal/assert.h\"\n"
  },
  {
    "path": "deps/jemalloc/src/mutex.c",
    "content": "#define JEMALLOC_MUTEX_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/malloc_io.h\"\n#include \"jemalloc/internal/spin.h\"\n\n#ifndef _CRT_SPINCOUNT\n#define _CRT_SPINCOUNT 4000\n#endif\n\n/******************************************************************************/\n/* Data. */\n\n#ifdef JEMALLOC_LAZY_LOCK\nbool isthreaded = false;\n#endif\n#ifdef JEMALLOC_MUTEX_INIT_CB\nstatic bool\t\tpostpone_init = true;\nstatic malloc_mutex_t\t*postponed_mutexes = NULL;\n#endif\n\n/******************************************************************************/\n/*\n * We intercept pthread_create() calls in order to toggle isthreaded if the\n * process goes multi-threaded.\n */\n\n#if defined(JEMALLOC_LAZY_LOCK) && !defined(_WIN32)\nJEMALLOC_EXPORT int\npthread_create(pthread_t *__restrict thread,\n    const pthread_attr_t *__restrict attr, void *(*start_routine)(void *),\n    void *__restrict arg) {\n\treturn pthread_create_wrapper(thread, attr, start_routine, arg);\n}\n#endif\n\n/******************************************************************************/\n\n#ifdef JEMALLOC_MUTEX_INIT_CB\nJEMALLOC_EXPORT int\t_pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex,\n    void *(calloc_cb)(size_t, size_t));\n#endif\n\nvoid\nmalloc_mutex_lock_slow(malloc_mutex_t *mutex) {\n\tmutex_prof_data_t *data = &mutex->prof_data;\n\tnstime_t before = NSTIME_ZERO_INITIALIZER;\n\n\tif (ncpus == 1) {\n\t\tgoto label_spin_done;\n\t}\n\n\tint cnt = 0, max_cnt = MALLOC_MUTEX_MAX_SPIN;\n\tdo {\n\t\tspin_cpu_spinwait();\n\t\tif (!atomic_load_b(&mutex->locked, ATOMIC_RELAXED)\n                    && !malloc_mutex_trylock_final(mutex)) {\n\t\t\tdata->n_spin_acquired++;\n\t\t\treturn;\n\t\t}\n\t} while (cnt++ < max_cnt);\n\n\tif (!config_stats) {\n\t\t/* Only spin is useful when stats is off. */\n\t\tmalloc_mutex_lock_final(mutex);\n\t\treturn;\n\t}\nlabel_spin_done:\n\tnstime_update(&before);\n\t/* Copy before to after to avoid clock skews. */\n\tnstime_t after;\n\tnstime_copy(&after, &before);\n\tuint32_t n_thds = atomic_fetch_add_u32(&data->n_waiting_thds, 1,\n\t    ATOMIC_RELAXED) + 1;\n\t/* One last try as above two calls may take quite some cycles. */\n\tif (!malloc_mutex_trylock_final(mutex)) {\n\t\tatomic_fetch_sub_u32(&data->n_waiting_thds, 1, ATOMIC_RELAXED);\n\t\tdata->n_spin_acquired++;\n\t\treturn;\n\t}\n\n\t/* True slow path. */\n\tmalloc_mutex_lock_final(mutex);\n\t/* Update more slow-path only counters. */\n\tatomic_fetch_sub_u32(&data->n_waiting_thds, 1, ATOMIC_RELAXED);\n\tnstime_update(&after);\n\n\tnstime_t delta;\n\tnstime_copy(&delta, &after);\n\tnstime_subtract(&delta, &before);\n\n\tdata->n_wait_times++;\n\tnstime_add(&data->tot_wait_time, &delta);\n\tif (nstime_compare(&data->max_wait_time, &delta) < 0) {\n\t\tnstime_copy(&data->max_wait_time, &delta);\n\t}\n\tif (n_thds > data->max_n_thds) {\n\t\tdata->max_n_thds = n_thds;\n\t}\n}\n\nstatic void\nmutex_prof_data_init(mutex_prof_data_t *data) {\n\tmemset(data, 0, sizeof(mutex_prof_data_t));\n\tnstime_init(&data->max_wait_time, 0);\n\tnstime_init(&data->tot_wait_time, 0);\n\tdata->prev_owner = NULL;\n}\n\nvoid\nmalloc_mutex_prof_data_reset(tsdn_t *tsdn, malloc_mutex_t *mutex) {\n\tmalloc_mutex_assert_owner(tsdn, mutex);\n\tmutex_prof_data_init(&mutex->prof_data);\n}\n\nstatic int\nmutex_addr_comp(const witness_t *witness1, void *mutex1,\n    const witness_t *witness2, void *mutex2) {\n\tassert(mutex1 != NULL);\n\tassert(mutex2 != NULL);\n\tuintptr_t mu1int = (uintptr_t)mutex1;\n\tuintptr_t mu2int = (uintptr_t)mutex2;\n\tif (mu1int < mu2int) {\n\t\treturn -1;\n\t} else if (mu1int == mu2int) {\n\t\treturn 0;\n\t} else {\n\t\treturn 1;\n\t}\n}\n\nbool\nmalloc_mutex_init(malloc_mutex_t *mutex, const char *name,\n    witness_rank_t rank, malloc_mutex_lock_order_t lock_order) {\n\tmutex_prof_data_init(&mutex->prof_data);\n#ifdef _WIN32\n#  if _WIN32_WINNT >= 0x0600\n\tInitializeSRWLock(&mutex->lock);\n#  else\n\tif (!InitializeCriticalSectionAndSpinCount(&mutex->lock,\n\t    _CRT_SPINCOUNT)) {\n\t\treturn true;\n\t}\n#  endif\n#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))\n       mutex->lock = OS_UNFAIR_LOCK_INIT;\n#elif (defined(JEMALLOC_MUTEX_INIT_CB))\n\tif (postpone_init) {\n\t\tmutex->postponed_next = postponed_mutexes;\n\t\tpostponed_mutexes = mutex;\n\t} else {\n\t\tif (_pthread_mutex_init_calloc_cb(&mutex->lock,\n\t\t    bootstrap_calloc) != 0) {\n\t\t\treturn true;\n\t\t}\n\t}\n#else\n\tpthread_mutexattr_t attr;\n\n\tif (pthread_mutexattr_init(&attr) != 0) {\n\t\treturn true;\n\t}\n\tpthread_mutexattr_settype(&attr, MALLOC_MUTEX_TYPE);\n\tif (pthread_mutex_init(&mutex->lock, &attr) != 0) {\n\t\tpthread_mutexattr_destroy(&attr);\n\t\treturn true;\n\t}\n\tpthread_mutexattr_destroy(&attr);\n#endif\n\tif (config_debug) {\n\t\tmutex->lock_order = lock_order;\n\t\tif (lock_order == malloc_mutex_address_ordered) {\n\t\t\twitness_init(&mutex->witness, name, rank,\n\t\t\t    mutex_addr_comp, mutex);\n\t\t} else {\n\t\t\twitness_init(&mutex->witness, name, rank, NULL, NULL);\n\t\t}\n\t}\n\treturn false;\n}\n\nvoid\nmalloc_mutex_prefork(tsdn_t *tsdn, malloc_mutex_t *mutex) {\n\tmalloc_mutex_lock(tsdn, mutex);\n}\n\nvoid\nmalloc_mutex_postfork_parent(tsdn_t *tsdn, malloc_mutex_t *mutex) {\n\tmalloc_mutex_unlock(tsdn, mutex);\n}\n\nvoid\nmalloc_mutex_postfork_child(tsdn_t *tsdn, malloc_mutex_t *mutex) {\n#ifdef JEMALLOC_MUTEX_INIT_CB\n\tmalloc_mutex_unlock(tsdn, mutex);\n#else\n\tif (malloc_mutex_init(mutex, mutex->witness.name,\n\t    mutex->witness.rank, mutex->lock_order)) {\n\t\tmalloc_printf(\"<jemalloc>: Error re-initializing mutex in \"\n\t\t    \"child\\n\");\n\t\tif (opt_abort) {\n\t\t\tabort();\n\t\t}\n\t}\n#endif\n}\n\nbool\nmalloc_mutex_boot(void) {\n#ifdef JEMALLOC_MUTEX_INIT_CB\n\tpostpone_init = false;\n\twhile (postponed_mutexes != NULL) {\n\t\tif (_pthread_mutex_init_calloc_cb(&postponed_mutexes->lock,\n\t\t    bootstrap_calloc) != 0) {\n\t\t\treturn true;\n\t\t}\n\t\tpostponed_mutexes = postponed_mutexes->postponed_next;\n\t}\n#endif\n\treturn false;\n}\n"
  },
  {
    "path": "deps/jemalloc/src/mutex_pool.c",
    "content": "#define JEMALLOC_MUTEX_POOL_C_\n\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/mutex_pool.h\"\n\nbool\nmutex_pool_init(mutex_pool_t *pool, const char *name, witness_rank_t rank) {\n\tfor (int i = 0; i < MUTEX_POOL_SIZE; ++i) {\n\t\tif (malloc_mutex_init(&pool->mutexes[i], name, rank,\n\t\t    malloc_mutex_address_ordered)) {\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n"
  },
  {
    "path": "deps/jemalloc/src/nstime.c",
    "content": "#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/nstime.h\"\n\n#include \"jemalloc/internal/assert.h\"\n\n#define BILLION\tUINT64_C(1000000000)\n#define MILLION\tUINT64_C(1000000)\n\nvoid\nnstime_init(nstime_t *time, uint64_t ns) {\n\ttime->ns = ns;\n}\n\nvoid\nnstime_init2(nstime_t *time, uint64_t sec, uint64_t nsec) {\n\ttime->ns = sec * BILLION + nsec;\n}\n\nuint64_t\nnstime_ns(const nstime_t *time) {\n\treturn time->ns;\n}\n\nuint64_t\nnstime_msec(const nstime_t *time) {\n\treturn time->ns / MILLION;\n}\n\nuint64_t\nnstime_sec(const nstime_t *time) {\n\treturn time->ns / BILLION;\n}\n\nuint64_t\nnstime_nsec(const nstime_t *time) {\n\treturn time->ns % BILLION;\n}\n\nvoid\nnstime_copy(nstime_t *time, const nstime_t *source) {\n\t*time = *source;\n}\n\nint\nnstime_compare(const nstime_t *a, const nstime_t *b) {\n\treturn (a->ns > b->ns) - (a->ns < b->ns);\n}\n\nvoid\nnstime_add(nstime_t *time, const nstime_t *addend) {\n\tassert(UINT64_MAX - time->ns >= addend->ns);\n\n\ttime->ns += addend->ns;\n}\n\nvoid\nnstime_iadd(nstime_t *time, uint64_t addend) {\n\tassert(UINT64_MAX - time->ns >= addend);\n\n\ttime->ns += addend;\n}\n\nvoid\nnstime_subtract(nstime_t *time, const nstime_t *subtrahend) {\n\tassert(nstime_compare(time, subtrahend) >= 0);\n\n\ttime->ns -= subtrahend->ns;\n}\n\nvoid\nnstime_isubtract(nstime_t *time, uint64_t subtrahend) {\n\tassert(time->ns >= subtrahend);\n\n\ttime->ns -= subtrahend;\n}\n\nvoid\nnstime_imultiply(nstime_t *time, uint64_t multiplier) {\n\tassert((((time->ns | multiplier) & (UINT64_MAX << (sizeof(uint64_t) <<\n\t    2))) == 0) || ((time->ns * multiplier) / multiplier == time->ns));\n\n\ttime->ns *= multiplier;\n}\n\nvoid\nnstime_idivide(nstime_t *time, uint64_t divisor) {\n\tassert(divisor != 0);\n\n\ttime->ns /= divisor;\n}\n\nuint64_t\nnstime_divide(const nstime_t *time, const nstime_t *divisor) {\n\tassert(divisor->ns != 0);\n\n\treturn time->ns / divisor->ns;\n}\n\n#ifdef _WIN32\n#  define NSTIME_MONOTONIC true\nstatic void\nnstime_get(nstime_t *time) {\n\tFILETIME ft;\n\tuint64_t ticks_100ns;\n\n\tGetSystemTimeAsFileTime(&ft);\n\tticks_100ns = (((uint64_t)ft.dwHighDateTime) << 32) | ft.dwLowDateTime;\n\n\tnstime_init(time, ticks_100ns * 100);\n}\n#elif defined(JEMALLOC_HAVE_CLOCK_MONOTONIC_COARSE)\n#  define NSTIME_MONOTONIC true\nstatic void\nnstime_get(nstime_t *time) {\n\tstruct timespec ts;\n\n\tclock_gettime(CLOCK_MONOTONIC_COARSE, &ts);\n\tnstime_init2(time, ts.tv_sec, ts.tv_nsec);\n}\n#elif defined(JEMALLOC_HAVE_CLOCK_MONOTONIC)\n#  define NSTIME_MONOTONIC true\nstatic void\nnstime_get(nstime_t *time) {\n\tstruct timespec ts;\n\n\tclock_gettime(CLOCK_MONOTONIC, &ts);\n\tnstime_init2(time, ts.tv_sec, ts.tv_nsec);\n}\n#elif defined(JEMALLOC_HAVE_MACH_ABSOLUTE_TIME)\n#  define NSTIME_MONOTONIC true\nstatic void\nnstime_get(nstime_t *time) {\n\tnstime_init(time, mach_absolute_time());\n}\n#else\n#  define NSTIME_MONOTONIC false\nstatic void\nnstime_get(nstime_t *time) {\n\tstruct timeval tv;\n\n\tgettimeofday(&tv, NULL);\n\tnstime_init2(time, tv.tv_sec, tv.tv_usec * 1000);\n}\n#endif\n\nstatic bool\nnstime_monotonic_impl(void) {\n\treturn NSTIME_MONOTONIC;\n#undef NSTIME_MONOTONIC\n}\nnstime_monotonic_t *JET_MUTABLE nstime_monotonic = nstime_monotonic_impl;\n\nstatic bool\nnstime_update_impl(nstime_t *time) {\n\tnstime_t old_time;\n\n\tnstime_copy(&old_time, time);\n\tnstime_get(time);\n\n\t/* Handle non-monotonic clocks. */\n\tif (unlikely(nstime_compare(&old_time, time) > 0)) {\n\t\tnstime_copy(time, &old_time);\n\t\treturn true;\n\t}\n\n\treturn false;\n}\nnstime_update_t *JET_MUTABLE nstime_update = nstime_update_impl;\n"
  },
  {
    "path": "deps/jemalloc/src/pages.c",
    "content": "#define JEMALLOC_PAGES_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n\n#include \"jemalloc/internal/pages.h\"\n\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/malloc_io.h\"\n\n#ifdef JEMALLOC_SYSCTL_VM_OVERCOMMIT\n#include <sys/sysctl.h>\n#ifdef __FreeBSD__\n#include <vm/vm_param.h>\n#endif\n#endif\n\n/******************************************************************************/\n/* Data. */\n\n/* Actual operating system page size, detected during bootstrap, <= PAGE. */\nstatic size_t\tos_page;\n\n#ifndef _WIN32\n#  define PAGES_PROT_COMMIT (PROT_READ | PROT_WRITE)\n#  define PAGES_PROT_DECOMMIT (PROT_NONE)\nstatic int\tmmap_flags;\n#endif\nstatic bool\tos_overcommits;\n\nconst char *thp_mode_names[] = {\n\t\"default\",\n\t\"always\",\n\t\"never\",\n\t\"not supported\"\n};\nthp_mode_t opt_thp = THP_MODE_DEFAULT;\nthp_mode_t init_system_thp_mode;\n\n/* Runtime support for lazy purge. Irrelevant when !pages_can_purge_lazy. */\nstatic bool pages_can_purge_lazy_runtime = true;\n\n/******************************************************************************/\n/*\n * Function prototypes for static functions that are referenced prior to\n * definition.\n */\n\nstatic void os_pages_unmap(void *addr, size_t size);\n\n/******************************************************************************/\n\nstatic void *\nos_pages_map(void *addr, size_t size, size_t alignment, bool *commit) {\n\tassert(ALIGNMENT_ADDR2BASE(addr, os_page) == addr);\n\tassert(ALIGNMENT_CEILING(size, os_page) == size);\n\tassert(size != 0);\n\n\tif (os_overcommits) {\n\t\t*commit = true;\n\t}\n\n\tvoid *ret;\n#ifdef _WIN32\n\t/*\n\t * If VirtualAlloc can't allocate at the given address when one is\n\t * given, it fails and returns NULL.\n\t */\n\tret = VirtualAlloc(addr, size, MEM_RESERVE | (*commit ? MEM_COMMIT : 0),\n\t    PAGE_READWRITE);\n#else\n\t/*\n\t * We don't use MAP_FIXED here, because it can cause the *replacement*\n\t * of existing mappings, and we only want to create new mappings.\n\t */\n\t{\n\t\tint prot = *commit ? PAGES_PROT_COMMIT : PAGES_PROT_DECOMMIT;\n\n\t\tret = mmap(addr, size, prot, mmap_flags, -1, 0);\n\t}\n\tassert(ret != NULL);\n\n\tif (ret == MAP_FAILED) {\n\t\tret = NULL;\n\t} else if (addr != NULL && ret != addr) {\n\t\t/*\n\t\t * We succeeded in mapping memory, but not in the right place.\n\t\t */\n\t\tos_pages_unmap(ret, size);\n\t\tret = NULL;\n\t}\n#endif\n\tassert(ret == NULL || (addr == NULL && ret != addr) || (addr != NULL &&\n\t    ret == addr));\n\treturn ret;\n}\n\nstatic void *\nos_pages_trim(void *addr, size_t alloc_size, size_t leadsize, size_t size,\n    bool *commit) {\n\tvoid *ret = (void *)((uintptr_t)addr + leadsize);\n\n\tassert(alloc_size >= leadsize + size);\n#ifdef _WIN32\n\tos_pages_unmap(addr, alloc_size);\n\tvoid *new_addr = os_pages_map(ret, size, PAGE, commit);\n\tif (new_addr == ret) {\n\t\treturn ret;\n\t}\n\tif (new_addr != NULL) {\n\t\tos_pages_unmap(new_addr, size);\n\t}\n\treturn NULL;\n#else\n\tsize_t trailsize = alloc_size - leadsize - size;\n\n\tif (leadsize != 0) {\n\t\tos_pages_unmap(addr, leadsize);\n\t}\n\tif (trailsize != 0) {\n\t\tos_pages_unmap((void *)((uintptr_t)ret + size), trailsize);\n\t}\n\treturn ret;\n#endif\n}\n\nstatic void\nos_pages_unmap(void *addr, size_t size) {\n\tassert(ALIGNMENT_ADDR2BASE(addr, os_page) == addr);\n\tassert(ALIGNMENT_CEILING(size, os_page) == size);\n\n#ifdef _WIN32\n\tif (VirtualFree(addr, 0, MEM_RELEASE) == 0)\n#else\n\tif (munmap(addr, size) == -1)\n#endif\n\t{\n\t\tchar buf[BUFERROR_BUF];\n\n\t\tbuferror(get_errno(), buf, sizeof(buf));\n\t\tmalloc_printf(\"<jemalloc>: Error in \"\n#ifdef _WIN32\n\t\t    \"VirtualFree\"\n#else\n\t\t    \"munmap\"\n#endif\n\t\t    \"(): %s\\n\", buf);\n\t\tif (opt_abort) {\n\t\t\tabort();\n\t\t}\n\t}\n}\n\nstatic void *\npages_map_slow(size_t size, size_t alignment, bool *commit) {\n\tsize_t alloc_size = size + alignment - os_page;\n\t/* Beware size_t wrap-around. */\n\tif (alloc_size < size) {\n\t\treturn NULL;\n\t}\n\n\tvoid *ret;\n\tdo {\n\t\tvoid *pages = os_pages_map(NULL, alloc_size, alignment, commit);\n\t\tif (pages == NULL) {\n\t\t\treturn NULL;\n\t\t}\n\t\tsize_t leadsize = ALIGNMENT_CEILING((uintptr_t)pages, alignment)\n\t\t    - (uintptr_t)pages;\n\t\tret = os_pages_trim(pages, alloc_size, leadsize, size, commit);\n\t} while (ret == NULL);\n\n\tassert(ret != NULL);\n\tassert(PAGE_ADDR2BASE(ret) == ret);\n\treturn ret;\n}\n\nvoid *\npages_map(void *addr, size_t size, size_t alignment, bool *commit) {\n\tassert(alignment >= PAGE);\n\tassert(ALIGNMENT_ADDR2BASE(addr, alignment) == addr);\n\n#if defined(__FreeBSD__) && defined(MAP_EXCL)\n\t/*\n\t * FreeBSD has mechanisms both to mmap at specific address without\n\t * touching existing mappings, and to mmap with specific alignment.\n\t */\n\t{\n\t\tif (os_overcommits) {\n\t\t\t*commit = true;\n\t\t}\n\n\t\tint prot = *commit ? PAGES_PROT_COMMIT : PAGES_PROT_DECOMMIT;\n\t\tint flags = mmap_flags;\n\n\t\tif (addr != NULL) {\n\t\t\tflags |= MAP_FIXED | MAP_EXCL;\n\t\t} else {\n\t\t\tunsigned alignment_bits = ffs_zu(alignment);\n\t\t\tassert(alignment_bits > 1);\n\t\t\tflags |= MAP_ALIGNED(alignment_bits - 1);\n\t\t}\n\n\t\tvoid *ret = mmap(addr, size, prot, flags, -1, 0);\n\t\tif (ret == MAP_FAILED) {\n\t\t\tret = NULL;\n\t\t}\n\n\t\treturn ret;\n\t}\n#endif\n\t/*\n\t * Ideally, there would be a way to specify alignment to mmap() (like\n\t * NetBSD has), but in the absence of such a feature, we have to work\n\t * hard to efficiently create aligned mappings.  The reliable, but\n\t * slow method is to create a mapping that is over-sized, then trim the\n\t * excess.  However, that always results in one or two calls to\n\t * os_pages_unmap(), and it can leave holes in the process's virtual\n\t * memory map if memory grows downward.\n\t *\n\t * Optimistically try mapping precisely the right amount before falling\n\t * back to the slow method, with the expectation that the optimistic\n\t * approach works most of the time.\n\t */\n\n\tvoid *ret = os_pages_map(addr, size, os_page, commit);\n\tif (ret == NULL || ret == addr) {\n\t\treturn ret;\n\t}\n\tassert(addr == NULL);\n\tif (ALIGNMENT_ADDR2OFFSET(ret, alignment) != 0) {\n\t\tos_pages_unmap(ret, size);\n\t\treturn pages_map_slow(size, alignment, commit);\n\t}\n\n\tassert(PAGE_ADDR2BASE(ret) == ret);\n\treturn ret;\n}\n\nvoid\npages_unmap(void *addr, size_t size) {\n\tassert(PAGE_ADDR2BASE(addr) == addr);\n\tassert(PAGE_CEILING(size) == size);\n\n\tos_pages_unmap(addr, size);\n}\n\nstatic bool\npages_commit_impl(void *addr, size_t size, bool commit) {\n\tassert(PAGE_ADDR2BASE(addr) == addr);\n\tassert(PAGE_CEILING(size) == size);\n\n\tif (os_overcommits) {\n\t\treturn true;\n\t}\n\n#ifdef _WIN32\n\treturn (commit ? (addr != VirtualAlloc(addr, size, MEM_COMMIT,\n\t    PAGE_READWRITE)) : (!VirtualFree(addr, size, MEM_DECOMMIT)));\n#else\n\t{\n\t\tint prot = commit ? PAGES_PROT_COMMIT : PAGES_PROT_DECOMMIT;\n\t\tvoid *result = mmap(addr, size, prot, mmap_flags | MAP_FIXED,\n\t\t    -1, 0);\n\t\tif (result == MAP_FAILED) {\n\t\t\treturn true;\n\t\t}\n\t\tif (result != addr) {\n\t\t\t/*\n\t\t\t * We succeeded in mapping memory, but not in the right\n\t\t\t * place.\n\t\t\t */\n\t\t\tos_pages_unmap(result, size);\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n#endif\n}\n\nbool\npages_commit(void *addr, size_t size) {\n\treturn pages_commit_impl(addr, size, true);\n}\n\nbool\npages_decommit(void *addr, size_t size) {\n\treturn pages_commit_impl(addr, size, false);\n}\n\nbool\npages_purge_lazy(void *addr, size_t size) {\n\tassert(ALIGNMENT_ADDR2BASE(addr, os_page) == addr);\n\tassert(PAGE_CEILING(size) == size);\n\n\tif (!pages_can_purge_lazy) {\n\t\treturn true;\n\t}\n\tif (!pages_can_purge_lazy_runtime) {\n\t\t/*\n\t\t * Built with lazy purge enabled, but detected it was not\n\t\t * supported on the current system.\n\t\t */\n\t\treturn true;\n\t}\n\n#ifdef _WIN32\n\tVirtualAlloc(addr, size, MEM_RESET, PAGE_READWRITE);\n\treturn false;\n#elif defined(JEMALLOC_PURGE_MADVISE_FREE)\n\treturn (madvise(addr, size,\n#  ifdef MADV_FREE\n\t    MADV_FREE\n#  else\n\t    JEMALLOC_MADV_FREE\n#  endif\n\t    ) != 0);\n#elif defined(JEMALLOC_PURGE_MADVISE_DONTNEED) && \\\n    !defined(JEMALLOC_PURGE_MADVISE_DONTNEED_ZEROS)\n\treturn (madvise(addr, size, MADV_DONTNEED) != 0);\n#else\n\tnot_reached();\n#endif\n}\n\nbool\npages_purge_forced(void *addr, size_t size) {\n\tassert(PAGE_ADDR2BASE(addr) == addr);\n\tassert(PAGE_CEILING(size) == size);\n\n\tif (!pages_can_purge_forced) {\n\t\treturn true;\n\t}\n\n#if defined(JEMALLOC_PURGE_MADVISE_DONTNEED) && \\\n    defined(JEMALLOC_PURGE_MADVISE_DONTNEED_ZEROS)\n\treturn (madvise(addr, size, MADV_DONTNEED) != 0);\n#elif defined(JEMALLOC_MAPS_COALESCE)\n\t/* Try to overlay a new demand-zeroed mapping. */\n\treturn pages_commit(addr, size);\n#else\n\tnot_reached();\n#endif\n}\n\nstatic bool\npages_huge_impl(void *addr, size_t size, bool aligned) {\n\tif (aligned) {\n\t\tassert(HUGEPAGE_ADDR2BASE(addr) == addr);\n\t\tassert(HUGEPAGE_CEILING(size) == size);\n\t}\n#ifdef JEMALLOC_HAVE_MADVISE_HUGE\n\treturn (madvise(addr, size, MADV_HUGEPAGE) != 0);\n#else\n\treturn true;\n#endif\n}\n\nbool\npages_huge(void *addr, size_t size) {\n\treturn pages_huge_impl(addr, size, true);\n}\n\nstatic bool\npages_huge_unaligned(void *addr, size_t size) {\n\treturn pages_huge_impl(addr, size, false);\n}\n\nstatic bool\npages_nohuge_impl(void *addr, size_t size, bool aligned) {\n\tif (aligned) {\n\t\tassert(HUGEPAGE_ADDR2BASE(addr) == addr);\n\t\tassert(HUGEPAGE_CEILING(size) == size);\n\t}\n\n#ifdef JEMALLOC_HAVE_MADVISE_HUGE\n\treturn (madvise(addr, size, MADV_NOHUGEPAGE) != 0);\n#else\n\treturn false;\n#endif\n}\n\nbool\npages_nohuge(void *addr, size_t size) {\n\treturn pages_nohuge_impl(addr, size, true);\n}\n\nstatic bool\npages_nohuge_unaligned(void *addr, size_t size) {\n\treturn pages_nohuge_impl(addr, size, false);\n}\n\nbool\npages_dontdump(void *addr, size_t size) {\n\tassert(PAGE_ADDR2BASE(addr) == addr);\n\tassert(PAGE_CEILING(size) == size);\n#ifdef JEMALLOC_MADVISE_DONTDUMP\n\treturn madvise(addr, size, MADV_DONTDUMP) != 0;\n#else\n\treturn false;\n#endif\n}\n\nbool\npages_dodump(void *addr, size_t size) {\n\tassert(PAGE_ADDR2BASE(addr) == addr);\n\tassert(PAGE_CEILING(size) == size);\n#ifdef JEMALLOC_MADVISE_DONTDUMP\n\treturn madvise(addr, size, MADV_DODUMP) != 0;\n#else\n\treturn false;\n#endif\n}\n\n\nstatic size_t\nos_page_detect(void) {\n#ifdef _WIN32\n\tSYSTEM_INFO si;\n\tGetSystemInfo(&si);\n\treturn si.dwPageSize;\n#elif defined(__FreeBSD__)\n\t/*\n\t * This returns the value obtained from\n\t * the auxv vector, avoiding a syscall.\n\t */\n\treturn getpagesize();\n#else\n\tlong result = sysconf(_SC_PAGESIZE);\n\tif (result == -1) {\n\t\treturn LG_PAGE;\n\t}\n\treturn (size_t)result;\n#endif\n}\n\n#ifdef JEMALLOC_SYSCTL_VM_OVERCOMMIT\nstatic bool\nos_overcommits_sysctl(void) {\n\tint vm_overcommit;\n\tsize_t sz;\n\n\tsz = sizeof(vm_overcommit);\n#if defined(__FreeBSD__) && defined(VM_OVERCOMMIT)\n\tint mib[2];\n\n\tmib[0] = CTL_VM;\n\tmib[1] = VM_OVERCOMMIT;\n\tif (sysctl(mib, 2, &vm_overcommit, &sz, NULL, 0) != 0) {\n\t\treturn false; /* Error. */\n\t}\n#else\n\tif (sysctlbyname(\"vm.overcommit\", &vm_overcommit, &sz, NULL, 0) != 0) {\n\t\treturn false; /* Error. */\n\t}\n#endif\n\n\treturn ((vm_overcommit & 0x3) == 0);\n}\n#endif\n\n#ifdef JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY\n/*\n * Use syscall(2) rather than {open,read,close}(2) when possible to avoid\n * reentry during bootstrapping if another library has interposed system call\n * wrappers.\n */\nstatic bool\nos_overcommits_proc(void) {\n\tint fd;\n\tchar buf[1];\n\n#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_open)\n\t#if defined(O_CLOEXEC)\n\t\tfd = (int)syscall(SYS_open, \"/proc/sys/vm/overcommit_memory\", O_RDONLY |\n\t\t\tO_CLOEXEC);\n\t#else\n\t\tfd = (int)syscall(SYS_open, \"/proc/sys/vm/overcommit_memory\", O_RDONLY);\n\t\tif (fd != -1) {\n\t\t\tfcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);\n\t\t}\n\t#endif\n#elif defined(JEMALLOC_USE_SYSCALL) && defined(SYS_openat)\n\t#if defined(O_CLOEXEC)\n\t\tfd = (int)syscall(SYS_openat,\n\t\t\tAT_FDCWD, \"/proc/sys/vm/overcommit_memory\", O_RDONLY | O_CLOEXEC);\n\t#else\n\t\tfd = (int)syscall(SYS_openat,\n\t\t\tAT_FDCWD, \"/proc/sys/vm/overcommit_memory\", O_RDONLY);\n\t\tif (fd != -1) {\n\t\t\tfcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);\n\t\t}\n\t#endif\n#else\n\t#if defined(O_CLOEXEC)\n\t\tfd = open(\"/proc/sys/vm/overcommit_memory\", O_RDONLY | O_CLOEXEC);\n\t#else\n\t\tfd = open(\"/proc/sys/vm/overcommit_memory\", O_RDONLY);\n\t\tif (fd != -1) {\n\t\t\tfcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);\n\t\t}\n\t#endif\n#endif\n\n\tif (fd == -1) {\n\t\treturn false; /* Error. */\n\t}\n\n\tssize_t nread = malloc_read_fd(fd, &buf, sizeof(buf));\n#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_close)\n\tsyscall(SYS_close, fd);\n#else\n\tclose(fd);\n#endif\n\n\tif (nread < 1) {\n\t\treturn false; /* Error. */\n\t}\n\t/*\n\t * /proc/sys/vm/overcommit_memory meanings:\n\t * 0: Heuristic overcommit.\n\t * 1: Always overcommit.\n\t * 2: Never overcommit.\n\t */\n\treturn (buf[0] == '0' || buf[0] == '1');\n}\n#endif\n\nvoid\npages_set_thp_state (void *ptr, size_t size) {\n\tif (opt_thp == thp_mode_default || opt_thp == init_system_thp_mode) {\n\t\treturn;\n\t}\n\tassert(opt_thp != thp_mode_not_supported &&\n\t    init_system_thp_mode != thp_mode_not_supported);\n\n\tif (opt_thp == thp_mode_always\n\t    && init_system_thp_mode != thp_mode_never) {\n\t\tassert(init_system_thp_mode == thp_mode_default);\n\t\tpages_huge_unaligned(ptr, size);\n\t} else if (opt_thp == thp_mode_never) {\n\t\tassert(init_system_thp_mode == thp_mode_default ||\n\t\t    init_system_thp_mode == thp_mode_always);\n\t\tpages_nohuge_unaligned(ptr, size);\n\t}\n}\n\nstatic void\ninit_thp_state(void) {\n\tif (!have_madvise_huge) {\n\t\tif (metadata_thp_enabled() && opt_abort) {\n\t\t\tmalloc_write(\"<jemalloc>: no MADV_HUGEPAGE support\\n\");\n\t\t\tabort();\n\t\t}\n\t\tgoto label_error;\n\t}\n\n\tstatic const char sys_state_madvise[] = \"always [madvise] never\\n\";\n\tstatic const char sys_state_always[] = \"[always] madvise never\\n\";\n\tstatic const char sys_state_never[] = \"always madvise [never]\\n\";\n\tchar buf[sizeof(sys_state_madvise)];\n\n#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_open)\n\tint fd = (int)syscall(SYS_open,\n\t    \"/sys/kernel/mm/transparent_hugepage/enabled\", O_RDONLY);\n#else\n\tint fd = open(\"/sys/kernel/mm/transparent_hugepage/enabled\", O_RDONLY);\n#endif\n\tif (fd == -1) {\n\t\tgoto label_error;\n\t}\n\n\tssize_t nread = malloc_read_fd(fd, &buf, sizeof(buf));\n#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_close)\n\tsyscall(SYS_close, fd);\n#else\n\tclose(fd);\n#endif\n\n        if (nread < 0) {\n\t\tgoto label_error; \n        }\n\n\tif (strncmp(buf, sys_state_madvise, (size_t)nread) == 0) {\n\t\tinit_system_thp_mode = thp_mode_default;\n\t} else if (strncmp(buf, sys_state_always, (size_t)nread) == 0) {\n\t\tinit_system_thp_mode = thp_mode_always;\n\t} else if (strncmp(buf, sys_state_never, (size_t)nread) == 0) {\n\t\tinit_system_thp_mode = thp_mode_never;\n\t} else {\n\t\tgoto label_error;\n\t}\n\treturn;\nlabel_error:\n\topt_thp = init_system_thp_mode = thp_mode_not_supported;\n}\n\nbool\npages_boot(void) {\n\tos_page = os_page_detect();\n\tif (os_page > PAGE) {\n\t\tmalloc_write(\"<jemalloc>: Unsupported system page size\\n\");\n\t\tif (opt_abort) {\n\t\t\tabort();\n\t\t}\n\t\treturn true;\n\t}\n\n#ifndef _WIN32\n\tmmap_flags = MAP_PRIVATE | MAP_ANON;\n#endif\n\n#ifdef JEMALLOC_SYSCTL_VM_OVERCOMMIT\n\tos_overcommits = os_overcommits_sysctl();\n#elif defined(JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY)\n\tos_overcommits = os_overcommits_proc();\n#  ifdef MAP_NORESERVE\n\tif (os_overcommits) {\n\t\tmmap_flags |= MAP_NORESERVE;\n\t}\n#  endif\n#else\n\tos_overcommits = false;\n#endif\n\n\tinit_thp_state();\n\n#ifdef __FreeBSD__\n\t/*\n\t * FreeBSD doesn't need the check; madvise(2) is known to work.\n\t */\n#else\n\t/* Detect lazy purge runtime support. */\n\tif (pages_can_purge_lazy) {\n\t\tbool committed = false;\n\t\tvoid *madv_free_page = os_pages_map(NULL, PAGE, PAGE, &committed);\n\t\tif (madv_free_page == NULL) {\n\t\t\treturn true;\n\t\t}\n\t\tassert(pages_can_purge_lazy_runtime);\n\t\tif (pages_purge_lazy(madv_free_page, PAGE)) {\n\t\t\tpages_can_purge_lazy_runtime = false;\n\t\t}\n\t\tos_pages_unmap(madv_free_page, PAGE);\n\t}\n#endif\n\n\treturn false;\n}\n"
  },
  {
    "path": "deps/jemalloc/src/prng.c",
    "content": "#define JEMALLOC_PRNG_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n"
  },
  {
    "path": "deps/jemalloc/src/prof.c",
    "content": "#define JEMALLOC_PROF_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/ckh.h\"\n#include \"jemalloc/internal/hash.h\"\n#include \"jemalloc/internal/malloc_io.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/emitter.h\"\n\n/******************************************************************************/\n\n#ifdef JEMALLOC_PROF_LIBUNWIND\n#define UNW_LOCAL_ONLY\n#include <libunwind.h>\n#endif\n\n#ifdef JEMALLOC_PROF_LIBGCC\n/*\n * We have a circular dependency -- jemalloc_internal.h tells us if we should\n * use libgcc's unwinding functionality, but after we've included that, we've\n * already hooked _Unwind_Backtrace.  We'll temporarily disable hooking.\n */\n#undef _Unwind_Backtrace\n#include <unwind.h>\n#define _Unwind_Backtrace JEMALLOC_HOOK(_Unwind_Backtrace, test_hooks_libc_hook)\n#endif\n\n/******************************************************************************/\n/* Data. */\n\nbool\t\topt_prof = false;\nbool\t\topt_prof_active = true;\nbool\t\topt_prof_thread_active_init = true;\nsize_t\t\topt_lg_prof_sample = LG_PROF_SAMPLE_DEFAULT;\nssize_t\t\topt_lg_prof_interval = LG_PROF_INTERVAL_DEFAULT;\nbool\t\topt_prof_gdump = false;\nbool\t\topt_prof_final = false;\nbool\t\topt_prof_leak = false;\nbool\t\topt_prof_accum = false;\nbool\t\topt_prof_log = false;\nchar\t\topt_prof_prefix[\n    /* Minimize memory bloat for non-prof builds. */\n#ifdef JEMALLOC_PROF\n    PATH_MAX +\n#endif\n    1];\n\n/*\n * Initialized as opt_prof_active, and accessed via\n * prof_active_[gs]et{_unlocked,}().\n */\nbool\t\t\tprof_active;\nstatic malloc_mutex_t\tprof_active_mtx;\n\n/*\n * Initialized as opt_prof_thread_active_init, and accessed via\n * prof_thread_active_init_[gs]et().\n */\nstatic bool\t\tprof_thread_active_init;\nstatic malloc_mutex_t\tprof_thread_active_init_mtx;\n\n/*\n * Initialized as opt_prof_gdump, and accessed via\n * prof_gdump_[gs]et{_unlocked,}().\n */\nbool\t\t\tprof_gdump_val;\nstatic malloc_mutex_t\tprof_gdump_mtx;\n\nuint64_t\tprof_interval = 0;\n\nsize_t\t\tlg_prof_sample;\n\ntypedef enum prof_logging_state_e prof_logging_state_t;\nenum prof_logging_state_e {\n\tprof_logging_state_stopped,\n\tprof_logging_state_started,\n\tprof_logging_state_dumping\n};\n\n/*\n * - stopped: log_start never called, or previous log_stop has completed.\n * - started: log_start called, log_stop not called yet. Allocations are logged.\n * - dumping: log_stop called but not finished; samples are not logged anymore.\n */\nprof_logging_state_t prof_logging_state = prof_logging_state_stopped;\n\n#ifdef JEMALLOC_JET\nstatic bool prof_log_dummy = false;\n#endif\n\n/* Incremented for every log file that is output. */\nstatic uint64_t log_seq = 0;\nstatic char log_filename[\n    /* Minimize memory bloat for non-prof builds. */\n#ifdef JEMALLOC_PROF\n    PATH_MAX +\n#endif\n    1];\n\n/* Timestamp for most recent call to log_start(). */\nstatic nstime_t log_start_timestamp = NSTIME_ZERO_INITIALIZER;\n\n/* Increment these when adding to the log_bt and log_thr linked lists. */\nstatic size_t log_bt_index = 0;\nstatic size_t log_thr_index = 0;\n\n/* Linked list node definitions. These are only used in prof.c. */\ntypedef struct prof_bt_node_s prof_bt_node_t;\n\nstruct prof_bt_node_s {\n\tprof_bt_node_t *next;\n\tsize_t index;\n\tprof_bt_t bt;\n\t/* Variable size backtrace vector pointed to by bt. */\n\tvoid *vec[1];\n};\n\ntypedef struct prof_thr_node_s prof_thr_node_t;\n\nstruct prof_thr_node_s {\n\tprof_thr_node_t *next;\n\tsize_t index;\n\tuint64_t thr_uid;\n\t/* Variable size based on thr_name_sz. */\n\tchar name[1];\n};\n\ntypedef struct prof_alloc_node_s prof_alloc_node_t;\n\n/* This is output when logging sampled allocations. */\nstruct prof_alloc_node_s {\n\tprof_alloc_node_t *next;\n\t/* Indices into an array of thread data. */\n\tsize_t alloc_thr_ind;\n\tsize_t free_thr_ind;\n\n\t/* Indices into an array of backtraces. */\n\tsize_t alloc_bt_ind;\n\tsize_t free_bt_ind;\n\n\tuint64_t alloc_time_ns;\n\tuint64_t free_time_ns;\n\n\tsize_t usize;\n};\n\n/*\n * Created on the first call to prof_log_start and deleted on prof_log_stop.\n * These are the backtraces and threads that have already been logged by an\n * allocation.\n */\nstatic bool log_tables_initialized = false;\nstatic ckh_t log_bt_node_set;\nstatic ckh_t log_thr_node_set;\n\n/* Store linked lists for logged data. */\nstatic prof_bt_node_t *log_bt_first = NULL;\nstatic prof_bt_node_t *log_bt_last = NULL;\nstatic prof_thr_node_t *log_thr_first = NULL;\nstatic prof_thr_node_t *log_thr_last = NULL;\nstatic prof_alloc_node_t *log_alloc_first = NULL;\nstatic prof_alloc_node_t *log_alloc_last = NULL;\n\n/* Protects the prof_logging_state and any log_{...} variable. */\nstatic malloc_mutex_t log_mtx;\n\n/*\n * Table of mutexes that are shared among gctx's.  These are leaf locks, so\n * there is no problem with using them for more than one gctx at the same time.\n * The primary motivation for this sharing though is that gctx's are ephemeral,\n * and destroying mutexes causes complications for systems that allocate when\n * creating/destroying mutexes.\n */\nstatic malloc_mutex_t\t*gctx_locks;\nstatic atomic_u_t\tcum_gctxs; /* Atomic counter. */\n\n/*\n * Table of mutexes that are shared among tdata's.  No operations require\n * holding multiple tdata locks, so there is no problem with using them for more\n * than one tdata at the same time, even though a gctx lock may be acquired\n * while holding a tdata lock.\n */\nstatic malloc_mutex_t\t*tdata_locks;\n\n/*\n * Global hash of (prof_bt_t *)-->(prof_gctx_t *).  This is the master data\n * structure that knows about all backtraces currently captured.\n */\nstatic ckh_t\t\tbt2gctx;\n/* Non static to enable profiling. */\nmalloc_mutex_t\t\tbt2gctx_mtx;\n\n/*\n * Tree of all extant prof_tdata_t structures, regardless of state,\n * {attached,detached,expired}.\n */\nstatic prof_tdata_tree_t\ttdatas;\nstatic malloc_mutex_t\ttdatas_mtx;\n\nstatic uint64_t\t\tnext_thr_uid;\nstatic malloc_mutex_t\tnext_thr_uid_mtx;\n\nstatic malloc_mutex_t\tprof_dump_seq_mtx;\nstatic uint64_t\t\tprof_dump_seq;\nstatic uint64_t\t\tprof_dump_iseq;\nstatic uint64_t\t\tprof_dump_mseq;\nstatic uint64_t\t\tprof_dump_useq;\n\n/*\n * This buffer is rather large for stack allocation, so use a single buffer for\n * all profile dumps.\n */\nstatic malloc_mutex_t\tprof_dump_mtx;\nstatic char\t\tprof_dump_buf[\n    /* Minimize memory bloat for non-prof builds. */\n#ifdef JEMALLOC_PROF\n    PROF_DUMP_BUFSIZE\n#else\n    1\n#endif\n];\nstatic size_t\t\tprof_dump_buf_end;\nstatic int\t\tprof_dump_fd;\n\n/* Do not dump any profiles until bootstrapping is complete. */\nstatic bool\t\tprof_booted = false;\n\n/******************************************************************************/\n/*\n * Function prototypes for static functions that are referenced prior to\n * definition.\n */\n\nstatic bool\tprof_tctx_should_destroy(tsdn_t *tsdn, prof_tctx_t *tctx);\nstatic void\tprof_tctx_destroy(tsd_t *tsd, prof_tctx_t *tctx);\nstatic bool\tprof_tdata_should_destroy(tsdn_t *tsdn, prof_tdata_t *tdata,\n    bool even_if_attached);\nstatic void\tprof_tdata_destroy(tsd_t *tsd, prof_tdata_t *tdata,\n    bool even_if_attached);\nstatic char\t*prof_thread_name_alloc(tsdn_t *tsdn, const char *thread_name);\n\n/* Hashtable functions for log_bt_node_set and log_thr_node_set. */\nstatic void prof_thr_node_hash(const void *key, size_t r_hash[2]);\nstatic bool prof_thr_node_keycomp(const void *k1, const void *k2);\nstatic void prof_bt_node_hash(const void *key, size_t r_hash[2]);\nstatic bool prof_bt_node_keycomp(const void *k1, const void *k2);\n\n/******************************************************************************/\n/* Red-black trees. */\n\nstatic int\nprof_tctx_comp(const prof_tctx_t *a, const prof_tctx_t *b) {\n\tuint64_t a_thr_uid = a->thr_uid;\n\tuint64_t b_thr_uid = b->thr_uid;\n\tint ret = (a_thr_uid > b_thr_uid) - (a_thr_uid < b_thr_uid);\n\tif (ret == 0) {\n\t\tuint64_t a_thr_discrim = a->thr_discrim;\n\t\tuint64_t b_thr_discrim = b->thr_discrim;\n\t\tret = (a_thr_discrim > b_thr_discrim) - (a_thr_discrim <\n\t\t    b_thr_discrim);\n\t\tif (ret == 0) {\n\t\t\tuint64_t a_tctx_uid = a->tctx_uid;\n\t\t\tuint64_t b_tctx_uid = b->tctx_uid;\n\t\t\tret = (a_tctx_uid > b_tctx_uid) - (a_tctx_uid <\n\t\t\t    b_tctx_uid);\n\t\t}\n\t}\n\treturn ret;\n}\n\nrb_gen(static UNUSED, tctx_tree_, prof_tctx_tree_t, prof_tctx_t,\n    tctx_link, prof_tctx_comp)\n\nstatic int\nprof_gctx_comp(const prof_gctx_t *a, const prof_gctx_t *b) {\n\tunsigned a_len = a->bt.len;\n\tunsigned b_len = b->bt.len;\n\tunsigned comp_len = (a_len < b_len) ? a_len : b_len;\n\tint ret = memcmp(a->bt.vec, b->bt.vec, comp_len * sizeof(void *));\n\tif (ret == 0) {\n\t\tret = (a_len > b_len) - (a_len < b_len);\n\t}\n\treturn ret;\n}\n\nrb_gen(static UNUSED, gctx_tree_, prof_gctx_tree_t, prof_gctx_t, dump_link,\n    prof_gctx_comp)\n\nstatic int\nprof_tdata_comp(const prof_tdata_t *a, const prof_tdata_t *b) {\n\tint ret;\n\tuint64_t a_uid = a->thr_uid;\n\tuint64_t b_uid = b->thr_uid;\n\n\tret = ((a_uid > b_uid) - (a_uid < b_uid));\n\tif (ret == 0) {\n\t\tuint64_t a_discrim = a->thr_discrim;\n\t\tuint64_t b_discrim = b->thr_discrim;\n\n\t\tret = ((a_discrim > b_discrim) - (a_discrim < b_discrim));\n\t}\n\treturn ret;\n}\n\nrb_gen(static UNUSED, tdata_tree_, prof_tdata_tree_t, prof_tdata_t, tdata_link,\n    prof_tdata_comp)\n\n/******************************************************************************/\n\nvoid\nprof_alloc_rollback(tsd_t *tsd, prof_tctx_t *tctx, bool updated) {\n\tprof_tdata_t *tdata;\n\n\tcassert(config_prof);\n\n\tif (updated) {\n\t\t/*\n\t\t * Compute a new sample threshold.  This isn't very important in\n\t\t * practice, because this function is rarely executed, so the\n\t\t * potential for sample bias is minimal except in contrived\n\t\t * programs.\n\t\t */\n\t\ttdata = prof_tdata_get(tsd, true);\n\t\tif (tdata != NULL) {\n\t\t\tprof_sample_threshold_update(tdata);\n\t\t}\n\t}\n\n\tif ((uintptr_t)tctx > (uintptr_t)1U) {\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), tctx->tdata->lock);\n\t\ttctx->prepared = false;\n\t\tif (prof_tctx_should_destroy(tsd_tsdn(tsd), tctx)) {\n\t\t\tprof_tctx_destroy(tsd, tctx);\n\t\t} else {\n\t\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), tctx->tdata->lock);\n\t\t}\n\t}\n}\n\nvoid\nprof_malloc_sample_object(tsdn_t *tsdn, const void *ptr, size_t usize,\n    prof_tctx_t *tctx) {\n\tprof_tctx_set(tsdn, ptr, usize, NULL, tctx);\n\n\t/* Get the current time and set this in the extent_t. We'll read this\n\t * when free() is called. */\n\tnstime_t t = NSTIME_ZERO_INITIALIZER;\n\tnstime_update(&t);\n\tprof_alloc_time_set(tsdn, ptr, NULL, t);\n\n\tmalloc_mutex_lock(tsdn, tctx->tdata->lock);\n\ttctx->cnts.curobjs++;\n\ttctx->cnts.curbytes += usize;\n\tif (opt_prof_accum) {\n\t\ttctx->cnts.accumobjs++;\n\t\ttctx->cnts.accumbytes += usize;\n\t}\n\ttctx->prepared = false;\n\tmalloc_mutex_unlock(tsdn, tctx->tdata->lock);\n}\n\nstatic size_t\nprof_log_bt_index(tsd_t *tsd, prof_bt_t *bt) {\n\tassert(prof_logging_state == prof_logging_state_started);\n\tmalloc_mutex_assert_owner(tsd_tsdn(tsd), &log_mtx);\n\n\tprof_bt_node_t dummy_node;\n\tdummy_node.bt = *bt;\n\tprof_bt_node_t *node;\n\n\t/* See if this backtrace is already cached in the table. */\n\tif (ckh_search(&log_bt_node_set, (void *)(&dummy_node),\n\t    (void **)(&node), NULL)) {\n\t\tsize_t sz = offsetof(prof_bt_node_t, vec) +\n\t\t\t        (bt->len * sizeof(void *));\n\t\tprof_bt_node_t *new_node = (prof_bt_node_t *)\n\t\t    iallocztm(tsd_tsdn(tsd), sz, sz_size2index(sz), false, NULL,\n\t\t    true, arena_get(TSDN_NULL, 0, true), true);\n\t\tif (log_bt_first == NULL) {\n\t\t\tlog_bt_first = new_node;\n\t\t\tlog_bt_last = new_node;\n\t\t} else {\n\t\t\tlog_bt_last->next = new_node;\n\t\t\tlog_bt_last = new_node;\n\t\t}\n\n\t\tnew_node->next = NULL;\n\t\tnew_node->index = log_bt_index;\n\t\t/*\n\t\t * Copy the backtrace: bt is inside a tdata or gctx, which\n\t\t * might die before prof_log_stop is called.\n\t\t */\n\t\tnew_node->bt.len = bt->len;\n\t\tmemcpy(new_node->vec, bt->vec, bt->len * sizeof(void *));\n\t\tnew_node->bt.vec = new_node->vec;\n\n\t\tlog_bt_index++;\n\t\tckh_insert(tsd, &log_bt_node_set, (void *)new_node, NULL);\n\t\treturn new_node->index;\n\t} else {\n\t\treturn node->index;\n\t}\n}\nstatic size_t\nprof_log_thr_index(tsd_t *tsd, uint64_t thr_uid, const char *name) {\n\tassert(prof_logging_state == prof_logging_state_started);\n\tmalloc_mutex_assert_owner(tsd_tsdn(tsd), &log_mtx);\n\n\tprof_thr_node_t dummy_node;\n\tdummy_node.thr_uid = thr_uid;\n\tprof_thr_node_t *node;\n\n\t/* See if this thread is already cached in the table. */\n\tif (ckh_search(&log_thr_node_set, (void *)(&dummy_node),\n\t    (void **)(&node), NULL)) {\n\t\tsize_t sz = offsetof(prof_thr_node_t, name) + strlen(name) + 1;\n\t\tprof_thr_node_t *new_node = (prof_thr_node_t *)\n\t\t    iallocztm(tsd_tsdn(tsd), sz, sz_size2index(sz), false, NULL,\n\t\t    true, arena_get(TSDN_NULL, 0, true), true);\n\t\tif (log_thr_first == NULL) {\n\t\t\tlog_thr_first = new_node;\n\t\t\tlog_thr_last = new_node;\n\t\t} else {\n\t\t\tlog_thr_last->next = new_node;\n\t\t\tlog_thr_last = new_node;\n\t\t}\n\n\t\tnew_node->next = NULL;\n\t\tnew_node->index = log_thr_index;\n\t\tnew_node->thr_uid = thr_uid;\n\t\tstrcpy(new_node->name, name);\n\n\t\tlog_thr_index++;\n\t\tckh_insert(tsd, &log_thr_node_set, (void *)new_node, NULL);\n\t\treturn new_node->index;\n\t} else {\n\t\treturn node->index;\n\t}\n}\n\nstatic void\nprof_try_log(tsd_t *tsd, const void *ptr, size_t usize, prof_tctx_t *tctx) {\n\tmalloc_mutex_assert_owner(tsd_tsdn(tsd), tctx->tdata->lock);\n\n\tprof_tdata_t *cons_tdata = prof_tdata_get(tsd, false);\n\tif (cons_tdata == NULL) {\n\t\t/*\n\t\t * We decide not to log these allocations. cons_tdata will be\n\t\t * NULL only when the current thread is in a weird state (e.g.\n\t\t * it's being destroyed).\n\t\t */\n\t\treturn;\n\t}\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &log_mtx);\n\n\tif (prof_logging_state != prof_logging_state_started) {\n\t\tgoto label_done;\n\t}\n\n\tif (!log_tables_initialized) {\n\t\tbool err1 = ckh_new(tsd, &log_bt_node_set, PROF_CKH_MINITEMS,\n\t\t\t\tprof_bt_node_hash, prof_bt_node_keycomp);\n\t\tbool err2 = ckh_new(tsd, &log_thr_node_set, PROF_CKH_MINITEMS,\n\t\t\t\tprof_thr_node_hash, prof_thr_node_keycomp);\n\t\tif (err1 || err2) {\n\t\t\tgoto label_done;\n\t\t}\n\t\tlog_tables_initialized = true;\n\t}\n\n\tnstime_t alloc_time = prof_alloc_time_get(tsd_tsdn(tsd), ptr,\n\t\t\t          (alloc_ctx_t *)NULL);\n\tnstime_t free_time = NSTIME_ZERO_INITIALIZER;\n\tnstime_update(&free_time);\n\n\tsize_t sz = sizeof(prof_alloc_node_t);\n\tprof_alloc_node_t *new_node = (prof_alloc_node_t *)\n\t    iallocztm(tsd_tsdn(tsd), sz, sz_size2index(sz), false, NULL, true,\n\t    arena_get(TSDN_NULL, 0, true), true);\n\n\tconst char *prod_thr_name = (tctx->tdata->thread_name == NULL)?\n\t\t\t\t        \"\" : tctx->tdata->thread_name;\n\tconst char *cons_thr_name = prof_thread_name_get(tsd);\n\n\tprof_bt_t bt;\n\t/* Initialize the backtrace, using the buffer in tdata to store it. */\n\tbt_init(&bt, cons_tdata->vec);\n\tprof_backtrace(&bt);\n\tprof_bt_t *cons_bt = &bt;\n\n\t/* We haven't destroyed tctx yet, so gctx should be good to read. */\n\tprof_bt_t *prod_bt = &tctx->gctx->bt;\n\n\tnew_node->next = NULL;\n\tnew_node->alloc_thr_ind = prof_log_thr_index(tsd, tctx->tdata->thr_uid,\n\t\t\t\t      prod_thr_name);\n\tnew_node->free_thr_ind = prof_log_thr_index(tsd, cons_tdata->thr_uid,\n\t\t\t\t     cons_thr_name);\n\tnew_node->alloc_bt_ind = prof_log_bt_index(tsd, prod_bt);\n\tnew_node->free_bt_ind = prof_log_bt_index(tsd, cons_bt);\n\tnew_node->alloc_time_ns = nstime_ns(&alloc_time);\n\tnew_node->free_time_ns = nstime_ns(&free_time);\n\tnew_node->usize = usize;\n\n\tif (log_alloc_first == NULL) {\n\t\tlog_alloc_first = new_node;\n\t\tlog_alloc_last = new_node;\n\t} else {\n\t\tlog_alloc_last->next = new_node;\n\t\tlog_alloc_last = new_node;\n\t}\n\nlabel_done:\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &log_mtx);\n}\n\nvoid\nprof_free_sampled_object(tsd_t *tsd, const void *ptr, size_t usize,\n    prof_tctx_t *tctx) {\n\tmalloc_mutex_lock(tsd_tsdn(tsd), tctx->tdata->lock);\n\n\tassert(tctx->cnts.curobjs > 0);\n\tassert(tctx->cnts.curbytes >= usize);\n\ttctx->cnts.curobjs--;\n\ttctx->cnts.curbytes -= usize;\n\n\tprof_try_log(tsd, ptr, usize, tctx);\n\n\tif (prof_tctx_should_destroy(tsd_tsdn(tsd), tctx)) {\n\t\tprof_tctx_destroy(tsd, tctx);\n\t} else {\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), tctx->tdata->lock);\n\t}\n}\n\nvoid\nbt_init(prof_bt_t *bt, void **vec) {\n\tcassert(config_prof);\n\n\tbt->vec = vec;\n\tbt->len = 0;\n}\n\nstatic void\nprof_enter(tsd_t *tsd, prof_tdata_t *tdata) {\n\tcassert(config_prof);\n\tassert(tdata == prof_tdata_get(tsd, false));\n\n\tif (tdata != NULL) {\n\t\tassert(!tdata->enq);\n\t\ttdata->enq = true;\n\t}\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &bt2gctx_mtx);\n}\n\nstatic void\nprof_leave(tsd_t *tsd, prof_tdata_t *tdata) {\n\tcassert(config_prof);\n\tassert(tdata == prof_tdata_get(tsd, false));\n\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &bt2gctx_mtx);\n\n\tif (tdata != NULL) {\n\t\tbool idump, gdump;\n\n\t\tassert(tdata->enq);\n\t\ttdata->enq = false;\n\t\tidump = tdata->enq_idump;\n\t\ttdata->enq_idump = false;\n\t\tgdump = tdata->enq_gdump;\n\t\ttdata->enq_gdump = false;\n\n\t\tif (idump) {\n\t\t\tprof_idump(tsd_tsdn(tsd));\n\t\t}\n\t\tif (gdump) {\n\t\t\tprof_gdump(tsd_tsdn(tsd));\n\t\t}\n\t}\n}\n\n#ifdef JEMALLOC_PROF_LIBUNWIND\nvoid\nprof_backtrace(prof_bt_t *bt) {\n\tint nframes;\n\n\tcassert(config_prof);\n\tassert(bt->len == 0);\n\tassert(bt->vec != NULL);\n\n\tnframes = unw_backtrace(bt->vec, PROF_BT_MAX);\n\tif (nframes <= 0) {\n\t\treturn;\n\t}\n\tbt->len = nframes;\n}\n#elif (defined(JEMALLOC_PROF_LIBGCC))\nstatic _Unwind_Reason_Code\nprof_unwind_init_callback(struct _Unwind_Context *context, void *arg) {\n\tcassert(config_prof);\n\n\treturn _URC_NO_REASON;\n}\n\nstatic _Unwind_Reason_Code\nprof_unwind_callback(struct _Unwind_Context *context, void *arg) {\n\tprof_unwind_data_t *data = (prof_unwind_data_t *)arg;\n\tvoid *ip;\n\n\tcassert(config_prof);\n\n\tip = (void *)_Unwind_GetIP(context);\n\tif (ip == NULL) {\n\t\treturn _URC_END_OF_STACK;\n\t}\n\tdata->bt->vec[data->bt->len] = ip;\n\tdata->bt->len++;\n\tif (data->bt->len == data->max) {\n\t\treturn _URC_END_OF_STACK;\n\t}\n\n\treturn _URC_NO_REASON;\n}\n\nvoid\nprof_backtrace(prof_bt_t *bt) {\n\tprof_unwind_data_t data = {bt, PROF_BT_MAX};\n\n\tcassert(config_prof);\n\n\t_Unwind_Backtrace(prof_unwind_callback, &data);\n}\n#elif (defined(JEMALLOC_PROF_GCC))\nvoid\nprof_backtrace(prof_bt_t *bt) {\n#define BT_FRAME(i)\t\t\t\t\t\t\t\\\n\tif ((i) < PROF_BT_MAX) {\t\t\t\t\t\\\n\t\tvoid *p;\t\t\t\t\t\t\\\n\t\tif (__builtin_frame_address(i) == 0) {\t\t\t\\\n\t\t\treturn;\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\tp = __builtin_return_address(i);\t\t\t\\\n\t\tif (p == NULL) {\t\t\t\t\t\\\n\t\t\treturn;\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\tbt->vec[(i)] = p;\t\t\t\t\t\\\n\t\tbt->len = (i) + 1;\t\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t\treturn;\t\t\t\t\t\t\t\\\n\t}\n\n\tcassert(config_prof);\n\n\tBT_FRAME(0)\n\tBT_FRAME(1)\n\tBT_FRAME(2)\n\tBT_FRAME(3)\n\tBT_FRAME(4)\n\tBT_FRAME(5)\n\tBT_FRAME(6)\n\tBT_FRAME(7)\n\tBT_FRAME(8)\n\tBT_FRAME(9)\n\n\tBT_FRAME(10)\n\tBT_FRAME(11)\n\tBT_FRAME(12)\n\tBT_FRAME(13)\n\tBT_FRAME(14)\n\tBT_FRAME(15)\n\tBT_FRAME(16)\n\tBT_FRAME(17)\n\tBT_FRAME(18)\n\tBT_FRAME(19)\n\n\tBT_FRAME(20)\n\tBT_FRAME(21)\n\tBT_FRAME(22)\n\tBT_FRAME(23)\n\tBT_FRAME(24)\n\tBT_FRAME(25)\n\tBT_FRAME(26)\n\tBT_FRAME(27)\n\tBT_FRAME(28)\n\tBT_FRAME(29)\n\n\tBT_FRAME(30)\n\tBT_FRAME(31)\n\tBT_FRAME(32)\n\tBT_FRAME(33)\n\tBT_FRAME(34)\n\tBT_FRAME(35)\n\tBT_FRAME(36)\n\tBT_FRAME(37)\n\tBT_FRAME(38)\n\tBT_FRAME(39)\n\n\tBT_FRAME(40)\n\tBT_FRAME(41)\n\tBT_FRAME(42)\n\tBT_FRAME(43)\n\tBT_FRAME(44)\n\tBT_FRAME(45)\n\tBT_FRAME(46)\n\tBT_FRAME(47)\n\tBT_FRAME(48)\n\tBT_FRAME(49)\n\n\tBT_FRAME(50)\n\tBT_FRAME(51)\n\tBT_FRAME(52)\n\tBT_FRAME(53)\n\tBT_FRAME(54)\n\tBT_FRAME(55)\n\tBT_FRAME(56)\n\tBT_FRAME(57)\n\tBT_FRAME(58)\n\tBT_FRAME(59)\n\n\tBT_FRAME(60)\n\tBT_FRAME(61)\n\tBT_FRAME(62)\n\tBT_FRAME(63)\n\tBT_FRAME(64)\n\tBT_FRAME(65)\n\tBT_FRAME(66)\n\tBT_FRAME(67)\n\tBT_FRAME(68)\n\tBT_FRAME(69)\n\n\tBT_FRAME(70)\n\tBT_FRAME(71)\n\tBT_FRAME(72)\n\tBT_FRAME(73)\n\tBT_FRAME(74)\n\tBT_FRAME(75)\n\tBT_FRAME(76)\n\tBT_FRAME(77)\n\tBT_FRAME(78)\n\tBT_FRAME(79)\n\n\tBT_FRAME(80)\n\tBT_FRAME(81)\n\tBT_FRAME(82)\n\tBT_FRAME(83)\n\tBT_FRAME(84)\n\tBT_FRAME(85)\n\tBT_FRAME(86)\n\tBT_FRAME(87)\n\tBT_FRAME(88)\n\tBT_FRAME(89)\n\n\tBT_FRAME(90)\n\tBT_FRAME(91)\n\tBT_FRAME(92)\n\tBT_FRAME(93)\n\tBT_FRAME(94)\n\tBT_FRAME(95)\n\tBT_FRAME(96)\n\tBT_FRAME(97)\n\tBT_FRAME(98)\n\tBT_FRAME(99)\n\n\tBT_FRAME(100)\n\tBT_FRAME(101)\n\tBT_FRAME(102)\n\tBT_FRAME(103)\n\tBT_FRAME(104)\n\tBT_FRAME(105)\n\tBT_FRAME(106)\n\tBT_FRAME(107)\n\tBT_FRAME(108)\n\tBT_FRAME(109)\n\n\tBT_FRAME(110)\n\tBT_FRAME(111)\n\tBT_FRAME(112)\n\tBT_FRAME(113)\n\tBT_FRAME(114)\n\tBT_FRAME(115)\n\tBT_FRAME(116)\n\tBT_FRAME(117)\n\tBT_FRAME(118)\n\tBT_FRAME(119)\n\n\tBT_FRAME(120)\n\tBT_FRAME(121)\n\tBT_FRAME(122)\n\tBT_FRAME(123)\n\tBT_FRAME(124)\n\tBT_FRAME(125)\n\tBT_FRAME(126)\n\tBT_FRAME(127)\n#undef BT_FRAME\n}\n#else\nvoid\nprof_backtrace(prof_bt_t *bt) {\n\tcassert(config_prof);\n\tnot_reached();\n}\n#endif\n\nstatic malloc_mutex_t *\nprof_gctx_mutex_choose(void) {\n\tunsigned ngctxs = atomic_fetch_add_u(&cum_gctxs, 1, ATOMIC_RELAXED);\n\n\treturn &gctx_locks[(ngctxs - 1) % PROF_NCTX_LOCKS];\n}\n\nstatic malloc_mutex_t *\nprof_tdata_mutex_choose(uint64_t thr_uid) {\n\treturn &tdata_locks[thr_uid % PROF_NTDATA_LOCKS];\n}\n\nstatic prof_gctx_t *\nprof_gctx_create(tsdn_t *tsdn, prof_bt_t *bt) {\n\t/*\n\t * Create a single allocation that has space for vec of length bt->len.\n\t */\n\tsize_t size = offsetof(prof_gctx_t, vec) + (bt->len * sizeof(void *));\n\tprof_gctx_t *gctx = (prof_gctx_t *)iallocztm(tsdn, size,\n\t    sz_size2index(size), false, NULL, true, arena_get(TSDN_NULL, 0, true),\n\t    true);\n\tif (gctx == NULL) {\n\t\treturn NULL;\n\t}\n\tgctx->lock = prof_gctx_mutex_choose();\n\t/*\n\t * Set nlimbo to 1, in order to avoid a race condition with\n\t * prof_tctx_destroy()/prof_gctx_try_destroy().\n\t */\n\tgctx->nlimbo = 1;\n\ttctx_tree_new(&gctx->tctxs);\n\t/* Duplicate bt. */\n\tmemcpy(gctx->vec, bt->vec, bt->len * sizeof(void *));\n\tgctx->bt.vec = gctx->vec;\n\tgctx->bt.len = bt->len;\n\treturn gctx;\n}\n\nstatic void\nprof_gctx_try_destroy(tsd_t *tsd, prof_tdata_t *tdata_self, prof_gctx_t *gctx,\n    prof_tdata_t *tdata) {\n\tcassert(config_prof);\n\n\t/*\n\t * Check that gctx is still unused by any thread cache before destroying\n\t * it.  prof_lookup() increments gctx->nlimbo in order to avoid a race\n\t * condition with this function, as does prof_tctx_destroy() in order to\n\t * avoid a race between the main body of prof_tctx_destroy() and entry\n\t * into this function.\n\t */\n\tprof_enter(tsd, tdata_self);\n\tmalloc_mutex_lock(tsd_tsdn(tsd), gctx->lock);\n\tassert(gctx->nlimbo != 0);\n\tif (tctx_tree_empty(&gctx->tctxs) && gctx->nlimbo == 1) {\n\t\t/* Remove gctx from bt2gctx. */\n\t\tif (ckh_remove(tsd, &bt2gctx, &gctx->bt, NULL, NULL)) {\n\t\t\tnot_reached();\n\t\t}\n\t\tprof_leave(tsd, tdata_self);\n\t\t/* Destroy gctx. */\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock);\n\t\tidalloctm(tsd_tsdn(tsd), gctx, NULL, NULL, true, true);\n\t} else {\n\t\t/*\n\t\t * Compensate for increment in prof_tctx_destroy() or\n\t\t * prof_lookup().\n\t\t */\n\t\tgctx->nlimbo--;\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock);\n\t\tprof_leave(tsd, tdata_self);\n\t}\n}\n\nstatic bool\nprof_tctx_should_destroy(tsdn_t *tsdn, prof_tctx_t *tctx) {\n\tmalloc_mutex_assert_owner(tsdn, tctx->tdata->lock);\n\n\tif (opt_prof_accum) {\n\t\treturn false;\n\t}\n\tif (tctx->cnts.curobjs != 0) {\n\t\treturn false;\n\t}\n\tif (tctx->prepared) {\n\t\treturn false;\n\t}\n\treturn true;\n}\n\nstatic bool\nprof_gctx_should_destroy(prof_gctx_t *gctx) {\n\tif (opt_prof_accum) {\n\t\treturn false;\n\t}\n\tif (!tctx_tree_empty(&gctx->tctxs)) {\n\t\treturn false;\n\t}\n\tif (gctx->nlimbo != 0) {\n\t\treturn false;\n\t}\n\treturn true;\n}\n\nstatic void\nprof_tctx_destroy(tsd_t *tsd, prof_tctx_t *tctx) {\n\tprof_tdata_t *tdata = tctx->tdata;\n\tprof_gctx_t *gctx = tctx->gctx;\n\tbool destroy_tdata, destroy_tctx, destroy_gctx;\n\n\tmalloc_mutex_assert_owner(tsd_tsdn(tsd), tctx->tdata->lock);\n\n\tassert(tctx->cnts.curobjs == 0);\n\tassert(tctx->cnts.curbytes == 0);\n\tassert(!opt_prof_accum);\n\tassert(tctx->cnts.accumobjs == 0);\n\tassert(tctx->cnts.accumbytes == 0);\n\n\tckh_remove(tsd, &tdata->bt2tctx, &gctx->bt, NULL, NULL);\n\tdestroy_tdata = prof_tdata_should_destroy(tsd_tsdn(tsd), tdata, false);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), tdata->lock);\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), gctx->lock);\n\tswitch (tctx->state) {\n\tcase prof_tctx_state_nominal:\n\t\ttctx_tree_remove(&gctx->tctxs, tctx);\n\t\tdestroy_tctx = true;\n\t\tif (prof_gctx_should_destroy(gctx)) {\n\t\t\t/*\n\t\t\t * Increment gctx->nlimbo in order to keep another\n\t\t\t * thread from winning the race to destroy gctx while\n\t\t\t * this one has gctx->lock dropped.  Without this, it\n\t\t\t * would be possible for another thread to:\n\t\t\t *\n\t\t\t * 1) Sample an allocation associated with gctx.\n\t\t\t * 2) Deallocate the sampled object.\n\t\t\t * 3) Successfully prof_gctx_try_destroy(gctx).\n\t\t\t *\n\t\t\t * The result would be that gctx no longer exists by the\n\t\t\t * time this thread accesses it in\n\t\t\t * prof_gctx_try_destroy().\n\t\t\t */\n\t\t\tgctx->nlimbo++;\n\t\t\tdestroy_gctx = true;\n\t\t} else {\n\t\t\tdestroy_gctx = false;\n\t\t}\n\t\tbreak;\n\tcase prof_tctx_state_dumping:\n\t\t/*\n\t\t * A dumping thread needs tctx to remain valid until dumping\n\t\t * has finished.  Change state such that the dumping thread will\n\t\t * complete destruction during a late dump iteration phase.\n\t\t */\n\t\ttctx->state = prof_tctx_state_purgatory;\n\t\tdestroy_tctx = false;\n\t\tdestroy_gctx = false;\n\t\tbreak;\n\tdefault:\n\t\tnot_reached();\n\t\tdestroy_tctx = false;\n\t\tdestroy_gctx = false;\n\t}\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock);\n\tif (destroy_gctx) {\n\t\tprof_gctx_try_destroy(tsd, prof_tdata_get(tsd, false), gctx,\n\t\t    tdata);\n\t}\n\n\tmalloc_mutex_assert_not_owner(tsd_tsdn(tsd), tctx->tdata->lock);\n\n\tif (destroy_tdata) {\n\t\tprof_tdata_destroy(tsd, tdata, false);\n\t}\n\n\tif (destroy_tctx) {\n\t\tidalloctm(tsd_tsdn(tsd), tctx, NULL, NULL, true, true);\n\t}\n}\n\nstatic bool\nprof_lookup_global(tsd_t *tsd, prof_bt_t *bt, prof_tdata_t *tdata,\n    void **p_btkey, prof_gctx_t **p_gctx, bool *p_new_gctx) {\n\tunion {\n\t\tprof_gctx_t\t*p;\n\t\tvoid\t\t*v;\n\t} gctx, tgctx;\n\tunion {\n\t\tprof_bt_t\t*p;\n\t\tvoid\t\t*v;\n\t} btkey;\n\tbool new_gctx;\n\n\tprof_enter(tsd, tdata);\n\tif (ckh_search(&bt2gctx, bt, &btkey.v, &gctx.v)) {\n\t\t/* bt has never been seen before.  Insert it. */\n\t\tprof_leave(tsd, tdata);\n\t\ttgctx.p = prof_gctx_create(tsd_tsdn(tsd), bt);\n\t\tif (tgctx.v == NULL) {\n\t\t\treturn true;\n\t\t}\n\t\tprof_enter(tsd, tdata);\n\t\tif (ckh_search(&bt2gctx, bt, &btkey.v, &gctx.v)) {\n\t\t\tgctx.p = tgctx.p;\n\t\t\tbtkey.p = &gctx.p->bt;\n\t\t\tif (ckh_insert(tsd, &bt2gctx, btkey.v, gctx.v)) {\n\t\t\t\t/* OOM. */\n\t\t\t\tprof_leave(tsd, tdata);\n\t\t\t\tidalloctm(tsd_tsdn(tsd), gctx.v, NULL, NULL,\n\t\t\t\t    true, true);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tnew_gctx = true;\n\t\t} else {\n\t\t\tnew_gctx = false;\n\t\t}\n\t} else {\n\t\ttgctx.v = NULL;\n\t\tnew_gctx = false;\n\t}\n\n\tif (!new_gctx) {\n\t\t/*\n\t\t * Increment nlimbo, in order to avoid a race condition with\n\t\t * prof_tctx_destroy()/prof_gctx_try_destroy().\n\t\t */\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), gctx.p->lock);\n\t\tgctx.p->nlimbo++;\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), gctx.p->lock);\n\t\tnew_gctx = false;\n\n\t\tif (tgctx.v != NULL) {\n\t\t\t/* Lost race to insert. */\n\t\t\tidalloctm(tsd_tsdn(tsd), tgctx.v, NULL, NULL, true,\n\t\t\t    true);\n\t\t}\n\t}\n\tprof_leave(tsd, tdata);\n\n\t*p_btkey = btkey.v;\n\t*p_gctx = gctx.p;\n\t*p_new_gctx = new_gctx;\n\treturn false;\n}\n\nprof_tctx_t *\nprof_lookup(tsd_t *tsd, prof_bt_t *bt) {\n\tunion {\n\t\tprof_tctx_t\t*p;\n\t\tvoid\t\t*v;\n\t} ret;\n\tprof_tdata_t *tdata;\n\tbool not_found;\n\n\tcassert(config_prof);\n\n\ttdata = prof_tdata_get(tsd, false);\n\tif (tdata == NULL) {\n\t\treturn NULL;\n\t}\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), tdata->lock);\n\tnot_found = ckh_search(&tdata->bt2tctx, bt, NULL, &ret.v);\n\tif (!not_found) { /* Note double negative! */\n\t\tret.p->prepared = true;\n\t}\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), tdata->lock);\n\tif (not_found) {\n\t\tvoid *btkey;\n\t\tprof_gctx_t *gctx;\n\t\tbool new_gctx, error;\n\n\t\t/*\n\t\t * This thread's cache lacks bt.  Look for it in the global\n\t\t * cache.\n\t\t */\n\t\tif (prof_lookup_global(tsd, bt, tdata, &btkey, &gctx,\n\t\t    &new_gctx)) {\n\t\t\treturn NULL;\n\t\t}\n\n\t\t/* Link a prof_tctx_t into gctx for this thread. */\n\t\tret.v = iallocztm(tsd_tsdn(tsd), sizeof(prof_tctx_t),\n\t\t    sz_size2index(sizeof(prof_tctx_t)), false, NULL, true,\n\t\t    arena_ichoose(tsd, NULL), true);\n\t\tif (ret.p == NULL) {\n\t\t\tif (new_gctx) {\n\t\t\t\tprof_gctx_try_destroy(tsd, tdata, gctx, tdata);\n\t\t\t}\n\t\t\treturn NULL;\n\t\t}\n\t\tret.p->tdata = tdata;\n\t\tret.p->thr_uid = tdata->thr_uid;\n\t\tret.p->thr_discrim = tdata->thr_discrim;\n\t\tmemset(&ret.p->cnts, 0, sizeof(prof_cnt_t));\n\t\tret.p->gctx = gctx;\n\t\tret.p->tctx_uid = tdata->tctx_uid_next++;\n\t\tret.p->prepared = true;\n\t\tret.p->state = prof_tctx_state_initializing;\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), tdata->lock);\n\t\terror = ckh_insert(tsd, &tdata->bt2tctx, btkey, ret.v);\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), tdata->lock);\n\t\tif (error) {\n\t\t\tif (new_gctx) {\n\t\t\t\tprof_gctx_try_destroy(tsd, tdata, gctx, tdata);\n\t\t\t}\n\t\t\tidalloctm(tsd_tsdn(tsd), ret.v, NULL, NULL, true, true);\n\t\t\treturn NULL;\n\t\t}\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), gctx->lock);\n\t\tret.p->state = prof_tctx_state_nominal;\n\t\ttctx_tree_insert(&gctx->tctxs, ret.p);\n\t\tgctx->nlimbo--;\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock);\n\t}\n\n\treturn ret.p;\n}\n\n/*\n * The bodies of this function and prof_leakcheck() are compiled out unless heap\n * profiling is enabled, so that it is possible to compile jemalloc with\n * floating point support completely disabled.  Avoiding floating point code is\n * important on memory-constrained systems, but it also enables a workaround for\n * versions of glibc that don't properly save/restore floating point registers\n * during dynamic lazy symbol loading (which internally calls into whatever\n * malloc implementation happens to be integrated into the application).  Note\n * that some compilers (e.g.  gcc 4.8) may use floating point registers for fast\n * memory moves, so jemalloc must be compiled with such optimizations disabled\n * (e.g.\n * -mno-sse) in order for the workaround to be complete.\n */\nvoid\nprof_sample_threshold_update(prof_tdata_t *tdata) {\n#ifdef JEMALLOC_PROF\n\tif (!config_prof) {\n\t\treturn;\n\t}\n\n\tif (lg_prof_sample == 0) {\n\t\ttsd_bytes_until_sample_set(tsd_fetch(), 0);\n\t\treturn;\n\t}\n\n\t/*\n\t * Compute sample interval as a geometrically distributed random\n\t * variable with mean (2^lg_prof_sample).\n\t *\n\t *                             __        __\n\t *                             |  log(u)  |                     1\n\t * tdata->bytes_until_sample = | -------- |, where p = ---------------\n\t *                             | log(1-p) |             lg_prof_sample\n\t *                                                     2\n\t *\n\t * For more information on the math, see:\n\t *\n\t *   Non-Uniform Random Variate Generation\n\t *   Luc Devroye\n\t *   Springer-Verlag, New York, 1986\n\t *   pp 500\n\t *   (http://luc.devroye.org/rnbookindex.html)\n\t */\n\tuint64_t r = prng_lg_range_u64(&tdata->prng_state, 53);\n\tdouble u = (double)r * (1.0/9007199254740992.0L);\n\tuint64_t bytes_until_sample = (uint64_t)(log(u) /\n\t    log(1.0 - (1.0 / (double)((uint64_t)1U << lg_prof_sample))))\n\t    + (uint64_t)1U;\n\tif (bytes_until_sample > SSIZE_MAX) {\n\t\tbytes_until_sample = SSIZE_MAX;\n\t}\n\ttsd_bytes_until_sample_set(tsd_fetch(), bytes_until_sample);\n\n#endif\n}\n\n#ifdef JEMALLOC_JET\nstatic prof_tdata_t *\nprof_tdata_count_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata,\n    void *arg) {\n\tsize_t *tdata_count = (size_t *)arg;\n\n\t(*tdata_count)++;\n\n\treturn NULL;\n}\n\nsize_t\nprof_tdata_count(void) {\n\tsize_t tdata_count = 0;\n\ttsdn_t *tsdn;\n\n\ttsdn = tsdn_fetch();\n\tmalloc_mutex_lock(tsdn, &tdatas_mtx);\n\ttdata_tree_iter(&tdatas, NULL, prof_tdata_count_iter,\n\t    (void *)&tdata_count);\n\tmalloc_mutex_unlock(tsdn, &tdatas_mtx);\n\n\treturn tdata_count;\n}\n\nsize_t\nprof_bt_count(void) {\n\tsize_t bt_count;\n\ttsd_t *tsd;\n\tprof_tdata_t *tdata;\n\n\ttsd = tsd_fetch();\n\ttdata = prof_tdata_get(tsd, false);\n\tif (tdata == NULL) {\n\t\treturn 0;\n\t}\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &bt2gctx_mtx);\n\tbt_count = ckh_count(&bt2gctx);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &bt2gctx_mtx);\n\n\treturn bt_count;\n}\n#endif\n\nstatic int\nprof_dump_open_impl(bool propagate_err, const char *filename) {\n\tint fd;\n\n\tfd = creat(filename, 0644);\n\tif (fd == -1 && !propagate_err) {\n\t\tmalloc_printf(\"<jemalloc>: creat(\\\"%s\\\"), 0644) failed\\n\",\n\t\t    filename);\n\t\tif (opt_abort) {\n\t\t\tabort();\n\t\t}\n\t}\n\n\treturn fd;\n}\nprof_dump_open_t *JET_MUTABLE prof_dump_open = prof_dump_open_impl;\n\nstatic bool\nprof_dump_flush(bool propagate_err) {\n\tbool ret = false;\n\tssize_t err;\n\n\tcassert(config_prof);\n\n\terr = malloc_write_fd(prof_dump_fd, prof_dump_buf, prof_dump_buf_end);\n\tif (err == -1) {\n\t\tif (!propagate_err) {\n\t\t\tmalloc_write(\"<jemalloc>: write() failed during heap \"\n\t\t\t    \"profile flush\\n\");\n\t\t\tif (opt_abort) {\n\t\t\t\tabort();\n\t\t\t}\n\t\t}\n\t\tret = true;\n\t}\n\tprof_dump_buf_end = 0;\n\n\treturn ret;\n}\n\nstatic bool\nprof_dump_close(bool propagate_err) {\n\tbool ret;\n\n\tassert(prof_dump_fd != -1);\n\tret = prof_dump_flush(propagate_err);\n\tclose(prof_dump_fd);\n\tprof_dump_fd = -1;\n\n\treturn ret;\n}\n\nstatic bool\nprof_dump_write(bool propagate_err, const char *s) {\n\tsize_t i, slen, n;\n\n\tcassert(config_prof);\n\n\ti = 0;\n\tslen = strlen(s);\n\twhile (i < slen) {\n\t\t/* Flush the buffer if it is full. */\n\t\tif (prof_dump_buf_end == PROF_DUMP_BUFSIZE) {\n\t\t\tif (prof_dump_flush(propagate_err) && propagate_err) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\n\t\tif (prof_dump_buf_end + slen - i <= PROF_DUMP_BUFSIZE) {\n\t\t\t/* Finish writing. */\n\t\t\tn = slen - i;\n\t\t} else {\n\t\t\t/* Write as much of s as will fit. */\n\t\t\tn = PROF_DUMP_BUFSIZE - prof_dump_buf_end;\n\t\t}\n\t\tmemcpy(&prof_dump_buf[prof_dump_buf_end], &s[i], n);\n\t\tprof_dump_buf_end += n;\n\t\ti += n;\n\t}\n\tassert(i == slen);\n\n\treturn false;\n}\n\nJEMALLOC_FORMAT_PRINTF(2, 3)\nstatic bool\nprof_dump_printf(bool propagate_err, const char *format, ...) {\n\tbool ret;\n\tva_list ap;\n\tchar buf[PROF_PRINTF_BUFSIZE];\n\n\tva_start(ap, format);\n\tmalloc_vsnprintf(buf, sizeof(buf), format, ap);\n\tva_end(ap);\n\tret = prof_dump_write(propagate_err, buf);\n\n\treturn ret;\n}\n\nstatic void\nprof_tctx_merge_tdata(tsdn_t *tsdn, prof_tctx_t *tctx, prof_tdata_t *tdata) {\n\tmalloc_mutex_assert_owner(tsdn, tctx->tdata->lock);\n\n\tmalloc_mutex_lock(tsdn, tctx->gctx->lock);\n\n\tswitch (tctx->state) {\n\tcase prof_tctx_state_initializing:\n\t\tmalloc_mutex_unlock(tsdn, tctx->gctx->lock);\n\t\treturn;\n\tcase prof_tctx_state_nominal:\n\t\ttctx->state = prof_tctx_state_dumping;\n\t\tmalloc_mutex_unlock(tsdn, tctx->gctx->lock);\n\n\t\tmemcpy(&tctx->dump_cnts, &tctx->cnts, sizeof(prof_cnt_t));\n\n\t\ttdata->cnt_summed.curobjs += tctx->dump_cnts.curobjs;\n\t\ttdata->cnt_summed.curbytes += tctx->dump_cnts.curbytes;\n\t\tif (opt_prof_accum) {\n\t\t\ttdata->cnt_summed.accumobjs +=\n\t\t\t    tctx->dump_cnts.accumobjs;\n\t\t\ttdata->cnt_summed.accumbytes +=\n\t\t\t    tctx->dump_cnts.accumbytes;\n\t\t}\n\t\tbreak;\n\tcase prof_tctx_state_dumping:\n\tcase prof_tctx_state_purgatory:\n\t\tnot_reached();\n\t}\n}\n\nstatic void\nprof_tctx_merge_gctx(tsdn_t *tsdn, prof_tctx_t *tctx, prof_gctx_t *gctx) {\n\tmalloc_mutex_assert_owner(tsdn, gctx->lock);\n\n\tgctx->cnt_summed.curobjs += tctx->dump_cnts.curobjs;\n\tgctx->cnt_summed.curbytes += tctx->dump_cnts.curbytes;\n\tif (opt_prof_accum) {\n\t\tgctx->cnt_summed.accumobjs += tctx->dump_cnts.accumobjs;\n\t\tgctx->cnt_summed.accumbytes += tctx->dump_cnts.accumbytes;\n\t}\n}\n\nstatic prof_tctx_t *\nprof_tctx_merge_iter(prof_tctx_tree_t *tctxs, prof_tctx_t *tctx, void *arg) {\n\ttsdn_t *tsdn = (tsdn_t *)arg;\n\n\tmalloc_mutex_assert_owner(tsdn, tctx->gctx->lock);\n\n\tswitch (tctx->state) {\n\tcase prof_tctx_state_nominal:\n\t\t/* New since dumping started; ignore. */\n\t\tbreak;\n\tcase prof_tctx_state_dumping:\n\tcase prof_tctx_state_purgatory:\n\t\tprof_tctx_merge_gctx(tsdn, tctx, tctx->gctx);\n\t\tbreak;\n\tdefault:\n\t\tnot_reached();\n\t}\n\n\treturn NULL;\n}\n\nstruct prof_tctx_dump_iter_arg_s {\n\ttsdn_t\t*tsdn;\n\tbool\tpropagate_err;\n};\n\nstatic prof_tctx_t *\nprof_tctx_dump_iter(prof_tctx_tree_t *tctxs, prof_tctx_t *tctx, void *opaque) {\n\tstruct prof_tctx_dump_iter_arg_s *arg =\n\t    (struct prof_tctx_dump_iter_arg_s *)opaque;\n\n\tmalloc_mutex_assert_owner(arg->tsdn, tctx->gctx->lock);\n\n\tswitch (tctx->state) {\n\tcase prof_tctx_state_initializing:\n\tcase prof_tctx_state_nominal:\n\t\t/* Not captured by this dump. */\n\t\tbreak;\n\tcase prof_tctx_state_dumping:\n\tcase prof_tctx_state_purgatory:\n\t\tif (prof_dump_printf(arg->propagate_err,\n\t\t    \"  t%\"FMTu64\": %\"FMTu64\": %\"FMTu64\" [%\"FMTu64\": \"\n\t\t    \"%\"FMTu64\"]\\n\", tctx->thr_uid, tctx->dump_cnts.curobjs,\n\t\t    tctx->dump_cnts.curbytes, tctx->dump_cnts.accumobjs,\n\t\t    tctx->dump_cnts.accumbytes)) {\n\t\t\treturn tctx;\n\t\t}\n\t\tbreak;\n\tdefault:\n\t\tnot_reached();\n\t}\n\treturn NULL;\n}\n\nstatic prof_tctx_t *\nprof_tctx_finish_iter(prof_tctx_tree_t *tctxs, prof_tctx_t *tctx, void *arg) {\n\ttsdn_t *tsdn = (tsdn_t *)arg;\n\tprof_tctx_t *ret;\n\n\tmalloc_mutex_assert_owner(tsdn, tctx->gctx->lock);\n\n\tswitch (tctx->state) {\n\tcase prof_tctx_state_nominal:\n\t\t/* New since dumping started; ignore. */\n\t\tbreak;\n\tcase prof_tctx_state_dumping:\n\t\ttctx->state = prof_tctx_state_nominal;\n\t\tbreak;\n\tcase prof_tctx_state_purgatory:\n\t\tret = tctx;\n\t\tgoto label_return;\n\tdefault:\n\t\tnot_reached();\n\t}\n\n\tret = NULL;\nlabel_return:\n\treturn ret;\n}\n\nstatic void\nprof_dump_gctx_prep(tsdn_t *tsdn, prof_gctx_t *gctx, prof_gctx_tree_t *gctxs) {\n\tcassert(config_prof);\n\n\tmalloc_mutex_lock(tsdn, gctx->lock);\n\n\t/*\n\t * Increment nlimbo so that gctx won't go away before dump.\n\t * Additionally, link gctx into the dump list so that it is included in\n\t * prof_dump()'s second pass.\n\t */\n\tgctx->nlimbo++;\n\tgctx_tree_insert(gctxs, gctx);\n\n\tmemset(&gctx->cnt_summed, 0, sizeof(prof_cnt_t));\n\n\tmalloc_mutex_unlock(tsdn, gctx->lock);\n}\n\nstruct prof_gctx_merge_iter_arg_s {\n\ttsdn_t\t*tsdn;\n\tsize_t\tleak_ngctx;\n};\n\nstatic prof_gctx_t *\nprof_gctx_merge_iter(prof_gctx_tree_t *gctxs, prof_gctx_t *gctx, void *opaque) {\n\tstruct prof_gctx_merge_iter_arg_s *arg =\n\t    (struct prof_gctx_merge_iter_arg_s *)opaque;\n\n\tmalloc_mutex_lock(arg->tsdn, gctx->lock);\n\ttctx_tree_iter(&gctx->tctxs, NULL, prof_tctx_merge_iter,\n\t    (void *)arg->tsdn);\n\tif (gctx->cnt_summed.curobjs != 0) {\n\t\targ->leak_ngctx++;\n\t}\n\tmalloc_mutex_unlock(arg->tsdn, gctx->lock);\n\n\treturn NULL;\n}\n\nstatic void\nprof_gctx_finish(tsd_t *tsd, prof_gctx_tree_t *gctxs) {\n\tprof_tdata_t *tdata = prof_tdata_get(tsd, false);\n\tprof_gctx_t *gctx;\n\n\t/*\n\t * Standard tree iteration won't work here, because as soon as we\n\t * decrement gctx->nlimbo and unlock gctx, another thread can\n\t * concurrently destroy it, which will corrupt the tree.  Therefore,\n\t * tear down the tree one node at a time during iteration.\n\t */\n\twhile ((gctx = gctx_tree_first(gctxs)) != NULL) {\n\t\tgctx_tree_remove(gctxs, gctx);\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), gctx->lock);\n\t\t{\n\t\t\tprof_tctx_t *next;\n\n\t\t\tnext = NULL;\n\t\t\tdo {\n\t\t\t\tprof_tctx_t *to_destroy =\n\t\t\t\t    tctx_tree_iter(&gctx->tctxs, next,\n\t\t\t\t    prof_tctx_finish_iter,\n\t\t\t\t    (void *)tsd_tsdn(tsd));\n\t\t\t\tif (to_destroy != NULL) {\n\t\t\t\t\tnext = tctx_tree_next(&gctx->tctxs,\n\t\t\t\t\t    to_destroy);\n\t\t\t\t\ttctx_tree_remove(&gctx->tctxs,\n\t\t\t\t\t    to_destroy);\n\t\t\t\t\tidalloctm(tsd_tsdn(tsd), to_destroy,\n\t\t\t\t\t    NULL, NULL, true, true);\n\t\t\t\t} else {\n\t\t\t\t\tnext = NULL;\n\t\t\t\t}\n\t\t\t} while (next != NULL);\n\t\t}\n\t\tgctx->nlimbo--;\n\t\tif (prof_gctx_should_destroy(gctx)) {\n\t\t\tgctx->nlimbo++;\n\t\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock);\n\t\t\tprof_gctx_try_destroy(tsd, tdata, gctx, tdata);\n\t\t} else {\n\t\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock);\n\t\t}\n\t}\n}\n\nstruct prof_tdata_merge_iter_arg_s {\n\ttsdn_t\t\t*tsdn;\n\tprof_cnt_t\tcnt_all;\n};\n\nstatic prof_tdata_t *\nprof_tdata_merge_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata,\n    void *opaque) {\n\tstruct prof_tdata_merge_iter_arg_s *arg =\n\t    (struct prof_tdata_merge_iter_arg_s *)opaque;\n\n\tmalloc_mutex_lock(arg->tsdn, tdata->lock);\n\tif (!tdata->expired) {\n\t\tsize_t tabind;\n\t\tunion {\n\t\t\tprof_tctx_t\t*p;\n\t\t\tvoid\t\t*v;\n\t\t} tctx;\n\n\t\ttdata->dumping = true;\n\t\tmemset(&tdata->cnt_summed, 0, sizeof(prof_cnt_t));\n\t\tfor (tabind = 0; !ckh_iter(&tdata->bt2tctx, &tabind, NULL,\n\t\t    &tctx.v);) {\n\t\t\tprof_tctx_merge_tdata(arg->tsdn, tctx.p, tdata);\n\t\t}\n\n\t\targ->cnt_all.curobjs += tdata->cnt_summed.curobjs;\n\t\targ->cnt_all.curbytes += tdata->cnt_summed.curbytes;\n\t\tif (opt_prof_accum) {\n\t\t\targ->cnt_all.accumobjs += tdata->cnt_summed.accumobjs;\n\t\t\targ->cnt_all.accumbytes += tdata->cnt_summed.accumbytes;\n\t\t}\n\t} else {\n\t\ttdata->dumping = false;\n\t}\n\tmalloc_mutex_unlock(arg->tsdn, tdata->lock);\n\n\treturn NULL;\n}\n\nstatic prof_tdata_t *\nprof_tdata_dump_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata,\n    void *arg) {\n\tbool propagate_err = *(bool *)arg;\n\n\tif (!tdata->dumping) {\n\t\treturn NULL;\n\t}\n\n\tif (prof_dump_printf(propagate_err,\n\t    \"  t%\"FMTu64\": %\"FMTu64\": %\"FMTu64\" [%\"FMTu64\": %\"FMTu64\"]%s%s\\n\",\n\t    tdata->thr_uid, tdata->cnt_summed.curobjs,\n\t    tdata->cnt_summed.curbytes, tdata->cnt_summed.accumobjs,\n\t    tdata->cnt_summed.accumbytes,\n\t    (tdata->thread_name != NULL) ? \" \" : \"\",\n\t    (tdata->thread_name != NULL) ? tdata->thread_name : \"\")) {\n\t\treturn tdata;\n\t}\n\treturn NULL;\n}\n\nstatic bool\nprof_dump_header_impl(tsdn_t *tsdn, bool propagate_err,\n    const prof_cnt_t *cnt_all) {\n\tbool ret;\n\n\tif (prof_dump_printf(propagate_err,\n\t    \"heap_v2/%\"FMTu64\"\\n\"\n\t    \"  t*: %\"FMTu64\": %\"FMTu64\" [%\"FMTu64\": %\"FMTu64\"]\\n\",\n\t    ((uint64_t)1U << lg_prof_sample), cnt_all->curobjs,\n\t    cnt_all->curbytes, cnt_all->accumobjs, cnt_all->accumbytes)) {\n\t\treturn true;\n\t}\n\n\tmalloc_mutex_lock(tsdn, &tdatas_mtx);\n\tret = (tdata_tree_iter(&tdatas, NULL, prof_tdata_dump_iter,\n\t    (void *)&propagate_err) != NULL);\n\tmalloc_mutex_unlock(tsdn, &tdatas_mtx);\n\treturn ret;\n}\nprof_dump_header_t *JET_MUTABLE prof_dump_header = prof_dump_header_impl;\n\nstatic bool\nprof_dump_gctx(tsdn_t *tsdn, bool propagate_err, prof_gctx_t *gctx,\n    const prof_bt_t *bt, prof_gctx_tree_t *gctxs) {\n\tbool ret;\n\tunsigned i;\n\tstruct prof_tctx_dump_iter_arg_s prof_tctx_dump_iter_arg;\n\n\tcassert(config_prof);\n\tmalloc_mutex_assert_owner(tsdn, gctx->lock);\n\n\t/* Avoid dumping such gctx's that have no useful data. */\n\tif ((!opt_prof_accum && gctx->cnt_summed.curobjs == 0) ||\n\t    (opt_prof_accum && gctx->cnt_summed.accumobjs == 0)) {\n\t\tassert(gctx->cnt_summed.curobjs == 0);\n\t\tassert(gctx->cnt_summed.curbytes == 0);\n\t\tassert(gctx->cnt_summed.accumobjs == 0);\n\t\tassert(gctx->cnt_summed.accumbytes == 0);\n\t\tret = false;\n\t\tgoto label_return;\n\t}\n\n\tif (prof_dump_printf(propagate_err, \"@\")) {\n\t\tret = true;\n\t\tgoto label_return;\n\t}\n\tfor (i = 0; i < bt->len; i++) {\n\t\tif (prof_dump_printf(propagate_err, \" %#\"FMTxPTR,\n\t\t    (uintptr_t)bt->vec[i])) {\n\t\t\tret = true;\n\t\t\tgoto label_return;\n\t\t}\n\t}\n\n\tif (prof_dump_printf(propagate_err,\n\t    \"\\n\"\n\t    \"  t*: %\"FMTu64\": %\"FMTu64\" [%\"FMTu64\": %\"FMTu64\"]\\n\",\n\t    gctx->cnt_summed.curobjs, gctx->cnt_summed.curbytes,\n\t    gctx->cnt_summed.accumobjs, gctx->cnt_summed.accumbytes)) {\n\t\tret = true;\n\t\tgoto label_return;\n\t}\n\n\tprof_tctx_dump_iter_arg.tsdn = tsdn;\n\tprof_tctx_dump_iter_arg.propagate_err = propagate_err;\n\tif (tctx_tree_iter(&gctx->tctxs, NULL, prof_tctx_dump_iter,\n\t    (void *)&prof_tctx_dump_iter_arg) != NULL) {\n\t\tret = true;\n\t\tgoto label_return;\n\t}\n\n\tret = false;\nlabel_return:\n\treturn ret;\n}\n\n#ifndef _WIN32\nJEMALLOC_FORMAT_PRINTF(1, 2)\nstatic int\nprof_open_maps(const char *format, ...) {\n\tint mfd;\n\tva_list ap;\n\tchar filename[PATH_MAX + 1];\n\n\tva_start(ap, format);\n\tmalloc_vsnprintf(filename, sizeof(filename), format, ap);\n\tva_end(ap);\n\n#if defined(O_CLOEXEC)\n\tmfd = open(filename, O_RDONLY | O_CLOEXEC);\n#else\n\tmfd = open(filename, O_RDONLY);\n\tif (mfd != -1) {\n\t\tfcntl(mfd, F_SETFD, fcntl(mfd, F_GETFD) | FD_CLOEXEC);\n\t}\n#endif\n\n\treturn mfd;\n}\n#endif\n\nstatic int\nprof_getpid(void) {\n#ifdef _WIN32\n\treturn GetCurrentProcessId();\n#else\n\treturn getpid();\n#endif\n}\n\nstatic bool\nprof_dump_maps(bool propagate_err) {\n\tbool ret;\n\tint mfd;\n\n\tcassert(config_prof);\n#ifdef __FreeBSD__\n\tmfd = prof_open_maps(\"/proc/curproc/map\");\n#elif defined(_WIN32)\n\tmfd = -1; // Not implemented\n#else\n\t{\n\t\tint pid = prof_getpid();\n\n\t\tmfd = prof_open_maps(\"/proc/%d/task/%d/maps\", pid, pid);\n\t\tif (mfd == -1) {\n\t\t\tmfd = prof_open_maps(\"/proc/%d/maps\", pid);\n\t\t}\n\t}\n#endif\n\tif (mfd != -1) {\n\t\tssize_t nread;\n\n\t\tif (prof_dump_write(propagate_err, \"\\nMAPPED_LIBRARIES:\\n\") &&\n\t\t    propagate_err) {\n\t\t\tret = true;\n\t\t\tgoto label_return;\n\t\t}\n\t\tnread = 0;\n\t\tdo {\n\t\t\tprof_dump_buf_end += nread;\n\t\t\tif (prof_dump_buf_end == PROF_DUMP_BUFSIZE) {\n\t\t\t\t/* Make space in prof_dump_buf before read(). */\n\t\t\t\tif (prof_dump_flush(propagate_err) &&\n\t\t\t\t    propagate_err) {\n\t\t\t\t\tret = true;\n\t\t\t\t\tgoto label_return;\n\t\t\t\t}\n\t\t\t}\n\t\t\tnread = malloc_read_fd(mfd,\n\t\t\t    &prof_dump_buf[prof_dump_buf_end], PROF_DUMP_BUFSIZE\n\t\t\t    - prof_dump_buf_end);\n\t\t} while (nread > 0);\n\t} else {\n\t\tret = true;\n\t\tgoto label_return;\n\t}\n\n\tret = false;\nlabel_return:\n\tif (mfd != -1) {\n\t\tclose(mfd);\n\t}\n\treturn ret;\n}\n\n/*\n * See prof_sample_threshold_update() comment for why the body of this function\n * is conditionally compiled.\n */\nstatic void\nprof_leakcheck(const prof_cnt_t *cnt_all, size_t leak_ngctx,\n    const char *filename) {\n#ifdef JEMALLOC_PROF\n\t/*\n\t * Scaling is equivalent AdjustSamples() in jeprof, but the result may\n\t * differ slightly from what jeprof reports, because here we scale the\n\t * summary values, whereas jeprof scales each context individually and\n\t * reports the sums of the scaled values.\n\t */\n\tif (cnt_all->curbytes != 0) {\n\t\tdouble sample_period = (double)((uint64_t)1 << lg_prof_sample);\n\t\tdouble ratio = (((double)cnt_all->curbytes) /\n\t\t    (double)cnt_all->curobjs) / sample_period;\n\t\tdouble scale_factor = 1.0 / (1.0 - exp(-ratio));\n\t\tuint64_t curbytes = (uint64_t)round(((double)cnt_all->curbytes)\n\t\t    * scale_factor);\n\t\tuint64_t curobjs = (uint64_t)round(((double)cnt_all->curobjs) *\n\t\t    scale_factor);\n\n\t\tmalloc_printf(\"<jemalloc>: Leak approximation summary: ~%\"FMTu64\n\t\t    \" byte%s, ~%\"FMTu64\" object%s, >= %zu context%s\\n\",\n\t\t    curbytes, (curbytes != 1) ? \"s\" : \"\", curobjs, (curobjs !=\n\t\t    1) ? \"s\" : \"\", leak_ngctx, (leak_ngctx != 1) ? \"s\" : \"\");\n\t\tmalloc_printf(\n\t\t    \"<jemalloc>: Run jeprof on \\\"%s\\\" for leak detail\\n\",\n\t\t    filename);\n\t}\n#endif\n}\n\nstruct prof_gctx_dump_iter_arg_s {\n\ttsdn_t\t*tsdn;\n\tbool\tpropagate_err;\n};\n\nstatic prof_gctx_t *\nprof_gctx_dump_iter(prof_gctx_tree_t *gctxs, prof_gctx_t *gctx, void *opaque) {\n\tprof_gctx_t *ret;\n\tstruct prof_gctx_dump_iter_arg_s *arg =\n\t    (struct prof_gctx_dump_iter_arg_s *)opaque;\n\n\tmalloc_mutex_lock(arg->tsdn, gctx->lock);\n\n\tif (prof_dump_gctx(arg->tsdn, arg->propagate_err, gctx, &gctx->bt,\n\t    gctxs)) {\n\t\tret = gctx;\n\t\tgoto label_return;\n\t}\n\n\tret = NULL;\nlabel_return:\n\tmalloc_mutex_unlock(arg->tsdn, gctx->lock);\n\treturn ret;\n}\n\nstatic void\nprof_dump_prep(tsd_t *tsd, prof_tdata_t *tdata,\n    struct prof_tdata_merge_iter_arg_s *prof_tdata_merge_iter_arg,\n    struct prof_gctx_merge_iter_arg_s *prof_gctx_merge_iter_arg,\n    prof_gctx_tree_t *gctxs) {\n\tsize_t tabind;\n\tunion {\n\t\tprof_gctx_t\t*p;\n\t\tvoid\t\t*v;\n\t} gctx;\n\n\tprof_enter(tsd, tdata);\n\n\t/*\n\t * Put gctx's in limbo and clear their counters in preparation for\n\t * summing.\n\t */\n\tgctx_tree_new(gctxs);\n\tfor (tabind = 0; !ckh_iter(&bt2gctx, &tabind, NULL, &gctx.v);) {\n\t\tprof_dump_gctx_prep(tsd_tsdn(tsd), gctx.p, gctxs);\n\t}\n\n\t/*\n\t * Iterate over tdatas, and for the non-expired ones snapshot their tctx\n\t * stats and merge them into the associated gctx's.\n\t */\n\tprof_tdata_merge_iter_arg->tsdn = tsd_tsdn(tsd);\n\tmemset(&prof_tdata_merge_iter_arg->cnt_all, 0, sizeof(prof_cnt_t));\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &tdatas_mtx);\n\ttdata_tree_iter(&tdatas, NULL, prof_tdata_merge_iter,\n\t    (void *)prof_tdata_merge_iter_arg);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &tdatas_mtx);\n\n\t/* Merge tctx stats into gctx's. */\n\tprof_gctx_merge_iter_arg->tsdn = tsd_tsdn(tsd);\n\tprof_gctx_merge_iter_arg->leak_ngctx = 0;\n\tgctx_tree_iter(gctxs, NULL, prof_gctx_merge_iter,\n\t    (void *)prof_gctx_merge_iter_arg);\n\n\tprof_leave(tsd, tdata);\n}\n\nstatic bool\nprof_dump_file(tsd_t *tsd, bool propagate_err, const char *filename,\n    bool leakcheck, prof_tdata_t *tdata,\n    struct prof_tdata_merge_iter_arg_s *prof_tdata_merge_iter_arg,\n    struct prof_gctx_merge_iter_arg_s *prof_gctx_merge_iter_arg,\n    struct prof_gctx_dump_iter_arg_s *prof_gctx_dump_iter_arg,\n    prof_gctx_tree_t *gctxs) {\n\t/* Create dump file. */\n\tif ((prof_dump_fd = prof_dump_open(propagate_err, filename)) == -1) {\n\t\treturn true;\n\t}\n\n\t/* Dump profile header. */\n\tif (prof_dump_header(tsd_tsdn(tsd), propagate_err,\n\t    &prof_tdata_merge_iter_arg->cnt_all)) {\n\t\tgoto label_write_error;\n\t}\n\n\t/* Dump per gctx profile stats. */\n\tprof_gctx_dump_iter_arg->tsdn = tsd_tsdn(tsd);\n\tprof_gctx_dump_iter_arg->propagate_err = propagate_err;\n\tif (gctx_tree_iter(gctxs, NULL, prof_gctx_dump_iter,\n\t    (void *)prof_gctx_dump_iter_arg) != NULL) {\n\t\tgoto label_write_error;\n\t}\n\n\t/* Dump /proc/<pid>/maps if possible. */\n\tif (prof_dump_maps(propagate_err)) {\n\t\tgoto label_write_error;\n\t}\n\n\tif (prof_dump_close(propagate_err)) {\n\t\treturn true;\n\t}\n\n\treturn false;\nlabel_write_error:\n\tprof_dump_close(propagate_err);\n\treturn true;\n}\n\nstatic bool\nprof_dump(tsd_t *tsd, bool propagate_err, const char *filename,\n    bool leakcheck) {\n\tcassert(config_prof);\n\tassert(tsd_reentrancy_level_get(tsd) == 0);\n\n\tprof_tdata_t * tdata = prof_tdata_get(tsd, true);\n\tif (tdata == NULL) {\n\t\treturn true;\n\t}\n\n\tpre_reentrancy(tsd, NULL);\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &prof_dump_mtx);\n\n\tprof_gctx_tree_t gctxs;\n\tstruct prof_tdata_merge_iter_arg_s prof_tdata_merge_iter_arg;\n\tstruct prof_gctx_merge_iter_arg_s prof_gctx_merge_iter_arg;\n\tstruct prof_gctx_dump_iter_arg_s prof_gctx_dump_iter_arg;\n\tprof_dump_prep(tsd, tdata, &prof_tdata_merge_iter_arg,\n\t    &prof_gctx_merge_iter_arg, &gctxs);\n\tbool err = prof_dump_file(tsd, propagate_err, filename, leakcheck, tdata,\n\t    &prof_tdata_merge_iter_arg, &prof_gctx_merge_iter_arg,\n\t    &prof_gctx_dump_iter_arg, &gctxs);\n\tprof_gctx_finish(tsd, &gctxs);\n\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &prof_dump_mtx);\n\tpost_reentrancy(tsd);\n\n\tif (err) {\n\t\treturn true;\n\t}\n\n\tif (leakcheck) {\n\t\tprof_leakcheck(&prof_tdata_merge_iter_arg.cnt_all,\n\t\t    prof_gctx_merge_iter_arg.leak_ngctx, filename);\n\t}\n\treturn false;\n}\n\n#ifdef JEMALLOC_JET\nvoid\nprof_cnt_all(uint64_t *curobjs, uint64_t *curbytes, uint64_t *accumobjs,\n    uint64_t *accumbytes) {\n\ttsd_t *tsd;\n\tprof_tdata_t *tdata;\n\tstruct prof_tdata_merge_iter_arg_s prof_tdata_merge_iter_arg;\n\tstruct prof_gctx_merge_iter_arg_s prof_gctx_merge_iter_arg;\n\tprof_gctx_tree_t gctxs;\n\n\ttsd = tsd_fetch();\n\ttdata = prof_tdata_get(tsd, false);\n\tif (tdata == NULL) {\n\t\tif (curobjs != NULL) {\n\t\t\t*curobjs = 0;\n\t\t}\n\t\tif (curbytes != NULL) {\n\t\t\t*curbytes = 0;\n\t\t}\n\t\tif (accumobjs != NULL) {\n\t\t\t*accumobjs = 0;\n\t\t}\n\t\tif (accumbytes != NULL) {\n\t\t\t*accumbytes = 0;\n\t\t}\n\t\treturn;\n\t}\n\n\tprof_dump_prep(tsd, tdata, &prof_tdata_merge_iter_arg,\n\t    &prof_gctx_merge_iter_arg, &gctxs);\n\tprof_gctx_finish(tsd, &gctxs);\n\n\tif (curobjs != NULL) {\n\t\t*curobjs = prof_tdata_merge_iter_arg.cnt_all.curobjs;\n\t}\n\tif (curbytes != NULL) {\n\t\t*curbytes = prof_tdata_merge_iter_arg.cnt_all.curbytes;\n\t}\n\tif (accumobjs != NULL) {\n\t\t*accumobjs = prof_tdata_merge_iter_arg.cnt_all.accumobjs;\n\t}\n\tif (accumbytes != NULL) {\n\t\t*accumbytes = prof_tdata_merge_iter_arg.cnt_all.accumbytes;\n\t}\n}\n#endif\n\n#define DUMP_FILENAME_BUFSIZE\t(PATH_MAX + 1)\n#define VSEQ_INVALID\t\tUINT64_C(0xffffffffffffffff)\nstatic void\nprof_dump_filename(char *filename, char v, uint64_t vseq) {\n\tcassert(config_prof);\n\n\tif (vseq != VSEQ_INVALID) {\n\t        /* \"<prefix>.<pid>.<seq>.v<vseq>.heap\" */\n\t\tmalloc_snprintf(filename, DUMP_FILENAME_BUFSIZE,\n\t\t    \"%s.%d.%\"FMTu64\".%c%\"FMTu64\".heap\",\n\t\t    opt_prof_prefix, prof_getpid(), prof_dump_seq, v, vseq);\n\t} else {\n\t        /* \"<prefix>.<pid>.<seq>.<v>.heap\" */\n\t\tmalloc_snprintf(filename, DUMP_FILENAME_BUFSIZE,\n\t\t    \"%s.%d.%\"FMTu64\".%c.heap\",\n\t\t    opt_prof_prefix, prof_getpid(), prof_dump_seq, v);\n\t}\n\tprof_dump_seq++;\n}\n\nstatic void\nprof_fdump(void) {\n\ttsd_t *tsd;\n\tchar filename[DUMP_FILENAME_BUFSIZE];\n\n\tcassert(config_prof);\n\tassert(opt_prof_final);\n\tassert(opt_prof_prefix[0] != '\\0');\n\n\tif (!prof_booted) {\n\t\treturn;\n\t}\n\ttsd = tsd_fetch();\n\tassert(tsd_reentrancy_level_get(tsd) == 0);\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &prof_dump_seq_mtx);\n\tprof_dump_filename(filename, 'f', VSEQ_INVALID);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &prof_dump_seq_mtx);\n\tprof_dump(tsd, false, filename, opt_prof_leak);\n}\n\nbool\nprof_accum_init(tsdn_t *tsdn, prof_accum_t *prof_accum) {\n\tcassert(config_prof);\n\n#ifndef JEMALLOC_ATOMIC_U64\n\tif (malloc_mutex_init(&prof_accum->mtx, \"prof_accum\",\n\t    WITNESS_RANK_PROF_ACCUM, malloc_mutex_rank_exclusive)) {\n\t\treturn true;\n\t}\n\tprof_accum->accumbytes = 0;\n#else\n\tatomic_store_u64(&prof_accum->accumbytes, 0, ATOMIC_RELAXED);\n#endif\n\treturn false;\n}\n\nvoid\nprof_idump(tsdn_t *tsdn) {\n\ttsd_t *tsd;\n\tprof_tdata_t *tdata;\n\n\tcassert(config_prof);\n\n\tif (!prof_booted || tsdn_null(tsdn) || !prof_active_get_unlocked()) {\n\t\treturn;\n\t}\n\ttsd = tsdn_tsd(tsdn);\n\tif (tsd_reentrancy_level_get(tsd) > 0) {\n\t\treturn;\n\t}\n\n\ttdata = prof_tdata_get(tsd, false);\n\tif (tdata == NULL) {\n\t\treturn;\n\t}\n\tif (tdata->enq) {\n\t\ttdata->enq_idump = true;\n\t\treturn;\n\t}\n\n\tif (opt_prof_prefix[0] != '\\0') {\n\t\tchar filename[PATH_MAX + 1];\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &prof_dump_seq_mtx);\n\t\tprof_dump_filename(filename, 'i', prof_dump_iseq);\n\t\tprof_dump_iseq++;\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &prof_dump_seq_mtx);\n\t\tprof_dump(tsd, false, filename, false);\n\t}\n}\n\nbool\nprof_mdump(tsd_t *tsd, const char *filename) {\n\tcassert(config_prof);\n\tassert(tsd_reentrancy_level_get(tsd) == 0);\n\n\tif (!opt_prof || !prof_booted) {\n\t\treturn true;\n\t}\n\tchar filename_buf[DUMP_FILENAME_BUFSIZE];\n\tif (filename == NULL) {\n\t\t/* No filename specified, so automatically generate one. */\n\t\tif (opt_prof_prefix[0] == '\\0') {\n\t\t\treturn true;\n\t\t}\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &prof_dump_seq_mtx);\n\t\tprof_dump_filename(filename_buf, 'm', prof_dump_mseq);\n\t\tprof_dump_mseq++;\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &prof_dump_seq_mtx);\n\t\tfilename = filename_buf;\n\t}\n\treturn prof_dump(tsd, true, filename, false);\n}\n\nvoid\nprof_gdump(tsdn_t *tsdn) {\n\ttsd_t *tsd;\n\tprof_tdata_t *tdata;\n\n\tcassert(config_prof);\n\n\tif (!prof_booted || tsdn_null(tsdn) || !prof_active_get_unlocked()) {\n\t\treturn;\n\t}\n\ttsd = tsdn_tsd(tsdn);\n\tif (tsd_reentrancy_level_get(tsd) > 0) {\n\t\treturn;\n\t}\n\n\ttdata = prof_tdata_get(tsd, false);\n\tif (tdata == NULL) {\n\t\treturn;\n\t}\n\tif (tdata->enq) {\n\t\ttdata->enq_gdump = true;\n\t\treturn;\n\t}\n\n\tif (opt_prof_prefix[0] != '\\0') {\n\t\tchar filename[DUMP_FILENAME_BUFSIZE];\n\t\tmalloc_mutex_lock(tsdn, &prof_dump_seq_mtx);\n\t\tprof_dump_filename(filename, 'u', prof_dump_useq);\n\t\tprof_dump_useq++;\n\t\tmalloc_mutex_unlock(tsdn, &prof_dump_seq_mtx);\n\t\tprof_dump(tsd, false, filename, false);\n\t}\n}\n\nstatic void\nprof_bt_hash(const void *key, size_t r_hash[2]) {\n\tprof_bt_t *bt = (prof_bt_t *)key;\n\n\tcassert(config_prof);\n\n\thash(bt->vec, bt->len * sizeof(void *), 0x94122f33U, r_hash);\n}\n\nstatic bool\nprof_bt_keycomp(const void *k1, const void *k2) {\n\tconst prof_bt_t *bt1 = (prof_bt_t *)k1;\n\tconst prof_bt_t *bt2 = (prof_bt_t *)k2;\n\n\tcassert(config_prof);\n\n\tif (bt1->len != bt2->len) {\n\t\treturn false;\n\t}\n\treturn (memcmp(bt1->vec, bt2->vec, bt1->len * sizeof(void *)) == 0);\n}\n\nstatic void\nprof_bt_node_hash(const void *key, size_t r_hash[2]) {\n\tconst prof_bt_node_t *bt_node = (prof_bt_node_t *)key;\n\tprof_bt_hash((void *)(&bt_node->bt), r_hash);\n}\n\nstatic bool\nprof_bt_node_keycomp(const void *k1, const void *k2) {\n\tconst prof_bt_node_t *bt_node1 = (prof_bt_node_t *)k1;\n\tconst prof_bt_node_t *bt_node2 = (prof_bt_node_t *)k2;\n\treturn prof_bt_keycomp((void *)(&bt_node1->bt),\n\t    (void *)(&bt_node2->bt));\n}\n\nstatic void\nprof_thr_node_hash(const void *key, size_t r_hash[2]) {\n\tconst prof_thr_node_t *thr_node = (prof_thr_node_t *)key;\n\thash(&thr_node->thr_uid, sizeof(uint64_t), 0x94122f35U, r_hash);\n}\n\nstatic bool\nprof_thr_node_keycomp(const void *k1, const void *k2) {\n\tconst prof_thr_node_t *thr_node1 = (prof_thr_node_t *)k1;\n\tconst prof_thr_node_t *thr_node2 = (prof_thr_node_t *)k2;\n\treturn thr_node1->thr_uid == thr_node2->thr_uid;\n}\n\nstatic uint64_t\nprof_thr_uid_alloc(tsdn_t *tsdn) {\n\tuint64_t thr_uid;\n\n\tmalloc_mutex_lock(tsdn, &next_thr_uid_mtx);\n\tthr_uid = next_thr_uid;\n\tnext_thr_uid++;\n\tmalloc_mutex_unlock(tsdn, &next_thr_uid_mtx);\n\n\treturn thr_uid;\n}\n\nstatic prof_tdata_t *\nprof_tdata_init_impl(tsd_t *tsd, uint64_t thr_uid, uint64_t thr_discrim,\n    char *thread_name, bool active) {\n\tprof_tdata_t *tdata;\n\n\tcassert(config_prof);\n\n\t/* Initialize an empty cache for this thread. */\n\ttdata = (prof_tdata_t *)iallocztm(tsd_tsdn(tsd), sizeof(prof_tdata_t),\n\t    sz_size2index(sizeof(prof_tdata_t)), false, NULL, true,\n\t    arena_get(TSDN_NULL, 0, true), true);\n\tif (tdata == NULL) {\n\t\treturn NULL;\n\t}\n\n\ttdata->lock = prof_tdata_mutex_choose(thr_uid);\n\ttdata->thr_uid = thr_uid;\n\ttdata->thr_discrim = thr_discrim;\n\ttdata->thread_name = thread_name;\n\ttdata->attached = true;\n\ttdata->expired = false;\n\ttdata->tctx_uid_next = 0;\n\n\tif (ckh_new(tsd, &tdata->bt2tctx, PROF_CKH_MINITEMS, prof_bt_hash,\n\t    prof_bt_keycomp)) {\n\t\tidalloctm(tsd_tsdn(tsd), tdata, NULL, NULL, true, true);\n\t\treturn NULL;\n\t}\n\n\ttdata->prng_state = (uint64_t)(uintptr_t)tdata;\n\tprof_sample_threshold_update(tdata);\n\n\ttdata->enq = false;\n\ttdata->enq_idump = false;\n\ttdata->enq_gdump = false;\n\n\ttdata->dumping = false;\n\ttdata->active = active;\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &tdatas_mtx);\n\ttdata_tree_insert(&tdatas, tdata);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &tdatas_mtx);\n\n\treturn tdata;\n}\n\nprof_tdata_t *\nprof_tdata_init(tsd_t *tsd) {\n\treturn prof_tdata_init_impl(tsd, prof_thr_uid_alloc(tsd_tsdn(tsd)), 0,\n\t    NULL, prof_thread_active_init_get(tsd_tsdn(tsd)));\n}\n\nstatic bool\nprof_tdata_should_destroy_unlocked(prof_tdata_t *tdata, bool even_if_attached) {\n\tif (tdata->attached && !even_if_attached) {\n\t\treturn false;\n\t}\n\tif (ckh_count(&tdata->bt2tctx) != 0) {\n\t\treturn false;\n\t}\n\treturn true;\n}\n\nstatic bool\nprof_tdata_should_destroy(tsdn_t *tsdn, prof_tdata_t *tdata,\n    bool even_if_attached) {\n\tmalloc_mutex_assert_owner(tsdn, tdata->lock);\n\n\treturn prof_tdata_should_destroy_unlocked(tdata, even_if_attached);\n}\n\nstatic void\nprof_tdata_destroy_locked(tsd_t *tsd, prof_tdata_t *tdata,\n    bool even_if_attached) {\n\tmalloc_mutex_assert_owner(tsd_tsdn(tsd), &tdatas_mtx);\n\n\ttdata_tree_remove(&tdatas, tdata);\n\n\tassert(prof_tdata_should_destroy_unlocked(tdata, even_if_attached));\n\n\tif (tdata->thread_name != NULL) {\n\t\tidalloctm(tsd_tsdn(tsd), tdata->thread_name, NULL, NULL, true,\n\t\t    true);\n\t}\n\tckh_delete(tsd, &tdata->bt2tctx);\n\tidalloctm(tsd_tsdn(tsd), tdata, NULL, NULL, true, true);\n}\n\nstatic void\nprof_tdata_destroy(tsd_t *tsd, prof_tdata_t *tdata, bool even_if_attached) {\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &tdatas_mtx);\n\tprof_tdata_destroy_locked(tsd, tdata, even_if_attached);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &tdatas_mtx);\n}\n\nstatic void\nprof_tdata_detach(tsd_t *tsd, prof_tdata_t *tdata) {\n\tbool destroy_tdata;\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), tdata->lock);\n\tif (tdata->attached) {\n\t\tdestroy_tdata = prof_tdata_should_destroy(tsd_tsdn(tsd), tdata,\n\t\t    true);\n\t\t/*\n\t\t * Only detach if !destroy_tdata, because detaching would allow\n\t\t * another thread to win the race to destroy tdata.\n\t\t */\n\t\tif (!destroy_tdata) {\n\t\t\ttdata->attached = false;\n\t\t}\n\t\ttsd_prof_tdata_set(tsd, NULL);\n\t} else {\n\t\tdestroy_tdata = false;\n\t}\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), tdata->lock);\n\tif (destroy_tdata) {\n\t\tprof_tdata_destroy(tsd, tdata, true);\n\t}\n}\n\nprof_tdata_t *\nprof_tdata_reinit(tsd_t *tsd, prof_tdata_t *tdata) {\n\tuint64_t thr_uid = tdata->thr_uid;\n\tuint64_t thr_discrim = tdata->thr_discrim + 1;\n\tchar *thread_name = (tdata->thread_name != NULL) ?\n\t    prof_thread_name_alloc(tsd_tsdn(tsd), tdata->thread_name) : NULL;\n\tbool active = tdata->active;\n\n\tprof_tdata_detach(tsd, tdata);\n\treturn prof_tdata_init_impl(tsd, thr_uid, thr_discrim, thread_name,\n\t    active);\n}\n\nstatic bool\nprof_tdata_expire(tsdn_t *tsdn, prof_tdata_t *tdata) {\n\tbool destroy_tdata;\n\n\tmalloc_mutex_lock(tsdn, tdata->lock);\n\tif (!tdata->expired) {\n\t\ttdata->expired = true;\n\t\tdestroy_tdata = tdata->attached ? false :\n\t\t    prof_tdata_should_destroy(tsdn, tdata, false);\n\t} else {\n\t\tdestroy_tdata = false;\n\t}\n\tmalloc_mutex_unlock(tsdn, tdata->lock);\n\n\treturn destroy_tdata;\n}\n\nstatic prof_tdata_t *\nprof_tdata_reset_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata,\n    void *arg) {\n\ttsdn_t *tsdn = (tsdn_t *)arg;\n\n\treturn (prof_tdata_expire(tsdn, tdata) ? tdata : NULL);\n}\n\nvoid\nprof_reset(tsd_t *tsd, size_t lg_sample) {\n\tprof_tdata_t *next;\n\n\tassert(lg_sample < (sizeof(uint64_t) << 3));\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &prof_dump_mtx);\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &tdatas_mtx);\n\n\tlg_prof_sample = lg_sample;\n\n\tnext = NULL;\n\tdo {\n\t\tprof_tdata_t *to_destroy = tdata_tree_iter(&tdatas, next,\n\t\t    prof_tdata_reset_iter, (void *)tsd);\n\t\tif (to_destroy != NULL) {\n\t\t\tnext = tdata_tree_next(&tdatas, to_destroy);\n\t\t\tprof_tdata_destroy_locked(tsd, to_destroy, false);\n\t\t} else {\n\t\t\tnext = NULL;\n\t\t}\n\t} while (next != NULL);\n\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &tdatas_mtx);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &prof_dump_mtx);\n}\n\nvoid\nprof_tdata_cleanup(tsd_t *tsd) {\n\tprof_tdata_t *tdata;\n\n\tif (!config_prof) {\n\t\treturn;\n\t}\n\n\ttdata = tsd_prof_tdata_get(tsd);\n\tif (tdata != NULL) {\n\t\tprof_tdata_detach(tsd, tdata);\n\t}\n}\n\nbool\nprof_active_get(tsdn_t *tsdn) {\n\tbool prof_active_current;\n\n\tmalloc_mutex_lock(tsdn, &prof_active_mtx);\n\tprof_active_current = prof_active;\n\tmalloc_mutex_unlock(tsdn, &prof_active_mtx);\n\treturn prof_active_current;\n}\n\nbool\nprof_active_set(tsdn_t *tsdn, bool active) {\n\tbool prof_active_old;\n\n\tmalloc_mutex_lock(tsdn, &prof_active_mtx);\n\tprof_active_old = prof_active;\n\tprof_active = active;\n\tmalloc_mutex_unlock(tsdn, &prof_active_mtx);\n\treturn prof_active_old;\n}\n\n#ifdef JEMALLOC_JET\nsize_t\nprof_log_bt_count(void) {\n\tsize_t cnt = 0;\n\tprof_bt_node_t *node = log_bt_first;\n\twhile (node != NULL) {\n\t\tcnt++;\n\t\tnode = node->next;\n\t}\n\treturn cnt;\n}\n\nsize_t\nprof_log_alloc_count(void) {\n\tsize_t cnt = 0;\n\tprof_alloc_node_t *node = log_alloc_first;\n\twhile (node != NULL) {\n\t\tcnt++;\n\t\tnode = node->next;\n\t}\n\treturn cnt;\n}\n\nsize_t\nprof_log_thr_count(void) {\n\tsize_t cnt = 0;\n\tprof_thr_node_t *node = log_thr_first;\n\twhile (node != NULL) {\n\t\tcnt++;\n\t\tnode = node->next;\n\t}\n\treturn cnt;\n}\n\nbool\nprof_log_is_logging(void) {\n\treturn prof_logging_state == prof_logging_state_started;\n}\n\nbool\nprof_log_rep_check(void) {\n\tif (prof_logging_state == prof_logging_state_stopped\n\t    && log_tables_initialized) {\n\t\treturn true;\n\t}\n\n\tif (log_bt_last != NULL && log_bt_last->next != NULL) {\n\t\treturn true;\n\t}\n\tif (log_thr_last != NULL && log_thr_last->next != NULL) {\n\t\treturn true;\n\t}\n\tif (log_alloc_last != NULL && log_alloc_last->next != NULL) {\n\t\treturn true;\n\t}\n\n\tsize_t bt_count = prof_log_bt_count();\n\tsize_t thr_count = prof_log_thr_count();\n\tsize_t alloc_count = prof_log_alloc_count();\n\n\n\tif (prof_logging_state == prof_logging_state_stopped) {\n\t\tif (bt_count != 0 || thr_count != 0 || alloc_count || 0) {\n\t\t\treturn true;\n\t\t}\n\t}\n\n\tprof_alloc_node_t *node = log_alloc_first;\n\twhile (node != NULL) {\n\t\tif (node->alloc_bt_ind >= bt_count) {\n\t\t\treturn true;\n\t\t}\n\t\tif (node->free_bt_ind >= bt_count) {\n\t\t\treturn true;\n\t\t}\n\t\tif (node->alloc_thr_ind >= thr_count) {\n\t\t\treturn true;\n\t\t}\n\t\tif (node->free_thr_ind >= thr_count) {\n\t\t\treturn true;\n\t\t}\n\t\tif (node->alloc_time_ns > node->free_time_ns) {\n\t\t\treturn true;\n\t\t}\n\t\tnode = node->next;\n\t}\n\n\treturn false;\n}\n\nvoid\nprof_log_dummy_set(bool new_value) {\n\tprof_log_dummy = new_value;\n}\n#endif\n\nbool\nprof_log_start(tsdn_t *tsdn, const char *filename) {\n\tif (!opt_prof || !prof_booted) {\n\t\treturn true;\n\t}\n\n\tbool ret = false;\n\tsize_t buf_size = PATH_MAX + 1;\n\n\tmalloc_mutex_lock(tsdn, &log_mtx);\n\n\tif (prof_logging_state != prof_logging_state_stopped) {\n\t\tret = true;\n\t} else if (filename == NULL) {\n\t\t/* Make default name. */\n\t\tmalloc_snprintf(log_filename, buf_size, \"%s.%d.%\"FMTu64\".json\",\n\t\t    opt_prof_prefix, prof_getpid(), log_seq);\n\t\tlog_seq++;\n\t\tprof_logging_state = prof_logging_state_started;\n\t} else if (strlen(filename) >= buf_size) {\n\t\tret = true;\n\t} else {\n\t\tstrcpy(log_filename, filename);\n\t\tprof_logging_state = prof_logging_state_started;\n\t}\n\n\tif (!ret) {\n\t\tnstime_update(&log_start_timestamp);\n\t}\n\n\tmalloc_mutex_unlock(tsdn, &log_mtx);\n\n\treturn ret;\n}\n\n/* Used as an atexit function to stop logging on exit. */\nstatic void\nprof_log_stop_final(void) {\n\ttsd_t *tsd = tsd_fetch();\n\tprof_log_stop(tsd_tsdn(tsd));\n}\n\nstruct prof_emitter_cb_arg_s {\n\tint fd;\n\tssize_t ret;\n};\n\nstatic void\nprof_emitter_write_cb(void *opaque, const char *to_write) {\n\tstruct prof_emitter_cb_arg_s *arg =\n\t    (struct prof_emitter_cb_arg_s *)opaque;\n\tsize_t bytes = strlen(to_write);\n#ifdef JEMALLOC_JET\n\tif (prof_log_dummy) {\n\t\treturn;\n\t}\n#endif\n\targ->ret = write(arg->fd, (void *)to_write, bytes);\n}\n\n/*\n * prof_log_emit_{...} goes through the appropriate linked list, emitting each\n * node to the json and deallocating it.\n */\nstatic void\nprof_log_emit_threads(tsd_t *tsd, emitter_t *emitter) {\n\temitter_json_array_kv_begin(emitter, \"threads\");\n\tprof_thr_node_t *thr_node = log_thr_first;\n\tprof_thr_node_t *thr_old_node;\n\twhile (thr_node != NULL) {\n\t\temitter_json_object_begin(emitter);\n\n\t\temitter_json_kv(emitter, \"thr_uid\", emitter_type_uint64,\n\t\t    &thr_node->thr_uid);\n\n\t\tchar *thr_name = thr_node->name;\n\n\t\temitter_json_kv(emitter, \"thr_name\", emitter_type_string,\n\t\t    &thr_name);\n\n\t\temitter_json_object_end(emitter);\n\t\tthr_old_node = thr_node;\n\t\tthr_node = thr_node->next;\n\t\tidalloc(tsd, thr_old_node);\n\t}\n\temitter_json_array_end(emitter);\n}\n\nstatic void\nprof_log_emit_traces(tsd_t *tsd, emitter_t *emitter) {\n\temitter_json_array_kv_begin(emitter, \"stack_traces\");\n\tprof_bt_node_t *bt_node = log_bt_first;\n\tprof_bt_node_t *bt_old_node;\n\t/*\n\t * Calculate how many hex digits we need: twice number of bytes, two for\n\t * \"0x\", and then one more for terminating '\\0'.\n\t */\n\tchar buf[2 * sizeof(intptr_t) + 3];\n\tsize_t buf_sz = sizeof(buf);\n\twhile (bt_node != NULL) {\n\t\temitter_json_array_begin(emitter);\n\t\tsize_t i;\n\t\tfor (i = 0; i < bt_node->bt.len; i++) {\n\t\t\tmalloc_snprintf(buf, buf_sz, \"%p\", bt_node->bt.vec[i]);\n\t\t\tchar *trace_str = buf;\n\t\t\temitter_json_value(emitter, emitter_type_string,\n\t\t\t    &trace_str);\n\t\t}\n\t\temitter_json_array_end(emitter);\n\n\t\tbt_old_node = bt_node;\n\t\tbt_node = bt_node->next;\n\t\tidalloc(tsd, bt_old_node);\n\t}\n\temitter_json_array_end(emitter);\n}\n\nstatic void\nprof_log_emit_allocs(tsd_t *tsd, emitter_t *emitter) {\n\temitter_json_array_kv_begin(emitter, \"allocations\");\n\tprof_alloc_node_t *alloc_node = log_alloc_first;\n\tprof_alloc_node_t *alloc_old_node;\n\twhile (alloc_node != NULL) {\n\t\temitter_json_object_begin(emitter);\n\n\t\temitter_json_kv(emitter, \"alloc_thread\", emitter_type_size,\n\t\t    &alloc_node->alloc_thr_ind);\n\n\t\temitter_json_kv(emitter, \"free_thread\", emitter_type_size,\n\t\t    &alloc_node->free_thr_ind);\n\n\t\temitter_json_kv(emitter, \"alloc_trace\", emitter_type_size,\n\t\t    &alloc_node->alloc_bt_ind);\n\n\t\temitter_json_kv(emitter, \"free_trace\", emitter_type_size,\n\t\t    &alloc_node->free_bt_ind);\n\n\t\temitter_json_kv(emitter, \"alloc_timestamp\",\n\t\t    emitter_type_uint64, &alloc_node->alloc_time_ns);\n\n\t\temitter_json_kv(emitter, \"free_timestamp\", emitter_type_uint64,\n\t\t    &alloc_node->free_time_ns);\n\n\t\temitter_json_kv(emitter, \"usize\", emitter_type_uint64,\n\t\t    &alloc_node->usize);\n\n\t\temitter_json_object_end(emitter);\n\n\t\talloc_old_node = alloc_node;\n\t\talloc_node = alloc_node->next;\n\t\tidalloc(tsd, alloc_old_node);\n\t}\n\temitter_json_array_end(emitter);\n}\n\nstatic void\nprof_log_emit_metadata(emitter_t *emitter) {\n\temitter_json_object_kv_begin(emitter, \"info\");\n\n\tnstime_t now = NSTIME_ZERO_INITIALIZER;\n\n\tnstime_update(&now);\n\tuint64_t ns = nstime_ns(&now) - nstime_ns(&log_start_timestamp);\n\temitter_json_kv(emitter, \"duration\", emitter_type_uint64, &ns);\n\n\tchar *vers = JEMALLOC_VERSION;\n\temitter_json_kv(emitter, \"version\",\n\t    emitter_type_string, &vers);\n\n\temitter_json_kv(emitter, \"lg_sample_rate\",\n\t    emitter_type_int, &lg_prof_sample);\n\n\tint pid = prof_getpid();\n\temitter_json_kv(emitter, \"pid\", emitter_type_int, &pid);\n\n\temitter_json_object_end(emitter);\n}\n\n\nbool\nprof_log_stop(tsdn_t *tsdn) {\n\tif (!opt_prof || !prof_booted) {\n\t\treturn true;\n\t}\n\n\ttsd_t *tsd = tsdn_tsd(tsdn);\n\tmalloc_mutex_lock(tsdn, &log_mtx);\n\n\tif (prof_logging_state != prof_logging_state_started) {\n\t\tmalloc_mutex_unlock(tsdn, &log_mtx);\n\t\treturn true;\n\t}\n\n\t/*\n\t * Set the state to dumping. We'll set it to stopped when we're done.\n\t * Since other threads won't be able to start/stop/log when the state is\n\t * dumping, we don't have to hold the lock during the whole method.\n\t */\n\tprof_logging_state = prof_logging_state_dumping;\n\tmalloc_mutex_unlock(tsdn, &log_mtx);\n\n\n\temitter_t emitter;\n\n\t/* Create a file. */\n\n\tint fd;\n#ifdef JEMALLOC_JET\n\tif (prof_log_dummy) {\n\t\tfd = 0;\n\t} else {\n\t\tfd = creat(log_filename, 0644);\n\t}\n#else\n\tfd = creat(log_filename, 0644);\n#endif\n\n\tif (fd == -1) {\n\t\tmalloc_printf(\"<jemalloc>: creat() for log file \\\"%s\\\" \"\n\t\t\t      \" failed with %d\\n\", log_filename, errno);\n\t\tif (opt_abort) {\n\t\t\tabort();\n\t\t}\n\t\treturn true;\n\t}\n\n\t/* Emit to json. */\n\tstruct prof_emitter_cb_arg_s arg;\n\targ.fd = fd;\n\temitter_init(&emitter, emitter_output_json, &prof_emitter_write_cb,\n\t    (void *)(&arg));\n\n\temitter_begin(&emitter);\n\tprof_log_emit_metadata(&emitter);\n\tprof_log_emit_threads(tsd, &emitter);\n\tprof_log_emit_traces(tsd, &emitter);\n\tprof_log_emit_allocs(tsd, &emitter);\n\temitter_end(&emitter);\n\n\t/* Reset global state. */\n\tif (log_tables_initialized) {\n\t\tckh_delete(tsd, &log_bt_node_set);\n\t\tckh_delete(tsd, &log_thr_node_set);\n\t}\n\tlog_tables_initialized = false;\n\tlog_bt_index = 0;\n\tlog_thr_index = 0;\n\tlog_bt_first = NULL;\n\tlog_bt_last = NULL;\n\tlog_thr_first = NULL;\n\tlog_thr_last = NULL;\n\tlog_alloc_first = NULL;\n\tlog_alloc_last = NULL;\n\n\tmalloc_mutex_lock(tsdn, &log_mtx);\n\tprof_logging_state = prof_logging_state_stopped;\n\tmalloc_mutex_unlock(tsdn, &log_mtx);\n\n#ifdef JEMALLOC_JET\n\tif (prof_log_dummy) {\n\t\treturn false;\n\t}\n#endif\n\treturn close(fd);\n}\n\nconst char *\nprof_thread_name_get(tsd_t *tsd) {\n\tprof_tdata_t *tdata;\n\n\ttdata = prof_tdata_get(tsd, true);\n\tif (tdata == NULL) {\n\t\treturn \"\";\n\t}\n\treturn (tdata->thread_name != NULL ? tdata->thread_name : \"\");\n}\n\nstatic char *\nprof_thread_name_alloc(tsdn_t *tsdn, const char *thread_name) {\n\tchar *ret;\n\tsize_t size;\n\n\tif (thread_name == NULL) {\n\t\treturn NULL;\n\t}\n\n\tsize = strlen(thread_name) + 1;\n\tif (size == 1) {\n\t\treturn \"\";\n\t}\n\n\tret = iallocztm(tsdn, size, sz_size2index(size), false, NULL, true,\n\t    arena_get(TSDN_NULL, 0, true), true);\n\tif (ret == NULL) {\n\t\treturn NULL;\n\t}\n\tmemcpy(ret, thread_name, size);\n\treturn ret;\n}\n\nint\nprof_thread_name_set(tsd_t *tsd, const char *thread_name) {\n\tprof_tdata_t *tdata;\n\tunsigned i;\n\tchar *s;\n\n\ttdata = prof_tdata_get(tsd, true);\n\tif (tdata == NULL) {\n\t\treturn EAGAIN;\n\t}\n\n\t/* Validate input. */\n\tif (thread_name == NULL) {\n\t\treturn EFAULT;\n\t}\n\tfor (i = 0; thread_name[i] != '\\0'; i++) {\n\t\tchar c = thread_name[i];\n\t\tif (!isgraph(c) && !isblank(c)) {\n\t\t\treturn EFAULT;\n\t\t}\n\t}\n\n\ts = prof_thread_name_alloc(tsd_tsdn(tsd), thread_name);\n\tif (s == NULL) {\n\t\treturn EAGAIN;\n\t}\n\n\tif (tdata->thread_name != NULL) {\n\t\tidalloctm(tsd_tsdn(tsd), tdata->thread_name, NULL, NULL, true,\n\t\t    true);\n\t\ttdata->thread_name = NULL;\n\t}\n\tif (strlen(s) > 0) {\n\t\ttdata->thread_name = s;\n\t}\n\treturn 0;\n}\n\nbool\nprof_thread_active_get(tsd_t *tsd) {\n\tprof_tdata_t *tdata;\n\n\ttdata = prof_tdata_get(tsd, true);\n\tif (tdata == NULL) {\n\t\treturn false;\n\t}\n\treturn tdata->active;\n}\n\nbool\nprof_thread_active_set(tsd_t *tsd, bool active) {\n\tprof_tdata_t *tdata;\n\n\ttdata = prof_tdata_get(tsd, true);\n\tif (tdata == NULL) {\n\t\treturn true;\n\t}\n\ttdata->active = active;\n\treturn false;\n}\n\nbool\nprof_thread_active_init_get(tsdn_t *tsdn) {\n\tbool active_init;\n\n\tmalloc_mutex_lock(tsdn, &prof_thread_active_init_mtx);\n\tactive_init = prof_thread_active_init;\n\tmalloc_mutex_unlock(tsdn, &prof_thread_active_init_mtx);\n\treturn active_init;\n}\n\nbool\nprof_thread_active_init_set(tsdn_t *tsdn, bool active_init) {\n\tbool active_init_old;\n\n\tmalloc_mutex_lock(tsdn, &prof_thread_active_init_mtx);\n\tactive_init_old = prof_thread_active_init;\n\tprof_thread_active_init = active_init;\n\tmalloc_mutex_unlock(tsdn, &prof_thread_active_init_mtx);\n\treturn active_init_old;\n}\n\nbool\nprof_gdump_get(tsdn_t *tsdn) {\n\tbool prof_gdump_current;\n\n\tmalloc_mutex_lock(tsdn, &prof_gdump_mtx);\n\tprof_gdump_current = prof_gdump_val;\n\tmalloc_mutex_unlock(tsdn, &prof_gdump_mtx);\n\treturn prof_gdump_current;\n}\n\nbool\nprof_gdump_set(tsdn_t *tsdn, bool gdump) {\n\tbool prof_gdump_old;\n\n\tmalloc_mutex_lock(tsdn, &prof_gdump_mtx);\n\tprof_gdump_old = prof_gdump_val;\n\tprof_gdump_val = gdump;\n\tmalloc_mutex_unlock(tsdn, &prof_gdump_mtx);\n\treturn prof_gdump_old;\n}\n\nvoid\nprof_boot0(void) {\n\tcassert(config_prof);\n\n\tmemcpy(opt_prof_prefix, PROF_PREFIX_DEFAULT,\n\t    sizeof(PROF_PREFIX_DEFAULT));\n}\n\nvoid\nprof_boot1(void) {\n\tcassert(config_prof);\n\n\t/*\n\t * opt_prof must be in its final state before any arenas are\n\t * initialized, so this function must be executed early.\n\t */\n\n\tif (opt_prof_leak && !opt_prof) {\n\t\t/*\n\t\t * Enable opt_prof, but in such a way that profiles are never\n\t\t * automatically dumped.\n\t\t */\n\t\topt_prof = true;\n\t\topt_prof_gdump = false;\n\t} else if (opt_prof) {\n\t\tif (opt_lg_prof_interval >= 0) {\n\t\t\tprof_interval = (((uint64_t)1U) <<\n\t\t\t    opt_lg_prof_interval);\n\t\t}\n\t}\n}\n\nbool\nprof_boot2(tsd_t *tsd) {\n\tcassert(config_prof);\n\n\tif (opt_prof) {\n\t\tunsigned i;\n\n\t\tlg_prof_sample = opt_lg_prof_sample;\n\n\t\tprof_active = opt_prof_active;\n\t\tif (malloc_mutex_init(&prof_active_mtx, \"prof_active\",\n\t\t    WITNESS_RANK_PROF_ACTIVE, malloc_mutex_rank_exclusive)) {\n\t\t\treturn true;\n\t\t}\n\n\t\tprof_gdump_val = opt_prof_gdump;\n\t\tif (malloc_mutex_init(&prof_gdump_mtx, \"prof_gdump\",\n\t\t    WITNESS_RANK_PROF_GDUMP, malloc_mutex_rank_exclusive)) {\n\t\t\treturn true;\n\t\t}\n\n\t\tprof_thread_active_init = opt_prof_thread_active_init;\n\t\tif (malloc_mutex_init(&prof_thread_active_init_mtx,\n\t\t    \"prof_thread_active_init\",\n\t\t    WITNESS_RANK_PROF_THREAD_ACTIVE_INIT,\n\t\t    malloc_mutex_rank_exclusive)) {\n\t\t\treturn true;\n\t\t}\n\n\t\tif (ckh_new(tsd, &bt2gctx, PROF_CKH_MINITEMS, prof_bt_hash,\n\t\t    prof_bt_keycomp)) {\n\t\t\treturn true;\n\t\t}\n\t\tif (malloc_mutex_init(&bt2gctx_mtx, \"prof_bt2gctx\",\n\t\t    WITNESS_RANK_PROF_BT2GCTX, malloc_mutex_rank_exclusive)) {\n\t\t\treturn true;\n\t\t}\n\n\t\ttdata_tree_new(&tdatas);\n\t\tif (malloc_mutex_init(&tdatas_mtx, \"prof_tdatas\",\n\t\t    WITNESS_RANK_PROF_TDATAS, malloc_mutex_rank_exclusive)) {\n\t\t\treturn true;\n\t\t}\n\n\t\tnext_thr_uid = 0;\n\t\tif (malloc_mutex_init(&next_thr_uid_mtx, \"prof_next_thr_uid\",\n\t\t    WITNESS_RANK_PROF_NEXT_THR_UID, malloc_mutex_rank_exclusive)) {\n\t\t\treturn true;\n\t\t}\n\n\t\tif (malloc_mutex_init(&prof_dump_seq_mtx, \"prof_dump_seq\",\n\t\t    WITNESS_RANK_PROF_DUMP_SEQ, malloc_mutex_rank_exclusive)) {\n\t\t\treturn true;\n\t\t}\n\t\tif (malloc_mutex_init(&prof_dump_mtx, \"prof_dump\",\n\t\t    WITNESS_RANK_PROF_DUMP, malloc_mutex_rank_exclusive)) {\n\t\t\treturn true;\n\t\t}\n\n\t\tif (opt_prof_final && opt_prof_prefix[0] != '\\0' &&\n\t\t    atexit(prof_fdump) != 0) {\n\t\t\tmalloc_write(\"<jemalloc>: Error in atexit()\\n\");\n\t\t\tif (opt_abort) {\n\t\t\t\tabort();\n\t\t\t}\n\t\t}\n\n\t\tif (opt_prof_log) {\n\t\t\tprof_log_start(tsd_tsdn(tsd), NULL);\n\t\t}\n\n\t\tif (atexit(prof_log_stop_final) != 0) {\n\t\t\tmalloc_write(\"<jemalloc>: Error in atexit() \"\n\t\t\t\t     \"for logging\\n\");\n\t\t\tif (opt_abort) {\n\t\t\t\tabort();\n\t\t\t}\n\t\t}\n\n\t\tif (malloc_mutex_init(&log_mtx, \"prof_log\",\n\t\t    WITNESS_RANK_PROF_LOG, malloc_mutex_rank_exclusive)) {\n\t\t\treturn true;\n\t\t}\n\n\t\tif (ckh_new(tsd, &log_bt_node_set, PROF_CKH_MINITEMS,\n\t\t    prof_bt_node_hash, prof_bt_node_keycomp)) {\n\t\t\treturn true;\n\t\t}\n\n\t\tif (ckh_new(tsd, &log_thr_node_set, PROF_CKH_MINITEMS,\n\t\t    prof_thr_node_hash, prof_thr_node_keycomp)) {\n\t\t\treturn true;\n\t\t}\n\n\t\tlog_tables_initialized = true;\n\n\t\tgctx_locks = (malloc_mutex_t *)base_alloc(tsd_tsdn(tsd),\n\t\t    b0get(), PROF_NCTX_LOCKS * sizeof(malloc_mutex_t),\n\t\t    CACHELINE);\n\t\tif (gctx_locks == NULL) {\n\t\t\treturn true;\n\t\t}\n\t\tfor (i = 0; i < PROF_NCTX_LOCKS; i++) {\n\t\t\tif (malloc_mutex_init(&gctx_locks[i], \"prof_gctx\",\n\t\t\t    WITNESS_RANK_PROF_GCTX,\n\t\t\t    malloc_mutex_rank_exclusive)) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\n\t\ttdata_locks = (malloc_mutex_t *)base_alloc(tsd_tsdn(tsd),\n\t\t    b0get(), PROF_NTDATA_LOCKS * sizeof(malloc_mutex_t),\n\t\t    CACHELINE);\n\t\tif (tdata_locks == NULL) {\n\t\t\treturn true;\n\t\t}\n\t\tfor (i = 0; i < PROF_NTDATA_LOCKS; i++) {\n\t\t\tif (malloc_mutex_init(&tdata_locks[i], \"prof_tdata\",\n\t\t\t    WITNESS_RANK_PROF_TDATA,\n\t\t\t    malloc_mutex_rank_exclusive)) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n#ifdef JEMALLOC_PROF_LIBGCC\n\t\t/*\n\t\t * Cause the backtracing machinery to allocate its internal\n\t\t * state before enabling profiling.\n\t\t */\n\t\t_Unwind_Backtrace(prof_unwind_init_callback, NULL);\n#endif\n\t}\n\tprof_booted = true;\n\n\treturn false;\n}\n\nvoid\nprof_prefork0(tsdn_t *tsdn) {\n\tif (config_prof && opt_prof) {\n\t\tunsigned i;\n\n\t\tmalloc_mutex_prefork(tsdn, &prof_dump_mtx);\n\t\tmalloc_mutex_prefork(tsdn, &bt2gctx_mtx);\n\t\tmalloc_mutex_prefork(tsdn, &tdatas_mtx);\n\t\tfor (i = 0; i < PROF_NTDATA_LOCKS; i++) {\n\t\t\tmalloc_mutex_prefork(tsdn, &tdata_locks[i]);\n\t\t}\n\t\tfor (i = 0; i < PROF_NCTX_LOCKS; i++) {\n\t\t\tmalloc_mutex_prefork(tsdn, &gctx_locks[i]);\n\t\t}\n\t}\n}\n\nvoid\nprof_prefork1(tsdn_t *tsdn) {\n\tif (config_prof && opt_prof) {\n\t\tmalloc_mutex_prefork(tsdn, &prof_active_mtx);\n\t\tmalloc_mutex_prefork(tsdn, &prof_dump_seq_mtx);\n\t\tmalloc_mutex_prefork(tsdn, &prof_gdump_mtx);\n\t\tmalloc_mutex_prefork(tsdn, &next_thr_uid_mtx);\n\t\tmalloc_mutex_prefork(tsdn, &prof_thread_active_init_mtx);\n\t}\n}\n\nvoid\nprof_postfork_parent(tsdn_t *tsdn) {\n\tif (config_prof && opt_prof) {\n\t\tunsigned i;\n\n\t\tmalloc_mutex_postfork_parent(tsdn,\n\t\t    &prof_thread_active_init_mtx);\n\t\tmalloc_mutex_postfork_parent(tsdn, &next_thr_uid_mtx);\n\t\tmalloc_mutex_postfork_parent(tsdn, &prof_gdump_mtx);\n\t\tmalloc_mutex_postfork_parent(tsdn, &prof_dump_seq_mtx);\n\t\tmalloc_mutex_postfork_parent(tsdn, &prof_active_mtx);\n\t\tfor (i = 0; i < PROF_NCTX_LOCKS; i++) {\n\t\t\tmalloc_mutex_postfork_parent(tsdn, &gctx_locks[i]);\n\t\t}\n\t\tfor (i = 0; i < PROF_NTDATA_LOCKS; i++) {\n\t\t\tmalloc_mutex_postfork_parent(tsdn, &tdata_locks[i]);\n\t\t}\n\t\tmalloc_mutex_postfork_parent(tsdn, &tdatas_mtx);\n\t\tmalloc_mutex_postfork_parent(tsdn, &bt2gctx_mtx);\n\t\tmalloc_mutex_postfork_parent(tsdn, &prof_dump_mtx);\n\t}\n}\n\nvoid\nprof_postfork_child(tsdn_t *tsdn) {\n\tif (config_prof && opt_prof) {\n\t\tunsigned i;\n\n\t\tmalloc_mutex_postfork_child(tsdn, &prof_thread_active_init_mtx);\n\t\tmalloc_mutex_postfork_child(tsdn, &next_thr_uid_mtx);\n\t\tmalloc_mutex_postfork_child(tsdn, &prof_gdump_mtx);\n\t\tmalloc_mutex_postfork_child(tsdn, &prof_dump_seq_mtx);\n\t\tmalloc_mutex_postfork_child(tsdn, &prof_active_mtx);\n\t\tfor (i = 0; i < PROF_NCTX_LOCKS; i++) {\n\t\t\tmalloc_mutex_postfork_child(tsdn, &gctx_locks[i]);\n\t\t}\n\t\tfor (i = 0; i < PROF_NTDATA_LOCKS; i++) {\n\t\t\tmalloc_mutex_postfork_child(tsdn, &tdata_locks[i]);\n\t\t}\n\t\tmalloc_mutex_postfork_child(tsdn, &tdatas_mtx);\n\t\tmalloc_mutex_postfork_child(tsdn, &bt2gctx_mtx);\n\t\tmalloc_mutex_postfork_child(tsdn, &prof_dump_mtx);\n\t}\n}\n\n/******************************************************************************/\n"
  },
  {
    "path": "deps/jemalloc/src/rtree.c",
    "content": "#define JEMALLOC_RTREE_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/mutex.h\"\n\n/*\n * Only the most significant bits of keys passed to rtree_{read,write}() are\n * used.\n */\nbool\nrtree_new(rtree_t *rtree, bool zeroed) {\n#ifdef JEMALLOC_JET\n\tif (!zeroed) {\n\t\tmemset(rtree, 0, sizeof(rtree_t)); /* Clear root. */\n\t}\n#else\n\tassert(zeroed);\n#endif\n\n\tif (malloc_mutex_init(&rtree->init_lock, \"rtree\", WITNESS_RANK_RTREE,\n\t    malloc_mutex_rank_exclusive)) {\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\nstatic rtree_node_elm_t *\nrtree_node_alloc_impl(tsdn_t *tsdn, rtree_t *rtree, size_t nelms) {\n\treturn (rtree_node_elm_t *)base_alloc(tsdn, b0get(), nelms *\n\t    sizeof(rtree_node_elm_t), CACHELINE);\n}\nrtree_node_alloc_t *JET_MUTABLE rtree_node_alloc = rtree_node_alloc_impl;\n\nstatic void\nrtree_node_dalloc_impl(tsdn_t *tsdn, rtree_t *rtree, rtree_node_elm_t *node) {\n\t/* Nodes are never deleted during normal operation. */\n\tnot_reached();\n}\nrtree_node_dalloc_t *JET_MUTABLE rtree_node_dalloc =\n    rtree_node_dalloc_impl;\n\nstatic rtree_leaf_elm_t *\nrtree_leaf_alloc_impl(tsdn_t *tsdn, rtree_t *rtree, size_t nelms) {\n\treturn (rtree_leaf_elm_t *)base_alloc(tsdn, b0get(), nelms *\n\t    sizeof(rtree_leaf_elm_t), CACHELINE);\n}\nrtree_leaf_alloc_t *JET_MUTABLE rtree_leaf_alloc = rtree_leaf_alloc_impl;\n\nstatic void\nrtree_leaf_dalloc_impl(tsdn_t *tsdn, rtree_t *rtree, rtree_leaf_elm_t *leaf) {\n\t/* Leaves are never deleted during normal operation. */\n\tnot_reached();\n}\nrtree_leaf_dalloc_t *JET_MUTABLE rtree_leaf_dalloc =\n    rtree_leaf_dalloc_impl;\n\n#ifdef JEMALLOC_JET\n#  if RTREE_HEIGHT > 1\nstatic void\nrtree_delete_subtree(tsdn_t *tsdn, rtree_t *rtree, rtree_node_elm_t *subtree,\n    unsigned level) {\n\tsize_t nchildren = ZU(1) << rtree_levels[level].bits;\n\tif (level + 2 < RTREE_HEIGHT) {\n\t\tfor (size_t i = 0; i < nchildren; i++) {\n\t\t\trtree_node_elm_t *node =\n\t\t\t    (rtree_node_elm_t *)atomic_load_p(&subtree[i].child,\n\t\t\t    ATOMIC_RELAXED);\n\t\t\tif (node != NULL) {\n\t\t\t\trtree_delete_subtree(tsdn, rtree, node, level +\n\t\t\t\t    1);\n\t\t\t}\n\t\t}\n\t} else {\n\t\tfor (size_t i = 0; i < nchildren; i++) {\n\t\t\trtree_leaf_elm_t *leaf =\n\t\t\t    (rtree_leaf_elm_t *)atomic_load_p(&subtree[i].child,\n\t\t\t    ATOMIC_RELAXED);\n\t\t\tif (leaf != NULL) {\n\t\t\t\trtree_leaf_dalloc(tsdn, rtree, leaf);\n\t\t\t}\n\t\t}\n\t}\n\n\tif (subtree != rtree->root) {\n\t\trtree_node_dalloc(tsdn, rtree, subtree);\n\t}\n}\n#  endif\n\nvoid\nrtree_delete(tsdn_t *tsdn, rtree_t *rtree) {\n#  if RTREE_HEIGHT > 1\n\trtree_delete_subtree(tsdn, rtree, rtree->root, 0);\n#  endif\n}\n#endif\n\nstatic rtree_node_elm_t *\nrtree_node_init(tsdn_t *tsdn, rtree_t *rtree, unsigned level,\n    atomic_p_t *elmp) {\n\tmalloc_mutex_lock(tsdn, &rtree->init_lock);\n\t/*\n\t * If *elmp is non-null, then it was initialized with the init lock\n\t * held, so we can get by with 'relaxed' here.\n\t */\n\trtree_node_elm_t *node = atomic_load_p(elmp, ATOMIC_RELAXED);\n\tif (node == NULL) {\n\t\tnode = rtree_node_alloc(tsdn, rtree, ZU(1) <<\n\t\t    rtree_levels[level].bits);\n\t\tif (node == NULL) {\n\t\t\tmalloc_mutex_unlock(tsdn, &rtree->init_lock);\n\t\t\treturn NULL;\n\t\t}\n\t\t/*\n\t\t * Even though we hold the lock, a later reader might not; we\n\t\t * need release semantics.\n\t\t */\n\t\tatomic_store_p(elmp, node, ATOMIC_RELEASE);\n\t}\n\tmalloc_mutex_unlock(tsdn, &rtree->init_lock);\n\n\treturn node;\n}\n\nstatic rtree_leaf_elm_t *\nrtree_leaf_init(tsdn_t *tsdn, rtree_t *rtree, atomic_p_t *elmp) {\n\tmalloc_mutex_lock(tsdn, &rtree->init_lock);\n\t/*\n\t * If *elmp is non-null, then it was initialized with the init lock\n\t * held, so we can get by with 'relaxed' here.\n\t */\n\trtree_leaf_elm_t *leaf = atomic_load_p(elmp, ATOMIC_RELAXED);\n\tif (leaf == NULL) {\n\t\tleaf = rtree_leaf_alloc(tsdn, rtree, ZU(1) <<\n\t\t    rtree_levels[RTREE_HEIGHT-1].bits);\n\t\tif (leaf == NULL) {\n\t\t\tmalloc_mutex_unlock(tsdn, &rtree->init_lock);\n\t\t\treturn NULL;\n\t\t}\n\t\t/*\n\t\t * Even though we hold the lock, a later reader might not; we\n\t\t * need release semantics.\n\t\t */\n\t\tatomic_store_p(elmp, leaf, ATOMIC_RELEASE);\n\t}\n\tmalloc_mutex_unlock(tsdn, &rtree->init_lock);\n\n\treturn leaf;\n}\n\nstatic bool\nrtree_node_valid(rtree_node_elm_t *node) {\n\treturn ((uintptr_t)node != (uintptr_t)0);\n}\n\nstatic bool\nrtree_leaf_valid(rtree_leaf_elm_t *leaf) {\n\treturn ((uintptr_t)leaf != (uintptr_t)0);\n}\n\nstatic rtree_node_elm_t *\nrtree_child_node_tryread(rtree_node_elm_t *elm, bool dependent) {\n\trtree_node_elm_t *node;\n\n\tif (dependent) {\n\t\tnode = (rtree_node_elm_t *)atomic_load_p(&elm->child,\n\t\t    ATOMIC_RELAXED);\n\t} else {\n\t\tnode = (rtree_node_elm_t *)atomic_load_p(&elm->child,\n\t\t    ATOMIC_ACQUIRE);\n\t}\n\n\tassert(!dependent || node != NULL);\n\treturn node;\n}\n\nstatic rtree_node_elm_t *\nrtree_child_node_read(tsdn_t *tsdn, rtree_t *rtree, rtree_node_elm_t *elm,\n    unsigned level, bool dependent) {\n\trtree_node_elm_t *node;\n\n\tnode = rtree_child_node_tryread(elm, dependent);\n\tif (!dependent && unlikely(!rtree_node_valid(node))) {\n\t\tnode = rtree_node_init(tsdn, rtree, level + 1, &elm->child);\n\t}\n\tassert(!dependent || node != NULL);\n\treturn node;\n}\n\nstatic rtree_leaf_elm_t *\nrtree_child_leaf_tryread(rtree_node_elm_t *elm, bool dependent) {\n\trtree_leaf_elm_t *leaf;\n\n\tif (dependent) {\n\t\tleaf = (rtree_leaf_elm_t *)atomic_load_p(&elm->child,\n\t\t    ATOMIC_RELAXED);\n\t} else {\n\t\tleaf = (rtree_leaf_elm_t *)atomic_load_p(&elm->child,\n\t\t    ATOMIC_ACQUIRE);\n\t}\n\n\tassert(!dependent || leaf != NULL);\n\treturn leaf;\n}\n\nstatic rtree_leaf_elm_t *\nrtree_child_leaf_read(tsdn_t *tsdn, rtree_t *rtree, rtree_node_elm_t *elm,\n    unsigned level, bool dependent) {\n\trtree_leaf_elm_t *leaf;\n\n\tleaf = rtree_child_leaf_tryread(elm, dependent);\n\tif (!dependent && unlikely(!rtree_leaf_valid(leaf))) {\n\t\tleaf = rtree_leaf_init(tsdn, rtree, &elm->child);\n\t}\n\tassert(!dependent || leaf != NULL);\n\treturn leaf;\n}\n\nrtree_leaf_elm_t *\nrtree_leaf_elm_lookup_hard(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,\n    uintptr_t key, bool dependent, bool init_missing) {\n\trtree_node_elm_t *node;\n\trtree_leaf_elm_t *leaf;\n#if RTREE_HEIGHT > 1\n\tnode = rtree->root;\n#else\n\tleaf = rtree->root;\n#endif\n\n\tif (config_debug) {\n\t\tuintptr_t leafkey = rtree_leafkey(key);\n\t\tfor (unsigned i = 0; i < RTREE_CTX_NCACHE; i++) {\n\t\t\tassert(rtree_ctx->cache[i].leafkey != leafkey);\n\t\t}\n\t\tfor (unsigned i = 0; i < RTREE_CTX_NCACHE_L2; i++) {\n\t\t\tassert(rtree_ctx->l2_cache[i].leafkey != leafkey);\n\t\t}\n\t}\n\n#define RTREE_GET_CHILD(level) {\t\t\t\t\t\\\n\t\tassert(level < RTREE_HEIGHT-1);\t\t\t\t\\\n\t\tif (level != 0 && !dependent &&\t\t\t\t\\\n\t\t    unlikely(!rtree_node_valid(node))) {\t\t\\\n\t\t\treturn NULL;\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\tuintptr_t subkey = rtree_subkey(key, level);\t\t\\\n\t\tif (level + 2 < RTREE_HEIGHT) {\t\t\t\t\\\n\t\t\tnode = init_missing ?\t\t\t\t\\\n\t\t\t    rtree_child_node_read(tsdn, rtree,\t\t\\\n\t\t\t    &node[subkey], level, dependent) :\t\t\\\n\t\t\t    rtree_child_node_tryread(&node[subkey],\t\\\n\t\t\t    dependent);\t\t\t\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t\tleaf = init_missing ?\t\t\t\t\\\n\t\t\t    rtree_child_leaf_read(tsdn, rtree,\t\t\\\n\t\t\t    &node[subkey], level, dependent) :\t\t\\\n\t\t\t    rtree_child_leaf_tryread(&node[subkey],\t\\\n\t\t\t    dependent);\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\n\t/*\n\t * Cache replacement upon hard lookup (i.e. L1 & L2 rtree cache miss):\n\t * (1) evict last entry in L2 cache; (2) move the collision slot from L1\n\t * cache down to L2; and 3) fill L1.\n\t */\n#define RTREE_GET_LEAF(level) {\t\t\t\t\t\t\\\n\t\tassert(level == RTREE_HEIGHT-1);\t\t\t\\\n\t\tif (!dependent && unlikely(!rtree_leaf_valid(leaf))) {\t\\\n\t\t\treturn NULL;\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\tif (RTREE_CTX_NCACHE_L2 > 1) {\t\t\t\t\\\n\t\t\tmemmove(&rtree_ctx->l2_cache[1],\t\t\\\n\t\t\t    &rtree_ctx->l2_cache[0],\t\t\t\\\n\t\t\t    sizeof(rtree_ctx_cache_elm_t) *\t\t\\\n\t\t\t    (RTREE_CTX_NCACHE_L2 - 1));\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\tsize_t slot = rtree_cache_direct_map(key);\t\t\\\n\t\trtree_ctx->l2_cache[0].leafkey =\t\t\t\\\n\t\t    rtree_ctx->cache[slot].leafkey;\t\t\t\\\n\t\trtree_ctx->l2_cache[0].leaf =\t\t\t\t\\\n\t\t    rtree_ctx->cache[slot].leaf;\t\t\t\\\n\t\tuintptr_t leafkey = rtree_leafkey(key);\t\t\t\\\n\t\trtree_ctx->cache[slot].leafkey = leafkey;\t\t\\\n\t\trtree_ctx->cache[slot].leaf = leaf;\t\t\t\\\n\t\tuintptr_t subkey = rtree_subkey(key, level);\t\t\\\n\t\treturn &leaf[subkey];\t\t\t\t\t\\\n\t}\n\tif (RTREE_HEIGHT > 1) {\n\t\tRTREE_GET_CHILD(0)\n\t}\n\tif (RTREE_HEIGHT > 2) {\n\t\tRTREE_GET_CHILD(1)\n\t}\n\tif (RTREE_HEIGHT > 3) {\n\t\tfor (unsigned i = 2; i < RTREE_HEIGHT-1; i++) {\n\t\t\tRTREE_GET_CHILD(i)\n\t\t}\n\t}\n\tRTREE_GET_LEAF(RTREE_HEIGHT-1)\n#undef RTREE_GET_CHILD\n#undef RTREE_GET_LEAF\n\tnot_reached();\n}\n\nvoid\nrtree_ctx_data_init(rtree_ctx_t *ctx) {\n\tfor (unsigned i = 0; i < RTREE_CTX_NCACHE; i++) {\n\t\trtree_ctx_cache_elm_t *cache = &ctx->cache[i];\n\t\tcache->leafkey = RTREE_LEAFKEY_INVALID;\n\t\tcache->leaf = NULL;\n\t}\n\tfor (unsigned i = 0; i < RTREE_CTX_NCACHE_L2; i++) {\n\t\trtree_ctx_cache_elm_t *cache = &ctx->l2_cache[i];\n\t\tcache->leafkey = RTREE_LEAFKEY_INVALID;\n\t\tcache->leaf = NULL;\n\t}\n}\n"
  },
  {
    "path": "deps/jemalloc/src/safety_check.c",
    "content": "#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\nstatic void (*safety_check_abort)(const char *message);\n\nvoid safety_check_set_abort(void (*abort_fn)(const char *)) {\n\tsafety_check_abort = abort_fn;\n}\n\nvoid safety_check_fail(const char *format, ...) {\n\tchar buf[MALLOC_PRINTF_BUFSIZE];\n\n\tva_list ap;\n\tva_start(ap, format);\n\tmalloc_vsnprintf(buf, MALLOC_PRINTF_BUFSIZE, format, ap);\n\tva_end(ap);\n\n\tif (safety_check_abort == NULL) {\n\t\tmalloc_write(buf);\n\t\tabort();\n\t} else {\n\t\tsafety_check_abort(buf);\n\t}\n}\n"
  },
  {
    "path": "deps/jemalloc/src/sc.c",
    "content": "#include \"jemalloc/internal/jemalloc_preamble.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/bit_util.h\"\n#include \"jemalloc/internal/bitmap.h\"\n#include \"jemalloc/internal/pages.h\"\n#include \"jemalloc/internal/sc.h\"\n\n/*\n * This module computes the size classes used to satisfy allocations.  The logic\n * here was ported more or less line-by-line from a shell script, and because of\n * that is not the most idiomatic C.  Eventually we should fix this, but for now\n * at least the damage is compartmentalized to this file.\n */\n\nsc_data_t sc_data_global;\n\nstatic size_t\nreg_size_compute(int lg_base, int lg_delta, int ndelta) {\n\treturn (ZU(1) << lg_base) + (ZU(ndelta) << lg_delta);\n}\n\n/* Returns the number of pages in the slab. */\nstatic int\nslab_size(int lg_page, int lg_base, int lg_delta, int ndelta) {\n\tsize_t page = (ZU(1) << lg_page);\n\tsize_t reg_size = reg_size_compute(lg_base, lg_delta, ndelta);\n\n\tsize_t try_slab_size = page;\n\tsize_t try_nregs = try_slab_size / reg_size;\n\tsize_t perfect_slab_size = 0;\n\tbool perfect = false;\n\t/*\n\t * This loop continues until we find the least common multiple of the\n\t * page size and size class size.  Size classes are all of the form\n\t * base + ndelta * delta == (ndelta + base/ndelta) * delta, which is\n\t * (ndelta + ngroup) * delta.  The way we choose slabbing strategies\n\t * means that delta is at most the page size and ndelta < ngroup.  So\n\t * the loop executes for at most 2 * ngroup - 1 iterations, which is\n\t * also the bound on the number of pages in a slab chosen by default.\n\t * With the current default settings, this is at most 7.\n\t */\n\twhile (!perfect) {\n\t\tperfect_slab_size = try_slab_size;\n\t\tsize_t perfect_nregs = try_nregs;\n\t\ttry_slab_size += page;\n\t\ttry_nregs = try_slab_size / reg_size;\n\t\tif (perfect_slab_size == perfect_nregs * reg_size) {\n\t\t\tperfect = true;\n\t\t}\n\t}\n\treturn (int)(perfect_slab_size / page);\n}\n\nstatic void\nsize_class(\n    /* Output. */\n    sc_t *sc,\n    /* Configuration decisions. */\n    int lg_max_lookup, int lg_page, int lg_ngroup,\n    /* Inputs specific to the size class. */\n    int index, int lg_base, int lg_delta, int ndelta) {\n\tsc->index = index;\n\tsc->lg_base = lg_base;\n\tsc->lg_delta = lg_delta;\n\tsc->ndelta = ndelta;\n\tsc->psz = (reg_size_compute(lg_base, lg_delta, ndelta)\n\t    % (ZU(1) << lg_page) == 0);\n\tsize_t size = (ZU(1) << lg_base) + (ZU(ndelta) << lg_delta);\n\tif (index == 0) {\n\t\tassert(!sc->psz);\n\t}\n\tif (size < (ZU(1) << (lg_page + lg_ngroup))) {\n\t\tsc->bin = true;\n\t\tsc->pgs = slab_size(lg_page, lg_base, lg_delta, ndelta);\n\t} else {\n\t\tsc->bin = false;\n\t\tsc->pgs = 0;\n\t}\n\tif (size <= (ZU(1) << lg_max_lookup)) {\n\t\tsc->lg_delta_lookup = lg_delta;\n\t} else {\n\t\tsc->lg_delta_lookup = 0;\n\t}\n}\n\nstatic void\nsize_classes(\n    /* Output. */\n    sc_data_t *sc_data,\n    /* Determined by the system. */\n    size_t lg_ptr_size, int lg_quantum,\n    /* Configuration decisions. */\n    int lg_tiny_min, int lg_max_lookup, int lg_page, int lg_ngroup) {\n\tint ptr_bits = (1 << lg_ptr_size) * 8;\n\tint ngroup = (1 << lg_ngroup);\n\tint ntiny = 0;\n\tint nlbins = 0;\n\tint lg_tiny_maxclass = (unsigned)-1;\n\tint nbins = 0;\n\tint npsizes = 0;\n\n\tint index = 0;\n\n\tint ndelta = 0;\n\tint lg_base = lg_tiny_min;\n\tint lg_delta = lg_base;\n\n\t/* Outputs that we update as we go. */\n\tsize_t lookup_maxclass = 0;\n\tsize_t small_maxclass = 0;\n\tint lg_large_minclass = 0;\n\tsize_t large_maxclass = 0;\n\n\t/* Tiny size classes. */\n\twhile (lg_base < lg_quantum) {\n\t\tsc_t *sc = &sc_data->sc[index];\n\t\tsize_class(sc, lg_max_lookup, lg_page, lg_ngroup, index,\n\t\t    lg_base, lg_delta, ndelta);\n\t\tif (sc->lg_delta_lookup != 0) {\n\t\t\tnlbins = index + 1;\n\t\t}\n\t\tif (sc->psz) {\n\t\t\tnpsizes++;\n\t\t}\n\t\tif (sc->bin) {\n\t\t\tnbins++;\n\t\t}\n\t\tntiny++;\n\t\t/* Final written value is correct. */\n\t\tlg_tiny_maxclass = lg_base;\n\t\tindex++;\n\t\tlg_delta = lg_base;\n\t\tlg_base++;\n\t}\n\n\t/* First non-tiny (pseudo) group. */\n\tif (ntiny != 0) {\n\t\tsc_t *sc = &sc_data->sc[index];\n\t\t/*\n\t\t * See the note in sc.h; the first non-tiny size class has an\n\t\t * unusual encoding.\n\t\t */\n\t\tlg_base--;\n\t\tndelta = 1;\n\t\tsize_class(sc, lg_max_lookup, lg_page, lg_ngroup, index,\n\t\t    lg_base, lg_delta, ndelta);\n\t\tindex++;\n\t\tlg_base++;\n\t\tlg_delta++;\n\t\tif (sc->psz) {\n\t\t\tnpsizes++;\n\t\t}\n\t\tif (sc->bin) {\n\t\t\tnbins++;\n\t\t}\n\t}\n\twhile (ndelta < ngroup) {\n\t\tsc_t *sc = &sc_data->sc[index];\n\t\tsize_class(sc, lg_max_lookup, lg_page, lg_ngroup, index,\n\t\t    lg_base, lg_delta, ndelta);\n\t\tindex++;\n\t\tndelta++;\n\t\tif (sc->psz) {\n\t\t\tnpsizes++;\n\t\t}\n\t\tif (sc->bin) {\n\t\t\tnbins++;\n\t\t}\n\t}\n\n\t/* All remaining groups. */\n\tlg_base = lg_base + lg_ngroup;\n\twhile (lg_base < ptr_bits - 1) {\n\t\tndelta = 1;\n\t\tint ndelta_limit;\n\t\tif (lg_base == ptr_bits - 2) {\n\t\t\tndelta_limit = ngroup - 1;\n\t\t} else {\n\t\t\tndelta_limit = ngroup;\n\t\t}\n\t\twhile (ndelta <= ndelta_limit) {\n\t\t\tsc_t *sc = &sc_data->sc[index];\n\t\t\tsize_class(sc, lg_max_lookup, lg_page, lg_ngroup, index,\n\t\t\t    lg_base, lg_delta, ndelta);\n\t\t\tif (sc->lg_delta_lookup != 0) {\n\t\t\t\tnlbins = index + 1;\n\t\t\t\t/* Final written value is correct. */\n\t\t\t\tlookup_maxclass = (ZU(1) << lg_base)\n\t\t\t\t    + (ZU(ndelta) << lg_delta);\n\t\t\t}\n\t\t\tif (sc->psz) {\n\t\t\t\tnpsizes++;\n\t\t\t}\n\t\t\tif (sc->bin) {\n\t\t\t\tnbins++;\n\t\t\t\t/* Final written value is correct. */\n\t\t\t\tsmall_maxclass = (ZU(1) << lg_base)\n\t\t\t\t    + (ZU(ndelta) << lg_delta);\n\t\t\t\tif (lg_ngroup > 0) {\n\t\t\t\t\tlg_large_minclass = lg_base + 1;\n\t\t\t\t} else {\n\t\t\t\t\tlg_large_minclass = lg_base + 2;\n\t\t\t\t}\n\t\t\t}\n\t\t\tlarge_maxclass = (ZU(1) << lg_base)\n\t\t\t    + (ZU(ndelta) << lg_delta);\n\t\t\tindex++;\n\t\t\tndelta++;\n\t\t}\n\t\tlg_base++;\n\t\tlg_delta++;\n\t}\n\t/* Additional outputs. */\n\tint nsizes = index;\n\tunsigned lg_ceil_nsizes = lg_ceil(nsizes);\n\n\t/* Fill in the output data. */\n\tsc_data->ntiny = ntiny;\n\tsc_data->nlbins = nlbins;\n\tsc_data->nbins = nbins;\n\tsc_data->nsizes = nsizes;\n\tsc_data->lg_ceil_nsizes = lg_ceil_nsizes;\n\tsc_data->npsizes = npsizes;\n\tsc_data->lg_tiny_maxclass = lg_tiny_maxclass;\n\tsc_data->lookup_maxclass = lookup_maxclass;\n\tsc_data->small_maxclass = small_maxclass;\n\tsc_data->lg_large_minclass = lg_large_minclass;\n\tsc_data->large_minclass = (ZU(1) << lg_large_minclass);\n\tsc_data->large_maxclass = large_maxclass;\n\n\t/*\n\t * We compute these values in two ways:\n\t *   - Incrementally, as above.\n\t *   - In macros, in sc.h.\n\t * The computation is easier when done incrementally, but putting it in\n\t * a constant makes it available to the fast paths without having to\n\t * touch the extra global cacheline.  We assert, however, that the two\n\t * computations are equivalent.\n\t */\n\tassert(sc_data->npsizes == SC_NPSIZES);\n\tassert(sc_data->lg_tiny_maxclass == SC_LG_TINY_MAXCLASS);\n\tassert(sc_data->small_maxclass == SC_SMALL_MAXCLASS);\n\tassert(sc_data->large_minclass == SC_LARGE_MINCLASS);\n\tassert(sc_data->lg_large_minclass == SC_LG_LARGE_MINCLASS);\n\tassert(sc_data->large_maxclass == SC_LARGE_MAXCLASS);\n\n\t/* \n\t * In the allocation fastpath, we want to assume that we can\n\t * unconditionally subtract the requested allocation size from\n\t * a ssize_t, and detect passing through 0 correctly.  This\n\t * results in optimal generated code.  For this to work, the\n\t * maximum allocation size must be less than SSIZE_MAX.\n\t */\n\tassert(SC_LARGE_MAXCLASS < SSIZE_MAX);\n}\n\nvoid\nsc_data_init(sc_data_t *sc_data) {\n\tassert(!sc_data->initialized);\n\n\tint lg_max_lookup = 12;\n\n\tsize_classes(sc_data, LG_SIZEOF_PTR, LG_QUANTUM, SC_LG_TINY_MIN,\n\t    lg_max_lookup, LG_PAGE, 2);\n\n\tsc_data->initialized = true;\n}\n\nstatic void\nsc_data_update_sc_slab_size(sc_t *sc, size_t reg_size, size_t pgs_guess) {\n\tsize_t min_pgs = reg_size / PAGE;\n\tif (reg_size % PAGE != 0) {\n\t\tmin_pgs++;\n\t}\n\t/*\n\t * BITMAP_MAXBITS is actually determined by putting the smallest\n\t * possible size-class on one page, so this can never be 0.\n\t */\n\tsize_t max_pgs = BITMAP_MAXBITS * reg_size / PAGE;\n\n\tassert(min_pgs <= max_pgs);\n\tassert(min_pgs > 0);\n\tassert(max_pgs >= 1);\n\tif (pgs_guess < min_pgs) {\n\t\tsc->pgs = (int)min_pgs;\n\t} else if (pgs_guess > max_pgs) {\n\t\tsc->pgs = (int)max_pgs;\n\t} else {\n\t\tsc->pgs = (int)pgs_guess;\n\t}\n}\n\nvoid\nsc_data_update_slab_size(sc_data_t *data, size_t begin, size_t end, int pgs) {\n\tassert(data->initialized);\n\tfor (int i = 0; i < data->nsizes; i++) {\n\t\tsc_t *sc = &data->sc[i];\n\t\tif (!sc->bin) {\n\t\t\tbreak;\n\t\t}\n\t\tsize_t reg_size = reg_size_compute(sc->lg_base, sc->lg_delta,\n\t\t    sc->ndelta);\n\t\tif (begin <= reg_size && reg_size <= end) {\n\t\t\tsc_data_update_sc_slab_size(sc, reg_size, pgs);\n\t\t}\n\t}\n}\n\nvoid\nsc_boot(sc_data_t *data) {\n\tsc_data_init(data);\n}\n"
  },
  {
    "path": "deps/jemalloc/src/stats.c",
    "content": "#define JEMALLOC_STATS_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/ctl.h\"\n#include \"jemalloc/internal/emitter.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/mutex_prof.h\"\n\nconst char *global_mutex_names[mutex_prof_num_global_mutexes] = {\n#define OP(mtx) #mtx,\n\tMUTEX_PROF_GLOBAL_MUTEXES\n#undef OP\n};\n\nconst char *arena_mutex_names[mutex_prof_num_arena_mutexes] = {\n#define OP(mtx) #mtx,\n\tMUTEX_PROF_ARENA_MUTEXES\n#undef OP\n};\n\n#define CTL_GET(n, v, t) do {\t\t\t\t\t\t\\\n\tsize_t sz = sizeof(t);\t\t\t\t\t\t\\\n\txmallctl(n, (void *)v, &sz, NULL, 0);\t\t\t\t\\\n} while (0)\n\n#define CTL_M2_GET(n, i, v, t) do {\t\t\t\t\t\\\n\tsize_t mib[CTL_MAX_DEPTH];\t\t\t\t\t\\\n\tsize_t miblen = sizeof(mib) / sizeof(size_t);\t\t\t\\\n\tsize_t sz = sizeof(t);\t\t\t\t\t\t\\\n\txmallctlnametomib(n, mib, &miblen);\t\t\t\t\\\n\tmib[2] = (i);\t\t\t\t\t\t\t\\\n\txmallctlbymib(mib, miblen, (void *)v, &sz, NULL, 0);\t\t\\\n} while (0)\n\n#define CTL_M2_M4_GET(n, i, j, v, t) do {\t\t\t\t\\\n\tsize_t mib[CTL_MAX_DEPTH];\t\t\t\t\t\\\n\tsize_t miblen = sizeof(mib) / sizeof(size_t);\t\t\t\\\n\tsize_t sz = sizeof(t);\t\t\t\t\t\t\\\n\txmallctlnametomib(n, mib, &miblen);\t\t\t\t\\\n\tmib[2] = (i);\t\t\t\t\t\t\t\\\n\tmib[4] = (j);\t\t\t\t\t\t\t\\\n\txmallctlbymib(mib, miblen, (void *)v, &sz, NULL, 0);\t\t\\\n} while (0)\n\n/******************************************************************************/\n/* Data. */\n\nbool opt_stats_print = false;\nchar opt_stats_print_opts[stats_print_tot_num_options+1] = \"\";\n\n/******************************************************************************/\n\nstatic uint64_t\nrate_per_second(uint64_t value, uint64_t uptime_ns) {\n\tuint64_t billion = 1000000000;\n\tif (uptime_ns == 0 || value == 0) {\n\t\treturn 0;\n\t}\n\tif (uptime_ns < billion) {\n\t\treturn value;\n\t} else {\n\t\tuint64_t uptime_s = uptime_ns / billion;\n\t\treturn value / uptime_s;\n\t}\n}\n\n/* Calculate x.yyy and output a string (takes a fixed sized char array). */\nstatic bool\nget_rate_str(uint64_t dividend, uint64_t divisor, char str[6]) {\n\tif (divisor == 0 || dividend > divisor) {\n\t\t/* The rate is not supposed to be greater than 1. */\n\t\treturn true;\n\t}\n\tif (dividend > 0) {\n\t\tassert(UINT64_MAX / dividend >= 1000);\n\t}\n\n\tunsigned n = (unsigned)((dividend * 1000) / divisor);\n\tif (n < 10) {\n\t\tmalloc_snprintf(str, 6, \"0.00%u\", n);\n\t} else if (n < 100) {\n\t\tmalloc_snprintf(str, 6, \"0.0%u\", n);\n\t} else if (n < 1000) {\n\t\tmalloc_snprintf(str, 6, \"0.%u\", n);\n\t} else {\n\t\tmalloc_snprintf(str, 6, \"1\");\n\t}\n\n\treturn false;\n}\n\n#define MUTEX_CTL_STR_MAX_LENGTH 128\nstatic void\ngen_mutex_ctl_str(char *str, size_t buf_len, const char *prefix,\n    const char *mutex, const char *counter) {\n\tmalloc_snprintf(str, buf_len, \"stats.%s.%s.%s\", prefix, mutex, counter);\n}\n\nstatic void\nmutex_stats_init_cols(emitter_row_t *row, const char *table_name,\n    emitter_col_t *name,\n    emitter_col_t col_uint64_t[mutex_prof_num_uint64_t_counters],\n    emitter_col_t col_uint32_t[mutex_prof_num_uint32_t_counters]) {\n\tmutex_prof_uint64_t_counter_ind_t k_uint64_t = 0;\n\tmutex_prof_uint32_t_counter_ind_t k_uint32_t = 0;\n\n\temitter_col_t *col;\n\n\tif (name != NULL) {\n\t\temitter_col_init(name, row);\n\t\tname->justify = emitter_justify_left;\n\t\tname->width = 21;\n\t\tname->type = emitter_type_title;\n\t\tname->str_val = table_name;\n\t}\n\n#define WIDTH_uint32_t 12\n#define WIDTH_uint64_t 16\n#define OP(counter, counter_type, human, derived, base_counter)\t\\\n\tcol = &col_##counter_type[k_##counter_type];\t\t\t\\\n\t++k_##counter_type;\t\t\t\t\t\t\\\n\temitter_col_init(col, row);\t\t\t\t\t\\\n\tcol->justify = emitter_justify_right;\t\t\t\t\\\n\tcol->width = derived ? 8 : WIDTH_##counter_type;\t\t\\\n\tcol->type = emitter_type_title;\t\t\t\t\t\\\n\tcol->str_val = human;\n\tMUTEX_PROF_COUNTERS\n#undef OP\n#undef WIDTH_uint32_t\n#undef WIDTH_uint64_t\n\tcol_uint64_t[mutex_counter_total_wait_time_ps].width = 10;\n}\n\nstatic void\nmutex_stats_read_global(const char *name, emitter_col_t *col_name,\n    emitter_col_t col_uint64_t[mutex_prof_num_uint64_t_counters],\n    emitter_col_t col_uint32_t[mutex_prof_num_uint32_t_counters],\n    uint64_t uptime) {\n\tchar cmd[MUTEX_CTL_STR_MAX_LENGTH];\n\n\tcol_name->str_val = name;\n\n\temitter_col_t *dst;\n#define EMITTER_TYPE_uint32_t emitter_type_uint32\n#define EMITTER_TYPE_uint64_t emitter_type_uint64\n#define OP(counter, counter_type, human, derived, base_counter)\t\\\n\tdst = &col_##counter_type[mutex_counter_##counter];\t\t\\\n\tdst->type = EMITTER_TYPE_##counter_type;\t\t\t\\\n\tif (!derived) {\t\t\t\t\t\t\t\\\n\t\tgen_mutex_ctl_str(cmd, MUTEX_CTL_STR_MAX_LENGTH,\t\\\n\t\t    \"mutexes\", name, #counter);\t\t\t\t\\\n\t\tCTL_GET(cmd, (counter_type *)&dst->bool_val, counter_type);\t\\\n\t} else { \\\n\t    emitter_col_t *base = &col_##counter_type[mutex_counter_##base_counter];\t\\\n\t    dst->counter_type##_val = rate_per_second(base->counter_type##_val, uptime); \\\n\t}\n\tMUTEX_PROF_COUNTERS\n#undef OP\n#undef EMITTER_TYPE_uint32_t\n#undef EMITTER_TYPE_uint64_t\n}\n\nstatic void\nmutex_stats_read_arena(unsigned arena_ind, mutex_prof_arena_ind_t mutex_ind,\n    const char *name, emitter_col_t *col_name,\n    emitter_col_t col_uint64_t[mutex_prof_num_uint64_t_counters],\n    emitter_col_t col_uint32_t[mutex_prof_num_uint32_t_counters],\n    uint64_t uptime) {\n\tchar cmd[MUTEX_CTL_STR_MAX_LENGTH];\n\n\tcol_name->str_val = name;\n\n\temitter_col_t *dst;\n#define EMITTER_TYPE_uint32_t emitter_type_uint32\n#define EMITTER_TYPE_uint64_t emitter_type_uint64\n#define OP(counter, counter_type, human, derived, base_counter)\t\\\n\tdst = &col_##counter_type[mutex_counter_##counter];\t\t\\\n\tdst->type = EMITTER_TYPE_##counter_type;\t\t\t\\\n\tif (!derived) {                                   \\\n\t\tgen_mutex_ctl_str(cmd, MUTEX_CTL_STR_MAX_LENGTH,        \\\n\t\t    \"arenas.0.mutexes\", arena_mutex_names[mutex_ind], #counter);\\\n\t\tCTL_M2_GET(cmd, arena_ind, (counter_type *)&dst->bool_val, counter_type); \\\n\t} else {                      \\\n\t\temitter_col_t *base = &col_##counter_type[mutex_counter_##base_counter];\t\\\n\t\tdst->counter_type##_val = rate_per_second(base->counter_type##_val, uptime); \\\n\t}\n\tMUTEX_PROF_COUNTERS\n#undef OP\n#undef EMITTER_TYPE_uint32_t\n#undef EMITTER_TYPE_uint64_t\n}\n\nstatic void\nmutex_stats_read_arena_bin(unsigned arena_ind, unsigned bin_ind,\n    emitter_col_t col_uint64_t[mutex_prof_num_uint64_t_counters],\n    emitter_col_t col_uint32_t[mutex_prof_num_uint32_t_counters],\n    uint64_t uptime) {\n\tchar cmd[MUTEX_CTL_STR_MAX_LENGTH];\n\temitter_col_t *dst;\n\n#define EMITTER_TYPE_uint32_t emitter_type_uint32\n#define EMITTER_TYPE_uint64_t emitter_type_uint64\n#define OP(counter, counter_type, human, derived, base_counter)\t\\\n\tdst = &col_##counter_type[mutex_counter_##counter];\t\t\\\n\tdst->type = EMITTER_TYPE_##counter_type;\t\t\t\\\n\tif (!derived) {                                   \\\n\t\tgen_mutex_ctl_str(cmd, MUTEX_CTL_STR_MAX_LENGTH,        \\\n\t\t    \"arenas.0.bins.0\",\"mutex\", #counter);            \\\n\t\tCTL_M2_M4_GET(cmd, arena_ind, bin_ind,                \\\n\t\t    (counter_type *)&dst->bool_val, counter_type);  \\\n\t} else {                      \\\n\t\temitter_col_t *base = &col_##counter_type[mutex_counter_##base_counter]; \\\n\t\tdst->counter_type##_val = rate_per_second(base->counter_type##_val, uptime); \\\n\t}\n\tMUTEX_PROF_COUNTERS\n#undef OP\n#undef EMITTER_TYPE_uint32_t\n#undef EMITTER_TYPE_uint64_t\n}\n\n/* \"row\" can be NULL to avoid emitting in table mode. */\nstatic void\nmutex_stats_emit(emitter_t *emitter, emitter_row_t *row,\n    emitter_col_t col_uint64_t[mutex_prof_num_uint64_t_counters],\n    emitter_col_t col_uint32_t[mutex_prof_num_uint32_t_counters]) {\n\tif (row != NULL) {\n\t\temitter_table_row(emitter, row);\n\t}\n\n\tmutex_prof_uint64_t_counter_ind_t k_uint64_t = 0;\n\tmutex_prof_uint32_t_counter_ind_t k_uint32_t = 0;\n\n\temitter_col_t *col;\n\n#define EMITTER_TYPE_uint32_t emitter_type_uint32\n#define EMITTER_TYPE_uint64_t emitter_type_uint64\n#define OP(counter, type, human, derived, base_counter)\t\t\\\n\tif (!derived) {                    \\\n\t\tcol = &col_##type[k_##type];                        \\\n\t\t++k_##type;                            \\\n\t\temitter_json_kv(emitter, #counter, EMITTER_TYPE_##type,        \\\n\t\t    (const void *)&col->bool_val); \\\n\t}\n\tMUTEX_PROF_COUNTERS;\n#undef OP\n#undef EMITTER_TYPE_uint32_t\n#undef EMITTER_TYPE_uint64_t\n}\n\n#define COL(row_name, column_name, left_or_right, col_width, etype)      \\\n\temitter_col_t col_##column_name;                                     \\\n\temitter_col_init(&col_##column_name, &row_name);                     \\\n\tcol_##column_name.justify = emitter_justify_##left_or_right;         \\\n\tcol_##column_name.width = col_width;                                 \\\n\tcol_##column_name.type = emitter_type_##etype;\n\n#define COL_HDR(row_name, column_name, human, left_or_right, col_width, etype)  \\\n\tCOL(row_name, column_name, left_or_right, col_width, etype)\t         \\\n\temitter_col_t header_##column_name;                                  \\\n\temitter_col_init(&header_##column_name, &header_##row_name);         \\\n\theader_##column_name.justify = emitter_justify_##left_or_right;      \\\n\theader_##column_name.width = col_width;                              \\\n\theader_##column_name.type = emitter_type_title;                      \\\n\theader_##column_name.str_val = human ? human : #column_name;\n\n\nstatic void\nstats_arena_bins_print(emitter_t *emitter, bool mutex, unsigned i, uint64_t uptime) {\n\tsize_t page;\n\tbool in_gap, in_gap_prev;\n\tunsigned nbins, j;\n\n\tCTL_GET(\"arenas.page\", &page, size_t);\n\n\tCTL_GET(\"arenas.nbins\", &nbins, unsigned);\n\n\temitter_row_t header_row;\n\temitter_row_init(&header_row);\n\n\temitter_row_t row;\n\temitter_row_init(&row);\n\n\tCOL_HDR(row, size, NULL, right, 20, size)\n\tCOL_HDR(row, ind, NULL, right, 4, unsigned)\n\tCOL_HDR(row, allocated, NULL, right, 13, uint64)\n\tCOL_HDR(row, nmalloc, NULL, right, 13, uint64)\n\tCOL_HDR(row, nmalloc_ps, \"(#/sec)\", right, 8, uint64)\n\tCOL_HDR(row, ndalloc, NULL, right, 13, uint64)\n\tCOL_HDR(row, ndalloc_ps, \"(#/sec)\", right, 8, uint64)\n\tCOL_HDR(row, nrequests, NULL, right, 13, uint64)\n\tCOL_HDR(row, nrequests_ps, \"(#/sec)\", right, 10, uint64)\n\tCOL_HDR(row, nshards, NULL, right, 9, unsigned)\n\tCOL_HDR(row, curregs, NULL, right, 13, size)\n\tCOL_HDR(row, curslabs, NULL, right, 13, size)\n\tCOL_HDR(row, nonfull_slabs, NULL, right, 15, size)\n\tCOL_HDR(row, regs, NULL, right, 5, unsigned)\n\tCOL_HDR(row, pgs, NULL, right, 4, size)\n\t/* To buffer a right- and left-justified column. */\n\tCOL_HDR(row, justify_spacer, NULL, right, 1, title)\n\tCOL_HDR(row, util, NULL, right, 6, title)\n\tCOL_HDR(row, nfills, NULL, right, 13, uint64)\n\tCOL_HDR(row, nfills_ps, \"(#/sec)\", right, 8, uint64)\n\tCOL_HDR(row, nflushes, NULL, right, 13, uint64)\n\tCOL_HDR(row, nflushes_ps, \"(#/sec)\", right, 8, uint64)\n\tCOL_HDR(row, nslabs, NULL, right, 13, uint64)\n\tCOL_HDR(row, nreslabs, NULL, right, 13, uint64)\n\tCOL_HDR(row, nreslabs_ps, \"(#/sec)\", right, 8, uint64)\n\n\t/* Don't want to actually print the name. */\n\theader_justify_spacer.str_val = \" \";\n\tcol_justify_spacer.str_val = \" \";\n\n\temitter_col_t col_mutex64[mutex_prof_num_uint64_t_counters];\n\temitter_col_t col_mutex32[mutex_prof_num_uint32_t_counters];\n\n\temitter_col_t header_mutex64[mutex_prof_num_uint64_t_counters];\n\temitter_col_t header_mutex32[mutex_prof_num_uint32_t_counters];\n\n\tif (mutex) {\n\t\tmutex_stats_init_cols(&row, NULL, NULL, col_mutex64,\n\t\t    col_mutex32);\n\t\tmutex_stats_init_cols(&header_row, NULL, NULL, header_mutex64,\n\t\t    header_mutex32);\n\t}\n\n\t/*\n\t * We print a \"bins:\" header as part of the table row; we need to adjust\n\t * the header size column to compensate.\n\t */\n\theader_size.width -=5;\n\temitter_table_printf(emitter, \"bins:\");\n\temitter_table_row(emitter, &header_row);\n\temitter_json_array_kv_begin(emitter, \"bins\");\n\n\tfor (j = 0, in_gap = false; j < nbins; j++) {\n\t\tuint64_t nslabs;\n\t\tsize_t reg_size, slab_size, curregs;\n\t\tsize_t curslabs;\n\t\tsize_t nonfull_slabs;\n\t\tuint32_t nregs, nshards;\n\t\tuint64_t nmalloc, ndalloc, nrequests, nfills, nflushes;\n\t\tuint64_t nreslabs;\n\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.bins.0.nslabs\", i, j, &nslabs,\n\t\t    uint64_t);\n\t\tin_gap_prev = in_gap;\n\t\tin_gap = (nslabs == 0);\n\n\t\tif (in_gap_prev && !in_gap) {\n\t\t\temitter_table_printf(emitter,\n\t\t\t    \"                     ---\\n\");\n\t\t}\n\n\t\tCTL_M2_GET(\"arenas.bin.0.size\", j, &reg_size, size_t);\n\t\tCTL_M2_GET(\"arenas.bin.0.nregs\", j, &nregs, uint32_t);\n\t\tCTL_M2_GET(\"arenas.bin.0.slab_size\", j, &slab_size, size_t);\n\t\tCTL_M2_GET(\"arenas.bin.0.nshards\", j, &nshards, uint32_t);\n\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.bins.0.nmalloc\", i, j, &nmalloc,\n\t\t    uint64_t);\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.bins.0.ndalloc\", i, j, &ndalloc,\n\t\t    uint64_t);\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.bins.0.curregs\", i, j, &curregs,\n\t\t    size_t);\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.bins.0.nrequests\", i, j,\n\t\t    &nrequests, uint64_t);\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.bins.0.nfills\", i, j, &nfills,\n\t\t    uint64_t);\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.bins.0.nflushes\", i, j, &nflushes,\n\t\t    uint64_t);\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.bins.0.nreslabs\", i, j, &nreslabs,\n\t\t    uint64_t);\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.bins.0.curslabs\", i, j, &curslabs,\n\t\t    size_t);\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.bins.0.nonfull_slabs\", i, j, &nonfull_slabs,\n\t\t    size_t);\n\n\t\tif (mutex) {\n\t\t\tmutex_stats_read_arena_bin(i, j, col_mutex64,\n\t\t\t    col_mutex32, uptime);\n\t\t}\n\n\t\temitter_json_object_begin(emitter);\n\t\temitter_json_kv(emitter, \"nmalloc\", emitter_type_uint64,\n\t\t    &nmalloc);\n\t\temitter_json_kv(emitter, \"ndalloc\", emitter_type_uint64,\n\t\t    &ndalloc);\n\t\temitter_json_kv(emitter, \"curregs\", emitter_type_size,\n\t\t    &curregs);\n\t\temitter_json_kv(emitter, \"nrequests\", emitter_type_uint64,\n\t\t    &nrequests);\n\t\temitter_json_kv(emitter, \"nfills\", emitter_type_uint64,\n\t\t    &nfills);\n\t\temitter_json_kv(emitter, \"nflushes\", emitter_type_uint64,\n\t\t    &nflushes);\n\t\temitter_json_kv(emitter, \"nreslabs\", emitter_type_uint64,\n\t\t    &nreslabs);\n\t\temitter_json_kv(emitter, \"curslabs\", emitter_type_size,\n\t\t    &curslabs);\n\t\temitter_json_kv(emitter, \"nonfull_slabs\", emitter_type_size,\n\t\t    &nonfull_slabs);\n\t\tif (mutex) {\n\t\t\temitter_json_object_kv_begin(emitter, \"mutex\");\n\t\t\tmutex_stats_emit(emitter, NULL, col_mutex64,\n\t\t\t    col_mutex32);\n\t\t\temitter_json_object_end(emitter);\n\t\t}\n\t\temitter_json_object_end(emitter);\n\n\t\tsize_t availregs = nregs * curslabs;\n\t\tchar util[6];\n\t\tif (get_rate_str((uint64_t)curregs, (uint64_t)availregs, util))\n\t\t{\n\t\t\tif (availregs == 0) {\n\t\t\t\tmalloc_snprintf(util, sizeof(util), \"1\");\n\t\t\t} else if (curregs > availregs) {\n\t\t\t\t/*\n\t\t\t\t * Race detected: the counters were read in\n\t\t\t\t * separate mallctl calls and concurrent\n\t\t\t\t * operations happened in between.  In this case\n\t\t\t\t * no meaningful utilization can be computed.\n\t\t\t\t */\n\t\t\t\tmalloc_snprintf(util, sizeof(util), \" race\");\n\t\t\t} else {\n\t\t\t\tnot_reached();\n\t\t\t}\n\t\t}\n\n\t\tcol_size.size_val = reg_size;\n\t\tcol_ind.unsigned_val = j;\n\t\tcol_allocated.size_val = curregs * reg_size;\n\t\tcol_nmalloc.uint64_val = nmalloc;\n\t\tcol_nmalloc_ps.uint64_val = rate_per_second(nmalloc, uptime);\n\t\tcol_ndalloc.uint64_val = ndalloc;\n\t\tcol_ndalloc_ps.uint64_val = rate_per_second(ndalloc, uptime);\n\t\tcol_nrequests.uint64_val = nrequests;\n\t\tcol_nrequests_ps.uint64_val = rate_per_second(nrequests, uptime);\n\t\tcol_nshards.unsigned_val = nshards;\n\t\tcol_curregs.size_val = curregs;\n\t\tcol_curslabs.size_val = curslabs;\n\t\tcol_nonfull_slabs.size_val = nonfull_slabs;\n\t\tcol_regs.unsigned_val = nregs;\n\t\tcol_pgs.size_val = slab_size / page;\n\t\tcol_util.str_val = util;\n\t\tcol_nfills.uint64_val = nfills;\n\t\tcol_nfills_ps.uint64_val = rate_per_second(nfills, uptime);\n\t\tcol_nflushes.uint64_val = nflushes;\n\t\tcol_nflushes_ps.uint64_val = rate_per_second(nflushes, uptime);\n\t\tcol_nslabs.uint64_val = nslabs;\n\t\tcol_nreslabs.uint64_val = nreslabs;\n\t\tcol_nreslabs_ps.uint64_val = rate_per_second(nreslabs, uptime);\n\n\t\t/*\n\t\t * Note that mutex columns were initialized above, if mutex ==\n\t\t * true.\n\t\t */\n\n\t\temitter_table_row(emitter, &row);\n\t}\n\temitter_json_array_end(emitter); /* Close \"bins\". */\n\n\tif (in_gap) {\n\t\temitter_table_printf(emitter, \"                     ---\\n\");\n\t}\n}\n\nstatic void\nstats_arena_lextents_print(emitter_t *emitter, unsigned i, uint64_t uptime) {\n\tunsigned nbins, nlextents, j;\n\tbool in_gap, in_gap_prev;\n\n\tCTL_GET(\"arenas.nbins\", &nbins, unsigned);\n\tCTL_GET(\"arenas.nlextents\", &nlextents, unsigned);\n\n\temitter_row_t header_row;\n\temitter_row_init(&header_row);\n\temitter_row_t row;\n\temitter_row_init(&row);\n\n\tCOL_HDR(row, size, NULL, right, 20, size)\n\tCOL_HDR(row, ind, NULL, right, 4, unsigned)\n\tCOL_HDR(row, allocated, NULL, right, 13, size)\n\tCOL_HDR(row, nmalloc, NULL, right, 13, uint64)\n\tCOL_HDR(row, nmalloc_ps, \"(#/sec)\", right, 8, uint64)\n\tCOL_HDR(row, ndalloc, NULL, right, 13, uint64)\n\tCOL_HDR(row, ndalloc_ps, \"(#/sec)\", right, 8, uint64)\n\tCOL_HDR(row, nrequests, NULL, right, 13, uint64)\n\tCOL_HDR(row, nrequests_ps, \"(#/sec)\", right, 8, uint64)\n\tCOL_HDR(row, curlextents, NULL, right, 13, size)\n\n\t/* As with bins, we label the large extents table. */\n\theader_size.width -= 6;\n\temitter_table_printf(emitter, \"large:\");\n\temitter_table_row(emitter, &header_row);\n\temitter_json_array_kv_begin(emitter, \"lextents\");\n\n\tfor (j = 0, in_gap = false; j < nlextents; j++) {\n\t\tuint64_t nmalloc, ndalloc, nrequests;\n\t\tsize_t lextent_size, curlextents;\n\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.lextents.0.nmalloc\", i, j,\n\t\t    &nmalloc, uint64_t);\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.lextents.0.ndalloc\", i, j,\n\t\t    &ndalloc, uint64_t);\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.lextents.0.nrequests\", i, j,\n\t\t    &nrequests, uint64_t);\n\t\tin_gap_prev = in_gap;\n\t\tin_gap = (nrequests == 0);\n\n\t\tif (in_gap_prev && !in_gap) {\n\t\t\temitter_table_printf(emitter,\n\t\t\t    \"                     ---\\n\");\n\t\t}\n\n\t\tCTL_M2_GET(\"arenas.lextent.0.size\", j, &lextent_size, size_t);\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.lextents.0.curlextents\", i, j,\n\t\t    &curlextents, size_t);\n\n\t\temitter_json_object_begin(emitter);\n\t\temitter_json_kv(emitter, \"curlextents\", emitter_type_size,\n\t\t    &curlextents);\n\t\temitter_json_object_end(emitter);\n\n\t\tcol_size.size_val = lextent_size;\n\t\tcol_ind.unsigned_val = nbins + j;\n\t\tcol_allocated.size_val = curlextents * lextent_size;\n\t\tcol_nmalloc.uint64_val = nmalloc;\n\t\tcol_nmalloc_ps.uint64_val = rate_per_second(nmalloc, uptime);\n\t\tcol_ndalloc.uint64_val = ndalloc;\n\t\tcol_ndalloc_ps.uint64_val = rate_per_second(ndalloc, uptime);\n\t\tcol_nrequests.uint64_val = nrequests;\n\t\tcol_nrequests_ps.uint64_val = rate_per_second(nrequests, uptime);\n\t\tcol_curlextents.size_val = curlextents;\n\n\t\tif (!in_gap) {\n\t\t\temitter_table_row(emitter, &row);\n\t\t}\n\t}\n\temitter_json_array_end(emitter); /* Close \"lextents\". */\n\tif (in_gap) {\n\t\temitter_table_printf(emitter, \"                     ---\\n\");\n\t}\n}\n\nstatic void\nstats_arena_extents_print(emitter_t *emitter, unsigned i) {\n\tunsigned j;\n\tbool in_gap, in_gap_prev;\n\temitter_row_t header_row;\n\temitter_row_init(&header_row);\n\temitter_row_t row;\n\temitter_row_init(&row);\n\n\tCOL_HDR(row, size, NULL, right, 20, size)\n\tCOL_HDR(row, ind, NULL, right, 4, unsigned)\n\tCOL_HDR(row, ndirty, NULL, right, 13, size)\n\tCOL_HDR(row, dirty, NULL, right, 13, size)\n\tCOL_HDR(row, nmuzzy, NULL, right, 13, size)\n\tCOL_HDR(row, muzzy, NULL, right, 13, size)\n\tCOL_HDR(row, nretained, NULL, right, 13, size)\n\tCOL_HDR(row, retained, NULL, right, 13, size)\n\tCOL_HDR(row, ntotal, NULL, right, 13, size)\n\tCOL_HDR(row, total, NULL, right, 13, size)\n\n\t/* Label this section. */\n\theader_size.width -= 8;\n\temitter_table_printf(emitter, \"extents:\");\n\temitter_table_row(emitter, &header_row);\n\temitter_json_array_kv_begin(emitter, \"extents\");\n\n\tin_gap = false;\n\tfor (j = 0; j < SC_NPSIZES; j++) {\n\t\tsize_t ndirty, nmuzzy, nretained, total, dirty_bytes,\n\t\t    muzzy_bytes, retained_bytes, total_bytes;\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.extents.0.ndirty\", i, j,\n\t\t    &ndirty, size_t);\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.extents.0.nmuzzy\", i, j,\n\t\t    &nmuzzy, size_t);\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.extents.0.nretained\", i, j,\n\t\t    &nretained, size_t);\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.extents.0.dirty_bytes\", i, j,\n\t\t    &dirty_bytes, size_t);\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.extents.0.muzzy_bytes\", i, j,\n\t\t    &muzzy_bytes, size_t);\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.extents.0.retained_bytes\", i, j,\n\t\t    &retained_bytes, size_t);\n\t\ttotal = ndirty + nmuzzy + nretained;\n\t\ttotal_bytes = dirty_bytes + muzzy_bytes + retained_bytes;\n\n\t\tin_gap_prev = in_gap;\n\t\tin_gap = (total == 0);\n\n\t\tif (in_gap_prev && !in_gap) {\n\t\t\temitter_table_printf(emitter,\n\t\t\t    \"                     ---\\n\");\n\t\t}\n\n\t\temitter_json_object_begin(emitter);\n\t\temitter_json_kv(emitter, \"ndirty\", emitter_type_size, &ndirty);\n\t\temitter_json_kv(emitter, \"nmuzzy\", emitter_type_size, &nmuzzy);\n\t\temitter_json_kv(emitter, \"nretained\", emitter_type_size,\n\t\t    &nretained);\n\n\t\temitter_json_kv(emitter, \"dirty_bytes\", emitter_type_size,\n\t\t    &dirty_bytes);\n\t\temitter_json_kv(emitter, \"muzzy_bytes\", emitter_type_size,\n\t\t    &muzzy_bytes);\n\t\temitter_json_kv(emitter, \"retained_bytes\", emitter_type_size,\n\t\t    &retained_bytes);\n\t\temitter_json_object_end(emitter);\n\n\t\tcol_size.size_val = sz_pind2sz(j);\n\t\tcol_ind.size_val = j;\n\t\tcol_ndirty.size_val = ndirty;\n\t\tcol_dirty.size_val = dirty_bytes;\n\t\tcol_nmuzzy.size_val = nmuzzy;\n\t\tcol_muzzy.size_val = muzzy_bytes;\n\t\tcol_nretained.size_val = nretained;\n\t\tcol_retained.size_val = retained_bytes;\n\t\tcol_ntotal.size_val = total;\n\t\tcol_total.size_val = total_bytes;\n\n\t\tif (!in_gap) {\n\t\t\temitter_table_row(emitter, &row);\n\t\t}\n\t}\n\temitter_json_array_end(emitter); /* Close \"extents\". */\n\tif (in_gap) {\n\t\temitter_table_printf(emitter, \"                     ---\\n\");\n\t}\n}\n\nstatic void\nstats_arena_mutexes_print(emitter_t *emitter, unsigned arena_ind, uint64_t uptime) {\n\temitter_row_t row;\n\temitter_col_t col_name;\n\temitter_col_t col64[mutex_prof_num_uint64_t_counters];\n\temitter_col_t col32[mutex_prof_num_uint32_t_counters];\n\n\temitter_row_init(&row);\n\tmutex_stats_init_cols(&row, \"\", &col_name, col64, col32);\n\n\temitter_json_object_kv_begin(emitter, \"mutexes\");\n\temitter_table_row(emitter, &row);\n\n\tfor (mutex_prof_arena_ind_t i = 0; i < mutex_prof_num_arena_mutexes;\n\t    i++) {\n\t\tconst char *name = arena_mutex_names[i];\n\t\temitter_json_object_kv_begin(emitter, name);\n\t\tmutex_stats_read_arena(arena_ind, i, name, &col_name, col64,\n\t\t    col32, uptime);\n\t\tmutex_stats_emit(emitter, &row, col64, col32);\n\t\temitter_json_object_end(emitter); /* Close the mutex dict. */\n\t}\n\temitter_json_object_end(emitter); /* End \"mutexes\". */\n}\n\nstatic void\nstats_arena_print(emitter_t *emitter, unsigned i, bool bins, bool large,\n    bool mutex, bool extents) {\n\tunsigned nthreads;\n\tconst char *dss;\n\tssize_t dirty_decay_ms, muzzy_decay_ms;\n\tsize_t page, pactive, pdirty, pmuzzy, mapped, retained;\n\tsize_t base, internal, resident, metadata_thp, extent_avail;\n\tuint64_t dirty_npurge, dirty_nmadvise, dirty_purged;\n\tuint64_t muzzy_npurge, muzzy_nmadvise, muzzy_purged;\n\tsize_t small_allocated;\n\tuint64_t small_nmalloc, small_ndalloc, small_nrequests, small_nfills,\n\t    small_nflushes;\n\tsize_t large_allocated;\n\tuint64_t large_nmalloc, large_ndalloc, large_nrequests, large_nfills,\n\t    large_nflushes;\n\tsize_t tcache_bytes, abandoned_vm;\n\tuint64_t uptime;\n\n\tCTL_GET(\"arenas.page\", &page, size_t);\n\n\tCTL_M2_GET(\"stats.arenas.0.nthreads\", i, &nthreads, unsigned);\n\temitter_kv(emitter, \"nthreads\", \"assigned threads\",\n\t    emitter_type_unsigned, &nthreads);\n\n\tCTL_M2_GET(\"stats.arenas.0.uptime\", i, &uptime, uint64_t);\n\temitter_kv(emitter, \"uptime_ns\", \"uptime\", emitter_type_uint64,\n\t    &uptime);\n\n\tCTL_M2_GET(\"stats.arenas.0.dss\", i, &dss, const char *);\n\temitter_kv(emitter, \"dss\", \"dss allocation precedence\",\n\t    emitter_type_string, &dss);\n\n\tCTL_M2_GET(\"stats.arenas.0.dirty_decay_ms\", i, &dirty_decay_ms,\n\t    ssize_t);\n\tCTL_M2_GET(\"stats.arenas.0.muzzy_decay_ms\", i, &muzzy_decay_ms,\n\t    ssize_t);\n\tCTL_M2_GET(\"stats.arenas.0.pactive\", i, &pactive, size_t);\n\tCTL_M2_GET(\"stats.arenas.0.pdirty\", i, &pdirty, size_t);\n\tCTL_M2_GET(\"stats.arenas.0.pmuzzy\", i, &pmuzzy, size_t);\n\tCTL_M2_GET(\"stats.arenas.0.dirty_npurge\", i, &dirty_npurge, uint64_t);\n\tCTL_M2_GET(\"stats.arenas.0.dirty_nmadvise\", i, &dirty_nmadvise,\n\t    uint64_t);\n\tCTL_M2_GET(\"stats.arenas.0.dirty_purged\", i, &dirty_purged, uint64_t);\n\tCTL_M2_GET(\"stats.arenas.0.muzzy_npurge\", i, &muzzy_npurge, uint64_t);\n\tCTL_M2_GET(\"stats.arenas.0.muzzy_nmadvise\", i, &muzzy_nmadvise,\n\t    uint64_t);\n\tCTL_M2_GET(\"stats.arenas.0.muzzy_purged\", i, &muzzy_purged, uint64_t);\n\n\temitter_row_t decay_row;\n\temitter_row_init(&decay_row);\n\n\t/* JSON-style emission. */\n\temitter_json_kv(emitter, \"dirty_decay_ms\", emitter_type_ssize,\n\t    &dirty_decay_ms);\n\temitter_json_kv(emitter, \"muzzy_decay_ms\", emitter_type_ssize,\n\t    &muzzy_decay_ms);\n\n\temitter_json_kv(emitter, \"pactive\", emitter_type_size, &pactive);\n\temitter_json_kv(emitter, \"pdirty\", emitter_type_size, &pdirty);\n\temitter_json_kv(emitter, \"pmuzzy\", emitter_type_size, &pmuzzy);\n\n\temitter_json_kv(emitter, \"dirty_npurge\", emitter_type_uint64,\n\t    &dirty_npurge);\n\temitter_json_kv(emitter, \"dirty_nmadvise\", emitter_type_uint64,\n\t    &dirty_nmadvise);\n\temitter_json_kv(emitter, \"dirty_purged\", emitter_type_uint64,\n\t    &dirty_purged);\n\n\temitter_json_kv(emitter, \"muzzy_npurge\", emitter_type_uint64,\n\t    &muzzy_npurge);\n\temitter_json_kv(emitter, \"muzzy_nmadvise\", emitter_type_uint64,\n\t    &muzzy_nmadvise);\n\temitter_json_kv(emitter, \"muzzy_purged\", emitter_type_uint64,\n\t    &muzzy_purged);\n\n\t/* Table-style emission. */\n\tCOL(decay_row, decay_type, right, 9, title);\n\tcol_decay_type.str_val = \"decaying:\";\n\n\tCOL(decay_row, decay_time, right, 6, title);\n\tcol_decay_time.str_val = \"time\";\n\n\tCOL(decay_row, decay_npages, right, 13, title);\n\tcol_decay_npages.str_val = \"npages\";\n\n\tCOL(decay_row, decay_sweeps, right, 13, title);\n\tcol_decay_sweeps.str_val = \"sweeps\";\n\n\tCOL(decay_row, decay_madvises, right, 13, title);\n\tcol_decay_madvises.str_val = \"madvises\";\n\n\tCOL(decay_row, decay_purged, right, 13, title);\n\tcol_decay_purged.str_val = \"purged\";\n\n\t/* Title row. */\n\temitter_table_row(emitter, &decay_row);\n\n\t/* Dirty row. */\n\tcol_decay_type.str_val = \"dirty:\";\n\n\tif (dirty_decay_ms >= 0) {\n\t\tcol_decay_time.type = emitter_type_ssize;\n\t\tcol_decay_time.ssize_val = dirty_decay_ms;\n\t} else {\n\t\tcol_decay_time.type = emitter_type_title;\n\t\tcol_decay_time.str_val = \"N/A\";\n\t}\n\n\tcol_decay_npages.type = emitter_type_size;\n\tcol_decay_npages.size_val = pdirty;\n\n\tcol_decay_sweeps.type = emitter_type_uint64;\n\tcol_decay_sweeps.uint64_val = dirty_npurge;\n\n\tcol_decay_madvises.type = emitter_type_uint64;\n\tcol_decay_madvises.uint64_val = dirty_nmadvise;\n\n\tcol_decay_purged.type = emitter_type_uint64;\n\tcol_decay_purged.uint64_val = dirty_purged;\n\n\temitter_table_row(emitter, &decay_row);\n\n\t/* Muzzy row. */\n\tcol_decay_type.str_val = \"muzzy:\";\n\n\tif (muzzy_decay_ms >= 0) {\n\t\tcol_decay_time.type = emitter_type_ssize;\n\t\tcol_decay_time.ssize_val = muzzy_decay_ms;\n\t} else {\n\t\tcol_decay_time.type = emitter_type_title;\n\t\tcol_decay_time.str_val = \"N/A\";\n\t}\n\n\tcol_decay_npages.type = emitter_type_size;\n\tcol_decay_npages.size_val = pmuzzy;\n\n\tcol_decay_sweeps.type = emitter_type_uint64;\n\tcol_decay_sweeps.uint64_val = muzzy_npurge;\n\n\tcol_decay_madvises.type = emitter_type_uint64;\n\tcol_decay_madvises.uint64_val = muzzy_nmadvise;\n\n\tcol_decay_purged.type = emitter_type_uint64;\n\tcol_decay_purged.uint64_val = muzzy_purged;\n\n\temitter_table_row(emitter, &decay_row);\n\n\t/* Small / large / total allocation counts. */\n\temitter_row_t alloc_count_row;\n\temitter_row_init(&alloc_count_row);\n\n\tCOL(alloc_count_row, count_title, left, 21, title);\n\tcol_count_title.str_val = \"\";\n\n\tCOL(alloc_count_row, count_allocated, right, 16, title);\n\tcol_count_allocated.str_val = \"allocated\";\n\n\tCOL(alloc_count_row, count_nmalloc, right, 16, title);\n\tcol_count_nmalloc.str_val = \"nmalloc\";\n\tCOL(alloc_count_row, count_nmalloc_ps, right, 8, title);\n\tcol_count_nmalloc_ps.str_val = \"(#/sec)\";\n\n\tCOL(alloc_count_row, count_ndalloc, right, 16, title);\n\tcol_count_ndalloc.str_val = \"ndalloc\";\n\tCOL(alloc_count_row, count_ndalloc_ps, right, 8, title);\n\tcol_count_ndalloc_ps.str_val = \"(#/sec)\";\n\n\tCOL(alloc_count_row, count_nrequests, right, 16, title);\n\tcol_count_nrequests.str_val = \"nrequests\";\n\tCOL(alloc_count_row, count_nrequests_ps, right, 10, title);\n\tcol_count_nrequests_ps.str_val = \"(#/sec)\";\n\n\tCOL(alloc_count_row, count_nfills, right, 16, title);\n\tcol_count_nfills.str_val = \"nfill\";\n\tCOL(alloc_count_row, count_nfills_ps, right, 10, title);\n\tcol_count_nfills_ps.str_val = \"(#/sec)\";\n\n\tCOL(alloc_count_row, count_nflushes, right, 16, title);\n\tcol_count_nflushes.str_val = \"nflush\";\n\tCOL(alloc_count_row, count_nflushes_ps, right, 10, title);\n\tcol_count_nflushes_ps.str_val = \"(#/sec)\";\n\n\temitter_table_row(emitter, &alloc_count_row);\n\n\tcol_count_nmalloc_ps.type = emitter_type_uint64;\n\tcol_count_ndalloc_ps.type = emitter_type_uint64;\n\tcol_count_nrequests_ps.type = emitter_type_uint64;\n\tcol_count_nfills_ps.type = emitter_type_uint64;\n\tcol_count_nflushes_ps.type = emitter_type_uint64;\n\n#define GET_AND_EMIT_ALLOC_STAT(small_or_large, name, valtype)\t\t\\\n\tCTL_M2_GET(\"stats.arenas.0.\" #small_or_large \".\" #name, i,\t\\\n\t    &small_or_large##_##name, valtype##_t);\t\t\t\\\n\temitter_json_kv(emitter, #name, emitter_type_##valtype,\t\t\\\n\t    &small_or_large##_##name);\t\t\t\t\t\\\n\tcol_count_##name.type = emitter_type_##valtype;\t\t\\\n\tcol_count_##name.valtype##_val = small_or_large##_##name;\n\n\temitter_json_object_kv_begin(emitter, \"small\");\n\tcol_count_title.str_val = \"small:\";\n\n\tGET_AND_EMIT_ALLOC_STAT(small, allocated, size)\n\tGET_AND_EMIT_ALLOC_STAT(small, nmalloc, uint64)\n\tcol_count_nmalloc_ps.uint64_val =\n\t    rate_per_second(col_count_nmalloc.uint64_val, uptime);\n\tGET_AND_EMIT_ALLOC_STAT(small, ndalloc, uint64)\n\tcol_count_ndalloc_ps.uint64_val =\n\t    rate_per_second(col_count_ndalloc.uint64_val, uptime);\n\tGET_AND_EMIT_ALLOC_STAT(small, nrequests, uint64)\n\tcol_count_nrequests_ps.uint64_val =\n\t    rate_per_second(col_count_nrequests.uint64_val, uptime);\n\tGET_AND_EMIT_ALLOC_STAT(small, nfills, uint64)\n\tcol_count_nfills_ps.uint64_val =\n\t    rate_per_second(col_count_nfills.uint64_val, uptime);\n\tGET_AND_EMIT_ALLOC_STAT(small, nflushes, uint64)\n\tcol_count_nflushes_ps.uint64_val =\n\t    rate_per_second(col_count_nflushes.uint64_val, uptime);\n\n\temitter_table_row(emitter, &alloc_count_row);\n\temitter_json_object_end(emitter); /* Close \"small\". */\n\n\temitter_json_object_kv_begin(emitter, \"large\");\n\tcol_count_title.str_val = \"large:\";\n\n\tGET_AND_EMIT_ALLOC_STAT(large, allocated, size)\n\tGET_AND_EMIT_ALLOC_STAT(large, nmalloc, uint64)\n\tcol_count_nmalloc_ps.uint64_val =\n\t    rate_per_second(col_count_nmalloc.uint64_val, uptime);\n\tGET_AND_EMIT_ALLOC_STAT(large, ndalloc, uint64)\n\tcol_count_ndalloc_ps.uint64_val =\n\t    rate_per_second(col_count_ndalloc.uint64_val, uptime);\n\tGET_AND_EMIT_ALLOC_STAT(large, nrequests, uint64)\n\tcol_count_nrequests_ps.uint64_val =\n\t    rate_per_second(col_count_nrequests.uint64_val, uptime);\n\tGET_AND_EMIT_ALLOC_STAT(large, nfills, uint64)\n\tcol_count_nfills_ps.uint64_val =\n\t    rate_per_second(col_count_nfills.uint64_val, uptime);\n\tGET_AND_EMIT_ALLOC_STAT(large, nflushes, uint64)\n\tcol_count_nflushes_ps.uint64_val =\n\t    rate_per_second(col_count_nflushes.uint64_val, uptime);\n\n\temitter_table_row(emitter, &alloc_count_row);\n\temitter_json_object_end(emitter); /* Close \"large\". */\n\n#undef GET_AND_EMIT_ALLOC_STAT\n\n\t/* Aggregated small + large stats are emitter only in table mode. */\n\tcol_count_title.str_val = \"total:\";\n\tcol_count_allocated.size_val = small_allocated + large_allocated;\n\tcol_count_nmalloc.uint64_val = small_nmalloc + large_nmalloc;\n\tcol_count_ndalloc.uint64_val = small_ndalloc + large_ndalloc;\n\tcol_count_nrequests.uint64_val = small_nrequests + large_nrequests;\n\tcol_count_nfills.uint64_val = small_nfills + large_nfills;\n\tcol_count_nflushes.uint64_val = small_nflushes + large_nflushes;\n\tcol_count_nmalloc_ps.uint64_val =\n\t    rate_per_second(col_count_nmalloc.uint64_val, uptime);\n\tcol_count_ndalloc_ps.uint64_val =\n\t    rate_per_second(col_count_ndalloc.uint64_val, uptime);\n\tcol_count_nrequests_ps.uint64_val =\n\t    rate_per_second(col_count_nrequests.uint64_val, uptime);\n\tcol_count_nfills_ps.uint64_val =\n\t    rate_per_second(col_count_nfills.uint64_val, uptime);\n\tcol_count_nflushes_ps.uint64_val =\n\t    rate_per_second(col_count_nflushes.uint64_val, uptime);\n\temitter_table_row(emitter, &alloc_count_row);\n\n\temitter_row_t mem_count_row;\n\temitter_row_init(&mem_count_row);\n\n\temitter_col_t mem_count_title;\n\temitter_col_init(&mem_count_title, &mem_count_row);\n\tmem_count_title.justify = emitter_justify_left;\n\tmem_count_title.width = 21;\n\tmem_count_title.type = emitter_type_title;\n\tmem_count_title.str_val = \"\";\n\n\temitter_col_t mem_count_val;\n\temitter_col_init(&mem_count_val, &mem_count_row);\n\tmem_count_val.justify = emitter_justify_right;\n\tmem_count_val.width = 16;\n\tmem_count_val.type = emitter_type_title;\n\tmem_count_val.str_val = \"\";\n\n\temitter_table_row(emitter, &mem_count_row);\n\tmem_count_val.type = emitter_type_size;\n\n\t/* Active count in bytes is emitted only in table mode. */\n\tmem_count_title.str_val = \"active:\";\n\tmem_count_val.size_val = pactive * page;\n\temitter_table_row(emitter, &mem_count_row);\n\n#define GET_AND_EMIT_MEM_STAT(stat)\t\t\t\t\t\\\n\tCTL_M2_GET(\"stats.arenas.0.\"#stat, i, &stat, size_t);\t\t\\\n\temitter_json_kv(emitter, #stat, emitter_type_size, &stat);\t\\\n\tmem_count_title.str_val = #stat\":\";\t\t\t\t\\\n\tmem_count_val.size_val = stat;\t\t\t\t\t\\\n\temitter_table_row(emitter, &mem_count_row);\n\n\tGET_AND_EMIT_MEM_STAT(mapped)\n\tGET_AND_EMIT_MEM_STAT(retained)\n\tGET_AND_EMIT_MEM_STAT(base)\n\tGET_AND_EMIT_MEM_STAT(internal)\n\tGET_AND_EMIT_MEM_STAT(metadata_thp)\n\tGET_AND_EMIT_MEM_STAT(tcache_bytes)\n\tGET_AND_EMIT_MEM_STAT(resident)\n\tGET_AND_EMIT_MEM_STAT(abandoned_vm)\n\tGET_AND_EMIT_MEM_STAT(extent_avail)\n#undef GET_AND_EMIT_MEM_STAT\n\n\tif (mutex) {\n\t\tstats_arena_mutexes_print(emitter, i, uptime);\n\t}\n\tif (bins) {\n\t\tstats_arena_bins_print(emitter, mutex, i, uptime);\n\t}\n\tif (large) {\n\t\tstats_arena_lextents_print(emitter, i, uptime);\n\t}\n\tif (extents) {\n\t\tstats_arena_extents_print(emitter, i);\n\t}\n}\n\nstatic void\nstats_general_print(emitter_t *emitter) {\n\tconst char *cpv;\n\tbool bv, bv2;\n\tunsigned uv;\n\tuint32_t u32v;\n\tuint64_t u64v;\n\tssize_t ssv, ssv2;\n\tsize_t sv, bsz, usz, ssz, sssz, cpsz;\n\n\tbsz = sizeof(bool);\n\tusz = sizeof(unsigned);\n\tssz = sizeof(size_t);\n\tsssz = sizeof(ssize_t);\n\tcpsz = sizeof(const char *);\n\n\tCTL_GET(\"version\", &cpv, const char *);\n\temitter_kv(emitter, \"version\", \"Version\", emitter_type_string, &cpv);\n\n\t/* config. */\n\temitter_dict_begin(emitter, \"config\", \"Build-time option settings\");\n#define CONFIG_WRITE_BOOL(name)\t\t\t\t\t\t\\\n\tdo {\t\t\t\t\t\t\t\t\\\n\t\tCTL_GET(\"config.\"#name, &bv, bool);\t\t\t\\\n\t\temitter_kv(emitter, #name, \"config.\"#name,\t\t\\\n\t\t    emitter_type_bool, &bv);\t\t\t\t\\\n\t} while (0)\n\n\tCONFIG_WRITE_BOOL(cache_oblivious);\n\tCONFIG_WRITE_BOOL(debug);\n\tCONFIG_WRITE_BOOL(fill);\n\tCONFIG_WRITE_BOOL(lazy_lock);\n\temitter_kv(emitter, \"malloc_conf\", \"config.malloc_conf\",\n\t    emitter_type_string, &config_malloc_conf);\n\n\tCONFIG_WRITE_BOOL(opt_safety_checks);\n\tCONFIG_WRITE_BOOL(prof);\n\tCONFIG_WRITE_BOOL(prof_libgcc);\n\tCONFIG_WRITE_BOOL(prof_libunwind);\n\tCONFIG_WRITE_BOOL(stats);\n\tCONFIG_WRITE_BOOL(utrace);\n\tCONFIG_WRITE_BOOL(xmalloc);\n#undef CONFIG_WRITE_BOOL\n\temitter_dict_end(emitter); /* Close \"config\" dict. */\n\n\t/* opt. */\n#define OPT_WRITE(name, var, size, emitter_type)\t\t\t\\\n\tif (je_mallctl(\"opt.\"name, (void *)&var, &size, NULL, 0) ==\t\\\n\t    0) {\t\t\t\t\t\t\t\\\n\t\temitter_kv(emitter, name, \"opt.\"name, emitter_type,\t\\\n\t\t    &var);\t\t\t\t\t\t\\\n\t}\n\n#define OPT_WRITE_MUTABLE(name, var1, var2, size, emitter_type,\t\t\\\n    altname)\t\t\t\t\t\t\t\t\\\n\tif (je_mallctl(\"opt.\"name, (void *)&var1, &size, NULL, 0) ==\t\\\n\t    0 && je_mallctl(altname, (void *)&var2, &size, NULL, 0)\t\\\n\t    == 0) {\t\t\t\t\t\t\t\\\n\t\temitter_kv_note(emitter, name, \"opt.\"name,\t\t\\\n\t\t    emitter_type, &var1, altname, emitter_type,\t\t\\\n\t\t    &var2);\t\t\t\t\t\t\\\n\t}\n\n#define OPT_WRITE_BOOL(name) OPT_WRITE(name, bv, bsz, emitter_type_bool)\n#define OPT_WRITE_BOOL_MUTABLE(name, altname)\t\t\t\t\\\n\tOPT_WRITE_MUTABLE(name, bv, bv2, bsz, emitter_type_bool, altname)\n\n#define OPT_WRITE_UNSIGNED(name)\t\t\t\t\t\\\n\tOPT_WRITE(name, uv, usz, emitter_type_unsigned)\n\n#define OPT_WRITE_SIZE_T(name)\t\t\t\t\t\t\\\n\tOPT_WRITE(name, sv, ssz, emitter_type_size)\n#define OPT_WRITE_SSIZE_T(name)\t\t\t\t\t\t\\\n\tOPT_WRITE(name, ssv, sssz, emitter_type_ssize)\n#define OPT_WRITE_SSIZE_T_MUTABLE(name, altname)\t\t\t\\\n\tOPT_WRITE_MUTABLE(name, ssv, ssv2, sssz, emitter_type_ssize,\t\\\n\t    altname)\n\n#define OPT_WRITE_CHAR_P(name)\t\t\t\t\t\t\\\n\tOPT_WRITE(name, cpv, cpsz, emitter_type_string)\n\n\temitter_dict_begin(emitter, \"opt\", \"Run-time option settings\");\n\n\tOPT_WRITE_BOOL(\"abort\")\n\tOPT_WRITE_BOOL(\"abort_conf\")\n\tOPT_WRITE_BOOL(\"confirm_conf\")\n\tOPT_WRITE_BOOL(\"retain\")\n\tOPT_WRITE_CHAR_P(\"dss\")\n\tOPT_WRITE_UNSIGNED(\"narenas\")\n\tOPT_WRITE_CHAR_P(\"percpu_arena\")\n\tOPT_WRITE_SIZE_T(\"oversize_threshold\")\n\tOPT_WRITE_CHAR_P(\"metadata_thp\")\n\tOPT_WRITE_BOOL_MUTABLE(\"background_thread\", \"background_thread\")\n\tOPT_WRITE_SSIZE_T_MUTABLE(\"dirty_decay_ms\", \"arenas.dirty_decay_ms\")\n\tOPT_WRITE_SSIZE_T_MUTABLE(\"muzzy_decay_ms\", \"arenas.muzzy_decay_ms\")\n\tOPT_WRITE_SIZE_T(\"lg_extent_max_active_fit\")\n\tOPT_WRITE_CHAR_P(\"junk\")\n\tOPT_WRITE_BOOL(\"zero\")\n\tOPT_WRITE_BOOL(\"utrace\")\n\tOPT_WRITE_BOOL(\"xmalloc\")\n\tOPT_WRITE_BOOL(\"tcache\")\n\tOPT_WRITE_SSIZE_T(\"lg_tcache_max\")\n\tOPT_WRITE_CHAR_P(\"thp\")\n\tOPT_WRITE_BOOL(\"prof\")\n\tOPT_WRITE_CHAR_P(\"prof_prefix\")\n\tOPT_WRITE_BOOL_MUTABLE(\"prof_active\", \"prof.active\")\n\tOPT_WRITE_BOOL_MUTABLE(\"prof_thread_active_init\",\n\t    \"prof.thread_active_init\")\n\tOPT_WRITE_SSIZE_T_MUTABLE(\"lg_prof_sample\", \"prof.lg_sample\")\n\tOPT_WRITE_BOOL(\"prof_accum\")\n\tOPT_WRITE_SSIZE_T(\"lg_prof_interval\")\n\tOPT_WRITE_BOOL(\"prof_gdump\")\n\tOPT_WRITE_BOOL(\"prof_final\")\n\tOPT_WRITE_BOOL(\"prof_leak\")\n\tOPT_WRITE_BOOL(\"stats_print\")\n\tOPT_WRITE_CHAR_P(\"stats_print_opts\")\n\n\temitter_dict_end(emitter);\n\n#undef OPT_WRITE\n#undef OPT_WRITE_MUTABLE\n#undef OPT_WRITE_BOOL\n#undef OPT_WRITE_BOOL_MUTABLE\n#undef OPT_WRITE_UNSIGNED\n#undef OPT_WRITE_SSIZE_T\n#undef OPT_WRITE_SSIZE_T_MUTABLE\n#undef OPT_WRITE_CHAR_P\n\n\t/* prof. */\n\tif (config_prof) {\n\t\temitter_dict_begin(emitter, \"prof\", \"Profiling settings\");\n\n\t\tCTL_GET(\"prof.thread_active_init\", &bv, bool);\n\t\temitter_kv(emitter, \"thread_active_init\",\n\t\t    \"prof.thread_active_init\", emitter_type_bool, &bv);\n\n\t\tCTL_GET(\"prof.active\", &bv, bool);\n\t\temitter_kv(emitter, \"active\", \"prof.active\", emitter_type_bool,\n\t\t    &bv);\n\n\t\tCTL_GET(\"prof.gdump\", &bv, bool);\n\t\temitter_kv(emitter, \"gdump\", \"prof.gdump\", emitter_type_bool,\n\t\t    &bv);\n\n\t\tCTL_GET(\"prof.interval\", &u64v, uint64_t);\n\t\temitter_kv(emitter, \"interval\", \"prof.interval\",\n\t\t    emitter_type_uint64, &u64v);\n\n\t\tCTL_GET(\"prof.lg_sample\", &ssv, ssize_t);\n\t\temitter_kv(emitter, \"lg_sample\", \"prof.lg_sample\",\n\t\t    emitter_type_ssize, &ssv);\n\n\t\temitter_dict_end(emitter); /* Close \"prof\". */\n\t}\n\n\t/* arenas. */\n\t/*\n\t * The json output sticks arena info into an \"arenas\" dict; the table\n\t * output puts them at the top-level.\n\t */\n\temitter_json_object_kv_begin(emitter, \"arenas\");\n\n\tCTL_GET(\"arenas.narenas\", &uv, unsigned);\n\temitter_kv(emitter, \"narenas\", \"Arenas\", emitter_type_unsigned, &uv);\n\n\t/*\n\t * Decay settings are emitted only in json mode; in table mode, they're\n\t * emitted as notes with the opt output, above.\n\t */\n\tCTL_GET(\"arenas.dirty_decay_ms\", &ssv, ssize_t);\n\temitter_json_kv(emitter, \"dirty_decay_ms\", emitter_type_ssize, &ssv);\n\n\tCTL_GET(\"arenas.muzzy_decay_ms\", &ssv, ssize_t);\n\temitter_json_kv(emitter, \"muzzy_decay_ms\", emitter_type_ssize, &ssv);\n\n\tCTL_GET(\"arenas.quantum\", &sv, size_t);\n\temitter_kv(emitter, \"quantum\", \"Quantum size\", emitter_type_size, &sv);\n\n\tCTL_GET(\"arenas.page\", &sv, size_t);\n\temitter_kv(emitter, \"page\", \"Page size\", emitter_type_size, &sv);\n\n\tif (je_mallctl(\"arenas.tcache_max\", (void *)&sv, &ssz, NULL, 0) == 0) {\n\t\temitter_kv(emitter, \"tcache_max\",\n\t\t    \"Maximum thread-cached size class\", emitter_type_size, &sv);\n\t}\n\n\tunsigned nbins;\n\tCTL_GET(\"arenas.nbins\", &nbins, unsigned);\n\temitter_kv(emitter, \"nbins\", \"Number of bin size classes\",\n\t    emitter_type_unsigned, &nbins);\n\n\tunsigned nhbins;\n\tCTL_GET(\"arenas.nhbins\", &nhbins, unsigned);\n\temitter_kv(emitter, \"nhbins\", \"Number of thread-cache bin size classes\",\n\t    emitter_type_unsigned, &nhbins);\n\n\t/*\n\t * We do enough mallctls in a loop that we actually want to omit them\n\t * (not just omit the printing).\n\t */\n\tif (emitter->output == emitter_output_json) {\n\t\temitter_json_array_kv_begin(emitter, \"bin\");\n\t\tfor (unsigned i = 0; i < nbins; i++) {\n\t\t\temitter_json_object_begin(emitter);\n\n\t\t\tCTL_M2_GET(\"arenas.bin.0.size\", i, &sv, size_t);\n\t\t\temitter_json_kv(emitter, \"size\", emitter_type_size,\n\t\t\t    &sv);\n\n\t\t\tCTL_M2_GET(\"arenas.bin.0.nregs\", i, &u32v, uint32_t);\n\t\t\temitter_json_kv(emitter, \"nregs\", emitter_type_uint32,\n\t\t\t    &u32v);\n\n\t\t\tCTL_M2_GET(\"arenas.bin.0.slab_size\", i, &sv, size_t);\n\t\t\temitter_json_kv(emitter, \"slab_size\", emitter_type_size,\n\t\t\t    &sv);\n\n\t\t\tCTL_M2_GET(\"arenas.bin.0.nshards\", i, &u32v, uint32_t);\n\t\t\temitter_json_kv(emitter, \"nshards\", emitter_type_uint32,\n\t\t\t    &u32v);\n\n\t\t\temitter_json_object_end(emitter);\n\t\t}\n\t\temitter_json_array_end(emitter); /* Close \"bin\". */\n\t}\n\n\tunsigned nlextents;\n\tCTL_GET(\"arenas.nlextents\", &nlextents, unsigned);\n\temitter_kv(emitter, \"nlextents\", \"Number of large size classes\",\n\t    emitter_type_unsigned, &nlextents);\n\n\tif (emitter->output == emitter_output_json) {\n\t\temitter_json_array_kv_begin(emitter, \"lextent\");\n\t\tfor (unsigned i = 0; i < nlextents; i++) {\n\t\t\temitter_json_object_begin(emitter);\n\n\t\t\tCTL_M2_GET(\"arenas.lextent.0.size\", i, &sv, size_t);\n\t\t\temitter_json_kv(emitter, \"size\", emitter_type_size,\n\t\t\t    &sv);\n\n\t\t\temitter_json_object_end(emitter);\n\t\t}\n\t\temitter_json_array_end(emitter); /* Close \"lextent\". */\n\t}\n\n\temitter_json_object_end(emitter); /* Close \"arenas\" */\n}\n\nstatic void\nstats_print_helper(emitter_t *emitter, bool merged, bool destroyed,\n    bool unmerged, bool bins, bool large, bool mutex, bool extents) {\n\t/*\n\t * These should be deleted.  We keep them around for a while, to aid in\n\t * the transition to the emitter code.\n\t */\n\tsize_t allocated, active, metadata, metadata_thp, resident, mapped,\n\t    retained;\n\tsize_t num_background_threads;\n\tuint64_t background_thread_num_runs, background_thread_run_interval;\n\n\tCTL_GET(\"stats.allocated\", &allocated, size_t);\n\tCTL_GET(\"stats.active\", &active, size_t);\n\tCTL_GET(\"stats.metadata\", &metadata, size_t);\n\tCTL_GET(\"stats.metadata_thp\", &metadata_thp, size_t);\n\tCTL_GET(\"stats.resident\", &resident, size_t);\n\tCTL_GET(\"stats.mapped\", &mapped, size_t);\n\tCTL_GET(\"stats.retained\", &retained, size_t);\n\n\tif (have_background_thread) {\n\t\tCTL_GET(\"stats.background_thread.num_threads\",\n\t\t    &num_background_threads, size_t);\n\t\tCTL_GET(\"stats.background_thread.num_runs\",\n\t\t    &background_thread_num_runs, uint64_t);\n\t\tCTL_GET(\"stats.background_thread.run_interval\",\n\t\t    &background_thread_run_interval, uint64_t);\n\t} else {\n\t\tnum_background_threads = 0;\n\t\tbackground_thread_num_runs = 0;\n\t\tbackground_thread_run_interval = 0;\n\t}\n\n\t/* Generic global stats. */\n\temitter_json_object_kv_begin(emitter, \"stats\");\n\temitter_json_kv(emitter, \"allocated\", emitter_type_size, &allocated);\n\temitter_json_kv(emitter, \"active\", emitter_type_size, &active);\n\temitter_json_kv(emitter, \"metadata\", emitter_type_size, &metadata);\n\temitter_json_kv(emitter, \"metadata_thp\", emitter_type_size,\n\t    &metadata_thp);\n\temitter_json_kv(emitter, \"resident\", emitter_type_size, &resident);\n\temitter_json_kv(emitter, \"mapped\", emitter_type_size, &mapped);\n\temitter_json_kv(emitter, \"retained\", emitter_type_size, &retained);\n\n\temitter_table_printf(emitter, \"Allocated: %zu, active: %zu, \"\n\t    \"metadata: %zu (n_thp %zu), resident: %zu, mapped: %zu, \"\n\t    \"retained: %zu\\n\", allocated, active, metadata, metadata_thp,\n\t    resident, mapped, retained);\n\n\t/* Background thread stats. */\n\temitter_json_object_kv_begin(emitter, \"background_thread\");\n\temitter_json_kv(emitter, \"num_threads\", emitter_type_size,\n\t    &num_background_threads);\n\temitter_json_kv(emitter, \"num_runs\", emitter_type_uint64,\n\t    &background_thread_num_runs);\n\temitter_json_kv(emitter, \"run_interval\", emitter_type_uint64,\n\t    &background_thread_run_interval);\n\temitter_json_object_end(emitter); /* Close \"background_thread\". */\n\n\temitter_table_printf(emitter, \"Background threads: %zu, \"\n\t    \"num_runs: %\"FMTu64\", run_interval: %\"FMTu64\" ns\\n\",\n\t    num_background_threads, background_thread_num_runs,\n\t    background_thread_run_interval);\n\n\tif (mutex) {\n\t\temitter_row_t row;\n\t\temitter_col_t name;\n\t\temitter_col_t col64[mutex_prof_num_uint64_t_counters];\n\t\temitter_col_t col32[mutex_prof_num_uint32_t_counters];\n\t\tuint64_t uptime;\n\n\t\temitter_row_init(&row);\n\t\tmutex_stats_init_cols(&row, \"\", &name, col64, col32);\n\n\t\temitter_table_row(emitter, &row);\n\t\temitter_json_object_kv_begin(emitter, \"mutexes\");\n\n\t\tCTL_M2_GET(\"stats.arenas.0.uptime\", 0, &uptime, uint64_t);\n\n\t\tfor (int i = 0; i < mutex_prof_num_global_mutexes; i++) {\n\t\t\tmutex_stats_read_global(global_mutex_names[i], &name,\n\t\t\t    col64, col32, uptime);\n\t\t\temitter_json_object_kv_begin(emitter, global_mutex_names[i]);\n\t\t\tmutex_stats_emit(emitter, &row, col64, col32);\n\t\t\temitter_json_object_end(emitter);\n\t\t}\n\n\t\temitter_json_object_end(emitter); /* Close \"mutexes\". */\n\t}\n\n\temitter_json_object_end(emitter); /* Close \"stats\". */\n\n\tif (merged || destroyed || unmerged) {\n\t\tunsigned narenas;\n\n\t\temitter_json_object_kv_begin(emitter, \"stats.arenas\");\n\n\t\tCTL_GET(\"arenas.narenas\", &narenas, unsigned);\n\t\tsize_t mib[3];\n\t\tsize_t miblen = sizeof(mib) / sizeof(size_t);\n\t\tsize_t sz;\n\t\tVARIABLE_ARRAY(bool, initialized, narenas);\n\t\tbool destroyed_initialized;\n\t\tunsigned i, j, ninitialized;\n\n\t\txmallctlnametomib(\"arena.0.initialized\", mib, &miblen);\n\t\tfor (i = ninitialized = 0; i < narenas; i++) {\n\t\t\tmib[1] = i;\n\t\t\tsz = sizeof(bool);\n\t\t\txmallctlbymib(mib, miblen, &initialized[i], &sz,\n\t\t\t    NULL, 0);\n\t\t\tif (initialized[i]) {\n\t\t\t\tninitialized++;\n\t\t\t}\n\t\t}\n\t\tmib[1] = MALLCTL_ARENAS_DESTROYED;\n\t\tsz = sizeof(bool);\n\t\txmallctlbymib(mib, miblen, &destroyed_initialized, &sz,\n\t\t    NULL, 0);\n\n\t\t/* Merged stats. */\n\t\tif (merged && (ninitialized > 1 || !unmerged)) {\n\t\t\t/* Print merged arena stats. */\n\t\t\temitter_table_printf(emitter, \"Merged arenas stats:\\n\");\n\t\t\temitter_json_object_kv_begin(emitter, \"merged\");\n\t\t\tstats_arena_print(emitter, MALLCTL_ARENAS_ALL, bins,\n\t\t\t    large, mutex, extents);\n\t\t\temitter_json_object_end(emitter); /* Close \"merged\". */\n\t\t}\n\n\t\t/* Destroyed stats. */\n\t\tif (destroyed_initialized && destroyed) {\n\t\t\t/* Print destroyed arena stats. */\n\t\t\temitter_table_printf(emitter,\n\t\t\t    \"Destroyed arenas stats:\\n\");\n\t\t\temitter_json_object_kv_begin(emitter, \"destroyed\");\n\t\t\tstats_arena_print(emitter, MALLCTL_ARENAS_DESTROYED,\n\t\t\t    bins, large, mutex, extents);\n\t\t\temitter_json_object_end(emitter); /* Close \"destroyed\". */\n\t\t}\n\n\t\t/* Unmerged stats. */\n\t\tif (unmerged) {\n\t\t\tfor (i = j = 0; i < narenas; i++) {\n\t\t\t\tif (initialized[i]) {\n\t\t\t\t\tchar arena_ind_str[20];\n\t\t\t\t\tmalloc_snprintf(arena_ind_str,\n\t\t\t\t\t    sizeof(arena_ind_str), \"%u\", i);\n\t\t\t\t\temitter_json_object_kv_begin(emitter,\n\t\t\t\t\t    arena_ind_str);\n\t\t\t\t\temitter_table_printf(emitter,\n\t\t\t\t\t    \"arenas[%s]:\\n\", arena_ind_str);\n\t\t\t\t\tstats_arena_print(emitter, i, bins,\n\t\t\t\t\t    large, mutex, extents);\n\t\t\t\t\t/* Close \"<arena-ind>\". */\n\t\t\t\t\temitter_json_object_end(emitter);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\temitter_json_object_end(emitter); /* Close \"stats.arenas\". */\n\t}\n}\n\nvoid\nstats_print(void (*write_cb)(void *, const char *), void *cbopaque,\n    const char *opts) {\n\tint err;\n\tuint64_t epoch;\n\tsize_t u64sz;\n#define OPTION(o, v, d, s) bool v = d;\n\tSTATS_PRINT_OPTIONS\n#undef OPTION\n\n\t/*\n\t * Refresh stats, in case mallctl() was called by the application.\n\t *\n\t * Check for OOM here, since refreshing the ctl cache can trigger\n\t * allocation.  In practice, none of the subsequent mallctl()-related\n\t * calls in this function will cause OOM if this one succeeds.\n\t * */\n\tepoch = 1;\n\tu64sz = sizeof(uint64_t);\n\terr = je_mallctl(\"epoch\", (void *)&epoch, &u64sz, (void *)&epoch,\n\t    sizeof(uint64_t));\n\tif (err != 0) {\n\t\tif (err == EAGAIN) {\n\t\t\tmalloc_write(\"<jemalloc>: Memory allocation failure in \"\n\t\t\t    \"mallctl(\\\"epoch\\\", ...)\\n\");\n\t\t\treturn;\n\t\t}\n\t\tmalloc_write(\"<jemalloc>: Failure in mallctl(\\\"epoch\\\", \"\n\t\t    \"...)\\n\");\n\t\tabort();\n\t}\n\n\tif (opts != NULL) {\n\t\tfor (unsigned i = 0; opts[i] != '\\0'; i++) {\n\t\t\tswitch (opts[i]) {\n#define OPTION(o, v, d, s) case o: v = s; break;\n\t\t\t\tSTATS_PRINT_OPTIONS\n#undef OPTION\n\t\t\tdefault:;\n\t\t\t}\n\t\t}\n\t}\n\n\temitter_t emitter;\n\temitter_init(&emitter,\n\t    json ? emitter_output_json : emitter_output_table, write_cb,\n\t    cbopaque);\n\temitter_begin(&emitter);\n\temitter_table_printf(&emitter, \"___ Begin jemalloc statistics ___\\n\");\n\temitter_json_object_kv_begin(&emitter, \"jemalloc\");\n\n\tif (general) {\n\t\tstats_general_print(&emitter);\n\t}\n\tif (config_stats) {\n\t\tstats_print_helper(&emitter, merged, destroyed, unmerged,\n\t\t    bins, large, mutex, extents);\n\t}\n\n\temitter_json_object_end(&emitter); /* Closes the \"jemalloc\" dict. */\n\temitter_table_printf(&emitter, \"--- End jemalloc statistics ---\\n\");\n\temitter_end(&emitter);\n}\n"
  },
  {
    "path": "deps/jemalloc/src/sz.c",
    "content": "#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/sz.h\"\n\nJEMALLOC_ALIGNED(CACHELINE)\nsize_t sz_pind2sz_tab[SC_NPSIZES+1];\n\nstatic void\nsz_boot_pind2sz_tab(const sc_data_t *sc_data) {\n\tint pind = 0;\n\tfor (unsigned i = 0; i < SC_NSIZES; i++) {\n\t\tconst sc_t *sc = &sc_data->sc[i];\n\t\tif (sc->psz) {\n\t\t\tsz_pind2sz_tab[pind] = (ZU(1) << sc->lg_base)\n\t\t\t    + (ZU(sc->ndelta) << sc->lg_delta);\n\t\t\tpind++;\n\t\t}\n\t}\n\tfor (int i = pind; i <= (int)SC_NPSIZES; i++) {\n\t\tsz_pind2sz_tab[pind] = sc_data->large_maxclass + PAGE;\n\t}\n}\n\nJEMALLOC_ALIGNED(CACHELINE)\nsize_t sz_index2size_tab[SC_NSIZES];\n\nstatic void\nsz_boot_index2size_tab(const sc_data_t *sc_data) {\n\tfor (unsigned i = 0; i < SC_NSIZES; i++) {\n\t\tconst sc_t *sc = &sc_data->sc[i];\n\t\tsz_index2size_tab[i] = (ZU(1) << sc->lg_base)\n\t\t    + (ZU(sc->ndelta) << (sc->lg_delta));\n\t}\n}\n\n/*\n * To keep this table small, we divide sizes by the tiny min size, which gives\n * the smallest interval for which the result can change.\n */\nJEMALLOC_ALIGNED(CACHELINE)\nuint8_t sz_size2index_tab[(SC_LOOKUP_MAXCLASS >> SC_LG_TINY_MIN) + 1];\n\nstatic void\nsz_boot_size2index_tab(const sc_data_t *sc_data) {\n\tsize_t dst_max = (SC_LOOKUP_MAXCLASS >> SC_LG_TINY_MIN) + 1;\n\tsize_t dst_ind = 0;\n\tfor (unsigned sc_ind = 0; sc_ind < SC_NSIZES && dst_ind < dst_max;\n\t    sc_ind++) {\n\t\tconst sc_t *sc = &sc_data->sc[sc_ind];\n\t\tsize_t sz = (ZU(1) << sc->lg_base)\n\t\t    + (ZU(sc->ndelta) << sc->lg_delta);\n\t\tsize_t max_ind = ((sz + (ZU(1) << SC_LG_TINY_MIN) - 1)\n\t\t\t\t   >> SC_LG_TINY_MIN);\n\t\tfor (; dst_ind <= max_ind && dst_ind < dst_max; dst_ind++) {\n\t\t\tsz_size2index_tab[dst_ind] = sc_ind;\n\t\t}\n\t}\n}\n\nvoid\nsz_boot(const sc_data_t *sc_data) {\n\tsz_boot_pind2sz_tab(sc_data);\n\tsz_boot_index2size_tab(sc_data);\n\tsz_boot_size2index_tab(sc_data);\n}\n"
  },
  {
    "path": "deps/jemalloc/src/tcache.c",
    "content": "#define JEMALLOC_TCACHE_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/safety_check.h\"\n#include \"jemalloc/internal/sc.h\"\n\n/******************************************************************************/\n/* Data. */\n\nbool\topt_tcache = true;\nssize_t\topt_lg_tcache_max = LG_TCACHE_MAXCLASS_DEFAULT;\n\ncache_bin_info_t\t*tcache_bin_info;\nstatic unsigned\t\tstack_nelms; /* Total stack elms per tcache. */\n\nunsigned\t\tnhbins;\nsize_t\t\t\ttcache_maxclass;\n\ntcaches_t\t\t*tcaches;\n\n/* Index of first element within tcaches that has never been used. */\nstatic unsigned\t\ttcaches_past;\n\n/* Head of singly linked list tracking available tcaches elements. */\nstatic tcaches_t\t*tcaches_avail;\n\n/* Protects tcaches{,_past,_avail}. */\nstatic malloc_mutex_t\ttcaches_mtx;\n\n/******************************************************************************/\n\nsize_t\ntcache_salloc(tsdn_t *tsdn, const void *ptr) {\n\treturn arena_salloc(tsdn, ptr);\n}\n\nvoid\ntcache_event_hard(tsd_t *tsd, tcache_t *tcache) {\n\tszind_t binind = tcache->next_gc_bin;\n\n\tcache_bin_t *tbin;\n\tif (binind < SC_NBINS) {\n\t\ttbin = tcache_small_bin_get(tcache, binind);\n\t} else {\n\t\ttbin = tcache_large_bin_get(tcache, binind);\n\t}\n\tif (tbin->low_water > 0) {\n\t\t/*\n\t\t * Flush (ceiling) 3/4 of the objects below the low water mark.\n\t\t */\n\t\tif (binind < SC_NBINS) {\n\t\t\ttcache_bin_flush_small(tsd, tcache, tbin, binind,\n\t\t\t    tbin->ncached - tbin->low_water + (tbin->low_water\n\t\t\t    >> 2));\n\t\t\t/*\n\t\t\t * Reduce fill count by 2X.  Limit lg_fill_div such that\n\t\t\t * the fill count is always at least 1.\n\t\t\t */\n\t\t\tcache_bin_info_t *tbin_info = &tcache_bin_info[binind];\n\t\t\tif ((tbin_info->ncached_max >>\n\t\t\t     (tcache->lg_fill_div[binind] + 1)) >= 1) {\n\t\t\t\ttcache->lg_fill_div[binind]++;\n\t\t\t}\n\t\t} else {\n\t\t\ttcache_bin_flush_large(tsd, tbin, binind, tbin->ncached\n\t\t\t    - tbin->low_water + (tbin->low_water >> 2), tcache);\n\t\t}\n\t} else if (tbin->low_water < 0) {\n\t\t/*\n\t\t * Increase fill count by 2X for small bins.  Make sure\n\t\t * lg_fill_div stays greater than 0.\n\t\t */\n\t\tif (binind < SC_NBINS && tcache->lg_fill_div[binind] > 1) {\n\t\t\ttcache->lg_fill_div[binind]--;\n\t\t}\n\t}\n\ttbin->low_water = tbin->ncached;\n\n\ttcache->next_gc_bin++;\n\tif (tcache->next_gc_bin == nhbins) {\n\t\ttcache->next_gc_bin = 0;\n\t}\n}\n\nvoid *\ntcache_alloc_small_hard(tsdn_t *tsdn, arena_t *arena, tcache_t *tcache,\n    cache_bin_t *tbin, szind_t binind, bool *tcache_success) {\n\tvoid *ret;\n\n\tassert(tcache->arena != NULL);\n\tarena_tcache_fill_small(tsdn, arena, tcache, tbin, binind,\n\t    config_prof ? tcache->prof_accumbytes : 0);\n\tif (config_prof) {\n\t\ttcache->prof_accumbytes = 0;\n\t}\n\tret = cache_bin_alloc_easy(tbin, tcache_success);\n\n\treturn ret;\n}\n\n/* Enabled with --enable-extra-size-check. */\nstatic void\ntbin_extents_lookup_size_check(tsdn_t *tsdn, cache_bin_t *tbin, szind_t binind,\n    size_t nflush, extent_t **extents){\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\n\t/*\n\t * Verify that the items in the tcache all have the correct size; this\n\t * is useful for catching sized deallocation bugs, also to fail early\n\t * instead of corrupting metadata.  Since this can be turned on for opt\n\t * builds, avoid the branch in the loop.\n\t */\n\tszind_t szind;\n\tsize_t sz_sum = binind * nflush;\n\tfor (unsigned i = 0 ; i < nflush; i++) {\n\t\trtree_extent_szind_read(tsdn, &extents_rtree,\n\t\t    rtree_ctx, (uintptr_t)*(tbin->avail - 1 - i), true,\n\t\t    &extents[i], &szind);\n\t\tsz_sum -= szind;\n\t}\n\tif (sz_sum != 0) {\n\t\tsafety_check_fail(\"<jemalloc>: size mismatch in thread cache \"\n\t\t    \"detected, likely caused by sized deallocation bugs by \"\n\t\t    \"application. Abort.\\n\");\n\t\tabort();\n\t}\n}\n\nvoid\ntcache_bin_flush_small(tsd_t *tsd, tcache_t *tcache, cache_bin_t *tbin,\n    szind_t binind, unsigned rem) {\n\tbool merged_stats = false;\n\n\tassert(binind < SC_NBINS);\n\tassert((cache_bin_sz_t)rem <= tbin->ncached);\n\n\tarena_t *arena = tcache->arena;\n\tassert(arena != NULL);\n\tunsigned nflush = tbin->ncached - rem;\n\tVARIABLE_ARRAY(extent_t *, item_extent, nflush);\n\n\t/* Look up extent once per item. */\n\tif (config_opt_safety_checks) {\n\t\ttbin_extents_lookup_size_check(tsd_tsdn(tsd), tbin, binind,\n\t\t    nflush, item_extent);\n\t} else {\n\t\tfor (unsigned i = 0 ; i < nflush; i++) {\n\t\t\titem_extent[i] = iealloc(tsd_tsdn(tsd),\n\t\t\t    *(tbin->avail - 1 - i));\n\t\t}\n\t}\n\twhile (nflush > 0) {\n\t\t/* Lock the arena bin associated with the first object. */\n\t\textent_t *extent = item_extent[0];\n\t\tunsigned bin_arena_ind = extent_arena_ind_get(extent);\n\t\tarena_t *bin_arena = arena_get(tsd_tsdn(tsd), bin_arena_ind,\n\t\t    false);\n\t\tunsigned binshard = extent_binshard_get(extent);\n\t\tassert(binshard < bin_infos[binind].n_shards);\n\t\tbin_t *bin = &bin_arena->bins[binind].bin_shards[binshard];\n\n\t\tif (config_prof && bin_arena == arena) {\n\t\t\tif (arena_prof_accum(tsd_tsdn(tsd), arena,\n\t\t\t    tcache->prof_accumbytes)) {\n\t\t\t\tprof_idump(tsd_tsdn(tsd));\n\t\t\t}\n\t\t\ttcache->prof_accumbytes = 0;\n\t\t}\n\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &bin->lock);\n\t\tif (config_stats && bin_arena == arena && !merged_stats) {\n\t\t\tmerged_stats = true;\n\t\t\tbin->stats.nflushes++;\n\t\t\tbin->stats.nrequests += tbin->tstats.nrequests;\n\t\t\ttbin->tstats.nrequests = 0;\n\t\t}\n\t\tunsigned ndeferred = 0;\n\t\tfor (unsigned i = 0; i < nflush; i++) {\n\t\t\tvoid *ptr = *(tbin->avail - 1 - i);\n\t\t\textent = item_extent[i];\n\t\t\tassert(ptr != NULL && extent != NULL);\n\n\t\t\tif (extent_arena_ind_get(extent) == bin_arena_ind\n\t\t\t    && extent_binshard_get(extent) == binshard) {\n\t\t\t\tarena_dalloc_bin_junked_locked(tsd_tsdn(tsd),\n\t\t\t\t    bin_arena, bin, binind, extent, ptr);\n\t\t\t} else {\n\t\t\t\t/*\n\t\t\t\t * This object was allocated via a different\n\t\t\t\t * arena bin than the one that is currently\n\t\t\t\t * locked.  Stash the object, so that it can be\n\t\t\t\t * handled in a future pass.\n\t\t\t\t */\n\t\t\t\t*(tbin->avail - 1 - ndeferred) = ptr;\n\t\t\t\titem_extent[ndeferred] = extent;\n\t\t\t\tndeferred++;\n\t\t\t}\n\t\t}\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &bin->lock);\n\t\tarena_decay_ticks(tsd_tsdn(tsd), bin_arena, nflush - ndeferred);\n\t\tnflush = ndeferred;\n\t}\n\tif (config_stats && !merged_stats) {\n\t\t/*\n\t\t * The flush loop didn't happen to flush to this thread's\n\t\t * arena, so the stats didn't get merged.  Manually do so now.\n\t\t */\n\t\tunsigned binshard;\n\t\tbin_t *bin = arena_bin_choose_lock(tsd_tsdn(tsd), arena, binind,\n\t\t    &binshard);\n\t\tbin->stats.nflushes++;\n\t\tbin->stats.nrequests += tbin->tstats.nrequests;\n\t\ttbin->tstats.nrequests = 0;\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &bin->lock);\n\t}\n\n\tmemmove(tbin->avail - rem, tbin->avail - tbin->ncached, rem *\n\t    sizeof(void *));\n\ttbin->ncached = rem;\n\tif (tbin->ncached < tbin->low_water) {\n\t\ttbin->low_water = tbin->ncached;\n\t}\n}\n\nvoid\ntcache_bin_flush_large(tsd_t *tsd, cache_bin_t *tbin, szind_t binind,\n    unsigned rem, tcache_t *tcache) {\n\tbool merged_stats = false;\n\n\tassert(binind < nhbins);\n\tassert((cache_bin_sz_t)rem <= tbin->ncached);\n\n\tarena_t *tcache_arena = tcache->arena;\n\tassert(tcache_arena != NULL);\n\tunsigned nflush = tbin->ncached - rem;\n\tVARIABLE_ARRAY(extent_t *, item_extent, nflush);\n\n#ifndef JEMALLOC_EXTRA_SIZE_CHECK\n\t/* Look up extent once per item. */\n\tfor (unsigned i = 0 ; i < nflush; i++) {\n\t\titem_extent[i] = iealloc(tsd_tsdn(tsd), *(tbin->avail - 1 - i));\n\t}\n#else\n\ttbin_extents_lookup_size_check(tsd_tsdn(tsd), tbin, binind, nflush,\n\t    item_extent);\n#endif\n\twhile (nflush > 0) {\n\t\t/* Lock the arena associated with the first object. */\n\t\textent_t *extent = item_extent[0];\n\t\tunsigned locked_arena_ind = extent_arena_ind_get(extent);\n\t\tarena_t *locked_arena = arena_get(tsd_tsdn(tsd),\n\t\t    locked_arena_ind, false);\n\t\tbool idump;\n\n\t\tif (config_prof) {\n\t\t\tidump = false;\n\t\t}\n\n\t\tbool lock_large = !arena_is_auto(locked_arena);\n\t\tif (lock_large) {\n\t\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &locked_arena->large_mtx);\n\t\t}\n\t\tfor (unsigned i = 0; i < nflush; i++) {\n\t\t\tvoid *ptr = *(tbin->avail - 1 - i);\n\t\t\tassert(ptr != NULL);\n\t\t\textent = item_extent[i];\n\t\t\tif (extent_arena_ind_get(extent) == locked_arena_ind) {\n\t\t\t\tlarge_dalloc_prep_junked_locked(tsd_tsdn(tsd),\n\t\t\t\t    extent);\n\t\t\t}\n\t\t}\n\t\tif ((config_prof || config_stats) &&\n\t\t    (locked_arena == tcache_arena)) {\n\t\t\tif (config_prof) {\n\t\t\t\tidump = arena_prof_accum(tsd_tsdn(tsd),\n\t\t\t\t    tcache_arena, tcache->prof_accumbytes);\n\t\t\t\ttcache->prof_accumbytes = 0;\n\t\t\t}\n\t\t\tif (config_stats) {\n\t\t\t\tmerged_stats = true;\n\t\t\t\tarena_stats_large_flush_nrequests_add(\n\t\t\t\t    tsd_tsdn(tsd), &tcache_arena->stats, binind,\n\t\t\t\t    tbin->tstats.nrequests);\n\t\t\t\ttbin->tstats.nrequests = 0;\n\t\t\t}\n\t\t}\n\t\tif (lock_large) {\n\t\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &locked_arena->large_mtx);\n\t\t}\n\n\t\tunsigned ndeferred = 0;\n\t\tfor (unsigned i = 0; i < nflush; i++) {\n\t\t\tvoid *ptr = *(tbin->avail - 1 - i);\n\t\t\textent = item_extent[i];\n\t\t\tassert(ptr != NULL && extent != NULL);\n\n\t\t\tif (extent_arena_ind_get(extent) == locked_arena_ind) {\n\t\t\t\tlarge_dalloc_finish(tsd_tsdn(tsd), extent);\n\t\t\t} else {\n\t\t\t\t/*\n\t\t\t\t * This object was allocated via a different\n\t\t\t\t * arena than the one that is currently locked.\n\t\t\t\t * Stash the object, so that it can be handled\n\t\t\t\t * in a future pass.\n\t\t\t\t */\n\t\t\t\t*(tbin->avail - 1 - ndeferred) = ptr;\n\t\t\t\titem_extent[ndeferred] = extent;\n\t\t\t\tndeferred++;\n\t\t\t}\n\t\t}\n\t\tif (config_prof && idump) {\n\t\t\tprof_idump(tsd_tsdn(tsd));\n\t\t}\n\t\tarena_decay_ticks(tsd_tsdn(tsd), locked_arena, nflush -\n\t\t    ndeferred);\n\t\tnflush = ndeferred;\n\t}\n\tif (config_stats && !merged_stats) {\n\t\t/*\n\t\t * The flush loop didn't happen to flush to this thread's\n\t\t * arena, so the stats didn't get merged.  Manually do so now.\n\t\t */\n\t\tarena_stats_large_flush_nrequests_add(tsd_tsdn(tsd),\n\t\t    &tcache_arena->stats, binind, tbin->tstats.nrequests);\n\t\ttbin->tstats.nrequests = 0;\n\t}\n\n\tmemmove(tbin->avail - rem, tbin->avail - tbin->ncached, rem *\n\t    sizeof(void *));\n\ttbin->ncached = rem;\n\tif (tbin->ncached < tbin->low_water) {\n\t\ttbin->low_water = tbin->ncached;\n\t}\n}\n\nvoid\ntcache_arena_associate(tsdn_t *tsdn, tcache_t *tcache, arena_t *arena) {\n\tassert(tcache->arena == NULL);\n\ttcache->arena = arena;\n\n\tif (config_stats) {\n\t\t/* Link into list of extant tcaches. */\n\t\tmalloc_mutex_lock(tsdn, &arena->tcache_ql_mtx);\n\n\t\tql_elm_new(tcache, link);\n\t\tql_tail_insert(&arena->tcache_ql, tcache, link);\n\t\tcache_bin_array_descriptor_init(\n\t\t    &tcache->cache_bin_array_descriptor, tcache->bins_small,\n\t\t    tcache->bins_large);\n\t\tql_tail_insert(&arena->cache_bin_array_descriptor_ql,\n\t\t    &tcache->cache_bin_array_descriptor, link);\n\n\t\tmalloc_mutex_unlock(tsdn, &arena->tcache_ql_mtx);\n\t}\n}\n\nstatic void\ntcache_arena_dissociate(tsdn_t *tsdn, tcache_t *tcache) {\n\tarena_t *arena = tcache->arena;\n\tassert(arena != NULL);\n\tif (config_stats) {\n\t\t/* Unlink from list of extant tcaches. */\n\t\tmalloc_mutex_lock(tsdn, &arena->tcache_ql_mtx);\n\t\tif (config_debug) {\n\t\t\tbool in_ql = false;\n\t\t\ttcache_t *iter;\n\t\t\tql_foreach(iter, &arena->tcache_ql, link) {\n\t\t\t\tif (iter == tcache) {\n\t\t\t\t\tin_ql = true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tassert(in_ql);\n\t\t}\n\t\tql_remove(&arena->tcache_ql, tcache, link);\n\t\tql_remove(&arena->cache_bin_array_descriptor_ql,\n\t\t    &tcache->cache_bin_array_descriptor, link);\n\t\ttcache_stats_merge(tsdn, tcache, arena);\n\t\tmalloc_mutex_unlock(tsdn, &arena->tcache_ql_mtx);\n\t}\n\ttcache->arena = NULL;\n}\n\nvoid\ntcache_arena_reassociate(tsdn_t *tsdn, tcache_t *tcache, arena_t *arena) {\n\ttcache_arena_dissociate(tsdn, tcache);\n\ttcache_arena_associate(tsdn, tcache, arena);\n}\n\nbool\ntsd_tcache_enabled_data_init(tsd_t *tsd) {\n\t/* Called upon tsd initialization. */\n\ttsd_tcache_enabled_set(tsd, opt_tcache);\n\ttsd_slow_update(tsd);\n\n\tif (opt_tcache) {\n\t\t/* Trigger tcache init. */\n\t\ttsd_tcache_data_init(tsd);\n\t}\n\n\treturn false;\n}\n\n/* Initialize auto tcache (embedded in TSD). */\nstatic void\ntcache_init(tsd_t *tsd, tcache_t *tcache, void *avail_stack) {\n\tmemset(&tcache->link, 0, sizeof(ql_elm(tcache_t)));\n\ttcache->prof_accumbytes = 0;\n\ttcache->next_gc_bin = 0;\n\ttcache->arena = NULL;\n\n\tticker_init(&tcache->gc_ticker, TCACHE_GC_INCR);\n\n\tsize_t stack_offset = 0;\n\tassert((TCACHE_NSLOTS_SMALL_MAX & 1U) == 0);\n\tmemset(tcache->bins_small, 0, sizeof(cache_bin_t) * SC_NBINS);\n\tmemset(tcache->bins_large, 0, sizeof(cache_bin_t) * (nhbins - SC_NBINS));\n\tunsigned i = 0;\n\tfor (; i < SC_NBINS; i++) {\n\t\ttcache->lg_fill_div[i] = 1;\n\t\tstack_offset += tcache_bin_info[i].ncached_max * sizeof(void *);\n\t\t/*\n\t\t * avail points past the available space.  Allocations will\n\t\t * access the slots toward higher addresses (for the benefit of\n\t\t * prefetch).\n\t\t */\n\t\ttcache_small_bin_get(tcache, i)->avail =\n\t\t    (void **)((uintptr_t)avail_stack + (uintptr_t)stack_offset);\n\t}\n\tfor (; i < nhbins; i++) {\n\t\tstack_offset += tcache_bin_info[i].ncached_max * sizeof(void *);\n\t\ttcache_large_bin_get(tcache, i)->avail =\n\t\t    (void **)((uintptr_t)avail_stack + (uintptr_t)stack_offset);\n\t}\n\tassert(stack_offset == stack_nelms * sizeof(void *));\n}\n\n/* Initialize auto tcache (embedded in TSD). */\nbool\ntsd_tcache_data_init(tsd_t *tsd) {\n\ttcache_t *tcache = tsd_tcachep_get_unsafe(tsd);\n\tassert(tcache_small_bin_get(tcache, 0)->avail == NULL);\n\tsize_t size = stack_nelms * sizeof(void *);\n\t/* Avoid false cacheline sharing. */\n\tsize = sz_sa2u(size, CACHELINE);\n\n\tvoid *avail_array = ipallocztm(tsd_tsdn(tsd), size, CACHELINE, true,\n\t    NULL, true, arena_get(TSDN_NULL, 0, true));\n\tif (avail_array == NULL) {\n\t\treturn true;\n\t}\n\n\ttcache_init(tsd, tcache, avail_array);\n\t/*\n\t * Initialization is a bit tricky here.  After malloc init is done, all\n\t * threads can rely on arena_choose and associate tcache accordingly.\n\t * However, the thread that does actual malloc bootstrapping relies on\n\t * functional tsd, and it can only rely on a0.  In that case, we\n\t * associate its tcache to a0 temporarily, and later on\n\t * arena_choose_hard() will re-associate properly.\n\t */\n\ttcache->arena = NULL;\n\tarena_t *arena;\n\tif (!malloc_initialized()) {\n\t\t/* If in initialization, assign to a0. */\n\t\tarena = arena_get(tsd_tsdn(tsd), 0, false);\n\t\ttcache_arena_associate(tsd_tsdn(tsd), tcache, arena);\n\t} else {\n\t\tarena = arena_choose(tsd, NULL);\n\t\t/* This may happen if thread.tcache.enabled is used. */\n\t\tif (tcache->arena == NULL) {\n\t\t\ttcache_arena_associate(tsd_tsdn(tsd), tcache, arena);\n\t\t}\n\t}\n\tassert(arena == tcache->arena);\n\n\treturn false;\n}\n\n/* Created manual tcache for tcache.create mallctl. */\ntcache_t *\ntcache_create_explicit(tsd_t *tsd) {\n\ttcache_t *tcache;\n\tsize_t size, stack_offset;\n\n\tsize = sizeof(tcache_t);\n\t/* Naturally align the pointer stacks. */\n\tsize = PTR_CEILING(size);\n\tstack_offset = size;\n\tsize += stack_nelms * sizeof(void *);\n\t/* Avoid false cacheline sharing. */\n\tsize = sz_sa2u(size, CACHELINE);\n\n\ttcache = ipallocztm(tsd_tsdn(tsd), size, CACHELINE, true, NULL, true,\n\t    arena_get(TSDN_NULL, 0, true));\n\tif (tcache == NULL) {\n\t\treturn NULL;\n\t}\n\n\ttcache_init(tsd, tcache,\n\t    (void *)((uintptr_t)tcache + (uintptr_t)stack_offset));\n\ttcache_arena_associate(tsd_tsdn(tsd), tcache, arena_ichoose(tsd, NULL));\n\n\treturn tcache;\n}\n\nstatic void\ntcache_flush_cache(tsd_t *tsd, tcache_t *tcache) {\n\tassert(tcache->arena != NULL);\n\n\tfor (unsigned i = 0; i < SC_NBINS; i++) {\n\t\tcache_bin_t *tbin = tcache_small_bin_get(tcache, i);\n\t\ttcache_bin_flush_small(tsd, tcache, tbin, i, 0);\n\n\t\tif (config_stats) {\n\t\t\tassert(tbin->tstats.nrequests == 0);\n\t\t}\n\t}\n\tfor (unsigned i = SC_NBINS; i < nhbins; i++) {\n\t\tcache_bin_t *tbin = tcache_large_bin_get(tcache, i);\n\t\ttcache_bin_flush_large(tsd, tbin, i, 0, tcache);\n\n\t\tif (config_stats) {\n\t\t\tassert(tbin->tstats.nrequests == 0);\n\t\t}\n\t}\n\n\tif (config_prof && tcache->prof_accumbytes > 0 &&\n\t    arena_prof_accum(tsd_tsdn(tsd), tcache->arena,\n\t    tcache->prof_accumbytes)) {\n\t\tprof_idump(tsd_tsdn(tsd));\n\t}\n}\n\nvoid\ntcache_flush(tsd_t *tsd) {\n\tassert(tcache_available(tsd));\n\ttcache_flush_cache(tsd, tsd_tcachep_get(tsd));\n}\n\nstatic void\ntcache_destroy(tsd_t *tsd, tcache_t *tcache, bool tsd_tcache) {\n\ttcache_flush_cache(tsd, tcache);\n\tarena_t *arena = tcache->arena;\n\ttcache_arena_dissociate(tsd_tsdn(tsd), tcache);\n\n\tif (tsd_tcache) {\n\t\t/* Release the avail array for the TSD embedded auto tcache. */\n\t\tvoid *avail_array =\n\t\t    (void *)((uintptr_t)tcache_small_bin_get(tcache, 0)->avail -\n\t\t    (uintptr_t)tcache_bin_info[0].ncached_max * sizeof(void *));\n\t\tidalloctm(tsd_tsdn(tsd), avail_array, NULL, NULL, true, true);\n\t} else {\n\t\t/* Release both the tcache struct and avail array. */\n\t\tidalloctm(tsd_tsdn(tsd), tcache, NULL, NULL, true, true);\n\t}\n\n\t/*\n\t * The deallocation and tcache flush above may not trigger decay since\n\t * we are on the tcache shutdown path (potentially with non-nominal\n\t * tsd).  Manually trigger decay to avoid pathological cases.  Also\n\t * include arena 0 because the tcache array is allocated from it.\n\t */\n\tarena_decay(tsd_tsdn(tsd), arena_get(tsd_tsdn(tsd), 0, false),\n\t    false, false);\n\n\tif (arena_nthreads_get(arena, false) == 0 &&\n\t    !background_thread_enabled()) {\n\t\t/* Force purging when no threads assigned to the arena anymore. */\n\t\tarena_decay(tsd_tsdn(tsd), arena, false, true);\n\t} else {\n\t\tarena_decay(tsd_tsdn(tsd), arena, false, false);\n\t}\n}\n\n/* For auto tcache (embedded in TSD) only. */\nvoid\ntcache_cleanup(tsd_t *tsd) {\n\ttcache_t *tcache = tsd_tcachep_get(tsd);\n\tif (!tcache_available(tsd)) {\n\t\tassert(tsd_tcache_enabled_get(tsd) == false);\n\t\tif (config_debug) {\n\t\t\tassert(tcache_small_bin_get(tcache, 0)->avail == NULL);\n\t\t}\n\t\treturn;\n\t}\n\tassert(tsd_tcache_enabled_get(tsd));\n\tassert(tcache_small_bin_get(tcache, 0)->avail != NULL);\n\n\ttcache_destroy(tsd, tcache, true);\n\tif (config_debug) {\n\t\ttcache_small_bin_get(tcache, 0)->avail = NULL;\n\t}\n}\n\nvoid\ntcache_stats_merge(tsdn_t *tsdn, tcache_t *tcache, arena_t *arena) {\n\tunsigned i;\n\n\tcassert(config_stats);\n\n\t/* Merge and reset tcache stats. */\n\tfor (i = 0; i < SC_NBINS; i++) {\n\t\tcache_bin_t *tbin = tcache_small_bin_get(tcache, i);\n\t\tunsigned binshard;\n\t\tbin_t *bin = arena_bin_choose_lock(tsdn, arena, i, &binshard);\n\t\tbin->stats.nrequests += tbin->tstats.nrequests;\n\t\tmalloc_mutex_unlock(tsdn, &bin->lock);\n\t\ttbin->tstats.nrequests = 0;\n\t}\n\n\tfor (; i < nhbins; i++) {\n\t\tcache_bin_t *tbin = tcache_large_bin_get(tcache, i);\n\t\tarena_stats_large_flush_nrequests_add(tsdn, &arena->stats, i,\n\t\t    tbin->tstats.nrequests);\n\t\ttbin->tstats.nrequests = 0;\n\t}\n}\n\nstatic bool\ntcaches_create_prep(tsd_t *tsd) {\n\tbool err;\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &tcaches_mtx);\n\n\tif (tcaches == NULL) {\n\t\ttcaches = base_alloc(tsd_tsdn(tsd), b0get(), sizeof(tcache_t *)\n\t\t    * (MALLOCX_TCACHE_MAX+1), CACHELINE);\n\t\tif (tcaches == NULL) {\n\t\t\terr = true;\n\t\t\tgoto label_return;\n\t\t}\n\t}\n\n\tif (tcaches_avail == NULL && tcaches_past > MALLOCX_TCACHE_MAX) {\n\t\terr = true;\n\t\tgoto label_return;\n\t}\n\n\terr = false;\nlabel_return:\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &tcaches_mtx);\n\treturn err;\n}\n\nbool\ntcaches_create(tsd_t *tsd, unsigned *r_ind) {\n\twitness_assert_depth(tsdn_witness_tsdp_get(tsd_tsdn(tsd)), 0);\n\n\tbool err;\n\n\tif (tcaches_create_prep(tsd)) {\n\t\terr = true;\n\t\tgoto label_return;\n\t}\n\n\ttcache_t *tcache = tcache_create_explicit(tsd);\n\tif (tcache == NULL) {\n\t\terr = true;\n\t\tgoto label_return;\n\t}\n\n\ttcaches_t *elm;\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &tcaches_mtx);\n\tif (tcaches_avail != NULL) {\n\t\telm = tcaches_avail;\n\t\ttcaches_avail = tcaches_avail->next;\n\t\telm->tcache = tcache;\n\t\t*r_ind = (unsigned)(elm - tcaches);\n\t} else {\n\t\telm = &tcaches[tcaches_past];\n\t\telm->tcache = tcache;\n\t\t*r_ind = tcaches_past;\n\t\ttcaches_past++;\n\t}\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &tcaches_mtx);\n\n\terr = false;\nlabel_return:\n\twitness_assert_depth(tsdn_witness_tsdp_get(tsd_tsdn(tsd)), 0);\n\treturn err;\n}\n\nstatic tcache_t *\ntcaches_elm_remove(tsd_t *tsd, tcaches_t *elm, bool allow_reinit) {\n\tmalloc_mutex_assert_owner(tsd_tsdn(tsd), &tcaches_mtx);\n\n\tif (elm->tcache == NULL) {\n\t\treturn NULL;\n\t}\n\ttcache_t *tcache = elm->tcache;\n\tif (allow_reinit) {\n\t\telm->tcache = TCACHES_ELM_NEED_REINIT;\n\t} else {\n\t\telm->tcache = NULL;\n\t}\n\n\tif (tcache == TCACHES_ELM_NEED_REINIT) {\n\t\treturn NULL;\n\t}\n\treturn tcache;\n}\n\nvoid\ntcaches_flush(tsd_t *tsd, unsigned ind) {\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &tcaches_mtx);\n\ttcache_t *tcache = tcaches_elm_remove(tsd, &tcaches[ind], true);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &tcaches_mtx);\n\tif (tcache != NULL) {\n\t\t/* Destroy the tcache; recreate in tcaches_get() if needed. */\n\t\ttcache_destroy(tsd, tcache, false);\n\t}\n}\n\nvoid\ntcaches_destroy(tsd_t *tsd, unsigned ind) {\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &tcaches_mtx);\n\ttcaches_t *elm = &tcaches[ind];\n\ttcache_t *tcache = tcaches_elm_remove(tsd, elm, false);\n\telm->next = tcaches_avail;\n\ttcaches_avail = elm;\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &tcaches_mtx);\n\tif (tcache != NULL) {\n\t\ttcache_destroy(tsd, tcache, false);\n\t}\n}\n\nbool\ntcache_boot(tsdn_t *tsdn) {\n\t/* If necessary, clamp opt_lg_tcache_max. */\n\tif (opt_lg_tcache_max < 0 || (ZU(1) << opt_lg_tcache_max) <\n\t    SC_SMALL_MAXCLASS) {\n\t\ttcache_maxclass = SC_SMALL_MAXCLASS;\n\t} else {\n\t\ttcache_maxclass = (ZU(1) << opt_lg_tcache_max);\n\t}\n\n\tif (malloc_mutex_init(&tcaches_mtx, \"tcaches\", WITNESS_RANK_TCACHES,\n\t    malloc_mutex_rank_exclusive)) {\n\t\treturn true;\n\t}\n\n\tnhbins = sz_size2index(tcache_maxclass) + 1;\n\n\t/* Initialize tcache_bin_info. */\n\ttcache_bin_info = (cache_bin_info_t *)base_alloc(tsdn, b0get(), nhbins\n\t    * sizeof(cache_bin_info_t), CACHELINE);\n\tif (tcache_bin_info == NULL) {\n\t\treturn true;\n\t}\n\tstack_nelms = 0;\n\tunsigned i;\n\tfor (i = 0; i < SC_NBINS; i++) {\n\t\tif ((bin_infos[i].nregs << 1) <= TCACHE_NSLOTS_SMALL_MIN) {\n\t\t\ttcache_bin_info[i].ncached_max =\n\t\t\t    TCACHE_NSLOTS_SMALL_MIN;\n\t\t} else if ((bin_infos[i].nregs << 1) <=\n\t\t    TCACHE_NSLOTS_SMALL_MAX) {\n\t\t\ttcache_bin_info[i].ncached_max =\n\t\t\t    (bin_infos[i].nregs << 1);\n\t\t} else {\n\t\t\ttcache_bin_info[i].ncached_max =\n\t\t\t    TCACHE_NSLOTS_SMALL_MAX;\n\t\t}\n\t\tstack_nelms += tcache_bin_info[i].ncached_max;\n\t}\n\tfor (; i < nhbins; i++) {\n\t\ttcache_bin_info[i].ncached_max = TCACHE_NSLOTS_LARGE;\n\t\tstack_nelms += tcache_bin_info[i].ncached_max;\n\t}\n\n\treturn false;\n}\n\nvoid\ntcache_prefork(tsdn_t *tsdn) {\n\tif (!config_prof && opt_tcache) {\n\t\tmalloc_mutex_prefork(tsdn, &tcaches_mtx);\n\t}\n}\n\nvoid\ntcache_postfork_parent(tsdn_t *tsdn) {\n\tif (!config_prof && opt_tcache) {\n\t\tmalloc_mutex_postfork_parent(tsdn, &tcaches_mtx);\n\t}\n}\n\nvoid\ntcache_postfork_child(tsdn_t *tsdn) {\n\tif (!config_prof && opt_tcache) {\n\t\tmalloc_mutex_postfork_child(tsdn, &tcaches_mtx);\n\t}\n}\n"
  },
  {
    "path": "deps/jemalloc/src/test_hooks.c",
    "content": "#include \"jemalloc/internal/jemalloc_preamble.h\"\n\n/*\n * The hooks are a little bit screwy -- they're not genuinely exported in the\n * sense that we want them available to end-users, but we do want them visible\n * from outside the generated library, so that we can use them in test code.\n */\nJEMALLOC_EXPORT\nvoid (*test_hooks_arena_new_hook)() = NULL;\n\nJEMALLOC_EXPORT\nvoid (*test_hooks_libc_hook)() = NULL;\n"
  },
  {
    "path": "deps/jemalloc/src/ticker.c",
    "content": "#define JEMALLOC_TICKER_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n"
  },
  {
    "path": "deps/jemalloc/src/tsd.c",
    "content": "#define JEMALLOC_TSD_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/rtree.h\"\n\n/******************************************************************************/\n/* Data. */\n\nstatic unsigned ncleanups;\nstatic malloc_tsd_cleanup_t cleanups[MALLOC_TSD_CLEANUPS_MAX];\n\n/* TSD_INITIALIZER triggers \"-Wmissing-field-initializer\" */\nJEMALLOC_DIAGNOSTIC_PUSH\nJEMALLOC_DIAGNOSTIC_IGNORE_MISSING_STRUCT_FIELD_INITIALIZERS\n\n#ifdef JEMALLOC_MALLOC_THREAD_CLEANUP\nJEMALLOC_TSD_TYPE_ATTR(tsd_t) tsd_tls = TSD_INITIALIZER;\nJEMALLOC_TSD_TYPE_ATTR(bool) JEMALLOC_TLS_MODEL tsd_initialized = false;\nbool tsd_booted = false;\n#elif (defined(JEMALLOC_TLS))\nJEMALLOC_TSD_TYPE_ATTR(tsd_t) tsd_tls = TSD_INITIALIZER;\npthread_key_t tsd_tsd;\nbool tsd_booted = false;\n#elif (defined(_WIN32))\nDWORD tsd_tsd;\ntsd_wrapper_t tsd_boot_wrapper = {false, TSD_INITIALIZER};\nbool tsd_booted = false;\n#else\n\n/*\n * This contains a mutex, but it's pretty convenient to allow the mutex code to\n * have a dependency on tsd.  So we define the struct here, and only refer to it\n * by pointer in the header.\n */\nstruct tsd_init_head_s {\n\tql_head(tsd_init_block_t) blocks;\n\tmalloc_mutex_t lock;\n};\n\npthread_key_t tsd_tsd;\ntsd_init_head_t\ttsd_init_head = {\n\tql_head_initializer(blocks),\n\tMALLOC_MUTEX_INITIALIZER\n};\n\ntsd_wrapper_t tsd_boot_wrapper = {\n\tfalse,\n\tTSD_INITIALIZER\n};\nbool tsd_booted = false;\n#endif\n\nJEMALLOC_DIAGNOSTIC_POP\n\n/******************************************************************************/\n\n/* A list of all the tsds in the nominal state. */\ntypedef ql_head(tsd_t) tsd_list_t;\nstatic tsd_list_t tsd_nominal_tsds = ql_head_initializer(tsd_nominal_tsds);\nstatic malloc_mutex_t tsd_nominal_tsds_lock;\n\n/* How many slow-path-enabling features are turned on. */\nstatic atomic_u32_t tsd_global_slow_count = ATOMIC_INIT(0);\n\nstatic bool\ntsd_in_nominal_list(tsd_t *tsd) {\n\ttsd_t *tsd_list;\n\tbool found = false;\n\t/*\n\t * We don't know that tsd is nominal; it might not be safe to get data\n\t * out of it here.\n\t */\n\tmalloc_mutex_lock(TSDN_NULL, &tsd_nominal_tsds_lock);\n\tql_foreach(tsd_list, &tsd_nominal_tsds, TSD_MANGLE(tcache).tsd_link) {\n\t\tif (tsd == tsd_list) {\n\t\t\tfound = true;\n\t\t\tbreak;\n\t\t}\n\t}\n\tmalloc_mutex_unlock(TSDN_NULL, &tsd_nominal_tsds_lock);\n\treturn found;\n}\n\nstatic void\ntsd_add_nominal(tsd_t *tsd) {\n\tassert(!tsd_in_nominal_list(tsd));\n\tassert(tsd_state_get(tsd) <= tsd_state_nominal_max);\n\tql_elm_new(tsd, TSD_MANGLE(tcache).tsd_link);\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &tsd_nominal_tsds_lock);\n\tql_tail_insert(&tsd_nominal_tsds, tsd, TSD_MANGLE(tcache).tsd_link);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &tsd_nominal_tsds_lock);\n}\n\nstatic void\ntsd_remove_nominal(tsd_t *tsd) {\n\tassert(tsd_in_nominal_list(tsd));\n\tassert(tsd_state_get(tsd) <= tsd_state_nominal_max);\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &tsd_nominal_tsds_lock);\n\tql_remove(&tsd_nominal_tsds, tsd, TSD_MANGLE(tcache).tsd_link);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &tsd_nominal_tsds_lock);\n}\n\nstatic void\ntsd_force_recompute(tsdn_t *tsdn) {\n\t/*\n\t * The stores to tsd->state here need to synchronize with the exchange\n\t * in tsd_slow_update.\n\t */\n\tatomic_fence(ATOMIC_RELEASE);\n\tmalloc_mutex_lock(tsdn, &tsd_nominal_tsds_lock);\n\ttsd_t *remote_tsd;\n\tql_foreach(remote_tsd, &tsd_nominal_tsds, TSD_MANGLE(tcache).tsd_link) {\n\t\tassert(tsd_atomic_load(&remote_tsd->state, ATOMIC_RELAXED)\n\t\t    <= tsd_state_nominal_max);\n\t\ttsd_atomic_store(&remote_tsd->state, tsd_state_nominal_recompute,\n\t\t    ATOMIC_RELAXED);\n\t}\n\tmalloc_mutex_unlock(tsdn, &tsd_nominal_tsds_lock);\n}\n\nvoid\ntsd_global_slow_inc(tsdn_t *tsdn) {\n\tatomic_fetch_add_u32(&tsd_global_slow_count, 1, ATOMIC_RELAXED);\n\t/*\n\t * We unconditionally force a recompute, even if the global slow count\n\t * was already positive.  If we didn't, then it would be possible for us\n\t * to return to the user, have the user synchronize externally with some\n\t * other thread, and then have that other thread not have picked up the\n\t * update yet (since the original incrementing thread might still be\n\t * making its way through the tsd list).\n\t */\n\ttsd_force_recompute(tsdn);\n}\n\nvoid tsd_global_slow_dec(tsdn_t *tsdn) {\n\tatomic_fetch_sub_u32(&tsd_global_slow_count, 1, ATOMIC_RELAXED);\n\t/* See the note in ..._inc(). */\n\ttsd_force_recompute(tsdn);\n}\n\nstatic bool\ntsd_local_slow(tsd_t *tsd) {\n\treturn !tsd_tcache_enabled_get(tsd)\n\t    || tsd_reentrancy_level_get(tsd) > 0;\n}\n\nbool\ntsd_global_slow() {\n\treturn atomic_load_u32(&tsd_global_slow_count, ATOMIC_RELAXED) > 0;\n}\n\n/******************************************************************************/\n\nstatic uint8_t\ntsd_state_compute(tsd_t *tsd) {\n\tif (!tsd_nominal(tsd)) {\n\t\treturn tsd_state_get(tsd);\n\t}\n\t/* We're in *a* nominal state; but which one? */\n\tif (malloc_slow || tsd_local_slow(tsd) || tsd_global_slow()) {\n\t\treturn tsd_state_nominal_slow;\n\t} else {\n\t\treturn tsd_state_nominal;\n\t}\n}\n\nvoid\ntsd_slow_update(tsd_t *tsd) {\n\tuint8_t old_state;\n\tdo {\n\t\tuint8_t new_state = tsd_state_compute(tsd);\n\t\told_state = tsd_atomic_exchange(&tsd->state, new_state,\n\t\t    ATOMIC_ACQUIRE);\n\t} while (old_state == tsd_state_nominal_recompute);\n}\n\nvoid\ntsd_state_set(tsd_t *tsd, uint8_t new_state) {\n\t/* Only the tsd module can change the state *to* recompute. */\n\tassert(new_state != tsd_state_nominal_recompute);\n\tuint8_t old_state = tsd_atomic_load(&tsd->state, ATOMIC_RELAXED);\n\tif (old_state > tsd_state_nominal_max) {\n\t\t/*\n\t\t * Not currently in the nominal list, but it might need to be\n\t\t * inserted there.\n\t\t */\n\t\tassert(!tsd_in_nominal_list(tsd));\n\t\ttsd_atomic_store(&tsd->state, new_state, ATOMIC_RELAXED);\n\t\tif (new_state <= tsd_state_nominal_max) {\n\t\t\ttsd_add_nominal(tsd);\n\t\t}\n\t} else {\n\t\t/*\n\t\t * We're currently nominal.  If the new state is non-nominal,\n\t\t * great; we take ourselves off the list and just enter the new\n\t\t * state.\n\t\t */\n\t\tassert(tsd_in_nominal_list(tsd));\n\t\tif (new_state > tsd_state_nominal_max) {\n\t\t\ttsd_remove_nominal(tsd);\n\t\t\ttsd_atomic_store(&tsd->state, new_state,\n\t\t\t    ATOMIC_RELAXED);\n\t\t} else {\n\t\t\t/*\n\t\t\t * This is the tricky case.  We're transitioning from\n\t\t\t * one nominal state to another.  The caller can't know\n\t\t\t * about any races that are occuring at the same time,\n\t\t\t * so we always have to recompute no matter what.\n\t\t\t */\n\t\t\ttsd_slow_update(tsd);\n\t\t}\n\t}\n}\n\nstatic bool\ntsd_data_init(tsd_t *tsd) {\n\t/*\n\t * We initialize the rtree context first (before the tcache), since the\n\t * tcache initialization depends on it.\n\t */\n\trtree_ctx_data_init(tsd_rtree_ctxp_get_unsafe(tsd));\n\n\t/*\n\t * A nondeterministic seed based on the address of tsd reduces\n\t * the likelihood of lockstep non-uniform cache index\n\t * utilization among identical concurrent processes, but at the\n\t * cost of test repeatability.  For debug builds, instead use a\n\t * deterministic seed.\n\t */\n\t*tsd_offset_statep_get(tsd) = config_debug ? 0 :\n\t    (uint64_t)(uintptr_t)tsd;\n\n\treturn tsd_tcache_enabled_data_init(tsd);\n}\n\nstatic void\nassert_tsd_data_cleanup_done(tsd_t *tsd) {\n\tassert(!tsd_nominal(tsd));\n\tassert(!tsd_in_nominal_list(tsd));\n\tassert(*tsd_arenap_get_unsafe(tsd) == NULL);\n\tassert(*tsd_iarenap_get_unsafe(tsd) == NULL);\n\tassert(*tsd_arenas_tdata_bypassp_get_unsafe(tsd) == true);\n\tassert(*tsd_arenas_tdatap_get_unsafe(tsd) == NULL);\n\tassert(*tsd_tcache_enabledp_get_unsafe(tsd) == false);\n\tassert(*tsd_prof_tdatap_get_unsafe(tsd) == NULL);\n}\n\nstatic bool\ntsd_data_init_nocleanup(tsd_t *tsd) {\n\tassert(tsd_state_get(tsd) == tsd_state_reincarnated ||\n\t    tsd_state_get(tsd) == tsd_state_minimal_initialized);\n\t/*\n\t * During reincarnation, there is no guarantee that the cleanup function\n\t * will be called (deallocation may happen after all tsd destructors).\n\t * We set up tsd in a way that no cleanup is needed.\n\t */\n\trtree_ctx_data_init(tsd_rtree_ctxp_get_unsafe(tsd));\n\t*tsd_arenas_tdata_bypassp_get(tsd) = true;\n\t*tsd_tcache_enabledp_get_unsafe(tsd) = false;\n\t*tsd_reentrancy_levelp_get(tsd) = 1;\n\tassert_tsd_data_cleanup_done(tsd);\n\n\treturn false;\n}\n\ntsd_t *\ntsd_fetch_slow(tsd_t *tsd, bool minimal) {\n\tassert(!tsd_fast(tsd));\n\n\tif (tsd_state_get(tsd) == tsd_state_nominal_slow) {\n\t\t/*\n\t\t * On slow path but no work needed.  Note that we can't\n\t\t * necessarily *assert* that we're slow, because we might be\n\t\t * slow because of an asynchronous modification to global state,\n\t\t * which might be asynchronously modified *back*.\n\t\t */\n\t} else if (tsd_state_get(tsd) == tsd_state_nominal_recompute) {\n\t\ttsd_slow_update(tsd);\n\t} else if (tsd_state_get(tsd) == tsd_state_uninitialized) {\n\t\tif (!minimal) {\n\t\t\tif (tsd_booted) {\n\t\t\t\ttsd_state_set(tsd, tsd_state_nominal);\n\t\t\t\ttsd_slow_update(tsd);\n\t\t\t\t/* Trigger cleanup handler registration. */\n\t\t\t\ttsd_set(tsd);\n\t\t\t\ttsd_data_init(tsd);\n\t\t\t}\n\t\t} else {\n\t\t\ttsd_state_set(tsd, tsd_state_minimal_initialized);\n\t\t\ttsd_set(tsd);\n\t\t\ttsd_data_init_nocleanup(tsd);\n\t\t}\n\t} else if (tsd_state_get(tsd) == tsd_state_minimal_initialized) {\n\t\tif (!minimal) {\n\t\t\t/* Switch to fully initialized. */\n\t\t\ttsd_state_set(tsd, tsd_state_nominal);\n\t\t\tassert(*tsd_reentrancy_levelp_get(tsd) >= 1);\n\t\t\t(*tsd_reentrancy_levelp_get(tsd))--;\n\t\t\ttsd_slow_update(tsd);\n\t\t\ttsd_data_init(tsd);\n\t\t} else {\n\t\t\tassert_tsd_data_cleanup_done(tsd);\n\t\t}\n\t} else if (tsd_state_get(tsd) == tsd_state_purgatory) {\n\t\ttsd_state_set(tsd, tsd_state_reincarnated);\n\t\ttsd_set(tsd);\n\t\ttsd_data_init_nocleanup(tsd);\n\t} else {\n\t\tassert(tsd_state_get(tsd) == tsd_state_reincarnated);\n\t}\n\n\treturn tsd;\n}\n\nvoid *\nmalloc_tsd_malloc(size_t size) {\n\treturn a0malloc(CACHELINE_CEILING(size));\n}\n\nvoid\nmalloc_tsd_dalloc(void *wrapper) {\n\ta0dalloc(wrapper);\n}\n\n#if defined(JEMALLOC_MALLOC_THREAD_CLEANUP) || defined(_WIN32)\n#ifndef _WIN32\nJEMALLOC_EXPORT\n#endif\nvoid\n_malloc_thread_cleanup(void) {\n\tbool pending[MALLOC_TSD_CLEANUPS_MAX], again;\n\tunsigned i;\n\n\tfor (i = 0; i < ncleanups; i++) {\n\t\tpending[i] = true;\n\t}\n\n\tdo {\n\t\tagain = false;\n\t\tfor (i = 0; i < ncleanups; i++) {\n\t\t\tif (pending[i]) {\n\t\t\t\tpending[i] = cleanups[i]();\n\t\t\t\tif (pending[i]) {\n\t\t\t\t\tagain = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t} while (again);\n}\n#endif\n\nvoid\nmalloc_tsd_cleanup_register(bool (*f)(void)) {\n\tassert(ncleanups < MALLOC_TSD_CLEANUPS_MAX);\n\tcleanups[ncleanups] = f;\n\tncleanups++;\n}\n\nstatic void\ntsd_do_data_cleanup(tsd_t *tsd) {\n\tprof_tdata_cleanup(tsd);\n\tiarena_cleanup(tsd);\n\tarena_cleanup(tsd);\n\tarenas_tdata_cleanup(tsd);\n\ttcache_cleanup(tsd);\n\twitnesses_cleanup(tsd_witness_tsdp_get_unsafe(tsd));\n}\n\nvoid\ntsd_cleanup(void *arg) {\n\ttsd_t *tsd = (tsd_t *)arg;\n\n\tswitch (tsd_state_get(tsd)) {\n\tcase tsd_state_uninitialized:\n\t\t/* Do nothing. */\n\t\tbreak;\n\tcase tsd_state_minimal_initialized:\n\t\t/* This implies the thread only did free() in its life time. */\n\t\t/* Fall through. */\n\tcase tsd_state_reincarnated:\n\t\t/*\n\t\t * Reincarnated means another destructor deallocated memory\n\t\t * after the destructor was called.  Cleanup isn't required but\n\t\t * is still called for testing and completeness.\n\t\t */\n\t\tassert_tsd_data_cleanup_done(tsd);\n\t\t/* Fall through. */\n\tcase tsd_state_nominal:\n\tcase tsd_state_nominal_slow:\n\t\ttsd_do_data_cleanup(tsd);\n\t\ttsd_state_set(tsd, tsd_state_purgatory);\n\t\ttsd_set(tsd);\n\t\tbreak;\n\tcase tsd_state_purgatory:\n\t\t/*\n\t\t * The previous time this destructor was called, we set the\n\t\t * state to tsd_state_purgatory so that other destructors\n\t\t * wouldn't cause re-creation of the tsd.  This time, do\n\t\t * nothing, and do not request another callback.\n\t\t */\n\t\tbreak;\n\tdefault:\n\t\tnot_reached();\n\t}\n#ifdef JEMALLOC_JET\n\ttest_callback_t test_callback = *tsd_test_callbackp_get_unsafe(tsd);\n\tint *data = tsd_test_datap_get_unsafe(tsd);\n\tif (test_callback != NULL) {\n\t\ttest_callback(data);\n\t}\n#endif\n}\n\ntsd_t *\nmalloc_tsd_boot0(void) {\n\ttsd_t *tsd;\n\n\tncleanups = 0;\n\tif (malloc_mutex_init(&tsd_nominal_tsds_lock, \"tsd_nominal_tsds_lock\",\n\t    WITNESS_RANK_OMIT, malloc_mutex_rank_exclusive)) {\n\t\treturn NULL;\n\t}\n\tif (tsd_boot0()) {\n\t\treturn NULL;\n\t}\n\ttsd = tsd_fetch();\n\t*tsd_arenas_tdata_bypassp_get(tsd) = true;\n\treturn tsd;\n}\n\nvoid\nmalloc_tsd_boot1(void) {\n\ttsd_boot1();\n\ttsd_t *tsd = tsd_fetch();\n\t/* malloc_slow has been set properly.  Update tsd_slow. */\n\ttsd_slow_update(tsd);\n\t*tsd_arenas_tdata_bypassp_get(tsd) = false;\n}\n\n#ifdef _WIN32\nstatic BOOL WINAPI\n_tls_callback(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {\n\tswitch (fdwReason) {\n#ifdef JEMALLOC_LAZY_LOCK\n\tcase DLL_THREAD_ATTACH:\n\t\tisthreaded = true;\n\t\tbreak;\n#endif\n\tcase DLL_THREAD_DETACH:\n\t\t_malloc_thread_cleanup();\n\t\tbreak;\n\tdefault:\n\t\tbreak;\n\t}\n\treturn true;\n}\n\n/*\n * We need to be able to say \"read\" here (in the \"pragma section\"), but have\n * hooked \"read\". We won't read for the rest of the file, so we can get away\n * with unhooking.\n */\n#ifdef read\n#  undef read\n#endif\n\n#ifdef _MSC_VER\n#  ifdef _M_IX86\n#    pragma comment(linker, \"/INCLUDE:__tls_used\")\n#    pragma comment(linker, \"/INCLUDE:_tls_callback\")\n#  else\n#    pragma comment(linker, \"/INCLUDE:_tls_used\")\n#    pragma comment(linker, \"/INCLUDE:\" STRINGIFY(tls_callback) )\n#  endif\n#  pragma section(\".CRT$XLY\",long,read)\n#endif\nJEMALLOC_SECTION(\".CRT$XLY\") JEMALLOC_ATTR(used)\nBOOL\t(WINAPI *const tls_callback)(HINSTANCE hinstDLL,\n    DWORD fdwReason, LPVOID lpvReserved) = _tls_callback;\n#endif\n\n#if (!defined(JEMALLOC_MALLOC_THREAD_CLEANUP) && !defined(JEMALLOC_TLS) && \\\n    !defined(_WIN32))\nvoid *\ntsd_init_check_recursion(tsd_init_head_t *head, tsd_init_block_t *block) {\n\tpthread_t self = pthread_self();\n\ttsd_init_block_t *iter;\n\n\t/* Check whether this thread has already inserted into the list. */\n\tmalloc_mutex_lock(TSDN_NULL, &head->lock);\n\tql_foreach(iter, &head->blocks, link) {\n\t\tif (iter->thread == self) {\n\t\t\tmalloc_mutex_unlock(TSDN_NULL, &head->lock);\n\t\t\treturn iter->data;\n\t\t}\n\t}\n\t/* Insert block into list. */\n\tql_elm_new(block, link);\n\tblock->thread = self;\n\tql_tail_insert(&head->blocks, block, link);\n\tmalloc_mutex_unlock(TSDN_NULL, &head->lock);\n\treturn NULL;\n}\n\nvoid\ntsd_init_finish(tsd_init_head_t *head, tsd_init_block_t *block) {\n\tmalloc_mutex_lock(TSDN_NULL, &head->lock);\n\tql_remove(&head->blocks, block, link);\n\tmalloc_mutex_unlock(TSDN_NULL, &head->lock);\n}\n#endif\n\nvoid\ntsd_prefork(tsd_t *tsd) {\n\tmalloc_mutex_prefork(tsd_tsdn(tsd), &tsd_nominal_tsds_lock);\n}\n\nvoid\ntsd_postfork_parent(tsd_t *tsd) {\n\tmalloc_mutex_postfork_parent(tsd_tsdn(tsd), &tsd_nominal_tsds_lock);\n}\n\nvoid\ntsd_postfork_child(tsd_t *tsd) {\n\tmalloc_mutex_postfork_child(tsd_tsdn(tsd), &tsd_nominal_tsds_lock);\n\tql_new(&tsd_nominal_tsds);\n\n\tif (tsd_state_get(tsd) <= tsd_state_nominal_max) {\n\t\ttsd_add_nominal(tsd);\n\t}\n}\n"
  },
  {
    "path": "deps/jemalloc/src/witness.c",
    "content": "#define JEMALLOC_WITNESS_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/malloc_io.h\"\n\nvoid\nwitness_init(witness_t *witness, const char *name, witness_rank_t rank,\n    witness_comp_t *comp, void *opaque) {\n\twitness->name = name;\n\twitness->rank = rank;\n\twitness->comp = comp;\n\twitness->opaque = opaque;\n}\n\nstatic void\nwitness_lock_error_impl(const witness_list_t *witnesses,\n    const witness_t *witness) {\n\twitness_t *w;\n\n\tmalloc_printf(\"<jemalloc>: Lock rank order reversal:\");\n\tql_foreach(w, witnesses, link) {\n\t\tmalloc_printf(\" %s(%u)\", w->name, w->rank);\n\t}\n\tmalloc_printf(\" %s(%u)\\n\", witness->name, witness->rank);\n\tabort();\n}\nwitness_lock_error_t *JET_MUTABLE witness_lock_error = witness_lock_error_impl;\n\nstatic void\nwitness_owner_error_impl(const witness_t *witness) {\n\tmalloc_printf(\"<jemalloc>: Should own %s(%u)\\n\", witness->name,\n\t    witness->rank);\n\tabort();\n}\nwitness_owner_error_t *JET_MUTABLE witness_owner_error =\n    witness_owner_error_impl;\n\nstatic void\nwitness_not_owner_error_impl(const witness_t *witness) {\n\tmalloc_printf(\"<jemalloc>: Should not own %s(%u)\\n\", witness->name,\n\t    witness->rank);\n\tabort();\n}\nwitness_not_owner_error_t *JET_MUTABLE witness_not_owner_error =\n    witness_not_owner_error_impl;\n\nstatic void\nwitness_depth_error_impl(const witness_list_t *witnesses,\n    witness_rank_t rank_inclusive, unsigned depth) {\n\twitness_t *w;\n\n\tmalloc_printf(\"<jemalloc>: Should own %u lock%s of rank >= %u:\", depth,\n\t    (depth != 1) ?  \"s\" : \"\", rank_inclusive);\n\tql_foreach(w, witnesses, link) {\n\t\tmalloc_printf(\" %s(%u)\", w->name, w->rank);\n\t}\n\tmalloc_printf(\"\\n\");\n\tabort();\n}\nwitness_depth_error_t *JET_MUTABLE witness_depth_error =\n    witness_depth_error_impl;\n\nvoid\nwitnesses_cleanup(witness_tsd_t *witness_tsd) {\n\twitness_assert_lockless(witness_tsd_tsdn(witness_tsd));\n\n\t/* Do nothing. */\n}\n\nvoid\nwitness_prefork(witness_tsd_t *witness_tsd) {\n\tif (!config_debug) {\n\t\treturn;\n\t}\n\twitness_tsd->forking = true;\n}\n\nvoid\nwitness_postfork_parent(witness_tsd_t *witness_tsd) {\n\tif (!config_debug) {\n\t\treturn;\n\t}\n\twitness_tsd->forking = false;\n}\n\nvoid\nwitness_postfork_child(witness_tsd_t *witness_tsd) {\n\tif (!config_debug) {\n\t\treturn;\n\t}\n#ifndef JEMALLOC_MUTEX_INIT_CB\n\twitness_list_t *witnesses;\n\n\twitnesses = &witness_tsd->witnesses;\n\tql_new(witnesses);\n#endif\n\twitness_tsd->forking = false;\n}\n"
  },
  {
    "path": "deps/jemalloc/src/zone.c",
    "content": "#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n\n#ifndef JEMALLOC_ZONE\n#  error \"This source file is for zones on Darwin (OS X).\"\n#endif\n\n/* Definitions of the following structs in malloc/malloc.h might be too old\n * for the built binary to run on newer versions of OSX. So use the newest\n * possible version of those structs.\n */\ntypedef struct _malloc_zone_t {\n\tvoid *reserved1;\n\tvoid *reserved2;\n\tsize_t (*size)(struct _malloc_zone_t *, const void *);\n\tvoid *(*malloc)(struct _malloc_zone_t *, size_t);\n\tvoid *(*calloc)(struct _malloc_zone_t *, size_t, size_t);\n\tvoid *(*valloc)(struct _malloc_zone_t *, size_t);\n\tvoid (*free)(struct _malloc_zone_t *, void *);\n\tvoid *(*realloc)(struct _malloc_zone_t *, void *, size_t);\n\tvoid (*destroy)(struct _malloc_zone_t *);\n\tconst char *zone_name;\n\tunsigned (*batch_malloc)(struct _malloc_zone_t *, size_t, void **, unsigned);\n\tvoid (*batch_free)(struct _malloc_zone_t *, void **, unsigned);\n\tstruct malloc_introspection_t *introspect;\n\tunsigned version;\n\tvoid *(*memalign)(struct _malloc_zone_t *, size_t, size_t);\n\tvoid (*free_definite_size)(struct _malloc_zone_t *, void *, size_t);\n\tsize_t (*pressure_relief)(struct _malloc_zone_t *, size_t);\n} malloc_zone_t;\n\ntypedef struct {\n\tvm_address_t address;\n\tvm_size_t size;\n} vm_range_t;\n\ntypedef struct malloc_statistics_t {\n\tunsigned blocks_in_use;\n\tsize_t size_in_use;\n\tsize_t max_size_in_use;\n\tsize_t size_allocated;\n} malloc_statistics_t;\n\ntypedef kern_return_t memory_reader_t(task_t, vm_address_t, vm_size_t, void **);\n\ntypedef void vm_range_recorder_t(task_t, void *, unsigned type, vm_range_t *, unsigned);\n\ntypedef struct malloc_introspection_t {\n\tkern_return_t (*enumerator)(task_t, void *, unsigned, vm_address_t, memory_reader_t, vm_range_recorder_t);\n\tsize_t (*good_size)(malloc_zone_t *, size_t);\n\tboolean_t (*check)(malloc_zone_t *);\n\tvoid (*print)(malloc_zone_t *, boolean_t);\n\tvoid (*log)(malloc_zone_t *, void *);\n\tvoid (*force_lock)(malloc_zone_t *);\n\tvoid (*force_unlock)(malloc_zone_t *);\n\tvoid (*statistics)(malloc_zone_t *, malloc_statistics_t *);\n\tboolean_t (*zone_locked)(malloc_zone_t *);\n\tboolean_t (*enable_discharge_checking)(malloc_zone_t *);\n\tboolean_t (*disable_discharge_checking)(malloc_zone_t *);\n\tvoid (*discharge)(malloc_zone_t *, void *);\n#ifdef __BLOCKS__\n\tvoid (*enumerate_discharged_pointers)(malloc_zone_t *, void (^)(void *, void *));\n#else\n\tvoid *enumerate_unavailable_without_blocks;\n#endif\n\tvoid (*reinit_lock)(malloc_zone_t *);\n} malloc_introspection_t;\n\nextern kern_return_t malloc_get_all_zones(task_t, memory_reader_t, vm_address_t **, unsigned *);\n\nextern malloc_zone_t *malloc_default_zone(void);\n\nextern void malloc_zone_register(malloc_zone_t *zone);\n\nextern void malloc_zone_unregister(malloc_zone_t *zone);\n\n/*\n * The malloc_default_purgeable_zone() function is only available on >= 10.6.\n * We need to check whether it is present at runtime, thus the weak_import.\n */\nextern malloc_zone_t *malloc_default_purgeable_zone(void)\nJEMALLOC_ATTR(weak_import);\n\n/******************************************************************************/\n/* Data. */\n\nstatic malloc_zone_t *default_zone, *purgeable_zone;\nstatic malloc_zone_t jemalloc_zone;\nstatic struct malloc_introspection_t jemalloc_zone_introspect;\nstatic pid_t zone_force_lock_pid = -1;\n\n/******************************************************************************/\n/* Function prototypes for non-inline static functions. */\n\nstatic size_t\tzone_size(malloc_zone_t *zone, const void *ptr);\nstatic void\t*zone_malloc(malloc_zone_t *zone, size_t size);\nstatic void\t*zone_calloc(malloc_zone_t *zone, size_t num, size_t size);\nstatic void\t*zone_valloc(malloc_zone_t *zone, size_t size);\nstatic void\tzone_free(malloc_zone_t *zone, void *ptr);\nstatic void\t*zone_realloc(malloc_zone_t *zone, void *ptr, size_t size);\nstatic void\t*zone_memalign(malloc_zone_t *zone, size_t alignment,\n    size_t size);\nstatic void\tzone_free_definite_size(malloc_zone_t *zone, void *ptr,\n    size_t size);\nstatic void\tzone_destroy(malloc_zone_t *zone);\nstatic unsigned\tzone_batch_malloc(struct _malloc_zone_t *zone, size_t size,\n    void **results, unsigned num_requested);\nstatic void\tzone_batch_free(struct _malloc_zone_t *zone,\n    void **to_be_freed, unsigned num_to_be_freed);\nstatic size_t\tzone_pressure_relief(struct _malloc_zone_t *zone, size_t goal);\nstatic size_t\tzone_good_size(malloc_zone_t *zone, size_t size);\nstatic kern_return_t\tzone_enumerator(task_t task, void *data, unsigned type_mask,\n    vm_address_t zone_address, memory_reader_t reader,\n    vm_range_recorder_t recorder);\nstatic boolean_t\tzone_check(malloc_zone_t *zone);\nstatic void\tzone_print(malloc_zone_t *zone, boolean_t verbose);\nstatic void\tzone_log(malloc_zone_t *zone, void *address);\nstatic void\tzone_force_lock(malloc_zone_t *zone);\nstatic void\tzone_force_unlock(malloc_zone_t *zone);\nstatic void\tzone_statistics(malloc_zone_t *zone,\n    malloc_statistics_t *stats);\nstatic boolean_t\tzone_locked(malloc_zone_t *zone);\nstatic void\tzone_reinit_lock(malloc_zone_t *zone);\n\n/******************************************************************************/\n/*\n * Functions.\n */\n\nstatic size_t\nzone_size(malloc_zone_t *zone, const void *ptr) {\n\t/*\n\t * There appear to be places within Darwin (such as setenv(3)) that\n\t * cause calls to this function with pointers that *no* zone owns.  If\n\t * we knew that all pointers were owned by *some* zone, we could split\n\t * our zone into two parts, and use one as the default allocator and\n\t * the other as the default deallocator/reallocator.  Since that will\n\t * not work in practice, we must check all pointers to assure that they\n\t * reside within a mapped extent before determining size.\n\t */\n\treturn ivsalloc(tsdn_fetch(), ptr);\n}\n\nstatic void *\nzone_malloc(malloc_zone_t *zone, size_t size) {\n\treturn je_malloc(size);\n}\n\nstatic void *\nzone_calloc(malloc_zone_t *zone, size_t num, size_t size) {\n\treturn je_calloc(num, size);\n}\n\nstatic void *\nzone_valloc(malloc_zone_t *zone, size_t size) {\n\tvoid *ret = NULL; /* Assignment avoids useless compiler warning. */\n\n\tje_posix_memalign(&ret, PAGE, size);\n\n\treturn ret;\n}\n\nstatic void\nzone_free(malloc_zone_t *zone, void *ptr) {\n\tif (ivsalloc(tsdn_fetch(), ptr) != 0) {\n\t\tje_free(ptr);\n\t\treturn;\n\t}\n\n\tfree(ptr);\n}\n\nstatic void *\nzone_realloc(malloc_zone_t *zone, void *ptr, size_t size) {\n\tif (ivsalloc(tsdn_fetch(), ptr) != 0) {\n\t\treturn je_realloc(ptr, size);\n\t}\n\n\treturn realloc(ptr, size);\n}\n\nstatic void *\nzone_memalign(malloc_zone_t *zone, size_t alignment, size_t size) {\n\tvoid *ret = NULL; /* Assignment avoids useless compiler warning. */\n\n\tje_posix_memalign(&ret, alignment, size);\n\n\treturn ret;\n}\n\nstatic void\nzone_free_definite_size(malloc_zone_t *zone, void *ptr, size_t size) {\n\tsize_t alloc_size;\n\n\talloc_size = ivsalloc(tsdn_fetch(), ptr);\n\tif (alloc_size != 0) {\n\t\tassert(alloc_size == size);\n\t\tje_free(ptr);\n\t\treturn;\n\t}\n\n\tfree(ptr);\n}\n\nstatic void\nzone_destroy(malloc_zone_t *zone) {\n\t/* This function should never be called. */\n\tnot_reached();\n}\n\nstatic unsigned\nzone_batch_malloc(struct _malloc_zone_t *zone, size_t size, void **results,\n    unsigned num_requested) {\n\tunsigned i;\n\n\tfor (i = 0; i < num_requested; i++) {\n\t\tresults[i] = je_malloc(size);\n\t\tif (!results[i])\n\t\t\tbreak;\n\t}\n\n\treturn i;\n}\n\nstatic void\nzone_batch_free(struct _malloc_zone_t *zone, void **to_be_freed,\n    unsigned num_to_be_freed) {\n\tunsigned i;\n\n\tfor (i = 0; i < num_to_be_freed; i++) {\n\t\tzone_free(zone, to_be_freed[i]);\n\t\tto_be_freed[i] = NULL;\n\t}\n}\n\nstatic size_t\nzone_pressure_relief(struct _malloc_zone_t *zone, size_t goal) {\n\treturn 0;\n}\n\nstatic size_t\nzone_good_size(malloc_zone_t *zone, size_t size) {\n\tif (size == 0) {\n\t\tsize = 1;\n\t}\n\treturn sz_s2u(size);\n}\n\nstatic kern_return_t\nzone_enumerator(task_t task, void *data, unsigned type_mask,\n    vm_address_t zone_address, memory_reader_t reader,\n    vm_range_recorder_t recorder) {\n\treturn KERN_SUCCESS;\n}\n\nstatic boolean_t\nzone_check(malloc_zone_t *zone) {\n\treturn true;\n}\n\nstatic void\nzone_print(malloc_zone_t *zone, boolean_t verbose) {\n}\n\nstatic void\nzone_log(malloc_zone_t *zone, void *address) {\n}\n\nstatic void\nzone_force_lock(malloc_zone_t *zone) {\n\tif (isthreaded) {\n\t\t/*\n\t\t * See the note in zone_force_unlock, below, to see why we need\n\t\t * this.\n\t\t */\n\t\tassert(zone_force_lock_pid == -1);\n\t\tzone_force_lock_pid = getpid();\n\t\tjemalloc_prefork();\n\t}\n}\n\nstatic void\nzone_force_unlock(malloc_zone_t *zone) {\n\t/*\n\t * zone_force_lock and zone_force_unlock are the entry points to the\n\t * forking machinery on OS X.  The tricky thing is, the child is not\n\t * allowed to unlock mutexes locked in the parent, even if owned by the\n\t * forking thread (and the mutex type we use in OS X will fail an assert\n\t * if we try).  In the child, we can get away with reinitializing all\n\t * the mutexes, which has the effect of unlocking them.  In the parent,\n\t * doing this would mean we wouldn't wake any waiters blocked on the\n\t * mutexes we unlock.  So, we record the pid of the current thread in\n\t * zone_force_lock, and use that to detect if we're in the parent or\n\t * child here, to decide which unlock logic we need.\n\t */\n\tif (isthreaded) {\n\t\tassert(zone_force_lock_pid != -1);\n\t\tif (getpid() == zone_force_lock_pid) {\n\t\t\tjemalloc_postfork_parent();\n\t\t} else {\n\t\t\tjemalloc_postfork_child();\n\t\t}\n\t\tzone_force_lock_pid = -1;\n\t}\n}\n\nstatic void\nzone_statistics(malloc_zone_t *zone, malloc_statistics_t *stats) {\n\t/* We make no effort to actually fill the values */\n\tstats->blocks_in_use = 0;\n\tstats->size_in_use = 0;\n\tstats->max_size_in_use = 0;\n\tstats->size_allocated = 0;\n}\n\nstatic boolean_t\nzone_locked(malloc_zone_t *zone) {\n\t/* Pretend no lock is being held */\n\treturn false;\n}\n\nstatic void\nzone_reinit_lock(malloc_zone_t *zone) {\n\t/* As of OSX 10.12, this function is only used when force_unlock would\n\t * be used if the zone version were < 9. So just use force_unlock. */\n\tzone_force_unlock(zone);\n}\n\nstatic void\nzone_init(void) {\n\tjemalloc_zone.size = zone_size;\n\tjemalloc_zone.malloc = zone_malloc;\n\tjemalloc_zone.calloc = zone_calloc;\n\tjemalloc_zone.valloc = zone_valloc;\n\tjemalloc_zone.free = zone_free;\n\tjemalloc_zone.realloc = zone_realloc;\n\tjemalloc_zone.destroy = zone_destroy;\n\tjemalloc_zone.zone_name = \"jemalloc_zone\";\n\tjemalloc_zone.batch_malloc = zone_batch_malloc;\n\tjemalloc_zone.batch_free = zone_batch_free;\n\tjemalloc_zone.introspect = &jemalloc_zone_introspect;\n\tjemalloc_zone.version = 9;\n\tjemalloc_zone.memalign = zone_memalign;\n\tjemalloc_zone.free_definite_size = zone_free_definite_size;\n\tjemalloc_zone.pressure_relief = zone_pressure_relief;\n\n\tjemalloc_zone_introspect.enumerator = zone_enumerator;\n\tjemalloc_zone_introspect.good_size = zone_good_size;\n\tjemalloc_zone_introspect.check = zone_check;\n\tjemalloc_zone_introspect.print = zone_print;\n\tjemalloc_zone_introspect.log = zone_log;\n\tjemalloc_zone_introspect.force_lock = zone_force_lock;\n\tjemalloc_zone_introspect.force_unlock = zone_force_unlock;\n\tjemalloc_zone_introspect.statistics = zone_statistics;\n\tjemalloc_zone_introspect.zone_locked = zone_locked;\n\tjemalloc_zone_introspect.enable_discharge_checking = NULL;\n\tjemalloc_zone_introspect.disable_discharge_checking = NULL;\n\tjemalloc_zone_introspect.discharge = NULL;\n#ifdef __BLOCKS__\n\tjemalloc_zone_introspect.enumerate_discharged_pointers = NULL;\n#else\n\tjemalloc_zone_introspect.enumerate_unavailable_without_blocks = NULL;\n#endif\n\tjemalloc_zone_introspect.reinit_lock = zone_reinit_lock;\n}\n\nstatic malloc_zone_t *\nzone_default_get(void) {\n\tmalloc_zone_t **zones = NULL;\n\tunsigned int num_zones = 0;\n\n\t/*\n\t * On OSX 10.12, malloc_default_zone returns a special zone that is not\n\t * present in the list of registered zones. That zone uses a \"lite zone\"\n\t * if one is present (apparently enabled when malloc stack logging is\n\t * enabled), or the first registered zone otherwise. In practice this\n\t * means unless malloc stack logging is enabled, the first registered\n\t * zone is the default.  So get the list of zones to get the first one,\n\t * instead of relying on malloc_default_zone.\n\t */\n\tif (KERN_SUCCESS != malloc_get_all_zones(0, NULL,\n\t    (vm_address_t**)&zones, &num_zones)) {\n\t\t/*\n\t\t * Reset the value in case the failure happened after it was\n\t\t * set.\n\t\t */\n\t\tnum_zones = 0;\n\t}\n\n\tif (num_zones) {\n\t\treturn zones[0];\n\t}\n\n\treturn malloc_default_zone();\n}\n\n/* As written, this function can only promote jemalloc_zone. */\nstatic void\nzone_promote(void) {\n\tmalloc_zone_t *zone;\n\n\tdo {\n\t\t/*\n\t\t * Unregister and reregister the default zone.  On OSX >= 10.6,\n\t\t * unregistering takes the last registered zone and places it\n\t\t * at the location of the specified zone.  Unregistering the\n\t\t * default zone thus makes the last registered one the default.\n\t\t * On OSX < 10.6, unregistering shifts all registered zones.\n\t\t * The first registered zone then becomes the default.\n\t\t */\n\t\tmalloc_zone_unregister(default_zone);\n\t\tmalloc_zone_register(default_zone);\n\n\t\t/*\n\t\t * On OSX 10.6, having the default purgeable zone appear before\n\t\t * the default zone makes some things crash because it thinks it\n\t\t * owns the default zone allocated pointers.  We thus\n\t\t * unregister/re-register it in order to ensure it's always\n\t\t * after the default zone.  On OSX < 10.6, there is no purgeable\n\t\t * zone, so this does nothing.  On OSX >= 10.6, unregistering\n\t\t * replaces the purgeable zone with the last registered zone\n\t\t * above, i.e. the default zone.  Registering it again then puts\n\t\t * it at the end, obviously after the default zone.\n\t\t */\n\t\tif (purgeable_zone != NULL) {\n\t\t\tmalloc_zone_unregister(purgeable_zone);\n\t\t\tmalloc_zone_register(purgeable_zone);\n\t\t}\n\n\t\tzone = zone_default_get();\n\t} while (zone != &jemalloc_zone);\n}\n\nJEMALLOC_ATTR(constructor)\nvoid\nzone_register(void) {\n\t/*\n\t * If something else replaced the system default zone allocator, don't\n\t * register jemalloc's.\n\t */\n\tdefault_zone = zone_default_get();\n\tif (!default_zone->zone_name || strcmp(default_zone->zone_name,\n\t    \"DefaultMallocZone\") != 0) {\n\t\treturn;\n\t}\n\n\t/*\n\t * The default purgeable zone is created lazily by OSX's libc.  It uses\n\t * the default zone when it is created for \"small\" allocations\n\t * (< 15 KiB), but assumes the default zone is a scalable_zone.  This\n\t * obviously fails when the default zone is the jemalloc zone, so\n\t * malloc_default_purgeable_zone() is called beforehand so that the\n\t * default purgeable zone is created when the default zone is still\n\t * a scalable_zone.  As purgeable zones only exist on >= 10.6, we need\n\t * to check for the existence of malloc_default_purgeable_zone() at\n\t * run time.\n\t */\n\tpurgeable_zone = (malloc_default_purgeable_zone == NULL) ? NULL :\n\t    malloc_default_purgeable_zone();\n\n\t/* Register the custom zone.  At this point it won't be the default. */\n\tzone_init();\n\tmalloc_zone_register(&jemalloc_zone);\n\n\t/* Promote the custom zone to be default. */\n\tzone_promote();\n}\n"
  },
  {
    "path": "deps/jemalloc/test/include/test/SFMT-alti.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n/**\n * @file SFMT-alti.h\n *\n * @brief SIMD oriented Fast Mersenne Twister(SFMT)\n * pseudorandom number generator\n *\n * @author Mutsuo Saito (Hiroshima University)\n * @author Makoto Matsumoto (Hiroshima University)\n *\n * Copyright (C) 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n * University. All rights reserved.\n *\n * The new BSD License is applied to this software.\n * see LICENSE.txt\n */\n\n#ifndef SFMT_ALTI_H\n#define SFMT_ALTI_H\n\n/**\n * This function represents the recursion formula in AltiVec and BIG ENDIAN.\n * @param a a 128-bit part of the interal state array\n * @param b a 128-bit part of the interal state array\n * @param c a 128-bit part of the interal state array\n * @param d a 128-bit part of the interal state array\n * @return output\n */\nJEMALLOC_ALWAYS_INLINE\nvector unsigned int vec_recursion(vector unsigned int a,\n\t\t\t\t\t\tvector unsigned int b,\n\t\t\t\t\t\tvector unsigned int c,\n\t\t\t\t\t\tvector unsigned int d) {\n\n    const vector unsigned int sl1 = ALTI_SL1;\n    const vector unsigned int sr1 = ALTI_SR1;\n#ifdef ONLY64\n    const vector unsigned int mask = ALTI_MSK64;\n    const vector unsigned char perm_sl = ALTI_SL2_PERM64;\n    const vector unsigned char perm_sr = ALTI_SR2_PERM64;\n#else\n    const vector unsigned int mask = ALTI_MSK;\n    const vector unsigned char perm_sl = ALTI_SL2_PERM;\n    const vector unsigned char perm_sr = ALTI_SR2_PERM;\n#endif\n    vector unsigned int v, w, x, y, z;\n    x = vec_perm(a, (vector unsigned int)perm_sl, perm_sl);\n    v = a;\n    y = vec_sr(b, sr1);\n    z = vec_perm(c, (vector unsigned int)perm_sr, perm_sr);\n    w = vec_sl(d, sl1);\n    z = vec_xor(z, w);\n    y = vec_and(y, mask);\n    v = vec_xor(v, x);\n    z = vec_xor(z, y);\n    z = vec_xor(z, v);\n    return z;\n}\n\n/**\n * This function fills the internal state array with pseudorandom\n * integers.\n */\nstatic inline void gen_rand_all(sfmt_t *ctx) {\n    int i;\n    vector unsigned int r, r1, r2;\n\n    r1 = ctx->sfmt[N - 2].s;\n    r2 = ctx->sfmt[N - 1].s;\n    for (i = 0; i < N - POS1; i++) {\n\tr = vec_recursion(ctx->sfmt[i].s, ctx->sfmt[i + POS1].s, r1, r2);\n\tctx->sfmt[i].s = r;\n\tr1 = r2;\n\tr2 = r;\n    }\n    for (; i < N; i++) {\n\tr = vec_recursion(ctx->sfmt[i].s, ctx->sfmt[i + POS1 - N].s, r1, r2);\n\tctx->sfmt[i].s = r;\n\tr1 = r2;\n\tr2 = r;\n    }\n}\n\n/**\n * This function fills the user-specified array with pseudorandom\n * integers.\n *\n * @param array an 128-bit array to be filled by pseudorandom numbers.\n * @param size number of 128-bit pesudorandom numbers to be generated.\n */\nstatic inline void gen_rand_array(sfmt_t *ctx, w128_t *array, int size) {\n    int i, j;\n    vector unsigned int r, r1, r2;\n\n    r1 = ctx->sfmt[N - 2].s;\n    r2 = ctx->sfmt[N - 1].s;\n    for (i = 0; i < N - POS1; i++) {\n\tr = vec_recursion(ctx->sfmt[i].s, ctx->sfmt[i + POS1].s, r1, r2);\n\tarray[i].s = r;\n\tr1 = r2;\n\tr2 = r;\n    }\n    for (; i < N; i++) {\n\tr = vec_recursion(ctx->sfmt[i].s, array[i + POS1 - N].s, r1, r2);\n\tarray[i].s = r;\n\tr1 = r2;\n\tr2 = r;\n    }\n    /* main loop */\n    for (; i < size - N; i++) {\n\tr = vec_recursion(array[i - N].s, array[i + POS1 - N].s, r1, r2);\n\tarray[i].s = r;\n\tr1 = r2;\n\tr2 = r;\n    }\n    for (j = 0; j < 2 * N - size; j++) {\n\tctx->sfmt[j].s = array[j + size - N].s;\n    }\n    for (; i < size; i++) {\n\tr = vec_recursion(array[i - N].s, array[i + POS1 - N].s, r1, r2);\n\tarray[i].s = r;\n\tctx->sfmt[j++].s = r;\n\tr1 = r2;\n\tr2 = r;\n    }\n}\n\n#ifndef ONLY64\n#if defined(__APPLE__)\n#define ALTI_SWAP (vector unsigned char) \\\n\t(4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11)\n#else\n#define ALTI_SWAP {4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11}\n#endif\n/**\n * This function swaps high and low 32-bit of 64-bit integers in user\n * specified array.\n *\n * @param array an 128-bit array to be swaped.\n * @param size size of 128-bit array.\n */\nstatic inline void swap(w128_t *array, int size) {\n    int i;\n    const vector unsigned char perm = ALTI_SWAP;\n\n    for (i = 0; i < size; i++) {\n\tarray[i].s = vec_perm(array[i].s, (vector unsigned int)perm, perm);\n    }\n}\n#endif\n\n#endif\n"
  },
  {
    "path": "deps/jemalloc/test/include/test/SFMT-params.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#ifndef SFMT_PARAMS_H\n#define SFMT_PARAMS_H\n\n#if !defined(MEXP)\n#ifdef __GNUC__\n  #warning \"MEXP is not defined. I assume MEXP is 19937.\"\n#endif\n  #define MEXP 19937\n#endif\n/*-----------------\n  BASIC DEFINITIONS\n  -----------------*/\n/** Mersenne Exponent. The period of the sequence \n *  is a multiple of 2^MEXP-1.\n * #define MEXP 19937 */\n/** SFMT generator has an internal state array of 128-bit integers,\n * and N is its size. */\n#define N (MEXP / 128 + 1)\n/** N32 is the size of internal state array when regarded as an array\n * of 32-bit integers.*/\n#define N32 (N * 4)\n/** N64 is the size of internal state array when regarded as an array\n * of 64-bit integers.*/\n#define N64 (N * 2)\n\n/*----------------------\n  the parameters of SFMT\n  following definitions are in paramsXXXX.h file.\n  ----------------------*/\n/** the pick up position of the array.\n#define POS1 122 \n*/\n\n/** the parameter of shift left as four 32-bit registers.\n#define SL1 18\n */\n\n/** the parameter of shift left as one 128-bit register. \n * The 128-bit integer is shifted by (SL2 * 8) bits. \n#define SL2 1 \n*/\n\n/** the parameter of shift right as four 32-bit registers.\n#define SR1 11\n*/\n\n/** the parameter of shift right as one 128-bit register. \n * The 128-bit integer is shifted by (SL2 * 8) bits. \n#define SR2 1 \n*/\n\n/** A bitmask, used in the recursion.  These parameters are introduced\n * to break symmetry of SIMD.\n#define MSK1 0xdfffffefU\n#define MSK2 0xddfecb7fU\n#define MSK3 0xbffaffffU\n#define MSK4 0xbffffff6U \n*/\n\n/** These definitions are part of a 128-bit period certification vector.\n#define PARITY1\t0x00000001U\n#define PARITY2\t0x00000000U\n#define PARITY3\t0x00000000U\n#define PARITY4\t0xc98e126aU\n*/\n\n#if MEXP == 607\n  #include \"test/SFMT-params607.h\"\n#elif MEXP == 1279\n  #include \"test/SFMT-params1279.h\"\n#elif MEXP == 2281\n  #include \"test/SFMT-params2281.h\"\n#elif MEXP == 4253\n  #include \"test/SFMT-params4253.h\"\n#elif MEXP == 11213\n  #include \"test/SFMT-params11213.h\"\n#elif MEXP == 19937\n  #include \"test/SFMT-params19937.h\"\n#elif MEXP == 44497\n  #include \"test/SFMT-params44497.h\"\n#elif MEXP == 86243\n  #include \"test/SFMT-params86243.h\"\n#elif MEXP == 132049\n  #include \"test/SFMT-params132049.h\"\n#elif MEXP == 216091\n  #include \"test/SFMT-params216091.h\"\n#else\n#ifdef __GNUC__\n  #error \"MEXP is not valid.\"\n  #undef MEXP\n#else\n  #undef MEXP\n#endif\n\n#endif\n\n#endif /* SFMT_PARAMS_H */\n"
  },
  {
    "path": "deps/jemalloc/test/include/test/SFMT-params11213.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#ifndef SFMT_PARAMS11213_H\n#define SFMT_PARAMS11213_H\n\n#define POS1\t68\n#define SL1\t14\n#define SL2\t3\n#define SR1\t7\n#define SR2\t3\n#define MSK1\t0xeffff7fbU\n#define MSK2\t0xffffffefU\n#define MSK3\t0xdfdfbfffU\n#define MSK4\t0x7fffdbfdU\n#define PARITY1\t0x00000001U\n#define PARITY2\t0x00000000U\n#define PARITY3\t0xe8148000U\n#define PARITY4\t0xd0c7afa3U\n\n\n/* PARAMETERS FOR ALTIVEC */\n#if defined(__APPLE__)\t/* For OSX */\n    #define ALTI_SL1\t(vector unsigned int)(SL1, SL1, SL1, SL1)\n    #define ALTI_SR1\t(vector unsigned int)(SR1, SR1, SR1, SR1)\n    #define ALTI_MSK\t(vector unsigned int)(MSK1, MSK2, MSK3, MSK4)\n    #define ALTI_MSK64 \\\n\t(vector unsigned int)(MSK2, MSK1, MSK4, MSK3)\n    #define ALTI_SL2_PERM \\\n\t(vector unsigned char)(3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10)\n    #define ALTI_SL2_PERM64 \\\n\t(vector unsigned char)(3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2)\n    #define ALTI_SR2_PERM \\\n\t(vector unsigned char)(5,6,7,0,9,10,11,4,13,14,15,8,19,19,19,12)\n    #define ALTI_SR2_PERM64 \\\n\t(vector unsigned char)(13,14,15,0,1,2,3,4,19,19,19,8,9,10,11,12)\n#else\t/* For OTHER OSs(Linux?) */\n    #define ALTI_SL1\t{SL1, SL1, SL1, SL1}\n    #define ALTI_SR1\t{SR1, SR1, SR1, SR1}\n    #define ALTI_MSK\t{MSK1, MSK2, MSK3, MSK4}\n    #define ALTI_MSK64\t{MSK2, MSK1, MSK4, MSK3}\n    #define ALTI_SL2_PERM\t{3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10}\n    #define ALTI_SL2_PERM64\t{3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2}\n    #define ALTI_SR2_PERM\t{5,6,7,0,9,10,11,4,13,14,15,8,19,19,19,12}\n    #define ALTI_SR2_PERM64\t{13,14,15,0,1,2,3,4,19,19,19,8,9,10,11,12}\n#endif\t/* For OSX */\n#define IDSTR\t\"SFMT-11213:68-14-3-7-3:effff7fb-ffffffef-dfdfbfff-7fffdbfd\"\n\n#endif /* SFMT_PARAMS11213_H */\n"
  },
  {
    "path": "deps/jemalloc/test/include/test/SFMT-params1279.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#ifndef SFMT_PARAMS1279_H\n#define SFMT_PARAMS1279_H\n\n#define POS1\t7\n#define SL1\t14\n#define SL2\t3\n#define SR1\t5\n#define SR2\t1\n#define MSK1\t0xf7fefffdU\n#define MSK2\t0x7fefcfffU\n#define MSK3\t0xaff3ef3fU\n#define MSK4\t0xb5ffff7fU\n#define PARITY1\t0x00000001U\n#define PARITY2\t0x00000000U\n#define PARITY3\t0x00000000U\n#define PARITY4\t0x20000000U\n\n\n/* PARAMETERS FOR ALTIVEC */\n#if defined(__APPLE__)\t/* For OSX */\n    #define ALTI_SL1\t(vector unsigned int)(SL1, SL1, SL1, SL1)\n    #define ALTI_SR1\t(vector unsigned int)(SR1, SR1, SR1, SR1)\n    #define ALTI_MSK\t(vector unsigned int)(MSK1, MSK2, MSK3, MSK4)\n    #define ALTI_MSK64 \\\n\t(vector unsigned int)(MSK2, MSK1, MSK4, MSK3)\n    #define ALTI_SL2_PERM \\\n\t(vector unsigned char)(3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10)\n    #define ALTI_SL2_PERM64 \\\n\t(vector unsigned char)(3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2)\n    #define ALTI_SR2_PERM \\\n\t(vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14)\n    #define ALTI_SR2_PERM64 \\\n\t(vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14)\n#else\t/* For OTHER OSs(Linux?) */\n    #define ALTI_SL1\t{SL1, SL1, SL1, SL1}\n    #define ALTI_SR1\t{SR1, SR1, SR1, SR1}\n    #define ALTI_MSK\t{MSK1, MSK2, MSK3, MSK4}\n    #define ALTI_MSK64\t{MSK2, MSK1, MSK4, MSK3}\n    #define ALTI_SL2_PERM\t{3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10}\n    #define ALTI_SL2_PERM64\t{3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2}\n    #define ALTI_SR2_PERM\t{7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14}\n    #define ALTI_SR2_PERM64\t{15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14}\n#endif\t/* For OSX */\n#define IDSTR\t\"SFMT-1279:7-14-3-5-1:f7fefffd-7fefcfff-aff3ef3f-b5ffff7f\"\n\n#endif /* SFMT_PARAMS1279_H */\n"
  },
  {
    "path": "deps/jemalloc/test/include/test/SFMT-params132049.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#ifndef SFMT_PARAMS132049_H\n#define SFMT_PARAMS132049_H\n\n#define POS1\t110\n#define SL1\t19\n#define SL2\t1\n#define SR1\t21\n#define SR2\t1\n#define MSK1\t0xffffbb5fU\n#define MSK2\t0xfb6ebf95U\n#define MSK3\t0xfffefffaU\n#define MSK4\t0xcff77fffU\n#define PARITY1\t0x00000001U\n#define PARITY2\t0x00000000U\n#define PARITY3\t0xcb520000U\n#define PARITY4\t0xc7e91c7dU\n\n\n/* PARAMETERS FOR ALTIVEC */\n#if defined(__APPLE__)\t/* For OSX */\n    #define ALTI_SL1\t(vector unsigned int)(SL1, SL1, SL1, SL1)\n    #define ALTI_SR1\t(vector unsigned int)(SR1, SR1, SR1, SR1)\n    #define ALTI_MSK\t(vector unsigned int)(MSK1, MSK2, MSK3, MSK4)\n    #define ALTI_MSK64 \\\n\t(vector unsigned int)(MSK2, MSK1, MSK4, MSK3)\n    #define ALTI_SL2_PERM \\\n\t(vector unsigned char)(1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8)\n    #define ALTI_SL2_PERM64 \\\n\t(vector unsigned char)(1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0)\n    #define ALTI_SR2_PERM \\\n\t(vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14)\n    #define ALTI_SR2_PERM64 \\\n\t(vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14)\n#else\t/* For OTHER OSs(Linux?) */\n    #define ALTI_SL1\t{SL1, SL1, SL1, SL1}\n    #define ALTI_SR1\t{SR1, SR1, SR1, SR1}\n    #define ALTI_MSK\t{MSK1, MSK2, MSK3, MSK4}\n    #define ALTI_MSK64\t{MSK2, MSK1, MSK4, MSK3}\n    #define ALTI_SL2_PERM\t{1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8}\n    #define ALTI_SL2_PERM64\t{1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0}\n    #define ALTI_SR2_PERM\t{7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14}\n    #define ALTI_SR2_PERM64\t{15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14}\n#endif\t/* For OSX */\n#define IDSTR\t\"SFMT-132049:110-19-1-21-1:ffffbb5f-fb6ebf95-fffefffa-cff77fff\"\n\n#endif /* SFMT_PARAMS132049_H */\n"
  },
  {
    "path": "deps/jemalloc/test/include/test/SFMT-params19937.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#ifndef SFMT_PARAMS19937_H\n#define SFMT_PARAMS19937_H\n\n#define POS1\t122\n#define SL1\t18\n#define SL2\t1\n#define SR1\t11\n#define SR2\t1\n#define MSK1\t0xdfffffefU\n#define MSK2\t0xddfecb7fU\n#define MSK3\t0xbffaffffU\n#define MSK4\t0xbffffff6U\n#define PARITY1\t0x00000001U\n#define PARITY2\t0x00000000U\n#define PARITY3\t0x00000000U\n#define PARITY4\t0x13c9e684U\n\n\n/* PARAMETERS FOR ALTIVEC */\n#if defined(__APPLE__)\t/* For OSX */\n    #define ALTI_SL1\t(vector unsigned int)(SL1, SL1, SL1, SL1)\n    #define ALTI_SR1\t(vector unsigned int)(SR1, SR1, SR1, SR1)\n    #define ALTI_MSK\t(vector unsigned int)(MSK1, MSK2, MSK3, MSK4)\n    #define ALTI_MSK64 \\\n\t(vector unsigned int)(MSK2, MSK1, MSK4, MSK3)\n    #define ALTI_SL2_PERM \\\n\t(vector unsigned char)(1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8)\n    #define ALTI_SL2_PERM64 \\\n\t(vector unsigned char)(1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0)\n    #define ALTI_SR2_PERM \\\n\t(vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14)\n    #define ALTI_SR2_PERM64 \\\n\t(vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14)\n#else\t/* For OTHER OSs(Linux?) */\n    #define ALTI_SL1\t{SL1, SL1, SL1, SL1}\n    #define ALTI_SR1\t{SR1, SR1, SR1, SR1}\n    #define ALTI_MSK\t{MSK1, MSK2, MSK3, MSK4}\n    #define ALTI_MSK64\t{MSK2, MSK1, MSK4, MSK3}\n    #define ALTI_SL2_PERM\t{1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8}\n    #define ALTI_SL2_PERM64\t{1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0}\n    #define ALTI_SR2_PERM\t{7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14}\n    #define ALTI_SR2_PERM64\t{15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14}\n#endif\t/* For OSX */\n#define IDSTR\t\"SFMT-19937:122-18-1-11-1:dfffffef-ddfecb7f-bffaffff-bffffff6\"\n\n#endif /* SFMT_PARAMS19937_H */\n"
  },
  {
    "path": "deps/jemalloc/test/include/test/SFMT-params216091.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#ifndef SFMT_PARAMS216091_H\n#define SFMT_PARAMS216091_H\n\n#define POS1\t627\n#define SL1\t11\n#define SL2\t3\n#define SR1\t10\n#define SR2\t1\n#define MSK1\t0xbff7bff7U\n#define MSK2\t0xbfffffffU\n#define MSK3\t0xbffffa7fU\n#define MSK4\t0xffddfbfbU\n#define PARITY1\t0xf8000001U\n#define PARITY2\t0x89e80709U\n#define PARITY3\t0x3bd2b64bU\n#define PARITY4\t0x0c64b1e4U\n\n\n/* PARAMETERS FOR ALTIVEC */\n#if defined(__APPLE__)\t/* For OSX */\n    #define ALTI_SL1\t(vector unsigned int)(SL1, SL1, SL1, SL1)\n    #define ALTI_SR1\t(vector unsigned int)(SR1, SR1, SR1, SR1)\n    #define ALTI_MSK\t(vector unsigned int)(MSK1, MSK2, MSK3, MSK4)\n    #define ALTI_MSK64 \\\n\t(vector unsigned int)(MSK2, MSK1, MSK4, MSK3)\n    #define ALTI_SL2_PERM \\\n\t(vector unsigned char)(3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10)\n    #define ALTI_SL2_PERM64 \\\n\t(vector unsigned char)(3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2)\n    #define ALTI_SR2_PERM \\\n\t(vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14)\n    #define ALTI_SR2_PERM64 \\\n\t(vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14)\n#else\t/* For OTHER OSs(Linux?) */\n    #define ALTI_SL1\t{SL1, SL1, SL1, SL1}\n    #define ALTI_SR1\t{SR1, SR1, SR1, SR1}\n    #define ALTI_MSK\t{MSK1, MSK2, MSK3, MSK4}\n    #define ALTI_MSK64\t{MSK2, MSK1, MSK4, MSK3}\n    #define ALTI_SL2_PERM\t{3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10}\n    #define ALTI_SL2_PERM64\t{3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2}\n    #define ALTI_SR2_PERM\t{7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14}\n    #define ALTI_SR2_PERM64\t{15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14}\n#endif\t/* For OSX */\n#define IDSTR\t\"SFMT-216091:627-11-3-10-1:bff7bff7-bfffffff-bffffa7f-ffddfbfb\"\n\n#endif /* SFMT_PARAMS216091_H */\n"
  },
  {
    "path": "deps/jemalloc/test/include/test/SFMT-params2281.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#ifndef SFMT_PARAMS2281_H\n#define SFMT_PARAMS2281_H\n\n#define POS1\t12\n#define SL1\t19\n#define SL2\t1\n#define SR1\t5\n#define SR2\t1\n#define MSK1\t0xbff7ffbfU\n#define MSK2\t0xfdfffffeU\n#define MSK3\t0xf7ffef7fU\n#define MSK4\t0xf2f7cbbfU\n#define PARITY1\t0x00000001U\n#define PARITY2\t0x00000000U\n#define PARITY3\t0x00000000U\n#define PARITY4\t0x41dfa600U\n\n\n/* PARAMETERS FOR ALTIVEC */\n#if defined(__APPLE__)\t/* For OSX */\n    #define ALTI_SL1\t(vector unsigned int)(SL1, SL1, SL1, SL1)\n    #define ALTI_SR1\t(vector unsigned int)(SR1, SR1, SR1, SR1)\n    #define ALTI_MSK\t(vector unsigned int)(MSK1, MSK2, MSK3, MSK4)\n    #define ALTI_MSK64 \\\n\t(vector unsigned int)(MSK2, MSK1, MSK4, MSK3)\n    #define ALTI_SL2_PERM \\\n\t(vector unsigned char)(1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8)\n    #define ALTI_SL2_PERM64 \\\n\t(vector unsigned char)(1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0)\n    #define ALTI_SR2_PERM \\\n\t(vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14)\n    #define ALTI_SR2_PERM64 \\\n\t(vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14)\n#else\t/* For OTHER OSs(Linux?) */\n    #define ALTI_SL1\t{SL1, SL1, SL1, SL1}\n    #define ALTI_SR1\t{SR1, SR1, SR1, SR1}\n    #define ALTI_MSK\t{MSK1, MSK2, MSK3, MSK4}\n    #define ALTI_MSK64\t{MSK2, MSK1, MSK4, MSK3}\n    #define ALTI_SL2_PERM\t{1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8}\n    #define ALTI_SL2_PERM64\t{1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0}\n    #define ALTI_SR2_PERM\t{7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14}\n    #define ALTI_SR2_PERM64\t{15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14}\n#endif\t/* For OSX */\n#define IDSTR\t\"SFMT-2281:12-19-1-5-1:bff7ffbf-fdfffffe-f7ffef7f-f2f7cbbf\"\n\n#endif /* SFMT_PARAMS2281_H */\n"
  },
  {
    "path": "deps/jemalloc/test/include/test/SFMT-params4253.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#ifndef SFMT_PARAMS4253_H\n#define SFMT_PARAMS4253_H\n\n#define POS1\t17\n#define SL1\t20\n#define SL2\t1\n#define SR1\t7\n#define SR2\t1\n#define MSK1\t0x9f7bffffU\n#define MSK2\t0x9fffff5fU\n#define MSK3\t0x3efffffbU\n#define MSK4\t0xfffff7bbU\n#define PARITY1\t0xa8000001U\n#define PARITY2\t0xaf5390a3U\n#define PARITY3\t0xb740b3f8U\n#define PARITY4\t0x6c11486dU\n\n\n/* PARAMETERS FOR ALTIVEC */\n#if defined(__APPLE__)\t/* For OSX */\n    #define ALTI_SL1\t(vector unsigned int)(SL1, SL1, SL1, SL1)\n    #define ALTI_SR1\t(vector unsigned int)(SR1, SR1, SR1, SR1)\n    #define ALTI_MSK\t(vector unsigned int)(MSK1, MSK2, MSK3, MSK4)\n    #define ALTI_MSK64 \\\n\t(vector unsigned int)(MSK2, MSK1, MSK4, MSK3)\n    #define ALTI_SL2_PERM \\\n\t(vector unsigned char)(1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8)\n    #define ALTI_SL2_PERM64 \\\n\t(vector unsigned char)(1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0)\n    #define ALTI_SR2_PERM \\\n\t(vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14)\n    #define ALTI_SR2_PERM64 \\\n\t(vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14)\n#else\t/* For OTHER OSs(Linux?) */\n    #define ALTI_SL1\t{SL1, SL1, SL1, SL1}\n    #define ALTI_SR1\t{SR1, SR1, SR1, SR1}\n    #define ALTI_MSK\t{MSK1, MSK2, MSK3, MSK4}\n    #define ALTI_MSK64\t{MSK2, MSK1, MSK4, MSK3}\n    #define ALTI_SL2_PERM\t{1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8}\n    #define ALTI_SL2_PERM64\t{1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0}\n    #define ALTI_SR2_PERM\t{7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14}\n    #define ALTI_SR2_PERM64\t{15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14}\n#endif\t/* For OSX */\n#define IDSTR\t\"SFMT-4253:17-20-1-7-1:9f7bffff-9fffff5f-3efffffb-fffff7bb\"\n\n#endif /* SFMT_PARAMS4253_H */\n"
  },
  {
    "path": "deps/jemalloc/test/include/test/SFMT-params44497.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#ifndef SFMT_PARAMS44497_H\n#define SFMT_PARAMS44497_H\n\n#define POS1\t330\n#define SL1\t5\n#define SL2\t3\n#define SR1\t9\n#define SR2\t3\n#define MSK1\t0xeffffffbU\n#define MSK2\t0xdfbebfffU\n#define MSK3\t0xbfbf7befU\n#define MSK4\t0x9ffd7bffU\n#define PARITY1\t0x00000001U\n#define PARITY2\t0x00000000U\n#define PARITY3\t0xa3ac4000U\n#define PARITY4\t0xecc1327aU\n\n\n/* PARAMETERS FOR ALTIVEC */\n#if defined(__APPLE__)\t/* For OSX */\n    #define ALTI_SL1\t(vector unsigned int)(SL1, SL1, SL1, SL1)\n    #define ALTI_SR1\t(vector unsigned int)(SR1, SR1, SR1, SR1)\n    #define ALTI_MSK\t(vector unsigned int)(MSK1, MSK2, MSK3, MSK4)\n    #define ALTI_MSK64 \\\n\t(vector unsigned int)(MSK2, MSK1, MSK4, MSK3)\n    #define ALTI_SL2_PERM \\\n\t(vector unsigned char)(3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10)\n    #define ALTI_SL2_PERM64 \\\n\t(vector unsigned char)(3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2)\n    #define ALTI_SR2_PERM \\\n\t(vector unsigned char)(5,6,7,0,9,10,11,4,13,14,15,8,19,19,19,12)\n    #define ALTI_SR2_PERM64 \\\n\t(vector unsigned char)(13,14,15,0,1,2,3,4,19,19,19,8,9,10,11,12)\n#else\t/* For OTHER OSs(Linux?) */\n    #define ALTI_SL1\t{SL1, SL1, SL1, SL1}\n    #define ALTI_SR1\t{SR1, SR1, SR1, SR1}\n    #define ALTI_MSK\t{MSK1, MSK2, MSK3, MSK4}\n    #define ALTI_MSK64\t{MSK2, MSK1, MSK4, MSK3}\n    #define ALTI_SL2_PERM\t{3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10}\n    #define ALTI_SL2_PERM64\t{3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2}\n    #define ALTI_SR2_PERM\t{5,6,7,0,9,10,11,4,13,14,15,8,19,19,19,12}\n    #define ALTI_SR2_PERM64\t{13,14,15,0,1,2,3,4,19,19,19,8,9,10,11,12}\n#endif\t/* For OSX */\n#define IDSTR\t\"SFMT-44497:330-5-3-9-3:effffffb-dfbebfff-bfbf7bef-9ffd7bff\"\n\n#endif /* SFMT_PARAMS44497_H */\n"
  },
  {
    "path": "deps/jemalloc/test/include/test/SFMT-params607.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#ifndef SFMT_PARAMS607_H\n#define SFMT_PARAMS607_H\n\n#define POS1\t2\n#define SL1\t15\n#define SL2\t3\n#define SR1\t13\n#define SR2\t3\n#define MSK1\t0xfdff37ffU\n#define MSK2\t0xef7f3f7dU\n#define MSK3\t0xff777b7dU\n#define MSK4\t0x7ff7fb2fU\n#define PARITY1\t0x00000001U\n#define PARITY2\t0x00000000U\n#define PARITY3\t0x00000000U\n#define PARITY4\t0x5986f054U\n\n\n/* PARAMETERS FOR ALTIVEC */\n#if defined(__APPLE__)\t/* For OSX */\n    #define ALTI_SL1\t(vector unsigned int)(SL1, SL1, SL1, SL1)\n    #define ALTI_SR1\t(vector unsigned int)(SR1, SR1, SR1, SR1)\n    #define ALTI_MSK\t(vector unsigned int)(MSK1, MSK2, MSK3, MSK4)\n    #define ALTI_MSK64 \\\n\t(vector unsigned int)(MSK2, MSK1, MSK4, MSK3)\n    #define ALTI_SL2_PERM \\\n\t(vector unsigned char)(3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10)\n    #define ALTI_SL2_PERM64 \\\n\t(vector unsigned char)(3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2)\n    #define ALTI_SR2_PERM \\\n\t(vector unsigned char)(5,6,7,0,9,10,11,4,13,14,15,8,19,19,19,12)\n    #define ALTI_SR2_PERM64 \\\n\t(vector unsigned char)(13,14,15,0,1,2,3,4,19,19,19,8,9,10,11,12)\n#else\t/* For OTHER OSs(Linux?) */\n    #define ALTI_SL1\t{SL1, SL1, SL1, SL1}\n    #define ALTI_SR1\t{SR1, SR1, SR1, SR1}\n    #define ALTI_MSK\t{MSK1, MSK2, MSK3, MSK4}\n    #define ALTI_MSK64\t{MSK2, MSK1, MSK4, MSK3}\n    #define ALTI_SL2_PERM\t{3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10}\n    #define ALTI_SL2_PERM64\t{3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2}\n    #define ALTI_SR2_PERM\t{5,6,7,0,9,10,11,4,13,14,15,8,19,19,19,12}\n    #define ALTI_SR2_PERM64\t{13,14,15,0,1,2,3,4,19,19,19,8,9,10,11,12}\n#endif\t/* For OSX */\n#define IDSTR\t\"SFMT-607:2-15-3-13-3:fdff37ff-ef7f3f7d-ff777b7d-7ff7fb2f\"\n\n#endif /* SFMT_PARAMS607_H */\n"
  },
  {
    "path": "deps/jemalloc/test/include/test/SFMT-params86243.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#ifndef SFMT_PARAMS86243_H\n#define SFMT_PARAMS86243_H\n\n#define POS1\t366\n#define SL1\t6\n#define SL2\t7\n#define SR1\t19\n#define SR2\t1\n#define MSK1\t0xfdbffbffU\n#define MSK2\t0xbff7ff3fU\n#define MSK3\t0xfd77efffU\n#define MSK4\t0xbf9ff3ffU\n#define PARITY1\t0x00000001U\n#define PARITY2\t0x00000000U\n#define PARITY3\t0x00000000U\n#define PARITY4\t0xe9528d85U\n\n\n/* PARAMETERS FOR ALTIVEC */\n#if defined(__APPLE__)\t/* For OSX */\n    #define ALTI_SL1\t(vector unsigned int)(SL1, SL1, SL1, SL1)\n    #define ALTI_SR1\t(vector unsigned int)(SR1, SR1, SR1, SR1)\n    #define ALTI_MSK\t(vector unsigned int)(MSK1, MSK2, MSK3, MSK4)\n    #define ALTI_MSK64 \\\n\t(vector unsigned int)(MSK2, MSK1, MSK4, MSK3)\n    #define ALTI_SL2_PERM \\\n\t(vector unsigned char)(25,25,25,25,3,25,25,25,7,0,1,2,11,4,5,6)\n    #define ALTI_SL2_PERM64 \\\n\t(vector unsigned char)(7,25,25,25,25,25,25,25,15,0,1,2,3,4,5,6)\n    #define ALTI_SR2_PERM \\\n\t(vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14)\n    #define ALTI_SR2_PERM64 \\\n\t(vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14)\n#else\t/* For OTHER OSs(Linux?) */\n    #define ALTI_SL1\t{SL1, SL1, SL1, SL1}\n    #define ALTI_SR1\t{SR1, SR1, SR1, SR1}\n    #define ALTI_MSK\t{MSK1, MSK2, MSK3, MSK4}\n    #define ALTI_MSK64\t{MSK2, MSK1, MSK4, MSK3}\n    #define ALTI_SL2_PERM\t{25,25,25,25,3,25,25,25,7,0,1,2,11,4,5,6}\n    #define ALTI_SL2_PERM64\t{7,25,25,25,25,25,25,25,15,0,1,2,3,4,5,6}\n    #define ALTI_SR2_PERM\t{7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14}\n    #define ALTI_SR2_PERM64\t{15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14}\n#endif\t/* For OSX */\n#define IDSTR\t\"SFMT-86243:366-6-7-19-1:fdbffbff-bff7ff3f-fd77efff-bf9ff3ff\"\n\n#endif /* SFMT_PARAMS86243_H */\n"
  },
  {
    "path": "deps/jemalloc/test/include/test/SFMT-sse2.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n/**\n * @file  SFMT-sse2.h\n * @brief SIMD oriented Fast Mersenne Twister(SFMT) for Intel SSE2\n *\n * @author Mutsuo Saito (Hiroshima University)\n * @author Makoto Matsumoto (Hiroshima University)\n *\n * @note We assume LITTLE ENDIAN in this file\n *\n * Copyright (C) 2006, 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n * University. All rights reserved.\n *\n * The new BSD License is applied to this software, see LICENSE.txt\n */\n\n#ifndef SFMT_SSE2_H\n#define SFMT_SSE2_H\n\n/**\n * This function represents the recursion formula.\n * @param a a 128-bit part of the interal state array\n * @param b a 128-bit part of the interal state array\n * @param c a 128-bit part of the interal state array\n * @param d a 128-bit part of the interal state array\n * @param mask 128-bit mask\n * @return output\n */\nJEMALLOC_ALWAYS_INLINE __m128i mm_recursion(__m128i *a, __m128i *b,\n\t\t\t\t   __m128i c, __m128i d, __m128i mask) {\n    __m128i v, x, y, z;\n\n    x = _mm_load_si128(a);\n    y = _mm_srli_epi32(*b, SR1);\n    z = _mm_srli_si128(c, SR2);\n    v = _mm_slli_epi32(d, SL1);\n    z = _mm_xor_si128(z, x);\n    z = _mm_xor_si128(z, v);\n    x = _mm_slli_si128(x, SL2);\n    y = _mm_and_si128(y, mask);\n    z = _mm_xor_si128(z, x);\n    z = _mm_xor_si128(z, y);\n    return z;\n}\n\n/**\n * This function fills the internal state array with pseudorandom\n * integers.\n */\nstatic inline void gen_rand_all(sfmt_t *ctx) {\n    int i;\n    __m128i r, r1, r2, mask;\n    mask = _mm_set_epi32(MSK4, MSK3, MSK2, MSK1);\n\n    r1 = _mm_load_si128(&ctx->sfmt[N - 2].si);\n    r2 = _mm_load_si128(&ctx->sfmt[N - 1].si);\n    for (i = 0; i < N - POS1; i++) {\n\tr = mm_recursion(&ctx->sfmt[i].si, &ctx->sfmt[i + POS1].si, r1, r2,\n\t  mask);\n\t_mm_store_si128(&ctx->sfmt[i].si, r);\n\tr1 = r2;\n\tr2 = r;\n    }\n    for (; i < N; i++) {\n\tr = mm_recursion(&ctx->sfmt[i].si, &ctx->sfmt[i + POS1 - N].si, r1, r2,\n\t  mask);\n\t_mm_store_si128(&ctx->sfmt[i].si, r);\n\tr1 = r2;\n\tr2 = r;\n    }\n}\n\n/**\n * This function fills the user-specified array with pseudorandom\n * integers.\n *\n * @param array an 128-bit array to be filled by pseudorandom numbers.\n * @param size number of 128-bit pesudorandom numbers to be generated.\n */\nstatic inline void gen_rand_array(sfmt_t *ctx, w128_t *array, int size) {\n    int i, j;\n    __m128i r, r1, r2, mask;\n    mask = _mm_set_epi32(MSK4, MSK3, MSK2, MSK1);\n\n    r1 = _mm_load_si128(&ctx->sfmt[N - 2].si);\n    r2 = _mm_load_si128(&ctx->sfmt[N - 1].si);\n    for (i = 0; i < N - POS1; i++) {\n\tr = mm_recursion(&ctx->sfmt[i].si, &ctx->sfmt[i + POS1].si, r1, r2,\n\t  mask);\n\t_mm_store_si128(&array[i].si, r);\n\tr1 = r2;\n\tr2 = r;\n    }\n    for (; i < N; i++) {\n\tr = mm_recursion(&ctx->sfmt[i].si, &array[i + POS1 - N].si, r1, r2,\n\t  mask);\n\t_mm_store_si128(&array[i].si, r);\n\tr1 = r2;\n\tr2 = r;\n    }\n    /* main loop */\n    for (; i < size - N; i++) {\n\tr = mm_recursion(&array[i - N].si, &array[i + POS1 - N].si, r1, r2,\n\t\t\t mask);\n\t_mm_store_si128(&array[i].si, r);\n\tr1 = r2;\n\tr2 = r;\n    }\n    for (j = 0; j < 2 * N - size; j++) {\n\tr = _mm_load_si128(&array[j + size - N].si);\n\t_mm_store_si128(&ctx->sfmt[j].si, r);\n    }\n    for (; i < size; i++) {\n\tr = mm_recursion(&array[i - N].si, &array[i + POS1 - N].si, r1, r2,\n\t\t\t mask);\n\t_mm_store_si128(&array[i].si, r);\n\t_mm_store_si128(&ctx->sfmt[j++].si, r);\n\tr1 = r2;\n\tr2 = r;\n    }\n}\n\n#endif\n"
  },
  {
    "path": "deps/jemalloc/test/include/test/SFMT.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n/** \n * @file SFMT.h \n *\n * @brief SIMD oriented Fast Mersenne Twister(SFMT) pseudorandom\n * number generator\n *\n * @author Mutsuo Saito (Hiroshima University)\n * @author Makoto Matsumoto (Hiroshima University)\n *\n * Copyright (C) 2006, 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n * University. All rights reserved.\n *\n * The new BSD License is applied to this software.\n * see LICENSE.txt\n *\n * @note We assume that your system has inttypes.h.  If your system\n * doesn't have inttypes.h, you have to typedef uint32_t and uint64_t,\n * and you have to define PRIu64 and PRIx64 in this file as follows:\n * @verbatim\n typedef unsigned int uint32_t\n typedef unsigned long long uint64_t  \n #define PRIu64 \"llu\"\n #define PRIx64 \"llx\"\n@endverbatim\n * uint32_t must be exactly 32-bit unsigned integer type (no more, no\n * less), and uint64_t must be exactly 64-bit unsigned integer type.\n * PRIu64 and PRIx64 are used for printf function to print 64-bit\n * unsigned int and 64-bit unsigned int in hexadecimal format.\n */\n\n#ifndef SFMT_H\n#define SFMT_H\n\ntypedef struct sfmt_s sfmt_t;\n\nuint32_t gen_rand32(sfmt_t *ctx);\nuint32_t gen_rand32_range(sfmt_t *ctx, uint32_t limit);\nuint64_t gen_rand64(sfmt_t *ctx);\nuint64_t gen_rand64_range(sfmt_t *ctx, uint64_t limit);\nvoid fill_array32(sfmt_t *ctx, uint32_t *array, int size);\nvoid fill_array64(sfmt_t *ctx, uint64_t *array, int size);\nsfmt_t *init_gen_rand(uint32_t seed);\nsfmt_t *init_by_array(uint32_t *init_key, int key_length);\nvoid fini_gen_rand(sfmt_t *ctx);\nconst char *get_idstring(void);\nint get_min_array_size32(void);\nint get_min_array_size64(void);\n\n/* These real versions are due to Isaku Wada */\n/** generates a random number on [0,1]-real-interval */\nstatic inline double to_real1(uint32_t v) {\n    return v * (1.0/4294967295.0); \n    /* divided by 2^32-1 */ \n}\n\n/** generates a random number on [0,1]-real-interval */\nstatic inline double genrand_real1(sfmt_t *ctx) {\n    return to_real1(gen_rand32(ctx));\n}\n\n/** generates a random number on [0,1)-real-interval */\nstatic inline double to_real2(uint32_t v) {\n    return v * (1.0/4294967296.0); \n    /* divided by 2^32 */\n}\n\n/** generates a random number on [0,1)-real-interval */\nstatic inline double genrand_real2(sfmt_t *ctx) {\n    return to_real2(gen_rand32(ctx));\n}\n\n/** generates a random number on (0,1)-real-interval */\nstatic inline double to_real3(uint32_t v) {\n    return (((double)v) + 0.5)*(1.0/4294967296.0); \n    /* divided by 2^32 */\n}\n\n/** generates a random number on (0,1)-real-interval */\nstatic inline double genrand_real3(sfmt_t *ctx) {\n    return to_real3(gen_rand32(ctx));\n}\n/** These real versions are due to Isaku Wada */\n\n/** generates a random number on [0,1) with 53-bit resolution*/\nstatic inline double to_res53(uint64_t v) {\n    return v * (1.0/18446744073709551616.0L);\n}\n\n/** generates a random number on [0,1) with 53-bit resolution from two\n * 32 bit integers */\nstatic inline double to_res53_mix(uint32_t x, uint32_t y) {\n    return to_res53(x | ((uint64_t)y << 32));\n}\n\n/** generates a random number on [0,1) with 53-bit resolution\n */\nstatic inline double genrand_res53(sfmt_t *ctx) {\n    return to_res53(gen_rand64(ctx));\n}\n\n/** generates a random number on [0,1) with 53-bit resolution\n    using 32bit integer.\n */\nstatic inline double genrand_res53_mix(sfmt_t *ctx) {\n    uint32_t x, y;\n\n    x = gen_rand32(ctx);\n    y = gen_rand32(ctx);\n    return to_res53_mix(x, y);\n}\n#endif\n"
  },
  {
    "path": "deps/jemalloc/test/include/test/btalloc.h",
    "content": "/* btalloc() provides a mechanism for allocating via permuted backtraces. */\nvoid\t*btalloc(size_t size, unsigned bits);\n\n#define btalloc_n_proto(n)\t\t\t\t\t\t\\\nvoid\t*btalloc_##n(size_t size, unsigned bits);\nbtalloc_n_proto(0)\nbtalloc_n_proto(1)\n\n#define btalloc_n_gen(n)\t\t\t\t\t\t\\\nvoid *\t\t\t\t\t\t\t\t\t\\\nbtalloc_##n(size_t size, unsigned bits) {\t\t\t\t\\\n\tvoid *p;\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tif (bits == 0) {\t\t\t\t\t\t\\\n\t\tp = mallocx(size, 0);\t\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t\tswitch (bits & 0x1U) {\t\t\t\t\t\\\n\t\tcase 0:\t\t\t\t\t\t\t\\\n\t\t\tp = (btalloc_0(size, bits >> 1));\t\t\\\n\t\t\tbreak;\t\t\t\t\t\t\\\n\t\tcase 1:\t\t\t\t\t\t\t\\\n\t\t\tp = (btalloc_1(size, bits >> 1));\t\t\\\n\t\t\tbreak;\t\t\t\t\t\t\\\n\t\tdefault: not_reached();\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\t/* Intentionally sabotage tail call optimization. */\t\t\\\n\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\t\t\\\n\treturn p;\t\t\t\t\t\t\t\\\n}\n"
  },
  {
    "path": "deps/jemalloc/test/include/test/extent_hooks.h",
    "content": "/*\n * Boilerplate code used for testing extent hooks via interception and\n * passthrough.\n */\n\nstatic void\t*extent_alloc_hook(extent_hooks_t *extent_hooks, void *new_addr,\n    size_t size, size_t alignment, bool *zero, bool *commit,\n    unsigned arena_ind);\nstatic bool\textent_dalloc_hook(extent_hooks_t *extent_hooks, void *addr,\n    size_t size, bool committed, unsigned arena_ind);\nstatic void\textent_destroy_hook(extent_hooks_t *extent_hooks, void *addr,\n    size_t size, bool committed, unsigned arena_ind);\nstatic bool\textent_commit_hook(extent_hooks_t *extent_hooks, void *addr,\n    size_t size, size_t offset, size_t length, unsigned arena_ind);\nstatic bool\textent_decommit_hook(extent_hooks_t *extent_hooks, void *addr,\n    size_t size, size_t offset, size_t length, unsigned arena_ind);\nstatic bool\textent_purge_lazy_hook(extent_hooks_t *extent_hooks, void *addr,\n    size_t size, size_t offset, size_t length, unsigned arena_ind);\nstatic bool\textent_purge_forced_hook(extent_hooks_t *extent_hooks,\n    void *addr, size_t size, size_t offset, size_t length, unsigned arena_ind);\nstatic bool\textent_split_hook(extent_hooks_t *extent_hooks, void *addr,\n    size_t size, size_t size_a, size_t size_b, bool committed,\n    unsigned arena_ind);\nstatic bool\textent_merge_hook(extent_hooks_t *extent_hooks, void *addr_a,\n    size_t size_a, void *addr_b, size_t size_b, bool committed,\n    unsigned arena_ind);\n\nstatic extent_hooks_t *default_hooks;\nstatic extent_hooks_t hooks = {\n\textent_alloc_hook,\n\textent_dalloc_hook,\n\textent_destroy_hook,\n\textent_commit_hook,\n\textent_decommit_hook,\n\textent_purge_lazy_hook,\n\textent_purge_forced_hook,\n\textent_split_hook,\n\textent_merge_hook\n};\n\n/* Control whether hook functions pass calls through to default hooks. */\nstatic bool try_alloc = true;\nstatic bool try_dalloc = true;\nstatic bool try_destroy = true;\nstatic bool try_commit = true;\nstatic bool try_decommit = true;\nstatic bool try_purge_lazy = true;\nstatic bool try_purge_forced = true;\nstatic bool try_split = true;\nstatic bool try_merge = true;\n\n/* Set to false prior to operations, then introspect after operations. */\nstatic bool called_alloc;\nstatic bool called_dalloc;\nstatic bool called_destroy;\nstatic bool called_commit;\nstatic bool called_decommit;\nstatic bool called_purge_lazy;\nstatic bool called_purge_forced;\nstatic bool called_split;\nstatic bool called_merge;\n\n/* Set to false prior to operations, then introspect after operations. */\nstatic bool did_alloc;\nstatic bool did_dalloc;\nstatic bool did_destroy;\nstatic bool did_commit;\nstatic bool did_decommit;\nstatic bool did_purge_lazy;\nstatic bool did_purge_forced;\nstatic bool did_split;\nstatic bool did_merge;\n\n#if 0\n#  define TRACE_HOOK(fmt, ...) malloc_printf(fmt, __VA_ARGS__)\n#else\n#  define TRACE_HOOK(fmt, ...)\n#endif\n\nstatic void *\nextent_alloc_hook(extent_hooks_t *extent_hooks, void *new_addr, size_t size,\n    size_t alignment, bool *zero, bool *commit, unsigned arena_ind) {\n\tvoid *ret;\n\n\tTRACE_HOOK(\"%s(extent_hooks=%p, new_addr=%p, size=%zu, alignment=%zu, \"\n\t    \"*zero=%s, *commit=%s, arena_ind=%u)\\n\", __func__, extent_hooks,\n\t    new_addr, size, alignment, *zero ?  \"true\" : \"false\", *commit ?\n\t    \"true\" : \"false\", arena_ind);\n\tassert_ptr_eq(extent_hooks, &hooks,\n\t    \"extent_hooks should be same as pointer used to set hooks\");\n\tassert_ptr_eq(extent_hooks->alloc, extent_alloc_hook,\n\t    \"Wrong hook function\");\n\tcalled_alloc = true;\n\tif (!try_alloc) {\n\t\treturn NULL;\n\t}\n\tret = default_hooks->alloc(default_hooks, new_addr, size, alignment,\n\t    zero, commit, 0);\n\tdid_alloc = (ret != NULL);\n\treturn ret;\n}\n\nstatic bool\nextent_dalloc_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    bool committed, unsigned arena_ind) {\n\tbool err;\n\n\tTRACE_HOOK(\"%s(extent_hooks=%p, addr=%p, size=%zu, committed=%s, \"\n\t    \"arena_ind=%u)\\n\", __func__, extent_hooks, addr, size, committed ?\n\t    \"true\" : \"false\", arena_ind);\n\tassert_ptr_eq(extent_hooks, &hooks,\n\t    \"extent_hooks should be same as pointer used to set hooks\");\n\tassert_ptr_eq(extent_hooks->dalloc, extent_dalloc_hook,\n\t    \"Wrong hook function\");\n\tcalled_dalloc = true;\n\tif (!try_dalloc) {\n\t\treturn true;\n\t}\n\terr = default_hooks->dalloc(default_hooks, addr, size, committed, 0);\n\tdid_dalloc = !err;\n\treturn err;\n}\n\nstatic void\nextent_destroy_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    bool committed, unsigned arena_ind) {\n\tTRACE_HOOK(\"%s(extent_hooks=%p, addr=%p, size=%zu, committed=%s, \"\n\t    \"arena_ind=%u)\\n\", __func__, extent_hooks, addr, size, committed ?\n\t    \"true\" : \"false\", arena_ind);\n\tassert_ptr_eq(extent_hooks, &hooks,\n\t    \"extent_hooks should be same as pointer used to set hooks\");\n\tassert_ptr_eq(extent_hooks->destroy, extent_destroy_hook,\n\t    \"Wrong hook function\");\n\tcalled_destroy = true;\n\tif (!try_destroy) {\n\t\treturn;\n\t}\n\tdefault_hooks->destroy(default_hooks, addr, size, committed, 0);\n\tdid_destroy = true;\n}\n\nstatic bool\nextent_commit_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    size_t offset, size_t length, unsigned arena_ind) {\n\tbool err;\n\n\tTRACE_HOOK(\"%s(extent_hooks=%p, addr=%p, size=%zu, offset=%zu, \"\n\t    \"length=%zu, arena_ind=%u)\\n\", __func__, extent_hooks, addr, size,\n\t    offset, length, arena_ind);\n\tassert_ptr_eq(extent_hooks, &hooks,\n\t    \"extent_hooks should be same as pointer used to set hooks\");\n\tassert_ptr_eq(extent_hooks->commit, extent_commit_hook,\n\t    \"Wrong hook function\");\n\tcalled_commit = true;\n\tif (!try_commit) {\n\t\treturn true;\n\t}\n\terr = default_hooks->commit(default_hooks, addr, size, offset, length,\n\t    0);\n\tdid_commit = !err;\n\treturn err;\n}\n\nstatic bool\nextent_decommit_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    size_t offset, size_t length, unsigned arena_ind) {\n\tbool err;\n\n\tTRACE_HOOK(\"%s(extent_hooks=%p, addr=%p, size=%zu, offset=%zu, \"\n\t    \"length=%zu, arena_ind=%u)\\n\", __func__, extent_hooks, addr, size,\n\t    offset, length, arena_ind);\n\tassert_ptr_eq(extent_hooks, &hooks,\n\t    \"extent_hooks should be same as pointer used to set hooks\");\n\tassert_ptr_eq(extent_hooks->decommit, extent_decommit_hook,\n\t    \"Wrong hook function\");\n\tcalled_decommit = true;\n\tif (!try_decommit) {\n\t\treturn true;\n\t}\n\terr = default_hooks->decommit(default_hooks, addr, size, offset, length,\n\t    0);\n\tdid_decommit = !err;\n\treturn err;\n}\n\nstatic bool\nextent_purge_lazy_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    size_t offset, size_t length, unsigned arena_ind) {\n\tbool err;\n\n\tTRACE_HOOK(\"%s(extent_hooks=%p, addr=%p, size=%zu, offset=%zu, \"\n\t    \"length=%zu arena_ind=%u)\\n\", __func__, extent_hooks, addr, size,\n\t    offset, length, arena_ind);\n\tassert_ptr_eq(extent_hooks, &hooks,\n\t    \"extent_hooks should be same as pointer used to set hooks\");\n\tassert_ptr_eq(extent_hooks->purge_lazy, extent_purge_lazy_hook,\n\t    \"Wrong hook function\");\n\tcalled_purge_lazy = true;\n\tif (!try_purge_lazy) {\n\t\treturn true;\n\t}\n\terr = default_hooks->purge_lazy == NULL ||\n\t    default_hooks->purge_lazy(default_hooks, addr, size, offset, length,\n\t    0);\n\tdid_purge_lazy = !err;\n\treturn err;\n}\n\nstatic bool\nextent_purge_forced_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    size_t offset, size_t length, unsigned arena_ind) {\n\tbool err;\n\n\tTRACE_HOOK(\"%s(extent_hooks=%p, addr=%p, size=%zu, offset=%zu, \"\n\t    \"length=%zu arena_ind=%u)\\n\", __func__, extent_hooks, addr, size,\n\t    offset, length, arena_ind);\n\tassert_ptr_eq(extent_hooks, &hooks,\n\t    \"extent_hooks should be same as pointer used to set hooks\");\n\tassert_ptr_eq(extent_hooks->purge_forced, extent_purge_forced_hook,\n\t    \"Wrong hook function\");\n\tcalled_purge_forced = true;\n\tif (!try_purge_forced) {\n\t\treturn true;\n\t}\n\terr = default_hooks->purge_forced == NULL ||\n\t    default_hooks->purge_forced(default_hooks, addr, size, offset,\n\t    length, 0);\n\tdid_purge_forced = !err;\n\treturn err;\n}\n\nstatic bool\nextent_split_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    size_t size_a, size_t size_b, bool committed, unsigned arena_ind) {\n\tbool err;\n\n\tTRACE_HOOK(\"%s(extent_hooks=%p, addr=%p, size=%zu, size_a=%zu, \"\n\t    \"size_b=%zu, committed=%s, arena_ind=%u)\\n\", __func__, extent_hooks,\n\t    addr, size, size_a, size_b, committed ? \"true\" : \"false\",\n\t    arena_ind);\n\tassert_ptr_eq(extent_hooks, &hooks,\n\t    \"extent_hooks should be same as pointer used to set hooks\");\n\tassert_ptr_eq(extent_hooks->split, extent_split_hook,\n\t    \"Wrong hook function\");\n\tcalled_split = true;\n\tif (!try_split) {\n\t\treturn true;\n\t}\n\terr = (default_hooks->split == NULL ||\n\t    default_hooks->split(default_hooks, addr, size, size_a, size_b,\n\t    committed, 0));\n\tdid_split = !err;\n\treturn err;\n}\n\nstatic bool\nextent_merge_hook(extent_hooks_t *extent_hooks, void *addr_a, size_t size_a,\n    void *addr_b, size_t size_b, bool committed, unsigned arena_ind) {\n\tbool err;\n\n\tTRACE_HOOK(\"%s(extent_hooks=%p, addr_a=%p, size_a=%zu, addr_b=%p \"\n\t    \"size_b=%zu, committed=%s, arena_ind=%u)\\n\", __func__, extent_hooks,\n\t    addr_a, size_a, addr_b, size_b, committed ? \"true\" : \"false\",\n\t    arena_ind);\n\tassert_ptr_eq(extent_hooks, &hooks,\n\t    \"extent_hooks should be same as pointer used to set hooks\");\n\tassert_ptr_eq(extent_hooks->merge, extent_merge_hook,\n\t    \"Wrong hook function\");\n\tassert_ptr_eq((void *)((uintptr_t)addr_a + size_a), addr_b,\n\t    \"Extents not mergeable\");\n\tcalled_merge = true;\n\tif (!try_merge) {\n\t\treturn true;\n\t}\n\terr = (default_hooks->merge == NULL ||\n\t    default_hooks->merge(default_hooks, addr_a, size_a, addr_b, size_b,\n\t    committed, 0));\n\tdid_merge = !err;\n\treturn err;\n}\n\nstatic void\nextent_hooks_prep(void) {\n\tsize_t sz;\n\n\tsz = sizeof(default_hooks);\n\tassert_d_eq(mallctl(\"arena.0.extent_hooks\", (void *)&default_hooks, &sz,\n\t    NULL, 0), 0, \"Unexpected mallctl() error\");\n}\n"
  },
  {
    "path": "deps/jemalloc/test/include/test/jemalloc_test.h.in",
    "content": "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#include <limits.h>\n#ifndef SIZE_T_MAX\n#  define SIZE_T_MAX\tSIZE_MAX\n#endif\n#include <stdlib.h>\n#include <stdarg.h>\n#include <stdbool.h>\n#include <errno.h>\n#include <math.h>\n#include <string.h>\n#ifdef _WIN32\n#  include \"msvc_compat/strings.h\"\n#endif\n\n#ifdef _WIN32\n#  include <windows.h>\n#  include \"msvc_compat/windows_extra.h\"\n#else\n#  include <pthread.h>\n#endif\n\n#include \"test/jemalloc_test_defs.h\"\n\n#if defined(JEMALLOC_OSATOMIC)\n#  include <libkern/OSAtomic.h>\n#endif\n\n#if defined(HAVE_ALTIVEC) && !defined(__APPLE__)\n#  include <altivec.h>\n#endif\n#ifdef HAVE_SSE2\n#  include <emmintrin.h>\n#endif\n\n/******************************************************************************/\n/*\n * For unit tests, expose all public and private interfaces.\n */\n#ifdef JEMALLOC_UNIT_TEST\n#  define JEMALLOC_JET\n#  define JEMALLOC_MANGLE\n#  include \"jemalloc/internal/jemalloc_preamble.h\"\n#  include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n/******************************************************************************/\n/*\n * For integration tests, expose the public jemalloc interfaces, but only\n * expose the minimum necessary internal utility code (to avoid re-implementing\n * essentially identical code within the test infrastructure).\n */\n#elif defined(JEMALLOC_INTEGRATION_TEST) || \\\n    defined(JEMALLOC_INTEGRATION_CPP_TEST)\n#  define JEMALLOC_MANGLE\n#  include \"jemalloc/jemalloc@install_suffix@.h\"\n#  include \"jemalloc/internal/jemalloc_internal_defs.h\"\n#  include \"jemalloc/internal/jemalloc_internal_macros.h\"\n\nstatic const bool config_debug =\n#ifdef JEMALLOC_DEBUG\n    true\n#else\n    false\n#endif\n    ;\n\n#  define JEMALLOC_N(n) @private_namespace@##n\n#  include \"jemalloc/internal/private_namespace.h\"\n#  include \"jemalloc/internal/test_hooks.h\"\n\n/* Hermetic headers. */\n#  include \"jemalloc/internal/assert.h\"\n#  include \"jemalloc/internal/malloc_io.h\"\n#  include \"jemalloc/internal/nstime.h\"\n#  include \"jemalloc/internal/util.h\"\n\n/* Non-hermetic headers. */\n#  include \"jemalloc/internal/qr.h\"\n#  include \"jemalloc/internal/ql.h\"\n\n/******************************************************************************/\n/*\n * For stress tests, expose the public jemalloc interfaces with name mangling\n * so that they can be tested as e.g. malloc() and free().  Also expose the\n * public jemalloc interfaces with jet_ prefixes, so that stress tests can use\n * a separate allocator for their internal data structures.\n */\n#elif defined(JEMALLOC_STRESS_TEST)\n#  include \"jemalloc/jemalloc@install_suffix@.h\"\n\n#  include \"jemalloc/jemalloc_protos_jet.h\"\n\n#  define JEMALLOC_JET\n#  include \"jemalloc/internal/jemalloc_preamble.h\"\n#  include \"jemalloc/internal/jemalloc_internal_includes.h\"\n#  include \"jemalloc/internal/public_unnamespace.h\"\n#  undef JEMALLOC_JET\n\n#  include \"jemalloc/jemalloc_rename.h\"\n#  define JEMALLOC_MANGLE\n#  ifdef JEMALLOC_STRESS_TESTLIB\n#    include \"jemalloc/jemalloc_mangle_jet.h\"\n#  else\n#    include \"jemalloc/jemalloc_mangle.h\"\n#  endif\n\n/******************************************************************************/\n/*\n * This header does dangerous things, the effects of which only test code\n * should be subject to.\n */\n#else\n#  error \"This header cannot be included outside a testing context\"\n#endif\n\n/******************************************************************************/\n/*\n * Common test utilities.\n */\n#include \"test/btalloc.h\"\n#include \"test/math.h\"\n#include \"test/mtx.h\"\n#include \"test/mq.h\"\n#include \"test/test.h\"\n#include \"test/timer.h\"\n#include \"test/thd.h\"\n#define MEXP 19937\n#include \"test/SFMT.h\"\n\n/******************************************************************************/\n/*\n * Define always-enabled assertion macros, so that test assertions execute even\n * if assertions are disabled in the library code.\n */\n#undef assert\n#undef not_reached\n#undef not_implemented\n#undef assert_not_implemented\n\n#define assert(e) do {\t\t\t\t\t\t\t\\\n\tif (!(e)) {\t\t\t\t\t\t\t\\\n\t\tmalloc_printf(\t\t\t\t\t\t\\\n\t\t    \"<jemalloc>: %s:%d: Failed assertion: \\\"%s\\\"\\n\",\t\\\n\t\t    __FILE__, __LINE__, #e);\t\t\t\t\\\n\t\tabort();\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define not_reached() do {\t\t\t\t\t\t\\\n\tmalloc_printf(\t\t\t\t\t\t\t\\\n\t    \"<jemalloc>: %s:%d: Unreachable code reached\\n\",\t\t\\\n\t    __FILE__, __LINE__);\t\t\t\t\t\\\n\tabort();\t\t\t\t\t\t\t\\\n} while (0)\n\n#define not_implemented() do {\t\t\t\t\t\t\\\n\tmalloc_printf(\"<jemalloc>: %s:%d: Not implemented\\n\",\t\t\\\n\t    __FILE__, __LINE__);\t\t\t\t\t\\\n\tabort();\t\t\t\t\t\t\t\\\n} while (0)\n\n#define assert_not_implemented(e) do {\t\t\t\t\t\\\n\tif (!(e)) {\t\t\t\t\t\t\t\\\n\t\tnot_implemented();\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#ifdef __cplusplus\n}\n#endif\n"
  },
  {
    "path": "deps/jemalloc/test/include/test/jemalloc_test_defs.h.in",
    "content": "#include \"jemalloc/internal/jemalloc_internal_defs.h\"\n#include \"jemalloc/internal/jemalloc_internal_decls.h\"\n\n/*\n * For use by SFMT.  configure.ac doesn't actually define HAVE_SSE2 because its\n * dependencies are notoriously unportable in practice.\n */\n#undef HAVE_SSE2\n#undef HAVE_ALTIVEC\n"
  },
  {
    "path": "deps/jemalloc/test/include/test/math.h",
    "content": "/*\n * Compute the natural log of Gamma(x), accurate to 10 decimal places.\n *\n * This implementation is based on:\n *\n *   Pike, M.C., I.D. Hill (1966) Algorithm 291: Logarithm of Gamma function\n *   [S14].  Communications of the ACM 9(9):684.\n */\nstatic inline double\nln_gamma(double x) {\n\tdouble f, z;\n\n\tassert(x > 0.0);\n\n\tif (x < 7.0) {\n\t\tf = 1.0;\n\t\tz = x;\n\t\twhile (z < 7.0) {\n\t\t\tf *= z;\n\t\t\tz += 1.0;\n\t\t}\n\t\tx = z;\n\t\tf = -log(f);\n\t} else {\n\t\tf = 0.0;\n\t}\n\n\tz = 1.0 / (x * x);\n\n\treturn f + (x-0.5) * log(x) - x + 0.918938533204673 +\n\t    (((-0.000595238095238 * z + 0.000793650793651) * z -\n\t    0.002777777777778) * z + 0.083333333333333) / x;\n}\n\n/*\n * Compute the incomplete Gamma ratio for [0..x], where p is the shape\n * parameter, and ln_gamma_p is ln_gamma(p).\n *\n * This implementation is based on:\n *\n *   Bhattacharjee, G.P. (1970) Algorithm AS 32: The incomplete Gamma integral.\n *   Applied Statistics 19:285-287.\n */\nstatic inline double\ni_gamma(double x, double p, double ln_gamma_p) {\n\tdouble acu, factor, oflo, gin, term, rn, a, b, an, dif;\n\tdouble pn[6];\n\tunsigned i;\n\n\tassert(p > 0.0);\n\tassert(x >= 0.0);\n\n\tif (x == 0.0) {\n\t\treturn 0.0;\n\t}\n\n\tacu = 1.0e-10;\n\toflo = 1.0e30;\n\tgin = 0.0;\n\tfactor = exp(p * log(x) - x - ln_gamma_p);\n\n\tif (x <= 1.0 || x < p) {\n\t\t/* Calculation by series expansion. */\n\t\tgin = 1.0;\n\t\tterm = 1.0;\n\t\trn = p;\n\n\t\twhile (true) {\n\t\t\trn += 1.0;\n\t\t\tterm *= x / rn;\n\t\t\tgin += term;\n\t\t\tif (term <= acu) {\n\t\t\t\tgin *= factor / p;\n\t\t\t\treturn gin;\n\t\t\t}\n\t\t}\n\t} else {\n\t\t/* Calculation by continued fraction. */\n\t\ta = 1.0 - p;\n\t\tb = a + x + 1.0;\n\t\tterm = 0.0;\n\t\tpn[0] = 1.0;\n\t\tpn[1] = x;\n\t\tpn[2] = x + 1.0;\n\t\tpn[3] = x * b;\n\t\tgin = pn[2] / pn[3];\n\n\t\twhile (true) {\n\t\t\ta += 1.0;\n\t\t\tb += 2.0;\n\t\t\tterm += 1.0;\n\t\t\tan = a * term;\n\t\t\tfor (i = 0; i < 2; i++) {\n\t\t\t\tpn[i+4] = b * pn[i+2] - an * pn[i];\n\t\t\t}\n\t\t\tif (pn[5] != 0.0) {\n\t\t\t\trn = pn[4] / pn[5];\n\t\t\t\tdif = fabs(gin - rn);\n\t\t\t\tif (dif <= acu && dif <= acu * rn) {\n\t\t\t\t\tgin = 1.0 - factor * gin;\n\t\t\t\t\treturn gin;\n\t\t\t\t}\n\t\t\t\tgin = rn;\n\t\t\t}\n\t\t\tfor (i = 0; i < 4; i++) {\n\t\t\t\tpn[i] = pn[i+2];\n\t\t\t}\n\n\t\t\tif (fabs(pn[4]) >= oflo) {\n\t\t\t\tfor (i = 0; i < 4; i++) {\n\t\t\t\t\tpn[i] /= oflo;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\n/*\n * Given a value p in [0..1] of the lower tail area of the normal distribution,\n * compute the limit on the definite integral from [-inf..z] that satisfies p,\n * accurate to 16 decimal places.\n *\n * This implementation is based on:\n *\n *   Wichura, M.J. (1988) Algorithm AS 241: The percentage points of the normal\n *   distribution.  Applied Statistics 37(3):477-484.\n */\nstatic inline double\npt_norm(double p) {\n\tdouble q, r, ret;\n\n\tassert(p > 0.0 && p < 1.0);\n\n\tq = p - 0.5;\n\tif (fabs(q) <= 0.425) {\n\t\t/* p close to 1/2. */\n\t\tr = 0.180625 - q * q;\n\t\treturn q * (((((((2.5090809287301226727e3 * r +\n\t\t    3.3430575583588128105e4) * r + 6.7265770927008700853e4) * r\n\t\t    + 4.5921953931549871457e4) * r + 1.3731693765509461125e4) *\n\t\t    r + 1.9715909503065514427e3) * r + 1.3314166789178437745e2)\n\t\t    * r + 3.3871328727963666080e0) /\n\t\t    (((((((5.2264952788528545610e3 * r +\n\t\t    2.8729085735721942674e4) * r + 3.9307895800092710610e4) * r\n\t\t    + 2.1213794301586595867e4) * r + 5.3941960214247511077e3) *\n\t\t    r + 6.8718700749205790830e2) * r + 4.2313330701600911252e1)\n\t\t    * r + 1.0);\n\t} else {\n\t\tif (q < 0.0) {\n\t\t\tr = p;\n\t\t} else {\n\t\t\tr = 1.0 - p;\n\t\t}\n\t\tassert(r > 0.0);\n\n\t\tr = sqrt(-log(r));\n\t\tif (r <= 5.0) {\n\t\t\t/* p neither close to 1/2 nor 0 or 1. */\n\t\t\tr -= 1.6;\n\t\t\tret = ((((((((7.74545014278341407640e-4 * r +\n\t\t\t    2.27238449892691845833e-2) * r +\n\t\t\t    2.41780725177450611770e-1) * r +\n\t\t\t    1.27045825245236838258e0) * r +\n\t\t\t    3.64784832476320460504e0) * r +\n\t\t\t    5.76949722146069140550e0) * r +\n\t\t\t    4.63033784615654529590e0) * r +\n\t\t\t    1.42343711074968357734e0) /\n\t\t\t    (((((((1.05075007164441684324e-9 * r +\n\t\t\t    5.47593808499534494600e-4) * r +\n\t\t\t    1.51986665636164571966e-2)\n\t\t\t    * r + 1.48103976427480074590e-1) * r +\n\t\t\t    6.89767334985100004550e-1) * r +\n\t\t\t    1.67638483018380384940e0) * r +\n\t\t\t    2.05319162663775882187e0) * r + 1.0));\n\t\t} else {\n\t\t\t/* p near 0 or 1. */\n\t\t\tr -= 5.0;\n\t\t\tret = ((((((((2.01033439929228813265e-7 * r +\n\t\t\t    2.71155556874348757815e-5) * r +\n\t\t\t    1.24266094738807843860e-3) * r +\n\t\t\t    2.65321895265761230930e-2) * r +\n\t\t\t    2.96560571828504891230e-1) * r +\n\t\t\t    1.78482653991729133580e0) * r +\n\t\t\t    5.46378491116411436990e0) * r +\n\t\t\t    6.65790464350110377720e0) /\n\t\t\t    (((((((2.04426310338993978564e-15 * r +\n\t\t\t    1.42151175831644588870e-7) * r +\n\t\t\t    1.84631831751005468180e-5) * r +\n\t\t\t    7.86869131145613259100e-4) * r +\n\t\t\t    1.48753612908506148525e-2) * r +\n\t\t\t    1.36929880922735805310e-1) * r +\n\t\t\t    5.99832206555887937690e-1)\n\t\t\t    * r + 1.0));\n\t\t}\n\t\tif (q < 0.0) {\n\t\t\tret = -ret;\n\t\t}\n\t\treturn ret;\n\t}\n}\n\n/*\n * Given a value p in [0..1] of the lower tail area of the Chi^2 distribution\n * with df degrees of freedom, where ln_gamma_df_2 is ln_gamma(df/2.0), compute\n * the upper limit on the definite integral from [0..z] that satisfies p,\n * accurate to 12 decimal places.\n *\n * This implementation is based on:\n *\n *   Best, D.J., D.E. Roberts (1975) Algorithm AS 91: The percentage points of\n *   the Chi^2 distribution.  Applied Statistics 24(3):385-388.\n *\n *   Shea, B.L. (1991) Algorithm AS R85: A remark on AS 91: The percentage\n *   points of the Chi^2 distribution.  Applied Statistics 40(1):233-235.\n */\nstatic inline double\npt_chi2(double p, double df, double ln_gamma_df_2) {\n\tdouble e, aa, xx, c, ch, a, q, p1, p2, t, x, b, s1, s2, s3, s4, s5, s6;\n\tunsigned i;\n\n\tassert(p >= 0.0 && p < 1.0);\n\tassert(df > 0.0);\n\n\te = 5.0e-7;\n\taa = 0.6931471805;\n\n\txx = 0.5 * df;\n\tc = xx - 1.0;\n\n\tif (df < -1.24 * log(p)) {\n\t\t/* Starting approximation for small Chi^2. */\n\t\tch = pow(p * xx * exp(ln_gamma_df_2 + xx * aa), 1.0 / xx);\n\t\tif (ch - e < 0.0) {\n\t\t\treturn ch;\n\t\t}\n\t} else {\n\t\tif (df > 0.32) {\n\t\t\tx = pt_norm(p);\n\t\t\t/*\n\t\t\t * Starting approximation using Wilson and Hilferty\n\t\t\t * estimate.\n\t\t\t */\n\t\t\tp1 = 0.222222 / df;\n\t\t\tch = df * pow(x * sqrt(p1) + 1.0 - p1, 3.0);\n\t\t\t/* Starting approximation for p tending to 1. */\n\t\t\tif (ch > 2.2 * df + 6.0) {\n\t\t\t\tch = -2.0 * (log(1.0 - p) - c * log(0.5 * ch) +\n\t\t\t\t    ln_gamma_df_2);\n\t\t\t}\n\t\t} else {\n\t\t\tch = 0.4;\n\t\t\ta = log(1.0 - p);\n\t\t\twhile (true) {\n\t\t\t\tq = ch;\n\t\t\t\tp1 = 1.0 + ch * (4.67 + ch);\n\t\t\t\tp2 = ch * (6.73 + ch * (6.66 + ch));\n\t\t\t\tt = -0.5 + (4.67 + 2.0 * ch) / p1 - (6.73 + ch\n\t\t\t\t    * (13.32 + 3.0 * ch)) / p2;\n\t\t\t\tch -= (1.0 - exp(a + ln_gamma_df_2 + 0.5 * ch +\n\t\t\t\t    c * aa) * p2 / p1) / t;\n\t\t\t\tif (fabs(q / ch - 1.0) - 0.01 <= 0.0) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tfor (i = 0; i < 20; i++) {\n\t\t/* Calculation of seven-term Taylor series. */\n\t\tq = ch;\n\t\tp1 = 0.5 * ch;\n\t\tif (p1 < 0.0) {\n\t\t\treturn -1.0;\n\t\t}\n\t\tp2 = p - i_gamma(p1, xx, ln_gamma_df_2);\n\t\tt = p2 * exp(xx * aa + ln_gamma_df_2 + p1 - c * log(ch));\n\t\tb = t / ch;\n\t\ta = 0.5 * t - b * c;\n\t\ts1 = (210.0 + a * (140.0 + a * (105.0 + a * (84.0 + a * (70.0 +\n\t\t    60.0 * a))))) / 420.0;\n\t\ts2 = (420.0 + a * (735.0 + a * (966.0 + a * (1141.0 + 1278.0 *\n\t\t    a)))) / 2520.0;\n\t\ts3 = (210.0 + a * (462.0 + a * (707.0 + 932.0 * a))) / 2520.0;\n\t\ts4 = (252.0 + a * (672.0 + 1182.0 * a) + c * (294.0 + a *\n\t\t    (889.0 + 1740.0 * a))) / 5040.0;\n\t\ts5 = (84.0 + 264.0 * a + c * (175.0 + 606.0 * a)) / 2520.0;\n\t\ts6 = (120.0 + c * (346.0 + 127.0 * c)) / 5040.0;\n\t\tch += t * (1.0 + 0.5 * t * s1 - b * c * (s1 - b * (s2 - b * (s3\n\t\t    - b * (s4 - b * (s5 - b * s6))))));\n\t\tif (fabs(q / ch - 1.0) <= e) {\n\t\t\tbreak;\n\t\t}\n\t}\n\n\treturn ch;\n}\n\n/*\n * Given a value p in [0..1] and Gamma distribution shape and scale parameters,\n * compute the upper limit on the definite integral from [0..z] that satisfies\n * p.\n */\nstatic inline double\npt_gamma(double p, double shape, double scale, double ln_gamma_shape) {\n\treturn pt_chi2(p, shape * 2.0, ln_gamma_shape) * 0.5 * scale;\n}\n"
  },
  {
    "path": "deps/jemalloc/test/include/test/mq.h",
    "content": "void\tmq_nanosleep(unsigned ns);\n\n/*\n * Simple templated message queue implementation that relies on only mutexes for\n * synchronization (which reduces portability issues).  Given the following\n * setup:\n *\n *   typedef struct mq_msg_s mq_msg_t;\n *   struct mq_msg_s {\n *           mq_msg(mq_msg_t) link;\n *           [message data]\n *   };\n *   mq_gen(, mq_, mq_t, mq_msg_t, link)\n *\n * The API is as follows:\n *\n *   bool mq_init(mq_t *mq);\n *   void mq_fini(mq_t *mq);\n *   unsigned mq_count(mq_t *mq);\n *   mq_msg_t *mq_tryget(mq_t *mq);\n *   mq_msg_t *mq_get(mq_t *mq);\n *   void mq_put(mq_t *mq, mq_msg_t *msg);\n *\n * The message queue linkage embedded in each message is to be treated as\n * externally opaque (no need to initialize or clean up externally).  mq_fini()\n * does not perform any cleanup of messages, since it knows nothing of their\n * payloads.\n */\n#define mq_msg(a_mq_msg_type)\tql_elm(a_mq_msg_type)\n\n#define mq_gen(a_attr, a_prefix, a_mq_type, a_mq_msg_type, a_field)\t\\\ntypedef struct {\t\t\t\t\t\t\t\\\n\tmtx_t\t\t\tlock;\t\t\t\t\t\\\n\tql_head(a_mq_msg_type)\tmsgs;\t\t\t\t\t\\\n\tunsigned\t\tcount;\t\t\t\t\t\\\n} a_mq_type;\t\t\t\t\t\t\t\t\\\na_attr bool\t\t\t\t\t\t\t\t\\\na_prefix##init(a_mq_type *mq) {\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tif (mtx_init(&mq->lock)) {\t\t\t\t\t\\\n\t\treturn true;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tql_new(&mq->msgs);\t\t\t\t\t\t\\\n\tmq->count = 0;\t\t\t\t\t\t\t\\\n\treturn false;\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##fini(a_mq_type *mq) {\t\t\t\t\t\t\\\n\tmtx_fini(&mq->lock);\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr unsigned\t\t\t\t\t\t\t\t\\\na_prefix##count(a_mq_type *mq) {\t\t\t\t\t\\\n\tunsigned count;\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tmtx_lock(&mq->lock);\t\t\t\t\t\t\\\n\tcount = mq->count;\t\t\t\t\t\t\\\n\tmtx_unlock(&mq->lock);\t\t\t\t\t\t\\\n\treturn count;\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_mq_msg_type *\t\t\t\t\t\t\t\\\na_prefix##tryget(a_mq_type *mq) {\t\t\t\t\t\\\n\ta_mq_msg_type *msg;\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tmtx_lock(&mq->lock);\t\t\t\t\t\t\\\n\tmsg = ql_first(&mq->msgs);\t\t\t\t\t\\\n\tif (msg != NULL) {\t\t\t\t\t\t\\\n\t\tql_head_remove(&mq->msgs, a_mq_msg_type, a_field);\t\\\n\t\tmq->count--;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tmtx_unlock(&mq->lock);\t\t\t\t\t\t\\\n\treturn msg;\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_mq_msg_type *\t\t\t\t\t\t\t\\\na_prefix##get(a_mq_type *mq) {\t\t\t\t\t\t\\\n\ta_mq_msg_type *msg;\t\t\t\t\t\t\\\n\tunsigned ns;\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tmsg = a_prefix##tryget(mq);\t\t\t\t\t\\\n\tif (msg != NULL) {\t\t\t\t\t\t\\\n\t\treturn msg;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tns = 1;\t\t\t\t\t\t\t\t\\\n\twhile (true) {\t\t\t\t\t\t\t\\\n\t\tmq_nanosleep(ns);\t\t\t\t\t\\\n\t\tmsg = a_prefix##tryget(mq);\t\t\t\t\\\n\t\tif (msg != NULL) {\t\t\t\t\t\\\n\t\t\treturn msg;\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\tif (ns < 1000*1000*1000) {\t\t\t\t\\\n\t\t\t/* Double sleep time, up to max 1 second. */\t\\\n\t\t\tns <<= 1;\t\t\t\t\t\\\n\t\t\tif (ns > 1000*1000*1000) {\t\t\t\\\n\t\t\t\tns = 1000*1000*1000;\t\t\t\\\n\t\t\t}\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##put(a_mq_type *mq, a_mq_msg_type *msg) {\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tmtx_lock(&mq->lock);\t\t\t\t\t\t\\\n\tql_elm_new(msg, a_field);\t\t\t\t\t\\\n\tql_tail_insert(&mq->msgs, msg, a_field);\t\t\t\\\n\tmq->count++;\t\t\t\t\t\t\t\\\n\tmtx_unlock(&mq->lock);\t\t\t\t\t\t\\\n}\n"
  },
  {
    "path": "deps/jemalloc/test/include/test/mtx.h",
    "content": "/*\n * mtx is a slightly simplified version of malloc_mutex.  This code duplication\n * is unfortunate, but there are allocator bootstrapping considerations that\n * would leak into the test infrastructure if malloc_mutex were used directly\n * in tests.\n */\n\ntypedef struct {\n#ifdef _WIN32\n\tCRITICAL_SECTION\tlock;\n#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))\n\tos_unfair_lock\t\tlock;\n#else\n\tpthread_mutex_t\t\tlock;\n#endif\n} mtx_t;\n\nbool\tmtx_init(mtx_t *mtx);\nvoid\tmtx_fini(mtx_t *mtx);\nvoid\tmtx_lock(mtx_t *mtx);\nvoid\tmtx_unlock(mtx_t *mtx);\n"
  },
  {
    "path": "deps/jemalloc/test/include/test/test.h",
    "content": "#define ASSERT_BUFSIZE\t256\n\n#define assert_cmp(t, a, b, cmp, neg_cmp, pri, ...) do {\t\t\\\n\tt a_ = (a);\t\t\t\t\t\t\t\\\n\tt b_ = (b);\t\t\t\t\t\t\t\\\n\tif (!(a_ cmp b_)) {\t\t\t\t\t\t\\\n\t\tchar prefix[ASSERT_BUFSIZE];\t\t\t\t\\\n\t\tchar message[ASSERT_BUFSIZE];\t\t\t\t\\\n\t\tmalloc_snprintf(prefix, sizeof(prefix),\t\t\t\\\n\t\t    \"%s:%s:%d: Failed assertion: \"\t\t\t\\\n\t\t    \"(%s) \" #cmp \" (%s) --> \"\t\t\t\t\\\n\t\t    \"%\" pri \" \" #neg_cmp \" %\" pri \": \",\t\t\t\\\n\t\t    __func__, __FILE__, __LINE__,\t\t\t\\\n\t\t    #a, #b, a_, b_);\t\t\t\t\t\\\n\t\tmalloc_snprintf(message, sizeof(message), __VA_ARGS__);\t\\\n\t\tp_test_fail(prefix, message);\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define assert_ptr_eq(a, b, ...)\tassert_cmp(void *, a, b, ==,\t\\\n    !=, \"p\", __VA_ARGS__)\n#define assert_ptr_ne(a, b, ...)\tassert_cmp(void *, a, b, !=,\t\\\n    ==, \"p\", __VA_ARGS__)\n#define assert_ptr_null(a, ...)\t\tassert_cmp(void *, a, NULL, ==,\t\\\n    !=, \"p\", __VA_ARGS__)\n#define assert_ptr_not_null(a, ...)\tassert_cmp(void *, a, NULL, !=,\t\\\n    ==, \"p\", __VA_ARGS__)\n\n#define assert_c_eq(a, b, ...)\tassert_cmp(char, a, b, ==, !=, \"c\", __VA_ARGS__)\n#define assert_c_ne(a, b, ...)\tassert_cmp(char, a, b, !=, ==, \"c\", __VA_ARGS__)\n#define assert_c_lt(a, b, ...)\tassert_cmp(char, a, b, <, >=, \"c\", __VA_ARGS__)\n#define assert_c_le(a, b, ...)\tassert_cmp(char, a, b, <=, >, \"c\", __VA_ARGS__)\n#define assert_c_ge(a, b, ...)\tassert_cmp(char, a, b, >=, <, \"c\", __VA_ARGS__)\n#define assert_c_gt(a, b, ...)\tassert_cmp(char, a, b, >, <=, \"c\", __VA_ARGS__)\n\n#define assert_x_eq(a, b, ...)\tassert_cmp(int, a, b, ==, !=, \"#x\", __VA_ARGS__)\n#define assert_x_ne(a, b, ...)\tassert_cmp(int, a, b, !=, ==, \"#x\", __VA_ARGS__)\n#define assert_x_lt(a, b, ...)\tassert_cmp(int, a, b, <, >=, \"#x\", __VA_ARGS__)\n#define assert_x_le(a, b, ...)\tassert_cmp(int, a, b, <=, >, \"#x\", __VA_ARGS__)\n#define assert_x_ge(a, b, ...)\tassert_cmp(int, a, b, >=, <, \"#x\", __VA_ARGS__)\n#define assert_x_gt(a, b, ...)\tassert_cmp(int, a, b, >, <=, \"#x\", __VA_ARGS__)\n\n#define assert_d_eq(a, b, ...)\tassert_cmp(int, a, b, ==, !=, \"d\", __VA_ARGS__)\n#define assert_d_ne(a, b, ...)\tassert_cmp(int, a, b, !=, ==, \"d\", __VA_ARGS__)\n#define assert_d_lt(a, b, ...)\tassert_cmp(int, a, b, <, >=, \"d\", __VA_ARGS__)\n#define assert_d_le(a, b, ...)\tassert_cmp(int, a, b, <=, >, \"d\", __VA_ARGS__)\n#define assert_d_ge(a, b, ...)\tassert_cmp(int, a, b, >=, <, \"d\", __VA_ARGS__)\n#define assert_d_gt(a, b, ...)\tassert_cmp(int, a, b, >, <=, \"d\", __VA_ARGS__)\n\n#define assert_u_eq(a, b, ...)\tassert_cmp(int, a, b, ==, !=, \"u\", __VA_ARGS__)\n#define assert_u_ne(a, b, ...)\tassert_cmp(int, a, b, !=, ==, \"u\", __VA_ARGS__)\n#define assert_u_lt(a, b, ...)\tassert_cmp(int, a, b, <, >=, \"u\", __VA_ARGS__)\n#define assert_u_le(a, b, ...)\tassert_cmp(int, a, b, <=, >, \"u\", __VA_ARGS__)\n#define assert_u_ge(a, b, ...)\tassert_cmp(int, a, b, >=, <, \"u\", __VA_ARGS__)\n#define assert_u_gt(a, b, ...)\tassert_cmp(int, a, b, >, <=, \"u\", __VA_ARGS__)\n\n#define assert_ld_eq(a, b, ...)\tassert_cmp(long, a, b, ==,\t\\\n    !=, \"ld\", __VA_ARGS__)\n#define assert_ld_ne(a, b, ...)\tassert_cmp(long, a, b, !=,\t\\\n    ==, \"ld\", __VA_ARGS__)\n#define assert_ld_lt(a, b, ...)\tassert_cmp(long, a, b, <,\t\\\n    >=, \"ld\", __VA_ARGS__)\n#define assert_ld_le(a, b, ...)\tassert_cmp(long, a, b, <=,\t\\\n    >, \"ld\", __VA_ARGS__)\n#define assert_ld_ge(a, b, ...)\tassert_cmp(long, a, b, >=,\t\\\n    <, \"ld\", __VA_ARGS__)\n#define assert_ld_gt(a, b, ...)\tassert_cmp(long, a, b, >,\t\\\n    <=, \"ld\", __VA_ARGS__)\n\n#define assert_lu_eq(a, b, ...)\tassert_cmp(unsigned long,\t\\\n    a, b, ==, !=, \"lu\", __VA_ARGS__)\n#define assert_lu_ne(a, b, ...)\tassert_cmp(unsigned long,\t\\\n    a, b, !=, ==, \"lu\", __VA_ARGS__)\n#define assert_lu_lt(a, b, ...)\tassert_cmp(unsigned long,\t\\\n    a, b, <, >=, \"lu\", __VA_ARGS__)\n#define assert_lu_le(a, b, ...)\tassert_cmp(unsigned long,\t\\\n    a, b, <=, >, \"lu\", __VA_ARGS__)\n#define assert_lu_ge(a, b, ...)\tassert_cmp(unsigned long,\t\\\n    a, b, >=, <, \"lu\", __VA_ARGS__)\n#define assert_lu_gt(a, b, ...)\tassert_cmp(unsigned long,\t\\\n    a, b, >, <=, \"lu\", __VA_ARGS__)\n\n#define assert_qd_eq(a, b, ...)\tassert_cmp(long long, a, b, ==,\t\\\n    !=, \"qd\", __VA_ARGS__)\n#define assert_qd_ne(a, b, ...)\tassert_cmp(long long, a, b, !=,\t\\\n    ==, \"qd\", __VA_ARGS__)\n#define assert_qd_lt(a, b, ...)\tassert_cmp(long long, a, b, <,\t\\\n    >=, \"qd\", __VA_ARGS__)\n#define assert_qd_le(a, b, ...)\tassert_cmp(long long, a, b, <=,\t\\\n    >, \"qd\", __VA_ARGS__)\n#define assert_qd_ge(a, b, ...)\tassert_cmp(long long, a, b, >=,\t\\\n    <, \"qd\", __VA_ARGS__)\n#define assert_qd_gt(a, b, ...)\tassert_cmp(long long, a, b, >,\t\\\n    <=, \"qd\", __VA_ARGS__)\n\n#define assert_qu_eq(a, b, ...)\tassert_cmp(unsigned long long,\t\\\n    a, b, ==, !=, \"qu\", __VA_ARGS__)\n#define assert_qu_ne(a, b, ...)\tassert_cmp(unsigned long long,\t\\\n    a, b, !=, ==, \"qu\", __VA_ARGS__)\n#define assert_qu_lt(a, b, ...)\tassert_cmp(unsigned long long,\t\\\n    a, b, <, >=, \"qu\", __VA_ARGS__)\n#define assert_qu_le(a, b, ...)\tassert_cmp(unsigned long long,\t\\\n    a, b, <=, >, \"qu\", __VA_ARGS__)\n#define assert_qu_ge(a, b, ...)\tassert_cmp(unsigned long long,\t\\\n    a, b, >=, <, \"qu\", __VA_ARGS__)\n#define assert_qu_gt(a, b, ...)\tassert_cmp(unsigned long long,\t\\\n    a, b, >, <=, \"qu\", __VA_ARGS__)\n\n#define assert_jd_eq(a, b, ...)\tassert_cmp(intmax_t, a, b, ==,\t\\\n    !=, \"jd\", __VA_ARGS__)\n#define assert_jd_ne(a, b, ...)\tassert_cmp(intmax_t, a, b, !=,\t\\\n    ==, \"jd\", __VA_ARGS__)\n#define assert_jd_lt(a, b, ...)\tassert_cmp(intmax_t, a, b, <,\t\\\n    >=, \"jd\", __VA_ARGS__)\n#define assert_jd_le(a, b, ...)\tassert_cmp(intmax_t, a, b, <=,\t\\\n    >, \"jd\", __VA_ARGS__)\n#define assert_jd_ge(a, b, ...)\tassert_cmp(intmax_t, a, b, >=,\t\\\n    <, \"jd\", __VA_ARGS__)\n#define assert_jd_gt(a, b, ...)\tassert_cmp(intmax_t, a, b, >,\t\\\n    <=, \"jd\", __VA_ARGS__)\n\n#define assert_ju_eq(a, b, ...)\tassert_cmp(uintmax_t, a, b, ==,\t\\\n    !=, \"ju\", __VA_ARGS__)\n#define assert_ju_ne(a, b, ...)\tassert_cmp(uintmax_t, a, b, !=,\t\\\n    ==, \"ju\", __VA_ARGS__)\n#define assert_ju_lt(a, b, ...)\tassert_cmp(uintmax_t, a, b, <,\t\\\n    >=, \"ju\", __VA_ARGS__)\n#define assert_ju_le(a, b, ...)\tassert_cmp(uintmax_t, a, b, <=,\t\\\n    >, \"ju\", __VA_ARGS__)\n#define assert_ju_ge(a, b, ...)\tassert_cmp(uintmax_t, a, b, >=,\t\\\n    <, \"ju\", __VA_ARGS__)\n#define assert_ju_gt(a, b, ...)\tassert_cmp(uintmax_t, a, b, >,\t\\\n    <=, \"ju\", __VA_ARGS__)\n\n#define assert_zd_eq(a, b, ...)\tassert_cmp(ssize_t, a, b, ==,\t\\\n    !=, \"zd\", __VA_ARGS__)\n#define assert_zd_ne(a, b, ...)\tassert_cmp(ssize_t, a, b, !=,\t\\\n    ==, \"zd\", __VA_ARGS__)\n#define assert_zd_lt(a, b, ...)\tassert_cmp(ssize_t, a, b, <,\t\\\n    >=, \"zd\", __VA_ARGS__)\n#define assert_zd_le(a, b, ...)\tassert_cmp(ssize_t, a, b, <=,\t\\\n    >, \"zd\", __VA_ARGS__)\n#define assert_zd_ge(a, b, ...)\tassert_cmp(ssize_t, a, b, >=,\t\\\n    <, \"zd\", __VA_ARGS__)\n#define assert_zd_gt(a, b, ...)\tassert_cmp(ssize_t, a, b, >,\t\\\n    <=, \"zd\", __VA_ARGS__)\n\n#define assert_zu_eq(a, b, ...)\tassert_cmp(size_t, a, b, ==,\t\\\n    !=, \"zu\", __VA_ARGS__)\n#define assert_zu_ne(a, b, ...)\tassert_cmp(size_t, a, b, !=,\t\\\n    ==, \"zu\", __VA_ARGS__)\n#define assert_zu_lt(a, b, ...)\tassert_cmp(size_t, a, b, <,\t\\\n    >=, \"zu\", __VA_ARGS__)\n#define assert_zu_le(a, b, ...)\tassert_cmp(size_t, a, b, <=,\t\\\n    >, \"zu\", __VA_ARGS__)\n#define assert_zu_ge(a, b, ...)\tassert_cmp(size_t, a, b, >=,\t\\\n    <, \"zu\", __VA_ARGS__)\n#define assert_zu_gt(a, b, ...)\tassert_cmp(size_t, a, b, >,\t\\\n    <=, \"zu\", __VA_ARGS__)\n\n#define assert_d32_eq(a, b, ...)\tassert_cmp(int32_t, a, b, ==,\t\\\n    !=, FMTd32, __VA_ARGS__)\n#define assert_d32_ne(a, b, ...)\tassert_cmp(int32_t, a, b, !=,\t\\\n    ==, FMTd32, __VA_ARGS__)\n#define assert_d32_lt(a, b, ...)\tassert_cmp(int32_t, a, b, <,\t\\\n    >=, FMTd32, __VA_ARGS__)\n#define assert_d32_le(a, b, ...)\tassert_cmp(int32_t, a, b, <=,\t\\\n    >, FMTd32, __VA_ARGS__)\n#define assert_d32_ge(a, b, ...)\tassert_cmp(int32_t, a, b, >=,\t\\\n    <, FMTd32, __VA_ARGS__)\n#define assert_d32_gt(a, b, ...)\tassert_cmp(int32_t, a, b, >,\t\\\n    <=, FMTd32, __VA_ARGS__)\n\n#define assert_u32_eq(a, b, ...)\tassert_cmp(uint32_t, a, b, ==,\t\\\n    !=, FMTu32, __VA_ARGS__)\n#define assert_u32_ne(a, b, ...)\tassert_cmp(uint32_t, a, b, !=,\t\\\n    ==, FMTu32, __VA_ARGS__)\n#define assert_u32_lt(a, b, ...)\tassert_cmp(uint32_t, a, b, <,\t\\\n    >=, FMTu32, __VA_ARGS__)\n#define assert_u32_le(a, b, ...)\tassert_cmp(uint32_t, a, b, <=,\t\\\n    >, FMTu32, __VA_ARGS__)\n#define assert_u32_ge(a, b, ...)\tassert_cmp(uint32_t, a, b, >=,\t\\\n    <, FMTu32, __VA_ARGS__)\n#define assert_u32_gt(a, b, ...)\tassert_cmp(uint32_t, a, b, >,\t\\\n    <=, FMTu32, __VA_ARGS__)\n\n#define assert_d64_eq(a, b, ...)\tassert_cmp(int64_t, a, b, ==,\t\\\n    !=, FMTd64, __VA_ARGS__)\n#define assert_d64_ne(a, b, ...)\tassert_cmp(int64_t, a, b, !=,\t\\\n    ==, FMTd64, __VA_ARGS__)\n#define assert_d64_lt(a, b, ...)\tassert_cmp(int64_t, a, b, <,\t\\\n    >=, FMTd64, __VA_ARGS__)\n#define assert_d64_le(a, b, ...)\tassert_cmp(int64_t, a, b, <=,\t\\\n    >, FMTd64, __VA_ARGS__)\n#define assert_d64_ge(a, b, ...)\tassert_cmp(int64_t, a, b, >=,\t\\\n    <, FMTd64, __VA_ARGS__)\n#define assert_d64_gt(a, b, ...)\tassert_cmp(int64_t, a, b, >,\t\\\n    <=, FMTd64, __VA_ARGS__)\n\n#define assert_u64_eq(a, b, ...)\tassert_cmp(uint64_t, a, b, ==,\t\\\n    !=, FMTu64, __VA_ARGS__)\n#define assert_u64_ne(a, b, ...)\tassert_cmp(uint64_t, a, b, !=,\t\\\n    ==, FMTu64, __VA_ARGS__)\n#define assert_u64_lt(a, b, ...)\tassert_cmp(uint64_t, a, b, <,\t\\\n    >=, FMTu64, __VA_ARGS__)\n#define assert_u64_le(a, b, ...)\tassert_cmp(uint64_t, a, b, <=,\t\\\n    >, FMTu64, __VA_ARGS__)\n#define assert_u64_ge(a, b, ...)\tassert_cmp(uint64_t, a, b, >=,\t\\\n    <, FMTu64, __VA_ARGS__)\n#define assert_u64_gt(a, b, ...)\tassert_cmp(uint64_t, a, b, >,\t\\\n    <=, FMTu64, __VA_ARGS__)\n\n#define assert_b_eq(a, b, ...) do {\t\t\t\t\t\\\n\tbool a_ = (a);\t\t\t\t\t\t\t\\\n\tbool b_ = (b);\t\t\t\t\t\t\t\\\n\tif (!(a_ == b_)) {\t\t\t\t\t\t\\\n\t\tchar prefix[ASSERT_BUFSIZE];\t\t\t\t\\\n\t\tchar message[ASSERT_BUFSIZE];\t\t\t\t\\\n\t\tmalloc_snprintf(prefix, sizeof(prefix),\t\t\t\\\n\t\t    \"%s:%s:%d: Failed assertion: \"\t\t\t\\\n\t\t    \"(%s) == (%s) --> %s != %s: \",\t\t\t\\\n\t\t    __func__, __FILE__, __LINE__,\t\t\t\\\n\t\t    #a, #b, a_ ? \"true\" : \"false\",\t\t\t\\\n\t\t    b_ ? \"true\" : \"false\");\t\t\t\t\\\n\t\tmalloc_snprintf(message, sizeof(message), __VA_ARGS__);\t\\\n\t\tp_test_fail(prefix, message);\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n#define assert_b_ne(a, b, ...) do {\t\t\t\t\t\\\n\tbool a_ = (a);\t\t\t\t\t\t\t\\\n\tbool b_ = (b);\t\t\t\t\t\t\t\\\n\tif (!(a_ != b_)) {\t\t\t\t\t\t\\\n\t\tchar prefix[ASSERT_BUFSIZE];\t\t\t\t\\\n\t\tchar message[ASSERT_BUFSIZE];\t\t\t\t\\\n\t\tmalloc_snprintf(prefix, sizeof(prefix),\t\t\t\\\n\t\t    \"%s:%s:%d: Failed assertion: \"\t\t\t\\\n\t\t    \"(%s) != (%s) --> %s == %s: \",\t\t\t\\\n\t\t    __func__, __FILE__, __LINE__,\t\t\t\\\n\t\t    #a, #b, a_ ? \"true\" : \"false\",\t\t\t\\\n\t\t    b_ ? \"true\" : \"false\");\t\t\t\t\\\n\t\tmalloc_snprintf(message, sizeof(message), __VA_ARGS__);\t\\\n\t\tp_test_fail(prefix, message);\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n#define assert_true(a, ...)\tassert_b_eq(a, true, __VA_ARGS__)\n#define assert_false(a, ...)\tassert_b_eq(a, false, __VA_ARGS__)\n\n#define assert_str_eq(a, b, ...) do {\t\t\t\t\\\n\tif (strcmp((a), (b))) {\t\t\t\t\t\t\\\n\t\tchar prefix[ASSERT_BUFSIZE];\t\t\t\t\\\n\t\tchar message[ASSERT_BUFSIZE];\t\t\t\t\\\n\t\tmalloc_snprintf(prefix, sizeof(prefix),\t\t\t\\\n\t\t    \"%s:%s:%d: Failed assertion: \"\t\t\t\\\n\t\t    \"(%s) same as (%s) --> \"\t\t\t\t\\\n\t\t    \"\\\"%s\\\" differs from \\\"%s\\\": \",\t\t\t\\\n\t\t    __func__, __FILE__, __LINE__, #a, #b, a, b);\t\\\n\t\tmalloc_snprintf(message, sizeof(message), __VA_ARGS__);\t\\\n\t\tp_test_fail(prefix, message);\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n#define assert_str_ne(a, b, ...) do {\t\t\t\t\\\n\tif (!strcmp((a), (b))) {\t\t\t\t\t\\\n\t\tchar prefix[ASSERT_BUFSIZE];\t\t\t\t\\\n\t\tchar message[ASSERT_BUFSIZE];\t\t\t\t\\\n\t\tmalloc_snprintf(prefix, sizeof(prefix),\t\t\t\\\n\t\t    \"%s:%s:%d: Failed assertion: \"\t\t\t\\\n\t\t    \"(%s) differs from (%s) --> \"\t\t\t\\\n\t\t    \"\\\"%s\\\" same as \\\"%s\\\": \",\t\t\t\t\\\n\t\t    __func__, __FILE__, __LINE__, #a, #b, a, b);\t\\\n\t\tmalloc_snprintf(message, sizeof(message), __VA_ARGS__);\t\\\n\t\tp_test_fail(prefix, message);\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define assert_not_reached(...) do {\t\t\t\t\t\\\n\tchar prefix[ASSERT_BUFSIZE];\t\t\t\t\t\\\n\tchar message[ASSERT_BUFSIZE];\t\t\t\t\t\\\n\tmalloc_snprintf(prefix, sizeof(prefix),\t\t\t\t\\\n\t    \"%s:%s:%d: Unreachable code reached: \",\t\t\t\\\n\t    __func__, __FILE__, __LINE__);\t\t\t\t\\\n\tmalloc_snprintf(message, sizeof(message), __VA_ARGS__);\t\t\\\n\tp_test_fail(prefix, message);\t\t\t\t\t\\\n} while (0)\n\n/*\n * If this enum changes, corresponding changes in test/test.sh.in are also\n * necessary.\n */\ntypedef enum {\n\ttest_status_pass = 0,\n\ttest_status_skip = 1,\n\ttest_status_fail = 2,\n\n\ttest_status_count = 3\n} test_status_t;\n\ntypedef void (test_t)(void);\n\n#define TEST_BEGIN(f)\t\t\t\t\t\t\t\\\nstatic void\t\t\t\t\t\t\t\t\\\nf(void) {\t\t\t\t\t\t\t\t\\\n\tp_test_init(#f);\n\n#define TEST_END\t\t\t\t\t\t\t\\\n\tgoto label_test_end;\t\t\t\t\t\t\\\nlabel_test_end:\t\t\t\t\t\t\t\t\\\n\tp_test_fini();\t\t\t\t\t\t\t\\\n}\n\n#define test(...)\t\t\t\t\t\t\t\\\n\tp_test(__VA_ARGS__, NULL)\n\n#define test_no_reentrancy(...)\t\t\t\t\t\t\t\\\n\tp_test_no_reentrancy(__VA_ARGS__, NULL)\n\n#define test_no_malloc_init(...)\t\t\t\t\t\\\n\tp_test_no_malloc_init(__VA_ARGS__, NULL)\n\n#define test_skip_if(e) do {\t\t\t\t\t\t\\\n\tif (e) {\t\t\t\t\t\t\t\\\n\t\ttest_skip(\"%s:%s:%d: Test skipped: (%s)\",\t\t\\\n\t\t    __func__, __FILE__, __LINE__, #e);\t\t\t\\\n\t\tgoto label_test_end;\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\nbool test_is_reentrant();\n\nvoid\ttest_skip(const char *format, ...) JEMALLOC_FORMAT_PRINTF(1, 2);\nvoid\ttest_fail(const char *format, ...) JEMALLOC_FORMAT_PRINTF(1, 2);\n\n/* For private use by macros. */\ntest_status_t\tp_test(test_t *t, ...);\ntest_status_t\tp_test_no_reentrancy(test_t *t, ...);\ntest_status_t\tp_test_no_malloc_init(test_t *t, ...);\nvoid\tp_test_init(const char *name);\nvoid\tp_test_fini(void);\nvoid\tp_test_fail(const char *prefix, const char *message);\n"
  },
  {
    "path": "deps/jemalloc/test/include/test/thd.h",
    "content": "/* Abstraction layer for threading in tests. */\n#ifdef _WIN32\ntypedef HANDLE thd_t;\n#else\ntypedef pthread_t thd_t;\n#endif\n\nvoid\tthd_create(thd_t *thd, void *(*proc)(void *), void *arg);\nvoid\tthd_join(thd_t thd, void **ret);\n"
  },
  {
    "path": "deps/jemalloc/test/include/test/timer.h",
    "content": "/* Simple timer, for use in benchmark reporting. */\n\ntypedef struct {\n\tnstime_t t0;\n\tnstime_t t1;\n} timedelta_t;\n\nvoid\ttimer_start(timedelta_t *timer);\nvoid\ttimer_stop(timedelta_t *timer);\nuint64_t\ttimer_usec(const timedelta_t *timer);\nvoid\ttimer_ratio(timedelta_t *a, timedelta_t *b, char *buf, size_t buflen);\n"
  },
  {
    "path": "deps/jemalloc/test/integration/MALLOCX_ARENA.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#define NTHREADS 10\n\nstatic bool have_dss =\n#ifdef JEMALLOC_DSS\n    true\n#else\n    false\n#endif\n    ;\n\nvoid *\nthd_start(void *arg) {\n\tunsigned thread_ind = (unsigned)(uintptr_t)arg;\n\tunsigned arena_ind;\n\tvoid *p;\n\tsize_t sz;\n\n\tsz = sizeof(arena_ind);\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena_ind, &sz, NULL, 0),\n\t    0, \"Error in arenas.create\");\n\n\tif (thread_ind % 4 != 3) {\n\t\tsize_t mib[3];\n\t\tsize_t miblen = sizeof(mib) / sizeof(size_t);\n\t\tconst char *dss_precs[] = {\"disabled\", \"primary\", \"secondary\"};\n\t\tunsigned prec_ind = thread_ind %\n\t\t    (sizeof(dss_precs)/sizeof(char*));\n\t\tconst char *dss = dss_precs[prec_ind];\n\t\tint expected_err = (have_dss || prec_ind == 0) ? 0 : EFAULT;\n\t\tassert_d_eq(mallctlnametomib(\"arena.0.dss\", mib, &miblen), 0,\n\t\t    \"Error in mallctlnametomib()\");\n\t\tmib[1] = arena_ind;\n\t\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, (void *)&dss,\n\t\t    sizeof(const char *)), expected_err,\n\t\t    \"Error in mallctlbymib()\");\n\t}\n\n\tp = mallocx(1, MALLOCX_ARENA(arena_ind));\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\tdallocx(p, 0);\n\n\treturn NULL;\n}\n\nTEST_BEGIN(test_MALLOCX_ARENA) {\n\tthd_t thds[NTHREADS];\n\tunsigned i;\n\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tthd_create(&thds[i], thd_start,\n\t\t    (void *)(uintptr_t)i);\n\t}\n\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tthd_join(thds[i], NULL);\n\t}\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_MALLOCX_ARENA);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/integration/aligned_alloc.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#define MAXALIGN (((size_t)1) << 23)\n\n/*\n * On systems which can't merge extents, tests that call this function generate\n * a lot of dirty memory very quickly.  Purging between cycles mitigates\n * potential OOM on e.g. 32-bit Windows.\n */\nstatic void\npurge(void) {\n\tassert_d_eq(mallctl(\"arena.0.purge\", NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctl error\");\n}\n\nTEST_BEGIN(test_alignment_errors) {\n\tsize_t alignment;\n\tvoid *p;\n\n\talignment = 0;\n\tset_errno(0);\n\tp = aligned_alloc(alignment, 1);\n\tassert_false(p != NULL || get_errno() != EINVAL,\n\t    \"Expected error for invalid alignment %zu\", alignment);\n\n\tfor (alignment = sizeof(size_t); alignment < MAXALIGN;\n\t    alignment <<= 1) {\n\t\tset_errno(0);\n\t\tp = aligned_alloc(alignment + 1, 1);\n\t\tassert_false(p != NULL || get_errno() != EINVAL,\n\t\t    \"Expected error for invalid alignment %zu\",\n\t\t    alignment + 1);\n\t}\n}\nTEST_END\n\n\n/*\n * GCC \"-Walloc-size-larger-than\" warning detects when one of the memory\n * allocation functions is called with a size larger than the maximum size that\n * they support. Here we want to explicitly test that the allocation functions\n * do indeed fail properly when this is the case, which triggers the warning.\n * Therefore we disable the warning for these tests.\n */\nJEMALLOC_DIAGNOSTIC_PUSH\nJEMALLOC_DIAGNOSTIC_IGNORE_ALLOC_SIZE_LARGER_THAN\n\nTEST_BEGIN(test_oom_errors) {\n\tsize_t alignment, size;\n\tvoid *p;\n\n#if LG_SIZEOF_PTR == 3\n\talignment = UINT64_C(0x8000000000000000);\n\tsize      = UINT64_C(0x8000000000000000);\n#else\n\talignment = 0x80000000LU;\n\tsize      = 0x80000000LU;\n#endif\n\tset_errno(0);\n\tp = aligned_alloc(alignment, size);\n\tassert_false(p != NULL || get_errno() != ENOMEM,\n\t    \"Expected error for aligned_alloc(%zu, %zu)\",\n\t    alignment, size);\n\n#if LG_SIZEOF_PTR == 3\n\talignment = UINT64_C(0x4000000000000000);\n\tsize      = UINT64_C(0xc000000000000001);\n#else\n\talignment = 0x40000000LU;\n\tsize      = 0xc0000001LU;\n#endif\n\tset_errno(0);\n\tp = aligned_alloc(alignment, size);\n\tassert_false(p != NULL || get_errno() != ENOMEM,\n\t    \"Expected error for aligned_alloc(%zu, %zu)\",\n\t    alignment, size);\n\n\talignment = 0x10LU;\n#if LG_SIZEOF_PTR == 3\n\tsize = UINT64_C(0xfffffffffffffff0);\n#else\n\tsize = 0xfffffff0LU;\n#endif\n\tset_errno(0);\n\tp = aligned_alloc(alignment, size);\n\tassert_false(p != NULL || get_errno() != ENOMEM,\n\t    \"Expected error for aligned_alloc(&p, %zu, %zu)\",\n\t    alignment, size);\n}\nTEST_END\n\n/* Re-enable the \"-Walloc-size-larger-than=\" warning */\nJEMALLOC_DIAGNOSTIC_POP\n\nTEST_BEGIN(test_alignment_and_size) {\n#define NITER 4\n\tsize_t alignment, size, total;\n\tunsigned i;\n\tvoid *ps[NITER];\n\n\tfor (i = 0; i < NITER; i++) {\n\t\tps[i] = NULL;\n\t}\n\n\tfor (alignment = 8;\n\t    alignment <= MAXALIGN;\n\t    alignment <<= 1) {\n\t\ttotal = 0;\n\t\tfor (size = 1;\n\t\t    size < 3 * alignment && size < (1U << 31);\n\t\t    size += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {\n\t\t\tfor (i = 0; i < NITER; i++) {\n\t\t\t\tps[i] = aligned_alloc(alignment, size);\n\t\t\t\tif (ps[i] == NULL) {\n\t\t\t\t\tchar buf[BUFERROR_BUF];\n\n\t\t\t\t\tbuferror(get_errno(), buf, sizeof(buf));\n\t\t\t\t\ttest_fail(\n\t\t\t\t\t    \"Error for alignment=%zu, \"\n\t\t\t\t\t    \"size=%zu (%#zx): %s\",\n\t\t\t\t\t    alignment, size, size, buf);\n\t\t\t\t}\n\t\t\t\ttotal += malloc_usable_size(ps[i]);\n\t\t\t\tif (total >= (MAXALIGN << 1)) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (i = 0; i < NITER; i++) {\n\t\t\t\tif (ps[i] != NULL) {\n\t\t\t\t\tfree(ps[i]);\n\t\t\t\t\tps[i] = NULL;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tpurge();\n\t}\n#undef NITER\n}\nTEST_END\n\nTEST_BEGIN(test_zero_alloc) {\n\tvoid *res = aligned_alloc(8, 0);\n\tassert(res);\n\tsize_t usable = malloc_usable_size(res);\n\tassert(usable > 0);\n\tfree(res);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_alignment_errors,\n\t    test_oom_errors,\n\t    test_alignment_and_size,\n\t    test_zero_alloc);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/integration/allocated.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic const bool config_stats =\n#ifdef JEMALLOC_STATS\n    true\n#else\n    false\n#endif\n    ;\n\nvoid *\nthd_start(void *arg) {\n\tint err;\n\tvoid *p;\n\tuint64_t a0, a1, d0, d1;\n\tuint64_t *ap0, *ap1, *dp0, *dp1;\n\tsize_t sz, usize;\n\n\tsz = sizeof(a0);\n\tif ((err = mallctl(\"thread.allocated\", (void *)&a0, &sz, NULL, 0))) {\n\t\tif (err == ENOENT) {\n\t\t\tgoto label_ENOENT;\n\t\t}\n\t\ttest_fail(\"%s(): Error in mallctl(): %s\", __func__,\n\t\t    strerror(err));\n\t}\n\tsz = sizeof(ap0);\n\tif ((err = mallctl(\"thread.allocatedp\", (void *)&ap0, &sz, NULL, 0))) {\n\t\tif (err == ENOENT) {\n\t\t\tgoto label_ENOENT;\n\t\t}\n\t\ttest_fail(\"%s(): Error in mallctl(): %s\", __func__,\n\t\t    strerror(err));\n\t}\n\tassert_u64_eq(*ap0, a0,\n\t    \"\\\"thread.allocatedp\\\" should provide a pointer to internal \"\n\t    \"storage\");\n\n\tsz = sizeof(d0);\n\tif ((err = mallctl(\"thread.deallocated\", (void *)&d0, &sz, NULL, 0))) {\n\t\tif (err == ENOENT) {\n\t\t\tgoto label_ENOENT;\n\t\t}\n\t\ttest_fail(\"%s(): Error in mallctl(): %s\", __func__,\n\t\t    strerror(err));\n\t}\n\tsz = sizeof(dp0);\n\tif ((err = mallctl(\"thread.deallocatedp\", (void *)&dp0, &sz, NULL,\n\t    0))) {\n\t\tif (err == ENOENT) {\n\t\t\tgoto label_ENOENT;\n\t\t}\n\t\ttest_fail(\"%s(): Error in mallctl(): %s\", __func__,\n\t\t    strerror(err));\n\t}\n\tassert_u64_eq(*dp0, d0,\n\t    \"\\\"thread.deallocatedp\\\" should provide a pointer to internal \"\n\t    \"storage\");\n\n\tp = malloc(1);\n\tassert_ptr_not_null(p, \"Unexpected malloc() error\");\n\n\tsz = sizeof(a1);\n\tmallctl(\"thread.allocated\", (void *)&a1, &sz, NULL, 0);\n\tsz = sizeof(ap1);\n\tmallctl(\"thread.allocatedp\", (void *)&ap1, &sz, NULL, 0);\n\tassert_u64_eq(*ap1, a1,\n\t    \"Dereferenced \\\"thread.allocatedp\\\" value should equal \"\n\t    \"\\\"thread.allocated\\\" value\");\n\tassert_ptr_eq(ap0, ap1,\n\t    \"Pointer returned by \\\"thread.allocatedp\\\" should not change\");\n\n\tusize = malloc_usable_size(p);\n\tassert_u64_le(a0 + usize, a1,\n\t    \"Allocated memory counter should increase by at least the amount \"\n\t    \"explicitly allocated\");\n\n\tfree(p);\n\n\tsz = sizeof(d1);\n\tmallctl(\"thread.deallocated\", (void *)&d1, &sz, NULL, 0);\n\tsz = sizeof(dp1);\n\tmallctl(\"thread.deallocatedp\", (void *)&dp1, &sz, NULL, 0);\n\tassert_u64_eq(*dp1, d1,\n\t    \"Dereferenced \\\"thread.deallocatedp\\\" value should equal \"\n\t    \"\\\"thread.deallocated\\\" value\");\n\tassert_ptr_eq(dp0, dp1,\n\t    \"Pointer returned by \\\"thread.deallocatedp\\\" should not change\");\n\n\tassert_u64_le(d0 + usize, d1,\n\t    \"Deallocated memory counter should increase by at least the amount \"\n\t    \"explicitly deallocated\");\n\n\treturn NULL;\nlabel_ENOENT:\n\tassert_false(config_stats,\n\t    \"ENOENT should only be returned if stats are disabled\");\n\ttest_skip(\"\\\"thread.allocated\\\" mallctl not available\");\n\treturn NULL;\n}\n\nTEST_BEGIN(test_main_thread) {\n\tthd_start(NULL);\n}\nTEST_END\n\nTEST_BEGIN(test_subthread) {\n\tthd_t thd;\n\n\tthd_create(&thd, thd_start, NULL);\n\tthd_join(thd, NULL);\n}\nTEST_END\n\nint\nmain(void) {\n\t/* Run tests multiple times to check for bad interactions. */\n\treturn test(\n\t    test_main_thread,\n\t    test_subthread,\n\t    test_main_thread,\n\t    test_subthread,\n\t    test_main_thread);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/integration/cpp/basic.cpp",
    "content": "#include <memory>\n#include \"test/jemalloc_test.h\"\n\nTEST_BEGIN(test_basic) {\n\tauto foo = new long(4);\n\tassert_ptr_not_null(foo, \"Unexpected new[] failure\");\n\tdelete foo;\n\t// Test nullptr handling.\n\tfoo = nullptr;\n\tdelete foo;\n\n\tauto bar = new long;\n\tassert_ptr_not_null(bar, \"Unexpected new failure\");\n\tdelete bar;\n\t// Test nullptr handling.\n\tbar = nullptr;\n\tdelete bar;\n}\nTEST_END\n\nint\nmain() {\n\treturn test(\n\t    test_basic);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/integration/extent.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"test/extent_hooks.h\"\n\nstatic bool\ncheck_background_thread_enabled(void) {\n\tbool enabled;\n\tsize_t sz = sizeof(bool);\n\tint ret = mallctl(\"background_thread\", (void *)&enabled, &sz, NULL,0);\n\tif (ret == ENOENT) {\n\t\treturn false;\n\t}\n\tassert_d_eq(ret, 0, \"Unexpected mallctl error\");\n\treturn enabled;\n}\n\nstatic void\ntest_extent_body(unsigned arena_ind) {\n\tvoid *p;\n\tsize_t large0, large1, large2, sz;\n\tsize_t purge_mib[3];\n\tsize_t purge_miblen;\n\tint flags;\n\tbool xallocx_success_a, xallocx_success_b, xallocx_success_c;\n\n\tflags = MALLOCX_ARENA(arena_ind) | MALLOCX_TCACHE_NONE;\n\n\t/* Get large size classes. */\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctl(\"arenas.lextent.0.size\", (void *)&large0, &sz, NULL,\n\t    0), 0, \"Unexpected arenas.lextent.0.size failure\");\n\tassert_d_eq(mallctl(\"arenas.lextent.1.size\", (void *)&large1, &sz, NULL,\n\t    0), 0, \"Unexpected arenas.lextent.1.size failure\");\n\tassert_d_eq(mallctl(\"arenas.lextent.2.size\", (void *)&large2, &sz, NULL,\n\t    0), 0, \"Unexpected arenas.lextent.2.size failure\");\n\n\t/* Test dalloc/decommit/purge cascade. */\n\tpurge_miblen = sizeof(purge_mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"arena.0.purge\", purge_mib, &purge_miblen),\n\t    0, \"Unexpected mallctlnametomib() failure\");\n\tpurge_mib[1] = (size_t)arena_ind;\n\tcalled_alloc = false;\n\ttry_alloc = true;\n\ttry_dalloc = false;\n\ttry_decommit = false;\n\tp = mallocx(large0 * 2, flags);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\tassert_true(called_alloc, \"Expected alloc call\");\n\tcalled_dalloc = false;\n\tcalled_decommit = false;\n\tdid_purge_lazy = false;\n\tdid_purge_forced = false;\n\tcalled_split = false;\n\txallocx_success_a = (xallocx(p, large0, 0, flags) == large0);\n\tassert_d_eq(mallctlbymib(purge_mib, purge_miblen, NULL, NULL, NULL, 0),\n\t    0, \"Unexpected arena.%u.purge error\", arena_ind);\n\tif (xallocx_success_a) {\n\t\tassert_true(called_dalloc, \"Expected dalloc call\");\n\t\tassert_true(called_decommit, \"Expected decommit call\");\n\t\tassert_true(did_purge_lazy || did_purge_forced,\n\t\t    \"Expected purge\");\n\t}\n\tassert_true(called_split, \"Expected split call\");\n\tdallocx(p, flags);\n\ttry_dalloc = true;\n\n\t/* Test decommit/commit and observe split/merge. */\n\ttry_dalloc = false;\n\ttry_decommit = true;\n\tp = mallocx(large0 * 2, flags);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\tdid_decommit = false;\n\tdid_commit = false;\n\tcalled_split = false;\n\tdid_split = false;\n\tdid_merge = false;\n\txallocx_success_b = (xallocx(p, large0, 0, flags) == large0);\n\tassert_d_eq(mallctlbymib(purge_mib, purge_miblen, NULL, NULL, NULL, 0),\n\t    0, \"Unexpected arena.%u.purge error\", arena_ind);\n\tif (xallocx_success_b) {\n\t\tassert_true(did_split, \"Expected split\");\n\t}\n\txallocx_success_c = (xallocx(p, large0 * 2, 0, flags) == large0 * 2);\n\tif (did_split) {\n\t\tassert_b_eq(did_decommit, did_commit,\n\t\t    \"Expected decommit/commit match\");\n\t}\n\tif (xallocx_success_b && xallocx_success_c) {\n\t\tassert_true(did_merge, \"Expected merge\");\n\t}\n\tdallocx(p, flags);\n\ttry_dalloc = true;\n\ttry_decommit = false;\n\n\t/* Make sure non-large allocation succeeds. */\n\tp = mallocx(42, flags);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\tdallocx(p, flags);\n}\n\nstatic void\ntest_manual_hook_auto_arena(void) {\n\tunsigned narenas;\n\tsize_t old_size, new_size, sz;\n\tsize_t hooks_mib[3];\n\tsize_t hooks_miblen;\n\textent_hooks_t *new_hooks, *old_hooks;\n\n\textent_hooks_prep();\n\n\tsz = sizeof(unsigned);\n\t/* Get number of auto arenas. */\n\tassert_d_eq(mallctl(\"opt.narenas\", (void *)&narenas, &sz, NULL, 0),\n\t    0, \"Unexpected mallctl() failure\");\n\tif (narenas == 1) {\n\t\treturn;\n\t}\n\n\t/* Install custom extent hooks on arena 1 (might not be initialized). */\n\thooks_miblen = sizeof(hooks_mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"arena.0.extent_hooks\", hooks_mib,\n\t    &hooks_miblen), 0, \"Unexpected mallctlnametomib() failure\");\n\thooks_mib[1] = 1;\n\told_size = sizeof(extent_hooks_t *);\n\tnew_hooks = &hooks;\n\tnew_size = sizeof(extent_hooks_t *);\n\tassert_d_eq(mallctlbymib(hooks_mib, hooks_miblen, (void *)&old_hooks,\n\t    &old_size, (void *)&new_hooks, new_size), 0,\n\t    \"Unexpected extent_hooks error\");\n\tstatic bool auto_arena_created = false;\n\tif (old_hooks != &hooks) {\n\t\tassert_b_eq(auto_arena_created, false,\n\t\t    \"Expected auto arena 1 created only once.\");\n\t\tauto_arena_created = true;\n\t}\n}\n\nstatic void\ntest_manual_hook_body(void) {\n\tunsigned arena_ind;\n\tsize_t old_size, new_size, sz;\n\tsize_t hooks_mib[3];\n\tsize_t hooks_miblen;\n\textent_hooks_t *new_hooks, *old_hooks;\n\n\textent_hooks_prep();\n\n\tsz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena_ind, &sz, NULL, 0),\n\t    0, \"Unexpected mallctl() failure\");\n\n\t/* Install custom extent hooks. */\n\thooks_miblen = sizeof(hooks_mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"arena.0.extent_hooks\", hooks_mib,\n\t    &hooks_miblen), 0, \"Unexpected mallctlnametomib() failure\");\n\thooks_mib[1] = (size_t)arena_ind;\n\told_size = sizeof(extent_hooks_t *);\n\tnew_hooks = &hooks;\n\tnew_size = sizeof(extent_hooks_t *);\n\tassert_d_eq(mallctlbymib(hooks_mib, hooks_miblen, (void *)&old_hooks,\n\t    &old_size, (void *)&new_hooks, new_size), 0,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_ne(old_hooks->alloc, extent_alloc_hook,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_ne(old_hooks->dalloc, extent_dalloc_hook,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_ne(old_hooks->commit, extent_commit_hook,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_ne(old_hooks->decommit, extent_decommit_hook,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_ne(old_hooks->purge_lazy, extent_purge_lazy_hook,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_ne(old_hooks->purge_forced, extent_purge_forced_hook,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_ne(old_hooks->split, extent_split_hook,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_ne(old_hooks->merge, extent_merge_hook,\n\t    \"Unexpected extent_hooks error\");\n\n\tif (!check_background_thread_enabled()) {\n\t\ttest_extent_body(arena_ind);\n\t}\n\n\t/* Restore extent hooks. */\n\tassert_d_eq(mallctlbymib(hooks_mib, hooks_miblen, NULL, NULL,\n\t    (void *)&old_hooks, new_size), 0, \"Unexpected extent_hooks error\");\n\tassert_d_eq(mallctlbymib(hooks_mib, hooks_miblen, (void *)&old_hooks,\n\t    &old_size, NULL, 0), 0, \"Unexpected extent_hooks error\");\n\tassert_ptr_eq(old_hooks, default_hooks, \"Unexpected extent_hooks error\");\n\tassert_ptr_eq(old_hooks->alloc, default_hooks->alloc,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_eq(old_hooks->dalloc, default_hooks->dalloc,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_eq(old_hooks->commit, default_hooks->commit,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_eq(old_hooks->decommit, default_hooks->decommit,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_eq(old_hooks->purge_lazy, default_hooks->purge_lazy,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_eq(old_hooks->purge_forced, default_hooks->purge_forced,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_eq(old_hooks->split, default_hooks->split,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_eq(old_hooks->merge, default_hooks->merge,\n\t    \"Unexpected extent_hooks error\");\n}\n\nTEST_BEGIN(test_extent_manual_hook) {\n\ttest_manual_hook_auto_arena();\n\ttest_manual_hook_body();\n\n\t/* Test failure paths. */\n\ttry_split = false;\n\ttest_manual_hook_body();\n\ttry_merge = false;\n\ttest_manual_hook_body();\n\ttry_purge_lazy = false;\n\ttry_purge_forced = false;\n\ttest_manual_hook_body();\n\n\ttry_split = try_merge = try_purge_lazy = try_purge_forced = true;\n}\nTEST_END\n\nTEST_BEGIN(test_extent_auto_hook) {\n\tunsigned arena_ind;\n\tsize_t new_size, sz;\n\textent_hooks_t *new_hooks;\n\n\textent_hooks_prep();\n\n\tsz = sizeof(unsigned);\n\tnew_hooks = &hooks;\n\tnew_size = sizeof(extent_hooks_t *);\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena_ind, &sz,\n\t    (void *)&new_hooks, new_size), 0, \"Unexpected mallctl() failure\");\n\n\ttest_skip_if(check_background_thread_enabled());\n\ttest_extent_body(arena_ind);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_extent_manual_hook,\n\t    test_extent_auto_hook);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/integration/extent.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_fill}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"junk:false\"\nfi\n"
  },
  {
    "path": "deps/jemalloc/test/integration/malloc.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nTEST_BEGIN(test_zero_alloc) {\n\tvoid *res = malloc(0);\n\tassert(res);\n\tsize_t usable = malloc_usable_size(res);\n\tassert(usable > 0);\n\tfree(res);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_zero_alloc);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/integration/mallocx.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic unsigned\nget_nsizes_impl(const char *cmd) {\n\tunsigned ret;\n\tsize_t z;\n\n\tz = sizeof(unsigned);\n\tassert_d_eq(mallctl(cmd, (void *)&ret, &z, NULL, 0), 0,\n\t    \"Unexpected mallctl(\\\"%s\\\", ...) failure\", cmd);\n\n\treturn ret;\n}\n\nstatic unsigned\nget_nlarge(void) {\n\treturn get_nsizes_impl(\"arenas.nlextents\");\n}\n\nstatic size_t\nget_size_impl(const char *cmd, size_t ind) {\n\tsize_t ret;\n\tsize_t z;\n\tsize_t mib[4];\n\tsize_t miblen = 4;\n\n\tz = sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(cmd, mib, &miblen),\n\t    0, \"Unexpected mallctlnametomib(\\\"%s\\\", ...) failure\", cmd);\n\tmib[2] = ind;\n\tz = sizeof(size_t);\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&ret, &z, NULL, 0),\n\t    0, \"Unexpected mallctlbymib([\\\"%s\\\", %zu], ...) failure\", cmd, ind);\n\n\treturn ret;\n}\n\nstatic size_t\nget_large_size(size_t ind) {\n\treturn get_size_impl(\"arenas.lextent.0.size\", ind);\n}\n\n/*\n * On systems which can't merge extents, tests that call this function generate\n * a lot of dirty memory very quickly.  Purging between cycles mitigates\n * potential OOM on e.g. 32-bit Windows.\n */\nstatic void\npurge(void) {\n\tassert_d_eq(mallctl(\"arena.0.purge\", NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctl error\");\n}\n\n/*\n * GCC \"-Walloc-size-larger-than\" warning detects when one of the memory\n * allocation functions is called with a size larger than the maximum size that\n * they support. Here we want to explicitly test that the allocation functions\n * do indeed fail properly when this is the case, which triggers the warning.\n * Therefore we disable the warning for these tests.\n */\nJEMALLOC_DIAGNOSTIC_PUSH\nJEMALLOC_DIAGNOSTIC_IGNORE_ALLOC_SIZE_LARGER_THAN\n\nTEST_BEGIN(test_overflow) {\n\tsize_t largemax;\n\n\tlargemax = get_large_size(get_nlarge()-1);\n\n\tassert_ptr_null(mallocx(largemax+1, 0),\n\t    \"Expected OOM for mallocx(size=%#zx, 0)\", largemax+1);\n\n\tassert_ptr_null(mallocx(ZU(PTRDIFF_MAX)+1, 0),\n\t    \"Expected OOM for mallocx(size=%#zx, 0)\", ZU(PTRDIFF_MAX)+1);\n\n\tassert_ptr_null(mallocx(SIZE_T_MAX, 0),\n\t    \"Expected OOM for mallocx(size=%#zx, 0)\", SIZE_T_MAX);\n\n\tassert_ptr_null(mallocx(1, MALLOCX_ALIGN(ZU(PTRDIFF_MAX)+1)),\n\t    \"Expected OOM for mallocx(size=1, MALLOCX_ALIGN(%#zx))\",\n\t    ZU(PTRDIFF_MAX)+1);\n}\nTEST_END\n\nstatic void *\nremote_alloc(void *arg) {\n\tunsigned arena;\n\tsize_t sz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\tsize_t large_sz;\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctl(\"arenas.lextent.0.size\", (void *)&large_sz, &sz,\n\t    NULL, 0), 0, \"Unexpected mallctl failure\");\n\n\tvoid *ptr = mallocx(large_sz, MALLOCX_ARENA(arena)\n\t    | MALLOCX_TCACHE_NONE);\n\tvoid **ret = (void **)arg;\n\t*ret = ptr;\n\n\treturn NULL;\n}\n\nTEST_BEGIN(test_remote_free) {\n\tthd_t thd;\n\tvoid *ret;\n\tthd_create(&thd, remote_alloc, (void *)&ret);\n\tthd_join(thd, NULL);\n\tassert_ptr_not_null(ret, \"Unexpected mallocx failure\");\n\n\t/* Avoid TCACHE_NONE to explicitly test tcache_flush(). */\n\tdallocx(ret, 0);\n\tmallctl(\"thread.tcache.flush\", NULL, NULL, NULL, 0);\n}\nTEST_END\n\nTEST_BEGIN(test_oom) {\n\tsize_t largemax;\n\tbool oom;\n\tvoid *ptrs[3];\n\tunsigned i;\n\n\t/*\n\t * It should be impossible to allocate three objects that each consume\n\t * nearly half the virtual address space.\n\t */\n\tlargemax = get_large_size(get_nlarge()-1);\n\toom = false;\n\tfor (i = 0; i < sizeof(ptrs) / sizeof(void *); i++) {\n\t\tptrs[i] = mallocx(largemax, MALLOCX_ARENA(0));\n\t\tif (ptrs[i] == NULL) {\n\t\t\toom = true;\n\t\t}\n\t}\n\tassert_true(oom,\n\t    \"Expected OOM during series of calls to mallocx(size=%zu, 0)\",\n\t    largemax);\n\tfor (i = 0; i < sizeof(ptrs) / sizeof(void *); i++) {\n\t\tif (ptrs[i] != NULL) {\n\t\t\tdallocx(ptrs[i], 0);\n\t\t}\n\t}\n\tpurge();\n\n#if LG_SIZEOF_PTR == 3\n\tassert_ptr_null(mallocx(0x8000000000000000ULL,\n\t    MALLOCX_ALIGN(0x8000000000000000ULL)),\n\t    \"Expected OOM for mallocx()\");\n\tassert_ptr_null(mallocx(0x8000000000000000ULL,\n\t    MALLOCX_ALIGN(0x80000000)),\n\t    \"Expected OOM for mallocx()\");\n#else\n\tassert_ptr_null(mallocx(0x80000000UL, MALLOCX_ALIGN(0x80000000UL)),\n\t    \"Expected OOM for mallocx()\");\n#endif\n}\nTEST_END\n\n/* Re-enable the \"-Walloc-size-larger-than=\" warning */\nJEMALLOC_DIAGNOSTIC_POP\n\nTEST_BEGIN(test_basic) {\n#define MAXSZ (((size_t)1) << 23)\n\tsize_t sz;\n\n\tfor (sz = 1; sz < MAXSZ; sz = nallocx(sz, 0) + 1) {\n\t\tsize_t nsz, rsz;\n\t\tvoid *p;\n\t\tnsz = nallocx(sz, 0);\n\t\tassert_zu_ne(nsz, 0, \"Unexpected nallocx() error\");\n\t\tp = mallocx(sz, 0);\n\t\tassert_ptr_not_null(p,\n\t\t    \"Unexpected mallocx(size=%zx, flags=0) error\", sz);\n\t\trsz = sallocx(p, 0);\n\t\tassert_zu_ge(rsz, sz, \"Real size smaller than expected\");\n\t\tassert_zu_eq(nsz, rsz, \"nallocx()/sallocx() size mismatch\");\n\t\tdallocx(p, 0);\n\n\t\tp = mallocx(sz, 0);\n\t\tassert_ptr_not_null(p,\n\t\t    \"Unexpected mallocx(size=%zx, flags=0) error\", sz);\n\t\tdallocx(p, 0);\n\n\t\tnsz = nallocx(sz, MALLOCX_ZERO);\n\t\tassert_zu_ne(nsz, 0, \"Unexpected nallocx() error\");\n\t\tp = mallocx(sz, MALLOCX_ZERO);\n\t\tassert_ptr_not_null(p,\n\t\t    \"Unexpected mallocx(size=%zx, flags=MALLOCX_ZERO) error\",\n\t\t    nsz);\n\t\trsz = sallocx(p, 0);\n\t\tassert_zu_eq(nsz, rsz, \"nallocx()/sallocx() rsize mismatch\");\n\t\tdallocx(p, 0);\n\t\tpurge();\n\t}\n#undef MAXSZ\n}\nTEST_END\n\nTEST_BEGIN(test_alignment_and_size) {\n\tconst char *percpu_arena;\n\tsize_t sz = sizeof(percpu_arena);\n\n\tif(mallctl(\"opt.percpu_arena\", (void *)&percpu_arena, &sz, NULL, 0) ||\n\t    strcmp(percpu_arena, \"disabled\") != 0) {\n\t\ttest_skip(\"test_alignment_and_size skipped: \"\n\t\t    \"not working with percpu arena.\");\n\t};\n#define MAXALIGN (((size_t)1) << 23)\n#define NITER 4\n\tsize_t nsz, rsz, alignment, total;\n\tunsigned i;\n\tvoid *ps[NITER];\n\n\tfor (i = 0; i < NITER; i++) {\n\t\tps[i] = NULL;\n\t}\n\n\tfor (alignment = 8;\n\t    alignment <= MAXALIGN;\n\t    alignment <<= 1) {\n\t\ttotal = 0;\n\t\tfor (sz = 1;\n\t\t    sz < 3 * alignment && sz < (1U << 31);\n\t\t    sz += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {\n\t\t\tfor (i = 0; i < NITER; i++) {\n\t\t\t\tnsz = nallocx(sz, MALLOCX_ALIGN(alignment) |\n\t\t\t\t    MALLOCX_ZERO | MALLOCX_ARENA(0));\n\t\t\t\tassert_zu_ne(nsz, 0,\n\t\t\t\t    \"nallocx() error for alignment=%zu, \"\n\t\t\t\t    \"size=%zu (%#zx)\", alignment, sz, sz);\n\t\t\t\tps[i] = mallocx(sz, MALLOCX_ALIGN(alignment) |\n\t\t\t\t    MALLOCX_ZERO | MALLOCX_ARENA(0));\n\t\t\t\tassert_ptr_not_null(ps[i],\n\t\t\t\t    \"mallocx() error for alignment=%zu, \"\n\t\t\t\t    \"size=%zu (%#zx)\", alignment, sz, sz);\n\t\t\t\trsz = sallocx(ps[i], 0);\n\t\t\t\tassert_zu_ge(rsz, sz,\n\t\t\t\t    \"Real size smaller than expected for \"\n\t\t\t\t    \"alignment=%zu, size=%zu\", alignment, sz);\n\t\t\t\tassert_zu_eq(nsz, rsz,\n\t\t\t\t    \"nallocx()/sallocx() size mismatch for \"\n\t\t\t\t    \"alignment=%zu, size=%zu\", alignment, sz);\n\t\t\t\tassert_ptr_null(\n\t\t\t\t    (void *)((uintptr_t)ps[i] & (alignment-1)),\n\t\t\t\t    \"%p inadequately aligned for\"\n\t\t\t\t    \" alignment=%zu, size=%zu\", ps[i],\n\t\t\t\t    alignment, sz);\n\t\t\t\ttotal += rsz;\n\t\t\t\tif (total >= (MAXALIGN << 1)) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (i = 0; i < NITER; i++) {\n\t\t\t\tif (ps[i] != NULL) {\n\t\t\t\t\tdallocx(ps[i], 0);\n\t\t\t\t\tps[i] = NULL;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tpurge();\n\t}\n#undef MAXALIGN\n#undef NITER\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_overflow,\n\t    test_oom,\n\t    test_remote_free,\n\t    test_basic,\n\t    test_alignment_and_size);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/integration/mallocx.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_fill}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"junk:false\"\nfi\n"
  },
  {
    "path": "deps/jemalloc/test/integration/overflow.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n/*\n * GCC \"-Walloc-size-larger-than\" warning detects when one of the memory\n * allocation functions is called with a size larger than the maximum size that\n * they support. Here we want to explicitly test that the allocation functions\n * do indeed fail properly when this is the case, which triggers the warning.\n * Therefore we disable the warning for these tests.\n */\nJEMALLOC_DIAGNOSTIC_PUSH\nJEMALLOC_DIAGNOSTIC_IGNORE_ALLOC_SIZE_LARGER_THAN\n\nTEST_BEGIN(test_overflow) {\n\tunsigned nlextents;\n\tsize_t mib[4];\n\tsize_t sz, miblen, max_size_class;\n\tvoid *p;\n\n\tsz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.nlextents\", (void *)&nlextents, &sz, NULL,\n\t    0), 0, \"Unexpected mallctl() error\");\n\n\tmiblen = sizeof(mib) / sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"arenas.lextent.0.size\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() error\");\n\tmib[2] = nlextents - 1;\n\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&max_size_class, &sz,\n\t    NULL, 0), 0, \"Unexpected mallctlbymib() error\");\n\n\tassert_ptr_null(malloc(max_size_class + 1),\n\t    \"Expected OOM due to over-sized allocation request\");\n\tassert_ptr_null(malloc(SIZE_T_MAX),\n\t    \"Expected OOM due to over-sized allocation request\");\n\n\tassert_ptr_null(calloc(1, max_size_class + 1),\n\t    \"Expected OOM due to over-sized allocation request\");\n\tassert_ptr_null(calloc(1, SIZE_T_MAX),\n\t    \"Expected OOM due to over-sized allocation request\");\n\n\tp = malloc(1);\n\tassert_ptr_not_null(p, \"Unexpected malloc() OOM\");\n\tassert_ptr_null(realloc(p, max_size_class + 1),\n\t    \"Expected OOM due to over-sized allocation request\");\n\tassert_ptr_null(realloc(p, SIZE_T_MAX),\n\t    \"Expected OOM due to over-sized allocation request\");\n\tfree(p);\n}\nTEST_END\n\n/* Re-enable the \"-Walloc-size-larger-than=\" warning */\nJEMALLOC_DIAGNOSTIC_POP\n\nint\nmain(void) {\n\treturn test(\n\t    test_overflow);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/integration/posix_memalign.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#define MAXALIGN (((size_t)1) << 23)\n\n/*\n * On systems which can't merge extents, tests that call this function generate\n * a lot of dirty memory very quickly.  Purging between cycles mitigates\n * potential OOM on e.g. 32-bit Windows.\n */\nstatic void\npurge(void) {\n\tassert_d_eq(mallctl(\"arena.0.purge\", NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctl error\");\n}\n\nTEST_BEGIN(test_alignment_errors) {\n\tsize_t alignment;\n\tvoid *p;\n\n\tfor (alignment = 0; alignment < sizeof(void *); alignment++) {\n\t\tassert_d_eq(posix_memalign(&p, alignment, 1), EINVAL,\n\t\t    \"Expected error for invalid alignment %zu\",\n\t\t    alignment);\n\t}\n\n\tfor (alignment = sizeof(size_t); alignment < MAXALIGN;\n\t    alignment <<= 1) {\n\t\tassert_d_ne(posix_memalign(&p, alignment + 1, 1), 0,\n\t\t    \"Expected error for invalid alignment %zu\",\n\t\t    alignment + 1);\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_oom_errors) {\n\tsize_t alignment, size;\n\tvoid *p;\n\n#if LG_SIZEOF_PTR == 3\n\talignment = UINT64_C(0x8000000000000000);\n\tsize      = UINT64_C(0x8000000000000000);\n#else\n\talignment = 0x80000000LU;\n\tsize      = 0x80000000LU;\n#endif\n\tassert_d_ne(posix_memalign(&p, alignment, size), 0,\n\t    \"Expected error for posix_memalign(&p, %zu, %zu)\",\n\t    alignment, size);\n\n#if LG_SIZEOF_PTR == 3\n\talignment = UINT64_C(0x4000000000000000);\n\tsize      = UINT64_C(0xc000000000000001);\n#else\n\talignment = 0x40000000LU;\n\tsize      = 0xc0000001LU;\n#endif\n\tassert_d_ne(posix_memalign(&p, alignment, size), 0,\n\t    \"Expected error for posix_memalign(&p, %zu, %zu)\",\n\t    alignment, size);\n\n\talignment = 0x10LU;\n#if LG_SIZEOF_PTR == 3\n\tsize = UINT64_C(0xfffffffffffffff0);\n#else\n\tsize = 0xfffffff0LU;\n#endif\n\tassert_d_ne(posix_memalign(&p, alignment, size), 0,\n\t    \"Expected error for posix_memalign(&p, %zu, %zu)\",\n\t    alignment, size);\n}\nTEST_END\n\nTEST_BEGIN(test_alignment_and_size) {\n#define NITER 4\n\tsize_t alignment, size, total;\n\tunsigned i;\n\tint err;\n\tvoid *ps[NITER];\n\n\tfor (i = 0; i < NITER; i++) {\n\t\tps[i] = NULL;\n\t}\n\n\tfor (alignment = 8;\n\t    alignment <= MAXALIGN;\n\t    alignment <<= 1) {\n\t\ttotal = 0;\n\t\tfor (size = 0;\n\t\t    size < 3 * alignment && size < (1U << 31);\n\t\t    size += ((size == 0) ? 1 :\n\t\t    (alignment >> (LG_SIZEOF_PTR-1)) - 1)) {\n\t\t\tfor (i = 0; i < NITER; i++) {\n\t\t\t\terr = posix_memalign(&ps[i],\n\t\t\t\t    alignment, size);\n\t\t\t\tif (err) {\n\t\t\t\t\tchar buf[BUFERROR_BUF];\n\n\t\t\t\t\tbuferror(get_errno(), buf, sizeof(buf));\n\t\t\t\t\ttest_fail(\n\t\t\t\t\t    \"Error for alignment=%zu, \"\n\t\t\t\t\t    \"size=%zu (%#zx): %s\",\n\t\t\t\t\t    alignment, size, size, buf);\n\t\t\t\t}\n\t\t\t\ttotal += malloc_usable_size(ps[i]);\n\t\t\t\tif (total >= (MAXALIGN << 1)) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (i = 0; i < NITER; i++) {\n\t\t\t\tif (ps[i] != NULL) {\n\t\t\t\t\tfree(ps[i]);\n\t\t\t\t\tps[i] = NULL;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tpurge();\n\t}\n#undef NITER\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_alignment_errors,\n\t    test_oom_errors,\n\t    test_alignment_and_size);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/integration/rallocx.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic unsigned\nget_nsizes_impl(const char *cmd) {\n\tunsigned ret;\n\tsize_t z;\n\n\tz = sizeof(unsigned);\n\tassert_d_eq(mallctl(cmd, (void *)&ret, &z, NULL, 0), 0,\n\t    \"Unexpected mallctl(\\\"%s\\\", ...) failure\", cmd);\n\n\treturn ret;\n}\n\nstatic unsigned\nget_nlarge(void) {\n\treturn get_nsizes_impl(\"arenas.nlextents\");\n}\n\nstatic size_t\nget_size_impl(const char *cmd, size_t ind) {\n\tsize_t ret;\n\tsize_t z;\n\tsize_t mib[4];\n\tsize_t miblen = 4;\n\n\tz = sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(cmd, mib, &miblen),\n\t    0, \"Unexpected mallctlnametomib(\\\"%s\\\", ...) failure\", cmd);\n\tmib[2] = ind;\n\tz = sizeof(size_t);\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&ret, &z, NULL, 0),\n\t    0, \"Unexpected mallctlbymib([\\\"%s\\\", %zu], ...) failure\", cmd, ind);\n\n\treturn ret;\n}\n\nstatic size_t\nget_large_size(size_t ind) {\n\treturn get_size_impl(\"arenas.lextent.0.size\", ind);\n}\n\nTEST_BEGIN(test_grow_and_shrink) {\n\tvoid *p, *q;\n\tsize_t tsz;\n#define NCYCLES 3\n\tunsigned i, j;\n#define NSZS 1024\n\tsize_t szs[NSZS];\n#define MAXSZ ZU(12 * 1024 * 1024)\n\n\tp = mallocx(1, 0);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\tszs[0] = sallocx(p, 0);\n\n\tfor (i = 0; i < NCYCLES; i++) {\n\t\tfor (j = 1; j < NSZS && szs[j-1] < MAXSZ; j++) {\n\t\t\tq = rallocx(p, szs[j-1]+1, 0);\n\t\t\tassert_ptr_not_null(q,\n\t\t\t    \"Unexpected rallocx() error for size=%zu-->%zu\",\n\t\t\t    szs[j-1], szs[j-1]+1);\n\t\t\tszs[j] = sallocx(q, 0);\n\t\t\tassert_zu_ne(szs[j], szs[j-1]+1,\n\t\t\t    \"Expected size to be at least: %zu\", szs[j-1]+1);\n\t\t\tp = q;\n\t\t}\n\n\t\tfor (j--; j > 0; j--) {\n\t\t\tq = rallocx(p, szs[j-1], 0);\n\t\t\tassert_ptr_not_null(q,\n\t\t\t    \"Unexpected rallocx() error for size=%zu-->%zu\",\n\t\t\t    szs[j], szs[j-1]);\n\t\t\ttsz = sallocx(q, 0);\n\t\t\tassert_zu_eq(tsz, szs[j-1],\n\t\t\t    \"Expected size=%zu, got size=%zu\", szs[j-1], tsz);\n\t\t\tp = q;\n\t\t}\n\t}\n\n\tdallocx(p, 0);\n#undef MAXSZ\n#undef NSZS\n#undef NCYCLES\n}\nTEST_END\n\nstatic bool\nvalidate_fill(const void *p, uint8_t c, size_t offset, size_t len) {\n\tbool ret = false;\n\tconst uint8_t *buf = (const uint8_t *)p;\n\tsize_t i;\n\n\tfor (i = 0; i < len; i++) {\n\t\tuint8_t b = buf[offset+i];\n\t\tif (b != c) {\n\t\t\ttest_fail(\"Allocation at %p (len=%zu) contains %#x \"\n\t\t\t    \"rather than %#x at offset %zu\", p, len, b, c,\n\t\t\t    offset+i);\n\t\t\tret = true;\n\t\t}\n\t}\n\n\treturn ret;\n}\n\nTEST_BEGIN(test_zero) {\n\tvoid *p, *q;\n\tsize_t psz, qsz, i, j;\n\tsize_t start_sizes[] = {1, 3*1024, 63*1024, 4095*1024};\n#define FILL_BYTE 0xaaU\n#define RANGE 2048\n\n\tfor (i = 0; i < sizeof(start_sizes)/sizeof(size_t); i++) {\n\t\tsize_t start_size = start_sizes[i];\n\t\tp = mallocx(start_size, MALLOCX_ZERO);\n\t\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\t\tpsz = sallocx(p, 0);\n\n\t\tassert_false(validate_fill(p, 0, 0, psz),\n\t\t    \"Expected zeroed memory\");\n\t\tmemset(p, FILL_BYTE, psz);\n\t\tassert_false(validate_fill(p, FILL_BYTE, 0, psz),\n\t\t    \"Expected filled memory\");\n\n\t\tfor (j = 1; j < RANGE; j++) {\n\t\t\tq = rallocx(p, start_size+j, MALLOCX_ZERO);\n\t\t\tassert_ptr_not_null(q, \"Unexpected rallocx() error\");\n\t\t\tqsz = sallocx(q, 0);\n\t\t\tif (q != p || qsz != psz) {\n\t\t\t\tassert_false(validate_fill(q, FILL_BYTE, 0,\n\t\t\t\t    psz), \"Expected filled memory\");\n\t\t\t\tassert_false(validate_fill(q, 0, psz, qsz-psz),\n\t\t\t\t    \"Expected zeroed memory\");\n\t\t\t}\n\t\t\tif (psz != qsz) {\n\t\t\t\tmemset((void *)((uintptr_t)q+psz), FILL_BYTE,\n\t\t\t\t    qsz-psz);\n\t\t\t\tpsz = qsz;\n\t\t\t}\n\t\t\tp = q;\n\t\t}\n\t\tassert_false(validate_fill(p, FILL_BYTE, 0, psz),\n\t\t    \"Expected filled memory\");\n\t\tdallocx(p, 0);\n\t}\n#undef FILL_BYTE\n}\nTEST_END\n\nTEST_BEGIN(test_align) {\n\tvoid *p, *q;\n\tsize_t align;\n#define MAX_ALIGN (ZU(1) << 25)\n\n\talign = ZU(1);\n\tp = mallocx(1, MALLOCX_ALIGN(align));\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\n\tfor (align <<= 1; align <= MAX_ALIGN; align <<= 1) {\n\t\tq = rallocx(p, 1, MALLOCX_ALIGN(align));\n\t\tassert_ptr_not_null(q,\n\t\t    \"Unexpected rallocx() error for align=%zu\", align);\n\t\tassert_ptr_null(\n\t\t    (void *)((uintptr_t)q & (align-1)),\n\t\t    \"%p inadequately aligned for align=%zu\",\n\t\t    q, align);\n\t\tp = q;\n\t}\n\tdallocx(p, 0);\n#undef MAX_ALIGN\n}\nTEST_END\n\nTEST_BEGIN(test_lg_align_and_zero) {\n\tvoid *p, *q;\n\tunsigned lg_align;\n\tsize_t sz;\n#define MAX_LG_ALIGN 25\n#define MAX_VALIDATE (ZU(1) << 22)\n\n\tlg_align = 0;\n\tp = mallocx(1, MALLOCX_LG_ALIGN(lg_align)|MALLOCX_ZERO);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\n\tfor (lg_align++; lg_align <= MAX_LG_ALIGN; lg_align++) {\n\t\tq = rallocx(p, 1, MALLOCX_LG_ALIGN(lg_align)|MALLOCX_ZERO);\n\t\tassert_ptr_not_null(q,\n\t\t    \"Unexpected rallocx() error for lg_align=%u\", lg_align);\n\t\tassert_ptr_null(\n\t\t    (void *)((uintptr_t)q & ((ZU(1) << lg_align)-1)),\n\t\t    \"%p inadequately aligned for lg_align=%u\", q, lg_align);\n\t\tsz = sallocx(q, 0);\n\t\tif ((sz << 1) <= MAX_VALIDATE) {\n\t\t\tassert_false(validate_fill(q, 0, 0, sz),\n\t\t\t    \"Expected zeroed memory\");\n\t\t} else {\n\t\t\tassert_false(validate_fill(q, 0, 0, MAX_VALIDATE),\n\t\t\t    \"Expected zeroed memory\");\n\t\t\tassert_false(validate_fill(\n\t\t\t    (void *)((uintptr_t)q+sz-MAX_VALIDATE),\n\t\t\t    0, 0, MAX_VALIDATE), \"Expected zeroed memory\");\n\t\t}\n\t\tp = q;\n\t}\n\tdallocx(p, 0);\n#undef MAX_VALIDATE\n#undef MAX_LG_ALIGN\n}\nTEST_END\n\n/*\n * GCC \"-Walloc-size-larger-than\" warning detects when one of the memory\n * allocation functions is called with a size larger than the maximum size that\n * they support. Here we want to explicitly test that the allocation functions\n * do indeed fail properly when this is the case, which triggers the warning.\n * Therefore we disable the warning for these tests.\n */\nJEMALLOC_DIAGNOSTIC_PUSH\nJEMALLOC_DIAGNOSTIC_IGNORE_ALLOC_SIZE_LARGER_THAN\n\nTEST_BEGIN(test_overflow) {\n\tsize_t largemax;\n\tvoid *p;\n\n\tlargemax = get_large_size(get_nlarge()-1);\n\n\tp = mallocx(1, 0);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\n\tassert_ptr_null(rallocx(p, largemax+1, 0),\n\t    \"Expected OOM for rallocx(p, size=%#zx, 0)\", largemax+1);\n\n\tassert_ptr_null(rallocx(p, ZU(PTRDIFF_MAX)+1, 0),\n\t    \"Expected OOM for rallocx(p, size=%#zx, 0)\", ZU(PTRDIFF_MAX)+1);\n\n\tassert_ptr_null(rallocx(p, SIZE_T_MAX, 0),\n\t    \"Expected OOM for rallocx(p, size=%#zx, 0)\", SIZE_T_MAX);\n\n\tassert_ptr_null(rallocx(p, 1, MALLOCX_ALIGN(ZU(PTRDIFF_MAX)+1)),\n\t    \"Expected OOM for rallocx(p, size=1, MALLOCX_ALIGN(%#zx))\",\n\t    ZU(PTRDIFF_MAX)+1);\n\n\tdallocx(p, 0);\n}\nTEST_END\n\n/* Re-enable the \"-Walloc-size-larger-than=\" warning */\nJEMALLOC_DIAGNOSTIC_POP\n\nint\nmain(void) {\n\treturn test(\n\t    test_grow_and_shrink,\n\t    test_zero,\n\t    test_align,\n\t    test_lg_align_and_zero,\n\t    test_overflow);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/integration/sdallocx.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#define MAXALIGN (((size_t)1) << 22)\n#define NITER 3\n\nTEST_BEGIN(test_basic) {\n\tvoid *ptr = mallocx(64, 0);\n\tsdallocx(ptr, 64, 0);\n}\nTEST_END\n\nTEST_BEGIN(test_alignment_and_size) {\n\tsize_t nsz, sz, alignment, total;\n\tunsigned i;\n\tvoid *ps[NITER];\n\n\tfor (i = 0; i < NITER; i++) {\n\t\tps[i] = NULL;\n\t}\n\n\tfor (alignment = 8;\n\t    alignment <= MAXALIGN;\n\t    alignment <<= 1) {\n\t\ttotal = 0;\n\t\tfor (sz = 1;\n\t\t    sz < 3 * alignment && sz < (1U << 31);\n\t\t    sz += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {\n\t\t\tfor (i = 0; i < NITER; i++) {\n\t\t\t\tnsz = nallocx(sz, MALLOCX_ALIGN(alignment) |\n\t\t\t\t    MALLOCX_ZERO);\n\t\t\t\tps[i] = mallocx(sz, MALLOCX_ALIGN(alignment) |\n\t\t\t\t    MALLOCX_ZERO);\n\t\t\t\ttotal += nsz;\n\t\t\t\tif (total >= (MAXALIGN << 1)) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (i = 0; i < NITER; i++) {\n\t\t\t\tif (ps[i] != NULL) {\n\t\t\t\t\tsdallocx(ps[i], sz,\n\t\t\t\t\t    MALLOCX_ALIGN(alignment));\n\t\t\t\t\tps[i] = NULL;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test_no_reentrancy(\n\t    test_basic,\n\t    test_alignment_and_size);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/integration/slab_sizes.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n/* Note that this test relies on the unusual slab sizes set in slab_sizes.sh. */\n\nTEST_BEGIN(test_slab_sizes) {\n\tunsigned nbins;\n\tsize_t page;\n\tsize_t sizemib[4];\n\tsize_t slabmib[4];\n\tsize_t len;\n\n\tlen = sizeof(nbins);\n\tassert_d_eq(mallctl(\"arenas.nbins\", &nbins, &len, NULL, 0), 0,\n\t    \"nbins mallctl failure\");\n\n\tlen = sizeof(page);\n\tassert_d_eq(mallctl(\"arenas.page\", &page, &len, NULL, 0), 0,\n\t    \"page mallctl failure\");\n\n\tlen = 4;\n\tassert_d_eq(mallctlnametomib(\"arenas.bin.0.size\", sizemib, &len), 0,\n\t    \"bin size mallctlnametomib failure\");\n\n\tlen = 4;\n\tassert_d_eq(mallctlnametomib(\"arenas.bin.0.slab_size\", slabmib, &len),\n\t    0, \"slab size mallctlnametomib failure\");\n\n\tsize_t biggest_slab_seen = 0;\n\n\tfor (unsigned i = 0; i < nbins; i++) {\n\t\tsize_t bin_size;\n\t\tsize_t slab_size;\n\t\tlen = sizeof(size_t);\n\t\tsizemib[2] = i;\n\t\tslabmib[2] = i;\n\t\tassert_d_eq(mallctlbymib(sizemib, 4, (void *)&bin_size, &len,\n\t\t    NULL, 0), 0, \"bin size mallctlbymib failure\");\n\n\t\tlen = sizeof(size_t);\n\t\tassert_d_eq(mallctlbymib(slabmib, 4, (void *)&slab_size, &len,\n\t\t    NULL, 0), 0, \"slab size mallctlbymib failure\");\n\n\t\tif (bin_size < 100) {\n\t\t\t/*\n\t\t\t * Then we should be as close to 17 as possible.  Since\n\t\t\t * not all page sizes are valid (because of bitmap\n\t\t\t * limitations on the number of items in a slab), we\n\t\t\t * should at least make sure that the number of pages\n\t\t\t * goes up.\n\t\t\t */\n\t\t\tassert_zu_ge(slab_size, biggest_slab_seen,\n\t\t\t    \"Slab sizes should go up\");\n\t\t\tbiggest_slab_seen = slab_size;\n\t\t} else if (\n\t\t    (100 <= bin_size && bin_size < 128)\n\t\t    || (128 < bin_size && bin_size <= 200)) {\n\t\t\tassert_zu_eq(slab_size, page,\n\t\t\t    \"Forced-small slabs should be small\");\n\t\t} else if (bin_size == 128) {\n\t\t\tassert_zu_eq(slab_size, 2 * page,\n\t\t\t    \"Forced-2-page slab should be 2 pages\");\n\t\t} else if (200 < bin_size && bin_size <= 4096) {\n\t\t\tassert_zu_ge(slab_size, biggest_slab_seen,\n\t\t\t    \"Slab sizes should go up\");\n\t\t\tbiggest_slab_seen = slab_size;\n\t\t}\n\t}\n\t/*\n\t * For any reasonable configuration, 17 pages should be a valid slab\n\t * size for 4096-byte items.\n\t */\n\tassert_zu_eq(biggest_slab_seen, 17 * page, \"Didn't hit page target\");\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_slab_sizes);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/integration/slab_sizes.sh",
    "content": "#!/bin/sh\n\n# Some screwy-looking slab sizes.\nexport MALLOC_CONF=\"slab_sizes:1-4096:17|100-200:1|128-128:2\"\n"
  },
  {
    "path": "deps/jemalloc/test/integration/smallocx.c",
    "content": "#include \"test/jemalloc_test.h\"\n#include \"jemalloc/jemalloc_macros.h\"\n\n#define STR_HELPER(x) #x\n#define STR(x) STR_HELPER(x)\n\n#ifndef JEMALLOC_VERSION_GID_IDENT\n  #error \"JEMALLOC_VERSION_GID_IDENT not defined\"\n#endif\n\n#define JOIN(x, y) x ## y\n#define JOIN2(x, y) JOIN(x, y)\n#define smallocx JOIN2(smallocx_, JEMALLOC_VERSION_GID_IDENT)\n\ntypedef struct {\n\tvoid *ptr;\n\tsize_t size;\n} smallocx_return_t;\n\nextern smallocx_return_t\nsmallocx(size_t size, int flags);\n\nstatic unsigned\nget_nsizes_impl(const char *cmd) {\n\tunsigned ret;\n\tsize_t z;\n\n\tz = sizeof(unsigned);\n\tassert_d_eq(mallctl(cmd, (void *)&ret, &z, NULL, 0), 0,\n\t    \"Unexpected mallctl(\\\"%s\\\", ...) failure\", cmd);\n\n\treturn ret;\n}\n\nstatic unsigned\nget_nlarge(void) {\n\treturn get_nsizes_impl(\"arenas.nlextents\");\n}\n\nstatic size_t\nget_size_impl(const char *cmd, size_t ind) {\n\tsize_t ret;\n\tsize_t z;\n\tsize_t mib[4];\n\tsize_t miblen = 4;\n\n\tz = sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(cmd, mib, &miblen),\n\t    0, \"Unexpected mallctlnametomib(\\\"%s\\\", ...) failure\", cmd);\n\tmib[2] = ind;\n\tz = sizeof(size_t);\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&ret, &z, NULL, 0),\n\t    0, \"Unexpected mallctlbymib([\\\"%s\\\", %zu], ...) failure\", cmd, ind);\n\n\treturn ret;\n}\n\nstatic size_t\nget_large_size(size_t ind) {\n\treturn get_size_impl(\"arenas.lextent.0.size\", ind);\n}\n\n/*\n * On systems which can't merge extents, tests that call this function generate\n * a lot of dirty memory very quickly.  Purging between cycles mitigates\n * potential OOM on e.g. 32-bit Windows.\n */\nstatic void\npurge(void) {\n\tassert_d_eq(mallctl(\"arena.0.purge\", NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctl error\");\n}\n\n/*\n * GCC \"-Walloc-size-larger-than\" warning detects when one of the memory\n * allocation functions is called with a size larger than the maximum size that\n * they support. Here we want to explicitly test that the allocation functions\n * do indeed fail properly when this is the case, which triggers the warning.\n * Therefore we disable the warning for these tests.\n */\nJEMALLOC_DIAGNOSTIC_PUSH\nJEMALLOC_DIAGNOSTIC_IGNORE_ALLOC_SIZE_LARGER_THAN\n\nTEST_BEGIN(test_overflow) {\n\tsize_t largemax;\n\n\tlargemax = get_large_size(get_nlarge()-1);\n\n\tassert_ptr_null(smallocx(largemax+1, 0).ptr,\n\t    \"Expected OOM for smallocx(size=%#zx, 0)\", largemax+1);\n\n\tassert_ptr_null(smallocx(ZU(PTRDIFF_MAX)+1, 0).ptr,\n\t    \"Expected OOM for smallocx(size=%#zx, 0)\", ZU(PTRDIFF_MAX)+1);\n\n\tassert_ptr_null(smallocx(SIZE_T_MAX, 0).ptr,\n\t    \"Expected OOM for smallocx(size=%#zx, 0)\", SIZE_T_MAX);\n\n\tassert_ptr_null(smallocx(1, MALLOCX_ALIGN(ZU(PTRDIFF_MAX)+1)).ptr,\n\t    \"Expected OOM for smallocx(size=1, MALLOCX_ALIGN(%#zx))\",\n\t    ZU(PTRDIFF_MAX)+1);\n}\nTEST_END\n\nstatic void *\nremote_alloc(void *arg) {\n\tunsigned arena;\n\tsize_t sz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\tsize_t large_sz;\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctl(\"arenas.lextent.0.size\", (void *)&large_sz, &sz,\n\t    NULL, 0), 0, \"Unexpected mallctl failure\");\n\n\tsmallocx_return_t r\n\t    = smallocx(large_sz, MALLOCX_ARENA(arena) | MALLOCX_TCACHE_NONE);\n\tvoid *ptr = r.ptr;\n\tassert_zu_eq(r.size,\n\t    nallocx(large_sz, MALLOCX_ARENA(arena) | MALLOCX_TCACHE_NONE),\n\t    \"Expected smalloc(size,flags).size == nallocx(size,flags)\");\n\tvoid **ret = (void **)arg;\n\t*ret = ptr;\n\n\treturn NULL;\n}\n\nTEST_BEGIN(test_remote_free) {\n\tthd_t thd;\n\tvoid *ret;\n\tthd_create(&thd, remote_alloc, (void *)&ret);\n\tthd_join(thd, NULL);\n\tassert_ptr_not_null(ret, \"Unexpected smallocx failure\");\n\n\t/* Avoid TCACHE_NONE to explicitly test tcache_flush(). */\n\tdallocx(ret, 0);\n\tmallctl(\"thread.tcache.flush\", NULL, NULL, NULL, 0);\n}\nTEST_END\n\nTEST_BEGIN(test_oom) {\n\tsize_t largemax;\n\tbool oom;\n\tvoid *ptrs[3];\n\tunsigned i;\n\n\t/*\n\t * It should be impossible to allocate three objects that each consume\n\t * nearly half the virtual address space.\n\t */\n\tlargemax = get_large_size(get_nlarge()-1);\n\toom = false;\n\tfor (i = 0; i < sizeof(ptrs) / sizeof(void *); i++) {\n\t\tptrs[i] = smallocx(largemax, 0).ptr;\n\t\tif (ptrs[i] == NULL) {\n\t\t\toom = true;\n\t\t}\n\t}\n\tassert_true(oom,\n\t    \"Expected OOM during series of calls to smallocx(size=%zu, 0)\",\n\t    largemax);\n\tfor (i = 0; i < sizeof(ptrs) / sizeof(void *); i++) {\n\t\tif (ptrs[i] != NULL) {\n\t\t\tdallocx(ptrs[i], 0);\n\t\t}\n\t}\n\tpurge();\n\n#if LG_SIZEOF_PTR == 3\n\tassert_ptr_null(smallocx(0x8000000000000000ULL,\n\t    MALLOCX_ALIGN(0x8000000000000000ULL)).ptr,\n\t    \"Expected OOM for smallocx()\");\n\tassert_ptr_null(smallocx(0x8000000000000000ULL,\n\t    MALLOCX_ALIGN(0x80000000)).ptr,\n\t    \"Expected OOM for smallocx()\");\n#else\n\tassert_ptr_null(smallocx(0x80000000UL, MALLOCX_ALIGN(0x80000000UL)).ptr,\n\t    \"Expected OOM for smallocx()\");\n#endif\n}\nTEST_END\n\n/* Re-enable the \"-Walloc-size-larger-than=\" warning */\nJEMALLOC_DIAGNOSTIC_POP\n\nTEST_BEGIN(test_basic) {\n#define MAXSZ (((size_t)1) << 23)\n\tsize_t sz;\n\n\tfor (sz = 1; sz < MAXSZ; sz = nallocx(sz, 0) + 1) {\n\t\tsmallocx_return_t ret;\n\t\tsize_t nsz, rsz, smz;\n\t\tvoid *p;\n\t\tnsz = nallocx(sz, 0);\n\t\tassert_zu_ne(nsz, 0, \"Unexpected nallocx() error\");\n\t\tret = smallocx(sz, 0);\n\t\tp = ret.ptr;\n\t\tsmz = ret.size;\n\t\tassert_ptr_not_null(p,\n\t\t    \"Unexpected smallocx(size=%zx, flags=0) error\", sz);\n\t\trsz = sallocx(p, 0);\n\t\tassert_zu_ge(rsz, sz, \"Real size smaller than expected\");\n\t\tassert_zu_eq(nsz, rsz, \"nallocx()/sallocx() size mismatch\");\n\t\tassert_zu_eq(nsz, smz, \"nallocx()/smallocx() size mismatch\");\n\t\tdallocx(p, 0);\n\n\t\tret = smallocx(sz, 0);\n\t\tp = ret.ptr;\n\t\tsmz = ret.size;\n\t\tassert_ptr_not_null(p,\n\t\t    \"Unexpected smallocx(size=%zx, flags=0) error\", sz);\n\t\tdallocx(p, 0);\n\n\t\tnsz = nallocx(sz, MALLOCX_ZERO);\n\t\tassert_zu_ne(nsz, 0, \"Unexpected nallocx() error\");\n\t\tassert_zu_ne(smz, 0, \"Unexpected smallocx() error\");\n\t\tret = smallocx(sz, MALLOCX_ZERO);\n\t\tp = ret.ptr;\n\t\tassert_ptr_not_null(p,\n\t\t    \"Unexpected smallocx(size=%zx, flags=MALLOCX_ZERO) error\",\n\t\t    nsz);\n\t\trsz = sallocx(p, 0);\n\t\tassert_zu_eq(nsz, rsz, \"nallocx()/sallocx() rsize mismatch\");\n\t\tassert_zu_eq(nsz, smz, \"nallocx()/smallocx() size mismatch\");\n\t\tdallocx(p, 0);\n\t\tpurge();\n\t}\n#undef MAXSZ\n}\nTEST_END\n\nTEST_BEGIN(test_alignment_and_size) {\n\tconst char *percpu_arena;\n\tsize_t sz = sizeof(percpu_arena);\n\n\tif(mallctl(\"opt.percpu_arena\", (void *)&percpu_arena, &sz, NULL, 0) ||\n\t    strcmp(percpu_arena, \"disabled\") != 0) {\n\t\ttest_skip(\"test_alignment_and_size skipped: \"\n\t\t    \"not working with percpu arena.\");\n\t};\n#define MAXALIGN (((size_t)1) << 23)\n#define NITER 4\n\tsize_t nsz, rsz, smz, alignment, total;\n\tunsigned i;\n\tvoid *ps[NITER];\n\n\tfor (i = 0; i < NITER; i++) {\n\t\tps[i] = NULL;\n\t}\n\n\tfor (alignment = 8;\n\t    alignment <= MAXALIGN;\n\t    alignment <<= 1) {\n\t\ttotal = 0;\n\t\tfor (sz = 1;\n\t\t    sz < 3 * alignment && sz < (1U << 31);\n\t\t    sz += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {\n\t\t\tfor (i = 0; i < NITER; i++) {\n\t\t\t\tnsz = nallocx(sz, MALLOCX_ALIGN(alignment) |\n\t\t\t\t    MALLOCX_ZERO);\n\t\t\t\tassert_zu_ne(nsz, 0,\n\t\t\t\t    \"nallocx() error for alignment=%zu, \"\n\t\t\t\t    \"size=%zu (%#zx)\", alignment, sz, sz);\n\t\t\t\tsmallocx_return_t ret\n\t\t\t\t    = smallocx(sz, MALLOCX_ALIGN(alignment) | MALLOCX_ZERO);\n\t\t\t\tps[i] = ret.ptr;\n\t\t\t\tassert_ptr_not_null(ps[i],\n\t\t\t\t    \"smallocx() error for alignment=%zu, \"\n\t\t\t\t    \"size=%zu (%#zx)\", alignment, sz, sz);\n\t\t\t\trsz = sallocx(ps[i], 0);\n\t\t\t\tsmz = ret.size;\n\t\t\t\tassert_zu_ge(rsz, sz,\n\t\t\t\t    \"Real size smaller than expected for \"\n\t\t\t\t    \"alignment=%zu, size=%zu\", alignment, sz);\n\t\t\t\tassert_zu_eq(nsz, rsz,\n\t\t\t\t    \"nallocx()/sallocx() size mismatch for \"\n\t\t\t\t    \"alignment=%zu, size=%zu\", alignment, sz);\n\t\t\t\tassert_zu_eq(nsz, smz,\n\t\t\t\t    \"nallocx()/smallocx() size mismatch for \"\n\t\t\t\t    \"alignment=%zu, size=%zu\", alignment, sz);\n\t\t\t\tassert_ptr_null(\n\t\t\t\t    (void *)((uintptr_t)ps[i] & (alignment-1)),\n\t\t\t\t    \"%p inadequately aligned for\"\n\t\t\t\t    \" alignment=%zu, size=%zu\", ps[i],\n\t\t\t\t    alignment, sz);\n\t\t\t\ttotal += rsz;\n\t\t\t\tif (total >= (MAXALIGN << 1)) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (i = 0; i < NITER; i++) {\n\t\t\t\tif (ps[i] != NULL) {\n\t\t\t\t\tdallocx(ps[i], 0);\n\t\t\t\t\tps[i] = NULL;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tpurge();\n\t}\n#undef MAXALIGN\n#undef NITER\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_overflow,\n\t    test_oom,\n\t    test_remote_free,\n\t    test_basic,\n\t    test_alignment_and_size);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/integration/smallocx.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_fill}\" = \"x1\" ] ; then\n    export MALLOC_CONF=\"junk:false\"\nfi\n"
  },
  {
    "path": "deps/jemalloc/test/integration/thread_arena.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#define NTHREADS 10\n\nvoid *\nthd_start(void *arg) {\n\tunsigned main_arena_ind = *(unsigned *)arg;\n\tvoid *p;\n\tunsigned arena_ind;\n\tsize_t size;\n\tint err;\n\n\tp = malloc(1);\n\tassert_ptr_not_null(p, \"Error in malloc()\");\n\tfree(p);\n\n\tsize = sizeof(arena_ind);\n\tif ((err = mallctl(\"thread.arena\", (void *)&arena_ind, &size,\n\t    (void *)&main_arena_ind, sizeof(main_arena_ind)))) {\n\t\tchar buf[BUFERROR_BUF];\n\n\t\tbuferror(err, buf, sizeof(buf));\n\t\ttest_fail(\"Error in mallctl(): %s\", buf);\n\t}\n\n\tsize = sizeof(arena_ind);\n\tif ((err = mallctl(\"thread.arena\", (void *)&arena_ind, &size, NULL,\n\t    0))) {\n\t\tchar buf[BUFERROR_BUF];\n\n\t\tbuferror(err, buf, sizeof(buf));\n\t\ttest_fail(\"Error in mallctl(): %s\", buf);\n\t}\n\tassert_u_eq(arena_ind, main_arena_ind,\n\t    \"Arena index should be same as for main thread\");\n\n\treturn NULL;\n}\n\nstatic void\nmallctl_failure(int err) {\n\tchar buf[BUFERROR_BUF];\n\n\tbuferror(err, buf, sizeof(buf));\n\ttest_fail(\"Error in mallctl(): %s\", buf);\n}\n\nTEST_BEGIN(test_thread_arena) {\n\tvoid *p;\n\tint err;\n\tthd_t thds[NTHREADS];\n\tunsigned i;\n\n\tp = malloc(1);\n\tassert_ptr_not_null(p, \"Error in malloc()\");\n\n\tunsigned arena_ind, old_arena_ind;\n\tsize_t sz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena_ind, &sz, NULL, 0),\n\t    0, \"Arena creation failure\");\n\n\tsize_t size = sizeof(arena_ind);\n\tif ((err = mallctl(\"thread.arena\", (void *)&old_arena_ind, &size,\n\t    (void *)&arena_ind, sizeof(arena_ind))) != 0) {\n\t\tmallctl_failure(err);\n\t}\n\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tthd_create(&thds[i], thd_start,\n\t\t    (void *)&arena_ind);\n\t}\n\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tintptr_t join_ret;\n\t\tthd_join(thds[i], (void *)&join_ret);\n\t\tassert_zd_eq(join_ret, 0, \"Unexpected thread join error\");\n\t}\n\tfree(p);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_thread_arena);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/integration/thread_tcache_enabled.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nvoid *\nthd_start(void *arg) {\n\tbool e0, e1;\n\tsize_t sz = sizeof(bool);\n\tassert_d_eq(mallctl(\"thread.tcache.enabled\", (void *)&e0, &sz, NULL,\n\t    0), 0, \"Unexpected mallctl failure\");\n\n\tif (e0) {\n\t\te1 = false;\n\t\tassert_d_eq(mallctl(\"thread.tcache.enabled\", (void *)&e0, &sz,\n\t\t    (void *)&e1, sz), 0, \"Unexpected mallctl() error\");\n\t\tassert_true(e0, \"tcache should be enabled\");\n\t}\n\n\te1 = true;\n\tassert_d_eq(mallctl(\"thread.tcache.enabled\", (void *)&e0, &sz,\n\t    (void *)&e1, sz), 0, \"Unexpected mallctl() error\");\n\tassert_false(e0, \"tcache should be disabled\");\n\n\te1 = true;\n\tassert_d_eq(mallctl(\"thread.tcache.enabled\", (void *)&e0, &sz,\n\t    (void *)&e1, sz), 0, \"Unexpected mallctl() error\");\n\tassert_true(e0, \"tcache should be enabled\");\n\n\te1 = false;\n\tassert_d_eq(mallctl(\"thread.tcache.enabled\", (void *)&e0, &sz,\n\t    (void *)&e1, sz), 0, \"Unexpected mallctl() error\");\n\tassert_true(e0, \"tcache should be enabled\");\n\n\te1 = false;\n\tassert_d_eq(mallctl(\"thread.tcache.enabled\", (void *)&e0, &sz,\n\t    (void *)&e1, sz), 0, \"Unexpected mallctl() error\");\n\tassert_false(e0, \"tcache should be disabled\");\n\n\tfree(malloc(1));\n\te1 = true;\n\tassert_d_eq(mallctl(\"thread.tcache.enabled\", (void *)&e0, &sz,\n\t    (void *)&e1, sz), 0, \"Unexpected mallctl() error\");\n\tassert_false(e0, \"tcache should be disabled\");\n\n\tfree(malloc(1));\n\te1 = true;\n\tassert_d_eq(mallctl(\"thread.tcache.enabled\", (void *)&e0, &sz,\n\t    (void *)&e1, sz), 0, \"Unexpected mallctl() error\");\n\tassert_true(e0, \"tcache should be enabled\");\n\n\tfree(malloc(1));\n\te1 = false;\n\tassert_d_eq(mallctl(\"thread.tcache.enabled\", (void *)&e0, &sz,\n\t    (void *)&e1, sz), 0, \"Unexpected mallctl() error\");\n\tassert_true(e0, \"tcache should be enabled\");\n\n\tfree(malloc(1));\n\te1 = false;\n\tassert_d_eq(mallctl(\"thread.tcache.enabled\", (void *)&e0, &sz,\n\t    (void *)&e1, sz), 0, \"Unexpected mallctl() error\");\n\tassert_false(e0, \"tcache should be disabled\");\n\n\tfree(malloc(1));\n\treturn NULL;\n}\n\nTEST_BEGIN(test_main_thread) {\n\tthd_start(NULL);\n}\nTEST_END\n\nTEST_BEGIN(test_subthread) {\n\tthd_t thd;\n\n\tthd_create(&thd, thd_start, NULL);\n\tthd_join(thd, NULL);\n}\nTEST_END\n\nint\nmain(void) {\n\t/* Run tests multiple times to check for bad interactions. */\n\treturn test(\n\t    test_main_thread,\n\t    test_subthread,\n\t    test_main_thread,\n\t    test_subthread,\n\t    test_main_thread);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/integration/xallocx.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n/*\n * Use a separate arena for xallocx() extension/contraction tests so that\n * internal allocation e.g. by heap profiling can't interpose allocations where\n * xallocx() would ordinarily be able to extend.\n */\nstatic unsigned\narena_ind(void) {\n\tstatic unsigned ind = 0;\n\n\tif (ind == 0) {\n\t\tsize_t sz = sizeof(ind);\n\t\tassert_d_eq(mallctl(\"arenas.create\", (void *)&ind, &sz, NULL,\n\t\t    0), 0, \"Unexpected mallctl failure creating arena\");\n\t}\n\n\treturn ind;\n}\n\nTEST_BEGIN(test_same_size) {\n\tvoid *p;\n\tsize_t sz, tsz;\n\n\tp = mallocx(42, 0);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\tsz = sallocx(p, 0);\n\n\ttsz = xallocx(p, sz, 0, 0);\n\tassert_zu_eq(tsz, sz, \"Unexpected size change: %zu --> %zu\", sz, tsz);\n\n\tdallocx(p, 0);\n}\nTEST_END\n\nTEST_BEGIN(test_extra_no_move) {\n\tvoid *p;\n\tsize_t sz, tsz;\n\n\tp = mallocx(42, 0);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\tsz = sallocx(p, 0);\n\n\ttsz = xallocx(p, sz, sz-42, 0);\n\tassert_zu_eq(tsz, sz, \"Unexpected size change: %zu --> %zu\", sz, tsz);\n\n\tdallocx(p, 0);\n}\nTEST_END\n\nTEST_BEGIN(test_no_move_fail) {\n\tvoid *p;\n\tsize_t sz, tsz;\n\n\tp = mallocx(42, 0);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\tsz = sallocx(p, 0);\n\n\ttsz = xallocx(p, sz + 5, 0, 0);\n\tassert_zu_eq(tsz, sz, \"Unexpected size change: %zu --> %zu\", sz, tsz);\n\n\tdallocx(p, 0);\n}\nTEST_END\n\nstatic unsigned\nget_nsizes_impl(const char *cmd) {\n\tunsigned ret;\n\tsize_t z;\n\n\tz = sizeof(unsigned);\n\tassert_d_eq(mallctl(cmd, (void *)&ret, &z, NULL, 0), 0,\n\t    \"Unexpected mallctl(\\\"%s\\\", ...) failure\", cmd);\n\n\treturn ret;\n}\n\nstatic unsigned\nget_nsmall(void) {\n\treturn get_nsizes_impl(\"arenas.nbins\");\n}\n\nstatic unsigned\nget_nlarge(void) {\n\treturn get_nsizes_impl(\"arenas.nlextents\");\n}\n\nstatic size_t\nget_size_impl(const char *cmd, size_t ind) {\n\tsize_t ret;\n\tsize_t z;\n\tsize_t mib[4];\n\tsize_t miblen = 4;\n\n\tz = sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(cmd, mib, &miblen),\n\t    0, \"Unexpected mallctlnametomib(\\\"%s\\\", ...) failure\", cmd);\n\tmib[2] = ind;\n\tz = sizeof(size_t);\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&ret, &z, NULL, 0),\n\t    0, \"Unexpected mallctlbymib([\\\"%s\\\", %zu], ...) failure\", cmd, ind);\n\n\treturn ret;\n}\n\nstatic size_t\nget_small_size(size_t ind) {\n\treturn get_size_impl(\"arenas.bin.0.size\", ind);\n}\n\nstatic size_t\nget_large_size(size_t ind) {\n\treturn get_size_impl(\"arenas.lextent.0.size\", ind);\n}\n\nTEST_BEGIN(test_size) {\n\tsize_t small0, largemax;\n\tvoid *p;\n\n\t/* Get size classes. */\n\tsmall0 = get_small_size(0);\n\tlargemax = get_large_size(get_nlarge()-1);\n\n\tp = mallocx(small0, 0);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\n\t/* Test smallest supported size. */\n\tassert_zu_eq(xallocx(p, 1, 0, 0), small0,\n\t    \"Unexpected xallocx() behavior\");\n\n\t/* Test largest supported size. */\n\tassert_zu_le(xallocx(p, largemax, 0, 0), largemax,\n\t    \"Unexpected xallocx() behavior\");\n\n\t/* Test size overflow. */\n\tassert_zu_le(xallocx(p, largemax+1, 0, 0), largemax,\n\t    \"Unexpected xallocx() behavior\");\n\tassert_zu_le(xallocx(p, SIZE_T_MAX, 0, 0), largemax,\n\t    \"Unexpected xallocx() behavior\");\n\n\tdallocx(p, 0);\n}\nTEST_END\n\nTEST_BEGIN(test_size_extra_overflow) {\n\tsize_t small0, largemax;\n\tvoid *p;\n\n\t/* Get size classes. */\n\tsmall0 = get_small_size(0);\n\tlargemax = get_large_size(get_nlarge()-1);\n\n\tp = mallocx(small0, 0);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\n\t/* Test overflows that can be resolved by clamping extra. */\n\tassert_zu_le(xallocx(p, largemax-1, 2, 0), largemax,\n\t    \"Unexpected xallocx() behavior\");\n\tassert_zu_le(xallocx(p, largemax, 1, 0), largemax,\n\t    \"Unexpected xallocx() behavior\");\n\n\t/* Test overflow such that largemax-size underflows. */\n\tassert_zu_le(xallocx(p, largemax+1, 2, 0), largemax,\n\t    \"Unexpected xallocx() behavior\");\n\tassert_zu_le(xallocx(p, largemax+2, 3, 0), largemax,\n\t    \"Unexpected xallocx() behavior\");\n\tassert_zu_le(xallocx(p, SIZE_T_MAX-2, 2, 0), largemax,\n\t    \"Unexpected xallocx() behavior\");\n\tassert_zu_le(xallocx(p, SIZE_T_MAX-1, 1, 0), largemax,\n\t    \"Unexpected xallocx() behavior\");\n\n\tdallocx(p, 0);\n}\nTEST_END\n\nTEST_BEGIN(test_extra_small) {\n\tsize_t small0, small1, largemax;\n\tvoid *p;\n\n\t/* Get size classes. */\n\tsmall0 = get_small_size(0);\n\tsmall1 = get_small_size(1);\n\tlargemax = get_large_size(get_nlarge()-1);\n\n\tp = mallocx(small0, 0);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\n\tassert_zu_eq(xallocx(p, small1, 0, 0), small0,\n\t    \"Unexpected xallocx() behavior\");\n\n\tassert_zu_eq(xallocx(p, small1, 0, 0), small0,\n\t    \"Unexpected xallocx() behavior\");\n\n\tassert_zu_eq(xallocx(p, small0, small1 - small0, 0), small0,\n\t    \"Unexpected xallocx() behavior\");\n\n\t/* Test size+extra overflow. */\n\tassert_zu_eq(xallocx(p, small0, largemax - small0 + 1, 0), small0,\n\t    \"Unexpected xallocx() behavior\");\n\tassert_zu_eq(xallocx(p, small0, SIZE_T_MAX - small0, 0), small0,\n\t    \"Unexpected xallocx() behavior\");\n\n\tdallocx(p, 0);\n}\nTEST_END\n\nTEST_BEGIN(test_extra_large) {\n\tint flags = MALLOCX_ARENA(arena_ind());\n\tsize_t smallmax, large1, large2, large3, largemax;\n\tvoid *p;\n\n\t/* Get size classes. */\n\tsmallmax = get_small_size(get_nsmall()-1);\n\tlarge1 = get_large_size(1);\n\tlarge2 = get_large_size(2);\n\tlarge3 = get_large_size(3);\n\tlargemax = get_large_size(get_nlarge()-1);\n\n\tp = mallocx(large3, flags);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\n\tassert_zu_eq(xallocx(p, large3, 0, flags), large3,\n\t    \"Unexpected xallocx() behavior\");\n\t/* Test size decrease with zero extra. */\n\tassert_zu_ge(xallocx(p, large1, 0, flags), large1,\n\t    \"Unexpected xallocx() behavior\");\n\tassert_zu_ge(xallocx(p, smallmax, 0, flags), large1,\n\t    \"Unexpected xallocx() behavior\");\n\n\tif (xallocx(p, large3, 0, flags) != large3) {\n\t\tp = rallocx(p, large3, flags);\n\t\tassert_ptr_not_null(p, \"Unexpected rallocx() failure\");\n\t}\n\t/* Test size decrease with non-zero extra. */\n\tassert_zu_eq(xallocx(p, large1, large3 - large1, flags), large3,\n\t    \"Unexpected xallocx() behavior\");\n\tassert_zu_eq(xallocx(p, large2, large3 - large2, flags), large3,\n\t    \"Unexpected xallocx() behavior\");\n\tassert_zu_ge(xallocx(p, large1, large2 - large1, flags), large2,\n\t    \"Unexpected xallocx() behavior\");\n\tassert_zu_ge(xallocx(p, smallmax, large1 - smallmax, flags), large1,\n\t    \"Unexpected xallocx() behavior\");\n\n\tassert_zu_ge(xallocx(p, large1, 0, flags), large1,\n\t    \"Unexpected xallocx() behavior\");\n\t/* Test size increase with zero extra. */\n\tassert_zu_le(xallocx(p, large3, 0, flags), large3,\n\t    \"Unexpected xallocx() behavior\");\n\tassert_zu_le(xallocx(p, largemax+1, 0, flags), large3,\n\t    \"Unexpected xallocx() behavior\");\n\n\tassert_zu_ge(xallocx(p, large1, 0, flags), large1,\n\t    \"Unexpected xallocx() behavior\");\n\t/* Test size increase with non-zero extra. */\n\tassert_zu_le(xallocx(p, large1, SIZE_T_MAX - large1, flags), largemax,\n\t    \"Unexpected xallocx() behavior\");\n\n\tassert_zu_ge(xallocx(p, large1, 0, flags), large1,\n\t    \"Unexpected xallocx() behavior\");\n\t/* Test size increase with non-zero extra. */\n\tassert_zu_le(xallocx(p, large1, large3 - large1, flags), large3,\n\t    \"Unexpected xallocx() behavior\");\n\n\tif (xallocx(p, large3, 0, flags) != large3) {\n\t\tp = rallocx(p, large3, flags);\n\t\tassert_ptr_not_null(p, \"Unexpected rallocx() failure\");\n\t}\n\t/* Test size+extra overflow. */\n\tassert_zu_le(xallocx(p, large3, largemax - large3 + 1, flags), largemax,\n\t    \"Unexpected xallocx() behavior\");\n\n\tdallocx(p, flags);\n}\nTEST_END\n\nstatic void\nprint_filled_extents(const void *p, uint8_t c, size_t len) {\n\tconst uint8_t *pc = (const uint8_t *)p;\n\tsize_t i, range0;\n\tuint8_t c0;\n\n\tmalloc_printf(\"  p=%p, c=%#x, len=%zu:\", p, c, len);\n\trange0 = 0;\n\tc0 = pc[0];\n\tfor (i = 0; i < len; i++) {\n\t\tif (pc[i] != c0) {\n\t\t\tmalloc_printf(\" %#x[%zu..%zu)\", c0, range0, i);\n\t\t\trange0 = i;\n\t\t\tc0 = pc[i];\n\t\t}\n\t}\n\tmalloc_printf(\" %#x[%zu..%zu)\\n\", c0, range0, i);\n}\n\nstatic bool\nvalidate_fill(const void *p, uint8_t c, size_t offset, size_t len) {\n\tconst uint8_t *pc = (const uint8_t *)p;\n\tbool err;\n\tsize_t i;\n\n\tfor (i = offset, err = false; i < offset+len; i++) {\n\t\tif (pc[i] != c) {\n\t\t\terr = true;\n\t\t}\n\t}\n\n\tif (err) {\n\t\tprint_filled_extents(p, c, offset + len);\n\t}\n\n\treturn err;\n}\n\nstatic void\ntest_zero(size_t szmin, size_t szmax) {\n\tint flags = MALLOCX_ARENA(arena_ind()) | MALLOCX_ZERO;\n\tsize_t sz, nsz;\n\tvoid *p;\n#define FILL_BYTE 0x7aU\n\n\tsz = szmax;\n\tp = mallocx(sz, flags);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\tassert_false(validate_fill(p, 0x00, 0, sz), \"Memory not filled: sz=%zu\",\n\t    sz);\n\n\t/*\n\t * Fill with non-zero so that non-debug builds are more likely to detect\n\t * errors.\n\t */\n\tmemset(p, FILL_BYTE, sz);\n\tassert_false(validate_fill(p, FILL_BYTE, 0, sz),\n\t    \"Memory not filled: sz=%zu\", sz);\n\n\t/* Shrink in place so that we can expect growing in place to succeed. */\n\tsz = szmin;\n\tif (xallocx(p, sz, 0, flags) != sz) {\n\t\tp = rallocx(p, sz, flags);\n\t\tassert_ptr_not_null(p, \"Unexpected rallocx() failure\");\n\t}\n\tassert_false(validate_fill(p, FILL_BYTE, 0, sz),\n\t    \"Memory not filled: sz=%zu\", sz);\n\n\tfor (sz = szmin; sz < szmax; sz = nsz) {\n\t\tnsz = nallocx(sz+1, flags);\n\t\tif (xallocx(p, sz+1, 0, flags) != nsz) {\n\t\t\tp = rallocx(p, sz+1, flags);\n\t\t\tassert_ptr_not_null(p, \"Unexpected rallocx() failure\");\n\t\t}\n\t\tassert_false(validate_fill(p, FILL_BYTE, 0, sz),\n\t\t    \"Memory not filled: sz=%zu\", sz);\n\t\tassert_false(validate_fill(p, 0x00, sz, nsz-sz),\n\t\t    \"Memory not filled: sz=%zu, nsz-sz=%zu\", sz, nsz-sz);\n\t\tmemset((void *)((uintptr_t)p + sz), FILL_BYTE, nsz-sz);\n\t\tassert_false(validate_fill(p, FILL_BYTE, 0, nsz),\n\t\t    \"Memory not filled: nsz=%zu\", nsz);\n\t}\n\n\tdallocx(p, flags);\n}\n\nTEST_BEGIN(test_zero_large) {\n\tsize_t large0, large1;\n\n\t/* Get size classes. */\n\tlarge0 = get_large_size(0);\n\tlarge1 = get_large_size(1);\n\n\ttest_zero(large1, large0 * 2);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_same_size,\n\t    test_extra_no_move,\n\t    test_no_move_fail,\n\t    test_size,\n\t    test_size_extra_overflow,\n\t    test_extra_small,\n\t    test_extra_large,\n\t    test_zero_large);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/integration/xallocx.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_fill}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"junk:false\"\nfi\n"
  },
  {
    "path": "deps/jemalloc/test/src/SFMT.c",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n/**\n * @file  SFMT.c\n * @brief SIMD oriented Fast Mersenne Twister(SFMT)\n *\n * @author Mutsuo Saito (Hiroshima University)\n * @author Makoto Matsumoto (Hiroshima University)\n *\n * Copyright (C) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n * University. All rights reserved.\n *\n * The new BSD License is applied to this software, see LICENSE.txt\n */\n#define SFMT_C_\n#include \"test/jemalloc_test.h\"\n#include \"test/SFMT-params.h\"\n\n#if defined(JEMALLOC_BIG_ENDIAN) && !defined(BIG_ENDIAN64)\n#define BIG_ENDIAN64 1\n#endif\n#if defined(__BIG_ENDIAN__) && !defined(__amd64) && !defined(BIG_ENDIAN64)\n#define BIG_ENDIAN64 1\n#endif\n#if defined(HAVE_ALTIVEC) && !defined(BIG_ENDIAN64)\n#define BIG_ENDIAN64 1\n#endif\n#if defined(ONLY64) && !defined(BIG_ENDIAN64)\n  #if defined(__GNUC__)\n    #error \"-DONLY64 must be specified with -DBIG_ENDIAN64\"\n  #endif\n#undef ONLY64\n#endif\n/*------------------------------------------------------\n  128-bit SIMD data type for Altivec, SSE2 or standard C\n  ------------------------------------------------------*/\n#if defined(HAVE_ALTIVEC)\n/** 128-bit data structure */\nunion W128_T {\n    vector unsigned int s;\n    uint32_t u[4];\n};\n/** 128-bit data type */\ntypedef union W128_T w128_t;\n\n#elif defined(HAVE_SSE2)\n/** 128-bit data structure */\nunion W128_T {\n    __m128i si;\n    uint32_t u[4];\n};\n/** 128-bit data type */\ntypedef union W128_T w128_t;\n\n#else\n\n/** 128-bit data structure */\nstruct W128_T {\n    uint32_t u[4];\n};\n/** 128-bit data type */\ntypedef struct W128_T w128_t;\n\n#endif\n\nstruct sfmt_s {\n    /** the 128-bit internal state array */\n    w128_t sfmt[N];\n    /** index counter to the 32-bit internal state array */\n    int idx;\n    /** a flag: it is 0 if and only if the internal state is not yet\n     * initialized. */\n    int initialized;\n};\n\n/*--------------------------------------\n  FILE GLOBAL VARIABLES\n  internal state, index counter and flag\n  --------------------------------------*/\n\n/** a parity check vector which certificate the period of 2^{MEXP} */\nstatic uint32_t parity[4] = {PARITY1, PARITY2, PARITY3, PARITY4};\n\n/*----------------\n  STATIC FUNCTIONS\n  ----------------*/\nstatic inline int idxof(int i);\n#if (!defined(HAVE_ALTIVEC)) && (!defined(HAVE_SSE2))\nstatic inline void rshift128(w128_t *out,  w128_t const *in, int shift);\nstatic inline void lshift128(w128_t *out,  w128_t const *in, int shift);\n#endif\nstatic inline void gen_rand_all(sfmt_t *ctx);\nstatic inline void gen_rand_array(sfmt_t *ctx, w128_t *array, int size);\nstatic inline uint32_t func1(uint32_t x);\nstatic inline uint32_t func2(uint32_t x);\nstatic void period_certification(sfmt_t *ctx);\n#if defined(BIG_ENDIAN64) && !defined(ONLY64)\nstatic inline void swap(w128_t *array, int size);\n#endif\n\n#if defined(HAVE_ALTIVEC)\n  #include \"test/SFMT-alti.h\"\n#elif defined(HAVE_SSE2)\n  #include \"test/SFMT-sse2.h\"\n#endif\n\n/**\n * This function simulate a 64-bit index of LITTLE ENDIAN\n * in BIG ENDIAN machine.\n */\n#ifdef ONLY64\nstatic inline int idxof(int i) {\n    return i ^ 1;\n}\n#else\nstatic inline int idxof(int i) {\n    return i;\n}\n#endif\n/**\n * This function simulates SIMD 128-bit right shift by the standard C.\n * The 128-bit integer given in in is shifted by (shift * 8) bits.\n * This function simulates the LITTLE ENDIAN SIMD.\n * @param out the output of this function\n * @param in the 128-bit data to be shifted\n * @param shift the shift value\n */\n#if (!defined(HAVE_ALTIVEC)) && (!defined(HAVE_SSE2))\n#ifdef ONLY64\nstatic inline void rshift128(w128_t *out, w128_t const *in, int shift) {\n    uint64_t th, tl, oh, ol;\n\n    th = ((uint64_t)in->u[2] << 32) | ((uint64_t)in->u[3]);\n    tl = ((uint64_t)in->u[0] << 32) | ((uint64_t)in->u[1]);\n\n    oh = th >> (shift * 8);\n    ol = tl >> (shift * 8);\n    ol |= th << (64 - shift * 8);\n    out->u[0] = (uint32_t)(ol >> 32);\n    out->u[1] = (uint32_t)ol;\n    out->u[2] = (uint32_t)(oh >> 32);\n    out->u[3] = (uint32_t)oh;\n}\n#else\nstatic inline void rshift128(w128_t *out, w128_t const *in, int shift) {\n    uint64_t th, tl, oh, ol;\n\n    th = ((uint64_t)in->u[3] << 32) | ((uint64_t)in->u[2]);\n    tl = ((uint64_t)in->u[1] << 32) | ((uint64_t)in->u[0]);\n\n    oh = th >> (shift * 8);\n    ol = tl >> (shift * 8);\n    ol |= th << (64 - shift * 8);\n    out->u[1] = (uint32_t)(ol >> 32);\n    out->u[0] = (uint32_t)ol;\n    out->u[3] = (uint32_t)(oh >> 32);\n    out->u[2] = (uint32_t)oh;\n}\n#endif\n/**\n * This function simulates SIMD 128-bit left shift by the standard C.\n * The 128-bit integer given in in is shifted by (shift * 8) bits.\n * This function simulates the LITTLE ENDIAN SIMD.\n * @param out the output of this function\n * @param in the 128-bit data to be shifted\n * @param shift the shift value\n */\n#ifdef ONLY64\nstatic inline void lshift128(w128_t *out, w128_t const *in, int shift) {\n    uint64_t th, tl, oh, ol;\n\n    th = ((uint64_t)in->u[2] << 32) | ((uint64_t)in->u[3]);\n    tl = ((uint64_t)in->u[0] << 32) | ((uint64_t)in->u[1]);\n\n    oh = th << (shift * 8);\n    ol = tl << (shift * 8);\n    oh |= tl >> (64 - shift * 8);\n    out->u[0] = (uint32_t)(ol >> 32);\n    out->u[1] = (uint32_t)ol;\n    out->u[2] = (uint32_t)(oh >> 32);\n    out->u[3] = (uint32_t)oh;\n}\n#else\nstatic inline void lshift128(w128_t *out, w128_t const *in, int shift) {\n    uint64_t th, tl, oh, ol;\n\n    th = ((uint64_t)in->u[3] << 32) | ((uint64_t)in->u[2]);\n    tl = ((uint64_t)in->u[1] << 32) | ((uint64_t)in->u[0]);\n\n    oh = th << (shift * 8);\n    ol = tl << (shift * 8);\n    oh |= tl >> (64 - shift * 8);\n    out->u[1] = (uint32_t)(ol >> 32);\n    out->u[0] = (uint32_t)ol;\n    out->u[3] = (uint32_t)(oh >> 32);\n    out->u[2] = (uint32_t)oh;\n}\n#endif\n#endif\n\n/**\n * This function represents the recursion formula.\n * @param r output\n * @param a a 128-bit part of the internal state array\n * @param b a 128-bit part of the internal state array\n * @param c a 128-bit part of the internal state array\n * @param d a 128-bit part of the internal state array\n */\n#if (!defined(HAVE_ALTIVEC)) && (!defined(HAVE_SSE2))\n#ifdef ONLY64\nstatic inline void do_recursion(w128_t *r, w128_t *a, w128_t *b, w128_t *c,\n\t\t\t\tw128_t *d) {\n    w128_t x;\n    w128_t y;\n\n    lshift128(&x, a, SL2);\n    rshift128(&y, c, SR2);\n    r->u[0] = a->u[0] ^ x.u[0] ^ ((b->u[0] >> SR1) & MSK2) ^ y.u[0]\n\t^ (d->u[0] << SL1);\n    r->u[1] = a->u[1] ^ x.u[1] ^ ((b->u[1] >> SR1) & MSK1) ^ y.u[1]\n\t^ (d->u[1] << SL1);\n    r->u[2] = a->u[2] ^ x.u[2] ^ ((b->u[2] >> SR1) & MSK4) ^ y.u[2]\n\t^ (d->u[2] << SL1);\n    r->u[3] = a->u[3] ^ x.u[3] ^ ((b->u[3] >> SR1) & MSK3) ^ y.u[3]\n\t^ (d->u[3] << SL1);\n}\n#else\nstatic inline void do_recursion(w128_t *r, w128_t *a, w128_t *b, w128_t *c,\n\t\t\t\tw128_t *d) {\n    w128_t x;\n    w128_t y;\n\n    lshift128(&x, a, SL2);\n    rshift128(&y, c, SR2);\n    r->u[0] = a->u[0] ^ x.u[0] ^ ((b->u[0] >> SR1) & MSK1) ^ y.u[0]\n\t^ (d->u[0] << SL1);\n    r->u[1] = a->u[1] ^ x.u[1] ^ ((b->u[1] >> SR1) & MSK2) ^ y.u[1]\n\t^ (d->u[1] << SL1);\n    r->u[2] = a->u[2] ^ x.u[2] ^ ((b->u[2] >> SR1) & MSK3) ^ y.u[2]\n\t^ (d->u[2] << SL1);\n    r->u[3] = a->u[3] ^ x.u[3] ^ ((b->u[3] >> SR1) & MSK4) ^ y.u[3]\n\t^ (d->u[3] << SL1);\n}\n#endif\n#endif\n\n#if (!defined(HAVE_ALTIVEC)) && (!defined(HAVE_SSE2))\n/**\n * This function fills the internal state array with pseudorandom\n * integers.\n */\nstatic inline void gen_rand_all(sfmt_t *ctx) {\n    int i;\n    w128_t *r1, *r2;\n\n    r1 = &ctx->sfmt[N - 2];\n    r2 = &ctx->sfmt[N - 1];\n    for (i = 0; i < N - POS1; i++) {\n\tdo_recursion(&ctx->sfmt[i], &ctx->sfmt[i], &ctx->sfmt[i + POS1], r1,\n\t  r2);\n\tr1 = r2;\n\tr2 = &ctx->sfmt[i];\n    }\n    for (; i < N; i++) {\n\tdo_recursion(&ctx->sfmt[i], &ctx->sfmt[i], &ctx->sfmt[i + POS1 - N], r1,\n\t  r2);\n\tr1 = r2;\n\tr2 = &ctx->sfmt[i];\n    }\n}\n\n/**\n * This function fills the user-specified array with pseudorandom\n * integers.\n *\n * @param array an 128-bit array to be filled by pseudorandom numbers.\n * @param size number of 128-bit pseudorandom numbers to be generated.\n */\nstatic inline void gen_rand_array(sfmt_t *ctx, w128_t *array, int size) {\n    int i, j;\n    w128_t *r1, *r2;\n\n    r1 = &ctx->sfmt[N - 2];\n    r2 = &ctx->sfmt[N - 1];\n    for (i = 0; i < N - POS1; i++) {\n\tdo_recursion(&array[i], &ctx->sfmt[i], &ctx->sfmt[i + POS1], r1, r2);\n\tr1 = r2;\n\tr2 = &array[i];\n    }\n    for (; i < N; i++) {\n\tdo_recursion(&array[i], &ctx->sfmt[i], &array[i + POS1 - N], r1, r2);\n\tr1 = r2;\n\tr2 = &array[i];\n    }\n    for (; i < size - N; i++) {\n\tdo_recursion(&array[i], &array[i - N], &array[i + POS1 - N], r1, r2);\n\tr1 = r2;\n\tr2 = &array[i];\n    }\n    for (j = 0; j < 2 * N - size; j++) {\n\tctx->sfmt[j] = array[j + size - N];\n    }\n    for (; i < size; i++, j++) {\n\tdo_recursion(&array[i], &array[i - N], &array[i + POS1 - N], r1, r2);\n\tr1 = r2;\n\tr2 = &array[i];\n\tctx->sfmt[j] = array[i];\n    }\n}\n#endif\n\n#if defined(BIG_ENDIAN64) && !defined(ONLY64) && !defined(HAVE_ALTIVEC)\nstatic inline void swap(w128_t *array, int size) {\n    int i;\n    uint32_t x, y;\n\n    for (i = 0; i < size; i++) {\n\tx = array[i].u[0];\n\ty = array[i].u[2];\n\tarray[i].u[0] = array[i].u[1];\n\tarray[i].u[2] = array[i].u[3];\n\tarray[i].u[1] = x;\n\tarray[i].u[3] = y;\n    }\n}\n#endif\n/**\n * This function represents a function used in the initialization\n * by init_by_array\n * @param x 32-bit integer\n * @return 32-bit integer\n */\nstatic uint32_t func1(uint32_t x) {\n    return (x ^ (x >> 27)) * (uint32_t)1664525UL;\n}\n\n/**\n * This function represents a function used in the initialization\n * by init_by_array\n * @param x 32-bit integer\n * @return 32-bit integer\n */\nstatic uint32_t func2(uint32_t x) {\n    return (x ^ (x >> 27)) * (uint32_t)1566083941UL;\n}\n\n/**\n * This function certificate the period of 2^{MEXP}\n */\nstatic void period_certification(sfmt_t *ctx) {\n    int inner = 0;\n    int i, j;\n    uint32_t work;\n    uint32_t *psfmt32 = &ctx->sfmt[0].u[0];\n\n    for (i = 0; i < 4; i++)\n\tinner ^= psfmt32[idxof(i)] & parity[i];\n    for (i = 16; i > 0; i >>= 1)\n\tinner ^= inner >> i;\n    inner &= 1;\n    /* check OK */\n    if (inner == 1) {\n\treturn;\n    }\n    /* check NG, and modification */\n    for (i = 0; i < 4; i++) {\n\twork = 1;\n\tfor (j = 0; j < 32; j++) {\n\t    if ((work & parity[i]) != 0) {\n\t\tpsfmt32[idxof(i)] ^= work;\n\t\treturn;\n\t    }\n\t    work = work << 1;\n\t}\n    }\n}\n\n/*----------------\n  PUBLIC FUNCTIONS\n  ----------------*/\n/**\n * This function returns the identification string.\n * The string shows the word size, the Mersenne exponent,\n * and all parameters of this generator.\n */\nconst char *get_idstring(void) {\n    return IDSTR;\n}\n\n/**\n * This function returns the minimum size of array used for \\b\n * fill_array32() function.\n * @return minimum size of array used for fill_array32() function.\n */\nint get_min_array_size32(void) {\n    return N32;\n}\n\n/**\n * This function returns the minimum size of array used for \\b\n * fill_array64() function.\n * @return minimum size of array used for fill_array64() function.\n */\nint get_min_array_size64(void) {\n    return N64;\n}\n\n#ifndef ONLY64\n/**\n * This function generates and returns 32-bit pseudorandom number.\n * init_gen_rand or init_by_array must be called before this function.\n * @return 32-bit pseudorandom number\n */\nuint32_t gen_rand32(sfmt_t *ctx) {\n    uint32_t r;\n    uint32_t *psfmt32 = &ctx->sfmt[0].u[0];\n\n    assert(ctx->initialized);\n    if (ctx->idx >= N32) {\n\tgen_rand_all(ctx);\n\tctx->idx = 0;\n    }\n    r = psfmt32[ctx->idx++];\n    return r;\n}\n\n/* Generate a random integer in [0..limit). */\nuint32_t gen_rand32_range(sfmt_t *ctx, uint32_t limit) {\n    uint32_t ret, above;\n\n    above = 0xffffffffU - (0xffffffffU % limit);\n    while (1) {\n\tret = gen_rand32(ctx);\n\tif (ret < above) {\n\t    ret %= limit;\n\t    break;\n\t}\n    }\n    return ret;\n}\n#endif\n/**\n * This function generates and returns 64-bit pseudorandom number.\n * init_gen_rand or init_by_array must be called before this function.\n * The function gen_rand64 should not be called after gen_rand32,\n * unless an initialization is again executed.\n * @return 64-bit pseudorandom number\n */\nuint64_t gen_rand64(sfmt_t *ctx) {\n#if defined(BIG_ENDIAN64) && !defined(ONLY64)\n    uint32_t r1, r2;\n    uint32_t *psfmt32 = &ctx->sfmt[0].u[0];\n#else\n    uint64_t r;\n    uint64_t *psfmt64 = (uint64_t *)&ctx->sfmt[0].u[0];\n#endif\n\n    assert(ctx->initialized);\n    assert(ctx->idx % 2 == 0);\n\n    if (ctx->idx >= N32) {\n\tgen_rand_all(ctx);\n\tctx->idx = 0;\n    }\n#if defined(BIG_ENDIAN64) && !defined(ONLY64)\n    r1 = psfmt32[ctx->idx];\n    r2 = psfmt32[ctx->idx + 1];\n    ctx->idx += 2;\n    return ((uint64_t)r2 << 32) | r1;\n#else\n    r = psfmt64[ctx->idx / 2];\n    ctx->idx += 2;\n    return r;\n#endif\n}\n\n/* Generate a random integer in [0..limit). */\nuint64_t gen_rand64_range(sfmt_t *ctx, uint64_t limit) {\n    uint64_t ret, above;\n\n    above = KQU(0xffffffffffffffff) - (KQU(0xffffffffffffffff) % limit);\n    while (1) {\n\tret = gen_rand64(ctx);\n\tif (ret < above) {\n\t    ret %= limit;\n\t    break;\n\t}\n    }\n    return ret;\n}\n\n#ifndef ONLY64\n/**\n * This function generates pseudorandom 32-bit integers in the\n * specified array[] by one call. The number of pseudorandom integers\n * is specified by the argument size, which must be at least 624 and a\n * multiple of four.  The generation by this function is much faster\n * than the following gen_rand function.\n *\n * For initialization, init_gen_rand or init_by_array must be called\n * before the first call of this function. This function can not be\n * used after calling gen_rand function, without initialization.\n *\n * @param array an array where pseudorandom 32-bit integers are filled\n * by this function.  The pointer to the array must be \\b \"aligned\"\n * (namely, must be a multiple of 16) in the SIMD version, since it\n * refers to the address of a 128-bit integer.  In the standard C\n * version, the pointer is arbitrary.\n *\n * @param size the number of 32-bit pseudorandom integers to be\n * generated.  size must be a multiple of 4, and greater than or equal\n * to (MEXP / 128 + 1) * 4.\n *\n * @note \\b memalign or \\b posix_memalign is available to get aligned\n * memory. Mac OSX doesn't have these functions, but \\b malloc of OSX\n * returns the pointer to the aligned memory block.\n */\nvoid fill_array32(sfmt_t *ctx, uint32_t *array, int size) {\n    assert(ctx->initialized);\n    assert(ctx->idx == N32);\n    assert(size % 4 == 0);\n    assert(size >= N32);\n\n    gen_rand_array(ctx, (w128_t *)array, size / 4);\n    ctx->idx = N32;\n}\n#endif\n\n/**\n * This function generates pseudorandom 64-bit integers in the\n * specified array[] by one call. The number of pseudorandom integers\n * is specified by the argument size, which must be at least 312 and a\n * multiple of two.  The generation by this function is much faster\n * than the following gen_rand function.\n *\n * For initialization, init_gen_rand or init_by_array must be called\n * before the first call of this function. This function can not be\n * used after calling gen_rand function, without initialization.\n *\n * @param array an array where pseudorandom 64-bit integers are filled\n * by this function.  The pointer to the array must be \"aligned\"\n * (namely, must be a multiple of 16) in the SIMD version, since it\n * refers to the address of a 128-bit integer.  In the standard C\n * version, the pointer is arbitrary.\n *\n * @param size the number of 64-bit pseudorandom integers to be\n * generated.  size must be a multiple of 2, and greater than or equal\n * to (MEXP / 128 + 1) * 2\n *\n * @note \\b memalign or \\b posix_memalign is available to get aligned\n * memory. Mac OSX doesn't have these functions, but \\b malloc of OSX\n * returns the pointer to the aligned memory block.\n */\nvoid fill_array64(sfmt_t *ctx, uint64_t *array, int size) {\n    assert(ctx->initialized);\n    assert(ctx->idx == N32);\n    assert(size % 2 == 0);\n    assert(size >= N64);\n\n    gen_rand_array(ctx, (w128_t *)array, size / 2);\n    ctx->idx = N32;\n\n#if defined(BIG_ENDIAN64) && !defined(ONLY64)\n    swap((w128_t *)array, size /2);\n#endif\n}\n\n/**\n * This function initializes the internal state array with a 32-bit\n * integer seed.\n *\n * @param seed a 32-bit integer used as the seed.\n */\nsfmt_t *init_gen_rand(uint32_t seed) {\n    void *p;\n    sfmt_t *ctx;\n    int i;\n    uint32_t *psfmt32;\n\n    if (posix_memalign(&p, sizeof(w128_t), sizeof(sfmt_t)) != 0) {\n\treturn NULL;\n    }\n    ctx = (sfmt_t *)p;\n    psfmt32 = &ctx->sfmt[0].u[0];\n\n    psfmt32[idxof(0)] = seed;\n    for (i = 1; i < N32; i++) {\n\tpsfmt32[idxof(i)] = 1812433253UL * (psfmt32[idxof(i - 1)]\n\t\t\t\t\t    ^ (psfmt32[idxof(i - 1)] >> 30))\n\t    + i;\n    }\n    ctx->idx = N32;\n    period_certification(ctx);\n    ctx->initialized = 1;\n\n    return ctx;\n}\n\n/**\n * This function initializes the internal state array,\n * with an array of 32-bit integers used as the seeds\n * @param init_key the array of 32-bit integers, used as a seed.\n * @param key_length the length of init_key.\n */\nsfmt_t *init_by_array(uint32_t *init_key, int key_length) {\n    void *p;\n    sfmt_t *ctx;\n    int i, j, count;\n    uint32_t r;\n    int lag;\n    int mid;\n    int size = N * 4;\n    uint32_t *psfmt32;\n\n    if (posix_memalign(&p, sizeof(w128_t), sizeof(sfmt_t)) != 0) {\n\treturn NULL;\n    }\n    ctx = (sfmt_t *)p;\n    psfmt32 = &ctx->sfmt[0].u[0];\n\n    if (size >= 623) {\n\tlag = 11;\n    } else if (size >= 68) {\n\tlag = 7;\n    } else if (size >= 39) {\n\tlag = 5;\n    } else {\n\tlag = 3;\n    }\n    mid = (size - lag) / 2;\n\n    memset(ctx->sfmt, 0x8b, sizeof(ctx->sfmt));\n    if (key_length + 1 > N32) {\n\tcount = key_length + 1;\n    } else {\n\tcount = N32;\n    }\n    r = func1(psfmt32[idxof(0)] ^ psfmt32[idxof(mid)]\n\t      ^ psfmt32[idxof(N32 - 1)]);\n    psfmt32[idxof(mid)] += r;\n    r += key_length;\n    psfmt32[idxof(mid + lag)] += r;\n    psfmt32[idxof(0)] = r;\n\n    count--;\n    for (i = 1, j = 0; (j < count) && (j < key_length); j++) {\n\tr = func1(psfmt32[idxof(i)] ^ psfmt32[idxof((i + mid) % N32)]\n\t\t  ^ psfmt32[idxof((i + N32 - 1) % N32)]);\n\tpsfmt32[idxof((i + mid) % N32)] += r;\n\tr += init_key[j] + i;\n\tpsfmt32[idxof((i + mid + lag) % N32)] += r;\n\tpsfmt32[idxof(i)] = r;\n\ti = (i + 1) % N32;\n    }\n    for (; j < count; j++) {\n\tr = func1(psfmt32[idxof(i)] ^ psfmt32[idxof((i + mid) % N32)]\n\t\t  ^ psfmt32[idxof((i + N32 - 1) % N32)]);\n\tpsfmt32[idxof((i + mid) % N32)] += r;\n\tr += i;\n\tpsfmt32[idxof((i + mid + lag) % N32)] += r;\n\tpsfmt32[idxof(i)] = r;\n\ti = (i + 1) % N32;\n    }\n    for (j = 0; j < N32; j++) {\n\tr = func2(psfmt32[idxof(i)] + psfmt32[idxof((i + mid) % N32)]\n\t\t  + psfmt32[idxof((i + N32 - 1) % N32)]);\n\tpsfmt32[idxof((i + mid) % N32)] ^= r;\n\tr -= i;\n\tpsfmt32[idxof((i + mid + lag) % N32)] ^= r;\n\tpsfmt32[idxof(i)] = r;\n\ti = (i + 1) % N32;\n    }\n\n    ctx->idx = N32;\n    period_certification(ctx);\n    ctx->initialized = 1;\n\n    return ctx;\n}\n\nvoid fini_gen_rand(sfmt_t *ctx) {\n    assert(ctx != NULL);\n\n    ctx->initialized = 0;\n    free(ctx);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/src/btalloc.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nvoid *\nbtalloc(size_t size, unsigned bits) {\n\treturn btalloc_0(size, bits);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/src/btalloc_0.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nbtalloc_n_gen(0)\n"
  },
  {
    "path": "deps/jemalloc/test/src/btalloc_1.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nbtalloc_n_gen(1)\n"
  },
  {
    "path": "deps/jemalloc/test/src/math.c",
    "content": "#define MATH_C_\n#include \"test/jemalloc_test.h\"\n"
  },
  {
    "path": "deps/jemalloc/test/src/mq.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n/*\n * Sleep for approximately ns nanoseconds.  No lower *nor* upper bound on sleep\n * time is guaranteed.\n */\nvoid\nmq_nanosleep(unsigned ns) {\n\tassert(ns <= 1000*1000*1000);\n\n#ifdef _WIN32\n\tSleep(ns / 1000);\n#else\n\t{\n\t\tstruct timespec timeout;\n\n\t\tif (ns < 1000*1000*1000) {\n\t\t\ttimeout.tv_sec = 0;\n\t\t\ttimeout.tv_nsec = ns;\n\t\t} else {\n\t\t\ttimeout.tv_sec = 1;\n\t\t\ttimeout.tv_nsec = 0;\n\t\t}\n\t\tnanosleep(&timeout, NULL);\n\t}\n#endif\n}\n"
  },
  {
    "path": "deps/jemalloc/test/src/mtx.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#ifndef _CRT_SPINCOUNT\n#define _CRT_SPINCOUNT 4000\n#endif\n\nbool\nmtx_init(mtx_t *mtx) {\n#ifdef _WIN32\n\tif (!InitializeCriticalSectionAndSpinCount(&mtx->lock,\n\t    _CRT_SPINCOUNT)) {\n\t\treturn true;\n\t}\n#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))\n\tmtx->lock = OS_UNFAIR_LOCK_INIT;\n#else\n\tpthread_mutexattr_t attr;\n\n\tif (pthread_mutexattr_init(&attr) != 0) {\n\t\treturn true;\n\t}\n\tpthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT);\n\tif (pthread_mutex_init(&mtx->lock, &attr) != 0) {\n\t\tpthread_mutexattr_destroy(&attr);\n\t\treturn true;\n\t}\n\tpthread_mutexattr_destroy(&attr);\n#endif\n\treturn false;\n}\n\nvoid\nmtx_fini(mtx_t *mtx) {\n#ifdef _WIN32\n#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))\n#else\n\tpthread_mutex_destroy(&mtx->lock);\n#endif\n}\n\nvoid\nmtx_lock(mtx_t *mtx) {\n#ifdef _WIN32\n\tEnterCriticalSection(&mtx->lock);\n#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))\n\tos_unfair_lock_lock(&mtx->lock);\n#else\n\tpthread_mutex_lock(&mtx->lock);\n#endif\n}\n\nvoid\nmtx_unlock(mtx_t *mtx) {\n#ifdef _WIN32\n\tLeaveCriticalSection(&mtx->lock);\n#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))\n\tos_unfair_lock_unlock(&mtx->lock);\n#else\n\tpthread_mutex_unlock(&mtx->lock);\n#endif\n}\n"
  },
  {
    "path": "deps/jemalloc/test/src/test.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n/* Test status state. */\n\nstatic unsigned\t\ttest_count = 0;\nstatic test_status_t\ttest_counts[test_status_count] = {0, 0, 0};\nstatic test_status_t\ttest_status = test_status_pass;\nstatic const char *\ttest_name = \"\";\n\n/* Reentrancy testing helpers. */\n\n#define NUM_REENTRANT_ALLOCS 20\ntypedef enum {\n\tnon_reentrant = 0,\n\tlibc_reentrant = 1,\n\tarena_new_reentrant = 2\n} reentrancy_t;\nstatic reentrancy_t reentrancy;\n\nstatic bool libc_hook_ran = false;\nstatic bool arena_new_hook_ran = false;\n\nstatic const char *\nreentrancy_t_str(reentrancy_t r) {\n\tswitch (r) {\n\tcase non_reentrant:\n\t\treturn \"non-reentrant\";\n\tcase libc_reentrant:\n\t\treturn \"libc-reentrant\";\n\tcase arena_new_reentrant:\n\t\treturn \"arena_new-reentrant\";\n\tdefault:\n\t\tunreachable();\n\t}\n}\n\nstatic void\ndo_hook(bool *hook_ran, void (**hook)()) {\n\t*hook_ran = true;\n\t*hook = NULL;\n\n\tsize_t alloc_size = 1;\n\tfor (int i = 0; i < NUM_REENTRANT_ALLOCS; i++) {\n\t\tfree(malloc(alloc_size));\n\t\talloc_size *= 2;\n\t}\n}\n\nstatic void\nlibc_reentrancy_hook() {\n\tdo_hook(&libc_hook_ran, &test_hooks_libc_hook);\n}\n\nstatic void\narena_new_reentrancy_hook() {\n\tdo_hook(&arena_new_hook_ran, &test_hooks_arena_new_hook);\n}\n\n/* Actual test infrastructure. */\nbool\ntest_is_reentrant() {\n\treturn reentrancy != non_reentrant;\n}\n\nJEMALLOC_FORMAT_PRINTF(1, 2)\nvoid\ntest_skip(const char *format, ...) {\n\tva_list ap;\n\n\tva_start(ap, format);\n\tmalloc_vcprintf(NULL, NULL, format, ap);\n\tva_end(ap);\n\tmalloc_printf(\"\\n\");\n\ttest_status = test_status_skip;\n}\n\nJEMALLOC_FORMAT_PRINTF(1, 2)\nvoid\ntest_fail(const char *format, ...) {\n\tva_list ap;\n\n\tva_start(ap, format);\n\tmalloc_vcprintf(NULL, NULL, format, ap);\n\tva_end(ap);\n\tmalloc_printf(\"\\n\");\n\ttest_status = test_status_fail;\n}\n\nstatic const char *\ntest_status_string(test_status_t test_status) {\n\tswitch (test_status) {\n\tcase test_status_pass: return \"pass\";\n\tcase test_status_skip: return \"skip\";\n\tcase test_status_fail: return \"fail\";\n\tdefault: not_reached();\n\t}\n}\n\nvoid\np_test_init(const char *name) {\n\ttest_count++;\n\ttest_status = test_status_pass;\n\ttest_name = name;\n}\n\nvoid\np_test_fini(void) {\n\ttest_counts[test_status]++;\n\tmalloc_printf(\"%s (%s): %s\\n\", test_name, reentrancy_t_str(reentrancy),\n\t    test_status_string(test_status));\n}\n\nstatic void\ncheck_global_slow(test_status_t *status) {\n#ifdef JEMALLOC_UNIT_TEST\n\t/*\n\t * This check needs to peek into tsd internals, which is why it's only\n\t * exposed in unit tests.\n\t */\n\tif (tsd_global_slow()) {\n\t\tmalloc_printf(\"Testing increased global slow count\\n\");\n\t\t*status = test_status_fail;\n\t}\n#endif\n}\n\nstatic test_status_t\np_test_impl(bool do_malloc_init, bool do_reentrant, test_t *t, va_list ap) {\n\ttest_status_t ret;\n\n\tif (do_malloc_init) {\n\t\t/*\n\t\t * Make sure initialization occurs prior to running tests.\n\t\t * Tests are special because they may use internal facilities\n\t\t * prior to triggering initialization as a side effect of\n\t\t * calling into the public API.\n\t\t */\n\t\tif (nallocx(1, 0) == 0) {\n\t\t\tmalloc_printf(\"Initialization error\");\n\t\t\treturn test_status_fail;\n\t\t}\n\t}\n\n\tret = test_status_pass;\n\tfor (; t != NULL; t = va_arg(ap, test_t *)) {\n\t\t/* Non-reentrant run. */\n\t\treentrancy = non_reentrant;\n\t\ttest_hooks_arena_new_hook = test_hooks_libc_hook = NULL;\n\t\tt();\n\t\tif (test_status > ret) {\n\t\t\tret = test_status;\n\t\t}\n\t\tcheck_global_slow(&ret);\n\t\t/* Reentrant run. */\n\t\tif (do_reentrant) {\n\t\t\treentrancy = libc_reentrant;\n\t\t\ttest_hooks_arena_new_hook = NULL;\n\t\t\ttest_hooks_libc_hook = &libc_reentrancy_hook;\n\t\t\tt();\n\t\t\tif (test_status > ret) {\n\t\t\t\tret = test_status;\n\t\t\t}\n\t\t\tcheck_global_slow(&ret);\n\n\t\t\treentrancy = arena_new_reentrant;\n\t\t\ttest_hooks_libc_hook = NULL;\n\t\t\ttest_hooks_arena_new_hook = &arena_new_reentrancy_hook;\n\t\t\tt();\n\t\t\tif (test_status > ret) {\n\t\t\t\tret = test_status;\n\t\t\t}\n\t\t\tcheck_global_slow(&ret);\n\t\t}\n\t}\n\n\tmalloc_printf(\"--- %s: %u/%u, %s: %u/%u, %s: %u/%u ---\\n\",\n\t    test_status_string(test_status_pass),\n\t    test_counts[test_status_pass], test_count,\n\t    test_status_string(test_status_skip),\n\t    test_counts[test_status_skip], test_count,\n\t    test_status_string(test_status_fail),\n\t    test_counts[test_status_fail], test_count);\n\n\treturn ret;\n}\n\ntest_status_t\np_test(test_t *t, ...) {\n\ttest_status_t ret;\n\tva_list ap;\n\n\tret = test_status_pass;\n\tva_start(ap, t);\n\tret = p_test_impl(true, true, t, ap);\n\tva_end(ap);\n\n\treturn ret;\n}\n\ntest_status_t\np_test_no_reentrancy(test_t *t, ...) {\n\ttest_status_t ret;\n\tva_list ap;\n\n\tret = test_status_pass;\n\tva_start(ap, t);\n\tret = p_test_impl(true, false, t, ap);\n\tva_end(ap);\n\n\treturn ret;\n}\n\ntest_status_t\np_test_no_malloc_init(test_t *t, ...) {\n\ttest_status_t ret;\n\tva_list ap;\n\n\tret = test_status_pass;\n\tva_start(ap, t);\n\t/*\n\t * We also omit reentrancy from bootstrapping tests, since we don't\n\t * (yet) care about general reentrancy during bootstrapping.\n\t */\n\tret = p_test_impl(false, false, t, ap);\n\tva_end(ap);\n\n\treturn ret;\n}\n\nvoid\np_test_fail(const char *prefix, const char *message) {\n\tmalloc_cprintf(NULL, NULL, \"%s%s\\n\", prefix, message);\n\ttest_status = test_status_fail;\n}\n"
  },
  {
    "path": "deps/jemalloc/test/src/thd.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#ifdef _WIN32\nvoid\nthd_create(thd_t *thd, void *(*proc)(void *), void *arg) {\n\tLPTHREAD_START_ROUTINE routine = (LPTHREAD_START_ROUTINE)proc;\n\t*thd = CreateThread(NULL, 0, routine, arg, 0, NULL);\n\tif (*thd == NULL) {\n\t\ttest_fail(\"Error in CreateThread()\\n\");\n\t}\n}\n\nvoid\nthd_join(thd_t thd, void **ret) {\n\tif (WaitForSingleObject(thd, INFINITE) == WAIT_OBJECT_0 && ret) {\n\t\tDWORD exit_code;\n\t\tGetExitCodeThread(thd, (LPDWORD) &exit_code);\n\t\t*ret = (void *)(uintptr_t)exit_code;\n\t}\n}\n\n#else\nvoid\nthd_create(thd_t *thd, void *(*proc)(void *), void *arg) {\n\tif (pthread_create(thd, NULL, proc, arg) != 0) {\n\t\ttest_fail(\"Error in pthread_create()\\n\");\n\t}\n}\n\nvoid\nthd_join(thd_t thd, void **ret) {\n\tpthread_join(thd, ret);\n}\n#endif\n"
  },
  {
    "path": "deps/jemalloc/test/src/timer.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nvoid\ntimer_start(timedelta_t *timer) {\n\tnstime_init(&timer->t0, 0);\n\tnstime_update(&timer->t0);\n}\n\nvoid\ntimer_stop(timedelta_t *timer) {\n\tnstime_copy(&timer->t1, &timer->t0);\n\tnstime_update(&timer->t1);\n}\n\nuint64_t\ntimer_usec(const timedelta_t *timer) {\n\tnstime_t delta;\n\n\tnstime_copy(&delta, &timer->t1);\n\tnstime_subtract(&delta, &timer->t0);\n\treturn nstime_ns(&delta) / 1000;\n}\n\nvoid\ntimer_ratio(timedelta_t *a, timedelta_t *b, char *buf, size_t buflen) {\n\tuint64_t t0 = timer_usec(a);\n\tuint64_t t1 = timer_usec(b);\n\tuint64_t mult;\n\tsize_t i = 0;\n\tsize_t j, n;\n\n\t/* Whole. */\n\tn = malloc_snprintf(&buf[i], buflen-i, \"%\"FMTu64, t0 / t1);\n\ti += n;\n\tif (i >= buflen) {\n\t\treturn;\n\t}\n\tmult = 1;\n\tfor (j = 0; j < n; j++) {\n\t\tmult *= 10;\n\t}\n\n\t/* Decimal. */\n\tn = malloc_snprintf(&buf[i], buflen-i, \".\");\n\ti += n;\n\n\t/* Fraction. */\n\twhile (i < buflen-1) {\n\t\tuint64_t round = (i+1 == buflen-1 && ((t0 * mult * 10 / t1) % 10\n\t\t    >= 5)) ? 1 : 0;\n\t\tn = malloc_snprintf(&buf[i], buflen-i,\n\t\t    \"%\"FMTu64, (t0 * mult / t1) % 10 + round);\n\t\ti += n;\n\t\tmult *= 10;\n\t}\n}\n"
  },
  {
    "path": "deps/jemalloc/test/stress/hookbench.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic void\nnoop_alloc_hook(void *extra, hook_alloc_t type, void *result,\n    uintptr_t result_raw, uintptr_t args_raw[3]) {\n}\n\nstatic void\nnoop_dalloc_hook(void *extra, hook_dalloc_t type, void *address,\n    uintptr_t args_raw[3]) {\n}\n\nstatic void\nnoop_expand_hook(void *extra, hook_expand_t type, void *address,\n    size_t old_usize, size_t new_usize, uintptr_t result_raw,\n    uintptr_t args_raw[4]) {\n}\n\nstatic void\nmalloc_free_loop(int iters) {\n\tfor (int i = 0; i < iters; i++) {\n\t\tvoid *p = mallocx(1, 0);\n\t\tfree(p);\n\t}\n}\n\nstatic void\ntest_hooked(int iters) {\n\thooks_t hooks = {&noop_alloc_hook, &noop_dalloc_hook, &noop_expand_hook,\n\t\tNULL};\n\n\tint err;\n\tvoid *handles[HOOK_MAX];\n\tsize_t sz = sizeof(handles[0]);\n\n\tfor (int i = 0; i < HOOK_MAX; i++) {\n\t\terr = mallctl(\"experimental.hooks.install\", &handles[i],\n\t\t    &sz, &hooks, sizeof(hooks));\n\t\tassert(err == 0);\n\n\t\ttimedelta_t timer;\n\t\ttimer_start(&timer);\n\t\tmalloc_free_loop(iters);\n\t\ttimer_stop(&timer);\n\t\tmalloc_printf(\"With %d hook%s: %\"FMTu64\"us\\n\", i + 1,\n\t\t    i + 1 == 1 ? \"\" : \"s\", timer_usec(&timer));\n\t}\n\tfor (int i = 0; i < HOOK_MAX; i++) {\n\t\terr = mallctl(\"experimental.hooks.remove\", NULL, NULL,\n\t\t    &handles[i], sizeof(handles[i]));\n\t\tassert(err == 0);\n\t}\n}\n\nstatic void\ntest_unhooked(int iters) {\n\ttimedelta_t timer;\n\ttimer_start(&timer);\n\tmalloc_free_loop(iters);\n\ttimer_stop(&timer);\n\n\tmalloc_printf(\"Without hooks: %\"FMTu64\"us\\n\", timer_usec(&timer));\n}\n\nint\nmain(void) {\n\t/* Initialize */\n\tfree(mallocx(1, 0));\n\tint iters = 10 * 1000 * 1000;\n\tmalloc_printf(\"Benchmarking hooks with %d iterations:\\n\", iters);\n\ttest_hooked(iters);\n\ttest_unhooked(iters);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/stress/microbench.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic inline void\ntime_func(timedelta_t *timer, uint64_t nwarmup, uint64_t niter,\n    void (*func)(void)) {\n\tuint64_t i;\n\n\tfor (i = 0; i < nwarmup; i++) {\n\t\tfunc();\n\t}\n\ttimer_start(timer);\n\tfor (i = 0; i < niter; i++) {\n\t\tfunc();\n\t}\n\ttimer_stop(timer);\n}\n\nvoid\ncompare_funcs(uint64_t nwarmup, uint64_t niter, const char *name_a,\n    void (*func_a), const char *name_b, void (*func_b)) {\n\ttimedelta_t timer_a, timer_b;\n\tchar ratio_buf[6];\n\tvoid *p;\n\n\tp = mallocx(1, 0);\n\tif (p == NULL) {\n\t\ttest_fail(\"Unexpected mallocx() failure\");\n\t\treturn;\n\t}\n\n\ttime_func(&timer_a, nwarmup, niter, func_a);\n\ttime_func(&timer_b, nwarmup, niter, func_b);\n\n\ttimer_ratio(&timer_a, &timer_b, ratio_buf, sizeof(ratio_buf));\n\tmalloc_printf(\"%\"FMTu64\" iterations, %s=%\"FMTu64\"us, \"\n\t    \"%s=%\"FMTu64\"us, ratio=1:%s\\n\",\n\t    niter, name_a, timer_usec(&timer_a), name_b, timer_usec(&timer_b),\n\t    ratio_buf);\n\n\tdallocx(p, 0);\n}\n\nstatic void\nmalloc_free(void) {\n\t/* The compiler can optimize away free(malloc(1))! */\n\tvoid *p = malloc(1);\n\tif (p == NULL) {\n\t\ttest_fail(\"Unexpected malloc() failure\");\n\t\treturn;\n\t}\n\tfree(p);\n}\n\nstatic void\nmallocx_free(void) {\n\tvoid *p = mallocx(1, 0);\n\tif (p == NULL) {\n\t\ttest_fail(\"Unexpected mallocx() failure\");\n\t\treturn;\n\t}\n\tfree(p);\n}\n\nTEST_BEGIN(test_malloc_vs_mallocx) {\n\tcompare_funcs(10*1000*1000, 100*1000*1000, \"malloc\",\n\t    malloc_free, \"mallocx\", mallocx_free);\n}\nTEST_END\n\nstatic void\nmalloc_dallocx(void) {\n\tvoid *p = malloc(1);\n\tif (p == NULL) {\n\t\ttest_fail(\"Unexpected malloc() failure\");\n\t\treturn;\n\t}\n\tdallocx(p, 0);\n}\n\nstatic void\nmalloc_sdallocx(void) {\n\tvoid *p = malloc(1);\n\tif (p == NULL) {\n\t\ttest_fail(\"Unexpected malloc() failure\");\n\t\treturn;\n\t}\n\tsdallocx(p, 1, 0);\n}\n\nTEST_BEGIN(test_free_vs_dallocx) {\n\tcompare_funcs(10*1000*1000, 100*1000*1000, \"free\", malloc_free,\n\t    \"dallocx\", malloc_dallocx);\n}\nTEST_END\n\nTEST_BEGIN(test_dallocx_vs_sdallocx) {\n\tcompare_funcs(10*1000*1000, 100*1000*1000, \"dallocx\", malloc_dallocx,\n\t    \"sdallocx\", malloc_sdallocx);\n}\nTEST_END\n\nstatic void\nmalloc_mus_free(void) {\n\tvoid *p;\n\n\tp = malloc(1);\n\tif (p == NULL) {\n\t\ttest_fail(\"Unexpected malloc() failure\");\n\t\treturn;\n\t}\n\tmalloc_usable_size(p);\n\tfree(p);\n}\n\nstatic void\nmalloc_sallocx_free(void) {\n\tvoid *p;\n\n\tp = malloc(1);\n\tif (p == NULL) {\n\t\ttest_fail(\"Unexpected malloc() failure\");\n\t\treturn;\n\t}\n\tif (sallocx(p, 0) < 1) {\n\t\ttest_fail(\"Unexpected sallocx() failure\");\n\t}\n\tfree(p);\n}\n\nTEST_BEGIN(test_mus_vs_sallocx) {\n\tcompare_funcs(10*1000*1000, 100*1000*1000, \"malloc_usable_size\",\n\t    malloc_mus_free, \"sallocx\", malloc_sallocx_free);\n}\nTEST_END\n\nstatic void\nmalloc_nallocx_free(void) {\n\tvoid *p;\n\n\tp = malloc(1);\n\tif (p == NULL) {\n\t\ttest_fail(\"Unexpected malloc() failure\");\n\t\treturn;\n\t}\n\tif (nallocx(1, 0) < 1) {\n\t\ttest_fail(\"Unexpected nallocx() failure\");\n\t}\n\tfree(p);\n}\n\nTEST_BEGIN(test_sallocx_vs_nallocx) {\n\tcompare_funcs(10*1000*1000, 100*1000*1000, \"sallocx\",\n\t    malloc_sallocx_free, \"nallocx\", malloc_nallocx_free);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test_no_reentrancy(\n\t    test_malloc_vs_mallocx,\n\t    test_free_vs_dallocx,\n\t    test_dallocx_vs_sdallocx,\n\t    test_mus_vs_sallocx,\n\t    test_sallocx_vs_nallocx);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/test.sh.in",
    "content": "#!/bin/sh\n\ncase @abi@ in\n  macho)\n    export DYLD_FALLBACK_LIBRARY_PATH=\"@objroot@lib\"\n    ;;\n  pecoff)\n    export PATH=\"${PATH}:@objroot@lib\"\n    ;;\n  *)\n    ;;\nesac\n\n# Make a copy of the @JEMALLOC_CPREFIX@MALLOC_CONF passed in to this script, so\n# it can be repeatedly concatenated with per test settings.\nexport MALLOC_CONF_ALL=${@JEMALLOC_CPREFIX@MALLOC_CONF}\n# Concatenate the individual test's MALLOC_CONF and MALLOC_CONF_ALL.\nexport_malloc_conf() {\n  if [ \"x${MALLOC_CONF}\" != \"x\" -a \"x${MALLOC_CONF_ALL}\" != \"x\" ] ; then\n    export @JEMALLOC_CPREFIX@MALLOC_CONF=\"${MALLOC_CONF},${MALLOC_CONF_ALL}\"\n  else\n    export @JEMALLOC_CPREFIX@MALLOC_CONF=\"${MALLOC_CONF}${MALLOC_CONF_ALL}\"\n  fi\n}\n\n# Corresponds to test_status_t.\npass_code=0\nskip_code=1\nfail_code=2\n\npass_count=0\nskip_count=0\nfail_count=0\nfor t in $@; do\n  if [ $pass_count -ne 0 -o $skip_count -ne 0 -o $fail_count != 0 ] ; then\n    echo\n  fi\n  echo \"=== ${t} ===\"\n  if [ -e \"@srcroot@${t}.sh\" ] ; then\n    # Source the shell script corresponding to the test in a subshell and\n    # execute the test.  This allows the shell script to set MALLOC_CONF, which\n    # is then used to set @JEMALLOC_CPREFIX@MALLOC_CONF (thus allowing the\n    # per test shell script to ignore the @JEMALLOC_CPREFIX@ detail).\n    enable_fill=@enable_fill@ \\\n    enable_prof=@enable_prof@ \\\n    . @srcroot@${t}.sh && \\\n    export_malloc_conf && \\\n    $JEMALLOC_TEST_PREFIX ${t}@exe@ @abs_srcroot@ @abs_objroot@\n  else\n    export MALLOC_CONF= && \\\n    export_malloc_conf && \\\n    $JEMALLOC_TEST_PREFIX ${t}@exe@ @abs_srcroot@ @abs_objroot@\n  fi\n  result_code=$?\n  case ${result_code} in\n    ${pass_code})\n      pass_count=$((pass_count+1))\n      ;;\n    ${skip_code})\n      skip_count=$((skip_count+1))\n      ;;\n    ${fail_code})\n      fail_count=$((fail_count+1))\n      ;;\n    *)\n      echo \"Test harness error: ${t} w/ MALLOC_CONF=\\\"${MALLOC_CONF}\\\"\" 1>&2\n      echo \"Use prefix to debug, e.g. JEMALLOC_TEST_PREFIX=\\\"gdb --args\\\" sh test/test.sh ${t}\" 1>&2\n      exit 1\n  esac\ndone\n\ntotal_count=`expr ${pass_count} + ${skip_count} + ${fail_count}`\necho\necho \"Test suite summary: pass: ${pass_count}/${total_count}, skip: ${skip_count}/${total_count}, fail: ${fail_count}/${total_count}\"\n\nif [ ${fail_count} -eq 0 ] ; then\n  exit 0\nelse\n  exit 1\nfi\n"
  },
  {
    "path": "deps/jemalloc/test/unit/SFMT.c",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#include \"test/jemalloc_test.h\"\n\n#define BLOCK_SIZE 10000\n#define BLOCK_SIZE64 (BLOCK_SIZE / 2)\n#define COUNT_1 1000\n#define COUNT_2 700\n\nstatic const uint32_t init_gen_rand_32_expected[] = {\n\t3440181298U, 1564997079U, 1510669302U, 2930277156U, 1452439940U,\n\t3796268453U,  423124208U, 2143818589U, 3827219408U, 2987036003U,\n\t2674978610U, 1536842514U, 2027035537U, 2534897563U, 1686527725U,\n\t 545368292U, 1489013321U, 1370534252U, 4231012796U, 3994803019U,\n\t1764869045U,  824597505U,  862581900U, 2469764249U,  812862514U,\n\t 359318673U,  116957936U, 3367389672U, 2327178354U, 1898245200U,\n\t3206507879U, 2378925033U, 1040214787U, 2524778605U, 3088428700U,\n\t1417665896U,  964324147U, 2282797708U, 2456269299U,  313400376U,\n\t2245093271U, 1015729427U, 2694465011U, 3246975184U, 1992793635U,\n\t 463679346U, 3721104591U, 3475064196U,  856141236U, 1499559719U,\n\t3522818941U, 3721533109U, 1954826617U, 1282044024U, 1543279136U,\n\t1301863085U, 2669145051U, 4221477354U, 3896016841U, 3392740262U,\n\t 462466863U, 1037679449U, 1228140306U,  922298197U, 1205109853U,\n\t1872938061U, 3102547608U, 2742766808U, 1888626088U, 4028039414U,\n\t 157593879U, 1136901695U, 4038377686U, 3572517236U, 4231706728U,\n\t2997311961U, 1189931652U, 3981543765U, 2826166703U,   87159245U,\n\t1721379072U, 3897926942U, 1790395498U, 2569178939U, 1047368729U,\n\t2340259131U, 3144212906U, 2301169789U, 2442885464U, 3034046771U,\n\t3667880593U, 3935928400U, 2372805237U, 1666397115U, 2460584504U,\n\t 513866770U, 3810869743U, 2147400037U, 2792078025U, 2941761810U,\n\t3212265810U,  984692259U,  346590253U, 1804179199U, 3298543443U,\n\t 750108141U, 2880257022U,  243310542U, 1869036465U, 1588062513U,\n\t2983949551U, 1931450364U, 4034505847U, 2735030199U, 1628461061U,\n\t2539522841U,  127965585U, 3992448871U,  913388237U,  559130076U,\n\t1202933193U, 4087643167U, 2590021067U, 2256240196U, 1746697293U,\n\t1013913783U, 1155864921U, 2715773730U,  915061862U, 1948766573U,\n\t2322882854U, 3761119102U, 1343405684U, 3078711943U, 3067431651U,\n\t3245156316U, 3588354584U, 3484623306U, 3899621563U, 4156689741U,\n\t3237090058U, 3880063844U,  862416318U, 4039923869U, 2303788317U,\n\t3073590536U,  701653667U, 2131530884U, 3169309950U, 2028486980U,\n\t 747196777U, 3620218225U,  432016035U, 1449580595U, 2772266392U,\n\t 444224948U, 1662832057U, 3184055582U, 3028331792U, 1861686254U,\n\t1104864179U,  342430307U, 1350510923U, 3024656237U, 1028417492U,\n\t2870772950U,  290847558U, 3675663500U,  508431529U, 4264340390U,\n\t2263569913U, 1669302976U,  519511383U, 2706411211U, 3764615828U,\n\t3883162495U, 4051445305U, 2412729798U, 3299405164U, 3991911166U,\n\t2348767304U, 2664054906U, 3763609282U,  593943581U, 3757090046U,\n\t2075338894U, 2020550814U, 4287452920U, 4290140003U, 1422957317U,\n\t2512716667U, 2003485045U, 2307520103U, 2288472169U, 3940751663U,\n\t4204638664U, 2892583423U, 1710068300U, 3904755993U, 2363243951U,\n\t3038334120U,  547099465U,  771105860U, 3199983734U, 4282046461U,\n\t2298388363U,  934810218U, 2837827901U, 3952500708U, 2095130248U,\n\t3083335297U,   26885281U, 3932155283U, 1531751116U, 1425227133U,\n\t 495654159U, 3279634176U, 3855562207U, 3957195338U, 4159985527U,\n\t 893375062U, 1875515536U, 1327247422U, 3754140693U, 1028923197U,\n\t1729880440U,  805571298U,  448971099U, 2726757106U, 2749436461U,\n\t2485987104U,  175337042U, 3235477922U, 3882114302U, 2020970972U,\n\t 943926109U, 2762587195U, 1904195558U, 3452650564U,  108432281U,\n\t3893463573U, 3977583081U, 2636504348U, 1110673525U, 3548479841U,\n\t4258854744U,  980047703U, 4057175418U, 3890008292U,  145653646U,\n\t3141868989U, 3293216228U, 1194331837U, 1254570642U, 3049934521U,\n\t2868313360U, 2886032750U, 1110873820U,  279553524U, 3007258565U,\n\t1104807822U, 3186961098U,  315764646U, 2163680838U, 3574508994U,\n\t3099755655U,  191957684U, 3642656737U, 3317946149U, 3522087636U,\n\t 444526410U,  779157624U, 1088229627U, 1092460223U, 1856013765U,\n\t3659877367U,  368270451U,  503570716U, 3000984671U, 2742789647U,\n\t 928097709U, 2914109539U,  308843566U, 2816161253U, 3667192079U,\n\t2762679057U, 3395240989U, 2928925038U, 1491465914U, 3458702834U,\n\t3787782576U, 2894104823U, 1296880455U, 1253636503U,  989959407U,\n\t2291560361U, 2776790436U, 1913178042U, 1584677829U,  689637520U,\n\t1898406878U,  688391508U, 3385234998U,  845493284U, 1943591856U,\n\t2720472050U,  222695101U, 1653320868U, 2904632120U, 4084936008U,\n\t1080720688U, 3938032556U,  387896427U, 2650839632U,   99042991U,\n\t1720913794U, 1047186003U, 1877048040U, 2090457659U,  517087501U,\n\t4172014665U, 2129713163U, 2413533132U, 2760285054U, 4129272496U,\n\t1317737175U, 2309566414U, 2228873332U, 3889671280U, 1110864630U,\n\t3576797776U, 2074552772U,  832002644U, 3097122623U, 2464859298U,\n\t2679603822U, 1667489885U, 3237652716U, 1478413938U, 1719340335U,\n\t2306631119U,  639727358U, 3369698270U,  226902796U, 2099920751U,\n\t1892289957U, 2201594097U, 3508197013U, 3495811856U, 3900381493U,\n\t 841660320U, 3974501451U, 3360949056U, 1676829340U,  728899254U,\n\t2047809627U, 2390948962U,  670165943U, 3412951831U, 4189320049U,\n\t1911595255U, 2055363086U,  507170575U,  418219594U, 4141495280U,\n\t2692088692U, 4203630654U, 3540093932U,  791986533U, 2237921051U,\n\t2526864324U, 2956616642U, 1394958700U, 1983768223U, 1893373266U,\n\t 591653646U,  228432437U, 1611046598U, 3007736357U, 1040040725U,\n\t2726180733U, 2789804360U, 4263568405U,  829098158U, 3847722805U,\n\t1123578029U, 1804276347U,  997971319U, 4203797076U, 4185199713U,\n\t2811733626U, 2343642194U, 2985262313U, 1417930827U, 3759587724U,\n\t1967077982U, 1585223204U, 1097475516U, 1903944948U,  740382444U,\n\t1114142065U, 1541796065U, 1718384172U, 1544076191U, 1134682254U,\n\t3519754455U, 2866243923U,  341865437U,  645498576U, 2690735853U,\n\t1046963033U, 2493178460U, 1187604696U, 1619577821U,  488503634U,\n\t3255768161U, 2306666149U, 1630514044U, 2377698367U, 2751503746U,\n\t3794467088U, 1796415981U, 3657173746U,  409136296U, 1387122342U,\n\t1297726519U,  219544855U, 4270285558U,  437578827U, 1444698679U,\n\t2258519491U,  963109892U, 3982244073U, 3351535275U,  385328496U,\n\t1804784013U,  698059346U, 3920535147U,  708331212U,  784338163U,\n\t 785678147U, 1238376158U, 1557298846U, 2037809321U,  271576218U,\n\t4145155269U, 1913481602U, 2763691931U,  588981080U, 1201098051U,\n\t3717640232U, 1509206239U,  662536967U, 3180523616U, 1133105435U,\n\t2963500837U, 2253971215U, 3153642623U, 1066925709U, 2582781958U,\n\t3034720222U, 1090798544U, 2942170004U, 4036187520U,  686972531U,\n\t2610990302U, 2641437026U, 1837562420U,  722096247U, 1315333033U,\n\t2102231203U, 3402389208U, 3403698140U, 1312402831U, 2898426558U,\n\t 814384596U,  385649582U, 1916643285U, 1924625106U, 2512905582U,\n\t2501170304U, 4275223366U, 2841225246U, 1467663688U, 3563567847U,\n\t2969208552U,  884750901U,  102992576U,  227844301U, 3681442994U,\n\t3502881894U, 4034693299U, 1166727018U, 1697460687U, 1737778332U,\n\t1787161139U, 1053003655U, 1215024478U, 2791616766U, 2525841204U,\n\t1629323443U,    3233815U, 2003823032U, 3083834263U, 2379264872U,\n\t3752392312U, 1287475550U, 3770904171U, 3004244617U, 1502117784U,\n\t 918698423U, 2419857538U, 3864502062U, 1751322107U, 2188775056U,\n\t4018728324U,  983712955U,  440071928U, 3710838677U, 2001027698U,\n\t3994702151U,   22493119U, 3584400918U, 3446253670U, 4254789085U,\n\t1405447860U, 1240245579U, 1800644159U, 1661363424U, 3278326132U,\n\t3403623451U,   67092802U, 2609352193U, 3914150340U, 1814842761U,\n\t3610830847U,  591531412U, 3880232807U, 1673505890U, 2585326991U,\n\t1678544474U, 3148435887U, 3457217359U, 1193226330U, 2816576908U,\n\t 154025329U,  121678860U, 1164915738U,  973873761U,  269116100U,\n\t  52087970U,  744015362U,  498556057U,   94298882U, 1563271621U,\n\t2383059628U, 4197367290U, 3958472990U, 2592083636U, 2906408439U,\n\t1097742433U, 3924840517U,  264557272U, 2292287003U, 3203307984U,\n\t4047038857U, 3820609705U, 2333416067U, 1839206046U, 3600944252U,\n\t3412254904U,  583538222U, 2390557166U, 4140459427U, 2810357445U,\n\t 226777499U, 2496151295U, 2207301712U, 3283683112U,  611630281U,\n\t1933218215U, 3315610954U, 3889441987U, 3719454256U, 3957190521U,\n\t1313998161U, 2365383016U, 3146941060U, 1801206260U,  796124080U,\n\t2076248581U, 1747472464U, 3254365145U,  595543130U, 3573909503U,\n\t3758250204U, 2020768540U, 2439254210U,   93368951U, 3155792250U,\n\t2600232980U, 3709198295U, 3894900440U, 2971850836U, 1578909644U,\n\t1443493395U, 2581621665U, 3086506297U, 2443465861U,  558107211U,\n\t1519367835U,  249149686U,  908102264U, 2588765675U, 1232743965U,\n\t1001330373U, 3561331654U, 2259301289U, 1564977624U, 3835077093U,\n\t 727244906U, 4255738067U, 1214133513U, 2570786021U, 3899704621U,\n\t1633861986U, 1636979509U, 1438500431U,   58463278U, 2823485629U,\n\t2297430187U, 2926781924U, 3371352948U, 1864009023U, 2722267973U,\n\t1444292075U,  437703973U, 1060414512U,  189705863U,  910018135U,\n\t4077357964U,  884213423U, 2644986052U, 3973488374U, 1187906116U,\n\t2331207875U,  780463700U, 3713351662U, 3854611290U,  412805574U,\n\t2978462572U, 2176222820U,  829424696U, 2790788332U, 2750819108U,\n\t1594611657U, 3899878394U, 3032870364U, 1702887682U, 1948167778U,\n\t  14130042U,  192292500U,  947227076U,   90719497U, 3854230320U,\n\t 784028434U, 2142399787U, 1563449646U, 2844400217U,  819143172U,\n\t2883302356U, 2328055304U, 1328532246U, 2603885363U, 3375188924U,\n\t 933941291U, 3627039714U, 2129697284U, 2167253953U, 2506905438U,\n\t1412424497U, 2981395985U, 1418359660U, 2925902456U,   52752784U,\n\t3713667988U, 3924669405U,  648975707U, 1145520213U, 4018650664U,\n\t3805915440U, 2380542088U, 2013260958U, 3262572197U, 2465078101U,\n\t1114540067U, 3728768081U, 2396958768U,  590672271U,  904818725U,\n\t4263660715U,  700754408U, 1042601829U, 4094111823U, 4274838909U,\n\t2512692617U, 2774300207U, 2057306915U, 3470942453U,   99333088U,\n\t1142661026U, 2889931380U,   14316674U, 2201179167U,  415289459U,\n\t 448265759U, 3515142743U, 3254903683U,  246633281U, 1184307224U,\n\t2418347830U, 2092967314U, 2682072314U, 2558750234U, 2000352263U,\n\t1544150531U,  399010405U, 1513946097U,  499682937U,  461167460U,\n\t3045570638U, 1633669705U,  851492362U, 4052801922U, 2055266765U,\n\t 635556996U,  368266356U, 2385737383U, 3218202352U, 2603772408U,\n\t 349178792U,  226482567U, 3102426060U, 3575998268U, 2103001871U,\n\t3243137071U,  225500688U, 1634718593U, 4283311431U, 4292122923U,\n\t3842802787U,  811735523U,  105712518U,  663434053U, 1855889273U,\n\t2847972595U, 1196355421U, 2552150115U, 4254510614U, 3752181265U,\n\t3430721819U, 3828705396U, 3436287905U, 3441964937U, 4123670631U,\n\t 353001539U,  459496439U, 3799690868U, 1293777660U, 2761079737U,\n\t 498096339U, 3398433374U, 4080378380U, 2304691596U, 2995729055U,\n\t4134660419U, 3903444024U, 3576494993U,  203682175U, 3321164857U,\n\t2747963611U,   79749085U, 2992890370U, 1240278549U, 1772175713U,\n\t2111331972U, 2655023449U, 1683896345U, 2836027212U, 3482868021U,\n\t2489884874U,  756853961U, 2298874501U, 4013448667U, 4143996022U,\n\t2948306858U, 4132920035U, 1283299272U,  995592228U, 3450508595U,\n\t1027845759U, 1766942720U, 3861411826U, 1446861231U,   95974993U,\n\t3502263554U, 1487532194U,  601502472U, 4129619129U,  250131773U,\n\t2050079547U, 3198903947U, 3105589778U, 4066481316U, 3026383978U,\n\t2276901713U,  365637751U, 2260718426U, 1394775634U, 1791172338U,\n\t2690503163U, 2952737846U, 1568710462U,  732623190U, 2980358000U,\n\t1053631832U, 1432426951U, 3229149635U, 1854113985U, 3719733532U,\n\t3204031934U,  735775531U,  107468620U, 3734611984U,  631009402U,\n\t3083622457U, 4109580626U,  159373458U, 1301970201U, 4132389302U,\n\t1293255004U,  847182752U, 4170022737U,   96712900U, 2641406755U,\n\t1381727755U,  405608287U, 4287919625U, 1703554290U, 3589580244U,\n\t2911403488U,    2166565U, 2647306451U, 2330535117U, 1200815358U,\n\t1165916754U,  245060911U, 4040679071U, 3684908771U, 2452834126U,\n\t2486872773U, 2318678365U, 2940627908U, 1837837240U, 3447897409U,\n\t4270484676U, 1495388728U, 3754288477U, 4204167884U, 1386977705U,\n\t2692224733U, 3076249689U, 4109568048U, 4170955115U, 4167531356U,\n\t4020189950U, 4261855038U, 3036907575U, 3410399885U, 3076395737U,\n\t1046178638U,  144496770U,  230725846U, 3349637149U,   17065717U,\n\t2809932048U, 2054581785U, 3608424964U, 3259628808U,  134897388U,\n\t3743067463U,  257685904U, 3795656590U, 1562468719U, 3589103904U,\n\t3120404710U,  254684547U, 2653661580U, 3663904795U, 2631942758U,\n\t1063234347U, 2609732900U, 2332080715U, 3521125233U, 1180599599U,\n\t1935868586U, 4110970440U,  296706371U, 2128666368U, 1319875791U,\n\t1570900197U, 3096025483U, 1799882517U, 1928302007U, 1163707758U,\n\t1244491489U, 3533770203U,  567496053U, 2757924305U, 2781639343U,\n\t2818420107U,  560404889U, 2619609724U, 4176035430U, 2511289753U,\n\t2521842019U, 3910553502U, 2926149387U, 3302078172U, 4237118867U,\n\t 330725126U,  367400677U,  888239854U,  545570454U, 4259590525U,\n\t 134343617U, 1102169784U, 1647463719U, 3260979784U, 1518840883U,\n\t3631537963U, 3342671457U, 1301549147U, 2083739356U,  146593792U,\n\t3217959080U,  652755743U, 2032187193U, 3898758414U, 1021358093U,\n\t4037409230U, 2176407931U, 3427391950U, 2883553603U,  985613827U,\n\t3105265092U, 3423168427U, 3387507672U,  467170288U, 2141266163U,\n\t3723870208U,  916410914U, 1293987799U, 2652584950U,  769160137U,\n\t3205292896U, 1561287359U, 1684510084U, 3136055621U, 3765171391U,\n\t 639683232U, 2639569327U, 1218546948U, 4263586685U, 3058215773U,\n\t2352279820U,  401870217U, 2625822463U, 1529125296U, 2981801895U,\n\t1191285226U, 4027725437U, 3432700217U, 4098835661U,  971182783U,\n\t2443861173U, 3881457123U, 3874386651U,  457276199U, 2638294160U,\n\t4002809368U,  421169044U, 1112642589U, 3076213779U, 3387033971U,\n\t2499610950U, 3057240914U, 1662679783U,  461224431U, 1168395933U\n};\nstatic const uint32_t init_by_array_32_expected[] = {\n\t2920711183U, 3885745737U, 3501893680U,  856470934U, 1421864068U,\n\t 277361036U, 1518638004U, 2328404353U, 3355513634U,   64329189U,\n\t1624587673U, 3508467182U, 2481792141U, 3706480799U, 1925859037U,\n\t2913275699U,  882658412U,  384641219U,  422202002U, 1873384891U,\n\t2006084383U, 3924929912U, 1636718106U, 3108838742U, 1245465724U,\n\t4195470535U,  779207191U, 1577721373U, 1390469554U, 2928648150U,\n\t 121399709U, 3170839019U, 4044347501U,  953953814U, 3821710850U,\n\t3085591323U, 3666535579U, 3577837737U, 2012008410U, 3565417471U,\n\t4044408017U,  433600965U, 1637785608U, 1798509764U,  860770589U,\n\t3081466273U, 3982393409U, 2451928325U, 3437124742U, 4093828739U,\n\t3357389386U, 2154596123U,  496568176U, 2650035164U, 2472361850U,\n\t   3438299U, 2150366101U, 1577256676U, 3802546413U, 1787774626U,\n\t4078331588U, 3706103141U,  170391138U, 3806085154U, 1680970100U,\n\t1961637521U, 3316029766U,  890610272U, 1453751581U, 1430283664U,\n\t3051057411U, 3597003186U,  542563954U, 3796490244U, 1690016688U,\n\t3448752238U,  440702173U,  347290497U, 1121336647U, 2540588620U,\n\t 280881896U, 2495136428U,  213707396U,   15104824U, 2946180358U,\n\t 659000016U,  566379385U, 2614030979U, 2855760170U,  334526548U,\n\t2315569495U, 2729518615U,  564745877U, 1263517638U, 3157185798U,\n\t1604852056U, 1011639885U, 2950579535U, 2524219188U,  312951012U,\n\t1528896652U, 1327861054U, 2846910138U, 3966855905U, 2536721582U,\n\t 855353911U, 1685434729U, 3303978929U, 1624872055U, 4020329649U,\n\t3164802143U, 1642802700U, 1957727869U, 1792352426U, 3334618929U,\n\t2631577923U, 3027156164U,  842334259U, 3353446843U, 1226432104U,\n\t1742801369U, 3552852535U, 3471698828U, 1653910186U, 3380330939U,\n\t2313782701U, 3351007196U, 2129839995U, 1800682418U, 4085884420U,\n\t1625156629U, 3669701987U,  615211810U, 3294791649U, 4131143784U,\n\t2590843588U, 3207422808U, 3275066464U,  561592872U, 3957205738U,\n\t3396578098U,   48410678U, 3505556445U, 1005764855U, 3920606528U,\n\t2936980473U, 2378918600U, 2404449845U, 1649515163U,  701203563U,\n\t3705256349U,   83714199U, 3586854132U,  922978446U, 2863406304U,\n\t3523398907U, 2606864832U, 2385399361U, 3171757816U, 4262841009U,\n\t3645837721U, 1169579486U, 3666433897U, 3174689479U, 1457866976U,\n\t3803895110U, 3346639145U, 1907224409U, 1978473712U, 1036712794U,\n\t 980754888U, 1302782359U, 1765252468U,  459245755U, 3728923860U,\n\t1512894209U, 2046491914U,  207860527U,  514188684U, 2288713615U,\n\t1597354672U, 3349636117U, 2357291114U, 3995796221U,  945364213U,\n\t1893326518U, 3770814016U, 1691552714U, 2397527410U,  967486361U,\n\t 776416472U, 4197661421U,  951150819U, 1852770983U, 4044624181U,\n\t1399439738U, 4194455275U, 2284037669U, 1550734958U, 3321078108U,\n\t1865235926U, 2912129961U, 2664980877U, 1357572033U, 2600196436U,\n\t2486728200U, 2372668724U, 1567316966U, 2374111491U, 1839843570U,\n\t  20815612U, 3727008608U, 3871996229U,  824061249U, 1932503978U,\n\t3404541726U,  758428924U, 2609331364U, 1223966026U, 1299179808U,\n\t 648499352U, 2180134401U,  880821170U, 3781130950U,  113491270U,\n\t1032413764U, 4185884695U, 2490396037U, 1201932817U, 4060951446U,\n\t4165586898U, 1629813212U, 2887821158U,  415045333U,  628926856U,\n\t2193466079U, 3391843445U, 2227540681U, 1907099846U, 2848448395U,\n\t1717828221U, 1372704537U, 1707549841U, 2294058813U, 2101214437U,\n\t2052479531U, 1695809164U, 3176587306U, 2632770465U,   81634404U,\n\t1603220563U,  644238487U,  302857763U,  897352968U, 2613146653U,\n\t1391730149U, 4245717312U, 4191828749U, 1948492526U, 2618174230U,\n\t3992984522U, 2178852787U, 3596044509U, 3445573503U, 2026614616U,\n\t 915763564U, 3415689334U, 2532153403U, 3879661562U, 2215027417U,\n\t3111154986U, 2929478371U,  668346391U, 1152241381U, 2632029711U,\n\t3004150659U, 2135025926U,  948690501U, 2799119116U, 4228829406U,\n\t1981197489U, 4209064138U,  684318751U, 3459397845U,  201790843U,\n\t4022541136U, 3043635877U,  492509624U, 3263466772U, 1509148086U,\n\t 921459029U, 3198857146U,  705479721U, 3835966910U, 3603356465U,\n\t 576159741U, 1742849431U,  594214882U, 2055294343U, 3634861861U,\n\t 449571793U, 3246390646U, 3868232151U, 1479156585U, 2900125656U,\n\t2464815318U, 3960178104U, 1784261920U,   18311476U, 3627135050U,\n\t 644609697U,  424968996U,  919890700U, 2986824110U,  816423214U,\n\t4003562844U, 1392714305U, 1757384428U, 2569030598U,  995949559U,\n\t3875659880U, 2933807823U, 2752536860U, 2993858466U, 4030558899U,\n\t2770783427U, 2775406005U, 2777781742U, 1931292655U,  472147933U,\n\t3865853827U, 2726470545U, 2668412860U, 2887008249U,  408979190U,\n\t3578063323U, 3242082049U, 1778193530U,   27981909U, 2362826515U,\n\t 389875677U, 1043878156U,  581653903U, 3830568952U,  389535942U,\n\t3713523185U, 2768373359U, 2526101582U, 1998618197U, 1160859704U,\n\t3951172488U, 1098005003U,  906275699U, 3446228002U, 2220677963U,\n\t2059306445U,  132199571U,  476838790U, 1868039399U, 3097344807U,\n\t 857300945U,  396345050U, 2835919916U, 1782168828U, 1419519470U,\n\t4288137521U,  819087232U,  596301494U,  872823172U, 1526888217U,\n\t 805161465U, 1116186205U, 2829002754U, 2352620120U,  620121516U,\n\t 354159268U, 3601949785U,  209568138U, 1352371732U, 2145977349U,\n\t4236871834U, 1539414078U, 3558126206U, 3224857093U, 4164166682U,\n\t3817553440U, 3301780278U, 2682696837U, 3734994768U, 1370950260U,\n\t1477421202U, 2521315749U, 1330148125U, 1261554731U, 2769143688U,\n\t3554756293U, 4235882678U, 3254686059U, 3530579953U, 1215452615U,\n\t3574970923U, 4057131421U,  589224178U, 1000098193U,  171190718U,\n\t2521852045U, 2351447494U, 2284441580U, 2646685513U, 3486933563U,\n\t3789864960U, 1190528160U, 1702536782U, 1534105589U, 4262946827U,\n\t2726686826U, 3584544841U, 2348270128U, 2145092281U, 2502718509U,\n\t1027832411U, 3571171153U, 1287361161U, 4011474411U, 3241215351U,\n\t2419700818U,  971242709U, 1361975763U, 1096842482U, 3271045537U,\n\t  81165449U,  612438025U, 3912966678U, 1356929810U,  733545735U,\n\t 537003843U, 1282953084U,  884458241U,  588930090U, 3930269801U,\n\t2961472450U, 1219535534U, 3632251943U,  268183903U, 1441240533U,\n\t3653903360U, 3854473319U, 2259087390U, 2548293048U, 2022641195U,\n\t2105543911U, 1764085217U, 3246183186U,  482438805U,  888317895U,\n\t2628314765U, 2466219854U,  717546004U, 2322237039U,  416725234U,\n\t1544049923U, 1797944973U, 3398652364U, 3111909456U,  485742908U,\n\t2277491072U, 1056355088U, 3181001278U,  129695079U, 2693624550U,\n\t1764438564U, 3797785470U,  195503713U, 3266519725U, 2053389444U,\n\t1961527818U, 3400226523U, 3777903038U, 2597274307U, 4235851091U,\n\t4094406648U, 2171410785U, 1781151386U, 1378577117U,  654643266U,\n\t3424024173U, 3385813322U,  679385799U,  479380913U,  681715441U,\n\t3096225905U,  276813409U, 3854398070U, 2721105350U,  831263315U,\n\t3276280337U, 2628301522U, 3984868494U, 1466099834U, 2104922114U,\n\t1412672743U,  820330404U, 3491501010U,  942735832U,  710652807U,\n\t3972652090U,  679881088U,   40577009U, 3705286397U, 2815423480U,\n\t3566262429U,  663396513U, 3777887429U, 4016670678U,  404539370U,\n\t1142712925U, 1140173408U, 2913248352U, 2872321286U,  263751841U,\n\t3175196073U, 3162557581U, 2878996619U,   75498548U, 3836833140U,\n\t3284664959U, 1157523805U,  112847376U,  207855609U, 1337979698U,\n\t1222578451U,  157107174U,  901174378U, 3883717063U, 1618632639U,\n\t1767889440U, 4264698824U, 1582999313U,  884471997U, 2508825098U,\n\t3756370771U, 2457213553U, 3565776881U, 3709583214U,  915609601U,\n\t 460833524U, 1091049576U,   85522880U,    2553251U,  132102809U,\n\t2429882442U, 2562084610U, 1386507633U, 4112471229U,   21965213U,\n\t1981516006U, 2418435617U, 3054872091U, 4251511224U, 2025783543U,\n\t1916911512U, 2454491136U, 3938440891U, 3825869115U, 1121698605U,\n\t3463052265U,  802340101U, 1912886800U, 4031997367U, 3550640406U,\n\t1596096923U,  610150600U,  431464457U, 2541325046U,  486478003U,\n\t 739704936U, 2862696430U, 3037903166U, 1129749694U, 2611481261U,\n\t1228993498U,  510075548U, 3424962587U, 2458689681U,  818934833U,\n\t4233309125U, 1608196251U, 3419476016U, 1858543939U, 2682166524U,\n\t3317854285U,  631986188U, 3008214764U,  613826412U, 3567358221U,\n\t3512343882U, 1552467474U, 3316162670U, 1275841024U, 4142173454U,\n\t 565267881U,  768644821U,  198310105U, 2396688616U, 1837659011U,\n\t 203429334U,  854539004U, 4235811518U, 3338304926U, 3730418692U,\n\t3852254981U, 3032046452U, 2329811860U, 2303590566U, 2696092212U,\n\t3894665932U,  145835667U,  249563655U, 1932210840U, 2431696407U,\n\t3312636759U,  214962629U, 2092026914U, 3020145527U, 4073039873U,\n\t2739105705U, 1308336752U,  855104522U, 2391715321U,   67448785U,\n\t 547989482U,  854411802U, 3608633740U,  431731530U,  537375589U,\n\t3888005760U,  696099141U,  397343236U, 1864511780U,   44029739U,\n\t1729526891U, 1993398655U, 2010173426U, 2591546756U,  275223291U,\n\t1503900299U, 4217765081U, 2185635252U, 1122436015U, 3550155364U,\n\t 681707194U, 3260479338U,  933579397U, 2983029282U, 2505504587U,\n\t2667410393U, 2962684490U, 4139721708U, 2658172284U, 2452602383U,\n\t2607631612U, 1344296217U, 3075398709U, 2949785295U, 1049956168U,\n\t3917185129U, 2155660174U, 3280524475U, 1503827867U,  674380765U,\n\t1918468193U, 3843983676U,  634358221U, 2538335643U, 1873351298U,\n\t3368723763U, 2129144130U, 3203528633U, 3087174986U, 2691698871U,\n\t2516284287U,   24437745U, 1118381474U, 2816314867U, 2448576035U,\n\t4281989654U,  217287825U,  165872888U, 2628995722U, 3533525116U,\n\t2721669106U,  872340568U, 3429930655U, 3309047304U, 3916704967U,\n\t3270160355U, 1348884255U, 1634797670U,  881214967U, 4259633554U,\n\t 174613027U, 1103974314U, 1625224232U, 2678368291U, 1133866707U,\n\t3853082619U, 4073196549U, 1189620777U,  637238656U,  930241537U,\n\t4042750792U, 3842136042U, 2417007212U, 2524907510U, 1243036827U,\n\t1282059441U, 3764588774U, 1394459615U, 2323620015U, 1166152231U,\n\t3307479609U, 3849322257U, 3507445699U, 4247696636U,  758393720U,\n\t 967665141U, 1095244571U, 1319812152U,  407678762U, 2640605208U,\n\t2170766134U, 3663594275U, 4039329364U, 2512175520U,  725523154U,\n\t2249807004U, 3312617979U, 2414634172U, 1278482215U,  349206484U,\n\t1573063308U, 1196429124U, 3873264116U, 2400067801U,  268795167U,\n\t 226175489U, 2961367263U, 1968719665U,   42656370U, 1010790699U,\n\t 561600615U, 2422453992U, 3082197735U, 1636700484U, 3977715296U,\n\t3125350482U, 3478021514U, 2227819446U, 1540868045U, 3061908980U,\n\t1087362407U, 3625200291U,  361937537U,  580441897U, 1520043666U,\n\t2270875402U, 1009161260U, 2502355842U, 4278769785U,  473902412U,\n\t1057239083U, 1905829039U, 1483781177U, 2080011417U, 1207494246U,\n\t1806991954U, 2194674403U, 3455972205U,  807207678U, 3655655687U,\n\t 674112918U,  195425752U, 3917890095U, 1874364234U, 1837892715U,\n\t3663478166U, 1548892014U, 2570748714U, 2049929836U, 2167029704U,\n\t 697543767U, 3499545023U, 3342496315U, 1725251190U, 3561387469U,\n\t2905606616U, 1580182447U, 3934525927U, 4103172792U, 1365672522U,\n\t1534795737U, 3308667416U, 2841911405U, 3943182730U, 4072020313U,\n\t3494770452U, 3332626671U,   55327267U,  478030603U,  411080625U,\n\t3419529010U, 1604767823U, 3513468014U,  570668510U,  913790824U,\n\t2283967995U,  695159462U, 3825542932U, 4150698144U, 1829758699U,\n\t 202895590U, 1609122645U, 1267651008U, 2910315509U, 2511475445U,\n\t2477423819U, 3932081579U,  900879979U, 2145588390U, 2670007504U,\n\t 580819444U, 1864996828U, 2526325979U, 1019124258U,  815508628U,\n\t2765933989U, 1277301341U, 3006021786U,  855540956U,  288025710U,\n\t1919594237U, 2331223864U,  177452412U, 2475870369U, 2689291749U,\n\t 865194284U,  253432152U, 2628531804U, 2861208555U, 2361597573U,\n\t1653952120U, 1039661024U, 2159959078U, 3709040440U, 3564718533U,\n\t2596878672U, 2041442161U,   31164696U, 2662962485U, 3665637339U,\n\t1678115244U, 2699839832U, 3651968520U, 3521595541U,  458433303U,\n\t2423096824U,   21831741U,  380011703U, 2498168716U,  861806087U,\n\t1673574843U, 4188794405U, 2520563651U, 2632279153U, 2170465525U,\n\t4171949898U, 3886039621U, 1661344005U, 3424285243U,  992588372U,\n\t2500984144U, 2993248497U, 3590193895U, 1535327365U,  515645636U,\n\t 131633450U, 3729760261U, 1613045101U, 3254194278U,   15889678U,\n\t1493590689U,  244148718U, 2991472662U, 1401629333U,  777349878U,\n\t2501401703U, 4285518317U, 3794656178U,  955526526U, 3442142820U,\n\t3970298374U,  736025417U, 2737370764U, 1271509744U,  440570731U,\n\t 136141826U, 1596189518U,  923399175U,  257541519U, 3505774281U,\n\t2194358432U, 2518162991U, 1379893637U, 2667767062U, 3748146247U,\n\t1821712620U, 3923161384U, 1947811444U, 2392527197U, 4127419685U,\n\t1423694998U, 4156576871U, 1382885582U, 3420127279U, 3617499534U,\n\t2994377493U, 4038063986U, 1918458672U, 2983166794U, 4200449033U,\n\t 353294540U, 1609232588U,  243926648U, 2332803291U,  507996832U,\n\t2392838793U, 4075145196U, 2060984340U, 4287475136U,   88232602U,\n\t2491531140U, 4159725633U, 2272075455U,  759298618U,  201384554U,\n\t 838356250U, 1416268324U,  674476934U,   90795364U,  141672229U,\n\t3660399588U, 4196417251U, 3249270244U, 3774530247U,   59587265U,\n\t3683164208U,   19392575U, 1463123697U, 1882205379U,  293780489U,\n\t2553160622U, 2933904694U,  675638239U, 2851336944U, 1435238743U,\n\t2448730183U,  804436302U, 2119845972U,  322560608U, 4097732704U,\n\t2987802540U,  641492617U, 2575442710U, 4217822703U, 3271835300U,\n\t2836418300U, 3739921620U, 2138378768U, 2879771855U, 4294903423U,\n\t3121097946U, 2603440486U, 2560820391U, 1012930944U, 2313499967U,\n\t 584489368U, 3431165766U,  897384869U, 2062537737U, 2847889234U,\n\t3742362450U, 2951174585U, 4204621084U, 1109373893U, 3668075775U,\n\t2750138839U, 3518055702U,  733072558U, 4169325400U,  788493625U\n};\nstatic const uint64_t init_gen_rand_64_expected[] = {\n\tKQU(16924766246869039260), KQU( 8201438687333352714),\n\tKQU( 2265290287015001750), KQU(18397264611805473832),\n\tKQU( 3375255223302384358), KQU( 6345559975416828796),\n\tKQU(18229739242790328073), KQU( 7596792742098800905),\n\tKQU(  255338647169685981), KQU( 2052747240048610300),\n\tKQU(18328151576097299343), KQU(12472905421133796567),\n\tKQU(11315245349717600863), KQU(16594110197775871209),\n\tKQU(15708751964632456450), KQU(10452031272054632535),\n\tKQU(11097646720811454386), KQU( 4556090668445745441),\n\tKQU(17116187693090663106), KQU(14931526836144510645),\n\tKQU( 9190752218020552591), KQU( 9625800285771901401),\n\tKQU(13995141077659972832), KQU( 5194209094927829625),\n\tKQU( 4156788379151063303), KQU( 8523452593770139494),\n\tKQU(14082382103049296727), KQU( 2462601863986088483),\n\tKQU( 3030583461592840678), KQU( 5221622077872827681),\n\tKQU( 3084210671228981236), KQU(13956758381389953823),\n\tKQU(13503889856213423831), KQU(15696904024189836170),\n\tKQU( 4612584152877036206), KQU( 6231135538447867881),\n\tKQU(10172457294158869468), KQU( 6452258628466708150),\n\tKQU(14044432824917330221), KQU(  370168364480044279),\n\tKQU(10102144686427193359), KQU(  667870489994776076),\n\tKQU( 2732271956925885858), KQU(18027788905977284151),\n\tKQU(15009842788582923859), KQU( 7136357960180199542),\n\tKQU(15901736243475578127), KQU(16951293785352615701),\n\tKQU(10551492125243691632), KQU(17668869969146434804),\n\tKQU(13646002971174390445), KQU( 9804471050759613248),\n\tKQU( 5511670439655935493), KQU(18103342091070400926),\n\tKQU(17224512747665137533), KQU(15534627482992618168),\n\tKQU( 1423813266186582647), KQU(15821176807932930024),\n\tKQU(   30323369733607156), KQU(11599382494723479403),\n\tKQU(  653856076586810062), KQU( 3176437395144899659),\n\tKQU(14028076268147963917), KQU(16156398271809666195),\n\tKQU( 3166955484848201676), KQU( 5746805620136919390),\n\tKQU(17297845208891256593), KQU(11691653183226428483),\n\tKQU(17900026146506981577), KQU(15387382115755971042),\n\tKQU(16923567681040845943), KQU( 8039057517199388606),\n\tKQU(11748409241468629263), KQU(  794358245539076095),\n\tKQU(13438501964693401242), KQU(14036803236515618962),\n\tKQU( 5252311215205424721), KQU(17806589612915509081),\n\tKQU( 6802767092397596006), KQU(14212120431184557140),\n\tKQU( 1072951366761385712), KQU(13098491780722836296),\n\tKQU( 9466676828710797353), KQU(12673056849042830081),\n\tKQU(12763726623645357580), KQU(16468961652999309493),\n\tKQU(15305979875636438926), KQU(17444713151223449734),\n\tKQU( 5692214267627883674), KQU(13049589139196151505),\n\tKQU(  880115207831670745), KQU( 1776529075789695498),\n\tKQU(16695225897801466485), KQU(10666901778795346845),\n\tKQU( 6164389346722833869), KQU( 2863817793264300475),\n\tKQU( 9464049921886304754), KQU( 3993566636740015468),\n\tKQU( 9983749692528514136), KQU(16375286075057755211),\n\tKQU(16042643417005440820), KQU(11445419662923489877),\n\tKQU( 7999038846885158836), KQU( 6721913661721511535),\n\tKQU( 5363052654139357320), KQU( 1817788761173584205),\n\tKQU(13290974386445856444), KQU( 4650350818937984680),\n\tKQU( 8219183528102484836), KQU( 1569862923500819899),\n\tKQU( 4189359732136641860), KQU(14202822961683148583),\n\tKQU( 4457498315309429058), KQU(13089067387019074834),\n\tKQU(11075517153328927293), KQU(10277016248336668389),\n\tKQU( 7070509725324401122), KQU(17808892017780289380),\n\tKQU(13143367339909287349), KQU( 1377743745360085151),\n\tKQU( 5749341807421286485), KQU(14832814616770931325),\n\tKQU( 7688820635324359492), KQU(10960474011539770045),\n\tKQU(   81970066653179790), KQU(12619476072607878022),\n\tKQU( 4419566616271201744), KQU(15147917311750568503),\n\tKQU( 5549739182852706345), KQU( 7308198397975204770),\n\tKQU(13580425496671289278), KQU(17070764785210130301),\n\tKQU( 8202832846285604405), KQU( 6873046287640887249),\n\tKQU( 6927424434308206114), KQU( 6139014645937224874),\n\tKQU(10290373645978487639), KQU(15904261291701523804),\n\tKQU( 9628743442057826883), KQU(18383429096255546714),\n\tKQU( 4977413265753686967), KQU( 7714317492425012869),\n\tKQU( 9025232586309926193), KQU(14627338359776709107),\n\tKQU(14759849896467790763), KQU(10931129435864423252),\n\tKQU( 4588456988775014359), KQU(10699388531797056724),\n\tKQU(  468652268869238792), KQU( 5755943035328078086),\n\tKQU( 2102437379988580216), KQU( 9986312786506674028),\n\tKQU( 2654207180040945604), KQU( 8726634790559960062),\n\tKQU(  100497234871808137), KQU( 2800137176951425819),\n\tKQU( 6076627612918553487), KQU( 5780186919186152796),\n\tKQU( 8179183595769929098), KQU( 6009426283716221169),\n\tKQU( 2796662551397449358), KQU( 1756961367041986764),\n\tKQU( 6972897917355606205), KQU(14524774345368968243),\n\tKQU( 2773529684745706940), KQU( 4853632376213075959),\n\tKQU( 4198177923731358102), KQU( 8271224913084139776),\n\tKQU( 2741753121611092226), KQU(16782366145996731181),\n\tKQU(15426125238972640790), KQU(13595497100671260342),\n\tKQU( 3173531022836259898), KQU( 6573264560319511662),\n\tKQU(18041111951511157441), KQU( 2351433581833135952),\n\tKQU( 3113255578908173487), KQU( 1739371330877858784),\n\tKQU(16046126562789165480), KQU( 8072101652214192925),\n\tKQU(15267091584090664910), KQU( 9309579200403648940),\n\tKQU( 5218892439752408722), KQU(14492477246004337115),\n\tKQU(17431037586679770619), KQU( 7385248135963250480),\n\tKQU( 9580144956565560660), KQU( 4919546228040008720),\n\tKQU(15261542469145035584), KQU(18233297270822253102),\n\tKQU( 5453248417992302857), KQU( 9309519155931460285),\n\tKQU(10342813012345291756), KQU(15676085186784762381),\n\tKQU(15912092950691300645), KQU( 9371053121499003195),\n\tKQU( 9897186478226866746), KQU(14061858287188196327),\n\tKQU(  122575971620788119), KQU(12146750969116317754),\n\tKQU( 4438317272813245201), KQU( 8332576791009527119),\n\tKQU(13907785691786542057), KQU(10374194887283287467),\n\tKQU( 2098798755649059566), KQU( 3416235197748288894),\n\tKQU( 8688269957320773484), KQU( 7503964602397371571),\n\tKQU(16724977015147478236), KQU( 9461512855439858184),\n\tKQU(13259049744534534727), KQU( 3583094952542899294),\n\tKQU( 8764245731305528292), KQU(13240823595462088985),\n\tKQU(13716141617617910448), KQU(18114969519935960955),\n\tKQU( 2297553615798302206), KQU( 4585521442944663362),\n\tKQU(17776858680630198686), KQU( 4685873229192163363),\n\tKQU(  152558080671135627), KQU(15424900540842670088),\n\tKQU(13229630297130024108), KQU(17530268788245718717),\n\tKQU(16675633913065714144), KQU( 3158912717897568068),\n\tKQU(15399132185380087288), KQU( 7401418744515677872),\n\tKQU(13135412922344398535), KQU( 6385314346100509511),\n\tKQU(13962867001134161139), KQU(10272780155442671999),\n\tKQU(12894856086597769142), KQU(13340877795287554994),\n\tKQU(12913630602094607396), KQU(12543167911119793857),\n\tKQU(17343570372251873096), KQU(10959487764494150545),\n\tKQU( 6966737953093821128), KQU(13780699135496988601),\n\tKQU( 4405070719380142046), KQU(14923788365607284982),\n\tKQU( 2869487678905148380), KQU( 6416272754197188403),\n\tKQU(15017380475943612591), KQU( 1995636220918429487),\n\tKQU( 3402016804620122716), KQU(15800188663407057080),\n\tKQU(11362369990390932882), KQU(15262183501637986147),\n\tKQU(10239175385387371494), KQU( 9352042420365748334),\n\tKQU( 1682457034285119875), KQU( 1724710651376289644),\n\tKQU( 2038157098893817966), KQU( 9897825558324608773),\n\tKQU( 1477666236519164736), KQU(16835397314511233640),\n\tKQU(10370866327005346508), KQU(10157504370660621982),\n\tKQU(12113904045335882069), KQU(13326444439742783008),\n\tKQU(11302769043000765804), KQU(13594979923955228484),\n\tKQU(11779351762613475968), KQU( 3786101619539298383),\n\tKQU( 8021122969180846063), KQU(15745904401162500495),\n\tKQU(10762168465993897267), KQU(13552058957896319026),\n\tKQU(11200228655252462013), KQU( 5035370357337441226),\n\tKQU( 7593918984545500013), KQU( 5418554918361528700),\n\tKQU( 4858270799405446371), KQU( 9974659566876282544),\n\tKQU(18227595922273957859), KQU( 2772778443635656220),\n\tKQU(14285143053182085385), KQU( 9939700992429600469),\n\tKQU(12756185904545598068), KQU( 2020783375367345262),\n\tKQU(   57026775058331227), KQU(  950827867930065454),\n\tKQU( 6602279670145371217), KQU( 2291171535443566929),\n\tKQU( 5832380724425010313), KQU( 1220343904715982285),\n\tKQU(17045542598598037633), KQU(15460481779702820971),\n\tKQU(13948388779949365130), KQU(13975040175430829518),\n\tKQU(17477538238425541763), KQU(11104663041851745725),\n\tKQU(15860992957141157587), KQU(14529434633012950138),\n\tKQU( 2504838019075394203), KQU( 7512113882611121886),\n\tKQU( 4859973559980886617), KQU( 1258601555703250219),\n\tKQU(15594548157514316394), KQU( 4516730171963773048),\n\tKQU(11380103193905031983), KQU( 6809282239982353344),\n\tKQU(18045256930420065002), KQU( 2453702683108791859),\n\tKQU(  977214582986981460), KQU( 2006410402232713466),\n\tKQU( 6192236267216378358), KQU( 3429468402195675253),\n\tKQU(18146933153017348921), KQU(17369978576367231139),\n\tKQU( 1246940717230386603), KQU(11335758870083327110),\n\tKQU(14166488801730353682), KQU( 9008573127269635732),\n\tKQU(10776025389820643815), KQU(15087605441903942962),\n\tKQU( 1359542462712147922), KQU(13898874411226454206),\n\tKQU(17911176066536804411), KQU( 9435590428600085274),\n\tKQU(  294488509967864007), KQU( 8890111397567922046),\n\tKQU( 7987823476034328778), KQU(13263827582440967651),\n\tKQU( 7503774813106751573), KQU(14974747296185646837),\n\tKQU( 8504765037032103375), KQU(17340303357444536213),\n\tKQU( 7704610912964485743), KQU( 8107533670327205061),\n\tKQU( 9062969835083315985), KQU(16968963142126734184),\n\tKQU(12958041214190810180), KQU( 2720170147759570200),\n\tKQU( 2986358963942189566), KQU(14884226322219356580),\n\tKQU(  286224325144368520), KQU(11313800433154279797),\n\tKQU(18366849528439673248), KQU(17899725929482368789),\n\tKQU( 3730004284609106799), KQU( 1654474302052767205),\n\tKQU( 5006698007047077032), KQU( 8196893913601182838),\n\tKQU(15214541774425211640), KQU(17391346045606626073),\n\tKQU( 8369003584076969089), KQU( 3939046733368550293),\n\tKQU(10178639720308707785), KQU( 2180248669304388697),\n\tKQU(   62894391300126322), KQU( 9205708961736223191),\n\tKQU( 6837431058165360438), KQU( 3150743890848308214),\n\tKQU(17849330658111464583), KQU(12214815643135450865),\n\tKQU(13410713840519603402), KQU( 3200778126692046802),\n\tKQU(13354780043041779313), KQU(  800850022756886036),\n\tKQU(15660052933953067433), KQU( 6572823544154375676),\n\tKQU(11030281857015819266), KQU(12682241941471433835),\n\tKQU(11654136407300274693), KQU( 4517795492388641109),\n\tKQU( 9757017371504524244), KQU(17833043400781889277),\n\tKQU(12685085201747792227), KQU(10408057728835019573),\n\tKQU(   98370418513455221), KQU( 6732663555696848598),\n\tKQU(13248530959948529780), KQU( 3530441401230622826),\n\tKQU(18188251992895660615), KQU( 1847918354186383756),\n\tKQU( 1127392190402660921), KQU(11293734643143819463),\n\tKQU( 3015506344578682982), KQU(13852645444071153329),\n\tKQU( 2121359659091349142), KQU( 1294604376116677694),\n\tKQU( 5616576231286352318), KQU( 7112502442954235625),\n\tKQU(11676228199551561689), KQU(12925182803007305359),\n\tKQU( 7852375518160493082), KQU( 1136513130539296154),\n\tKQU( 5636923900916593195), KQU( 3221077517612607747),\n\tKQU(17784790465798152513), KQU( 3554210049056995938),\n\tKQU(17476839685878225874), KQU( 3206836372585575732),\n\tKQU( 2765333945644823430), KQU(10080070903718799528),\n\tKQU( 5412370818878286353), KQU( 9689685887726257728),\n\tKQU( 8236117509123533998), KQU( 1951139137165040214),\n\tKQU( 4492205209227980349), KQU(16541291230861602967),\n\tKQU( 1424371548301437940), KQU( 9117562079669206794),\n\tKQU(14374681563251691625), KQU(13873164030199921303),\n\tKQU( 6680317946770936731), KQU(15586334026918276214),\n\tKQU(10896213950976109802), KQU( 9506261949596413689),\n\tKQU( 9903949574308040616), KQU( 6038397344557204470),\n\tKQU(  174601465422373648), KQU(15946141191338238030),\n\tKQU(17142225620992044937), KQU( 7552030283784477064),\n\tKQU( 2947372384532947997), KQU(  510797021688197711),\n\tKQU( 4962499439249363461), KQU(   23770320158385357),\n\tKQU(  959774499105138124), KQU( 1468396011518788276),\n\tKQU( 2015698006852312308), KQU( 4149400718489980136),\n\tKQU( 5992916099522371188), KQU(10819182935265531076),\n\tKQU(16189787999192351131), KQU(  342833961790261950),\n\tKQU(12470830319550495336), KQU(18128495041912812501),\n\tKQU( 1193600899723524337), KQU( 9056793666590079770),\n\tKQU( 2154021227041669041), KQU( 4963570213951235735),\n\tKQU( 4865075960209211409), KQU( 2097724599039942963),\n\tKQU( 2024080278583179845), KQU(11527054549196576736),\n\tKQU(10650256084182390252), KQU( 4808408648695766755),\n\tKQU( 1642839215013788844), KQU(10607187948250398390),\n\tKQU( 7076868166085913508), KQU(  730522571106887032),\n\tKQU(12500579240208524895), KQU( 4484390097311355324),\n\tKQU(15145801330700623870), KQU( 8055827661392944028),\n\tKQU( 5865092976832712268), KQU(15159212508053625143),\n\tKQU( 3560964582876483341), KQU( 4070052741344438280),\n\tKQU( 6032585709886855634), KQU(15643262320904604873),\n\tKQU( 2565119772293371111), KQU(  318314293065348260),\n\tKQU(15047458749141511872), KQU( 7772788389811528730),\n\tKQU( 7081187494343801976), KQU( 6465136009467253947),\n\tKQU(10425940692543362069), KQU(  554608190318339115),\n\tKQU(14796699860302125214), KQU( 1638153134431111443),\n\tKQU(10336967447052276248), KQU( 8412308070396592958),\n\tKQU( 4004557277152051226), KQU( 8143598997278774834),\n\tKQU(16413323996508783221), KQU(13139418758033994949),\n\tKQU( 9772709138335006667), KQU( 2818167159287157659),\n\tKQU(17091740573832523669), KQU(14629199013130751608),\n\tKQU(18268322711500338185), KQU( 8290963415675493063),\n\tKQU( 8830864907452542588), KQU( 1614839084637494849),\n\tKQU(14855358500870422231), KQU( 3472996748392519937),\n\tKQU(15317151166268877716), KQU( 5825895018698400362),\n\tKQU(16730208429367544129), KQU(10481156578141202800),\n\tKQU( 4746166512382823750), KQU(12720876014472464998),\n\tKQU( 8825177124486735972), KQU(13733447296837467838),\n\tKQU( 6412293741681359625), KQU( 8313213138756135033),\n\tKQU(11421481194803712517), KQU( 7997007691544174032),\n\tKQU( 6812963847917605930), KQU( 9683091901227558641),\n\tKQU(14703594165860324713), KQU( 1775476144519618309),\n\tKQU( 2724283288516469519), KQU(  717642555185856868),\n\tKQU( 8736402192215092346), KQU(11878800336431381021),\n\tKQU( 4348816066017061293), KQU( 6115112756583631307),\n\tKQU( 9176597239667142976), KQU(12615622714894259204),\n\tKQU(10283406711301385987), KQU( 5111762509485379420),\n\tKQU( 3118290051198688449), KQU( 7345123071632232145),\n\tKQU( 9176423451688682359), KQU( 4843865456157868971),\n\tKQU(12008036363752566088), KQU(12058837181919397720),\n\tKQU( 2145073958457347366), KQU( 1526504881672818067),\n\tKQU( 3488830105567134848), KQU(13208362960674805143),\n\tKQU( 4077549672899572192), KQU( 7770995684693818365),\n\tKQU( 1398532341546313593), KQU(12711859908703927840),\n\tKQU( 1417561172594446813), KQU(17045191024194170604),\n\tKQU( 4101933177604931713), KQU(14708428834203480320),\n\tKQU(17447509264469407724), KQU(14314821973983434255),\n\tKQU(17990472271061617265), KQU( 5087756685841673942),\n\tKQU(12797820586893859939), KQU( 1778128952671092879),\n\tKQU( 3535918530508665898), KQU( 9035729701042481301),\n\tKQU(14808661568277079962), KQU(14587345077537747914),\n\tKQU(11920080002323122708), KQU( 6426515805197278753),\n\tKQU( 3295612216725984831), KQU(11040722532100876120),\n\tKQU(12305952936387598754), KQU(16097391899742004253),\n\tKQU( 4908537335606182208), KQU(12446674552196795504),\n\tKQU(16010497855816895177), KQU( 9194378874788615551),\n\tKQU( 3382957529567613384), KQU( 5154647600754974077),\n\tKQU( 9801822865328396141), KQU( 9023662173919288143),\n\tKQU(17623115353825147868), KQU( 8238115767443015816),\n\tKQU(15811444159859002560), KQU( 9085612528904059661),\n\tKQU( 6888601089398614254), KQU(  258252992894160189),\n\tKQU( 6704363880792428622), KQU( 6114966032147235763),\n\tKQU(11075393882690261875), KQU( 8797664238933620407),\n\tKQU( 5901892006476726920), KQU( 5309780159285518958),\n\tKQU(14940808387240817367), KQU(14642032021449656698),\n\tKQU( 9808256672068504139), KQU( 3670135111380607658),\n\tKQU(11211211097845960152), KQU( 1474304506716695808),\n\tKQU(15843166204506876239), KQU( 7661051252471780561),\n\tKQU(10170905502249418476), KQU( 7801416045582028589),\n\tKQU( 2763981484737053050), KQU( 9491377905499253054),\n\tKQU(16201395896336915095), KQU( 9256513756442782198),\n\tKQU( 5411283157972456034), KQU( 5059433122288321676),\n\tKQU( 4327408006721123357), KQU( 9278544078834433377),\n\tKQU( 7601527110882281612), KQU(11848295896975505251),\n\tKQU(12096998801094735560), KQU(14773480339823506413),\n\tKQU(15586227433895802149), KQU(12786541257830242872),\n\tKQU( 6904692985140503067), KQU( 5309011515263103959),\n\tKQU(12105257191179371066), KQU(14654380212442225037),\n\tKQU( 2556774974190695009), KQU( 4461297399927600261),\n\tKQU(14888225660915118646), KQU(14915459341148291824),\n\tKQU( 2738802166252327631), KQU( 6047155789239131512),\n\tKQU(12920545353217010338), KQU(10697617257007840205),\n\tKQU( 2751585253158203504), KQU(13252729159780047496),\n\tKQU(14700326134672815469), KQU(14082527904374600529),\n\tKQU(16852962273496542070), KQU(17446675504235853907),\n\tKQU(15019600398527572311), KQU(12312781346344081551),\n\tKQU(14524667935039810450), KQU( 5634005663377195738),\n\tKQU(11375574739525000569), KQU( 2423665396433260040),\n\tKQU( 5222836914796015410), KQU( 4397666386492647387),\n\tKQU( 4619294441691707638), KQU(  665088602354770716),\n\tKQU(13246495665281593610), KQU( 6564144270549729409),\n\tKQU(10223216188145661688), KQU( 3961556907299230585),\n\tKQU(11543262515492439914), KQU(16118031437285993790),\n\tKQU( 7143417964520166465), KQU(13295053515909486772),\n\tKQU(   40434666004899675), KQU(17127804194038347164),\n\tKQU( 8599165966560586269), KQU( 8214016749011284903),\n\tKQU(13725130352140465239), KQU( 5467254474431726291),\n\tKQU( 7748584297438219877), KQU(16933551114829772472),\n\tKQU( 2169618439506799400), KQU( 2169787627665113463),\n\tKQU(17314493571267943764), KQU(18053575102911354912),\n\tKQU(11928303275378476973), KQU(11593850925061715550),\n\tKQU(17782269923473589362), KQU( 3280235307704747039),\n\tKQU( 6145343578598685149), KQU(17080117031114086090),\n\tKQU(18066839902983594755), KQU( 6517508430331020706),\n\tKQU( 8092908893950411541), KQU(12558378233386153732),\n\tKQU( 4476532167973132976), KQU(16081642430367025016),\n\tKQU( 4233154094369139361), KQU( 8693630486693161027),\n\tKQU(11244959343027742285), KQU(12273503967768513508),\n\tKQU(14108978636385284876), KQU( 7242414665378826984),\n\tKQU( 6561316938846562432), KQU( 8601038474994665795),\n\tKQU(17532942353612365904), KQU(17940076637020912186),\n\tKQU( 7340260368823171304), KQU( 7061807613916067905),\n\tKQU(10561734935039519326), KQU(17990796503724650862),\n\tKQU( 6208732943911827159), KQU(  359077562804090617),\n\tKQU(14177751537784403113), KQU(10659599444915362902),\n\tKQU(15081727220615085833), KQU(13417573895659757486),\n\tKQU(15513842342017811524), KQU(11814141516204288231),\n\tKQU( 1827312513875101814), KQU( 2804611699894603103),\n\tKQU(17116500469975602763), KQU(12270191815211952087),\n\tKQU(12256358467786024988), KQU(18435021722453971267),\n\tKQU(  671330264390865618), KQU(  476504300460286050),\n\tKQU(16465470901027093441), KQU( 4047724406247136402),\n\tKQU( 1322305451411883346), KQU( 1388308688834322280),\n\tKQU( 7303989085269758176), KQU( 9323792664765233642),\n\tKQU( 4542762575316368936), KQU(17342696132794337618),\n\tKQU( 4588025054768498379), KQU(13415475057390330804),\n\tKQU(17880279491733405570), KQU(10610553400618620353),\n\tKQU( 3180842072658960139), KQU(13002966655454270120),\n\tKQU( 1665301181064982826), KQU( 7083673946791258979),\n\tKQU(  190522247122496820), KQU(17388280237250677740),\n\tKQU( 8430770379923642945), KQU(12987180971921668584),\n\tKQU( 2311086108365390642), KQU( 2870984383579822345),\n\tKQU(14014682609164653318), KQU(14467187293062251484),\n\tKQU(  192186361147413298), KQU(15171951713531796524),\n\tKQU( 9900305495015948728), KQU(17958004775615466344),\n\tKQU(14346380954498606514), KQU(18040047357617407096),\n\tKQU( 5035237584833424532), KQU(15089555460613972287),\n\tKQU( 4131411873749729831), KQU( 1329013581168250330),\n\tKQU(10095353333051193949), KQU(10749518561022462716),\n\tKQU( 9050611429810755847), KQU(15022028840236655649),\n\tKQU( 8775554279239748298), KQU(13105754025489230502),\n\tKQU(15471300118574167585), KQU(   89864764002355628),\n\tKQU( 8776416323420466637), KQU( 5280258630612040891),\n\tKQU( 2719174488591862912), KQU( 7599309137399661994),\n\tKQU(15012887256778039979), KQU(14062981725630928925),\n\tKQU(12038536286991689603), KQU( 7089756544681775245),\n\tKQU(10376661532744718039), KQU( 1265198725901533130),\n\tKQU(13807996727081142408), KQU( 2935019626765036403),\n\tKQU( 7651672460680700141), KQU( 3644093016200370795),\n\tKQU( 2840982578090080674), KQU(17956262740157449201),\n\tKQU(18267979450492880548), KQU(11799503659796848070),\n\tKQU( 9942537025669672388), KQU(11886606816406990297),\n\tKQU( 5488594946437447576), KQU( 7226714353282744302),\n\tKQU( 3784851653123877043), KQU(  878018453244803041),\n\tKQU(12110022586268616085), KQU(  734072179404675123),\n\tKQU(11869573627998248542), KQU(  469150421297783998),\n\tKQU(  260151124912803804), KQU(11639179410120968649),\n\tKQU( 9318165193840846253), KQU(12795671722734758075),\n\tKQU(15318410297267253933), KQU(  691524703570062620),\n\tKQU( 5837129010576994601), KQU(15045963859726941052),\n\tKQU( 5850056944932238169), KQU(12017434144750943807),\n\tKQU( 7447139064928956574), KQU( 3101711812658245019),\n\tKQU(16052940704474982954), KQU(18195745945986994042),\n\tKQU( 8932252132785575659), KQU(13390817488106794834),\n\tKQU(11582771836502517453), KQU( 4964411326683611686),\n\tKQU( 2195093981702694011), KQU(14145229538389675669),\n\tKQU(16459605532062271798), KQU(  866316924816482864),\n\tKQU( 4593041209937286377), KQU( 8415491391910972138),\n\tKQU( 4171236715600528969), KQU(16637569303336782889),\n\tKQU( 2002011073439212680), KQU(17695124661097601411),\n\tKQU( 4627687053598611702), KQU( 7895831936020190403),\n\tKQU( 8455951300917267802), KQU( 2923861649108534854),\n\tKQU( 8344557563927786255), KQU( 6408671940373352556),\n\tKQU(12210227354536675772), KQU(14294804157294222295),\n\tKQU(10103022425071085127), KQU(10092959489504123771),\n\tKQU( 6554774405376736268), KQU(12629917718410641774),\n\tKQU( 6260933257596067126), KQU( 2460827021439369673),\n\tKQU( 2541962996717103668), KQU(  597377203127351475),\n\tKQU( 5316984203117315309), KQU( 4811211393563241961),\n\tKQU(13119698597255811641), KQU( 8048691512862388981),\n\tKQU(10216818971194073842), KQU( 4612229970165291764),\n\tKQU(10000980798419974770), KQU( 6877640812402540687),\n\tKQU( 1488727563290436992), KQU( 2227774069895697318),\n\tKQU(11237754507523316593), KQU(13478948605382290972),\n\tKQU( 1963583846976858124), KQU( 5512309205269276457),\n\tKQU( 3972770164717652347), KQU( 3841751276198975037),\n\tKQU(10283343042181903117), KQU( 8564001259792872199),\n\tKQU(16472187244722489221), KQU( 8953493499268945921),\n\tKQU( 3518747340357279580), KQU( 4003157546223963073),\n\tKQU( 3270305958289814590), KQU( 3966704458129482496),\n\tKQU( 8122141865926661939), KQU(14627734748099506653),\n\tKQU(13064426990862560568), KQU( 2414079187889870829),\n\tKQU( 5378461209354225306), KQU(10841985740128255566),\n\tKQU(  538582442885401738), KQU( 7535089183482905946),\n\tKQU(16117559957598879095), KQU( 8477890721414539741),\n\tKQU( 1459127491209533386), KQU(17035126360733620462),\n\tKQU( 8517668552872379126), KQU(10292151468337355014),\n\tKQU(17081267732745344157), KQU(13751455337946087178),\n\tKQU(14026945459523832966), KQU( 6653278775061723516),\n\tKQU(10619085543856390441), KQU( 2196343631481122885),\n\tKQU(10045966074702826136), KQU(10082317330452718282),\n\tKQU( 5920859259504831242), KQU( 9951879073426540617),\n\tKQU( 7074696649151414158), KQU(15808193543879464318),\n\tKQU( 7385247772746953374), KQU( 3192003544283864292),\n\tKQU(18153684490917593847), KQU(12423498260668568905),\n\tKQU(10957758099756378169), KQU(11488762179911016040),\n\tKQU( 2099931186465333782), KQU(11180979581250294432),\n\tKQU( 8098916250668367933), KQU( 3529200436790763465),\n\tKQU(12988418908674681745), KQU( 6147567275954808580),\n\tKQU( 3207503344604030989), KQU(10761592604898615360),\n\tKQU(  229854861031893504), KQU( 8809853962667144291),\n\tKQU(13957364469005693860), KQU( 7634287665224495886),\n\tKQU(12353487366976556874), KQU( 1134423796317152034),\n\tKQU( 2088992471334107068), KQU( 7393372127190799698),\n\tKQU( 1845367839871058391), KQU(  207922563987322884),\n\tKQU(11960870813159944976), KQU(12182120053317317363),\n\tKQU(17307358132571709283), KQU(13871081155552824936),\n\tKQU(18304446751741566262), KQU( 7178705220184302849),\n\tKQU(10929605677758824425), KQU(16446976977835806844),\n\tKQU(13723874412159769044), KQU( 6942854352100915216),\n\tKQU( 1726308474365729390), KQU( 2150078766445323155),\n\tKQU(15345558947919656626), KQU(12145453828874527201),\n\tKQU( 2054448620739726849), KQU( 2740102003352628137),\n\tKQU(11294462163577610655), KQU(  756164283387413743),\n\tKQU(17841144758438810880), KQU(10802406021185415861),\n\tKQU( 8716455530476737846), KQU( 6321788834517649606),\n\tKQU(14681322910577468426), KQU(17330043563884336387),\n\tKQU(12701802180050071614), KQU(14695105111079727151),\n\tKQU( 5112098511654172830), KQU( 4957505496794139973),\n\tKQU( 8270979451952045982), KQU(12307685939199120969),\n\tKQU(12425799408953443032), KQU( 8376410143634796588),\n\tKQU(16621778679680060464), KQU( 3580497854566660073),\n\tKQU( 1122515747803382416), KQU(  857664980960597599),\n\tKQU( 6343640119895925918), KQU(12878473260854462891),\n\tKQU(10036813920765722626), KQU(14451335468363173812),\n\tKQU( 5476809692401102807), KQU(16442255173514366342),\n\tKQU(13060203194757167104), KQU(14354124071243177715),\n\tKQU(15961249405696125227), KQU(13703893649690872584),\n\tKQU(  363907326340340064), KQU( 6247455540491754842),\n\tKQU(12242249332757832361), KQU(  156065475679796717),\n\tKQU( 9351116235749732355), KQU( 4590350628677701405),\n\tKQU( 1671195940982350389), KQU(13501398458898451905),\n\tKQU( 6526341991225002255), KQU( 1689782913778157592),\n\tKQU( 7439222350869010334), KQU(13975150263226478308),\n\tKQU(11411961169932682710), KQU(17204271834833847277),\n\tKQU(  541534742544435367), KQU( 6591191931218949684),\n\tKQU( 2645454775478232486), KQU( 4322857481256485321),\n\tKQU( 8477416487553065110), KQU(12902505428548435048),\n\tKQU(  971445777981341415), KQU(14995104682744976712),\n\tKQU( 4243341648807158063), KQU( 8695061252721927661),\n\tKQU( 5028202003270177222), KQU( 2289257340915567840),\n\tKQU(13870416345121866007), KQU(13994481698072092233),\n\tKQU( 6912785400753196481), KQU( 2278309315841980139),\n\tKQU( 4329765449648304839), KQU( 5963108095785485298),\n\tKQU( 4880024847478722478), KQU(16015608779890240947),\n\tKQU( 1866679034261393544), KQU(  914821179919731519),\n\tKQU( 9643404035648760131), KQU( 2418114953615593915),\n\tKQU(  944756836073702374), KQU(15186388048737296834),\n\tKQU( 7723355336128442206), KQU( 7500747479679599691),\n\tKQU(18013961306453293634), KQU( 2315274808095756456),\n\tKQU(13655308255424029566), KQU(17203800273561677098),\n\tKQU( 1382158694422087756), KQU( 5090390250309588976),\n\tKQU(  517170818384213989), KQU( 1612709252627729621),\n\tKQU( 1330118955572449606), KQU(  300922478056709885),\n\tKQU(18115693291289091987), KQU(13491407109725238321),\n\tKQU(15293714633593827320), KQU( 5151539373053314504),\n\tKQU( 5951523243743139207), KQU(14459112015249527975),\n\tKQU( 5456113959000700739), KQU( 3877918438464873016),\n\tKQU(12534071654260163555), KQU(15871678376893555041),\n\tKQU(11005484805712025549), KQU(16353066973143374252),\n\tKQU( 4358331472063256685), KQU( 8268349332210859288),\n\tKQU(12485161590939658075), KQU(13955993592854471343),\n\tKQU( 5911446886848367039), KQU(14925834086813706974),\n\tKQU( 6590362597857994805), KQU( 1280544923533661875),\n\tKQU( 1637756018947988164), KQU( 4734090064512686329),\n\tKQU(16693705263131485912), KQU( 6834882340494360958),\n\tKQU( 8120732176159658505), KQU( 2244371958905329346),\n\tKQU(10447499707729734021), KQU( 7318742361446942194),\n\tKQU( 8032857516355555296), KQU(14023605983059313116),\n\tKQU( 1032336061815461376), KQU( 9840995337876562612),\n\tKQU( 9869256223029203587), KQU(12227975697177267636),\n\tKQU(12728115115844186033), KQU( 7752058479783205470),\n\tKQU(  729733219713393087), KQU(12954017801239007622)\n};\nstatic const uint64_t init_by_array_64_expected[] = {\n\tKQU( 2100341266307895239), KQU( 8344256300489757943),\n\tKQU(15687933285484243894), KQU( 8268620370277076319),\n\tKQU(12371852309826545459), KQU( 8800491541730110238),\n\tKQU(18113268950100835773), KQU( 2886823658884438119),\n\tKQU( 3293667307248180724), KQU( 9307928143300172731),\n\tKQU( 7688082017574293629), KQU(  900986224735166665),\n\tKQU( 9977972710722265039), KQU( 6008205004994830552),\n\tKQU(  546909104521689292), KQU( 7428471521869107594),\n\tKQU(14777563419314721179), KQU(16116143076567350053),\n\tKQU( 5322685342003142329), KQU( 4200427048445863473),\n\tKQU( 4693092150132559146), KQU(13671425863759338582),\n\tKQU( 6747117460737639916), KQU( 4732666080236551150),\n\tKQU( 5912839950611941263), KQU( 3903717554504704909),\n\tKQU( 2615667650256786818), KQU(10844129913887006352),\n\tKQU(13786467861810997820), KQU(14267853002994021570),\n\tKQU(13767807302847237439), KQU(16407963253707224617),\n\tKQU( 4802498363698583497), KQU( 2523802839317209764),\n\tKQU( 3822579397797475589), KQU( 8950320572212130610),\n\tKQU( 3745623504978342534), KQU(16092609066068482806),\n\tKQU( 9817016950274642398), KQU(10591660660323829098),\n\tKQU(11751606650792815920), KQU( 5122873818577122211),\n\tKQU(17209553764913936624), KQU( 6249057709284380343),\n\tKQU(15088791264695071830), KQU(15344673071709851930),\n\tKQU( 4345751415293646084), KQU( 2542865750703067928),\n\tKQU(13520525127852368784), KQU(18294188662880997241),\n\tKQU( 3871781938044881523), KQU( 2873487268122812184),\n\tKQU(15099676759482679005), KQU(15442599127239350490),\n\tKQU( 6311893274367710888), KQU( 3286118760484672933),\n\tKQU( 4146067961333542189), KQU(13303942567897208770),\n\tKQU( 8196013722255630418), KQU( 4437815439340979989),\n\tKQU(15433791533450605135), KQU( 4254828956815687049),\n\tKQU( 1310903207708286015), KQU(10529182764462398549),\n\tKQU(14900231311660638810), KQU( 9727017277104609793),\n\tKQU( 1821308310948199033), KQU(11628861435066772084),\n\tKQU( 9469019138491546924), KQU( 3145812670532604988),\n\tKQU( 9938468915045491919), KQU( 1562447430672662142),\n\tKQU(13963995266697989134), KQU( 3356884357625028695),\n\tKQU( 4499850304584309747), KQU( 8456825817023658122),\n\tKQU(10859039922814285279), KQU( 8099512337972526555),\n\tKQU(  348006375109672149), KQU(11919893998241688603),\n\tKQU( 1104199577402948826), KQU(16689191854356060289),\n\tKQU(10992552041730168078), KQU( 7243733172705465836),\n\tKQU( 5668075606180319560), KQU(18182847037333286970),\n\tKQU( 4290215357664631322), KQU( 4061414220791828613),\n\tKQU(13006291061652989604), KQU( 7140491178917128798),\n\tKQU(12703446217663283481), KQU( 5500220597564558267),\n\tKQU(10330551509971296358), KQU(15958554768648714492),\n\tKQU( 5174555954515360045), KQU( 1731318837687577735),\n\tKQU( 3557700801048354857), KQU(13764012341928616198),\n\tKQU(13115166194379119043), KQU( 7989321021560255519),\n\tKQU( 2103584280905877040), KQU( 9230788662155228488),\n\tKQU(16396629323325547654), KQU(  657926409811318051),\n\tKQU(15046700264391400727), KQU( 5120132858771880830),\n\tKQU( 7934160097989028561), KQU( 6963121488531976245),\n\tKQU(17412329602621742089), KQU(15144843053931774092),\n\tKQU(17204176651763054532), KQU(13166595387554065870),\n\tKQU( 8590377810513960213), KQU( 5834365135373991938),\n\tKQU( 7640913007182226243), KQU( 3479394703859418425),\n\tKQU(16402784452644521040), KQU( 4993979809687083980),\n\tKQU(13254522168097688865), KQU(15643659095244365219),\n\tKQU( 5881437660538424982), KQU(11174892200618987379),\n\tKQU(  254409966159711077), KQU(17158413043140549909),\n\tKQU( 3638048789290376272), KQU( 1376816930299489190),\n\tKQU( 4622462095217761923), KQU(15086407973010263515),\n\tKQU(13253971772784692238), KQU( 5270549043541649236),\n\tKQU(11182714186805411604), KQU(12283846437495577140),\n\tKQU( 5297647149908953219), KQU(10047451738316836654),\n\tKQU( 4938228100367874746), KQU(12328523025304077923),\n\tKQU( 3601049438595312361), KQU( 9313624118352733770),\n\tKQU(13322966086117661798), KQU(16660005705644029394),\n\tKQU(11337677526988872373), KQU(13869299102574417795),\n\tKQU(15642043183045645437), KQU( 3021755569085880019),\n\tKQU( 4979741767761188161), KQU(13679979092079279587),\n\tKQU( 3344685842861071743), KQU(13947960059899588104),\n\tKQU(  305806934293368007), KQU( 5749173929201650029),\n\tKQU(11123724852118844098), KQU(15128987688788879802),\n\tKQU(15251651211024665009), KQU( 7689925933816577776),\n\tKQU(16732804392695859449), KQU(17087345401014078468),\n\tKQU(14315108589159048871), KQU( 4820700266619778917),\n\tKQU(16709637539357958441), KQU( 4936227875177351374),\n\tKQU( 2137907697912987247), KQU(11628565601408395420),\n\tKQU( 2333250549241556786), KQU( 5711200379577778637),\n\tKQU( 5170680131529031729), KQU(12620392043061335164),\n\tKQU(   95363390101096078), KQU( 5487981914081709462),\n\tKQU( 1763109823981838620), KQU( 3395861271473224396),\n\tKQU( 1300496844282213595), KQU( 6894316212820232902),\n\tKQU(10673859651135576674), KQU( 5911839658857903252),\n\tKQU(17407110743387299102), KQU( 8257427154623140385),\n\tKQU(11389003026741800267), KQU( 4070043211095013717),\n\tKQU(11663806997145259025), KQU(15265598950648798210),\n\tKQU(  630585789434030934), KQU( 3524446529213587334),\n\tKQU( 7186424168495184211), KQU(10806585451386379021),\n\tKQU(11120017753500499273), KQU( 1586837651387701301),\n\tKQU(17530454400954415544), KQU( 9991670045077880430),\n\tKQU( 7550997268990730180), KQU( 8640249196597379304),\n\tKQU( 3522203892786893823), KQU(10401116549878854788),\n\tKQU(13690285544733124852), KQU( 8295785675455774586),\n\tKQU(15535716172155117603), KQU( 3112108583723722511),\n\tKQU(17633179955339271113), KQU(18154208056063759375),\n\tKQU( 1866409236285815666), KQU(13326075895396412882),\n\tKQU( 8756261842948020025), KQU( 6281852999868439131),\n\tKQU(15087653361275292858), KQU(10333923911152949397),\n\tKQU( 5265567645757408500), KQU(12728041843210352184),\n\tKQU( 6347959327507828759), KQU(  154112802625564758),\n\tKQU(18235228308679780218), KQU( 3253805274673352418),\n\tKQU( 4849171610689031197), KQU(17948529398340432518),\n\tKQU(13803510475637409167), KQU(13506570190409883095),\n\tKQU(15870801273282960805), KQU( 8451286481299170773),\n\tKQU( 9562190620034457541), KQU( 8518905387449138364),\n\tKQU(12681306401363385655), KQU( 3788073690559762558),\n\tKQU( 5256820289573487769), KQU( 2752021372314875467),\n\tKQU( 6354035166862520716), KQU( 4328956378309739069),\n\tKQU(  449087441228269600), KQU( 5533508742653090868),\n\tKQU( 1260389420404746988), KQU(18175394473289055097),\n\tKQU( 1535467109660399420), KQU( 8818894282874061442),\n\tKQU(12140873243824811213), KQU(15031386653823014946),\n\tKQU( 1286028221456149232), KQU( 6329608889367858784),\n\tKQU( 9419654354945132725), KQU( 6094576547061672379),\n\tKQU(17706217251847450255), KQU( 1733495073065878126),\n\tKQU(16918923754607552663), KQU( 8881949849954945044),\n\tKQU(12938977706896313891), KQU(14043628638299793407),\n\tKQU(18393874581723718233), KQU( 6886318534846892044),\n\tKQU(14577870878038334081), KQU(13541558383439414119),\n\tKQU(13570472158807588273), KQU(18300760537910283361),\n\tKQU(  818368572800609205), KQU( 1417000585112573219),\n\tKQU(12337533143867683655), KQU(12433180994702314480),\n\tKQU(  778190005829189083), KQU(13667356216206524711),\n\tKQU( 9866149895295225230), KQU(11043240490417111999),\n\tKQU( 1123933826541378598), KQU( 6469631933605123610),\n\tKQU(14508554074431980040), KQU(13918931242962026714),\n\tKQU( 2870785929342348285), KQU(14786362626740736974),\n\tKQU(13176680060902695786), KQU( 9591778613541679456),\n\tKQU( 9097662885117436706), KQU(  749262234240924947),\n\tKQU( 1944844067793307093), KQU( 4339214904577487742),\n\tKQU( 8009584152961946551), KQU(16073159501225501777),\n\tKQU( 3335870590499306217), KQU(17088312653151202847),\n\tKQU( 3108893142681931848), KQU(16636841767202792021),\n\tKQU(10423316431118400637), KQU( 8008357368674443506),\n\tKQU(11340015231914677875), KQU(17687896501594936090),\n\tKQU(15173627921763199958), KQU(  542569482243721959),\n\tKQU(15071714982769812975), KQU( 4466624872151386956),\n\tKQU( 1901780715602332461), KQU( 9822227742154351098),\n\tKQU( 1479332892928648780), KQU( 6981611948382474400),\n\tKQU( 7620824924456077376), KQU(14095973329429406782),\n\tKQU( 7902744005696185404), KQU(15830577219375036920),\n\tKQU(10287076667317764416), KQU(12334872764071724025),\n\tKQU( 4419302088133544331), KQU(14455842851266090520),\n\tKQU(12488077416504654222), KQU( 7953892017701886766),\n\tKQU( 6331484925529519007), KQU( 4902145853785030022),\n\tKQU(17010159216096443073), KQU(11945354668653886087),\n\tKQU(15112022728645230829), KQU(17363484484522986742),\n\tKQU( 4423497825896692887), KQU( 8155489510809067471),\n\tKQU(  258966605622576285), KQU( 5462958075742020534),\n\tKQU( 6763710214913276228), KQU( 2368935183451109054),\n\tKQU(14209506165246453811), KQU( 2646257040978514881),\n\tKQU( 3776001911922207672), KQU( 1419304601390147631),\n\tKQU(14987366598022458284), KQU( 3977770701065815721),\n\tKQU(  730820417451838898), KQU( 3982991703612885327),\n\tKQU( 2803544519671388477), KQU(17067667221114424649),\n\tKQU( 2922555119737867166), KQU( 1989477584121460932),\n\tKQU(15020387605892337354), KQU( 9293277796427533547),\n\tKQU(10722181424063557247), KQU(16704542332047511651),\n\tKQU( 5008286236142089514), KQU(16174732308747382540),\n\tKQU(17597019485798338402), KQU(13081745199110622093),\n\tKQU( 8850305883842258115), KQU(12723629125624589005),\n\tKQU( 8140566453402805978), KQU(15356684607680935061),\n\tKQU(14222190387342648650), KQU(11134610460665975178),\n\tKQU( 1259799058620984266), KQU(13281656268025610041),\n\tKQU(  298262561068153992), KQU(12277871700239212922),\n\tKQU(13911297774719779438), KQU(16556727962761474934),\n\tKQU(17903010316654728010), KQU( 9682617699648434744),\n\tKQU(14757681836838592850), KQU( 1327242446558524473),\n\tKQU(11126645098780572792), KQU( 1883602329313221774),\n\tKQU( 2543897783922776873), KQU(15029168513767772842),\n\tKQU(12710270651039129878), KQU(16118202956069604504),\n\tKQU(15010759372168680524), KQU( 2296827082251923948),\n\tKQU(10793729742623518101), KQU(13829764151845413046),\n\tKQU(17769301223184451213), KQU( 3118268169210783372),\n\tKQU(17626204544105123127), KQU( 7416718488974352644),\n\tKQU(10450751996212925994), KQU( 9352529519128770586),\n\tKQU(  259347569641110140), KQU( 8048588892269692697),\n\tKQU( 1774414152306494058), KQU(10669548347214355622),\n\tKQU(13061992253816795081), KQU(18432677803063861659),\n\tKQU( 8879191055593984333), KQU(12433753195199268041),\n\tKQU(14919392415439730602), KQU( 6612848378595332963),\n\tKQU( 6320986812036143628), KQU(10465592420226092859),\n\tKQU( 4196009278962570808), KQU( 3747816564473572224),\n\tKQU(17941203486133732898), KQU( 2350310037040505198),\n\tKQU( 5811779859134370113), KQU(10492109599506195126),\n\tKQU( 7699650690179541274), KQU( 1954338494306022961),\n\tKQU(14095816969027231152), KQU( 5841346919964852061),\n\tKQU(14945969510148214735), KQU( 3680200305887550992),\n\tKQU( 6218047466131695792), KQU( 8242165745175775096),\n\tKQU(11021371934053307357), KQU( 1265099502753169797),\n\tKQU( 4644347436111321718), KQU( 3609296916782832859),\n\tKQU( 8109807992218521571), KQU(18387884215648662020),\n\tKQU(14656324896296392902), KQU(17386819091238216751),\n\tKQU(17788300878582317152), KQU( 7919446259742399591),\n\tKQU( 4466613134576358004), KQU(12928181023667938509),\n\tKQU(13147446154454932030), KQU(16552129038252734620),\n\tKQU( 8395299403738822450), KQU(11313817655275361164),\n\tKQU(  434258809499511718), KQU( 2074882104954788676),\n\tKQU( 7929892178759395518), KQU( 9006461629105745388),\n\tKQU( 5176475650000323086), KQU(11128357033468341069),\n\tKQU(12026158851559118955), KQU(14699716249471156500),\n\tKQU(  448982497120206757), KQU( 4156475356685519900),\n\tKQU( 6063816103417215727), KQU(10073289387954971479),\n\tKQU( 8174466846138590962), KQU( 2675777452363449006),\n\tKQU( 9090685420572474281), KQU( 6659652652765562060),\n\tKQU(12923120304018106621), KQU(11117480560334526775),\n\tKQU(  937910473424587511), KQU( 1838692113502346645),\n\tKQU(11133914074648726180), KQU( 7922600945143884053),\n\tKQU(13435287702700959550), KQU( 5287964921251123332),\n\tKQU(11354875374575318947), KQU(17955724760748238133),\n\tKQU(13728617396297106512), KQU( 4107449660118101255),\n\tKQU( 1210269794886589623), KQU(11408687205733456282),\n\tKQU( 4538354710392677887), KQU(13566803319341319267),\n\tKQU(17870798107734050771), KQU( 3354318982568089135),\n\tKQU( 9034450839405133651), KQU(13087431795753424314),\n\tKQU(  950333102820688239), KQU( 1968360654535604116),\n\tKQU(16840551645563314995), KQU( 8867501803892924995),\n\tKQU(11395388644490626845), KQU( 1529815836300732204),\n\tKQU(13330848522996608842), KQU( 1813432878817504265),\n\tKQU( 2336867432693429560), KQU(15192805445973385902),\n\tKQU( 2528593071076407877), KQU(  128459777936689248),\n\tKQU( 9976345382867214866), KQU( 6208885766767996043),\n\tKQU(14982349522273141706), KQU( 3099654362410737822),\n\tKQU(13776700761947297661), KQU( 8806185470684925550),\n\tKQU( 8151717890410585321), KQU(  640860591588072925),\n\tKQU(14592096303937307465), KQU( 9056472419613564846),\n\tKQU(14861544647742266352), KQU(12703771500398470216),\n\tKQU( 3142372800384138465), KQU( 6201105606917248196),\n\tKQU(18337516409359270184), KQU(15042268695665115339),\n\tKQU(15188246541383283846), KQU(12800028693090114519),\n\tKQU( 5992859621101493472), KQU(18278043971816803521),\n\tKQU( 9002773075219424560), KQU( 7325707116943598353),\n\tKQU( 7930571931248040822), KQU( 5645275869617023448),\n\tKQU( 7266107455295958487), KQU( 4363664528273524411),\n\tKQU(14313875763787479809), KQU(17059695613553486802),\n\tKQU( 9247761425889940932), KQU(13704726459237593128),\n\tKQU( 2701312427328909832), KQU(17235532008287243115),\n\tKQU(14093147761491729538), KQU( 6247352273768386516),\n\tKQU( 8268710048153268415), KQU( 7985295214477182083),\n\tKQU(15624495190888896807), KQU( 3772753430045262788),\n\tKQU( 9133991620474991698), KQU( 5665791943316256028),\n\tKQU( 7551996832462193473), KQU(13163729206798953877),\n\tKQU( 9263532074153846374), KQU( 1015460703698618353),\n\tKQU(17929874696989519390), KQU(18257884721466153847),\n\tKQU(16271867543011222991), KQU( 3905971519021791941),\n\tKQU(16814488397137052085), KQU( 1321197685504621613),\n\tKQU( 2870359191894002181), KQU(14317282970323395450),\n\tKQU(13663920845511074366), KQU( 2052463995796539594),\n\tKQU(14126345686431444337), KQU( 1727572121947022534),\n\tKQU(17793552254485594241), KQU( 6738857418849205750),\n\tKQU( 1282987123157442952), KQU(16655480021581159251),\n\tKQU( 6784587032080183866), KQU(14726758805359965162),\n\tKQU( 7577995933961987349), KQU(12539609320311114036),\n\tKQU(10789773033385439494), KQU( 8517001497411158227),\n\tKQU(10075543932136339710), KQU(14838152340938811081),\n\tKQU( 9560840631794044194), KQU(17445736541454117475),\n\tKQU(10633026464336393186), KQU(15705729708242246293),\n\tKQU( 1117517596891411098), KQU( 4305657943415886942),\n\tKQU( 4948856840533979263), KQU(16071681989041789593),\n\tKQU(13723031429272486527), KQU( 7639567622306509462),\n\tKQU(12670424537483090390), KQU( 9715223453097197134),\n\tKQU( 5457173389992686394), KQU(  289857129276135145),\n\tKQU(17048610270521972512), KQU(  692768013309835485),\n\tKQU(14823232360546632057), KQU(18218002361317895936),\n\tKQU( 3281724260212650204), KQU(16453957266549513795),\n\tKQU( 8592711109774511881), KQU(  929825123473369579),\n\tKQU(15966784769764367791), KQU( 9627344291450607588),\n\tKQU(10849555504977813287), KQU( 9234566913936339275),\n\tKQU( 6413807690366911210), KQU(10862389016184219267),\n\tKQU(13842504799335374048), KQU( 1531994113376881174),\n\tKQU( 2081314867544364459), KQU(16430628791616959932),\n\tKQU( 8314714038654394368), KQU( 9155473892098431813),\n\tKQU(12577843786670475704), KQU( 4399161106452401017),\n\tKQU( 1668083091682623186), KQU( 1741383777203714216),\n\tKQU( 2162597285417794374), KQU(15841980159165218736),\n\tKQU( 1971354603551467079), KQU( 1206714764913205968),\n\tKQU( 4790860439591272330), KQU(14699375615594055799),\n\tKQU( 8374423871657449988), KQU(10950685736472937738),\n\tKQU(  697344331343267176), KQU(10084998763118059810),\n\tKQU(12897369539795983124), KQU(12351260292144383605),\n\tKQU( 1268810970176811234), KQU( 7406287800414582768),\n\tKQU(  516169557043807831), KQU( 5077568278710520380),\n\tKQU( 3828791738309039304), KQU( 7721974069946943610),\n\tKQU( 3534670260981096460), KQU( 4865792189600584891),\n\tKQU(16892578493734337298), KQU( 9161499464278042590),\n\tKQU(11976149624067055931), KQU(13219479887277343990),\n\tKQU(14161556738111500680), KQU(14670715255011223056),\n\tKQU( 4671205678403576558), KQU(12633022931454259781),\n\tKQU(14821376219869187646), KQU(  751181776484317028),\n\tKQU( 2192211308839047070), KQU(11787306362361245189),\n\tKQU(10672375120744095707), KQU( 4601972328345244467),\n\tKQU(15457217788831125879), KQU( 8464345256775460809),\n\tKQU(10191938789487159478), KQU( 6184348739615197613),\n\tKQU(11425436778806882100), KQU( 2739227089124319793),\n\tKQU(  461464518456000551), KQU( 4689850170029177442),\n\tKQU( 6120307814374078625), KQU(11153579230681708671),\n\tKQU( 7891721473905347926), KQU(10281646937824872400),\n\tKQU( 3026099648191332248), KQU( 8666750296953273818),\n\tKQU(14978499698844363232), KQU(13303395102890132065),\n\tKQU( 8182358205292864080), KQU(10560547713972971291),\n\tKQU(11981635489418959093), KQU( 3134621354935288409),\n\tKQU(11580681977404383968), KQU(14205530317404088650),\n\tKQU( 5997789011854923157), KQU(13659151593432238041),\n\tKQU(11664332114338865086), KQU( 7490351383220929386),\n\tKQU( 7189290499881530378), KQU(15039262734271020220),\n\tKQU( 2057217285976980055), KQU(  555570804905355739),\n\tKQU(11235311968348555110), KQU(13824557146269603217),\n\tKQU(16906788840653099693), KQU( 7222878245455661677),\n\tKQU( 5245139444332423756), KQU( 4723748462805674292),\n\tKQU(12216509815698568612), KQU(17402362976648951187),\n\tKQU(17389614836810366768), KQU( 4880936484146667711),\n\tKQU( 9085007839292639880), KQU(13837353458498535449),\n\tKQU(11914419854360366677), KQU(16595890135313864103),\n\tKQU( 6313969847197627222), KQU(18296909792163910431),\n\tKQU(10041780113382084042), KQU( 2499478551172884794),\n\tKQU(11057894246241189489), KQU( 9742243032389068555),\n\tKQU(12838934582673196228), KQU(13437023235248490367),\n\tKQU(13372420669446163240), KQU( 6752564244716909224),\n\tKQU( 7157333073400313737), KQU(12230281516370654308),\n\tKQU( 1182884552219419117), KQU( 2955125381312499218),\n\tKQU(10308827097079443249), KQU( 1337648572986534958),\n\tKQU(16378788590020343939), KQU(  108619126514420935),\n\tKQU( 3990981009621629188), KQU( 5460953070230946410),\n\tKQU( 9703328329366531883), KQU(13166631489188077236),\n\tKQU( 1104768831213675170), KQU( 3447930458553877908),\n\tKQU( 8067172487769945676), KQU( 5445802098190775347),\n\tKQU( 3244840981648973873), KQU(17314668322981950060),\n\tKQU( 5006812527827763807), KQU(18158695070225526260),\n\tKQU( 2824536478852417853), KQU(13974775809127519886),\n\tKQU( 9814362769074067392), KQU(17276205156374862128),\n\tKQU(11361680725379306967), KQU( 3422581970382012542),\n\tKQU(11003189603753241266), KQU(11194292945277862261),\n\tKQU( 6839623313908521348), KQU(11935326462707324634),\n\tKQU( 1611456788685878444), KQU(13112620989475558907),\n\tKQU(  517659108904450427), KQU(13558114318574407624),\n\tKQU(15699089742731633077), KQU( 4988979278862685458),\n\tKQU( 8111373583056521297), KQU( 3891258746615399627),\n\tKQU( 8137298251469718086), KQU(12748663295624701649),\n\tKQU( 4389835683495292062), KQU( 5775217872128831729),\n\tKQU( 9462091896405534927), KQU( 8498124108820263989),\n\tKQU( 8059131278842839525), KQU(10503167994254090892),\n\tKQU(11613153541070396656), KQU(18069248738504647790),\n\tKQU(  570657419109768508), KQU( 3950574167771159665),\n\tKQU( 5514655599604313077), KQU( 2908460854428484165),\n\tKQU(10777722615935663114), KQU(12007363304839279486),\n\tKQU( 9800646187569484767), KQU( 8795423564889864287),\n\tKQU(14257396680131028419), KQU( 6405465117315096498),\n\tKQU( 7939411072208774878), KQU(17577572378528990006),\n\tKQU(14785873806715994850), KQU(16770572680854747390),\n\tKQU(18127549474419396481), KQU(11637013449455757750),\n\tKQU(14371851933996761086), KQU( 3601181063650110280),\n\tKQU( 4126442845019316144), KQU(10198287239244320669),\n\tKQU(18000169628555379659), KQU(18392482400739978269),\n\tKQU( 6219919037686919957), KQU( 3610085377719446052),\n\tKQU( 2513925039981776336), KQU(16679413537926716955),\n\tKQU(12903302131714909434), KQU( 5581145789762985009),\n\tKQU(12325955044293303233), KQU(17216111180742141204),\n\tKQU( 6321919595276545740), KQU( 3507521147216174501),\n\tKQU( 9659194593319481840), KQU(11473976005975358326),\n\tKQU(14742730101435987026), KQU(  492845897709954780),\n\tKQU(16976371186162599676), KQU(17712703422837648655),\n\tKQU( 9881254778587061697), KQU( 8413223156302299551),\n\tKQU( 1563841828254089168), KQU( 9996032758786671975),\n\tKQU(  138877700583772667), KQU(13003043368574995989),\n\tKQU( 4390573668650456587), KQU( 8610287390568126755),\n\tKQU(15126904974266642199), KQU( 6703637238986057662),\n\tKQU( 2873075592956810157), KQU( 6035080933946049418),\n\tKQU(13382846581202353014), KQU( 7303971031814642463),\n\tKQU(18418024405307444267), KQU( 5847096731675404647),\n\tKQU( 4035880699639842500), KQU(11525348625112218478),\n\tKQU( 3041162365459574102), KQU( 2604734487727986558),\n\tKQU(15526341771636983145), KQU(14556052310697370254),\n\tKQU(12997787077930808155), KQU( 9601806501755554499),\n\tKQU(11349677952521423389), KQU(14956777807644899350),\n\tKQU(16559736957742852721), KQU(12360828274778140726),\n\tKQU( 6685373272009662513), KQU(16932258748055324130),\n\tKQU(15918051131954158508), KQU( 1692312913140790144),\n\tKQU(  546653826801637367), KQU( 5341587076045986652),\n\tKQU(14975057236342585662), KQU(12374976357340622412),\n\tKQU(10328833995181940552), KQU(12831807101710443149),\n\tKQU(10548514914382545716), KQU( 2217806727199715993),\n\tKQU(12627067369242845138), KQU( 4598965364035438158),\n\tKQU(  150923352751318171), KQU(14274109544442257283),\n\tKQU( 4696661475093863031), KQU( 1505764114384654516),\n\tKQU(10699185831891495147), KQU( 2392353847713620519),\n\tKQU( 3652870166711788383), KQU( 8640653276221911108),\n\tKQU( 3894077592275889704), KQU( 4918592872135964845),\n\tKQU(16379121273281400789), KQU(12058465483591683656),\n\tKQU(11250106829302924945), KQU( 1147537556296983005),\n\tKQU( 6376342756004613268), KQU(14967128191709280506),\n\tKQU(18007449949790627628), KQU( 9497178279316537841),\n\tKQU( 7920174844809394893), KQU(10037752595255719907),\n\tKQU(15875342784985217697), KQU(15311615921712850696),\n\tKQU( 9552902652110992950), KQU(14054979450099721140),\n\tKQU( 5998709773566417349), KQU(18027910339276320187),\n\tKQU( 8223099053868585554), KQU( 7842270354824999767),\n\tKQU( 4896315688770080292), KQU(12969320296569787895),\n\tKQU( 2674321489185759961), KQU( 4053615936864718439),\n\tKQU(11349775270588617578), KQU( 4743019256284553975),\n\tKQU( 5602100217469723769), KQU(14398995691411527813),\n\tKQU( 7412170493796825470), KQU(  836262406131744846),\n\tKQU( 8231086633845153022), KQU( 5161377920438552287),\n\tKQU( 8828731196169924949), KQU(16211142246465502680),\n\tKQU( 3307990879253687818), KQU( 5193405406899782022),\n\tKQU( 8510842117467566693), KQU( 6070955181022405365),\n\tKQU(14482950231361409799), KQU(12585159371331138077),\n\tKQU( 3511537678933588148), KQU( 2041849474531116417),\n\tKQU(10944936685095345792), KQU(18303116923079107729),\n\tKQU( 2720566371239725320), KQU( 4958672473562397622),\n\tKQU( 3032326668253243412), KQU(13689418691726908338),\n\tKQU( 1895205511728843996), KQU( 8146303515271990527),\n\tKQU(16507343500056113480), KQU(  473996939105902919),\n\tKQU( 9897686885246881481), KQU(14606433762712790575),\n\tKQU( 6732796251605566368), KQU( 1399778120855368916),\n\tKQU(  935023885182833777), KQU(16066282816186753477),\n\tKQU( 7291270991820612055), KQU(17530230393129853844),\n\tKQU(10223493623477451366), KQU(15841725630495676683),\n\tKQU(17379567246435515824), KQU( 8588251429375561971),\n\tKQU(18339511210887206423), KQU(17349587430725976100),\n\tKQU(12244876521394838088), KQU( 6382187714147161259),\n\tKQU(12335807181848950831), KQU(16948885622305460665),\n\tKQU(13755097796371520506), KQU(14806740373324947801),\n\tKQU( 4828699633859287703), KQU( 8209879281452301604),\n\tKQU(12435716669553736437), KQU(13970976859588452131),\n\tKQU( 6233960842566773148), KQU(12507096267900505759),\n\tKQU( 1198713114381279421), KQU(14989862731124149015),\n\tKQU(15932189508707978949), KQU( 2526406641432708722),\n\tKQU(   29187427817271982), KQU( 1499802773054556353),\n\tKQU(10816638187021897173), KQU( 5436139270839738132),\n\tKQU( 6659882287036010082), KQU( 2154048955317173697),\n\tKQU(10887317019333757642), KQU(16281091802634424955),\n\tKQU(10754549879915384901), KQU(10760611745769249815),\n\tKQU( 2161505946972504002), KQU( 5243132808986265107),\n\tKQU(10129852179873415416), KQU(  710339480008649081),\n\tKQU( 7802129453068808528), KQU(17967213567178907213),\n\tKQU(15730859124668605599), KQU(13058356168962376502),\n\tKQU( 3701224985413645909), KQU(14464065869149109264),\n\tKQU( 9959272418844311646), KQU(10157426099515958752),\n\tKQU(14013736814538268528), KQU(17797456992065653951),\n\tKQU(17418878140257344806), KQU(15457429073540561521),\n\tKQU( 2184426881360949378), KQU( 2062193041154712416),\n\tKQU( 8553463347406931661), KQU( 4913057625202871854),\n\tKQU( 2668943682126618425), KQU(17064444737891172288),\n\tKQU( 4997115903913298637), KQU(12019402608892327416),\n\tKQU(17603584559765897352), KQU(11367529582073647975),\n\tKQU( 8211476043518436050), KQU( 8676849804070323674),\n\tKQU(18431829230394475730), KQU(10490177861361247904),\n\tKQU( 9508720602025651349), KQU( 7409627448555722700),\n\tKQU( 5804047018862729008), KQU(11943858176893142594),\n\tKQU(11908095418933847092), KQU( 5415449345715887652),\n\tKQU( 1554022699166156407), KQU( 9073322106406017161),\n\tKQU( 7080630967969047082), KQU(18049736940860732943),\n\tKQU(12748714242594196794), KQU( 1226992415735156741),\n\tKQU(17900981019609531193), KQU(11720739744008710999),\n\tKQU( 3006400683394775434), KQU(11347974011751996028),\n\tKQU( 3316999628257954608), KQU( 8384484563557639101),\n\tKQU(18117794685961729767), KQU( 1900145025596618194),\n\tKQU(17459527840632892676), KQU( 5634784101865710994),\n\tKQU( 7918619300292897158), KQU( 3146577625026301350),\n\tKQU( 9955212856499068767), KQU( 1873995843681746975),\n\tKQU( 1561487759967972194), KQU( 8322718804375878474),\n\tKQU(11300284215327028366), KQU( 4667391032508998982),\n\tKQU( 9820104494306625580), KQU(17922397968599970610),\n\tKQU( 1784690461886786712), KQU(14940365084341346821),\n\tKQU( 5348719575594186181), KQU(10720419084507855261),\n\tKQU(14210394354145143274), KQU( 2426468692164000131),\n\tKQU(16271062114607059202), KQU(14851904092357070247),\n\tKQU( 6524493015693121897), KQU( 9825473835127138531),\n\tKQU(14222500616268569578), KQU(15521484052007487468),\n\tKQU(14462579404124614699), KQU(11012375590820665520),\n\tKQU(11625327350536084927), KQU(14452017765243785417),\n\tKQU( 9989342263518766305), KQU( 3640105471101803790),\n\tKQU( 4749866455897513242), KQU(13963064946736312044),\n\tKQU(10007416591973223791), KQU(18314132234717431115),\n\tKQU( 3286596588617483450), KQU( 7726163455370818765),\n\tKQU( 7575454721115379328), KQU( 5308331576437663422),\n\tKQU(18288821894903530934), KQU( 8028405805410554106),\n\tKQU(15744019832103296628), KQU(  149765559630932100),\n\tKQU( 6137705557200071977), KQU(14513416315434803615),\n\tKQU(11665702820128984473), KQU(  218926670505601386),\n\tKQU( 6868675028717769519), KQU(15282016569441512302),\n\tKQU( 5707000497782960236), KQU( 6671120586555079567),\n\tKQU( 2194098052618985448), KQU(16849577895477330978),\n\tKQU(12957148471017466283), KQU( 1997805535404859393),\n\tKQU( 1180721060263860490), KQU(13206391310193756958),\n\tKQU(12980208674461861797), KQU( 3825967775058875366),\n\tKQU(17543433670782042631), KQU( 1518339070120322730),\n\tKQU(16344584340890991669), KQU( 2611327165318529819),\n\tKQU(11265022723283422529), KQU( 4001552800373196817),\n\tKQU(14509595890079346161), KQU( 3528717165416234562),\n\tKQU(18153222571501914072), KQU( 9387182977209744425),\n\tKQU(10064342315985580021), KQU(11373678413215253977),\n\tKQU( 2308457853228798099), KQU( 9729042942839545302),\n\tKQU( 7833785471140127746), KQU( 6351049900319844436),\n\tKQU(14454610627133496067), KQU(12533175683634819111),\n\tKQU(15570163926716513029), KQU(13356980519185762498)\n};\n\nTEST_BEGIN(test_gen_rand_32) {\n\tuint32_t array32[BLOCK_SIZE] JEMALLOC_ATTR(aligned(16));\n\tuint32_t array32_2[BLOCK_SIZE] JEMALLOC_ATTR(aligned(16));\n\tint i;\n\tuint32_t r32;\n\tsfmt_t *ctx;\n\n\tassert_d_le(get_min_array_size32(), BLOCK_SIZE,\n\t    \"Array size too small\");\n\tctx = init_gen_rand(1234);\n\tfill_array32(ctx, array32, BLOCK_SIZE);\n\tfill_array32(ctx, array32_2, BLOCK_SIZE);\n\tfini_gen_rand(ctx);\n\n\tctx = init_gen_rand(1234);\n\tfor (i = 0; i < BLOCK_SIZE; i++) {\n\t\tif (i < COUNT_1) {\n\t\t\tassert_u32_eq(array32[i], init_gen_rand_32_expected[i],\n\t\t\t    \"Output mismatch for i=%d\", i);\n\t\t}\n\t\tr32 = gen_rand32(ctx);\n\t\tassert_u32_eq(r32, array32[i],\n\t\t    \"Mismatch at array32[%d]=%x, gen=%x\", i, array32[i], r32);\n\t}\n\tfor (i = 0; i < COUNT_2; i++) {\n\t\tr32 = gen_rand32(ctx);\n\t\tassert_u32_eq(r32, array32_2[i],\n\t\t    \"Mismatch at array32_2[%d]=%x, gen=%x\", i, array32_2[i],\n\t\t    r32);\n\t}\n\tfini_gen_rand(ctx);\n}\nTEST_END\n\nTEST_BEGIN(test_by_array_32) {\n\tuint32_t array32[BLOCK_SIZE] JEMALLOC_ATTR(aligned(16));\n\tuint32_t array32_2[BLOCK_SIZE] JEMALLOC_ATTR(aligned(16));\n\tint i;\n\tuint32_t ini[4] = {0x1234, 0x5678, 0x9abc, 0xdef0};\n\tuint32_t r32;\n\tsfmt_t *ctx;\n\n\tassert_d_le(get_min_array_size32(), BLOCK_SIZE,\n\t    \"Array size too small\");\n\tctx = init_by_array(ini, 4);\n\tfill_array32(ctx, array32, BLOCK_SIZE);\n\tfill_array32(ctx, array32_2, BLOCK_SIZE);\n\tfini_gen_rand(ctx);\n\n\tctx = init_by_array(ini, 4);\n\tfor (i = 0; i < BLOCK_SIZE; i++) {\n\t\tif (i < COUNT_1) {\n\t\t\tassert_u32_eq(array32[i], init_by_array_32_expected[i],\n\t\t\t    \"Output mismatch for i=%d\", i);\n\t\t}\n\t\tr32 = gen_rand32(ctx);\n\t\tassert_u32_eq(r32, array32[i],\n\t\t    \"Mismatch at array32[%d]=%x, gen=%x\", i, array32[i], r32);\n\t}\n\tfor (i = 0; i < COUNT_2; i++) {\n\t\tr32 = gen_rand32(ctx);\n\t\tassert_u32_eq(r32, array32_2[i],\n\t\t    \"Mismatch at array32_2[%d]=%x, gen=%x\", i, array32_2[i],\n\t\t    r32);\n\t}\n\tfini_gen_rand(ctx);\n}\nTEST_END\n\nTEST_BEGIN(test_gen_rand_64) {\n\tuint64_t array64[BLOCK_SIZE64] JEMALLOC_ATTR(aligned(16));\n\tuint64_t array64_2[BLOCK_SIZE64] JEMALLOC_ATTR(aligned(16));\n\tint i;\n\tuint64_t r;\n\tsfmt_t *ctx;\n\n\tassert_d_le(get_min_array_size64(), BLOCK_SIZE64,\n\t    \"Array size too small\");\n\tctx = init_gen_rand(4321);\n\tfill_array64(ctx, array64, BLOCK_SIZE64);\n\tfill_array64(ctx, array64_2, BLOCK_SIZE64);\n\tfini_gen_rand(ctx);\n\n\tctx = init_gen_rand(4321);\n\tfor (i = 0; i < BLOCK_SIZE64; i++) {\n\t\tif (i < COUNT_1) {\n\t\t\tassert_u64_eq(array64[i], init_gen_rand_64_expected[i],\n\t\t\t    \"Output mismatch for i=%d\", i);\n\t\t}\n\t\tr = gen_rand64(ctx);\n\t\tassert_u64_eq(r, array64[i],\n\t\t    \"Mismatch at array64[%d]=%\"FMTx64\", gen=%\"FMTx64, i,\n\t\t    array64[i], r);\n\t}\n\tfor (i = 0; i < COUNT_2; i++) {\n\t\tr = gen_rand64(ctx);\n\t\tassert_u64_eq(r, array64_2[i],\n\t\t    \"Mismatch at array64_2[%d]=%\"FMTx64\" gen=%\"FMTx64\"\", i,\n\t\t    array64_2[i], r);\n\t}\n\tfini_gen_rand(ctx);\n}\nTEST_END\n\nTEST_BEGIN(test_by_array_64) {\n\tuint64_t array64[BLOCK_SIZE64] JEMALLOC_ATTR(aligned(16));\n\tuint64_t array64_2[BLOCK_SIZE64] JEMALLOC_ATTR(aligned(16));\n\tint i;\n\tuint64_t r;\n\tuint32_t ini[] = {5, 4, 3, 2, 1};\n\tsfmt_t *ctx;\n\n\tassert_d_le(get_min_array_size64(), BLOCK_SIZE64,\n\t    \"Array size too small\");\n\tctx = init_by_array(ini, 5);\n\tfill_array64(ctx, array64, BLOCK_SIZE64);\n\tfill_array64(ctx, array64_2, BLOCK_SIZE64);\n\tfini_gen_rand(ctx);\n\n\tctx = init_by_array(ini, 5);\n\tfor (i = 0; i < BLOCK_SIZE64; i++) {\n\t\tif (i < COUNT_1) {\n\t\t\tassert_u64_eq(array64[i], init_by_array_64_expected[i],\n\t\t\t    \"Output mismatch for i=%d\", i);\n\t\t}\n\t\tr = gen_rand64(ctx);\n\t\tassert_u64_eq(r, array64[i],\n\t\t    \"Mismatch at array64[%d]=%\"FMTx64\" gen=%\"FMTx64, i,\n\t\t    array64[i], r);\n\t}\n\tfor (i = 0; i < COUNT_2; i++) {\n\t\tr = gen_rand64(ctx);\n\t\tassert_u64_eq(r, array64_2[i],\n\t\t    \"Mismatch at array64_2[%d]=%\"FMTx64\" gen=%\"FMTx64, i,\n\t\t    array64_2[i], r);\n\t}\n\tfini_gen_rand(ctx);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_gen_rand_32,\n\t    test_by_array_32,\n\t    test_gen_rand_64,\n\t    test_by_array_64);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/a0.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nTEST_BEGIN(test_a0) {\n\tvoid *p;\n\n\tp = a0malloc(1);\n\tassert_ptr_not_null(p, \"Unexpected a0malloc() error\");\n\ta0dalloc(p);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test_no_malloc_init(\n\t    test_a0);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/arena_reset.c",
    "content": "#ifndef ARENA_RESET_PROF_C_\n#include \"test/jemalloc_test.h\"\n#endif\n\n#include \"jemalloc/internal/extent_mmap.h\"\n#include \"jemalloc/internal/rtree.h\"\n\n#include \"test/extent_hooks.h\"\n\nstatic unsigned\nget_nsizes_impl(const char *cmd) {\n\tunsigned ret;\n\tsize_t z;\n\n\tz = sizeof(unsigned);\n\tassert_d_eq(mallctl(cmd, (void *)&ret, &z, NULL, 0), 0,\n\t    \"Unexpected mallctl(\\\"%s\\\", ...) failure\", cmd);\n\n\treturn ret;\n}\n\nstatic unsigned\nget_nsmall(void) {\n\treturn get_nsizes_impl(\"arenas.nbins\");\n}\n\nstatic unsigned\nget_nlarge(void) {\n\treturn get_nsizes_impl(\"arenas.nlextents\");\n}\n\nstatic size_t\nget_size_impl(const char *cmd, size_t ind) {\n\tsize_t ret;\n\tsize_t z;\n\tsize_t mib[4];\n\tsize_t miblen = 4;\n\n\tz = sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(cmd, mib, &miblen),\n\t    0, \"Unexpected mallctlnametomib(\\\"%s\\\", ...) failure\", cmd);\n\tmib[2] = ind;\n\tz = sizeof(size_t);\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&ret, &z, NULL, 0),\n\t    0, \"Unexpected mallctlbymib([\\\"%s\\\", %zu], ...) failure\", cmd, ind);\n\n\treturn ret;\n}\n\nstatic size_t\nget_small_size(size_t ind) {\n\treturn get_size_impl(\"arenas.bin.0.size\", ind);\n}\n\nstatic size_t\nget_large_size(size_t ind) {\n\treturn get_size_impl(\"arenas.lextent.0.size\", ind);\n}\n\n/* Like ivsalloc(), but safe to call on discarded allocations. */\nstatic size_t\nvsalloc(tsdn_t *tsdn, const void *ptr) {\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\n\textent_t *extent;\n\tszind_t szind;\n\tif (rtree_extent_szind_read(tsdn, &extents_rtree, rtree_ctx,\n\t    (uintptr_t)ptr, false, &extent, &szind)) {\n\t\treturn 0;\n\t}\n\n\tif (extent == NULL) {\n\t\treturn 0;\n\t}\n\tif (extent_state_get(extent) != extent_state_active) {\n\t\treturn 0;\n\t}\n\n\tif (szind == SC_NSIZES) {\n\t\treturn 0;\n\t}\n\n\treturn sz_index2size(szind);\n}\n\nstatic unsigned\ndo_arena_create(extent_hooks_t *h) {\n\tunsigned arena_ind;\n\tsize_t sz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena_ind, &sz,\n\t    (void *)(h != NULL ? &h : NULL), (h != NULL ? sizeof(h) : 0)), 0,\n\t    \"Unexpected mallctl() failure\");\n\treturn arena_ind;\n}\n\nstatic void\ndo_arena_reset_pre(unsigned arena_ind, void ***ptrs, unsigned *nptrs) {\n#define NLARGE\t32\n\tunsigned nsmall, nlarge, i;\n\tsize_t sz;\n\tint flags;\n\ttsdn_t *tsdn;\n\n\tflags = MALLOCX_ARENA(arena_ind) | MALLOCX_TCACHE_NONE;\n\n\tnsmall = get_nsmall();\n\tnlarge = get_nlarge() > NLARGE ? NLARGE : get_nlarge();\n\t*nptrs = nsmall + nlarge;\n\t*ptrs = (void **)malloc(*nptrs * sizeof(void *));\n\tassert_ptr_not_null(*ptrs, \"Unexpected malloc() failure\");\n\n\t/* Allocate objects with a wide range of sizes. */\n\tfor (i = 0; i < nsmall; i++) {\n\t\tsz = get_small_size(i);\n\t\t(*ptrs)[i] = mallocx(sz, flags);\n\t\tassert_ptr_not_null((*ptrs)[i],\n\t\t    \"Unexpected mallocx(%zu, %#x) failure\", sz, flags);\n\t}\n\tfor (i = 0; i < nlarge; i++) {\n\t\tsz = get_large_size(i);\n\t\t(*ptrs)[nsmall + i] = mallocx(sz, flags);\n\t\tassert_ptr_not_null((*ptrs)[i],\n\t\t    \"Unexpected mallocx(%zu, %#x) failure\", sz, flags);\n\t}\n\n\ttsdn = tsdn_fetch();\n\n\t/* Verify allocations. */\n\tfor (i = 0; i < *nptrs; i++) {\n\t\tassert_zu_gt(ivsalloc(tsdn, (*ptrs)[i]), 0,\n\t\t    \"Allocation should have queryable size\");\n\t}\n}\n\nstatic void\ndo_arena_reset_post(void **ptrs, unsigned nptrs, unsigned arena_ind) {\n\ttsdn_t *tsdn;\n\tunsigned i;\n\n\ttsdn = tsdn_fetch();\n\n\tif (have_background_thread) {\n\t\tmalloc_mutex_lock(tsdn,\n\t\t    &background_thread_info_get(arena_ind)->mtx);\n\t}\n\t/* Verify allocations no longer exist. */\n\tfor (i = 0; i < nptrs; i++) {\n\t\tassert_zu_eq(vsalloc(tsdn, ptrs[i]), 0,\n\t\t    \"Allocation should no longer exist\");\n\t}\n\tif (have_background_thread) {\n\t\tmalloc_mutex_unlock(tsdn,\n\t\t    &background_thread_info_get(arena_ind)->mtx);\n\t}\n\n\tfree(ptrs);\n}\n\nstatic void\ndo_arena_reset_destroy(const char *name, unsigned arena_ind) {\n\tsize_t mib[3];\n\tsize_t miblen;\n\n\tmiblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(name, mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tmib[1] = (size_t)arena_ind;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n}\n\nstatic void\ndo_arena_reset(unsigned arena_ind) {\n\tdo_arena_reset_destroy(\"arena.0.reset\", arena_ind);\n}\n\nstatic void\ndo_arena_destroy(unsigned arena_ind) {\n\tdo_arena_reset_destroy(\"arena.0.destroy\", arena_ind);\n}\n\nTEST_BEGIN(test_arena_reset) {\n\tunsigned arena_ind;\n\tvoid **ptrs;\n\tunsigned nptrs;\n\n\tarena_ind = do_arena_create(NULL);\n\tdo_arena_reset_pre(arena_ind, &ptrs, &nptrs);\n\tdo_arena_reset(arena_ind);\n\tdo_arena_reset_post(ptrs, nptrs, arena_ind);\n}\nTEST_END\n\nstatic bool\narena_i_initialized(unsigned arena_ind, bool refresh) {\n\tbool initialized;\n\tsize_t mib[3];\n\tsize_t miblen, sz;\n\n\tif (refresh) {\n\t\tuint64_t epoch = 1;\n\t\tassert_d_eq(mallctl(\"epoch\", NULL, NULL, (void *)&epoch,\n\t\t    sizeof(epoch)), 0, \"Unexpected mallctl() failure\");\n\t}\n\n\tmiblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"arena.0.initialized\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tmib[1] = (size_t)arena_ind;\n\tsz = sizeof(initialized);\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&initialized, &sz, NULL,\n\t    0), 0, \"Unexpected mallctlbymib() failure\");\n\n\treturn initialized;\n}\n\nTEST_BEGIN(test_arena_destroy_initial) {\n\tassert_false(arena_i_initialized(MALLCTL_ARENAS_DESTROYED, false),\n\t    \"Destroyed arena stats should not be initialized\");\n}\nTEST_END\n\nTEST_BEGIN(test_arena_destroy_hooks_default) {\n\tunsigned arena_ind, arena_ind_another, arena_ind_prev;\n\tvoid **ptrs;\n\tunsigned nptrs;\n\n\tarena_ind = do_arena_create(NULL);\n\tdo_arena_reset_pre(arena_ind, &ptrs, &nptrs);\n\n\tassert_false(arena_i_initialized(arena_ind, false),\n\t    \"Arena stats should not be initialized\");\n\tassert_true(arena_i_initialized(arena_ind, true),\n\t    \"Arena stats should be initialized\");\n\n\t/*\n\t * Create another arena before destroying one, to better verify arena\n\t * index reuse.\n\t */\n\tarena_ind_another = do_arena_create(NULL);\n\n\tdo_arena_destroy(arena_ind);\n\n\tassert_false(arena_i_initialized(arena_ind, true),\n\t    \"Arena stats should not be initialized\");\n\tassert_true(arena_i_initialized(MALLCTL_ARENAS_DESTROYED, false),\n\t    \"Destroyed arena stats should be initialized\");\n\n\tdo_arena_reset_post(ptrs, nptrs, arena_ind);\n\n\tarena_ind_prev = arena_ind;\n\tarena_ind = do_arena_create(NULL);\n\tdo_arena_reset_pre(arena_ind, &ptrs, &nptrs);\n\tassert_u_eq(arena_ind, arena_ind_prev,\n\t    \"Arena index should have been recycled\");\n\tdo_arena_destroy(arena_ind);\n\tdo_arena_reset_post(ptrs, nptrs, arena_ind);\n\n\tdo_arena_destroy(arena_ind_another);\n}\nTEST_END\n\n/*\n * Actually unmap extents, regardless of opt_retain, so that attempts to access\n * a destroyed arena's memory will segfault.\n */\nstatic bool\nextent_dalloc_unmap(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    bool committed, unsigned arena_ind) {\n\tTRACE_HOOK(\"%s(extent_hooks=%p, addr=%p, size=%zu, committed=%s, \"\n\t    \"arena_ind=%u)\\n\", __func__, extent_hooks, addr, size, committed ?\n\t    \"true\" : \"false\", arena_ind);\n\tassert_ptr_eq(extent_hooks, &hooks,\n\t    \"extent_hooks should be same as pointer used to set hooks\");\n\tassert_ptr_eq(extent_hooks->dalloc, extent_dalloc_unmap,\n\t    \"Wrong hook function\");\n\tcalled_dalloc = true;\n\tif (!try_dalloc) {\n\t\treturn true;\n\t}\n\tdid_dalloc = true;\n\tif (!maps_coalesce && opt_retain) {\n\t\treturn true;\n\t}\n\tpages_unmap(addr, size);\n\treturn false;\n}\n\nstatic extent_hooks_t hooks_orig;\n\nstatic extent_hooks_t hooks_unmap = {\n\textent_alloc_hook,\n\textent_dalloc_unmap, /* dalloc */\n\textent_destroy_hook,\n\textent_commit_hook,\n\textent_decommit_hook,\n\textent_purge_lazy_hook,\n\textent_purge_forced_hook,\n\textent_split_hook,\n\textent_merge_hook\n};\n\nTEST_BEGIN(test_arena_destroy_hooks_unmap) {\n\tunsigned arena_ind;\n\tvoid **ptrs;\n\tunsigned nptrs;\n\n\textent_hooks_prep();\n\tif (maps_coalesce) {\n\t\ttry_decommit = false;\n\t}\n\tmemcpy(&hooks_orig, &hooks, sizeof(extent_hooks_t));\n\tmemcpy(&hooks, &hooks_unmap, sizeof(extent_hooks_t));\n\n\tdid_alloc = false;\n\tarena_ind = do_arena_create(&hooks);\n\tdo_arena_reset_pre(arena_ind, &ptrs, &nptrs);\n\n\tassert_true(did_alloc, \"Expected alloc\");\n\n\tassert_false(arena_i_initialized(arena_ind, false),\n\t    \"Arena stats should not be initialized\");\n\tassert_true(arena_i_initialized(arena_ind, true),\n\t    \"Arena stats should be initialized\");\n\n\tdid_dalloc = false;\n\tdo_arena_destroy(arena_ind);\n\tassert_true(did_dalloc, \"Expected dalloc\");\n\n\tassert_false(arena_i_initialized(arena_ind, true),\n\t    \"Arena stats should not be initialized\");\n\tassert_true(arena_i_initialized(MALLCTL_ARENAS_DESTROYED, false),\n\t    \"Destroyed arena stats should be initialized\");\n\n\tdo_arena_reset_post(ptrs, nptrs, arena_ind);\n\n\tmemcpy(&hooks, &hooks_orig, sizeof(extent_hooks_t));\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_arena_reset,\n\t    test_arena_destroy_initial,\n\t    test_arena_destroy_hooks_default,\n\t    test_arena_destroy_hooks_unmap);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/arena_reset_prof.c",
    "content": "#include \"test/jemalloc_test.h\"\n#define ARENA_RESET_PROF_C_\n\n#include \"arena_reset.c\"\n"
  },
  {
    "path": "deps/jemalloc/test/unit/arena_reset_prof.sh",
    "content": "#!/bin/sh\n\nexport MALLOC_CONF=\"prof:true,lg_prof_sample:0\"\n"
  },
  {
    "path": "deps/jemalloc/test/unit/atomic.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n/*\n * We *almost* have consistent short names (e.g. \"u32\" for uint32_t, \"b\" for\n * bool, etc.  The one exception is that the short name for void * is \"p\" in\n * some places and \"ptr\" in others.  In the long run it would be nice to unify\n * these, but in the short run we'll use this shim.\n */\n#define assert_p_eq assert_ptr_eq\n\n/*\n * t: the non-atomic type, like \"uint32_t\".\n * ta: the short name for the type, like \"u32\".\n * val[1,2,3]: Values of the given type.  The CAS tests use val2 for expected,\n * and val3 for desired.\n */\n\n#define DO_TESTS(t, ta, val1, val2, val3) do {\t\t\t\t\\\n\tt val;\t\t\t\t\t\t\t\t\\\n\tt expected;\t\t\t\t\t\t\t\\\n\tbool success;\t\t\t\t\t\t\t\\\n\t/* This (along with the load below) also tests ATOMIC_LOAD. */\t\\\n\tatomic_##ta##_t atom = ATOMIC_INIT(val1);\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t/* ATOMIC_INIT and load. */\t\t\t\t\t\\\n\tval = atomic_load_##ta(&atom, ATOMIC_RELAXED);\t\t\t\\\n\tassert_##ta##_eq(val1, val, \"Load or init failed\");\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t/* Store. */\t\t\t\t\t\t\t\\\n\tatomic_store_##ta(&atom, val1, ATOMIC_RELAXED);\t\t\t\\\n\tatomic_store_##ta(&atom, val2, ATOMIC_RELAXED);\t\t\t\\\n\tval = atomic_load_##ta(&atom, ATOMIC_RELAXED);\t\t\t\\\n\tassert_##ta##_eq(val2, val, \"Store failed\");\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t/* Exchange. */\t\t\t\t\t\t\t\\\n\tatomic_store_##ta(&atom, val1, ATOMIC_RELAXED);\t\t\t\\\n\tval = atomic_exchange_##ta(&atom, val2, ATOMIC_RELAXED);\t\\\n\tassert_##ta##_eq(val1, val, \"Exchange returned invalid value\");\t\\\n\tval = atomic_load_##ta(&atom, ATOMIC_RELAXED);\t\t\t\\\n\tassert_##ta##_eq(val2, val, \"Exchange store invalid value\");\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t/* \t\t\t\t\t\t\t\t\\\n\t * Weak CAS.  Spurious failures are allowed, so we loop a few\t\\\n\t * times.\t\t\t\t\t\t\t\\\n\t */\t\t\t\t\t\t\t\t\\\n\tatomic_store_##ta(&atom, val1, ATOMIC_RELAXED);\t\t\t\\\n\tsuccess = false;\t\t\t\t\t\t\\\n\tfor (int i = 0; i < 10 && !success; i++) {\t\t\t\\\n\t\texpected = val2;\t\t\t\t\t\\\n\t\tsuccess = atomic_compare_exchange_weak_##ta(&atom,\t\\\n\t\t    &expected, val3, ATOMIC_RELAXED, ATOMIC_RELAXED);\t\\\n\t\tassert_##ta##_eq(val1, expected, \t\t\t\\\n\t\t    \"CAS should update expected\");\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tassert_b_eq(val1 == val2, success,\t\t\t\t\\\n\t    \"Weak CAS did the wrong state update\");\t\t\t\\\n\tval = atomic_load_##ta(&atom, ATOMIC_RELAXED);\t\t\t\\\n\tif (success) {\t\t\t\t\t\t\t\\\n\t\tassert_##ta##_eq(val3, val,\t\t\t\t\\\n\t\t    \"Successful CAS should update atomic\");\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t\tassert_##ta##_eq(val1, val,\t\t\t\t\\\n\t\t    \"Unsuccessful CAS should not update atomic\");\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t/* Strong CAS. */\t\t\t\t\t\t\\\n\tatomic_store_##ta(&atom, val1, ATOMIC_RELAXED);\t\t\t\\\n\texpected = val2;\t\t\t\t\t\t\\\n\tsuccess = atomic_compare_exchange_strong_##ta(&atom, &expected,\t\\\n\t    val3, ATOMIC_RELAXED, ATOMIC_RELAXED);\t\t\t\\\n\tassert_b_eq(val1 == val2, success,\t\t\t\t\\\n\t    \"Strong CAS did the wrong state update\");\t\t\t\\\n\tval = atomic_load_##ta(&atom, ATOMIC_RELAXED);\t\t\t\\\n\tif (success) {\t\t\t\t\t\t\t\\\n\t\tassert_##ta##_eq(val3, val,\t\t\t\t\\\n\t\t    \"Successful CAS should update atomic\");\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t\tassert_##ta##_eq(val1, val,\t\t\t\t\\\n\t\t    \"Unsuccessful CAS should not update atomic\");\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define DO_INTEGER_TESTS(t, ta, val1, val2) do {\t\t\t\\\n\tatomic_##ta##_t atom;\t\t\t\t\t\t\\\n\tt val;\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t/* Fetch-add. */\t\t\t\t\t\t\\\n\tatomic_store_##ta(&atom, val1, ATOMIC_RELAXED);\t\t\t\\\n\tval = atomic_fetch_add_##ta(&atom, val2, ATOMIC_RELAXED);\t\\\n\tassert_##ta##_eq(val1, val,\t\t\t\t\t\\\n\t    \"Fetch-add should return previous value\");\t\t\t\\\n\tval = atomic_load_##ta(&atom, ATOMIC_RELAXED);\t\t\t\\\n\tassert_##ta##_eq(val1 + val2, val,\t\t\t\t\\\n\t    \"Fetch-add should update atomic\");\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t/* Fetch-sub. */\t\t\t\t\t\t\\\n\tatomic_store_##ta(&atom, val1, ATOMIC_RELAXED);\t\t\t\\\n\tval = atomic_fetch_sub_##ta(&atom, val2, ATOMIC_RELAXED);\t\\\n\tassert_##ta##_eq(val1, val,\t\t\t\t\t\\\n\t    \"Fetch-sub should return previous value\");\t\t\t\\\n\tval = atomic_load_##ta(&atom, ATOMIC_RELAXED);\t\t\t\\\n\tassert_##ta##_eq(val1 - val2, val,\t\t\t\t\\\n\t    \"Fetch-sub should update atomic\");\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t/* Fetch-and. */\t\t\t\t\t\t\\\n\tatomic_store_##ta(&atom, val1, ATOMIC_RELAXED);\t\t\t\\\n\tval = atomic_fetch_and_##ta(&atom, val2, ATOMIC_RELAXED);\t\\\n\tassert_##ta##_eq(val1, val,\t\t\t\t\t\\\n\t    \"Fetch-and should return previous value\");\t\t\t\\\n\tval = atomic_load_##ta(&atom, ATOMIC_RELAXED);\t\t\t\\\n\tassert_##ta##_eq(val1 & val2, val,\t\t\t\t\\\n\t    \"Fetch-and should update atomic\");\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t/* Fetch-or. */\t\t\t\t\t\t\t\\\n\tatomic_store_##ta(&atom, val1, ATOMIC_RELAXED);\t\t\t\\\n\tval = atomic_fetch_or_##ta(&atom, val2, ATOMIC_RELAXED);\t\\\n\tassert_##ta##_eq(val1, val,\t\t\t\t\t\\\n\t    \"Fetch-or should return previous value\");\t\t\t\\\n\tval = atomic_load_##ta(&atom, ATOMIC_RELAXED);\t\t\t\\\n\tassert_##ta##_eq(val1 | val2, val,\t\t\t\t\\\n\t    \"Fetch-or should update atomic\");\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t/* Fetch-xor. */\t\t\t\t\t\t\\\n\tatomic_store_##ta(&atom, val1, ATOMIC_RELAXED);\t\t\t\\\n\tval = atomic_fetch_xor_##ta(&atom, val2, ATOMIC_RELAXED);\t\\\n\tassert_##ta##_eq(val1, val,\t\t\t\t\t\\\n\t    \"Fetch-xor should return previous value\");\t\t\t\\\n\tval = atomic_load_##ta(&atom, ATOMIC_RELAXED);\t\t\t\\\n\tassert_##ta##_eq(val1 ^ val2, val,\t\t\t\t\\\n\t    \"Fetch-xor should update atomic\");\t\t\t\t\\\n} while (0)\n\n#define TEST_STRUCT(t, ta)\t\t\t\t\t\t\\\ntypedef struct {\t\t\t\t\t\t\t\\\n\tt val1;\t\t\t\t\t\t\t\t\\\n\tt val2;\t\t\t\t\t\t\t\t\\\n\tt val3;\t\t\t\t\t\t\t\t\\\n} ta##_test_t;\n\n#define TEST_CASES(t) {\t\t\t\t\t\t\t\\\n\t{(t)-1, (t)-1, (t)-2},\t\t\t\t\t\t\\\n\t{(t)-1, (t) 0, (t)-2},\t\t\t\t\t\t\\\n\t{(t)-1, (t) 1, (t)-2},\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t{(t) 0, (t)-1, (t)-2},\t\t\t\t\t\t\\\n\t{(t) 0, (t) 0, (t)-2},\t\t\t\t\t\t\\\n\t{(t) 0, (t) 1, (t)-2},\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t{(t) 1, (t)-1, (t)-2},\t\t\t\t\t\t\\\n\t{(t) 1, (t) 0, (t)-2},\t\t\t\t\t\t\\\n\t{(t) 1, (t) 1, (t)-2},\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t{(t)0, (t)-(1 << 22), (t)-2},\t\t\t\t\t\\\n\t{(t)0, (t)(1 << 22), (t)-2},\t\t\t\t\t\\\n\t{(t)(1 << 22), (t)-(1 << 22), (t)-2},\t\t\t\t\\\n\t{(t)(1 << 22), (t)(1 << 22), (t)-2}\t\t\t\t\\\n}\n\n#define TEST_BODY(t, ta) do {\t\t\t\t\t\t\\\n\tconst ta##_test_t tests[] = TEST_CASES(t);\t\t\t\\\n\tfor (unsigned i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {\t\\\n\t\tta##_test_t test = tests[i];\t\t\t\t\\\n\t\tDO_TESTS(t, ta, test.val1, test.val2, test.val3);\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define INTEGER_TEST_BODY(t, ta) do {\t\t\t\t\t\\\n\tconst ta##_test_t tests[] = TEST_CASES(t);\t\t\t\\\n\tfor (unsigned i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {\t\\\n\t\tta##_test_t test = tests[i];\t\t\t\t\\\n\t\tDO_TESTS(t, ta, test.val1, test.val2, test.val3);\t\\\n\t\tDO_INTEGER_TESTS(t, ta, test.val1, test.val2);\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\nTEST_STRUCT(uint64_t, u64);\nTEST_BEGIN(test_atomic_u64) {\n#if !(LG_SIZEOF_PTR == 3 || LG_SIZEOF_INT == 3)\n\ttest_skip(\"64-bit atomic operations not supported\");\n#else\n\tINTEGER_TEST_BODY(uint64_t, u64);\n#endif\n}\nTEST_END\n\n\nTEST_STRUCT(uint32_t, u32);\nTEST_BEGIN(test_atomic_u32) {\n\tINTEGER_TEST_BODY(uint32_t, u32);\n}\nTEST_END\n\nTEST_STRUCT(void *, p);\nTEST_BEGIN(test_atomic_p) {\n\tTEST_BODY(void *, p);\n}\nTEST_END\n\nTEST_STRUCT(size_t, zu);\nTEST_BEGIN(test_atomic_zu) {\n\tINTEGER_TEST_BODY(size_t, zu);\n}\nTEST_END\n\nTEST_STRUCT(ssize_t, zd);\nTEST_BEGIN(test_atomic_zd) {\n\tINTEGER_TEST_BODY(ssize_t, zd);\n}\nTEST_END\n\n\nTEST_STRUCT(unsigned, u);\nTEST_BEGIN(test_atomic_u) {\n\tINTEGER_TEST_BODY(unsigned, u);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_atomic_u64,\n\t    test_atomic_u32,\n\t    test_atomic_p,\n\t    test_atomic_zu,\n\t    test_atomic_zd,\n\t    test_atomic_u);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/background_thread.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/util.h\"\n\nstatic void\ntest_switch_background_thread_ctl(bool new_val) {\n\tbool e0, e1;\n\tsize_t sz = sizeof(bool);\n\n\te1 = new_val;\n\tassert_d_eq(mallctl(\"background_thread\", (void *)&e0, &sz,\n\t    &e1, sz), 0, \"Unexpected mallctl() failure\");\n\tassert_b_eq(e0, !e1,\n\t    \"background_thread should be %d before.\\n\", !e1);\n\tif (e1) {\n\t\tassert_zu_gt(n_background_threads, 0,\n\t\t    \"Number of background threads should be non zero.\\n\");\n\t} else {\n\t\tassert_zu_eq(n_background_threads, 0,\n\t\t    \"Number of background threads should be zero.\\n\");\n\t}\n}\n\nstatic void\ntest_repeat_background_thread_ctl(bool before) {\n\tbool e0, e1;\n\tsize_t sz = sizeof(bool);\n\n\te1 = before;\n\tassert_d_eq(mallctl(\"background_thread\", (void *)&e0, &sz,\n\t    &e1, sz), 0, \"Unexpected mallctl() failure\");\n\tassert_b_eq(e0, before,\n\t    \"background_thread should be %d.\\n\", before);\n\tif (e1) {\n\t\tassert_zu_gt(n_background_threads, 0,\n\t\t    \"Number of background threads should be non zero.\\n\");\n\t} else {\n\t\tassert_zu_eq(n_background_threads, 0,\n\t\t    \"Number of background threads should be zero.\\n\");\n\t}\n}\n\nTEST_BEGIN(test_background_thread_ctl) {\n\ttest_skip_if(!have_background_thread);\n\n\tbool e0, e1;\n\tsize_t sz = sizeof(bool);\n\n\tassert_d_eq(mallctl(\"opt.background_thread\", (void *)&e0, &sz,\n\t    NULL, 0), 0, \"Unexpected mallctl() failure\");\n\tassert_d_eq(mallctl(\"background_thread\", (void *)&e1, &sz,\n\t    NULL, 0), 0, \"Unexpected mallctl() failure\");\n\tassert_b_eq(e0, e1,\n\t    \"Default and opt.background_thread does not match.\\n\");\n\tif (e0) {\n\t\ttest_switch_background_thread_ctl(false);\n\t}\n\tassert_zu_eq(n_background_threads, 0,\n\t    \"Number of background threads should be 0.\\n\");\n\n\tfor (unsigned i = 0; i < 4; i++) {\n\t\ttest_switch_background_thread_ctl(true);\n\t\ttest_repeat_background_thread_ctl(true);\n\t\ttest_repeat_background_thread_ctl(true);\n\n\t\ttest_switch_background_thread_ctl(false);\n\t\ttest_repeat_background_thread_ctl(false);\n\t\ttest_repeat_background_thread_ctl(false);\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_background_thread_running) {\n\ttest_skip_if(!have_background_thread);\n\ttest_skip_if(!config_stats);\n\n#if defined(JEMALLOC_BACKGROUND_THREAD)\n\ttsd_t *tsd = tsd_fetch();\n\tbackground_thread_info_t *info = &background_thread_info[0];\n\n\ttest_repeat_background_thread_ctl(false);\n\ttest_switch_background_thread_ctl(true);\n\tassert_b_eq(info->state, background_thread_started,\n\t    \"Background_thread did not start.\\n\");\n\n\tnstime_t start, now;\n\tnstime_init(&start, 0);\n\tnstime_update(&start);\n\n\tbool ran = false;\n\twhile (true) {\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);\n\t\tif (info->tot_n_runs > 0) {\n\t\t\tran = true;\n\t\t}\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);\n\t\tif (ran) {\n\t\t\tbreak;\n\t\t}\n\n\t\tnstime_init(&now, 0);\n\t\tnstime_update(&now);\n\t\tnstime_subtract(&now, &start);\n\t\tassert_u64_lt(nstime_sec(&now), 1000,\n\t\t    \"Background threads did not run for 1000 seconds.\");\n\t\tsleep(1);\n\t}\n\ttest_switch_background_thread_ctl(false);\n#endif\n}\nTEST_END\n\nint\nmain(void) {\n\t/* Background_thread creation tests reentrancy naturally. */\n\treturn test_no_reentrancy(\n\t    test_background_thread_ctl,\n\t    test_background_thread_running);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/background_thread_enable.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nconst char *malloc_conf = \"background_thread:false,narenas:1,max_background_threads:20\";\n\nTEST_BEGIN(test_deferred) {\n\ttest_skip_if(!have_background_thread);\n\n\tunsigned id;\n\tsize_t sz_u = sizeof(unsigned);\n\n\t/*\n\t * 10 here is somewhat arbitrary, except insofar as we want to ensure\n\t * that the number of background threads is smaller than the number of\n\t * arenas.  I'll ragequit long before we have to spin up 10 threads per\n\t * cpu to handle background purging, so this is a conservative\n\t * approximation.\n\t */\n\tfor (unsigned i = 0; i < 10 * ncpus; i++) {\n\t\tassert_d_eq(mallctl(\"arenas.create\", &id, &sz_u, NULL, 0), 0,\n\t\t    \"Failed to create arena\");\n\t}\n\n\tbool enable = true;\n\tsize_t sz_b = sizeof(bool);\n\tassert_d_eq(mallctl(\"background_thread\", NULL, NULL, &enable, sz_b), 0,\n\t    \"Failed to enable background threads\");\n\tenable = false;\n\tassert_d_eq(mallctl(\"background_thread\", NULL, NULL, &enable, sz_b), 0,\n\t    \"Failed to disable background threads\");\n}\nTEST_END\n\nTEST_BEGIN(test_max_background_threads) {\n\ttest_skip_if(!have_background_thread);\n\n\tsize_t max_n_thds;\n\tsize_t opt_max_n_thds;\n\tsize_t sz_m = sizeof(max_n_thds);\n\tassert_d_eq(mallctl(\"opt.max_background_threads\",\n\t    &opt_max_n_thds, &sz_m, NULL, 0), 0,\n\t    \"Failed to get opt.max_background_threads\");\n\tassert_d_eq(mallctl(\"max_background_threads\", &max_n_thds, &sz_m, NULL,\n\t    0), 0, \"Failed to get max background threads\");\n\tassert_zu_eq(opt_max_n_thds, max_n_thds,\n\t    \"max_background_threads and \"\n\t    \"opt.max_background_threads should match\");\n\tassert_d_eq(mallctl(\"max_background_threads\", NULL, NULL, &max_n_thds,\n\t    sz_m), 0, \"Failed to set max background threads\");\n\n\tunsigned id;\n\tsize_t sz_u = sizeof(unsigned);\n\n\tfor (unsigned i = 0; i < 10 * ncpus; i++) {\n\t\tassert_d_eq(mallctl(\"arenas.create\", &id, &sz_u, NULL, 0), 0,\n\t\t    \"Failed to create arena\");\n\t}\n\n\tbool enable = true;\n\tsize_t sz_b = sizeof(bool);\n\tassert_d_eq(mallctl(\"background_thread\", NULL, NULL, &enable, sz_b), 0,\n\t    \"Failed to enable background threads\");\n\tassert_zu_eq(n_background_threads, max_n_thds,\n\t    \"Number of background threads should not change.\\n\");\n\tsize_t new_max_thds = max_n_thds - 1;\n\tif (new_max_thds > 0) {\n\t\tassert_d_eq(mallctl(\"max_background_threads\", NULL, NULL,\n\t\t    &new_max_thds, sz_m), 0,\n\t\t    \"Failed to set max background threads\");\n\t\tassert_zu_eq(n_background_threads, new_max_thds,\n\t\t    \"Number of background threads should decrease by 1.\\n\");\n\t}\n\tnew_max_thds = 1;\n\tassert_d_eq(mallctl(\"max_background_threads\", NULL, NULL, &new_max_thds,\n\t    sz_m), 0, \"Failed to set max background threads\");\n\tassert_zu_eq(n_background_threads, new_max_thds,\n\t    \"Number of background threads should be 1.\\n\");\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test_no_reentrancy(\n\t\ttest_deferred,\n\t\ttest_max_background_threads);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/base.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"test/extent_hooks.h\"\n\nstatic extent_hooks_t hooks_null = {\n\textent_alloc_hook,\n\tNULL, /* dalloc */\n\tNULL, /* destroy */\n\tNULL, /* commit */\n\tNULL, /* decommit */\n\tNULL, /* purge_lazy */\n\tNULL, /* purge_forced */\n\tNULL, /* split */\n\tNULL /* merge */\n};\n\nstatic extent_hooks_t hooks_not_null = {\n\textent_alloc_hook,\n\textent_dalloc_hook,\n\textent_destroy_hook,\n\tNULL, /* commit */\n\textent_decommit_hook,\n\textent_purge_lazy_hook,\n\textent_purge_forced_hook,\n\tNULL, /* split */\n\tNULL /* merge */\n};\n\nTEST_BEGIN(test_base_hooks_default) {\n\tbase_t *base;\n\tsize_t allocated0, allocated1, resident, mapped, n_thp;\n\n\ttsdn_t *tsdn = tsd_tsdn(tsd_fetch());\n\tbase = base_new(tsdn, 0, (extent_hooks_t *)&extent_hooks_default);\n\n\tif (config_stats) {\n\t\tbase_stats_get(tsdn, base, &allocated0, &resident, &mapped,\n\t\t    &n_thp);\n\t\tassert_zu_ge(allocated0, sizeof(base_t),\n\t\t    \"Base header should count as allocated\");\n\t\tif (opt_metadata_thp == metadata_thp_always) {\n\t\t\tassert_zu_gt(n_thp, 0,\n\t\t\t    \"Base should have 1 THP at least.\");\n\t\t}\n\t}\n\n\tassert_ptr_not_null(base_alloc(tsdn, base, 42, 1),\n\t    \"Unexpected base_alloc() failure\");\n\n\tif (config_stats) {\n\t\tbase_stats_get(tsdn, base, &allocated1, &resident, &mapped,\n\t\t    &n_thp);\n\t\tassert_zu_ge(allocated1 - allocated0, 42,\n\t\t    \"At least 42 bytes were allocated by base_alloc()\");\n\t}\n\n\tbase_delete(tsdn, base);\n}\nTEST_END\n\nTEST_BEGIN(test_base_hooks_null) {\n\textent_hooks_t hooks_orig;\n\tbase_t *base;\n\tsize_t allocated0, allocated1, resident, mapped, n_thp;\n\n\textent_hooks_prep();\n\ttry_dalloc = false;\n\ttry_destroy = true;\n\ttry_decommit = false;\n\ttry_purge_lazy = false;\n\ttry_purge_forced = false;\n\tmemcpy(&hooks_orig, &hooks, sizeof(extent_hooks_t));\n\tmemcpy(&hooks, &hooks_null, sizeof(extent_hooks_t));\n\n\ttsdn_t *tsdn = tsd_tsdn(tsd_fetch());\n\tbase = base_new(tsdn, 0, &hooks);\n\tassert_ptr_not_null(base, \"Unexpected base_new() failure\");\n\n\tif (config_stats) {\n\t\tbase_stats_get(tsdn, base, &allocated0, &resident, &mapped,\n\t\t    &n_thp);\n\t\tassert_zu_ge(allocated0, sizeof(base_t),\n\t\t    \"Base header should count as allocated\");\n\t\tif (opt_metadata_thp == metadata_thp_always) {\n\t\t\tassert_zu_gt(n_thp, 0,\n\t\t\t    \"Base should have 1 THP at least.\");\n\t\t}\n\t}\n\n\tassert_ptr_not_null(base_alloc(tsdn, base, 42, 1),\n\t    \"Unexpected base_alloc() failure\");\n\n\tif (config_stats) {\n\t\tbase_stats_get(tsdn, base, &allocated1, &resident, &mapped,\n\t\t    &n_thp);\n\t\tassert_zu_ge(allocated1 - allocated0, 42,\n\t\t    \"At least 42 bytes were allocated by base_alloc()\");\n\t}\n\n\tbase_delete(tsdn, base);\n\n\tmemcpy(&hooks, &hooks_orig, sizeof(extent_hooks_t));\n}\nTEST_END\n\nTEST_BEGIN(test_base_hooks_not_null) {\n\textent_hooks_t hooks_orig;\n\tbase_t *base;\n\tvoid *p, *q, *r, *r_exp;\n\n\textent_hooks_prep();\n\ttry_dalloc = false;\n\ttry_destroy = true;\n\ttry_decommit = false;\n\ttry_purge_lazy = false;\n\ttry_purge_forced = false;\n\tmemcpy(&hooks_orig, &hooks, sizeof(extent_hooks_t));\n\tmemcpy(&hooks, &hooks_not_null, sizeof(extent_hooks_t));\n\n\ttsdn_t *tsdn = tsd_tsdn(tsd_fetch());\n\tdid_alloc = false;\n\tbase = base_new(tsdn, 0, &hooks);\n\tassert_ptr_not_null(base, \"Unexpected base_new() failure\");\n\tassert_true(did_alloc, \"Expected alloc\");\n\n\t/*\n\t * Check for tight packing at specified alignment under simple\n\t * conditions.\n\t */\n\t{\n\t\tconst size_t alignments[] = {\n\t\t\t1,\n\t\t\tQUANTUM,\n\t\t\tQUANTUM << 1,\n\t\t\tCACHELINE,\n\t\t\tCACHELINE << 1,\n\t\t};\n\t\tunsigned i;\n\n\t\tfor (i = 0; i < sizeof(alignments) / sizeof(size_t); i++) {\n\t\t\tsize_t alignment = alignments[i];\n\t\t\tsize_t align_ceil = ALIGNMENT_CEILING(alignment,\n\t\t\t    QUANTUM);\n\t\t\tp = base_alloc(tsdn, base, 1, alignment);\n\t\t\tassert_ptr_not_null(p,\n\t\t\t    \"Unexpected base_alloc() failure\");\n\t\t\tassert_ptr_eq(p,\n\t\t\t    (void *)(ALIGNMENT_CEILING((uintptr_t)p,\n\t\t\t    alignment)), \"Expected quantum alignment\");\n\t\t\tq = base_alloc(tsdn, base, alignment, alignment);\n\t\t\tassert_ptr_not_null(q,\n\t\t\t    \"Unexpected base_alloc() failure\");\n\t\t\tassert_ptr_eq((void *)((uintptr_t)p + align_ceil), q,\n\t\t\t    \"Minimal allocation should take up %zu bytes\",\n\t\t\t    align_ceil);\n\t\t\tr = base_alloc(tsdn, base, 1, alignment);\n\t\t\tassert_ptr_not_null(r,\n\t\t\t    \"Unexpected base_alloc() failure\");\n\t\t\tassert_ptr_eq((void *)((uintptr_t)q + align_ceil), r,\n\t\t\t    \"Minimal allocation should take up %zu bytes\",\n\t\t\t    align_ceil);\n\t\t}\n\t}\n\n\t/*\n\t * Allocate an object that cannot fit in the first block, then verify\n\t * that the first block's remaining space is considered for subsequent\n\t * allocation.\n\t */\n\tassert_zu_ge(extent_bsize_get(&base->blocks->extent), QUANTUM,\n\t    \"Remainder insufficient for test\");\n\t/* Use up all but one quantum of block. */\n\twhile (extent_bsize_get(&base->blocks->extent) > QUANTUM) {\n\t\tp = base_alloc(tsdn, base, QUANTUM, QUANTUM);\n\t\tassert_ptr_not_null(p, \"Unexpected base_alloc() failure\");\n\t}\n\tr_exp = extent_addr_get(&base->blocks->extent);\n\tassert_zu_eq(base->extent_sn_next, 1, \"One extant block expected\");\n\tq = base_alloc(tsdn, base, QUANTUM + 1, QUANTUM);\n\tassert_ptr_not_null(q, \"Unexpected base_alloc() failure\");\n\tassert_ptr_ne(q, r_exp, \"Expected allocation from new block\");\n\tassert_zu_eq(base->extent_sn_next, 2, \"Two extant blocks expected\");\n\tr = base_alloc(tsdn, base, QUANTUM, QUANTUM);\n\tassert_ptr_not_null(r, \"Unexpected base_alloc() failure\");\n\tassert_ptr_eq(r, r_exp, \"Expected allocation from first block\");\n\tassert_zu_eq(base->extent_sn_next, 2, \"Two extant blocks expected\");\n\n\t/*\n\t * Check for proper alignment support when normal blocks are too small.\n\t */\n\t{\n\t\tconst size_t alignments[] = {\n\t\t\tHUGEPAGE,\n\t\t\tHUGEPAGE << 1\n\t\t};\n\t\tunsigned i;\n\n\t\tfor (i = 0; i < sizeof(alignments) / sizeof(size_t); i++) {\n\t\t\tsize_t alignment = alignments[i];\n\t\t\tp = base_alloc(tsdn, base, QUANTUM, alignment);\n\t\t\tassert_ptr_not_null(p,\n\t\t\t    \"Unexpected base_alloc() failure\");\n\t\t\tassert_ptr_eq(p,\n\t\t\t    (void *)(ALIGNMENT_CEILING((uintptr_t)p,\n\t\t\t    alignment)), \"Expected %zu-byte alignment\",\n\t\t\t    alignment);\n\t\t}\n\t}\n\n\tcalled_dalloc = called_destroy = called_decommit = called_purge_lazy =\n\t    called_purge_forced = false;\n\tbase_delete(tsdn, base);\n\tassert_true(called_dalloc, \"Expected dalloc call\");\n\tassert_true(!called_destroy, \"Unexpected destroy call\");\n\tassert_true(called_decommit, \"Expected decommit call\");\n\tassert_true(called_purge_lazy, \"Expected purge_lazy call\");\n\tassert_true(called_purge_forced, \"Expected purge_forced call\");\n\n\ttry_dalloc = true;\n\ttry_destroy = true;\n\ttry_decommit = true;\n\ttry_purge_lazy = true;\n\ttry_purge_forced = true;\n\tmemcpy(&hooks, &hooks_orig, sizeof(extent_hooks_t));\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_base_hooks_default,\n\t    test_base_hooks_null,\n\t    test_base_hooks_not_null);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/binshard.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n/* Config -- \"narenas:1,bin_shards:1-160:16|129-512:4|256-256:8\" */\n\n#define NTHREADS 16\n#define REMOTE_NALLOC 256\n\nstatic void *\nthd_producer(void *varg) {\n\tvoid **mem = varg;\n\tunsigned arena, i;\n\tsize_t sz;\n\n\tsz = sizeof(arena);\n\t/* Remote arena. */\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\tfor (i = 0; i < REMOTE_NALLOC / 2; i++) {\n\t\tmem[i] = mallocx(1, MALLOCX_TCACHE_NONE | MALLOCX_ARENA(arena));\n\t}\n\n\t/* Remote bin. */\n\tfor (; i < REMOTE_NALLOC; i++) {\n\t\tmem[i] = mallocx(1, MALLOCX_TCACHE_NONE | MALLOCX_ARENA(0));\n\t}\n\n\treturn NULL;\n}\n\nTEST_BEGIN(test_producer_consumer) {\n\tthd_t thds[NTHREADS];\n\tvoid *mem[NTHREADS][REMOTE_NALLOC];\n\tunsigned i;\n\n\t/* Create producer threads to allocate. */\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tthd_create(&thds[i], thd_producer, mem[i]);\n\t}\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tthd_join(thds[i], NULL);\n\t}\n\t/* Remote deallocation by the current thread. */\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tfor (unsigned j = 0; j < REMOTE_NALLOC; j++) {\n\t\t\tassert_ptr_not_null(mem[i][j],\n\t\t\t    \"Unexpected remote allocation failure\");\n\t\t\tdallocx(mem[i][j], 0);\n\t\t}\n\t}\n}\nTEST_END\n\nstatic void *\nthd_start(void *varg) {\n\tvoid *ptr, *ptr2;\n\textent_t *extent;\n\tunsigned shard1, shard2;\n\n\ttsdn_t *tsdn = tsdn_fetch();\n\t/* Try triggering allocations from sharded bins. */\n\tfor (unsigned i = 0; i < 1024; i++) {\n\t\tptr = mallocx(1, MALLOCX_TCACHE_NONE);\n\t\tptr2 = mallocx(129, MALLOCX_TCACHE_NONE);\n\n\t\textent = iealloc(tsdn, ptr);\n\t\tshard1 = extent_binshard_get(extent);\n\t\tdallocx(ptr, 0);\n\t\tassert_u_lt(shard1, 16, \"Unexpected bin shard used\");\n\n\t\textent = iealloc(tsdn, ptr2);\n\t\tshard2 = extent_binshard_get(extent);\n\t\tdallocx(ptr2, 0);\n\t\tassert_u_lt(shard2, 4, \"Unexpected bin shard used\");\n\n\t\tif (shard1 > 0 || shard2 > 0) {\n\t\t\t/* Triggered sharded bin usage. */\n\t\t\treturn (void *)(uintptr_t)shard1;\n\t\t}\n\t}\n\n\treturn NULL;\n}\n\nTEST_BEGIN(test_bin_shard_mt) {\n\ttest_skip_if(have_percpu_arena &&\n\t    PERCPU_ARENA_ENABLED(opt_percpu_arena));\n\n\tthd_t thds[NTHREADS];\n\tunsigned i;\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tthd_create(&thds[i], thd_start, NULL);\n\t}\n\tbool sharded = false;\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tvoid *ret;\n\t\tthd_join(thds[i], &ret);\n\t\tif (ret != NULL) {\n\t\t\tsharded = true;\n\t\t}\n\t}\n\tassert_b_eq(sharded, true, \"Did not find sharded bins\");\n}\nTEST_END\n\nTEST_BEGIN(test_bin_shard) {\n\tunsigned nbins, i;\n\tsize_t mib[4], mib2[4];\n\tsize_t miblen, miblen2, len;\n\n\tlen = sizeof(nbins);\n\tassert_d_eq(mallctl(\"arenas.nbins\", (void *)&nbins, &len, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tmiblen = 4;\n\tassert_d_eq(mallctlnametomib(\"arenas.bin.0.nshards\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tmiblen2 = 4;\n\tassert_d_eq(mallctlnametomib(\"arenas.bin.0.size\", mib2, &miblen2), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\n\tfor (i = 0; i < nbins; i++) {\n\t\tuint32_t nshards;\n\t\tsize_t size, sz1, sz2;\n\n\t\tmib[2] = i;\n\t\tsz1 = sizeof(nshards);\n\t\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&nshards, &sz1,\n\t\t    NULL, 0), 0, \"Unexpected mallctlbymib() failure\");\n\n\t\tmib2[2] = i;\n\t\tsz2 = sizeof(size);\n\t\tassert_d_eq(mallctlbymib(mib2, miblen2, (void *)&size, &sz2,\n\t\t    NULL, 0), 0, \"Unexpected mallctlbymib() failure\");\n\n\t\tif (size >= 1 && size <= 128) {\n\t\t\tassert_u_eq(nshards, 16, \"Unexpected nshards\");\n\t\t} else if (size == 256) {\n\t\t\tassert_u_eq(nshards, 8, \"Unexpected nshards\");\n\t\t} else if (size > 128 && size <= 512) {\n\t\t\tassert_u_eq(nshards, 4, \"Unexpected nshards\");\n\t\t} else {\n\t\t\tassert_u_eq(nshards, 1, \"Unexpected nshards\");\n\t\t}\n\t}\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test_no_reentrancy(\n\t    test_bin_shard,\n\t    test_bin_shard_mt,\n\t    test_producer_consumer);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/binshard.sh",
    "content": "#!/bin/sh\n\nexport MALLOC_CONF=\"narenas:1,bin_shards:1-160:16|129-512:4|256-256:8\"\n"
  },
  {
    "path": "deps/jemalloc/test/unit/bit_util.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/bit_util.h\"\n\n#define TEST_POW2_CEIL(t, suf, pri) do {\t\t\t\t\\\n\tunsigned i, pow2;\t\t\t\t\t\t\\\n\tt x;\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tassert_##suf##_eq(pow2_ceil_##suf(0), 0, \"Unexpected result\");\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tfor (i = 0; i < sizeof(t) * 8; i++) {\t\t\t\t\\\n\t\tassert_##suf##_eq(pow2_ceil_##suf(((t)1) << i), ((t)1)\t\\\n\t\t    << i, \"Unexpected result\");\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tfor (i = 2; i < sizeof(t) * 8; i++) {\t\t\t\t\\\n\t\tassert_##suf##_eq(pow2_ceil_##suf((((t)1) << i) - 1),\t\\\n\t\t    ((t)1) << i, \"Unexpected result\");\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tfor (i = 0; i < sizeof(t) * 8 - 1; i++) {\t\t\t\\\n\t\tassert_##suf##_eq(pow2_ceil_##suf((((t)1) << i) + 1),\t\\\n\t\t    ((t)1) << (i+1), \"Unexpected result\");\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tfor (pow2 = 1; pow2 < 25; pow2++) {\t\t\t\t\\\n\t\tfor (x = (((t)1) << (pow2-1)) + 1; x <= ((t)1) << pow2;\t\\\n\t\t    x++) {\t\t\t\t\t\t\\\n\t\t\tassert_##suf##_eq(pow2_ceil_##suf(x),\t\t\\\n\t\t\t    ((t)1) << pow2,\t\t\t\t\\\n\t\t\t    \"Unexpected result, x=%\"pri, x);\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\nTEST_BEGIN(test_pow2_ceil_u64) {\n\tTEST_POW2_CEIL(uint64_t, u64, FMTu64);\n}\nTEST_END\n\nTEST_BEGIN(test_pow2_ceil_u32) {\n\tTEST_POW2_CEIL(uint32_t, u32, FMTu32);\n}\nTEST_END\n\nTEST_BEGIN(test_pow2_ceil_zu) {\n\tTEST_POW2_CEIL(size_t, zu, \"zu\");\n}\nTEST_END\n\nvoid\nassert_lg_ceil_range(size_t input, unsigned answer) {\n\tif (input == 1) {\n\t\tassert_u_eq(0, answer, \"Got %u as lg_ceil of 1\", answer);\n\t\treturn;\n\t}\n\tassert_zu_le(input, (ZU(1) << answer),\n\t    \"Got %u as lg_ceil of %zu\", answer, input);\n\tassert_zu_gt(input, (ZU(1) << (answer - 1)),\n\t    \"Got %u as lg_ceil of %zu\", answer, input);\n}\n\nvoid\nassert_lg_floor_range(size_t input, unsigned answer) {\n\tif (input == 1) {\n\t\tassert_u_eq(0, answer, \"Got %u as lg_floor of 1\", answer);\n\t\treturn;\n\t}\n\tassert_zu_ge(input, (ZU(1) << answer),\n\t    \"Got %u as lg_floor of %zu\", answer, input);\n\tassert_zu_lt(input, (ZU(1) << (answer + 1)),\n\t    \"Got %u as lg_floor of %zu\", answer, input);\n}\n\nTEST_BEGIN(test_lg_ceil_floor) {\n\tfor (size_t i = 1; i < 10 * 1000 * 1000; i++) {\n\t\tassert_lg_ceil_range(i, lg_ceil(i));\n\t\tassert_lg_ceil_range(i, LG_CEIL(i));\n\t\tassert_lg_floor_range(i, lg_floor(i));\n\t\tassert_lg_floor_range(i, LG_FLOOR(i));\n\t}\n\tfor (int i = 10; i < 8 * (1 << LG_SIZEOF_PTR) - 5; i++) {\n\t\tfor (size_t j = 0; j < (1 << 4); j++) {\n\t\t\tsize_t num1 = ((size_t)1 << i)\n\t\t\t    - j * ((size_t)1 << (i - 4));\n\t\t\tsize_t num2 = ((size_t)1 << i)\n\t\t\t    + j * ((size_t)1 << (i - 4));\n\t\t\tassert_zu_ne(num1, 0, \"Invalid lg argument\");\n\t\t\tassert_zu_ne(num2, 0, \"Invalid lg argument\");\n\t\t\tassert_lg_ceil_range(num1, lg_ceil(num1));\n\t\t\tassert_lg_ceil_range(num1, LG_CEIL(num1));\n\t\t\tassert_lg_ceil_range(num2, lg_ceil(num2));\n\t\t\tassert_lg_ceil_range(num2, LG_CEIL(num2));\n\n\t\t\tassert_lg_floor_range(num1, lg_floor(num1));\n\t\t\tassert_lg_floor_range(num1, LG_FLOOR(num1));\n\t\t\tassert_lg_floor_range(num2, lg_floor(num2));\n\t\t\tassert_lg_floor_range(num2, LG_FLOOR(num2));\n\t\t}\n\t}\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_pow2_ceil_u64,\n\t    test_pow2_ceil_u32,\n\t    test_pow2_ceil_zu,\n\t    test_lg_ceil_floor);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/bitmap.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#define NBITS_TAB \\\n    NB( 1) \\\n    NB( 2) \\\n    NB( 3) \\\n    NB( 4) \\\n    NB( 5) \\\n    NB( 6) \\\n    NB( 7) \\\n    NB( 8) \\\n    NB( 9) \\\n    NB(10) \\\n    NB(11) \\\n    NB(12) \\\n    NB(13) \\\n    NB(14) \\\n    NB(15) \\\n    NB(16) \\\n    NB(17) \\\n    NB(18) \\\n    NB(19) \\\n    NB(20) \\\n    NB(21) \\\n    NB(22) \\\n    NB(23) \\\n    NB(24) \\\n    NB(25) \\\n    NB(26) \\\n    NB(27) \\\n    NB(28) \\\n    NB(29) \\\n    NB(30) \\\n    NB(31) \\\n    NB(32) \\\n    \\\n    NB(33) \\\n    NB(34) \\\n    NB(35) \\\n    NB(36) \\\n    NB(37) \\\n    NB(38) \\\n    NB(39) \\\n    NB(40) \\\n    NB(41) \\\n    NB(42) \\\n    NB(43) \\\n    NB(44) \\\n    NB(45) \\\n    NB(46) \\\n    NB(47) \\\n    NB(48) \\\n    NB(49) \\\n    NB(50) \\\n    NB(51) \\\n    NB(52) \\\n    NB(53) \\\n    NB(54) \\\n    NB(55) \\\n    NB(56) \\\n    NB(57) \\\n    NB(58) \\\n    NB(59) \\\n    NB(60) \\\n    NB(61) \\\n    NB(62) \\\n    NB(63) \\\n    NB(64) \\\n    NB(65) \\\n    \\\n    NB(126) \\\n    NB(127) \\\n    NB(128) \\\n    NB(129) \\\n    NB(130) \\\n    \\\n    NB(254) \\\n    NB(255) \\\n    NB(256) \\\n    NB(257) \\\n    NB(258) \\\n    \\\n    NB(510) \\\n    NB(511) \\\n    NB(512) \\\n    NB(513) \\\n    NB(514) \\\n    \\\n    NB(1024) \\\n    NB(2048) \\\n    NB(4096) \\\n    NB(8192) \\\n    NB(16384) \\\n\nstatic void\ntest_bitmap_initializer_body(const bitmap_info_t *binfo, size_t nbits) {\n\tbitmap_info_t binfo_dyn;\n\tbitmap_info_init(&binfo_dyn, nbits);\n\n\tassert_zu_eq(bitmap_size(binfo), bitmap_size(&binfo_dyn),\n\t    \"Unexpected difference between static and dynamic initialization, \"\n\t    \"nbits=%zu\", nbits);\n\tassert_zu_eq(binfo->nbits, binfo_dyn.nbits,\n\t    \"Unexpected difference between static and dynamic initialization, \"\n\t    \"nbits=%zu\", nbits);\n#ifdef BITMAP_USE_TREE\n\tassert_u_eq(binfo->nlevels, binfo_dyn.nlevels,\n\t    \"Unexpected difference between static and dynamic initialization, \"\n\t    \"nbits=%zu\", nbits);\n\t{\n\t\tunsigned i;\n\n\t\tfor (i = 0; i < binfo->nlevels; i++) {\n\t\t\tassert_zu_eq(binfo->levels[i].group_offset,\n\t\t\t    binfo_dyn.levels[i].group_offset,\n\t\t\t    \"Unexpected difference between static and dynamic \"\n\t\t\t    \"initialization, nbits=%zu, level=%u\", nbits, i);\n\t\t}\n\t}\n#else\n\tassert_zu_eq(binfo->ngroups, binfo_dyn.ngroups,\n\t    \"Unexpected difference between static and dynamic initialization\");\n#endif\n}\n\nTEST_BEGIN(test_bitmap_initializer) {\n#define NB(nbits) {\t\t\t\t\t\t\t\\\n\t\tif (nbits <= BITMAP_MAXBITS) {\t\t\t\t\\\n\t\t\tbitmap_info_t binfo =\t\t\t\t\\\n\t\t\t    BITMAP_INFO_INITIALIZER(nbits);\t\t\\\n\t\t\ttest_bitmap_initializer_body(&binfo, nbits);\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\n\tNBITS_TAB\n#undef NB\n}\nTEST_END\n\nstatic size_t\ntest_bitmap_size_body(const bitmap_info_t *binfo, size_t nbits,\n    size_t prev_size) {\n\tsize_t size = bitmap_size(binfo);\n\tassert_zu_ge(size, (nbits >> 3),\n\t    \"Bitmap size is smaller than expected\");\n\tassert_zu_ge(size, prev_size, \"Bitmap size is smaller than expected\");\n\treturn size;\n}\n\nTEST_BEGIN(test_bitmap_size) {\n\tsize_t nbits, prev_size;\n\n\tprev_size = 0;\n\tfor (nbits = 1; nbits <= BITMAP_MAXBITS; nbits++) {\n\t\tbitmap_info_t binfo;\n\t\tbitmap_info_init(&binfo, nbits);\n\t\tprev_size = test_bitmap_size_body(&binfo, nbits, prev_size);\n\t}\n#define NB(nbits) {\t\t\t\t\t\t\t\\\n\t\tbitmap_info_t binfo = BITMAP_INFO_INITIALIZER(nbits);\t\\\n\t\tprev_size = test_bitmap_size_body(&binfo, nbits,\t\\\n\t\t    prev_size);\t\t\t\t\t\t\\\n\t}\n\tprev_size = 0;\n\tNBITS_TAB\n#undef NB\n}\nTEST_END\n\nstatic void\ntest_bitmap_init_body(const bitmap_info_t *binfo, size_t nbits) {\n\tsize_t i;\n\tbitmap_t *bitmap = (bitmap_t *)malloc(bitmap_size(binfo));\n\tassert_ptr_not_null(bitmap, \"Unexpected malloc() failure\");\n\n\tbitmap_init(bitmap, binfo, false);\n\tfor (i = 0; i < nbits; i++) {\n\t\tassert_false(bitmap_get(bitmap, binfo, i),\n\t\t    \"Bit should be unset\");\n\t}\n\n\tbitmap_init(bitmap, binfo, true);\n\tfor (i = 0; i < nbits; i++) {\n\t\tassert_true(bitmap_get(bitmap, binfo, i), \"Bit should be set\");\n\t}\n\n\tfree(bitmap);\n}\n\nTEST_BEGIN(test_bitmap_init) {\n\tsize_t nbits;\n\n\tfor (nbits = 1; nbits <= BITMAP_MAXBITS; nbits++) {\n\t\tbitmap_info_t binfo;\n\t\tbitmap_info_init(&binfo, nbits);\n\t\ttest_bitmap_init_body(&binfo, nbits);\n\t}\n#define NB(nbits) {\t\t\t\t\t\t\t\\\n\t\tbitmap_info_t binfo = BITMAP_INFO_INITIALIZER(nbits);\t\\\n\t\ttest_bitmap_init_body(&binfo, nbits);\t\t\t\\\n\t}\n\tNBITS_TAB\n#undef NB\n}\nTEST_END\n\nstatic void\ntest_bitmap_set_body(const bitmap_info_t *binfo, size_t nbits) {\n\tsize_t i;\n\tbitmap_t *bitmap = (bitmap_t *)malloc(bitmap_size(binfo));\n\tassert_ptr_not_null(bitmap, \"Unexpected malloc() failure\");\n\tbitmap_init(bitmap, binfo, false);\n\n\tfor (i = 0; i < nbits; i++) {\n\t\tbitmap_set(bitmap, binfo, i);\n\t}\n\tassert_true(bitmap_full(bitmap, binfo), \"All bits should be set\");\n\tfree(bitmap);\n}\n\nTEST_BEGIN(test_bitmap_set) {\n\tsize_t nbits;\n\n\tfor (nbits = 1; nbits <= BITMAP_MAXBITS; nbits++) {\n\t\tbitmap_info_t binfo;\n\t\tbitmap_info_init(&binfo, nbits);\n\t\ttest_bitmap_set_body(&binfo, nbits);\n\t}\n#define NB(nbits) {\t\t\t\t\t\t\t\\\n\t\tbitmap_info_t binfo = BITMAP_INFO_INITIALIZER(nbits);\t\\\n\t\ttest_bitmap_set_body(&binfo, nbits);\t\t\t\\\n\t}\n\tNBITS_TAB\n#undef NB\n}\nTEST_END\n\nstatic void\ntest_bitmap_unset_body(const bitmap_info_t *binfo, size_t nbits) {\n\tsize_t i;\n\tbitmap_t *bitmap = (bitmap_t *)malloc(bitmap_size(binfo));\n\tassert_ptr_not_null(bitmap, \"Unexpected malloc() failure\");\n\tbitmap_init(bitmap, binfo, false);\n\n\tfor (i = 0; i < nbits; i++) {\n\t\tbitmap_set(bitmap, binfo, i);\n\t}\n\tassert_true(bitmap_full(bitmap, binfo), \"All bits should be set\");\n\tfor (i = 0; i < nbits; i++) {\n\t\tbitmap_unset(bitmap, binfo, i);\n\t}\n\tfor (i = 0; i < nbits; i++) {\n\t\tbitmap_set(bitmap, binfo, i);\n\t}\n\tassert_true(bitmap_full(bitmap, binfo), \"All bits should be set\");\n\tfree(bitmap);\n}\n\nTEST_BEGIN(test_bitmap_unset) {\n\tsize_t nbits;\n\n\tfor (nbits = 1; nbits <= BITMAP_MAXBITS; nbits++) {\n\t\tbitmap_info_t binfo;\n\t\tbitmap_info_init(&binfo, nbits);\n\t\ttest_bitmap_unset_body(&binfo, nbits);\n\t}\n#define NB(nbits) {\t\t\t\t\t\t\t\\\n\t\tbitmap_info_t binfo = BITMAP_INFO_INITIALIZER(nbits);\t\\\n\t\ttest_bitmap_unset_body(&binfo, nbits);\t\t\t\\\n\t}\n\tNBITS_TAB\n#undef NB\n}\nTEST_END\n\nstatic void\ntest_bitmap_xfu_body(const bitmap_info_t *binfo, size_t nbits) {\n\tbitmap_t *bitmap = (bitmap_t *)malloc(bitmap_size(binfo));\n\tassert_ptr_not_null(bitmap, \"Unexpected malloc() failure\");\n\tbitmap_init(bitmap, binfo, false);\n\n\t/* Iteratively set bits starting at the beginning. */\n\tfor (size_t i = 0; i < nbits; i++) {\n\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, 0), i,\n\t\t    \"First unset bit should be just after previous first unset \"\n\t\t    \"bit\");\n\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, (i > 0) ? i-1 : i), i,\n\t\t    \"First unset bit should be just after previous first unset \"\n\t\t    \"bit\");\n\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, i), i,\n\t\t    \"First unset bit should be just after previous first unset \"\n\t\t    \"bit\");\n\t\tassert_zu_eq(bitmap_sfu(bitmap, binfo), i,\n\t\t    \"First unset bit should be just after previous first unset \"\n\t\t    \"bit\");\n\t}\n\tassert_true(bitmap_full(bitmap, binfo), \"All bits should be set\");\n\n\t/*\n\t * Iteratively unset bits starting at the end, and verify that\n\t * bitmap_sfu() reaches the unset bits.\n\t */\n\tfor (size_t i = nbits - 1; i < nbits; i--) { /* (nbits..0] */\n\t\tbitmap_unset(bitmap, binfo, i);\n\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, 0), i,\n\t\t    \"First unset bit should the bit previously unset\");\n\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, (i > 0) ? i-1 : i), i,\n\t\t    \"First unset bit should the bit previously unset\");\n\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, i), i,\n\t\t    \"First unset bit should the bit previously unset\");\n\t\tassert_zu_eq(bitmap_sfu(bitmap, binfo), i,\n\t\t    \"First unset bit should the bit previously unset\");\n\t\tbitmap_unset(bitmap, binfo, i);\n\t}\n\tassert_false(bitmap_get(bitmap, binfo, 0), \"Bit should be unset\");\n\n\t/*\n\t * Iteratively set bits starting at the beginning, and verify that\n\t * bitmap_sfu() looks past them.\n\t */\n\tfor (size_t i = 1; i < nbits; i++) {\n\t\tbitmap_set(bitmap, binfo, i - 1);\n\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, 0), i,\n\t\t    \"First unset bit should be just after the bit previously \"\n\t\t    \"set\");\n\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, (i > 0) ? i-1 : i), i,\n\t\t    \"First unset bit should be just after the bit previously \"\n\t\t    \"set\");\n\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, i), i,\n\t\t    \"First unset bit should be just after the bit previously \"\n\t\t    \"set\");\n\t\tassert_zu_eq(bitmap_sfu(bitmap, binfo), i,\n\t\t    \"First unset bit should be just after the bit previously \"\n\t\t    \"set\");\n\t\tbitmap_unset(bitmap, binfo, i);\n\t}\n\tassert_zu_eq(bitmap_ffu(bitmap, binfo, 0), nbits - 1,\n\t    \"First unset bit should be the last bit\");\n\tassert_zu_eq(bitmap_ffu(bitmap, binfo, (nbits > 1) ? nbits-2 : nbits-1),\n\t    nbits - 1, \"First unset bit should be the last bit\");\n\tassert_zu_eq(bitmap_ffu(bitmap, binfo, nbits - 1), nbits - 1,\n\t    \"First unset bit should be the last bit\");\n\tassert_zu_eq(bitmap_sfu(bitmap, binfo), nbits - 1,\n\t    \"First unset bit should be the last bit\");\n\tassert_true(bitmap_full(bitmap, binfo), \"All bits should be set\");\n\n\t/*\n\t * Bubble a \"usu\" pattern through the bitmap and verify that\n\t * bitmap_ffu() finds the correct bit for all five min_bit cases.\n\t */\n\tif (nbits >= 3) {\n\t\tfor (size_t i = 0; i < nbits-2; i++) {\n\t\t\tbitmap_unset(bitmap, binfo, i);\n\t\t\tbitmap_unset(bitmap, binfo, i+2);\n\t\t\tif (i > 0) {\n\t\t\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, i-1), i,\n\t\t\t\t    \"Unexpected first unset bit\");\n\t\t\t}\n\t\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, i), i,\n\t\t\t    \"Unexpected first unset bit\");\n\t\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, i+1), i+2,\n\t\t\t    \"Unexpected first unset bit\");\n\t\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, i+2), i+2,\n\t\t\t    \"Unexpected first unset bit\");\n\t\t\tif (i + 3 < nbits) {\n\t\t\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, i+3),\n\t\t\t\t    nbits, \"Unexpected first unset bit\");\n\t\t\t}\n\t\t\tassert_zu_eq(bitmap_sfu(bitmap, binfo), i,\n\t\t\t    \"Unexpected first unset bit\");\n\t\t\tassert_zu_eq(bitmap_sfu(bitmap, binfo), i+2,\n\t\t\t    \"Unexpected first unset bit\");\n\t\t}\n\t}\n\n\t/*\n\t * Unset the last bit, bubble another unset bit through the bitmap, and\n\t * verify that bitmap_ffu() finds the correct bit for all four min_bit\n\t * cases.\n\t */\n\tif (nbits >= 3) {\n\t\tbitmap_unset(bitmap, binfo, nbits-1);\n\t\tfor (size_t i = 0; i < nbits-1; i++) {\n\t\t\tbitmap_unset(bitmap, binfo, i);\n\t\t\tif (i > 0) {\n\t\t\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, i-1), i,\n\t\t\t\t    \"Unexpected first unset bit\");\n\t\t\t}\n\t\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, i), i,\n\t\t\t    \"Unexpected first unset bit\");\n\t\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, i+1), nbits-1,\n\t\t\t    \"Unexpected first unset bit\");\n\t\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, nbits-1),\n\t\t\t    nbits-1, \"Unexpected first unset bit\");\n\n\t\t\tassert_zu_eq(bitmap_sfu(bitmap, binfo), i,\n\t\t\t    \"Unexpected first unset bit\");\n\t\t}\n\t\tassert_zu_eq(bitmap_sfu(bitmap, binfo), nbits-1,\n\t\t    \"Unexpected first unset bit\");\n\t}\n\n\tfree(bitmap);\n}\n\nTEST_BEGIN(test_bitmap_xfu) {\n\tsize_t nbits;\n\n\tfor (nbits = 1; nbits <= BITMAP_MAXBITS; nbits++) {\n\t\tbitmap_info_t binfo;\n\t\tbitmap_info_init(&binfo, nbits);\n\t\ttest_bitmap_xfu_body(&binfo, nbits);\n\t}\n#define NB(nbits) {\t\t\t\t\t\t\t\\\n\t\tbitmap_info_t binfo = BITMAP_INFO_INITIALIZER(nbits);\t\\\n\t\ttest_bitmap_xfu_body(&binfo, nbits);\t\t\t\\\n\t}\n\tNBITS_TAB\n#undef NB\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_bitmap_initializer,\n\t    test_bitmap_size,\n\t    test_bitmap_init,\n\t    test_bitmap_set,\n\t    test_bitmap_unset,\n\t    test_bitmap_xfu);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/ckh.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nTEST_BEGIN(test_new_delete) {\n\ttsd_t *tsd;\n\tckh_t ckh;\n\n\ttsd = tsd_fetch();\n\n\tassert_false(ckh_new(tsd, &ckh, 2, ckh_string_hash,\n\t    ckh_string_keycomp), \"Unexpected ckh_new() error\");\n\tckh_delete(tsd, &ckh);\n\n\tassert_false(ckh_new(tsd, &ckh, 3, ckh_pointer_hash,\n\t    ckh_pointer_keycomp), \"Unexpected ckh_new() error\");\n\tckh_delete(tsd, &ckh);\n}\nTEST_END\n\nTEST_BEGIN(test_count_insert_search_remove) {\n\ttsd_t *tsd;\n\tckh_t ckh;\n\tconst char *strs[] = {\n\t    \"a string\",\n\t    \"A string\",\n\t    \"a string.\",\n\t    \"A string.\"\n\t};\n\tconst char *missing = \"A string not in the hash table.\";\n\tsize_t i;\n\n\ttsd = tsd_fetch();\n\n\tassert_false(ckh_new(tsd, &ckh, 2, ckh_string_hash,\n\t    ckh_string_keycomp), \"Unexpected ckh_new() error\");\n\tassert_zu_eq(ckh_count(&ckh), 0,\n\t    \"ckh_count() should return %zu, but it returned %zu\", ZU(0),\n\t    ckh_count(&ckh));\n\n\t/* Insert. */\n\tfor (i = 0; i < sizeof(strs)/sizeof(const char *); i++) {\n\t\tckh_insert(tsd, &ckh, strs[i], strs[i]);\n\t\tassert_zu_eq(ckh_count(&ckh), i+1,\n\t\t    \"ckh_count() should return %zu, but it returned %zu\", i+1,\n\t\t    ckh_count(&ckh));\n\t}\n\n\t/* Search. */\n\tfor (i = 0; i < sizeof(strs)/sizeof(const char *); i++) {\n\t\tunion {\n\t\t\tvoid *p;\n\t\t\tconst char *s;\n\t\t} k, v;\n\t\tvoid **kp, **vp;\n\t\tconst char *ks, *vs;\n\n\t\tkp = (i & 1) ? &k.p : NULL;\n\t\tvp = (i & 2) ? &v.p : NULL;\n\t\tk.p = NULL;\n\t\tv.p = NULL;\n\t\tassert_false(ckh_search(&ckh, strs[i], kp, vp),\n\t\t    \"Unexpected ckh_search() error\");\n\n\t\tks = (i & 1) ? strs[i] : (const char *)NULL;\n\t\tvs = (i & 2) ? strs[i] : (const char *)NULL;\n\t\tassert_ptr_eq((void *)ks, (void *)k.s, \"Key mismatch, i=%zu\",\n\t\t    i);\n\t\tassert_ptr_eq((void *)vs, (void *)v.s, \"Value mismatch, i=%zu\",\n\t\t    i);\n\t}\n\tassert_true(ckh_search(&ckh, missing, NULL, NULL),\n\t    \"Unexpected ckh_search() success\");\n\n\t/* Remove. */\n\tfor (i = 0; i < sizeof(strs)/sizeof(const char *); i++) {\n\t\tunion {\n\t\t\tvoid *p;\n\t\t\tconst char *s;\n\t\t} k, v;\n\t\tvoid **kp, **vp;\n\t\tconst char *ks, *vs;\n\n\t\tkp = (i & 1) ? &k.p : NULL;\n\t\tvp = (i & 2) ? &v.p : NULL;\n\t\tk.p = NULL;\n\t\tv.p = NULL;\n\t\tassert_false(ckh_remove(tsd, &ckh, strs[i], kp, vp),\n\t\t    \"Unexpected ckh_remove() error\");\n\n\t\tks = (i & 1) ? strs[i] : (const char *)NULL;\n\t\tvs = (i & 2) ? strs[i] : (const char *)NULL;\n\t\tassert_ptr_eq((void *)ks, (void *)k.s, \"Key mismatch, i=%zu\",\n\t\t    i);\n\t\tassert_ptr_eq((void *)vs, (void *)v.s, \"Value mismatch, i=%zu\",\n\t\t    i);\n\t\tassert_zu_eq(ckh_count(&ckh),\n\t\t    sizeof(strs)/sizeof(const char *) - i - 1,\n\t\t    \"ckh_count() should return %zu, but it returned %zu\",\n\t\t        sizeof(strs)/sizeof(const char *) - i - 1,\n\t\t    ckh_count(&ckh));\n\t}\n\n\tckh_delete(tsd, &ckh);\n}\nTEST_END\n\nTEST_BEGIN(test_insert_iter_remove) {\n#define NITEMS ZU(1000)\n\ttsd_t *tsd;\n\tckh_t ckh;\n\tvoid **p[NITEMS];\n\tvoid *q, *r;\n\tsize_t i;\n\n\ttsd = tsd_fetch();\n\n\tassert_false(ckh_new(tsd, &ckh, 2, ckh_pointer_hash,\n\t    ckh_pointer_keycomp), \"Unexpected ckh_new() error\");\n\n\tfor (i = 0; i < NITEMS; i++) {\n\t\tp[i] = mallocx(i+1, 0);\n\t\tassert_ptr_not_null(p[i], \"Unexpected mallocx() failure\");\n\t}\n\n\tfor (i = 0; i < NITEMS; i++) {\n\t\tsize_t j;\n\n\t\tfor (j = i; j < NITEMS; j++) {\n\t\t\tassert_false(ckh_insert(tsd, &ckh, p[j], p[j]),\n\t\t\t    \"Unexpected ckh_insert() failure\");\n\t\t\tassert_false(ckh_search(&ckh, p[j], &q, &r),\n\t\t\t    \"Unexpected ckh_search() failure\");\n\t\t\tassert_ptr_eq(p[j], q, \"Key pointer mismatch\");\n\t\t\tassert_ptr_eq(p[j], r, \"Value pointer mismatch\");\n\t\t}\n\n\t\tassert_zu_eq(ckh_count(&ckh), NITEMS,\n\t\t    \"ckh_count() should return %zu, but it returned %zu\",\n\t\t    NITEMS, ckh_count(&ckh));\n\n\t\tfor (j = i + 1; j < NITEMS; j++) {\n\t\t\tassert_false(ckh_search(&ckh, p[j], NULL, NULL),\n\t\t\t    \"Unexpected ckh_search() failure\");\n\t\t\tassert_false(ckh_remove(tsd, &ckh, p[j], &q, &r),\n\t\t\t    \"Unexpected ckh_remove() failure\");\n\t\t\tassert_ptr_eq(p[j], q, \"Key pointer mismatch\");\n\t\t\tassert_ptr_eq(p[j], r, \"Value pointer mismatch\");\n\t\t\tassert_true(ckh_search(&ckh, p[j], NULL, NULL),\n\t\t\t    \"Unexpected ckh_search() success\");\n\t\t\tassert_true(ckh_remove(tsd, &ckh, p[j], &q, &r),\n\t\t\t    \"Unexpected ckh_remove() success\");\n\t\t}\n\n\t\t{\n\t\t\tbool seen[NITEMS];\n\t\t\tsize_t tabind;\n\n\t\t\tmemset(seen, 0, sizeof(seen));\n\n\t\t\tfor (tabind = 0; !ckh_iter(&ckh, &tabind, &q, &r);) {\n\t\t\t\tsize_t k;\n\n\t\t\t\tassert_ptr_eq(q, r, \"Key and val not equal\");\n\n\t\t\t\tfor (k = 0; k < NITEMS; k++) {\n\t\t\t\t\tif (p[k] == q) {\n\t\t\t\t\t\tassert_false(seen[k],\n\t\t\t\t\t\t    \"Item %zu already seen\", k);\n\t\t\t\t\t\tseen[k] = true;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfor (j = 0; j < i + 1; j++) {\n\t\t\t\tassert_true(seen[j], \"Item %zu not seen\", j);\n\t\t\t}\n\t\t\tfor (; j < NITEMS; j++) {\n\t\t\t\tassert_false(seen[j], \"Item %zu seen\", j);\n\t\t\t}\n\t\t}\n\t}\n\n\tfor (i = 0; i < NITEMS; i++) {\n\t\tassert_false(ckh_search(&ckh, p[i], NULL, NULL),\n\t\t    \"Unexpected ckh_search() failure\");\n\t\tassert_false(ckh_remove(tsd, &ckh, p[i], &q, &r),\n\t\t    \"Unexpected ckh_remove() failure\");\n\t\tassert_ptr_eq(p[i], q, \"Key pointer mismatch\");\n\t\tassert_ptr_eq(p[i], r, \"Value pointer mismatch\");\n\t\tassert_true(ckh_search(&ckh, p[i], NULL, NULL),\n\t\t    \"Unexpected ckh_search() success\");\n\t\tassert_true(ckh_remove(tsd, &ckh, p[i], &q, &r),\n\t\t    \"Unexpected ckh_remove() success\");\n\t\tdallocx(p[i], 0);\n\t}\n\n\tassert_zu_eq(ckh_count(&ckh), 0,\n\t    \"ckh_count() should return %zu, but it returned %zu\",\n\t    ZU(0), ckh_count(&ckh));\n\tckh_delete(tsd, &ckh);\n#undef NITEMS\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_new_delete,\n\t    test_count_insert_search_remove,\n\t    test_insert_iter_remove);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/decay.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/ticker.h\"\n\nstatic nstime_monotonic_t *nstime_monotonic_orig;\nstatic nstime_update_t *nstime_update_orig;\n\nstatic unsigned nupdates_mock;\nstatic nstime_t time_mock;\nstatic bool monotonic_mock;\n\nstatic bool\ncheck_background_thread_enabled(void) {\n\tbool enabled;\n\tsize_t sz = sizeof(bool);\n\tint ret = mallctl(\"background_thread\", (void *)&enabled, &sz, NULL,0);\n\tif (ret == ENOENT) {\n\t\treturn false;\n\t}\n\tassert_d_eq(ret, 0, \"Unexpected mallctl error\");\n\treturn enabled;\n}\n\nstatic bool\nnstime_monotonic_mock(void) {\n\treturn monotonic_mock;\n}\n\nstatic bool\nnstime_update_mock(nstime_t *time) {\n\tnupdates_mock++;\n\tif (monotonic_mock) {\n\t\tnstime_copy(time, &time_mock);\n\t}\n\treturn !monotonic_mock;\n}\n\nstatic unsigned\ndo_arena_create(ssize_t dirty_decay_ms, ssize_t muzzy_decay_ms) {\n\tunsigned arena_ind;\n\tsize_t sz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena_ind, &sz, NULL, 0),\n\t    0, \"Unexpected mallctl() failure\");\n\tsize_t mib[3];\n\tsize_t miblen = sizeof(mib)/sizeof(size_t);\n\n\tassert_d_eq(mallctlnametomib(\"arena.0.dirty_decay_ms\", mib, &miblen),\n\t    0, \"Unexpected mallctlnametomib() failure\");\n\tmib[1] = (size_t)arena_ind;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL,\n\t    (void *)&dirty_decay_ms, sizeof(dirty_decay_ms)), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n\n\tassert_d_eq(mallctlnametomib(\"arena.0.muzzy_decay_ms\", mib, &miblen),\n\t    0, \"Unexpected mallctlnametomib() failure\");\n\tmib[1] = (size_t)arena_ind;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL,\n\t    (void *)&muzzy_decay_ms, sizeof(muzzy_decay_ms)), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n\n\treturn arena_ind;\n}\n\nstatic void\ndo_arena_destroy(unsigned arena_ind) {\n\tsize_t mib[3];\n\tsize_t miblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"arena.0.destroy\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tmib[1] = (size_t)arena_ind;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n}\n\nvoid\ndo_epoch(void) {\n\tuint64_t epoch = 1;\n\tassert_d_eq(mallctl(\"epoch\", NULL, NULL, (void *)&epoch, sizeof(epoch)),\n\t    0, \"Unexpected mallctl() failure\");\n}\n\nvoid\ndo_purge(unsigned arena_ind) {\n\tsize_t mib[3];\n\tsize_t miblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"arena.0.purge\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tmib[1] = (size_t)arena_ind;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n}\n\nvoid\ndo_decay(unsigned arena_ind) {\n\tsize_t mib[3];\n\tsize_t miblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"arena.0.decay\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tmib[1] = (size_t)arena_ind;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n}\n\nstatic uint64_t\nget_arena_npurge_impl(const char *mibname, unsigned arena_ind) {\n\tsize_t mib[4];\n\tsize_t miblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(mibname, mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tmib[2] = (size_t)arena_ind;\n\tuint64_t npurge = 0;\n\tsize_t sz = sizeof(npurge);\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&npurge, &sz, NULL, 0),\n\t    config_stats ? 0 : ENOENT, \"Unexpected mallctlbymib() failure\");\n\treturn npurge;\n}\n\nstatic uint64_t\nget_arena_dirty_npurge(unsigned arena_ind) {\n\tdo_epoch();\n\treturn get_arena_npurge_impl(\"stats.arenas.0.dirty_npurge\", arena_ind);\n}\n\nstatic uint64_t\nget_arena_dirty_purged(unsigned arena_ind) {\n\tdo_epoch();\n\treturn get_arena_npurge_impl(\"stats.arenas.0.dirty_purged\", arena_ind);\n}\n\nstatic uint64_t\nget_arena_muzzy_npurge(unsigned arena_ind) {\n\tdo_epoch();\n\treturn get_arena_npurge_impl(\"stats.arenas.0.muzzy_npurge\", arena_ind);\n}\n\nstatic uint64_t\nget_arena_npurge(unsigned arena_ind) {\n\tdo_epoch();\n\treturn get_arena_npurge_impl(\"stats.arenas.0.dirty_npurge\", arena_ind) +\n\t    get_arena_npurge_impl(\"stats.arenas.0.muzzy_npurge\", arena_ind);\n}\n\nstatic size_t\nget_arena_pdirty(unsigned arena_ind) {\n\tdo_epoch();\n\tsize_t mib[4];\n\tsize_t miblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"stats.arenas.0.pdirty\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tmib[2] = (size_t)arena_ind;\n\tsize_t pdirty;\n\tsize_t sz = sizeof(pdirty);\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&pdirty, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n\treturn pdirty;\n}\n\nstatic size_t\nget_arena_pmuzzy(unsigned arena_ind) {\n\tdo_epoch();\n\tsize_t mib[4];\n\tsize_t miblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"stats.arenas.0.pmuzzy\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tmib[2] = (size_t)arena_ind;\n\tsize_t pmuzzy;\n\tsize_t sz = sizeof(pmuzzy);\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&pmuzzy, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n\treturn pmuzzy;\n}\n\nstatic void *\ndo_mallocx(size_t size, int flags) {\n\tvoid *p = mallocx(size, flags);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\treturn p;\n}\n\nstatic void\ngenerate_dirty(unsigned arena_ind, size_t size) {\n\tint flags = MALLOCX_ARENA(arena_ind) | MALLOCX_TCACHE_NONE;\n\tvoid *p = do_mallocx(size, flags);\n\tdallocx(p, flags);\n}\n\nTEST_BEGIN(test_decay_ticks) {\n\ttest_skip_if(check_background_thread_enabled());\n\n\tticker_t *decay_ticker;\n\tunsigned tick0, tick1, arena_ind;\n\tsize_t sz, large0;\n\tvoid *p;\n\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctl(\"arenas.lextent.0.size\", (void *)&large0, &sz, NULL,\n\t    0), 0, \"Unexpected mallctl failure\");\n\n\t/* Set up a manually managed arena for test. */\n\tarena_ind = do_arena_create(0, 0);\n\n\t/* Migrate to the new arena, and get the ticker. */\n\tunsigned old_arena_ind;\n\tsize_t sz_arena_ind = sizeof(old_arena_ind);\n\tassert_d_eq(mallctl(\"thread.arena\", (void *)&old_arena_ind,\n\t    &sz_arena_ind, (void *)&arena_ind, sizeof(arena_ind)), 0,\n\t    \"Unexpected mallctl() failure\");\n\tdecay_ticker = decay_ticker_get(tsd_fetch(), arena_ind);\n\tassert_ptr_not_null(decay_ticker,\n\t    \"Unexpected failure getting decay ticker\");\n\n\t/*\n\t * Test the standard APIs using a large size class, since we can't\n\t * control tcache interactions for small size classes (except by\n\t * completely disabling tcache for the entire test program).\n\t */\n\n\t/* malloc(). */\n\ttick0 = ticker_read(decay_ticker);\n\tp = malloc(large0);\n\tassert_ptr_not_null(p, \"Unexpected malloc() failure\");\n\ttick1 = ticker_read(decay_ticker);\n\tassert_u32_ne(tick1, tick0, \"Expected ticker to tick during malloc()\");\n\t/* free(). */\n\ttick0 = ticker_read(decay_ticker);\n\tfree(p);\n\ttick1 = ticker_read(decay_ticker);\n\tassert_u32_ne(tick1, tick0, \"Expected ticker to tick during free()\");\n\n\t/* calloc(). */\n\ttick0 = ticker_read(decay_ticker);\n\tp = calloc(1, large0);\n\tassert_ptr_not_null(p, \"Unexpected calloc() failure\");\n\ttick1 = ticker_read(decay_ticker);\n\tassert_u32_ne(tick1, tick0, \"Expected ticker to tick during calloc()\");\n\tfree(p);\n\n\t/* posix_memalign(). */\n\ttick0 = ticker_read(decay_ticker);\n\tassert_d_eq(posix_memalign(&p, sizeof(size_t), large0), 0,\n\t    \"Unexpected posix_memalign() failure\");\n\ttick1 = ticker_read(decay_ticker);\n\tassert_u32_ne(tick1, tick0,\n\t    \"Expected ticker to tick during posix_memalign()\");\n\tfree(p);\n\n\t/* aligned_alloc(). */\n\ttick0 = ticker_read(decay_ticker);\n\tp = aligned_alloc(sizeof(size_t), large0);\n\tassert_ptr_not_null(p, \"Unexpected aligned_alloc() failure\");\n\ttick1 = ticker_read(decay_ticker);\n\tassert_u32_ne(tick1, tick0,\n\t    \"Expected ticker to tick during aligned_alloc()\");\n\tfree(p);\n\n\t/* realloc(). */\n\t/* Allocate. */\n\ttick0 = ticker_read(decay_ticker);\n\tp = realloc(NULL, large0);\n\tassert_ptr_not_null(p, \"Unexpected realloc() failure\");\n\ttick1 = ticker_read(decay_ticker);\n\tassert_u32_ne(tick1, tick0, \"Expected ticker to tick during realloc()\");\n\t/* Reallocate. */\n\ttick0 = ticker_read(decay_ticker);\n\tp = realloc(p, large0);\n\tassert_ptr_not_null(p, \"Unexpected realloc() failure\");\n\ttick1 = ticker_read(decay_ticker);\n\tassert_u32_ne(tick1, tick0, \"Expected ticker to tick during realloc()\");\n\t/* Deallocate. */\n\ttick0 = ticker_read(decay_ticker);\n\trealloc(p, 0);\n\ttick1 = ticker_read(decay_ticker);\n\tassert_u32_ne(tick1, tick0, \"Expected ticker to tick during realloc()\");\n\n\t/*\n\t * Test the *allocx() APIs using large and small size classes, with\n\t * tcache explicitly disabled.\n\t */\n\t{\n\t\tunsigned i;\n\t\tsize_t allocx_sizes[2];\n\t\tallocx_sizes[0] = large0;\n\t\tallocx_sizes[1] = 1;\n\n\t\tfor (i = 0; i < sizeof(allocx_sizes) / sizeof(size_t); i++) {\n\t\t\tsz = allocx_sizes[i];\n\n\t\t\t/* mallocx(). */\n\t\t\ttick0 = ticker_read(decay_ticker);\n\t\t\tp = mallocx(sz, MALLOCX_TCACHE_NONE);\n\t\t\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\t\t\ttick1 = ticker_read(decay_ticker);\n\t\t\tassert_u32_ne(tick1, tick0,\n\t\t\t    \"Expected ticker to tick during mallocx() (sz=%zu)\",\n\t\t\t    sz);\n\t\t\t/* rallocx(). */\n\t\t\ttick0 = ticker_read(decay_ticker);\n\t\t\tp = rallocx(p, sz, MALLOCX_TCACHE_NONE);\n\t\t\tassert_ptr_not_null(p, \"Unexpected rallocx() failure\");\n\t\t\ttick1 = ticker_read(decay_ticker);\n\t\t\tassert_u32_ne(tick1, tick0,\n\t\t\t    \"Expected ticker to tick during rallocx() (sz=%zu)\",\n\t\t\t    sz);\n\t\t\t/* xallocx(). */\n\t\t\ttick0 = ticker_read(decay_ticker);\n\t\t\txallocx(p, sz, 0, MALLOCX_TCACHE_NONE);\n\t\t\ttick1 = ticker_read(decay_ticker);\n\t\t\tassert_u32_ne(tick1, tick0,\n\t\t\t    \"Expected ticker to tick during xallocx() (sz=%zu)\",\n\t\t\t    sz);\n\t\t\t/* dallocx(). */\n\t\t\ttick0 = ticker_read(decay_ticker);\n\t\t\tdallocx(p, MALLOCX_TCACHE_NONE);\n\t\t\ttick1 = ticker_read(decay_ticker);\n\t\t\tassert_u32_ne(tick1, tick0,\n\t\t\t    \"Expected ticker to tick during dallocx() (sz=%zu)\",\n\t\t\t    sz);\n\t\t\t/* sdallocx(). */\n\t\t\tp = mallocx(sz, MALLOCX_TCACHE_NONE);\n\t\t\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\t\t\ttick0 = ticker_read(decay_ticker);\n\t\t\tsdallocx(p, sz, MALLOCX_TCACHE_NONE);\n\t\t\ttick1 = ticker_read(decay_ticker);\n\t\t\tassert_u32_ne(tick1, tick0,\n\t\t\t    \"Expected ticker to tick during sdallocx() \"\n\t\t\t    \"(sz=%zu)\", sz);\n\t\t}\n\t}\n\n\t/*\n\t * Test tcache fill/flush interactions for large and small size classes,\n\t * using an explicit tcache.\n\t */\n\tunsigned tcache_ind, i;\n\tsize_t tcache_sizes[2];\n\ttcache_sizes[0] = large0;\n\ttcache_sizes[1] = 1;\n\n\tsize_t tcache_max, sz_tcache_max;\n\tsz_tcache_max = sizeof(tcache_max);\n\tassert_d_eq(mallctl(\"arenas.tcache_max\", (void *)&tcache_max,\n\t    &sz_tcache_max, NULL, 0), 0, \"Unexpected mallctl() failure\");\n\n\tsz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"tcache.create\", (void *)&tcache_ind, &sz,\n\t    NULL, 0), 0, \"Unexpected mallctl failure\");\n\n\tfor (i = 0; i < sizeof(tcache_sizes) / sizeof(size_t); i++) {\n\t\tsz = tcache_sizes[i];\n\n\t\t/* tcache fill. */\n\t\ttick0 = ticker_read(decay_ticker);\n\t\tp = mallocx(sz, MALLOCX_TCACHE(tcache_ind));\n\t\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\t\ttick1 = ticker_read(decay_ticker);\n\t\tassert_u32_ne(tick1, tick0,\n\t\t    \"Expected ticker to tick during tcache fill \"\n\t\t    \"(sz=%zu)\", sz);\n\t\t/* tcache flush. */\n\t\tdallocx(p, MALLOCX_TCACHE(tcache_ind));\n\t\ttick0 = ticker_read(decay_ticker);\n\t\tassert_d_eq(mallctl(\"tcache.flush\", NULL, NULL,\n\t\t    (void *)&tcache_ind, sizeof(unsigned)), 0,\n\t\t    \"Unexpected mallctl failure\");\n\t\ttick1 = ticker_read(decay_ticker);\n\n\t\t/* Will only tick if it's in tcache. */\n\t\tif (sz <= tcache_max) {\n\t\t\tassert_u32_ne(tick1, tick0,\n\t\t\t    \"Expected ticker to tick during tcache \"\n\t\t\t    \"flush (sz=%zu)\", sz);\n\t\t} else {\n\t\t\tassert_u32_eq(tick1, tick0,\n\t\t\t    \"Unexpected ticker tick during tcache \"\n\t\t\t    \"flush (sz=%zu)\", sz);\n\t\t}\n\t}\n}\nTEST_END\n\nstatic void\ndecay_ticker_helper(unsigned arena_ind, int flags, bool dirty, ssize_t dt,\n    uint64_t dirty_npurge0, uint64_t muzzy_npurge0, bool terminate_asap) {\n#define NINTERVALS 101\n\tnstime_t time, update_interval, decay_ms, deadline;\n\n\tnstime_init(&time, 0);\n\tnstime_update(&time);\n\n\tnstime_init2(&decay_ms, dt, 0);\n\tnstime_copy(&deadline, &time);\n\tnstime_add(&deadline, &decay_ms);\n\n\tnstime_init2(&update_interval, dt, 0);\n\tnstime_idivide(&update_interval, NINTERVALS);\n\n\t/*\n\t * Keep q's slab from being deallocated during the looping below.  If a\n\t * cached slab were to repeatedly come and go during looping, it could\n\t * prevent the decay backlog ever becoming empty.\n\t */\n\tvoid *p = do_mallocx(1, flags);\n\tuint64_t dirty_npurge1, muzzy_npurge1;\n\tdo {\n\t\tfor (unsigned i = 0; i < DECAY_NTICKS_PER_UPDATE / 2;\n\t\t    i++) {\n\t\t\tvoid *q = do_mallocx(1, flags);\n\t\t\tdallocx(q, flags);\n\t\t}\n\t\tdirty_npurge1 = get_arena_dirty_npurge(arena_ind);\n\t\tmuzzy_npurge1 = get_arena_muzzy_npurge(arena_ind);\n\n\t\tnstime_add(&time_mock, &update_interval);\n\t\tnstime_update(&time);\n\t} while (nstime_compare(&time, &deadline) <= 0 && ((dirty_npurge1 ==\n\t    dirty_npurge0 && muzzy_npurge1 == muzzy_npurge0) ||\n\t    !terminate_asap));\n\tdallocx(p, flags);\n\n\tif (config_stats) {\n\t\tassert_u64_gt(dirty_npurge1 + muzzy_npurge1, dirty_npurge0 +\n\t\t    muzzy_npurge0, \"Expected purging to occur\");\n\t}\n#undef NINTERVALS\n}\n\nTEST_BEGIN(test_decay_ticker) {\n\ttest_skip_if(check_background_thread_enabled());\n#define NPS 2048\n\tssize_t ddt = opt_dirty_decay_ms;\n\tssize_t mdt = opt_muzzy_decay_ms;\n\tunsigned arena_ind = do_arena_create(ddt, mdt);\n\tint flags = (MALLOCX_ARENA(arena_ind) | MALLOCX_TCACHE_NONE);\n\tvoid *ps[NPS];\n\tsize_t large;\n\n\t/*\n\t * Allocate a bunch of large objects, pause the clock, deallocate every\n\t * other object (to fragment virtual memory), restore the clock, then\n\t * [md]allocx() in a tight loop while advancing time rapidly to verify\n\t * the ticker triggers purging.\n\t */\n\n\tsize_t tcache_max;\n\tsize_t sz = sizeof(size_t);\n\tassert_d_eq(mallctl(\"arenas.tcache_max\", (void *)&tcache_max, &sz, NULL,\n\t    0), 0, \"Unexpected mallctl failure\");\n\tlarge = nallocx(tcache_max + 1, flags);\n\n\tdo_purge(arena_ind);\n\tuint64_t dirty_npurge0 = get_arena_dirty_npurge(arena_ind);\n\tuint64_t muzzy_npurge0 = get_arena_muzzy_npurge(arena_ind);\n\n\tfor (unsigned i = 0; i < NPS; i++) {\n\t\tps[i] = do_mallocx(large, flags);\n\t}\n\n\tnupdates_mock = 0;\n\tnstime_init(&time_mock, 0);\n\tnstime_update(&time_mock);\n\tmonotonic_mock = true;\n\n\tnstime_monotonic_orig = nstime_monotonic;\n\tnstime_update_orig = nstime_update;\n\tnstime_monotonic = nstime_monotonic_mock;\n\tnstime_update = nstime_update_mock;\n\n\tfor (unsigned i = 0; i < NPS; i += 2) {\n\t\tdallocx(ps[i], flags);\n\t\tunsigned nupdates0 = nupdates_mock;\n\t\tdo_decay(arena_ind);\n\t\tassert_u_gt(nupdates_mock, nupdates0,\n\t\t    \"Expected nstime_update() to be called\");\n\t}\n\n\tdecay_ticker_helper(arena_ind, flags, true, ddt, dirty_npurge0,\n\t    muzzy_npurge0, true);\n\tdecay_ticker_helper(arena_ind, flags, false, ddt+mdt, dirty_npurge0,\n\t    muzzy_npurge0, false);\n\n\tdo_arena_destroy(arena_ind);\n\n\tnstime_monotonic = nstime_monotonic_orig;\n\tnstime_update = nstime_update_orig;\n#undef NPS\n}\nTEST_END\n\nTEST_BEGIN(test_decay_nonmonotonic) {\n\ttest_skip_if(check_background_thread_enabled());\n#define NPS (SMOOTHSTEP_NSTEPS + 1)\n\tint flags = (MALLOCX_ARENA(0) | MALLOCX_TCACHE_NONE);\n\tvoid *ps[NPS];\n\tuint64_t npurge0 = 0;\n\tuint64_t npurge1 = 0;\n\tsize_t sz, large0;\n\tunsigned i, nupdates0;\n\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctl(\"arenas.lextent.0.size\", (void *)&large0, &sz, NULL,\n\t    0), 0, \"Unexpected mallctl failure\");\n\n\tassert_d_eq(mallctl(\"arena.0.purge\", NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctl failure\");\n\tdo_epoch();\n\tsz = sizeof(uint64_t);\n\tnpurge0 = get_arena_npurge(0);\n\n\tnupdates_mock = 0;\n\tnstime_init(&time_mock, 0);\n\tnstime_update(&time_mock);\n\tmonotonic_mock = false;\n\n\tnstime_monotonic_orig = nstime_monotonic;\n\tnstime_update_orig = nstime_update;\n\tnstime_monotonic = nstime_monotonic_mock;\n\tnstime_update = nstime_update_mock;\n\n\tfor (i = 0; i < NPS; i++) {\n\t\tps[i] = mallocx(large0, flags);\n\t\tassert_ptr_not_null(ps[i], \"Unexpected mallocx() failure\");\n\t}\n\n\tfor (i = 0; i < NPS; i++) {\n\t\tdallocx(ps[i], flags);\n\t\tnupdates0 = nupdates_mock;\n\t\tassert_d_eq(mallctl(\"arena.0.decay\", NULL, NULL, NULL, 0), 0,\n\t\t    \"Unexpected arena.0.decay failure\");\n\t\tassert_u_gt(nupdates_mock, nupdates0,\n\t\t    \"Expected nstime_update() to be called\");\n\t}\n\n\tdo_epoch();\n\tsz = sizeof(uint64_t);\n\tnpurge1 = get_arena_npurge(0);\n\n\tif (config_stats) {\n\t\tassert_u64_eq(npurge0, npurge1, \"Unexpected purging occurred\");\n\t}\n\n\tnstime_monotonic = nstime_monotonic_orig;\n\tnstime_update = nstime_update_orig;\n#undef NPS\n}\nTEST_END\n\nTEST_BEGIN(test_decay_now) {\n\ttest_skip_if(check_background_thread_enabled());\n\n\tunsigned arena_ind = do_arena_create(0, 0);\n\tassert_zu_eq(get_arena_pdirty(arena_ind), 0, \"Unexpected dirty pages\");\n\tassert_zu_eq(get_arena_pmuzzy(arena_ind), 0, \"Unexpected muzzy pages\");\n\tsize_t sizes[] = {16, PAGE<<2, HUGEPAGE<<2};\n\t/* Verify that dirty/muzzy pages never linger after deallocation. */\n\tfor (unsigned i = 0; i < sizeof(sizes)/sizeof(size_t); i++) {\n\t\tsize_t size = sizes[i];\n\t\tgenerate_dirty(arena_ind, size);\n\t\tassert_zu_eq(get_arena_pdirty(arena_ind), 0,\n\t\t    \"Unexpected dirty pages\");\n\t\tassert_zu_eq(get_arena_pmuzzy(arena_ind), 0,\n\t\t    \"Unexpected muzzy pages\");\n\t}\n\tdo_arena_destroy(arena_ind);\n}\nTEST_END\n\nTEST_BEGIN(test_decay_never) {\n\ttest_skip_if(check_background_thread_enabled() || !config_stats);\n\n\tunsigned arena_ind = do_arena_create(-1, -1);\n\tint flags = MALLOCX_ARENA(arena_ind) | MALLOCX_TCACHE_NONE;\n\tassert_zu_eq(get_arena_pdirty(arena_ind), 0, \"Unexpected dirty pages\");\n\tassert_zu_eq(get_arena_pmuzzy(arena_ind), 0, \"Unexpected muzzy pages\");\n\tsize_t sizes[] = {16, PAGE<<2, HUGEPAGE<<2};\n\tvoid *ptrs[sizeof(sizes)/sizeof(size_t)];\n\tfor (unsigned i = 0; i < sizeof(sizes)/sizeof(size_t); i++) {\n\t\tptrs[i] = do_mallocx(sizes[i], flags);\n\t}\n\t/* Verify that each deallocation generates additional dirty pages. */\n\tsize_t pdirty_prev = get_arena_pdirty(arena_ind);\n\tsize_t pmuzzy_prev = get_arena_pmuzzy(arena_ind);\n\tassert_zu_eq(pdirty_prev, 0, \"Unexpected dirty pages\");\n\tassert_zu_eq(pmuzzy_prev, 0, \"Unexpected muzzy pages\");\n\tfor (unsigned i = 0; i < sizeof(sizes)/sizeof(size_t); i++) {\n\t\tdallocx(ptrs[i], flags);\n\t\tsize_t pdirty = get_arena_pdirty(arena_ind);\n\t\tsize_t pmuzzy = get_arena_pmuzzy(arena_ind);\n\t\tassert_zu_gt(pdirty + (size_t)get_arena_dirty_purged(arena_ind),\n\t\t    pdirty_prev, \"Expected dirty pages to increase.\");\n\t\tassert_zu_eq(pmuzzy, 0, \"Unexpected muzzy pages\");\n\t\tpdirty_prev = pdirty;\n\t}\n\tdo_arena_destroy(arena_ind);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_decay_ticks,\n\t    test_decay_ticker,\n\t    test_decay_nonmonotonic,\n\t    test_decay_now,\n\t    test_decay_never);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/decay.sh",
    "content": "#!/bin/sh\n\nexport MALLOC_CONF=\"dirty_decay_ms:1000,muzzy_decay_ms:1000,lg_tcache_max:0\"\n"
  },
  {
    "path": "deps/jemalloc/test/unit/div.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/div.h\"\n\nTEST_BEGIN(test_div_exhaustive) {\n\tfor (size_t divisor = 2; divisor < 1000 * 1000; ++divisor) {\n\t\tdiv_info_t div_info;\n\t\tdiv_init(&div_info, divisor);\n\t\tsize_t max = 1000 * divisor;\n\t\tif (max < 1000 * 1000) {\n\t\t\tmax = 1000 * 1000;\n\t\t}\n\t\tfor (size_t dividend = 0; dividend < 1000 * divisor;\n\t\t    dividend += divisor) {\n\t\t\tsize_t quotient = div_compute(\n\t\t\t    &div_info, dividend);\n\t\t\tassert_zu_eq(dividend, quotient * divisor,\n\t\t\t    \"With divisor = %zu, dividend = %zu, \"\n\t\t\t    \"got quotient %zu\", divisor, dividend, quotient);\n\t\t}\n\t}\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test_no_reentrancy(\n\t    test_div_exhaustive);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/emitter.c",
    "content": "#include \"test/jemalloc_test.h\"\n#include \"jemalloc/internal/emitter.h\"\n\n/*\n * This is so useful for debugging and feature work, we'll leave printing\n * functionality committed but disabled by default.\n */\n/* Print the text as it will appear. */\nstatic bool print_raw = false;\n/* Print the text escaped, so it can be copied back into the test case. */\nstatic bool print_escaped = false;\n\ntypedef struct buf_descriptor_s buf_descriptor_t;\nstruct buf_descriptor_s {\n\tchar *buf;\n\tsize_t len;\n\tbool mid_quote;\n};\n\n/*\n * Forwards all writes to the passed-in buf_v (which should be cast from a\n * buf_descriptor_t *).\n */\nstatic void\nforwarding_cb(void *buf_descriptor_v, const char *str) {\n\tbuf_descriptor_t *buf_descriptor = (buf_descriptor_t *)buf_descriptor_v;\n\n\tif (print_raw) {\n\t\tmalloc_printf(\"%s\", str);\n\t}\n\tif (print_escaped) {\n\t\tconst char *it = str;\n\t\twhile (*it != '\\0') {\n\t\t\tif (!buf_descriptor->mid_quote) {\n\t\t\t\tmalloc_printf(\"\\\"\");\n\t\t\t\tbuf_descriptor->mid_quote = true;\n\t\t\t}\n\t\t\tswitch (*it) {\n\t\t\tcase '\\\\':\n\t\t\t\tmalloc_printf(\"\\\\\");\n\t\t\t\tbreak;\n\t\t\tcase '\\\"':\n\t\t\t\tmalloc_printf(\"\\\\\\\"\");\n\t\t\t\tbreak;\n\t\t\tcase '\\t':\n\t\t\t\tmalloc_printf(\"\\\\t\");\n\t\t\t\tbreak;\n\t\t\tcase '\\n':\n\t\t\t\tmalloc_printf(\"\\\\n\\\"\\n\");\n\t\t\t\tbuf_descriptor->mid_quote = false;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tmalloc_printf(\"%c\", *it);\n\t\t\t}\n\t\t\tit++;\n\t\t}\n\t}\n\n\tsize_t written = malloc_snprintf(buf_descriptor->buf,\n\t    buf_descriptor->len, \"%s\", str);\n\tassert_zu_eq(written, strlen(str), \"Buffer overflow!\");\n\tbuf_descriptor->buf += written;\n\tbuf_descriptor->len -= written;\n\tassert_zu_gt(buf_descriptor->len, 0, \"Buffer out of space!\");\n}\n\nstatic void\nassert_emit_output(void (*emit_fn)(emitter_t *),\n    const char *expected_json_output, const char *expected_table_output) {\n\temitter_t emitter;\n\tchar buf[MALLOC_PRINTF_BUFSIZE];\n\tbuf_descriptor_t buf_descriptor;\n\n\tbuf_descriptor.buf = buf;\n\tbuf_descriptor.len = MALLOC_PRINTF_BUFSIZE;\n\tbuf_descriptor.mid_quote = false;\n\n\temitter_init(&emitter, emitter_output_json, &forwarding_cb,\n\t    &buf_descriptor);\n\t(*emit_fn)(&emitter);\n\tassert_str_eq(expected_json_output, buf, \"json output failure\");\n\n\tbuf_descriptor.buf = buf;\n\tbuf_descriptor.len = MALLOC_PRINTF_BUFSIZE;\n\tbuf_descriptor.mid_quote = false;\n\n\temitter_init(&emitter, emitter_output_table, &forwarding_cb,\n\t    &buf_descriptor);\n\t(*emit_fn)(&emitter);\n\tassert_str_eq(expected_table_output, buf, \"table output failure\");\n}\n\nstatic void\nemit_dict(emitter_t *emitter) {\n\tbool b_false = false;\n\tbool b_true = true;\n\tint i_123 = 123;\n\tconst char *str = \"a string\";\n\n\temitter_begin(emitter);\n\temitter_dict_begin(emitter, \"foo\", \"This is the foo table:\");\n\temitter_kv(emitter, \"abc\", \"ABC\", emitter_type_bool, &b_false);\n\temitter_kv(emitter, \"def\", \"DEF\", emitter_type_bool, &b_true);\n\temitter_kv_note(emitter, \"ghi\", \"GHI\", emitter_type_int, &i_123,\n\t    \"note_key1\", emitter_type_string, &str);\n\temitter_kv_note(emitter, \"jkl\", \"JKL\", emitter_type_string, &str,\n\t    \"note_key2\", emitter_type_bool, &b_false);\n\temitter_dict_end(emitter);\n\temitter_end(emitter);\n}\nstatic const char *dict_json =\n\"{\\n\"\n\"\\t\\\"foo\\\": {\\n\"\n\"\\t\\t\\\"abc\\\": false,\\n\"\n\"\\t\\t\\\"def\\\": true,\\n\"\n\"\\t\\t\\\"ghi\\\": 123,\\n\"\n\"\\t\\t\\\"jkl\\\": \\\"a string\\\"\\n\"\n\"\\t}\\n\"\n\"}\\n\";\nstatic const char *dict_table =\n\"This is the foo table:\\n\"\n\"  ABC: false\\n\"\n\"  DEF: true\\n\"\n\"  GHI: 123 (note_key1: \\\"a string\\\")\\n\"\n\"  JKL: \\\"a string\\\" (note_key2: false)\\n\";\n\nTEST_BEGIN(test_dict) {\n\tassert_emit_output(&emit_dict, dict_json, dict_table);\n}\nTEST_END\n\nstatic void\nemit_table_printf(emitter_t *emitter) {\n\temitter_begin(emitter);\n\temitter_table_printf(emitter, \"Table note 1\\n\");\n\temitter_table_printf(emitter, \"Table note 2 %s\\n\",\n\t    \"with format string\");\n\temitter_end(emitter);\n}\n\nstatic const char *table_printf_json =\n\"{\\n\"\n\"}\\n\";\n\nstatic const char *table_printf_table =\n\"Table note 1\\n\"\n\"Table note 2 with format string\\n\";\n\nTEST_BEGIN(test_table_printf) {\n\tassert_emit_output(&emit_table_printf, table_printf_json,\n\t    table_printf_table);\n}\nTEST_END\n\nstatic void emit_nested_dict(emitter_t *emitter) {\n\tint val = 123;\n\temitter_begin(emitter);\n\temitter_dict_begin(emitter, \"json1\", \"Dict 1\");\n\temitter_dict_begin(emitter, \"json2\", \"Dict 2\");\n\temitter_kv(emitter, \"primitive\", \"A primitive\", emitter_type_int, &val);\n\temitter_dict_end(emitter); /* Close 2 */\n\temitter_dict_begin(emitter, \"json3\", \"Dict 3\");\n\temitter_dict_end(emitter); /* Close 3 */\n\temitter_dict_end(emitter); /* Close 1 */\n\temitter_dict_begin(emitter, \"json4\", \"Dict 4\");\n\temitter_kv(emitter, \"primitive\", \"Another primitive\",\n\t    emitter_type_int, &val);\n\temitter_dict_end(emitter); /* Close 4 */\n\temitter_end(emitter);\n}\n\nstatic const char *nested_object_json =\n\"{\\n\"\n\"\\t\\\"json1\\\": {\\n\"\n\"\\t\\t\\\"json2\\\": {\\n\"\n\"\\t\\t\\t\\\"primitive\\\": 123\\n\"\n\"\\t\\t},\\n\"\n\"\\t\\t\\\"json3\\\": {\\n\"\n\"\\t\\t}\\n\"\n\"\\t},\\n\"\n\"\\t\\\"json4\\\": {\\n\"\n\"\\t\\t\\\"primitive\\\": 123\\n\"\n\"\\t}\\n\"\n\"}\\n\";\n\nstatic const char *nested_object_table =\n\"Dict 1\\n\"\n\"  Dict 2\\n\"\n\"    A primitive: 123\\n\"\n\"  Dict 3\\n\"\n\"Dict 4\\n\"\n\"  Another primitive: 123\\n\";\n\nTEST_BEGIN(test_nested_dict) {\n\tassert_emit_output(&emit_nested_dict, nested_object_json,\n\t    nested_object_table);\n}\nTEST_END\n\nstatic void\nemit_types(emitter_t *emitter) {\n\tbool b = false;\n\tint i = -123;\n\tunsigned u = 123;\n\tssize_t zd = -456;\n\tsize_t zu = 456;\n\tconst char *str = \"string\";\n\tuint32_t u32 = 789;\n\tuint64_t u64 = 10000000000ULL;\n\n\temitter_begin(emitter);\n\temitter_kv(emitter, \"k1\", \"K1\", emitter_type_bool, &b);\n\temitter_kv(emitter, \"k2\", \"K2\", emitter_type_int, &i);\n\temitter_kv(emitter, \"k3\", \"K3\", emitter_type_unsigned, &u);\n\temitter_kv(emitter, \"k4\", \"K4\", emitter_type_ssize, &zd);\n\temitter_kv(emitter, \"k5\", \"K5\", emitter_type_size, &zu);\n\temitter_kv(emitter, \"k6\", \"K6\", emitter_type_string, &str);\n\temitter_kv(emitter, \"k7\", \"K7\", emitter_type_uint32, &u32);\n\temitter_kv(emitter, \"k8\", \"K8\", emitter_type_uint64, &u64);\n\t/*\n\t * We don't test the title type, since it's only used for tables.  It's\n\t * tested in the emitter_table_row tests.\n\t */\n\temitter_end(emitter);\n}\n\nstatic const char *types_json =\n\"{\\n\"\n\"\\t\\\"k1\\\": false,\\n\"\n\"\\t\\\"k2\\\": -123,\\n\"\n\"\\t\\\"k3\\\": 123,\\n\"\n\"\\t\\\"k4\\\": -456,\\n\"\n\"\\t\\\"k5\\\": 456,\\n\"\n\"\\t\\\"k6\\\": \\\"string\\\",\\n\"\n\"\\t\\\"k7\\\": 789,\\n\"\n\"\\t\\\"k8\\\": 10000000000\\n\"\n\"}\\n\";\n\nstatic const char *types_table =\n\"K1: false\\n\"\n\"K2: -123\\n\"\n\"K3: 123\\n\"\n\"K4: -456\\n\"\n\"K5: 456\\n\"\n\"K6: \\\"string\\\"\\n\"\n\"K7: 789\\n\"\n\"K8: 10000000000\\n\";\n\nTEST_BEGIN(test_types) {\n\tassert_emit_output(&emit_types, types_json, types_table);\n}\nTEST_END\n\nstatic void\nemit_modal(emitter_t *emitter) {\n\tint val = 123;\n\temitter_begin(emitter);\n\temitter_dict_begin(emitter, \"j0\", \"T0\");\n\temitter_json_key(emitter, \"j1\");\n\temitter_json_object_begin(emitter);\n\temitter_kv(emitter, \"i1\", \"I1\", emitter_type_int, &val);\n\temitter_json_kv(emitter, \"i2\", emitter_type_int, &val);\n\temitter_table_kv(emitter, \"I3\", emitter_type_int, &val);\n\temitter_table_dict_begin(emitter, \"T1\");\n\temitter_kv(emitter, \"i4\", \"I4\", emitter_type_int, &val);\n\temitter_json_object_end(emitter); /* Close j1 */\n\temitter_kv(emitter, \"i5\", \"I5\", emitter_type_int, &val);\n\temitter_table_dict_end(emitter); /* Close T1 */\n\temitter_kv(emitter, \"i6\", \"I6\", emitter_type_int, &val);\n\temitter_dict_end(emitter); /* Close j0 / T0 */\n\temitter_end(emitter);\n}\n\nconst char *modal_json =\n\"{\\n\"\n\"\\t\\\"j0\\\": {\\n\"\n\"\\t\\t\\\"j1\\\": {\\n\"\n\"\\t\\t\\t\\\"i1\\\": 123,\\n\"\n\"\\t\\t\\t\\\"i2\\\": 123,\\n\"\n\"\\t\\t\\t\\\"i4\\\": 123\\n\"\n\"\\t\\t},\\n\"\n\"\\t\\t\\\"i5\\\": 123,\\n\"\n\"\\t\\t\\\"i6\\\": 123\\n\"\n\"\\t}\\n\"\n\"}\\n\";\n\nconst char *modal_table =\n\"T0\\n\"\n\"  I1: 123\\n\"\n\"  I3: 123\\n\"\n\"  T1\\n\"\n\"    I4: 123\\n\"\n\"    I5: 123\\n\"\n\"  I6: 123\\n\";\n\nTEST_BEGIN(test_modal) {\n\tassert_emit_output(&emit_modal, modal_json, modal_table);\n}\nTEST_END\n\nstatic void\nemit_json_arr(emitter_t *emitter) {\n\tint ival = 123;\n\n\temitter_begin(emitter);\n\temitter_json_key(emitter, \"dict\");\n\temitter_json_object_begin(emitter);\n\temitter_json_key(emitter, \"arr\");\n\temitter_json_array_begin(emitter);\n\temitter_json_object_begin(emitter);\n\temitter_json_kv(emitter, \"foo\", emitter_type_int, &ival);\n\temitter_json_object_end(emitter); /* Close arr[0] */\n\t/* arr[1] and arr[2] are primitives. */\n\temitter_json_value(emitter, emitter_type_int, &ival);\n\temitter_json_value(emitter, emitter_type_int, &ival);\n\temitter_json_object_begin(emitter);\n\temitter_json_kv(emitter, \"bar\", emitter_type_int, &ival);\n\temitter_json_kv(emitter, \"baz\", emitter_type_int, &ival);\n\temitter_json_object_end(emitter); /* Close arr[3]. */\n\temitter_json_array_end(emitter); /* Close arr. */\n\temitter_json_object_end(emitter); /* Close dict. */\n\temitter_end(emitter);\n}\n\nstatic const char *json_array_json =\n\"{\\n\"\n\"\\t\\\"dict\\\": {\\n\"\n\"\\t\\t\\\"arr\\\": [\\n\"\n\"\\t\\t\\t{\\n\"\n\"\\t\\t\\t\\t\\\"foo\\\": 123\\n\"\n\"\\t\\t\\t},\\n\"\n\"\\t\\t\\t123,\\n\"\n\"\\t\\t\\t123,\\n\"\n\"\\t\\t\\t{\\n\"\n\"\\t\\t\\t\\t\\\"bar\\\": 123,\\n\"\n\"\\t\\t\\t\\t\\\"baz\\\": 123\\n\"\n\"\\t\\t\\t}\\n\"\n\"\\t\\t]\\n\"\n\"\\t}\\n\"\n\"}\\n\";\n\nstatic const char *json_array_table = \"\";\n\nTEST_BEGIN(test_json_arr) {\n\tassert_emit_output(&emit_json_arr, json_array_json, json_array_table);\n}\nTEST_END\n\nstatic void\nemit_json_nested_array(emitter_t *emitter) {\n\tint ival = 123;\n\tchar *sval = \"foo\";\n\temitter_begin(emitter);\n\temitter_json_array_begin(emitter);\n\t\temitter_json_array_begin(emitter);\n\t\temitter_json_value(emitter, emitter_type_int, &ival);\n\t\temitter_json_value(emitter, emitter_type_string, &sval);\n\t\temitter_json_value(emitter, emitter_type_int, &ival);\n\t\temitter_json_value(emitter, emitter_type_string, &sval);\n\t\temitter_json_array_end(emitter);\n\t\temitter_json_array_begin(emitter);\n\t\temitter_json_value(emitter, emitter_type_int, &ival);\n\t\temitter_json_array_end(emitter);\n\t\temitter_json_array_begin(emitter);\n\t\temitter_json_value(emitter, emitter_type_string, &sval);\n\t\temitter_json_value(emitter, emitter_type_int, &ival);\n\t\temitter_json_array_end(emitter);\n\t\temitter_json_array_begin(emitter);\n\t\temitter_json_array_end(emitter);\n\temitter_json_array_end(emitter);\n\temitter_end(emitter);\n}\n\nstatic const char *json_nested_array_json =\n\"{\\n\"\n\"\\t[\\n\"\n\"\\t\\t[\\n\"\n\"\\t\\t\\t123,\\n\"\n\"\\t\\t\\t\\\"foo\\\",\\n\"\n\"\\t\\t\\t123,\\n\"\n\"\\t\\t\\t\\\"foo\\\"\\n\"\n\"\\t\\t],\\n\"\n\"\\t\\t[\\n\"\n\"\\t\\t\\t123\\n\"\n\"\\t\\t],\\n\"\n\"\\t\\t[\\n\"\n\"\\t\\t\\t\\\"foo\\\",\\n\"\n\"\\t\\t\\t123\\n\"\n\"\\t\\t],\\n\"\n\"\\t\\t[\\n\"\n\"\\t\\t]\\n\"\n\"\\t]\\n\"\n\"}\\n\";\n\nTEST_BEGIN(test_json_nested_arr) {\n\tassert_emit_output(&emit_json_nested_array, json_nested_array_json,\n\t    json_array_table);\n}\nTEST_END\n\nstatic void\nemit_table_row(emitter_t *emitter) {\n\temitter_begin(emitter);\n\temitter_row_t row;\n\temitter_col_t abc = {emitter_justify_left, 10, emitter_type_title, {0}, {0, 0}};\n\tabc.str_val = \"ABC title\";\n\temitter_col_t def = {emitter_justify_right, 15, emitter_type_title, {0}, {0, 0}};\n\tdef.str_val = \"DEF title\";\n\temitter_col_t ghi = {emitter_justify_right, 5, emitter_type_title, {0}, {0, 0}};\n\tghi.str_val = \"GHI\";\n\n\temitter_row_init(&row);\n\temitter_col_init(&abc, &row);\n\temitter_col_init(&def, &row);\n\temitter_col_init(&ghi, &row);\n\n\temitter_table_row(emitter, &row);\n\n\tabc.type = emitter_type_int;\n\tdef.type = emitter_type_bool;\n\tghi.type = emitter_type_int;\n\n\tabc.int_val = 123;\n\tdef.bool_val = true;\n\tghi.int_val = 456;\n\temitter_table_row(emitter, &row);\n\n\tabc.int_val = 789;\n\tdef.bool_val = false;\n\tghi.int_val = 1011;\n\temitter_table_row(emitter, &row);\n\n\tabc.type = emitter_type_string;\n\tabc.str_val = \"a string\";\n\tdef.bool_val = false;\n\tghi.type = emitter_type_title;\n\tghi.str_val = \"ghi\";\n\temitter_table_row(emitter, &row);\n\n\temitter_end(emitter);\n}\n\nstatic const char *table_row_json =\n\"{\\n\"\n\"}\\n\";\n\nstatic const char *table_row_table =\n\"ABC title       DEF title  GHI\\n\"\n\"123                  true  456\\n\"\n\"789                 false 1011\\n\"\n\"\\\"a string\\\"          false  ghi\\n\";\n\nTEST_BEGIN(test_table_row) {\n\tassert_emit_output(&emit_table_row, table_row_json, table_row_table);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test_no_reentrancy(\n\t    test_dict,\n\t    test_table_printf,\n\t    test_nested_dict,\n\t    test_types,\n\t    test_modal,\n\t    test_json_arr,\n\t    test_json_nested_arr,\n\t    test_table_row);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/extent_quantize.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nTEST_BEGIN(test_small_extent_size) {\n\tunsigned nbins, i;\n\tsize_t sz, extent_size;\n\tsize_t mib[4];\n\tsize_t miblen = sizeof(mib) / sizeof(size_t);\n\n\t/*\n\t * Iterate over all small size classes, get their extent sizes, and\n\t * verify that the quantized size is the same as the extent size.\n\t */\n\n\tsz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.nbins\", (void *)&nbins, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl failure\");\n\n\tassert_d_eq(mallctlnametomib(\"arenas.bin.0.slab_size\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib failure\");\n\tfor (i = 0; i < nbins; i++) {\n\t\tmib[2] = i;\n\t\tsz = sizeof(size_t);\n\t\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&extent_size, &sz,\n\t\t    NULL, 0), 0, \"Unexpected mallctlbymib failure\");\n\t\tassert_zu_eq(extent_size,\n\t\t    extent_size_quantize_floor(extent_size),\n\t\t    \"Small extent quantization should be a no-op \"\n\t\t    \"(extent_size=%zu)\", extent_size);\n\t\tassert_zu_eq(extent_size,\n\t\t    extent_size_quantize_ceil(extent_size),\n\t\t    \"Small extent quantization should be a no-op \"\n\t\t    \"(extent_size=%zu)\", extent_size);\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_large_extent_size) {\n\tbool cache_oblivious;\n\tunsigned nlextents, i;\n\tsize_t sz, extent_size_prev, ceil_prev;\n\tsize_t mib[4];\n\tsize_t miblen = sizeof(mib) / sizeof(size_t);\n\n\t/*\n\t * Iterate over all large size classes, get their extent sizes, and\n\t * verify that the quantized size is the same as the extent size.\n\t */\n\n\tsz = sizeof(bool);\n\tassert_d_eq(mallctl(\"config.cache_oblivious\", (void *)&cache_oblivious,\n\t    &sz, NULL, 0), 0, \"Unexpected mallctl failure\");\n\n\tsz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.nlextents\", (void *)&nlextents, &sz, NULL,\n\t    0), 0, \"Unexpected mallctl failure\");\n\n\tassert_d_eq(mallctlnametomib(\"arenas.lextent.0.size\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib failure\");\n\tfor (i = 0; i < nlextents; i++) {\n\t\tsize_t lextent_size, extent_size, floor, ceil;\n\n\t\tmib[2] = i;\n\t\tsz = sizeof(size_t);\n\t\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&lextent_size,\n\t\t    &sz, NULL, 0), 0, \"Unexpected mallctlbymib failure\");\n\t\textent_size = cache_oblivious ? lextent_size + PAGE :\n\t\t    lextent_size;\n\t\tfloor = extent_size_quantize_floor(extent_size);\n\t\tceil = extent_size_quantize_ceil(extent_size);\n\n\t\tassert_zu_eq(extent_size, floor,\n\t\t    \"Extent quantization should be a no-op for precise size \"\n\t\t    \"(lextent_size=%zu, extent_size=%zu)\", lextent_size,\n\t\t    extent_size);\n\t\tassert_zu_eq(extent_size, ceil,\n\t\t    \"Extent quantization should be a no-op for precise size \"\n\t\t    \"(lextent_size=%zu, extent_size=%zu)\", lextent_size,\n\t\t    extent_size);\n\n\t\tif (i > 0) {\n\t\t\tassert_zu_eq(extent_size_prev,\n\t\t\t    extent_size_quantize_floor(extent_size - PAGE),\n\t\t\t    \"Floor should be a precise size\");\n\t\t\tif (extent_size_prev < ceil_prev) {\n\t\t\t\tassert_zu_eq(ceil_prev, extent_size,\n\t\t\t\t    \"Ceiling should be a precise size \"\n\t\t\t\t    \"(extent_size_prev=%zu, ceil_prev=%zu, \"\n\t\t\t\t    \"extent_size=%zu)\", extent_size_prev,\n\t\t\t\t    ceil_prev, extent_size);\n\t\t\t}\n\t\t}\n\t\tif (i + 1 < nlextents) {\n\t\t\textent_size_prev = floor;\n\t\t\tceil_prev = extent_size_quantize_ceil(extent_size +\n\t\t\t    PAGE);\n\t\t}\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_monotonic) {\n#define SZ_MAX\tZU(4 * 1024 * 1024)\n\tunsigned i;\n\tsize_t floor_prev, ceil_prev;\n\n\tfloor_prev = 0;\n\tceil_prev = 0;\n\tfor (i = 1; i <= SZ_MAX >> LG_PAGE; i++) {\n\t\tsize_t extent_size, floor, ceil;\n\n\t\textent_size = i << LG_PAGE;\n\t\tfloor = extent_size_quantize_floor(extent_size);\n\t\tceil = extent_size_quantize_ceil(extent_size);\n\n\t\tassert_zu_le(floor, extent_size,\n\t\t    \"Floor should be <= (floor=%zu, extent_size=%zu, ceil=%zu)\",\n\t\t    floor, extent_size, ceil);\n\t\tassert_zu_ge(ceil, extent_size,\n\t\t    \"Ceiling should be >= (floor=%zu, extent_size=%zu, \"\n\t\t    \"ceil=%zu)\", floor, extent_size, ceil);\n\n\t\tassert_zu_le(floor_prev, floor, \"Floor should be monotonic \"\n\t\t    \"(floor_prev=%zu, floor=%zu, extent_size=%zu, ceil=%zu)\",\n\t\t    floor_prev, floor, extent_size, ceil);\n\t\tassert_zu_le(ceil_prev, ceil, \"Ceiling should be monotonic \"\n\t\t    \"(floor=%zu, extent_size=%zu, ceil_prev=%zu, ceil=%zu)\",\n\t\t    floor, extent_size, ceil_prev, ceil);\n\n\t\tfloor_prev = floor;\n\t\tceil_prev = ceil;\n\t}\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_small_extent_size,\n\t    test_large_extent_size,\n\t    test_monotonic);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/extent_util.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#define TEST_UTIL_EINVAL(node, a, b, c, d, why_inval) do {\t\t\\\n\tassert_d_eq(mallctl(\"experimental.utilization.\" node,\t\t\\\n\t    a, b, c, d), EINVAL, \"Should fail when \" why_inval);\t\\\n\tassert_zu_eq(out_sz, out_sz_ref,\t\t\t\t\\\n\t    \"Output size touched when given invalid arguments\");\t\\\n\tassert_d_eq(memcmp(out, out_ref, out_sz_ref), 0,\t\t\\\n\t    \"Output content touched when given invalid arguments\");\t\\\n} while (0)\n\n#define TEST_UTIL_QUERY_EINVAL(a, b, c, d, why_inval)\t\t\t\\\n\tTEST_UTIL_EINVAL(\"query\", a, b, c, d, why_inval)\n#define TEST_UTIL_BATCH_EINVAL(a, b, c, d, why_inval)\t\t\t\\\n\tTEST_UTIL_EINVAL(\"batch_query\", a, b, c, d, why_inval)\n\n#define TEST_UTIL_VALID(node) do {\t\t\t\t\t\\\n        assert_d_eq(mallctl(\"experimental.utilization.\" node,\t\t\\\n\t    out, &out_sz, in, in_sz), 0,\t\t\t\t\\\n\t    \"Should return 0 on correct arguments\");\t\t\t\\\n        assert_zu_eq(out_sz, out_sz_ref, \"incorrect output size\");\t\\\n\tassert_d_ne(memcmp(out, out_ref, out_sz_ref), 0,\t\t\\\n\t    \"Output content should be changed\");\t\t\t\\\n} while (0)\n\n#define TEST_UTIL_BATCH_VALID TEST_UTIL_VALID(\"batch_query\")\n\n#define TEST_MAX_SIZE (1 << 20)\n\nTEST_BEGIN(test_query) {\n\tsize_t sz;\n\t/*\n\t * Select some sizes that can span both small and large sizes, and are\n\t * numerically unrelated to any size boundaries.\n\t */\n\tfor (sz = 7; sz <= TEST_MAX_SIZE && sz <= SC_LARGE_MAXCLASS;\n\t    sz += (sz <= SC_SMALL_MAXCLASS ? 1009 : 99989)) {\n\t\tvoid *p = mallocx(sz, 0);\n\t\tvoid **in = &p;\n\t\tsize_t in_sz = sizeof(const void *);\n\t\tsize_t out_sz = sizeof(void *) + sizeof(size_t) * 5;\n\t\tvoid *out = mallocx(out_sz, 0);\n\t\tvoid *out_ref = mallocx(out_sz, 0);\n\t\tsize_t out_sz_ref = out_sz;\n\n\t\tassert_ptr_not_null(p,\n\t\t    \"test pointer allocation failed\");\n\t\tassert_ptr_not_null(out,\n\t\t    \"test output allocation failed\");\n\t\tassert_ptr_not_null(out_ref,\n\t\t    \"test reference output allocation failed\");\n\n#define SLABCUR_READ(out) (*(void **)out)\n#define COUNTS(out) ((size_t *)((void **)out + 1))\n#define NFREE_READ(out) COUNTS(out)[0]\n#define NREGS_READ(out) COUNTS(out)[1]\n#define SIZE_READ(out) COUNTS(out)[2]\n#define BIN_NFREE_READ(out) COUNTS(out)[3]\n#define BIN_NREGS_READ(out) COUNTS(out)[4]\n\n\t\tSLABCUR_READ(out) = NULL;\n\t\tNFREE_READ(out) = NREGS_READ(out) = SIZE_READ(out) = -1;\n\t\tBIN_NFREE_READ(out) = BIN_NREGS_READ(out) = -1;\n\t\tmemcpy(out_ref, out, out_sz);\n\n\t\t/* Test invalid argument(s) errors */\n\t\tTEST_UTIL_QUERY_EINVAL(NULL, &out_sz, in, in_sz,\n\t\t    \"old is NULL\");\n\t\tTEST_UTIL_QUERY_EINVAL(out, NULL, in, in_sz,\n\t\t    \"oldlenp is NULL\");\n\t\tTEST_UTIL_QUERY_EINVAL(out, &out_sz, NULL, in_sz,\n\t\t    \"newp is NULL\");\n\t\tTEST_UTIL_QUERY_EINVAL(out, &out_sz, in, 0,\n\t\t    \"newlen is zero\");\n\t\tin_sz -= 1;\n\t\tTEST_UTIL_QUERY_EINVAL(out, &out_sz, in, in_sz,\n\t\t    \"invalid newlen\");\n\t\tin_sz += 1;\n\t\tout_sz_ref = out_sz -= 2 * sizeof(size_t);\n\t\tTEST_UTIL_QUERY_EINVAL(out, &out_sz, in, in_sz,\n\t\t    \"invalid *oldlenp\");\n\t\tout_sz_ref = out_sz += 2 * sizeof(size_t);\n\n\t\t/* Examine output for valid call */\n\t\tTEST_UTIL_VALID(\"query\");\n\t\tassert_zu_le(sz, SIZE_READ(out),\n\t\t    \"Extent size should be at least allocation size\");\n\t\tassert_zu_eq(SIZE_READ(out) & (PAGE - 1), 0,\n\t\t    \"Extent size should be a multiple of page size\");\n\t\tif (sz <= SC_SMALL_MAXCLASS) {\n\t\t\tassert_zu_le(NFREE_READ(out), NREGS_READ(out),\n\t\t\t    \"Extent free count exceeded region count\");\n\t\t\tassert_zu_le(NREGS_READ(out), SIZE_READ(out),\n\t\t\t    \"Extent region count exceeded size\");\n\t\t\tassert_zu_ne(NREGS_READ(out), 0,\n\t\t\t    \"Extent region count must be positive\");\n\t\t\tassert_ptr_not_null(SLABCUR_READ(out),\n\t\t\t    \"Current slab is null\");\n\t\t\tassert_true(NFREE_READ(out) == 0\n\t\t\t    || SLABCUR_READ(out) <= p,\n\t\t\t    \"Allocation should follow first fit principle\");\n\t\t\tif (config_stats) {\n\t\t\t\tassert_zu_le(BIN_NFREE_READ(out),\n\t\t\t\t    BIN_NREGS_READ(out),\n\t\t\t\t    \"Bin free count exceeded region count\");\n\t\t\t\tassert_zu_ne(BIN_NREGS_READ(out), 0,\n\t\t\t\t    \"Bin region count must be positive\");\n\t\t\t\tassert_zu_le(NFREE_READ(out),\n\t\t\t\t    BIN_NFREE_READ(out),\n\t\t\t\t    \"Extent free count exceeded bin free count\");\n\t\t\t\tassert_zu_le(NREGS_READ(out),\n\t\t\t\t    BIN_NREGS_READ(out),\n\t\t\t\t    \"Extent region count exceeded \"\n\t\t\t\t    \"bin region count\");\n\t\t\t\tassert_zu_eq(BIN_NREGS_READ(out)\n\t\t\t\t    % NREGS_READ(out), 0,\n\t\t\t\t    \"Bin region count isn't a multiple of \"\n\t\t\t\t    \"extent region count\");\n\t\t\t\tassert_zu_le(\n\t\t\t\t    BIN_NFREE_READ(out) - NFREE_READ(out),\n\t\t\t\t    BIN_NREGS_READ(out) - NREGS_READ(out),\n\t\t\t\t    \"Free count in other extents in the bin \"\n\t\t\t\t    \"exceeded region count in other extents \"\n\t\t\t\t    \"in the bin\");\n\t\t\t\tassert_zu_le(NREGS_READ(out) - NFREE_READ(out),\n\t\t\t\t    BIN_NREGS_READ(out) - BIN_NFREE_READ(out),\n\t\t\t\t    \"Extent utilized count exceeded \"\n\t\t\t\t    \"bin utilized count\");\n\t\t\t}\n\t\t} else {\n\t\t\tassert_zu_eq(NFREE_READ(out), 0,\n\t\t\t    \"Extent free count should be zero\");\n\t\t\tassert_zu_eq(NREGS_READ(out), 1,\n\t\t\t    \"Extent region count should be one\");\n\t\t\tassert_ptr_null(SLABCUR_READ(out),\n\t\t\t    \"Current slab must be null for large size classes\");\n\t\t\tif (config_stats) {\n\t\t\t\tassert_zu_eq(BIN_NFREE_READ(out), 0,\n\t\t\t\t    \"Bin free count must be zero for \"\n\t\t\t\t    \"large sizes\");\n\t\t\t\tassert_zu_eq(BIN_NREGS_READ(out), 0,\n\t\t\t\t    \"Bin region count must be zero for \"\n\t\t\t\t    \"large sizes\");\n\t\t\t}\n\t\t}\n\n#undef BIN_NREGS_READ\n#undef BIN_NFREE_READ\n#undef SIZE_READ\n#undef NREGS_READ\n#undef NFREE_READ\n#undef COUNTS\n#undef SLABCUR_READ\n\n\t\tfree(out_ref);\n\t\tfree(out);\n\t\tfree(p);\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_batch) {\n\tsize_t sz;\n\t/*\n\t * Select some sizes that can span both small and large sizes, and are\n\t * numerically unrelated to any size boundaries.\n\t */\n\tfor (sz = 17; sz <= TEST_MAX_SIZE && sz <= SC_LARGE_MAXCLASS;\n\t    sz += (sz <= SC_SMALL_MAXCLASS ? 1019 : 99991)) {\n\t\tvoid *p = mallocx(sz, 0);\n\t\tvoid *q = mallocx(sz, 0);\n\t\tvoid *in[] = {p, q};\n\t\tsize_t in_sz = sizeof(const void *) * 2;\n\t\tsize_t out[] = {-1, -1, -1, -1, -1, -1};\n\t\tsize_t out_sz = sizeof(size_t) * 6;\n\t\tsize_t out_ref[] = {-1, -1, -1, -1, -1, -1};\n\t\tsize_t out_sz_ref = out_sz;\n\n\t\tassert_ptr_not_null(p, \"test pointer allocation failed\");\n\t\tassert_ptr_not_null(q, \"test pointer allocation failed\");\n\n\t\t/* Test invalid argument(s) errors */\n\t\tTEST_UTIL_BATCH_EINVAL(NULL, &out_sz, in, in_sz,\n\t\t    \"old is NULL\");\n\t\tTEST_UTIL_BATCH_EINVAL(out, NULL, in, in_sz,\n\t\t    \"oldlenp is NULL\");\n\t\tTEST_UTIL_BATCH_EINVAL(out, &out_sz, NULL, in_sz,\n\t\t    \"newp is NULL\");\n\t\tTEST_UTIL_BATCH_EINVAL(out, &out_sz, in, 0,\n\t\t    \"newlen is zero\");\n\t\tin_sz -= 1;\n\t\tTEST_UTIL_BATCH_EINVAL(out, &out_sz, in, in_sz,\n\t\t    \"newlen is not an exact multiple\");\n\t\tin_sz += 1;\n\t\tout_sz_ref = out_sz -= 2 * sizeof(size_t);\n\t\tTEST_UTIL_BATCH_EINVAL(out, &out_sz, in, in_sz,\n\t\t    \"*oldlenp is not an exact multiple\");\n\t\tout_sz_ref = out_sz += 2 * sizeof(size_t);\n\t\tin_sz -= sizeof(const void *);\n\t\tTEST_UTIL_BATCH_EINVAL(out, &out_sz, in, in_sz,\n\t\t    \"*oldlenp and newlen do not match\");\n\t\tin_sz += sizeof(const void *);\n\n\t/* Examine output for valid calls */\n#define TEST_EQUAL_REF(i, message) \\\n\tassert_d_eq(memcmp(out + (i) * 3, out_ref + (i) * 3, 3), 0, message)\n\n#define NFREE_READ(out, i) out[(i) * 3]\n#define NREGS_READ(out, i) out[(i) * 3 + 1]\n#define SIZE_READ(out, i) out[(i) * 3 + 2]\n\n\t\tout_sz_ref = out_sz /= 2;\n\t\tin_sz /= 2;\n\t\tTEST_UTIL_BATCH_VALID;\n\t\tassert_zu_le(sz, SIZE_READ(out, 0),\n\t\t    \"Extent size should be at least allocation size\");\n\t\tassert_zu_eq(SIZE_READ(out, 0) & (PAGE - 1), 0,\n\t\t    \"Extent size should be a multiple of page size\");\n\t\tif (sz <= SC_SMALL_MAXCLASS) {\n\t\t\tassert_zu_le(NFREE_READ(out, 0), NREGS_READ(out, 0),\n\t\t\t    \"Extent free count exceeded region count\");\n\t\t\tassert_zu_le(NREGS_READ(out, 0), SIZE_READ(out, 0),\n\t\t\t    \"Extent region count exceeded size\");\n\t\t\tassert_zu_ne(NREGS_READ(out, 0), 0,\n\t\t\t    \"Extent region count must be positive\");\n\t\t} else {\n\t\t\tassert_zu_eq(NFREE_READ(out, 0), 0,\n\t\t\t    \"Extent free count should be zero\");\n\t\t\tassert_zu_eq(NREGS_READ(out, 0), 1,\n\t\t\t    \"Extent region count should be one\");\n\t\t}\n\t\tTEST_EQUAL_REF(1,\n\t\t    \"Should not overwrite content beyond what's needed\");\n\t\tin_sz *= 2;\n\t\tout_sz_ref = out_sz *= 2;\n\n\t\tmemcpy(out_ref, out, 3 * sizeof(size_t));\n\t\tTEST_UTIL_BATCH_VALID;\n\t\tTEST_EQUAL_REF(0, \"Statistics should be stable across calls\");\n\t\tif (sz <= SC_SMALL_MAXCLASS) {\n\t\t\tassert_zu_le(NFREE_READ(out, 1), NREGS_READ(out, 1),\n\t\t\t    \"Extent free count exceeded region count\");\n\t\t} else {\n\t\t\tassert_zu_eq(NFREE_READ(out, 0), 0,\n\t\t\t    \"Extent free count should be zero\");\n\t\t}\n\t\tassert_zu_eq(NREGS_READ(out, 0), NREGS_READ(out, 1),\n\t\t    \"Extent region count should be same for same region size\");\n\t\tassert_zu_eq(SIZE_READ(out, 0), SIZE_READ(out, 1),\n\t\t    \"Extent size should be same for same region size\");\n\n#undef SIZE_READ\n#undef NREGS_READ\n#undef NFREE_READ\n\n#undef TEST_EQUAL_REF\n\n\t\tfree(q);\n\t\tfree(p);\n\t}\n}\nTEST_END\n\nint\nmain(void) {\n\tassert_zu_lt(SC_SMALL_MAXCLASS, TEST_MAX_SIZE,\n\t    \"Test case cannot cover large classes\");\n\treturn test(test_query, test_batch);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/fork.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#ifndef _WIN32\n#include <sys/wait.h>\n#endif\n\n#ifndef _WIN32\nstatic void\nwait_for_child_exit(int pid) {\n\tint status;\n\twhile (true) {\n\t\tif (waitpid(pid, &status, 0) == -1) {\n\t\t\ttest_fail(\"Unexpected waitpid() failure.\");\n\t\t}\n\t\tif (WIFSIGNALED(status)) {\n\t\t\ttest_fail(\"Unexpected child termination due to \"\n\t\t\t    \"signal %d\", WTERMSIG(status));\n\t\t\tbreak;\n\t\t}\n\t\tif (WIFEXITED(status)) {\n\t\t\tif (WEXITSTATUS(status) != 0) {\n\t\t\t\ttest_fail(\"Unexpected child exit value %d\",\n\t\t\t\t    WEXITSTATUS(status));\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t}\n}\n#endif\n\nTEST_BEGIN(test_fork) {\n#ifndef _WIN32\n\tvoid *p;\n\tpid_t pid;\n\n\t/* Set up a manually managed arena for test. */\n\tunsigned arena_ind;\n\tsize_t sz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena_ind, &sz, NULL, 0),\n\t    0, \"Unexpected mallctl() failure\");\n\n\t/* Migrate to the new arena. */\n\tunsigned old_arena_ind;\n\tsz = sizeof(old_arena_ind);\n\tassert_d_eq(mallctl(\"thread.arena\", (void *)&old_arena_ind, &sz,\n\t    (void *)&arena_ind, sizeof(arena_ind)), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tp = malloc(1);\n\tassert_ptr_not_null(p, \"Unexpected malloc() failure\");\n\n\tpid = fork();\n\n\tfree(p);\n\n\tp = malloc(64);\n\tassert_ptr_not_null(p, \"Unexpected malloc() failure\");\n\tfree(p);\n\n\tif (pid == -1) {\n\t\t/* Error. */\n\t\ttest_fail(\"Unexpected fork() failure\");\n\t} else if (pid == 0) {\n\t\t/* Child. */\n\t\t_exit(0);\n\t} else {\n\t\twait_for_child_exit(pid);\n\t}\n#else\n\ttest_skip(\"fork(2) is irrelevant to Windows\");\n#endif\n}\nTEST_END\n\n#ifndef _WIN32\nstatic void *\ndo_fork_thd(void *arg) {\n\tmalloc(1);\n\tint pid = fork();\n\tif (pid == -1) {\n\t\t/* Error. */\n\t\ttest_fail(\"Unexpected fork() failure\");\n\t} else if (pid == 0) {\n\t\t/* Child. */\n\t\tchar *args[] = {\"true\", NULL};\n\t\texecvp(args[0], args);\n\t\ttest_fail(\"Exec failed\");\n\t} else {\n\t\t/* Parent */\n\t\twait_for_child_exit(pid);\n\t}\n\treturn NULL;\n}\n#endif\n\n#ifndef _WIN32\nstatic void\ndo_test_fork_multithreaded() {\n\tthd_t child;\n\tthd_create(&child, do_fork_thd, NULL);\n\tdo_fork_thd(NULL);\n\tthd_join(child, NULL);\n}\n#endif\n\nTEST_BEGIN(test_fork_multithreaded) {\n#ifndef _WIN32\n\t/*\n\t * We've seen bugs involving hanging on arenas_lock (though the same\n\t * class of bugs can happen on any mutex).  The bugs are intermittent\n\t * though, so we want to run the test multiple times.  Since we hold the\n\t * arenas lock only early in the process lifetime, we can't just run\n\t * this test in a loop (since, after all the arenas are initialized, we\n\t * won't acquire arenas_lock any further).  We therefore repeat the test\n\t * with multiple processes.\n\t */\n\tfor (int i = 0; i < 100; i++) {\n\t\tint pid = fork();\n\t\tif (pid == -1) {\n\t\t\t/* Error. */\n\t\t\ttest_fail(\"Unexpected fork() failure,\");\n\t\t} else if (pid == 0) {\n\t\t\t/* Child. */\n\t\t\tdo_test_fork_multithreaded();\n\t\t\t_exit(0);\n\t\t} else {\n\t\t\twait_for_child_exit(pid);\n\t\t}\n\t}\n#else\n\ttest_skip(\"fork(2) is irrelevant to Windows\");\n#endif\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test_no_reentrancy(\n\t    test_fork,\n\t    test_fork_multithreaded);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/hash.c",
    "content": "/*\n * This file is based on code that is part of SMHasher\n * (https://code.google.com/p/smhasher/), and is subject to the MIT license\n * (http://www.opensource.org/licenses/mit-license.php).  Both email addresses\n * associated with the source code's revision history belong to Austin Appleby,\n * and the revision history ranges from 2010 to 2012.  Therefore the copyright\n * and license are here taken to be:\n *\n * Copyright (c) 2010-2012 Austin Appleby\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in\n * all copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n * THE SOFTWARE.\n */\n\n#include \"test/jemalloc_test.h\"\n#include \"jemalloc/internal/hash.h\"\n\ntypedef enum {\n\thash_variant_x86_32,\n\thash_variant_x86_128,\n\thash_variant_x64_128\n} hash_variant_t;\n\nstatic int\nhash_variant_bits(hash_variant_t variant) {\n\tswitch (variant) {\n\tcase hash_variant_x86_32: return 32;\n\tcase hash_variant_x86_128: return 128;\n\tcase hash_variant_x64_128: return 128;\n\tdefault: not_reached();\n\t}\n}\n\nstatic const char *\nhash_variant_string(hash_variant_t variant) {\n\tswitch (variant) {\n\tcase hash_variant_x86_32: return \"hash_x86_32\";\n\tcase hash_variant_x86_128: return \"hash_x86_128\";\n\tcase hash_variant_x64_128: return \"hash_x64_128\";\n\tdefault: not_reached();\n\t}\n}\n\n#define KEY_SIZE\t256\nstatic void\nhash_variant_verify_key(hash_variant_t variant, uint8_t *key) {\n\tconst int hashbytes = hash_variant_bits(variant) / 8;\n\tconst int hashes_size = hashbytes * 256;\n\tVARIABLE_ARRAY(uint8_t, hashes, hashes_size);\n\tVARIABLE_ARRAY(uint8_t, final, hashbytes);\n\tunsigned i;\n\tuint32_t computed, expected;\n\n\tmemset(key, 0, KEY_SIZE);\n\tmemset(hashes, 0, hashes_size);\n\tmemset(final, 0, hashbytes);\n\n\t/*\n\t * Hash keys of the form {0}, {0,1}, {0,1,2}, ..., {0,1,...,255} as the\n\t * seed.\n\t */\n\tfor (i = 0; i < 256; i++) {\n\t\tkey[i] = (uint8_t)i;\n\t\tswitch (variant) {\n\t\tcase hash_variant_x86_32: {\n\t\t\tuint32_t out;\n\t\t\tout = hash_x86_32(key, i, 256-i);\n\t\t\tmemcpy(&hashes[i*hashbytes], &out, hashbytes);\n\t\t\tbreak;\n\t\t} case hash_variant_x86_128: {\n\t\t\tuint64_t out[2];\n\t\t\thash_x86_128(key, i, 256-i, out);\n\t\t\tmemcpy(&hashes[i*hashbytes], out, hashbytes);\n\t\t\tbreak;\n\t\t} case hash_variant_x64_128: {\n\t\t\tuint64_t out[2];\n\t\t\thash_x64_128(key, i, 256-i, out);\n\t\t\tmemcpy(&hashes[i*hashbytes], out, hashbytes);\n\t\t\tbreak;\n\t\t} default: not_reached();\n\t\t}\n\t}\n\n\t/* Hash the result array. */\n\tswitch (variant) {\n\tcase hash_variant_x86_32: {\n\t\tuint32_t out = hash_x86_32(hashes, hashes_size, 0);\n\t\tmemcpy(final, &out, sizeof(out));\n\t\tbreak;\n\t} case hash_variant_x86_128: {\n\t\tuint64_t out[2];\n\t\thash_x86_128(hashes, hashes_size, 0, out);\n\t\tmemcpy(final, out, sizeof(out));\n\t\tbreak;\n\t} case hash_variant_x64_128: {\n\t\tuint64_t out[2];\n\t\thash_x64_128(hashes, hashes_size, 0, out);\n\t\tmemcpy(final, out, sizeof(out));\n\t\tbreak;\n\t} default: not_reached();\n\t}\n\n\tcomputed = (final[0] << 0) | (final[1] << 8) | (final[2] << 16) |\n\t    (final[3] << 24);\n\n\tswitch (variant) {\n#ifdef JEMALLOC_BIG_ENDIAN\n\tcase hash_variant_x86_32: expected = 0x6213303eU; break;\n\tcase hash_variant_x86_128: expected = 0x266820caU; break;\n\tcase hash_variant_x64_128: expected = 0xcc622b6fU; break;\n#else\n\tcase hash_variant_x86_32: expected = 0xb0f57ee3U; break;\n\tcase hash_variant_x86_128: expected = 0xb3ece62aU; break;\n\tcase hash_variant_x64_128: expected = 0x6384ba69U; break;\n#endif\n\tdefault: not_reached();\n\t}\n\n\tassert_u32_eq(computed, expected,\n\t    \"Hash mismatch for %s(): expected %#x but got %#x\",\n\t    hash_variant_string(variant), expected, computed);\n}\n\nstatic void\nhash_variant_verify(hash_variant_t variant) {\n#define MAX_ALIGN\t16\n\tuint8_t key[KEY_SIZE + (MAX_ALIGN - 1)];\n\tunsigned i;\n\n\tfor (i = 0; i < MAX_ALIGN; i++) {\n\t\thash_variant_verify_key(variant, &key[i]);\n\t}\n#undef MAX_ALIGN\n}\n#undef KEY_SIZE\n\nTEST_BEGIN(test_hash_x86_32) {\n\thash_variant_verify(hash_variant_x86_32);\n}\nTEST_END\n\nTEST_BEGIN(test_hash_x86_128) {\n\thash_variant_verify(hash_variant_x86_128);\n}\nTEST_END\n\nTEST_BEGIN(test_hash_x64_128) {\n\thash_variant_verify(hash_variant_x64_128);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_hash_x86_32,\n\t    test_hash_x86_128,\n\t    test_hash_x64_128);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/hook.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/hook.h\"\n\nstatic void *arg_extra;\nstatic int arg_type;\nstatic void *arg_result;\nstatic void *arg_address;\nstatic size_t arg_old_usize;\nstatic size_t arg_new_usize;\nstatic uintptr_t arg_result_raw;\nstatic uintptr_t arg_args_raw[4];\n\nstatic int call_count = 0;\n\nstatic void\nreset_args() {\n\targ_extra = NULL;\n\targ_type = 12345;\n\targ_result = NULL;\n\targ_address = NULL;\n\targ_old_usize = 0;\n\targ_new_usize = 0;\n\targ_result_raw = 0;\n\tmemset(arg_args_raw, 77, sizeof(arg_args_raw));\n}\n\nstatic void\nalloc_free_size(size_t sz) {\n\tvoid *ptr = mallocx(1, 0);\n\tfree(ptr);\n\tptr = mallocx(1, 0);\n\tfree(ptr);\n\tptr = mallocx(1, MALLOCX_TCACHE_NONE);\n\tdallocx(ptr, MALLOCX_TCACHE_NONE);\n}\n\n/*\n * We want to support a degree of user reentrancy.  This tests a variety of\n * allocation scenarios.\n */\nstatic void\nbe_reentrant() {\n\t/* Let's make sure the tcache is non-empty if enabled. */\n\talloc_free_size(1);\n\talloc_free_size(1024);\n\talloc_free_size(64 * 1024);\n\talloc_free_size(256 * 1024);\n\talloc_free_size(1024 * 1024);\n\n\t/* Some reallocation. */\n\tvoid *ptr = mallocx(129, 0);\n\tptr = rallocx(ptr, 130, 0);\n\tfree(ptr);\n\n\tptr = mallocx(2 * 1024 * 1024, 0);\n\tfree(ptr);\n\tptr = mallocx(1 * 1024 * 1024, 0);\n\tptr = rallocx(ptr, 2 * 1024 * 1024, 0);\n\tfree(ptr);\n\n\tptr = mallocx(1, 0);\n\tptr = rallocx(ptr, 1000, 0);\n\tfree(ptr);\n}\n\nstatic void\nset_args_raw(uintptr_t *args_raw, int nargs) {\n\tmemcpy(arg_args_raw, args_raw, sizeof(uintptr_t) * nargs);\n}\n\nstatic void\nassert_args_raw(uintptr_t *args_raw_expected, int nargs) {\n\tint cmp = memcmp(args_raw_expected, arg_args_raw,\n\t    sizeof(uintptr_t) * nargs);\n\tassert_d_eq(cmp, 0, \"Raw args mismatch\");\n}\n\nstatic void\nreset() {\n\tcall_count = 0;\n\treset_args();\n}\n\nstatic void\ntest_alloc_hook(void *extra, hook_alloc_t type, void *result,\n    uintptr_t result_raw, uintptr_t args_raw[3]) {\n\tcall_count++;\n\targ_extra = extra;\n\targ_type = (int)type;\n\targ_result = result;\n\targ_result_raw = result_raw;\n\tset_args_raw(args_raw, 3);\n\tbe_reentrant();\n}\n\nstatic void\ntest_dalloc_hook(void *extra, hook_dalloc_t type, void *address,\n    uintptr_t args_raw[3]) {\n\tcall_count++;\n\targ_extra = extra;\n\targ_type = (int)type;\n\targ_address = address;\n\tset_args_raw(args_raw, 3);\n\tbe_reentrant();\n}\n\nstatic void\ntest_expand_hook(void *extra, hook_expand_t type, void *address,\n    size_t old_usize, size_t new_usize, uintptr_t result_raw,\n    uintptr_t args_raw[4]) {\n\tcall_count++;\n\targ_extra = extra;\n\targ_type = (int)type;\n\targ_address = address;\n\targ_old_usize = old_usize;\n\targ_new_usize = new_usize;\n\targ_result_raw = result_raw;\n\tset_args_raw(args_raw, 4);\n\tbe_reentrant();\n}\n\nTEST_BEGIN(test_hooks_basic) {\n\t/* Just verify that the record their arguments correctly. */\n\thooks_t hooks = {\n\t\t&test_alloc_hook, &test_dalloc_hook, &test_expand_hook,\n\t\t(void *)111};\n\tvoid *handle = hook_install(TSDN_NULL, &hooks);\n\tuintptr_t args_raw[4] = {10, 20, 30, 40};\n\n\t/* Alloc */\n\treset_args();\n\thook_invoke_alloc(hook_alloc_posix_memalign, (void *)222, 333,\n\t    args_raw);\n\tassert_ptr_eq(arg_extra, (void *)111, \"Passed wrong user pointer\");\n\tassert_d_eq((int)hook_alloc_posix_memalign, arg_type,\n\t    \"Passed wrong alloc type\");\n\tassert_ptr_eq((void *)222, arg_result, \"Passed wrong result address\");\n\tassert_u64_eq(333, arg_result_raw, \"Passed wrong result\");\n\tassert_args_raw(args_raw, 3);\n\n\t/* Dalloc */\n\treset_args();\n\thook_invoke_dalloc(hook_dalloc_sdallocx, (void *)222, args_raw);\n\tassert_d_eq((int)hook_dalloc_sdallocx, arg_type,\n\t    \"Passed wrong dalloc type\");\n\tassert_ptr_eq((void *)111, arg_extra, \"Passed wrong user pointer\");\n\tassert_ptr_eq((void *)222, arg_address, \"Passed wrong address\");\n\tassert_args_raw(args_raw, 3);\n\n\t/* Expand */\n\treset_args();\n\thook_invoke_expand(hook_expand_xallocx, (void *)222, 333, 444, 555,\n\t    args_raw);\n\tassert_d_eq((int)hook_expand_xallocx, arg_type,\n\t    \"Passed wrong expand type\");\n\tassert_ptr_eq((void *)111, arg_extra, \"Passed wrong user pointer\");\n\tassert_ptr_eq((void *)222, arg_address, \"Passed wrong address\");\n\tassert_zu_eq(333, arg_old_usize, \"Passed wrong old usize\");\n\tassert_zu_eq(444, arg_new_usize, \"Passed wrong new usize\");\n\tassert_zu_eq(555, arg_result_raw, \"Passed wrong result\");\n\tassert_args_raw(args_raw, 4);\n\n\thook_remove(TSDN_NULL, handle);\n}\nTEST_END\n\nTEST_BEGIN(test_hooks_null) {\n\t/* Null hooks should be ignored, not crash. */\n\thooks_t hooks1 = {NULL, NULL, NULL, NULL};\n\thooks_t hooks2 = {&test_alloc_hook, NULL, NULL, NULL};\n\thooks_t hooks3 = {NULL, &test_dalloc_hook, NULL, NULL};\n\thooks_t hooks4 = {NULL, NULL, &test_expand_hook, NULL};\n\n\tvoid *handle1 = hook_install(TSDN_NULL, &hooks1);\n\tvoid *handle2 = hook_install(TSDN_NULL, &hooks2);\n\tvoid *handle3 = hook_install(TSDN_NULL, &hooks3);\n\tvoid *handle4 = hook_install(TSDN_NULL, &hooks4);\n\n\tassert_ptr_ne(handle1, NULL, \"Hook installation failed\");\n\tassert_ptr_ne(handle2, NULL, \"Hook installation failed\");\n\tassert_ptr_ne(handle3, NULL, \"Hook installation failed\");\n\tassert_ptr_ne(handle4, NULL, \"Hook installation failed\");\n\n\tuintptr_t args_raw[4] = {10, 20, 30, 40};\n\n\tcall_count = 0;\n\thook_invoke_alloc(hook_alloc_malloc, NULL, 0, args_raw);\n\tassert_d_eq(call_count, 1, \"Called wrong number of times\");\n\n\tcall_count = 0;\n\thook_invoke_dalloc(hook_dalloc_free, NULL, args_raw);\n\tassert_d_eq(call_count, 1, \"Called wrong number of times\");\n\n\tcall_count = 0;\n\thook_invoke_expand(hook_expand_realloc, NULL, 0, 0, 0, args_raw);\n\tassert_d_eq(call_count, 1, \"Called wrong number of times\");\n\n\thook_remove(TSDN_NULL, handle1);\n\thook_remove(TSDN_NULL, handle2);\n\thook_remove(TSDN_NULL, handle3);\n\thook_remove(TSDN_NULL, handle4);\n}\nTEST_END\n\nTEST_BEGIN(test_hooks_remove) {\n\thooks_t hooks = {&test_alloc_hook, NULL, NULL, NULL};\n\tvoid *handle = hook_install(TSDN_NULL, &hooks);\n\tassert_ptr_ne(handle, NULL, \"Hook installation failed\");\n\tcall_count = 0;\n\tuintptr_t args_raw[4] = {10, 20, 30, 40};\n\thook_invoke_alloc(hook_alloc_malloc, NULL, 0, args_raw);\n\tassert_d_eq(call_count, 1, \"Hook not invoked\");\n\n\tcall_count = 0;\n\thook_remove(TSDN_NULL, handle);\n\thook_invoke_alloc(hook_alloc_malloc, NULL, 0, NULL);\n\tassert_d_eq(call_count, 0, \"Hook invoked after removal\");\n\n}\nTEST_END\n\nTEST_BEGIN(test_hooks_alloc_simple) {\n\t/* \"Simple\" in the sense that we're not in a realloc variant. */\n\thooks_t hooks = {&test_alloc_hook, NULL, NULL, (void *)123};\n\tvoid *handle = hook_install(TSDN_NULL, &hooks);\n\tassert_ptr_ne(handle, NULL, \"Hook installation failed\");\n\n\t/* Stop malloc from being optimized away. */\n\tvolatile int err;\n\tvoid *volatile ptr;\n\n\t/* malloc */\n\treset();\n\tptr = malloc(1);\n\tassert_d_eq(call_count, 1, \"Hook not called\");\n\tassert_ptr_eq(arg_extra, (void *)123, \"Wrong extra\");\n\tassert_d_eq(arg_type, (int)hook_alloc_malloc, \"Wrong hook type\");\n\tassert_ptr_eq(ptr, arg_result, \"Wrong result\");\n\tassert_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,\n\t    \"Wrong raw result\");\n\tassert_u64_eq((uintptr_t)1, arg_args_raw[0], \"Wrong argument\");\n\tfree(ptr);\n\n\t/* posix_memalign */\n\treset();\n\terr = posix_memalign((void **)&ptr, 1024, 1);\n\tassert_d_eq(call_count, 1, \"Hook not called\");\n\tassert_ptr_eq(arg_extra, (void *)123, \"Wrong extra\");\n\tassert_d_eq(arg_type, (int)hook_alloc_posix_memalign,\n\t    \"Wrong hook type\");\n\tassert_ptr_eq(ptr, arg_result, \"Wrong result\");\n\tassert_u64_eq((uintptr_t)err, (uintptr_t)arg_result_raw,\n\t    \"Wrong raw result\");\n\tassert_u64_eq((uintptr_t)&ptr, arg_args_raw[0], \"Wrong argument\");\n\tassert_u64_eq((uintptr_t)1024, arg_args_raw[1], \"Wrong argument\");\n\tassert_u64_eq((uintptr_t)1, arg_args_raw[2], \"Wrong argument\");\n\tfree(ptr);\n\n\t/* aligned_alloc */\n\treset();\n\tptr = aligned_alloc(1024, 1);\n\tassert_d_eq(call_count, 1, \"Hook not called\");\n\tassert_ptr_eq(arg_extra, (void *)123, \"Wrong extra\");\n\tassert_d_eq(arg_type, (int)hook_alloc_aligned_alloc,\n\t    \"Wrong hook type\");\n\tassert_ptr_eq(ptr, arg_result, \"Wrong result\");\n\tassert_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,\n\t    \"Wrong raw result\");\n\tassert_u64_eq((uintptr_t)1024, arg_args_raw[0], \"Wrong argument\");\n\tassert_u64_eq((uintptr_t)1, arg_args_raw[1], \"Wrong argument\");\n\tfree(ptr);\n\n\t/* calloc */\n\treset();\n\tptr = calloc(11, 13);\n\tassert_d_eq(call_count, 1, \"Hook not called\");\n\tassert_ptr_eq(arg_extra, (void *)123, \"Wrong extra\");\n\tassert_d_eq(arg_type, (int)hook_alloc_calloc, \"Wrong hook type\");\n\tassert_ptr_eq(ptr, arg_result, \"Wrong result\");\n\tassert_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,\n\t    \"Wrong raw result\");\n\tassert_u64_eq((uintptr_t)11, arg_args_raw[0], \"Wrong argument\");\n\tassert_u64_eq((uintptr_t)13, arg_args_raw[1], \"Wrong argument\");\n\tfree(ptr);\n\n\t/* memalign */\n#ifdef JEMALLOC_OVERRIDE_MEMALIGN\n\treset();\n\tptr = memalign(1024, 1);\n\tassert_d_eq(call_count, 1, \"Hook not called\");\n\tassert_ptr_eq(arg_extra, (void *)123, \"Wrong extra\");\n\tassert_d_eq(arg_type, (int)hook_alloc_memalign, \"Wrong hook type\");\n\tassert_ptr_eq(ptr, arg_result, \"Wrong result\");\n\tassert_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,\n\t    \"Wrong raw result\");\n\tassert_u64_eq((uintptr_t)1024, arg_args_raw[0], \"Wrong argument\");\n\tassert_u64_eq((uintptr_t)1, arg_args_raw[1], \"Wrong argument\");\n\tfree(ptr);\n#endif /* JEMALLOC_OVERRIDE_MEMALIGN */\n\n\t/* valloc */\n#ifdef JEMALLOC_OVERRIDE_VALLOC\n\treset();\n\tptr = valloc(1);\n\tassert_d_eq(call_count, 1, \"Hook not called\");\n\tassert_ptr_eq(arg_extra, (void *)123, \"Wrong extra\");\n\tassert_d_eq(arg_type, (int)hook_alloc_valloc, \"Wrong hook type\");\n\tassert_ptr_eq(ptr, arg_result, \"Wrong result\");\n\tassert_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,\n\t    \"Wrong raw result\");\n\tassert_u64_eq((uintptr_t)1, arg_args_raw[0], \"Wrong argument\");\n\tfree(ptr);\n#endif /* JEMALLOC_OVERRIDE_VALLOC */\n\n\t/* mallocx */\n\treset();\n\tptr = mallocx(1, MALLOCX_LG_ALIGN(10));\n\tassert_d_eq(call_count, 1, \"Hook not called\");\n\tassert_ptr_eq(arg_extra, (void *)123, \"Wrong extra\");\n\tassert_d_eq(arg_type, (int)hook_alloc_mallocx, \"Wrong hook type\");\n\tassert_ptr_eq(ptr, arg_result, \"Wrong result\");\n\tassert_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,\n\t    \"Wrong raw result\");\n\tassert_u64_eq((uintptr_t)1, arg_args_raw[0], \"Wrong argument\");\n\tassert_u64_eq((uintptr_t)MALLOCX_LG_ALIGN(10), arg_args_raw[1],\n\t    \"Wrong flags\");\n\tfree(ptr);\n\n\thook_remove(TSDN_NULL, handle);\n}\nTEST_END\n\nTEST_BEGIN(test_hooks_dalloc_simple) {\n\t/* \"Simple\" in the sense that we're not in a realloc variant. */\n\thooks_t hooks = {NULL, &test_dalloc_hook, NULL, (void *)123};\n\tvoid *handle = hook_install(TSDN_NULL, &hooks);\n\tassert_ptr_ne(handle, NULL, \"Hook installation failed\");\n\n\tvoid *volatile ptr;\n\n\t/* free() */\n\treset();\n\tptr = malloc(1);\n\tfree(ptr);\n\tassert_d_eq(call_count, 1, \"Hook not called\");\n\tassert_ptr_eq(arg_extra, (void *)123, \"Wrong extra\");\n\tassert_d_eq(arg_type, (int)hook_dalloc_free, \"Wrong hook type\");\n\tassert_ptr_eq(ptr, arg_address, \"Wrong pointer freed\");\n\tassert_u64_eq((uintptr_t)ptr, arg_args_raw[0], \"Wrong raw arg\");\n\n\t/* dallocx() */\n\treset();\n\tptr = malloc(1);\n\tdallocx(ptr, MALLOCX_TCACHE_NONE);\n\tassert_d_eq(call_count, 1, \"Hook not called\");\n\tassert_ptr_eq(arg_extra, (void *)123, \"Wrong extra\");\n\tassert_d_eq(arg_type, (int)hook_dalloc_dallocx, \"Wrong hook type\");\n\tassert_ptr_eq(ptr, arg_address, \"Wrong pointer freed\");\n\tassert_u64_eq((uintptr_t)ptr, arg_args_raw[0], \"Wrong raw arg\");\n\tassert_u64_eq((uintptr_t)MALLOCX_TCACHE_NONE, arg_args_raw[1],\n\t    \"Wrong raw arg\");\n\n\t/* sdallocx() */\n\treset();\n\tptr = malloc(1);\n\tsdallocx(ptr, 1, MALLOCX_TCACHE_NONE);\n\tassert_d_eq(call_count, 1, \"Hook not called\");\n\tassert_ptr_eq(arg_extra, (void *)123, \"Wrong extra\");\n\tassert_d_eq(arg_type, (int)hook_dalloc_sdallocx, \"Wrong hook type\");\n\tassert_ptr_eq(ptr, arg_address, \"Wrong pointer freed\");\n\tassert_u64_eq((uintptr_t)ptr, arg_args_raw[0], \"Wrong raw arg\");\n\tassert_u64_eq((uintptr_t)1, arg_args_raw[1], \"Wrong raw arg\");\n\tassert_u64_eq((uintptr_t)MALLOCX_TCACHE_NONE, arg_args_raw[2],\n\t    \"Wrong raw arg\");\n\n\thook_remove(TSDN_NULL, handle);\n}\nTEST_END\n\nTEST_BEGIN(test_hooks_expand_simple) {\n\t/* \"Simple\" in the sense that we're not in a realloc variant. */\n\thooks_t hooks = {NULL, NULL, &test_expand_hook, (void *)123};\n\tvoid *handle = hook_install(TSDN_NULL, &hooks);\n\tassert_ptr_ne(handle, NULL, \"Hook installation failed\");\n\n\tvoid *volatile ptr;\n\n\t/* xallocx() */\n\treset();\n\tptr = malloc(1);\n\tsize_t new_usize = xallocx(ptr, 100, 200, MALLOCX_TCACHE_NONE);\n\tassert_d_eq(call_count, 1, \"Hook not called\");\n\tassert_ptr_eq(arg_extra, (void *)123, \"Wrong extra\");\n\tassert_d_eq(arg_type, (int)hook_expand_xallocx, \"Wrong hook type\");\n\tassert_ptr_eq(ptr, arg_address, \"Wrong pointer expanded\");\n\tassert_u64_eq(arg_old_usize, nallocx(1, 0), \"Wrong old usize\");\n\tassert_u64_eq(arg_new_usize, sallocx(ptr, 0), \"Wrong new usize\");\n\tassert_u64_eq(new_usize, arg_result_raw, \"Wrong result\");\n\tassert_u64_eq((uintptr_t)ptr, arg_args_raw[0], \"Wrong arg\");\n\tassert_u64_eq(100, arg_args_raw[1], \"Wrong arg\");\n\tassert_u64_eq(200, arg_args_raw[2], \"Wrong arg\");\n\tassert_u64_eq(MALLOCX_TCACHE_NONE, arg_args_raw[3], \"Wrong arg\");\n\n\thook_remove(TSDN_NULL, handle);\n}\nTEST_END\n\nTEST_BEGIN(test_hooks_realloc_as_malloc_or_free) {\n\thooks_t hooks = {&test_alloc_hook, &test_dalloc_hook,\n\t\t&test_expand_hook, (void *)123};\n\tvoid *handle = hook_install(TSDN_NULL, &hooks);\n\tassert_ptr_ne(handle, NULL, \"Hook installation failed\");\n\n\tvoid *volatile ptr;\n\n\t/* realloc(NULL, size) as malloc */\n\treset();\n\tptr = realloc(NULL, 1);\n\tassert_d_eq(call_count, 1, \"Hook not called\");\n\tassert_ptr_eq(arg_extra, (void *)123, \"Wrong extra\");\n\tassert_d_eq(arg_type, (int)hook_alloc_realloc, \"Wrong hook type\");\n\tassert_ptr_eq(ptr, arg_result, \"Wrong result\");\n\tassert_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,\n\t    \"Wrong raw result\");\n\tassert_u64_eq((uintptr_t)NULL, arg_args_raw[0], \"Wrong argument\");\n\tassert_u64_eq((uintptr_t)1, arg_args_raw[1], \"Wrong argument\");\n\tfree(ptr);\n\n\t/* realloc(ptr, 0) as free */\n\tptr = malloc(1);\n\treset();\n\trealloc(ptr, 0);\n\tassert_d_eq(call_count, 1, \"Hook not called\");\n\tassert_ptr_eq(arg_extra, (void *)123, \"Wrong extra\");\n\tassert_d_eq(arg_type, (int)hook_dalloc_realloc, \"Wrong hook type\");\n\tassert_ptr_eq(ptr, arg_address, \"Wrong pointer freed\");\n\tassert_u64_eq((uintptr_t)ptr, arg_args_raw[0], \"Wrong raw arg\");\n\tassert_u64_eq((uintptr_t)0, arg_args_raw[1], \"Wrong raw arg\");\n\n\t/* realloc(NULL, 0) as malloc(0) */\n\treset();\n\tptr = realloc(NULL, 0);\n\tassert_d_eq(call_count, 1, \"Hook not called\");\n\tassert_ptr_eq(arg_extra, (void *)123, \"Wrong extra\");\n\tassert_d_eq(arg_type, (int)hook_alloc_realloc, \"Wrong hook type\");\n\tassert_ptr_eq(ptr, arg_result, \"Wrong result\");\n\tassert_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,\n\t    \"Wrong raw result\");\n\tassert_u64_eq((uintptr_t)NULL, arg_args_raw[0], \"Wrong argument\");\n\tassert_u64_eq((uintptr_t)0, arg_args_raw[1], \"Wrong argument\");\n\tfree(ptr);\n\n\thook_remove(TSDN_NULL, handle);\n}\nTEST_END\n\nstatic void\ndo_realloc_test(void *(*ralloc)(void *, size_t, int), int flags,\n    int expand_type, int dalloc_type) {\n\thooks_t hooks = {&test_alloc_hook, &test_dalloc_hook,\n\t\t&test_expand_hook, (void *)123};\n\tvoid *handle = hook_install(TSDN_NULL, &hooks);\n\tassert_ptr_ne(handle, NULL, \"Hook installation failed\");\n\n\tvoid *volatile ptr;\n\tvoid *volatile ptr2;\n\n\t/* Realloc in-place, small. */\n\tptr = malloc(129);\n\treset();\n\tptr2 = ralloc(ptr, 130, flags);\n\tassert_ptr_eq(ptr, ptr2, \"Small realloc moved\");\n\n\tassert_d_eq(call_count, 1, \"Hook not called\");\n\tassert_ptr_eq(arg_extra, (void *)123, \"Wrong extra\");\n\tassert_d_eq(arg_type, expand_type, \"Wrong hook type\");\n\tassert_ptr_eq(ptr, arg_address, \"Wrong address\");\n\tassert_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,\n\t    \"Wrong raw result\");\n\tassert_u64_eq((uintptr_t)ptr, arg_args_raw[0], \"Wrong argument\");\n\tassert_u64_eq((uintptr_t)130, arg_args_raw[1], \"Wrong argument\");\n\tfree(ptr);\n\n\t/*\n\t * Realloc in-place, large.  Since we can't guarantee the large case\n\t * across all platforms, we stay resilient to moving results.\n\t */\n\tptr = malloc(2 * 1024 * 1024);\n\tfree(ptr);\n\tptr2 = malloc(1 * 1024 * 1024);\n\treset();\n\tptr = ralloc(ptr2, 2 * 1024 * 1024, flags);\n\t/* ptr is the new address, ptr2 is the old address. */\n\tif (ptr == ptr2) {\n\t\tassert_d_eq(call_count, 1, \"Hook not called\");\n\t\tassert_d_eq(arg_type, expand_type, \"Wrong hook type\");\n\t} else {\n\t\tassert_d_eq(call_count, 2, \"Wrong hooks called\");\n\t\tassert_ptr_eq(ptr, arg_result, \"Wrong address\");\n\t\tassert_d_eq(arg_type, dalloc_type, \"Wrong hook type\");\n\t}\n\tassert_ptr_eq(arg_extra, (void *)123, \"Wrong extra\");\n\tassert_ptr_eq(ptr2, arg_address, \"Wrong address\");\n\tassert_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,\n\t    \"Wrong raw result\");\n\tassert_u64_eq((uintptr_t)ptr2, arg_args_raw[0], \"Wrong argument\");\n\tassert_u64_eq((uintptr_t)2 * 1024 * 1024, arg_args_raw[1],\n\t    \"Wrong argument\");\n\tfree(ptr);\n\n\t/* Realloc with move, small. */\n\tptr = malloc(8);\n\treset();\n\tptr2 = ralloc(ptr, 128, flags);\n\tassert_ptr_ne(ptr, ptr2, \"Small realloc didn't move\");\n\n\tassert_d_eq(call_count, 2, \"Hook not called\");\n\tassert_ptr_eq(arg_extra, (void *)123, \"Wrong extra\");\n\tassert_d_eq(arg_type, dalloc_type, \"Wrong hook type\");\n\tassert_ptr_eq(ptr, arg_address, \"Wrong address\");\n\tassert_ptr_eq(ptr2, arg_result, \"Wrong address\");\n\tassert_u64_eq((uintptr_t)ptr2, (uintptr_t)arg_result_raw,\n\t    \"Wrong raw result\");\n\tassert_u64_eq((uintptr_t)ptr, arg_args_raw[0], \"Wrong argument\");\n\tassert_u64_eq((uintptr_t)128, arg_args_raw[1], \"Wrong argument\");\n\tfree(ptr2);\n\n\t/* Realloc with move, large. */\n\tptr = malloc(1);\n\treset();\n\tptr2 = ralloc(ptr, 2 * 1024 * 1024, flags);\n\tassert_ptr_ne(ptr, ptr2, \"Large realloc didn't move\");\n\n\tassert_d_eq(call_count, 2, \"Hook not called\");\n\tassert_ptr_eq(arg_extra, (void *)123, \"Wrong extra\");\n\tassert_d_eq(arg_type, dalloc_type, \"Wrong hook type\");\n\tassert_ptr_eq(ptr, arg_address, \"Wrong address\");\n\tassert_ptr_eq(ptr2, arg_result, \"Wrong address\");\n\tassert_u64_eq((uintptr_t)ptr2, (uintptr_t)arg_result_raw,\n\t    \"Wrong raw result\");\n\tassert_u64_eq((uintptr_t)ptr, arg_args_raw[0], \"Wrong argument\");\n\tassert_u64_eq((uintptr_t)2 * 1024 * 1024, arg_args_raw[1],\n\t    \"Wrong argument\");\n\tfree(ptr2);\n\n\thook_remove(TSDN_NULL, handle);\n}\n\nstatic void *\nrealloc_wrapper(void *ptr, size_t size, UNUSED int flags) {\n\treturn realloc(ptr, size);\n}\n\nTEST_BEGIN(test_hooks_realloc) {\n\tdo_realloc_test(&realloc_wrapper, 0, hook_expand_realloc,\n\t    hook_dalloc_realloc);\n}\nTEST_END\n\nTEST_BEGIN(test_hooks_rallocx) {\n\tdo_realloc_test(&rallocx, MALLOCX_TCACHE_NONE, hook_expand_rallocx,\n\t    hook_dalloc_rallocx);\n}\nTEST_END\n\nint\nmain(void) {\n\t/* We assert on call counts. */\n\treturn test_no_reentrancy(\n\t    test_hooks_basic,\n\t    test_hooks_null,\n\t    test_hooks_remove,\n\t    test_hooks_alloc_simple,\n\t    test_hooks_dalloc_simple,\n\t    test_hooks_expand_simple,\n\t    test_hooks_realloc_as_malloc_or_free,\n\t    test_hooks_realloc,\n\t    test_hooks_rallocx);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/huge.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n/* Threshold: 2 << 20 = 2097152. */\nconst char *malloc_conf = \"oversize_threshold:2097152\";\n\n#define HUGE_SZ (2 << 20)\n#define SMALL_SZ (8)\n\nTEST_BEGIN(huge_bind_thread) {\n\tunsigned arena1, arena2;\n\tsize_t sz = sizeof(unsigned);\n\n\t/* Bind to a manual arena. */\n\tassert_d_eq(mallctl(\"arenas.create\", &arena1, &sz, NULL, 0), 0,\n\t    \"Failed to create arena\");\n\tassert_d_eq(mallctl(\"thread.arena\", NULL, NULL, &arena1,\n\t    sizeof(arena1)), 0, \"Fail to bind thread\");\n\n\tvoid *ptr = mallocx(HUGE_SZ, 0);\n\tassert_ptr_not_null(ptr, \"Fail to allocate huge size\");\n\tassert_d_eq(mallctl(\"arenas.lookup\", &arena2, &sz, &ptr,\n\t    sizeof(ptr)), 0, \"Unexpected mallctl() failure\");\n\tassert_u_eq(arena1, arena2, \"Wrong arena used after binding\");\n\tdallocx(ptr, 0);\n\n\t/* Switch back to arena 0. */\n\ttest_skip_if(have_percpu_arena &&\n\t    PERCPU_ARENA_ENABLED(opt_percpu_arena));\n\tarena2 = 0;\n\tassert_d_eq(mallctl(\"thread.arena\", NULL, NULL, &arena2,\n\t    sizeof(arena2)), 0, \"Fail to bind thread\");\n\tptr = mallocx(SMALL_SZ, MALLOCX_TCACHE_NONE);\n\tassert_d_eq(mallctl(\"arenas.lookup\", &arena2, &sz, &ptr,\n\t    sizeof(ptr)), 0, \"Unexpected mallctl() failure\");\n\tassert_u_eq(arena2, 0, \"Wrong arena used after binding\");\n\tdallocx(ptr, MALLOCX_TCACHE_NONE);\n\n\t/* Then huge allocation should use the huge arena. */\n\tptr = mallocx(HUGE_SZ, 0);\n\tassert_ptr_not_null(ptr, \"Fail to allocate huge size\");\n\tassert_d_eq(mallctl(\"arenas.lookup\", &arena2, &sz, &ptr,\n\t    sizeof(ptr)), 0, \"Unexpected mallctl() failure\");\n\tassert_u_ne(arena2, 0, \"Wrong arena used after binding\");\n\tassert_u_ne(arena1, arena2, \"Wrong arena used after binding\");\n\tdallocx(ptr, 0);\n}\nTEST_END\n\nTEST_BEGIN(huge_mallocx) {\n\tunsigned arena1, arena2;\n\tsize_t sz = sizeof(unsigned);\n\n\tassert_d_eq(mallctl(\"arenas.create\", &arena1, &sz, NULL, 0), 0,\n\t    \"Failed to create arena\");\n\tvoid *huge = mallocx(HUGE_SZ, MALLOCX_ARENA(arena1));\n\tassert_ptr_not_null(huge, \"Fail to allocate huge size\");\n\tassert_d_eq(mallctl(\"arenas.lookup\", &arena2, &sz, &huge,\n\t    sizeof(huge)), 0, \"Unexpected mallctl() failure\");\n\tassert_u_eq(arena1, arena2, \"Wrong arena used for mallocx\");\n\tdallocx(huge, MALLOCX_ARENA(arena1));\n\n\tvoid *huge2 = mallocx(HUGE_SZ, 0);\n\tassert_ptr_not_null(huge, \"Fail to allocate huge size\");\n\tassert_d_eq(mallctl(\"arenas.lookup\", &arena2, &sz, &huge2,\n\t    sizeof(huge2)), 0, \"Unexpected mallctl() failure\");\n\tassert_u_ne(arena1, arena2,\n\t    \"Huge allocation should not come from the manual arena.\");\n\tassert_u_ne(arena2, 0,\n\t    \"Huge allocation should not come from the arena 0.\");\n\tdallocx(huge2, 0);\n}\nTEST_END\n\nTEST_BEGIN(huge_allocation) {\n\tunsigned arena1, arena2;\n\n\tvoid *ptr = mallocx(HUGE_SZ, 0);\n\tassert_ptr_not_null(ptr, \"Fail to allocate huge size\");\n\tsize_t sz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.lookup\", &arena1, &sz, &ptr, sizeof(ptr)),\n\t    0, \"Unexpected mallctl() failure\");\n\tassert_u_gt(arena1, 0, \"Huge allocation should not come from arena 0\");\n\tdallocx(ptr, 0);\n\n\tptr = mallocx(HUGE_SZ >> 1, 0);\n\tassert_ptr_not_null(ptr, \"Fail to allocate half huge size\");\n\tassert_d_eq(mallctl(\"arenas.lookup\", &arena2, &sz, &ptr,\n\t    sizeof(ptr)), 0, \"Unexpected mallctl() failure\");\n\tassert_u_ne(arena1, arena2, \"Wrong arena used for half huge\");\n\tdallocx(ptr, 0);\n\n\tptr = mallocx(SMALL_SZ, MALLOCX_TCACHE_NONE);\n\tassert_ptr_not_null(ptr, \"Fail to allocate small size\");\n\tassert_d_eq(mallctl(\"arenas.lookup\", &arena2, &sz, &ptr,\n\t    sizeof(ptr)), 0, \"Unexpected mallctl() failure\");\n\tassert_u_ne(arena1, arena2,\n\t    \"Huge and small should be from different arenas\");\n\tdallocx(ptr, 0);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    huge_allocation,\n\t    huge_mallocx,\n\t    huge_bind_thread);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/junk.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/util.h\"\n\nstatic arena_dalloc_junk_small_t *arena_dalloc_junk_small_orig;\nstatic large_dalloc_junk_t *large_dalloc_junk_orig;\nstatic large_dalloc_maybe_junk_t *large_dalloc_maybe_junk_orig;\nstatic void *watch_for_junking;\nstatic bool saw_junking;\n\nstatic void\nwatch_junking(void *p) {\n\twatch_for_junking = p;\n\tsaw_junking = false;\n}\n\nstatic void\narena_dalloc_junk_small_intercept(void *ptr, const bin_info_t *bin_info) {\n\tsize_t i;\n\n\tarena_dalloc_junk_small_orig(ptr, bin_info);\n\tfor (i = 0; i < bin_info->reg_size; i++) {\n\t\tassert_u_eq(((uint8_t *)ptr)[i], JEMALLOC_FREE_JUNK,\n\t\t    \"Missing junk fill for byte %zu/%zu of deallocated region\",\n\t\t    i, bin_info->reg_size);\n\t}\n\tif (ptr == watch_for_junking) {\n\t\tsaw_junking = true;\n\t}\n}\n\nstatic void\nlarge_dalloc_junk_intercept(void *ptr, size_t usize) {\n\tsize_t i;\n\n\tlarge_dalloc_junk_orig(ptr, usize);\n\tfor (i = 0; i < usize; i++) {\n\t\tassert_u_eq(((uint8_t *)ptr)[i], JEMALLOC_FREE_JUNK,\n\t\t    \"Missing junk fill for byte %zu/%zu of deallocated region\",\n\t\t    i, usize);\n\t}\n\tif (ptr == watch_for_junking) {\n\t\tsaw_junking = true;\n\t}\n}\n\nstatic void\nlarge_dalloc_maybe_junk_intercept(void *ptr, size_t usize) {\n\tlarge_dalloc_maybe_junk_orig(ptr, usize);\n\tif (ptr == watch_for_junking) {\n\t\tsaw_junking = true;\n\t}\n}\n\nstatic void\ntest_junk(size_t sz_min, size_t sz_max) {\n\tuint8_t *s;\n\tsize_t sz_prev, sz, i;\n\n\tif (opt_junk_free) {\n\t\tarena_dalloc_junk_small_orig = arena_dalloc_junk_small;\n\t\tarena_dalloc_junk_small = arena_dalloc_junk_small_intercept;\n\t\tlarge_dalloc_junk_orig = large_dalloc_junk;\n\t\tlarge_dalloc_junk = large_dalloc_junk_intercept;\n\t\tlarge_dalloc_maybe_junk_orig = large_dalloc_maybe_junk;\n\t\tlarge_dalloc_maybe_junk = large_dalloc_maybe_junk_intercept;\n\t}\n\n\tsz_prev = 0;\n\ts = (uint8_t *)mallocx(sz_min, 0);\n\tassert_ptr_not_null((void *)s, \"Unexpected mallocx() failure\");\n\n\tfor (sz = sallocx(s, 0); sz <= sz_max;\n\t    sz_prev = sz, sz = sallocx(s, 0)) {\n\t\tif (sz_prev > 0) {\n\t\t\tassert_u_eq(s[0], 'a',\n\t\t\t    \"Previously allocated byte %zu/%zu is corrupted\",\n\t\t\t    ZU(0), sz_prev);\n\t\t\tassert_u_eq(s[sz_prev-1], 'a',\n\t\t\t    \"Previously allocated byte %zu/%zu is corrupted\",\n\t\t\t    sz_prev-1, sz_prev);\n\t\t}\n\n\t\tfor (i = sz_prev; i < sz; i++) {\n\t\t\tif (opt_junk_alloc) {\n\t\t\t\tassert_u_eq(s[i], JEMALLOC_ALLOC_JUNK,\n\t\t\t\t    \"Newly allocated byte %zu/%zu isn't \"\n\t\t\t\t    \"junk-filled\", i, sz);\n\t\t\t}\n\t\t\ts[i] = 'a';\n\t\t}\n\n\t\tif (xallocx(s, sz+1, 0, 0) == sz) {\n\t\t\tuint8_t *t;\n\t\t\twatch_junking(s);\n\t\t\tt = (uint8_t *)rallocx(s, sz+1, 0);\n\t\t\tassert_ptr_not_null((void *)t,\n\t\t\t    \"Unexpected rallocx() failure\");\n\t\t\tassert_zu_ge(sallocx(t, 0), sz+1,\n\t\t\t    \"Unexpectedly small rallocx() result\");\n\t\t\tif (!background_thread_enabled()) {\n\t\t\t\tassert_ptr_ne(s, t,\n\t\t\t\t    \"Unexpected in-place rallocx()\");\n\t\t\t\tassert_true(!opt_junk_free || saw_junking,\n\t\t\t\t    \"Expected region of size %zu to be \"\n\t\t\t\t    \"junk-filled\", sz);\n\t\t\t}\n\t\t\ts = t;\n\t\t}\n\t}\n\n\twatch_junking(s);\n\tdallocx(s, 0);\n\tassert_true(!opt_junk_free || saw_junking,\n\t    \"Expected region of size %zu to be junk-filled\", sz);\n\n\tif (opt_junk_free) {\n\t\tarena_dalloc_junk_small = arena_dalloc_junk_small_orig;\n\t\tlarge_dalloc_junk = large_dalloc_junk_orig;\n\t\tlarge_dalloc_maybe_junk = large_dalloc_maybe_junk_orig;\n\t}\n}\n\nTEST_BEGIN(test_junk_small) {\n\ttest_skip_if(!config_fill);\n\ttest_junk(1, SC_SMALL_MAXCLASS - 1);\n}\nTEST_END\n\nTEST_BEGIN(test_junk_large) {\n\ttest_skip_if(!config_fill);\n\ttest_junk(SC_SMALL_MAXCLASS + 1, (1U << (SC_LG_LARGE_MINCLASS + 1)));\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_junk_small,\n\t    test_junk_large);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/junk.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_fill}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"abort:false,zero:false,junk:true\"\nfi\n"
  },
  {
    "path": "deps/jemalloc/test/unit/junk_alloc.c",
    "content": "#include \"junk.c\"\n"
  },
  {
    "path": "deps/jemalloc/test/unit/junk_alloc.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_fill}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"abort:false,zero:false,junk:alloc\"\nfi\n"
  },
  {
    "path": "deps/jemalloc/test/unit/junk_free.c",
    "content": "#include \"junk.c\"\n"
  },
  {
    "path": "deps/jemalloc/test/unit/junk_free.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_fill}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"abort:false,zero:false,junk:free\"\nfi\n"
  },
  {
    "path": "deps/jemalloc/test/unit/log.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/log.h\"\n\nstatic void\nexpect_no_logging(const char *names) {\n\tlog_var_t log_l1 = LOG_VAR_INIT(\"l1\");\n\tlog_var_t log_l2 = LOG_VAR_INIT(\"l2\");\n\tlog_var_t log_l2_a = LOG_VAR_INIT(\"l2.a\");\n\n\tstrcpy(log_var_names, names);\n\n\tint count = 0;\n\n\tfor (int i = 0; i < 10; i++) {\n\t\tlog_do_begin(log_l1)\n\t\t\tcount++;\n\t\tlog_do_end(log_l1)\n\n\t\tlog_do_begin(log_l2)\n\t\t\tcount++;\n\t\tlog_do_end(log_l2)\n\n\t\tlog_do_begin(log_l2_a)\n\t\t\tcount++;\n\t\tlog_do_end(log_l2_a)\n\t}\n\tassert_d_eq(count, 0, \"Disabled logging not ignored!\");\n}\n\nTEST_BEGIN(test_log_disabled) {\n\ttest_skip_if(!config_log);\n\tatomic_store_b(&log_init_done, true, ATOMIC_RELAXED);\n\texpect_no_logging(\"\");\n\texpect_no_logging(\"abc\");\n\texpect_no_logging(\"a.b.c\");\n\texpect_no_logging(\"l12\");\n\texpect_no_logging(\"l123|a456|b789\");\n\texpect_no_logging(\"|||\");\n}\nTEST_END\n\nTEST_BEGIN(test_log_enabled_direct) {\n\ttest_skip_if(!config_log);\n\tatomic_store_b(&log_init_done, true, ATOMIC_RELAXED);\n\tlog_var_t log_l1 = LOG_VAR_INIT(\"l1\");\n\tlog_var_t log_l1_a = LOG_VAR_INIT(\"l1.a\");\n\tlog_var_t log_l2 = LOG_VAR_INIT(\"l2\");\n\n\tint count;\n\n\tcount = 0;\n\tstrcpy(log_var_names, \"l1\");\n\tfor (int i = 0; i < 10; i++) {\n\t\tlog_do_begin(log_l1)\n\t\t\tcount++;\n\t\tlog_do_end(log_l1)\n\t}\n\tassert_d_eq(count, 10, \"Mis-logged!\");\n\n\tcount = 0;\n\tstrcpy(log_var_names, \"l1.a\");\n\tfor (int i = 0; i < 10; i++) {\n\t\tlog_do_begin(log_l1_a)\n\t\t\tcount++;\n\t\tlog_do_end(log_l1_a)\n\t}\n\tassert_d_eq(count, 10, \"Mis-logged!\");\n\n\tcount = 0;\n\tstrcpy(log_var_names, \"l1.a|abc|l2|def\");\n\tfor (int i = 0; i < 10; i++) {\n\t\tlog_do_begin(log_l1_a)\n\t\t\tcount++;\n\t\tlog_do_end(log_l1_a)\n\n\t\tlog_do_begin(log_l2)\n\t\t\tcount++;\n\t\tlog_do_end(log_l2)\n\t}\n\tassert_d_eq(count, 20, \"Mis-logged!\");\n}\nTEST_END\n\nTEST_BEGIN(test_log_enabled_indirect) {\n\ttest_skip_if(!config_log);\n\tatomic_store_b(&log_init_done, true, ATOMIC_RELAXED);\n\tstrcpy(log_var_names, \"l0|l1|abc|l2.b|def\");\n\n\t/* On. */\n\tlog_var_t log_l1 = LOG_VAR_INIT(\"l1\");\n\t/* Off. */\n\tlog_var_t log_l1a = LOG_VAR_INIT(\"l1a\");\n\t/* On. */\n\tlog_var_t log_l1_a = LOG_VAR_INIT(\"l1.a\");\n\t/* Off. */\n\tlog_var_t log_l2_a = LOG_VAR_INIT(\"l2.a\");\n\t/* On. */\n\tlog_var_t log_l2_b_a = LOG_VAR_INIT(\"l2.b.a\");\n\t/* On. */\n\tlog_var_t log_l2_b_b = LOG_VAR_INIT(\"l2.b.b\");\n\n\t/* 4 are on total, so should sum to 40. */\n\tint count = 0;\n\tfor (int i = 0; i < 10; i++) {\n\t\tlog_do_begin(log_l1)\n\t\t\tcount++;\n\t\tlog_do_end(log_l1)\n\n\t\tlog_do_begin(log_l1a)\n\t\t\tcount++;\n\t\tlog_do_end(log_l1a)\n\n\t\tlog_do_begin(log_l1_a)\n\t\t\tcount++;\n\t\tlog_do_end(log_l1_a)\n\n\t\tlog_do_begin(log_l2_a)\n\t\t\tcount++;\n\t\tlog_do_end(log_l2_a)\n\n\t\tlog_do_begin(log_l2_b_a)\n\t\t\tcount++;\n\t\tlog_do_end(log_l2_b_a)\n\n\t\tlog_do_begin(log_l2_b_b)\n\t\t\tcount++;\n\t\tlog_do_end(log_l2_b_b)\n\t}\n\n\tassert_d_eq(count, 40, \"Mis-logged!\");\n}\nTEST_END\n\nTEST_BEGIN(test_log_enabled_global) {\n\ttest_skip_if(!config_log);\n\tatomic_store_b(&log_init_done, true, ATOMIC_RELAXED);\n\tstrcpy(log_var_names, \"abc|.|def\");\n\n\tlog_var_t log_l1 = LOG_VAR_INIT(\"l1\");\n\tlog_var_t log_l2_a_a = LOG_VAR_INIT(\"l2.a.a\");\n\n\tint count = 0;\n\tfor (int i = 0; i < 10; i++) {\n\t\tlog_do_begin(log_l1)\n\t\t    count++;\n\t\tlog_do_end(log_l1)\n\n\t\tlog_do_begin(log_l2_a_a)\n\t\t    count++;\n\t\tlog_do_end(log_l2_a_a)\n\t}\n\tassert_d_eq(count, 20, \"Mis-logged!\");\n}\nTEST_END\n\nTEST_BEGIN(test_logs_if_no_init) {\n\ttest_skip_if(!config_log);\n\tatomic_store_b(&log_init_done, false, ATOMIC_RELAXED);\n\n\tlog_var_t l = LOG_VAR_INIT(\"definitely.not.enabled\");\n\n\tint count = 0;\n\tfor (int i = 0; i < 10; i++) {\n\t\tlog_do_begin(l)\n\t\t\tcount++;\n\t\tlog_do_end(l)\n\t}\n\tassert_d_eq(count, 0, \"Logging shouldn't happen if not initialized.\");\n}\nTEST_END\n\n/*\n * This really just checks to make sure that this usage compiles; we don't have\n * any test code to run.\n */\nTEST_BEGIN(test_log_only_format_string) {\n\tif (false) {\n\t\tLOG(\"log_str\", \"No arguments follow this format string.\");\n\t}\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_log_disabled,\n\t    test_log_enabled_direct,\n\t    test_log_enabled_indirect,\n\t    test_log_enabled_global,\n\t    test_logs_if_no_init,\n\t    test_log_only_format_string);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/mallctl.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/hook.h\"\n#include \"jemalloc/internal/util.h\"\n\nTEST_BEGIN(test_mallctl_errors) {\n\tuint64_t epoch;\n\tsize_t sz;\n\n\tassert_d_eq(mallctl(\"no_such_name\", NULL, NULL, NULL, 0), ENOENT,\n\t    \"mallctl() should return ENOENT for non-existent names\");\n\n\tassert_d_eq(mallctl(\"version\", NULL, NULL, \"0.0.0\", strlen(\"0.0.0\")),\n\t    EPERM, \"mallctl() should return EPERM on attempt to write \"\n\t    \"read-only value\");\n\n\tassert_d_eq(mallctl(\"epoch\", NULL, NULL, (void *)&epoch,\n\t    sizeof(epoch)-1), EINVAL,\n\t    \"mallctl() should return EINVAL for input size mismatch\");\n\tassert_d_eq(mallctl(\"epoch\", NULL, NULL, (void *)&epoch,\n\t    sizeof(epoch)+1), EINVAL,\n\t    \"mallctl() should return EINVAL for input size mismatch\");\n\n\tsz = sizeof(epoch)-1;\n\tassert_d_eq(mallctl(\"epoch\", (void *)&epoch, &sz, NULL, 0), EINVAL,\n\t    \"mallctl() should return EINVAL for output size mismatch\");\n\tsz = sizeof(epoch)+1;\n\tassert_d_eq(mallctl(\"epoch\", (void *)&epoch, &sz, NULL, 0), EINVAL,\n\t    \"mallctl() should return EINVAL for output size mismatch\");\n}\nTEST_END\n\nTEST_BEGIN(test_mallctlnametomib_errors) {\n\tsize_t mib[1];\n\tsize_t miblen;\n\n\tmiblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"no_such_name\", mib, &miblen), ENOENT,\n\t    \"mallctlnametomib() should return ENOENT for non-existent names\");\n}\nTEST_END\n\nTEST_BEGIN(test_mallctlbymib_errors) {\n\tuint64_t epoch;\n\tsize_t sz;\n\tsize_t mib[1];\n\tsize_t miblen;\n\n\tmiblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"version\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, \"0.0.0\",\n\t    strlen(\"0.0.0\")), EPERM, \"mallctl() should return EPERM on \"\n\t    \"attempt to write read-only value\");\n\n\tmiblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"epoch\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, (void *)&epoch,\n\t    sizeof(epoch)-1), EINVAL,\n\t    \"mallctlbymib() should return EINVAL for input size mismatch\");\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, (void *)&epoch,\n\t    sizeof(epoch)+1), EINVAL,\n\t    \"mallctlbymib() should return EINVAL for input size mismatch\");\n\n\tsz = sizeof(epoch)-1;\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&epoch, &sz, NULL, 0),\n\t    EINVAL,\n\t    \"mallctlbymib() should return EINVAL for output size mismatch\");\n\tsz = sizeof(epoch)+1;\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&epoch, &sz, NULL, 0),\n\t    EINVAL,\n\t    \"mallctlbymib() should return EINVAL for output size mismatch\");\n}\nTEST_END\n\nTEST_BEGIN(test_mallctl_read_write) {\n\tuint64_t old_epoch, new_epoch;\n\tsize_t sz = sizeof(old_epoch);\n\n\t/* Blind. */\n\tassert_d_eq(mallctl(\"epoch\", NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\tassert_zu_eq(sz, sizeof(old_epoch), \"Unexpected output size\");\n\n\t/* Read. */\n\tassert_d_eq(mallctl(\"epoch\", (void *)&old_epoch, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\tassert_zu_eq(sz, sizeof(old_epoch), \"Unexpected output size\");\n\n\t/* Write. */\n\tassert_d_eq(mallctl(\"epoch\", NULL, NULL, (void *)&new_epoch,\n\t    sizeof(new_epoch)), 0, \"Unexpected mallctl() failure\");\n\tassert_zu_eq(sz, sizeof(old_epoch), \"Unexpected output size\");\n\n\t/* Read+write. */\n\tassert_d_eq(mallctl(\"epoch\", (void *)&old_epoch, &sz,\n\t    (void *)&new_epoch, sizeof(new_epoch)), 0,\n\t    \"Unexpected mallctl() failure\");\n\tassert_zu_eq(sz, sizeof(old_epoch), \"Unexpected output size\");\n}\nTEST_END\n\nTEST_BEGIN(test_mallctlnametomib_short_mib) {\n\tsize_t mib[4];\n\tsize_t miblen;\n\n\tmiblen = 3;\n\tmib[3] = 42;\n\tassert_d_eq(mallctlnametomib(\"arenas.bin.0.nregs\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tassert_zu_eq(miblen, 3, \"Unexpected mib output length\");\n\tassert_zu_eq(mib[3], 42,\n\t    \"mallctlnametomib() wrote past the end of the input mib\");\n}\nTEST_END\n\nTEST_BEGIN(test_mallctl_config) {\n#define TEST_MALLCTL_CONFIG(config, t) do {\t\t\t\t\\\n\tt oldval;\t\t\t\t\t\t\t\\\n\tsize_t sz = sizeof(oldval);\t\t\t\t\t\\\n\tassert_d_eq(mallctl(\"config.\"#config, (void *)&oldval, &sz,\t\\\n\t    NULL, 0), 0, \"Unexpected mallctl() failure\");\t\t\\\n\tassert_b_eq(oldval, config_##config, \"Incorrect config value\");\t\\\n\tassert_zu_eq(sz, sizeof(oldval), \"Unexpected output size\");\t\\\n} while (0)\n\n\tTEST_MALLCTL_CONFIG(cache_oblivious, bool);\n\tTEST_MALLCTL_CONFIG(debug, bool);\n\tTEST_MALLCTL_CONFIG(fill, bool);\n\tTEST_MALLCTL_CONFIG(lazy_lock, bool);\n\tTEST_MALLCTL_CONFIG(malloc_conf, const char *);\n\tTEST_MALLCTL_CONFIG(prof, bool);\n\tTEST_MALLCTL_CONFIG(prof_libgcc, bool);\n\tTEST_MALLCTL_CONFIG(prof_libunwind, bool);\n\tTEST_MALLCTL_CONFIG(stats, bool);\n\tTEST_MALLCTL_CONFIG(utrace, bool);\n\tTEST_MALLCTL_CONFIG(xmalloc, bool);\n\n#undef TEST_MALLCTL_CONFIG\n}\nTEST_END\n\nTEST_BEGIN(test_mallctl_opt) {\n\tbool config_always = true;\n\n#define TEST_MALLCTL_OPT(t, opt, config) do {\t\t\t\t\\\n\tt oldval;\t\t\t\t\t\t\t\\\n\tsize_t sz = sizeof(oldval);\t\t\t\t\t\\\n\tint expected = config_##config ? 0 : ENOENT;\t\t\t\\\n\tint result = mallctl(\"opt.\"#opt, (void *)&oldval, &sz, NULL,\t\\\n\t    0);\t\t\t\t\t\t\t\t\\\n\tassert_d_eq(result, expected,\t\t\t\t\t\\\n\t    \"Unexpected mallctl() result for opt.\"#opt);\t\t\\\n\tassert_zu_eq(sz, sizeof(oldval), \"Unexpected output size\");\t\\\n} while (0)\n\n\tTEST_MALLCTL_OPT(bool, abort, always);\n\tTEST_MALLCTL_OPT(bool, abort_conf, always);\n\tTEST_MALLCTL_OPT(bool, confirm_conf, always);\n\tTEST_MALLCTL_OPT(const char *, metadata_thp, always);\n\tTEST_MALLCTL_OPT(bool, retain, always);\n\tTEST_MALLCTL_OPT(const char *, dss, always);\n\tTEST_MALLCTL_OPT(unsigned, narenas, always);\n\tTEST_MALLCTL_OPT(const char *, percpu_arena, always);\n\tTEST_MALLCTL_OPT(size_t, oversize_threshold, always);\n\tTEST_MALLCTL_OPT(bool, background_thread, always);\n\tTEST_MALLCTL_OPT(ssize_t, dirty_decay_ms, always);\n\tTEST_MALLCTL_OPT(ssize_t, muzzy_decay_ms, always);\n\tTEST_MALLCTL_OPT(bool, stats_print, always);\n\tTEST_MALLCTL_OPT(const char *, junk, fill);\n\tTEST_MALLCTL_OPT(bool, zero, fill);\n\tTEST_MALLCTL_OPT(bool, utrace, utrace);\n\tTEST_MALLCTL_OPT(bool, xmalloc, xmalloc);\n\tTEST_MALLCTL_OPT(bool, tcache, always);\n\tTEST_MALLCTL_OPT(size_t, lg_extent_max_active_fit, always);\n\tTEST_MALLCTL_OPT(size_t, lg_tcache_max, always);\n\tTEST_MALLCTL_OPT(const char *, thp, always);\n\tTEST_MALLCTL_OPT(bool, prof, prof);\n\tTEST_MALLCTL_OPT(const char *, prof_prefix, prof);\n\tTEST_MALLCTL_OPT(bool, prof_active, prof);\n\tTEST_MALLCTL_OPT(ssize_t, lg_prof_sample, prof);\n\tTEST_MALLCTL_OPT(bool, prof_accum, prof);\n\tTEST_MALLCTL_OPT(ssize_t, lg_prof_interval, prof);\n\tTEST_MALLCTL_OPT(bool, prof_gdump, prof);\n\tTEST_MALLCTL_OPT(bool, prof_final, prof);\n\tTEST_MALLCTL_OPT(bool, prof_leak, prof);\n\n#undef TEST_MALLCTL_OPT\n}\nTEST_END\n\nTEST_BEGIN(test_manpage_example) {\n\tunsigned nbins, i;\n\tsize_t mib[4];\n\tsize_t len, miblen;\n\n\tlen = sizeof(nbins);\n\tassert_d_eq(mallctl(\"arenas.nbins\", (void *)&nbins, &len, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tmiblen = 4;\n\tassert_d_eq(mallctlnametomib(\"arenas.bin.0.size\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tfor (i = 0; i < nbins; i++) {\n\t\tsize_t bin_size;\n\n\t\tmib[2] = i;\n\t\tlen = sizeof(bin_size);\n\t\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&bin_size, &len,\n\t\t    NULL, 0), 0, \"Unexpected mallctlbymib() failure\");\n\t\t/* Do something with bin_size... */\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_tcache_none) {\n\ttest_skip_if(!opt_tcache);\n\n\t/* Allocate p and q. */\n\tvoid *p0 = mallocx(42, 0);\n\tassert_ptr_not_null(p0, \"Unexpected mallocx() failure\");\n\tvoid *q = mallocx(42, 0);\n\tassert_ptr_not_null(q, \"Unexpected mallocx() failure\");\n\n\t/* Deallocate p and q, but bypass the tcache for q. */\n\tdallocx(p0, 0);\n\tdallocx(q, MALLOCX_TCACHE_NONE);\n\n\t/* Make sure that tcache-based allocation returns p, not q. */\n\tvoid *p1 = mallocx(42, 0);\n\tassert_ptr_not_null(p1, \"Unexpected mallocx() failure\");\n\tassert_ptr_eq(p0, p1, \"Expected tcache to allocate cached region\");\n\n\t/* Clean up. */\n\tdallocx(p1, MALLOCX_TCACHE_NONE);\n}\nTEST_END\n\nTEST_BEGIN(test_tcache) {\n#define NTCACHES\t10\n\tunsigned tis[NTCACHES];\n\tvoid *ps[NTCACHES];\n\tvoid *qs[NTCACHES];\n\tunsigned i;\n\tsize_t sz, psz, qsz;\n\n\tpsz = 42;\n\tqsz = nallocx(psz, 0) + 1;\n\n\t/* Create tcaches. */\n\tfor (i = 0; i < NTCACHES; i++) {\n\t\tsz = sizeof(unsigned);\n\t\tassert_d_eq(mallctl(\"tcache.create\", (void *)&tis[i], &sz, NULL,\n\t\t    0), 0, \"Unexpected mallctl() failure, i=%u\", i);\n\t}\n\n\t/* Exercise tcache ID recycling. */\n\tfor (i = 0; i < NTCACHES; i++) {\n\t\tassert_d_eq(mallctl(\"tcache.destroy\", NULL, NULL,\n\t\t    (void *)&tis[i], sizeof(unsigned)), 0,\n\t\t    \"Unexpected mallctl() failure, i=%u\", i);\n\t}\n\tfor (i = 0; i < NTCACHES; i++) {\n\t\tsz = sizeof(unsigned);\n\t\tassert_d_eq(mallctl(\"tcache.create\", (void *)&tis[i], &sz, NULL,\n\t\t    0), 0, \"Unexpected mallctl() failure, i=%u\", i);\n\t}\n\n\t/* Flush empty tcaches. */\n\tfor (i = 0; i < NTCACHES; i++) {\n\t\tassert_d_eq(mallctl(\"tcache.flush\", NULL, NULL, (void *)&tis[i],\n\t\t    sizeof(unsigned)), 0, \"Unexpected mallctl() failure, i=%u\",\n\t\t    i);\n\t}\n\n\t/* Cache some allocations. */\n\tfor (i = 0; i < NTCACHES; i++) {\n\t\tps[i] = mallocx(psz, MALLOCX_TCACHE(tis[i]));\n\t\tassert_ptr_not_null(ps[i], \"Unexpected mallocx() failure, i=%u\",\n\t\t    i);\n\t\tdallocx(ps[i], MALLOCX_TCACHE(tis[i]));\n\n\t\tqs[i] = mallocx(qsz, MALLOCX_TCACHE(tis[i]));\n\t\tassert_ptr_not_null(qs[i], \"Unexpected mallocx() failure, i=%u\",\n\t\t    i);\n\t\tdallocx(qs[i], MALLOCX_TCACHE(tis[i]));\n\t}\n\n\t/* Verify that tcaches allocate cached regions. */\n\tfor (i = 0; i < NTCACHES; i++) {\n\t\tvoid *p0 = ps[i];\n\t\tps[i] = mallocx(psz, MALLOCX_TCACHE(tis[i]));\n\t\tassert_ptr_not_null(ps[i], \"Unexpected mallocx() failure, i=%u\",\n\t\t    i);\n\t\tassert_ptr_eq(ps[i], p0,\n\t\t    \"Expected mallocx() to allocate cached region, i=%u\", i);\n\t}\n\n\t/* Verify that reallocation uses cached regions. */\n\tfor (i = 0; i < NTCACHES; i++) {\n\t\tvoid *q0 = qs[i];\n\t\tqs[i] = rallocx(ps[i], qsz, MALLOCX_TCACHE(tis[i]));\n\t\tassert_ptr_not_null(qs[i], \"Unexpected rallocx() failure, i=%u\",\n\t\t    i);\n\t\tassert_ptr_eq(qs[i], q0,\n\t\t    \"Expected rallocx() to allocate cached region, i=%u\", i);\n\t\t/* Avoid undefined behavior in case of test failure. */\n\t\tif (qs[i] == NULL) {\n\t\t\tqs[i] = ps[i];\n\t\t}\n\t}\n\tfor (i = 0; i < NTCACHES; i++) {\n\t\tdallocx(qs[i], MALLOCX_TCACHE(tis[i]));\n\t}\n\n\t/* Flush some non-empty tcaches. */\n\tfor (i = 0; i < NTCACHES/2; i++) {\n\t\tassert_d_eq(mallctl(\"tcache.flush\", NULL, NULL, (void *)&tis[i],\n\t\t    sizeof(unsigned)), 0, \"Unexpected mallctl() failure, i=%u\",\n\t\t    i);\n\t}\n\n\t/* Destroy tcaches. */\n\tfor (i = 0; i < NTCACHES; i++) {\n\t\tassert_d_eq(mallctl(\"tcache.destroy\", NULL, NULL,\n\t\t    (void *)&tis[i], sizeof(unsigned)), 0,\n\t\t    \"Unexpected mallctl() failure, i=%u\", i);\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_thread_arena) {\n\tunsigned old_arena_ind, new_arena_ind, narenas;\n\n\tconst char *opa;\n\tsize_t sz = sizeof(opa);\n\tassert_d_eq(mallctl(\"opt.percpu_arena\", (void *)&opa, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tsz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.narenas\", (void *)&narenas, &sz, NULL, 0),\n\t    0, \"Unexpected mallctl() failure\");\n\tif (opt_oversize_threshold != 0) {\n\t\tnarenas--;\n\t}\n\tassert_u_eq(narenas, opt_narenas, \"Number of arenas incorrect\");\n\n\tif (strcmp(opa, \"disabled\") == 0) {\n\t\tnew_arena_ind = narenas - 1;\n\t\tassert_d_eq(mallctl(\"thread.arena\", (void *)&old_arena_ind, &sz,\n\t\t    (void *)&new_arena_ind, sizeof(unsigned)), 0,\n\t\t    \"Unexpected mallctl() failure\");\n\t\tnew_arena_ind = 0;\n\t\tassert_d_eq(mallctl(\"thread.arena\", (void *)&old_arena_ind, &sz,\n\t\t    (void *)&new_arena_ind, sizeof(unsigned)), 0,\n\t\t    \"Unexpected mallctl() failure\");\n\t} else {\n\t\tassert_d_eq(mallctl(\"thread.arena\", (void *)&old_arena_ind, &sz,\n\t\t    NULL, 0), 0, \"Unexpected mallctl() failure\");\n\t\tnew_arena_ind = percpu_arena_ind_limit(opt_percpu_arena) - 1;\n\t\tif (old_arena_ind != new_arena_ind) {\n\t\t\tassert_d_eq(mallctl(\"thread.arena\",\n\t\t\t    (void *)&old_arena_ind, &sz, (void *)&new_arena_ind,\n\t\t\t    sizeof(unsigned)), EPERM, \"thread.arena ctl \"\n\t\t\t    \"should not be allowed with percpu arena\");\n\t\t}\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_arena_i_initialized) {\n\tunsigned narenas, i;\n\tsize_t sz;\n\tsize_t mib[3];\n\tsize_t miblen = sizeof(mib) / sizeof(size_t);\n\tbool initialized;\n\n\tsz = sizeof(narenas);\n\tassert_d_eq(mallctl(\"arenas.narenas\", (void *)&narenas, &sz, NULL, 0),\n\t    0, \"Unexpected mallctl() failure\");\n\n\tassert_d_eq(mallctlnametomib(\"arena.0.initialized\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tfor (i = 0; i < narenas; i++) {\n\t\tmib[1] = i;\n\t\tsz = sizeof(initialized);\n\t\tassert_d_eq(mallctlbymib(mib, miblen, &initialized, &sz, NULL,\n\t\t    0), 0, \"Unexpected mallctl() failure\");\n\t}\n\n\tmib[1] = MALLCTL_ARENAS_ALL;\n\tsz = sizeof(initialized);\n\tassert_d_eq(mallctlbymib(mib, miblen, &initialized, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\tassert_true(initialized,\n\t    \"Merged arena statistics should always be initialized\");\n\n\t/* Equivalent to the above but using mallctl() directly. */\n\tsz = sizeof(initialized);\n\tassert_d_eq(mallctl(\n\t    \"arena.\" STRINGIFY(MALLCTL_ARENAS_ALL) \".initialized\",\n\t    (void *)&initialized, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\tassert_true(initialized,\n\t    \"Merged arena statistics should always be initialized\");\n}\nTEST_END\n\nTEST_BEGIN(test_arena_i_dirty_decay_ms) {\n\tssize_t dirty_decay_ms, orig_dirty_decay_ms, prev_dirty_decay_ms;\n\tsize_t sz = sizeof(ssize_t);\n\n\tassert_d_eq(mallctl(\"arena.0.dirty_decay_ms\",\n\t    (void *)&orig_dirty_decay_ms, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tdirty_decay_ms = -2;\n\tassert_d_eq(mallctl(\"arena.0.dirty_decay_ms\", NULL, NULL,\n\t    (void *)&dirty_decay_ms, sizeof(ssize_t)), EFAULT,\n\t    \"Unexpected mallctl() success\");\n\n\tdirty_decay_ms = 0x7fffffff;\n\tassert_d_eq(mallctl(\"arena.0.dirty_decay_ms\", NULL, NULL,\n\t    (void *)&dirty_decay_ms, sizeof(ssize_t)), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tfor (prev_dirty_decay_ms = dirty_decay_ms, dirty_decay_ms = -1;\n\t    dirty_decay_ms < 20; prev_dirty_decay_ms = dirty_decay_ms,\n\t    dirty_decay_ms++) {\n\t\tssize_t old_dirty_decay_ms;\n\n\t\tassert_d_eq(mallctl(\"arena.0.dirty_decay_ms\",\n\t\t    (void *)&old_dirty_decay_ms, &sz, (void *)&dirty_decay_ms,\n\t\t    sizeof(ssize_t)), 0, \"Unexpected mallctl() failure\");\n\t\tassert_zd_eq(old_dirty_decay_ms, prev_dirty_decay_ms,\n\t\t    \"Unexpected old arena.0.dirty_decay_ms\");\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_arena_i_muzzy_decay_ms) {\n\tssize_t muzzy_decay_ms, orig_muzzy_decay_ms, prev_muzzy_decay_ms;\n\tsize_t sz = sizeof(ssize_t);\n\n\tassert_d_eq(mallctl(\"arena.0.muzzy_decay_ms\",\n\t    (void *)&orig_muzzy_decay_ms, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tmuzzy_decay_ms = -2;\n\tassert_d_eq(mallctl(\"arena.0.muzzy_decay_ms\", NULL, NULL,\n\t    (void *)&muzzy_decay_ms, sizeof(ssize_t)), EFAULT,\n\t    \"Unexpected mallctl() success\");\n\n\tmuzzy_decay_ms = 0x7fffffff;\n\tassert_d_eq(mallctl(\"arena.0.muzzy_decay_ms\", NULL, NULL,\n\t    (void *)&muzzy_decay_ms, sizeof(ssize_t)), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tfor (prev_muzzy_decay_ms = muzzy_decay_ms, muzzy_decay_ms = -1;\n\t    muzzy_decay_ms < 20; prev_muzzy_decay_ms = muzzy_decay_ms,\n\t    muzzy_decay_ms++) {\n\t\tssize_t old_muzzy_decay_ms;\n\n\t\tassert_d_eq(mallctl(\"arena.0.muzzy_decay_ms\",\n\t\t    (void *)&old_muzzy_decay_ms, &sz, (void *)&muzzy_decay_ms,\n\t\t    sizeof(ssize_t)), 0, \"Unexpected mallctl() failure\");\n\t\tassert_zd_eq(old_muzzy_decay_ms, prev_muzzy_decay_ms,\n\t\t    \"Unexpected old arena.0.muzzy_decay_ms\");\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_arena_i_purge) {\n\tunsigned narenas;\n\tsize_t sz = sizeof(unsigned);\n\tsize_t mib[3];\n\tsize_t miblen = 3;\n\n\tassert_d_eq(mallctl(\"arena.0.purge\", NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tassert_d_eq(mallctl(\"arenas.narenas\", (void *)&narenas, &sz, NULL, 0),\n\t    0, \"Unexpected mallctl() failure\");\n\tassert_d_eq(mallctlnametomib(\"arena.0.purge\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tmib[1] = narenas;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n\n\tmib[1] = MALLCTL_ARENAS_ALL;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n}\nTEST_END\n\nTEST_BEGIN(test_arena_i_decay) {\n\tunsigned narenas;\n\tsize_t sz = sizeof(unsigned);\n\tsize_t mib[3];\n\tsize_t miblen = 3;\n\n\tassert_d_eq(mallctl(\"arena.0.decay\", NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tassert_d_eq(mallctl(\"arenas.narenas\", (void *)&narenas, &sz, NULL, 0),\n\t    0, \"Unexpected mallctl() failure\");\n\tassert_d_eq(mallctlnametomib(\"arena.0.decay\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tmib[1] = narenas;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n\n\tmib[1] = MALLCTL_ARENAS_ALL;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n}\nTEST_END\n\nTEST_BEGIN(test_arena_i_dss) {\n\tconst char *dss_prec_old, *dss_prec_new;\n\tsize_t sz = sizeof(dss_prec_old);\n\tsize_t mib[3];\n\tsize_t miblen;\n\n\tmiblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"arena.0.dss\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() error\");\n\n\tdss_prec_new = \"disabled\";\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&dss_prec_old, &sz,\n\t    (void *)&dss_prec_new, sizeof(dss_prec_new)), 0,\n\t    \"Unexpected mallctl() failure\");\n\tassert_str_ne(dss_prec_old, \"primary\",\n\t    \"Unexpected default for dss precedence\");\n\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&dss_prec_new, &sz,\n\t    (void *)&dss_prec_old, sizeof(dss_prec_old)), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&dss_prec_old, &sz, NULL,\n\t    0), 0, \"Unexpected mallctl() failure\");\n\tassert_str_ne(dss_prec_old, \"primary\",\n\t    \"Unexpected value for dss precedence\");\n\n\tmib[1] = narenas_total_get();\n\tdss_prec_new = \"disabled\";\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&dss_prec_old, &sz,\n\t    (void *)&dss_prec_new, sizeof(dss_prec_new)), 0,\n\t    \"Unexpected mallctl() failure\");\n\tassert_str_ne(dss_prec_old, \"primary\",\n\t    \"Unexpected default for dss precedence\");\n\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&dss_prec_new, &sz,\n\t    (void *)&dss_prec_old, sizeof(dss_prec_new)), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&dss_prec_old, &sz, NULL,\n\t    0), 0, \"Unexpected mallctl() failure\");\n\tassert_str_ne(dss_prec_old, \"primary\",\n\t    \"Unexpected value for dss precedence\");\n}\nTEST_END\n\nTEST_BEGIN(test_arena_i_retain_grow_limit) {\n\tsize_t old_limit, new_limit, default_limit;\n\tsize_t mib[3];\n\tsize_t miblen;\n\n\tbool retain_enabled;\n\tsize_t sz = sizeof(retain_enabled);\n\tassert_d_eq(mallctl(\"opt.retain\", &retain_enabled, &sz, NULL, 0),\n\t    0, \"Unexpected mallctl() failure\");\n\ttest_skip_if(!retain_enabled);\n\n\tsz = sizeof(default_limit);\n\tmiblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"arena.0.retain_grow_limit\", mib, &miblen),\n\t    0, \"Unexpected mallctlnametomib() error\");\n\n\tassert_d_eq(mallctlbymib(mib, miblen, &default_limit, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\tassert_zu_eq(default_limit, SC_LARGE_MAXCLASS,\n\t    \"Unexpected default for retain_grow_limit\");\n\n\tnew_limit = PAGE - 1;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, &new_limit,\n\t    sizeof(new_limit)), EFAULT, \"Unexpected mallctl() success\");\n\n\tnew_limit = PAGE + 1;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, &new_limit,\n\t    sizeof(new_limit)), 0, \"Unexpected mallctl() failure\");\n\tassert_d_eq(mallctlbymib(mib, miblen, &old_limit, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\tassert_zu_eq(old_limit, PAGE,\n\t    \"Unexpected value for retain_grow_limit\");\n\n\t/* Expect grow less than psize class 10. */\n\tnew_limit = sz_pind2sz(10) - 1;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, &new_limit,\n\t    sizeof(new_limit)), 0, \"Unexpected mallctl() failure\");\n\tassert_d_eq(mallctlbymib(mib, miblen, &old_limit, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\tassert_zu_eq(old_limit, sz_pind2sz(9),\n\t    \"Unexpected value for retain_grow_limit\");\n\n\t/* Restore to default. */\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, &default_limit,\n\t    sizeof(default_limit)), 0, \"Unexpected mallctl() failure\");\n}\nTEST_END\n\nTEST_BEGIN(test_arenas_dirty_decay_ms) {\n\tssize_t dirty_decay_ms, orig_dirty_decay_ms, prev_dirty_decay_ms;\n\tsize_t sz = sizeof(ssize_t);\n\n\tassert_d_eq(mallctl(\"arenas.dirty_decay_ms\",\n\t    (void *)&orig_dirty_decay_ms, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tdirty_decay_ms = -2;\n\tassert_d_eq(mallctl(\"arenas.dirty_decay_ms\", NULL, NULL,\n\t    (void *)&dirty_decay_ms, sizeof(ssize_t)), EFAULT,\n\t    \"Unexpected mallctl() success\");\n\n\tdirty_decay_ms = 0x7fffffff;\n\tassert_d_eq(mallctl(\"arenas.dirty_decay_ms\", NULL, NULL,\n\t    (void *)&dirty_decay_ms, sizeof(ssize_t)), 0,\n\t    \"Expected mallctl() failure\");\n\n\tfor (prev_dirty_decay_ms = dirty_decay_ms, dirty_decay_ms = -1;\n\t    dirty_decay_ms < 20; prev_dirty_decay_ms = dirty_decay_ms,\n\t    dirty_decay_ms++) {\n\t\tssize_t old_dirty_decay_ms;\n\n\t\tassert_d_eq(mallctl(\"arenas.dirty_decay_ms\",\n\t\t    (void *)&old_dirty_decay_ms, &sz, (void *)&dirty_decay_ms,\n\t\t    sizeof(ssize_t)), 0, \"Unexpected mallctl() failure\");\n\t\tassert_zd_eq(old_dirty_decay_ms, prev_dirty_decay_ms,\n\t\t    \"Unexpected old arenas.dirty_decay_ms\");\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_arenas_muzzy_decay_ms) {\n\tssize_t muzzy_decay_ms, orig_muzzy_decay_ms, prev_muzzy_decay_ms;\n\tsize_t sz = sizeof(ssize_t);\n\n\tassert_d_eq(mallctl(\"arenas.muzzy_decay_ms\",\n\t    (void *)&orig_muzzy_decay_ms, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tmuzzy_decay_ms = -2;\n\tassert_d_eq(mallctl(\"arenas.muzzy_decay_ms\", NULL, NULL,\n\t    (void *)&muzzy_decay_ms, sizeof(ssize_t)), EFAULT,\n\t    \"Unexpected mallctl() success\");\n\n\tmuzzy_decay_ms = 0x7fffffff;\n\tassert_d_eq(mallctl(\"arenas.muzzy_decay_ms\", NULL, NULL,\n\t    (void *)&muzzy_decay_ms, sizeof(ssize_t)), 0,\n\t    \"Expected mallctl() failure\");\n\n\tfor (prev_muzzy_decay_ms = muzzy_decay_ms, muzzy_decay_ms = -1;\n\t    muzzy_decay_ms < 20; prev_muzzy_decay_ms = muzzy_decay_ms,\n\t    muzzy_decay_ms++) {\n\t\tssize_t old_muzzy_decay_ms;\n\n\t\tassert_d_eq(mallctl(\"arenas.muzzy_decay_ms\",\n\t\t    (void *)&old_muzzy_decay_ms, &sz, (void *)&muzzy_decay_ms,\n\t\t    sizeof(ssize_t)), 0, \"Unexpected mallctl() failure\");\n\t\tassert_zd_eq(old_muzzy_decay_ms, prev_muzzy_decay_ms,\n\t\t    \"Unexpected old arenas.muzzy_decay_ms\");\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_arenas_constants) {\n#define TEST_ARENAS_CONSTANT(t, name, expected) do {\t\t\t\\\n\tt name;\t\t\t\t\t\t\t\t\\\n\tsize_t sz = sizeof(t);\t\t\t\t\t\t\\\n\tassert_d_eq(mallctl(\"arenas.\"#name, (void *)&name, &sz, NULL,\t\\\n\t    0), 0, \"Unexpected mallctl() failure\");\t\t\t\\\n\tassert_zu_eq(name, expected, \"Incorrect \"#name\" size\");\t\t\\\n} while (0)\n\n\tTEST_ARENAS_CONSTANT(size_t, quantum, QUANTUM);\n\tTEST_ARENAS_CONSTANT(size_t, page, PAGE);\n\tTEST_ARENAS_CONSTANT(unsigned, nbins, SC_NBINS);\n\tTEST_ARENAS_CONSTANT(unsigned, nlextents, SC_NSIZES - SC_NBINS);\n\n#undef TEST_ARENAS_CONSTANT\n}\nTEST_END\n\nTEST_BEGIN(test_arenas_bin_constants) {\n#define TEST_ARENAS_BIN_CONSTANT(t, name, expected) do {\t\t\\\n\tt name;\t\t\t\t\t\t\t\t\\\n\tsize_t sz = sizeof(t);\t\t\t\t\t\t\\\n\tassert_d_eq(mallctl(\"arenas.bin.0.\"#name, (void *)&name, &sz,\t\\\n\t    NULL, 0), 0, \"Unexpected mallctl() failure\");\t\t\\\n\tassert_zu_eq(name, expected, \"Incorrect \"#name\" size\");\t\t\\\n} while (0)\n\n\tTEST_ARENAS_BIN_CONSTANT(size_t, size, bin_infos[0].reg_size);\n\tTEST_ARENAS_BIN_CONSTANT(uint32_t, nregs, bin_infos[0].nregs);\n\tTEST_ARENAS_BIN_CONSTANT(size_t, slab_size,\n\t    bin_infos[0].slab_size);\n\tTEST_ARENAS_BIN_CONSTANT(uint32_t, nshards, bin_infos[0].n_shards);\n\n#undef TEST_ARENAS_BIN_CONSTANT\n}\nTEST_END\n\nTEST_BEGIN(test_arenas_lextent_constants) {\n#define TEST_ARENAS_LEXTENT_CONSTANT(t, name, expected) do {\t\t\\\n\tt name;\t\t\t\t\t\t\t\t\\\n\tsize_t sz = sizeof(t);\t\t\t\t\t\t\\\n\tassert_d_eq(mallctl(\"arenas.lextent.0.\"#name, (void *)&name,\t\\\n\t    &sz, NULL, 0), 0, \"Unexpected mallctl() failure\");\t\t\\\n\tassert_zu_eq(name, expected, \"Incorrect \"#name\" size\");\t\t\\\n} while (0)\n\n\tTEST_ARENAS_LEXTENT_CONSTANT(size_t, size,\n\t    SC_LARGE_MINCLASS);\n\n#undef TEST_ARENAS_LEXTENT_CONSTANT\n}\nTEST_END\n\nTEST_BEGIN(test_arenas_create) {\n\tunsigned narenas_before, arena, narenas_after;\n\tsize_t sz = sizeof(unsigned);\n\n\tassert_d_eq(mallctl(\"arenas.narenas\", (void *)&narenas_before, &sz,\n\t    NULL, 0), 0, \"Unexpected mallctl() failure\");\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\tassert_d_eq(mallctl(\"arenas.narenas\", (void *)&narenas_after, &sz, NULL,\n\t    0), 0, \"Unexpected mallctl() failure\");\n\n\tassert_u_eq(narenas_before+1, narenas_after,\n\t    \"Unexpected number of arenas before versus after extension\");\n\tassert_u_eq(arena, narenas_after-1, \"Unexpected arena index\");\n}\nTEST_END\n\nTEST_BEGIN(test_arenas_lookup) {\n\tunsigned arena, arena1;\n\tvoid *ptr;\n\tsize_t sz = sizeof(unsigned);\n\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\tptr = mallocx(42, MALLOCX_ARENA(arena) | MALLOCX_TCACHE_NONE);\n\tassert_ptr_not_null(ptr, \"Unexpected mallocx() failure\");\n\tassert_d_eq(mallctl(\"arenas.lookup\", &arena1, &sz, &ptr, sizeof(ptr)),\n\t    0, \"Unexpected mallctl() failure\");\n\tassert_u_eq(arena, arena1, \"Unexpected arena index\");\n\tdallocx(ptr, 0);\n}\nTEST_END\n\nTEST_BEGIN(test_stats_arenas) {\n#define TEST_STATS_ARENAS(t, name) do {\t\t\t\t\t\\\n\tt name;\t\t\t\t\t\t\t\t\\\n\tsize_t sz = sizeof(t);\t\t\t\t\t\t\\\n\tassert_d_eq(mallctl(\"stats.arenas.0.\"#name, (void *)&name, &sz,\t\\\n\t    NULL, 0), 0, \"Unexpected mallctl() failure\");\t\t\\\n} while (0)\n\n\tTEST_STATS_ARENAS(unsigned, nthreads);\n\tTEST_STATS_ARENAS(const char *, dss);\n\tTEST_STATS_ARENAS(ssize_t, dirty_decay_ms);\n\tTEST_STATS_ARENAS(ssize_t, muzzy_decay_ms);\n\tTEST_STATS_ARENAS(size_t, pactive);\n\tTEST_STATS_ARENAS(size_t, pdirty);\n\n#undef TEST_STATS_ARENAS\n}\nTEST_END\n\nstatic void\nalloc_hook(void *extra, UNUSED hook_alloc_t type, UNUSED void *result,\n    UNUSED uintptr_t result_raw, UNUSED uintptr_t args_raw[3]) {\n\t*(bool *)extra = true;\n}\n\nstatic void\ndalloc_hook(void *extra, UNUSED hook_dalloc_t type,\n    UNUSED void *address, UNUSED uintptr_t args_raw[3]) {\n\t*(bool *)extra = true;\n}\n\nTEST_BEGIN(test_hooks) {\n\tbool hook_called = false;\n\thooks_t hooks = {&alloc_hook, &dalloc_hook, NULL, &hook_called};\n\tvoid *handle = NULL;\n\tsize_t sz = sizeof(handle);\n\tint err = mallctl(\"experimental.hooks.install\", &handle, &sz, &hooks,\n\t    sizeof(hooks));\n\tassert_d_eq(err, 0, \"Hook installation failed\");\n\tassert_ptr_ne(handle, NULL, \"Hook installation gave null handle\");\n\tvoid *ptr = mallocx(1, 0);\n\tassert_true(hook_called, \"Alloc hook not called\");\n\thook_called = false;\n\tfree(ptr);\n\tassert_true(hook_called, \"Free hook not called\");\n\n\terr = mallctl(\"experimental.hooks.remove\", NULL, NULL, &handle,\n\t    sizeof(handle));\n\tassert_d_eq(err, 0, \"Hook removal failed\");\n\thook_called = false;\n\tptr = mallocx(1, 0);\n\tfree(ptr);\n\tassert_false(hook_called, \"Hook called after removal\");\n}\nTEST_END\n\nTEST_BEGIN(test_hooks_exhaustion) {\n\tbool hook_called = false;\n\thooks_t hooks = {&alloc_hook, &dalloc_hook, NULL, &hook_called};\n\n\tvoid *handle;\n\tvoid *handles[HOOK_MAX];\n\tsize_t sz = sizeof(handle);\n\tint err;\n\tfor (int i = 0; i < HOOK_MAX; i++) {\n\t\thandle = NULL;\n\t\terr = mallctl(\"experimental.hooks.install\", &handle, &sz,\n\t\t    &hooks, sizeof(hooks));\n\t\tassert_d_eq(err, 0, \"Error installation hooks\");\n\t\tassert_ptr_ne(handle, NULL, \"Got NULL handle\");\n\t\thandles[i] = handle;\n\t}\n\terr = mallctl(\"experimental.hooks.install\", &handle, &sz, &hooks,\n\t    sizeof(hooks));\n\tassert_d_eq(err, EAGAIN, \"Should have failed hook installation\");\n\tfor (int i = 0; i < HOOK_MAX; i++) {\n\t\terr = mallctl(\"experimental.hooks.remove\", NULL, NULL,\n\t\t    &handles[i], sizeof(handles[i]));\n\t\tassert_d_eq(err, 0, \"Hook removal failed\");\n\t}\n\t/* Insertion failed, but then we removed some; it should work now. */\n\thandle = NULL;\n\terr = mallctl(\"experimental.hooks.install\", &handle, &sz, &hooks,\n\t    sizeof(hooks));\n\tassert_d_eq(err, 0, \"Hook insertion failed\");\n\tassert_ptr_ne(handle, NULL, \"Got NULL handle\");\n\terr = mallctl(\"experimental.hooks.remove\", NULL, NULL, &handle,\n\t    sizeof(handle));\n\tassert_d_eq(err, 0, \"Hook removal failed\");\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_mallctl_errors,\n\t    test_mallctlnametomib_errors,\n\t    test_mallctlbymib_errors,\n\t    test_mallctl_read_write,\n\t    test_mallctlnametomib_short_mib,\n\t    test_mallctl_config,\n\t    test_mallctl_opt,\n\t    test_manpage_example,\n\t    test_tcache_none,\n\t    test_tcache,\n\t    test_thread_arena,\n\t    test_arena_i_initialized,\n\t    test_arena_i_dirty_decay_ms,\n\t    test_arena_i_muzzy_decay_ms,\n\t    test_arena_i_purge,\n\t    test_arena_i_decay,\n\t    test_arena_i_dss,\n\t    test_arena_i_retain_grow_limit,\n\t    test_arenas_dirty_decay_ms,\n\t    test_arenas_muzzy_decay_ms,\n\t    test_arenas_constants,\n\t    test_arenas_bin_constants,\n\t    test_arenas_lextent_constants,\n\t    test_arenas_create,\n\t    test_arenas_lookup,\n\t    test_stats_arenas,\n\t    test_hooks,\n\t    test_hooks_exhaustion);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/malloc_io.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nTEST_BEGIN(test_malloc_strtoumax_no_endptr) {\n\tint err;\n\n\tset_errno(0);\n\tassert_ju_eq(malloc_strtoumax(\"0\", NULL, 0), 0, \"Unexpected result\");\n\terr = get_errno();\n\tassert_d_eq(err, 0, \"Unexpected failure\");\n}\nTEST_END\n\nTEST_BEGIN(test_malloc_strtoumax) {\n\tstruct test_s {\n\t\tconst char *input;\n\t\tconst char *expected_remainder;\n\t\tint base;\n\t\tint expected_errno;\n\t\tconst char *expected_errno_name;\n\t\tuintmax_t expected_x;\n\t};\n#define ERR(e)\t\te, #e\n#define KUMAX(x)\t((uintmax_t)x##ULL)\n#define KSMAX(x)\t((uintmax_t)(intmax_t)x##LL)\n\tstruct test_s tests[] = {\n\t\t{\"0\",\t\t\"0\",\t-1,\tERR(EINVAL),\tUINTMAX_MAX},\n\t\t{\"0\",\t\t\"0\",\t1,\tERR(EINVAL),\tUINTMAX_MAX},\n\t\t{\"0\",\t\t\"0\",\t37,\tERR(EINVAL),\tUINTMAX_MAX},\n\n\t\t{\"\",\t\t\"\",\t0,\tERR(EINVAL),\tUINTMAX_MAX},\n\t\t{\"+\",\t\t\"+\",\t0,\tERR(EINVAL),\tUINTMAX_MAX},\n\t\t{\"++3\",\t\t\"++3\",\t0,\tERR(EINVAL),\tUINTMAX_MAX},\n\t\t{\"-\",\t\t\"-\",\t0,\tERR(EINVAL),\tUINTMAX_MAX},\n\n\t\t{\"42\",\t\t\"\",\t0,\tERR(0),\t\tKUMAX(42)},\n\t\t{\"+42\",\t\t\"\",\t0,\tERR(0),\t\tKUMAX(42)},\n\t\t{\"-42\",\t\t\"\",\t0,\tERR(0),\t\tKSMAX(-42)},\n\t\t{\"042\",\t\t\"\",\t0,\tERR(0),\t\tKUMAX(042)},\n\t\t{\"+042\",\t\"\",\t0,\tERR(0),\t\tKUMAX(042)},\n\t\t{\"-042\",\t\"\",\t0,\tERR(0),\t\tKSMAX(-042)},\n\t\t{\"0x42\",\t\"\",\t0,\tERR(0),\t\tKUMAX(0x42)},\n\t\t{\"+0x42\",\t\"\",\t0,\tERR(0),\t\tKUMAX(0x42)},\n\t\t{\"-0x42\",\t\"\",\t0,\tERR(0),\t\tKSMAX(-0x42)},\n\n\t\t{\"0\",\t\t\"\",\t0,\tERR(0),\t\tKUMAX(0)},\n\t\t{\"1\",\t\t\"\",\t0,\tERR(0),\t\tKUMAX(1)},\n\n\t\t{\"42\",\t\t\"\",\t0,\tERR(0),\t\tKUMAX(42)},\n\t\t{\" 42\",\t\t\"\",\t0,\tERR(0),\t\tKUMAX(42)},\n\t\t{\"42 \",\t\t\" \",\t0,\tERR(0),\t\tKUMAX(42)},\n\t\t{\"0x\",\t\t\"x\",\t0,\tERR(0),\t\tKUMAX(0)},\n\t\t{\"42x\",\t\t\"x\",\t0,\tERR(0),\t\tKUMAX(42)},\n\n\t\t{\"07\",\t\t\"\",\t0,\tERR(0),\t\tKUMAX(7)},\n\t\t{\"010\",\t\t\"\",\t0,\tERR(0),\t\tKUMAX(8)},\n\t\t{\"08\",\t\t\"8\",\t0,\tERR(0),\t\tKUMAX(0)},\n\t\t{\"0_\",\t\t\"_\",\t0,\tERR(0),\t\tKUMAX(0)},\n\n\t\t{\"0x\",\t\t\"x\",\t0,\tERR(0),\t\tKUMAX(0)},\n\t\t{\"0X\",\t\t\"X\",\t0,\tERR(0),\t\tKUMAX(0)},\n\t\t{\"0xg\",\t\t\"xg\",\t0,\tERR(0),\t\tKUMAX(0)},\n\t\t{\"0XA\",\t\t\"\",\t0,\tERR(0),\t\tKUMAX(10)},\n\n\t\t{\"010\",\t\t\"\",\t10,\tERR(0),\t\tKUMAX(10)},\n\t\t{\"0x3\",\t\t\"x3\",\t10,\tERR(0),\t\tKUMAX(0)},\n\n\t\t{\"12\",\t\t\"2\",\t2,\tERR(0),\t\tKUMAX(1)},\n\t\t{\"78\",\t\t\"8\",\t8,\tERR(0),\t\tKUMAX(7)},\n\t\t{\"9a\",\t\t\"a\",\t10,\tERR(0),\t\tKUMAX(9)},\n\t\t{\"9A\",\t\t\"A\",\t10,\tERR(0),\t\tKUMAX(9)},\n\t\t{\"fg\",\t\t\"g\",\t16,\tERR(0),\t\tKUMAX(15)},\n\t\t{\"FG\",\t\t\"G\",\t16,\tERR(0),\t\tKUMAX(15)},\n\t\t{\"0xfg\",\t\"g\",\t16,\tERR(0),\t\tKUMAX(15)},\n\t\t{\"0XFG\",\t\"G\",\t16,\tERR(0),\t\tKUMAX(15)},\n\t\t{\"z_\",\t\t\"_\",\t36,\tERR(0),\t\tKUMAX(35)},\n\t\t{\"Z_\",\t\t\"_\",\t36,\tERR(0),\t\tKUMAX(35)}\n\t};\n#undef ERR\n#undef KUMAX\n#undef KSMAX\n\tunsigned i;\n\n\tfor (i = 0; i < sizeof(tests)/sizeof(struct test_s); i++) {\n\t\tstruct test_s *test = &tests[i];\n\t\tint err;\n\t\tuintmax_t result;\n\t\tchar *remainder;\n\n\t\tset_errno(0);\n\t\tresult = malloc_strtoumax(test->input, &remainder, test->base);\n\t\terr = get_errno();\n\t\tassert_d_eq(err, test->expected_errno,\n\t\t    \"Expected errno %s for \\\"%s\\\", base %d\",\n\t\t    test->expected_errno_name, test->input, test->base);\n\t\tassert_str_eq(remainder, test->expected_remainder,\n\t\t    \"Unexpected remainder for \\\"%s\\\", base %d\",\n\t\t    test->input, test->base);\n\t\tif (err == 0) {\n\t\t\tassert_ju_eq(result, test->expected_x,\n\t\t\t    \"Unexpected result for \\\"%s\\\", base %d\",\n\t\t\t    test->input, test->base);\n\t\t}\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_malloc_snprintf_truncated) {\n#define BUFLEN\t15\n\tchar buf[BUFLEN];\n\tsize_t result;\n\tsize_t len;\n#define TEST(expected_str_untruncated, ...) do {\t\t\t\\\n\tresult = malloc_snprintf(buf, len, __VA_ARGS__);\t\t\\\n\tassert_d_eq(strncmp(buf, expected_str_untruncated, len-1), 0,\t\\\n\t    \"Unexpected string inequality (\\\"%s\\\" vs \\\"%s\\\")\",\t\t\\\n\t    buf, expected_str_untruncated);\t\t\t\t\\\n\tassert_zu_eq(result, strlen(expected_str_untruncated),\t\t\\\n\t    \"Unexpected result\");\t\t\t\t\t\\\n} while (0)\n\n\tfor (len = 1; len < BUFLEN; len++) {\n\t\tTEST(\"012346789\",\t\"012346789\");\n\t\tTEST(\"a0123b\",\t\t\"a%sb\", \"0123\");\n\t\tTEST(\"a01234567\",\t\"a%s%s\", \"0123\", \"4567\");\n\t\tTEST(\"a0123  \",\t\t\"a%-6s\", \"0123\");\n\t\tTEST(\"a  0123\",\t\t\"a%6s\", \"0123\");\n\t\tTEST(\"a   012\",\t\t\"a%6.3s\", \"0123\");\n\t\tTEST(\"a   012\",\t\t\"a%*.*s\", 6, 3, \"0123\");\n\t\tTEST(\"a 123b\",\t\t\"a% db\", 123);\n\t\tTEST(\"a123b\",\t\t\"a%-db\", 123);\n\t\tTEST(\"a-123b\",\t\t\"a%-db\", -123);\n\t\tTEST(\"a+123b\",\t\t\"a%+db\", 123);\n\t}\n#undef BUFLEN\n#undef TEST\n}\nTEST_END\n\nTEST_BEGIN(test_malloc_snprintf) {\n#define BUFLEN\t128\n\tchar buf[BUFLEN];\n\tsize_t result;\n#define TEST(expected_str, ...) do {\t\t\t\t\t\\\n\tresult = malloc_snprintf(buf, sizeof(buf), __VA_ARGS__);\t\\\n\tassert_str_eq(buf, expected_str, \"Unexpected output\");\t\t\\\n\tassert_zu_eq(result, strlen(expected_str), \"Unexpected result\");\\\n} while (0)\n\n\tTEST(\"hello\", \"hello\");\n\n\tTEST(\"50%, 100%\", \"50%%, %d%%\", 100);\n\n\tTEST(\"a0123b\", \"a%sb\", \"0123\");\n\n\tTEST(\"a 0123b\", \"a%5sb\", \"0123\");\n\tTEST(\"a 0123b\", \"a%*sb\", 5, \"0123\");\n\n\tTEST(\"a0123 b\", \"a%-5sb\", \"0123\");\n\tTEST(\"a0123b\", \"a%*sb\", -1, \"0123\");\n\tTEST(\"a0123 b\", \"a%*sb\", -5, \"0123\");\n\tTEST(\"a0123 b\", \"a%-*sb\", -5, \"0123\");\n\n\tTEST(\"a012b\", \"a%.3sb\", \"0123\");\n\tTEST(\"a012b\", \"a%.*sb\", 3, \"0123\");\n\tTEST(\"a0123b\", \"a%.*sb\", -3, \"0123\");\n\n\tTEST(\"a  012b\", \"a%5.3sb\", \"0123\");\n\tTEST(\"a  012b\", \"a%5.*sb\", 3, \"0123\");\n\tTEST(\"a  012b\", \"a%*.3sb\", 5, \"0123\");\n\tTEST(\"a  012b\", \"a%*.*sb\", 5, 3, \"0123\");\n\tTEST(\"a 0123b\", \"a%*.*sb\", 5, -3, \"0123\");\n\n\tTEST(\"_abcd_\", \"_%x_\", 0xabcd);\n\tTEST(\"_0xabcd_\", \"_%#x_\", 0xabcd);\n\tTEST(\"_1234_\", \"_%o_\", 01234);\n\tTEST(\"_01234_\", \"_%#o_\", 01234);\n\tTEST(\"_1234_\", \"_%u_\", 1234);\n\n\tTEST(\"_1234_\", \"_%d_\", 1234);\n\tTEST(\"_ 1234_\", \"_% d_\", 1234);\n\tTEST(\"_+1234_\", \"_%+d_\", 1234);\n\tTEST(\"_-1234_\", \"_%d_\", -1234);\n\tTEST(\"_-1234_\", \"_% d_\", -1234);\n\tTEST(\"_-1234_\", \"_%+d_\", -1234);\n\n\tTEST(\"_-1234_\", \"_%d_\", -1234);\n\tTEST(\"_1234_\", \"_%d_\", 1234);\n\tTEST(\"_-1234_\", \"_%i_\", -1234);\n\tTEST(\"_1234_\", \"_%i_\", 1234);\n\tTEST(\"_01234_\", \"_%#o_\", 01234);\n\tTEST(\"_1234_\", \"_%u_\", 1234);\n\tTEST(\"_0x1234abc_\", \"_%#x_\", 0x1234abc);\n\tTEST(\"_0X1234ABC_\", \"_%#X_\", 0x1234abc);\n\tTEST(\"_c_\", \"_%c_\", 'c');\n\tTEST(\"_string_\", \"_%s_\", \"string\");\n\tTEST(\"_0x42_\", \"_%p_\", ((void *)0x42));\n\n\tTEST(\"_-1234_\", \"_%ld_\", ((long)-1234));\n\tTEST(\"_1234_\", \"_%ld_\", ((long)1234));\n\tTEST(\"_-1234_\", \"_%li_\", ((long)-1234));\n\tTEST(\"_1234_\", \"_%li_\", ((long)1234));\n\tTEST(\"_01234_\", \"_%#lo_\", ((long)01234));\n\tTEST(\"_1234_\", \"_%lu_\", ((long)1234));\n\tTEST(\"_0x1234abc_\", \"_%#lx_\", ((long)0x1234abc));\n\tTEST(\"_0X1234ABC_\", \"_%#lX_\", ((long)0x1234ABC));\n\n\tTEST(\"_-1234_\", \"_%lld_\", ((long long)-1234));\n\tTEST(\"_1234_\", \"_%lld_\", ((long long)1234));\n\tTEST(\"_-1234_\", \"_%lli_\", ((long long)-1234));\n\tTEST(\"_1234_\", \"_%lli_\", ((long long)1234));\n\tTEST(\"_01234_\", \"_%#llo_\", ((long long)01234));\n\tTEST(\"_1234_\", \"_%llu_\", ((long long)1234));\n\tTEST(\"_0x1234abc_\", \"_%#llx_\", ((long long)0x1234abc));\n\tTEST(\"_0X1234ABC_\", \"_%#llX_\", ((long long)0x1234ABC));\n\n\tTEST(\"_-1234_\", \"_%qd_\", ((long long)-1234));\n\tTEST(\"_1234_\", \"_%qd_\", ((long long)1234));\n\tTEST(\"_-1234_\", \"_%qi_\", ((long long)-1234));\n\tTEST(\"_1234_\", \"_%qi_\", ((long long)1234));\n\tTEST(\"_01234_\", \"_%#qo_\", ((long long)01234));\n\tTEST(\"_1234_\", \"_%qu_\", ((long long)1234));\n\tTEST(\"_0x1234abc_\", \"_%#qx_\", ((long long)0x1234abc));\n\tTEST(\"_0X1234ABC_\", \"_%#qX_\", ((long long)0x1234ABC));\n\n\tTEST(\"_-1234_\", \"_%jd_\", ((intmax_t)-1234));\n\tTEST(\"_1234_\", \"_%jd_\", ((intmax_t)1234));\n\tTEST(\"_-1234_\", \"_%ji_\", ((intmax_t)-1234));\n\tTEST(\"_1234_\", \"_%ji_\", ((intmax_t)1234));\n\tTEST(\"_01234_\", \"_%#jo_\", ((intmax_t)01234));\n\tTEST(\"_1234_\", \"_%ju_\", ((intmax_t)1234));\n\tTEST(\"_0x1234abc_\", \"_%#jx_\", ((intmax_t)0x1234abc));\n\tTEST(\"_0X1234ABC_\", \"_%#jX_\", ((intmax_t)0x1234ABC));\n\n\tTEST(\"_1234_\", \"_%td_\", ((ptrdiff_t)1234));\n\tTEST(\"_-1234_\", \"_%td_\", ((ptrdiff_t)-1234));\n\tTEST(\"_1234_\", \"_%ti_\", ((ptrdiff_t)1234));\n\tTEST(\"_-1234_\", \"_%ti_\", ((ptrdiff_t)-1234));\n\n\tTEST(\"_-1234_\", \"_%zd_\", ((ssize_t)-1234));\n\tTEST(\"_1234_\", \"_%zd_\", ((ssize_t)1234));\n\tTEST(\"_-1234_\", \"_%zi_\", ((ssize_t)-1234));\n\tTEST(\"_1234_\", \"_%zi_\", ((ssize_t)1234));\n\tTEST(\"_01234_\", \"_%#zo_\", ((ssize_t)01234));\n\tTEST(\"_1234_\", \"_%zu_\", ((ssize_t)1234));\n\tTEST(\"_0x1234abc_\", \"_%#zx_\", ((ssize_t)0x1234abc));\n\tTEST(\"_0X1234ABC_\", \"_%#zX_\", ((ssize_t)0x1234ABC));\n#undef BUFLEN\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_malloc_strtoumax_no_endptr,\n\t    test_malloc_strtoumax,\n\t    test_malloc_snprintf_truncated,\n\t    test_malloc_snprintf);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/math.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#define MAX_REL_ERR 1.0e-9\n#define MAX_ABS_ERR 1.0e-9\n\n#include <float.h>\n\n#ifdef __PGI\n#undef INFINITY\n#endif\n\n#ifndef INFINITY\n#define INFINITY (DBL_MAX + DBL_MAX)\n#endif\n\nstatic bool\ndouble_eq_rel(double a, double b, double max_rel_err, double max_abs_err) {\n\tdouble rel_err;\n\n\tif (fabs(a - b) < max_abs_err) {\n\t\treturn true;\n\t}\n\trel_err = (fabs(b) > fabs(a)) ? fabs((a-b)/b) : fabs((a-b)/a);\n\treturn (rel_err < max_rel_err);\n}\n\nstatic uint64_t\nfactorial(unsigned x) {\n\tuint64_t ret = 1;\n\tunsigned i;\n\n\tfor (i = 2; i <= x; i++) {\n\t\tret *= (uint64_t)i;\n\t}\n\n\treturn ret;\n}\n\nTEST_BEGIN(test_ln_gamma_factorial) {\n\tunsigned x;\n\n\t/* exp(ln_gamma(x)) == (x-1)! for integer x. */\n\tfor (x = 1; x <= 21; x++) {\n\t\tassert_true(double_eq_rel(exp(ln_gamma(x)),\n\t\t    (double)factorial(x-1), MAX_REL_ERR, MAX_ABS_ERR),\n\t\t    \"Incorrect factorial result for x=%u\", x);\n\t}\n}\nTEST_END\n\n/* Expected ln_gamma([0.0..100.0] increment=0.25). */\nstatic const double ln_gamma_misc_expected[] = {\n\tINFINITY,\n\t1.28802252469807743, 0.57236494292470008, 0.20328095143129538,\n\t0.00000000000000000, -0.09827183642181320, -0.12078223763524518,\n\t-0.08440112102048555, 0.00000000000000000, 0.12487171489239651,\n\t0.28468287047291918, 0.47521466691493719, 0.69314718055994529,\n\t0.93580193110872523, 1.20097360234707429, 1.48681557859341718,\n\t1.79175946922805496, 2.11445692745037128, 2.45373657084244234,\n\t2.80857141857573644, 3.17805383034794575, 3.56137591038669710,\n\t3.95781396761871651, 4.36671603662228680, 4.78749174278204581,\n\t5.21960398699022932, 5.66256205985714178, 6.11591589143154568,\n\t6.57925121201010121, 7.05218545073853953, 7.53436423675873268,\n\t8.02545839631598312, 8.52516136106541467, 9.03318691960512332,\n\t9.54926725730099690, 10.07315123968123949, 10.60460290274525086,\n\t11.14340011995171231, 11.68933342079726856, 12.24220494005076176,\n\t12.80182748008146909, 13.36802367147604720, 13.94062521940376342,\n\t14.51947222506051816, 15.10441257307551943, 15.69530137706046524,\n\t16.29200047656724237, 16.89437797963419285, 17.50230784587389010,\n\t18.11566950571089407, 18.73434751193644843, 19.35823122022435427,\n\t19.98721449566188468, 20.62119544270163018, 21.26007615624470048,\n\t21.90376249182879320, 22.55216385312342098, 23.20519299513386002,\n\t23.86276584168908954, 24.52480131594137802, 25.19122118273868338,\n\t25.86194990184851861, 26.53691449111561340, 27.21604439872720604,\n\t27.89927138384089389, 28.58652940490193828, 29.27775451504081516,\n\t29.97288476399884871, 30.67186010608067548, 31.37462231367769050,\n\t32.08111489594735843, 32.79128302226991565, 33.50507345013689076,\n\t34.22243445715505317, 34.94331577687681545, 35.66766853819134298,\n\t36.39544520803305261, 37.12659953718355865, 37.86108650896109395,\n\t38.59886229060776230, 39.33988418719949465, 40.08411059791735198,\n\t40.83150097453079752, 41.58201578195490100, 42.33561646075348506,\n\t43.09226539146988699, 43.85192586067515208, 44.61456202863158893,\n\t45.38013889847690052, 46.14862228684032885, 46.91997879580877395,\n\t47.69417578616628361, 48.47118135183522014, 49.25096429545256882,\n\t50.03349410501914463, 50.81874093156324790, 51.60667556776436982,\n\t52.39726942748592364, 53.19049452616926743, 53.98632346204390586,\n\t54.78472939811231157, 55.58568604486942633, 56.38916764371992940,\n\t57.19514895105859864, 58.00360522298051080, 58.81451220059079787,\n\t59.62784609588432261, 60.44358357816834371, 61.26170176100199427,\n\t62.08217818962842927, 62.90499082887649962, 63.73011805151035958,\n\t64.55753862700632340, 65.38723171073768015, 66.21917683354901385,\n\t67.05335389170279825, 67.88974313718154008, 68.72832516833013017,\n\t69.56908092082363737, 70.41199165894616385, 71.25703896716800045,\n\t72.10420474200799390, 72.95347118416940191, 73.80482079093779646,\n\t74.65823634883015814, 75.51370092648485866, 76.37119786778275454,\n\t77.23071078519033961, 78.09222355331530707, 78.95572030266725960,\n\t79.82118541361435859, 80.68860351052903468, 81.55795945611502873,\n\t82.42923834590904164, 83.30242550295004378, 84.17750647261028973,\n\t85.05446701758152983, 85.93329311301090456, 86.81397094178107920,\n\t87.69648688992882057, 88.58082754219766741, 89.46697967771913795,\n\t90.35493026581838194, 91.24466646193963015, 92.13617560368709292,\n\t93.02944520697742803, 93.92446296229978486, 94.82121673107967297,\n\t95.71969454214321615, 96.61988458827809723, 97.52177522288820910,\n\t98.42535495673848800, 99.33061245478741341, 100.23753653310367895,\n\t101.14611615586458981, 102.05634043243354370, 102.96819861451382394,\n\t103.88168009337621811, 104.79677439715833032, 105.71347118823287303,\n\t106.63176026064346047, 107.55163153760463501, 108.47307506906540198,\n\t109.39608102933323153, 110.32063971475740516, 111.24674154146920557,\n\t112.17437704317786995, 113.10353686902013237, 114.03421178146170689,\n\t114.96639265424990128, 115.90007047041454769, 116.83523632031698014,\n\t117.77188139974506953, 118.70999700805310795, 119.64957454634490830,\n\t120.59060551569974962, 121.53308151543865279, 122.47699424143097247,\n\t123.42233548443955726, 124.36909712850338394, 125.31727114935689826,\n\t126.26684961288492559, 127.21782467361175861, 128.17018857322420899,\n\t129.12393363912724453, 130.07905228303084755, 131.03553699956862033,\n\t131.99338036494577864, 132.95257503561629164, 133.91311374698926784,\n\t134.87498931216194364, 135.83819462068046846, 136.80272263732638294,\n\t137.76856640092901785, 138.73571902320256299, 139.70417368760718091,\n\t140.67392364823425055, 141.64496222871400732, 142.61728282114600574,\n\t143.59087888505104047, 144.56574394634486680, 145.54187159633210058,\n\t146.51925549072063859, 147.49788934865566148, 148.47776695177302031,\n\t149.45888214327129617, 150.44122882700193600, 151.42480096657754984,\n\t152.40959258449737490, 153.39559776128982094, 154.38281063467164245,\n\t155.37122539872302696, 156.36083630307879844, 157.35163765213474107,\n\t158.34362380426921391, 159.33678917107920370, 160.33112821663092973,\n\t161.32663545672428995, 162.32330545817117695, 163.32113283808695314,\n\t164.32011226319519892, 165.32023844914485267, 166.32150615984036790,\n\t167.32391020678358018, 168.32744544842768164, 169.33210678954270634,\n\t170.33788918059275375, 171.34478761712384198, 172.35279713916281707,\n\t173.36191283062726143, 174.37212981874515094, 175.38344327348534080,\n\t176.39584840699734514, 177.40934047306160437, 178.42391476654847793,\n\t179.43956662288721304, 180.45629141754378111, 181.47408456550741107,\n\t182.49294152078630304, 183.51285777591152737, 184.53382886144947861,\n\t185.55585034552262869, 186.57891783333786861, 187.60302696672312095,\n\t188.62817342367162610, 189.65435291789341932, 190.68156119837468054,\n\t191.70979404894376330, 192.73904728784492590, 193.76931676731820176,\n\t194.80059837318714244, 195.83288802445184729, 196.86618167288995096,\n\t197.90047530266301123, 198.93576492992946214, 199.97204660246373464,\n\t201.00931639928148797, 202.04757043027063901, 203.08680483582807597,\n\t204.12701578650228385, 205.16819948264117102, 206.21035215404597807,\n\t207.25347005962987623, 208.29754948708190909, 209.34258675253678916,\n\t210.38857820024875878, 211.43552020227099320, 212.48340915813977858,\n\t213.53224149456323744, 214.58201366511514152, 215.63272214993284592,\n\t216.68436345542014010, 217.73693411395422004, 218.79043068359703739,\n\t219.84484974781133815, 220.90018791517996988, 221.95644181913033322,\n\t223.01360811766215875, 224.07168349307951871, 225.13066465172661879,\n\t226.19054832372759734, 227.25133126272962159, 228.31301024565024704,\n\t229.37558207242807384, 230.43904356577689896, 231.50339157094342113,\n\t232.56862295546847008, 233.63473460895144740, 234.70172344281823484,\n\t235.76958639009222907, 236.83832040516844586, 237.90792246359117712,\n\t238.97838956183431947, 240.04971871708477238, 241.12190696702904802,\n\t242.19495136964280846, 243.26884900298270509, 244.34359696498191283,\n\t245.41919237324782443, 246.49563236486270057, 247.57291409618682110,\n\t248.65103474266476269, 249.72999149863338175, 250.80978157713354904,\n\t251.89040220972316320, 252.97185064629374551, 254.05412415488834199,\n\t255.13722002152300661, 256.22113555000953511, 257.30586806178126835,\n\t258.39141489572085675, 259.47777340799029844, 260.56494097186322279,\n\t261.65291497755913497, 262.74169283208021852, 263.83127195904967266,\n\t264.92164979855277807, 266.01282380697938379, 267.10479145686849733,\n\t268.19755023675537586, 269.29109765101975427, 270.38543121973674488,\n\t271.48054847852881721, 272.57644697842033565, 273.67312428569374561,\n\t274.77057798174683967, 275.86880566295326389, 276.96780494052313770,\n\t278.06757344036617496, 279.16810880295668085, 280.26940868320008349,\n\t281.37147075030043197, 282.47429268763045229, 283.57787219260217171,\n\t284.68220697654078322, 285.78729476455760050, 286.89313329542699194,\n\t287.99972032146268930, 289.10705360839756395, 290.21513093526289140,\n\t291.32395009427028754, 292.43350889069523646, 293.54380514276073200,\n\t294.65483668152336350, 295.76660135076059532, 296.87909700685889902,\n\t297.99232151870342022, 299.10627276756946458, 300.22094864701409733,\n\t301.33634706277030091, 302.45246593264130297, 303.56930318639643929,\n\t304.68685676566872189, 305.80512462385280514, 306.92410472600477078,\n\t308.04379504874236773, 309.16419358014690033, 310.28529831966631036,\n\t311.40710727801865687, 312.52961847709792664, 313.65282994987899201,\n\t314.77673974032603610, 315.90134590329950015, 317.02664650446632777,\n\t318.15263962020929966, 319.27932333753892635, 320.40669575400545455,\n\t321.53475497761127144, 322.66349912672620803, 323.79292633000159185,\n\t324.92303472628691452, 326.05382246454587403, 327.18528770377525916,\n\t328.31742861292224234, 329.45024337080525356, 330.58373016603343331,\n\t331.71788719692847280, 332.85271267144611329, 333.98820480709991898,\n\t335.12436183088397001, 336.26118197919845443, 337.39866349777429377,\n\t338.53680464159958774, 339.67560367484657036, 340.81505887079896411,\n\t341.95516851178109619, 343.09593088908627578, 344.23734430290727460,\n\t345.37940706226686416, 346.52211748494903532, 347.66547389743118401,\n\t348.80947463481720661, 349.95411804077025408, 351.09940246744753267,\n\t352.24532627543504759, 353.39188783368263103, 354.53908551944078908,\n\t355.68691771819692349, 356.83538282361303118, 357.98447923746385868,\n\t359.13420536957539753\n};\n\nTEST_BEGIN(test_ln_gamma_misc) {\n\tunsigned i;\n\n\tfor (i = 1; i < sizeof(ln_gamma_misc_expected)/sizeof(double); i++) {\n\t\tdouble x = (double)i * 0.25;\n\t\tassert_true(double_eq_rel(ln_gamma(x),\n\t\t    ln_gamma_misc_expected[i], MAX_REL_ERR, MAX_ABS_ERR),\n\t\t    \"Incorrect ln_gamma result for i=%u\", i);\n\t}\n}\nTEST_END\n\n/* Expected pt_norm([0.01..0.99] increment=0.01). */\nstatic const double pt_norm_expected[] = {\n\t-INFINITY,\n\t-2.32634787404084076, -2.05374891063182252, -1.88079360815125085,\n\t-1.75068607125216946, -1.64485362695147264, -1.55477359459685305,\n\t-1.47579102817917063, -1.40507156030963221, -1.34075503369021654,\n\t-1.28155156554460081, -1.22652812003661049, -1.17498679206608991,\n\t-1.12639112903880045, -1.08031934081495606, -1.03643338949378938,\n\t-0.99445788320975281, -0.95416525314619416, -0.91536508784281390,\n\t-0.87789629505122846, -0.84162123357291418, -0.80642124701824025,\n\t-0.77219321418868492, -0.73884684918521371, -0.70630256284008752,\n\t-0.67448975019608171, -0.64334540539291685, -0.61281299101662701,\n\t-0.58284150727121620, -0.55338471955567281, -0.52440051270804067,\n\t-0.49585034734745320, -0.46769879911450812, -0.43991316567323380,\n\t-0.41246312944140462, -0.38532046640756751, -0.35845879325119373,\n\t-0.33185334643681652, -0.30548078809939738, -0.27931903444745404,\n\t-0.25334710313579978, -0.22754497664114931, -0.20189347914185077,\n\t-0.17637416478086135, -0.15096921549677725, -0.12566134685507399,\n\t-0.10043372051146975, -0.07526986209982976, -0.05015358346473352,\n\t-0.02506890825871106, 0.00000000000000000, 0.02506890825871106,\n\t0.05015358346473366, 0.07526986209982990, 0.10043372051146990,\n\t0.12566134685507413, 0.15096921549677739, 0.17637416478086146,\n\t0.20189347914185105, 0.22754497664114931, 0.25334710313579978,\n\t0.27931903444745404, 0.30548078809939738, 0.33185334643681652,\n\t0.35845879325119373, 0.38532046640756762, 0.41246312944140484,\n\t0.43991316567323391, 0.46769879911450835, 0.49585034734745348,\n\t0.52440051270804111, 0.55338471955567303, 0.58284150727121620,\n\t0.61281299101662701, 0.64334540539291685, 0.67448975019608171,\n\t0.70630256284008752, 0.73884684918521371, 0.77219321418868492,\n\t0.80642124701824036, 0.84162123357291441, 0.87789629505122879,\n\t0.91536508784281423, 0.95416525314619460, 0.99445788320975348,\n\t1.03643338949378938, 1.08031934081495606, 1.12639112903880045,\n\t1.17498679206608991, 1.22652812003661049, 1.28155156554460081,\n\t1.34075503369021654, 1.40507156030963265, 1.47579102817917085,\n\t1.55477359459685394, 1.64485362695147308, 1.75068607125217102,\n\t1.88079360815125041, 2.05374891063182208, 2.32634787404084076\n};\n\nTEST_BEGIN(test_pt_norm) {\n\tunsigned i;\n\n\tfor (i = 1; i < sizeof(pt_norm_expected)/sizeof(double); i++) {\n\t\tdouble p = (double)i * 0.01;\n\t\tassert_true(double_eq_rel(pt_norm(p), pt_norm_expected[i],\n\t\t    MAX_REL_ERR, MAX_ABS_ERR),\n\t\t    \"Incorrect pt_norm result for i=%u\", i);\n\t}\n}\nTEST_END\n\n/*\n * Expected pt_chi2(p=[0.01..0.99] increment=0.07,\n *                  df={0.1, 1.1, 10.1, 100.1, 1000.1}).\n */\nstatic const double pt_chi2_df[] = {0.1, 1.1, 10.1, 100.1, 1000.1};\nstatic const double pt_chi2_expected[] = {\n\t1.168926411457320e-40, 1.347680397072034e-22, 3.886980416666260e-17,\n\t8.245951724356564e-14, 2.068936347497604e-11, 1.562561743309233e-09,\n\t5.459543043426564e-08, 1.114775688149252e-06, 1.532101202364371e-05,\n\t1.553884683726585e-04, 1.239396954915939e-03, 8.153872320255721e-03,\n\t4.631183739647523e-02, 2.473187311701327e-01, 2.175254800183617e+00,\n\n\t0.0003729887888876379, 0.0164409238228929513, 0.0521523015190650113,\n\t0.1064701372271216612, 0.1800913735793082115, 0.2748704281195626931,\n\t0.3939246282787986497, 0.5420727552260817816, 0.7267265822221973259,\n\t0.9596554296000253670, 1.2607440376386165326, 1.6671185084541604304,\n\t2.2604828984738705167, 3.2868613342148607082, 6.9298574921692139839,\n\n\t2.606673548632508, 4.602913725294877, 5.646152813924212,\n\t6.488971315540869, 7.249823275816285, 7.977314231410841,\n\t8.700354939944047, 9.441728024225892, 10.224338321374127,\n\t11.076435368801061, 12.039320937038386, 13.183878752697167,\n\t14.657791935084575, 16.885728216339373, 23.361991680031817,\n\n\t70.14844087392152, 80.92379498849355, 85.53325420085891,\n\t88.94433120715347, 91.83732712857017, 94.46719943606301,\n\t96.96896479994635, 99.43412843510363, 101.94074719829733,\n\t104.57228644307247, 107.43900093448734, 110.71844673417287,\n\t114.76616819871325, 120.57422505959563, 135.92318818757556,\n\n\t899.0072447849649, 937.9271278858220, 953.8117189560207,\n\t965.3079371501154, 974.8974061207954, 983.4936235182347,\n\t991.5691170518946, 999.4334123954690, 1007.3391826856553,\n\t1015.5445154999951, 1024.3777075619569, 1034.3538789836223,\n\t1046.4872561869577, 1063.5717461999654, 1107.0741966053859\n};\n\nTEST_BEGIN(test_pt_chi2) {\n\tunsigned i, j;\n\tunsigned e = 0;\n\n\tfor (i = 0; i < sizeof(pt_chi2_df)/sizeof(double); i++) {\n\t\tdouble df = pt_chi2_df[i];\n\t\tdouble ln_gamma_df = ln_gamma(df * 0.5);\n\t\tfor (j = 1; j < 100; j += 7) {\n\t\t\tdouble p = (double)j * 0.01;\n\t\t\tassert_true(double_eq_rel(pt_chi2(p, df, ln_gamma_df),\n\t\t\t    pt_chi2_expected[e], MAX_REL_ERR, MAX_ABS_ERR),\n\t\t\t    \"Incorrect pt_chi2 result for i=%u, j=%u\", i, j);\n\t\t\te++;\n\t\t}\n\t}\n}\nTEST_END\n\n/*\n * Expected pt_gamma(p=[0.1..0.99] increment=0.07,\n *                   shape=[0.5..3.0] increment=0.5).\n */\nstatic const double pt_gamma_shape[] = {0.5, 1.0, 1.5, 2.0, 2.5, 3.0};\nstatic const double pt_gamma_expected[] = {\n\t7.854392895485103e-05, 5.043466107888016e-03, 1.788288957794883e-02,\n\t3.900956150232906e-02, 6.913847560638034e-02, 1.093710833465766e-01,\n\t1.613412523825817e-01, 2.274682115597864e-01, 3.114117323127083e-01,\n\t4.189466220207417e-01, 5.598106789059246e-01, 7.521856146202706e-01,\n\t1.036125427911119e+00, 1.532450860038180e+00, 3.317448300510606e+00,\n\n\t0.01005033585350144, 0.08338160893905107, 0.16251892949777497,\n\t0.24846135929849966, 0.34249030894677596, 0.44628710262841947,\n\t0.56211891815354142, 0.69314718055994529, 0.84397007029452920,\n\t1.02165124753198167, 1.23787435600161766, 1.51412773262977574,\n\t1.89711998488588196, 2.52572864430825783, 4.60517018598809091,\n\n\t0.05741590094955853, 0.24747378084860744, 0.39888572212236084,\n\t0.54394139997444901, 0.69048812513915159, 0.84311389861296104,\n\t1.00580622221479898, 1.18298694218766931, 1.38038096305861213,\n\t1.60627736383027453, 1.87396970522337947, 2.20749220408081070,\n\t2.65852391865854942, 3.37934630984842244, 5.67243336507218476,\n\n\t0.1485547402532659, 0.4657458011640391, 0.6832386130709406,\n\t0.8794297834672100, 1.0700752852474524, 1.2629614217350744,\n\t1.4638400448580779, 1.6783469900166610, 1.9132338090606940,\n\t2.1778589228618777, 2.4868823970010991, 2.8664695666264195,\n\t3.3724415436062114, 4.1682658512758071, 6.6383520679938108,\n\n\t0.2771490383641385, 0.7195001279643727, 0.9969081732265243,\n\t1.2383497880608061, 1.4675206597269927, 1.6953064251816552,\n\t1.9291243435606809, 2.1757300955477641, 2.4428032131216391,\n\t2.7406534569230616, 3.0851445039665513, 3.5043101122033367,\n\t4.0575997065264637, 4.9182956424675286, 7.5431362346944937,\n\n\t0.4360451650782932, 0.9983600902486267, 1.3306365880734528,\n\t1.6129750834753802, 1.8767241606994294, 2.1357032436097660,\n\t2.3988853336865565, 2.6740603137235603, 2.9697561737517959,\n\t3.2971457713883265, 3.6731795898504660, 4.1275751617770631,\n\t4.7230515633946677, 5.6417477865306020, 8.4059469148854635\n};\n\nTEST_BEGIN(test_pt_gamma_shape) {\n\tunsigned i, j;\n\tunsigned e = 0;\n\n\tfor (i = 0; i < sizeof(pt_gamma_shape)/sizeof(double); i++) {\n\t\tdouble shape = pt_gamma_shape[i];\n\t\tdouble ln_gamma_shape = ln_gamma(shape);\n\t\tfor (j = 1; j < 100; j += 7) {\n\t\t\tdouble p = (double)j * 0.01;\n\t\t\tassert_true(double_eq_rel(pt_gamma(p, shape, 1.0,\n\t\t\t    ln_gamma_shape), pt_gamma_expected[e], MAX_REL_ERR,\n\t\t\t    MAX_ABS_ERR),\n\t\t\t    \"Incorrect pt_gamma result for i=%u, j=%u\", i, j);\n\t\t\te++;\n\t\t}\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_pt_gamma_scale) {\n\tdouble shape = 1.0;\n\tdouble ln_gamma_shape = ln_gamma(shape);\n\n\tassert_true(double_eq_rel(\n\t    pt_gamma(0.5, shape, 1.0, ln_gamma_shape) * 10.0,\n\t    pt_gamma(0.5, shape, 10.0, ln_gamma_shape), MAX_REL_ERR,\n\t    MAX_ABS_ERR),\n\t    \"Scale should be trivially equivalent to external multiplication\");\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_ln_gamma_factorial,\n\t    test_ln_gamma_misc,\n\t    test_pt_norm,\n\t    test_pt_chi2,\n\t    test_pt_gamma_shape,\n\t    test_pt_gamma_scale);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/mq.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#define NSENDERS\t3\n#define NMSGS\t\t100000\n\ntypedef struct mq_msg_s mq_msg_t;\nstruct mq_msg_s {\n\tmq_msg(mq_msg_t)\tlink;\n};\nmq_gen(static, mq_, mq_t, mq_msg_t, link)\n\nTEST_BEGIN(test_mq_basic) {\n\tmq_t mq;\n\tmq_msg_t msg;\n\n\tassert_false(mq_init(&mq), \"Unexpected mq_init() failure\");\n\tassert_u_eq(mq_count(&mq), 0, \"mq should be empty\");\n\tassert_ptr_null(mq_tryget(&mq),\n\t    \"mq_tryget() should fail when the queue is empty\");\n\n\tmq_put(&mq, &msg);\n\tassert_u_eq(mq_count(&mq), 1, \"mq should contain one message\");\n\tassert_ptr_eq(mq_tryget(&mq), &msg, \"mq_tryget() should return msg\");\n\n\tmq_put(&mq, &msg);\n\tassert_ptr_eq(mq_get(&mq), &msg, \"mq_get() should return msg\");\n\n\tmq_fini(&mq);\n}\nTEST_END\n\nstatic void *\nthd_receiver_start(void *arg) {\n\tmq_t *mq = (mq_t *)arg;\n\tunsigned i;\n\n\tfor (i = 0; i < (NSENDERS * NMSGS); i++) {\n\t\tmq_msg_t *msg = mq_get(mq);\n\t\tassert_ptr_not_null(msg, \"mq_get() should never return NULL\");\n\t\tdallocx(msg, 0);\n\t}\n\treturn NULL;\n}\n\nstatic void *\nthd_sender_start(void *arg) {\n\tmq_t *mq = (mq_t *)arg;\n\tunsigned i;\n\n\tfor (i = 0; i < NMSGS; i++) {\n\t\tmq_msg_t *msg;\n\t\tvoid *p;\n\t\tp = mallocx(sizeof(mq_msg_t), 0);\n\t\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\t\tmsg = (mq_msg_t *)p;\n\t\tmq_put(mq, msg);\n\t}\n\treturn NULL;\n}\n\nTEST_BEGIN(test_mq_threaded) {\n\tmq_t mq;\n\tthd_t receiver;\n\tthd_t senders[NSENDERS];\n\tunsigned i;\n\n\tassert_false(mq_init(&mq), \"Unexpected mq_init() failure\");\n\n\tthd_create(&receiver, thd_receiver_start, (void *)&mq);\n\tfor (i = 0; i < NSENDERS; i++) {\n\t\tthd_create(&senders[i], thd_sender_start, (void *)&mq);\n\t}\n\n\tthd_join(receiver, NULL);\n\tfor (i = 0; i < NSENDERS; i++) {\n\t\tthd_join(senders[i], NULL);\n\t}\n\n\tmq_fini(&mq);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_mq_basic,\n\t    test_mq_threaded);\n}\n\n"
  },
  {
    "path": "deps/jemalloc/test/unit/mtx.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#define NTHREADS\t2\n#define NINCRS\t\t2000000\n\nTEST_BEGIN(test_mtx_basic) {\n\tmtx_t mtx;\n\n\tassert_false(mtx_init(&mtx), \"Unexpected mtx_init() failure\");\n\tmtx_lock(&mtx);\n\tmtx_unlock(&mtx);\n\tmtx_fini(&mtx);\n}\nTEST_END\n\ntypedef struct {\n\tmtx_t\t\tmtx;\n\tunsigned\tx;\n} thd_start_arg_t;\n\nstatic void *\nthd_start(void *varg) {\n\tthd_start_arg_t *arg = (thd_start_arg_t *)varg;\n\tunsigned i;\n\n\tfor (i = 0; i < NINCRS; i++) {\n\t\tmtx_lock(&arg->mtx);\n\t\targ->x++;\n\t\tmtx_unlock(&arg->mtx);\n\t}\n\treturn NULL;\n}\n\nTEST_BEGIN(test_mtx_race) {\n\tthd_start_arg_t arg;\n\tthd_t thds[NTHREADS];\n\tunsigned i;\n\n\tassert_false(mtx_init(&arg.mtx), \"Unexpected mtx_init() failure\");\n\targ.x = 0;\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tthd_create(&thds[i], thd_start, (void *)&arg);\n\t}\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tthd_join(thds[i], NULL);\n\t}\n\tassert_u_eq(arg.x, NTHREADS * NINCRS,\n\t    \"Race-related counter corruption\");\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_mtx_basic,\n\t    test_mtx_race);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/nstime.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#define BILLION\tUINT64_C(1000000000)\n\nTEST_BEGIN(test_nstime_init) {\n\tnstime_t nst;\n\n\tnstime_init(&nst, 42000000043);\n\tassert_u64_eq(nstime_ns(&nst), 42000000043, \"ns incorrectly read\");\n\tassert_u64_eq(nstime_sec(&nst), 42, \"sec incorrectly read\");\n\tassert_u64_eq(nstime_nsec(&nst), 43, \"nsec incorrectly read\");\n}\nTEST_END\n\nTEST_BEGIN(test_nstime_init2) {\n\tnstime_t nst;\n\n\tnstime_init2(&nst, 42, 43);\n\tassert_u64_eq(nstime_sec(&nst), 42, \"sec incorrectly read\");\n\tassert_u64_eq(nstime_nsec(&nst), 43, \"nsec incorrectly read\");\n}\nTEST_END\n\nTEST_BEGIN(test_nstime_copy) {\n\tnstime_t nsta, nstb;\n\n\tnstime_init2(&nsta, 42, 43);\n\tnstime_init(&nstb, 0);\n\tnstime_copy(&nstb, &nsta);\n\tassert_u64_eq(nstime_sec(&nstb), 42, \"sec incorrectly copied\");\n\tassert_u64_eq(nstime_nsec(&nstb), 43, \"nsec incorrectly copied\");\n}\nTEST_END\n\nTEST_BEGIN(test_nstime_compare) {\n\tnstime_t nsta, nstb;\n\n\tnstime_init2(&nsta, 42, 43);\n\tnstime_copy(&nstb, &nsta);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 0, \"Times should be equal\");\n\tassert_d_eq(nstime_compare(&nstb, &nsta), 0, \"Times should be equal\");\n\n\tnstime_init2(&nstb, 42, 42);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 1,\n\t    \"nsta should be greater than nstb\");\n\tassert_d_eq(nstime_compare(&nstb, &nsta), -1,\n\t    \"nstb should be less than nsta\");\n\n\tnstime_init2(&nstb, 42, 44);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), -1,\n\t    \"nsta should be less than nstb\");\n\tassert_d_eq(nstime_compare(&nstb, &nsta), 1,\n\t    \"nstb should be greater than nsta\");\n\n\tnstime_init2(&nstb, 41, BILLION - 1);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 1,\n\t    \"nsta should be greater than nstb\");\n\tassert_d_eq(nstime_compare(&nstb, &nsta), -1,\n\t    \"nstb should be less than nsta\");\n\n\tnstime_init2(&nstb, 43, 0);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), -1,\n\t    \"nsta should be less than nstb\");\n\tassert_d_eq(nstime_compare(&nstb, &nsta), 1,\n\t    \"nstb should be greater than nsta\");\n}\nTEST_END\n\nTEST_BEGIN(test_nstime_add) {\n\tnstime_t nsta, nstb;\n\n\tnstime_init2(&nsta, 42, 43);\n\tnstime_copy(&nstb, &nsta);\n\tnstime_add(&nsta, &nstb);\n\tnstime_init2(&nstb, 84, 86);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 0,\n\t    \"Incorrect addition result\");\n\n\tnstime_init2(&nsta, 42, BILLION - 1);\n\tnstime_copy(&nstb, &nsta);\n\tnstime_add(&nsta, &nstb);\n\tnstime_init2(&nstb, 85, BILLION - 2);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 0,\n\t    \"Incorrect addition result\");\n}\nTEST_END\n\nTEST_BEGIN(test_nstime_iadd) {\n\tnstime_t nsta, nstb;\n\n\tnstime_init2(&nsta, 42, BILLION - 1);\n\tnstime_iadd(&nsta, 1);\n\tnstime_init2(&nstb, 43, 0);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 0,\n\t    \"Incorrect addition result\");\n\n\tnstime_init2(&nsta, 42, 1);\n\tnstime_iadd(&nsta, BILLION + 1);\n\tnstime_init2(&nstb, 43, 2);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 0,\n\t    \"Incorrect addition result\");\n}\nTEST_END\n\nTEST_BEGIN(test_nstime_subtract) {\n\tnstime_t nsta, nstb;\n\n\tnstime_init2(&nsta, 42, 43);\n\tnstime_copy(&nstb, &nsta);\n\tnstime_subtract(&nsta, &nstb);\n\tnstime_init(&nstb, 0);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 0,\n\t    \"Incorrect subtraction result\");\n\n\tnstime_init2(&nsta, 42, 43);\n\tnstime_init2(&nstb, 41, 44);\n\tnstime_subtract(&nsta, &nstb);\n\tnstime_init2(&nstb, 0, BILLION - 1);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 0,\n\t    \"Incorrect subtraction result\");\n}\nTEST_END\n\nTEST_BEGIN(test_nstime_isubtract) {\n\tnstime_t nsta, nstb;\n\n\tnstime_init2(&nsta, 42, 43);\n\tnstime_isubtract(&nsta, 42*BILLION + 43);\n\tnstime_init(&nstb, 0);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 0,\n\t    \"Incorrect subtraction result\");\n\n\tnstime_init2(&nsta, 42, 43);\n\tnstime_isubtract(&nsta, 41*BILLION + 44);\n\tnstime_init2(&nstb, 0, BILLION - 1);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 0,\n\t    \"Incorrect subtraction result\");\n}\nTEST_END\n\nTEST_BEGIN(test_nstime_imultiply) {\n\tnstime_t nsta, nstb;\n\n\tnstime_init2(&nsta, 42, 43);\n\tnstime_imultiply(&nsta, 10);\n\tnstime_init2(&nstb, 420, 430);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 0,\n\t    \"Incorrect multiplication result\");\n\n\tnstime_init2(&nsta, 42, 666666666);\n\tnstime_imultiply(&nsta, 3);\n\tnstime_init2(&nstb, 127, 999999998);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 0,\n\t    \"Incorrect multiplication result\");\n}\nTEST_END\n\nTEST_BEGIN(test_nstime_idivide) {\n\tnstime_t nsta, nstb;\n\n\tnstime_init2(&nsta, 42, 43);\n\tnstime_copy(&nstb, &nsta);\n\tnstime_imultiply(&nsta, 10);\n\tnstime_idivide(&nsta, 10);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 0,\n\t    \"Incorrect division result\");\n\n\tnstime_init2(&nsta, 42, 666666666);\n\tnstime_copy(&nstb, &nsta);\n\tnstime_imultiply(&nsta, 3);\n\tnstime_idivide(&nsta, 3);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 0,\n\t    \"Incorrect division result\");\n}\nTEST_END\n\nTEST_BEGIN(test_nstime_divide) {\n\tnstime_t nsta, nstb, nstc;\n\n\tnstime_init2(&nsta, 42, 43);\n\tnstime_copy(&nstb, &nsta);\n\tnstime_imultiply(&nsta, 10);\n\tassert_u64_eq(nstime_divide(&nsta, &nstb), 10,\n\t    \"Incorrect division result\");\n\n\tnstime_init2(&nsta, 42, 43);\n\tnstime_copy(&nstb, &nsta);\n\tnstime_imultiply(&nsta, 10);\n\tnstime_init(&nstc, 1);\n\tnstime_add(&nsta, &nstc);\n\tassert_u64_eq(nstime_divide(&nsta, &nstb), 10,\n\t    \"Incorrect division result\");\n\n\tnstime_init2(&nsta, 42, 43);\n\tnstime_copy(&nstb, &nsta);\n\tnstime_imultiply(&nsta, 10);\n\tnstime_init(&nstc, 1);\n\tnstime_subtract(&nsta, &nstc);\n\tassert_u64_eq(nstime_divide(&nsta, &nstb), 9,\n\t    \"Incorrect division result\");\n}\nTEST_END\n\nTEST_BEGIN(test_nstime_monotonic) {\n\tnstime_monotonic();\n}\nTEST_END\n\nTEST_BEGIN(test_nstime_update) {\n\tnstime_t nst;\n\n\tnstime_init(&nst, 0);\n\n\tassert_false(nstime_update(&nst), \"Basic time update failed.\");\n\n\t/* Only Rip Van Winkle sleeps this long. */\n\t{\n\t\tnstime_t addend;\n\t\tnstime_init2(&addend, 631152000, 0);\n\t\tnstime_add(&nst, &addend);\n\t}\n\t{\n\t\tnstime_t nst0;\n\t\tnstime_copy(&nst0, &nst);\n\t\tassert_true(nstime_update(&nst),\n\t\t    \"Update should detect time roll-back.\");\n\t\tassert_d_eq(nstime_compare(&nst, &nst0), 0,\n\t\t    \"Time should not have been modified\");\n\t}\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_nstime_init,\n\t    test_nstime_init2,\n\t    test_nstime_copy,\n\t    test_nstime_compare,\n\t    test_nstime_add,\n\t    test_nstime_iadd,\n\t    test_nstime_subtract,\n\t    test_nstime_isubtract,\n\t    test_nstime_imultiply,\n\t    test_nstime_idivide,\n\t    test_nstime_divide,\n\t    test_nstime_monotonic,\n\t    test_nstime_update);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/pack.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n/*\n * Size class that is a divisor of the page size, ideally 4+ regions per run.\n */\n#if LG_PAGE <= 14\n#define SZ\t(ZU(1) << (LG_PAGE - 2))\n#else\n#define SZ\tZU(4096)\n#endif\n\n/*\n * Number of slabs to consume at high water mark.  Should be at least 2 so that\n * if mmap()ed memory grows downward, downward growth of mmap()ed memory is\n * tested.\n */\n#define NSLABS\t8\n\nstatic unsigned\nbinind_compute(void) {\n\tsize_t sz;\n\tunsigned nbins, i;\n\n\tsz = sizeof(nbins);\n\tassert_d_eq(mallctl(\"arenas.nbins\", (void *)&nbins, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl failure\");\n\n\tfor (i = 0; i < nbins; i++) {\n\t\tsize_t mib[4];\n\t\tsize_t miblen = sizeof(mib)/sizeof(size_t);\n\t\tsize_t size;\n\n\t\tassert_d_eq(mallctlnametomib(\"arenas.bin.0.size\", mib,\n\t\t    &miblen), 0, \"Unexpected mallctlnametomb failure\");\n\t\tmib[2] = (size_t)i;\n\n\t\tsz = sizeof(size);\n\t\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&size, &sz, NULL,\n\t\t    0), 0, \"Unexpected mallctlbymib failure\");\n\t\tif (size == SZ) {\n\t\t\treturn i;\n\t\t}\n\t}\n\n\ttest_fail(\"Unable to compute nregs_per_run\");\n\treturn 0;\n}\n\nstatic size_t\nnregs_per_run_compute(void) {\n\tuint32_t nregs;\n\tsize_t sz;\n\tunsigned binind = binind_compute();\n\tsize_t mib[4];\n\tsize_t miblen = sizeof(mib)/sizeof(size_t);\n\n\tassert_d_eq(mallctlnametomib(\"arenas.bin.0.nregs\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomb failure\");\n\tmib[2] = (size_t)binind;\n\tsz = sizeof(nregs);\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&nregs, &sz, NULL,\n\t    0), 0, \"Unexpected mallctlbymib failure\");\n\treturn nregs;\n}\n\nstatic unsigned\narenas_create_mallctl(void) {\n\tunsigned arena_ind;\n\tsize_t sz;\n\n\tsz = sizeof(arena_ind);\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena_ind, &sz, NULL, 0),\n\t    0, \"Error in arenas.create\");\n\n\treturn arena_ind;\n}\n\nstatic void\narena_reset_mallctl(unsigned arena_ind) {\n\tsize_t mib[3];\n\tsize_t miblen = sizeof(mib)/sizeof(size_t);\n\n\tassert_d_eq(mallctlnametomib(\"arena.0.reset\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tmib[1] = (size_t)arena_ind;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n}\n\nTEST_BEGIN(test_pack) {\n\tbool prof_enabled;\n\tsize_t sz = sizeof(prof_enabled);\n\tif (mallctl(\"opt.prof\", (void *)&prof_enabled, &sz, NULL, 0) == 0) {\n\t\ttest_skip_if(prof_enabled);\n\t}\n\n\tunsigned arena_ind = arenas_create_mallctl();\n\tsize_t nregs_per_run = nregs_per_run_compute();\n\tsize_t nregs = nregs_per_run * NSLABS;\n\tVARIABLE_ARRAY(void *, ptrs, nregs);\n\tsize_t i, j, offset;\n\n\t/* Fill matrix. */\n\tfor (i = offset = 0; i < NSLABS; i++) {\n\t\tfor (j = 0; j < nregs_per_run; j++) {\n\t\t\tvoid *p = mallocx(SZ, MALLOCX_ARENA(arena_ind) |\n\t\t\t    MALLOCX_TCACHE_NONE);\n\t\t\tassert_ptr_not_null(p,\n\t\t\t    \"Unexpected mallocx(%zu, MALLOCX_ARENA(%u) |\"\n\t\t\t    \" MALLOCX_TCACHE_NONE) failure, run=%zu, reg=%zu\",\n\t\t\t    SZ, arena_ind, i, j);\n\t\t\tptrs[(i * nregs_per_run) + j] = p;\n\t\t}\n\t}\n\n\t/*\n\t * Free all but one region of each run, but rotate which region is\n\t * preserved, so that subsequent allocations exercise the within-run\n\t * layout policy.\n\t */\n\toffset = 0;\n\tfor (i = offset = 0;\n\t    i < NSLABS;\n\t    i++, offset = (offset + 1) % nregs_per_run) {\n\t\tfor (j = 0; j < nregs_per_run; j++) {\n\t\t\tvoid *p = ptrs[(i * nregs_per_run) + j];\n\t\t\tif (offset == j) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tdallocx(p, MALLOCX_ARENA(arena_ind) |\n\t\t\t    MALLOCX_TCACHE_NONE);\n\t\t}\n\t}\n\n\t/*\n\t * Logically refill matrix, skipping preserved regions and verifying\n\t * that the matrix is unmodified.\n\t */\n\toffset = 0;\n\tfor (i = offset = 0;\n\t    i < NSLABS;\n\t    i++, offset = (offset + 1) % nregs_per_run) {\n\t\tfor (j = 0; j < nregs_per_run; j++) {\n\t\t\tvoid *p;\n\n\t\t\tif (offset == j) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tp = mallocx(SZ, MALLOCX_ARENA(arena_ind) |\n\t\t\t    MALLOCX_TCACHE_NONE);\n\t\t\tassert_ptr_eq(p, ptrs[(i * nregs_per_run) + j],\n\t\t\t    \"Unexpected refill discrepancy, run=%zu, reg=%zu\\n\",\n\t\t\t    i, j);\n\t\t}\n\t}\n\n\t/* Clean up. */\n\tarena_reset_mallctl(arena_ind);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_pack);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/pack.sh",
    "content": "#!/bin/sh\n\n# Immediately purge to minimize fragmentation.\nexport MALLOC_CONF=\"dirty_decay_ms:0,muzzy_decay_ms:0\"\n"
  },
  {
    "path": "deps/jemalloc/test/unit/pages.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nTEST_BEGIN(test_pages_huge) {\n\tsize_t alloc_size;\n\tbool commit;\n\tvoid *pages, *hugepage;\n\n\talloc_size = HUGEPAGE * 2 - PAGE;\n\tcommit = true;\n\tpages = pages_map(NULL, alloc_size, PAGE, &commit);\n\tassert_ptr_not_null(pages, \"Unexpected pages_map() error\");\n\n\tif (init_system_thp_mode == thp_mode_default) {\n\t    hugepage = (void *)(ALIGNMENT_CEILING((uintptr_t)pages, HUGEPAGE));\n\t    assert_b_ne(pages_huge(hugepage, HUGEPAGE), have_madvise_huge,\n\t        \"Unexpected pages_huge() result\");\n\t    assert_false(pages_nohuge(hugepage, HUGEPAGE),\n\t        \"Unexpected pages_nohuge() result\");\n\t}\n\n\tpages_unmap(pages, alloc_size);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_pages_huge);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/ph.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/ph.h\"\n\ntypedef struct node_s node_t;\n\nstruct node_s {\n#define NODE_MAGIC 0x9823af7e\n\tuint32_t magic;\n\tphn(node_t) link;\n\tuint64_t key;\n};\n\nstatic int\nnode_cmp(const node_t *a, const node_t *b) {\n\tint ret;\n\n\tret = (a->key > b->key) - (a->key < b->key);\n\tif (ret == 0) {\n\t\t/*\n\t\t * Duplicates are not allowed in the heap, so force an\n\t\t * arbitrary ordering for non-identical items with equal keys.\n\t\t */\n\t\tret = (((uintptr_t)a) > ((uintptr_t)b))\n\t\t    - (((uintptr_t)a) < ((uintptr_t)b));\n\t}\n\treturn ret;\n}\n\nstatic int\nnode_cmp_magic(const node_t *a, const node_t *b) {\n\n\tassert_u32_eq(a->magic, NODE_MAGIC, \"Bad magic\");\n\tassert_u32_eq(b->magic, NODE_MAGIC, \"Bad magic\");\n\n\treturn node_cmp(a, b);\n}\n\ntypedef ph(node_t) heap_t;\nph_gen(static, heap_, heap_t, node_t, link, node_cmp_magic);\n\nstatic void\nnode_print(const node_t *node, unsigned depth) {\n\tunsigned i;\n\tnode_t *leftmost_child, *sibling;\n\n\tfor (i = 0; i < depth; i++) {\n\t\tmalloc_printf(\"\\t\");\n\t}\n\tmalloc_printf(\"%2\"FMTu64\"\\n\", node->key);\n\n\tleftmost_child = phn_lchild_get(node_t, link, node);\n\tif (leftmost_child == NULL) {\n\t\treturn;\n\t}\n\tnode_print(leftmost_child, depth + 1);\n\n\tfor (sibling = phn_next_get(node_t, link, leftmost_child); sibling !=\n\t    NULL; sibling = phn_next_get(node_t, link, sibling)) {\n\t\tnode_print(sibling, depth + 1);\n\t}\n}\n\nstatic void\nheap_print(const heap_t *heap) {\n\tnode_t *auxelm;\n\n\tmalloc_printf(\"vvv heap %p vvv\\n\", heap);\n\tif (heap->ph_root == NULL) {\n\t\tgoto label_return;\n\t}\n\n\tnode_print(heap->ph_root, 0);\n\n\tfor (auxelm = phn_next_get(node_t, link, heap->ph_root); auxelm != NULL;\n\t    auxelm = phn_next_get(node_t, link, auxelm)) {\n\t\tassert_ptr_eq(phn_next_get(node_t, link, phn_prev_get(node_t,\n\t\t    link, auxelm)), auxelm,\n\t\t    \"auxelm's prev doesn't link to auxelm\");\n\t\tnode_print(auxelm, 0);\n\t}\n\nlabel_return:\n\tmalloc_printf(\"^^^ heap %p ^^^\\n\", heap);\n}\n\nstatic unsigned\nnode_validate(const node_t *node, const node_t *parent) {\n\tunsigned nnodes = 1;\n\tnode_t *leftmost_child, *sibling;\n\n\tif (parent != NULL) {\n\t\tassert_d_ge(node_cmp_magic(node, parent), 0,\n\t\t    \"Child is less than parent\");\n\t}\n\n\tleftmost_child = phn_lchild_get(node_t, link, node);\n\tif (leftmost_child == NULL) {\n\t\treturn nnodes;\n\t}\n\tassert_ptr_eq((void *)phn_prev_get(node_t, link, leftmost_child),\n\t    (void *)node, \"Leftmost child does not link to node\");\n\tnnodes += node_validate(leftmost_child, node);\n\n\tfor (sibling = phn_next_get(node_t, link, leftmost_child); sibling !=\n\t    NULL; sibling = phn_next_get(node_t, link, sibling)) {\n\t\tassert_ptr_eq(phn_next_get(node_t, link, phn_prev_get(node_t,\n\t\t    link, sibling)), sibling,\n\t\t    \"sibling's prev doesn't link to sibling\");\n\t\tnnodes += node_validate(sibling, node);\n\t}\n\treturn nnodes;\n}\n\nstatic unsigned\nheap_validate(const heap_t *heap) {\n\tunsigned nnodes = 0;\n\tnode_t *auxelm;\n\n\tif (heap->ph_root == NULL) {\n\t\tgoto label_return;\n\t}\n\n\tnnodes += node_validate(heap->ph_root, NULL);\n\n\tfor (auxelm = phn_next_get(node_t, link, heap->ph_root); auxelm != NULL;\n\t    auxelm = phn_next_get(node_t, link, auxelm)) {\n\t\tassert_ptr_eq(phn_next_get(node_t, link, phn_prev_get(node_t,\n\t\t    link, auxelm)), auxelm,\n\t\t    \"auxelm's prev doesn't link to auxelm\");\n\t\tnnodes += node_validate(auxelm, NULL);\n\t}\n\nlabel_return:\n\tif (false) {\n\t\theap_print(heap);\n\t}\n\treturn nnodes;\n}\n\nTEST_BEGIN(test_ph_empty) {\n\theap_t heap;\n\n\theap_new(&heap);\n\tassert_true(heap_empty(&heap), \"Heap should be empty\");\n\tassert_ptr_null(heap_first(&heap), \"Unexpected node\");\n\tassert_ptr_null(heap_any(&heap), \"Unexpected node\");\n}\nTEST_END\n\nstatic void\nnode_remove(heap_t *heap, node_t *node) {\n\theap_remove(heap, node);\n\n\tnode->magic = 0;\n}\n\nstatic node_t *\nnode_remove_first(heap_t *heap) {\n\tnode_t *node = heap_remove_first(heap);\n\tnode->magic = 0;\n\treturn node;\n}\n\nstatic node_t *\nnode_remove_any(heap_t *heap) {\n\tnode_t *node = heap_remove_any(heap);\n\tnode->magic = 0;\n\treturn node;\n}\n\nTEST_BEGIN(test_ph_random) {\n#define NNODES 25\n#define NBAGS 250\n#define SEED 42\n\tsfmt_t *sfmt;\n\tuint64_t bag[NNODES];\n\theap_t heap;\n\tnode_t nodes[NNODES];\n\tunsigned i, j, k;\n\n\tsfmt = init_gen_rand(SEED);\n\tfor (i = 0; i < NBAGS; i++) {\n\t\tswitch (i) {\n\t\tcase 0:\n\t\t\t/* Insert in order. */\n\t\t\tfor (j = 0; j < NNODES; j++) {\n\t\t\t\tbag[j] = j;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase 1:\n\t\t\t/* Insert in reverse order. */\n\t\t\tfor (j = 0; j < NNODES; j++) {\n\t\t\t\tbag[j] = NNODES - j - 1;\n\t\t\t}\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tfor (j = 0; j < NNODES; j++) {\n\t\t\t\tbag[j] = gen_rand64_range(sfmt, NNODES);\n\t\t\t}\n\t\t}\n\n\t\tfor (j = 1; j <= NNODES; j++) {\n\t\t\t/* Initialize heap and nodes. */\n\t\t\theap_new(&heap);\n\t\t\tassert_u_eq(heap_validate(&heap), 0,\n\t\t\t    \"Incorrect node count\");\n\t\t\tfor (k = 0; k < j; k++) {\n\t\t\t\tnodes[k].magic = NODE_MAGIC;\n\t\t\t\tnodes[k].key = bag[k];\n\t\t\t}\n\n\t\t\t/* Insert nodes. */\n\t\t\tfor (k = 0; k < j; k++) {\n\t\t\t\theap_insert(&heap, &nodes[k]);\n\t\t\t\tif (i % 13 == 12) {\n\t\t\t\t\tassert_ptr_not_null(heap_any(&heap),\n\t\t\t\t\t    \"Heap should not be empty\");\n\t\t\t\t\t/* Trigger merging. */\n\t\t\t\t\tassert_ptr_not_null(heap_first(&heap),\n\t\t\t\t\t    \"Heap should not be empty\");\n\t\t\t\t}\n\t\t\t\tassert_u_eq(heap_validate(&heap), k + 1,\n\t\t\t\t    \"Incorrect node count\");\n\t\t\t}\n\n\t\t\tassert_false(heap_empty(&heap),\n\t\t\t    \"Heap should not be empty\");\n\n\t\t\t/* Remove nodes. */\n\t\t\tswitch (i % 6) {\n\t\t\tcase 0:\n\t\t\t\tfor (k = 0; k < j; k++) {\n\t\t\t\t\tassert_u_eq(heap_validate(&heap), j - k,\n\t\t\t\t\t    \"Incorrect node count\");\n\t\t\t\t\tnode_remove(&heap, &nodes[k]);\n\t\t\t\t\tassert_u_eq(heap_validate(&heap), j - k\n\t\t\t\t\t    - 1, \"Incorrect node count\");\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase 1:\n\t\t\t\tfor (k = j; k > 0; k--) {\n\t\t\t\t\tnode_remove(&heap, &nodes[k-1]);\n\t\t\t\t\tassert_u_eq(heap_validate(&heap), k - 1,\n\t\t\t\t\t    \"Incorrect node count\");\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase 2: {\n\t\t\t\tnode_t *prev = NULL;\n\t\t\t\tfor (k = 0; k < j; k++) {\n\t\t\t\t\tnode_t *node = node_remove_first(&heap);\n\t\t\t\t\tassert_u_eq(heap_validate(&heap), j - k\n\t\t\t\t\t    - 1, \"Incorrect node count\");\n\t\t\t\t\tif (prev != NULL) {\n\t\t\t\t\t\tassert_d_ge(node_cmp(node,\n\t\t\t\t\t\t    prev), 0,\n\t\t\t\t\t\t    \"Bad removal order\");\n\t\t\t\t\t}\n\t\t\t\t\tprev = node;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t} case 3: {\n\t\t\t\tnode_t *prev = NULL;\n\t\t\t\tfor (k = 0; k < j; k++) {\n\t\t\t\t\tnode_t *node = heap_first(&heap);\n\t\t\t\t\tassert_u_eq(heap_validate(&heap), j - k,\n\t\t\t\t\t    \"Incorrect node count\");\n\t\t\t\t\tif (prev != NULL) {\n\t\t\t\t\t\tassert_d_ge(node_cmp(node,\n\t\t\t\t\t\t    prev), 0,\n\t\t\t\t\t\t    \"Bad removal order\");\n\t\t\t\t\t}\n\t\t\t\t\tnode_remove(&heap, node);\n\t\t\t\t\tassert_u_eq(heap_validate(&heap), j - k\n\t\t\t\t\t    - 1, \"Incorrect node count\");\n\t\t\t\t\tprev = node;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t} case 4: {\n\t\t\t\tfor (k = 0; k < j; k++) {\n\t\t\t\t\tnode_remove_any(&heap);\n\t\t\t\t\tassert_u_eq(heap_validate(&heap), j - k\n\t\t\t\t\t    - 1, \"Incorrect node count\");\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t} case 5: {\n\t\t\t\tfor (k = 0; k < j; k++) {\n\t\t\t\t\tnode_t *node = heap_any(&heap);\n\t\t\t\t\tassert_u_eq(heap_validate(&heap), j - k,\n\t\t\t\t\t    \"Incorrect node count\");\n\t\t\t\t\tnode_remove(&heap, node);\n\t\t\t\t\tassert_u_eq(heap_validate(&heap), j - k\n\t\t\t\t\t    - 1, \"Incorrect node count\");\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t} default:\n\t\t\t\tnot_reached();\n\t\t\t}\n\n\t\t\tassert_ptr_null(heap_first(&heap),\n\t\t\t    \"Heap should be empty\");\n\t\t\tassert_ptr_null(heap_any(&heap),\n\t\t\t    \"Heap should be empty\");\n\t\t\tassert_true(heap_empty(&heap), \"Heap should be empty\");\n\t\t}\n\t}\n\tfini_gen_rand(sfmt);\n#undef NNODES\n#undef SEED\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_ph_empty,\n\t    test_ph_random);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/prng.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic void\ntest_prng_lg_range_u32(bool atomic) {\n\tatomic_u32_t sa, sb;\n\tuint32_t ra, rb;\n\tunsigned lg_range;\n\n\tatomic_store_u32(&sa, 42, ATOMIC_RELAXED);\n\tra = prng_lg_range_u32(&sa, 32, atomic);\n\tatomic_store_u32(&sa, 42, ATOMIC_RELAXED);\n\trb = prng_lg_range_u32(&sa, 32, atomic);\n\tassert_u32_eq(ra, rb,\n\t    \"Repeated generation should produce repeated results\");\n\n\tatomic_store_u32(&sb, 42, ATOMIC_RELAXED);\n\trb = prng_lg_range_u32(&sb, 32, atomic);\n\tassert_u32_eq(ra, rb,\n\t    \"Equivalent generation should produce equivalent results\");\n\n\tatomic_store_u32(&sa, 42, ATOMIC_RELAXED);\n\tra = prng_lg_range_u32(&sa, 32, atomic);\n\trb = prng_lg_range_u32(&sa, 32, atomic);\n\tassert_u32_ne(ra, rb,\n\t    \"Full-width results must not immediately repeat\");\n\n\tatomic_store_u32(&sa, 42, ATOMIC_RELAXED);\n\tra = prng_lg_range_u32(&sa, 32, atomic);\n\tfor (lg_range = 31; lg_range > 0; lg_range--) {\n\t\tatomic_store_u32(&sb, 42, ATOMIC_RELAXED);\n\t\trb = prng_lg_range_u32(&sb, lg_range, atomic);\n\t\tassert_u32_eq((rb & (UINT32_C(0xffffffff) << lg_range)),\n\t\t    0, \"High order bits should be 0, lg_range=%u\", lg_range);\n\t\tassert_u32_eq(rb, (ra >> (32 - lg_range)),\n\t\t    \"Expected high order bits of full-width result, \"\n\t\t    \"lg_range=%u\", lg_range);\n\t}\n}\n\nstatic void\ntest_prng_lg_range_u64(void) {\n\tuint64_t sa, sb, ra, rb;\n\tunsigned lg_range;\n\n\tsa = 42;\n\tra = prng_lg_range_u64(&sa, 64);\n\tsa = 42;\n\trb = prng_lg_range_u64(&sa, 64);\n\tassert_u64_eq(ra, rb,\n\t    \"Repeated generation should produce repeated results\");\n\n\tsb = 42;\n\trb = prng_lg_range_u64(&sb, 64);\n\tassert_u64_eq(ra, rb,\n\t    \"Equivalent generation should produce equivalent results\");\n\n\tsa = 42;\n\tra = prng_lg_range_u64(&sa, 64);\n\trb = prng_lg_range_u64(&sa, 64);\n\tassert_u64_ne(ra, rb,\n\t    \"Full-width results must not immediately repeat\");\n\n\tsa = 42;\n\tra = prng_lg_range_u64(&sa, 64);\n\tfor (lg_range = 63; lg_range > 0; lg_range--) {\n\t\tsb = 42;\n\t\trb = prng_lg_range_u64(&sb, lg_range);\n\t\tassert_u64_eq((rb & (UINT64_C(0xffffffffffffffff) << lg_range)),\n\t\t    0, \"High order bits should be 0, lg_range=%u\", lg_range);\n\t\tassert_u64_eq(rb, (ra >> (64 - lg_range)),\n\t\t    \"Expected high order bits of full-width result, \"\n\t\t    \"lg_range=%u\", lg_range);\n\t}\n}\n\nstatic void\ntest_prng_lg_range_zu(bool atomic) {\n\tatomic_zu_t sa, sb;\n\tsize_t ra, rb;\n\tunsigned lg_range;\n\n\tatomic_store_zu(&sa, 42, ATOMIC_RELAXED);\n\tra = prng_lg_range_zu(&sa, ZU(1) << (3 + LG_SIZEOF_PTR), atomic);\n\tatomic_store_zu(&sa, 42, ATOMIC_RELAXED);\n\trb = prng_lg_range_zu(&sa, ZU(1) << (3 + LG_SIZEOF_PTR), atomic);\n\tassert_zu_eq(ra, rb,\n\t    \"Repeated generation should produce repeated results\");\n\n\tatomic_store_zu(&sb, 42, ATOMIC_RELAXED);\n\trb = prng_lg_range_zu(&sb, ZU(1) << (3 + LG_SIZEOF_PTR), atomic);\n\tassert_zu_eq(ra, rb,\n\t    \"Equivalent generation should produce equivalent results\");\n\n\tatomic_store_zu(&sa, 42, ATOMIC_RELAXED);\n\tra = prng_lg_range_zu(&sa, ZU(1) << (3 + LG_SIZEOF_PTR), atomic);\n\trb = prng_lg_range_zu(&sa, ZU(1) << (3 + LG_SIZEOF_PTR), atomic);\n\tassert_zu_ne(ra, rb,\n\t    \"Full-width results must not immediately repeat\");\n\n\tatomic_store_zu(&sa, 42, ATOMIC_RELAXED);\n\tra = prng_lg_range_zu(&sa, ZU(1) << (3 + LG_SIZEOF_PTR), atomic);\n\tfor (lg_range = (ZU(1) << (3 + LG_SIZEOF_PTR)) - 1; lg_range > 0;\n\t    lg_range--) {\n\t\tatomic_store_zu(&sb, 42, ATOMIC_RELAXED);\n\t\trb = prng_lg_range_zu(&sb, lg_range, atomic);\n\t\tassert_zu_eq((rb & (SIZE_T_MAX << lg_range)),\n\t\t    0, \"High order bits should be 0, lg_range=%u\", lg_range);\n\t\tassert_zu_eq(rb, (ra >> ((ZU(1) << (3 + LG_SIZEOF_PTR)) -\n\t\t    lg_range)), \"Expected high order bits of full-width \"\n\t\t    \"result, lg_range=%u\", lg_range);\n\t}\n}\n\nTEST_BEGIN(test_prng_lg_range_u32_nonatomic) {\n\ttest_prng_lg_range_u32(false);\n}\nTEST_END\n\nTEST_BEGIN(test_prng_lg_range_u32_atomic) {\n\ttest_prng_lg_range_u32(true);\n}\nTEST_END\n\nTEST_BEGIN(test_prng_lg_range_u64_nonatomic) {\n\ttest_prng_lg_range_u64();\n}\nTEST_END\n\nTEST_BEGIN(test_prng_lg_range_zu_nonatomic) {\n\ttest_prng_lg_range_zu(false);\n}\nTEST_END\n\nTEST_BEGIN(test_prng_lg_range_zu_atomic) {\n\ttest_prng_lg_range_zu(true);\n}\nTEST_END\n\nstatic void\ntest_prng_range_u32(bool atomic) {\n\tuint32_t range;\n#define MAX_RANGE\t10000000\n#define RANGE_STEP\t97\n#define NREPS\t\t10\n\n\tfor (range = 2; range < MAX_RANGE; range += RANGE_STEP) {\n\t\tatomic_u32_t s;\n\t\tunsigned rep;\n\n\t\tatomic_store_u32(&s, range, ATOMIC_RELAXED);\n\t\tfor (rep = 0; rep < NREPS; rep++) {\n\t\t\tuint32_t r = prng_range_u32(&s, range, atomic);\n\n\t\t\tassert_u32_lt(r, range, \"Out of range\");\n\t\t}\n\t}\n}\n\nstatic void\ntest_prng_range_u64(void) {\n\tuint64_t range;\n#define MAX_RANGE\t10000000\n#define RANGE_STEP\t97\n#define NREPS\t\t10\n\n\tfor (range = 2; range < MAX_RANGE; range += RANGE_STEP) {\n\t\tuint64_t s;\n\t\tunsigned rep;\n\n\t\ts = range;\n\t\tfor (rep = 0; rep < NREPS; rep++) {\n\t\t\tuint64_t r = prng_range_u64(&s, range);\n\n\t\t\tassert_u64_lt(r, range, \"Out of range\");\n\t\t}\n\t}\n}\n\nstatic void\ntest_prng_range_zu(bool atomic) {\n\tsize_t range;\n#define MAX_RANGE\t10000000\n#define RANGE_STEP\t97\n#define NREPS\t\t10\n\n\tfor (range = 2; range < MAX_RANGE; range += RANGE_STEP) {\n\t\tatomic_zu_t s;\n\t\tunsigned rep;\n\n\t\tatomic_store_zu(&s, range, ATOMIC_RELAXED);\n\t\tfor (rep = 0; rep < NREPS; rep++) {\n\t\t\tsize_t r = prng_range_zu(&s, range, atomic);\n\n\t\t\tassert_zu_lt(r, range, \"Out of range\");\n\t\t}\n\t}\n}\n\nTEST_BEGIN(test_prng_range_u32_nonatomic) {\n\ttest_prng_range_u32(false);\n}\nTEST_END\n\nTEST_BEGIN(test_prng_range_u32_atomic) {\n\ttest_prng_range_u32(true);\n}\nTEST_END\n\nTEST_BEGIN(test_prng_range_u64_nonatomic) {\n\ttest_prng_range_u64();\n}\nTEST_END\n\nTEST_BEGIN(test_prng_range_zu_nonatomic) {\n\ttest_prng_range_zu(false);\n}\nTEST_END\n\nTEST_BEGIN(test_prng_range_zu_atomic) {\n\ttest_prng_range_zu(true);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_prng_lg_range_u32_nonatomic,\n\t    test_prng_lg_range_u32_atomic,\n\t    test_prng_lg_range_u64_nonatomic,\n\t    test_prng_lg_range_zu_nonatomic,\n\t    test_prng_lg_range_zu_atomic,\n\t    test_prng_range_u32_nonatomic,\n\t    test_prng_range_u32_atomic,\n\t    test_prng_range_u64_nonatomic,\n\t    test_prng_range_zu_nonatomic,\n\t    test_prng_range_zu_atomic);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/prof_accum.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#define NTHREADS\t\t4\n#define NALLOCS_PER_THREAD\t50\n#define DUMP_INTERVAL\t\t1\n#define BT_COUNT_CHECK_INTERVAL\t5\n\nstatic int\nprof_dump_open_intercept(bool propagate_err, const char *filename) {\n\tint fd;\n\n\tfd = open(\"/dev/null\", O_WRONLY);\n\tassert_d_ne(fd, -1, \"Unexpected open() failure\");\n\n\treturn fd;\n}\n\nstatic void *\nalloc_from_permuted_backtrace(unsigned thd_ind, unsigned iteration) {\n\treturn btalloc(1, thd_ind*NALLOCS_PER_THREAD + iteration);\n}\n\nstatic void *\nthd_start(void *varg) {\n\tunsigned thd_ind = *(unsigned *)varg;\n\tsize_t bt_count_prev, bt_count;\n\tunsigned i_prev, i;\n\n\ti_prev = 0;\n\tbt_count_prev = 0;\n\tfor (i = 0; i < NALLOCS_PER_THREAD; i++) {\n\t\tvoid *p = alloc_from_permuted_backtrace(thd_ind, i);\n\t\tdallocx(p, 0);\n\t\tif (i % DUMP_INTERVAL == 0) {\n\t\t\tassert_d_eq(mallctl(\"prof.dump\", NULL, NULL, NULL, 0),\n\t\t\t    0, \"Unexpected error while dumping heap profile\");\n\t\t}\n\n\t\tif (i % BT_COUNT_CHECK_INTERVAL == 0 ||\n\t\t    i+1 == NALLOCS_PER_THREAD) {\n\t\t\tbt_count = prof_bt_count();\n\t\t\tassert_zu_le(bt_count_prev+(i-i_prev), bt_count,\n\t\t\t    \"Expected larger backtrace count increase\");\n\t\t\ti_prev = i;\n\t\t\tbt_count_prev = bt_count;\n\t\t}\n\t}\n\n\treturn NULL;\n}\n\nTEST_BEGIN(test_idump) {\n\tbool active;\n\tthd_t thds[NTHREADS];\n\tunsigned thd_args[NTHREADS];\n\tunsigned i;\n\n\ttest_skip_if(!config_prof);\n\n\tactive = true;\n\tassert_d_eq(mallctl(\"prof.active\", NULL, NULL, (void *)&active,\n\t    sizeof(active)), 0,\n\t    \"Unexpected mallctl failure while activating profiling\");\n\n\tprof_dump_open = prof_dump_open_intercept;\n\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tthd_args[i] = i;\n\t\tthd_create(&thds[i], thd_start, (void *)&thd_args[i]);\n\t}\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tthd_join(thds[i], NULL);\n\t}\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test_no_reentrancy(\n\t    test_idump);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/prof_accum.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_prof}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"prof:true,prof_accum:true,prof_active:false,lg_prof_sample:0\"\nfi\n"
  },
  {
    "path": "deps/jemalloc/test/unit/prof_active.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic void\nmallctl_bool_get(const char *name, bool expected, const char *func, int line) {\n\tbool old;\n\tsize_t sz;\n\n\tsz = sizeof(old);\n\tassert_d_eq(mallctl(name, (void *)&old, &sz, NULL, 0), 0,\n\t    \"%s():%d: Unexpected mallctl failure reading %s\", func, line, name);\n\tassert_b_eq(old, expected, \"%s():%d: Unexpected %s value\", func, line,\n\t    name);\n}\n\nstatic void\nmallctl_bool_set(const char *name, bool old_expected, bool val_new,\n    const char *func, int line) {\n\tbool old;\n\tsize_t sz;\n\n\tsz = sizeof(old);\n\tassert_d_eq(mallctl(name, (void *)&old, &sz, (void *)&val_new,\n\t    sizeof(val_new)), 0,\n\t    \"%s():%d: Unexpected mallctl failure reading/writing %s\", func,\n\t    line, name);\n\tassert_b_eq(old, old_expected, \"%s():%d: Unexpected %s value\", func,\n\t    line, name);\n}\n\nstatic void\nmallctl_prof_active_get_impl(bool prof_active_old_expected, const char *func,\n    int line) {\n\tmallctl_bool_get(\"prof.active\", prof_active_old_expected, func, line);\n}\n#define mallctl_prof_active_get(a)\t\t\t\t\t\\\n\tmallctl_prof_active_get_impl(a, __func__, __LINE__)\n\nstatic void\nmallctl_prof_active_set_impl(bool prof_active_old_expected,\n    bool prof_active_new, const char *func, int line) {\n\tmallctl_bool_set(\"prof.active\", prof_active_old_expected,\n\t    prof_active_new, func, line);\n}\n#define mallctl_prof_active_set(a, b)\t\t\t\t\t\\\n\tmallctl_prof_active_set_impl(a, b, __func__, __LINE__)\n\nstatic void\nmallctl_thread_prof_active_get_impl(bool thread_prof_active_old_expected,\n    const char *func, int line) {\n\tmallctl_bool_get(\"thread.prof.active\", thread_prof_active_old_expected,\n\t    func, line);\n}\n#define mallctl_thread_prof_active_get(a)\t\t\t\t\\\n\tmallctl_thread_prof_active_get_impl(a, __func__, __LINE__)\n\nstatic void\nmallctl_thread_prof_active_set_impl(bool thread_prof_active_old_expected,\n    bool thread_prof_active_new, const char *func, int line) {\n\tmallctl_bool_set(\"thread.prof.active\", thread_prof_active_old_expected,\n\t    thread_prof_active_new, func, line);\n}\n#define mallctl_thread_prof_active_set(a, b)\t\t\t\t\\\n\tmallctl_thread_prof_active_set_impl(a, b, __func__, __LINE__)\n\nstatic void\nprof_sampling_probe_impl(bool expect_sample, const char *func, int line) {\n\tvoid *p;\n\tsize_t expected_backtraces = expect_sample ? 1 : 0;\n\n\tassert_zu_eq(prof_bt_count(), 0, \"%s():%d: Expected 0 backtraces\", func,\n\t    line);\n\tp = mallocx(1, 0);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\tassert_zu_eq(prof_bt_count(), expected_backtraces,\n\t    \"%s():%d: Unexpected backtrace count\", func, line);\n\tdallocx(p, 0);\n}\n#define prof_sampling_probe(a)\t\t\t\t\t\t\\\n\tprof_sampling_probe_impl(a, __func__, __LINE__)\n\nTEST_BEGIN(test_prof_active) {\n\ttest_skip_if(!config_prof);\n\n\tmallctl_prof_active_get(true);\n\tmallctl_thread_prof_active_get(false);\n\n\tmallctl_prof_active_set(true, true);\n\tmallctl_thread_prof_active_set(false, false);\n\t/* prof.active, !thread.prof.active. */\n\tprof_sampling_probe(false);\n\n\tmallctl_prof_active_set(true, false);\n\tmallctl_thread_prof_active_set(false, false);\n\t/* !prof.active, !thread.prof.active. */\n\tprof_sampling_probe(false);\n\n\tmallctl_prof_active_set(false, false);\n\tmallctl_thread_prof_active_set(false, true);\n\t/* !prof.active, thread.prof.active. */\n\tprof_sampling_probe(false);\n\n\tmallctl_prof_active_set(false, true);\n\tmallctl_thread_prof_active_set(true, true);\n\t/* prof.active, thread.prof.active. */\n\tprof_sampling_probe(true);\n\n\t/* Restore settings. */\n\tmallctl_prof_active_set(true, true);\n\tmallctl_thread_prof_active_set(true, false);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test_no_reentrancy(\n\t    test_prof_active);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/prof_active.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_prof}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"prof:true,prof_thread_active_init:false,lg_prof_sample:0\"\nfi\n"
  },
  {
    "path": "deps/jemalloc/test/unit/prof_gdump.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic bool did_prof_dump_open;\n\nstatic int\nprof_dump_open_intercept(bool propagate_err, const char *filename) {\n\tint fd;\n\n\tdid_prof_dump_open = true;\n\n\tfd = open(\"/dev/null\", O_WRONLY);\n\tassert_d_ne(fd, -1, \"Unexpected open() failure\");\n\n\treturn fd;\n}\n\nTEST_BEGIN(test_gdump) {\n\tbool active, gdump, gdump_old;\n\tvoid *p, *q, *r, *s;\n\tsize_t sz;\n\n\ttest_skip_if(!config_prof);\n\n\tactive = true;\n\tassert_d_eq(mallctl(\"prof.active\", NULL, NULL, (void *)&active,\n\t    sizeof(active)), 0,\n\t    \"Unexpected mallctl failure while activating profiling\");\n\n\tprof_dump_open = prof_dump_open_intercept;\n\n\tdid_prof_dump_open = false;\n\tp = mallocx((1U << SC_LG_LARGE_MINCLASS), 0);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\tassert_true(did_prof_dump_open, \"Expected a profile dump\");\n\n\tdid_prof_dump_open = false;\n\tq = mallocx((1U << SC_LG_LARGE_MINCLASS), 0);\n\tassert_ptr_not_null(q, \"Unexpected mallocx() failure\");\n\tassert_true(did_prof_dump_open, \"Expected a profile dump\");\n\n\tgdump = false;\n\tsz = sizeof(gdump_old);\n\tassert_d_eq(mallctl(\"prof.gdump\", (void *)&gdump_old, &sz,\n\t    (void *)&gdump, sizeof(gdump)), 0,\n\t    \"Unexpected mallctl failure while disabling prof.gdump\");\n\tassert(gdump_old);\n\tdid_prof_dump_open = false;\n\tr = mallocx((1U << SC_LG_LARGE_MINCLASS), 0);\n\tassert_ptr_not_null(q, \"Unexpected mallocx() failure\");\n\tassert_false(did_prof_dump_open, \"Unexpected profile dump\");\n\n\tgdump = true;\n\tsz = sizeof(gdump_old);\n\tassert_d_eq(mallctl(\"prof.gdump\", (void *)&gdump_old, &sz,\n\t    (void *)&gdump, sizeof(gdump)), 0,\n\t    \"Unexpected mallctl failure while enabling prof.gdump\");\n\tassert(!gdump_old);\n\tdid_prof_dump_open = false;\n\ts = mallocx((1U << SC_LG_LARGE_MINCLASS), 0);\n\tassert_ptr_not_null(q, \"Unexpected mallocx() failure\");\n\tassert_true(did_prof_dump_open, \"Expected a profile dump\");\n\n\tdallocx(p, 0);\n\tdallocx(q, 0);\n\tdallocx(r, 0);\n\tdallocx(s, 0);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test_no_reentrancy(\n\t    test_gdump);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/prof_gdump.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_prof}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"prof:true,prof_active:false,prof_gdump:true\"\nfi\n\n"
  },
  {
    "path": "deps/jemalloc/test/unit/prof_idump.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic bool did_prof_dump_open;\n\nstatic int\nprof_dump_open_intercept(bool propagate_err, const char *filename) {\n\tint fd;\n\n\tdid_prof_dump_open = true;\n\n\tfd = open(\"/dev/null\", O_WRONLY);\n\tassert_d_ne(fd, -1, \"Unexpected open() failure\");\n\n\treturn fd;\n}\n\nTEST_BEGIN(test_idump) {\n\tbool active;\n\tvoid *p;\n\n\ttest_skip_if(!config_prof);\n\n\tactive = true;\n\tassert_d_eq(mallctl(\"prof.active\", NULL, NULL, (void *)&active,\n\t    sizeof(active)), 0,\n\t    \"Unexpected mallctl failure while activating profiling\");\n\n\tprof_dump_open = prof_dump_open_intercept;\n\n\tdid_prof_dump_open = false;\n\tp = mallocx(1, 0);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\tdallocx(p, 0);\n\tassert_true(did_prof_dump_open, \"Expected a profile dump\");\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_idump);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/prof_idump.sh",
    "content": "#!/bin/sh\n\nexport MALLOC_CONF=\"tcache:false\"\nif [ \"x${enable_prof}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"${MALLOC_CONF},prof:true,prof_accum:true,prof_active:false,lg_prof_sample:0,lg_prof_interval:0\"\nfi\n\n\n"
  },
  {
    "path": "deps/jemalloc/test/unit/prof_log.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#define N_PARAM 100\n#define N_THREADS 10\n\nstatic void assert_rep() {\n\tassert_b_eq(prof_log_rep_check(), false, \"Rep check failed\");\n}\n\nstatic void assert_log_empty() {\n\tassert_zu_eq(prof_log_bt_count(), 0,\n\t    \"The log has backtraces; it isn't empty\");\n\tassert_zu_eq(prof_log_thr_count(), 0,\n\t    \"The log has threads; it isn't empty\");\n\tassert_zu_eq(prof_log_alloc_count(), 0,\n\t    \"The log has allocations; it isn't empty\");\n}\n\nvoid *buf[N_PARAM];\n\nstatic void f() {\n\tint i;\n\tfor (i = 0; i < N_PARAM; i++) {\n\t\tbuf[i] = malloc(100);\n\t}\n\tfor (i = 0; i < N_PARAM; i++) {\n\t\tfree(buf[i]);\n\t}\n}\n\nTEST_BEGIN(test_prof_log_many_logs) {\n\tint i;\n\n\ttest_skip_if(!config_prof);\n\n\tfor (i = 0; i < N_PARAM; i++) {\n\t\tassert_b_eq(prof_log_is_logging(), false,\n\t\t    \"Logging shouldn't have started yet\");\n\t\tassert_d_eq(mallctl(\"prof.log_start\", NULL, NULL, NULL, 0), 0,\n\t\t    \"Unexpected mallctl failure when starting logging\");\n\t\tassert_b_eq(prof_log_is_logging(), true,\n\t\t    \"Logging should be started by now\");\n\t\tassert_log_empty();\n\t\tassert_rep();\n\t\tf();\n\t\tassert_zu_eq(prof_log_thr_count(), 1, \"Wrong thread count\");\n\t\tassert_rep();\n\t\tassert_b_eq(prof_log_is_logging(), true,\n\t\t    \"Logging should still be on\");\n\t\tassert_d_eq(mallctl(\"prof.log_stop\", NULL, NULL, NULL, 0), 0,\n\t\t    \"Unexpected mallctl failure when stopping logging\");\n\t\tassert_b_eq(prof_log_is_logging(), false,\n\t\t    \"Logging should have turned off\");\n\t}\n}\nTEST_END\n\nthd_t thr_buf[N_THREADS];\n\nstatic void *f_thread(void *unused) {\n\tint i;\n\tfor (i = 0; i < N_PARAM; i++) {\n\t\tvoid *p = malloc(100);\n\t\tmemset(p, 100, sizeof(char));\n\t\tfree(p);\n\t}\n\n\treturn NULL;\n}\n\nTEST_BEGIN(test_prof_log_many_threads) {\n\n\ttest_skip_if(!config_prof);\n\n\tint i;\n\tassert_d_eq(mallctl(\"prof.log_start\", NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctl failure when starting logging\");\n\tfor (i = 0; i < N_THREADS; i++) {\n\t\tthd_create(&thr_buf[i], &f_thread, NULL);\n\t}\n\n\tfor (i = 0; i < N_THREADS; i++) {\n\t\tthd_join(thr_buf[i], NULL);\n\t}\n\tassert_zu_eq(prof_log_thr_count(), N_THREADS,\n\t    \"Wrong number of thread entries\");\n\tassert_rep();\n\tassert_d_eq(mallctl(\"prof.log_stop\", NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctl failure when stopping logging\");\n}\nTEST_END\n\nstatic void f3() {\n\tvoid *p = malloc(100);\n\tfree(p);\n}\n\nstatic void f1() {\n\tvoid *p = malloc(100);\n\tf3();\n\tfree(p);\n}\n\nstatic void f2() {\n\tvoid *p = malloc(100);\n\tfree(p);\n}\n\nTEST_BEGIN(test_prof_log_many_traces) {\n\n\ttest_skip_if(!config_prof);\n\n\tassert_d_eq(mallctl(\"prof.log_start\", NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctl failure when starting logging\");\n\tint i;\n\tassert_rep();\n\tassert_log_empty();\n\tfor (i = 0; i < N_PARAM; i++) {\n\t\tassert_rep();\n\t\tf1();\n\t\tassert_rep();\n\t\tf2();\n\t\tassert_rep();\n\t\tf3();\n\t\tassert_rep();\n\t}\n\t/*\n\t * There should be 8 total backtraces: two for malloc/free in f1(), two\n\t * for malloc/free in f2(), two for malloc/free in f3(), and then two\n\t * for malloc/free in f1()'s call to f3().  However compiler\n\t * optimizations such as loop unrolling might generate more call sites.\n\t * So >= 8 traces are expected.\n\t */\n\tassert_zu_ge(prof_log_bt_count(), 8,\n\t    \"Expect at least 8 backtraces given sample workload\");\n\tassert_d_eq(mallctl(\"prof.log_stop\", NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctl failure when stopping logging\");\n}\nTEST_END\n\nint\nmain(void) {\n\tprof_log_dummy_set(true);\n\treturn test_no_reentrancy(\n\t    test_prof_log_many_logs,\n\t    test_prof_log_many_traces,\n\t    test_prof_log_many_threads);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/prof_log.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_prof}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"prof:true,lg_prof_sample:0\"\nfi\n"
  },
  {
    "path": "deps/jemalloc/test/unit/prof_reset.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic int\nprof_dump_open_intercept(bool propagate_err, const char *filename) {\n\tint fd;\n\n\tfd = open(\"/dev/null\", O_WRONLY);\n\tassert_d_ne(fd, -1, \"Unexpected open() failure\");\n\n\treturn fd;\n}\n\nstatic void\nset_prof_active(bool active) {\n\tassert_d_eq(mallctl(\"prof.active\", NULL, NULL, (void *)&active,\n\t    sizeof(active)), 0, \"Unexpected mallctl failure\");\n}\n\nstatic size_t\nget_lg_prof_sample(void) {\n\tsize_t lg_prof_sample;\n\tsize_t sz = sizeof(size_t);\n\n\tassert_d_eq(mallctl(\"prof.lg_sample\", (void *)&lg_prof_sample, &sz,\n\t    NULL, 0), 0,\n\t    \"Unexpected mallctl failure while reading profiling sample rate\");\n\treturn lg_prof_sample;\n}\n\nstatic void\ndo_prof_reset(size_t lg_prof_sample) {\n\tassert_d_eq(mallctl(\"prof.reset\", NULL, NULL,\n\t    (void *)&lg_prof_sample, sizeof(size_t)), 0,\n\t    \"Unexpected mallctl failure while resetting profile data\");\n\tassert_zu_eq(lg_prof_sample, get_lg_prof_sample(),\n\t    \"Expected profile sample rate change\");\n}\n\nTEST_BEGIN(test_prof_reset_basic) {\n\tsize_t lg_prof_sample_orig, lg_prof_sample, lg_prof_sample_next;\n\tsize_t sz;\n\tunsigned i;\n\n\ttest_skip_if(!config_prof);\n\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctl(\"opt.lg_prof_sample\", (void *)&lg_prof_sample_orig,\n\t    &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl failure while reading profiling sample rate\");\n\tassert_zu_eq(lg_prof_sample_orig, 0,\n\t    \"Unexpected profiling sample rate\");\n\tlg_prof_sample = get_lg_prof_sample();\n\tassert_zu_eq(lg_prof_sample_orig, lg_prof_sample,\n\t    \"Unexpected disagreement between \\\"opt.lg_prof_sample\\\" and \"\n\t    \"\\\"prof.lg_sample\\\"\");\n\n\t/* Test simple resets. */\n\tfor (i = 0; i < 2; i++) {\n\t\tassert_d_eq(mallctl(\"prof.reset\", NULL, NULL, NULL, 0), 0,\n\t\t    \"Unexpected mallctl failure while resetting profile data\");\n\t\tlg_prof_sample = get_lg_prof_sample();\n\t\tassert_zu_eq(lg_prof_sample_orig, lg_prof_sample,\n\t\t    \"Unexpected profile sample rate change\");\n\t}\n\n\t/* Test resets with prof.lg_sample changes. */\n\tlg_prof_sample_next = 1;\n\tfor (i = 0; i < 2; i++) {\n\t\tdo_prof_reset(lg_prof_sample_next);\n\t\tlg_prof_sample = get_lg_prof_sample();\n\t\tassert_zu_eq(lg_prof_sample, lg_prof_sample_next,\n\t\t    \"Expected profile sample rate change\");\n\t\tlg_prof_sample_next = lg_prof_sample_orig;\n\t}\n\n\t/* Make sure the test code restored prof.lg_sample. */\n\tlg_prof_sample = get_lg_prof_sample();\n\tassert_zu_eq(lg_prof_sample_orig, lg_prof_sample,\n\t    \"Unexpected disagreement between \\\"opt.lg_prof_sample\\\" and \"\n\t    \"\\\"prof.lg_sample\\\"\");\n}\nTEST_END\n\nbool prof_dump_header_intercepted = false;\nprof_cnt_t cnt_all_copy = {0, 0, 0, 0};\nstatic bool\nprof_dump_header_intercept(tsdn_t *tsdn, bool propagate_err,\n    const prof_cnt_t *cnt_all) {\n\tprof_dump_header_intercepted = true;\n\tmemcpy(&cnt_all_copy, cnt_all, sizeof(prof_cnt_t));\n\n\treturn false;\n}\n\nTEST_BEGIN(test_prof_reset_cleanup) {\n\tvoid *p;\n\tprof_dump_header_t *prof_dump_header_orig;\n\n\ttest_skip_if(!config_prof);\n\n\tset_prof_active(true);\n\n\tassert_zu_eq(prof_bt_count(), 0, \"Expected 0 backtraces\");\n\tp = mallocx(1, 0);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\tassert_zu_eq(prof_bt_count(), 1, \"Expected 1 backtrace\");\n\n\tprof_dump_header_orig = prof_dump_header;\n\tprof_dump_header = prof_dump_header_intercept;\n\tassert_false(prof_dump_header_intercepted, \"Unexpected intercept\");\n\n\tassert_d_eq(mallctl(\"prof.dump\", NULL, NULL, NULL, 0),\n\t    0, \"Unexpected error while dumping heap profile\");\n\tassert_true(prof_dump_header_intercepted, \"Expected intercept\");\n\tassert_u64_eq(cnt_all_copy.curobjs, 1, \"Expected 1 allocation\");\n\n\tassert_d_eq(mallctl(\"prof.reset\", NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected error while resetting heap profile data\");\n\tassert_d_eq(mallctl(\"prof.dump\", NULL, NULL, NULL, 0),\n\t    0, \"Unexpected error while dumping heap profile\");\n\tassert_u64_eq(cnt_all_copy.curobjs, 0, \"Expected 0 allocations\");\n\tassert_zu_eq(prof_bt_count(), 1, \"Expected 1 backtrace\");\n\n\tprof_dump_header = prof_dump_header_orig;\n\n\tdallocx(p, 0);\n\tassert_zu_eq(prof_bt_count(), 0, \"Expected 0 backtraces\");\n\n\tset_prof_active(false);\n}\nTEST_END\n\n#define NTHREADS\t\t4\n#define NALLOCS_PER_THREAD\t(1U << 13)\n#define OBJ_RING_BUF_COUNT\t1531\n#define RESET_INTERVAL\t\t(1U << 10)\n#define DUMP_INTERVAL\t\t3677\nstatic void *\nthd_start(void *varg) {\n\tunsigned thd_ind = *(unsigned *)varg;\n\tunsigned i;\n\tvoid *objs[OBJ_RING_BUF_COUNT];\n\n\tmemset(objs, 0, sizeof(objs));\n\n\tfor (i = 0; i < NALLOCS_PER_THREAD; i++) {\n\t\tif (i % RESET_INTERVAL == 0) {\n\t\t\tassert_d_eq(mallctl(\"prof.reset\", NULL, NULL, NULL, 0),\n\t\t\t    0, \"Unexpected error while resetting heap profile \"\n\t\t\t    \"data\");\n\t\t}\n\n\t\tif (i % DUMP_INTERVAL == 0) {\n\t\t\tassert_d_eq(mallctl(\"prof.dump\", NULL, NULL, NULL, 0),\n\t\t\t    0, \"Unexpected error while dumping heap profile\");\n\t\t}\n\n\t\t{\n\t\t\tvoid **pp = &objs[i % OBJ_RING_BUF_COUNT];\n\t\t\tif (*pp != NULL) {\n\t\t\t\tdallocx(*pp, 0);\n\t\t\t\t*pp = NULL;\n\t\t\t}\n\t\t\t*pp = btalloc(1, thd_ind*NALLOCS_PER_THREAD + i);\n\t\t\tassert_ptr_not_null(*pp,\n\t\t\t    \"Unexpected btalloc() failure\");\n\t\t}\n\t}\n\n\t/* Clean up any remaining objects. */\n\tfor (i = 0; i < OBJ_RING_BUF_COUNT; i++) {\n\t\tvoid **pp = &objs[i % OBJ_RING_BUF_COUNT];\n\t\tif (*pp != NULL) {\n\t\t\tdallocx(*pp, 0);\n\t\t\t*pp = NULL;\n\t\t}\n\t}\n\n\treturn NULL;\n}\n\nTEST_BEGIN(test_prof_reset) {\n\tsize_t lg_prof_sample_orig;\n\tthd_t thds[NTHREADS];\n\tunsigned thd_args[NTHREADS];\n\tunsigned i;\n\tsize_t bt_count, tdata_count;\n\n\ttest_skip_if(!config_prof);\n\n\tbt_count = prof_bt_count();\n\tassert_zu_eq(bt_count, 0,\n\t    \"Unexpected pre-existing tdata structures\");\n\ttdata_count = prof_tdata_count();\n\n\tlg_prof_sample_orig = get_lg_prof_sample();\n\tdo_prof_reset(5);\n\n\tset_prof_active(true);\n\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tthd_args[i] = i;\n\t\tthd_create(&thds[i], thd_start, (void *)&thd_args[i]);\n\t}\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tthd_join(thds[i], NULL);\n\t}\n\n\tassert_zu_eq(prof_bt_count(), bt_count,\n\t    \"Unexpected bactrace count change\");\n\tassert_zu_eq(prof_tdata_count(), tdata_count,\n\t    \"Unexpected remaining tdata structures\");\n\n\tset_prof_active(false);\n\n\tdo_prof_reset(lg_prof_sample_orig);\n}\nTEST_END\n#undef NTHREADS\n#undef NALLOCS_PER_THREAD\n#undef OBJ_RING_BUF_COUNT\n#undef RESET_INTERVAL\n#undef DUMP_INTERVAL\n\n/* Test sampling at the same allocation site across resets. */\n#define NITER 10\nTEST_BEGIN(test_xallocx) {\n\tsize_t lg_prof_sample_orig;\n\tunsigned i;\n\tvoid *ptrs[NITER];\n\n\ttest_skip_if(!config_prof);\n\n\tlg_prof_sample_orig = get_lg_prof_sample();\n\tset_prof_active(true);\n\n\t/* Reset profiling. */\n\tdo_prof_reset(0);\n\n\tfor (i = 0; i < NITER; i++) {\n\t\tvoid *p;\n\t\tsize_t sz, nsz;\n\n\t\t/* Reset profiling. */\n\t\tdo_prof_reset(0);\n\n\t\t/* Allocate small object (which will be promoted). */\n\t\tp = ptrs[i] = mallocx(1, 0);\n\t\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\n\t\t/* Reset profiling. */\n\t\tdo_prof_reset(0);\n\n\t\t/* Perform successful xallocx(). */\n\t\tsz = sallocx(p, 0);\n\t\tassert_zu_eq(xallocx(p, sz, 0, 0), sz,\n\t\t    \"Unexpected xallocx() failure\");\n\n\t\t/* Perform unsuccessful xallocx(). */\n\t\tnsz = nallocx(sz+1, 0);\n\t\tassert_zu_eq(xallocx(p, nsz, 0, 0), sz,\n\t\t    \"Unexpected xallocx() success\");\n\t}\n\n\tfor (i = 0; i < NITER; i++) {\n\t\t/* dallocx. */\n\t\tdallocx(ptrs[i], 0);\n\t}\n\n\tset_prof_active(false);\n\tdo_prof_reset(lg_prof_sample_orig);\n}\nTEST_END\n#undef NITER\n\nint\nmain(void) {\n\t/* Intercept dumping prior to running any tests. */\n\tprof_dump_open = prof_dump_open_intercept;\n\n\treturn test_no_reentrancy(\n\t    test_prof_reset_basic,\n\t    test_prof_reset_cleanup,\n\t    test_prof_reset,\n\t    test_xallocx);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/prof_reset.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_prof}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"prof:true,prof_active:false,lg_prof_sample:0\"\nfi\n"
  },
  {
    "path": "deps/jemalloc/test/unit/prof_tctx.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nTEST_BEGIN(test_prof_realloc) {\n\ttsdn_t *tsdn;\n\tint flags;\n\tvoid *p, *q;\n\tprof_tctx_t *tctx_p, *tctx_q;\n\tuint64_t curobjs_0, curobjs_1, curobjs_2, curobjs_3;\n\n\ttest_skip_if(!config_prof);\n\n\ttsdn = tsdn_fetch();\n\tflags = MALLOCX_TCACHE_NONE;\n\n\tprof_cnt_all(&curobjs_0, NULL, NULL, NULL);\n\tp = mallocx(1024, flags);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\ttctx_p = prof_tctx_get(tsdn, p, NULL);\n\tassert_ptr_ne(tctx_p, (prof_tctx_t *)(uintptr_t)1U,\n\t    \"Expected valid tctx\");\n\tprof_cnt_all(&curobjs_1, NULL, NULL, NULL);\n\tassert_u64_eq(curobjs_0 + 1, curobjs_1,\n\t    \"Allocation should have increased sample size\");\n\n\tq = rallocx(p, 2048, flags);\n\tassert_ptr_ne(p, q, \"Expected move\");\n\tassert_ptr_not_null(p, \"Unexpected rmallocx() failure\");\n\ttctx_q = prof_tctx_get(tsdn, q, NULL);\n\tassert_ptr_ne(tctx_q, (prof_tctx_t *)(uintptr_t)1U,\n\t    \"Expected valid tctx\");\n\tprof_cnt_all(&curobjs_2, NULL, NULL, NULL);\n\tassert_u64_eq(curobjs_1, curobjs_2,\n\t    \"Reallocation should not have changed sample size\");\n\n\tdallocx(q, flags);\n\tprof_cnt_all(&curobjs_3, NULL, NULL, NULL);\n\tassert_u64_eq(curobjs_0, curobjs_3,\n\t    \"Sample size should have returned to base level\");\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test_no_reentrancy(\n\t    test_prof_realloc);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/prof_tctx.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_prof}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"prof:true,lg_prof_sample:0\"\nfi\n"
  },
  {
    "path": "deps/jemalloc/test/unit/prof_thread_name.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic void\nmallctl_thread_name_get_impl(const char *thread_name_expected, const char *func,\n    int line) {\n\tconst char *thread_name_old;\n\tsize_t sz;\n\n\tsz = sizeof(thread_name_old);\n\tassert_d_eq(mallctl(\"thread.prof.name\", (void *)&thread_name_old, &sz,\n\t    NULL, 0), 0,\n\t    \"%s():%d: Unexpected mallctl failure reading thread.prof.name\",\n\t    func, line);\n\tassert_str_eq(thread_name_old, thread_name_expected,\n\t    \"%s():%d: Unexpected thread.prof.name value\", func, line);\n}\n#define mallctl_thread_name_get(a)\t\t\t\t\t\\\n\tmallctl_thread_name_get_impl(a, __func__, __LINE__)\n\nstatic void\nmallctl_thread_name_set_impl(const char *thread_name, const char *func,\n    int line) {\n\tassert_d_eq(mallctl(\"thread.prof.name\", NULL, NULL,\n\t    (void *)&thread_name, sizeof(thread_name)), 0,\n\t    \"%s():%d: Unexpected mallctl failure reading thread.prof.name\",\n\t    func, line);\n\tmallctl_thread_name_get_impl(thread_name, func, line);\n}\n#define mallctl_thread_name_set(a)\t\t\t\t\t\\\n\tmallctl_thread_name_set_impl(a, __func__, __LINE__)\n\nTEST_BEGIN(test_prof_thread_name_validation) {\n\tconst char *thread_name;\n\n\ttest_skip_if(!config_prof);\n\n\tmallctl_thread_name_get(\"\");\n\tmallctl_thread_name_set(\"hi there\");\n\n\t/* NULL input shouldn't be allowed. */\n\tthread_name = NULL;\n\tassert_d_eq(mallctl(\"thread.prof.name\", NULL, NULL,\n\t    (void *)&thread_name, sizeof(thread_name)), EFAULT,\n\t    \"Unexpected mallctl result writing \\\"%s\\\" to thread.prof.name\",\n\t    thread_name);\n\n\t/* '\\n' shouldn't be allowed. */\n\tthread_name = \"hi\\nthere\";\n\tassert_d_eq(mallctl(\"thread.prof.name\", NULL, NULL,\n\t    (void *)&thread_name, sizeof(thread_name)), EFAULT,\n\t    \"Unexpected mallctl result writing \\\"%s\\\" to thread.prof.name\",\n\t    thread_name);\n\n\t/* Simultaneous read/write shouldn't be allowed. */\n\t{\n\t\tconst char *thread_name_old;\n\t\tsize_t sz;\n\n\t\tsz = sizeof(thread_name_old);\n\t\tassert_d_eq(mallctl(\"thread.prof.name\",\n\t\t    (void *)&thread_name_old, &sz, (void *)&thread_name,\n\t\t    sizeof(thread_name)), EPERM,\n\t\t    \"Unexpected mallctl result writing \\\"%s\\\" to \"\n\t\t    \"thread.prof.name\", thread_name);\n\t}\n\n\tmallctl_thread_name_set(\"\");\n}\nTEST_END\n\n#define NTHREADS\t4\n#define NRESET\t\t25\nstatic void *\nthd_start(void *varg) {\n\tunsigned thd_ind = *(unsigned *)varg;\n\tchar thread_name[16] = \"\";\n\tunsigned i;\n\n\tmalloc_snprintf(thread_name, sizeof(thread_name), \"thread %u\", thd_ind);\n\n\tmallctl_thread_name_get(\"\");\n\tmallctl_thread_name_set(thread_name);\n\n\tfor (i = 0; i < NRESET; i++) {\n\t\tassert_d_eq(mallctl(\"prof.reset\", NULL, NULL, NULL, 0), 0,\n\t\t    \"Unexpected error while resetting heap profile data\");\n\t\tmallctl_thread_name_get(thread_name);\n\t}\n\n\tmallctl_thread_name_set(thread_name);\n\tmallctl_thread_name_set(\"\");\n\n\treturn NULL;\n}\n\nTEST_BEGIN(test_prof_thread_name_threaded) {\n\tthd_t thds[NTHREADS];\n\tunsigned thd_args[NTHREADS];\n\tunsigned i;\n\n\ttest_skip_if(!config_prof);\n\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tthd_args[i] = i;\n\t\tthd_create(&thds[i], thd_start, (void *)&thd_args[i]);\n\t}\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tthd_join(thds[i], NULL);\n\t}\n}\nTEST_END\n#undef NTHREADS\n#undef NRESET\n\nint\nmain(void) {\n\treturn test(\n\t    test_prof_thread_name_validation,\n\t    test_prof_thread_name_threaded);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/prof_thread_name.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_prof}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"prof:true,prof_active:false\"\nfi\n"
  },
  {
    "path": "deps/jemalloc/test/unit/ql.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/ql.h\"\n\n/* Number of ring entries, in [2..26]. */\n#define NENTRIES 9\n\ntypedef struct list_s list_t;\ntypedef ql_head(list_t) list_head_t;\n\nstruct list_s {\n\tql_elm(list_t) link;\n\tchar id;\n};\n\nstatic void\ntest_empty_list(list_head_t *head) {\n\tlist_t *t;\n\tunsigned i;\n\n\tassert_ptr_null(ql_first(head), \"Unexpected element for empty list\");\n\tassert_ptr_null(ql_last(head, link),\n\t    \"Unexpected element for empty list\");\n\n\ti = 0;\n\tql_foreach(t, head, link) {\n\t\ti++;\n\t}\n\tassert_u_eq(i, 0, \"Unexpected element for empty list\");\n\n\ti = 0;\n\tql_reverse_foreach(t, head, link) {\n\t\ti++;\n\t}\n\tassert_u_eq(i, 0, \"Unexpected element for empty list\");\n}\n\nTEST_BEGIN(test_ql_empty) {\n\tlist_head_t head;\n\n\tql_new(&head);\n\ttest_empty_list(&head);\n}\nTEST_END\n\nstatic void\ninit_entries(list_t *entries, unsigned nentries) {\n\tunsigned i;\n\n\tfor (i = 0; i < nentries; i++) {\n\t\tentries[i].id = 'a' + i;\n\t\tql_elm_new(&entries[i], link);\n\t}\n}\n\nstatic void\ntest_entries_list(list_head_t *head, list_t *entries, unsigned nentries) {\n\tlist_t *t;\n\tunsigned i;\n\n\tassert_c_eq(ql_first(head)->id, entries[0].id, \"Element id mismatch\");\n\tassert_c_eq(ql_last(head, link)->id, entries[nentries-1].id,\n\t    \"Element id mismatch\");\n\n\ti = 0;\n\tql_foreach(t, head, link) {\n\t\tassert_c_eq(t->id, entries[i].id, \"Element id mismatch\");\n\t\ti++;\n\t}\n\n\ti = 0;\n\tql_reverse_foreach(t, head, link) {\n\t\tassert_c_eq(t->id, entries[nentries-i-1].id,\n\t\t    \"Element id mismatch\");\n\t\ti++;\n\t}\n\n\tfor (i = 0; i < nentries-1; i++) {\n\t\tt = ql_next(head, &entries[i], link);\n\t\tassert_c_eq(t->id, entries[i+1].id, \"Element id mismatch\");\n\t}\n\tassert_ptr_null(ql_next(head, &entries[nentries-1], link),\n\t    \"Unexpected element\");\n\n\tassert_ptr_null(ql_prev(head, &entries[0], link), \"Unexpected element\");\n\tfor (i = 1; i < nentries; i++) {\n\t\tt = ql_prev(head, &entries[i], link);\n\t\tassert_c_eq(t->id, entries[i-1].id, \"Element id mismatch\");\n\t}\n}\n\nTEST_BEGIN(test_ql_tail_insert) {\n\tlist_head_t head;\n\tlist_t entries[NENTRIES];\n\tunsigned i;\n\n\tql_new(&head);\n\tinit_entries(entries, sizeof(entries)/sizeof(list_t));\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tql_tail_insert(&head, &entries[i], link);\n\t}\n\n\ttest_entries_list(&head, entries, NENTRIES);\n}\nTEST_END\n\nTEST_BEGIN(test_ql_tail_remove) {\n\tlist_head_t head;\n\tlist_t entries[NENTRIES];\n\tunsigned i;\n\n\tql_new(&head);\n\tinit_entries(entries, sizeof(entries)/sizeof(list_t));\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tql_tail_insert(&head, &entries[i], link);\n\t}\n\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\ttest_entries_list(&head, entries, NENTRIES-i);\n\t\tql_tail_remove(&head, list_t, link);\n\t}\n\ttest_empty_list(&head);\n}\nTEST_END\n\nTEST_BEGIN(test_ql_head_insert) {\n\tlist_head_t head;\n\tlist_t entries[NENTRIES];\n\tunsigned i;\n\n\tql_new(&head);\n\tinit_entries(entries, sizeof(entries)/sizeof(list_t));\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tql_head_insert(&head, &entries[NENTRIES-i-1], link);\n\t}\n\n\ttest_entries_list(&head, entries, NENTRIES);\n}\nTEST_END\n\nTEST_BEGIN(test_ql_head_remove) {\n\tlist_head_t head;\n\tlist_t entries[NENTRIES];\n\tunsigned i;\n\n\tql_new(&head);\n\tinit_entries(entries, sizeof(entries)/sizeof(list_t));\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tql_head_insert(&head, &entries[NENTRIES-i-1], link);\n\t}\n\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\ttest_entries_list(&head, &entries[i], NENTRIES-i);\n\t\tql_head_remove(&head, list_t, link);\n\t}\n\ttest_empty_list(&head);\n}\nTEST_END\n\nTEST_BEGIN(test_ql_insert) {\n\tlist_head_t head;\n\tlist_t entries[8];\n\tlist_t *a, *b, *c, *d, *e, *f, *g, *h;\n\n\tql_new(&head);\n\tinit_entries(entries, sizeof(entries)/sizeof(list_t));\n\ta = &entries[0];\n\tb = &entries[1];\n\tc = &entries[2];\n\td = &entries[3];\n\te = &entries[4];\n\tf = &entries[5];\n\tg = &entries[6];\n\th = &entries[7];\n\n\t/*\n\t * ql_remove(), ql_before_insert(), and ql_after_insert() are used\n\t * internally by other macros that are already tested, so there's no\n\t * need to test them completely.  However, insertion/deletion from the\n\t * middle of lists is not otherwise tested; do so here.\n\t */\n\tql_tail_insert(&head, f, link);\n\tql_before_insert(&head, f, b, link);\n\tql_before_insert(&head, f, c, link);\n\tql_after_insert(f, h, link);\n\tql_after_insert(f, g, link);\n\tql_before_insert(&head, b, a, link);\n\tql_after_insert(c, d, link);\n\tql_before_insert(&head, f, e, link);\n\n\ttest_entries_list(&head, entries, sizeof(entries)/sizeof(list_t));\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_ql_empty,\n\t    test_ql_tail_insert,\n\t    test_ql_tail_remove,\n\t    test_ql_head_insert,\n\t    test_ql_head_remove,\n\t    test_ql_insert);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/qr.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/qr.h\"\n\n/* Number of ring entries, in [2..26]. */\n#define NENTRIES 9\n/* Split index, in [1..NENTRIES). */\n#define SPLIT_INDEX 5\n\ntypedef struct ring_s ring_t;\n\nstruct ring_s {\n\tqr(ring_t) link;\n\tchar id;\n};\n\nstatic void\ninit_entries(ring_t *entries) {\n\tunsigned i;\n\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tqr_new(&entries[i], link);\n\t\tentries[i].id = 'a' + i;\n\t}\n}\n\nstatic void\ntest_independent_entries(ring_t *entries) {\n\tring_t *t;\n\tunsigned i, j;\n\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tj = 0;\n\t\tqr_foreach(t, &entries[i], link) {\n\t\t\tj++;\n\t\t}\n\t\tassert_u_eq(j, 1,\n\t\t    \"Iteration over single-element ring should visit precisely \"\n\t\t    \"one element\");\n\t}\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tj = 0;\n\t\tqr_reverse_foreach(t, &entries[i], link) {\n\t\t\tj++;\n\t\t}\n\t\tassert_u_eq(j, 1,\n\t\t    \"Iteration over single-element ring should visit precisely \"\n\t\t    \"one element\");\n\t}\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tt = qr_next(&entries[i], link);\n\t\tassert_ptr_eq(t, &entries[i],\n\t\t    \"Next element in single-element ring should be same as \"\n\t\t    \"current element\");\n\t}\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tt = qr_prev(&entries[i], link);\n\t\tassert_ptr_eq(t, &entries[i],\n\t\t    \"Previous element in single-element ring should be same as \"\n\t\t    \"current element\");\n\t}\n}\n\nTEST_BEGIN(test_qr_one) {\n\tring_t entries[NENTRIES];\n\n\tinit_entries(entries);\n\ttest_independent_entries(entries);\n}\nTEST_END\n\nstatic void\ntest_entries_ring(ring_t *entries) {\n\tring_t *t;\n\tunsigned i, j;\n\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tj = 0;\n\t\tqr_foreach(t, &entries[i], link) {\n\t\t\tassert_c_eq(t->id, entries[(i+j) % NENTRIES].id,\n\t\t\t    \"Element id mismatch\");\n\t\t\tj++;\n\t\t}\n\t}\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tj = 0;\n\t\tqr_reverse_foreach(t, &entries[i], link) {\n\t\t\tassert_c_eq(t->id, entries[(NENTRIES+i-j-1) %\n\t\t\t    NENTRIES].id, \"Element id mismatch\");\n\t\t\tj++;\n\t\t}\n\t}\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tt = qr_next(&entries[i], link);\n\t\tassert_c_eq(t->id, entries[(i+1) % NENTRIES].id,\n\t\t    \"Element id mismatch\");\n\t}\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tt = qr_prev(&entries[i], link);\n\t\tassert_c_eq(t->id, entries[(NENTRIES+i-1) % NENTRIES].id,\n\t\t    \"Element id mismatch\");\n\t}\n}\n\nTEST_BEGIN(test_qr_after_insert) {\n\tring_t entries[NENTRIES];\n\tunsigned i;\n\n\tinit_entries(entries);\n\tfor (i = 1; i < NENTRIES; i++) {\n\t\tqr_after_insert(&entries[i - 1], &entries[i], link);\n\t}\n\ttest_entries_ring(entries);\n}\nTEST_END\n\nTEST_BEGIN(test_qr_remove) {\n\tring_t entries[NENTRIES];\n\tring_t *t;\n\tunsigned i, j;\n\n\tinit_entries(entries);\n\tfor (i = 1; i < NENTRIES; i++) {\n\t\tqr_after_insert(&entries[i - 1], &entries[i], link);\n\t}\n\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tj = 0;\n\t\tqr_foreach(t, &entries[i], link) {\n\t\t\tassert_c_eq(t->id, entries[i+j].id,\n\t\t\t    \"Element id mismatch\");\n\t\t\tj++;\n\t\t}\n\t\tj = 0;\n\t\tqr_reverse_foreach(t, &entries[i], link) {\n\t\t\tassert_c_eq(t->id, entries[NENTRIES - 1 - j].id,\n\t\t\t\"Element id mismatch\");\n\t\t\tj++;\n\t\t}\n\t\tqr_remove(&entries[i], link);\n\t}\n\ttest_independent_entries(entries);\n}\nTEST_END\n\nTEST_BEGIN(test_qr_before_insert) {\n\tring_t entries[NENTRIES];\n\tring_t *t;\n\tunsigned i, j;\n\n\tinit_entries(entries);\n\tfor (i = 1; i < NENTRIES; i++) {\n\t\tqr_before_insert(&entries[i - 1], &entries[i], link);\n\t}\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tj = 0;\n\t\tqr_foreach(t, &entries[i], link) {\n\t\t\tassert_c_eq(t->id, entries[(NENTRIES+i-j) %\n\t\t\t    NENTRIES].id, \"Element id mismatch\");\n\t\t\tj++;\n\t\t}\n\t}\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tj = 0;\n\t\tqr_reverse_foreach(t, &entries[i], link) {\n\t\t\tassert_c_eq(t->id, entries[(i+j+1) % NENTRIES].id,\n\t\t\t    \"Element id mismatch\");\n\t\t\tj++;\n\t\t}\n\t}\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tt = qr_next(&entries[i], link);\n\t\tassert_c_eq(t->id, entries[(NENTRIES+i-1) % NENTRIES].id,\n\t\t    \"Element id mismatch\");\n\t}\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tt = qr_prev(&entries[i], link);\n\t\tassert_c_eq(t->id, entries[(i+1) % NENTRIES].id,\n\t\t    \"Element id mismatch\");\n\t}\n}\nTEST_END\n\nstatic void\ntest_split_entries(ring_t *entries) {\n\tring_t *t;\n\tunsigned i, j;\n\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tj = 0;\n\t\tqr_foreach(t, &entries[i], link) {\n\t\t\tif (i < SPLIT_INDEX) {\n\t\t\t\tassert_c_eq(t->id,\n\t\t\t\t    entries[(i+j) % SPLIT_INDEX].id,\n\t\t\t\t    \"Element id mismatch\");\n\t\t\t} else {\n\t\t\t\tassert_c_eq(t->id, entries[(i+j-SPLIT_INDEX) %\n\t\t\t\t    (NENTRIES-SPLIT_INDEX) + SPLIT_INDEX].id,\n\t\t\t\t    \"Element id mismatch\");\n\t\t\t}\n\t\t\tj++;\n\t\t}\n\t}\n}\n\nTEST_BEGIN(test_qr_meld_split) {\n\tring_t entries[NENTRIES];\n\tunsigned i;\n\n\tinit_entries(entries);\n\tfor (i = 1; i < NENTRIES; i++) {\n\t\tqr_after_insert(&entries[i - 1], &entries[i], link);\n\t}\n\n\tqr_split(&entries[0], &entries[SPLIT_INDEX], ring_t, link);\n\ttest_split_entries(entries);\n\n\tqr_meld(&entries[0], &entries[SPLIT_INDEX], ring_t, link);\n\ttest_entries_ring(entries);\n\n\tqr_meld(&entries[0], &entries[SPLIT_INDEX], ring_t, link);\n\ttest_split_entries(entries);\n\n\tqr_split(&entries[0], &entries[SPLIT_INDEX], ring_t, link);\n\ttest_entries_ring(entries);\n\n\tqr_split(&entries[0], &entries[0], ring_t, link);\n\ttest_entries_ring(entries);\n\n\tqr_meld(&entries[0], &entries[0], ring_t, link);\n\ttest_entries_ring(entries);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_qr_one,\n\t    test_qr_after_insert,\n\t    test_qr_remove,\n\t    test_qr_before_insert,\n\t    test_qr_meld_split);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/rb.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/rb.h\"\n\n#define rbtn_black_height(a_type, a_field, a_rbt, r_height) do {\t\\\n\ta_type *rbp_bh_t;\t\t\t\t\t\t\\\n\tfor (rbp_bh_t = (a_rbt)->rbt_root, (r_height) = 0; rbp_bh_t !=\t\\\n\t    NULL; rbp_bh_t = rbtn_left_get(a_type, a_field,\t\t\\\n\t    rbp_bh_t)) {\t\t\t\t\t\t\\\n\t\tif (!rbtn_red_get(a_type, a_field, rbp_bh_t)) {\t\t\\\n\t\t(r_height)++;\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\ntypedef struct node_s node_t;\n\nstruct node_s {\n#define NODE_MAGIC 0x9823af7e\n\tuint32_t magic;\n\trb_node(node_t) link;\n\tuint64_t key;\n};\n\nstatic int\nnode_cmp(const node_t *a, const node_t *b) {\n\tint ret;\n\n\tassert_u32_eq(a->magic, NODE_MAGIC, \"Bad magic\");\n\tassert_u32_eq(b->magic, NODE_MAGIC, \"Bad magic\");\n\n\tret = (a->key > b->key) - (a->key < b->key);\n\tif (ret == 0) {\n\t\t/*\n\t\t * Duplicates are not allowed in the tree, so force an\n\t\t * arbitrary ordering for non-identical items with equal keys.\n\t\t */\n\t\tret = (((uintptr_t)a) > ((uintptr_t)b))\n\t\t    - (((uintptr_t)a) < ((uintptr_t)b));\n\t}\n\treturn ret;\n}\n\ntypedef rb_tree(node_t) tree_t;\nrb_gen(static, tree_, tree_t, node_t, link, node_cmp);\n\nTEST_BEGIN(test_rb_empty) {\n\ttree_t tree;\n\tnode_t key;\n\n\ttree_new(&tree);\n\n\tassert_true(tree_empty(&tree), \"Tree should be empty\");\n\tassert_ptr_null(tree_first(&tree), \"Unexpected node\");\n\tassert_ptr_null(tree_last(&tree), \"Unexpected node\");\n\n\tkey.key = 0;\n\tkey.magic = NODE_MAGIC;\n\tassert_ptr_null(tree_search(&tree, &key), \"Unexpected node\");\n\n\tkey.key = 0;\n\tkey.magic = NODE_MAGIC;\n\tassert_ptr_null(tree_nsearch(&tree, &key), \"Unexpected node\");\n\n\tkey.key = 0;\n\tkey.magic = NODE_MAGIC;\n\tassert_ptr_null(tree_psearch(&tree, &key), \"Unexpected node\");\n}\nTEST_END\n\nstatic unsigned\ntree_recurse(node_t *node, unsigned black_height, unsigned black_depth) {\n\tunsigned ret = 0;\n\tnode_t *left_node;\n\tnode_t *right_node;\n\n\tif (node == NULL) {\n\t\treturn ret;\n\t}\n\n\tleft_node = rbtn_left_get(node_t, link, node);\n\tright_node = rbtn_right_get(node_t, link, node);\n\n\tif (!rbtn_red_get(node_t, link, node)) {\n\t\tblack_depth++;\n\t}\n\n\t/* Red nodes must be interleaved with black nodes. */\n\tif (rbtn_red_get(node_t, link, node)) {\n\t\tif (left_node != NULL) {\n\t\t\tassert_false(rbtn_red_get(node_t, link, left_node),\n\t\t\t\t\"Node should be black\");\n\t\t}\n\t\tif (right_node != NULL) {\n\t\t\tassert_false(rbtn_red_get(node_t, link, right_node),\n\t\t\t    \"Node should be black\");\n\t\t}\n\t}\n\n\t/* Self. */\n\tassert_u32_eq(node->magic, NODE_MAGIC, \"Bad magic\");\n\n\t/* Left subtree. */\n\tif (left_node != NULL) {\n\t\tret += tree_recurse(left_node, black_height, black_depth);\n\t} else {\n\t\tret += (black_depth != black_height);\n\t}\n\n\t/* Right subtree. */\n\tif (right_node != NULL) {\n\t\tret += tree_recurse(right_node, black_height, black_depth);\n\t} else {\n\t\tret += (black_depth != black_height);\n\t}\n\n\treturn ret;\n}\n\nstatic node_t *\ntree_iterate_cb(tree_t *tree, node_t *node, void *data) {\n\tunsigned *i = (unsigned *)data;\n\tnode_t *search_node;\n\n\tassert_u32_eq(node->magic, NODE_MAGIC, \"Bad magic\");\n\n\t/* Test rb_search(). */\n\tsearch_node = tree_search(tree, node);\n\tassert_ptr_eq(search_node, node,\n\t    \"tree_search() returned unexpected node\");\n\n\t/* Test rb_nsearch(). */\n\tsearch_node = tree_nsearch(tree, node);\n\tassert_ptr_eq(search_node, node,\n\t    \"tree_nsearch() returned unexpected node\");\n\n\t/* Test rb_psearch(). */\n\tsearch_node = tree_psearch(tree, node);\n\tassert_ptr_eq(search_node, node,\n\t    \"tree_psearch() returned unexpected node\");\n\n\t(*i)++;\n\n\treturn NULL;\n}\n\nstatic unsigned\ntree_iterate(tree_t *tree) {\n\tunsigned i;\n\n\ti = 0;\n\ttree_iter(tree, NULL, tree_iterate_cb, (void *)&i);\n\n\treturn i;\n}\n\nstatic unsigned\ntree_iterate_reverse(tree_t *tree) {\n\tunsigned i;\n\n\ti = 0;\n\ttree_reverse_iter(tree, NULL, tree_iterate_cb, (void *)&i);\n\n\treturn i;\n}\n\nstatic void\nnode_remove(tree_t *tree, node_t *node, unsigned nnodes) {\n\tnode_t *search_node;\n\tunsigned black_height, imbalances;\n\n\ttree_remove(tree, node);\n\n\t/* Test rb_nsearch(). */\n\tsearch_node = tree_nsearch(tree, node);\n\tif (search_node != NULL) {\n\t\tassert_u64_ge(search_node->key, node->key,\n\t\t    \"Key ordering error\");\n\t}\n\n\t/* Test rb_psearch(). */\n\tsearch_node = tree_psearch(tree, node);\n\tif (search_node != NULL) {\n\t\tassert_u64_le(search_node->key, node->key,\n\t\t    \"Key ordering error\");\n\t}\n\n\tnode->magic = 0;\n\n\trbtn_black_height(node_t, link, tree, black_height);\n\timbalances = tree_recurse(tree->rbt_root, black_height, 0);\n\tassert_u_eq(imbalances, 0, \"Tree is unbalanced\");\n\tassert_u_eq(tree_iterate(tree), nnodes-1,\n\t    \"Unexpected node iteration count\");\n\tassert_u_eq(tree_iterate_reverse(tree), nnodes-1,\n\t    \"Unexpected node iteration count\");\n}\n\nstatic node_t *\nremove_iterate_cb(tree_t *tree, node_t *node, void *data) {\n\tunsigned *nnodes = (unsigned *)data;\n\tnode_t *ret = tree_next(tree, node);\n\n\tnode_remove(tree, node, *nnodes);\n\n\treturn ret;\n}\n\nstatic node_t *\nremove_reverse_iterate_cb(tree_t *tree, node_t *node, void *data) {\n\tunsigned *nnodes = (unsigned *)data;\n\tnode_t *ret = tree_prev(tree, node);\n\n\tnode_remove(tree, node, *nnodes);\n\n\treturn ret;\n}\n\nstatic void\ndestroy_cb(node_t *node, void *data) {\n\tunsigned *nnodes = (unsigned *)data;\n\n\tassert_u_gt(*nnodes, 0, \"Destruction removed too many nodes\");\n\t(*nnodes)--;\n}\n\nTEST_BEGIN(test_rb_random) {\n#define NNODES 25\n#define NBAGS 250\n#define SEED 42\n\tsfmt_t *sfmt;\n\tuint64_t bag[NNODES];\n\ttree_t tree;\n\tnode_t nodes[NNODES];\n\tunsigned i, j, k, black_height, imbalances;\n\n\tsfmt = init_gen_rand(SEED);\n\tfor (i = 0; i < NBAGS; i++) {\n\t\tswitch (i) {\n\t\tcase 0:\n\t\t\t/* Insert in order. */\n\t\t\tfor (j = 0; j < NNODES; j++) {\n\t\t\t\tbag[j] = j;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase 1:\n\t\t\t/* Insert in reverse order. */\n\t\t\tfor (j = 0; j < NNODES; j++) {\n\t\t\t\tbag[j] = NNODES - j - 1;\n\t\t\t}\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tfor (j = 0; j < NNODES; j++) {\n\t\t\t\tbag[j] = gen_rand64_range(sfmt, NNODES);\n\t\t\t}\n\t\t}\n\n\t\tfor (j = 1; j <= NNODES; j++) {\n\t\t\t/* Initialize tree and nodes. */\n\t\t\ttree_new(&tree);\n\t\t\tfor (k = 0; k < j; k++) {\n\t\t\t\tnodes[k].magic = NODE_MAGIC;\n\t\t\t\tnodes[k].key = bag[k];\n\t\t\t}\n\n\t\t\t/* Insert nodes. */\n\t\t\tfor (k = 0; k < j; k++) {\n\t\t\t\ttree_insert(&tree, &nodes[k]);\n\n\t\t\t\trbtn_black_height(node_t, link, &tree,\n\t\t\t\t    black_height);\n\t\t\t\timbalances = tree_recurse(tree.rbt_root,\n\t\t\t\t    black_height, 0);\n\t\t\t\tassert_u_eq(imbalances, 0,\n\t\t\t\t    \"Tree is unbalanced\");\n\n\t\t\t\tassert_u_eq(tree_iterate(&tree), k+1,\n\t\t\t\t    \"Unexpected node iteration count\");\n\t\t\t\tassert_u_eq(tree_iterate_reverse(&tree), k+1,\n\t\t\t\t    \"Unexpected node iteration count\");\n\n\t\t\t\tassert_false(tree_empty(&tree),\n\t\t\t\t    \"Tree should not be empty\");\n\t\t\t\tassert_ptr_not_null(tree_first(&tree),\n\t\t\t\t    \"Tree should not be empty\");\n\t\t\t\tassert_ptr_not_null(tree_last(&tree),\n\t\t\t\t    \"Tree should not be empty\");\n\n\t\t\t\ttree_next(&tree, &nodes[k]);\n\t\t\t\ttree_prev(&tree, &nodes[k]);\n\t\t\t}\n\n\t\t\t/* Remove nodes. */\n\t\t\tswitch (i % 5) {\n\t\t\tcase 0:\n\t\t\t\tfor (k = 0; k < j; k++) {\n\t\t\t\t\tnode_remove(&tree, &nodes[k], j - k);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase 1:\n\t\t\t\tfor (k = j; k > 0; k--) {\n\t\t\t\t\tnode_remove(&tree, &nodes[k-1], k);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase 2: {\n\t\t\t\tnode_t *start;\n\t\t\t\tunsigned nnodes = j;\n\n\t\t\t\tstart = NULL;\n\t\t\t\tdo {\n\t\t\t\t\tstart = tree_iter(&tree, start,\n\t\t\t\t\t    remove_iterate_cb, (void *)&nnodes);\n\t\t\t\t\tnnodes--;\n\t\t\t\t} while (start != NULL);\n\t\t\t\tassert_u_eq(nnodes, 0,\n\t\t\t\t    \"Removal terminated early\");\n\t\t\t\tbreak;\n\t\t\t} case 3: {\n\t\t\t\tnode_t *start;\n\t\t\t\tunsigned nnodes = j;\n\n\t\t\t\tstart = NULL;\n\t\t\t\tdo {\n\t\t\t\t\tstart = tree_reverse_iter(&tree, start,\n\t\t\t\t\t    remove_reverse_iterate_cb,\n\t\t\t\t\t    (void *)&nnodes);\n\t\t\t\t\tnnodes--;\n\t\t\t\t} while (start != NULL);\n\t\t\t\tassert_u_eq(nnodes, 0,\n\t\t\t\t    \"Removal terminated early\");\n\t\t\t\tbreak;\n\t\t\t} case 4: {\n\t\t\t\tunsigned nnodes = j;\n\t\t\t\ttree_destroy(&tree, destroy_cb, &nnodes);\n\t\t\t\tassert_u_eq(nnodes, 0,\n\t\t\t\t    \"Destruction terminated early\");\n\t\t\t\tbreak;\n\t\t\t} default:\n\t\t\t\tnot_reached();\n\t\t\t}\n\t\t}\n\t}\n\tfini_gen_rand(sfmt);\n#undef NNODES\n#undef NBAGS\n#undef SEED\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_rb_empty,\n\t    test_rb_random);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/retained.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/spin.h\"\n\nstatic unsigned\t\tarena_ind;\nstatic size_t\t\tsz;\nstatic size_t\t\tesz;\n#define NEPOCHS\t\t8\n#define PER_THD_NALLOCS\t1\nstatic atomic_u_t\tepoch;\nstatic atomic_u_t\tnfinished;\n\nstatic unsigned\ndo_arena_create(extent_hooks_t *h) {\n\tunsigned arena_ind;\n\tsize_t sz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena_ind, &sz,\n\t    (void *)(h != NULL ? &h : NULL), (h != NULL ? sizeof(h) : 0)), 0,\n\t    \"Unexpected mallctl() failure\");\n\treturn arena_ind;\n}\n\nstatic void\ndo_arena_destroy(unsigned arena_ind) {\n\tsize_t mib[3];\n\tsize_t miblen;\n\n\tmiblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"arena.0.destroy\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tmib[1] = (size_t)arena_ind;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n}\n\nstatic void\ndo_refresh(void) {\n\tuint64_t epoch = 1;\n\tassert_d_eq(mallctl(\"epoch\", NULL, NULL, (void *)&epoch,\n\t    sizeof(epoch)), 0, \"Unexpected mallctl() failure\");\n}\n\nstatic size_t\ndo_get_size_impl(const char *cmd, unsigned arena_ind) {\n\tsize_t mib[4];\n\tsize_t miblen = sizeof(mib) / sizeof(size_t);\n\tsize_t z = sizeof(size_t);\n\n\tassert_d_eq(mallctlnametomib(cmd, mib, &miblen),\n\t    0, \"Unexpected mallctlnametomib(\\\"%s\\\", ...) failure\", cmd);\n\tmib[2] = arena_ind;\n\tsize_t size;\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&size, &z, NULL, 0),\n\t    0, \"Unexpected mallctlbymib([\\\"%s\\\"], ...) failure\", cmd);\n\n\treturn size;\n}\n\nstatic size_t\ndo_get_active(unsigned arena_ind) {\n\treturn do_get_size_impl(\"stats.arenas.0.pactive\", arena_ind) * PAGE;\n}\n\nstatic size_t\ndo_get_mapped(unsigned arena_ind) {\n\treturn do_get_size_impl(\"stats.arenas.0.mapped\", arena_ind);\n}\n\nstatic void *\nthd_start(void *arg) {\n\tfor (unsigned next_epoch = 1; next_epoch < NEPOCHS; next_epoch++) {\n\t\t/* Busy-wait for next epoch. */\n\t\tunsigned cur_epoch;\n\t\tspin_t spinner = SPIN_INITIALIZER;\n\t\twhile ((cur_epoch = atomic_load_u(&epoch, ATOMIC_ACQUIRE)) !=\n\t\t    next_epoch) {\n\t\t\tspin_adaptive(&spinner);\n\t\t}\n\t\tassert_u_eq(cur_epoch, next_epoch, \"Unexpected epoch\");\n\n\t\t/*\n\t\t * Allocate.  The main thread will reset the arena, so there's\n\t\t * no need to deallocate.\n\t\t */\n\t\tfor (unsigned i = 0; i < PER_THD_NALLOCS; i++) {\n\t\t\tvoid *p = mallocx(sz, MALLOCX_ARENA(arena_ind) |\n\t\t\t    MALLOCX_TCACHE_NONE\n\t\t\t    );\n\t\t\tassert_ptr_not_null(p,\n\t\t\t    \"Unexpected mallocx() failure\\n\");\n\t\t}\n\n\t\t/* Let the main thread know we've finished this iteration. */\n\t\tatomic_fetch_add_u(&nfinished, 1, ATOMIC_RELEASE);\n\t}\n\n\treturn NULL;\n}\n\nTEST_BEGIN(test_retained) {\n\ttest_skip_if(!config_stats);\n\n\tarena_ind = do_arena_create(NULL);\n\tsz = nallocx(HUGEPAGE, 0);\n\tesz = sz + sz_large_pad;\n\n\tatomic_store_u(&epoch, 0, ATOMIC_RELAXED);\n\n\tunsigned nthreads = ncpus * 2;\n\tif (LG_SIZEOF_PTR < 3 && nthreads > 16) {\n\t\tnthreads = 16; /* 32-bit platform could run out of vaddr. */\n\t}\n\tVARIABLE_ARRAY(thd_t, threads, nthreads);\n\tfor (unsigned i = 0; i < nthreads; i++) {\n\t\tthd_create(&threads[i], thd_start, NULL);\n\t}\n\n\tfor (unsigned e = 1; e < NEPOCHS; e++) {\n\t\tatomic_store_u(&nfinished, 0, ATOMIC_RELEASE);\n\t\tatomic_store_u(&epoch, e, ATOMIC_RELEASE);\n\n\t\t/* Wait for threads to finish allocating. */\n\t\tspin_t spinner = SPIN_INITIALIZER;\n\t\twhile (atomic_load_u(&nfinished, ATOMIC_ACQUIRE) < nthreads) {\n\t\t\tspin_adaptive(&spinner);\n\t\t}\n\n\t\t/*\n\t\t * Assert that retained is no more than the sum of size classes\n\t\t * that should have been used to satisfy the worker threads'\n\t\t * requests, discounting per growth fragmentation.\n\t\t */\n\t\tdo_refresh();\n\n\t\tsize_t allocated = esz * nthreads * PER_THD_NALLOCS;\n\t\tsize_t active = do_get_active(arena_ind);\n\t\tassert_zu_le(allocated, active, \"Unexpected active memory\");\n\t\tsize_t mapped = do_get_mapped(arena_ind);\n\t\tassert_zu_le(active, mapped, \"Unexpected mapped memory\");\n\n\t\tarena_t *arena = arena_get(tsdn_fetch(), arena_ind, false);\n\t\tsize_t usable = 0;\n\t\tsize_t fragmented = 0;\n\t\tfor (pszind_t pind = sz_psz2ind(HUGEPAGE); pind <\n\t\t    arena->extent_grow_next; pind++) {\n\t\t\tsize_t psz = sz_pind2sz(pind);\n\t\t\tsize_t psz_fragmented = psz % esz;\n\t\t\tsize_t psz_usable = psz - psz_fragmented;\n\t\t\t/*\n\t\t\t * Only consider size classes that wouldn't be skipped.\n\t\t\t */\n\t\t\tif (psz_usable > 0) {\n\t\t\t\tassert_zu_lt(usable, allocated,\n\t\t\t\t    \"Excessive retained memory \"\n\t\t\t\t    \"(%#zx[+%#zx] > %#zx)\", usable, psz_usable,\n\t\t\t\t    allocated);\n\t\t\t\tfragmented += psz_fragmented;\n\t\t\t\tusable += psz_usable;\n\t\t\t}\n\t\t}\n\n\t\t/*\n\t\t * Clean up arena.  Destroying and recreating the arena\n\t\t * is simpler that specifying extent hooks that deallocate\n\t\t * (rather than retaining) during reset.\n\t\t */\n\t\tdo_arena_destroy(arena_ind);\n\t\tassert_u_eq(do_arena_create(NULL), arena_ind,\n\t\t    \"Unexpected arena index\");\n\t}\n\n\tfor (unsigned i = 0; i < nthreads; i++) {\n\t\tthd_join(threads[i], NULL);\n\t}\n\n\tdo_arena_destroy(arena_ind);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_retained);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/rtree.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/rtree.h\"\n\nrtree_node_alloc_t *rtree_node_alloc_orig;\nrtree_node_dalloc_t *rtree_node_dalloc_orig;\nrtree_leaf_alloc_t *rtree_leaf_alloc_orig;\nrtree_leaf_dalloc_t *rtree_leaf_dalloc_orig;\n\n/* Potentially too large to safely place on the stack. */\nrtree_t test_rtree;\n\nstatic rtree_node_elm_t *\nrtree_node_alloc_intercept(tsdn_t *tsdn, rtree_t *rtree, size_t nelms) {\n\trtree_node_elm_t *node;\n\n\tif (rtree != &test_rtree) {\n\t\treturn rtree_node_alloc_orig(tsdn, rtree, nelms);\n\t}\n\n\tmalloc_mutex_unlock(tsdn, &rtree->init_lock);\n\tnode = (rtree_node_elm_t *)calloc(nelms, sizeof(rtree_node_elm_t));\n\tassert_ptr_not_null(node, \"Unexpected calloc() failure\");\n\tmalloc_mutex_lock(tsdn, &rtree->init_lock);\n\n\treturn node;\n}\n\nstatic void\nrtree_node_dalloc_intercept(tsdn_t *tsdn, rtree_t *rtree,\n    rtree_node_elm_t *node) {\n\tif (rtree != &test_rtree) {\n\t\trtree_node_dalloc_orig(tsdn, rtree, node);\n\t\treturn;\n\t}\n\n\tfree(node);\n}\n\nstatic rtree_leaf_elm_t *\nrtree_leaf_alloc_intercept(tsdn_t *tsdn, rtree_t *rtree, size_t nelms) {\n\trtree_leaf_elm_t *leaf;\n\n\tif (rtree != &test_rtree) {\n\t\treturn rtree_leaf_alloc_orig(tsdn, rtree, nelms);\n\t}\n\n\tmalloc_mutex_unlock(tsdn, &rtree->init_lock);\n\tleaf = (rtree_leaf_elm_t *)calloc(nelms, sizeof(rtree_leaf_elm_t));\n\tassert_ptr_not_null(leaf, \"Unexpected calloc() failure\");\n\tmalloc_mutex_lock(tsdn, &rtree->init_lock);\n\n\treturn leaf;\n}\n\nstatic void\nrtree_leaf_dalloc_intercept(tsdn_t *tsdn, rtree_t *rtree,\n    rtree_leaf_elm_t *leaf) {\n\tif (rtree != &test_rtree) {\n\t\trtree_leaf_dalloc_orig(tsdn, rtree, leaf);\n\t\treturn;\n\t}\n\n\tfree(leaf);\n}\n\nTEST_BEGIN(test_rtree_read_empty) {\n\ttsdn_t *tsdn;\n\n\ttsdn = tsdn_fetch();\n\n\trtree_t *rtree = &test_rtree;\n\trtree_ctx_t rtree_ctx;\n\trtree_ctx_data_init(&rtree_ctx);\n\tassert_false(rtree_new(rtree, false), \"Unexpected rtree_new() failure\");\n\tassert_ptr_null(rtree_extent_read(tsdn, rtree, &rtree_ctx, PAGE,\n\t    false), \"rtree_extent_read() should return NULL for empty tree\");\n\trtree_delete(tsdn, rtree);\n}\nTEST_END\n\n#undef NTHREADS\n#undef NITERS\n#undef SEED\n\nTEST_BEGIN(test_rtree_extrema) {\n\textent_t extent_a, extent_b;\n\textent_init(&extent_a, NULL, NULL, SC_LARGE_MINCLASS, false,\n\t    sz_size2index(SC_LARGE_MINCLASS), 0,\n\t    extent_state_active, false, false, true, EXTENT_NOT_HEAD);\n\textent_init(&extent_b, NULL, NULL, 0, false, SC_NSIZES, 0,\n\t    extent_state_active, false, false, true, EXTENT_NOT_HEAD);\n\n\ttsdn_t *tsdn = tsdn_fetch();\n\n\trtree_t *rtree = &test_rtree;\n\trtree_ctx_t rtree_ctx;\n\trtree_ctx_data_init(&rtree_ctx);\n\tassert_false(rtree_new(rtree, false), \"Unexpected rtree_new() failure\");\n\n\tassert_false(rtree_write(tsdn, rtree, &rtree_ctx, PAGE, &extent_a,\n\t    extent_szind_get(&extent_a), extent_slab_get(&extent_a)),\n\t    \"Unexpected rtree_write() failure\");\n\trtree_szind_slab_update(tsdn, rtree, &rtree_ctx, PAGE,\n\t    extent_szind_get(&extent_a), extent_slab_get(&extent_a));\n\tassert_ptr_eq(rtree_extent_read(tsdn, rtree, &rtree_ctx, PAGE, true),\n\t    &extent_a,\n\t    \"rtree_extent_read() should return previously set value\");\n\n\tassert_false(rtree_write(tsdn, rtree, &rtree_ctx, ~((uintptr_t)0),\n\t    &extent_b, extent_szind_get_maybe_invalid(&extent_b),\n\t    extent_slab_get(&extent_b)), \"Unexpected rtree_write() failure\");\n\tassert_ptr_eq(rtree_extent_read(tsdn, rtree, &rtree_ctx,\n\t    ~((uintptr_t)0), true), &extent_b,\n\t    \"rtree_extent_read() should return previously set value\");\n\n\trtree_delete(tsdn, rtree);\n}\nTEST_END\n\nTEST_BEGIN(test_rtree_bits) {\n\ttsdn_t *tsdn = tsdn_fetch();\n\n\tuintptr_t keys[] = {PAGE, PAGE + 1,\n\t    PAGE + (((uintptr_t)1) << LG_PAGE) - 1};\n\n\textent_t extent;\n\textent_init(&extent, NULL, NULL, 0, false, SC_NSIZES, 0,\n\t    extent_state_active, false, false, true, EXTENT_NOT_HEAD);\n\n\trtree_t *rtree = &test_rtree;\n\trtree_ctx_t rtree_ctx;\n\trtree_ctx_data_init(&rtree_ctx);\n\tassert_false(rtree_new(rtree, false), \"Unexpected rtree_new() failure\");\n\n\tfor (unsigned i = 0; i < sizeof(keys)/sizeof(uintptr_t); i++) {\n\t\tassert_false(rtree_write(tsdn, rtree, &rtree_ctx, keys[i],\n\t\t    &extent, SC_NSIZES, false),\n\t\t    \"Unexpected rtree_write() failure\");\n\t\tfor (unsigned j = 0; j < sizeof(keys)/sizeof(uintptr_t); j++) {\n\t\t\tassert_ptr_eq(rtree_extent_read(tsdn, rtree, &rtree_ctx,\n\t\t\t    keys[j], true), &extent,\n\t\t\t    \"rtree_extent_read() should return previously set \"\n\t\t\t    \"value and ignore insignificant key bits; i=%u, \"\n\t\t\t    \"j=%u, set key=%#\"FMTxPTR\", get key=%#\"FMTxPTR, i,\n\t\t\t    j, keys[i], keys[j]);\n\t\t}\n\t\tassert_ptr_null(rtree_extent_read(tsdn, rtree, &rtree_ctx,\n\t\t    (((uintptr_t)2) << LG_PAGE), false),\n\t\t    \"Only leftmost rtree leaf should be set; i=%u\", i);\n\t\trtree_clear(tsdn, rtree, &rtree_ctx, keys[i]);\n\t}\n\n\trtree_delete(tsdn, rtree);\n}\nTEST_END\n\nTEST_BEGIN(test_rtree_random) {\n#define NSET 16\n#define SEED 42\n\tsfmt_t *sfmt = init_gen_rand(SEED);\n\ttsdn_t *tsdn = tsdn_fetch();\n\tuintptr_t keys[NSET];\n\trtree_t *rtree = &test_rtree;\n\trtree_ctx_t rtree_ctx;\n\trtree_ctx_data_init(&rtree_ctx);\n\n\textent_t extent;\n\textent_init(&extent, NULL, NULL, 0, false, SC_NSIZES, 0,\n\t    extent_state_active, false, false, true, EXTENT_NOT_HEAD);\n\n\tassert_false(rtree_new(rtree, false), \"Unexpected rtree_new() failure\");\n\n\tfor (unsigned i = 0; i < NSET; i++) {\n\t\tkeys[i] = (uintptr_t)gen_rand64(sfmt);\n\t\trtree_leaf_elm_t *elm = rtree_leaf_elm_lookup(tsdn, rtree,\n\t\t    &rtree_ctx, keys[i], false, true);\n\t\tassert_ptr_not_null(elm,\n\t\t    \"Unexpected rtree_leaf_elm_lookup() failure\");\n\t\trtree_leaf_elm_write(tsdn, rtree, elm, &extent, SC_NSIZES,\n\t\t    false);\n\t\tassert_ptr_eq(rtree_extent_read(tsdn, rtree, &rtree_ctx,\n\t\t    keys[i], true), &extent,\n\t\t    \"rtree_extent_read() should return previously set value\");\n\t}\n\tfor (unsigned i = 0; i < NSET; i++) {\n\t\tassert_ptr_eq(rtree_extent_read(tsdn, rtree, &rtree_ctx,\n\t\t    keys[i], true), &extent,\n\t\t    \"rtree_extent_read() should return previously set value, \"\n\t\t    \"i=%u\", i);\n\t}\n\n\tfor (unsigned i = 0; i < NSET; i++) {\n\t\trtree_clear(tsdn, rtree, &rtree_ctx, keys[i]);\n\t\tassert_ptr_null(rtree_extent_read(tsdn, rtree, &rtree_ctx,\n\t\t    keys[i], true),\n\t\t   \"rtree_extent_read() should return previously set value\");\n\t}\n\tfor (unsigned i = 0; i < NSET; i++) {\n\t\tassert_ptr_null(rtree_extent_read(tsdn, rtree, &rtree_ctx,\n\t\t    keys[i], true),\n\t\t    \"rtree_extent_read() should return previously set value\");\n\t}\n\n\trtree_delete(tsdn, rtree);\n\tfini_gen_rand(sfmt);\n#undef NSET\n#undef SEED\n}\nTEST_END\n\nint\nmain(void) {\n\trtree_node_alloc_orig = rtree_node_alloc;\n\trtree_node_alloc = rtree_node_alloc_intercept;\n\trtree_node_dalloc_orig = rtree_node_dalloc;\n\trtree_node_dalloc = rtree_node_dalloc_intercept;\n\trtree_leaf_alloc_orig = rtree_leaf_alloc;\n\trtree_leaf_alloc = rtree_leaf_alloc_intercept;\n\trtree_leaf_dalloc_orig = rtree_leaf_dalloc;\n\trtree_leaf_dalloc = rtree_leaf_dalloc_intercept;\n\n\treturn test(\n\t    test_rtree_read_empty,\n\t    test_rtree_extrema,\n\t    test_rtree_bits,\n\t    test_rtree_random);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/safety_check.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/safety_check.h\"\n\n/*\n * Note that we get called through safety_check.sh, which turns on sampling for\n * everything.\n */\n\nbool fake_abort_called;\nvoid fake_abort(const char *message) {\n\t(void)message;\n\tfake_abort_called = true;\n}\n\nTEST_BEGIN(test_malloc_free_overflow) {\n\ttest_skip_if(!config_prof);\n\ttest_skip_if(!config_opt_safety_checks);\n\n\tsafety_check_set_abort(&fake_abort);\n\t/* Buffer overflow! */\n\tchar* ptr = malloc(128);\n\tptr[128] = 0;\n\tfree(ptr);\n\tsafety_check_set_abort(NULL);\n\n\tassert_b_eq(fake_abort_called, true, \"Redzone check didn't fire.\");\n\tfake_abort_called = false;\n}\nTEST_END\n\nTEST_BEGIN(test_mallocx_dallocx_overflow) {\n\ttest_skip_if(!config_prof);\n\ttest_skip_if(!config_opt_safety_checks);\n\n\tsafety_check_set_abort(&fake_abort);\n\t/* Buffer overflow! */\n\tchar* ptr = mallocx(128, 0);\n\tptr[128] = 0;\n\tdallocx(ptr, 0);\n\tsafety_check_set_abort(NULL);\n\n\tassert_b_eq(fake_abort_called, true, \"Redzone check didn't fire.\");\n\tfake_abort_called = false;\n}\nTEST_END\n\nTEST_BEGIN(test_malloc_sdallocx_overflow) {\n\ttest_skip_if(!config_prof);\n\ttest_skip_if(!config_opt_safety_checks);\n\n\tsafety_check_set_abort(&fake_abort);\n\t/* Buffer overflow! */\n\tchar* ptr = malloc(128);\n\tptr[128] = 0;\n\tsdallocx(ptr, 128, 0);\n\tsafety_check_set_abort(NULL);\n\n\tassert_b_eq(fake_abort_called, true, \"Redzone check didn't fire.\");\n\tfake_abort_called = false;\n}\nTEST_END\n\nTEST_BEGIN(test_realloc_overflow) {\n\ttest_skip_if(!config_prof);\n\ttest_skip_if(!config_opt_safety_checks);\n\n\tsafety_check_set_abort(&fake_abort);\n\t/* Buffer overflow! */\n\tchar* ptr = malloc(128);\n\tptr[128] = 0;\n\tptr = realloc(ptr, 129);\n\tsafety_check_set_abort(NULL);\n\tfree(ptr);\n\n\tassert_b_eq(fake_abort_called, true, \"Redzone check didn't fire.\");\n\tfake_abort_called = false;\n}\nTEST_END\n\nTEST_BEGIN(test_rallocx_overflow) {\n\ttest_skip_if(!config_prof);\n\ttest_skip_if(!config_opt_safety_checks);\n\n\tsafety_check_set_abort(&fake_abort);\n\t/* Buffer overflow! */\n\tchar* ptr = malloc(128);\n\tptr[128] = 0;\n\tptr = rallocx(ptr, 129, 0);\n\tsafety_check_set_abort(NULL);\n\tfree(ptr);\n\n\tassert_b_eq(fake_abort_called, true, \"Redzone check didn't fire.\");\n\tfake_abort_called = false;\n}\nTEST_END\n\nTEST_BEGIN(test_xallocx_overflow) {\n\ttest_skip_if(!config_prof);\n\ttest_skip_if(!config_opt_safety_checks);\n\n\tsafety_check_set_abort(&fake_abort);\n\t/* Buffer overflow! */\n\tchar* ptr = malloc(128);\n\tptr[128] = 0;\n\tsize_t result = xallocx(ptr, 129, 0, 0);\n\tassert_zu_eq(result, 128, \"\");\n\tfree(ptr);\n\tassert_b_eq(fake_abort_called, true, \"Redzone check didn't fire.\");\n\tfake_abort_called = false;\n\tsafety_check_set_abort(NULL);\n}\nTEST_END\n\nTEST_BEGIN(test_realloc_no_overflow) {\n\tchar* ptr = malloc(128);\n\tptr = realloc(ptr, 256);\n\tptr[128] = 0;\n\tptr[255] = 0;\n\tfree(ptr);\n\n\tptr = malloc(128);\n\tptr = realloc(ptr, 64);\n\tptr[63] = 0;\n\tptr[0] = 0;\n\tfree(ptr);\n}\nTEST_END\n\nTEST_BEGIN(test_rallocx_no_overflow) {\n\tchar* ptr = malloc(128);\n\tptr = rallocx(ptr, 256, 0);\n\tptr[128] = 0;\n\tptr[255] = 0;\n\tfree(ptr);\n\n\tptr = malloc(128);\n\tptr = rallocx(ptr, 64, 0);\n\tptr[63] = 0;\n\tptr[0] = 0;\n\tfree(ptr);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_malloc_free_overflow,\n\t    test_mallocx_dallocx_overflow,\n\t    test_malloc_sdallocx_overflow,\n\t    test_realloc_overflow,\n\t    test_rallocx_overflow,\n\t    test_xallocx_overflow,\n\t    test_realloc_no_overflow,\n\t    test_rallocx_no_overflow);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/safety_check.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_prof}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"prof:true,lg_prof_sample:0\"\nfi\n"
  },
  {
    "path": "deps/jemalloc/test/unit/sc.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nTEST_BEGIN(test_update_slab_size) {\n\tsc_data_t data;\n\tmemset(&data, 0, sizeof(data));\n\tsc_data_init(&data);\n\tsc_t *tiny = &data.sc[0];\n\tsize_t tiny_size = (ZU(1) << tiny->lg_base)\n\t    + (ZU(tiny->ndelta) << tiny->lg_delta);\n\tsize_t pgs_too_big = (tiny_size * BITMAP_MAXBITS + PAGE - 1) / PAGE + 1;\n\tsc_data_update_slab_size(&data, tiny_size, tiny_size, (int)pgs_too_big);\n\tassert_zu_lt((size_t)tiny->pgs, pgs_too_big, \"Allowed excessive pages\");\n\n\tsc_data_update_slab_size(&data, 1, 10 * PAGE, 1);\n\tfor (int i = 0; i < data.nbins; i++) {\n\t\tsc_t *sc = &data.sc[i];\n\t\tsize_t reg_size = (ZU(1) << sc->lg_base)\n\t\t    + (ZU(sc->ndelta) << sc->lg_delta);\n\t\tif (reg_size <= PAGE) {\n\t\t\tassert_d_eq(sc->pgs, 1, \"Ignored valid page size hint\");\n\t\t} else {\n\t\t\tassert_d_gt(sc->pgs, 1,\n\t\t\t    \"Allowed invalid page size hint\");\n\t\t}\n\t}\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_update_slab_size);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/seq.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/seq.h\"\n\ntypedef struct data_s data_t;\nstruct data_s {\n\tint arr[10];\n};\n\nstatic void\nset_data(data_t *data, int num) {\n\tfor (int i = 0; i < 10; i++) {\n\t\tdata->arr[i] = num;\n\t}\n}\n\nstatic void\nassert_data(data_t *data) {\n\tint num = data->arr[0];\n\tfor (int i = 0; i < 10; i++) {\n\t\tassert_d_eq(num, data->arr[i], \"Data consistency error\");\n\t}\n}\n\nseq_define(data_t, data)\n\ntypedef struct thd_data_s thd_data_t;\nstruct thd_data_s {\n\tseq_data_t data;\n};\n\nstatic void *\nseq_reader_thd(void *arg) {\n\tthd_data_t *thd_data = (thd_data_t *)arg;\n\tint iter = 0;\n\tdata_t local_data;\n\twhile (iter < 1000 * 1000 - 1) {\n\t\tbool success = seq_try_load_data(&local_data, &thd_data->data);\n\t\tif (success) {\n\t\t\tassert_data(&local_data);\n\t\t\tassert_d_le(iter, local_data.arr[0],\n\t\t\t    \"Seq read went back in time.\");\n\t\t\titer = local_data.arr[0];\n\t\t}\n\t}\n\treturn NULL;\n}\n\nstatic void *\nseq_writer_thd(void *arg) {\n\tthd_data_t *thd_data = (thd_data_t *)arg;\n\tdata_t local_data;\n\tmemset(&local_data, 0, sizeof(local_data));\n\tfor (int i = 0; i < 1000 * 1000; i++) {\n\t\tset_data(&local_data, i);\n\t\tseq_store_data(&thd_data->data, &local_data);\n\t}\n\treturn NULL;\n}\n\nTEST_BEGIN(test_seq_threaded) {\n\tthd_data_t thd_data;\n\tmemset(&thd_data, 0, sizeof(thd_data));\n\n\tthd_t reader;\n\tthd_t writer;\n\n\tthd_create(&reader, seq_reader_thd, &thd_data);\n\tthd_create(&writer, seq_writer_thd, &thd_data);\n\n\tthd_join(reader, NULL);\n\tthd_join(writer, NULL);\n}\nTEST_END\n\nTEST_BEGIN(test_seq_simple) {\n\tdata_t data;\n\tseq_data_t seq;\n\tmemset(&seq, 0, sizeof(seq));\n\tfor (int i = 0; i < 1000 * 1000; i++) {\n\t\tset_data(&data, i);\n\t\tseq_store_data(&seq, &data);\n\t\tset_data(&data, 0);\n\t\tbool success = seq_try_load_data(&data, &seq);\n\t\tassert_b_eq(success, true, \"Failed non-racing read\");\n\t\tassert_data(&data);\n\t}\n}\nTEST_END\n\nint main(void) {\n\treturn test_no_reentrancy(\n\t    test_seq_simple,\n\t    test_seq_threaded);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/size_classes.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic size_t\nget_max_size_class(void) {\n\tunsigned nlextents;\n\tsize_t mib[4];\n\tsize_t sz, miblen, max_size_class;\n\n\tsz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.nlextents\", (void *)&nlextents, &sz, NULL,\n\t    0), 0, \"Unexpected mallctl() error\");\n\n\tmiblen = sizeof(mib) / sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"arenas.lextent.0.size\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() error\");\n\tmib[2] = nlextents - 1;\n\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&max_size_class, &sz,\n\t    NULL, 0), 0, \"Unexpected mallctlbymib() error\");\n\n\treturn max_size_class;\n}\n\nTEST_BEGIN(test_size_classes) {\n\tsize_t size_class, max_size_class;\n\tszind_t index, max_index;\n\n\tmax_size_class = get_max_size_class();\n\tmax_index = sz_size2index(max_size_class);\n\n\tfor (index = 0, size_class = sz_index2size(index); index < max_index ||\n\t    size_class < max_size_class; index++, size_class =\n\t    sz_index2size(index)) {\n\t\tassert_true(index < max_index,\n\t\t    \"Loop conditionals should be equivalent; index=%u, \"\n\t\t    \"size_class=%zu (%#zx)\", index, size_class, size_class);\n\t\tassert_true(size_class < max_size_class,\n\t\t    \"Loop conditionals should be equivalent; index=%u, \"\n\t\t    \"size_class=%zu (%#zx)\", index, size_class, size_class);\n\n\t\tassert_u_eq(index, sz_size2index(size_class),\n\t\t    \"sz_size2index() does not reverse sz_index2size(): index=%u\"\n\t\t    \" --> size_class=%zu --> index=%u --> size_class=%zu\",\n\t\t    index, size_class, sz_size2index(size_class),\n\t\t    sz_index2size(sz_size2index(size_class)));\n\t\tassert_zu_eq(size_class,\n\t\t    sz_index2size(sz_size2index(size_class)),\n\t\t    \"sz_index2size() does not reverse sz_size2index(): index=%u\"\n\t\t    \" --> size_class=%zu --> index=%u --> size_class=%zu\",\n\t\t    index, size_class, sz_size2index(size_class),\n\t\t    sz_index2size(sz_size2index(size_class)));\n\n\t\tassert_u_eq(index+1, sz_size2index(size_class+1),\n\t\t    \"Next size_class does not round up properly\");\n\n\t\tassert_zu_eq(size_class, (index > 0) ?\n\t\t    sz_s2u(sz_index2size(index-1)+1) : sz_s2u(1),\n\t\t    \"sz_s2u() does not round up to size class\");\n\t\tassert_zu_eq(size_class, sz_s2u(size_class-1),\n\t\t    \"sz_s2u() does not round up to size class\");\n\t\tassert_zu_eq(size_class, sz_s2u(size_class),\n\t\t    \"sz_s2u() does not compute same size class\");\n\t\tassert_zu_eq(sz_s2u(size_class+1), sz_index2size(index+1),\n\t\t    \"sz_s2u() does not round up to next size class\");\n\t}\n\n\tassert_u_eq(index, sz_size2index(sz_index2size(index)),\n\t    \"sz_size2index() does not reverse sz_index2size()\");\n\tassert_zu_eq(max_size_class, sz_index2size(\n\t    sz_size2index(max_size_class)),\n\t    \"sz_index2size() does not reverse sz_size2index()\");\n\n\tassert_zu_eq(size_class, sz_s2u(sz_index2size(index-1)+1),\n\t    \"sz_s2u() does not round up to size class\");\n\tassert_zu_eq(size_class, sz_s2u(size_class-1),\n\t    \"sz_s2u() does not round up to size class\");\n\tassert_zu_eq(size_class, sz_s2u(size_class),\n\t    \"sz_s2u() does not compute same size class\");\n}\nTEST_END\n\nTEST_BEGIN(test_psize_classes) {\n\tsize_t size_class, max_psz;\n\tpszind_t pind, max_pind;\n\n\tmax_psz = get_max_size_class() + PAGE;\n\tmax_pind = sz_psz2ind(max_psz);\n\n\tfor (pind = 0, size_class = sz_pind2sz(pind);\n\t    pind < max_pind || size_class < max_psz;\n\t    pind++, size_class = sz_pind2sz(pind)) {\n\t\tassert_true(pind < max_pind,\n\t\t    \"Loop conditionals should be equivalent; pind=%u, \"\n\t\t    \"size_class=%zu (%#zx)\", pind, size_class, size_class);\n\t\tassert_true(size_class < max_psz,\n\t\t    \"Loop conditionals should be equivalent; pind=%u, \"\n\t\t    \"size_class=%zu (%#zx)\", pind, size_class, size_class);\n\n\t\tassert_u_eq(pind, sz_psz2ind(size_class),\n\t\t    \"sz_psz2ind() does not reverse sz_pind2sz(): pind=%u -->\"\n\t\t    \" size_class=%zu --> pind=%u --> size_class=%zu\", pind,\n\t\t    size_class, sz_psz2ind(size_class),\n\t\t    sz_pind2sz(sz_psz2ind(size_class)));\n\t\tassert_zu_eq(size_class, sz_pind2sz(sz_psz2ind(size_class)),\n\t\t    \"sz_pind2sz() does not reverse sz_psz2ind(): pind=%u -->\"\n\t\t    \" size_class=%zu --> pind=%u --> size_class=%zu\", pind,\n\t\t    size_class, sz_psz2ind(size_class),\n\t\t    sz_pind2sz(sz_psz2ind(size_class)));\n\n\t\tif (size_class == SC_LARGE_MAXCLASS) {\n\t\t\tassert_u_eq(SC_NPSIZES, sz_psz2ind(size_class + 1),\n\t\t\t    \"Next size_class does not round up properly\");\n\t\t} else {\n\t\t\tassert_u_eq(pind + 1, sz_psz2ind(size_class + 1),\n\t\t\t    \"Next size_class does not round up properly\");\n\t\t}\n\n\t\tassert_zu_eq(size_class, (pind > 0) ?\n\t\t    sz_psz2u(sz_pind2sz(pind-1)+1) : sz_psz2u(1),\n\t\t    \"sz_psz2u() does not round up to size class\");\n\t\tassert_zu_eq(size_class, sz_psz2u(size_class-1),\n\t\t    \"sz_psz2u() does not round up to size class\");\n\t\tassert_zu_eq(size_class, sz_psz2u(size_class),\n\t\t    \"sz_psz2u() does not compute same size class\");\n\t\tassert_zu_eq(sz_psz2u(size_class+1), sz_pind2sz(pind+1),\n\t\t    \"sz_psz2u() does not round up to next size class\");\n\t}\n\n\tassert_u_eq(pind, sz_psz2ind(sz_pind2sz(pind)),\n\t    \"sz_psz2ind() does not reverse sz_pind2sz()\");\n\tassert_zu_eq(max_psz, sz_pind2sz(sz_psz2ind(max_psz)),\n\t    \"sz_pind2sz() does not reverse sz_psz2ind()\");\n\n\tassert_zu_eq(size_class, sz_psz2u(sz_pind2sz(pind-1)+1),\n\t    \"sz_psz2u() does not round up to size class\");\n\tassert_zu_eq(size_class, sz_psz2u(size_class-1),\n\t    \"sz_psz2u() does not round up to size class\");\n\tassert_zu_eq(size_class, sz_psz2u(size_class),\n\t    \"sz_psz2u() does not compute same size class\");\n}\nTEST_END\n\nTEST_BEGIN(test_overflow) {\n\tsize_t max_size_class, max_psz;\n\n\tmax_size_class = get_max_size_class();\n\tmax_psz = max_size_class + PAGE;\n\n\tassert_u_eq(sz_size2index(max_size_class+1), SC_NSIZES,\n\t    \"sz_size2index() should return NSIZES on overflow\");\n\tassert_u_eq(sz_size2index(ZU(PTRDIFF_MAX)+1), SC_NSIZES,\n\t    \"sz_size2index() should return NSIZES on overflow\");\n\tassert_u_eq(sz_size2index(SIZE_T_MAX), SC_NSIZES,\n\t    \"sz_size2index() should return NSIZES on overflow\");\n\n\tassert_zu_eq(sz_s2u(max_size_class+1), 0,\n\t    \"sz_s2u() should return 0 for unsupported size\");\n\tassert_zu_eq(sz_s2u(ZU(PTRDIFF_MAX)+1), 0,\n\t    \"sz_s2u() should return 0 for unsupported size\");\n\tassert_zu_eq(sz_s2u(SIZE_T_MAX), 0,\n\t    \"sz_s2u() should return 0 on overflow\");\n\n\tassert_u_eq(sz_psz2ind(max_size_class+1), SC_NPSIZES,\n\t    \"sz_psz2ind() should return NPSIZES on overflow\");\n\tassert_u_eq(sz_psz2ind(ZU(PTRDIFF_MAX)+1), SC_NPSIZES,\n\t    \"sz_psz2ind() should return NPSIZES on overflow\");\n\tassert_u_eq(sz_psz2ind(SIZE_T_MAX), SC_NPSIZES,\n\t    \"sz_psz2ind() should return NPSIZES on overflow\");\n\n\tassert_zu_eq(sz_psz2u(max_size_class+1), max_psz,\n\t    \"sz_psz2u() should return (LARGE_MAXCLASS + PAGE) for unsupported\"\n\t    \" size\");\n\tassert_zu_eq(sz_psz2u(ZU(PTRDIFF_MAX)+1), max_psz,\n\t    \"sz_psz2u() should return (LARGE_MAXCLASS + PAGE) for unsupported \"\n\t    \"size\");\n\tassert_zu_eq(sz_psz2u(SIZE_T_MAX), max_psz,\n\t    \"sz_psz2u() should return (LARGE_MAXCLASS + PAGE) on overflow\");\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_size_classes,\n\t    test_psize_classes,\n\t    test_overflow);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/slab.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nTEST_BEGIN(test_arena_slab_regind) {\n\tszind_t binind;\n\n\tfor (binind = 0; binind < SC_NBINS; binind++) {\n\t\tsize_t regind;\n\t\textent_t slab;\n\t\tconst bin_info_t *bin_info = &bin_infos[binind];\n\t\textent_init(&slab, NULL, mallocx(bin_info->slab_size,\n\t\t    MALLOCX_LG_ALIGN(LG_PAGE)), bin_info->slab_size, true,\n\t\t    binind, 0, extent_state_active, false, true, true,\n\t\t    EXTENT_NOT_HEAD);\n\t\tassert_ptr_not_null(extent_addr_get(&slab),\n\t\t    \"Unexpected malloc() failure\");\n\t\tfor (regind = 0; regind < bin_info->nregs; regind++) {\n\t\t\tvoid *reg = (void *)((uintptr_t)extent_addr_get(&slab) +\n\t\t\t    (bin_info->reg_size * regind));\n\t\t\tassert_zu_eq(arena_slab_regind(&slab, binind, reg),\n\t\t\t    regind,\n\t\t\t    \"Incorrect region index computed for size %zu\",\n\t\t\t    bin_info->reg_size);\n\t\t}\n\t\tfree(extent_addr_get(&slab));\n\t}\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_arena_slab_regind);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/smoothstep.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic const uint64_t smoothstep_tab[] = {\n#define STEP(step, h, x, y)\t\t\t\\\n\th,\n\tSMOOTHSTEP\n#undef STEP\n};\n\nTEST_BEGIN(test_smoothstep_integral) {\n\tuint64_t sum, min, max;\n\tunsigned i;\n\n\t/*\n\t * The integral of smoothstep in the [0..1] range equals 1/2.  Verify\n\t * that the fixed point representation's integral is no more than\n\t * rounding error distant from 1/2.  Regarding rounding, each table\n\t * element is rounded down to the nearest fixed point value, so the\n\t * integral may be off by as much as SMOOTHSTEP_NSTEPS ulps.\n\t */\n\tsum = 0;\n\tfor (i = 0; i < SMOOTHSTEP_NSTEPS; i++) {\n\t\tsum += smoothstep_tab[i];\n\t}\n\n\tmax = (KQU(1) << (SMOOTHSTEP_BFP-1)) * (SMOOTHSTEP_NSTEPS+1);\n\tmin = max - SMOOTHSTEP_NSTEPS;\n\n\tassert_u64_ge(sum, min,\n\t    \"Integral too small, even accounting for truncation\");\n\tassert_u64_le(sum, max, \"Integral exceeds 1/2\");\n\tif (false) {\n\t\tmalloc_printf(\"%\"FMTu64\" ulps under 1/2 (limit %d)\\n\",\n\t\t    max - sum, SMOOTHSTEP_NSTEPS);\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_smoothstep_monotonic) {\n\tuint64_t prev_h;\n\tunsigned i;\n\n\t/*\n\t * The smoothstep function is monotonic in [0..1], i.e. its slope is\n\t * non-negative.  In practice we want to parametrize table generation\n\t * such that piecewise slope is greater than zero, but do not require\n\t * that here.\n\t */\n\tprev_h = 0;\n\tfor (i = 0; i < SMOOTHSTEP_NSTEPS; i++) {\n\t\tuint64_t h = smoothstep_tab[i];\n\t\tassert_u64_ge(h, prev_h, \"Piecewise non-monotonic, i=%u\", i);\n\t\tprev_h = h;\n\t}\n\tassert_u64_eq(smoothstep_tab[SMOOTHSTEP_NSTEPS-1],\n\t    (KQU(1) << SMOOTHSTEP_BFP), \"Last step must equal 1\");\n}\nTEST_END\n\nTEST_BEGIN(test_smoothstep_slope) {\n\tuint64_t prev_h, prev_delta;\n\tunsigned i;\n\n\t/*\n\t * The smoothstep slope strictly increases until x=0.5, and then\n\t * strictly decreases until x=1.0.  Verify the slightly weaker\n\t * requirement of monotonicity, so that inadequate table precision does\n\t * not cause false test failures.\n\t */\n\tprev_h = 0;\n\tprev_delta = 0;\n\tfor (i = 0; i < SMOOTHSTEP_NSTEPS / 2 + SMOOTHSTEP_NSTEPS % 2; i++) {\n\t\tuint64_t h = smoothstep_tab[i];\n\t\tuint64_t delta = h - prev_h;\n\t\tassert_u64_ge(delta, prev_delta,\n\t\t    \"Slope must monotonically increase in 0.0 <= x <= 0.5, \"\n\t\t    \"i=%u\", i);\n\t\tprev_h = h;\n\t\tprev_delta = delta;\n\t}\n\n\tprev_h = KQU(1) << SMOOTHSTEP_BFP;\n\tprev_delta = 0;\n\tfor (i = SMOOTHSTEP_NSTEPS-1; i >= SMOOTHSTEP_NSTEPS / 2; i--) {\n\t\tuint64_t h = smoothstep_tab[i];\n\t\tuint64_t delta = prev_h - h;\n\t\tassert_u64_ge(delta, prev_delta,\n\t\t    \"Slope must monotonically decrease in 0.5 <= x <= 1.0, \"\n\t\t    \"i=%u\", i);\n\t\tprev_h = h;\n\t\tprev_delta = delta;\n\t}\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_smoothstep_integral,\n\t    test_smoothstep_monotonic,\n\t    test_smoothstep_slope);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/spin.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/spin.h\"\n\nTEST_BEGIN(test_spin) {\n\tspin_t spinner = SPIN_INITIALIZER;\n\n\tfor (unsigned i = 0; i < 100; i++) {\n\t\tspin_adaptive(&spinner);\n\t}\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_spin);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/stats.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nTEST_BEGIN(test_stats_summary) {\n\tsize_t sz, allocated, active, resident, mapped;\n\tint expected = config_stats ? 0 : ENOENT;\n\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctl(\"stats.allocated\", (void *)&allocated, &sz, NULL,\n\t    0), expected, \"Unexpected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.active\", (void *)&active, &sz, NULL, 0),\n\t    expected, \"Unexpected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.resident\", (void *)&resident, &sz, NULL, 0),\n\t    expected, \"Unexpected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.mapped\", (void *)&mapped, &sz, NULL, 0),\n\t    expected, \"Unexpected mallctl() result\");\n\n\tif (config_stats) {\n\t\tassert_zu_le(allocated, active,\n\t\t    \"allocated should be no larger than active\");\n\t\tassert_zu_lt(active, resident,\n\t\t    \"active should be less than resident\");\n\t\tassert_zu_lt(active, mapped,\n\t\t    \"active should be less than mapped\");\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_stats_large) {\n\tvoid *p;\n\tuint64_t epoch;\n\tsize_t allocated;\n\tuint64_t nmalloc, ndalloc, nrequests;\n\tsize_t sz;\n\tint expected = config_stats ? 0 : ENOENT;\n\n\tp = mallocx(SC_SMALL_MAXCLASS + 1, MALLOCX_ARENA(0));\n\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\n\tassert_d_eq(mallctl(\"epoch\", NULL, NULL, (void *)&epoch, sizeof(epoch)),\n\t    0, \"Unexpected mallctl() failure\");\n\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctl(\"stats.arenas.0.large.allocated\",\n\t    (void *)&allocated, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\tsz = sizeof(uint64_t);\n\tassert_d_eq(mallctl(\"stats.arenas.0.large.nmalloc\", (void *)&nmalloc,\n\t    &sz, NULL, 0), expected, \"Unexpected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.arenas.0.large.ndalloc\", (void *)&ndalloc,\n\t    &sz, NULL, 0), expected, \"Unexpected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.arenas.0.large.nrequests\",\n\t    (void *)&nrequests, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\n\tif (config_stats) {\n\t\tassert_zu_gt(allocated, 0,\n\t\t    \"allocated should be greater than zero\");\n\t\tassert_u64_ge(nmalloc, ndalloc,\n\t\t    \"nmalloc should be at least as large as ndalloc\");\n\t\tassert_u64_le(nmalloc, nrequests,\n\t\t    \"nmalloc should no larger than nrequests\");\n\t}\n\n\tdallocx(p, 0);\n}\nTEST_END\n\nTEST_BEGIN(test_stats_arenas_summary) {\n\tvoid *little, *large;\n\tuint64_t epoch;\n\tsize_t sz;\n\tint expected = config_stats ? 0 : ENOENT;\n\tsize_t mapped;\n\tuint64_t dirty_npurge, dirty_nmadvise, dirty_purged;\n\tuint64_t muzzy_npurge, muzzy_nmadvise, muzzy_purged;\n\n\tlittle = mallocx(SC_SMALL_MAXCLASS, MALLOCX_ARENA(0));\n\tassert_ptr_not_null(little, \"Unexpected mallocx() failure\");\n\tlarge = mallocx((1U << SC_LG_LARGE_MINCLASS),\n\t    MALLOCX_ARENA(0));\n\tassert_ptr_not_null(large, \"Unexpected mallocx() failure\");\n\n\tdallocx(little, 0);\n\tdallocx(large, 0);\n\n\tassert_d_eq(mallctl(\"thread.tcache.flush\", NULL, NULL, NULL, 0),\n\t    opt_tcache ? 0 : EFAULT, \"Unexpected mallctl() result\");\n\tassert_d_eq(mallctl(\"arena.0.purge\", NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tassert_d_eq(mallctl(\"epoch\", NULL, NULL, (void *)&epoch, sizeof(epoch)),\n\t    0, \"Unexpected mallctl() failure\");\n\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctl(\"stats.arenas.0.mapped\", (void *)&mapped, &sz, NULL,\n\t    0), expected, \"Unexepected mallctl() result\");\n\n\tsz = sizeof(uint64_t);\n\tassert_d_eq(mallctl(\"stats.arenas.0.dirty_npurge\",\n\t    (void *)&dirty_npurge, &sz, NULL, 0), expected,\n\t    \"Unexepected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.arenas.0.dirty_nmadvise\",\n\t    (void *)&dirty_nmadvise, &sz, NULL, 0), expected,\n\t    \"Unexepected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.arenas.0.dirty_purged\",\n\t    (void *)&dirty_purged, &sz, NULL, 0), expected,\n\t    \"Unexepected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.arenas.0.muzzy_npurge\",\n\t    (void *)&muzzy_npurge, &sz, NULL, 0), expected,\n\t    \"Unexepected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.arenas.0.muzzy_nmadvise\",\n\t    (void *)&muzzy_nmadvise, &sz, NULL, 0), expected,\n\t    \"Unexepected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.arenas.0.muzzy_purged\",\n\t    (void *)&muzzy_purged, &sz, NULL, 0), expected,\n\t    \"Unexepected mallctl() result\");\n\n\tif (config_stats) {\n\t\tif (!background_thread_enabled()) {\n\t\t\tassert_u64_gt(dirty_npurge + muzzy_npurge, 0,\n\t\t\t    \"At least one purge should have occurred\");\n\t\t}\n\t\tassert_u64_le(dirty_nmadvise, dirty_purged,\n\t\t    \"dirty_nmadvise should be no greater than dirty_purged\");\n\t\tassert_u64_le(muzzy_nmadvise, muzzy_purged,\n\t\t    \"muzzy_nmadvise should be no greater than muzzy_purged\");\n\t}\n}\nTEST_END\n\nvoid *\nthd_start(void *arg) {\n\treturn NULL;\n}\n\nstatic void\nno_lazy_lock(void) {\n\tthd_t thd;\n\n\tthd_create(&thd, thd_start, NULL);\n\tthd_join(thd, NULL);\n}\n\nTEST_BEGIN(test_stats_arenas_small) {\n\tvoid *p;\n\tsize_t sz, allocated;\n\tuint64_t epoch, nmalloc, ndalloc, nrequests;\n\tint expected = config_stats ? 0 : ENOENT;\n\n\tno_lazy_lock(); /* Lazy locking would dodge tcache testing. */\n\n\tp = mallocx(SC_SMALL_MAXCLASS, MALLOCX_ARENA(0));\n\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\n\tassert_d_eq(mallctl(\"thread.tcache.flush\", NULL, NULL, NULL, 0),\n\t    opt_tcache ? 0 : EFAULT, \"Unexpected mallctl() result\");\n\n\tassert_d_eq(mallctl(\"epoch\", NULL, NULL, (void *)&epoch, sizeof(epoch)),\n\t    0, \"Unexpected mallctl() failure\");\n\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctl(\"stats.arenas.0.small.allocated\",\n\t    (void *)&allocated, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\tsz = sizeof(uint64_t);\n\tassert_d_eq(mallctl(\"stats.arenas.0.small.nmalloc\", (void *)&nmalloc,\n\t    &sz, NULL, 0), expected, \"Unexpected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.arenas.0.small.ndalloc\", (void *)&ndalloc,\n\t    &sz, NULL, 0), expected, \"Unexpected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.arenas.0.small.nrequests\",\n\t    (void *)&nrequests, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\n\tif (config_stats) {\n\t\tassert_zu_gt(allocated, 0,\n\t\t    \"allocated should be greater than zero\");\n\t\tassert_u64_gt(nmalloc, 0,\n\t\t    \"nmalloc should be no greater than zero\");\n\t\tassert_u64_ge(nmalloc, ndalloc,\n\t\t    \"nmalloc should be at least as large as ndalloc\");\n\t\tassert_u64_gt(nrequests, 0,\n\t\t    \"nrequests should be greater than zero\");\n\t}\n\n\tdallocx(p, 0);\n}\nTEST_END\n\nTEST_BEGIN(test_stats_arenas_large) {\n\tvoid *p;\n\tsize_t sz, allocated;\n\tuint64_t epoch, nmalloc, ndalloc;\n\tint expected = config_stats ? 0 : ENOENT;\n\n\tp = mallocx((1U << SC_LG_LARGE_MINCLASS), MALLOCX_ARENA(0));\n\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\n\tassert_d_eq(mallctl(\"epoch\", NULL, NULL, (void *)&epoch, sizeof(epoch)),\n\t    0, \"Unexpected mallctl() failure\");\n\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctl(\"stats.arenas.0.large.allocated\",\n\t    (void *)&allocated, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\tsz = sizeof(uint64_t);\n\tassert_d_eq(mallctl(\"stats.arenas.0.large.nmalloc\", (void *)&nmalloc,\n\t    &sz, NULL, 0), expected, \"Unexpected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.arenas.0.large.ndalloc\", (void *)&ndalloc,\n\t    &sz, NULL, 0), expected, \"Unexpected mallctl() result\");\n\n\tif (config_stats) {\n\t\tassert_zu_gt(allocated, 0,\n\t\t    \"allocated should be greater than zero\");\n\t\tassert_u64_gt(nmalloc, 0,\n\t\t    \"nmalloc should be greater than zero\");\n\t\tassert_u64_ge(nmalloc, ndalloc,\n\t\t    \"nmalloc should be at least as large as ndalloc\");\n\t}\n\n\tdallocx(p, 0);\n}\nTEST_END\n\nstatic void\ngen_mallctl_str(char *cmd, char *name, unsigned arena_ind) {\n\tsprintf(cmd, \"stats.arenas.%u.bins.0.%s\", arena_ind, name);\n}\n\nTEST_BEGIN(test_stats_arenas_bins) {\n\tvoid *p;\n\tsize_t sz, curslabs, curregs, nonfull_slabs;\n\tuint64_t epoch, nmalloc, ndalloc, nrequests, nfills, nflushes;\n\tuint64_t nslabs, nreslabs;\n\tint expected = config_stats ? 0 : ENOENT;\n\n\t/* Make sure allocation below isn't satisfied by tcache. */\n\tassert_d_eq(mallctl(\"thread.tcache.flush\", NULL, NULL, NULL, 0),\n\t    opt_tcache ? 0 : EFAULT, \"Unexpected mallctl() result\");\n\n\tunsigned arena_ind, old_arena_ind;\n\tsz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena_ind, &sz, NULL, 0),\n\t    0, \"Arena creation failure\");\n\tsz = sizeof(arena_ind);\n\tassert_d_eq(mallctl(\"thread.arena\", (void *)&old_arena_ind, &sz,\n\t    (void *)&arena_ind, sizeof(arena_ind)), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tp = malloc(bin_infos[0].reg_size);\n\tassert_ptr_not_null(p, \"Unexpected malloc() failure\");\n\n\tassert_d_eq(mallctl(\"thread.tcache.flush\", NULL, NULL, NULL, 0),\n\t    opt_tcache ? 0 : EFAULT, \"Unexpected mallctl() result\");\n\n\tassert_d_eq(mallctl(\"epoch\", NULL, NULL, (void *)&epoch, sizeof(epoch)),\n\t    0, \"Unexpected mallctl() failure\");\n\n\tchar cmd[128];\n\tsz = sizeof(uint64_t);\n\tgen_mallctl_str(cmd, \"nmalloc\", arena_ind);\n\tassert_d_eq(mallctl(cmd, (void *)&nmalloc, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\tgen_mallctl_str(cmd, \"ndalloc\", arena_ind);\n\tassert_d_eq(mallctl(cmd, (void *)&ndalloc, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\tgen_mallctl_str(cmd, \"nrequests\", arena_ind);\n\tassert_d_eq(mallctl(cmd, (void *)&nrequests, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\tsz = sizeof(size_t);\n\tgen_mallctl_str(cmd, \"curregs\", arena_ind);\n\tassert_d_eq(mallctl(cmd, (void *)&curregs, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\n\tsz = sizeof(uint64_t);\n\tgen_mallctl_str(cmd, \"nfills\", arena_ind);\n\tassert_d_eq(mallctl(cmd, (void *)&nfills, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\tgen_mallctl_str(cmd, \"nflushes\", arena_ind);\n\tassert_d_eq(mallctl(cmd, (void *)&nflushes, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\n\tgen_mallctl_str(cmd, \"nslabs\", arena_ind);\n\tassert_d_eq(mallctl(cmd, (void *)&nslabs, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\tgen_mallctl_str(cmd, \"nreslabs\", arena_ind);\n\tassert_d_eq(mallctl(cmd, (void *)&nreslabs, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\tsz = sizeof(size_t);\n\tgen_mallctl_str(cmd, \"curslabs\", arena_ind);\n\tassert_d_eq(mallctl(cmd, (void *)&curslabs, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\tgen_mallctl_str(cmd, \"nonfull_slabs\", arena_ind);\n\tassert_d_eq(mallctl(cmd, (void *)&nonfull_slabs, &sz, NULL, 0),\n\t    expected, \"Unexpected mallctl() result\");\n\n\tif (config_stats) {\n\t\tassert_u64_gt(nmalloc, 0,\n\t\t    \"nmalloc should be greater than zero\");\n\t\tassert_u64_ge(nmalloc, ndalloc,\n\t\t    \"nmalloc should be at least as large as ndalloc\");\n\t\tassert_u64_gt(nrequests, 0,\n\t\t    \"nrequests should be greater than zero\");\n\t\tassert_zu_gt(curregs, 0,\n\t\t    \"allocated should be greater than zero\");\n\t\tif (opt_tcache) {\n\t\t\tassert_u64_gt(nfills, 0,\n\t\t\t    \"At least one fill should have occurred\");\n\t\t\tassert_u64_gt(nflushes, 0,\n\t\t\t    \"At least one flush should have occurred\");\n\t\t}\n\t\tassert_u64_gt(nslabs, 0,\n\t\t    \"At least one slab should have been allocated\");\n\t\tassert_zu_gt(curslabs, 0,\n\t\t    \"At least one slab should be currently allocated\");\n\t\tassert_zu_eq(nonfull_slabs, 0,\n\t\t    \"slabs_nonfull should be empty\");\n\t}\n\n\tdallocx(p, 0);\n}\nTEST_END\n\nTEST_BEGIN(test_stats_arenas_lextents) {\n\tvoid *p;\n\tuint64_t epoch, nmalloc, ndalloc;\n\tsize_t curlextents, sz, hsize;\n\tint expected = config_stats ? 0 : ENOENT;\n\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctl(\"arenas.lextent.0.size\", (void *)&hsize, &sz, NULL,\n\t    0), 0, \"Unexpected mallctl() failure\");\n\n\tp = mallocx(hsize, MALLOCX_ARENA(0));\n\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\n\tassert_d_eq(mallctl(\"epoch\", NULL, NULL, (void *)&epoch, sizeof(epoch)),\n\t    0, \"Unexpected mallctl() failure\");\n\n\tsz = sizeof(uint64_t);\n\tassert_d_eq(mallctl(\"stats.arenas.0.lextents.0.nmalloc\",\n\t    (void *)&nmalloc, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.arenas.0.lextents.0.ndalloc\",\n\t    (void *)&ndalloc, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctl(\"stats.arenas.0.lextents.0.curlextents\",\n\t    (void *)&curlextents, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\n\tif (config_stats) {\n\t\tassert_u64_gt(nmalloc, 0,\n\t\t    \"nmalloc should be greater than zero\");\n\t\tassert_u64_ge(nmalloc, ndalloc,\n\t\t    \"nmalloc should be at least as large as ndalloc\");\n\t\tassert_u64_gt(curlextents, 0,\n\t\t    \"At least one extent should be currently allocated\");\n\t}\n\n\tdallocx(p, 0);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test_no_reentrancy(\n\t    test_stats_summary,\n\t    test_stats_large,\n\t    test_stats_arenas_summary,\n\t    test_stats_arenas_small,\n\t    test_stats_arenas_large,\n\t    test_stats_arenas_bins,\n\t    test_stats_arenas_lextents);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/stats_print.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/util.h\"\n\ntypedef enum {\n\tTOKEN_TYPE_NONE,\n\tTOKEN_TYPE_ERROR,\n\tTOKEN_TYPE_EOI,\n\tTOKEN_TYPE_NULL,\n\tTOKEN_TYPE_FALSE,\n\tTOKEN_TYPE_TRUE,\n\tTOKEN_TYPE_LBRACKET,\n\tTOKEN_TYPE_RBRACKET,\n\tTOKEN_TYPE_LBRACE,\n\tTOKEN_TYPE_RBRACE,\n\tTOKEN_TYPE_COLON,\n\tTOKEN_TYPE_COMMA,\n\tTOKEN_TYPE_STRING,\n\tTOKEN_TYPE_NUMBER\n} token_type_t;\n\ntypedef struct parser_s parser_t;\ntypedef struct {\n\tparser_t\t*parser;\n\ttoken_type_t\ttoken_type;\n\tsize_t\t\tpos;\n\tsize_t\t\tlen;\n\tsize_t\t\tline;\n\tsize_t\t\tcol;\n} token_t;\n\nstruct parser_s {\n\tbool verbose;\n\tchar\t*buf; /* '\\0'-terminated. */\n\tsize_t\tlen; /* Number of characters preceding '\\0' in buf. */\n\tsize_t\tpos;\n\tsize_t\tline;\n\tsize_t\tcol;\n\ttoken_t\ttoken;\n};\n\nstatic void\ntoken_init(token_t *token, parser_t *parser, token_type_t token_type,\n    size_t pos, size_t len, size_t line, size_t col) {\n\ttoken->parser = parser;\n\ttoken->token_type = token_type;\n\ttoken->pos = pos;\n\ttoken->len = len;\n\ttoken->line = line;\n\ttoken->col = col;\n}\n\nstatic void\ntoken_error(token_t *token) {\n\tif (!token->parser->verbose) {\n\t\treturn;\n\t}\n\tswitch (token->token_type) {\n\tcase TOKEN_TYPE_NONE:\n\t\tnot_reached();\n\tcase TOKEN_TYPE_ERROR:\n\t\tmalloc_printf(\"%zu:%zu: Unexpected character in token: \",\n\t\t    token->line, token->col);\n\t\tbreak;\n\tdefault:\n\t\tmalloc_printf(\"%zu:%zu: Unexpected token: \", token->line,\n\t\t    token->col);\n\t\tbreak;\n\t}\n\tUNUSED ssize_t err = malloc_write_fd(STDERR_FILENO,\n\t    &token->parser->buf[token->pos], token->len);\n\tmalloc_printf(\"\\n\");\n}\n\nstatic void\nparser_init(parser_t *parser, bool verbose) {\n\tparser->verbose = verbose;\n\tparser->buf = NULL;\n\tparser->len = 0;\n\tparser->pos = 0;\n\tparser->line = 1;\n\tparser->col = 0;\n}\n\nstatic void\nparser_fini(parser_t *parser) {\n\tif (parser->buf != NULL) {\n\t\tdallocx(parser->buf, MALLOCX_TCACHE_NONE);\n\t}\n}\n\nstatic bool\nparser_append(parser_t *parser, const char *str) {\n\tsize_t len = strlen(str);\n\tchar *buf = (parser->buf == NULL) ? mallocx(len + 1,\n\t    MALLOCX_TCACHE_NONE) : rallocx(parser->buf, parser->len + len + 1,\n\t    MALLOCX_TCACHE_NONE);\n\tif (buf == NULL) {\n\t\treturn true;\n\t}\n\tmemcpy(&buf[parser->len], str, len + 1);\n\tparser->buf = buf;\n\tparser->len += len;\n\treturn false;\n}\n\nstatic bool\nparser_tokenize(parser_t *parser) {\n\tenum {\n\t\tSTATE_START,\n\t\tSTATE_EOI,\n\t\tSTATE_N, STATE_NU, STATE_NUL, STATE_NULL,\n\t\tSTATE_F, STATE_FA, STATE_FAL, STATE_FALS, STATE_FALSE,\n\t\tSTATE_T, STATE_TR, STATE_TRU, STATE_TRUE,\n\t\tSTATE_LBRACKET,\n\t\tSTATE_RBRACKET,\n\t\tSTATE_LBRACE,\n\t\tSTATE_RBRACE,\n\t\tSTATE_COLON,\n\t\tSTATE_COMMA,\n\t\tSTATE_CHARS,\n\t\tSTATE_CHAR_ESCAPE,\n\t\tSTATE_CHAR_U, STATE_CHAR_UD, STATE_CHAR_UDD, STATE_CHAR_UDDD,\n\t\tSTATE_STRING,\n\t\tSTATE_MINUS,\n\t\tSTATE_LEADING_ZERO,\n\t\tSTATE_DIGITS,\n\t\tSTATE_DECIMAL,\n\t\tSTATE_FRAC_DIGITS,\n\t\tSTATE_EXP,\n\t\tSTATE_EXP_SIGN,\n\t\tSTATE_EXP_DIGITS,\n\t\tSTATE_ACCEPT\n\t} state = STATE_START;\n\tsize_t token_pos JEMALLOC_CC_SILENCE_INIT(0);\n\tsize_t token_line JEMALLOC_CC_SILENCE_INIT(1);\n\tsize_t token_col JEMALLOC_CC_SILENCE_INIT(0);\n\n\tassert_zu_le(parser->pos, parser->len,\n\t    \"Position is past end of buffer\");\n\n\twhile (state != STATE_ACCEPT) {\n\t\tchar c = parser->buf[parser->pos];\n\n\t\tswitch (state) {\n\t\tcase STATE_START:\n\t\t\ttoken_pos = parser->pos;\n\t\t\ttoken_line = parser->line;\n\t\t\ttoken_col = parser->col;\n\t\t\tswitch (c) {\n\t\t\tcase ' ': case '\\b': case '\\n': case '\\r': case '\\t':\n\t\t\t\tbreak;\n\t\t\tcase '\\0':\n\t\t\t\tstate = STATE_EOI;\n\t\t\t\tbreak;\n\t\t\tcase 'n':\n\t\t\t\tstate = STATE_N;\n\t\t\t\tbreak;\n\t\t\tcase 'f':\n\t\t\t\tstate = STATE_F;\n\t\t\t\tbreak;\n\t\t\tcase 't':\n\t\t\t\tstate = STATE_T;\n\t\t\t\tbreak;\n\t\t\tcase '[':\n\t\t\t\tstate = STATE_LBRACKET;\n\t\t\t\tbreak;\n\t\t\tcase ']':\n\t\t\t\tstate = STATE_RBRACKET;\n\t\t\t\tbreak;\n\t\t\tcase '{':\n\t\t\t\tstate = STATE_LBRACE;\n\t\t\t\tbreak;\n\t\t\tcase '}':\n\t\t\t\tstate = STATE_RBRACE;\n\t\t\t\tbreak;\n\t\t\tcase ':':\n\t\t\t\tstate = STATE_COLON;\n\t\t\t\tbreak;\n\t\t\tcase ',':\n\t\t\t\tstate = STATE_COMMA;\n\t\t\t\tbreak;\n\t\t\tcase '\"':\n\t\t\t\tstate = STATE_CHARS;\n\t\t\t\tbreak;\n\t\t\tcase '-':\n\t\t\t\tstate = STATE_MINUS;\n\t\t\t\tbreak;\n\t\t\tcase '0':\n\t\t\t\tstate = STATE_LEADING_ZERO;\n\t\t\t\tbreak;\n\t\t\tcase '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9':\n\t\t\t\tstate = STATE_DIGITS;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_EOI:\n\t\t\ttoken_init(&parser->token, parser,\n\t\t\t    TOKEN_TYPE_EOI, token_pos, parser->pos -\n\t\t\t    token_pos, token_line, token_col);\n\t\t\tstate = STATE_ACCEPT;\n\t\t\tbreak;\n\t\tcase STATE_N:\n\t\t\tswitch (c) {\n\t\t\tcase 'u':\n\t\t\t\tstate = STATE_NU;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_NU:\n\t\t\tswitch (c) {\n\t\t\tcase 'l':\n\t\t\t\tstate = STATE_NUL;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_NUL:\n\t\t\tswitch (c) {\n\t\t\tcase 'l':\n\t\t\t\tstate = STATE_NULL;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_NULL:\n\t\t\tswitch (c) {\n\t\t\tcase ' ': case '\\b': case '\\n': case '\\r': case '\\t':\n\t\t\tcase '\\0':\n\t\t\tcase '[': case ']': case '{': case '}': case ':':\n\t\t\tcase ',':\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\ttoken_init(&parser->token, parser, TOKEN_TYPE_NULL,\n\t\t\t    token_pos, parser->pos - token_pos, token_line,\n\t\t\t    token_col);\n\t\t\tstate = STATE_ACCEPT;\n\t\t\tbreak;\n\t\tcase STATE_F:\n\t\t\tswitch (c) {\n\t\t\tcase 'a':\n\t\t\t\tstate = STATE_FA;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_FA:\n\t\t\tswitch (c) {\n\t\t\tcase 'l':\n\t\t\t\tstate = STATE_FAL;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_FAL:\n\t\t\tswitch (c) {\n\t\t\tcase 's':\n\t\t\t\tstate = STATE_FALS;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_FALS:\n\t\t\tswitch (c) {\n\t\t\tcase 'e':\n\t\t\t\tstate = STATE_FALSE;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_FALSE:\n\t\t\tswitch (c) {\n\t\t\tcase ' ': case '\\b': case '\\n': case '\\r': case '\\t':\n\t\t\tcase '\\0':\n\t\t\tcase '[': case ']': case '{': case '}': case ':':\n\t\t\tcase ',':\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\ttoken_init(&parser->token, parser,\n\t\t\t    TOKEN_TYPE_FALSE, token_pos, parser->pos -\n\t\t\t    token_pos, token_line, token_col);\n\t\t\tstate = STATE_ACCEPT;\n\t\t\tbreak;\n\t\tcase STATE_T:\n\t\t\tswitch (c) {\n\t\t\tcase 'r':\n\t\t\t\tstate = STATE_TR;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_TR:\n\t\t\tswitch (c) {\n\t\t\tcase 'u':\n\t\t\t\tstate = STATE_TRU;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_TRU:\n\t\t\tswitch (c) {\n\t\t\tcase 'e':\n\t\t\t\tstate = STATE_TRUE;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_TRUE:\n\t\t\tswitch (c) {\n\t\t\tcase ' ': case '\\b': case '\\n': case '\\r': case '\\t':\n\t\t\tcase '\\0':\n\t\t\tcase '[': case ']': case '{': case '}': case ':':\n\t\t\tcase ',':\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\ttoken_init(&parser->token, parser, TOKEN_TYPE_TRUE,\n\t\t\t    token_pos, parser->pos - token_pos, token_line,\n\t\t\t    token_col);\n\t\t\tstate = STATE_ACCEPT;\n\t\t\tbreak;\n\t\tcase STATE_LBRACKET:\n\t\t\ttoken_init(&parser->token, parser, TOKEN_TYPE_LBRACKET,\n\t\t\t    token_pos, parser->pos - token_pos, token_line,\n\t\t\t    token_col);\n\t\t\tstate = STATE_ACCEPT;\n\t\t\tbreak;\n\t\tcase STATE_RBRACKET:\n\t\t\ttoken_init(&parser->token, parser, TOKEN_TYPE_RBRACKET,\n\t\t\t    token_pos, parser->pos - token_pos, token_line,\n\t\t\t    token_col);\n\t\t\tstate = STATE_ACCEPT;\n\t\t\tbreak;\n\t\tcase STATE_LBRACE:\n\t\t\ttoken_init(&parser->token, parser, TOKEN_TYPE_LBRACE,\n\t\t\t    token_pos, parser->pos - token_pos, token_line,\n\t\t\t    token_col);\n\t\t\tstate = STATE_ACCEPT;\n\t\t\tbreak;\n\t\tcase STATE_RBRACE:\n\t\t\ttoken_init(&parser->token, parser, TOKEN_TYPE_RBRACE,\n\t\t\t    token_pos, parser->pos - token_pos, token_line,\n\t\t\t    token_col);\n\t\t\tstate = STATE_ACCEPT;\n\t\t\tbreak;\n\t\tcase STATE_COLON:\n\t\t\ttoken_init(&parser->token, parser, TOKEN_TYPE_COLON,\n\t\t\t    token_pos, parser->pos - token_pos, token_line,\n\t\t\t    token_col);\n\t\t\tstate = STATE_ACCEPT;\n\t\t\tbreak;\n\t\tcase STATE_COMMA:\n\t\t\ttoken_init(&parser->token, parser, TOKEN_TYPE_COMMA,\n\t\t\t    token_pos, parser->pos - token_pos, token_line,\n\t\t\t    token_col);\n\t\t\tstate = STATE_ACCEPT;\n\t\t\tbreak;\n\t\tcase STATE_CHARS:\n\t\t\tswitch (c) {\n\t\t\tcase '\\\\':\n\t\t\t\tstate = STATE_CHAR_ESCAPE;\n\t\t\t\tbreak;\n\t\t\tcase '\"':\n\t\t\t\tstate = STATE_STRING;\n\t\t\t\tbreak;\n\t\t\tcase 0x00: case 0x01: case 0x02: case 0x03: case 0x04:\n\t\t\tcase 0x05: case 0x06: case 0x07: case 0x08: case 0x09:\n\t\t\tcase 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e:\n\t\t\tcase 0x0f: case 0x10: case 0x11: case 0x12: case 0x13:\n\t\t\tcase 0x14: case 0x15: case 0x16: case 0x17: case 0x18:\n\t\t\tcase 0x19: case 0x1a: case 0x1b: case 0x1c: case 0x1d:\n\t\t\tcase 0x1e: case 0x1f:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\tdefault:\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_CHAR_ESCAPE:\n\t\t\tswitch (c) {\n\t\t\tcase '\"': case '\\\\': case '/': case 'b': case 'n':\n\t\t\tcase 'r': case 't':\n\t\t\t\tstate = STATE_CHARS;\n\t\t\t\tbreak;\n\t\t\tcase 'u':\n\t\t\t\tstate = STATE_CHAR_U;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_CHAR_U:\n\t\t\tswitch (c) {\n\t\t\tcase '0': case '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9':\n\t\t\tcase 'a': case 'b': case 'c': case 'd': case 'e':\n\t\t\tcase 'f':\n\t\t\tcase 'A': case 'B': case 'C': case 'D': case 'E':\n\t\t\tcase 'F':\n\t\t\t\tstate = STATE_CHAR_UD;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_CHAR_UD:\n\t\t\tswitch (c) {\n\t\t\tcase '0': case '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9':\n\t\t\tcase 'a': case 'b': case 'c': case 'd': case 'e':\n\t\t\tcase 'f':\n\t\t\tcase 'A': case 'B': case 'C': case 'D': case 'E':\n\t\t\tcase 'F':\n\t\t\t\tstate = STATE_CHAR_UDD;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_CHAR_UDD:\n\t\t\tswitch (c) {\n\t\t\tcase '0': case '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9':\n\t\t\tcase 'a': case 'b': case 'c': case 'd': case 'e':\n\t\t\tcase 'f':\n\t\t\tcase 'A': case 'B': case 'C': case 'D': case 'E':\n\t\t\tcase 'F':\n\t\t\t\tstate = STATE_CHAR_UDDD;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_CHAR_UDDD:\n\t\t\tswitch (c) {\n\t\t\tcase '0': case '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9':\n\t\t\tcase 'a': case 'b': case 'c': case 'd': case 'e':\n\t\t\tcase 'f':\n\t\t\tcase 'A': case 'B': case 'C': case 'D': case 'E':\n\t\t\tcase 'F':\n\t\t\t\tstate = STATE_CHARS;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_STRING:\n\t\t\ttoken_init(&parser->token, parser, TOKEN_TYPE_STRING,\n\t\t\t    token_pos, parser->pos - token_pos, token_line,\n\t\t\t    token_col);\n\t\t\tstate = STATE_ACCEPT;\n\t\t\tbreak;\n\t\tcase STATE_MINUS:\n\t\t\tswitch (c) {\n\t\t\tcase '0':\n\t\t\t\tstate = STATE_LEADING_ZERO;\n\t\t\t\tbreak;\n\t\t\tcase '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9':\n\t\t\t\tstate = STATE_DIGITS;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_LEADING_ZERO:\n\t\t\tswitch (c) {\n\t\t\tcase '.':\n\t\t\t\tstate = STATE_DECIMAL;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_NUMBER, token_pos, parser->pos -\n\t\t\t\t    token_pos, token_line, token_col);\n\t\t\t\tstate = STATE_ACCEPT;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_DIGITS:\n\t\t\tswitch (c) {\n\t\t\tcase '0': case '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9':\n\t\t\t\tbreak;\n\t\t\tcase '.':\n\t\t\t\tstate = STATE_DECIMAL;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_NUMBER, token_pos, parser->pos -\n\t\t\t\t    token_pos, token_line, token_col);\n\t\t\t\tstate = STATE_ACCEPT;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_DECIMAL:\n\t\t\tswitch (c) {\n\t\t\tcase '0': case '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9':\n\t\t\t\tstate = STATE_FRAC_DIGITS;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_FRAC_DIGITS:\n\t\t\tswitch (c) {\n\t\t\tcase '0': case '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9':\n\t\t\t\tbreak;\n\t\t\tcase 'e': case 'E':\n\t\t\t\tstate = STATE_EXP;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_NUMBER, token_pos, parser->pos -\n\t\t\t\t    token_pos, token_line, token_col);\n\t\t\t\tstate = STATE_ACCEPT;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_EXP:\n\t\t\tswitch (c) {\n\t\t\tcase '-': case '+':\n\t\t\t\tstate = STATE_EXP_SIGN;\n\t\t\t\tbreak;\n\t\t\tcase '0': case '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9':\n\t\t\t\tstate = STATE_EXP_DIGITS;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_EXP_SIGN:\n\t\t\tswitch (c) {\n\t\t\tcase '0': case '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9':\n\t\t\t\tstate = STATE_EXP_DIGITS;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_EXP_DIGITS:\n\t\t\tswitch (c) {\n\t\t\tcase '0': case '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9':\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_NUMBER, token_pos, parser->pos -\n\t\t\t\t    token_pos, token_line, token_col);\n\t\t\t\tstate = STATE_ACCEPT;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tnot_reached();\n\t\t}\n\n\t\tif (state != STATE_ACCEPT) {\n\t\t\tif (c == '\\n') {\n\t\t\t\tparser->line++;\n\t\t\t\tparser->col = 0;\n\t\t\t} else {\n\t\t\t\tparser->col++;\n\t\t\t}\n\t\t\tparser->pos++;\n\t\t}\n\t}\n\treturn false;\n}\n\nstatic bool\tparser_parse_array(parser_t *parser);\nstatic bool\tparser_parse_object(parser_t *parser);\n\nstatic bool\nparser_parse_value(parser_t *parser) {\n\tswitch (parser->token.token_type) {\n\tcase TOKEN_TYPE_NULL:\n\tcase TOKEN_TYPE_FALSE:\n\tcase TOKEN_TYPE_TRUE:\n\tcase TOKEN_TYPE_STRING:\n\tcase TOKEN_TYPE_NUMBER:\n\t\treturn false;\n\tcase TOKEN_TYPE_LBRACE:\n\t\treturn parser_parse_object(parser);\n\tcase TOKEN_TYPE_LBRACKET:\n\t\treturn parser_parse_array(parser);\n\tdefault:\n\t\treturn true;\n\t}\n\tnot_reached();\n}\n\nstatic bool\nparser_parse_pair(parser_t *parser) {\n\tassert_d_eq(parser->token.token_type, TOKEN_TYPE_STRING,\n\t    \"Pair should start with string\");\n\tif (parser_tokenize(parser)) {\n\t\treturn true;\n\t}\n\tswitch (parser->token.token_type) {\n\tcase TOKEN_TYPE_COLON:\n\t\tif (parser_tokenize(parser)) {\n\t\t\treturn true;\n\t\t}\n\t\treturn parser_parse_value(parser);\n\tdefault:\n\t\treturn true;\n\t}\n}\n\nstatic bool\nparser_parse_values(parser_t *parser) {\n\tif (parser_parse_value(parser)) {\n\t\treturn true;\n\t}\n\n\twhile (true) {\n\t\tif (parser_tokenize(parser)) {\n\t\t\treturn true;\n\t\t}\n\t\tswitch (parser->token.token_type) {\n\t\tcase TOKEN_TYPE_COMMA:\n\t\t\tif (parser_tokenize(parser)) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tif (parser_parse_value(parser)) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase TOKEN_TYPE_RBRACKET:\n\t\t\treturn false;\n\t\tdefault:\n\t\t\treturn true;\n\t\t}\n\t}\n}\n\nstatic bool\nparser_parse_array(parser_t *parser) {\n\tassert_d_eq(parser->token.token_type, TOKEN_TYPE_LBRACKET,\n\t    \"Array should start with [\");\n\tif (parser_tokenize(parser)) {\n\t\treturn true;\n\t}\n\tswitch (parser->token.token_type) {\n\tcase TOKEN_TYPE_RBRACKET:\n\t\treturn false;\n\tdefault:\n\t\treturn parser_parse_values(parser);\n\t}\n\tnot_reached();\n}\n\nstatic bool\nparser_parse_pairs(parser_t *parser) {\n\tassert_d_eq(parser->token.token_type, TOKEN_TYPE_STRING,\n\t    \"Object should start with string\");\n\tif (parser_parse_pair(parser)) {\n\t\treturn true;\n\t}\n\n\twhile (true) {\n\t\tif (parser_tokenize(parser)) {\n\t\t\treturn true;\n\t\t}\n\t\tswitch (parser->token.token_type) {\n\t\tcase TOKEN_TYPE_COMMA:\n\t\t\tif (parser_tokenize(parser)) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tswitch (parser->token.token_type) {\n\t\t\tcase TOKEN_TYPE_STRING:\n\t\t\t\tif (parser_parse_pair(parser)) {\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase TOKEN_TYPE_RBRACE:\n\t\t\treturn false;\n\t\tdefault:\n\t\t\treturn true;\n\t\t}\n\t}\n}\n\nstatic bool\nparser_parse_object(parser_t *parser) {\n\tassert_d_eq(parser->token.token_type, TOKEN_TYPE_LBRACE,\n\t    \"Object should start with {\");\n\tif (parser_tokenize(parser)) {\n\t\treturn true;\n\t}\n\tswitch (parser->token.token_type) {\n\tcase TOKEN_TYPE_STRING:\n\t\treturn parser_parse_pairs(parser);\n\tcase TOKEN_TYPE_RBRACE:\n\t\treturn false;\n\tdefault:\n\t\treturn true;\n\t}\n\tnot_reached();\n}\n\nstatic bool\nparser_parse(parser_t *parser) {\n\tif (parser_tokenize(parser)) {\n\t\tgoto label_error;\n\t}\n\tif (parser_parse_value(parser)) {\n\t\tgoto label_error;\n\t}\n\n\tif (parser_tokenize(parser)) {\n\t\tgoto label_error;\n\t}\n\tswitch (parser->token.token_type) {\n\tcase TOKEN_TYPE_EOI:\n\t\treturn false;\n\tdefault:\n\t\tgoto label_error;\n\t}\n\tnot_reached();\n\nlabel_error:\n\ttoken_error(&parser->token);\n\treturn true;\n}\n\nTEST_BEGIN(test_json_parser) {\n\tsize_t i;\n\tconst char *invalid_inputs[] = {\n\t\t/* Tokenizer error case tests. */\n\t\t\"{ \\\"string\\\": X }\",\n\t\t\"{ \\\"string\\\": nXll }\",\n\t\t\"{ \\\"string\\\": nuXl }\",\n\t\t\"{ \\\"string\\\": nulX }\",\n\t\t\"{ \\\"string\\\": nullX }\",\n\t\t\"{ \\\"string\\\": fXlse }\",\n\t\t\"{ \\\"string\\\": faXse }\",\n\t\t\"{ \\\"string\\\": falXe }\",\n\t\t\"{ \\\"string\\\": falsX }\",\n\t\t\"{ \\\"string\\\": falseX }\",\n\t\t\"{ \\\"string\\\": tXue }\",\n\t\t\"{ \\\"string\\\": trXe }\",\n\t\t\"{ \\\"string\\\": truX }\",\n\t\t\"{ \\\"string\\\": trueX }\",\n\t\t\"{ \\\"string\\\": \\\"\\n\\\" }\",\n\t\t\"{ \\\"string\\\": \\\"\\\\z\\\" }\",\n\t\t\"{ \\\"string\\\": \\\"\\\\uX000\\\" }\",\n\t\t\"{ \\\"string\\\": \\\"\\\\u0X00\\\" }\",\n\t\t\"{ \\\"string\\\": \\\"\\\\u00X0\\\" }\",\n\t\t\"{ \\\"string\\\": \\\"\\\\u000X\\\" }\",\n\t\t\"{ \\\"string\\\": -X }\",\n\t\t\"{ \\\"string\\\": 0.X }\",\n\t\t\"{ \\\"string\\\": 0.0eX }\",\n\t\t\"{ \\\"string\\\": 0.0e+X }\",\n\n\t\t/* Parser error test cases. */\n\t\t\"{\\\"string\\\": }\",\n\t\t\"{\\\"string\\\" }\",\n\t\t\"{\\\"string\\\": [ 0 }\",\n\t\t\"{\\\"string\\\": {\\\"a\\\":0, 1 } }\",\n\t\t\"{\\\"string\\\": {\\\"a\\\":0: } }\",\n\t\t\"{\",\n\t\t\"{}{\",\n\t};\n\tconst char *valid_inputs[] = {\n\t\t/* Token tests. */\n\t\t\"null\",\n\t\t\"false\",\n\t\t\"true\",\n\t\t\"{}\",\n\t\t\"{\\\"a\\\": 0}\",\n\t\t\"[]\",\n\t\t\"[0, 1]\",\n\t\t\"0\",\n\t\t\"1\",\n\t\t\"10\",\n\t\t\"-10\",\n\t\t\"10.23\",\n\t\t\"10.23e4\",\n\t\t\"10.23e-4\",\n\t\t\"10.23e+4\",\n\t\t\"10.23E4\",\n\t\t\"10.23E-4\",\n\t\t\"10.23E+4\",\n\t\t\"-10.23\",\n\t\t\"-10.23e4\",\n\t\t\"-10.23e-4\",\n\t\t\"-10.23e+4\",\n\t\t\"-10.23E4\",\n\t\t\"-10.23E-4\",\n\t\t\"-10.23E+4\",\n\t\t\"\\\"value\\\"\",\n\t\t\"\\\" \\\\\\\" \\\\/ \\\\b \\\\n \\\\r \\\\t \\\\u0abc \\\\u1DEF \\\"\",\n\n\t\t/* Parser test with various nesting. */\n\t\t\"{\\\"a\\\":null, \\\"b\\\":[1,[{\\\"c\\\":2},3]], \\\"d\\\":{\\\"e\\\":true}}\",\n\t};\n\n\tfor (i = 0; i < sizeof(invalid_inputs)/sizeof(const char *); i++) {\n\t\tconst char *input = invalid_inputs[i];\n\t\tparser_t parser;\n\t\tparser_init(&parser, false);\n\t\tassert_false(parser_append(&parser, input),\n\t\t    \"Unexpected input appending failure\");\n\t\tassert_true(parser_parse(&parser),\n\t\t    \"Unexpected parse success for input: %s\", input);\n\t\tparser_fini(&parser);\n\t}\n\n\tfor (i = 0; i < sizeof(valid_inputs)/sizeof(const char *); i++) {\n\t\tconst char *input = valid_inputs[i];\n\t\tparser_t parser;\n\t\tparser_init(&parser, true);\n\t\tassert_false(parser_append(&parser, input),\n\t\t    \"Unexpected input appending failure\");\n\t\tassert_false(parser_parse(&parser),\n\t\t    \"Unexpected parse error for input: %s\", input);\n\t\tparser_fini(&parser);\n\t}\n}\nTEST_END\n\nvoid\nwrite_cb(void *opaque, const char *str) {\n\tparser_t *parser = (parser_t *)opaque;\n\tif (parser_append(parser, str)) {\n\t\ttest_fail(\"Unexpected input appending failure\");\n\t}\n}\n\nTEST_BEGIN(test_stats_print_json) {\n\tconst char *opts[] = {\n\t\t\"J\",\n\t\t\"Jg\",\n\t\t\"Jm\",\n\t\t\"Jd\",\n\t\t\"Jmd\",\n\t\t\"Jgd\",\n\t\t\"Jgm\",\n\t\t\"Jgmd\",\n\t\t\"Ja\",\n\t\t\"Jb\",\n\t\t\"Jl\",\n\t\t\"Jx\",\n\t\t\"Jbl\",\n\t\t\"Jal\",\n\t\t\"Jab\",\n\t\t\"Jabl\",\n\t\t\"Jax\",\n\t\t\"Jbx\",\n\t\t\"Jlx\",\n\t\t\"Jablx\",\n\t\t\"Jgmdablx\",\n\t};\n\tunsigned arena_ind, i;\n\n\tfor (i = 0; i < 3; i++) {\n\t\tunsigned j;\n\n\t\tswitch (i) {\n\t\tcase 0:\n\t\t\tbreak;\n\t\tcase 1: {\n\t\t\tsize_t sz = sizeof(arena_ind);\n\t\t\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena_ind,\n\t\t\t    &sz, NULL, 0), 0, \"Unexpected mallctl failure\");\n\t\t\tbreak;\n\t\t} case 2: {\n\t\t\tsize_t mib[3];\n\t\t\tsize_t miblen = sizeof(mib)/sizeof(size_t);\n\t\t\tassert_d_eq(mallctlnametomib(\"arena.0.destroy\",\n\t\t\t    mib, &miblen), 0,\n\t\t\t    \"Unexpected mallctlnametomib failure\");\n\t\t\tmib[1] = arena_ind;\n\t\t\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL,\n\t\t\t    0), 0, \"Unexpected mallctlbymib failure\");\n\t\t\tbreak;\n\t\t} default:\n\t\t\tnot_reached();\n\t\t}\n\n\t\tfor (j = 0; j < sizeof(opts)/sizeof(const char *); j++) {\n\t\t\tparser_t parser;\n\n\t\t\tparser_init(&parser, true);\n\t\t\tmalloc_stats_print(write_cb, (void *)&parser, opts[j]);\n\t\t\tassert_false(parser_parse(&parser),\n\t\t\t    \"Unexpected parse error, opts=\\\"%s\\\"\", opts[j]);\n\t\t\tparser_fini(&parser);\n\t\t}\n\t}\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_json_parser,\n\t    test_stats_print_json);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/test_hooks.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic bool hook_called = false;\n\nstatic void\nhook() {\n\thook_called = true;\n}\n\nstatic int\nfunc_to_hook(int arg1, int arg2) {\n\treturn arg1 + arg2;\n}\n\n#define func_to_hook JEMALLOC_HOOK(func_to_hook, test_hooks_libc_hook)\n\nTEST_BEGIN(unhooked_call) {\n\ttest_hooks_libc_hook = NULL;\n\thook_called = false;\n\tassert_d_eq(3, func_to_hook(1, 2), \"Hooking changed return value.\");\n\tassert_false(hook_called, \"Nulling out hook didn't take.\");\n}\nTEST_END\n\nTEST_BEGIN(hooked_call) {\n\ttest_hooks_libc_hook = &hook;\n\thook_called = false;\n\tassert_d_eq(3, func_to_hook(1, 2), \"Hooking changed return value.\");\n\tassert_true(hook_called, \"Hook should have executed.\");\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    unhooked_call,\n\t    hooked_call);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/ticker.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/ticker.h\"\n\nTEST_BEGIN(test_ticker_tick) {\n#define NREPS 2\n#define NTICKS 3\n\tticker_t ticker;\n\tint32_t i, j;\n\n\tticker_init(&ticker, NTICKS);\n\tfor (i = 0; i < NREPS; i++) {\n\t\tfor (j = 0; j < NTICKS; j++) {\n\t\t\tassert_u_eq(ticker_read(&ticker), NTICKS - j,\n\t\t\t    \"Unexpected ticker value (i=%d, j=%d)\", i, j);\n\t\t\tassert_false(ticker_tick(&ticker),\n\t\t\t    \"Unexpected ticker fire (i=%d, j=%d)\", i, j);\n\t\t}\n\t\tassert_u32_eq(ticker_read(&ticker), 0,\n\t\t    \"Expected ticker depletion\");\n\t\tassert_true(ticker_tick(&ticker),\n\t\t    \"Expected ticker fire (i=%d)\", i);\n\t\tassert_u32_eq(ticker_read(&ticker), NTICKS,\n\t\t    \"Expected ticker reset\");\n\t}\n#undef NTICKS\n}\nTEST_END\n\nTEST_BEGIN(test_ticker_ticks) {\n#define NTICKS 3\n\tticker_t ticker;\n\n\tticker_init(&ticker, NTICKS);\n\n\tassert_u_eq(ticker_read(&ticker), NTICKS, \"Unexpected ticker value\");\n\tassert_false(ticker_ticks(&ticker, NTICKS), \"Unexpected ticker fire\");\n\tassert_u_eq(ticker_read(&ticker), 0, \"Unexpected ticker value\");\n\tassert_true(ticker_ticks(&ticker, NTICKS), \"Expected ticker fire\");\n\tassert_u_eq(ticker_read(&ticker), NTICKS, \"Unexpected ticker value\");\n\n\tassert_true(ticker_ticks(&ticker, NTICKS + 1), \"Expected ticker fire\");\n\tassert_u_eq(ticker_read(&ticker), NTICKS, \"Unexpected ticker value\");\n#undef NTICKS\n}\nTEST_END\n\nTEST_BEGIN(test_ticker_copy) {\n#define NTICKS 3\n\tticker_t ta, tb;\n\n\tticker_init(&ta, NTICKS);\n\tticker_copy(&tb, &ta);\n\tassert_u_eq(ticker_read(&tb), NTICKS, \"Unexpected ticker value\");\n\tassert_true(ticker_ticks(&tb, NTICKS + 1), \"Expected ticker fire\");\n\tassert_u_eq(ticker_read(&tb), NTICKS, \"Unexpected ticker value\");\n\n\tticker_tick(&ta);\n\tticker_copy(&tb, &ta);\n\tassert_u_eq(ticker_read(&tb), NTICKS - 1, \"Unexpected ticker value\");\n\tassert_true(ticker_ticks(&tb, NTICKS), \"Expected ticker fire\");\n\tassert_u_eq(ticker_read(&tb), NTICKS, \"Unexpected ticker value\");\n#undef NTICKS\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_ticker_tick,\n\t    test_ticker_ticks,\n\t    test_ticker_copy);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/tsd.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n/*\n * If we're e.g. in debug mode, we *never* enter the fast path, and so shouldn't\n * be asserting that we're on one.\n */\nstatic bool originally_fast;\nstatic int data_cleanup_count;\n\nvoid\ndata_cleanup(int *data) {\n\tif (data_cleanup_count == 0) {\n\t\tassert_x_eq(*data, MALLOC_TSD_TEST_DATA_INIT,\n\t\t    \"Argument passed into cleanup function should match tsd \"\n\t\t    \"value\");\n\t}\n\t++data_cleanup_count;\n\n\t/*\n\t * Allocate during cleanup for two rounds, in order to assure that\n\t * jemalloc's internal tsd reinitialization happens.\n\t */\n\tbool reincarnate = false;\n\tswitch (*data) {\n\tcase MALLOC_TSD_TEST_DATA_INIT:\n\t\t*data = 1;\n\t\treincarnate = true;\n\t\tbreak;\n\tcase 1:\n\t\t*data = 2;\n\t\treincarnate = true;\n\t\tbreak;\n\tcase 2:\n\t\treturn;\n\tdefault:\n\t\tnot_reached();\n\t}\n\n\tif (reincarnate) {\n\t\tvoid *p = mallocx(1, 0);\n\t\tassert_ptr_not_null(p, \"Unexpeced mallocx() failure\");\n\t\tdallocx(p, 0);\n\t}\n}\n\nstatic void *\nthd_start(void *arg) {\n\tint d = (int)(uintptr_t)arg;\n\tvoid *p;\n\n\ttsd_t *tsd = tsd_fetch();\n\tassert_x_eq(tsd_test_data_get(tsd), MALLOC_TSD_TEST_DATA_INIT,\n\t    \"Initial tsd get should return initialization value\");\n\n\tp = malloc(1);\n\tassert_ptr_not_null(p, \"Unexpected malloc() failure\");\n\n\ttsd_test_data_set(tsd, d);\n\tassert_x_eq(tsd_test_data_get(tsd), d,\n\t    \"After tsd set, tsd get should return value that was set\");\n\n\td = 0;\n\tassert_x_eq(tsd_test_data_get(tsd), (int)(uintptr_t)arg,\n\t    \"Resetting local data should have no effect on tsd\");\n\n\ttsd_test_callback_set(tsd, &data_cleanup);\n\n\tfree(p);\n\treturn NULL;\n}\n\nTEST_BEGIN(test_tsd_main_thread) {\n\tthd_start((void *)(uintptr_t)0xa5f3e329);\n}\nTEST_END\n\nTEST_BEGIN(test_tsd_sub_thread) {\n\tthd_t thd;\n\n\tdata_cleanup_count = 0;\n\tthd_create(&thd, thd_start, (void *)MALLOC_TSD_TEST_DATA_INIT);\n\tthd_join(thd, NULL);\n\t/*\n\t * We reincarnate twice in the data cleanup, so it should execute at\n\t * least 3 times.\n\t */\n\tassert_x_ge(data_cleanup_count, 3,\n\t    \"Cleanup function should have executed multiple times.\");\n}\nTEST_END\n\nstatic void *\nthd_start_reincarnated(void *arg) {\n\ttsd_t *tsd = tsd_fetch();\n\tassert(tsd);\n\n\tvoid *p = malloc(1);\n\tassert_ptr_not_null(p, \"Unexpected malloc() failure\");\n\n\t/* Manually trigger reincarnation. */\n\tassert_ptr_not_null(tsd_arena_get(tsd),\n\t    \"Should have tsd arena set.\");\n\ttsd_cleanup((void *)tsd);\n\tassert_ptr_null(*tsd_arenap_get_unsafe(tsd),\n\t    \"TSD arena should have been cleared.\");\n\tassert_u_eq(tsd_state_get(tsd), tsd_state_purgatory,\n\t    \"TSD state should be purgatory\\n\");\n\n\tfree(p);\n\tassert_u_eq(tsd_state_get(tsd), tsd_state_reincarnated,\n\t    \"TSD state should be reincarnated\\n\");\n\tp = mallocx(1, MALLOCX_TCACHE_NONE);\n\tassert_ptr_not_null(p, \"Unexpected malloc() failure\");\n\tassert_ptr_null(*tsd_arenap_get_unsafe(tsd),\n\t    \"Should not have tsd arena set after reincarnation.\");\n\n\tfree(p);\n\ttsd_cleanup((void *)tsd);\n\tassert_ptr_null(*tsd_arenap_get_unsafe(tsd),\n\t    \"TSD arena should have been cleared after 2nd cleanup.\");\n\n\treturn NULL;\n}\n\nTEST_BEGIN(test_tsd_reincarnation) {\n\tthd_t thd;\n\tthd_create(&thd, thd_start_reincarnated, NULL);\n\tthd_join(thd, NULL);\n}\nTEST_END\n\ntypedef struct {\n\tatomic_u32_t phase;\n\tatomic_b_t error;\n} global_slow_data_t;\n\nstatic void *\nthd_start_global_slow(void *arg) {\n\t/* PHASE 0 */\n\tglobal_slow_data_t *data = (global_slow_data_t *)arg;\n\tfree(mallocx(1, 0));\n\n\ttsd_t *tsd = tsd_fetch();\n\t/*\n\t * No global slowness has happened yet; there was an error if we were\n\t * originally fast but aren't now.\n\t */\n\tatomic_store_b(&data->error, originally_fast && !tsd_fast(tsd),\n\t    ATOMIC_SEQ_CST);\n\tatomic_store_u32(&data->phase, 1, ATOMIC_SEQ_CST);\n\n\t/* PHASE 2 */\n\twhile (atomic_load_u32(&data->phase, ATOMIC_SEQ_CST) != 2) {\n\t}\n\tfree(mallocx(1, 0));\n\tatomic_store_b(&data->error, tsd_fast(tsd), ATOMIC_SEQ_CST);\n\tatomic_store_u32(&data->phase, 3, ATOMIC_SEQ_CST);\n\n\t/* PHASE 4 */\n\twhile (atomic_load_u32(&data->phase, ATOMIC_SEQ_CST) != 4) {\n\t}\n\tfree(mallocx(1, 0));\n\tatomic_store_b(&data->error, tsd_fast(tsd), ATOMIC_SEQ_CST);\n\tatomic_store_u32(&data->phase, 5, ATOMIC_SEQ_CST);\n\n\t/* PHASE 6 */\n\twhile (atomic_load_u32(&data->phase, ATOMIC_SEQ_CST) != 6) {\n\t}\n\tfree(mallocx(1, 0));\n\t/* Only one decrement so far. */\n\tatomic_store_b(&data->error, tsd_fast(tsd), ATOMIC_SEQ_CST);\n\tatomic_store_u32(&data->phase, 7, ATOMIC_SEQ_CST);\n\n\t/* PHASE 8 */\n\twhile (atomic_load_u32(&data->phase, ATOMIC_SEQ_CST) != 8) {\n\t}\n\tfree(mallocx(1, 0));\n\t/*\n\t * Both decrements happened; we should be fast again (if we ever\n\t * were)\n\t */\n\tatomic_store_b(&data->error, originally_fast && !tsd_fast(tsd),\n\t    ATOMIC_SEQ_CST);\n\tatomic_store_u32(&data->phase, 9, ATOMIC_SEQ_CST);\n\n\treturn NULL;\n}\n\nTEST_BEGIN(test_tsd_global_slow) {\n\tglobal_slow_data_t data = {ATOMIC_INIT(0), ATOMIC_INIT(false)};\n\t/*\n\t * Note that the \"mallocx\" here (vs. malloc) is important, since the\n\t * compiler is allowed to optimize away free(malloc(1)) but not\n\t * free(mallocx(1)).\n\t */\n\tfree(mallocx(1, 0));\n\ttsd_t *tsd = tsd_fetch();\n\toriginally_fast = tsd_fast(tsd);\n\n\tthd_t thd;\n\tthd_create(&thd, thd_start_global_slow, (void *)&data.phase);\n\t/* PHASE 1 */\n\twhile (atomic_load_u32(&data.phase, ATOMIC_SEQ_CST) != 1) {\n\t\t/*\n\t\t * We don't have a portable condvar/semaphore mechanism.\n\t\t * Spin-wait.\n\t\t */\n\t}\n\tassert_false(atomic_load_b(&data.error, ATOMIC_SEQ_CST), \"\");\n\ttsd_global_slow_inc(tsd_tsdn(tsd));\n\tfree(mallocx(1, 0));\n\tassert_false(tsd_fast(tsd), \"\");\n\tatomic_store_u32(&data.phase, 2, ATOMIC_SEQ_CST);\n\n\t/* PHASE 3 */\n\twhile (atomic_load_u32(&data.phase, ATOMIC_SEQ_CST) != 3) {\n\t}\n\tassert_false(atomic_load_b(&data.error, ATOMIC_SEQ_CST), \"\");\n\t/* Increase again, so that we can test multiple fast/slow changes. */\n\ttsd_global_slow_inc(tsd_tsdn(tsd));\n\tatomic_store_u32(&data.phase, 4, ATOMIC_SEQ_CST);\n\tfree(mallocx(1, 0));\n\tassert_false(tsd_fast(tsd), \"\");\n\n\t/* PHASE 5 */\n\twhile (atomic_load_u32(&data.phase, ATOMIC_SEQ_CST) != 5) {\n\t}\n\tassert_false(atomic_load_b(&data.error, ATOMIC_SEQ_CST), \"\");\n\ttsd_global_slow_dec(tsd_tsdn(tsd));\n\tatomic_store_u32(&data.phase, 6, ATOMIC_SEQ_CST);\n\t/* We only decreased once; things should still be slow. */\n\tfree(mallocx(1, 0));\n\tassert_false(tsd_fast(tsd), \"\");\n\n\t/* PHASE 7 */\n\twhile (atomic_load_u32(&data.phase, ATOMIC_SEQ_CST) != 7) {\n\t}\n\tassert_false(atomic_load_b(&data.error, ATOMIC_SEQ_CST), \"\");\n\ttsd_global_slow_dec(tsd_tsdn(tsd));\n\tatomic_store_u32(&data.phase, 8, ATOMIC_SEQ_CST);\n\t/* We incremented and then decremented twice; we should be fast now. */\n\tfree(mallocx(1, 0));\n\tassert_true(!originally_fast || tsd_fast(tsd), \"\");\n\n\t/* PHASE 9 */\n\twhile (atomic_load_u32(&data.phase, ATOMIC_SEQ_CST) != 9) {\n\t}\n\tassert_false(atomic_load_b(&data.error, ATOMIC_SEQ_CST), \"\");\n\n\tthd_join(thd, NULL);\n}\nTEST_END\n\nint\nmain(void) {\n\t/* Ensure tsd bootstrapped. */\n\tif (nallocx(1, 0) == 0) {\n\t\tmalloc_printf(\"Initialization error\");\n\t\treturn test_status_fail;\n\t}\n\n\treturn test_no_reentrancy(\n\t    test_tsd_main_thread,\n\t    test_tsd_sub_thread,\n\t    test_tsd_reincarnation,\n\t    test_tsd_global_slow);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/witness.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic witness_lock_error_t *witness_lock_error_orig;\nstatic witness_owner_error_t *witness_owner_error_orig;\nstatic witness_not_owner_error_t *witness_not_owner_error_orig;\nstatic witness_depth_error_t *witness_depth_error_orig;\n\nstatic bool saw_lock_error;\nstatic bool saw_owner_error;\nstatic bool saw_not_owner_error;\nstatic bool saw_depth_error;\n\nstatic void\nwitness_lock_error_intercept(const witness_list_t *witnesses,\n    const witness_t *witness) {\n\tsaw_lock_error = true;\n}\n\nstatic void\nwitness_owner_error_intercept(const witness_t *witness) {\n\tsaw_owner_error = true;\n}\n\nstatic void\nwitness_not_owner_error_intercept(const witness_t *witness) {\n\tsaw_not_owner_error = true;\n}\n\nstatic void\nwitness_depth_error_intercept(const witness_list_t *witnesses,\n    witness_rank_t rank_inclusive, unsigned depth) {\n\tsaw_depth_error = true;\n}\n\nstatic int\nwitness_comp(const witness_t *a, void *oa, const witness_t *b, void *ob) {\n\tassert_u_eq(a->rank, b->rank, \"Witnesses should have equal rank\");\n\n\tassert(oa == (void *)a);\n\tassert(ob == (void *)b);\n\n\treturn strcmp(a->name, b->name);\n}\n\nstatic int\nwitness_comp_reverse(const witness_t *a, void *oa, const witness_t *b,\n    void *ob) {\n\tassert_u_eq(a->rank, b->rank, \"Witnesses should have equal rank\");\n\n\tassert(oa == (void *)a);\n\tassert(ob == (void *)b);\n\n\treturn -strcmp(a->name, b->name);\n}\n\nTEST_BEGIN(test_witness) {\n\twitness_t a, b;\n\twitness_tsdn_t witness_tsdn = { WITNESS_TSD_INITIALIZER };\n\n\ttest_skip_if(!config_debug);\n\n\twitness_assert_lockless(&witness_tsdn);\n\twitness_assert_depth(&witness_tsdn, 0);\n\twitness_assert_depth_to_rank(&witness_tsdn, (witness_rank_t)1U, 0);\n\n\twitness_init(&a, \"a\", 1, NULL, NULL);\n\twitness_assert_not_owner(&witness_tsdn, &a);\n\twitness_lock(&witness_tsdn, &a);\n\twitness_assert_owner(&witness_tsdn, &a);\n\twitness_assert_depth(&witness_tsdn, 1);\n\twitness_assert_depth_to_rank(&witness_tsdn, (witness_rank_t)1U, 1);\n\twitness_assert_depth_to_rank(&witness_tsdn, (witness_rank_t)2U, 0);\n\n\twitness_init(&b, \"b\", 2, NULL, NULL);\n\twitness_assert_not_owner(&witness_tsdn, &b);\n\twitness_lock(&witness_tsdn, &b);\n\twitness_assert_owner(&witness_tsdn, &b);\n\twitness_assert_depth(&witness_tsdn, 2);\n\twitness_assert_depth_to_rank(&witness_tsdn, (witness_rank_t)1U, 2);\n\twitness_assert_depth_to_rank(&witness_tsdn, (witness_rank_t)2U, 1);\n\twitness_assert_depth_to_rank(&witness_tsdn, (witness_rank_t)3U, 0);\n\n\twitness_unlock(&witness_tsdn, &a);\n\twitness_assert_depth(&witness_tsdn, 1);\n\twitness_assert_depth_to_rank(&witness_tsdn, (witness_rank_t)1U, 1);\n\twitness_assert_depth_to_rank(&witness_tsdn, (witness_rank_t)2U, 1);\n\twitness_assert_depth_to_rank(&witness_tsdn, (witness_rank_t)3U, 0);\n\twitness_unlock(&witness_tsdn, &b);\n\n\twitness_assert_lockless(&witness_tsdn);\n\twitness_assert_depth(&witness_tsdn, 0);\n\twitness_assert_depth_to_rank(&witness_tsdn, (witness_rank_t)1U, 0);\n}\nTEST_END\n\nTEST_BEGIN(test_witness_comp) {\n\twitness_t a, b, c, d;\n\twitness_tsdn_t witness_tsdn = { WITNESS_TSD_INITIALIZER };\n\n\ttest_skip_if(!config_debug);\n\n\twitness_assert_lockless(&witness_tsdn);\n\n\twitness_init(&a, \"a\", 1, witness_comp, &a);\n\twitness_assert_not_owner(&witness_tsdn, &a);\n\twitness_lock(&witness_tsdn, &a);\n\twitness_assert_owner(&witness_tsdn, &a);\n\twitness_assert_depth(&witness_tsdn, 1);\n\n\twitness_init(&b, \"b\", 1, witness_comp, &b);\n\twitness_assert_not_owner(&witness_tsdn, &b);\n\twitness_lock(&witness_tsdn, &b);\n\twitness_assert_owner(&witness_tsdn, &b);\n\twitness_assert_depth(&witness_tsdn, 2);\n\twitness_unlock(&witness_tsdn, &b);\n\twitness_assert_depth(&witness_tsdn, 1);\n\n\twitness_lock_error_orig = witness_lock_error;\n\twitness_lock_error = witness_lock_error_intercept;\n\tsaw_lock_error = false;\n\n\twitness_init(&c, \"c\", 1, witness_comp_reverse, &c);\n\twitness_assert_not_owner(&witness_tsdn, &c);\n\tassert_false(saw_lock_error, \"Unexpected witness lock error\");\n\twitness_lock(&witness_tsdn, &c);\n\tassert_true(saw_lock_error, \"Expected witness lock error\");\n\twitness_unlock(&witness_tsdn, &c);\n\twitness_assert_depth(&witness_tsdn, 1);\n\n\tsaw_lock_error = false;\n\n\twitness_init(&d, \"d\", 1, NULL, NULL);\n\twitness_assert_not_owner(&witness_tsdn, &d);\n\tassert_false(saw_lock_error, \"Unexpected witness lock error\");\n\twitness_lock(&witness_tsdn, &d);\n\tassert_true(saw_lock_error, \"Expected witness lock error\");\n\twitness_unlock(&witness_tsdn, &d);\n\twitness_assert_depth(&witness_tsdn, 1);\n\n\twitness_unlock(&witness_tsdn, &a);\n\n\twitness_assert_lockless(&witness_tsdn);\n\n\twitness_lock_error = witness_lock_error_orig;\n}\nTEST_END\n\nTEST_BEGIN(test_witness_reversal) {\n\twitness_t a, b;\n\twitness_tsdn_t witness_tsdn = { WITNESS_TSD_INITIALIZER };\n\n\ttest_skip_if(!config_debug);\n\n\twitness_lock_error_orig = witness_lock_error;\n\twitness_lock_error = witness_lock_error_intercept;\n\tsaw_lock_error = false;\n\n\twitness_assert_lockless(&witness_tsdn);\n\n\twitness_init(&a, \"a\", 1, NULL, NULL);\n\twitness_init(&b, \"b\", 2, NULL, NULL);\n\n\twitness_lock(&witness_tsdn, &b);\n\twitness_assert_depth(&witness_tsdn, 1);\n\tassert_false(saw_lock_error, \"Unexpected witness lock error\");\n\twitness_lock(&witness_tsdn, &a);\n\tassert_true(saw_lock_error, \"Expected witness lock error\");\n\n\twitness_unlock(&witness_tsdn, &a);\n\twitness_assert_depth(&witness_tsdn, 1);\n\twitness_unlock(&witness_tsdn, &b);\n\n\twitness_assert_lockless(&witness_tsdn);\n\n\twitness_lock_error = witness_lock_error_orig;\n}\nTEST_END\n\nTEST_BEGIN(test_witness_recursive) {\n\twitness_t a;\n\twitness_tsdn_t witness_tsdn = { WITNESS_TSD_INITIALIZER };\n\n\ttest_skip_if(!config_debug);\n\n\twitness_not_owner_error_orig = witness_not_owner_error;\n\twitness_not_owner_error = witness_not_owner_error_intercept;\n\tsaw_not_owner_error = false;\n\n\twitness_lock_error_orig = witness_lock_error;\n\twitness_lock_error = witness_lock_error_intercept;\n\tsaw_lock_error = false;\n\n\twitness_assert_lockless(&witness_tsdn);\n\n\twitness_init(&a, \"a\", 1, NULL, NULL);\n\n\twitness_lock(&witness_tsdn, &a);\n\tassert_false(saw_lock_error, \"Unexpected witness lock error\");\n\tassert_false(saw_not_owner_error, \"Unexpected witness not owner error\");\n\twitness_lock(&witness_tsdn, &a);\n\tassert_true(saw_lock_error, \"Expected witness lock error\");\n\tassert_true(saw_not_owner_error, \"Expected witness not owner error\");\n\n\twitness_unlock(&witness_tsdn, &a);\n\n\twitness_assert_lockless(&witness_tsdn);\n\n\twitness_owner_error = witness_owner_error_orig;\n\twitness_lock_error = witness_lock_error_orig;\n\n}\nTEST_END\n\nTEST_BEGIN(test_witness_unlock_not_owned) {\n\twitness_t a;\n\twitness_tsdn_t witness_tsdn = { WITNESS_TSD_INITIALIZER };\n\n\ttest_skip_if(!config_debug);\n\n\twitness_owner_error_orig = witness_owner_error;\n\twitness_owner_error = witness_owner_error_intercept;\n\tsaw_owner_error = false;\n\n\twitness_assert_lockless(&witness_tsdn);\n\n\twitness_init(&a, \"a\", 1, NULL, NULL);\n\n\tassert_false(saw_owner_error, \"Unexpected owner error\");\n\twitness_unlock(&witness_tsdn, &a);\n\tassert_true(saw_owner_error, \"Expected owner error\");\n\n\twitness_assert_lockless(&witness_tsdn);\n\n\twitness_owner_error = witness_owner_error_orig;\n}\nTEST_END\n\nTEST_BEGIN(test_witness_depth) {\n\twitness_t a;\n\twitness_tsdn_t witness_tsdn = { WITNESS_TSD_INITIALIZER };\n\n\ttest_skip_if(!config_debug);\n\n\twitness_depth_error_orig = witness_depth_error;\n\twitness_depth_error = witness_depth_error_intercept;\n\tsaw_depth_error = false;\n\n\twitness_assert_lockless(&witness_tsdn);\n\twitness_assert_depth(&witness_tsdn, 0);\n\n\twitness_init(&a, \"a\", 1, NULL, NULL);\n\n\tassert_false(saw_depth_error, \"Unexpected depth error\");\n\twitness_assert_lockless(&witness_tsdn);\n\twitness_assert_depth(&witness_tsdn, 0);\n\n\twitness_lock(&witness_tsdn, &a);\n\twitness_assert_lockless(&witness_tsdn);\n\twitness_assert_depth(&witness_tsdn, 0);\n\tassert_true(saw_depth_error, \"Expected depth error\");\n\n\twitness_unlock(&witness_tsdn, &a);\n\n\twitness_assert_lockless(&witness_tsdn);\n\twitness_assert_depth(&witness_tsdn, 0);\n\n\twitness_depth_error = witness_depth_error_orig;\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_witness,\n\t    test_witness_comp,\n\t    test_witness_reversal,\n\t    test_witness_recursive,\n\t    test_witness_unlock_not_owned,\n\t    test_witness_depth);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/zero.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic void\ntest_zero(size_t sz_min, size_t sz_max) {\n\tuint8_t *s;\n\tsize_t sz_prev, sz, i;\n#define MAGIC\t((uint8_t)0x61)\n\n\tsz_prev = 0;\n\ts = (uint8_t *)mallocx(sz_min, 0);\n\tassert_ptr_not_null((void *)s, \"Unexpected mallocx() failure\");\n\n\tfor (sz = sallocx(s, 0); sz <= sz_max;\n\t    sz_prev = sz, sz = sallocx(s, 0)) {\n\t\tif (sz_prev > 0) {\n\t\t\tassert_u_eq(s[0], MAGIC,\n\t\t\t    \"Previously allocated byte %zu/%zu is corrupted\",\n\t\t\t    ZU(0), sz_prev);\n\t\t\tassert_u_eq(s[sz_prev-1], MAGIC,\n\t\t\t    \"Previously allocated byte %zu/%zu is corrupted\",\n\t\t\t    sz_prev-1, sz_prev);\n\t\t}\n\n\t\tfor (i = sz_prev; i < sz; i++) {\n\t\t\tassert_u_eq(s[i], 0x0,\n\t\t\t    \"Newly allocated byte %zu/%zu isn't zero-filled\",\n\t\t\t    i, sz);\n\t\t\ts[i] = MAGIC;\n\t\t}\n\n\t\tif (xallocx(s, sz+1, 0, 0) == sz) {\n\t\t\ts = (uint8_t *)rallocx(s, sz+1, 0);\n\t\t\tassert_ptr_not_null((void *)s,\n\t\t\t    \"Unexpected rallocx() failure\");\n\t\t}\n\t}\n\n\tdallocx(s, 0);\n#undef MAGIC\n}\n\nTEST_BEGIN(test_zero_small) {\n\ttest_skip_if(!config_fill);\n\ttest_zero(1, SC_SMALL_MAXCLASS - 1);\n}\nTEST_END\n\nTEST_BEGIN(test_zero_large) {\n\ttest_skip_if(!config_fill);\n\ttest_zero(SC_SMALL_MAXCLASS + 1, 1U << (SC_LG_LARGE_MINCLASS + 1));\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_zero_small,\n\t    test_zero_large);\n}\n"
  },
  {
    "path": "deps/jemalloc/test/unit/zero.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_fill}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"abort:false,junk:false,zero:true\"\nfi\n"
  },
  {
    "path": "deps/license/keycheck.h",
    "content": "#pragma once\n\nbool FValidKey(const char *key, size_t cch);\n"
  },
  {
    "path": "deps/linenoise/.gitignore",
    "content": "linenoise_example\n*.dSYM\nhistory.txt\n"
  },
  {
    "path": "deps/linenoise/Makefile",
    "content": "STD=\nWARN= -Wall\nOPT= -Os\n\nR_CFLAGS= $(STD) $(WARN) $(OPT) $(DEBUG) $(CFLAGS)\nR_LDFLAGS= $(LDFLAGS)\nDEBUG= -g\n\nR_CC=$(CC) $(R_CFLAGS)\nR_LD=$(CC) $(R_LDFLAGS)\n\nlinenoise.o: linenoise.h linenoise.c\n\nlinenoise_example: linenoise.o example.o\n\t$(R_LD) -o $@ $^\n\n.c.o:\n\t$(R_CC) -c $<\n\nclean:\n\trm -f linenoise_example *.o\n"
  },
  {
    "path": "deps/linenoise/README.markdown",
    "content": "# Linenoise\n\nA minimal, zero-config, BSD licensed, readline replacement used in Redis,\nMongoDB, and Android.\n\n* Single and multi line editing mode with the usual key bindings implemented.\n* History handling.\n* Completion.\n* Hints (suggestions at the right of the prompt as you type).\n* About 1,100 lines of BSD license source code.\n* Only uses a subset of VT100 escapes (ANSI.SYS compatible).\n\n## Can a line editing library be 20k lines of code?\n\nLine editing with some support for history is a really important feature for command line utilities. Instead of retyping almost the same stuff again and again it's just much better to hit the up arrow and edit on syntax errors, or in order to try a slightly different command. But apparently code dealing with terminals is some sort of Black Magic: readline is 30k lines of code, libedit 20k. Is it reasonable to link small utilities to huge libraries just to get a minimal support for line editing?\n\nSo what usually happens is either:\n\n * Large programs with configure scripts disabling line editing if readline is not present in the system, or not supporting it at all since readline is GPL licensed and libedit (the BSD clone) is not as known and available as readline is (Real world example of this problem: Tclsh).\n * Smaller programs not using a configure script not supporting line editing at all (A problem we had with Redis-cli for instance).\n \nThe result is a pollution of binaries without line editing support.\n\nSo I spent more or less two hours doing a reality check resulting in this little library: is it *really* needed for a line editing library to be 20k lines of code? Apparently not, it is possibe to get a very small, zero configuration, trivial to embed library, that solves the problem. Smaller programs will just include this, supporting line editing out of the box. Larger programs may use this little library or just checking with configure if readline/libedit is available and resorting to Linenoise if not.\n\n## Terminals, in 2010.\n\nApparently almost every terminal you can happen to use today has some kind of support for basic VT100 escape sequences. So I tried to write a lib using just very basic VT100 features. The resulting library appears to work everywhere I tried to use it, and now can work even on ANSI.SYS compatible terminals, since no\nVT220 specific sequences are used anymore.\n\nThe library is currently about 1100 lines of code. In order to use it in your project just look at the *example.c* file in the source distribution, it is trivial. Linenoise is BSD code, so you can use both in free software and commercial software.\n\n## Tested with...\n\n * Linux text only console ($TERM = linux)\n * Linux KDE terminal application ($TERM = xterm)\n * Linux xterm ($TERM = xterm)\n * Linux Buildroot ($TERM = vt100)\n * Mac OS X iTerm ($TERM = xterm)\n * Mac OS X default Terminal.app ($TERM = xterm)\n * OpenBSD 4.5 through an OSX Terminal.app ($TERM = screen)\n * IBM AIX 6.1\n * FreeBSD xterm ($TERM = xterm)\n * ANSI.SYS\n * Emacs comint mode ($TERM = dumb)\n\nPlease test it everywhere you can and report back!\n\n## Let's push this forward!\n\nPatches should be provided in the respect of Linenoise sensibility for small\neasy to understand code.\n\nSend feedbacks to antirez at gmail\n\n# The API\n\nLinenoise is very easy to use, and reading the example shipped with the\nlibrary should get you up to speed ASAP. Here is a list of API calls\nand how to use them.\n\n    char *linenoise(const char *prompt);\n\nThis is the main Linenoise call: it shows the user a prompt with line editing\nand history capabilities. The prompt you specify is used as a prompt, that is,\nit will be printed to the left of the cursor. The library returns a buffer\nwith the line composed by the user, or NULL on end of file or when there\nis an out of memory condition.\n\nWhen a tty is detected (the user is actually typing into a terminal session)\nthe maximum editable line length is `LINENOISE_MAX_LINE`. When instead the\nstandard input is not a tty, which happens every time you redirect a file\nto a program, or use it in an Unix pipeline, there are no limits to the\nlength of the line that can be returned.\n\nThe returned line should be freed with the `free()` standard system call.\nHowever sometimes it could happen that your program uses a different dynamic\nallocation library, so you may also used `linenoiseFree` to make sure the\nline is freed with the same allocator it was created.\n\nThe canonical loop used by a program using Linenoise will be something like\nthis:\n\n    while((line = linenoise(\"hello> \")) != NULL) {\n        printf(\"You wrote: %s\\n\", line);\n        linenoiseFree(line); /* Or just free(line) if you use libc malloc. */\n    }\n\n## Single line VS multi line editing\n\nBy default, Linenoise uses single line editing, that is, a single row on the\nscreen will be used, and as the user types more, the text will scroll towards\nleft to make room. This works if your program is one where the user is\nunlikely to write a lot of text, otherwise multi line editing, where multiple\nscreens rows are used, can be a lot more comfortable.\n\nIn order to enable multi line editing use the following API call:\n\n    linenoiseSetMultiLine(1);\n\nYou can disable it using `0` as argument.\n\n## History\n\nLinenoise supporst history, so that the user does not have to retype\nagain and again the same things, but can use the down and up arrows in order\nto search and re-edit already inserted lines of text.\n\nThe followings are the history API calls:\n\n    int linenoiseHistoryAdd(const char *line);\n    int linenoiseHistorySetMaxLen(int len);\n    int linenoiseHistorySave(const char *filename);\n    int linenoiseHistoryLoad(const char *filename);\n\nUse `linenoiseHistoryAdd` every time you want to add a new element\nto the top of the history (it will be the first the user will see when\nusing the up arrow).\n\nNote that for history to work, you have to set a length for the history\n(which is zero by default, so history will be disabled if you don't set\na proper one). This is accomplished using the `linenoiseHistorySetMaxLen`\nfunction.\n\nLinenoise has direct support for persisting the history into an history\nfile. The functions `linenoiseHistorySave` and `linenoiseHistoryLoad` do\njust that. Both functions return -1 on error and 0 on success.\n\n## Mask mode\n\nSometimes it is useful to allow the user to type passwords or other\nsecrets that should not be displayed. For such situations linenoise supports\na \"mask mode\" that will just replace the characters the user is typing \nwith `*` characters, like in the following example:\n\n    $ ./linenoise_example\n    hello> get mykey\n    echo: 'get mykey'\n    hello> /mask\n    hello> *********\n\nYou can enable and disable mask mode using the following two functions:\n\n    void linenoiseMaskModeEnable(void);\n    void linenoiseMaskModeDisable(void);\n\n## Completion\n\nLinenoise supports completion, which is the ability to complete the user\ninput when she or he presses the `<TAB>` key.\n\nIn order to use completion, you need to register a completion callback, which\nis called every time the user presses `<TAB>`. Your callback will return a\nlist of items that are completions for the current string.\n\nThe following is an example of registering a completion callback:\n\n    linenoiseSetCompletionCallback(completion);\n\nThe completion must be a function returning `void` and getting as input\na `const char` pointer, which is the line the user has typed so far, and\na `linenoiseCompletions` object pointer, which is used as argument of\n`linenoiseAddCompletion` in order to add completions inside the callback.\nAn example will make it more clear:\n\n    void completion(const char *buf, linenoiseCompletions *lc) {\n        if (buf[0] == 'h') {\n            linenoiseAddCompletion(lc,\"hello\");\n            linenoiseAddCompletion(lc,\"hello there\");\n        }\n    }\n\nBasically in your completion callback, you inspect the input, and return\na list of items that are good completions by using `linenoiseAddCompletion`.\n\nIf you want to test the completion feature, compile the example program\nwith `make`, run it, type `h` and press `<TAB>`.\n\n## Hints\n\nLinenoise has a feature called *hints* which is very useful when you\nuse Linenoise in order to implement a REPL (Read Eval Print Loop) for\na program that accepts commands and arguments, but may also be useful in\nother conditions.\n\nThe feature shows, on the right of the cursor, as the user types, hints that\nmay be useful. The hints can be displayed using a different color compared\nto the color the user is typing, and can also be bold.\n\nFor example as the user starts to type `\"git remote add\"`, with hints it's\npossible to show on the right of the prompt a string `<name> <url>`.\n\nThe feature works similarly to the history feature, using a callback.\nTo register the callback we use:\n\n    linenoiseSetHintsCallback(hints);\n\nThe callback itself is implemented like this:\n\n    char *hints(const char *buf, int *color, int *bold) {\n        if (!strcasecmp(buf,\"git remote add\")) {\n            *color = 35;\n            *bold = 0;\n            return \" <name> <url>\";\n        }\n        return NULL;\n    }\n\nThe callback function returns the string that should be displayed or NULL\nif no hint is available for the text the user currently typed. The returned\nstring will be trimmed as needed depending on the number of columns available\non the screen.\n\nIt is possible to return a string allocated in dynamic way, by also registering\na function to deallocate the hint string once used:\n\n    void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *);\n\nThe free hint callback will just receive the pointer and free the string\nas needed (depending on how the hits callback allocated it).\n\nAs you can see in the example above, a `color` (in xterm color terminal codes)\ncan be provided together with a `bold` attribute. If no color is set, the\ncurrent terminal foreground color is used. If no bold attribute is set,\nnon-bold text is printed.\n\nColor codes are:\n\n    red = 31\n    green = 32\n    yellow = 33\n    blue = 34\n    magenta = 35\n    cyan = 36\n    white = 37;\n\n## Screen handling\n\nSometimes you may want to clear the screen as a result of something the\nuser typed. You can do this by calling the following function:\n\n    void linenoiseClearScreen(void);\n\n## Related projects\n\n* [Linenoise NG](https://github.com/arangodb/linenoise-ng) is a fork of Linenoise that aims to add more advanced features like UTF-8 support, Windows support and other features. Uses C++ instead of C as development language.\n* [Linenoise-swift](https://github.com/andybest/linenoise-swift) is a reimplementation of Linenoise written in Swift.\n"
  },
  {
    "path": "deps/linenoise/example.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include \"linenoise.h\"\n\n\nvoid completion(const char *buf, linenoiseCompletions *lc) {\n    if (buf[0] == 'h') {\n        linenoiseAddCompletion(lc,\"hello\");\n        linenoiseAddCompletion(lc,\"hello there\");\n    }\n}\n\nchar *hints(const char *buf, int *color, int *bold) {\n    if (!strcasecmp(buf,\"hello\")) {\n        *color = 35;\n        *bold = 0;\n        return \" World\";\n    }\n    return NULL;\n}\n\nint main(int argc, char **argv) {\n    char *line;\n    char *prgname = argv[0];\n\n    /* Parse options, with --multiline we enable multi line editing. */\n    while(argc > 1) {\n        argc--;\n        argv++;\n        if (!strcmp(*argv,\"--multiline\")) {\n            linenoiseSetMultiLine(1);\n            printf(\"Multi-line mode enabled.\\n\");\n        } else if (!strcmp(*argv,\"--keycodes\")) {\n            linenoisePrintKeyCodes();\n            exit(0);\n        } else {\n            fprintf(stderr, \"Usage: %s [--multiline] [--keycodes]\\n\", prgname);\n            exit(1);\n        }\n    }\n\n    /* Set the completion callback. This will be called every time the\n     * user uses the <tab> key. */\n    linenoiseSetCompletionCallback(completion);\n    linenoiseSetHintsCallback(hints);\n\n    /* Load history from file. The history file is just a plain text file\n     * where entries are separated by newlines. */\n    linenoiseHistoryLoad(\"history.txt\"); /* Load the history at startup */\n\n    /* Now this is the main loop of the typical linenoise-based application.\n     * The call to linenoise() will block as long as the user types something\n     * and presses enter.\n     *\n     * The typed string is returned as a malloc() allocated string by\n     * linenoise, so the user needs to free() it. */\n    \n    while((line = linenoise(\"hello> \")) != NULL) {\n        /* Do something with the string. */\n        if (line[0] != '\\0' && line[0] != '/') {\n            printf(\"echo: '%s'\\n\", line);\n            linenoiseHistoryAdd(line); /* Add to the history. */\n            linenoiseHistorySave(\"history.txt\"); /* Save the history on disk. */\n        } else if (!strncmp(line,\"/historylen\",11)) {\n            /* The \"/historylen\" command will change the history len. */\n            int len = atoi(line+11);\n            linenoiseHistorySetMaxLen(len);\n        } else if (!strncmp(line, \"/mask\", 5)) {\n            linenoiseMaskModeEnable();\n        } else if (!strncmp(line, \"/unmask\", 7)) {\n            linenoiseMaskModeDisable();\n        } else if (line[0] == '/') {\n            printf(\"Unreconized command: %s\\n\", line);\n        }\n        free(line);\n    }\n    return 0;\n}\n"
  },
  {
    "path": "deps/linenoise/linenoise.c",
    "content": "/* linenoise.c -- guerrilla line editing library against the idea that a\n * line editing lib needs to be 20,000 lines of C code.\n *\n * You can find the latest source code at:\n *\n *   http://github.com/antirez/linenoise\n *\n * Does a number of crazy assumptions that happen to be true in 99.9999% of\n * the 2010 UNIX computers around.\n *\n * ------------------------------------------------------------------------\n *\n * Copyright (c) 2010-2016, Salvatore Sanfilippo <antirez at gmail dot com>\n * Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>\n *\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are\n * met:\n *\n *  *  Redistributions of source code must retain the above copyright\n *     notice, this list of conditions and the following disclaimer.\n *\n *  *  Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n * \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n *\n * ------------------------------------------------------------------------\n *\n * References:\n * - http://invisible-island.net/xterm/ctlseqs/ctlseqs.html\n * - http://www.3waylabs.com/nw/WWW/products/wizcon/vt220.html\n *\n * Todo list:\n * - Filter bogus Ctrl+<char> combinations.\n * - Win32 support\n *\n * Bloat:\n * - History search like Ctrl+r in readline?\n *\n * List of escape sequences used by this program, we do everything just\n * with three sequences. In order to be so cheap we may have some\n * flickering effect with some slow terminal, but the lesser sequences\n * the more compatible.\n *\n * EL (Erase Line)\n *    Sequence: ESC [ n K\n *    Effect: if n is 0 or missing, clear from cursor to end of line\n *    Effect: if n is 1, clear from beginning of line to cursor\n *    Effect: if n is 2, clear entire line\n *\n * CUF (CUrsor Forward)\n *    Sequence: ESC [ n C\n *    Effect: moves cursor forward n chars\n *\n * CUB (CUrsor Backward)\n *    Sequence: ESC [ n D\n *    Effect: moves cursor backward n chars\n *\n * The following is used to get the terminal width if getting\n * the width with the TIOCGWINSZ ioctl fails\n *\n * DSR (Device Status Report)\n *    Sequence: ESC [ 6 n\n *    Effect: reports the current cusor position as ESC [ n ; m R\n *            where n is the row and m is the column\n *\n * When multi line mode is enabled, we also use an additional escape\n * sequence. However multi line editing is disabled by default.\n *\n * CUU (Cursor Up)\n *    Sequence: ESC [ n A\n *    Effect: moves cursor up of n chars.\n *\n * CUD (Cursor Down)\n *    Sequence: ESC [ n B\n *    Effect: moves cursor down of n chars.\n *\n * When linenoiseClearScreen() is called, two additional escape sequences\n * are used in order to clear the screen and position the cursor at home\n * position.\n *\n * CUP (Cursor position)\n *    Sequence: ESC [ H\n *    Effect: moves the cursor to upper left corner\n *\n * ED (Erase display)\n *    Sequence: ESC [ 2 J\n *    Effect: clear the whole screen\n *\n */\n\n#include <termios.h>\n#include <unistd.h>\n#include <stdlib.h>\n#include <stdio.h>\n#include <errno.h>\n#include <string.h>\n#include <stdlib.h>\n#include <ctype.h>\n#include <sys/stat.h>\n#include <sys/types.h>\n#include <sys/ioctl.h>\n#include <unistd.h>\n#include \"linenoise.h\"\n\n#define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100\n#define LINENOISE_MAX_LINE 4096\nstatic char *unsupported_term[] = {\"dumb\",\"cons25\",\"emacs\",NULL};\nstatic linenoiseCompletionCallback *completionCallback = NULL;\nstatic linenoiseHintsCallback *hintsCallback = NULL;\nstatic linenoiseFreeHintsCallback *freeHintsCallback = NULL;\n\nstatic struct termios orig_termios; /* In order to restore at exit.*/\nstatic int maskmode = 0; /* Show \"***\" instead of input. For passwords. */\nstatic int rawmode = 0; /* For atexit() function to check if restore is needed*/\nstatic int mlmode = 0;  /* Multi line mode. Default is single line. */\nstatic int atexit_registered = 0; /* Register atexit just 1 time. */\nstatic int history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN;\nstatic int history_len = 0;\nstatic char **history = NULL;\n\n/* The linenoiseState structure represents the state during line editing.\n * We pass this state to functions implementing specific editing\n * functionalities. */\nstruct linenoiseState {\n    int ifd;            /* Terminal stdin file descriptor. */\n    int ofd;            /* Terminal stdout file descriptor. */\n    char *buf;          /* Edited line buffer. */\n    size_t buflen;      /* Edited line buffer size. */\n    const char *prompt; /* Prompt to display. */\n    size_t plen;        /* Prompt length. */\n    size_t pos;         /* Current cursor position. */\n    size_t oldpos;      /* Previous refresh cursor position. */\n    size_t len;         /* Current edited line length. */\n    size_t cols;        /* Number of columns in terminal. */\n    size_t maxrows;     /* Maximum num of rows used so far (multiline mode) */\n    int history_index;  /* The history index we are currently editing. */\n};\n\nenum KEY_ACTION{\n\tKEY_NULL = 0,\t    /* NULL */\n\tCTRL_A = 1,         /* Ctrl+a */\n\tCTRL_B = 2,         /* Ctrl-b */\n\tCTRL_C = 3,         /* Ctrl-c */\n\tCTRL_D = 4,         /* Ctrl-d */\n\tCTRL_E = 5,         /* Ctrl-e */\n\tCTRL_F = 6,         /* Ctrl-f */\n\tCTRL_H = 8,         /* Ctrl-h */\n\tTAB = 9,            /* Tab */\n\tCTRL_K = 11,        /* Ctrl+k */\n\tCTRL_L = 12,        /* Ctrl+l */\n\tENTER = 13,         /* Enter */\n\tCTRL_N = 14,        /* Ctrl-n */\n\tCTRL_P = 16,        /* Ctrl-p */\n\tCTRL_T = 20,        /* Ctrl-t */\n\tCTRL_U = 21,        /* Ctrl+u */\n\tCTRL_W = 23,        /* Ctrl+w */\n\tESC = 27,           /* Escape */\n\tBACKSPACE =  127    /* Backspace */\n};\n\nstatic void linenoiseAtExit(void);\nint linenoiseHistoryAdd(const char *line);\nstatic void refreshLine(struct linenoiseState *l);\n\n/* Debugging macro. */\n#if 0\nFILE *lndebug_fp = NULL;\n#define lndebug(...) \\\n    do { \\\n        if (lndebug_fp == NULL) { \\\n            lndebug_fp = fopen(\"/tmp/lndebug.txt\",\"a\"); \\\n            fprintf(lndebug_fp, \\\n            \"[%d %d %d] p: %d, rows: %d, rpos: %d, max: %d, oldmax: %d\\n\", \\\n            (int)l->len,(int)l->pos,(int)l->oldpos,plen,rows,rpos, \\\n            (int)l->maxrows,old_rows); \\\n        } \\\n        fprintf(lndebug_fp, \", \" __VA_ARGS__); \\\n        fflush(lndebug_fp); \\\n    } while (0)\n#else\n#define lndebug(fmt, ...)\n#endif\n\n/* ======================= Low level terminal handling ====================== */\n\n/* Enable \"mask mode\". When it is enabled, instead of the input that\n * the user is typing, the terminal will just display a corresponding\n * number of asterisks, like \"****\". This is useful for passwords and other\n * secrets that should not be displayed. */\nvoid linenoiseMaskModeEnable(void) {\n    maskmode = 1;\n}\n\n/* Disable mask mode. */\nvoid linenoiseMaskModeDisable(void) {\n    maskmode = 0;\n}\n\n/* Set if to use or not the multi line mode. */\nvoid linenoiseSetMultiLine(int ml) {\n    mlmode = ml;\n}\n\n/* Return true if the terminal name is in the list of terminals we know are\n * not able to understand basic escape sequences. */\nstatic int isUnsupportedTerm(void) {\n    char *term = getenv(\"TERM\");\n    int j;\n\n    if (term == NULL) return 0;\n    for (j = 0; unsupported_term[j]; j++)\n        if (!strcasecmp(term,unsupported_term[j])) return 1;\n    return 0;\n}\n\n/* Raw mode: 1960 magic shit. */\nstatic int enableRawMode(int fd) {\n    struct termios raw;\n\n    if (!isatty(STDIN_FILENO)) goto fatal;\n    if (!atexit_registered) {\n        atexit(linenoiseAtExit);\n        atexit_registered = 1;\n    }\n    if (tcgetattr(fd,&orig_termios) == -1) goto fatal;\n\n    raw = orig_termios;  /* modify the original mode */\n    /* input modes: no break, no CR to NL, no parity check, no strip char,\n     * no start/stop output control. */\n    raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);\n    /* output modes - disable post processing */\n    raw.c_oflag &= ~(OPOST);\n    /* control modes - set 8 bit chars */\n    raw.c_cflag |= (CS8);\n    /* local modes - choing off, canonical off, no extended functions,\n     * no signal chars (^Z,^C) */\n    raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);\n    /* control chars - set return condition: min number of bytes and timer.\n     * We want read to return every single byte, without timeout. */\n    raw.c_cc[VMIN] = 1; raw.c_cc[VTIME] = 0; /* 1 byte, no timer */\n\n    /* put terminal in raw mode after flushing */\n    if (tcsetattr(fd,TCSAFLUSH,&raw) < 0) goto fatal;\n    rawmode = 1;\n    return 0;\n\nfatal:\n    errno = ENOTTY;\n    return -1;\n}\n\nstatic void disableRawMode(int fd) {\n    /* Don't even check the return value as it's too late. */\n    if (rawmode && tcsetattr(fd,TCSAFLUSH,&orig_termios) != -1)\n        rawmode = 0;\n}\n\n/* Use the ESC [6n escape sequence to query the horizontal cursor position\n * and return it. On error -1 is returned, on success the position of the\n * cursor. */\nstatic int getCursorPosition(int ifd, int ofd) {\n    char buf[32];\n    int cols, rows;\n    unsigned int i = 0;\n\n    /* Report cursor location */\n    if (write(ofd, \"\\x1b[6n\", 4) != 4) return -1;\n\n    /* Read the response: ESC [ rows ; cols R */\n    while (i < sizeof(buf)-1) {\n        if (read(ifd,buf+i,1) != 1) break;\n        if (buf[i] == 'R') break;\n        i++;\n    }\n    buf[i] = '\\0';\n\n    /* Parse it. */\n    if (buf[0] != ESC || buf[1] != '[') return -1;\n    if (sscanf(buf+2,\"%d;%d\",&rows,&cols) != 2) return -1;\n    return cols;\n}\n\n/* Try to get the number of columns in the current terminal, or assume 80\n * if it fails. */\nstatic int getColumns(int ifd, int ofd) {\n    struct winsize ws;\n\n    if (ioctl(1, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {\n        /* ioctl() failed. Try to query the terminal itself. */\n        int start, cols;\n\n        /* Get the initial position so we can restore it later. */\n        start = getCursorPosition(ifd,ofd);\n        if (start == -1) goto failed;\n\n        /* Go to right margin and get position. */\n        if (write(ofd,\"\\x1b[999C\",6) != 6) goto failed;\n        cols = getCursorPosition(ifd,ofd);\n        if (cols == -1) goto failed;\n\n        /* Restore position. */\n        if (cols > start) {\n            char seq[32];\n            snprintf(seq,32,\"\\x1b[%dD\",cols-start);\n            if (write(ofd,seq,strlen(seq)) == -1) {\n                /* Can't recover... */\n            }\n        }\n        return cols;\n    } else {\n        return ws.ws_col;\n    }\n\nfailed:\n    return 80;\n}\n\n/* Clear the screen. Used to handle ctrl+l */\nvoid linenoiseClearScreen(void) {\n    if (write(STDOUT_FILENO,\"\\x1b[H\\x1b[2J\",7) <= 0) {\n        /* nothing to do, just to avoid warning. */\n    }\n}\n\n/* Beep, used for completion when there is nothing to complete or when all\n * the choices were already shown. */\nstatic void linenoiseBeep(void) {\n    fprintf(stderr, \"\\x7\");\n    fflush(stderr);\n}\n\n/* ============================== Completion ================================ */\n\n/* Free a list of completion option populated by linenoiseAddCompletion(). */\nstatic void freeCompletions(linenoiseCompletions *lc) {\n    size_t i;\n    for (i = 0; i < lc->len; i++)\n        free(lc->cvec[i]);\n    if (lc->cvec != NULL)\n        free(lc->cvec);\n}\n\n/* This is an helper function for linenoiseEdit() and is called when the\n * user types the <tab> key in order to complete the string currently in the\n * input.\n *\n * The state of the editing is encapsulated into the pointed linenoiseState\n * structure as described in the structure definition. */\nstatic int completeLine(struct linenoiseState *ls) {\n    linenoiseCompletions lc = { 0, NULL };\n    int nread, nwritten;\n    char c = 0;\n\n    completionCallback(ls->buf,&lc);\n    if (lc.len == 0) {\n        linenoiseBeep();\n    } else {\n        size_t stop = 0, i = 0;\n\n        while(!stop) {\n            /* Show completion or original buffer */\n            if (i < lc.len) {\n                struct linenoiseState saved = *ls;\n\n                ls->len = ls->pos = strlen(lc.cvec[i]);\n                ls->buf = lc.cvec[i];\n                refreshLine(ls);\n                ls->len = saved.len;\n                ls->pos = saved.pos;\n                ls->buf = saved.buf;\n            } else {\n                refreshLine(ls);\n            }\n\n            nread = read(ls->ifd,&c,1);\n            if (nread <= 0) {\n                freeCompletions(&lc);\n                return -1;\n            }\n\n            switch(c) {\n                case 9: /* tab */\n                    i = (i+1) % (lc.len+1);\n                    if (i == lc.len) linenoiseBeep();\n                    break;\n                case 27: /* escape */\n                    /* Re-show original buffer */\n                    if (i < lc.len) refreshLine(ls);\n                    stop = 1;\n                    break;\n                default:\n                    /* Update buffer and return */\n                    if (i < lc.len) {\n                        nwritten = snprintf(ls->buf,ls->buflen,\"%s\",lc.cvec[i]);\n                        ls->len = ls->pos = nwritten;\n                    }\n                    stop = 1;\n                    break;\n            }\n        }\n    }\n\n    freeCompletions(&lc);\n    return c; /* Return last read character */\n}\n\n/* Register a callback function to be called for tab-completion. */\nvoid linenoiseSetCompletionCallback(linenoiseCompletionCallback *fn) {\n    completionCallback = fn;\n}\n\n/* Register a hits function to be called to show hits to the user at the\n * right of the prompt. */\nvoid linenoiseSetHintsCallback(linenoiseHintsCallback *fn) {\n    hintsCallback = fn;\n}\n\n/* Register a function to free the hints returned by the hints callback\n * registered with linenoiseSetHintsCallback(). */\nvoid linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *fn) {\n    freeHintsCallback = fn;\n}\n\n/* This function is used by the callback function registered by the user\n * in order to add completion options given the input string when the\n * user typed <tab>. See the example.c source code for a very easy to\n * understand example. */\nvoid linenoiseAddCompletion(linenoiseCompletions *lc, const char *str) {\n    size_t len = strlen(str);\n    char *copy, **cvec;\n\n    copy = malloc(len+1);\n    if (copy == NULL) return;\n    memcpy(copy,str,len+1);\n    cvec = realloc(lc->cvec,sizeof(char*)*(lc->len+1));\n    if (cvec == NULL) {\n        free(copy);\n        return;\n    }\n    lc->cvec = cvec;\n    lc->cvec[lc->len++] = copy;\n}\n\n/* =========================== Line editing ================================= */\n\n/* We define a very simple \"append buffer\" structure, that is an heap\n * allocated string where we can append to. This is useful in order to\n * write all the escape sequences in a buffer and flush them to the standard\n * output in a single call, to avoid flickering effects. */\nstruct abuf {\n    char *b;\n    int len;\n};\n\nstatic void abInit(struct abuf *ab) {\n    ab->b = NULL;\n    ab->len = 0;\n}\n\nstatic void abAppend(struct abuf *ab, const char *s, int len) {\n    char *new = realloc(ab->b,ab->len+len);\n\n    if (new == NULL) return;\n    memcpy(new+ab->len,s,len);\n    ab->b = new;\n    ab->len += len;\n}\n\nstatic void abFree(struct abuf *ab) {\n    free(ab->b);\n}\n\n/* Helper of refreshSingleLine() and refreshMultiLine() to show hints\n * to the right of the prompt. */\nvoid refreshShowHints(struct abuf *ab, struct linenoiseState *l, int plen) {\n    char seq[64];\n    if (hintsCallback && plen+l->len < l->cols) {\n        int color = -1, bold = 0;\n        char *hint = hintsCallback(l->buf,&color,&bold);\n        if (hint) {\n            int hintlen = strlen(hint);\n            int hintmaxlen = l->cols-(plen+l->len);\n            if (hintlen > hintmaxlen) hintlen = hintmaxlen;\n            if (bold == 1 && color == -1) color = 37;\n            if (color != -1 || bold != 0)\n                snprintf(seq,64,\"\\033[%d;%d;49m\",bold,color);\n            else\n                seq[0] = '\\0';\n            abAppend(ab,seq,strlen(seq));\n            abAppend(ab,hint,hintlen);\n            if (color != -1 || bold != 0)\n                abAppend(ab,\"\\033[0m\",4);\n            /* Call the function to free the hint returned. */\n            if (freeHintsCallback) freeHintsCallback(hint);\n        }\n    }\n}\n\n/* Single line low level line refresh.\n *\n * Rewrite the currently edited line accordingly to the buffer content,\n * cursor position, and number of columns of the terminal. */\nstatic void refreshSingleLine(struct linenoiseState *l) {\n    char seq[64];\n    size_t plen = strlen(l->prompt);\n    int fd = l->ofd;\n    char *buf = l->buf;\n    size_t len = l->len;\n    size_t pos = l->pos;\n    struct abuf ab;\n\n    while((plen+pos) >= l->cols) {\n        buf++;\n        len--;\n        pos--;\n    }\n    while (plen+len > l->cols) {\n        len--;\n    }\n\n    abInit(&ab);\n    /* Cursor to left edge */\n    snprintf(seq,64,\"\\r\");\n    abAppend(&ab,seq,strlen(seq));\n    /* Write the prompt and the current buffer content */\n    abAppend(&ab,l->prompt,strlen(l->prompt));\n    if (maskmode == 1) {\n        while (len--) abAppend(&ab,\"*\",1);\n    } else {\n        abAppend(&ab,buf,len);\n    }\n    /* Show hits if any. */\n    refreshShowHints(&ab,l,plen);\n    /* Erase to right */\n    snprintf(seq,64,\"\\x1b[0K\");\n    abAppend(&ab,seq,strlen(seq));\n    /* Move cursor to original position. */\n    snprintf(seq,64,\"\\r\\x1b[%dC\", (int)(pos+plen));\n    abAppend(&ab,seq,strlen(seq));\n    if (write(fd,ab.b,ab.len) == -1) {} /* Can't recover from write error. */\n    abFree(&ab);\n}\n\n/* Multi line low level line refresh.\n *\n * Rewrite the currently edited line accordingly to the buffer content,\n * cursor position, and number of columns of the terminal. */\nstatic void refreshMultiLine(struct linenoiseState *l) {\n    char seq[64];\n    int plen = strlen(l->prompt);\n    int rows = (plen+l->len+l->cols-1)/l->cols; /* rows used by current buf. */\n    int rpos = (plen+l->oldpos+l->cols)/l->cols; /* cursor relative row. */\n    int rpos2; /* rpos after refresh. */\n    int col; /* colum position, zero-based. */\n    int old_rows = l->maxrows;\n    int fd = l->ofd, j;\n    struct abuf ab;\n\n    /* Update maxrows if needed. */\n    if (rows > (int)l->maxrows) l->maxrows = rows;\n\n    /* First step: clear all the lines used before. To do so start by\n     * going to the last row. */\n    abInit(&ab);\n    if (old_rows-rpos > 0) {\n        lndebug(\"go down %d\", old_rows-rpos);\n        snprintf(seq,64,\"\\x1b[%dB\", old_rows-rpos);\n        abAppend(&ab,seq,strlen(seq));\n    }\n\n    /* Now for every row clear it, go up. */\n    for (j = 0; j < old_rows-1; j++) {\n        lndebug(\"clear+up\");\n        snprintf(seq,64,\"\\r\\x1b[0K\\x1b[1A\");\n        abAppend(&ab,seq,strlen(seq));\n    }\n\n    /* Clean the top line. */\n    lndebug(\"clear\");\n    snprintf(seq,64,\"\\r\\x1b[0K\");\n    abAppend(&ab,seq,strlen(seq));\n\n    /* Write the prompt and the current buffer content */\n    abAppend(&ab,l->prompt,strlen(l->prompt));\n    if (maskmode == 1) {\n        unsigned int i;\n        for (i = 0; i < l->len; i++) abAppend(&ab,\"*\",1);\n    } else {\n        abAppend(&ab,l->buf,l->len);\n    }\n\n    /* Show hits if any. */\n    refreshShowHints(&ab,l,plen);\n\n    /* If we are at the very end of the screen with our prompt, we need to\n     * emit a newline and move the prompt to the first column. */\n    if (l->pos &&\n        l->pos == l->len &&\n        (l->pos+plen) % l->cols == 0)\n    {\n        lndebug(\"<newline>\");\n        abAppend(&ab,\"\\n\",1);\n        snprintf(seq,64,\"\\r\");\n        abAppend(&ab,seq,strlen(seq));\n        rows++;\n        if (rows > (int)l->maxrows) l->maxrows = rows;\n    }\n\n    /* Move cursor to right position. */\n    rpos2 = (plen+l->pos+l->cols)/l->cols; /* current cursor relative row. */\n    lndebug(\"rpos2 %d\", rpos2);\n\n    /* Go up till we reach the expected position. */\n    if (rows-rpos2 > 0) {\n        lndebug(\"go-up %d\", rows-rpos2);\n        snprintf(seq,64,\"\\x1b[%dA\", rows-rpos2);\n        abAppend(&ab,seq,strlen(seq));\n    }\n\n    /* Set column. */\n    col = (plen+(int)l->pos) % (int)l->cols;\n    lndebug(\"set col %d\", 1+col);\n    if (col)\n        snprintf(seq,64,\"\\r\\x1b[%dC\", col);\n    else\n        snprintf(seq,64,\"\\r\");\n    abAppend(&ab,seq,strlen(seq));\n\n    lndebug(\"\\n\");\n    l->oldpos = l->pos;\n\n    if (write(fd,ab.b,ab.len) == -1) {} /* Can't recover from write error. */\n    abFree(&ab);\n}\n\n/* Calls the two low level functions refreshSingleLine() or\n * refreshMultiLine() according to the selected mode. */\nstatic void refreshLine(struct linenoiseState *l) {\n    if (mlmode)\n        refreshMultiLine(l);\n    else\n        refreshSingleLine(l);\n}\n\n/* Insert the character 'c' at cursor current position.\n *\n * On error writing to the terminal -1 is returned, otherwise 0. */\nint linenoiseEditInsert(struct linenoiseState *l, char c) {\n    if (l->len < l->buflen) {\n        if (l->len == l->pos) {\n            l->buf[l->pos] = c;\n            l->pos++;\n            l->len++;\n            l->buf[l->len] = '\\0';\n            if ((!mlmode && l->plen+l->len < l->cols && !hintsCallback)) {\n                /* Avoid a full update of the line in the\n                 * trivial case. */\n                char d = (maskmode==1) ? '*' : c;\n                if (write(l->ofd,&d,1) == -1) return -1;\n            } else {\n                refreshLine(l);\n            }\n        } else {\n            memmove(l->buf+l->pos+1,l->buf+l->pos,l->len-l->pos);\n            l->buf[l->pos] = c;\n            l->len++;\n            l->pos++;\n            l->buf[l->len] = '\\0';\n            refreshLine(l);\n        }\n    }\n    return 0;\n}\n\n/* Move cursor on the left. */\nvoid linenoiseEditMoveLeft(struct linenoiseState *l) {\n    if (l->pos > 0) {\n        l->pos--;\n        refreshLine(l);\n    }\n}\n\n/* Move cursor on the right. */\nvoid linenoiseEditMoveRight(struct linenoiseState *l) {\n    if (l->pos != l->len) {\n        l->pos++;\n        refreshLine(l);\n    }\n}\n\n/* Move cursor to the start of the line. */\nvoid linenoiseEditMoveHome(struct linenoiseState *l) {\n    if (l->pos != 0) {\n        l->pos = 0;\n        refreshLine(l);\n    }\n}\n\n/* Move cursor to the end of the line. */\nvoid linenoiseEditMoveEnd(struct linenoiseState *l) {\n    if (l->pos != l->len) {\n        l->pos = l->len;\n        refreshLine(l);\n    }\n}\n\n/* Substitute the currently edited line with the next or previous history\n * entry as specified by 'dir'. */\n#define LINENOISE_HISTORY_NEXT 0\n#define LINENOISE_HISTORY_PREV 1\nvoid linenoiseEditHistoryNext(struct linenoiseState *l, int dir) {\n    if (history_len > 1) {\n        /* Update the current history entry before to\n         * overwrite it with the next one. */\n        free(history[history_len - 1 - l->history_index]);\n        history[history_len - 1 - l->history_index] = strdup(l->buf);\n        /* Show the new entry */\n        l->history_index += (dir == LINENOISE_HISTORY_PREV) ? 1 : -1;\n        if (l->history_index < 0) {\n            l->history_index = 0;\n            return;\n        } else if (l->history_index >= history_len) {\n            l->history_index = history_len-1;\n            return;\n        }\n        strncpy(l->buf,history[history_len - 1 - l->history_index],l->buflen);\n        l->buf[l->buflen-1] = '\\0';\n        l->len = l->pos = strlen(l->buf);\n        refreshLine(l);\n    }\n}\n\n/* Delete the character at the right of the cursor without altering the cursor\n * position. Basically this is what happens with the \"Delete\" keyboard key. */\nvoid linenoiseEditDelete(struct linenoiseState *l) {\n    if (l->len > 0 && l->pos < l->len) {\n        memmove(l->buf+l->pos,l->buf+l->pos+1,l->len-l->pos-1);\n        l->len--;\n        l->buf[l->len] = '\\0';\n        refreshLine(l);\n    }\n}\n\n/* Backspace implementation. */\nvoid linenoiseEditBackspace(struct linenoiseState *l) {\n    if (l->pos > 0 && l->len > 0) {\n        memmove(l->buf+l->pos-1,l->buf+l->pos,l->len-l->pos);\n        l->pos--;\n        l->len--;\n        l->buf[l->len] = '\\0';\n        refreshLine(l);\n    }\n}\n\n/* Delete the previous word, maintaining the cursor at the start of the\n * current word. */\nvoid linenoiseEditDeletePrevWord(struct linenoiseState *l) {\n    size_t old_pos = l->pos;\n    size_t diff;\n\n    while (l->pos > 0 && l->buf[l->pos-1] == ' ')\n        l->pos--;\n    while (l->pos > 0 && l->buf[l->pos-1] != ' ')\n        l->pos--;\n    diff = old_pos - l->pos;\n    memmove(l->buf+l->pos,l->buf+old_pos,l->len-old_pos+1);\n    l->len -= diff;\n    refreshLine(l);\n}\n\n/* This function is the core of the line editing capability of linenoise.\n * It expects 'fd' to be already in \"raw mode\" so that every key pressed\n * will be returned ASAP to read().\n *\n * The resulting string is put into 'buf' when the user type enter, or\n * when ctrl+d is typed.\n *\n * The function returns the length of the current buffer. */\nstatic int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen, const char *prompt)\n{\n    struct linenoiseState l;\n\n    /* Populate the linenoise state that we pass to functions implementing\n     * specific editing functionalities. */\n    l.ifd = stdin_fd;\n    l.ofd = stdout_fd;\n    l.buf = buf;\n    l.buflen = buflen;\n    l.prompt = prompt;\n    l.plen = strlen(prompt);\n    l.oldpos = l.pos = 0;\n    l.len = 0;\n    l.cols = getColumns(stdin_fd, stdout_fd);\n    l.maxrows = 0;\n    l.history_index = 0;\n\n    /* Buffer starts empty. */\n    l.buf[0] = '\\0';\n    l.buflen--; /* Make sure there is always space for the nulterm */\n\n    /* The latest history entry is always our current buffer, that\n     * initially is just an empty string. */\n    linenoiseHistoryAdd(\"\");\n\n    if (write(l.ofd,prompt,l.plen) == -1) return -1;\n    while(1) {\n        char c;\n        int nread;\n        char seq[3];\n\n        nread = read(l.ifd,&c,1);\n        if (nread <= 0) return l.len;\n\n        /* Only autocomplete when the callback is set. It returns < 0 when\n         * there was an error reading from fd. Otherwise it will return the\n         * character that should be handled next. */\n        if (c == 9 && completionCallback != NULL) {\n            c = completeLine(&l);\n            /* Return on errors */\n            if (c < 0) return l.len;\n            /* Read next character when 0 */\n            if (c == 0) continue;\n        }\n\n        switch(c) {\n        case ENTER:    /* enter */\n            history_len--;\n            free(history[history_len]);\n            if (mlmode) linenoiseEditMoveEnd(&l);\n            if (hintsCallback) {\n                /* Force a refresh without hints to leave the previous\n                 * line as the user typed it after a newline. */\n                linenoiseHintsCallback *hc = hintsCallback;\n                hintsCallback = NULL;\n                refreshLine(&l);\n                hintsCallback = hc;\n            }\n            return (int)l.len;\n        case CTRL_C:     /* ctrl-c */\n            errno = EAGAIN;\n            return -1;\n        case BACKSPACE:   /* backspace */\n        case 8:     /* ctrl-h */\n            linenoiseEditBackspace(&l);\n            break;\n        case CTRL_D:     /* ctrl-d, remove char at right of cursor, or if the\n                            line is empty, act as end-of-file. */\n            if (l.len > 0) {\n                linenoiseEditDelete(&l);\n            } else {\n                history_len--;\n                free(history[history_len]);\n                return -1;\n            }\n            break;\n        case CTRL_T:    /* ctrl-t, swaps current character with previous. */\n            if (l.pos > 0 && l.pos < l.len) {\n                int aux = buf[l.pos-1];\n                buf[l.pos-1] = buf[l.pos];\n                buf[l.pos] = aux;\n                if (l.pos != l.len-1) l.pos++;\n                refreshLine(&l);\n            }\n            break;\n        case CTRL_B:     /* ctrl-b */\n            linenoiseEditMoveLeft(&l);\n            break;\n        case CTRL_F:     /* ctrl-f */\n            linenoiseEditMoveRight(&l);\n            break;\n        case CTRL_P:    /* ctrl-p */\n            linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_PREV);\n            break;\n        case CTRL_N:    /* ctrl-n */\n            linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_NEXT);\n            break;\n        case ESC:    /* escape sequence */\n            /* Read the next two bytes representing the escape sequence.\n             * Use two calls to handle slow terminals returning the two\n             * chars at different times. */\n            if (read(l.ifd,seq,1) == -1) break;\n            if (read(l.ifd,seq+1,1) == -1) break;\n\n            /* ESC [ sequences. */\n            if (seq[0] == '[') {\n                if (seq[1] >= '0' && seq[1] <= '9') {\n                    /* Extended escape, read additional byte. */\n                    if (read(l.ifd,seq+2,1) == -1) break;\n                    if (seq[2] == '~') {\n                        switch(seq[1]) {\n                        case '3': /* Delete key. */\n                            linenoiseEditDelete(&l);\n                            break;\n                        }\n                    }\n                } else {\n                    switch(seq[1]) {\n                    case 'A': /* Up */\n                        linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_PREV);\n                        break;\n                    case 'B': /* Down */\n                        linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_NEXT);\n                        break;\n                    case 'C': /* Right */\n                        linenoiseEditMoveRight(&l);\n                        break;\n                    case 'D': /* Left */\n                        linenoiseEditMoveLeft(&l);\n                        break;\n                    case 'H': /* Home */\n                        linenoiseEditMoveHome(&l);\n                        break;\n                    case 'F': /* End*/\n                        linenoiseEditMoveEnd(&l);\n                        break;\n                    }\n                }\n            }\n\n            /* ESC O sequences. */\n            else if (seq[0] == 'O') {\n                switch(seq[1]) {\n                case 'H': /* Home */\n                    linenoiseEditMoveHome(&l);\n                    break;\n                case 'F': /* End*/\n                    linenoiseEditMoveEnd(&l);\n                    break;\n                }\n            }\n            break;\n        default:\n            if (linenoiseEditInsert(&l,c)) return -1;\n            break;\n        case CTRL_U: /* Ctrl+u, delete the whole line. */\n            buf[0] = '\\0';\n            l.pos = l.len = 0;\n            refreshLine(&l);\n            break;\n        case CTRL_K: /* Ctrl+k, delete from current to end of line. */\n            buf[l.pos] = '\\0';\n            l.len = l.pos;\n            refreshLine(&l);\n            break;\n        case CTRL_A: /* Ctrl+a, go to the start of the line */\n            linenoiseEditMoveHome(&l);\n            break;\n        case CTRL_E: /* ctrl+e, go to the end of the line */\n            linenoiseEditMoveEnd(&l);\n            break;\n        case CTRL_L: /* ctrl+l, clear screen */\n            linenoiseClearScreen();\n            refreshLine(&l);\n            break;\n        case CTRL_W: /* ctrl+w, delete previous word */\n            linenoiseEditDeletePrevWord(&l);\n            break;\n        }\n    }\n    return l.len;\n}\n\n/* This special mode is used by linenoise in order to print scan codes\n * on screen for debugging / development purposes. It is implemented\n * by the linenoise_example program using the --keycodes option. */\nvoid linenoisePrintKeyCodes(void) {\n    char quit[4];\n\n    printf(\"Linenoise key codes debugging mode.\\n\"\n            \"Press keys to see scan codes. Type 'quit' at any time to exit.\\n\");\n    if (enableRawMode(STDIN_FILENO) == -1) return;\n    memset(quit,' ',4);\n    while(1) {\n        char c;\n        int nread;\n\n        nread = read(STDIN_FILENO,&c,1);\n        if (nread <= 0) continue;\n        memmove(quit,quit+1,sizeof(quit)-1); /* shift string to left. */\n        quit[sizeof(quit)-1] = c; /* Insert current char on the right. */\n        if (memcmp(quit,\"quit\",sizeof(quit)) == 0) break;\n\n        printf(\"'%c' %02x (%d) (type quit to exit)\\n\",\n            isprint(c) ? c : '?', (int)c, (int)c);\n        printf(\"\\r\"); /* Go left edge manually, we are in raw mode. */\n        fflush(stdout);\n    }\n    disableRawMode(STDIN_FILENO);\n}\n\n/* This function calls the line editing function linenoiseEdit() using\n * the STDIN file descriptor set in raw mode. */\nstatic int linenoiseRaw(char *buf, size_t buflen, const char *prompt) {\n    int count;\n\n    if (buflen == 0) {\n        errno = EINVAL;\n        return -1;\n    }\n\n    if (enableRawMode(STDIN_FILENO) == -1) return -1;\n    count = linenoiseEdit(STDIN_FILENO, STDOUT_FILENO, buf, buflen, prompt);\n    disableRawMode(STDIN_FILENO);\n    printf(\"\\n\");\n    return count;\n}\n\n/* This function is called when linenoise() is called with the standard\n * input file descriptor not attached to a TTY. So for example when the\n * program using linenoise is called in pipe or with a file redirected\n * to its standard input. In this case, we want to be able to return the\n * line regardless of its length (by default we are limited to 4k). */\nstatic char *linenoiseNoTTY(void) {\n    char *line = NULL;\n    size_t len = 0, maxlen = 0;\n\n    while(1) {\n        if (len == maxlen) {\n            if (maxlen == 0) maxlen = 16;\n            maxlen *= 2;\n            char *oldval = line;\n            line = realloc(line,maxlen);\n            if (line == NULL) {\n                if (oldval) free(oldval);\n                return NULL;\n            }\n        }\n        int c = fgetc(stdin);\n        if (c == EOF || c == '\\n') {\n            if (c == EOF && len == 0) {\n                free(line);\n                return NULL;\n            } else {\n                line[len] = '\\0';\n                return line;\n            }\n        } else {\n            line[len] = c;\n            len++;\n        }\n    }\n}\n\n/* The high level function that is the main API of the linenoise library.\n * This function checks if the terminal has basic capabilities, just checking\n * for a blacklist of stupid terminals, and later either calls the line\n * editing function or uses dummy fgets() so that you will be able to type\n * something even in the most desperate of the conditions. */\nchar *linenoise(const char *prompt) {\n    char buf[LINENOISE_MAX_LINE];\n    int count;\n\n    if (!isatty(STDIN_FILENO)) {\n        /* Not a tty: read from file / pipe. In this mode we don't want any\n         * limit to the line size, so we call a function to handle that. */\n        return linenoiseNoTTY();\n    } else if (isUnsupportedTerm()) {\n        size_t len;\n\n        printf(\"%s\",prompt);\n        fflush(stdout);\n        if (fgets(buf,LINENOISE_MAX_LINE,stdin) == NULL) return NULL;\n        len = strlen(buf);\n        while(len && (buf[len-1] == '\\n' || buf[len-1] == '\\r')) {\n            len--;\n            buf[len] = '\\0';\n        }\n        return strdup(buf);\n    } else {\n        count = linenoiseRaw(buf,LINENOISE_MAX_LINE,prompt);\n        if (count == -1) return NULL;\n        return strdup(buf);\n    }\n}\n\n/* This is just a wrapper the user may want to call in order to make sure\n * the linenoise returned buffer is freed with the same allocator it was\n * created with. Useful when the main program is using an alternative\n * allocator. */\nvoid linenoiseFree(void *ptr) {\n    free(ptr);\n}\n\n/* ================================ History ================================= */\n\n/* Free the history, but does not reset it. Only used when we have to\n * exit() to avoid memory leaks are reported by valgrind & co. */\nstatic void freeHistory(void) {\n    if (history) {\n        int j;\n\n        for (j = 0; j < history_len; j++)\n            free(history[j]);\n        free(history);\n    }\n}\n\n/* At exit we'll try to fix the terminal to the initial conditions. */\nstatic void linenoiseAtExit(void) {\n    disableRawMode(STDIN_FILENO);\n    freeHistory();\n}\n\n/* This is the API call to add a new entry in the linenoise history.\n * It uses a fixed array of char pointers that are shifted (memmoved)\n * when the history max length is reached in order to remove the older\n * entry and make room for the new one, so it is not exactly suitable for huge\n * histories, but will work well for a few hundred of entries.\n *\n * Using a circular buffer is smarter, but a bit more complex to handle. */\nint linenoiseHistoryAdd(const char *line) {\n    char *linecopy;\n\n    if (history_max_len == 0) return 0;\n\n    /* Initialization on first call. */\n    if (history == NULL) {\n        history = malloc(sizeof(char*)*history_max_len);\n        if (history == NULL) return 0;\n        memset(history,0,(sizeof(char*)*history_max_len));\n    }\n\n    /* Don't add duplicated lines. */\n    if (history_len && !strcmp(history[history_len-1], line)) return 0;\n\n    /* Add an heap allocated copy of the line in the history.\n     * If we reached the max length, remove the older line. */\n    linecopy = strdup(line);\n    if (!linecopy) return 0;\n    if (history_len == history_max_len) {\n        free(history[0]);\n        memmove(history,history+1,sizeof(char*)*(history_max_len-1));\n        history_len--;\n    }\n    history[history_len] = linecopy;\n    history_len++;\n    return 1;\n}\n\n/* Set the maximum length for the history. This function can be called even\n * if there is already some history, the function will make sure to retain\n * just the latest 'len' elements if the new history length value is smaller\n * than the amount of items already inside the history. */\nint linenoiseHistorySetMaxLen(int len) {\n    char **new;\n\n    if (len < 1) return 0;\n    if (history) {\n        int tocopy = history_len;\n\n        new = malloc(sizeof(char*)*len);\n        if (new == NULL) return 0;\n\n        /* If we can't copy everything, free the elements we'll not use. */\n        if (len < tocopy) {\n            int j;\n\n            for (j = 0; j < tocopy-len; j++) free(history[j]);\n            tocopy = len;\n        }\n        memset(new,0,sizeof(char*)*len);\n        memcpy(new,history+(history_len-tocopy), sizeof(char*)*tocopy);\n        free(history);\n        history = new;\n    }\n    history_max_len = len;\n    if (history_len > history_max_len)\n        history_len = history_max_len;\n    return 1;\n}\n\n/* Save the history in the specified file. On success 0 is returned\n * otherwise -1 is returned. */\nint linenoiseHistorySave(const char *filename) {\n    mode_t old_umask = umask(S_IXUSR|S_IRWXG|S_IRWXO);\n    FILE *fp;\n    int j;\n\n    fp = fopen(filename,\"w\");\n    umask(old_umask);\n    if (fp == NULL) return -1;\n    chmod(filename,S_IRUSR|S_IWUSR);\n    for (j = 0; j < history_len; j++)\n        fprintf(fp,\"%s\\n\",history[j]);\n    fclose(fp);\n    return 0;\n}\n\n/* Load the history from the specified file. If the file does not exist\n * zero is returned and no operation is performed.\n *\n * If the file exists and the operation succeeded 0 is returned, otherwise\n * on error -1 is returned. */\nint linenoiseHistoryLoad(const char *filename) {\n    FILE *fp = fopen(filename,\"r\");\n    char buf[LINENOISE_MAX_LINE];\n\n    if (fp == NULL) return -1;\n\n    while (fgets(buf,LINENOISE_MAX_LINE,fp) != NULL) {\n        char *p;\n\n        p = strchr(buf,'\\r');\n        if (!p) p = strchr(buf,'\\n');\n        if (p) *p = '\\0';\n        linenoiseHistoryAdd(buf);\n    }\n    fclose(fp);\n    return 0;\n}\n"
  },
  {
    "path": "deps/linenoise/linenoise.h",
    "content": "/* linenoise.h -- VERSION 1.0\n *\n * Guerrilla line editing library against the idea that a line editing lib\n * needs to be 20,000 lines of C code.\n *\n * See linenoise.c for more information.\n *\n * ------------------------------------------------------------------------\n *\n * Copyright (c) 2010-2014, Salvatore Sanfilippo <antirez at gmail dot com>\n * Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>\n *\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are\n * met:\n *\n *  *  Redistributions of source code must retain the above copyright\n *     notice, this list of conditions and the following disclaimer.\n *\n *  *  Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n * \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __LINENOISE_H\n#define __LINENOISE_H\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\ntypedef struct linenoiseCompletions {\n  size_t len;\n  char **cvec;\n} linenoiseCompletions;\n\ntypedef void(linenoiseCompletionCallback)(const char *, linenoiseCompletions *);\ntypedef char*(linenoiseHintsCallback)(const char *, int *color, int *bold);\ntypedef void(linenoiseFreeHintsCallback)(void *);\nvoid linenoiseSetCompletionCallback(linenoiseCompletionCallback *);\nvoid linenoiseSetHintsCallback(linenoiseHintsCallback *);\nvoid linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *);\nvoid linenoiseAddCompletion(linenoiseCompletions *, const char *);\n\nchar *linenoise(const char *prompt);\nvoid linenoiseFree(void *ptr);\nint linenoiseHistoryAdd(const char *line);\nint linenoiseHistorySetMaxLen(int len);\nint linenoiseHistorySave(const char *filename);\nint linenoiseHistoryLoad(const char *filename);\nvoid linenoiseClearScreen(void);\nvoid linenoiseSetMultiLine(int ml);\nvoid linenoisePrintKeyCodes(void);\nvoid linenoiseMaskModeEnable(void);\nvoid linenoiseMaskModeDisable(void);\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif /* __LINENOISE_H */\n"
  },
  {
    "path": "deps/lua/COPYRIGHT",
    "content": "Lua License\n-----------\n\nLua is licensed under the terms of the MIT license reproduced below.\nThis means that Lua is free software and can be used for both academic\nand commercial purposes at absolutely no cost.\n\nFor details and rationale, see http://www.lua.org/license.html .\n\n===============================================================================\n\nCopyright (C) 1994-2012 Lua.org, PUC-Rio.\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n\n===============================================================================\n\n(end of COPYRIGHT)\n"
  },
  {
    "path": "deps/lua/HISTORY",
    "content": "HISTORY for Lua 5.1\n\n* Changes from version 5.0 to 5.1\n  -------------------------------\n  Language:\n  + new module system.\n  + new semantics for control variables of fors.\n  + new semantics for setn/getn.\n  + new syntax/semantics for varargs.\n  + new long strings and comments.\n  + new `mod' operator (`%')\n  + new length operator #t\n  + metatables for all types\n  API:\n  + new functions: lua_createtable, lua_get(set)field, lua_push(to)integer.\n  + user supplies memory allocator (lua_open becomes lua_newstate).\n  + luaopen_* functions must be called through Lua.\n  Implementation:\n  + new configuration scheme via luaconf.h.\n  + incremental garbage collection.\n  + better handling of end-of-line in the lexer.\n  + fully reentrant parser (new Lua function `load')\n  + better support for 64-bit machines.\n  + native loadlib support for Mac OS X.\n  + standard distribution in only one library (lualib.a merged into lua.a)\n\n* Changes from version 4.0 to 5.0\n  -------------------------------\n  Language:\n  + lexical scoping.\n  + Lua coroutines.\n  + standard libraries now packaged in tables.\n  + tags replaced by metatables and tag methods replaced by metamethods,\n    stored in metatables.\n  + proper tail calls.\n  + each function can have its own global table, which can be shared.\n  + new __newindex metamethod, called when we insert a new key into a table.\n  + new block comments: --[[ ... ]].\n  + new generic for.\n  + new weak tables.\n  + new boolean type.\n  + new syntax \"local function\".\n  + (f()) returns the first value returned by f.\n  + {f()} fills a table with all values returned by f.\n  + \\n ignored in [[\\n .\n  + fixed and-or priorities.\n  + more general syntax for function definition (e.g. function a.x.y:f()...end).\n  + more general syntax for function calls (e.g. (print or write)(9)).\n  + new functions (time/date, tmpfile, unpack, require, load*, etc.).\n  API:\n  + chunks are loaded by using lua_load; new luaL_loadfile and luaL_loadbuffer.\n  + introduced lightweight userdata, a simple \"void*\" without a metatable.\n  + new error handling protocol: the core no longer prints error messages;\n    all errors are reported to the caller on the stack.\n  + new lua_atpanic for host cleanup.\n  + new, signal-safe, hook scheme.\n  Implementation:\n  + new license: MIT.\n  + new, faster, register-based virtual machine.\n  + support for external multithreading and coroutines.\n  + new and consistent error message format.\n  + the core no longer needs \"stdio.h\" for anything (except for a single\n    use of sprintf to convert numbers to strings).\n  + lua.c now runs the environment variable LUA_INIT, if present. It can\n    be \"@filename\", to run a file, or the chunk itself.\n  + support for user extensions in lua.c.\n    sample implementation given for command line editing.\n  + new dynamic loading library, active by default on several platforms.\n  + safe garbage-collector metamethods.\n  + precompiled bytecodes checked for integrity (secure binary dostring).\n  + strings are fully aligned.\n  + position capture in string.find.\n  + read('*l') can read lines with embedded zeros.\n\n* Changes from version 3.2 to 4.0\n  -------------------------------\n  Language:\n  + new \"break\" and \"for\" statements (both numerical and for tables).\n  + uniform treatment of globals: globals are now stored in a Lua table.\n  + improved error messages.\n  + no more '$debug': full speed *and* full debug information.\n  + new read form: read(N) for next N bytes.\n  + general read patterns now deprecated.\n    (still available with -DCOMPAT_READPATTERNS.)\n  + all return values are passed as arguments for the last function\n    (old semantics still available with -DLUA_COMPAT_ARGRET)\n  + garbage collection tag methods for tables now deprecated.\n  + there is now only one tag method for order.\n  API:\n  + New API: fully re-entrant, simpler, and more efficient.\n  + New debug API.\n  Implementation:\n  + faster than ever: cleaner virtual machine and new hashing algorithm.\n  + non-recursive garbage-collector algorithm.\n  + reduced memory usage for programs with many strings.\n  + improved treatment for memory allocation errors.\n  + improved support for 16-bit machines (we hope).\n  + code now compiles unmodified as both ANSI C and C++.\n  + numbers in bases other than 10 are converted using strtoul.\n  + new -f option in Lua to support #! scripts.\n  + luac can now combine text and binaries.\n\n* Changes from version 3.1 to 3.2\n  -------------------------------\n  + redirected all output in Lua's core to _ERRORMESSAGE and _ALERT.\n  + increased limit on the number of constants and globals per function\n    (from 2^16 to 2^24).\n  + debugging info (lua_debug and hooks) moved into lua_state and new API\n    functions provided to get and set this info.\n  + new debug lib gives full debugging access within Lua.\n  + new table functions \"foreachi\", \"sort\", \"tinsert\", \"tremove\", \"getn\".\n  + new io functions \"flush\", \"seek\".\n\n* Changes from version 3.0 to 3.1\n  -------------------------------\n  + NEW FEATURE: anonymous functions with closures (via \"upvalues\").\n  + new syntax:\n    - local variables in chunks.\n    - better scope control with DO block END.\n    - constructors can now be also written: { record-part; list-part }.\n    - more general syntax for function calls and lvalues, e.g.:\n      f(x).y=1\n      o:f(x,y):g(z)\n      f\"string\" is sugar for f(\"string\")\n  + strings may now contain arbitrary binary data (e.g., embedded zeros).\n  + major code re-organization and clean-up; reduced module interdependecies.\n  + no arbitrary limits on the total number of constants and globals.\n  + support for multiple global contexts.\n  + better syntax error messages.\n  + new traversal functions \"foreach\" and \"foreachvar\".\n  + the default for numbers is now double.\n    changing it to use floats or longs is easy.\n  + complete debug information stored in pre-compiled chunks.\n  + sample interpreter now prompts user when run interactively, and also\n    handles control-C interruptions gracefully.\n\n* Changes from version 2.5 to 3.0\n  -------------------------------\n  + NEW CONCEPT: \"tag methods\".\n    Tag methods replace fallbacks as the meta-mechanism for extending the\n    semantics of Lua. Whereas fallbacks had a global nature, tag methods\n    work on objects having the same tag (e.g., groups of tables).\n    Existing code that uses fallbacks should work without change.\n  + new, general syntax for constructors {[exp] = exp, ... }.\n  + support for handling variable number of arguments in functions (varargs).\n  + support for conditional compilation ($if ... $else ... $end).\n  + cleaner semantics in API simplifies host code.\n  + better support for writing libraries (auxlib.h).\n  + better type checking and error messages in the standard library.\n  + luac can now also undump.\n\n* Changes from version 2.4 to 2.5\n  -------------------------------\n  + io and string libraries are now based on pattern matching;\n    the old libraries are still available for compatibility\n  + dofile and dostring can now return values (via return statement)\n  + better support for 16- and 64-bit machines\n  + expanded documentation, with more examples\n\n* Changes from version 2.2 to 2.4\n  -------------------------------\n  + external compiler creates portable binary files that can be loaded faster\n  + interface for debugging and profiling\n  + new \"getglobal\" fallback\n  + new functions for handling references to Lua objects\n  + new functions in standard lib\n  + only one copy of each string is stored\n  + expanded documentation, with more examples\n\n* Changes from version 2.1 to 2.2\n  -------------------------------\n  + functions now may be declared with any \"lvalue\" as a name\n  + garbage collection of functions\n  + support for pipes\n\n* Changes from version 1.1 to 2.1\n  -------------------------------\n  + object-oriented support\n  + fallbacks\n  + simplified syntax for tables\n  + many internal improvements\n\n(end of HISTORY)\n"
  },
  {
    "path": "deps/lua/INSTALL",
    "content": "INSTALL for Lua 5.1\n\n* Building Lua\n  ------------\n  Lua is built in the src directory, but the build process can be\n  controlled from the top-level Makefile.\n\n  Building Lua on Unix systems should be very easy. First do \"make\" and\n  see if your platform is listed. If so, just do \"make xxx\", where xxx\n  is your platform name. The platforms currently supported are:\n    aix ansi bsd freebsd generic linux macosx mingw posix solaris\n\n  If your platform is not listed, try the closest one or posix, generic,\n  ansi, in this order.\n\n  See below for customization instructions and for instructions on how\n  to build with other Windows compilers.\n\n  If you want to check that Lua has been built correctly, do \"make test\"\n  after building Lua. Also, have a look at the example programs in test.\n\n* Installing Lua\n  --------------\n  Once you have built Lua, you may want to install it in an official\n  place in your system. In this case, do \"make install\". The official\n  place and the way to install files are defined in Makefile. You must\n  have the right permissions to install files.\n\n  If you want to build and install Lua in one step, do \"make xxx install\",\n  where xxx is your platform name.\n\n  If you want to install Lua locally, then do \"make local\". This will\n  create directories bin, include, lib, man, and install Lua there as\n  follows:\n\n    bin:\tlua luac\n    include:\tlua.h luaconf.h lualib.h lauxlib.h lua.hpp\n    lib:\tliblua.a\n    man/man1:\tlua.1 luac.1\n\n  These are the only directories you need for development.\n\n  There are man pages for lua and luac, in both nroff and html, and a\n  reference manual in html in doc, some sample code in test, and some\n  useful stuff in etc. You don't need these directories for development.\n\n  If you want to install Lua locally, but in some other directory, do\n  \"make install INSTALL_TOP=xxx\", where xxx is your chosen directory.\n\n  See below for instructions for Windows and other systems.\n\n* Customization\n  -------------\n  Three things can be customized by editing a file:\n    - Where and how to install Lua -- edit Makefile.\n    - How to build Lua -- edit src/Makefile.\n    - Lua features -- edit src/luaconf.h.\n\n  You don't actually need to edit the Makefiles because you may set the\n  relevant variables when invoking make.\n\n  On the other hand, if you need to select some Lua features, you'll need\n  to edit src/luaconf.h. The edited file will be the one installed, and\n  it will be used by any Lua clients that you build, to ensure consistency.\n\n  We strongly recommend that you enable dynamic loading. This is done\n  automatically for all platforms listed above that have this feature\n  (and also Windows). See src/luaconf.h and also src/Makefile.\n\n* Building Lua on Windows and other systems\n  -----------------------------------------\n  If you're not using the usual Unix tools, then the instructions for\n  building Lua depend on the compiler you use. You'll need to create\n  projects (or whatever your compiler uses) for building the library,\n  the interpreter, and the compiler, as follows:\n\n  library:\tlapi.c lcode.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c\n\t\tlmem.c lobject.c lopcodes.c lparser.c lstate.c lstring.c\n\t\tltable.c ltm.c lundump.c lvm.c lzio.c\n\t\tlauxlib.c lbaselib.c ldblib.c liolib.c lmathlib.c loslib.c\n\t\tltablib.c lstrlib.c loadlib.c linit.c\n\n  interpreter:\tlibrary, lua.c\n\n  compiler:\tlibrary, luac.c print.c\n\n  If you use Visual Studio .NET, you can use etc/luavs.bat in its\n  \"Command Prompt\".\n\n  If all you want is to build the Lua interpreter, you may put all .c files\n  in a single project, except for luac.c and print.c. Or just use etc/all.c.\n\n  To use Lua as a library in your own programs, you'll need to know how to\n  create and use libraries with your compiler.\n\n  As mentioned above, you may edit luaconf.h to select some features before\n  building Lua.\n\n(end of INSTALL)\n"
  },
  {
    "path": "deps/lua/Makefile",
    "content": "# makefile for installing Lua\n# see INSTALL for installation instructions\n# see src/Makefile and src/luaconf.h for further customization\n\n# == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT =======================\n\n# Your platform. See PLATS for possible values.\nPLAT= none\n\n# Where to install. The installation starts in the src and doc directories,\n# so take care if INSTALL_TOP is not an absolute path.\nINSTALL_TOP= /usr/local\nINSTALL_BIN= $(INSTALL_TOP)/bin\nINSTALL_INC= $(INSTALL_TOP)/include\nINSTALL_LIB= $(INSTALL_TOP)/lib\nINSTALL_MAN= $(INSTALL_TOP)/man/man1\n#\n# You probably want to make INSTALL_LMOD and INSTALL_CMOD consistent with\n# LUA_ROOT, LUA_LDIR, and LUA_CDIR in luaconf.h (and also with etc/lua.pc).\nINSTALL_LMOD= $(INSTALL_TOP)/share/lua/$V\nINSTALL_CMOD= $(INSTALL_TOP)/lib/lua/$V\n\n# How to install. If your install program does not support \"-p\", then you\n# may have to run ranlib on the installed liblua.a (do \"make ranlib\").\nINSTALL= install -p\nINSTALL_EXEC= $(INSTALL) -m 0755\nINSTALL_DATA= $(INSTALL) -m 0644\n#\n# If you don't have install you can use cp instead.\n# INSTALL= cp -p\n# INSTALL_EXEC= $(INSTALL)\n# INSTALL_DATA= $(INSTALL)\n\n# Utilities.\nMKDIR= mkdir -p\nRANLIB= ranlib\n\n# == END OF USER SETTINGS. NO NEED TO CHANGE ANYTHING BELOW THIS LINE =========\n\n# Convenience platforms targets.\nPLATS= aix ansi bsd freebsd generic linux macosx mingw posix solaris\n\n# What to install.\nTO_BIN= lua luac\nTO_INC= lua.h luaconf.h lualib.h lauxlib.h ../etc/lua.hpp\nTO_LIB= liblua.a\nTO_MAN= lua.1 luac.1\n\n# Lua version and release.\nV= 5.1\nR= 5.1.5\n\nall:\t$(PLAT)\n\n$(PLATS) clean:\n\tcd src && $(MAKE) $@\n\ntest:\tdummy\n\tsrc/lua test/hello.lua\n\ninstall: dummy\n\tcd src && $(MKDIR) $(INSTALL_BIN) $(INSTALL_INC) $(INSTALL_LIB) $(INSTALL_MAN) $(INSTALL_LMOD) $(INSTALL_CMOD)\n\tcd src && $(INSTALL_EXEC) $(TO_BIN) $(INSTALL_BIN)\n\tcd src && $(INSTALL_DATA) $(TO_INC) $(INSTALL_INC)\n\tcd src && $(INSTALL_DATA) $(TO_LIB) $(INSTALL_LIB)\n\tcd doc && $(INSTALL_DATA) $(TO_MAN) $(INSTALL_MAN)\n\nranlib:\n\tcd src && cd $(INSTALL_LIB) && $(RANLIB) $(TO_LIB)\n\nlocal:\n\t$(MAKE) install INSTALL_TOP=..\n\nnone:\n\t@echo \"Please do\"\n\t@echo \"   make PLATFORM\"\n\t@echo \"where PLATFORM is one of these:\"\n\t@echo \"   $(PLATS)\"\n\t@echo \"See INSTALL for complete instructions.\"\n\n# make may get confused with test/ and INSTALL in a case-insensitive OS\ndummy:\n\n# echo config parameters\necho:\n\t@echo \"\"\n\t@echo \"These are the parameters currently set in src/Makefile to build Lua $R:\"\n\t@echo \"\"\n\t@cd src && $(MAKE) -s echo\n\t@echo \"\"\n\t@echo \"These are the parameters currently set in Makefile to install Lua $R:\"\n\t@echo \"\"\n\t@echo \"PLAT = $(PLAT)\"\n\t@echo \"INSTALL_TOP = $(INSTALL_TOP)\"\n\t@echo \"INSTALL_BIN = $(INSTALL_BIN)\"\n\t@echo \"INSTALL_INC = $(INSTALL_INC)\"\n\t@echo \"INSTALL_LIB = $(INSTALL_LIB)\"\n\t@echo \"INSTALL_MAN = $(INSTALL_MAN)\"\n\t@echo \"INSTALL_LMOD = $(INSTALL_LMOD)\"\n\t@echo \"INSTALL_CMOD = $(INSTALL_CMOD)\"\n\t@echo \"INSTALL_EXEC = $(INSTALL_EXEC)\"\n\t@echo \"INSTALL_DATA = $(INSTALL_DATA)\"\n\t@echo \"\"\n\t@echo \"See also src/luaconf.h .\"\n\t@echo \"\"\n\n# echo private config parameters\npecho:\n\t@echo \"V = $(V)\"\n\t@echo \"R = $(R)\"\n\t@echo \"TO_BIN = $(TO_BIN)\"\n\t@echo \"TO_INC = $(TO_INC)\"\n\t@echo \"TO_LIB = $(TO_LIB)\"\n\t@echo \"TO_MAN = $(TO_MAN)\"\n\n# echo config parameters as Lua code\n# uncomment the last sed expression if you want nil instead of empty strings\nlecho:\n\t@echo \"-- installation parameters for Lua $R\"\n\t@echo \"VERSION = '$V'\"\n\t@echo \"RELEASE = '$R'\"\n\t@$(MAKE) echo | grep = | sed -e 's/= /= \"/' -e 's/$$/\"/' #-e 's/\"\"/nil/'\n\t@echo \"-- EOF\"\n\n# list targets that do not create files (but not all makes understand .PHONY)\n.PHONY: all $(PLATS) clean test install local none dummy echo pecho lecho\n\n# (end of Makefile)\n"
  },
  {
    "path": "deps/lua/README",
    "content": "README for Lua 5.1\n\nSee INSTALL for installation instructions.\nSee HISTORY for a summary of changes since the last released version.\n\n* What is Lua?\n  ------------\n  Lua is a powerful, light-weight programming language designed for extending\n  applications. Lua is also frequently used as a general-purpose, stand-alone\n  language. Lua is free software.\n\n  For complete information, visit Lua's web site at http://www.lua.org/ .\n  For an executive summary, see http://www.lua.org/about.html .\n\n  Lua has been used in many different projects around the world.\n  For a short list, see http://www.lua.org/uses.html .\n\n* Availability\n  ------------\n  Lua is freely available for both academic and commercial purposes.\n  See COPYRIGHT and http://www.lua.org/license.html for details.\n  Lua can be downloaded at http://www.lua.org/download.html .\n\n* Installation\n  ------------\n  Lua is implemented in pure ANSI C, and compiles unmodified in all known\n  platforms that have an ANSI C compiler. In most Unix-like platforms, simply\n  do \"make\" with a suitable target. See INSTALL for detailed instructions.\n\n* Origin\n  ------\n  Lua is developed at Lua.org, a laboratory of the Department of Computer\n  Science of PUC-Rio (the Pontifical Catholic University of Rio de Janeiro\n  in Brazil).\n  For more information about the authors, see http://www.lua.org/authors.html .\n\n(end of README)\n"
  },
  {
    "path": "deps/lua/doc/contents.html",
    "content": "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n<HTML>\n<HEAD>\n<TITLE>Lua 5.1 Reference Manual - contents</TITLE>\n<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"lua.css\">\n<META HTTP-EQUIV=\"content-type\" CONTENT=\"text/html; charset=utf-8\">\n<STYLE TYPE=\"text/css\">\nul {\n\tlist-style-type: none ;\n}\n</STYLE>\n</HEAD>\n\n<BODY>\n\n<HR>\n<H1>\n<A HREF=\"http://www.lua.org/\"><IMG SRC=\"logo.gif\" ALT=\"\" BORDER=0></A>\nLua 5.1 Reference Manual\n</H1>\n\n<P>\nThe reference manual is the official definition of the Lua language.\nFor a complete introduction to Lua programming, see the book\n<A HREF=\"http://www.lua.org/docs.html#pil\">Programming in Lua</A>.\n\n<P>\nThis manual is also available as a book:\n<BLOCKQUOTE>\n<A HREF=\"http://www.amazon.com/exec/obidos/ASIN/8590379833/lua-indexmanual-20\">\n<IMG SRC=\"cover.png\" ALT=\"\" TITLE=\"buy from Amazon\" BORDER=1 ALIGN=\"left\" HSPACE=12>\n</A>\n<B>Lua 5.1 Reference Manual</B>\n<BR>by R. Ierusalimschy, L. H. de Figueiredo, W. Celes\n<BR>Lua.org, August 2006\n<BR>ISBN 85-903798-3-3\n<BR CLEAR=\"all\">\n</BLOCKQUOTE>\n\n<P>\n<A HREF=\"http://www.amazon.com/exec/obidos/ASIN/8590379833/lua-indexmanual-20\">Buy a copy</A>\nof this book and\n<A HREF=\"http://www.lua.org/donations.html\">help to support</A>\nthe Lua project.\n\n<P>\n<A HREF=\"manual.html\">start</A>\n&middot;\n<A HREF=\"#contents\">contents</A>\n&middot;\n<A HREF=\"#index\">index</A>\n&middot;\n<A HREF=\"http://www.lua.org/manual/\">other versions</A>\n<HR>\n<SMALL>\nCopyright &copy; 2006&ndash;2012 Lua.org, PUC-Rio.\nFreely available under the terms of the\n<A HREF=\"http://www.lua.org/license.html\">Lua license</A>.\n</SMALL>\n\n<H2><A NAME=\"contents\">Contents</A></H2>\n<UL style=\"padding: 0\">\n<LI><A HREF=\"manual.html\">1 &ndash; Introduction</A>\n<P>\n<LI><A HREF=\"manual.html#2\">2 &ndash; The Language</A>\n<UL>\n<LI><A HREF=\"manual.html#2.1\">2.1 &ndash; Lexical Conventions</A>\n<LI><A HREF=\"manual.html#2.2\">2.2 &ndash; Values and Types</A>\n<UL>\n<LI><A HREF=\"manual.html#2.2.1\">2.2.1 &ndash; Coercion</A>\n</UL>\n<LI><A HREF=\"manual.html#2.3\">2.3 &ndash; Variables</A>\n<LI><A HREF=\"manual.html#2.4\">2.4 &ndash; Statements</A>\n<UL>\n<LI><A HREF=\"manual.html#2.4.1\">2.4.1 &ndash; Chunks</A>\n<LI><A HREF=\"manual.html#2.4.2\">2.4.2 &ndash; Blocks</A>\n<LI><A HREF=\"manual.html#2.4.3\">2.4.3 &ndash; Assignment</A>\n<LI><A HREF=\"manual.html#2.4.4\">2.4.4 &ndash; Control Structures</A>\n<LI><A HREF=\"manual.html#2.4.5\">2.4.5 &ndash; For Statement</A>\n<LI><A HREF=\"manual.html#2.4.6\">2.4.6 &ndash; Function Calls as Statements</A>\n<LI><A HREF=\"manual.html#2.4.7\">2.4.7 &ndash; Local Declarations</A>\n</UL>\n<LI><A HREF=\"manual.html#2.5\">2.5 &ndash; Expressions</A>\n<UL>\n<LI><A HREF=\"manual.html#2.5.1\">2.5.1 &ndash; Arithmetic Operators</A>\n<LI><A HREF=\"manual.html#2.5.2\">2.5.2 &ndash; Relational Operators</A>\n<LI><A HREF=\"manual.html#2.5.3\">2.5.3 &ndash; Logical Operators</A>\n<LI><A HREF=\"manual.html#2.5.4\">2.5.4 &ndash; Concatenation</A>\n<LI><A HREF=\"manual.html#2.5.5\">2.5.5 &ndash; The Length Operator</A>\n<LI><A HREF=\"manual.html#2.5.6\">2.5.6 &ndash; Precedence</A>\n<LI><A HREF=\"manual.html#2.5.7\">2.5.7 &ndash; Table Constructors</A>\n<LI><A HREF=\"manual.html#2.5.8\">2.5.8 &ndash; Function Calls</A>\n<LI><A HREF=\"manual.html#2.5.9\">2.5.9 &ndash; Function Definitions</A>\n</UL>\n<LI><A HREF=\"manual.html#2.6\">2.6 &ndash; Visibility Rules</A>\n<LI><A HREF=\"manual.html#2.7\">2.7 &ndash; Error Handling</A>\n<LI><A HREF=\"manual.html#2.8\">2.8 &ndash; Metatables</A>\n<LI><A HREF=\"manual.html#2.9\">2.9 &ndash; Environments</A>\n<LI><A HREF=\"manual.html#2.10\">2.10 &ndash; Garbage Collection</A>\n<UL>\n<LI><A HREF=\"manual.html#2.10.1\">2.10.1 &ndash; Garbage-Collection Metamethods</A>\n<LI><A HREF=\"manual.html#2.10.2\">2.10.2 &ndash; Weak Tables</A>\n</UL>\n<LI><A HREF=\"manual.html#2.11\">2.11 &ndash; Coroutines</A>\n</UL>\n<P>\n<LI><A HREF=\"manual.html#3\">3 &ndash; The Application Program Interface</A>\n<UL>\n<LI><A HREF=\"manual.html#3.1\">3.1 &ndash; The Stack</A>\n<LI><A HREF=\"manual.html#3.2\">3.2 &ndash; Stack Size</A>\n<LI><A HREF=\"manual.html#3.3\">3.3 &ndash; Pseudo-Indices</A>\n<LI><A HREF=\"manual.html#3.4\">3.4 &ndash; C Closures</A>\n<LI><A HREF=\"manual.html#3.5\">3.5 &ndash; Registry</A>\n<LI><A HREF=\"manual.html#3.6\">3.6 &ndash; Error Handling in C</A>\n<LI><A HREF=\"manual.html#3.7\">3.7 &ndash; Functions and Types</A>\n<LI><A HREF=\"manual.html#3.8\">3.8 &ndash; The Debug Interface</A>\n</UL>\n<P>\n<LI><A HREF=\"manual.html#4\">4 &ndash; The Auxiliary Library</A>\n<UL>\n<LI><A HREF=\"manual.html#4.1\">4.1 &ndash; Functions and Types</A>\n</UL>\n<P>\n<LI><A HREF=\"manual.html#5\">5 &ndash; Standard Libraries</A>\n<UL>\n<LI><A HREF=\"manual.html#5.1\">5.1 &ndash; Basic Functions</A>\n<LI><A HREF=\"manual.html#5.2\">5.2 &ndash; Coroutine Manipulation</A>\n<LI><A HREF=\"manual.html#5.3\">5.3 &ndash; Modules</A>\n<LI><A HREF=\"manual.html#5.4\">5.4 &ndash; String Manipulation</A>\n<UL>\n<LI><A HREF=\"manual.html#5.4.1\">5.4.1 &ndash; Patterns</A>\n</UL>\n<LI><A HREF=\"manual.html#5.5\">5.5 &ndash; Table Manipulation</A>\n<LI><A HREF=\"manual.html#5.6\">5.6 &ndash; Mathematical Functions</A>\n<LI><A HREF=\"manual.html#5.7\">5.7 &ndash; Input and Output Facilities</A>\n<LI><A HREF=\"manual.html#5.8\">5.8 &ndash; Operating System Facilities</A>\n<LI><A HREF=\"manual.html#5.9\">5.9 &ndash; The Debug Library</A>\n</UL>\n<P>\n<LI><A HREF=\"manual.html#6\">6 &ndash; Lua Stand-alone</A>\n<P>\n<LI><A HREF=\"manual.html#7\">7 &ndash; Incompatibilities with the Previous Version</A>\n<UL>\n<LI><A HREF=\"manual.html#7.1\">7.1 &ndash; Changes in the Language</A>\n<LI><A HREF=\"manual.html#7.2\">7.2 &ndash; Changes in the Libraries</A>\n<LI><A HREF=\"manual.html#7.3\">7.3 &ndash; Changes in the API</A>\n</UL>\n<P>\n<LI><A HREF=\"manual.html#8\">8 &ndash; The Complete Syntax of Lua</A>\n</UL>\n\n<H2><A NAME=\"index\">Index</A></H2>\n<TABLE WIDTH=\"100%\">\n<TR VALIGN=\"top\">\n<TD>\n<H3><A NAME=\"functions\">Lua functions</A></H3>\n<A HREF=\"manual.html#pdf-_G\">_G</A><BR>\n<A HREF=\"manual.html#pdf-_VERSION\">_VERSION</A><BR>\n<P>\n\n<A HREF=\"manual.html#pdf-assert\">assert</A><BR>\n<A HREF=\"manual.html#pdf-collectgarbage\">collectgarbage</A><BR>\n<A HREF=\"manual.html#pdf-dofile\">dofile</A><BR>\n<A HREF=\"manual.html#pdf-error\">error</A><BR>\n<A HREF=\"manual.html#pdf-getfenv\">getfenv</A><BR>\n<A HREF=\"manual.html#pdf-getmetatable\">getmetatable</A><BR>\n<A HREF=\"manual.html#pdf-ipairs\">ipairs</A><BR>\n<A HREF=\"manual.html#pdf-load\">load</A><BR>\n<A HREF=\"manual.html#pdf-loadfile\">loadfile</A><BR>\n<A HREF=\"manual.html#pdf-loadstring\">loadstring</A><BR>\n<A HREF=\"manual.html#pdf-module\">module</A><BR>\n<A HREF=\"manual.html#pdf-next\">next</A><BR>\n<A HREF=\"manual.html#pdf-pairs\">pairs</A><BR>\n<A HREF=\"manual.html#pdf-pcall\">pcall</A><BR>\n<A HREF=\"manual.html#pdf-print\">print</A><BR>\n<A HREF=\"manual.html#pdf-rawequal\">rawequal</A><BR>\n<A HREF=\"manual.html#pdf-rawget\">rawget</A><BR>\n<A HREF=\"manual.html#pdf-rawset\">rawset</A><BR>\n<A HREF=\"manual.html#pdf-require\">require</A><BR>\n<A HREF=\"manual.html#pdf-select\">select</A><BR>\n<A HREF=\"manual.html#pdf-setfenv\">setfenv</A><BR>\n<A HREF=\"manual.html#pdf-setmetatable\">setmetatable</A><BR>\n<A HREF=\"manual.html#pdf-tonumber\">tonumber</A><BR>\n<A HREF=\"manual.html#pdf-tostring\">tostring</A><BR>\n<A HREF=\"manual.html#pdf-type\">type</A><BR>\n<A HREF=\"manual.html#pdf-unpack\">unpack</A><BR>\n<A HREF=\"manual.html#pdf-xpcall\">xpcall</A><BR>\n<P>\n\n<A HREF=\"manual.html#pdf-coroutine.create\">coroutine.create</A><BR>\n<A HREF=\"manual.html#pdf-coroutine.resume\">coroutine.resume</A><BR>\n<A HREF=\"manual.html#pdf-coroutine.running\">coroutine.running</A><BR>\n<A HREF=\"manual.html#pdf-coroutine.status\">coroutine.status</A><BR>\n<A HREF=\"manual.html#pdf-coroutine.wrap\">coroutine.wrap</A><BR>\n<A HREF=\"manual.html#pdf-coroutine.yield\">coroutine.yield</A><BR>\n<P>\n\n<A HREF=\"manual.html#pdf-debug.debug\">debug.debug</A><BR>\n<A HREF=\"manual.html#pdf-debug.getfenv\">debug.getfenv</A><BR>\n<A HREF=\"manual.html#pdf-debug.gethook\">debug.gethook</A><BR>\n<A HREF=\"manual.html#pdf-debug.getinfo\">debug.getinfo</A><BR>\n<A HREF=\"manual.html#pdf-debug.getlocal\">debug.getlocal</A><BR>\n<A HREF=\"manual.html#pdf-debug.getmetatable\">debug.getmetatable</A><BR>\n<A HREF=\"manual.html#pdf-debug.getregistry\">debug.getregistry</A><BR>\n<A HREF=\"manual.html#pdf-debug.getupvalue\">debug.getupvalue</A><BR>\n<A HREF=\"manual.html#pdf-debug.setfenv\">debug.setfenv</A><BR>\n<A HREF=\"manual.html#pdf-debug.sethook\">debug.sethook</A><BR>\n<A HREF=\"manual.html#pdf-debug.setlocal\">debug.setlocal</A><BR>\n<A HREF=\"manual.html#pdf-debug.setmetatable\">debug.setmetatable</A><BR>\n<A HREF=\"manual.html#pdf-debug.setupvalue\">debug.setupvalue</A><BR>\n<A HREF=\"manual.html#pdf-debug.traceback\">debug.traceback</A><BR>\n\n</TD>\n<TD>\n<H3>&nbsp;</H3>\n<A HREF=\"manual.html#pdf-file:close\">file:close</A><BR>\n<A HREF=\"manual.html#pdf-file:flush\">file:flush</A><BR>\n<A HREF=\"manual.html#pdf-file:lines\">file:lines</A><BR>\n<A HREF=\"manual.html#pdf-file:read\">file:read</A><BR>\n<A HREF=\"manual.html#pdf-file:seek\">file:seek</A><BR>\n<A HREF=\"manual.html#pdf-file:setvbuf\">file:setvbuf</A><BR>\n<A HREF=\"manual.html#pdf-file:write\">file:write</A><BR>\n<P>\n\n<A HREF=\"manual.html#pdf-io.close\">io.close</A><BR>\n<A HREF=\"manual.html#pdf-io.flush\">io.flush</A><BR>\n<A HREF=\"manual.html#pdf-io.input\">io.input</A><BR>\n<A HREF=\"manual.html#pdf-io.lines\">io.lines</A><BR>\n<A HREF=\"manual.html#pdf-io.open\">io.open</A><BR>\n<A HREF=\"manual.html#pdf-io.output\">io.output</A><BR>\n<A HREF=\"manual.html#pdf-io.popen\">io.popen</A><BR>\n<A HREF=\"manual.html#pdf-io.read\">io.read</A><BR>\n<A HREF=\"manual.html#pdf-io.stderr\">io.stderr</A><BR>\n<A HREF=\"manual.html#pdf-io.stdin\">io.stdin</A><BR>\n<A HREF=\"manual.html#pdf-io.stdout\">io.stdout</A><BR>\n<A HREF=\"manual.html#pdf-io.tmpfile\">io.tmpfile</A><BR>\n<A HREF=\"manual.html#pdf-io.type\">io.type</A><BR>\n<A HREF=\"manual.html#pdf-io.write\">io.write</A><BR>\n<P>\n\n<A HREF=\"manual.html#pdf-math.abs\">math.abs</A><BR>\n<A HREF=\"manual.html#pdf-math.acos\">math.acos</A><BR>\n<A HREF=\"manual.html#pdf-math.asin\">math.asin</A><BR>\n<A HREF=\"manual.html#pdf-math.atan\">math.atan</A><BR>\n<A HREF=\"manual.html#pdf-math.atan2\">math.atan2</A><BR>\n<A HREF=\"manual.html#pdf-math.ceil\">math.ceil</A><BR>\n<A HREF=\"manual.html#pdf-math.cos\">math.cos</A><BR>\n<A HREF=\"manual.html#pdf-math.cosh\">math.cosh</A><BR>\n<A HREF=\"manual.html#pdf-math.deg\">math.deg</A><BR>\n<A HREF=\"manual.html#pdf-math.exp\">math.exp</A><BR>\n<A HREF=\"manual.html#pdf-math.floor\">math.floor</A><BR>\n<A HREF=\"manual.html#pdf-math.fmod\">math.fmod</A><BR>\n<A HREF=\"manual.html#pdf-math.frexp\">math.frexp</A><BR>\n<A HREF=\"manual.html#pdf-math.huge\">math.huge</A><BR>\n<A HREF=\"manual.html#pdf-math.ldexp\">math.ldexp</A><BR>\n<A HREF=\"manual.html#pdf-math.log\">math.log</A><BR>\n<A HREF=\"manual.html#pdf-math.log10\">math.log10</A><BR>\n<A HREF=\"manual.html#pdf-math.max\">math.max</A><BR>\n<A HREF=\"manual.html#pdf-math.min\">math.min</A><BR>\n<A HREF=\"manual.html#pdf-math.modf\">math.modf</A><BR>\n<A HREF=\"manual.html#pdf-math.pi\">math.pi</A><BR>\n<A HREF=\"manual.html#pdf-math.pow\">math.pow</A><BR>\n<A HREF=\"manual.html#pdf-math.rad\">math.rad</A><BR>\n<A HREF=\"manual.html#pdf-math.random\">math.random</A><BR>\n<A HREF=\"manual.html#pdf-math.randomseed\">math.randomseed</A><BR>\n<A HREF=\"manual.html#pdf-math.sin\">math.sin</A><BR>\n<A HREF=\"manual.html#pdf-math.sinh\">math.sinh</A><BR>\n<A HREF=\"manual.html#pdf-math.sqrt\">math.sqrt</A><BR>\n<A HREF=\"manual.html#pdf-math.tan\">math.tan</A><BR>\n<A HREF=\"manual.html#pdf-math.tanh\">math.tanh</A><BR>\n<P>\n\n<A HREF=\"manual.html#pdf-os.clock\">os.clock</A><BR>\n<A HREF=\"manual.html#pdf-os.date\">os.date</A><BR>\n<A HREF=\"manual.html#pdf-os.difftime\">os.difftime</A><BR>\n<A HREF=\"manual.html#pdf-os.execute\">os.execute</A><BR>\n<A HREF=\"manual.html#pdf-os.exit\">os.exit</A><BR>\n<A HREF=\"manual.html#pdf-os.getenv\">os.getenv</A><BR>\n<A HREF=\"manual.html#pdf-os.remove\">os.remove</A><BR>\n<A HREF=\"manual.html#pdf-os.rename\">os.rename</A><BR>\n<A HREF=\"manual.html#pdf-os.setlocale\">os.setlocale</A><BR>\n<A HREF=\"manual.html#pdf-os.time\">os.time</A><BR>\n<A HREF=\"manual.html#pdf-os.tmpname\">os.tmpname</A><BR>\n<P>\n\n<A HREF=\"manual.html#pdf-package.cpath\">package.cpath</A><BR>\n<A HREF=\"manual.html#pdf-package.loaded\">package.loaded</A><BR>\n<A HREF=\"manual.html#pdf-package.loaders\">package.loaders</A><BR>\n<A HREF=\"manual.html#pdf-package.loadlib\">package.loadlib</A><BR>\n<A HREF=\"manual.html#pdf-package.path\">package.path</A><BR>\n<A HREF=\"manual.html#pdf-package.preload\">package.preload</A><BR>\n<A HREF=\"manual.html#pdf-package.seeall\">package.seeall</A><BR>\n<P>\n\n<A HREF=\"manual.html#pdf-string.byte\">string.byte</A><BR>\n<A HREF=\"manual.html#pdf-string.char\">string.char</A><BR>\n<A HREF=\"manual.html#pdf-string.dump\">string.dump</A><BR>\n<A HREF=\"manual.html#pdf-string.find\">string.find</A><BR>\n<A HREF=\"manual.html#pdf-string.format\">string.format</A><BR>\n<A HREF=\"manual.html#pdf-string.gmatch\">string.gmatch</A><BR>\n<A HREF=\"manual.html#pdf-string.gsub\">string.gsub</A><BR>\n<A HREF=\"manual.html#pdf-string.len\">string.len</A><BR>\n<A HREF=\"manual.html#pdf-string.lower\">string.lower</A><BR>\n<A HREF=\"manual.html#pdf-string.match\">string.match</A><BR>\n<A HREF=\"manual.html#pdf-string.rep\">string.rep</A><BR>\n<A HREF=\"manual.html#pdf-string.reverse\">string.reverse</A><BR>\n<A HREF=\"manual.html#pdf-string.sub\">string.sub</A><BR>\n<A HREF=\"manual.html#pdf-string.upper\">string.upper</A><BR>\n<P>\n\n<A HREF=\"manual.html#pdf-table.concat\">table.concat</A><BR>\n<A HREF=\"manual.html#pdf-table.insert\">table.insert</A><BR>\n<A HREF=\"manual.html#pdf-table.maxn\">table.maxn</A><BR>\n<A HREF=\"manual.html#pdf-table.remove\">table.remove</A><BR>\n<A HREF=\"manual.html#pdf-table.sort\">table.sort</A><BR>\n\n</TD>\n<TD>\n<H3>C API</H3>\n<A HREF=\"manual.html#lua_Alloc\">lua_Alloc</A><BR>\n<A HREF=\"manual.html#lua_CFunction\">lua_CFunction</A><BR>\n<A HREF=\"manual.html#lua_Debug\">lua_Debug</A><BR>\n<A HREF=\"manual.html#lua_Hook\">lua_Hook</A><BR>\n<A HREF=\"manual.html#lua_Integer\">lua_Integer</A><BR>\n<A HREF=\"manual.html#lua_Number\">lua_Number</A><BR>\n<A HREF=\"manual.html#lua_Reader\">lua_Reader</A><BR>\n<A HREF=\"manual.html#lua_State\">lua_State</A><BR>\n<A HREF=\"manual.html#lua_Writer\">lua_Writer</A><BR>\n<P>\n\n<A HREF=\"manual.html#lua_atpanic\">lua_atpanic</A><BR>\n<A HREF=\"manual.html#lua_call\">lua_call</A><BR>\n<A HREF=\"manual.html#lua_checkstack\">lua_checkstack</A><BR>\n<A HREF=\"manual.html#lua_close\">lua_close</A><BR>\n<A HREF=\"manual.html#lua_concat\">lua_concat</A><BR>\n<A HREF=\"manual.html#lua_cpcall\">lua_cpcall</A><BR>\n<A HREF=\"manual.html#lua_createtable\">lua_createtable</A><BR>\n<A HREF=\"manual.html#lua_dump\">lua_dump</A><BR>\n<A HREF=\"manual.html#lua_equal\">lua_equal</A><BR>\n<A HREF=\"manual.html#lua_error\">lua_error</A><BR>\n<A HREF=\"manual.html#lua_gc\">lua_gc</A><BR>\n<A HREF=\"manual.html#lua_getallocf\">lua_getallocf</A><BR>\n<A HREF=\"manual.html#lua_getfenv\">lua_getfenv</A><BR>\n<A HREF=\"manual.html#lua_getfield\">lua_getfield</A><BR>\n<A HREF=\"manual.html#lua_getglobal\">lua_getglobal</A><BR>\n<A HREF=\"manual.html#lua_gethook\">lua_gethook</A><BR>\n<A HREF=\"manual.html#lua_gethookcount\">lua_gethookcount</A><BR>\n<A HREF=\"manual.html#lua_gethookmask\">lua_gethookmask</A><BR>\n<A HREF=\"manual.html#lua_getinfo\">lua_getinfo</A><BR>\n<A HREF=\"manual.html#lua_getlocal\">lua_getlocal</A><BR>\n<A HREF=\"manual.html#lua_getmetatable\">lua_getmetatable</A><BR>\n<A HREF=\"manual.html#lua_getstack\">lua_getstack</A><BR>\n<A HREF=\"manual.html#lua_gettable\">lua_gettable</A><BR>\n<A HREF=\"manual.html#lua_gettop\">lua_gettop</A><BR>\n<A HREF=\"manual.html#lua_getupvalue\">lua_getupvalue</A><BR>\n<A HREF=\"manual.html#lua_insert\">lua_insert</A><BR>\n<A HREF=\"manual.html#lua_isboolean\">lua_isboolean</A><BR>\n<A HREF=\"manual.html#lua_iscfunction\">lua_iscfunction</A><BR>\n<A HREF=\"manual.html#lua_isfunction\">lua_isfunction</A><BR>\n<A HREF=\"manual.html#lua_islightuserdata\">lua_islightuserdata</A><BR>\n<A HREF=\"manual.html#lua_isnil\">lua_isnil</A><BR>\n<A HREF=\"manual.html#lua_isnone\">lua_isnone</A><BR>\n<A HREF=\"manual.html#lua_isnoneornil\">lua_isnoneornil</A><BR>\n<A HREF=\"manual.html#lua_isnumber\">lua_isnumber</A><BR>\n<A HREF=\"manual.html#lua_isstring\">lua_isstring</A><BR>\n<A HREF=\"manual.html#lua_istable\">lua_istable</A><BR>\n<A HREF=\"manual.html#lua_isthread\">lua_isthread</A><BR>\n<A HREF=\"manual.html#lua_isuserdata\">lua_isuserdata</A><BR>\n<A HREF=\"manual.html#lua_lessthan\">lua_lessthan</A><BR>\n<A HREF=\"manual.html#lua_load\">lua_load</A><BR>\n<A HREF=\"manual.html#lua_newstate\">lua_newstate</A><BR>\n<A HREF=\"manual.html#lua_newtable\">lua_newtable</A><BR>\n<A HREF=\"manual.html#lua_newthread\">lua_newthread</A><BR>\n<A HREF=\"manual.html#lua_newuserdata\">lua_newuserdata</A><BR>\n<A HREF=\"manual.html#lua_next\">lua_next</A><BR>\n<A HREF=\"manual.html#lua_objlen\">lua_objlen</A><BR>\n<A HREF=\"manual.html#lua_pcall\">lua_pcall</A><BR>\n<A HREF=\"manual.html#lua_pop\">lua_pop</A><BR>\n<A HREF=\"manual.html#lua_pushboolean\">lua_pushboolean</A><BR>\n<A HREF=\"manual.html#lua_pushcclosure\">lua_pushcclosure</A><BR>\n<A HREF=\"manual.html#lua_pushcfunction\">lua_pushcfunction</A><BR>\n<A HREF=\"manual.html#lua_pushfstring\">lua_pushfstring</A><BR>\n<A HREF=\"manual.html#lua_pushinteger\">lua_pushinteger</A><BR>\n<A HREF=\"manual.html#lua_pushlightuserdata\">lua_pushlightuserdata</A><BR>\n<A HREF=\"manual.html#lua_pushliteral\">lua_pushliteral</A><BR>\n<A HREF=\"manual.html#lua_pushlstring\">lua_pushlstring</A><BR>\n<A HREF=\"manual.html#lua_pushnil\">lua_pushnil</A><BR>\n<A HREF=\"manual.html#lua_pushnumber\">lua_pushnumber</A><BR>\n<A HREF=\"manual.html#lua_pushstring\">lua_pushstring</A><BR>\n<A HREF=\"manual.html#lua_pushthread\">lua_pushthread</A><BR>\n<A HREF=\"manual.html#lua_pushvalue\">lua_pushvalue</A><BR>\n<A HREF=\"manual.html#lua_pushvfstring\">lua_pushvfstring</A><BR>\n<A HREF=\"manual.html#lua_rawequal\">lua_rawequal</A><BR>\n<A HREF=\"manual.html#lua_rawget\">lua_rawget</A><BR>\n<A HREF=\"manual.html#lua_rawgeti\">lua_rawgeti</A><BR>\n<A HREF=\"manual.html#lua_rawset\">lua_rawset</A><BR>\n<A HREF=\"manual.html#lua_rawseti\">lua_rawseti</A><BR>\n<A HREF=\"manual.html#lua_register\">lua_register</A><BR>\n<A HREF=\"manual.html#lua_remove\">lua_remove</A><BR>\n<A HREF=\"manual.html#lua_replace\">lua_replace</A><BR>\n<A HREF=\"manual.html#lua_resume\">lua_resume</A><BR>\n<A HREF=\"manual.html#lua_setallocf\">lua_setallocf</A><BR>\n<A HREF=\"manual.html#lua_setfenv\">lua_setfenv</A><BR>\n<A HREF=\"manual.html#lua_setfield\">lua_setfield</A><BR>\n<A HREF=\"manual.html#lua_setglobal\">lua_setglobal</A><BR>\n<A HREF=\"manual.html#lua_sethook\">lua_sethook</A><BR>\n<A HREF=\"manual.html#lua_setlocal\">lua_setlocal</A><BR>\n<A HREF=\"manual.html#lua_setmetatable\">lua_setmetatable</A><BR>\n<A HREF=\"manual.html#lua_settable\">lua_settable</A><BR>\n<A HREF=\"manual.html#lua_settop\">lua_settop</A><BR>\n<A HREF=\"manual.html#lua_setupvalue\">lua_setupvalue</A><BR>\n<A HREF=\"manual.html#lua_status\">lua_status</A><BR>\n<A HREF=\"manual.html#lua_toboolean\">lua_toboolean</A><BR>\n<A HREF=\"manual.html#lua_tocfunction\">lua_tocfunction</A><BR>\n<A HREF=\"manual.html#lua_tointeger\">lua_tointeger</A><BR>\n<A HREF=\"manual.html#lua_tolstring\">lua_tolstring</A><BR>\n<A HREF=\"manual.html#lua_tonumber\">lua_tonumber</A><BR>\n<A HREF=\"manual.html#lua_topointer\">lua_topointer</A><BR>\n<A HREF=\"manual.html#lua_tostring\">lua_tostring</A><BR>\n<A HREF=\"manual.html#lua_tothread\">lua_tothread</A><BR>\n<A HREF=\"manual.html#lua_touserdata\">lua_touserdata</A><BR>\n<A HREF=\"manual.html#lua_type\">lua_type</A><BR>\n<A HREF=\"manual.html#lua_typename\">lua_typename</A><BR>\n<A HREF=\"manual.html#lua_upvalueindex\">lua_upvalueindex</A><BR>\n<A HREF=\"manual.html#lua_xmove\">lua_xmove</A><BR>\n<A HREF=\"manual.html#lua_yield\">lua_yield</A><BR>\n\n</TD>\n<TD>\n<H3>auxiliary library</H3>\n<A HREF=\"manual.html#luaL_Buffer\">luaL_Buffer</A><BR>\n<A HREF=\"manual.html#luaL_Reg\">luaL_Reg</A><BR>\n<P>\n\n<A HREF=\"manual.html#luaL_addchar\">luaL_addchar</A><BR>\n<A HREF=\"manual.html#luaL_addlstring\">luaL_addlstring</A><BR>\n<A HREF=\"manual.html#luaL_addsize\">luaL_addsize</A><BR>\n<A HREF=\"manual.html#luaL_addstring\">luaL_addstring</A><BR>\n<A HREF=\"manual.html#luaL_addvalue\">luaL_addvalue</A><BR>\n<A HREF=\"manual.html#luaL_argcheck\">luaL_argcheck</A><BR>\n<A HREF=\"manual.html#luaL_argerror\">luaL_argerror</A><BR>\n<A HREF=\"manual.html#luaL_buffinit\">luaL_buffinit</A><BR>\n<A HREF=\"manual.html#luaL_callmeta\">luaL_callmeta</A><BR>\n<A HREF=\"manual.html#luaL_checkany\">luaL_checkany</A><BR>\n<A HREF=\"manual.html#luaL_checkint\">luaL_checkint</A><BR>\n<A HREF=\"manual.html#luaL_checkinteger\">luaL_checkinteger</A><BR>\n<A HREF=\"manual.html#luaL_checklong\">luaL_checklong</A><BR>\n<A HREF=\"manual.html#luaL_checklstring\">luaL_checklstring</A><BR>\n<A HREF=\"manual.html#luaL_checknumber\">luaL_checknumber</A><BR>\n<A HREF=\"manual.html#luaL_checkoption\">luaL_checkoption</A><BR>\n<A HREF=\"manual.html#luaL_checkstack\">luaL_checkstack</A><BR>\n<A HREF=\"manual.html#luaL_checkstring\">luaL_checkstring</A><BR>\n<A HREF=\"manual.html#luaL_checktype\">luaL_checktype</A><BR>\n<A HREF=\"manual.html#luaL_checkudata\">luaL_checkudata</A><BR>\n<A HREF=\"manual.html#luaL_dofile\">luaL_dofile</A><BR>\n<A HREF=\"manual.html#luaL_dostring\">luaL_dostring</A><BR>\n<A HREF=\"manual.html#luaL_error\">luaL_error</A><BR>\n<A HREF=\"manual.html#luaL_getmetafield\">luaL_getmetafield</A><BR>\n<A HREF=\"manual.html#luaL_getmetatable\">luaL_getmetatable</A><BR>\n<A HREF=\"manual.html#luaL_gsub\">luaL_gsub</A><BR>\n<A HREF=\"manual.html#luaL_loadbuffer\">luaL_loadbuffer</A><BR>\n<A HREF=\"manual.html#luaL_loadfile\">luaL_loadfile</A><BR>\n<A HREF=\"manual.html#luaL_loadstring\">luaL_loadstring</A><BR>\n<A HREF=\"manual.html#luaL_newmetatable\">luaL_newmetatable</A><BR>\n<A HREF=\"manual.html#luaL_newstate\">luaL_newstate</A><BR>\n<A HREF=\"manual.html#luaL_openlibs\">luaL_openlibs</A><BR>\n<A HREF=\"manual.html#luaL_optint\">luaL_optint</A><BR>\n<A HREF=\"manual.html#luaL_optinteger\">luaL_optinteger</A><BR>\n<A HREF=\"manual.html#luaL_optlong\">luaL_optlong</A><BR>\n<A HREF=\"manual.html#luaL_optlstring\">luaL_optlstring</A><BR>\n<A HREF=\"manual.html#luaL_optnumber\">luaL_optnumber</A><BR>\n<A HREF=\"manual.html#luaL_optstring\">luaL_optstring</A><BR>\n<A HREF=\"manual.html#luaL_prepbuffer\">luaL_prepbuffer</A><BR>\n<A HREF=\"manual.html#luaL_pushresult\">luaL_pushresult</A><BR>\n<A HREF=\"manual.html#luaL_ref\">luaL_ref</A><BR>\n<A HREF=\"manual.html#luaL_register\">luaL_register</A><BR>\n<A HREF=\"manual.html#luaL_typename\">luaL_typename</A><BR>\n<A HREF=\"manual.html#luaL_typerror\">luaL_typerror</A><BR>\n<A HREF=\"manual.html#luaL_unref\">luaL_unref</A><BR>\n<A HREF=\"manual.html#luaL_where\">luaL_where</A><BR>\n\n</TD>\n</TR>\n</TABLE>\n<P>\n\n<HR>\n<SMALL CLASS=\"footer\">\nLast update:\nMon Feb 13 18:53:32 BRST 2012\n</SMALL>\n<!--\nLast change: revised for Lua 5.1.5\n-->\n\n</BODY>\n</HTML>\n"
  },
  {
    "path": "deps/lua/doc/lua.1",
    "content": ".\\\" $Id: lua.man,v 1.11 2006/01/06 16:03:34 lhf Exp $\n.TH LUA 1 \"$Date: 2006/01/06 16:03:34 $\"\n.SH NAME\nlua \\- Lua interpreter\n.SH SYNOPSIS\n.B lua\n[\n.I options\n]\n[\n.I script\n[\n.I args\n]\n]\n.SH DESCRIPTION\n.B lua\nis the stand-alone Lua interpreter.\nIt loads and executes Lua programs,\neither in textual source form or\nin precompiled binary form.\n(Precompiled binaries are output by\n.BR luac ,\nthe Lua compiler.)\n.B lua\ncan be used as a batch interpreter and also interactively.\n.LP\nThe given\n.I options\n(see below)\nare executed and then\nthe Lua program in file\n.I script\nis loaded and executed.\nThe given\n.I args\nare available to\n.I script\nas strings in a global table named\n.BR arg .\nIf these arguments contain spaces or other characters special to the shell,\nthen they should be quoted\n(but note that the quotes will be removed by the shell).\nThe arguments in\n.B arg\nstart at 0,\nwhich contains the string\n.RI ' script '.\nThe index of the last argument is stored in\n.BR arg.n .\nThe arguments given in the command line before\n.IR script ,\nincluding the name of the interpreter,\nare available in negative indices in\n.BR arg .\n.LP\nAt the very start,\nbefore even handling the command line,\n.B lua\nexecutes the contents of the environment variable\n.BR LUA_INIT ,\nif it is defined.\nIf the value of\n.B LUA_INIT\nis of the form\n.RI '@ filename ',\nthen\n.I filename\nis executed.\nOtherwise, the string is assumed to be a Lua statement and is executed.\n.LP\nOptions start with\n.B '\\-'\nand are described below.\nYou can use\n.B \"'\\--'\"\nto signal the end of options.\n.LP\nIf no arguments are given,\nthen\n.B \"\\-v \\-i\"\nis assumed when the standard input is a terminal;\notherwise,\n.B \"\\-\"\nis assumed.\n.LP\nIn interactive mode,\n.B lua\nprompts the user,\nreads lines from the standard input,\nand executes them as they are read.\nIf a line does not contain a complete statement,\nthen a secondary prompt is displayed and\nlines are read until a complete statement is formed or\na syntax error is found.\nSo, one way to interrupt the reading of an incomplete statement is\nto force a syntax error:\nadding a\n.B ';' \nin the middle of a statement is a sure way of forcing a syntax error\n(except inside multiline strings and comments; these must be closed explicitly).\nIf a line starts with\n.BR '=' ,\nthen\n.B lua\ndisplays the values of all the expressions in the remainder of the\nline. The expressions must be separated by commas.\nThe primary prompt is the value of the global variable\n.BR _PROMPT ,\nif this value is a string;\notherwise, the default prompt is used.\nSimilarly, the secondary prompt is the value of the global variable\n.BR _PROMPT2 .\nSo,\nto change the prompts,\nset the corresponding variable to a string of your choice.\nYou can do that after calling the interpreter\nor on the command line\n(but in this case you have to be careful with quotes\nif the prompt string contains a space; otherwise you may confuse the shell.)\nThe default prompts are \"> \" and \">> \".\n.SH OPTIONS\n.TP\n.B \\-\nload and execute the standard input as a file,\nthat is,\nnot interactively,\neven when the standard input is a terminal.\n.TP\n.BI \\-e \" stat\"\nexecute statement\n.IR stat .\nYou need to quote\n.I stat \nif it contains spaces, quotes,\nor other characters special to the shell.\n.TP\n.B \\-i\nenter interactive mode after\n.I script\nis executed.\n.TP\n.BI \\-l \" name\"\ncall\n.BI require(' name ')\nbefore executing\n.IR script .\nTypically used to load libraries.\n.TP\n.B \\-v\nshow version information.\n.SH \"SEE ALSO\"\n.BR luac (1)\n.br\nhttp://www.lua.org/\n.SH DIAGNOSTICS\nError messages should be self explanatory.\n.SH AUTHORS\nR. Ierusalimschy,\nL. H. de Figueiredo,\nand\nW. Celes\n.\\\" EOF\n"
  },
  {
    "path": "deps/lua/doc/lua.css",
    "content": "body {\n\tcolor: #000000 ;\n\tbackground-color: #FFFFFF ;\n\tfont-family: Helvetica, Arial, sans-serif ;\n\ttext-align: justify ;\n\tmargin-right: 30px ;\n\tmargin-left: 30px ;\n}\n\nh1, h2, h3, h4 {\n\tfont-family: Verdana, Geneva, sans-serif ;\n\tfont-weight: normal ;\n\tfont-style: italic ;\n}\n\nh2 {\n\tpadding-top: 0.4em ;\n\tpadding-bottom: 0.4em ;\n\tpadding-left: 30px ;\n\tpadding-right: 30px ;\n\tmargin-left: -30px ;\n\tbackground-color: #E0E0FF ;\n}\n\nh3 {\n\tpadding-left: 0.5em ;\n\tborder-left: solid #E0E0FF 1em ;\n}\n\ntable h3 {\n\tpadding-left: 0px ;\n\tborder-left: none ;\n}\n\na:link {\n\tcolor: #000080 ;\n\tbackground-color: inherit ;\n\ttext-decoration: none ;\n}\n\na:visited {\n\tbackground-color: inherit ;\n\ttext-decoration: none ;\n}\n\na:link:hover, a:visited:hover {\n\tcolor: #000080 ;\n\tbackground-color: #E0E0FF ;\n}\n\na:link:active, a:visited:active {\n\tcolor: #FF0000 ;\n}\n\nhr {\n\tborder: 0 ;\n\theight: 1px ;\n\tcolor: #a0a0a0 ;\n\tbackground-color: #a0a0a0 ;\n}\n\n:target {\n\tbackground-color: #F8F8F8 ;\n\tpadding: 8px ;\n\tborder: solid #a0a0a0 2px ;\n}\n\n.footer {\n\tcolor: gray ;\n\tfont-size: small ;\n}\n\ninput[type=text] {\n\tborder: solid #a0a0a0 2px ;\n\tborder-radius: 2em ;\n\t-moz-border-radius: 2em ;\n\tbackground-image: url('images/search.png') ;\n\tbackground-repeat: no-repeat;\n\tbackground-position: 4px center ;\n\tpadding-left: 20px ;\n\theight: 2em ;\n}\n\n"
  },
  {
    "path": "deps/lua/doc/lua.html",
    "content": "<!-- $Id: lua.man,v 1.11 2006/01/06 16:03:34 lhf Exp $ -->\n<HTML>\n<HEAD>\n<TITLE>LUA man page</TITLE>\n<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"lua.css\">\n</HEAD>\n\n<BODY BGCOLOR=\"#FFFFFF\">\n\n<H2>NAME</H2>\nlua - Lua interpreter\n<H2>SYNOPSIS</H2>\n<B>lua</B>\n[\n<I>options</I>\n]\n[\n<I>script</I>\n[\n<I>args</I>\n]\n]\n<H2>DESCRIPTION</H2>\n<B>lua</B>\nis the stand-alone Lua interpreter.\nIt loads and executes Lua programs,\neither in textual source form or\nin precompiled binary form.\n(Precompiled binaries are output by\n<B>luac</B>,\nthe Lua compiler.)\n<B>lua</B>\ncan be used as a batch interpreter and also interactively.\n<P>\nThe given\n<I>options</I>\n(see below)\nare executed and then\nthe Lua program in file\n<I>script</I>\nis loaded and executed.\nThe given\n<I>args</I>\nare available to\n<I>script</I>\nas strings in a global table named\n<B>arg</B>.\nIf these arguments contain spaces or other characters special to the shell,\nthen they should be quoted\n(but note that the quotes will be removed by the shell).\nThe arguments in\n<B>arg</B>\nstart at 0,\nwhich contains the string\n'<I>script</I>'.\nThe index of the last argument is stored in\n<B>arg.n</B>.\nThe arguments given in the command line before\n<I>script</I>,\nincluding the name of the interpreter,\nare available in negative indices in\n<B>arg</B>.\n<P>\nAt the very start,\nbefore even handling the command line,\n<B>lua</B>\nexecutes the contents of the environment variable\n<B>LUA_INIT</B>,\nif it is defined.\nIf the value of\n<B>LUA_INIT</B>\nis of the form\n'@<I>filename</I>',\nthen\n<I>filename</I>\nis executed.\nOtherwise, the string is assumed to be a Lua statement and is executed.\n<P>\nOptions start with\n<B>'-'</B>\nand are described below.\nYou can use\n<B>'--'</B>\nto signal the end of options.\n<P>\nIf no arguments are given,\nthen\n<B>\"-v -i\"</B>\nis assumed when the standard input is a terminal;\notherwise,\n<B>\"-\"</B>\nis assumed.\n<P>\nIn interactive mode,\n<B>lua</B>\nprompts the user,\nreads lines from the standard input,\nand executes them as they are read.\nIf a line does not contain a complete statement,\nthen a secondary prompt is displayed and\nlines are read until a complete statement is formed or\na syntax error is found.\nSo, one way to interrupt the reading of an incomplete statement is\nto force a syntax error:\nadding a\n<B>';'</B>\nin the middle of a statement is a sure way of forcing a syntax error\n(except inside multiline strings and comments; these must be closed explicitly).\nIf a line starts with\n<B>'='</B>,\nthen\n<B>lua</B>\ndisplays the values of all the expressions in the remainder of the\nline. The expressions must be separated by commas.\nThe primary prompt is the value of the global variable\n<B>_PROMPT</B>,\nif this value is a string;\notherwise, the default prompt is used.\nSimilarly, the secondary prompt is the value of the global variable\n<B>_PROMPT2</B>.\nSo,\nto change the prompts,\nset the corresponding variable to a string of your choice.\nYou can do that after calling the interpreter\nor on the command line\n(but in this case you have to be careful with quotes\nif the prompt string contains a space; otherwise you may confuse the shell.)\nThe default prompts are \"&gt; \" and \"&gt;&gt; \".\n<H2>OPTIONS</H2>\n<P>\n<B>-</B>\nload and execute the standard input as a file,\nthat is,\nnot interactively,\neven when the standard input is a terminal.\n<P>\n<B>-e </B><I>stat</I>\nexecute statement\n<I>stat</I>.\nYou need to quote\n<I>stat </I>\nif it contains spaces, quotes,\nor other characters special to the shell.\n<P>\n<B>-i</B>\nenter interactive mode after\n<I>script</I>\nis executed.\n<P>\n<B>-l </B><I>name</I>\ncall\n<B>require</B>('<I>name</I>')\nbefore executing\n<I>script</I>.\nTypically used to load libraries.\n<P>\n<B>-v</B>\nshow version information.\n<H2>SEE ALSO</H2>\n<B>luac</B>(1)\n<BR>\n<A HREF=\"http://www.lua.org/\">http://www.lua.org/</A>\n<H2>DIAGNOSTICS</H2>\nError messages should be self explanatory.\n<H2>AUTHORS</H2>\nR. Ierusalimschy,\nL. H. de Figueiredo,\nand\nW. Celes\n<!-- EOF -->\n</BODY>\n</HTML>\n"
  },
  {
    "path": "deps/lua/doc/luac.1",
    "content": ".\\\" $Id: luac.man,v 1.28 2006/01/06 16:03:34 lhf Exp $\n.TH LUAC 1 \"$Date: 2006/01/06 16:03:34 $\"\n.SH NAME\nluac \\- Lua compiler\n.SH SYNOPSIS\n.B luac\n[\n.I options\n] [\n.I filenames\n]\n.SH DESCRIPTION\n.B luac\nis the Lua compiler.\nIt translates programs written in the Lua programming language\ninto binary files that can be later loaded and executed.\n.LP\nThe main advantages of precompiling chunks are:\nfaster loading,\nprotecting source code from accidental user changes,\nand\noff-line syntax checking.\n.LP\nPre-compiling does not imply faster execution\nbecause in Lua chunks are always compiled into bytecodes before being executed.\n.B luac\nsimply allows those bytecodes to be saved in a file for later execution.\n.LP\nPre-compiled chunks are not necessarily smaller than the corresponding source.\nThe main goal in pre-compiling is faster loading.\n.LP\nThe binary files created by\n.B luac\nare portable only among architectures with the same word size and byte order.\n.LP\n.B luac\nproduces a single output file containing the bytecodes\nfor all source files given.\nBy default,\nthe output file is named\n.BR luac.out ,\nbut you can change this with the\n.B \\-o\noption.\n.LP\nIn the command line,\nyou can mix\ntext files containing Lua source and\nbinary files containing precompiled chunks.\nThis is useful to combine several precompiled chunks,\neven from different (but compatible) platforms,\ninto a single precompiled chunk.\n.LP\nYou can use\n.B \"'\\-'\"\nto indicate the standard input as a source file\nand\n.B \"'\\--'\"\nto signal the end of options\n(that is,\nall remaining arguments will be treated as files even if they start with\n.BR \"'\\-'\" ).\n.LP\nThe internal format of the binary files produced by\n.B luac\nis likely to change when a new version of Lua is released.\nSo,\nsave the source files of all Lua programs that you precompile.\n.LP\n.SH OPTIONS\nOptions must be separate.\n.TP\n.B \\-l\nproduce a listing of the compiled bytecode for Lua's virtual machine.\nListing bytecodes is useful to learn about Lua's virtual machine.\nIf no files are given, then\n.B luac\nloads\n.B luac.out\nand lists its contents.\n.TP\n.BI \\-o \" file\"\noutput to\n.IR file ,\ninstead of the default\n.BR luac.out .\n(You can use\n.B \"'\\-'\"\nfor standard output,\nbut not on platforms that open standard output in text mode.)\nThe output file may be a source file because\nall files are loaded before the output file is written.\nBe careful not to overwrite precious files.\n.TP\n.B \\-p\nload files but do not generate any output file.\nUsed mainly for syntax checking and for testing precompiled chunks:\ncorrupted files will probably generate errors when loaded.\nLua always performs a thorough integrity test on precompiled chunks.\nBytecode that passes this test is completely safe,\nin the sense that it will not break the interpreter.\nHowever,\nthere is no guarantee that such code does anything sensible.\n(None can be given, because the halting problem is unsolvable.)\nIf no files are given, then\n.B luac\nloads\n.B luac.out\nand tests its contents.\nNo messages are displayed if the file passes the integrity test.\n.TP\n.B \\-s\nstrip debug information before writing the output file.\nThis saves some space in very large chunks,\nbut if errors occur when running a stripped chunk,\nthen the error messages may not contain the full information they usually do.\nFor instance,\nline numbers and names of local variables are lost.\n.TP\n.B \\-v\nshow version information.\n.SH FILES\n.TP 15\n.B luac.out\ndefault output file\n.SH \"SEE ALSO\"\n.BR lua (1)\n.br\nhttp://www.lua.org/\n.SH DIAGNOSTICS\nError messages should be self explanatory.\n.SH AUTHORS\nL. H. de Figueiredo,\nR. Ierusalimschy and\nW. Celes\n.\\\" EOF\n"
  },
  {
    "path": "deps/lua/doc/luac.html",
    "content": "<!-- $Id: luac.man,v 1.28 2006/01/06 16:03:34 lhf Exp $ -->\n<HTML>\n<HEAD>\n<TITLE>LUAC man page</TITLE>\n<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"lua.css\">\n</HEAD>\n\n<BODY BGCOLOR=\"#FFFFFF\">\n\n<H2>NAME</H2>\nluac - Lua compiler\n<H2>SYNOPSIS</H2>\n<B>luac</B>\n[\n<I>options</I>\n] [\n<I>filenames</I>\n]\n<H2>DESCRIPTION</H2>\n<B>luac</B>\nis the Lua compiler.\nIt translates programs written in the Lua programming language\ninto binary files that can be later loaded and executed.\n<P>\nThe main advantages of precompiling chunks are:\nfaster loading,\nprotecting source code from accidental user changes,\nand\noff-line syntax checking.\n<P>\nPrecompiling does not imply faster execution\nbecause in Lua chunks are always compiled into bytecodes before being executed.\n<B>luac</B>\nsimply allows those bytecodes to be saved in a file for later execution.\n<P>\nPrecompiled chunks are not necessarily smaller than the corresponding source.\nThe main goal in precompiling is faster loading.\n<P>\nThe binary files created by\n<B>luac</B>\nare portable only among architectures with the same word size and byte order.\n<P>\n<B>luac</B>\nproduces a single output file containing the bytecodes\nfor all source files given.\nBy default,\nthe output file is named\n<B>luac.out</B>,\nbut you can change this with the\n<B>-o</B>\noption.\n<P>\nIn the command line,\nyou can mix\ntext files containing Lua source and\nbinary files containing precompiled chunks.\nThis is useful because several precompiled chunks,\neven from different (but compatible) platforms,\ncan be combined into a single precompiled chunk.\n<P>\nYou can use\n<B>'-'</B>\nto indicate the standard input as a source file\nand\n<B>'--'</B>\nto signal the end of options\n(that is,\nall remaining arguments will be treated as files even if they start with\n<B>'-'</B>).\n<P>\nThe internal format of the binary files produced by\n<B>luac</B>\nis likely to change when a new version of Lua is released.\nSo,\nsave the source files of all Lua programs that you precompile.\n<P>\n<H2>OPTIONS</H2>\nOptions must be separate.\n<P>\n<B>-l</B>\nproduce a listing of the compiled bytecode for Lua's virtual machine.\nListing bytecodes is useful to learn about Lua's virtual machine.\nIf no files are given, then\n<B>luac</B>\nloads\n<B>luac.out</B>\nand lists its contents.\n<P>\n<B>-o </B><I>file</I>\noutput to\n<I>file</I>,\ninstead of the default\n<B>luac.out</B>.\n(You can use\n<B>'-'</B>\nfor standard output,\nbut not on platforms that open standard output in text mode.)\nThe output file may be a source file because\nall files are loaded before the output file is written.\nBe careful not to overwrite precious files.\n<P>\n<B>-p</B>\nload files but do not generate any output file.\nUsed mainly for syntax checking and for testing precompiled chunks:\ncorrupted files will probably generate errors when loaded.\nLua always performs a thorough integrity test on precompiled chunks.\nBytecode that passes this test is completely safe,\nin the sense that it will not break the interpreter.\nHowever,\nthere is no guarantee that such code does anything sensible.\n(None can be given, because the halting problem is unsolvable.)\nIf no files are given, then\n<B>luac</B>\nloads\n<B>luac.out</B>\nand tests its contents.\nNo messages are displayed if the file passes the integrity test.\n<P>\n<B>-s</B>\nstrip debug information before writing the output file.\nThis saves some space in very large chunks,\nbut if errors occur when running a stripped chunk,\nthen the error messages may not contain the full information they usually do.\nFor instance,\nline numbers and names of local variables are lost.\n<P>\n<B>-v</B>\nshow version information.\n<H2>FILES</H2>\n<P>\n<B>luac.out</B>\ndefault output file\n<H2>SEE ALSO</H2>\n<B>lua</B>(1)\n<BR>\n<A HREF=\"http://www.lua.org/\">http://www.lua.org/</A>\n<H2>DIAGNOSTICS</H2>\nError messages should be self explanatory.\n<H2>AUTHORS</H2>\nL. H. de Figueiredo,\nR. Ierusalimschy and\nW. Celes\n<!-- EOF -->\n</BODY>\n</HTML>\n"
  },
  {
    "path": "deps/lua/doc/manual.css",
    "content": "h3 code {\n\tfont-family: inherit ;\n\tfont-size: inherit ;\n}\n\npre, code {\n\tfont-size: 12pt ;\n}\n\nspan.apii {\n\tfloat: right ;\n\tfont-family: inherit ;\n\tfont-style: normal ;\n\tfont-size: small ;\n\tcolor: gray ;\n}\n\np+h1, ul+h1 {\n\tpadding-top: 0.4em ;\n\tpadding-bottom: 0.4em ;\n\tpadding-left: 30px ;\n\tmargin-left: -30px ;\n\tbackground-color: #E0E0FF ;\n}\n"
  },
  {
    "path": "deps/lua/doc/manual.html",
    "content": "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n<html>\n\n<head>\n<title>Lua 5.1 Reference Manual</title>\n<link rel=\"stylesheet\" type=\"text/css\" href=\"lua.css\">\n<link rel=\"stylesheet\" type=\"text/css\" href=\"manual.css\">\n<META HTTP-EQUIV=\"content-type\" CONTENT=\"text/html; charset=iso-8859-1\">\n</head>\n\n<body>\n\n<hr>\n<h1>\n<a href=\"http://www.lua.org/\"><img src=\"logo.gif\" alt=\"\" border=\"0\"></a>\nLua 5.1 Reference Manual\n</h1>\n\nby Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes\n<p>\n<small>\nCopyright &copy; 2006&ndash;2012 Lua.org, PUC-Rio.\nFreely available under the terms of the\n<a href=\"http://www.lua.org/license.html\">Lua license</a>.\n</small>\n<hr>\n<p>\n\n<a href=\"contents.html#contents\">contents</A>\n&middot;\n<a href=\"contents.html#index\">index</A>\n&middot;\n<A HREF=\"http://www.lua.org/manual/\">other versions</A>\n\n<!-- ====================================================================== -->\n<p>\n\n<!-- $Id: manual.of,v 1.49.1.2 2012/01/13 20:23:26 roberto Exp $ -->\n\n\n\n\n<h1>1 - <a name=\"1\">Introduction</a></h1>\n\n<p>\nLua is an extension programming language designed to support\ngeneral procedural programming with data description\nfacilities.\nIt also offers good support for object-oriented programming,\nfunctional programming, and data-driven programming.\nLua is intended to be used as a powerful, light-weight\nscripting language for any program that needs one.\nLua is implemented as a library, written in <em>clean</em> C\n(that is, in the common subset of ANSI&nbsp;C and C++).\n\n\n<p>\nBeing an extension language, Lua has no notion of a \"main\" program:\nit only works <em>embedded</em> in a host client,\ncalled the <em>embedding program</em> or simply the <em>host</em>.\nThis host program can invoke functions to execute a piece of Lua code,\ncan write and read Lua variables,\nand can register C&nbsp;functions to be called by Lua code.\nThrough the use of C&nbsp;functions, Lua can be augmented to cope with\na wide range of different domains,\nthus creating customized programming languages sharing a syntactical framework.\nThe Lua distribution includes a sample host program called <code>lua</code>,\nwhich uses the Lua library to offer a complete, stand-alone Lua interpreter.\n\n\n<p>\nLua is free software,\nand is provided as usual with no guarantees,\nas stated in its license.\nThe implementation described in this manual is available\nat Lua's official web site, <code>www.lua.org</code>.\n\n\n<p>\nLike any other reference manual,\nthis document is dry in places.\nFor a discussion of the decisions behind the design of Lua,\nsee the technical papers available at Lua's web site.\nFor a detailed introduction to programming in Lua,\nsee Roberto's book, <em>Programming in Lua (Second Edition)</em>.\n\n\n\n<h1>2 - <a name=\"2\">The Language</a></h1>\n\n<p>\nThis section describes the lexis, the syntax, and the semantics of Lua.\nIn other words,\nthis section describes\nwhich tokens are valid,\nhow they can be combined,\nand what their combinations mean.\n\n\n<p>\nThe language constructs will be explained using the usual extended BNF notation,\nin which\n{<em>a</em>}&nbsp;means&nbsp;0 or more <em>a</em>'s, and\n[<em>a</em>]&nbsp;means an optional <em>a</em>.\nNon-terminals are shown like non-terminal,\nkeywords are shown like <b>kword</b>,\nand other terminal symbols are shown like `<b>=</b>&acute;.\nThe complete syntax of Lua can be found in <a href=\"#8\">&sect;8</a>\nat the end of this manual.\n\n\n\n<h2>2.1 - <a name=\"2.1\">Lexical Conventions</a></h2>\n\n<p>\n<em>Names</em>\n(also called <em>identifiers</em>)\nin Lua can be any string of letters,\ndigits, and underscores,\nnot beginning with a digit.\nThis coincides with the definition of names in most languages.\n(The definition of letter depends on the current locale:\nany character considered alphabetic by the current locale\ncan be used in an identifier.)\nIdentifiers are used to name variables and table fields.\n\n\n<p>\nThe following <em>keywords</em> are reserved\nand cannot be used as names:\n\n\n<pre>\n     and       break     do        else      elseif\n     end       false     for       function  if\n     in        local     nil       not       or\n     repeat    return    then      true      until     while\n</pre>\n\n<p>\nLua is a case-sensitive language:\n<code>and</code> is a reserved word, but <code>And</code> and <code>AND</code>\nare two different, valid names.\nAs a convention, names starting with an underscore followed by\nuppercase letters (such as <a href=\"#pdf-_VERSION\"><code>_VERSION</code></a>)\nare reserved for internal global variables used by Lua.\n\n\n<p>\nThe following strings denote other tokens:\n\n<pre>\n     +     -     *     /     %     ^     #\n     ==    ~=    &lt;=    &gt;=    &lt;     &gt;     =\n     (     )     {     }     [     ]\n     ;     :     ,     .     ..    ...\n</pre>\n\n<p>\n<em>Literal strings</em>\ncan be delimited by matching single or double quotes,\nand can contain the following C-like escape sequences:\n'<code>\\a</code>' (bell),\n'<code>\\b</code>' (backspace),\n'<code>\\f</code>' (form feed),\n'<code>\\n</code>' (newline),\n'<code>\\r</code>' (carriage return),\n'<code>\\t</code>' (horizontal tab),\n'<code>\\v</code>' (vertical tab),\n'<code>\\\\</code>' (backslash),\n'<code>\\\"</code>' (quotation mark [double quote]),\nand '<code>\\'</code>' (apostrophe [single quote]).\nMoreover, a backslash followed by a real newline\nresults in a newline in the string.\nA character in a string can also be specified by its numerical value\nusing the escape sequence <code>\\<em>ddd</em></code>,\nwhere <em>ddd</em> is a sequence of up to three decimal digits.\n(Note that if a numerical escape is to be followed by a digit,\nit must be expressed using exactly three digits.)\nStrings in Lua can contain any 8-bit value, including embedded zeros,\nwhich can be specified as '<code>\\0</code>'.\n\n\n<p>\nLiteral strings can also be defined using a long format\nenclosed by <em>long brackets</em>.\nWe define an <em>opening long bracket of level <em>n</em></em> as an opening\nsquare bracket followed by <em>n</em> equal signs followed by another\nopening square bracket.\nSo, an opening long bracket of level&nbsp;0 is written as <code>[[</code>,\nan opening long bracket of level&nbsp;1 is written as <code>[=[</code>,\nand so on.\nA <em>closing long bracket</em> is defined similarly;\nfor instance, a closing long bracket of level&nbsp;4 is written as <code>]====]</code>.\nA long string starts with an opening long bracket of any level and\nends at the first closing long bracket of the same level.\nLiterals in this bracketed form can run for several lines,\ndo not interpret any escape sequences,\nand ignore long brackets of any other level.\nThey can contain anything except a closing bracket of the proper level.\n\n\n<p>\nFor convenience,\nwhen the opening long bracket is immediately followed by a newline,\nthe newline is not included in the string.\nAs an example, in a system using ASCII\n(in which '<code>a</code>' is coded as&nbsp;97,\nnewline is coded as&nbsp;10, and '<code>1</code>' is coded as&nbsp;49),\nthe five literal strings below denote the same string:\n\n<pre>\n     a = 'alo\\n123\"'\n     a = \"alo\\n123\\\"\"\n     a = '\\97lo\\10\\04923\"'\n     a = [[alo\n     123\"]]\n     a = [==[\n     alo\n     123\"]==]\n</pre>\n\n<p>\nA <em>numerical constant</em> can be written with an optional decimal part\nand an optional decimal exponent.\nLua also accepts integer hexadecimal constants,\nby prefixing them with <code>0x</code>.\nExamples of valid numerical constants are\n\n<pre>\n     3   3.0   3.1416   314.16e-2   0.31416E1   0xff   0x56\n</pre>\n\n<p>\nA <em>comment</em> starts with a double hyphen (<code>--</code>)\nanywhere outside a string.\nIf the text immediately after <code>--</code> is not an opening long bracket,\nthe comment is a <em>short comment</em>,\nwhich runs until the end of the line.\nOtherwise, it is a <em>long comment</em>,\nwhich runs until the corresponding closing long bracket.\nLong comments are frequently used to disable code temporarily.\n\n\n\n\n\n<h2>2.2 - <a name=\"2.2\">Values and Types</a></h2>\n\n<p>\nLua is a <em>dynamically typed language</em>.\nThis means that\nvariables do not have types; only values do.\nThere are no type definitions in the language.\nAll values carry their own type.\n\n\n<p>\nAll values in Lua are <em>first-class values</em>.\nThis means that all values can be stored in variables,\npassed as arguments to other functions, and returned as results.\n\n\n<p>\nThere are eight basic types in Lua:\n<em>nil</em>, <em>boolean</em>, <em>number</em>,\n<em>string</em>, <em>function</em>, <em>userdata</em>,\n<em>thread</em>, and <em>table</em>.\n<em>Nil</em> is the type of the value <b>nil</b>,\nwhose main property is to be different from any other value;\nit usually represents the absence of a useful value.\n<em>Boolean</em> is the type of the values <b>false</b> and <b>true</b>.\nBoth <b>nil</b> and <b>false</b> make a condition false;\nany other value makes it true.\n<em>Number</em> represents real (double-precision floating-point) numbers.\n(It is easy to build Lua interpreters that use other\ninternal representations for numbers,\nsuch as single-precision float or long integers;\nsee file <code>luaconf.h</code>.)\n<em>String</em> represents arrays of characters.\n\nLua is 8-bit clean:\nstrings can contain any 8-bit character,\nincluding embedded zeros ('<code>\\0</code>') (see <a href=\"#2.1\">&sect;2.1</a>).\n\n\n<p>\nLua can call (and manipulate) functions written in Lua and\nfunctions written in C\n(see <a href=\"#2.5.8\">&sect;2.5.8</a>).\n\n\n<p>\nThe type <em>userdata</em> is provided to allow arbitrary C&nbsp;data to\nbe stored in Lua variables.\nThis type corresponds to a block of raw memory\nand has no pre-defined operations in Lua,\nexcept assignment and identity test.\nHowever, by using <em>metatables</em>,\nthe programmer can define operations for userdata values\n(see <a href=\"#2.8\">&sect;2.8</a>).\nUserdata values cannot be created or modified in Lua,\nonly through the C&nbsp;API.\nThis guarantees the integrity of data owned by the host program.\n\n\n<p>\nThe type <em>thread</em> represents independent threads of execution\nand it is used to implement coroutines (see <a href=\"#2.11\">&sect;2.11</a>).\nDo not confuse Lua threads with operating-system threads.\nLua supports coroutines on all systems,\neven those that do not support threads.\n\n\n<p>\nThe type <em>table</em> implements associative arrays,\nthat is, arrays that can be indexed not only with numbers,\nbut with any value (except <b>nil</b>).\nTables can be <em>heterogeneous</em>;\nthat is, they can contain values of all types (except <b>nil</b>).\nTables are the sole data structuring mechanism in Lua;\nthey can be used to represent ordinary arrays,\nsymbol tables, sets, records, graphs, trees, etc.\nTo represent records, Lua uses the field name as an index.\nThe language supports this representation by\nproviding <code>a.name</code> as syntactic sugar for <code>a[\"name\"]</code>.\nThere are several convenient ways to create tables in Lua\n(see <a href=\"#2.5.7\">&sect;2.5.7</a>).\n\n\n<p>\nLike indices,\nthe value of a table field can be of any type (except <b>nil</b>).\nIn particular,\nbecause functions are first-class values,\ntable fields can contain functions.\nThus tables can also carry <em>methods</em> (see <a href=\"#2.5.9\">&sect;2.5.9</a>).\n\n\n<p>\nTables, functions, threads, and (full) userdata values are <em>objects</em>:\nvariables do not actually <em>contain</em> these values,\nonly <em>references</em> to them.\nAssignment, parameter passing, and function returns\nalways manipulate references to such values;\nthese operations do not imply any kind of copy.\n\n\n<p>\nThe library function <a href=\"#pdf-type\"><code>type</code></a> returns a string describing the type\nof a given value.\n\n\n\n<h3>2.2.1 - <a name=\"2.2.1\">Coercion</a></h3>\n\n<p>\nLua provides automatic conversion between\nstring and number values at run time.\nAny arithmetic operation applied to a string tries to convert\nthis string to a number, following the usual conversion rules.\nConversely, whenever a number is used where a string is expected,\nthe number is converted to a string, in a reasonable format.\nFor complete control over how numbers are converted to strings,\nuse the <code>format</code> function from the string library\n(see <a href=\"#pdf-string.format\"><code>string.format</code></a>).\n\n\n\n\n\n\n\n<h2>2.3 - <a name=\"2.3\">Variables</a></h2>\n\n<p>\nVariables are places that store values.\n\nThere are three kinds of variables in Lua:\nglobal variables, local variables, and table fields.\n\n\n<p>\nA single name can denote a global variable or a local variable\n(or a function's formal parameter,\nwhich is a particular kind of local variable):\n\n<pre>\n\tvar ::= Name\n</pre><p>\nName denotes identifiers, as defined in <a href=\"#2.1\">&sect;2.1</a>.\n\n\n<p>\nAny variable is assumed to be global unless explicitly declared\nas a local (see <a href=\"#2.4.7\">&sect;2.4.7</a>).\nLocal variables are <em>lexically scoped</em>:\nlocal variables can be freely accessed by functions\ndefined inside their scope (see <a href=\"#2.6\">&sect;2.6</a>).\n\n\n<p>\nBefore the first assignment to a variable, its value is <b>nil</b>.\n\n\n<p>\nSquare brackets are used to index a table:\n\n<pre>\n\tvar ::= prefixexp `<b>[</b>&acute; exp `<b>]</b>&acute;\n</pre><p>\nThe meaning of accesses to global variables \nand table fields can be changed via metatables.\nAn access to an indexed variable <code>t[i]</code> is equivalent to\na call <code>gettable_event(t,i)</code>.\n(See <a href=\"#2.8\">&sect;2.8</a> for a complete description of the\n<code>gettable_event</code> function.\nThis function is not defined or callable in Lua.\nWe use it here only for explanatory purposes.)\n\n\n<p>\nThe syntax <code>var.Name</code> is just syntactic sugar for\n<code>var[\"Name\"]</code>:\n\n<pre>\n\tvar ::= prefixexp `<b>.</b>&acute; Name\n</pre>\n\n<p>\nAll global variables live as fields in ordinary Lua tables,\ncalled <em>environment tables</em> or simply\n<em>environments</em> (see <a href=\"#2.9\">&sect;2.9</a>).\nEach function has its own reference to an environment,\nso that all global variables in this function\nwill refer to this environment table.\nWhen a function is created,\nit inherits the environment from the function that created it.\nTo get the environment table of a Lua function,\nyou call <a href=\"#pdf-getfenv\"><code>getfenv</code></a>.\nTo replace it,\nyou call <a href=\"#pdf-setfenv\"><code>setfenv</code></a>.\n(You can only manipulate the environment of C&nbsp;functions\nthrough the debug library; (see <a href=\"#5.9\">&sect;5.9</a>).)\n\n\n<p>\nAn access to a global variable <code>x</code>\nis equivalent to <code>_env.x</code>,\nwhich in turn is equivalent to\n\n<pre>\n     gettable_event(_env, \"x\")\n</pre><p>\nwhere <code>_env</code> is the environment of the running function.\n(See <a href=\"#2.8\">&sect;2.8</a> for a complete description of the\n<code>gettable_event</code> function.\nThis function is not defined or callable in Lua.\nSimilarly, the <code>_env</code> variable is not defined in Lua.\nWe use them here only for explanatory purposes.)\n\n\n\n\n\n<h2>2.4 - <a name=\"2.4\">Statements</a></h2>\n\n<p>\nLua supports an almost conventional set of statements,\nsimilar to those in Pascal or C.\nThis set includes\nassignments, control structures, function calls,\nand variable declarations.\n\n\n\n<h3>2.4.1 - <a name=\"2.4.1\">Chunks</a></h3>\n\n<p>\nThe unit of execution of Lua is called a <em>chunk</em>.\nA chunk is simply a sequence of statements,\nwhich are executed sequentially.\nEach statement can be optionally followed by a semicolon:\n\n<pre>\n\tchunk ::= {stat [`<b>;</b>&acute;]}\n</pre><p>\nThere are no empty statements and thus '<code>;;</code>' is not legal.\n\n\n<p>\nLua handles a chunk as the body of an anonymous function \nwith a variable number of arguments\n(see <a href=\"#2.5.9\">&sect;2.5.9</a>).\nAs such, chunks can define local variables,\nreceive arguments, and return values.\n\n\n<p>\nA chunk can be stored in a file or in a string inside the host program.\nTo execute a chunk,\nLua first pre-compiles the chunk into instructions for a virtual machine,\nand then it executes the compiled code\nwith an interpreter for the virtual machine.\n\n\n<p>\nChunks can also be pre-compiled into binary form;\nsee program <code>luac</code> for details.\nPrograms in source and compiled forms are interchangeable;\nLua automatically detects the file type and acts accordingly.\n\n\n\n\n\n\n<h3>2.4.2 - <a name=\"2.4.2\">Blocks</a></h3><p>\nA block is a list of statements;\nsyntactically, a block is the same as a chunk:\n\n<pre>\n\tblock ::= chunk\n</pre>\n\n<p>\nA block can be explicitly delimited to produce a single statement:\n\n<pre>\n\tstat ::= <b>do</b> block <b>end</b>\n</pre><p>\nExplicit blocks are useful\nto control the scope of variable declarations.\nExplicit blocks are also sometimes used to\nadd a <b>return</b> or <b>break</b> statement in the middle\nof another block (see <a href=\"#2.4.4\">&sect;2.4.4</a>).\n\n\n\n\n\n<h3>2.4.3 - <a name=\"2.4.3\">Assignment</a></h3>\n\n<p>\nLua allows multiple assignments.\nTherefore, the syntax for assignment\ndefines a list of variables on the left side\nand a list of expressions on the right side.\nThe elements in both lists are separated by commas:\n\n<pre>\n\tstat ::= varlist `<b>=</b>&acute; explist\n\tvarlist ::= var {`<b>,</b>&acute; var}\n\texplist ::= exp {`<b>,</b>&acute; exp}\n</pre><p>\nExpressions are discussed in <a href=\"#2.5\">&sect;2.5</a>.\n\n\n<p>\nBefore the assignment,\nthe list of values is <em>adjusted</em> to the length of\nthe list of variables.\nIf there are more values than needed,\nthe excess values are thrown away.\nIf there are fewer values than needed,\nthe list is extended with as many  <b>nil</b>'s as needed.\nIf the list of expressions ends with a function call,\nthen all values returned by that call enter the list of values,\nbefore the adjustment\n(except when the call is enclosed in parentheses; see <a href=\"#2.5\">&sect;2.5</a>).\n\n\n<p>\nThe assignment statement first evaluates all its expressions\nand only then are the assignments performed.\nThus the code\n\n<pre>\n     i = 3\n     i, a[i] = i+1, 20\n</pre><p>\nsets <code>a[3]</code> to 20, without affecting <code>a[4]</code>\nbecause the <code>i</code> in <code>a[i]</code> is evaluated (to 3)\nbefore it is assigned&nbsp;4.\nSimilarly, the line\n\n<pre>\n     x, y = y, x\n</pre><p>\nexchanges the values of <code>x</code> and <code>y</code>,\nand\n\n<pre>\n     x, y, z = y, z, x\n</pre><p>\ncyclically permutes the values of <code>x</code>, <code>y</code>, and <code>z</code>.\n\n\n<p>\nThe meaning of assignments to global variables\nand table fields can be changed via metatables.\nAn assignment to an indexed variable <code>t[i] = val</code> is equivalent to\n<code>settable_event(t,i,val)</code>.\n(See <a href=\"#2.8\">&sect;2.8</a> for a complete description of the\n<code>settable_event</code> function.\nThis function is not defined or callable in Lua.\nWe use it here only for explanatory purposes.)\n\n\n<p>\nAn assignment to a global variable <code>x = val</code>\nis equivalent to the assignment\n<code>_env.x = val</code>,\nwhich in turn is equivalent to\n\n<pre>\n     settable_event(_env, \"x\", val)\n</pre><p>\nwhere <code>_env</code> is the environment of the running function.\n(The <code>_env</code> variable is not defined in Lua.\nWe use it here only for explanatory purposes.)\n\n\n\n\n\n<h3>2.4.4 - <a name=\"2.4.4\">Control Structures</a></h3><p>\nThe control structures\n<b>if</b>, <b>while</b>, and <b>repeat</b> have the usual meaning and\nfamiliar syntax:\n\n\n\n\n<pre>\n\tstat ::= <b>while</b> exp <b>do</b> block <b>end</b>\n\tstat ::= <b>repeat</b> block <b>until</b> exp\n\tstat ::= <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b>\n</pre><p>\nLua also has a <b>for</b> statement, in two flavors (see <a href=\"#2.4.5\">&sect;2.4.5</a>).\n\n\n<p>\nThe condition expression of a\ncontrol structure can return any value.\nBoth <b>false</b> and <b>nil</b> are considered false.\nAll values different from <b>nil</b> and <b>false</b> are considered true\n(in particular, the number 0 and the empty string are also true).\n\n\n<p>\nIn the <b>repeat</b>&ndash;<b>until</b> loop,\nthe inner block does not end at the <b>until</b> keyword,\nbut only after the condition.\nSo, the condition can refer to local variables\ndeclared inside the loop block.\n\n\n<p>\nThe <b>return</b> statement is used to return values\nfrom a function or a chunk (which is just a function).\n\nFunctions and chunks can return more than one value,\nand so the syntax for the <b>return</b> statement is\n\n<pre>\n\tstat ::= <b>return</b> [explist]\n</pre>\n\n<p>\nThe <b>break</b> statement is used to terminate the execution of a\n<b>while</b>, <b>repeat</b>, or <b>for</b> loop,\nskipping to the next statement after the loop:\n\n\n<pre>\n\tstat ::= <b>break</b>\n</pre><p>\nA <b>break</b> ends the innermost enclosing loop.\n\n\n<p>\nThe <b>return</b> and <b>break</b>\nstatements can only be written as the <em>last</em> statement of a block.\nIf it is really necessary to <b>return</b> or <b>break</b> in the\nmiddle of a block,\nthen an explicit inner block can be used,\nas in the idioms\n<code>do return end</code> and <code>do break end</code>,\nbecause now <b>return</b> and <b>break</b> are the last statements in\ntheir (inner) blocks.\n\n\n\n\n\n<h3>2.4.5 - <a name=\"2.4.5\">For Statement</a></h3>\n\n<p>\n\nThe <b>for</b> statement has two forms:\none numeric and one generic.\n\n\n<p>\nThe numeric <b>for</b> loop repeats a block of code while a\ncontrol variable runs through an arithmetic progression.\nIt has the following syntax:\n\n<pre>\n\tstat ::= <b>for</b> Name `<b>=</b>&acute; exp `<b>,</b>&acute; exp [`<b>,</b>&acute; exp] <b>do</b> block <b>end</b>\n</pre><p>\nThe <em>block</em> is repeated for <em>name</em> starting at the value of\nthe first <em>exp</em>, until it passes the second <em>exp</em> by steps of the\nthird <em>exp</em>.\nMore precisely, a <b>for</b> statement like\n\n<pre>\n     for v = <em>e1</em>, <em>e2</em>, <em>e3</em> do <em>block</em> end\n</pre><p>\nis equivalent to the code:\n\n<pre>\n     do\n       local <em>var</em>, <em>limit</em>, <em>step</em> = tonumber(<em>e1</em>), tonumber(<em>e2</em>), tonumber(<em>e3</em>)\n       if not (<em>var</em> and <em>limit</em> and <em>step</em>) then error() end\n       while (<em>step</em> &gt; 0 and <em>var</em> &lt;= <em>limit</em>) or (<em>step</em> &lt;= 0 and <em>var</em> &gt;= <em>limit</em>) do\n         local v = <em>var</em>\n         <em>block</em>\n         <em>var</em> = <em>var</em> + <em>step</em>\n       end\n     end\n</pre><p>\nNote the following:\n\n<ul>\n\n<li>\nAll three control expressions are evaluated only once,\nbefore the loop starts.\nThey must all result in numbers.\n</li>\n\n<li>\n<code><em>var</em></code>, <code><em>limit</em></code>, and <code><em>step</em></code> are invisible variables.\nThe names shown here are for explanatory purposes only.\n</li>\n\n<li>\nIf the third expression (the step) is absent,\nthen a step of&nbsp;1 is used.\n</li>\n\n<li>\nYou can use <b>break</b> to exit a <b>for</b> loop.\n</li>\n\n<li>\nThe loop variable <code>v</code> is local to the loop;\nyou cannot use its value after the <b>for</b> ends or is broken.\nIf you need this value,\nassign it to another variable before breaking or exiting the loop.\n</li>\n\n</ul>\n\n<p>\nThe generic <b>for</b> statement works over functions,\ncalled <em>iterators</em>.\nOn each iteration, the iterator function is called to produce a new value,\nstopping when this new value is <b>nil</b>.\nThe generic <b>for</b> loop has the following syntax:\n\n<pre>\n\tstat ::= <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b>\n\tnamelist ::= Name {`<b>,</b>&acute; Name}\n</pre><p>\nA <b>for</b> statement like\n\n<pre>\n     for <em>var_1</em>, &middot;&middot;&middot;, <em>var_n</em> in <em>explist</em> do <em>block</em> end\n</pre><p>\nis equivalent to the code:\n\n<pre>\n     do\n       local <em>f</em>, <em>s</em>, <em>var</em> = <em>explist</em>\n       while true do\n         local <em>var_1</em>, &middot;&middot;&middot;, <em>var_n</em> = <em>f</em>(<em>s</em>, <em>var</em>)\n         <em>var</em> = <em>var_1</em>\n         if <em>var</em> == nil then break end\n         <em>block</em>\n       end\n     end\n</pre><p>\nNote the following:\n\n<ul>\n\n<li>\n<code><em>explist</em></code> is evaluated only once.\nIts results are an <em>iterator</em> function,\na <em>state</em>,\nand an initial value for the first <em>iterator variable</em>.\n</li>\n\n<li>\n<code><em>f</em></code>, <code><em>s</em></code>, and <code><em>var</em></code> are invisible variables.\nThe names are here for explanatory purposes only.\n</li>\n\n<li>\nYou can use <b>break</b> to exit a <b>for</b> loop.\n</li>\n\n<li>\nThe loop variables <code><em>var_i</em></code> are local to the loop;\nyou cannot use their values after the <b>for</b> ends.\nIf you need these values,\nthen assign them to other variables before breaking or exiting the loop.\n</li>\n\n</ul>\n\n\n\n\n<h3>2.4.6 - <a name=\"2.4.6\">Function Calls as Statements</a></h3><p>\nTo allow possible side-effects,\nfunction calls can be executed as statements:\n\n<pre>\n\tstat ::= functioncall\n</pre><p>\nIn this case, all returned values are thrown away.\nFunction calls are explained in <a href=\"#2.5.8\">&sect;2.5.8</a>.\n\n\n\n\n\n<h3>2.4.7 - <a name=\"2.4.7\">Local Declarations</a></h3><p>\nLocal variables can be declared anywhere inside a block.\nThe declaration can include an initial assignment:\n\n<pre>\n\tstat ::= <b>local</b> namelist [`<b>=</b>&acute; explist]\n</pre><p>\nIf present, an initial assignment has the same semantics\nof a multiple assignment (see <a href=\"#2.4.3\">&sect;2.4.3</a>).\nOtherwise, all variables are initialized with <b>nil</b>.\n\n\n<p>\nA chunk is also a block (see <a href=\"#2.4.1\">&sect;2.4.1</a>),\nand so local variables can be declared in a chunk outside any explicit block.\nThe scope of such local variables extends until the end of the chunk.\n\n\n<p>\nThe visibility rules for local variables are explained in <a href=\"#2.6\">&sect;2.6</a>.\n\n\n\n\n\n\n\n<h2>2.5 - <a name=\"2.5\">Expressions</a></h2>\n\n<p>\nThe basic expressions in Lua are the following:\n\n<pre>\n\texp ::= prefixexp\n\texp ::= <b>nil</b> | <b>false</b> | <b>true</b>\n\texp ::= Number\n\texp ::= String\n\texp ::= function\n\texp ::= tableconstructor\n\texp ::= `<b>...</b>&acute;\n\texp ::= exp binop exp\n\texp ::= unop exp\n\tprefixexp ::= var | functioncall | `<b>(</b>&acute; exp `<b>)</b>&acute;\n</pre>\n\n<p>\nNumbers and literal strings are explained in <a href=\"#2.1\">&sect;2.1</a>;\nvariables are explained in <a href=\"#2.3\">&sect;2.3</a>;\nfunction definitions are explained in <a href=\"#2.5.9\">&sect;2.5.9</a>;\nfunction calls are explained in <a href=\"#2.5.8\">&sect;2.5.8</a>;\ntable constructors are explained in <a href=\"#2.5.7\">&sect;2.5.7</a>.\nVararg expressions,\ndenoted by three dots ('<code>...</code>'), can only be used when\ndirectly inside a vararg function;\nthey are explained in <a href=\"#2.5.9\">&sect;2.5.9</a>.\n\n\n<p>\nBinary operators comprise arithmetic operators (see <a href=\"#2.5.1\">&sect;2.5.1</a>),\nrelational operators (see <a href=\"#2.5.2\">&sect;2.5.2</a>), logical operators (see <a href=\"#2.5.3\">&sect;2.5.3</a>),\nand the concatenation operator (see <a href=\"#2.5.4\">&sect;2.5.4</a>).\nUnary operators comprise the unary minus (see <a href=\"#2.5.1\">&sect;2.5.1</a>),\nthe unary <b>not</b> (see <a href=\"#2.5.3\">&sect;2.5.3</a>),\nand the unary <em>length operator</em> (see <a href=\"#2.5.5\">&sect;2.5.5</a>).\n\n\n<p>\nBoth function calls and vararg expressions can result in multiple values.\nIf an expression is used as a statement\n(only possible for function calls (see <a href=\"#2.4.6\">&sect;2.4.6</a>)),\nthen its return list is adjusted to zero elements,\nthus discarding all returned values.\nIf an expression is used as the last (or the only) element\nof a list of expressions,\nthen no adjustment is made\n(unless the call is enclosed in parentheses).\nIn all other contexts,\nLua adjusts the result list to one element,\ndiscarding all values except the first one.\n\n\n<p>\nHere are some examples:\n\n<pre>\n     f()                -- adjusted to 0 results\n     g(f(), x)          -- f() is adjusted to 1 result\n     g(x, f())          -- g gets x plus all results from f()\n     a,b,c = f(), x     -- f() is adjusted to 1 result (c gets nil)\n     a,b = ...          -- a gets the first vararg parameter, b gets\n                        -- the second (both a and b can get nil if there\n                        -- is no corresponding vararg parameter)\n     \n     a,b,c = x, f()     -- f() is adjusted to 2 results\n     a,b,c = f()        -- f() is adjusted to 3 results\n     return f()         -- returns all results from f()\n     return ...         -- returns all received vararg parameters\n     return x,y,f()     -- returns x, y, and all results from f()\n     {f()}              -- creates a list with all results from f()\n     {...}              -- creates a list with all vararg parameters\n     {f(), nil}         -- f() is adjusted to 1 result\n</pre>\n\n<p>\nAny expression enclosed in parentheses always results in only one value.\nThus,\n<code>(f(x,y,z))</code> is always a single value,\neven if <code>f</code> returns several values.\n(The value of <code>(f(x,y,z))</code> is the first value returned by <code>f</code>\nor <b>nil</b> if <code>f</code> does not return any values.)\n\n\n\n<h3>2.5.1 - <a name=\"2.5.1\">Arithmetic Operators</a></h3><p>\nLua supports the usual arithmetic operators:\nthe binary <code>+</code> (addition),\n<code>-</code> (subtraction), <code>*</code> (multiplication),\n<code>/</code> (division), <code>%</code> (modulo), and <code>^</code> (exponentiation);\nand unary <code>-</code> (negation).\nIf the operands are numbers, or strings that can be converted to\nnumbers (see <a href=\"#2.2.1\">&sect;2.2.1</a>),\nthen all operations have the usual meaning.\nExponentiation works for any exponent.\nFor instance, <code>x^(-0.5)</code> computes the inverse of the square root of <code>x</code>.\nModulo is defined as\n\n<pre>\n     a % b == a - math.floor(a/b)*b\n</pre><p>\nThat is, it is the remainder of a division that rounds\nthe quotient towards minus infinity.\n\n\n\n\n\n<h3>2.5.2 - <a name=\"2.5.2\">Relational Operators</a></h3><p>\nThe relational operators in Lua are\n\n<pre>\n     ==    ~=    &lt;     &gt;     &lt;=    &gt;=\n</pre><p>\nThese operators always result in <b>false</b> or <b>true</b>.\n\n\n<p>\nEquality (<code>==</code>) first compares the type of its operands.\nIf the types are different, then the result is <b>false</b>.\nOtherwise, the values of the operands are compared.\nNumbers and strings are compared in the usual way.\nObjects (tables, userdata, threads, and functions)\nare compared by <em>reference</em>:\ntwo objects are considered equal only if they are the <em>same</em> object.\nEvery time you create a new object\n(a table, userdata, thread, or function),\nthis new object is different from any previously existing object.\n\n\n<p>\nYou can change the way that Lua compares tables and userdata \nby using the \"eq\" metamethod (see <a href=\"#2.8\">&sect;2.8</a>).\n\n\n<p>\nThe conversion rules of <a href=\"#2.2.1\">&sect;2.2.1</a>\n<em>do not</em> apply to equality comparisons.\nThus, <code>\"0\"==0</code> evaluates to <b>false</b>,\nand <code>t[0]</code> and <code>t[\"0\"]</code> denote different\nentries in a table.\n\n\n<p>\nThe operator <code>~=</code> is exactly the negation of equality (<code>==</code>).\n\n\n<p>\nThe order operators work as follows.\nIf both arguments are numbers, then they are compared as such.\nOtherwise, if both arguments are strings,\nthen their values are compared according to the current locale.\nOtherwise, Lua tries to call the \"lt\" or the \"le\"\nmetamethod (see <a href=\"#2.8\">&sect;2.8</a>).\nA comparison <code>a &gt; b</code> is translated to <code>b &lt; a</code>\nand <code>a &gt;= b</code> is translated to <code>b &lt;= a</code>.\n\n\n\n\n\n<h3>2.5.3 - <a name=\"2.5.3\">Logical Operators</a></h3><p>\nThe logical operators in Lua are\n<b>and</b>, <b>or</b>, and <b>not</b>.\nLike the control structures (see <a href=\"#2.4.4\">&sect;2.4.4</a>),\nall logical operators consider both <b>false</b> and <b>nil</b> as false\nand anything else as true.\n\n\n<p>\nThe negation operator <b>not</b> always returns <b>false</b> or <b>true</b>.\nThe conjunction operator <b>and</b> returns its first argument\nif this value is <b>false</b> or <b>nil</b>;\notherwise, <b>and</b> returns its second argument.\nThe disjunction operator <b>or</b> returns its first argument\nif this value is different from <b>nil</b> and <b>false</b>;\notherwise, <b>or</b> returns its second argument.\nBoth <b>and</b> and <b>or</b> use short-cut evaluation;\nthat is,\nthe second operand is evaluated only if necessary.\nHere are some examples:\n\n<pre>\n     10 or 20            --&gt; 10\n     10 or error()       --&gt; 10\n     nil or \"a\"          --&gt; \"a\"\n     nil and 10          --&gt; nil\n     false and error()   --&gt; false\n     false and nil       --&gt; false\n     false or nil        --&gt; nil\n     10 and 20           --&gt; 20\n</pre><p>\n(In this manual,\n<code>--&gt;</code> indicates the result of the preceding expression.)\n\n\n\n\n\n<h3>2.5.4 - <a name=\"2.5.4\">Concatenation</a></h3><p>\nThe string concatenation operator in Lua is\ndenoted by two dots ('<code>..</code>').\nIf both operands are strings or numbers, then they are converted to\nstrings according to the rules mentioned in <a href=\"#2.2.1\">&sect;2.2.1</a>.\nOtherwise, the \"concat\" metamethod is called (see <a href=\"#2.8\">&sect;2.8</a>).\n\n\n\n\n\n<h3>2.5.5 - <a name=\"2.5.5\">The Length Operator</a></h3>\n\n<p>\nThe length operator is denoted by the unary operator <code>#</code>.\nThe length of a string is its number of bytes\n(that is, the usual meaning of string length when each\ncharacter is one byte).\n\n\n<p>\nThe length of a table <code>t</code> is defined to be any\ninteger index <code>n</code>\nsuch that <code>t[n]</code> is not <b>nil</b> and <code>t[n+1]</code> is <b>nil</b>;\nmoreover, if <code>t[1]</code> is <b>nil</b>, <code>n</code> can be zero.\nFor a regular array, with non-nil values from 1 to a given <code>n</code>,\nits length is exactly that <code>n</code>,\nthe index of its last value.\nIf the array has \"holes\"\n(that is, <b>nil</b> values between other non-nil values),\nthen <code>#t</code> can be any of the indices that\ndirectly precedes a <b>nil</b> value\n(that is, it may consider any such <b>nil</b> value as the end of\nthe array). \n\n\n\n\n\n<h3>2.5.6 - <a name=\"2.5.6\">Precedence</a></h3><p>\nOperator precedence in Lua follows the table below,\nfrom lower to higher priority:\n\n<pre>\n     or\n     and\n     &lt;     &gt;     &lt;=    &gt;=    ~=    ==\n     ..\n     +     -\n     *     /     %\n     not   #     - (unary)\n     ^\n</pre><p>\nAs usual,\nyou can use parentheses to change the precedences of an expression.\nThe concatenation ('<code>..</code>') and exponentiation ('<code>^</code>')\noperators are right associative.\nAll other binary operators are left associative.\n\n\n\n\n\n<h3>2.5.7 - <a name=\"2.5.7\">Table Constructors</a></h3><p>\nTable constructors are expressions that create tables.\nEvery time a constructor is evaluated, a new table is created.\nA constructor can be used to create an empty table\nor to create a table and initialize some of its fields.\nThe general syntax for constructors is\n\n<pre>\n\ttableconstructor ::= `<b>{</b>&acute; [fieldlist] `<b>}</b>&acute;\n\tfieldlist ::= field {fieldsep field} [fieldsep]\n\tfield ::= `<b>[</b>&acute; exp `<b>]</b>&acute; `<b>=</b>&acute; exp | Name `<b>=</b>&acute; exp | exp\n\tfieldsep ::= `<b>,</b>&acute; | `<b>;</b>&acute;\n</pre>\n\n<p>\nEach field of the form <code>[exp1] = exp2</code> adds to the new table an entry\nwith key <code>exp1</code> and value <code>exp2</code>.\nA field of the form <code>name = exp</code> is equivalent to\n<code>[\"name\"] = exp</code>.\nFinally, fields of the form <code>exp</code> are equivalent to\n<code>[i] = exp</code>, where <code>i</code> are consecutive numerical integers,\nstarting with 1.\nFields in the other formats do not affect this counting.\nFor example,\n\n<pre>\n     a = { [f(1)] = g; \"x\", \"y\"; x = 1, f(x), [30] = 23; 45 }\n</pre><p>\nis equivalent to\n\n<pre>\n     do\n       local t = {}\n       t[f(1)] = g\n       t[1] = \"x\"         -- 1st exp\n       t[2] = \"y\"         -- 2nd exp\n       t.x = 1            -- t[\"x\"] = 1\n       t[3] = f(x)        -- 3rd exp\n       t[30] = 23\n       t[4] = 45          -- 4th exp\n       a = t\n     end\n</pre>\n\n<p>\nIf the last field in the list has the form <code>exp</code>\nand the expression is a function call or a vararg expression,\nthen all values returned by this expression enter the list consecutively\n(see <a href=\"#2.5.8\">&sect;2.5.8</a>).\nTo avoid this,\nenclose the function call or the vararg expression\nin parentheses (see <a href=\"#2.5\">&sect;2.5</a>).\n\n\n<p>\nThe field list can have an optional trailing separator,\nas a convenience for machine-generated code.\n\n\n\n\n\n<h3>2.5.8 - <a name=\"2.5.8\">Function Calls</a></h3><p>\nA function call in Lua has the following syntax:\n\n<pre>\n\tfunctioncall ::= prefixexp args\n</pre><p>\nIn a function call,\nfirst prefixexp and args are evaluated.\nIf the value of prefixexp has type <em>function</em>,\nthen this function is called\nwith the given arguments.\nOtherwise, the prefixexp \"call\" metamethod is called,\nhaving as first parameter the value of prefixexp,\nfollowed by the original call arguments\n(see <a href=\"#2.8\">&sect;2.8</a>).\n\n\n<p>\nThe form\n\n<pre>\n\tfunctioncall ::= prefixexp `<b>:</b>&acute; Name args\n</pre><p>\ncan be used to call \"methods\".\nA call <code>v:name(<em>args</em>)</code>\nis syntactic sugar for <code>v.name(v,<em>args</em>)</code>,\nexcept that <code>v</code> is evaluated only once.\n\n\n<p>\nArguments have the following syntax:\n\n<pre>\n\targs ::= `<b>(</b>&acute; [explist] `<b>)</b>&acute;\n\targs ::= tableconstructor\n\targs ::= String\n</pre><p>\nAll argument expressions are evaluated before the call.\nA call of the form <code>f{<em>fields</em>}</code> is\nsyntactic sugar for <code>f({<em>fields</em>})</code>;\nthat is, the argument list is a single new table.\nA call of the form <code>f'<em>string</em>'</code>\n(or <code>f\"<em>string</em>\"</code> or <code>f[[<em>string</em>]]</code>)\nis syntactic sugar for <code>f('<em>string</em>')</code>;\nthat is, the argument list is a single literal string.\n\n\n<p>\nAs an exception to the free-format syntax of Lua,\nyou cannot put a line break before the '<code>(</code>' in a function call.\nThis restriction avoids some ambiguities in the language.\nIf you write\n\n<pre>\n     a = f\n     (g).x(a)\n</pre><p>\nLua would see that as a single statement, <code>a = f(g).x(a)</code>.\nSo, if you want two statements, you must add a semi-colon between them.\nIf you actually want to call <code>f</code>,\nyou must remove the line break before <code>(g)</code>.\n\n\n<p>\nA call of the form <code>return</code> <em>functioncall</em> is called\na <em>tail call</em>.\nLua implements <em>proper tail calls</em>\n(or <em>proper tail recursion</em>):\nin a tail call,\nthe called function reuses the stack entry of the calling function.\nTherefore, there is no limit on the number of nested tail calls that\na program can execute.\nHowever, a tail call erases any debug information about the\ncalling function.\nNote that a tail call only happens with a particular syntax,\nwhere the <b>return</b> has one single function call as argument;\nthis syntax makes the calling function return exactly\nthe returns of the called function.\nSo, none of the following examples are tail calls:\n\n<pre>\n     return (f(x))        -- results adjusted to 1\n     return 2 * f(x)\n     return x, f(x)       -- additional results\n     f(x); return         -- results discarded\n     return x or f(x)     -- results adjusted to 1\n</pre>\n\n\n\n\n<h3>2.5.9 - <a name=\"2.5.9\">Function Definitions</a></h3>\n\n<p>\nThe syntax for function definition is\n\n<pre>\n\tfunction ::= <b>function</b> funcbody\n\tfuncbody ::= `<b>(</b>&acute; [parlist] `<b>)</b>&acute; block <b>end</b>\n</pre>\n\n<p>\nThe following syntactic sugar simplifies function definitions:\n\n<pre>\n\tstat ::= <b>function</b> funcname funcbody\n\tstat ::= <b>local</b> <b>function</b> Name funcbody\n\tfuncname ::= Name {`<b>.</b>&acute; Name} [`<b>:</b>&acute; Name]\n</pre><p>\nThe statement\n\n<pre>\n     function f () <em>body</em> end\n</pre><p>\ntranslates to\n\n<pre>\n     f = function () <em>body</em> end\n</pre><p>\nThe statement\n\n<pre>\n     function t.a.b.c.f () <em>body</em> end\n</pre><p>\ntranslates to\n\n<pre>\n     t.a.b.c.f = function () <em>body</em> end\n</pre><p>\nThe statement\n\n<pre>\n     local function f () <em>body</em> end\n</pre><p>\ntranslates to\n\n<pre>\n     local f; f = function () <em>body</em> end\n</pre><p>\n<em>not</em> to\n\n<pre>\n     local f = function () <em>body</em> end\n</pre><p>\n(This only makes a difference when the body of the function\ncontains references to <code>f</code>.)\n\n\n<p>\nA function definition is an executable expression,\nwhose value has type <em>function</em>.\nWhen Lua pre-compiles a chunk,\nall its function bodies are pre-compiled too.\nThen, whenever Lua executes the function definition,\nthe function is <em>instantiated</em> (or <em>closed</em>).\nThis function instance (or <em>closure</em>)\nis the final value of the expression.\nDifferent instances of the same function\ncan refer to different  external local variables\nand can have different environment tables.\n\n\n<p>\nParameters act as local variables that are\ninitialized with the argument values:\n\n<pre>\n\tparlist ::= namelist [`<b>,</b>&acute; `<b>...</b>&acute;] | `<b>...</b>&acute;\n</pre><p>\nWhen a function is called,\nthe list of arguments is adjusted to\nthe length of the list of parameters,\nunless the function is a variadic or <em>vararg function</em>,\nwhich is\nindicated by three dots ('<code>...</code>') at the end of its parameter list.\nA vararg function does not adjust its argument list;\ninstead, it collects all extra arguments and supplies them\nto the function through a <em>vararg expression</em>,\nwhich is also written as three dots.\nThe value of this expression is a list of all actual extra arguments,\nsimilar to a function with multiple results.\nIf a vararg expression is used inside another expression\nor in the middle of a list of expressions,\nthen its return list is adjusted to one element.\nIf the expression is used as the last element of a list of expressions,\nthen no adjustment is made\n(unless that last expression is enclosed in parentheses).\n\n\n<p>\nAs an example, consider the following definitions:\n\n<pre>\n     function f(a, b) end\n     function g(a, b, ...) end\n     function r() return 1,2,3 end\n</pre><p>\nThen, we have the following mapping from arguments to parameters and\nto the vararg expression:\n\n<pre>\n     CALL            PARAMETERS\n     \n     f(3)             a=3, b=nil\n     f(3, 4)          a=3, b=4\n     f(3, 4, 5)       a=3, b=4\n     f(r(), 10)       a=1, b=10\n     f(r())           a=1, b=2\n     \n     g(3)             a=3, b=nil, ... --&gt;  (nothing)\n     g(3, 4)          a=3, b=4,   ... --&gt;  (nothing)\n     g(3, 4, 5, 8)    a=3, b=4,   ... --&gt;  5  8\n     g(5, r())        a=5, b=1,   ... --&gt;  2  3\n</pre>\n\n<p>\nResults are returned using the <b>return</b> statement (see <a href=\"#2.4.4\">&sect;2.4.4</a>).\nIf control reaches the end of a function\nwithout encountering a <b>return</b> statement,\nthen the function returns with no results.\n\n\n<p>\nThe <em>colon</em> syntax\nis used for defining <em>methods</em>,\nthat is, functions that have an implicit extra parameter <code>self</code>.\nThus, the statement\n\n<pre>\n     function t.a.b.c:f (<em>params</em>) <em>body</em> end\n</pre><p>\nis syntactic sugar for\n\n<pre>\n     t.a.b.c.f = function (self, <em>params</em>) <em>body</em> end\n</pre>\n\n\n\n\n\n\n<h2>2.6 - <a name=\"2.6\">Visibility Rules</a></h2>\n\n<p>\n\nLua is a lexically scoped language.\nThe scope of variables begins at the first statement <em>after</em>\ntheir declaration and lasts until the end of the innermost block that\nincludes the declaration.\nConsider the following example:\n\n<pre>\n     x = 10                -- global variable\n     do                    -- new block\n       local x = x         -- new 'x', with value 10\n       print(x)            --&gt; 10\n       x = x+1\n       do                  -- another block\n         local x = x+1     -- another 'x'\n         print(x)          --&gt; 12\n       end\n       print(x)            --&gt; 11\n     end\n     print(x)              --&gt; 10  (the global one)\n</pre>\n\n<p>\nNotice that, in a declaration like <code>local x = x</code>,\nthe new <code>x</code> being declared is not in scope yet,\nand so the second <code>x</code> refers to the outside variable.\n\n\n<p>\nBecause of the lexical scoping rules,\nlocal variables can be freely accessed by functions\ndefined inside their scope.\nA local variable used by an inner function is called\nan <em>upvalue</em>, or <em>external local variable</em>,\ninside the inner function.\n\n\n<p>\nNotice that each execution of a <b>local</b> statement\ndefines new local variables.\nConsider the following example:\n\n<pre>\n     a = {}\n     local x = 20\n     for i=1,10 do\n       local y = 0\n       a[i] = function () y=y+1; return x+y end\n     end\n</pre><p>\nThe loop creates ten closures\n(that is, ten instances of the anonymous function).\nEach of these closures uses a different <code>y</code> variable,\nwhile all of them share the same <code>x</code>.\n\n\n\n\n\n<h2>2.7 - <a name=\"2.7\">Error Handling</a></h2>\n\n<p>\nBecause Lua is an embedded extension language,\nall Lua actions start from C&nbsp;code in the host program\ncalling a function from the Lua library (see <a href=\"#lua_pcall\"><code>lua_pcall</code></a>).\nWhenever an error occurs during Lua compilation or execution,\ncontrol returns to C,\nwhich can take appropriate measures\n(such as printing an error message).\n\n\n<p>\nLua code can explicitly generate an error by calling the\n<a href=\"#pdf-error\"><code>error</code></a> function.\nIf you need to catch errors in Lua,\nyou can use the <a href=\"#pdf-pcall\"><code>pcall</code></a> function.\n\n\n\n\n\n<h2>2.8 - <a name=\"2.8\">Metatables</a></h2>\n\n<p>\nEvery value in Lua can have a <em>metatable</em>.\nThis <em>metatable</em> is an ordinary Lua table\nthat defines the behavior of the original value\nunder certain special operations.\nYou can change several aspects of the behavior\nof operations over a value by setting specific fields in its metatable.\nFor instance, when a non-numeric value is the operand of an addition,\nLua checks for a function in the field <code>\"__add\"</code> in its metatable.\nIf it finds one,\nLua calls this function to perform the addition.\n\n\n<p>\nWe call the keys in a metatable <em>events</em>\nand the values <em>metamethods</em>.\nIn the previous example, the event is <code>\"add\"</code> \nand the metamethod is the function that performs the addition.\n\n\n<p>\nYou can query the metatable of any value\nthrough the <a href=\"#pdf-getmetatable\"><code>getmetatable</code></a> function.\n\n\n<p>\nYou can replace the metatable of tables\nthrough the <a href=\"#pdf-setmetatable\"><code>setmetatable</code></a>\nfunction.\nYou cannot change the metatable of other types from Lua\n(except by using the debug library);\nyou must use the C&nbsp;API for that.\n\n\n<p>\nTables and full userdata have individual metatables\n(although multiple tables and userdata can share their metatables).\nValues of all other types share one single metatable per type;\nthat is, there is one single metatable for all numbers,\none for all strings, etc.\n\n\n<p>\nA metatable controls how an object behaves in arithmetic operations,\norder comparisons, concatenation, length operation, and indexing.\nA metatable also can define a function to be called when a userdata\nis garbage collected.\nFor each of these operations Lua associates a specific key\ncalled an <em>event</em>.\nWhen Lua performs one of these operations over a value,\nit checks whether this value has a metatable with the corresponding event.\nIf so, the value associated with that key (the metamethod)\ncontrols how Lua will perform the operation.\n\n\n<p>\nMetatables control the operations listed next.\nEach operation is identified by its corresponding name.\nThe key for each operation is a string with its name prefixed by\ntwo underscores, '<code>__</code>';\nfor instance, the key for operation \"add\" is the\nstring <code>\"__add\"</code>.\nThe semantics of these operations is better explained by a Lua function\ndescribing how the interpreter executes the operation.\n\n\n<p>\nThe code shown here in Lua is only illustrative;\nthe real behavior is hard coded in the interpreter\nand it is much more efficient than this simulation.\nAll functions used in these descriptions\n(<a href=\"#pdf-rawget\"><code>rawget</code></a>, <a href=\"#pdf-tonumber\"><code>tonumber</code></a>, etc.)\nare described in <a href=\"#5.1\">&sect;5.1</a>.\nIn particular, to retrieve the metamethod of a given object,\nwe use the expression\n\n<pre>\n     metatable(obj)[event]\n</pre><p>\nThis should be read as\n\n<pre>\n     rawget(getmetatable(obj) or {}, event)\n</pre><p>\n\nThat is, the access to a metamethod does not invoke other metamethods,\nand the access to objects with no metatables does not fail\n(it simply results in <b>nil</b>).\n\n\n\n<ul>\n\n<li><b>\"add\":</b>\nthe <code>+</code> operation.\n\n\n\n<p>\nThe function <code>getbinhandler</code> below defines how Lua chooses a handler\nfor a binary operation.\nFirst, Lua tries the first operand.\nIf its type does not define a handler for the operation,\nthen Lua tries the second operand.\n\n<pre>\n     function getbinhandler (op1, op2, event)\n       return metatable(op1)[event] or metatable(op2)[event]\n     end\n</pre><p>\nBy using this function,\nthe behavior of the <code>op1 + op2</code> is\n\n<pre>\n     function add_event (op1, op2)\n       local o1, o2 = tonumber(op1), tonumber(op2)\n       if o1 and o2 then  -- both operands are numeric?\n         return o1 + o2   -- '+' here is the primitive 'add'\n       else  -- at least one of the operands is not numeric\n         local h = getbinhandler(op1, op2, \"__add\")\n         if h then\n           -- call the handler with both operands\n           return (h(op1, op2))\n         else  -- no handler available: default behavior\n           error(&middot;&middot;&middot;)\n         end\n       end\n     end\n</pre><p>\n</li>\n\n<li><b>\"sub\":</b>\nthe <code>-</code> operation.\n\nBehavior similar to the \"add\" operation.\n</li>\n\n<li><b>\"mul\":</b>\nthe <code>*</code> operation.\n\nBehavior similar to the \"add\" operation.\n</li>\n\n<li><b>\"div\":</b>\nthe <code>/</code> operation.\n\nBehavior similar to the \"add\" operation.\n</li>\n\n<li><b>\"mod\":</b>\nthe <code>%</code> operation.\n\nBehavior similar to the \"add\" operation,\nwith the operation\n<code>o1 - floor(o1/o2)*o2</code> as the primitive operation.\n</li>\n\n<li><b>\"pow\":</b>\nthe <code>^</code> (exponentiation) operation.\n\nBehavior similar to the \"add\" operation,\nwith the function <code>pow</code> (from the C&nbsp;math library)\nas the primitive operation.\n</li>\n\n<li><b>\"unm\":</b>\nthe unary <code>-</code> operation.\n\n\n<pre>\n     function unm_event (op)\n       local o = tonumber(op)\n       if o then  -- operand is numeric?\n         return -o  -- '-' here is the primitive 'unm'\n       else  -- the operand is not numeric.\n         -- Try to get a handler from the operand\n         local h = metatable(op).__unm\n         if h then\n           -- call the handler with the operand\n           return (h(op))\n         else  -- no handler available: default behavior\n           error(&middot;&middot;&middot;)\n         end\n       end\n     end\n</pre><p>\n</li>\n\n<li><b>\"concat\":</b>\nthe <code>..</code> (concatenation) operation.\n\n\n<pre>\n     function concat_event (op1, op2)\n       if (type(op1) == \"string\" or type(op1) == \"number\") and\n          (type(op2) == \"string\" or type(op2) == \"number\") then\n         return op1 .. op2  -- primitive string concatenation\n       else\n         local h = getbinhandler(op1, op2, \"__concat\")\n         if h then\n           return (h(op1, op2))\n         else\n           error(&middot;&middot;&middot;)\n         end\n       end\n     end\n</pre><p>\n</li>\n\n<li><b>\"len\":</b>\nthe <code>#</code> operation.\n\n\n<pre>\n     function len_event (op)\n       if type(op) == \"string\" then\n         return strlen(op)         -- primitive string length\n       elseif type(op) == \"table\" then\n         return #op                -- primitive table length\n       else\n         local h = metatable(op).__len\n         if h then\n           -- call the handler with the operand\n           return (h(op))\n         else  -- no handler available: default behavior\n           error(&middot;&middot;&middot;)\n         end\n       end\n     end\n</pre><p>\nSee <a href=\"#2.5.5\">&sect;2.5.5</a> for a description of the length of a table.\n</li>\n\n<li><b>\"eq\":</b>\nthe <code>==</code> operation.\n\nThe function <code>getcomphandler</code> defines how Lua chooses a metamethod\nfor comparison operators.\nA metamethod only is selected when both objects\nbeing compared have the same type\nand the same metamethod for the selected operation.\n\n<pre>\n     function getcomphandler (op1, op2, event)\n       if type(op1) ~= type(op2) then return nil end\n       local mm1 = metatable(op1)[event]\n       local mm2 = metatable(op2)[event]\n       if mm1 == mm2 then return mm1 else return nil end\n     end\n</pre><p>\nThe \"eq\" event is defined as follows:\n\n<pre>\n     function eq_event (op1, op2)\n       if type(op1) ~= type(op2) then  -- different types?\n         return false   -- different objects\n       end\n       if op1 == op2 then   -- primitive equal?\n         return true   -- objects are equal\n       end\n       -- try metamethod\n       local h = getcomphandler(op1, op2, \"__eq\")\n       if h then\n         return (h(op1, op2))\n       else\n         return false\n       end\n     end\n</pre><p>\n<code>a ~= b</code> is equivalent to <code>not (a == b)</code>.\n</li>\n\n<li><b>\"lt\":</b>\nthe <code>&lt;</code> operation.\n\n\n<pre>\n     function lt_event (op1, op2)\n       if type(op1) == \"number\" and type(op2) == \"number\" then\n         return op1 &lt; op2   -- numeric comparison\n       elseif type(op1) == \"string\" and type(op2) == \"string\" then\n         return op1 &lt; op2   -- lexicographic comparison\n       else\n         local h = getcomphandler(op1, op2, \"__lt\")\n         if h then\n           return (h(op1, op2))\n         else\n           error(&middot;&middot;&middot;)\n         end\n       end\n     end\n</pre><p>\n<code>a &gt; b</code> is equivalent to <code>b &lt; a</code>.\n</li>\n\n<li><b>\"le\":</b>\nthe <code>&lt;=</code> operation.\n\n\n<pre>\n     function le_event (op1, op2)\n       if type(op1) == \"number\" and type(op2) == \"number\" then\n         return op1 &lt;= op2   -- numeric comparison\n       elseif type(op1) == \"string\" and type(op2) == \"string\" then\n         return op1 &lt;= op2   -- lexicographic comparison\n       else\n         local h = getcomphandler(op1, op2, \"__le\")\n         if h then\n           return (h(op1, op2))\n         else\n           h = getcomphandler(op1, op2, \"__lt\")\n           if h then\n             return not h(op2, op1)\n           else\n             error(&middot;&middot;&middot;)\n           end\n         end\n       end\n     end\n</pre><p>\n<code>a &gt;= b</code> is equivalent to <code>b &lt;= a</code>.\nNote that, in the absence of a \"le\" metamethod,\nLua tries the \"lt\", assuming that <code>a &lt;= b</code> is\nequivalent to <code>not (b &lt; a)</code>.\n</li>\n\n<li><b>\"index\":</b>\nThe indexing access <code>table[key]</code>.\n\n\n<pre>\n     function gettable_event (table, key)\n       local h\n       if type(table) == \"table\" then\n         local v = rawget(table, key)\n         if v ~= nil then return v end\n         h = metatable(table).__index\n         if h == nil then return nil end\n       else\n         h = metatable(table).__index\n         if h == nil then\n           error(&middot;&middot;&middot;)\n         end\n       end\n       if type(h) == \"function\" then\n         return (h(table, key))     -- call the handler\n       else return h[key]           -- or repeat operation on it\n       end\n     end\n</pre><p>\n</li>\n\n<li><b>\"newindex\":</b>\nThe indexing assignment <code>table[key] = value</code>.\n\n\n<pre>\n     function settable_event (table, key, value)\n       local h\n       if type(table) == \"table\" then\n         local v = rawget(table, key)\n         if v ~= nil then rawset(table, key, value); return end\n         h = metatable(table).__newindex\n         if h == nil then rawset(table, key, value); return end\n       else\n         h = metatable(table).__newindex\n         if h == nil then\n           error(&middot;&middot;&middot;)\n         end\n       end\n       if type(h) == \"function\" then\n         h(table, key,value)           -- call the handler\n       else h[key] = value             -- or repeat operation on it\n       end\n     end\n</pre><p>\n</li>\n\n<li><b>\"call\":</b>\ncalled when Lua calls a value.\n\n\n<pre>\n     function function_event (func, ...)\n       if type(func) == \"function\" then\n         return func(...)   -- primitive call\n       else\n         local h = metatable(func).__call\n         if h then\n           return h(func, ...)\n         else\n           error(&middot;&middot;&middot;)\n         end\n       end\n     end\n</pre><p>\n</li>\n\n</ul>\n\n\n\n\n<h2>2.9 - <a name=\"2.9\">Environments</a></h2>\n\n<p>\nBesides metatables,\nobjects of types thread, function, and userdata\nhave another table associated with them,\ncalled their <em>environment</em>.\nLike metatables, environments are regular tables and\nmultiple objects can share the same environment.\n\n\n<p>\nThreads are created sharing the environment of the creating thread.\nUserdata and C&nbsp;functions are created sharing the environment\nof the creating C&nbsp;function.\nNon-nested Lua functions\n(created by <a href=\"#pdf-loadfile\"><code>loadfile</code></a>, <a href=\"#pdf-loadstring\"><code>loadstring</code></a> or <a href=\"#pdf-load\"><code>load</code></a>)\nare created sharing the environment of the creating thread.\nNested Lua functions are created sharing the environment of\nthe creating Lua function.\n\n\n<p>\nEnvironments associated with userdata have no meaning for Lua.\nIt is only a convenience feature for programmers to associate a table to\na userdata.\n\n\n<p>\nEnvironments associated with threads are called\n<em>global environments</em>.\nThey are used as the default environment for threads and\nnon-nested Lua functions created by the thread\nand can be directly accessed by C&nbsp;code (see <a href=\"#3.3\">&sect;3.3</a>).\n\n\n<p>\nThe environment associated with a C&nbsp;function can be directly\naccessed by C&nbsp;code (see <a href=\"#3.3\">&sect;3.3</a>).\nIt is used as the default environment for other C&nbsp;functions\nand userdata created by the function.\n\n\n<p>\nEnvironments associated with Lua functions are used to resolve\nall accesses to global variables within the function (see <a href=\"#2.3\">&sect;2.3</a>).\nThey are used as the default environment for nested Lua functions\ncreated by the function.\n\n\n<p>\nYou can change the environment of a Lua function or the\nrunning thread by calling <a href=\"#pdf-setfenv\"><code>setfenv</code></a>.\nYou can get the environment of a Lua function or the running thread\nby calling <a href=\"#pdf-getfenv\"><code>getfenv</code></a>.\nTo manipulate the environment of other objects\n(userdata, C&nbsp;functions, other threads) you must\nuse the C&nbsp;API.\n\n\n\n\n\n<h2>2.10 - <a name=\"2.10\">Garbage Collection</a></h2>\n\n<p>\nLua performs automatic memory management.\nThis means that\nyou have to worry neither about allocating memory for new objects\nnor about freeing it when the objects are no longer needed.\nLua manages memory automatically by running\na <em>garbage collector</em> from time to time\nto collect all <em>dead objects</em>\n(that is, objects that are no longer accessible from Lua).\nAll memory used by Lua is subject to automatic management:\ntables, userdata, functions, threads, strings, etc.\n\n\n<p>\nLua implements an incremental mark-and-sweep collector.\nIt uses two numbers to control its garbage-collection cycles:\nthe <em>garbage-collector pause</em> and\nthe <em>garbage-collector step multiplier</em>.\nBoth use percentage points as units\n(so that a value of 100 means an internal value of 1).\n\n\n<p>\nThe garbage-collector pause\ncontrols how long the collector waits before starting a new cycle.\nLarger values make the collector less aggressive.\nValues smaller than 100 mean the collector will not wait to\nstart a new cycle.\nA value of 200 means that the collector waits for the total memory in use\nto double before starting a new cycle.\n\n\n<p>\nThe step multiplier\ncontrols the relative speed of the collector relative to\nmemory allocation.\nLarger values make the collector more aggressive but also increase\nthe size of each incremental step.\nValues smaller than 100 make the collector too slow and\ncan result in the collector never finishing a cycle.\nThe default, 200, means that the collector runs at \"twice\"\nthe speed of memory allocation.\n\n\n<p>\nYou can change these numbers by calling <a href=\"#lua_gc\"><code>lua_gc</code></a> in C\nor <a href=\"#pdf-collectgarbage\"><code>collectgarbage</code></a> in Lua.\nWith these functions you can also control \nthe collector directly (e.g., stop and restart it).\n\n\n\n<h3>2.10.1 - <a name=\"2.10.1\">Garbage-Collection Metamethods</a></h3>\n\n<p>\nUsing the C&nbsp;API,\nyou can set garbage-collector metamethods for userdata (see <a href=\"#2.8\">&sect;2.8</a>).\nThese metamethods are also called <em>finalizers</em>.\nFinalizers allow you to coordinate Lua's garbage collection\nwith external resource management\n(such as closing files, network or database connections,\nor freeing your own memory).\n\n\n<p>\nGarbage userdata with a field <code>__gc</code> in their metatables are not\ncollected immediately by the garbage collector.\nInstead, Lua puts them in a list.\nAfter the collection,\nLua does the equivalent of the following function\nfor each userdata in that list:\n\n<pre>\n     function gc_event (udata)\n       local h = metatable(udata).__gc\n       if h then\n         h(udata)\n       end\n     end\n</pre>\n\n<p>\nAt the end of each garbage-collection cycle,\nthe finalizers for userdata are called in <em>reverse</em>\norder of their creation,\namong those collected in that cycle.\nThat is, the first finalizer to be called is the one associated\nwith the userdata created last in the program.\nThe userdata itself is freed only in the next garbage-collection cycle.\n\n\n\n\n\n<h3>2.10.2 - <a name=\"2.10.2\">Weak Tables</a></h3>\n\n<p>\nA <em>weak table</em> is a table whose elements are\n<em>weak references</em>.\nA weak reference is ignored by the garbage collector.\nIn other words,\nif the only references to an object are weak references,\nthen the garbage collector will collect this object.\n\n\n<p>\nA weak table can have weak keys, weak values, or both.\nA table with weak keys allows the collection of its keys,\nbut prevents the collection of its values.\nA table with both weak keys and weak values allows the collection of\nboth keys and values.\nIn any case, if either the key or the value is collected,\nthe whole pair is removed from the table.\nThe weakness of a table is controlled by the\n<code>__mode</code> field of its metatable.\nIf the <code>__mode</code> field is a string containing the character&nbsp;'<code>k</code>',\nthe keys in the table are weak.\nIf <code>__mode</code> contains '<code>v</code>',\nthe values in the table are weak.\n\n\n<p>\nAfter you use a table as a metatable,\nyou should not change the value of its <code>__mode</code> field.\nOtherwise, the weak behavior of the tables controlled by this\nmetatable is undefined.\n\n\n\n\n\n\n\n<h2>2.11 - <a name=\"2.11\">Coroutines</a></h2>\n\n<p>\nLua supports coroutines,\nalso called <em>collaborative multithreading</em>.\nA coroutine in Lua represents an independent thread of execution.\nUnlike threads in multithread systems, however,\na coroutine only suspends its execution by explicitly calling\na yield function.\n\n\n<p>\nYou create a coroutine with a call to <a href=\"#pdf-coroutine.create\"><code>coroutine.create</code></a>.\nIts sole argument is a function\nthat is the main function of the coroutine.\nThe <code>create</code> function only creates a new coroutine and\nreturns a handle to it (an object of type <em>thread</em>);\nit does not start the coroutine execution.\n\n\n<p>\nWhen you first call <a href=\"#pdf-coroutine.resume\"><code>coroutine.resume</code></a>,\npassing as its first argument\na thread returned by <a href=\"#pdf-coroutine.create\"><code>coroutine.create</code></a>,\nthe coroutine starts its execution,\nat the first line of its main function.\nExtra arguments passed to <a href=\"#pdf-coroutine.resume\"><code>coroutine.resume</code></a> are passed on\nto the coroutine main function.\nAfter the coroutine starts running,\nit runs until it terminates or <em>yields</em>.\n\n\n<p>\nA coroutine can terminate its execution in two ways:\nnormally, when its main function returns\n(explicitly or implicitly, after the last instruction);\nand abnormally, if there is an unprotected error.\nIn the first case, <a href=\"#pdf-coroutine.resume\"><code>coroutine.resume</code></a> returns <b>true</b>,\nplus any values returned by the coroutine main function.\nIn case of errors, <a href=\"#pdf-coroutine.resume\"><code>coroutine.resume</code></a> returns <b>false</b>\nplus an error message.\n\n\n<p>\nA coroutine yields by calling <a href=\"#pdf-coroutine.yield\"><code>coroutine.yield</code></a>.\nWhen a coroutine yields,\nthe corresponding <a href=\"#pdf-coroutine.resume\"><code>coroutine.resume</code></a> returns immediately,\neven if the yield happens inside nested function calls\n(that is, not in the main function,\nbut in a function directly or indirectly called by the main function).\nIn the case of a yield, <a href=\"#pdf-coroutine.resume\"><code>coroutine.resume</code></a> also returns <b>true</b>,\nplus any values passed to <a href=\"#pdf-coroutine.yield\"><code>coroutine.yield</code></a>.\nThe next time you resume the same coroutine,\nit continues its execution from the point where it yielded,\nwith the call to <a href=\"#pdf-coroutine.yield\"><code>coroutine.yield</code></a> returning any extra\narguments passed to <a href=\"#pdf-coroutine.resume\"><code>coroutine.resume</code></a>.\n\n\n<p>\nLike <a href=\"#pdf-coroutine.create\"><code>coroutine.create</code></a>,\nthe <a href=\"#pdf-coroutine.wrap\"><code>coroutine.wrap</code></a> function also creates a coroutine,\nbut instead of returning the coroutine itself,\nit returns a function that, when called, resumes the coroutine.\nAny arguments passed to this function\ngo as extra arguments to <a href=\"#pdf-coroutine.resume\"><code>coroutine.resume</code></a>.\n<a href=\"#pdf-coroutine.wrap\"><code>coroutine.wrap</code></a> returns all the values returned by <a href=\"#pdf-coroutine.resume\"><code>coroutine.resume</code></a>,\nexcept the first one (the boolean error code).\nUnlike <a href=\"#pdf-coroutine.resume\"><code>coroutine.resume</code></a>,\n<a href=\"#pdf-coroutine.wrap\"><code>coroutine.wrap</code></a> does not catch errors;\nany error is propagated to the caller.\n\n\n<p>\nAs an example,\nconsider the following code:\n\n<pre>\n     function foo (a)\n       print(\"foo\", a)\n       return coroutine.yield(2*a)\n     end\n     \n     co = coroutine.create(function (a,b)\n           print(\"co-body\", a, b)\n           local r = foo(a+1)\n           print(\"co-body\", r)\n           local r, s = coroutine.yield(a+b, a-b)\n           print(\"co-body\", r, s)\n           return b, \"end\"\n     end)\n            \n     print(\"main\", coroutine.resume(co, 1, 10))\n     print(\"main\", coroutine.resume(co, \"r\"))\n     print(\"main\", coroutine.resume(co, \"x\", \"y\"))\n     print(\"main\", coroutine.resume(co, \"x\", \"y\"))\n</pre><p>\nWhen you run it, it produces the following output:\n\n<pre>\n     co-body 1       10\n     foo     2\n     \n     main    true    4\n     co-body r\n     main    true    11      -9\n     co-body x       y\n     main    true    10      end\n     main    false   cannot resume dead coroutine\n</pre>\n\n\n\n\n<h1>3 - <a name=\"3\">The Application Program Interface</a></h1>\n\n<p>\n\nThis section describes the C&nbsp;API for Lua, that is,\nthe set of C&nbsp;functions available to the host program to communicate\nwith Lua.\nAll API functions and related types and constants\nare declared in the header file <a name=\"pdf-lua.h\"><code>lua.h</code></a>.\n\n\n<p>\nEven when we use the term \"function\",\nany facility in the API may be provided as a macro instead.\nAll such macros use each of their arguments exactly once\n(except for the first argument, which is always a Lua state),\nand so do not generate any hidden side-effects.\n\n\n<p>\nAs in most C&nbsp;libraries,\nthe Lua API functions do not check their arguments for validity or consistency.\nHowever, you can change this behavior by compiling Lua\nwith a proper definition for the macro <a name=\"pdf-luai_apicheck\"><code>luai_apicheck</code></a>,\nin file <code>luaconf.h</code>.\n\n\n\n<h2>3.1 - <a name=\"3.1\">The Stack</a></h2>\n\n<p>\nLua uses a <em>virtual stack</em> to pass values to and from C.\nEach element in this stack represents a Lua value\n(<b>nil</b>, number, string, etc.).\n\n\n<p>\nWhenever Lua calls C, the called function gets a new stack,\nwhich is independent of previous stacks and of stacks of\nC&nbsp;functions that are still active.\nThis stack initially contains any arguments to the C&nbsp;function\nand it is where the C&nbsp;function pushes its results\nto be returned to the caller (see <a href=\"#lua_CFunction\"><code>lua_CFunction</code></a>).\n\n\n<p>\nFor convenience,\nmost query operations in the API do not follow a strict stack discipline.\nInstead, they can refer to any element in the stack\nby using an <em>index</em>:\nA positive index represents an <em>absolute</em> stack position\n(starting at&nbsp;1);\na negative index represents an <em>offset</em> relative to the top of the stack.\nMore specifically, if the stack has <em>n</em> elements,\nthen index&nbsp;1 represents the first element\n(that is, the element that was pushed onto the stack first)\nand\nindex&nbsp;<em>n</em> represents the last element;\nindex&nbsp;-1 also represents the last element\n(that is, the element at the&nbsp;top)\nand index <em>-n</em> represents the first element.\nWe say that an index is <em>valid</em>\nif it lies between&nbsp;1 and the stack top\n(that is, if <code>1 &le; abs(index) &le; top</code>).\n \n\n\n\n\n\n<h2>3.2 - <a name=\"3.2\">Stack Size</a></h2>\n\n<p>\nWhen you interact with Lua API,\nyou are responsible for ensuring consistency.\nIn particular,\n<em>you are responsible for controlling stack overflow</em>.\nYou can use the function <a href=\"#lua_checkstack\"><code>lua_checkstack</code></a>\nto grow the stack size.\n\n\n<p>\nWhenever Lua calls C,\nit ensures that at least <a name=\"pdf-LUA_MINSTACK\"><code>LUA_MINSTACK</code></a> stack positions are available.\n<code>LUA_MINSTACK</code> is defined as 20,\nso that usually you do not have to worry about stack space\nunless your code has loops pushing elements onto the stack.\n\n\n<p>\nMost query functions accept as indices any value inside the\navailable stack space, that is, indices up to the maximum stack size\nyou have set through <a href=\"#lua_checkstack\"><code>lua_checkstack</code></a>.\nSuch indices are called <em>acceptable indices</em>.\nMore formally, we define an <em>acceptable index</em>\nas follows:\n\n<pre>\n     (index &lt; 0 &amp;&amp; abs(index) &lt;= top) ||\n     (index &gt; 0 &amp;&amp; index &lt;= stackspace)\n</pre><p>\nNote that 0 is never an acceptable index.\n\n\n\n\n\n<h2>3.3 - <a name=\"3.3\">Pseudo-Indices</a></h2>\n\n<p>\nUnless otherwise noted,\nany function that accepts valid indices can also be called with\n<em>pseudo-indices</em>,\nwhich represent some Lua values that are accessible to C&nbsp;code\nbut which are not in the stack.\nPseudo-indices are used to access the thread environment,\nthe function environment,\nthe registry,\nand the upvalues of a C&nbsp;function (see <a href=\"#3.4\">&sect;3.4</a>).\n\n\n<p>\nThe thread environment (where global variables live) is\nalways at pseudo-index <a name=\"pdf-LUA_GLOBALSINDEX\"><code>LUA_GLOBALSINDEX</code></a>.\nThe environment of the running C&nbsp;function is always\nat pseudo-index <a name=\"pdf-LUA_ENVIRONINDEX\"><code>LUA_ENVIRONINDEX</code></a>.\n\n\n<p>\nTo access and change the value of global variables,\nyou can use regular table operations over an environment table.\nFor instance, to access the value of a global variable, do\n\n<pre>\n     lua_getfield(L, LUA_GLOBALSINDEX, varname);\n</pre>\n\n\n\n\n<h2>3.4 - <a name=\"3.4\">C Closures</a></h2>\n\n<p>\nWhen a C&nbsp;function is created,\nit is possible to associate some values with it,\nthus creating a <em>C&nbsp;closure</em>;\nthese values are called <em>upvalues</em> and are\naccessible to the function whenever it is called\n(see <a href=\"#lua_pushcclosure\"><code>lua_pushcclosure</code></a>).\n\n\n<p>\nWhenever a C&nbsp;function is called,\nits upvalues are located at specific pseudo-indices.\nThese pseudo-indices are produced by the macro\n<a name=\"lua_upvalueindex\"><code>lua_upvalueindex</code></a>.\nThe first value associated with a function is at position\n<code>lua_upvalueindex(1)</code>, and so on.\nAny access to <code>lua_upvalueindex(<em>n</em>)</code>,\nwhere <em>n</em> is greater than the number of upvalues of the\ncurrent function (but not greater than 256),\nproduces an acceptable (but invalid) index.\n\n\n\n\n\n<h2>3.5 - <a name=\"3.5\">Registry</a></h2>\n\n<p>\nLua provides a <em>registry</em>,\na pre-defined table that can be used by any C&nbsp;code to\nstore whatever Lua value it needs to store.\nThis table is always located at pseudo-index\n<a name=\"pdf-LUA_REGISTRYINDEX\"><code>LUA_REGISTRYINDEX</code></a>.\nAny C&nbsp;library can store data into this table,\nbut it should take care to choose keys different from those used\nby other libraries, to avoid collisions.\nTypically, you should use as key a string containing your library name\nor a light userdata with the address of a C&nbsp;object in your code.\n\n\n<p>\nThe integer keys in the registry are used by the reference mechanism,\nimplemented by the auxiliary library,\nand therefore should not be used for other purposes.\n\n\n\n\n\n<h2>3.6 - <a name=\"3.6\">Error Handling in C</a></h2>\n\n<p>\nInternally, Lua uses the C <code>longjmp</code> facility to handle errors.\n(You can also choose to use exceptions if you use C++;\nsee file <code>luaconf.h</code>.)\nWhen Lua faces any error\n(such as memory allocation errors, type errors, syntax errors,\nand runtime errors)\nit <em>raises</em> an error;\nthat is, it does a long jump.\nA <em>protected environment</em> uses <code>setjmp</code>\nto set a recover point;\nany error jumps to the most recent active recover point.\n\n\n<p>\nMost functions in the API can throw an error,\nfor instance due to a memory allocation error.\nThe documentation for each function indicates whether\nit can throw errors.\n\n\n<p>\nInside a C&nbsp;function you can throw an error by calling <a href=\"#lua_error\"><code>lua_error</code></a>.\n\n\n\n\n\n<h2>3.7 - <a name=\"3.7\">Functions and Types</a></h2>\n\n<p>\nHere we list all functions and types from the C&nbsp;API in\nalphabetical order.\nEach function has an indicator like this:\n<span class=\"apii\">[-o, +p, <em>x</em>]</span>\n\n\n<p>\nThe first field, <code>o</code>,\nis how many elements the function pops from the stack.\nThe second field, <code>p</code>,\nis how many elements the function pushes onto the stack.\n(Any function always pushes its results after popping its arguments.)\nA field in the form <code>x|y</code> means the function can push (or pop)\n<code>x</code> or <code>y</code> elements,\ndepending on the situation;\nan interrogation mark '<code>?</code>' means that\nwe cannot know how many elements the function pops/pushes\nby looking only at its arguments\n(e.g., they may depend on what is on the stack).\nThe third field, <code>x</code>,\ntells whether the function may throw errors:\n'<code>-</code>' means the function never throws any error;\n'<code>m</code>' means the function may throw an error\nonly due to not enough memory;\n'<code>e</code>' means the function may throw other kinds of errors;\n'<code>v</code>' means the function may throw an error on purpose.\n\n\n\n<hr><h3><a name=\"lua_Alloc\"><code>lua_Alloc</code></a></h3>\n<pre>typedef void * (*lua_Alloc) (void *ud,\n                             void *ptr,\n                             size_t osize,\n                             size_t nsize);</pre>\n\n<p>\nThe type of the memory-allocation function used by Lua states.\nThe allocator function must provide a\nfunctionality similar to <code>realloc</code>,\nbut not exactly the same.\nIts arguments are\n<code>ud</code>, an opaque pointer passed to <a href=\"#lua_newstate\"><code>lua_newstate</code></a>;\n<code>ptr</code>, a pointer to the block being allocated/reallocated/freed;\n<code>osize</code>, the original size of the block;\n<code>nsize</code>, the new size of the block.\n<code>ptr</code> is <code>NULL</code> if and only if <code>osize</code> is zero.\nWhen <code>nsize</code> is zero, the allocator must return <code>NULL</code>;\nif <code>osize</code> is not zero,\nit should free the block pointed to by <code>ptr</code>.\nWhen <code>nsize</code> is not zero, the allocator returns <code>NULL</code>\nif and only if it cannot fill the request.\nWhen <code>nsize</code> is not zero and <code>osize</code> is zero,\nthe allocator should behave like <code>malloc</code>.\nWhen <code>nsize</code> and <code>osize</code> are not zero,\nthe allocator behaves like <code>realloc</code>.\nLua assumes that the allocator never fails when\n<code>osize &gt;= nsize</code>.\n\n\n<p>\nHere is a simple implementation for the allocator function.\nIt is used in the auxiliary library by <a href=\"#luaL_newstate\"><code>luaL_newstate</code></a>.\n\n<pre>\n     static void *l_alloc (void *ud, void *ptr, size_t osize,\n                                                size_t nsize) {\n       (void)ud;  (void)osize;  /* not used */\n       if (nsize == 0) {\n         free(ptr);\n         return NULL;\n       }\n       else\n         return realloc(ptr, nsize);\n     }\n</pre><p>\nThis code assumes\nthat <code>free(NULL)</code> has no effect and that\n<code>realloc(NULL, size)</code> is equivalent to <code>malloc(size)</code>.\nANSI&nbsp;C ensures both behaviors.\n\n\n\n\n\n<hr><h3><a name=\"lua_atpanic\"><code>lua_atpanic</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf);</pre>\n\n<p>\nSets a new panic function and returns the old one.\n\n\n<p>\nIf an error happens outside any protected environment,\nLua calls a <em>panic function</em>\nand then calls <code>exit(EXIT_FAILURE)</code>,\nthus exiting the host application.\nYour panic function can avoid this exit by\nnever returning (e.g., doing a long jump).\n\n\n<p>\nThe panic function can access the error message at the top of the stack.\n\n\n\n\n\n<hr><h3><a name=\"lua_call\"><code>lua_call</code></a></h3><p>\n<span class=\"apii\">[-(nargs + 1), +nresults, <em>e</em>]</span>\n<pre>void lua_call (lua_State *L, int nargs, int nresults);</pre>\n\n<p>\nCalls a function.\n\n\n<p>\nTo call a function you must use the following protocol:\nfirst, the function to be called is pushed onto the stack;\nthen, the arguments to the function are pushed\nin direct order;\nthat is, the first argument is pushed first.\nFinally you call <a href=\"#lua_call\"><code>lua_call</code></a>;\n<code>nargs</code> is the number of arguments that you pushed onto the stack.\nAll arguments and the function value are popped from the stack\nwhen the function is called.\nThe function results are pushed onto the stack when the function returns.\nThe number of results is adjusted to <code>nresults</code>,\nunless <code>nresults</code> is <a name=\"pdf-LUA_MULTRET\"><code>LUA_MULTRET</code></a>.\nIn this case, <em>all</em> results from the function are pushed.\nLua takes care that the returned values fit into the stack space.\nThe function results are pushed onto the stack in direct order\n(the first result is pushed first),\nso that after the call the last result is on the top of the stack.\n\n\n<p>\nAny error inside the called function is propagated upwards\n(with a <code>longjmp</code>).\n\n\n<p>\nThe following example shows how the host program can do the\nequivalent to this Lua code:\n\n<pre>\n     a = f(\"how\", t.x, 14)\n</pre><p>\nHere it is in&nbsp;C:\n\n<pre>\n     lua_getfield(L, LUA_GLOBALSINDEX, \"f\"); /* function to be called */\n     lua_pushstring(L, \"how\");                        /* 1st argument */\n     lua_getfield(L, LUA_GLOBALSINDEX, \"t\");   /* table to be indexed */\n     lua_getfield(L, -1, \"x\");        /* push result of t.x (2nd arg) */\n     lua_remove(L, -2);                  /* remove 't' from the stack */\n     lua_pushinteger(L, 14);                          /* 3rd argument */\n     lua_call(L, 3, 1);     /* call 'f' with 3 arguments and 1 result */\n     lua_setfield(L, LUA_GLOBALSINDEX, \"a\");        /* set global 'a' */\n</pre><p>\nNote that the code above is \"balanced\":\nat its end, the stack is back to its original configuration.\nThis is considered good programming practice.\n\n\n\n\n\n<hr><h3><a name=\"lua_CFunction\"><code>lua_CFunction</code></a></h3>\n<pre>typedef int (*lua_CFunction) (lua_State *L);</pre>\n\n<p>\nType for C&nbsp;functions.\n\n\n<p>\nIn order to communicate properly with Lua,\na C&nbsp;function must use the following protocol,\nwhich defines the way parameters and results are passed:\na C&nbsp;function receives its arguments from Lua in its stack\nin direct order (the first argument is pushed first).\nSo, when the function starts,\n<code>lua_gettop(L)</code> returns the number of arguments received by the function.\nThe first argument (if any) is at index 1\nand its last argument is at index <code>lua_gettop(L)</code>.\nTo return values to Lua, a C&nbsp;function just pushes them onto the stack,\nin direct order (the first result is pushed first),\nand returns the number of results.\nAny other value in the stack below the results will be properly\ndiscarded by Lua.\nLike a Lua function, a C&nbsp;function called by Lua can also return\nmany results.\n\n\n<p>\nAs an example, the following function receives a variable number\nof numerical arguments and returns their average and sum:\n\n<pre>\n     static int foo (lua_State *L) {\n       int n = lua_gettop(L);    /* number of arguments */\n       lua_Number sum = 0;\n       int i;\n       for (i = 1; i &lt;= n; i++) {\n         if (!lua_isnumber(L, i)) {\n           lua_pushstring(L, \"incorrect argument\");\n           lua_error(L);\n         }\n         sum += lua_tonumber(L, i);\n       }\n       lua_pushnumber(L, sum/n);        /* first result */\n       lua_pushnumber(L, sum);         /* second result */\n       return 2;                   /* number of results */\n     }\n</pre>\n\n\n\n\n<hr><h3><a name=\"lua_checkstack\"><code>lua_checkstack</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>m</em>]</span>\n<pre>int lua_checkstack (lua_State *L, int extra);</pre>\n\n<p>\nEnsures that there are at least <code>extra</code> free stack slots in the stack.\nIt returns false if it cannot grow the stack to that size.\nThis function never shrinks the stack;\nif the stack is already larger than the new size,\nit is left unchanged.\n\n\n\n\n\n<hr><h3><a name=\"lua_close\"><code>lua_close</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>void lua_close (lua_State *L);</pre>\n\n<p>\nDestroys all objects in the given Lua state\n(calling the corresponding garbage-collection metamethods, if any)\nand frees all dynamic memory used by this state.\nOn several platforms, you may not need to call this function,\nbecause all resources are naturally released when the host program ends.\nOn the other hand, long-running programs,\nsuch as a daemon or a web server,\nmight need to release states as soon as they are not needed,\nto avoid growing too large.\n\n\n\n\n\n<hr><h3><a name=\"lua_concat\"><code>lua_concat</code></a></h3><p>\n<span class=\"apii\">[-n, +1, <em>e</em>]</span>\n<pre>void lua_concat (lua_State *L, int n);</pre>\n\n<p>\nConcatenates the <code>n</code> values at the top of the stack,\npops them, and leaves the result at the top.\nIf <code>n</code>&nbsp;is&nbsp;1, the result is the single value on the stack\n(that is, the function does nothing);\nif <code>n</code> is 0, the result is the empty string.\nConcatenation is performed following the usual semantics of Lua\n(see <a href=\"#2.5.4\">&sect;2.5.4</a>).\n\n\n\n\n\n<hr><h3><a name=\"lua_cpcall\"><code>lua_cpcall</code></a></h3><p>\n<span class=\"apii\">[-0, +(0|1), <em>-</em>]</span>\n<pre>int lua_cpcall (lua_State *L, lua_CFunction func, void *ud);</pre>\n\n<p>\nCalls the C&nbsp;function <code>func</code> in protected mode.\n<code>func</code> starts with only one element in its stack,\na light userdata containing <code>ud</code>.\nIn case of errors,\n<a href=\"#lua_cpcall\"><code>lua_cpcall</code></a> returns the same error codes as <a href=\"#lua_pcall\"><code>lua_pcall</code></a>,\nplus the error object on the top of the stack;\notherwise, it returns zero, and does not change the stack.\nAll values returned by <code>func</code> are discarded.\n\n\n\n\n\n<hr><h3><a name=\"lua_createtable\"><code>lua_createtable</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>m</em>]</span>\n<pre>void lua_createtable (lua_State *L, int narr, int nrec);</pre>\n\n<p>\nCreates a new empty table and pushes it onto the stack.\nThe new table has space pre-allocated\nfor <code>narr</code> array elements and <code>nrec</code> non-array elements.\nThis pre-allocation is useful when you know exactly how many elements\nthe table will have.\nOtherwise you can use the function <a href=\"#lua_newtable\"><code>lua_newtable</code></a>.\n\n\n\n\n\n<hr><h3><a name=\"lua_dump\"><code>lua_dump</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>m</em>]</span>\n<pre>int lua_dump (lua_State *L, lua_Writer writer, void *data);</pre>\n\n<p>\nDumps a function as a binary chunk.\nReceives a Lua function on the top of the stack\nand produces a binary chunk that,\nif loaded again,\nresults in a function equivalent to the one dumped.\nAs it produces parts of the chunk,\n<a href=\"#lua_dump\"><code>lua_dump</code></a> calls function <code>writer</code> (see <a href=\"#lua_Writer\"><code>lua_Writer</code></a>)\nwith the given <code>data</code>\nto write them.\n\n\n<p>\nThe value returned is the error code returned by the last\ncall to the writer;\n0&nbsp;means no errors.\n\n\n<p>\nThis function does not pop the Lua function from the stack.\n\n\n\n\n\n<hr><h3><a name=\"lua_equal\"><code>lua_equal</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>e</em>]</span>\n<pre>int lua_equal (lua_State *L, int index1, int index2);</pre>\n\n<p>\nReturns 1 if the two values in acceptable indices <code>index1</code> and\n<code>index2</code> are equal,\nfollowing the semantics of the Lua <code>==</code> operator\n(that is, may call metamethods).\nOtherwise returns&nbsp;0.\nAlso returns&nbsp;0 if any of the indices is non valid.\n\n\n\n\n\n<hr><h3><a name=\"lua_error\"><code>lua_error</code></a></h3><p>\n<span class=\"apii\">[-1, +0, <em>v</em>]</span>\n<pre>int lua_error (lua_State *L);</pre>\n\n<p>\nGenerates a Lua error.\nThe error message (which can actually be a Lua value of any type)\nmust be on the stack top.\nThis function does a long jump,\nand therefore never returns.\n(see <a href=\"#luaL_error\"><code>luaL_error</code></a>).\n\n\n\n\n\n<hr><h3><a name=\"lua_gc\"><code>lua_gc</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>e</em>]</span>\n<pre>int lua_gc (lua_State *L, int what, int data);</pre>\n\n<p>\nControls the garbage collector.\n\n\n<p>\nThis function performs several tasks,\naccording to the value of the parameter <code>what</code>:\n\n<ul>\n\n<li><b><code>LUA_GCSTOP</code>:</b>\nstops the garbage collector.\n</li>\n\n<li><b><code>LUA_GCRESTART</code>:</b>\nrestarts the garbage collector.\n</li>\n\n<li><b><code>LUA_GCCOLLECT</code>:</b>\nperforms a full garbage-collection cycle.\n</li>\n\n<li><b><code>LUA_GCCOUNT</code>:</b>\nreturns the current amount of memory (in Kbytes) in use by Lua.\n</li>\n\n<li><b><code>LUA_GCCOUNTB</code>:</b>\nreturns the remainder of dividing the current amount of bytes of\nmemory in use by Lua by 1024.\n</li>\n\n<li><b><code>LUA_GCSTEP</code>:</b>\nperforms an incremental step of garbage collection.\nThe step \"size\" is controlled by <code>data</code>\n(larger values mean more steps) in a non-specified way.\nIf you want to control the step size\nyou must experimentally tune the value of <code>data</code>.\nThe function returns 1 if the step finished a\ngarbage-collection cycle.\n</li>\n\n<li><b><code>LUA_GCSETPAUSE</code>:</b>\nsets <code>data</code> as the new value\nfor the <em>pause</em> of the collector (see <a href=\"#2.10\">&sect;2.10</a>).\nThe function returns the previous value of the pause.\n</li>\n\n<li><b><code>LUA_GCSETSTEPMUL</code>:</b>\nsets <code>data</code> as the new value for the <em>step multiplier</em> of\nthe collector (see <a href=\"#2.10\">&sect;2.10</a>).\nThe function returns the previous value of the step multiplier.\n</li>\n\n</ul>\n\n\n\n\n<hr><h3><a name=\"lua_getallocf\"><code>lua_getallocf</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>lua_Alloc lua_getallocf (lua_State *L, void **ud);</pre>\n\n<p>\nReturns the memory-allocation function of a given state.\nIf <code>ud</code> is not <code>NULL</code>, Lua stores in <code>*ud</code> the\nopaque pointer passed to <a href=\"#lua_newstate\"><code>lua_newstate</code></a>.\n\n\n\n\n\n<hr><h3><a name=\"lua_getfenv\"><code>lua_getfenv</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>-</em>]</span>\n<pre>void lua_getfenv (lua_State *L, int index);</pre>\n\n<p>\nPushes onto the stack the environment table of\nthe value at the given index.\n\n\n\n\n\n<hr><h3><a name=\"lua_getfield\"><code>lua_getfield</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>e</em>]</span>\n<pre>void lua_getfield (lua_State *L, int index, const char *k);</pre>\n\n<p>\nPushes onto the stack the value <code>t[k]</code>,\nwhere <code>t</code> is the value at the given valid index.\nAs in Lua, this function may trigger a metamethod\nfor the \"index\" event (see <a href=\"#2.8\">&sect;2.8</a>).\n\n\n\n\n\n<hr><h3><a name=\"lua_getglobal\"><code>lua_getglobal</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>e</em>]</span>\n<pre>void lua_getglobal (lua_State *L, const char *name);</pre>\n\n<p>\nPushes onto the stack the value of the global <code>name</code>.\nIt is defined as a macro:\n\n<pre>\n     #define lua_getglobal(L,s)  lua_getfield(L, LUA_GLOBALSINDEX, s)\n</pre>\n\n\n\n\n<hr><h3><a name=\"lua_getmetatable\"><code>lua_getmetatable</code></a></h3><p>\n<span class=\"apii\">[-0, +(0|1), <em>-</em>]</span>\n<pre>int lua_getmetatable (lua_State *L, int index);</pre>\n\n<p>\nPushes onto the stack the metatable of the value at the given\nacceptable index.\nIf the index is not valid,\nor if the value does not have a metatable,\nthe function returns&nbsp;0 and pushes nothing on the stack.\n\n\n\n\n\n<hr><h3><a name=\"lua_gettable\"><code>lua_gettable</code></a></h3><p>\n<span class=\"apii\">[-1, +1, <em>e</em>]</span>\n<pre>void lua_gettable (lua_State *L, int index);</pre>\n\n<p>\nPushes onto the stack the value <code>t[k]</code>,\nwhere <code>t</code> is the value at the given valid index\nand <code>k</code> is the value at the top of the stack.\n\n\n<p>\nThis function pops the key from the stack\n(putting the resulting value in its place).\nAs in Lua, this function may trigger a metamethod\nfor the \"index\" event (see <a href=\"#2.8\">&sect;2.8</a>).\n\n\n\n\n\n<hr><h3><a name=\"lua_gettop\"><code>lua_gettop</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>int lua_gettop (lua_State *L);</pre>\n\n<p>\nReturns the index of the top element in the stack.\nBecause indices start at&nbsp;1,\nthis result is equal to the number of elements in the stack\n(and so 0&nbsp;means an empty stack).\n\n\n\n\n\n<hr><h3><a name=\"lua_insert\"><code>lua_insert</code></a></h3><p>\n<span class=\"apii\">[-1, +1, <em>-</em>]</span>\n<pre>void lua_insert (lua_State *L, int index);</pre>\n\n<p>\nMoves the top element into the given valid index,\nshifting up the elements above this index to open space.\nCannot be called with a pseudo-index,\nbecause a pseudo-index is not an actual stack position.\n\n\n\n\n\n<hr><h3><a name=\"lua_Integer\"><code>lua_Integer</code></a></h3>\n<pre>typedef ptrdiff_t lua_Integer;</pre>\n\n<p>\nThe type used by the Lua API to represent integral values.\n\n\n<p>\nBy default it is a <code>ptrdiff_t</code>,\nwhich is usually the largest signed integral type the machine handles\n\"comfortably\".\n\n\n\n\n\n<hr><h3><a name=\"lua_isboolean\"><code>lua_isboolean</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>int lua_isboolean (lua_State *L, int index);</pre>\n\n<p>\nReturns 1 if the value at the given acceptable index has type boolean,\nand 0&nbsp;otherwise.\n\n\n\n\n\n<hr><h3><a name=\"lua_iscfunction\"><code>lua_iscfunction</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>int lua_iscfunction (lua_State *L, int index);</pre>\n\n<p>\nReturns 1 if the value at the given acceptable index is a C&nbsp;function,\nand 0&nbsp;otherwise.\n\n\n\n\n\n<hr><h3><a name=\"lua_isfunction\"><code>lua_isfunction</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>int lua_isfunction (lua_State *L, int index);</pre>\n\n<p>\nReturns 1 if the value at the given acceptable index is a function\n(either C or Lua), and 0&nbsp;otherwise.\n\n\n\n\n\n<hr><h3><a name=\"lua_islightuserdata\"><code>lua_islightuserdata</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>int lua_islightuserdata (lua_State *L, int index);</pre>\n\n<p>\nReturns 1 if the value at the given acceptable index is a light userdata,\nand 0&nbsp;otherwise.\n\n\n\n\n\n<hr><h3><a name=\"lua_isnil\"><code>lua_isnil</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>int lua_isnil (lua_State *L, int index);</pre>\n\n<p>\nReturns 1 if the value at the given acceptable index is <b>nil</b>,\nand 0&nbsp;otherwise.\n\n\n\n\n\n<hr><h3><a name=\"lua_isnone\"><code>lua_isnone</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>int lua_isnone (lua_State *L, int index);</pre>\n\n<p>\nReturns 1 if the given acceptable index is not valid\n(that is, it refers to an element outside the current stack),\nand 0&nbsp;otherwise.\n\n\n\n\n\n<hr><h3><a name=\"lua_isnoneornil\"><code>lua_isnoneornil</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>int lua_isnoneornil (lua_State *L, int index);</pre>\n\n<p>\nReturns 1 if the given acceptable index is not valid\n(that is, it refers to an element outside the current stack)\nor if the value at this index is <b>nil</b>,\nand 0&nbsp;otherwise.\n\n\n\n\n\n<hr><h3><a name=\"lua_isnumber\"><code>lua_isnumber</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>int lua_isnumber (lua_State *L, int index);</pre>\n\n<p>\nReturns 1 if the value at the given acceptable index is a number\nor a string convertible to a number,\nand 0&nbsp;otherwise.\n\n\n\n\n\n<hr><h3><a name=\"lua_isstring\"><code>lua_isstring</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>int lua_isstring (lua_State *L, int index);</pre>\n\n<p>\nReturns 1 if the value at the given acceptable index is a string\nor a number (which is always convertible to a string),\nand 0&nbsp;otherwise.\n\n\n\n\n\n<hr><h3><a name=\"lua_istable\"><code>lua_istable</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>int lua_istable (lua_State *L, int index);</pre>\n\n<p>\nReturns 1 if the value at the given acceptable index is a table,\nand 0&nbsp;otherwise.\n\n\n\n\n\n<hr><h3><a name=\"lua_isthread\"><code>lua_isthread</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>int lua_isthread (lua_State *L, int index);</pre>\n\n<p>\nReturns 1 if the value at the given acceptable index is a thread,\nand 0&nbsp;otherwise.\n\n\n\n\n\n<hr><h3><a name=\"lua_isuserdata\"><code>lua_isuserdata</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>int lua_isuserdata (lua_State *L, int index);</pre>\n\n<p>\nReturns 1 if the value at the given acceptable index is a userdata\n(either full or light), and 0&nbsp;otherwise.\n\n\n\n\n\n<hr><h3><a name=\"lua_lessthan\"><code>lua_lessthan</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>e</em>]</span>\n<pre>int lua_lessthan (lua_State *L, int index1, int index2);</pre>\n\n<p>\nReturns 1 if the value at acceptable index <code>index1</code> is smaller\nthan the value at acceptable index <code>index2</code>,\nfollowing the semantics of the Lua <code>&lt;</code> operator\n(that is, may call metamethods).\nOtherwise returns&nbsp;0.\nAlso returns&nbsp;0 if any of the indices is non valid.\n\n\n\n\n\n<hr><h3><a name=\"lua_load\"><code>lua_load</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>-</em>]</span>\n<pre>int lua_load (lua_State *L,\n              lua_Reader reader,\n              void *data,\n              const char *chunkname);</pre>\n\n<p>\nLoads a Lua chunk.\nIf there are no errors,\n<a href=\"#lua_load\"><code>lua_load</code></a> pushes the compiled chunk as a Lua\nfunction on top of the stack.\nOtherwise, it pushes an error message.\nThe return values of <a href=\"#lua_load\"><code>lua_load</code></a> are:\n\n<ul>\n\n<li><b>0:</b> no errors;</li>\n\n<li><b><a name=\"pdf-LUA_ERRSYNTAX\"><code>LUA_ERRSYNTAX</code></a>:</b>\nsyntax error during pre-compilation;</li>\n\n<li><b><a href=\"#pdf-LUA_ERRMEM\"><code>LUA_ERRMEM</code></a>:</b>\nmemory allocation error.</li>\n\n</ul>\n\n<p>\nThis function only loads a chunk;\nit does not run it.\n\n\n<p>\n<a href=\"#lua_load\"><code>lua_load</code></a> automatically detects whether the chunk is text or binary,\nand loads it accordingly (see program <code>luac</code>).\n\n\n<p>\nThe <a href=\"#lua_load\"><code>lua_load</code></a> function uses a user-supplied <code>reader</code> function\nto read the chunk (see <a href=\"#lua_Reader\"><code>lua_Reader</code></a>).\nThe <code>data</code> argument is an opaque value passed to the reader function.\n\n\n<p>\nThe <code>chunkname</code> argument gives a name to the chunk,\nwhich is used for error messages and in debug information (see <a href=\"#3.8\">&sect;3.8</a>).\n\n\n\n\n\n<hr><h3><a name=\"lua_newstate\"><code>lua_newstate</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>lua_State *lua_newstate (lua_Alloc f, void *ud);</pre>\n\n<p>\nCreates a new, independent state.\nReturns <code>NULL</code> if cannot create the state\n(due to lack of memory).\nThe argument <code>f</code> is the allocator function;\nLua does all memory allocation for this state through this function.\nThe second argument, <code>ud</code>, is an opaque pointer that Lua\nsimply passes to the allocator in every call.\n\n\n\n\n\n<hr><h3><a name=\"lua_newtable\"><code>lua_newtable</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>m</em>]</span>\n<pre>void lua_newtable (lua_State *L);</pre>\n\n<p>\nCreates a new empty table and pushes it onto the stack.\nIt is equivalent to <code>lua_createtable(L, 0, 0)</code>.\n\n\n\n\n\n<hr><h3><a name=\"lua_newthread\"><code>lua_newthread</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>m</em>]</span>\n<pre>lua_State *lua_newthread (lua_State *L);</pre>\n\n<p>\nCreates a new thread, pushes it on the stack,\nand returns a pointer to a <a href=\"#lua_State\"><code>lua_State</code></a> that represents this new thread.\nThe new state returned by this function shares with the original state\nall global objects (such as tables),\nbut has an independent execution stack.\n\n\n<p>\nThere is no explicit function to close or to destroy a thread.\nThreads are subject to garbage collection,\nlike any Lua object.\n\n\n\n\n\n<hr><h3><a name=\"lua_newuserdata\"><code>lua_newuserdata</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>m</em>]</span>\n<pre>void *lua_newuserdata (lua_State *L, size_t size);</pre>\n\n<p>\nThis function allocates a new block of memory with the given size,\npushes onto the stack a new full userdata with the block address,\nand returns this address.\n\n\n<p>\nUserdata represent C&nbsp;values in Lua.\nA <em>full userdata</em> represents a block of memory.\nIt is an object (like a table):\nyou must create it, it can have its own metatable,\nand you can detect when it is being collected.\nA full userdata is only equal to itself (under raw equality).\n\n\n<p>\nWhen Lua collects a full userdata with a <code>gc</code> metamethod,\nLua calls the metamethod and marks the userdata as finalized.\nWhen this userdata is collected again then\nLua frees its corresponding memory.\n\n\n\n\n\n<hr><h3><a name=\"lua_next\"><code>lua_next</code></a></h3><p>\n<span class=\"apii\">[-1, +(2|0), <em>e</em>]</span>\n<pre>int lua_next (lua_State *L, int index);</pre>\n\n<p>\nPops a key from the stack,\nand pushes a key-value pair from the table at the given index\n(the \"next\" pair after the given key).\nIf there are no more elements in the table,\nthen <a href=\"#lua_next\"><code>lua_next</code></a> returns 0 (and pushes nothing).\n\n\n<p>\nA typical traversal looks like this:\n\n<pre>\n     /* table is in the stack at index 't' */\n     lua_pushnil(L);  /* first key */\n     while (lua_next(L, t) != 0) {\n       /* uses 'key' (at index -2) and 'value' (at index -1) */\n       printf(\"%s - %s\\n\",\n              lua_typename(L, lua_type(L, -2)),\n              lua_typename(L, lua_type(L, -1)));\n       /* removes 'value'; keeps 'key' for next iteration */\n       lua_pop(L, 1);\n     }\n</pre>\n\n<p>\nWhile traversing a table,\ndo not call <a href=\"#lua_tolstring\"><code>lua_tolstring</code></a> directly on a key,\nunless you know that the key is actually a string.\nRecall that <a href=\"#lua_tolstring\"><code>lua_tolstring</code></a> <em>changes</em>\nthe value at the given index;\nthis confuses the next call to <a href=\"#lua_next\"><code>lua_next</code></a>.\n\n\n\n\n\n<hr><h3><a name=\"lua_Number\"><code>lua_Number</code></a></h3>\n<pre>typedef double lua_Number;</pre>\n\n<p>\nThe type of numbers in Lua.\nBy default, it is double, but that can be changed in <code>luaconf.h</code>.\n\n\n<p>\nThrough the configuration file you can change\nLua to operate with another type for numbers (e.g., float or long).\n\n\n\n\n\n<hr><h3><a name=\"lua_objlen\"><code>lua_objlen</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>size_t lua_objlen (lua_State *L, int index);</pre>\n\n<p>\nReturns the \"length\" of the value at the given acceptable index:\nfor strings, this is the string length;\nfor tables, this is the result of the length operator ('<code>#</code>');\nfor userdata, this is the size of the block of memory allocated\nfor the userdata;\nfor other values, it is&nbsp;0.\n\n\n\n\n\n<hr><h3><a name=\"lua_pcall\"><code>lua_pcall</code></a></h3><p>\n<span class=\"apii\">[-(nargs + 1), +(nresults|1), <em>-</em>]</span>\n<pre>int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc);</pre>\n\n<p>\nCalls a function in protected mode.\n\n\n<p>\nBoth <code>nargs</code> and <code>nresults</code> have the same meaning as\nin <a href=\"#lua_call\"><code>lua_call</code></a>.\nIf there are no errors during the call,\n<a href=\"#lua_pcall\"><code>lua_pcall</code></a> behaves exactly like <a href=\"#lua_call\"><code>lua_call</code></a>.\nHowever, if there is any error,\n<a href=\"#lua_pcall\"><code>lua_pcall</code></a> catches it,\npushes a single value on the stack (the error message),\nand returns an error code.\nLike <a href=\"#lua_call\"><code>lua_call</code></a>,\n<a href=\"#lua_pcall\"><code>lua_pcall</code></a> always removes the function\nand its arguments from the stack.\n\n\n<p>\nIf <code>errfunc</code> is 0,\nthen the error message returned on the stack\nis exactly the original error message.\nOtherwise, <code>errfunc</code> is the stack index of an\n<em>error handler function</em>.\n(In the current implementation, this index cannot be a pseudo-index.)\nIn case of runtime errors,\nthis function will be called with the error message\nand its return value will be the message returned on the stack by <a href=\"#lua_pcall\"><code>lua_pcall</code></a>.\n\n\n<p>\nTypically, the error handler function is used to add more debug\ninformation to the error message, such as a stack traceback.\nSuch information cannot be gathered after the return of <a href=\"#lua_pcall\"><code>lua_pcall</code></a>,\nsince by then the stack has unwound.\n\n\n<p>\nThe <a href=\"#lua_pcall\"><code>lua_pcall</code></a> function returns 0 in case of success\nor one of the following error codes\n(defined in <code>lua.h</code>):\n\n<ul>\n\n<li><b><a name=\"pdf-LUA_ERRRUN\"><code>LUA_ERRRUN</code></a>:</b>\na runtime error.\n</li>\n\n<li><b><a name=\"pdf-LUA_ERRMEM\"><code>LUA_ERRMEM</code></a>:</b>\nmemory allocation error.\nFor such errors, Lua does not call the error handler function.\n</li>\n\n<li><b><a name=\"pdf-LUA_ERRERR\"><code>LUA_ERRERR</code></a>:</b>\nerror while running the error handler function.\n</li>\n\n</ul>\n\n\n\n\n<hr><h3><a name=\"lua_pop\"><code>lua_pop</code></a></h3><p>\n<span class=\"apii\">[-n, +0, <em>-</em>]</span>\n<pre>void lua_pop (lua_State *L, int n);</pre>\n\n<p>\nPops <code>n</code> elements from the stack.\n\n\n\n\n\n<hr><h3><a name=\"lua_pushboolean\"><code>lua_pushboolean</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>-</em>]</span>\n<pre>void lua_pushboolean (lua_State *L, int b);</pre>\n\n<p>\nPushes a boolean value with value <code>b</code> onto the stack.\n\n\n\n\n\n<hr><h3><a name=\"lua_pushcclosure\"><code>lua_pushcclosure</code></a></h3><p>\n<span class=\"apii\">[-n, +1, <em>m</em>]</span>\n<pre>void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);</pre>\n\n<p>\nPushes a new C&nbsp;closure onto the stack.\n\n\n<p>\nWhen a C&nbsp;function is created,\nit is possible to associate some values with it,\nthus creating a C&nbsp;closure (see <a href=\"#3.4\">&sect;3.4</a>);\nthese values are then accessible to the function whenever it is called.\nTo associate values with a C&nbsp;function,\nfirst these values should be pushed onto the stack\n(when there are multiple values, the first value is pushed first).\nThen <a href=\"#lua_pushcclosure\"><code>lua_pushcclosure</code></a>\nis called to create and push the C&nbsp;function onto the stack,\nwith the argument <code>n</code> telling how many values should be\nassociated with the function.\n<a href=\"#lua_pushcclosure\"><code>lua_pushcclosure</code></a> also pops these values from the stack.\n\n\n<p>\nThe maximum value for <code>n</code> is 255.\n\n\n\n\n\n<hr><h3><a name=\"lua_pushcfunction\"><code>lua_pushcfunction</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>m</em>]</span>\n<pre>void lua_pushcfunction (lua_State *L, lua_CFunction f);</pre>\n\n<p>\nPushes a C&nbsp;function onto the stack.\nThis function receives a pointer to a C function\nand pushes onto the stack a Lua value of type <code>function</code> that,\nwhen called, invokes the corresponding C&nbsp;function.\n\n\n<p>\nAny function to be registered in Lua must\nfollow the correct protocol to receive its parameters\nand return its results (see <a href=\"#lua_CFunction\"><code>lua_CFunction</code></a>).\n\n\n<p>\n<code>lua_pushcfunction</code> is defined as a macro:\n\n<pre>\n     #define lua_pushcfunction(L,f)  lua_pushcclosure(L,f,0)\n</pre>\n\n\n\n\n<hr><h3><a name=\"lua_pushfstring\"><code>lua_pushfstring</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>m</em>]</span>\n<pre>const char *lua_pushfstring (lua_State *L, const char *fmt, ...);</pre>\n\n<p>\nPushes onto the stack a formatted string\nand returns a pointer to this string.\nIt is similar to the C&nbsp;function <code>sprintf</code>,\nbut has some important differences:\n\n<ul>\n\n<li>\nYou do not have to allocate space for the result:\nthe result is a Lua string and Lua takes care of memory allocation\n(and deallocation, through garbage collection).\n</li>\n\n<li>\nThe conversion specifiers are quite restricted.\nThere are no flags, widths, or precisions.\nThe conversion specifiers can only be\n'<code>%%</code>' (inserts a '<code>%</code>' in the string),\n'<code>%s</code>' (inserts a zero-terminated string, with no size restrictions),\n'<code>%f</code>' (inserts a <a href=\"#lua_Number\"><code>lua_Number</code></a>),\n'<code>%p</code>' (inserts a pointer as a hexadecimal numeral),\n'<code>%d</code>' (inserts an <code>int</code>), and\n'<code>%c</code>' (inserts an <code>int</code> as a character).\n</li>\n\n</ul>\n\n\n\n\n<hr><h3><a name=\"lua_pushinteger\"><code>lua_pushinteger</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>-</em>]</span>\n<pre>void lua_pushinteger (lua_State *L, lua_Integer n);</pre>\n\n<p>\nPushes a number with value <code>n</code> onto the stack.\n\n\n\n\n\n<hr><h3><a name=\"lua_pushlightuserdata\"><code>lua_pushlightuserdata</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>-</em>]</span>\n<pre>void lua_pushlightuserdata (lua_State *L, void *p);</pre>\n\n<p>\nPushes a light userdata onto the stack.\n\n\n<p>\nUserdata represent C&nbsp;values in Lua.\nA <em>light userdata</em> represents a pointer.\nIt is a value (like a number):\nyou do not create it, it has no individual metatable,\nand it is not collected (as it was never created).\nA light userdata is equal to \"any\"\nlight userdata with the same C&nbsp;address.\n\n\n\n\n\n<hr><h3><a name=\"lua_pushliteral\"><code>lua_pushliteral</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>m</em>]</span>\n<pre>void lua_pushliteral (lua_State *L, const char *s);</pre>\n\n<p>\nThis macro is equivalent to <a href=\"#lua_pushlstring\"><code>lua_pushlstring</code></a>,\nbut can be used only when <code>s</code> is a literal string.\nIn these cases, it automatically provides the string length.\n\n\n\n\n\n<hr><h3><a name=\"lua_pushlstring\"><code>lua_pushlstring</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>m</em>]</span>\n<pre>void lua_pushlstring (lua_State *L, const char *s, size_t len);</pre>\n\n<p>\nPushes the string pointed to by <code>s</code> with size <code>len</code>\nonto the stack.\nLua makes (or reuses) an internal copy of the given string,\nso the memory at <code>s</code> can be freed or reused immediately after\nthe function returns.\nThe string can contain embedded zeros.\n\n\n\n\n\n<hr><h3><a name=\"lua_pushnil\"><code>lua_pushnil</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>-</em>]</span>\n<pre>void lua_pushnil (lua_State *L);</pre>\n\n<p>\nPushes a nil value onto the stack.\n\n\n\n\n\n<hr><h3><a name=\"lua_pushnumber\"><code>lua_pushnumber</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>-</em>]</span>\n<pre>void lua_pushnumber (lua_State *L, lua_Number n);</pre>\n\n<p>\nPushes a number with value <code>n</code> onto the stack.\n\n\n\n\n\n<hr><h3><a name=\"lua_pushstring\"><code>lua_pushstring</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>m</em>]</span>\n<pre>void lua_pushstring (lua_State *L, const char *s);</pre>\n\n<p>\nPushes the zero-terminated string pointed to by <code>s</code>\nonto the stack.\nLua makes (or reuses) an internal copy of the given string,\nso the memory at <code>s</code> can be freed or reused immediately after\nthe function returns.\nThe string cannot contain embedded zeros;\nit is assumed to end at the first zero.\n\n\n\n\n\n<hr><h3><a name=\"lua_pushthread\"><code>lua_pushthread</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>-</em>]</span>\n<pre>int lua_pushthread (lua_State *L);</pre>\n\n<p>\nPushes the thread represented by <code>L</code> onto the stack.\nReturns 1 if this thread is the main thread of its state.\n\n\n\n\n\n<hr><h3><a name=\"lua_pushvalue\"><code>lua_pushvalue</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>-</em>]</span>\n<pre>void lua_pushvalue (lua_State *L, int index);</pre>\n\n<p>\nPushes a copy of the element at the given valid index\nonto the stack.\n\n\n\n\n\n<hr><h3><a name=\"lua_pushvfstring\"><code>lua_pushvfstring</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>m</em>]</span>\n<pre>const char *lua_pushvfstring (lua_State *L,\n                              const char *fmt,\n                              va_list argp);</pre>\n\n<p>\nEquivalent to <a href=\"#lua_pushfstring\"><code>lua_pushfstring</code></a>, except that it receives a <code>va_list</code>\ninstead of a variable number of arguments.\n\n\n\n\n\n<hr><h3><a name=\"lua_rawequal\"><code>lua_rawequal</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>int lua_rawequal (lua_State *L, int index1, int index2);</pre>\n\n<p>\nReturns 1 if the two values in acceptable indices <code>index1</code> and\n<code>index2</code> are primitively equal\n(that is, without calling metamethods).\nOtherwise returns&nbsp;0.\nAlso returns&nbsp;0 if any of the indices are non valid.\n\n\n\n\n\n<hr><h3><a name=\"lua_rawget\"><code>lua_rawget</code></a></h3><p>\n<span class=\"apii\">[-1, +1, <em>-</em>]</span>\n<pre>void lua_rawget (lua_State *L, int index);</pre>\n\n<p>\nSimilar to <a href=\"#lua_gettable\"><code>lua_gettable</code></a>, but does a raw access\n(i.e., without metamethods).\n\n\n\n\n\n<hr><h3><a name=\"lua_rawgeti\"><code>lua_rawgeti</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>-</em>]</span>\n<pre>void lua_rawgeti (lua_State *L, int index, int n);</pre>\n\n<p>\nPushes onto the stack the value <code>t[n]</code>,\nwhere <code>t</code> is the value at the given valid index.\nThe access is raw;\nthat is, it does not invoke metamethods.\n\n\n\n\n\n<hr><h3><a name=\"lua_rawset\"><code>lua_rawset</code></a></h3><p>\n<span class=\"apii\">[-2, +0, <em>m</em>]</span>\n<pre>void lua_rawset (lua_State *L, int index);</pre>\n\n<p>\nSimilar to <a href=\"#lua_settable\"><code>lua_settable</code></a>, but does a raw assignment\n(i.e., without metamethods).\n\n\n\n\n\n<hr><h3><a name=\"lua_rawseti\"><code>lua_rawseti</code></a></h3><p>\n<span class=\"apii\">[-1, +0, <em>m</em>]</span>\n<pre>void lua_rawseti (lua_State *L, int index, int n);</pre>\n\n<p>\nDoes the equivalent of <code>t[n] = v</code>,\nwhere <code>t</code> is the value at the given valid index\nand <code>v</code> is the value at the top of the stack.\n\n\n<p>\nThis function pops the value from the stack.\nThe assignment is raw;\nthat is, it does not invoke metamethods.\n\n\n\n\n\n<hr><h3><a name=\"lua_Reader\"><code>lua_Reader</code></a></h3>\n<pre>typedef const char * (*lua_Reader) (lua_State *L,\n                                    void *data,\n                                    size_t *size);</pre>\n\n<p>\nThe reader function used by <a href=\"#lua_load\"><code>lua_load</code></a>.\nEvery time it needs another piece of the chunk,\n<a href=\"#lua_load\"><code>lua_load</code></a> calls the reader,\npassing along its <code>data</code> parameter.\nThe reader must return a pointer to a block of memory\nwith a new piece of the chunk\nand set <code>size</code> to the block size.\nThe block must exist until the reader function is called again.\nTo signal the end of the chunk,\nthe reader must return <code>NULL</code> or set <code>size</code> to zero.\nThe reader function may return pieces of any size greater than zero.\n\n\n\n\n\n<hr><h3><a name=\"lua_register\"><code>lua_register</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>e</em>]</span>\n<pre>void lua_register (lua_State *L,\n                   const char *name,\n                   lua_CFunction f);</pre>\n\n<p>\nSets the C function <code>f</code> as the new value of global <code>name</code>.\nIt is defined as a macro:\n\n<pre>\n     #define lua_register(L,n,f) \\\n            (lua_pushcfunction(L, f), lua_setglobal(L, n))\n</pre>\n\n\n\n\n<hr><h3><a name=\"lua_remove\"><code>lua_remove</code></a></h3><p>\n<span class=\"apii\">[-1, +0, <em>-</em>]</span>\n<pre>void lua_remove (lua_State *L, int index);</pre>\n\n<p>\nRemoves the element at the given valid index,\nshifting down the elements above this index to fill the gap.\nCannot be called with a pseudo-index,\nbecause a pseudo-index is not an actual stack position.\n\n\n\n\n\n<hr><h3><a name=\"lua_replace\"><code>lua_replace</code></a></h3><p>\n<span class=\"apii\">[-1, +0, <em>-</em>]</span>\n<pre>void lua_replace (lua_State *L, int index);</pre>\n\n<p>\nMoves the top element into the given position (and pops it),\nwithout shifting any element\n(therefore replacing the value at the given position).\n\n\n\n\n\n<hr><h3><a name=\"lua_resume\"><code>lua_resume</code></a></h3><p>\n<span class=\"apii\">[-?, +?, <em>-</em>]</span>\n<pre>int lua_resume (lua_State *L, int narg);</pre>\n\n<p>\nStarts and resumes a coroutine in a given thread.\n\n\n<p>\nTo start a coroutine, you first create a new thread\n(see <a href=\"#lua_newthread\"><code>lua_newthread</code></a>);\nthen you push onto its stack the main function plus any arguments;\nthen you call <a href=\"#lua_resume\"><code>lua_resume</code></a>,\nwith <code>narg</code> being the number of arguments.\nThis call returns when the coroutine suspends or finishes its execution.\nWhen it returns, the stack contains all values passed to <a href=\"#lua_yield\"><code>lua_yield</code></a>,\nor all values returned by the body function.\n<a href=\"#lua_resume\"><code>lua_resume</code></a> returns\n<a href=\"#pdf-LUA_YIELD\"><code>LUA_YIELD</code></a> if the coroutine yields,\n0 if the coroutine finishes its execution\nwithout errors,\nor an error code in case of errors (see <a href=\"#lua_pcall\"><code>lua_pcall</code></a>).\nIn case of errors,\nthe stack is not unwound,\nso you can use the debug API over it.\nThe error message is on the top of the stack.\nTo restart a coroutine, you put on its stack only the values to\nbe passed as results from <code>yield</code>,\nand then call <a href=\"#lua_resume\"><code>lua_resume</code></a>.\n\n\n\n\n\n<hr><h3><a name=\"lua_setallocf\"><code>lua_setallocf</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);</pre>\n\n<p>\nChanges the allocator function of a given state to <code>f</code>\nwith user data <code>ud</code>.\n\n\n\n\n\n<hr><h3><a name=\"lua_setfenv\"><code>lua_setfenv</code></a></h3><p>\n<span class=\"apii\">[-1, +0, <em>-</em>]</span>\n<pre>int lua_setfenv (lua_State *L, int index);</pre>\n\n<p>\nPops a table from the stack and sets it as\nthe new environment for the value at the given index.\nIf the value at the given index is\nneither a function nor a thread nor a userdata,\n<a href=\"#lua_setfenv\"><code>lua_setfenv</code></a> returns 0.\nOtherwise it returns 1.\n\n\n\n\n\n<hr><h3><a name=\"lua_setfield\"><code>lua_setfield</code></a></h3><p>\n<span class=\"apii\">[-1, +0, <em>e</em>]</span>\n<pre>void lua_setfield (lua_State *L, int index, const char *k);</pre>\n\n<p>\nDoes the equivalent to <code>t[k] = v</code>,\nwhere <code>t</code> is the value at the given valid index\nand <code>v</code> is the value at the top of the stack.\n\n\n<p>\nThis function pops the value from the stack.\nAs in Lua, this function may trigger a metamethod\nfor the \"newindex\" event (see <a href=\"#2.8\">&sect;2.8</a>).\n\n\n\n\n\n<hr><h3><a name=\"lua_setglobal\"><code>lua_setglobal</code></a></h3><p>\n<span class=\"apii\">[-1, +0, <em>e</em>]</span>\n<pre>void lua_setglobal (lua_State *L, const char *name);</pre>\n\n<p>\nPops a value from the stack and\nsets it as the new value of global <code>name</code>.\nIt is defined as a macro:\n\n<pre>\n     #define lua_setglobal(L,s)   lua_setfield(L, LUA_GLOBALSINDEX, s)\n</pre>\n\n\n\n\n<hr><h3><a name=\"lua_setmetatable\"><code>lua_setmetatable</code></a></h3><p>\n<span class=\"apii\">[-1, +0, <em>-</em>]</span>\n<pre>int lua_setmetatable (lua_State *L, int index);</pre>\n\n<p>\nPops a table from the stack and\nsets it as the new metatable for the value at the given\nacceptable index.\n\n\n\n\n\n<hr><h3><a name=\"lua_settable\"><code>lua_settable</code></a></h3><p>\n<span class=\"apii\">[-2, +0, <em>e</em>]</span>\n<pre>void lua_settable (lua_State *L, int index);</pre>\n\n<p>\nDoes the equivalent to <code>t[k] = v</code>,\nwhere <code>t</code> is the value at the given valid index,\n<code>v</code> is the value at the top of the stack,\nand <code>k</code> is the value just below the top.\n\n\n<p>\nThis function pops both the key and the value from the stack.\nAs in Lua, this function may trigger a metamethod\nfor the \"newindex\" event (see <a href=\"#2.8\">&sect;2.8</a>).\n\n\n\n\n\n<hr><h3><a name=\"lua_settop\"><code>lua_settop</code></a></h3><p>\n<span class=\"apii\">[-?, +?, <em>-</em>]</span>\n<pre>void lua_settop (lua_State *L, int index);</pre>\n\n<p>\nAccepts any acceptable index, or&nbsp;0,\nand sets the stack top to this index.\nIf the new top is larger than the old one,\nthen the new elements are filled with <b>nil</b>.\nIf <code>index</code> is&nbsp;0, then all stack elements are removed.\n\n\n\n\n\n<hr><h3><a name=\"lua_State\"><code>lua_State</code></a></h3>\n<pre>typedef struct lua_State lua_State;</pre>\n\n<p>\nOpaque structure that keeps the whole state of a Lua interpreter.\nThe Lua library is fully reentrant:\nit has no global variables.\nAll information about a state is kept in this structure.\n\n\n<p>\nA pointer to this state must be passed as the first argument to\nevery function in the library, except to <a href=\"#lua_newstate\"><code>lua_newstate</code></a>,\nwhich creates a Lua state from scratch.\n\n\n\n\n\n<hr><h3><a name=\"lua_status\"><code>lua_status</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>int lua_status (lua_State *L);</pre>\n\n<p>\nReturns the status of the thread <code>L</code>.\n\n\n<p>\nThe status can be 0 for a normal thread,\nan error code if the thread finished its execution with an error,\nor <a name=\"pdf-LUA_YIELD\"><code>LUA_YIELD</code></a> if the thread is suspended.\n\n\n\n\n\n<hr><h3><a name=\"lua_toboolean\"><code>lua_toboolean</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>int lua_toboolean (lua_State *L, int index);</pre>\n\n<p>\nConverts the Lua value at the given acceptable index to a C&nbsp;boolean\nvalue (0&nbsp;or&nbsp;1).\nLike all tests in Lua,\n<a href=\"#lua_toboolean\"><code>lua_toboolean</code></a> returns 1 for any Lua value\ndifferent from <b>false</b> and <b>nil</b>;\notherwise it returns 0.\nIt also returns 0 when called with a non-valid index.\n(If you want to accept only actual boolean values,\nuse <a href=\"#lua_isboolean\"><code>lua_isboolean</code></a> to test the value's type.)\n\n\n\n\n\n<hr><h3><a name=\"lua_tocfunction\"><code>lua_tocfunction</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>lua_CFunction lua_tocfunction (lua_State *L, int index);</pre>\n\n<p>\nConverts a value at the given acceptable index to a C&nbsp;function.\nThat value must be a C&nbsp;function;\notherwise, returns <code>NULL</code>.\n\n\n\n\n\n<hr><h3><a name=\"lua_tointeger\"><code>lua_tointeger</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>lua_Integer lua_tointeger (lua_State *L, int index);</pre>\n\n<p>\nConverts the Lua value at the given acceptable index\nto the signed integral type <a href=\"#lua_Integer\"><code>lua_Integer</code></a>.\nThe Lua value must be a number or a string convertible to a number\n(see <a href=\"#2.2.1\">&sect;2.2.1</a>);\notherwise, <a href=\"#lua_tointeger\"><code>lua_tointeger</code></a> returns&nbsp;0.\n\n\n<p>\nIf the number is not an integer,\nit is truncated in some non-specified way.\n\n\n\n\n\n<hr><h3><a name=\"lua_tolstring\"><code>lua_tolstring</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>m</em>]</span>\n<pre>const char *lua_tolstring (lua_State *L, int index, size_t *len);</pre>\n\n<p>\nConverts the Lua value at the given acceptable index to a C&nbsp;string.\nIf <code>len</code> is not <code>NULL</code>,\nit also sets <code>*len</code> with the string length.\nThe Lua value must be a string or a number;\notherwise, the function returns <code>NULL</code>.\nIf the value is a number,\nthen <a href=\"#lua_tolstring\"><code>lua_tolstring</code></a> also\n<em>changes the actual value in the stack to a string</em>.\n(This change confuses <a href=\"#lua_next\"><code>lua_next</code></a>\nwhen <a href=\"#lua_tolstring\"><code>lua_tolstring</code></a> is applied to keys during a table traversal.)\n\n\n<p>\n<a href=\"#lua_tolstring\"><code>lua_tolstring</code></a> returns a fully aligned pointer\nto a string inside the Lua state.\nThis string always has a zero ('<code>\\0</code>')\nafter its last character (as in&nbsp;C),\nbut can contain other zeros in its body.\nBecause Lua has garbage collection,\nthere is no guarantee that the pointer returned by <a href=\"#lua_tolstring\"><code>lua_tolstring</code></a>\nwill be valid after the corresponding value is removed from the stack.\n\n\n\n\n\n<hr><h3><a name=\"lua_tonumber\"><code>lua_tonumber</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>lua_Number lua_tonumber (lua_State *L, int index);</pre>\n\n<p>\nConverts the Lua value at the given acceptable index\nto the C&nbsp;type <a href=\"#lua_Number\"><code>lua_Number</code></a> (see <a href=\"#lua_Number\"><code>lua_Number</code></a>).\nThe Lua value must be a number or a string convertible to a number\n(see <a href=\"#2.2.1\">&sect;2.2.1</a>);\notherwise, <a href=\"#lua_tonumber\"><code>lua_tonumber</code></a> returns&nbsp;0.\n\n\n\n\n\n<hr><h3><a name=\"lua_topointer\"><code>lua_topointer</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>const void *lua_topointer (lua_State *L, int index);</pre>\n\n<p>\nConverts the value at the given acceptable index to a generic\nC&nbsp;pointer (<code>void*</code>).\nThe value can be a userdata, a table, a thread, or a function;\notherwise, <a href=\"#lua_topointer\"><code>lua_topointer</code></a> returns <code>NULL</code>.\nDifferent objects will give different pointers.\nThere is no way to convert the pointer back to its original value.\n\n\n<p>\nTypically this function is used only for debug information.\n\n\n\n\n\n<hr><h3><a name=\"lua_tostring\"><code>lua_tostring</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>m</em>]</span>\n<pre>const char *lua_tostring (lua_State *L, int index);</pre>\n\n<p>\nEquivalent to <a href=\"#lua_tolstring\"><code>lua_tolstring</code></a> with <code>len</code> equal to <code>NULL</code>.\n\n\n\n\n\n<hr><h3><a name=\"lua_tothread\"><code>lua_tothread</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>lua_State *lua_tothread (lua_State *L, int index);</pre>\n\n<p>\nConverts the value at the given acceptable index to a Lua thread\n(represented as <code>lua_State*</code>).\nThis value must be a thread;\notherwise, the function returns <code>NULL</code>.\n\n\n\n\n\n<hr><h3><a name=\"lua_touserdata\"><code>lua_touserdata</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>void *lua_touserdata (lua_State *L, int index);</pre>\n\n<p>\nIf the value at the given acceptable index is a full userdata,\nreturns its block address.\nIf the value is a light userdata,\nreturns its pointer.\nOtherwise, returns <code>NULL</code>.\n\n\n\n\n\n<hr><h3><a name=\"lua_type\"><code>lua_type</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>int lua_type (lua_State *L, int index);</pre>\n\n<p>\nReturns the type of the value in the given acceptable index,\nor <code>LUA_TNONE</code> for a non-valid index\n(that is, an index to an \"empty\" stack position).\nThe types returned by <a href=\"#lua_type\"><code>lua_type</code></a> are coded by the following constants\ndefined in <code>lua.h</code>:\n<code>LUA_TNIL</code>,\n<code>LUA_TNUMBER</code>,\n<code>LUA_TBOOLEAN</code>,\n<code>LUA_TSTRING</code>,\n<code>LUA_TTABLE</code>,\n<code>LUA_TFUNCTION</code>,\n<code>LUA_TUSERDATA</code>,\n<code>LUA_TTHREAD</code>,\nand\n<code>LUA_TLIGHTUSERDATA</code>.\n\n\n\n\n\n<hr><h3><a name=\"lua_typename\"><code>lua_typename</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>const char *lua_typename  (lua_State *L, int tp);</pre>\n\n<p>\nReturns the name of the type encoded by the value <code>tp</code>,\nwhich must be one the values returned by <a href=\"#lua_type\"><code>lua_type</code></a>.\n\n\n\n\n\n<hr><h3><a name=\"lua_Writer\"><code>lua_Writer</code></a></h3>\n<pre>typedef int (*lua_Writer) (lua_State *L,\n                           const void* p,\n                           size_t sz,\n                           void* ud);</pre>\n\n<p>\nThe type of the writer function used by <a href=\"#lua_dump\"><code>lua_dump</code></a>.\nEvery time it produces another piece of chunk,\n<a href=\"#lua_dump\"><code>lua_dump</code></a> calls the writer,\npassing along the buffer to be written (<code>p</code>),\nits size (<code>sz</code>),\nand the <code>data</code> parameter supplied to <a href=\"#lua_dump\"><code>lua_dump</code></a>.\n\n\n<p>\nThe writer returns an error code:\n0&nbsp;means no errors;\nany other value means an error and stops <a href=\"#lua_dump\"><code>lua_dump</code></a> from\ncalling the writer again.\n\n\n\n\n\n<hr><h3><a name=\"lua_xmove\"><code>lua_xmove</code></a></h3><p>\n<span class=\"apii\">[-?, +?, <em>-</em>]</span>\n<pre>void lua_xmove (lua_State *from, lua_State *to, int n);</pre>\n\n<p>\nExchange values between different threads of the <em>same</em> global state.\n\n\n<p>\nThis function pops <code>n</code> values from the stack <code>from</code>,\nand pushes them onto the stack <code>to</code>.\n\n\n\n\n\n<hr><h3><a name=\"lua_yield\"><code>lua_yield</code></a></h3><p>\n<span class=\"apii\">[-?, +?, <em>-</em>]</span>\n<pre>int lua_yield  (lua_State *L, int nresults);</pre>\n\n<p>\nYields a coroutine.\n\n\n<p>\nThis function should only be called as the\nreturn expression of a C&nbsp;function, as follows:\n\n<pre>\n     return lua_yield (L, nresults);\n</pre><p>\nWhen a C&nbsp;function calls <a href=\"#lua_yield\"><code>lua_yield</code></a> in that way,\nthe running coroutine suspends its execution,\nand the call to <a href=\"#lua_resume\"><code>lua_resume</code></a> that started this coroutine returns.\nThe parameter <code>nresults</code> is the number of values from the stack\nthat are passed as results to <a href=\"#lua_resume\"><code>lua_resume</code></a>.\n\n\n\n\n\n\n\n<h2>3.8 - <a name=\"3.8\">The Debug Interface</a></h2>\n\n<p>\nLua has no built-in debugging facilities.\nInstead, it offers a special interface\nby means of functions and <em>hooks</em>.\nThis interface allows the construction of different\nkinds of debuggers, profilers, and other tools\nthat need \"inside information\" from the interpreter.\n\n\n\n<hr><h3><a name=\"lua_Debug\"><code>lua_Debug</code></a></h3>\n<pre>typedef struct lua_Debug {\n  int event;\n  const char *name;           /* (n) */\n  const char *namewhat;       /* (n) */\n  const char *what;           /* (S) */\n  const char *source;         /* (S) */\n  int currentline;            /* (l) */\n  int nups;                   /* (u) number of upvalues */\n  int linedefined;            /* (S) */\n  int lastlinedefined;        /* (S) */\n  char short_src[LUA_IDSIZE]; /* (S) */\n  /* private part */\n  <em>other fields</em>\n} lua_Debug;</pre>\n\n<p>\nA structure used to carry different pieces of\ninformation about an active function.\n<a href=\"#lua_getstack\"><code>lua_getstack</code></a> fills only the private part\nof this structure, for later use.\nTo fill the other fields of <a href=\"#lua_Debug\"><code>lua_Debug</code></a> with useful information,\ncall <a href=\"#lua_getinfo\"><code>lua_getinfo</code></a>.\n\n\n<p>\nThe fields of <a href=\"#lua_Debug\"><code>lua_Debug</code></a> have the following meaning:\n\n<ul>\n\n<li><b><code>source</code>:</b>\nIf the function was defined in a string,\nthen <code>source</code> is that string.\nIf the function was defined in a file,\nthen <code>source</code> starts with a '<code>@</code>' followed by the file name.\n</li>\n\n<li><b><code>short_src</code>:</b>\na \"printable\" version of <code>source</code>, to be used in error messages.\n</li>\n\n<li><b><code>linedefined</code>:</b>\nthe line number where the definition of the function starts.\n</li>\n\n<li><b><code>lastlinedefined</code>:</b>\nthe line number where the definition of the function ends.\n</li>\n\n<li><b><code>what</code>:</b>\nthe string <code>\"Lua\"</code> if the function is a Lua function,\n<code>\"C\"</code> if it is a C&nbsp;function,\n<code>\"main\"</code> if it is the main part of a chunk,\nand <code>\"tail\"</code> if it was a function that did a tail call.\nIn the latter case,\nLua has no other information about the function.\n</li>\n\n<li><b><code>currentline</code>:</b>\nthe current line where the given function is executing.\nWhen no line information is available,\n<code>currentline</code> is set to -1.\n</li>\n\n<li><b><code>name</code>:</b>\na reasonable name for the given function.\nBecause functions in Lua are first-class values,\nthey do not have a fixed name:\nsome functions can be the value of multiple global variables,\nwhile others can be stored only in a table field.\nThe <code>lua_getinfo</code> function checks how the function was\ncalled to find a suitable name.\nIf it cannot find a name,\nthen <code>name</code> is set to <code>NULL</code>.\n</li>\n\n<li><b><code>namewhat</code>:</b>\nexplains the <code>name</code> field.\nThe value of <code>namewhat</code> can be\n<code>\"global\"</code>, <code>\"local\"</code>, <code>\"method\"</code>,\n<code>\"field\"</code>, <code>\"upvalue\"</code>, or <code>\"\"</code> (the empty string),\naccording to how the function was called.\n(Lua uses the empty string when no other option seems to apply.)\n</li>\n\n<li><b><code>nups</code>:</b>\nthe number of upvalues of the function.\n</li>\n\n</ul>\n\n\n\n\n<hr><h3><a name=\"lua_gethook\"><code>lua_gethook</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>lua_Hook lua_gethook (lua_State *L);</pre>\n\n<p>\nReturns the current hook function.\n\n\n\n\n\n<hr><h3><a name=\"lua_gethookcount\"><code>lua_gethookcount</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>int lua_gethookcount (lua_State *L);</pre>\n\n<p>\nReturns the current hook count.\n\n\n\n\n\n<hr><h3><a name=\"lua_gethookmask\"><code>lua_gethookmask</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>int lua_gethookmask (lua_State *L);</pre>\n\n<p>\nReturns the current hook mask.\n\n\n\n\n\n<hr><h3><a name=\"lua_getinfo\"><code>lua_getinfo</code></a></h3><p>\n<span class=\"apii\">[-(0|1), +(0|1|2), <em>m</em>]</span>\n<pre>int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);</pre>\n\n<p>\nReturns information about a specific function or function invocation.\n\n\n<p>\nTo get information about a function invocation,\nthe parameter <code>ar</code> must be a valid activation record that was\nfilled by a previous call to <a href=\"#lua_getstack\"><code>lua_getstack</code></a> or\ngiven as argument to a hook (see <a href=\"#lua_Hook\"><code>lua_Hook</code></a>).\n\n\n<p>\nTo get information about a function you push it onto the stack\nand start the <code>what</code> string with the character '<code>&gt;</code>'.\n(In that case,\n<code>lua_getinfo</code> pops the function in the top of the stack.)\nFor instance, to know in which line a function <code>f</code> was defined,\nyou can write the following code:\n\n<pre>\n     lua_Debug ar;\n     lua_getfield(L, LUA_GLOBALSINDEX, \"f\");  /* get global 'f' */\n     lua_getinfo(L, \"&gt;S\", &amp;ar);\n     printf(\"%d\\n\", ar.linedefined);\n</pre>\n\n<p>\nEach character in the string <code>what</code>\nselects some fields of the structure <code>ar</code> to be filled or\na value to be pushed on the stack:\n\n<ul>\n\n<li><b>'<code>n</code>':</b> fills in the field <code>name</code> and <code>namewhat</code>;\n</li>\n\n<li><b>'<code>S</code>':</b>\nfills in the fields <code>source</code>, <code>short_src</code>,\n<code>linedefined</code>, <code>lastlinedefined</code>, and <code>what</code>;\n</li>\n\n<li><b>'<code>l</code>':</b> fills in the field <code>currentline</code>;\n</li>\n\n<li><b>'<code>u</code>':</b> fills in the field <code>nups</code>;\n</li>\n\n<li><b>'<code>f</code>':</b>\npushes onto the stack the function that is\nrunning at the given level;\n</li>\n\n<li><b>'<code>L</code>':</b>\npushes onto the stack a table whose indices are the\nnumbers of the lines that are valid on the function.\n(A <em>valid line</em> is a line with some associated code,\nthat is, a line where you can put a break point.\nNon-valid lines include empty lines and comments.)\n</li>\n\n</ul>\n\n<p>\nThis function returns 0 on error\n(for instance, an invalid option in <code>what</code>).\n\n\n\n\n\n<hr><h3><a name=\"lua_getlocal\"><code>lua_getlocal</code></a></h3><p>\n<span class=\"apii\">[-0, +(0|1), <em>-</em>]</span>\n<pre>const char *lua_getlocal (lua_State *L, lua_Debug *ar, int n);</pre>\n\n<p>\nGets information about a local variable of a given activation record.\nThe parameter <code>ar</code> must be a valid activation record that was\nfilled by a previous call to <a href=\"#lua_getstack\"><code>lua_getstack</code></a> or\ngiven as argument to a hook (see <a href=\"#lua_Hook\"><code>lua_Hook</code></a>).\nThe index <code>n</code> selects which local variable to inspect\n(1 is the first parameter or active local variable, and so on,\nuntil the last active local variable).\n<a href=\"#lua_getlocal\"><code>lua_getlocal</code></a> pushes the variable's value onto the stack\nand returns its name.\n\n\n<p>\nVariable names starting with '<code>(</code>' (open parentheses)\nrepresent internal variables\n(loop control variables, temporaries, and C&nbsp;function locals).\n\n\n<p>\nReturns <code>NULL</code> (and pushes nothing)\nwhen the index is greater than\nthe number of active local variables.\n\n\n\n\n\n<hr><h3><a name=\"lua_getstack\"><code>lua_getstack</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>int lua_getstack (lua_State *L, int level, lua_Debug *ar);</pre>\n\n<p>\nGet information about the interpreter runtime stack.\n\n\n<p>\nThis function fills parts of a <a href=\"#lua_Debug\"><code>lua_Debug</code></a> structure with\nan identification of the <em>activation record</em>\nof the function executing at a given level.\nLevel&nbsp;0 is the current running function,\nwhereas level <em>n+1</em> is the function that has called level <em>n</em>.\nWhen there are no errors, <a href=\"#lua_getstack\"><code>lua_getstack</code></a> returns 1;\nwhen called with a level greater than the stack depth,\nit returns 0.\n\n\n\n\n\n<hr><h3><a name=\"lua_getupvalue\"><code>lua_getupvalue</code></a></h3><p>\n<span class=\"apii\">[-0, +(0|1), <em>-</em>]</span>\n<pre>const char *lua_getupvalue (lua_State *L, int funcindex, int n);</pre>\n\n<p>\nGets information about a closure's upvalue.\n(For Lua functions,\nupvalues are the external local variables that the function uses,\nand that are consequently included in its closure.)\n<a href=\"#lua_getupvalue\"><code>lua_getupvalue</code></a> gets the index <code>n</code> of an upvalue,\npushes the upvalue's value onto the stack,\nand returns its name.\n<code>funcindex</code> points to the closure in the stack.\n(Upvalues have no particular order,\nas they are active through the whole function.\nSo, they are numbered in an arbitrary order.)\n\n\n<p>\nReturns <code>NULL</code> (and pushes nothing)\nwhen the index is greater than the number of upvalues.\nFor C&nbsp;functions, this function uses the empty string <code>\"\"</code>\nas a name for all upvalues.\n\n\n\n\n\n<hr><h3><a name=\"lua_Hook\"><code>lua_Hook</code></a></h3>\n<pre>typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);</pre>\n\n<p>\nType for debugging hook functions.\n\n\n<p>\nWhenever a hook is called, its <code>ar</code> argument has its field\n<code>event</code> set to the specific event that triggered the hook.\nLua identifies these events with the following constants:\n<a name=\"pdf-LUA_HOOKCALL\"><code>LUA_HOOKCALL</code></a>, <a name=\"pdf-LUA_HOOKRET\"><code>LUA_HOOKRET</code></a>,\n<a name=\"pdf-LUA_HOOKTAILRET\"><code>LUA_HOOKTAILRET</code></a>, <a name=\"pdf-LUA_HOOKLINE\"><code>LUA_HOOKLINE</code></a>,\nand <a name=\"pdf-LUA_HOOKCOUNT\"><code>LUA_HOOKCOUNT</code></a>.\nMoreover, for line events, the field <code>currentline</code> is also set.\nTo get the value of any other field in <code>ar</code>,\nthe hook must call <a href=\"#lua_getinfo\"><code>lua_getinfo</code></a>.\nFor return events, <code>event</code> can be <code>LUA_HOOKRET</code>,\nthe normal value, or <code>LUA_HOOKTAILRET</code>.\nIn the latter case, Lua is simulating a return from\na function that did a tail call;\nin this case, it is useless to call <a href=\"#lua_getinfo\"><code>lua_getinfo</code></a>.\n\n\n<p>\nWhile Lua is running a hook, it disables other calls to hooks.\nTherefore, if a hook calls back Lua to execute a function or a chunk,\nthis execution occurs without any calls to hooks.\n\n\n\n\n\n<hr><h3><a name=\"lua_sethook\"><code>lua_sethook</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>int lua_sethook (lua_State *L, lua_Hook f, int mask, int count);</pre>\n\n<p>\nSets the debugging hook function.\n\n\n<p>\nArgument <code>f</code> is the hook function.\n<code>mask</code> specifies on which events the hook will be called:\nit is formed by a bitwise or of the constants\n<a name=\"pdf-LUA_MASKCALL\"><code>LUA_MASKCALL</code></a>,\n<a name=\"pdf-LUA_MASKRET\"><code>LUA_MASKRET</code></a>,\n<a name=\"pdf-LUA_MASKLINE\"><code>LUA_MASKLINE</code></a>,\nand <a name=\"pdf-LUA_MASKCOUNT\"><code>LUA_MASKCOUNT</code></a>.\nThe <code>count</code> argument is only meaningful when the mask\nincludes <code>LUA_MASKCOUNT</code>.\nFor each event, the hook is called as explained below:\n\n<ul>\n\n<li><b>The call hook:</b> is called when the interpreter calls a function.\nThe hook is called just after Lua enters the new function,\nbefore the function gets its arguments.\n</li>\n\n<li><b>The return hook:</b> is called when the interpreter returns from a function.\nThe hook is called just before Lua leaves the function.\nYou have no access to the values to be returned by the function.\n</li>\n\n<li><b>The line hook:</b> is called when the interpreter is about to\nstart the execution of a new line of code,\nor when it jumps back in the code (even to the same line).\n(This event only happens while Lua is executing a Lua function.)\n</li>\n\n<li><b>The count hook:</b> is called after the interpreter executes every\n<code>count</code> instructions.\n(This event only happens while Lua is executing a Lua function.)\n</li>\n\n</ul>\n\n<p>\nA hook is disabled by setting <code>mask</code> to zero.\n\n\n\n\n\n<hr><h3><a name=\"lua_setlocal\"><code>lua_setlocal</code></a></h3><p>\n<span class=\"apii\">[-(0|1), +0, <em>-</em>]</span>\n<pre>const char *lua_setlocal (lua_State *L, lua_Debug *ar, int n);</pre>\n\n<p>\nSets the value of a local variable of a given activation record.\nParameters <code>ar</code> and <code>n</code> are as in <a href=\"#lua_getlocal\"><code>lua_getlocal</code></a>\n(see <a href=\"#lua_getlocal\"><code>lua_getlocal</code></a>).\n<a href=\"#lua_setlocal\"><code>lua_setlocal</code></a> assigns the value at the top of the stack\nto the variable and returns its name.\nIt also pops the value from the stack.\n\n\n<p>\nReturns <code>NULL</code> (and pops nothing)\nwhen the index is greater than\nthe number of active local variables.\n\n\n\n\n\n<hr><h3><a name=\"lua_setupvalue\"><code>lua_setupvalue</code></a></h3><p>\n<span class=\"apii\">[-(0|1), +0, <em>-</em>]</span>\n<pre>const char *lua_setupvalue (lua_State *L, int funcindex, int n);</pre>\n\n<p>\nSets the value of a closure's upvalue.\nIt assigns the value at the top of the stack\nto the upvalue and returns its name.\nIt also pops the value from the stack.\nParameters <code>funcindex</code> and <code>n</code> are as in the <a href=\"#lua_getupvalue\"><code>lua_getupvalue</code></a>\n(see <a href=\"#lua_getupvalue\"><code>lua_getupvalue</code></a>).\n\n\n<p>\nReturns <code>NULL</code> (and pops nothing)\nwhen the index is greater than the number of upvalues.\n\n\n\n\n\n\n\n<h1>4 - <a name=\"4\">The Auxiliary Library</a></h1>\n\n<p>\n\nThe <em>auxiliary library</em> provides several convenient functions\nto interface C with Lua.\nWhile the basic API provides the primitive functions for all \ninteractions between C and Lua,\nthe auxiliary library provides higher-level functions for some\ncommon tasks.\n\n\n<p>\nAll functions from the auxiliary library\nare defined in header file <code>lauxlib.h</code> and\nhave a prefix <code>luaL_</code>.\n\n\n<p>\nAll functions in the auxiliary library are built on\ntop of the basic API,\nand so they provide nothing that cannot be done with this API.\n\n\n<p>\nSeveral functions in the auxiliary library are used to\ncheck C&nbsp;function arguments.\nTheir names are always <code>luaL_check*</code> or <code>luaL_opt*</code>.\nAll of these functions throw an error if the check is not satisfied.\nBecause the error message is formatted for arguments\n(e.g., \"<code>bad argument #1</code>\"),\nyou should not use these functions for other stack values.\n\n\n\n<h2>4.1 - <a name=\"4.1\">Functions and Types</a></h2>\n\n<p>\nHere we list all functions and types from the auxiliary library\nin alphabetical order.\n\n\n\n<hr><h3><a name=\"luaL_addchar\"><code>luaL_addchar</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>m</em>]</span>\n<pre>void luaL_addchar (luaL_Buffer *B, char c);</pre>\n\n<p>\nAdds the character <code>c</code> to the buffer <code>B</code>\n(see <a href=\"#luaL_Buffer\"><code>luaL_Buffer</code></a>).\n\n\n\n\n\n<hr><h3><a name=\"luaL_addlstring\"><code>luaL_addlstring</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>m</em>]</span>\n<pre>void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);</pre>\n\n<p>\nAdds the string pointed to by <code>s</code> with length <code>l</code> to\nthe buffer <code>B</code>\n(see <a href=\"#luaL_Buffer\"><code>luaL_Buffer</code></a>).\nThe string may contain embedded zeros.\n\n\n\n\n\n<hr><h3><a name=\"luaL_addsize\"><code>luaL_addsize</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>m</em>]</span>\n<pre>void luaL_addsize (luaL_Buffer *B, size_t n);</pre>\n\n<p>\nAdds to the buffer <code>B</code> (see <a href=\"#luaL_Buffer\"><code>luaL_Buffer</code></a>)\na string of length <code>n</code> previously copied to the\nbuffer area (see <a href=\"#luaL_prepbuffer\"><code>luaL_prepbuffer</code></a>).\n\n\n\n\n\n<hr><h3><a name=\"luaL_addstring\"><code>luaL_addstring</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>m</em>]</span>\n<pre>void luaL_addstring (luaL_Buffer *B, const char *s);</pre>\n\n<p>\nAdds the zero-terminated string pointed to by <code>s</code>\nto the buffer <code>B</code>\n(see <a href=\"#luaL_Buffer\"><code>luaL_Buffer</code></a>).\nThe string may not contain embedded zeros.\n\n\n\n\n\n<hr><h3><a name=\"luaL_addvalue\"><code>luaL_addvalue</code></a></h3><p>\n<span class=\"apii\">[-1, +0, <em>m</em>]</span>\n<pre>void luaL_addvalue (luaL_Buffer *B);</pre>\n\n<p>\nAdds the value at the top of the stack\nto the buffer <code>B</code>\n(see <a href=\"#luaL_Buffer\"><code>luaL_Buffer</code></a>).\nPops the value.\n\n\n<p>\nThis is the only function on string buffers that can (and must)\nbe called with an extra element on the stack,\nwhich is the value to be added to the buffer.\n\n\n\n\n\n<hr><h3><a name=\"luaL_argcheck\"><code>luaL_argcheck</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>v</em>]</span>\n<pre>void luaL_argcheck (lua_State *L,\n                    int cond,\n                    int narg,\n                    const char *extramsg);</pre>\n\n<p>\nChecks whether <code>cond</code> is true.\nIf not, raises an error with the following message,\nwhere <code>func</code> is retrieved from the call stack:\n\n<pre>\n     bad argument #&lt;narg&gt; to &lt;func&gt; (&lt;extramsg&gt;)\n</pre>\n\n\n\n\n<hr><h3><a name=\"luaL_argerror\"><code>luaL_argerror</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>v</em>]</span>\n<pre>int luaL_argerror (lua_State *L, int narg, const char *extramsg);</pre>\n\n<p>\nRaises an error with the following message,\nwhere <code>func</code> is retrieved from the call stack:\n\n<pre>\n     bad argument #&lt;narg&gt; to &lt;func&gt; (&lt;extramsg&gt;)\n</pre>\n\n<p>\nThis function never returns,\nbut it is an idiom to use it in C&nbsp;functions\nas <code>return luaL_argerror(<em>args</em>)</code>.\n\n\n\n\n\n<hr><h3><a name=\"luaL_Buffer\"><code>luaL_Buffer</code></a></h3>\n<pre>typedef struct luaL_Buffer luaL_Buffer;</pre>\n\n<p>\nType for a <em>string buffer</em>.\n\n\n<p>\nA string buffer allows C&nbsp;code to build Lua strings piecemeal.\nIts pattern of use is as follows:\n\n<ul>\n\n<li>First you declare a variable <code>b</code> of type <a href=\"#luaL_Buffer\"><code>luaL_Buffer</code></a>.</li>\n\n<li>Then you initialize it with a call <code>luaL_buffinit(L, &amp;b)</code>.</li>\n\n<li>\nThen you add string pieces to the buffer calling any of\nthe <code>luaL_add*</code> functions.\n</li>\n\n<li>\nYou finish by calling <code>luaL_pushresult(&amp;b)</code>.\nThis call leaves the final string on the top of the stack.\n</li>\n\n</ul>\n\n<p>\nDuring its normal operation,\na string buffer uses a variable number of stack slots.\nSo, while using a buffer, you cannot assume that you know where\nthe top of the stack is.\nYou can use the stack between successive calls to buffer operations\nas long as that use is balanced;\nthat is,\nwhen you call a buffer operation,\nthe stack is at the same level\nit was immediately after the previous buffer operation.\n(The only exception to this rule is <a href=\"#luaL_addvalue\"><code>luaL_addvalue</code></a>.)\nAfter calling <a href=\"#luaL_pushresult\"><code>luaL_pushresult</code></a> the stack is back to its\nlevel when the buffer was initialized,\nplus the final string on its top.\n\n\n\n\n\n<hr><h3><a name=\"luaL_buffinit\"><code>luaL_buffinit</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>void luaL_buffinit (lua_State *L, luaL_Buffer *B);</pre>\n\n<p>\nInitializes a buffer <code>B</code>.\nThis function does not allocate any space;\nthe buffer must be declared as a variable\n(see <a href=\"#luaL_Buffer\"><code>luaL_Buffer</code></a>).\n\n\n\n\n\n<hr><h3><a name=\"luaL_callmeta\"><code>luaL_callmeta</code></a></h3><p>\n<span class=\"apii\">[-0, +(0|1), <em>e</em>]</span>\n<pre>int luaL_callmeta (lua_State *L, int obj, const char *e);</pre>\n\n<p>\nCalls a metamethod.\n\n\n<p>\nIf the object at index <code>obj</code> has a metatable and this\nmetatable has a field <code>e</code>,\nthis function calls this field and passes the object as its only argument.\nIn this case this function returns 1 and pushes onto the\nstack the value returned by the call.\nIf there is no metatable or no metamethod,\nthis function returns 0 (without pushing any value on the stack).\n\n\n\n\n\n<hr><h3><a name=\"luaL_checkany\"><code>luaL_checkany</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>v</em>]</span>\n<pre>void luaL_checkany (lua_State *L, int narg);</pre>\n\n<p>\nChecks whether the function has an argument\nof any type (including <b>nil</b>) at position <code>narg</code>.\n\n\n\n\n\n<hr><h3><a name=\"luaL_checkint\"><code>luaL_checkint</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>v</em>]</span>\n<pre>int luaL_checkint (lua_State *L, int narg);</pre>\n\n<p>\nChecks whether the function argument <code>narg</code> is a number\nand returns this number cast to an <code>int</code>.\n\n\n\n\n\n<hr><h3><a name=\"luaL_checkinteger\"><code>luaL_checkinteger</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>v</em>]</span>\n<pre>lua_Integer luaL_checkinteger (lua_State *L, int narg);</pre>\n\n<p>\nChecks whether the function argument <code>narg</code> is a number\nand returns this number cast to a <a href=\"#lua_Integer\"><code>lua_Integer</code></a>.\n\n\n\n\n\n<hr><h3><a name=\"luaL_checklong\"><code>luaL_checklong</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>v</em>]</span>\n<pre>long luaL_checklong (lua_State *L, int narg);</pre>\n\n<p>\nChecks whether the function argument <code>narg</code> is a number\nand returns this number cast to a <code>long</code>.\n\n\n\n\n\n<hr><h3><a name=\"luaL_checklstring\"><code>luaL_checklstring</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>v</em>]</span>\n<pre>const char *luaL_checklstring (lua_State *L, int narg, size_t *l);</pre>\n\n<p>\nChecks whether the function argument <code>narg</code> is a string\nand returns this string;\nif <code>l</code> is not <code>NULL</code> fills <code>*l</code>\nwith the string's length.\n\n\n<p>\nThis function uses <a href=\"#lua_tolstring\"><code>lua_tolstring</code></a> to get its result,\nso all conversions and caveats of that function apply here.\n\n\n\n\n\n<hr><h3><a name=\"luaL_checknumber\"><code>luaL_checknumber</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>v</em>]</span>\n<pre>lua_Number luaL_checknumber (lua_State *L, int narg);</pre>\n\n<p>\nChecks whether the function argument <code>narg</code> is a number\nand returns this number.\n\n\n\n\n\n<hr><h3><a name=\"luaL_checkoption\"><code>luaL_checkoption</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>v</em>]</span>\n<pre>int luaL_checkoption (lua_State *L,\n                      int narg,\n                      const char *def,\n                      const char *const lst[]);</pre>\n\n<p>\nChecks whether the function argument <code>narg</code> is a string and\nsearches for this string in the array <code>lst</code>\n(which must be NULL-terminated).\nReturns the index in the array where the string was found.\nRaises an error if the argument is not a string or\nif the string cannot be found.\n\n\n<p>\nIf <code>def</code> is not <code>NULL</code>,\nthe function uses <code>def</code> as a default value when\nthere is no argument <code>narg</code> or if this argument is <b>nil</b>.\n\n\n<p>\nThis is a useful function for mapping strings to C&nbsp;enums.\n(The usual convention in Lua libraries is\nto use strings instead of numbers to select options.)\n\n\n\n\n\n<hr><h3><a name=\"luaL_checkstack\"><code>luaL_checkstack</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>v</em>]</span>\n<pre>void luaL_checkstack (lua_State *L, int sz, const char *msg);</pre>\n\n<p>\nGrows the stack size to <code>top + sz</code> elements,\nraising an error if the stack cannot grow to that size.\n<code>msg</code> is an additional text to go into the error message.\n\n\n\n\n\n<hr><h3><a name=\"luaL_checkstring\"><code>luaL_checkstring</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>v</em>]</span>\n<pre>const char *luaL_checkstring (lua_State *L, int narg);</pre>\n\n<p>\nChecks whether the function argument <code>narg</code> is a string\nand returns this string.\n\n\n<p>\nThis function uses <a href=\"#lua_tolstring\"><code>lua_tolstring</code></a> to get its result,\nso all conversions and caveats of that function apply here.\n\n\n\n\n\n<hr><h3><a name=\"luaL_checktype\"><code>luaL_checktype</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>v</em>]</span>\n<pre>void luaL_checktype (lua_State *L, int narg, int t);</pre>\n\n<p>\nChecks whether the function argument <code>narg</code> has type <code>t</code>.\nSee <a href=\"#lua_type\"><code>lua_type</code></a> for the encoding of types for <code>t</code>.\n\n\n\n\n\n<hr><h3><a name=\"luaL_checkudata\"><code>luaL_checkudata</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>v</em>]</span>\n<pre>void *luaL_checkudata (lua_State *L, int narg, const char *tname);</pre>\n\n<p>\nChecks whether the function argument <code>narg</code> is a userdata\nof the type <code>tname</code> (see <a href=\"#luaL_newmetatable\"><code>luaL_newmetatable</code></a>).\n\n\n\n\n\n<hr><h3><a name=\"luaL_dofile\"><code>luaL_dofile</code></a></h3><p>\n<span class=\"apii\">[-0, +?, <em>m</em>]</span>\n<pre>int luaL_dofile (lua_State *L, const char *filename);</pre>\n\n<p>\nLoads and runs the given file.\nIt is defined as the following macro:\n\n<pre>\n     (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0))\n</pre><p>\nIt returns 0 if there are no errors\nor 1 in case of errors.\n\n\n\n\n\n<hr><h3><a name=\"luaL_dostring\"><code>luaL_dostring</code></a></h3><p>\n<span class=\"apii\">[-0, +?, <em>m</em>]</span>\n<pre>int luaL_dostring (lua_State *L, const char *str);</pre>\n\n<p>\nLoads and runs the given string.\nIt is defined as the following macro:\n\n<pre>\n     (luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0))\n</pre><p>\nIt returns 0 if there are no errors\nor 1 in case of errors.\n\n\n\n\n\n<hr><h3><a name=\"luaL_error\"><code>luaL_error</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>v</em>]</span>\n<pre>int luaL_error (lua_State *L, const char *fmt, ...);</pre>\n\n<p>\nRaises an error.\nThe error message format is given by <code>fmt</code>\nplus any extra arguments,\nfollowing the same rules of <a href=\"#lua_pushfstring\"><code>lua_pushfstring</code></a>.\nIt also adds at the beginning of the message the file name and\nthe line number where the error occurred,\nif this information is available.\n\n\n<p>\nThis function never returns,\nbut it is an idiom to use it in C&nbsp;functions\nas <code>return luaL_error(<em>args</em>)</code>.\n\n\n\n\n\n<hr><h3><a name=\"luaL_getmetafield\"><code>luaL_getmetafield</code></a></h3><p>\n<span class=\"apii\">[-0, +(0|1), <em>m</em>]</span>\n<pre>int luaL_getmetafield (lua_State *L, int obj, const char *e);</pre>\n\n<p>\nPushes onto the stack the field <code>e</code> from the metatable\nof the object at index <code>obj</code>.\nIf the object does not have a metatable,\nor if the metatable does not have this field,\nreturns 0 and pushes nothing.\n\n\n\n\n\n<hr><h3><a name=\"luaL_getmetatable\"><code>luaL_getmetatable</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>-</em>]</span>\n<pre>void luaL_getmetatable (lua_State *L, const char *tname);</pre>\n\n<p>\nPushes onto the stack the metatable associated with name <code>tname</code>\nin the registry (see <a href=\"#luaL_newmetatable\"><code>luaL_newmetatable</code></a>).\n\n\n\n\n\n<hr><h3><a name=\"luaL_gsub\"><code>luaL_gsub</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>m</em>]</span>\n<pre>const char *luaL_gsub (lua_State *L,\n                       const char *s,\n                       const char *p,\n                       const char *r);</pre>\n\n<p>\nCreates a copy of string <code>s</code> by replacing\nany occurrence of the string <code>p</code>\nwith the string <code>r</code>.\nPushes the resulting string on the stack and returns it.\n\n\n\n\n\n<hr><h3><a name=\"luaL_loadbuffer\"><code>luaL_loadbuffer</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>m</em>]</span>\n<pre>int luaL_loadbuffer (lua_State *L,\n                     const char *buff,\n                     size_t sz,\n                     const char *name);</pre>\n\n<p>\nLoads a buffer as a Lua chunk.\nThis function uses <a href=\"#lua_load\"><code>lua_load</code></a> to load the chunk in the\nbuffer pointed to by <code>buff</code> with size <code>sz</code>.\n\n\n<p>\nThis function returns the same results as <a href=\"#lua_load\"><code>lua_load</code></a>.\n<code>name</code> is the chunk name,\nused for debug information and error messages.\n\n\n\n\n\n<hr><h3><a name=\"luaL_loadfile\"><code>luaL_loadfile</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>m</em>]</span>\n<pre>int luaL_loadfile (lua_State *L, const char *filename);</pre>\n\n<p>\nLoads a file as a Lua chunk.\nThis function uses <a href=\"#lua_load\"><code>lua_load</code></a> to load the chunk in the file\nnamed <code>filename</code>.\nIf <code>filename</code> is <code>NULL</code>,\nthen it loads from the standard input.\nThe first line in the file is ignored if it starts with a <code>#</code>.\n\n\n<p>\nThis function returns the same results as <a href=\"#lua_load\"><code>lua_load</code></a>,\nbut it has an extra error code <a name=\"pdf-LUA_ERRFILE\"><code>LUA_ERRFILE</code></a>\nif it cannot open/read the file.\n\n\n<p>\nAs <a href=\"#lua_load\"><code>lua_load</code></a>, this function only loads the chunk;\nit does not run it.\n\n\n\n\n\n<hr><h3><a name=\"luaL_loadstring\"><code>luaL_loadstring</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>m</em>]</span>\n<pre>int luaL_loadstring (lua_State *L, const char *s);</pre>\n\n<p>\nLoads a string as a Lua chunk.\nThis function uses <a href=\"#lua_load\"><code>lua_load</code></a> to load the chunk in\nthe zero-terminated string <code>s</code>.\n\n\n<p>\nThis function returns the same results as <a href=\"#lua_load\"><code>lua_load</code></a>.\n\n\n<p>\nAlso as <a href=\"#lua_load\"><code>lua_load</code></a>, this function only loads the chunk;\nit does not run it.\n\n\n\n\n\n<hr><h3><a name=\"luaL_newmetatable\"><code>luaL_newmetatable</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>m</em>]</span>\n<pre>int luaL_newmetatable (lua_State *L, const char *tname);</pre>\n\n<p>\nIf the registry already has the key <code>tname</code>,\nreturns 0.\nOtherwise,\ncreates a new table to be used as a metatable for userdata,\nadds it to the registry with key <code>tname</code>,\nand returns 1.\n\n\n<p>\nIn both cases pushes onto the stack the final value associated\nwith <code>tname</code> in the registry.\n\n\n\n\n\n<hr><h3><a name=\"luaL_newstate\"><code>luaL_newstate</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>lua_State *luaL_newstate (void);</pre>\n\n<p>\nCreates a new Lua state.\nIt calls <a href=\"#lua_newstate\"><code>lua_newstate</code></a> with an\nallocator based on the standard&nbsp;C <code>realloc</code> function\nand then sets a panic function (see <a href=\"#lua_atpanic\"><code>lua_atpanic</code></a>) that prints\nan error message to the standard error output in case of fatal\nerrors.\n\n\n<p>\nReturns the new state,\nor <code>NULL</code> if there is a memory allocation error.\n\n\n\n\n\n<hr><h3><a name=\"luaL_openlibs\"><code>luaL_openlibs</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>m</em>]</span>\n<pre>void luaL_openlibs (lua_State *L);</pre>\n\n<p>\nOpens all standard Lua libraries into the given state.\n\n\n\n\n\n<hr><h3><a name=\"luaL_optint\"><code>luaL_optint</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>v</em>]</span>\n<pre>int luaL_optint (lua_State *L, int narg, int d);</pre>\n\n<p>\nIf the function argument <code>narg</code> is a number,\nreturns this number cast to an <code>int</code>.\nIf this argument is absent or is <b>nil</b>,\nreturns <code>d</code>.\nOtherwise, raises an error.\n\n\n\n\n\n<hr><h3><a name=\"luaL_optinteger\"><code>luaL_optinteger</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>v</em>]</span>\n<pre>lua_Integer luaL_optinteger (lua_State *L,\n                             int narg,\n                             lua_Integer d);</pre>\n\n<p>\nIf the function argument <code>narg</code> is a number,\nreturns this number cast to a <a href=\"#lua_Integer\"><code>lua_Integer</code></a>.\nIf this argument is absent or is <b>nil</b>,\nreturns <code>d</code>.\nOtherwise, raises an error.\n\n\n\n\n\n<hr><h3><a name=\"luaL_optlong\"><code>luaL_optlong</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>v</em>]</span>\n<pre>long luaL_optlong (lua_State *L, int narg, long d);</pre>\n\n<p>\nIf the function argument <code>narg</code> is a number,\nreturns this number cast to a <code>long</code>.\nIf this argument is absent or is <b>nil</b>,\nreturns <code>d</code>.\nOtherwise, raises an error.\n\n\n\n\n\n<hr><h3><a name=\"luaL_optlstring\"><code>luaL_optlstring</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>v</em>]</span>\n<pre>const char *luaL_optlstring (lua_State *L,\n                             int narg,\n                             const char *d,\n                             size_t *l);</pre>\n\n<p>\nIf the function argument <code>narg</code> is a string,\nreturns this string.\nIf this argument is absent or is <b>nil</b>,\nreturns <code>d</code>.\nOtherwise, raises an error.\n\n\n<p>\nIf <code>l</code> is not <code>NULL</code>,\nfills the position <code>*l</code> with the results's length.\n\n\n\n\n\n<hr><h3><a name=\"luaL_optnumber\"><code>luaL_optnumber</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>v</em>]</span>\n<pre>lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number d);</pre>\n\n<p>\nIf the function argument <code>narg</code> is a number,\nreturns this number.\nIf this argument is absent or is <b>nil</b>,\nreturns <code>d</code>.\nOtherwise, raises an error.\n\n\n\n\n\n<hr><h3><a name=\"luaL_optstring\"><code>luaL_optstring</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>v</em>]</span>\n<pre>const char *luaL_optstring (lua_State *L,\n                            int narg,\n                            const char *d);</pre>\n\n<p>\nIf the function argument <code>narg</code> is a string,\nreturns this string.\nIf this argument is absent or is <b>nil</b>,\nreturns <code>d</code>.\nOtherwise, raises an error.\n\n\n\n\n\n<hr><h3><a name=\"luaL_prepbuffer\"><code>luaL_prepbuffer</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>char *luaL_prepbuffer (luaL_Buffer *B);</pre>\n\n<p>\nReturns an address to a space of size <a name=\"pdf-LUAL_BUFFERSIZE\"><code>LUAL_BUFFERSIZE</code></a>\nwhere you can copy a string to be added to buffer <code>B</code>\n(see <a href=\"#luaL_Buffer\"><code>luaL_Buffer</code></a>).\nAfter copying the string into this space you must call\n<a href=\"#luaL_addsize\"><code>luaL_addsize</code></a> with the size of the string to actually add \nit to the buffer.\n\n\n\n\n\n<hr><h3><a name=\"luaL_pushresult\"><code>luaL_pushresult</code></a></h3><p>\n<span class=\"apii\">[-?, +1, <em>m</em>]</span>\n<pre>void luaL_pushresult (luaL_Buffer *B);</pre>\n\n<p>\nFinishes the use of buffer <code>B</code> leaving the final string on\nthe top of the stack.\n\n\n\n\n\n<hr><h3><a name=\"luaL_ref\"><code>luaL_ref</code></a></h3><p>\n<span class=\"apii\">[-1, +0, <em>m</em>]</span>\n<pre>int luaL_ref (lua_State *L, int t);</pre>\n\n<p>\nCreates and returns a <em>reference</em>,\nin the table at index <code>t</code>,\nfor the object at the top of the stack (and pops the object).\n\n\n<p>\nA reference is a unique integer key.\nAs long as you do not manually add integer keys into table <code>t</code>,\n<a href=\"#luaL_ref\"><code>luaL_ref</code></a> ensures the uniqueness of the key it returns.\nYou can retrieve an object referred by reference <code>r</code>\nby calling <code>lua_rawgeti(L, t, r)</code>.\nFunction <a href=\"#luaL_unref\"><code>luaL_unref</code></a> frees a reference and its associated object.\n\n\n<p>\nIf the object at the top of the stack is <b>nil</b>,\n<a href=\"#luaL_ref\"><code>luaL_ref</code></a> returns the constant <a name=\"pdf-LUA_REFNIL\"><code>LUA_REFNIL</code></a>.\nThe constant <a name=\"pdf-LUA_NOREF\"><code>LUA_NOREF</code></a> is guaranteed to be different\nfrom any reference returned by <a href=\"#luaL_ref\"><code>luaL_ref</code></a>.\n\n\n\n\n\n<hr><h3><a name=\"luaL_Reg\"><code>luaL_Reg</code></a></h3>\n<pre>typedef struct luaL_Reg {\n  const char *name;\n  lua_CFunction func;\n} luaL_Reg;</pre>\n\n<p>\nType for arrays of functions to be registered by\n<a href=\"#luaL_register\"><code>luaL_register</code></a>.\n<code>name</code> is the function name and <code>func</code> is a pointer to\nthe function.\nAny array of <a href=\"#luaL_Reg\"><code>luaL_Reg</code></a> must end with an sentinel entry\nin which both <code>name</code> and <code>func</code> are <code>NULL</code>.\n\n\n\n\n\n<hr><h3><a name=\"luaL_register\"><code>luaL_register</code></a></h3><p>\n<span class=\"apii\">[-(0|1), +1, <em>m</em>]</span>\n<pre>void luaL_register (lua_State *L,\n                    const char *libname,\n                    const luaL_Reg *l);</pre>\n\n<p>\nOpens a library.\n\n\n<p>\nWhen called with <code>libname</code> equal to <code>NULL</code>,\nit simply registers all functions in the list <code>l</code>\n(see <a href=\"#luaL_Reg\"><code>luaL_Reg</code></a>) into the table on the top of the stack.\n\n\n<p>\nWhen called with a non-null <code>libname</code>,\n<code>luaL_register</code> creates a new table <code>t</code>,\nsets it as the value of the global variable <code>libname</code>,\nsets it as the value of <code>package.loaded[libname]</code>,\nand registers on it all functions in the list <code>l</code>.\nIf there is a table in <code>package.loaded[libname]</code> or in\nvariable <code>libname</code>,\nreuses this table instead of creating a new one.\n\n\n<p>\nIn any case the function leaves the table\non the top of the stack.\n\n\n\n\n\n<hr><h3><a name=\"luaL_typename\"><code>luaL_typename</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>const char *luaL_typename (lua_State *L, int index);</pre>\n\n<p>\nReturns the name of the type of the value at the given index.\n\n\n\n\n\n<hr><h3><a name=\"luaL_typerror\"><code>luaL_typerror</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>v</em>]</span>\n<pre>int luaL_typerror (lua_State *L, int narg, const char *tname);</pre>\n\n<p>\nGenerates an error with a message like the following:\n\n<pre>\n     <em>location</em>: bad argument <em>narg</em> to '<em>func</em>' (<em>tname</em> expected, got <em>rt</em>)\n</pre><p>\nwhere <code><em>location</em></code> is produced by <a href=\"#luaL_where\"><code>luaL_where</code></a>,\n<code><em>func</em></code> is the name of the current function,\nand <code><em>rt</em></code> is the type name of the actual argument.\n\n\n\n\n\n<hr><h3><a name=\"luaL_unref\"><code>luaL_unref</code></a></h3><p>\n<span class=\"apii\">[-0, +0, <em>-</em>]</span>\n<pre>void luaL_unref (lua_State *L, int t, int ref);</pre>\n\n<p>\nReleases reference <code>ref</code> from the table at index <code>t</code>\n(see <a href=\"#luaL_ref\"><code>luaL_ref</code></a>).\nThe entry is removed from the table,\nso that the referred object can be collected.\nThe reference <code>ref</code> is also freed to be used again.\n\n\n<p>\nIf <code>ref</code> is <a href=\"#pdf-LUA_NOREF\"><code>LUA_NOREF</code></a> or <a href=\"#pdf-LUA_REFNIL\"><code>LUA_REFNIL</code></a>,\n<a href=\"#luaL_unref\"><code>luaL_unref</code></a> does nothing.\n\n\n\n\n\n<hr><h3><a name=\"luaL_where\"><code>luaL_where</code></a></h3><p>\n<span class=\"apii\">[-0, +1, <em>m</em>]</span>\n<pre>void luaL_where (lua_State *L, int lvl);</pre>\n\n<p>\nPushes onto the stack a string identifying the current position\nof the control at level <code>lvl</code> in the call stack.\nTypically this string has the following format:\n\n<pre>\n     <em>chunkname</em>:<em>currentline</em>:\n</pre><p>\nLevel&nbsp;0 is the running function,\nlevel&nbsp;1 is the function that called the running function,\netc.\n\n\n<p>\nThis function is used to build a prefix for error messages.\n\n\n\n\n\n\n\n<h1>5 - <a name=\"5\">Standard Libraries</a></h1>\n\n<p>\nThe standard Lua libraries provide useful functions\nthat are implemented directly through the C&nbsp;API.\nSome of these functions provide essential services to the language\n(e.g., <a href=\"#pdf-type\"><code>type</code></a> and <a href=\"#pdf-getmetatable\"><code>getmetatable</code></a>);\nothers provide access to \"outside\" services (e.g., I/O);\nand others could be implemented in Lua itself,\nbut are quite useful or have critical performance requirements that\ndeserve an implementation in C (e.g., <a href=\"#pdf-table.sort\"><code>table.sort</code></a>).\n\n\n<p>\nAll libraries are implemented through the official C&nbsp;API\nand are provided as separate C&nbsp;modules.\nCurrently, Lua has the following standard libraries:\n\n<ul>\n\n<li>basic library, which includes the coroutine sub-library;</li>\n\n<li>package library;</li>\n\n<li>string manipulation;</li>\n\n<li>table manipulation;</li>\n\n<li>mathematical functions (sin, log, etc.);</li>\n\n<li>input and output;</li>\n\n<li>operating system facilities;</li>\n\n<li>debug facilities.</li>\n\n</ul><p>\nExcept for the basic and package libraries,\neach library provides all its functions as fields of a global table\nor as methods of its objects.\n\n\n<p>\nTo have access to these libraries,\nthe C&nbsp;host program should call the <a href=\"#luaL_openlibs\"><code>luaL_openlibs</code></a> function,\nwhich opens all standard libraries.\nAlternatively,\nit can open them individually by calling\n<a name=\"pdf-luaopen_base\"><code>luaopen_base</code></a> (for the basic library),\n<a name=\"pdf-luaopen_package\"><code>luaopen_package</code></a> (for the package library),\n<a name=\"pdf-luaopen_string\"><code>luaopen_string</code></a> (for the string library),\n<a name=\"pdf-luaopen_table\"><code>luaopen_table</code></a> (for the table library),\n<a name=\"pdf-luaopen_math\"><code>luaopen_math</code></a> (for the mathematical library),\n<a name=\"pdf-luaopen_io\"><code>luaopen_io</code></a> (for the I/O library),\n<a name=\"pdf-luaopen_os\"><code>luaopen_os</code></a> (for the Operating System library),\nand <a name=\"pdf-luaopen_debug\"><code>luaopen_debug</code></a> (for the debug library).\nThese functions are declared in <a name=\"pdf-lualib.h\"><code>lualib.h</code></a>\nand should not be called directly:\nyou must call them like any other Lua C&nbsp;function,\ne.g., by using <a href=\"#lua_call\"><code>lua_call</code></a>.\n\n\n\n<h2>5.1 - <a name=\"5.1\">Basic Functions</a></h2>\n\n<p>\nThe basic library provides some core functions to Lua.\nIf you do not include this library in your application,\nyou should check carefully whether you need to provide \nimplementations for some of its facilities.\n\n\n<p>\n<hr><h3><a name=\"pdf-assert\"><code>assert (v [, message])</code></a></h3>\nIssues an  error when\nthe value of its argument <code>v</code> is false (i.e., <b>nil</b> or <b>false</b>);\notherwise, returns all its arguments.\n<code>message</code> is an error message;\nwhen absent, it defaults to \"assertion failed!\"\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-collectgarbage\"><code>collectgarbage ([opt [, arg]])</code></a></h3>\n\n\n<p>\nThis function is a generic interface to the garbage collector.\nIt performs different functions according to its first argument, <code>opt</code>:\n\n<ul>\n\n<li><b>\"collect\":</b>\nperforms a full garbage-collection cycle.\nThis is the default option.\n</li>\n\n<li><b>\"stop\":</b>\nstops the garbage collector.\n</li>\n\n<li><b>\"restart\":</b>\nrestarts the garbage collector.\n</li>\n\n<li><b>\"count\":</b>\nreturns the total memory in use by Lua (in Kbytes).\n</li>\n\n<li><b>\"step\":</b>\nperforms a garbage-collection step.\nThe step \"size\" is controlled by <code>arg</code>\n(larger values mean more steps) in a non-specified way.\nIf you want to control the step size\nyou must experimentally tune the value of <code>arg</code>.\nReturns <b>true</b> if the step finished a collection cycle.\n</li>\n\n<li><b>\"setpause\":</b>\nsets <code>arg</code> as the new value for the <em>pause</em> of\nthe collector (see <a href=\"#2.10\">&sect;2.10</a>).\nReturns the previous value for <em>pause</em>.\n</li>\n\n<li><b>\"setstepmul\":</b>\nsets <code>arg</code> as the new value for the <em>step multiplier</em> of\nthe collector (see <a href=\"#2.10\">&sect;2.10</a>).\nReturns the previous value for <em>step</em>.\n</li>\n\n</ul>\n\n\n\n<p>\n<hr><h3><a name=\"pdf-dofile\"><code>dofile ([filename])</code></a></h3>\nOpens the named file and executes its contents as a Lua chunk.\nWhen called without arguments,\n<code>dofile</code> executes the contents of the standard input (<code>stdin</code>).\nReturns all values returned by the chunk.\nIn case of errors, <code>dofile</code> propagates the error\nto its caller (that is, <code>dofile</code> does not run in protected mode).\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-error\"><code>error (message [, level])</code></a></h3>\nTerminates the last protected function called\nand returns <code>message</code> as the error message.\nFunction <code>error</code> never returns.\n\n\n<p>\nUsually, <code>error</code> adds some information about the error position\nat the beginning of the message.\nThe <code>level</code> argument specifies how to get the error position.\nWith level&nbsp;1 (the default), the error position is where the\n<code>error</code> function was called.\nLevel&nbsp;2 points the error to where the function\nthat called <code>error</code> was called; and so on.\nPassing a level&nbsp;0 avoids the addition of error position information\nto the message.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-_G\"><code>_G</code></a></h3>\nA global variable (not a function) that\nholds the global environment (that is, <code>_G._G = _G</code>).\nLua itself does not use this variable;\nchanging its value does not affect any environment,\nnor vice-versa.\n(Use <a href=\"#pdf-setfenv\"><code>setfenv</code></a> to change environments.)\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-getfenv\"><code>getfenv ([f])</code></a></h3>\nReturns the current environment in use by the function.\n<code>f</code> can be a Lua function or a number\nthat specifies the function at that stack level:\nLevel&nbsp;1 is the function calling <code>getfenv</code>.\nIf the given function is not a Lua function,\nor if <code>f</code> is 0,\n<code>getfenv</code> returns the global environment.\nThe default for <code>f</code> is 1.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-getmetatable\"><code>getmetatable (object)</code></a></h3>\n\n\n<p>\nIf <code>object</code> does not have a metatable, returns <b>nil</b>.\nOtherwise,\nif the object's metatable has a <code>\"__metatable\"</code> field,\nreturns the associated value.\nOtherwise, returns the metatable of the given object.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-ipairs\"><code>ipairs (t)</code></a></h3>\n\n\n<p>\nReturns three values: an iterator function, the table <code>t</code>, and 0,\nso that the construction\n\n<pre>\n     for i,v in ipairs(t) do <em>body</em> end\n</pre><p>\nwill iterate over the pairs (<code>1,t[1]</code>), (<code>2,t[2]</code>), &middot;&middot;&middot;,\nup to the first integer key absent from the table.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-load\"><code>load (func [, chunkname])</code></a></h3>\n\n\n<p>\nLoads a chunk using function <code>func</code> to get its pieces.\nEach call to <code>func</code> must return a string that concatenates\nwith previous results.\nA return of an empty string, <b>nil</b>, or no value signals the end of the chunk.\n\n\n<p>\nIf there are no errors, \nreturns the compiled chunk as a function;\notherwise, returns <b>nil</b> plus the error message.\nThe environment of the returned function is the global environment.\n\n\n<p>\n<code>chunkname</code> is used as the chunk name for error messages\nand debug information.\nWhen absent,\nit defaults to \"<code>=(load)</code>\".\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-loadfile\"><code>loadfile ([filename])</code></a></h3>\n\n\n<p>\nSimilar to <a href=\"#pdf-load\"><code>load</code></a>,\nbut gets the chunk from file <code>filename</code>\nor from the standard input,\nif no file name is given.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-loadstring\"><code>loadstring (string [, chunkname])</code></a></h3>\n\n\n<p>\nSimilar to <a href=\"#pdf-load\"><code>load</code></a>,\nbut gets the chunk from the given string.\n\n\n<p>\nTo load and run a given string, use the idiom\n\n<pre>\n     assert(loadstring(s))()\n</pre>\n\n<p>\nWhen absent,\n<code>chunkname</code> defaults to the given string.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-next\"><code>next (table [, index])</code></a></h3>\n\n\n<p>\nAllows a program to traverse all fields of a table.\nIts first argument is a table and its second argument\nis an index in this table.\n<code>next</code> returns the next index of the table\nand its associated value.\nWhen called with <b>nil</b> as its second argument,\n<code>next</code> returns an initial index\nand its associated value.\nWhen called with the last index,\nor with <b>nil</b> in an empty table,\n<code>next</code> returns <b>nil</b>.\nIf the second argument is absent, then it is interpreted as <b>nil</b>.\nIn particular,\nyou can use <code>next(t)</code> to check whether a table is empty.\n\n\n<p>\nThe order in which the indices are enumerated is not specified,\n<em>even for numeric indices</em>.\n(To traverse a table in numeric order,\nuse a numerical <b>for</b> or the <a href=\"#pdf-ipairs\"><code>ipairs</code></a> function.)\n\n\n<p>\nThe behavior of <code>next</code> is <em>undefined</em> if,\nduring the traversal,\nyou assign any value to a non-existent field in the table.\nYou may however modify existing fields.\nIn particular, you may clear existing fields.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-pairs\"><code>pairs (t)</code></a></h3>\n\n\n<p>\nReturns three values: the <a href=\"#pdf-next\"><code>next</code></a> function, the table <code>t</code>, and <b>nil</b>,\nso that the construction\n\n<pre>\n     for k,v in pairs(t) do <em>body</em> end\n</pre><p>\nwill iterate over all key&ndash;value pairs of table <code>t</code>.\n\n\n<p>\nSee function <a href=\"#pdf-next\"><code>next</code></a> for the caveats of modifying\nthe table during its traversal.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-pcall\"><code>pcall (f, arg1, &middot;&middot;&middot;)</code></a></h3>\n\n\n<p>\nCalls function <code>f</code> with\nthe given arguments in <em>protected mode</em>.\nThis means that any error inside&nbsp;<code>f</code> is not propagated;\ninstead, <code>pcall</code> catches the error\nand returns a status code.\nIts first result is the status code (a boolean),\nwhich is true if the call succeeds without errors.\nIn such case, <code>pcall</code> also returns all results from the call,\nafter this first result.\nIn case of any error, <code>pcall</code> returns <b>false</b> plus the error message.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-print\"><code>print (&middot;&middot;&middot;)</code></a></h3>\nReceives any number of arguments,\nand prints their values to <code>stdout</code>,\nusing the <a href=\"#pdf-tostring\"><code>tostring</code></a> function to convert them to strings.\n<code>print</code> is not intended for formatted output,\nbut only as a quick way to show a value,\ntypically for debugging.\nFor formatted output, use <a href=\"#pdf-string.format\"><code>string.format</code></a>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-rawequal\"><code>rawequal (v1, v2)</code></a></h3>\nChecks whether <code>v1</code> is equal to <code>v2</code>,\nwithout invoking any metamethod.\nReturns a boolean.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-rawget\"><code>rawget (table, index)</code></a></h3>\nGets the real value of <code>table[index]</code>,\nwithout invoking any metamethod.\n<code>table</code> must be a table;\n<code>index</code> may be any value.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-rawset\"><code>rawset (table, index, value)</code></a></h3>\nSets the real value of <code>table[index]</code> to <code>value</code>,\nwithout invoking any metamethod.\n<code>table</code> must be a table,\n<code>index</code> any value different from <b>nil</b>,\nand <code>value</code> any Lua value.\n\n\n<p>\nThis function returns <code>table</code>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-select\"><code>select (index, &middot;&middot;&middot;)</code></a></h3>\n\n\n<p>\nIf <code>index</code> is a number,\nreturns all arguments after argument number <code>index</code>.\nOtherwise, <code>index</code> must be the string <code>\"#\"</code>,\nand <code>select</code> returns the total number of extra arguments it received.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-setfenv\"><code>setfenv (f, table)</code></a></h3>\n\n\n<p>\nSets the environment to be used by the given function.\n<code>f</code> can be a Lua function or a number\nthat specifies the function at that stack level:\nLevel&nbsp;1 is the function calling <code>setfenv</code>.\n<code>setfenv</code> returns the given function.\n\n\n<p>\nAs a special case, when <code>f</code> is 0 <code>setfenv</code> changes\nthe environment of the running thread.\nIn this case, <code>setfenv</code> returns no values.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-setmetatable\"><code>setmetatable (table, metatable)</code></a></h3>\n\n\n<p>\nSets the metatable for the given table.\n(You cannot change the metatable of other types from Lua, only from&nbsp;C.)\nIf <code>metatable</code> is <b>nil</b>,\nremoves the metatable of the given table.\nIf the original metatable has a <code>\"__metatable\"</code> field,\nraises an error.\n\n\n<p>\nThis function returns <code>table</code>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-tonumber\"><code>tonumber (e [, base])</code></a></h3>\nTries to convert its argument to a number.\nIf the argument is already a number or a string convertible\nto a number, then <code>tonumber</code> returns this number;\notherwise, it returns <b>nil</b>.\n\n\n<p>\nAn optional argument specifies the base to interpret the numeral.\nThe base may be any integer between 2 and 36, inclusive.\nIn bases above&nbsp;10, the letter '<code>A</code>' (in either upper or lower case)\nrepresents&nbsp;10, '<code>B</code>' represents&nbsp;11, and so forth,\nwith '<code>Z</code>' representing 35.\nIn base 10 (the default), the number can have a decimal part,\nas well as an optional exponent part (see <a href=\"#2.1\">&sect;2.1</a>).\nIn other bases, only unsigned integers are accepted.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-tostring\"><code>tostring (e)</code></a></h3>\nReceives an argument of any type and\nconverts it to a string in a reasonable format.\nFor complete control of how numbers are converted,\nuse <a href=\"#pdf-string.format\"><code>string.format</code></a>.\n\n\n<p>\nIf the metatable of <code>e</code> has a <code>\"__tostring\"</code> field,\nthen <code>tostring</code> calls the corresponding value\nwith <code>e</code> as argument,\nand uses the result of the call as its result.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-type\"><code>type (v)</code></a></h3>\nReturns the type of its only argument, coded as a string.\nThe possible results of this function are\n\"<code>nil</code>\" (a string, not the value <b>nil</b>),\n\"<code>number</code>\",\n\"<code>string</code>\",\n\"<code>boolean</code>\",\n\"<code>table</code>\",\n\"<code>function</code>\",\n\"<code>thread</code>\",\nand \"<code>userdata</code>\".\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-unpack\"><code>unpack (list [, i [, j]])</code></a></h3>\nReturns the elements from the given table.\nThis function is equivalent to\n\n<pre>\n     return list[i], list[i+1], &middot;&middot;&middot;, list[j]\n</pre><p>\nexcept that the above code can be written only for a fixed number\nof elements.\nBy default, <code>i</code> is&nbsp;1 and <code>j</code> is the length of the list,\nas defined by the length operator (see <a href=\"#2.5.5\">&sect;2.5.5</a>).\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-_VERSION\"><code>_VERSION</code></a></h3>\nA global variable (not a function) that\nholds a string containing the current interpreter version.\nThe current contents of this variable is \"<code>Lua 5.1</code>\".\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-xpcall\"><code>xpcall (f, err)</code></a></h3>\n\n\n<p>\nThis function is similar to <a href=\"#pdf-pcall\"><code>pcall</code></a>,\nexcept that you can set a new error handler.\n\n\n<p>\n<code>xpcall</code> calls function <code>f</code> in protected mode,\nusing <code>err</code> as the error handler.\nAny error inside <code>f</code> is not propagated;\ninstead, <code>xpcall</code> catches the error,\ncalls the <code>err</code> function with the original error object,\nand returns a status code.\nIts first result is the status code (a boolean),\nwhich is true if the call succeeds without errors.\nIn this case, <code>xpcall</code> also returns all results from the call,\nafter this first result.\nIn case of any error,\n<code>xpcall</code> returns <b>false</b> plus the result from <code>err</code>.\n\n\n\n\n\n\n\n<h2>5.2 - <a name=\"5.2\">Coroutine Manipulation</a></h2>\n\n<p>\nThe operations related to coroutines comprise a sub-library of\nthe basic library and come inside the table <a name=\"pdf-coroutine\"><code>coroutine</code></a>.\nSee <a href=\"#2.11\">&sect;2.11</a> for a general description of coroutines.\n\n\n<p>\n<hr><h3><a name=\"pdf-coroutine.create\"><code>coroutine.create (f)</code></a></h3>\n\n\n<p>\nCreates a new coroutine, with body <code>f</code>.\n<code>f</code> must be a Lua function.\nReturns this new coroutine,\nan object with type <code>\"thread\"</code>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-coroutine.resume\"><code>coroutine.resume (co [, val1, &middot;&middot;&middot;])</code></a></h3>\n\n\n<p>\nStarts or continues the execution of coroutine <code>co</code>.\nThe first time you resume a coroutine,\nit starts running its body.\nThe values <code>val1</code>, &middot;&middot;&middot; are passed\nas the arguments to the body function.\nIf the coroutine has yielded,\n<code>resume</code> restarts it;\nthe values <code>val1</code>, &middot;&middot;&middot; are passed\nas the results from the yield.\n\n\n<p>\nIf the coroutine runs without any errors,\n<code>resume</code> returns <b>true</b> plus any values passed to <code>yield</code>\n(if the coroutine yields) or any values returned by the body function\n(if the coroutine terminates).\nIf there is any error,\n<code>resume</code> returns <b>false</b> plus the error message.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-coroutine.running\"><code>coroutine.running ()</code></a></h3>\n\n\n<p>\nReturns the running coroutine,\nor <b>nil</b> when called by the main thread.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-coroutine.status\"><code>coroutine.status (co)</code></a></h3>\n\n\n<p>\nReturns the status of coroutine <code>co</code>, as a string:\n<code>\"running\"</code>,\nif the coroutine is running (that is, it called <code>status</code>);\n<code>\"suspended\"</code>, if the coroutine is suspended in a call to <code>yield</code>,\nor if it has not started running yet;\n<code>\"normal\"</code> if the coroutine is active but not running\n(that is, it has resumed another coroutine);\nand <code>\"dead\"</code> if the coroutine has finished its body function,\nor if it has stopped with an error.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-coroutine.wrap\"><code>coroutine.wrap (f)</code></a></h3>\n\n\n<p>\nCreates a new coroutine, with body <code>f</code>.\n<code>f</code> must be a Lua function.\nReturns a function that resumes the coroutine each time it is called.\nAny arguments passed to the function behave as the\nextra arguments to <code>resume</code>.\nReturns the same values returned by <code>resume</code>,\nexcept the first boolean.\nIn case of error, propagates the error.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-coroutine.yield\"><code>coroutine.yield (&middot;&middot;&middot;)</code></a></h3>\n\n\n<p>\nSuspends the execution of the calling coroutine.\nThe coroutine cannot be running a C&nbsp;function,\na metamethod, or an iterator.\nAny arguments to <code>yield</code> are passed as extra results to <code>resume</code>.\n\n\n\n\n\n\n\n<h2>5.3 - <a name=\"5.3\">Modules</a></h2>\n\n<p>\nThe package library provides basic\nfacilities for loading and building modules in Lua.\nIt exports two of its functions directly in the global environment:\n<a href=\"#pdf-require\"><code>require</code></a> and <a href=\"#pdf-module\"><code>module</code></a>.\nEverything else is exported in a table <a name=\"pdf-package\"><code>package</code></a>.\n\n\n<p>\n<hr><h3><a name=\"pdf-module\"><code>module (name [, &middot;&middot;&middot;])</code></a></h3>\n\n\n<p>\nCreates a module.\nIf there is a table in <code>package.loaded[name]</code>,\nthis table is the module.\nOtherwise, if there is a global table <code>t</code> with the given name,\nthis table is the module.\nOtherwise creates a new table <code>t</code> and\nsets it as the value of the global <code>name</code> and\nthe value of <code>package.loaded[name]</code>.\nThis function also initializes <code>t._NAME</code> with the given name,\n<code>t._M</code> with the module (<code>t</code> itself),\nand <code>t._PACKAGE</code> with the package name\n(the full module name minus last component; see below).\nFinally, <code>module</code> sets <code>t</code> as the new environment\nof the current function and the new value of <code>package.loaded[name]</code>,\nso that <a href=\"#pdf-require\"><code>require</code></a> returns <code>t</code>.\n\n\n<p>\nIf <code>name</code> is a compound name\n(that is, one with components separated by dots),\n<code>module</code> creates (or reuses, if they already exist)\ntables for each component.\nFor instance, if <code>name</code> is <code>a.b.c</code>,\nthen <code>module</code> stores the module table in field <code>c</code> of\nfield <code>b</code> of global <code>a</code>.\n\n\n<p>\nThis function can receive optional <em>options</em> after\nthe module name,\nwhere each option is a function to be applied over the module.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-require\"><code>require (modname)</code></a></h3>\n\n\n<p>\nLoads the given module.\nThe function starts by looking into the <a href=\"#pdf-package.loaded\"><code>package.loaded</code></a> table\nto determine whether <code>modname</code> is already loaded.\nIf it is, then <code>require</code> returns the value stored\nat <code>package.loaded[modname]</code>.\nOtherwise, it tries to find a <em>loader</em> for the module.\n\n\n<p>\nTo find a loader,\n<code>require</code> is guided by the <a href=\"#pdf-package.loaders\"><code>package.loaders</code></a> array.\nBy changing this array,\nwe can change how <code>require</code> looks for a module.\nThe following explanation is based on the default configuration\nfor <a href=\"#pdf-package.loaders\"><code>package.loaders</code></a>.\n\n\n<p>\nFirst <code>require</code> queries <code>package.preload[modname]</code>.\nIf it has a value,\nthis value (which should be a function) is the loader.\nOtherwise <code>require</code> searches for a Lua loader using the\npath stored in <a href=\"#pdf-package.path\"><code>package.path</code></a>.\nIf that also fails, it searches for a C&nbsp;loader using the\npath stored in <a href=\"#pdf-package.cpath\"><code>package.cpath</code></a>.\nIf that also fails,\nit tries an <em>all-in-one</em> loader (see <a href=\"#pdf-package.loaders\"><code>package.loaders</code></a>).\n\n\n<p>\nOnce a loader is found,\n<code>require</code> calls the loader with a single argument, <code>modname</code>.\nIf the loader returns any value,\n<code>require</code> assigns the returned value to <code>package.loaded[modname]</code>.\nIf the loader returns no value and\nhas not assigned any value to <code>package.loaded[modname]</code>,\nthen <code>require</code> assigns <b>true</b> to this entry.\nIn any case, <code>require</code> returns the\nfinal value of <code>package.loaded[modname]</code>.\n\n\n<p>\nIf there is any error loading or running the module,\nor if it cannot find any loader for the module,\nthen <code>require</code> signals an error. \n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-package.cpath\"><code>package.cpath</code></a></h3>\n\n\n<p>\nThe path used by <a href=\"#pdf-require\"><code>require</code></a> to search for a C&nbsp;loader.\n\n\n<p>\nLua initializes the C&nbsp;path <a href=\"#pdf-package.cpath\"><code>package.cpath</code></a> in the same way\nit initializes the Lua path <a href=\"#pdf-package.path\"><code>package.path</code></a>,\nusing the environment variable <a name=\"pdf-LUA_CPATH\"><code>LUA_CPATH</code></a>\nor a default path defined in <code>luaconf.h</code>.\n\n\n\n\n<p>\n\n<hr><h3><a name=\"pdf-package.loaded\"><code>package.loaded</code></a></h3>\n\n\n<p>\nA table used by <a href=\"#pdf-require\"><code>require</code></a> to control which\nmodules are already loaded.\nWhen you require a module <code>modname</code> and\n<code>package.loaded[modname]</code> is not false,\n<a href=\"#pdf-require\"><code>require</code></a> simply returns the value stored there.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-package.loaders\"><code>package.loaders</code></a></h3>\n\n\n<p>\nA table used by <a href=\"#pdf-require\"><code>require</code></a> to control how to load modules.\n\n\n<p>\nEach entry in this table is a <em>searcher function</em>.\nWhen looking for a module,\n<a href=\"#pdf-require\"><code>require</code></a> calls each of these searchers in ascending order,\nwith the module name (the argument given to <a href=\"#pdf-require\"><code>require</code></a>) as its\nsole parameter.\nThe function can return another function (the module <em>loader</em>)\nor a string explaining why it did not find that module\n(or <b>nil</b> if it has nothing to say).\nLua initializes this table with four functions.\n\n\n<p>\nThe first searcher simply looks for a loader in the\n<a href=\"#pdf-package.preload\"><code>package.preload</code></a> table.\n\n\n<p>\nThe second searcher looks for a loader as a Lua library,\nusing the path stored at <a href=\"#pdf-package.path\"><code>package.path</code></a>.\nA path is a sequence of <em>templates</em> separated by semicolons.\nFor each template,\nthe searcher will change each interrogation\nmark in the template by <code>filename</code>,\nwhich is the module name with each dot replaced by a\n\"directory separator\" (such as \"<code>/</code>\" in Unix);\nthen it will try to open the resulting file name.\nSo, for instance, if the Lua path is the string\n\n<pre>\n     \"./?.lua;./?.lc;/usr/local/?/init.lua\"\n</pre><p>\nthe search for a Lua file for module <code>foo</code>\nwill try to open the files\n<code>./foo.lua</code>, <code>./foo.lc</code>, and\n<code>/usr/local/foo/init.lua</code>, in that order.\n\n\n<p>\nThe third searcher looks for a loader as a C&nbsp;library,\nusing the path given by the variable <a href=\"#pdf-package.cpath\"><code>package.cpath</code></a>.\nFor instance,\nif the C&nbsp;path is the string\n\n<pre>\n     \"./?.so;./?.dll;/usr/local/?/init.so\"\n</pre><p>\nthe searcher for module <code>foo</code>\nwill try to open the files <code>./foo.so</code>, <code>./foo.dll</code>,\nand <code>/usr/local/foo/init.so</code>, in that order.\nOnce it finds a C&nbsp;library,\nthis searcher first uses a dynamic link facility to link the\napplication with the library.\nThen it tries to find a C&nbsp;function inside the library to\nbe used as the loader.\nThe name of this C&nbsp;function is the string \"<code>luaopen_</code>\"\nconcatenated with a copy of the module name where each dot\nis replaced by an underscore.\nMoreover, if the module name has a hyphen,\nits prefix up to (and including) the first hyphen is removed.\nFor instance, if the module name is <code>a.v1-b.c</code>,\nthe function name will be <code>luaopen_b_c</code>.\n\n\n<p>\nThe fourth searcher tries an <em>all-in-one loader</em>.\nIt searches the C&nbsp;path for a library for\nthe root name of the given module.\nFor instance, when requiring <code>a.b.c</code>,\nit will search for a C&nbsp;library for <code>a</code>.\nIf found, it looks into it for an open function for\nthe submodule;\nin our example, that would be <code>luaopen_a_b_c</code>.\nWith this facility, a package can pack several C&nbsp;submodules\ninto one single library,\nwith each submodule keeping its original open function.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-package.loadlib\"><code>package.loadlib (libname, funcname)</code></a></h3>\n\n\n<p>\nDynamically links the host program with the C&nbsp;library <code>libname</code>.\nInside this library, looks for a function <code>funcname</code>\nand returns this function as a C&nbsp;function.\n(So, <code>funcname</code> must follow the protocol (see <a href=\"#lua_CFunction\"><code>lua_CFunction</code></a>)).\n\n\n<p>\nThis is a low-level function.\nIt completely bypasses the package and module system.\nUnlike <a href=\"#pdf-require\"><code>require</code></a>,\nit does not perform any path searching and\ndoes not automatically adds extensions.\n<code>libname</code> must be the complete file name of the C&nbsp;library,\nincluding if necessary a path and extension.\n<code>funcname</code> must be the exact name exported by the C&nbsp;library\n(which may depend on the C&nbsp;compiler and linker used).\n\n\n<p>\nThis function is not supported by ANSI C.\nAs such, it is only available on some platforms\n(Windows, Linux, Mac OS X, Solaris, BSD,\nplus other Unix systems that support the <code>dlfcn</code> standard).\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-package.path\"><code>package.path</code></a></h3>\n\n\n<p>\nThe path used by <a href=\"#pdf-require\"><code>require</code></a> to search for a Lua loader.\n\n\n<p>\nAt start-up, Lua initializes this variable with\nthe value of the environment variable <a name=\"pdf-LUA_PATH\"><code>LUA_PATH</code></a> or\nwith a default path defined in <code>luaconf.h</code>,\nif the environment variable is not defined.\nAny \"<code>;;</code>\" in the value of the environment variable\nis replaced by the default path.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-package.preload\"><code>package.preload</code></a></h3>\n\n\n<p>\nA table to store loaders for specific modules\n(see <a href=\"#pdf-require\"><code>require</code></a>).\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-package.seeall\"><code>package.seeall (module)</code></a></h3>\n\n\n<p>\nSets a metatable for <code>module</code> with\nits <code>__index</code> field referring to the global environment,\nso that this module inherits values\nfrom the global environment.\nTo be used as an option to function <a href=\"#pdf-module\"><code>module</code></a>.\n\n\n\n\n\n\n\n<h2>5.4 - <a name=\"5.4\">String Manipulation</a></h2>\n\n<p>\nThis library provides generic functions for string manipulation,\nsuch as finding and extracting substrings, and pattern matching.\nWhen indexing a string in Lua, the first character is at position&nbsp;1\n(not at&nbsp;0, as in C).\nIndices are allowed to be negative and are interpreted as indexing backwards,\nfrom the end of the string.\nThus, the last character is at position -1, and so on.\n\n\n<p>\nThe string library provides all its functions inside the table\n<a name=\"pdf-string\"><code>string</code></a>.\nIt also sets a metatable for strings\nwhere the <code>__index</code> field points to the <code>string</code> table.\nTherefore, you can use the string functions in object-oriented style.\nFor instance, <code>string.byte(s, i)</code>\ncan be written as <code>s:byte(i)</code>.\n\n\n<p>\nThe string library assumes one-byte character encodings.\n\n\n<p>\n<hr><h3><a name=\"pdf-string.byte\"><code>string.byte (s [, i [, j]])</code></a></h3>\nReturns the internal numerical codes of the characters <code>s[i]</code>,\n<code>s[i+1]</code>, &middot;&middot;&middot;, <code>s[j]</code>.\nThe default value for <code>i</code> is&nbsp;1;\nthe default value for <code>j</code> is&nbsp;<code>i</code>.\n\n\n<p>\nNote that numerical codes are not necessarily portable across platforms.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-string.char\"><code>string.char (&middot;&middot;&middot;)</code></a></h3>\nReceives zero or more integers.\nReturns a string with length equal to the number of arguments,\nin which each character has the internal numerical code equal\nto its corresponding argument.\n\n\n<p>\nNote that numerical codes are not necessarily portable across platforms.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-string.dump\"><code>string.dump (function)</code></a></h3>\n\n\n<p>\nReturns a string containing a binary representation of the given function,\nso that a later <a href=\"#pdf-loadstring\"><code>loadstring</code></a> on this string returns\na copy of the function.\n<code>function</code> must be a Lua function without upvalues.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-string.find\"><code>string.find (s, pattern [, init [, plain]])</code></a></h3>\nLooks for the first match of\n<code>pattern</code> in the string <code>s</code>.\nIf it finds a match, then <code>find</code> returns the indices of&nbsp;<code>s</code>\nwhere this occurrence starts and ends;\notherwise, it returns <b>nil</b>.\nA third, optional numerical argument <code>init</code> specifies\nwhere to start the search;\nits default value is&nbsp;1 and can be negative.\nA value of <b>true</b> as a fourth, optional argument <code>plain</code>\nturns off the pattern matching facilities,\nso the function does a plain \"find substring\" operation,\nwith no characters in <code>pattern</code> being considered \"magic\".\nNote that if <code>plain</code> is given, then <code>init</code> must be given as well.\n\n\n<p>\nIf the pattern has captures,\nthen in a successful match\nthe captured values are also returned,\nafter the two indices.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-string.format\"><code>string.format (formatstring, &middot;&middot;&middot;)</code></a></h3>\nReturns a formatted version of its variable number of arguments\nfollowing the description given in its first argument (which must be a string).\nThe format string follows the same rules as the <code>printf</code> family of\nstandard C&nbsp;functions.\nThe only differences are that the options/modifiers\n<code>*</code>, <code>l</code>, <code>L</code>, <code>n</code>, <code>p</code>,\nand <code>h</code> are not supported\nand that there is an extra option, <code>q</code>.\nThe <code>q</code> option formats a string in a form suitable to be safely read\nback by the Lua interpreter:\nthe string is written between double quotes,\nand all double quotes, newlines, embedded zeros,\nand backslashes in the string\nare correctly escaped when written.\nFor instance, the call\n\n<pre>\n     string.format('%q', 'a string with \"quotes\" and \\n new line')\n</pre><p>\nwill produce the string:\n\n<pre>\n     \"a string with \\\"quotes\\\" and \\\n      new line\"\n</pre>\n\n<p>\nThe options <code>c</code>, <code>d</code>, <code>E</code>, <code>e</code>, <code>f</code>,\n<code>g</code>, <code>G</code>, <code>i</code>, <code>o</code>, <code>u</code>, <code>X</code>, and <code>x</code> all\nexpect a number as argument,\nwhereas <code>q</code> and <code>s</code> expect a string.\n\n\n<p>\nThis function does not accept string values\ncontaining embedded zeros,\nexcept as arguments to the <code>q</code> option.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-string.gmatch\"><code>string.gmatch (s, pattern)</code></a></h3>\nReturns an iterator function that,\neach time it is called,\nreturns the next captures from <code>pattern</code> over string <code>s</code>.\nIf <code>pattern</code> specifies no captures,\nthen the whole match is produced in each call.\n\n\n<p>\nAs an example, the following loop\n\n<pre>\n     s = \"hello world from Lua\"\n     for w in string.gmatch(s, \"%a+\") do\n       print(w)\n     end\n</pre><p>\nwill iterate over all the words from string <code>s</code>,\nprinting one per line.\nThe next example collects all pairs <code>key=value</code> from the\ngiven string into a table:\n\n<pre>\n     t = {}\n     s = \"from=world, to=Lua\"\n     for k, v in string.gmatch(s, \"(%w+)=(%w+)\") do\n       t[k] = v\n     end\n</pre>\n\n<p>\nFor this function, a '<code>^</code>' at the start of a pattern does not\nwork as an anchor, as this would prevent the iteration.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-string.gsub\"><code>string.gsub (s, pattern, repl [, n])</code></a></h3>\nReturns a copy of <code>s</code>\nin which all (or the first <code>n</code>, if given)\noccurrences of the <code>pattern</code> have been\nreplaced by a replacement string specified by <code>repl</code>,\nwhich can be a string, a table, or a function.\n<code>gsub</code> also returns, as its second value,\nthe total number of matches that occurred.\n\n\n<p>\nIf <code>repl</code> is a string, then its value is used for replacement.\nThe character&nbsp;<code>%</code> works as an escape character:\nany sequence in <code>repl</code> of the form <code>%<em>n</em></code>,\nwith <em>n</em> between 1 and 9,\nstands for the value of the <em>n</em>-th captured substring (see below).\nThe sequence <code>%0</code> stands for the whole match.\nThe sequence <code>%%</code> stands for a single&nbsp;<code>%</code>.\n\n\n<p>\nIf <code>repl</code> is a table, then the table is queried for every match,\nusing the first capture as the key;\nif the pattern specifies no captures,\nthen the whole match is used as the key.\n\n\n<p>\nIf <code>repl</code> is a function, then this function is called every time a\nmatch occurs, with all captured substrings passed as arguments,\nin order;\nif the pattern specifies no captures,\nthen the whole match is passed as a sole argument.\n\n\n<p>\nIf the value returned by the table query or by the function call\nis a string or a number,\nthen it is used as the replacement string;\notherwise, if it is <b>false</b> or <b>nil</b>,\nthen there is no replacement\n(that is, the original match is kept in the string).\n\n\n<p>\nHere are some examples:\n\n<pre>\n     x = string.gsub(\"hello world\", \"(%w+)\", \"%1 %1\")\n     --&gt; x=\"hello hello world world\"\n     \n     x = string.gsub(\"hello world\", \"%w+\", \"%0 %0\", 1)\n     --&gt; x=\"hello hello world\"\n     \n     x = string.gsub(\"hello world from Lua\", \"(%w+)%s*(%w+)\", \"%2 %1\")\n     --&gt; x=\"world hello Lua from\"\n     \n     x = string.gsub(\"home = $HOME, user = $USER\", \"%$(%w+)\", os.getenv)\n     --&gt; x=\"home = /home/roberto, user = roberto\"\n     \n     x = string.gsub(\"4+5 = $return 4+5$\", \"%$(.-)%$\", function (s)\n           return loadstring(s)()\n         end)\n     --&gt; x=\"4+5 = 9\"\n     \n     local t = {name=\"lua\", version=\"5.1\"}\n     x = string.gsub(\"$name-$version.tar.gz\", \"%$(%w+)\", t)\n     --&gt; x=\"lua-5.1.tar.gz\"\n</pre>\n\n\n\n<p>\n<hr><h3><a name=\"pdf-string.len\"><code>string.len (s)</code></a></h3>\nReceives a string and returns its length.\nThe empty string <code>\"\"</code> has length 0.\nEmbedded zeros are counted,\nso <code>\"a\\000bc\\000\"</code> has length 5.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-string.lower\"><code>string.lower (s)</code></a></h3>\nReceives a string and returns a copy of this string with all\nuppercase letters changed to lowercase.\nAll other characters are left unchanged.\nThe definition of what an uppercase letter is depends on the current locale.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-string.match\"><code>string.match (s, pattern [, init])</code></a></h3>\nLooks for the first <em>match</em> of\n<code>pattern</code> in the string <code>s</code>.\nIf it finds one, then <code>match</code> returns\nthe captures from the pattern;\notherwise it returns <b>nil</b>.\nIf <code>pattern</code> specifies no captures,\nthen the whole match is returned.\nA third, optional numerical argument <code>init</code> specifies\nwhere to start the search;\nits default value is&nbsp;1 and can be negative.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-string.rep\"><code>string.rep (s, n)</code></a></h3>\nReturns a string that is the concatenation of <code>n</code> copies of\nthe string <code>s</code>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-string.reverse\"><code>string.reverse (s)</code></a></h3>\nReturns a string that is the string <code>s</code> reversed.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-string.sub\"><code>string.sub (s, i [, j])</code></a></h3>\nReturns the substring of <code>s</code> that\nstarts at <code>i</code>  and continues until <code>j</code>;\n<code>i</code> and <code>j</code> can be negative.\nIf <code>j</code> is absent, then it is assumed to be equal to -1\n(which is the same as the string length).\nIn particular,\nthe call <code>string.sub(s,1,j)</code> returns a prefix of <code>s</code>\nwith length <code>j</code>,\nand <code>string.sub(s, -i)</code> returns a suffix of <code>s</code>\nwith length <code>i</code>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-string.upper\"><code>string.upper (s)</code></a></h3>\nReceives a string and returns a copy of this string with all\nlowercase letters changed to uppercase.\nAll other characters are left unchanged.\nThe definition of what a lowercase letter is depends on the current locale.\n\n\n\n<h3>5.4.1 - <a name=\"5.4.1\">Patterns</a></h3>\n\n\n<h4>Character Class:</h4><p>\nA <em>character class</em> is used to represent a set of characters.\nThe following combinations are allowed in describing a character class:\n\n<ul>\n\n<li><b><em>x</em>:</b>\n(where <em>x</em> is not one of the <em>magic characters</em>\n<code>^$()%.[]*+-?</code>)\nrepresents the character <em>x</em> itself.\n</li>\n\n<li><b><code>.</code>:</b> (a dot) represents all characters.</li>\n\n<li><b><code>%a</code>:</b> represents all letters.</li>\n\n<li><b><code>%c</code>:</b> represents all control characters.</li>\n\n<li><b><code>%d</code>:</b> represents all digits.</li>\n\n<li><b><code>%l</code>:</b> represents all lowercase letters.</li>\n\n<li><b><code>%p</code>:</b> represents all punctuation characters.</li>\n\n<li><b><code>%s</code>:</b> represents all space characters.</li>\n\n<li><b><code>%u</code>:</b> represents all uppercase letters.</li>\n\n<li><b><code>%w</code>:</b> represents all alphanumeric characters.</li>\n\n<li><b><code>%x</code>:</b> represents all hexadecimal digits.</li>\n\n<li><b><code>%z</code>:</b> represents the character with representation 0.</li>\n\n<li><b><code>%<em>x</em></code>:</b> (where <em>x</em> is any non-alphanumeric character)\nrepresents the character <em>x</em>.\nThis is the standard way to escape the magic characters.\nAny punctuation character (even the non magic)\ncan be preceded by a '<code>%</code>'\nwhen used to represent itself in a pattern.\n</li>\n\n<li><b><code>[<em>set</em>]</code>:</b>\nrepresents the class which is the union of all\ncharacters in <em>set</em>.\nA range of characters can be specified by\nseparating the end characters of the range with a '<code>-</code>'.\nAll classes <code>%</code><em>x</em> described above can also be used as\ncomponents in <em>set</em>.\nAll other characters in <em>set</em> represent themselves.\nFor example, <code>[%w_]</code> (or <code>[_%w]</code>)\nrepresents all alphanumeric characters plus the underscore,\n<code>[0-7]</code> represents the octal digits,\nand <code>[0-7%l%-]</code> represents the octal digits plus\nthe lowercase letters plus the '<code>-</code>' character.\n\n\n<p>\nThe interaction between ranges and classes is not defined.\nTherefore, patterns like <code>[%a-z]</code> or <code>[a-%%]</code>\nhave no meaning.\n</li>\n\n<li><b><code>[^<em>set</em>]</code>:</b>\nrepresents the complement of <em>set</em>,\nwhere <em>set</em> is interpreted as above.\n</li>\n\n</ul><p>\nFor all classes represented by single letters (<code>%a</code>, <code>%c</code>, etc.),\nthe corresponding uppercase letter represents the complement of the class.\nFor instance, <code>%S</code> represents all non-space characters.\n\n\n<p>\nThe definitions of letter, space, and other character groups\ndepend on the current locale.\nIn particular, the class <code>[a-z]</code> may not be equivalent to <code>%l</code>.\n\n\n\n\n\n<h4>Pattern Item:</h4><p>\nA <em>pattern item</em> can be\n\n<ul>\n\n<li>\na single character class,\nwhich matches any single character in the class;\n</li>\n\n<li>\na single character class followed by '<code>*</code>',\nwhich matches 0 or more repetitions of characters in the class.\nThese repetition items will always match the longest possible sequence;\n</li>\n\n<li>\na single character class followed by '<code>+</code>',\nwhich matches 1 or more repetitions of characters in the class.\nThese repetition items will always match the longest possible sequence;\n</li>\n\n<li>\na single character class followed by '<code>-</code>',\nwhich also matches 0 or more repetitions of characters in the class.\nUnlike '<code>*</code>',\nthese repetition items will always match the <em>shortest</em> possible sequence;\n</li>\n\n<li>\na single character class followed by '<code>?</code>',\nwhich matches 0 or 1 occurrence of a character in the class;\n</li>\n\n<li>\n<code>%<em>n</em></code>, for <em>n</em> between 1 and 9;\nsuch item matches a substring equal to the <em>n</em>-th captured string\n(see below);\n</li>\n\n<li>\n<code>%b<em>xy</em></code>, where <em>x</em> and <em>y</em> are two distinct characters;\nsuch item matches strings that start with&nbsp;<em>x</em>, end with&nbsp;<em>y</em>,\nand where the <em>x</em> and <em>y</em> are <em>balanced</em>.\nThis means that, if one reads the string from left to right,\ncounting <em>+1</em> for an <em>x</em> and <em>-1</em> for a <em>y</em>,\nthe ending <em>y</em> is the first <em>y</em> where the count reaches 0.\nFor instance, the item <code>%b()</code> matches expressions with\nbalanced parentheses.\n</li>\n\n</ul>\n\n\n\n\n<h4>Pattern:</h4><p>\nA <em>pattern</em> is a sequence of pattern items.\nA '<code>^</code>' at the beginning of a pattern anchors the match at the\nbeginning of the subject string.\nA '<code>$</code>' at the end of a pattern anchors the match at the\nend of the subject string.\nAt other positions,\n'<code>^</code>' and '<code>$</code>' have no special meaning and represent themselves.\n\n\n\n\n\n<h4>Captures:</h4><p>\nA pattern can contain sub-patterns enclosed in parentheses;\nthey describe <em>captures</em>.\nWhen a match succeeds, the substrings of the subject string\nthat match captures are stored (<em>captured</em>) for future use.\nCaptures are numbered according to their left parentheses.\nFor instance, in the pattern <code>\"(a*(.)%w(%s*))\"</code>,\nthe part of the string matching <code>\"a*(.)%w(%s*)\"</code> is\nstored as the first capture (and therefore has number&nbsp;1);\nthe character matching \"<code>.</code>\" is captured with number&nbsp;2,\nand the part matching \"<code>%s*</code>\" has number&nbsp;3.\n\n\n<p>\nAs a special case, the empty capture <code>()</code> captures\nthe current string position (a number).\nFor instance, if we apply the pattern <code>\"()aa()\"</code> on the\nstring <code>\"flaaap\"</code>, there will be two captures: 3&nbsp;and&nbsp;5.\n\n\n<p>\nA pattern cannot contain embedded zeros.  Use <code>%z</code> instead.\n\n\n\n\n\n\n\n\n\n\n\n<h2>5.5 - <a name=\"5.5\">Table Manipulation</a></h2><p>\nThis library provides generic functions for table manipulation.\nIt provides all its functions inside the table <a name=\"pdf-table\"><code>table</code></a>.\n\n\n<p>\nMost functions in the table library assume that the table\nrepresents an array or a list.\nFor these functions, when we talk about the \"length\" of a table\nwe mean the result of the length operator.\n\n\n<p>\n<hr><h3><a name=\"pdf-table.concat\"><code>table.concat (table [, sep [, i [, j]]])</code></a></h3>\nGiven an array where all elements are strings or numbers,\nreturns <code>table[i]..sep..table[i+1] &middot;&middot;&middot; sep..table[j]</code>.\nThe default value for <code>sep</code> is the empty string,\nthe default for <code>i</code> is 1,\nand the default for <code>j</code> is the length of the table.\nIf <code>i</code> is greater than <code>j</code>, returns the empty string.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-table.insert\"><code>table.insert (table, [pos,] value)</code></a></h3>\n\n\n<p>\nInserts element <code>value</code> at position <code>pos</code> in <code>table</code>,\nshifting up other elements to open space, if necessary.\nThe default value for <code>pos</code> is <code>n+1</code>,\nwhere <code>n</code> is the length of the table (see <a href=\"#2.5.5\">&sect;2.5.5</a>),\nso that a call <code>table.insert(t,x)</code> inserts <code>x</code> at the end\nof table <code>t</code>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-table.maxn\"><code>table.maxn (table)</code></a></h3>\n\n\n<p>\nReturns the largest positive numerical index of the given table,\nor zero if the table has no positive numerical indices.\n(To do its job this function does a linear traversal of\nthe whole table.) \n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-table.remove\"><code>table.remove (table [, pos])</code></a></h3>\n\n\n<p>\nRemoves from <code>table</code> the element at position <code>pos</code>,\nshifting down other elements to close the space, if necessary.\nReturns the value of the removed element.\nThe default value for <code>pos</code> is <code>n</code>,\nwhere <code>n</code> is the length of the table,\nso that a call <code>table.remove(t)</code> removes the last element\nof table <code>t</code>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-table.sort\"><code>table.sort (table [, comp])</code></a></h3>\nSorts table elements in a given order, <em>in-place</em>,\nfrom <code>table[1]</code> to <code>table[n]</code>,\nwhere <code>n</code> is the length of the table.\nIf <code>comp</code> is given,\nthen it must be a function that receives two table elements,\nand returns true\nwhen the first is less than the second\n(so that <code>not comp(a[i+1],a[i])</code> will be true after the sort).\nIf <code>comp</code> is not given,\nthen the standard Lua operator <code>&lt;</code> is used instead.\n\n\n<p>\nThe sort algorithm is not stable;\nthat is, elements considered equal by the given order\nmay have their relative positions changed by the sort.\n\n\n\n\n\n\n\n<h2>5.6 - <a name=\"5.6\">Mathematical Functions</a></h2>\n\n<p>\nThis library is an interface to the standard C&nbsp;math library.\nIt provides all its functions inside the table <a name=\"pdf-math\"><code>math</code></a>.\n\n\n<p>\n<hr><h3><a name=\"pdf-math.abs\"><code>math.abs (x)</code></a></h3>\n\n\n<p>\nReturns the absolute value of <code>x</code>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.acos\"><code>math.acos (x)</code></a></h3>\n\n\n<p>\nReturns the arc cosine of <code>x</code> (in radians).\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.asin\"><code>math.asin (x)</code></a></h3>\n\n\n<p>\nReturns the arc sine of <code>x</code> (in radians).\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.atan\"><code>math.atan (x)</code></a></h3>\n\n\n<p>\nReturns the arc tangent of <code>x</code> (in radians).\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.atan2\"><code>math.atan2 (y, x)</code></a></h3>\n\n\n<p>\nReturns the arc tangent of <code>y/x</code> (in radians),\nbut uses the signs of both parameters to find the\nquadrant of the result.\n(It also handles correctly the case of <code>x</code> being zero.)\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.ceil\"><code>math.ceil (x)</code></a></h3>\n\n\n<p>\nReturns the smallest integer larger than or equal to <code>x</code>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.cos\"><code>math.cos (x)</code></a></h3>\n\n\n<p>\nReturns the cosine of <code>x</code> (assumed to be in radians).\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.cosh\"><code>math.cosh (x)</code></a></h3>\n\n\n<p>\nReturns the hyperbolic cosine of <code>x</code>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.deg\"><code>math.deg (x)</code></a></h3>\n\n\n<p>\nReturns the angle <code>x</code> (given in radians) in degrees.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.exp\"><code>math.exp (x)</code></a></h3>\n\n\n<p>\nReturns the value <em>e<sup>x</sup></em>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.floor\"><code>math.floor (x)</code></a></h3>\n\n\n<p>\nReturns the largest integer smaller than or equal to <code>x</code>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.fmod\"><code>math.fmod (x, y)</code></a></h3>\n\n\n<p>\nReturns the remainder of the division of <code>x</code> by <code>y</code>\nthat rounds the quotient towards zero.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.frexp\"><code>math.frexp (x)</code></a></h3>\n\n\n<p>\nReturns <code>m</code> and <code>e</code> such that <em>x = m2<sup>e</sup></em>,\n<code>e</code> is an integer and the absolute value of <code>m</code> is\nin the range <em>[0.5, 1)</em>\n(or zero when <code>x</code> is zero).\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.huge\"><code>math.huge</code></a></h3>\n\n\n<p>\nThe value <code>HUGE_VAL</code>,\na value larger than or equal to any other numerical value.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.ldexp\"><code>math.ldexp (m, e)</code></a></h3>\n\n\n<p>\nReturns <em>m2<sup>e</sup></em> (<code>e</code> should be an integer).\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.log\"><code>math.log (x)</code></a></h3>\n\n\n<p>\nReturns the natural logarithm of <code>x</code>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.log10\"><code>math.log10 (x)</code></a></h3>\n\n\n<p>\nReturns the base-10 logarithm of <code>x</code>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.max\"><code>math.max (x, &middot;&middot;&middot;)</code></a></h3>\n\n\n<p>\nReturns the maximum value among its arguments.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.min\"><code>math.min (x, &middot;&middot;&middot;)</code></a></h3>\n\n\n<p>\nReturns the minimum value among its arguments.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.modf\"><code>math.modf (x)</code></a></h3>\n\n\n<p>\nReturns two numbers,\nthe integral part of <code>x</code> and the fractional part of <code>x</code>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.pi\"><code>math.pi</code></a></h3>\n\n\n<p>\nThe value of <em>pi</em>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.pow\"><code>math.pow (x, y)</code></a></h3>\n\n\n<p>\nReturns <em>x<sup>y</sup></em>.\n(You can also use the expression <code>x^y</code> to compute this value.)\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.rad\"><code>math.rad (x)</code></a></h3>\n\n\n<p>\nReturns the angle <code>x</code> (given in degrees) in radians.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.random\"><code>math.random ([m [, n]])</code></a></h3>\n\n\n<p>\nThis function is an interface to the simple\npseudo-random generator function <code>rand</code> provided by ANSI&nbsp;C.\n(No guarantees can be given for its statistical properties.)\n\n\n<p>\nWhen called without arguments,\nreturns a uniform pseudo-random real number\nin the range <em>[0,1)</em>.  \nWhen called with an integer number <code>m</code>,\n<code>math.random</code> returns\na uniform pseudo-random integer in the range <em>[1, m]</em>.\nWhen called with two integer numbers <code>m</code> and <code>n</code>,\n<code>math.random</code> returns a uniform pseudo-random\ninteger in the range <em>[m, n]</em>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.randomseed\"><code>math.randomseed (x)</code></a></h3>\n\n\n<p>\nSets <code>x</code> as the \"seed\"\nfor the pseudo-random generator:\nequal seeds produce equal sequences of numbers.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.sin\"><code>math.sin (x)</code></a></h3>\n\n\n<p>\nReturns the sine of <code>x</code> (assumed to be in radians).\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.sinh\"><code>math.sinh (x)</code></a></h3>\n\n\n<p>\nReturns the hyperbolic sine of <code>x</code>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.sqrt\"><code>math.sqrt (x)</code></a></h3>\n\n\n<p>\nReturns the square root of <code>x</code>.\n(You can also use the expression <code>x^0.5</code> to compute this value.)\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.tan\"><code>math.tan (x)</code></a></h3>\n\n\n<p>\nReturns the tangent of <code>x</code> (assumed to be in radians).\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-math.tanh\"><code>math.tanh (x)</code></a></h3>\n\n\n<p>\nReturns the hyperbolic tangent of <code>x</code>.\n\n\n\n\n\n\n\n<h2>5.7 - <a name=\"5.7\">Input and Output Facilities</a></h2>\n\n<p>\nThe I/O library provides two different styles for file manipulation.\nThe first one uses implicit file descriptors;\nthat is, there are operations to set a default input file and a\ndefault output file,\nand all input/output operations are over these default files.\nThe second style uses explicit file descriptors.\n\n\n<p>\nWhen using implicit file descriptors,\nall operations are supplied by table <a name=\"pdf-io\"><code>io</code></a>.\nWhen using explicit file descriptors,\nthe operation <a href=\"#pdf-io.open\"><code>io.open</code></a> returns a file descriptor\nand then all operations are supplied as methods of the file descriptor.\n\n\n<p>\nThe table <code>io</code> also provides\nthree predefined file descriptors with their usual meanings from C:\n<a name=\"pdf-io.stdin\"><code>io.stdin</code></a>, <a name=\"pdf-io.stdout\"><code>io.stdout</code></a>, and <a name=\"pdf-io.stderr\"><code>io.stderr</code></a>.\nThe I/O library never closes these files.\n\n\n<p>\nUnless otherwise stated,\nall I/O functions return <b>nil</b> on failure\n(plus an error message as a second result and\na system-dependent error code as a third result)\nand some value different from <b>nil</b> on success.\n\n\n<p>\n<hr><h3><a name=\"pdf-io.close\"><code>io.close ([file])</code></a></h3>\n\n\n<p>\nEquivalent to <code>file:close()</code>.\nWithout a <code>file</code>, closes the default output file.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-io.flush\"><code>io.flush ()</code></a></h3>\n\n\n<p>\nEquivalent to <code>file:flush</code> over the default output file.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-io.input\"><code>io.input ([file])</code></a></h3>\n\n\n<p>\nWhen called with a file name, it opens the named file (in text mode),\nand sets its handle as the default input file.\nWhen called with a file handle,\nit simply sets this file handle as the default input file.\nWhen called without parameters,\nit returns the current default input file.\n\n\n<p>\nIn case of errors this function raises the error,\ninstead of returning an error code.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-io.lines\"><code>io.lines ([filename])</code></a></h3>\n\n\n<p>\nOpens the given file name in read mode\nand returns an iterator function that,\neach time it is called,\nreturns a new line from the file.\nTherefore, the construction\n\n<pre>\n     for line in io.lines(filename) do <em>body</em> end\n</pre><p>\nwill iterate over all lines of the file.\nWhen the iterator function detects the end of file,\nit returns <b>nil</b> (to finish the loop) and automatically closes the file.\n\n\n<p>\nThe call <code>io.lines()</code> (with no file name) is equivalent\nto <code>io.input():lines()</code>;\nthat is, it iterates over the lines of the default input file.\nIn this case it does not close the file when the loop ends.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-io.open\"><code>io.open (filename [, mode])</code></a></h3>\n\n\n<p>\nThis function opens a file,\nin the mode specified in the string <code>mode</code>.\nIt returns a new file handle,\nor, in case of errors, <b>nil</b> plus an error message.\n\n\n<p>\nThe <code>mode</code> string can be any of the following:\n\n<ul>\n<li><b>\"r\":</b> read mode (the default);</li>\n<li><b>\"w\":</b> write mode;</li>\n<li><b>\"a\":</b> append mode;</li>\n<li><b>\"r+\":</b> update mode, all previous data is preserved;</li>\n<li><b>\"w+\":</b> update mode, all previous data is erased;</li>\n<li><b>\"a+\":</b> append update mode, previous data is preserved,\n  writing is only allowed at the end of file.</li>\n</ul><p>\nThe <code>mode</code> string can also have a '<code>b</code>' at the end,\nwhich is needed in some systems to open the file in binary mode.\nThis string is exactly what is used in the\nstandard&nbsp;C function <code>fopen</code>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-io.output\"><code>io.output ([file])</code></a></h3>\n\n\n<p>\nSimilar to <a href=\"#pdf-io.input\"><code>io.input</code></a>, but operates over the default output file.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-io.popen\"><code>io.popen (prog [, mode])</code></a></h3>\n\n\n<p>\nStarts program <code>prog</code> in a separated process and returns\na file handle that you can use to read data from this program\n(if <code>mode</code> is <code>\"r\"</code>, the default)\nor to write data to this program\n(if <code>mode</code> is <code>\"w\"</code>).\n\n\n<p>\nThis function is system dependent and is not available\non all platforms.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-io.read\"><code>io.read (&middot;&middot;&middot;)</code></a></h3>\n\n\n<p>\nEquivalent to <code>io.input():read</code>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-io.tmpfile\"><code>io.tmpfile ()</code></a></h3>\n\n\n<p>\nReturns a handle for a temporary file.\nThis file is opened in update mode\nand it is automatically removed when the program ends.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-io.type\"><code>io.type (obj)</code></a></h3>\n\n\n<p>\nChecks whether <code>obj</code> is a valid file handle.\nReturns the string <code>\"file\"</code> if <code>obj</code> is an open file handle,\n<code>\"closed file\"</code> if <code>obj</code> is a closed file handle,\nor <b>nil</b> if <code>obj</code> is not a file handle.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-io.write\"><code>io.write (&middot;&middot;&middot;)</code></a></h3>\n\n\n<p>\nEquivalent to <code>io.output():write</code>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-file:close\"><code>file:close ()</code></a></h3>\n\n\n<p>\nCloses <code>file</code>.\nNote that files are automatically closed when\ntheir handles are garbage collected,\nbut that takes an unpredictable amount of time to happen.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-file:flush\"><code>file:flush ()</code></a></h3>\n\n\n<p>\nSaves any written data to <code>file</code>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-file:lines\"><code>file:lines ()</code></a></h3>\n\n\n<p>\nReturns an iterator function that,\neach time it is called,\nreturns a new line from the file.\nTherefore, the construction\n\n<pre>\n     for line in file:lines() do <em>body</em> end\n</pre><p>\nwill iterate over all lines of the file.\n(Unlike <a href=\"#pdf-io.lines\"><code>io.lines</code></a>, this function does not close the file\nwhen the loop ends.)\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-file:read\"><code>file:read (&middot;&middot;&middot;)</code></a></h3>\n\n\n<p>\nReads the file <code>file</code>,\naccording to the given formats, which specify what to read.\nFor each format,\nthe function returns a string (or a number) with the characters read,\nor <b>nil</b> if it cannot read data with the specified format.\nWhen called without formats,\nit uses a default format that reads the entire next line\n(see below).\n\n\n<p>\nThe available formats are\n\n<ul>\n\n<li><b>\"*n\":</b>\nreads a number;\nthis is the only format that returns a number instead of a string.\n</li>\n\n<li><b>\"*a\":</b>\nreads the whole file, starting at the current position.\nOn end of file, it returns the empty string.\n</li>\n\n<li><b>\"*l\":</b>\nreads the next line (skipping the end of line),\nreturning <b>nil</b> on end of file.\nThis is the default format.\n</li>\n\n<li><b><em>number</em>:</b>\nreads a string with up to this number of characters,\nreturning <b>nil</b> on end of file.\nIf number is zero,\nit reads nothing and returns an empty string,\nor <b>nil</b> on end of file.\n</li>\n\n</ul>\n\n\n\n<p>\n<hr><h3><a name=\"pdf-file:seek\"><code>file:seek ([whence] [, offset])</code></a></h3>\n\n\n<p>\nSets and gets the file position,\nmeasured from the beginning of the file,\nto the position given by <code>offset</code> plus a base\nspecified by the string <code>whence</code>, as follows:\n\n<ul>\n<li><b>\"set\":</b> base is position 0 (beginning of the file);</li>\n<li><b>\"cur\":</b> base is current position;</li>\n<li><b>\"end\":</b> base is end of file;</li>\n</ul><p>\nIn case of success, function <code>seek</code> returns the final file position,\nmeasured in bytes from the beginning of the file.\nIf this function fails, it returns <b>nil</b>,\nplus a string describing the error.\n\n\n<p>\nThe default value for <code>whence</code> is <code>\"cur\"</code>,\nand for <code>offset</code> is 0.\nTherefore, the call <code>file:seek()</code> returns the current\nfile position, without changing it;\nthe call <code>file:seek(\"set\")</code> sets the position to the\nbeginning of the file (and returns 0);\nand the call <code>file:seek(\"end\")</code> sets the position to the\nend of the file, and returns its size.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-file:setvbuf\"><code>file:setvbuf (mode [, size])</code></a></h3>\n\n\n<p>\nSets the buffering mode for an output file.\nThere are three available modes:\n\n<ul>\n\n<li><b>\"no\":</b>\nno buffering; the result of any output operation appears immediately.\n</li>\n\n<li><b>\"full\":</b>\nfull buffering; output operation is performed only\nwhen the buffer is full (or when you explicitly <code>flush</code> the file\n(see <a href=\"#pdf-io.flush\"><code>io.flush</code></a>)).\n</li>\n\n<li><b>\"line\":</b>\nline buffering; output is buffered until a newline is output\nor there is any input from some special files\n(such as a terminal device).\n</li>\n\n</ul><p>\nFor the last two cases, <code>size</code>\nspecifies the size of the buffer, in bytes.\nThe default is an appropriate size.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-file:write\"><code>file:write (&middot;&middot;&middot;)</code></a></h3>\n\n\n<p>\nWrites the value of each of its arguments to\nthe <code>file</code>.\nThe arguments must be strings or numbers.\nTo write other values,\nuse <a href=\"#pdf-tostring\"><code>tostring</code></a> or <a href=\"#pdf-string.format\"><code>string.format</code></a> before <code>write</code>.\n\n\n\n\n\n\n\n<h2>5.8 - <a name=\"5.8\">Operating System Facilities</a></h2>\n\n<p>\nThis library is implemented through table <a name=\"pdf-os\"><code>os</code></a>.\n\n\n<p>\n<hr><h3><a name=\"pdf-os.clock\"><code>os.clock ()</code></a></h3>\n\n\n<p>\nReturns an approximation of the amount in seconds of CPU time\nused by the program.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-os.date\"><code>os.date ([format [, time]])</code></a></h3>\n\n\n<p>\nReturns a string or a table containing date and time,\nformatted according to the given string <code>format</code>.\n\n\n<p>\nIf the <code>time</code> argument is present,\nthis is the time to be formatted\n(see the <a href=\"#pdf-os.time\"><code>os.time</code></a> function for a description of this value).\nOtherwise, <code>date</code> formats the current time.\n\n\n<p>\nIf <code>format</code> starts with '<code>!</code>',\nthen the date is formatted in Coordinated Universal Time.\nAfter this optional character,\nif <code>format</code> is the string \"<code>*t</code>\",\nthen <code>date</code> returns a table with the following fields:\n<code>year</code> (four digits), <code>month</code> (1--12), <code>day</code> (1--31),\n<code>hour</code> (0--23), <code>min</code> (0--59), <code>sec</code> (0--61),\n<code>wday</code> (weekday, Sunday is&nbsp;1),\n<code>yday</code> (day of the year),\nand <code>isdst</code> (daylight saving flag, a boolean).\n\n\n<p>\nIf <code>format</code> is not \"<code>*t</code>\",\nthen <code>date</code> returns the date as a string,\nformatted according to the same rules as the C&nbsp;function <code>strftime</code>.\n\n\n<p>\nWhen called without arguments,\n<code>date</code> returns a reasonable date and time representation that depends on\nthe host system and on the current locale\n(that is, <code>os.date()</code> is equivalent to <code>os.date(\"%c\")</code>).\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-os.difftime\"><code>os.difftime (t2, t1)</code></a></h3>\n\n\n<p>\nReturns the number of seconds from time <code>t1</code> to time <code>t2</code>.\nIn POSIX, Windows, and some other systems,\nthis value is exactly <code>t2</code><em>-</em><code>t1</code>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-os.execute\"><code>os.execute ([command])</code></a></h3>\n\n\n<p>\nThis function is equivalent to the C&nbsp;function <code>system</code>.\nIt passes <code>command</code> to be executed by an operating system shell.\nIt returns a status code, which is system-dependent.\nIf <code>command</code> is absent, then it returns nonzero if a shell is available\nand zero otherwise.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-os.exit\"><code>os.exit ([code])</code></a></h3>\n\n\n<p>\nCalls the C&nbsp;function <code>exit</code>,\nwith an optional <code>code</code>,\nto terminate the host program.\nThe default value for <code>code</code> is the success code.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-os.getenv\"><code>os.getenv (varname)</code></a></h3>\n\n\n<p>\nReturns the value of the process environment variable <code>varname</code>,\nor <b>nil</b> if the variable is not defined.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-os.remove\"><code>os.remove (filename)</code></a></h3>\n\n\n<p>\nDeletes the file or directory with the given name.\nDirectories must be empty to be removed.\nIf this function fails, it returns <b>nil</b>,\nplus a string describing the error.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-os.rename\"><code>os.rename (oldname, newname)</code></a></h3>\n\n\n<p>\nRenames file or directory named <code>oldname</code> to <code>newname</code>.\nIf this function fails, it returns <b>nil</b>,\nplus a string describing the error.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-os.setlocale\"><code>os.setlocale (locale [, category])</code></a></h3>\n\n\n<p>\nSets the current locale of the program.\n<code>locale</code> is a string specifying a locale;\n<code>category</code> is an optional string describing which category to change:\n<code>\"all\"</code>, <code>\"collate\"</code>, <code>\"ctype\"</code>,\n<code>\"monetary\"</code>, <code>\"numeric\"</code>, or <code>\"time\"</code>;\nthe default category is <code>\"all\"</code>.\nThe function returns the name of the new locale,\nor <b>nil</b> if the request cannot be honored.\n\n\n<p>\nIf <code>locale</code> is the empty string,\nthe current locale is set to an implementation-defined native locale.\nIf <code>locale</code> is the string \"<code>C</code>\",\nthe current locale is set to the standard C locale.\n\n\n<p>\nWhen called with <b>nil</b> as the first argument,\nthis function only returns the name of the current locale\nfor the given category.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-os.time\"><code>os.time ([table])</code></a></h3>\n\n\n<p>\nReturns the current time when called without arguments,\nor a time representing the date and time specified by the given table.\nThis table must have fields <code>year</code>, <code>month</code>, and <code>day</code>,\nand may have fields <code>hour</code>, <code>min</code>, <code>sec</code>, and <code>isdst</code>\n(for a description of these fields, see the <a href=\"#pdf-os.date\"><code>os.date</code></a> function).\n\n\n<p>\nThe returned value is a number, whose meaning depends on your system.\nIn POSIX, Windows, and some other systems, this number counts the number\nof seconds since some given start time (the \"epoch\").\nIn other systems, the meaning is not specified,\nand the number returned by <code>time</code> can be used only as an argument to\n<code>date</code> and <code>difftime</code>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-os.tmpname\"><code>os.tmpname ()</code></a></h3>\n\n\n<p>\nReturns a string with a file name that can\nbe used for a temporary file.\nThe file must be explicitly opened before its use\nand explicitly removed when no longer needed.\n\n\n<p>\nOn some systems (POSIX),\nthis function also creates a file with that name,\nto avoid security risks.\n(Someone else might create the file with wrong permissions\nin the time between getting the name and creating the file.)\nYou still have to open the file to use it\nand to remove it (even if you do not use it).\n\n\n<p>\nWhen possible,\nyou may prefer to use <a href=\"#pdf-io.tmpfile\"><code>io.tmpfile</code></a>,\nwhich automatically removes the file when the program ends.\n\n\n\n\n\n\n\n<h2>5.9 - <a name=\"5.9\">The Debug Library</a></h2>\n\n<p>\nThis library provides\nthe functionality of the debug interface to Lua programs.\nYou should exert care when using this library.\nThe functions provided here should be used exclusively for debugging\nand similar tasks, such as profiling.\nPlease resist the temptation to use them as a\nusual programming tool:\nthey can be very slow.\nMoreover, several of these functions\nviolate some assumptions about Lua code\n(e.g., that variables local to a function\ncannot be accessed from outside or\nthat userdata metatables cannot be changed by Lua code)\nand therefore can compromise otherwise secure code.\n\n\n<p>\nAll functions in this library are provided\ninside the <a name=\"pdf-debug\"><code>debug</code></a> table.\nAll functions that operate over a thread\nhave an optional first argument which is the\nthread to operate over.\nThe default is always the current thread.\n\n\n<p>\n<hr><h3><a name=\"pdf-debug.debug\"><code>debug.debug ()</code></a></h3>\n\n\n<p>\nEnters an interactive mode with the user,\nrunning each string that the user enters.\nUsing simple commands and other debug facilities,\nthe user can inspect global and local variables,\nchange their values, evaluate expressions, and so on.\nA line containing only the word <code>cont</code> finishes this function,\nso that the caller continues its execution.\n\n\n<p>\nNote that commands for <code>debug.debug</code> are not lexically nested\nwithin any function, and so have no direct access to local variables.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-debug.getfenv\"><code>debug.getfenv (o)</code></a></h3>\nReturns the environment of object <code>o</code>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-debug.gethook\"><code>debug.gethook ([thread])</code></a></h3>\n\n\n<p>\nReturns the current hook settings of the thread, as three values:\nthe current hook function, the current hook mask,\nand the current hook count\n(as set by the <a href=\"#pdf-debug.sethook\"><code>debug.sethook</code></a> function).\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-debug.getinfo\"><code>debug.getinfo ([thread,] function [, what])</code></a></h3>\n\n\n<p>\nReturns a table with information about a function.\nYou can give the function directly,\nor you can give a number as the value of <code>function</code>,\nwhich means the function running at level <code>function</code> of the call stack\nof the given thread:\nlevel&nbsp;0 is the current function (<code>getinfo</code> itself);\nlevel&nbsp;1 is the function that called <code>getinfo</code>;\nand so on.\nIf <code>function</code> is a number larger than the number of active functions,\nthen <code>getinfo</code> returns <b>nil</b>.\n\n\n<p>\nThe returned table can contain all the fields returned by <a href=\"#lua_getinfo\"><code>lua_getinfo</code></a>,\nwith the string <code>what</code> describing which fields to fill in.\nThe default for <code>what</code> is to get all information available,\nexcept the table of valid lines.\nIf present,\nthe option '<code>f</code>'\nadds a field named <code>func</code> with the function itself.\nIf present,\nthe option '<code>L</code>'\nadds a field named <code>activelines</code> with the table of\nvalid lines.\n\n\n<p>\nFor instance, the expression <code>debug.getinfo(1,\"n\").name</code> returns\na table with a name for the current function,\nif a reasonable name can be found,\nand the expression <code>debug.getinfo(print)</code>\nreturns a table with all available information\nabout the <a href=\"#pdf-print\"><code>print</code></a> function.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-debug.getlocal\"><code>debug.getlocal ([thread,] level, local)</code></a></h3>\n\n\n<p>\nThis function returns the name and the value of the local variable\nwith index <code>local</code> of the function at level <code>level</code> of the stack.\n(The first parameter or local variable has index&nbsp;1, and so on,\nuntil the last active local variable.)\nThe function returns <b>nil</b> if there is no local\nvariable with the given index,\nand raises an error when called with a <code>level</code> out of range.\n(You can call <a href=\"#pdf-debug.getinfo\"><code>debug.getinfo</code></a> to check whether the level is valid.)\n\n\n<p>\nVariable names starting with '<code>(</code>' (open parentheses)\nrepresent internal variables\n(loop control variables, temporaries, and C&nbsp;function locals).\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-debug.getmetatable\"><code>debug.getmetatable (object)</code></a></h3>\n\n\n<p>\nReturns the metatable of the given <code>object</code>\nor <b>nil</b> if it does not have a metatable.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-debug.getregistry\"><code>debug.getregistry ()</code></a></h3>\n\n\n<p>\nReturns the registry table (see <a href=\"#3.5\">&sect;3.5</a>).\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-debug.getupvalue\"><code>debug.getupvalue (func, up)</code></a></h3>\n\n\n<p>\nThis function returns the name and the value of the upvalue\nwith index <code>up</code> of the function <code>func</code>.\nThe function returns <b>nil</b> if there is no upvalue with the given index.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-debug.setfenv\"><code>debug.setfenv (object, table)</code></a></h3>\n\n\n<p>\nSets the environment of the given <code>object</code> to the given <code>table</code>.\nReturns <code>object</code>.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-debug.sethook\"><code>debug.sethook ([thread,] hook, mask [, count])</code></a></h3>\n\n\n<p>\nSets the given function as a hook.\nThe string <code>mask</code> and the number <code>count</code> describe\nwhen the hook will be called.\nThe string mask may have the following characters,\nwith the given meaning:\n\n<ul>\n<li><b><code>\"c\"</code>:</b> the hook is called every time Lua calls a function;</li>\n<li><b><code>\"r\"</code>:</b> the hook is called every time Lua returns from a function;</li>\n<li><b><code>\"l\"</code>:</b> the hook is called every time Lua enters a new line of code.</li>\n</ul><p>\nWith a <code>count</code> different from zero,\nthe hook is called after every <code>count</code> instructions.\n\n\n<p>\nWhen called without arguments,\n<a href=\"#pdf-debug.sethook\"><code>debug.sethook</code></a> turns off the hook.\n\n\n<p>\nWhen the hook is called, its first parameter is a string\ndescribing the event that has triggered its call:\n<code>\"call\"</code>, <code>\"return\"</code> (or <code>\"tail return\"</code>,\nwhen simulating a return from a tail call),\n<code>\"line\"</code>, and <code>\"count\"</code>.\nFor line events,\nthe hook also gets the new line number as its second parameter.\nInside a hook,\nyou can call <code>getinfo</code> with level&nbsp;2 to get more information about\nthe running function\n(level&nbsp;0 is the <code>getinfo</code> function,\nand level&nbsp;1 is the hook function),\nunless the event is <code>\"tail return\"</code>.\nIn this case, Lua is only simulating the return,\nand a call to <code>getinfo</code> will return invalid data.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-debug.setlocal\"><code>debug.setlocal ([thread,] level, local, value)</code></a></h3>\n\n\n<p>\nThis function assigns the value <code>value</code> to the local variable\nwith index <code>local</code> of the function at level <code>level</code> of the stack.\nThe function returns <b>nil</b> if there is no local\nvariable with the given index,\nand raises an error when called with a <code>level</code> out of range.\n(You can call <code>getinfo</code> to check whether the level is valid.)\nOtherwise, it returns the name of the local variable.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-debug.setmetatable\"><code>debug.setmetatable (object, table)</code></a></h3>\n\n\n<p>\nSets the metatable for the given <code>object</code> to the given <code>table</code>\n(which can be <b>nil</b>).\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-debug.setupvalue\"><code>debug.setupvalue (func, up, value)</code></a></h3>\n\n\n<p>\nThis function assigns the value <code>value</code> to the upvalue\nwith index <code>up</code> of the function <code>func</code>.\nThe function returns <b>nil</b> if there is no upvalue\nwith the given index.\nOtherwise, it returns the name of the upvalue.\n\n\n\n\n<p>\n<hr><h3><a name=\"pdf-debug.traceback\"><code>debug.traceback ([thread,] [message [, level]])</code></a></h3>\n\n\n<p>\nReturns a string with a traceback of the call stack.\nAn optional <code>message</code> string is appended\nat the beginning of the traceback.\nAn optional <code>level</code> number tells at which level\nto start the traceback\n(default is 1, the function calling <code>traceback</code>).\n\n\n\n\n\n\n\n<h1>6 - <a name=\"6\">Lua Stand-alone</a></h1>\n\n<p>\nAlthough Lua has been designed as an extension language,\nto be embedded in a host C&nbsp;program,\nit is also frequently used as a stand-alone language.\nAn interpreter for Lua as a stand-alone language,\ncalled simply <code>lua</code>,\nis provided with the standard distribution.\nThe stand-alone interpreter includes\nall standard libraries, including the debug library.\nIts usage is:\n\n<pre>\n     lua [options] [script [args]]\n</pre><p>\nThe options are:\n\n<ul>\n<li><b><code>-e <em>stat</em></code>:</b> executes string <em>stat</em>;</li>\n<li><b><code>-l <em>mod</em></code>:</b> \"requires\" <em>mod</em>;</li>\n<li><b><code>-i</code>:</b> enters interactive mode after running <em>script</em>;</li>\n<li><b><code>-v</code>:</b> prints version information;</li>\n<li><b><code>--</code>:</b> stops handling options;</li>\n<li><b><code>-</code>:</b> executes <code>stdin</code> as a file and stops handling options.</li>\n</ul><p>\nAfter handling its options, <code>lua</code> runs the given <em>script</em>,\npassing to it the given <em>args</em> as string arguments.\nWhen called without arguments,\n<code>lua</code> behaves as <code>lua -v -i</code>\nwhen the standard input (<code>stdin</code>) is a terminal,\nand as <code>lua -</code> otherwise.\n\n\n<p>\nBefore running any argument,\nthe interpreter checks for an environment variable <a name=\"pdf-LUA_INIT\"><code>LUA_INIT</code></a>.\nIf its format is <code>@<em>filename</em></code>,\nthen <code>lua</code> executes the file.\nOtherwise, <code>lua</code> executes the string itself.\n\n\n<p>\nAll options are handled in order, except <code>-i</code>.\nFor instance, an invocation like\n\n<pre>\n     $ lua -e'a=1' -e 'print(a)' script.lua\n</pre><p>\nwill first set <code>a</code> to 1, then print the value of <code>a</code> (which is '<code>1</code>'),\nand finally run the file <code>script.lua</code> with no arguments.\n(Here <code>$</code> is the shell prompt. Your prompt may be different.)\n\n\n<p>\nBefore starting to run the script,\n<code>lua</code> collects all arguments in the command line\nin a global table called <code>arg</code>.\nThe script name is stored at index 0,\nthe first argument after the script name goes to index 1,\nand so on.\nAny arguments before the script name\n(that is, the interpreter name plus the options)\ngo to negative indices.\nFor instance, in the call\n\n<pre>\n     $ lua -la b.lua t1 t2\n</pre><p>\nthe interpreter first runs the file <code>a.lua</code>,\nthen creates a table\n\n<pre>\n     arg = { [-2] = \"lua\", [-1] = \"-la\",\n             [0] = \"b.lua\",\n             [1] = \"t1\", [2] = \"t2\" }\n</pre><p>\nand finally runs the file <code>b.lua</code>.\nThe script is called with <code>arg[1]</code>, <code>arg[2]</code>, &middot;&middot;&middot;\nas arguments;\nit can also access these arguments with the vararg expression '<code>...</code>'.\n\n\n<p>\nIn interactive mode,\nif you write an incomplete statement,\nthe interpreter waits for its completion\nby issuing a different prompt.\n\n\n<p>\nIf the global variable <a name=\"pdf-_PROMPT\"><code>_PROMPT</code></a> contains a string,\nthen its value is used as the prompt.\nSimilarly, if the global variable <a name=\"pdf-_PROMPT2\"><code>_PROMPT2</code></a> contains a string,\nits value is used as the secondary prompt\n(issued during incomplete statements).\nTherefore, both prompts can be changed directly on the command line\nor in any Lua programs by assigning to <code>_PROMPT</code>.\nSee the next example:\n\n<pre>\n     $ lua -e\"_PROMPT='myprompt&gt; '\" -i\n</pre><p>\n(The outer pair of quotes is for the shell,\nthe inner pair is for Lua.)\nNote the use of <code>-i</code> to enter interactive mode;\notherwise,\nthe program would just end silently\nright after the assignment to <code>_PROMPT</code>.\n\n\n<p>\nTo allow the use of Lua as a\nscript interpreter in Unix systems,\nthe stand-alone interpreter skips\nthe first line of a chunk if it starts with <code>#</code>.\nTherefore, Lua scripts can be made into executable programs\nby using <code>chmod +x</code> and the&nbsp;<code>#!</code> form,\nas in\n\n<pre>\n     #!/usr/local/bin/lua\n</pre><p>\n(Of course,\nthe location of the Lua interpreter may be different in your machine.\nIf <code>lua</code> is in your <code>PATH</code>,\nthen \n\n<pre>\n     #!/usr/bin/env lua\n</pre><p>\nis a more portable solution.) \n\n\n\n<h1>7 - <a name=\"7\">Incompatibilities with the Previous Version</a></h1>\n\n<p>\nHere we list the incompatibilities that you may find when moving a program\nfrom Lua&nbsp;5.0 to Lua&nbsp;5.1.\nYou can avoid most of the incompatibilities compiling Lua with\nappropriate options (see file <code>luaconf.h</code>).\nHowever,\nall these compatibility options will be removed in the next version of Lua.\n\n\n\n<h2>7.1 - <a name=\"7.1\">Changes in the Language</a></h2>\n<ul>\n\n<li>\nThe vararg system changed from the pseudo-argument <code>arg</code> with a\ntable with the extra arguments to the vararg expression.\n(See compile-time option <code>LUA_COMPAT_VARARG</code> in <code>luaconf.h</code>.)\n</li>\n\n<li>\nThere was a subtle change in the scope of the implicit\nvariables of the <b>for</b> statement and for the <b>repeat</b> statement.\n</li>\n\n<li>\nThe long string/long comment syntax (<code>[[<em>string</em>]]</code>)\ndoes not allow nesting.\nYou can use the new syntax (<code>[=[<em>string</em>]=]</code>) in these cases.\n(See compile-time option <code>LUA_COMPAT_LSTR</code> in <code>luaconf.h</code>.)\n</li>\n\n</ul>\n\n\n\n\n<h2>7.2 - <a name=\"7.2\">Changes in the Libraries</a></h2>\n<ul>\n\n<li>\nFunction <code>string.gfind</code> was renamed <a href=\"#pdf-string.gmatch\"><code>string.gmatch</code></a>.\n(See compile-time option <code>LUA_COMPAT_GFIND</code> in <code>luaconf.h</code>.)\n</li>\n\n<li>\nWhen <a href=\"#pdf-string.gsub\"><code>string.gsub</code></a> is called with a function as its\nthird argument,\nwhenever this function returns <b>nil</b> or <b>false</b> the\nreplacement string is the whole match,\ninstead of the empty string.\n</li>\n\n<li>\nFunction <code>table.setn</code> was deprecated.\nFunction <code>table.getn</code> corresponds\nto the new length operator (<code>#</code>);\nuse the operator instead of the function.\n(See compile-time option <code>LUA_COMPAT_GETN</code> in <code>luaconf.h</code>.)\n</li>\n\n<li>\nFunction <code>loadlib</code> was renamed <a href=\"#pdf-package.loadlib\"><code>package.loadlib</code></a>.\n(See compile-time option <code>LUA_COMPAT_LOADLIB</code> in <code>luaconf.h</code>.)\n</li>\n\n<li>\nFunction <code>math.mod</code> was renamed <a href=\"#pdf-math.fmod\"><code>math.fmod</code></a>.\n(See compile-time option <code>LUA_COMPAT_MOD</code> in <code>luaconf.h</code>.)\n</li>\n\n<li>\nFunctions <code>table.foreach</code> and <code>table.foreachi</code> are deprecated.\nYou can use a for loop with <code>pairs</code> or <code>ipairs</code> instead.\n</li>\n\n<li>\nThere were substantial changes in function <a href=\"#pdf-require\"><code>require</code></a> due to\nthe new module system.\nHowever, the new behavior is mostly compatible with the old,\nbut <code>require</code> gets the path from <a href=\"#pdf-package.path\"><code>package.path</code></a> instead\nof from <code>LUA_PATH</code>.\n</li>\n\n<li>\nFunction <a href=\"#pdf-collectgarbage\"><code>collectgarbage</code></a> has different arguments.\nFunction <code>gcinfo</code> is deprecated;\nuse <code>collectgarbage(\"count\")</code> instead.\n</li>\n\n</ul>\n\n\n\n\n<h2>7.3 - <a name=\"7.3\">Changes in the API</a></h2>\n<ul>\n\n<li>\nThe <code>luaopen_*</code> functions (to open libraries)\ncannot be called directly,\nlike a regular C function.\nThey must be called through Lua,\nlike a Lua function.\n</li>\n\n<li>\nFunction <code>lua_open</code> was replaced by <a href=\"#lua_newstate\"><code>lua_newstate</code></a> to\nallow the user to set a memory-allocation function.\nYou can use <a href=\"#luaL_newstate\"><code>luaL_newstate</code></a> from the standard library to\ncreate a state with a standard allocation function\n(based on <code>realloc</code>).\n</li>\n\n<li>\nFunctions <code>luaL_getn</code> and <code>luaL_setn</code>\n(from the auxiliary library) are deprecated.\nUse <a href=\"#lua_objlen\"><code>lua_objlen</code></a> instead of <code>luaL_getn</code>\nand nothing instead of <code>luaL_setn</code>.\n</li>\n\n<li>\nFunction <code>luaL_openlib</code> was replaced by <a href=\"#luaL_register\"><code>luaL_register</code></a>.\n</li>\n\n<li>\nFunction <code>luaL_checkudata</code> now throws an error when the given value\nis not a userdata of the expected type.\n(In Lua&nbsp;5.0 it returned <code>NULL</code>.)\n</li>\n\n</ul>\n\n\n\n\n<h1>8 - <a name=\"8\">The Complete Syntax of Lua</a></h1>\n\n<p>\nHere is the complete syntax of Lua in extended BNF.\n(It does not describe operator precedences.)\n\n\n\n\n<pre>\n\n\tchunk ::= {stat [`<b>;</b>&acute;]} [laststat [`<b>;</b>&acute;]]\n\n\tblock ::= chunk\n\n\tstat ::=  varlist `<b>=</b>&acute; explist | \n\t\t functioncall | \n\t\t <b>do</b> block <b>end</b> | \n\t\t <b>while</b> exp <b>do</b> block <b>end</b> | \n\t\t <b>repeat</b> block <b>until</b> exp | \n\t\t <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b> | \n\t\t <b>for</b> Name `<b>=</b>&acute; exp `<b>,</b>&acute; exp [`<b>,</b>&acute; exp] <b>do</b> block <b>end</b> | \n\t\t <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b> | \n\t\t <b>function</b> funcname funcbody | \n\t\t <b>local</b> <b>function</b> Name funcbody | \n\t\t <b>local</b> namelist [`<b>=</b>&acute; explist] \n\n\tlaststat ::= <b>return</b> [explist] | <b>break</b>\n\n\tfuncname ::= Name {`<b>.</b>&acute; Name} [`<b>:</b>&acute; Name]\n\n\tvarlist ::= var {`<b>,</b>&acute; var}\n\n\tvar ::=  Name | prefixexp `<b>[</b>&acute; exp `<b>]</b>&acute; | prefixexp `<b>.</b>&acute; Name \n\n\tnamelist ::= Name {`<b>,</b>&acute; Name}\n\n\texplist ::= {exp `<b>,</b>&acute;} exp\n\n\texp ::=  <b>nil</b> | <b>false</b> | <b>true</b> | Number | String | `<b>...</b>&acute; | function | \n\t\t prefixexp | tableconstructor | exp binop exp | unop exp \n\n\tprefixexp ::= var | functioncall | `<b>(</b>&acute; exp `<b>)</b>&acute;\n\n\tfunctioncall ::=  prefixexp args | prefixexp `<b>:</b>&acute; Name args \n\n\targs ::=  `<b>(</b>&acute; [explist] `<b>)</b>&acute; | tableconstructor | String \n\n\tfunction ::= <b>function</b> funcbody\n\n\tfuncbody ::= `<b>(</b>&acute; [parlist] `<b>)</b>&acute; block <b>end</b>\n\n\tparlist ::= namelist [`<b>,</b>&acute; `<b>...</b>&acute;] | `<b>...</b>&acute;\n\n\ttableconstructor ::= `<b>{</b>&acute; [fieldlist] `<b>}</b>&acute;\n\n\tfieldlist ::= field {fieldsep field} [fieldsep]\n\n\tfield ::= `<b>[</b>&acute; exp `<b>]</b>&acute; `<b>=</b>&acute; exp | Name `<b>=</b>&acute; exp | exp\n\n\tfieldsep ::= `<b>,</b>&acute; | `<b>;</b>&acute;\n\n\tbinop ::= `<b>+</b>&acute; | `<b>-</b>&acute; | `<b>*</b>&acute; | `<b>/</b>&acute; | `<b>^</b>&acute; | `<b>%</b>&acute; | `<b>..</b>&acute; | \n\t\t `<b>&lt;</b>&acute; | `<b>&lt;=</b>&acute; | `<b>&gt;</b>&acute; | `<b>&gt;=</b>&acute; | `<b>==</b>&acute; | `<b>~=</b>&acute; | \n\t\t <b>and</b> | <b>or</b>\n\n\tunop ::= `<b>-</b>&acute; | <b>not</b> | `<b>#</b>&acute;\n\n</pre>\n\n<p>\n\n\n\n\n\n\n\n<HR>\n<SMALL CLASS=\"footer\">\nLast update:\nMon Feb 13 18:54:19 BRST 2012\n</SMALL>\n<!--\nLast change: revised for Lua 5.1.5\n-->\n\n</body></html>\n\n"
  },
  {
    "path": "deps/lua/doc/readme.html",
    "content": "<HTML>\n<HEAD>\n<TITLE>Lua documentation</TITLE>\n<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"lua.css\">\n</HEAD>\n\n<BODY>\n\n<HR>\n<H1>\n<A HREF=\"http://www.lua.org/\"><IMG SRC=\"logo.gif\" ALT=\"Lua\" BORDER=0></A>\nDocumentation\n</H1>\n\nThis is the documentation included in the source distribution of Lua 5.1.5.\n\n<UL>\n<LI><A HREF=\"contents.html\">Reference manual</A>\n<LI><A HREF=\"lua.html\">lua man page</A>\n<LI><A HREF=\"luac.html\">luac man page</A>\n<LI><A HREF=\"../README\">lua/README</A>\n<LI><A HREF=\"../etc/README\">lua/etc/README</A>\n<LI><A HREF=\"../test/README\">lua/test/README</A>\n</UL>\n\nLua's\n<A HREF=\"http://www.lua.org/\">official web site</A>\ncontains updated documentation,\nespecially the\n<A HREF=\"http://www.lua.org/manual/5.1/\">reference manual</A>.\n<P>\n\n<HR>\n<SMALL>\nLast update:\nFri Feb  3 09:44:42 BRST 2012\n</SMALL>\n\n</BODY>\n</HTML>\n"
  },
  {
    "path": "deps/lua/etc/Makefile",
    "content": "# makefile for Lua etc\n\nTOP= ..\nLIB= $(TOP)/src\nINC= $(TOP)/src\nBIN= $(TOP)/src\nSRC= $(TOP)/src\nTST= $(TOP)/test\n\nCC= gcc\nCFLAGS= -O2 -Wall -I$(INC) $(MYCFLAGS)\nMYCFLAGS= \nMYLDFLAGS= -Wl,-E\nMYLIBS= -lm\n#MYLIBS= -lm -Wl,-E -ldl -lreadline -lhistory -lncurses\nRM= rm -f\n\ndefault:\n\t@echo 'Please choose a target: min noparser one strict clean'\n\nmin:\tmin.c\n\t$(CC) $(CFLAGS) $@.c -L$(LIB) -llua $(MYLIBS)\n\techo 'print\"Hello there!\"' | ./a.out\n\nnoparser: noparser.o\n\t$(CC) noparser.o $(SRC)/lua.o -L$(LIB) -llua $(MYLIBS)\n\t$(BIN)/luac $(TST)/hello.lua\n\t-./a.out luac.out\n\t-./a.out -e'a=1'\n\none:\n\t$(CC) $(CFLAGS) all.c $(MYLIBS)\n\t./a.out $(TST)/hello.lua\n\nstrict:\n\t-$(BIN)/lua -e 'print(a);b=2'\n\t-$(BIN)/lua -lstrict -e 'print(a)'\n\t-$(BIN)/lua -e 'function f() b=2 end f()'\n\t-$(BIN)/lua -lstrict -e 'function f() b=2 end f()'\n\nclean:\n\t$(RM) a.out core core.* *.o luac.out\n\n.PHONY:\tdefault min noparser one strict clean\n"
  },
  {
    "path": "deps/lua/etc/README",
    "content": "This directory contains some useful files and code.\nUnlike the code in ../src, everything here is in the public domain.\n\nIf any of the makes fail, you're probably not using the same libraries\nused to build Lua. Set MYLIBS in Makefile accordingly.\n\nall.c\n\tFull Lua interpreter in a single file.\n\tDo \"make one\" for a demo.\n\nlua.hpp\n\tLua header files for C++ using 'extern \"C\"'.\n\nlua.ico\n\tA Lua icon for Windows (and web sites: save as favicon.ico).\n\tDrawn by hand by Markus Gritsch <gritsch@iue.tuwien.ac.at>.\n\nlua.pc\n\tpkg-config data for Lua\n\nluavs.bat\n\tScript to build Lua under \"Visual Studio .NET Command Prompt\".\n\tRun it from the toplevel as etc\\luavs.bat.\n\nmin.c\n\tA minimal Lua interpreter.\n\tGood for learning and for starting your own.\n\tDo \"make min\" for a demo.\n\nnoparser.c\n\tLinking with noparser.o avoids loading the parsing modules in lualib.a.\n\tDo \"make noparser\" for a demo.\n\nstrict.lua\n\tTraps uses of undeclared global variables.\n\tDo \"make strict\" for a demo.\n\n"
  },
  {
    "path": "deps/lua/etc/all.c",
    "content": "/*\n* all.c -- Lua core, libraries and interpreter in a single file\n*/\n\n#define luaall_c\n\n#include \"lapi.c\"\n#include \"lcode.c\"\n#include \"ldebug.c\"\n#include \"ldo.c\"\n#include \"ldump.c\"\n#include \"lfunc.c\"\n#include \"lgc.c\"\n#include \"llex.c\"\n#include \"lmem.c\"\n#include \"lobject.c\"\n#include \"lopcodes.c\"\n#include \"lparser.c\"\n#include \"lstate.c\"\n#include \"lstring.c\"\n#include \"ltable.c\"\n#include \"ltm.c\"\n#include \"lundump.c\"\n#include \"lvm.c\"\n#include \"lzio.c\"\n\n#include \"lauxlib.c\"\n#include \"lbaselib.c\"\n#include \"ldblib.c\"\n#include \"liolib.c\"\n#include \"linit.c\"\n#include \"lmathlib.c\"\n#include \"loadlib.c\"\n#include \"loslib.c\"\n#include \"lstrlib.c\"\n#include \"ltablib.c\"\n\n#include \"lua.c\"\n"
  },
  {
    "path": "deps/lua/etc/lua.hpp",
    "content": "// lua.hpp\n// Lua header files for C++\n// <<extern \"C\">> not supplied automatically because Lua also compiles as C++\n\nextern \"C\" {\n#include \"lua.h\"\n#include \"lualib.h\"\n#include \"lauxlib.h\"\n}\n"
  },
  {
    "path": "deps/lua/etc/lua.pc",
    "content": "# lua.pc -- pkg-config data for Lua\n\n# vars from install Makefile\n\n# grep '^V=' ../Makefile\nV= 5.1\n# grep '^R=' ../Makefile\nR= 5.1.5\n\n# grep '^INSTALL_.*=' ../Makefile | sed 's/INSTALL_TOP/prefix/'\nprefix= /usr/local\nINSTALL_BIN= ${prefix}/bin\nINSTALL_INC= ${prefix}/include\nINSTALL_LIB= ${prefix}/lib\nINSTALL_MAN= ${prefix}/man/man1\nINSTALL_LMOD= ${prefix}/share/lua/${V}\nINSTALL_CMOD= ${prefix}/lib/lua/${V}\n\n# canonical vars\nexec_prefix=${prefix}\nlibdir=${exec_prefix}/lib\nincludedir=${prefix}/include\n\nName: Lua\nDescription: An Extensible Extension Language\nVersion: ${R}\nRequires: \nLibs: -L${libdir} -llua -lm\nCflags: -I${includedir}\n\n# (end of lua.pc)\n"
  },
  {
    "path": "deps/lua/etc/luavs.bat",
    "content": "@rem Script to build Lua under \"Visual Studio .NET Command Prompt\".\r\n@rem Do not run from this directory; run it from the toplevel: etc\\luavs.bat .\r\n@rem It creates lua51.dll, lua51.lib, lua.exe, and luac.exe in src.\r\n@rem (contributed by David Manura and Mike Pall)\r\n\r\n@setlocal\r\n@set MYCOMPILE=cl /nologo /MD /O2 /W3 /c /D_CRT_SECURE_NO_DEPRECATE\r\n@set MYLINK=link /nologo\r\n@set MYMT=mt /nologo\r\n\r\ncd src\r\n%MYCOMPILE% /DLUA_BUILD_AS_DLL l*.c\r\ndel lua.obj luac.obj\r\n%MYLINK% /DLL /out:lua51.dll l*.obj\r\nif exist lua51.dll.manifest^\r\n  %MYMT% -manifest lua51.dll.manifest -outputresource:lua51.dll;2\r\n%MYCOMPILE% /DLUA_BUILD_AS_DLL lua.c\r\n%MYLINK% /out:lua.exe lua.obj lua51.lib\r\nif exist lua.exe.manifest^\r\n  %MYMT% -manifest lua.exe.manifest -outputresource:lua.exe\r\n%MYCOMPILE% l*.c print.c\r\ndel lua.obj linit.obj lbaselib.obj ldblib.obj liolib.obj lmathlib.obj^\r\n    loslib.obj ltablib.obj lstrlib.obj loadlib.obj\r\n%MYLINK% /out:luac.exe *.obj\r\nif exist luac.exe.manifest^\r\n  %MYMT% -manifest luac.exe.manifest -outputresource:luac.exe\r\ndel *.obj *.manifest\r\ncd ..\r\n"
  },
  {
    "path": "deps/lua/etc/min.c",
    "content": "/*\n* min.c -- a minimal Lua interpreter\n* loads stdin only with minimal error handling.\n* no interaction, and no standard library, only a \"print\" function.\n*/\n\n#include <stdio.h>\n\n#include \"lua.h\"\n#include \"lauxlib.h\"\n\nstatic int print(lua_State *L)\n{\n int n=lua_gettop(L);\n int i;\n for (i=1; i<=n; i++)\n {\n  if (i>1) printf(\"\\t\");\n  if (lua_isstring(L,i))\n   printf(\"%s\",lua_tostring(L,i));\n  else if (lua_isnil(L,i))\n   printf(\"%s\",\"nil\");\n  else if (lua_isboolean(L,i))\n   printf(\"%s\",lua_toboolean(L,i) ? \"true\" : \"false\");\n  else\n   printf(\"%s:%p\",luaL_typename(L,i),lua_topointer(L,i));\n }\n printf(\"\\n\");\n return 0;\n}\n\nint main(void)\n{\n lua_State *L=lua_open();\n lua_register(L,\"print\",print);\n if (luaL_dofile(L,NULL)!=0) fprintf(stderr,\"%s\\n\",lua_tostring(L,-1));\n lua_close(L);\n return 0;\n}\n"
  },
  {
    "path": "deps/lua/etc/noparser.c",
    "content": "/*\n* The code below can be used to make a Lua core that does not contain the\n* parsing modules (lcode, llex, lparser), which represent 35% of the total core.\n* You'll only be able to load binary files and strings, precompiled with luac.\n* (Of course, you'll have to build luac with the original parsing modules!)\n*\n* To use this module, simply compile it (\"make noparser\" does that) and list\n* its object file before the Lua libraries. The linker should then not load\n* the parsing modules. To try it, do \"make luab\".\n*\n* If you also want to avoid the dump module (ldump.o), define NODUMP.\n* #define NODUMP\n*/\n\n#define LUA_CORE\n\n#include \"llex.h\"\n#include \"lparser.h\"\n#include \"lzio.h\"\n\nLUAI_FUNC void luaX_init (lua_State *L) {\n  UNUSED(L);\n}\n\nLUAI_FUNC Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) {\n  UNUSED(z);\n  UNUSED(buff);\n  UNUSED(name);\n  lua_pushliteral(L,\"parser not loaded\");\n  lua_error(L);\n  return NULL;\n}\n\n#ifdef NODUMP\n#include \"lundump.h\"\n\nLUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) {\n  UNUSED(f);\n  UNUSED(w);\n  UNUSED(data);\n  UNUSED(strip);\n#if 1\n  UNUSED(L);\n  return 0;\n#else\n  lua_pushliteral(L,\"dumper not loaded\");\n  lua_error(L);\n#endif\n}\n#endif\n"
  },
  {
    "path": "deps/lua/etc/strict.lua",
    "content": "--\n-- strict.lua\n-- checks uses of undeclared global variables\n-- All global variables must be 'declared' through a regular assignment\n-- (even assigning nil will do) in a main chunk before being used\n-- anywhere or assigned to inside a function.\n--\n\nlocal getinfo, error, rawset, rawget = debug.getinfo, error, rawset, rawget\n\nlocal mt = getmetatable(_G)\nif mt == nil then\n  mt = {}\n  setmetatable(_G, mt)\nend\n\nmt.__declared = {}\n\nlocal function what ()\n  local d = getinfo(3, \"S\")\n  return d and d.what or \"C\"\nend\n\nmt.__newindex = function (t, n, v)\n  if not mt.__declared[n] then\n    local w = what()\n    if w ~= \"main\" and w ~= \"C\" then\n      error(\"assign to undeclared variable '\"..n..\"'\", 2)\n    end\n    mt.__declared[n] = true\n  end\n  rawset(t, n, v)\nend\n  \nmt.__index = function (t, n)\n  if not mt.__declared[n] and what() ~= \"C\" then\n    error(\"variable '\"..n..\"' is not declared\", 2)\n  end\n  return rawget(t, n)\nend\n\n"
  },
  {
    "path": "deps/lua/src/Makefile",
    "content": "# makefile for building Lua\n# see ../INSTALL for installation instructions\n# see ../Makefile and luaconf.h for further customization\n\n# == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT =======================\n\n# Your platform. See PLATS for possible values.\nPLAT= none\n\nCC?= gcc\nCFLAGS= -O2 -Wall $(MYCFLAGS)\nAR= ar rcu\nRANLIB= ranlib\nRM= rm -f\nLIBS= -lm $(MYLIBS)\n\nMYCFLAGS=\nMYLDFLAGS=\nMYLIBS=\n\n# == END OF USER SETTINGS. NO NEED TO CHANGE ANYTHING BELOW THIS LINE =========\n\nPLATS= aix ansi bsd freebsd generic linux macosx mingw posix solaris\n\nLUA_A=\tliblua.a\nCORE_O=\tlapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o \\\n\tlobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o  \\\n\tlundump.o lvm.o lzio.o strbuf.o fpconv.o\nLIB_O=\tlauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o \\\n\tlstrlib.o loadlib.o linit.o lua_cjson.o lua_struct.o lua_cmsgpack.o \\\n\tlua_bit.o\n\nLUA_T=\tlua\nLUA_O=\tlua.o\n\nLUAC_T=\tluac\nLUAC_O=\tluac.o print.o\n\nALL_O= $(CORE_O) $(LIB_O) $(LUA_O) $(LUAC_O)\nALL_T= $(LUA_A) $(LUA_T) $(LUAC_T)\nALL_A= $(LUA_A)\n\ndefault: $(PLAT)\n\nall:\t$(ALL_T)\n\no:\t$(ALL_O)\n\na:\t$(ALL_A)\n\n$(LUA_A): $(CORE_O) $(LIB_O)\n\t$(AR) $@ $(CORE_O) $(LIB_O)\t# DLL needs all object files\n\t$(RANLIB) $@\n\n$(LUA_T): $(LUA_O) $(LUA_A)\n\t$(CC) -o $@ $(MYLDFLAGS) $(LUA_O) $(LUA_A) $(LIBS)\n\n$(LUAC_T): $(LUAC_O) $(LUA_A)\n\t$(CC) -o $@ $(MYLDFLAGS) $(LUAC_O) $(LUA_A) $(LIBS)\n\nclean:\n\t$(RM) $(ALL_T) $(ALL_O)\n\ndepend:\n\t@$(CC) $(CFLAGS) -MM l*.c print.c\n\necho:\n\t@echo \"PLAT = $(PLAT)\"\n\t@echo \"CC = $(CC)\"\n\t@echo \"CFLAGS = $(CFLAGS)\"\n\t@echo \"AR = $(AR)\"\n\t@echo \"RANLIB = $(RANLIB)\"\n\t@echo \"RM = $(RM)\"\n\t@echo \"MYCFLAGS = $(MYCFLAGS)\"\n\t@echo \"MYLDFLAGS = $(MYLDFLAGS)\"\n\t@echo \"MYLIBS = $(MYLIBS)\"\n\n# convenience targets for popular platforms\n\nnone:\n\t@echo \"Please choose a platform:\"\n\t@echo \"   $(PLATS)\"\n\naix:\n\t$(MAKE) all CC=\"xlc\" CFLAGS=\"-O2 -DLUA_USE_POSIX -DLUA_USE_DLOPEN\" MYLIBS=\"-ldl\" MYLDFLAGS=\"-brtl -bexpall\"\n\nansi:\n\t$(MAKE) all MYCFLAGS=-DLUA_ANSI\n\nbsd:\n\t$(MAKE) all MYCFLAGS=\"-DLUA_USE_POSIX -DLUA_USE_DLOPEN\" MYLIBS=\"-Wl,-E\"\n\nfreebsd:\n\t$(MAKE) all MYCFLAGS=\"-DLUA_USE_LINUX\" MYLIBS=\"-Wl,-E -lreadline\"\n\ngeneric:\n\t$(MAKE) all MYCFLAGS=\n\nlinux:\n\t$(MAKE) all MYCFLAGS=-DLUA_USE_LINUX MYLIBS=\"-Wl,-E -ldl -lreadline -lhistory -lncurses\"\n\nmacosx:\n\t$(MAKE) all MYCFLAGS=-DLUA_USE_LINUX MYLIBS=\"-lreadline\"\n# use this on Mac OS X 10.3-\n#\t$(MAKE) all MYCFLAGS=-DLUA_USE_MACOSX\n\nmingw:\n\t$(MAKE) \"LUA_A=lua51.dll\" \"LUA_T=lua.exe\" \\\n\t\"AR=$(CC) -shared -o\" \"RANLIB=strip --strip-unneeded\" \\\n\t\"MYCFLAGS=-DLUA_BUILD_AS_DLL\" \"MYLIBS=\" \"MYLDFLAGS=-s\" lua.exe\n\t$(MAKE) \"LUAC_T=luac.exe\" luac.exe\n\nposix:\n\t$(MAKE) all MYCFLAGS=-DLUA_USE_POSIX\n\nsolaris:\n\t$(MAKE) all MYCFLAGS=\"-DLUA_USE_POSIX -DLUA_USE_DLOPEN\" MYLIBS=\"-ldl\"\n\n# list targets that do not create files (but not all makes understand .PHONY)\n.PHONY: all $(PLATS) default o a clean depend echo none\n\n# DO NOT DELETE\n\nlapi.o: lapi.c lua.h luaconf.h lapi.h lobject.h llimits.h ldebug.h \\\n  lstate.h ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h \\\n  lundump.h lvm.h\nlauxlib.o: lauxlib.c lua.h luaconf.h lauxlib.h\nlbaselib.o: lbaselib.c lua.h luaconf.h lauxlib.h lualib.h\nlcode.o: lcode.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \\\n  lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h lgc.h \\\n  ltable.h\nldblib.o: ldblib.c lua.h luaconf.h lauxlib.h lualib.h\nldebug.o: ldebug.c lua.h luaconf.h lapi.h lobject.h llimits.h lcode.h \\\n  llex.h lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h \\\n  lfunc.h lstring.h lgc.h ltable.h lvm.h\nldo.o: ldo.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \\\n  lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lparser.h lstring.h \\\n  ltable.h lundump.h lvm.h\nldump.o: ldump.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h \\\n  lzio.h lmem.h lundump.h\nlfunc.o: lfunc.c lua.h luaconf.h lfunc.h lobject.h llimits.h lgc.h lmem.h \\\n  lstate.h ltm.h lzio.h\nlgc.o: lgc.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \\\n  lzio.h lmem.h ldo.h lfunc.h lgc.h lstring.h ltable.h\nlinit.o: linit.c lua.h luaconf.h lualib.h lauxlib.h\nliolib.o: liolib.c lua.h luaconf.h lauxlib.h lualib.h\nllex.o: llex.c lua.h luaconf.h ldo.h lobject.h llimits.h lstate.h ltm.h \\\n  lzio.h lmem.h llex.h lparser.h lstring.h lgc.h ltable.h\nlmathlib.o: lmathlib.c lua.h luaconf.h lauxlib.h lualib.h\nlmem.o: lmem.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \\\n  ltm.h lzio.h lmem.h ldo.h\nloadlib.o: loadlib.c lua.h luaconf.h lauxlib.h lualib.h\nlobject.o: lobject.c lua.h luaconf.h ldo.h lobject.h llimits.h lstate.h \\\n  ltm.h lzio.h lmem.h lstring.h lgc.h lvm.h\nlopcodes.o: lopcodes.c lopcodes.h llimits.h lua.h luaconf.h\nloslib.o: loslib.c lua.h luaconf.h lauxlib.h lualib.h\nlparser.o: lparser.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \\\n  lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h \\\n  lfunc.h lstring.h lgc.h ltable.h\nlstate.o: lstate.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \\\n  ltm.h lzio.h lmem.h ldo.h lfunc.h lgc.h llex.h lstring.h ltable.h\nlstring.o: lstring.c lua.h luaconf.h lmem.h llimits.h lobject.h lstate.h \\\n  ltm.h lzio.h lstring.h lgc.h\nlstrlib.o: lstrlib.c lua.h luaconf.h lauxlib.h lualib.h\nltable.o: ltable.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \\\n  ltm.h lzio.h lmem.h ldo.h lgc.h ltable.h\nltablib.o: ltablib.c lua.h luaconf.h lauxlib.h lualib.h\nltm.o: ltm.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h lzio.h \\\n  lmem.h lstring.h lgc.h ltable.h\nlua.o: lua.c lua.h luaconf.h lauxlib.h lualib.h\nluac.o: luac.c lua.h luaconf.h lauxlib.h ldo.h lobject.h llimits.h \\\n  lstate.h ltm.h lzio.h lmem.h lfunc.h lopcodes.h lstring.h lgc.h \\\n  lundump.h\nlundump.o: lundump.c lua.h luaconf.h ldebug.h lstate.h lobject.h \\\n  llimits.h ltm.h lzio.h lmem.h ldo.h lfunc.h lstring.h lgc.h lundump.h\nlvm.o: lvm.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h ltm.h \\\n  lzio.h lmem.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h ltable.h lvm.h\nlzio.o: lzio.c lua.h luaconf.h llimits.h lmem.h lstate.h lobject.h ltm.h \\\n  lzio.h\nprint.o: print.c ldebug.h lstate.h lua.h luaconf.h lobject.h llimits.h \\\n  ltm.h lzio.h lmem.h lopcodes.h lundump.h\n\n# (end of Makefile)\n"
  },
  {
    "path": "deps/lua/src/fpconv.c",
    "content": "/* fpconv - Floating point conversion routines\n *\n * Copyright (c) 2011-2012  Mark Pulford <mark@kyne.com.au>\n *\n * Permission is hereby granted, free of charge, to any person obtaining\n * a copy of this software and associated documentation files (the\n * \"Software\"), to deal in the Software without restriction, including\n * without limitation the rights to use, copy, modify, merge, publish,\n * distribute, sublicense, and/or sell copies of the Software, and to\n * permit persons to whom the Software is furnished to do so, subject to\n * the following conditions:\n *\n * The above copyright notice and this permission notice shall be\n * included in all copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\n * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\n * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\n * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\n * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n */\n\n/* JSON uses a '.' decimal separator. strtod() / sprintf() under C libraries\n * with locale support will break when the decimal separator is a comma.\n *\n * fpconv_* will around these issues with a translation buffer if required.\n */\n\n#include <stdio.h>\n#include <stdlib.h>\n#include <assert.h>\n#include <string.h>\n\n#include \"fpconv.h\"\n\n/* Lua CJSON assumes the locale is the same for all threads within a\n * process and doesn't change after initialisation.\n *\n * This avoids the need for per thread storage or expensive checks\n * for call. */\nstatic char locale_decimal_point = '.';\n\n/* In theory multibyte decimal_points are possible, but\n * Lua CJSON only supports UTF-8 and known locales only have\n * single byte decimal points ([.,]).\n *\n * localconv() may not be thread safe (=>crash), and nl_langinfo() is\n * not supported on some platforms. Use sprintf() instead - if the\n * locale does change, at least Lua CJSON won't crash. */\nstatic void fpconv_update_locale()\n{\n    char buf[8];\n\n    snprintf(buf, sizeof(buf), \"%g\", 0.5);\n\n    /* Failing this test might imply the platform has a buggy dtoa\n     * implementation or wide characters */\n    if (buf[0] != '0' || buf[2] != '5' || buf[3] != 0) {\n        fprintf(stderr, \"Error: wide characters found or printf() bug.\");\n        abort();\n    }\n\n    locale_decimal_point = buf[1];\n}\n\n/* Check for a valid number character: [-+0-9a-yA-Y.]\n * Eg: -0.6e+5, infinity, 0xF0.F0pF0\n *\n * Used to find the probable end of a number. It doesn't matter if\n * invalid characters are counted - strtod() will find the valid\n * number if it exists.  The risk is that slightly more memory might\n * be allocated before a parse error occurs. */\nstatic inline int valid_number_character(char ch)\n{\n    char lower_ch;\n\n    if ('0' <= ch && ch <= '9')\n        return 1;\n    if (ch == '-' || ch == '+' || ch == '.')\n        return 1;\n\n    /* Hex digits, exponent (e), base (p), \"infinity\",.. */\n    lower_ch = ch | 0x20;\n    if ('a' <= lower_ch && lower_ch <= 'y')\n        return 1;\n\n    return 0;\n}\n\n/* Calculate the size of the buffer required for a strtod locale\n * conversion. */\nstatic int strtod_buffer_size(const char *s)\n{\n    const char *p = s;\n\n    while (valid_number_character(*p))\n        p++;\n\n    return p - s;\n}\n\n/* Similar to strtod(), but must be passed the current locale's decimal point\n * character. Guaranteed to be called at the start of any valid number in a string */\ndouble fpconv_strtod(const char *nptr, char **endptr)\n{\n    char localbuf[FPCONV_G_FMT_BUFSIZE];\n    char *buf, *endbuf, *dp;\n    int buflen;\n    double value;\n\n    /* System strtod() is fine when decimal point is '.' */\n    if (locale_decimal_point == '.')\n        return strtod(nptr, endptr);\n\n    buflen = strtod_buffer_size(nptr);\n    if (!buflen) {\n        /* No valid characters found, standard strtod() return */\n        *endptr = (char *)nptr;\n        return 0;\n    }\n\n    /* Duplicate number into buffer */\n    if (buflen >= FPCONV_G_FMT_BUFSIZE) {\n        /* Handle unusually large numbers */\n        buf = malloc(buflen + 1);\n        if (!buf) {\n            fprintf(stderr, \"Out of memory\");\n            abort();\n        }\n    } else {\n        /* This is the common case.. */\n        buf = localbuf;\n    }\n    memcpy(buf, nptr, buflen);\n    buf[buflen] = 0;\n\n    /* Update decimal point character if found */\n    dp = strchr(buf, '.');\n    if (dp)\n        *dp = locale_decimal_point;\n\n    value = strtod(buf, &endbuf);\n    *endptr = (char *)&nptr[endbuf - buf];\n    if (buflen >= FPCONV_G_FMT_BUFSIZE)\n        free(buf);\n\n    return value;\n}\n\n/* \"fmt\" must point to a buffer of at least 6 characters */\nstatic void set_number_format(char *fmt, int precision)\n{\n    int d1, d2, i;\n\n    assert(1 <= precision && precision <= 14);\n\n    /* Create printf format (%.14g) from precision */\n    d1 = precision / 10;\n    d2 = precision % 10;\n    fmt[0] = '%';\n    fmt[1] = '.';\n    i = 2;\n    if (d1) {\n        fmt[i++] = '0' + d1;\n    }\n    fmt[i++] = '0' + d2;\n    fmt[i++] = 'g';\n    fmt[i] = 0;\n}\n\n/* Assumes there is always at least 32 characters available in the target buffer */\nint fpconv_g_fmt(char *str, double num, int precision)\n{\n    char buf[FPCONV_G_FMT_BUFSIZE];\n    char fmt[6];\n    int len;\n    char *b;\n\n    set_number_format(fmt, precision);\n\n    /* Pass through when decimal point character is dot. */\n    if (locale_decimal_point == '.')\n        return snprintf(str, FPCONV_G_FMT_BUFSIZE, fmt, num);\n\n    /* snprintf() to a buffer then translate for other decimal point characters */\n    len = snprintf(buf, FPCONV_G_FMT_BUFSIZE, fmt, num);\n\n    /* Copy into target location. Translate decimal point if required */\n    b = buf;\n    do {\n        *str++ = (*b == locale_decimal_point ? '.' : *b);\n    } while(*b++);\n\n    return len;\n}\n\nvoid fpconv_init()\n{\n    fpconv_update_locale();\n}\n\n/* vi:ai et sw=4 ts=4:\n */\n"
  },
  {
    "path": "deps/lua/src/fpconv.h",
    "content": "/* Lua CJSON floating point conversion routines */\n\n/* Buffer required to store the largest string representation of a double.\n *\n * Longest double printed with %.14g is 21 characters long:\n * -1.7976931348623e+308 */\n# define FPCONV_G_FMT_BUFSIZE   32\n\n#ifdef USE_INTERNAL_FPCONV\nstatic inline void fpconv_init()\n{\n    /* Do nothing - not required */\n}\n#else\nextern void fpconv_init();\n#endif\n\nextern int fpconv_g_fmt(char*, double, int);\nextern double fpconv_strtod(const char*, char**);\n\n/* vi:ai et sw=4 ts=4:\n */\n"
  },
  {
    "path": "deps/lua/src/lapi.c",
    "content": "/*\n** $Id: lapi.c,v 2.55.1.5 2008/07/04 18:41:18 roberto Exp $\n** Lua API\n** See Copyright Notice in lua.h\n*/\n\n\n#include <assert.h>\n#include <math.h>\n#include <stdarg.h>\n#include <string.h>\n\n#define lapi_c\n#define LUA_CORE\n\n#include \"lua.h\"\n\n#include \"lapi.h\"\n#include \"ldebug.h\"\n#include \"ldo.h\"\n#include \"lfunc.h\"\n#include \"lgc.h\"\n#include \"lmem.h\"\n#include \"lobject.h\"\n#include \"lstate.h\"\n#include \"lstring.h\"\n#include \"ltable.h\"\n#include \"ltm.h\"\n#include \"lundump.h\"\n#include \"lvm.h\"\n\n\n\nconst char lua_ident[] =\n  \"$Lua: \" LUA_RELEASE \" \" LUA_COPYRIGHT \" $\\n\"\n  \"$Authors: \" LUA_AUTHORS \" $\\n\"\n  \"$URL: www.lua.org $\\n\";\n\n\n\n#define api_checknelems(L, n)\tapi_check(L, (n) <= (L->top - L->base))\n\n#define api_checkvalidindex(L, i)\tapi_check(L, (i) != luaO_nilobject)\n\n#define api_incr_top(L)   {api_check(L, L->top < L->ci->top); L->top++;}\n\n\n\nstatic TValue *index2adr (lua_State *L, int idx) {\n  if (idx > 0) {\n    TValue *o = L->base + (idx - 1);\n    api_check(L, idx <= L->ci->top - L->base);\n    if (o >= L->top) return cast(TValue *, luaO_nilobject);\n    else return o;\n  }\n  else if (idx > LUA_REGISTRYINDEX) {\n    api_check(L, idx != 0 && -idx <= L->top - L->base);\n    return L->top + idx;\n  }\n  else switch (idx) {  /* pseudo-indices */\n    case LUA_REGISTRYINDEX: return registry(L);\n    case LUA_ENVIRONINDEX: {\n      Closure *func = curr_func(L);\n      sethvalue(L, &L->env, func->c.env);\n      return &L->env;\n    }\n    case LUA_GLOBALSINDEX: return gt(L);\n    default: {\n      Closure *func = curr_func(L);\n      idx = LUA_GLOBALSINDEX - idx;\n      return (idx <= func->c.nupvalues)\n                ? &func->c.upvalue[idx-1]\n                : cast(TValue *, luaO_nilobject);\n    }\n  }\n}\n\n\nstatic Table *getcurrenv (lua_State *L) {\n  if (L->ci == L->base_ci)  /* no enclosing function? */\n    return hvalue(gt(L));  /* use global table as environment */\n  else {\n    Closure *func = curr_func(L);\n    return func->c.env;\n  }\n}\n\n\nvoid luaA_pushobject (lua_State *L, const TValue *o) {\n  setobj2s(L, L->top, o);\n  api_incr_top(L);\n}\n\n\nLUA_API int lua_checkstack (lua_State *L, int size) {\n  int res = 1;\n  lua_lock(L);\n  if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK)\n    res = 0;  /* stack overflow */\n  else if (size > 0) {\n    luaD_checkstack(L, size);\n    if (L->ci->top < L->top + size)\n      L->ci->top = L->top + size;\n  }\n  lua_unlock(L);\n  return res;\n}\n\n\nLUA_API void lua_xmove (lua_State *from, lua_State *to, int n) {\n  int i;\n  if (from == to) return;\n  lua_lock(to);\n  api_checknelems(from, n);\n  api_check(from, G(from) == G(to));\n  api_check(from, to->ci->top - to->top >= n);\n  from->top -= n;\n  for (i = 0; i < n; i++) {\n    setobj2s(to, to->top++, from->top + i);\n  }\n  lua_unlock(to);\n}\n\n\nLUA_API void lua_setlevel (lua_State *from, lua_State *to) {\n  to->nCcalls = from->nCcalls;\n}\n\n\nLUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {\n  lua_CFunction old;\n  lua_lock(L);\n  old = G(L)->panic;\n  G(L)->panic = panicf;\n  lua_unlock(L);\n  return old;\n}\n\n\nLUA_API lua_State *lua_newthread (lua_State *L) {\n  lua_State *L1;\n  lua_lock(L);\n  luaC_checkGC(L);\n  L1 = luaE_newthread(L);\n  setthvalue(L, L->top, L1);\n  api_incr_top(L);\n  lua_unlock(L);\n  luai_userstatethread(L, L1);\n  return L1;\n}\n\n\n\n/*\n** basic stack manipulation\n*/\n\n\nLUA_API int lua_gettop (lua_State *L) {\n  return cast_int(L->top - L->base);\n}\n\n\nLUA_API void lua_settop (lua_State *L, int idx) {\n  lua_lock(L);\n  if (idx >= 0) {\n    api_check(L, idx <= L->stack_last - L->base);\n    while (L->top < L->base + idx)\n      setnilvalue(L->top++);\n    L->top = L->base + idx;\n  }\n  else {\n    api_check(L, -(idx+1) <= (L->top - L->base));\n    L->top += idx+1;  /* `subtract' index (index is negative) */\n  }\n  lua_unlock(L);\n}\n\n\nLUA_API void lua_remove (lua_State *L, int idx) {\n  StkId p;\n  lua_lock(L);\n  p = index2adr(L, idx);\n  api_checkvalidindex(L, p);\n  while (++p < L->top) setobjs2s(L, p-1, p);\n  L->top--;\n  lua_unlock(L);\n}\n\n\nLUA_API void lua_insert (lua_State *L, int idx) {\n  StkId p;\n  StkId q;\n  lua_lock(L);\n  p = index2adr(L, idx);\n  api_checkvalidindex(L, p);\n  for (q = L->top; q>p; q--) setobjs2s(L, q, q-1);\n  setobjs2s(L, p, L->top);\n  lua_unlock(L);\n}\n\n\nLUA_API void lua_replace (lua_State *L, int idx) {\n  StkId o;\n  lua_lock(L);\n  /* explicit test for incompatible code */\n  if (idx == LUA_ENVIRONINDEX && L->ci == L->base_ci)\n    luaG_runerror(L, \"no calling environment\");\n  api_checknelems(L, 1);\n  o = index2adr(L, idx);\n  api_checkvalidindex(L, o);\n  if (idx == LUA_ENVIRONINDEX) {\n    Closure *func = curr_func(L);\n    api_check(L, ttistable(L->top - 1)); \n    func->c.env = hvalue(L->top - 1);\n    luaC_barrier(L, func, L->top - 1);\n  }\n  else {\n    setobj(L, o, L->top - 1);\n    if (idx < LUA_GLOBALSINDEX)  /* function upvalue? */\n      luaC_barrier(L, curr_func(L), L->top - 1);\n  }\n  L->top--;\n  lua_unlock(L);\n}\n\n\nLUA_API void lua_pushvalue (lua_State *L, int idx) {\n  lua_lock(L);\n  setobj2s(L, L->top, index2adr(L, idx));\n  api_incr_top(L);\n  lua_unlock(L);\n}\n\n\n\n/*\n** access functions (stack -> C)\n*/\n\n\nLUA_API int lua_type (lua_State *L, int idx) {\n  StkId o = index2adr(L, idx);\n  return (o == luaO_nilobject) ? LUA_TNONE : ttype(o);\n}\n\n\nLUA_API const char *lua_typename (lua_State *L, int t) {\n  UNUSED(L);\n  return (t == LUA_TNONE) ? \"no value\" : luaT_typenames[t];\n}\n\n\nLUA_API int lua_iscfunction (lua_State *L, int idx) {\n  StkId o = index2adr(L, idx);\n  return iscfunction(o);\n}\n\n\nLUA_API int lua_isnumber (lua_State *L, int idx) {\n  TValue n;\n  const TValue *o = index2adr(L, idx);\n  return tonumber(o, &n);\n}\n\n\nLUA_API int lua_isstring (lua_State *L, int idx) {\n  int t = lua_type(L, idx);\n  return (t == LUA_TSTRING || t == LUA_TNUMBER);\n}\n\n\nLUA_API int lua_isuserdata (lua_State *L, int idx) {\n  const TValue *o = index2adr(L, idx);\n  return (ttisuserdata(o) || ttislightuserdata(o));\n}\n\n\nLUA_API int lua_rawequal (lua_State *L, int index1, int index2) {\n  StkId o1 = index2adr(L, index1);\n  StkId o2 = index2adr(L, index2);\n  return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0\n         : luaO_rawequalObj(o1, o2);\n}\n\n\nLUA_API int lua_equal (lua_State *L, int index1, int index2) {\n  StkId o1, o2;\n  int i;\n  lua_lock(L);  /* may call tag method */\n  o1 = index2adr(L, index1);\n  o2 = index2adr(L, index2);\n  i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 : equalobj(L, o1, o2);\n  lua_unlock(L);\n  return i;\n}\n\n\nLUA_API int lua_lessthan (lua_State *L, int index1, int index2) {\n  StkId o1, o2;\n  int i;\n  lua_lock(L);  /* may call tag method */\n  o1 = index2adr(L, index1);\n  o2 = index2adr(L, index2);\n  i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0\n       : luaV_lessthan(L, o1, o2);\n  lua_unlock(L);\n  return i;\n}\n\n\n\nLUA_API lua_Number lua_tonumber (lua_State *L, int idx) {\n  TValue n;\n  const TValue *o = index2adr(L, idx);\n  if (tonumber(o, &n))\n    return nvalue(o);\n  else\n    return 0;\n}\n\n\nLUA_API lua_Integer lua_tointeger (lua_State *L, int idx) {\n  TValue n;\n  const TValue *o = index2adr(L, idx);\n  if (tonumber(o, &n)) {\n    lua_Integer res;\n    lua_Number num = nvalue(o);\n    lua_number2integer(res, num);\n    return res;\n  }\n  else\n    return 0;\n}\n\n\nLUA_API int lua_toboolean (lua_State *L, int idx) {\n  const TValue *o = index2adr(L, idx);\n  return !l_isfalse(o);\n}\n\n\nLUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {\n  StkId o = index2adr(L, idx);\n  if (!ttisstring(o)) {\n    lua_lock(L);  /* `luaV_tostring' may create a new string */\n    if (!luaV_tostring(L, o)) {  /* conversion failed? */\n      if (len != NULL) *len = 0;\n      lua_unlock(L);\n      return NULL;\n    }\n    luaC_checkGC(L);\n    o = index2adr(L, idx);  /* previous call may reallocate the stack */\n    lua_unlock(L);\n  }\n  if (len != NULL) *len = tsvalue(o)->len;\n  return svalue(o);\n}\n\n\nLUA_API size_t lua_objlen (lua_State *L, int idx) {\n  StkId o = index2adr(L, idx);\n  switch (ttype(o)) {\n    case LUA_TSTRING: return tsvalue(o)->len;\n    case LUA_TUSERDATA: return uvalue(o)->len;\n    case LUA_TTABLE: return luaH_getn(hvalue(o));\n    case LUA_TNUMBER: {\n      size_t l;\n      lua_lock(L);  /* `luaV_tostring' may create a new string */\n      l = (luaV_tostring(L, o) ? tsvalue(o)->len : 0);\n      lua_unlock(L);\n      return l;\n    }\n    default: return 0;\n  }\n}\n\n\nLUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {\n  StkId o = index2adr(L, idx);\n  return (!iscfunction(o)) ? NULL : clvalue(o)->c.f;\n}\n\n\nLUA_API void *lua_touserdata (lua_State *L, int idx) {\n  StkId o = index2adr(L, idx);\n  switch (ttype(o)) {\n    case LUA_TUSERDATA: return (rawuvalue(o) + 1);\n    case LUA_TLIGHTUSERDATA: return pvalue(o);\n    default: return NULL;\n  }\n}\n\n\nLUA_API lua_State *lua_tothread (lua_State *L, int idx) {\n  StkId o = index2adr(L, idx);\n  return (!ttisthread(o)) ? NULL : thvalue(o);\n}\n\n\nLUA_API const void *lua_topointer (lua_State *L, int idx) {\n  StkId o = index2adr(L, idx);\n  switch (ttype(o)) {\n    case LUA_TTABLE: return hvalue(o);\n    case LUA_TFUNCTION: return clvalue(o);\n    case LUA_TTHREAD: return thvalue(o);\n    case LUA_TUSERDATA:\n    case LUA_TLIGHTUSERDATA:\n      return lua_touserdata(L, idx);\n    default: return NULL;\n  }\n}\n\n\n\n/*\n** push functions (C -> stack)\n*/\n\n\nLUA_API void lua_pushnil (lua_State *L) {\n  lua_lock(L);\n  setnilvalue(L->top);\n  api_incr_top(L);\n  lua_unlock(L);\n}\n\n\nLUA_API void lua_pushnumber (lua_State *L, lua_Number n) {\n  lua_lock(L);\n  setnvalue(L->top, n);\n  api_incr_top(L);\n  lua_unlock(L);\n}\n\n\nLUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {\n  lua_lock(L);\n  setnvalue(L->top, cast_num(n));\n  api_incr_top(L);\n  lua_unlock(L);\n}\n\n\nLUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {\n  lua_lock(L);\n  luaC_checkGC(L);\n  setsvalue2s(L, L->top, luaS_newlstr(L, s, len));\n  api_incr_top(L);\n  lua_unlock(L);\n}\n\n\nLUA_API void lua_pushstring (lua_State *L, const char *s) {\n  if (s == NULL)\n    lua_pushnil(L);\n  else\n    lua_pushlstring(L, s, strlen(s));\n}\n\n\nLUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt,\n                                      va_list argp) {\n  const char *ret;\n  lua_lock(L);\n  luaC_checkGC(L);\n  ret = luaO_pushvfstring(L, fmt, argp);\n  lua_unlock(L);\n  return ret;\n}\n\n\nLUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) {\n  const char *ret;\n  va_list argp;\n  lua_lock(L);\n  luaC_checkGC(L);\n  va_start(argp, fmt);\n  ret = luaO_pushvfstring(L, fmt, argp);\n  va_end(argp);\n  lua_unlock(L);\n  return ret;\n}\n\n\nLUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {\n  Closure *cl;\n  lua_lock(L);\n  luaC_checkGC(L);\n  api_checknelems(L, n);\n  cl = luaF_newCclosure(L, n, getcurrenv(L));\n  cl->c.f = fn;\n  L->top -= n;\n  while (n--)\n    setobj2n(L, &cl->c.upvalue[n], L->top+n);\n  setclvalue(L, L->top, cl);\n  lua_assert(iswhite(obj2gco(cl)));\n  api_incr_top(L);\n  lua_unlock(L);\n}\n\n\nLUA_API void lua_pushboolean (lua_State *L, int b) {\n  lua_lock(L);\n  setbvalue(L->top, (b != 0));  /* ensure that true is 1 */\n  api_incr_top(L);\n  lua_unlock(L);\n}\n\n\nLUA_API void lua_pushlightuserdata (lua_State *L, void *p) {\n  lua_lock(L);\n  setpvalue(L->top, p);\n  api_incr_top(L);\n  lua_unlock(L);\n}\n\n\nLUA_API int lua_pushthread (lua_State *L) {\n  lua_lock(L);\n  setthvalue(L, L->top, L);\n  api_incr_top(L);\n  lua_unlock(L);\n  return (G(L)->mainthread == L);\n}\n\n\n\n/*\n** get functions (Lua -> stack)\n*/\n\n\nLUA_API void lua_gettable (lua_State *L, int idx) {\n  StkId t;\n  lua_lock(L);\n  t = index2adr(L, idx);\n  api_checkvalidindex(L, t);\n  luaV_gettable(L, t, L->top - 1, L->top - 1);\n  lua_unlock(L);\n}\n\n\nLUA_API void lua_getfield (lua_State *L, int idx, const char *k) {\n  StkId t;\n  TValue key;\n  lua_lock(L);\n  t = index2adr(L, idx);\n  api_checkvalidindex(L, t);\n  setsvalue(L, &key, luaS_new(L, k));\n  luaV_gettable(L, t, &key, L->top);\n  api_incr_top(L);\n  lua_unlock(L);\n}\n\n\nLUA_API void lua_rawget (lua_State *L, int idx) {\n  StkId t;\n  lua_lock(L);\n  t = index2adr(L, idx);\n  api_check(L, ttistable(t));\n  setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1));\n  lua_unlock(L);\n}\n\n\nLUA_API void lua_rawgeti (lua_State *L, int idx, int n) {\n  StkId o;\n  lua_lock(L);\n  o = index2adr(L, idx);\n  api_check(L, ttistable(o));\n  setobj2s(L, L->top, luaH_getnum(hvalue(o), n));\n  api_incr_top(L);\n  lua_unlock(L);\n}\n\n\nLUA_API void lua_createtable (lua_State *L, int narray, int nrec) {\n  lua_lock(L);\n  luaC_checkGC(L);\n  sethvalue(L, L->top, luaH_new(L, narray, nrec));\n  api_incr_top(L);\n  lua_unlock(L);\n}\n\n\nLUA_API int lua_getmetatable (lua_State *L, int objindex) {\n  const TValue *obj;\n  Table *mt = NULL;\n  int res;\n  lua_lock(L);\n  obj = index2adr(L, objindex);\n  switch (ttype(obj)) {\n    case LUA_TTABLE:\n      mt = hvalue(obj)->metatable;\n      break;\n    case LUA_TUSERDATA:\n      mt = uvalue(obj)->metatable;\n      break;\n    default:\n      mt = G(L)->mt[ttype(obj)];\n      break;\n  }\n  if (mt == NULL)\n    res = 0;\n  else {\n    sethvalue(L, L->top, mt);\n    api_incr_top(L);\n    res = 1;\n  }\n  lua_unlock(L);\n  return res;\n}\n\n\nLUA_API void lua_getfenv (lua_State *L, int idx) {\n  StkId o;\n  lua_lock(L);\n  o = index2adr(L, idx);\n  api_checkvalidindex(L, o);\n  switch (ttype(o)) {\n    case LUA_TFUNCTION:\n      sethvalue(L, L->top, clvalue(o)->c.env);\n      break;\n    case LUA_TUSERDATA:\n      sethvalue(L, L->top, uvalue(o)->env);\n      break;\n    case LUA_TTHREAD:\n      setobj2s(L, L->top,  gt(thvalue(o)));\n      break;\n    default:\n      setnilvalue(L->top);\n      break;\n  }\n  api_incr_top(L);\n  lua_unlock(L);\n}\n\n\n/*\n** set functions (stack -> Lua)\n*/\n\n\nLUA_API void lua_settable (lua_State *L, int idx) {\n  StkId t;\n  lua_lock(L);\n  api_checknelems(L, 2);\n  t = index2adr(L, idx);\n  api_checkvalidindex(L, t);\n  luaV_settable(L, t, L->top - 2, L->top - 1);\n  L->top -= 2;  /* pop index and value */\n  lua_unlock(L);\n}\n\n\nLUA_API void lua_setfield (lua_State *L, int idx, const char *k) {\n  StkId t;\n  TValue key;\n  lua_lock(L);\n  api_checknelems(L, 1);\n  t = index2adr(L, idx);\n  api_checkvalidindex(L, t);\n  setsvalue(L, &key, luaS_new(L, k));\n  luaV_settable(L, t, &key, L->top - 1);\n  L->top--;  /* pop value */\n  lua_unlock(L);\n}\n\n\nLUA_API void lua_rawset (lua_State *L, int idx) {\n  StkId t;\n  lua_lock(L);\n  api_checknelems(L, 2);\n  t = index2adr(L, idx);\n  api_check(L, ttistable(t));\n  setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1);\n  luaC_barriert(L, hvalue(t), L->top-1);\n  L->top -= 2;\n  lua_unlock(L);\n}\n\n\nLUA_API void lua_rawseti (lua_State *L, int idx, int n) {\n  StkId o;\n  lua_lock(L);\n  api_checknelems(L, 1);\n  o = index2adr(L, idx);\n  api_check(L, ttistable(o));\n  setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1);\n  luaC_barriert(L, hvalue(o), L->top-1);\n  L->top--;\n  lua_unlock(L);\n}\n\n\nLUA_API int lua_setmetatable (lua_State *L, int objindex) {\n  TValue *obj;\n  Table *mt;\n  lua_lock(L);\n  api_checknelems(L, 1);\n  obj = index2adr(L, objindex);\n  api_checkvalidindex(L, obj);\n  if (ttisnil(L->top - 1))\n    mt = NULL;\n  else {\n    api_check(L, ttistable(L->top - 1));\n    mt = hvalue(L->top - 1);\n  }\n  switch (ttype(obj)) {\n    case LUA_TTABLE: {\n      hvalue(obj)->metatable = mt;\n      if (mt)\n        luaC_objbarriert(L, hvalue(obj), mt);\n      break;\n    }\n    case LUA_TUSERDATA: {\n      uvalue(obj)->metatable = mt;\n      if (mt)\n        luaC_objbarrier(L, rawuvalue(obj), mt);\n      break;\n    }\n    default: {\n      G(L)->mt[ttype(obj)] = mt;\n      break;\n    }\n  }\n  L->top--;\n  lua_unlock(L);\n  return 1;\n}\n\n\nLUA_API int lua_setfenv (lua_State *L, int idx) {\n  StkId o;\n  int res = 1;\n  lua_lock(L);\n  api_checknelems(L, 1);\n  o = index2adr(L, idx);\n  api_checkvalidindex(L, o);\n  api_check(L, ttistable(L->top - 1));\n  switch (ttype(o)) {\n    case LUA_TFUNCTION:\n      clvalue(o)->c.env = hvalue(L->top - 1);\n      break;\n    case LUA_TUSERDATA:\n      uvalue(o)->env = hvalue(L->top - 1);\n      break;\n    case LUA_TTHREAD:\n      sethvalue(L, gt(thvalue(o)), hvalue(L->top - 1));\n      break;\n    default:\n      res = 0;\n      break;\n  }\n  if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));\n  L->top--;\n  lua_unlock(L);\n  return res;\n}\n\n\n/*\n** `load' and `call' functions (run Lua code)\n*/\n\n\n#define adjustresults(L,nres) \\\n    { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; }\n\n\n#define checkresults(L,na,nr) \\\n     api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na)))\n\t\n\nLUA_API void lua_call (lua_State *L, int nargs, int nresults) {\n  StkId func;\n  lua_lock(L);\n  api_checknelems(L, nargs+1);\n  checkresults(L, nargs, nresults);\n  func = L->top - (nargs+1);\n  luaD_call(L, func, nresults);\n  adjustresults(L, nresults);\n  lua_unlock(L);\n}\n\n\n\n/*\n** Execute a protected call.\n*/\nstruct CallS {  /* data to `f_call' */\n  StkId func;\n  int nresults;\n};\n\n\nstatic void f_call (lua_State *L, void *ud) {\n  struct CallS *c = cast(struct CallS *, ud);\n  luaD_call(L, c->func, c->nresults);\n}\n\n\n\nLUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) {\n  struct CallS c;\n  int status;\n  ptrdiff_t func;\n  lua_lock(L);\n  api_checknelems(L, nargs+1);\n  checkresults(L, nargs, nresults);\n  if (errfunc == 0)\n    func = 0;\n  else {\n    StkId o = index2adr(L, errfunc);\n    api_checkvalidindex(L, o);\n    func = savestack(L, o);\n  }\n  c.func = L->top - (nargs+1);  /* function to be called */\n  c.nresults = nresults;\n  status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func);\n  adjustresults(L, nresults);\n  lua_unlock(L);\n  return status;\n}\n\n\n/*\n** Execute a protected C call.\n*/\nstruct CCallS {  /* data to `f_Ccall' */\n  lua_CFunction func;\n  void *ud;\n};\n\n\nstatic void f_Ccall (lua_State *L, void *ud) {\n  struct CCallS *c = cast(struct CCallS *, ud);\n  Closure *cl;\n  cl = luaF_newCclosure(L, 0, getcurrenv(L));\n  cl->c.f = c->func;\n  setclvalue(L, L->top, cl);  /* push function */\n  api_incr_top(L);\n  setpvalue(L->top, c->ud);  /* push only argument */\n  api_incr_top(L);\n  luaD_call(L, L->top - 2, 0);\n}\n\n\nLUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) {\n  struct CCallS c;\n  int status;\n  lua_lock(L);\n  c.func = func;\n  c.ud = ud;\n  status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0);\n  lua_unlock(L);\n  return status;\n}\n\n\nLUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,\n                      const char *chunkname) {\n  ZIO z;\n  int status;\n  lua_lock(L);\n  if (!chunkname) chunkname = \"?\";\n  luaZ_init(L, &z, reader, data);\n  status = luaD_protectedparser(L, &z, chunkname);\n  lua_unlock(L);\n  return status;\n}\n\n\nLUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) {\n  int status;\n  TValue *o;\n  lua_lock(L);\n  api_checknelems(L, 1);\n  o = L->top - 1;\n  if (isLfunction(o))\n    status = luaU_dump(L, clvalue(o)->l.p, writer, data, 0);\n  else\n    status = 1;\n  lua_unlock(L);\n  return status;\n}\n\n\nLUA_API int  lua_status (lua_State *L) {\n  return L->status;\n}\n\n\n/*\n** Garbage-collection function\n*/\n\nLUA_API int lua_gc (lua_State *L, int what, int data) {\n  int res = 0;\n  global_State *g;\n  lua_lock(L);\n  g = G(L);\n  switch (what) {\n    case LUA_GCSTOP: {\n      g->GCthreshold = MAX_LUMEM;\n      break;\n    }\n    case LUA_GCRESTART: {\n      g->GCthreshold = g->totalbytes;\n      break;\n    }\n    case LUA_GCCOLLECT: {\n      luaC_fullgc(L);\n      break;\n    }\n    case LUA_GCCOUNT: {\n      /* GC values are expressed in Kbytes: #bytes/2^10 */\n      res = cast_int(g->totalbytes >> 10);\n      break;\n    }\n    case LUA_GCCOUNTB: {\n      res = cast_int(g->totalbytes & 0x3ff);\n      break;\n    }\n    case LUA_GCSTEP: {\n      lu_mem a = (cast(lu_mem, data) << 10);\n      if (a <= g->totalbytes)\n        g->GCthreshold = g->totalbytes - a;\n      else\n        g->GCthreshold = 0;\n      while (g->GCthreshold <= g->totalbytes) {\n        luaC_step(L);\n        if (g->gcstate == GCSpause) {  /* end of cycle? */\n          res = 1;  /* signal it */\n          break;\n        }\n      }\n      break;\n    }\n    case LUA_GCSETPAUSE: {\n      res = g->gcpause;\n      g->gcpause = data;\n      break;\n    }\n    case LUA_GCSETSTEPMUL: {\n      res = g->gcstepmul;\n      g->gcstepmul = data;\n      break;\n    }\n    default: res = -1;  /* invalid option */\n  }\n  lua_unlock(L);\n  return res;\n}\n\n\n\n/*\n** miscellaneous functions\n*/\n\n\nLUA_API int lua_error (lua_State *L) {\n  lua_lock(L);\n  api_checknelems(L, 1);\n  luaG_errormsg(L);\n  lua_unlock(L);\n  return 0;  /* to avoid warnings */\n}\n\n\nLUA_API int lua_next (lua_State *L, int idx) {\n  StkId t;\n  int more;\n  lua_lock(L);\n  t = index2adr(L, idx);\n  api_check(L, ttistable(t));\n  more = luaH_next(L, hvalue(t), L->top - 1);\n  if (more) {\n    api_incr_top(L);\n  }\n  else  /* no more elements */\n    L->top -= 1;  /* remove key */\n  lua_unlock(L);\n  return more;\n}\n\n\nLUA_API void lua_concat (lua_State *L, int n) {\n  lua_lock(L);\n  api_checknelems(L, n);\n  if (n >= 2) {\n    luaC_checkGC(L);\n    luaV_concat(L, n, cast_int(L->top - L->base) - 1);\n    L->top -= (n-1);\n  }\n  else if (n == 0) {  /* push empty string */\n    setsvalue2s(L, L->top, luaS_newlstr(L, \"\", 0));\n    api_incr_top(L);\n  }\n  /* else n == 1; nothing to do */\n  lua_unlock(L);\n}\n\n\nLUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) {\n  lua_Alloc f;\n  lua_lock(L);\n  if (ud) *ud = G(L)->ud;\n  f = G(L)->frealloc;\n  lua_unlock(L);\n  return f;\n}\n\n\nLUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) {\n  lua_lock(L);\n  G(L)->ud = ud;\n  G(L)->frealloc = f;\n  lua_unlock(L);\n}\n\n\nLUA_API void *lua_newuserdata (lua_State *L, size_t size) {\n  Udata *u;\n  lua_lock(L);\n  luaC_checkGC(L);\n  u = luaS_newudata(L, size, getcurrenv(L));\n  setuvalue(L, L->top, u);\n  api_incr_top(L);\n  lua_unlock(L);\n  return u + 1;\n}\n\n\n\n\nstatic const char *aux_upvalue (StkId fi, int n, TValue **val) {\n  Closure *f;\n  if (!ttisfunction(fi)) return NULL;\n  f = clvalue(fi);\n  if (f->c.isC) {\n    if (!(1 <= n && n <= f->c.nupvalues)) return NULL;\n    *val = &f->c.upvalue[n-1];\n    return \"\";\n  }\n  else {\n    Proto *p = f->l.p;\n    if (!(1 <= n && n <= p->sizeupvalues)) return NULL;\n    *val = f->l.upvals[n-1]->v;\n    return getstr(p->upvalues[n-1]);\n  }\n}\n\n\nLUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {\n  const char *name;\n  TValue *val;\n  lua_lock(L);\n  name = aux_upvalue(index2adr(L, funcindex), n, &val);\n  if (name) {\n    setobj2s(L, L->top, val);\n    api_incr_top(L);\n  }\n  lua_unlock(L);\n  return name;\n}\n\n\nLUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {\n  const char *name;\n  TValue *val;\n  StkId fi;\n  lua_lock(L);\n  fi = index2adr(L, funcindex);\n  api_checknelems(L, 1);\n  name = aux_upvalue(fi, n, &val);\n  if (name) {\n    L->top--;\n    setobj(L, val, L->top);\n    luaC_barrier(L, clvalue(fi), L->top);\n  }\n  lua_unlock(L);\n  return name;\n}\n\n"
  },
  {
    "path": "deps/lua/src/lapi.h",
    "content": "/*\n** $Id: lapi.h,v 2.2.1.1 2007/12/27 13:02:25 roberto Exp $\n** Auxiliary functions from Lua API\n** See Copyright Notice in lua.h\n*/\n\n#ifndef lapi_h\n#define lapi_h\n\n\n#include \"lobject.h\"\n\n\nLUAI_FUNC void luaA_pushobject (lua_State *L, const TValue *o);\n\n#endif\n"
  },
  {
    "path": "deps/lua/src/lauxlib.c",
    "content": "/*\n** $Id: lauxlib.c,v 1.159.1.3 2008/01/21 13:20:51 roberto Exp $\n** Auxiliary functions for building Lua libraries\n** See Copyright Notice in lua.h\n*/\n\n\n#include <ctype.h>\n#include <errno.h>\n#include <stdarg.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n\n/* This file uses only the official API of Lua.\n** Any function declared here could be written as an application function.\n*/\n\n#define lauxlib_c\n#define LUA_LIB\n\n#include \"lua.h\"\n\n#include \"lauxlib.h\"\n\n\n#define FREELIST_REF\t0\t/* free list of references */\n\n\n/* convert a stack index to positive */\n#define abs_index(L, i)\t\t((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \\\n\t\t\t\t\tlua_gettop(L) + (i) + 1)\n\n\n/*\n** {======================================================\n** Error-report functions\n** =======================================================\n*/\n\n\nLUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {\n  lua_Debug ar;\n  if (!lua_getstack(L, 0, &ar))  /* no stack frame? */\n    return luaL_error(L, \"bad argument #%d (%s)\", narg, extramsg);\n  lua_getinfo(L, \"n\", &ar);\n  if (strcmp(ar.namewhat, \"method\") == 0) {\n    narg--;  /* do not count `self' */\n    if (narg == 0)  /* error is in the self argument itself? */\n      return luaL_error(L, \"calling \" LUA_QS \" on bad self (%s)\",\n                           ar.name, extramsg);\n  }\n  if (ar.name == NULL)\n    ar.name = \"?\";\n  return luaL_error(L, \"bad argument #%d to \" LUA_QS \" (%s)\",\n                        narg, ar.name, extramsg);\n}\n\n\nLUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) {\n  const char *msg = lua_pushfstring(L, \"%s expected, got %s\",\n                                    tname, luaL_typename(L, narg));\n  return luaL_argerror(L, narg, msg);\n}\n\n\nstatic void tag_error (lua_State *L, int narg, int tag) {\n  luaL_typerror(L, narg, lua_typename(L, tag));\n}\n\n\nLUALIB_API void luaL_where (lua_State *L, int level) {\n  lua_Debug ar;\n  if (lua_getstack(L, level, &ar)) {  /* check function at level */\n    lua_getinfo(L, \"Sl\", &ar);  /* get info about it */\n    if (ar.currentline > 0) {  /* is there info? */\n      lua_pushfstring(L, \"%s:%d: \", ar.short_src, ar.currentline);\n      return;\n    }\n  }\n  lua_pushliteral(L, \"\");  /* else, no information available... */\n}\n\n\nLUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {\n  va_list argp;\n  va_start(argp, fmt);\n  luaL_where(L, 1);\n  lua_pushvfstring(L, fmt, argp);\n  va_end(argp);\n  lua_concat(L, 2);\n  return lua_error(L);\n}\n\n/* }====================================================== */\n\n\nLUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def,\n                                 const char *const lst[]) {\n  const char *name = (def) ? luaL_optstring(L, narg, def) :\n                             luaL_checkstring(L, narg);\n  int i;\n  for (i=0; lst[i]; i++)\n    if (strcmp(lst[i], name) == 0)\n      return i;\n  return luaL_argerror(L, narg,\n                       lua_pushfstring(L, \"invalid option \" LUA_QS, name));\n}\n\n\nLUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {\n  lua_getfield(L, LUA_REGISTRYINDEX, tname);  /* get registry.name */\n  if (!lua_isnil(L, -1))  /* name already in use? */\n    return 0;  /* leave previous value on top, but return 0 */\n  lua_pop(L, 1);\n  lua_newtable(L);  /* create metatable */\n  lua_pushvalue(L, -1);\n  lua_setfield(L, LUA_REGISTRYINDEX, tname);  /* registry.name = metatable */\n  return 1;\n}\n\n\nLUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {\n  void *p = lua_touserdata(L, ud);\n  if (p != NULL) {  /* value is a userdata? */\n    if (lua_getmetatable(L, ud)) {  /* does it have a metatable? */\n      lua_getfield(L, LUA_REGISTRYINDEX, tname);  /* get correct metatable */\n      if (lua_rawequal(L, -1, -2)) {  /* does it have the correct mt? */\n        lua_pop(L, 2);  /* remove both metatables */\n        return p;\n      }\n    }\n  }\n  luaL_typerror(L, ud, tname);  /* else error */\n  return NULL;  /* to avoid warnings */\n}\n\n\nLUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) {\n  if (!lua_checkstack(L, space))\n    luaL_error(L, \"stack overflow (%s)\", mes);\n}\n\n\nLUALIB_API void luaL_checktype (lua_State *L, int narg, int t) {\n  if (lua_type(L, narg) != t)\n    tag_error(L, narg, t);\n}\n\n\nLUALIB_API void luaL_checkany (lua_State *L, int narg) {\n  if (lua_type(L, narg) == LUA_TNONE)\n    luaL_argerror(L, narg, \"value expected\");\n}\n\n\nLUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) {\n  const char *s = lua_tolstring(L, narg, len);\n  if (!s) tag_error(L, narg, LUA_TSTRING);\n  return s;\n}\n\n\nLUALIB_API const char *luaL_optlstring (lua_State *L, int narg,\n                                        const char *def, size_t *len) {\n  if (lua_isnoneornil(L, narg)) {\n    if (len)\n      *len = (def ? strlen(def) : 0);\n    return def;\n  }\n  else return luaL_checklstring(L, narg, len);\n}\n\n\nLUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) {\n  lua_Number d = lua_tonumber(L, narg);\n  if (d == 0 && !lua_isnumber(L, narg))  /* avoid extra test when d is not 0 */\n    tag_error(L, narg, LUA_TNUMBER);\n  return d;\n}\n\n\nLUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {\n  return luaL_opt(L, luaL_checknumber, narg, def);\n}\n\n\nLUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {\n  lua_Integer d = lua_tointeger(L, narg);\n  if (d == 0 && !lua_isnumber(L, narg))  /* avoid extra test when d is not 0 */\n    tag_error(L, narg, LUA_TNUMBER);\n  return d;\n}\n\n\nLUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg,\n                                                      lua_Integer def) {\n  return luaL_opt(L, luaL_checkinteger, narg, def);\n}\n\n\nLUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {\n  if (!lua_getmetatable(L, obj))  /* no metatable? */\n    return 0;\n  lua_pushstring(L, event);\n  lua_rawget(L, -2);\n  if (lua_isnil(L, -1)) {\n    lua_pop(L, 2);  /* remove metatable and metafield */\n    return 0;\n  }\n  else {\n    lua_remove(L, -2);  /* remove only metatable */\n    return 1;\n  }\n}\n\n\nLUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {\n  obj = abs_index(L, obj);\n  if (!luaL_getmetafield(L, obj, event))  /* no metafield? */\n    return 0;\n  lua_pushvalue(L, obj);\n  lua_call(L, 1, 1);\n  return 1;\n}\n\n\nLUALIB_API void (luaL_register) (lua_State *L, const char *libname,\n                                const luaL_Reg *l) {\n  luaI_openlib(L, libname, l, 0);\n}\n\n\nstatic int libsize (const luaL_Reg *l) {\n  int size = 0;\n  for (; l->name; l++) size++;\n  return size;\n}\n\n\nLUALIB_API void luaI_openlib (lua_State *L, const char *libname,\n                              const luaL_Reg *l, int nup) {\n  if (libname) {\n    int size = libsize(l);\n    /* check whether lib already exists */\n    luaL_findtable(L, LUA_REGISTRYINDEX, \"_LOADED\", 1);\n    lua_getfield(L, -1, libname);  /* get _LOADED[libname] */\n    if (!lua_istable(L, -1)) {  /* not found? */\n      lua_pop(L, 1);  /* remove previous result */\n      /* try global variable (and create one if it does not exist) */\n      if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL)\n        luaL_error(L, \"name conflict for module \" LUA_QS, libname);\n      lua_pushvalue(L, -1);\n      lua_setfield(L, -3, libname);  /* _LOADED[libname] = new table */\n    }\n    lua_remove(L, -2);  /* remove _LOADED table */\n    lua_insert(L, -(nup+1));  /* move library table to below upvalues */\n  }\n  for (; l->name; l++) {\n    int i;\n    for (i=0; i<nup; i++)  /* copy upvalues to the top */\n      lua_pushvalue(L, -nup);\n    lua_pushcclosure(L, l->func, nup);\n    lua_setfield(L, -(nup+2), l->name);\n  }\n  lua_pop(L, nup);  /* remove upvalues */\n}\n\n\n\n/*\n** {======================================================\n** getn-setn: size for arrays\n** =======================================================\n*/\n\n#if defined(LUA_COMPAT_GETN)\n\nstatic int checkint (lua_State *L, int topop) {\n  int n = (lua_type(L, -1) == LUA_TNUMBER) ? lua_tointeger(L, -1) : -1;\n  lua_pop(L, topop);\n  return n;\n}\n\n\nstatic void getsizes (lua_State *L) {\n  lua_getfield(L, LUA_REGISTRYINDEX, \"LUA_SIZES\");\n  if (lua_isnil(L, -1)) {  /* no `size' table? */\n    lua_pop(L, 1);  /* remove nil */\n    lua_newtable(L);  /* create it */\n    lua_pushvalue(L, -1);  /* `size' will be its own metatable */\n    lua_setmetatable(L, -2);\n    lua_pushliteral(L, \"kv\");\n    lua_setfield(L, -2, \"__mode\");  /* metatable(N).__mode = \"kv\" */\n    lua_pushvalue(L, -1);\n    lua_setfield(L, LUA_REGISTRYINDEX, \"LUA_SIZES\");  /* store in register */\n  }\n}\n\n\nLUALIB_API void luaL_setn (lua_State *L, int t, int n) {\n  t = abs_index(L, t);\n  lua_pushliteral(L, \"n\");\n  lua_rawget(L, t);\n  if (checkint(L, 1) >= 0) {  /* is there a numeric field `n'? */\n    lua_pushliteral(L, \"n\");  /* use it */\n    lua_pushinteger(L, n);\n    lua_rawset(L, t);\n  }\n  else {  /* use `sizes' */\n    getsizes(L);\n    lua_pushvalue(L, t);\n    lua_pushinteger(L, n);\n    lua_rawset(L, -3);  /* sizes[t] = n */\n    lua_pop(L, 1);  /* remove `sizes' */\n  }\n}\n\n\nLUALIB_API int luaL_getn (lua_State *L, int t) {\n  int n;\n  t = abs_index(L, t);\n  lua_pushliteral(L, \"n\");  /* try t.n */\n  lua_rawget(L, t);\n  if ((n = checkint(L, 1)) >= 0) return n;\n  getsizes(L);  /* else try sizes[t] */\n  lua_pushvalue(L, t);\n  lua_rawget(L, -2);\n  if ((n = checkint(L, 2)) >= 0) return n;\n  return (int)lua_objlen(L, t);\n}\n\n#endif\n\n/* }====================================================== */\n\n\n\nLUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,\n                                                               const char *r) {\n  const char *wild;\n  size_t l = strlen(p);\n  luaL_Buffer b;\n  luaL_buffinit(L, &b);\n  while ((wild = strstr(s, p)) != NULL) {\n    luaL_addlstring(&b, s, wild - s);  /* push prefix */\n    luaL_addstring(&b, r);  /* push replacement in place of pattern */\n    s = wild + l;  /* continue after `p' */\n  }\n  luaL_addstring(&b, s);  /* push last suffix */\n  luaL_pushresult(&b);\n  return lua_tostring(L, -1);\n}\n\n\nLUALIB_API const char *luaL_findtable (lua_State *L, int idx,\n                                       const char *fname, int szhint) {\n  const char *e;\n  lua_pushvalue(L, idx);\n  do {\n    e = strchr(fname, '.');\n    if (e == NULL) e = fname + strlen(fname);\n    lua_pushlstring(L, fname, e - fname);\n    lua_rawget(L, -2);\n    if (lua_isnil(L, -1)) {  /* no such field? */\n      lua_pop(L, 1);  /* remove this nil */\n      lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */\n      lua_pushlstring(L, fname, e - fname);\n      lua_pushvalue(L, -2);\n      lua_settable(L, -4);  /* set new table into field */\n    }\n    else if (!lua_istable(L, -1)) {  /* field has a non-table value? */\n      lua_pop(L, 2);  /* remove table and value */\n      return fname;  /* return problematic part of the name */\n    }\n    lua_remove(L, -2);  /* remove previous table */\n    fname = e + 1;\n  } while (*e == '.');\n  return NULL;\n}\n\n\n\n/*\n** {======================================================\n** Generic Buffer manipulation\n** =======================================================\n*/\n\n\n#define bufflen(B)\t((B)->p - (B)->buffer)\n#define bufffree(B)\t((size_t)(LUAL_BUFFERSIZE - bufflen(B)))\n\n#define LIMIT\t(LUA_MINSTACK/2)\n\n\nstatic int emptybuffer (luaL_Buffer *B) {\n  size_t l = bufflen(B);\n  if (l == 0) return 0;  /* put nothing on stack */\n  else {\n    lua_pushlstring(B->L, B->buffer, l);\n    B->p = B->buffer;\n    B->lvl++;\n    return 1;\n  }\n}\n\n\nstatic void adjuststack (luaL_Buffer *B) {\n  if (B->lvl > 1) {\n    lua_State *L = B->L;\n    int toget = 1;  /* number of levels to concat */\n    size_t toplen = lua_strlen(L, -1);\n    do {\n      size_t l = lua_strlen(L, -(toget+1));\n      if (B->lvl - toget + 1 >= LIMIT || toplen > l) {\n        toplen += l;\n        toget++;\n      }\n      else break;\n    } while (toget < B->lvl);\n    lua_concat(L, toget);\n    B->lvl = B->lvl - toget + 1;\n  }\n}\n\n\nLUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) {\n  if (emptybuffer(B))\n    adjuststack(B);\n  return B->buffer;\n}\n\n\nLUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {\n  while (l--)\n    luaL_addchar(B, *s++);\n}\n\n\nLUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {\n  luaL_addlstring(B, s, strlen(s));\n}\n\n\nLUALIB_API void luaL_pushresult (luaL_Buffer *B) {\n  emptybuffer(B);\n  lua_concat(B->L, B->lvl);\n  B->lvl = 1;\n}\n\n\nLUALIB_API void luaL_addvalue (luaL_Buffer *B) {\n  lua_State *L = B->L;\n  size_t vl;\n  const char *s = lua_tolstring(L, -1, &vl);\n  if (vl <= bufffree(B)) {  /* fit into buffer? */\n    memcpy(B->p, s, vl);  /* put it there */\n    B->p += vl;\n    lua_pop(L, 1);  /* remove from stack */\n  }\n  else {\n    if (emptybuffer(B))\n      lua_insert(L, -2);  /* put buffer before new value */\n    B->lvl++;  /* add new value into B stack */\n    adjuststack(B);\n  }\n}\n\n\nLUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {\n  B->L = L;\n  B->p = B->buffer;\n  B->lvl = 0;\n}\n\n/* }====================================================== */\n\n\nLUALIB_API int luaL_ref (lua_State *L, int t) {\n  int ref;\n  t = abs_index(L, t);\n  if (lua_isnil(L, -1)) {\n    lua_pop(L, 1);  /* remove from stack */\n    return LUA_REFNIL;  /* `nil' has a unique fixed reference */\n  }\n  lua_rawgeti(L, t, FREELIST_REF);  /* get first free element */\n  ref = (int)lua_tointeger(L, -1);  /* ref = t[FREELIST_REF] */\n  lua_pop(L, 1);  /* remove it from stack */\n  if (ref != 0) {  /* any free element? */\n    lua_rawgeti(L, t, ref);  /* remove it from list */\n    lua_rawseti(L, t, FREELIST_REF);  /* (t[FREELIST_REF] = t[ref]) */\n  }\n  else {  /* no free elements */\n    ref = (int)lua_objlen(L, t);\n    ref++;  /* create new reference */\n  }\n  lua_rawseti(L, t, ref);\n  return ref;\n}\n\n\nLUALIB_API void luaL_unref (lua_State *L, int t, int ref) {\n  if (ref >= 0) {\n    t = abs_index(L, t);\n    lua_rawgeti(L, t, FREELIST_REF);\n    lua_rawseti(L, t, ref);  /* t[ref] = t[FREELIST_REF] */\n    lua_pushinteger(L, ref);\n    lua_rawseti(L, t, FREELIST_REF);  /* t[FREELIST_REF] = ref */\n  }\n}\n\n\n\n/*\n** {======================================================\n** Load functions\n** =======================================================\n*/\n\ntypedef struct LoadF {\n  int extraline;\n  FILE *f;\n  char buff[LUAL_BUFFERSIZE];\n} LoadF;\n\n\nstatic const char *getF (lua_State *L, void *ud, size_t *size) {\n  LoadF *lf = (LoadF *)ud;\n  (void)L;\n  if (lf->extraline) {\n    lf->extraline = 0;\n    *size = 1;\n    return \"\\n\";\n  }\n  if (feof(lf->f)) return NULL;\n  *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f);\n  return (*size > 0) ? lf->buff : NULL;\n}\n\n\nstatic int errfile (lua_State *L, const char *what, int fnameindex) {\n  const char *serr = strerror(errno);\n  const char *filename = lua_tostring(L, fnameindex) + 1;\n  lua_pushfstring(L, \"cannot %s %s: %s\", what, filename, serr);\n  lua_remove(L, fnameindex);\n  return LUA_ERRFILE;\n}\n\n\nLUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {\n  LoadF lf;\n  int status, readstatus;\n  int c;\n  int fnameindex = lua_gettop(L) + 1;  /* index of filename on the stack */\n  lf.extraline = 0;\n  if (filename == NULL) {\n    lua_pushliteral(L, \"=stdin\");\n    lf.f = stdin;\n  }\n  else {\n    lua_pushfstring(L, \"@%s\", filename);\n    lf.f = fopen(filename, \"r\");\n    if (lf.f == NULL) return errfile(L, \"open\", fnameindex);\n  }\n  c = getc(lf.f);\n  if (c == '#') {  /* Unix exec. file? */\n    lf.extraline = 1;\n    while ((c = getc(lf.f)) != EOF && c != '\\n') ;  /* skip first line */\n    if (c == '\\n') c = getc(lf.f);\n  }\n  if (c == LUA_SIGNATURE[0] && filename) {  /* binary file? */\n    lf.f = freopen(filename, \"rb\", lf.f);  /* reopen in binary mode */\n    if (lf.f == NULL) return errfile(L, \"reopen\", fnameindex);\n    /* skip eventual `#!...' */\n   while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ;\n   lf.extraline = 0;\n  }\n  ungetc(c, lf.f);\n  status = lua_load(L, getF, &lf, lua_tostring(L, -1));\n  readstatus = ferror(lf.f);\n  if (filename) fclose(lf.f);  /* close file (even in case of errors) */\n  if (readstatus) {\n    lua_settop(L, fnameindex);  /* ignore results from `lua_load' */\n    return errfile(L, \"read\", fnameindex);\n  }\n  lua_remove(L, fnameindex);\n  return status;\n}\n\n\ntypedef struct LoadS {\n  const char *s;\n  size_t size;\n} LoadS;\n\n\nstatic const char *getS (lua_State *L, void *ud, size_t *size) {\n  LoadS *ls = (LoadS *)ud;\n  (void)L;\n  if (ls->size == 0) return NULL;\n  *size = ls->size;\n  ls->size = 0;\n  return ls->s;\n}\n\n\nLUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size,\n                                const char *name) {\n  LoadS ls;\n  ls.s = buff;\n  ls.size = size;\n  return lua_load(L, getS, &ls, name);\n}\n\n\nLUALIB_API int (luaL_loadstring) (lua_State *L, const char *s) {\n  return luaL_loadbuffer(L, s, strlen(s), s);\n}\n\n\n\n/* }====================================================== */\n\n\nstatic void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {\n  (void)ud;\n  (void)osize;\n  if (nsize == 0) {\n    free(ptr);\n    return NULL;\n  }\n  else\n    return realloc(ptr, nsize);\n}\n\n\nstatic int panic (lua_State *L) {\n  (void)L;  /* to avoid warnings */\n  fprintf(stderr, \"PANIC: unprotected error in call to Lua API (%s)\\n\",\n                   lua_tostring(L, -1));\n  return 0;\n}\n\n\nLUALIB_API lua_State *luaL_newstate (void) {\n  lua_State *L = lua_newstate(l_alloc, NULL);\n  if (L) lua_atpanic(L, &panic);\n  return L;\n}\n\n"
  },
  {
    "path": "deps/lua/src/lauxlib.h",
    "content": "/*\n** $Id: lauxlib.h,v 1.88.1.1 2007/12/27 13:02:25 roberto Exp $\n** Auxiliary functions for building Lua libraries\n** See Copyright Notice in lua.h\n*/\n\n\n#ifndef lauxlib_h\n#define lauxlib_h\n\n\n#include <stddef.h>\n#include <stdio.h>\n\n#include \"lua.h\"\n\n\n#if defined(LUA_COMPAT_GETN)\nLUALIB_API int (luaL_getn) (lua_State *L, int t);\nLUALIB_API void (luaL_setn) (lua_State *L, int t, int n);\n#else\n#define luaL_getn(L,i)          ((int)lua_objlen(L, i))\n#define luaL_setn(L,i,j)        ((void)0)  /* no op! */\n#endif\n\n#if defined(LUA_COMPAT_OPENLIB)\n#define luaI_openlib\tluaL_openlib\n#endif\n\n\n/* extra error code for `luaL_load' */\n#define LUA_ERRFILE     (LUA_ERRERR+1)\n\n\ntypedef struct luaL_Reg {\n  const char *name;\n  lua_CFunction func;\n} luaL_Reg;\n\n\n\nLUALIB_API void (luaI_openlib) (lua_State *L, const char *libname,\n                                const luaL_Reg *l, int nup);\nLUALIB_API void (luaL_register) (lua_State *L, const char *libname,\n                                const luaL_Reg *l);\nLUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e);\nLUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e);\nLUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname);\nLUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg);\nLUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg,\n                                                          size_t *l);\nLUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg,\n                                          const char *def, size_t *l);\nLUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg);\nLUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def);\n\nLUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg);\nLUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg,\n                                          lua_Integer def);\n\nLUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg);\nLUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t);\nLUALIB_API void (luaL_checkany) (lua_State *L, int narg);\n\nLUALIB_API int   (luaL_newmetatable) (lua_State *L, const char *tname);\nLUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname);\n\nLUALIB_API void (luaL_where) (lua_State *L, int lvl);\nLUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...);\n\nLUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def,\n                                   const char *const lst[]);\n\nLUALIB_API int (luaL_ref) (lua_State *L, int t);\nLUALIB_API void (luaL_unref) (lua_State *L, int t, int ref);\n\nLUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename);\nLUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz,\n                                  const char *name);\nLUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);\n\nLUALIB_API lua_State *(luaL_newstate) (void);\n\n\nLUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p,\n                                                  const char *r);\n\nLUALIB_API const char *(luaL_findtable) (lua_State *L, int idx,\n                                         const char *fname, int szhint);\n\n\n\n\n/*\n** ===============================================================\n** some useful macros\n** ===============================================================\n*/\n\n#define luaL_argcheck(L, cond,numarg,extramsg)\t\\\n\t\t((void)((cond) || luaL_argerror(L, (numarg), (extramsg))))\n#define luaL_checkstring(L,n)\t(luaL_checklstring(L, (n), NULL))\n#define luaL_optstring(L,n,d)\t(luaL_optlstring(L, (n), (d), NULL))\n#define luaL_checkint(L,n)\t((int)luaL_checkinteger(L, (n)))\n#define luaL_optint(L,n,d)\t((int)luaL_optinteger(L, (n), (d)))\n#define luaL_checklong(L,n)\t((long)luaL_checkinteger(L, (n)))\n#define luaL_optlong(L,n,d)\t((long)luaL_optinteger(L, (n), (d)))\n\n#define luaL_typename(L,i)\tlua_typename(L, lua_type(L,(i)))\n\n#define luaL_dofile(L, fn) \\\n\t(luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))\n\n#define luaL_dostring(L, s) \\\n\t(luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0))\n\n#define luaL_getmetatable(L,n)\t(lua_getfield(L, LUA_REGISTRYINDEX, (n)))\n\n#define luaL_opt(L,f,n,d)\t(lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))\n\n/*\n** {======================================================\n** Generic Buffer manipulation\n** =======================================================\n*/\n\n\n\ntypedef struct luaL_Buffer {\n  char *p;\t\t\t/* current position in buffer */\n  int lvl;  /* number of strings in the stack (level) */\n  lua_State *L;\n  char buffer[LUAL_BUFFERSIZE];\n} luaL_Buffer;\n\n#define luaL_addchar(B,c) \\\n  ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \\\n   (*(B)->p++ = (char)(c)))\n\n/* compatibility only */\n#define luaL_putchar(B,c)\tluaL_addchar(B,c)\n\n#define luaL_addsize(B,n)\t((B)->p += (n))\n\nLUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B);\nLUALIB_API char *(luaL_prepbuffer) (luaL_Buffer *B);\nLUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);\nLUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s);\nLUALIB_API void (luaL_addvalue) (luaL_Buffer *B);\nLUALIB_API void (luaL_pushresult) (luaL_Buffer *B);\n\n\n/* }====================================================== */\n\n\n/* compatibility with ref system */\n\n/* pre-defined references */\n#define LUA_NOREF       (-2)\n#define LUA_REFNIL      (-1)\n\n#define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \\\n      (lua_pushstring(L, \"unlocked references are obsolete\"), lua_error(L), 0))\n\n#define lua_unref(L,ref)        luaL_unref(L, LUA_REGISTRYINDEX, (ref))\n\n#define lua_getref(L,ref)       lua_rawgeti(L, LUA_REGISTRYINDEX, (ref))\n\n\n#define luaL_reg\tluaL_Reg\n\n#endif\n\n\n"
  },
  {
    "path": "deps/lua/src/lbaselib.c",
    "content": "/*\n** $Id: lbaselib.c,v 1.191.1.6 2008/02/14 16:46:22 roberto Exp $\n** Basic library\n** See Copyright Notice in lua.h\n*/\n\n\n\n#include <ctype.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#define lbaselib_c\n#define LUA_LIB\n\n#include \"lua.h\"\n\n#include \"lauxlib.h\"\n#include \"lualib.h\"\n\n\n\n\n/*\n** If your system does not support `stdout', you can just remove this function.\n** If you need, you can define your own `print' function, following this\n** model but changing `fputs' to put the strings at a proper place\n** (a console window or a log file, for instance).\n*/\nstatic int luaB_print (lua_State *L) {\n  int n = lua_gettop(L);  /* number of arguments */\n  int i;\n  lua_getglobal(L, \"tostring\");\n  for (i=1; i<=n; i++) {\n    const char *s;\n    lua_pushvalue(L, -1);  /* function to be called */\n    lua_pushvalue(L, i);   /* value to print */\n    lua_call(L, 1, 1);\n    s = lua_tostring(L, -1);  /* get result */\n    if (s == NULL)\n      return luaL_error(L, LUA_QL(\"tostring\") \" must return a string to \"\n                           LUA_QL(\"print\"));\n    if (i>1) fputs(\"\\t\", stdout);\n    fputs(s, stdout);\n    lua_pop(L, 1);  /* pop result */\n  }\n  fputs(\"\\n\", stdout);\n  return 0;\n}\n\n\nstatic int luaB_tonumber (lua_State *L) {\n  int base = luaL_optint(L, 2, 10);\n  if (base == 10) {  /* standard conversion */\n    luaL_checkany(L, 1);\n    if (lua_isnumber(L, 1)) {\n      lua_pushnumber(L, lua_tonumber(L, 1));\n      return 1;\n    }\n  }\n  else {\n    const char *s1 = luaL_checkstring(L, 1);\n    char *s2;\n    unsigned long n;\n    luaL_argcheck(L, 2 <= base && base <= 36, 2, \"base out of range\");\n    n = strtoul(s1, &s2, base);\n    if (s1 != s2) {  /* at least one valid digit? */\n      while (isspace((unsigned char)(*s2))) s2++;  /* skip trailing spaces */\n      if (*s2 == '\\0') {  /* no invalid trailing characters? */\n        lua_pushnumber(L, (lua_Number)n);\n        return 1;\n      }\n    }\n  }\n  lua_pushnil(L);  /* else not a number */\n  return 1;\n}\n\n\nstatic int luaB_error (lua_State *L) {\n  int level = luaL_optint(L, 2, 1);\n  lua_settop(L, 1);\n  if (lua_isstring(L, 1) && level > 0) {  /* add extra information? */\n    luaL_where(L, level);\n    lua_pushvalue(L, 1);\n    lua_concat(L, 2);\n  }\n  return lua_error(L);\n}\n\n\nstatic int luaB_getmetatable (lua_State *L) {\n  luaL_checkany(L, 1);\n  if (!lua_getmetatable(L, 1)) {\n    lua_pushnil(L);\n    return 1;  /* no metatable */\n  }\n  luaL_getmetafield(L, 1, \"__metatable\");\n  return 1;  /* returns either __metatable field (if present) or metatable */\n}\n\n\nstatic int luaB_setmetatable (lua_State *L) {\n  int t = lua_type(L, 2);\n  luaL_checktype(L, 1, LUA_TTABLE);\n  luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,\n                    \"nil or table expected\");\n  if (luaL_getmetafield(L, 1, \"__metatable\"))\n    luaL_error(L, \"cannot change a protected metatable\");\n  lua_settop(L, 2);\n  lua_setmetatable(L, 1);\n  return 1;\n}\n\n\nstatic void getfunc (lua_State *L, int opt) {\n  if (lua_isfunction(L, 1)) lua_pushvalue(L, 1);\n  else {\n    lua_Debug ar;\n    int level = opt ? luaL_optint(L, 1, 1) : luaL_checkint(L, 1);\n    luaL_argcheck(L, level >= 0, 1, \"level must be non-negative\");\n    if (lua_getstack(L, level, &ar) == 0)\n      luaL_argerror(L, 1, \"invalid level\");\n    lua_getinfo(L, \"f\", &ar);\n    if (lua_isnil(L, -1))\n      luaL_error(L, \"no function environment for tail call at level %d\",\n                    level);\n  }\n}\n\n\nstatic int luaB_getfenv (lua_State *L) {\n  getfunc(L, 1);\n  if (lua_iscfunction(L, -1))  /* is a C function? */\n    lua_pushvalue(L, LUA_GLOBALSINDEX);  /* return the thread's global env. */\n  else\n    lua_getfenv(L, -1);\n  return 1;\n}\n\n\nstatic int luaB_setfenv (lua_State *L) {\n  luaL_checktype(L, 2, LUA_TTABLE);\n  getfunc(L, 0);\n  lua_pushvalue(L, 2);\n  if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) {\n    /* change environment of current thread */\n    lua_pushthread(L);\n    lua_insert(L, -2);\n    lua_setfenv(L, -2);\n    return 0;\n  }\n  else if (lua_iscfunction(L, -2) || lua_setfenv(L, -2) == 0)\n    luaL_error(L,\n          LUA_QL(\"setfenv\") \" cannot change environment of given object\");\n  return 1;\n}\n\n\nstatic int luaB_rawequal (lua_State *L) {\n  luaL_checkany(L, 1);\n  luaL_checkany(L, 2);\n  lua_pushboolean(L, lua_rawequal(L, 1, 2));\n  return 1;\n}\n\n\nstatic int luaB_rawget (lua_State *L) {\n  luaL_checktype(L, 1, LUA_TTABLE);\n  luaL_checkany(L, 2);\n  lua_settop(L, 2);\n  lua_rawget(L, 1);\n  return 1;\n}\n\nstatic int luaB_rawset (lua_State *L) {\n  luaL_checktype(L, 1, LUA_TTABLE);\n  luaL_checkany(L, 2);\n  luaL_checkany(L, 3);\n  lua_settop(L, 3);\n  lua_rawset(L, 1);\n  return 1;\n}\n\n\nstatic int luaB_gcinfo (lua_State *L) {\n  lua_pushinteger(L, lua_getgccount(L));\n  return 1;\n}\n\n\nstatic int luaB_collectgarbage (lua_State *L) {\n  static const char *const opts[] = {\"stop\", \"restart\", \"collect\",\n    \"count\", \"step\", \"setpause\", \"setstepmul\", NULL};\n  static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,\n    LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL};\n  int o = luaL_checkoption(L, 1, \"collect\", opts);\n  int ex = luaL_optint(L, 2, 0);\n  int res = lua_gc(L, optsnum[o], ex);\n  switch (optsnum[o]) {\n    case LUA_GCCOUNT: {\n      int b = lua_gc(L, LUA_GCCOUNTB, 0);\n      lua_pushnumber(L, res + ((lua_Number)b/1024));\n      return 1;\n    }\n    case LUA_GCSTEP: {\n      lua_pushboolean(L, res);\n      return 1;\n    }\n    default: {\n      lua_pushnumber(L, res);\n      return 1;\n    }\n  }\n}\n\n\nstatic int luaB_type (lua_State *L) {\n  luaL_checkany(L, 1);\n  lua_pushstring(L, luaL_typename(L, 1));\n  return 1;\n}\n\n\nstatic int luaB_next (lua_State *L) {\n  luaL_checktype(L, 1, LUA_TTABLE);\n  lua_settop(L, 2);  /* create a 2nd argument if there isn't one */\n  if (lua_next(L, 1))\n    return 2;\n  else {\n    lua_pushnil(L);\n    return 1;\n  }\n}\n\n\nstatic int luaB_pairs (lua_State *L) {\n  luaL_checktype(L, 1, LUA_TTABLE);\n  lua_pushvalue(L, lua_upvalueindex(1));  /* return generator, */\n  lua_pushvalue(L, 1);  /* state, */\n  lua_pushnil(L);  /* and initial value */\n  return 3;\n}\n\n\nstatic int ipairsaux (lua_State *L) {\n  int i = luaL_checkint(L, 2);\n  luaL_checktype(L, 1, LUA_TTABLE);\n  i++;  /* next value */\n  lua_pushinteger(L, i);\n  lua_rawgeti(L, 1, i);\n  return (lua_isnil(L, -1)) ? 0 : 2;\n}\n\n\nstatic int luaB_ipairs (lua_State *L) {\n  luaL_checktype(L, 1, LUA_TTABLE);\n  lua_pushvalue(L, lua_upvalueindex(1));  /* return generator, */\n  lua_pushvalue(L, 1);  /* state, */\n  lua_pushinteger(L, 0);  /* and initial value */\n  return 3;\n}\n\n\nstatic int load_aux (lua_State *L, int status) {\n  if (status == 0)  /* OK? */\n    return 1;\n  else {\n    lua_pushnil(L);\n    lua_insert(L, -2);  /* put before error message */\n    return 2;  /* return nil plus error message */\n  }\n}\n\n\nstatic int luaB_loadstring (lua_State *L) {\n  size_t l;\n  const char *s = luaL_checklstring(L, 1, &l);\n  const char *chunkname = luaL_optstring(L, 2, s);\n  return load_aux(L, luaL_loadbuffer(L, s, l, chunkname));\n}\n\n\nstatic int luaB_loadfile (lua_State *L) {\n  const char *fname = luaL_optstring(L, 1, NULL);\n  return load_aux(L, luaL_loadfile(L, fname));\n}\n\n\n/*\n** Reader for generic `load' function: `lua_load' uses the\n** stack for internal stuff, so the reader cannot change the\n** stack top. Instead, it keeps its resulting string in a\n** reserved slot inside the stack.\n*/\nstatic const char *generic_reader (lua_State *L, void *ud, size_t *size) {\n  (void)ud;  /* to avoid warnings */\n  luaL_checkstack(L, 2, \"too many nested functions\");\n  lua_pushvalue(L, 1);  /* get function */\n  lua_call(L, 0, 1);  /* call it */\n  if (lua_isnil(L, -1)) {\n    *size = 0;\n    return NULL;\n  }\n  else if (lua_isstring(L, -1)) {\n    lua_replace(L, 3);  /* save string in a reserved stack slot */\n    return lua_tolstring(L, 3, size);\n  }\n  else luaL_error(L, \"reader function must return a string\");\n  return NULL;  /* to avoid warnings */\n}\n\n\nstatic int luaB_load (lua_State *L) {\n  int status;\n  const char *cname = luaL_optstring(L, 2, \"=(load)\");\n  luaL_checktype(L, 1, LUA_TFUNCTION);\n  lua_settop(L, 3);  /* function, eventual name, plus one reserved slot */\n  status = lua_load(L, generic_reader, NULL, cname);\n  return load_aux(L, status);\n}\n\n\nstatic int luaB_dofile (lua_State *L) {\n  const char *fname = luaL_optstring(L, 1, NULL);\n  int n = lua_gettop(L);\n  if (luaL_loadfile(L, fname) != 0) lua_error(L);\n  lua_call(L, 0, LUA_MULTRET);\n  return lua_gettop(L) - n;\n}\n\n\nstatic int luaB_assert (lua_State *L) {\n  luaL_checkany(L, 1);\n  if (!lua_toboolean(L, 1))\n    return luaL_error(L, \"%s\", luaL_optstring(L, 2, \"assertion failed!\"));\n  return lua_gettop(L);\n}\n\n\nstatic int luaB_unpack (lua_State *L) {\n  int i, e, n;\n  luaL_checktype(L, 1, LUA_TTABLE);\n  i = luaL_optint(L, 2, 1);\n  e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1));\n  if (i > e) return 0;  /* empty range */\n  n = e - i + 1;  /* number of elements */\n  if (n <= 0 || !lua_checkstack(L, n))  /* n <= 0 means arith. overflow */\n    return luaL_error(L, \"too many results to unpack\");\n  lua_rawgeti(L, 1, i);  /* push arg[i] (avoiding overflow problems) */\n  while (i++ < e)  /* push arg[i + 1...e] */\n    lua_rawgeti(L, 1, i);\n  return n;\n}\n\n\nstatic int luaB_select (lua_State *L) {\n  int n = lua_gettop(L);\n  if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') {\n    lua_pushinteger(L, n-1);\n    return 1;\n  }\n  else {\n    int i = luaL_checkint(L, 1);\n    if (i < 0) i = n + i;\n    else if (i > n) i = n;\n    luaL_argcheck(L, 1 <= i, 1, \"index out of range\");\n    return n - i;\n  }\n}\n\n\nstatic int luaB_pcall (lua_State *L) {\n  int status;\n  luaL_checkany(L, 1);\n  status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0);\n  lua_pushboolean(L, (status == 0));\n  lua_insert(L, 1);\n  return lua_gettop(L);  /* return status + all results */\n}\n\n\nstatic int luaB_xpcall (lua_State *L) {\n  int status;\n  luaL_checkany(L, 2);\n  lua_settop(L, 2);\n  lua_insert(L, 1);  /* put error function under function to be called */\n  status = lua_pcall(L, 0, LUA_MULTRET, 1);\n  lua_pushboolean(L, (status == 0));\n  lua_replace(L, 1);\n  return lua_gettop(L);  /* return status + all results */\n}\n\n\nstatic int luaB_tostring (lua_State *L) {\n  luaL_checkany(L, 1);\n  if (luaL_callmeta(L, 1, \"__tostring\"))  /* is there a metafield? */\n    return 1;  /* use its value */\n  switch (lua_type(L, 1)) {\n    case LUA_TNUMBER:\n      lua_pushstring(L, lua_tostring(L, 1));\n      break;\n    case LUA_TSTRING:\n      lua_pushvalue(L, 1);\n      break;\n    case LUA_TBOOLEAN:\n      lua_pushstring(L, (lua_toboolean(L, 1) ? \"true\" : \"false\"));\n      break;\n    case LUA_TNIL:\n      lua_pushliteral(L, \"nil\");\n      break;\n    default:\n      lua_pushfstring(L, \"%s: %p\", luaL_typename(L, 1), lua_topointer(L, 1));\n      break;\n  }\n  return 1;\n}\n\n\nstatic int luaB_newproxy (lua_State *L) {\n  lua_settop(L, 1);\n  lua_newuserdata(L, 0);  /* create proxy */\n  if (lua_toboolean(L, 1) == 0)\n    return 1;  /* no metatable */\n  else if (lua_isboolean(L, 1)) {\n    lua_newtable(L);  /* create a new metatable `m' ... */\n    lua_pushvalue(L, -1);  /* ... and mark `m' as a valid metatable */\n    lua_pushboolean(L, 1);\n    lua_rawset(L, lua_upvalueindex(1));  /* weaktable[m] = true */\n  }\n  else {\n    int validproxy = 0;  /* to check if weaktable[metatable(u)] == true */\n    if (lua_getmetatable(L, 1)) {\n      lua_rawget(L, lua_upvalueindex(1));\n      validproxy = lua_toboolean(L, -1);\n      lua_pop(L, 1);  /* remove value */\n    }\n    luaL_argcheck(L, validproxy, 1, \"boolean or proxy expected\");\n    lua_getmetatable(L, 1);  /* metatable is valid; get it */\n  }\n  lua_setmetatable(L, 2);\n  return 1;\n}\n\n\nstatic const luaL_Reg base_funcs[] = {\n  {\"assert\", luaB_assert},\n  {\"collectgarbage\", luaB_collectgarbage},\n  {\"dofile\", luaB_dofile},\n  {\"error\", luaB_error},\n  {\"gcinfo\", luaB_gcinfo},\n  {\"getfenv\", luaB_getfenv},\n  {\"getmetatable\", luaB_getmetatable},\n  {\"loadfile\", luaB_loadfile},\n  {\"load\", luaB_load},\n  {\"loadstring\", luaB_loadstring},\n  {\"next\", luaB_next},\n  {\"pcall\", luaB_pcall},\n  {\"print\", luaB_print},\n  {\"rawequal\", luaB_rawequal},\n  {\"rawget\", luaB_rawget},\n  {\"rawset\", luaB_rawset},\n  {\"select\", luaB_select},\n  {\"setfenv\", luaB_setfenv},\n  {\"setmetatable\", luaB_setmetatable},\n  {\"tonumber\", luaB_tonumber},\n  {\"tostring\", luaB_tostring},\n  {\"type\", luaB_type},\n  {\"unpack\", luaB_unpack},\n  {\"xpcall\", luaB_xpcall},\n  {NULL, NULL}\n};\n\n\n/*\n** {======================================================\n** Coroutine library\n** =======================================================\n*/\n\n#define CO_RUN\t0\t/* running */\n#define CO_SUS\t1\t/* suspended */\n#define CO_NOR\t2\t/* 'normal' (it resumed another coroutine) */\n#define CO_DEAD\t3\n\nstatic const char *const statnames[] =\n    {\"running\", \"suspended\", \"normal\", \"dead\"};\n\nstatic int costatus (lua_State *L, lua_State *co) {\n  if (L == co) return CO_RUN;\n  switch (lua_status(co)) {\n    case LUA_YIELD:\n      return CO_SUS;\n    case 0: {\n      lua_Debug ar;\n      if (lua_getstack(co, 0, &ar) > 0)  /* does it have frames? */\n        return CO_NOR;  /* it is running */\n      else if (lua_gettop(co) == 0)\n          return CO_DEAD;\n      else\n        return CO_SUS;  /* initial state */\n    }\n    default:  /* some error occured */\n      return CO_DEAD;\n  }\n}\n\n\nstatic int luaB_costatus (lua_State *L) {\n  lua_State *co = lua_tothread(L, 1);\n  luaL_argcheck(L, co, 1, \"coroutine expected\");\n  lua_pushstring(L, statnames[costatus(L, co)]);\n  return 1;\n}\n\n\nstatic int auxresume (lua_State *L, lua_State *co, int narg) {\n  int status = costatus(L, co);\n  if (!lua_checkstack(co, narg))\n    luaL_error(L, \"too many arguments to resume\");\n  if (status != CO_SUS) {\n    lua_pushfstring(L, \"cannot resume %s coroutine\", statnames[status]);\n    return -1;  /* error flag */\n  }\n  lua_xmove(L, co, narg);\n  lua_setlevel(L, co);\n  status = lua_resume(co, narg);\n  if (status == 0 || status == LUA_YIELD) {\n    int nres = lua_gettop(co);\n    if (!lua_checkstack(L, nres + 1))\n      luaL_error(L, \"too many results to resume\");\n    lua_xmove(co, L, nres);  /* move yielded values */\n    return nres;\n  }\n  else {\n    lua_xmove(co, L, 1);  /* move error message */\n    return -1;  /* error flag */\n  }\n}\n\n\nstatic int luaB_coresume (lua_State *L) {\n  lua_State *co = lua_tothread(L, 1);\n  int r;\n  luaL_argcheck(L, co, 1, \"coroutine expected\");\n  r = auxresume(L, co, lua_gettop(L) - 1);\n  if (r < 0) {\n    lua_pushboolean(L, 0);\n    lua_insert(L, -2);\n    return 2;  /* return false + error message */\n  }\n  else {\n    lua_pushboolean(L, 1);\n    lua_insert(L, -(r + 1));\n    return r + 1;  /* return true + `resume' returns */\n  }\n}\n\n\nstatic int luaB_auxwrap (lua_State *L) {\n  lua_State *co = lua_tothread(L, lua_upvalueindex(1));\n  int r = auxresume(L, co, lua_gettop(L));\n  if (r < 0) {\n    if (lua_isstring(L, -1)) {  /* error object is a string? */\n      luaL_where(L, 1);  /* add extra info */\n      lua_insert(L, -2);\n      lua_concat(L, 2);\n    }\n    lua_error(L);  /* propagate error */\n  }\n  return r;\n}\n\n\nstatic int luaB_cocreate (lua_State *L) {\n  lua_State *NL = lua_newthread(L);\n  luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,\n    \"Lua function expected\");\n  lua_pushvalue(L, 1);  /* move function to top */\n  lua_xmove(L, NL, 1);  /* move function from L to NL */\n  return 1;\n}\n\n\nstatic int luaB_cowrap (lua_State *L) {\n  luaB_cocreate(L);\n  lua_pushcclosure(L, luaB_auxwrap, 1);\n  return 1;\n}\n\n\nstatic int luaB_yield (lua_State *L) {\n  return lua_yield(L, lua_gettop(L));\n}\n\n\nstatic int luaB_corunning (lua_State *L) {\n  if (lua_pushthread(L))\n    lua_pushnil(L);  /* main thread is not a coroutine */\n  return 1;\n}\n\n\nstatic const luaL_Reg co_funcs[] = {\n  {\"create\", luaB_cocreate},\n  {\"resume\", luaB_coresume},\n  {\"running\", luaB_corunning},\n  {\"status\", luaB_costatus},\n  {\"wrap\", luaB_cowrap},\n  {\"yield\", luaB_yield},\n  {NULL, NULL}\n};\n\n/* }====================================================== */\n\n\nstatic void auxopen (lua_State *L, const char *name,\n                     lua_CFunction f, lua_CFunction u) {\n  lua_pushcfunction(L, u);\n  lua_pushcclosure(L, f, 1);\n  lua_setfield(L, -2, name);\n}\n\n\nstatic void base_open (lua_State *L) {\n  /* set global _G */\n  lua_pushvalue(L, LUA_GLOBALSINDEX);\n  lua_setglobal(L, \"_G\");\n  /* open lib into global table */\n  luaL_register(L, \"_G\", base_funcs);\n  lua_pushliteral(L, LUA_VERSION);\n  lua_setglobal(L, \"_VERSION\");  /* set global _VERSION */\n  /* `ipairs' and `pairs' need auxiliary functions as upvalues */\n  auxopen(L, \"ipairs\", luaB_ipairs, ipairsaux);\n  auxopen(L, \"pairs\", luaB_pairs, luaB_next);\n  /* `newproxy' needs a weaktable as upvalue */\n  lua_createtable(L, 0, 1);  /* new table `w' */\n  lua_pushvalue(L, -1);  /* `w' will be its own metatable */\n  lua_setmetatable(L, -2);\n  lua_pushliteral(L, \"kv\");\n  lua_setfield(L, -2, \"__mode\");  /* metatable(w).__mode = \"kv\" */\n  lua_pushcclosure(L, luaB_newproxy, 1);\n  lua_setglobal(L, \"newproxy\");  /* set global `newproxy' */\n}\n\n\nLUALIB_API int luaopen_base (lua_State *L) {\n  base_open(L);\n  luaL_register(L, LUA_COLIBNAME, co_funcs);\n  return 2;\n}\n\n"
  },
  {
    "path": "deps/lua/src/lcode.c",
    "content": "/*\n** $Id: lcode.c,v 2.25.1.5 2011/01/31 14:53:16 roberto Exp $\n** Code generator for Lua\n** See Copyright Notice in lua.h\n*/\n\n\n#include <stdlib.h>\n\n#define lcode_c\n#define LUA_CORE\n\n#include \"lua.h\"\n\n#include \"lcode.h\"\n#include \"ldebug.h\"\n#include \"ldo.h\"\n#include \"lgc.h\"\n#include \"llex.h\"\n#include \"lmem.h\"\n#include \"lobject.h\"\n#include \"lopcodes.h\"\n#include \"lparser.h\"\n#include \"ltable.h\"\n\n\n#define hasjumps(e)\t((e)->t != (e)->f)\n\n\nstatic int isnumeral(expdesc *e) {\n  return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP);\n}\n\n\nvoid luaK_nil (FuncState *fs, int from, int n) {\n  Instruction *previous;\n  if (fs->pc > fs->lasttarget) {  /* no jumps to current position? */\n    if (fs->pc == 0) {  /* function start? */\n      if (from >= fs->nactvar)\n        return;  /* positions are already clean */\n    }\n    else {\n      previous = &fs->f->code[fs->pc-1];\n      if (GET_OPCODE(*previous) == OP_LOADNIL) {\n        int pfrom = GETARG_A(*previous);\n        int pto = GETARG_B(*previous);\n        if (pfrom <= from && from <= pto+1) {  /* can connect both? */\n          if (from+n-1 > pto)\n            SETARG_B(*previous, from+n-1);\n          return;\n        }\n      }\n    }\n  }\n  luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0);  /* else no optimization */\n}\n\n\nint luaK_jump (FuncState *fs) {\n  int jpc = fs->jpc;  /* save list of jumps to here */\n  int j;\n  fs->jpc = NO_JUMP;\n  j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP);\n  luaK_concat(fs, &j, jpc);  /* keep them on hold */\n  return j;\n}\n\n\nvoid luaK_ret (FuncState *fs, int first, int nret) {\n  luaK_codeABC(fs, OP_RETURN, first, nret+1, 0);\n}\n\n\nstatic int condjump (FuncState *fs, OpCode op, int A, int B, int C) {\n  luaK_codeABC(fs, op, A, B, C);\n  return luaK_jump(fs);\n}\n\n\nstatic void fixjump (FuncState *fs, int pc, int dest) {\n  Instruction *jmp = &fs->f->code[pc];\n  int offset = dest-(pc+1);\n  lua_assert(dest != NO_JUMP);\n  if (abs(offset) > MAXARG_sBx)\n    luaX_syntaxerror(fs->ls, \"control structure too long\");\n  SETARG_sBx(*jmp, offset);\n}\n\n\n/*\n** returns current `pc' and marks it as a jump target (to avoid wrong\n** optimizations with consecutive instructions not in the same basic block).\n*/\nint luaK_getlabel (FuncState *fs) {\n  fs->lasttarget = fs->pc;\n  return fs->pc;\n}\n\n\nstatic int getjump (FuncState *fs, int pc) {\n  int offset = GETARG_sBx(fs->f->code[pc]);\n  if (offset == NO_JUMP)  /* point to itself represents end of list */\n    return NO_JUMP;  /* end of list */\n  else\n    return (pc+1)+offset;  /* turn offset into absolute position */\n}\n\n\nstatic Instruction *getjumpcontrol (FuncState *fs, int pc) {\n  Instruction *pi = &fs->f->code[pc];\n  if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1))))\n    return pi-1;\n  else\n    return pi;\n}\n\n\n/*\n** check whether list has any jump that do not produce a value\n** (or produce an inverted value)\n*/\nstatic int need_value (FuncState *fs, int list) {\n  for (; list != NO_JUMP; list = getjump(fs, list)) {\n    Instruction i = *getjumpcontrol(fs, list);\n    if (GET_OPCODE(i) != OP_TESTSET) return 1;\n  }\n  return 0;  /* not found */\n}\n\n\nstatic int patchtestreg (FuncState *fs, int node, int reg) {\n  Instruction *i = getjumpcontrol(fs, node);\n  if (GET_OPCODE(*i) != OP_TESTSET)\n    return 0;  /* cannot patch other instructions */\n  if (reg != NO_REG && reg != GETARG_B(*i))\n    SETARG_A(*i, reg);\n  else  /* no register to put value or register already has the value */\n    *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i));\n\n  return 1;\n}\n\n\nstatic void removevalues (FuncState *fs, int list) {\n  for (; list != NO_JUMP; list = getjump(fs, list))\n      patchtestreg(fs, list, NO_REG);\n}\n\n\nstatic void patchlistaux (FuncState *fs, int list, int vtarget, int reg,\n                          int dtarget) {\n  while (list != NO_JUMP) {\n    int next = getjump(fs, list);\n    if (patchtestreg(fs, list, reg))\n      fixjump(fs, list, vtarget);\n    else\n      fixjump(fs, list, dtarget);  /* jump to default target */\n    list = next;\n  }\n}\n\n\nstatic void dischargejpc (FuncState *fs) {\n  patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc);\n  fs->jpc = NO_JUMP;\n}\n\n\nvoid luaK_patchlist (FuncState *fs, int list, int target) {\n  if (target == fs->pc)\n    luaK_patchtohere(fs, list);\n  else {\n    lua_assert(target < fs->pc);\n    patchlistaux(fs, list, target, NO_REG, target);\n  }\n}\n\n\nvoid luaK_patchtohere (FuncState *fs, int list) {\n  luaK_getlabel(fs);\n  luaK_concat(fs, &fs->jpc, list);\n}\n\n\nvoid luaK_concat (FuncState *fs, int *l1, int l2) {\n  if (l2 == NO_JUMP) return;\n  else if (*l1 == NO_JUMP)\n    *l1 = l2;\n  else {\n    int list = *l1;\n    int next;\n    while ((next = getjump(fs, list)) != NO_JUMP)  /* find last element */\n      list = next;\n    fixjump(fs, list, l2);\n  }\n}\n\n\nvoid luaK_checkstack (FuncState *fs, int n) {\n  int newstack = fs->freereg + n;\n  if (newstack > fs->f->maxstacksize) {\n    if (newstack >= MAXSTACK)\n      luaX_syntaxerror(fs->ls, \"function or expression too complex\");\n    fs->f->maxstacksize = cast_byte(newstack);\n  }\n}\n\n\nvoid luaK_reserveregs (FuncState *fs, int n) {\n  luaK_checkstack(fs, n);\n  fs->freereg += n;\n}\n\n\nstatic void freereg (FuncState *fs, int reg) {\n  if (!ISK(reg) && reg >= fs->nactvar) {\n    fs->freereg--;\n    lua_assert(reg == fs->freereg);\n  }\n}\n\n\nstatic void freeexp (FuncState *fs, expdesc *e) {\n  if (e->k == VNONRELOC)\n    freereg(fs, e->u.s.info);\n}\n\n\nstatic int addk (FuncState *fs, TValue *k, TValue *v) {\n  lua_State *L = fs->L;\n  TValue *idx = luaH_set(L, fs->h, k);\n  Proto *f = fs->f;\n  int oldsize = f->sizek;\n  if (ttisnumber(idx)) {\n    lua_assert(luaO_rawequalObj(&fs->f->k[cast_int(nvalue(idx))], v));\n    return cast_int(nvalue(idx));\n  }\n  else {  /* constant not found; create a new entry */\n    setnvalue(idx, cast_num(fs->nk));\n    luaM_growvector(L, f->k, fs->nk, f->sizek, TValue,\n                    MAXARG_Bx, \"constant table overflow\");\n    while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);\n    setobj(L, &f->k[fs->nk], v);\n    luaC_barrier(L, f, v);\n    return fs->nk++;\n  }\n}\n\n\nint luaK_stringK (FuncState *fs, TString *s) {\n  TValue o;\n  setsvalue(fs->L, &o, s);\n  return addk(fs, &o, &o);\n}\n\n\nint luaK_numberK (FuncState *fs, lua_Number r) {\n  TValue o;\n  setnvalue(&o, r);\n  return addk(fs, &o, &o);\n}\n\n\nstatic int boolK (FuncState *fs, int b) {\n  TValue o;\n  setbvalue(&o, b);\n  return addk(fs, &o, &o);\n}\n\n\nstatic int nilK (FuncState *fs) {\n  TValue k, v;\n  setnilvalue(&v);\n  /* cannot use nil as key; instead use table itself to represent nil */\n  sethvalue(fs->L, &k, fs->h);\n  return addk(fs, &k, &v);\n}\n\n\nvoid luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {\n  if (e->k == VCALL) {  /* expression is an open function call? */\n    SETARG_C(getcode(fs, e), nresults+1);\n  }\n  else if (e->k == VVARARG) {\n    SETARG_B(getcode(fs, e), nresults+1);\n    SETARG_A(getcode(fs, e), fs->freereg);\n    luaK_reserveregs(fs, 1);\n  }\n}\n\n\nvoid luaK_setoneret (FuncState *fs, expdesc *e) {\n  if (e->k == VCALL) {  /* expression is an open function call? */\n    e->k = VNONRELOC;\n    e->u.s.info = GETARG_A(getcode(fs, e));\n  }\n  else if (e->k == VVARARG) {\n    SETARG_B(getcode(fs, e), 2);\n    e->k = VRELOCABLE;  /* can relocate its simple result */\n  }\n}\n\n\nvoid luaK_dischargevars (FuncState *fs, expdesc *e) {\n  switch (e->k) {\n    case VLOCAL: {\n      e->k = VNONRELOC;\n      break;\n    }\n    case VUPVAL: {\n      e->u.s.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.s.info, 0);\n      e->k = VRELOCABLE;\n      break;\n    }\n    case VGLOBAL: {\n      e->u.s.info = luaK_codeABx(fs, OP_GETGLOBAL, 0, e->u.s.info);\n      e->k = VRELOCABLE;\n      break;\n    }\n    case VINDEXED: {\n      freereg(fs, e->u.s.aux);\n      freereg(fs, e->u.s.info);\n      e->u.s.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.s.info, e->u.s.aux);\n      e->k = VRELOCABLE;\n      break;\n    }\n    case VVARARG:\n    case VCALL: {\n      luaK_setoneret(fs, e);\n      break;\n    }\n    default: break;  /* there is one value available (somewhere) */\n  }\n}\n\n\nstatic int code_label (FuncState *fs, int A, int b, int jump) {\n  luaK_getlabel(fs);  /* those instructions may be jump targets */\n  return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);\n}\n\n\nstatic void discharge2reg (FuncState *fs, expdesc *e, int reg) {\n  luaK_dischargevars(fs, e);\n  switch (e->k) {\n    case VNIL: {\n      luaK_nil(fs, reg, 1);\n      break;\n    }\n    case VFALSE:  case VTRUE: {\n      luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0);\n      break;\n    }\n    case VK: {\n      luaK_codeABx(fs, OP_LOADK, reg, e->u.s.info);\n      break;\n    }\n    case VKNUM: {\n      luaK_codeABx(fs, OP_LOADK, reg, luaK_numberK(fs, e->u.nval));\n      break;\n    }\n    case VRELOCABLE: {\n      Instruction *pc = &getcode(fs, e);\n      SETARG_A(*pc, reg);\n      break;\n    }\n    case VNONRELOC: {\n      if (reg != e->u.s.info)\n        luaK_codeABC(fs, OP_MOVE, reg, e->u.s.info, 0);\n      break;\n    }\n    default: {\n      lua_assert(e->k == VVOID || e->k == VJMP);\n      return;  /* nothing to do... */\n    }\n  }\n  e->u.s.info = reg;\n  e->k = VNONRELOC;\n}\n\n\nstatic void discharge2anyreg (FuncState *fs, expdesc *e) {\n  if (e->k != VNONRELOC) {\n    luaK_reserveregs(fs, 1);\n    discharge2reg(fs, e, fs->freereg-1);\n  }\n}\n\n\nstatic void exp2reg (FuncState *fs, expdesc *e, int reg) {\n  discharge2reg(fs, e, reg);\n  if (e->k == VJMP)\n    luaK_concat(fs, &e->t, e->u.s.info);  /* put this jump in `t' list */\n  if (hasjumps(e)) {\n    int final;  /* position after whole expression */\n    int p_f = NO_JUMP;  /* position of an eventual LOAD false */\n    int p_t = NO_JUMP;  /* position of an eventual LOAD true */\n    if (need_value(fs, e->t) || need_value(fs, e->f)) {\n      int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs);\n      p_f = code_label(fs, reg, 0, 1);\n      p_t = code_label(fs, reg, 1, 0);\n      luaK_patchtohere(fs, fj);\n    }\n    final = luaK_getlabel(fs);\n    patchlistaux(fs, e->f, final, reg, p_f);\n    patchlistaux(fs, e->t, final, reg, p_t);\n  }\n  e->f = e->t = NO_JUMP;\n  e->u.s.info = reg;\n  e->k = VNONRELOC;\n}\n\n\nvoid luaK_exp2nextreg (FuncState *fs, expdesc *e) {\n  luaK_dischargevars(fs, e);\n  freeexp(fs, e);\n  luaK_reserveregs(fs, 1);\n  exp2reg(fs, e, fs->freereg - 1);\n}\n\n\nint luaK_exp2anyreg (FuncState *fs, expdesc *e) {\n  luaK_dischargevars(fs, e);\n  if (e->k == VNONRELOC) {\n    if (!hasjumps(e)) return e->u.s.info;  /* exp is already in a register */\n    if (e->u.s.info >= fs->nactvar) {  /* reg. is not a local? */\n      exp2reg(fs, e, e->u.s.info);  /* put value on it */\n      return e->u.s.info;\n    }\n  }\n  luaK_exp2nextreg(fs, e);  /* default */\n  return e->u.s.info;\n}\n\n\nvoid luaK_exp2val (FuncState *fs, expdesc *e) {\n  if (hasjumps(e))\n    luaK_exp2anyreg(fs, e);\n  else\n    luaK_dischargevars(fs, e);\n}\n\n\nint luaK_exp2RK (FuncState *fs, expdesc *e) {\n  luaK_exp2val(fs, e);\n  switch (e->k) {\n    case VKNUM:\n    case VTRUE:\n    case VFALSE:\n    case VNIL: {\n      if (fs->nk <= MAXINDEXRK) {  /* constant fit in RK operand? */\n        e->u.s.info = (e->k == VNIL)  ? nilK(fs) :\n                      (e->k == VKNUM) ? luaK_numberK(fs, e->u.nval) :\n                                        boolK(fs, (e->k == VTRUE));\n        e->k = VK;\n        return RKASK(e->u.s.info);\n      }\n      else break;\n    }\n    case VK: {\n      if (e->u.s.info <= MAXINDEXRK)  /* constant fit in argC? */\n        return RKASK(e->u.s.info);\n      else break;\n    }\n    default: break;\n  }\n  /* not a constant in the right range: put it in a register */\n  return luaK_exp2anyreg(fs, e);\n}\n\n\nvoid luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {\n  switch (var->k) {\n    case VLOCAL: {\n      freeexp(fs, ex);\n      exp2reg(fs, ex, var->u.s.info);\n      return;\n    }\n    case VUPVAL: {\n      int e = luaK_exp2anyreg(fs, ex);\n      luaK_codeABC(fs, OP_SETUPVAL, e, var->u.s.info, 0);\n      break;\n    }\n    case VGLOBAL: {\n      int e = luaK_exp2anyreg(fs, ex);\n      luaK_codeABx(fs, OP_SETGLOBAL, e, var->u.s.info);\n      break;\n    }\n    case VINDEXED: {\n      int e = luaK_exp2RK(fs, ex);\n      luaK_codeABC(fs, OP_SETTABLE, var->u.s.info, var->u.s.aux, e);\n      break;\n    }\n    default: {\n      lua_assert(0);  /* invalid var kind to store */\n      break;\n    }\n  }\n  freeexp(fs, ex);\n}\n\n\nvoid luaK_self (FuncState *fs, expdesc *e, expdesc *key) {\n  int func;\n  luaK_exp2anyreg(fs, e);\n  freeexp(fs, e);\n  func = fs->freereg;\n  luaK_reserveregs(fs, 2);\n  luaK_codeABC(fs, OP_SELF, func, e->u.s.info, luaK_exp2RK(fs, key));\n  freeexp(fs, key);\n  e->u.s.info = func;\n  e->k = VNONRELOC;\n}\n\n\nstatic void invertjump (FuncState *fs, expdesc *e) {\n  Instruction *pc = getjumpcontrol(fs, e->u.s.info);\n  lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET &&\n                                           GET_OPCODE(*pc) != OP_TEST);\n  SETARG_A(*pc, !(GETARG_A(*pc)));\n}\n\n\nstatic int jumponcond (FuncState *fs, expdesc *e, int cond) {\n  if (e->k == VRELOCABLE) {\n    Instruction ie = getcode(fs, e);\n    if (GET_OPCODE(ie) == OP_NOT) {\n      fs->pc--;  /* remove previous OP_NOT */\n      return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond);\n    }\n    /* else go through */\n  }\n  discharge2anyreg(fs, e);\n  freeexp(fs, e);\n  return condjump(fs, OP_TESTSET, NO_REG, e->u.s.info, cond);\n}\n\n\nvoid luaK_goiftrue (FuncState *fs, expdesc *e) {\n  int pc;  /* pc of last jump */\n  luaK_dischargevars(fs, e);\n  switch (e->k) {\n    case VK: case VKNUM: case VTRUE: {\n      pc = NO_JUMP;  /* always true; do nothing */\n      break;\n    }\n    case VJMP: {\n      invertjump(fs, e);\n      pc = e->u.s.info;\n      break;\n    }\n    default: {\n      pc = jumponcond(fs, e, 0);\n      break;\n    }\n  }\n  luaK_concat(fs, &e->f, pc);  /* insert last jump in `f' list */\n  luaK_patchtohere(fs, e->t);\n  e->t = NO_JUMP;\n}\n\n\nstatic void luaK_goiffalse (FuncState *fs, expdesc *e) {\n  int pc;  /* pc of last jump */\n  luaK_dischargevars(fs, e);\n  switch (e->k) {\n    case VNIL: case VFALSE: {\n      pc = NO_JUMP;  /* always false; do nothing */\n      break;\n    }\n    case VJMP: {\n      pc = e->u.s.info;\n      break;\n    }\n    default: {\n      pc = jumponcond(fs, e, 1);\n      break;\n    }\n  }\n  luaK_concat(fs, &e->t, pc);  /* insert last jump in `t' list */\n  luaK_patchtohere(fs, e->f);\n  e->f = NO_JUMP;\n}\n\n\nstatic void codenot (FuncState *fs, expdesc *e) {\n  luaK_dischargevars(fs, e);\n  switch (e->k) {\n    case VNIL: case VFALSE: {\n      e->k = VTRUE;\n      break;\n    }\n    case VK: case VKNUM: case VTRUE: {\n      e->k = VFALSE;\n      break;\n    }\n    case VJMP: {\n      invertjump(fs, e);\n      break;\n    }\n    case VRELOCABLE:\n    case VNONRELOC: {\n      discharge2anyreg(fs, e);\n      freeexp(fs, e);\n      e->u.s.info = luaK_codeABC(fs, OP_NOT, 0, e->u.s.info, 0);\n      e->k = VRELOCABLE;\n      break;\n    }\n    default: {\n      lua_assert(0);  /* cannot happen */\n      break;\n    }\n  }\n  /* interchange true and false lists */\n  { int temp = e->f; e->f = e->t; e->t = temp; }\n  removevalues(fs, e->f);\n  removevalues(fs, e->t);\n}\n\n\nvoid luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {\n  t->u.s.aux = luaK_exp2RK(fs, k);\n  t->k = VINDEXED;\n}\n\n\nstatic int constfolding (OpCode op, expdesc *e1, expdesc *e2) {\n  lua_Number v1, v2, r;\n  if (!isnumeral(e1) || !isnumeral(e2)) return 0;\n  v1 = e1->u.nval;\n  v2 = e2->u.nval;\n  switch (op) {\n    case OP_ADD: r = luai_numadd(v1, v2); break;\n    case OP_SUB: r = luai_numsub(v1, v2); break;\n    case OP_MUL: r = luai_nummul(v1, v2); break;\n    case OP_DIV:\n      if (v2 == 0) return 0;  /* do not attempt to divide by 0 */\n      r = luai_numdiv(v1, v2); break;\n    case OP_MOD:\n      if (v2 == 0) return 0;  /* do not attempt to divide by 0 */\n      r = luai_nummod(v1, v2); break;\n    case OP_POW: r = luai_numpow(v1, v2); break;\n    case OP_UNM: r = luai_numunm(v1); break;\n    case OP_LEN: return 0;  /* no constant folding for 'len' */\n    default: lua_assert(0); r = 0; break;\n  }\n  if (luai_numisnan(r)) return 0;  /* do not attempt to produce NaN */\n  e1->u.nval = r;\n  return 1;\n}\n\n\nstatic void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) {\n  if (constfolding(op, e1, e2))\n    return;\n  else {\n    int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0;\n    int o1 = luaK_exp2RK(fs, e1);\n    if (o1 > o2) {\n      freeexp(fs, e1);\n      freeexp(fs, e2);\n    }\n    else {\n      freeexp(fs, e2);\n      freeexp(fs, e1);\n    }\n    e1->u.s.info = luaK_codeABC(fs, op, 0, o1, o2);\n    e1->k = VRELOCABLE;\n  }\n}\n\n\nstatic void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1,\n                                                          expdesc *e2) {\n  int o1 = luaK_exp2RK(fs, e1);\n  int o2 = luaK_exp2RK(fs, e2);\n  freeexp(fs, e2);\n  freeexp(fs, e1);\n  if (cond == 0 && op != OP_EQ) {\n    int temp;  /* exchange args to replace by `<' or `<=' */\n    temp = o1; o1 = o2; o2 = temp;  /* o1 <==> o2 */\n    cond = 1;\n  }\n  e1->u.s.info = condjump(fs, op, cond, o1, o2);\n  e1->k = VJMP;\n}\n\n\nvoid luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {\n  expdesc e2;\n  e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0;\n  switch (op) {\n    case OPR_MINUS: {\n      if (!isnumeral(e))\n        luaK_exp2anyreg(fs, e);  /* cannot operate on non-numeric constants */\n      codearith(fs, OP_UNM, e, &e2);\n      break;\n    }\n    case OPR_NOT: codenot(fs, e); break;\n    case OPR_LEN: {\n      luaK_exp2anyreg(fs, e);  /* cannot operate on constants */\n      codearith(fs, OP_LEN, e, &e2);\n      break;\n    }\n    default: lua_assert(0);\n  }\n}\n\n\nvoid luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {\n  switch (op) {\n    case OPR_AND: {\n      luaK_goiftrue(fs, v);\n      break;\n    }\n    case OPR_OR: {\n      luaK_goiffalse(fs, v);\n      break;\n    }\n    case OPR_CONCAT: {\n      luaK_exp2nextreg(fs, v);  /* operand must be on the `stack' */\n      break;\n    }\n    case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:\n    case OPR_MOD: case OPR_POW: {\n      if (!isnumeral(v)) luaK_exp2RK(fs, v);\n      break;\n    }\n    default: {\n      luaK_exp2RK(fs, v);\n      break;\n    }\n  }\n}\n\n\nvoid luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {\n  switch (op) {\n    case OPR_AND: {\n      lua_assert(e1->t == NO_JUMP);  /* list must be closed */\n      luaK_dischargevars(fs, e2);\n      luaK_concat(fs, &e2->f, e1->f);\n      *e1 = *e2;\n      break;\n    }\n    case OPR_OR: {\n      lua_assert(e1->f == NO_JUMP);  /* list must be closed */\n      luaK_dischargevars(fs, e2);\n      luaK_concat(fs, &e2->t, e1->t);\n      *e1 = *e2;\n      break;\n    }\n    case OPR_CONCAT: {\n      luaK_exp2val(fs, e2);\n      if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) {\n        lua_assert(e1->u.s.info == GETARG_B(getcode(fs, e2))-1);\n        freeexp(fs, e1);\n        SETARG_B(getcode(fs, e2), e1->u.s.info);\n        e1->k = VRELOCABLE; e1->u.s.info = e2->u.s.info;\n      }\n      else {\n        luaK_exp2nextreg(fs, e2);  /* operand must be on the 'stack' */\n        codearith(fs, OP_CONCAT, e1, e2);\n      }\n      break;\n    }\n    case OPR_ADD: codearith(fs, OP_ADD, e1, e2); break;\n    case OPR_SUB: codearith(fs, OP_SUB, e1, e2); break;\n    case OPR_MUL: codearith(fs, OP_MUL, e1, e2); break;\n    case OPR_DIV: codearith(fs, OP_DIV, e1, e2); break;\n    case OPR_MOD: codearith(fs, OP_MOD, e1, e2); break;\n    case OPR_POW: codearith(fs, OP_POW, e1, e2); break;\n    case OPR_EQ: codecomp(fs, OP_EQ, 1, e1, e2); break;\n    case OPR_NE: codecomp(fs, OP_EQ, 0, e1, e2); break;\n    case OPR_LT: codecomp(fs, OP_LT, 1, e1, e2); break;\n    case OPR_LE: codecomp(fs, OP_LE, 1, e1, e2); break;\n    case OPR_GT: codecomp(fs, OP_LT, 0, e1, e2); break;\n    case OPR_GE: codecomp(fs, OP_LE, 0, e1, e2); break;\n    default: lua_assert(0);\n  }\n}\n\n\nvoid luaK_fixline (FuncState *fs, int line) {\n  fs->f->lineinfo[fs->pc - 1] = line;\n}\n\n\nstatic int luaK_code (FuncState *fs, Instruction i, int line) {\n  Proto *f = fs->f;\n  dischargejpc(fs);  /* `pc' will change */\n  /* put new instruction in code array */\n  luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,\n                  MAX_INT, \"code size overflow\");\n  f->code[fs->pc] = i;\n  /* save corresponding line information */\n  luaM_growvector(fs->L, f->lineinfo, fs->pc, f->sizelineinfo, int,\n                  MAX_INT, \"code size overflow\");\n  f->lineinfo[fs->pc] = line;\n  return fs->pc++;\n}\n\n\nint luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {\n  lua_assert(getOpMode(o) == iABC);\n  lua_assert(getBMode(o) != OpArgN || b == 0);\n  lua_assert(getCMode(o) != OpArgN || c == 0);\n  return luaK_code(fs, CREATE_ABC(o, a, b, c), fs->ls->lastline);\n}\n\n\nint luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {\n  lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx);\n  lua_assert(getCMode(o) == OpArgN);\n  return luaK_code(fs, CREATE_ABx(o, a, bc), fs->ls->lastline);\n}\n\n\nvoid luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {\n  int c =  (nelems - 1)/LFIELDS_PER_FLUSH + 1;\n  int b = (tostore == LUA_MULTRET) ? 0 : tostore;\n  lua_assert(tostore != 0);\n  if (c <= MAXARG_C)\n    luaK_codeABC(fs, OP_SETLIST, base, b, c);\n  else {\n    luaK_codeABC(fs, OP_SETLIST, base, b, 0);\n    luaK_code(fs, cast(Instruction, c), fs->ls->lastline);\n  }\n  fs->freereg = base + 1;  /* free registers with list values */\n}\n\n"
  },
  {
    "path": "deps/lua/src/lcode.h",
    "content": "/*\n** $Id: lcode.h,v 1.48.1.1 2007/12/27 13:02:25 roberto Exp $\n** Code generator for Lua\n** See Copyright Notice in lua.h\n*/\n\n#ifndef lcode_h\n#define lcode_h\n\n#include \"llex.h\"\n#include \"lobject.h\"\n#include \"lopcodes.h\"\n#include \"lparser.h\"\n\n\n/*\n** Marks the end of a patch list. It is an invalid value both as an absolute\n** address, and as a list link (would link an element to itself).\n*/\n#define NO_JUMP (-1)\n\n\n/*\n** grep \"ORDER OPR\" if you change these enums\n*/\ntypedef enum BinOpr {\n  OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW,\n  OPR_CONCAT,\n  OPR_NE, OPR_EQ,\n  OPR_LT, OPR_LE, OPR_GT, OPR_GE,\n  OPR_AND, OPR_OR,\n  OPR_NOBINOPR\n} BinOpr;\n\n\ntypedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;\n\n\n#define getcode(fs,e)\t((fs)->f->code[(e)->u.s.info])\n\n#define luaK_codeAsBx(fs,o,A,sBx)\tluaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx)\n\n#define luaK_setmultret(fs,e)\tluaK_setreturns(fs, e, LUA_MULTRET)\n\nLUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx);\nLUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C);\nLUAI_FUNC void luaK_fixline (FuncState *fs, int line);\nLUAI_FUNC void luaK_nil (FuncState *fs, int from, int n);\nLUAI_FUNC void luaK_reserveregs (FuncState *fs, int n);\nLUAI_FUNC void luaK_checkstack (FuncState *fs, int n);\nLUAI_FUNC int luaK_stringK (FuncState *fs, TString *s);\nLUAI_FUNC int luaK_numberK (FuncState *fs, lua_Number r);\nLUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e);\nLUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e);\nLUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e);\nLUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e);\nLUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e);\nLUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key);\nLUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k);\nLUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e);\nLUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e);\nLUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults);\nLUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e);\nLUAI_FUNC int luaK_jump (FuncState *fs);\nLUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret);\nLUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target);\nLUAI_FUNC void luaK_patchtohere (FuncState *fs, int list);\nLUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2);\nLUAI_FUNC int luaK_getlabel (FuncState *fs);\nLUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v);\nLUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v);\nLUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2);\nLUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore);\n\n\n#endif\n"
  },
  {
    "path": "deps/lua/src/ldblib.c",
    "content": "/*\n** $Id: ldblib.c,v 1.104.1.4 2009/08/04 18:50:18 roberto Exp $\n** Interface from Lua to its debug API\n** See Copyright Notice in lua.h\n*/\n\n\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#define ldblib_c\n#define LUA_LIB\n\n#include \"lua.h\"\n\n#include \"lauxlib.h\"\n#include \"lualib.h\"\n\n\n\nstatic int db_getregistry (lua_State *L) {\n  lua_pushvalue(L, LUA_REGISTRYINDEX);\n  return 1;\n}\n\n\nstatic int db_getmetatable (lua_State *L) {\n  luaL_checkany(L, 1);\n  if (!lua_getmetatable(L, 1)) {\n    lua_pushnil(L);  /* no metatable */\n  }\n  return 1;\n}\n\n\nstatic int db_setmetatable (lua_State *L) {\n  int t = lua_type(L, 2);\n  luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,\n                    \"nil or table expected\");\n  lua_settop(L, 2);\n  lua_pushboolean(L, lua_setmetatable(L, 1));\n  return 1;\n}\n\n\nstatic int db_getfenv (lua_State *L) {\n  luaL_checkany(L, 1);\n  lua_getfenv(L, 1);\n  return 1;\n}\n\n\nstatic int db_setfenv (lua_State *L) {\n  luaL_checktype(L, 2, LUA_TTABLE);\n  lua_settop(L, 2);\n  if (lua_setfenv(L, 1) == 0)\n    luaL_error(L, LUA_QL(\"setfenv\")\n                  \" cannot change environment of given object\");\n  return 1;\n}\n\n\nstatic void settabss (lua_State *L, const char *i, const char *v) {\n  lua_pushstring(L, v);\n  lua_setfield(L, -2, i);\n}\n\n\nstatic void settabsi (lua_State *L, const char *i, int v) {\n  lua_pushinteger(L, v);\n  lua_setfield(L, -2, i);\n}\n\n\nstatic lua_State *getthread (lua_State *L, int *arg) {\n  if (lua_isthread(L, 1)) {\n    *arg = 1;\n    return lua_tothread(L, 1);\n  }\n  else {\n    *arg = 0;\n    return L;\n  }\n}\n\n\nstatic void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {\n  if (L == L1) {\n    lua_pushvalue(L, -2);\n    lua_remove(L, -3);\n  }\n  else\n    lua_xmove(L1, L, 1);\n  lua_setfield(L, -2, fname);\n}\n\n\nstatic int db_getinfo (lua_State *L) {\n  lua_Debug ar;\n  int arg;\n  lua_State *L1 = getthread(L, &arg);\n  const char *options = luaL_optstring(L, arg+2, \"flnSu\");\n  if (lua_isnumber(L, arg+1)) {\n    if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) {\n      lua_pushnil(L);  /* level out of range */\n      return 1;\n    }\n  }\n  else if (lua_isfunction(L, arg+1)) {\n    lua_pushfstring(L, \">%s\", options);\n    options = lua_tostring(L, -1);\n    lua_pushvalue(L, arg+1);\n    lua_xmove(L, L1, 1);\n  }\n  else\n    return luaL_argerror(L, arg+1, \"function or level expected\");\n  if (!lua_getinfo(L1, options, &ar))\n    return luaL_argerror(L, arg+2, \"invalid option\");\n  lua_createtable(L, 0, 2);\n  if (strchr(options, 'S')) {\n    settabss(L, \"source\", ar.source);\n    settabss(L, \"short_src\", ar.short_src);\n    settabsi(L, \"linedefined\", ar.linedefined);\n    settabsi(L, \"lastlinedefined\", ar.lastlinedefined);\n    settabss(L, \"what\", ar.what);\n  }\n  if (strchr(options, 'l'))\n    settabsi(L, \"currentline\", ar.currentline);\n  if (strchr(options, 'u'))\n    settabsi(L, \"nups\", ar.nups);\n  if (strchr(options, 'n')) {\n    settabss(L, \"name\", ar.name);\n    settabss(L, \"namewhat\", ar.namewhat);\n  }\n  if (strchr(options, 'L'))\n    treatstackoption(L, L1, \"activelines\");\n  if (strchr(options, 'f'))\n    treatstackoption(L, L1, \"func\");\n  return 1;  /* return table */\n}\n    \n\nstatic int db_getlocal (lua_State *L) {\n  int arg;\n  lua_State *L1 = getthread(L, &arg);\n  lua_Debug ar;\n  const char *name;\n  if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar))  /* out of range? */\n    return luaL_argerror(L, arg+1, \"level out of range\");\n  name = lua_getlocal(L1, &ar, luaL_checkint(L, arg+2));\n  if (name) {\n    lua_xmove(L1, L, 1);\n    lua_pushstring(L, name);\n    lua_pushvalue(L, -2);\n    return 2;\n  }\n  else {\n    lua_pushnil(L);\n    return 1;\n  }\n}\n\n\nstatic int db_setlocal (lua_State *L) {\n  int arg;\n  lua_State *L1 = getthread(L, &arg);\n  lua_Debug ar;\n  if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar))  /* out of range? */\n    return luaL_argerror(L, arg+1, \"level out of range\");\n  luaL_checkany(L, arg+3);\n  lua_settop(L, arg+3);\n  lua_xmove(L, L1, 1);\n  lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+2)));\n  return 1;\n}\n\n\nstatic int auxupvalue (lua_State *L, int get) {\n  const char *name;\n  int n = luaL_checkint(L, 2);\n  luaL_checktype(L, 1, LUA_TFUNCTION);\n  if (lua_iscfunction(L, 1)) return 0;  /* cannot touch C upvalues from Lua */\n  name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);\n  if (name == NULL) return 0;\n  lua_pushstring(L, name);\n  lua_insert(L, -(get+1));\n  return get + 1;\n}\n\n\nstatic int db_getupvalue (lua_State *L) {\n  return auxupvalue(L, 1);\n}\n\n\nstatic int db_setupvalue (lua_State *L) {\n  luaL_checkany(L, 3);\n  return auxupvalue(L, 0);\n}\n\n\n\nstatic const char KEY_HOOK = 'h';\n\n\nstatic void hookf (lua_State *L, lua_Debug *ar) {\n  static const char *const hooknames[] =\n    {\"call\", \"return\", \"line\", \"count\", \"tail return\"};\n  lua_pushlightuserdata(L, (void *)&KEY_HOOK);\n  lua_rawget(L, LUA_REGISTRYINDEX);\n  lua_pushlightuserdata(L, L);\n  lua_rawget(L, -2);\n  if (lua_isfunction(L, -1)) {\n    lua_pushstring(L, hooknames[(int)ar->event]);\n    if (ar->currentline >= 0)\n      lua_pushinteger(L, ar->currentline);\n    else lua_pushnil(L);\n    lua_assert(lua_getinfo(L, \"lS\", ar));\n    lua_call(L, 2, 0);\n  }\n}\n\n\nstatic int makemask (const char *smask, int count) {\n  int mask = 0;\n  if (strchr(smask, 'c')) mask |= LUA_MASKCALL;\n  if (strchr(smask, 'r')) mask |= LUA_MASKRET;\n  if (strchr(smask, 'l')) mask |= LUA_MASKLINE;\n  if (count > 0) mask |= LUA_MASKCOUNT;\n  return mask;\n}\n\n\nstatic char *unmakemask (int mask, char *smask) {\n  int i = 0;\n  if (mask & LUA_MASKCALL) smask[i++] = 'c';\n  if (mask & LUA_MASKRET) smask[i++] = 'r';\n  if (mask & LUA_MASKLINE) smask[i++] = 'l';\n  smask[i] = '\\0';\n  return smask;\n}\n\n\nstatic void gethooktable (lua_State *L) {\n  lua_pushlightuserdata(L, (void *)&KEY_HOOK);\n  lua_rawget(L, LUA_REGISTRYINDEX);\n  if (!lua_istable(L, -1)) {\n    lua_pop(L, 1);\n    lua_createtable(L, 0, 1);\n    lua_pushlightuserdata(L, (void *)&KEY_HOOK);\n    lua_pushvalue(L, -2);\n    lua_rawset(L, LUA_REGISTRYINDEX);\n  }\n}\n\n\nstatic int db_sethook (lua_State *L) {\n  int arg, mask, count;\n  lua_Hook func;\n  lua_State *L1 = getthread(L, &arg);\n  if (lua_isnoneornil(L, arg+1)) {\n    lua_settop(L, arg+1);\n    func = NULL; mask = 0; count = 0;  /* turn off hooks */\n  }\n  else {\n    const char *smask = luaL_checkstring(L, arg+2);\n    luaL_checktype(L, arg+1, LUA_TFUNCTION);\n    count = luaL_optint(L, arg+3, 0);\n    func = hookf; mask = makemask(smask, count);\n  }\n  gethooktable(L);\n  lua_pushlightuserdata(L, L1);\n  lua_pushvalue(L, arg+1);\n  lua_rawset(L, -3);  /* set new hook */\n  lua_pop(L, 1);  /* remove hook table */\n  lua_sethook(L1, func, mask, count);  /* set hooks */\n  return 0;\n}\n\n\nstatic int db_gethook (lua_State *L) {\n  int arg;\n  lua_State *L1 = getthread(L, &arg);\n  char buff[5];\n  int mask = lua_gethookmask(L1);\n  lua_Hook hook = lua_gethook(L1);\n  if (hook != NULL && hook != hookf)  /* external hook? */\n    lua_pushliteral(L, \"external hook\");\n  else {\n    gethooktable(L);\n    lua_pushlightuserdata(L, L1);\n    lua_rawget(L, -2);   /* get hook */\n    lua_remove(L, -2);  /* remove hook table */\n  }\n  lua_pushstring(L, unmakemask(mask, buff));\n  lua_pushinteger(L, lua_gethookcount(L1));\n  return 3;\n}\n\n\nstatic int db_debug (lua_State *L) {\n  for (;;) {\n    char buffer[250];\n    fputs(\"lua_debug> \", stderr);\n    if (fgets(buffer, sizeof(buffer), stdin) == 0 ||\n        strcmp(buffer, \"cont\\n\") == 0)\n      return 0;\n    if (luaL_loadbuffer(L, buffer, strlen(buffer), \"=(debug command)\") ||\n        lua_pcall(L, 0, 0, 0)) {\n      fputs(lua_tostring(L, -1), stderr);\n      fputs(\"\\n\", stderr);\n    }\n    lua_settop(L, 0);  /* remove eventual returns */\n  }\n}\n\n\n#define LEVELS1\t12\t/* size of the first part of the stack */\n#define LEVELS2\t10\t/* size of the second part of the stack */\n\nstatic int db_errorfb (lua_State *L) {\n  int level;\n  int firstpart = 1;  /* still before eventual `...' */\n  int arg;\n  lua_State *L1 = getthread(L, &arg);\n  lua_Debug ar;\n  if (lua_isnumber(L, arg+2)) {\n    level = (int)lua_tointeger(L, arg+2);\n    lua_pop(L, 1);\n  }\n  else\n    level = (L == L1) ? 1 : 0;  /* level 0 may be this own function */\n  if (lua_gettop(L) == arg)\n    lua_pushliteral(L, \"\");\n  else if (!lua_isstring(L, arg+1)) return 1;  /* message is not a string */\n  else lua_pushliteral(L, \"\\n\");\n  lua_pushliteral(L, \"stack traceback:\");\n  while (lua_getstack(L1, level++, &ar)) {\n    if (level > LEVELS1 && firstpart) {\n      /* no more than `LEVELS2' more levels? */\n      if (!lua_getstack(L1, level+LEVELS2, &ar))\n        level--;  /* keep going */\n      else {\n        lua_pushliteral(L, \"\\n\\t...\");  /* too many levels */\n        while (lua_getstack(L1, level+LEVELS2, &ar))  /* find last levels */\n          level++;\n      }\n      firstpart = 0;\n      continue;\n    }\n    lua_pushliteral(L, \"\\n\\t\");\n    lua_getinfo(L1, \"Snl\", &ar);\n    lua_pushfstring(L, \"%s:\", ar.short_src);\n    if (ar.currentline > 0)\n      lua_pushfstring(L, \"%d:\", ar.currentline);\n    if (*ar.namewhat != '\\0')  /* is there a name? */\n        lua_pushfstring(L, \" in function \" LUA_QS, ar.name);\n    else {\n      if (*ar.what == 'm')  /* main? */\n        lua_pushfstring(L, \" in main chunk\");\n      else if (*ar.what == 'C' || *ar.what == 't')\n        lua_pushliteral(L, \" ?\");  /* C function or tail call */\n      else\n        lua_pushfstring(L, \" in function <%s:%d>\",\n                           ar.short_src, ar.linedefined);\n    }\n    lua_concat(L, lua_gettop(L) - arg);\n  }\n  lua_concat(L, lua_gettop(L) - arg);\n  return 1;\n}\n\n\nstatic const luaL_Reg dblib[] = {\n  {\"debug\", db_debug},\n  {\"getfenv\", db_getfenv},\n  {\"gethook\", db_gethook},\n  {\"getinfo\", db_getinfo},\n  {\"getlocal\", db_getlocal},\n  {\"getregistry\", db_getregistry},\n  {\"getmetatable\", db_getmetatable},\n  {\"getupvalue\", db_getupvalue},\n  {\"setfenv\", db_setfenv},\n  {\"sethook\", db_sethook},\n  {\"setlocal\", db_setlocal},\n  {\"setmetatable\", db_setmetatable},\n  {\"setupvalue\", db_setupvalue},\n  {\"traceback\", db_errorfb},\n  {NULL, NULL}\n};\n\n\nLUALIB_API int luaopen_debug (lua_State *L) {\n  luaL_register(L, LUA_DBLIBNAME, dblib);\n  return 1;\n}\n\n"
  },
  {
    "path": "deps/lua/src/ldebug.c",
    "content": "/*\n** $Id: ldebug.c,v 2.29.1.6 2008/05/08 16:56:26 roberto Exp $\n** Debug Interface\n** See Copyright Notice in lua.h\n*/\n\n\n#include <stdarg.h>\n#include <stddef.h>\n#include <string.h>\n\n\n#define ldebug_c\n#define LUA_CORE\n\n#include \"lua.h\"\n\n#include \"lapi.h\"\n#include \"lcode.h\"\n#include \"ldebug.h\"\n#include \"ldo.h\"\n#include \"lfunc.h\"\n#include \"lobject.h\"\n#include \"lopcodes.h\"\n#include \"lstate.h\"\n#include \"lstring.h\"\n#include \"ltable.h\"\n#include \"ltm.h\"\n#include \"lvm.h\"\n\n\n\nstatic const char *getfuncname (lua_State *L, CallInfo *ci, const char **name);\n\n\nstatic int currentpc (lua_State *L, CallInfo *ci) {\n  if (!isLua(ci)) return -1;  /* function is not a Lua function? */\n  if (ci == L->ci)\n    ci->savedpc = L->savedpc;\n  return pcRel(ci->savedpc, ci_func(ci)->l.p);\n}\n\n\nstatic int currentline (lua_State *L, CallInfo *ci) {\n  int pc = currentpc(L, ci);\n  if (pc < 0)\n    return -1;  /* only active lua functions have current-line information */\n  else\n    return getline(ci_func(ci)->l.p, pc);\n}\n\n\n/*\n** this function can be called asynchronous (e.g. during a signal)\n*/\nLUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) {\n  if (func == NULL || mask == 0) {  /* turn off hooks? */\n    mask = 0;\n    func = NULL;\n  }\n  L->hook = func;\n  L->basehookcount = count;\n  resethookcount(L);\n  L->hookmask = cast_byte(mask);\n  return 1;\n}\n\n\nLUA_API lua_Hook lua_gethook (lua_State *L) {\n  return L->hook;\n}\n\n\nLUA_API int lua_gethookmask (lua_State *L) {\n  return L->hookmask;\n}\n\n\nLUA_API int lua_gethookcount (lua_State *L) {\n  return L->basehookcount;\n}\n\n\nLUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) {\n  int status;\n  CallInfo *ci;\n  lua_lock(L);\n  for (ci = L->ci; level > 0 && ci > L->base_ci; ci--) {\n    level--;\n    if (f_isLua(ci))  /* Lua function? */\n      level -= ci->tailcalls;  /* skip lost tail calls */\n  }\n  if (level == 0 && ci > L->base_ci) {  /* level found? */\n    status = 1;\n    ar->i_ci = cast_int(ci - L->base_ci);\n  }\n  else if (level < 0) {  /* level is of a lost tail call? */\n    status = 1;\n    ar->i_ci = 0;\n  }\n  else status = 0;  /* no such level */\n  lua_unlock(L);\n  return status;\n}\n\n\nstatic Proto *getluaproto (CallInfo *ci) {\n  return (isLua(ci) ? ci_func(ci)->l.p : NULL);\n}\n\n\nstatic const char *findlocal (lua_State *L, CallInfo *ci, int n) {\n  const char *name;\n  Proto *fp = getluaproto(ci);\n  if (fp && (name = luaF_getlocalname(fp, n, currentpc(L, ci))) != NULL)\n    return name;  /* is a local variable in a Lua function */\n  else {\n    StkId limit = (ci == L->ci) ? L->top : (ci+1)->func;\n    if (limit - ci->base >= n && n > 0)  /* is 'n' inside 'ci' stack? */\n      return \"(*temporary)\";\n    else\n      return NULL;\n  }\n}\n\n\nLUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) {\n  CallInfo *ci = L->base_ci + ar->i_ci;\n  const char *name = findlocal(L, ci, n);\n  lua_lock(L);\n  if (name)\n      luaA_pushobject(L, ci->base + (n - 1));\n  lua_unlock(L);\n  return name;\n}\n\n\nLUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {\n  CallInfo *ci = L->base_ci + ar->i_ci;\n  const char *name = findlocal(L, ci, n);\n  lua_lock(L);\n  if (name)\n      setobjs2s(L, ci->base + (n - 1), L->top - 1);\n  L->top--;  /* pop value */\n  lua_unlock(L);\n  return name;\n}\n\n\nstatic void funcinfo (lua_Debug *ar, Closure *cl) {\n  if (cl->c.isC) {\n    ar->source = \"=[C]\";\n    ar->linedefined = -1;\n    ar->lastlinedefined = -1;\n    ar->what = \"C\";\n  }\n  else {\n    ar->source = getstr(cl->l.p->source);\n    ar->linedefined = cl->l.p->linedefined;\n    ar->lastlinedefined = cl->l.p->lastlinedefined;\n    ar->what = (ar->linedefined == 0) ? \"main\" : \"Lua\";\n  }\n  luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);\n}\n\n\nstatic void info_tailcall (lua_Debug *ar) {\n  ar->name = ar->namewhat = \"\";\n  ar->what = \"tail\";\n  ar->lastlinedefined = ar->linedefined = ar->currentline = -1;\n  ar->source = \"=(tail call)\";\n  luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE);\n  ar->nups = 0;\n}\n\n\nstatic void collectvalidlines (lua_State *L, Closure *f) {\n  if (f == NULL || f->c.isC) {\n    setnilvalue(L->top);\n  }\n  else {\n    Table *t = luaH_new(L, 0, 0);\n    int *lineinfo = f->l.p->lineinfo;\n    int i;\n    for (i=0; i<f->l.p->sizelineinfo; i++)\n      setbvalue(luaH_setnum(L, t, lineinfo[i]), 1);\n    sethvalue(L, L->top, t); \n  }\n  incr_top(L);\n}\n\n\nstatic int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar,\n                    Closure *f, CallInfo *ci) {\n  int status = 1;\n  if (f == NULL) {\n    info_tailcall(ar);\n    return status;\n  }\n  for (; *what; what++) {\n    switch (*what) {\n      case 'S': {\n        funcinfo(ar, f);\n        break;\n      }\n      case 'l': {\n        ar->currentline = (ci) ? currentline(L, ci) : -1;\n        break;\n      }\n      case 'u': {\n        ar->nups = f->c.nupvalues;\n        break;\n      }\n      case 'n': {\n        ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL;\n        if (ar->namewhat == NULL) {\n          ar->namewhat = \"\";  /* not found */\n          ar->name = NULL;\n        }\n        break;\n      }\n      case 'L':\n      case 'f':  /* handled by lua_getinfo */\n        break;\n      default: status = 0;  /* invalid option */\n    }\n  }\n  return status;\n}\n\n\nLUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {\n  int status;\n  Closure *f = NULL;\n  CallInfo *ci = NULL;\n  lua_lock(L);\n  if (*what == '>') {\n    StkId func = L->top - 1;\n    luai_apicheck(L, ttisfunction(func));\n    what++;  /* skip the '>' */\n    f = clvalue(func);\n    L->top--;  /* pop function */\n  }\n  else if (ar->i_ci != 0) {  /* no tail call? */\n    ci = L->base_ci + ar->i_ci;\n    lua_assert(ttisfunction(ci->func));\n    f = clvalue(ci->func);\n  }\n  status = auxgetinfo(L, what, ar, f, ci);\n  if (strchr(what, 'f')) {\n    if (f == NULL) setnilvalue(L->top);\n    else setclvalue(L, L->top, f);\n    incr_top(L);\n  }\n  if (strchr(what, 'L'))\n    collectvalidlines(L, f);\n  lua_unlock(L);\n  return status;\n}\n\n\n/*\n** {======================================================\n** Symbolic Execution and code checker\n** =======================================================\n*/\n\n#define check(x)\t\tif (!(x)) return 0;\n\n#define checkjump(pt,pc)\tcheck(0 <= pc && pc < pt->sizecode)\n\n#define checkreg(pt,reg)\tcheck((reg) < (pt)->maxstacksize)\n\n\n\nstatic int precheck (const Proto *pt) {\n  check(pt->maxstacksize <= MAXSTACK);\n  check(pt->numparams+(pt->is_vararg & VARARG_HASARG) <= pt->maxstacksize);\n  check(!(pt->is_vararg & VARARG_NEEDSARG) ||\n              (pt->is_vararg & VARARG_HASARG));\n  check(pt->sizeupvalues <= pt->nups);\n  check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0);\n  check(pt->sizecode > 0 && GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN);\n  return 1;\n}\n\n\n#define checkopenop(pt,pc)\tluaG_checkopenop((pt)->code[(pc)+1])\n\nint luaG_checkopenop (Instruction i) {\n  switch (GET_OPCODE(i)) {\n    case OP_CALL:\n    case OP_TAILCALL:\n    case OP_RETURN:\n    case OP_SETLIST: {\n      check(GETARG_B(i) == 0);\n      return 1;\n    }\n    default: return 0;  /* invalid instruction after an open call */\n  }\n}\n\n\nstatic int checkArgMode (const Proto *pt, int r, enum OpArgMask mode) {\n  switch (mode) {\n    case OpArgN: check(r == 0); break;\n    case OpArgU: break;\n    case OpArgR: checkreg(pt, r); break;\n    case OpArgK:\n      check(ISK(r) ? INDEXK(r) < pt->sizek : r < pt->maxstacksize);\n      break;\n  }\n  return 1;\n}\n\n\nstatic Instruction symbexec (const Proto *pt, int lastpc, int reg) {\n  int pc;\n  int last;  /* stores position of last instruction that changed `reg' */\n  last = pt->sizecode-1;  /* points to final return (a `neutral' instruction) */\n  check(precheck(pt));\n  for (pc = 0; pc < lastpc; pc++) {\n    Instruction i = pt->code[pc];\n    OpCode op = GET_OPCODE(i);\n    int a = GETARG_A(i);\n    int b = 0;\n    int c = 0;\n    check(op < NUM_OPCODES);\n    checkreg(pt, a);\n    switch (getOpMode(op)) {\n      case iABC: {\n        b = GETARG_B(i);\n        c = GETARG_C(i);\n        check(checkArgMode(pt, b, getBMode(op)));\n        check(checkArgMode(pt, c, getCMode(op)));\n        break;\n      }\n      case iABx: {\n        b = GETARG_Bx(i);\n        if (getBMode(op) == OpArgK) check(b < pt->sizek);\n        break;\n      }\n      case iAsBx: {\n        b = GETARG_sBx(i);\n        if (getBMode(op) == OpArgR) {\n          int dest = pc+1+b;\n          check(0 <= dest && dest < pt->sizecode);\n          if (dest > 0) {\n            int j;\n            /* check that it does not jump to a setlist count; this\n               is tricky, because the count from a previous setlist may\n               have the same value of an invalid setlist; so, we must\n               go all the way back to the first of them (if any) */\n            for (j = 0; j < dest; j++) {\n              Instruction d = pt->code[dest-1-j];\n              if (!(GET_OPCODE(d) == OP_SETLIST && GETARG_C(d) == 0)) break;\n            }\n            /* if 'j' is even, previous value is not a setlist (even if\n               it looks like one) */\n            check((j&1) == 0);\n          }\n        }\n        break;\n      }\n    }\n    if (testAMode(op)) {\n      if (a == reg) last = pc;  /* change register `a' */\n    }\n    if (testTMode(op)) {\n      check(pc+2 < pt->sizecode);  /* check skip */\n      check(GET_OPCODE(pt->code[pc+1]) == OP_JMP);\n    }\n    switch (op) {\n      case OP_LOADBOOL: {\n        if (c == 1) {  /* does it jump? */\n          check(pc+2 < pt->sizecode);  /* check its jump */\n          check(GET_OPCODE(pt->code[pc+1]) != OP_SETLIST ||\n                GETARG_C(pt->code[pc+1]) != 0);\n        }\n        break;\n      }\n      case OP_LOADNIL: {\n        if (a <= reg && reg <= b)\n          last = pc;  /* set registers from `a' to `b' */\n        break;\n      }\n      case OP_GETUPVAL:\n      case OP_SETUPVAL: {\n        check(b < pt->nups);\n        break;\n      }\n      case OP_GETGLOBAL:\n      case OP_SETGLOBAL: {\n        check(ttisstring(&pt->k[b]));\n        break;\n      }\n      case OP_SELF: {\n        checkreg(pt, a+1);\n        if (reg == a+1) last = pc;\n        break;\n      }\n      case OP_CONCAT: {\n        check(b < c);  /* at least two operands */\n        break;\n      }\n      case OP_TFORLOOP: {\n        check(c >= 1);  /* at least one result (control variable) */\n        checkreg(pt, a+2+c);  /* space for results */\n        if (reg >= a+2) last = pc;  /* affect all regs above its base */\n        break;\n      }\n      case OP_FORLOOP:\n      case OP_FORPREP:\n        checkreg(pt, a+3);\n        /* go through */\n      case OP_JMP: {\n        int dest = pc+1+b;\n        /* not full check and jump is forward and do not skip `lastpc'? */\n        if (reg != NO_REG && pc < dest && dest <= lastpc)\n          pc += b;  /* do the jump */\n        break;\n      }\n      case OP_CALL:\n      case OP_TAILCALL: {\n        if (b != 0) {\n          checkreg(pt, a+b-1);\n        }\n        c--;  /* c = num. returns */\n        if (c == LUA_MULTRET) {\n          check(checkopenop(pt, pc));\n        }\n        else if (c != 0)\n          checkreg(pt, a+c-1);\n        if (reg >= a) last = pc;  /* affect all registers above base */\n        break;\n      }\n      case OP_RETURN: {\n        b--;  /* b = num. returns */\n        if (b > 0) checkreg(pt, a+b-1);\n        break;\n      }\n      case OP_SETLIST: {\n        if (b > 0) checkreg(pt, a + b);\n        if (c == 0) {\n          pc++;\n          check(pc < pt->sizecode - 1);\n        }\n        break;\n      }\n      case OP_CLOSURE: {\n        int nup, j;\n        check(b < pt->sizep);\n        nup = pt->p[b]->nups;\n        check(pc + nup < pt->sizecode);\n        for (j = 1; j <= nup; j++) {\n          OpCode op1 = GET_OPCODE(pt->code[pc + j]);\n          check(op1 == OP_GETUPVAL || op1 == OP_MOVE);\n        }\n        if (reg != NO_REG)  /* tracing? */\n          pc += nup;  /* do not 'execute' these pseudo-instructions */\n        break;\n      }\n      case OP_VARARG: {\n        check((pt->is_vararg & VARARG_ISVARARG) &&\n             !(pt->is_vararg & VARARG_NEEDSARG));\n        b--;\n        if (b == LUA_MULTRET) check(checkopenop(pt, pc));\n        checkreg(pt, a+b-1);\n        break;\n      }\n      default: break;\n    }\n  }\n  return pt->code[last];\n}\n\n#undef check\n#undef checkjump\n#undef checkreg\n\n/* }====================================================== */\n\n\nint luaG_checkcode (const Proto *pt) {\n  return (symbexec(pt, pt->sizecode, NO_REG) != 0);\n}\n\n\nstatic const char *kname (Proto *p, int c) {\n  if (ISK(c) && ttisstring(&p->k[INDEXK(c)]))\n    return svalue(&p->k[INDEXK(c)]);\n  else\n    return \"?\";\n}\n\n\nstatic const char *getobjname (lua_State *L, CallInfo *ci, int stackpos,\n                               const char **name) {\n  if (isLua(ci)) {  /* a Lua function? */\n    Proto *p = ci_func(ci)->l.p;\n    int pc = currentpc(L, ci);\n    Instruction i;\n    *name = luaF_getlocalname(p, stackpos+1, pc);\n    if (*name)  /* is a local? */\n      return \"local\";\n    i = symbexec(p, pc, stackpos);  /* try symbolic execution */\n    lua_assert(pc != -1);\n    switch (GET_OPCODE(i)) {\n      case OP_GETGLOBAL: {\n        int g = GETARG_Bx(i);  /* global index */\n        lua_assert(ttisstring(&p->k[g]));\n        *name = svalue(&p->k[g]);\n        return \"global\";\n      }\n      case OP_MOVE: {\n        int a = GETARG_A(i);\n        int b = GETARG_B(i);  /* move from `b' to `a' */\n        if (b < a)\n          return getobjname(L, ci, b, name);  /* get name for `b' */\n        break;\n      }\n      case OP_GETTABLE: {\n        int k = GETARG_C(i);  /* key index */\n        *name = kname(p, k);\n        return \"field\";\n      }\n      case OP_GETUPVAL: {\n        int u = GETARG_B(i);  /* upvalue index */\n        *name = p->upvalues ? getstr(p->upvalues[u]) : \"?\";\n        return \"upvalue\";\n      }\n      case OP_SELF: {\n        int k = GETARG_C(i);  /* key index */\n        *name = kname(p, k);\n        return \"method\";\n      }\n      default: break;\n    }\n  }\n  return NULL;  /* no useful name found */\n}\n\n\nstatic const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {\n  Instruction i;\n  if ((isLua(ci) && ci->tailcalls > 0) || !isLua(ci - 1))\n    return NULL;  /* calling function is not Lua (or is unknown) */\n  ci--;  /* calling function */\n  i = ci_func(ci)->l.p->code[currentpc(L, ci)];\n  if (GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL ||\n      GET_OPCODE(i) == OP_TFORLOOP)\n    return getobjname(L, ci, GETARG_A(i), name);\n  else\n    return NULL;  /* no useful name can be found */\n}\n\n\n/* only ANSI way to check whether a pointer points to an array */\nstatic int isinstack (CallInfo *ci, const TValue *o) {\n  StkId p;\n  for (p = ci->base; p < ci->top; p++)\n    if (o == p) return 1;\n  return 0;\n}\n\n\nvoid luaG_typeerror (lua_State *L, const TValue *o, const char *op) {\n  const char *name = NULL;\n  const char *t = luaT_typenames[ttype(o)];\n  const char *kind = (isinstack(L->ci, o)) ?\n                         getobjname(L, L->ci, cast_int(o - L->base), &name) :\n                         NULL;\n  if (kind)\n    luaG_runerror(L, \"attempt to %s %s \" LUA_QS \" (a %s value)\",\n                op, kind, name, t);\n  else\n    luaG_runerror(L, \"attempt to %s a %s value\", op, t);\n}\n\n\nvoid luaG_concaterror (lua_State *L, StkId p1, StkId p2) {\n  if (ttisstring(p1) || ttisnumber(p1)) p1 = p2;\n  lua_assert(!ttisstring(p1) && !ttisnumber(p1));\n  luaG_typeerror(L, p1, \"concatenate\");\n}\n\n\nvoid luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {\n  TValue temp;\n  if (luaV_tonumber(p1, &temp) == NULL)\n    p2 = p1;  /* first operand is wrong */\n  luaG_typeerror(L, p2, \"perform arithmetic on\");\n}\n\n\nint luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {\n  const char *t1 = luaT_typenames[ttype(p1)];\n  const char *t2 = luaT_typenames[ttype(p2)];\n  if (t1[2] == t2[2])\n    luaG_runerror(L, \"attempt to compare two %s values\", t1);\n  else\n    luaG_runerror(L, \"attempt to compare %s with %s\", t1, t2);\n  return 0;\n}\n\n\nstatic void addinfo (lua_State *L, const char *msg) {\n  CallInfo *ci = L->ci;\n  if (isLua(ci)) {  /* is Lua code? */\n    char buff[LUA_IDSIZE];  /* add file:line information */\n    int line = currentline(L, ci);\n    luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE);\n    luaO_pushfstring(L, \"%s:%d: %s\", buff, line, msg);\n  }\n}\n\n\nvoid luaG_errormsg (lua_State *L) {\n  if (L->errfunc != 0) {  /* is there an error handling function? */\n    StkId errfunc = restorestack(L, L->errfunc);\n    if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);\n    setobjs2s(L, L->top, L->top - 1);  /* move argument */\n    setobjs2s(L, L->top - 1, errfunc);  /* push function */\n    incr_top(L);\n    luaD_call(L, L->top - 2, 1);  /* call it */\n  }\n  luaD_throw(L, LUA_ERRRUN);\n}\n\n\nvoid luaG_runerror (lua_State *L, const char *fmt, ...) {\n  va_list argp;\n  va_start(argp, fmt);\n  addinfo(L, luaO_pushvfstring(L, fmt, argp));\n  va_end(argp);\n  luaG_errormsg(L);\n}\n\n"
  },
  {
    "path": "deps/lua/src/ldebug.h",
    "content": "/*\n** $Id: ldebug.h,v 2.3.1.1 2007/12/27 13:02:25 roberto Exp $\n** Auxiliary functions from Debug Interface module\n** See Copyright Notice in lua.h\n*/\n\n#ifndef ldebug_h\n#define ldebug_h\n\n\n#include \"lstate.h\"\n\n\n#define pcRel(pc, p)\t(cast(int, (pc) - (p)->code) - 1)\n\n#define getline(f,pc)\t(((f)->lineinfo) ? (f)->lineinfo[pc] : 0)\n\n#define resethookcount(L)\t(L->hookcount = L->basehookcount)\n\n\nLUAI_FUNC void luaG_typeerror (lua_State *L, const TValue *o,\n                                             const char *opname);\nLUAI_FUNC void luaG_concaterror (lua_State *L, StkId p1, StkId p2);\nLUAI_FUNC void luaG_aritherror (lua_State *L, const TValue *p1,\n                                              const TValue *p2);\nLUAI_FUNC int luaG_ordererror (lua_State *L, const TValue *p1,\n                                             const TValue *p2);\nLUAI_FUNC void luaG_runerror (lua_State *L, const char *fmt, ...);\nLUAI_FUNC void luaG_errormsg (lua_State *L);\nLUAI_FUNC int luaG_checkcode (const Proto *pt);\nLUAI_FUNC int luaG_checkopenop (Instruction i);\n\n#endif\n"
  },
  {
    "path": "deps/lua/src/ldo.c",
    "content": "/*\n** $Id: ldo.c,v 2.38.1.4 2012/01/18 02:27:10 roberto Exp $\n** Stack and Call structure of Lua\n** See Copyright Notice in lua.h\n*/\n\n\n#include <setjmp.h>\n#include <stdlib.h>\n#include <string.h>\n\n#define ldo_c\n#define LUA_CORE\n\n#include \"lua.h\"\n\n#include \"ldebug.h\"\n#include \"ldo.h\"\n#include \"lfunc.h\"\n#include \"lgc.h\"\n#include \"lmem.h\"\n#include \"lobject.h\"\n#include \"lopcodes.h\"\n#include \"lparser.h\"\n#include \"lstate.h\"\n#include \"lstring.h\"\n#include \"ltable.h\"\n#include \"ltm.h\"\n#include \"lundump.h\"\n#include \"lvm.h\"\n#include \"lzio.h\"\n\n\n\n\n/*\n** {======================================================\n** Error-recovery functions\n** =======================================================\n*/\n\n\n/* chain list of long jump buffers */\nstruct lua_longjmp {\n  struct lua_longjmp *previous;\n  luai_jmpbuf b;\n  volatile int status;  /* error code */\n};\n\n\nvoid luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) {\n  switch (errcode) {\n    case LUA_ERRMEM: {\n      setsvalue2s(L, oldtop, luaS_newliteral(L, MEMERRMSG));\n      break;\n    }\n    case LUA_ERRERR: {\n      setsvalue2s(L, oldtop, luaS_newliteral(L, \"error in error handling\"));\n      break;\n    }\n    case LUA_ERRSYNTAX:\n    case LUA_ERRRUN: {\n      setobjs2s(L, oldtop, L->top - 1);  /* error message on current top */\n      break;\n    }\n  }\n  L->top = oldtop + 1;\n}\n\n\nstatic void restore_stack_limit (lua_State *L) {\n  lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1);\n  if (L->size_ci > LUAI_MAXCALLS) {  /* there was an overflow? */\n    int inuse = cast_int(L->ci - L->base_ci);\n    if (inuse + 1 < LUAI_MAXCALLS)  /* can `undo' overflow? */\n      luaD_reallocCI(L, LUAI_MAXCALLS);\n  }\n}\n\n\nstatic void resetstack (lua_State *L, int status) {\n  L->ci = L->base_ci;\n  L->base = L->ci->base;\n  luaF_close(L, L->base);  /* close eventual pending closures */\n  luaD_seterrorobj(L, status, L->base);\n  L->nCcalls = L->baseCcalls;\n  L->allowhook = 1;\n  restore_stack_limit(L);\n  L->errfunc = 0;\n  L->errorJmp = NULL;\n}\n\n\nvoid luaD_throw (lua_State *L, int errcode) {\n  if (L->errorJmp) {\n    L->errorJmp->status = errcode;\n    LUAI_THROW(L, L->errorJmp);\n  }\n  else {\n    L->status = cast_byte(errcode);\n    if (G(L)->panic) {\n      resetstack(L, errcode);\n      lua_unlock(L);\n      G(L)->panic(L);\n    }\n    exit(EXIT_FAILURE);\n  }\n}\n\n\nint luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {\n  struct lua_longjmp lj;\n  lj.status = 0;\n  lj.previous = L->errorJmp;  /* chain new error handler */\n  L->errorJmp = &lj;\n  LUAI_TRY(L, &lj,\n    (*f)(L, ud);\n  );\n  L->errorJmp = lj.previous;  /* restore old error handler */\n  return lj.status;\n}\n\n/* }====================================================== */\n\n\nstatic void correctstack (lua_State *L, TValue *oldstack) {\n  CallInfo *ci;\n  GCObject *up;\n  L->top = (L->top - oldstack) + L->stack;\n  for (up = L->openupval; up != NULL; up = up->gch.next)\n    gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack;\n  for (ci = L->base_ci; ci <= L->ci; ci++) {\n    ci->top = (ci->top - oldstack) + L->stack;\n    ci->base = (ci->base - oldstack) + L->stack;\n    ci->func = (ci->func - oldstack) + L->stack;\n  }\n  L->base = (L->base - oldstack) + L->stack;\n}\n\n\nvoid luaD_reallocstack (lua_State *L, int newsize) {\n  TValue *oldstack = L->stack;\n  int realsize = newsize + 1 + EXTRA_STACK;\n  lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1);\n  luaM_reallocvector(L, L->stack, L->stacksize, realsize, TValue);\n  L->stacksize = realsize;\n  L->stack_last = L->stack+newsize;\n  correctstack(L, oldstack);\n}\n\n\nvoid luaD_reallocCI (lua_State *L, int newsize) {\n  CallInfo *oldci = L->base_ci;\n  luaM_reallocvector(L, L->base_ci, L->size_ci, newsize, CallInfo);\n  L->size_ci = newsize;\n  L->ci = (L->ci - oldci) + L->base_ci;\n  L->end_ci = L->base_ci + L->size_ci - 1;\n}\n\n\nvoid luaD_growstack (lua_State *L, int n) {\n  if (n <= L->stacksize)  /* double size is enough? */\n    luaD_reallocstack(L, 2*L->stacksize);\n  else\n    luaD_reallocstack(L, L->stacksize + n);\n}\n\n\nstatic CallInfo *growCI (lua_State *L) {\n  if (L->size_ci > LUAI_MAXCALLS)  /* overflow while handling overflow? */\n    luaD_throw(L, LUA_ERRERR);\n  else {\n    luaD_reallocCI(L, 2*L->size_ci);\n    if (L->size_ci > LUAI_MAXCALLS)\n      luaG_runerror(L, \"stack overflow\");\n  }\n  return ++L->ci;\n}\n\n\nvoid luaD_callhook (lua_State *L, int event, int line) {\n  lua_Hook hook = L->hook;\n  if (hook && L->allowhook) {\n    ptrdiff_t top = savestack(L, L->top);\n    ptrdiff_t ci_top = savestack(L, L->ci->top);\n    lua_Debug ar;\n    ar.event = event;\n    ar.currentline = line;\n    if (event == LUA_HOOKTAILRET)\n      ar.i_ci = 0;  /* tail call; no debug information about it */\n    else\n      ar.i_ci = cast_int(L->ci - L->base_ci);\n    luaD_checkstack(L, LUA_MINSTACK);  /* ensure minimum stack size */\n    L->ci->top = L->top + LUA_MINSTACK;\n    lua_assert(L->ci->top <= L->stack_last);\n    L->allowhook = 0;  /* cannot call hooks inside a hook */\n    lua_unlock(L);\n    (*hook)(L, &ar);\n    lua_lock(L);\n    lua_assert(!L->allowhook);\n    L->allowhook = 1;\n    L->ci->top = restorestack(L, ci_top);\n    L->top = restorestack(L, top);\n  }\n}\n\n\nstatic StkId adjust_varargs (lua_State *L, Proto *p, int actual) {\n  int i;\n  int nfixargs = p->numparams;\n  Table *htab = NULL;\n  StkId base, fixed;\n  for (; actual < nfixargs; ++actual)\n    setnilvalue(L->top++);\n#if defined(LUA_COMPAT_VARARG)\n  if (p->is_vararg & VARARG_NEEDSARG) { /* compat. with old-style vararg? */\n    int nvar = actual - nfixargs;  /* number of extra arguments */\n    lua_assert(p->is_vararg & VARARG_HASARG);\n    luaC_checkGC(L);\n    luaD_checkstack(L, p->maxstacksize);\n    htab = luaH_new(L, nvar, 1);  /* create `arg' table */\n    for (i=0; i<nvar; i++)  /* put extra arguments into `arg' table */\n      setobj2n(L, luaH_setnum(L, htab, i+1), L->top - nvar + i);\n    /* store counter in field `n' */\n    setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, \"n\")), cast_num(nvar));\n  }\n#endif\n  /* move fixed parameters to final position */\n  fixed = L->top - actual;  /* first fixed argument */\n  base = L->top;  /* final position of first argument */\n  for (i=0; i<nfixargs; i++) {\n    setobjs2s(L, L->top++, fixed+i);\n    setnilvalue(fixed+i);\n  }\n  /* add `arg' parameter */\n  if (htab) {\n    sethvalue(L, L->top++, htab);\n    lua_assert(iswhite(obj2gco(htab)));\n  }\n  return base;\n}\n\n\nstatic StkId tryfuncTM (lua_State *L, StkId func) {\n  const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL);\n  StkId p;\n  ptrdiff_t funcr = savestack(L, func);\n  if (!ttisfunction(tm))\n    luaG_typeerror(L, func, \"call\");\n  /* Open a hole inside the stack at `func' */\n  for (p = L->top; p > func; p--) setobjs2s(L, p, p-1);\n  incr_top(L);\n  func = restorestack(L, funcr);  /* previous call may change stack */\n  setobj2s(L, func, tm);  /* tag method is the new function to be called */\n  return func;\n}\n\n\n\n#define inc_ci(L) \\\n  ((L->ci == L->end_ci) ? growCI(L) : \\\n   (condhardstacktests(luaD_reallocCI(L, L->size_ci)), ++L->ci))\n\n\nint luaD_precall (lua_State *L, StkId func, int nresults) {\n  LClosure *cl;\n  ptrdiff_t funcr;\n  if (!ttisfunction(func)) /* `func' is not a function? */\n    func = tryfuncTM(L, func);  /* check the `function' tag method */\n  funcr = savestack(L, func);\n  cl = &clvalue(func)->l;\n  L->ci->savedpc = L->savedpc;\n  if (!cl->isC) {  /* Lua function? prepare its call */\n    CallInfo *ci;\n    StkId st, base;\n    Proto *p = cl->p;\n    luaD_checkstack(L, p->maxstacksize + p->numparams);\n    func = restorestack(L, funcr);\n    if (!p->is_vararg) {  /* no varargs? */\n      base = func + 1;\n      if (L->top > base + p->numparams)\n        L->top = base + p->numparams;\n    }\n    else {  /* vararg function */\n      int nargs = cast_int(L->top - func) - 1;\n      base = adjust_varargs(L, p, nargs);\n      func = restorestack(L, funcr);  /* previous call may change the stack */\n    }\n    ci = inc_ci(L);  /* now `enter' new function */\n    ci->func = func;\n    L->base = ci->base = base;\n    ci->top = L->base + p->maxstacksize;\n    lua_assert(ci->top <= L->stack_last);\n    L->savedpc = p->code;  /* starting point */\n    ci->tailcalls = 0;\n    ci->nresults = nresults;\n    for (st = L->top; st < ci->top; st++)\n      setnilvalue(st);\n    L->top = ci->top;\n    if (L->hookmask & LUA_MASKCALL) {\n      L->savedpc++;  /* hooks assume 'pc' is already incremented */\n      luaD_callhook(L, LUA_HOOKCALL, -1);\n      L->savedpc--;  /* correct 'pc' */\n    }\n    return PCRLUA;\n  }\n  else {  /* if is a C function, call it */\n    CallInfo *ci;\n    int n;\n    luaD_checkstack(L, LUA_MINSTACK);  /* ensure minimum stack size */\n    ci = inc_ci(L);  /* now `enter' new function */\n    ci->func = restorestack(L, funcr);\n    L->base = ci->base = ci->func + 1;\n    ci->top = L->top + LUA_MINSTACK;\n    lua_assert(ci->top <= L->stack_last);\n    ci->nresults = nresults;\n    if (L->hookmask & LUA_MASKCALL)\n      luaD_callhook(L, LUA_HOOKCALL, -1);\n    lua_unlock(L);\n    n = (*curr_func(L)->c.f)(L);  /* do the actual call */\n    lua_lock(L);\n    if (n < 0)  /* yielding? */\n      return PCRYIELD;\n    else {\n      luaD_poscall(L, L->top - n);\n      return PCRC;\n    }\n  }\n}\n\n\nstatic StkId callrethooks (lua_State *L, StkId firstResult) {\n  ptrdiff_t fr = savestack(L, firstResult);  /* next call may change stack */\n  luaD_callhook(L, LUA_HOOKRET, -1);\n  if (f_isLua(L->ci)) {  /* Lua function? */\n    while ((L->hookmask & LUA_MASKRET) && L->ci->tailcalls--) /* tail calls */\n      luaD_callhook(L, LUA_HOOKTAILRET, -1);\n  }\n  return restorestack(L, fr);\n}\n\n\nint luaD_poscall (lua_State *L, StkId firstResult) {\n  StkId res;\n  int wanted, i;\n  CallInfo *ci;\n  if (L->hookmask & LUA_MASKRET)\n    firstResult = callrethooks(L, firstResult);\n  ci = L->ci--;\n  res = ci->func;  /* res == final position of 1st result */\n  wanted = ci->nresults;\n  L->base = (ci - 1)->base;  /* restore base */\n  L->savedpc = (ci - 1)->savedpc;  /* restore savedpc */\n  /* move results to correct place */\n  for (i = wanted; i != 0 && firstResult < L->top; i--)\n    setobjs2s(L, res++, firstResult++);\n  while (i-- > 0)\n    setnilvalue(res++);\n  L->top = res;\n  return (wanted - LUA_MULTRET);  /* 0 iff wanted == LUA_MULTRET */\n}\n\n\n/*\n** Call a function (C or Lua). The function to be called is at *func.\n** The arguments are on the stack, right after the function.\n** When returns, all the results are on the stack, starting at the original\n** function position.\n*/ \nvoid luaD_call (lua_State *L, StkId func, int nResults) {\n  if (++L->nCcalls >= LUAI_MAXCCALLS) {\n    if (L->nCcalls == LUAI_MAXCCALLS)\n      luaG_runerror(L, \"C stack overflow\");\n    else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))\n      luaD_throw(L, LUA_ERRERR);  /* error while handing stack error */\n  }\n  if (luaD_precall(L, func, nResults) == PCRLUA)  /* is a Lua function? */\n    luaV_execute(L, 1);  /* call it */\n  L->nCcalls--;\n  luaC_checkGC(L);\n}\n\n\nstatic void resume (lua_State *L, void *ud) {\n  StkId firstArg = cast(StkId, ud);\n  CallInfo *ci = L->ci;\n  if (L->status == 0) {  /* start coroutine? */\n    lua_assert(ci == L->base_ci && firstArg > L->base);\n    if (luaD_precall(L, firstArg - 1, LUA_MULTRET) != PCRLUA)\n      return;\n  }\n  else {  /* resuming from previous yield */\n    lua_assert(L->status == LUA_YIELD);\n    L->status = 0;\n    if (!f_isLua(ci)) {  /* `common' yield? */\n      /* finish interrupted execution of `OP_CALL' */\n      lua_assert(GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_CALL ||\n                 GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_TAILCALL);\n      if (luaD_poscall(L, firstArg))  /* complete it... */\n        L->top = L->ci->top;  /* and correct top if not multiple results */\n    }\n    else  /* yielded inside a hook: just continue its execution */\n      L->base = L->ci->base;\n  }\n  luaV_execute(L, cast_int(L->ci - L->base_ci));\n}\n\n\nstatic int resume_error (lua_State *L, const char *msg) {\n  L->top = L->ci->base;\n  setsvalue2s(L, L->top, luaS_new(L, msg));\n  incr_top(L);\n  lua_unlock(L);\n  return LUA_ERRRUN;\n}\n\n\nLUA_API int lua_resume (lua_State *L, int nargs) {\n  int status;\n  lua_lock(L);\n  if (L->status != LUA_YIELD && (L->status != 0 || L->ci != L->base_ci))\n      return resume_error(L, \"cannot resume non-suspended coroutine\");\n  if (L->nCcalls >= LUAI_MAXCCALLS)\n    return resume_error(L, \"C stack overflow\");\n  luai_userstateresume(L, nargs);\n  lua_assert(L->errfunc == 0);\n  L->baseCcalls = ++L->nCcalls;\n  status = luaD_rawrunprotected(L, resume, L->top - nargs);\n  if (status != 0) {  /* error? */\n    L->status = cast_byte(status);  /* mark thread as `dead' */\n    luaD_seterrorobj(L, status, L->top);\n    L->ci->top = L->top;\n  }\n  else {\n    lua_assert(L->nCcalls == L->baseCcalls);\n    status = L->status;\n  }\n  --L->nCcalls;\n  lua_unlock(L);\n  return status;\n}\n\n\nLUA_API int lua_yield (lua_State *L, int nresults) {\n  luai_userstateyield(L, nresults);\n  lua_lock(L);\n  if (L->nCcalls > L->baseCcalls)\n    luaG_runerror(L, \"attempt to yield across metamethod/C-call boundary\");\n  L->base = L->top - nresults;  /* protect stack slots below */\n  L->status = LUA_YIELD;\n  lua_unlock(L);\n  return -1;\n}\n\n\nint luaD_pcall (lua_State *L, Pfunc func, void *u,\n                ptrdiff_t old_top, ptrdiff_t ef) {\n  int status;\n  unsigned short oldnCcalls = L->nCcalls;\n  ptrdiff_t old_ci = saveci(L, L->ci);\n  lu_byte old_allowhooks = L->allowhook;\n  ptrdiff_t old_errfunc = L->errfunc;\n  L->errfunc = ef;\n  status = luaD_rawrunprotected(L, func, u);\n  if (status != 0) {  /* an error occurred? */\n    StkId oldtop = restorestack(L, old_top);\n    luaF_close(L, oldtop);  /* close eventual pending closures */\n    luaD_seterrorobj(L, status, oldtop);\n    L->nCcalls = oldnCcalls;\n    L->ci = restoreci(L, old_ci);\n    L->base = L->ci->base;\n    L->savedpc = L->ci->savedpc;\n    L->allowhook = old_allowhooks;\n    restore_stack_limit(L);\n  }\n  L->errfunc = old_errfunc;\n  return status;\n}\n\n\n\n/*\n** Execute a protected parser.\n*/\nstruct SParser {  /* data to `f_parser' */\n  ZIO *z;\n  Mbuffer buff;  /* buffer to be used by the scanner */\n  const char *name;\n};\n\nstatic void f_parser (lua_State *L, void *ud) {\n  int i;\n  Proto *tf;\n  Closure *cl;\n  struct SParser *p = cast(struct SParser *, ud);\n  luaZ_lookahead(p->z);\n  luaC_checkGC(L);\n  tf = (luaY_parser)(L, p->z,\n                                                             &p->buff, p->name);\n  cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L)));\n  cl->l.p = tf;\n  for (i = 0; i < tf->nups; i++)  /* initialize eventual upvalues */\n    cl->l.upvals[i] = luaF_newupval(L);\n  setclvalue(L, L->top, cl);\n  incr_top(L);\n}\n\n\nint luaD_protectedparser (lua_State *L, ZIO *z, const char *name) {\n  struct SParser p;\n  int status;\n  p.z = z; p.name = name;\n  luaZ_initbuffer(L, &p.buff);\n  status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);\n  luaZ_freebuffer(L, &p.buff);\n  return status;\n}\n\n\n"
  },
  {
    "path": "deps/lua/src/ldo.h",
    "content": "/*\n** $Id: ldo.h,v 2.7.1.1 2007/12/27 13:02:25 roberto Exp $\n** Stack and Call structure of Lua\n** See Copyright Notice in lua.h\n*/\n\n#ifndef ldo_h\n#define ldo_h\n\n\n#include \"lobject.h\"\n#include \"lstate.h\"\n#include \"lzio.h\"\n\n\n#define luaD_checkstack(L,n)\t\\\n  if ((char *)L->stack_last - (char *)L->top <= (n)*(int)sizeof(TValue)) \\\n    luaD_growstack(L, n); \\\n  else condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1));\n\n\n#define incr_top(L) {luaD_checkstack(L,1); L->top++;}\n\n#define savestack(L,p)\t\t((char *)(p) - (char *)L->stack)\n#define restorestack(L,n)\t((TValue *)((char *)L->stack + (n)))\n\n#define saveci(L,p)\t\t((char *)(p) - (char *)L->base_ci)\n#define restoreci(L,n)\t\t((CallInfo *)((char *)L->base_ci + (n)))\n\n\n/* results from luaD_precall */\n#define PCRLUA\t\t0\t/* initiated a call to a Lua function */\n#define PCRC\t\t1\t/* did a call to a C function */\n#define PCRYIELD\t2\t/* C funtion yielded */\n\n\n/* type of protected functions, to be ran by `runprotected' */\ntypedef void (*Pfunc) (lua_State *L, void *ud);\n\nLUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name);\nLUAI_FUNC void luaD_callhook (lua_State *L, int event, int line);\nLUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults);\nLUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults);\nLUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u,\n                                        ptrdiff_t oldtop, ptrdiff_t ef);\nLUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult);\nLUAI_FUNC void luaD_reallocCI (lua_State *L, int newsize);\nLUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize);\nLUAI_FUNC void luaD_growstack (lua_State *L, int n);\n\nLUAI_FUNC void luaD_throw (lua_State *L, int errcode);\nLUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud);\n\nLUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop);\n\n#endif\n\n"
  },
  {
    "path": "deps/lua/src/ldump.c",
    "content": "/*\n** $Id: ldump.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $\n** save precompiled Lua chunks\n** See Copyright Notice in lua.h\n*/\n\n#include <stddef.h>\n\n#define ldump_c\n#define LUA_CORE\n\n#include \"lua.h\"\n\n#include \"lobject.h\"\n#include \"lstate.h\"\n#include \"lundump.h\"\n\ntypedef struct {\n lua_State* L;\n lua_Writer writer;\n void* data;\n int strip;\n int status;\n} DumpState;\n\n#define DumpMem(b,n,size,D)\tDumpBlock(b,(n)*(size),D)\n#define DumpVar(x,D)\t \tDumpMem(&x,1,sizeof(x),D)\n\nstatic void DumpBlock(const void* b, size_t size, DumpState* D)\n{\n if (D->status==0)\n {\n  lua_unlock(D->L);\n  D->status=(*D->writer)(D->L,b,size,D->data);\n  lua_lock(D->L);\n }\n}\n\nstatic void DumpChar(int y, DumpState* D)\n{\n char x=(char)y;\n DumpVar(x,D);\n}\n\nstatic void DumpInt(int x, DumpState* D)\n{\n DumpVar(x,D);\n}\n\nstatic void DumpNumber(lua_Number x, DumpState* D)\n{\n DumpVar(x,D);\n}\n\nstatic void DumpVector(const void* b, int n, size_t size, DumpState* D)\n{\n DumpInt(n,D);\n DumpMem(b,n,size,D);\n}\n\nstatic void DumpString(const TString* s, DumpState* D)\n{\n if (s==NULL || getstr(s)==NULL)\n {\n  size_t size=0;\n  DumpVar(size,D);\n }\n else\n {\n  size_t size=s->tsv.len+1;\t\t/* include trailing '\\0' */\n  DumpVar(size,D);\n  DumpBlock(getstr(s),size,D);\n }\n}\n\n#define DumpCode(f,D)\t DumpVector(f->code,f->sizecode,sizeof(Instruction),D)\n\nstatic void DumpFunction(const Proto* f, const TString* p, DumpState* D);\n\nstatic void DumpConstants(const Proto* f, DumpState* D)\n{\n int i,n=f->sizek;\n DumpInt(n,D);\n for (i=0; i<n; i++)\n {\n  const TValue* o=&f->k[i];\n  DumpChar(ttype(o),D);\n  switch (ttype(o))\n  {\n   case LUA_TNIL:\n\tbreak;\n   case LUA_TBOOLEAN:\n\tDumpChar(bvalue(o),D);\n\tbreak;\n   case LUA_TNUMBER:\n\tDumpNumber(nvalue(o),D);\n\tbreak;\n   case LUA_TSTRING:\n\tDumpString(rawtsvalue(o),D);\n\tbreak;\n   default:\n\tlua_assert(0);\t\t\t/* cannot happen */\n\tbreak;\n  }\n }\n n=f->sizep;\n DumpInt(n,D);\n for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D);\n}\n\nstatic void DumpDebug(const Proto* f, DumpState* D)\n{\n int i,n;\n n= (D->strip) ? 0 : f->sizelineinfo;\n DumpVector(f->lineinfo,n,sizeof(int),D);\n n= (D->strip) ? 0 : f->sizelocvars;\n DumpInt(n,D);\n for (i=0; i<n; i++)\n {\n  DumpString(f->locvars[i].varname,D);\n  DumpInt(f->locvars[i].startpc,D);\n  DumpInt(f->locvars[i].endpc,D);\n }\n n= (D->strip) ? 0 : f->sizeupvalues;\n DumpInt(n,D);\n for (i=0; i<n; i++) DumpString(f->upvalues[i],D);\n}\n\nstatic void DumpFunction(const Proto* f, const TString* p, DumpState* D)\n{\n DumpString((f->source==p || D->strip) ? NULL : f->source,D);\n DumpInt(f->linedefined,D);\n DumpInt(f->lastlinedefined,D);\n DumpChar(f->nups,D);\n DumpChar(f->numparams,D);\n DumpChar(f->is_vararg,D);\n DumpChar(f->maxstacksize,D);\n DumpCode(f,D);\n DumpConstants(f,D);\n DumpDebug(f,D);\n}\n\nstatic void DumpHeader(DumpState* D)\n{\n char h[LUAC_HEADERSIZE];\n luaU_header(h);\n DumpBlock(h,LUAC_HEADERSIZE,D);\n}\n\n/*\n** dump Lua function as precompiled chunk\n*/\nint luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip)\n{\n DumpState D;\n D.L=L;\n D.writer=w;\n D.data=data;\n D.strip=strip;\n D.status=0;\n DumpHeader(&D);\n DumpFunction(f,NULL,&D);\n return D.status;\n}\n"
  },
  {
    "path": "deps/lua/src/lfunc.c",
    "content": "/*\n** $Id: lfunc.c,v 2.12.1.2 2007/12/28 14:58:43 roberto Exp $\n** Auxiliary functions to manipulate prototypes and closures\n** See Copyright Notice in lua.h\n*/\n\n\n#include <stddef.h>\n\n#define lfunc_c\n#define LUA_CORE\n\n#include \"lua.h\"\n\n#include \"lfunc.h\"\n#include \"lgc.h\"\n#include \"lmem.h\"\n#include \"lobject.h\"\n#include \"lstate.h\"\n\n\n\nClosure *luaF_newCclosure (lua_State *L, int nelems, Table *e) {\n  Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems)));\n  luaC_link(L, obj2gco(c), LUA_TFUNCTION);\n  c->c.isC = 1;\n  c->c.env = e;\n  c->c.nupvalues = cast_byte(nelems);\n  return c;\n}\n\n\nClosure *luaF_newLclosure (lua_State *L, int nelems, Table *e) {\n  Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems)));\n  luaC_link(L, obj2gco(c), LUA_TFUNCTION);\n  c->l.isC = 0;\n  c->l.env = e;\n  c->l.nupvalues = cast_byte(nelems);\n  while (nelems--) c->l.upvals[nelems] = NULL;\n  return c;\n}\n\n\nUpVal *luaF_newupval (lua_State *L) {\n  UpVal *uv = luaM_new(L, UpVal);\n  luaC_link(L, obj2gco(uv), LUA_TUPVAL);\n  uv->v = &uv->u.value;\n  setnilvalue(uv->v);\n  return uv;\n}\n\n\nUpVal *luaF_findupval (lua_State *L, StkId level) {\n  global_State *g = G(L);\n  GCObject **pp = &L->openupval;\n  UpVal *p;\n  UpVal *uv;\n  while (*pp != NULL && (p = ngcotouv(*pp))->v >= level) {\n    lua_assert(p->v != &p->u.value);\n    if (p->v == level) {  /* found a corresponding upvalue? */\n      if (isdead(g, obj2gco(p)))  /* is it dead? */\n        changewhite(obj2gco(p));  /* ressurect it */\n      return p;\n    }\n    pp = &p->next;\n  }\n  uv = luaM_new(L, UpVal);  /* not found: create a new one */\n  uv->tt = LUA_TUPVAL;\n  uv->marked = luaC_white(g);\n  uv->v = level;  /* current value lives in the stack */\n  uv->next = *pp;  /* chain it in the proper position */\n  *pp = obj2gco(uv);\n  uv->u.l.prev = &g->uvhead;  /* double link it in `uvhead' list */\n  uv->u.l.next = g->uvhead.u.l.next;\n  uv->u.l.next->u.l.prev = uv;\n  g->uvhead.u.l.next = uv;\n  lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);\n  return uv;\n}\n\n\nstatic void unlinkupval (UpVal *uv) {\n  lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);\n  uv->u.l.next->u.l.prev = uv->u.l.prev;  /* remove from `uvhead' list */\n  uv->u.l.prev->u.l.next = uv->u.l.next;\n}\n\n\nvoid luaF_freeupval (lua_State *L, UpVal *uv) {\n  if (uv->v != &uv->u.value)  /* is it open? */\n    unlinkupval(uv);  /* remove from open list */\n  luaM_free(L, uv);  /* free upvalue */\n}\n\n\nvoid luaF_close (lua_State *L, StkId level) {\n  UpVal *uv;\n  global_State *g = G(L);\n  while (L->openupval != NULL && (uv = ngcotouv(L->openupval))->v >= level) {\n    GCObject *o = obj2gco(uv);\n    lua_assert(!isblack(o) && uv->v != &uv->u.value);\n    L->openupval = uv->next;  /* remove from `open' list */\n    if (isdead(g, o))\n      luaF_freeupval(L, uv);  /* free upvalue */\n    else {\n      unlinkupval(uv);\n      setobj(L, &uv->u.value, uv->v);\n      uv->v = &uv->u.value;  /* now current value lives here */\n      luaC_linkupval(L, uv);  /* link upvalue into `gcroot' list */\n    }\n  }\n}\n\n\nProto *luaF_newproto (lua_State *L) {\n  Proto *f = luaM_new(L, Proto);\n  luaC_link(L, obj2gco(f), LUA_TPROTO);\n  f->k = NULL;\n  f->sizek = 0;\n  f->p = NULL;\n  f->sizep = 0;\n  f->code = NULL;\n  f->sizecode = 0;\n  f->sizelineinfo = 0;\n  f->sizeupvalues = 0;\n  f->nups = 0;\n  f->upvalues = NULL;\n  f->numparams = 0;\n  f->is_vararg = 0;\n  f->maxstacksize = 0;\n  f->lineinfo = NULL;\n  f->sizelocvars = 0;\n  f->locvars = NULL;\n  f->linedefined = 0;\n  f->lastlinedefined = 0;\n  f->source = NULL;\n  return f;\n}\n\n\nvoid luaF_freeproto (lua_State *L, Proto *f) {\n  luaM_freearray(L, f->code, f->sizecode, Instruction);\n  luaM_freearray(L, f->p, f->sizep, Proto *);\n  luaM_freearray(L, f->k, f->sizek, TValue);\n  luaM_freearray(L, f->lineinfo, f->sizelineinfo, int);\n  luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar);\n  luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *);\n  luaM_free(L, f);\n}\n\n\nvoid luaF_freeclosure (lua_State *L, Closure *c) {\n  int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) :\n                          sizeLclosure(c->l.nupvalues);\n  luaM_freemem(L, c, size);\n}\n\n\n/*\n** Look for n-th local variable at line `line' in function `func'.\n** Returns NULL if not found.\n*/\nconst char *luaF_getlocalname (const Proto *f, int local_number, int pc) {\n  int i;\n  for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {\n    if (pc < f->locvars[i].endpc) {  /* is variable active? */\n      local_number--;\n      if (local_number == 0)\n        return getstr(f->locvars[i].varname);\n    }\n  }\n  return NULL;  /* not found */\n}\n\n"
  },
  {
    "path": "deps/lua/src/lfunc.h",
    "content": "/*\n** $Id: lfunc.h,v 2.4.1.1 2007/12/27 13:02:25 roberto Exp $\n** Auxiliary functions to manipulate prototypes and closures\n** See Copyright Notice in lua.h\n*/\n\n#ifndef lfunc_h\n#define lfunc_h\n\n\n#include \"lobject.h\"\n\n\n#define sizeCclosure(n)\t(cast(int, sizeof(CClosure)) + \\\n                         cast(int, sizeof(TValue)*((n)-1)))\n\n#define sizeLclosure(n)\t(cast(int, sizeof(LClosure)) + \\\n                         cast(int, sizeof(TValue *)*((n)-1)))\n\n\nLUAI_FUNC Proto *luaF_newproto (lua_State *L);\nLUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e);\nLUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e);\nLUAI_FUNC UpVal *luaF_newupval (lua_State *L);\nLUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level);\nLUAI_FUNC void luaF_close (lua_State *L, StkId level);\nLUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f);\nLUAI_FUNC void luaF_freeclosure (lua_State *L, Closure *c);\nLUAI_FUNC void luaF_freeupval (lua_State *L, UpVal *uv);\nLUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number,\n                                         int pc);\n\n\n#endif\n"
  },
  {
    "path": "deps/lua/src/lgc.c",
    "content": "/*\n** $Id: lgc.c,v 2.38.1.2 2011/03/18 18:05:38 roberto Exp $\n** Garbage Collector\n** See Copyright Notice in lua.h\n*/\n\n#include <string.h>\n\n#define lgc_c\n#define LUA_CORE\n\n#include \"lua.h\"\n\n#include \"ldebug.h\"\n#include \"ldo.h\"\n#include \"lfunc.h\"\n#include \"lgc.h\"\n#include \"lmem.h\"\n#include \"lobject.h\"\n#include \"lstate.h\"\n#include \"lstring.h\"\n#include \"ltable.h\"\n#include \"ltm.h\"\n\n\n#define GCSTEPSIZE\t1024u\n#define GCSWEEPMAX\t40\n#define GCSWEEPCOST\t10\n#define GCFINALIZECOST\t100\n\n\n#define maskmarks\tcast_byte(~(bitmask(BLACKBIT)|WHITEBITS))\n\n#define makewhite(g,x)\t\\\n   ((x)->gch.marked = cast_byte(((x)->gch.marked & maskmarks) | luaC_white(g)))\n\n#define white2gray(x)\treset2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT)\n#define black2gray(x)\tresetbit((x)->gch.marked, BLACKBIT)\n\n#define stringmark(s)\treset2bits((s)->tsv.marked, WHITE0BIT, WHITE1BIT)\n\n\n#define isfinalized(u)\t\ttestbit((u)->marked, FINALIZEDBIT)\n#define markfinalized(u)\tl_setbit((u)->marked, FINALIZEDBIT)\n\n\n#define KEYWEAK         bitmask(KEYWEAKBIT)\n#define VALUEWEAK       bitmask(VALUEWEAKBIT)\n\n\n\n#define markvalue(g,o) { checkconsistency(o); \\\n  if (iscollectable(o) && iswhite(gcvalue(o))) reallymarkobject(g,gcvalue(o)); }\n\n#define markobject(g,t) { if (iswhite(obj2gco(t))) \\\n\t\treallymarkobject(g, obj2gco(t)); }\n\n\n#define setthreshold(g)  (g->GCthreshold = (g->estimate/100) * g->gcpause)\n\n\nstatic void removeentry (Node *n) {\n  lua_assert(ttisnil(gval(n)));\n  if (iscollectable(gkey(n)))\n    setttype(gkey(n), LUA_TDEADKEY);  /* dead key; remove it */\n}\n\n\nstatic void reallymarkobject (global_State *g, GCObject *o) {\n  lua_assert(iswhite(o) && !isdead(g, o));\n  white2gray(o);\n  switch (o->gch.tt) {\n    case LUA_TSTRING: {\n      return;\n    }\n    case LUA_TUSERDATA: {\n      Table *mt = gco2u(o)->metatable;\n      gray2black(o);  /* udata are never gray */\n      if (mt) markobject(g, mt);\n      markobject(g, gco2u(o)->env);\n      return;\n    }\n    case LUA_TUPVAL: {\n      UpVal *uv = gco2uv(o);\n      markvalue(g, uv->v);\n      if (uv->v == &uv->u.value)  /* closed? */\n        gray2black(o);  /* open upvalues are never black */\n      return;\n    }\n    case LUA_TFUNCTION: {\n      gco2cl(o)->c.gclist = g->gray;\n      g->gray = o;\n      break;\n    }\n    case LUA_TTABLE: {\n      gco2h(o)->gclist = g->gray;\n      g->gray = o;\n      break;\n    }\n    case LUA_TTHREAD: {\n      gco2th(o)->gclist = g->gray;\n      g->gray = o;\n      break;\n    }\n    case LUA_TPROTO: {\n      gco2p(o)->gclist = g->gray;\n      g->gray = o;\n      break;\n    }\n    default: lua_assert(0);\n  }\n}\n\n\nstatic void marktmu (global_State *g) {\n  GCObject *u = g->tmudata;\n  if (u) {\n    do {\n      u = u->gch.next;\n      makewhite(g, u);  /* may be marked, if left from previous GC */\n      reallymarkobject(g, u);\n    } while (u != g->tmudata);\n  }\n}\n\n\n/* move `dead' udata that need finalization to list `tmudata' */\nsize_t luaC_separateudata (lua_State *L, int all) {\n  global_State *g = G(L);\n  size_t deadmem = 0;\n  GCObject **p = &g->mainthread->next;\n  GCObject *curr;\n  while ((curr = *p) != NULL) {\n    if (!(iswhite(curr) || all) || isfinalized(gco2u(curr)))\n      p = &curr->gch.next;  /* don't bother with them */\n    else if (fasttm(L, gco2u(curr)->metatable, TM_GC) == NULL) {\n      markfinalized(gco2u(curr));  /* don't need finalization */\n      p = &curr->gch.next;\n    }\n    else {  /* must call its gc method */\n      deadmem += sizeudata(gco2u(curr));\n      markfinalized(gco2u(curr));\n      *p = curr->gch.next;\n      /* link `curr' at the end of `tmudata' list */\n      if (g->tmudata == NULL)  /* list is empty? */\n        g->tmudata = curr->gch.next = curr;  /* creates a circular list */\n      else {\n        curr->gch.next = g->tmudata->gch.next;\n        g->tmudata->gch.next = curr;\n        g->tmudata = curr;\n      }\n    }\n  }\n  return deadmem;\n}\n\n\nstatic int traversetable (global_State *g, Table *h) {\n  int i;\n  int weakkey = 0;\n  int weakvalue = 0;\n  const TValue *mode;\n  if (h->metatable)\n    markobject(g, h->metatable);\n  mode = gfasttm(g, h->metatable, TM_MODE);\n  if (mode && ttisstring(mode)) {  /* is there a weak mode? */\n    weakkey = (strchr(svalue(mode), 'k') != NULL);\n    weakvalue = (strchr(svalue(mode), 'v') != NULL);\n    if (weakkey || weakvalue) {  /* is really weak? */\n      h->marked &= ~(KEYWEAK | VALUEWEAK);  /* clear bits */\n      h->marked |= cast_byte((weakkey << KEYWEAKBIT) |\n                             (weakvalue << VALUEWEAKBIT));\n      h->gclist = g->weak;  /* must be cleared after GC, ... */\n      g->weak = obj2gco(h);  /* ... so put in the appropriate list */\n    }\n  }\n  if (weakkey && weakvalue) return 1;\n  if (!weakvalue) {\n    i = h->sizearray;\n    while (i--)\n      markvalue(g, &h->array[i]);\n  }\n  i = sizenode(h);\n  while (i--) {\n    Node *n = gnode(h, i);\n    lua_assert(ttype(gkey(n)) != LUA_TDEADKEY || ttisnil(gval(n)));\n    if (ttisnil(gval(n)))\n      removeentry(n);  /* remove empty entries */\n    else {\n      lua_assert(!ttisnil(gkey(n)));\n      if (!weakkey) markvalue(g, gkey(n));\n      if (!weakvalue) markvalue(g, gval(n));\n    }\n  }\n  return weakkey || weakvalue;\n}\n\n\n/*\n** All marks are conditional because a GC may happen while the\n** prototype is still being created\n*/\nstatic void traverseproto (global_State *g, Proto *f) {\n  int i;\n  if (f->source) stringmark(f->source);\n  for (i=0; i<f->sizek; i++)  /* mark literals */\n    markvalue(g, &f->k[i]);\n  for (i=0; i<f->sizeupvalues; i++) {  /* mark upvalue names */\n    if (f->upvalues[i])\n      stringmark(f->upvalues[i]);\n  }\n  for (i=0; i<f->sizep; i++) {  /* mark nested protos */\n    if (f->p[i])\n      markobject(g, f->p[i]);\n  }\n  for (i=0; i<f->sizelocvars; i++) {  /* mark local-variable names */\n    if (f->locvars[i].varname)\n      stringmark(f->locvars[i].varname);\n  }\n}\n\n\n\nstatic void traverseclosure (global_State *g, Closure *cl) {\n  markobject(g, cl->c.env);\n  if (cl->c.isC) {\n    int i;\n    for (i=0; i<cl->c.nupvalues; i++)  /* mark its upvalues */\n      markvalue(g, &cl->c.upvalue[i]);\n  }\n  else {\n    int i;\n    lua_assert(cl->l.nupvalues == cl->l.p->nups);\n    markobject(g, cl->l.p);\n    for (i=0; i<cl->l.nupvalues; i++)  /* mark its upvalues */\n      markobject(g, cl->l.upvals[i]);\n  }\n}\n\n\nstatic void checkstacksizes (lua_State *L, StkId max) {\n  int ci_used = cast_int(L->ci - L->base_ci);  /* number of `ci' in use */\n  int s_used = cast_int(max - L->stack);  /* part of stack in use */\n  if (L->size_ci > LUAI_MAXCALLS)  /* handling overflow? */\n    return;  /* do not touch the stacks */\n  if (4*ci_used < L->size_ci && 2*BASIC_CI_SIZE < L->size_ci)\n    luaD_reallocCI(L, L->size_ci/2);  /* still big enough... */\n  condhardstacktests(luaD_reallocCI(L, ci_used + 1));\n  if (4*s_used < L->stacksize &&\n      2*(BASIC_STACK_SIZE+EXTRA_STACK) < L->stacksize)\n    luaD_reallocstack(L, L->stacksize/2);  /* still big enough... */\n  condhardstacktests(luaD_reallocstack(L, s_used));\n}\n\n\nstatic void traversestack (global_State *g, lua_State *l) {\n  StkId o, lim;\n  CallInfo *ci;\n  markvalue(g, gt(l));\n  lim = l->top;\n  for (ci = l->base_ci; ci <= l->ci; ci++) {\n    lua_assert(ci->top <= l->stack_last);\n    if (lim < ci->top) lim = ci->top;\n  }\n  for (o = l->stack; o < l->top; o++)\n    markvalue(g, o);\n  for (; o <= lim; o++)\n    setnilvalue(o);\n  checkstacksizes(l, lim);\n}\n\n\n/*\n** traverse one gray object, turning it to black.\n** Returns `quantity' traversed.\n*/\nstatic l_mem propagatemark (global_State *g) {\n  GCObject *o = g->gray;\n  lua_assert(isgray(o));\n  gray2black(o);\n  switch (o->gch.tt) {\n    case LUA_TTABLE: {\n      Table *h = gco2h(o);\n      g->gray = h->gclist;\n      if (traversetable(g, h))  /* table is weak? */\n        black2gray(o);  /* keep it gray */\n      return sizeof(Table) + sizeof(TValue) * h->sizearray +\n                             sizeof(Node) * sizenode(h);\n    }\n    case LUA_TFUNCTION: {\n      Closure *cl = gco2cl(o);\n      g->gray = cl->c.gclist;\n      traverseclosure(g, cl);\n      return (cl->c.isC) ? sizeCclosure(cl->c.nupvalues) :\n                           sizeLclosure(cl->l.nupvalues);\n    }\n    case LUA_TTHREAD: {\n      lua_State *th = gco2th(o);\n      g->gray = th->gclist;\n      th->gclist = g->grayagain;\n      g->grayagain = o;\n      black2gray(o);\n      traversestack(g, th);\n      return sizeof(lua_State) + sizeof(TValue) * th->stacksize +\n                                 sizeof(CallInfo) * th->size_ci;\n    }\n    case LUA_TPROTO: {\n      Proto *p = gco2p(o);\n      g->gray = p->gclist;\n      traverseproto(g, p);\n      return sizeof(Proto) + sizeof(Instruction) * p->sizecode +\n                             sizeof(Proto *) * p->sizep +\n                             sizeof(TValue) * p->sizek + \n                             sizeof(int) * p->sizelineinfo +\n                             sizeof(LocVar) * p->sizelocvars +\n                             sizeof(TString *) * p->sizeupvalues;\n    }\n    default: lua_assert(0); return 0;\n  }\n}\n\n\nstatic size_t propagateall (global_State *g) {\n  size_t m = 0;\n  while (g->gray) m += propagatemark(g);\n  return m;\n}\n\n\n/*\n** The next function tells whether a key or value can be cleared from\n** a weak table. Non-collectable objects are never removed from weak\n** tables. Strings behave as `values', so are never removed too. for\n** other objects: if really collected, cannot keep them; for userdata\n** being finalized, keep them in keys, but not in values\n*/\nstatic int iscleared (const TValue *o, int iskey) {\n  if (!iscollectable(o)) return 0;\n  if (ttisstring(o)) {\n    stringmark(rawtsvalue(o));  /* strings are `values', so are never weak */\n    return 0;\n  }\n  return iswhite(gcvalue(o)) ||\n    (ttisuserdata(o) && (!iskey && isfinalized(uvalue(o))));\n}\n\n\n/*\n** clear collected entries from weaktables\n*/\nstatic void cleartable (GCObject *l) {\n  while (l) {\n    Table *h = gco2h(l);\n    int i = h->sizearray;\n    lua_assert(testbit(h->marked, VALUEWEAKBIT) ||\n               testbit(h->marked, KEYWEAKBIT));\n    if (testbit(h->marked, VALUEWEAKBIT)) {\n      while (i--) {\n        TValue *o = &h->array[i];\n        if (iscleared(o, 0))  /* value was collected? */\n          setnilvalue(o);  /* remove value */\n      }\n    }\n    i = sizenode(h);\n    while (i--) {\n      Node *n = gnode(h, i);\n      if (!ttisnil(gval(n)) &&  /* non-empty entry? */\n          (iscleared(key2tval(n), 1) || iscleared(gval(n), 0))) {\n        setnilvalue(gval(n));  /* remove value ... */\n        removeentry(n);  /* remove entry from table */\n      }\n    }\n    l = h->gclist;\n  }\n}\n\n\nstatic void freeobj (lua_State *L, GCObject *o) {\n  switch (o->gch.tt) {\n    case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break;\n    case LUA_TFUNCTION: luaF_freeclosure(L, gco2cl(o)); break;\n    case LUA_TUPVAL: luaF_freeupval(L, gco2uv(o)); break;\n    case LUA_TTABLE: luaH_free(L, gco2h(o)); break;\n    case LUA_TTHREAD: {\n      lua_assert(gco2th(o) != L && gco2th(o) != G(L)->mainthread);\n      luaE_freethread(L, gco2th(o));\n      break;\n    }\n    case LUA_TSTRING: {\n      G(L)->strt.nuse--;\n      luaM_freemem(L, o, sizestring(gco2ts(o)));\n      break;\n    }\n    case LUA_TUSERDATA: {\n      luaM_freemem(L, o, sizeudata(gco2u(o)));\n      break;\n    }\n    default: lua_assert(0);\n  }\n}\n\n\n\n#define sweepwholelist(L,p)\tsweeplist(L,p,MAX_LUMEM)\n\n\nstatic GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) {\n  GCObject *curr;\n  global_State *g = G(L);\n  int deadmask = otherwhite(g);\n  while ((curr = *p) != NULL && count-- > 0) {\n    if (curr->gch.tt == LUA_TTHREAD)  /* sweep open upvalues of each thread */\n      sweepwholelist(L, &gco2th(curr)->openupval);\n    if ((curr->gch.marked ^ WHITEBITS) & deadmask) {  /* not dead? */\n      lua_assert(!isdead(g, curr) || testbit(curr->gch.marked, FIXEDBIT));\n      makewhite(g, curr);  /* make it white (for next cycle) */\n      p = &curr->gch.next;\n    }\n    else {  /* must erase `curr' */\n      lua_assert(isdead(g, curr) || deadmask == bitmask(SFIXEDBIT));\n      *p = curr->gch.next;\n      if (curr == g->rootgc)  /* is the first element of the list? */\n        g->rootgc = curr->gch.next;  /* adjust first */\n      freeobj(L, curr);\n    }\n  }\n  return p;\n}\n\n\nstatic void checkSizes (lua_State *L) {\n  global_State *g = G(L);\n  /* check size of string hash */\n  if (g->strt.nuse < cast(lu_int32, g->strt.size/4) &&\n      g->strt.size > MINSTRTABSIZE*2)\n    luaS_resize(L, g->strt.size/2);  /* table is too big */\n  /* check size of buffer */\n  if (luaZ_sizebuffer(&g->buff) > LUA_MINBUFFER*2) {  /* buffer too big? */\n    size_t newsize = luaZ_sizebuffer(&g->buff) / 2;\n    luaZ_resizebuffer(L, &g->buff, newsize);\n  }\n}\n\n\nstatic void GCTM (lua_State *L) {\n  global_State *g = G(L);\n  GCObject *o = g->tmudata->gch.next;  /* get first element */\n  Udata *udata = rawgco2u(o);\n  const TValue *tm;\n  /* remove udata from `tmudata' */\n  if (o == g->tmudata)  /* last element? */\n    g->tmudata = NULL;\n  else\n    g->tmudata->gch.next = udata->uv.next;\n  udata->uv.next = g->mainthread->next;  /* return it to `root' list */\n  g->mainthread->next = o;\n  makewhite(g, o);\n  tm = fasttm(L, udata->uv.metatable, TM_GC);\n  if (tm != NULL) {\n    lu_byte oldah = L->allowhook;\n    lu_mem oldt = g->GCthreshold;\n    L->allowhook = 0;  /* stop debug hooks during GC tag method */\n    g->GCthreshold = 2*g->totalbytes;  /* avoid GC steps */\n    setobj2s(L, L->top, tm);\n    setuvalue(L, L->top+1, udata);\n    L->top += 2;\n    luaD_call(L, L->top - 2, 0);\n    L->allowhook = oldah;  /* restore hooks */\n    g->GCthreshold = oldt;  /* restore threshold */\n  }\n}\n\n\n/*\n** Call all GC tag methods\n*/\nvoid luaC_callGCTM (lua_State *L) {\n  while (G(L)->tmudata)\n    GCTM(L);\n}\n\n\nvoid luaC_freeall (lua_State *L) {\n  global_State *g = G(L);\n  int i;\n  g->currentwhite = WHITEBITS | bitmask(SFIXEDBIT);  /* mask to collect all elements */\n  sweepwholelist(L, &g->rootgc);\n  for (i = 0; i < g->strt.size; i++)  /* free all string lists */\n    sweepwholelist(L, &g->strt.hash[i]);\n}\n\n\nstatic void markmt (global_State *g) {\n  int i;\n  for (i=0; i<NUM_TAGS; i++)\n    if (g->mt[i]) markobject(g, g->mt[i]);\n}\n\n\n/* mark root set */\nstatic void markroot (lua_State *L) {\n  global_State *g = G(L);\n  g->gray = NULL;\n  g->grayagain = NULL;\n  g->weak = NULL;\n  markobject(g, g->mainthread);\n  /* make global table be traversed before main stack */\n  markvalue(g, gt(g->mainthread));\n  markvalue(g, registry(L));\n  markmt(g);\n  g->gcstate = GCSpropagate;\n}\n\n\nstatic void remarkupvals (global_State *g) {\n  UpVal *uv;\n  for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) {\n    lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv);\n    if (isgray(obj2gco(uv)))\n      markvalue(g, uv->v);\n  }\n}\n\n\nstatic void atomic (lua_State *L) {\n  global_State *g = G(L);\n  size_t udsize;  /* total size of userdata to be finalized */\n  /* remark occasional upvalues of (maybe) dead threads */\n  remarkupvals(g);\n  /* traverse objects cautch by write barrier and by 'remarkupvals' */\n  propagateall(g);\n  /* remark weak tables */\n  g->gray = g->weak;\n  g->weak = NULL;\n  lua_assert(!iswhite(obj2gco(g->mainthread)));\n  markobject(g, L);  /* mark running thread */\n  markmt(g);  /* mark basic metatables (again) */\n  propagateall(g);\n  /* remark gray again */\n  g->gray = g->grayagain;\n  g->grayagain = NULL;\n  propagateall(g);\n  udsize = luaC_separateudata(L, 0);  /* separate userdata to be finalized */\n  marktmu(g);  /* mark `preserved' userdata */\n  udsize += propagateall(g);  /* remark, to propagate `preserveness' */\n  cleartable(g->weak);  /* remove collected objects from weak tables */\n  /* flip current white */\n  g->currentwhite = cast_byte(otherwhite(g));\n  g->sweepstrgc = 0;\n  g->sweepgc = &g->rootgc;\n  g->gcstate = GCSsweepstring;\n  g->estimate = g->totalbytes - udsize;  /* first estimate */\n}\n\n\nstatic l_mem singlestep (lua_State *L) {\n  global_State *g = G(L);\n  /*lua_checkmemory(L);*/\n  switch (g->gcstate) {\n    case GCSpause: {\n      markroot(L);  /* start a new collection */\n      return 0;\n    }\n    case GCSpropagate: {\n      if (g->gray)\n        return propagatemark(g);\n      else {  /* no more `gray' objects */\n        atomic(L);  /* finish mark phase */\n        return 0;\n      }\n    }\n    case GCSsweepstring: {\n      lu_mem old = g->totalbytes;\n      sweepwholelist(L, &g->strt.hash[g->sweepstrgc++]);\n      if (g->sweepstrgc >= g->strt.size)  /* nothing more to sweep? */\n        g->gcstate = GCSsweep;  /* end sweep-string phase */\n      lua_assert(old >= g->totalbytes);\n      g->estimate -= old - g->totalbytes;\n      return GCSWEEPCOST;\n    }\n    case GCSsweep: {\n      lu_mem old = g->totalbytes;\n      g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX);\n      if (*g->sweepgc == NULL) {  /* nothing more to sweep? */\n        checkSizes(L);\n        g->gcstate = GCSfinalize;  /* end sweep phase */\n      }\n      lua_assert(old >= g->totalbytes);\n      g->estimate -= old - g->totalbytes;\n      return GCSWEEPMAX*GCSWEEPCOST;\n    }\n    case GCSfinalize: {\n      if (g->tmudata) {\n        GCTM(L);\n        if (g->estimate > GCFINALIZECOST)\n          g->estimate -= GCFINALIZECOST;\n        return GCFINALIZECOST;\n      }\n      else {\n        g->gcstate = GCSpause;  /* end collection */\n        g->gcdept = 0;\n        return 0;\n      }\n    }\n    default: lua_assert(0); return 0;\n  }\n}\n\n\nvoid luaC_step (lua_State *L) {\n  global_State *g = G(L);\n  l_mem lim = (GCSTEPSIZE/100) * g->gcstepmul;\n  if (lim == 0)\n    lim = (MAX_LUMEM-1)/2;  /* no limit */\n  g->gcdept += g->totalbytes - g->GCthreshold;\n  do {\n    lim -= singlestep(L);\n    if (g->gcstate == GCSpause)\n      break;\n  } while (lim > 0);\n  if (g->gcstate != GCSpause) {\n    if (g->gcdept < GCSTEPSIZE)\n      g->GCthreshold = g->totalbytes + GCSTEPSIZE;  /* - lim/g->gcstepmul;*/\n    else {\n      g->gcdept -= GCSTEPSIZE;\n      g->GCthreshold = g->totalbytes;\n    }\n  }\n  else {\n    setthreshold(g);\n  }\n}\n\n\nvoid luaC_fullgc (lua_State *L) {\n  global_State *g = G(L);\n  if (g->gcstate <= GCSpropagate) {\n    /* reset sweep marks to sweep all elements (returning them to white) */\n    g->sweepstrgc = 0;\n    g->sweepgc = &g->rootgc;\n    /* reset other collector lists */\n    g->gray = NULL;\n    g->grayagain = NULL;\n    g->weak = NULL;\n    g->gcstate = GCSsweepstring;\n  }\n  lua_assert(g->gcstate != GCSpause && g->gcstate != GCSpropagate);\n  /* finish any pending sweep phase */\n  while (g->gcstate != GCSfinalize) {\n    lua_assert(g->gcstate == GCSsweepstring || g->gcstate == GCSsweep);\n    singlestep(L);\n  }\n  markroot(L);\n  while (g->gcstate != GCSpause) {\n    singlestep(L);\n  }\n  setthreshold(g);\n}\n\n\nvoid luaC_barrierf (lua_State *L, GCObject *o, GCObject *v) {\n  global_State *g = G(L);\n  lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o));\n  lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause);\n  lua_assert(ttype(&o->gch) != LUA_TTABLE);\n  /* must keep invariant? */\n  if (g->gcstate == GCSpropagate)\n    reallymarkobject(g, v);  /* restore invariant */\n  else  /* don't mind */\n    makewhite(g, o);  /* mark as white just to avoid other barriers */\n}\n\n\nvoid luaC_barrierback (lua_State *L, Table *t) {\n  global_State *g = G(L);\n  GCObject *o = obj2gco(t);\n  lua_assert(isblack(o) && !isdead(g, o));\n  lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause);\n  black2gray(o);  /* make table gray (again) */\n  t->gclist = g->grayagain;\n  g->grayagain = o;\n}\n\n\nvoid luaC_link (lua_State *L, GCObject *o, lu_byte tt) {\n  global_State *g = G(L);\n  o->gch.next = g->rootgc;\n  g->rootgc = o;\n  o->gch.marked = luaC_white(g);\n  o->gch.tt = tt;\n}\n\n\nvoid luaC_linkupval (lua_State *L, UpVal *uv) {\n  global_State *g = G(L);\n  GCObject *o = obj2gco(uv);\n  o->gch.next = g->rootgc;  /* link upvalue into `rootgc' list */\n  g->rootgc = o;\n  if (isgray(o)) { \n    if (g->gcstate == GCSpropagate) {\n      gray2black(o);  /* closed upvalues need barrier */\n      luaC_barrier(L, uv, uv->v);\n    }\n    else {  /* sweep phase: sweep it (turning it into white) */\n      makewhite(g, o);\n      lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause);\n    }\n  }\n}\n\n"
  },
  {
    "path": "deps/lua/src/lgc.h",
    "content": "/*\n** $Id: lgc.h,v 2.15.1.1 2007/12/27 13:02:25 roberto Exp $\n** Garbage Collector\n** See Copyright Notice in lua.h\n*/\n\n#ifndef lgc_h\n#define lgc_h\n\n\n#include \"lobject.h\"\n\n\n/*\n** Possible states of the Garbage Collector\n*/\n#define GCSpause\t0\n#define GCSpropagate\t1\n#define GCSsweepstring\t2\n#define GCSsweep\t3\n#define GCSfinalize\t4\n\n\n/*\n** some userful bit tricks\n*/\n#define resetbits(x,m)\t((x) &= cast(lu_byte, ~(m)))\n#define setbits(x,m)\t((x) |= (m))\n#define testbits(x,m)\t((x) & (m))\n#define bitmask(b)\t(1<<(b))\n#define bit2mask(b1,b2)\t(bitmask(b1) | bitmask(b2))\n#define l_setbit(x,b)\tsetbits(x, bitmask(b))\n#define resetbit(x,b)\tresetbits(x, bitmask(b))\n#define testbit(x,b)\ttestbits(x, bitmask(b))\n#define set2bits(x,b1,b2)\tsetbits(x, (bit2mask(b1, b2)))\n#define reset2bits(x,b1,b2)\tresetbits(x, (bit2mask(b1, b2)))\n#define test2bits(x,b1,b2)\ttestbits(x, (bit2mask(b1, b2)))\n\n\n\n/*\n** Layout for bit use in `marked' field:\n** bit 0 - object is white (type 0)\n** bit 1 - object is white (type 1)\n** bit 2 - object is black\n** bit 3 - for userdata: has been finalized\n** bit 3 - for tables: has weak keys\n** bit 4 - for tables: has weak values\n** bit 5 - object is fixed (should not be collected)\n** bit 6 - object is \"super\" fixed (only the main thread)\n*/\n\n\n#define WHITE0BIT\t0\n#define WHITE1BIT\t1\n#define BLACKBIT\t2\n#define FINALIZEDBIT\t3\n#define KEYWEAKBIT\t3\n#define VALUEWEAKBIT\t4\n#define FIXEDBIT\t5\n#define SFIXEDBIT\t6\n#define WHITEBITS\tbit2mask(WHITE0BIT, WHITE1BIT)\n\n\n#define iswhite(x)      test2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT)\n#define isblack(x)      testbit((x)->gch.marked, BLACKBIT)\n#define isgray(x)\t(!isblack(x) && !iswhite(x))\n\n#define otherwhite(g)\t(g->currentwhite ^ WHITEBITS)\n#define isdead(g,v)\t((v)->gch.marked & otherwhite(g) & WHITEBITS)\n\n#define changewhite(x)\t((x)->gch.marked ^= WHITEBITS)\n#define gray2black(x)\tl_setbit((x)->gch.marked, BLACKBIT)\n\n#define valiswhite(x)\t(iscollectable(x) && iswhite(gcvalue(x)))\n\n#define luaC_white(g)\tcast(lu_byte, (g)->currentwhite & WHITEBITS)\n\n\n#define luaC_checkGC(L) { \\\n  condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); \\\n  if (G(L)->totalbytes >= G(L)->GCthreshold) \\\n\tluaC_step(L); }\n\n\n#define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p)))  \\\n\tluaC_barrierf(L,obj2gco(p),gcvalue(v)); }\n\n#define luaC_barriert(L,t,v) { if (valiswhite(v) && isblack(obj2gco(t)))  \\\n\tluaC_barrierback(L,t); }\n\n#define luaC_objbarrier(L,p,o)  \\\n\t{ if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \\\n\t\tluaC_barrierf(L,obj2gco(p),obj2gco(o)); }\n\n#define luaC_objbarriert(L,t,o)  \\\n   { if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) luaC_barrierback(L,t); }\n\nLUAI_FUNC size_t luaC_separateudata (lua_State *L, int all);\nLUAI_FUNC void luaC_callGCTM (lua_State *L);\nLUAI_FUNC void luaC_freeall (lua_State *L);\nLUAI_FUNC void luaC_step (lua_State *L);\nLUAI_FUNC void luaC_fullgc (lua_State *L);\nLUAI_FUNC void luaC_link (lua_State *L, GCObject *o, lu_byte tt);\nLUAI_FUNC void luaC_linkupval (lua_State *L, UpVal *uv);\nLUAI_FUNC void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v);\nLUAI_FUNC void luaC_barrierback (lua_State *L, Table *t);\n\n\n#endif\n"
  },
  {
    "path": "deps/lua/src/linit.c",
    "content": "/*\n** $Id: linit.c,v 1.14.1.1 2007/12/27 13:02:25 roberto Exp $\n** Initialization of libraries for lua.c\n** See Copyright Notice in lua.h\n*/\n\n\n#define linit_c\n#define LUA_LIB\n\n#include \"lua.h\"\n\n#include \"lualib.h\"\n#include \"lauxlib.h\"\n\n\nstatic const luaL_Reg lualibs[] = {\n  {\"\", luaopen_base},\n  {LUA_LOADLIBNAME, luaopen_package},\n  {LUA_TABLIBNAME, luaopen_table},\n  {LUA_IOLIBNAME, luaopen_io},\n  {LUA_OSLIBNAME, luaopen_os},\n  {LUA_STRLIBNAME, luaopen_string},\n  {LUA_MATHLIBNAME, luaopen_math},\n  {LUA_DBLIBNAME, luaopen_debug},\n  {NULL, NULL}\n};\n\n\nLUALIB_API void luaL_openlibs (lua_State *L) {\n  const luaL_Reg *lib = lualibs;\n  for (; lib->func; lib++) {\n    lua_pushcfunction(L, lib->func);\n    lua_pushstring(L, lib->name);\n    lua_call(L, 1, 0);\n  }\n}\n\n"
  },
  {
    "path": "deps/lua/src/liolib.c",
    "content": "/*\n** $Id: liolib.c,v 2.73.1.4 2010/05/14 15:33:51 roberto Exp $\n** Standard I/O (and system) library\n** See Copyright Notice in lua.h\n*/\n\n\n#include <errno.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#define liolib_c\n#define LUA_LIB\n\n#include \"lua.h\"\n\n#include \"lauxlib.h\"\n#include \"lualib.h\"\n\n\n\n#define IO_INPUT\t1\n#define IO_OUTPUT\t2\n\n\nstatic const char *const fnames[] = {\"input\", \"output\"};\n\n\nstatic int pushresult (lua_State *L, int i, const char *filename) {\n  int en = errno;  /* calls to Lua API may change this value */\n  if (i) {\n    lua_pushboolean(L, 1);\n    return 1;\n  }\n  else {\n    lua_pushnil(L);\n    if (filename)\n      lua_pushfstring(L, \"%s: %s\", filename, strerror(en));\n    else\n      lua_pushfstring(L, \"%s\", strerror(en));\n    lua_pushinteger(L, en);\n    return 3;\n  }\n}\n\n\nstatic void fileerror (lua_State *L, int arg, const char *filename) {\n  lua_pushfstring(L, \"%s: %s\", filename, strerror(errno));\n  luaL_argerror(L, arg, lua_tostring(L, -1));\n}\n\n\n#define tofilep(L)\t((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE))\n\n\nstatic int io_type (lua_State *L) {\n  void *ud;\n  luaL_checkany(L, 1);\n  ud = lua_touserdata(L, 1);\n  lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE);\n  if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1))\n    lua_pushnil(L);  /* not a file */\n  else if (*((FILE **)ud) == NULL)\n    lua_pushliteral(L, \"closed file\");\n  else\n    lua_pushliteral(L, \"file\");\n  return 1;\n}\n\n\nstatic FILE *tofile (lua_State *L) {\n  FILE **f = tofilep(L);\n  if (*f == NULL)\n    luaL_error(L, \"attempt to use a closed file\");\n  return *f;\n}\n\n\n\n/*\n** When creating file handles, always creates a `closed' file handle\n** before opening the actual file; so, if there is a memory error, the\n** file is not left opened.\n*/\nstatic FILE **newfile (lua_State *L) {\n  FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *));\n  *pf = NULL;  /* file handle is currently `closed' */\n  luaL_getmetatable(L, LUA_FILEHANDLE);\n  lua_setmetatable(L, -2);\n  return pf;\n}\n\n\n/*\n** function to (not) close the standard files stdin, stdout, and stderr\n*/\nstatic int io_noclose (lua_State *L) {\n  lua_pushnil(L);\n  lua_pushliteral(L, \"cannot close standard file\");\n  return 2;\n}\n\n\n/*\n** function to close 'popen' files\n*/\nstatic int io_pclose (lua_State *L) {\n  FILE **p = tofilep(L);\n  int ok = lua_pclose(L, *p);\n  *p = NULL;\n  return pushresult(L, ok, NULL);\n}\n\n\n/*\n** function to close regular files\n*/\nstatic int io_fclose (lua_State *L) {\n  FILE **p = tofilep(L);\n  int ok = (fclose(*p) == 0);\n  *p = NULL;\n  return pushresult(L, ok, NULL);\n}\n\n\nstatic int aux_close (lua_State *L) {\n  lua_getfenv(L, 1);\n  lua_getfield(L, -1, \"__close\");\n  return (lua_tocfunction(L, -1))(L);\n}\n\n\nstatic int io_close (lua_State *L) {\n  if (lua_isnone(L, 1))\n    lua_rawgeti(L, LUA_ENVIRONINDEX, IO_OUTPUT);\n  tofile(L);  /* make sure argument is a file */\n  return aux_close(L);\n}\n\n\nstatic int io_gc (lua_State *L) {\n  FILE *f = *tofilep(L);\n  /* ignore closed files */\n  if (f != NULL)\n    aux_close(L);\n  return 0;\n}\n\n\nstatic int io_tostring (lua_State *L) {\n  FILE *f = *tofilep(L);\n  if (f == NULL)\n    lua_pushliteral(L, \"file (closed)\");\n  else\n    lua_pushfstring(L, \"file (%p)\", f);\n  return 1;\n}\n\n\nstatic int io_open (lua_State *L) {\n  const char *filename = luaL_checkstring(L, 1);\n  const char *mode = luaL_optstring(L, 2, \"r\");\n  FILE **pf = newfile(L);\n  *pf = fopen(filename, mode);\n  return (*pf == NULL) ? pushresult(L, 0, filename) : 1;\n}\n\n\n/*\n** this function has a separated environment, which defines the\n** correct __close for 'popen' files\n*/\nstatic int io_popen (lua_State *L) {\n  const char *filename = luaL_checkstring(L, 1);\n  const char *mode = luaL_optstring(L, 2, \"r\");\n  FILE **pf = newfile(L);\n  *pf = lua_popen(L, filename, mode);\n  return (*pf == NULL) ? pushresult(L, 0, filename) : 1;\n}\n\n\nstatic int io_tmpfile (lua_State *L) {\n  FILE **pf = newfile(L);\n  *pf = tmpfile();\n  return (*pf == NULL) ? pushresult(L, 0, NULL) : 1;\n}\n\n\nstatic FILE *getiofile (lua_State *L, int findex) {\n  FILE *f;\n  lua_rawgeti(L, LUA_ENVIRONINDEX, findex);\n  f = *(FILE **)lua_touserdata(L, -1);\n  if (f == NULL)\n    luaL_error(L, \"standard %s file is closed\", fnames[findex - 1]);\n  return f;\n}\n\n\nstatic int g_iofile (lua_State *L, int f, const char *mode) {\n  if (!lua_isnoneornil(L, 1)) {\n    const char *filename = lua_tostring(L, 1);\n    if (filename) {\n      FILE **pf = newfile(L);\n      *pf = fopen(filename, mode);\n      if (*pf == NULL)\n        fileerror(L, 1, filename);\n    }\n    else {\n      tofile(L);  /* check that it's a valid file handle */\n      lua_pushvalue(L, 1);\n    }\n    lua_rawseti(L, LUA_ENVIRONINDEX, f);\n  }\n  /* return current value */\n  lua_rawgeti(L, LUA_ENVIRONINDEX, f);\n  return 1;\n}\n\n\nstatic int io_input (lua_State *L) {\n  return g_iofile(L, IO_INPUT, \"r\");\n}\n\n\nstatic int io_output (lua_State *L) {\n  return g_iofile(L, IO_OUTPUT, \"w\");\n}\n\n\nstatic int io_readline (lua_State *L);\n\n\nstatic void aux_lines (lua_State *L, int idx, int toclose) {\n  lua_pushvalue(L, idx);\n  lua_pushboolean(L, toclose);  /* close/not close file when finished */\n  lua_pushcclosure(L, io_readline, 2);\n}\n\n\nstatic int f_lines (lua_State *L) {\n  tofile(L);  /* check that it's a valid file handle */\n  aux_lines(L, 1, 0);\n  return 1;\n}\n\n\nstatic int io_lines (lua_State *L) {\n  if (lua_isnoneornil(L, 1)) {  /* no arguments? */\n    /* will iterate over default input */\n    lua_rawgeti(L, LUA_ENVIRONINDEX, IO_INPUT);\n    return f_lines(L);\n  }\n  else {\n    const char *filename = luaL_checkstring(L, 1);\n    FILE **pf = newfile(L);\n    *pf = fopen(filename, \"r\");\n    if (*pf == NULL)\n      fileerror(L, 1, filename);\n    aux_lines(L, lua_gettop(L), 1);\n    return 1;\n  }\n}\n\n\n/*\n** {======================================================\n** READ\n** =======================================================\n*/\n\n\nstatic int read_number (lua_State *L, FILE *f) {\n  lua_Number d;\n  if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) {\n    lua_pushnumber(L, d);\n    return 1;\n  }\n  else {\n    lua_pushnil(L);  /* \"result\" to be removed */\n    return 0;  /* read fails */\n  }\n}\n\n\nstatic int test_eof (lua_State *L, FILE *f) {\n  int c = getc(f);\n  ungetc(c, f);\n  lua_pushlstring(L, NULL, 0);\n  return (c != EOF);\n}\n\n\nstatic int read_line (lua_State *L, FILE *f) {\n  luaL_Buffer b;\n  luaL_buffinit(L, &b);\n  for (;;) {\n    size_t l;\n    char *p = luaL_prepbuffer(&b);\n    if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) {  /* eof? */\n      luaL_pushresult(&b);  /* close buffer */\n      return (lua_objlen(L, -1) > 0);  /* check whether read something */\n    }\n    l = strlen(p);\n    if (l == 0 || p[l-1] != '\\n')\n      luaL_addsize(&b, l);\n    else {\n      luaL_addsize(&b, l - 1);  /* do not include `eol' */\n      luaL_pushresult(&b);  /* close buffer */\n      return 1;  /* read at least an `eol' */\n    }\n  }\n}\n\n\nstatic int read_chars (lua_State *L, FILE *f, size_t n) {\n  size_t rlen;  /* how much to read */\n  size_t nr;  /* number of chars actually read */\n  luaL_Buffer b;\n  luaL_buffinit(L, &b);\n  rlen = LUAL_BUFFERSIZE;  /* try to read that much each time */\n  do {\n    char *p = luaL_prepbuffer(&b);\n    if (rlen > n) rlen = n;  /* cannot read more than asked */\n    nr = fread(p, sizeof(char), rlen, f);\n    luaL_addsize(&b, nr);\n    n -= nr;  /* still have to read `n' chars */\n  } while (n > 0 && nr == rlen);  /* until end of count or eof */\n  luaL_pushresult(&b);  /* close buffer */\n  return (n == 0 || lua_objlen(L, -1) > 0);\n}\n\n\nstatic int g_read (lua_State *L, FILE *f, int first) {\n  int nargs = lua_gettop(L) - 1;\n  int success;\n  int n;\n  clearerr(f);\n  if (nargs == 0) {  /* no arguments? */\n    success = read_line(L, f);\n    n = first+1;  /* to return 1 result */\n  }\n  else {  /* ensure stack space for all results and for auxlib's buffer */\n    luaL_checkstack(L, nargs+LUA_MINSTACK, \"too many arguments\");\n    success = 1;\n    for (n = first; nargs-- && success; n++) {\n      if (lua_type(L, n) == LUA_TNUMBER) {\n        size_t l = (size_t)lua_tointeger(L, n);\n        success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);\n      }\n      else {\n        const char *p = lua_tostring(L, n);\n        luaL_argcheck(L, p && p[0] == '*', n, \"invalid option\");\n        switch (p[1]) {\n          case 'n':  /* number */\n            success = read_number(L, f);\n            break;\n          case 'l':  /* line */\n            success = read_line(L, f);\n            break;\n          case 'a':  /* file */\n            read_chars(L, f, ~((size_t)0));  /* read MAX_SIZE_T chars */\n            success = 1; /* always success */\n            break;\n          default:\n            return luaL_argerror(L, n, \"invalid format\");\n        }\n      }\n    }\n  }\n  if (ferror(f))\n    return pushresult(L, 0, NULL);\n  if (!success) {\n    lua_pop(L, 1);  /* remove last result */\n    lua_pushnil(L);  /* push nil instead */\n  }\n  return n - first;\n}\n\n\nstatic int io_read (lua_State *L) {\n  return g_read(L, getiofile(L, IO_INPUT), 1);\n}\n\n\nstatic int f_read (lua_State *L) {\n  return g_read(L, tofile(L), 2);\n}\n\n\nstatic int io_readline (lua_State *L) {\n  FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(1));\n  int sucess;\n  if (f == NULL)  /* file is already closed? */\n    luaL_error(L, \"file is already closed\");\n  sucess = read_line(L, f);\n  if (ferror(f))\n    return luaL_error(L, \"%s\", strerror(errno));\n  if (sucess) return 1;\n  else {  /* EOF */\n    if (lua_toboolean(L, lua_upvalueindex(2))) {  /* generator created file? */\n      lua_settop(L, 0);\n      lua_pushvalue(L, lua_upvalueindex(1));\n      aux_close(L);  /* close it */\n    }\n    return 0;\n  }\n}\n\n/* }====================================================== */\n\n\nstatic int g_write (lua_State *L, FILE *f, int arg) {\n  int nargs = lua_gettop(L) - 1;\n  int status = 1;\n  for (; nargs--; arg++) {\n    if (lua_type(L, arg) == LUA_TNUMBER) {\n      /* optimization: could be done exactly as for strings */\n      status = status &&\n          fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;\n    }\n    else {\n      size_t l;\n      const char *s = luaL_checklstring(L, arg, &l);\n      status = status && (fwrite(s, sizeof(char), l, f) == l);\n    }\n  }\n  return pushresult(L, status, NULL);\n}\n\n\nstatic int io_write (lua_State *L) {\n  return g_write(L, getiofile(L, IO_OUTPUT), 1);\n}\n\n\nstatic int f_write (lua_State *L) {\n  return g_write(L, tofile(L), 2);\n}\n\n\nstatic int f_seek (lua_State *L) {\n  static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};\n  static const char *const modenames[] = {\"set\", \"cur\", \"end\", NULL};\n  FILE *f = tofile(L);\n  int op = luaL_checkoption(L, 2, \"cur\", modenames);\n  long offset = luaL_optlong(L, 3, 0);\n  op = fseek(f, offset, mode[op]);\n  if (op)\n    return pushresult(L, 0, NULL);  /* error */\n  else {\n    lua_pushinteger(L, ftell(f));\n    return 1;\n  }\n}\n\n\nstatic int f_setvbuf (lua_State *L) {\n  static const int mode[] = {_IONBF, _IOFBF, _IOLBF};\n  static const char *const modenames[] = {\"no\", \"full\", \"line\", NULL};\n  FILE *f = tofile(L);\n  int op = luaL_checkoption(L, 2, NULL, modenames);\n  lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);\n  int res = setvbuf(f, NULL, mode[op], sz);\n  return pushresult(L, res == 0, NULL);\n}\n\n\n\nstatic int io_flush (lua_State *L) {\n  return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);\n}\n\n\nstatic int f_flush (lua_State *L) {\n  return pushresult(L, fflush(tofile(L)) == 0, NULL);\n}\n\n\nstatic const luaL_Reg iolib[] = {\n  {\"close\", io_close},\n  {\"flush\", io_flush},\n  {\"input\", io_input},\n  {\"lines\", io_lines},\n  {\"open\", io_open},\n  {\"output\", io_output},\n  {\"popen\", io_popen},\n  {\"read\", io_read},\n  {\"tmpfile\", io_tmpfile},\n  {\"type\", io_type},\n  {\"write\", io_write},\n  {NULL, NULL}\n};\n\n\nstatic const luaL_Reg flib[] = {\n  {\"close\", io_close},\n  {\"flush\", f_flush},\n  {\"lines\", f_lines},\n  {\"read\", f_read},\n  {\"seek\", f_seek},\n  {\"setvbuf\", f_setvbuf},\n  {\"write\", f_write},\n  {\"__gc\", io_gc},\n  {\"__tostring\", io_tostring},\n  {NULL, NULL}\n};\n\n\nstatic void createmeta (lua_State *L) {\n  luaL_newmetatable(L, LUA_FILEHANDLE);  /* create metatable for file handles */\n  lua_pushvalue(L, -1);  /* push metatable */\n  lua_setfield(L, -2, \"__index\");  /* metatable.__index = metatable */\n  luaL_register(L, NULL, flib);  /* file methods */\n}\n\n\nstatic void createstdfile (lua_State *L, FILE *f, int k, const char *fname) {\n  *newfile(L) = f;\n  if (k > 0) {\n    lua_pushvalue(L, -1);\n    lua_rawseti(L, LUA_ENVIRONINDEX, k);\n  }\n  lua_pushvalue(L, -2);  /* copy environment */\n  lua_setfenv(L, -2);  /* set it */\n  lua_setfield(L, -3, fname);\n}\n\n\nstatic void newfenv (lua_State *L, lua_CFunction cls) {\n  lua_createtable(L, 0, 1);\n  lua_pushcfunction(L, cls);\n  lua_setfield(L, -2, \"__close\");\n}\n\n\nLUALIB_API int luaopen_io (lua_State *L) {\n  createmeta(L);\n  /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */\n  newfenv(L, io_fclose);\n  lua_replace(L, LUA_ENVIRONINDEX);\n  /* open library */\n  luaL_register(L, LUA_IOLIBNAME, iolib);\n  /* create (and set) default files */\n  newfenv(L, io_noclose);  /* close function for default files */\n  createstdfile(L, stdin, IO_INPUT, \"stdin\");\n  createstdfile(L, stdout, IO_OUTPUT, \"stdout\");\n  createstdfile(L, stderr, 0, \"stderr\");\n  lua_pop(L, 1);  /* pop environment for default files */\n  lua_getfield(L, -1, \"popen\");\n  newfenv(L, io_pclose);  /* create environment for 'popen' */\n  lua_setfenv(L, -2);  /* set fenv for 'popen' */\n  lua_pop(L, 1);  /* pop 'popen' */\n  return 1;\n}\n\n"
  },
  {
    "path": "deps/lua/src/llex.c",
    "content": "/*\n** $Id: llex.c,v 2.20.1.2 2009/11/23 14:58:22 roberto Exp $\n** Lexical Analyzer\n** See Copyright Notice in lua.h\n*/\n\n\n#include <ctype.h>\n#include <locale.h>\n#include <string.h>\n\n#define llex_c\n#define LUA_CORE\n\n#include \"lua.h\"\n\n#include \"ldo.h\"\n#include \"llex.h\"\n#include \"lobject.h\"\n#include \"lparser.h\"\n#include \"lstate.h\"\n#include \"lstring.h\"\n#include \"ltable.h\"\n#include \"lzio.h\"\n\n\n\n#define next(ls) (ls->current = zgetc(ls->z))\n\n\n\n\n#define currIsNewline(ls)\t(ls->current == '\\n' || ls->current == '\\r')\n\n\n/* ORDER RESERVED */\nconst char *const luaX_tokens [] = {\n    \"and\", \"break\", \"do\", \"else\", \"elseif\",\n    \"end\", \"false\", \"for\", \"function\", \"if\",\n    \"in\", \"local\", \"nil\", \"not\", \"or\", \"repeat\",\n    \"return\", \"then\", \"true\", \"until\", \"while\",\n    \"..\", \"...\", \"==\", \">=\", \"<=\", \"~=\",\n    \"<number>\", \"<name>\", \"<string>\", \"<eof>\",\n    NULL\n};\n\n\n#define save_and_next(ls) (save(ls, ls->current), next(ls))\n\n\nstatic void save (LexState *ls, int c) {\n  Mbuffer *b = ls->buff;\n  if (b->n + 1 > b->buffsize) {\n    size_t newsize;\n    if (b->buffsize >= MAX_SIZET/2)\n      luaX_lexerror(ls, \"lexical element too long\", 0);\n    newsize = b->buffsize * 2;\n    luaZ_resizebuffer(ls->L, b, newsize);\n  }\n  b->buffer[b->n++] = cast(char, c);\n}\n\n\nvoid luaX_init (lua_State *L) {\n  int i;\n  for (i=0; i<NUM_RESERVED; i++) {\n    TString *ts = luaS_new(L, luaX_tokens[i]);\n    luaS_fix(ts);  /* reserved words are never collected */\n    lua_assert(strlen(luaX_tokens[i])+1 <= TOKEN_LEN);\n    ts->tsv.reserved = cast_byte(i+1);  /* reserved word */\n  }\n}\n\n\n#define MAXSRC          80\n\n\nconst char *luaX_token2str (LexState *ls, int token) {\n  if (token < FIRST_RESERVED) {\n    lua_assert(token == cast(unsigned char, token));\n    return (iscntrl(token)) ? luaO_pushfstring(ls->L, \"char(%d)\", token) :\n                              luaO_pushfstring(ls->L, \"%c\", token);\n  }\n  else\n    return luaX_tokens[token-FIRST_RESERVED];\n}\n\n\nstatic const char *txtToken (LexState *ls, int token) {\n  switch (token) {\n    case TK_NAME:\n    case TK_STRING:\n    case TK_NUMBER:\n      save(ls, '\\0');\n      return luaZ_buffer(ls->buff);\n    default:\n      return luaX_token2str(ls, token);\n  }\n}\n\n\nvoid luaX_lexerror (LexState *ls, const char *msg, int token) {\n  char buff[MAXSRC];\n  luaO_chunkid(buff, getstr(ls->source), MAXSRC);\n  msg = luaO_pushfstring(ls->L, \"%s:%d: %s\", buff, ls->linenumber, msg);\n  if (token)\n    luaO_pushfstring(ls->L, \"%s near \" LUA_QS, msg, txtToken(ls, token));\n  luaD_throw(ls->L, LUA_ERRSYNTAX);\n}\n\n\nvoid luaX_syntaxerror (LexState *ls, const char *msg) {\n  luaX_lexerror(ls, msg, ls->t.token);\n}\n\n\nTString *luaX_newstring (LexState *ls, const char *str, size_t l) {\n  lua_State *L = ls->L;\n  TString *ts = luaS_newlstr(L, str, l);\n  TValue *o = luaH_setstr(L, ls->fs->h, ts);  /* entry for `str' */\n  if (ttisnil(o)) {\n    setbvalue(o, 1);  /* make sure `str' will not be collected */\n    luaC_checkGC(L);\n  }\n  return ts;\n}\n\n\nstatic void inclinenumber (LexState *ls) {\n  int old = ls->current;\n  lua_assert(currIsNewline(ls));\n  next(ls);  /* skip `\\n' or `\\r' */\n  if (currIsNewline(ls) && ls->current != old)\n    next(ls);  /* skip `\\n\\r' or `\\r\\n' */\n  if (++ls->linenumber >= MAX_INT)\n    luaX_syntaxerror(ls, \"chunk has too many lines\");\n}\n\n\nvoid luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) {\n  ls->decpoint = '.';\n  ls->L = L;\n  ls->lookahead.token = TK_EOS;  /* no look-ahead token */\n  ls->z = z;\n  ls->fs = NULL;\n  ls->linenumber = 1;\n  ls->lastline = 1;\n  ls->source = source;\n  luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER);  /* initialize buffer */\n  next(ls);  /* read first char */\n}\n\n\n\n/*\n** =======================================================\n** LEXICAL ANALYZER\n** =======================================================\n*/\n\n\n\nstatic int check_next (LexState *ls, const char *set) {\n  if (!strchr(set, ls->current))\n    return 0;\n  save_and_next(ls);\n  return 1;\n}\n\n\nstatic void buffreplace (LexState *ls, char from, char to) {\n  size_t n = luaZ_bufflen(ls->buff);\n  char *p = luaZ_buffer(ls->buff);\n  while (n--)\n    if (p[n] == from) p[n] = to;\n}\n\n\nstatic void trydecpoint (LexState *ls, SemInfo *seminfo) {\n  /* format error: try to update decimal point separator */\n  struct lconv *cv = localeconv();\n  char old = ls->decpoint;\n  ls->decpoint = (cv ? cv->decimal_point[0] : '.');\n  buffreplace(ls, old, ls->decpoint);  /* try updated decimal separator */\n  if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) {\n    /* format error with correct decimal point: no more options */\n    buffreplace(ls, ls->decpoint, '.');  /* undo change (for error message) */\n    luaX_lexerror(ls, \"malformed number\", TK_NUMBER);\n  }\n}\n\n\n/* LUA_NUMBER */\nstatic void read_numeral (LexState *ls, SemInfo *seminfo) {\n  lua_assert(isdigit(ls->current));\n  do {\n    save_and_next(ls);\n  } while (isdigit(ls->current) || ls->current == '.');\n  if (check_next(ls, \"Ee\"))  /* `E'? */\n    check_next(ls, \"+-\");  /* optional exponent sign */\n  while (isalnum(ls->current) || ls->current == '_')\n    save_and_next(ls);\n  save(ls, '\\0');\n  buffreplace(ls, '.', ls->decpoint);  /* follow locale for decimal point */\n  if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r))  /* format error? */\n    trydecpoint(ls, seminfo); /* try to update decimal point separator */\n}\n\n\nstatic int skip_sep (LexState *ls) {\n  int count = 0;\n  int s = ls->current;\n  lua_assert(s == '[' || s == ']');\n  save_and_next(ls);\n  while (ls->current == '=') {\n    save_and_next(ls);\n    count++;\n  }\n  return (ls->current == s) ? count : (-count) - 1;\n}\n\n\nstatic void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {\n  int cont = 0;\n  (void)(cont);  /* avoid warnings when `cont' is not used */\n  save_and_next(ls);  /* skip 2nd `[' */\n  if (currIsNewline(ls))  /* string starts with a newline? */\n    inclinenumber(ls);  /* skip it */\n  for (;;) {\n    switch (ls->current) {\n      case EOZ:\n        luaX_lexerror(ls, (seminfo) ? \"unfinished long string\" :\n                                   \"unfinished long comment\", TK_EOS);\n        break;  /* to avoid warnings */\n#if defined(LUA_COMPAT_LSTR)\n      case '[': {\n        if (skip_sep(ls) == sep) {\n          save_and_next(ls);  /* skip 2nd `[' */\n          cont++;\n#if LUA_COMPAT_LSTR == 1\n          if (sep == 0)\n            luaX_lexerror(ls, \"nesting of [[...]] is deprecated\", '[');\n#endif\n        }\n        break;\n      }\n#endif\n      case ']': {\n        if (skip_sep(ls) == sep) {\n          save_and_next(ls);  /* skip 2nd `]' */\n#if defined(LUA_COMPAT_LSTR) && LUA_COMPAT_LSTR == 2\n          cont--;\n          if (sep == 0 && cont >= 0) break;\n#endif\n          goto endloop;\n        }\n        break;\n      }\n      case '\\n':\n      case '\\r': {\n        save(ls, '\\n');\n        inclinenumber(ls);\n        if (!seminfo) luaZ_resetbuffer(ls->buff);  /* avoid wasting space */\n        break;\n      }\n      default: {\n        if (seminfo) save_and_next(ls);\n        else next(ls);\n      }\n    }\n  } endloop:\n  if (seminfo)\n    seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep),\n                                     luaZ_bufflen(ls->buff) - 2*(2 + sep));\n}\n\n\nstatic void read_string (LexState *ls, int del, SemInfo *seminfo) {\n  save_and_next(ls);\n  while (ls->current != del) {\n    switch (ls->current) {\n      case EOZ:\n        luaX_lexerror(ls, \"unfinished string\", TK_EOS);\n        continue;  /* to avoid warnings */\n      case '\\n':\n      case '\\r':\n        luaX_lexerror(ls, \"unfinished string\", TK_STRING);\n        continue;  /* to avoid warnings */\n      case '\\\\': {\n        int c;\n        next(ls);  /* do not save the `\\' */\n        switch (ls->current) {\n          case 'a': c = '\\a'; break;\n          case 'b': c = '\\b'; break;\n          case 'f': c = '\\f'; break;\n          case 'n': c = '\\n'; break;\n          case 'r': c = '\\r'; break;\n          case 't': c = '\\t'; break;\n          case 'v': c = '\\v'; break;\n          case '\\n':  /* go through */\n          case '\\r': save(ls, '\\n'); inclinenumber(ls); continue;\n          case EOZ: continue;  /* will raise an error next loop */\n          default: {\n            if (!isdigit(ls->current))\n              save_and_next(ls);  /* handles \\\\, \\\", \\', and \\? */\n            else {  /* \\xxx */\n              int i = 0;\n              c = 0;\n              do {\n                c = 10*c + (ls->current-'0');\n                next(ls);\n              } while (++i<3 && isdigit(ls->current));\n              if (c > UCHAR_MAX)\n                luaX_lexerror(ls, \"escape sequence too large\", TK_STRING);\n              save(ls, c);\n            }\n            continue;\n          }\n        }\n        save(ls, c);\n        next(ls);\n        continue;\n      }\n      default:\n        save_and_next(ls);\n    }\n  }\n  save_and_next(ls);  /* skip delimiter */\n  seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1,\n                                   luaZ_bufflen(ls->buff) - 2);\n}\n\n\nstatic int llex (LexState *ls, SemInfo *seminfo) {\n  luaZ_resetbuffer(ls->buff);\n  for (;;) {\n    switch (ls->current) {\n      case '\\n':\n      case '\\r': {\n        inclinenumber(ls);\n        continue;\n      }\n      case '-': {\n        next(ls);\n        if (ls->current != '-') return '-';\n        /* else is a comment */\n        next(ls);\n        if (ls->current == '[') {\n          int sep = skip_sep(ls);\n          luaZ_resetbuffer(ls->buff);  /* `skip_sep' may dirty the buffer */\n          if (sep >= 0) {\n            read_long_string(ls, NULL, sep);  /* long comment */\n            luaZ_resetbuffer(ls->buff);\n            continue;\n          }\n        }\n        /* else short comment */\n        while (!currIsNewline(ls) && ls->current != EOZ)\n          next(ls);\n        continue;\n      }\n      case '[': {\n        int sep = skip_sep(ls);\n        if (sep >= 0) {\n          read_long_string(ls, seminfo, sep);\n          return TK_STRING;\n        }\n        else if (sep == -1) return '[';\n        else luaX_lexerror(ls, \"invalid long string delimiter\", TK_STRING);\n      }\n      case '=': {\n        next(ls);\n        if (ls->current != '=') return '=';\n        else { next(ls); return TK_EQ; }\n      }\n      case '<': {\n        next(ls);\n        if (ls->current != '=') return '<';\n        else { next(ls); return TK_LE; }\n      }\n      case '>': {\n        next(ls);\n        if (ls->current != '=') return '>';\n        else { next(ls); return TK_GE; }\n      }\n      case '~': {\n        next(ls);\n        if (ls->current != '=') return '~';\n        else { next(ls); return TK_NE; }\n      }\n      case '\"':\n      case '\\'': {\n        read_string(ls, ls->current, seminfo);\n        return TK_STRING;\n      }\n      case '.': {\n        save_and_next(ls);\n        if (check_next(ls, \".\")) {\n          if (check_next(ls, \".\"))\n            return TK_DOTS;   /* ... */\n          else return TK_CONCAT;   /* .. */\n        }\n        else if (!isdigit(ls->current)) return '.';\n        else {\n          read_numeral(ls, seminfo);\n          return TK_NUMBER;\n        }\n      }\n      case EOZ: {\n        return TK_EOS;\n      }\n      default: {\n        if (isspace(ls->current)) {\n          lua_assert(!currIsNewline(ls));\n          next(ls);\n          continue;\n        }\n        else if (isdigit(ls->current)) {\n          read_numeral(ls, seminfo);\n          return TK_NUMBER;\n        }\n        else if (isalpha(ls->current) || ls->current == '_') {\n          /* identifier or reserved word */\n          TString *ts;\n          do {\n            save_and_next(ls);\n          } while (isalnum(ls->current) || ls->current == '_');\n          ts = luaX_newstring(ls, luaZ_buffer(ls->buff),\n                                  luaZ_bufflen(ls->buff));\n          if (ts->tsv.reserved > 0)  /* reserved word? */\n            return ts->tsv.reserved - 1 + FIRST_RESERVED;\n          else {\n            seminfo->ts = ts;\n            return TK_NAME;\n          }\n        }\n        else {\n          int c = ls->current;\n          next(ls);\n          return c;  /* single-char tokens (+ - / ...) */\n        }\n      }\n    }\n  }\n}\n\n\nvoid luaX_next (LexState *ls) {\n  ls->lastline = ls->linenumber;\n  if (ls->lookahead.token != TK_EOS) {  /* is there a look-ahead token? */\n    ls->t = ls->lookahead;  /* use this one */\n    ls->lookahead.token = TK_EOS;  /* and discharge it */\n  }\n  else\n    ls->t.token = llex(ls, &ls->t.seminfo);  /* read next token */\n}\n\n\nvoid luaX_lookahead (LexState *ls) {\n  lua_assert(ls->lookahead.token == TK_EOS);\n  ls->lookahead.token = llex(ls, &ls->lookahead.seminfo);\n}\n\n"
  },
  {
    "path": "deps/lua/src/llex.h",
    "content": "/*\n** $Id: llex.h,v 1.58.1.1 2007/12/27 13:02:25 roberto Exp $\n** Lexical Analyzer\n** See Copyright Notice in lua.h\n*/\n\n#ifndef llex_h\n#define llex_h\n\n#include \"lobject.h\"\n#include \"lzio.h\"\n\n\n#define FIRST_RESERVED\t257\n\n/* maximum length of a reserved word */\n#define TOKEN_LEN\t(sizeof(\"function\")/sizeof(char))\n\n\n/*\n* WARNING: if you change the order of this enumeration,\n* grep \"ORDER RESERVED\"\n*/\nenum RESERVED {\n  /* terminal symbols denoted by reserved words */\n  TK_AND = FIRST_RESERVED, TK_BREAK,\n  TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION,\n  TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT,\n  TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE,\n  /* other terminal symbols */\n  TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER,\n  TK_NAME, TK_STRING, TK_EOS\n};\n\n/* number of reserved words */\n#define NUM_RESERVED\t(cast(int, TK_WHILE-FIRST_RESERVED+1))\n\n\n/* array with token `names' */\nLUAI_DATA const char *const luaX_tokens [];\n\n\ntypedef union {\n  lua_Number r;\n  TString *ts;\n} SemInfo;  /* semantics information */\n\n\ntypedef struct Token {\n  int token;\n  SemInfo seminfo;\n} Token;\n\n\ntypedef struct LexState {\n  int current;  /* current character (charint) */\n  int linenumber;  /* input line counter */\n  int lastline;  /* line of last token `consumed' */\n  Token t;  /* current token */\n  Token lookahead;  /* look ahead token */\n  struct FuncState *fs;  /* `FuncState' is private to the parser */\n  struct lua_State *L;\n  ZIO *z;  /* input stream */\n  Mbuffer *buff;  /* buffer for tokens */\n  TString *source;  /* current source name */\n  char decpoint;  /* locale decimal point */\n} LexState;\n\n\nLUAI_FUNC void luaX_init (lua_State *L);\nLUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z,\n                              TString *source);\nLUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l);\nLUAI_FUNC void luaX_next (LexState *ls);\nLUAI_FUNC void luaX_lookahead (LexState *ls);\nLUAI_FUNC void luaX_lexerror (LexState *ls, const char *msg, int token);\nLUAI_FUNC void luaX_syntaxerror (LexState *ls, const char *s);\nLUAI_FUNC const char *luaX_token2str (LexState *ls, int token);\n\n\n#endif\n"
  },
  {
    "path": "deps/lua/src/llimits.h",
    "content": "/*\n** $Id: llimits.h,v 1.69.1.1 2007/12/27 13:02:25 roberto Exp $\n** Limits, basic types, and some other `installation-dependent' definitions\n** See Copyright Notice in lua.h\n*/\n\n#ifndef llimits_h\n#define llimits_h\n\n\n#include <limits.h>\n#include <stddef.h>\n\n\n#include \"lua.h\"\n\n\ntypedef LUAI_UINT32 lu_int32;\n\ntypedef LUAI_UMEM lu_mem;\n\ntypedef LUAI_MEM l_mem;\n\n\n\n/* chars used as small naturals (so that `char' is reserved for characters) */\ntypedef unsigned char lu_byte;\n\n\n#define MAX_SIZET\t((size_t)(~(size_t)0)-2)\n\n#define MAX_LUMEM\t((lu_mem)(~(lu_mem)0)-2)\n\n\n#define MAX_INT (INT_MAX-2)  /* maximum value of an int (-2 for safety) */\n\n/*\n** conversion of pointer to integer\n** this is for hashing only; there is no problem if the integer\n** cannot hold the whole pointer value\n*/\n#define IntPoint(p)  ((unsigned int)(lu_mem)(p))\n\n\n\n/* type to ensure maximum alignment */\ntypedef LUAI_USER_ALIGNMENT_T L_Umaxalign;\n\n\n/* result of a `usual argument conversion' over lua_Number */\ntypedef LUAI_UACNUMBER l_uacNumber;\n\n\n/* internal assertions for in-house debugging */\n#ifdef lua_assert\n\n#define check_exp(c,e)\t\t(lua_assert(c), (e))\n#define api_check(l,e)\t\tlua_assert(e)\n\n#else\n\n#define lua_assert(c)\t\t((void)0)\n#define check_exp(c,e)\t\t(e)\n#define api_check\t\tluai_apicheck\n\n#endif\n\n\n#ifndef UNUSED\n#define UNUSED(x)\t((void)(x))\t/* to avoid warnings */\n#endif\n\n\n#ifndef cast\n#define cast(t, exp)\t((t)(exp))\n#endif\n\n#define cast_byte(i)\tcast(lu_byte, (i))\n#define cast_num(i)\tcast(lua_Number, (i))\n#define cast_int(i)\tcast(int, (i))\n\n\n\n/*\n** type for virtual-machine instructions\n** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h)\n*/\ntypedef lu_int32 Instruction;\n\n\n\n/* maximum stack for a Lua function */\n#define MAXSTACK\t250\n\n\n\n/* minimum size for the string table (must be power of 2) */\n#ifndef MINSTRTABSIZE\n#define MINSTRTABSIZE\t32\n#endif\n\n\n/* minimum size for string buffer */\n#ifndef LUA_MINBUFFER\n#define LUA_MINBUFFER\t32\n#endif\n\n\n#ifndef lua_lock\n#define lua_lock(L)     ((void) 0) \n#define lua_unlock(L)   ((void) 0)\n#endif\n\n#ifndef luai_threadyield\n#define luai_threadyield(L)     {lua_unlock(L); lua_lock(L);}\n#endif\n\n\n/*\n** macro to control inclusion of some hard tests on stack reallocation\n*/ \n#ifndef HARDSTACKTESTS\n#define condhardstacktests(x)\t((void)0)\n#else\n#define condhardstacktests(x)\tx\n#endif\n\n#endif\n"
  },
  {
    "path": "deps/lua/src/lmathlib.c",
    "content": "/*\n** $Id: lmathlib.c,v 1.67.1.1 2007/12/27 13:02:25 roberto Exp $\n** Standard mathematical library\n** See Copyright Notice in lua.h\n*/\n\n\n#include <stdlib.h>\n#include <math.h>\n\n#define lmathlib_c\n#define LUA_LIB\n\n#include \"lua.h\"\n\n#include \"lauxlib.h\"\n#include \"lualib.h\"\n\n\n#undef PI\n#define PI (3.14159265358979323846)\n#define RADIANS_PER_DEGREE (PI/180.0)\n\n\n\nstatic int math_abs (lua_State *L) {\n  lua_pushnumber(L, fabs(luaL_checknumber(L, 1)));\n  return 1;\n}\n\nstatic int math_sin (lua_State *L) {\n  lua_pushnumber(L, sin(luaL_checknumber(L, 1)));\n  return 1;\n}\n\nstatic int math_sinh (lua_State *L) {\n  lua_pushnumber(L, sinh(luaL_checknumber(L, 1)));\n  return 1;\n}\n\nstatic int math_cos (lua_State *L) {\n  lua_pushnumber(L, cos(luaL_checknumber(L, 1)));\n  return 1;\n}\n\nstatic int math_cosh (lua_State *L) {\n  lua_pushnumber(L, cosh(luaL_checknumber(L, 1)));\n  return 1;\n}\n\nstatic int math_tan (lua_State *L) {\n  lua_pushnumber(L, tan(luaL_checknumber(L, 1)));\n  return 1;\n}\n\nstatic int math_tanh (lua_State *L) {\n  lua_pushnumber(L, tanh(luaL_checknumber(L, 1)));\n  return 1;\n}\n\nstatic int math_asin (lua_State *L) {\n  lua_pushnumber(L, asin(luaL_checknumber(L, 1)));\n  return 1;\n}\n\nstatic int math_acos (lua_State *L) {\n  lua_pushnumber(L, acos(luaL_checknumber(L, 1)));\n  return 1;\n}\n\nstatic int math_atan (lua_State *L) {\n  lua_pushnumber(L, atan(luaL_checknumber(L, 1)));\n  return 1;\n}\n\nstatic int math_atan2 (lua_State *L) {\n  lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));\n  return 1;\n}\n\nstatic int math_ceil (lua_State *L) {\n  lua_pushnumber(L, ceil(luaL_checknumber(L, 1)));\n  return 1;\n}\n\nstatic int math_floor (lua_State *L) {\n  lua_pushnumber(L, floor(luaL_checknumber(L, 1)));\n  return 1;\n}\n\nstatic int math_fmod (lua_State *L) {\n  lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));\n  return 1;\n}\n\nstatic int math_modf (lua_State *L) {\n  double ip;\n  double fp = modf(luaL_checknumber(L, 1), &ip);\n  lua_pushnumber(L, ip);\n  lua_pushnumber(L, fp);\n  return 2;\n}\n\nstatic int math_sqrt (lua_State *L) {\n  lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));\n  return 1;\n}\n\nstatic int math_pow (lua_State *L) {\n  lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));\n  return 1;\n}\n\nstatic int math_log (lua_State *L) {\n  lua_pushnumber(L, log(luaL_checknumber(L, 1)));\n  return 1;\n}\n\nstatic int math_log10 (lua_State *L) {\n  lua_pushnumber(L, log10(luaL_checknumber(L, 1)));\n  return 1;\n}\n\nstatic int math_exp (lua_State *L) {\n  lua_pushnumber(L, exp(luaL_checknumber(L, 1)));\n  return 1;\n}\n\nstatic int math_deg (lua_State *L) {\n  lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE);\n  return 1;\n}\n\nstatic int math_rad (lua_State *L) {\n  lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE);\n  return 1;\n}\n\nstatic int math_frexp (lua_State *L) {\n  int e;\n  lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e));\n  lua_pushinteger(L, e);\n  return 2;\n}\n\nstatic int math_ldexp (lua_State *L) {\n  lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2)));\n  return 1;\n}\n\n\n\nstatic int math_min (lua_State *L) {\n  int n = lua_gettop(L);  /* number of arguments */\n  lua_Number dmin = luaL_checknumber(L, 1);\n  int i;\n  for (i=2; i<=n; i++) {\n    lua_Number d = luaL_checknumber(L, i);\n    if (d < dmin)\n      dmin = d;\n  }\n  lua_pushnumber(L, dmin);\n  return 1;\n}\n\n\nstatic int math_max (lua_State *L) {\n  int n = lua_gettop(L);  /* number of arguments */\n  lua_Number dmax = luaL_checknumber(L, 1);\n  int i;\n  for (i=2; i<=n; i++) {\n    lua_Number d = luaL_checknumber(L, i);\n    if (d > dmax)\n      dmax = d;\n  }\n  lua_pushnumber(L, dmax);\n  return 1;\n}\n\n\nstatic int math_random (lua_State *L) {\n  /* the `%' avoids the (rare) case of r==1, and is needed also because on\n     some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */\n  lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;\n  switch (lua_gettop(L)) {  /* check number of arguments */\n    case 0: {  /* no arguments */\n      lua_pushnumber(L, r);  /* Number between 0 and 1 */\n      break;\n    }\n    case 1: {  /* only upper limit */\n      int u = luaL_checkint(L, 1);\n      luaL_argcheck(L, 1<=u, 1, \"interval is empty\");\n      lua_pushnumber(L, floor(r*u)+1);  /* int between 1 and `u' */\n      break;\n    }\n    case 2: {  /* lower and upper limits */\n      int l = luaL_checkint(L, 1);\n      int u = luaL_checkint(L, 2);\n      luaL_argcheck(L, l<=u, 2, \"interval is empty\");\n      lua_pushnumber(L, floor(r*(u-l+1))+l);  /* int between `l' and `u' */\n      break;\n    }\n    default: return luaL_error(L, \"wrong number of arguments\");\n  }\n  return 1;\n}\n\n\nstatic int math_randomseed (lua_State *L) {\n  srand(luaL_checkint(L, 1));\n  return 0;\n}\n\n\nstatic const luaL_Reg mathlib[] = {\n  {\"abs\",   math_abs},\n  {\"acos\",  math_acos},\n  {\"asin\",  math_asin},\n  {\"atan2\", math_atan2},\n  {\"atan\",  math_atan},\n  {\"ceil\",  math_ceil},\n  {\"cosh\",   math_cosh},\n  {\"cos\",   math_cos},\n  {\"deg\",   math_deg},\n  {\"exp\",   math_exp},\n  {\"floor\", math_floor},\n  {\"fmod\",   math_fmod},\n  {\"frexp\", math_frexp},\n  {\"ldexp\", math_ldexp},\n  {\"log10\", math_log10},\n  {\"log\",   math_log},\n  {\"max\",   math_max},\n  {\"min\",   math_min},\n  {\"modf\",   math_modf},\n  {\"pow\",   math_pow},\n  {\"rad\",   math_rad},\n  {\"random\",     math_random},\n  {\"randomseed\", math_randomseed},\n  {\"sinh\",   math_sinh},\n  {\"sin\",   math_sin},\n  {\"sqrt\",  math_sqrt},\n  {\"tanh\",   math_tanh},\n  {\"tan\",   math_tan},\n  {NULL, NULL}\n};\n\n\n/*\n** Open math library\n*/\nLUALIB_API int luaopen_math (lua_State *L) {\n  luaL_register(L, LUA_MATHLIBNAME, mathlib);\n  lua_pushnumber(L, PI);\n  lua_setfield(L, -2, \"pi\");\n  lua_pushnumber(L, HUGE_VAL);\n  lua_setfield(L, -2, \"huge\");\n#if defined(LUA_COMPAT_MOD)\n  lua_getfield(L, -1, \"fmod\");\n  lua_setfield(L, -2, \"mod\");\n#endif\n  return 1;\n}\n\n"
  },
  {
    "path": "deps/lua/src/lmem.c",
    "content": "/*\n** $Id: lmem.c,v 1.70.1.1 2007/12/27 13:02:25 roberto Exp $\n** Interface to Memory Manager\n** See Copyright Notice in lua.h\n*/\n\n\n#include <stddef.h>\n\n#define lmem_c\n#define LUA_CORE\n\n#include \"lua.h\"\n\n#include \"ldebug.h\"\n#include \"ldo.h\"\n#include \"lmem.h\"\n#include \"lobject.h\"\n#include \"lstate.h\"\n\n\n\n/*\n** About the realloc function:\n** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);\n** (`osize' is the old size, `nsize' is the new size)\n**\n** Lua ensures that (ptr == NULL) iff (osize == 0).\n**\n** * frealloc(ud, NULL, 0, x) creates a new block of size `x'\n**\n** * frealloc(ud, p, x, 0) frees the block `p'\n** (in this specific case, frealloc must return NULL).\n** particularly, frealloc(ud, NULL, 0, 0) does nothing\n** (which is equivalent to free(NULL) in ANSI C)\n**\n** frealloc returns NULL if it cannot create or reallocate the area\n** (any reallocation to an equal or smaller size cannot fail!)\n*/\n\n\n\n#define MINSIZEARRAY\t4\n\n\nvoid *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems,\n                     int limit, const char *errormsg) {\n  void *newblock;\n  int newsize;\n  if (*size >= limit/2) {  /* cannot double it? */\n    if (*size >= limit)  /* cannot grow even a little? */\n      luaG_runerror(L, errormsg);\n    newsize = limit;  /* still have at least one free place */\n  }\n  else {\n    newsize = (*size)*2;\n    if (newsize < MINSIZEARRAY)\n      newsize = MINSIZEARRAY;  /* minimum size */\n  }\n  newblock = luaM_reallocv(L, block, *size, newsize, size_elems);\n  *size = newsize;  /* update only when everything else is OK */\n  return newblock;\n}\n\n\nvoid *luaM_toobig (lua_State *L) {\n  luaG_runerror(L, \"memory allocation error: block too big\");\n  return NULL;  /* to avoid warnings */\n}\n\n\n\n/*\n** generic allocation routine.\n*/\nvoid *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {\n  global_State *g = G(L);\n  lua_assert((osize == 0) == (block == NULL));\n  block = (*g->frealloc)(g->ud, block, osize, nsize);\n  if (block == NULL && nsize > 0)\n    luaD_throw(L, LUA_ERRMEM);\n  lua_assert((nsize == 0) == (block == NULL));\n  g->totalbytes = (g->totalbytes - osize) + nsize;\n  return block;\n}\n\n"
  },
  {
    "path": "deps/lua/src/lmem.h",
    "content": "/*\n** $Id: lmem.h,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $\n** Interface to Memory Manager\n** See Copyright Notice in lua.h\n*/\n\n#ifndef lmem_h\n#define lmem_h\n\n\n#include <stddef.h>\n\n#include \"llimits.h\"\n#include \"lua.h\"\n\n#define MEMERRMSG\t\"not enough memory\"\n\n\n#define luaM_reallocv(L,b,on,n,e) \\\n\t((cast(size_t, (n)+1) <= MAX_SIZET/(e)) ?  /* +1 to avoid warnings */ \\\n\t\tluaM_realloc_(L, (b), (on)*(e), (n)*(e)) : \\\n\t\tluaM_toobig(L))\n\n#define luaM_freemem(L, b, s)\tluaM_realloc_(L, (b), (s), 0)\n#define luaM_free(L, b)\t\tluaM_realloc_(L, (b), sizeof(*(b)), 0)\n#define luaM_freearray(L, b, n, t)   luaM_reallocv(L, (b), n, 0, sizeof(t))\n\n#define luaM_malloc(L,t)\tluaM_realloc_(L, NULL, 0, (t))\n#define luaM_new(L,t)\t\tcast(t *, luaM_malloc(L, sizeof(t)))\n#define luaM_newvector(L,n,t) \\\n\t\tcast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t)))\n\n#define luaM_growvector(L,v,nelems,size,t,limit,e) \\\n          if ((nelems)+1 > (size)) \\\n            ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e)))\n\n#define luaM_reallocvector(L, v,oldn,n,t) \\\n   ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t))))\n\n\nLUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize,\n                                                          size_t size);\nLUAI_FUNC void *luaM_toobig (lua_State *L);\nLUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size,\n                               size_t size_elem, int limit,\n                               const char *errormsg);\n\n#endif\n\n"
  },
  {
    "path": "deps/lua/src/loadlib.c",
    "content": "/*\n** $Id: loadlib.c,v 1.52.1.4 2009/09/09 13:17:16 roberto Exp $\n** Dynamic library loader for Lua\n** See Copyright Notice in lua.h\n**\n** This module contains an implementation of loadlib for Unix systems\n** that have dlfcn, an implementation for Darwin (Mac OS X), an\n** implementation for Windows, and a stub for other systems.\n*/\n\n\n#include <stdlib.h>\n#include <string.h>\n\n\n#define loadlib_c\n#define LUA_LIB\n\n#include \"lua.h\"\n\n#include \"lauxlib.h\"\n#include \"lualib.h\"\n\n\n/* prefix for open functions in C libraries */\n#define LUA_POF\t\t\"luaopen_\"\n\n/* separator for open functions in C libraries */\n#define LUA_OFSEP\t\"_\"\n\n\n#define LIBPREFIX\t\"LOADLIB: \"\n\n#define POF\t\tLUA_POF\n#define LIB_FAIL\t\"open\"\n\n\n/* error codes for ll_loadfunc */\n#define ERRLIB\t\t1\n#define ERRFUNC\t\t2\n\n#define setprogdir(L)\t\t((void)0)\n\n\nstatic void ll_unloadlib (void *lib);\nstatic void *ll_load (lua_State *L, const char *path);\nstatic lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym);\n\n\n\n#if defined(LUA_DL_DLOPEN)\n/*\n** {========================================================================\n** This is an implementation of loadlib based on the dlfcn interface.\n** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,\n** NetBSD, AIX 4.2, HPUX 11, and  probably most other Unix flavors, at least\n** as an emulation layer on top of native functions.\n** =========================================================================\n*/\n\n#include <dlfcn.h>\n\nstatic void ll_unloadlib (void *lib) {\n  dlclose(lib);\n}\n\n\nstatic void *ll_load (lua_State *L, const char *path) {\n  void *lib = dlopen(path, RTLD_NOW);\n  if (lib == NULL) lua_pushstring(L, dlerror());\n  return lib;\n}\n\n\nstatic lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {\n  lua_CFunction f = (lua_CFunction)dlsym(lib, sym);\n  if (f == NULL) lua_pushstring(L, dlerror());\n  return f;\n}\n\n/* }====================================================== */\n\n\n\n#elif defined(LUA_DL_DLL)\n/*\n** {======================================================================\n** This is an implementation of loadlib for Windows using native functions.\n** =======================================================================\n*/\n\n#include <windows.h>\n\n\n#undef setprogdir\n\nstatic void setprogdir (lua_State *L) {\n  char buff[MAX_PATH + 1];\n  char *lb;\n  DWORD nsize = sizeof(buff)/sizeof(char);\n  DWORD n = GetModuleFileNameA(NULL, buff, nsize);\n  if (n == 0 || n == nsize || (lb = strrchr(buff, '\\\\')) == NULL)\n    luaL_error(L, \"unable to get ModuleFileName\");\n  else {\n    *lb = '\\0';\n    luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff);\n    lua_remove(L, -2);  /* remove original string */\n  }\n}\n\n\nstatic void pusherror (lua_State *L) {\n  int error = GetLastError();\n  char buffer[128];\n  if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,\n      NULL, error, 0, buffer, sizeof(buffer), NULL))\n    lua_pushstring(L, buffer);\n  else\n    lua_pushfstring(L, \"system error %d\\n\", error);\n}\n\nstatic void ll_unloadlib (void *lib) {\n  FreeLibrary((HINSTANCE)lib);\n}\n\n\nstatic void *ll_load (lua_State *L, const char *path) {\n  HINSTANCE lib = LoadLibraryA(path);\n  if (lib == NULL) pusherror(L);\n  return lib;\n}\n\n\nstatic lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {\n  lua_CFunction f = (lua_CFunction)GetProcAddress((HINSTANCE)lib, sym);\n  if (f == NULL) pusherror(L);\n  return f;\n}\n\n/* }====================================================== */\n\n\n\n#elif defined(LUA_DL_DYLD)\n/*\n** {======================================================================\n** Native Mac OS X / Darwin Implementation\n** =======================================================================\n*/\n\n#include <mach-o/dyld.h>\n\n\n/* Mac appends a `_' before C function names */\n#undef POF\n#define POF\t\"_\" LUA_POF\n\n\nstatic void pusherror (lua_State *L) {\n  const char *err_str;\n  const char *err_file;\n  NSLinkEditErrors err;\n  int err_num;\n  NSLinkEditError(&err, &err_num, &err_file, &err_str);\n  lua_pushstring(L, err_str);\n}\n\n\nstatic const char *errorfromcode (NSObjectFileImageReturnCode ret) {\n  switch (ret) {\n    case NSObjectFileImageInappropriateFile:\n      return \"file is not a bundle\";\n    case NSObjectFileImageArch:\n      return \"library is for wrong CPU type\";\n    case NSObjectFileImageFormat:\n      return \"bad format\";\n    case NSObjectFileImageAccess:\n      return \"cannot access file\";\n    case NSObjectFileImageFailure:\n    default:\n      return \"unable to load library\";\n  }\n}\n\n\nstatic void ll_unloadlib (void *lib) {\n  NSUnLinkModule((NSModule)lib, NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES);\n}\n\n\nstatic void *ll_load (lua_State *L, const char *path) {\n  NSObjectFileImage img;\n  NSObjectFileImageReturnCode ret;\n  /* this would be a rare case, but prevents crashing if it happens */\n  if(!_dyld_present()) {\n    lua_pushliteral(L, \"dyld not present\");\n    return NULL;\n  }\n  ret = NSCreateObjectFileImageFromFile(path, &img);\n  if (ret == NSObjectFileImageSuccess) {\n    NSModule mod = NSLinkModule(img, path, NSLINKMODULE_OPTION_PRIVATE |\n                       NSLINKMODULE_OPTION_RETURN_ON_ERROR);\n    NSDestroyObjectFileImage(img);\n    if (mod == NULL) pusherror(L);\n    return mod;\n  }\n  lua_pushstring(L, errorfromcode(ret));\n  return NULL;\n}\n\n\nstatic lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {\n  NSSymbol nss = NSLookupSymbolInModule((NSModule)lib, sym);\n  if (nss == NULL) {\n    lua_pushfstring(L, \"symbol \" LUA_QS \" not found\", sym);\n    return NULL;\n  }\n  return (lua_CFunction)NSAddressOfSymbol(nss);\n}\n\n/* }====================================================== */\n\n\n\n#else\n/*\n** {======================================================\n** Fallback for other systems\n** =======================================================\n*/\n\n#undef LIB_FAIL\n#define LIB_FAIL\t\"absent\"\n\n\n#define DLMSG\t\"dynamic libraries not enabled; check your Lua installation\"\n\n\nstatic void ll_unloadlib (void *lib) {\n  (void)lib;  /* to avoid warnings */\n}\n\n\nstatic void *ll_load (lua_State *L, const char *path) {\n  (void)path;  /* to avoid warnings */\n  lua_pushliteral(L, DLMSG);\n  return NULL;\n}\n\n\nstatic lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {\n  (void)lib; (void)sym;  /* to avoid warnings */\n  lua_pushliteral(L, DLMSG);\n  return NULL;\n}\n\n/* }====================================================== */\n#endif\n\n\n\nstatic void **ll_register (lua_State *L, const char *path) {\n  void **plib;\n  lua_pushfstring(L, \"%s%s\", LIBPREFIX, path);\n  lua_gettable(L, LUA_REGISTRYINDEX);  /* check library in registry? */\n  if (!lua_isnil(L, -1))  /* is there an entry? */\n    plib = (void **)lua_touserdata(L, -1);\n  else {  /* no entry yet; create one */\n    lua_pop(L, 1);\n    plib = (void **)lua_newuserdata(L, sizeof(const void *));\n    *plib = NULL;\n    luaL_getmetatable(L, \"_LOADLIB\");\n    lua_setmetatable(L, -2);\n    lua_pushfstring(L, \"%s%s\", LIBPREFIX, path);\n    lua_pushvalue(L, -2);\n    lua_settable(L, LUA_REGISTRYINDEX);\n  }\n  return plib;\n}\n\n\n/*\n** __gc tag method: calls library's `ll_unloadlib' function with the lib\n** handle\n*/\nstatic int gctm (lua_State *L) {\n  void **lib = (void **)luaL_checkudata(L, 1, \"_LOADLIB\");\n  if (*lib) ll_unloadlib(*lib);\n  *lib = NULL;  /* mark library as closed */\n  return 0;\n}\n\n\nstatic int ll_loadfunc (lua_State *L, const char *path, const char *sym) {\n  void **reg = ll_register(L, path);\n  if (*reg == NULL) *reg = ll_load(L, path);\n  if (*reg == NULL)\n    return ERRLIB;  /* unable to load library */\n  else {\n    lua_CFunction f = ll_sym(L, *reg, sym);\n    if (f == NULL)\n      return ERRFUNC;  /* unable to find function */\n    lua_pushcfunction(L, f);\n    return 0;  /* return function */\n  }\n}\n\n\nstatic int ll_loadlib (lua_State *L) {\n  const char *path = luaL_checkstring(L, 1);\n  const char *init = luaL_checkstring(L, 2);\n  int stat = ll_loadfunc(L, path, init);\n  if (stat == 0)  /* no errors? */\n    return 1;  /* return the loaded function */\n  else {  /* error; error message is on stack top */\n    lua_pushnil(L);\n    lua_insert(L, -2);\n    lua_pushstring(L, (stat == ERRLIB) ?  LIB_FAIL : \"init\");\n    return 3;  /* return nil, error message, and where */\n  }\n}\n\n\n\n/*\n** {======================================================\n** 'require' function\n** =======================================================\n*/\n\n\nstatic int readable (const char *filename) {\n  FILE *f = fopen(filename, \"r\");  /* try to open file */\n  if (f == NULL) return 0;  /* open failed */\n  fclose(f);\n  return 1;\n}\n\n\nstatic const char *pushnexttemplate (lua_State *L, const char *path) {\n  const char *l;\n  while (*path == *LUA_PATHSEP) path++;  /* skip separators */\n  if (*path == '\\0') return NULL;  /* no more templates */\n  l = strchr(path, *LUA_PATHSEP);  /* find next separator */\n  if (l == NULL) l = path + strlen(path);\n  lua_pushlstring(L, path, l - path);  /* template */\n  return l;\n}\n\n\nstatic const char *findfile (lua_State *L, const char *name,\n                                           const char *pname) {\n  const char *path;\n  name = luaL_gsub(L, name, \".\", LUA_DIRSEP);\n  lua_getfield(L, LUA_ENVIRONINDEX, pname);\n  path = lua_tostring(L, -1);\n  if (path == NULL)\n    luaL_error(L, LUA_QL(\"package.%s\") \" must be a string\", pname);\n  lua_pushliteral(L, \"\");  /* error accumulator */\n  while ((path = pushnexttemplate(L, path)) != NULL) {\n    const char *filename;\n    filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);\n    lua_remove(L, -2);  /* remove path template */\n    if (readable(filename))  /* does file exist and is readable? */\n      return filename;  /* return that file name */\n    lua_pushfstring(L, \"\\n\\tno file \" LUA_QS, filename);\n    lua_remove(L, -2);  /* remove file name */\n    lua_concat(L, 2);  /* add entry to possible error message */\n  }\n  return NULL;  /* not found */\n}\n\n\nstatic void loaderror (lua_State *L, const char *filename) {\n  luaL_error(L, \"error loading module \" LUA_QS \" from file \" LUA_QS \":\\n\\t%s\",\n                lua_tostring(L, 1), filename, lua_tostring(L, -1));\n}\n\n\nstatic int loader_Lua (lua_State *L) {\n  const char *filename;\n  const char *name = luaL_checkstring(L, 1);\n  filename = findfile(L, name, \"path\");\n  if (filename == NULL) return 1;  /* library not found in this path */\n  if (luaL_loadfile(L, filename) != 0)\n    loaderror(L, filename);\n  return 1;  /* library loaded successfully */\n}\n\n\nstatic const char *mkfuncname (lua_State *L, const char *modname) {\n  const char *funcname;\n  const char *mark = strchr(modname, *LUA_IGMARK);\n  if (mark) modname = mark + 1;\n  funcname = luaL_gsub(L, modname, \".\", LUA_OFSEP);\n  funcname = lua_pushfstring(L, POF\"%s\", funcname);\n  lua_remove(L, -2);  /* remove 'gsub' result */\n  return funcname;\n}\n\n\nstatic int loader_C (lua_State *L) {\n  const char *funcname;\n  const char *name = luaL_checkstring(L, 1);\n  const char *filename = findfile(L, name, \"cpath\");\n  if (filename == NULL) return 1;  /* library not found in this path */\n  funcname = mkfuncname(L, name);\n  if (ll_loadfunc(L, filename, funcname) != 0)\n    loaderror(L, filename);\n  return 1;  /* library loaded successfully */\n}\n\n\nstatic int loader_Croot (lua_State *L) {\n  const char *funcname;\n  const char *filename;\n  const char *name = luaL_checkstring(L, 1);\n  const char *p = strchr(name, '.');\n  int stat;\n  if (p == NULL) return 0;  /* is root */\n  lua_pushlstring(L, name, p - name);\n  filename = findfile(L, lua_tostring(L, -1), \"cpath\");\n  if (filename == NULL) return 1;  /* root not found */\n  funcname = mkfuncname(L, name);\n  if ((stat = ll_loadfunc(L, filename, funcname)) != 0) {\n    if (stat != ERRFUNC) loaderror(L, filename);  /* real error */\n    lua_pushfstring(L, \"\\n\\tno module \" LUA_QS \" in file \" LUA_QS,\n                       name, filename);\n    return 1;  /* function not found */\n  }\n  return 1;\n}\n\n\nstatic int loader_preload (lua_State *L) {\n  const char *name = luaL_checkstring(L, 1);\n  lua_getfield(L, LUA_ENVIRONINDEX, \"preload\");\n  if (!lua_istable(L, -1))\n    luaL_error(L, LUA_QL(\"package.preload\") \" must be a table\");\n  lua_getfield(L, -1, name);\n  if (lua_isnil(L, -1))  /* not found? */\n    lua_pushfstring(L, \"\\n\\tno field package.preload['%s']\", name);\n  return 1;\n}\n\n\nstatic const int sentinel_ = 0;\n#define sentinel\t((void *)&sentinel_)\n\n\nstatic int ll_require (lua_State *L) {\n  const char *name = luaL_checkstring(L, 1);\n  int i;\n  lua_settop(L, 1);  /* _LOADED table will be at index 2 */\n  lua_getfield(L, LUA_REGISTRYINDEX, \"_LOADED\");\n  lua_getfield(L, 2, name);\n  if (lua_toboolean(L, -1)) {  /* is it there? */\n    if (lua_touserdata(L, -1) == sentinel)  /* check loops */\n      luaL_error(L, \"loop or previous error loading module \" LUA_QS, name);\n    return 1;  /* package is already loaded */\n  }\n  /* else must load it; iterate over available loaders */\n  lua_getfield(L, LUA_ENVIRONINDEX, \"loaders\");\n  if (!lua_istable(L, -1))\n    luaL_error(L, LUA_QL(\"package.loaders\") \" must be a table\");\n  lua_pushliteral(L, \"\");  /* error message accumulator */\n  for (i=1; ; i++) {\n    lua_rawgeti(L, -2, i);  /* get a loader */\n    if (lua_isnil(L, -1))\n      luaL_error(L, \"module \" LUA_QS \" not found:%s\",\n                    name, lua_tostring(L, -2));\n    lua_pushstring(L, name);\n    lua_call(L, 1, 1);  /* call it */\n    if (lua_isfunction(L, -1))  /* did it find module? */\n      break;  /* module loaded successfully */\n    else if (lua_isstring(L, -1))  /* loader returned error message? */\n      lua_concat(L, 2);  /* accumulate it */\n    else\n      lua_pop(L, 1);\n  }\n  lua_pushlightuserdata(L, sentinel);\n  lua_setfield(L, 2, name);  /* _LOADED[name] = sentinel */\n  lua_pushstring(L, name);  /* pass name as argument to module */\n  lua_call(L, 1, 1);  /* run loaded module */\n  if (!lua_isnil(L, -1))  /* non-nil return? */\n    lua_setfield(L, 2, name);  /* _LOADED[name] = returned value */\n  lua_getfield(L, 2, name);\n  if (lua_touserdata(L, -1) == sentinel) {   /* module did not set a value? */\n    lua_pushboolean(L, 1);  /* use true as result */\n    lua_pushvalue(L, -1);  /* extra copy to be returned */\n    lua_setfield(L, 2, name);  /* _LOADED[name] = true */\n  }\n  return 1;\n}\n\n/* }====================================================== */\n\n\n\n/*\n** {======================================================\n** 'module' function\n** =======================================================\n*/\n  \n\nstatic void setfenv (lua_State *L) {\n  lua_Debug ar;\n  if (lua_getstack(L, 1, &ar) == 0 ||\n      lua_getinfo(L, \"f\", &ar) == 0 ||  /* get calling function */\n      lua_iscfunction(L, -1))\n    luaL_error(L, LUA_QL(\"module\") \" not called from a Lua function\");\n  lua_pushvalue(L, -2);\n  lua_setfenv(L, -2);\n  lua_pop(L, 1);\n}\n\n\nstatic void dooptions (lua_State *L, int n) {\n  int i;\n  for (i = 2; i <= n; i++) {\n    lua_pushvalue(L, i);  /* get option (a function) */\n    lua_pushvalue(L, -2);  /* module */\n    lua_call(L, 1, 0);\n  }\n}\n\n\nstatic void modinit (lua_State *L, const char *modname) {\n  const char *dot;\n  lua_pushvalue(L, -1);\n  lua_setfield(L, -2, \"_M\");  /* module._M = module */\n  lua_pushstring(L, modname);\n  lua_setfield(L, -2, \"_NAME\");\n  dot = strrchr(modname, '.');  /* look for last dot in module name */\n  if (dot == NULL) dot = modname;\n  else dot++;\n  /* set _PACKAGE as package name (full module name minus last part) */\n  lua_pushlstring(L, modname, dot - modname);\n  lua_setfield(L, -2, \"_PACKAGE\");\n}\n\n\nstatic int ll_module (lua_State *L) {\n  const char *modname = luaL_checkstring(L, 1);\n  int loaded = lua_gettop(L) + 1;  /* index of _LOADED table */\n  lua_getfield(L, LUA_REGISTRYINDEX, \"_LOADED\");\n  lua_getfield(L, loaded, modname);  /* get _LOADED[modname] */\n  if (!lua_istable(L, -1)) {  /* not found? */\n    lua_pop(L, 1);  /* remove previous result */\n    /* try global variable (and create one if it does not exist) */\n    if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, 1) != NULL)\n      return luaL_error(L, \"name conflict for module \" LUA_QS, modname);\n    lua_pushvalue(L, -1);\n    lua_setfield(L, loaded, modname);  /* _LOADED[modname] = new table */\n  }\n  /* check whether table already has a _NAME field */\n  lua_getfield(L, -1, \"_NAME\");\n  if (!lua_isnil(L, -1))  /* is table an initialized module? */\n    lua_pop(L, 1);\n  else {  /* no; initialize it */\n    lua_pop(L, 1);\n    modinit(L, modname);\n  }\n  lua_pushvalue(L, -1);\n  setfenv(L);\n  dooptions(L, loaded - 1);\n  return 0;\n}\n\n\nstatic int ll_seeall (lua_State *L) {\n  luaL_checktype(L, 1, LUA_TTABLE);\n  if (!lua_getmetatable(L, 1)) {\n    lua_createtable(L, 0, 1); /* create new metatable */\n    lua_pushvalue(L, -1);\n    lua_setmetatable(L, 1);\n  }\n  lua_pushvalue(L, LUA_GLOBALSINDEX);\n  lua_setfield(L, -2, \"__index\");  /* mt.__index = _G */\n  return 0;\n}\n\n\n/* }====================================================== */\n\n\n\n/* auxiliary mark (for internal use) */\n#define AUXMARK\t\t\"\\1\"\n\nstatic void setpath (lua_State *L, const char *fieldname, const char *envname,\n                                   const char *def) {\n  const char *path = getenv(envname);\n  if (path == NULL)  /* no environment variable? */\n    lua_pushstring(L, def);  /* use default */\n  else {\n    /* replace \";;\" by \";AUXMARK;\" and then AUXMARK by default path */\n    path = luaL_gsub(L, path, LUA_PATHSEP LUA_PATHSEP,\n                              LUA_PATHSEP AUXMARK LUA_PATHSEP);\n    luaL_gsub(L, path, AUXMARK, def);\n    lua_remove(L, -2);\n  }\n  setprogdir(L);\n  lua_setfield(L, -2, fieldname);\n}\n\n\nstatic const luaL_Reg pk_funcs[] = {\n  {\"loadlib\", ll_loadlib},\n  {\"seeall\", ll_seeall},\n  {NULL, NULL}\n};\n\n\nstatic const luaL_Reg ll_funcs[] = {\n  {\"module\", ll_module},\n  {\"require\", ll_require},\n  {NULL, NULL}\n};\n\n\nstatic const lua_CFunction loaders[] =\n  {loader_preload, loader_Lua, loader_C, loader_Croot, NULL};\n\n\nLUALIB_API int luaopen_package (lua_State *L) {\n  int i;\n  /* create new type _LOADLIB */\n  luaL_newmetatable(L, \"_LOADLIB\");\n  lua_pushcfunction(L, gctm);\n  lua_setfield(L, -2, \"__gc\");\n  /* create `package' table */\n  luaL_register(L, LUA_LOADLIBNAME, pk_funcs);\n#if defined(LUA_COMPAT_LOADLIB) \n  lua_getfield(L, -1, \"loadlib\");\n  lua_setfield(L, LUA_GLOBALSINDEX, \"loadlib\");\n#endif\n  lua_pushvalue(L, -1);\n  lua_replace(L, LUA_ENVIRONINDEX);\n  /* create `loaders' table */\n  lua_createtable(L, sizeof(loaders)/sizeof(loaders[0]) - 1, 0);\n  /* fill it with pre-defined loaders */\n  for (i=0; loaders[i] != NULL; i++) {\n    lua_pushcfunction(L, loaders[i]);\n    lua_rawseti(L, -2, i+1);\n  }\n  lua_setfield(L, -2, \"loaders\");  /* put it in field `loaders' */\n  setpath(L, \"path\", LUA_PATH, LUA_PATH_DEFAULT);  /* set field `path' */\n  setpath(L, \"cpath\", LUA_CPATH, LUA_CPATH_DEFAULT); /* set field `cpath' */\n  /* store config information */\n  lua_pushliteral(L, LUA_DIRSEP \"\\n\" LUA_PATHSEP \"\\n\" LUA_PATH_MARK \"\\n\"\n                     LUA_EXECDIR \"\\n\" LUA_IGMARK);\n  lua_setfield(L, -2, \"config\");\n  /* set field `loaded' */\n  luaL_findtable(L, LUA_REGISTRYINDEX, \"_LOADED\", 2);\n  lua_setfield(L, -2, \"loaded\");\n  /* set field `preload' */\n  lua_newtable(L);\n  lua_setfield(L, -2, \"preload\");\n  lua_pushvalue(L, LUA_GLOBALSINDEX);\n  luaL_register(L, NULL, ll_funcs);  /* open lib into global table */\n  lua_pop(L, 1);\n  return 1;  /* return 'package' table */\n}\n\n"
  },
  {
    "path": "deps/lua/src/lobject.c",
    "content": "/*\n** $Id: lobject.c,v 2.22.1.1 2007/12/27 13:02:25 roberto Exp $\n** Some generic functions over Lua objects\n** See Copyright Notice in lua.h\n*/\n\n#include <ctype.h>\n#include <stdarg.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#define lobject_c\n#define LUA_CORE\n\n#include \"lua.h\"\n\n#include \"ldo.h\"\n#include \"lmem.h\"\n#include \"lobject.h\"\n#include \"lstate.h\"\n#include \"lstring.h\"\n#include \"lvm.h\"\n\n\n\nconst TValue luaO_nilobject_ = {{NULL}, LUA_TNIL};\n\n\n/*\n** converts an integer to a \"floating point byte\", represented as\n** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if\n** eeeee != 0 and (xxx) otherwise.\n*/\nint luaO_int2fb (unsigned int x) {\n  int e = 0;  /* expoent */\n  while (x >= 16) {\n    x = (x+1) >> 1;\n    e++;\n  }\n  if (x < 8) return x;\n  else return ((e+1) << 3) | (cast_int(x) - 8);\n}\n\n\n/* converts back */\nint luaO_fb2int (int x) {\n  int e = (x >> 3) & 31;\n  if (e == 0) return x;\n  else return ((x & 7)+8) << (e - 1);\n}\n\n\nint luaO_log2 (unsigned int x) {\n  static const lu_byte log_2[256] = {\n    0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,\n    6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,\n    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,\n    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,\n    8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,\n    8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,\n    8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,\n    8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8\n  };\n  int l = -1;\n  while (x >= 256) { l += 8; x >>= 8; }\n  return l + log_2[x];\n\n}\n\n\nint luaO_rawequalObj (const TValue *t1, const TValue *t2) {\n  if (ttype(t1) != ttype(t2)) return 0;\n  else switch (ttype(t1)) {\n    case LUA_TNIL:\n      return 1;\n    case LUA_TNUMBER:\n      return luai_numeq(nvalue(t1), nvalue(t2));\n    case LUA_TBOOLEAN:\n      return bvalue(t1) == bvalue(t2);  /* boolean true must be 1 !! */\n    case LUA_TLIGHTUSERDATA:\n      return pvalue(t1) == pvalue(t2);\n    default:\n      lua_assert(iscollectable(t1));\n      return gcvalue(t1) == gcvalue(t2);\n  }\n}\n\n\nint luaO_str2d (const char *s, lua_Number *result) {\n  char *endptr;\n  *result = lua_str2number(s, &endptr);\n  if (endptr == s) return 0;  /* conversion failed */\n  if (*endptr == 'x' || *endptr == 'X')  /* maybe an hexadecimal constant? */\n    *result = cast_num(strtoul(s, &endptr, 16));\n  if (*endptr == '\\0') return 1;  /* most common case */\n  while (isspace(cast(unsigned char, *endptr))) endptr++;\n  if (*endptr != '\\0') return 0;  /* invalid trailing characters? */\n  return 1;\n}\n\n\n\nstatic void pushstr (lua_State *L, const char *str) {\n  setsvalue2s(L, L->top, luaS_new(L, str));\n  incr_top(L);\n}\n\n\n/* this function handles only `%d', `%c', %f, %p, and `%s' formats */\nconst char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {\n  int n = 1;\n  pushstr(L, \"\");\n  for (;;) {\n    const char *e = strchr(fmt, '%');\n    if (e == NULL) break;\n    setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt));\n    incr_top(L);\n    switch (*(e+1)) {\n      case 's': {\n        const char *s = va_arg(argp, char *);\n        if (s == NULL) s = \"(null)\";\n        pushstr(L, s);\n        break;\n      }\n      case 'c': {\n        char buff[2];\n        buff[0] = cast(char, va_arg(argp, int));\n        buff[1] = '\\0';\n        pushstr(L, buff);\n        break;\n      }\n      case 'd': {\n        setnvalue(L->top, cast_num(va_arg(argp, int)));\n        incr_top(L);\n        break;\n      }\n      case 'f': {\n        setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));\n        incr_top(L);\n        break;\n      }\n      case 'p': {\n        char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */\n        sprintf(buff, \"%p\", va_arg(argp, void *));\n        pushstr(L, buff);\n        break;\n      }\n      case '%': {\n        pushstr(L, \"%\");\n        break;\n      }\n      default: {\n        char buff[3];\n        buff[0] = '%';\n        buff[1] = *(e+1);\n        buff[2] = '\\0';\n        pushstr(L, buff);\n        break;\n      }\n    }\n    n += 2;\n    fmt = e+2;\n  }\n  pushstr(L, fmt);\n  luaV_concat(L, n+1, cast_int(L->top - L->base) - 1);\n  L->top -= n;\n  return svalue(L->top - 1);\n}\n\n\nconst char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {\n  const char *msg;\n  va_list argp;\n  va_start(argp, fmt);\n  msg = luaO_pushvfstring(L, fmt, argp);\n  va_end(argp);\n  return msg;\n}\n\n\nvoid luaO_chunkid (char *out, const char *source, size_t bufflen) {\n  if (*source == '=') {\n    strncpy(out, source+1, bufflen);  /* remove first char */\n    out[bufflen-1] = '\\0';  /* ensures null termination */\n  }\n  else {  /* out = \"source\", or \"...source\" */\n    if (*source == '@') {\n      size_t l;\n      source++;  /* skip the `@' */\n      bufflen -= sizeof(\" '...' \");\n      l = strlen(source);\n      strcpy(out, \"\");\n      if (l > bufflen) {\n        source += (l-bufflen);  /* get last part of file name */\n        strcat(out, \"...\");\n      }\n      strcat(out, source);\n    }\n    else {  /* out = [string \"string\"] */\n      size_t len = strcspn(source, \"\\n\\r\");  /* stop at first newline */\n      bufflen -= sizeof(\" [string \\\"...\\\"] \");\n      if (len > bufflen) len = bufflen;\n      strcpy(out, \"[string \\\"\");\n      if (source[len] != '\\0') {  /* must truncate? */\n        strncat(out, source, len);\n        strcat(out, \"...\");\n      }\n      else\n        strcat(out, source);\n      strcat(out, \"\\\"]\");\n    }\n  }\n}\n"
  },
  {
    "path": "deps/lua/src/lobject.h",
    "content": "/*\n** $Id: lobject.h,v 2.20.1.2 2008/08/06 13:29:48 roberto Exp $\n** Type definitions for Lua objects\n** See Copyright Notice in lua.h\n*/\n\n\n#ifndef lobject_h\n#define lobject_h\n\n\n#include <stdarg.h>\n\n\n#include \"llimits.h\"\n#include \"lua.h\"\n\n\n/* tags for values visible from Lua */\n#define LAST_TAG\tLUA_TTHREAD\n\n#define NUM_TAGS\t(LAST_TAG+1)\n\n\n/*\n** Extra tags for non-values\n*/\n#define LUA_TPROTO\t(LAST_TAG+1)\n#define LUA_TUPVAL\t(LAST_TAG+2)\n#define LUA_TDEADKEY\t(LAST_TAG+3)\n\n\n/*\n** Union of all collectable objects\n*/\ntypedef union GCObject GCObject;\n\n\n/*\n** Common Header for all collectable objects (in macro form, to be\n** included in other objects)\n*/\n#define CommonHeader\tGCObject *next; lu_byte tt; lu_byte marked\n\n\n/*\n** Common header in struct form\n*/\ntypedef struct GCheader {\n  CommonHeader;\n} GCheader;\n\n\n\n\n/*\n** Union of all Lua values\n*/\ntypedef union {\n  GCObject *gc;\n  void *p;\n  lua_Number n;\n  int b;\n} Value;\n\n\n/*\n** Tagged Values\n*/\n\n#define TValuefields\tValue value; int tt\n\ntypedef struct lua_TValue {\n  TValuefields;\n} TValue;\n\n\n/* Macros to test type */\n#define ttisnil(o)\t(ttype(o) == LUA_TNIL)\n#define ttisnumber(o)\t(ttype(o) == LUA_TNUMBER)\n#define ttisstring(o)\t(ttype(o) == LUA_TSTRING)\n#define ttistable(o)\t(ttype(o) == LUA_TTABLE)\n#define ttisfunction(o)\t(ttype(o) == LUA_TFUNCTION)\n#define ttisboolean(o)\t(ttype(o) == LUA_TBOOLEAN)\n#define ttisuserdata(o)\t(ttype(o) == LUA_TUSERDATA)\n#define ttisthread(o)\t(ttype(o) == LUA_TTHREAD)\n#define ttislightuserdata(o)\t(ttype(o) == LUA_TLIGHTUSERDATA)\n\n/* Macros to access values */\n#define ttype(o)\t((o)->tt)\n#define gcvalue(o)\tcheck_exp(iscollectable(o), (o)->value.gc)\n#define pvalue(o)\tcheck_exp(ttislightuserdata(o), (o)->value.p)\n#define nvalue(o)\tcheck_exp(ttisnumber(o), (o)->value.n)\n#define rawtsvalue(o)\tcheck_exp(ttisstring(o), &(o)->value.gc->ts)\n#define tsvalue(o)\t(&rawtsvalue(o)->tsv)\n#define rawuvalue(o)\tcheck_exp(ttisuserdata(o), &(o)->value.gc->u)\n#define uvalue(o)\t(&rawuvalue(o)->uv)\n#define clvalue(o)\tcheck_exp(ttisfunction(o), &(o)->value.gc->cl)\n#define hvalue(o)\tcheck_exp(ttistable(o), &(o)->value.gc->h)\n#define bvalue(o)\tcheck_exp(ttisboolean(o), (o)->value.b)\n#define thvalue(o)\tcheck_exp(ttisthread(o), &(o)->value.gc->th)\n\n#define l_isfalse(o)\t(ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))\n\n/*\n** for internal debug only\n*/\n#define checkconsistency(obj) \\\n  lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt))\n\n#define checkliveness(g,obj) \\\n  lua_assert(!iscollectable(obj) || \\\n  ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc)))\n\n\n/* Macros to set values */\n#define setnilvalue(obj) ((obj)->tt=LUA_TNIL)\n\n#define setnvalue(obj,x) \\\n  { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; }\n\n#define setpvalue(obj,x) \\\n  { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; }\n\n#define setbvalue(obj,x) \\\n  { TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; }\n\n#define setsvalue(L,obj,x) \\\n  { TValue *i_o=(obj); \\\n    i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TSTRING; \\\n    checkliveness(G(L),i_o); }\n\n#define setuvalue(L,obj,x) \\\n  { TValue *i_o=(obj); \\\n    i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TUSERDATA; \\\n    checkliveness(G(L),i_o); }\n\n#define setthvalue(L,obj,x) \\\n  { TValue *i_o=(obj); \\\n    i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTHREAD; \\\n    checkliveness(G(L),i_o); }\n\n#define setclvalue(L,obj,x) \\\n  { TValue *i_o=(obj); \\\n    i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TFUNCTION; \\\n    checkliveness(G(L),i_o); }\n\n#define sethvalue(L,obj,x) \\\n  { TValue *i_o=(obj); \\\n    i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTABLE; \\\n    checkliveness(G(L),i_o); }\n\n#define setptvalue(L,obj,x) \\\n  { TValue *i_o=(obj); \\\n    i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \\\n    checkliveness(G(L),i_o); }\n\n\n\n\n#define setobj(L,obj1,obj2) \\\n  { const TValue *o2=(obj2); TValue *o1=(obj1); \\\n    o1->value = o2->value; o1->tt=o2->tt; \\\n    checkliveness(G(L),o1); }\n\n\n/*\n** different types of sets, according to destination\n*/\n\n/* from stack to (same) stack */\n#define setobjs2s\tsetobj\n/* to stack (not from same stack) */\n#define setobj2s\tsetobj\n#define setsvalue2s\tsetsvalue\n#define sethvalue2s\tsethvalue\n#define setptvalue2s\tsetptvalue\n/* from table to same table */\n#define setobjt2t\tsetobj\n/* to table */\n#define setobj2t\tsetobj\n/* to new object */\n#define setobj2n\tsetobj\n#define setsvalue2n\tsetsvalue\n\n#define setttype(obj, tt) (ttype(obj) = (tt))\n\n\n#define iscollectable(o)\t(ttype(o) >= LUA_TSTRING)\n\n\n\ntypedef TValue *StkId;  /* index to stack elements */\n\n\n/*\n** String headers for string table\n*/\ntypedef union TString {\n  L_Umaxalign dummy;  /* ensures maximum alignment for strings */\n  struct {\n    CommonHeader;\n    lu_byte reserved;\n    unsigned int hash;\n    size_t len;\n  } tsv;\n} TString;\n\n\n#define getstr(ts)\tcast(const char *, (ts) + 1)\n#define svalue(o)       getstr(rawtsvalue(o))\n\n\n\ntypedef union Udata {\n  L_Umaxalign dummy;  /* ensures maximum alignment for `local' udata */\n  struct {\n    CommonHeader;\n    struct Table *metatable;\n    struct Table *env;\n    size_t len;\n  } uv;\n} Udata;\n\n\n\n\n/*\n** Function Prototypes\n*/\ntypedef struct Proto {\n  CommonHeader;\n  TValue *k;  /* constants used by the function */\n  Instruction *code;\n  struct Proto **p;  /* functions defined inside the function */\n  int *lineinfo;  /* map from opcodes to source lines */\n  struct LocVar *locvars;  /* information about local variables */\n  TString **upvalues;  /* upvalue names */\n  TString  *source;\n  int sizeupvalues;\n  int sizek;  /* size of `k' */\n  int sizecode;\n  int sizelineinfo;\n  int sizep;  /* size of `p' */\n  int sizelocvars;\n  int linedefined;\n  int lastlinedefined;\n  GCObject *gclist;\n  lu_byte nups;  /* number of upvalues */\n  lu_byte numparams;\n  lu_byte is_vararg;\n  lu_byte maxstacksize;\n} Proto;\n\n\n/* masks for new-style vararg */\n#define VARARG_HASARG\t\t1\n#define VARARG_ISVARARG\t\t2\n#define VARARG_NEEDSARG\t\t4\n\n\ntypedef struct LocVar {\n  TString *varname;\n  int startpc;  /* first point where variable is active */\n  int endpc;    /* first point where variable is dead */\n} LocVar;\n\n\n\n/*\n** Upvalues\n*/\n\ntypedef struct UpVal {\n  CommonHeader;\n  TValue *v;  /* points to stack or to its own value */\n  union {\n    TValue value;  /* the value (when closed) */\n    struct {  /* double linked list (when open) */\n      struct UpVal *prev;\n      struct UpVal *next;\n    } l;\n  } u;\n} UpVal;\n\n\n/*\n** Closures\n*/\n\n#define ClosureHeader \\\n\tCommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \\\n\tstruct Table *env\n\ntypedef struct CClosure {\n  ClosureHeader;\n  lua_CFunction f;\n  TValue upvalue[1];\n} CClosure;\n\n\ntypedef struct LClosure {\n  ClosureHeader;\n  struct Proto *p;\n  UpVal *upvals[1];\n} LClosure;\n\n\ntypedef union Closure {\n  CClosure c;\n  LClosure l;\n} Closure;\n\n\n#define iscfunction(o)\t(ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)\n#define isLfunction(o)\t(ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)\n\n\n/*\n** Tables\n*/\n\ntypedef union TKey {\n  struct {\n    TValuefields;\n    struct Node *next;  /* for chaining */\n  } nk;\n  TValue tvk;\n} TKey;\n\n\ntypedef struct Node {\n  TValue i_val;\n  TKey i_key;\n} Node;\n\n\ntypedef struct Table {\n  CommonHeader;\n  lu_byte flags;  /* 1<<p means tagmethod(p) is not present */ \n  lu_byte lsizenode;  /* log2 of size of `node' array */\n  struct Table *metatable;\n  TValue *array;  /* array part */\n  Node *node;\n  Node *lastfree;  /* any free position is before this position */\n  GCObject *gclist;\n  int sizearray;  /* size of `array' array */\n} Table;\n\n\n\n/*\n** `module' operation for hashing (size is always a power of 2)\n*/\n#define lmod(s,size) \\\n\t(check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))\n\n\n#define twoto(x)\t(1<<(x))\n#define sizenode(t)\t(twoto((t)->lsizenode))\n\n\n#define luaO_nilobject\t\t(&luaO_nilobject_)\n\nLUAI_DATA const TValue luaO_nilobject_;\n\n#define ceillog2(x)\t(luaO_log2((x)-1) + 1)\n\nLUAI_FUNC int luaO_log2 (unsigned int x);\nLUAI_FUNC int luaO_int2fb (unsigned int x);\nLUAI_FUNC int luaO_fb2int (int x);\nLUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);\nLUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);\nLUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,\n                                                       va_list argp);\nLUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);\nLUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);\n\n\n#endif\n\n"
  },
  {
    "path": "deps/lua/src/lopcodes.c",
    "content": "/*\n** $Id: lopcodes.c,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $\n** See Copyright Notice in lua.h\n*/\n\n\n#define lopcodes_c\n#define LUA_CORE\n\n\n#include \"lopcodes.h\"\n\n\n/* ORDER OP */\n\nconst char *const luaP_opnames[NUM_OPCODES+1] = {\n  \"MOVE\",\n  \"LOADK\",\n  \"LOADBOOL\",\n  \"LOADNIL\",\n  \"GETUPVAL\",\n  \"GETGLOBAL\",\n  \"GETTABLE\",\n  \"SETGLOBAL\",\n  \"SETUPVAL\",\n  \"SETTABLE\",\n  \"NEWTABLE\",\n  \"SELF\",\n  \"ADD\",\n  \"SUB\",\n  \"MUL\",\n  \"DIV\",\n  \"MOD\",\n  \"POW\",\n  \"UNM\",\n  \"NOT\",\n  \"LEN\",\n  \"CONCAT\",\n  \"JMP\",\n  \"EQ\",\n  \"LT\",\n  \"LE\",\n  \"TEST\",\n  \"TESTSET\",\n  \"CALL\",\n  \"TAILCALL\",\n  \"RETURN\",\n  \"FORLOOP\",\n  \"FORPREP\",\n  \"TFORLOOP\",\n  \"SETLIST\",\n  \"CLOSE\",\n  \"CLOSURE\",\n  \"VARARG\",\n  NULL\n};\n\n\n#define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m))\n\nconst lu_byte luaP_opmodes[NUM_OPCODES] = {\n/*       T  A    B       C     mode\t\t   opcode\t*/\n  opmode(0, 1, OpArgR, OpArgN, iABC) \t\t/* OP_MOVE */\n ,opmode(0, 1, OpArgK, OpArgN, iABx)\t\t/* OP_LOADK */\n ,opmode(0, 1, OpArgU, OpArgU, iABC)\t\t/* OP_LOADBOOL */\n ,opmode(0, 1, OpArgR, OpArgN, iABC)\t\t/* OP_LOADNIL */\n ,opmode(0, 1, OpArgU, OpArgN, iABC)\t\t/* OP_GETUPVAL */\n ,opmode(0, 1, OpArgK, OpArgN, iABx)\t\t/* OP_GETGLOBAL */\n ,opmode(0, 1, OpArgR, OpArgK, iABC)\t\t/* OP_GETTABLE */\n ,opmode(0, 0, OpArgK, OpArgN, iABx)\t\t/* OP_SETGLOBAL */\n ,opmode(0, 0, OpArgU, OpArgN, iABC)\t\t/* OP_SETUPVAL */\n ,opmode(0, 0, OpArgK, OpArgK, iABC)\t\t/* OP_SETTABLE */\n ,opmode(0, 1, OpArgU, OpArgU, iABC)\t\t/* OP_NEWTABLE */\n ,opmode(0, 1, OpArgR, OpArgK, iABC)\t\t/* OP_SELF */\n ,opmode(0, 1, OpArgK, OpArgK, iABC)\t\t/* OP_ADD */\n ,opmode(0, 1, OpArgK, OpArgK, iABC)\t\t/* OP_SUB */\n ,opmode(0, 1, OpArgK, OpArgK, iABC)\t\t/* OP_MUL */\n ,opmode(0, 1, OpArgK, OpArgK, iABC)\t\t/* OP_DIV */\n ,opmode(0, 1, OpArgK, OpArgK, iABC)\t\t/* OP_MOD */\n ,opmode(0, 1, OpArgK, OpArgK, iABC)\t\t/* OP_POW */\n ,opmode(0, 1, OpArgR, OpArgN, iABC)\t\t/* OP_UNM */\n ,opmode(0, 1, OpArgR, OpArgN, iABC)\t\t/* OP_NOT */\n ,opmode(0, 1, OpArgR, OpArgN, iABC)\t\t/* OP_LEN */\n ,opmode(0, 1, OpArgR, OpArgR, iABC)\t\t/* OP_CONCAT */\n ,opmode(0, 0, OpArgR, OpArgN, iAsBx)\t\t/* OP_JMP */\n ,opmode(1, 0, OpArgK, OpArgK, iABC)\t\t/* OP_EQ */\n ,opmode(1, 0, OpArgK, OpArgK, iABC)\t\t/* OP_LT */\n ,opmode(1, 0, OpArgK, OpArgK, iABC)\t\t/* OP_LE */\n ,opmode(1, 1, OpArgR, OpArgU, iABC)\t\t/* OP_TEST */\n ,opmode(1, 1, OpArgR, OpArgU, iABC)\t\t/* OP_TESTSET */\n ,opmode(0, 1, OpArgU, OpArgU, iABC)\t\t/* OP_CALL */\n ,opmode(0, 1, OpArgU, OpArgU, iABC)\t\t/* OP_TAILCALL */\n ,opmode(0, 0, OpArgU, OpArgN, iABC)\t\t/* OP_RETURN */\n ,opmode(0, 1, OpArgR, OpArgN, iAsBx)\t\t/* OP_FORLOOP */\n ,opmode(0, 1, OpArgR, OpArgN, iAsBx)\t\t/* OP_FORPREP */\n ,opmode(1, 0, OpArgN, OpArgU, iABC)\t\t/* OP_TFORLOOP */\n ,opmode(0, 0, OpArgU, OpArgU, iABC)\t\t/* OP_SETLIST */\n ,opmode(0, 0, OpArgN, OpArgN, iABC)\t\t/* OP_CLOSE */\n ,opmode(0, 1, OpArgU, OpArgN, iABx)\t\t/* OP_CLOSURE */\n ,opmode(0, 1, OpArgU, OpArgN, iABC)\t\t/* OP_VARARG */\n};\n\n"
  },
  {
    "path": "deps/lua/src/lopcodes.h",
    "content": "/*\n** $Id: lopcodes.h,v 1.125.1.1 2007/12/27 13:02:25 roberto Exp $\n** Opcodes for Lua virtual machine\n** See Copyright Notice in lua.h\n*/\n\n#ifndef lopcodes_h\n#define lopcodes_h\n\n#include \"llimits.h\"\n\n\n/*===========================================================================\n  We assume that instructions are unsigned numbers.\n  All instructions have an opcode in the first 6 bits.\n  Instructions can have the following fields:\n\t`A' : 8 bits\n\t`B' : 9 bits\n\t`C' : 9 bits\n\t`Bx' : 18 bits (`B' and `C' together)\n\t`sBx' : signed Bx\n\n  A signed argument is represented in excess K; that is, the number\n  value is the unsigned value minus K. K is exactly the maximum value\n  for that argument (so that -max is represented by 0, and +max is\n  represented by 2*max), which is half the maximum for the corresponding\n  unsigned argument.\n===========================================================================*/\n\n\nenum OpMode {iABC, iABx, iAsBx};  /* basic instruction format */\n\n\n/*\n** size and position of opcode arguments.\n*/\n#define SIZE_C\t\t9\n#define SIZE_B\t\t9\n#define SIZE_Bx\t\t(SIZE_C + SIZE_B)\n#define SIZE_A\t\t8\n\n#define SIZE_OP\t\t6\n\n#define POS_OP\t\t0\n#define POS_A\t\t(POS_OP + SIZE_OP)\n#define POS_C\t\t(POS_A + SIZE_A)\n#define POS_B\t\t(POS_C + SIZE_C)\n#define POS_Bx\t\tPOS_C\n\n\n/*\n** limits for opcode arguments.\n** we use (signed) int to manipulate most arguments,\n** so they must fit in LUAI_BITSINT-1 bits (-1 for sign)\n*/\n#if SIZE_Bx < LUAI_BITSINT-1\n#define MAXARG_Bx        ((1<<SIZE_Bx)-1)\n#define MAXARG_sBx        (MAXARG_Bx>>1)         /* `sBx' is signed */\n#else\n#define MAXARG_Bx        MAX_INT\n#define MAXARG_sBx        MAX_INT\n#endif\n\n\n#define MAXARG_A        ((1<<SIZE_A)-1)\n#define MAXARG_B        ((1<<SIZE_B)-1)\n#define MAXARG_C        ((1<<SIZE_C)-1)\n\n\n/* creates a mask with `n' 1 bits at position `p' */\n#define MASK1(n,p)\t((~((~(Instruction)0)<<n))<<p)\n\n/* creates a mask with `n' 0 bits at position `p' */\n#define MASK0(n,p)\t(~MASK1(n,p))\n\n/*\n** the following macros help to manipulate instructions\n*/\n\n#define GET_OPCODE(i)\t(cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0)))\n#define SET_OPCODE(i,o)\t((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \\\n\t\t((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))\n\n#define GETARG_A(i)\t(cast(int, ((i)>>POS_A) & MASK1(SIZE_A,0)))\n#define SETARG_A(i,u)\t((i) = (((i)&MASK0(SIZE_A,POS_A)) | \\\n\t\t((cast(Instruction, u)<<POS_A)&MASK1(SIZE_A,POS_A))))\n\n#define GETARG_B(i)\t(cast(int, ((i)>>POS_B) & MASK1(SIZE_B,0)))\n#define SETARG_B(i,b)\t((i) = (((i)&MASK0(SIZE_B,POS_B)) | \\\n\t\t((cast(Instruction, b)<<POS_B)&MASK1(SIZE_B,POS_B))))\n\n#define GETARG_C(i)\t(cast(int, ((i)>>POS_C) & MASK1(SIZE_C,0)))\n#define SETARG_C(i,b)\t((i) = (((i)&MASK0(SIZE_C,POS_C)) | \\\n\t\t((cast(Instruction, b)<<POS_C)&MASK1(SIZE_C,POS_C))))\n\n#define GETARG_Bx(i)\t(cast(int, ((i)>>POS_Bx) & MASK1(SIZE_Bx,0)))\n#define SETARG_Bx(i,b)\t((i) = (((i)&MASK0(SIZE_Bx,POS_Bx)) | \\\n\t\t((cast(Instruction, b)<<POS_Bx)&MASK1(SIZE_Bx,POS_Bx))))\n\n#define GETARG_sBx(i)\t(GETARG_Bx(i)-MAXARG_sBx)\n#define SETARG_sBx(i,b)\tSETARG_Bx((i),cast(unsigned int, (b)+MAXARG_sBx))\n\n\n#define CREATE_ABC(o,a,b,c)\t((cast(Instruction, o)<<POS_OP) \\\n\t\t\t| (cast(Instruction, a)<<POS_A) \\\n\t\t\t| (cast(Instruction, b)<<POS_B) \\\n\t\t\t| (cast(Instruction, c)<<POS_C))\n\n#define CREATE_ABx(o,a,bc)\t((cast(Instruction, o)<<POS_OP) \\\n\t\t\t| (cast(Instruction, a)<<POS_A) \\\n\t\t\t| (cast(Instruction, bc)<<POS_Bx))\n\n\n/*\n** Macros to operate RK indices\n*/\n\n/* this bit 1 means constant (0 means register) */\n#define BITRK\t\t(1 << (SIZE_B - 1))\n\n/* test whether value is a constant */\n#define ISK(x)\t\t((x) & BITRK)\n\n/* gets the index of the constant */\n#define INDEXK(r)\t((int)(r) & ~BITRK)\n\n#define MAXINDEXRK\t(BITRK - 1)\n\n/* code a constant index as a RK value */\n#define RKASK(x)\t((x) | BITRK)\n\n\n/*\n** invalid register that fits in 8 bits\n*/\n#define NO_REG\t\tMAXARG_A\n\n\n/*\n** R(x) - register\n** Kst(x) - constant (in constant table)\n** RK(x) == if ISK(x) then Kst(INDEXK(x)) else R(x)\n*/\n\n\n/*\n** grep \"ORDER OP\" if you change these enums\n*/\n\ntypedef enum {\n/*----------------------------------------------------------------------\nname\t\targs\tdescription\n------------------------------------------------------------------------*/\nOP_MOVE,/*\tA B\tR(A) := R(B)\t\t\t\t\t*/\nOP_LOADK,/*\tA Bx\tR(A) := Kst(Bx)\t\t\t\t\t*/\nOP_LOADBOOL,/*\tA B C\tR(A) := (Bool)B; if (C) pc++\t\t\t*/\nOP_LOADNIL,/*\tA B\tR(A) := ... := R(B) := nil\t\t\t*/\nOP_GETUPVAL,/*\tA B\tR(A) := UpValue[B]\t\t\t\t*/\n\nOP_GETGLOBAL,/*\tA Bx\tR(A) := Gbl[Kst(Bx)]\t\t\t\t*/\nOP_GETTABLE,/*\tA B C\tR(A) := R(B)[RK(C)]\t\t\t\t*/\n\nOP_SETGLOBAL,/*\tA Bx\tGbl[Kst(Bx)] := R(A)\t\t\t\t*/\nOP_SETUPVAL,/*\tA B\tUpValue[B] := R(A)\t\t\t\t*/\nOP_SETTABLE,/*\tA B C\tR(A)[RK(B)] := RK(C)\t\t\t\t*/\n\nOP_NEWTABLE,/*\tA B C\tR(A) := {} (size = B,C)\t\t\t\t*/\n\nOP_SELF,/*\tA B C\tR(A+1) := R(B); R(A) := R(B)[RK(C)]\t\t*/\n\nOP_ADD,/*\tA B C\tR(A) := RK(B) + RK(C)\t\t\t\t*/\nOP_SUB,/*\tA B C\tR(A) := RK(B) - RK(C)\t\t\t\t*/\nOP_MUL,/*\tA B C\tR(A) := RK(B) * RK(C)\t\t\t\t*/\nOP_DIV,/*\tA B C\tR(A) := RK(B) / RK(C)\t\t\t\t*/\nOP_MOD,/*\tA B C\tR(A) := RK(B) % RK(C)\t\t\t\t*/\nOP_POW,/*\tA B C\tR(A) := RK(B) ^ RK(C)\t\t\t\t*/\nOP_UNM,/*\tA B\tR(A) := -R(B)\t\t\t\t\t*/\nOP_NOT,/*\tA B\tR(A) := not R(B)\t\t\t\t*/\nOP_LEN,/*\tA B\tR(A) := length of R(B)\t\t\t\t*/\n\nOP_CONCAT,/*\tA B C\tR(A) := R(B).. ... ..R(C)\t\t\t*/\n\nOP_JMP,/*\tsBx\tpc+=sBx\t\t\t\t\t*/\n\nOP_EQ,/*\tA B C\tif ((RK(B) == RK(C)) ~= A) then pc++\t\t*/\nOP_LT,/*\tA B C\tif ((RK(B) <  RK(C)) ~= A) then pc++  \t\t*/\nOP_LE,/*\tA B C\tif ((RK(B) <= RK(C)) ~= A) then pc++  \t\t*/\n\nOP_TEST,/*\tA C\tif not (R(A) <=> C) then pc++\t\t\t*/ \nOP_TESTSET,/*\tA B C\tif (R(B) <=> C) then R(A) := R(B) else pc++\t*/ \n\nOP_CALL,/*\tA B C\tR(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */\nOP_TAILCALL,/*\tA B C\treturn R(A)(R(A+1), ... ,R(A+B-1))\t\t*/\nOP_RETURN,/*\tA B\treturn R(A), ... ,R(A+B-2)\t(see note)\t*/\n\nOP_FORLOOP,/*\tA sBx\tR(A)+=R(A+2);\n\t\t\tif R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/\nOP_FORPREP,/*\tA sBx\tR(A)-=R(A+2); pc+=sBx\t\t\t\t*/\n\nOP_TFORLOOP,/*\tA C\tR(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2)); \n                        if R(A+3) ~= nil then R(A+2)=R(A+3) else pc++\t*/ \nOP_SETLIST,/*\tA B C\tR(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B\t*/\n\nOP_CLOSE,/*\tA \tclose all variables in the stack up to (>=) R(A)*/\nOP_CLOSURE,/*\tA Bx\tR(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n))\t*/\n\nOP_VARARG/*\tA B\tR(A), R(A+1), ..., R(A+B-1) = vararg\t\t*/\n} OpCode;\n\n\n#define NUM_OPCODES\t(cast(int, OP_VARARG) + 1)\n\n\n\n/*===========================================================================\n  Notes:\n  (*) In OP_CALL, if (B == 0) then B = top. C is the number of returns - 1,\n      and can be 0: OP_CALL then sets `top' to last_result+1, so\n      next open instruction (OP_CALL, OP_RETURN, OP_SETLIST) may use `top'.\n\n  (*) In OP_VARARG, if (B == 0) then use actual number of varargs and\n      set top (like in OP_CALL with C == 0).\n\n  (*) In OP_RETURN, if (B == 0) then return up to `top'\n\n  (*) In OP_SETLIST, if (B == 0) then B = `top';\n      if (C == 0) then next `instruction' is real C\n\n  (*) For comparisons, A specifies what condition the test should accept\n      (true or false).\n\n  (*) All `skips' (pc++) assume that next instruction is a jump\n===========================================================================*/\n\n\n/*\n** masks for instruction properties. The format is:\n** bits 0-1: op mode\n** bits 2-3: C arg mode\n** bits 4-5: B arg mode\n** bit 6: instruction set register A\n** bit 7: operator is a test\n*/  \n\nenum OpArgMask {\n  OpArgN,  /* argument is not used */\n  OpArgU,  /* argument is used */\n  OpArgR,  /* argument is a register or a jump offset */\n  OpArgK   /* argument is a constant or register/constant */\n};\n\nLUAI_DATA const lu_byte luaP_opmodes[NUM_OPCODES];\n\n#define getOpMode(m)\t(cast(enum OpMode, luaP_opmodes[m] & 3))\n#define getBMode(m)\t(cast(enum OpArgMask, (luaP_opmodes[m] >> 4) & 3))\n#define getCMode(m)\t(cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3))\n#define testAMode(m)\t(luaP_opmodes[m] & (1 << 6))\n#define testTMode(m)\t(luaP_opmodes[m] & (1 << 7))\n\n\nLUAI_DATA const char *const luaP_opnames[NUM_OPCODES+1];  /* opcode names */\n\n\n/* number of list items to accumulate before a SETLIST instruction */\n#define LFIELDS_PER_FLUSH\t50\n\n\n#endif\n"
  },
  {
    "path": "deps/lua/src/loslib.c",
    "content": "/*\n** $Id: loslib.c,v 1.19.1.3 2008/01/18 16:38:18 roberto Exp $\n** Standard Operating System library\n** See Copyright Notice in lua.h\n*/\n\n\n#include <errno.h>\n#include <locale.h>\n#include <stdlib.h>\n#include <string.h>\n#include <time.h>\n\n#define loslib_c\n#define LUA_LIB\n\n#include \"lua.h\"\n\n#include \"lauxlib.h\"\n#include \"lualib.h\"\n\n\nstatic int os_pushresult (lua_State *L, int i, const char *filename) {\n  int en = errno;  /* calls to Lua API may change this value */\n  if (i) {\n    lua_pushboolean(L, 1);\n    return 1;\n  }\n  else {\n    lua_pushnil(L);\n    lua_pushfstring(L, \"%s: %s\", filename, strerror(en));\n    lua_pushinteger(L, en);\n    return 3;\n  }\n}\n\n\nstatic int os_execute (lua_State *L) {\n  lua_pushinteger(L, system(luaL_optstring(L, 1, NULL)));\n  return 1;\n}\n\n\nstatic int os_remove (lua_State *L) {\n  const char *filename = luaL_checkstring(L, 1);\n  return os_pushresult(L, remove(filename) == 0, filename);\n}\n\n\nstatic int os_rename (lua_State *L) {\n  const char *fromname = luaL_checkstring(L, 1);\n  const char *toname = luaL_checkstring(L, 2);\n  return os_pushresult(L, rename(fromname, toname) == 0, fromname);\n}\n\n\nstatic int os_tmpname (lua_State *L) {\n  char buff[LUA_TMPNAMBUFSIZE];\n  int err;\n  lua_tmpnam(buff, err);\n  if (err)\n    return luaL_error(L, \"unable to generate a unique filename\");\n  lua_pushstring(L, buff);\n  return 1;\n}\n\n\nstatic int os_getenv (lua_State *L) {\n  lua_pushstring(L, getenv(luaL_checkstring(L, 1)));  /* if NULL push nil */\n  return 1;\n}\n\n\nstatic int os_clock (lua_State *L) {\n  lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);\n  return 1;\n}\n\n\n/*\n** {======================================================\n** Time/Date operations\n** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,\n**   wday=%w+1, yday=%j, isdst=? }\n** =======================================================\n*/\n\nstatic void setfield (lua_State *L, const char *key, int value) {\n  lua_pushinteger(L, value);\n  lua_setfield(L, -2, key);\n}\n\nstatic void setboolfield (lua_State *L, const char *key, int value) {\n  if (value < 0)  /* undefined? */\n    return;  /* does not set field */\n  lua_pushboolean(L, value);\n  lua_setfield(L, -2, key);\n}\n\nstatic int getboolfield (lua_State *L, const char *key) {\n  int res;\n  lua_getfield(L, -1, key);\n  res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);\n  lua_pop(L, 1);\n  return res;\n}\n\n\nstatic int getfield (lua_State *L, const char *key, int d) {\n  int res;\n  lua_getfield(L, -1, key);\n  if (lua_isnumber(L, -1))\n    res = (int)lua_tointeger(L, -1);\n  else {\n    if (d < 0)\n      return luaL_error(L, \"field \" LUA_QS \" missing in date table\", key);\n    res = d;\n  }\n  lua_pop(L, 1);\n  return res;\n}\n\n\nstatic int os_date (lua_State *L) {\n  const char *s = luaL_optstring(L, 1, \"%c\");\n  time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));\n  struct tm *stm;\n  if (*s == '!') {  /* UTC? */\n    stm = gmtime(&t);\n    s++;  /* skip `!' */\n  }\n  else\n    stm = localtime(&t);\n  if (stm == NULL)  /* invalid date? */\n    lua_pushnil(L);\n  else if (strcmp(s, \"*t\") == 0) {\n    lua_createtable(L, 0, 9);  /* 9 = number of fields */\n    setfield(L, \"sec\", stm->tm_sec);\n    setfield(L, \"min\", stm->tm_min);\n    setfield(L, \"hour\", stm->tm_hour);\n    setfield(L, \"day\", stm->tm_mday);\n    setfield(L, \"month\", stm->tm_mon+1);\n    setfield(L, \"year\", stm->tm_year+1900);\n    setfield(L, \"wday\", stm->tm_wday+1);\n    setfield(L, \"yday\", stm->tm_yday+1);\n    setboolfield(L, \"isdst\", stm->tm_isdst);\n  }\n  else {\n    char cc[3];\n    luaL_Buffer b;\n    cc[0] = '%'; cc[2] = '\\0';\n    luaL_buffinit(L, &b);\n    for (; *s; s++) {\n      if (*s != '%' || *(s + 1) == '\\0')  /* no conversion specifier? */\n        luaL_addchar(&b, *s);\n      else {\n        size_t reslen;\n        char buff[200];  /* should be big enough for any conversion result */\n        cc[1] = *(++s);\n        reslen = strftime(buff, sizeof(buff), cc, stm);\n        luaL_addlstring(&b, buff, reslen);\n      }\n    }\n    luaL_pushresult(&b);\n  }\n  return 1;\n}\n\n\nstatic int os_time (lua_State *L) {\n  time_t t;\n  if (lua_isnoneornil(L, 1))  /* called without args? */\n    t = time(NULL);  /* get current time */\n  else {\n    struct tm ts;\n    luaL_checktype(L, 1, LUA_TTABLE);\n    lua_settop(L, 1);  /* make sure table is at the top */\n    ts.tm_sec = getfield(L, \"sec\", 0);\n    ts.tm_min = getfield(L, \"min\", 0);\n    ts.tm_hour = getfield(L, \"hour\", 12);\n    ts.tm_mday = getfield(L, \"day\", -1);\n    ts.tm_mon = getfield(L, \"month\", -1) - 1;\n    ts.tm_year = getfield(L, \"year\", -1) - 1900;\n    ts.tm_isdst = getboolfield(L, \"isdst\");\n    t = mktime(&ts);\n  }\n  if (t == (time_t)(-1))\n    lua_pushnil(L);\n  else\n    lua_pushnumber(L, (lua_Number)t);\n  return 1;\n}\n\n\nstatic int os_difftime (lua_State *L) {\n  lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),\n                             (time_t)(luaL_optnumber(L, 2, 0))));\n  return 1;\n}\n\n/* }====================================================== */\n\n\nstatic int os_setlocale (lua_State *L) {\n  static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,\n                      LC_NUMERIC, LC_TIME};\n  static const char *const catnames[] = {\"all\", \"collate\", \"ctype\", \"monetary\",\n     \"numeric\", \"time\", NULL};\n  const char *l = luaL_optstring(L, 1, NULL);\n  int op = luaL_checkoption(L, 2, \"all\", catnames);\n  lua_pushstring(L, setlocale(cat[op], l));\n  return 1;\n}\n\n\nstatic int os_exit (lua_State *L) {\n  exit(luaL_optint(L, 1, EXIT_SUCCESS));\n}\n\nstatic const luaL_Reg syslib[] = {\n  {\"clock\",     os_clock},\n  {\"date\",      os_date},\n  {\"difftime\",  os_difftime},\n  {\"execute\",   os_execute},\n  {\"exit\",      os_exit},\n  {\"getenv\",    os_getenv},\n  {\"remove\",    os_remove},\n  {\"rename\",    os_rename},\n  {\"setlocale\", os_setlocale},\n  {\"time\",      os_time},\n  {\"tmpname\",   os_tmpname},\n  {NULL, NULL}\n};\n\n/* }====================================================== */\n\n\n\nLUALIB_API int luaopen_os (lua_State *L) {\n  luaL_register(L, LUA_OSLIBNAME, syslib);\n  return 1;\n}\n\n"
  },
  {
    "path": "deps/lua/src/lparser.c",
    "content": "/*\n** $Id: lparser.c,v 2.42.1.4 2011/10/21 19:31:42 roberto Exp $\n** Lua Parser\n** See Copyright Notice in lua.h\n*/\n\n\n#include <string.h>\n\n#define lparser_c\n#define LUA_CORE\n\n#include \"lua.h\"\n\n#include \"lcode.h\"\n#include \"ldebug.h\"\n#include \"ldo.h\"\n#include \"lfunc.h\"\n#include \"llex.h\"\n#include \"lmem.h\"\n#include \"lobject.h\"\n#include \"lopcodes.h\"\n#include \"lparser.h\"\n#include \"lstate.h\"\n#include \"lstring.h\"\n#include \"ltable.h\"\n\n\n\n#define hasmultret(k)\t\t((k) == VCALL || (k) == VVARARG)\n\n#define getlocvar(fs, i)\t((fs)->f->locvars[(fs)->actvar[i]])\n\n#define luaY_checklimit(fs,v,l,m)\tif ((v)>(l)) errorlimit(fs,l,m)\n\n\n/*\n** nodes for block list (list of active blocks)\n*/\ntypedef struct BlockCnt {\n  struct BlockCnt *previous;  /* chain */\n  int breaklist;  /* list of jumps out of this loop */\n  lu_byte nactvar;  /* # active locals outside the breakable structure */\n  lu_byte upval;  /* true if some variable in the block is an upvalue */\n  lu_byte isbreakable;  /* true if `block' is a loop */\n} BlockCnt;\n\n\n\n/*\n** prototypes for recursive non-terminal functions\n*/\nstatic void chunk (LexState *ls);\nstatic void expr (LexState *ls, expdesc *v);\n\n\nstatic void anchor_token (LexState *ls) {\n  if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) {\n    TString *ts = ls->t.seminfo.ts;\n    luaX_newstring(ls, getstr(ts), ts->tsv.len);\n  }\n}\n\n\nstatic void error_expected (LexState *ls, int token) {\n  luaX_syntaxerror(ls,\n      luaO_pushfstring(ls->L, LUA_QS \" expected\", luaX_token2str(ls, token)));\n}\n\n\nstatic void errorlimit (FuncState *fs, int limit, const char *what) {\n  const char *msg = (fs->f->linedefined == 0) ?\n    luaO_pushfstring(fs->L, \"main function has more than %d %s\", limit, what) :\n    luaO_pushfstring(fs->L, \"function at line %d has more than %d %s\",\n                            fs->f->linedefined, limit, what);\n  luaX_lexerror(fs->ls, msg, 0);\n}\n\n\nstatic int testnext (LexState *ls, int c) {\n  if (ls->t.token == c) {\n    luaX_next(ls);\n    return 1;\n  }\n  else return 0;\n}\n\n\nstatic void check (LexState *ls, int c) {\n  if (ls->t.token != c)\n    error_expected(ls, c);\n}\n\nstatic void checknext (LexState *ls, int c) {\n  check(ls, c);\n  luaX_next(ls);\n}\n\n\n#define check_condition(ls,c,msg)\t{ if (!(c)) luaX_syntaxerror(ls, msg); }\n\n\n\nstatic void check_match (LexState *ls, int what, int who, int where) {\n  if (!testnext(ls, what)) {\n    if (where == ls->linenumber)\n      error_expected(ls, what);\n    else {\n      luaX_syntaxerror(ls, luaO_pushfstring(ls->L,\n             LUA_QS \" expected (to close \" LUA_QS \" at line %d)\",\n              luaX_token2str(ls, what), luaX_token2str(ls, who), where));\n    }\n  }\n}\n\n\nstatic TString *str_checkname (LexState *ls) {\n  TString *ts;\n  check(ls, TK_NAME);\n  ts = ls->t.seminfo.ts;\n  luaX_next(ls);\n  return ts;\n}\n\n\nstatic void init_exp (expdesc *e, expkind k, int i) {\n  e->f = e->t = NO_JUMP;\n  e->k = k;\n  e->u.s.info = i;\n}\n\n\nstatic void codestring (LexState *ls, expdesc *e, TString *s) {\n  init_exp(e, VK, luaK_stringK(ls->fs, s));\n}\n\n\nstatic void checkname(LexState *ls, expdesc *e) {\n  codestring(ls, e, str_checkname(ls));\n}\n\n\nstatic int registerlocalvar (LexState *ls, TString *varname) {\n  FuncState *fs = ls->fs;\n  Proto *f = fs->f;\n  int oldsize = f->sizelocvars;\n  luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars,\n                  LocVar, SHRT_MAX, \"too many local variables\");\n  while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL;\n  f->locvars[fs->nlocvars].varname = varname;\n  luaC_objbarrier(ls->L, f, varname);\n  return fs->nlocvars++;\n}\n\n\n#define new_localvarliteral(ls,v,n) \\\n  new_localvar(ls, luaX_newstring(ls, \"\" v, (sizeof(v)/sizeof(char))-1), n)\n\n\nstatic void new_localvar (LexState *ls, TString *name, int n) {\n  FuncState *fs = ls->fs;\n  luaY_checklimit(fs, fs->nactvar+n+1, LUAI_MAXVARS, \"local variables\");\n  fs->actvar[fs->nactvar+n] = cast(unsigned short, registerlocalvar(ls, name));\n}\n\n\nstatic void adjustlocalvars (LexState *ls, int nvars) {\n  FuncState *fs = ls->fs;\n  fs->nactvar = cast_byte(fs->nactvar + nvars);\n  for (; nvars; nvars--) {\n    getlocvar(fs, fs->nactvar - nvars).startpc = fs->pc;\n  }\n}\n\n\nstatic void removevars (LexState *ls, int tolevel) {\n  FuncState *fs = ls->fs;\n  while (fs->nactvar > tolevel)\n    getlocvar(fs, --fs->nactvar).endpc = fs->pc;\n}\n\n\nstatic int indexupvalue (FuncState *fs, TString *name, expdesc *v) {\n  int i;\n  Proto *f = fs->f;\n  int oldsize = f->sizeupvalues;\n  for (i=0; i<f->nups; i++) {\n    if (fs->upvalues[i].k == v->k && fs->upvalues[i].info == v->u.s.info) {\n      lua_assert(f->upvalues[i] == name);\n      return i;\n    }\n  }\n  /* new one */\n  luaY_checklimit(fs, f->nups + 1, LUAI_MAXUPVALUES, \"upvalues\");\n  luaM_growvector(fs->L, f->upvalues, f->nups, f->sizeupvalues,\n                  TString *, MAX_INT, \"\");\n  while (oldsize < f->sizeupvalues) f->upvalues[oldsize++] = NULL;\n  f->upvalues[f->nups] = name;\n  luaC_objbarrier(fs->L, f, name);\n  lua_assert(v->k == VLOCAL || v->k == VUPVAL);\n  fs->upvalues[f->nups].k = cast_byte(v->k);\n  fs->upvalues[f->nups].info = cast_byte(v->u.s.info);\n  return f->nups++;\n}\n\n\nstatic int searchvar (FuncState *fs, TString *n) {\n  int i;\n  for (i=fs->nactvar-1; i >= 0; i--) {\n    if (n == getlocvar(fs, i).varname)\n      return i;\n  }\n  return -1;  /* not found */\n}\n\n\nstatic void markupval (FuncState *fs, int level) {\n  BlockCnt *bl = fs->bl;\n  while (bl && bl->nactvar > level) bl = bl->previous;\n  if (bl) bl->upval = 1;\n}\n\n\nstatic int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) {\n  if (fs == NULL) {  /* no more levels? */\n    init_exp(var, VGLOBAL, NO_REG);  /* default is global variable */\n    return VGLOBAL;\n  }\n  else {\n    int v = searchvar(fs, n);  /* look up at current level */\n    if (v >= 0) {\n      init_exp(var, VLOCAL, v);\n      if (!base)\n        markupval(fs, v);  /* local will be used as an upval */\n      return VLOCAL;\n    }\n    else {  /* not found at current level; try upper one */\n      if (singlevaraux(fs->prev, n, var, 0) == VGLOBAL)\n        return VGLOBAL;\n      var->u.s.info = indexupvalue(fs, n, var);  /* else was LOCAL or UPVAL */\n      var->k = VUPVAL;  /* upvalue in this level */\n      return VUPVAL;\n    }\n  }\n}\n\n\nstatic void singlevar (LexState *ls, expdesc *var) {\n  TString *varname = str_checkname(ls);\n  FuncState *fs = ls->fs;\n  if (singlevaraux(fs, varname, var, 1) == VGLOBAL)\n    var->u.s.info = luaK_stringK(fs, varname);  /* info points to global name */\n}\n\n\nstatic void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) {\n  FuncState *fs = ls->fs;\n  int extra = nvars - nexps;\n  if (hasmultret(e->k)) {\n    extra++;  /* includes call itself */\n    if (extra < 0) extra = 0;\n    luaK_setreturns(fs, e, extra);  /* last exp. provides the difference */\n    if (extra > 1) luaK_reserveregs(fs, extra-1);\n  }\n  else {\n    if (e->k != VVOID) luaK_exp2nextreg(fs, e);  /* close last expression */\n    if (extra > 0) {\n      int reg = fs->freereg;\n      luaK_reserveregs(fs, extra);\n      luaK_nil(fs, reg, extra);\n    }\n  }\n}\n\n\nstatic void enterlevel (LexState *ls) {\n  if (++ls->L->nCcalls > LUAI_MAXCCALLS)\n\tluaX_lexerror(ls, \"chunk has too many syntax levels\", 0);\n}\n\n\n#define leavelevel(ls)\t((ls)->L->nCcalls--)\n\n\nstatic void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isbreakable) {\n  bl->breaklist = NO_JUMP;\n  bl->isbreakable = isbreakable;\n  bl->nactvar = fs->nactvar;\n  bl->upval = 0;\n  bl->previous = fs->bl;\n  fs->bl = bl;\n  lua_assert(fs->freereg == fs->nactvar);\n}\n\n\nstatic void leaveblock (FuncState *fs) {\n  BlockCnt *bl = fs->bl;\n  fs->bl = bl->previous;\n  removevars(fs->ls, bl->nactvar);\n  if (bl->upval)\n    luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);\n  /* a block either controls scope or breaks (never both) */\n  lua_assert(!bl->isbreakable || !bl->upval);\n  lua_assert(bl->nactvar == fs->nactvar);\n  fs->freereg = fs->nactvar;  /* free registers */\n  luaK_patchtohere(fs, bl->breaklist);\n}\n\n\nstatic void pushclosure (LexState *ls, FuncState *func, expdesc *v) {\n  FuncState *fs = ls->fs;\n  Proto *f = fs->f;\n  int oldsize = f->sizep;\n  int i;\n  luaM_growvector(ls->L, f->p, fs->np, f->sizep, Proto *,\n                  MAXARG_Bx, \"constant table overflow\");\n  while (oldsize < f->sizep) f->p[oldsize++] = NULL;\n  f->p[fs->np++] = func->f;\n  luaC_objbarrier(ls->L, f, func->f);\n  init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1));\n  for (i=0; i<func->f->nups; i++) {\n    OpCode o = (func->upvalues[i].k == VLOCAL) ? OP_MOVE : OP_GETUPVAL;\n    luaK_codeABC(fs, o, 0, func->upvalues[i].info, 0);\n  }\n}\n\n\nstatic void open_func (LexState *ls, FuncState *fs) {\n  lua_State *L = ls->L;\n  Proto *f = luaF_newproto(L);\n  fs->f = f;\n  fs->prev = ls->fs;  /* linked list of funcstates */\n  fs->ls = ls;\n  fs->L = L;\n  ls->fs = fs;\n  fs->pc = 0;\n  fs->lasttarget = -1;\n  fs->jpc = NO_JUMP;\n  fs->freereg = 0;\n  fs->nk = 0;\n  fs->np = 0;\n  fs->nlocvars = 0;\n  fs->nactvar = 0;\n  fs->bl = NULL;\n  f->source = ls->source;\n  f->maxstacksize = 2;  /* registers 0/1 are always valid */\n  fs->h = luaH_new(L, 0, 0);\n  /* anchor table of constants and prototype (to avoid being collected) */\n  sethvalue2s(L, L->top, fs->h);\n  incr_top(L);\n  setptvalue2s(L, L->top, f);\n  incr_top(L);\n}\n\n\nstatic void close_func (LexState *ls) {\n  lua_State *L = ls->L;\n  FuncState *fs = ls->fs;\n  Proto *f = fs->f;\n  removevars(ls, 0);\n  luaK_ret(fs, 0, 0);  /* final return */\n  luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction);\n  f->sizecode = fs->pc;\n  luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int);\n  f->sizelineinfo = fs->pc;\n  luaM_reallocvector(L, f->k, f->sizek, fs->nk, TValue);\n  f->sizek = fs->nk;\n  luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *);\n  f->sizep = fs->np;\n  luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar);\n  f->sizelocvars = fs->nlocvars;\n  luaM_reallocvector(L, f->upvalues, f->sizeupvalues, f->nups, TString *);\n  f->sizeupvalues = f->nups;\n  lua_assert(luaG_checkcode(f));\n  lua_assert(fs->bl == NULL);\n  ls->fs = fs->prev;\n  /* last token read was anchored in defunct function; must reanchor it */\n  if (fs) anchor_token(ls);\n  L->top -= 2;  /* remove table and prototype from the stack */\n}\n\n\nProto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) {\n  struct LexState lexstate;\n  struct FuncState funcstate;\n  lexstate.buff = buff;\n  luaX_setinput(L, &lexstate, z, luaS_new(L, name));\n  open_func(&lexstate, &funcstate);\n  funcstate.f->is_vararg = VARARG_ISVARARG;  /* main func. is always vararg */\n  luaX_next(&lexstate);  /* read first token */\n  chunk(&lexstate);\n  check(&lexstate, TK_EOS);\n  close_func(&lexstate);\n  lua_assert(funcstate.prev == NULL);\n  lua_assert(funcstate.f->nups == 0);\n  lua_assert(lexstate.fs == NULL);\n  return funcstate.f;\n}\n\n\n\n/*============================================================*/\n/* GRAMMAR RULES */\n/*============================================================*/\n\n\nstatic void field (LexState *ls, expdesc *v) {\n  /* field -> ['.' | ':'] NAME */\n  FuncState *fs = ls->fs;\n  expdesc key;\n  luaK_exp2anyreg(fs, v);\n  luaX_next(ls);  /* skip the dot or colon */\n  checkname(ls, &key);\n  luaK_indexed(fs, v, &key);\n}\n\n\nstatic void yindex (LexState *ls, expdesc *v) {\n  /* index -> '[' expr ']' */\n  luaX_next(ls);  /* skip the '[' */\n  expr(ls, v);\n  luaK_exp2val(ls->fs, v);\n  checknext(ls, ']');\n}\n\n\n/*\n** {======================================================================\n** Rules for Constructors\n** =======================================================================\n*/\n\n\nstruct ConsControl {\n  expdesc v;  /* last list item read */\n  expdesc *t;  /* table descriptor */\n  int nh;  /* total number of `record' elements */\n  int na;  /* total number of array elements */\n  int tostore;  /* number of array elements pending to be stored */\n};\n\n\nstatic void recfield (LexState *ls, struct ConsControl *cc) {\n  /* recfield -> (NAME | `['exp1`]') = exp1 */\n  FuncState *fs = ls->fs;\n  int reg = ls->fs->freereg;\n  expdesc key, val;\n  int rkkey;\n  if (ls->t.token == TK_NAME) {\n    luaY_checklimit(fs, cc->nh, MAX_INT, \"items in a constructor\");\n    checkname(ls, &key);\n  }\n  else  /* ls->t.token == '[' */\n    yindex(ls, &key);\n  cc->nh++;\n  checknext(ls, '=');\n  rkkey = luaK_exp2RK(fs, &key);\n  expr(ls, &val);\n  luaK_codeABC(fs, OP_SETTABLE, cc->t->u.s.info, rkkey, luaK_exp2RK(fs, &val));\n  fs->freereg = reg;  /* free registers */\n}\n\n\nstatic void closelistfield (FuncState *fs, struct ConsControl *cc) {\n  if (cc->v.k == VVOID) return;  /* there is no list item */\n  luaK_exp2nextreg(fs, &cc->v);\n  cc->v.k = VVOID;\n  if (cc->tostore == LFIELDS_PER_FLUSH) {\n    luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore);  /* flush */\n    cc->tostore = 0;  /* no more items pending */\n  }\n}\n\n\nstatic void lastlistfield (FuncState *fs, struct ConsControl *cc) {\n  if (cc->tostore == 0) return;\n  if (hasmultret(cc->v.k)) {\n    luaK_setmultret(fs, &cc->v);\n    luaK_setlist(fs, cc->t->u.s.info, cc->na, LUA_MULTRET);\n    cc->na--;  /* do not count last expression (unknown number of elements) */\n  }\n  else {\n    if (cc->v.k != VVOID)\n      luaK_exp2nextreg(fs, &cc->v);\n    luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore);\n  }\n}\n\n\nstatic void listfield (LexState *ls, struct ConsControl *cc) {\n  expr(ls, &cc->v);\n  luaY_checklimit(ls->fs, cc->na, MAX_INT, \"items in a constructor\");\n  cc->na++;\n  cc->tostore++;\n}\n\n\nstatic void constructor (LexState *ls, expdesc *t) {\n  /* constructor -> ?? */\n  FuncState *fs = ls->fs;\n  int line = ls->linenumber;\n  int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0);\n  struct ConsControl cc;\n  cc.na = cc.nh = cc.tostore = 0;\n  cc.t = t;\n  init_exp(t, VRELOCABLE, pc);\n  init_exp(&cc.v, VVOID, 0);  /* no value (yet) */\n  luaK_exp2nextreg(ls->fs, t);  /* fix it at stack top (for gc) */\n  checknext(ls, '{');\n  do {\n    lua_assert(cc.v.k == VVOID || cc.tostore > 0);\n    if (ls->t.token == '}') break;\n    closelistfield(fs, &cc);\n    switch(ls->t.token) {\n      case TK_NAME: {  /* may be listfields or recfields */\n        luaX_lookahead(ls);\n        if (ls->lookahead.token != '=')  /* expression? */\n          listfield(ls, &cc);\n        else\n          recfield(ls, &cc);\n        break;\n      }\n      case '[': {  /* constructor_item -> recfield */\n        recfield(ls, &cc);\n        break;\n      }\n      default: {  /* constructor_part -> listfield */\n        listfield(ls, &cc);\n        break;\n      }\n    }\n  } while (testnext(ls, ',') || testnext(ls, ';'));\n  check_match(ls, '}', '{', line);\n  lastlistfield(fs, &cc);\n  SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */\n  SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh));  /* set initial table size */\n}\n\n/* }====================================================================== */\n\n\n\nstatic void parlist (LexState *ls) {\n  /* parlist -> [ param { `,' param } ] */\n  FuncState *fs = ls->fs;\n  Proto *f = fs->f;\n  int nparams = 0;\n  f->is_vararg = 0;\n  if (ls->t.token != ')') {  /* is `parlist' not empty? */\n    do {\n      switch (ls->t.token) {\n        case TK_NAME: {  /* param -> NAME */\n          new_localvar(ls, str_checkname(ls), nparams++);\n          break;\n        }\n        case TK_DOTS: {  /* param -> `...' */\n          luaX_next(ls);\n#if defined(LUA_COMPAT_VARARG)\n          /* use `arg' as default name */\n          new_localvarliteral(ls, \"arg\", nparams++);\n          f->is_vararg = VARARG_HASARG | VARARG_NEEDSARG;\n#endif\n          f->is_vararg |= VARARG_ISVARARG;\n          break;\n        }\n        default: luaX_syntaxerror(ls, \"<name> or \" LUA_QL(\"...\") \" expected\");\n      }\n    } while (!f->is_vararg && testnext(ls, ','));\n  }\n  adjustlocalvars(ls, nparams);\n  f->numparams = cast_byte(fs->nactvar - (f->is_vararg & VARARG_HASARG));\n  luaK_reserveregs(fs, fs->nactvar);  /* reserve register for parameters */\n}\n\n\nstatic void body (LexState *ls, expdesc *e, int needself, int line) {\n  /* body ->  `(' parlist `)' chunk END */\n  FuncState new_fs;\n  open_func(ls, &new_fs);\n  new_fs.f->linedefined = line;\n  checknext(ls, '(');\n  if (needself) {\n    new_localvarliteral(ls, \"self\", 0);\n    adjustlocalvars(ls, 1);\n  }\n  parlist(ls);\n  checknext(ls, ')');\n  chunk(ls);\n  new_fs.f->lastlinedefined = ls->linenumber;\n  check_match(ls, TK_END, TK_FUNCTION, line);\n  close_func(ls);\n  pushclosure(ls, &new_fs, e);\n}\n\n\nstatic int explist1 (LexState *ls, expdesc *v) {\n  /* explist1 -> expr { `,' expr } */\n  int n = 1;  /* at least one expression */\n  expr(ls, v);\n  while (testnext(ls, ',')) {\n    luaK_exp2nextreg(ls->fs, v);\n    expr(ls, v);\n    n++;\n  }\n  return n;\n}\n\n\nstatic void funcargs (LexState *ls, expdesc *f) {\n  FuncState *fs = ls->fs;\n  expdesc args;\n  int base, nparams;\n  int line = ls->linenumber;\n  switch (ls->t.token) {\n    case '(': {  /* funcargs -> `(' [ explist1 ] `)' */\n      if (line != ls->lastline)\n        luaX_syntaxerror(ls,\"ambiguous syntax (function call x new statement)\");\n      luaX_next(ls);\n      if (ls->t.token == ')')  /* arg list is empty? */\n        args.k = VVOID;\n      else {\n        explist1(ls, &args);\n        luaK_setmultret(fs, &args);\n      }\n      check_match(ls, ')', '(', line);\n      break;\n    }\n    case '{': {  /* funcargs -> constructor */\n      constructor(ls, &args);\n      break;\n    }\n    case TK_STRING: {  /* funcargs -> STRING */\n      codestring(ls, &args, ls->t.seminfo.ts);\n      luaX_next(ls);  /* must use `seminfo' before `next' */\n      break;\n    }\n    default: {\n      luaX_syntaxerror(ls, \"function arguments expected\");\n      return;\n    }\n  }\n  lua_assert(f->k == VNONRELOC);\n  base = f->u.s.info;  /* base register for call */\n  if (hasmultret(args.k))\n    nparams = LUA_MULTRET;  /* open call */\n  else {\n    if (args.k != VVOID)\n      luaK_exp2nextreg(fs, &args);  /* close last argument */\n    nparams = fs->freereg - (base+1);\n  }\n  init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2));\n  luaK_fixline(fs, line);\n  fs->freereg = base+1;  /* call remove function and arguments and leaves\n                            (unless changed) one result */\n}\n\n\n\n\n/*\n** {======================================================================\n** Expression parsing\n** =======================================================================\n*/\n\n\nstatic void prefixexp (LexState *ls, expdesc *v) {\n  /* prefixexp -> NAME | '(' expr ')' */\n  switch (ls->t.token) {\n    case '(': {\n      int line = ls->linenumber;\n      luaX_next(ls);\n      expr(ls, v);\n      check_match(ls, ')', '(', line);\n      luaK_dischargevars(ls->fs, v);\n      return;\n    }\n    case TK_NAME: {\n      singlevar(ls, v);\n      return;\n    }\n    default: {\n      luaX_syntaxerror(ls, \"unexpected symbol\");\n      return;\n    }\n  }\n}\n\n\nstatic void primaryexp (LexState *ls, expdesc *v) {\n  /* primaryexp ->\n        prefixexp { `.' NAME | `[' exp `]' | `:' NAME funcargs | funcargs } */\n  FuncState *fs = ls->fs;\n  prefixexp(ls, v);\n  for (;;) {\n    switch (ls->t.token) {\n      case '.': {  /* field */\n        field(ls, v);\n        break;\n      }\n      case '[': {  /* `[' exp1 `]' */\n        expdesc key;\n        luaK_exp2anyreg(fs, v);\n        yindex(ls, &key);\n        luaK_indexed(fs, v, &key);\n        break;\n      }\n      case ':': {  /* `:' NAME funcargs */\n        expdesc key;\n        luaX_next(ls);\n        checkname(ls, &key);\n        luaK_self(fs, v, &key);\n        funcargs(ls, v);\n        break;\n      }\n      case '(': case TK_STRING: case '{': {  /* funcargs */\n        luaK_exp2nextreg(fs, v);\n        funcargs(ls, v);\n        break;\n      }\n      default: return;\n    }\n  }\n}\n\n\nstatic void simpleexp (LexState *ls, expdesc *v) {\n  /* simpleexp -> NUMBER | STRING | NIL | true | false | ... |\n                  constructor | FUNCTION body | primaryexp */\n  switch (ls->t.token) {\n    case TK_NUMBER: {\n      init_exp(v, VKNUM, 0);\n      v->u.nval = ls->t.seminfo.r;\n      break;\n    }\n    case TK_STRING: {\n      codestring(ls, v, ls->t.seminfo.ts);\n      break;\n    }\n    case TK_NIL: {\n      init_exp(v, VNIL, 0);\n      break;\n    }\n    case TK_TRUE: {\n      init_exp(v, VTRUE, 0);\n      break;\n    }\n    case TK_FALSE: {\n      init_exp(v, VFALSE, 0);\n      break;\n    }\n    case TK_DOTS: {  /* vararg */\n      FuncState *fs = ls->fs;\n      check_condition(ls, fs->f->is_vararg,\n                      \"cannot use \" LUA_QL(\"...\") \" outside a vararg function\");\n      fs->f->is_vararg &= ~VARARG_NEEDSARG;  /* don't need 'arg' */\n      init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0));\n      break;\n    }\n    case '{': {  /* constructor */\n      constructor(ls, v);\n      return;\n    }\n    case TK_FUNCTION: {\n      luaX_next(ls);\n      body(ls, v, 0, ls->linenumber);\n      return;\n    }\n    default: {\n      primaryexp(ls, v);\n      return;\n    }\n  }\n  luaX_next(ls);\n}\n\n\nstatic UnOpr getunopr (int op) {\n  switch (op) {\n    case TK_NOT: return OPR_NOT;\n    case '-': return OPR_MINUS;\n    case '#': return OPR_LEN;\n    default: return OPR_NOUNOPR;\n  }\n}\n\n\nstatic BinOpr getbinopr (int op) {\n  switch (op) {\n    case '+': return OPR_ADD;\n    case '-': return OPR_SUB;\n    case '*': return OPR_MUL;\n    case '/': return OPR_DIV;\n    case '%': return OPR_MOD;\n    case '^': return OPR_POW;\n    case TK_CONCAT: return OPR_CONCAT;\n    case TK_NE: return OPR_NE;\n    case TK_EQ: return OPR_EQ;\n    case '<': return OPR_LT;\n    case TK_LE: return OPR_LE;\n    case '>': return OPR_GT;\n    case TK_GE: return OPR_GE;\n    case TK_AND: return OPR_AND;\n    case TK_OR: return OPR_OR;\n    default: return OPR_NOBINOPR;\n  }\n}\n\n\nstatic const struct {\n  lu_byte left;  /* left priority for each binary operator */\n  lu_byte right; /* right priority */\n} priority[] = {  /* ORDER OPR */\n   {6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7},  /* `+' `-' `/' `%' */\n   {10, 9}, {5, 4},                 /* power and concat (right associative) */\n   {3, 3}, {3, 3},                  /* equality and inequality */\n   {3, 3}, {3, 3}, {3, 3}, {3, 3},  /* order */\n   {2, 2}, {1, 1}                   /* logical (and/or) */\n};\n\n#define UNARY_PRIORITY\t8  /* priority for unary operators */\n\n\n/*\n** subexpr -> (simpleexp | unop subexpr) { binop subexpr }\n** where `binop' is any binary operator with a priority higher than `limit'\n*/\nstatic BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) {\n  BinOpr op;\n  UnOpr uop;\n  enterlevel(ls);\n  uop = getunopr(ls->t.token);\n  if (uop != OPR_NOUNOPR) {\n    luaX_next(ls);\n    subexpr(ls, v, UNARY_PRIORITY);\n    luaK_prefix(ls->fs, uop, v);\n  }\n  else simpleexp(ls, v);\n  /* expand while operators have priorities higher than `limit' */\n  op = getbinopr(ls->t.token);\n  while (op != OPR_NOBINOPR && priority[op].left > limit) {\n    expdesc v2;\n    BinOpr nextop;\n    luaX_next(ls);\n    luaK_infix(ls->fs, op, v);\n    /* read sub-expression with higher priority */\n    nextop = subexpr(ls, &v2, priority[op].right);\n    luaK_posfix(ls->fs, op, v, &v2);\n    op = nextop;\n  }\n  leavelevel(ls);\n  return op;  /* return first untreated operator */\n}\n\n\nstatic void expr (LexState *ls, expdesc *v) {\n  subexpr(ls, v, 0);\n}\n\n/* }==================================================================== */\n\n\n\n/*\n** {======================================================================\n** Rules for Statements\n** =======================================================================\n*/\n\n\nstatic int block_follow (int token) {\n  switch (token) {\n    case TK_ELSE: case TK_ELSEIF: case TK_END:\n    case TK_UNTIL: case TK_EOS:\n      return 1;\n    default: return 0;\n  }\n}\n\n\nstatic void block (LexState *ls) {\n  /* block -> chunk */\n  FuncState *fs = ls->fs;\n  BlockCnt bl;\n  enterblock(fs, &bl, 0);\n  chunk(ls);\n  lua_assert(bl.breaklist == NO_JUMP);\n  leaveblock(fs);\n}\n\n\n/*\n** structure to chain all variables in the left-hand side of an\n** assignment\n*/\nstruct LHS_assign {\n  struct LHS_assign *prev;\n  expdesc v;  /* variable (global, local, upvalue, or indexed) */\n};\n\n\n/*\n** check whether, in an assignment to a local variable, the local variable\n** is needed in a previous assignment (to a table). If so, save original\n** local value in a safe place and use this safe copy in the previous\n** assignment.\n*/\nstatic void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) {\n  FuncState *fs = ls->fs;\n  int extra = fs->freereg;  /* eventual position to save local variable */\n  int conflict = 0;\n  for (; lh; lh = lh->prev) {\n    if (lh->v.k == VINDEXED) {\n      if (lh->v.u.s.info == v->u.s.info) {  /* conflict? */\n        conflict = 1;\n        lh->v.u.s.info = extra;  /* previous assignment will use safe copy */\n      }\n      if (lh->v.u.s.aux == v->u.s.info) {  /* conflict? */\n        conflict = 1;\n        lh->v.u.s.aux = extra;  /* previous assignment will use safe copy */\n      }\n    }\n  }\n  if (conflict) {\n    luaK_codeABC(fs, OP_MOVE, fs->freereg, v->u.s.info, 0);  /* make copy */\n    luaK_reserveregs(fs, 1);\n  }\n}\n\n\nstatic void assignment (LexState *ls, struct LHS_assign *lh, int nvars) {\n  expdesc e;\n  check_condition(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED,\n                      \"syntax error\");\n  if (testnext(ls, ',')) {  /* assignment -> `,' primaryexp assignment */\n    struct LHS_assign nv;\n    nv.prev = lh;\n    primaryexp(ls, &nv.v);\n    if (nv.v.k == VLOCAL)\n      check_conflict(ls, lh, &nv.v);\n    luaY_checklimit(ls->fs, nvars, LUAI_MAXCCALLS - ls->L->nCcalls,\n                    \"variables in assignment\");\n    assignment(ls, &nv, nvars+1);\n  }\n  else {  /* assignment -> `=' explist1 */\n    int nexps;\n    checknext(ls, '=');\n    nexps = explist1(ls, &e);\n    if (nexps != nvars) {\n      adjust_assign(ls, nvars, nexps, &e);\n      if (nexps > nvars)\n        ls->fs->freereg -= nexps - nvars;  /* remove extra values */\n    }\n    else {\n      luaK_setoneret(ls->fs, &e);  /* close last expression */\n      luaK_storevar(ls->fs, &lh->v, &e);\n      return;  /* avoid default */\n    }\n  }\n  init_exp(&e, VNONRELOC, ls->fs->freereg-1);  /* default assignment */\n  luaK_storevar(ls->fs, &lh->v, &e);\n}\n\n\nstatic int cond (LexState *ls) {\n  /* cond -> exp */\n  expdesc v;\n  expr(ls, &v);  /* read condition */\n  if (v.k == VNIL) v.k = VFALSE;  /* `falses' are all equal here */\n  luaK_goiftrue(ls->fs, &v);\n  return v.f;\n}\n\n\nstatic void breakstat (LexState *ls) {\n  FuncState *fs = ls->fs;\n  BlockCnt *bl = fs->bl;\n  int upval = 0;\n  while (bl && !bl->isbreakable) {\n    upval |= bl->upval;\n    bl = bl->previous;\n  }\n  if (!bl)\n    luaX_syntaxerror(ls, \"no loop to break\");\n  if (upval)\n    luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);\n  luaK_concat(fs, &bl->breaklist, luaK_jump(fs));\n}\n\n\nstatic void whilestat (LexState *ls, int line) {\n  /* whilestat -> WHILE cond DO block END */\n  FuncState *fs = ls->fs;\n  int whileinit;\n  int condexit;\n  BlockCnt bl;\n  luaX_next(ls);  /* skip WHILE */\n  whileinit = luaK_getlabel(fs);\n  condexit = cond(ls);\n  enterblock(fs, &bl, 1);\n  checknext(ls, TK_DO);\n  block(ls);\n  luaK_patchlist(fs, luaK_jump(fs), whileinit);\n  check_match(ls, TK_END, TK_WHILE, line);\n  leaveblock(fs);\n  luaK_patchtohere(fs, condexit);  /* false conditions finish the loop */\n}\n\n\nstatic void repeatstat (LexState *ls, int line) {\n  /* repeatstat -> REPEAT block UNTIL cond */\n  int condexit;\n  FuncState *fs = ls->fs;\n  int repeat_init = luaK_getlabel(fs);\n  BlockCnt bl1, bl2;\n  enterblock(fs, &bl1, 1);  /* loop block */\n  enterblock(fs, &bl2, 0);  /* scope block */\n  luaX_next(ls);  /* skip REPEAT */\n  chunk(ls);\n  check_match(ls, TK_UNTIL, TK_REPEAT, line);\n  condexit = cond(ls);  /* read condition (inside scope block) */\n  if (!bl2.upval) {  /* no upvalues? */\n    leaveblock(fs);  /* finish scope */\n    luaK_patchlist(ls->fs, condexit, repeat_init);  /* close the loop */\n  }\n  else {  /* complete semantics when there are upvalues */\n    breakstat(ls);  /* if condition then break */\n    luaK_patchtohere(ls->fs, condexit);  /* else... */\n    leaveblock(fs);  /* finish scope... */\n    luaK_patchlist(ls->fs, luaK_jump(fs), repeat_init);  /* and repeat */\n  }\n  leaveblock(fs);  /* finish loop */\n}\n\n\nstatic int exp1 (LexState *ls) {\n  expdesc e;\n  int k;\n  expr(ls, &e);\n  k = e.k;\n  luaK_exp2nextreg(ls->fs, &e);\n  return k;\n}\n\n\nstatic void forbody (LexState *ls, int base, int line, int nvars, int isnum) {\n  /* forbody -> DO block */\n  BlockCnt bl;\n  FuncState *fs = ls->fs;\n  int prep, endfor;\n  adjustlocalvars(ls, 3);  /* control variables */\n  checknext(ls, TK_DO);\n  prep = isnum ? luaK_codeAsBx(fs, OP_FORPREP, base, NO_JUMP) : luaK_jump(fs);\n  enterblock(fs, &bl, 0);  /* scope for declared variables */\n  adjustlocalvars(ls, nvars);\n  luaK_reserveregs(fs, nvars);\n  block(ls);\n  leaveblock(fs);  /* end of scope for declared variables */\n  luaK_patchtohere(fs, prep);\n  endfor = (isnum) ? luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP) :\n                     luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars);\n  luaK_fixline(fs, line);  /* pretend that `OP_FOR' starts the loop */\n  luaK_patchlist(fs, (isnum ? endfor : luaK_jump(fs)), prep + 1);\n}\n\n\nstatic void fornum (LexState *ls, TString *varname, int line) {\n  /* fornum -> NAME = exp1,exp1[,exp1] forbody */\n  FuncState *fs = ls->fs;\n  int base = fs->freereg;\n  new_localvarliteral(ls, \"(for index)\", 0);\n  new_localvarliteral(ls, \"(for limit)\", 1);\n  new_localvarliteral(ls, \"(for step)\", 2);\n  new_localvar(ls, varname, 3);\n  checknext(ls, '=');\n  exp1(ls);  /* initial value */\n  checknext(ls, ',');\n  exp1(ls);  /* limit */\n  if (testnext(ls, ','))\n    exp1(ls);  /* optional step */\n  else {  /* default step = 1 */\n    luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));\n    luaK_reserveregs(fs, 1);\n  }\n  forbody(ls, base, line, 1, 1);\n}\n\n\nstatic void forlist (LexState *ls, TString *indexname) {\n  /* forlist -> NAME {,NAME} IN explist1 forbody */\n  FuncState *fs = ls->fs;\n  expdesc e;\n  int nvars = 0;\n  int line;\n  int base = fs->freereg;\n  /* create control variables */\n  new_localvarliteral(ls, \"(for generator)\", nvars++);\n  new_localvarliteral(ls, \"(for state)\", nvars++);\n  new_localvarliteral(ls, \"(for control)\", nvars++);\n  /* create declared variables */\n  new_localvar(ls, indexname, nvars++);\n  while (testnext(ls, ','))\n    new_localvar(ls, str_checkname(ls), nvars++);\n  checknext(ls, TK_IN);\n  line = ls->linenumber;\n  adjust_assign(ls, 3, explist1(ls, &e), &e);\n  luaK_checkstack(fs, 3);  /* extra space to call generator */\n  forbody(ls, base, line, nvars - 3, 0);\n}\n\n\nstatic void forstat (LexState *ls, int line) {\n  /* forstat -> FOR (fornum | forlist) END */\n  FuncState *fs = ls->fs;\n  TString *varname;\n  BlockCnt bl;\n  enterblock(fs, &bl, 1);  /* scope for loop and control variables */\n  luaX_next(ls);  /* skip `for' */\n  varname = str_checkname(ls);  /* first variable name */\n  switch (ls->t.token) {\n    case '=': fornum(ls, varname, line); break;\n    case ',': case TK_IN: forlist(ls, varname); break;\n    default: luaX_syntaxerror(ls, LUA_QL(\"=\") \" or \" LUA_QL(\"in\") \" expected\");\n  }\n  check_match(ls, TK_END, TK_FOR, line);\n  leaveblock(fs);  /* loop scope (`break' jumps to this point) */\n}\n\n\nstatic int test_then_block (LexState *ls) {\n  /* test_then_block -> [IF | ELSEIF] cond THEN block */\n  int condexit;\n  luaX_next(ls);  /* skip IF or ELSEIF */\n  condexit = cond(ls);\n  checknext(ls, TK_THEN);\n  block(ls);  /* `then' part */\n  return condexit;\n}\n\n\nstatic void ifstat (LexState *ls, int line) {\n  /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */\n  FuncState *fs = ls->fs;\n  int flist;\n  int escapelist = NO_JUMP;\n  flist = test_then_block(ls);  /* IF cond THEN block */\n  while (ls->t.token == TK_ELSEIF) {\n    luaK_concat(fs, &escapelist, luaK_jump(fs));\n    luaK_patchtohere(fs, flist);\n    flist = test_then_block(ls);  /* ELSEIF cond THEN block */\n  }\n  if (ls->t.token == TK_ELSE) {\n    luaK_concat(fs, &escapelist, luaK_jump(fs));\n    luaK_patchtohere(fs, flist);\n    luaX_next(ls);  /* skip ELSE (after patch, for correct line info) */\n    block(ls);  /* `else' part */\n  }\n  else\n    luaK_concat(fs, &escapelist, flist);\n  luaK_patchtohere(fs, escapelist);\n  check_match(ls, TK_END, TK_IF, line);\n}\n\n\nstatic void localfunc (LexState *ls) {\n  expdesc v, b;\n  FuncState *fs = ls->fs;\n  new_localvar(ls, str_checkname(ls), 0);\n  init_exp(&v, VLOCAL, fs->freereg);\n  luaK_reserveregs(fs, 1);\n  adjustlocalvars(ls, 1);\n  body(ls, &b, 0, ls->linenumber);\n  luaK_storevar(fs, &v, &b);\n  /* debug information will only see the variable after this point! */\n  getlocvar(fs, fs->nactvar - 1).startpc = fs->pc;\n}\n\n\nstatic void localstat (LexState *ls) {\n  /* stat -> LOCAL NAME {`,' NAME} [`=' explist1] */\n  int nvars = 0;\n  int nexps;\n  expdesc e;\n  do {\n    new_localvar(ls, str_checkname(ls), nvars++);\n  } while (testnext(ls, ','));\n  if (testnext(ls, '='))\n    nexps = explist1(ls, &e);\n  else {\n    e.k = VVOID;\n    nexps = 0;\n  }\n  adjust_assign(ls, nvars, nexps, &e);\n  adjustlocalvars(ls, nvars);\n}\n\n\nstatic int funcname (LexState *ls, expdesc *v) {\n  /* funcname -> NAME {field} [`:' NAME] */\n  int needself = 0;\n  singlevar(ls, v);\n  while (ls->t.token == '.')\n    field(ls, v);\n  if (ls->t.token == ':') {\n    needself = 1;\n    field(ls, v);\n  }\n  return needself;\n}\n\n\nstatic void funcstat (LexState *ls, int line) {\n  /* funcstat -> FUNCTION funcname body */\n  int needself;\n  expdesc v, b;\n  luaX_next(ls);  /* skip FUNCTION */\n  needself = funcname(ls, &v);\n  body(ls, &b, needself, line);\n  luaK_storevar(ls->fs, &v, &b);\n  luaK_fixline(ls->fs, line);  /* definition `happens' in the first line */\n}\n\n\nstatic void exprstat (LexState *ls) {\n  /* stat -> func | assignment */\n  FuncState *fs = ls->fs;\n  struct LHS_assign v;\n  primaryexp(ls, &v.v);\n  if (v.v.k == VCALL)  /* stat -> func */\n    SETARG_C(getcode(fs, &v.v), 1);  /* call statement uses no results */\n  else {  /* stat -> assignment */\n    v.prev = NULL;\n    assignment(ls, &v, 1);\n  }\n}\n\n\nstatic void retstat (LexState *ls) {\n  /* stat -> RETURN explist */\n  FuncState *fs = ls->fs;\n  expdesc e;\n  int first, nret;  /* registers with returned values */\n  luaX_next(ls);  /* skip RETURN */\n  if (block_follow(ls->t.token) || ls->t.token == ';')\n    first = nret = 0;  /* return no values */\n  else {\n    nret = explist1(ls, &e);  /* optional return values */\n    if (hasmultret(e.k)) {\n      luaK_setmultret(fs, &e);\n      if (e.k == VCALL && nret == 1) {  /* tail call? */\n        SET_OPCODE(getcode(fs,&e), OP_TAILCALL);\n        lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar);\n      }\n      first = fs->nactvar;\n      nret = LUA_MULTRET;  /* return all values */\n    }\n    else {\n      if (nret == 1)  /* only one single value? */\n        first = luaK_exp2anyreg(fs, &e);\n      else {\n        luaK_exp2nextreg(fs, &e);  /* values must go to the `stack' */\n        first = fs->nactvar;  /* return all `active' values */\n        lua_assert(nret == fs->freereg - first);\n      }\n    }\n  }\n  luaK_ret(fs, first, nret);\n}\n\n\nstatic int statement (LexState *ls) {\n  int line = ls->linenumber;  /* may be needed for error messages */\n  switch (ls->t.token) {\n    case TK_IF: {  /* stat -> ifstat */\n      ifstat(ls, line);\n      return 0;\n    }\n    case TK_WHILE: {  /* stat -> whilestat */\n      whilestat(ls, line);\n      return 0;\n    }\n    case TK_DO: {  /* stat -> DO block END */\n      luaX_next(ls);  /* skip DO */\n      block(ls);\n      check_match(ls, TK_END, TK_DO, line);\n      return 0;\n    }\n    case TK_FOR: {  /* stat -> forstat */\n      forstat(ls, line);\n      return 0;\n    }\n    case TK_REPEAT: {  /* stat -> repeatstat */\n      repeatstat(ls, line);\n      return 0;\n    }\n    case TK_FUNCTION: {\n      funcstat(ls, line);  /* stat -> funcstat */\n      return 0;\n    }\n    case TK_LOCAL: {  /* stat -> localstat */\n      luaX_next(ls);  /* skip LOCAL */\n      if (testnext(ls, TK_FUNCTION))  /* local function? */\n        localfunc(ls);\n      else\n        localstat(ls);\n      return 0;\n    }\n    case TK_RETURN: {  /* stat -> retstat */\n      retstat(ls);\n      return 1;  /* must be last statement */\n    }\n    case TK_BREAK: {  /* stat -> breakstat */\n      luaX_next(ls);  /* skip BREAK */\n      breakstat(ls);\n      return 1;  /* must be last statement */\n    }\n    default: {\n      exprstat(ls);\n      return 0;  /* to avoid warnings */\n    }\n  }\n}\n\n\nstatic void chunk (LexState *ls) {\n  /* chunk -> { stat [`;'] } */\n  int islast = 0;\n  enterlevel(ls);\n  while (!islast && !block_follow(ls->t.token)) {\n    islast = statement(ls);\n    testnext(ls, ';');\n    lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg &&\n               ls->fs->freereg >= ls->fs->nactvar);\n    ls->fs->freereg = ls->fs->nactvar;  /* free registers */\n  }\n  leavelevel(ls);\n}\n\n/* }====================================================================== */\n"
  },
  {
    "path": "deps/lua/src/lparser.h",
    "content": "/*\n** $Id: lparser.h,v 1.57.1.1 2007/12/27 13:02:25 roberto Exp $\n** Lua Parser\n** See Copyright Notice in lua.h\n*/\n\n#ifndef lparser_h\n#define lparser_h\n\n#include \"llimits.h\"\n#include \"lobject.h\"\n#include \"lzio.h\"\n\n\n/*\n** Expression descriptor\n*/\n\ntypedef enum {\n  VVOID,\t/* no value */\n  VNIL,\n  VTRUE,\n  VFALSE,\n  VK,\t\t/* info = index of constant in `k' */\n  VKNUM,\t/* nval = numerical value */\n  VLOCAL,\t/* info = local register */\n  VUPVAL,       /* info = index of upvalue in `upvalues' */\n  VGLOBAL,\t/* info = index of table; aux = index of global name in `k' */\n  VINDEXED,\t/* info = table register; aux = index register (or `k') */\n  VJMP,\t\t/* info = instruction pc */\n  VRELOCABLE,\t/* info = instruction pc */\n  VNONRELOC,\t/* info = result register */\n  VCALL,\t/* info = instruction pc */\n  VVARARG\t/* info = instruction pc */\n} expkind;\n\ntypedef struct expdesc {\n  expkind k;\n  union {\n    struct { int info, aux; } s;\n    lua_Number nval;\n  } u;\n  int t;  /* patch list of `exit when true' */\n  int f;  /* patch list of `exit when false' */\n} expdesc;\n\n\ntypedef struct upvaldesc {\n  lu_byte k;\n  lu_byte info;\n} upvaldesc;\n\n\nstruct BlockCnt;  /* defined in lparser.c */\n\n\n/* state needed to generate code for a given function */\ntypedef struct FuncState {\n  Proto *f;  /* current function header */\n  Table *h;  /* table to find (and reuse) elements in `k' */\n  struct FuncState *prev;  /* enclosing function */\n  struct LexState *ls;  /* lexical state */\n  struct lua_State *L;  /* copy of the Lua state */\n  struct BlockCnt *bl;  /* chain of current blocks */\n  int pc;  /* next position to code (equivalent to `ncode') */\n  int lasttarget;   /* `pc' of last `jump target' */\n  int jpc;  /* list of pending jumps to `pc' */\n  int freereg;  /* first free register */\n  int nk;  /* number of elements in `k' */\n  int np;  /* number of elements in `p' */\n  short nlocvars;  /* number of elements in `locvars' */\n  lu_byte nactvar;  /* number of active local variables */\n  upvaldesc upvalues[LUAI_MAXUPVALUES];  /* upvalues */\n  unsigned short actvar[LUAI_MAXVARS];  /* declared-variable stack */\n} FuncState;\n\n\nLUAI_FUNC Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,\n                                            const char *name);\n\n\n#endif\n"
  },
  {
    "path": "deps/lua/src/lstate.c",
    "content": "/*\n** $Id: lstate.c,v 2.36.1.2 2008/01/03 15:20:39 roberto Exp $\n** Global State\n** See Copyright Notice in lua.h\n*/\n\n\n#include <stddef.h>\n\n#define lstate_c\n#define LUA_CORE\n\n#include \"lua.h\"\n\n#include \"ldebug.h\"\n#include \"ldo.h\"\n#include \"lfunc.h\"\n#include \"lgc.h\"\n#include \"llex.h\"\n#include \"lmem.h\"\n#include \"lstate.h\"\n#include \"lstring.h\"\n#include \"ltable.h\"\n#include \"ltm.h\"\n\n\n#define state_size(x)\t(sizeof(x) + LUAI_EXTRASPACE)\n#define fromstate(l)\t(cast(lu_byte *, (l)) - LUAI_EXTRASPACE)\n#define tostate(l)   (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE))\n\n\n/*\n** Main thread combines a thread state and the global state\n*/\ntypedef struct LG {\n  lua_State l;\n  global_State g;\n} LG;\n  \n\n\nstatic void stack_init (lua_State *L1, lua_State *L) {\n  /* initialize CallInfo array */\n  L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo);\n  L1->ci = L1->base_ci;\n  L1->size_ci = BASIC_CI_SIZE;\n  L1->end_ci = L1->base_ci + L1->size_ci - 1;\n  /* initialize stack array */\n  L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TValue);\n  L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK;\n  L1->top = L1->stack;\n  L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1;\n  /* initialize first ci */\n  L1->ci->func = L1->top;\n  setnilvalue(L1->top++);  /* `function' entry for this `ci' */\n  L1->base = L1->ci->base = L1->top;\n  L1->ci->top = L1->top + LUA_MINSTACK;\n}\n\n\nstatic void freestack (lua_State *L, lua_State *L1) {\n  luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo);\n  luaM_freearray(L, L1->stack, L1->stacksize, TValue);\n}\n\n\n/*\n** open parts that may cause memory-allocation errors\n*/\nstatic void f_luaopen (lua_State *L, void *ud) {\n  global_State *g = G(L);\n  UNUSED(ud);\n  stack_init(L, L);  /* init stack */\n  sethvalue(L, gt(L), luaH_new(L, 0, 2));  /* table of globals */\n  sethvalue(L, registry(L), luaH_new(L, 0, 2));  /* registry */\n  luaS_resize(L, MINSTRTABSIZE);  /* initial size of string table */\n  luaT_init(L);\n  luaX_init(L);\n  luaS_fix(luaS_newliteral(L, MEMERRMSG));\n  g->GCthreshold = 4*g->totalbytes;\n}\n\n\nstatic void preinit_state (lua_State *L, global_State *g) {\n  G(L) = g;\n  L->stack = NULL;\n  L->stacksize = 0;\n  L->errorJmp = NULL;\n  L->hook = NULL;\n  L->hookmask = 0;\n  L->basehookcount = 0;\n  L->allowhook = 1;\n  resethookcount(L);\n  L->openupval = NULL;\n  L->size_ci = 0;\n  L->nCcalls = L->baseCcalls = 0;\n  L->status = 0;\n  L->base_ci = L->ci = NULL;\n  L->savedpc = NULL;\n  L->errfunc = 0;\n  setnilvalue(gt(L));\n}\n\n\nstatic void close_state (lua_State *L) {\n  global_State *g = G(L);\n  luaF_close(L, L->stack);  /* close all upvalues for this thread */\n  luaC_freeall(L);  /* collect all objects */\n  lua_assert(g->rootgc == obj2gco(L));\n  lua_assert(g->strt.nuse == 0);\n  luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *);\n  luaZ_freebuffer(L, &g->buff);\n  freestack(L, L);\n  lua_assert(g->totalbytes == sizeof(LG));\n  (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0);\n}\n\n\nlua_State *luaE_newthread (lua_State *L) {\n  lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State)));\n  luaC_link(L, obj2gco(L1), LUA_TTHREAD);\n  preinit_state(L1, G(L));\n  stack_init(L1, L);  /* init stack */\n  setobj2n(L, gt(L1), gt(L));  /* share table of globals */\n  L1->hookmask = L->hookmask;\n  L1->basehookcount = L->basehookcount;\n  L1->hook = L->hook;\n  resethookcount(L1);\n  lua_assert(iswhite(obj2gco(L1)));\n  return L1;\n}\n\n\nvoid luaE_freethread (lua_State *L, lua_State *L1) {\n  luaF_close(L1, L1->stack);  /* close all upvalues for this thread */\n  lua_assert(L1->openupval == NULL);\n  luai_userstatefree(L1);\n  freestack(L, L1);\n  luaM_freemem(L, fromstate(L1), state_size(lua_State));\n}\n\n\nLUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {\n  int i;\n  lua_State *L;\n  global_State *g;\n  void *l = (*f)(ud, NULL, 0, state_size(LG));\n  if (l == NULL) return NULL;\n  L = tostate(l);\n  g = &((LG *)L)->g;\n  L->next = NULL;\n  L->tt = LUA_TTHREAD;\n  g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);\n  L->marked = luaC_white(g);\n  set2bits(L->marked, FIXEDBIT, SFIXEDBIT);\n  preinit_state(L, g);\n  g->frealloc = f;\n  g->ud = ud;\n  g->mainthread = L;\n  g->uvhead.u.l.prev = &g->uvhead;\n  g->uvhead.u.l.next = &g->uvhead;\n  g->GCthreshold = 0;  /* mark it as unfinished state */\n  g->strt.size = 0;\n  g->strt.nuse = 0;\n  g->strt.hash = NULL;\n  setnilvalue(registry(L));\n  luaZ_initbuffer(L, &g->buff);\n  g->panic = NULL;\n  g->gcstate = GCSpause;\n  g->rootgc = obj2gco(L);\n  g->sweepstrgc = 0;\n  g->sweepgc = &g->rootgc;\n  g->gray = NULL;\n  g->grayagain = NULL;\n  g->weak = NULL;\n  g->tmudata = NULL;\n  g->totalbytes = sizeof(LG);\n  g->gcpause = LUAI_GCPAUSE;\n  g->gcstepmul = LUAI_GCMUL;\n  g->gcdept = 0;\n  for (i=0; i<NUM_TAGS; i++) g->mt[i] = NULL;\n  if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) {\n    /* memory allocation error: free partial state */\n    close_state(L);\n    L = NULL;\n  }\n  else\n    luai_userstateopen(L);\n  return L;\n}\n\n\nstatic void callallgcTM (lua_State *L, void *ud) {\n  UNUSED(ud);\n  luaC_callGCTM(L);  /* call GC metamethods for all udata */\n}\n\n\nLUA_API void lua_close (lua_State *L) {\n  L = G(L)->mainthread;  /* only the main thread can be closed */\n  lua_lock(L);\n  luaF_close(L, L->stack);  /* close all upvalues for this thread */\n  luaC_separateudata(L, 1);  /* separate udata that have GC metamethods */\n  L->errfunc = 0;  /* no error function during GC metamethods */\n  do {  /* repeat until no more errors */\n    L->ci = L->base_ci;\n    L->base = L->top = L->ci->base;\n    L->nCcalls = L->baseCcalls = 0;\n  } while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0);\n  lua_assert(G(L)->tmudata == NULL);\n  luai_userstateclose(L);\n  close_state(L);\n}\n\n"
  },
  {
    "path": "deps/lua/src/lstate.h",
    "content": "/*\n** $Id: lstate.h,v 2.24.1.2 2008/01/03 15:20:39 roberto Exp $\n** Global State\n** See Copyright Notice in lua.h\n*/\n\n#ifndef lstate_h\n#define lstate_h\n\n#include \"lua.h\"\n\n#include \"lobject.h\"\n#include \"ltm.h\"\n#include \"lzio.h\"\n\n\n\nstruct lua_longjmp;  /* defined in ldo.c */\n\n\n/* table of globals */\n#define gt(L)\t(&L->l_gt)\n\n/* registry */\n#define registry(L)\t(&G(L)->l_registry)\n\n\n/* extra stack space to handle TM calls and some other extras */\n#define EXTRA_STACK   5\n\n\n#define BASIC_CI_SIZE           8\n\n#define BASIC_STACK_SIZE        (2*LUA_MINSTACK)\n\n\n\ntypedef struct stringtable {\n  GCObject **hash;\n  lu_int32 nuse;  /* number of elements */\n  int size;\n} stringtable;\n\n\n/*\n** informations about a call\n*/\ntypedef struct CallInfo {\n  StkId base;  /* base for this function */\n  StkId func;  /* function index in the stack */\n  StkId\ttop;  /* top for this function */\n  const Instruction *savedpc;\n  int nresults;  /* expected number of results from this function */\n  int tailcalls;  /* number of tail calls lost under this entry */\n} CallInfo;\n\n\n\n#define curr_func(L)\t(clvalue(L->ci->func))\n#define ci_func(ci)\t(clvalue((ci)->func))\n#define f_isLua(ci)\t(!ci_func(ci)->c.isC)\n#define isLua(ci)\t(ttisfunction((ci)->func) && f_isLua(ci))\n\n\n/*\n** `global state', shared by all threads of this state\n*/\ntypedef struct global_State {\n  stringtable strt;  /* hash table for strings */\n  lua_Alloc frealloc;  /* function to reallocate memory */\n  void *ud;         /* auxiliary data to `frealloc' */\n  lu_byte currentwhite;\n  lu_byte gcstate;  /* state of garbage collector */\n  int sweepstrgc;  /* position of sweep in `strt' */\n  GCObject *rootgc;  /* list of all collectable objects */\n  GCObject **sweepgc;  /* position of sweep in `rootgc' */\n  GCObject *gray;  /* list of gray objects */\n  GCObject *grayagain;  /* list of objects to be traversed atomically */\n  GCObject *weak;  /* list of weak tables (to be cleared) */\n  GCObject *tmudata;  /* last element of list of userdata to be GC */\n  Mbuffer buff;  /* temporary buffer for string concatentation */\n  lu_mem GCthreshold;\n  lu_mem totalbytes;  /* number of bytes currently allocated */\n  lu_mem estimate;  /* an estimate of number of bytes actually in use */\n  lu_mem gcdept;  /* how much GC is `behind schedule' */\n  int gcpause;  /* size of pause between successive GCs */\n  int gcstepmul;  /* GC `granularity' */\n  lua_CFunction panic;  /* to be called in unprotected errors */\n  TValue l_registry;\n  struct lua_State *mainthread;\n  UpVal uvhead;  /* head of double-linked list of all open upvalues */\n  struct Table *mt[NUM_TAGS];  /* metatables for basic types */\n  TString *tmname[TM_N];  /* array with tag-method names */\n} global_State;\n\n\n/*\n** `per thread' state\n*/\nstruct lua_State {\n  CommonHeader;\n  lu_byte status;\n  StkId top;  /* first free slot in the stack */\n  StkId base;  /* base of current function */\n  global_State *l_G;\n  CallInfo *ci;  /* call info for current function */\n  const Instruction *savedpc;  /* `savedpc' of current function */\n  StkId stack_last;  /* last free slot in the stack */\n  StkId stack;  /* stack base */\n  CallInfo *end_ci;  /* points after end of ci array*/\n  CallInfo *base_ci;  /* array of CallInfo's */\n  int stacksize;\n  int size_ci;  /* size of array `base_ci' */\n  unsigned short nCcalls;  /* number of nested C calls */\n  unsigned short baseCcalls;  /* nested C calls when resuming coroutine */\n  lu_byte hookmask;\n  lu_byte allowhook;\n  int basehookcount;\n  int hookcount;\n  lua_Hook hook;\n  TValue l_gt;  /* table of globals */\n  TValue env;  /* temporary place for environments */\n  GCObject *openupval;  /* list of open upvalues in this stack */\n  GCObject *gclist;\n  struct lua_longjmp *errorJmp;  /* current error recover point */\n  ptrdiff_t errfunc;  /* current error handling function (stack index) */\n};\n\n\n#define G(L)\t(L->l_G)\n\n\n/*\n** Union of all collectable objects\n*/\nunion GCObject {\n  GCheader gch;\n  union TString ts;\n  union Udata u;\n  union Closure cl;\n  struct Table h;\n  struct Proto p;\n  struct UpVal uv;\n  struct lua_State th;  /* thread */\n};\n\n\n/* macros to convert a GCObject into a specific value */\n#define rawgco2ts(o)\tcheck_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts))\n#define gco2ts(o)\t(&rawgco2ts(o)->tsv)\n#define rawgco2u(o)\tcheck_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u))\n#define gco2u(o)\t(&rawgco2u(o)->uv)\n#define gco2cl(o)\tcheck_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl))\n#define gco2h(o)\tcheck_exp((o)->gch.tt == LUA_TTABLE, &((o)->h))\n#define gco2p(o)\tcheck_exp((o)->gch.tt == LUA_TPROTO, &((o)->p))\n#define gco2uv(o)\tcheck_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv))\n#define ngcotouv(o) \\\n\tcheck_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv))\n#define gco2th(o)\tcheck_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th))\n\n/* macro to convert any Lua object into a GCObject */\n#define obj2gco(v)\t(cast(GCObject *, (v)))\n\n\nLUAI_FUNC lua_State *luaE_newthread (lua_State *L);\nLUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);\n\n#endif\n\n"
  },
  {
    "path": "deps/lua/src/lstring.c",
    "content": "/*\n** $Id: lstring.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $\n** String table (keeps all strings handled by Lua)\n** See Copyright Notice in lua.h\n*/\n\n\n#include <string.h>\n\n#define lstring_c\n#define LUA_CORE\n\n#include \"lua.h\"\n\n#include \"lmem.h\"\n#include \"lobject.h\"\n#include \"lstate.h\"\n#include \"lstring.h\"\n\n\n\nvoid luaS_resize (lua_State *L, int newsize) {\n  GCObject **newhash;\n  stringtable *tb;\n  int i;\n  if (G(L)->gcstate == GCSsweepstring)\n    return;  /* cannot resize during GC traverse */\n  newhash = luaM_newvector(L, newsize, GCObject *);\n  tb = &G(L)->strt;\n  for (i=0; i<newsize; i++) newhash[i] = NULL;\n  /* rehash */\n  for (i=0; i<tb->size; i++) {\n    GCObject *p = tb->hash[i];\n    while (p) {  /* for each node in the list */\n      GCObject *next = p->gch.next;  /* save next */\n      unsigned int h = gco2ts(p)->hash;\n      int h1 = lmod(h, newsize);  /* new position */\n      lua_assert(cast_int(h%newsize) == lmod(h, newsize));\n      p->gch.next = newhash[h1];  /* chain it */\n      newhash[h1] = p;\n      p = next;\n    }\n  }\n  luaM_freearray(L, tb->hash, tb->size, TString *);\n  tb->size = newsize;\n  tb->hash = newhash;\n}\n\n\nstatic TString *newlstr (lua_State *L, const char *str, size_t l,\n                                       unsigned int h) {\n  TString *ts;\n  stringtable *tb;\n  if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char))\n    luaM_toobig(L);\n  ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString)));\n  ts->tsv.len = l;\n  ts->tsv.hash = h;\n  ts->tsv.marked = luaC_white(G(L));\n  ts->tsv.tt = LUA_TSTRING;\n  ts->tsv.reserved = 0;\n  memcpy(ts+1, str, l*sizeof(char));\n  ((char *)(ts+1))[l] = '\\0';  /* ending 0 */\n  tb = &G(L)->strt;\n  h = lmod(h, tb->size);\n  ts->tsv.next = tb->hash[h];  /* chain new entry */\n  tb->hash[h] = obj2gco(ts);\n  tb->nuse++;\n  if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)\n    luaS_resize(L, tb->size*2);  /* too crowded */\n  return ts;\n}\n\n\nTString *luaS_newlstr (lua_State *L, const char *str, size_t l) {\n  GCObject *o;\n  unsigned int h = cast(unsigned int, l);  /* seed */\n  size_t step = (l>>5)+1;  /* if string is too long, don't hash all its chars */\n  size_t l1;\n  for (l1=l; l1>=step; l1-=step)  /* compute hash */\n    h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1]));\n  for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)];\n       o != NULL;\n       o = o->gch.next) {\n    TString *ts = rawgco2ts(o);\n    if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) {\n      /* string may be dead */\n      if (isdead(G(L), o)) changewhite(o);\n      return ts;\n    }\n  }\n  return newlstr(L, str, l, h);  /* not found */\n}\n\n\nUdata *luaS_newudata (lua_State *L, size_t s, Table *e) {\n  Udata *u;\n  if (s > MAX_SIZET - sizeof(Udata))\n    luaM_toobig(L);\n  u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata)));\n  u->uv.marked = luaC_white(G(L));  /* is not finalized */\n  u->uv.tt = LUA_TUSERDATA;\n  u->uv.len = s;\n  u->uv.metatable = NULL;\n  u->uv.env = e;\n  /* chain it on udata list (after main thread) */\n  u->uv.next = G(L)->mainthread->next;\n  G(L)->mainthread->next = obj2gco(u);\n  return u;\n}\n\n"
  },
  {
    "path": "deps/lua/src/lstring.h",
    "content": "/*\n** $Id: lstring.h,v 1.43.1.1 2007/12/27 13:02:25 roberto Exp $\n** String table (keep all strings handled by Lua)\n** See Copyright Notice in lua.h\n*/\n\n#ifndef lstring_h\n#define lstring_h\n\n\n#include \"lgc.h\"\n#include \"lobject.h\"\n#include \"lstate.h\"\n\n\n#define sizestring(s)\t(sizeof(union TString)+((s)->len+1)*sizeof(char))\n\n#define sizeudata(u)\t(sizeof(union Udata)+(u)->len)\n\n#define luaS_new(L, s)\t(luaS_newlstr(L, s, strlen(s)))\n#define luaS_newliteral(L, s)\t(luaS_newlstr(L, \"\" s, \\\n                                 (sizeof(s)/sizeof(char))-1))\n\n#define luaS_fix(s)\tl_setbit((s)->tsv.marked, FIXEDBIT)\n\nLUAI_FUNC void luaS_resize (lua_State *L, int newsize);\nLUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e);\nLUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l);\n\n\n#endif\n"
  },
  {
    "path": "deps/lua/src/lstrlib.c",
    "content": "/*\n** $Id: lstrlib.c,v 1.132.1.5 2010/05/14 15:34:19 roberto Exp $\n** Standard library for string operations and pattern-matching\n** See Copyright Notice in lua.h\n*/\n\n\n#include <ctype.h>\n#include <stddef.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#define lstrlib_c\n#define LUA_LIB\n\n#include \"lua.h\"\n\n#include \"lauxlib.h\"\n#include \"lualib.h\"\n\n\n/* macro to `unsign' a character */\n#define uchar(c)        ((unsigned char)(c))\n\n\n\nstatic int str_len (lua_State *L) {\n  size_t l;\n  luaL_checklstring(L, 1, &l);\n  lua_pushinteger(L, l);\n  return 1;\n}\n\n\nstatic ptrdiff_t posrelat (ptrdiff_t pos, size_t len) {\n  /* relative string position: negative means back from end */\n  if (pos < 0) pos += (ptrdiff_t)len + 1;\n  return (pos >= 0) ? pos : 0;\n}\n\n\nstatic int str_sub (lua_State *L) {\n  size_t l;\n  const char *s = luaL_checklstring(L, 1, &l);\n  ptrdiff_t start = posrelat(luaL_checkinteger(L, 2), l);\n  ptrdiff_t end = posrelat(luaL_optinteger(L, 3, -1), l);\n  if (start < 1) start = 1;\n  if (end > (ptrdiff_t)l) end = (ptrdiff_t)l;\n  if (start <= end)\n    lua_pushlstring(L, s+start-1, end-start+1);\n  else lua_pushliteral(L, \"\");\n  return 1;\n}\n\n\nstatic int str_reverse (lua_State *L) {\n  size_t l;\n  luaL_Buffer b;\n  const char *s = luaL_checklstring(L, 1, &l);\n  luaL_buffinit(L, &b);\n  while (l--) luaL_addchar(&b, s[l]);\n  luaL_pushresult(&b);\n  return 1;\n}\n\n\nstatic int str_lower (lua_State *L) {\n  size_t l;\n  size_t i;\n  luaL_Buffer b;\n  const char *s = luaL_checklstring(L, 1, &l);\n  luaL_buffinit(L, &b);\n  for (i=0; i<l; i++)\n    luaL_addchar(&b, tolower(uchar(s[i])));\n  luaL_pushresult(&b);\n  return 1;\n}\n\n\nstatic int str_upper (lua_State *L) {\n  size_t l;\n  size_t i;\n  luaL_Buffer b;\n  const char *s = luaL_checklstring(L, 1, &l);\n  luaL_buffinit(L, &b);\n  for (i=0; i<l; i++)\n    luaL_addchar(&b, toupper(uchar(s[i])));\n  luaL_pushresult(&b);\n  return 1;\n}\n\nstatic int str_rep (lua_State *L) {\n  size_t l;\n  luaL_Buffer b;\n  const char *s = luaL_checklstring(L, 1, &l);\n  int n = luaL_checkint(L, 2);\n  luaL_buffinit(L, &b);\n  while (n-- > 0)\n    luaL_addlstring(&b, s, l);\n  luaL_pushresult(&b);\n  return 1;\n}\n\n\nstatic int str_byte (lua_State *L) {\n  size_t l;\n  const char *s = luaL_checklstring(L, 1, &l);\n  ptrdiff_t posi = posrelat(luaL_optinteger(L, 2, 1), l);\n  ptrdiff_t pose = posrelat(luaL_optinteger(L, 3, posi), l);\n  int n, i;\n  if (posi <= 0) posi = 1;\n  if ((size_t)pose > l) pose = l;\n  if (posi > pose) return 0;  /* empty interval; return no values */\n  n = (int)(pose -  posi + 1);\n  if (posi + n <= pose)  /* overflow? */\n    luaL_error(L, \"string slice too long\");\n  luaL_checkstack(L, n, \"string slice too long\");\n  for (i=0; i<n; i++)\n    lua_pushinteger(L, uchar(s[posi+i-1]));\n  return n;\n}\n\n\nstatic int str_char (lua_State *L) {\n  int n = lua_gettop(L);  /* number of arguments */\n  int i;\n  luaL_Buffer b;\n  luaL_buffinit(L, &b);\n  for (i=1; i<=n; i++) {\n    int c = luaL_checkint(L, i);\n    luaL_argcheck(L, uchar(c) == c, i, \"invalid value\");\n    luaL_addchar(&b, uchar(c));\n  }\n  luaL_pushresult(&b);\n  return 1;\n}\n\n\nstatic int writer (lua_State *L, const void* b, size_t size, void* B) {\n  (void)L;\n  luaL_addlstring((luaL_Buffer*) B, (const char *)b, size);\n  return 0;\n}\n\n\nstatic int str_dump (lua_State *L) {\n  luaL_Buffer b;\n  luaL_checktype(L, 1, LUA_TFUNCTION);\n  lua_settop(L, 1);\n  luaL_buffinit(L,&b);\n  if (lua_dump(L, writer, &b) != 0)\n    luaL_error(L, \"unable to dump given function\");\n  luaL_pushresult(&b);\n  return 1;\n}\n\n\n\n/*\n** {======================================================\n** PATTERN MATCHING\n** =======================================================\n*/\n\n\n#define CAP_UNFINISHED\t(-1)\n#define CAP_POSITION\t(-2)\n\ntypedef struct MatchState {\n  const char *src_init;  /* init of source string */\n  const char *src_end;  /* end (`\\0') of source string */\n  lua_State *L;\n  int level;  /* total number of captures (finished or unfinished) */\n  struct {\n    const char *init;\n    ptrdiff_t len;\n  } capture[LUA_MAXCAPTURES];\n} MatchState;\n\n\n#define L_ESC\t\t'%'\n#define SPECIALS\t\"^$*+?.([%-\"\n\n\nstatic int check_capture (MatchState *ms, int l) {\n  l -= '1';\n  if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)\n    return luaL_error(ms->L, \"invalid capture index\");\n  return l;\n}\n\n\nstatic int capture_to_close (MatchState *ms) {\n  int level = ms->level;\n  for (level--; level>=0; level--)\n    if (ms->capture[level].len == CAP_UNFINISHED) return level;\n  return luaL_error(ms->L, \"invalid pattern capture\");\n}\n\n\nstatic const char *classend (MatchState *ms, const char *p) {\n  switch (*p++) {\n    case L_ESC: {\n      if (*p == '\\0')\n        luaL_error(ms->L, \"malformed pattern (ends with \" LUA_QL(\"%%\") \")\");\n      return p+1;\n    }\n    case '[': {\n      if (*p == '^') p++;\n      do {  /* look for a `]' */\n        if (*p == '\\0')\n          luaL_error(ms->L, \"malformed pattern (missing \" LUA_QL(\"]\") \")\");\n        if (*(p++) == L_ESC && *p != '\\0')\n          p++;  /* skip escapes (e.g. `%]') */\n      } while (*p != ']');\n      return p+1;\n    }\n    default: {\n      return p;\n    }\n  }\n}\n\n\nstatic int match_class (int c, int cl) {\n  int res;\n  switch (tolower(cl)) {\n    case 'a' : res = isalpha(c); break;\n    case 'c' : res = iscntrl(c); break;\n    case 'd' : res = isdigit(c); break;\n    case 'l' : res = islower(c); break;\n    case 'p' : res = ispunct(c); break;\n    case 's' : res = isspace(c); break;\n    case 'u' : res = isupper(c); break;\n    case 'w' : res = isalnum(c); break;\n    case 'x' : res = isxdigit(c); break;\n    case 'z' : res = (c == 0); break;\n    default: return (cl == c);\n  }\n  return (islower(cl) ? res : !res);\n}\n\n\nstatic int matchbracketclass (int c, const char *p, const char *ec) {\n  int sig = 1;\n  if (*(p+1) == '^') {\n    sig = 0;\n    p++;  /* skip the `^' */\n  }\n  while (++p < ec) {\n    if (*p == L_ESC) {\n      p++;\n      if (match_class(c, uchar(*p)))\n        return sig;\n    }\n    else if ((*(p+1) == '-') && (p+2 < ec)) {\n      p+=2;\n      if (uchar(*(p-2)) <= c && c <= uchar(*p))\n        return sig;\n    }\n    else if (uchar(*p) == c) return sig;\n  }\n  return !sig;\n}\n\n\nstatic int singlematch (int c, const char *p, const char *ep) {\n  switch (*p) {\n    case '.': return 1;  /* matches any char */\n    case L_ESC: return match_class(c, uchar(*(p+1)));\n    case '[': return matchbracketclass(c, p, ep-1);\n    default:  return (uchar(*p) == c);\n  }\n}\n\n\nstatic const char *match (MatchState *ms, const char *s, const char *p);\n\n\nstatic const char *matchbalance (MatchState *ms, const char *s,\n                                   const char *p) {\n  if (*p == 0 || *(p+1) == 0)\n    luaL_error(ms->L, \"unbalanced pattern\");\n  if (*s != *p) return NULL;\n  else {\n    int b = *p;\n    int e = *(p+1);\n    int cont = 1;\n    while (++s < ms->src_end) {\n      if (*s == e) {\n        if (--cont == 0) return s+1;\n      }\n      else if (*s == b) cont++;\n    }\n  }\n  return NULL;  /* string ends out of balance */\n}\n\n\nstatic const char *max_expand (MatchState *ms, const char *s,\n                                 const char *p, const char *ep) {\n  ptrdiff_t i = 0;  /* counts maximum expand for item */\n  while ((s+i)<ms->src_end && singlematch(uchar(*(s+i)), p, ep))\n    i++;\n  /* keeps trying to match with the maximum repetitions */\n  while (i>=0) {\n    const char *res = match(ms, (s+i), ep+1);\n    if (res) return res;\n    i--;  /* else didn't match; reduce 1 repetition to try again */\n  }\n  return NULL;\n}\n\n\nstatic const char *min_expand (MatchState *ms, const char *s,\n                                 const char *p, const char *ep) {\n  for (;;) {\n    const char *res = match(ms, s, ep+1);\n    if (res != NULL)\n      return res;\n    else if (s<ms->src_end && singlematch(uchar(*s), p, ep))\n      s++;  /* try with one more repetition */\n    else return NULL;\n  }\n}\n\n\nstatic const char *start_capture (MatchState *ms, const char *s,\n                                    const char *p, int what) {\n  const char *res;\n  int level = ms->level;\n  if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, \"too many captures\");\n  ms->capture[level].init = s;\n  ms->capture[level].len = what;\n  ms->level = level+1;\n  if ((res=match(ms, s, p)) == NULL)  /* match failed? */\n    ms->level--;  /* undo capture */\n  return res;\n}\n\n\nstatic const char *end_capture (MatchState *ms, const char *s,\n                                  const char *p) {\n  int l = capture_to_close(ms);\n  const char *res;\n  ms->capture[l].len = s - ms->capture[l].init;  /* close capture */\n  if ((res = match(ms, s, p)) == NULL)  /* match failed? */\n    ms->capture[l].len = CAP_UNFINISHED;  /* undo capture */\n  return res;\n}\n\n\nstatic const char *match_capture (MatchState *ms, const char *s, int l) {\n  size_t len;\n  l = check_capture(ms, l);\n  len = ms->capture[l].len;\n  if ((size_t)(ms->src_end-s) >= len &&\n      memcmp(ms->capture[l].init, s, len) == 0)\n    return s+len;\n  else return NULL;\n}\n\n\nstatic const char *match (MatchState *ms, const char *s, const char *p) {\n  init: /* using goto's to optimize tail recursion */\n  switch (*p) {\n    case '(': {  /* start capture */\n      if (*(p+1) == ')')  /* position capture? */\n        return start_capture(ms, s, p+2, CAP_POSITION);\n      else\n        return start_capture(ms, s, p+1, CAP_UNFINISHED);\n    }\n    case ')': {  /* end capture */\n      return end_capture(ms, s, p+1);\n    }\n    case L_ESC: {\n      switch (*(p+1)) {\n        case 'b': {  /* balanced string? */\n          s = matchbalance(ms, s, p+2);\n          if (s == NULL) return NULL;\n          p+=4; goto init;  /* else return match(ms, s, p+4); */\n        }\n        case 'f': {  /* frontier? */\n          const char *ep; char previous;\n          p += 2;\n          if (*p != '[')\n            luaL_error(ms->L, \"missing \" LUA_QL(\"[\") \" after \"\n                               LUA_QL(\"%%f\") \" in pattern\");\n          ep = classend(ms, p);  /* points to what is next */\n          previous = (s == ms->src_init) ? '\\0' : *(s-1);\n          if (matchbracketclass(uchar(previous), p, ep-1) ||\n             !matchbracketclass(uchar(*s), p, ep-1)) return NULL;\n          p=ep; goto init;  /* else return match(ms, s, ep); */\n        }\n        default: {\n          if (isdigit(uchar(*(p+1)))) {  /* capture results (%0-%9)? */\n            s = match_capture(ms, s, uchar(*(p+1)));\n            if (s == NULL) return NULL;\n            p+=2; goto init;  /* else return match(ms, s, p+2) */\n          }\n          goto dflt;  /* case default */\n        }\n      }\n    }\n    case '\\0': {  /* end of pattern */\n      return s;  /* match succeeded */\n    }\n    case '$': {\n      if (*(p+1) == '\\0')  /* is the `$' the last char in pattern? */\n        return (s == ms->src_end) ? s : NULL;  /* check end of string */\n      else goto dflt;\n    }\n    default: dflt: {  /* it is a pattern item */\n      const char *ep = classend(ms, p);  /* points to what is next */\n      int m = s<ms->src_end && singlematch(uchar(*s), p, ep);\n      switch (*ep) {\n        case '?': {  /* optional */\n          const char *res;\n          if (m && ((res=match(ms, s+1, ep+1)) != NULL))\n            return res;\n          p=ep+1; goto init;  /* else return match(ms, s, ep+1); */\n        }\n        case '*': {  /* 0 or more repetitions */\n          return max_expand(ms, s, p, ep);\n        }\n        case '+': {  /* 1 or more repetitions */\n          return (m ? max_expand(ms, s+1, p, ep) : NULL);\n        }\n        case '-': {  /* 0 or more repetitions (minimum) */\n          return min_expand(ms, s, p, ep);\n        }\n        default: {\n          if (!m) return NULL;\n          s++; p=ep; goto init;  /* else return match(ms, s+1, ep); */\n        }\n      }\n    }\n  }\n}\n\n\n\nstatic const char *lmemfind (const char *s1, size_t l1,\n                               const char *s2, size_t l2) {\n  if (l2 == 0) return s1;  /* empty strings are everywhere */\n  else if (l2 > l1) return NULL;  /* avoids a negative `l1' */\n  else {\n    const char *init;  /* to search for a `*s2' inside `s1' */\n    l2--;  /* 1st char will be checked by `memchr' */\n    l1 = l1-l2;  /* `s2' cannot be found after that */\n    while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {\n      init++;   /* 1st char is already checked */\n      if (memcmp(init, s2+1, l2) == 0)\n        return init-1;\n      else {  /* correct `l1' and `s1' to try again */\n        l1 -= init-s1;\n        s1 = init;\n      }\n    }\n    return NULL;  /* not found */\n  }\n}\n\n\nstatic void push_onecapture (MatchState *ms, int i, const char *s,\n                                                    const char *e) {\n  if (i >= ms->level) {\n    if (i == 0)  /* ms->level == 0, too */\n      lua_pushlstring(ms->L, s, e - s);  /* add whole match */\n    else\n      luaL_error(ms->L, \"invalid capture index\");\n  }\n  else {\n    ptrdiff_t l = ms->capture[i].len;\n    if (l == CAP_UNFINISHED) luaL_error(ms->L, \"unfinished capture\");\n    if (l == CAP_POSITION)\n      lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1);\n    else\n      lua_pushlstring(ms->L, ms->capture[i].init, l);\n  }\n}\n\n\nstatic int push_captures (MatchState *ms, const char *s, const char *e) {\n  int i;\n  int nlevels = (ms->level == 0 && s) ? 1 : ms->level;\n  luaL_checkstack(ms->L, nlevels, \"too many captures\");\n  for (i = 0; i < nlevels; i++)\n    push_onecapture(ms, i, s, e);\n  return nlevels;  /* number of strings pushed */\n}\n\n\nstatic int str_find_aux (lua_State *L, int find) {\n  size_t l1, l2;\n  const char *s = luaL_checklstring(L, 1, &l1);\n  const char *p = luaL_checklstring(L, 2, &l2);\n  ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1;\n  if (init < 0) init = 0;\n  else if ((size_t)(init) > l1) init = (ptrdiff_t)l1;\n  if (find && (lua_toboolean(L, 4) ||  /* explicit request? */\n      strpbrk(p, SPECIALS) == NULL)) {  /* or no special characters? */\n    /* do a plain search */\n    const char *s2 = lmemfind(s+init, l1-init, p, l2);\n    if (s2) {\n      lua_pushinteger(L, s2-s+1);\n      lua_pushinteger(L, s2-s+l2);\n      return 2;\n    }\n  }\n  else {\n    MatchState ms;\n    int anchor = (*p == '^') ? (p++, 1) : 0;\n    const char *s1=s+init;\n    ms.L = L;\n    ms.src_init = s;\n    ms.src_end = s+l1;\n    do {\n      const char *res;\n      ms.level = 0;\n      if ((res=match(&ms, s1, p)) != NULL) {\n        if (find) {\n          lua_pushinteger(L, s1-s+1);  /* start */\n          lua_pushinteger(L, res-s);   /* end */\n          return push_captures(&ms, NULL, 0) + 2;\n        }\n        else\n          return push_captures(&ms, s1, res);\n      }\n    } while (s1++ < ms.src_end && !anchor);\n  }\n  lua_pushnil(L);  /* not found */\n  return 1;\n}\n\n\nstatic int str_find (lua_State *L) {\n  return str_find_aux(L, 1);\n}\n\n\nstatic int str_match (lua_State *L) {\n  return str_find_aux(L, 0);\n}\n\n\nstatic int gmatch_aux (lua_State *L) {\n  MatchState ms;\n  size_t ls;\n  const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);\n  const char *p = lua_tostring(L, lua_upvalueindex(2));\n  const char *src;\n  ms.L = L;\n  ms.src_init = s;\n  ms.src_end = s+ls;\n  for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3));\n       src <= ms.src_end;\n       src++) {\n    const char *e;\n    ms.level = 0;\n    if ((e = match(&ms, src, p)) != NULL) {\n      lua_Integer newstart = e-s;\n      if (e == src) newstart++;  /* empty match? go at least one position */\n      lua_pushinteger(L, newstart);\n      lua_replace(L, lua_upvalueindex(3));\n      return push_captures(&ms, src, e);\n    }\n  }\n  return 0;  /* not found */\n}\n\n\nstatic int gmatch (lua_State *L) {\n  luaL_checkstring(L, 1);\n  luaL_checkstring(L, 2);\n  lua_settop(L, 2);\n  lua_pushinteger(L, 0);\n  lua_pushcclosure(L, gmatch_aux, 3);\n  return 1;\n}\n\n\nstatic int gfind_nodef (lua_State *L) {\n  return luaL_error(L, LUA_QL(\"string.gfind\") \" was renamed to \"\n                       LUA_QL(\"string.gmatch\"));\n}\n\n\nstatic void add_s (MatchState *ms, luaL_Buffer *b, const char *s,\n                                                   const char *e) {\n  size_t l, i;\n  const char *news = lua_tolstring(ms->L, 3, &l);\n  for (i = 0; i < l; i++) {\n    if (news[i] != L_ESC)\n      luaL_addchar(b, news[i]);\n    else {\n      i++;  /* skip ESC */\n      if (!isdigit(uchar(news[i])))\n        luaL_addchar(b, news[i]);\n      else if (news[i] == '0')\n          luaL_addlstring(b, s, e - s);\n      else {\n        push_onecapture(ms, news[i] - '1', s, e);\n        luaL_addvalue(b);  /* add capture to accumulated result */\n      }\n    }\n  }\n}\n\n\nstatic void add_value (MatchState *ms, luaL_Buffer *b, const char *s,\n                                                       const char *e) {\n  lua_State *L = ms->L;\n  switch (lua_type(L, 3)) {\n    case LUA_TNUMBER:\n    case LUA_TSTRING: {\n      add_s(ms, b, s, e);\n      return;\n    }\n    case LUA_TFUNCTION: {\n      int n;\n      lua_pushvalue(L, 3);\n      n = push_captures(ms, s, e);\n      lua_call(L, n, 1);\n      break;\n    }\n    case LUA_TTABLE: {\n      push_onecapture(ms, 0, s, e);\n      lua_gettable(L, 3);\n      break;\n    }\n  }\n  if (!lua_toboolean(L, -1)) {  /* nil or false? */\n    lua_pop(L, 1);\n    lua_pushlstring(L, s, e - s);  /* keep original text */\n  }\n  else if (!lua_isstring(L, -1))\n    luaL_error(L, \"invalid replacement value (a %s)\", luaL_typename(L, -1)); \n  luaL_addvalue(b);  /* add result to accumulator */\n}\n\n\nstatic int str_gsub (lua_State *L) {\n  size_t srcl;\n  const char *src = luaL_checklstring(L, 1, &srcl);\n  const char *p = luaL_checkstring(L, 2);\n  int  tr = lua_type(L, 3);\n  int max_s = luaL_optint(L, 4, srcl+1);\n  int anchor = (*p == '^') ? (p++, 1) : 0;\n  int n = 0;\n  MatchState ms;\n  luaL_Buffer b;\n  luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING ||\n                   tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3,\n                      \"string/function/table expected\");\n  luaL_buffinit(L, &b);\n  ms.L = L;\n  ms.src_init = src;\n  ms.src_end = src+srcl;\n  while (n < max_s) {\n    const char *e;\n    ms.level = 0;\n    e = match(&ms, src, p);\n    if (e) {\n      n++;\n      add_value(&ms, &b, src, e);\n    }\n    if (e && e>src) /* non empty match? */\n      src = e;  /* skip it */\n    else if (src < ms.src_end)\n      luaL_addchar(&b, *src++);\n    else break;\n    if (anchor) break;\n  }\n  luaL_addlstring(&b, src, ms.src_end-src);\n  luaL_pushresult(&b);\n  lua_pushinteger(L, n);  /* number of substitutions */\n  return 2;\n}\n\n/* }====================================================== */\n\n\n/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */\n#define MAX_ITEM\t512\n/* valid flags in a format specification */\n#define FLAGS\t\"-+ #0\"\n/*\n** maximum size of each format specification (such as '%-099.99d')\n** (+10 accounts for %99.99x plus margin of error)\n*/\n#define MAX_FORMAT\t(sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10)\n\n\nstatic void addquoted (lua_State *L, luaL_Buffer *b, int arg) {\n  size_t l;\n  const char *s = luaL_checklstring(L, arg, &l);\n  luaL_addchar(b, '\"');\n  while (l--) {\n    switch (*s) {\n      case '\"': case '\\\\': case '\\n': {\n        luaL_addchar(b, '\\\\');\n        luaL_addchar(b, *s);\n        break;\n      }\n      case '\\r': {\n        luaL_addlstring(b, \"\\\\r\", 2);\n        break;\n      }\n      case '\\0': {\n        luaL_addlstring(b, \"\\\\000\", 4);\n        break;\n      }\n      default: {\n        luaL_addchar(b, *s);\n        break;\n      }\n    }\n    s++;\n  }\n  luaL_addchar(b, '\"');\n}\n\nstatic const char *scanformat (lua_State *L, const char *strfrmt, char *form) {\n  const char *p = strfrmt;\n  while (*p != '\\0' && strchr(FLAGS, *p) != NULL) p++;  /* skip flags */\n  if ((size_t)(p - strfrmt) >= sizeof(FLAGS))\n    luaL_error(L, \"invalid format (repeated flags)\");\n  if (isdigit(uchar(*p))) p++;  /* skip width */\n  if (isdigit(uchar(*p))) p++;  /* (2 digits at most) */\n  if (*p == '.') {\n    p++;\n    if (isdigit(uchar(*p))) p++;  /* skip precision */\n    if (isdigit(uchar(*p))) p++;  /* (2 digits at most) */\n  }\n  if (isdigit(uchar(*p)))\n    luaL_error(L, \"invalid format (width or precision too long)\");\n  *(form++) = '%';\n  strncpy(form, strfrmt, p - strfrmt + 1);\n  form += p - strfrmt + 1;\n  *form = '\\0';\n  return p;\n}\n\n\nstatic void addintlen (char *form) {\n  size_t l = strlen(form);\n  char spec = form[l - 1];\n  strcpy(form + l - 1, LUA_INTFRMLEN);\n  form[l + sizeof(LUA_INTFRMLEN) - 2] = spec;\n  form[l + sizeof(LUA_INTFRMLEN) - 1] = '\\0';\n}\n\n\nstatic int str_format (lua_State *L) {\n  int top = lua_gettop(L);\n  int arg = 1;\n  size_t sfl;\n  const char *strfrmt = luaL_checklstring(L, arg, &sfl);\n  const char *strfrmt_end = strfrmt+sfl;\n  luaL_Buffer b;\n  luaL_buffinit(L, &b);\n  while (strfrmt < strfrmt_end) {\n    if (*strfrmt != L_ESC)\n      luaL_addchar(&b, *strfrmt++);\n    else if (*++strfrmt == L_ESC)\n      luaL_addchar(&b, *strfrmt++);  /* %% */\n    else { /* format item */\n      char form[MAX_FORMAT];  /* to store the format (`%...') */\n      char buff[MAX_ITEM];  /* to store the formatted item */\n      if (++arg > top)\n        luaL_argerror(L, arg, \"no value\");\n      strfrmt = scanformat(L, strfrmt, form);\n      switch (*strfrmt++) {\n        case 'c': {\n          sprintf(buff, form, (int)luaL_checknumber(L, arg));\n          break;\n        }\n        case 'd':  case 'i': {\n          addintlen(form);\n          sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg));\n          break;\n        }\n        case 'o':  case 'u':  case 'x':  case 'X': {\n          addintlen(form);\n          sprintf(buff, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg));\n          break;\n        }\n        case 'e':  case 'E': case 'f':\n        case 'g': case 'G': {\n          sprintf(buff, form, (double)luaL_checknumber(L, arg));\n          break;\n        }\n        case 'q': {\n          addquoted(L, &b, arg);\n          continue;  /* skip the 'addsize' at the end */\n        }\n        case 's': {\n          size_t l;\n          const char *s = luaL_checklstring(L, arg, &l);\n          if (!strchr(form, '.') && l >= 100) {\n            /* no precision and string is too long to be formatted;\n               keep original string */\n            lua_pushvalue(L, arg);\n            luaL_addvalue(&b);\n            continue;  /* skip the `addsize' at the end */\n          }\n          else {\n            sprintf(buff, form, s);\n            break;\n          }\n        }\n        default: {  /* also treat cases `pnLlh' */\n          return luaL_error(L, \"invalid option \" LUA_QL(\"%%%c\") \" to \"\n                               LUA_QL(\"format\"), *(strfrmt - 1));\n        }\n      }\n      luaL_addlstring(&b, buff, strlen(buff));\n    }\n  }\n  luaL_pushresult(&b);\n  return 1;\n}\n\n\nstatic const luaL_Reg strlib[] = {\n  {\"byte\", str_byte},\n  {\"char\", str_char},\n  {\"dump\", str_dump},\n  {\"find\", str_find},\n  {\"format\", str_format},\n  {\"gfind\", gfind_nodef},\n  {\"gmatch\", gmatch},\n  {\"gsub\", str_gsub},\n  {\"len\", str_len},\n  {\"lower\", str_lower},\n  {\"match\", str_match},\n  {\"rep\", str_rep},\n  {\"reverse\", str_reverse},\n  {\"sub\", str_sub},\n  {\"upper\", str_upper},\n  {NULL, NULL}\n};\n\n\nstatic void createmetatable (lua_State *L) {\n  lua_createtable(L, 0, 1);  /* create metatable for strings */\n  lua_pushliteral(L, \"\");  /* dummy string */\n  lua_pushvalue(L, -2);\n  lua_setmetatable(L, -2);  /* set string metatable */\n  lua_pop(L, 1);  /* pop dummy string */\n  lua_pushvalue(L, -2);  /* string library... */\n  lua_setfield(L, -2, \"__index\");  /* ...is the __index metamethod */\n  lua_pop(L, 1);  /* pop metatable */\n}\n\n\n/*\n** Open string library\n*/\nLUALIB_API int luaopen_string (lua_State *L) {\n  luaL_register(L, LUA_STRLIBNAME, strlib);\n#if defined(LUA_COMPAT_GFIND)\n  lua_getfield(L, -1, \"gmatch\");\n  lua_setfield(L, -2, \"gfind\");\n#endif\n  createmetatable(L);\n  return 1;\n}\n\n"
  },
  {
    "path": "deps/lua/src/ltable.c",
    "content": "/*\n** $Id: ltable.c,v 2.32.1.2 2007/12/28 15:32:23 roberto Exp $\n** Lua tables (hash)\n** See Copyright Notice in lua.h\n*/\n\n\n/*\n** Implementation of tables (aka arrays, objects, or hash tables).\n** Tables keep its elements in two parts: an array part and a hash part.\n** Non-negative integer keys are all candidates to be kept in the array\n** part. The actual size of the array is the largest `n' such that at\n** least half the slots between 0 and n are in use.\n** Hash uses a mix of chained scatter table with Brent's variation.\n** A main invariant of these tables is that, if an element is not\n** in its main position (i.e. the `original' position that its hash gives\n** to it), then the colliding element is in its own main position.\n** Hence even when the load factor reaches 100%, performance remains good.\n*/\n\n#include <math.h>\n#include <string.h>\n\n#define ltable_c\n#define LUA_CORE\n\n#include \"lua.h\"\n\n#include \"ldebug.h\"\n#include \"ldo.h\"\n#include \"lgc.h\"\n#include \"lmem.h\"\n#include \"lobject.h\"\n#include \"lstate.h\"\n#include \"ltable.h\"\n\n\n/*\n** max size of array part is 2^MAXBITS\n*/\n#if LUAI_BITSINT > 26\n#define MAXBITS\t\t26\n#else\n#define MAXBITS\t\t(LUAI_BITSINT-2)\n#endif\n\n#define MAXASIZE\t(1 << MAXBITS)\n\n\n#define hashpow2(t,n)      (gnode(t, lmod((n), sizenode(t))))\n  \n#define hashstr(t,str)  hashpow2(t, (str)->tsv.hash)\n#define hashboolean(t,p)        hashpow2(t, p)\n\n\n/*\n** for some types, it is better to avoid modulus by power of 2, as\n** they tend to have many 2 factors.\n*/\n#define hashmod(t,n)\t(gnode(t, ((n) % ((sizenode(t)-1)|1))))\n\n\n#define hashpointer(t,p)\thashmod(t, IntPoint(p))\n\n\n/*\n** number of ints inside a lua_Number\n*/\n#define numints\t\tcast_int(sizeof(lua_Number)/sizeof(int))\n\n\n\n#define dummynode\t\t(&dummynode_)\n\nstatic const Node dummynode_ = {\n  {{NULL}, LUA_TNIL},  /* value */\n  {{{NULL}, LUA_TNIL, NULL}}  /* key */\n};\n\n\n/*\n** hash for lua_Numbers\n*/\nstatic Node *hashnum (const Table *t, lua_Number n) {\n  unsigned int a[numints];\n  int i;\n  if (luai_numeq(n, 0))  /* avoid problems with -0 */\n    return gnode(t, 0);\n  memcpy(a, &n, sizeof(a));\n  for (i = 1; i < numints; i++) a[0] += a[i];\n  return hashmod(t, a[0]);\n}\n\n\n\n/*\n** returns the `main' position of an element in a table (that is, the index\n** of its hash value)\n*/\nstatic Node *mainposition (const Table *t, const TValue *key) {\n  switch (ttype(key)) {\n    case LUA_TNUMBER:\n      return hashnum(t, nvalue(key));\n    case LUA_TSTRING:\n      return hashstr(t, rawtsvalue(key));\n    case LUA_TBOOLEAN:\n      return hashboolean(t, bvalue(key));\n    case LUA_TLIGHTUSERDATA:\n      return hashpointer(t, pvalue(key));\n    default:\n      return hashpointer(t, gcvalue(key));\n  }\n}\n\n\n/*\n** returns the index for `key' if `key' is an appropriate key to live in\n** the array part of the table, -1 otherwise.\n*/\nstatic int arrayindex (const TValue *key) {\n  if (ttisnumber(key)) {\n    lua_Number n = nvalue(key);\n    int k;\n    lua_number2int(k, n);\n    if (luai_numeq(cast_num(k), n))\n      return k;\n  }\n  return -1;  /* `key' did not match some condition */\n}\n\n\n/*\n** returns the index of a `key' for table traversals. First goes all\n** elements in the array part, then elements in the hash part. The\n** beginning of a traversal is signalled by -1.\n*/\nstatic int findindex (lua_State *L, Table *t, StkId key) {\n  int i;\n  if (ttisnil(key)) return -1;  /* first iteration */\n  i = arrayindex(key);\n  if (0 < i && i <= t->sizearray)  /* is `key' inside array part? */\n    return i-1;  /* yes; that's the index (corrected to C) */\n  else {\n    Node *n = mainposition(t, key);\n    do {  /* check whether `key' is somewhere in the chain */\n      /* key may be dead already, but it is ok to use it in `next' */\n      if (luaO_rawequalObj(key2tval(n), key) ||\n            (ttype(gkey(n)) == LUA_TDEADKEY && iscollectable(key) &&\n             gcvalue(gkey(n)) == gcvalue(key))) {\n        i = cast_int(n - gnode(t, 0));  /* key index in hash table */\n        /* hash elements are numbered after array ones */\n        return i + t->sizearray;\n      }\n      else n = gnext(n);\n    } while (n);\n    luaG_runerror(L, \"invalid key to \" LUA_QL(\"next\"));  /* key not found */\n    return 0;  /* to avoid warnings */\n  }\n}\n\n\nint luaH_next (lua_State *L, Table *t, StkId key) {\n  int i = findindex(L, t, key);  /* find original element */\n  for (i++; i < t->sizearray; i++) {  /* try first array part */\n    if (!ttisnil(&t->array[i])) {  /* a non-nil value? */\n      setnvalue(key, cast_num(i+1));\n      setobj2s(L, key+1, &t->array[i]);\n      return 1;\n    }\n  }\n  for (i -= t->sizearray; i < sizenode(t); i++) {  /* then hash part */\n    if (!ttisnil(gval(gnode(t, i)))) {  /* a non-nil value? */\n      setobj2s(L, key, key2tval(gnode(t, i)));\n      setobj2s(L, key+1, gval(gnode(t, i)));\n      return 1;\n    }\n  }\n  return 0;  /* no more elements */\n}\n\n\n/*\n** {=============================================================\n** Rehash\n** ==============================================================\n*/\n\n\nstatic int computesizes (int nums[], int *narray) {\n  int i;\n  int twotoi;  /* 2^i */\n  int a = 0;  /* number of elements smaller than 2^i */\n  int na = 0;  /* number of elements to go to array part */\n  int n = 0;  /* optimal size for array part */\n  for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) {\n    if (nums[i] > 0) {\n      a += nums[i];\n      if (a > twotoi/2) {  /* more than half elements present? */\n        n = twotoi;  /* optimal size (till now) */\n        na = a;  /* all elements smaller than n will go to array part */\n      }\n    }\n    if (a == *narray) break;  /* all elements already counted */\n  }\n  *narray = n;\n  lua_assert(*narray/2 <= na && na <= *narray);\n  return na;\n}\n\n\nstatic int countint (const TValue *key, int *nums) {\n  int k = arrayindex(key);\n  if (0 < k && k <= MAXASIZE) {  /* is `key' an appropriate array index? */\n    nums[ceillog2(k)]++;  /* count as such */\n    return 1;\n  }\n  else\n    return 0;\n}\n\n\nstatic int numusearray (const Table *t, int *nums) {\n  int lg;\n  int ttlg;  /* 2^lg */\n  int ause = 0;  /* summation of `nums' */\n  int i = 1;  /* count to traverse all array keys */\n  for (lg=0, ttlg=1; lg<=MAXBITS; lg++, ttlg*=2) {  /* for each slice */\n    int lc = 0;  /* counter */\n    int lim = ttlg;\n    if (lim > t->sizearray) {\n      lim = t->sizearray;  /* adjust upper limit */\n      if (i > lim)\n        break;  /* no more elements to count */\n    }\n    /* count elements in range (2^(lg-1), 2^lg] */\n    for (; i <= lim; i++) {\n      if (!ttisnil(&t->array[i-1]))\n        lc++;\n    }\n    nums[lg] += lc;\n    ause += lc;\n  }\n  return ause;\n}\n\n\nstatic int numusehash (const Table *t, int *nums, int *pnasize) {\n  int totaluse = 0;  /* total number of elements */\n  int ause = 0;  /* summation of `nums' */\n  int i = sizenode(t);\n  while (i--) {\n    Node *n = &t->node[i];\n    if (!ttisnil(gval(n))) {\n      ause += countint(key2tval(n), nums);\n      totaluse++;\n    }\n  }\n  *pnasize += ause;\n  return totaluse;\n}\n\n\nstatic void setarrayvector (lua_State *L, Table *t, int size) {\n  int i;\n  luaM_reallocvector(L, t->array, t->sizearray, size, TValue);\n  for (i=t->sizearray; i<size; i++)\n     setnilvalue(&t->array[i]);\n  t->sizearray = size;\n}\n\n\nstatic void setnodevector (lua_State *L, Table *t, int size) {\n  int lsize;\n  if (size == 0) {  /* no elements to hash part? */\n    t->node = cast(Node *, dummynode);  /* use common `dummynode' */\n    lsize = 0;\n  }\n  else {\n    int i;\n    lsize = ceillog2(size);\n    if (lsize > MAXBITS)\n      luaG_runerror(L, \"table overflow\");\n    size = twoto(lsize);\n    t->node = luaM_newvector(L, size, Node);\n    for (i=0; i<size; i++) {\n      Node *n = gnode(t, i);\n      gnext(n) = NULL;\n      setnilvalue(gkey(n));\n      setnilvalue(gval(n));\n    }\n  }\n  t->lsizenode = cast_byte(lsize);\n  t->lastfree = gnode(t, size);  /* all positions are free */\n}\n\n\nstatic void resize (lua_State *L, Table *t, int nasize, int nhsize) {\n  int i;\n  int oldasize = t->sizearray;\n  int oldhsize = t->lsizenode;\n  Node *nold = t->node;  /* save old hash ... */\n  if (nasize > oldasize)  /* array part must grow? */\n    setarrayvector(L, t, nasize);\n  /* create new hash part with appropriate size */\n  setnodevector(L, t, nhsize);  \n  if (nasize < oldasize) {  /* array part must shrink? */\n    t->sizearray = nasize;\n    /* re-insert elements from vanishing slice */\n    for (i=nasize; i<oldasize; i++) {\n      if (!ttisnil(&t->array[i]))\n        setobjt2t(L, luaH_setnum(L, t, i+1), &t->array[i]);\n    }\n    /* shrink array */\n    luaM_reallocvector(L, t->array, oldasize, nasize, TValue);\n  }\n  /* re-insert elements from hash part */\n  for (i = twoto(oldhsize) - 1; i >= 0; i--) {\n    Node *old = nold+i;\n    if (!ttisnil(gval(old)))\n      setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old));\n  }\n  if (nold != dummynode)\n    luaM_freearray(L, nold, twoto(oldhsize), Node);  /* free old array */\n}\n\n\nvoid luaH_resizearray (lua_State *L, Table *t, int nasize) {\n  int nsize = (t->node == dummynode) ? 0 : sizenode(t);\n  resize(L, t, nasize, nsize);\n}\n\n\nstatic void rehash (lua_State *L, Table *t, const TValue *ek) {\n  int nasize, na;\n  int nums[MAXBITS+1];  /* nums[i] = number of keys between 2^(i-1) and 2^i */\n  int i;\n  int totaluse;\n  for (i=0; i<=MAXBITS; i++) nums[i] = 0;  /* reset counts */\n  nasize = numusearray(t, nums);  /* count keys in array part */\n  totaluse = nasize;  /* all those keys are integer keys */\n  totaluse += numusehash(t, nums, &nasize);  /* count keys in hash part */\n  /* count extra key */\n  nasize += countint(ek, nums);\n  totaluse++;\n  /* compute new size for array part */\n  na = computesizes(nums, &nasize);\n  /* resize the table to new computed sizes */\n  resize(L, t, nasize, totaluse - na);\n}\n\n\n\n/*\n** }=============================================================\n*/\n\n\nTable *luaH_new (lua_State *L, int narray, int nhash) {\n  Table *t = luaM_new(L, Table);\n  luaC_link(L, obj2gco(t), LUA_TTABLE);\n  t->metatable = NULL;\n  t->flags = cast_byte(~0);\n  /* temporary values (kept only if some malloc fails) */\n  t->array = NULL;\n  t->sizearray = 0;\n  t->lsizenode = 0;\n  t->node = cast(Node *, dummynode);\n  setarrayvector(L, t, narray);\n  setnodevector(L, t, nhash);\n  return t;\n}\n\n\nvoid luaH_free (lua_State *L, Table *t) {\n  if (t->node != dummynode)\n    luaM_freearray(L, t->node, sizenode(t), Node);\n  luaM_freearray(L, t->array, t->sizearray, TValue);\n  luaM_free(L, t);\n}\n\n\nstatic Node *getfreepos (Table *t) {\n  while (t->lastfree-- > t->node) {\n    if (ttisnil(gkey(t->lastfree)))\n      return t->lastfree;\n  }\n  return NULL;  /* could not find a free place */\n}\n\n\n\n/*\n** inserts a new key into a hash table; first, check whether key's main \n** position is free. If not, check whether colliding node is in its main \n** position or not: if it is not, move colliding node to an empty place and \n** put new key in its main position; otherwise (colliding node is in its main \n** position), new key goes to an empty position. \n*/\nstatic TValue *newkey (lua_State *L, Table *t, const TValue *key) {\n  Node *mp = mainposition(t, key);\n  if (!ttisnil(gval(mp)) || mp == dummynode) {\n    Node *othern;\n    Node *n = getfreepos(t);  /* get a free place */\n    if (n == NULL) {  /* cannot find a free place? */\n      rehash(L, t, key);  /* grow table */\n      return luaH_set(L, t, key);  /* re-insert key into grown table */\n    }\n    lua_assert(n != dummynode);\n    othern = mainposition(t, key2tval(mp));\n    if (othern != mp) {  /* is colliding node out of its main position? */\n      /* yes; move colliding node into free position */\n      while (gnext(othern) != mp) othern = gnext(othern);  /* find previous */\n      gnext(othern) = n;  /* redo the chain with `n' in place of `mp' */\n      *n = *mp;  /* copy colliding node into free pos. (mp->next also goes) */\n      gnext(mp) = NULL;  /* now `mp' is free */\n      setnilvalue(gval(mp));\n    }\n    else {  /* colliding node is in its own main position */\n      /* new node will go into free position */\n      gnext(n) = gnext(mp);  /* chain new position */\n      gnext(mp) = n;\n      mp = n;\n    }\n  }\n  gkey(mp)->value = key->value; gkey(mp)->tt = key->tt;\n  luaC_barriert(L, t, key);\n  lua_assert(ttisnil(gval(mp)));\n  return gval(mp);\n}\n\n\n/*\n** search function for integers\n*/\nconst TValue *luaH_getnum (Table *t, int key) {\n  /* (1 <= key && key <= t->sizearray) */\n  if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray))\n    return &t->array[key-1];\n  else {\n    lua_Number nk = cast_num(key);\n    Node *n = hashnum(t, nk);\n    do {  /* check whether `key' is somewhere in the chain */\n      if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk))\n        return gval(n);  /* that's it */\n      else n = gnext(n);\n    } while (n);\n    return luaO_nilobject;\n  }\n}\n\n\n/*\n** search function for strings\n*/\nconst TValue *luaH_getstr (Table *t, TString *key) {\n  Node *n = hashstr(t, key);\n  do {  /* check whether `key' is somewhere in the chain */\n    if (ttisstring(gkey(n)) && rawtsvalue(gkey(n)) == key)\n      return gval(n);  /* that's it */\n    else n = gnext(n);\n  } while (n);\n  return luaO_nilobject;\n}\n\n\n/*\n** main search function\n*/\nconst TValue *luaH_get (Table *t, const TValue *key) {\n  switch (ttype(key)) {\n    case LUA_TNIL: return luaO_nilobject;\n    case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key));\n    case LUA_TNUMBER: {\n      int k;\n      lua_Number n = nvalue(key);\n      lua_number2int(k, n);\n      if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */\n        return luaH_getnum(t, k);  /* use specialized version */\n      /* else go through */\n    }\n    default: {\n      Node *n = mainposition(t, key);\n      do {  /* check whether `key' is somewhere in the chain */\n        if (luaO_rawequalObj(key2tval(n), key))\n          return gval(n);  /* that's it */\n        else n = gnext(n);\n      } while (n);\n      return luaO_nilobject;\n    }\n  }\n}\n\n\nTValue *luaH_set (lua_State *L, Table *t, const TValue *key) {\n  const TValue *p = luaH_get(t, key);\n  t->flags = 0;\n  if (p != luaO_nilobject)\n    return cast(TValue *, p);\n  else {\n    if (ttisnil(key)) luaG_runerror(L, \"table index is nil\");\n    else if (ttisnumber(key) && luai_numisnan(nvalue(key)))\n      luaG_runerror(L, \"table index is NaN\");\n    return newkey(L, t, key);\n  }\n}\n\n\nTValue *luaH_setnum (lua_State *L, Table *t, int key) {\n  const TValue *p = luaH_getnum(t, key);\n  if (p != luaO_nilobject)\n    return cast(TValue *, p);\n  else {\n    TValue k;\n    setnvalue(&k, cast_num(key));\n    return newkey(L, t, &k);\n  }\n}\n\n\nTValue *luaH_setstr (lua_State *L, Table *t, TString *key) {\n  const TValue *p = luaH_getstr(t, key);\n  if (p != luaO_nilobject)\n    return cast(TValue *, p);\n  else {\n    TValue k;\n    setsvalue(L, &k, key);\n    return newkey(L, t, &k);\n  }\n}\n\n\nstatic int unbound_search (Table *t, unsigned int j) {\n  unsigned int i = j;  /* i is zero or a present index */\n  j++;\n  /* find `i' and `j' such that i is present and j is not */\n  while (!ttisnil(luaH_getnum(t, j))) {\n    i = j;\n    j *= 2;\n    if (j > cast(unsigned int, MAX_INT)) {  /* overflow? */\n      /* table was built with bad purposes: resort to linear search */\n      i = 1;\n      while (!ttisnil(luaH_getnum(t, i))) i++;\n      return i - 1;\n    }\n  }\n  /* now do a binary search between them */\n  while (j - i > 1) {\n    unsigned int m = (i+j)/2;\n    if (ttisnil(luaH_getnum(t, m))) j = m;\n    else i = m;\n  }\n  return i;\n}\n\n\n/*\n** Try to find a boundary in table `t'. A `boundary' is an integer index\n** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).\n*/\nint luaH_getn (Table *t) {\n  unsigned int j = t->sizearray;\n  if (j > 0 && ttisnil(&t->array[j - 1])) {\n    /* there is a boundary in the array part: (binary) search for it */\n    unsigned int i = 0;\n    while (j - i > 1) {\n      unsigned int m = (i+j)/2;\n      if (ttisnil(&t->array[m - 1])) j = m;\n      else i = m;\n    }\n    return i;\n  }\n  /* else must find a boundary in hash part */\n  else if (t->node == dummynode)  /* hash part is empty? */\n    return j;  /* that is easy... */\n  else return unbound_search(t, j);\n}\n\n\n\n#if defined(LUA_DEBUG)\n\nNode *luaH_mainposition (const Table *t, const TValue *key) {\n  return mainposition(t, key);\n}\n\nint luaH_isdummy (Node *n) { return n == dummynode; }\n\n#endif\n"
  },
  {
    "path": "deps/lua/src/ltable.h",
    "content": "/*\n** $Id: ltable.h,v 2.10.1.1 2007/12/27 13:02:25 roberto Exp $\n** Lua tables (hash)\n** See Copyright Notice in lua.h\n*/\n\n#ifndef ltable_h\n#define ltable_h\n\n#include \"lobject.h\"\n\n\n#define gnode(t,i)\t(&(t)->node[i])\n#define gkey(n)\t\t(&(n)->i_key.nk)\n#define gval(n)\t\t(&(n)->i_val)\n#define gnext(n)\t((n)->i_key.nk.next)\n\n#define key2tval(n)\t(&(n)->i_key.tvk)\n\n\nLUAI_FUNC const TValue *luaH_getnum (Table *t, int key);\nLUAI_FUNC TValue *luaH_setnum (lua_State *L, Table *t, int key);\nLUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key);\nLUAI_FUNC TValue *luaH_setstr (lua_State *L, Table *t, TString *key);\nLUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key);\nLUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key);\nLUAI_FUNC Table *luaH_new (lua_State *L, int narray, int lnhash);\nLUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize);\nLUAI_FUNC void luaH_free (lua_State *L, Table *t);\nLUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key);\nLUAI_FUNC int luaH_getn (Table *t);\n\n\n#if defined(LUA_DEBUG)\nLUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key);\nLUAI_FUNC int luaH_isdummy (Node *n);\n#endif\n\n\n#endif\n"
  },
  {
    "path": "deps/lua/src/ltablib.c",
    "content": "/*\n** $Id: ltablib.c,v 1.38.1.3 2008/02/14 16:46:58 roberto Exp $\n** Library for Table Manipulation\n** See Copyright Notice in lua.h\n*/\n\n\n#include <stddef.h>\n\n#define ltablib_c\n#define LUA_LIB\n\n#include \"lua.h\"\n\n#include \"lauxlib.h\"\n#include \"lualib.h\"\n\n\n#define aux_getn(L,n)\t(luaL_checktype(L, n, LUA_TTABLE), luaL_getn(L, n))\n\n\nstatic int foreachi (lua_State *L) {\n  int i;\n  int n = aux_getn(L, 1);\n  luaL_checktype(L, 2, LUA_TFUNCTION);\n  for (i=1; i <= n; i++) {\n    lua_pushvalue(L, 2);  /* function */\n    lua_pushinteger(L, i);  /* 1st argument */\n    lua_rawgeti(L, 1, i);  /* 2nd argument */\n    lua_call(L, 2, 1);\n    if (!lua_isnil(L, -1))\n      return 1;\n    lua_pop(L, 1);  /* remove nil result */\n  }\n  return 0;\n}\n\n\nstatic int foreach (lua_State *L) {\n  luaL_checktype(L, 1, LUA_TTABLE);\n  luaL_checktype(L, 2, LUA_TFUNCTION);\n  lua_pushnil(L);  /* first key */\n  while (lua_next(L, 1)) {\n    lua_pushvalue(L, 2);  /* function */\n    lua_pushvalue(L, -3);  /* key */\n    lua_pushvalue(L, -3);  /* value */\n    lua_call(L, 2, 1);\n    if (!lua_isnil(L, -1))\n      return 1;\n    lua_pop(L, 2);  /* remove value and result */\n  }\n  return 0;\n}\n\n\nstatic int maxn (lua_State *L) {\n  lua_Number max = 0;\n  luaL_checktype(L, 1, LUA_TTABLE);\n  lua_pushnil(L);  /* first key */\n  while (lua_next(L, 1)) {\n    lua_pop(L, 1);  /* remove value */\n    if (lua_type(L, -1) == LUA_TNUMBER) {\n      lua_Number v = lua_tonumber(L, -1);\n      if (v > max) max = v;\n    }\n  }\n  lua_pushnumber(L, max);\n  return 1;\n}\n\n\nstatic int getn (lua_State *L) {\n  lua_pushinteger(L, aux_getn(L, 1));\n  return 1;\n}\n\n\nstatic int setn (lua_State *L) {\n  luaL_checktype(L, 1, LUA_TTABLE);\n#ifndef luaL_setn\n  luaL_setn(L, 1, luaL_checkint(L, 2));\n#else\n  luaL_error(L, LUA_QL(\"setn\") \" is obsolete\");\n#endif\n  lua_pushvalue(L, 1);\n  return 1;\n}\n\n\nstatic int tinsert (lua_State *L) {\n  int e = aux_getn(L, 1) + 1;  /* first empty element */\n  int pos;  /* where to insert new element */\n  switch (lua_gettop(L)) {\n    case 2: {  /* called with only 2 arguments */\n      pos = e;  /* insert new element at the end */\n      break;\n    }\n    case 3: {\n      int i;\n      pos = luaL_checkint(L, 2);  /* 2nd argument is the position */\n      if (pos > e) e = pos;  /* `grow' array if necessary */\n      for (i = e; i > pos; i--) {  /* move up elements */\n        lua_rawgeti(L, 1, i-1);\n        lua_rawseti(L, 1, i);  /* t[i] = t[i-1] */\n      }\n      break;\n    }\n    default: {\n      return luaL_error(L, \"wrong number of arguments to \" LUA_QL(\"insert\"));\n    }\n  }\n  luaL_setn(L, 1, e);  /* new size */\n  lua_rawseti(L, 1, pos);  /* t[pos] = v */\n  return 0;\n}\n\n\nstatic int tremove (lua_State *L) {\n  int e = aux_getn(L, 1);\n  int pos = luaL_optint(L, 2, e);\n  if (!(1 <= pos && pos <= e))  /* position is outside bounds? */\n   return 0;  /* nothing to remove */\n  luaL_setn(L, 1, e - 1);  /* t.n = n-1 */\n  lua_rawgeti(L, 1, pos);  /* result = t[pos] */\n  for ( ;pos<e; pos++) {\n    lua_rawgeti(L, 1, pos+1);\n    lua_rawseti(L, 1, pos);  /* t[pos] = t[pos+1] */\n  }\n  lua_pushnil(L);\n  lua_rawseti(L, 1, e);  /* t[e] = nil */\n  return 1;\n}\n\n\nstatic void addfield (lua_State *L, luaL_Buffer *b, int i) {\n  lua_rawgeti(L, 1, i);\n  if (!lua_isstring(L, -1))\n    luaL_error(L, \"invalid value (%s) at index %d in table for \"\n                  LUA_QL(\"concat\"), luaL_typename(L, -1), i);\n  luaL_addvalue(b);\n}\n\n\nstatic int tconcat (lua_State *L) {\n  luaL_Buffer b;\n  size_t lsep;\n  int i, last;\n  const char *sep = luaL_optlstring(L, 2, \"\", &lsep);\n  luaL_checktype(L, 1, LUA_TTABLE);\n  i = luaL_optint(L, 3, 1);\n  last = luaL_opt(L, luaL_checkint, 4, luaL_getn(L, 1));\n  luaL_buffinit(L, &b);\n  for (; i < last; i++) {\n    addfield(L, &b, i);\n    luaL_addlstring(&b, sep, lsep);\n  }\n  if (i == last)  /* add last value (if interval was not empty) */\n    addfield(L, &b, i);\n  luaL_pushresult(&b);\n  return 1;\n}\n\n\n\n/*\n** {======================================================\n** Quicksort\n** (based on `Algorithms in MODULA-3', Robert Sedgewick;\n**  Addison-Wesley, 1993.)\n*/\n\n\nstatic void set2 (lua_State *L, int i, int j) {\n  lua_rawseti(L, 1, i);\n  lua_rawseti(L, 1, j);\n}\n\nstatic int sort_comp (lua_State *L, int a, int b) {\n  if (!lua_isnil(L, 2)) {  /* function? */\n    int res;\n    lua_pushvalue(L, 2);\n    lua_pushvalue(L, a-1);  /* -1 to compensate function */\n    lua_pushvalue(L, b-2);  /* -2 to compensate function and `a' */\n    lua_call(L, 2, 1);\n    res = lua_toboolean(L, -1);\n    lua_pop(L, 1);\n    return res;\n  }\n  else  /* a < b? */\n    return lua_lessthan(L, a, b);\n}\n\nstatic void auxsort (lua_State *L, int l, int u) {\n  while (l < u) {  /* for tail recursion */\n    int i, j;\n    /* sort elements a[l], a[(l+u)/2] and a[u] */\n    lua_rawgeti(L, 1, l);\n    lua_rawgeti(L, 1, u);\n    if (sort_comp(L, -1, -2))  /* a[u] < a[l]? */\n      set2(L, l, u);  /* swap a[l] - a[u] */\n    else\n      lua_pop(L, 2);\n    if (u-l == 1) break;  /* only 2 elements */\n    i = (l+u)/2;\n    lua_rawgeti(L, 1, i);\n    lua_rawgeti(L, 1, l);\n    if (sort_comp(L, -2, -1))  /* a[i]<a[l]? */\n      set2(L, i, l);\n    else {\n      lua_pop(L, 1);  /* remove a[l] */\n      lua_rawgeti(L, 1, u);\n      if (sort_comp(L, -1, -2))  /* a[u]<a[i]? */\n        set2(L, i, u);\n      else\n        lua_pop(L, 2);\n    }\n    if (u-l == 2) break;  /* only 3 elements */\n    lua_rawgeti(L, 1, i);  /* Pivot */\n    lua_pushvalue(L, -1);\n    lua_rawgeti(L, 1, u-1);\n    set2(L, i, u-1);\n    /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */\n    i = l; j = u-1;\n    for (;;) {  /* invariant: a[l..i] <= P <= a[j..u] */\n      /* repeat ++i until a[i] >= P */\n      while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {\n        if (i>u) luaL_error(L, \"invalid order function for sorting\");\n        lua_pop(L, 1);  /* remove a[i] */\n      }\n      /* repeat --j until a[j] <= P */\n      while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) {\n        if (j<l) luaL_error(L, \"invalid order function for sorting\");\n        lua_pop(L, 1);  /* remove a[j] */\n      }\n      if (j<i) {\n        lua_pop(L, 3);  /* pop pivot, a[i], a[j] */\n        break;\n      }\n      set2(L, i, j);\n    }\n    lua_rawgeti(L, 1, u-1);\n    lua_rawgeti(L, 1, i);\n    set2(L, u-1, i);  /* swap pivot (a[u-1]) with a[i] */\n    /* a[l..i-1] <= a[i] == P <= a[i+1..u] */\n    /* adjust so that smaller half is in [j..i] and larger one in [l..u] */\n    if (i-l < u-i) {\n      j=l; i=i-1; l=i+2;\n    }\n    else {\n      j=i+1; i=u; u=j-2;\n    }\n    auxsort(L, j, i);  /* call recursively the smaller one */\n  }  /* repeat the routine for the larger one */\n}\n\nstatic int sort (lua_State *L) {\n  int n = aux_getn(L, 1);\n  luaL_checkstack(L, 40, \"\");  /* assume array is smaller than 2^40 */\n  if (!lua_isnoneornil(L, 2))  /* is there a 2nd argument? */\n    luaL_checktype(L, 2, LUA_TFUNCTION);\n  lua_settop(L, 2);  /* make sure there is two arguments */\n  auxsort(L, 1, n);\n  return 0;\n}\n\n/* }====================================================== */\n\n\nstatic const luaL_Reg tab_funcs[] = {\n  {\"concat\", tconcat},\n  {\"foreach\", foreach},\n  {\"foreachi\", foreachi},\n  {\"getn\", getn},\n  {\"maxn\", maxn},\n  {\"insert\", tinsert},\n  {\"remove\", tremove},\n  {\"setn\", setn},\n  {\"sort\", sort},\n  {NULL, NULL}\n};\n\n\nLUALIB_API int luaopen_table (lua_State *L) {\n  luaL_register(L, LUA_TABLIBNAME, tab_funcs);\n  return 1;\n}\n\n"
  },
  {
    "path": "deps/lua/src/ltm.c",
    "content": "/*\n** $Id: ltm.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $\n** Tag methods\n** See Copyright Notice in lua.h\n*/\n\n\n#include <string.h>\n\n#define ltm_c\n#define LUA_CORE\n\n#include \"lua.h\"\n\n#include \"lobject.h\"\n#include \"lstate.h\"\n#include \"lstring.h\"\n#include \"ltable.h\"\n#include \"ltm.h\"\n\n\n\nconst char *const luaT_typenames[] = {\n  \"nil\", \"boolean\", \"userdata\", \"number\",\n  \"string\", \"table\", \"function\", \"userdata\", \"thread\",\n  \"proto\", \"upval\"\n};\n\n\nvoid luaT_init (lua_State *L) {\n  static const char *const luaT_eventname[] = {  /* ORDER TM */\n    \"__index\", \"__newindex\",\n    \"__gc\", \"__mode\", \"__eq\",\n    \"__add\", \"__sub\", \"__mul\", \"__div\", \"__mod\",\n    \"__pow\", \"__unm\", \"__len\", \"__lt\", \"__le\",\n    \"__concat\", \"__call\"\n  };\n  int i;\n  for (i=0; i<TM_N; i++) {\n    G(L)->tmname[i] = luaS_new(L, luaT_eventname[i]);\n    luaS_fix(G(L)->tmname[i]);  /* never collect these names */\n  }\n}\n\n\n/*\n** function to be used with macro \"fasttm\": optimized for absence of\n** tag methods\n*/\nconst TValue *luaT_gettm (Table *events, TMS event, TString *ename) {\n  const TValue *tm = luaH_getstr(events, ename);\n  lua_assert(event <= TM_EQ);\n  if (ttisnil(tm)) {  /* no tag method? */\n    events->flags |= cast_byte(1u<<event);  /* cache this fact */\n    return NULL;\n  }\n  else return tm;\n}\n\n\nconst TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {\n  Table *mt;\n  switch (ttype(o)) {\n    case LUA_TTABLE:\n      mt = hvalue(o)->metatable;\n      break;\n    case LUA_TUSERDATA:\n      mt = uvalue(o)->metatable;\n      break;\n    default:\n      mt = G(L)->mt[ttype(o)];\n  }\n  return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject);\n}\n\n"
  },
  {
    "path": "deps/lua/src/ltm.h",
    "content": "/*\n** $Id: ltm.h,v 2.6.1.1 2007/12/27 13:02:25 roberto Exp $\n** Tag methods\n** See Copyright Notice in lua.h\n*/\n\n#ifndef ltm_h\n#define ltm_h\n\n\n#include \"lobject.h\"\n\n\n/*\n* WARNING: if you change the order of this enumeration,\n* grep \"ORDER TM\"\n*/\ntypedef enum {\n  TM_INDEX,\n  TM_NEWINDEX,\n  TM_GC,\n  TM_MODE,\n  TM_EQ,  /* last tag method with `fast' access */\n  TM_ADD,\n  TM_SUB,\n  TM_MUL,\n  TM_DIV,\n  TM_MOD,\n  TM_POW,\n  TM_UNM,\n  TM_LEN,\n  TM_LT,\n  TM_LE,\n  TM_CONCAT,\n  TM_CALL,\n  TM_N\t\t/* number of elements in the enum */\n} TMS;\n\n\n\n#define gfasttm(g,et,e) ((et) == NULL ? NULL : \\\n  ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e]))\n\n#define fasttm(l,et,e)\tgfasttm(G(l), et, e)\n\nLUAI_DATA const char *const luaT_typenames[];\n\n\nLUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename);\nLUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o,\n                                                       TMS event);\nLUAI_FUNC void luaT_init (lua_State *L);\n\n#endif\n"
  },
  {
    "path": "deps/lua/src/lua.c",
    "content": "/*\n** $Id: lua.c,v 1.160.1.2 2007/12/28 15:32:23 roberto Exp $\n** Lua stand-alone interpreter\n** See Copyright Notice in lua.h\n*/\n\n\n#include <signal.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#define lua_c\n\n#include \"lua.h\"\n\n#include \"lauxlib.h\"\n#include \"lualib.h\"\n\n\n\nstatic lua_State *globalL = NULL;\n\nstatic const char *progname = LUA_PROGNAME;\n\n\n\nstatic void lstop (lua_State *L, lua_Debug *ar) {\n  (void)ar;  /* unused arg. */\n  lua_sethook(L, NULL, 0, 0);\n  luaL_error(L, \"interrupted!\");\n}\n\n\nstatic void laction (int i) {\n  signal(i, SIG_DFL); /* if another SIGINT happens before lstop,\n                              terminate process (default action) */\n  lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);\n}\n\n\nstatic void print_usage (void) {\n  fprintf(stderr,\n  \"usage: %s [options] [script [args]].\\n\"\n  \"Available options are:\\n\"\n  \"  -e stat  execute string \" LUA_QL(\"stat\") \"\\n\"\n  \"  -l name  require library \" LUA_QL(\"name\") \"\\n\"\n  \"  -i       enter interactive mode after executing \" LUA_QL(\"script\") \"\\n\"\n  \"  -v       show version information\\n\"\n  \"  --       stop handling options\\n\"\n  \"  -        execute stdin and stop handling options\\n\"\n  ,\n  progname);\n  fflush(stderr);\n}\n\n\nstatic void l_message (const char *pname, const char *msg) {\n  if (pname) fprintf(stderr, \"%s: \", pname);\n  fprintf(stderr, \"%s\\n\", msg);\n  fflush(stderr);\n}\n\n\nstatic int report (lua_State *L, int status) {\n  if (status && !lua_isnil(L, -1)) {\n    const char *msg = lua_tostring(L, -1);\n    if (msg == NULL) msg = \"(error object is not a string)\";\n    l_message(progname, msg);\n    lua_pop(L, 1);\n  }\n  return status;\n}\n\n\nstatic int traceback (lua_State *L) {\n  if (!lua_isstring(L, 1))  /* 'message' not a string? */\n    return 1;  /* keep it intact */\n  lua_getfield(L, LUA_GLOBALSINDEX, \"debug\");\n  if (!lua_istable(L, -1)) {\n    lua_pop(L, 1);\n    return 1;\n  }\n  lua_getfield(L, -1, \"traceback\");\n  if (!lua_isfunction(L, -1)) {\n    lua_pop(L, 2);\n    return 1;\n  }\n  lua_pushvalue(L, 1);  /* pass error message */\n  lua_pushinteger(L, 2);  /* skip this function and traceback */\n  lua_call(L, 2, 1);  /* call debug.traceback */\n  return 1;\n}\n\n\nstatic int docall (lua_State *L, int narg, int clear) {\n  int status;\n  int base = lua_gettop(L) - narg;  /* function index */\n  lua_pushcfunction(L, traceback);  /* push traceback function */\n  lua_insert(L, base);  /* put it under chunk and args */\n  signal(SIGINT, laction);\n  status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);\n  signal(SIGINT, SIG_DFL);\n  lua_remove(L, base);  /* remove traceback function */\n  /* force a complete garbage collection in case of errors */\n  if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);\n  return status;\n}\n\n\nstatic void print_version (void) {\n  l_message(NULL, LUA_RELEASE \"  \" LUA_COPYRIGHT);\n}\n\n\nstatic int getargs (lua_State *L, char **argv, int n) {\n  int narg;\n  int i;\n  int argc = 0;\n  while (argv[argc]) argc++;  /* count total number of arguments */\n  narg = argc - (n + 1);  /* number of arguments to the script */\n  luaL_checkstack(L, narg + 3, \"too many arguments to script\");\n  for (i=n+1; i < argc; i++)\n    lua_pushstring(L, argv[i]);\n  lua_createtable(L, narg, n + 1);\n  for (i=0; i < argc; i++) {\n    lua_pushstring(L, argv[i]);\n    lua_rawseti(L, -2, i - n);\n  }\n  return narg;\n}\n\n\nstatic int dofile (lua_State *L, const char *name) {\n  int status = luaL_loadfile(L, name) || docall(L, 0, 1);\n  return report(L, status);\n}\n\n\nstatic int dostring (lua_State *L, const char *s, const char *name) {\n  int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);\n  return report(L, status);\n}\n\n\nstatic int dolibrary (lua_State *L, const char *name) {\n  lua_getglobal(L, \"require\");\n  lua_pushstring(L, name);\n  return report(L, docall(L, 1, 1));\n}\n\n\nstatic const char *get_prompt (lua_State *L, int firstline) {\n  const char *p;\n  lua_getfield(L, LUA_GLOBALSINDEX, firstline ? \"_PROMPT\" : \"_PROMPT2\");\n  p = lua_tostring(L, -1);\n  if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);\n  lua_pop(L, 1);  /* remove global */\n  return p;\n}\n\n\nstatic int incomplete (lua_State *L, int status) {\n  if (status == LUA_ERRSYNTAX) {\n    size_t lmsg;\n    const char *msg = lua_tolstring(L, -1, &lmsg);\n    const char *tp = msg + lmsg - (sizeof(LUA_QL(\"<eof>\")) - 1);\n    if (strstr(msg, LUA_QL(\"<eof>\")) == tp) {\n      lua_pop(L, 1);\n      return 1;\n    }\n  }\n  return 0;  /* else... */\n}\n\n\nstatic int pushline (lua_State *L, int firstline) {\n  char buffer[LUA_MAXINPUT];\n  char *b = buffer;\n  size_t l;\n  const char *prmt = get_prompt(L, firstline);\n  if (lua_readline(L, b, prmt) == 0)\n    return 0;  /* no input */\n  l = strlen(b);\n  if (l > 0 && b[l-1] == '\\n')  /* line ends with newline? */\n    b[l-1] = '\\0';  /* remove it */\n  if (firstline && b[0] == '=')  /* first line starts with `=' ? */\n    lua_pushfstring(L, \"return %s\", b+1);  /* change it to `return' */\n  else\n    lua_pushstring(L, b);\n  lua_freeline(L, b);\n  return 1;\n}\n\n\nstatic int loadline (lua_State *L) {\n  int status;\n  lua_settop(L, 0);\n  if (!pushline(L, 1))\n    return -1;  /* no input */\n  for (;;) {  /* repeat until gets a complete line */\n    status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), \"=stdin\");\n    if (!incomplete(L, status)) break;  /* cannot try to add lines? */\n    if (!pushline(L, 0))  /* no more input? */\n      return -1;\n    lua_pushliteral(L, \"\\n\");  /* add a new line... */\n    lua_insert(L, -2);  /* ...between the two lines */\n    lua_concat(L, 3);  /* join them */\n  }\n  lua_saveline(L, 1);\n  lua_remove(L, 1);  /* remove line */\n  return status;\n}\n\n\nstatic void dotty (lua_State *L) {\n  int status;\n  const char *oldprogname = progname;\n  progname = NULL;\n  while ((status = loadline(L)) != -1) {\n    if (status == 0) status = docall(L, 0, 0);\n    report(L, status);\n    if (status == 0 && lua_gettop(L) > 0) {  /* any result to print? */\n      lua_getglobal(L, \"print\");\n      lua_insert(L, 1);\n      if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)\n        l_message(progname, lua_pushfstring(L,\n                               \"error calling \" LUA_QL(\"print\") \" (%s)\",\n                               lua_tostring(L, -1)));\n    }\n  }\n  lua_settop(L, 0);  /* clear stack */\n  fputs(\"\\n\", stdout);\n  fflush(stdout);\n  progname = oldprogname;\n}\n\n\nstatic int handle_script (lua_State *L, char **argv, int n) {\n  int status;\n  const char *fname;\n  int narg = getargs(L, argv, n);  /* collect arguments */\n  lua_setglobal(L, \"arg\");\n  fname = argv[n];\n  if (strcmp(fname, \"-\") == 0 && strcmp(argv[n-1], \"--\") != 0) \n    fname = NULL;  /* stdin */\n  status = luaL_loadfile(L, fname);\n  lua_insert(L, -(narg+1));\n  if (status == 0)\n    status = docall(L, narg, 0);\n  else\n    lua_pop(L, narg);      \n  return report(L, status);\n}\n\n\n/* check that argument has no extra characters at the end */\n#define notail(x)\t{if ((x)[2] != '\\0') return -1;}\n\n\nstatic int collectargs (char **argv, int *pi, int *pv, int *pe) {\n  int i;\n  for (i = 1; argv[i] != NULL; i++) {\n    if (argv[i][0] != '-')  /* not an option? */\n        return i;\n    switch (argv[i][1]) {  /* option */\n      case '-':\n        notail(argv[i]);\n        return (argv[i+1] != NULL ? i+1 : 0);\n      case '\\0':\n        return i;\n      case 'i':\n        notail(argv[i]);\n        *pi = 1;  /* go through */\n      case 'v':\n        notail(argv[i]);\n        *pv = 1;\n        break;\n      case 'e':\n        *pe = 1;  /* go through */\n      case 'l':\n        if (argv[i][2] == '\\0') {\n          i++;\n          if (argv[i] == NULL) return -1;\n        }\n        break;\n      default: return -1;  /* invalid option */\n    }\n  }\n  return 0;\n}\n\n\nstatic int runargs (lua_State *L, char **argv, int n) {\n  int i;\n  for (i = 1; i < n; i++) {\n    if (argv[i] == NULL) continue;\n    lua_assert(argv[i][0] == '-');\n    switch (argv[i][1]) {  /* option */\n      case 'e': {\n        const char *chunk = argv[i] + 2;\n        if (*chunk == '\\0') chunk = argv[++i];\n        lua_assert(chunk != NULL);\n        if (dostring(L, chunk, \"=(command line)\") != 0)\n          return 1;\n        break;\n      }\n      case 'l': {\n        const char *filename = argv[i] + 2;\n        if (*filename == '\\0') filename = argv[++i];\n        lua_assert(filename != NULL);\n        if (dolibrary(L, filename))\n          return 1;  /* stop if file fails */\n        break;\n      }\n      default: break;\n    }\n  }\n  return 0;\n}\n\n\nstatic int handle_luainit (lua_State *L) {\n  const char *init = getenv(LUA_INIT);\n  if (init == NULL) return 0;  /* status OK */\n  else if (init[0] == '@')\n    return dofile(L, init+1);\n  else\n    return dostring(L, init, \"=\" LUA_INIT);\n}\n\n\nstruct Smain {\n  int argc;\n  char **argv;\n  int status;\n};\n\n\nstatic int pmain (lua_State *L) {\n  struct Smain *s = (struct Smain *)lua_touserdata(L, 1);\n  char **argv = s->argv;\n  int script;\n  int has_i = 0, has_v = 0, has_e = 0;\n  globalL = L;\n  if (argv[0] && argv[0][0]) progname = argv[0];\n  lua_gc(L, LUA_GCSTOP, 0);  /* stop collector during initialization */\n  luaL_openlibs(L);  /* open libraries */\n  lua_gc(L, LUA_GCRESTART, 0);\n  s->status = handle_luainit(L);\n  if (s->status != 0) return 0;\n  script = collectargs(argv, &has_i, &has_v, &has_e);\n  if (script < 0) {  /* invalid args? */\n    print_usage();\n    s->status = 1;\n    return 0;\n  }\n  if (has_v) print_version();\n  s->status = runargs(L, argv, (script > 0) ? script : s->argc);\n  if (s->status != 0) return 0;\n  if (script)\n    s->status = handle_script(L, argv, script);\n  if (s->status != 0) return 0;\n  if (has_i)\n    dotty(L);\n  else if (script == 0 && !has_e && !has_v) {\n    if (lua_stdin_is_tty()) {\n      print_version();\n      dotty(L);\n    }\n    else dofile(L, NULL);  /* executes stdin as a file */\n  }\n  return 0;\n}\n\n\nint main (int argc, char **argv) {\n  int status;\n  struct Smain s;\n  lua_State *L = lua_open();  /* create state */\n  if (L == NULL) {\n    l_message(argv[0], \"cannot create state: not enough memory\");\n    return EXIT_FAILURE;\n  }\n  s.argc = argc;\n  s.argv = argv;\n  status = lua_cpcall(L, &pmain, &s);\n  report(L, status);\n  lua_close(L);\n  return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;\n}\n\n"
  },
  {
    "path": "deps/lua/src/lua.h",
    "content": "/*\n** $Id: lua.h,v 1.218.1.7 2012/01/13 20:36:20 roberto Exp $\n** Lua - An Extensible Extension Language\n** Lua.org, PUC-Rio, Brazil (http://www.lua.org)\n** See Copyright Notice at the end of this file\n*/\n\n\n#ifndef lua_h\n#define lua_h\n\n#include <stdarg.h>\n#include <stddef.h>\n\n\n#include \"luaconf.h\"\n\n\n#define LUA_VERSION\t\"Lua 5.1\"\n#define LUA_RELEASE\t\"Lua 5.1.5\"\n#define LUA_VERSION_NUM\t501\n#define LUA_COPYRIGHT\t\"Copyright (C) 1994-2012 Lua.org, PUC-Rio\"\n#define LUA_AUTHORS \t\"R. Ierusalimschy, L. H. de Figueiredo & W. Celes\"\n\n\n/* mark for precompiled code (`<esc>Lua') */\n#define\tLUA_SIGNATURE\t\"\\033Lua\"\n\n/* option for multiple returns in `lua_pcall' and `lua_call' */\n#define LUA_MULTRET\t(-1)\n\n\n/*\n** pseudo-indices\n*/\n#define LUA_REGISTRYINDEX\t(-10000)\n#define LUA_ENVIRONINDEX\t(-10001)\n#define LUA_GLOBALSINDEX\t(-10002)\n#define lua_upvalueindex(i)\t(LUA_GLOBALSINDEX-(i))\n\n\n/* thread status; 0 is OK */\n#define LUA_YIELD\t1\n#define LUA_ERRRUN\t2\n#define LUA_ERRSYNTAX\t3\n#define LUA_ERRMEM\t4\n#define LUA_ERRERR\t5\n\n\ntypedef struct lua_State lua_State;\n\ntypedef int (*lua_CFunction) (lua_State *L);\n\n\n/*\n** functions that read/write blocks when loading/dumping Lua chunks\n*/\ntypedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz);\n\ntypedef int (*lua_Writer) (lua_State *L, const void* p, size_t sz, void* ud);\n\n\n/*\n** prototype for memory-allocation functions\n*/\ntypedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize);\n\n\n/*\n** basic types\n*/\n#define LUA_TNONE\t\t(-1)\n\n#define LUA_TNIL\t\t0\n#define LUA_TBOOLEAN\t\t1\n#define LUA_TLIGHTUSERDATA\t2\n#define LUA_TNUMBER\t\t3\n#define LUA_TSTRING\t\t4\n#define LUA_TTABLE\t\t5\n#define LUA_TFUNCTION\t\t6\n#define LUA_TUSERDATA\t\t7\n#define LUA_TTHREAD\t\t8\n\n\n\n/* minimum Lua stack available to a C function */\n#define LUA_MINSTACK\t20\n\n\n/*\n** generic extra include file\n*/\n#if defined(LUA_USER_H)\n#include LUA_USER_H\n#endif\n\n\n/* type of numbers in Lua */\ntypedef LUA_NUMBER lua_Number;\n\n\n/* type for integer functions */\ntypedef LUA_INTEGER lua_Integer;\n\n\n\n/*\n** state manipulation\n*/\nLUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud);\nLUA_API void       (lua_close) (lua_State *L);\nLUA_API lua_State *(lua_newthread) (lua_State *L);\n\nLUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf);\n\n\n/*\n** basic stack manipulation\n*/\nLUA_API int   (lua_gettop) (lua_State *L);\nLUA_API void  (lua_settop) (lua_State *L, int idx);\nLUA_API void  (lua_pushvalue) (lua_State *L, int idx);\nLUA_API void  (lua_remove) (lua_State *L, int idx);\nLUA_API void  (lua_insert) (lua_State *L, int idx);\nLUA_API void  (lua_replace) (lua_State *L, int idx);\nLUA_API int   (lua_checkstack) (lua_State *L, int sz);\n\nLUA_API void  (lua_xmove) (lua_State *from, lua_State *to, int n);\n\n\n/*\n** access functions (stack -> C)\n*/\n\nLUA_API int             (lua_isnumber) (lua_State *L, int idx);\nLUA_API int             (lua_isstring) (lua_State *L, int idx);\nLUA_API int             (lua_iscfunction) (lua_State *L, int idx);\nLUA_API int             (lua_isuserdata) (lua_State *L, int idx);\nLUA_API int             (lua_type) (lua_State *L, int idx);\nLUA_API const char     *(lua_typename) (lua_State *L, int tp);\n\nLUA_API int            (lua_equal) (lua_State *L, int idx1, int idx2);\nLUA_API int            (lua_rawequal) (lua_State *L, int idx1, int idx2);\nLUA_API int            (lua_lessthan) (lua_State *L, int idx1, int idx2);\n\nLUA_API lua_Number      (lua_tonumber) (lua_State *L, int idx);\nLUA_API lua_Integer     (lua_tointeger) (lua_State *L, int idx);\nLUA_API int             (lua_toboolean) (lua_State *L, int idx);\nLUA_API const char     *(lua_tolstring) (lua_State *L, int idx, size_t *len);\nLUA_API size_t          (lua_objlen) (lua_State *L, int idx);\nLUA_API lua_CFunction   (lua_tocfunction) (lua_State *L, int idx);\nLUA_API void\t       *(lua_touserdata) (lua_State *L, int idx);\nLUA_API lua_State      *(lua_tothread) (lua_State *L, int idx);\nLUA_API const void     *(lua_topointer) (lua_State *L, int idx);\n\n\n/*\n** push functions (C -> stack)\n*/\nLUA_API void  (lua_pushnil) (lua_State *L);\nLUA_API void  (lua_pushnumber) (lua_State *L, lua_Number n);\nLUA_API void  (lua_pushinteger) (lua_State *L, lua_Integer n);\nLUA_API void  (lua_pushlstring) (lua_State *L, const char *s, size_t l);\nLUA_API void  (lua_pushstring) (lua_State *L, const char *s);\nLUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt,\n                                                      va_list argp);\nLUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...);\nLUA_API void  (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);\nLUA_API void  (lua_pushboolean) (lua_State *L, int b);\nLUA_API void  (lua_pushlightuserdata) (lua_State *L, void *p);\nLUA_API int   (lua_pushthread) (lua_State *L);\n\n\n/*\n** get functions (Lua -> stack)\n*/\nLUA_API void  (lua_gettable) (lua_State *L, int idx);\nLUA_API void  (lua_getfield) (lua_State *L, int idx, const char *k);\nLUA_API void  (lua_rawget) (lua_State *L, int idx);\nLUA_API void  (lua_rawgeti) (lua_State *L, int idx, int n);\nLUA_API void  (lua_createtable) (lua_State *L, int narr, int nrec);\nLUA_API void *(lua_newuserdata) (lua_State *L, size_t sz);\nLUA_API int   (lua_getmetatable) (lua_State *L, int objindex);\nLUA_API void  (lua_getfenv) (lua_State *L, int idx);\n\n\n/*\n** set functions (stack -> Lua)\n*/\nLUA_API void  (lua_settable) (lua_State *L, int idx);\nLUA_API void  (lua_setfield) (lua_State *L, int idx, const char *k);\nLUA_API void  (lua_rawset) (lua_State *L, int idx);\nLUA_API void  (lua_rawseti) (lua_State *L, int idx, int n);\nLUA_API int   (lua_setmetatable) (lua_State *L, int objindex);\nLUA_API int   (lua_setfenv) (lua_State *L, int idx);\n\n\n/*\n** `load' and `call' functions (load and run Lua code)\n*/\nLUA_API void  (lua_call) (lua_State *L, int nargs, int nresults);\nLUA_API int   (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);\nLUA_API int   (lua_cpcall) (lua_State *L, lua_CFunction func, void *ud);\nLUA_API int   (lua_load) (lua_State *L, lua_Reader reader, void *dt,\n                                        const char *chunkname);\n\nLUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data);\n\n\n/*\n** coroutine functions\n*/\nLUA_API int  (lua_yield) (lua_State *L, int nresults);\nLUA_API int  (lua_resume) (lua_State *L, int narg);\nLUA_API int  (lua_status) (lua_State *L);\n\n/*\n** garbage-collection function and options\n*/\n\n#define LUA_GCSTOP\t\t0\n#define LUA_GCRESTART\t\t1\n#define LUA_GCCOLLECT\t\t2\n#define LUA_GCCOUNT\t\t3\n#define LUA_GCCOUNTB\t\t4\n#define LUA_GCSTEP\t\t5\n#define LUA_GCSETPAUSE\t\t6\n#define LUA_GCSETSTEPMUL\t7\n\nLUA_API int (lua_gc) (lua_State *L, int what, int data);\n\n\n/*\n** miscellaneous functions\n*/\n\nLUA_API int   (lua_error) (lua_State *L);\n\nLUA_API int   (lua_next) (lua_State *L, int idx);\n\nLUA_API void  (lua_concat) (lua_State *L, int n);\n\nLUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);\nLUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);\n\n\n\n/* \n** ===============================================================\n** some useful macros\n** ===============================================================\n*/\n\n#define lua_pop(L,n)\t\tlua_settop(L, -(n)-1)\n\n#define lua_newtable(L)\t\tlua_createtable(L, 0, 0)\n\n#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n)))\n\n#define lua_pushcfunction(L,f)\tlua_pushcclosure(L, (f), 0)\n\n#define lua_strlen(L,i)\t\tlua_objlen(L, (i))\n\n#define lua_isfunction(L,n)\t(lua_type(L, (n)) == LUA_TFUNCTION)\n#define lua_istable(L,n)\t(lua_type(L, (n)) == LUA_TTABLE)\n#define lua_islightuserdata(L,n)\t(lua_type(L, (n)) == LUA_TLIGHTUSERDATA)\n#define lua_isnil(L,n)\t\t(lua_type(L, (n)) == LUA_TNIL)\n#define lua_isboolean(L,n)\t(lua_type(L, (n)) == LUA_TBOOLEAN)\n#define lua_isthread(L,n)\t(lua_type(L, (n)) == LUA_TTHREAD)\n#define lua_isnone(L,n)\t\t(lua_type(L, (n)) == LUA_TNONE)\n#define lua_isnoneornil(L, n)\t(lua_type(L, (n)) <= 0)\n\n#define lua_pushliteral(L, s)\t\\\n\tlua_pushlstring(L, \"\" s, (sizeof(s)/sizeof(char))-1)\n\n#define lua_setglobal(L,s)\tlua_setfield(L, LUA_GLOBALSINDEX, (s))\n#define lua_getglobal(L,s)\tlua_getfield(L, LUA_GLOBALSINDEX, (s))\n\n#define lua_tostring(L,i)\tlua_tolstring(L, (i), NULL)\n\n\n\n/*\n** compatibility macros and functions\n*/\n\n#define lua_open()\tluaL_newstate()\n\n#define lua_getregistry(L)\tlua_pushvalue(L, LUA_REGISTRYINDEX)\n\n#define lua_getgccount(L)\tlua_gc(L, LUA_GCCOUNT, 0)\n\n#define lua_Chunkreader\t\tlua_Reader\n#define lua_Chunkwriter\t\tlua_Writer\n\n\n/* hack */\nLUA_API void lua_setlevel\t(lua_State *from, lua_State *to);\n\n\n/*\n** {======================================================================\n** Debug API\n** =======================================================================\n*/\n\n\n/*\n** Event codes\n*/\n#define LUA_HOOKCALL\t0\n#define LUA_HOOKRET\t1\n#define LUA_HOOKLINE\t2\n#define LUA_HOOKCOUNT\t3\n#define LUA_HOOKTAILRET 4\n\n\n/*\n** Event masks\n*/\n#define LUA_MASKCALL\t(1 << LUA_HOOKCALL)\n#define LUA_MASKRET\t(1 << LUA_HOOKRET)\n#define LUA_MASKLINE\t(1 << LUA_HOOKLINE)\n#define LUA_MASKCOUNT\t(1 << LUA_HOOKCOUNT)\n\ntypedef struct lua_Debug lua_Debug;  /* activation record */\n\n\n/* Functions to be called by the debuger in specific events */\ntypedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);\n\n\nLUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar);\nLUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);\nLUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n);\nLUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);\nLUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n);\nLUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n);\n\nLUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count);\nLUA_API lua_Hook lua_gethook (lua_State *L);\nLUA_API int lua_gethookmask (lua_State *L);\nLUA_API int lua_gethookcount (lua_State *L);\n\n\nstruct lua_Debug {\n  int event;\n  const char *name;\t/* (n) */\n  const char *namewhat;\t/* (n) `global', `local', `field', `method' */\n  const char *what;\t/* (S) `Lua', `C', `main', `tail' */\n  const char *source;\t/* (S) */\n  int currentline;\t/* (l) */\n  int nups;\t\t/* (u) number of upvalues */\n  int linedefined;\t/* (S) */\n  int lastlinedefined;\t/* (S) */\n  char short_src[LUA_IDSIZE]; /* (S) */\n  /* private part */\n  int i_ci;  /* active function */\n};\n\n/* }====================================================================== */\n\n\n/******************************************************************************\n* Copyright (C) 1994-2012 Lua.org, PUC-Rio.  All rights reserved.\n*\n* Permission is hereby granted, free of charge, to any person obtaining\n* a copy of this software and associated documentation files (the\n* \"Software\"), to deal in the Software without restriction, including\n* without limitation the rights to use, copy, modify, merge, publish,\n* distribute, sublicense, and/or sell copies of the Software, and to\n* permit persons to whom the Software is furnished to do so, subject to\n* the following conditions:\n*\n* The above copyright notice and this permission notice shall be\n* included in all copies or substantial portions of the Software.\n*\n* THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\n* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\n* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\n* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\n* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n******************************************************************************/\n\n\n#endif\n"
  },
  {
    "path": "deps/lua/src/lua_bit.c",
    "content": "/*\n** Lua BitOp -- a bit operations library for Lua 5.1/5.2.\n** http://bitop.luajit.org/\n**\n** Copyright (C) 2008-2012 Mike Pall. All rights reserved.\n**\n** Permission is hereby granted, free of charge, to any person obtaining\n** a copy of this software and associated documentation files (the\n** \"Software\"), to deal in the Software without restriction, including\n** without limitation the rights to use, copy, modify, merge, publish,\n** distribute, sublicense, and/or sell copies of the Software, and to\n** permit persons to whom the Software is furnished to do so, subject to\n** the following conditions:\n**\n** The above copyright notice and this permission notice shall be\n** included in all copies or substantial portions of the Software.\n**\n** THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\n** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\n** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\n** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\n** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n**\n** [ MIT license: http://www.opensource.org/licenses/mit-license.php ]\n*/\n\n#define LUA_BITOP_VERSION\t\"1.0.2\"\n\n#define LUA_LIB\n#include \"lua.h\"\n#include \"lauxlib.h\"\n\n#ifdef _MSC_VER\n/* MSVC is stuck in the last century and doesn't have C99's stdint.h. */\ntypedef __int32 int32_t;\ntypedef unsigned __int32 uint32_t;\ntypedef unsigned __int64 uint64_t;\n#else\n#include <stdint.h>\n#endif\n\ntypedef int32_t SBits;\ntypedef uint32_t UBits;\n\ntypedef union {\n  lua_Number n;\n#ifdef LUA_NUMBER_DOUBLE\n  uint64_t b;\n#else\n  UBits b;\n#endif\n} BitNum;\n\n/* Convert argument to bit type. */\nstatic UBits barg(lua_State *L, int idx)\n{\n  BitNum bn;\n  UBits b;\n#if LUA_VERSION_NUM < 502\n  bn.n = lua_tonumber(L, idx);\n#else\n  bn.n = luaL_checknumber(L, idx);\n#endif\n#if defined(LUA_NUMBER_DOUBLE)\n  bn.n += 6755399441055744.0;  /* 2^52+2^51 */\n#ifdef SWAPPED_DOUBLE\n  b = (UBits)(bn.b >> 32);\n#else\n  b = (UBits)bn.b;\n#endif\n#elif defined(LUA_NUMBER_INT) || defined(LUA_NUMBER_LONG) || \\\n      defined(LUA_NUMBER_LONGLONG) || defined(LUA_NUMBER_LONG_LONG) || \\\n      defined(LUA_NUMBER_LLONG)\n  if (sizeof(UBits) == sizeof(lua_Number))\n    b = bn.b;\n  else\n    b = (UBits)(SBits)bn.n;\n#elif defined(LUA_NUMBER_FLOAT)\n#error \"A 'float' lua_Number type is incompatible with this library\"\n#else\n#error \"Unknown number type, check LUA_NUMBER_* in luaconf.h\"\n#endif\n#if LUA_VERSION_NUM < 502\n  if (b == 0 && !lua_isnumber(L, idx)) {\n    luaL_typerror(L, idx, \"number\");\n  }\n#endif\n  return b;\n}\n\n/* Return bit type. */\n#define BRET(b)  lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1;\n\nstatic int bit_tobit(lua_State *L) { BRET(barg(L, 1)) }\nstatic int bit_bnot(lua_State *L) { BRET(~barg(L, 1)) }\n\n#define BIT_OP(func, opr) \\\n  static int func(lua_State *L) { int i; UBits b = barg(L, 1); \\\n    for (i = lua_gettop(L); i > 1; i--) b opr barg(L, i); BRET(b) }\nBIT_OP(bit_band, &=)\nBIT_OP(bit_bor, |=)\nBIT_OP(bit_bxor, ^=)\n\n#define bshl(b, n)  (b << n)\n#define bshr(b, n)  (b >> n)\n#define bsar(b, n)  ((SBits)b >> n)\n#define brol(b, n)  ((b << n) | (b >> (32-n)))\n#define bror(b, n)  ((b << (32-n)) | (b >> n))\n#define BIT_SH(func, fn) \\\n  static int func(lua_State *L) { \\\n    UBits b = barg(L, 1); UBits n = barg(L, 2) & 31; BRET(fn(b, n)) }\nBIT_SH(bit_lshift, bshl)\nBIT_SH(bit_rshift, bshr)\nBIT_SH(bit_arshift, bsar)\nBIT_SH(bit_rol, brol)\nBIT_SH(bit_ror, bror)\n\nstatic int bit_bswap(lua_State *L)\n{\n  UBits b = barg(L, 1);\n  b = (b >> 24) | ((b >> 8) & 0xff00) | ((b & 0xff00) << 8) | (b << 24);\n  BRET(b)\n}\n\nstatic int bit_tohex(lua_State *L)\n{\n  UBits b = barg(L, 1);\n  SBits n = lua_isnone(L, 2) ? 8 : (SBits)barg(L, 2);\n  const char *hexdigits = \"0123456789abcdef\";\n  char buf[8];\n  int i;\n  if (n < 0) { n = -n; hexdigits = \"0123456789ABCDEF\"; }\n  if (n > 8) n = 8;\n  for (i = (int)n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; }\n  lua_pushlstring(L, buf, (size_t)n);\n  return 1;\n}\n\nstatic const struct luaL_Reg bit_funcs[] = {\n  { \"tobit\",\tbit_tobit },\n  { \"bnot\",\tbit_bnot },\n  { \"band\",\tbit_band },\n  { \"bor\",\tbit_bor },\n  { \"bxor\",\tbit_bxor },\n  { \"lshift\",\tbit_lshift },\n  { \"rshift\",\tbit_rshift },\n  { \"arshift\",\tbit_arshift },\n  { \"rol\",\tbit_rol },\n  { \"ror\",\tbit_ror },\n  { \"bswap\",\tbit_bswap },\n  { \"tohex\",\tbit_tohex },\n  { NULL, NULL }\n};\n\n/* Signed right-shifts are implementation-defined per C89/C99.\n** But the de facto standard are arithmetic right-shifts on two's\n** complement CPUs. This behaviour is required here, so test for it.\n*/\n#define BAD_SAR\t\t(bsar(-8, 2) != (SBits)-2)\n\nLUALIB_API int luaopen_bit(lua_State *L)\n{\n  UBits b;\n  lua_pushnumber(L, (lua_Number)1437217655L);\n  b = barg(L, -1);\n  if (b != (UBits)1437217655L || BAD_SAR) {  /* Perform a simple self-test. */\n    const char *msg = \"compiled with incompatible luaconf.h\";\n#ifdef LUA_NUMBER_DOUBLE\n#ifdef _WIN32\n    if (b == (UBits)1610612736L)\n      msg = \"use D3DCREATE_FPU_PRESERVE with DirectX\";\n#endif\n    if (b == (UBits)1127743488L)\n      msg = \"not compiled with SWAPPED_DOUBLE\";\n#endif\n    if (BAD_SAR)\n      msg = \"arithmetic right-shift broken\";\n    luaL_error(L, \"bit library self-test failed (%s)\", msg);\n  }\n#if LUA_VERSION_NUM < 502\n  luaL_register(L, \"bit\", bit_funcs);\n#else\n  luaL_newlib(L, bit_funcs);\n#endif\n  return 1;\n}\n\n"
  },
  {
    "path": "deps/lua/src/lua_cjson.c",
    "content": "/* Lua CJSON - JSON support for Lua\n *\n * Copyright (c) 2010-2012  Mark Pulford <mark@kyne.com.au>\n *\n * Permission is hereby granted, free of charge, to any person obtaining\n * a copy of this software and associated documentation files (the\n * \"Software\"), to deal in the Software without restriction, including\n * without limitation the rights to use, copy, modify, merge, publish,\n * distribute, sublicense, and/or sell copies of the Software, and to\n * permit persons to whom the Software is furnished to do so, subject to\n * the following conditions:\n *\n * The above copyright notice and this permission notice shall be\n * included in all copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\n * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\n * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\n * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\n * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n */\n\n/* Caveats:\n * - JSON \"null\" values are represented as lightuserdata since Lua\n *   tables cannot contain \"nil\". Compare with cjson.null.\n * - Invalid UTF-8 characters are not detected and will be passed\n *   untouched. If required, UTF-8 error checking should be done\n *   outside this library.\n * - Javascript comments are not part of the JSON spec, and are not\n *   currently supported.\n *\n * Note: Decoding is slower than encoding. Lua spends significant\n *       time (30%) managing tables when parsing JSON since it is\n *       difficult to know object/array sizes ahead of time.\n */\n\n#include <assert.h>\n#include <string.h>\n#include <math.h>\n#include <limits.h>\n#include \"lua.h\"\n#include \"lauxlib.h\"\n\n#include \"strbuf.h\"\n#include \"fpconv.h\"\n\n#include \"../../../src/solarisfixes.h\"\n\n#ifndef CJSON_MODNAME\n#define CJSON_MODNAME   \"cjson\"\n#endif\n\n#ifndef CJSON_VERSION\n#define CJSON_VERSION   \"2.1.0\"\n#endif\n\n/* Workaround for Solaris platforms missing isinf() */\n#if !defined(isinf) && (defined(USE_INTERNAL_ISINF) || defined(MISSING_ISINF))\n#define isinf(x) (!isnan(x) && isnan((x) - (x)))\n#endif\n\n#define DEFAULT_SPARSE_CONVERT 0\n#define DEFAULT_SPARSE_RATIO 2\n#define DEFAULT_SPARSE_SAFE 10\n#define DEFAULT_ENCODE_MAX_DEPTH 1000\n#define DEFAULT_DECODE_MAX_DEPTH 1000\n#define DEFAULT_ENCODE_INVALID_NUMBERS 0\n#define DEFAULT_DECODE_INVALID_NUMBERS 1\n#define DEFAULT_ENCODE_KEEP_BUFFER 1\n#define DEFAULT_ENCODE_NUMBER_PRECISION 14\n\n#ifdef DISABLE_INVALID_NUMBERS\n#undef DEFAULT_DECODE_INVALID_NUMBERS\n#define DEFAULT_DECODE_INVALID_NUMBERS 0\n#endif\n\ntypedef enum {\n    T_OBJ_BEGIN,\n    T_OBJ_END,\n    T_ARR_BEGIN,\n    T_ARR_END,\n    T_STRING,\n    T_NUMBER,\n    T_BOOLEAN,\n    T_NULL,\n    T_COLON,\n    T_COMMA,\n    T_END,\n    T_WHITESPACE,\n    T_ERROR,\n    T_UNKNOWN\n} json_token_type_t;\n\nstatic const char *json_token_type_name[] = {\n    \"T_OBJ_BEGIN\",\n    \"T_OBJ_END\",\n    \"T_ARR_BEGIN\",\n    \"T_ARR_END\",\n    \"T_STRING\",\n    \"T_NUMBER\",\n    \"T_BOOLEAN\",\n    \"T_NULL\",\n    \"T_COLON\",\n    \"T_COMMA\",\n    \"T_END\",\n    \"T_WHITESPACE\",\n    \"T_ERROR\",\n    \"T_UNKNOWN\",\n    NULL\n};\n\ntypedef struct {\n    json_token_type_t ch2token[256];\n    char escape2char[256];  /* Decoding */\n\n    /* encode_buf is only allocated and used when\n     * encode_keep_buffer is set */\n    strbuf_t encode_buf;\n\n    int encode_sparse_convert;\n    int encode_sparse_ratio;\n    int encode_sparse_safe;\n    int encode_max_depth;\n    int encode_invalid_numbers;     /* 2 => Encode as \"null\" */\n    int encode_number_precision;\n    int encode_keep_buffer;\n\n    int decode_invalid_numbers;\n    int decode_max_depth;\n} json_config_t;\n\ntypedef struct {\n    const char *data;\n    const char *ptr;\n    strbuf_t *tmp;    /* Temporary storage for strings */\n    json_config_t *cfg;\n    int current_depth;\n} json_parse_t;\n\ntypedef struct {\n    json_token_type_t type;\n    int index;\n    union {\n        const char *string;\n        double number;\n        int boolean;\n    } value;\n    int string_len;\n} json_token_t;\n\nstatic const char *char2escape[256] = {\n    \"\\\\u0000\", \"\\\\u0001\", \"\\\\u0002\", \"\\\\u0003\",\n    \"\\\\u0004\", \"\\\\u0005\", \"\\\\u0006\", \"\\\\u0007\",\n    \"\\\\b\", \"\\\\t\", \"\\\\n\", \"\\\\u000b\",\n    \"\\\\f\", \"\\\\r\", \"\\\\u000e\", \"\\\\u000f\",\n    \"\\\\u0010\", \"\\\\u0011\", \"\\\\u0012\", \"\\\\u0013\",\n    \"\\\\u0014\", \"\\\\u0015\", \"\\\\u0016\", \"\\\\u0017\",\n    \"\\\\u0018\", \"\\\\u0019\", \"\\\\u001a\", \"\\\\u001b\",\n    \"\\\\u001c\", \"\\\\u001d\", \"\\\\u001e\", \"\\\\u001f\",\n    NULL, NULL, \"\\\\\\\"\", NULL, NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, \"\\\\/\",\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL, \"\\\\\\\\\", NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, \"\\\\u007f\",\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n};\n\n/* ===== CONFIGURATION ===== */\n\nstatic json_config_t *json_fetch_config(lua_State *l)\n{\n    json_config_t *cfg;\n\n    cfg = lua_touserdata(l, lua_upvalueindex(1));\n    if (!cfg)\n        luaL_error(l, \"BUG: Unable to fetch CJSON configuration\");\n\n    return cfg;\n}\n\n/* Ensure the correct number of arguments have been provided.\n * Pad with nil to allow other functions to simply check arg[i]\n * to find whether an argument was provided */\nstatic json_config_t *json_arg_init(lua_State *l, int args)\n{\n    luaL_argcheck(l, lua_gettop(l) <= args, args + 1,\n                  \"found too many arguments\");\n\n    while (lua_gettop(l) < args)\n        lua_pushnil(l);\n\n    return json_fetch_config(l);\n}\n\n/* Process integer options for configuration functions */\nstatic int json_integer_option(lua_State *l, int optindex, int *setting,\n                               int min, int max)\n{\n    char errmsg[64];\n    int value;\n\n    if (!lua_isnil(l, optindex)) {\n        value = luaL_checkinteger(l, optindex);\n        snprintf(errmsg, sizeof(errmsg), \"expected integer between %d and %d\", min, max);\n        luaL_argcheck(l, min <= value && value <= max, 1, errmsg);\n        *setting = value;\n    }\n\n    lua_pushinteger(l, *setting);\n\n    return 1;\n}\n\n/* Process enumerated arguments for a configuration function */\nstatic int json_enum_option(lua_State *l, int optindex, int *setting,\n                            const char **options, int bool_true)\n{\n    static const char *bool_options[] = { \"off\", \"on\", NULL };\n\n    if (!options) {\n        options = bool_options;\n        bool_true = 1;\n    }\n\n    if (!lua_isnil(l, optindex)) {\n        if (bool_true && lua_isboolean(l, optindex))\n            *setting = lua_toboolean(l, optindex) * bool_true;\n        else\n            *setting = luaL_checkoption(l, optindex, NULL, options);\n    }\n\n    if (bool_true && (*setting == 0 || *setting == bool_true))\n        lua_pushboolean(l, *setting);\n    else\n        lua_pushstring(l, options[*setting]);\n\n    return 1;\n}\n\n/* Configures handling of extremely sparse arrays:\n * convert: Convert extremely sparse arrays into objects? Otherwise error.\n * ratio: 0: always allow sparse; 1: never allow sparse; >1: use ratio\n * safe: Always use an array when the max index <= safe */\nstatic int json_cfg_encode_sparse_array(lua_State *l)\n{\n    json_config_t *cfg = json_arg_init(l, 3);\n\n    json_enum_option(l, 1, &cfg->encode_sparse_convert, NULL, 1);\n    json_integer_option(l, 2, &cfg->encode_sparse_ratio, 0, INT_MAX);\n    json_integer_option(l, 3, &cfg->encode_sparse_safe, 0, INT_MAX);\n\n    return 3;\n}\n\n/* Configures the maximum number of nested arrays/objects allowed when\n * encoding */\nstatic int json_cfg_encode_max_depth(lua_State *l)\n{\n    json_config_t *cfg = json_arg_init(l, 1);\n\n    return json_integer_option(l, 1, &cfg->encode_max_depth, 1, INT_MAX);\n}\n\n/* Configures the maximum number of nested arrays/objects allowed when\n * encoding */\nstatic int json_cfg_decode_max_depth(lua_State *l)\n{\n    json_config_t *cfg = json_arg_init(l, 1);\n\n    return json_integer_option(l, 1, &cfg->decode_max_depth, 1, INT_MAX);\n}\n\n/* Configures number precision when converting doubles to text */\nstatic int json_cfg_encode_number_precision(lua_State *l)\n{\n    json_config_t *cfg = json_arg_init(l, 1);\n\n    return json_integer_option(l, 1, &cfg->encode_number_precision, 1, 14);\n}\n\n/* Configures JSON encoding buffer persistence */\nstatic int json_cfg_encode_keep_buffer(lua_State *l)\n{\n    json_config_t *cfg = json_arg_init(l, 1);\n    int old_value;\n\n    old_value = cfg->encode_keep_buffer;\n\n    json_enum_option(l, 1, &cfg->encode_keep_buffer, NULL, 1);\n\n    /* Init / free the buffer if the setting has changed */\n    if (old_value ^ cfg->encode_keep_buffer) {\n        if (cfg->encode_keep_buffer)\n            strbuf_init(&cfg->encode_buf, 0);\n        else\n            strbuf_free(&cfg->encode_buf);\n    }\n\n    return 1;\n}\n\n#if defined(DISABLE_INVALID_NUMBERS) && !defined(USE_INTERNAL_FPCONV)\nvoid json_verify_invalid_number_setting(lua_State *l, int *setting)\n{\n    if (*setting == 1) {\n        *setting = 0;\n        luaL_error(l, \"Infinity, NaN, and/or hexadecimal numbers are not supported.\");\n    }\n}\n#else\n#define json_verify_invalid_number_setting(l, s)    do { } while(0)\n#endif\n\nstatic int json_cfg_encode_invalid_numbers(lua_State *l)\n{\n    static const char *options[] = { \"off\", \"on\", \"null\", NULL };\n    json_config_t *cfg = json_arg_init(l, 1);\n\n    json_enum_option(l, 1, &cfg->encode_invalid_numbers, options, 1);\n\n    json_verify_invalid_number_setting(l, &cfg->encode_invalid_numbers);\n\n    return 1;\n}\n\nstatic int json_cfg_decode_invalid_numbers(lua_State *l)\n{\n    json_config_t *cfg = json_arg_init(l, 1);\n\n    json_enum_option(l, 1, &cfg->decode_invalid_numbers, NULL, 1);\n\n    json_verify_invalid_number_setting(l, &cfg->encode_invalid_numbers);\n\n    return 1;\n}\n\nstatic int json_destroy_config(lua_State *l)\n{\n    json_config_t *cfg;\n\n    cfg = lua_touserdata(l, 1);\n    if (cfg)\n        strbuf_free(&cfg->encode_buf);\n    cfg = NULL;\n\n    return 0;\n}\n\nstatic void json_create_config(lua_State *l)\n{\n    json_config_t *cfg;\n    int i;\n\n    cfg = lua_newuserdata(l, sizeof(*cfg));\n\n    /* Create GC method to clean up strbuf */\n    lua_newtable(l);\n    lua_pushcfunction(l, json_destroy_config);\n    lua_setfield(l, -2, \"__gc\");\n    lua_setmetatable(l, -2);\n\n    cfg->encode_sparse_convert = DEFAULT_SPARSE_CONVERT;\n    cfg->encode_sparse_ratio = DEFAULT_SPARSE_RATIO;\n    cfg->encode_sparse_safe = DEFAULT_SPARSE_SAFE;\n    cfg->encode_max_depth = DEFAULT_ENCODE_MAX_DEPTH;\n    cfg->decode_max_depth = DEFAULT_DECODE_MAX_DEPTH;\n    cfg->encode_invalid_numbers = DEFAULT_ENCODE_INVALID_NUMBERS;\n    cfg->decode_invalid_numbers = DEFAULT_DECODE_INVALID_NUMBERS;\n    cfg->encode_keep_buffer = DEFAULT_ENCODE_KEEP_BUFFER;\n    cfg->encode_number_precision = DEFAULT_ENCODE_NUMBER_PRECISION;\n\n#if DEFAULT_ENCODE_KEEP_BUFFER > 0\n    strbuf_init(&cfg->encode_buf, 0);\n#endif\n\n    /* Decoding init */\n\n    /* Tag all characters as an error */\n    for (i = 0; i < 256; i++)\n        cfg->ch2token[i] = T_ERROR;\n\n    /* Set tokens that require no further processing */\n    cfg->ch2token['{'] = T_OBJ_BEGIN;\n    cfg->ch2token['}'] = T_OBJ_END;\n    cfg->ch2token['['] = T_ARR_BEGIN;\n    cfg->ch2token[']'] = T_ARR_END;\n    cfg->ch2token[','] = T_COMMA;\n    cfg->ch2token[':'] = T_COLON;\n    cfg->ch2token['\\0'] = T_END;\n    cfg->ch2token[' '] = T_WHITESPACE;\n    cfg->ch2token['\\t'] = T_WHITESPACE;\n    cfg->ch2token['\\n'] = T_WHITESPACE;\n    cfg->ch2token['\\r'] = T_WHITESPACE;\n\n    /* Update characters that require further processing */\n    cfg->ch2token['f'] = T_UNKNOWN;     /* false? */\n    cfg->ch2token['i'] = T_UNKNOWN;     /* inf, ininity? */\n    cfg->ch2token['I'] = T_UNKNOWN;\n    cfg->ch2token['n'] = T_UNKNOWN;     /* null, nan? */\n    cfg->ch2token['N'] = T_UNKNOWN;\n    cfg->ch2token['t'] = T_UNKNOWN;     /* true? */\n    cfg->ch2token['\"'] = T_UNKNOWN;     /* string? */\n    cfg->ch2token['+'] = T_UNKNOWN;     /* number? */\n    cfg->ch2token['-'] = T_UNKNOWN;\n    for (i = 0; i < 10; i++)\n        cfg->ch2token['0' + i] = T_UNKNOWN;\n\n    /* Lookup table for parsing escape characters */\n    for (i = 0; i < 256; i++)\n        cfg->escape2char[i] = 0;          /* String error */\n    cfg->escape2char['\"'] = '\"';\n    cfg->escape2char['\\\\'] = '\\\\';\n    cfg->escape2char['/'] = '/';\n    cfg->escape2char['b'] = '\\b';\n    cfg->escape2char['t'] = '\\t';\n    cfg->escape2char['n'] = '\\n';\n    cfg->escape2char['f'] = '\\f';\n    cfg->escape2char['r'] = '\\r';\n    cfg->escape2char['u'] = 'u';          /* Unicode parsing required */\n}\n\n/* ===== ENCODING ===== */\n\nstatic void json_encode_exception(lua_State *l, json_config_t *cfg, strbuf_t *json, int lindex,\n                                  const char *reason)\n{\n    if (!cfg->encode_keep_buffer)\n        strbuf_free(json);\n    luaL_error(l, \"Cannot serialise %s: %s\",\n                  lua_typename(l, lua_type(l, lindex)), reason);\n}\n\n/* json_append_string args:\n * - lua_State\n * - JSON strbuf\n * - String (Lua stack index)\n *\n * Returns nothing. Doesn't remove string from Lua stack */\nstatic void json_append_string(lua_State *l, strbuf_t *json, int lindex)\n{\n    const char *escstr;\n    int i;\n    const char *str;\n    size_t len;\n\n    str = lua_tolstring(l, lindex, &len);\n\n    /* Worst case is len * 6 (all unicode escapes).\n     * This buffer is reused constantly for small strings\n     * If there are any excess pages, they won't be hit anyway.\n     * This gains ~5% speedup. */\n    strbuf_ensure_empty_length(json, len * 6 + 2);\n\n    strbuf_append_char_unsafe(json, '\\\"');\n    for (i = 0; i < len; i++) {\n        escstr = char2escape[(unsigned char)str[i]];\n        if (escstr)\n            strbuf_append_string(json, escstr);\n        else\n            strbuf_append_char_unsafe(json, str[i]);\n    }\n    strbuf_append_char_unsafe(json, '\\\"');\n}\n\n/* Find the size of the array on the top of the Lua stack\n * -1   object (not a pure array)\n * >=0  elements in array\n */\nstatic int lua_array_length(lua_State *l, json_config_t *cfg, strbuf_t *json)\n{\n    double k;\n    int max;\n    int items;\n\n    max = 0;\n    items = 0;\n\n    lua_pushnil(l);\n    /* table, startkey */\n    while (lua_next(l, -2) != 0) {\n        /* table, key, value */\n        if (lua_type(l, -2) == LUA_TNUMBER &&\n            (k = lua_tonumber(l, -2))) {\n            /* Integer >= 1 ? */\n            if (floor(k) == k && k >= 1) {\n                if (k > max)\n                    max = k;\n                items++;\n                lua_pop(l, 1);\n                continue;\n            }\n        }\n\n        /* Must not be an array (non integer key) */\n        lua_pop(l, 2);\n        return -1;\n    }\n\n    /* Encode excessively sparse arrays as objects (if enabled) */\n    if (cfg->encode_sparse_ratio > 0 &&\n        max > items * cfg->encode_sparse_ratio &&\n        max > cfg->encode_sparse_safe) {\n        if (!cfg->encode_sparse_convert)\n            json_encode_exception(l, cfg, json, -1, \"excessively sparse array\");\n\n        return -1;\n    }\n\n    return max;\n}\n\nstatic void json_check_encode_depth(lua_State *l, json_config_t *cfg,\n                                    int current_depth, strbuf_t *json)\n{\n    /* Ensure there are enough slots free to traverse a table (key,\n     * value) and push a string for a potential error message.\n     *\n     * Unlike \"decode\", the key and value are still on the stack when\n     * lua_checkstack() is called.  Hence an extra slot for luaL_error()\n     * below is required just in case the next check to lua_checkstack()\n     * fails.\n     *\n     * While this won't cause a crash due to the EXTRA_STACK reserve\n     * slots, it would still be an improper use of the API. */\n    if (current_depth <= cfg->encode_max_depth && lua_checkstack(l, 3))\n        return;\n\n    if (!cfg->encode_keep_buffer)\n        strbuf_free(json);\n\n    luaL_error(l, \"Cannot serialise, excessive nesting (%d)\",\n               current_depth);\n}\n\nstatic void json_append_data(lua_State *l, json_config_t *cfg,\n                             int current_depth, strbuf_t *json);\n\n/* json_append_array args:\n * - lua_State\n * - JSON strbuf\n * - Size of passwd Lua array (top of stack) */\nstatic void json_append_array(lua_State *l, json_config_t *cfg, int current_depth,\n                              strbuf_t *json, int array_length)\n{\n    int comma, i;\n\n    strbuf_append_char(json, '[');\n\n    comma = 0;\n    for (i = 1; i <= array_length; i++) {\n        if (comma)\n            strbuf_append_char(json, ',');\n        else\n            comma = 1;\n\n        lua_rawgeti(l, -1, i);\n        json_append_data(l, cfg, current_depth, json);\n        lua_pop(l, 1);\n    }\n\n    strbuf_append_char(json, ']');\n}\n\nstatic void json_append_number(lua_State *l, json_config_t *cfg,\n                               strbuf_t *json, int lindex)\n{\n    double num = lua_tonumber(l, lindex);\n    int len;\n\n    if (cfg->encode_invalid_numbers == 0) {\n        /* Prevent encoding invalid numbers */\n        if (isinf(num) || isnan(num))\n            json_encode_exception(l, cfg, json, lindex, \"must not be NaN or Inf\");\n    } else if (cfg->encode_invalid_numbers == 1) {\n        /* Encode invalid numbers, but handle \"nan\" separately\n         * since some platforms may encode as \"-nan\". */\n        if (isnan(num)) {\n            strbuf_append_mem(json, \"nan\", 3);\n            return;\n        }\n    } else {\n        /* Encode invalid numbers as \"null\" */\n        if (isinf(num) || isnan(num)) {\n            strbuf_append_mem(json, \"null\", 4);\n            return;\n        }\n    }\n\n    strbuf_ensure_empty_length(json, FPCONV_G_FMT_BUFSIZE);\n    len = fpconv_g_fmt(strbuf_empty_ptr(json), num, cfg->encode_number_precision);\n    strbuf_extend_length(json, len);\n}\n\nstatic void json_append_object(lua_State *l, json_config_t *cfg,\n                               int current_depth, strbuf_t *json)\n{\n    int comma, keytype;\n\n    /* Object */\n    strbuf_append_char(json, '{');\n\n    lua_pushnil(l);\n    /* table, startkey */\n    comma = 0;\n    while (lua_next(l, -2) != 0) {\n        if (comma)\n            strbuf_append_char(json, ',');\n        else\n            comma = 1;\n\n        /* table, key, value */\n        keytype = lua_type(l, -2);\n        if (keytype == LUA_TNUMBER) {\n            strbuf_append_char(json, '\"');\n            json_append_number(l, cfg, json, -2);\n            strbuf_append_mem(json, \"\\\":\", 2);\n        } else if (keytype == LUA_TSTRING) {\n            json_append_string(l, json, -2);\n            strbuf_append_char(json, ':');\n        } else {\n            json_encode_exception(l, cfg, json, -2,\n                                  \"table key must be a number or string\");\n            /* never returns */\n        }\n\n        /* table, key, value */\n        json_append_data(l, cfg, current_depth, json);\n        lua_pop(l, 1);\n        /* table, key */\n    }\n\n    strbuf_append_char(json, '}');\n}\n\n/* Serialise Lua data into JSON string. */\nstatic void json_append_data(lua_State *l, json_config_t *cfg,\n                             int current_depth, strbuf_t *json)\n{\n    int len;\n\n    switch (lua_type(l, -1)) {\n    case LUA_TSTRING:\n        json_append_string(l, json, -1);\n        break;\n    case LUA_TNUMBER:\n        json_append_number(l, cfg, json, -1);\n        break;\n    case LUA_TBOOLEAN:\n        if (lua_toboolean(l, -1))\n            strbuf_append_mem(json, \"true\", 4);\n        else\n            strbuf_append_mem(json, \"false\", 5);\n        break;\n    case LUA_TTABLE:\n        current_depth++;\n        json_check_encode_depth(l, cfg, current_depth, json);\n        len = lua_array_length(l, cfg, json);\n        if (len > 0)\n            json_append_array(l, cfg, current_depth, json, len);\n        else\n            json_append_object(l, cfg, current_depth, json);\n        break;\n    case LUA_TNIL:\n        strbuf_append_mem(json, \"null\", 4);\n        break;\n    case LUA_TLIGHTUSERDATA:\n        if (lua_touserdata(l, -1) == NULL) {\n            strbuf_append_mem(json, \"null\", 4);\n            break;\n        }\n    default:\n        /* Remaining types (LUA_TFUNCTION, LUA_TUSERDATA, LUA_TTHREAD,\n         * and LUA_TLIGHTUSERDATA) cannot be serialised */\n        json_encode_exception(l, cfg, json, -1, \"type not supported\");\n        /* never returns */\n    }\n}\n\nstatic int json_encode(lua_State *l)\n{\n    json_config_t *cfg = json_fetch_config(l);\n    strbuf_t local_encode_buf;\n    strbuf_t *encode_buf;\n    char *json;\n    int len;\n\n    luaL_argcheck(l, lua_gettop(l) == 1, 1, \"expected 1 argument\");\n\n    if (!cfg->encode_keep_buffer) {\n        /* Use private buffer */\n        encode_buf = &local_encode_buf;\n        strbuf_init(encode_buf, 0);\n    } else {\n        /* Reuse existing buffer */\n        encode_buf = &cfg->encode_buf;\n        strbuf_reset(encode_buf);\n    }\n\n    json_append_data(l, cfg, 0, encode_buf);\n    json = strbuf_string(encode_buf, &len);\n\n    lua_pushlstring(l, json, len);\n\n    if (!cfg->encode_keep_buffer)\n        strbuf_free(encode_buf);\n\n    return 1;\n}\n\n/* ===== DECODING ===== */\n\nstatic void json_process_value(lua_State *l, json_parse_t *json,\n                               json_token_t *token);\n\nstatic int hexdigit2int(char hex)\n{\n    if ('0' <= hex  && hex <= '9')\n        return hex - '0';\n\n    /* Force lowercase */\n    hex |= 0x20;\n    if ('a' <= hex && hex <= 'f')\n        return 10 + hex - 'a';\n\n    return -1;\n}\n\nstatic int decode_hex4(const char *hex)\n{\n    int digit[4];\n    int i;\n\n    /* Convert ASCII hex digit to numeric digit\n     * Note: this returns an error for invalid hex digits, including\n     *       NULL */\n    for (i = 0; i < 4; i++) {\n        digit[i] = hexdigit2int(hex[i]);\n        if (digit[i] < 0) {\n            return -1;\n        }\n    }\n\n    return (digit[0] << 12) +\n           (digit[1] << 8) +\n           (digit[2] << 4) +\n            digit[3];\n}\n\n/* Converts a Unicode codepoint to UTF-8.\n * Returns UTF-8 string length, and up to 4 bytes in *utf8 */\nstatic int codepoint_to_utf8(char *utf8, int codepoint)\n{\n    /* 0xxxxxxx */\n    if (codepoint <= 0x7F) {\n        utf8[0] = codepoint;\n        return 1;\n    }\n\n    /* 110xxxxx 10xxxxxx */\n    if (codepoint <= 0x7FF) {\n        utf8[0] = (codepoint >> 6) | 0xC0;\n        utf8[1] = (codepoint & 0x3F) | 0x80;\n        return 2;\n    }\n\n    /* 1110xxxx 10xxxxxx 10xxxxxx */\n    if (codepoint <= 0xFFFF) {\n        utf8[0] = (codepoint >> 12) | 0xE0;\n        utf8[1] = ((codepoint >> 6) & 0x3F) | 0x80;\n        utf8[2] = (codepoint & 0x3F) | 0x80;\n        return 3;\n    }\n\n    /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */\n    if (codepoint <= 0x1FFFFF) {\n        utf8[0] = (codepoint >> 18) | 0xF0;\n        utf8[1] = ((codepoint >> 12) & 0x3F) | 0x80;\n        utf8[2] = ((codepoint >> 6) & 0x3F) | 0x80;\n        utf8[3] = (codepoint & 0x3F) | 0x80;\n        return 4;\n    }\n\n    return 0;\n}\n\n\n/* Called when index pointing to beginning of UTF-16 code escape: \\uXXXX\n * \\u is guaranteed to exist, but the remaining hex characters may be\n * missing.\n * Translate to UTF-8 and append to temporary token string.\n * Must advance index to the next character to be processed.\n * Returns: 0   success\n *          -1  error\n */\nstatic int json_append_unicode_escape(json_parse_t *json)\n{\n    char utf8[4];       /* Surrogate pairs require 4 UTF-8 bytes */\n    int codepoint;\n    int surrogate_low;\n    int len;\n    int escape_len = 6;\n\n    /* Fetch UTF-16 code unit */\n    codepoint = decode_hex4(json->ptr + 2);\n    if (codepoint < 0)\n        return -1;\n\n    /* UTF-16 surrogate pairs take the following 2 byte form:\n     *      11011 x yyyyyyyyyy\n     * When x = 0: y is the high 10 bits of the codepoint\n     *      x = 1: y is the low 10 bits of the codepoint\n     *\n     * Check for a surrogate pair (high or low) */\n    if ((codepoint & 0xF800) == 0xD800) {\n        /* Error if the 1st surrogate is not high */\n        if (codepoint & 0x400)\n            return -1;\n\n        /* Ensure the next code is a unicode escape */\n        if (*(json->ptr + escape_len) != '\\\\' ||\n            *(json->ptr + escape_len + 1) != 'u') {\n            return -1;\n        }\n\n        /* Fetch the next codepoint */\n        surrogate_low = decode_hex4(json->ptr + 2 + escape_len);\n        if (surrogate_low < 0)\n            return -1;\n\n        /* Error if the 2nd code is not a low surrogate */\n        if ((surrogate_low & 0xFC00) != 0xDC00)\n            return -1;\n\n        /* Calculate Unicode codepoint */\n        codepoint = (codepoint & 0x3FF) << 10;\n        surrogate_low &= 0x3FF;\n        codepoint = (codepoint | surrogate_low) + 0x10000;\n        escape_len = 12;\n    }\n\n    /* Convert codepoint to UTF-8 */\n    len = codepoint_to_utf8(utf8, codepoint);\n    if (!len)\n        return -1;\n\n    /* Append bytes and advance parse index */\n    strbuf_append_mem_unsafe(json->tmp, utf8, len);\n    json->ptr += escape_len;\n\n    return 0;\n}\n\nstatic void json_set_token_error(json_token_t *token, json_parse_t *json,\n                                 const char *errtype)\n{\n    token->type = T_ERROR;\n    token->index = json->ptr - json->data;\n    token->value.string = errtype;\n}\n\nstatic void json_next_string_token(json_parse_t *json, json_token_t *token)\n{\n    char *escape2char = json->cfg->escape2char;\n    char ch;\n\n    /* Caller must ensure a string is next */\n    assert(*json->ptr == '\"');\n\n    /* Skip \" */\n    json->ptr++;\n\n    /* json->tmp is the temporary strbuf used to accumulate the\n     * decoded string value.\n     * json->tmp is sized to handle JSON containing only a string value.\n     */\n    strbuf_reset(json->tmp);\n\n    while ((ch = *json->ptr) != '\"') {\n        if (!ch) {\n            /* Premature end of the string */\n            json_set_token_error(token, json, \"unexpected end of string\");\n            return;\n        }\n\n        /* Handle escapes */\n        if (ch == '\\\\') {\n            /* Fetch escape character */\n            ch = *(json->ptr + 1);\n\n            /* Translate escape code and append to tmp string */\n            ch = escape2char[(unsigned char)ch];\n            if (ch == 'u') {\n                if (json_append_unicode_escape(json) == 0)\n                    continue;\n\n                json_set_token_error(token, json,\n                                     \"invalid unicode escape code\");\n                return;\n            }\n            if (!ch) {\n                json_set_token_error(token, json, \"invalid escape code\");\n                return;\n            }\n\n            /* Skip '\\' */\n            json->ptr++;\n        }\n        /* Append normal character or translated single character\n         * Unicode escapes are handled above */\n        strbuf_append_char_unsafe(json->tmp, ch);\n        json->ptr++;\n    }\n    json->ptr++;    /* Eat final quote (\") */\n\n    strbuf_ensure_null(json->tmp);\n\n    token->type = T_STRING;\n    token->value.string = strbuf_string(json->tmp, &token->string_len);\n}\n\n/* JSON numbers should take the following form:\n *      -?(0|[1-9]|[1-9][0-9]+)(.[0-9]+)?([eE][-+]?[0-9]+)?\n *\n * json_next_number_token() uses strtod() which allows other forms:\n * - numbers starting with '+'\n * - NaN, -NaN, infinity, -infinity\n * - hexadecimal numbers\n * - numbers with leading zeros\n *\n * json_is_invalid_number() detects \"numbers\" which may pass strtod()'s\n * error checking, but should not be allowed with strict JSON.\n *\n * json_is_invalid_number() may pass numbers which cause strtod()\n * to generate an error.\n */\nstatic int json_is_invalid_number(json_parse_t *json)\n{\n    const char *p = json->ptr;\n\n    /* Reject numbers starting with + */\n    if (*p == '+')\n        return 1;\n\n    /* Skip minus sign if it exists */\n    if (*p == '-')\n        p++;\n\n    /* Reject numbers starting with 0x, or leading zeros */\n    if (*p == '0') {\n        int ch2 = *(p + 1);\n\n        if ((ch2 | 0x20) == 'x' ||          /* Hex */\n            ('0' <= ch2 && ch2 <= '9'))     /* Leading zero */\n            return 1;\n\n        return 0;\n    } else if (*p <= '9') {\n        return 0;                           /* Ordinary number */\n    }\n\n    /* Reject inf/nan */\n    if (!strncasecmp(p, \"inf\", 3))\n        return 1;\n    if (!strncasecmp(p, \"nan\", 3))\n        return 1;\n\n    /* Pass all other numbers which may still be invalid, but\n     * strtod() will catch them. */\n    return 0;\n}\n\nstatic void json_next_number_token(json_parse_t *json, json_token_t *token)\n{\n    char *endptr;\n\n    token->type = T_NUMBER;\n    token->value.number = fpconv_strtod(json->ptr, &endptr);\n    if (json->ptr == endptr)\n        json_set_token_error(token, json, \"invalid number\");\n    else\n        json->ptr = endptr;     /* Skip the processed number */\n\n    return;\n}\n\n/* Fills in the token struct.\n * T_STRING will return a pointer to the json_parse_t temporary string\n * T_ERROR will leave the json->ptr pointer at the error.\n */\nstatic void json_next_token(json_parse_t *json, json_token_t *token)\n{\n    const json_token_type_t *ch2token = json->cfg->ch2token;\n    int ch;\n\n    /* Eat whitespace. */\n    while (1) {\n        ch = (unsigned char)*(json->ptr);\n        token->type = ch2token[ch];\n        if (token->type != T_WHITESPACE)\n            break;\n        json->ptr++;\n    }\n\n    /* Store location of new token. Required when throwing errors\n     * for unexpected tokens (syntax errors). */\n    token->index = json->ptr - json->data;\n\n    /* Don't advance the pointer for an error or the end */\n    if (token->type == T_ERROR) {\n        json_set_token_error(token, json, \"invalid token\");\n        return;\n    }\n\n    if (token->type == T_END) {\n        return;\n    }\n\n    /* Found a known single character token, advance index and return */\n    if (token->type != T_UNKNOWN) {\n        json->ptr++;\n        return;\n    }\n\n    /* Process characters which triggered T_UNKNOWN\n     *\n     * Must use strncmp() to match the front of the JSON string.\n     * JSON identifier must be lowercase.\n     * When strict_numbers if disabled, either case is allowed for\n     * Infinity/NaN (since we are no longer following the spec..) */\n    if (ch == '\"') {\n        json_next_string_token(json, token);\n        return;\n    } else if (ch == '-' || ('0' <= ch && ch <= '9')) {\n        if (!json->cfg->decode_invalid_numbers && json_is_invalid_number(json)) {\n            json_set_token_error(token, json, \"invalid number\");\n            return;\n        }\n        json_next_number_token(json, token);\n        return;\n    } else if (!strncmp(json->ptr, \"true\", 4)) {\n        token->type = T_BOOLEAN;\n        token->value.boolean = 1;\n        json->ptr += 4;\n        return;\n    } else if (!strncmp(json->ptr, \"false\", 5)) {\n        token->type = T_BOOLEAN;\n        token->value.boolean = 0;\n        json->ptr += 5;\n        return;\n    } else if (!strncmp(json->ptr, \"null\", 4)) {\n        token->type = T_NULL;\n        json->ptr += 4;\n        return;\n    } else if (json->cfg->decode_invalid_numbers &&\n               json_is_invalid_number(json)) {\n        /* When decode_invalid_numbers is enabled, only attempt to process\n         * numbers we know are invalid JSON (Inf, NaN, hex)\n         * This is required to generate an appropriate token error,\n         * otherwise all bad tokens will register as \"invalid number\"\n         */\n        json_next_number_token(json, token);\n        return;\n    }\n\n    /* Token starts with t/f/n but isn't recognised above. */\n    json_set_token_error(token, json, \"invalid token\");\n}\n\n/* This function does not return.\n * DO NOT CALL WITH DYNAMIC MEMORY ALLOCATED.\n * The only supported exception is the temporary parser string\n * json->tmp struct.\n * json and token should exist on the stack somewhere.\n * luaL_error() will long_jmp and release the stack */\nstatic void json_throw_parse_error(lua_State *l, json_parse_t *json,\n                                   const char *exp, json_token_t *token)\n{\n    const char *found;\n\n    strbuf_free(json->tmp);\n\n    if (token->type == T_ERROR)\n        found = token->value.string;\n    else\n        found = json_token_type_name[token->type];\n\n    /* Note: token->index is 0 based, display starting from 1 */\n    luaL_error(l, \"Expected %s but found %s at character %d\",\n               exp, found, token->index + 1);\n}\n\nstatic inline void json_decode_ascend(json_parse_t *json)\n{\n    json->current_depth--;\n}\n\nstatic void json_decode_descend(lua_State *l, json_parse_t *json, int slots)\n{\n    json->current_depth++;\n\n    if (json->current_depth <= json->cfg->decode_max_depth &&\n        lua_checkstack(l, slots)) {\n        return;\n    }\n\n    strbuf_free(json->tmp);\n    luaL_error(l, \"Found too many nested data structures (%d) at character %d\",\n        json->current_depth, json->ptr - json->data);\n}\n\nstatic void json_parse_object_context(lua_State *l, json_parse_t *json)\n{\n    json_token_t token;\n\n    /* 3 slots required:\n     * .., table, key, value */\n    json_decode_descend(l, json, 3);\n\n    lua_newtable(l);\n\n    json_next_token(json, &token);\n\n    /* Handle empty objects */\n    if (token.type == T_OBJ_END) {\n        json_decode_ascend(json);\n        return;\n    }\n\n    while (1) {\n        if (token.type != T_STRING)\n            json_throw_parse_error(l, json, \"object key string\", &token);\n\n        /* Push key */\n        lua_pushlstring(l, token.value.string, token.string_len);\n\n        json_next_token(json, &token);\n        if (token.type != T_COLON)\n            json_throw_parse_error(l, json, \"colon\", &token);\n\n        /* Fetch value */\n        json_next_token(json, &token);\n        json_process_value(l, json, &token);\n\n        /* Set key = value */\n        lua_rawset(l, -3);\n\n        json_next_token(json, &token);\n\n        if (token.type == T_OBJ_END) {\n            json_decode_ascend(json);\n            return;\n        }\n\n        if (token.type != T_COMMA)\n            json_throw_parse_error(l, json, \"comma or object end\", &token);\n\n        json_next_token(json, &token);\n    }\n}\n\n/* Handle the array context */\nstatic void json_parse_array_context(lua_State *l, json_parse_t *json)\n{\n    json_token_t token;\n    int i;\n\n    /* 2 slots required:\n     * .., table, value */\n    json_decode_descend(l, json, 2);\n\n    lua_newtable(l);\n\n    json_next_token(json, &token);\n\n    /* Handle empty arrays */\n    if (token.type == T_ARR_END) {\n        json_decode_ascend(json);\n        return;\n    }\n\n    for (i = 1; ; i++) {\n        json_process_value(l, json, &token);\n        lua_rawseti(l, -2, i);            /* arr[i] = value */\n\n        json_next_token(json, &token);\n\n        if (token.type == T_ARR_END) {\n            json_decode_ascend(json);\n            return;\n        }\n\n        if (token.type != T_COMMA)\n            json_throw_parse_error(l, json, \"comma or array end\", &token);\n\n        json_next_token(json, &token);\n    }\n}\n\n/* Handle the \"value\" context */\nstatic void json_process_value(lua_State *l, json_parse_t *json,\n                               json_token_t *token)\n{\n    switch (token->type) {\n    case T_STRING:\n        lua_pushlstring(l, token->value.string, token->string_len);\n        break;;\n    case T_NUMBER:\n        lua_pushnumber(l, token->value.number);\n        break;;\n    case T_BOOLEAN:\n        lua_pushboolean(l, token->value.boolean);\n        break;;\n    case T_OBJ_BEGIN:\n        json_parse_object_context(l, json);\n        break;;\n    case T_ARR_BEGIN:\n        json_parse_array_context(l, json);\n        break;;\n    case T_NULL:\n        /* In Lua, setting \"t[k] = nil\" will delete k from the table.\n         * Hence a NULL pointer lightuserdata object is used instead */\n        lua_pushlightuserdata(l, NULL);\n        break;;\n    default:\n        json_throw_parse_error(l, json, \"value\", token);\n    }\n}\n\nstatic int json_decode(lua_State *l)\n{\n    json_parse_t json;\n    json_token_t token;\n    size_t json_len;\n\n    luaL_argcheck(l, lua_gettop(l) == 1, 1, \"expected 1 argument\");\n\n    json.cfg = json_fetch_config(l);\n    json.data = luaL_checklstring(l, 1, &json_len);\n    json.current_depth = 0;\n    json.ptr = json.data;\n\n    /* Detect Unicode other than UTF-8 (see RFC 4627, Sec 3)\n     *\n     * CJSON can support any simple data type, hence only the first\n     * character is guaranteed to be ASCII (at worst: '\"'). This is\n     * still enough to detect whether the wrong encoding is in use. */\n    if (json_len >= 2 && (!json.data[0] || !json.data[1]))\n        luaL_error(l, \"JSON parser does not support UTF-16 or UTF-32\");\n\n    /* Ensure the temporary buffer can hold the entire string.\n     * This means we no longer need to do length checks since the decoded\n     * string must be smaller than the entire json string */\n    json.tmp = strbuf_new(json_len);\n\n    json_next_token(&json, &token);\n    json_process_value(l, &json, &token);\n\n    /* Ensure there is no more input left */\n    json_next_token(&json, &token);\n\n    if (token.type != T_END)\n        json_throw_parse_error(l, &json, \"the end\", &token);\n\n    strbuf_free(json.tmp);\n\n    return 1;\n}\n\n/* ===== INITIALISATION ===== */\n\n#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 502\n/* Compatibility for Lua 5.1.\n *\n * luaL_setfuncs() is used to create a module table where the functions have\n * json_config_t as their first upvalue. Code borrowed from Lua 5.2 source. */\nstatic void luaL_setfuncs (lua_State *l, const luaL_Reg *reg, int nup)\n{\n    int i;\n\n    luaL_checkstack(l, nup, \"too many upvalues\");\n    for (; reg->name != NULL; reg++) {  /* fill the table with given functions */\n        for (i = 0; i < nup; i++)  /* copy upvalues to the top */\n            lua_pushvalue(l, -nup);\n        lua_pushcclosure(l, reg->func, nup);  /* closure with those upvalues */\n        lua_setfield(l, -(nup + 2), reg->name);\n    }\n    lua_pop(l, nup);  /* remove upvalues */\n}\n#endif\n\n/* Call target function in protected mode with all supplied args.\n * Assumes target function only returns a single non-nil value.\n * Convert and return thrown errors as: nil, \"error message\" */\nstatic int json_protect_conversion(lua_State *l)\n{\n    int err;\n\n    /* Deliberately throw an error for invalid arguments */\n    luaL_argcheck(l, lua_gettop(l) == 1, 1, \"expected 1 argument\");\n\n    /* pcall() the function stored as upvalue(1) */\n    lua_pushvalue(l, lua_upvalueindex(1));\n    lua_insert(l, 1);\n    err = lua_pcall(l, 1, 1, 0);\n    if (!err)\n        return 1;\n\n    if (err == LUA_ERRRUN) {\n        lua_pushnil(l);\n        lua_insert(l, -2);\n        return 2;\n    }\n\n    /* Since we are not using a custom error handler, the only remaining\n     * errors are memory related */\n    return luaL_error(l, \"Memory allocation error in CJSON protected call\");\n}\n\n/* Return cjson module table */\nstatic int lua_cjson_new(lua_State *l)\n{\n    luaL_Reg reg[] = {\n        { \"encode\", json_encode },\n        { \"decode\", json_decode },\n        { \"encode_sparse_array\", json_cfg_encode_sparse_array },\n        { \"encode_max_depth\", json_cfg_encode_max_depth },\n        { \"decode_max_depth\", json_cfg_decode_max_depth },\n        { \"encode_number_precision\", json_cfg_encode_number_precision },\n        { \"encode_keep_buffer\", json_cfg_encode_keep_buffer },\n        { \"encode_invalid_numbers\", json_cfg_encode_invalid_numbers },\n        { \"decode_invalid_numbers\", json_cfg_decode_invalid_numbers },\n        { \"new\", lua_cjson_new },\n        { NULL, NULL }\n    };\n\n    /* Initialise number conversions */\n    fpconv_init();\n\n    /* cjson module table */\n    lua_newtable(l);\n\n    /* Register functions with config data as upvalue */\n    json_create_config(l);\n    luaL_setfuncs(l, reg, 1);\n\n    /* Set cjson.null */\n    lua_pushlightuserdata(l, NULL);\n    lua_setfield(l, -2, \"null\");\n\n    /* Set module name / version fields */\n    lua_pushliteral(l, CJSON_MODNAME);\n    lua_setfield(l, -2, \"_NAME\");\n    lua_pushliteral(l, CJSON_VERSION);\n    lua_setfield(l, -2, \"_VERSION\");\n\n    return 1;\n}\n\n/* Return cjson.safe module table */\nstatic int lua_cjson_safe_new(lua_State *l)\n{\n    const char *func[] = { \"decode\", \"encode\", NULL };\n    int i;\n\n    lua_cjson_new(l);\n\n    /* Fix new() method */\n    lua_pushcfunction(l, lua_cjson_safe_new);\n    lua_setfield(l, -2, \"new\");\n\n    for (i = 0; func[i]; i++) {\n        lua_getfield(l, -1, func[i]);\n        lua_pushcclosure(l, json_protect_conversion, 1);\n        lua_setfield(l, -2, func[i]);\n    }\n\n    return 1;\n}\n\nint luaopen_cjson(lua_State *l)\n{\n    lua_cjson_new(l);\n\n#ifdef ENABLE_CJSON_GLOBAL\n    /* Register a global \"cjson\" table. */\n    lua_pushvalue(l, -1);\n    lua_setglobal(l, CJSON_MODNAME);\n#endif\n\n    /* Return cjson table */\n    return 1;\n}\n\nint luaopen_cjson_safe(lua_State *l)\n{\n    lua_cjson_safe_new(l);\n\n    /* Return cjson.safe table */\n    return 1;\n}\n\n/* vi:ai et sw=4 ts=4:\n */\n"
  },
  {
    "path": "deps/lua/src/lua_cmsgpack.c",
    "content": "#include <math.h>\n#include <stdlib.h>\n#include <stdint.h>\n#include <string.h>\n#include <assert.h>\n\n#include \"lua.h\"\n#include \"lauxlib.h\"\n\n#define LUACMSGPACK_NAME        \"cmsgpack\"\n#define LUACMSGPACK_SAFE_NAME   \"cmsgpack_safe\"\n#define LUACMSGPACK_VERSION     \"lua-cmsgpack 0.4.0\"\n#define LUACMSGPACK_COPYRIGHT   \"Copyright (C) 2012, Salvatore Sanfilippo\"\n#define LUACMSGPACK_DESCRIPTION \"MessagePack C implementation for Lua\"\n\n/* Allows a preprocessor directive to override MAX_NESTING */\n#ifndef LUACMSGPACK_MAX_NESTING\n    #define LUACMSGPACK_MAX_NESTING  16 /* Max tables nesting. */\n#endif\n\n/* Check if float or double can be an integer without loss of precision */\n#define IS_INT_TYPE_EQUIVALENT(x, T) (!isinf(x) && (T)(x) == (x))\n\n#define IS_INT64_EQUIVALENT(x) IS_INT_TYPE_EQUIVALENT(x, int64_t)\n#define IS_INT_EQUIVALENT(x) IS_INT_TYPE_EQUIVALENT(x, int)\n\n/* If size of pointer is equal to a 4 byte integer, we're on 32 bits. */\n#if UINTPTR_MAX == UINT_MAX\n    #define BITS_32 1\n#else\n    #define BITS_32 0\n#endif\n\n#if BITS_32\n    #define lua_pushunsigned(L, n) lua_pushnumber(L, n)\n#else\n    #define lua_pushunsigned(L, n) lua_pushinteger(L, n)\n#endif\n\n/* =============================================================================\n * MessagePack implementation and bindings for Lua 5.1/5.2.\n * Copyright(C) 2012 Salvatore Sanfilippo <antirez@gmail.com>\n *\n * http://github.com/antirez/lua-cmsgpack\n *\n * For MessagePack specification check the following web site:\n * http://wiki.msgpack.org/display/MSGPACK/Format+specification\n *\n * See Copyright Notice at the end of this file.\n *\n * CHANGELOG:\n * 19-Feb-2012 (ver 0.1.0): Initial release.\n * 20-Feb-2012 (ver 0.2.0): Tables encoding improved.\n * 20-Feb-2012 (ver 0.2.1): Minor bug fixing.\n * 20-Feb-2012 (ver 0.3.0): Module renamed lua-cmsgpack (was lua-msgpack).\n * 04-Apr-2014 (ver 0.3.1): Lua 5.2 support and minor bug fix.\n * 07-Apr-2014 (ver 0.4.0): Multiple pack/unpack, lua allocator, efficiency.\n * ========================================================================== */\n\n/* -------------------------- Endian conversion --------------------------------\n * We use it only for floats and doubles, all the other conversions performed\n * in an endian independent fashion. So the only thing we need is a function\n * that swaps a binary string if arch is little endian (and left it untouched\n * otherwise). */\n\n/* Reverse memory bytes if arch is little endian. Given the conceptual\n * simplicity of the Lua build system we prefer check for endianess at runtime.\n * The performance difference should be acceptable. */\nvoid memrevifle(void *ptr, size_t len) {\n    unsigned char   *p = (unsigned char *)ptr,\n                    *e = (unsigned char *)p+len-1,\n                    aux;\n    int test = 1;\n    unsigned char *testp = (unsigned char*) &test;\n\n    if (testp[0] == 0) return; /* Big endian, nothing to do. */\n    len /= 2;\n    while(len--) {\n        aux = *p;\n        *p = *e;\n        *e = aux;\n        p++;\n        e--;\n    }\n}\n\n/* ---------------------------- String buffer ----------------------------------\n * This is a simple implementation of string buffers. The only operation\n * supported is creating empty buffers and appending bytes to it.\n * The string buffer uses 2x preallocation on every realloc for O(N) append\n * behavior.  */\n\ntypedef struct mp_buf {\n    unsigned char *b;\n    size_t len, free;\n} mp_buf;\n\nvoid *mp_realloc(lua_State *L, void *target, size_t osize,size_t nsize) {\n    void *(*local_realloc) (void *, void *, size_t osize, size_t nsize) = NULL;\n    void *ud;\n\n    local_realloc = lua_getallocf(L, &ud);\n\n    return local_realloc(ud, target, osize, nsize);\n}\n\nmp_buf *mp_buf_new(lua_State *L) {\n    mp_buf *buf = NULL;\n\n    /* Old size = 0; new size = sizeof(*buf) */\n    buf = (mp_buf*)mp_realloc(L, NULL, 0, sizeof(*buf));\n\n    buf->b = NULL;\n    buf->len = buf->free = 0;\n    return buf;\n}\n\nvoid mp_buf_append(lua_State *L, mp_buf *buf, const unsigned char *s, size_t len) {\n    if (buf->free < len) {\n        size_t newsize = (buf->len+len)*2;\n\n        buf->b = (unsigned char*)mp_realloc(L, buf->b, buf->len + buf->free, newsize);\n        buf->free = newsize - buf->len;\n    }\n    memcpy(buf->b+buf->len,s,len);\n    buf->len += len;\n    buf->free -= len;\n}\n\nvoid mp_buf_free(lua_State *L, mp_buf *buf) {\n    mp_realloc(L, buf->b, buf->len + buf->free, 0); /* realloc to 0 = free */\n    mp_realloc(L, buf, sizeof(*buf), 0);\n}\n\n/* ---------------------------- String cursor ----------------------------------\n * This simple data structure is used for parsing. Basically you create a cursor\n * using a string pointer and a length, then it is possible to access the\n * current string position with cursor->p, check the remaining length\n * in cursor->left, and finally consume more string using\n * mp_cur_consume(cursor,len), to advance 'p' and subtract 'left'.\n * An additional field cursor->error is set to zero on initialization and can\n * be used to report errors. */\n\n#define MP_CUR_ERROR_NONE   0\n#define MP_CUR_ERROR_EOF    1   /* Not enough data to complete operation. */\n#define MP_CUR_ERROR_BADFMT 2   /* Bad data format */\n\ntypedef struct mp_cur {\n    const unsigned char *p;\n    size_t left;\n    int err;\n} mp_cur;\n\nvoid mp_cur_init(mp_cur *cursor, const unsigned char *s, size_t len) {\n    cursor->p = s;\n    cursor->left = len;\n    cursor->err = MP_CUR_ERROR_NONE;\n}\n\n#define mp_cur_consume(_c,_len) do { _c->p += _len; _c->left -= _len; } while(0)\n\n/* When there is not enough room we set an error in the cursor and return. This\n * is very common across the code so we have a macro to make the code look\n * a bit simpler. */\n#define mp_cur_need(_c,_len) do { \\\n    if (_c->left < _len) { \\\n        _c->err = MP_CUR_ERROR_EOF; \\\n        return; \\\n    } \\\n} while(0)\n\n/* ------------------------- Low level MP encoding -------------------------- */\n\nvoid mp_encode_bytes(lua_State *L, mp_buf *buf, const unsigned char *s, size_t len) {\n    unsigned char hdr[5];\n    int hdrlen;\n\n    if (len < 32) {\n        hdr[0] = 0xa0 | (len&0xff); /* fix raw */\n        hdrlen = 1;\n    } else if (len <= 0xff) {\n        hdr[0] = 0xd9;\n        hdr[1] = len;\n        hdrlen = 2;\n    } else if (len <= 0xffff) {\n        hdr[0] = 0xda;\n        hdr[1] = (len&0xff00)>>8;\n        hdr[2] = len&0xff;\n        hdrlen = 3;\n    } else {\n        hdr[0] = 0xdb;\n        hdr[1] = (len&0xff000000)>>24;\n        hdr[2] = (len&0xff0000)>>16;\n        hdr[3] = (len&0xff00)>>8;\n        hdr[4] = len&0xff;\n        hdrlen = 5;\n    }\n    mp_buf_append(L,buf,hdr,hdrlen);\n    mp_buf_append(L,buf,s,len);\n}\n\n/* we assume IEEE 754 internal format for single and double precision floats. */\nvoid mp_encode_double(lua_State *L, mp_buf *buf, double d) {\n    unsigned char b[9];\n    float f = d;\n\n    assert(sizeof(f) == 4 && sizeof(d) == 8);\n    if (d == (double)f) {\n        b[0] = 0xca;    /* float IEEE 754 */\n        memcpy(b+1,&f,4);\n        memrevifle(b+1,4);\n        mp_buf_append(L,buf,b,5);\n    } else if (sizeof(d) == 8) {\n        b[0] = 0xcb;    /* double IEEE 754 */\n        memcpy(b+1,&d,8);\n        memrevifle(b+1,8);\n        mp_buf_append(L,buf,b,9);\n    }\n}\n\nvoid mp_encode_int(lua_State *L, mp_buf *buf, int64_t n) {\n    unsigned char b[9];\n    int enclen;\n\n    if (n >= 0) {\n        if (n <= 127) {\n            b[0] = n & 0x7f;    /* positive fixnum */\n            enclen = 1;\n        } else if (n <= 0xff) {\n            b[0] = 0xcc;        /* uint 8 */\n            b[1] = n & 0xff;\n            enclen = 2;\n        } else if (n <= 0xffff) {\n            b[0] = 0xcd;        /* uint 16 */\n            b[1] = (n & 0xff00) >> 8;\n            b[2] = n & 0xff;\n            enclen = 3;\n        } else if (n <= 0xffffffffLL) {\n            b[0] = 0xce;        /* uint 32 */\n            b[1] = (n & 0xff000000) >> 24;\n            b[2] = (n & 0xff0000) >> 16;\n            b[3] = (n & 0xff00) >> 8;\n            b[4] = n & 0xff;\n            enclen = 5;\n        } else {\n            b[0] = 0xcf;        /* uint 64 */\n            b[1] = (n & 0xff00000000000000LL) >> 56;\n            b[2] = (n & 0xff000000000000LL) >> 48;\n            b[3] = (n & 0xff0000000000LL) >> 40;\n            b[4] = (n & 0xff00000000LL) >> 32;\n            b[5] = (n & 0xff000000) >> 24;\n            b[6] = (n & 0xff0000) >> 16;\n            b[7] = (n & 0xff00) >> 8;\n            b[8] = n & 0xff;\n            enclen = 9;\n        }\n    } else {\n        if (n >= -32) {\n            b[0] = ((signed char)n);   /* negative fixnum */\n            enclen = 1;\n        } else if (n >= -128) {\n            b[0] = 0xd0;        /* int 8 */\n            b[1] = n & 0xff;\n            enclen = 2;\n        } else if (n >= -32768) {\n            b[0] = 0xd1;        /* int 16 */\n            b[1] = (n & 0xff00) >> 8;\n            b[2] = n & 0xff;\n            enclen = 3;\n        } else if (n >= -2147483648LL) {\n            b[0] = 0xd2;        /* int 32 */\n            b[1] = (n & 0xff000000) >> 24;\n            b[2] = (n & 0xff0000) >> 16;\n            b[3] = (n & 0xff00) >> 8;\n            b[4] = n & 0xff;\n            enclen = 5;\n        } else {\n            b[0] = 0xd3;        /* int 64 */\n            b[1] = (n & 0xff00000000000000LL) >> 56;\n            b[2] = (n & 0xff000000000000LL) >> 48;\n            b[3] = (n & 0xff0000000000LL) >> 40;\n            b[4] = (n & 0xff00000000LL) >> 32;\n            b[5] = (n & 0xff000000) >> 24;\n            b[6] = (n & 0xff0000) >> 16;\n            b[7] = (n & 0xff00) >> 8;\n            b[8] = n & 0xff;\n            enclen = 9;\n        }\n    }\n    mp_buf_append(L,buf,b,enclen);\n}\n\nvoid mp_encode_array(lua_State *L, mp_buf *buf, int64_t n) {\n    unsigned char b[5];\n    int enclen;\n\n    if (n <= 15) {\n        b[0] = 0x90 | (n & 0xf);    /* fix array */\n        enclen = 1;\n    } else if (n <= 65535) {\n        b[0] = 0xdc;                /* array 16 */\n        b[1] = (n & 0xff00) >> 8;\n        b[2] = n & 0xff;\n        enclen = 3;\n    } else {\n        b[0] = 0xdd;                /* array 32 */\n        b[1] = (n & 0xff000000) >> 24;\n        b[2] = (n & 0xff0000) >> 16;\n        b[3] = (n & 0xff00) >> 8;\n        b[4] = n & 0xff;\n        enclen = 5;\n    }\n    mp_buf_append(L,buf,b,enclen);\n}\n\nvoid mp_encode_map(lua_State *L, mp_buf *buf, int64_t n) {\n    unsigned char b[5];\n    int enclen;\n\n    if (n <= 15) {\n        b[0] = 0x80 | (n & 0xf);    /* fix map */\n        enclen = 1;\n    } else if (n <= 65535) {\n        b[0] = 0xde;                /* map 16 */\n        b[1] = (n & 0xff00) >> 8;\n        b[2] = n & 0xff;\n        enclen = 3;\n    } else {\n        b[0] = 0xdf;                /* map 32 */\n        b[1] = (n & 0xff000000) >> 24;\n        b[2] = (n & 0xff0000) >> 16;\n        b[3] = (n & 0xff00) >> 8;\n        b[4] = n & 0xff;\n        enclen = 5;\n    }\n    mp_buf_append(L,buf,b,enclen);\n}\n\n/* --------------------------- Lua types encoding --------------------------- */\n\nvoid mp_encode_lua_string(lua_State *L, mp_buf *buf) {\n    size_t len;\n    const char *s;\n\n    s = lua_tolstring(L,-1,&len);\n    mp_encode_bytes(L,buf,(const unsigned char*)s,len);\n}\n\nvoid mp_encode_lua_bool(lua_State *L, mp_buf *buf) {\n    unsigned char b = lua_toboolean(L,-1) ? 0xc3 : 0xc2;\n    mp_buf_append(L,buf,&b,1);\n}\n\n/* Lua 5.3 has a built in 64-bit integer type */\nvoid mp_encode_lua_integer(lua_State *L, mp_buf *buf) {\n#if (LUA_VERSION_NUM < 503) && BITS_32\n    lua_Number i = lua_tonumber(L,-1);\n#else\n    lua_Integer i = lua_tointeger(L,-1);\n#endif\n    mp_encode_int(L, buf, (int64_t)i);\n}\n\n/* Lua 5.2 and lower only has 64-bit doubles, so we need to\n * detect if the double may be representable as an int\n * for Lua < 5.3 */\nvoid mp_encode_lua_number(lua_State *L, mp_buf *buf) {\n    lua_Number n = lua_tonumber(L,-1);\n\n    if (IS_INT64_EQUIVALENT(n)) {\n        mp_encode_lua_integer(L, buf);\n    } else {\n        mp_encode_double(L,buf,(double)n);\n    }\n}\n\nvoid mp_encode_lua_type(lua_State *L, mp_buf *buf, int level);\n\n/* Convert a lua table into a message pack list. */\nvoid mp_encode_lua_table_as_array(lua_State *L, mp_buf *buf, int level) {\n#if LUA_VERSION_NUM < 502\n    size_t len = lua_objlen(L,-1), j;\n#else\n    size_t len = lua_rawlen(L,-1), j;\n#endif\n\n    mp_encode_array(L,buf,len);\n    luaL_checkstack(L, 1, \"in function mp_encode_lua_table_as_array\");\n    for (j = 1; j <= len; j++) {\n        lua_pushnumber(L,j);\n        lua_gettable(L,-2);\n        mp_encode_lua_type(L,buf,level+1);\n    }\n}\n\n/* Convert a lua table into a message pack key-value map. */\nvoid mp_encode_lua_table_as_map(lua_State *L, mp_buf *buf, int level) {\n    size_t len = 0;\n\n    /* First step: count keys into table. No other way to do it with the\n     * Lua API, we need to iterate a first time. Note that an alternative\n     * would be to do a single run, and then hack the buffer to insert the\n     * map opcodes for message pack. Too hackish for this lib. */\n    luaL_checkstack(L, 3, \"in function mp_encode_lua_table_as_map\");\n    lua_pushnil(L);\n    while(lua_next(L,-2)) {\n        lua_pop(L,1); /* remove value, keep key for next iteration. */\n        len++;\n    }\n\n    /* Step two: actually encoding of the map. */\n    mp_encode_map(L,buf,len);\n    lua_pushnil(L);\n    while(lua_next(L,-2)) {\n        /* Stack: ... key value */\n        lua_pushvalue(L,-2); /* Stack: ... key value key */\n        mp_encode_lua_type(L,buf,level+1); /* encode key */\n        mp_encode_lua_type(L,buf,level+1); /* encode val */\n    }\n}\n\n/* Returns true if the Lua table on top of the stack is exclusively composed\n * of keys from numerical keys from 1 up to N, with N being the total number\n * of elements, without any hole in the middle. */\nint table_is_an_array(lua_State *L) {\n    int count = 0, max = 0;\n#if LUA_VERSION_NUM < 503\n    lua_Number n;\n#else\n    lua_Integer n;\n#endif\n\n    /* Stack top on function entry */\n    int stacktop;\n\n    stacktop = lua_gettop(L);\n\n    lua_pushnil(L);\n    while(lua_next(L,-2)) {\n        /* Stack: ... key value */\n        lua_pop(L,1); /* Stack: ... key */\n        /* The <= 0 check is valid here because we're comparing indexes. */\n#if LUA_VERSION_NUM < 503\n        if ((LUA_TNUMBER != lua_type(L,-1)) || (n = lua_tonumber(L, -1)) <= 0 ||\n            !IS_INT_EQUIVALENT(n))\n#else\n        if (!lua_isinteger(L,-1) || (n = lua_tointeger(L, -1)) <= 0)\n#endif\n        {\n            lua_settop(L, stacktop);\n            return 0;\n        }\n        max = (n > max ? n : max);\n        count++;\n    }\n    /* We have the total number of elements in \"count\". Also we have\n     * the max index encountered in \"max\". We can't reach this code\n     * if there are indexes <= 0. If you also note that there can not be\n     * repeated keys into a table, you have that if max==count you are sure\n     * that there are all the keys form 1 to count (both included). */\n    lua_settop(L, stacktop);\n    return max == count;\n}\n\n/* If the length operator returns non-zero, that is, there is at least\n * an object at key '1', we serialize to message pack list. Otherwise\n * we use a map. */\nvoid mp_encode_lua_table(lua_State *L, mp_buf *buf, int level) {\n    if (table_is_an_array(L))\n        mp_encode_lua_table_as_array(L,buf,level);\n    else\n        mp_encode_lua_table_as_map(L,buf,level);\n}\n\nvoid mp_encode_lua_null(lua_State *L, mp_buf *buf) {\n    unsigned char b[1];\n\n    b[0] = 0xc0;\n    mp_buf_append(L,buf,b,1);\n}\n\nvoid mp_encode_lua_type(lua_State *L, mp_buf *buf, int level) {\n    int t = lua_type(L,-1);\n\n    /* Limit the encoding of nested tables to a specified maximum depth, so that\n     * we survive when called against circular references in tables. */\n    if (t == LUA_TTABLE && level == LUACMSGPACK_MAX_NESTING) t = LUA_TNIL;\n    switch(t) {\n    case LUA_TSTRING: mp_encode_lua_string(L,buf); break;\n    case LUA_TBOOLEAN: mp_encode_lua_bool(L,buf); break;\n    case LUA_TNUMBER:\n    #if LUA_VERSION_NUM < 503\n        mp_encode_lua_number(L,buf); break;\n    #else\n        if (lua_isinteger(L, -1)) {\n            mp_encode_lua_integer(L, buf);\n        } else {\n            mp_encode_lua_number(L, buf);\n        }\n        break;\n    #endif\n    case LUA_TTABLE: mp_encode_lua_table(L,buf,level); break;\n    default: mp_encode_lua_null(L,buf); break;\n    }\n    lua_pop(L,1);\n}\n\n/*\n * Packs all arguments as a stream for multiple upacking later.\n * Returns error if no arguments provided.\n */\nint mp_pack(lua_State *L) {\n    int nargs = lua_gettop(L);\n    int i;\n    mp_buf *buf;\n\n    if (nargs == 0)\n        return luaL_argerror(L, 0, \"MessagePack pack needs input.\");\n\n    if (!lua_checkstack(L, nargs))\n        return luaL_argerror(L, 0, \"Too many arguments for MessagePack pack.\");\n\n    buf = mp_buf_new(L);\n    for(i = 1; i <= nargs; i++) {\n        /* Copy argument i to top of stack for _encode processing;\n         * the encode function pops it from the stack when complete. */\n        luaL_checkstack(L, 1, \"in function mp_check\");\n        lua_pushvalue(L, i);\n\n        mp_encode_lua_type(L,buf,0);\n\n        lua_pushlstring(L,(char*)buf->b,buf->len);\n\n        /* Reuse the buffer for the next operation by\n         * setting its free count to the total buffer size\n         * and the current position to zero. */\n        buf->free += buf->len;\n        buf->len = 0;\n    }\n    mp_buf_free(L, buf);\n\n    /* Concatenate all nargs buffers together */\n    lua_concat(L, nargs);\n    return 1;\n}\n\n/* ------------------------------- Decoding --------------------------------- */\n\nvoid mp_decode_to_lua_type(lua_State *L, mp_cur *c);\n\nvoid mp_decode_to_lua_array(lua_State *L, mp_cur *c, size_t len) {\n    assert(len <= UINT_MAX);\n    int index = 1;\n\n    lua_newtable(L);\n    luaL_checkstack(L, 1, \"in function mp_decode_to_lua_array\");\n    while(len--) {\n        lua_pushnumber(L,index++);\n        mp_decode_to_lua_type(L,c);\n        if (c->err) return;\n        lua_settable(L,-3);\n    }\n}\n\nvoid mp_decode_to_lua_hash(lua_State *L, mp_cur *c, size_t len) {\n    assert(len <= UINT_MAX);\n    lua_newtable(L);\n    while(len--) {\n        mp_decode_to_lua_type(L,c); /* key */\n        if (c->err) return;\n        mp_decode_to_lua_type(L,c); /* value */\n        if (c->err) return;\n        lua_settable(L,-3);\n    }\n}\n\n/* Decode a Message Pack raw object pointed by the string cursor 'c' to\n * a Lua type, that is left as the only result on the stack. */\nvoid mp_decode_to_lua_type(lua_State *L, mp_cur *c) {\n    mp_cur_need(c,1);\n\n    /* If we return more than 18 elements, we must resize the stack to\n     * fit all our return values.  But, there is no way to\n     * determine how many objects a msgpack will unpack to up front, so\n     * we request a +1 larger stack on each iteration (noop if stack is\n     * big enough, and when stack does require resize it doubles in size) */\n    luaL_checkstack(L, 1,\n        \"too many return values at once; \"\n        \"use unpack_one or unpack_limit instead.\");\n\n    switch(c->p[0]) {\n    case 0xcc:  /* uint 8 */\n        mp_cur_need(c,2);\n        lua_pushunsigned(L,c->p[1]);\n        mp_cur_consume(c,2);\n        break;\n    case 0xd0:  /* int 8 */\n        mp_cur_need(c,2);\n        lua_pushinteger(L,(signed char)c->p[1]);\n        mp_cur_consume(c,2);\n        break;\n    case 0xcd:  /* uint 16 */\n        mp_cur_need(c,3);\n        lua_pushunsigned(L,\n            (c->p[1] << 8) |\n             c->p[2]);\n        mp_cur_consume(c,3);\n        break;\n    case 0xd1:  /* int 16 */\n        mp_cur_need(c,3);\n        lua_pushinteger(L,(int16_t)\n            (c->p[1] << 8) |\n             c->p[2]);\n        mp_cur_consume(c,3);\n        break;\n    case 0xce:  /* uint 32 */\n        mp_cur_need(c,5);\n        lua_pushunsigned(L,\n            ((uint32_t)c->p[1] << 24) |\n            ((uint32_t)c->p[2] << 16) |\n            ((uint32_t)c->p[3] << 8) |\n             (uint32_t)c->p[4]);\n        mp_cur_consume(c,5);\n        break;\n    case 0xd2:  /* int 32 */\n        mp_cur_need(c,5);\n        lua_pushinteger(L,\n            ((int32_t)c->p[1] << 24) |\n            ((int32_t)c->p[2] << 16) |\n            ((int32_t)c->p[3] << 8) |\n             (int32_t)c->p[4]);\n        mp_cur_consume(c,5);\n        break;\n    case 0xcf:  /* uint 64 */\n        mp_cur_need(c,9);\n        lua_pushunsigned(L,\n            ((uint64_t)c->p[1] << 56) |\n            ((uint64_t)c->p[2] << 48) |\n            ((uint64_t)c->p[3] << 40) |\n            ((uint64_t)c->p[4] << 32) |\n            ((uint64_t)c->p[5] << 24) |\n            ((uint64_t)c->p[6] << 16) |\n            ((uint64_t)c->p[7] << 8) |\n             (uint64_t)c->p[8]);\n        mp_cur_consume(c,9);\n        break;\n    case 0xd3:  /* int 64 */\n        mp_cur_need(c,9);\n#if LUA_VERSION_NUM < 503\n        lua_pushnumber(L,\n#else\n        lua_pushinteger(L,\n#endif\n            ((int64_t)c->p[1] << 56) |\n            ((int64_t)c->p[2] << 48) |\n            ((int64_t)c->p[3] << 40) |\n            ((int64_t)c->p[4] << 32) |\n            ((int64_t)c->p[5] << 24) |\n            ((int64_t)c->p[6] << 16) |\n            ((int64_t)c->p[7] << 8) |\n             (int64_t)c->p[8]);\n        mp_cur_consume(c,9);\n        break;\n    case 0xc0:  /* nil */\n        lua_pushnil(L);\n        mp_cur_consume(c,1);\n        break;\n    case 0xc3:  /* true */\n        lua_pushboolean(L,1);\n        mp_cur_consume(c,1);\n        break;\n    case 0xc2:  /* false */\n        lua_pushboolean(L,0);\n        mp_cur_consume(c,1);\n        break;\n    case 0xca:  /* float */\n        mp_cur_need(c,5);\n        assert(sizeof(float) == 4);\n        {\n            float f;\n            memcpy(&f,c->p+1,4);\n            memrevifle(&f,4);\n            lua_pushnumber(L,f);\n            mp_cur_consume(c,5);\n        }\n        break;\n    case 0xcb:  /* double */\n        mp_cur_need(c,9);\n        assert(sizeof(double) == 8);\n        {\n            double d;\n            memcpy(&d,c->p+1,8);\n            memrevifle(&d,8);\n            lua_pushnumber(L,d);\n            mp_cur_consume(c,9);\n        }\n        break;\n    case 0xd9:  /* raw 8 */\n        mp_cur_need(c,2);\n        {\n            size_t l = c->p[1];\n            mp_cur_need(c,2+l);\n            lua_pushlstring(L,(char*)c->p+2,l);\n            mp_cur_consume(c,2+l);\n        }\n        break;\n    case 0xda:  /* raw 16 */\n        mp_cur_need(c,3);\n        {\n            size_t l = (c->p[1] << 8) | c->p[2];\n            mp_cur_need(c,3+l);\n            lua_pushlstring(L,(char*)c->p+3,l);\n            mp_cur_consume(c,3+l);\n        }\n        break;\n    case 0xdb:  /* raw 32 */\n        mp_cur_need(c,5);\n        {\n            size_t l = ((size_t)c->p[1] << 24) |\n                       ((size_t)c->p[2] << 16) |\n                       ((size_t)c->p[3] << 8) |\n                       (size_t)c->p[4];\n            mp_cur_consume(c,5);\n            mp_cur_need(c,l);\n            lua_pushlstring(L,(char*)c->p,l);\n            mp_cur_consume(c,l);\n        }\n        break;\n    case 0xdc:  /* array 16 */\n        mp_cur_need(c,3);\n        {\n            size_t l = (c->p[1] << 8) | c->p[2];\n            mp_cur_consume(c,3);\n            mp_decode_to_lua_array(L,c,l);\n        }\n        break;\n    case 0xdd:  /* array 32 */\n        mp_cur_need(c,5);\n        {\n            size_t l = ((size_t)c->p[1] << 24) |\n                       ((size_t)c->p[2] << 16) |\n                       ((size_t)c->p[3] << 8) |\n                       (size_t)c->p[4];\n            mp_cur_consume(c,5);\n            mp_decode_to_lua_array(L,c,l);\n        }\n        break;\n    case 0xde:  /* map 16 */\n        mp_cur_need(c,3);\n        {\n            size_t l = (c->p[1] << 8) | c->p[2];\n            mp_cur_consume(c,3);\n            mp_decode_to_lua_hash(L,c,l);\n        }\n        break;\n    case 0xdf:  /* map 32 */\n        mp_cur_need(c,5);\n        {\n            size_t l = ((size_t)c->p[1] << 24) |\n                       ((size_t)c->p[2] << 16) |\n                       ((size_t)c->p[3] << 8) |\n                       (size_t)c->p[4];\n            mp_cur_consume(c,5);\n            mp_decode_to_lua_hash(L,c,l);\n        }\n        break;\n    default:    /* types that can't be idenitified by first byte value. */\n        if ((c->p[0] & 0x80) == 0) {   /* positive fixnum */\n            lua_pushunsigned(L,c->p[0]);\n            mp_cur_consume(c,1);\n        } else if ((c->p[0] & 0xe0) == 0xe0) {  /* negative fixnum */\n            lua_pushinteger(L,(signed char)c->p[0]);\n            mp_cur_consume(c,1);\n        } else if ((c->p[0] & 0xe0) == 0xa0) {  /* fix raw */\n            size_t l = c->p[0] & 0x1f;\n            mp_cur_need(c,1+l);\n            lua_pushlstring(L,(char*)c->p+1,l);\n            mp_cur_consume(c,1+l);\n        } else if ((c->p[0] & 0xf0) == 0x90) {  /* fix map */\n            size_t l = c->p[0] & 0xf;\n            mp_cur_consume(c,1);\n            mp_decode_to_lua_array(L,c,l);\n        } else if ((c->p[0] & 0xf0) == 0x80) {  /* fix map */\n            size_t l = c->p[0] & 0xf;\n            mp_cur_consume(c,1);\n            mp_decode_to_lua_hash(L,c,l);\n        } else {\n            c->err = MP_CUR_ERROR_BADFMT;\n        }\n    }\n}\n\nint mp_unpack_full(lua_State *L, int limit, int offset) {\n    size_t len;\n    const char *s;\n    mp_cur c;\n    int cnt; /* Number of objects unpacked */\n    int decode_all = (!limit && !offset);\n\n    s = luaL_checklstring(L,1,&len); /* if no match, exits */\n\n    if (offset < 0 || limit < 0) /* requesting negative off or lim is invalid */\n        return luaL_error(L,\n            \"Invalid request to unpack with offset of %d and limit of %d.\",\n            offset, len);\n    else if (offset > len)\n        return luaL_error(L,\n            \"Start offset %d greater than input length %d.\", offset, len);\n\n    if (decode_all) limit = INT_MAX;\n\n    mp_cur_init(&c,(const unsigned char *)s+offset,len-offset);\n\n    /* We loop over the decode because this could be a stream\n     * of multiple top-level values serialized together */\n    for(cnt = 0; c.left > 0 && cnt < limit; cnt++) {\n        mp_decode_to_lua_type(L,&c);\n\n        if (c.err == MP_CUR_ERROR_EOF) {\n            return luaL_error(L,\"Missing bytes in input.\");\n        } else if (c.err == MP_CUR_ERROR_BADFMT) {\n            return luaL_error(L,\"Bad data format in input.\");\n        }\n    }\n\n    if (!decode_all) {\n        /* c->left is the remaining size of the input buffer.\n         * subtract the entire buffer size from the unprocessed size\n         * to get our next start offset */\n        int offset = len - c.left;\n\n        luaL_checkstack(L, 1, \"in function mp_unpack_full\");\n\n        /* Return offset -1 when we have have processed the entire buffer. */\n        lua_pushinteger(L, c.left == 0 ? -1 : offset);\n        /* Results are returned with the arg elements still\n         * in place. Lua takes care of only returning\n         * elements above the args for us.\n         * In this case, we have one arg on the stack\n         * for this function, so we insert our first return\n         * value at position 2. */\n        lua_insert(L, 2);\n        cnt += 1; /* increase return count by one to make room for offset */\n    }\n\n    return cnt;\n}\n\nint mp_unpack(lua_State *L) {\n    return mp_unpack_full(L, 0, 0);\n}\n\nint mp_unpack_one(lua_State *L) {\n    int offset = luaL_optinteger(L, 2, 0);\n    /* Variable pop because offset may not exist */\n    lua_pop(L, lua_gettop(L)-1);\n    return mp_unpack_full(L, 1, offset);\n}\n\nint mp_unpack_limit(lua_State *L) {\n    int limit = luaL_checkinteger(L, 2);\n    int offset = luaL_optinteger(L, 3, 0);\n    /* Variable pop because offset may not exist */\n    lua_pop(L, lua_gettop(L)-1);\n\n    return mp_unpack_full(L, limit, offset);\n}\n\nint mp_safe(lua_State *L) {\n    int argc, err, total_results;\n\n    argc = lua_gettop(L);\n\n    /* This adds our function to the bottom of the stack\n     * (the \"call this function\" position) */\n    lua_pushvalue(L, lua_upvalueindex(1));\n    lua_insert(L, 1);\n\n    err = lua_pcall(L, argc, LUA_MULTRET, 0);\n    total_results = lua_gettop(L);\n\n    if (!err) {\n        return total_results;\n    } else {\n        lua_pushnil(L);\n        lua_insert(L,-2);\n        return 2;\n    }\n}\n\n/* -------------------------------------------------------------------------- */\nconst struct luaL_Reg cmds[] = {\n    {\"pack\", mp_pack},\n    {\"unpack\", mp_unpack},\n    {\"unpack_one\", mp_unpack_one},\n    {\"unpack_limit\", mp_unpack_limit},\n    {0}\n};\n\nint luaopen_create(lua_State *L) {\n    int i;\n    /* Manually construct our module table instead of\n     * relying on _register or _newlib */\n    lua_newtable(L);\n\n    for (i = 0; i < (sizeof(cmds)/sizeof(*cmds) - 1); i++) {\n        lua_pushcfunction(L, cmds[i].func);\n        lua_setfield(L, -2, cmds[i].name);\n    }\n\n    /* Add metadata */\n    lua_pushliteral(L, LUACMSGPACK_NAME);\n    lua_setfield(L, -2, \"_NAME\");\n    lua_pushliteral(L, LUACMSGPACK_VERSION);\n    lua_setfield(L, -2, \"_VERSION\");\n    lua_pushliteral(L, LUACMSGPACK_COPYRIGHT);\n    lua_setfield(L, -2, \"_COPYRIGHT\");\n    lua_pushliteral(L, LUACMSGPACK_DESCRIPTION);\n    lua_setfield(L, -2, \"_DESCRIPTION\");\n    return 1;\n}\n\nLUALIB_API int luaopen_cmsgpack(lua_State *L) {\n    luaopen_create(L);\n\n#if LUA_VERSION_NUM < 502\n    /* Register name globally for 5.1 */\n    lua_pushvalue(L, -1);\n    lua_setglobal(L, LUACMSGPACK_NAME);\n#endif\n\n    return 1;\n}\n\nLUALIB_API int luaopen_cmsgpack_safe(lua_State *L) {\n    int i;\n\n    luaopen_cmsgpack(L);\n\n    /* Wrap all functions in the safe handler */\n    for (i = 0; i < (sizeof(cmds)/sizeof(*cmds) - 1); i++) {\n        lua_getfield(L, -1, cmds[i].name);\n        lua_pushcclosure(L, mp_safe, 1);\n        lua_setfield(L, -2, cmds[i].name);\n    }\n\n#if LUA_VERSION_NUM < 502\n    /* Register name globally for 5.1 */\n    lua_pushvalue(L, -1);\n    lua_setglobal(L, LUACMSGPACK_SAFE_NAME);\n#endif\n\n    return 1;\n}\n\n/******************************************************************************\n* Copyright (C) 2012 Salvatore Sanfilippo.  All rights reserved.\n*\n* Permission is hereby granted, free of charge, to any person obtaining\n* a copy of this software and associated documentation files (the\n* \"Software\"), to deal in the Software without restriction, including\n* without limitation the rights to use, copy, modify, merge, publish,\n* distribute, sublicense, and/or sell copies of the Software, and to\n* permit persons to whom the Software is furnished to do so, subject to\n* the following conditions:\n*\n* The above copyright notice and this permission notice shall be\n* included in all copies or substantial portions of the Software.\n*\n* THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\n* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\n* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\n* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\n* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n******************************************************************************/\n"
  },
  {
    "path": "deps/lua/src/lua_struct.c",
    "content": "/*\n** {======================================================\n** Library for packing/unpacking structures.\n** $Id: struct.c,v 1.7 2018/05/11 22:04:31 roberto Exp $\n** See Copyright Notice at the end of this file\n** =======================================================\n*/\n/*\n** Valid formats:\n** > - big endian\n** < - little endian\n** ![num] - alignment\n** x - pading\n** b/B - signed/unsigned byte\n** h/H - signed/unsigned short\n** l/L - signed/unsigned long\n** T   - size_t\n** i/In - signed/unsigned integer with size 'n' (default is size of int)\n** cn - sequence of 'n' chars (from/to a string); when packing, n==0 means\n        the whole string; when unpacking, n==0 means use the previous\n        read number as the string length\n** s - zero-terminated string\n** f - float\n** d - double\n** ' ' - ignored\n*/\n\n\n#include <assert.h>\n#include <ctype.h>\n#include <limits.h>\n#include <stddef.h>\n#include <string.h>\n\n\n#include \"lua.h\"\n#include \"lauxlib.h\"\n\n\n#if (LUA_VERSION_NUM >= 502)\n\n#define luaL_register(L,n,f)\tluaL_newlib(L,f)\n\n#endif\n\n\n/* basic integer type */\n#if !defined(STRUCT_INT)\n#define STRUCT_INT\tlong\n#endif\n\ntypedef STRUCT_INT Inttype;\n\n/* corresponding unsigned version */\ntypedef unsigned STRUCT_INT Uinttype;\n\n\n/* maximum size (in bytes) for integral types */\n#define MAXINTSIZE\t32\n\n/* is 'x' a power of 2? */\n#define isp2(x)\t\t((x) > 0 && ((x) & ((x) - 1)) == 0)\n\n/* dummy structure to get alignment requirements */\nstruct cD {\n  char c;\n  double d;\n};\n\n\n#define PADDING\t\t(sizeof(struct cD) - sizeof(double))\n#define MAXALIGN  \t(PADDING > sizeof(int) ? PADDING : sizeof(int))\n\n\n/* endian options */\n#define BIG\t0\n#define LITTLE\t1\n\n\nstatic union {\n  int dummy;\n  char endian;\n} const native = {1};\n\n\ntypedef struct Header {\n  int endian;\n  int align;\n} Header;\n\n\nstatic int getnum (lua_State *L, const char **fmt, int df) {\n  if (!isdigit(**fmt))  /* no number? */\n    return df;  /* return default value */\n  else {\n    int a = 0;\n    do {\n      if (a > (INT_MAX / 10) || a * 10 > (INT_MAX - (**fmt - '0')))\n        luaL_error(L, \"integral size overflow\");\n      a = a*10 + *((*fmt)++) - '0';\n    } while (isdigit(**fmt));\n    return a;\n  }\n}\n\n\n#define defaultoptions(h)\t((h)->endian = native.endian, (h)->align = 1)\n\n\n\nstatic size_t optsize (lua_State *L, char opt, const char **fmt) {\n  switch (opt) {\n    case 'B': case 'b': return sizeof(char);\n    case 'H': case 'h': return sizeof(short);\n    case 'L': case 'l': return sizeof(long);\n    case 'T': return sizeof(size_t);\n    case 'f':  return sizeof(float);\n    case 'd':  return sizeof(double);\n    case 'x': return 1;\n    case 'c': return getnum(L, fmt, 1);\n    case 'i': case 'I': {\n      int sz = getnum(L, fmt, sizeof(int));\n      if (sz > MAXINTSIZE)\n        luaL_error(L, \"integral size %d is larger than limit of %d\",\n                       sz, MAXINTSIZE);\n      return sz;\n    }\n    default: return 0;  /* other cases do not need alignment */\n  }\n}\n\n\n/*\n** return number of bytes needed to align an element of size 'size'\n** at current position 'len'\n*/\nstatic int gettoalign (size_t len, Header *h, int opt, size_t size) {\n  if (size == 0 || opt == 'c') return 0;\n  if (size > (size_t)h->align)\n    size = h->align;  /* respect max. alignment */\n  return (size - (len & (size - 1))) & (size - 1);\n}\n\n\n/*\n** options to control endianess and alignment\n*/\nstatic void controloptions (lua_State *L, int opt, const char **fmt,\n                            Header *h) {\n  switch (opt) {\n    case  ' ': return;  /* ignore white spaces */\n    case '>': h->endian = BIG; return;\n    case '<': h->endian = LITTLE; return;\n    case '!': {\n      int a = getnum(L, fmt, MAXALIGN);\n      if (!isp2(a))\n        luaL_error(L, \"alignment %d is not a power of 2\", a);\n      h->align = a;\n      return;\n    }\n    default: {\n      const char *msg = lua_pushfstring(L, \"invalid format option '%c'\", opt);\n      luaL_argerror(L, 1, msg);\n    }\n  }\n}\n\n\nstatic void putinteger (lua_State *L, luaL_Buffer *b, int arg, int endian,\n                        int size) {\n  lua_Number n = luaL_checknumber(L, arg);\n  Uinttype value;\n  char buff[MAXINTSIZE];\n  if (n < 0)\n    value = (Uinttype)(Inttype)n;\n  else\n    value = (Uinttype)n;\n  if (endian == LITTLE) {\n    int i;\n    for (i = 0; i < size; i++) {\n      buff[i] = (value & 0xff);\n      value >>= 8;\n    }\n  }\n  else {\n    int i;\n    for (i = size - 1; i >= 0; i--) {\n      buff[i] = (value & 0xff);\n      value >>= 8;\n    }\n  }\n  luaL_addlstring(b, buff, size);\n}\n\n\nstatic void correctbytes (char *b, int size, int endian) {\n  if (endian != native.endian) {\n    int i = 0;\n    while (i < --size) {\n      char temp = b[i];\n      b[i++] = b[size];\n      b[size] = temp;\n    }\n  }\n}\n\n\nstatic int b_pack (lua_State *L) {\n  luaL_Buffer b;\n  const char *fmt = luaL_checkstring(L, 1);\n  Header h;\n  int arg = 2;\n  size_t totalsize = 0;\n  defaultoptions(&h);\n  lua_pushnil(L);  /* mark to separate arguments from string buffer */\n  luaL_buffinit(L, &b);\n  while (*fmt != '\\0') {\n    int opt = *fmt++;\n    size_t size = optsize(L, opt, &fmt);\n    int toalign = gettoalign(totalsize, &h, opt, size);\n    totalsize += toalign;\n    while (toalign-- > 0) luaL_addchar(&b, '\\0');\n    switch (opt) {\n      case 'b': case 'B': case 'h': case 'H':\n      case 'l': case 'L': case 'T': case 'i': case 'I': {  /* integer types */\n        putinteger(L, &b, arg++, h.endian, size);\n        break;\n      }\n      case 'x': {\n        luaL_addchar(&b, '\\0');\n        break;\n      }\n      case 'f': {\n        float f = (float)luaL_checknumber(L, arg++);\n        correctbytes((char *)&f, size, h.endian);\n        luaL_addlstring(&b, (char *)&f, size);\n        break;\n      }\n      case 'd': {\n        double d = luaL_checknumber(L, arg++);\n        correctbytes((char *)&d, size, h.endian);\n        luaL_addlstring(&b, (char *)&d, size);\n        break;\n      }\n      case 'c': case 's': {\n        size_t l;\n        const char *s = luaL_checklstring(L, arg++, &l);\n        if (size == 0) size = l;\n        luaL_argcheck(L, l >= (size_t)size, arg, \"string too short\");\n        luaL_addlstring(&b, s, size);\n        if (opt == 's') {\n          luaL_addchar(&b, '\\0');  /* add zero at the end */\n          size++;\n        }\n        break;\n      }\n      default: controloptions(L, opt, &fmt, &h);\n    }\n    totalsize += size;\n  }\n  luaL_pushresult(&b);\n  return 1;\n}\n\n\nstatic lua_Number getinteger (const char *buff, int endian,\n                        int issigned, int size) {\n  Uinttype l = 0;\n  int i;\n  if (endian == BIG) {\n    for (i = 0; i < size; i++) {\n      l <<= 8;\n      l |= (Uinttype)(unsigned char)buff[i];\n    }\n  }\n  else {\n    for (i = size - 1; i >= 0; i--) {\n      l <<= 8;\n      l |= (Uinttype)(unsigned char)buff[i];\n    }\n  }\n  if (!issigned)\n    return (lua_Number)l;\n  else {  /* signed format */\n    Uinttype mask = (Uinttype)(~((Uinttype)0)) << (size*8 - 1);\n    if (l & mask)  /* negative value? */\n      l |= mask;  /* signal extension */\n    return (lua_Number)(Inttype)l;\n  }\n}\n\n\nstatic int b_unpack (lua_State *L) {\n  Header h;\n  const char *fmt = luaL_checkstring(L, 1);\n  size_t ld;\n  const char *data = luaL_checklstring(L, 2, &ld);\n  size_t pos = luaL_optinteger(L, 3, 1);\n  luaL_argcheck(L, pos > 0, 3, \"offset must be 1 or greater\");\n  pos--; /* Lua indexes are 1-based, but here we want 0-based for C\n          * pointer math. */\n  int n = 0;  /* number of results */\n  defaultoptions(&h);\n  while (*fmt) {\n    int opt = *fmt++;\n    size_t size = optsize(L, opt, &fmt);\n    pos += gettoalign(pos, &h, opt, size);\n    luaL_argcheck(L, size <= ld && pos <= ld - size,\n                   2, \"data string too short\");\n    /* stack space for item + next position */\n    luaL_checkstack(L, 2, \"too many results\");\n    switch (opt) {\n      case 'b': case 'B': case 'h': case 'H':\n      case 'l': case 'L': case 'T': case 'i':  case 'I': {  /* integer types */\n        int issigned = islower(opt);\n        lua_Number res = getinteger(data+pos, h.endian, issigned, size);\n        lua_pushnumber(L, res); n++;\n        break;\n      }\n      case 'x': {\n        break;\n      }\n      case 'f': {\n        float f;\n        memcpy(&f, data+pos, size);\n        correctbytes((char *)&f, sizeof(f), h.endian);\n        lua_pushnumber(L, f); n++;\n        break;\n      }\n      case 'd': {\n        double d;\n        memcpy(&d, data+pos, size);\n        correctbytes((char *)&d, sizeof(d), h.endian);\n        lua_pushnumber(L, d); n++;\n        break;\n      }\n      case 'c': {\n        if (size == 0) {\n          if (n == 0 || !lua_isnumber(L, -1))\n            luaL_error(L, \"format 'c0' needs a previous size\");\n          size = lua_tonumber(L, -1);\n          lua_pop(L, 1); n--;\n          luaL_argcheck(L, size <= ld && pos <= ld - size,\n                           2, \"data string too short\");\n        }\n        lua_pushlstring(L, data+pos, size); n++;\n        break;\n      }\n      case 's': {\n        const char *e = (const char *)memchr(data+pos, '\\0', ld - pos);\n        if (e == NULL)\n          luaL_error(L, \"unfinished string in data\");\n        size = (e - (data+pos)) + 1;\n        lua_pushlstring(L, data+pos, size - 1); n++;\n        break;\n      }\n      default: controloptions(L, opt, &fmt, &h);\n    }\n    pos += size;\n  }\n  lua_pushinteger(L, pos + 1);  /* next position */\n  return n + 1;\n}\n\n\nstatic int b_size (lua_State *L) {\n  Header h;\n  const char *fmt = luaL_checkstring(L, 1);\n  size_t pos = 0;\n  defaultoptions(&h);\n  while (*fmt) {\n    int opt = *fmt++;\n    size_t size = optsize(L, opt, &fmt);\n    pos += gettoalign(pos, &h, opt, size);\n    if (opt == 's')\n      luaL_argerror(L, 1, \"option 's' has no fixed size\");\n    else if (opt == 'c' && size == 0)\n      luaL_argerror(L, 1, \"option 'c0' has no fixed size\");\n    if (!isalnum(opt))\n      controloptions(L, opt, &fmt, &h);\n    pos += size;\n  }\n  lua_pushinteger(L, pos);\n  return 1;\n}\n\n/* }====================================================== */\n\n\n\nstatic const struct luaL_Reg thislib[] = {\n  {\"pack\", b_pack},\n  {\"unpack\", b_unpack},\n  {\"size\", b_size},\n  {NULL, NULL}\n};\n\n\nLUALIB_API int luaopen_struct (lua_State *L);\n\nLUALIB_API int luaopen_struct (lua_State *L) {\n  luaL_register(L, \"struct\", thislib);\n  return 1;\n}\n\n\n/******************************************************************************\n* Copyright (C) 2010-2018 Lua.org, PUC-Rio.  All rights reserved.\n*\n* Permission is hereby granted, free of charge, to any person obtaining\n* a copy of this software and associated documentation files (the\n* \"Software\"), to deal in the Software without restriction, including\n* without limitation the rights to use, copy, modify, merge, publish,\n* distribute, sublicense, and/or sell copies of the Software, and to\n* permit persons to whom the Software is furnished to do so, subject to\n* the following conditions:\n*\n* The above copyright notice and this permission notice shall be\n* included in all copies or substantial portions of the Software.\n*\n* THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\n* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\n* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\n* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\n* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n******************************************************************************/\n\n"
  },
  {
    "path": "deps/lua/src/luac.c",
    "content": "/*\n** $Id: luac.c,v 1.54 2006/06/02 17:37:11 lhf Exp $\n** Lua compiler (saves bytecodes to files; also list bytecodes)\n** See Copyright Notice in lua.h\n*/\n\n#include <errno.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#define luac_c\n#define LUA_CORE\n\n#include \"lua.h\"\n#include \"lauxlib.h\"\n\n#include \"ldo.h\"\n#include \"lfunc.h\"\n#include \"lmem.h\"\n#include \"lobject.h\"\n#include \"lopcodes.h\"\n#include \"lstring.h\"\n#include \"lundump.h\"\n\n#define PROGNAME\t\"luac\"\t\t/* default program name */\n#define\tOUTPUT\t\tPROGNAME \".out\"\t/* default output file */\n\nstatic int listing=0;\t\t\t/* list bytecodes? */\nstatic int dumping=1;\t\t\t/* dump bytecodes? */\nstatic int stripping=0;\t\t\t/* strip debug information? */\nstatic char Output[]={ OUTPUT };\t/* default output file name */\nstatic const char* output=Output;\t/* actual output file name */\nstatic const char* progname=PROGNAME;\t/* actual program name */\n\nstatic void fatal(const char* message)\n{\n fprintf(stderr,\"%s: %s\\n\",progname,message);\n exit(EXIT_FAILURE);\n}\n\nstatic void cannot(const char* what)\n{\n fprintf(stderr,\"%s: cannot %s %s: %s\\n\",progname,what,output,strerror(errno));\n exit(EXIT_FAILURE);\n}\n\nstatic void usage(const char* message)\n{\n if (*message=='-')\n  fprintf(stderr,\"%s: unrecognized option \" LUA_QS \"\\n\",progname,message);\n else\n  fprintf(stderr,\"%s: %s\\n\",progname,message);\n fprintf(stderr,\n \"usage: %s [options] [filenames].\\n\"\n \"Available options are:\\n\"\n \"  -        process stdin\\n\"\n \"  -l       list\\n\"\n \"  -o name  output to file \" LUA_QL(\"name\") \" (default is \\\"%s\\\")\\n\"\n \"  -p       parse only\\n\"\n \"  -s       strip debug information\\n\"\n \"  -v       show version information\\n\"\n \"  --       stop handling options\\n\",\n progname,Output);\n exit(EXIT_FAILURE);\n}\n\n#define\tIS(s)\t(strcmp(argv[i],s)==0)\n\nstatic int doargs(int argc, char* argv[])\n{\n int i;\n int version=0;\n if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0];\n for (i=1; i<argc; i++)\n {\n  if (*argv[i]!='-')\t\t\t/* end of options; keep it */\n   break;\n  else if (IS(\"--\"))\t\t\t/* end of options; skip it */\n  {\n   ++i;\n   if (version) ++version;\n   break;\n  }\n  else if (IS(\"-\"))\t\t\t/* end of options; use stdin */\n   break;\n  else if (IS(\"-l\"))\t\t\t/* list */\n   ++listing;\n  else if (IS(\"-o\"))\t\t\t/* output file */\n  {\n   output=argv[++i];\n   if (output==NULL || *output==0) usage(LUA_QL(\"-o\") \" needs argument\");\n   if (IS(\"-\")) output=NULL;\n  }\n  else if (IS(\"-p\"))\t\t\t/* parse only */\n   dumping=0;\n  else if (IS(\"-s\"))\t\t\t/* strip debug information */\n   stripping=1;\n  else if (IS(\"-v\"))\t\t\t/* show version */\n   ++version;\n  else\t\t\t\t\t/* unknown option */\n   usage(argv[i]);\n }\n if (i==argc && (listing || !dumping))\n {\n  dumping=0;\n  argv[--i]=Output;\n }\n if (version)\n {\n  printf(\"%s  %s\\n\",LUA_RELEASE,LUA_COPYRIGHT);\n  if (version==argc-1) exit(EXIT_SUCCESS);\n }\n return i;\n}\n\n#define toproto(L,i) (clvalue(L->top+(i))->l.p)\n\nstatic const Proto* combine(lua_State* L, int n)\n{\n if (n==1)\n  return toproto(L,-1);\n else\n {\n  int i,pc;\n  Proto* f=luaF_newproto(L);\n  setptvalue2s(L,L->top,f); incr_top(L);\n  f->source=luaS_newliteral(L,\"=(\" PROGNAME \")\");\n  f->maxstacksize=1;\n  pc=2*n+1;\n  f->code=luaM_newvector(L,pc,Instruction);\n  f->sizecode=pc;\n  f->p=luaM_newvector(L,n,Proto*);\n  f->sizep=n;\n  pc=0;\n  for (i=0; i<n; i++)\n  {\n   f->p[i]=toproto(L,i-n-1);\n   f->code[pc++]=CREATE_ABx(OP_CLOSURE,0,i);\n   f->code[pc++]=CREATE_ABC(OP_CALL,0,1,1);\n  }\n  f->code[pc++]=CREATE_ABC(OP_RETURN,0,1,0);\n  return f;\n }\n}\n\nstatic int writer(lua_State* L, const void* p, size_t size, void* u)\n{\n UNUSED(L);\n return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0);\n}\n\nstruct Smain {\n int argc;\n char** argv;\n};\n\nstatic int pmain(lua_State* L)\n{\n struct Smain* s = (struct Smain*)lua_touserdata(L, 1);\n int argc=s->argc;\n char** argv=s->argv;\n const Proto* f;\n int i;\n if (!lua_checkstack(L,argc)) fatal(\"too many input files\");\n for (i=0; i<argc; i++)\n {\n  const char* filename=IS(\"-\") ? NULL : argv[i];\n  if (luaL_loadfile(L,filename)!=0) fatal(lua_tostring(L,-1));\n }\n f=combine(L,argc);\n if (listing) luaU_print(f,listing>1);\n if (dumping)\n {\n  FILE* D= (output==NULL) ? stdout : fopen(output,\"wb\");\n  if (D==NULL) cannot(\"open\");\n  lua_lock(L);\n  luaU_dump(L,f,writer,D,stripping);\n  lua_unlock(L);\n  if (ferror(D)) cannot(\"write\");\n  if (fclose(D)) cannot(\"close\");\n }\n return 0;\n}\n\nint main(int argc, char* argv[])\n{\n lua_State* L;\n struct Smain s;\n int i=doargs(argc,argv);\n argc-=i; argv+=i;\n if (argc<=0) usage(\"no input files given\");\n L=lua_open();\n if (L==NULL) fatal(\"not enough memory for state\");\n s.argc=argc;\n s.argv=argv;\n if (lua_cpcall(L,pmain,&s)!=0) fatal(lua_tostring(L,-1));\n lua_close(L);\n return EXIT_SUCCESS;\n}\n"
  },
  {
    "path": "deps/lua/src/luaconf.h",
    "content": "/*\n** $Id: luaconf.h,v 1.82.1.7 2008/02/11 16:25:08 roberto Exp $\n** Configuration file for Lua\n** See Copyright Notice in lua.h\n*/\n\n\n#ifndef lconfig_h\n#define lconfig_h\n\n#include <limits.h>\n#include <stddef.h>\n\n\n/*\n** ==================================================================\n** Search for \"@@\" to find all configurable definitions.\n** ===================================================================\n*/\n\n\n/*\n@@ LUA_ANSI controls the use of non-ansi features.\n** CHANGE it (define it) if you want Lua to avoid the use of any\n** non-ansi feature or library.\n*/\n#if defined(__STRICT_ANSI__)\n#define LUA_ANSI\n#endif\n\n\n#if !defined(LUA_ANSI) && defined(_WIN32)\n#define LUA_WIN\n#endif\n\n#if defined(LUA_USE_LINUX)\n#define LUA_USE_POSIX\n#define LUA_USE_DLOPEN\t\t/* needs an extra library: -ldl */\n#define LUA_USE_READLINE\t/* needs some extra libraries */\n#endif\n\n#if defined(LUA_USE_MACOSX)\n#define LUA_USE_POSIX\n#define LUA_DL_DYLD\t\t/* does not need extra library */\n#endif\n\n\n\n/*\n@@ LUA_USE_POSIX includes all functionallity listed as X/Open System\n@* Interfaces Extension (XSI).\n** CHANGE it (define it) if your system is XSI compatible.\n*/\n#if defined(LUA_USE_POSIX)\n#define LUA_USE_MKSTEMP\n#define LUA_USE_ISATTY\n#define LUA_USE_POPEN\n#define LUA_USE_ULONGJMP\n#endif\n\n\n/*\n@@ LUA_PATH and LUA_CPATH are the names of the environment variables that\n@* Lua check to set its paths.\n@@ LUA_INIT is the name of the environment variable that Lua\n@* checks for initialization code.\n** CHANGE them if you want different names.\n*/\n#define LUA_PATH        \"LUA_PATH\"\n#define LUA_CPATH       \"LUA_CPATH\"\n#define LUA_INIT\t\"LUA_INIT\"\n\n\n/*\n@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for\n@* Lua libraries.\n@@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for\n@* C libraries.\n** CHANGE them if your machine has a non-conventional directory\n** hierarchy or if you want to install your libraries in\n** non-conventional directories.\n*/\n#if defined(_WIN32)\n/*\n** In Windows, any exclamation mark ('!') in the path is replaced by the\n** path of the directory of the executable file of the current process.\n*/\n#define LUA_LDIR\t\"!\\\\lua\\\\\"\n#define LUA_CDIR\t\"!\\\\\"\n#define LUA_PATH_DEFAULT  \\\n\t\t\".\\\\?.lua;\"  LUA_LDIR\"?.lua;\"  LUA_LDIR\"?\\\\init.lua;\" \\\n\t\t             LUA_CDIR\"?.lua;\"  LUA_CDIR\"?\\\\init.lua\"\n#define LUA_CPATH_DEFAULT \\\n\t\".\\\\?.dll;\"  LUA_CDIR\"?.dll;\" LUA_CDIR\"loadall.dll\"\n\n#else\n#define LUA_ROOT\t\"/usr/local/\"\n#define LUA_LDIR\tLUA_ROOT \"share/lua/5.1/\"\n#define LUA_CDIR\tLUA_ROOT \"lib/lua/5.1/\"\n#define LUA_PATH_DEFAULT  \\\n\t\t\"./?.lua;\"  LUA_LDIR\"?.lua;\"  LUA_LDIR\"?/init.lua;\" \\\n\t\t            LUA_CDIR\"?.lua;\"  LUA_CDIR\"?/init.lua\"\n#define LUA_CPATH_DEFAULT \\\n\t\"./?.so;\"  LUA_CDIR\"?.so;\" LUA_CDIR\"loadall.so\"\n#endif\n\n\n/*\n@@ LUA_DIRSEP is the directory separator (for submodules).\n** CHANGE it if your machine does not use \"/\" as the directory separator\n** and is not Windows. (On Windows Lua automatically uses \"\\\".)\n*/\n#if defined(_WIN32)\n#define LUA_DIRSEP\t\"\\\\\"\n#else\n#define LUA_DIRSEP\t\"/\"\n#endif\n\n\n/*\n@@ LUA_PATHSEP is the character that separates templates in a path.\n@@ LUA_PATH_MARK is the string that marks the substitution points in a\n@* template.\n@@ LUA_EXECDIR in a Windows path is replaced by the executable's\n@* directory.\n@@ LUA_IGMARK is a mark to ignore all before it when bulding the\n@* luaopen_ function name.\n** CHANGE them if for some reason your system cannot use those\n** characters. (E.g., if one of those characters is a common character\n** in file/directory names.) Probably you do not need to change them.\n*/\n#define LUA_PATHSEP\t\";\"\n#define LUA_PATH_MARK\t\"?\"\n#define LUA_EXECDIR\t\"!\"\n#define LUA_IGMARK\t\"-\"\n\n\n/*\n@@ LUA_INTEGER is the integral type used by lua_pushinteger/lua_tointeger.\n** CHANGE that if ptrdiff_t is not adequate on your machine. (On most\n** machines, ptrdiff_t gives a good choice between int or long.)\n*/\n#define LUA_INTEGER\tptrdiff_t\n\n\n/*\n@@ LUA_API is a mark for all core API functions.\n@@ LUALIB_API is a mark for all standard library functions.\n** CHANGE them if you need to define those functions in some special way.\n** For instance, if you want to create one Windows DLL with the core and\n** the libraries, you may want to use the following definition (define\n** LUA_BUILD_AS_DLL to get it).\n*/\n#if defined(LUA_BUILD_AS_DLL)\n\n#if defined(LUA_CORE) || defined(LUA_LIB)\n#define LUA_API __declspec(dllexport)\n#else\n#define LUA_API __declspec(dllimport)\n#endif\n\n#else\n\n#define LUA_API\t\textern\n\n#endif\n\n/* more often than not the libs go together with the core */\n#define LUALIB_API\tLUA_API\n\n\n/*\n@@ LUAI_FUNC is a mark for all extern functions that are not to be\n@* exported to outside modules.\n@@ LUAI_DATA is a mark for all extern (const) variables that are not to\n@* be exported to outside modules.\n** CHANGE them if you need to mark them in some special way. Elf/gcc\n** (versions 3.2 and later) mark them as \"hidden\" to optimize access\n** when Lua is compiled as a shared library.\n*/\n#if defined(luaall_c)\n#define LUAI_FUNC\tstatic\n#define LUAI_DATA\t/* empty */\n\n#elif defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \\\n      defined(__ELF__)\n#define LUAI_FUNC\t__attribute__((visibility(\"hidden\"))) extern\n#define LUAI_DATA\tLUAI_FUNC\n\n#else\n#define LUAI_FUNC\textern\n#define LUAI_DATA\textern\n#endif\n\n\n\n/*\n@@ LUA_QL describes how error messages quote program elements.\n** CHANGE it if you want a different appearance.\n*/\n#define LUA_QL(x)\t\"'\" x \"'\"\n#define LUA_QS\t\tLUA_QL(\"%s\")\n\n\n/*\n@@ LUA_IDSIZE gives the maximum size for the description of the source\n@* of a function in debug information.\n** CHANGE it if you want a different size.\n*/\n#define LUA_IDSIZE\t60\n\n\n/*\n** {==================================================================\n** Stand-alone configuration\n** ===================================================================\n*/\n\n#if defined(lua_c) || defined(luaall_c)\n\n/*\n@@ lua_stdin_is_tty detects whether the standard input is a 'tty' (that\n@* is, whether we're running lua interactively).\n** CHANGE it if you have a better definition for non-POSIX/non-Windows\n** systems.\n*/\n#if defined(LUA_USE_ISATTY)\n#include <unistd.h>\n#define lua_stdin_is_tty()\tisatty(0)\n#elif defined(LUA_WIN)\n#include <io.h>\n#include <stdio.h>\n#define lua_stdin_is_tty()\t_isatty(_fileno(stdin))\n#else\n#define lua_stdin_is_tty()\t1  /* assume stdin is a tty */\n#endif\n\n\n/*\n@@ LUA_PROMPT is the default prompt used by stand-alone Lua.\n@@ LUA_PROMPT2 is the default continuation prompt used by stand-alone Lua.\n** CHANGE them if you want different prompts. (You can also change the\n** prompts dynamically, assigning to globals _PROMPT/_PROMPT2.)\n*/\n#define LUA_PROMPT\t\t\"> \"\n#define LUA_PROMPT2\t\t\">> \"\n\n\n/*\n@@ LUA_PROGNAME is the default name for the stand-alone Lua program.\n** CHANGE it if your stand-alone interpreter has a different name and\n** your system is not able to detect that name automatically.\n*/\n#define LUA_PROGNAME\t\t\"lua\"\n\n\n/*\n@@ LUA_MAXINPUT is the maximum length for an input line in the\n@* stand-alone interpreter.\n** CHANGE it if you need longer lines.\n*/\n#define LUA_MAXINPUT\t512\n\n\n/*\n@@ lua_readline defines how to show a prompt and then read a line from\n@* the standard input.\n@@ lua_saveline defines how to \"save\" a read line in a \"history\".\n@@ lua_freeline defines how to free a line read by lua_readline.\n** CHANGE them if you want to improve this functionality (e.g., by using\n** GNU readline and history facilities).\n*/\n#if defined(LUA_USE_READLINE)\n#include <stdio.h>\n#include <readline/readline.h>\n#include <readline/history.h>\n#define lua_readline(L,b,p)\t((void)L, ((b)=readline(p)) != NULL)\n#define lua_saveline(L,idx) \\\n\tif (lua_strlen(L,idx) > 0)  /* non-empty line? */ \\\n\t  add_history(lua_tostring(L, idx));  /* add it to history */\n#define lua_freeline(L,b)\t((void)L, free(b))\n#else\n#define lua_readline(L,b,p)\t\\\n\t((void)L, fputs(p, stdout), fflush(stdout),  /* show prompt */ \\\n\tfgets(b, LUA_MAXINPUT, stdin) != NULL)  /* get line */\n#define lua_saveline(L,idx)\t{ (void)L; (void)idx; }\n#define lua_freeline(L,b)\t{ (void)L; (void)b; }\n#endif\n\n#endif\n\n/* }================================================================== */\n\n\n/*\n@@ LUAI_GCPAUSE defines the default pause between garbage-collector cycles\n@* as a percentage.\n** CHANGE it if you want the GC to run faster or slower (higher values\n** mean larger pauses which mean slower collection.) You can also change\n** this value dynamically.\n*/\n#define LUAI_GCPAUSE\t200  /* 200% (wait memory to double before next GC) */\n\n\n/*\n@@ LUAI_GCMUL defines the default speed of garbage collection relative to\n@* memory allocation as a percentage.\n** CHANGE it if you want to change the granularity of the garbage\n** collection. (Higher values mean coarser collections. 0 represents\n** infinity, where each step performs a full collection.) You can also\n** change this value dynamically.\n*/\n#define LUAI_GCMUL\t200 /* GC runs 'twice the speed' of memory allocation */\n\n\n\n/*\n@@ LUA_COMPAT_GETN controls compatibility with old getn behavior.\n** CHANGE it (define it) if you want exact compatibility with the\n** behavior of setn/getn in Lua 5.0.\n*/\n#undef LUA_COMPAT_GETN\n\n/*\n@@ LUA_COMPAT_LOADLIB controls compatibility about global loadlib.\n** CHANGE it to undefined as soon as you do not need a global 'loadlib'\n** function (the function is still available as 'package.loadlib').\n*/\n#undef LUA_COMPAT_LOADLIB\n\n/*\n@@ LUA_COMPAT_VARARG controls compatibility with old vararg feature.\n** CHANGE it to undefined as soon as your programs use only '...' to\n** access vararg parameters (instead of the old 'arg' table).\n*/\n#define LUA_COMPAT_VARARG\n\n/*\n@@ LUA_COMPAT_MOD controls compatibility with old math.mod function.\n** CHANGE it to undefined as soon as your programs use 'math.fmod' or\n** the new '%' operator instead of 'math.mod'.\n*/\n#define LUA_COMPAT_MOD\n\n/*\n@@ LUA_COMPAT_LSTR controls compatibility with old long string nesting\n@* facility.\n** CHANGE it to 2 if you want the old behaviour, or undefine it to turn\n** off the advisory error when nesting [[...]].\n*/\n#define LUA_COMPAT_LSTR\t\t1\n\n/*\n@@ LUA_COMPAT_GFIND controls compatibility with old 'string.gfind' name.\n** CHANGE it to undefined as soon as you rename 'string.gfind' to\n** 'string.gmatch'.\n*/\n#define LUA_COMPAT_GFIND\n\n/*\n@@ LUA_COMPAT_OPENLIB controls compatibility with old 'luaL_openlib'\n@* behavior.\n** CHANGE it to undefined as soon as you replace to 'luaL_register'\n** your uses of 'luaL_openlib'\n*/\n#define LUA_COMPAT_OPENLIB\n\n\n\n/*\n@@ luai_apicheck is the assert macro used by the Lua-C API.\n** CHANGE luai_apicheck if you want Lua to perform some checks in the\n** parameters it gets from API calls. This may slow down the interpreter\n** a bit, but may be quite useful when debugging C code that interfaces\n** with Lua. A useful redefinition is to use assert.h.\n*/\n#if defined(LUA_USE_APICHECK)\n#include <assert.h>\n#define luai_apicheck(L,o)\t{ (void)L; assert(o); }\n#else\n#define luai_apicheck(L,o)\t{ (void)L; }\n#endif\n\n\n/*\n@@ LUAI_BITSINT defines the number of bits in an int.\n** CHANGE here if Lua cannot automatically detect the number of bits of\n** your machine. Probably you do not need to change this.\n*/\n/* avoid overflows in comparison */\n#if INT_MAX-20 < 32760\n#define LUAI_BITSINT\t16\n#elif INT_MAX > 2147483640L\n/* int has at least 32 bits */\n#define LUAI_BITSINT\t32\n#else\n#error \"you must define LUA_BITSINT with number of bits in an integer\"\n#endif\n\n\n/*\n@@ LUAI_UINT32 is an unsigned integer with at least 32 bits.\n@@ LUAI_INT32 is an signed integer with at least 32 bits.\n@@ LUAI_UMEM is an unsigned integer big enough to count the total\n@* memory used by Lua.\n@@ LUAI_MEM is a signed integer big enough to count the total memory\n@* used by Lua.\n** CHANGE here if for some weird reason the default definitions are not\n** good enough for your machine. (The definitions in the 'else'\n** part always works, but may waste space on machines with 64-bit\n** longs.) Probably you do not need to change this.\n*/\n#if LUAI_BITSINT >= 32\n#define LUAI_UINT32\tunsigned int\n#define LUAI_INT32\tint\n#define LUAI_MAXINT32\tINT_MAX\n#define LUAI_UMEM\tsize_t\n#define LUAI_MEM\tptrdiff_t\n#else\n/* 16-bit ints */\n#define LUAI_UINT32\tunsigned long\n#define LUAI_INT32\tlong\n#define LUAI_MAXINT32\tLONG_MAX\n#define LUAI_UMEM\tunsigned long\n#define LUAI_MEM\tlong\n#endif\n\n\n/*\n@@ LUAI_MAXCALLS limits the number of nested calls.\n** CHANGE it if you need really deep recursive calls. This limit is\n** arbitrary; its only purpose is to stop infinite recursion before\n** exhausting memory.\n*/\n#define LUAI_MAXCALLS\t20000\n\n\n/*\n@@ LUAI_MAXCSTACK limits the number of Lua stack slots that a C function\n@* can use.\n** CHANGE it if you need lots of (Lua) stack space for your C\n** functions. This limit is arbitrary; its only purpose is to stop C\n** functions to consume unlimited stack space. (must be smaller than\n** -LUA_REGISTRYINDEX)\n*/\n#define LUAI_MAXCSTACK\t8000\n\n\n\n/*\n** {==================================================================\n** CHANGE (to smaller values) the following definitions if your system\n** has a small C stack. (Or you may want to change them to larger\n** values if your system has a large C stack and these limits are\n** too rigid for you.) Some of these constants control the size of\n** stack-allocated arrays used by the compiler or the interpreter, while\n** others limit the maximum number of recursive calls that the compiler\n** or the interpreter can perform. Values too large may cause a C stack\n** overflow for some forms of deep constructs.\n** ===================================================================\n*/\n\n\n/*\n@@ LUAI_MAXCCALLS is the maximum depth for nested C calls (short) and\n@* syntactical nested non-terminals in a program.\n*/\n#define LUAI_MAXCCALLS\t\t200\n\n\n/*\n@@ LUAI_MAXVARS is the maximum number of local variables per function\n@* (must be smaller than 250).\n*/\n#define LUAI_MAXVARS\t\t200\n\n\n/*\n@@ LUAI_MAXUPVALUES is the maximum number of upvalues per function\n@* (must be smaller than 250).\n*/\n#define LUAI_MAXUPVALUES\t60\n\n\n/*\n@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system.\n*/\n#define LUAL_BUFFERSIZE\t\tBUFSIZ\n\n/* }================================================================== */\n\n\n\n\n/*\n** {==================================================================\n@@ LUA_NUMBER is the type of numbers in Lua.\n** CHANGE the following definitions only if you want to build Lua\n** with a number type different from double. You may also need to\n** change lua_number2int & lua_number2integer.\n** ===================================================================\n*/\n\n#define LUA_NUMBER_DOUBLE\n#define LUA_NUMBER\tdouble\n\n/*\n@@ LUAI_UACNUMBER is the result of an 'usual argument conversion'\n@* over a number.\n*/\n#define LUAI_UACNUMBER\tdouble\n\n\n/*\n@@ LUA_NUMBER_SCAN is the format for reading numbers.\n@@ LUA_NUMBER_FMT is the format for writing numbers.\n@@ lua_number2str converts a number to a string.\n@@ LUAI_MAXNUMBER2STR is maximum size of previous conversion.\n@@ lua_str2number converts a string to a number.\n*/\n#define LUA_NUMBER_SCAN\t\t\"%lf\"\n#define LUA_NUMBER_FMT\t\t\"%.14g\"\n#define lua_number2str(s,n)\tsprintf((s), LUA_NUMBER_FMT, (n))\n#define LUAI_MAXNUMBER2STR\t32 /* 16 digits, sign, point, and \\0 */\n#define lua_str2number(s,p)\tstrtod((s), (p))\n\n\n/*\n@@ The luai_num* macros define the primitive operations over numbers.\n*/\n#if defined(LUA_CORE)\n#include <math.h>\n#define luai_numadd(a,b)\t((a)+(b))\n#define luai_numsub(a,b)\t((a)-(b))\n#define luai_nummul(a,b)\t((a)*(b))\n#define luai_numdiv(a,b)\t((a)/(b))\n#define luai_nummod(a,b)\t((a) - floor((a)/(b))*(b))\n#define luai_numpow(a,b)\t(pow(a,b))\n#define luai_numunm(a)\t\t(-(a))\n#define luai_numeq(a,b)\t\t((a)==(b))\n#define luai_numlt(a,b)\t\t((a)<(b))\n#define luai_numle(a,b)\t\t((a)<=(b))\n#define luai_numisnan(a)\t(!luai_numeq((a), (a)))\n#endif\n\n\n/*\n@@ lua_number2int is a macro to convert lua_Number to int.\n@@ lua_number2integer is a macro to convert lua_Number to lua_Integer.\n** CHANGE them if you know a faster way to convert a lua_Number to\n** int (with any rounding method and without throwing errors) in your\n** system. In Pentium machines, a naive typecast from double to int\n** in C is extremely slow, so any alternative is worth trying.\n*/\n\n/* On a Pentium, resort to a trick */\n#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) && !defined(__SSE2__) && \\\n    (defined(__i386) || defined (_M_IX86) || defined(__i386__))\n\n/* On a Microsoft compiler, use assembler */\n#if defined(_MSC_VER)\n\n#define lua_number2int(i,d)   __asm fld d   __asm fistp i\n#define lua_number2integer(i,n)\t\tlua_number2int(i, n)\n\n/* the next trick should work on any Pentium, but sometimes clashes\n   with a DirectX idiosyncrasy */\n#else\n\nunion luai_Cast { double l_d; long l_l; };\n#define lua_number2int(i,d) \\\n  { volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; }\n#define lua_number2integer(i,n)\t\tlua_number2int(i, n)\n\n#endif\n\n\n/* this option always works, but may be slow */\n#else\n#define lua_number2int(i,d)\t((i)=(int)(d))\n#define lua_number2integer(i,d)\t((i)=(lua_Integer)(d))\n\n#endif\n\n/* }================================================================== */\n\n\n/*\n@@ LUAI_USER_ALIGNMENT_T is a type that requires maximum alignment.\n** CHANGE it if your system requires alignments larger than double. (For\n** instance, if your system supports long doubles and they must be\n** aligned in 16-byte boundaries, then you should add long double in the\n** union.) Probably you do not need to change this.\n*/\n#define LUAI_USER_ALIGNMENT_T\tunion { double u; void *s; long l; }\n\n\n/*\n@@ LUAI_THROW/LUAI_TRY define how Lua does exception handling.\n** CHANGE them if you prefer to use longjmp/setjmp even with C++\n** or if want/don't to use _longjmp/_setjmp instead of regular\n** longjmp/setjmp. By default, Lua handles errors with exceptions when\n** compiling as C++ code, with _longjmp/_setjmp when asked to use them,\n** and with longjmp/setjmp otherwise.\n*/\n#if defined(__cplusplus)\n/* C++ exceptions */\n#define LUAI_THROW(L,c)\tthrow(c)\n#define LUAI_TRY(L,c,a)\ttry { a } catch(...) \\\n\t{ if ((c)->status == 0) (c)->status = -1; }\n#define luai_jmpbuf\tint  /* dummy variable */\n\n#elif defined(LUA_USE_ULONGJMP)\n/* in Unix, try _longjmp/_setjmp (more efficient) */\n#define LUAI_THROW(L,c)\t_longjmp((c)->b, 1)\n#define LUAI_TRY(L,c,a)\tif (_setjmp((c)->b) == 0) { a }\n#define luai_jmpbuf\tjmp_buf\n\n#else\n/* default handling with long jumps */\n#define LUAI_THROW(L,c)\tlongjmp((c)->b, 1)\n#define LUAI_TRY(L,c,a)\tif (setjmp((c)->b) == 0) { a }\n#define luai_jmpbuf\tjmp_buf\n\n#endif\n\n\n/*\n@@ LUA_MAXCAPTURES is the maximum number of captures that a pattern\n@* can do during pattern-matching.\n** CHANGE it if you need more captures. This limit is arbitrary.\n*/\n#define LUA_MAXCAPTURES\t\t32\n\n\n/*\n@@ lua_tmpnam is the function that the OS library uses to create a\n@* temporary name.\n@@ LUA_TMPNAMBUFSIZE is the maximum size of a name created by lua_tmpnam.\n** CHANGE them if you have an alternative to tmpnam (which is considered\n** insecure) or if you want the original tmpnam anyway.  By default, Lua\n** uses tmpnam except when POSIX is available, where it uses mkstemp.\n*/\n#if defined(loslib_c) || defined(luaall_c)\n\n#if defined(LUA_USE_MKSTEMP)\n#include <unistd.h>\n#define LUA_TMPNAMBUFSIZE\t32\n#define lua_tmpnam(b,e)\t{ \\\n\tstrcpy(b, \"/tmp/lua_XXXXXX\"); \\\n\te = mkstemp(b); \\\n\tif (e != -1) close(e); \\\n\te = (e == -1); }\n\n#else\n#define LUA_TMPNAMBUFSIZE\tL_tmpnam\n#define lua_tmpnam(b,e)\t\t{ e = (tmpnam(b) == NULL); }\n#endif\n\n#endif\n\n\n/*\n@@ lua_popen spawns a new process connected to the current one through\n@* the file streams.\n** CHANGE it if you have a way to implement it in your system.\n*/\n#if defined(LUA_USE_POPEN)\n\n#define lua_popen(L,c,m)\t((void)L, fflush(NULL), popen(c,m))\n#define lua_pclose(L,file)\t((void)L, (pclose(file) != -1))\n\n#elif defined(LUA_WIN)\n\n#define lua_popen(L,c,m)\t((void)L, _popen(c,m))\n#define lua_pclose(L,file)\t((void)L, (_pclose(file) != -1))\n\n#else\n\n#define lua_popen(L,c,m)\t((void)((void)c, m),  \\\n\t\tluaL_error(L, LUA_QL(\"popen\") \" not supported\"), (FILE*)0)\n#define lua_pclose(L,file)\t\t((void)((void)L, file), 0)\n\n#endif\n\n/*\n@@ LUA_DL_* define which dynamic-library system Lua should use.\n** CHANGE here if Lua has problems choosing the appropriate\n** dynamic-library system for your platform (either Windows' DLL, Mac's\n** dyld, or Unix's dlopen). If your system is some kind of Unix, there\n** is a good chance that it has dlopen, so LUA_DL_DLOPEN will work for\n** it.  To use dlopen you also need to adapt the src/Makefile (probably\n** adding -ldl to the linker options), so Lua does not select it\n** automatically.  (When you change the makefile to add -ldl, you must\n** also add -DLUA_USE_DLOPEN.)\n** If you do not want any kind of dynamic library, undefine all these\n** options.\n** By default, _WIN32 gets LUA_DL_DLL and MAC OS X gets LUA_DL_DYLD.\n*/\n#if defined(LUA_USE_DLOPEN)\n#define LUA_DL_DLOPEN\n#endif\n\n#if defined(LUA_WIN)\n#define LUA_DL_DLL\n#endif\n\n\n/*\n@@ LUAI_EXTRASPACE allows you to add user-specific data in a lua_State\n@* (the data goes just *before* the lua_State pointer).\n** CHANGE (define) this if you really need that. This value must be\n** a multiple of the maximum alignment required for your machine.\n*/\n#define LUAI_EXTRASPACE\t\t0\n\n\n/*\n@@ luai_userstate* allow user-specific actions on threads.\n** CHANGE them if you defined LUAI_EXTRASPACE and need to do something\n** extra when a thread is created/deleted/resumed/yielded.\n*/\n#define luai_userstateopen(L)\t\t((void)L)\n#define luai_userstateclose(L)\t\t((void)L)\n#define luai_userstatethread(L,L1)\t((void)L)\n#define luai_userstatefree(L)\t\t((void)L)\n#define luai_userstateresume(L,n)\t((void)L)\n#define luai_userstateyield(L,n)\t((void)L)\n\n\n/*\n@@ LUA_INTFRMLEN is the length modifier for integer conversions\n@* in 'string.format'.\n@@ LUA_INTFRM_T is the integer type correspoding to the previous length\n@* modifier.\n** CHANGE them if your system supports long long or does not support long.\n*/\n\n#if defined(LUA_USELONGLONG)\n\n#define LUA_INTFRMLEN\t\t\"ll\"\n#define LUA_INTFRM_T\t\tlong long\n\n#else\n\n#define LUA_INTFRMLEN\t\t\"l\"\n#define LUA_INTFRM_T\t\tlong\n\n#endif\n\n\n\n/* =================================================================== */\n\n/*\n** Local configuration. You can use this space to add your redefinitions\n** without modifying the main part of the file.\n*/\n\n\n\n#endif\n\n"
  },
  {
    "path": "deps/lua/src/lualib.h",
    "content": "/*\n** $Id: lualib.h,v 1.36.1.1 2007/12/27 13:02:25 roberto Exp $\n** Lua standard libraries\n** See Copyright Notice in lua.h\n*/\n\n\n#ifndef lualib_h\n#define lualib_h\n\n#include \"lua.h\"\n\n\n/* Key to file-handle type */\n#define LUA_FILEHANDLE\t\t\"FILE*\"\n\n\n#define LUA_COLIBNAME\t\"coroutine\"\nLUALIB_API int (luaopen_base) (lua_State *L);\n\n#define LUA_TABLIBNAME\t\"table\"\nLUALIB_API int (luaopen_table) (lua_State *L);\n\n#define LUA_IOLIBNAME\t\"io\"\nLUALIB_API int (luaopen_io) (lua_State *L);\n\n#define LUA_OSLIBNAME\t\"os\"\nLUALIB_API int (luaopen_os) (lua_State *L);\n\n#define LUA_STRLIBNAME\t\"string\"\nLUALIB_API int (luaopen_string) (lua_State *L);\n\n#define LUA_MATHLIBNAME\t\"math\"\nLUALIB_API int (luaopen_math) (lua_State *L);\n\n#define LUA_DBLIBNAME\t\"debug\"\nLUALIB_API int (luaopen_debug) (lua_State *L);\n\n#define LUA_LOADLIBNAME\t\"package\"\nLUALIB_API int (luaopen_package) (lua_State *L);\n\n\n/* open all previous libraries */\nLUALIB_API void (luaL_openlibs) (lua_State *L); \n\n\n\n#ifndef lua_assert\n#define lua_assert(x)\t((void)0)\n#endif\n\n\n#endif\n"
  },
  {
    "path": "deps/lua/src/lundump.c",
    "content": "/*\n** $Id: lundump.c,v 2.7.1.4 2008/04/04 19:51:41 roberto Exp $\n** load precompiled Lua chunks\n** See Copyright Notice in lua.h\n*/\n\n#include <string.h>\n\n#define lundump_c\n#define LUA_CORE\n\n#include \"lua.h\"\n\n#include \"ldebug.h\"\n#include \"ldo.h\"\n#include \"lfunc.h\"\n#include \"lmem.h\"\n#include \"lobject.h\"\n#include \"lstring.h\"\n#include \"lundump.h\"\n#include \"lzio.h\"\n\ntypedef struct {\n lua_State* L;\n ZIO* Z;\n Mbuffer* b;\n const char* name;\n} LoadState;\n\n#ifdef LUAC_TRUST_BINARIES\n#define IF(c,s)\n#define error(S,s)\n#else\n#define IF(c,s)\t\tif (c) error(S,s)\n\nstatic void error(LoadState* S, const char* why)\n{\n luaO_pushfstring(S->L,\"%s: %s in precompiled chunk\",S->name,why);\n luaD_throw(S->L,LUA_ERRSYNTAX);\n}\n#endif\n\n#define LoadMem(S,b,n,size)\tLoadBlock(S,b,(n)*(size))\n#define\tLoadByte(S)\t\t(lu_byte)LoadChar(S)\n#define LoadVar(S,x)\t\tLoadMem(S,&x,1,sizeof(x))\n#define LoadVector(S,b,n,size)\tLoadMem(S,b,n,size)\n\nstatic void LoadBlock(LoadState* S, void* b, size_t size)\n{\n size_t r=luaZ_read(S->Z,b,size);\n IF (r!=0, \"unexpected end\");\n}\n\nstatic int LoadChar(LoadState* S)\n{\n char x;\n LoadVar(S,x);\n return x;\n}\n\nstatic int LoadInt(LoadState* S)\n{\n int x;\n LoadVar(S,x);\n IF (x<0, \"bad integer\");\n return x;\n}\n\nstatic lua_Number LoadNumber(LoadState* S)\n{\n lua_Number x;\n LoadVar(S,x);\n return x;\n}\n\nstatic TString* LoadString(LoadState* S)\n{\n size_t size;\n LoadVar(S,size);\n if (size==0)\n  return NULL;\n else\n {\n  char* s=luaZ_openspace(S->L,S->b,size);\n  LoadBlock(S,s,size);\n  return luaS_newlstr(S->L,s,size-1);\t\t/* remove trailing '\\0' */\n }\n}\n\nstatic void LoadCode(LoadState* S, Proto* f)\n{\n int n=LoadInt(S);\n f->code=luaM_newvector(S->L,n,Instruction);\n f->sizecode=n;\n LoadVector(S,f->code,n,sizeof(Instruction));\n}\n\nstatic Proto* LoadFunction(LoadState* S, TString* p);\n\nstatic void LoadConstants(LoadState* S, Proto* f)\n{\n int i,n;\n n=LoadInt(S);\n f->k=luaM_newvector(S->L,n,TValue);\n f->sizek=n;\n for (i=0; i<n; i++) setnilvalue(&f->k[i]);\n for (i=0; i<n; i++)\n {\n  TValue* o=&f->k[i];\n  int t=LoadChar(S);\n  switch (t)\n  {\n   case LUA_TNIL:\n   \tsetnilvalue(o);\n\tbreak;\n   case LUA_TBOOLEAN:\n   \tsetbvalue(o,LoadChar(S)!=0);\n\tbreak;\n   case LUA_TNUMBER:\n\tsetnvalue(o,LoadNumber(S));\n\tbreak;\n   case LUA_TSTRING:\n\tsetsvalue2n(S->L,o,LoadString(S));\n\tbreak;\n   default:\n\terror(S,\"bad constant\");\n\tbreak;\n  }\n }\n n=LoadInt(S);\n f->p=luaM_newvector(S->L,n,Proto*);\n f->sizep=n;\n for (i=0; i<n; i++) f->p[i]=NULL;\n for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);\n}\n\nstatic void LoadDebug(LoadState* S, Proto* f)\n{\n int i,n;\n n=LoadInt(S);\n f->lineinfo=luaM_newvector(S->L,n,int);\n f->sizelineinfo=n;\n LoadVector(S,f->lineinfo,n,sizeof(int));\n n=LoadInt(S);\n f->locvars=luaM_newvector(S->L,n,LocVar);\n f->sizelocvars=n;\n for (i=0; i<n; i++) f->locvars[i].varname=NULL;\n for (i=0; i<n; i++)\n {\n  f->locvars[i].varname=LoadString(S);\n  f->locvars[i].startpc=LoadInt(S);\n  f->locvars[i].endpc=LoadInt(S);\n }\n n=LoadInt(S);\n f->upvalues=luaM_newvector(S->L,n,TString*);\n f->sizeupvalues=n;\n for (i=0; i<n; i++) f->upvalues[i]=NULL;\n for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);\n}\n\nstatic Proto* LoadFunction(LoadState* S, TString* p)\n{\n Proto* f;\n if (++S->L->nCcalls > LUAI_MAXCCALLS) error(S,\"code too deep\");\n f=luaF_newproto(S->L);\n setptvalue2s(S->L,S->L->top,f); incr_top(S->L);\n f->source=LoadString(S); if (f->source==NULL) f->source=p;\n f->linedefined=LoadInt(S);\n f->lastlinedefined=LoadInt(S);\n f->nups=LoadByte(S);\n f->numparams=LoadByte(S);\n f->is_vararg=LoadByte(S);\n f->maxstacksize=LoadByte(S);\n LoadCode(S,f);\n LoadConstants(S,f);\n LoadDebug(S,f);\n IF (!luaG_checkcode(f), \"bad code\");\n S->L->top--;\n S->L->nCcalls--;\n return f;\n}\n\nstatic void LoadHeader(LoadState* S)\n{\n char h[LUAC_HEADERSIZE];\n char s[LUAC_HEADERSIZE];\n luaU_header(h);\n LoadBlock(S,s,LUAC_HEADERSIZE);\n IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, \"bad header\");\n}\n\n/*\n** load precompiled chunk\n*/\nProto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)\n{\n LoadState S;\n if (*name=='@' || *name=='=')\n  S.name=name+1;\n else if (*name==LUA_SIGNATURE[0])\n  S.name=\"binary string\";\n else\n  S.name=name;\n S.L=L;\n S.Z=Z;\n S.b=buff;\n LoadHeader(&S);\n return LoadFunction(&S,luaS_newliteral(L,\"=?\"));\n}\n\n/*\n* make header\n*/\nvoid luaU_header (char* h)\n{\n int x=1;\n memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);\n h+=sizeof(LUA_SIGNATURE)-1;\n *h++=(char)LUAC_VERSION;\n *h++=(char)LUAC_FORMAT;\n *h++=(char)*(char*)&x;\t\t\t\t/* endianness */\n *h++=(char)sizeof(int);\n *h++=(char)sizeof(size_t);\n *h++=(char)sizeof(Instruction);\n *h++=(char)sizeof(lua_Number);\n *h++=(char)(((lua_Number)0.5)==0);\t\t/* is lua_Number integral? */\n}\n"
  },
  {
    "path": "deps/lua/src/lundump.h",
    "content": "/*\n** $Id: lundump.h,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $\n** load precompiled Lua chunks\n** See Copyright Notice in lua.h\n*/\n\n#ifndef lundump_h\n#define lundump_h\n\n#include \"lobject.h\"\n#include \"lzio.h\"\n\n/* load one chunk; from lundump.c */\nLUAI_FUNC Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name);\n\n/* make header; from lundump.c */\nLUAI_FUNC void luaU_header (char* h);\n\n/* dump one chunk; from ldump.c */\nLUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip);\n\n#ifdef luac_c\n/* print one chunk; from print.c */\nLUAI_FUNC void luaU_print (const Proto* f, int full);\n#endif\n\n/* for header of binary files -- this is Lua 5.1 */\n#define LUAC_VERSION\t\t0x51\n\n/* for header of binary files -- this is the official format */\n#define LUAC_FORMAT\t\t0\n\n/* size of header of binary files */\n#define LUAC_HEADERSIZE\t\t12\n\n#endif\n"
  },
  {
    "path": "deps/lua/src/lvm.c",
    "content": "/*\n** $Id: lvm.c,v 2.63.1.5 2011/08/17 20:43:11 roberto Exp $\n** Lua virtual machine\n** See Copyright Notice in lua.h\n*/\n\n\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n#define lvm_c\n#define LUA_CORE\n\n#include \"lua.h\"\n\n#include \"ldebug.h\"\n#include \"ldo.h\"\n#include \"lfunc.h\"\n#include \"lgc.h\"\n#include \"lobject.h\"\n#include \"lopcodes.h\"\n#include \"lstate.h\"\n#include \"lstring.h\"\n#include \"ltable.h\"\n#include \"ltm.h\"\n#include \"lvm.h\"\n\n\n\n/* limit for table tag-method chains (to avoid loops) */\n#define MAXTAGLOOP\t100\n\n\nconst TValue *luaV_tonumber (const TValue *obj, TValue *n) {\n  lua_Number num;\n  if (ttisnumber(obj)) return obj;\n  if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {\n    setnvalue(n, num);\n    return n;\n  }\n  else\n    return NULL;\n}\n\n\nint luaV_tostring (lua_State *L, StkId obj) {\n  if (!ttisnumber(obj))\n    return 0;\n  else {\n    char s[LUAI_MAXNUMBER2STR];\n    lua_Number n = nvalue(obj);\n    lua_number2str(s, n);\n    setsvalue2s(L, obj, luaS_new(L, s));\n    return 1;\n  }\n}\n\n\nstatic void traceexec (lua_State *L, const Instruction *pc) {\n  lu_byte mask = L->hookmask;\n  const Instruction *oldpc = L->savedpc;\n  L->savedpc = pc;\n  if ((mask & LUA_MASKCOUNT) && L->hookcount == 0) {\n    resethookcount(L);\n    luaD_callhook(L, LUA_HOOKCOUNT, -1);\n  }\n  if (mask & LUA_MASKLINE) {\n    Proto *p = ci_func(L->ci)->l.p;\n    int npc = pcRel(pc, p);\n    int newline = getline(p, npc);\n    /* call linehook when enter a new function, when jump back (loop),\n       or when enter a new line */\n    if (npc == 0 || pc <= oldpc || newline != getline(p, pcRel(oldpc, p)))\n      luaD_callhook(L, LUA_HOOKLINE, newline);\n  }\n}\n\n\nstatic void callTMres (lua_State *L, StkId res, const TValue *f,\n                        const TValue *p1, const TValue *p2) {\n  ptrdiff_t result = savestack(L, res);\n  setobj2s(L, L->top, f);  /* push function */\n  setobj2s(L, L->top+1, p1);  /* 1st argument */\n  setobj2s(L, L->top+2, p2);  /* 2nd argument */\n  luaD_checkstack(L, 3);\n  L->top += 3;\n  luaD_call(L, L->top - 3, 1);\n  res = restorestack(L, result);\n  L->top--;\n  setobjs2s(L, res, L->top);\n}\n\n\n\nstatic void callTM (lua_State *L, const TValue *f, const TValue *p1,\n                    const TValue *p2, const TValue *p3) {\n  setobj2s(L, L->top, f);  /* push function */\n  setobj2s(L, L->top+1, p1);  /* 1st argument */\n  setobj2s(L, L->top+2, p2);  /* 2nd argument */\n  setobj2s(L, L->top+3, p3);  /* 3th argument */\n  luaD_checkstack(L, 4);\n  L->top += 4;\n  luaD_call(L, L->top - 4, 0);\n}\n\n\nvoid luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) {\n  int loop;\n  for (loop = 0; loop < MAXTAGLOOP; loop++) {\n    const TValue *tm;\n    if (ttistable(t)) {  /* `t' is a table? */\n      Table *h = hvalue(t);\n      const TValue *res = luaH_get(h, key); /* do a primitive get */\n      if (!ttisnil(res) ||  /* result is no nil? */\n          (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */\n        setobj2s(L, val, res);\n        return;\n      }\n      /* else will try the tag method */\n    }\n    else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))\n      luaG_typeerror(L, t, \"index\");\n    if (ttisfunction(tm)) {\n      callTMres(L, val, tm, t, key);\n      return;\n    }\n    t = tm;  /* else repeat with `tm' */ \n  }\n  luaG_runerror(L, \"loop in gettable\");\n}\n\n\nvoid luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) {\n  int loop;\n  TValue temp;\n  for (loop = 0; loop < MAXTAGLOOP; loop++) {\n    const TValue *tm;\n    if (ttistable(t)) {  /* `t' is a table? */\n      Table *h = hvalue(t);\n      TValue *oldval = luaH_set(L, h, key); /* do a primitive set */\n      if (!ttisnil(oldval) ||  /* result is no nil? */\n          (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */\n        setobj2t(L, oldval, val);\n        h->flags = 0;\n        luaC_barriert(L, h, val);\n        return;\n      }\n      /* else will try the tag method */\n    }\n    else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))\n      luaG_typeerror(L, t, \"index\");\n    if (ttisfunction(tm)) {\n      callTM(L, tm, t, key, val);\n      return;\n    }\n    /* else repeat with `tm' */\n    setobj(L, &temp, tm);  /* avoid pointing inside table (may rehash) */\n    t = &temp;\n  }\n  luaG_runerror(L, \"loop in settable\");\n}\n\n\nstatic int call_binTM (lua_State *L, const TValue *p1, const TValue *p2,\n                       StkId res, TMS event) {\n  const TValue *tm = luaT_gettmbyobj(L, p1, event);  /* try first operand */\n  if (ttisnil(tm))\n    tm = luaT_gettmbyobj(L, p2, event);  /* try second operand */\n  if (ttisnil(tm)) return 0;\n  callTMres(L, res, tm, p1, p2);\n  return 1;\n}\n\n\nstatic const TValue *get_compTM (lua_State *L, Table *mt1, Table *mt2,\n                                  TMS event) {\n  const TValue *tm1 = fasttm(L, mt1, event);\n  const TValue *tm2;\n  if (tm1 == NULL) return NULL;  /* no metamethod */\n  if (mt1 == mt2) return tm1;  /* same metatables => same metamethods */\n  tm2 = fasttm(L, mt2, event);\n  if (tm2 == NULL) return NULL;  /* no metamethod */\n  if (luaO_rawequalObj(tm1, tm2))  /* same metamethods? */\n    return tm1;\n  return NULL;\n}\n\n\nstatic int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2,\n                         TMS event) {\n  const TValue *tm1 = luaT_gettmbyobj(L, p1, event);\n  const TValue *tm2;\n  if (ttisnil(tm1)) return -1;  /* no metamethod? */\n  tm2 = luaT_gettmbyobj(L, p2, event);\n  if (!luaO_rawequalObj(tm1, tm2))  /* different metamethods? */\n    return -1;\n  callTMres(L, L->top, tm1, p1, p2);\n  return !l_isfalse(L->top);\n}\n\n\nstatic int l_strcmp (const TString *ls, const TString *rs) {\n  const char *l = getstr(ls);\n  size_t ll = ls->tsv.len;\n  const char *r = getstr(rs);\n  size_t lr = rs->tsv.len;\n  for (;;) {\n    int temp = strcoll(l, r);\n    if (temp != 0) return temp;\n    else {  /* strings are equal up to a `\\0' */\n      size_t len = strlen(l);  /* index of first `\\0' in both strings */\n      if (len == lr)  /* r is finished? */\n        return (len == ll) ? 0 : 1;\n      else if (len == ll)  /* l is finished? */\n        return -1;  /* l is smaller than r (because r is not finished) */\n      /* both strings longer than `len'; go on comparing (after the `\\0') */\n      len++;\n      l += len; ll -= len; r += len; lr -= len;\n    }\n  }\n}\n\n\nint luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {\n  int res;\n  if (ttype(l) != ttype(r))\n    return luaG_ordererror(L, l, r);\n  else if (ttisnumber(l))\n    return luai_numlt(nvalue(l), nvalue(r));\n  else if (ttisstring(l))\n    return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;\n  else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)\n    return res;\n  return luaG_ordererror(L, l, r);\n}\n\n\nstatic int lessequal (lua_State *L, const TValue *l, const TValue *r) {\n  int res;\n  if (ttype(l) != ttype(r))\n    return luaG_ordererror(L, l, r);\n  else if (ttisnumber(l))\n    return luai_numle(nvalue(l), nvalue(r));\n  else if (ttisstring(l))\n    return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;\n  else if ((res = call_orderTM(L, l, r, TM_LE)) != -1)  /* first try `le' */\n    return res;\n  else if ((res = call_orderTM(L, r, l, TM_LT)) != -1)  /* else try `lt' */\n    return !res;\n  return luaG_ordererror(L, l, r);\n}\n\n\nint luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) {\n  const TValue *tm;\n  lua_assert(ttype(t1) == ttype(t2));\n  switch (ttype(t1)) {\n    case LUA_TNIL: return 1;\n    case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2));\n    case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2);  /* true must be 1 !! */\n    case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2);\n    case LUA_TUSERDATA: {\n      if (uvalue(t1) == uvalue(t2)) return 1;\n      tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable,\n                         TM_EQ);\n      break;  /* will try TM */\n    }\n    case LUA_TTABLE: {\n      if (hvalue(t1) == hvalue(t2)) return 1;\n      tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);\n      break;  /* will try TM */\n    }\n    default: return gcvalue(t1) == gcvalue(t2);\n  }\n  if (tm == NULL) return 0;  /* no TM? */\n  callTMres(L, L->top, tm, t1, t2);  /* call TM */\n  return !l_isfalse(L->top);\n}\n\n\nvoid luaV_concat (lua_State *L, int total, int last) {\n  do {\n    StkId top = L->base + last + 1;\n    int n = 2;  /* number of elements handled in this pass (at least 2) */\n    if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) {\n      if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))\n        luaG_concaterror(L, top-2, top-1);\n    } else if (tsvalue(top-1)->len == 0)  /* second op is empty? */\n      (void)tostring(L, top - 2);  /* result is first op (as string) */\n    else {\n      /* at least two string values; get as many as possible */\n      size_t tl = tsvalue(top-1)->len;\n      char *buffer;\n      int i;\n      /* collect total length */\n      for (n = 1; n < total && tostring(L, top-n-1); n++) {\n        size_t l = tsvalue(top-n-1)->len;\n        if (l >= MAX_SIZET - tl) luaG_runerror(L, \"string length overflow\");\n        tl += l;\n      }\n      buffer = luaZ_openspace(L, &G(L)->buff, tl);\n      tl = 0;\n      for (i=n; i>0; i--) {  /* concat all strings */\n        size_t l = tsvalue(top-i)->len;\n        memcpy(buffer+tl, svalue(top-i), l);\n        tl += l;\n      }\n      setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));\n    }\n    total -= n-1;  /* got `n' strings to create 1 new */\n    last -= n-1;\n  } while (total > 1);  /* repeat until only 1 result left */\n}\n\n\nstatic void Arith (lua_State *L, StkId ra, const TValue *rb,\n                   const TValue *rc, TMS op) {\n  TValue tempb, tempc;\n  const TValue *b, *c;\n  if ((b = luaV_tonumber(rb, &tempb)) != NULL &&\n      (c = luaV_tonumber(rc, &tempc)) != NULL) {\n    lua_Number nb = nvalue(b), nc = nvalue(c);\n    switch (op) {\n      case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break;\n      case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break;\n      case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break;\n      case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break;\n      case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break;\n      case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break;\n      case TM_UNM: setnvalue(ra, luai_numunm(nb)); break;\n      default: lua_assert(0); break;\n    }\n  }\n  else if (!call_binTM(L, rb, rc, ra, op))\n    luaG_aritherror(L, rb, rc);\n}\n\n\n\n/*\n** some macros for common tasks in `luaV_execute'\n*/\n\n#define runtime_check(L, c)\t{ if (!(c)) break; }\n\n#define RA(i)\t(base+GETARG_A(i))\n/* to be used after possible stack reallocation */\n#define RB(i)\tcheck_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))\n#define RC(i)\tcheck_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i))\n#define RKB(i)\tcheck_exp(getBMode(GET_OPCODE(i)) == OpArgK, \\\n\tISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i))\n#define RKC(i)\tcheck_exp(getCMode(GET_OPCODE(i)) == OpArgK, \\\n\tISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i))\n#define KBx(i)\tcheck_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i))\n\n\n#define dojump(L,pc,i)\t{(pc) += (i); luai_threadyield(L);}\n\n\n#define Protect(x)\t{ L->savedpc = pc; {x;}; base = L->base; }\n\n\n#define arith_op(op,tm) { \\\n        TValue *rb = RKB(i); \\\n        TValue *rc = RKC(i); \\\n        if (ttisnumber(rb) && ttisnumber(rc)) { \\\n          lua_Number nb = nvalue(rb), nc = nvalue(rc); \\\n          setnvalue(ra, op(nb, nc)); \\\n        } \\\n        else \\\n          Protect(Arith(L, ra, rb, rc, tm)); \\\n      }\n\n\n\nvoid luaV_execute (lua_State *L, int nexeccalls) {\n  LClosure *cl;\n  StkId base;\n  TValue *k;\n  const Instruction *pc;\n reentry:  /* entry point */\n  lua_assert(isLua(L->ci));\n  pc = L->savedpc;\n  cl = &clvalue(L->ci->func)->l;\n  base = L->base;\n  k = cl->p->k;\n  /* main loop of interpreter */\n  for (;;) {\n    const Instruction i = *pc++;\n    StkId ra;\n    if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&\n        (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {\n      traceexec(L, pc);\n      if (L->status == LUA_YIELD) {  /* did hook yield? */\n        L->savedpc = pc - 1;\n        return;\n      }\n      base = L->base;\n    }\n    /* warning!! several calls may realloc the stack and invalidate `ra' */\n    ra = RA(i);\n    lua_assert(base == L->base && L->base == L->ci->base);\n    lua_assert(base <= L->top && L->top <= L->stack + L->stacksize);\n    lua_assert(L->top == L->ci->top || luaG_checkopenop(i));\n    switch (GET_OPCODE(i)) {\n      case OP_MOVE: {\n        setobjs2s(L, ra, RB(i));\n        continue;\n      }\n      case OP_LOADK: {\n        setobj2s(L, ra, KBx(i));\n        continue;\n      }\n      case OP_LOADBOOL: {\n        setbvalue(ra, GETARG_B(i));\n        if (GETARG_C(i)) pc++;  /* skip next instruction (if C) */\n        continue;\n      }\n      case OP_LOADNIL: {\n        TValue *rb = RB(i);\n        do {\n          setnilvalue(rb--);\n        } while (rb >= ra);\n        continue;\n      }\n      case OP_GETUPVAL: {\n        int b = GETARG_B(i);\n        setobj2s(L, ra, cl->upvals[b]->v);\n        continue;\n      }\n      case OP_GETGLOBAL: {\n        TValue g;\n        TValue *rb = KBx(i);\n        sethvalue(L, &g, cl->env);\n        lua_assert(ttisstring(rb));\n        Protect(luaV_gettable(L, &g, rb, ra));\n        continue;\n      }\n      case OP_GETTABLE: {\n        Protect(luaV_gettable(L, RB(i), RKC(i), ra));\n        continue;\n      }\n      case OP_SETGLOBAL: {\n        TValue g;\n        sethvalue(L, &g, cl->env);\n        lua_assert(ttisstring(KBx(i)));\n        Protect(luaV_settable(L, &g, KBx(i), ra));\n        continue;\n      }\n      case OP_SETUPVAL: {\n        UpVal *uv = cl->upvals[GETARG_B(i)];\n        setobj(L, uv->v, ra);\n        luaC_barrier(L, uv, ra);\n        continue;\n      }\n      case OP_SETTABLE: {\n        Protect(luaV_settable(L, ra, RKB(i), RKC(i)));\n        continue;\n      }\n      case OP_NEWTABLE: {\n        int b = GETARG_B(i);\n        int c = GETARG_C(i);\n        sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c)));\n        Protect(luaC_checkGC(L));\n        continue;\n      }\n      case OP_SELF: {\n        StkId rb = RB(i);\n        setobjs2s(L, ra+1, rb);\n        Protect(luaV_gettable(L, rb, RKC(i), ra));\n        continue;\n      }\n      case OP_ADD: {\n        arith_op(luai_numadd, TM_ADD);\n        continue;\n      }\n      case OP_SUB: {\n        arith_op(luai_numsub, TM_SUB);\n        continue;\n      }\n      case OP_MUL: {\n        arith_op(luai_nummul, TM_MUL);\n        continue;\n      }\n      case OP_DIV: {\n        arith_op(luai_numdiv, TM_DIV);\n        continue;\n      }\n      case OP_MOD: {\n        arith_op(luai_nummod, TM_MOD);\n        continue;\n      }\n      case OP_POW: {\n        arith_op(luai_numpow, TM_POW);\n        continue;\n      }\n      case OP_UNM: {\n        TValue *rb = RB(i);\n        if (ttisnumber(rb)) {\n          lua_Number nb = nvalue(rb);\n          setnvalue(ra, luai_numunm(nb));\n        }\n        else {\n          Protect(Arith(L, ra, rb, rb, TM_UNM));\n        }\n        continue;\n      }\n      case OP_NOT: {\n        int res = l_isfalse(RB(i));  /* next assignment may change this value */\n        setbvalue(ra, res);\n        continue;\n      }\n      case OP_LEN: {\n        const TValue *rb = RB(i);\n        switch (ttype(rb)) {\n          case LUA_TTABLE: {\n            setnvalue(ra, cast_num(luaH_getn(hvalue(rb))));\n            break;\n          }\n          case LUA_TSTRING: {\n            setnvalue(ra, cast_num(tsvalue(rb)->len));\n            break;\n          }\n          default: {  /* try metamethod */\n            Protect(\n              if (!call_binTM(L, rb, luaO_nilobject, ra, TM_LEN))\n                luaG_typeerror(L, rb, \"get length of\");\n            )\n          }\n        }\n        continue;\n      }\n      case OP_CONCAT: {\n        int b = GETARG_B(i);\n        int c = GETARG_C(i);\n        Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L));\n        setobjs2s(L, RA(i), base+b);\n        continue;\n      }\n      case OP_JMP: {\n        dojump(L, pc, GETARG_sBx(i));\n        continue;\n      }\n      case OP_EQ: {\n        TValue *rb = RKB(i);\n        TValue *rc = RKC(i);\n        Protect(\n          if (equalobj(L, rb, rc) == GETARG_A(i))\n            dojump(L, pc, GETARG_sBx(*pc));\n        )\n        pc++;\n        continue;\n      }\n      case OP_LT: {\n        Protect(\n          if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i))\n            dojump(L, pc, GETARG_sBx(*pc));\n        )\n        pc++;\n        continue;\n      }\n      case OP_LE: {\n        Protect(\n          if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i))\n            dojump(L, pc, GETARG_sBx(*pc));\n        )\n        pc++;\n        continue;\n      }\n      case OP_TEST: {\n        if (l_isfalse(ra) != GETARG_C(i))\n          dojump(L, pc, GETARG_sBx(*pc));\n        pc++;\n        continue;\n      }\n      case OP_TESTSET: {\n        TValue *rb = RB(i);\n        if (l_isfalse(rb) != GETARG_C(i)) {\n          setobjs2s(L, ra, rb);\n          dojump(L, pc, GETARG_sBx(*pc));\n        }\n        pc++;\n        continue;\n      }\n      case OP_CALL: {\n        int b = GETARG_B(i);\n        int nresults = GETARG_C(i) - 1;\n        if (b != 0) L->top = ra+b;  /* else previous instruction set top */\n        L->savedpc = pc;\n        switch (luaD_precall(L, ra, nresults)) {\n          case PCRLUA: {\n            nexeccalls++;\n            goto reentry;  /* restart luaV_execute over new Lua function */\n          }\n          case PCRC: {\n            /* it was a C function (`precall' called it); adjust results */\n            if (nresults >= 0) L->top = L->ci->top;\n            base = L->base;\n            continue;\n          }\n          default: {\n            return;  /* yield */\n          }\n        }\n      }\n      case OP_TAILCALL: {\n        int b = GETARG_B(i);\n        if (b != 0) L->top = ra+b;  /* else previous instruction set top */\n        L->savedpc = pc;\n        lua_assert(GETARG_C(i) - 1 == LUA_MULTRET);\n        switch (luaD_precall(L, ra, LUA_MULTRET)) {\n          case PCRLUA: {\n            /* tail call: put new frame in place of previous one */\n            CallInfo *ci = L->ci - 1;  /* previous frame */\n            int aux;\n            StkId func = ci->func;\n            StkId pfunc = (ci+1)->func;  /* previous function index */\n            if (L->openupval) luaF_close(L, ci->base);\n            L->base = ci->base = ci->func + ((ci+1)->base - pfunc);\n            for (aux = 0; pfunc+aux < L->top; aux++)  /* move frame down */\n              setobjs2s(L, func+aux, pfunc+aux);\n            ci->top = L->top = func+aux;  /* correct top */\n            lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize);\n            ci->savedpc = L->savedpc;\n            ci->tailcalls++;  /* one more call lost */\n            L->ci--;  /* remove new frame */\n            goto reentry;\n          }\n          case PCRC: {  /* it was a C function (`precall' called it) */\n            base = L->base;\n            continue;\n          }\n          default: {\n            return;  /* yield */\n          }\n        }\n      }\n      case OP_RETURN: {\n        int b = GETARG_B(i);\n        if (b != 0) L->top = ra+b-1;\n        if (L->openupval) luaF_close(L, base);\n        L->savedpc = pc;\n        b = luaD_poscall(L, ra);\n        if (--nexeccalls == 0)  /* was previous function running `here'? */\n          return;  /* no: return */\n        else {  /* yes: continue its execution */\n          if (b) L->top = L->ci->top;\n          lua_assert(isLua(L->ci));\n          lua_assert(GET_OPCODE(*((L->ci)->savedpc - 1)) == OP_CALL);\n          goto reentry;\n        }\n      }\n      case OP_FORLOOP: {\n        lua_Number step = nvalue(ra+2);\n        lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */\n        lua_Number limit = nvalue(ra+1);\n        if (luai_numlt(0, step) ? luai_numle(idx, limit)\n                                : luai_numle(limit, idx)) {\n          dojump(L, pc, GETARG_sBx(i));  /* jump back */\n          setnvalue(ra, idx);  /* update internal index... */\n          setnvalue(ra+3, idx);  /* ...and external index */\n        }\n        continue;\n      }\n      case OP_FORPREP: {\n        const TValue *init = ra;\n        const TValue *plimit = ra+1;\n        const TValue *pstep = ra+2;\n        L->savedpc = pc;  /* next steps may throw errors */\n        if (!tonumber(init, ra))\n          luaG_runerror(L, LUA_QL(\"for\") \" initial value must be a number\");\n        else if (!tonumber(plimit, ra+1))\n          luaG_runerror(L, LUA_QL(\"for\") \" limit must be a number\");\n        else if (!tonumber(pstep, ra+2))\n          luaG_runerror(L, LUA_QL(\"for\") \" step must be a number\");\n        setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep)));\n        dojump(L, pc, GETARG_sBx(i));\n        continue;\n      }\n      case OP_TFORLOOP: {\n        StkId cb = ra + 3;  /* call base */\n        setobjs2s(L, cb+2, ra+2);\n        setobjs2s(L, cb+1, ra+1);\n        setobjs2s(L, cb, ra);\n        L->top = cb+3;  /* func. + 2 args (state and index) */\n        Protect(luaD_call(L, cb, GETARG_C(i)));\n        L->top = L->ci->top;\n        cb = RA(i) + 3;  /* previous call may change the stack */\n        if (!ttisnil(cb)) {  /* continue loop? */\n          setobjs2s(L, cb-1, cb);  /* save control variable */\n          dojump(L, pc, GETARG_sBx(*pc));  /* jump back */\n        }\n        pc++;\n        continue;\n      }\n      case OP_SETLIST: {\n        int n = GETARG_B(i);\n        int c = GETARG_C(i);\n        int last;\n        Table *h;\n        if (n == 0) {\n          n = cast_int(L->top - ra) - 1;\n          L->top = L->ci->top;\n        }\n        if (c == 0) c = cast_int(*pc++);\n        runtime_check(L, ttistable(ra));\n        h = hvalue(ra);\n        last = ((c-1)*LFIELDS_PER_FLUSH) + n;\n        if (last > h->sizearray)  /* needs more space? */\n          luaH_resizearray(L, h, last);  /* pre-alloc it at once */\n        for (; n > 0; n--) {\n          TValue *val = ra+n;\n          setobj2t(L, luaH_setnum(L, h, last--), val);\n          luaC_barriert(L, h, val);\n        }\n        continue;\n      }\n      case OP_CLOSE: {\n        luaF_close(L, ra);\n        continue;\n      }\n      case OP_CLOSURE: {\n        Proto *p;\n        Closure *ncl;\n        int nup, j;\n        p = cl->p->p[GETARG_Bx(i)];\n        nup = p->nups;\n        ncl = luaF_newLclosure(L, nup, cl->env);\n        ncl->l.p = p;\n        for (j=0; j<nup; j++, pc++) {\n          if (GET_OPCODE(*pc) == OP_GETUPVAL)\n            ncl->l.upvals[j] = cl->upvals[GETARG_B(*pc)];\n          else {\n            lua_assert(GET_OPCODE(*pc) == OP_MOVE);\n            ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc));\n          }\n        }\n        setclvalue(L, ra, ncl);\n        Protect(luaC_checkGC(L));\n        continue;\n      }\n      case OP_VARARG: {\n        int b = GETARG_B(i) - 1;\n        int j;\n        CallInfo *ci = L->ci;\n        int n = cast_int(ci->base - ci->func) - cl->p->numparams - 1;\n        if (b == LUA_MULTRET) {\n          Protect(luaD_checkstack(L, n));\n          ra = RA(i);  /* previous call may change the stack */\n          b = n;\n          L->top = ra + n;\n        }\n        for (j = 0; j < b; j++) {\n          if (j < n) {\n            setobjs2s(L, ra + j, ci->base - n + j);\n          }\n          else {\n            setnilvalue(ra + j);\n          }\n        }\n        continue;\n      }\n    }\n  }\n}\n\n"
  },
  {
    "path": "deps/lua/src/lvm.h",
    "content": "/*\n** $Id: lvm.h,v 2.5.1.1 2007/12/27 13:02:25 roberto Exp $\n** Lua virtual machine\n** See Copyright Notice in lua.h\n*/\n\n#ifndef lvm_h\n#define lvm_h\n\n\n#include \"ldo.h\"\n#include \"lobject.h\"\n#include \"ltm.h\"\n\n\n#define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o)))\n\n#define tonumber(o,n)\t(ttype(o) == LUA_TNUMBER || \\\n                         (((o) = luaV_tonumber(o,n)) != NULL))\n\n#define equalobj(L,o1,o2) \\\n\t(ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2))\n\n\nLUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);\nLUAI_FUNC int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2);\nLUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n);\nLUAI_FUNC int luaV_tostring (lua_State *L, StkId obj);\nLUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key,\n                                            StkId val);\nLUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key,\n                                            StkId val);\nLUAI_FUNC void luaV_execute (lua_State *L, int nexeccalls);\nLUAI_FUNC void luaV_concat (lua_State *L, int total, int last);\n\n#endif\n"
  },
  {
    "path": "deps/lua/src/lzio.c",
    "content": "/*\n** $Id: lzio.c,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $\n** a generic input stream interface\n** See Copyright Notice in lua.h\n*/\n\n\n#include <string.h>\n\n#define lzio_c\n#define LUA_CORE\n\n#include \"lua.h\"\n\n#include \"llimits.h\"\n#include \"lmem.h\"\n#include \"lstate.h\"\n#include \"lzio.h\"\n\n\nint luaZ_fill (ZIO *z) {\n  size_t size;\n  lua_State *L = z->L;\n  const char *buff;\n  lua_unlock(L);\n  buff = z->reader(L, z->data, &size);\n  lua_lock(L);\n  if (buff == NULL || size == 0) return EOZ;\n  z->n = size - 1;\n  z->p = buff;\n  return char2int(*(z->p++));\n}\n\n\nint luaZ_lookahead (ZIO *z) {\n  if (z->n == 0) {\n    if (luaZ_fill(z) == EOZ)\n      return EOZ;\n    else {\n      z->n++;  /* luaZ_fill removed first byte; put back it */\n      z->p--;\n    }\n  }\n  return char2int(*z->p);\n}\n\n\nvoid luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {\n  z->L = L;\n  z->reader = reader;\n  z->data = data;\n  z->n = 0;\n  z->p = NULL;\n}\n\n\n/* --------------------------------------------------------------- read --- */\nsize_t luaZ_read (ZIO *z, void *b, size_t n) {\n  while (n) {\n    size_t m;\n    if (luaZ_lookahead(z) == EOZ)\n      return n;  /* return number of missing bytes */\n    m = (n <= z->n) ? n : z->n;  /* min. between n and z->n */\n    memcpy(b, z->p, m);\n    z->n -= m;\n    z->p += m;\n    b = (char *)b + m;\n    n -= m;\n  }\n  return 0;\n}\n\n/* ------------------------------------------------------------------------ */\nchar *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) {\n  if (n > buff->buffsize) {\n    if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;\n    luaZ_resizebuffer(L, buff, n);\n  }\n  return buff->buffer;\n}\n\n\n"
  },
  {
    "path": "deps/lua/src/lzio.h",
    "content": "/*\n** $Id: lzio.h,v 1.21.1.1 2007/12/27 13:02:25 roberto Exp $\n** Buffered streams\n** See Copyright Notice in lua.h\n*/\n\n\n#ifndef lzio_h\n#define lzio_h\n\n#include \"lua.h\"\n\n#include \"lmem.h\"\n\n\n#define EOZ\t(-1)\t\t\t/* end of stream */\n\ntypedef struct Zio ZIO;\n\n#define char2int(c)\tcast(int, cast(unsigned char, (c)))\n\n#define zgetc(z)  (((z)->n--)>0 ?  char2int(*(z)->p++) : luaZ_fill(z))\n\ntypedef struct Mbuffer {\n  char *buffer;\n  size_t n;\n  size_t buffsize;\n} Mbuffer;\n\n#define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0)\n\n#define luaZ_buffer(buff)\t((buff)->buffer)\n#define luaZ_sizebuffer(buff)\t((buff)->buffsize)\n#define luaZ_bufflen(buff)\t((buff)->n)\n\n#define luaZ_resetbuffer(buff) ((buff)->n = 0)\n\n\n#define luaZ_resizebuffer(L, buff, size) \\\n\t(luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \\\n\t(buff)->buffsize = size)\n\n#define luaZ_freebuffer(L, buff)\tluaZ_resizebuffer(L, buff, 0)\n\n\nLUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n);\nLUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader,\n                                        void *data);\nLUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n);\t/* read next n bytes */\nLUAI_FUNC int luaZ_lookahead (ZIO *z);\n\n\n\n/* --------- Private Part ------------------ */\n\nstruct Zio {\n  size_t n;\t\t\t/* bytes still unread */\n  const char *p;\t\t/* current position in buffer */\n  lua_Reader reader;\n  void* data;\t\t\t/* additional data */\n  lua_State *L;\t\t\t/* Lua state (for reader) */\n};\n\n\nLUAI_FUNC int luaZ_fill (ZIO *z);\n\n#endif\n"
  },
  {
    "path": "deps/lua/src/print.c",
    "content": "/*\n** $Id: print.c,v 1.55a 2006/05/31 13:30:05 lhf Exp $\n** print bytecodes\n** See Copyright Notice in lua.h\n*/\n\n#include <ctype.h>\n#include <stdio.h>\n\n#define luac_c\n#define LUA_CORE\n\n#include \"ldebug.h\"\n#include \"lobject.h\"\n#include \"lopcodes.h\"\n#include \"lundump.h\"\n\n#define PrintFunction\tluaU_print\n\n#define Sizeof(x)\t((int)sizeof(x))\n#define VOID(p)\t\t((const void*)(p))\n\nstatic void PrintString(const TString* ts)\n{\n const char* s=getstr(ts);\n size_t i,n=ts->tsv.len;\n putchar('\"');\n for (i=0; i<n; i++)\n {\n  int c=s[i];\n  switch (c)\n  {\n   case '\"': printf(\"\\\\\\\"\"); break;\n   case '\\\\': printf(\"\\\\\\\\\"); break;\n   case '\\a': printf(\"\\\\a\"); break;\n   case '\\b': printf(\"\\\\b\"); break;\n   case '\\f': printf(\"\\\\f\"); break;\n   case '\\n': printf(\"\\\\n\"); break;\n   case '\\r': printf(\"\\\\r\"); break;\n   case '\\t': printf(\"\\\\t\"); break;\n   case '\\v': printf(\"\\\\v\"); break;\n   default:\tif (isprint((unsigned char)c))\n   \t\t\tputchar(c);\n\t\telse\n\t\t\tprintf(\"\\\\%03u\",(unsigned char)c);\n  }\n }\n putchar('\"');\n}\n\nstatic void PrintConstant(const Proto* f, int i)\n{\n const TValue* o=&f->k[i];\n switch (ttype(o))\n {\n  case LUA_TNIL:\n\tprintf(\"nil\");\n\tbreak;\n  case LUA_TBOOLEAN:\n\tprintf(bvalue(o) ? \"true\" : \"false\");\n\tbreak;\n  case LUA_TNUMBER:\n\tprintf(LUA_NUMBER_FMT,nvalue(o));\n\tbreak;\n  case LUA_TSTRING:\n\tPrintString(rawtsvalue(o));\n\tbreak;\n  default:\t\t\t\t/* cannot happen */\n\tprintf(\"? type=%d\",ttype(o));\n\tbreak;\n }\n}\n\nstatic void PrintCode(const Proto* f)\n{\n const Instruction* code=f->code;\n int pc,n=f->sizecode;\n for (pc=0; pc<n; pc++)\n {\n  Instruction i=code[pc];\n  OpCode o=GET_OPCODE(i);\n  int a=GETARG_A(i);\n  int b=GETARG_B(i);\n  int c=GETARG_C(i);\n  int bx=GETARG_Bx(i);\n  int sbx=GETARG_sBx(i);\n  int line=getline(f,pc);\n  printf(\"\\t%d\\t\",pc+1);\n  if (line>0) printf(\"[%d]\\t\",line); else printf(\"[-]\\t\");\n  printf(\"%-9s\\t\",luaP_opnames[o]);\n  switch (getOpMode(o))\n  {\n   case iABC:\n    printf(\"%d\",a);\n    if (getBMode(o)!=OpArgN) printf(\" %d\",ISK(b) ? (-1-INDEXK(b)) : b);\n    if (getCMode(o)!=OpArgN) printf(\" %d\",ISK(c) ? (-1-INDEXK(c)) : c);\n    break;\n   case iABx:\n    if (getBMode(o)==OpArgK) printf(\"%d %d\",a,-1-bx); else printf(\"%d %d\",a,bx);\n    break;\n   case iAsBx:\n    if (o==OP_JMP) printf(\"%d\",sbx); else printf(\"%d %d\",a,sbx);\n    break;\n  }\n  switch (o)\n  {\n   case OP_LOADK:\n    printf(\"\\t; \"); PrintConstant(f,bx);\n    break;\n   case OP_GETUPVAL:\n   case OP_SETUPVAL:\n    printf(\"\\t; %s\", (f->sizeupvalues>0) ? getstr(f->upvalues[b]) : \"-\");\n    break;\n   case OP_GETGLOBAL:\n   case OP_SETGLOBAL:\n    printf(\"\\t; %s\",svalue(&f->k[bx]));\n    break;\n   case OP_GETTABLE:\n   case OP_SELF:\n    if (ISK(c)) { printf(\"\\t; \"); PrintConstant(f,INDEXK(c)); }\n    break;\n   case OP_SETTABLE:\n   case OP_ADD:\n   case OP_SUB:\n   case OP_MUL:\n   case OP_DIV:\n   case OP_POW:\n   case OP_EQ:\n   case OP_LT:\n   case OP_LE:\n    if (ISK(b) || ISK(c))\n    {\n     printf(\"\\t; \");\n     if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf(\"-\");\n     printf(\" \");\n     if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf(\"-\");\n    }\n    break;\n   case OP_JMP:\n   case OP_FORLOOP:\n   case OP_FORPREP:\n    printf(\"\\t; to %d\",sbx+pc+2);\n    break;\n   case OP_CLOSURE:\n    printf(\"\\t; %p\",VOID(f->p[bx]));\n    break;\n   case OP_SETLIST:\n    if (c==0) printf(\"\\t; %d\",(int)code[++pc]);\n    else printf(\"\\t; %d\",c);\n    break;\n   default:\n    break;\n  }\n  printf(\"\\n\");\n }\n}\n\n#define SS(x)\t(x==1)?\"\":\"s\"\n#define S(x)\tx,SS(x)\n\nstatic void PrintHeader(const Proto* f)\n{\n const char* s=getstr(f->source);\n if (*s=='@' || *s=='=')\n  s++;\n else if (*s==LUA_SIGNATURE[0])\n  s=\"(bstring)\";\n else\n  s=\"(string)\";\n printf(\"\\n%s <%s:%d,%d> (%d instruction%s, %d bytes at %p)\\n\",\n \t(f->linedefined==0)?\"main\":\"function\",s,\n\tf->linedefined,f->lastlinedefined,\n\tS(f->sizecode),f->sizecode*Sizeof(Instruction),VOID(f));\n printf(\"%d%s param%s, %d slot%s, %d upvalue%s, \",\n\tf->numparams,f->is_vararg?\"+\":\"\",SS(f->numparams),\n\tS(f->maxstacksize),S(f->nups));\n printf(\"%d local%s, %d constant%s, %d function%s\\n\",\n\tS(f->sizelocvars),S(f->sizek),S(f->sizep));\n}\n\nstatic void PrintConstants(const Proto* f)\n{\n int i,n=f->sizek;\n printf(\"constants (%d) for %p:\\n\",n,VOID(f));\n for (i=0; i<n; i++)\n {\n  printf(\"\\t%d\\t\",i+1);\n  PrintConstant(f,i);\n  printf(\"\\n\");\n }\n}\n\nstatic void PrintLocals(const Proto* f)\n{\n int i,n=f->sizelocvars;\n printf(\"locals (%d) for %p:\\n\",n,VOID(f));\n for (i=0; i<n; i++)\n {\n  printf(\"\\t%d\\t%s\\t%d\\t%d\\n\",\n  i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1);\n }\n}\n\nstatic void PrintUpvalues(const Proto* f)\n{\n int i,n=f->sizeupvalues;\n printf(\"upvalues (%d) for %p:\\n\",n,VOID(f));\n if (f->upvalues==NULL) return;\n for (i=0; i<n; i++)\n {\n  printf(\"\\t%d\\t%s\\n\",i,getstr(f->upvalues[i]));\n }\n}\n\nvoid PrintFunction(const Proto* f, int full)\n{\n int i,n=f->sizep;\n PrintHeader(f);\n PrintCode(f);\n if (full)\n {\n  PrintConstants(f);\n  PrintLocals(f);\n  PrintUpvalues(f);\n }\n for (i=0; i<n; i++) PrintFunction(f->p[i],full);\n}\n"
  },
  {
    "path": "deps/lua/src/strbuf.c",
    "content": "/* strbuf - String buffer routines\n *\n * Copyright (c) 2010-2012  Mark Pulford <mark@kyne.com.au>\n *\n * Permission is hereby granted, free of charge, to any person obtaining\n * a copy of this software and associated documentation files (the\n * \"Software\"), to deal in the Software without restriction, including\n * without limitation the rights to use, copy, modify, merge, publish,\n * distribute, sublicense, and/or sell copies of the Software, and to\n * permit persons to whom the Software is furnished to do so, subject to\n * the following conditions:\n *\n * The above copyright notice and this permission notice shall be\n * included in all copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\n * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\n * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\n * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\n * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n */\n\n#include <stdio.h>\n#include <stdlib.h>\n#include <stdarg.h>\n#include <string.h>\n\n#include \"strbuf.h\"\n\nstatic void die(const char *fmt, ...)\n{\n    va_list arg;\n\n    va_start(arg, fmt);\n    vfprintf(stderr, fmt, arg);\n    va_end(arg);\n    fprintf(stderr, \"\\n\");\n\n    exit(-1);\n}\n\nvoid strbuf_init(strbuf_t *s, int len)\n{\n    int size;\n\n    if (len <= 0)\n        size = STRBUF_DEFAULT_SIZE;\n    else\n        size = len + 1;         /* \\0 terminator */\n\n    s->buf = NULL;\n    s->size = size;\n    s->length = 0;\n    s->increment = STRBUF_DEFAULT_INCREMENT;\n    s->dynamic = 0;\n    s->reallocs = 0;\n    s->debug = 0;\n\n    s->buf = malloc(size);\n    if (!s->buf)\n        die(\"Out of memory\");\n\n    strbuf_ensure_null(s);\n}\n\nstrbuf_t *strbuf_new(int len)\n{\n    strbuf_t *s;\n\n    s = malloc(sizeof(strbuf_t));\n    if (!s)\n        die(\"Out of memory\");\n\n    strbuf_init(s, len);\n\n    /* Dynamic strbuf allocation / deallocation */\n    s->dynamic = 1;\n\n    return s;\n}\n\nvoid strbuf_set_increment(strbuf_t *s, int increment)\n{\n    /* Increment > 0:  Linear buffer growth rate\n     * Increment < -1: Exponential buffer growth rate */\n    if (increment == 0 || increment == -1)\n        die(\"BUG: Invalid string increment\");\n\n    s->increment = increment;\n}\n\nstatic inline void debug_stats(strbuf_t *s)\n{\n    if (s->debug) {\n        fprintf(stderr, \"strbuf(%lx) reallocs: %d, length: %d, size: %d\\n\",\n                (long)s, s->reallocs, s->length, s->size);\n    }\n}\n\n/* If strbuf_t has not been dynamically allocated, strbuf_free() can\n * be called any number of times strbuf_init() */\nvoid strbuf_free(strbuf_t *s)\n{\n    debug_stats(s);\n\n    if (s->buf) {\n        free(s->buf);\n        s->buf = NULL;\n    }\n    if (s->dynamic)\n        free(s);\n}\n\nchar *strbuf_free_to_string(strbuf_t *s, int *len)\n{\n    char *buf;\n\n    debug_stats(s);\n\n    strbuf_ensure_null(s);\n\n    buf = s->buf;\n    if (len)\n        *len = s->length;\n\n    if (s->dynamic)\n        free(s);\n\n    return buf;\n}\n\nstatic int calculate_new_size(strbuf_t *s, int len)\n{\n    int reqsize, newsize;\n\n    if (len <= 0)\n        die(\"BUG: Invalid strbuf length requested\");\n\n    /* Ensure there is room for optional NULL termination */\n    reqsize = len + 1;\n\n    /* If the user has requested to shrink the buffer, do it exactly */\n    if (s->size > reqsize)\n        return reqsize;\n\n    newsize = s->size;\n    if (s->increment < 0) {\n        /* Exponential sizing */\n        while (newsize < reqsize)\n            newsize *= -s->increment;\n    } else {\n        /* Linear sizing */\n        newsize = ((newsize + s->increment - 1) / s->increment) * s->increment;\n    }\n\n    return newsize;\n}\n\n\n/* Ensure strbuf can handle a string length bytes long (ignoring NULL\n * optional termination). */\nvoid strbuf_resize(strbuf_t *s, int len)\n{\n    int newsize;\n\n    newsize = calculate_new_size(s, len);\n\n    if (s->debug > 1) {\n        fprintf(stderr, \"strbuf(%lx) resize: %d => %d\\n\",\n                (long)s, s->size, newsize);\n    }\n\n    s->size = newsize;\n    s->buf = realloc(s->buf, s->size);\n    if (!s->buf)\n        die(\"Out of memory\");\n    s->reallocs++;\n}\n\nvoid strbuf_append_string(strbuf_t *s, const char *str)\n{\n    int space, i;\n\n    space = strbuf_empty_length(s);\n\n    for (i = 0; str[i]; i++) {\n        if (space < 1) {\n            strbuf_resize(s, s->length + 1);\n            space = strbuf_empty_length(s);\n        }\n\n        s->buf[s->length] = str[i];\n        s->length++;\n        space--;\n    }\n}\n\n/* strbuf_append_fmt() should only be used when an upper bound\n * is known for the output string. */\nvoid strbuf_append_fmt(strbuf_t *s, int len, const char *fmt, ...)\n{\n    va_list arg;\n    int fmt_len;\n\n    strbuf_ensure_empty_length(s, len);\n\n    va_start(arg, fmt);\n    fmt_len = vsnprintf(s->buf + s->length, len, fmt, arg);\n    va_end(arg);\n\n    if (fmt_len < 0)\n        die(\"BUG: Unable to convert number\");  /* This should never happen.. */\n\n    s->length += fmt_len;\n}\n\n/* strbuf_append_fmt_retry() can be used when the there is no known\n * upper bound for the output string. */\nvoid strbuf_append_fmt_retry(strbuf_t *s, const char *fmt, ...)\n{\n    va_list arg;\n    int fmt_len, try;\n    int empty_len;\n\n    /* If the first attempt to append fails, resize the buffer appropriately\n     * and try again */\n    for (try = 0; ; try++) {\n        va_start(arg, fmt);\n        /* Append the new formatted string */\n        /* fmt_len is the length of the string required, excluding the\n         * trailing NULL */\n        empty_len = strbuf_empty_length(s);\n        /* Add 1 since there is also space to store the terminating NULL. */\n        fmt_len = vsnprintf(s->buf + s->length, empty_len + 1, fmt, arg);\n        va_end(arg);\n\n        if (fmt_len <= empty_len)\n            break;  /* SUCCESS */\n        if (try > 0)\n            die(\"BUG: length of formatted string changed\");\n\n        strbuf_resize(s, s->length + fmt_len);\n    }\n\n    s->length += fmt_len;\n}\n\n/* vi:ai et sw=4 ts=4:\n */\n"
  },
  {
    "path": "deps/lua/src/strbuf.h",
    "content": "/* strbuf - String buffer routines\n *\n * Copyright (c) 2010-2012  Mark Pulford <mark@kyne.com.au>\n *\n * Permission is hereby granted, free of charge, to any person obtaining\n * a copy of this software and associated documentation files (the\n * \"Software\"), to deal in the Software without restriction, including\n * without limitation the rights to use, copy, modify, merge, publish,\n * distribute, sublicense, and/or sell copies of the Software, and to\n * permit persons to whom the Software is furnished to do so, subject to\n * the following conditions:\n *\n * The above copyright notice and this permission notice shall be\n * included in all copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\n * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\n * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\n * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\n * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n */\n\n#include <stdlib.h>\n#include <stdarg.h>\n\n/* Size: Total bytes allocated to *buf\n * Length: String length, excluding optional NULL terminator.\n * Increment: Allocation increments when resizing the string buffer.\n * Dynamic: True if created via strbuf_new()\n */\n\ntypedef struct {\n    char *buf;\n    int size;\n    int length;\n    int increment;\n    int dynamic;\n    int reallocs;\n    int debug;\n} strbuf_t;\n\n#ifndef STRBUF_DEFAULT_SIZE\n#define STRBUF_DEFAULT_SIZE 1023\n#endif\n#ifndef STRBUF_DEFAULT_INCREMENT\n#define STRBUF_DEFAULT_INCREMENT -2\n#endif\n\n/* Initialise */\nextern strbuf_t *strbuf_new(int len);\nextern void strbuf_init(strbuf_t *s, int len);\nextern void strbuf_set_increment(strbuf_t *s, int increment);\n\n/* Release */\nextern void strbuf_free(strbuf_t *s);\nextern char *strbuf_free_to_string(strbuf_t *s, int *len);\n\n/* Management */\nextern void strbuf_resize(strbuf_t *s, int len);\nstatic int strbuf_empty_length(strbuf_t *s);\nstatic int strbuf_length(strbuf_t *s);\nstatic char *strbuf_string(strbuf_t *s, int *len);\nstatic void strbuf_ensure_empty_length(strbuf_t *s, int len);\nstatic char *strbuf_empty_ptr(strbuf_t *s);\nstatic void strbuf_extend_length(strbuf_t *s, int len);\n\n/* Update */\nextern void strbuf_append_fmt(strbuf_t *s, int len, const char *fmt, ...);\nextern void strbuf_append_fmt_retry(strbuf_t *s, const char *format, ...);\nstatic void strbuf_append_mem(strbuf_t *s, const char *c, int len);\nextern void strbuf_append_string(strbuf_t *s, const char *str);\nstatic void strbuf_append_char(strbuf_t *s, const char c);\nstatic void strbuf_ensure_null(strbuf_t *s);\n\n/* Reset string for before use */\nstatic inline void strbuf_reset(strbuf_t *s)\n{\n    s->length = 0;\n}\n\nstatic inline int strbuf_allocated(strbuf_t *s)\n{\n    return s->buf != NULL;\n}\n\n/* Return bytes remaining in the string buffer\n * Ensure there is space for a NULL terminator. */\nstatic inline int strbuf_empty_length(strbuf_t *s)\n{\n    return s->size - s->length - 1;\n}\n\nstatic inline void strbuf_ensure_empty_length(strbuf_t *s, int len)\n{\n    if (len > strbuf_empty_length(s))\n        strbuf_resize(s, s->length + len);\n}\n\nstatic inline char *strbuf_empty_ptr(strbuf_t *s)\n{\n    return s->buf + s->length;\n}\n\nstatic inline void strbuf_extend_length(strbuf_t *s, int len)\n{\n    s->length += len;\n}\n\nstatic inline int strbuf_length(strbuf_t *s)\n{\n    return s->length;\n}\n\nstatic inline void strbuf_append_char(strbuf_t *s, const char c)\n{\n    strbuf_ensure_empty_length(s, 1);\n    s->buf[s->length++] = c;\n}\n\nstatic inline void strbuf_append_char_unsafe(strbuf_t *s, const char c)\n{\n    s->buf[s->length++] = c;\n}\n\nstatic inline void strbuf_append_mem(strbuf_t *s, const char *c, int len)\n{\n    strbuf_ensure_empty_length(s, len);\n    memcpy(s->buf + s->length, c, len);\n    s->length += len;\n}\n\nstatic inline void strbuf_append_mem_unsafe(strbuf_t *s, const char *c, int len)\n{\n    memcpy(s->buf + s->length, c, len);\n    s->length += len;\n}\n\nstatic inline void strbuf_ensure_null(strbuf_t *s)\n{\n    s->buf[s->length] = 0;\n}\n\nstatic inline char *strbuf_string(strbuf_t *s, int *len)\n{\n    if (len)\n        *len = s->length;\n\n    return s->buf;\n}\n\n/* vi:ai et sw=4 ts=4:\n */\n"
  },
  {
    "path": "deps/lua/test/README",
    "content": "These are simple tests for Lua.  Some of them contain useful code.\nThey are meant to be run to make sure Lua is built correctly and also\nto be read, to see how Lua programs look.\n\nHere is a one-line summary of each program:\n\n   bisect.lua\t\tbisection method for solving non-linear equations\n   cf.lua\t\ttemperature conversion table (celsius to farenheit)\n   echo.lua             echo command line arguments\n   env.lua              environment variables as automatic global variables\n   factorial.lua\tfactorial without recursion\n   fib.lua\t\tfibonacci function with cache\n   fibfor.lua\t\tfibonacci numbers with coroutines and generators\n   globals.lua\t\treport global variable usage\n   hello.lua\t\tthe first program in every language\n   life.lua\t\tConway's Game of Life\n   luac.lua\t \tbare-bones luac\n   printf.lua\t\tan implementation of printf\n   readonly.lua\t\tmake global variables readonly\n   sieve.lua\t\tthe sieve of of Eratosthenes programmed with coroutines\n   sort.lua\t\ttwo implementations of a sort function\n   table.lua\t\tmake table, grouping all data for the same item\n   trace-calls.lua\ttrace calls\n   trace-globals.lua\ttrace assigments to global variables\n   xd.lua\t\thex dump\n\n"
  },
  {
    "path": "deps/lua/test/bisect.lua",
    "content": "-- bisection method for solving non-linear equations\n\ndelta=1e-6\t-- tolerance\n\nfunction bisect(f,a,b,fa,fb)\n local c=(a+b)/2\n io.write(n,\" c=\",c,\" a=\",a,\" b=\",b,\"\\n\")\n if c==a or c==b or math.abs(a-b)<delta then return c,b-a end\n n=n+1\n local fc=f(c)\n if fa*fc<0 then return bisect(f,a,c,fa,fc) else return bisect(f,c,b,fc,fb) end\nend\n\n-- find root of f in the inverval [a,b]. needs f(a)*f(b)<0\nfunction solve(f,a,b)\n n=0\n local z,e=bisect(f,a,b,f(a),f(b))\n io.write(string.format(\"after %d steps, root is %.17g with error %.1e, f=%.1e\\n\",n,z,e,f(z)))\nend\n\n-- our function\nfunction f(x)\n return x*x*x-x-1\nend\n\n-- find zero in [1,2]\nsolve(f,1,2)\n"
  },
  {
    "path": "deps/lua/test/cf.lua",
    "content": "-- temperature conversion table (celsius to farenheit)\n\nfor c0=-20,50-1,10 do\n\tio.write(\"C \")\n\tfor c=c0,c0+10-1 do\n\t\tio.write(string.format(\"%3.0f \",c))\n\tend\n\tio.write(\"\\n\")\n\t\n\tio.write(\"F \")\n\tfor c=c0,c0+10-1 do\n\t\tf=(9/5)*c+32\n\t\tio.write(string.format(\"%3.0f \",f))\n\tend\n\tio.write(\"\\n\\n\")\nend\n"
  },
  {
    "path": "deps/lua/test/echo.lua",
    "content": "-- echo command line arguments\n\nfor i=0,table.getn(arg) do\n print(i,arg[i])\nend\n"
  },
  {
    "path": "deps/lua/test/env.lua",
    "content": "-- read environment variables as if they were global variables\n\nlocal f=function (t,i) return os.getenv(i) end\nsetmetatable(getfenv(),{__index=f})\n\n-- an example\nprint(a,USER,PATH)\n"
  },
  {
    "path": "deps/lua/test/factorial.lua",
    "content": "-- function closures are powerful\n\n-- traditional fixed-point operator from functional programming\nY = function (g)\n      local a = function (f) return f(f) end\n      return a(function (f)\n                 return g(function (x)\n                             local c=f(f)\n                             return c(x)\n                           end)\n               end)\nend\n\n\n-- factorial without recursion\nF = function (f)\n      return function (n)\n               if n == 0 then return 1\n               else return n*f(n-1) end\n             end\n    end\n\nfactorial = Y(F)   -- factorial is the fixed point of F\n\n-- now test it\nfunction test(x)\n\tio.write(x,\"! = \",factorial(x),\"\\n\")\nend\n\nfor n=0,16 do\n\ttest(n)\nend\n"
  },
  {
    "path": "deps/lua/test/fib.lua",
    "content": "-- fibonacci function with cache\n\n-- very inefficient fibonacci function\nfunction fib(n)\n\tN=N+1\n\tif n<2 then\n\t\treturn n\n\telse\n\t\treturn fib(n-1)+fib(n-2)\n\tend\nend\n\n-- a general-purpose value cache\nfunction cache(f)\n\tlocal c={}\n\treturn function (x)\n\t\tlocal y=c[x]\n\t\tif not y then\n\t\t\ty=f(x)\n\t\t\tc[x]=y\n\t\tend\n\t\treturn y\n\tend\nend\n\n-- run and time it\nfunction test(s,f)\n\tN=0\n\tlocal c=os.clock()\n\tlocal v=f(n)\n\tlocal t=os.clock()-c\n\tprint(s,n,v,t,N)\nend\n\nn=arg[1] or 24\t\t-- for other values, do lua fib.lua XX\nn=tonumber(n)\nprint(\"\",\"n\",\"value\",\"time\",\"evals\")\ntest(\"plain\",fib)\nfib=cache(fib)\ntest(\"cached\",fib)\n"
  },
  {
    "path": "deps/lua/test/fibfor.lua",
    "content": "-- example of for with generator functions\n\nfunction generatefib (n)\n  return coroutine.wrap(function ()\n    local a,b = 1, 1\n    while a <= n do\n      coroutine.yield(a)\n      a, b = b, a+b\n    end\n  end)\nend\n\nfor i in generatefib(1000) do print(i) end\n"
  },
  {
    "path": "deps/lua/test/globals.lua",
    "content": "-- reads luac listings and reports global variable usage\n-- lines where a global is written to are marked with \"*\"\n-- typical usage: luac -p -l file.lua | lua globals.lua | sort | lua table.lua\n\nwhile 1 do\n local s=io.read()\n if s==nil then break end\n local ok,_,l,op,g=string.find(s,\"%[%-?(%d*)%]%s*([GS])ETGLOBAL.-;%s+(.*)$\")\n if ok then\n  if op==\"S\" then op=\"*\" else op=\"\" end\n  io.write(g,\"\\t\",l,op,\"\\n\")\n end\nend\n"
  },
  {
    "path": "deps/lua/test/hello.lua",
    "content": "-- the first program in every language\n\nio.write(\"Hello world, from \",_VERSION,\"!\\n\")\n"
  },
  {
    "path": "deps/lua/test/life.lua",
    "content": "-- life.lua\n-- original by Dave Bollinger <DBollinger@compuserve.com> posted to lua-l\n-- modified to use ANSI terminal escape sequences\n-- modified to use for instead of while\n\nlocal write=io.write\n\nALIVE=\"\"\tDEAD=\"\"\nALIVE=\"O\"\tDEAD=\"-\"\n\nfunction delay() -- NOTE: SYSTEM-DEPENDENT, adjust as necessary\n  for i=1,10000 do end\n  -- local i=os.clock()+1 while(os.clock()<i) do end\nend\n\nfunction ARRAY2D(w,h)\n  local t = {w=w,h=h}\n  for y=1,h do\n    t[y] = {}\n    for x=1,w do\n      t[y][x]=0\n    end\n  end\n  return t\nend\n\n_CELLS = {}\n\n-- give birth to a \"shape\" within the cell array\nfunction _CELLS:spawn(shape,left,top)\n  for y=0,shape.h-1 do\n    for x=0,shape.w-1 do\n      self[top+y][left+x] = shape[y*shape.w+x+1]\n    end\n  end\nend\n\n-- run the CA and produce the next generation\nfunction _CELLS:evolve(next)\n  local ym1,y,yp1,yi=self.h-1,self.h,1,self.h\n  while yi > 0 do\n    local xm1,x,xp1,xi=self.w-1,self.w,1,self.w\n    while xi > 0 do\n      local sum = self[ym1][xm1] + self[ym1][x] + self[ym1][xp1] +\n                  self[y][xm1] + self[y][xp1] +\n                  self[yp1][xm1] + self[yp1][x] + self[yp1][xp1]\n      next[y][x] = ((sum==2) and self[y][x]) or ((sum==3) and 1) or 0\n      xm1,x,xp1,xi = x,xp1,xp1+1,xi-1\n    end\n    ym1,y,yp1,yi = y,yp1,yp1+1,yi-1\n  end\nend\n\n-- output the array to screen\nfunction _CELLS:draw()\n  local out=\"\" -- accumulate to reduce flicker\n  for y=1,self.h do\n   for x=1,self.w do\n      out=out..(((self[y][x]>0) and ALIVE) or DEAD)\n    end\n    out=out..\"\\n\"\n  end\n  write(out)\nend\n\n-- constructor\nfunction CELLS(w,h)\n  local c = ARRAY2D(w,h)\n  c.spawn = _CELLS.spawn\n  c.evolve = _CELLS.evolve\n  c.draw = _CELLS.draw\n  return c\nend\n\n--\n-- shapes suitable for use with spawn() above\n--\nHEART = { 1,0,1,1,0,1,1,1,1; w=3,h=3 }\nGLIDER = { 0,0,1,1,0,1,0,1,1; w=3,h=3 }\nEXPLODE = { 0,1,0,1,1,1,1,0,1,0,1,0; w=3,h=4 }\nFISH = { 0,1,1,1,1,1,0,0,0,1,0,0,0,0,1,1,0,0,1,0; w=5,h=4 }\nBUTTERFLY = { 1,0,0,0,1,0,1,1,1,0,1,0,0,0,1,1,0,1,0,1,1,0,0,0,1; w=5,h=5 }\n\n-- the main routine\nfunction LIFE(w,h)\n  -- create two arrays\n  local thisgen = CELLS(w,h)\n  local nextgen = CELLS(w,h)\n\n  -- create some life\n  -- about 1000 generations of fun, then a glider steady-state\n  thisgen:spawn(GLIDER,5,4)\n  thisgen:spawn(EXPLODE,25,10)\n  thisgen:spawn(FISH,4,12)\n\n  -- run until break\n  local gen=1\n  write(\"\\027[2J\")\t-- ANSI clear screen\n  while 1 do\n    thisgen:evolve(nextgen)\n    thisgen,nextgen = nextgen,thisgen\n    write(\"\\027[H\")\t-- ANSI home cursor\n    thisgen:draw()\n    write(\"Life - generation \",gen,\"\\n\")\n    gen=gen+1\n    if gen>2000 then break end\n    --delay()\t\t-- no delay\n  end\nend\n\nLIFE(40,20)\n"
  },
  {
    "path": "deps/lua/test/luac.lua",
    "content": "-- bare-bones luac in Lua\n-- usage: lua luac.lua file.lua\n\nassert(arg[1]~=nil and arg[2]==nil,\"usage: lua luac.lua file.lua\")\nf=assert(io.open(\"luac.out\",\"wb\"))\nassert(f:write(string.dump(assert(loadfile(arg[1])))))\nassert(f:close())\n"
  },
  {
    "path": "deps/lua/test/printf.lua",
    "content": "-- an implementation of printf\n\nfunction printf(...)\n io.write(string.format(...))\nend\n\nprintf(\"Hello %s from %s on %s\\n\",os.getenv\"USER\" or \"there\",_VERSION,os.date())\n"
  },
  {
    "path": "deps/lua/test/readonly.lua",
    "content": "-- make global variables readonly\n\nlocal f=function (t,i) error(\"cannot redefine global variable `\"..i..\"'\",2) end\nlocal g={}\nlocal G=getfenv()\nsetmetatable(g,{__index=G,__newindex=f})\nsetfenv(1,g)\n\n-- an example\nrawset(g,\"x\",3)\nx=2\ny=1\t-- cannot redefine `y'\n"
  },
  {
    "path": "deps/lua/test/sieve.lua",
    "content": "-- the sieve of of Eratosthenes programmed with coroutines\n-- typical usage: lua -e N=1000 sieve.lua | column\n\n-- generate all the numbers from 2 to n\nfunction gen (n)\n  return coroutine.wrap(function ()\n    for i=2,n do coroutine.yield(i) end\n  end)\nend\n\n-- filter the numbers generated by `g', removing multiples of `p'\nfunction filter (p, g)\n  return coroutine.wrap(function ()\n    while 1 do\n      local n = g()\n      if n == nil then return end\n      if math.mod(n, p) ~= 0 then coroutine.yield(n) end\n    end\n  end)\nend\n\nN=N or 1000\t\t-- from command line\nx = gen(N)\t\t-- generate primes up to N\nwhile 1 do\n  local n = x()\t\t-- pick a number until done\n  if n == nil then break end\n  print(n)\t\t-- must be a prime number\n  x = filter(n, x)\t-- now remove its multiples\nend\n"
  },
  {
    "path": "deps/lua/test/sort.lua",
    "content": "-- two implementations of a sort function\n-- this is an example only. Lua has now a built-in function \"sort\"\n\n-- extracted from Programming Pearls, page 110\nfunction qsort(x,l,u,f)\n if l<u then\n  local m=math.random(u-(l-1))+l-1\t-- choose a random pivot in range l..u\n  x[l],x[m]=x[m],x[l]\t\t\t-- swap pivot to first position\n  local t=x[l]\t\t\t\t-- pivot value\n  m=l\n  local i=l+1\n  while i<=u do\n    -- invariant: x[l+1..m] < t <= x[m+1..i-1]\n    if f(x[i],t) then\n      m=m+1\n      x[m],x[i]=x[i],x[m]\t\t-- swap x[i] and x[m]\n    end\n    i=i+1\n  end\n  x[l],x[m]=x[m],x[l]\t\t\t-- swap pivot to a valid place\n  -- x[l+1..m-1] < x[m] <= x[m+1..u]\n  qsort(x,l,m-1,f)\n  qsort(x,m+1,u,f)\n end\nend\n\nfunction selectionsort(x,n,f)\n local i=1\n while i<=n do\n  local m,j=i,i+1\n  while j<=n do\n   if f(x[j],x[m]) then m=j end\n   j=j+1\n  end\n x[i],x[m]=x[m],x[i]\t\t\t-- swap x[i] and x[m]\n i=i+1\n end\nend\n\nfunction show(m,x)\n io.write(m,\"\\n\\t\")\n local i=1\n while x[i] do\n  io.write(x[i])\n  i=i+1\n  if x[i] then io.write(\",\") end\n end\n io.write(\"\\n\")\nend\n\nfunction testsorts(x)\n local n=1\n while x[n] do n=n+1 end; n=n-1\t\t-- count elements\n show(\"original\",x)\n qsort(x,1,n,function (x,y) return x<y end)\n show(\"after quicksort\",x)\n selectionsort(x,n,function (x,y) return x>y end)\n show(\"after reverse selection sort\",x)\n qsort(x,1,n,function (x,y) return x<y end)\n show(\"after quicksort again\",x)\nend\n\n-- array to be sorted\nx={\"Jan\",\"Feb\",\"Mar\",\"Apr\",\"May\",\"Jun\",\"Jul\",\"Aug\",\"Sep\",\"Oct\",\"Nov\",\"Dec\"}\n\ntestsorts(x)\n"
  },
  {
    "path": "deps/lua/test/table.lua",
    "content": "-- make table, grouping all data for the same item\n-- input is 2 columns (item, data)\n\nlocal A\nwhile 1 do\n local l=io.read()\n if l==nil then break end\n local _,_,a,b=string.find(l,'\"?([_%w]+)\"?%s*(.*)$')\n if a~=A then A=a io.write(\"\\n\",a,\":\") end\n io.write(\" \",b)\nend\nio.write(\"\\n\")\n"
  },
  {
    "path": "deps/lua/test/trace-calls.lua",
    "content": "-- trace calls\n-- example: lua -ltrace-calls bisect.lua\n\nlocal level=0\n\nlocal function hook(event)\n local t=debug.getinfo(3)\n io.write(level,\" >>> \",string.rep(\" \",level))\n if t~=nil and t.currentline>=0 then io.write(t.short_src,\":\",t.currentline,\" \") end\n t=debug.getinfo(2)\n if event==\"call\" then\n  level=level+1\n else\n  level=level-1 if level<0 then level=0 end\n end\n if t.what==\"main\" then\n  if event==\"call\" then\n   io.write(\"begin \",t.short_src)\n  else\n   io.write(\"end \",t.short_src)\n  end\n elseif t.what==\"Lua\" then\n-- table.foreach(t,print)\n  io.write(event,\" \",t.name or \"(Lua)\",\" <\",t.linedefined,\":\",t.short_src,\">\")\n else\n io.write(event,\" \",t.name or \"(C)\",\" [\",t.what,\"] \")\n end\n io.write(\"\\n\")\nend\n\ndebug.sethook(hook,\"cr\")\nlevel=0\n"
  },
  {
    "path": "deps/lua/test/trace-globals.lua",
    "content": "-- trace assigments to global variables\n\ndo\n -- a tostring that quotes strings. note the use of the original tostring.\n local _tostring=tostring\n local tostring=function(a)\n  if type(a)==\"string\" then\n   return string.format(\"%q\",a)\n  else\n   return _tostring(a)\n  end\n end\n\n local log=function (name,old,new)\n  local t=debug.getinfo(3,\"Sl\")\n  local line=t.currentline\n  io.write(t.short_src)\n  if line>=0 then io.write(\":\",line) end\n  io.write(\": \",name,\" is now \",tostring(new),\" (was \",tostring(old),\")\",\"\\n\")\n end\n\n local g={}\n local set=function (t,name,value)\n  log(name,g[name],value)\n  g[name]=value\n end\n setmetatable(getfenv(),{__index=g,__newindex=set})\nend\n\n-- an example\n\na=1\nb=2\na=10\nb=20\nb=nil\nb=200\nprint(a,b,c)\n"
  },
  {
    "path": "deps/lua/test/xd.lua",
    "content": "-- hex dump\n-- usage: lua xd.lua < file\n\nlocal offset=0\nwhile true do\n local s=io.read(16)\n if s==nil then return end\n io.write(string.format(\"%08X  \",offset))\n string.gsub(s,\"(.)\",\n\tfunction (c) io.write(string.format(\"%02X \",string.byte(c))) end)\n io.write(string.rep(\" \",3*(16-string.len(s))))\n io.write(\" \",string.gsub(s,\"%c\",\".\"),\"\\n\") \n offset=offset+16\nend\n"
  },
  {
    "path": "deps/memkind/Makefile",
    "content": "export JE_PREFIX=jemk_\n\n.PHONY: all\nall: \n\tcd src;\t./build_jemalloc.sh\n\tcd src; ./build.sh\n\n.PHONY: clean\nclean:\n\tcd src; make clean\n\n\n"
  },
  {
    "path": "deps/memkind/src/.gitignore",
    "content": ".cproject\n.project\nautom4te.cache/\n*.a\n*.o\n*.so\n*.la\n*.lo\n.deps/\n.dirstamp\n.libs/\n/aclocal.m4\n/ar-lib\n/compile\n/config.guess\n/config.h\n/config.h.in\n/config.log\n/config.status\n/config.sub\n/configure\n/depcomp\n/config_tls.h\n/install-sh\n/libtool\n/ltmain.sh\n/m4/\n/memkind-*.spec\n/memkind-*.tar.gz\n/Makefile\n/Makefile.in\n/missing\n/stamp-h1\n/stamp-h2\n/jemalloc/obj/\n/jemalloc/configure\n/src/memkind_defines.h.in\n/test/all_tests\n/test/.deps/\n/test/.dirstamp\n/test/.libs/\n/test/*.o\n/test/performance/.deps/\n/test/*.so\n/test/check.sh.log\n/test/check.sh.trs\n/test-suite.log\n/VERSION\n/examples/.deps/\n/examples/.dirstamp\n/examples/.libs/\n/examples/*.o\n/examples/*.lo\n/examples/*.la\n/memkind-hbw-nodes\n/test/allocator_perf_tool_tests\n/test/autohbw_candidates\n/test/locality_test\n/test/decorator_test\n/test/eratosthenes\n/test/environerr_hbw_malloc_test\n/test/environerr_test\n/test/freeing_memory_segfault_test\n/test/filter_memkind\n/test/gb_page_tests_bind_policy\n/test/hello_hbw\n/test/hello_memkind\n/test/hello_memkind_debug\n/test/mallocerr_test\n/test/memkind_allocated\n/test/new_kind\n/test/perf_tool\n/test/pmem_kinds\n/test/pmem_malloc\n/test/pmem_malloc_unlimited\n/test/pmem_usable_size\n/test/pmem_alignment\n/test/pmem_and_default_kind\n/test/pmem_multithreads\n/test/pmem_multithreads_onekind\n/test/pmem_free_with_unknown_kind\n/test/pmem_cpp_allocator\n/test/schedcpu_test\n/test/tieddisterr_test\n/test/alloc_benchmark_glibc\n/test/alloc_benchmark_hbw\n/test/alloc_benchmark_tbb\n/test/alloc_benchmark_pmem\n/test/autohbw_test_helper\n/test/environ_err_hbw_malloc_test\n/test/trace_mechanism_test_helper\n/examples/autohbw_candidates\n/examples/eratosthenes\n/examples/filter_memkind\n/examples/hello_hbw\n/examples/hello_memkind\n/examples/hello_memkind_debug\n/examples/memkind_allocated\n/examples/new_kind\n/examples/numakind_test\n/examples/numakind_macro.h\n/examples/pmem_kinds\n/examples/pmem_malloc\n/examples/pmem_malloc_unlimited\n/examples/pmem_usable_size\n/examples/pmem_alignment\n/examples/pmem_and_default_kind\n/examples/pmem_multithreads\n/examples/pmem_multithreads_onekind\n/examples/pmem_free_with_unknown_kind\n/examples/pmem_cpp_allocator\n/test-driver\n/vs\n"
  },
  {
    "path": "deps/memkind/src/.travis.yml",
    "content": "dist: trusty\nsudo: required\nlanguage: cpp\ncache: ccache\nmatrix:\n  fast_finish: true\n  include:\n\n    - name: \"Astyle\"\n      addons:\n        apt:\n          sources:\n            - ubuntu-toolchain-r-test\n          packages:\n            - g++-4.8\n      before_install:\n        - ./install_astyle.sh\n      script:\n        - ./astyle.sh\n\n    - name: \"GCC 4.8\"\n      addons:\n        apt:\n          sources:\n            - ubuntu-toolchain-r-test\n          packages:\n            - g++-4.8\n            - libnuma-dev\n      env:\n         - MATRIX_EVAL=\"CC=gcc-4.8 && CXX=g++-4.8 && JEMALLOC_CC=gcc-4.8 && JEMALLOC_CXX=g++-4.8\"\n      install:\n      script:\n        - CXX=$JEMALLOC_CXX CC=$JEMALLOC_CC LDFLAGS=\"-lrt -Wl,--no-as-needed\" ./build_jemalloc.sh\n        - ./autogen.sh\n        - LDFLAGS=\"-lrt -Wl,--no-as-needed\" ./configure\n        - make all\n        - make checkprogs\n      before_install:\n        - eval \"${MATRIX_EVAL}\"\n\n    - name: \"GCC 8.0\"\n      addons:\n        apt:\n          sources:\n            - ubuntu-toolchain-r-test\n          packages:\n            - g++-8\n            - libnuma-dev\n      env:\n         - MATRIX_EVAL=\"CC=gcc-8 && CXX=g++-8 && JEMALLOC_CC=gcc-8 && JEMALLOC_CXX=g++-8\"\n      install:\n      script:\n        - CXX=$JEMALLOC_CXX CC=$JEMALLOC_CC LDFLAGS=\"-lrt -Wl,--no-as-needed\" ./build_jemalloc.sh\n        - ./autogen.sh\n        - LDFLAGS=\"-lrt -Wl,--no-as-needed\" ./configure\n        - make all\n        - make checkprogs\n      before_install:\n        - eval \"${MATRIX_EVAL}\"\n\n    - name: \"Clang 7\"\n      addons:\n        apt:\n          sources:\n            - ubuntu-toolchain-r-test\n            - llvm-toolchain-trusty-7\n          packages:\n            - g++-4.8\n            - clang-7\n            - libnuma-dev\n      env:\n        - MATRIX_EVAL=\"CC=clang-7 && CXX=clang++-7 && JEMALLOC_CC=gcc-4.8 && JEMALLOC_CXX=g++-4.8\"\n      install:\n      script:\n        - CXX=$JEMALLOC_CXX CC=$JEMALLOC_CC LDFLAGS=\"-lrt -Wl,--no-as-needed\" ./build_jemalloc.sh\n        - ./autogen.sh\n        - LDFLAGS=\"-lrt -Wl,--no-as-needed\" ./configure\n        - make all\n      before_install:\n        - eval \"${MATRIX_EVAL}\"\n"
  },
  {
    "path": "deps/memkind/src/AUTHORS",
    "content": "Christopher Cantalupo <christopher.m.cantalupo@intel.com>\nVishwanath Venkatesan <vishwanath.venkatesan@intel.com>\nSteven Hampson <steven.t.hampson@intel.com>\nKrzysztof Czurylo <krzysztof.czurylo@intel.com>\nLeobardo Rountree <leobardo.r.rountree@intel.com>\nHector Barajas Villalobos <hector.a.barajas.villalobos@intel.com>\nRuchira Sasanka <ruchira.sasanka@intel.com>\nKrzysztof Kulakowski <krzysztof.kulakowski@intel.com>\nRafael Aquini <aquini@redhat.com>\nAlexandr Konovalov <alexandr.konovalov@intel.com>\nGrzegorz Ozanski <grzegorz.ozanski@intel.com>\nArtur Koziej <artur.koziej@intel.com>\nJakub Dlugolecki <jakub.dlugolecki@intel.com>\nLukasz Anaczkowski <lukasz.anaczkowski@intel.com>\nAgata Wozniak <agata.wozniak@intel.com>\nGrzegorz Andrejczuk <grzegorz.andrejczuk@intel.com>\nKamil Patelczyk <kamil.patelczyk@intel.com>\nJeff Hammond <jeff.r.hammond@intel.com>\nAleksandra Hernik <aleksandra.hernik@intel.com>\nPawel Kochanek <pawel.b.kochanek@intel.com>\nKatarzyna Wasiuta <katarzyna.wasiuta@intel.com>\nKrzysztof Filipek <krzysztof.filipek@intel.com>\nMichal Biesek <michal.biesek@intel.com>\n"
  },
  {
    "path": "deps/memkind/src/CONTRIBUTING",
    "content": "CONTRIBUTING\n============\n\nThis file is intended to help those interested in contributing to the\nmemkind library.\n\n\nCOMMUNICATION\n=============\n\nPlease participate in the memkind mailing list:\n\n    https://lists.01.org/mailman/listinfo/memkind\n\nThere is also the option of opening an issue through github:\n\n    https://github.com/memkind/memkind/issues\n\nThe mailing list is intended for discussion and the issues are useful\nfor tracking progress on tasks.  The TODO file lists out a number of\ntopics, and in order to prioritize one of them please open a github\nissue with a related subject.\n\n\nTESTING\n=======\n\nThe tests require a Linux kernel newer than 3.11 (the details are\ndocumented in the memkind README), and the reservation of 3000 huge\npages.  The huge pages can be reserved with the following command:\n\n    $ sudo echo 3000 > /proc/sys/vm/nr_hugepages\n\nOnly in the case where gigabyte pages have been reserved will the\ntests associated with gigabyte pages be executed.  Reserving gigabyte\npages may require a modification to the kernel command line unless the\nkernel is quite recent.\n\nTo test memkind simply execute the \"make check\" target after building.\nThis target calls memkind/test/test.sh with parameters\ndepending on the environment.\n\nMost of the tests are written within the gtest framework, however, as\npart of testing the example programs are also executed and the return\ncode of the executable determines pass or fail.  The autotools test\ninfrastructure is used as a high level executor, but it does not track\nindividual tests.  The consequence of this is that the autotools\noutput records only one high level test which passes in the case where\nevery underlying test was successful and fails if any underlying test\nfails.  The individual test results are recorded in the directory\ncalled \"gtest_output.\" Here you will find the log of the tests in\ngtest_output/test.out and a set of junit style xml results: one for\neach test.  Note that a side effect of having only one autotools test\nis that autotools parallel testing is disabled.  We have\nmulti-threaded tests that use the OpenMP run-time which enables more\npurposeful and deterministic testing of threading issues.  Note that\nthe OpenMP run-time is only required for testing, it is not used by\nthe memkind library internally.\n\n\nCODING STYLE\n============\n\nBefore submitting a patch for inclusion, please run the modified\nsource code files through astyle with the following options\n\n    $ astyle --style=linux --indent=spaces=4 -S --max-continuation-indent=80 \\\n             --max-code-length=80 --break-after-logical --indent-namespaces -z2 \\\n             --align-pointer=name\n\nMore information about astyle can be found here:\n\n    http://astyle.sourceforge.net/\n\nIn C, source code constants are in all caps and everything else is in\nall lower case.  Underscores are used to separate words within a\nsymbol name. No camel case shall be used in C code.  The test code is\nlargely written in C++.  Here camel-case should be used for class\nnames and should always have a capitalized first letter.  Other\nsymbols in C++ should generally follow the C style.\n\nMost symbols with global scope shall be prefixed with \"memkind_\" or\n\"hbw_\" depending on which interface they are a part of.\n\nAny global variable shall have _g appended to the variable name and in\nmost cases shall be statically scoped within a single compilation\nunit.  The exception to that rule are static memory kinds that are\ndeclared as extern within the associated interface header and are\nconstant after initialization.  Global variables should be used in a\nvery narrow set of circumstances and in most cases modifications\nshould be guarded with pthread_once(3).\n\nFunctions not used outside of the compilation unit shall be declared\nas static.  All functions which are not declared as static shall be\ndocumented in a man page and have an associated interface header file.\n\nPreprocessor mark-up is discouraged when other options are possible.\nPlease use enum in place of #define when value control at configure or\nbuild time is not required.\n\n\nTESTS\n=====\n\nThe current state of the tests is not nearly as well organized as it\ncould be.  That being said, it is quite simple to add a new test.\nMost C++ files in the test directory are associated with a single\ngtest testing::Test class.  These classes usually have several\nassociated test fixtures in the same file.  If a new test can be added\nas a fixture to an existing class, simply add the fixture to the file\nand the test will be incorporated into the test infrastructure.\nIf a new class is required, create a new file and add it to the list\nof \"test_all_tests_SOURCES\" in memkind/test/Makfile.mk and it will\nbe incorporated into the test infrastructure.\n\nThere are a few files which define classes which are not google test\nclasses.  These are check.cpp, trial_generator.cpp and main.cpp.  The\ncheck.cpp file defines a class Check that can be used to validate\nfundamental memkind features like NUMA node location, and page size.\nThe trial_generator.cpp file is used to abstract a sequence of\nallocation and deallocation calls while performing checks on the\nresults of each call; this can be used to apply similar tests to all\nof the different allocation APIs.  The main.cpp file is a simple\nwrapper around testing::InitGoogleTest and RUN_ALL_TESTS().\n\n\nSUBMITTING A PATCH\n==================\n\nPlease be sure that all tests pass before submission and that the\nstyle conforms to the specifications given here.  If a new feature is\nimplemented in the patch, please also include unit tests and an\nexample which exercises this feature.  Once these requirements have\nbeen met, please submit a pull request through github.\n"
  },
  {
    "path": "deps/memkind/src/COPYING",
    "content": "Unless otherwise specified, files in the memkind source distribution are\nsubject to the following license:\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are met:\n\n    * Redistributions of source code must retain the above copyright notice,\n      this list of conditions and the following disclaimer.\n    * Redistributions in binary form must reproduce the above copyright\n      notice, this list of conditions and the following disclaimer in the\n      documentation and/or other materials provided with the distribution.\n    * Neither the name of Intel Corporation nor the names of its contributors\n      may be used to endorse or promote products derived from this software\n      without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\nAND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\nIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\nDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE\nFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\nDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\nSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\nCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\nOR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\nOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
  },
  {
    "path": "deps/memkind/src/ChangeLog",
    "content": "* Mon Dec 10 2018 Michal Biesek <michal.biesek@intel.com> v1.8.0\n- Fixed error with realloc/free method with passing thread-specific cache flag\n- Fixed error with memkind_create_pmem(), if other PMEM kind was destroyed before\n- Fixed error with zeroing large allocations in PMEM kind\n- Added support to create kind without maximum size limit of PMEM kind (max_size=0)\n- Extended memkind API with memkind_malloc_usable_size()\n- Removed EXPERIMENTAL from most methods in memkind API\n- Added MEMKIND_ERROR_ARENAS_CREATE code\n- Added C++ allocator for PMEM kind\n- Extended PMEM examples\n- Fixed integration with Travis CI\n- Extended Travis CI configuration with Astyle and Coverage\n- Added PMEM kind tests\n\n* Wed Jan 24 2018 Pawel Kochanek <pawel.b.kochanek@intel.com> v1.7.0\n- Updated internally used jemalloc to upstream version 5.0.\n- Fixed error that has been occuring while memkind was dynamically loaded.\n- Fixed MEMKIND_HBW_NODES behavior for single NUMA node system.\n- Removed licenses other than BSD 3-clause from COPYING.\n- Changed build instructions.\n- Added configurable jemalloc prefix in build scripts.\n- Upgraded gtest to version 1.8.0.\n- Added memory footprint tests.\n- Added locality test for MEMKIND_PREFERRED.\n- Applied test parametrization in BATests.\n- Fixed problems with pytest testing in Travis.\n- Added huge page configuration in several tests.\n- Removed several symbols that are no longer exposed in public API from man pages.\n- Fixed HBW_POLICY_BIND_ALL documentation.\n\n* Tue Jun 13 2017 Artur Koziej <artur.koziej@intel.com> v1.6.0\n- Introduced new policy HBW_POLICY_BIND_ALL.\n- Introduced MEMKIND_HBW_ALL, MEMKIND_HBW_ALL_HUGETLB and MEMKIND_REGULAR kinds.\n- Fixed hbw_posix_memalign_psize() return value in case of unsupported policy and\n  page size combination.\n- Documented supported policy and page size combination for\n  hbw_posix_memalign_psize().\n- Merged libmemkind.a and libjemalloc_pic.a into a single static library.\n- Fixed OOM during tcache acquisition.\n- Tuned huge pages configuration in memkind tests.\n- Added stress tests for allocations with large random sizes.\n- Added stress tests for allocations with random kinds.\n- Extended policy and NUMA node binding tests.\n- Fixed several Travis build problems.\n- Removed gb_page_tests_preferred_policy tests.\n\n* Tue Feb 21 2017 Krzysztof Kulakowski <krzysztof.kulakowski@intel.com> v1.5.0\n- Intel(R) TBB scalable_allocator can be used as heap management alternative\n  to jemalloc (requires installed Intel (R) TBB package and environment variable\n  MEMKIND_HEAP_MANAGER=TBB)\n- Updated internally used jemalloc to upstream version 4.3.1 (previously modified 3.5.1)\n- Introduced new environment option MEMKIND_HOG_MEMORY. Setting it to \"1\" will prevent\n  memkind from releasing memory to OS.\n- Introduced support for KNM processor from Intel(R) Xeon Phi(TM) family\n- General cleanup of build scripts (e.g. fixed issues found on Ubuntu,\n  introduced simple build.sh script)\n- Deprecated support for gigabyte-pages\n- Removed number of previously deprecated symbols\n\n* Thu Nov 17 2016 Krzysztof Kulakowski <krzysztof.kulakowski@intel.com> v1.4.0\n- Introduced hbw_verify_memory_region() for verifying if provided memory\n  region fully falls into high-bandwidth memory (for details please take a look\n  at man/hbwmalloc.3).\n- New API for creating kinds using set of predefined attributes:\n  memkind_create_kind(), memkind_destroy_kind() (for details please take a look\n  at man/memkind.3).\n- Fixed issue in autohbw where it was reporting limit error regardless of\n  value provided.\n- Added logging on error paths.\n- Test base fixes and improvements.\n\n* Tue Sep 27 2016 Krzysztof Kulakowski <krzysztof.kulakowski@intel.com> v1.3.0\n- Introduced logging mechanism (for details please take a look at\n  man/memkind.3).\n- Deprecated symbols: memkind_finalize(), memkind_get_num_kind(),\n  memkind_get_kind_by_partition(), memkind_get_kind_by_name(),\n  memkind_partition_mmap(), memkind_get_size(), MEMKIND_ERROR_MEMALIGN,\n  MEMKIND_ERROR_MALLCTL,  MEMKIND_ERROR_GETCPU, MEMKIND_ERROR_PMTT,\n  MEMKIND_ERROR_TIEDISTANCE, MEMKIND_ERROR_ALIGNMENT, MEMKIND_ERROR_MALLOCX,\n  MEMKIND_ERROR_REPNAME, MEMKIND_ERROR_PTHREAD, MEMKIND_ERROR_BADPOLICY,\n  MEMKIND_ERROR_REPPOLICY.\n- Added integration with hwloc (turned on with --with-hwloc).\n- Cleanup of symbols exposed by libmemkind.so (e.g. no longer exposing libnuma\n  and jemalloc symbols).\n- AutoHBW files have been moved to \"memkind/autohbw\" directory, code has\n  been refactored and tests have been added to appropriate scenarios.\n- Library is now built with flags improving security (can be turned off\n  with --disable-secure configure time option).\n- Changed configuration of jemalloc to turn off unused features.\n\n* Thu Aug 18 2016 Krzysztof Kulakowski <krzysztof.kulakowski@intel.com> v1.2.0\n- memkind_create() and memkind_ops have been deprecated (moved to\n  memkind_deprecated.h).\n- Deprecated the headers from memkind/internal and added compile-time\n  warnings to them (plan is to remove those from rpm packages).\n- autohbw files have been moved to memkind/autohbw directory.\n- API decorators (memkind_malloc_pre, memkind_malloc_post etc.) now\n  need to be enabled with configure-time option --enable-decorators.\n- Allocation time optimizations with up to 20% improvement.\n\n* Wed Jul 13 2016 Krzysztof Kulakowski <krzysztof.kulakowski@intel.com> v1.1.1\n- Performance improvement for memkind_free() in scenario where NULL\n  was passed as kind (reduced by 60%).\n- Introduced integration with Travis CI.\n- Fixed issue where memory returned from calloc was not filled with zeroes\n  when using memkind_pmem kinds.\n- Fixed issue where interleave kinds was failing on systems without\n  Transparent Huge Pages module configured.\n- Resolved several issues that was causing compilation errors on some systems.\n- test/test.sh is now able to detect system configuration, and run only those\n  tests which requirements are meet.\n- Added gtest to repo to avoid downloading it during build proecess.\n\n* Mon May 23 2016 Krzysztof Kulakowski <krzysztof.kulakowski@intel.com> v1.1.0\n- Implemented algorithm for detecting high-bandwidth memory on Intel Xeon Phi X200\n  without parsing PMTT.\n- PMTT-parsing service has been removed.\n- memkind struct definition from memkind.h has been replaced by forward\n  declaration. Definition has been moved to /internal/memkind_private.h and\n  shouldn't be considered as part of library interface anymore.\n- Performance improvment for hugetlb kinds (check_available() is no longer\n  parsing sysfs every time).\n- Performance fix by removing --enable-safe from jemmaloc build flags.\n- New tests for validating high-bandwidth memory detection has been added.\n- AFTS has been split into two groups: memkind-afts.ts and memkind-afts-ext.ts,\n  based on amount of required memory (for details view test/README).\n\n* Thu Mar 03 2016 Artur Koziej <artur.koziej@intel.com> v1.0.0\n- hbwmalloc.h released as stable API.\n- Introduce API standards: STANDARD API, NON-STANDARD API, EXPERIMENTAL API.\n- Introduce memkind versioning API.\n- Fix type names in hbwmalloc API.\n- Change error codes to POSIX standard ones in hbwmalloc API.\n- Move API headers to include directory.\n- Move NON-STANDARD API, or EXPERIMENTAL API to include/memkind/internal directory.\n- New man page (memkind_interleave.3) describing interleave kind.\n- Significant documentation improvement.\n- Performance fix for jemalloc - significantly decrease of library initialization time.\n- Enforce 2MB alignment in jemalloc due to the Linux kernel bug (munmap() fails for\n  huge pages, when the size is not aligned).\n- Move to systemd defined service (memkind.service), drop init.d script.\n- New rpm layout (memkind and memkind-devel).\n- Remove false dependency on OpenMP.\n- Extend test base for stress, longevity and initialization performance tests.\n- Fix memory leak in ctl_growk().\n- Rename namespace: hbwmalloc -> hbw.\n- Rename class: hbwmalloc_allocator -> allocator.\n\n* Thu Oct 15 2015 Krzysztof Kulakowski <krzysztof.kulakowski@intel.com> v0.3.0\n- Added two new kinds: MEMKIND_INTERLEAVE and MEMKIND_HBW_INTERLEAVE\n  (for details please take a look at man/memkind.3)\n- Added support for file-backed memory heaps\n  (for details please take a look at man/memkind_pmem.3)\n- Added autohbw library, that intercepts the standard heap allocations,\n  which let use high-bandwidth memory without modifying existing codebase\n  (for details please take a look at examples/autohbw_README)\n- jemalloc is now, staticly linked, internal component of memkind RPM\n  instead of rpm dependency.\n- Added memkind_allocated example which demonstrates usage of memkind\n  with alignas() alignment specifier introduced in C++11\n  (for details please take a look at examples/README)\n- Added support for using thread local storage to improve performance in\n  multithreaded applications (enabled by configure time option: --enable-tls)\n- Added posibility to set number of jemalloc arenas per kind by\n  configure time parameter or enviromental variable\n  (for details please take a look at MEMKIND_ARENA_NUM_PER_KIND section\n  of man/memkind.3).\n- Added decorators to the memkind allocation APIs.\n  These are weak symbols (pre and post for each API) which can modify\n  the input and output of each of the calls.\n- Significantly extended test base (new groups of tests: performance,\n  multithreaded, PMTT, nagtive, stress).\n\n* Fri Jan 9 2015 Christopher Cantalupo <christopher.m.cantalupo@intel.com> v0.2.0\n- Bumped memkind ABI version to 0:1:0.\n- Removed memkind_get_kind_for_free() from externally facing API's.  Instead if\n  memkind_free() is called with zero passed for the kind then\n  memkind_get_kind_for_free() is called internally.\n- Moved to single callback memkind_partition_mmap() to simplify\n  jemalloc modifications.\n- Added hooks for setting file descriptor and offset for mmap enabling\n  file-backed memory kinds.\n- Added a void pointer called \"priv\" to memkind structure for storing data for\n  user-defined kinds.\n- Removed call to sched_getcpu(), now thread id is hashed to determine the arena\n  index.\n- Added weak symbol hooks for decorating the heap management functions.\n- Fixed several issues with init.d/memkind script and spec file scriptlets that\n  are exposed by SLES-12.\n- Introduced an example library called numakind.\n  The numakind library will allocate from the closest NUMA node to a thread as\n  measured when that thread makes its first allocation call.\n- Fixed error handling in memkind_gbtlb_mmap() that could cause a segfault when\n  gigabyte pages are not available.\n- Added tests for PMTT parser.\n- Removed binary mock PMTT table from source code, replaced it with a hexdump.\n- Fixed a number of issues in test scripts which were suppressing errors.\n- Removed unnecessary includes from header files.\n- Better error checking in example code.\n- Documentation update and clean up.\n\n* Thu Nov 13 2014 Christopher Cantalupo <christopher.m.cantalupo@intel.com> v0.1.0\n- Increased test code coverage significantly.\n- Fixed bug in memkind_error_message() for MEMKIND_ERROR_TOOMANY.\n- Removed memkind_arena_free() API since it was redundant with memkind_default_free().\n- Static memkind structs are now declared as extern in the headers and defined in the\n  source files files rather than being statically defined in the headers.\n\n* Thu Oct 30 2014 Christopher Cantalupo <christopher.m.cantalupo@intel.com> v0.0.9\n- Now building with autotools.\n- Updated documentation.\n- Fixed typo in copyright.\n- Fixed test scripts to properly handle return code of each test.\n- Added C++03 standard allocator that uses hbw_malloc and hbw_free.\n* Tue Sep 30 2014 Christopher Cantalupo <christopher.m.cantalupo@intel.com> v0.0.8\n- Added GBTLB functionality, code clean up, documentation updates,\n  examples directory.  Examples includes stream modified to use\n  memkind interface.  Code coverage still lacking, and documentation\n  incomplete.\n"
  },
  {
    "path": "deps/memkind/src/INSTALL",
    "content": "Installation Instructions\n*************************\n\nCopyright (C) 1994-1996, 1999-2002, 2004-2013 Free Software Foundation,\nInc.\n\n   Copying and distribution of this file, with or without modification,\nare permitted in any medium without royalty provided the copyright\nnotice and this notice are preserved.  This file is offered as-is,\nwithout warranty of any kind.\n\nBasic Installation\n==================\n\n   Briefly, the shell commands `./configure; make; make install' should\nconfigure, build, and install this package.  The following\nmore-detailed instructions are generic; see the `README' file for\ninstructions specific to this package.  Some packages provide this\n`INSTALL' file but do not implement all of the features documented\nbelow.  The lack of an optional feature in a given package is not\nnecessarily a bug.  More recommendations for GNU packages can be found\nin *note Makefile Conventions: (standards)Makefile Conventions.\n\n   The `configure' shell script attempts to guess correct values for\nvarious system-dependent variables used during compilation.  It uses\nthose values to create a `Makefile' in each directory of the package.\nIt may also create one or more `.h' files containing system-dependent\ndefinitions.  Finally, it creates a shell script `config.status' that\nyou can run in the future to recreate the current configuration, and a\nfile `config.log' containing compiler output (useful mainly for\ndebugging `configure').\n\n   It can also use an optional file (typically called `config.cache'\nand enabled with `--cache-file=config.cache' or simply `-C') that saves\nthe results of its tests to speed up reconfiguring.  Caching is\ndisabled by default to prevent problems with accidental use of stale\ncache files.\n\n   If you need to do unusual things to compile the package, please try\nto figure out how `configure' could check whether to do them, and mail\ndiffs or instructions to the address given in the `README' so they can\nbe considered for the next release.  If you are using the cache, and at\nsome point `config.cache' contains results you don't want to keep, you\nmay remove or edit it.\n\n   The file `configure.ac' (or `configure.in') is used to create\n`configure' by a program called `autoconf'.  You need `configure.ac' if\nyou want to change it or regenerate `configure' using a newer version\nof `autoconf'.\n\n   The simplest way to compile this package is:\n\n  1. `cd' to the directory containing the package's source code and type\n     `./configure' to configure the package for your system.\n\n     Running `configure' might take a while.  While running, it prints\n     some messages telling which features it is checking for.\n\n  2. Type `make' to compile the package.\n\n  3. Optionally, type `make check' to run any self-tests that come with\n     the package, generally using the just-built uninstalled binaries.\n\n  4. Type `make install' to install the programs and any data files and\n     documentation.  When installing into a prefix owned by root, it is\n     recommended that the package be configured and built as a regular\n     user, and only the `make install' phase executed with root\n     privileges.\n\n  5. Optionally, type `make installcheck' to repeat any self-tests, but\n     this time using the binaries in their final installed location.\n     This target does not install anything.  Running this target as a\n     regular user, particularly if the prior `make install' required\n     root privileges, verifies that the installation completed\n     correctly.\n\n  6. You can remove the program binaries and object files from the\n     source code directory by typing `make clean'.  To also remove the\n     files that `configure' created (so you can compile the package for\n     a different kind of computer), type `make distclean'.  There is\n     also a `make maintainer-clean' target, but that is intended mainly\n     for the package's developers.  If you use it, you may have to get\n     all sorts of other programs in order to regenerate files that came\n     with the distribution.\n\n  7. Often, you can also type `make uninstall' to remove the installed\n     files again.  In practice, not all packages have tested that\n     uninstallation works correctly, even though it is required by the\n     GNU Coding Standards.\n\n  8. Some packages, particularly those that use Automake, provide `make\n     distcheck', which can by used by developers to test that all other\n     targets like `make install' and `make uninstall' work correctly.\n     This target is generally not run by end users.\n\nCompilers and Options\n=====================\n\n   Some systems require unusual options for compilation or linking that\nthe `configure' script does not know about.  Run `./configure --help'\nfor details on some of the pertinent environment variables.\n\n   You can give `configure' initial values for configuration parameters\nby setting variables in the command line or in the environment.  Here\nis an example:\n\n     ./configure CC=c99 CFLAGS=-g LIBS=-lposix\n\n   *Note Defining Variables::, for more details.\n\nCompiling For Multiple Architectures\n====================================\n\n   You can compile the package for more than one kind of computer at the\nsame time, by placing the object files for each architecture in their\nown directory.  To do this, you can use GNU `make'.  `cd' to the\ndirectory where you want the object files and executables to go and run\nthe `configure' script.  `configure' automatically checks for the\nsource code in the directory that `configure' is in and in `..'.  This\nis known as a \"VPATH\" build.\n\n   With a non-GNU `make', it is safer to compile the package for one\narchitecture at a time in the source code directory.  After you have\ninstalled the package for one architecture, use `make distclean' before\nreconfiguring for another architecture.\n\n   On MacOS X 10.5 and later systems, you can create libraries and\nexecutables that work on multiple system types--known as \"fat\" or\n\"universal\" binaries--by specifying multiple `-arch' options to the\ncompiler but only a single `-arch' option to the preprocessor.  Like\nthis:\n\n     ./configure CC=\"gcc -arch i386 -arch x86_64 -arch ppc -arch ppc64\" \\\n                 CXX=\"g++ -arch i386 -arch x86_64 -arch ppc -arch ppc64\" \\\n                 CPP=\"gcc -E\" CXXCPP=\"g++ -E\"\n\n   This is not guaranteed to produce working output in all cases, you\nmay have to build one architecture at a time and combine the results\nusing the `lipo' tool if you have problems.\n\nInstallation Names\n==================\n\n   By default, `make install' installs the package's commands under\n`/usr/local/bin', include files under `/usr/local/include', etc.  You\ncan specify an installation prefix other than `/usr/local' by giving\n`configure' the option `--prefix=PREFIX', where PREFIX must be an\nabsolute file name.\n\n   You can specify separate installation prefixes for\narchitecture-specific files and architecture-independent files.  If you\npass the option `--exec-prefix=PREFIX' to `configure', the package uses\nPREFIX as the prefix for installing programs and libraries.\nDocumentation and other data files still use the regular prefix.\n\n   In addition, if you use an unusual directory layout you can give\noptions like `--bindir=DIR' to specify different values for particular\nkinds of files.  Run `configure --help' for a list of the directories\nyou can set and what kinds of files go in them.  In general, the\ndefault for these options is expressed in terms of `${prefix}', so that\nspecifying just `--prefix' will affect all of the other directory\nspecifications that were not explicitly provided.\n\n   The most portable way to affect installation locations is to pass the\ncorrect locations to `configure'; however, many packages provide one or\nboth of the following shortcuts of passing variable assignments to the\n`make install' command line to change installation locations without\nhaving to reconfigure or recompile.\n\n   The first method involves providing an override variable for each\naffected directory.  For example, `make install\nprefix=/alternate/directory' will choose an alternate location for all\ndirectory configuration variables that were expressed in terms of\n`${prefix}'.  Any directories that were specified during `configure',\nbut not in terms of `${prefix}', must each be overridden at install\ntime for the entire installation to be relocated.  The approach of\nmakefile variable overrides for each directory variable is required by\nthe GNU Coding Standards, and ideally causes no recompilation.\nHowever, some platforms have known limitations with the semantics of\nshared libraries that end up requiring recompilation when using this\nmethod, particularly noticeable in packages that use GNU Libtool.\n\n   The second method involves providing the `DESTDIR' variable.  For\nexample, `make install DESTDIR=/alternate/directory' will prepend\n`/alternate/directory' before all installation names.  The approach of\n`DESTDIR' overrides is not required by the GNU Coding Standards, and\ndoes not work on platforms that have drive letters.  On the other hand,\nit does better at avoiding recompilation issues, and works well even\nwhen some directory options were not specified in terms of `${prefix}'\nat `configure' time.\n\nOptional Features\n=================\n\n   If the package supports it, you can cause programs to be installed\nwith an extra prefix or suffix on their names by giving `configure' the\noption `--program-prefix=PREFIX' or `--program-suffix=SUFFIX'.\n\n   Some packages pay attention to `--enable-FEATURE' options to\n`configure', where FEATURE indicates an optional part of the package.\nThey may also pay attention to `--with-PACKAGE' options, where PACKAGE\nis something like `gnu-as' or `x' (for the X Window System).  The\n`README' should mention any `--enable-' and `--with-' options that the\npackage recognizes.\n\n   For packages that use the X Window System, `configure' can usually\nfind the X include and library files automatically, but if it doesn't,\nyou can use the `configure' options `--x-includes=DIR' and\n`--x-libraries=DIR' to specify their locations.\n\n   Some packages offer the ability to configure how verbose the\nexecution of `make' will be.  For these packages, running `./configure\n--enable-silent-rules' sets the default to minimal output, which can be\noverridden with `make V=1'; while running `./configure\n--disable-silent-rules' sets the default to verbose, which can be\noverridden with `make V=0'.\n\nParticular systems\n==================\n\n   On HP-UX, the default C compiler is not ANSI C compatible.  If GNU\nCC is not installed, it is recommended to use the following options in\norder to use an ANSI C compiler:\n\n     ./configure CC=\"cc -Ae -D_XOPEN_SOURCE=500\"\n\nand if that doesn't work, install pre-built binaries of GCC for HP-UX.\n\n   HP-UX `make' updates targets which have the same time stamps as\ntheir prerequisites, which makes it generally unusable when shipped\ngenerated files such as `configure' are involved.  Use GNU `make'\ninstead.\n\n   On OSF/1 a.k.a. Tru64, some versions of the default C compiler cannot\nparse its `<wchar.h>' header file.  The option `-nodtk' can be used as\na workaround.  If GNU CC is not installed, it is therefore recommended\nto try\n\n     ./configure CC=\"cc\"\n\nand if that doesn't work, try\n\n     ./configure CC=\"cc -nodtk\"\n\n   On Solaris, don't put `/usr/ucb' early in your `PATH'.  This\ndirectory contains several dysfunctional programs; working variants of\nthese programs are available in `/usr/bin'.  So, if you need `/usr/ucb'\nin your `PATH', put it _after_ `/usr/bin'.\n\n   On Haiku, software installed for all users goes in `/boot/common',\nnot `/usr/local'.  It is recommended to use the following options:\n\n     ./configure --prefix=/boot/common\n\nSpecifying the System Type\n==========================\n\n   There may be some features `configure' cannot figure out\nautomatically, but needs to determine by the type of machine the package\nwill run on.  Usually, assuming the package is built to be run on the\n_same_ architectures, `configure' can figure that out, but if it prints\na message saying it cannot guess the machine type, give it the\n`--build=TYPE' option.  TYPE can either be a short name for the system\ntype, such as `sun4', or a canonical name which has the form:\n\n     CPU-COMPANY-SYSTEM\n\nwhere SYSTEM can have one of these forms:\n\n     OS\n     KERNEL-OS\n\n   See the file `config.sub' for the possible values of each field.  If\n`config.sub' isn't included in this package, then this package doesn't\nneed to know the machine type.\n\n   If you are _building_ compiler tools for cross-compiling, you should\nuse the option `--target=TYPE' to select the type of system they will\nproduce code for.\n\n   If you want to _use_ a cross compiler, that generates code for a\nplatform different from the build platform, you should specify the\n\"host\" platform (i.e., that on which the generated programs will\neventually be run) with `--host=TYPE'.\n\nSharing Defaults\n================\n\n   If you want to set default values for `configure' scripts to share,\nyou can create a site shell script called `config.site' that gives\ndefault values for variables like `CC', `cache_file', and `prefix'.\n`configure' looks for `PREFIX/share/config.site' if it exists, then\n`PREFIX/etc/config.site' if it exists.  Or, you can set the\n`CONFIG_SITE' environment variable to the location of the site script.\nA warning: not all `configure' scripts look for a site script.\n\nDefining Variables\n==================\n\n   Variables not defined in a site shell script can be set in the\nenvironment passed to `configure'.  However, some packages may run\nconfigure again during the build, and the customized values of these\nvariables may be lost.  In order to avoid this problem, you should set\nthem in the `configure' command line, using `VAR=value'.  For example:\n\n     ./configure CC=/usr/local2/bin/gcc\n\ncauses the specified `gcc' to be used as the C compiler (unless it is\noverridden in the site shell script).\n\nUnfortunately, this technique does not work for `CONFIG_SHELL' due to\nan Autoconf limitation.  Until the limitation is lifted, you can use\nthis workaround:\n\n     CONFIG_SHELL=/bin/bash ./configure CONFIG_SHELL=/bin/bash\n\n`configure' Invocation\n======================\n\n   `configure' recognizes the following options to control how it\noperates.\n\n`--help'\n`-h'\n     Print a summary of all of the options to `configure', and exit.\n\n`--help=short'\n`--help=recursive'\n     Print a summary of the options unique to this package's\n     `configure', and exit.  The `short' variant lists options used\n     only in the top level, while the `recursive' variant lists options\n     also present in any nested packages.\n\n`--version'\n`-V'\n     Print the version of Autoconf used to generate the `configure'\n     script, and exit.\n\n`--cache-file=FILE'\n     Enable the cache: use and save the results of the tests in FILE,\n     traditionally `config.cache'.  FILE defaults to `/dev/null' to\n     disable caching.\n\n`--config-cache'\n`-C'\n     Alias for `--cache-file=config.cache'.\n\n`--quiet'\n`--silent'\n`-q'\n     Do not print messages saying which checks are being made.  To\n     suppress all normal output, redirect it to `/dev/null' (any error\n     messages will still be shown).\n\n`--srcdir=DIR'\n     Look for the package's source code in directory DIR.  Usually\n     `configure' can determine that directory automatically.\n\n`--prefix=DIR'\n     Use DIR as the installation prefix.  *note Installation Names::\n     for more details, including other options available for fine-tuning\n     the installation locations.\n\n`--no-create'\n`-n'\n     Run the configure checks, but stop before creating any output\n     files.\n\n`configure' also accepts some other, not widely useful, options.  Run\n`configure --help' for more details.\n"
  },
  {
    "path": "deps/memkind/src/MANIFEST",
    "content": "AUTHORS\nCOPYING\nCONTRIBUTING\nChangeLog\nINSTALL\nMakefile.am\nNEWS\nPULL_REQUEST_TEMPLATE.md\nREADME\n.travis.yml\nastyle.sh\nautogen.sh\nbuild.sh\nbuild_jemalloc.sh\nconfigure.ac\nautohbw/Makefile.mk\nautohbw/autohbw.c\nautohbw/autohbw_get_src_lines.pl\nautohbw/autohbw_README\nautohbw/autohbw_test.sh\nexamples/autohbw_candidates.c\nexamples/filter_example.c\nexamples/hello_memkind_example.c\nexamples/README\nexamples/Makefile.mk\nexamples/hello_hbw_example.c\nexamples/memkind_allocated_example.cpp\nexamples/memkind_allocated.hpp\nexamples/memkind_decorator_debug.c\nexamples/pmem_kinds.c\nexamples/pmem_malloc.c\nexamples/pmem_malloc_unlimited.c\nexamples/pmem_usable_size.c\nexamples/pmem_alignment.c\nexamples/pmem_and_default_kind.c\nexamples/pmem_multithreads.c\nexamples/pmem_multithreads_onekind.c\nexamples/pmem_free_with_unknown_kind.c\nexamples/pmem_cpp_allocator.cpp\ninstall_astyle.sh\nm4/ax_cxx_compile_stdcxx.m4\nm4/ax_cxx_compile_stdcxx_11.m4\nm4/ax_pthread.m4\nman/memkind-hbw-nodes.1\nman/hbwmalloc.3\nman/memkind.3\nman/memkind_arena.3\nman/memkind_default.3\nman/memkind_hbw.3\nman/memkind_hugetlb.3\nman/memkind_pmem.3\nman/hbwallocator.3\nman/pmemallocator.3\nman/autohbw.7\nmemkind.spec.mk\nsrc/heap_manager.c\nsrc/hbwmalloc.c\nsrc/memkind.c\nsrc/memkind_arena.c\nsrc/memkind_default.c\nsrc/memkind_gbtlb.c\nsrc/memkind_hbw.c\nsrc/memkind_regular.c\nsrc/memkind_hugetlb.c\nsrc/memkind_interleave.c\nsrc/memkind_pmem.c\nsrc/memkind_log.c\nsrc/memkind-hbw-nodes.c\nsrc/tbb_wrapper.c\nsrc/Makefile.mk\ninclude/hbwmalloc.h\ninclude/memkind.h\ninclude/memkind_deprecated.h\ninclude/hbw_allocator.h\ninclude/pmem_allocator.h\ninclude/memkind/internal/heap_manager.h\ninclude/memkind/internal/memkind_arena.h\ninclude/memkind/internal/memkind_default.h\ninclude/memkind/internal/memkind_gbtlb.h\ninclude/memkind/internal/memkind_hbw.h\ninclude/memkind/internal/memkind_regular.h\ninclude/memkind/internal/memkind_hugetlb.h\ninclude/memkind/internal/memkind_interleave.h\ninclude/memkind/internal/memkind_pmem.h\ninclude/memkind/internal/memkind_private.h\ninclude/memkind/internal/memkind_log.h\ninclude/memkind/internal/tbb_wrapper.h\ninclude/memkind/internal/tbb_mem_pool_policy.h\ntest/Makefile.mk\ntest/Allocator.hpp\ntest/TestPolicy.hpp\ntest/test.sh\ntest/multithreaded_tests.cpp\ntest/gb_page_tests_bind_policy.cpp\ntest/environ_err_hbw_malloc_test.cpp\ntest/negative_tests.cpp\ntest/trial_generator.cpp\ntest/check.h\ntest/trial_generator.h\ntest/bat_tests.cpp\ntest/main.cpp\ntest/common.h\ntest/static_kinds_list.h\ntest/check.cpp\ntest/error_message_tests.cpp\ntest/get_arena_test.cpp\ntest/locality_test.cpp\ntest/memkind_pmem_tests.cpp\ntest/memkind_pmem_long_time_tests.cpp\ntest/memory_footprint_test.cpp\ntest/memory_manager.h\ntest/random_sizes_allocator.h\ntest/proc_stat.h\ntest/static_kinds_tests.cpp\ntest/huge_page_test.cpp\ntest/hbw_verify_function_test.cpp\ntest/hbw_detection_test.py\ntest/autohbw_test.py\ntest/trace_mechanism_test.py\ntest/memkind-afts.ts\ntest/memkind-afts-ext.ts\ntest/memkind-slts.ts\ntest/memkind-perf.ts\ntest/memkind-perf-ext.ts\ntest/memkind-pytests.ts\ntest/performance/framework.hpp\ntest/performance/framework.cpp\ntest/performance/operations.hpp\ntest/performance/perf_tests.hpp\ntest/performance/perf_tests.cpp\ntest/hbw_allocator_tests.cpp\ntest/decorator_test.cpp\ntest/decorator_test.h\ntest/alloc_performance_tests.cpp\ntest/allocator_perf_tool/AllocationSizes.hpp\ntest/allocator_perf_tool/Allocation_info.hpp\ntest/allocator_perf_tool/Allocation_info.cpp\ntest/allocator_perf_tool/Allocator.hpp\ntest/allocator_perf_tool/AllocatorFactory.hpp\ntest/allocator_perf_tool/CSVLogger.hpp\ntest/allocator_perf_tool/CommandLine.hpp\ntest/allocator_perf_tool/Configuration.hpp\ntest/allocator_perf_tool/ConsoleLog.hpp\ntest/allocator_perf_tool/FunctionCalls.hpp\ntest/allocator_perf_tool/FunctionCallsPerformanceTask.cpp\ntest/allocator_perf_tool/FunctionCallsPerformanceTask.h\ntest/allocator_perf_tool/GTestAdapter.hpp\ntest/allocator_perf_tool/Iterator.hpp\ntest/allocator_perf_tool/JemallocAllocatorWithTimer.hpp\ntest/allocator_perf_tool/Makefile\ntest/allocator_perf_tool/MemkindAllocatorWithTimer.hpp\ntest/allocator_perf_tool/Numastat.hpp\ntest/allocator_perf_tool/Runnable.hpp\ntest/allocator_perf_tool/PmemMockup.cpp\ntest/allocator_perf_tool/PmemMockup.hpp\ntest/allocator_perf_tool/ScenarioWorkload.cpp\ntest/allocator_perf_tool/ScenarioWorkload.h\ntest/allocator_perf_tool/StandardAllocatorWithTimer.hpp\ntest/allocator_perf_tool/Stats.hpp\ntest/allocator_perf_tool/StressIncreaseToMax.cpp\ntest/allocator_perf_tool/StressIncreaseToMax.h\ntest/allocator_perf_tool/Task.hpp\ntest/allocator_perf_tool/TaskFactory.hpp\ntest/allocator_perf_tool/Tests.hpp\ntest/allocator_perf_tool/Thread.hpp\ntest/allocator_perf_tool/TimerSysTime.hpp\ntest/allocator_perf_tool/VectorIterator.hpp\ntest/allocator_perf_tool/Workload.hpp\ntest/allocator_perf_tool/WrappersMacros.hpp\ntest/allocator_perf_tool/HugePageUnmap.hpp\ntest/allocator_perf_tool/HugePageOrganizer.hpp\ntest/allocator_perf_tool/HBWmallocAllocatorWithTimer.hpp\ntest/allocator_perf_tool/main.cpp\ntest/allocate_to_max_stress_test.cpp\ntest/memkind_versioning_tests.cpp\ntest/heap_manager_init_perf_test.cpp\ntest/autohbw_test_helper.c\ntest/trace_mechanism_test_helper.c\ntest/python_framework/cmd_helper.py\ntest/python_framework/huge_page_organizer.py\ntest/python_framework/__init__.py\ntest/draw_plots.py\ntest/run_alloc_benchmark.sh\ntest/alloc_benchmark.c\ntest/load_tbbmalloc_symbols.c\ntest/tbbmalloc.h\ntest/freeing_memory_segfault_test.cpp\ntest/dlopen_test.cpp\ntest/gtest_fused/gtest/gtest-all.cc\ntest/gtest_fused/gtest/gtest.h\ntest/pmem_allocator_tests.cpp\njemalloc/.appveyor.yml\njemalloc/.autom4te.cfg\njemalloc/.gitattributes\njemalloc/.gitignore\njemalloc/.travis.yml\njemalloc/COPYING\njemalloc/ChangeLog\njemalloc/INSTALL.md\njemalloc/Makefile.in\njemalloc/README\njemalloc/autogen.sh\njemalloc/bin/jemalloc-config.in\njemalloc/bin/jemalloc.sh.in\njemalloc/bin/jeprof.in\njemalloc/build-aux/config.guess\njemalloc/build-aux/config.sub\njemalloc/build-aux/install-sh\njemalloc/config.stamp.in\njemalloc/configure.ac\njemalloc/doc/html.xsl.in\njemalloc/doc/jemalloc.xml.in\njemalloc/doc/manpages.xsl.in\njemalloc/doc/stylesheet.xsl\njemalloc/include/jemalloc/internal/arena_externs.h\njemalloc/include/jemalloc/internal/arena_inlines_a.h\njemalloc/include/jemalloc/internal/arena_inlines_b.h\njemalloc/include/jemalloc/internal/arena_structs_a.h\njemalloc/include/jemalloc/internal/arena_structs_b.h\njemalloc/include/jemalloc/internal/arena_types.h\njemalloc/include/jemalloc/internal/assert.h\njemalloc/include/jemalloc/internal/atomic.h\njemalloc/include/jemalloc/internal/atomic_c11.h\njemalloc/include/jemalloc/internal/atomic_gcc_atomic.h\njemalloc/include/jemalloc/internal/atomic_gcc_sync.h\njemalloc/include/jemalloc/internal/atomic_msvc.h\njemalloc/include/jemalloc/internal/background_thread_externs.h\njemalloc/include/jemalloc/internal/background_thread_inlines.h\njemalloc/include/jemalloc/internal/background_thread_structs.h\njemalloc/include/jemalloc/internal/base_externs.h\njemalloc/include/jemalloc/internal/base_inlines.h\njemalloc/include/jemalloc/internal/base_structs.h\njemalloc/include/jemalloc/internal/base_types.h\njemalloc/include/jemalloc/internal/bit_util.h\njemalloc/include/jemalloc/internal/bitmap.h\njemalloc/include/jemalloc/internal/ckh.h\njemalloc/include/jemalloc/internal/ctl.h\njemalloc/include/jemalloc/internal/extent_dss.h\njemalloc/include/jemalloc/internal/extent_externs.h\njemalloc/include/jemalloc/internal/extent_inlines.h\njemalloc/include/jemalloc/internal/extent_mmap.h\njemalloc/include/jemalloc/internal/extent_structs.h\njemalloc/include/jemalloc/internal/extent_types.h\njemalloc/include/jemalloc/internal/hash.h\njemalloc/include/jemalloc/internal/hooks.h\njemalloc/include/jemalloc/internal/jemalloc_internal_decls.h\njemalloc/include/jemalloc/internal/jemalloc_internal_defs.h.in\njemalloc/include/jemalloc/internal/jemalloc_internal_externs.h\njemalloc/include/jemalloc/internal/jemalloc_internal_includes.h\njemalloc/include/jemalloc/internal/jemalloc_internal_inlines_a.h\njemalloc/include/jemalloc/internal/jemalloc_internal_inlines_b.h\njemalloc/include/jemalloc/internal/jemalloc_internal_inlines_c.h\njemalloc/include/jemalloc/internal/jemalloc_internal_macros.h\njemalloc/include/jemalloc/internal/jemalloc_internal_types.h\njemalloc/include/jemalloc/internal/jemalloc_preamble.h.in\njemalloc/include/jemalloc/internal/large_externs.h\njemalloc/include/jemalloc/internal/malloc_io.h\njemalloc/include/jemalloc/internal/mutex.h\njemalloc/include/jemalloc/internal/mutex_pool.h\njemalloc/include/jemalloc/internal/mutex_prof.h\njemalloc/include/jemalloc/internal/nstime.h\njemalloc/include/jemalloc/internal/pages.h\njemalloc/include/jemalloc/internal/ph.h\njemalloc/include/jemalloc/internal/private_namespace.sh\njemalloc/include/jemalloc/internal/private_symbols.sh\njemalloc/include/jemalloc/internal/prng.h\njemalloc/include/jemalloc/internal/prof_externs.h\njemalloc/include/jemalloc/internal/prof_inlines_a.h\njemalloc/include/jemalloc/internal/prof_inlines_b.h\njemalloc/include/jemalloc/internal/prof_structs.h\njemalloc/include/jemalloc/internal/prof_types.h\njemalloc/include/jemalloc/internal/public_namespace.sh\njemalloc/include/jemalloc/internal/public_unnamespace.sh\njemalloc/include/jemalloc/internal/ql.h\njemalloc/include/jemalloc/internal/qr.h\njemalloc/include/jemalloc/internal/rb.h\njemalloc/include/jemalloc/internal/rtree.h\njemalloc/include/jemalloc/internal/rtree_tsd.h\njemalloc/include/jemalloc/internal/size_classes.sh\njemalloc/include/jemalloc/internal/smoothstep.h\njemalloc/include/jemalloc/internal/smoothstep.sh\njemalloc/include/jemalloc/internal/spin.h\njemalloc/include/jemalloc/internal/stats.h\njemalloc/include/jemalloc/internal/stats_tsd.h\njemalloc/include/jemalloc/internal/sz.h\njemalloc/include/jemalloc/internal/tcache_externs.h\njemalloc/include/jemalloc/internal/tcache_inlines.h\njemalloc/include/jemalloc/internal/tcache_structs.h\njemalloc/include/jemalloc/internal/tcache_types.h\njemalloc/include/jemalloc/internal/ticker.h\njemalloc/include/jemalloc/internal/tsd.h\njemalloc/include/jemalloc/internal/tsd_generic.h\njemalloc/include/jemalloc/internal/tsd_malloc_thread_cleanup.h\njemalloc/include/jemalloc/internal/tsd_tls.h\njemalloc/include/jemalloc/internal/tsd_types.h\njemalloc/include/jemalloc/internal/tsd_win.h\njemalloc/include/jemalloc/internal/util.h\njemalloc/include/jemalloc/internal/witness.h\njemalloc/include/jemalloc/jemalloc.sh\njemalloc/include/jemalloc/jemalloc_defs.h.in\njemalloc/include/jemalloc/jemalloc_macros.h.in\njemalloc/include/jemalloc/jemalloc_mangle.sh\njemalloc/include/jemalloc/jemalloc_protos.h.in\njemalloc/include/jemalloc/jemalloc_rename.sh\njemalloc/include/jemalloc/jemalloc_typedefs.h.in\njemalloc/include/msvc_compat/C99/stdbool.h\njemalloc/include/msvc_compat/C99/stdint.h\njemalloc/include/msvc_compat/strings.h\njemalloc/include/msvc_compat/windows_extra.h\njemalloc/jemalloc.pc.in\njemalloc/m4/ax_cxx_compile_stdcxx.m4\njemalloc/msvc/ReadMe.txt\njemalloc/msvc/jemalloc_vc2015.sln\njemalloc/msvc/projects/vc2015/jemalloc/jemalloc.vcxproj\njemalloc/msvc/projects/vc2015/jemalloc/jemalloc.vcxproj.filters\njemalloc/msvc/projects/vc2015/test_threads/test_threads.cpp\njemalloc/msvc/projects/vc2015/test_threads/test_threads.h\njemalloc/msvc/projects/vc2015/test_threads/test_threads.vcxproj\njemalloc/msvc/projects/vc2015/test_threads/test_threads.vcxproj.filters\njemalloc/msvc/projects/vc2015/test_threads/test_threads_main.cpp\njemalloc/run_tests.sh\njemalloc/scripts/gen_run_tests.py\njemalloc/scripts/gen_travis.py\njemalloc/src/arena.c\njemalloc/src/background_thread.c\njemalloc/src/base.c\njemalloc/src/bitmap.c\njemalloc/src/ckh.c\njemalloc/src/ctl.c\njemalloc/src/extent.c\njemalloc/src/extent_dss.c\njemalloc/src/extent_mmap.c\njemalloc/src/hash.c\njemalloc/src/hooks.c\njemalloc/src/jemalloc.c\njemalloc/src/jemalloc_cpp.cpp\njemalloc/src/large.c\njemalloc/src/malloc_io.c\njemalloc/src/mutex.c\njemalloc/src/mutex_pool.c\njemalloc/src/nstime.c\njemalloc/src/pages.c\njemalloc/src/prng.c\njemalloc/src/prof.c\njemalloc/src/rtree.c\njemalloc/src/spin.c\njemalloc/src/stats.c\njemalloc/src/sz.c\njemalloc/src/tcache.c\njemalloc/src/ticker.c\njemalloc/src/tsd.c\njemalloc/src/witness.c\njemalloc/src/zone.c\njemalloc/test/include/test/SFMT-alti.h\njemalloc/test/include/test/SFMT-params.h\njemalloc/test/include/test/SFMT-params11213.h\njemalloc/test/include/test/SFMT-params1279.h\njemalloc/test/include/test/SFMT-params132049.h\njemalloc/test/include/test/SFMT-params19937.h\njemalloc/test/include/test/SFMT-params216091.h\njemalloc/test/include/test/SFMT-params2281.h\njemalloc/test/include/test/SFMT-params4253.h\njemalloc/test/include/test/SFMT-params44497.h\njemalloc/test/include/test/SFMT-params607.h\njemalloc/test/include/test/SFMT-params86243.h\njemalloc/test/include/test/SFMT-sse2.h\njemalloc/test/include/test/SFMT.h\njemalloc/test/include/test/btalloc.h\njemalloc/test/include/test/extent_hooks.h\njemalloc/test/include/test/jemalloc_test.h.in\njemalloc/test/include/test/jemalloc_test_defs.h.in\njemalloc/test/include/test/math.h\njemalloc/test/include/test/mq.h\njemalloc/test/include/test/mtx.h\njemalloc/test/include/test/test.h\njemalloc/test/include/test/thd.h\njemalloc/test/include/test/timer.h\njemalloc/test/integration/MALLOCX_ARENA.c\njemalloc/test/integration/aligned_alloc.c\njemalloc/test/integration/allocated.c\njemalloc/test/integration/extent.c\njemalloc/test/integration/extent.sh\njemalloc/test/integration/mallocx.c\njemalloc/test/integration/mallocx.sh\njemalloc/test/integration/overflow.c\njemalloc/test/integration/posix_memalign.c\njemalloc/test/integration/rallocx.c\njemalloc/test/integration/sdallocx.c\njemalloc/test/integration/thread_arena.c\njemalloc/test/integration/thread_tcache_enabled.c\njemalloc/test/integration/xallocx.c\njemalloc/test/integration/xallocx.sh\njemalloc/test/src/SFMT.c\njemalloc/test/src/btalloc.c\njemalloc/test/src/btalloc_0.c\njemalloc/test/src/btalloc_1.c\njemalloc/test/src/math.c\njemalloc/test/src/mq.c\njemalloc/test/src/mtx.c\njemalloc/test/src/test.c\njemalloc/test/src/thd.c\njemalloc/test/src/timer.c\njemalloc/test/stress/microbench.c\njemalloc/test/test.sh.in\njemalloc/test/unit/SFMT.c\njemalloc/test/unit/a0.c\njemalloc/test/unit/arena_reset.c\njemalloc/test/unit/arena_reset_prof.c\njemalloc/test/unit/arena_reset_prof.sh\njemalloc/test/unit/atomic.c\njemalloc/test/unit/background_thread.c\njemalloc/test/unit/base.c\njemalloc/test/unit/bit_util.c\njemalloc/test/unit/bitmap.c\njemalloc/test/unit/ckh.c\njemalloc/test/unit/decay.c\njemalloc/test/unit/decay.sh\njemalloc/test/unit/extent_quantize.c\njemalloc/test/unit/fork.c\njemalloc/test/unit/hash.c\njemalloc/test/unit/hooks.c\njemalloc/test/unit/junk.c\njemalloc/test/unit/junk.sh\njemalloc/test/unit/junk_alloc.c\njemalloc/test/unit/junk_alloc.sh\njemalloc/test/unit/junk_free.c\njemalloc/test/unit/junk_free.sh\njemalloc/test/unit/mallctl.c\njemalloc/test/unit/malloc_io.c\njemalloc/test/unit/math.c\njemalloc/test/unit/mq.c\njemalloc/test/unit/mtx.c\njemalloc/test/unit/nstime.c\njemalloc/test/unit/pack.c\njemalloc/test/unit/pack.sh\njemalloc/test/unit/pages.c\njemalloc/test/unit/ph.c\njemalloc/test/unit/prng.c\njemalloc/test/unit/prof_accum.c\njemalloc/test/unit/prof_accum.sh\njemalloc/test/unit/prof_active.c\njemalloc/test/unit/prof_active.sh\njemalloc/test/unit/prof_gdump.c\njemalloc/test/unit/prof_gdump.sh\njemalloc/test/unit/prof_idump.c\njemalloc/test/unit/prof_idump.sh\njemalloc/test/unit/prof_reset.c\njemalloc/test/unit/prof_reset.sh\njemalloc/test/unit/prof_tctx.c\njemalloc/test/unit/prof_tctx.sh\njemalloc/test/unit/prof_thread_name.c\njemalloc/test/unit/prof_thread_name.sh\njemalloc/test/unit/ql.c\njemalloc/test/unit/qr.c\njemalloc/test/unit/rb.c\njemalloc/test/unit/retained.c\njemalloc/test/unit/rtree.c\njemalloc/test/unit/size_classes.c\njemalloc/test/unit/slab.c\njemalloc/test/unit/smoothstep.c\njemalloc/test/unit/spin.c\njemalloc/test/unit/stats.c\njemalloc/test/unit/stats_print.c\njemalloc/test/unit/ticker.c\njemalloc/test/unit/tsd.c\njemalloc/test/unit/witness.c\njemalloc/test/unit/zero.c\njemalloc/test/unit/zero.sh\nbuild_jemalloc.sh\n"
  },
  {
    "path": "deps/memkind/src/Makefile.am",
    "content": "#\n#  Copyright (C) 2014 - 2018 Intel Corporation.\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#  1. Redistributions of source code must retain the above copyright notice(s),\n#     this list of conditions and the following disclaimer.\n#  2. Redistributions in binary form must reproduce the above copyright notice(s),\n#     this list of conditions and the following disclaimer in the documentation\n#     and/or other materials provided with the distribution.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n#  EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n#  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n#\n\nACLOCAL_AMFLAGS = -I m4\nAM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/jemalloc/obj/include\n\nlib_LTLIBRARIES = libmemkind.la\n\nlibmemkind_la_SOURCES = src/hbwmalloc.c \\\n                        src/heap_manager.c \\\n                        src/memkind_arena.c \\\n                        src/memkind.c \\\n                        src/memkind_default.c \\\n                        src/memkind_gbtlb.c \\\n                        src/memkind_hbw.c \\\n                        src/memkind_regular.c \\\n                        src/memkind_hugetlb.c \\\n                        src/memkind_pmem.c \\\n                        src/memkind_interleave.c \\\n                        src/memkind_log.c \\\n                        src/tbb_wrapper.c \\\n                        # end\n\n\nlibmemkind_la_LIBADD = jemalloc/obj/lib/libjemalloc_pic.a\nlibmemkind_la_LDFLAGS = -version-info 0:1:0 -ldl\ninclude_HEADERS = include/hbwmalloc.h \\\n                  include/hbw_allocator.h \\\n                  include/pmem_allocator.h \\\n                  include/memkind.h \\\n                  include/memkind_deprecated.h \\\n                  # end\n\nnonstandardincludedir = $(includedir)/memkind/internal\nnonstandardinclude_HEADERS = include/memkind/internal/memkind_arena.h \\\n                  include/memkind/internal/heap_manager.h \\\n                  include/memkind/internal/memkind_default.h \\\n                  include/memkind/internal/memkind_gbtlb.h \\\n                  include/memkind/internal/memkind_hbw.h \\\n                  include/memkind/internal/memkind_regular.h \\\n                  include/memkind/internal/memkind_hugetlb.h \\\n                  include/memkind/internal/memkind_interleave.h \\\n                  include/memkind/internal/memkind_pmem.h \\\n                  include/memkind/internal/memkind_private.h \\\n                  include/memkind/internal/memkind_log.h \\\n                  include/memkind/internal/tbb_wrapper.h \\\n                  include/memkind/internal/tbb_mem_pool_policy.h \\\n                  # end\n\nEXTRA_DIST = astyle.sh \\\n             autogen.sh \\\n             build.sh \\\n             build_jemalloc.sh \\\n             examples/README \\\n             test/test.sh \\\n             VERSION \\\n             CONTRIBUTING \\\n             memkind-$(VERSION).spec \\\n             memkind.spec.mk \\\n             jemalloc/.appveyor.yml \\\n             jemalloc/.autom4te.cfg \\\n             jemalloc/.gitattributes \\\n             jemalloc/.gitignore \\\n             jemalloc/.travis.yml \\\n             jemalloc/COPYING \\\n             jemalloc/ChangeLog \\\n             jemalloc/INSTALL.md \\\n             jemalloc/Makefile.in \\\n             jemalloc/README \\\n             jemalloc/autogen.sh \\\n             jemalloc/bin/jemalloc-config.in \\\n             jemalloc/bin/jemalloc.sh.in \\\n             jemalloc/bin/jeprof.in \\\n             jemalloc/build-aux/config.guess \\\n             jemalloc/build-aux/config.sub \\\n             jemalloc/build-aux/install-sh \\\n             jemalloc/config.stamp.in \\\n             jemalloc/configure.ac \\\n             jemalloc/doc/html.xsl.in \\\n             jemalloc/doc/jemalloc.xml.in \\\n             jemalloc/doc/manpages.xsl.in \\\n             jemalloc/doc/stylesheet.xsl \\\n             jemalloc/include/jemalloc/internal/arena_externs.h \\\n             jemalloc/include/jemalloc/internal/arena_inlines_a.h \\\n             jemalloc/include/jemalloc/internal/arena_inlines_b.h \\\n             jemalloc/include/jemalloc/internal/arena_structs_a.h \\\n             jemalloc/include/jemalloc/internal/arena_structs_b.h \\\n             jemalloc/include/jemalloc/internal/arena_types.h \\\n             jemalloc/include/jemalloc/internal/assert.h \\\n             jemalloc/include/jemalloc/internal/atomic.h \\\n             jemalloc/include/jemalloc/internal/atomic_c11.h \\\n             jemalloc/include/jemalloc/internal/atomic_gcc_atomic.h \\\n             jemalloc/include/jemalloc/internal/atomic_gcc_sync.h \\\n             jemalloc/include/jemalloc/internal/atomic_msvc.h \\\n             jemalloc/include/jemalloc/internal/background_thread_externs.h \\\n             jemalloc/include/jemalloc/internal/background_thread_inlines.h \\\n             jemalloc/include/jemalloc/internal/background_thread_structs.h \\\n             jemalloc/include/jemalloc/internal/base_externs.h \\\n             jemalloc/include/jemalloc/internal/base_inlines.h \\\n             jemalloc/include/jemalloc/internal/base_structs.h \\\n             jemalloc/include/jemalloc/internal/base_types.h \\\n             jemalloc/include/jemalloc/internal/bit_util.h \\\n             jemalloc/include/jemalloc/internal/bitmap.h \\\n             jemalloc/include/jemalloc/internal/ckh.h \\\n             jemalloc/include/jemalloc/internal/ctl.h \\\n             jemalloc/include/jemalloc/internal/extent_dss.h \\\n             jemalloc/include/jemalloc/internal/extent_externs.h \\\n             jemalloc/include/jemalloc/internal/extent_inlines.h \\\n             jemalloc/include/jemalloc/internal/extent_mmap.h \\\n             jemalloc/include/jemalloc/internal/extent_structs.h \\\n             jemalloc/include/jemalloc/internal/extent_types.h \\\n             jemalloc/include/jemalloc/internal/hash.h \\\n             jemalloc/include/jemalloc/internal/hooks.h \\\n             jemalloc/include/jemalloc/internal/jemalloc_internal_decls.h \\\n             jemalloc/include/jemalloc/internal/jemalloc_internal_defs.h.in \\\n             jemalloc/include/jemalloc/internal/jemalloc_internal_externs.h \\\n             jemalloc/include/jemalloc/internal/jemalloc_internal_includes.h \\\n             jemalloc/include/jemalloc/internal/jemalloc_internal_inlines_a.h \\\n             jemalloc/include/jemalloc/internal/jemalloc_internal_inlines_b.h \\\n             jemalloc/include/jemalloc/internal/jemalloc_internal_inlines_c.h \\\n             jemalloc/include/jemalloc/internal/jemalloc_internal_macros.h \\\n             jemalloc/include/jemalloc/internal/jemalloc_internal_types.h \\\n             jemalloc/include/jemalloc/internal/jemalloc_preamble.h.in \\\n             jemalloc/include/jemalloc/internal/large_externs.h \\\n             jemalloc/include/jemalloc/internal/malloc_io.h \\\n             jemalloc/include/jemalloc/internal/mutex.h \\\n             jemalloc/include/jemalloc/internal/mutex_pool.h \\\n             jemalloc/include/jemalloc/internal/mutex_prof.h \\\n             jemalloc/include/jemalloc/internal/nstime.h \\\n             jemalloc/include/jemalloc/internal/pages.h \\\n             jemalloc/include/jemalloc/internal/ph.h \\\n             jemalloc/include/jemalloc/internal/private_namespace.sh \\\n             jemalloc/include/jemalloc/internal/private_symbols.sh \\\n             jemalloc/include/jemalloc/internal/prng.h \\\n             jemalloc/include/jemalloc/internal/prof_externs.h \\\n             jemalloc/include/jemalloc/internal/prof_inlines_a.h \\\n             jemalloc/include/jemalloc/internal/prof_inlines_b.h \\\n             jemalloc/include/jemalloc/internal/prof_structs.h \\\n             jemalloc/include/jemalloc/internal/prof_types.h \\\n             jemalloc/include/jemalloc/internal/public_namespace.sh \\\n             jemalloc/include/jemalloc/internal/public_unnamespace.sh \\\n             jemalloc/include/jemalloc/internal/ql.h \\\n             jemalloc/include/jemalloc/internal/qr.h \\\n             jemalloc/include/jemalloc/internal/rb.h \\\n             jemalloc/include/jemalloc/internal/rtree.h \\\n             jemalloc/include/jemalloc/internal/rtree_tsd.h \\\n             jemalloc/include/jemalloc/internal/size_classes.sh \\\n             jemalloc/include/jemalloc/internal/smoothstep.h \\\n             jemalloc/include/jemalloc/internal/smoothstep.sh \\\n             jemalloc/include/jemalloc/internal/spin.h \\\n             jemalloc/include/jemalloc/internal/stats.h \\\n             jemalloc/include/jemalloc/internal/stats_tsd.h \\\n             jemalloc/include/jemalloc/internal/sz.h \\\n             jemalloc/include/jemalloc/internal/tcache_externs.h \\\n             jemalloc/include/jemalloc/internal/tcache_inlines.h \\\n             jemalloc/include/jemalloc/internal/tcache_structs.h \\\n             jemalloc/include/jemalloc/internal/tcache_types.h \\\n             jemalloc/include/jemalloc/internal/ticker.h \\\n             jemalloc/include/jemalloc/internal/tsd.h \\\n             jemalloc/include/jemalloc/internal/tsd_generic.h \\\n             jemalloc/include/jemalloc/internal/tsd_malloc_thread_cleanup.h \\\n             jemalloc/include/jemalloc/internal/tsd_tls.h \\\n             jemalloc/include/jemalloc/internal/tsd_types.h \\\n             jemalloc/include/jemalloc/internal/tsd_win.h \\\n             jemalloc/include/jemalloc/internal/util.h \\\n             jemalloc/include/jemalloc/internal/witness.h \\\n             jemalloc/include/jemalloc/jemalloc.sh \\\n             jemalloc/include/jemalloc/jemalloc_defs.h.in \\\n             jemalloc/include/jemalloc/jemalloc_macros.h.in \\\n             jemalloc/include/jemalloc/jemalloc_mangle.sh \\\n             jemalloc/include/jemalloc/jemalloc_protos.h.in \\\n             jemalloc/include/jemalloc/jemalloc_rename.sh \\\n             jemalloc/include/jemalloc/jemalloc_typedefs.h.in \\\n             jemalloc/include/msvc_compat/C99/stdbool.h \\\n             jemalloc/include/msvc_compat/C99/stdint.h \\\n             jemalloc/include/msvc_compat/strings.h \\\n             jemalloc/include/msvc_compat/windows_extra.h \\\n             jemalloc/jemalloc.pc.in \\\n             jemalloc/m4/ax_cxx_compile_stdcxx.m4 \\\n             jemalloc/msvc/ReadMe.txt \\\n             jemalloc/msvc/jemalloc_vc2015.sln \\\n             jemalloc/msvc/projects/vc2015/jemalloc/jemalloc.vcxproj \\\n             jemalloc/msvc/projects/vc2015/jemalloc/jemalloc.vcxproj.filters \\\n             jemalloc/msvc/projects/vc2015/test_threads/test_threads.cpp \\\n             jemalloc/msvc/projects/vc2015/test_threads/test_threads.h \\\n             jemalloc/msvc/projects/vc2015/test_threads/test_threads.vcxproj \\\n             jemalloc/msvc/projects/vc2015/test_threads/test_threads.vcxproj.filters \\\n             jemalloc/msvc/projects/vc2015/test_threads/test_threads_main.cpp \\\n             jemalloc/run_tests.sh \\\n             jemalloc/scripts/gen_run_tests.py \\\n             jemalloc/scripts/gen_travis.py \\\n             jemalloc/src/arena.c \\\n             jemalloc/src/background_thread.c \\\n             jemalloc/src/base.c \\\n             jemalloc/src/bitmap.c \\\n             jemalloc/src/ckh.c \\\n             jemalloc/src/ctl.c \\\n             jemalloc/src/extent.c \\\n             jemalloc/src/extent_dss.c \\\n             jemalloc/src/extent_mmap.c \\\n             jemalloc/src/hash.c \\\n             jemalloc/src/hooks.c \\\n             jemalloc/src/jemalloc.c \\\n             jemalloc/src/jemalloc_cpp.cpp \\\n             jemalloc/src/large.c \\\n             jemalloc/src/malloc_io.c \\\n             jemalloc/src/mutex.c \\\n             jemalloc/src/mutex_pool.c \\\n             jemalloc/src/nstime.c \\\n             jemalloc/src/pages.c \\\n             jemalloc/src/prng.c \\\n             jemalloc/src/prof.c \\\n             jemalloc/src/rtree.c \\\n             jemalloc/src/spin.c \\\n             jemalloc/src/stats.c \\\n             jemalloc/src/sz.c \\\n             jemalloc/src/tcache.c \\\n             jemalloc/src/ticker.c \\\n             jemalloc/src/tsd.c \\\n             jemalloc/src/witness.c \\\n             jemalloc/src/zone.c \\\n             jemalloc/test/include/test/SFMT-alti.h \\\n             jemalloc/test/include/test/SFMT-params.h \\\n             jemalloc/test/include/test/SFMT-params11213.h \\\n             jemalloc/test/include/test/SFMT-params1279.h \\\n             jemalloc/test/include/test/SFMT-params132049.h \\\n             jemalloc/test/include/test/SFMT-params19937.h \\\n             jemalloc/test/include/test/SFMT-params216091.h \\\n             jemalloc/test/include/test/SFMT-params2281.h \\\n             jemalloc/test/include/test/SFMT-params4253.h \\\n             jemalloc/test/include/test/SFMT-params44497.h \\\n             jemalloc/test/include/test/SFMT-params607.h \\\n             jemalloc/test/include/test/SFMT-params86243.h \\\n             jemalloc/test/include/test/SFMT-sse2.h \\\n             jemalloc/test/include/test/SFMT.h \\\n             jemalloc/test/include/test/btalloc.h \\\n             jemalloc/test/include/test/extent_hooks.h \\\n             jemalloc/test/include/test/jemalloc_test.h.in \\\n             jemalloc/test/include/test/jemalloc_test_defs.h.in \\\n             jemalloc/test/include/test/math.h \\\n             jemalloc/test/include/test/mq.h \\\n             jemalloc/test/include/test/mtx.h \\\n             jemalloc/test/include/test/test.h \\\n             jemalloc/test/include/test/thd.h \\\n             jemalloc/test/include/test/timer.h \\\n             jemalloc/test/integration/MALLOCX_ARENA.c \\\n             jemalloc/test/integration/aligned_alloc.c \\\n             jemalloc/test/integration/allocated.c \\\n             jemalloc/test/integration/extent.c \\\n             jemalloc/test/integration/extent.sh \\\n             jemalloc/test/integration/mallocx.c \\\n             jemalloc/test/integration/mallocx.sh \\\n             jemalloc/test/integration/overflow.c \\\n             jemalloc/test/integration/posix_memalign.c \\\n             jemalloc/test/integration/rallocx.c \\\n             jemalloc/test/integration/sdallocx.c \\\n             jemalloc/test/integration/thread_arena.c \\\n             jemalloc/test/integration/thread_tcache_enabled.c \\\n             jemalloc/test/integration/xallocx.c \\\n             jemalloc/test/integration/xallocx.sh \\\n             jemalloc/test/src/SFMT.c \\\n             jemalloc/test/src/btalloc.c \\\n             jemalloc/test/src/btalloc_0.c \\\n             jemalloc/test/src/btalloc_1.c \\\n             jemalloc/test/src/math.c \\\n             jemalloc/test/src/mq.c \\\n             jemalloc/test/src/mtx.c \\\n             jemalloc/test/src/test.c \\\n             jemalloc/test/src/thd.c \\\n             jemalloc/test/src/timer.c \\\n             jemalloc/test/stress/microbench.c \\\n             jemalloc/test/test.sh.in \\\n             jemalloc/test/unit/SFMT.c \\\n             jemalloc/test/unit/a0.c \\\n             jemalloc/test/unit/arena_reset.c \\\n             jemalloc/test/unit/arena_reset_prof.c \\\n             jemalloc/test/unit/arena_reset_prof.sh \\\n             jemalloc/test/unit/atomic.c \\\n             jemalloc/test/unit/background_thread.c \\\n             jemalloc/test/unit/base.c \\\n             jemalloc/test/unit/bit_util.c \\\n             jemalloc/test/unit/bitmap.c \\\n             jemalloc/test/unit/ckh.c \\\n             jemalloc/test/unit/decay.c \\\n             jemalloc/test/unit/decay.sh \\\n             jemalloc/test/unit/extent_quantize.c \\\n             jemalloc/test/unit/fork.c \\\n             jemalloc/test/unit/hash.c \\\n             jemalloc/test/unit/hooks.c \\\n             jemalloc/test/unit/junk.c \\\n             jemalloc/test/unit/junk.sh \\\n             jemalloc/test/unit/junk_alloc.c \\\n             jemalloc/test/unit/junk_alloc.sh \\\n             jemalloc/test/unit/junk_free.c \\\n             jemalloc/test/unit/junk_free.sh \\\n             jemalloc/test/unit/mallctl.c \\\n             jemalloc/test/unit/malloc_io.c \\\n             jemalloc/test/unit/math.c \\\n             jemalloc/test/unit/mq.c \\\n             jemalloc/test/unit/mtx.c \\\n             jemalloc/test/unit/nstime.c \\\n             jemalloc/test/unit/pack.c \\\n             jemalloc/test/unit/pack.sh \\\n             jemalloc/test/unit/pages.c \\\n             jemalloc/test/unit/ph.c \\\n             jemalloc/test/unit/prng.c \\\n             jemalloc/test/unit/prof_accum.c \\\n             jemalloc/test/unit/prof_accum.sh \\\n             jemalloc/test/unit/prof_active.c \\\n             jemalloc/test/unit/prof_active.sh \\\n             jemalloc/test/unit/prof_gdump.c \\\n             jemalloc/test/unit/prof_gdump.sh \\\n             jemalloc/test/unit/prof_idump.c \\\n             jemalloc/test/unit/prof_idump.sh \\\n             jemalloc/test/unit/prof_reset.c \\\n             jemalloc/test/unit/prof_reset.sh \\\n             jemalloc/test/unit/prof_tctx.c \\\n             jemalloc/test/unit/prof_tctx.sh \\\n             jemalloc/test/unit/prof_thread_name.c \\\n             jemalloc/test/unit/prof_thread_name.sh \\\n             jemalloc/test/unit/ql.c \\\n             jemalloc/test/unit/qr.c \\\n             jemalloc/test/unit/rb.c \\\n             jemalloc/test/unit/retained.c \\\n             jemalloc/test/unit/rtree.c \\\n             jemalloc/test/unit/size_classes.c \\\n             jemalloc/test/unit/slab.c \\\n             jemalloc/test/unit/smoothstep.c \\\n             jemalloc/test/unit/spin.c \\\n             jemalloc/test/unit/stats.c \\\n             jemalloc/test/unit/stats_print.c \\\n             jemalloc/test/unit/ticker.c \\\n             jemalloc/test/unit/tsd.c \\\n             jemalloc/test/unit/witness.c \\\n             jemalloc/test/unit/zero.c \\\n             jemalloc/test/unit/zero.sh \\\n             build_jemalloc.sh \\\n             # end\n\ndist_doc_DATA = README COPYING VERSION\ndist_man_MANS = man/memkind-hbw-nodes.1 \\\n                man/hbwmalloc.3 \\\n                man/memkind.3 \\\n                man/memkind_arena.3 \\\n                man/memkind_default.3 \\\n                man/memkind_hbw.3 \\\n                man/memkind_hugetlb.3 \\\n                man/memkind_pmem.3 \\\n                man/hbwallocator.3 \\\n                man/pmemallocator.3 \\\n                man/autohbw.7 \\\n                # end\n\nCLEANFILES = memkind-$(VERSION).spec\nDISTCLEANFILES = VERSION\n\nbin_PROGRAMS = memkind-hbw-nodes\n\nmemkind_hbw_nodes_SOURCES = src/memkind-hbw-nodes.c\nmemkind_hbw_nodes_LDADD = libmemkind.la\n\nbin_SCRIPTS =\ncheck_PROGRAMS =\nnoinst_PROGRAMS =\nnoinst_LTLIBRARIES =\nnoinst_HEADERS =\nTESTS =\n\nreq_flags = -fvisibility=hidden -Wall -Werror -msse4.2 -D_GNU_SOURCE -DMEMKIND_INTERNAL_API -DJE_PREFIX=$(JE_PREFIX)\n\nAM_CFLAGS = $(req_flags)\nAM_CXXFLAGS = $(req_flags)\n\n.PHONY: checkprogs\n\n# create libtool .lo files from jemalloc objects\n$(jemalloc_objects): jemalloc/obj/src/%.lo: jemalloc/obj/src/%.pic.o jemalloc/obj/src/%.o\n\t@echo \"# $@ - a libtool object file\" > $@\n\t@echo \"# Generated by \"`libtool --version | head -n 1` >> $@\n\t@echo \"# Actually generated by memkind build system spoofing libtool\" >> $@\n\t@echo \"pic_object='$*.pic.o'\" >> $@\n\t@echo \"non_pic_object='$*.o'\" >> $@\n\n# build check programs without running tests\ncheckprogs: libmemkind.la $(check_PROGRAMS)\n\nmemkind-$(VERSION).spec:\n\t$(MAKE) version=\"$(VERSION)\" -f memkind.spec.mk $@\n\nrpm: dist\n\t$(MAKE) version=\"$(VERSION)\" CPPFLAGS=\"$(CPPFLAGS)\" LDFLAGS=\"$(LDFLAGS)\" \\\n\t\t-f memkind.spec.mk $@\n\nall: static_lib\n\n# the script merge memkind and jemalloc libraries into one static library.\ndefine ar_prog\ncreate libmemkind.a\\n\\\naddlib .libs/libmemkind.a\\n\\\naddlib jemalloc/obj/lib/libjemalloc_pic.a\\n\\\nsave\\n\\\nend\nendef\n\nstatic_lib: jemalloc/obj/lib/libjemalloc_pic.a libmemkind.la\n\tbash -c \"ar -M < <(echo -e '$(ar_prog)')\"\n\tcp libmemkind.a .libs/\n\trm libmemkind.a\n\n\ninclude autohbw/Makefile.mk\ninclude test/Makefile.mk\ninclude examples/Makefile.mk\ninclude src/Makefile.mk\n"
  },
  {
    "path": "deps/memkind/src/NEWS",
    "content": "OPEN DEVELOPMENT\n================\n\nWe are moving to a more open development model with memkind.\n\n- The memkind github organization:\n  https://github.com/memkind\n\n- Rather than posting bulk patches at time of tag, we are posting more\n  incremental patches with finer grain commit details.\n\n- There is now a \"dev\" branch in the memkind repository and a\n  \"memkind-dev\" branch in the memkind fork of the jemalloc repository.\n  These will be used for open development work.\n\n- There is a memkind web page here:\n  http://memkind.github.io/memkind\n\n- We have posted an architecture document here:\n  http://memkind.github.io/memkind/memkind_arch_20150318.pdf\n\n- We invite you to join the memkind mailing list:\n  https://lists.01.org/mailman/listinfo/memkind\n\n- Feel free to open a github issue, especially if there is a TODO item\n  that you would like to prioritize.\n\n- We have posted source and binary RPMs for several distributions here:\n  http://download.opensuse.org/repositories/home:/cmcantalupo/\n\n- memkind is also avaiable in Fedora package repository (devel, EPEL7):\n  https://admin.fedoraproject.org/pkgdb/package/memkind/\n\n- Pull requests are welcome.\n\n- White space formatting generally conforms to\n  $ astyle --style=linux --indent=spaces=4 -S --max-continuation-indent=80 \\\n           --max-code-length=80 --break-after-logical --indent-namespaces -z2 \\\n           --align-pointer=name\n\n- For more information about astyle:\n  http://astyle.sourceforge.net/\n"
  },
  {
    "path": "deps/memkind/src/PULL_REQUEST_TEMPLATE.md",
    "content": "<!--- Provide a general summary of your changes in the Title above -->\n\n### Description\n<!--- Describe your changes in detail -->\n<!--- If it fixes an open issue, please link to the issue here. -->\n\n### Types of changes\n<!--- Put an `x` in the box(es) that apply -->\n\n- [ ] Bugfix (non-breaking change which fixes issue linked in Description above)\n- [ ] New feature (non-breaking change which adds functionality)\n- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)\n- [ ] Documentation (correction or otherwise)\n- [ ] Cosmetics (whitespace, appearance)\n- [ ] Other\n\n### Checklist\n<!--- Put an `x` in the box(es) that apply -->\n\n- [ ] Code compiles without errors\n- [ ] All tests pass locally with my changes (see TESTING section in CONTRIBUTING file)\n- [ ] Created tests which will fail without the change (if possible)\n- [ ] Extended the README/documentation (if necessary)\n- [ ] All newly added files have proprietary license (if necessary)\n- [ ] All newly added files are referenced in MANIFEST files (if necessary)\n\n## Further comments\n<!--- If this is a relatively large or complex change, kick off the discussion by explaining why you -->\n<!--- choose the solution you did and what alternatives you considered, etc... -->\n"
  },
  {
    "path": "deps/memkind/src/README",
    "content": "MEMKIND\n=======\n\n[![Build Status](https://travis-ci.org/memkind/memkind.svg)](https://travis-ci.org/memkind/memkind)\n[![MEMKIND version](https://img.shields.io/github/tag/memkind/memkind.svg)](https://github.com/memkind/memkind/releases/latest)\n[![Coverage Status](http://codecov.io/github/memkind/memkind/coverage.svg?branch=master)](http://codecov.io/gh/memkind/memkind?branch=master)\n\nDISCLAIMER\n----------\nSEE COPYING FILE FOR LICENSE INFORMATION.\n\nTHIS SOFTWARE IS PROVIDED AS A DEVELOPMENT SNAPSHOT TO AID\nCOLLABORATION AND WAS NOT ISSUED AS A RELEASED PRODUCT BY INTEL.\n\n\nLAST UPDATE\n-----------\nKrzysztof Kulakowski <krzysztof.kulakowski@intel.com>\nTuesday Mar 1 2016\n\nSUMMARY\n-------\nThe memkind library is a user extensible heap manager built on top of\njemalloc which enables control of memory characteristics and a\npartitioning of the heap between kinds of memory.  The kinds of memory\nare defined by operating system memory policies that have been applied\nto virtual address ranges.  Memory characteristics supported by\nmemkind without user extension include control of NUMA and page size\nfeatures.  The jemalloc non-standard interface has been extended to\nenable specialized arenas to make requests for virtual memory from the\noperating system through the memkind partition interface.  Through the\nother memkind interfaces the user can control and extend memory\npartition features and allocate memory while selecting enabled\nfeatures.\n\nAPI\n---\nThe memkind library delivers two interfaces:\n * hbwmalloc.h - recommended for high-bandwidth memory use cases (stable)\n * memkind.h - generic interface for more complex use cases (partially unstable)\n\nFor more detailed information about those interfaces see\ncorresponding manpages (located in man/ subdir):\n\n    man memkind\n\nor\n\n    man hbwmalloc\n\nBUILD REQUIREMENTS\n------------------\nTo build the memkind libraries on a RHEL Linux system first install\nthe required packages with the following command:\n\n        sudo yum install numactl-devel gcc-c++ unzip\n\nOn a SLES Linux system install the dependencies with the following\ncommand:\n\n        sudo zypper install libnuma-devel gcc-c++ unzip\n\nThe memkind library uses the GNU autotools (Autoconf, Automake,\nLibtool and m4) for configuration and build.  The configure scripts\nand gtest source code are distributed with the source tarball included\nin the source RPM, and this tarball is created with the memkind \"make\ndist\" target.  In contrast to the distributed source tarball, the git\nrepository does not include any generated files nor the gtest source\ncode.  For this reason some additional steps are required when\nbuilding from a checkout of the git repo.  Those steps include running\nthe bash script called \"autogen.sh\" prior to configure.  This script\nwill populate a VERSION file based on \"git describe\", and use\nautoreconf to generate a configure script.  The gtest library is\nrequired for building the test content.  This is downloaded\nautomatically prior to building the test content from the googlecode\nwebsite based on a target describe in memkind/test/Makefile.mk.\n\nBUILDING\n--------\n\na) Building jemalloc\n\nThe memkind library has a dependency on a related fork of jemalloc.\n\nThe jemalloc source was forked from jemalloc version 5.0.  This source tree\nis located within the jemalloc subdirectory of the memkind source.  The jemalloc\nsource code has been kept close to the original form, and in particular\nthe build system has been lightly modified.\nThe developer must configure and build jemalloc prior to configuring\nand building memkind.  You can do that using included shell script:\n\n    export JE_PREFIX=jemk_\n    ./build_jemalloc.sh\n\nAlternatively you can follow this step-by-step instruction:\n\n    cd jemalloc\n    autoconf\n    mkdir obj\n    cd obj\n    ../configure --enable-autogen --with-jemalloc-prefix=jemk_ --without-export \\\n                 --disable-stats --disable-fill \\\n                 --with-malloc-conf=\"narenas:256,lg_tcache_max:12\"\n    make\n    cd ../..\n\nNote: JE_PREFIX can be set to arbitrary value, including empty one.\n\nb) Building memkind\n\nBuilding and installing memkind can be as simple as typing the following while\nin the root directory of the source tree:\n\n    ./build.sh\n    make install\n\nAlternatively you can follow this step-by-step instruction:\n\n    export JE_PREFIX=jemk_\n    ./autogen.sh\n    ./configure\n    make\n    make install\n\nthis should configure, build, and install this package.\n\nSee the output of:\n\n    ./configure --help\n\nfor more information about either the memkind or the jemalloc\nconfiguration options.  Some useful information about building with autotools\ncan also be found in the INSTALL file.\n\n**Important Notes:**\n\nIf you are using build.sh script and later want to call 'make' command directly,\nthen you need to call firstly:\n\n    export JE_PREFIX=jemk_\n\notherwise you will get an error like:\n\n    undefined reference to mallocx\n\nRUN REQUIREMENTS\n----------------\nRequires kernel patch introduced in Linux v3.11 that impacts\nfunctionality of the NUMA system calls.  This is patch is commit\n3964acd0dbec123aa0a621973a2a0580034b4788 in the linux-stable git\nrepository from kernel.org.  Red Hat has back-ported this patch to the\nv3.10 kernel in the RHEL 7.0 GA release, so RHEL 7.0 onward supports\nmemkind even though this kernel version predates v3.11.\n\nFunctionality related to hugepages allocation require patches\ne0ec90ee7e6f6cbaa6d59ffb48d2a7af5e80e61d and\n099730d67417dfee273e9b10ac2560ca7fac7eb9 from kernel org. Without them\nphysical memory may end up being located on incorrect NUMA node.\n\nApplications using the memkind library require that libnuma and\nlibpthread be available for dynamic linking at run time.  Both of\nthese libraries should be available by default, but they can be\ninstalled on RHEL Linux with the following command:\n\n    sudo yum install pthread numactl\n\nand on a SLES Linux system with:\n\n    sudo zypper install pthread libnuma\n\nTo use the interfaces for obtaining 2MB pages please be sure\nto follow the instructions here:\n    https://www.kernel.org/doc/Documentation/vm/hugetlbpage.txt\nand pay particular attention to the use of the procfs files:\n    /proc/sys/vm/nr_hugepages\n    /proc/sys/vm/nr_overcommit_hugepages\nfor enabling the kernel's huge page pool.\n\nTo use the file-backed kind of memory (PMEM), please be sure that\nfilesystem which is used for PMEM kind supports FALLOC_FL_PUNCH_HOLE flag:\n    http://man7.org/linux/man-pages/man2/fallocate.2.html\n\nSETTING LOGGING MECHANISM\n-------------------------\nIn memkind library logging mechanism could be enabled by setting MEMKIND_DEBUG\nenvironment variable. Setting MEMKIND_DEBUG to \"1\" enables printing messages\nlike errors and general information about environment to stderr.\n\nSETTING HEAP MANAGER\n--------------------\nIn memkind library heap management can be adjusted with MEMKIND_HEAP_MANAGER\nenvironment variable, which allows for switching to one of the available\nheap managers.\nValues:\n    JEMALLOC – sets the jemalloc heap manager\n    TBB – sets Intel Threading Building Blocks heap manager. This option requires installed\n    Intel Threading Building Blocks library.\nIf the MEMKIND_HEAP_MANAGER is not set then the jemalloc heap manager will be used by default.\n\nTESTING\n-------\nAll existing tests pass. For more information on how to execute tests\nsee the CONTRIBUTING file.\n\nWhen tests are run on a NUMA platform without high bandwidth memory\nthe MEMKIND_HBW_NODES environment variable is used in conjunction with\n\"numactl --membind\" to force standard allocations to one NUMA node and\nhigh bandwidth allocations through a different NUMA node.  See next\nsection for more details.\n\n\nSIMULATE HIGH BANDWIDTH MEMORY\n------------------------------\nA method for testing for the benefit of high bandwidth memory on a\ndual socket Intel(R) Xeon(TM) system is to use the QPI bus to simulate\nslow memory.  This is not an accurate model of the bandwidth and\nlatency characteristics of the Intel's 2nd generation Intel(R) Xeon Phi(TM)\nProduct Family on package memory, but is a reasonable way to determine\nwhich data structures rely critically on bandwidth.\n\nIf the application a.out has been modified to use high bandwidth\nmemory with the memkind library then this can be done with numactl as\nfollows with the bash shell:\n\n    export MEMKIND_HBW_NODES=0\n    numactl --membind=1 --cpunodebind=0 a.out\n\nor with csh:\n\n    setenv MEMKIND_HBW_NODES 0\n    numactl --membind=1 --cpunodebind=0 a.out\n\nThe MEMKIND_HBW_NODES environment variable set to zero will bind high\nbandwidth allocations to NUMA node 0.  The --membind=1 flag to numactl\nwill bind standard allocations, static and stack variables to NUMA\nnode 1.  The --cpunodebind=0 option to numactl will bind the process\nthreads to CPUs associated with NUMA node 0.  With this configuration\nstandard allocations will be fetched across the QPI bus, and high\nbandwidth allocations will be local to the process CPU.\n\nNOTES\n-----\n* Using memkind with Transparent Huge Pages enabled may result in\n undesirably high memory footprint. To avoid that disable THP using following\n instruction: https://www.kernel.org/doc/Documentation/vm/transhuge.txt\n\nSTATUS\n------\nDifferent interfaces can represent different maturity level\n(as described in corresponding man pages).\nFeedback on design and implementation is greatly appreciated.\n"
  },
  {
    "path": "deps/memkind/src/astyle.sh",
    "content": "#!/bin/bash\n#\n#  Copyright (C) 2018 Intel Corporation.\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#  1. Redistributions of source code must retain the above copyright notice(s),\n#     this list of conditions and the following disclaimer.\n#  2. Redistributions in binary form must reproduce the above copyright notice(s),\n#     this list of conditions and the following disclaimer in the documentation\n#     and/or other materials provided with the distribution.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n#  EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n#  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\nASTYLE_MIN_VER=\"3.1\"\nASTYLE_OPT=\"--style=linux --indent=spaces=4 -S -r --max-continuation-indent=80 \"\nASTYLE_OPT+=\"--max-code-length=80 --break-after-logical --indent-namespaces -z2 \"\nASTYLE_OPT+=\"--align-pointer=name\"\nif ! ASTYLE=$(which astyle)\nthen\n    echo \"Package astyle was not found. Unable to check source files format policy.\" >&2\n    exit 1\nfi\nASTYLE_VER=$(astyle --version 2>&1 | awk '{print $NF}')\nif (( $(echo \"$ASTYLE_VER < $ASTYLE_MIN_VER\" | bc -l) ));\nthen\n    echo \"Minimal required version of astyle is $ASTYLE_MIN_VER. Detected version is $ASTYLE_VER\" >&2\n    echo \"Unable to check source files format policy.\" >&2\n    exit 1\nfi\n$ASTYLE $ASTYLE_OPT ./*.c,*.cpp,*.h,*.hpp --exclude=jemalloc > astyle.out\nif TEST=$(cat astyle.out | grep -c Formatted)\nthen\n    cat astyle.out\n    git --no-pager diff\n    echo \"Please fix style issues as shown above\"\n    exit 1\nelse\n    echo \"Source code is compliant with format policy. No style issues were found.\"\n    exit 0\nfi\n"
  },
  {
    "path": "deps/memkind/src/autogen.sh",
    "content": "#!/bin/bash\n#\n#  Copyright (C) 2014-2017 Intel Corporation.\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#  1. Redistributions of source code must retain the above copyright notice(s),\n#     this list of conditions and the following disclaimer.\n#  2. Redistributions in binary form must reproduce the above copyright notice(s),\n#     this list of conditions and the following disclaimer in the documentation\n#     and/or other materials provided with the distribution.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n#  EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n#  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n#\n\nset -e\n\n# If the VERSION file does not exist, then create it based on git\n# describe or if not in a git repo just set VERSION to 0.0.0.\nif [ ! -f VERSION ]; then\n    if [ -f .git/config ]; then\n        sha=$(git describe --long --always | awk -F- '{print $(NF)}')\n        release=$(git describe --long | awk -F- '{print $(NF-1)}')\n        version=$(git describe --long | sed -e \"s|\\(.*\\)-$release-$sha|\\1|\" -e \"s|-|+|g\" -e \"s|^v||\")\n        if [ \"$release\" == \"\" ]; then\n            version=${sha}\n        else\n            version=${version}+dev${release}-${sha}\n        fi\n    else\n        echo \"WARNING:  VERSION file does not exist and working directory is not a git repository, setting verison to 0.0.0\" 2>&1\n        version=0.0.0\n    fi\n    echo $version > VERSION\nfi\n\nautoreconf --install\n\n"
  },
  {
    "path": "deps/memkind/src/autohbw/Makefile.mk",
    "content": "#\n#  Copyright (C) 2014 - 2016 Intel Corporation.\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#  1. Redistributions of source code must retain the above copyright notice(s),\n#     this list of conditions and the following disclaimer.\n#  2. Redistributions in binary form must reproduce the above copyright notice(s),\n#     this list of conditions and the following disclaimer in the documentation\n#     and/or other materials provided with the distribution.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n#  EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n#  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n#\n\nlib_LTLIBRARIES += autohbw/libautohbw.la \\\n                   # end\n\nEXTRA_DIST += autohbw/autohbw_get_src_lines.pl\n\nautohbw_libautohbw_la_LIBADD = libmemkind.la\n\nautohbw_libautohbw_la_SOURCES = autohbw/autohbw.c\n\nclean-local: autohbw-clean\n\nautohbw-clean:\n\trm -f autohbw/*.gcno\n"
  },
  {
    "path": "deps/memkind/src/autohbw/autohbw.c",
    "content": "/*\n * Copyright (C) 2015 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n///////////////////////////////////////////////////////////////////////////\n// File   : autohbw.c\n// Purpose: Library to automatically allocate HBW (MCDRAM)\n// Author : Ruchira Sasanka (ruchira DOT sasanka AT intel DOT com)\n// Date   : Jan 30, 2015\n///////////////////////////////////////////////////////////////////////////\n\n#include <memkind.h>\n\n#include <assert.h>\n#include <ctype.h>\n#include <errno.h>\n#include <stdbool.h>\n#include <stddef.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <unistd.h>\n\n#define AUTOHBW_EXPORT __attribute__((visibility(\"default\")))\n#define AUTOHBW_INIT __attribute__((constructor))\n\n//-2 = nothing is printed\n//-1 = critical messages are printed\n// 0 = no log messages for allocations are printed but INFO messages are printed\n// 1 = a log message is printed for each allocation (Default)\n// 2 = a log message is printed for each allocation with a backtrace\nenum {\n    ALWAYS = -1,\n    INFO,\n    ALLOC,\n    VERBOSE\n};\n\n// Default is to print allocations\nstatic int LogLevel = ALLOC;\n\n// Allocations of size greater than low limit promoted to HBW memory.\n// If there is a high limit specified, allocations larger than this limit\n// will not be allocated in HBW.\nstatic size_t HBWLowLimit = 1 * 1024 * 1024;\nstatic size_t HBWHighLimit = -1ull;\n\n// Whether we have initialized HBW arena of memkind library -- by making\n// a dummy call to it. HBW arena (and hence any memkind_* call with kind\n// HBW) must NOT be used until this flag is set true.\nstatic bool MemkindInitDone = false;\n\n// Following is the type of HBW memory that is allocated using memkind.\n// By changing this type, this library can be used to allocate other\n// types of memory types (e.g., MEMKIND_HUGETLB, MEMKIND_GBTLB,\n// MEMKIND_HBW_HUGETLB etc.)\nstatic memkind_t hbw_kind;\n\n// API control for HBW allocations.\nstatic bool isAutoHBWEnabled = true;\n\n#define LOG(level, ...)                                                                            \\\n    do {                                                                                           \\\n        if (LogLevel >= level) {                                                                   \\\n            fprintf(stderr, __VA_ARGS__);                                                          \\\n        }                                                                                          \\\n    } while (0)\n\nstatic bool isAllocInHBW(size_t size)\n{\n    if (!MemkindInitDone)\n        return false;\n\n    if (!isAutoHBWEnabled)\n        return false;\n\n    if (size < HBWLowLimit)\n        return false;\n\n    if (size > HBWHighLimit)\n        return false;\n\n    return true;\n}\n\n// Returns the limit in bytes using a limit value and a multiplier\n// character like K, M, G\nstatic size_t getLimit(size_t limit, char lchar)\n{\n    // Now read the trailing character (e.g., K, M, G)\n    // Based on the character, determine the multiplier\n    if ((limit > 0) && isalpha(lchar)) {\n        long mult = 1;\n\n        switch (toupper(lchar)) {\n            case 'G':\n                mult *= 1024;\n            case 'M':\n                mult *= 1024;\n            case 'K':\n                mult *= 1024;\n        }\n\n        // check for overflow, saturate at max\n        if (limit >= -1ull / mult)\n            return -1ull;\n\n        return limit * mult;\n    }\n\n    return limit;\n}\n\n// Once HBWLowLimit (and HBWHighLimit) are set, call this method to\n// inform the user about the size range of arrays that will be allocated\n// in HBW\nstatic void printLimits()\n{\n    // Inform according to the limits set\n    if ((HBWLowLimit > 0) && (HBWHighLimit < -1ull)) {\n        // if both high and low limits are specified, we use a range\n        LOG(INFO, \"INFO: Allocations between %ldK - %ldK will be allocated in \"\n            \"HBW. Set AUTO_HBW_SIZE=X:Y to change this limit.\\n\",\n            HBWLowLimit / 1024, HBWHighLimit / 1024);\n    } else if (HBWLowLimit > 0) {\n        // if only a low limit is provided, use that\n        LOG(INFO, \"INFO: Allocations greater than %ldK will be allocated in HBW.\"\n            \" Set AUTO_HBW_SIZE=X:Y to change this limit.\\n\",\n            HBWLowLimit / 1024);\n    } else if (HBWHighLimit < -1ull) {\n        // if only a high limit is provided, use that\n        LOG(INFO, \"INFO: Allocations smaller than %ldK will be allocated in HBW. \"\n            \"Set AUTO_HBW_SIZE=X:Y to change this limit.\\n\",\n            HBWHighLimit / 1024);\n    } else {\n        // none of limits is set to non-edge value, everything goes to HBW\n        LOG(INFO, \"INFO: All allocation will be done in HBW.\");\n    }\n}\n\nstruct kind_name_t {\n    memkind_t *kind;\n    const char *name;\n};\n\nstatic struct kind_name_t named_kinds[] = {\n    { &MEMKIND_DEFAULT, \"memkind_default\" },\n    { &MEMKIND_HUGETLB, \"memkind_hugetlb\" },\n    { &MEMKIND_INTERLEAVE, \"memkind_interleave\" },\n    { &MEMKIND_HBW, \"memkind_hbw\" },\n    { &MEMKIND_HBW_PREFERRED, \"memkind_hbw_preferred\" },\n    { &MEMKIND_HBW_HUGETLB, \"memkind_hbw_hugetlb\" },\n    { &MEMKIND_HBW_PREFERRED_HUGETLB, \"memkind_hbw_preferred_hugetlb\" },\n    { &MEMKIND_HBW_GBTLB, \"memkind_hbw_gbtlb\" },\n    { &MEMKIND_HBW_PREFERRED_GBTLB, \"memkind_hbw_preferred_gbtlb\" },\n    { &MEMKIND_GBTLB, \"memkind_gbtlb\" },\n    { &MEMKIND_HBW_INTERLEAVE, \"memkind_hbw_interleave\" },\n};\n\nstatic memkind_t get_kind_by_name(const char *name)\n{\n    int i;\n    for (i = 0; i < sizeof(named_kinds) / sizeof(named_kinds[0]); ++i)\n        if (strcasecmp(named_kinds[i].name, name) == 0)\n            return *named_kinds[i].kind;\n\n    return 0;\n}\n\n// Read from the environment and sets global variables\n// Env variables are:\n//   AUTO_HBW_SIZE = gives the size for auto HBW allocation\n//   AUTO_HBW_LOG = gives logging level\nstatic void setEnvValues()\n{\n    // STEP: Read the log level from the env variable. Do this early because\n    //       printing depends on this\n    char *log_str = getenv(\"AUTO_HBW_LOG\");\n    if (log_str && strlen(log_str)) {\n        int level = atoi(log_str);\n        LogLevel = level;\n        LOG(ALWAYS, \"INFO: Setting log level to %d\\n\", LogLevel);\n    }\n\n    if (LogLevel == INFO) {\n        LOG(INFO, \"INFO: HBW allocation stats will not be printed. \"\n            \"Set AUTO_HBW_LOG to enable.\\n\");\n    } else if (LogLevel == ALLOC) {\n        LOG(INFO, \"INFO: Only HBW allocations will be printed. \"\n            \"Set AUTO_HBW_LOG to disable/enable.\\n\");\n    } else if (LogLevel == VERBOSE) {\n        LOG(INFO, \"INFO: HBW allocation with backtrace info will be printed. \"\n            \"Set AUTO_HBW_LOG to disable.\\n\");\n    }\n\n    // Set the memory type allocated by this library. By default, it is\n    // MEMKIND_HBW, but we can use this library to allocate other memory\n    // types\n    const char *memtype_str = getenv(\"AUTO_HBW_MEM_TYPE\");\n    if (memtype_str && strlen(memtype_str)) {\n        // Find the memkind_t using the name the user has provided in the env variable\n        memkind_t mty = get_kind_by_name(memtype_str);\n        if (mty != 0) {\n            hbw_kind = mty;\n            LOG(INFO, \"INFO: Setting HBW memory type to %s\\n\", memtype_str);\n        } else {\n            LOG(ALWAYS, \"WARN: Memory type %s not recognized. Using default type\\n\",\n                memtype_str);\n        }\n    }\n\n    // STEP: Set the size limits (thresholds) for HBW allocation\n    //\n    // Reads the environment variable\n    const char *size_str = getenv(\"AUTO_HBW_SIZE\");\n    if (size_str) {\n        size_t lowlim = HBWLowLimit / 1024;\n        size_t highlim = HBWHighLimit / 1024;\n        char lowC = 'K', highC = 'K';\n\n        if (size_str) {\n            char *ptr = (char *)size_str;\n            lowlim = strtoll(ptr, &ptr, 10);\n            if (*ptr != 0 && *ptr != ':')\n                lowC = *ptr++;\n            else\n                lowC = ' ';\n\n            if (*ptr++ == ':') {\n                highlim = strtoll(ptr, &ptr, 10);\n                if (*ptr)\n                    highC = *ptr;\n                else\n                    highC = ' ';\n            }\n\n            LOG(INFO, \"INFO: lowlim=%zu(%c), highlim=%zu(%c)\\n\", lowlim, lowC, highlim,\n                highC);\n        }\n\n        HBWLowLimit = getLimit(lowlim, lowC);\n        HBWHighLimit = getLimit(highlim, highC);\n\n        if (HBWLowLimit >= HBWHighLimit) {\n            LOG(ALWAYS, \"WARN: In AUTO_HBW_SIZE=X:Y, X cannot be greater or equal to Y. \"\n                \"None of allocations will use HBW memory.\\n\");\n        }\n    } else {\n        // if the user did not specify any limits, inform that we are using\n        // default limits\n        LOG(INFO, \"INFO: Using default values for array size thresholds. \"\n            \"Set AUTO_HBW_SIZE=X:Y to change.\\n\");\n    }\n\n    // inform the user about limits\n    printLimits();\n}\n\n// This function is executed at library load time.\n// Initialize HBW arena by making a dummy allocation/free at library load\n// time. Until HBW initialization is complete, we must not call any\n// allocation routines with HBW as kind.\nstatic void AUTOHBW_INIT autohbw_load(void)\n{\n    // First set the default memory type this library allocates. This can\n    // be overridden by env variable\n    // Note: 'memkind_hbw_preferred' will allow falling back to DDR but\n    //       'memkind_hbw will not'\n    // Note: If HBM is not installed on a system, memkind_hbw_preferred call\n    //       would fail. Therefore, we need to check for availability first.\n    if (memkind_check_available(MEMKIND_HBW) != 0) {\n        LOG(ALWAYS, \"WARN: *** No HBM found in system. Will use default (DDR) \"\n            \"OR user specified type ***\\n\");\n        hbw_kind = MEMKIND_DEFAULT;\n    } else {\n        hbw_kind = MEMKIND_HBW_PREFERRED;\n    }\n\n    // Read any env variables. This has to be done first because DbgLevel\n    // is set using env variables and debug printing is used below\n    setEnvValues(); // read any env variables\n\n    LOG(INFO, \"INFO: autohbw.so loaded!\\n\");\n\n    // dummy HBW call to initialize HBW arena\n    void *pp = memkind_malloc(hbw_kind, 16);\n    if (pp == 0) {\n        LOG(ALWAYS, \"\\t-HBW init call FAILED. \"\n            \"Is required memory type present on your system?\\n\");\n        abort();\n    }\n\n    LOG(ALWAYS, \"\\t-HBW int call succeeded\\n\");\n    memkind_free(hbw_kind, pp);\n\n    MemkindInitDone = true; // enable HBW allocation\n}\n\nstatic void *MemkindMalloc(size_t size)\n{\n    LOG(VERBOSE, \"In my memkind malloc sz:%ld ... \", size);\n\n    bool useHbw = isAllocInHBW(size);\n    memkind_t kind = useHbw ? hbw_kind : MEMKIND_DEFAULT;\n\n    if (useHbw)\n        LOG(VERBOSE, \"\\tHBW\");\n\n    void *ptr = memkind_malloc(kind, size);\n\n    LOG(VERBOSE, \"\\tptr:%p\\n\", ptr);\n    return ptr;\n}\n\nstatic void *MemkindCalloc(size_t nmemb, size_t size)\n{\n    LOG(VERBOSE, \"In my memkind calloc sz:%ld ..\", size * nmemb);\n\n    bool useHbw = isAllocInHBW(size);\n    memkind_t kind = useHbw ? hbw_kind : MEMKIND_DEFAULT;\n\n    if (useHbw)\n        LOG(VERBOSE, \"\\tHBW\");\n\n    void *ptr = memkind_calloc(kind, nmemb, size);\n\n    LOG(VERBOSE, \"\\tptr:%p\\n\", ptr);\n    return ptr;\n}\n\nstatic void *MemkindRealloc(void *ptr, size_t size)\n{\n    LOG(VERBOSE, \"In my memkind realloc sz:%ld, p1:%p ..\", size, ptr);\n\n    bool useHbw = isAllocInHBW(size);\n    memkind_t kind = useHbw ? hbw_kind : MEMKIND_DEFAULT;\n\n    if (useHbw)\n        LOG(VERBOSE, \"\\tHBW\");\n\n    void *nptr = memkind_realloc(kind, ptr, size);\n\n    LOG(VERBOSE, \"\\tptr=%p\\n\", nptr);\n    return nptr;\n}\n\nstatic int MemkindAlign(void **memptr, size_t alignment, size_t size)\n{\n    LOG(VERBOSE, \"In my memkind align sz:%ld .. \", size);\n\n    bool useHbw = isAllocInHBW(size);\n    memkind_t kind = useHbw ? hbw_kind : MEMKIND_DEFAULT;\n\n    if (useHbw)\n        LOG(VERBOSE, \"\\tHBW\");\n\n    int ret = memkind_posix_memalign(kind, memptr, alignment, size);\n\n    LOG(VERBOSE, \"\\tptr:%p\\n\", *memptr);\n    return ret;\n}\n\n// memkind_free does not need the exact kind, if kind is 0. Then\n// the library can figure out the proper kind itself.\nstatic void MemkindFree(void *ptr)\n{\n    // avoid to many useless logs\n    if (ptr)\n        LOG(VERBOSE, \"In my memkind free, ptr:%p\\n\", ptr);\n    memkind_free(0, ptr);\n}\n\n//--------------------------------------------------------------------------\n// ------------------ Public API of autohbw           ----------------------\n//--------------------------------------------------------------------------\n\nAUTOHBW_EXPORT void enableAutoHBW()\n{\n    isAutoHBWEnabled = true;\n    LOG(INFO, \"INFO: HBW allocations enabled by application (for this rank)\\n\");\n}\n\nAUTOHBW_EXPORT void disableAutoHBW()\n{\n    isAutoHBWEnabled = false;\n    LOG(INFO, \"INFO: HBW allocations disabled by application (for this rank)\\n\");\n}\n\nAUTOHBW_EXPORT void *malloc(size_t size)\n{\n    return MemkindMalloc(size);\n}\n\nAUTOHBW_EXPORT void *calloc(size_t nmemb, size_t size)\n{\n    return MemkindCalloc(nmemb, size);\n}\n\nAUTOHBW_EXPORT void *realloc(void *ptr, size_t size)\n{\n    return MemkindRealloc(ptr, size);\n}\n\nAUTOHBW_EXPORT int posix_memalign(void **memptr, size_t alignment, size_t size)\n{\n    return MemkindAlign(memptr, alignment, size);\n}\n\n// Warn about deprecated function usage.\nAUTOHBW_EXPORT void *valloc(size_t size)\n{\n    LOG(ALWAYS, \"use of deprecated valloc. Use posix_memalign instead\\n\");\n    void *memptr = 0;\n    size_t boundary = sysconf(_SC_PAGESIZE);\n    int status = MemkindAlign(&memptr, boundary, size);\n    if (status == 0 && memptr != 0)\n        return memptr;\n\n    return 0;\n}\n\n// Warn about deprecated function usage.\nAUTOHBW_EXPORT void *memalign(size_t boundary, size_t size)\n{\n    LOG(ALWAYS, \"use of deprecated memalign. Use posix_memalign instead\\n\");\n    void *memptr = 0;\n    int status = MemkindAlign(&memptr, boundary, size);\n    if (status == 0 && memptr != 0)\n        return memptr;\n\n    return 0;\n}\n\nAUTOHBW_EXPORT void free(void *ptr)\n{\n    return MemkindFree(ptr);\n}\n"
  },
  {
    "path": "deps/memkind/src/autohbw/autohbw_README",
    "content": "README for auothbw library\nRuchira Sasanka (ruchira DOT sasanka AT intel DOT com)\n2015 April 8\n\nPURPOSE\n-------\nThe autohbw library is used to automatically allocate high-bandwidth (HBW)\nmemory without any modifications to source code of your application.\nThis library intercepts the standard heap allocations (e.g., malloc, calloc)\nin your program so that it can serve those allocations out of HBW memory.\n\nThere are two libraries installed at the same location as memkind library:\n  1) libautohbw.so -- dynamic library\n  2) libautohbw.a  -- static library (provided for static linking)\n\nUSAGE\n-----\nTo use the dynamic library, insert LD_PRELOAD=libautohbw.so and libmekind.so\nbefore your commandline. For instance,\n\n   LD_PRELOAD=libautohbw.so /bin/ls\n\nexecutes Unix ls utility with autohbw library. You can execute the\nabove command on the prompt or put it in a shell script. An example of\nthe latter approach is given in ./autohbw_test.sh. Make sure that\nLD_LIBRARY_PATH enviornment variable contains the path to libautohbw.so\nand libmekind.so before you exeute the above command.\n\nTo use the statically linked library, insert -lautohbw -lmemkind\non your link line. You will have to provide the library path with -L to\npoint to the above libraries. If you are linking your program statically\n(e.g., with -static or -fast flags in ifort/icc), you must use the static\nversion of autohbw library (libautohbw.a).\n\nIt is possible to temporarily disable/enable automatic HBM allocations by\ncalling disableAutoHBW() and enableAutoHBW() in your source code. To call\nthese routines, please include autohbw_api.h header file and link with\n-lautohbw.\n\n\nUsage with MPI programs\n-----------------------\nTo use with MPI programs, you need to create a shell script to do the\nLD_PRELOAD. E.g.,\n   mpirun -n 2 ./autohbw_test.sh\n\nENVIORNMENT VARIABLES\n---------------------\nThe following enviornment variables control the behavior of the autohbw\nlibrary:\n\n  AUTO_HBW_SIZE=x[:y]\n  Indicates that any allocation larger than x and smaller than y should be\n  allocated in HBW memory. x and y can be followed by a K, M, or G to\n  indicate the size in Kilo/Memga/Giga bytes (e.g., 4K, 3M, 2G).\n  Examples:\n    AUTO_HBW_SIZE=4K     # all allocation larger than 4K allocated in HBW\n    AUTO_HBW_SIZE=1M:5M  # allocations betwen 1M and 5M allocated in HBW\n\n\n  AUTO_HBW_LOG=level\n  Sets the value of logging (printing) level. If level is:\n   -1 = no messages are printed\n    0 = no log messages for allocations are printed but INFO messages\n        are printed\n    1 = a log message is printed for each allocation (Default)\n    2 = a log message is printed for each allocation with a backtrace\n        Redirect this output and use autohbw_get_src_lines.pl to find\n        source lines for each allocation. Your application must be compiled\n        with -g to see source lines.\n  Notes:\n    1. Logging adds extra overhead. Therefore, performance critical runs,\n       logging level should be 0\n    2. The amount of free memory printed with log messages is only\n       approximate -- e.g. pages that are not touched yet are excluded\n  Examples:\n    AUTO_HBW_LOG=1\n\n\n  AUTO_HBW_MEM_TYPE=memory_type\n  Sets the type of memory type that should be automatically allocated. By\n  default, this type is MEMKIND_HBW_PREFERRED, if MCDRAM is found in your\n  system; otherwise, the default is MEMKIND_DEFAULT. The names of memory\n  types are definedin memkind(3) man page. If you are requesting any huge\n  TLB pages, pleasemake sure that the requested type is currently enabled\n  in your OS.\n  Examples:\n    AUTO_HBW_MEM_TYPE=MEMKIND_HBW_PREFERRED   (default, if MCDRAM present)\n    AUTO_HBW_MEM_TYPE=MEMKIND_DEFAULT         (default, if MCDRAM absent)\n    AUTO_HBW_MEM_TYPE=MEMKIND_HBW_HUGETLB\n    AUTO_HBW_MEM_TYPE=MEMKIND_HUGETLB\n\n"
  },
  {
    "path": "deps/memkind/src/autohbw/autohbw_api.h",
    "content": "/*\n * Copyright (C) 2015 - 2016 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n///////////////////////////////////////////////////////////////////////////\n// File   : autohbw_api.h\n// Purpose: Header file to include, to use API calls to AutoHBW\n// Author : Ruchira Sasanka (ruchira.sasanka AT intel.com)\n// Date   : Jan 30, 2015\n///////////////////////////////////////////////////////////////////////////\n\n#ifndef AUTOHBW_API_H\n#define AUTOHBW_API_H\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n// Temporarily enable HBM allocations\nvoid enableAutoHBW();\n\n// Temporarily disable HBM allocations\nvoid disableAutoHBW();\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n"
  },
  {
    "path": "deps/memkind/src/autohbw/autohbw_get_src_lines.pl",
    "content": "#!/usr/bin/perl\n#\n#  Copyright (C) 2015 - 2016 Intel Corporation.\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#  1. Redistributions of source code must retain the above copyright notice(s),\n#     this list of conditions and the following disclaimer.\n#  2. Redistributions in binary form must reproduce the above copyright notice(s),\n#     this list of conditions and the following disclaimer in the documentation\n#     and/or other materials provided with the distribution.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n#  EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n#  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n#\n\nuse strict;\n\nmy $usage = \"Usage: get_autohbw_srclines.pl output_log_of_AutoHBW executable\";\n\n# Check for 2 arguments\n#\nif (@ARGV != 2) {\n    print $usage, \"\\n\";\n    exit;\n}\n\n# Read the command line arguments\n#\nmy $LogF = shift @ARGV;\nmy $BinaryF = shift @ARGV;\n\n&main();\n\nsub main {\n\n\n    print(\"Info: Reading AutoHBW log from: $LogF\\n\");\n    print(\"Info: Binary file: $BinaryF\\n\");\n\n    # open the log file produced by AutoHBM and look at lines starting\n    # with Log\n    open LOGF, \"grep Log $LogF |\" or die \"Can't open log file $LogF\";\n\n    my $line;\n\n    # Read each log line\n    #\n    while ($line = <LOGF>) {\n\n        # if the line contain 3 backtrace addresses, try to find the source\n        # lines for them\n        #\n        if ($line =~ /^(Log:.*)Backtrace:.*0x([0-9a-f]+).*0x([0-9a-f]+).*0x([0-9a-f]+)/ ) {\n\n            #  Read the pointers\n            #\n            my @ptrs;\n\n            my $pre = $1;\n\n            $ptrs[0] = $2;\n            $ptrs[1] = $3;\n            $ptrs[2] = $4;\n\n            # prints the first portion of the line\n            #\n            print $pre, \"\\n\";\n\n            # for each of the pointers, lookup its source line using\n            # addr2line and print the src line(s) if found\n            #\n            my $i=0;\n            for ($i=0; $i < @ptrs; $i++) {\n\n                my $addr = $ptrs[$i];\n\n                open SRCL, \"addr2line -e $BinaryF 0x$addr |\"\n                    or die \"addr2line fail\";\n\n\n                my $srcl = <SRCL>;\n\n                if ($srcl =~ /^\\?/) {\n\n                } else {\n\n                    print \"\\t- Src: $srcl\";\n                }\n\n            }\n\n        }\n\n    }\n\n}\n"
  },
  {
    "path": "deps/memkind/src/autohbw/autohbw_test.sh",
    "content": "#!/bin/bash\n\n#  Copyright (C) 2015 - 2016 Intel Corporation.\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#  1. Redistributions of source code must retain the above copyright notice(s),\n#     this list of conditions and the following disclaimer.\n#  2. Redistributions in binary form must reproduce the above copyright notice(s),\n#     this list of conditions and the following disclaimer in the documentation\n#     and/or other materials provided with the distribution.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n#  EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n#  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\n# The following is needed if your system does not really have HBW nodes.\n# Please see 'man memkind' for a description of this variable\n#\n# export MEMKIND_HBW_NODES=0\n\n# Set any autohbw env variables\n#\nexport AUTO_HBW_SIZE=4K\n\n# Make sure you have set LD_LIBRARY_PATH to lib directory with libautohbw.so\n# and libmemkind.so\n#\nLD_PRELOAD=libautohbw.so:libmemkind.so /bin/ls\n"
  },
  {
    "path": "deps/memkind/src/build.sh",
    "content": "#!/bin/bash\n#\n#  Copyright (C) 2016 Intel Corporation.\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#  1. Redistributions of source code must retain the above copyright notice(s),\n#     this list of conditions and the following disclaimer.\n#  2. Redistributions in binary form must reproduce the above copyright notice(s),\n#     this list of conditions and the following disclaimer in the documentation\n#     and/or other materials provided with the distribution.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n#  EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n#  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\nset -e\n\nif [ -z \"$JE_PREFIX\" ]; then\n        export JE_PREFIX=jemk_\nfi\n\ncd $(dirname $0)\nEXTRA_CONF=$@\n\nif [ ! -f ./jemalloc/obj/lib/libjemalloc_pic.a ]; then\n\t./build_jemalloc.sh $EXTRA_CONF\nfi\n\nif [ ! -f ./configure ]; then\n\t./autogen.sh\nfi\n\nif [ ! -f ./Makefile ]; then\n\t./configure $EXTRA_CONF\nfi\n\n#use V=1 for full cmdlines of build\nmake all -j $MAKEOPTS\nmake checkprogs -j $MAKEOPTS\n"
  },
  {
    "path": "deps/memkind/src/build_jemalloc.sh",
    "content": "#!/bin/bash\n#\n#  Copyright (C) 2016 - 2018 Intel Corporation.\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#  1. Redistributions of source code must retain the above copyright notice(s),\n#     this list of conditions and the following disclaimer.\n#  2. Redistributions in binary form must reproduce the above copyright notice(s),\n#     this list of conditions and the following disclaimer in the documentation\n#     and/or other materials provided with the distribution.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n#  EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n#  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\nEXTRA_CONF=$@\n\ncd jemalloc\ntest -e configure || autoconf\ntest -e obj || mkdir obj\ncd obj\n../configure --enable-autogen --with-jemalloc-prefix=$JE_PREFIX --without-export \\\n             --disable-stats --disable-fill \\\n             $EXTRA_CONF --with-malloc-conf=\"narenas:256,lg_tcache_max:12\"\n\nmake -j`nproc`\n"
  },
  {
    "path": "deps/memkind/src/config.h.in~",
    "content": "/* config.h.in.  Generated from configure.ac by autoheader.  */\n\n/* Upper bound for number of arenas per kind */\n#undef ARENA_LIMIT_PER_KIND\n\n/* define if the compiler supports basic C++11 syntax */\n#undef HAVE_CXX11\n\n/* Define to 1 if you have the `numa' library (-lnuma). */\n#undef HAVE_LIBNUMA\n\n/* Have PTHREAD_PRIO_INHERIT. */\n#undef HAVE_PTHREAD_PRIO_INHERIT\n\n/* Enables code for debugging */\n#undef MEMKIND_DEBUG\n\n/* Enables decorators */\n#undef MEMKIND_DECORATION_ENABLED\n\n/* Enables TLS usage for mapping arenas to threads */\n#undef MEMKIND_TLS\n\n/* TLS model attribute */\n#undef MEMKIND_TLS_MODEL\n\n/* Name of package */\n#undef PACKAGE\n\n/* Define to the address where bug reports for this package should be sent. */\n#undef PACKAGE_BUGREPORT\n\n/* Define to the full name of this package. */\n#undef PACKAGE_NAME\n\n/* Define to the full name and version of this package. */\n#undef PACKAGE_STRING\n\n/* Define to the one symbol short name of this package. */\n#undef PACKAGE_TARNAME\n\n/* Define to the home page for this package. */\n#undef PACKAGE_URL\n\n/* Define to the version of this package. */\n#undef PACKAGE_VERSION\n\n/* Define to necessary symbol if this constant uses a non-standard name on\n   your system. */\n#undef PTHREAD_CREATE_JOINABLE\n\n/* Version number of package */\n#undef VERSION\n"
  },
  {
    "path": "deps/memkind/src/configure.ac",
    "content": "#\n#  Copyright (C) 2014 - 2018 Intel Corporation.\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#  1. Redistributions of source code must retain the above copyright notice(s),\n#     this list of conditions and the following disclaimer.\n#  2. Redistributions in binary form must reproduce the above copyright notice(s),\n#     this list of conditions and the following disclaimer in the documentation\n#     and/or other materials provided with the distribution.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n#  EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n#  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n#\n\n#                                               -*- Autoconf -*-\n# Process this file with autoconf to produce a configure script.\n\nAC_PREREQ([2.64])\nAC_INIT([memkind],m4_esyscmd([tr -d '\\n' < VERSION]))\n\nAC_CONFIG_HEADERS([config.h])\nAC_CONFIG_SRCDIR([memkind.spec.mk])\n\nAM_INIT_AUTOMAKE([-Wall -Werror foreign 1.11 silent-rules subdir-objects parallel-tests tar-pax])\nAM_SILENT_RULES([yes])\n\n# Checks for programs and libraries.\nAM_PROG_AR\nAC_PROG_CXX\nAC_PROG_CC\nAC_OPENMP\nAC_CHECK_LIB(numa, numa_available, [], [AC_MSG_ERROR([libnuma is required dependency])])\nAX_PTHREAD([LIBS=\"$PTHREAD_LIBS $LIBS\" CFLAGS=\"$CFLAGS $PTHREAD_CFLAGS\" CC=\"$PTHREAD_CC\"],\n           [AC_MSG_ERROR([pthreads are required dependency])])\n\nAM_PROG_CC_C_O\n\n#============================tls===============================================\n# Check for tls_model attribute support\nSAVED_CFLAGS=\"${CFLAGS}\"\nCFLAGS=\"$CFLAGS -Werror\"\nAC_MSG_CHECKING([for tls_model attribute support])\nAC_COMPILE_IFELSE([AC_LANG_PROGRAM(\n[[\n    static __thread int __attribute__((tls_model(\"local-dynamic\"))) x;\n]],\n[[\n    x = 1234;\n]])],\n    [AC_MSG_RESULT([yes])\n     tls_model=\"1\"],\n    [AC_MSG_RESULT([no])\n     tls_model=\"0\"])\n\nCFLAGS=\"${SAVED_CFLAGS}\"\nif test \"x${tls_model}\" = \"x1\" ; then\n  AC_DEFINE([MEMKIND_TLS_MODEL],\n            [__attribute__((tls_model(\"initial-exec\")))], [TLS model attribute])\nelse\n  AC_DEFINE([MEMKIND_TLS_MODEL], [ ], [TLS model attribute])\nfi\n\nAC_ARG_ENABLE([tls],\n  [AS_HELP_STRING([--enable-tls], [Enable thread-local storage (__thread keyword)])],\n[if test \"x$enable_tls\" = \"xyes\" ; then\n  enable_tls=\"1\"\nelse\n  enable_tls=\"0\"\nfi\n],\n[enable_tls=\"0\"]\n)\nif test \"x${enable_tls}\" = \"x1\" ; then\nAC_MSG_CHECKING([for TLS support])\nAC_COMPILE_IFELSE([AC_LANG_PROGRAM(\n[[\n    __thread int x;\n]], [[\n    x = 1234;\n]])],\n    AC_MSG_RESULT([yes]),\n    AC_MSG_RESULT([no])\n    enable_tls=\"0\")\nfi\nif test \"x${enable_tls}\" = \"x1\" ; then\n  AC_DEFINE([MEMKIND_TLS], [ ], [Enables TLS usage for mapping arenas to threads])\nfi\nAC_SUBST([enable_tls])\n\n#============================decorators========================================\nAC_ARG_ENABLE([decorators],\n  [AS_HELP_STRING([--enable-decorators], [Enable decorators])],\n[if test \"x$enable_decorators\" = \"xyes\" ; then\n  enable_decorators=\"1\"\nelse\n  enable_decorators=\"0\"\nfi\n],\n[enable_decorators=\"0\"]\n)\nif test \"x${enable_decorators}\" = \"x1\" ; then\n  AC_DEFINE([MEMKIND_DECORATION_ENABLED], [ ], [Enables decorators])\nfi\nAC_SUBST([enable_decorators])\n\n#============================debug=============================================\nAC_ARG_ENABLE([debug],\n  [AS_HELP_STRING([--enable-debug], [Build debugging code and compile with -O0 -g])],\n[if test \"x$enable_debug\" = \"xno\" ; then\n  enable_debug=\"0\"\nelse\n  enable_debug=\"1\"\nfi\n],\n[enable_debug=\"0\"]\n)\nif test \"x$enable_debug\" = \"x1\" ; then\n  AC_DEFINE([MEMKIND_DEBUG], [ ], [Enables code for debugging])\n  CFLAGS=\"$CFLAGS -O0 -g\"\n  CXXFLAGS=\"$CXXFLAGS -O0 -g\"\nfi\nAC_SUBST([enable_debug])\n\n#============================gcov==============================================\nAC_ARG_ENABLE([gcov],\n  [AS_HELP_STRING([--enable-gcov], [Build code with gcov instructions])],\n[if test \"x$enable_gcov\" = \"xno\" ; then\n  enable_gcov=\"0\"\nelse\n  enable_gcov=\"1\"\nfi\n],\n[enable_gcov=\"0\"]\n)\nif test \"x$enable_gcov\" = \"x1\" ; then\n  CFLAGS=\"$CFLAGS -O0 -fprofile-arcs -ftest-coverage\"\n  CXXFLAGS=\"$CXXFLAGS -O0 -fprofile-arcs -ftest-coverage\"\nfi\nAC_SUBST([enable_gcov])\n\n#============================secure_flags======================================\nAC_ARG_ENABLE([secure],\n  [AS_HELP_STRING([--enable-secure], [Build library with security enchantments])],\n[if test \"x$enable_secure\" = \"xno\" ; then\n  enable_secure=\"0\"\nelse\n  enable_secure=\"1\"\nfi\n],\n[enable_secure=\"1\"]\n)\nif test \"x$enable_secure\" = \"x1\" ; then\n  CFLAGS=\"$CFLAGS -fstack-protector\"\n  LDFLAGS=\"$LDFLAGS -Wl,-z,relro,-z,now\"\n\n  if test \"$CFLAGS\" != \"${CFLAGS%-O0*}\" ; then # if CFLAGS contains -O0\n      echo \"WARNING: Could not apply FORTIFY_SOURCE=2 due to lack of optimization (-O0)\"\n  else\n      CFLAGS=\"$CFLAGS -D_FORTIFY_SOURCE=2\" #FORTITFY_SOURCE does not work with -O0 (ex. if enable_debug=1 or enable_gcov=1)\n  fi\nfi\n\nAC_SUBST([enable_secure])\n\n#============================arena_limit=======================================\narena_limit=256;\nAC_ARG_VAR(ARENA_LIMIT,\n  [Upper bound for number of arenas per kind, if set to 0 then no limit]\n)\nif test \"$ARENA_LIMIT\" != \"\" ; then\n  arena_limit=$ARENA_LIMIT;\nfi\n\nAC_DEFINE_UNQUOTED([ARENA_LIMIT_PER_KIND], $arena_limit, [Upper bound for number of arenas per kind])\n\n#============================cxx11=============================================\n\nAX_CXX_COMPILE_STDCXX_11([noext], [optional])\nAM_CONDITIONAL([HAVE_CXX11], [test \"x$HAVE_CXX11\" = x1])\n\nLT_PREREQ([2.2])\nLT_INIT\n\nAC_CONFIG_FILES([Makefile])\n\nAC_OUTPUT\n\n"
  },
  {
    "path": "deps/memkind/src/copying_headers/MANIFEST.EXEMPT",
    "content": ".gitignore\nAUTHORS\nCOPYING\nCONTRIBUTING\nINSTALL\nNEWS\nPULL_REQUEST_TEMPLATE.md\nREADME\nVERSION\nm4/ax_cxx_compile_stdcxx.m4\nm4/ax_cxx_compile_stdcxx_11.m4\nm4/ax_pthread.m4\nChangeLog\n.travis.yml\nautohbw/autohbw_README\nexamples/README\ntest/memkind-afts.ts\ntest/memkind-afts-ext.ts\ntest/memkind-slts.ts\ntest/memkind-perf.ts\ntest/memkind-perf-ext.ts\ntest/memkind-pytests.ts\ntest/gtest_fused/gtest/gtest-all.cc\ntest/gtest_fused/gtest/gtest.h\njemalloc/.appveyor.yml\njemalloc/.autom4te.cfg\njemalloc/.gitattributes\njemalloc/.gitignore\njemalloc/.travis.yml\njemalloc/COPYING\njemalloc/ChangeLog\njemalloc/INSTALL.md\njemalloc/Makefile.in\njemalloc/README\njemalloc/autogen.sh\njemalloc/bin/jemalloc-config.in\njemalloc/bin/jemalloc.sh.in\njemalloc/bin/jeprof.in\njemalloc/build-aux/config.guess\njemalloc/build-aux/config.sub\njemalloc/build-aux/install-sh\njemalloc/config.stamp.in\njemalloc/configure.ac\njemalloc/doc/html.xsl.in\njemalloc/doc/jemalloc.xml.in\njemalloc/doc/manpages.xsl.in\njemalloc/doc/stylesheet.xsl\njemalloc/include/jemalloc/internal/arena_externs.h\njemalloc/include/jemalloc/internal/arena_inlines_a.h\njemalloc/include/jemalloc/internal/arena_inlines_b.h\njemalloc/include/jemalloc/internal/arena_structs_a.h\njemalloc/include/jemalloc/internal/arena_structs_b.h\njemalloc/include/jemalloc/internal/arena_types.h\njemalloc/include/jemalloc/internal/assert.h\njemalloc/include/jemalloc/internal/atomic.h\njemalloc/include/jemalloc/internal/atomic_c11.h\njemalloc/include/jemalloc/internal/atomic_gcc_atomic.h\njemalloc/include/jemalloc/internal/atomic_gcc_sync.h\njemalloc/include/jemalloc/internal/atomic_msvc.h\njemalloc/include/jemalloc/internal/background_thread_externs.h\njemalloc/include/jemalloc/internal/background_thread_inlines.h\njemalloc/include/jemalloc/internal/background_thread_structs.h\njemalloc/include/jemalloc/internal/base_externs.h\njemalloc/include/jemalloc/internal/base_inlines.h\njemalloc/include/jemalloc/internal/base_structs.h\njemalloc/include/jemalloc/internal/base_types.h\njemalloc/include/jemalloc/internal/bit_util.h\njemalloc/include/jemalloc/internal/bitmap.h\njemalloc/include/jemalloc/internal/ckh.h\njemalloc/include/jemalloc/internal/ctl.h\njemalloc/include/jemalloc/internal/extent_dss.h\njemalloc/include/jemalloc/internal/extent_externs.h\njemalloc/include/jemalloc/internal/extent_inlines.h\njemalloc/include/jemalloc/internal/extent_mmap.h\njemalloc/include/jemalloc/internal/extent_structs.h\njemalloc/include/jemalloc/internal/extent_types.h\njemalloc/include/jemalloc/internal/hash.h\njemalloc/include/jemalloc/internal/hooks.h\njemalloc/include/jemalloc/internal/jemalloc_internal_decls.h\njemalloc/include/jemalloc/internal/jemalloc_internal_defs.h.in\njemalloc/include/jemalloc/internal/jemalloc_internal_externs.h\njemalloc/include/jemalloc/internal/jemalloc_internal_includes.h\njemalloc/include/jemalloc/internal/jemalloc_internal_inlines_a.h\njemalloc/include/jemalloc/internal/jemalloc_internal_inlines_b.h\njemalloc/include/jemalloc/internal/jemalloc_internal_inlines_c.h\njemalloc/include/jemalloc/internal/jemalloc_internal_macros.h\njemalloc/include/jemalloc/internal/jemalloc_internal_types.h\njemalloc/include/jemalloc/internal/jemalloc_preamble.h.in\njemalloc/include/jemalloc/internal/large_externs.h\njemalloc/include/jemalloc/internal/malloc_io.h\njemalloc/include/jemalloc/internal/mutex.h\njemalloc/include/jemalloc/internal/mutex_pool.h\njemalloc/include/jemalloc/internal/mutex_prof.h\njemalloc/include/jemalloc/internal/nstime.h\njemalloc/include/jemalloc/internal/pages.h\njemalloc/include/jemalloc/internal/ph.h\njemalloc/include/jemalloc/internal/private_namespace.sh\njemalloc/include/jemalloc/internal/private_symbols.sh\njemalloc/include/jemalloc/internal/prng.h\njemalloc/include/jemalloc/internal/prof_externs.h\njemalloc/include/jemalloc/internal/prof_inlines_a.h\njemalloc/include/jemalloc/internal/prof_inlines_b.h\njemalloc/include/jemalloc/internal/prof_structs.h\njemalloc/include/jemalloc/internal/prof_types.h\njemalloc/include/jemalloc/internal/public_namespace.sh\njemalloc/include/jemalloc/internal/public_unnamespace.sh\njemalloc/include/jemalloc/internal/ql.h\njemalloc/include/jemalloc/internal/qr.h\njemalloc/include/jemalloc/internal/rb.h\njemalloc/include/jemalloc/internal/rtree.h\njemalloc/include/jemalloc/internal/rtree_tsd.h\njemalloc/include/jemalloc/internal/size_classes.sh\njemalloc/include/jemalloc/internal/smoothstep.h\njemalloc/include/jemalloc/internal/smoothstep.sh\njemalloc/include/jemalloc/internal/spin.h\njemalloc/include/jemalloc/internal/stats.h\njemalloc/include/jemalloc/internal/stats_tsd.h\njemalloc/include/jemalloc/internal/sz.h\njemalloc/include/jemalloc/internal/tcache_externs.h\njemalloc/include/jemalloc/internal/tcache_inlines.h\njemalloc/include/jemalloc/internal/tcache_structs.h\njemalloc/include/jemalloc/internal/tcache_types.h\njemalloc/include/jemalloc/internal/ticker.h\njemalloc/include/jemalloc/internal/tsd.h\njemalloc/include/jemalloc/internal/tsd_generic.h\njemalloc/include/jemalloc/internal/tsd_malloc_thread_cleanup.h\njemalloc/include/jemalloc/internal/tsd_tls.h\njemalloc/include/jemalloc/internal/tsd_types.h\njemalloc/include/jemalloc/internal/tsd_win.h\njemalloc/include/jemalloc/internal/util.h\njemalloc/include/jemalloc/internal/witness.h\njemalloc/include/jemalloc/jemalloc.sh\njemalloc/include/jemalloc/jemalloc_defs.h.in\njemalloc/include/jemalloc/jemalloc_macros.h.in\njemalloc/include/jemalloc/jemalloc_mangle.sh\njemalloc/include/jemalloc/jemalloc_protos.h.in\njemalloc/include/jemalloc/jemalloc_rename.sh\njemalloc/include/jemalloc/jemalloc_typedefs.h.in\njemalloc/include/msvc_compat/C99/stdbool.h\njemalloc/include/msvc_compat/C99/stdint.h\njemalloc/include/msvc_compat/strings.h\njemalloc/include/msvc_compat/windows_extra.h\njemalloc/jemalloc.pc.in\njemalloc/m4/ax_cxx_compile_stdcxx.m4\njemalloc/msvc/ReadMe.txt\njemalloc/msvc/jemalloc_vc2015.sln\njemalloc/msvc/projects/vc2015/jemalloc/jemalloc.vcxproj\njemalloc/msvc/projects/vc2015/jemalloc/jemalloc.vcxproj.filters\njemalloc/msvc/projects/vc2015/test_threads/test_threads.cpp\njemalloc/msvc/projects/vc2015/test_threads/test_threads.h\njemalloc/msvc/projects/vc2015/test_threads/test_threads.vcxproj\njemalloc/msvc/projects/vc2015/test_threads/test_threads.vcxproj.filters\njemalloc/msvc/projects/vc2015/test_threads/test_threads_main.cpp\njemalloc/run_tests.sh\njemalloc/scripts/gen_run_tests.py\njemalloc/scripts/gen_travis.py\njemalloc/src/arena.c\njemalloc/src/background_thread.c\njemalloc/src/base.c\njemalloc/src/bitmap.c\njemalloc/src/ckh.c\njemalloc/src/ctl.c\njemalloc/src/extent.c\njemalloc/src/extent_dss.c\njemalloc/src/extent_mmap.c\njemalloc/src/hash.c\njemalloc/src/hooks.c\njemalloc/src/jemalloc.c\njemalloc/src/jemalloc_cpp.cpp\njemalloc/src/large.c\njemalloc/src/malloc_io.c\njemalloc/src/mutex.c\njemalloc/src/mutex_pool.c\njemalloc/src/nstime.c\njemalloc/src/pages.c\njemalloc/src/prng.c\njemalloc/src/prof.c\njemalloc/src/rtree.c\njemalloc/src/spin.c\njemalloc/src/stats.c\njemalloc/src/sz.c\njemalloc/src/tcache.c\njemalloc/src/ticker.c\njemalloc/src/tsd.c\njemalloc/src/witness.c\njemalloc/src/zone.c\njemalloc/test/include/test/SFMT-alti.h\njemalloc/test/include/test/SFMT-params.h\njemalloc/test/include/test/SFMT-params11213.h\njemalloc/test/include/test/SFMT-params1279.h\njemalloc/test/include/test/SFMT-params132049.h\njemalloc/test/include/test/SFMT-params19937.h\njemalloc/test/include/test/SFMT-params216091.h\njemalloc/test/include/test/SFMT-params2281.h\njemalloc/test/include/test/SFMT-params4253.h\njemalloc/test/include/test/SFMT-params44497.h\njemalloc/test/include/test/SFMT-params607.h\njemalloc/test/include/test/SFMT-params86243.h\njemalloc/test/include/test/SFMT-sse2.h\njemalloc/test/include/test/SFMT.h\njemalloc/test/include/test/btalloc.h\njemalloc/test/include/test/extent_hooks.h\njemalloc/test/include/test/jemalloc_test.h.in\njemalloc/test/include/test/jemalloc_test_defs.h.in\njemalloc/test/include/test/math.h\njemalloc/test/include/test/mq.h\njemalloc/test/include/test/mtx.h\njemalloc/test/include/test/test.h\njemalloc/test/include/test/thd.h\njemalloc/test/include/test/timer.h\njemalloc/test/integration/MALLOCX_ARENA.c\njemalloc/test/integration/aligned_alloc.c\njemalloc/test/integration/allocated.c\njemalloc/test/integration/extent.c\njemalloc/test/integration/extent.sh\njemalloc/test/integration/mallocx.c\njemalloc/test/integration/mallocx.sh\njemalloc/test/integration/overflow.c\njemalloc/test/integration/posix_memalign.c\njemalloc/test/integration/rallocx.c\njemalloc/test/integration/sdallocx.c\njemalloc/test/integration/thread_arena.c\njemalloc/test/integration/thread_tcache_enabled.c\njemalloc/test/integration/xallocx.c\njemalloc/test/integration/xallocx.sh\njemalloc/test/src/SFMT.c\njemalloc/test/src/btalloc.c\njemalloc/test/src/btalloc_0.c\njemalloc/test/src/btalloc_1.c\njemalloc/test/src/math.c\njemalloc/test/src/mq.c\njemalloc/test/src/mtx.c\njemalloc/test/src/test.c\njemalloc/test/src/thd.c\njemalloc/test/src/timer.c\njemalloc/test/stress/microbench.c\njemalloc/test/test.sh.in\njemalloc/test/unit/SFMT.c\njemalloc/test/unit/a0.c\njemalloc/test/unit/arena_reset.c\njemalloc/test/unit/arena_reset_prof.c\njemalloc/test/unit/arena_reset_prof.sh\njemalloc/test/unit/atomic.c\njemalloc/test/unit/background_thread.c\njemalloc/test/unit/base.c\njemalloc/test/unit/bit_util.c\njemalloc/test/unit/bitmap.c\njemalloc/test/unit/ckh.c\njemalloc/test/unit/decay.c\njemalloc/test/unit/decay.sh\njemalloc/test/unit/extent_quantize.c\njemalloc/test/unit/fork.c\njemalloc/test/unit/hash.c\njemalloc/test/unit/hooks.c\njemalloc/test/unit/junk.c\njemalloc/test/unit/junk.sh\njemalloc/test/unit/junk_alloc.c\njemalloc/test/unit/junk_alloc.sh\njemalloc/test/unit/junk_free.c\njemalloc/test/unit/junk_free.sh\njemalloc/test/unit/mallctl.c\njemalloc/test/unit/malloc_io.c\njemalloc/test/unit/math.c\njemalloc/test/unit/mq.c\njemalloc/test/unit/mtx.c\njemalloc/test/unit/nstime.c\njemalloc/test/unit/pack.c\njemalloc/test/unit/pack.sh\njemalloc/test/unit/pages.c\njemalloc/test/unit/ph.c\njemalloc/test/unit/prng.c\njemalloc/test/unit/prof_accum.c\njemalloc/test/unit/prof_accum.sh\njemalloc/test/unit/prof_active.c\njemalloc/test/unit/prof_active.sh\njemalloc/test/unit/prof_gdump.c\njemalloc/test/unit/prof_gdump.sh\njemalloc/test/unit/prof_idump.c\njemalloc/test/unit/prof_idump.sh\njemalloc/test/unit/prof_reset.c\njemalloc/test/unit/prof_reset.sh\njemalloc/test/unit/prof_tctx.c\njemalloc/test/unit/prof_tctx.sh\njemalloc/test/unit/prof_thread_name.c\njemalloc/test/unit/prof_thread_name.sh\njemalloc/test/unit/ql.c\njemalloc/test/unit/qr.c\njemalloc/test/unit/rb.c\njemalloc/test/unit/retained.c\njemalloc/test/unit/rtree.c\njemalloc/test/unit/size_classes.c\njemalloc/test/unit/slab.c\njemalloc/test/unit/smoothstep.c\njemalloc/test/unit/spin.c\njemalloc/test/unit/stats.c\njemalloc/test/unit/stats_print.c\njemalloc/test/unit/ticker.c\njemalloc/test/unit/tsd.c\njemalloc/test/unit/witness.c\njemalloc/test/unit/zero.c\njemalloc/test/unit/zero.sh\ninclude/memkind/internal/tbb_mem_pool_policy.h\n"
  },
  {
    "path": "deps/memkind/src/copying_headers/MANIFEST.freeBSD",
    "content": "astyle.sh\nautogen.sh\nbuild.sh\nbuild_jemalloc.sh\ninstall_astyle.sh\nMakefile.am\nconfigure.ac\nautohbw/Makefile.mk\nautohbw/autohbw.c\nautohbw/autohbw_get_src_lines.pl\nautohbw/autohbw_test.sh\nexamples/Makefile.mk\nexamples/autohbw_candidates.c\nexamples/filter_example.c\nexamples/hello_hbw_example.c\nexamples/memkind_allocated_example.cpp\nexamples/memkind_allocated.hpp\nexamples/hello_memkind_example.c\nexamples/memkind_decorator_debug.c\nman/memkind-hbw-nodes.1\nman/autohbw.7\nman/hbwmalloc.3\nman/memkind.3\nman/memkind_arena.3\nman/memkind_default.3\nman/memkind_hbw.3\nman/memkind_hugetlb.3\nman/hbwallocator.3\nman/pmemallocator.3\nmemkind.spec.mk\nsrc/heap_manager.c\nsrc/hbwmalloc.c\nsrc/memkind.c\nsrc/memkind_arena.c\nsrc/memkind_default.c\nsrc/memkind_gbtlb.c\nsrc/memkind_hbw.c\nsrc/memkind_regular.c\nsrc/memkind_hugetlb.c\nsrc/memkind_interleave.c\nsrc/memkind_log.c\nsrc/memkind-hbw-nodes.c\nsrc/tbb_wrapper.c\nsrc/Makefile.mk\ninclude/hbwmalloc.h\ninclude/memkind.h\ninclude/memkind_deprecated.h\ninclude/hbw_allocator.h\ninclude/pmem_allocator.h\ninclude/memkind/internal/heap_manager.h\ninclude/memkind/internal/memkind_arena.h\ninclude/memkind/internal/memkind_default.h\ninclude/memkind/internal/memkind_gbtlb.h\ninclude/memkind/internal/memkind_hbw.h\ninclude/memkind/internal/memkind_regular.h\ninclude/memkind/internal/memkind_hugetlb.h\ninclude/memkind/internal/memkind_interleave.h\ninclude/memkind/internal/memkind_pmem.h\ninclude/memkind/internal/memkind_private.h\ninclude/memkind/internal/memkind_log.h\ninclude/memkind/internal/tbb_wrapper.h\ntest/Makefile.mk\ntest/Allocator.hpp\ntest/TestPolicy.hpp\ntest/bat_tests.cpp\ntest/check.cpp\ntest/check.h\ntest/common.h\ntest/static_kinds_list.h\ntest/environ_err_hbw_malloc_test.cpp\ntest/gb_page_tests_bind_policy.cpp\ntest/main.cpp\ntest/multithreaded_tests.cpp\ntest/negative_tests.cpp\ntest/test.sh\ntest/trial_generator.cpp\ntest/trial_generator.h\ntest/error_message_tests.cpp\ntest/get_arena_test.cpp\ntest/locality_test.cpp\ntest/memkind_pmem_tests.cpp\ntest/memkind_pmem_long_time_tests.cpp\ntest/memory_footprint_test.cpp\ntest/memory_manager.h\ntest/random_sizes_allocator.h\ntest/proc_stat.h\ntest/static_kinds_tests.cpp\ntest/huge_page_test.cpp\ntest/hbw_verify_function_test.cpp\ntest/performance/framework.hpp\ntest/performance/framework.cpp\ntest/performance/operations.hpp\ntest/performance/perf_tests.hpp\ntest/performance/perf_tests.cpp\ntest/hbw_allocator_tests.cpp\ntest/decorator_test.cpp\ntest/decorator_test.h\ntest/alloc_performance_tests.cpp\ntest/allocator_perf_tool/AllocationSizes.hpp\ntest/allocator_perf_tool/Allocation_info.hpp\ntest/allocator_perf_tool/Allocation_info.cpp\ntest/allocator_perf_tool/Allocator.hpp\ntest/allocator_perf_tool/AllocatorFactory.hpp\ntest/allocator_perf_tool/CSVLogger.hpp\ntest/allocator_perf_tool/CommandLine.hpp\ntest/allocator_perf_tool/Configuration.hpp\ntest/allocator_perf_tool/ConsoleLog.hpp\ntest/allocator_perf_tool/FunctionCalls.hpp\ntest/allocator_perf_tool/FunctionCallsPerformanceTask.cpp\ntest/allocator_perf_tool/FunctionCallsPerformanceTask.h\ntest/allocator_perf_tool/GTestAdapter.hpp\ntest/allocator_perf_tool/Iterator.hpp\ntest/allocator_perf_tool/JemallocAllocatorWithTimer.hpp\ntest/allocator_perf_tool/Makefile\ntest/allocator_perf_tool/MemkindAllocatorWithTimer.hpp\ntest/allocator_perf_tool/Numastat.hpp\ntest/allocator_perf_tool/Runnable.hpp\ntest/allocator_perf_tool/PmemMockup.cpp\ntest/allocator_perf_tool/PmemMockup.hpp\ntest/allocator_perf_tool/ScenarioWorkload.cpp\ntest/allocator_perf_tool/ScenarioWorkload.h\ntest/allocator_perf_tool/StandardAllocatorWithTimer.hpp\ntest/allocator_perf_tool/Stats.hpp\ntest/allocator_perf_tool/StressIncreaseToMax.cpp\ntest/allocator_perf_tool/StressIncreaseToMax.h\ntest/allocator_perf_tool/Task.hpp\ntest/allocator_perf_tool/TaskFactory.hpp\ntest/allocator_perf_tool/Tests.hpp\ntest/allocator_perf_tool/Thread.hpp\ntest/allocator_perf_tool/TimerSysTime.hpp\ntest/allocator_perf_tool/VectorIterator.hpp\ntest/allocator_perf_tool/Workload.hpp\ntest/allocator_perf_tool/WrappersMacros.hpp\ntest/allocator_perf_tool/HugePageUnmap.hpp\ntest/allocator_perf_tool/HugePageOrganizer.hpp\ntest/allocator_perf_tool/HBWmallocAllocatorWithTimer.hpp\ntest/allocator_perf_tool/main.cpp\ntest/allocate_to_max_stress_test.cpp\ntest/memkind_versioning_tests.cpp\ntest/heap_manager_init_perf_test.cpp\ntest/autohbw_test_helper.c\ntest/trace_mechanism_test_helper.c\ntest/hbw_detection_test.py\ntest/autohbw_test.py\ntest/trace_mechanism_test.py\ntest/python_framework/cmd_helper.py\ntest/python_framework/huge_page_organizer.py\ntest/python_framework/__init__.py\ntest/draw_plots.py\ntest/run_alloc_benchmark.sh\ntest/alloc_benchmark.c\ntest/load_tbbmalloc_symbols.c\ntest/tbbmalloc.h\ntest/freeing_memory_segfault_test.cpp\ntest/dlopen_test.cpp\ntest/pmem_allocator_tests.cpp\nman/memkind_pmem.3\nsrc/memkind_pmem.c\n\n"
  },
  {
    "path": "deps/memkind/src/copying_headers/MANIFEST.freeBSD3",
    "content": "examples/pmem_kinds.c\nexamples/pmem_malloc.c\nexamples/pmem_malloc_unlimited.c\nexamples/pmem_usable_size.c\nexamples/pmem_alignment.c\nexamples/pmem_and_default_kind\nexamples/pmem_multithreads.c\nexamples/pmem_multithreads_onekind.c\nexamples/pmem_free_with_unknown_kind.c\nexamples/pmem_cpp_allocator.cpp\n"
  },
  {
    "path": "deps/memkind/src/copying_headers/header.freeBSD",
    "content": "/*\n * Copyright (C) 2014 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */"
  },
  {
    "path": "deps/memkind/src/copying_headers/header.freeBSD3",
    "content": "/*\n * Copyright (c) 2015, Intel Corporation\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions\n * are met:\n *\n *     * Redistributions of source code must retain the above copyright\n *       notice, this list of conditions and the following disclaimer.\n *\n *     * Redistributions in binary form must reproduce the above copyright\n *       notice, this list of conditions and the following disclaimer in\n *       the documentation and/or other materials provided with the\n *       distribution.\n *\n *     * Neither the name of Intel Corporation nor the names of its\n *       contributors may be used to endorse or promote products derived\n *       from this software without specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n * \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY LOG OF THE USE\n * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n"
  },
  {
    "path": "deps/memkind/src/copying_headers/header.intel-acpi",
    "content": "/*\n * Copyright (C) 2000 - 2014, Intel Corp.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions\n * are met:\n * 1. Redistributions of source code must retain the above copyright\n *    notice, this list of conditions, and the following disclaimer,\n *    without modification.\n * 2. Redistributions in binary form must reproduce at minimum a disclaimer\n *    substantially similar to the \"NO WARRANTY\" disclaimer below\n *    (\"Disclaimer\") and any redistribution must be conditioned upon\n *    including a substantially similar Disclaimer requirement for further\n *    binary redistribution.\n * 3. Neither the names of the above-listed copyright holders nor the names\n *    of any contributors may be used to endorse or promote products derived\n *    from this software without specific prior written permission.\n *\n * Alternatively, this software may be distributed under the terms of the\n * GNU General Public License (\"GPL\") version 2 as published by the Free\n * Software Foundation.\n *\n * NO WARRANTY\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n * \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR\n * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\n * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING\n * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGES.\n */"
  },
  {
    "path": "deps/memkind/src/debian/changelog",
    "content": "memkind (0.0.0) UNRELEASED; urgency=low\n\n  * See ChangeLog for more details\n\n -- Krzysztof Kulakowski <krzysztof.kulakowski@intel.com>  Mon, 12 Dec 2016 15:53:31 +0000\n"
  },
  {
    "path": "deps/memkind/src/debian/compat",
    "content": "9\n"
  },
  {
    "path": "deps/memkind/src/debian/control",
    "content": "Source: memkind\nSection: utils\nPriority: optional\nMaintainer: Krzysztof Kulakowski <krzysztof.kulakowski@intel.com>\nBuild-Depends: debhelper (>= 8.0.0)\nStandards-Version: 3.9.4\nHomepage: https://github.com/memkind/memkind\n\nPackage: memkind\nArchitecture: any\nDepends: ${shlibs:Depends}, ${misc:Depends}\nDescription:\n The memkind library is an user extensible heap manager built on top of\n jemalloc which enables control of memory characteristics and a\n partitioning of the heap between kinds of memory. The kinds of memory\n are defined by operating system memory policies that have been applied\n to virtual address ranges. Memory characteristics supported by\n memkind without user extension include control of NUMA and page size\n features. The jemalloc non-standard interface has been extended to\n enable specialized arenas to make requests for virtual memory from the\n operating system through the memkind partition interface. Through the\n other memkind interfaces the user can control and extend memory\n partition features and allocate memory while selecting enabled\n features. This software is being made available for early evaluation.\n Feedback on design or implementation is greatly appreciated.\n"
  },
  {
    "path": "deps/memkind/src/debian/rules",
    "content": "#!/usr/bin/make -f\n# -*- makefile -*-\n\n# Uncomment this to turn on verbose mode.\n#export DH_VERBOSE=1\n\n# ignore tests during build\noverride_dh_auto_test:\n\noverride_dh_auto_configure:\n\t./build_jemalloc.sh\n\t./autogen.sh\n\tdh_auto_configure -- --prefix=/usr --libdir=/usr/lib \\\n           --includedir=/usr/include --sbindir=/usr/sbin --enable-cxx11 \\\n           --mandir=/usr/share/man --docdir=/usr/share/doc/memkind\n\noverride_dh_auto_build:\n\tdh_auto_build -- checkprogs\n\n%:\n\tdh $@\n"
  },
  {
    "path": "deps/memkind/src/examples/Makefile.mk",
    "content": "#\n#  Copyright (C) 2014 - 2018 Intel Corporation.\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#  1. Redistributions of source code must retain the above copyright notice(s),\n#     this list of conditions and the following disclaimer.\n#  2. Redistributions in binary form must reproduce the above copyright notice(s),\n#     this list of conditions and the following disclaimer in the documentation\n#     and/or other materials provided with the distribution.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n#  EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n#  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n#\n\nnoinst_PROGRAMS += examples/hello_memkind \\\n                   examples/hello_memkind_debug \\\n                   examples/hello_hbw \\\n                   examples/filter_memkind \\\n                   examples/pmem_kinds \\\n                   examples/pmem_malloc \\\n                   examples/pmem_malloc_unlimited \\\n                   examples/pmem_usable_size \\\n                   examples/pmem_alignment \\\n                   examples/pmem_and_default_kind \\\n                   examples/pmem_multithreads \\\n                   examples/pmem_multithreads_onekind \\\n                   examples/pmem_free_with_unknown_kind \\\n                   examples/autohbw_candidates \\\n                   # end\nif HAVE_CXX11\nnoinst_PROGRAMS += examples/memkind_allocated\nnoinst_PROGRAMS += examples/pmem_cpp_allocator\nendif\n\nexamples_hello_memkind_LDADD = libmemkind.la\nexamples_hello_memkind_debug_LDADD = libmemkind.la\nexamples_hello_hbw_LDADD = libmemkind.la\nexamples_filter_memkind_LDADD = libmemkind.la\nexamples_pmem_kinds_LDADD = libmemkind.la\nexamples_pmem_malloc_LDADD = libmemkind.la\nexamples_pmem_malloc_unlimited_LDADD = libmemkind.la\nexamples_pmem_usable_size_LDADD = libmemkind.la\nexamples_pmem_alignment_LDADD = libmemkind.la\nexamples_pmem_and_default_kind_LDADD = libmemkind.la\nexamples_pmem_multithreads_LDADD = libmemkind.la\nexamples_pmem_multithreads_onekind_LDADD = libmemkind.la\nexamples_pmem_free_with_unknown_kind_LDADD = libmemkind.la\nexamples_autohbw_candidates_LDADD = libmemkind.la\n\nif HAVE_CXX11\nexamples_memkind_allocated_LDADD = libmemkind.la\nexamples_pmem_cpp_allocator_LDADD = libmemkind.la\nendif\n\nexamples_hello_memkind_SOURCES = examples/hello_memkind_example.c\nexamples_hello_memkind_debug_SOURCES = examples/hello_memkind_example.c examples/memkind_decorator_debug.c\nexamples_hello_hbw_SOURCES = examples/hello_hbw_example.c\nexamples_filter_memkind_SOURCES = examples/filter_example.c\nexamples_pmem_kinds_SOURCES = examples/pmem_kinds.c\nexamples_pmem_malloc_SOURCES = examples/pmem_malloc.c\nexamples_pmem_malloc_unlimited_SOURCES = examples/pmem_malloc_unlimited.c\nexamples_pmem_usable_size_SOURCES = examples/pmem_usable_size.c\nexamples_pmem_alignment_SOURCES = examples/pmem_alignment.c\nexamples_pmem_and_default_kind_SOURCES = examples/pmem_and_default_kind.c\nexamples_pmem_multithreads_SOURCES = examples/pmem_multithreads.c\nexamples_pmem_multithreads_onekind_SOURCES = examples/pmem_multithreads_onekind.c\nexamples_pmem_free_with_unknown_kind_SOURCES = examples/pmem_free_with_unknown_kind.c\nexamples_autohbw_candidates_SOURCES = examples/autohbw_candidates.c\nif HAVE_CXX11\nexamples_memkind_allocated_SOURCES = examples/memkind_allocated_example.cpp examples/memkind_allocated.hpp\nexamples_pmem_cpp_allocator_SOURCES = examples/pmem_cpp_allocator.cpp\nendif\n\nclean-local:\n\trm -f examples/*.gcno examples/*.gcda\n"
  },
  {
    "path": "deps/memkind/src/examples/README",
    "content": "# Memkind examples\n\nThe example directory contains example codes that use the memkind\ninterface.\n\n## PMEM\n\nThe pmem_*.c(pp) demonstrates how to create and use a file-backed memory kind.\nThe default pmem path is \"/tmp/\".\nCustom directory is pass as first argument to all of PMEM example programs,\ne.g. to execute pmem_malloc example in /mnt/pmem location, call:\n\n    ./pmem_malloc /mnt/pmem/\n\n### pmem_kinds.c\n\nThis example shows how to create and destroy pmem kind with defined or unlimited size.\n\n### pmem_malloc.c\n\nThis example shows how to allocate memory and possibility to exceed pmem kind size.\n\n### pmem_malloc_unlimited.c\n\nThis example shows how to allocate memory with unlimited kind size.\n\n### pmem_usable_size.c\n\nThis example shows difference between the expected and the actual allocation size.\n\n### pmem_alignment.c\n\nThis example shows how to use memkind alignment and how it affects allocations.\n\n### pmem_multithreads.c\n\nThis example shows how to use multithreading with independent pmem kinds.\n\n### pmem_multithreads_onekind.c\n\nThis example shows how to use multithreading with one main pmem kind.\n\n### pmem_and_default_kind.c\n\nThis example shows how to allocate in standard memory and file-backed memory (pmem kind).\n\n### pmem_free_with_unknown_kind.c\n\nThis example shows how to allocate in standard memory and file-backed memory (pmem kind)\nand free memory without a need to remember which kind it belongs to.\n\n### pmem_cpp_allocator.cpp\n\nThis example shows usage of C++ allocator mechanism designed for file-backed memory\nkind with different data structures like: vector, list and map.\n\n## Other memkind examples\n\nThe simplest example is the hello_example.c which is a hello world\nvariant.  The filter_example.c shows how you would use high bandwidth\nmemory to store a reduction of a larger data set stored in DDR. There is\nalso an example of how to create user defined kinds.  This example\ncreates kinds which isolate allocations to a single NUMA node each\nbacked by a single arena.\n\nThe memkind_allocated example is simple usage of memkind in C++11 which\nshows how memkind can be used to allocate objects, and consists of two files:\nmemkind_allocated.hpp - which is definition of template class that should be\ninherited and parametrized by derived type (curiously recurring template\npattern), to let deriving class allocate their objects using specified kind.\nmemkind_allocated_example.cpp - which is usage example of this approach.\nLogic of memkind_allocated is based on overriding operator new() in template,\nand allocating memory on kind specified in new() parameter, or by overridable\nstatic method getClassKind(). This implementation also supports alignment\nspecifier's (alignas() - new feature in C++11).\nThe downside of this approach is that it will work properly only if\nmemkind_allocated template is inherited once in inheritance chain (which\nprobably makes that not very useful for most scenarios). Other thing is that it\noverriding class new() operator which can cause various problems if used\nunwisely.\n"
  },
  {
    "path": "deps/memkind/src/examples/autohbw_candidates.c",
    "content": "/*\n * Copyright (C) 2015 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n///////////////////////////////////////////////////////////////////////////\n// File   : autohbw_candidates.c\n// Purpose: Shows which functions are interposed by AutoHBW library.\n//        : These functions can be used for testing purposes\n// Author : Ruchira Sasanka (ruchira.sasanka AT intel.com)\n// Date   : Sept 10, 2015\n///////////////////////////////////////////////////////////////////////////\n\n\n#include <memkind.h>\n\n#include <stdlib.h>\n#include <stdio.h>\n\n\n///////////////////////////////////////////////////////////////////////////\n// This function contains an example case for each heap allocation function\n// intercepted by the AutoHBW library\n///////////////////////////////////////////////////////////////////////////\n\n//volatile is needed to prevent optimizing out below hooks\nvolatile int memkind_called_g;\n\nvoid memkind_malloc_post(struct memkind *kind, size_t size, void **result)\n{\n    memkind_called_g = 1;\n}\nvoid memkind_calloc_post(struct memkind *kind, size_t nmemb, size_t size,\n                         void **result)\n{\n    memkind_called_g = 1;\n}\nvoid memkind_posix_memalign_post(struct memkind *kind, void **memptr,\n                                 size_t alignment, size_t size, int *err)\n{\n    memkind_called_g = 1;\n}\nvoid memkind_realloc_post(struct memkind *kind, void *ptr, size_t size,\n                          void **result)\n{\n    memkind_called_g = 1;\n}\nvoid memkind_free_pre(struct memkind **kind, void **ptr)\n{\n    memkind_called_g = 1;\n}\n\nvoid finish_testcase(int fail_condition, const char *fail_message, int *err)\n{\n\n    if(memkind_called_g != 1 || fail_condition) {\n        printf(\"%s\\n\", fail_message);\n        *err= -1;\n    }\n    memkind_called_g = 0;\n}\n\nint main()\n{\n    int err = 0;\n    const size_t size = 1024 * 1024;   // 1M of data\n\n    void *buf = NULL;\n    memkind_called_g = 0;\n\n    // Test 1: Test malloc and free\n    buf = malloc(size);\n    finish_testcase(buf==NULL, \"Malloc failed!\", &err);\n\n    free(buf);\n    finish_testcase(0, \"Free after malloc failed!\", &err);\n\n    // Test 2: Test calloc and free\n    buf = calloc(size, 1);\n    finish_testcase(buf==NULL, \"Calloc failed!\", &err);\n\n    free(buf);\n    finish_testcase(0, \"Free after calloc failed!\", &err);\n\n    // Test 3: Test realloc and free\n    buf = malloc(size);\n    finish_testcase(buf==NULL, \"Malloc before realloc failed!\", &err);\n\n    buf = realloc(buf,  size * 2);\n    finish_testcase(buf==NULL, \"Realloc failed!\", &err);\n\n    free(buf);\n    finish_testcase(0, \"Free after realloc failed!\", &err);\n\n    // Test 4: Test posix_memalign and free\n    int ret = posix_memalign(&buf,  64, size);\n    finish_testcase(ret, \"Posix_memalign failed!\", &err);\n\n    free(buf);\n    finish_testcase(0, \"Free after posix_memalign failed!\", &err);\n\n    return err;\n}\n"
  },
  {
    "path": "deps/memkind/src/examples/filter_example.c",
    "content": "/*\n * Copyright (C) 2014 - 2016 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind.h>\n\n#include <stdlib.h>\n#include <stdio.h>\n#include <errno.h>\n\nint main(int argc, char **argv)\n{\n    const size_t stream_len = 1024 * 1024;\n    const size_t filter_len = 1024;\n    const size_t num_filter = stream_len / filter_len;\n    size_t i, j;\n    double *stream = NULL;\n    double *filter = NULL;\n    double *result = NULL;\n\n    srandom(0);\n\n    stream = (double *)memkind_malloc(MEMKIND_DEFAULT, stream_len * sizeof(double));\n    if (stream == NULL) {\n        perror(\"<memkind>\");\n        fprintf(stderr, \"Unable to allocate stream\\n\");\n        return errno ? -errno : 1;\n    }\n\n    filter = (double *)memkind_malloc(MEMKIND_HBW, filter_len * sizeof(double));\n    if (filter == NULL) {\n        perror(\"<memkind>\");\n        fprintf(stderr, \"Unable to allocate filter\\n\");\n        return errno ? -errno : 1;\n    }\n\n    result = (double *)memkind_calloc(MEMKIND_HBW, filter_len, sizeof(double));\n    if (result == NULL) {\n        perror(\"<memkind>\");\n        fprintf(stderr, \"Unable to allocate result\\n\");\n        return errno ? -errno : 1;\n    }\n\n    for (i = 0; i < stream_len; i++) {\n        stream[i] = (double)(random())/(double)(RAND_MAX);\n    }\n\n    for (i = 0; i < filter_len; i++) {\n        filter[i] = (double)(i)/(double)(filter_len);\n    }\n\n    for (i = 0; i < num_filter; i++) {\n        for (j = 0; j < filter_len; j++) {\n            result[j] += stream[i * filter_len + j] * filter[j];\n        }\n    }\n\n    for (i = 0; i < filter_len; i++) {\n        fprintf(stdout, \"%.6e\\n\", result[i]);\n    }\n\n    memkind_free(MEMKIND_HBW, result);\n    memkind_free(MEMKIND_HBW, filter);\n    memkind_free(MEMKIND_DEFAULT, stream);\n\n    return 0;\n}\n"
  },
  {
    "path": "deps/memkind/src/examples/hello_hbw_example.c",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <hbwmalloc.h>\n\n#include <stdlib.h>\n#include <stdio.h>\n#include <errno.h>\n\nint main(int argc, char **argv)\n{\n    const size_t size = 512;\n    char *default_str = NULL;\n    char *hbw_str = NULL;\n    char *hbw_hugetlb_str = NULL;\n    int err = 0;\n\n    default_str = (char *)malloc(size);\n    if (default_str == NULL) {\n        perror(\"malloc()\");\n        fprintf(stderr, \"Unable to allocate default string\\n\");\n        err = errno ? -errno : 1;\n        goto exit;\n    }\n    hbw_str = (char *)hbw_malloc(size);\n    if (hbw_str == NULL) {\n        perror(\"hbw_malloc()\");\n        fprintf(stderr, \"Unable to allocate hbw string\\n\");\n        err = errno ? -errno : 1;\n        goto exit;\n    }\n    err = hbw_posix_memalign_psize((void **)&hbw_hugetlb_str, 2097152, size,\n                                   HBW_PAGESIZE_2MB);\n    if (err) {\n        perror(\"hbw_posix_memalign()\");\n        fprintf(stderr, \"Unable to allocate hbw hugetlb string\\n\");\n        err = errno ? -errno : 1;\n        goto exit;\n    }\n\n    sprintf(default_str, \"Hello world from standard memory\\n\");\n    sprintf(hbw_str, \"Hello world from high bandwidth memory\\n\");\n    sprintf(hbw_hugetlb_str, \"Hello world from high bandwidth 2 MB paged memory\\n\");\n\n    fprintf(stdout, \"%s\", default_str);\n    fprintf(stdout, \"%s\", hbw_str);\n    fprintf(stdout, \"%s\", hbw_hugetlb_str);\n\nexit:\n    if (hbw_hugetlb_str) {\n        hbw_free(hbw_hugetlb_str);\n    }\n    if (hbw_str) {\n        hbw_free(hbw_str);\n    }\n    if (default_str) {\n        free(default_str);\n    }\n    return err;\n}\n"
  },
  {
    "path": "deps/memkind/src/examples/hello_memkind_example.c",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind.h>\n\n#include <stdlib.h>\n#include <stdio.h>\n#include <errno.h>\n\nint main(int argc, char **argv)\n{\n    const size_t size = 512;\n    char *default_str = NULL;\n    char *hugetlb_str = NULL;\n    char *hbw_str = NULL;\n    char *hbw_hugetlb_str = NULL;\n    char *hbw_preferred_str = NULL;\n    char *hbw_preferred_hugetlb_str = NULL;\n    char *hbw_interleave_str = NULL;\n\n    default_str = (char *)memkind_malloc(MEMKIND_DEFAULT, size);\n    if (default_str == NULL) {\n        perror(\"memkind_malloc()\");\n        fprintf(stderr, \"Unable to allocate default string\\n\");\n        return errno ? -errno : 1;\n    }\n    hugetlb_str = (char *)memkind_malloc(MEMKIND_HUGETLB, size);\n    if (hugetlb_str == NULL) {\n        perror(\"memkind_malloc()\");\n        fprintf(stderr, \"Unable to allocate hugetlb string\\n\");\n        return errno ? -errno : 1;\n    }\n    hbw_str = (char *)memkind_malloc(MEMKIND_HBW, size);\n    if (hbw_str == NULL) {\n        perror(\"memkind_malloc()\");\n        fprintf(stderr, \"Unable to allocate hbw string\\n\");\n        return errno ? -errno : 1;\n    }\n    hbw_hugetlb_str = (char *)memkind_malloc(MEMKIND_HBW_HUGETLB, size);\n    if (hbw_hugetlb_str == NULL) {\n        perror(\"memkind_malloc()\");\n        fprintf(stderr, \"Unable to allocate hbw_hugetlb string\\n\");\n        return errno ? -errno : 1;\n    }\n    hbw_preferred_str = (char *)memkind_malloc(MEMKIND_HBW_PREFERRED, size);\n    if (hbw_preferred_str == NULL) {\n        perror(\"memkind_malloc()\");\n        fprintf(stderr, \"Unable to allocate hbw_preferred string\\n\");\n        return errno ? -errno : 1;\n    }\n    hbw_preferred_hugetlb_str = (char *)memkind_malloc(\n                                    MEMKIND_HBW_PREFERRED_HUGETLB, size);\n    if (hbw_preferred_hugetlb_str == NULL) {\n        perror(\"memkind_malloc()\");\n        fprintf(stderr, \"Unable to allocate hbw_preferred_hugetlb string\\n\");\n        return errno ? -errno : 1;\n    }\n    hbw_interleave_str = (char *)memkind_malloc(MEMKIND_HBW_INTERLEAVE, size);\n    if (hbw_interleave_str == NULL) {\n        perror(\"memkind_malloc()\");\n        fprintf(stderr,\"Unable to allocate hbw_interleave string\\n\");\n        return errno ? -errno : 1;\n    }\n\n    sprintf(default_str, \"Hello world from standard memory\\n\");\n    sprintf(hugetlb_str, \"Hello world from standard memory with 2 MB pages\\n\");\n    sprintf(hbw_str, \"Hello world from high bandwidth memory\\n\");\n    sprintf(hbw_hugetlb_str, \"Hello world from high bandwidth 2 MB paged memory\\n\");\n    sprintf(hbw_preferred_str,\n            \"Hello world from high bandwidth memory if sufficient resources exist\\n\");\n    sprintf(hbw_preferred_hugetlb_str,\n            \"Hello world from high bandwidth 2 MB paged memory if sufficient resources exist\\n\");\n\n    sprintf(hbw_interleave_str,\n            \"Hello world from high bandwidth interleaved memory\\n\");\n\n    fprintf(stdout, \"%s\", default_str);\n    fprintf(stdout, \"%s\", hugetlb_str);\n    fprintf(stdout, \"%s\", hbw_str);\n    fprintf(stdout, \"%s\", hbw_hugetlb_str);\n    fprintf(stdout, \"%s\", hbw_preferred_str);\n    fprintf(stdout, \"%s\", hbw_preferred_hugetlb_str);\n    fprintf(stdout, \"%s\", hbw_interleave_str);\n\n    memkind_free(MEMKIND_HBW_INTERLEAVE, hbw_interleave_str);\n    memkind_free(MEMKIND_HBW_PREFERRED_HUGETLB, hbw_preferred_hugetlb_str);\n    memkind_free(MEMKIND_HBW_PREFERRED, hbw_preferred_str);\n    memkind_free(MEMKIND_HBW_HUGETLB, hbw_hugetlb_str);\n    memkind_free(MEMKIND_HBW, hbw_str);\n    memkind_free(MEMKIND_HUGETLB, hugetlb_str);\n    memkind_free(MEMKIND_DEFAULT, default_str);\n\n    return 0;\n}\n"
  },
  {
    "path": "deps/memkind/src/examples/memkind_allocated.hpp",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n///////////////////////////////////////////////////////////////////////////////////////////////////////////\n\n#include <memkind.h>\n\n#include <cstdlib>\n#include <new>\n\ntemplate <class deriving_class>\nclass memkind_allocated\n{\npublic:\n    static memkind_t getClassKind()\n    {\n        return MEMKIND_DEFAULT;\n    }\n\n    void *operator new(std::size_t size)\n    {\n        return deriving_class::operator new(size, deriving_class::getClassKind());\n    }\n\n    void *operator new[](std::size_t size)\n    {\n        return deriving_class::operator new(size, deriving_class::getClassKind());\n    }\n\n    void *operator new(std::size_t size, memkind_t memory_kind)\n    {\n        void *result_ptr = NULL;\n        int allocation_result = 0;\n\n        //This check if deriving_class has specified alignment, which is suitable\n        //to be used with posix_memalign()\n        if(alignof(deriving_class) <  sizeof(void *)) {\n            result_ptr = memkind_malloc(memory_kind, size);\n            allocation_result = result_ptr ? 1 : 0;\n        } else {\n            allocation_result = memkind_posix_memalign(memory_kind, &result_ptr,\n                                                       alignof(deriving_class), size);\n        }\n\n        if(allocation_result) {\n            throw std::bad_alloc();\n        }\n\n        return result_ptr;\n    }\n\n    void *operator new[](std::size_t size, memkind_t memory_kind)\n    {\n        return deriving_class::operator new(size, memory_kind);\n    }\n\n    void operator delete(void *ptr, memkind_t memory_kind)\n    {\n        memkind_free(memory_kind, ptr);\n    }\n\n    void operator delete(void *ptr)\n    {\n        memkind_free(0, ptr);\n    }\n\n    void operator delete[](void *ptr)\n    {\n        deriving_class::operator delete(ptr);\n    }\n\n    void operator delete[](void *ptr, memkind_t memory_kind)\n    {\n        deriving_class::operator delete(ptr, memory_kind);\n    }\n\nprotected:\n    memkind_allocated()\n    {\n    }\n\n    ~memkind_allocated()\n    {\n    }\n\n};\n"
  },
  {
    "path": "deps/memkind/src/examples/memkind_allocated_example.cpp",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n///////////////////////////////////////////////////////////////////////////////////////////////////////////\n\n#include <memkind.h>\n#include \"memkind_allocated.hpp\"\n\n// This code is example usage of C++11 features with custom allocator,\n// support for C++11 is required.\n#include <iostream>\n\n#if __cplusplus > 199711L\n\n#include <cstdlib>\n#include <new>\n#include <string>\n\n//example class definition, which derive from  memkind_allocated template to\n//have objects allocated with memkind, and have alignment specified by alignas()\nclass alignas(128) memkind_allocated_example : public\n    memkind_allocated<memkind_allocated_example>\n{\n    std::string message;\n\npublic:\n    //Override method for returning class default kind to make objects be by default allocated on High-Bandwith Memory\n    static memkind_t getClassKind()\n    {\n        return MEMKIND_HBW;\n    }\n\n    memkind_allocated_example(std::string my_message)\n    {\n        this->message = my_message;\n    }\n\n    memkind_allocated_example()\n    {\n    }\n\n    void print_message()\n    {\n        std::cout << message << std::endl;\n        std::cout << \"Memory adress of this object is: \" <<  (void *)this << std::endl\n                  << std::endl;\n    }\n};\n\nint main()\n{\n    memkind_t specified_kind = MEMKIND_HBW_HUGETLB;\n\n    memkind_allocated_example *default_kind_example = new memkind_allocated_example(\n        std::string(\"This object has been allocated using class default kind, which is: MEMKIND_DEFAULT\") );\n    default_kind_example->print_message();\n    delete default_kind_example;\n\n    memkind_allocated_example *specified_kind_example = new(\n        specified_kind) memkind_allocated_example(\n        std::string(\"This object has been allocated using specified kind, which is: MEMKIND_HBW_HUGETLB\") );\n    specified_kind_example->print_message();\n    delete specified_kind_example;\n\n    //examples for using same aproach for allocating arrays of objects, note that objects created that way can be initialized only with default (unparameterized) constructor\n    memkind_allocated_example *default_kind_array_example = new\n    memkind_allocated_example[5]();\n    delete[] default_kind_array_example;\n\n    memkind_allocated_example *specified_kind_array_example = new(\n        specified_kind) memkind_allocated_example[5]();\n    delete[] specified_kind_array_example;\n\n    return 0;\n}\n#else //If C++11 is not avaiable - do nothing.\nint main()\n{\n    std::cout << \"WARNING: because your compiler does not support C++11 standard,\"\n              << std::endl;\n    std::cout << \"this example is only as a dummy placeholder.\" << std::endl;\n    return 0;\n}\n#endif\n"
  },
  {
    "path": "deps/memkind/src/examples/memkind_decorator_debug.c",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind.h>\n\n#include <stdlib.h>\n#include <stdio.h>\n\n/* This is an example that enables debug printing on every alloction call */\n\nstatic void memkind_debug(const char *func, memkind_t kind, size_t size,\n                          void *ptr)\n{\n    fprintf(stderr, \"[ DEBUG ] func=%s kind=%p size=%zu ptr=0x%lx\\n\", func, kind,\n            size, (size_t)ptr);\n}\n\nvoid memkind_malloc_post(memkind_t kind, size_t size, void **result)\n{\n    memkind_debug(\"memkind_malloc\", kind, size, *result);\n}\n\nvoid memkind_calloc_post(memkind_t kind, size_t nmemb, size_t size,\n                         void **result)\n{\n    memkind_debug(\"memkind_calloc\", kind, nmemb * size, *result);\n}\n\nvoid memkind_posix_memalign_post(memkind_t kind, void **memptr,\n                                 size_t alignment, size_t size, int *err)\n{\n    memkind_debug(\"memkind_posix_memalign\", kind, size, *memptr);\n}\n\nvoid memkind_realloc_post(memkind_t kind, void *ptr, size_t size, void **result)\n{\n    memkind_debug(\"memkind_realloc\", kind, size, *result);\n}\n\nvoid memkind_free_pre(memkind_t kind, void **ptr)\n{\n    memkind_debug(\"memkind_free\", kind, 0, *ptr);\n}\n"
  },
  {
    "path": "deps/memkind/src/examples/pmem_alignment.c",
    "content": "/*\n * Copyright (c) 2018 Intel Corporation\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions\n * are met:\n *\n *     * Redistributions of source code must retain the above copyright\n *       notice, this list of conditions and the following disclaimer.\n *\n *     * Redistributions in binary form must reproduce the above copyright\n *       notice, this list of conditions and the following disclaimer in\n *       the documentation and/or other materials provided with the\n *       distribution.\n *\n *     * Neither the name of Intel Corporation nor the names of its\n *       contributors may be used to endorse or promote products derived\n *       from this software without specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n * \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY LOG OF THE USE\n * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind.h>\n\n#include <stdio.h>\n#include <errno.h>\n#include <sys/stat.h>\n\n#define PMEM_MAX_SIZE (1024 * 1024 * 32)\n\nstatic char *PMEM_DIR = \"/tmp/\";\n\nint main(int argc, char *argv[])\n{\n    struct memkind *pmem_kind = NULL;\n    int err = 0;\n    struct stat st;\n\n    if (argc > 2) {\n        fprintf(stderr, \"Usage: %s [pmem_kind_dir_path]\", argv[0]);\n        return 1;\n    } else if (argc == 2) {\n        if (stat(argv[1], &st) != 0 || !S_ISDIR(st.st_mode)) {\n            fprintf(stderr, \"%s : Invalid path to pmem kind directory\", argv[1]);\n            return 1;\n        } else {\n            PMEM_DIR = argv[1];\n        }\n    }\n\n    fprintf(stdout,\n            \"This example shows how to use memkind alignment and how it affects allocations.\\nPMEM kind directory: %s\\n\",\n            PMEM_DIR);\n\n    /* Create PMEM partition with PMEM_MAX_SIZE size */\n    err = memkind_create_pmem(PMEM_DIR, PMEM_MAX_SIZE, &pmem_kind);\n    if (err) {\n        perror(\"memkind_create_pmem()\");\n        fprintf(stderr, \"Unable to create pmem partition err=%d errno=%d\\n\", err,\n                errno);\n        return errno ? -errno : 1;\n    }\n\n    char *pmem_str10 = NULL;\n    char *pmem_str11 = NULL;\n\n    /* Lets make two 32 bytes allocations */\n    pmem_str10 = (char *)memkind_malloc(pmem_kind, 32);\n    if (pmem_str10 == NULL) {\n        perror(\"memkind_malloc()\");\n        fprintf(stderr, \"Unable to allocate pmem string (pmem_str10)\\n\");\n        return errno ? -errno : 1;\n    }\n\n    pmem_str11 = (char *)memkind_malloc(pmem_kind, 32);\n    if (pmem_str11 == NULL) {\n        perror(\"memkind_malloc()\");\n        fprintf(stderr, \"Unable to allocate pmem string (pmem_str11)\\n\");\n        return errno ? -errno : 1;\n    }\n\n    /* They will be very close to each other in memory */\n    if (pmem_str11 - pmem_str10 != 32) {\n        fprintf(stderr, \"Something went wrong\\n\");\n        return 1;\n    }\n\n    memkind_free(pmem_kind, pmem_str10);\n    memkind_free(pmem_kind, pmem_str11);\n\n    /* Lets make two 32 bytes allocations with alignment 64 this time */\n    err = memkind_posix_memalign(pmem_kind, (void **)&pmem_str10, 64, 32);\n    if (err) {\n        perror(\"memkind_posix_memalign()\");\n        fprintf(stderr, \"Unable to allocate pmem string (pmem_str10) with alignment\\n\");\n        return errno ? -errno : 1;\n    }\n\n    err = memkind_posix_memalign(pmem_kind, (void **)&pmem_str11, 64, 32);\n    if (err) {\n        perror(\"memkind_posix_memalign()\");\n        fprintf(stderr, \"Unable to allocate pmem string (pmem_str11) with alignment\\n\");\n        return errno ? -errno : 1;\n    }\n\n    /* This time addresses are not close to each other, they are aligned to 64 */\n    if (pmem_str11 - pmem_str10 != 64) {\n        fprintf(stderr, \"Something went wrong with alignment allocation\\n\");\n        return 1;\n    }\n\n    memkind_free(pmem_kind, pmem_str10);\n    memkind_free(pmem_kind, pmem_str11);\n\n    err = memkind_destroy_kind(pmem_kind);\n    if (err) {\n        perror(\"memkind_destroy_kind()\");\n        fprintf(stderr, \"Unable to destroy pmem partition\\n\");\n        return errno ? -errno : 1;\n    }\n\n    fprintf(stdout,\n            \"The memory has been successfully allocated using memkind alignment.\");\n\n    return 0;\n}\n"
  },
  {
    "path": "deps/memkind/src/examples/pmem_and_default_kind.c",
    "content": "/*\n * Copyright (c) 2018 Intel Corporation\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions\n * are met:\n *\n *     * Redistributions of source code must retain the above copyright\n *       notice, this list of conditions and the following disclaimer.\n *\n *     * Redistributions in binary form must reproduce the above copyright\n *       notice, this list of conditions and the following disclaimer in\n *       the documentation and/or other materials provided with the\n *       distribution.\n *\n *     * Neither the name of Intel Corporation nor the names of its\n *       contributors may be used to endorse or promote products derived\n *       from this software without specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n * \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY LOG OF THE USE\n * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind.h>\n\n#include <stdio.h>\n#include <errno.h>\n#include <sys/resource.h>\n#include <sys/stat.h>\n#include <sys/time.h>\n\n#define MB (1024 * 1024)\n#define HEAP_LIMIT_SIMULATE (1024 * MB)\n\nstatic char *PMEM_DIR = \"/tmp/\";\n\nint main(int argc, char *argv[])\n{\n    const size_t size = 512;\n    struct memkind *pmem_kind = NULL;\n    int err = 0;\n    errno = 0;\n    struct stat st;\n\n    if (argc > 2) {\n        fprintf(stderr, \"Usage: %s [pmem_kind_dir_path]\", argv[0]);\n        return 1;\n    } else if (argc == 2) {\n        if (stat(argv[1], &st) != 0 || !S_ISDIR(st.st_mode)) {\n            fprintf(stderr, \"%s : Invalid path to pmem kind directory\", argv[1]);\n            return 1;\n        } else {\n            PMEM_DIR = argv[1];\n        }\n    }\n\n    //operation below limit the current size of heap\n    //to show different place of allocation\n    const struct rlimit heap_limit = { HEAP_LIMIT_SIMULATE, HEAP_LIMIT_SIMULATE };\n    err = setrlimit(RLIMIT_DATA, &heap_limit);\n    if (err) {\n        perror(\"setrlimit()\");\n        fprintf(stderr, \"Unable to set heap limit\\n\");\n        return errno ? -errno : 1;\n    }\n\n    char *ptr_default = NULL;\n    char *ptr_default_not_possible = NULL;\n    char *ptr_pmem = NULL;\n\n    fprintf(stdout,\n            \"This example shows how to allocate memory using standard memory (MEMKIND_DEFAULT) \"\n            \"and file-backed kind of memory (PMEM).\\nPMEM kind directory: %s\\n\",\n            PMEM_DIR);\n\n    err = memkind_create_pmem(PMEM_DIR, 0, &pmem_kind);\n    if (err) {\n        perror(\"memkind_create_pmem()\");\n        fprintf(stderr, \"Unable to create pmem partition err=%d errno=%d\\n\", err,\n                errno);\n        return errno ? -errno : 1;\n    }\n\n    ptr_default = (char *)memkind_malloc(MEMKIND_DEFAULT, size);\n    if (!ptr_default) {\n        perror(\"memkind_malloc()\");\n        fprintf(stderr, \"Unable allocate 512 bytes in standard memory\");\n        return errno ? -errno : 1;\n    }\n\n    errno = 0;\n    ptr_default_not_possible = (char *)memkind_malloc(MEMKIND_DEFAULT,\n                                                      HEAP_LIMIT_SIMULATE);\n    if (ptr_default_not_possible) {\n        perror(\"memkind_malloc()\");\n        fprintf(stderr,\n                \"Failure, this allocation should not be possible \"\n                \"(expected result was NULL), because of setlimit function\\n\");\n        return errno ? -errno : 1;\n    }\n    if (errno != ENOMEM) {\n        perror(\"memkind_malloc()\");\n        fprintf(stderr,\n                \"Failure, this allocation should set errno to ENOMEM value, because of setlimit function\\n\");\n        return errno ? -errno : 1;\n    }\n\n    errno = 0;\n    ptr_pmem = (char *)memkind_malloc(pmem_kind, HEAP_LIMIT_SIMULATE);\n    if (!ptr_pmem) {\n        perror(\"memkind_malloc()\");\n        fprintf(stderr, \"Unable allocate HEAP_LIMIT_SIMULATE in file-backed memory\");\n        return errno ? -errno : 1;\n    }\n    if (errno != 0) {\n        perror(\"memkind_malloc()\");\n        fprintf(stderr, \"Failure, this allocation should not set errno value\\n\");\n        return errno ? -errno : 1;\n    }\n\n    sprintf(ptr_default, \"Hello world from standard memory - ptr_default\\n\");\n    sprintf(ptr_pmem, \"Hello world from file-backed memory - ptr_pmem\\n\");\n\n    fprintf(stdout, \"%s\", ptr_default);\n    fprintf(stdout, \"%s\", ptr_pmem);\n\n    memkind_free(MEMKIND_DEFAULT, ptr_default);\n    memkind_free(pmem_kind, ptr_pmem);\n\n    err = memkind_destroy_kind(pmem_kind);\n    if (err) {\n        perror(\"memkind_destroy_kind()\");\n        fprintf(stderr, \"Unable to destroy pmem partition\\n\");\n        return errno ? -errno : 1;\n    }\n\n    fprintf(stdout, \"Memory was successfully allocated and released.\\n\");\n\n    return 0;\n}\n"
  },
  {
    "path": "deps/memkind/src/examples/pmem_cpp_allocator.cpp",
    "content": "/*\n * Copyright (c) 2018 Intel Corporation\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions\n * are met:\n *\n *     * Redistributions of source code must retain the above copyright\n *       notice, this list of conditions and the following disclaimer.\n *\n *     * Redistributions in binary form must reproduce the above copyright\n *       notice, this list of conditions and the following disclaimer in\n *       the documentation and/or other materials provided with the\n *       distribution.\n *\n *     * Neither the name of Intel Corporation nor the names of its\n *       contributors may be used to endorse or promote products derived\n *       from this software without specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n * \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY LOG OF THE USE\n * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n\n#include \"pmem_allocator.h\"\n\n#include <sys/stat.h>\n#include <iostream>\n#include <vector>\n#include <list>\n#include <map>\n#include <string>\n#include <scoped_allocator>\n#include <cassert>\n\n#define STL_VECTOR_TEST\n#define STL_LIST_TEST\n#if _GLIBCXX_USE_CXX11_ABI\n#define STL_VEC_STRING_TEST\n#define STL_MAP_INT_STRING_TEST\n#endif\n\nvoid cpp_allocator_test(const char *pmem_directory)\n{\n    std::cout << \"TEST SCOPE: HELLO\" << std::endl;\n\n    size_t pmem_max_size = 1024*1024*1024;\n\n#ifdef STL_VECTOR_TEST\n    {\n        std::cout << \"VECTOR OPEN\" << std::endl;\n        pmem::allocator<int> alc{ pmem_directory, pmem_max_size };\n        std::vector<int, pmem::allocator<int>> vector{ alc };\n\n        for (int i = 0; i < 20; ++i) {\n            vector.push_back(0xDEAD + i);\n            assert(vector.back() == 0xDEAD + i);\n        }\n\n        std::cout << \"VECTOR CLOSE\" << std::endl;\n    }\n#endif\n\n#ifdef STL_LIST_TEST\n    {\n        std::cout << \"LIST OPEN\" << std::endl;\n        pmem::allocator<int> alc{ pmem_directory, pmem_max_size };\n        std::list<int, pmem::allocator<int>> list{ alc };\n\n        const int nx2 = 4;\n        for (int i = 0; i < nx2; ++i) {\n            list.emplace_back(0xBEAC011 + i);\n            assert(list.back() == 0xBEAC011 + i);\n        }\n\n        for (int i = 0; i < nx2; ++i) {\n            list.pop_back();\n        }\n\n        std::cout << \"LIST CLOSE\" << std::endl;\n    }\n#endif\n\n#ifdef STL_VEC_STRING_TEST\n    {\n        std::cout << \"STRINGED VECTOR OPEN\" << std::endl;\n        typedef pmem::allocator<char> str_alloc_t;\n        typedef std::basic_string<char, std::char_traits<char>, str_alloc_t>\n        pmem_string;\n        typedef pmem::allocator<pmem_string> vec_alloc_t;\n\n        vec_alloc_t vec_alloc{ pmem_directory, pmem_max_size };\n        str_alloc_t str_alloc{ pmem_directory, pmem_max_size };\n\n        std::vector<pmem_string, std::scoped_allocator_adaptor<vec_alloc_t> >\n        vec{ std::scoped_allocator_adaptor<vec_alloc_t>(vec_alloc) };\n\n        pmem_string arg{ \"Very very loooong striiiing\", str_alloc };\n\n        vec.push_back(arg);\n        assert(vec.back() == arg);\n\n        std::cout << \"STRINGED VECTOR CLOSE\" << std::endl;\n    }\n\n#endif\n\n#ifdef STL_MAP_INT_STRING_TEST\n    {\n        std::cout << \"INT_STRING MAP OPEN\" << std::endl;\n        typedef std::basic_string<char, std::char_traits<char>, pmem::allocator<char>>\n                                                                                    pmem_string;\n        typedef int key_t;\n        typedef pmem_string value_t;\n        typedef pmem::allocator<char> allocator_t;\n        typedef std::map<key_t, value_t, std::less<key_t>, std::scoped_allocator_adaptor<allocator_t>>\n                map_t;\n\n        allocator_t allocator( pmem_directory, pmem_max_size );\n\n        value_t source_str1(\"Lorem ipsum dolor \", allocator);\n        value_t source_str2(\"sit amet consectetuer adipiscing elit\", allocator );\n\n        map_t target_map{ std::scoped_allocator_adaptor<allocator_t>(allocator) };\n\n        target_map[key_t(165)] = source_str1;\n        assert(target_map[key_t(165)] == source_str1);\n        target_map[key_t(165)] = source_str2;\n        assert(target_map[key_t(165)] == source_str2);\n\n        std::cout << \"INT_STRING MAP CLOSE\" << std::endl;\n    }\n#endif\n    std::cout << \"TEST SCOPE: GOODBYE\" << std::endl;\n}\n\nint main(int argc, char *argv[])\n{\n    const char *pmem_directory = \"/tmp/\";\n\n    if (argc > 2) {\n        std::cerr << \"Usage: pmem_cpp_allocator [directory path]\\n\"\n                  << \"\\t[directory path] - directory where temporary file is created (default = \\\"/tmp/\\\")\"\n                  << std::endl;\n        return 0;\n    } else if (argc == 2) {\n        struct stat st;\n        if (stat(argv[1], &st) != 0 || !S_ISDIR(st.st_mode)) {\n            fprintf(stderr,\"%s : Invalid path to pmem kind directory\", argv[1]);\n            return 1;\n        }\n        pmem_directory = argv[1];\n    }\n\n    cpp_allocator_test(pmem_directory);\n    return 0;\n}\n"
  },
  {
    "path": "deps/memkind/src/examples/pmem_free_with_unknown_kind.c",
    "content": "/*\n * Copyright (c) 2018 Intel Corporation\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions\n * are met:\n *\n *     * Redistributions of source code must retain the above copyright\n *       notice, this list of conditions and the following disclaimer.\n *\n *     * Redistributions in binary form must reproduce the above copyright\n *       notice, this list of conditions and the following disclaimer in\n *       the documentation and/or other materials provided with the\n *       distribution.\n *\n *     * Neither the name of Intel Corporation nor the names of its\n *       contributors may be used to endorse or promote products derived\n *       from this software without specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n * \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY LOG OF THE USE\n * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind.h>\n\n#include <stdio.h>\n#include <errno.h>\n#include <sys/stat.h>\n\nstatic char *PMEM_DIR = \"/tmp/\";\nstatic const size_t PMEM_PART_SIZE = MEMKIND_PMEM_MIN_SIZE + 4 * 1024;\n\nint main(int argc, char **argv)\n{\n    const size_t size = 512;\n    struct memkind *pmem_kind = NULL;\n    struct stat st;\n    const int arraySize = 100;\n    char *ptr[100] = { NULL };\n    int i = 0;\n    int err = 0;\n\n    if (argc > 2) {\n        fprintf(stderr, \"Usage: %s [pmem_kind_dir_path]\", argv[0]);\n        return 1;\n    } else if (argc == 2) {\n        if (stat(argv[1], &st) != 0 || !S_ISDIR(st.st_mode)) {\n            fprintf(stderr, \"%s : Invalid path to pmem kind directory\", argv[1]);\n            return 1;\n        } else {\n            PMEM_DIR = argv[1];\n        }\n    }\n\n    fprintf(stdout,\n            \"This example shows how to use memkind_free with unknown kind as a parameter.\\n\");\n\n    err = memkind_create_pmem(PMEM_DIR, PMEM_PART_SIZE, &pmem_kind);\n    if (err) {\n        perror(\"memkind_create_pmem()\");\n        fprintf(stderr, \"Unable to create pmem partition err=%d errno=%d\\n\", err,\n                errno);\n        return errno ? -errno : 1;\n    }\n\n    for (i = 0; i < arraySize; ++i) {\n        if (i < 50) {\n            ptr[i] = memkind_malloc(MEMKIND_DEFAULT, size);\n            if (ptr[i] == NULL) {\n                perror(\"memkind_malloc()\");\n                fprintf(stderr, \"Unable to allocate memkind default\\n\");\n                return errno ? -errno : 1;\n            }\n        } else {\n            ptr[i] = memkind_malloc(pmem_kind, size);\n            if (ptr[i] == NULL) {\n                perror(\"memkind_malloc()\");\n                fprintf(stderr, \"Unable to allocate pmem\\n\");\n                return errno ? -errno : 1;\n            }\n        }\n    }\n    fprintf(stdout,\n            \"Memory was successfully allocated in default kind and pmem kind.\\n\");\n\n    sprintf(ptr[10], \"Hello world from standard memory - ptr[10].\\n\");\n    sprintf(ptr[40], \"Hello world from standard memory - ptr[40].\\n\");\n    sprintf(ptr[80], \"Hello world from persistent memory - ptr[80].\\n\");\n\n    fprintf(stdout, \"%s\", ptr[10]);\n    fprintf(stdout, \"%s\", ptr[40]);\n    fprintf(stdout, \"%s\", ptr[80]);\n\n    fprintf(stdout, \"Free memory without specifying kind.\\n\");\n    for (i = 0; i < arraySize; ++i) {\n        memkind_free(NULL, ptr[i]);\n    }\n\n    err = memkind_destroy_kind(pmem_kind);\n    if (err) {\n        perror(\"memkind_destroy_kind()\");\n        fprintf(stderr, \"Unable to destroy pmem partition\\n\");\n        return errno ? -errno : 1;\n    }\n\n    fprintf(stdout, \"Memory was successfully released.\\n\");\n\n    return 0;\n}\n"
  },
  {
    "path": "deps/memkind/src/examples/pmem_kinds.c",
    "content": "/*\n * Copyright (c) 2018 Intel Corporation\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions\n * are met:\n *\n *     * Redistributions of source code must retain the above copyright\n *       notice, this list of conditions and the following disclaimer.\n *\n *     * Redistributions in binary form must reproduce the above copyright\n *       notice, this list of conditions and the following disclaimer in\n *       the documentation and/or other materials provided with the\n *       distribution.\n *\n *     * Neither the name of Intel Corporation nor the names of its\n *       contributors may be used to endorse or promote products derived\n *       from this software without specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n * \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY LOG OF THE USE\n * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind.h>\n\n#include <stdio.h>\n#include <errno.h>\n#include <sys/stat.h>\n\n#define PMEM_MAX_SIZE (1024 * 1024 * 32)\n\nstatic char *PMEM_DIR = \"/tmp/\";\n\nint main(int argc, char *argv[])\n{\n    struct memkind *pmem_kinds[10] = {NULL};\n    struct memkind *pmem_kind = NULL;\n    struct memkind *pmem_kind_unlimited = NULL;\n\n    int err = 0, i = 0;\n    struct stat st;\n\n    if (argc > 2) {\n        fprintf(stderr, \"Usage: %s [pmem_kind_dir_path]\", argv[0]);\n        return 1;\n    } else if (argc == 2) {\n        if (stat(argv[1], &st) != 0 || !S_ISDIR(st.st_mode)) {\n            fprintf(stderr, \"%s : Invalid path to pmem kind directory\", argv[1]);\n            return 1;\n        } else {\n            PMEM_DIR = argv[1];\n        }\n    }\n\n    fprintf(stdout,\n            \"This example shows how to create and destroy pmem kind with defined or unlimited size.\"\n            \"\\nPMEM kind directory: %s\\n\",\n            PMEM_DIR);\n\n    /* Create PMEM partition with specific size */\n    err = memkind_create_pmem(PMEM_DIR, PMEM_MAX_SIZE, &pmem_kind);\n    if (err) {\n        perror(\"memkind_create_pmem()\");\n        fprintf(stderr, \"Unable to create pmem partition err=%d errno=%d\\n\", err,\n                errno);\n        return errno ? -errno : 1;\n    }\n\n    /* Create PMEM partition with unlimited size */\n    err = memkind_create_pmem(PMEM_DIR, 0, &pmem_kind_unlimited);\n    if (err) {\n        perror(\"memkind_create_pmem()\");\n        fprintf(stderr, \"Unable to create pmem partition err=%d errno=%d\\n\", err,\n                errno);\n        return errno ? -errno : 1;\n    }\n\n    /* and delete them */\n    err = memkind_destroy_kind(pmem_kind);\n    if (err) {\n        perror(\"memkind_destroy_kind()\");\n        fprintf(stderr, \"Unable to destroy pmem partition\\n\");\n        return errno ? -errno : 1;\n    }\n\n    err = memkind_destroy_kind(pmem_kind_unlimited);\n    if (err) {\n        perror(\"memkind_destroy_kind()\");\n        fprintf(stderr, \"Unable to destroy pmem partition\\n\");\n        return errno ? -errno : 1;\n    }\n\n    /* Create many PMEM kinds */\n    for (i = 0; i < 10; i++) {\n        err = memkind_create_pmem(PMEM_DIR, PMEM_MAX_SIZE, &pmem_kinds[i]);\n        if (err) {\n            perror(\"memkind_create_pmem()\");\n            fprintf(stderr, \"Unable to create pmem partition err=%d errno=%d\\n\", err,\n                    errno);\n            return errno ? -errno : 1;\n        }\n    }\n\n    /* and delete them */\n    for (i = 0; i < 10; i++) {\n        err = memkind_destroy_kind(pmem_kinds[i]);\n        if (err) {\n            perror(\"memkind_pmem_destroy()\");\n            fprintf(stderr, \"Unable to destroy pmem partition\\n\");\n            return errno ? -errno : 1;\n        }\n    }\n\n    fprintf(stdout, \"PMEM kinds have been successfully created and destroyed.\");\n\n    return 0;\n}\n"
  },
  {
    "path": "deps/memkind/src/examples/pmem_malloc.c",
    "content": "/*\n * Copyright (c) 2015 - 2018 Intel Corporation\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions\n * are met:\n *\n *     * Redistributions of source code must retain the above copyright\n *       notice, this list of conditions and the following disclaimer.\n *\n *     * Redistributions in binary form must reproduce the above copyright\n *       notice, this list of conditions and the following disclaimer in\n *       the documentation and/or other materials provided with the\n *       distribution.\n *\n *     * Neither the name of Intel Corporation nor the names of its\n *       contributors may be used to endorse or promote products derived\n *       from this software without specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n * \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY LOG OF THE USE\n * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind.h>\n\n#include <stdio.h>\n#include <errno.h>\n#include <sys/stat.h>\n\n#define PMEM_MAX_SIZE (1024 * 1024 * 32)\n\nstatic char *PMEM_DIR = \"/tmp/\";\n\nint main(int argc, char *argv[])\n{\n    struct memkind *pmem_kind = NULL;\n    int err = 0;\n    struct stat st;\n\n    if (argc > 2) {\n        fprintf(stderr, \"Usage: %s [pmem_kind_dir_path]\", argv[0]);\n        return 1;\n    } else if (argc == 2) {\n        if (stat(argv[1], &st) != 0 || !S_ISDIR(st.st_mode)) {\n            fprintf(stderr, \"%s : Invalid path to pmem kind directory\", argv[1]);\n            return 1;\n        } else {\n            PMEM_DIR = argv[1];\n        }\n    }\n\n    fprintf(stdout,\n            \"This example shows how to allocate memory and possibility to exceed pmem kind size.\"\n            \"\\nPMEM kind directory: %s\\n\",\n            PMEM_DIR);\n\n    /* Create PMEM partition with specific size */\n    err = memkind_create_pmem(PMEM_DIR, PMEM_MAX_SIZE, &pmem_kind);\n    if (err) {\n        perror(\"memkind_create_pmem()\");\n        fprintf(stderr, \"Unable to create pmem partition err=%d errno=%d\\n\", err,\n                errno);\n        return errno ? -errno : 1;\n    }\n\n    char *pmem_str1 = NULL;\n    char *pmem_str2 = NULL;\n    char *pmem_str3 = NULL;\n    char *pmem_str4 = NULL;\n\n    // allocate 512 Bytes of 32 MB available\n    pmem_str1 = (char *)memkind_malloc(pmem_kind, 512);\n    if (pmem_str1 == NULL) {\n        perror(\"memkind_malloc()\");\n        fprintf(stderr, \"Unable to allocate pmem string (pmem_str1)\\n\");\n        return errno ? -errno : 1;\n    }\n\n    // allocate 8 MB of 31.9 MB available\n    pmem_str2 = (char *)memkind_malloc(pmem_kind, 8 * 1024 * 1024);\n    if (pmem_str2 == NULL) {\n        perror(\"memkind_malloc()\");\n        fprintf(stderr, \"Unable to allocate pmem string (pmem_str11)\\n\");\n        return errno ? -errno : 1;\n    }\n\n    // allocate 16 MB of 23.9 MB available\n    pmem_str3 = (char *)memkind_malloc(pmem_kind, 16 * 1024 * 1024);\n    if (pmem_str3 == NULL) {\n        perror(\"memkind_malloc()\");\n        fprintf(stderr, \"Unable to allocate pmem string (pmem_str12)\\n\");\n        return errno ? -errno : 1;\n    }\n\n    // allocate 16 MB of 7.9 MB available -- Out Of Memory expected\n    pmem_str4 = (char *)memkind_malloc(pmem_kind, 16 * 1024 * 1024);\n    if (pmem_str4 != NULL) {\n        perror(\"memkind_malloc()\");\n        fprintf(stderr,\n                \"Failure, this allocation should not be possible (expected result was NULL)\\n\");\n        return errno ? -errno : 1;\n    }\n\n    sprintf(pmem_str1, \"Hello world from pmem - pmem_str1\\n\");\n    sprintf(pmem_str2, \"Hello world from pmem - pmem_str2\\n\");\n    sprintf(pmem_str3, \"Hello world from persistent memory - pmem_str3\\n\");\n\n    fprintf(stdout, \"%s\", pmem_str1);\n    fprintf(stdout, \"%s\", pmem_str2);\n    fprintf(stdout, \"%s\", pmem_str3);\n\n    memkind_free(pmem_kind, pmem_str1);\n    memkind_free(pmem_kind, pmem_str2);\n    memkind_free(pmem_kind, pmem_str3);\n\n    err = memkind_destroy_kind(pmem_kind);\n    if (err) {\n        perror(\"memkind_destroy_kind()\");\n        fprintf(stderr, \"Unable to destroy pmem partition\\n\");\n        return errno ? -errno : 1;\n    }\n\n    fprintf(stdout, \"Memory was successfully allocated and released.\\n\");\n\n    return 0;\n}\n"
  },
  {
    "path": "deps/memkind/src/examples/pmem_malloc_unlimited.c",
    "content": "/*\n * Copyright (c) 2018 Intel Corporation\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions\n * are met:\n *\n *     * Redistributions of source code must retain the above copyright\n *       notice, this list of conditions and the following disclaimer.\n *\n *     * Redistributions in binary form must reproduce the above copyright\n *       notice, this list of conditions and the following disclaimer in\n *       the documentation and/or other materials provided with the\n *       distribution.\n *\n *     * Neither the name of Intel Corporation nor the names of its\n *       contributors may be used to endorse or promote products derived\n *       from this software without specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n * \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY LOG OF THE USE\n * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind.h>\n\n#include <stdio.h>\n#include <errno.h>\n#include <sys/stat.h>\n\nstatic char *PMEM_DIR = \"/tmp/\";\n\nint main(int argc, char *argv[])\n{\n    struct memkind *pmem_kind_unlimited = NULL;\n    int err = 0;\n    struct stat st;\n\n    if (argc > 2) {\n        fprintf(stderr, \"Usage: %s [pmem_kind_dir_path]\", argv[0]);\n        return 1;\n    } else if (argc == 2) {\n        if (stat(argv[1], &st) != 0 || !S_ISDIR(st.st_mode)) {\n            fprintf(stderr, \"%s : Invalid path to pmem kind directory \", argv[1]);\n            return 1;\n        } else {\n            PMEM_DIR = argv[1];\n        }\n    }\n\n    fprintf(stdout,\n            \"This example shows how to allocate memory with unlimited kind size.\"\n            \"nPMEM kind directory: %s\\n\",\n            PMEM_DIR);\n\n    /* Create PMEM partition with unlimited size */\n    err = memkind_create_pmem(PMEM_DIR, 0, &pmem_kind_unlimited);\n    if (err) {\n        perror(\"memkind_create_pmem()\");\n        fprintf(stderr, \"Unable to create pmem partition err=%d errno=%d\\n\", err,\n                errno);\n        return errno ? -errno : 1;\n    }\n\n    char *pmem_str10 = NULL;\n    char *pmem_str11 = NULL;\n\n    /* Huge allocation */\n    pmem_str10 = (char *)memkind_malloc(pmem_kind_unlimited, 32 * 1024 * 1024);\n    if (pmem_str10 == NULL) {\n        perror(\"memkind_malloc()\");\n        fprintf(stderr, \"Unable to allocate pmem string (pmem_str10)\\n\");\n        return errno ? -errno : 1;\n    }\n\n    /* Another huge allocation, kind size is only limited by OS resources */\n    pmem_str11 = (char *)memkind_malloc(pmem_kind_unlimited, 32 * 1024 * 1024);\n    if (pmem_str11 == NULL) {\n        perror(\"memkind_malloc()\");\n        fprintf(stderr, \"Unable to allocate pmem string (pmem_str11)\\n\");\n        return errno ? -errno : 1;\n    }\n\n    memkind_free(pmem_kind_unlimited, pmem_str10);\n    memkind_free(pmem_kind_unlimited, pmem_str11);\n\n    err = memkind_destroy_kind(pmem_kind_unlimited);\n    if (err) {\n        perror(\"memkind_destroy_kind()\");\n        fprintf(stderr, \"Unable to destroy pmem partition\\n\");\n        return errno ? -errno : 1;\n    }\n\n    fprintf(stdout, \"Memory was successfully allocated and released.\");\n\n    return 0;\n}\n"
  },
  {
    "path": "deps/memkind/src/examples/pmem_multithreads.c",
    "content": "/*\n * Copyright (c) 2018 Intel Corporation\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions\n * are met:\n *\n *     * Redistributions of source code must retain the above copyright\n *       notice, this list of conditions and the following disclaimer.\n *\n *     * Redistributions in binary form must reproduce the above copyright\n *       notice, this list of conditions and the following disclaimer in\n *       the documentation and/or other materials provided with the\n *       distribution.\n *\n *     * Neither the name of Intel Corporation nor the names of its\n *       contributors may be used to endorse or promote products derived\n *       from this software without specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n * \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY LOG OF THE USE\n * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind.h>\n\n#include <stdio.h>\n#include <errno.h>\n#include <stdlib.h>\n#include <pthread.h>\n#include <sys/stat.h>\n#include <unistd.h>\n\n#define PMEM_MAX_SIZE (1024 * 1024 * 32)\n#define NUM_THREADS 10\n\nstatic char *PMEM_DIR = \"/tmp/\";\n\nvoid *thread_ind(void *arg);\n\nstatic pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;\nstatic pthread_cond_t cond = PTHREAD_COND_INITIALIZER;\n\nint main(int argc, char *argv[])\n{\n    int err = 0;\n    struct stat st;\n\n    if (argc > 2) {\n        fprintf(stderr, \"Usage: %s [pmem_kind_dir_path]\", argv[0]);\n        return 1;\n    } else if (argc == 2) {\n        if (stat(argv[1], &st) != 0 || !S_ISDIR(st.st_mode)) {\n            fprintf(stderr, \"%s : Invalid path to pmem kind directory\", argv[1]);\n            return 1;\n        } else {\n            PMEM_DIR = argv[1];\n        }\n    }\n\n    fprintf(stdout,\n            \"This example shows how to use multithreading with independent pmem kinds.\"\n            \"\\nPMEM kind directory: %s\\n\",\n            PMEM_DIR);\n\n    pthread_t pmem_threads[NUM_THREADS];\n    int t;\n\n    /* Lets create many independent threads */\n    for (t = 0; t < NUM_THREADS; t++) {\n        err = pthread_create(&pmem_threads[t], NULL, thread_ind, NULL);\n        if (err) {\n            fprintf(stderr, \"Unable to create a thread\\n\");\n            return 1;\n        }\n    }\n\n    sleep(1);\n    pthread_cond_broadcast(&cond);\n\n    for (t = 0; t < NUM_THREADS; t++) {\n        err = pthread_join(pmem_threads[t], NULL);\n        if (err) {\n            fprintf(stderr, \"Thread join failed\\n\");\n            return 1;\n        }\n    }\n\n    fprintf(stdout, \"Threads successfully allocated memory in the PMEM kinds.\");\n\n    return 0;\n}\n\nvoid *thread_ind(void *arg)\n{\n    struct memkind *pmem_kind;\n\n    pthread_mutex_lock(&mutex);\n    pthread_cond_wait(&cond, &mutex);\n    pthread_mutex_unlock(&mutex);\n\n    /* Create a pmem kind in thread */\n    int err = memkind_create_pmem(PMEM_DIR, PMEM_MAX_SIZE, &pmem_kind);\n    if (err) {\n        perror(\"thread memkind_create_pmem()\");\n        fprintf(stderr, \"Unable to create pmem partition err=%d errno=%d\\n\", err,\n                errno);\n        return NULL;\n    }\n\n    /* Alloc something */\n    void *test = memkind_malloc(pmem_kind, 32);\n    if (test == NULL) {\n        perror(\"thread memkind_malloc()\");\n        fprintf(stderr, \"Unable to allocate pmem (test)\\n\");\n        return NULL;\n    }\n\n    /* Free resources */\n    memkind_free(pmem_kind, test);\n\n    /* And destroy pmem kind */\n    err = memkind_destroy_kind(pmem_kind);\n    if (err) {\n        perror(\"thread memkind_pmem_destroy()\");\n        fprintf(stderr, \"Unable to destroy pmem partition\\n\");\n    }\n\n    return NULL;\n}\n"
  },
  {
    "path": "deps/memkind/src/examples/pmem_multithreads_onekind.c",
    "content": "/*\n * Copyright (c) 2018 Intel Corporation\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions\n * are met:\n *\n *     * Redistributions of source code must retain the above copyright\n *       notice, this list of conditions and the following disclaimer.\n *\n *     * Redistributions in binary form must reproduce the above copyright\n *       notice, this list of conditions and the following disclaimer in\n *       the documentation and/or other materials provided with the\n *       distribution.\n *\n *     * Neither the name of Intel Corporation nor the names of its\n *       contributors may be used to endorse or promote products derived\n *       from this software without specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n * \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY LOG OF THE USE\n * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind.h>\n\n#include <stdio.h>\n#include <errno.h>\n#include <stdlib.h>\n#include <pthread.h>\n#include <sys/stat.h>\n#include <unistd.h>\n\n#define NUM_THREADS 10\n\nstatic char *PMEM_DIR = \"/tmp/\";\n\nstruct arg_struct {\n    int id;\n    struct memkind *kind;\n    int **ptr;\n};\n\nvoid *thread_onekind(void *arg);\n\nstatic pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;\nstatic pthread_cond_t cond = PTHREAD_COND_INITIALIZER;\n\nint main(int argc, char *argv[])\n{\n    struct memkind *pmem_kind_unlimited = NULL;\n    int err = 0;\n    struct stat st;\n\n    if (argc > 2) {\n        fprintf(stderr, \"Usage: %s [pmem_kind_dir_path]\", argv[0]);\n        return 1;\n    } else if (argc == 2) {\n        if (stat(argv[1], &st) != 0 || !S_ISDIR(st.st_mode)) {\n            fprintf(stderr, \"%s : Invalid path to pmem kind directory\", argv[1]);\n            return 1;\n        } else {\n            PMEM_DIR = argv[1];\n        }\n    }\n\n    fprintf(stdout,\n            \"This example shows how to use multithreading with one main pmem kind.\"\n            \"\\nPMEM kind directory: %s\\n\",\n            PMEM_DIR);\n\n    /* Create PMEM partition with unlimited size */\n    err = memkind_create_pmem(PMEM_DIR, 0, &pmem_kind_unlimited);\n    if (err) {\n        perror(\"memkind_create_pmem()\");\n        fprintf(stderr, \"Unable to create pmem partition err=%d errno=%d\\n\", err,\n                errno);\n        return errno ? -errno : 1;\n    }\n\n    /* Create a few threads which will access to our main pmem_kind */\n    pthread_t pmem_threads[NUM_THREADS];\n    int *pmem_tint[NUM_THREADS][100];\n    int t = 0, i = 0;\n\n    struct arg_struct *args[NUM_THREADS];\n\n    for (t = 0; t<NUM_THREADS; t++) {\n        args[t] = malloc(sizeof(struct arg_struct));\n        args[t]->id = t;\n        args[t]->ptr = &pmem_tint[t][0];\n        args[t]->kind = pmem_kind_unlimited;\n\n        err = pthread_create(&pmem_threads[t], NULL, thread_onekind, (void *)args[t]);\n        if (err) {\n            fprintf(stderr, \"Unable to create a thread\\n\");\n            return 1;\n        }\n    }\n\n    sleep(1);\n    pthread_cond_broadcast(&cond);\n\n    for (t = 0; t < NUM_THREADS; t++) {\n        err = pthread_join(pmem_threads[t], NULL);\n        if (err) {\n            fprintf(stderr, \"Thread join failed\\n\");\n            return 1;\n        }\n    }\n\n    /* Check if we can read the values outside of threads and free resources */\n    for (t = 0; t < NUM_THREADS; t++) {\n        for (i = 0; i < 100; i++) {\n            if(*pmem_tint[t][i] != t) {\n                perror(\"read thread memkind_malloc()\");\n                fprintf(stderr, \"pmem_tint value has not been saved correctly in the thread\\n\");\n                return 1;\n            }\n            memkind_free(args[t]->kind, *(args[t]->ptr+i));\n        }\n        free(args[t]);\n    }\n\n    fprintf(stdout, \"Threads successfully allocated memory in the PMEM kind.\");\n\n    return 0;\n}\n\nvoid *thread_onekind(void *arg)\n{\n    struct arg_struct *args = (struct arg_struct *)arg;\n    int i;\n\n    pthread_mutex_lock(&mutex);\n    pthread_cond_wait(&cond, &mutex);\n    pthread_mutex_unlock(&mutex);\n\n    /* Lets alloc int and put there thread ID */\n    for (i = 0; i < 100; i++) {\n        *(args->ptr+i) = (int *)memkind_malloc(args->kind, sizeof(int));\n        if (*(args->ptr+i) == NULL) {\n            perror(\"thread memkind_malloc()\");\n            fprintf(stderr, \"Unable to allocate pmem int\\n\");\n            return NULL;\n        }\n        **(args->ptr+i) = args->id;\n    }\n\n    return NULL;\n}\n"
  },
  {
    "path": "deps/memkind/src/examples/pmem_usable_size.c",
    "content": "/*\n * Copyright (c) 2018 Intel Corporation\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions\n * are met:\n *\n *     * Redistributions of source code must retain the above copyright\n *       notice, this list of conditions and the following disclaimer.\n *\n *     * Redistributions in binary form must reproduce the above copyright\n *       notice, this list of conditions and the following disclaimer in\n *       the documentation and/or other materials provided with the\n *       distribution.\n *\n *     * Neither the name of Intel Corporation nor the names of its\n *       contributors may be used to endorse or promote products derived\n *       from this software without specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n * \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY LOG OF THE USE\n * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind.h>\n\n#include <stdio.h>\n#include <errno.h>\n#include <sys/stat.h>\n\nstatic char *PMEM_DIR = \"/tmp/\";\n\nint main(int argc, char *argv[])\n{\n    struct memkind *pmem_kind_unlimited = NULL;\n    int err = 0;\n    struct stat st;\n\n    if (argc > 2) {\n        fprintf(stderr, \"Usage: %s [pmem_kind_dir_path]\", argv[0]);\n        return 1;\n    } else if (argc == 2) {\n        if (stat(argv[1], &st) != 0 || !S_ISDIR(st.st_mode)) {\n            fprintf(stderr, \"%s : Invalid path to pmem kind directory\", argv[1]);\n            return 1;\n        } else {\n            PMEM_DIR = argv[1];\n        }\n    }\n\n    fprintf(stdout,\n            \"This example shows difference between the expected and the actual allocation size.\"\n            \"\\nPMEM kind directory: %s\\n\",\n            PMEM_DIR);\n\n    /* Create PMEM partition with unlimited size */\n    err = memkind_create_pmem(PMEM_DIR, 0, &pmem_kind_unlimited);\n    if (err) {\n        perror(\"memkind_create_pmem()\");\n        fprintf(stderr, \"Unable to create pmem partition err=%d errno=%d\\n\", err,\n                errno);\n        return errno ? -errno : 1;\n    }\n\n    char *pmem_str10 = NULL;\n    char *pmem_str11 = NULL;\n    char *pmem_str12 = NULL;\n\n    /* 32 bytes allocation */\n    pmem_str10 = (char *)memkind_malloc(pmem_kind_unlimited, 32);\n    if (pmem_str10 == NULL) {\n        perror(\"memkind_malloc()\");\n        fprintf(stderr, \"Unable to allocate pmem string (pmem_str10)\\n\");\n        return errno ? -errno : 1;\n    }\n\n    /* Check real usable size for this allocation */\n    if (memkind_malloc_usable_size(pmem_kind_unlimited, pmem_str10) != 32) {\n        perror(\"memkind_default_malloc_usable_size()\");\n        fprintf(stderr, \"Wrong usable size\\n\");\n        return 1;\n    }\n\n    /* 31 bytes allocation */\n    pmem_str11 = (char *)memkind_malloc(pmem_kind_unlimited, 31);\n    if (pmem_str11 == NULL) {\n        perror(\"memkind_malloc()\");\n        fprintf(stderr, \"Unable to allocate pmem string (pmem_str11)\\n\");\n        return errno ? -errno : 1;\n    }\n\n    /* Check real usable size for this allocation, its 32 again */\n    if (memkind_malloc_usable_size(pmem_kind_unlimited, pmem_str11) != 32) {\n        perror(\"memkind_default_malloc_usable_size()\");\n        fprintf(stderr, \"Wrong usable size\\n\");\n        return 1;\n    }\n\n    /* 33 bytes allocation */\n    pmem_str12 = (char *)memkind_malloc(pmem_kind_unlimited, 33);\n    if (pmem_str11 == NULL) {\n        perror(\"memkind_malloc()\");\n        fprintf(stderr, \"Unable to allocate pmem string (pmem_str12)\\n\");\n        return errno ? -errno : 1;\n    }\n\n    /* Check real usable size for this allocation, its 48 now */\n    if (memkind_malloc_usable_size(pmem_kind_unlimited, pmem_str12) != 48) {\n        perror(\"memkind_default_malloc_usable_size()\");\n        fprintf(stderr, \"Wrong usable size\\n\");\n        return 1;\n    }\n\n    memkind_free(pmem_kind_unlimited, pmem_str10);\n    memkind_free(pmem_kind_unlimited, pmem_str11);\n    memkind_free(pmem_kind_unlimited, pmem_str12);\n\n    /* 5MB allocation */\n    pmem_str10 = (char *)memkind_malloc(pmem_kind_unlimited, 5 * 1024 * 1024);\n    if (pmem_str10 == NULL) {\n        perror(\"memkind_malloc()\");\n        fprintf(stderr, \"Unable to allocate pmem string (pmem_str10)\\n\");\n        return errno ? -errno : 1;\n    }\n\n    /* Check real usable size for this allocation */\n    if (memkind_malloc_usable_size(pmem_kind_unlimited,\n                                   pmem_str10) !=  5 * 1024 * 1024) {\n        perror(\"memkind_default_malloc_usable_size()\");\n        fprintf(stderr, \"Wrong usable size\\n\");\n        return 1;\n    }\n\n    /* 5MB + 1B allocation */\n    pmem_str11 = (char *)memkind_malloc(pmem_kind_unlimited,  5 * 1024 * 1024 + 1);\n    if (pmem_str11 == NULL) {\n        perror(\"memkind_malloc()\");\n        fprintf(stderr, \"Unable to allocate pmem string (pmem_str11)\\n\");\n        return errno ? -errno : 1;\n    }\n\n    /* Check real usable size for this allocation, its 6MB now */\n    if (memkind_malloc_usable_size(pmem_kind_unlimited,\n                                   pmem_str11) !=  6 * 1024 * 1024) {\n        perror(\"memkind_default_malloc_usable_size()\");\n        fprintf(stderr, \"Wrong usable size\\n\");\n        return 1;\n    }\n\n    memkind_free(pmem_kind_unlimited, pmem_str10);\n    memkind_free(pmem_kind_unlimited, pmem_str11);\n\n    err = memkind_destroy_kind(pmem_kind_unlimited);\n    if (err) {\n        perror(\"memkind_destroy_kind()\");\n        fprintf(stderr, \"Unable to destroy pmem partition\\n\");\n        return errno ? -errno : 1;\n    }\n\n    fprintf(stdout,\n            \"The real size of the allocation has been successfully read.\");\n\n    return 0;\n}\n"
  },
  {
    "path": "deps/memkind/src/include/hbw_allocator.h",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#pragma once\n\n#include <hbwmalloc.h>\n\n#include <stddef.h>\n#include <new>\n/*\n * Header file for the C++ allocator compatible with the C++ standard library allocator concepts.\n * More details in hbwallocator(3) man page.\n * Note: memory heap management is based on hbwmalloc, refer to the hbwmalloc man page for more information.\n *\n * Functionality defined in this header is considered as EXPERIMENTAL API.\n * API standards are described in memkind(3) man page.\n */\nnamespace hbw\n{\n\n    template <class T>\n    class allocator\n    {\n    public:\n        /*\n         *  Public member types required and defined by the standard library allocator concepts.\n         */\n        typedef size_t size_type;\n        typedef ptrdiff_t difference_type;\n        typedef T *pointer;\n        typedef const T *const_pointer;\n        typedef T &reference;\n        typedef const T &const_reference;\n        typedef T value_type;\n\n        template <class U>\n        struct rebind {\n            typedef hbw::allocator<U> other;\n        };\n\n        /*\n         *  Public member functions required and defined by the standard library allocator concepts.\n         */\n        allocator() throw() { }\n\n        template <class U>\n        allocator(const allocator<U> &) throw() { }\n\n        ~allocator() throw() { }\n\n        pointer address(reference x) const\n        {\n            return &x;\n        }\n\n        const_pointer address(const_reference x) const\n        {\n            return &x;\n        }\n\n        /*\n         *  Allocates n*sizeof(T) bytes of high bandwidth memory using hbw_malloc().\n         *  Throws std::bad_alloc when cannot allocate memory.\n         */\n        pointer allocate(size_type n, const void * = 0)\n        {\n            if (n > this->max_size()) {\n                throw std::bad_alloc();\n            }\n            pointer result = static_cast<pointer>(hbw_malloc(n * sizeof(T)));\n            if (!result) {\n                throw std::bad_alloc();\n            }\n            return result;\n        }\n\n        /*\n         *  Deallocates memory associated with pointer returned by allocate() using hbw_free().\n         */\n        void deallocate(pointer p, size_type n)\n        {\n            hbw_free(static_cast<void *>(p));\n        }\n\n        size_type max_size() const throw()\n        {\n            return size_t(-1) / sizeof(T);\n        }\n\n        void construct(pointer p, const_reference val)\n        {\n            ::new(p) value_type(val);\n        }\n\n        void destroy(pointer p)\n        {\n            p->~T();\n        }\n    };\n\n    template <class T, class U>\n    bool operator==(const allocator<T> &, const allocator<U> &)\n    {\n        return true;\n    }\n\n    template <class T, class U>\n    bool operator!=(const allocator<T> &, const allocator<U> &)\n    {\n        return false;\n    }\n\n}\n"
  },
  {
    "path": "deps/memkind/src/include/hbwmalloc.h",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#pragma once\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#include <stdlib.h>\n/*\n *  Header file for the high bandwidth memory interface.\n *\n *  This file defines the external API's and enumerations for the\n *  hbwmalloc library.  These interfaces define a heap manager that\n *  targets the high bandwidth memory numa nodes.\n *\n *  hbwmalloc.h functionality is considered as stable API (STANDARD API).\n *\n *  Please read hbwmalloc(3) man page for or more details.\n */\n\n/*\n *  Fallback policy.\n *\n *  Policy that determines behavior when there is not enough free high\n *  bandwidth memory to satisfy a user request.  This enum is used with\n *  hbw_get_policy() and hbw_set_policy().\n */\ntypedef enum {\n    /*\n     *  If insufficient high bandwidth memory pages on nearest NUMA node are\n     *  available then OOM killer will be triggered.\n     */\n    HBW_POLICY_BIND = 1,\n    /*\n     *  If insufficient high bandwidth memory pages are available fall\n     *  back on standard memory pages.\n     */\n    HBW_POLICY_PREFERRED = 2,\n    /*\n     *  Interleave pages across high bandwidth nodes. If insufficient memory\n     *  pages are available then OOM killer will be triggered.\n     */\n    HBW_POLICY_INTERLEAVE = 3,\n    /*\n     *  If insufficient high bandwidth memory pages are available then\n     *  OOM killer will be triggered.\n     */\n    HBW_POLICY_BIND_ALL = 4,\n} hbw_policy_t;\n\n/*\n *  Page size selection.\n *\n *  The hbw_posix_memalign_psize() API gives the user the option to\n *  select the page size from this enumerated list.\n */\ntypedef enum {\n\n    /*\n     * The four kilobyte page size option. Note that with transparent huge\n     * pages enabled these allocations may be promoted by the operating system\n     * to two megabyte pages.\n     */\n    HBW_PAGESIZE_4KB           = 1,\n\n    /*\n     * The two megabyte page size option.\n     */\n    HBW_PAGESIZE_2MB           = 2,\n    /*\n     * This option is deprecated.\n     * Allocate high bandwidth memory using 1GB chunks backed by huge pages.\n     */\n    HBW_PAGESIZE_1GB_STRICT    = 3,\n\n    /*\n     * This option is deprecated.\n     * Allocate high bandwidth memory using 1GB chunks backed by huge pages.\n     */\n    HBW_PAGESIZE_1GB           = 4,\n\n    /*\n     * Helper representing value of the last enum element incremented by 1.\n     * Shall not be treated as a valid value for functions taking hbw_pagesize_t\n     * as parameter.\n     */\n    HBW_PAGESIZE_MAX_VALUE\n} hbw_pagesize_t;\n\n/*\n * Flags for hbw_verify_ptr function\n */\nenum {\n\n    /*\n     * This option touches first byte of all pages in address range starting from \"addr\" to \"addr\" + \"size\"\n     * by read and write (so the content will be overwitten by the same data as it was read).\n     */\n    HBW_TOUCH_PAGES      = (1 << 0)\n};\n\n/*\n * Returns the current fallback policy when insufficient high bandwidth memory\n * is available.\n */\nhbw_policy_t hbw_get_policy(void);\n\n/*\n * Sets the current fallback policy. The policy can be modified only once in\n * the lifetime of an application and before calling hbw_*alloc() or\n * hbw_posix_memalign*() function.\n * Note: If the policy is not set, than HBW_POLICY_PREFERRED will be used by\n * default.\n *\n * Returns:\n *   0: on success\n *   EPERM: if hbw_set_policy () was called more than once\n *   EINVAL: if mode argument was neither HBW_POLICY_PREFERRED, HBW_POLICY_BIND, HBW_POLICY_BIND_ALL nor HBW_POLICY_INTERLEAVE\n */\nint hbw_set_policy(hbw_policy_t mode);\n\n/*\n * Verifies high bandwidth memory availability.\n * Returns:\n *   0: if high bandwidth memory is available\n *   ENODEV: if high-bandwidth memory is unavailable.\n */\nint hbw_check_available(void);\n\n/*\n * Verifies if allocated memory fully fall into high bandwidth memory.\n * Returns:\n *   0: if memory in address range from \"addr\" to \"addr\" + \"size\" is allocated in high bandwidth memory\n *   -1: if any region of memory was not allocated in high bandwidth memory\n *   EINVAL: if addr is NULL, size equals 0 or flags contained unsupported bit set\n *   EFAULT: could not verify memory\n */\nint hbw_verify_memory_region(void *addr, size_t size, int flags);\n\n/*\n * Allocates size bytes of uninitialized high bandwidth memory.\n * The allocated space is  suitably  aligned (after  possible  pointer\n * coercion) for storage of any type of object. If size is zero then\n * hbw_malloc() returns NULL.\n */\nvoid *hbw_malloc(size_t size);\n\n/*\n * Allocates space for num objects in high bandwidth memory, each size bytes\n * in length.\n * The result is identical to calling hbw_malloc() with an argument of\n * num*size, with the exception that the allocated memory is explicitly\n * initialized to zero bytes.\n * If num or size is 0, then hbw_calloc() returns NULL.\n */\nvoid *hbw_calloc(size_t num, size_t size);\n\n/*\n * Allocates size bytes of high bandwidth memory such that the allocation's\n * base address is an even multiple of alignment, and returns the allocation\n * in the value pointed to by memptr.  The requested alignment must be a power\n * of 2 at least as large as sizeof(void *).\n * Returns:\n *   0: on success\n *   ENOMEM: if there was insufficient memory to satisfy the request\n *   EINVAL: if the alignment parameter was not a power of two, or was less than sizeof(void *)\n */\nint hbw_posix_memalign(void **memptr, size_t alignment, size_t size);\n\n/*\n * Allocates size bytes of high bandwidth memory such that the allocation's\n * base address is an even multiple of alignment, and returns the allocation\n * in the value pointed to by memptr. The requested alignment must be a power\n * of 2 at least as large as sizeof(void  *). The memory will be allocated\n * using pages determined by the pagesize variable.\n * Returns:\n *   0: on success\n *   ENOMEM: if there was insufficient memory to satisfy the request\n *   EINVAL: if the alignment parameter was not a power of two, or was less than sizeof(void *)\n */\nint hbw_posix_memalign_psize(void **memptr, size_t alignment, size_t size,\n                             hbw_pagesize_t pagesize);\n\n/*\n * Changes the size of the previously allocated memory referenced by ptr to\n * size bytes of the specified kind. The contents of the memory are unchanged\n * up to the lesser of the new and old size.\n * If the new size is larger, the contents of the newly allocated portion\n * of the memory are undefined.\n * Upon success, the memory referenced by ptr is freed and a pointer to the\n * newly allocated high bandwidth memory is returned.\n * Note: memkind_realloc() may move the memory allocation, resulting in a\n * different return value than ptr.\n * If ptr is NULL, the hbw_realloc() function behaves identically to\n * hbw_malloc() for the specified size. The address ptr, if not NULL,\n * was returned by a previous call to hbw_malloc(), hbw_calloc(),\n * hbw_realloc(), or hbw_posix_memalign(). Otherwise, or if hbw_free(ptr)\n * was called before, undefined behavior occurs.\n * Note: hbw_realloc() cannot be used with a pointer returned by\n * hbw_posix_memalign_psize().\n */\nvoid *hbw_realloc(void *ptr, size_t size);\n\n/*\n * Causes the allocated memory referenced by ptr to be made\n * available for future allocations. If ptr is NULL, no action occurs.\n * The address ptr, if not NULL, must have been returned by a previous call\n * to hbw_malloc(), hbw_calloc(), hbw_realloc(), hbw_posix_memalign(), or\n * hbw_posix_memalign_psize(). Otherwise, if hbw_free(ptr) was called before,\n * undefined behavior occurs.\n */\nvoid hbw_free(void *ptr);\n\n#ifdef __cplusplus\n}\n#endif\n"
  },
  {
    "path": "deps/memkind/src/include/memkind/internal/heap_manager.h",
    "content": "/*\n * Copyright (C) 2017 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#pragma once\n\n#include <memkind.h>\n\nvoid heap_manager_init(struct memkind *kind);\nvoid heap_manager_free(struct memkind *kind, void *ptr);\n"
  },
  {
    "path": "deps/memkind/src/include/memkind/internal/memkind_arena.h",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#pragma once\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#ifndef MEMKIND_INTERNAL_API\n#warning \"DO NOT INCLUDE THIS FILE! IT IS INTERNAL MEMKIND API AND SOON WILL BE REMOVED FROM BIN & DEVEL PACKAGES\"\n#endif\n\n#include <memkind.h>\n#include <jemalloc/jemalloc.h>\n#include <memkind/internal/memkind_private.h>\n\n/*\n * Header file for the jemalloc arena allocation memkind operations.\n * More details in memkind_arena(3) man page.\n *\n * Functionality defined in this header is considered as EXPERIMENTAL API.\n * API standards are described in memkind(3) man page.\n */\n\nstruct memkind *get_kind_by_arena(unsigned arena_ind);\n\nint memkind_arena_create(struct memkind *kind, struct memkind_ops *ops,\n                         const char *name);\nint memkind_arena_create_map(struct memkind *kind, extent_hooks_t *hooks);\nint memkind_arena_destroy(struct memkind *kind);\nvoid *memkind_arena_malloc(struct memkind *kind, size_t size);\nvoid *memkind_arena_calloc(struct memkind *kind, size_t num, size_t size);\nvoid *memkind_arena_pmem_calloc(struct memkind *kind, size_t num, size_t size);\nint memkind_arena_posix_memalign(struct memkind *kind, void **memptr,\n                                 size_t alignment, size_t size);\nvoid *memkind_arena_realloc(struct memkind *kind, void *ptr, size_t size);\nint memkind_bijective_get_arena(struct memkind *kind, unsigned int *arena,\n                                size_t size);\nint memkind_thread_get_arena(struct memkind *kind, unsigned int *arena,\n                             size_t size);\nint memkind_arena_finalize(struct memkind *kind);\nvoid memkind_arena_init(struct memkind *kind);\nvoid memkind_arena_free(struct memkind *kind, void *ptr);\n\n#ifdef __cplusplus\n}\n#endif\n"
  },
  {
    "path": "deps/memkind/src/include/memkind/internal/memkind_default.h",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#pragma once\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#ifndef MEMKIND_INTERNAL_API\n#warning \"DO NOT INCLUDE THIS FILE! IT IS INTERNAL MEMKIND API AND SOON WILL BE REMOVED FROM BIN & DEVEL PACKAGES\"\n#endif\n\n#include <memkind.h>\n#include <memkind/internal/memkind_private.h>\n\n/*\n * Header file for the default implementations for memkind operations.\n * More details in memkind_default(3) man page.\n *\n * Functionality defined in this header is considered as EXPERIMENTAL API.\n * API standards are described in memkind(3) man page.\n */\n\nint memkind_default_create(struct memkind *kind, struct memkind_ops *ops,\n                           const char *name);\nint memkind_default_destroy(struct memkind *kind);\nvoid *memkind_default_malloc(struct memkind *kind, size_t size);\nvoid *memkind_default_calloc(struct memkind *kind, size_t num, size_t size);\nint memkind_default_posix_memalign(struct memkind *kind, void **memptr,\n                                   size_t alignment, size_t size);\nvoid *memkind_default_realloc(struct memkind *kind, void *ptr, size_t size);\nvoid memkind_default_free(struct memkind *kind, void *ptr);\nvoid *memkind_default_mmap(struct memkind *kind, void *addr, size_t size);\nint memkind_default_mbind(struct memkind *kind, void *ptr, size_t size);\nint memkind_default_get_mmap_flags(struct memkind *kind, int *flags);\nint memkind_default_get_mbind_mode(struct memkind *kind, int *mode);\nint memkind_default_get_mbind_nodemask(struct memkind *kind,\n                                       unsigned long *nodemask, unsigned long maxnode);\nint memkind_preferred_get_mbind_mode(struct memkind *kind, int *mode);\nint memkind_interleave_get_mbind_mode(struct memkind *kind, int *mode);\nint memkind_nohugepage_madvise(struct memkind *kind, void *addr, size_t size);\nint memkind_posix_check_alignment(struct memkind *kind, size_t alignment);\nvoid memkind_default_init_once(void);\nsize_t memkind_default_malloc_usable_size(struct memkind *kind, void *ptr);\n\nstatic inline bool size_out_of_bounds(size_t size)\n{\n    return !size;\n}\n\nextern struct memkind_ops MEMKIND_DEFAULT_OPS;\n\n#ifdef __cplusplus\n}\n#endif\n"
  },
  {
    "path": "deps/memkind/src/include/memkind/internal/memkind_gbtlb.h",
    "content": "/*\n * Copyright (C) 2014 - 2016 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#pragma once\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#ifndef MEMKIND_INTERNAL_API\n#warning \"DO NOT INCLUDE THIS FILE! IT IS INTERNAL MEMKIND API AND SOON WILL BE REMOVED FROM BIN & DEVEL PACKAGES\"\n#endif\n\n#include <memkind.h>\n\n/*\n * Header file for the gigabyte TLB memkind operations.\n *\n * All function declarations has been moved to memkind_deprecated.h\n * because of end of GBTLB support.\n *\n * API standards are described in memkind(3) man page.\n */\n\n#ifdef __cplusplus\n}\n#endif\n"
  },
  {
    "path": "deps/memkind/src/include/memkind/internal/memkind_hbw.h",
    "content": "/*\n * Copyright (C) 2014 - 2017 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#pragma once\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#ifndef MEMKIND_INTERNAL_API\n#warning \"DO NOT INCLUDE THIS FILE! IT IS INTERNAL MEMKIND API AND SOON WILL BE REMOVED FROM BIN & DEVEL PACKAGES\"\n#endif\n\n#include <memkind.h>\n\n/*\n * Header file for the high bandwidth memory memkind operations.\n * More details in memkind_hbw(3) man page.\n *\n * Functionality defined in this header is considered as EXPERIMENTAL API.\n * API standards are described in memkind(3) man page.\n */\n\nint memkind_hbw_check_available(struct memkind *kind);\nint memkind_hbw_hugetlb_check_available(struct memkind *kind);\nint memkind_hbw_get_mbind_nodemask(struct memkind *kind,\n                                   unsigned long *nodemask,\n                                   unsigned long maxnode);\nint memkind_hbw_all_get_mbind_nodemask(struct memkind *kind,\n                                       unsigned long *nodemask,\n                                       unsigned long maxnode);\nvoid memkind_hbw_init_once(void);\nvoid memkind_hbw_all_init_once(void);\nvoid memkind_hbw_hugetlb_init_once(void);\nvoid memkind_hbw_all_hugetlb_init_once(void);\nvoid memkind_hbw_preferred_init_once(void);\nvoid memkind_hbw_preferred_hugetlb_init_once(void);\nvoid memkind_hbw_interleave_init_once(void);\n\nextern struct memkind_ops MEMKIND_HBW_OPS;\nextern struct memkind_ops MEMKIND_HBW_ALL_OPS;\nextern struct memkind_ops MEMKIND_HBW_HUGETLB_OPS;\nextern struct memkind_ops MEMKIND_HBW_ALL_HUGETLB_OPS;\nextern struct memkind_ops MEMKIND_HBW_PREFERRED_OPS;\nextern struct memkind_ops MEMKIND_HBW_PREFERRED_HUGETLB_OPS;\nextern struct memkind_ops MEMKIND_HBW_INTERLEAVE_OPS;\n\n#ifdef __cplusplus\n}\n#endif\n"
  },
  {
    "path": "deps/memkind/src/include/memkind/internal/memkind_hugetlb.h",
    "content": "/*\n * Copyright (C) 2014 - 2017 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#pragma once\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#ifndef MEMKIND_INTERNAL_API\n#warning \"DO NOT INCLUDE THIS FILE! IT IS INTERNAL MEMKIND API AND SOON WILL BE REMOVED FROM BIN & DEVEL PACKAGES\"\n#endif\n\n#include <memkind.h>\n\n/*\n * Header file for the hugetlb memory memkind operations.\n * More details in memkind_hugetlb(3) man page.\n *\n * Functionality defined in this header is considered as EXPERIMENTAL API.\n * API standards are described in memkind(3) man page.\n */\n\nint memkind_hugetlb_get_mmap_flags(struct memkind *kind, int *flags);\nvoid memkind_hugetlb_init_once(void);\nint memkind_hugetlb_check_available_2mb(struct memkind *kind);\n\nextern struct memkind_ops MEMKIND_HUGETLB_OPS;\n\n#ifdef __cplusplus\n}\n#endif\n"
  },
  {
    "path": "deps/memkind/src/include/memkind/internal/memkind_interleave.h",
    "content": "/*\n * Copyright (C) 2015 - 2017 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#pragma once\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#ifndef MEMKIND_INTERNAL_API\n#warning \"DO NOT INCLUDE THIS FILE! IT IS INTERNAL MEMKIND API AND SOON WILL BE REMOVED FROM BIN & DEVEL PACKAGES\"\n#endif\n\n/*\n * Header file for the interleave memory memkind operations.\n *\n * Functionality defined in this header is considered as EXPERIMENTAL API.\n * API standards are described in memkind(3) man page.\n */\n\nvoid memkind_interleave_init_once(void);\n\nextern struct memkind_ops MEMKIND_INTERLEAVE_OPS;\n\n#ifdef __cplusplus\n}\n#endif\n"
  },
  {
    "path": "deps/memkind/src/include/memkind/internal/memkind_log.h",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#pragma once\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#define PRINTF_FORMAT __attribute__ ((format (printf, 1, 2)))\n\n/*\n * For printing informational messages\n * Requires environment variable MEMKIND_DEBUG to be set to appropriate value\n */\nvoid log_info(const char *format, ...) PRINTF_FORMAT;\n\n/*\n * For printing messages regarding errors and failures\n * Requires environment variable MEMKIND_DEBUG to be set to appropriate value\n */\nvoid log_err(const char *format, ...) PRINTF_FORMAT;\n\n/*\n * For printing messages regarding fatal errors before calling abort()\n * Works *no matter* of MEMKIND_DEBUG state\n */\nvoid log_fatal(const char *format, ...)PRINTF_FORMAT;\n\n#ifdef __cplusplus\n}\n#endif\n"
  },
  {
    "path": "deps/memkind/src/include/memkind/internal/memkind_pmem.h",
    "content": "/*\n * Copyright (C) 2015 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#pragma once\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#ifndef MEMKIND_INTERNAL_API\n#warning \"DO NOT INCLUDE THIS FILE! IT IS INTERNAL MEMKIND API AND SOON WILL BE REMOVED FROM BIN & DEVEL PACKAGES\"\n#endif\n\n#include <memkind.h>\n#include \"memkind_default.h\"\n#include \"memkind_arena.h\"\n\n#include <pthread.h>\n\n/*\n * Header file for the file-backed memory memkind operations.\n * More details in memkind_pmem(3) man page.\n *\n * Functionality defined in this header is considered as EXPERIMENTAL API.\n * API standards are described in memkind(3) man page.\n */\n\n#define MEMKIND_PMEM_CHUNK_SIZE (1ull << 21ull) // 2MB\n\nint memkind_pmem_create(struct memkind *kind, struct memkind_ops *ops,\n                        const char *name);\nint memkind_pmem_destroy(struct memkind *kind);\nvoid *memkind_pmem_mmap(struct memkind *kind, void *addr, size_t size);\nint memkind_pmem_get_mmap_flags(struct memkind *kind, int *flags);\n\nstruct memkind_pmem_extent {\n    void *addrBase;\n    size_t cb;\n    off_t offset;\n};\nstruct memkind_pmem {\n    int fd;\n    off_t offset;\n    size_t max_size;\n    pthread_mutex_t pmem_lock;\n    int cextents;\n    int cextentsAlloc;\n    struct memkind_pmem_extent *rgextents;\n};\n\nextern struct memkind_ops MEMKIND_PMEM_OPS;\n\n#ifdef __cplusplus\n}\n#endif\n"
  },
  {
    "path": "deps/memkind/src/include/memkind/internal/memkind_private.h",
    "content": "/*\n * Copyright (C) 2016 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#pragma once\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#ifndef MEMKIND_INTERNAL_API\n#warning \"DO NOT INCLUDE THIS FILE! IT IS INTERNAL MEMKIND API AND SOON WILL BE REMOVED FROM BIN & DEVEL PACKAGES\"\n#endif\n\n#include \"memkind.h\"\n\n#include <stdbool.h>\n#include <pthread.h>\n\n#ifdef __GNUC__\n#   define MEMKIND_LIKELY(x)       __builtin_expect(!!(x), 1)\n#   define MEMKIND_UNLIKELY(x)     __builtin_expect(!!(x), 0)\n#else\n#   define MEMKIND_LIKELY(x)       (x)\n#   define MEMKIND_UNLIKELY(x)     (x)\n#endif\n\n#ifndef MEMKIND_EXPORT\n#   define MEMKIND_EXPORT __attribute__((visibility(\"default\")))\n#endif\n\n#ifndef JE_PREFIX\n#error \"Can't find JE_PREFIX define. Define one or use build.sh script.\"\n#endif\n\n// This ladder call is required due to meanders of C's preprocessor logic.\n// Without it, JE_PREFIX would be used directly (i.e. 'JE_PREFIX') and not\n// substituted with defined value.\n#define JE_SYMBOL2(a, b) a ## b\n#define JE_SYMBOL1(a, b) JE_SYMBOL2(a, b)\n#define JE_SYMBOL(b)     JE_SYMBOL1(JE_PREFIX, b)\n\n// Redefine symbols\n#define jemk_malloc                 JE_SYMBOL(malloc)\n#define jemk_mallocx                JE_SYMBOL(mallocx)\n#define jemk_calloc                 JE_SYMBOL(calloc)\n#define jemk_rallocx                JE_SYMBOL(rallocx)\n#define jemk_realloc                JE_SYMBOL(realloc)\n#define jemk_mallctl                JE_SYMBOL(mallctl)\n#define jemk_memalign               JE_SYMBOL(memalign)\n#define jemk_posix_memalign         JE_SYMBOL(posix_memalign)\n#define jemk_free                   JE_SYMBOL(free)\n#define jemk_dallocx                JE_SYMBOL(dallocx)\n#define jemk_malloc_usable_size     JE_SYMBOL(malloc_usable_size)\n\n/// \\note EXPERIMENTAL API\nint je_get_defrag_hint(void *ptr, int *bin_util, int *run_util);\n\nenum memkind_const_private {\n    MEMKIND_NAME_LENGTH_PRIV = 64\n};\n\nstruct memkind_ops {\n    int (* create)(struct memkind *kind, struct memkind_ops *ops, const char *name);\n    int (* destroy)(struct memkind *kind);\n    void *(* malloc)(struct memkind *kind, size_t size);\n    void *(* calloc)(struct memkind *kind, size_t num, size_t size);\n    int (* posix_memalign)(struct memkind *kind, void **memptr, size_t alignment,\n                           size_t size);\n    void *(* realloc)(struct memkind *kind, void *ptr, size_t size);\n    void (* free)(struct memkind *kind, void *ptr);\n    void *(* mmap)(struct memkind *kind, void *addr, size_t size);\n    int (* mbind)(struct memkind *kind, void *ptr, size_t size);\n    int (* madvise)(struct memkind *kind, void *addr, size_t size);\n    int (* get_mmap_flags)(struct memkind *kind, int *flags);\n    int (* get_mbind_mode)(struct memkind *kind, int *mode);\n    int (* get_mbind_nodemask)(struct memkind *kind, unsigned long *nodemask,\n                               unsigned long maxnode);\n    int (* get_arena)(struct memkind *kind, unsigned int *arena, size_t size);\n    int (* check_available)(struct memkind *kind);\n    int (* check_addr)(struct memkind *kind, void *addr);\n    void (* init_once)(void);\n    int (* finalize)(struct memkind *kind);\n    size_t (* malloc_usable_size)(struct memkind *kind, void *addr);\n};\n\nstruct memkind {\n    struct memkind_ops *ops;\n    unsigned int partition;\n    char name[MEMKIND_NAME_LENGTH_PRIV];\n    pthread_once_t init_once;\n    unsigned int arena_map_len; // is power of 2\n    unsigned int *arena_map; // To be deleted beyond 1.2.0+\n    pthread_key_t arena_key;\n    void *priv;\n    unsigned int\n    arena_map_mask; // arena_map_len - 1 to optimize modulo operation on arena_map_len\n    unsigned int arena_zero; // index first jemalloc arena of this kind\n};\n\nvoid memkind_init(memkind_t kind, bool check_numa);\n\nvoid *kind_mmap(struct memkind *kind, void *addr, size_t size);\n\n#ifdef __cplusplus\n}\n#endif\n\n"
  },
  {
    "path": "deps/memkind/src/include/memkind/internal/memkind_regular.h",
    "content": "/*\n * Copyright (C) 2017 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#pragma once\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#ifndef MEMKIND_INTERNAL_API\n#warning \"DO NOT INCLUDE THIS FILE! IT IS INTERNAL MEMKIND API AND SOON WILL BE REMOVED FROM BIN & DEVEL PACKAGES\"\n#endif\n\n#include <memkind.h>\n\n/*\n * Header file for the regular memory memkind operations.\n * More details in memkind_regular(3) man page.\n *\n * Functionality defined in this header is considered as EXPERIMENTAL API.\n * API standards are described in memkind(3) man page.\n */\n\nextern struct memkind_ops MEMKIND_REGULAR_OPS;\nint memkind_regular_all_get_mbind_nodemask(struct memkind *kind,\n                                           unsigned long *nodemask,\n                                           unsigned long maxnode);\n\n#ifdef __cplusplus\n}\n#endif\n"
  },
  {
    "path": "deps/memkind/src/include/memkind/internal/tbb_mem_pool_policy.h",
    "content": "/*\n    Copyright (c) 2005-2018 Intel Corporation\n\n    Licensed under the Apache License, Version 2.0 (the \"License\");\n    you may not use this file except in compliance with the License.\n    You may obtain a copy of the License at\n\n        http://www.apache.org/licenses/LICENSE-2.0\n\n    Unless required by applicable law or agreed to in writing, software\n    distributed under the License is distributed on an \"AS IS\" BASIS,\n    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n    See the License for the specific language governing permissions and\n    limitations under the License.\n\n\n\n\n*/\n\n#include <stdint.h>\n\ntypedef void *(*rawAllocType)(intptr_t pool_id, size_t *bytes);\ntypedef int   (*rawFreeType)(intptr_t pool_id, void *raw_ptr, size_t raw_bytes);\n\nstruct MemPoolPolicy {\n    rawAllocType pAlloc;\n    rawFreeType  pFree;\n    size_t       granularity;\n    int          version;\n    unsigned     fixedPool : 1,\n                 keepAllMemory : 1,\n                 reserved : 30;\n};\n"
  },
  {
    "path": "deps/memkind/src/include/memkind/internal/tbb_wrapper.h",
    "content": "/*\n * Copyright (C) 2017 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#pragma once\n#include <memkind.h>\n#include <memkind_deprecated.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/* ops callbacks are replaced by TBB callbacks. */\nvoid tbb_initialize(struct memkind *kind);\n\n/* ptr pointer must come from the valid TBB pool allocation */\nvoid tbb_pool_free(struct memkind *kind, void *ptr);\n\n#ifdef __cplusplus\n}\n#endif"
  },
  {
    "path": "deps/memkind/src/include/memkind.h",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#pragma once\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#include <sys/types.h>\n\n/**\n * Header file for the memkind heap manager.\n * More details in memkind(3) man page.\n *\n * API standards are described in memkind(3) man page.\n */\n\n/// \\warning EXPERIMENTAL API\n#define _MEMKIND_BIT(N) (1ull << N)\n\n/// \\brief Memkind memory types\n/// \\warning EXPERIMENTAL API\ntypedef enum memkind_memtype_t {\n\n    /**\n     * Select standard memory, the same as process use.\n     */\n    MEMKIND_MEMTYPE_DEFAULT = _MEMKIND_BIT(0),\n\n    /**\n     * Select high bandwidth memory (HBM).\n     * There must be at least two memories with different bandwidth to\n     * determine the HBM.\n     */\n    MEMKIND_MEMTYPE_HIGH_BANDWIDTH = _MEMKIND_BIT(1)\n\n} memkind_memtype_t;\n\n#undef _MEMKIND_BIT\n\n/// \\brief Memkind policy\n/// \\warning EXPERIMENTAL API\ntypedef enum memkind_policy_t {\n\n    /**\n     * Allocate local memory.\n     * If there is not enough memory to satisfy the request errno is set to\n     * ENOMEM and the allocated pointer is set to NULL.\n     */\n    MEMKIND_POLICY_BIND_LOCAL = 0,\n\n    /**\n     * Memory locality is ignored.\n     * If there is not enough memory to satisfy the request errno is set to\n     * ENOMEM and the allocated pointer is set to NULL.\n     */\n    MEMKIND_POLICY_BIND_ALL,\n\n    /**\n     * Allocate preferred memory that is local.\n     * If there is not enough preferred memory to satisfy the request or\n     * preferred memory is not available, the allocation will fall back on any\n     * other memory.\n     */\n    MEMKIND_POLICY_PREFERRED_LOCAL,\n\n    /**\n     * Interleave allocation across local memory.\n     * For n memory types the allocation will be interleaved across all of\n     * them.\n     */\n    MEMKIND_POLICY_INTERLEAVE_LOCAL,\n\n    /**\n     * Interleave allocation. Locality is ignored.\n     * For n memory types the allocation will be interleaved across all of\n     * them.\n     */\n    MEMKIND_POLICY_INTERLEAVE_ALL,\n\n    /**\n     * Max policy value.\n     */\n    MEMKIND_POLICY_MAX_VALUE\n\n} memkind_policy_t;\n\n/// \\brief Memkind bits definition\n/// \\warning EXPERIMENTAL API\n/// \\note The bits specify flags and masks. Bits <0,1,2,...,7> are reserved for page size, where page sizes are encoded\n///       by base-2 logarithm. If the page size bits are set to zero value, than default page size will be used.\ntypedef enum memkind_bits_t {\n    MEMKIND_MASK_PAGE_SIZE_2MB = 21ull,  /**<  Allocations backed by 2 MB page size (2^21 = 2MB) */\n} memkind_bits_t;\n\n/// \\brief Memkind type definition\n/// \\warning EXPERIMENTAL API\ntypedef struct memkind *memkind_t;\n\n\n/// \\brief Memkind constant values\n/// \\warning EXPERIMENTAL API\nenum memkind_const {\n    MEMKIND_MAX_KIND = 512,                     /**<  Maximum number of kinds */\n    MEMKIND_ERROR_MESSAGE_SIZE = 128,           /**<  Error message size */\n    MEMKIND_PMEM_MIN_SIZE = (1024 * 1024 * 16)  /**<  The minimum size which allows to limit the file-backed memory partition */\n};\n\n/// \\brief Memkind operation statuses\n/// \\warning EXPERIMENTAL API\nenum {\n    MEMKIND_SUCCESS = 0,                        /**<  Operation success */\n    MEMKIND_ERROR_UNAVAILABLE = -1,             /**<  Error: Memory kind is not available */\n    MEMKIND_ERROR_MBIND = -2,                   /**<  Error: Call to mbind() failed */\n    MEMKIND_ERROR_MMAP  = -3,                   /**<  Error: Call to mmap() failed */\n    MEMKIND_ERROR_MALLOC = -6,                  /**<  Error: Call to malloc() failed */\n    MEMKIND_ERROR_ENVIRON = -12,                /**<  Error: Unable to parse environment variable */\n    MEMKIND_ERROR_INVALID = -13,                /**<  Error: Invalid argument */\n    MEMKIND_ERROR_TOOMANY = -15,                /**<  Error: Attempt to initialize more than MEMKIND_MAX_KIND number of kinds */\n    MEMKIND_ERROR_BADOPS = -17,                 /**<  Error: Invalid memkind_ops structure */\n    MEMKIND_ERROR_HUGETLB = -18,                /**<  Error: Unable to allocate huge pages */\n    MEMKIND_ERROR_MEMTYPE_NOT_AVAILABLE = -20,  /**<  Error: Requested memory type is not available */\n    MEMKIND_ERROR_OPERATION_FAILED = -21,       /**<  Error: Operation failed */\n    MEMKIND_ERROR_ARENAS_CREATE = -22,          /**<  Error: Call to jemalloc's arenas.create failed */\n    MEMKIND_ERROR_RUNTIME = -255                /**<  Error: Unspecified run-time error */\n};\n\n///\n/// \\brief Create kind that allocates memory with specific memory type, memory binding policy and flags.\n/// \\warning EXPERIMENTAL API\n/// \\note Currently implemented memory type and policy configurations:\n///.      {MEMKIND_MEMTYPE_DEFAULT, MEMKIND_POLICY_PREFERRED_LOCAL},\n///.      {MEMKIND_MEMTYPE_HIGH_BANDWIDTH, MEMKIND_POLICY_BIND_LOCAL},\n///       {MEMKIND_MEMTYPE_HIGH_BANDWIDTH, MEMKIND_POLICY_PREFERRED_LOCAL},\n///       {MEMKIND_MEMTYPE_HIGH_BANDWIDTH, MEMKIND_POLICY_INTERLEAVE_ALL},\n///       {MEMKIND_MEMTYPE_DEFAULT | MEMKIND_MEMTYPE_HIGH_BANDWIDTH, MEMKIND_POLICY_INTERLEAVE_ALL}.\n/// \\param memtype_flags determine the memory types to allocate from by combination of memkind_memtype_t values.\n///        This field cannot have zero value.\n/// \\param policy specify policy for page binding to memory types selected by  memtype_flags.\n///        This field must be set to memkind_policy_t value. If policy is set to MEMKIND_POLICY_PREFERRED_LOCAL then only one memory\n///        type must be selected. Note: the value cannot be set to MEMKIND_POLICY_MAX_VALUE.\n/// \\param flags the field must be set to a combination of memkind_bits_t values.\n/// \\param kind pointer to kind which will be created\n/// \\return Memkind operation status, MEMKIND_SUCCESS on success, MEMKIND_ERROR_MEMTYPE_NOT_AVAILABLE or MEMKIND_ERROR_INVALID on failure\n///\nint memkind_create_kind(memkind_memtype_t memtype_flags,\n                        memkind_policy_t policy,\n                        memkind_bits_t flags,\n                        memkind_t *kind);\n\n///\n/// \\brief Destroy previously created kind object, which must have been returned\n///        by a call to memkind_create_kind() or memkind_create_pmem().\n///        The function has undefined behavior when the handle is invalid or\n///        memkind_destroy_kind(kind) was already called before\n/// \\warning EXPERIMENTAL API\n/// \\note if the kind was returned by memkind_create_kind() all allocated memory must be freed\n///       before kind is destroyed, otherwise this will cause memory leak.\n/// \\param kind specified memory kind\n/// \\return Memkind operation status, MEMKIND_SUCCESS on success, MEMKIND_ERROR_OPERATION_FAILED on failure\n///\nint memkind_destroy_kind(memkind_t kind);\n\n\n#include \"memkind_deprecated.h\"\n\n/// \\warning EXPERIMENTAL API\nextern memkind_t MEMKIND_REGULAR;\n\n/// \\warning EXPERIMENTAL API\nextern memkind_t MEMKIND_DEFAULT;\n\n/// \\warning EXPERIMENTAL API\nextern memkind_t MEMKIND_HUGETLB;\n\n/// \\warning EXPERIMENTAL API\nextern memkind_t MEMKIND_HBW;\n\n/// \\warning EXPERIMENTAL API\nextern memkind_t MEMKIND_HBW_ALL;\n\n/// \\warning EXPERIMENTAL API\nextern memkind_t MEMKIND_HBW_PREFERRED;\n\n/// \\warning EXPERIMENTAL API\nextern memkind_t MEMKIND_HBW_HUGETLB;\n\n/// \\warning EXPERIMENTAL API\nextern memkind_t MEMKIND_HBW_ALL_HUGETLB;\n\n/// \\warning EXPERIMENTAL API\nextern memkind_t MEMKIND_HBW_PREFERRED_HUGETLB;\n\n/// \\warning EXPERIMENTAL API\nextern memkind_t MEMKIND_HBW_INTERLEAVE;\n\n/// \\warning EXPERIMENTAL API\nextern memkind_t MEMKIND_INTERLEAVE;\n\n///\n/// \\brief Get Memkind API version\n/// \\note STANDARD API\n/// \\return Version number represented by a single integer number(major * 1000000 + minor * 1000 + patch)\n///\nint memkind_get_version();\n\n///\n/// \\brief Convert error number into an error message\n/// \\note STANDARD API\n/// \\param err error number\n/// \\param msg error message\n/// \\param size size of message\n///\nvoid memkind_error_message(int err, char *msg, size_t size);\n\n///\n/// \\brief Create a new PMEM (file-backed) kind of given size on top of a temporary file\n///        in the given directory dir\n/// \\note STANDARD API\n/// \\param dir path to specified directory to temporary file\n/// \\param max_size size limit for kind\n/// \\param kind pointer to kind which will be created\n/// \\return Memkind operation status, MEMKIND_SUCCESS on success, other values on failure\n///\nint memkind_create_pmem(const char *dir, size_t max_size, memkind_t *kind);\n\n///\n/// \\brief Check if kind is available\n/// \\note STANDARD API\n/// \\param kind specified memory kind\n/// \\return Memkind operation status, MEMKIND_SUCCESS on success, other values on failure\n///\nint memkind_check_available(memkind_t kind);\n\n/* HEAP MANAGEMENT INTERFACE */\n\n///\n/// \\brief Allocates size bytes of uninitialized storage of the specified kind\n/// \\note STANDARD API\n/// \\param kind specified memory kind\n/// \\param size number of bytes to allocate\n/// \\return Pointer to the allocated memory\n///\nvoid *memkind_malloc(memkind_t kind, size_t size);\n\n///\n/// \\brief Obtain size of block of memory allocated with the memkind API\n/// \\note STANDARD API\n/// \\param kind specified memory kind\n/// \\param ptr pointer to the allocated memory\n/// \\return Number of usable bytes\n///\nsize_t memkind_malloc_usable_size(memkind_t kind, void *ptr);\n\n///\n/// \\brief Allocates memory of the specified kind for an array of num elements\n///        of size bytes each and initializes all bytes in the allocated storage to zero\n/// \\note STANDARD API\n/// \\param kind specified memory kind\n/// \\param num number of objects\n/// \\param size specified size of each element\n/// \\return Pointer to the allocated memory\n///\nvoid *memkind_calloc(memkind_t kind, size_t num, size_t size);\n\n/// \\brief Allocates size bytes of the specified kind and places the address of the allocated memory\n///        in *memptr. The address of the allocated memory will be a multiple of alignment,\n///        which must be a power of two and a multiple of sizeof(void *)\n/// \\note EXPERIMENTAL API\n/// \\param kind specified memory kind\n/// \\param memptr address of the allocated memory\n/// \\param alignment specified alignment of bytes\n/// \\param size specified size of bytes\n/// \\return Memkind operation status, MEMKIND_SUCCESS on success, EINVAL or ENOMEM on failure\n///\nint memkind_posix_memalign(memkind_t kind, void **memptr, size_t alignment,\n                           size_t size);\n\n///\n/// \\brief Reallocates memory of the specified kind\n/// \\note STANDARD API\n/// \\param kind specified memory kind\n/// \\param ptr pointer to the memory block to be reallocated\n/// \\param size new size for the memory block in bytes\n/// \\return Pointer to the allocated memory\n///\nvoid *memkind_realloc(memkind_t kind, void *ptr, size_t size);\n\n///\n/// \\brief Free the memory space of the specified kind pointed by ptr\n/// \\note STANDARD API\n/// \\param kind specified memory kind\n/// \\param ptr pointer to the allocated memory\n///\nvoid memkind_free(memkind_t kind, void *ptr);\n\nint memkind_fd(struct memkind *kind);\nvoid memkind_pmem_remapfd(struct memkind *kind, int fdNew);\nint memkind_tmpfile(const char *dir, int *fd);\nmemkind_t memkind_get_kind(void *ptr);\n\n#ifdef __cplusplus\n}\n#endif\n"
  },
  {
    "path": "deps/memkind/src/include/memkind_deprecated.h",
    "content": "/*\n * Copyright (C) 2016 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n/*\n * !!!!!!!!!!!!!!!!!!!\n * !!!   WARNING   !!!\n * !!! PLEASE READ !!!\n * !!!!!!!!!!!!!!!!!!!\n *\n * This header file contains all memkind deprecated symbols.\n *\n * Please avoid usage of this API in newly developed code, as\n * eventually this code is subject for removal.\n */\n\n#pragma once\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#ifndef MEMKIND_DEPRECATED\n\n#ifdef __GNUC__\n#define MEMKIND_DEPRECATED(func) func __attribute__ ((deprecated))\n#elif defined(_MSC_VER)\n#define MEMKIND_DEPRECATED(func) __declspec(deprecated) func\n#else\n#pragma message(\"WARNING: You need to implement MEMKIND_DEPRECATED for this compiler\")\n#define MEMKIND_DEPRECATED(func) func\n#endif\n\n#endif\n\n/*\n * Symbols related to GBTLB that are no longer supported\n */\nextern memkind_t MEMKIND_HBW_GBTLB;\nextern memkind_t MEMKIND_HBW_PREFERRED_GBTLB;\nextern memkind_t MEMKIND_GBTLB;\n\nint MEMKIND_DEPRECATED(memkind_get_kind_by_partition(int partition,\n                                                     memkind_t *kind));\n\nenum memkind_base_partition {\n    MEMKIND_PARTITION_DEFAULT = 0,\n    MEMKIND_PARTITION_HBW = 1,\n    MEMKIND_PARTITION_HBW_HUGETLB = 2,\n    MEMKIND_PARTITION_HBW_PREFERRED = 3,\n    MEMKIND_PARTITION_HBW_PREFERRED_HUGETLB = 4,\n    MEMKIND_PARTITION_HUGETLB = 5,\n    MEMKIND_PARTITION_HBW_GBTLB = 6,\n    MEMKIND_PARTITION_HBW_PREFERRED_GBTLB = 7,\n    MEMKIND_PARTITION_GBTLB = 8,\n    MEMKIND_PARTITION_HBW_INTERLEAVE = 9,\n    MEMKIND_PARTITION_INTERLEAVE = 10,\n    MEMKIND_PARTITION_REGULAR = 11,\n    MEMKIND_PARTITION_HBW_ALL = 12,\n    MEMKIND_PARTITION_HBW_ALL_HUGETLB = 13,\n    MEMKIND_NUM_BASE_KIND\n};\n\n#ifdef __cplusplus\n}\n#endif\n"
  },
  {
    "path": "deps/memkind/src/include/pmem_allocator.h",
    "content": "/*\n * Copyright (C) 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#pragma once\n\n#include <memory>\n#include <string>\n#include <exception>\n#include <type_traits>\n#include <atomic>\n#include <cstddef>\n\n#include \"memkind.h\"\n\n/*\n * Header file for the C++ allocator compatible with the C++ standard library allocator concepts.\n * More details in pmemallocator(3) man page.\n * Note: memory heap management is based on memkind_malloc, refer to the memkind(3) man page for more\n * information.\n *\n * Functionality defined in this header is considered as EXPERIMENTAL API.\n * API standards are described in memkind(3) man page.\n */\nnamespace pmem\n{\n    namespace internal\n    {\n        class kind_wrapper_t\n        {\n        public:\n            kind_wrapper_t(const char *dir, std::size_t max_size)\n            {\n                int err_c  = memkind_create_pmem(dir, max_size, &kind);\n                if (err_c) {\n                    throw std::runtime_error(\n                        std::string(\"An error occured while creating pmem kind; error code: \") +\n                        std::to_string(err_c));\n                }\n            }\n\n            kind_wrapper_t(const kind_wrapper_t &) = delete;\n            void operator=(const kind_wrapper_t &) = delete;\n\n            ~kind_wrapper_t()\n            {\n                memkind_destroy_kind(kind);\n            }\n\n            memkind_t get() const\n            {\n                return kind;\n            }\n\n        private:\n            memkind_t kind;\n        };\n    }\n\n    template<typename T>\n    class allocator\n    {\n        using kind_wrapper_t = internal::kind_wrapper_t;\n        std::shared_ptr<kind_wrapper_t> kind_wrapper_ptr;\n    public:\n        using value_type = T;\n        using pointer = value_type*;\n        using const_pointer = const value_type*;\n        using reference = value_type&;\n        using const_reference = const value_type&;\n        using size_type = size_t;\n        using difference_type = ptrdiff_t;\n\n        template<class U>\n        struct rebind {\n            using other = allocator<U>;\n        };\n\n        template<typename U>\n        friend class allocator;\n\n#ifndef _GLIBCXX_USE_CXX11_ABI\n        /* This is a workaround for compilers (e.g GCC 4.8) that uses C++11 standard,\n         * but use old - non C++11 ABI */\n        template<typename V = void>\n        explicit allocator()\n        {\n            static_assert(std::is_same<V, void>::value,\n                          \"pmem::allocator cannot be compiled without CXX11 ABI\");\n        }\n#endif\n\n        explicit allocator(const char *dir, size_t max_size) :\n            kind_wrapper_ptr(std::make_shared<kind_wrapper_t>(dir, max_size))\n        {\n        }\n\n        explicit allocator(const std::string &dir, size_t max_size) :\n            allocator(dir.c_str(), max_size)\n        {\n        }\n\n        allocator(const allocator &other) = default;\n\n        template <typename U>\n        allocator(const allocator<U> &other) noexcept : kind_wrapper_ptr(\n                other.kind_wrapper_ptr)\n        {\n        }\n\n        allocator(allocator &&other) = default;\n\n        template <typename U>\n        allocator(allocator<U> &&other) noexcept :\n            kind_wrapper_ptr(std::move(other.kind_wrapper_ptr))\n        {\n        }\n\n        allocator<T> &operator = (const allocator &other) = default;\n\n        template <typename U>\n        allocator<T> &operator = (const allocator<U> &other) noexcept\n        {\n            kind_wrapper_ptr = other.kind_wrapper_ptr;\n            return *this;\n        }\n\n        allocator<T> &operator = (allocator &&other) = default;\n\n        template <typename U>\n        allocator<T> &operator = (allocator<U> &&other) noexcept\n        {\n            kind_wrapper_ptr = std::move(other.kind_wrapper_ptr);\n            return *this;\n        }\n\n        pointer allocate(size_type n) const\n        {\n            pointer result = static_cast<pointer>(memkind_malloc(kind_wrapper_ptr->get(),\n                                                                 n*sizeof(T)));\n            if (!result) {\n                throw std::bad_alloc();\n            }\n            return result;\n        }\n\n        void deallocate(pointer p, size_type n) const\n        {\n            memkind_free(kind_wrapper_ptr->get(), static_cast<void *>(p));\n        }\n\n        template <class U, class... Args>\n        void construct(U *p, Args &&... args) const\n        {\n            ::new((void *)p) U(std::forward<Args>(args)...);\n        }\n\n        void destroy(pointer p) const\n        {\n            p->~value_type();\n        }\n\n        template <typename U, typename V>\n        friend bool operator ==(const allocator<U> &lhs, const allocator<V> &rhs);\n\n        template <typename U, typename V>\n        friend bool operator !=(const allocator<U> &lhs, const allocator<V> &rhs);\n    };\n\n    template <typename U, typename V>\n    bool operator ==(const allocator<U> &lhs, const allocator<V> &rhs)\n    {\n        return lhs.kind_wrapper_ptr->get() == rhs.kind_wrapper_ptr->get();\n    }\n\n    template <typename U, typename V>\n    bool operator !=(const allocator<U> &lhs, const allocator<V> &rhs)\n    {\n        return !(lhs == rhs);\n    }\n}\n"
  },
  {
    "path": "deps/memkind/src/install_astyle.sh",
    "content": "#!/bin/bash\n#\n#  Copyright (C) 2018 Intel Corporation.\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#  1. Redistributions of source code must retain the above copyright notice(s),\n#     this list of conditions and the following disclaimer.\n#  2. Redistributions in binary form must reproduce the above copyright notice(s),\n#     this list of conditions and the following disclaimer in the documentation\n#     and/or other materials provided with the distribution.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n#  EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n#  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\nset -ex\ncurl -SL https://sourceforge.net/projects/astyle/files/astyle/astyle%203.1/astyle_3.1_linux.tar.gz -o /tmp/astyle.tar.gz\ntar -xzvf /tmp/astyle.tar.gz -C /tmp/\necho \"Install astyle\"\ncd /tmp/astyle/build/gcc\nmake\nsudo make install\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/.appveyor.yml",
    "content": "version: '{build}'\n\nenvironment:\n  matrix:\n  - MSYSTEM: MINGW64\n    CPU: x86_64\n    MSVC: amd64\n  - MSYSTEM: MINGW32\n    CPU: i686\n    MSVC: x86\n  - MSYSTEM: MINGW64\n    CPU: x86_64\n  - MSYSTEM: MINGW32\n    CPU: i686\n  - MSYSTEM: MINGW64\n    CPU: x86_64\n    MSVC: amd64\n    CONFIG_FLAGS: --enable-debug\n  - MSYSTEM: MINGW32\n    CPU: i686\n    MSVC: x86\n    CONFIG_FLAGS: --enable-debug\n  - MSYSTEM: MINGW64\n    CPU: x86_64\n    CONFIG_FLAGS: --enable-debug\n  - MSYSTEM: MINGW32\n    CPU: i686\n    CONFIG_FLAGS: --enable-debug\n\ninstall:\n  - set PATH=c:\\msys64\\%MSYSTEM%\\bin;c:\\msys64\\usr\\bin;%PATH%\n  - if defined MSVC call \"c:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\vcvarsall.bat\" %MSVC%\n  - if defined MSVC pacman --noconfirm -Rsc mingw-w64-%CPU%-gcc gcc\n  - pacman --noconfirm -Suy mingw-w64-%CPU%-make\n\nbuild_script:\n  - bash -c \"autoconf\"\n  - bash -c \"./configure $CONFIG_FLAGS\"\n  - mingw32-make\n  - file lib/jemalloc.dll\n  - mingw32-make tests\n  - mingw32-make -k check\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/.autom4te.cfg",
    "content": "begin-language: \"Autoconf-without-aclocal-m4\"\nargs: --no-cache\nend-language: \"Autoconf-without-aclocal-m4\"\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/.gitattributes",
    "content": "* text=auto eol=lf\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/.gitignore",
    "content": "/bin/jemalloc-config\n/bin/jemalloc.sh\n/bin/jeprof\n\n/config.stamp\n/config.log\n/config.status\n/configure\n\n/doc/html.xsl\n/doc/manpages.xsl\n/doc/jemalloc.xml\n/doc/jemalloc.html\n/doc/jemalloc.3\n\n/jemalloc.pc\n\n/lib/\n\n/Makefile\n\n/include/jemalloc/internal/jemalloc_preamble.h\n/include/jemalloc/internal/jemalloc_internal_defs.h\n/include/jemalloc/internal/private_namespace.gen.h\n/include/jemalloc/internal/private_namespace.h\n/include/jemalloc/internal/private_namespace_jet.gen.h\n/include/jemalloc/internal/private_namespace_jet.h\n/include/jemalloc/internal/private_symbols.awk\n/include/jemalloc/internal/private_symbols_jet.awk\n/include/jemalloc/internal/public_namespace.h\n/include/jemalloc/internal/public_symbols.txt\n/include/jemalloc/internal/public_unnamespace.h\n/include/jemalloc/internal/size_classes.h\n/include/jemalloc/jemalloc.h\n/include/jemalloc/jemalloc_defs.h\n/include/jemalloc/jemalloc_macros.h\n/include/jemalloc/jemalloc_mangle.h\n/include/jemalloc/jemalloc_mangle_jet.h\n/include/jemalloc/jemalloc_protos.h\n/include/jemalloc/jemalloc_protos_jet.h\n/include/jemalloc/jemalloc_rename.h\n/include/jemalloc/jemalloc_typedefs.h\n\n/src/*.[od]\n/src/*.sym\n\n/run_tests.out/\n\n/test/test.sh\ntest/include/test/jemalloc_test.h\ntest/include/test/jemalloc_test_defs.h\n\n/test/integration/[A-Za-z]*\n!/test/integration/[A-Za-z]*.*\n/test/integration/*.[od]\n/test/integration/*.out\n\n/test/integration/cpp/[A-Za-z]*\n!/test/integration/cpp/[A-Za-z]*.*\n/test/integration/cpp/*.[od]\n/test/integration/cpp/*.out\n\n/test/src/*.[od]\n\n/test/stress/[A-Za-z]*\n!/test/stress/[A-Za-z]*.*\n/test/stress/*.[od]\n/test/stress/*.out\n\n/test/unit/[A-Za-z]*\n!/test/unit/[A-Za-z]*.*\n/test/unit/*.[od]\n/test/unit/*.out\n\n/VERSION\n\n*.pdb\n*.sdf\n*.opendb\n*.opensdf\n*.cachefile\n*.suo\n*.user\n*.sln.docstates\n*.tmp\n/msvc/Win32/\n/msvc/x64/\n/msvc/projects/*/*/Debug*/\n/msvc/projects/*/*/Release*/\n/msvc/projects/*/*/Win32/\n/msvc/projects/*/*/x64/\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/.travis.yml",
    "content": "language: generic\n\nmatrix:\n  include:\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: osx\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=clang CXX=clang++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"-m32\" CONFIGURE_FLAGS=\"\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n      addons:\n        apt:\n          packages:\n            - gcc-multilib\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-debug\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-prof\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--disable-stats\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=tcache:false\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=dss:primary\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=percpu_arena:percpu\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=background_thread:true\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: osx\n      env: CC=clang CXX=clang++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: osx\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"-m32\" CONFIGURE_FLAGS=\"\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: osx\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-debug\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: osx\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--disable-stats\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: osx\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=tcache:false\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=clang CXX=clang++ COMPILER_FLAGS=\"-m32\" CONFIGURE_FLAGS=\"\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n      addons:\n        apt:\n          packages:\n            - gcc-multilib\n    - os: linux\n      env: CC=clang CXX=clang++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-debug\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=clang CXX=clang++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-prof\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=clang CXX=clang++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--disable-stats\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=clang CXX=clang++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=tcache:false\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=clang CXX=clang++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=dss:primary\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=clang CXX=clang++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=percpu_arena:percpu\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=clang CXX=clang++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=background_thread:true\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"-m32\" CONFIGURE_FLAGS=\"--enable-debug\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n      addons:\n        apt:\n          packages:\n            - gcc-multilib\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"-m32\" CONFIGURE_FLAGS=\"--enable-prof\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n      addons:\n        apt:\n          packages:\n            - gcc-multilib\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"-m32\" CONFIGURE_FLAGS=\"--disable-stats\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n      addons:\n        apt:\n          packages:\n            - gcc-multilib\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"-m32\" CONFIGURE_FLAGS=\"--with-malloc-conf=tcache:false\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n      addons:\n        apt:\n          packages:\n            - gcc-multilib\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"-m32\" CONFIGURE_FLAGS=\"--with-malloc-conf=dss:primary\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n      addons:\n        apt:\n          packages:\n            - gcc-multilib\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"-m32\" CONFIGURE_FLAGS=\"--with-malloc-conf=percpu_arena:percpu\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n      addons:\n        apt:\n          packages:\n            - gcc-multilib\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"-m32\" CONFIGURE_FLAGS=\"--with-malloc-conf=background_thread:true\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n      addons:\n        apt:\n          packages:\n            - gcc-multilib\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-debug --enable-prof\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-debug --disable-stats\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-debug --with-malloc-conf=tcache:false\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-debug --with-malloc-conf=dss:primary\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-debug --with-malloc-conf=percpu_arena:percpu\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-debug --with-malloc-conf=background_thread:true\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-prof --disable-stats\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-prof --with-malloc-conf=tcache:false\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-prof --with-malloc-conf=dss:primary\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-prof --with-malloc-conf=percpu_arena:percpu\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--enable-prof --with-malloc-conf=background_thread:true\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--disable-stats --with-malloc-conf=tcache:false\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--disable-stats --with-malloc-conf=dss:primary\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--disable-stats --with-malloc-conf=percpu_arena:percpu\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--disable-stats --with-malloc-conf=background_thread:true\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=tcache:false,dss:primary\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=tcache:false,percpu_arena:percpu\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=tcache:false,background_thread:true\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=dss:primary,percpu_arena:percpu\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=dss:primary,background_thread:true\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n    - os: linux\n      env: CC=gcc CXX=g++ COMPILER_FLAGS=\"\" CONFIGURE_FLAGS=\"--with-malloc-conf=percpu_arena:percpu,background_thread:true\" EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"\n\n\nbefore_script:\n  - autoconf\n  - ./configure ${COMPILER_FLAGS:+       CC=\"$CC $COMPILER_FLAGS\"       CXX=\"$CXX $COMPILER_FLAGS\" }       $CONFIGURE_FLAGS\n  - make -j3\n  - make -j3 tests\n\nscript:\n  - make check\n\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/COPYING",
    "content": "Unless otherwise specified, files in the jemalloc source distribution are\nsubject to the following license:\n--------------------------------------------------------------------------------\nCopyright (C) 2002-2017 Jason Evans <jasone@canonware.com>.\nAll rights reserved.\nCopyright (C) 2007-2012 Mozilla Foundation.  All rights reserved.\nCopyright (C) 2009-2017 Facebook, Inc.  All rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are met:\n1. Redistributions of source code must retain the above copyright notice(s),\n   this list of conditions and the following disclaimer.\n2. Redistributions in binary form must reproduce the above copyright notice(s),\n   this list of conditions and the following disclaimer in the documentation\n   and/or other materials provided with the distribution.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\nOR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\nMERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\nEVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\nINCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\nLIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\nPROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\nLIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\nOR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\nADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n--------------------------------------------------------------------------------\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/ChangeLog",
    "content": "Following are change highlights associated with official releases.  Important\nbug fixes are all mentioned, but some internal enhancements are omitted here for\nbrevity.  Much more detail can be found in the git revision history:\n\n    https://github.com/jemalloc/jemalloc\n\n* 5.0.0 (June 13, 2017)\n\n  Unlike all previous jemalloc releases, this release does not use naturally\n  aligned \"chunks\" for virtual memory management, and instead uses page-aligned\n  \"extents\".  This change has few externally visible effects, but the internal\n  impacts are... extensive.  Many other internal changes combine to make this\n  the most cohesively designed version of jemalloc so far, with ample\n  opportunity for further enhancements.\n\n  Continuous integration is now an integral aspect of development thanks to the\n  efforts of @davidtgoldblatt, and the dev branch tends to remain reasonably\n  stable on the tested platforms (Linux, FreeBSD, macOS, and Windows).  As a\n  side effect the official release frequency may decrease over time.\n\n  New features:\n  - Implement optional per-CPU arena support; threads choose which arena to use\n    based on current CPU rather than on fixed thread-->arena associations.\n    (@interwq)\n  - Implement two-phase decay of unused dirty pages.  Pages transition from\n    dirty-->muzzy-->clean, where the first phase transition relies on\n    madvise(... MADV_FREE) semantics, and the second phase transition discards\n    pages such that they are replaced with demand-zeroed pages on next access.\n    (@jasone)\n  - Increase decay time resolution from seconds to milliseconds.  (@jasone)\n  - Implement opt-in per CPU background threads, and use them for asynchronous\n    decay-driven unused dirty page purging.  (@interwq)\n  - Add mutex profiling, which collects a variety of statistics useful for\n    diagnosing overhead/contention issues.  (@interwq)\n  - Add C++ new/delete operator bindings.  (@djwatson)\n  - Support manually created arena destruction, such that all data and metadata\n    are discarded.  Add MALLCTL_ARENAS_DESTROYED for accessing merged stats\n    associated with destroyed arenas.  (@jasone)\n  - Add MALLCTL_ARENAS_ALL as a fixed index for use in accessing\n    merged/destroyed arena statistics via mallctl.  (@jasone)\n  - Add opt.abort_conf to optionally abort if invalid configuration options are\n    detected during initialization.  (@interwq)\n  - Add opt.stats_print_opts, so that e.g. JSON output can be selected for the\n    stats dumped during exit if opt.stats_print is true.  (@jasone)\n  - Add --with-version=VERSION for use when embedding jemalloc into another\n    project's git repository.  (@jasone)\n  - Add --disable-thp to support cross compiling.  (@jasone)\n  - Add --with-lg-hugepage to support cross compiling.  (@jasone)\n  - Add mallctl interfaces (various authors):\n    + background_thread\n    + opt.abort_conf\n    + opt.retain\n    + opt.percpu_arena\n    + opt.background_thread\n    + opt.{dirty,muzzy}_decay_ms\n    + opt.stats_print_opts\n    + arena.<i>.initialized\n    + arena.<i>.destroy\n    + arena.<i>.{dirty,muzzy}_decay_ms\n    + arena.<i>.extent_hooks\n    + arenas.{dirty,muzzy}_decay_ms\n    + arenas.bin.<i>.slab_size\n    + arenas.nlextents\n    + arenas.lextent.<i>.size\n    + arenas.create\n    + stats.background_thread.{num_threads,num_runs,run_interval}\n    + stats.mutexes.{ctl,background_thread,prof,reset}.\n      {num_ops,num_spin_acq,num_wait,max_wait_time,total_wait_time,max_num_thds,\n      num_owner_switch}\n    + stats.arenas.<i>.{dirty,muzzy}_decay_ms\n    + stats.arenas.<i>.uptime\n    + stats.arenas.<i>.{pmuzzy,base,internal,resident}\n    + stats.arenas.<i>.{dirty,muzzy}_{npurge,nmadvise,purged}\n    + stats.arenas.<i>.bins.<j>.{nslabs,reslabs,curslabs}\n    + stats.arenas.<i>.bins.<j>.mutex.\n      {num_ops,num_spin_acq,num_wait,max_wait_time,total_wait_time,max_num_thds,\n      num_owner_switch}\n    + stats.arenas.<i>.lextents.<j>.{nmalloc,ndalloc,nrequests,curlextents}\n    + stats.arenas.i.mutexes.{large,extent_avail,extents_dirty,extents_muzzy,\n      extents_retained,decay_dirty,decay_muzzy,base,tcache_list}.\n      {num_ops,num_spin_acq,num_wait,max_wait_time,total_wait_time,max_num_thds,\n      num_owner_switch}\n\n  Portability improvements:\n  - Improve reentrant allocation support, such that deadlock is less likely if\n    e.g. a system library call in turn allocates memory.  (@davidtgoldblatt,\n    @interwq)\n  - Support static linking of jemalloc with glibc.  (@djwatson)\n\n  Optimizations and refactors:\n  - Organize virtual memory as \"extents\" of virtual memory pages, rather than as\n    naturally aligned \"chunks\", and store all metadata in arbitrarily distant\n    locations.  This reduces virtual memory external fragmentation, and will\n    interact better with huge pages (not yet explicitly supported).  (@jasone)\n  - Fold large and huge size classes together; only small and large size classes\n    remain.  (@jasone)\n  - Unify the allocation paths, and merge most fast-path branching decisions.\n    (@davidtgoldblatt, @interwq)\n  - Embed per thread automatic tcache into thread-specific data, which reduces\n    conditional branches and dereferences.  Also reorganize tcache to increase\n    fast-path data locality.  (@interwq)\n  - Rewrite atomics to closely model the C11 API, convert various\n    synchronization from mutex-based to atomic, and use the explicit memory\n    ordering control to resolve various hypothetical races without increasing\n    synchronization overhead.  (@davidtgoldblatt)\n  - Extensively optimize rtree via various methods:\n    + Add multiple layers of rtree lookup caching, since rtree lookups are now\n      part of fast-path deallocation.  (@interwq)\n    + Determine rtree layout at compile time.  (@jasone)\n    + Make the tree shallower for common configurations.  (@jasone)\n    + Embed the root node in the top-level rtree data structure, thus avoiding\n      one level of indirection.  (@jasone)\n    + Further specialize leaf elements as compared to internal node elements,\n      and directly embed extent metadata needed for fast-path deallocation.\n      (@jasone)\n    + Ignore leading always-zero address bits (architecture-specific).\n      (@jasone)\n  - Reorganize headers (ongoing work) to make them hermetic, and disentangle\n    various module dependencies.  (@davidtgoldblatt)\n  - Convert various internal data structures such as size class metadata from\n    boot-time-initialized to compile-time-initialized.  Propagate resulting data\n    structure simplifications, such as making arena metadata fixed-size.\n    (@jasone)\n  - Simplify size class lookups when constrained to size classes that are\n    multiples of the page size.  This speeds lookups, but the primary benefit is\n    complexity reduction in code that was the source of numerous regressions.\n    (@jasone)\n  - Lock individual extents when possible for localized extent operations,\n    rather than relying on a top-level arena lock.  (@davidtgoldblatt, @jasone)\n  - Use first fit layout policy instead of best fit, in order to improve\n    packing.  (@jasone)\n  - If munmap(2) is not in use, use an exponential series to grow each arena's\n    virtual memory, so that the number of disjoint virtual memory mappings\n    remains low.  (@jasone)\n  - Implement per arena base allocators, so that arenas never share any virtual\n    memory pages.  (@jasone)\n  - Automatically generate private symbol name mangling macros.  (@jasone)\n\n  Incompatible changes:\n  - Replace chunk hooks with an expanded/normalized set of extent hooks.\n    (@jasone)\n  - Remove ratio-based purging.  (@jasone)\n  - Remove --disable-tcache.  (@jasone)\n  - Remove --disable-tls.  (@jasone)\n  - Remove --enable-ivsalloc.  (@jasone)\n  - Remove --with-lg-size-class-group.  (@jasone)\n  - Remove --with-lg-tiny-min.  (@jasone)\n  - Remove --disable-cc-silence.  (@jasone)\n  - Remove --enable-code-coverage.  (@jasone)\n  - Remove --disable-munmap (replaced by opt.retain).  (@jasone)\n  - Remove Valgrind support.  (@jasone)\n  - Remove quarantine support.  (@jasone)\n  - Remove redzone support.  (@jasone)\n  - Remove mallctl interfaces (various authors):\n    + config.munmap\n    + config.tcache\n    + config.tls\n    + config.valgrind\n    + opt.lg_chunk\n    + opt.purge\n    + opt.lg_dirty_mult\n    + opt.decay_time\n    + opt.quarantine\n    + opt.redzone\n    + opt.thp\n    + arena.<i>.lg_dirty_mult\n    + arena.<i>.decay_time\n    + arena.<i>.chunk_hooks\n    + arenas.initialized\n    + arenas.lg_dirty_mult\n    + arenas.decay_time\n    + arenas.bin.<i>.run_size\n    + arenas.nlruns\n    + arenas.lrun.<i>.size\n    + arenas.nhchunks\n    + arenas.hchunk.<i>.size\n    + arenas.extend\n    + stats.cactive\n    + stats.arenas.<i>.lg_dirty_mult\n    + stats.arenas.<i>.decay_time\n    + stats.arenas.<i>.metadata.{mapped,allocated}\n    + stats.arenas.<i>.{npurge,nmadvise,purged}\n    + stats.arenas.<i>.huge.{allocated,nmalloc,ndalloc,nrequests}\n    + stats.arenas.<i>.bins.<j>.{nruns,reruns,curruns}\n    + stats.arenas.<i>.lruns.<j>.{nmalloc,ndalloc,nrequests,curruns}\n    + stats.arenas.<i>.hchunks.<j>.{nmalloc,ndalloc,nrequests,curhchunks}\n\n  Bug fixes:\n  - Improve interval-based profile dump triggering to dump only one profile when\n    a single allocation's size exceeds the interval.  (@jasone)\n  - Use prefixed function names (as controlled by --with-jemalloc-prefix) when\n    pruning backtrace frames in jeprof.  (@jasone)\n\n* 4.5.0 (February 28, 2017)\n\n  This is the first release to benefit from much broader continuous integration\n  testing, thanks to @davidtgoldblatt.  Had we had this testing infrastructure\n  in place for prior releases, it would have caught all of the most serious\n  regressions fixed by this release.\n\n  New features:\n  - Add --disable-thp and the opt.thp mallctl to provide opt-out mechanisms for\n    transparent huge page integration.  (@jasone)\n  - Update zone allocator integration to work with macOS 10.12.  (@glandium)\n  - Restructure *CFLAGS configuration, so that CFLAGS behaves typically, and\n    EXTRA_CFLAGS provides a way to specify e.g. -Werror during building, but not\n    during configuration.  (@jasone, @ronawho)\n\n  Bug fixes:\n  - Fix DSS (sbrk(2)-based) allocation.  This regression was first released in\n    4.3.0.  (@jasone)\n  - Handle race in per size class utilization computation.  This functionality\n    was first released in 4.0.0.  (@interwq)\n  - Fix lock order reversal during gdump.  (@jasone)\n  - Fix/refactor tcache synchronization.  This regression was first released in\n    4.0.0.  (@jasone)\n  - Fix various JSON-formatted malloc_stats_print() bugs.  This functionality\n    was first released in 4.3.0.  (@jasone)\n  - Fix huge-aligned allocation.  This regression was first released in 4.4.0.\n    (@jasone)\n  - When transparent huge page integration is enabled, detect what state pages\n    start in according to the kernel's current operating mode, and only convert\n    arena chunks to non-huge during purging if that is not their initial state.\n    This functionality was first released in 4.4.0.  (@jasone)\n  - Fix lg_chunk clamping for the --enable-cache-oblivious --disable-fill case.\n    This regression was first released in 4.0.0.  (@jasone, @428desmo)\n  - Properly detect sparc64 when building for Linux.  (@glaubitz)\n\n* 4.4.0 (December 3, 2016)\n\n  New features:\n  - Add configure support for *-*-linux-android.  (@cferris1000, @jasone)\n  - Add the --disable-syscall configure option, for use on systems that place\n    security-motivated limitations on syscall(2).  (@jasone)\n  - Add support for Debian GNU/kFreeBSD.  (@thesam)\n\n  Optimizations:\n  - Add extent serial numbers and use them where appropriate as a sort key that\n    is higher priority than address, so that the allocation policy prefers older\n    extents.  This tends to improve locality (decrease fragmentation) when\n    memory grows downward.  (@jasone)\n  - Refactor madvise(2) configuration so that MADV_FREE is detected and utilized\n    on Linux 4.5 and newer.  (@jasone)\n  - Mark partially purged arena chunks as non-huge-page.  This improves\n    interaction with Linux's transparent huge page functionality.  (@jasone)\n\n  Bug fixes:\n  - Fix size class computations for edge conditions involving extremely large\n    allocations.  This regression was first released in 4.0.0.  (@jasone,\n    @ingvarha)\n  - Remove overly restrictive assertions related to the cactive statistic.  This\n    regression was first released in 4.1.0.  (@jasone)\n  - Implement a more reliable detection scheme for os_unfair_lock on macOS.\n    (@jszakmeister)\n\n* 4.3.1 (November 7, 2016)\n\n  Bug fixes:\n  - Fix a severe virtual memory leak.  This regression was first released in\n    4.3.0.  (@interwq, @jasone)\n  - Refactor atomic and prng APIs to restore support for 32-bit platforms that\n    use pre-C11 toolchains, e.g. FreeBSD's mips.  (@jasone)\n\n* 4.3.0 (November 4, 2016)\n\n  This is the first release that passes the test suite for multiple Windows\n  configurations, thanks in large part to @glandium setting up continuous\n  integration via AppVeyor (and Travis CI for Linux and OS X).\n\n  New features:\n  - Add \"J\" (JSON) support to malloc_stats_print().  (@jasone)\n  - Add Cray compiler support.  (@ronawho)\n\n  Optimizations:\n  - Add/use adaptive spinning for bootstrapping and radix tree node\n    initialization.  (@jasone)\n\n  Bug fixes:\n  - Fix large allocation to search starting in the optimal size class heap,\n    which can substantially reduce virtual memory churn and fragmentation.  This\n    regression was first released in 4.0.0.  (@mjp41, @jasone)\n  - Fix stats.arenas.<i>.nthreads accounting.  (@interwq)\n  - Fix and simplify decay-based purging.  (@jasone)\n  - Make DSS (sbrk(2)-related) operations lockless, which resolves potential\n    deadlocks during thread exit.  (@jasone)\n  - Fix over-sized allocation of radix tree leaf nodes.  (@mjp41, @ogaun,\n    @jasone)\n  - Fix over-sized allocation of arena_t (plus associated stats) data\n    structures.  (@jasone, @interwq)\n  - Fix EXTRA_CFLAGS to not affect configuration.  (@jasone)\n  - Fix a Valgrind integration bug.  (@ronawho)\n  - Disallow 0x5a junk filling when running in Valgrind.  (@jasone)\n  - Fix a file descriptor leak on Linux.  This regression was first released in\n    4.2.0.  (@vsarunas, @jasone)\n  - Fix static linking of jemalloc with glibc.  (@djwatson)\n  - Use syscall(2) rather than {open,read,close}(2) during boot on Linux.  This\n    works around other libraries' system call wrappers performing reentrant\n    allocation.  (@kspinka, @Whissi, @jasone)\n  - Fix OS X default zone replacement to work with OS X 10.12.  (@glandium,\n    @jasone)\n  - Fix cached memory management to avoid needless commit/decommit operations\n    during purging, which resolves permanent virtual memory map fragmentation\n    issues on Windows.  (@mjp41, @jasone)\n  - Fix TSD fetches to avoid (recursive) allocation.  This is relevant to\n    non-TLS and Windows configurations.  (@jasone)\n  - Fix malloc_conf overriding to work on Windows.  (@jasone)\n  - Forcibly disable lazy-lock on Windows (was forcibly *enabled*).  (@jasone)\n\n* 4.2.1 (June 8, 2016)\n\n  Bug fixes:\n  - Fix bootstrapping issues for configurations that require allocation during\n    tsd initialization (e.g. --disable-tls).  (@cferris1000, @jasone)\n  - Fix gettimeofday() version of nstime_update().  (@ronawho)\n  - Fix Valgrind regressions in calloc() and chunk_alloc_wrapper().  (@ronawho)\n  - Fix potential VM map fragmentation regression.  (@jasone)\n  - Fix opt_zero-triggered in-place huge reallocation zeroing.  (@jasone)\n  - Fix heap profiling context leaks in reallocation edge cases.  (@jasone)\n\n* 4.2.0 (May 12, 2016)\n\n  New features:\n  - Add the arena.<i>.reset mallctl, which makes it possible to discard all of\n    an arena's allocations in a single operation.  (@jasone)\n  - Add the stats.retained and stats.arenas.<i>.retained statistics.  (@jasone)\n  - Add the --with-version configure option.  (@jasone)\n  - Support --with-lg-page values larger than actual page size.  (@jasone)\n\n  Optimizations:\n  - Use pairing heaps rather than red-black trees for various hot data\n    structures.  (@djwatson, @jasone)\n  - Streamline fast paths of rtree operations.  (@jasone)\n  - Optimize the fast paths of calloc() and [m,d,sd]allocx().  (@jasone)\n  - Decommit unused virtual memory if the OS does not overcommit.  (@jasone)\n  - Specify MAP_NORESERVE on Linux if [heuristic] overcommit is active, in order\n    to avoid unfortunate interactions during fork(2).  (@jasone)\n\n  Bug fixes:\n  - Fix chunk accounting related to triggering gdump profiles.  (@jasone)\n  - Link against librt for clock_gettime(2) if glibc < 2.17.  (@jasone)\n  - Scale leak report summary according to sampling probability.  (@jasone)\n\n* 4.1.1 (May 3, 2016)\n\n  This bugfix release resolves a variety of mostly minor issues, though the\n  bitmap fix is critical for 64-bit Windows.\n\n  Bug fixes:\n  - Fix the linear scan version of bitmap_sfu() to shift by the proper amount\n    even when sizeof(long) is not the same as sizeof(void *), as on 64-bit\n    Windows.  (@jasone)\n  - Fix hashing functions to avoid unaligned memory accesses (and resulting\n    crashes).  This is relevant at least to some ARM-based platforms.\n    (@rkmisra)\n  - Fix fork()-related lock rank ordering reversals.  These reversals were\n    unlikely to cause deadlocks in practice except when heap profiling was\n    enabled and active.  (@jasone)\n  - Fix various chunk leaks in OOM code paths.  (@jasone)\n  - Fix malloc_stats_print() to print opt.narenas correctly.  (@jasone)\n  - Fix MSVC-specific build/test issues.  (@rustyx, @yuslepukhin)\n  - Fix a variety of test failures that were due to test fragility rather than\n    core bugs.  (@jasone)\n\n* 4.1.0 (February 28, 2016)\n\n  This release is primarily about optimizations, but it also incorporates a lot\n  of portability-motivated refactoring and enhancements.  Many people worked on\n  this release, to an extent that even with the omission here of minor changes\n  (see git revision history), and of the people who reported and diagnosed\n  issues, so much of the work was contributed that starting with this release,\n  changes are annotated with author credits to help reflect the collaborative\n  effort involved.\n\n  New features:\n  - Implement decay-based unused dirty page purging, a major optimization with\n    mallctl API impact.  This is an alternative to the existing ratio-based\n    unused dirty page purging, and is intended to eventually become the sole\n    purging mechanism.  New mallctls:\n    + opt.purge\n    + opt.decay_time\n    + arena.<i>.decay\n    + arena.<i>.decay_time\n    + arenas.decay_time\n    + stats.arenas.<i>.decay_time\n    (@jasone, @cevans87)\n  - Add --with-malloc-conf, which makes it possible to embed a default\n    options string during configuration.  This was motivated by the desire to\n    specify --with-malloc-conf=purge:decay , since the default must remain\n    purge:ratio until the 5.0.0 release.  (@jasone)\n  - Add MS Visual Studio 2015 support.  (@rustyx, @yuslepukhin)\n  - Make *allocx() size class overflow behavior defined.  The maximum\n    size class is now less than PTRDIFF_MAX to protect applications against\n    numerical overflow, and all allocation functions are guaranteed to indicate\n    errors rather than potentially crashing if the request size exceeds the\n    maximum size class.  (@jasone)\n  - jeprof:\n    + Add raw heap profile support.  (@jasone)\n    + Add --retain and --exclude for backtrace symbol filtering.  (@jasone)\n\n  Optimizations:\n  - Optimize the fast path to combine various bootstrapping and configuration\n    checks and execute more streamlined code in the common case.  (@interwq)\n  - Use linear scan for small bitmaps (used for small object tracking).  In\n    addition to speeding up bitmap operations on 64-bit systems, this reduces\n    allocator metadata overhead by approximately 0.2%.  (@djwatson)\n  - Separate arena_avail trees, which substantially speeds up run tree\n    operations.  (@djwatson)\n  - Use memoization (boot-time-computed table) for run quantization.  Separate\n    arena_avail trees reduced the importance of this optimization.  (@jasone)\n  - Attempt mmap-based in-place huge reallocation.  This can dramatically speed\n    up incremental huge reallocation.  (@jasone)\n\n  Incompatible changes:\n  - Make opt.narenas unsigned rather than size_t.  (@jasone)\n\n  Bug fixes:\n  - Fix stats.cactive accounting regression.  (@rustyx, @jasone)\n  - Handle unaligned keys in hash().  This caused problems for some ARM systems.\n    (@jasone, @cferris1000)\n  - Refactor arenas array.  In addition to fixing a fork-related deadlock, this\n    makes arena lookups faster and simpler.  (@jasone)\n  - Move retained memory allocation out of the default chunk allocation\n    function, to a location that gets executed even if the application installs\n    a custom chunk allocation function.  This resolves a virtual memory leak.\n    (@buchgr)\n  - Fix a potential tsd cleanup leak.  (@cferris1000, @jasone)\n  - Fix run quantization.  In practice this bug had no impact unless\n    applications requested memory with alignment exceeding one page.\n    (@jasone, @djwatson)\n  - Fix LinuxThreads-specific bootstrapping deadlock.  (Cosmin Paraschiv)\n  - jeprof:\n    + Don't discard curl options if timeout is not defined.  (@djwatson)\n    + Detect failed profile fetches.  (@djwatson)\n  - Fix stats.arenas.<i>.{dss,lg_dirty_mult,decay_time,pactive,pdirty} for\n    --disable-stats case.  (@jasone)\n\n* 4.0.4 (October 24, 2015)\n\n  This bugfix release fixes another xallocx() regression.  No other regressions\n  have come to light in over a month, so this is likely a good starting point\n  for people who prefer to wait for \"dot one\" releases with all the major issues\n  shaken out.\n\n  Bug fixes:\n  - Fix xallocx(..., MALLOCX_ZERO to zero the last full trailing page of large\n    allocations that have been randomly assigned an offset of 0 when\n    --enable-cache-oblivious configure option is enabled.\n\n* 4.0.3 (September 24, 2015)\n\n  This bugfix release continues the trend of xallocx() and heap profiling fixes.\n\n  Bug fixes:\n  - Fix xallocx(..., MALLOCX_ZERO) to zero all trailing bytes of large\n    allocations when --enable-cache-oblivious configure option is enabled.\n  - Fix xallocx(..., MALLOCX_ZERO) to zero trailing bytes of huge allocations\n    when resizing from/to a size class that is not a multiple of the chunk size.\n  - Fix prof_tctx_dump_iter() to filter out nodes that were created after heap\n    profile dumping started.\n  - Work around a potentially bad thread-specific data initialization\n    interaction with NPTL (glibc's pthreads implementation).\n\n* 4.0.2 (September 21, 2015)\n\n  This bugfix release addresses a few bugs specific to heap profiling.\n\n  Bug fixes:\n  - Fix ixallocx_prof_sample() to never modify nor create sampled small\n    allocations.  xallocx() is in general incapable of moving small allocations,\n    so this fix removes buggy code without loss of generality.\n  - Fix irallocx_prof_sample() to always allocate large regions, even when\n    alignment is non-zero.\n  - Fix prof_alloc_rollback() to read tdata from thread-specific data rather\n    than dereferencing a potentially invalid tctx.\n\n* 4.0.1 (September 15, 2015)\n\n  This is a bugfix release that is somewhat high risk due to the amount of\n  refactoring required to address deep xallocx() problems.  As a side effect of\n  these fixes, xallocx() now tries harder to partially fulfill requests for\n  optional extra space.  Note that a couple of minor heap profiling\n  optimizations are included, but these are better thought of as performance\n  fixes that were integral to disovering most of the other bugs.\n\n  Optimizations:\n  - Avoid a chunk metadata read in arena_prof_tctx_set(), since it is in the\n    fast path when heap profiling is enabled.  Additionally, split a special\n    case out into arena_prof_tctx_reset(), which also avoids chunk metadata\n    reads.\n  - Optimize irallocx_prof() to optimistically update the sampler state.  The\n    prior implementation appears to have been a holdover from when\n    rallocx()/xallocx() functionality was combined as rallocm().\n\n  Bug fixes:\n  - Fix TLS configuration such that it is enabled by default for platforms on\n    which it works correctly.\n  - Fix arenas_cache_cleanup() and arena_get_hard() to handle\n    allocation/deallocation within the application's thread-specific data\n    cleanup functions even after arenas_cache is torn down.\n  - Fix xallocx() bugs related to size+extra exceeding HUGE_MAXCLASS.\n  - Fix chunk purge hook calls for in-place huge shrinking reallocation to\n    specify the old chunk size rather than the new chunk size.  This bug caused\n    no correctness issues for the default chunk purge function, but was\n    visible to custom functions set via the \"arena.<i>.chunk_hooks\" mallctl.\n  - Fix heap profiling bugs:\n    + Fix heap profiling to distinguish among otherwise identical sample sites\n      with interposed resets (triggered via the \"prof.reset\" mallctl).  This bug\n      could cause data structure corruption that would most likely result in a\n      segfault.\n    + Fix irealloc_prof() to prof_alloc_rollback() on OOM.\n    + Make one call to prof_active_get_unlocked() per allocation event, and use\n      the result throughout the relevant functions that handle an allocation\n      event.  Also add a missing check in prof_realloc().  These fixes protect\n      allocation events against concurrent prof_active changes.\n    + Fix ixallocx_prof() to pass usize_max and zero to ixallocx_prof_sample()\n      in the correct order.\n    + Fix prof_realloc() to call prof_free_sampled_object() after calling\n      prof_malloc_sample_object().  Prior to this fix, if tctx and old_tctx were\n      the same, the tctx could have been prematurely destroyed.\n  - Fix portability bugs:\n    + Don't bitshift by negative amounts when encoding/decoding run sizes in\n      chunk header maps.  This affected systems with page sizes greater than 8\n      KiB.\n    + Rename index_t to szind_t to avoid an existing type on Solaris.\n    + Add JEMALLOC_CXX_THROW to the memalign() function prototype, in order to\n      match glibc and avoid compilation errors when including both\n      jemalloc/jemalloc.h and malloc.h in C++ code.\n    + Don't assume that /bin/sh is appropriate when running size_classes.sh\n      during configuration.\n    + Consider __sparcv9 a synonym for __sparc64__ when defining LG_QUANTUM.\n    + Link tests to librt if it contains clock_gettime(2).\n\n* 4.0.0 (August 17, 2015)\n\n  This version contains many speed and space optimizations, both minor and\n  major.  The major themes are generalization, unification, and simplification.\n  Although many of these optimizations cause no visible behavior change, their\n  cumulative effect is substantial.\n\n  New features:\n  - Normalize size class spacing to be consistent across the complete size\n    range.  By default there are four size classes per size doubling, but this\n    is now configurable via the --with-lg-size-class-group option.  Also add the\n    --with-lg-page, --with-lg-page-sizes, --with-lg-quantum, and\n    --with-lg-tiny-min options, which can be used to tweak page and size class\n    settings.  Impacts:\n    + Worst case performance for incrementally growing/shrinking reallocation\n      is improved because there are far fewer size classes, and therefore\n      copying happens less often.\n    + Internal fragmentation is limited to 20% for all but the smallest size\n      classes (those less than four times the quantum).  (1B + 4 KiB)\n      and (1B + 4 MiB) previously suffered nearly 50% internal fragmentation.\n    + Chunk fragmentation tends to be lower because there are fewer distinct run\n      sizes to pack.\n  - Add support for explicit tcaches.  The \"tcache.create\", \"tcache.flush\", and\n    \"tcache.destroy\" mallctls control tcache lifetime and flushing, and the\n    MALLOCX_TCACHE(tc) and MALLOCX_TCACHE_NONE flags to the *allocx() API\n    control which tcache is used for each operation.\n  - Implement per thread heap profiling, as well as the ability to\n    enable/disable heap profiling on a per thread basis.  Add the \"prof.reset\",\n    \"prof.lg_sample\", \"thread.prof.name\", \"thread.prof.active\",\n    \"opt.prof_thread_active_init\", \"prof.thread_active_init\", and\n    \"thread.prof.active\" mallctls.\n  - Add support for per arena application-specified chunk allocators, configured\n    via the \"arena.<i>.chunk_hooks\" mallctl.\n  - Refactor huge allocation to be managed by arenas, so that arenas now\n    function as general purpose independent allocators.  This is important in\n    the context of user-specified chunk allocators, aside from the scalability\n    benefits.  Related new statistics:\n    + The \"stats.arenas.<i>.huge.allocated\", \"stats.arenas.<i>.huge.nmalloc\",\n      \"stats.arenas.<i>.huge.ndalloc\", and \"stats.arenas.<i>.huge.nrequests\"\n      mallctls provide high level per arena huge allocation statistics.\n    + The \"arenas.nhchunks\", \"arenas.hchunk.<i>.size\",\n      \"stats.arenas.<i>.hchunks.<j>.nmalloc\",\n      \"stats.arenas.<i>.hchunks.<j>.ndalloc\",\n      \"stats.arenas.<i>.hchunks.<j>.nrequests\", and\n      \"stats.arenas.<i>.hchunks.<j>.curhchunks\" mallctls provide per size class\n      statistics.\n  - Add the 'util' column to malloc_stats_print() output, which reports the\n    proportion of available regions that are currently in use for each small\n    size class.\n  - Add \"alloc\" and \"free\" modes for for junk filling (see the \"opt.junk\"\n    mallctl), so that it is possible to separately enable junk filling for\n    allocation versus deallocation.\n  - Add the jemalloc-config script, which provides information about how\n    jemalloc was configured, and how to integrate it into application builds.\n  - Add metadata statistics, which are accessible via the \"stats.metadata\",\n    \"stats.arenas.<i>.metadata.mapped\", and\n    \"stats.arenas.<i>.metadata.allocated\" mallctls.\n  - Add the \"stats.resident\" mallctl, which reports the upper limit of\n    physically resident memory mapped by the allocator.\n  - Add per arena control over unused dirty page purging, via the\n    \"arenas.lg_dirty_mult\", \"arena.<i>.lg_dirty_mult\", and\n    \"stats.arenas.<i>.lg_dirty_mult\" mallctls.\n  - Add the \"prof.gdump\" mallctl, which makes it possible to toggle the gdump\n    feature on/off during program execution.\n  - Add sdallocx(), which implements sized deallocation.  The primary\n    optimization over dallocx() is the removal of a metadata read, which often\n    suffers an L1 cache miss.\n  - Add missing header includes in jemalloc/jemalloc.h, so that applications\n    only have to #include <jemalloc/jemalloc.h>.\n  - Add support for additional platforms:\n    + Bitrig\n    + Cygwin\n    + DragonFlyBSD\n    + iOS\n    + OpenBSD\n    + OpenRISC/or1k\n\n  Optimizations:\n  - Maintain dirty runs in per arena LRUs rather than in per arena trees of\n    dirty-run-containing chunks.  In practice this change significantly reduces\n    dirty page purging volume.\n  - Integrate whole chunks into the unused dirty page purging machinery.  This\n    reduces the cost of repeated huge allocation/deallocation, because it\n    effectively introduces a cache of chunks.\n  - Split the arena chunk map into two separate arrays, in order to increase\n    cache locality for the frequently accessed bits.\n  - Move small run metadata out of runs, into arena chunk headers.  This reduces\n    run fragmentation, smaller runs reduce external fragmentation for small size\n    classes, and packed (less uniformly aligned) metadata layout improves CPU\n    cache set distribution.\n  - Randomly distribute large allocation base pointer alignment relative to page\n    boundaries in order to more uniformly utilize CPU cache sets.  This can be\n    disabled via the --disable-cache-oblivious configure option, and queried via\n    the \"config.cache_oblivious\" mallctl.\n  - Micro-optimize the fast paths for the public API functions.\n  - Refactor thread-specific data to reside in a single structure.  This assures\n    that only a single TLS read is necessary per call into the public API.\n  - Implement in-place huge allocation growing and shrinking.\n  - Refactor rtree (radix tree for chunk lookups) to be lock-free, and make\n    additional optimizations that reduce maximum lookup depth to one or two\n    levels.  This resolves what was a concurrency bottleneck for per arena huge\n    allocation, because a global data structure is critical for determining\n    which arenas own which huge allocations.\n\n  Incompatible changes:\n  - Replace --enable-cc-silence with --disable-cc-silence to suppress spurious\n    warnings by default.\n  - Assure that the constness of malloc_usable_size()'s return type matches that\n    of the system implementation.\n  - Change the heap profile dump format to support per thread heap profiling,\n    rename pprof to jeprof, and enhance it with the --thread=<n> option.  As a\n    result, the bundled jeprof must now be used rather than the upstream\n    (gperftools) pprof.\n  - Disable \"opt.prof_final\" by default, in order to avoid atexit(3), which can\n    internally deadlock on some platforms.\n  - Change the \"arenas.nlruns\" mallctl type from size_t to unsigned.\n  - Replace the \"stats.arenas.<i>.bins.<j>.allocated\" mallctl with\n    \"stats.arenas.<i>.bins.<j>.curregs\".\n  - Ignore MALLOC_CONF in set{uid,gid,cap} binaries.\n  - Ignore MALLOCX_ARENA(a) in dallocx(), in favor of using the\n    MALLOCX_TCACHE(tc) and MALLOCX_TCACHE_NONE flags to control tcache usage.\n\n  Removed features:\n  - Remove the *allocm() API, which is superseded by the *allocx() API.\n  - Remove the --enable-dss options, and make dss non-optional on all platforms\n    which support sbrk(2).\n  - Remove the \"arenas.purge\" mallctl, which was obsoleted by the\n    \"arena.<i>.purge\" mallctl in 3.1.0.\n  - Remove the unnecessary \"opt.valgrind\" mallctl; jemalloc automatically\n    detects whether it is running inside Valgrind.\n  - Remove the \"stats.huge.allocated\", \"stats.huge.nmalloc\", and\n    \"stats.huge.ndalloc\" mallctls.\n  - Remove the --enable-mremap option.\n  - Remove the \"stats.chunks.current\", \"stats.chunks.total\", and\n    \"stats.chunks.high\" mallctls.\n\n  Bug fixes:\n  - Fix the cactive statistic to decrease (rather than increase) when active\n    memory decreases.  This regression was first released in 3.5.0.\n  - Fix OOM handling in memalign() and valloc().  A variant of this bug existed\n    in all releases since 2.0.0, which introduced these functions.\n  - Fix an OOM-related regression in arena_tcache_fill_small(), which could\n    cause cache corruption on OOM.  This regression was present in all releases\n    from 2.2.0 through 3.6.0.\n  - Fix size class overflow handling for malloc(), posix_memalign(), memalign(),\n    calloc(), and realloc() when profiling is enabled.\n  - Fix the \"arena.<i>.dss\" mallctl to return an error if \"primary\" or\n    \"secondary\" precedence is specified, but sbrk(2) is not supported.\n  - Fix fallback lg_floor() implementations to handle extremely large inputs.\n  - Ensure the default purgeable zone is after the default zone on OS X.\n  - Fix latent bugs in atomic_*().\n  - Fix the \"arena.<i>.dss\" mallctl to handle read-only calls.\n  - Fix tls_model configuration to enable the initial-exec model when possible.\n  - Mark malloc_conf as a weak symbol so that the application can override it.\n  - Correctly detect glibc's adaptive pthread mutexes.\n  - Fix the --without-export configure option.\n\n* 3.6.0 (March 31, 2014)\n\n  This version contains a critical bug fix for a regression present in 3.5.0 and\n  3.5.1.\n\n  Bug fixes:\n  - Fix a regression in arena_chunk_alloc() that caused crashes during\n    small/large allocation if chunk allocation failed.  In the absence of this\n    bug, chunk allocation failure would result in allocation failure, e.g.  NULL\n    return from malloc().  This regression was introduced in 3.5.0.\n  - Fix backtracing for gcc intrinsics-based backtracing by specifying\n    -fno-omit-frame-pointer to gcc.  Note that the application (and all the\n    libraries it links to) must also be compiled with this option for\n    backtracing to be reliable.\n  - Use dss allocation precedence for huge allocations as well as small/large\n    allocations.\n  - Fix test assertion failure message formatting.  This bug did not manifest on\n    x86_64 systems because of implementation subtleties in va_list.\n  - Fix inconsequential test failures for hash and SFMT code.\n\n  New features:\n  - Support heap profiling on FreeBSD.  This feature depends on the proc\n    filesystem being mounted during heap profile dumping.\n\n* 3.5.1 (February 25, 2014)\n\n  This version primarily addresses minor bugs in test code.\n\n  Bug fixes:\n  - Configure Solaris/Illumos to use MADV_FREE.\n  - Fix junk filling for mremap(2)-based huge reallocation.  This is only\n    relevant if configuring with the --enable-mremap option specified.\n  - Avoid compilation failure if 'restrict' C99 keyword is not supported by the\n    compiler.\n  - Add a configure test for SSE2 rather than assuming it is usable on i686\n    systems.  This fixes test compilation errors, especially on 32-bit Linux\n    systems.\n  - Fix mallctl argument size mismatches (size_t vs. uint64_t) in the stats unit\n    test.\n  - Fix/remove flawed alignment-related overflow tests.\n  - Prevent compiler optimizations that could change backtraces in the\n    prof_accum unit test.\n\n* 3.5.0 (January 22, 2014)\n\n  This version focuses on refactoring and automated testing, though it also\n  includes some non-trivial heap profiling optimizations not mentioned below.\n\n  New features:\n  - Add the *allocx() API, which is a successor to the experimental *allocm()\n    API.  The *allocx() functions are slightly simpler to use because they have\n    fewer parameters, they directly return the results of primary interest, and\n    mallocx()/rallocx() avoid the strict aliasing pitfall that\n    allocm()/rallocm() share with posix_memalign().  Note that *allocm() is\n    slated for removal in the next non-bugfix release.\n  - Add support for LinuxThreads.\n\n  Bug fixes:\n  - Unless heap profiling is enabled, disable floating point code and don't link\n    with libm.  This, in combination with e.g. EXTRA_CFLAGS=-mno-sse on x64\n    systems, makes it possible to completely disable floating point register\n    use.  Some versions of glibc neglect to save/restore caller-saved floating\n    point registers during dynamic lazy symbol loading, and the symbol loading\n    code uses whatever malloc the application happens to have linked/loaded\n    with, the result being potential floating point register corruption.\n  - Report ENOMEM rather than EINVAL if an OOM occurs during heap profiling\n    backtrace creation in imemalign().  This bug impacted posix_memalign() and\n    aligned_alloc().\n  - Fix a file descriptor leak in a prof_dump_maps() error path.\n  - Fix prof_dump() to close the dump file descriptor for all relevant error\n    paths.\n  - Fix rallocm() to use the arena specified by the ALLOCM_ARENA(s) flag for\n    allocation, not just deallocation.\n  - Fix a data race for large allocation stats counters.\n  - Fix a potential infinite loop during thread exit.  This bug occurred on\n    Solaris, and could affect other platforms with similar pthreads TSD\n    implementations.\n  - Don't junk-fill reallocations unless usable size changes.  This fixes a\n    violation of the *allocx()/*allocm() semantics.\n  - Fix growing large reallocation to junk fill new space.\n  - Fix huge deallocation to junk fill when munmap is disabled.\n  - Change the default private namespace prefix from empty to je_, and change\n    --with-private-namespace-prefix so that it prepends an additional prefix\n    rather than replacing je_.  This reduces the likelihood of applications\n    which statically link jemalloc experiencing symbol name collisions.\n  - Add missing private namespace mangling (relevant when\n    --with-private-namespace is specified).\n  - Add and use JEMALLOC_INLINE_C so that static inline functions are marked as\n    static even for debug builds.\n  - Add a missing mutex unlock in a malloc_init_hard() error path.  In practice\n    this error path is never executed.\n  - Fix numerous bugs in malloc_strotumax() error handling/reporting.  These\n    bugs had no impact except for malformed inputs.\n  - Fix numerous bugs in malloc_snprintf().  These bugs were not exercised by\n    existing calls, so they had no impact.\n\n* 3.4.1 (October 20, 2013)\n\n  Bug fixes:\n  - Fix a race in the \"arenas.extend\" mallctl that could cause memory corruption\n    of internal data structures and subsequent crashes.\n  - Fix Valgrind integration flaws that caused Valgrind warnings about reads of\n    uninitialized memory in:\n    + arena chunk headers\n    + internal zero-initialized data structures (relevant to tcache and prof\n      code)\n  - Preserve errno during the first allocation.  A readlink(2) call during\n    initialization fails unless /etc/malloc.conf exists, so errno was typically\n    set during the first allocation prior to this fix.\n  - Fix compilation warnings reported by gcc 4.8.1.\n\n* 3.4.0 (June 2, 2013)\n\n  This version is essentially a small bugfix release, but the addition of\n  aarch64 support requires that the minor version be incremented.\n\n  Bug fixes:\n  - Fix race-triggered deadlocks in chunk_record().  These deadlocks were\n    typically triggered by multiple threads concurrently deallocating huge\n    objects.\n\n  New features:\n  - Add support for the aarch64 architecture.\n\n* 3.3.1 (March 6, 2013)\n\n  This version fixes bugs that are typically encountered only when utilizing\n  custom run-time options.\n\n  Bug fixes:\n  - Fix a locking order bug that could cause deadlock during fork if heap\n    profiling were enabled.\n  - Fix a chunk recycling bug that could cause the allocator to lose track of\n    whether a chunk was zeroed.  On FreeBSD, NetBSD, and OS X, it could cause\n    corruption if allocating via sbrk(2) (unlikely unless running with the\n    \"dss:primary\" option specified).  This was completely harmless on Linux\n    unless using mlockall(2) (and unlikely even then, unless the\n    --disable-munmap configure option or the \"dss:primary\" option was\n    specified).  This regression was introduced in 3.1.0 by the\n    mlockall(2)/madvise(2) interaction fix.\n  - Fix TLS-related memory corruption that could occur during thread exit if the\n    thread never allocated memory.  Only the quarantine and prof facilities were\n    susceptible.\n  - Fix two quarantine bugs:\n    + Internal reallocation of the quarantined object array leaked the old\n      array.\n    + Reallocation failure for internal reallocation of the quarantined object\n      array (very unlikely) resulted in memory corruption.\n  - Fix Valgrind integration to annotate all internally allocated memory in a\n    way that keeps Valgrind happy about internal data structure access.\n  - Fix building for s390 systems.\n\n* 3.3.0 (January 23, 2013)\n\n  This version includes a few minor performance improvements in addition to the\n  listed new features and bug fixes.\n\n  New features:\n  - Add clipping support to lg_chunk option processing.\n  - Add the --enable-ivsalloc option.\n  - Add the --without-export option.\n  - Add the --disable-zone-allocator option.\n\n  Bug fixes:\n  - Fix \"arenas.extend\" mallctl to output the number of arenas.\n  - Fix chunk_recycle() to unconditionally inform Valgrind that returned memory\n    is undefined.\n  - Fix build break on FreeBSD related to alloca.h.\n\n* 3.2.0 (November 9, 2012)\n\n  In addition to a couple of bug fixes, this version modifies page run\n  allocation and dirty page purging algorithms in order to better control\n  page-level virtual memory fragmentation.\n\n  Incompatible changes:\n  - Change the \"opt.lg_dirty_mult\" default from 5 to 3 (32:1 to 8:1).\n\n  Bug fixes:\n  - Fix dss/mmap allocation precedence code to use recyclable mmap memory only\n    after primary dss allocation fails.\n  - Fix deadlock in the \"arenas.purge\" mallctl.  This regression was introduced\n    in 3.1.0 by the addition of the \"arena.<i>.purge\" mallctl.\n\n* 3.1.0 (October 16, 2012)\n\n  New features:\n  - Auto-detect whether running inside Valgrind, thus removing the need to\n    manually specify MALLOC_CONF=valgrind:true.\n  - Add the \"arenas.extend\" mallctl, which allows applications to create\n    manually managed arenas.\n  - Add the ALLOCM_ARENA() flag for {,r,d}allocm().\n  - Add the \"opt.dss\", \"arena.<i>.dss\", and \"stats.arenas.<i>.dss\" mallctls,\n    which provide control over dss/mmap precedence.\n  - Add the \"arena.<i>.purge\" mallctl, which obsoletes \"arenas.purge\".\n  - Define LG_QUANTUM for hppa.\n\n  Incompatible changes:\n  - Disable tcache by default if running inside Valgrind, in order to avoid\n    making unallocated objects appear reachable to Valgrind.\n  - Drop const from malloc_usable_size() argument on Linux.\n\n  Bug fixes:\n  - Fix heap profiling crash if sampled object is freed via realloc(p, 0).\n  - Remove const from __*_hook variable declarations, so that glibc can modify\n    them during process forking.\n  - Fix mlockall(2)/madvise(2) interaction.\n  - Fix fork(2)-related deadlocks.\n  - Fix error return value for \"thread.tcache.enabled\" mallctl.\n\n* 3.0.0 (May 11, 2012)\n\n  Although this version adds some major new features, the primary focus is on\n  internal code cleanup that facilitates maintainability and portability, most\n  of which is not reflected in the ChangeLog.  This is the first release to\n  incorporate substantial contributions from numerous other developers, and the\n  result is a more broadly useful allocator (see the git revision history for\n  contribution details).  Note that the license has been unified, thanks to\n  Facebook granting a license under the same terms as the other copyright\n  holders (see COPYING).\n\n  New features:\n  - Implement Valgrind support, redzones, and quarantine.\n  - Add support for additional platforms:\n    + FreeBSD\n    + Mac OS X Lion\n    + MinGW\n    + Windows (no support yet for replacing the system malloc)\n  - Add support for additional architectures:\n    + MIPS\n    + SH4\n    + Tilera\n  - Add support for cross compiling.\n  - Add nallocm(), which rounds a request size up to the nearest size class\n    without actually allocating.\n  - Implement aligned_alloc() (blame C11).\n  - Add the \"thread.tcache.enabled\" mallctl.\n  - Add the \"opt.prof_final\" mallctl.\n  - Update pprof (from gperftools 2.0).\n  - Add the --with-mangling option.\n  - Add the --disable-experimental option.\n  - Add the --disable-munmap option, and make it the default on Linux.\n  - Add the --enable-mremap option, which disables use of mremap(2) by default.\n\n  Incompatible changes:\n  - Enable stats by default.\n  - Enable fill by default.\n  - Disable lazy locking by default.\n  - Rename the \"tcache.flush\" mallctl to \"thread.tcache.flush\".\n  - Rename the \"arenas.pagesize\" mallctl to \"arenas.page\".\n  - Change the \"opt.lg_prof_sample\" default from 0 to 19 (1 B to 512 KiB).\n  - Change the \"opt.prof_accum\" default from true to false.\n\n  Removed features:\n  - Remove the swap feature, including the \"config.swap\", \"swap.avail\",\n    \"swap.prezeroed\", \"swap.nfds\", and \"swap.fds\" mallctls.\n  - Remove highruns statistics, including the\n    \"stats.arenas.<i>.bins.<j>.highruns\" and\n    \"stats.arenas.<i>.lruns.<j>.highruns\" mallctls.\n  - As part of small size class refactoring, remove the \"opt.lg_[qc]space_max\",\n    \"arenas.cacheline\", \"arenas.subpage\", \"arenas.[tqcs]space_{min,max}\", and\n    \"arenas.[tqcs]bins\" mallctls.\n  - Remove the \"arenas.chunksize\" mallctl.\n  - Remove the \"opt.lg_prof_tcmax\" option.\n  - Remove the \"opt.lg_prof_bt_max\" option.\n  - Remove the \"opt.lg_tcache_gc_sweep\" option.\n  - Remove the --disable-tiny option, including the \"config.tiny\" mallctl.\n  - Remove the --enable-dynamic-page-shift configure option.\n  - Remove the --enable-sysv configure option.\n\n  Bug fixes:\n  - Fix a statistics-related bug in the \"thread.arena\" mallctl that could cause\n    invalid statistics and crashes.\n  - Work around TLS deallocation via free() on Linux.  This bug could cause\n    write-after-free memory corruption.\n  - Fix a potential deadlock that could occur during interval- and\n    growth-triggered heap profile dumps.\n  - Fix large calloc() zeroing bugs due to dropping chunk map unzeroed flags.\n  - Fix chunk_alloc_dss() to stop claiming memory is zeroed.  This bug could\n    cause memory corruption and crashes with --enable-dss specified.\n  - Fix fork-related bugs that could cause deadlock in children between fork\n    and exec.\n  - Fix malloc_stats_print() to honor 'b' and 'l' in the opts parameter.\n  - Fix realloc(p, 0) to act like free(p).\n  - Do not enforce minimum alignment in memalign().\n  - Check for NULL pointer in malloc_usable_size().\n  - Fix an off-by-one heap profile statistics bug that could be observed in\n    interval- and growth-triggered heap profiles.\n  - Fix the \"epoch\" mallctl to update cached stats even if the passed in epoch\n    is 0.\n  - Fix bin->runcur management to fix a layout policy bug.  This bug did not\n    affect correctness.\n  - Fix a bug in choose_arena_hard() that potentially caused more arenas to be\n    initialized than necessary.\n  - Add missing \"opt.lg_tcache_max\" mallctl implementation.\n  - Use glibc allocator hooks to make mixed allocator usage less likely.\n  - Fix build issues for --disable-tcache.\n  - Don't mangle pthread_create() when --with-private-namespace is specified.\n\n* 2.2.5 (November 14, 2011)\n\n  Bug fixes:\n  - Fix huge_ralloc() race when using mremap(2).  This is a serious bug that\n    could cause memory corruption and/or crashes.\n  - Fix huge_ralloc() to maintain chunk statistics.\n  - Fix malloc_stats_print(..., \"a\") output.\n\n* 2.2.4 (November 5, 2011)\n\n  Bug fixes:\n  - Initialize arenas_tsd before using it.  This bug existed for 2.2.[0-3], as\n    well as for --disable-tls builds in earlier releases.\n  - Do not assume a 4 KiB page size in test/rallocm.c.\n\n* 2.2.3 (August 31, 2011)\n\n  This version fixes numerous bugs related to heap profiling.\n\n  Bug fixes:\n  - Fix a prof-related race condition.  This bug could cause memory corruption,\n    but only occurred in non-default configurations (prof_accum:false).\n  - Fix off-by-one backtracing issues (make sure that prof_alloc_prep() is\n    excluded from backtraces).\n  - Fix a prof-related bug in realloc() (only triggered by OOM errors).\n  - Fix prof-related bugs in allocm() and rallocm().\n  - Fix prof_tdata_cleanup() for --disable-tls builds.\n  - Fix a relative include path, to fix objdir builds.\n\n* 2.2.2 (July 30, 2011)\n\n  Bug fixes:\n  - Fix a build error for --disable-tcache.\n  - Fix assertions in arena_purge() (for real this time).\n  - Add the --with-private-namespace option.  This is a workaround for symbol\n    conflicts that can inadvertently arise when using static libraries.\n\n* 2.2.1 (March 30, 2011)\n\n  Bug fixes:\n  - Implement atomic operations for x86/x64.  This fixes compilation failures\n    for versions of gcc that are still in wide use.\n  - Fix an assertion in arena_purge().\n\n* 2.2.0 (March 22, 2011)\n\n  This version incorporates several improvements to algorithms and data\n  structures that tend to reduce fragmentation and increase speed.\n\n  New features:\n  - Add the \"stats.cactive\" mallctl.\n  - Update pprof (from google-perftools 1.7).\n  - Improve backtracing-related configuration logic, and add the\n    --disable-prof-libgcc option.\n\n  Bug fixes:\n  - Change default symbol visibility from \"internal\", to \"hidden\", which\n    decreases the overhead of library-internal function calls.\n  - Fix symbol visibility so that it is also set on OS X.\n  - Fix a build dependency regression caused by the introduction of the .pic.o\n    suffix for PIC object files.\n  - Add missing checks for mutex initialization failures.\n  - Don't use libgcc-based backtracing except on x64, where it is known to work.\n  - Fix deadlocks on OS X that were due to memory allocation in\n    pthread_mutex_lock().\n  - Heap profiling-specific fixes:\n    + Fix memory corruption due to integer overflow in small region index\n      computation, when using a small enough sample interval that profiling\n      context pointers are stored in small run headers.\n    + Fix a bootstrap ordering bug that only occurred with TLS disabled.\n    + Fix a rallocm() rsize bug.\n    + Fix error detection bugs for aligned memory allocation.\n\n* 2.1.3 (March 14, 2011)\n\n  Bug fixes:\n  - Fix a cpp logic regression (due to the \"thread.{de,}allocatedp\" mallctl fix\n    for OS X in 2.1.2).\n  - Fix a \"thread.arena\" mallctl bug.\n  - Fix a thread cache stats merging bug.\n\n* 2.1.2 (March 2, 2011)\n\n  Bug fixes:\n  - Fix \"thread.{de,}allocatedp\" mallctl for OS X.\n  - Add missing jemalloc.a to build system.\n\n* 2.1.1 (January 31, 2011)\n\n  Bug fixes:\n  - Fix aligned huge reallocation (affected allocm()).\n  - Fix the ALLOCM_LG_ALIGN macro definition.\n  - Fix a heap dumping deadlock.\n  - Fix a \"thread.arena\" mallctl bug.\n\n* 2.1.0 (December 3, 2010)\n\n  This version incorporates some optimizations that can't quite be considered\n  bug fixes.\n\n  New features:\n  - Use Linux's mremap(2) for huge object reallocation when possible.\n  - Avoid locking in mallctl*() when possible.\n  - Add the \"thread.[de]allocatedp\" mallctl's.\n  - Convert the manual page source from roff to DocBook, and generate both roff\n    and HTML manuals.\n\n  Bug fixes:\n  - Fix a crash due to incorrect bootstrap ordering.  This only impacted\n    --enable-debug --enable-dss configurations.\n  - Fix a minor statistics bug for mallctl(\"swap.avail\", ...).\n\n* 2.0.1 (October 29, 2010)\n\n  Bug fixes:\n  - Fix a race condition in heap profiling that could cause undefined behavior\n    if \"opt.prof_accum\" were disabled.\n  - Add missing mutex unlocks for some OOM error paths in the heap profiling\n    code.\n  - Fix a compilation error for non-C99 builds.\n\n* 2.0.0 (October 24, 2010)\n\n  This version focuses on the experimental *allocm() API, and on improved\n  run-time configuration/introspection.  Nonetheless, numerous performance\n  improvements are also included.\n\n  New features:\n  - Implement the experimental {,r,s,d}allocm() API, which provides a superset\n    of the functionality available via malloc(), calloc(), posix_memalign(),\n    realloc(), malloc_usable_size(), and free().  These functions can be used to\n    allocate/reallocate aligned zeroed memory, ask for optional extra memory\n    during reallocation, prevent object movement during reallocation, etc.\n  - Replace JEMALLOC_OPTIONS/JEMALLOC_PROF_PREFIX with MALLOC_CONF, which is\n    more human-readable, and more flexible.  For example:\n      JEMALLOC_OPTIONS=AJP\n    is now:\n      MALLOC_CONF=abort:true,fill:true,stats_print:true\n  - Port to Apple OS X.  Sponsored by Mozilla.\n  - Make it possible for the application to control thread-->arena mappings via\n    the \"thread.arena\" mallctl.\n  - Add compile-time support for all TLS-related functionality via pthreads TSD.\n    This is mainly of interest for OS X, which does not support TLS, but has a\n    TSD implementation with similar performance.\n  - Override memalign() and valloc() if they are provided by the system.\n  - Add the \"arenas.purge\" mallctl, which can be used to synchronously purge all\n    dirty unused pages.\n  - Make cumulative heap profiling data optional, so that it is possible to\n    limit the amount of memory consumed by heap profiling data structures.\n  - Add per thread allocation counters that can be accessed via the\n    \"thread.allocated\" and \"thread.deallocated\" mallctls.\n\n  Incompatible changes:\n  - Remove JEMALLOC_OPTIONS and malloc_options (see MALLOC_CONF above).\n  - Increase default backtrace depth from 4 to 128 for heap profiling.\n  - Disable interval-based profile dumps by default.\n\n  Bug fixes:\n  - Remove bad assertions in fork handler functions.  These assertions could\n    cause aborts for some combinations of configure settings.\n  - Fix strerror_r() usage to deal with non-standard semantics in GNU libc.\n  - Fix leak context reporting.  This bug tended to cause the number of contexts\n    to be underreported (though the reported number of objects and bytes were\n    correct).\n  - Fix a realloc() bug for large in-place growing reallocation.  This bug could\n    cause memory corruption, but it was hard to trigger.\n  - Fix an allocation bug for small allocations that could be triggered if\n    multiple threads raced to create a new run of backing pages.\n  - Enhance the heap profiler to trigger samples based on usable size, rather\n    than request size.\n  - Fix a heap profiling bug due to sometimes losing track of requested object\n    size for sampled objects.\n\n* 1.0.3 (August 12, 2010)\n\n  Bug fixes:\n  - Fix the libunwind-based implementation of stack backtracing (used for heap\n    profiling).  This bug could cause zero-length backtraces to be reported.\n  - Add a missing mutex unlock in library initialization code.  If multiple\n    threads raced to initialize malloc, some of them could end up permanently\n    blocked.\n\n* 1.0.2 (May 11, 2010)\n\n  Bug fixes:\n  - Fix junk filling of large objects, which could cause memory corruption.\n  - Add MAP_NORESERVE support for chunk mapping, because otherwise virtual\n    memory limits could cause swap file configuration to fail.  Contributed by\n    Jordan DeLong.\n\n* 1.0.1 (April 14, 2010)\n\n  Bug fixes:\n  - Fix compilation when --enable-fill is specified.\n  - Fix threads-related profiling bugs that affected accuracy and caused memory\n    to be leaked during thread exit.\n  - Fix dirty page purging race conditions that could cause crashes.\n  - Fix crash in tcache flushing code during thread destruction.\n\n* 1.0.0 (April 11, 2010)\n\n  This release focuses on speed and run-time introspection.  Numerous\n  algorithmic improvements make this release substantially faster than its\n  predecessors.\n\n  New features:\n  - Implement autoconf-based configuration system.\n  - Add mallctl*(), for the purposes of introspection and run-time\n    configuration.\n  - Make it possible for the application to manually flush a thread's cache, via\n    the \"tcache.flush\" mallctl.\n  - Base maximum dirty page count on proportion of active memory.\n  - Compute various additional run-time statistics, including per size class\n    statistics for large objects.\n  - Expose malloc_stats_print(), which can be called repeatedly by the\n    application.\n  - Simplify the malloc_message() signature to only take one string argument,\n    and incorporate an opaque data pointer argument for use by the application\n    in combination with malloc_stats_print().\n  - Add support for allocation backed by one or more swap files, and allow the\n    application to disable over-commit if swap files are in use.\n  - Implement allocation profiling and leak checking.\n\n  Removed features:\n  - Remove the dynamic arena rebalancing code, since thread-specific caching\n    reduces its utility.\n\n  Bug fixes:\n  - Modify chunk allocation to work when address space layout randomization\n    (ASLR) is in use.\n  - Fix thread cleanup bugs related to TLS destruction.\n  - Handle 0-size allocation requests in posix_memalign().\n  - Fix a chunk leak.  The leaked chunks were never touched, so this impacted\n    virtual memory usage, but not physical memory usage.\n\n* linux_2008082[78]a (August 27/28, 2008)\n\n  These snapshot releases are the simple result of incorporating Linux-specific\n  support into the FreeBSD malloc sources.\n\n--------------------------------------------------------------------------------\nvim:filetype=text:textwidth=80\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/INSTALL.md",
    "content": "Building and installing a packaged release of jemalloc can be as simple as\ntyping the following while in the root directory of the source tree:\n\n    ./configure\n    make\n    make install\n\nIf building from unpackaged developer sources, the simplest command sequence\nthat might work is:\n\n    ./autogen.sh\n    make dist\n    make\n    make install\n\nNote that documentation is not built by the default target because doing so\nwould create a dependency on xsltproc in packaged releases, hence the\nrequirement to either run 'make dist' or avoid installing docs via the various\ninstall_* targets documented below.\n\n\n## Advanced configuration\n\nThe 'configure' script supports numerous options that allow control of which\nfunctionality is enabled, where jemalloc is installed, etc.  Optionally, pass\nany of the following arguments (not a definitive list) to 'configure':\n\n* `--help`\n\n    Print a definitive list of options.\n\n* `--prefix=<install-root-dir>`\n\n    Set the base directory in which to install.  For example:\n\n        ./configure --prefix=/usr/local\n\n    will cause files to be installed into /usr/local/include, /usr/local/lib,\n    and /usr/local/man.\n\n* `--with-version=(<major>.<minor>.<bugfix>-<nrev>-g<gid>|VERSION)`\n\n    The VERSION file is mandatory for successful configuration, and the\n    following steps are taken to assure its presence:\n    1) If --with-version=<major>.<minor>.<bugfix>-<nrev>-g<gid> is specified,\n       generate VERSION using the specified value.\n    2) If --with-version is not specified in either form and the source\n       directory is inside a git repository, try to generate VERSION via 'git\n       describe' invocations that pattern-match release tags.\n    3) If VERSION is missing, generate it with a bogus version:\n       0.0.0-0-g0000000000000000000000000000000000000000\n\n    Note that --with-version=VERSION bypasses (1) and (2), which simplifies\n    VERSION configuration when embedding a jemalloc release into another\n    project's git repository.\n\n* `--with-rpath=<colon-separated-rpath>`\n\n    Embed one or more library paths, so that libjemalloc can find the libraries\n    it is linked to.  This works only on ELF-based systems.\n\n* `--with-mangling=<map>`\n\n    Mangle public symbols specified in <map> which is a comma-separated list of\n    name:mangled pairs.\n\n    For example, to use ld's --wrap option as an alternative method for\n    overriding libc's malloc implementation, specify something like:\n\n      --with-mangling=malloc:__wrap_malloc,free:__wrap_free[...]\n\n    Note that mangling happens prior to application of the prefix specified by\n    --with-jemalloc-prefix, and mangled symbols are then ignored when applying\n    the prefix.\n\n* `--with-jemalloc-prefix=<prefix>`\n\n    Prefix all public APIs with <prefix>.  For example, if <prefix> is\n    \"prefix_\", API changes like the following occur:\n\n      malloc()         --> prefix_malloc()\n      malloc_conf      --> prefix_malloc_conf\n      /etc/malloc.conf --> /etc/prefix_malloc.conf\n      MALLOC_CONF      --> PREFIX_MALLOC_CONF\n\n    This makes it possible to use jemalloc at the same time as the system\n    allocator, or even to use multiple copies of jemalloc simultaneously.\n\n    By default, the prefix is \"\", except on OS X, where it is \"je_\".  On OS X,\n    jemalloc overlays the default malloc zone, but makes no attempt to actually\n    replace the \"malloc\", \"calloc\", etc. symbols.\n\n* `--without-export`\n\n    Don't export public APIs.  This can be useful when building jemalloc as a\n    static library, or to avoid exporting public APIs when using the zone\n    allocator on OSX.\n\n* `--with-private-namespace=<prefix>`\n\n    Prefix all library-private APIs with <prefix>je_.  For shared libraries,\n    symbol visibility mechanisms prevent these symbols from being exported, but\n    for static libraries, naming collisions are a real possibility.  By\n    default, <prefix> is empty, which results in a symbol prefix of je_ .\n\n* `--with-install-suffix=<suffix>`\n\n    Append <suffix> to the base name of all installed files, such that multiple\n    versions of jemalloc can coexist in the same installation directory.  For\n    example, libjemalloc.so.0 becomes libjemalloc<suffix>.so.0.\n\n* `--with-malloc-conf=<malloc_conf>`\n\n    Embed `<malloc_conf>` as a run-time options string that is processed prior to\n    the malloc_conf global variable, the /etc/malloc.conf symlink, and the\n    MALLOC_CONF environment variable.  For example, to change the default decay\n    time to 30 seconds:\n\n      --with-malloc-conf=decay_ms:30000\n\n* `--enable-debug`\n\n    Enable assertions and validation code.  This incurs a substantial\n    performance hit, but is very useful during application development.\n\n* `--disable-stats`\n\n    Disable statistics gathering functionality.  See the \"opt.stats_print\"\n    option documentation for usage details.\n\n* `--enable-prof`\n\n    Enable heap profiling and leak detection functionality.  See the \"opt.prof\"\n    option documentation for usage details.  When enabled, there are several\n    approaches to backtracing, and the configure script chooses the first one\n    in the following list that appears to function correctly:\n\n    + libunwind      (requires --enable-prof-libunwind)\n    + libgcc         (unless --disable-prof-libgcc)\n    + gcc intrinsics (unless --disable-prof-gcc)\n\n* `--enable-prof-libunwind`\n\n    Use the libunwind library (http://www.nongnu.org/libunwind/) for stack\n    backtracing.\n\n* `--disable-prof-libgcc`\n\n    Disable the use of libgcc's backtracing functionality.\n\n* `--disable-prof-gcc`\n\n    Disable the use of gcc intrinsics for backtracing.\n\n* `--with-static-libunwind=<libunwind.a>`\n\n    Statically link against the specified libunwind.a rather than dynamically\n    linking with -lunwind.\n\n* `--disable-thp`\n\n    Disable transparent huge page (THP) integration.  This option can be useful\n    when cross compiling.\n\n* `--disable-fill`\n\n    Disable support for junk/zero filling of memory.  See the \"opt.junk\" and\n    \"opt.zero\" option documentation for usage details.\n\n* `--disable-zone-allocator`\n\n    Disable zone allocator for Darwin.  This means jemalloc won't be hooked as\n    the default allocator on OSX/iOS.\n\n* `--enable-utrace`\n\n    Enable utrace(2)-based allocation tracing.  This feature is not broadly\n    portable (FreeBSD has it, but Linux and OS X do not).\n\n* `--enable-xmalloc`\n\n    Enable support for optional immediate termination due to out-of-memory\n    errors, as is commonly implemented by \"xmalloc\" wrapper function for malloc.\n    See the \"opt.xmalloc\" option documentation for usage details.\n\n* `--enable-lazy-lock`\n\n    Enable code that wraps pthread_create() to detect when an application\n    switches from single-threaded to multi-threaded mode, so that it can avoid\n    mutex locking/unlocking operations while in single-threaded mode.  In\n    practice, this feature usually has little impact on performance unless\n    thread-specific caching is disabled.\n\n* `--disable-cache-oblivious`\n\n    Disable cache-oblivious large allocation alignment for large allocation\n    requests with no alignment constraints.  If this feature is disabled, all\n    large allocations are page-aligned as an implementation artifact, which can\n    severely harm CPU cache utilization.  However, the cache-oblivious layout\n    comes at the cost of one extra page per large allocation, which in the\n    most extreme case increases physical memory usage for the 16 KiB size class\n    to 20 KiB.\n\n* `--disable-syscall`\n\n    Disable use of syscall(2) rather than {open,read,write,close}(2).  This is\n    intended as a workaround for systems that place security limitations on\n    syscall(2).\n\n* `--disable-cxx`\n\n    Disable C++ integration.  This will cause new and delete operator\n    implementations to be omitted.\n\n* `--with-xslroot=<path>`\n\n    Specify where to find DocBook XSL stylesheets when building the\n    documentation.\n\n* `--with-lg-page=<lg-page>`\n\n    Specify the base 2 log of the allocator page size, which must in turn be at\n    least as large as the system page size.  By default the configure script\n    determines the host's page size and sets the allocator page size equal to\n    the system page size, so this option need not be specified unless the\n    system page size may change between configuration and execution, e.g. when\n    cross compiling.\n\n* `--with-lg-page-sizes=<lg-page-sizes>`\n\n    Specify the comma-separated base 2 logs of the page sizes to support.  This\n    option may be useful when cross compiling in combination with\n    `--with-lg-page`, but its primary use case is for integration with FreeBSD's\n    libc, wherein jemalloc is embedded.\n\n* `--with-lg-hugepage=<lg-hugepage>`\n\n    Specify the base 2 log of the system huge page size.  This option is useful\n    when cross compiling, or when overriding the default for systems that do\n    not explicitly support huge pages.\n\n* `--with-lg-quantum=<lg-quantum>`\n\n    Specify the base 2 log of the minimum allocation alignment.  jemalloc needs\n    to know the minimum alignment that meets the following C standard\n    requirement (quoted from the April 12, 2011 draft of the C11 standard):\n\n    >  The pointer returned if the allocation succeeds is suitably aligned so\n      that it may be assigned to a pointer to any type of object with a\n      fundamental alignment requirement and then used to access such an object\n      or an array of such objects in the space allocated [...]\n\n    This setting is architecture-specific, and although jemalloc includes known\n    safe values for the most commonly used modern architectures, there is a\n    wrinkle related to GNU libc (glibc) that may impact your choice of\n    <lg-quantum>.  On most modern architectures, this mandates 16-byte\n    alignment (<lg-quantum>=4), but the glibc developers chose not to meet this\n    requirement for performance reasons.  An old discussion can be found at\n    <https://sourceware.org/bugzilla/show_bug.cgi?id=206> .  Unlike glibc,\n    jemalloc does follow the C standard by default (caveat: jemalloc\n    technically cheats for size classes smaller than the quantum), but the fact\n    that Linux systems already work around this allocator noncompliance means\n    that it is generally safe in practice to let jemalloc's minimum alignment\n    follow glibc's lead.  If you specify `--with-lg-quantum=3` during\n    configuration, jemalloc will provide additional size classes that are not\n    16-byte-aligned (24, 40, and 56).\n\nThe following environment variables (not a definitive list) impact configure's\nbehavior:\n\n* `CFLAGS=\"?\"`\n* `CXXFLAGS=\"?\"`\n\n    Pass these flags to the C/C++ compiler.  Any flags set by the configure\n    script are prepended, which means explicitly set flags generally take\n    precedence.  Take care when specifying flags such as -Werror, because\n    configure tests may be affected in undesirable ways.\n\n* `EXTRA_CFLAGS=\"?\"`\n* `EXTRA_CXXFLAGS=\"?\"`\n\n    Append these flags to CFLAGS/CXXFLAGS, without passing them to the\n    compiler(s) during configuration.  This makes it possible to add flags such\n    as -Werror, while allowing the configure script to determine what other\n    flags are appropriate for the specified configuration.\n\n* `CPPFLAGS=\"?\"`\n\n    Pass these flags to the C preprocessor.  Note that CFLAGS is not passed to\n    'cpp' when 'configure' is looking for include files, so you must use\n    CPPFLAGS instead if you need to help 'configure' find header files.\n\n* `LD_LIBRARY_PATH=\"?\"`\n\n    'ld' uses this colon-separated list to find libraries.\n\n* `LDFLAGS=\"?\"`\n\n    Pass these flags when linking.\n\n* `PATH=\"?\"`\n\n    'configure' uses this to find programs.\n\nIn some cases it may be necessary to work around configuration results that do\nnot match reality.  For example, Linux 4.5 added support for the MADV_FREE flag\nto madvise(2), which can cause problems if building on a host with MADV_FREE\nsupport and deploying to a target without.  To work around this, use a cache\nfile to override the relevant configuration variable defined in configure.ac,\ne.g.:\n\n    echo \"je_cv_madv_free=no\" > config.cache && ./configure -C\n\n\n## Advanced compilation\n\nTo build only parts of jemalloc, use the following targets:\n\n    build_lib_shared\n    build_lib_static\n    build_lib\n    build_doc_html\n    build_doc_man\n    build_doc\n\nTo install only parts of jemalloc, use the following targets:\n\n    install_bin\n    install_include\n    install_lib_shared\n    install_lib_static\n    install_lib\n    install_doc_html\n    install_doc_man\n    install_doc\n\nTo clean up build results to varying degrees, use the following make targets:\n\n    clean\n    distclean\n    relclean\n\n\n## Advanced installation\n\nOptionally, define make variables when invoking make, including (not\nexclusively):\n\n* `INCLUDEDIR=\"?\"`\n\n    Use this as the installation prefix for header files.\n\n* `LIBDIR=\"?\"`\n\n    Use this as the installation prefix for libraries.\n\n* `MANDIR=\"?\"`\n\n    Use this as the installation prefix for man pages.\n\n* `DESTDIR=\"?\"`\n\n    Prepend DESTDIR to INCLUDEDIR, LIBDIR, DATADIR, and MANDIR.  This is useful\n    when installing to a different path than was specified via --prefix.\n\n* `CC=\"?\"`\n\n    Use this to invoke the C compiler.\n\n* `CFLAGS=\"?\"`\n\n    Pass these flags to the compiler.\n\n* `CPPFLAGS=\"?\"`\n\n    Pass these flags to the C preprocessor.\n\n* `LDFLAGS=\"?\"`\n\n    Pass these flags when linking.\n\n* `PATH=\"?\"`\n\n    Use this to search for programs used during configuration and building.\n\n\n## Development\n\nIf you intend to make non-trivial changes to jemalloc, use the 'autogen.sh'\nscript rather than 'configure'.  This re-generates 'configure', enables\nconfiguration dependency rules, and enables re-generation of automatically\ngenerated source files.\n\nThe build system supports using an object directory separate from the source\ntree.  For example, you can create an 'obj' directory, and from within that\ndirectory, issue configuration and build commands:\n\n    autoconf\n    mkdir obj\n    cd obj\n    ../configure --enable-autogen\n    make\n\n\n## Documentation\n\nThe manual page is generated in both html and roff formats.  Any web browser\ncan be used to view the html manual.  The roff manual page can be formatted\nprior to installation via the following command:\n\n    nroff -man -t doc/jemalloc.3\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/Makefile.in",
    "content": "# Clear out all vpaths, then set just one (default vpath) for the main build\n# directory.\nvpath\nvpath % .\n\n# Clear the default suffixes, so that built-in rules are not used.\n.SUFFIXES :\n\nSHELL := /bin/sh\n\nCC := @CC@\nCXX := @CXX@\n\n# Configuration parameters.\nDESTDIR =\nBINDIR := $(DESTDIR)@BINDIR@\nINCLUDEDIR := $(DESTDIR)@INCLUDEDIR@\nLIBDIR := $(DESTDIR)@LIBDIR@\nDATADIR := $(DESTDIR)@DATADIR@\nMANDIR := $(DESTDIR)@MANDIR@\nsrcroot := @srcroot@\nobjroot := @objroot@\nabs_srcroot := @abs_srcroot@\nabs_objroot := @abs_objroot@\n\n# Build parameters.\nCPPFLAGS := @CPPFLAGS@ -I$(srcroot)include -I$(objroot)include\nCONFIGURE_CFLAGS := @CONFIGURE_CFLAGS@\nSPECIFIED_CFLAGS := @SPECIFIED_CFLAGS@\nEXTRA_CFLAGS := @EXTRA_CFLAGS@\nCFLAGS := $(strip $(CONFIGURE_CFLAGS) $(SPECIFIED_CFLAGS) $(EXTRA_CFLAGS))\nCONFIGURE_CXXFLAGS := @CONFIGURE_CXXFLAGS@\nSPECIFIED_CXXFLAGS := @SPECIFIED_CXXFLAGS@\nEXTRA_CXXFLAGS := @EXTRA_CXXFLAGS@\nCXXFLAGS := $(strip $(CONFIGURE_CXXFLAGS) $(SPECIFIED_CXXFLAGS) $(EXTRA_CXXFLAGS))\nLDFLAGS := @LDFLAGS@\nEXTRA_LDFLAGS := @EXTRA_LDFLAGS@\nLIBS := @LIBS@\nRPATH_EXTRA := @RPATH_EXTRA@\nSO := @so@\nIMPORTLIB := @importlib@\nO := @o@\nA := @a@\nEXE := @exe@\nLIBPREFIX := @libprefix@\nREV := @rev@\ninstall_suffix := @install_suffix@\nABI := @abi@\nXSLTPROC := @XSLTPROC@\nAUTOCONF := @AUTOCONF@\n_RPATH = @RPATH@\nRPATH = $(if $(1),$(call _RPATH,$(1)))\ncfghdrs_in := $(addprefix $(srcroot),@cfghdrs_in@)\ncfghdrs_out := @cfghdrs_out@\ncfgoutputs_in := $(addprefix $(srcroot),@cfgoutputs_in@)\ncfgoutputs_out := @cfgoutputs_out@\nenable_autogen := @enable_autogen@\nenable_prof := @enable_prof@\nenable_zone_allocator := @enable_zone_allocator@\nMALLOC_CONF := @JEMALLOC_CPREFIX@MALLOC_CONF\nlink_whole_archive := @link_whole_archive@\nDSO_LDFLAGS = @DSO_LDFLAGS@\nSOREV = @SOREV@\nPIC_CFLAGS = @PIC_CFLAGS@\nCTARGET = @CTARGET@\nLDTARGET = @LDTARGET@\nTEST_LD_MODE = @TEST_LD_MODE@\nMKLIB = @MKLIB@\nAR = @AR@\nARFLAGS = @ARFLAGS@\nDUMP_SYMS = @DUMP_SYMS@\nAWK := @AWK@\nCC_MM = @CC_MM@\nLM := @LM@\nINSTALL = @INSTALL@\n\nifeq (macho, $(ABI))\nTEST_LIBRARY_PATH := DYLD_FALLBACK_LIBRARY_PATH=\"$(objroot)lib\"\nelse\nifeq (pecoff, $(ABI))\nTEST_LIBRARY_PATH := PATH=\"$(PATH):$(objroot)lib\"\nelse\nTEST_LIBRARY_PATH :=\nendif\nendif\n\nLIBJEMALLOC := $(LIBPREFIX)jemalloc$(install_suffix)\n\n# Lists of files.\nBINS := $(objroot)bin/jemalloc-config $(objroot)bin/jemalloc.sh $(objroot)bin/jeprof\nC_HDRS := $(objroot)include/jemalloc/jemalloc$(install_suffix).h\nC_SRCS := $(srcroot)src/jemalloc.c \\\n\t$(srcroot)src/arena.c \\\n\t$(srcroot)src/background_thread.c \\\n\t$(srcroot)src/base.c \\\n\t$(srcroot)src/bitmap.c \\\n\t$(srcroot)src/ckh.c \\\n\t$(srcroot)src/ctl.c \\\n\t$(srcroot)src/extent.c \\\n\t$(srcroot)src/extent_dss.c \\\n\t$(srcroot)src/extent_mmap.c \\\n\t$(srcroot)src/hash.c \\\n\t$(srcroot)src/hooks.c \\\n\t$(srcroot)src/large.c \\\n\t$(srcroot)src/malloc_io.c \\\n\t$(srcroot)src/mutex.c \\\n\t$(srcroot)src/mutex_pool.c \\\n\t$(srcroot)src/nstime.c \\\n\t$(srcroot)src/pages.c \\\n\t$(srcroot)src/prng.c \\\n\t$(srcroot)src/prof.c \\\n\t$(srcroot)src/rtree.c \\\n\t$(srcroot)src/stats.c \\\n\t$(srcroot)src/spin.c \\\n\t$(srcroot)src/sz.c \\\n\t$(srcroot)src/tcache.c \\\n\t$(srcroot)src/ticker.c \\\n\t$(srcroot)src/tsd.c \\\n\t$(srcroot)src/witness.c\nifeq ($(enable_zone_allocator), 1)\nC_SRCS += $(srcroot)src/zone.c\nendif\nifeq ($(IMPORTLIB),$(SO))\nSTATIC_LIBS := $(objroot)lib/$(LIBJEMALLOC).$(A)\nendif\nifdef PIC_CFLAGS\nSTATIC_LIBS += $(objroot)lib/$(LIBJEMALLOC)_pic.$(A)\nelse\nSTATIC_LIBS += $(objroot)lib/$(LIBJEMALLOC)_s.$(A)\nendif\nDSOS := $(objroot)lib/$(LIBJEMALLOC).$(SOREV)\nifneq ($(SOREV),$(SO))\nDSOS += $(objroot)lib/$(LIBJEMALLOC).$(SO)\nendif\nifeq (1, $(link_whole_archive))\nLJEMALLOC := -Wl,--whole-archive -L$(objroot)lib -l$(LIBJEMALLOC) -Wl,--no-whole-archive\nelse\nLJEMALLOC := $(objroot)lib/$(LIBJEMALLOC).$(IMPORTLIB)\nendif\nPC := $(objroot)jemalloc.pc\nMAN3 := $(objroot)doc/jemalloc$(install_suffix).3\nDOCS_XML := $(objroot)doc/jemalloc$(install_suffix).xml\nDOCS_HTML := $(DOCS_XML:$(objroot)%.xml=$(objroot)%.html)\nDOCS_MAN3 := $(DOCS_XML:$(objroot)%.xml=$(objroot)%.3)\nDOCS := $(DOCS_HTML) $(DOCS_MAN3)\nC_TESTLIB_SRCS := $(srcroot)test/src/btalloc.c $(srcroot)test/src/btalloc_0.c \\\n\t$(srcroot)test/src/btalloc_1.c $(srcroot)test/src/math.c \\\n\t$(srcroot)test/src/mtx.c $(srcroot)test/src/mq.c \\\n\t$(srcroot)test/src/SFMT.c $(srcroot)test/src/test.c \\\n\t$(srcroot)test/src/thd.c $(srcroot)test/src/timer.c\nifeq (1, $(link_whole_archive))\nC_UTIL_INTEGRATION_SRCS :=\nC_UTIL_CPP_SRCS :=\nelse\nC_UTIL_INTEGRATION_SRCS := $(srcroot)src/nstime.c $(srcroot)src/malloc_io.c\nC_UTIL_CPP_SRCS := $(srcroot)src/nstime.c $(srcroot)src/malloc_io.c\nendif\nTESTS_UNIT := \\\n\t$(srcroot)test/unit/a0.c \\\n\t$(srcroot)test/unit/arena_reset.c \\\n\t$(srcroot)test/unit/atomic.c \\\n\t$(srcroot)test/unit/background_thread.c \\\n\t$(srcroot)test/unit/base.c \\\n\t$(srcroot)test/unit/bitmap.c \\\n\t$(srcroot)test/unit/ckh.c \\\n\t$(srcroot)test/unit/decay.c \\\n\t$(srcroot)test/unit/extent_quantize.c \\\n\t$(srcroot)test/unit/fork.c \\\n\t$(srcroot)test/unit/hash.c \\\n\t$(srcroot)test/unit/hooks.c \\\n\t$(srcroot)test/unit/junk.c \\\n\t$(srcroot)test/unit/junk_alloc.c \\\n\t$(srcroot)test/unit/junk_free.c \\\n\t$(srcroot)test/unit/mallctl.c \\\n\t$(srcroot)test/unit/malloc_io.c \\\n\t$(srcroot)test/unit/math.c \\\n\t$(srcroot)test/unit/mq.c \\\n\t$(srcroot)test/unit/mtx.c \\\n\t$(srcroot)test/unit/pack.c \\\n\t$(srcroot)test/unit/pages.c \\\n\t$(srcroot)test/unit/ph.c \\\n\t$(srcroot)test/unit/prng.c \\\n\t$(srcroot)test/unit/prof_accum.c \\\n\t$(srcroot)test/unit/prof_active.c \\\n\t$(srcroot)test/unit/prof_gdump.c \\\n\t$(srcroot)test/unit/prof_idump.c \\\n\t$(srcroot)test/unit/prof_reset.c \\\n\t$(srcroot)test/unit/prof_tctx.c \\\n\t$(srcroot)test/unit/prof_thread_name.c \\\n\t$(srcroot)test/unit/ql.c \\\n\t$(srcroot)test/unit/qr.c \\\n\t$(srcroot)test/unit/rb.c \\\n\t$(srcroot)test/unit/retained.c \\\n\t$(srcroot)test/unit/rtree.c \\\n\t$(srcroot)test/unit/SFMT.c \\\n\t$(srcroot)test/unit/size_classes.c \\\n\t$(srcroot)test/unit/slab.c \\\n\t$(srcroot)test/unit/smoothstep.c \\\n\t$(srcroot)test/unit/spin.c \\\n\t$(srcroot)test/unit/stats.c \\\n\t$(srcroot)test/unit/stats_print.c \\\n\t$(srcroot)test/unit/ticker.c \\\n\t$(srcroot)test/unit/nstime.c \\\n\t$(srcroot)test/unit/tsd.c \\\n\t$(srcroot)test/unit/witness.c \\\n\t$(srcroot)test/unit/zero.c\nifeq (@enable_prof@, 1)\nTESTS_UNIT += \\\n\t$(srcroot)test/unit/arena_reset_prof.c\nendif\nTESTS_INTEGRATION := $(srcroot)test/integration/aligned_alloc.c \\\n\t$(srcroot)test/integration/allocated.c \\\n\t$(srcroot)test/integration/extent.c \\\n\t$(srcroot)test/integration/mallocx.c \\\n\t$(srcroot)test/integration/MALLOCX_ARENA.c \\\n\t$(srcroot)test/integration/overflow.c \\\n\t$(srcroot)test/integration/posix_memalign.c \\\n\t$(srcroot)test/integration/rallocx.c \\\n\t$(srcroot)test/integration/sdallocx.c \\\n\t$(srcroot)test/integration/thread_arena.c \\\n\t$(srcroot)test/integration/thread_tcache_enabled.c \\\n\t$(srcroot)test/integration/xallocx.c\nifeq (@enable_cxx@, 1)\nCPP_SRCS := $(srcroot)src/jemalloc_cpp.cpp\nTESTS_INTEGRATION_CPP := $(srcroot)test/integration/cpp/basic.cpp\nelse\nCPP_SRCS :=\nTESTS_INTEGRATION_CPP :=\nendif\nTESTS_STRESS := $(srcroot)test/stress/microbench.c\n\nTESTS := $(TESTS_UNIT) $(TESTS_INTEGRATION) $(TESTS_INTEGRATION_CPP) $(TESTS_STRESS)\n\nPRIVATE_NAMESPACE_HDRS := $(objroot)include/jemalloc/internal/private_namespace.h $(objroot)include/jemalloc/internal/private_namespace_jet.h\nPRIVATE_NAMESPACE_GEN_HDRS := $(PRIVATE_NAMESPACE_HDRS:%.h=%.gen.h)\nC_SYM_OBJS := $(C_SRCS:$(srcroot)%.c=$(objroot)%.sym.$(O))\nC_SYMS := $(C_SRCS:$(srcroot)%.c=$(objroot)%.sym)\nC_OBJS := $(C_SRCS:$(srcroot)%.c=$(objroot)%.$(O))\nCPP_OBJS := $(CPP_SRCS:$(srcroot)%.cpp=$(objroot)%.$(O))\nC_PIC_OBJS := $(C_SRCS:$(srcroot)%.c=$(objroot)%.pic.$(O))\nCPP_PIC_OBJS := $(CPP_SRCS:$(srcroot)%.cpp=$(objroot)%.pic.$(O))\nC_JET_SYM_OBJS := $(C_SRCS:$(srcroot)%.c=$(objroot)%.jet.sym.$(O))\nC_JET_SYMS := $(C_SRCS:$(srcroot)%.c=$(objroot)%.jet.sym)\nC_JET_OBJS := $(C_SRCS:$(srcroot)%.c=$(objroot)%.jet.$(O))\nC_TESTLIB_UNIT_OBJS := $(C_TESTLIB_SRCS:$(srcroot)%.c=$(objroot)%.unit.$(O))\nC_TESTLIB_INTEGRATION_OBJS := $(C_TESTLIB_SRCS:$(srcroot)%.c=$(objroot)%.integration.$(O))\nC_UTIL_INTEGRATION_OBJS := $(C_UTIL_INTEGRATION_SRCS:$(srcroot)%.c=$(objroot)%.integration.$(O))\nC_TESTLIB_STRESS_OBJS := $(C_TESTLIB_SRCS:$(srcroot)%.c=$(objroot)%.stress.$(O))\nC_TESTLIB_OBJS := $(C_TESTLIB_UNIT_OBJS) $(C_TESTLIB_INTEGRATION_OBJS) $(C_UTIL_INTEGRATION_OBJS) $(C_TESTLIB_STRESS_OBJS)\n\nTESTS_UNIT_OBJS := $(TESTS_UNIT:$(srcroot)%.c=$(objroot)%.$(O))\nTESTS_INTEGRATION_OBJS := $(TESTS_INTEGRATION:$(srcroot)%.c=$(objroot)%.$(O))\nTESTS_INTEGRATION_CPP_OBJS := $(TESTS_INTEGRATION_CPP:$(srcroot)%.cpp=$(objroot)%.$(O))\nTESTS_STRESS_OBJS := $(TESTS_STRESS:$(srcroot)%.c=$(objroot)%.$(O))\nTESTS_OBJS := $(TESTS_UNIT_OBJS) $(TESTS_INTEGRATION_OBJS) $(TESTS_STRESS_OBJS)\nTESTS_CPP_OBJS := $(TESTS_INTEGRATION_CPP_OBJS)\n\n.PHONY: all dist build_doc_html build_doc_man build_doc\n.PHONY: install_bin install_include install_lib\n.PHONY: install_doc_html install_doc_man install_doc install\n.PHONY: tests check clean distclean relclean\n\n.SECONDARY : $(PRIVATE_NAMESPACE_GEN_HDRS) $(TESTS_OBJS) $(TESTS_CPP_OBJS)\n\n# Default target.\nall: build_lib\n\ndist: build_doc\n\n$(objroot)doc/%.html : $(objroot)doc/%.xml $(srcroot)doc/stylesheet.xsl $(objroot)doc/html.xsl\n\t$(XSLTPROC) -o $@ $(objroot)doc/html.xsl $<\n\n$(objroot)doc/%.3 : $(objroot)doc/%.xml $(srcroot)doc/stylesheet.xsl $(objroot)doc/manpages.xsl\n\t$(XSLTPROC) -o $@ $(objroot)doc/manpages.xsl $<\n\nbuild_doc_html: $(DOCS_HTML)\nbuild_doc_man: $(DOCS_MAN3)\nbuild_doc: $(DOCS)\n\n#\n# Include generated dependency files.\n#\nifdef CC_MM\n-include $(C_SYM_OBJS:%.$(O)=%.d)\n-include $(C_OBJS:%.$(O)=%.d)\n-include $(CPP_OBJS:%.$(O)=%.d)\n-include $(C_PIC_OBJS:%.$(O)=%.d)\n-include $(CPP_PIC_OBJS:%.$(O)=%.d)\n-include $(C_JET_SYM_OBJS:%.$(O)=%.d)\n-include $(C_JET_OBJS:%.$(O)=%.d)\n-include $(C_TESTLIB_OBJS:%.$(O)=%.d)\n-include $(TESTS_OBJS:%.$(O)=%.d)\n-include $(TESTS_CPP_OBJS:%.$(O)=%.d)\nendif\n\n$(C_SYM_OBJS): $(objroot)src/%.sym.$(O): $(srcroot)src/%.c\n$(C_SYM_OBJS): CPPFLAGS += -DJEMALLOC_NO_PRIVATE_NAMESPACE\n$(C_SYMS): $(objroot)src/%.sym: $(objroot)src/%.sym.$(O)\n$(C_OBJS): $(objroot)src/%.$(O): $(srcroot)src/%.c\n$(CPP_OBJS): $(objroot)src/%.$(O): $(srcroot)src/%.cpp\n$(C_PIC_OBJS): $(objroot)src/%.pic.$(O): $(srcroot)src/%.c\n$(C_PIC_OBJS): CFLAGS += $(PIC_CFLAGS)\n$(CPP_PIC_OBJS): $(objroot)src/%.pic.$(O): $(srcroot)src/%.cpp\n$(CPP_PIC_OBJS): CXXFLAGS += $(PIC_CFLAGS)\n$(C_JET_SYM_OBJS): $(objroot)src/%.jet.sym.$(O): $(srcroot)src/%.c\n$(C_JET_SYM_OBJS): CPPFLAGS += -DJEMALLOC_JET -DJEMALLOC_NO_PRIVATE_NAMESPACE\n$(C_JET_SYMS): $(objroot)src/%.jet.sym: $(objroot)src/%.jet.sym.$(O)\n$(C_JET_OBJS): $(objroot)src/%.jet.$(O): $(srcroot)src/%.c\n$(C_JET_OBJS): CPPFLAGS += -DJEMALLOC_JET\n$(C_TESTLIB_UNIT_OBJS): $(objroot)test/src/%.unit.$(O): $(srcroot)test/src/%.c\n$(C_TESTLIB_UNIT_OBJS): CPPFLAGS += -DJEMALLOC_UNIT_TEST\n$(C_TESTLIB_INTEGRATION_OBJS): $(objroot)test/src/%.integration.$(O): $(srcroot)test/src/%.c\n$(C_TESTLIB_INTEGRATION_OBJS): CPPFLAGS += -DJEMALLOC_INTEGRATION_TEST\n$(C_UTIL_INTEGRATION_OBJS): $(objroot)src/%.integration.$(O): $(srcroot)src/%.c\n$(C_TESTLIB_STRESS_OBJS): $(objroot)test/src/%.stress.$(O): $(srcroot)test/src/%.c\n$(C_TESTLIB_STRESS_OBJS): CPPFLAGS += -DJEMALLOC_STRESS_TEST -DJEMALLOC_STRESS_TESTLIB\n$(C_TESTLIB_OBJS): CPPFLAGS += -I$(srcroot)test/include -I$(objroot)test/include\n$(TESTS_UNIT_OBJS): CPPFLAGS += -DJEMALLOC_UNIT_TEST\n$(TESTS_INTEGRATION_OBJS): CPPFLAGS += -DJEMALLOC_INTEGRATION_TEST\n$(TESTS_INTEGRATION_CPP_OBJS): CPPFLAGS += -DJEMALLOC_INTEGRATION_CPP_TEST\n$(TESTS_STRESS_OBJS): CPPFLAGS += -DJEMALLOC_STRESS_TEST\n$(TESTS_OBJS): $(objroot)test/%.$(O): $(srcroot)test/%.c\n$(TESTS_CPP_OBJS): $(objroot)test/%.$(O): $(srcroot)test/%.cpp\n$(TESTS_OBJS): CPPFLAGS += -I$(srcroot)test/include -I$(objroot)test/include\n$(TESTS_CPP_OBJS): CPPFLAGS += -I$(srcroot)test/include -I$(objroot)test/include\nifneq ($(IMPORTLIB),$(SO))\n$(CPP_OBJS) $(C_SYM_OBJS) $(C_OBJS) $(C_JET_SYM_OBJS) $(C_JET_OBJS): CPPFLAGS += -DDLLEXPORT\nendif\n\n# Dependencies.\nifndef CC_MM\nHEADER_DIRS = $(srcroot)include/jemalloc/internal \\\n\t$(objroot)include/jemalloc $(objroot)include/jemalloc/internal\nHEADERS = $(filter-out $(PRIVATE_NAMESPACE_HDRS),$(wildcard $(foreach dir,$(HEADER_DIRS),$(dir)/*.h)))\n$(C_SYM_OBJS) $(C_OBJS) $(CPP_OBJS) $(C_PIC_OBJS) $(CPP_PIC_OBJS) $(C_JET_SYM_OBJS) $(C_JET_OBJS) $(C_TESTLIB_OBJS) $(TESTS_OBJS) $(TESTS_CPP_OBJS): $(HEADERS)\n$(TESTS_OBJS) $(TESTS_CPP_OBJS): $(objroot)test/include/test/jemalloc_test.h\nendif\n\n$(C_OBJS) $(CPP_OBJS) $(C_PIC_OBJS) $(CPP_PIC_OBJS) $(C_TESTLIB_INTEGRATION_OBJS) $(C_UTIL_INTEGRATION_OBJS) $(TESTS_INTEGRATION_OBJS) $(TESTS_INTEGRATION_CPP_OBJS): $(objroot)include/jemalloc/internal/private_namespace.h\n$(C_JET_OBJS) $(C_TESTLIB_UNIT_OBJS) $(C_TESTLIB_STRESS_OBJS) $(TESTS_UNIT_OBJS) $(TESTS_STRESS_OBJS): $(objroot)include/jemalloc/internal/private_namespace_jet.h\n\n$(C_SYM_OBJS) $(C_OBJS) $(C_PIC_OBJS) $(C_JET_SYM_OBJS) $(C_JET_OBJS) $(C_TESTLIB_OBJS) $(TESTS_OBJS): %.$(O):\n\t@mkdir -p $(@D)\n\t$(CC) $(CFLAGS) -c $(CPPFLAGS) $(CTARGET) $<\nifdef CC_MM\n\t@$(CC) -MM $(CPPFLAGS) -MT $@ -o $(@:%.$(O)=%.d) $<\nendif\n\n$(C_SYMS): %.sym:\n\t@mkdir -p $(@D)\n\t$(DUMP_SYMS) $< | $(AWK) -f $(objroot)include/jemalloc/internal/private_symbols.awk > $@\n\n$(C_JET_SYMS): %.sym:\n\t@mkdir -p $(@D)\n\t$(DUMP_SYMS) $< | $(AWK) -f $(objroot)include/jemalloc/internal/private_symbols_jet.awk > $@\n\n$(objroot)include/jemalloc/internal/private_namespace.gen.h: $(C_SYMS)\n\t$(SHELL) $(srcroot)include/jemalloc/internal/private_namespace.sh $^ > $@\n\n$(objroot)include/jemalloc/internal/private_namespace_jet.gen.h: $(C_JET_SYMS)\n\t$(SHELL) $(srcroot)include/jemalloc/internal/private_namespace.sh $^ > $@\n\n%.h: %.gen.h\n\t@if ! `cmp -s $< $@` ; then echo \"cp $< $<\"; cp $< $@ ; fi\n\n$(CPP_OBJS) $(CPP_PIC_OBJS) $(TESTS_CPP_OBJS): %.$(O):\n\t@mkdir -p $(@D)\n\t$(CXX) $(CXXFLAGS) -c $(CPPFLAGS) $(CTARGET) $<\nifdef CC_MM\n\t@$(CXX) -MM $(CPPFLAGS) -MT $@ -o $(@:%.$(O)=%.d) $<\nendif\n\nifneq ($(SOREV),$(SO))\n%.$(SO) : %.$(SOREV)\n\t@mkdir -p $(@D)\n\tln -sf $(<F) $@\nendif\n\n$(objroot)lib/$(LIBJEMALLOC).$(SOREV) : $(if $(PIC_CFLAGS),$(C_PIC_OBJS),$(C_OBJS)) $(if $(PIC_CFLAGS),$(CPP_PIC_OBJS),$(CPP_OBJS))\n\t@mkdir -p $(@D)\n\t$(CC) $(DSO_LDFLAGS) $(call RPATH,$(RPATH_EXTRA)) $(LDTARGET) $+ $(LDFLAGS) $(LIBS) $(EXTRA_LDFLAGS)\n\n$(objroot)lib/$(LIBJEMALLOC)_pic.$(A) : $(C_PIC_OBJS) $(CPP_PIC_OBJS)\n$(objroot)lib/$(LIBJEMALLOC).$(A) : $(C_OBJS) $(CPP_OBJS)\n$(objroot)lib/$(LIBJEMALLOC)_s.$(A) : $(C_OBJS) $(CPP_OBJS)\n\n$(STATIC_LIBS):\n\t@mkdir -p $(@D)\n\t$(AR) $(ARFLAGS)@AROUT@ $+\n\n$(objroot)test/unit/%$(EXE): $(objroot)test/unit/%.$(O) $(C_JET_OBJS) $(C_TESTLIB_UNIT_OBJS)\n\t@mkdir -p $(@D)\n\t$(CC) $(LDTARGET) $(filter %.$(O),$^) $(call RPATH,$(objroot)lib) $(LDFLAGS) $(filter-out -lm,$(LIBS)) $(LM) $(EXTRA_LDFLAGS)\n\n$(objroot)test/integration/%$(EXE): $(objroot)test/integration/%.$(O) $(C_TESTLIB_INTEGRATION_OBJS) $(C_UTIL_INTEGRATION_OBJS) $(objroot)lib/$(LIBJEMALLOC).$(IMPORTLIB)\n\t@mkdir -p $(@D)\n\t$(CC) $(TEST_LD_MODE) $(LDTARGET) $(filter %.$(O),$^) $(call RPATH,$(objroot)lib) $(LJEMALLOC) $(LDFLAGS) $(filter-out -lm,$(filter -lrt -lpthread -lstdc++,$(LIBS))) $(LM) $(EXTRA_LDFLAGS)\n\n$(objroot)test/integration/cpp/%$(EXE): $(objroot)test/integration/cpp/%.$(O) $(C_TESTLIB_INTEGRATION_OBJS) $(C_UTIL_INTEGRATION_OBJS) $(objroot)lib/$(LIBJEMALLOC).$(IMPORTLIB)\n\t@mkdir -p $(@D)\n\t$(CXX) $(LDTARGET) $(filter %.$(O),$^) $(call RPATH,$(objroot)lib) $(objroot)lib/$(LIBJEMALLOC).$(IMPORTLIB) $(LDFLAGS) $(filter-out -lm,$(LIBS)) -lm $(EXTRA_LDFLAGS)\n\n$(objroot)test/stress/%$(EXE): $(objroot)test/stress/%.$(O) $(C_JET_OBJS) $(C_TESTLIB_STRESS_OBJS) $(objroot)lib/$(LIBJEMALLOC).$(IMPORTLIB)\n\t@mkdir -p $(@D)\n\t$(CC) $(TEST_LD_MODE) $(LDTARGET) $(filter %.$(O),$^) $(call RPATH,$(objroot)lib) $(objroot)lib/$(LIBJEMALLOC).$(IMPORTLIB) $(LDFLAGS) $(filter-out -lm,$(LIBS)) $(LM) $(EXTRA_LDFLAGS)\n\nbuild_lib_shared: $(DSOS)\nbuild_lib_static: $(STATIC_LIBS)\nbuild_lib: build_lib_shared build_lib_static\n\ninstall_bin:\n\t$(INSTALL) -d $(BINDIR)\n\t@for b in $(BINS); do \\\n\techo \"$(INSTALL) -m 755 $$b $(BINDIR)\"; \\\n\t$(INSTALL) -m 755 $$b $(BINDIR); \\\ndone\n\ninstall_include:\n\t$(INSTALL) -d $(INCLUDEDIR)/jemalloc\n\t@for h in $(C_HDRS); do \\\n\techo \"$(INSTALL) -m 644 $$h $(INCLUDEDIR)/jemalloc\"; \\\n\t$(INSTALL) -m 644 $$h $(INCLUDEDIR)/jemalloc; \\\ndone\n\ninstall_lib_shared: $(DSOS)\n\t$(INSTALL) -d $(LIBDIR)\n\t$(INSTALL) -m 755 $(objroot)lib/$(LIBJEMALLOC).$(SOREV) $(LIBDIR)\nifneq ($(SOREV),$(SO))\n\tln -sf $(LIBJEMALLOC).$(SOREV) $(LIBDIR)/$(LIBJEMALLOC).$(SO)\nendif\n\ninstall_lib_static: $(STATIC_LIBS)\n\t$(INSTALL) -d $(LIBDIR)\n\t@for l in $(STATIC_LIBS); do \\\n\techo \"$(INSTALL) -m 755 $$l $(LIBDIR)\"; \\\n\t$(INSTALL) -m 755 $$l $(LIBDIR); \\\ndone\n\ninstall_lib_pc: $(PC)\n\t$(INSTALL) -d $(LIBDIR)/pkgconfig\n\t@for l in $(PC); do \\\n\techo \"$(INSTALL) -m 644 $$l $(LIBDIR)/pkgconfig\"; \\\n\t$(INSTALL) -m 644 $$l $(LIBDIR)/pkgconfig; \\\ndone\n\ninstall_lib: install_lib_shared install_lib_static install_lib_pc\n\ninstall_doc_html:\n\t$(INSTALL) -d $(DATADIR)/doc/jemalloc$(install_suffix)\n\t@for d in $(DOCS_HTML); do \\\n\techo \"$(INSTALL) -m 644 $$d $(DATADIR)/doc/jemalloc$(install_suffix)\"; \\\n\t$(INSTALL) -m 644 $$d $(DATADIR)/doc/jemalloc$(install_suffix); \\\ndone\n\ninstall_doc_man:\n\t$(INSTALL) -d $(MANDIR)/man3\n\t@for d in $(DOCS_MAN3); do \\\n\techo \"$(INSTALL) -m 644 $$d $(MANDIR)/man3\"; \\\n\t$(INSTALL) -m 644 $$d $(MANDIR)/man3; \\\ndone\n\ninstall_doc: install_doc_html install_doc_man\n\ninstall: install_bin install_include install_lib install_doc\n\ntests_unit: $(TESTS_UNIT:$(srcroot)%.c=$(objroot)%$(EXE))\ntests_integration: $(TESTS_INTEGRATION:$(srcroot)%.c=$(objroot)%$(EXE)) $(TESTS_INTEGRATION_CPP:$(srcroot)%.cpp=$(objroot)%$(EXE))\ntests_stress: $(TESTS_STRESS:$(srcroot)%.c=$(objroot)%$(EXE))\ntests: tests_unit tests_integration tests_stress\n\ncheck_unit_dir:\n\t@mkdir -p $(objroot)test/unit\ncheck_integration_dir:\n\t@mkdir -p $(objroot)test/integration\nstress_dir:\n\t@mkdir -p $(objroot)test/stress\ncheck_dir: check_unit_dir check_integration_dir\n\ncheck_unit: tests_unit check_unit_dir\n\t$(SHELL) $(objroot)test/test.sh $(TESTS_UNIT:$(srcroot)%.c=$(objroot)%)\ncheck_integration_prof: tests_integration check_integration_dir\nifeq ($(enable_prof), 1)\n\t$(MALLOC_CONF)=\"prof:true\" $(SHELL) $(objroot)test/test.sh $(TESTS_INTEGRATION:$(srcroot)%.c=$(objroot)%) $(TESTS_INTEGRATION_CPP:$(srcroot)%.cpp=$(objroot)%)\n\t$(MALLOC_CONF)=\"prof:true,prof_active:false\" $(SHELL) $(objroot)test/test.sh $(TESTS_INTEGRATION:$(srcroot)%.c=$(objroot)%) $(TESTS_INTEGRATION_CPP:$(srcroot)%.cpp=$(objroot)%)\nendif\ncheck_integration_decay: tests_integration check_integration_dir\n\t$(MALLOC_CONF)=\"dirty_decay_ms:-1,muzzy_decay_ms:-1\" $(SHELL) $(objroot)test/test.sh $(TESTS_INTEGRATION:$(srcroot)%.c=$(objroot)%) $(TESTS_INTEGRATION_CPP:$(srcroot)%.cpp=$(objroot)%)\n\t$(MALLOC_CONF)=\"dirty_decay_ms:0,muzzy_decay_ms:0\" $(SHELL) $(objroot)test/test.sh $(TESTS_INTEGRATION:$(srcroot)%.c=$(objroot)%) $(TESTS_INTEGRATION_CPP:$(srcroot)%.cpp=$(objroot)%)\ncheck_integration: tests_integration check_integration_dir\n\t$(SHELL) $(objroot)test/test.sh $(TESTS_INTEGRATION:$(srcroot)%.c=$(objroot)%) $(TESTS_INTEGRATION_CPP:$(srcroot)%.cpp=$(objroot)%)\nstress: tests_stress stress_dir\n\t$(SHELL) $(objroot)test/test.sh $(TESTS_STRESS:$(srcroot)%.c=$(objroot)%)\ncheck: check_unit check_integration check_integration_decay check_integration_prof\n\nclean:\n\trm -f $(PRIVATE_NAMESPACE_HDRS)\n\trm -f $(PRIVATE_NAMESPACE_GEN_HDRS)\n\trm -f $(C_SYM_OBJS)\n\trm -f $(C_SYMS)\n\trm -f $(C_OBJS)\n\trm -f $(CPP_OBJS)\n\trm -f $(C_PIC_OBJS)\n\trm -f $(CPP_PIC_OBJS)\n\trm -f $(C_JET_SYM_OBJS)\n\trm -f $(C_JET_SYMS)\n\trm -f $(C_JET_OBJS)\n\trm -f $(C_TESTLIB_OBJS)\n\trm -f $(C_SYM_OBJS:%.$(O)=%.d)\n\trm -f $(C_OBJS:%.$(O)=%.d)\n\trm -f $(CPP_OBJS:%.$(O)=%.d)\n\trm -f $(C_PIC_OBJS:%.$(O)=%.d)\n\trm -f $(CPP_PIC_OBJS:%.$(O)=%.d)\n\trm -f $(C_JET_SYM_OBJS:%.$(O)=%.d)\n\trm -f $(C_JET_OBJS:%.$(O)=%.d)\n\trm -f $(C_TESTLIB_OBJS:%.$(O)=%.d)\n\trm -f $(TESTS_OBJS:%.$(O)=%$(EXE))\n\trm -f $(TESTS_OBJS)\n\trm -f $(TESTS_OBJS:%.$(O)=%.d)\n\trm -f $(TESTS_OBJS:%.$(O)=%.out)\n\trm -f $(TESTS_CPP_OBJS:%.$(O)=%$(EXE))\n\trm -f $(TESTS_CPP_OBJS)\n\trm -f $(TESTS_CPP_OBJS:%.$(O)=%.d)\n\trm -f $(TESTS_CPP_OBJS:%.$(O)=%.out)\n\trm -f $(DSOS) $(STATIC_LIBS)\n\ndistclean: clean\n\trm -f $(objroot)bin/jemalloc-config\n\trm -f $(objroot)bin/jemalloc.sh\n\trm -f $(objroot)bin/jeprof\n\trm -f $(objroot)config.log\n\trm -f $(objroot)config.status\n\trm -f $(objroot)config.stamp\n\trm -f $(cfghdrs_out)\n\trm -f $(cfgoutputs_out)\n\nrelclean: distclean\n\trm -f $(objroot)configure\n\trm -f $(objroot)VERSION\n\trm -f $(DOCS_HTML)\n\trm -f $(DOCS_MAN3)\n\n#===============================================================================\n# Re-configuration rules.\n\nifeq ($(enable_autogen), 1)\n$(srcroot)configure : $(srcroot)configure.ac\n\tcd ./$(srcroot) && $(AUTOCONF)\n\n$(objroot)config.status : $(srcroot)configure\n\t./$(objroot)config.status --recheck\n\n$(srcroot)config.stamp.in : $(srcroot)configure.ac\n\techo stamp > $(srcroot)config.stamp.in\n\n$(objroot)config.stamp : $(cfgoutputs_in) $(cfghdrs_in) $(srcroot)configure\n\t./$(objroot)config.status\n\t@touch $@\n\n# There must be some action in order for make to re-read Makefile when it is\n# out of date.\n$(cfgoutputs_out) $(cfghdrs_out) : $(objroot)config.stamp\n\t@true\nendif\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/README",
    "content": "jemalloc is a general purpose malloc(3) implementation that emphasizes\nfragmentation avoidance and scalable concurrency support.  jemalloc first came\ninto use as the FreeBSD libc allocator in 2005, and since then it has found its\nway into numerous applications that rely on its predictable behavior.  In 2010\njemalloc development efforts broadened to include developer support features\nsuch as heap profiling and extensive monitoring/tuning hooks.  Modern jemalloc\nreleases continue to be integrated back into FreeBSD, and therefore versatility\nremains critical.  Ongoing development efforts trend toward making jemalloc\namong the best allocators for a broad range of demanding applications, and\neliminating/mitigating weaknesses that have practical repercussions for real\nworld applications.\n\nThe COPYING file contains copyright and licensing information.\n\nThe INSTALL file contains information on how to configure, build, and install\njemalloc.\n\nThe ChangeLog file contains a brief summary of changes for each release.\n\nURL: http://jemalloc.net/\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/autogen.sh",
    "content": "#!/bin/sh\n\nfor i in autoconf; do\n    echo \"$i\"\n    $i\n    if [ $? -ne 0 ]; then\n\techo \"Error $? in $i\"\n\texit 1\n    fi\ndone\n\necho \"./configure --enable-autogen $@\"\n./configure --enable-autogen $@\nif [ $? -ne 0 ]; then\n    echo \"Error $? in ./configure\"\n    exit 1\nfi\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/bin/jemalloc-config.in",
    "content": "#!/bin/sh\n\nusage() {\n\tcat <<EOF\nUsage:\n  @BINDIR@/jemalloc-config <option>\nOptions:\n  --help | -h  : Print usage.\n  --version    : Print jemalloc version.\n  --revision   : Print shared library revision number.\n  --config     : Print configure options used to build jemalloc.\n  --prefix     : Print installation directory prefix.\n  --bindir     : Print binary installation directory.\n  --datadir    : Print data installation directory.\n  --includedir : Print include installation directory.\n  --libdir     : Print library installation directory.\n  --mandir     : Print manual page installation directory.\n  --cc         : Print compiler used to build jemalloc.\n  --cflags     : Print compiler flags used to build jemalloc.\n  --cppflags   : Print preprocessor flags used to build jemalloc.\n  --cxxflags   : Print C++ compiler flags used to build jemalloc.\n  --ldflags    : Print library flags used to build jemalloc.\n  --libs       : Print libraries jemalloc was linked against.\nEOF\n}\n\nprefix=\"@prefix@\"\nexec_prefix=\"@exec_prefix@\"\n\ncase \"$1\" in\n--help | -h)\n\tusage\n\texit 0\n\t;;\n--version)\n\techo \"@jemalloc_version@\"\n\t;;\n--revision)\n\techo \"@rev@\"\n\t;;\n--config)\n\techo \"@CONFIG@\"\n\t;;\n--prefix)\n\techo \"@PREFIX@\"\n\t;;\n--bindir)\n\techo \"@BINDIR@\"\n\t;;\n--datadir)\n\techo \"@DATADIR@\"\n\t;;\n--includedir)\n\techo \"@INCLUDEDIR@\"\n\t;;\n--libdir)\n\techo \"@LIBDIR@\"\n\t;;\n--mandir)\n\techo \"@MANDIR@\"\n\t;;\n--cc)\n\techo \"@CC@\"\n\t;;\n--cflags)\n\techo \"@CFLAGS@\"\n\t;;\n--cppflags)\n\techo \"@CPPFLAGS@\"\n\t;;\n--cxxflags)\n\techo \"@CXXFLAGS@\"\n\t;;\n--ldflags)\n\techo \"@LDFLAGS@ @EXTRA_LDFLAGS@\"\n\t;;\n--libs)\n\techo \"@LIBS@\"\n\t;;\n*)\n\tusage\n\texit 1\nesac\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/bin/jemalloc.sh.in",
    "content": "#!/bin/sh\n\nprefix=@prefix@\nexec_prefix=@exec_prefix@\nlibdir=@libdir@\n\n@LD_PRELOAD_VAR@=${libdir}/libjemalloc.@SOREV@\nexport @LD_PRELOAD_VAR@\nexec \"$@\"\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/bin/jeprof.in",
    "content": "#! /usr/bin/env perl\n\n# Copyright (c) 1998-2007, Google Inc.\n# All rights reserved.\n#\n# Redistribution and use in source and binary forms, with or without\n# modification, are permitted provided that the following conditions are\n# met:\n#\n#     * Redistributions of source code must retain the above copyright\n# notice, this list of conditions and the following disclaimer.\n#     * Redistributions in binary form must reproduce the above\n# copyright notice, this list of conditions and the following disclaimer\n# in the documentation and/or other materials provided with the\n# distribution.\n#     * Neither the name of Google Inc. nor the names of its\n# contributors may be used to endorse or promote products derived from\n# this software without specific prior written permission.\n#\n# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n# \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\n# ---\n# Program for printing the profile generated by common/profiler.cc,\n# or by the heap profiler (common/debugallocation.cc)\n#\n# The profile contains a sequence of entries of the form:\n#       <count> <stack trace>\n# This program parses the profile, and generates user-readable\n# output.\n#\n# Examples:\n#\n# % tools/jeprof \"program\" \"profile\"\n#   Enters \"interactive\" mode\n#\n# % tools/jeprof --text \"program\" \"profile\"\n#   Generates one line per procedure\n#\n# % tools/jeprof --gv \"program\" \"profile\"\n#   Generates annotated call-graph and displays via \"gv\"\n#\n# % tools/jeprof --gv --focus=Mutex \"program\" \"profile\"\n#   Restrict to code paths that involve an entry that matches \"Mutex\"\n#\n# % tools/jeprof --gv --focus=Mutex --ignore=string \"program\" \"profile\"\n#   Restrict to code paths that involve an entry that matches \"Mutex\"\n#   and does not match \"string\"\n#\n# % tools/jeprof --list=IBF_CheckDocid \"program\" \"profile\"\n#   Generates disassembly listing of all routines with at least one\n#   sample that match the --list=<regexp> pattern.  The listing is\n#   annotated with the flat and cumulative sample counts at each line.\n#\n# % tools/jeprof --disasm=IBF_CheckDocid \"program\" \"profile\"\n#   Generates disassembly listing of all routines with at least one\n#   sample that match the --disasm=<regexp> pattern.  The listing is\n#   annotated with the flat and cumulative sample counts at each PC value.\n#\n# TODO: Use color to indicate files?\n\nuse strict;\nuse warnings;\nuse Getopt::Long;\nuse Cwd;\n\nmy $JEPROF_VERSION = \"@jemalloc_version@\";\nmy $PPROF_VERSION = \"2.0\";\n\n# These are the object tools we use which can come from a\n# user-specified location using --tools, from the JEPROF_TOOLS\n# environment variable, or from the environment.\nmy %obj_tool_map = (\n  \"objdump\" => \"objdump\",\n  \"nm\" => \"nm\",\n  \"addr2line\" => \"addr2line\",\n  \"c++filt\" => \"c++filt\",\n  ## ConfigureObjTools may add architecture-specific entries:\n  #\"nm_pdb\" => \"nm-pdb\",       # for reading windows (PDB-format) executables\n  #\"addr2line_pdb\" => \"addr2line-pdb\",                                # ditto\n  #\"otool\" => \"otool\",         # equivalent of objdump on OS X\n);\n# NOTE: these are lists, so you can put in commandline flags if you want.\nmy @DOT = (\"dot\");          # leave non-absolute, since it may be in /usr/local\nmy @GV = (\"gv\");\nmy @EVINCE = (\"evince\");    # could also be xpdf or perhaps acroread\nmy @KCACHEGRIND = (\"kcachegrind\");\nmy @PS2PDF = (\"ps2pdf\");\n# These are used for dynamic profiles\nmy @URL_FETCHER = (\"curl\", \"-s\", \"--fail\");\n\n# These are the web pages that servers need to support for dynamic profiles\nmy $HEAP_PAGE = \"/pprof/heap\";\nmy $PROFILE_PAGE = \"/pprof/profile\";   # must support cgi-param \"?seconds=#\"\nmy $PMUPROFILE_PAGE = \"/pprof/pmuprofile(?:\\\\?.*)?\"; # must support cgi-param\n                                                # ?seconds=#&event=x&period=n\nmy $GROWTH_PAGE = \"/pprof/growth\";\nmy $CONTENTION_PAGE = \"/pprof/contention\";\nmy $WALL_PAGE = \"/pprof/wall(?:\\\\?.*)?\";  # accepts options like namefilter\nmy $FILTEREDPROFILE_PAGE = \"/pprof/filteredprofile(?:\\\\?.*)?\";\nmy $CENSUSPROFILE_PAGE = \"/pprof/censusprofile(?:\\\\?.*)?\"; # must support cgi-param\n                                                       # \"?seconds=#\",\n                                                       # \"?tags_regexp=#\" and\n                                                       # \"?type=#\".\nmy $SYMBOL_PAGE = \"/pprof/symbol\";     # must support symbol lookup via POST\nmy $PROGRAM_NAME_PAGE = \"/pprof/cmdline\";\n\n# These are the web pages that can be named on the command line.\n# All the alternatives must begin with /.\nmy $PROFILES = \"($HEAP_PAGE|$PROFILE_PAGE|$PMUPROFILE_PAGE|\" .\n               \"$GROWTH_PAGE|$CONTENTION_PAGE|$WALL_PAGE|\" .\n               \"$FILTEREDPROFILE_PAGE|$CENSUSPROFILE_PAGE)\";\n\n# default binary name\nmy $UNKNOWN_BINARY = \"(unknown)\";\n\n# There is a pervasive dependency on the length (in hex characters,\n# i.e., nibbles) of an address, distinguishing between 32-bit and\n# 64-bit profiles.  To err on the safe size, default to 64-bit here:\nmy $address_length = 16;\n\nmy $dev_null = \"/dev/null\";\nif (! -e $dev_null && $^O =~ /MSWin/) {    # $^O is the OS perl was built for\n  $dev_null = \"nul\";\n}\n\n# A list of paths to search for shared object files\nmy @prefix_list = ();\n\n# Special routine name that should not have any symbols.\n# Used as separator to parse \"addr2line -i\" output.\nmy $sep_symbol = '_fini';\nmy $sep_address = undef;\n\n##### Argument parsing #####\n\nsub usage_string {\n  return <<EOF;\nUsage:\njeprof [options] <program> <profiles>\n   <profiles> is a space separated list of profile names.\njeprof [options] <symbolized-profiles>\n   <symbolized-profiles> is a list of profile files where each file contains\n   the necessary symbol mappings  as well as profile data (likely generated\n   with --raw).\njeprof [options] <profile>\n   <profile> is a remote form.  Symbols are obtained from host:port$SYMBOL_PAGE\n\n   Each name can be:\n   /path/to/profile        - a path to a profile file\n   host:port[/<service>]   - a location of a service to get profile from\n\n   The /<service> can be $HEAP_PAGE, $PROFILE_PAGE, /pprof/pmuprofile,\n                         $GROWTH_PAGE, $CONTENTION_PAGE, /pprof/wall,\n                         $CENSUSPROFILE_PAGE, or /pprof/filteredprofile.\n   For instance:\n     jeprof http://myserver.com:80$HEAP_PAGE\n   If /<service> is omitted, the service defaults to $PROFILE_PAGE (cpu profiling).\njeprof --symbols <program>\n   Maps addresses to symbol names.  In this mode, stdin should be a\n   list of library mappings, in the same format as is found in the heap-\n   and cpu-profile files (this loosely matches that of /proc/self/maps\n   on linux), followed by a list of hex addresses to map, one per line.\n\n   For more help with querying remote servers, including how to add the\n   necessary server-side support code, see this filename (or one like it):\n\n   /usr/doc/gperftools-$PPROF_VERSION/pprof_remote_servers.html\n\nOptions:\n   --cum               Sort by cumulative data\n   --base=<base>       Subtract <base> from <profile> before display\n   --interactive       Run in interactive mode (interactive \"help\" gives help) [default]\n   --seconds=<n>       Length of time for dynamic profiles [default=30 secs]\n   --add_lib=<file>    Read additional symbols and line info from the given library\n   --lib_prefix=<dir>  Comma separated list of library path prefixes\n\nReporting Granularity:\n   --addresses         Report at address level\n   --lines             Report at source line level\n   --functions         Report at function level [default]\n   --files             Report at source file level\n\nOutput type:\n   --text              Generate text report\n   --callgrind         Generate callgrind format to stdout\n   --gv                Generate Postscript and display\n   --evince            Generate PDF and display\n   --web               Generate SVG and display\n   --list=<regexp>     Generate source listing of matching routines\n   --disasm=<regexp>   Generate disassembly of matching routines\n   --symbols           Print demangled symbol names found at given addresses\n   --dot               Generate DOT file to stdout\n   --ps                Generate Postcript to stdout\n   --pdf               Generate PDF to stdout\n   --svg               Generate SVG to stdout\n   --gif               Generate GIF to stdout\n   --raw               Generate symbolized jeprof data (useful with remote fetch)\n\nHeap-Profile Options:\n   --inuse_space       Display in-use (mega)bytes [default]\n   --inuse_objects     Display in-use objects\n   --alloc_space       Display allocated (mega)bytes\n   --alloc_objects     Display allocated objects\n   --show_bytes        Display space in bytes\n   --drop_negative     Ignore negative differences\n\nContention-profile options:\n   --total_delay       Display total delay at each region [default]\n   --contentions       Display number of delays at each region\n   --mean_delay        Display mean delay at each region\n\nCall-graph Options:\n   --nodecount=<n>     Show at most so many nodes [default=80]\n   --nodefraction=<f>  Hide nodes below <f>*total [default=.005]\n   --edgefraction=<f>  Hide edges below <f>*total [default=.001]\n   --maxdegree=<n>     Max incoming/outgoing edges per node [default=8]\n   --focus=<regexp>    Focus on backtraces with nodes matching <regexp>\n   --thread=<n>        Show profile for thread <n>\n   --ignore=<regexp>   Ignore backtraces with nodes matching <regexp>\n   --scale=<n>         Set GV scaling [default=0]\n   --heapcheck         Make nodes with non-0 object counts\n                       (i.e. direct leak generators) more visible\n   --retain=<regexp>   Retain only nodes that match <regexp>\n   --exclude=<regexp>  Exclude all nodes that match <regexp>\n\nMiscellaneous:\n   --tools=<prefix or binary:fullpath>[,...]   \\$PATH for object tool pathnames\n   --test              Run unit tests\n   --help              This message\n   --version           Version information\n\nEnvironment Variables:\n   JEPROF_TMPDIR        Profiles directory. Defaults to \\$HOME/jeprof\n   JEPROF_TOOLS         Prefix for object tools pathnames\n\nExamples:\n\njeprof /bin/ls ls.prof\n                       Enters \"interactive\" mode\njeprof --text /bin/ls ls.prof\n                       Outputs one line per procedure\njeprof --web /bin/ls ls.prof\n                       Displays annotated call-graph in web browser\njeprof --gv /bin/ls ls.prof\n                       Displays annotated call-graph via 'gv'\njeprof --gv --focus=Mutex /bin/ls ls.prof\n                       Restricts to code paths including a .*Mutex.* entry\njeprof --gv --focus=Mutex --ignore=string /bin/ls ls.prof\n                       Code paths including Mutex but not string\njeprof --list=getdir /bin/ls ls.prof\n                       (Per-line) annotated source listing for getdir()\njeprof --disasm=getdir /bin/ls ls.prof\n                       (Per-PC) annotated disassembly for getdir()\n\njeprof http://localhost:1234/\n                       Enters \"interactive\" mode\njeprof --text localhost:1234\n                       Outputs one line per procedure for localhost:1234\njeprof --raw localhost:1234 > ./local.raw\njeprof --text ./local.raw\n                       Fetches a remote profile for later analysis and then\n                       analyzes it in text mode.\nEOF\n}\n\nsub version_string {\n  return <<EOF\njeprof (part of jemalloc $JEPROF_VERSION)\nbased on pprof (part of gperftools $PPROF_VERSION)\n\nCopyright 1998-2007 Google Inc.\n\nThis is BSD licensed software; see the source for copying conditions\nand license information.\nThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\nPARTICULAR PURPOSE.\nEOF\n}\n\nsub usage {\n  my $msg = shift;\n  print STDERR \"$msg\\n\\n\";\n  print STDERR usage_string();\n  print STDERR \"\\nFATAL ERROR: $msg\\n\";    # just as a reminder\n  exit(1);\n}\n\nsub Init() {\n  # Setup tmp-file name and handler to clean it up.\n  # We do this in the very beginning so that we can use\n  # error() and cleanup() function anytime here after.\n  $main::tmpfile_sym = \"/tmp/jeprof$$.sym\";\n  $main::tmpfile_ps = \"/tmp/jeprof$$\";\n  $main::next_tmpfile = 0;\n  $SIG{'INT'} = \\&sighandler;\n\n  # Cache from filename/linenumber to source code\n  $main::source_cache = ();\n\n  $main::opt_help = 0;\n  $main::opt_version = 0;\n\n  $main::opt_cum = 0;\n  $main::opt_base = '';\n  $main::opt_addresses = 0;\n  $main::opt_lines = 0;\n  $main::opt_functions = 0;\n  $main::opt_files = 0;\n  $main::opt_lib_prefix = \"\";\n\n  $main::opt_text = 0;\n  $main::opt_callgrind = 0;\n  $main::opt_list = \"\";\n  $main::opt_disasm = \"\";\n  $main::opt_symbols = 0;\n  $main::opt_gv = 0;\n  $main::opt_evince = 0;\n  $main::opt_web = 0;\n  $main::opt_dot = 0;\n  $main::opt_ps = 0;\n  $main::opt_pdf = 0;\n  $main::opt_gif = 0;\n  $main::opt_svg = 0;\n  $main::opt_raw = 0;\n\n  $main::opt_nodecount = 80;\n  $main::opt_nodefraction = 0.005;\n  $main::opt_edgefraction = 0.001;\n  $main::opt_maxdegree = 8;\n  $main::opt_focus = '';\n  $main::opt_thread = undef;\n  $main::opt_ignore = '';\n  $main::opt_scale = 0;\n  $main::opt_heapcheck = 0;\n  $main::opt_retain = '';\n  $main::opt_exclude = '';\n  $main::opt_seconds = 30;\n  $main::opt_lib = \"\";\n\n  $main::opt_inuse_space   = 0;\n  $main::opt_inuse_objects = 0;\n  $main::opt_alloc_space   = 0;\n  $main::opt_alloc_objects = 0;\n  $main::opt_show_bytes    = 0;\n  $main::opt_drop_negative = 0;\n  $main::opt_interactive   = 0;\n\n  $main::opt_total_delay = 0;\n  $main::opt_contentions = 0;\n  $main::opt_mean_delay = 0;\n\n  $main::opt_tools   = \"\";\n  $main::opt_debug   = 0;\n  $main::opt_test    = 0;\n\n  # These are undocumented flags used only by unittests.\n  $main::opt_test_stride = 0;\n\n  # Are we using $SYMBOL_PAGE?\n  $main::use_symbol_page = 0;\n\n  # Files returned by TempName.\n  %main::tempnames = ();\n\n  # Type of profile we are dealing with\n  # Supported types:\n  #     cpu\n  #     heap\n  #     growth\n  #     contention\n  $main::profile_type = '';     # Empty type means \"unknown\"\n\n  GetOptions(\"help!\"          => \\$main::opt_help,\n             \"version!\"       => \\$main::opt_version,\n             \"cum!\"           => \\$main::opt_cum,\n             \"base=s\"         => \\$main::opt_base,\n             \"seconds=i\"      => \\$main::opt_seconds,\n             \"add_lib=s\"      => \\$main::opt_lib,\n             \"lib_prefix=s\"   => \\$main::opt_lib_prefix,\n             \"functions!\"     => \\$main::opt_functions,\n             \"lines!\"         => \\$main::opt_lines,\n             \"addresses!\"     => \\$main::opt_addresses,\n             \"files!\"         => \\$main::opt_files,\n             \"text!\"          => \\$main::opt_text,\n             \"callgrind!\"     => \\$main::opt_callgrind,\n             \"list=s\"         => \\$main::opt_list,\n             \"disasm=s\"       => \\$main::opt_disasm,\n             \"symbols!\"       => \\$main::opt_symbols,\n             \"gv!\"            => \\$main::opt_gv,\n             \"evince!\"        => \\$main::opt_evince,\n             \"web!\"           => \\$main::opt_web,\n             \"dot!\"           => \\$main::opt_dot,\n             \"ps!\"            => \\$main::opt_ps,\n             \"pdf!\"           => \\$main::opt_pdf,\n             \"svg!\"           => \\$main::opt_svg,\n             \"gif!\"           => \\$main::opt_gif,\n             \"raw!\"           => \\$main::opt_raw,\n             \"interactive!\"   => \\$main::opt_interactive,\n             \"nodecount=i\"    => \\$main::opt_nodecount,\n             \"nodefraction=f\" => \\$main::opt_nodefraction,\n             \"edgefraction=f\" => \\$main::opt_edgefraction,\n             \"maxdegree=i\"    => \\$main::opt_maxdegree,\n             \"focus=s\"        => \\$main::opt_focus,\n             \"thread=s\"       => \\$main::opt_thread,\n             \"ignore=s\"       => \\$main::opt_ignore,\n             \"scale=i\"        => \\$main::opt_scale,\n             \"heapcheck\"      => \\$main::opt_heapcheck,\n             \"retain=s\"       => \\$main::opt_retain,\n             \"exclude=s\"      => \\$main::opt_exclude,\n             \"inuse_space!\"   => \\$main::opt_inuse_space,\n             \"inuse_objects!\" => \\$main::opt_inuse_objects,\n             \"alloc_space!\"   => \\$main::opt_alloc_space,\n             \"alloc_objects!\" => \\$main::opt_alloc_objects,\n             \"show_bytes!\"    => \\$main::opt_show_bytes,\n             \"drop_negative!\" => \\$main::opt_drop_negative,\n             \"total_delay!\"   => \\$main::opt_total_delay,\n             \"contentions!\"   => \\$main::opt_contentions,\n             \"mean_delay!\"    => \\$main::opt_mean_delay,\n             \"tools=s\"        => \\$main::opt_tools,\n             \"test!\"          => \\$main::opt_test,\n             \"debug!\"         => \\$main::opt_debug,\n             # Undocumented flags used only by unittests:\n             \"test_stride=i\"  => \\$main::opt_test_stride,\n      ) || usage(\"Invalid option(s)\");\n\n  # Deal with the standard --help and --version\n  if ($main::opt_help) {\n    print usage_string();\n    exit(0);\n  }\n\n  if ($main::opt_version) {\n    print version_string();\n    exit(0);\n  }\n\n  # Disassembly/listing/symbols mode requires address-level info\n  if ($main::opt_disasm || $main::opt_list || $main::opt_symbols) {\n    $main::opt_functions = 0;\n    $main::opt_lines = 0;\n    $main::opt_addresses = 1;\n    $main::opt_files = 0;\n  }\n\n  # Check heap-profiling flags\n  if ($main::opt_inuse_space +\n      $main::opt_inuse_objects +\n      $main::opt_alloc_space +\n      $main::opt_alloc_objects > 1) {\n    usage(\"Specify at most on of --inuse/--alloc options\");\n  }\n\n  # Check output granularities\n  my $grains =\n      $main::opt_functions +\n      $main::opt_lines +\n      $main::opt_addresses +\n      $main::opt_files +\n      0;\n  if ($grains > 1) {\n    usage(\"Only specify one output granularity option\");\n  }\n  if ($grains == 0) {\n    $main::opt_functions = 1;\n  }\n\n  # Check output modes\n  my $modes =\n      $main::opt_text +\n      $main::opt_callgrind +\n      ($main::opt_list eq '' ? 0 : 1) +\n      ($main::opt_disasm eq '' ? 0 : 1) +\n      ($main::opt_symbols == 0 ? 0 : 1) +\n      $main::opt_gv +\n      $main::opt_evince +\n      $main::opt_web +\n      $main::opt_dot +\n      $main::opt_ps +\n      $main::opt_pdf +\n      $main::opt_svg +\n      $main::opt_gif +\n      $main::opt_raw +\n      $main::opt_interactive +\n      0;\n  if ($modes > 1) {\n    usage(\"Only specify one output mode\");\n  }\n  if ($modes == 0) {\n    if (-t STDOUT) {  # If STDOUT is a tty, activate interactive mode\n      $main::opt_interactive = 1;\n    } else {\n      $main::opt_text = 1;\n    }\n  }\n\n  if ($main::opt_test) {\n    RunUnitTests();\n    # Should not return\n    exit(1);\n  }\n\n  # Binary name and profile arguments list\n  $main::prog = \"\";\n  @main::pfile_args = ();\n\n  # Remote profiling without a binary (using $SYMBOL_PAGE instead)\n  if (@ARGV > 0) {\n    if (IsProfileURL($ARGV[0])) {\n      $main::use_symbol_page = 1;\n    } elsif (IsSymbolizedProfileFile($ARGV[0])) {\n      $main::use_symbolized_profile = 1;\n      $main::prog = $UNKNOWN_BINARY;  # will be set later from the profile file\n    }\n  }\n\n  if ($main::use_symbol_page || $main::use_symbolized_profile) {\n    # We don't need a binary!\n    my %disabled = ('--lines' => $main::opt_lines,\n                    '--disasm' => $main::opt_disasm);\n    for my $option (keys %disabled) {\n      usage(\"$option cannot be used without a binary\") if $disabled{$option};\n    }\n    # Set $main::prog later...\n    scalar(@ARGV) || usage(\"Did not specify profile file\");\n  } elsif ($main::opt_symbols) {\n    # --symbols needs a binary-name (to run nm on, etc) but not profiles\n    $main::prog = shift(@ARGV) || usage(\"Did not specify program\");\n  } else {\n    $main::prog = shift(@ARGV) || usage(\"Did not specify program\");\n    scalar(@ARGV) || usage(\"Did not specify profile file\");\n  }\n\n  # Parse profile file/location arguments\n  foreach my $farg (@ARGV) {\n    if ($farg =~ m/(.*)\\@([0-9]+)(|\\/.*)$/ ) {\n      my $machine = $1;\n      my $num_machines = $2;\n      my $path = $3;\n      for (my $i = 0; $i < $num_machines; $i++) {\n        unshift(@main::pfile_args, \"$i.$machine$path\");\n      }\n    } else {\n      unshift(@main::pfile_args, $farg);\n    }\n  }\n\n  if ($main::use_symbol_page) {\n    unless (IsProfileURL($main::pfile_args[0])) {\n      error(\"The first profile should be a remote form to use $SYMBOL_PAGE\\n\");\n    }\n    CheckSymbolPage();\n    $main::prog = FetchProgramName();\n  } elsif (!$main::use_symbolized_profile) {  # may not need objtools!\n    ConfigureObjTools($main::prog)\n  }\n\n  # Break the opt_lib_prefix into the prefix_list array\n  @prefix_list = split (',', $main::opt_lib_prefix);\n\n  # Remove trailing / from the prefixes, in the list to prevent\n  # searching things like /my/path//lib/mylib.so\n  foreach (@prefix_list) {\n    s|/+$||;\n  }\n}\n\nsub FilterAndPrint {\n  my ($profile, $symbols, $libs, $thread) = @_;\n\n  # Get total data in profile\n  my $total = TotalProfile($profile);\n\n  # Remove uniniteresting stack items\n  $profile = RemoveUninterestingFrames($symbols, $profile);\n\n  # Focus?\n  if ($main::opt_focus ne '') {\n    $profile = FocusProfile($symbols, $profile, $main::opt_focus);\n  }\n\n  # Ignore?\n  if ($main::opt_ignore ne '') {\n    $profile = IgnoreProfile($symbols, $profile, $main::opt_ignore);\n  }\n\n  my $calls = ExtractCalls($symbols, $profile);\n\n  # Reduce profiles to required output granularity, and also clean\n  # each stack trace so a given entry exists at most once.\n  my $reduced = ReduceProfile($symbols, $profile);\n\n  # Get derived profiles\n  my $flat = FlatProfile($reduced);\n  my $cumulative = CumulativeProfile($reduced);\n\n  # Print\n  if (!$main::opt_interactive) {\n    if ($main::opt_disasm) {\n      PrintDisassembly($libs, $flat, $cumulative, $main::opt_disasm);\n    } elsif ($main::opt_list) {\n      PrintListing($total, $libs, $flat, $cumulative, $main::opt_list, 0);\n    } elsif ($main::opt_text) {\n      # Make sure the output is empty when have nothing to report\n      # (only matters when --heapcheck is given but we must be\n      # compatible with old branches that did not pass --heapcheck always):\n      if ($total != 0) {\n        printf(\"Total%s: %s %s\\n\",\n               (defined($thread) ? \" (t$thread)\" : \"\"),\n               Unparse($total), Units());\n      }\n      PrintText($symbols, $flat, $cumulative, -1);\n    } elsif ($main::opt_raw) {\n      PrintSymbolizedProfile($symbols, $profile, $main::prog);\n    } elsif ($main::opt_callgrind) {\n      PrintCallgrind($calls);\n    } else {\n      if (PrintDot($main::prog, $symbols, $profile, $flat, $cumulative, $total)) {\n        if ($main::opt_gv) {\n          RunGV(TempName($main::next_tmpfile, \"ps\"), \"\");\n        } elsif ($main::opt_evince) {\n          RunEvince(TempName($main::next_tmpfile, \"pdf\"), \"\");\n        } elsif ($main::opt_web) {\n          my $tmp = TempName($main::next_tmpfile, \"svg\");\n          RunWeb($tmp);\n          # The command we run might hand the file name off\n          # to an already running browser instance and then exit.\n          # Normally, we'd remove $tmp on exit (right now),\n          # but fork a child to remove $tmp a little later, so that the\n          # browser has time to load it first.\n          delete $main::tempnames{$tmp};\n          if (fork() == 0) {\n            sleep 5;\n            unlink($tmp);\n            exit(0);\n          }\n        }\n      } else {\n        cleanup();\n        exit(1);\n      }\n    }\n  } else {\n    InteractiveMode($profile, $symbols, $libs, $total);\n  }\n}\n\nsub Main() {\n  Init();\n  $main::collected_profile = undef;\n  @main::profile_files = ();\n  $main::op_time = time();\n\n  # Printing symbols is special and requires a lot less info that most.\n  if ($main::opt_symbols) {\n    PrintSymbols(*STDIN);   # Get /proc/maps and symbols output from stdin\n    return;\n  }\n\n  # Fetch all profile data\n  FetchDynamicProfiles();\n\n  # this will hold symbols that we read from the profile files\n  my $symbol_map = {};\n\n  # Read one profile, pick the last item on the list\n  my $data = ReadProfile($main::prog, pop(@main::profile_files));\n  my $profile = $data->{profile};\n  my $pcs = $data->{pcs};\n  my $libs = $data->{libs};   # Info about main program and shared libraries\n  $symbol_map = MergeSymbols($symbol_map, $data->{symbols});\n\n  # Add additional profiles, if available.\n  if (scalar(@main::profile_files) > 0) {\n    foreach my $pname (@main::profile_files) {\n      my $data2 = ReadProfile($main::prog, $pname);\n      $profile = AddProfile($profile, $data2->{profile});\n      $pcs = AddPcs($pcs, $data2->{pcs});\n      $symbol_map = MergeSymbols($symbol_map, $data2->{symbols});\n    }\n  }\n\n  # Subtract base from profile, if specified\n  if ($main::opt_base ne '') {\n    my $base = ReadProfile($main::prog, $main::opt_base);\n    $profile = SubtractProfile($profile, $base->{profile});\n    $pcs = AddPcs($pcs, $base->{pcs});\n    $symbol_map = MergeSymbols($symbol_map, $base->{symbols});\n  }\n\n  # Collect symbols\n  my $symbols;\n  if ($main::use_symbolized_profile) {\n    $symbols = FetchSymbols($pcs, $symbol_map);\n  } elsif ($main::use_symbol_page) {\n    $symbols = FetchSymbols($pcs);\n  } else {\n    # TODO(csilvers): $libs uses the /proc/self/maps data from profile1,\n    # which may differ from the data from subsequent profiles, especially\n    # if they were run on different machines.  Use appropriate libs for\n    # each pc somehow.\n    $symbols = ExtractSymbols($libs, $pcs);\n  }\n\n  if (!defined($main::opt_thread)) {\n    FilterAndPrint($profile, $symbols, $libs);\n  }\n  if (defined($data->{threads})) {\n    foreach my $thread (sort { $a <=> $b } keys(%{$data->{threads}})) {\n      if (defined($main::opt_thread) &&\n          ($main::opt_thread eq '*' || $main::opt_thread == $thread)) {\n        my $thread_profile = $data->{threads}{$thread};\n        FilterAndPrint($thread_profile, $symbols, $libs, $thread);\n      }\n    }\n  }\n\n  cleanup();\n  exit(0);\n}\n\n##### Entry Point #####\n\nMain();\n\n# Temporary code to detect if we're running on a Goobuntu system.\n# These systems don't have the right stuff installed for the special\n# Readline libraries to work, so as a temporary workaround, we default\n# to using the normal stdio code, rather than the fancier readline-based\n# code\nsub ReadlineMightFail {\n  if (-e '/lib/libtermcap.so.2') {\n    return 0;  # libtermcap exists, so readline should be okay\n  } else {\n    return 1;\n  }\n}\n\nsub RunGV {\n  my $fname = shift;\n  my $bg = shift;       # \"\" or \" &\" if we should run in background\n  if (!system(ShellEscape(@GV, \"--version\") . \" >$dev_null 2>&1\")) {\n    # Options using double dash are supported by this gv version.\n    # Also, turn on noantialias to better handle bug in gv for\n    # postscript files with large dimensions.\n    # TODO: Maybe we should not pass the --noantialias flag\n    # if the gv version is known to work properly without the flag.\n    system(ShellEscape(@GV, \"--scale=$main::opt_scale\", \"--noantialias\", $fname)\n           . $bg);\n  } else {\n    # Old gv version - only supports options that use single dash.\n    print STDERR ShellEscape(@GV, \"-scale\", $main::opt_scale) . \"\\n\";\n    system(ShellEscape(@GV, \"-scale\", \"$main::opt_scale\", $fname) . $bg);\n  }\n}\n\nsub RunEvince {\n  my $fname = shift;\n  my $bg = shift;       # \"\" or \" &\" if we should run in background\n  system(ShellEscape(@EVINCE, $fname) . $bg);\n}\n\nsub RunWeb {\n  my $fname = shift;\n  print STDERR \"Loading web page file:///$fname\\n\";\n\n  if (`uname` =~ /Darwin/) {\n    # OS X: open will use standard preference for SVG files.\n    system(\"/usr/bin/open\", $fname);\n    return;\n  }\n\n  # Some kind of Unix; try generic symlinks, then specific browsers.\n  # (Stop once we find one.)\n  # Works best if the browser is already running.\n  my @alt = (\n    \"/etc/alternatives/gnome-www-browser\",\n    \"/etc/alternatives/x-www-browser\",\n    \"google-chrome\",\n    \"firefox\",\n  );\n  foreach my $b (@alt) {\n    if (system($b, $fname) == 0) {\n      return;\n    }\n  }\n\n  print STDERR \"Could not load web browser.\\n\";\n}\n\nsub RunKcachegrind {\n  my $fname = shift;\n  my $bg = shift;       # \"\" or \" &\" if we should run in background\n  print STDERR \"Starting '@KCACHEGRIND \" . $fname . $bg . \"'\\n\";\n  system(ShellEscape(@KCACHEGRIND, $fname) . $bg);\n}\n\n\n##### Interactive helper routines #####\n\nsub InteractiveMode {\n  $| = 1;  # Make output unbuffered for interactive mode\n  my ($orig_profile, $symbols, $libs, $total) = @_;\n\n  print STDERR \"Welcome to jeprof!  For help, type 'help'.\\n\";\n\n  # Use ReadLine if it's installed and input comes from a console.\n  if ( -t STDIN &&\n       !ReadlineMightFail() &&\n       defined(eval {require Term::ReadLine}) ) {\n    my $term = new Term::ReadLine 'jeprof';\n    while ( defined ($_ = $term->readline('(jeprof) '))) {\n      $term->addhistory($_) if /\\S/;\n      if (!InteractiveCommand($orig_profile, $symbols, $libs, $total, $_)) {\n        last;    # exit when we get an interactive command to quit\n      }\n    }\n  } else {       # don't have readline\n    while (1) {\n      print STDERR \"(jeprof) \";\n      $_ = <STDIN>;\n      last if ! defined $_ ;\n      s/\\r//g;         # turn windows-looking lines into unix-looking lines\n\n      # Save some flags that might be reset by InteractiveCommand()\n      my $save_opt_lines = $main::opt_lines;\n\n      if (!InteractiveCommand($orig_profile, $symbols, $libs, $total, $_)) {\n        last;    # exit when we get an interactive command to quit\n      }\n\n      # Restore flags\n      $main::opt_lines = $save_opt_lines;\n    }\n  }\n}\n\n# Takes two args: orig profile, and command to run.\n# Returns 1 if we should keep going, or 0 if we were asked to quit\nsub InteractiveCommand {\n  my($orig_profile, $symbols, $libs, $total, $command) = @_;\n  $_ = $command;                # just to make future m//'s easier\n  if (!defined($_)) {\n    print STDERR \"\\n\";\n    return 0;\n  }\n  if (m/^\\s*quit/) {\n    return 0;\n  }\n  if (m/^\\s*help/) {\n    InteractiveHelpMessage();\n    return 1;\n  }\n  # Clear all the mode options -- mode is controlled by \"$command\"\n  $main::opt_text = 0;\n  $main::opt_callgrind = 0;\n  $main::opt_disasm = 0;\n  $main::opt_list = 0;\n  $main::opt_gv = 0;\n  $main::opt_evince = 0;\n  $main::opt_cum = 0;\n\n  if (m/^\\s*(text|top)(\\d*)\\s*(.*)/) {\n    $main::opt_text = 1;\n\n    my $line_limit = ($2 ne \"\") ? int($2) : 10;\n\n    my $routine;\n    my $ignore;\n    ($routine, $ignore) = ParseInteractiveArgs($3);\n\n    my $profile = ProcessProfile($total, $orig_profile, $symbols, \"\", $ignore);\n    my $reduced = ReduceProfile($symbols, $profile);\n\n    # Get derived profiles\n    my $flat = FlatProfile($reduced);\n    my $cumulative = CumulativeProfile($reduced);\n\n    PrintText($symbols, $flat, $cumulative, $line_limit);\n    return 1;\n  }\n  if (m/^\\s*callgrind\\s*([^ \\n]*)/) {\n    $main::opt_callgrind = 1;\n\n    # Get derived profiles\n    my $calls = ExtractCalls($symbols, $orig_profile);\n    my $filename = $1;\n    if ( $1 eq '' ) {\n      $filename = TempName($main::next_tmpfile, \"callgrind\");\n    }\n    PrintCallgrind($calls, $filename);\n    if ( $1 eq '' ) {\n      RunKcachegrind($filename, \" & \");\n      $main::next_tmpfile++;\n    }\n\n    return 1;\n  }\n  if (m/^\\s*(web)?list\\s*(.+)/) {\n    my $html = (defined($1) && ($1 eq \"web\"));\n    $main::opt_list = 1;\n\n    my $routine;\n    my $ignore;\n    ($routine, $ignore) = ParseInteractiveArgs($2);\n\n    my $profile = ProcessProfile($total, $orig_profile, $symbols, \"\", $ignore);\n    my $reduced = ReduceProfile($symbols, $profile);\n\n    # Get derived profiles\n    my $flat = FlatProfile($reduced);\n    my $cumulative = CumulativeProfile($reduced);\n\n    PrintListing($total, $libs, $flat, $cumulative, $routine, $html);\n    return 1;\n  }\n  if (m/^\\s*disasm\\s*(.+)/) {\n    $main::opt_disasm = 1;\n\n    my $routine;\n    my $ignore;\n    ($routine, $ignore) = ParseInteractiveArgs($1);\n\n    # Process current profile to account for various settings\n    my $profile = ProcessProfile($total, $orig_profile, $symbols, \"\", $ignore);\n    my $reduced = ReduceProfile($symbols, $profile);\n\n    # Get derived profiles\n    my $flat = FlatProfile($reduced);\n    my $cumulative = CumulativeProfile($reduced);\n\n    PrintDisassembly($libs, $flat, $cumulative, $routine);\n    return 1;\n  }\n  if (m/^\\s*(gv|web|evince)\\s*(.*)/) {\n    $main::opt_gv = 0;\n    $main::opt_evince = 0;\n    $main::opt_web = 0;\n    if ($1 eq \"gv\") {\n      $main::opt_gv = 1;\n    } elsif ($1 eq \"evince\") {\n      $main::opt_evince = 1;\n    } elsif ($1 eq \"web\") {\n      $main::opt_web = 1;\n    }\n\n    my $focus;\n    my $ignore;\n    ($focus, $ignore) = ParseInteractiveArgs($2);\n\n    # Process current profile to account for various settings\n    my $profile = ProcessProfile($total, $orig_profile, $symbols,\n                                 $focus, $ignore);\n    my $reduced = ReduceProfile($symbols, $profile);\n\n    # Get derived profiles\n    my $flat = FlatProfile($reduced);\n    my $cumulative = CumulativeProfile($reduced);\n\n    if (PrintDot($main::prog, $symbols, $profile, $flat, $cumulative, $total)) {\n      if ($main::opt_gv) {\n        RunGV(TempName($main::next_tmpfile, \"ps\"), \" &\");\n      } elsif ($main::opt_evince) {\n        RunEvince(TempName($main::next_tmpfile, \"pdf\"), \" &\");\n      } elsif ($main::opt_web) {\n        RunWeb(TempName($main::next_tmpfile, \"svg\"));\n      }\n      $main::next_tmpfile++;\n    }\n    return 1;\n  }\n  if (m/^\\s*$/) {\n    return 1;\n  }\n  print STDERR \"Unknown command: try 'help'.\\n\";\n  return 1;\n}\n\n\nsub ProcessProfile {\n  my $total_count = shift;\n  my $orig_profile = shift;\n  my $symbols = shift;\n  my $focus = shift;\n  my $ignore = shift;\n\n  # Process current profile to account for various settings\n  my $profile = $orig_profile;\n  printf(\"Total: %s %s\\n\", Unparse($total_count), Units());\n  if ($focus ne '') {\n    $profile = FocusProfile($symbols, $profile, $focus);\n    my $focus_count = TotalProfile($profile);\n    printf(\"After focusing on '%s': %s %s of %s (%0.1f%%)\\n\",\n           $focus,\n           Unparse($focus_count), Units(),\n           Unparse($total_count), ($focus_count*100.0) / $total_count);\n  }\n  if ($ignore ne '') {\n    $profile = IgnoreProfile($symbols, $profile, $ignore);\n    my $ignore_count = TotalProfile($profile);\n    printf(\"After ignoring '%s': %s %s of %s (%0.1f%%)\\n\",\n           $ignore,\n           Unparse($ignore_count), Units(),\n           Unparse($total_count),\n           ($ignore_count*100.0) / $total_count);\n  }\n\n  return $profile;\n}\n\nsub InteractiveHelpMessage {\n  print STDERR <<ENDOFHELP;\nInteractive jeprof mode\n\nCommands:\n  gv\n  gv [focus] [-ignore1] [-ignore2]\n      Show graphical hierarchical display of current profile.  Without\n      any arguments, shows all samples in the profile.  With the optional\n      \"focus\" argument, restricts the samples shown to just those where\n      the \"focus\" regular expression matches a routine name on the stack\n      trace.\n\n  web\n  web [focus] [-ignore1] [-ignore2]\n      Like GV, but displays profile in your web browser instead of using\n      Ghostview. Works best if your web browser is already running.\n      To change the browser that gets used:\n      On Linux, set the /etc/alternatives/gnome-www-browser symlink.\n      On OS X, change the Finder association for SVG files.\n\n  list [routine_regexp] [-ignore1] [-ignore2]\n      Show source listing of routines whose names match \"routine_regexp\"\n\n  weblist [routine_regexp] [-ignore1] [-ignore2]\n     Displays a source listing of routines whose names match \"routine_regexp\"\n     in a web browser.  You can click on source lines to view the\n     corresponding disassembly.\n\n  top [--cum] [-ignore1] [-ignore2]\n  top20 [--cum] [-ignore1] [-ignore2]\n  top37 [--cum] [-ignore1] [-ignore2]\n      Show top lines ordered by flat profile count, or cumulative count\n      if --cum is specified.  If a number is present after 'top', the\n      top K routines will be shown (defaults to showing the top 10)\n\n  disasm [routine_regexp] [-ignore1] [-ignore2]\n      Show disassembly of routines whose names match \"routine_regexp\",\n      annotated with sample counts.\n\n  callgrind\n  callgrind [filename]\n      Generates callgrind file. If no filename is given, kcachegrind is called.\n\n  help - This listing\n  quit or ^D - End jeprof\n\nFor commands that accept optional -ignore tags, samples where any routine in\nthe stack trace matches the regular expression in any of the -ignore\nparameters will be ignored.\n\nFurther pprof details are available at this location (or one similar):\n\n /usr/doc/gperftools-$PPROF_VERSION/cpu_profiler.html\n /usr/doc/gperftools-$PPROF_VERSION/heap_profiler.html\n\nENDOFHELP\n}\nsub ParseInteractiveArgs {\n  my $args = shift;\n  my $focus = \"\";\n  my $ignore = \"\";\n  my @x = split(/ +/, $args);\n  foreach $a (@x) {\n    if ($a =~ m/^(--|-)lines$/) {\n      $main::opt_lines = 1;\n    } elsif ($a =~ m/^(--|-)cum$/) {\n      $main::opt_cum = 1;\n    } elsif ($a =~ m/^-(.*)/) {\n      $ignore .= (($ignore ne \"\") ? \"|\" : \"\" ) . $1;\n    } else {\n      $focus .= (($focus ne \"\") ? \"|\" : \"\" ) . $a;\n    }\n  }\n  if ($ignore ne \"\") {\n    print STDERR \"Ignoring samples in call stacks that match '$ignore'\\n\";\n  }\n  return ($focus, $ignore);\n}\n\n##### Output code #####\n\nsub TempName {\n  my $fnum = shift;\n  my $ext = shift;\n  my $file = \"$main::tmpfile_ps.$fnum.$ext\";\n  $main::tempnames{$file} = 1;\n  return $file;\n}\n\n# Print profile data in packed binary format (64-bit) to standard out\nsub PrintProfileData {\n  my $profile = shift;\n\n  # print header (64-bit style)\n  # (zero) (header-size) (version) (sample-period) (zero)\n  print pack('L*', 0, 0, 3, 0, 0, 0, 1, 0, 0, 0);\n\n  foreach my $k (keys(%{$profile})) {\n    my $count = $profile->{$k};\n    my @addrs = split(/\\n/, $k);\n    if ($#addrs >= 0) {\n      my $depth = $#addrs + 1;\n      # int(foo / 2**32) is the only reliable way to get rid of bottom\n      # 32 bits on both 32- and 64-bit systems.\n      print pack('L*', $count & 0xFFFFFFFF, int($count / 2**32));\n      print pack('L*', $depth & 0xFFFFFFFF, int($depth / 2**32));\n\n      foreach my $full_addr (@addrs) {\n        my $addr = $full_addr;\n        $addr =~ s/0x0*//;  # strip off leading 0x, zeroes\n        if (length($addr) > 16) {\n          print STDERR \"Invalid address in profile: $full_addr\\n\";\n          next;\n        }\n        my $low_addr = substr($addr, -8);       # get last 8 hex chars\n        my $high_addr = substr($addr, -16, 8);  # get up to 8 more hex chars\n        print pack('L*', hex('0x' . $low_addr), hex('0x' . $high_addr));\n      }\n    }\n  }\n}\n\n# Print symbols and profile data\nsub PrintSymbolizedProfile {\n  my $symbols = shift;\n  my $profile = shift;\n  my $prog = shift;\n\n  $SYMBOL_PAGE =~ m,[^/]+$,;    # matches everything after the last slash\n  my $symbol_marker = $&;\n\n  print '--- ', $symbol_marker, \"\\n\";\n  if (defined($prog)) {\n    print 'binary=', $prog, \"\\n\";\n  }\n  while (my ($pc, $name) = each(%{$symbols})) {\n    my $sep = ' ';\n    print '0x', $pc;\n    # We have a list of function names, which include the inlined\n    # calls.  They are separated (and terminated) by --, which is\n    # illegal in function names.\n    for (my $j = 2; $j <= $#{$name}; $j += 3) {\n      print $sep, $name->[$j];\n      $sep = '--';\n    }\n    print \"\\n\";\n  }\n  print '---', \"\\n\";\n\n  my $profile_marker;\n  if ($main::profile_type eq 'heap') {\n    $HEAP_PAGE =~ m,[^/]+$,;    # matches everything after the last slash\n    $profile_marker = $&;\n  } elsif ($main::profile_type eq 'growth') {\n    $GROWTH_PAGE =~ m,[^/]+$,;    # matches everything after the last slash\n    $profile_marker = $&;\n  } elsif ($main::profile_type eq 'contention') {\n    $CONTENTION_PAGE =~ m,[^/]+$,;    # matches everything after the last slash\n    $profile_marker = $&;\n  } else { # elsif ($main::profile_type eq 'cpu')\n    $PROFILE_PAGE =~ m,[^/]+$,;    # matches everything after the last slash\n    $profile_marker = $&;\n  }\n\n  print '--- ', $profile_marker, \"\\n\";\n  if (defined($main::collected_profile)) {\n    # if used with remote fetch, simply dump the collected profile to output.\n    open(SRC, \"<$main::collected_profile\");\n    while (<SRC>) {\n      print $_;\n    }\n    close(SRC);\n  } else {\n    # --raw/http: For everything to work correctly for non-remote profiles, we\n    # would need to extend PrintProfileData() to handle all possible profile\n    # types, re-enable the code that is currently disabled in ReadCPUProfile()\n    # and FixCallerAddresses(), and remove the remote profile dumping code in\n    # the block above.\n    die \"--raw/http: jeprof can only dump remote profiles for --raw\\n\";\n    # dump a cpu-format profile to standard out\n    PrintProfileData($profile);\n  }\n}\n\n# Print text output\nsub PrintText {\n  my $symbols = shift;\n  my $flat = shift;\n  my $cumulative = shift;\n  my $line_limit = shift;\n\n  my $total = TotalProfile($flat);\n\n  # Which profile to sort by?\n  my $s = $main::opt_cum ? $cumulative : $flat;\n\n  my $running_sum = 0;\n  my $lines = 0;\n  foreach my $k (sort { GetEntry($s, $b) <=> GetEntry($s, $a) || $a cmp $b }\n                 keys(%{$cumulative})) {\n    my $f = GetEntry($flat, $k);\n    my $c = GetEntry($cumulative, $k);\n    $running_sum += $f;\n\n    my $sym = $k;\n    if (exists($symbols->{$k})) {\n      $sym = $symbols->{$k}->[0] . \" \" . $symbols->{$k}->[1];\n      if ($main::opt_addresses) {\n        $sym = $k . \" \" . $sym;\n      }\n    }\n\n    if ($f != 0 || $c != 0) {\n      printf(\"%8s %6s %6s %8s %6s %s\\n\",\n             Unparse($f),\n             Percent($f, $total),\n             Percent($running_sum, $total),\n             Unparse($c),\n             Percent($c, $total),\n             $sym);\n    }\n    $lines++;\n    last if ($line_limit >= 0 && $lines >= $line_limit);\n  }\n}\n\n# Callgrind format has a compression for repeated function and file\n# names.  You show the name the first time, and just use its number\n# subsequently.  This can cut down the file to about a third or a\n# quarter of its uncompressed size.  $key and $val are the key/value\n# pair that would normally be printed by callgrind; $map is a map from\n# value to number.\nsub CompressedCGName {\n  my($key, $val, $map) = @_;\n  my $idx = $map->{$val};\n  # For very short keys, providing an index hurts rather than helps.\n  if (length($val) <= 3) {\n    return \"$key=$val\\n\";\n  } elsif (defined($idx)) {\n    return \"$key=($idx)\\n\";\n  } else {\n    # scalar(keys $map) gives the number of items in the map.\n    $idx = scalar(keys(%{$map})) + 1;\n    $map->{$val} = $idx;\n    return \"$key=($idx) $val\\n\";\n  }\n}\n\n# Print the call graph in a way that's suiteable for callgrind.\nsub PrintCallgrind {\n  my $calls = shift;\n  my $filename;\n  my %filename_to_index_map;\n  my %fnname_to_index_map;\n\n  if ($main::opt_interactive) {\n    $filename = shift;\n    print STDERR \"Writing callgrind file to '$filename'.\\n\"\n  } else {\n    $filename = \"&STDOUT\";\n  }\n  open(CG, \">$filename\");\n  printf CG (\"events: Hits\\n\\n\");\n  foreach my $call ( map { $_->[0] }\n                     sort { $a->[1] cmp $b ->[1] ||\n                            $a->[2] <=> $b->[2] }\n                     map { /([^:]+):(\\d+):([^ ]+)( -> ([^:]+):(\\d+):(.+))?/;\n                           [$_, $1, $2] }\n                     keys %$calls ) {\n    my $count = int($calls->{$call});\n    $call =~ /([^:]+):(\\d+):([^ ]+)( -> ([^:]+):(\\d+):(.+))?/;\n    my ( $caller_file, $caller_line, $caller_function,\n         $callee_file, $callee_line, $callee_function ) =\n       ( $1, $2, $3, $5, $6, $7 );\n\n    # TODO(csilvers): for better compression, collect all the\n    # caller/callee_files and functions first, before printing\n    # anything, and only compress those referenced more than once.\n    printf CG CompressedCGName(\"fl\", $caller_file, \\%filename_to_index_map);\n    printf CG CompressedCGName(\"fn\", $caller_function, \\%fnname_to_index_map);\n    if (defined $6) {\n      printf CG CompressedCGName(\"cfl\", $callee_file, \\%filename_to_index_map);\n      printf CG CompressedCGName(\"cfn\", $callee_function, \\%fnname_to_index_map);\n      printf CG (\"calls=$count $callee_line\\n\");\n    }\n    printf CG (\"$caller_line $count\\n\\n\");\n  }\n}\n\n# Print disassembly for all all routines that match $main::opt_disasm\nsub PrintDisassembly {\n  my $libs = shift;\n  my $flat = shift;\n  my $cumulative = shift;\n  my $disasm_opts = shift;\n\n  my $total = TotalProfile($flat);\n\n  foreach my $lib (@{$libs}) {\n    my $symbol_table = GetProcedureBoundaries($lib->[0], $disasm_opts);\n    my $offset = AddressSub($lib->[1], $lib->[3]);\n    foreach my $routine (sort ByName keys(%{$symbol_table})) {\n      my $start_addr = $symbol_table->{$routine}->[0];\n      my $end_addr = $symbol_table->{$routine}->[1];\n      # See if there are any samples in this routine\n      my $length = hex(AddressSub($end_addr, $start_addr));\n      my $addr = AddressAdd($start_addr, $offset);\n      for (my $i = 0; $i < $length; $i++) {\n        if (defined($cumulative->{$addr})) {\n          PrintDisassembledFunction($lib->[0], $offset,\n                                    $routine, $flat, $cumulative,\n                                    $start_addr, $end_addr, $total);\n          last;\n        }\n        $addr = AddressInc($addr);\n      }\n    }\n  }\n}\n\n# Return reference to array of tuples of the form:\n#       [start_address, filename, linenumber, instruction, limit_address]\n# E.g.,\n#       [\"0x806c43d\", \"/foo/bar.cc\", 131, \"ret\", \"0x806c440\"]\nsub Disassemble {\n  my $prog = shift;\n  my $offset = shift;\n  my $start_addr = shift;\n  my $end_addr = shift;\n\n  my $objdump = $obj_tool_map{\"objdump\"};\n  my $cmd = ShellEscape($objdump, \"-C\", \"-d\", \"-l\", \"--no-show-raw-insn\",\n                        \"--start-address=0x$start_addr\",\n                        \"--stop-address=0x$end_addr\", $prog);\n  open(OBJDUMP, \"$cmd |\") || error(\"$cmd: $!\\n\");\n  my @result = ();\n  my $filename = \"\";\n  my $linenumber = -1;\n  my $last = [\"\", \"\", \"\", \"\"];\n  while (<OBJDUMP>) {\n    s/\\r//g;         # turn windows-looking lines into unix-looking lines\n    chop;\n    if (m|\\s*([^:\\s]+):(\\d+)\\s*$|) {\n      # Location line of the form:\n      #   <filename>:<linenumber>\n      $filename = $1;\n      $linenumber = $2;\n    } elsif (m/^ +([0-9a-f]+):\\s*(.*)/) {\n      # Disassembly line -- zero-extend address to full length\n      my $addr = HexExtend($1);\n      my $k = AddressAdd($addr, $offset);\n      $last->[4] = $k;   # Store ending address for previous instruction\n      $last = [$k, $filename, $linenumber, $2, $end_addr];\n      push(@result, $last);\n    }\n  }\n  close(OBJDUMP);\n  return @result;\n}\n\n# The input file should contain lines of the form /proc/maps-like\n# output (same format as expected from the profiles) or that looks\n# like hex addresses (like \"0xDEADBEEF\").  We will parse all\n# /proc/maps output, and for all the hex addresses, we will output\n# \"short\" symbol names, one per line, in the same order as the input.\nsub PrintSymbols {\n  my $maps_and_symbols_file = shift;\n\n  # ParseLibraries expects pcs to be in a set.  Fine by us...\n  my @pclist = ();   # pcs in sorted order\n  my $pcs = {};\n  my $map = \"\";\n  foreach my $line (<$maps_and_symbols_file>) {\n    $line =~ s/\\r//g;    # turn windows-looking lines into unix-looking lines\n    if ($line =~ /\\b(0x[0-9a-f]+)\\b/i) {\n      push(@pclist, HexExtend($1));\n      $pcs->{$pclist[-1]} = 1;\n    } else {\n      $map .= $line;\n    }\n  }\n\n  my $libs = ParseLibraries($main::prog, $map, $pcs);\n  my $symbols = ExtractSymbols($libs, $pcs);\n\n  foreach my $pc (@pclist) {\n    # ->[0] is the shortname, ->[2] is the full name\n    print(($symbols->{$pc}->[0] || \"??\") . \"\\n\");\n  }\n}\n\n\n# For sorting functions by name\nsub ByName {\n  return ShortFunctionName($a) cmp ShortFunctionName($b);\n}\n\n# Print source-listing for all all routines that match $list_opts\nsub PrintListing {\n  my $total = shift;\n  my $libs = shift;\n  my $flat = shift;\n  my $cumulative = shift;\n  my $list_opts = shift;\n  my $html = shift;\n\n  my $output = \\*STDOUT;\n  my $fname = \"\";\n\n  if ($html) {\n    # Arrange to write the output to a temporary file\n    $fname = TempName($main::next_tmpfile, \"html\");\n    $main::next_tmpfile++;\n    if (!open(TEMP, \">$fname\")) {\n      print STDERR \"$fname: $!\\n\";\n      return;\n    }\n    $output = \\*TEMP;\n    print $output HtmlListingHeader();\n    printf $output (\"<div class=\\\"legend\\\">%s<br>Total: %s %s</div>\\n\",\n                    $main::prog, Unparse($total), Units());\n  }\n\n  my $listed = 0;\n  foreach my $lib (@{$libs}) {\n    my $symbol_table = GetProcedureBoundaries($lib->[0], $list_opts);\n    my $offset = AddressSub($lib->[1], $lib->[3]);\n    foreach my $routine (sort ByName keys(%{$symbol_table})) {\n      # Print if there are any samples in this routine\n      my $start_addr = $symbol_table->{$routine}->[0];\n      my $end_addr = $symbol_table->{$routine}->[1];\n      my $length = hex(AddressSub($end_addr, $start_addr));\n      my $addr = AddressAdd($start_addr, $offset);\n      for (my $i = 0; $i < $length; $i++) {\n        if (defined($cumulative->{$addr})) {\n          $listed += PrintSource(\n            $lib->[0], $offset,\n            $routine, $flat, $cumulative,\n            $start_addr, $end_addr,\n            $html,\n            $output);\n          last;\n        }\n        $addr = AddressInc($addr);\n      }\n    }\n  }\n\n  if ($html) {\n    if ($listed > 0) {\n      print $output HtmlListingFooter();\n      close($output);\n      RunWeb($fname);\n    } else {\n      close($output);\n      unlink($fname);\n    }\n  }\n}\n\nsub HtmlListingHeader {\n  return <<'EOF';\n<DOCTYPE html>\n<html>\n<head>\n<title>Pprof listing</title>\n<style type=\"text/css\">\nbody {\n  font-family: sans-serif;\n}\nh1 {\n  font-size: 1.5em;\n  margin-bottom: 4px;\n}\n.legend {\n  font-size: 1.25em;\n}\n.line {\n  color: #aaaaaa;\n}\n.nop {\n  color: #aaaaaa;\n}\n.unimportant {\n  color: #cccccc;\n}\n.disasmloc {\n  color: #000000;\n}\n.deadsrc {\n  cursor: pointer;\n}\n.deadsrc:hover {\n  background-color: #eeeeee;\n}\n.livesrc {\n  color: #0000ff;\n  cursor: pointer;\n}\n.livesrc:hover {\n  background-color: #eeeeee;\n}\n.asm {\n  color: #008800;\n  display: none;\n}\n</style>\n<script type=\"text/javascript\">\nfunction jeprof_toggle_asm(e) {\n  var target;\n  if (!e) e = window.event;\n  if (e.target) target = e.target;\n  else if (e.srcElement) target = e.srcElement;\n\n  if (target) {\n    var asm = target.nextSibling;\n    if (asm && asm.className == \"asm\") {\n      asm.style.display = (asm.style.display == \"block\" ? \"\" : \"block\");\n      e.preventDefault();\n      return false;\n    }\n  }\n}\n</script>\n</head>\n<body>\nEOF\n}\n\nsub HtmlListingFooter {\n  return <<'EOF';\n</body>\n</html>\nEOF\n}\n\nsub HtmlEscape {\n  my $text = shift;\n  $text =~ s/&/&amp;/g;\n  $text =~ s/</&lt;/g;\n  $text =~ s/>/&gt;/g;\n  return $text;\n}\n\n# Returns the indentation of the line, if it has any non-whitespace\n# characters.  Otherwise, returns -1.\nsub Indentation {\n  my $line = shift;\n  if (m/^(\\s*)\\S/) {\n    return length($1);\n  } else {\n    return -1;\n  }\n}\n\n# If the symbol table contains inlining info, Disassemble() may tag an\n# instruction with a location inside an inlined function.  But for\n# source listings, we prefer to use the location in the function we\n# are listing.  So use MapToSymbols() to fetch full location\n# information for each instruction and then pick out the first\n# location from a location list (location list contains callers before\n# callees in case of inlining).\n#\n# After this routine has run, each entry in $instructions contains:\n#   [0] start address\n#   [1] filename for function we are listing\n#   [2] line number for function we are listing\n#   [3] disassembly\n#   [4] limit address\n#   [5] most specific filename (may be different from [1] due to inlining)\n#   [6] most specific line number (may be different from [2] due to inlining)\nsub GetTopLevelLineNumbers {\n  my ($lib, $offset, $instructions) = @_;\n  my $pcs = [];\n  for (my $i = 0; $i <= $#{$instructions}; $i++) {\n    push(@{$pcs}, $instructions->[$i]->[0]);\n  }\n  my $symbols = {};\n  MapToSymbols($lib, $offset, $pcs, $symbols);\n  for (my $i = 0; $i <= $#{$instructions}; $i++) {\n    my $e = $instructions->[$i];\n    push(@{$e}, $e->[1]);\n    push(@{$e}, $e->[2]);\n    my $addr = $e->[0];\n    my $sym = $symbols->{$addr};\n    if (defined($sym)) {\n      if ($#{$sym} >= 2 && $sym->[1] =~ m/^(.*):(\\d+)$/) {\n        $e->[1] = $1;  # File name\n        $e->[2] = $2;  # Line number\n      }\n    }\n  }\n}\n\n# Print source-listing for one routine\nsub PrintSource {\n  my $prog = shift;\n  my $offset = shift;\n  my $routine = shift;\n  my $flat = shift;\n  my $cumulative = shift;\n  my $start_addr = shift;\n  my $end_addr = shift;\n  my $html = shift;\n  my $output = shift;\n\n  # Disassemble all instructions (just to get line numbers)\n  my @instructions = Disassemble($prog, $offset, $start_addr, $end_addr);\n  GetTopLevelLineNumbers($prog, $offset, \\@instructions);\n\n  # Hack 1: assume that the first source file encountered in the\n  # disassembly contains the routine\n  my $filename = undef;\n  for (my $i = 0; $i <= $#instructions; $i++) {\n    if ($instructions[$i]->[2] >= 0) {\n      $filename = $instructions[$i]->[1];\n      last;\n    }\n  }\n  if (!defined($filename)) {\n    print STDERR \"no filename found in $routine\\n\";\n    return 0;\n  }\n\n  # Hack 2: assume that the largest line number from $filename is the\n  # end of the procedure.  This is typically safe since if P1 contains\n  # an inlined call to P2, then P2 usually occurs earlier in the\n  # source file.  If this does not work, we might have to compute a\n  # density profile or just print all regions we find.\n  my $lastline = 0;\n  for (my $i = 0; $i <= $#instructions; $i++) {\n    my $f = $instructions[$i]->[1];\n    my $l = $instructions[$i]->[2];\n    if (($f eq $filename) && ($l > $lastline)) {\n      $lastline = $l;\n    }\n  }\n\n  # Hack 3: assume the first source location from \"filename\" is the start of\n  # the source code.\n  my $firstline = 1;\n  for (my $i = 0; $i <= $#instructions; $i++) {\n    if ($instructions[$i]->[1] eq $filename) {\n      $firstline = $instructions[$i]->[2];\n      last;\n    }\n  }\n\n  # Hack 4: Extend last line forward until its indentation is less than\n  # the indentation we saw on $firstline\n  my $oldlastline = $lastline;\n  {\n    if (!open(FILE, \"<$filename\")) {\n      print STDERR \"$filename: $!\\n\";\n      return 0;\n    }\n    my $l = 0;\n    my $first_indentation = -1;\n    while (<FILE>) {\n      s/\\r//g;         # turn windows-looking lines into unix-looking lines\n      $l++;\n      my $indent = Indentation($_);\n      if ($l >= $firstline) {\n        if ($first_indentation < 0 && $indent >= 0) {\n          $first_indentation = $indent;\n          last if ($first_indentation == 0);\n        }\n      }\n      if ($l >= $lastline && $indent >= 0) {\n        if ($indent >= $first_indentation) {\n          $lastline = $l+1;\n        } else {\n          last;\n        }\n      }\n    }\n    close(FILE);\n  }\n\n  # Assign all samples to the range $firstline,$lastline,\n  # Hack 4: If an instruction does not occur in the range, its samples\n  # are moved to the next instruction that occurs in the range.\n  my $samples1 = {};        # Map from line number to flat count\n  my $samples2 = {};        # Map from line number to cumulative count\n  my $running1 = 0;         # Unassigned flat counts\n  my $running2 = 0;         # Unassigned cumulative counts\n  my $total1 = 0;           # Total flat counts\n  my $total2 = 0;           # Total cumulative counts\n  my %disasm = ();          # Map from line number to disassembly\n  my $running_disasm = \"\";  # Unassigned disassembly\n  my $skip_marker = \"---\\n\";\n  if ($html) {\n    $skip_marker = \"\";\n    for (my $l = $firstline; $l <= $lastline; $l++) {\n      $disasm{$l} = \"\";\n    }\n  }\n  my $last_dis_filename = '';\n  my $last_dis_linenum = -1;\n  my $last_touched_line = -1;  # To detect gaps in disassembly for a line\n  foreach my $e (@instructions) {\n    # Add up counts for all address that fall inside this instruction\n    my $c1 = 0;\n    my $c2 = 0;\n    for (my $a = $e->[0]; $a lt $e->[4]; $a = AddressInc($a)) {\n      $c1 += GetEntry($flat, $a);\n      $c2 += GetEntry($cumulative, $a);\n    }\n\n    if ($html) {\n      my $dis = sprintf(\"      %6s %6s \\t\\t%8s: %s \",\n                        HtmlPrintNumber($c1),\n                        HtmlPrintNumber($c2),\n                        UnparseAddress($offset, $e->[0]),\n                        CleanDisassembly($e->[3]));\n\n      # Append the most specific source line associated with this instruction\n      if (length($dis) < 80) { $dis .= (' ' x (80 - length($dis))) };\n      $dis = HtmlEscape($dis);\n      my $f = $e->[5];\n      my $l = $e->[6];\n      if ($f ne $last_dis_filename) {\n        $dis .= sprintf(\"<span class=disasmloc>%s:%d</span>\",\n                        HtmlEscape(CleanFileName($f)), $l);\n      } elsif ($l ne $last_dis_linenum) {\n        # De-emphasize the unchanged file name portion\n        $dis .= sprintf(\"<span class=unimportant>%s</span>\" .\n                        \"<span class=disasmloc>:%d</span>\",\n                        HtmlEscape(CleanFileName($f)), $l);\n      } else {\n        # De-emphasize the entire location\n        $dis .= sprintf(\"<span class=unimportant>%s:%d</span>\",\n                        HtmlEscape(CleanFileName($f)), $l);\n      }\n      $last_dis_filename = $f;\n      $last_dis_linenum = $l;\n      $running_disasm .= $dis;\n      $running_disasm .= \"\\n\";\n    }\n\n    $running1 += $c1;\n    $running2 += $c2;\n    $total1 += $c1;\n    $total2 += $c2;\n    my $file = $e->[1];\n    my $line = $e->[2];\n    if (($file eq $filename) &&\n        ($line >= $firstline) &&\n        ($line <= $lastline)) {\n      # Assign all accumulated samples to this line\n      AddEntry($samples1, $line, $running1);\n      AddEntry($samples2, $line, $running2);\n      $running1 = 0;\n      $running2 = 0;\n      if ($html) {\n        if ($line != $last_touched_line && $disasm{$line} ne '') {\n          $disasm{$line} .= \"\\n\";\n        }\n        $disasm{$line} .= $running_disasm;\n        $running_disasm = '';\n        $last_touched_line = $line;\n      }\n    }\n  }\n\n  # Assign any leftover samples to $lastline\n  AddEntry($samples1, $lastline, $running1);\n  AddEntry($samples2, $lastline, $running2);\n  if ($html) {\n    if ($lastline != $last_touched_line && $disasm{$lastline} ne '') {\n      $disasm{$lastline} .= \"\\n\";\n    }\n    $disasm{$lastline} .= $running_disasm;\n  }\n\n  if ($html) {\n    printf $output (\n      \"<h1>%s</h1>%s\\n<pre onClick=\\\"jeprof_toggle_asm()\\\">\\n\" .\n      \"Total:%6s %6s (flat / cumulative %s)\\n\",\n      HtmlEscape(ShortFunctionName($routine)),\n      HtmlEscape(CleanFileName($filename)),\n      Unparse($total1),\n      Unparse($total2),\n      Units());\n  } else {\n    printf $output (\n      \"ROUTINE ====================== %s in %s\\n\" .\n      \"%6s %6s Total %s (flat / cumulative)\\n\",\n      ShortFunctionName($routine),\n      CleanFileName($filename),\n      Unparse($total1),\n      Unparse($total2),\n      Units());\n  }\n  if (!open(FILE, \"<$filename\")) {\n    print STDERR \"$filename: $!\\n\";\n    return 0;\n  }\n  my $l = 0;\n  while (<FILE>) {\n    s/\\r//g;         # turn windows-looking lines into unix-looking lines\n    $l++;\n    if ($l >= $firstline - 5 &&\n        (($l <= $oldlastline + 5) || ($l <= $lastline))) {\n      chop;\n      my $text = $_;\n      if ($l == $firstline) { print $output $skip_marker; }\n      my $n1 = GetEntry($samples1, $l);\n      my $n2 = GetEntry($samples2, $l);\n      if ($html) {\n        # Emit a span that has one of the following classes:\n        #    livesrc -- has samples\n        #    deadsrc -- has disassembly, but with no samples\n        #    nop     -- has no matching disasembly\n        # Also emit an optional span containing disassembly.\n        my $dis = $disasm{$l};\n        my $asm = \"\";\n        if (defined($dis) && $dis ne '') {\n          $asm = \"<span class=\\\"asm\\\">\" . $dis . \"</span>\";\n        }\n        my $source_class = (($n1 + $n2 > 0)\n                            ? \"livesrc\"\n                            : (($asm ne \"\") ? \"deadsrc\" : \"nop\"));\n        printf $output (\n          \"<span class=\\\"line\\\">%5d</span> \" .\n          \"<span class=\\\"%s\\\">%6s %6s %s</span>%s\\n\",\n          $l, $source_class,\n          HtmlPrintNumber($n1),\n          HtmlPrintNumber($n2),\n          HtmlEscape($text),\n          $asm);\n      } else {\n        printf $output(\n          \"%6s %6s %4d: %s\\n\",\n          UnparseAlt($n1),\n          UnparseAlt($n2),\n          $l,\n          $text);\n      }\n      if ($l == $lastline)  { print $output $skip_marker; }\n    };\n  }\n  close(FILE);\n  if ($html) {\n    print $output \"</pre>\\n\";\n  }\n  return 1;\n}\n\n# Return the source line for the specified file/linenumber.\n# Returns undef if not found.\nsub SourceLine {\n  my $file = shift;\n  my $line = shift;\n\n  # Look in cache\n  if (!defined($main::source_cache{$file})) {\n    if (100 < scalar keys(%main::source_cache)) {\n      # Clear the cache when it gets too big\n      $main::source_cache = ();\n    }\n\n    # Read all lines from the file\n    if (!open(FILE, \"<$file\")) {\n      print STDERR \"$file: $!\\n\";\n      $main::source_cache{$file} = [];  # Cache the negative result\n      return undef;\n    }\n    my $lines = [];\n    push(@{$lines}, \"\");        # So we can use 1-based line numbers as indices\n    while (<FILE>) {\n      push(@{$lines}, $_);\n    }\n    close(FILE);\n\n    # Save the lines in the cache\n    $main::source_cache{$file} = $lines;\n  }\n\n  my $lines = $main::source_cache{$file};\n  if (($line < 0) || ($line > $#{$lines})) {\n    return undef;\n  } else {\n    return $lines->[$line];\n  }\n}\n\n# Print disassembly for one routine with interspersed source if available\nsub PrintDisassembledFunction {\n  my $prog = shift;\n  my $offset = shift;\n  my $routine = shift;\n  my $flat = shift;\n  my $cumulative = shift;\n  my $start_addr = shift;\n  my $end_addr = shift;\n  my $total = shift;\n\n  # Disassemble all instructions\n  my @instructions = Disassemble($prog, $offset, $start_addr, $end_addr);\n\n  # Make array of counts per instruction\n  my @flat_count = ();\n  my @cum_count = ();\n  my $flat_total = 0;\n  my $cum_total = 0;\n  foreach my $e (@instructions) {\n    # Add up counts for all address that fall inside this instruction\n    my $c1 = 0;\n    my $c2 = 0;\n    for (my $a = $e->[0]; $a lt $e->[4]; $a = AddressInc($a)) {\n      $c1 += GetEntry($flat, $a);\n      $c2 += GetEntry($cumulative, $a);\n    }\n    push(@flat_count, $c1);\n    push(@cum_count, $c2);\n    $flat_total += $c1;\n    $cum_total += $c2;\n  }\n\n  # Print header with total counts\n  printf(\"ROUTINE ====================== %s\\n\" .\n         \"%6s %6s %s (flat, cumulative) %.1f%% of total\\n\",\n         ShortFunctionName($routine),\n         Unparse($flat_total),\n         Unparse($cum_total),\n         Units(),\n         ($cum_total * 100.0) / $total);\n\n  # Process instructions in order\n  my $current_file = \"\";\n  for (my $i = 0; $i <= $#instructions; ) {\n    my $e = $instructions[$i];\n\n    # Print the new file name whenever we switch files\n    if ($e->[1] ne $current_file) {\n      $current_file = $e->[1];\n      my $fname = $current_file;\n      $fname =~ s|^\\./||;   # Trim leading \"./\"\n\n      # Shorten long file names\n      if (length($fname) >= 58) {\n        $fname = \"...\" . substr($fname, -55);\n      }\n      printf(\"-------------------- %s\\n\", $fname);\n    }\n\n    # TODO: Compute range of lines to print together to deal with\n    # small reorderings.\n    my $first_line = $e->[2];\n    my $last_line = $first_line;\n    my %flat_sum = ();\n    my %cum_sum = ();\n    for (my $l = $first_line; $l <= $last_line; $l++) {\n      $flat_sum{$l} = 0;\n      $cum_sum{$l} = 0;\n    }\n\n    # Find run of instructions for this range of source lines\n    my $first_inst = $i;\n    while (($i <= $#instructions) &&\n           ($instructions[$i]->[2] >= $first_line) &&\n           ($instructions[$i]->[2] <= $last_line)) {\n      $e = $instructions[$i];\n      $flat_sum{$e->[2]} += $flat_count[$i];\n      $cum_sum{$e->[2]} += $cum_count[$i];\n      $i++;\n    }\n    my $last_inst = $i - 1;\n\n    # Print source lines\n    for (my $l = $first_line; $l <= $last_line; $l++) {\n      my $line = SourceLine($current_file, $l);\n      if (!defined($line)) {\n        $line = \"?\\n\";\n        next;\n      } else {\n        $line =~ s/^\\s+//;\n      }\n      printf(\"%6s %6s %5d: %s\",\n             UnparseAlt($flat_sum{$l}),\n             UnparseAlt($cum_sum{$l}),\n             $l,\n             $line);\n    }\n\n    # Print disassembly\n    for (my $x = $first_inst; $x <= $last_inst; $x++) {\n      my $e = $instructions[$x];\n      printf(\"%6s %6s    %8s: %6s\\n\",\n             UnparseAlt($flat_count[$x]),\n             UnparseAlt($cum_count[$x]),\n             UnparseAddress($offset, $e->[0]),\n             CleanDisassembly($e->[3]));\n    }\n  }\n}\n\n# Print DOT graph\nsub PrintDot {\n  my $prog = shift;\n  my $symbols = shift;\n  my $raw = shift;\n  my $flat = shift;\n  my $cumulative = shift;\n  my $overall_total = shift;\n\n  # Get total\n  my $local_total = TotalProfile($flat);\n  my $nodelimit = int($main::opt_nodefraction * $local_total);\n  my $edgelimit = int($main::opt_edgefraction * $local_total);\n  my $nodecount = $main::opt_nodecount;\n\n  # Find nodes to include\n  my @list = (sort { abs(GetEntry($cumulative, $b)) <=>\n                     abs(GetEntry($cumulative, $a))\n                     || $a cmp $b }\n              keys(%{$cumulative}));\n  my $last = $nodecount - 1;\n  if ($last > $#list) {\n    $last = $#list;\n  }\n  while (($last >= 0) &&\n         (abs(GetEntry($cumulative, $list[$last])) <= $nodelimit)) {\n    $last--;\n  }\n  if ($last < 0) {\n    print STDERR \"No nodes to print\\n\";\n    return 0;\n  }\n\n  if ($nodelimit > 0 || $edgelimit > 0) {\n    printf STDERR (\"Dropping nodes with <= %s %s; edges with <= %s abs(%s)\\n\",\n                   Unparse($nodelimit), Units(),\n                   Unparse($edgelimit), Units());\n  }\n\n  # Open DOT output file\n  my $output;\n  my $escaped_dot = ShellEscape(@DOT);\n  my $escaped_ps2pdf = ShellEscape(@PS2PDF);\n  if ($main::opt_gv) {\n    my $escaped_outfile = ShellEscape(TempName($main::next_tmpfile, \"ps\"));\n    $output = \"| $escaped_dot -Tps2 >$escaped_outfile\";\n  } elsif ($main::opt_evince) {\n    my $escaped_outfile = ShellEscape(TempName($main::next_tmpfile, \"pdf\"));\n    $output = \"| $escaped_dot -Tps2 | $escaped_ps2pdf - $escaped_outfile\";\n  } elsif ($main::opt_ps) {\n    $output = \"| $escaped_dot -Tps2\";\n  } elsif ($main::opt_pdf) {\n    $output = \"| $escaped_dot -Tps2 | $escaped_ps2pdf - -\";\n  } elsif ($main::opt_web || $main::opt_svg) {\n    # We need to post-process the SVG, so write to a temporary file always.\n    my $escaped_outfile = ShellEscape(TempName($main::next_tmpfile, \"svg\"));\n    $output = \"| $escaped_dot -Tsvg >$escaped_outfile\";\n  } elsif ($main::opt_gif) {\n    $output = \"| $escaped_dot -Tgif\";\n  } else {\n    $output = \">&STDOUT\";\n  }\n  open(DOT, $output) || error(\"$output: $!\\n\");\n\n  # Title\n  printf DOT (\"digraph \\\"%s; %s %s\\\" {\\n\",\n              $prog,\n              Unparse($overall_total),\n              Units());\n  if ($main::opt_pdf) {\n    # The output is more printable if we set the page size for dot.\n    printf DOT (\"size=\\\"8,11\\\"\\n\");\n  }\n  printf DOT (\"node [width=0.375,height=0.25];\\n\");\n\n  # Print legend\n  printf DOT (\"Legend [shape=box,fontsize=24,shape=plaintext,\" .\n              \"label=\\\"%s\\\\l%s\\\\l%s\\\\l%s\\\\l%s\\\\l\\\"];\\n\",\n              $prog,\n              sprintf(\"Total %s: %s\", Units(), Unparse($overall_total)),\n              sprintf(\"Focusing on: %s\", Unparse($local_total)),\n              sprintf(\"Dropped nodes with <= %s abs(%s)\",\n                      Unparse($nodelimit), Units()),\n              sprintf(\"Dropped edges with <= %s %s\",\n                      Unparse($edgelimit), Units())\n              );\n\n  # Print nodes\n  my %node = ();\n  my $nextnode = 1;\n  foreach my $a (@list[0..$last]) {\n    # Pick font size\n    my $f = GetEntry($flat, $a);\n    my $c = GetEntry($cumulative, $a);\n\n    my $fs = 8;\n    if ($local_total > 0) {\n      $fs = 8 + (50.0 * sqrt(abs($f * 1.0 / $local_total)));\n    }\n\n    $node{$a} = $nextnode++;\n    my $sym = $a;\n    $sym =~ s/\\s+/\\\\n/g;\n    $sym =~ s/::/\\\\n/g;\n\n    # Extra cumulative info to print for non-leaves\n    my $extra = \"\";\n    if ($f != $c) {\n      $extra = sprintf(\"\\\\rof %s (%s)\",\n                       Unparse($c),\n                       Percent($c, $local_total));\n    }\n    my $style = \"\";\n    if ($main::opt_heapcheck) {\n      if ($f > 0) {\n        # make leak-causing nodes more visible (add a background)\n        $style = \",style=filled,fillcolor=gray\"\n      } elsif ($f < 0) {\n        # make anti-leak-causing nodes (which almost never occur)\n        # stand out as well (triple border)\n        $style = \",peripheries=3\"\n      }\n    }\n\n    printf DOT (\"N%d [label=\\\"%s\\\\n%s (%s)%s\\\\r\" .\n                \"\\\",shape=box,fontsize=%.1f%s];\\n\",\n                $node{$a},\n                $sym,\n                Unparse($f),\n                Percent($f, $local_total),\n                $extra,\n                $fs,\n                $style,\n               );\n  }\n\n  # Get edges and counts per edge\n  my %edge = ();\n  my $n;\n  my $fullname_to_shortname_map = {};\n  FillFullnameToShortnameMap($symbols, $fullname_to_shortname_map);\n  foreach my $k (keys(%{$raw})) {\n    # TODO: omit low %age edges\n    $n = $raw->{$k};\n    my @translated = TranslateStack($symbols, $fullname_to_shortname_map, $k);\n    for (my $i = 1; $i <= $#translated; $i++) {\n      my $src = $translated[$i];\n      my $dst = $translated[$i-1];\n      #next if ($src eq $dst);  # Avoid self-edges?\n      if (exists($node{$src}) && exists($node{$dst})) {\n        my $edge_label = \"$src\\001$dst\";\n        if (!exists($edge{$edge_label})) {\n          $edge{$edge_label} = 0;\n        }\n        $edge{$edge_label} += $n;\n      }\n    }\n  }\n\n  # Print edges (process in order of decreasing counts)\n  my %indegree = ();   # Number of incoming edges added per node so far\n  my %outdegree = ();  # Number of outgoing edges added per node so far\n  foreach my $e (sort { $edge{$b} <=> $edge{$a} } keys(%edge)) {\n    my @x = split(/\\001/, $e);\n    $n = $edge{$e};\n\n    # Initialize degree of kept incoming and outgoing edges if necessary\n    my $src = $x[0];\n    my $dst = $x[1];\n    if (!exists($outdegree{$src})) { $outdegree{$src} = 0; }\n    if (!exists($indegree{$dst})) { $indegree{$dst} = 0; }\n\n    my $keep;\n    if ($indegree{$dst} == 0) {\n      # Keep edge if needed for reachability\n      $keep = 1;\n    } elsif (abs($n) <= $edgelimit) {\n      # Drop if we are below --edgefraction\n      $keep = 0;\n    } elsif ($outdegree{$src} >= $main::opt_maxdegree ||\n             $indegree{$dst} >= $main::opt_maxdegree) {\n      # Keep limited number of in/out edges per node\n      $keep = 0;\n    } else {\n      $keep = 1;\n    }\n\n    if ($keep) {\n      $outdegree{$src}++;\n      $indegree{$dst}++;\n\n      # Compute line width based on edge count\n      my $fraction = abs($local_total ? (3 * ($n / $local_total)) : 0);\n      if ($fraction > 1) { $fraction = 1; }\n      my $w = $fraction * 2;\n      if ($w < 1 && ($main::opt_web || $main::opt_svg)) {\n        # SVG output treats line widths < 1 poorly.\n        $w = 1;\n      }\n\n      # Dot sometimes segfaults if given edge weights that are too large, so\n      # we cap the weights at a large value\n      my $edgeweight = abs($n) ** 0.7;\n      if ($edgeweight > 100000) { $edgeweight = 100000; }\n      $edgeweight = int($edgeweight);\n\n      my $style = sprintf(\"setlinewidth(%f)\", $w);\n      if ($x[1] =~ m/\\(inline\\)/) {\n        $style .= \",dashed\";\n      }\n\n      # Use a slightly squashed function of the edge count as the weight\n      printf DOT (\"N%s -> N%s [label=%s, weight=%d, style=\\\"%s\\\"];\\n\",\n                  $node{$x[0]},\n                  $node{$x[1]},\n                  Unparse($n),\n                  $edgeweight,\n                  $style);\n    }\n  }\n\n  print DOT (\"}\\n\");\n  close(DOT);\n\n  if ($main::opt_web || $main::opt_svg) {\n    # Rewrite SVG to be more usable inside web browser.\n    RewriteSvg(TempName($main::next_tmpfile, \"svg\"));\n  }\n\n  return 1;\n}\n\nsub RewriteSvg {\n  my $svgfile = shift;\n\n  open(SVG, $svgfile) || die \"open temp svg: $!\";\n  my @svg = <SVG>;\n  close(SVG);\n  unlink $svgfile;\n  my $svg = join('', @svg);\n\n  # Dot's SVG output is\n  #\n  #    <svg width=\"___\" height=\"___\"\n  #     viewBox=\"___\" xmlns=...>\n  #    <g id=\"graph0\" transform=\"...\">\n  #    ...\n  #    </g>\n  #    </svg>\n  #\n  # Change it to\n  #\n  #    <svg width=\"100%\" height=\"100%\"\n  #     xmlns=...>\n  #    $svg_javascript\n  #    <g id=\"viewport\" transform=\"translate(0,0)\">\n  #    <g id=\"graph0\" transform=\"...\">\n  #    ...\n  #    </g>\n  #    </g>\n  #    </svg>\n\n  # Fix width, height; drop viewBox.\n  $svg =~ s/(?s)<svg width=\"[^\"]+\" height=\"[^\"]+\"(.*?)viewBox=\"[^\"]+\"/<svg width=\"100%\" height=\"100%\"$1/;\n\n  # Insert script, viewport <g> above first <g>\n  my $svg_javascript = SvgJavascript();\n  my $viewport = \"<g id=\\\"viewport\\\" transform=\\\"translate(0,0)\\\">\\n\";\n  $svg =~ s/<g id=\"graph\\d\"/$svg_javascript$viewport$&/;\n\n  # Insert final </g> above </svg>.\n  $svg =~ s/(.*)(<\\/svg>)/$1<\\/g>$2/;\n  $svg =~ s/<g id=\"graph\\d\"(.*?)/<g id=\"viewport\"$1/;\n\n  if ($main::opt_svg) {\n    # --svg: write to standard output.\n    print $svg;\n  } else {\n    # Write back to temporary file.\n    open(SVG, \">$svgfile\") || die \"open $svgfile: $!\";\n    print SVG $svg;\n    close(SVG);\n  }\n}\n\nsub SvgJavascript {\n  return <<'EOF';\n<script type=\"text/ecmascript\"><![CDATA[\n// SVGPan\n// http://www.cyberz.org/blog/2009/12/08/svgpan-a-javascript-svg-panzoomdrag-library/\n// Local modification: if(true || ...) below to force panning, never moving.\n\n/**\n *  SVGPan library 1.2\n * ====================\n *\n * Given an unique existing element with id \"viewport\", including the\n * the library into any SVG adds the following capabilities:\n *\n *  - Mouse panning\n *  - Mouse zooming (using the wheel)\n *  - Object dargging\n *\n * Known issues:\n *\n *  - Zooming (while panning) on Safari has still some issues\n *\n * Releases:\n *\n * 1.2, Sat Mar 20 08:42:50 GMT 2010, Zeng Xiaohui\n *\tFixed a bug with browser mouse handler interaction\n *\n * 1.1, Wed Feb  3 17:39:33 GMT 2010, Zeng Xiaohui\n *\tUpdated the zoom code to support the mouse wheel on Safari/Chrome\n *\n * 1.0, Andrea Leofreddi\n *\tFirst release\n *\n * This code is licensed under the following BSD license:\n *\n * Copyright 2009-2010 Andrea Leofreddi <a.leofreddi@itcharm.com>. All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without modification, are\n * permitted provided that the following conditions are met:\n *\n *    1. Redistributions of source code must retain the above copyright notice, this list of\n *       conditions and the following disclaimer.\n *\n *    2. Redistributions in binary form must reproduce the above copyright notice, this list\n *       of conditions and the following disclaimer in the documentation and/or other materials\n *       provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY Andrea Leofreddi ``AS IS'' AND ANY EXPRESS OR IMPLIED\n * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND\n * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Andrea Leofreddi OR\n * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\n * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\n * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n *\n * The views and conclusions contained in the software and documentation are those of the\n * authors and should not be interpreted as representing official policies, either expressed\n * or implied, of Andrea Leofreddi.\n */\n\nvar root = document.documentElement;\n\nvar state = 'none', stateTarget, stateOrigin, stateTf;\n\nsetupHandlers(root);\n\n/**\n * Register handlers\n */\nfunction setupHandlers(root){\n\tsetAttributes(root, {\n\t\t\"onmouseup\" : \"add(evt)\",\n\t\t\"onmousedown\" : \"handleMouseDown(evt)\",\n\t\t\"onmousemove\" : \"handleMouseMove(evt)\",\n\t\t\"onmouseup\" : \"handleMouseUp(evt)\",\n\t\t//\"onmouseout\" : \"handleMouseUp(evt)\", // Decomment this to stop the pan functionality when dragging out of the SVG element\n\t});\n\n\tif(navigator.userAgent.toLowerCase().indexOf('webkit') >= 0)\n\t\twindow.addEventListener('mousewheel', handleMouseWheel, false); // Chrome/Safari\n\telse\n\t\twindow.addEventListener('DOMMouseScroll', handleMouseWheel, false); // Others\n\n\tvar g = svgDoc.getElementById(\"svg\");\n\tg.width = \"100%\";\n\tg.height = \"100%\";\n}\n\n/**\n * Instance an SVGPoint object with given event coordinates.\n */\nfunction getEventPoint(evt) {\n\tvar p = root.createSVGPoint();\n\n\tp.x = evt.clientX;\n\tp.y = evt.clientY;\n\n\treturn p;\n}\n\n/**\n * Sets the current transform matrix of an element.\n */\nfunction setCTM(element, matrix) {\n\tvar s = \"matrix(\" + matrix.a + \",\" + matrix.b + \",\" + matrix.c + \",\" + matrix.d + \",\" + matrix.e + \",\" + matrix.f + \")\";\n\n\telement.setAttribute(\"transform\", s);\n}\n\n/**\n * Dumps a matrix to a string (useful for debug).\n */\nfunction dumpMatrix(matrix) {\n\tvar s = \"[ \" + matrix.a + \", \" + matrix.c + \", \" + matrix.e + \"\\n  \" + matrix.b + \", \" + matrix.d + \", \" + matrix.f + \"\\n  0, 0, 1 ]\";\n\n\treturn s;\n}\n\n/**\n * Sets attributes of an element.\n */\nfunction setAttributes(element, attributes){\n\tfor (i in attributes)\n\t\telement.setAttributeNS(null, i, attributes[i]);\n}\n\n/**\n * Handle mouse move event.\n */\nfunction handleMouseWheel(evt) {\n\tif(evt.preventDefault)\n\t\tevt.preventDefault();\n\n\tevt.returnValue = false;\n\n\tvar svgDoc = evt.target.ownerDocument;\n\n\tvar delta;\n\n\tif(evt.wheelDelta)\n\t\tdelta = evt.wheelDelta / 3600; // Chrome/Safari\n\telse\n\t\tdelta = evt.detail / -90; // Mozilla\n\n\tvar z = 1 + delta; // Zoom factor: 0.9/1.1\n\n\tvar g = svgDoc.getElementById(\"viewport\");\n\n\tvar p = getEventPoint(evt);\n\n\tp = p.matrixTransform(g.getCTM().inverse());\n\n\t// Compute new scale matrix in current mouse position\n\tvar k = root.createSVGMatrix().translate(p.x, p.y).scale(z).translate(-p.x, -p.y);\n\n        setCTM(g, g.getCTM().multiply(k));\n\n\tstateTf = stateTf.multiply(k.inverse());\n}\n\n/**\n * Handle mouse move event.\n */\nfunction handleMouseMove(evt) {\n\tif(evt.preventDefault)\n\t\tevt.preventDefault();\n\n\tevt.returnValue = false;\n\n\tvar svgDoc = evt.target.ownerDocument;\n\n\tvar g = svgDoc.getElementById(\"viewport\");\n\n\tif(state == 'pan') {\n\t\t// Pan mode\n\t\tvar p = getEventPoint(evt).matrixTransform(stateTf);\n\n\t\tsetCTM(g, stateTf.inverse().translate(p.x - stateOrigin.x, p.y - stateOrigin.y));\n\t} else if(state == 'move') {\n\t\t// Move mode\n\t\tvar p = getEventPoint(evt).matrixTransform(g.getCTM().inverse());\n\n\t\tsetCTM(stateTarget, root.createSVGMatrix().translate(p.x - stateOrigin.x, p.y - stateOrigin.y).multiply(g.getCTM().inverse()).multiply(stateTarget.getCTM()));\n\n\t\tstateOrigin = p;\n\t}\n}\n\n/**\n * Handle click event.\n */\nfunction handleMouseDown(evt) {\n\tif(evt.preventDefault)\n\t\tevt.preventDefault();\n\n\tevt.returnValue = false;\n\n\tvar svgDoc = evt.target.ownerDocument;\n\n\tvar g = svgDoc.getElementById(\"viewport\");\n\n\tif(true || evt.target.tagName == \"svg\") {\n\t\t// Pan mode\n\t\tstate = 'pan';\n\n\t\tstateTf = g.getCTM().inverse();\n\n\t\tstateOrigin = getEventPoint(evt).matrixTransform(stateTf);\n\t} else {\n\t\t// Move mode\n\t\tstate = 'move';\n\n\t\tstateTarget = evt.target;\n\n\t\tstateTf = g.getCTM().inverse();\n\n\t\tstateOrigin = getEventPoint(evt).matrixTransform(stateTf);\n\t}\n}\n\n/**\n * Handle mouse button release event.\n */\nfunction handleMouseUp(evt) {\n\tif(evt.preventDefault)\n\t\tevt.preventDefault();\n\n\tevt.returnValue = false;\n\n\tvar svgDoc = evt.target.ownerDocument;\n\n\tif(state == 'pan' || state == 'move') {\n\t\t// Quit pan mode\n\t\tstate = '';\n\t}\n}\n\n]]></script>\nEOF\n}\n\n# Provides a map from fullname to shortname for cases where the\n# shortname is ambiguous.  The symlist has both the fullname and\n# shortname for all symbols, which is usually fine, but sometimes --\n# such as overloaded functions -- two different fullnames can map to\n# the same shortname.  In that case, we use the address of the\n# function to disambiguate the two.  This function fills in a map that\n# maps fullnames to modified shortnames in such cases.  If a fullname\n# is not present in the map, the 'normal' shortname provided by the\n# symlist is the appropriate one to use.\nsub FillFullnameToShortnameMap {\n  my $symbols = shift;\n  my $fullname_to_shortname_map = shift;\n  my $shortnames_seen_once = {};\n  my $shortnames_seen_more_than_once = {};\n\n  foreach my $symlist (values(%{$symbols})) {\n    # TODO(csilvers): deal with inlined symbols too.\n    my $shortname = $symlist->[0];\n    my $fullname = $symlist->[2];\n    if ($fullname !~ /<[0-9a-fA-F]+>$/) {  # fullname doesn't end in an address\n      next;       # the only collisions we care about are when addresses differ\n    }\n    if (defined($shortnames_seen_once->{$shortname}) &&\n        $shortnames_seen_once->{$shortname} ne $fullname) {\n      $shortnames_seen_more_than_once->{$shortname} = 1;\n    } else {\n      $shortnames_seen_once->{$shortname} = $fullname;\n    }\n  }\n\n  foreach my $symlist (values(%{$symbols})) {\n    my $shortname = $symlist->[0];\n    my $fullname = $symlist->[2];\n    # TODO(csilvers): take in a list of addresses we care about, and only\n    # store in the map if $symlist->[1] is in that list.  Saves space.\n    next if defined($fullname_to_shortname_map->{$fullname});\n    if (defined($shortnames_seen_more_than_once->{$shortname})) {\n      if ($fullname =~ /<0*([^>]*)>$/) {   # fullname has address at end of it\n        $fullname_to_shortname_map->{$fullname} = \"$shortname\\@$1\";\n      }\n    }\n  }\n}\n\n# Return a small number that identifies the argument.\n# Multiple calls with the same argument will return the same number.\n# Calls with different arguments will return different numbers.\nsub ShortIdFor {\n  my $key = shift;\n  my $id = $main::uniqueid{$key};\n  if (!defined($id)) {\n    $id = keys(%main::uniqueid) + 1;\n    $main::uniqueid{$key} = $id;\n  }\n  return $id;\n}\n\n# Translate a stack of addresses into a stack of symbols\nsub TranslateStack {\n  my $symbols = shift;\n  my $fullname_to_shortname_map = shift;\n  my $k = shift;\n\n  my @addrs = split(/\\n/, $k);\n  my @result = ();\n  for (my $i = 0; $i <= $#addrs; $i++) {\n    my $a = $addrs[$i];\n\n    # Skip large addresses since they sometimes show up as fake entries on RH9\n    if (length($a) > 8 && $a gt \"7fffffffffffffff\") {\n      next;\n    }\n\n    if ($main::opt_disasm || $main::opt_list) {\n      # We want just the address for the key\n      push(@result, $a);\n      next;\n    }\n\n    my $symlist = $symbols->{$a};\n    if (!defined($symlist)) {\n      $symlist = [$a, \"\", $a];\n    }\n\n    # We can have a sequence of symbols for a particular entry\n    # (more than one symbol in the case of inlining).  Callers\n    # come before callees in symlist, so walk backwards since\n    # the translated stack should contain callees before callers.\n    for (my $j = $#{$symlist}; $j >= 2; $j -= 3) {\n      my $func = $symlist->[$j-2];\n      my $fileline = $symlist->[$j-1];\n      my $fullfunc = $symlist->[$j];\n      if (defined($fullname_to_shortname_map->{$fullfunc})) {\n        $func = $fullname_to_shortname_map->{$fullfunc};\n      }\n      if ($j > 2) {\n        $func = \"$func (inline)\";\n      }\n\n      # Do not merge nodes corresponding to Callback::Run since that\n      # causes confusing cycles in dot display.  Instead, we synthesize\n      # a unique name for this frame per caller.\n      if ($func =~ m/Callback.*::Run$/) {\n        my $caller = ($i > 0) ? $addrs[$i-1] : 0;\n        $func = \"Run#\" . ShortIdFor($caller);\n      }\n\n      if ($main::opt_addresses) {\n        push(@result, \"$a $func $fileline\");\n      } elsif ($main::opt_lines) {\n        if ($func eq '??' && $fileline eq '??:0') {\n          push(@result, \"$a\");\n        } else {\n          push(@result, \"$func $fileline\");\n        }\n      } elsif ($main::opt_functions) {\n        if ($func eq '??') {\n          push(@result, \"$a\");\n        } else {\n          push(@result, $func);\n        }\n      } elsif ($main::opt_files) {\n        if ($fileline eq '??:0' || $fileline eq '') {\n          push(@result, \"$a\");\n        } else {\n          my $f = $fileline;\n          $f =~ s/:\\d+$//;\n          push(@result, $f);\n        }\n      } else {\n        push(@result, $a);\n        last;  # Do not print inlined info\n      }\n    }\n  }\n\n  # print join(\",\", @addrs), \" => \", join(\",\", @result), \"\\n\";\n  return @result;\n}\n\n# Generate percent string for a number and a total\nsub Percent {\n  my $num = shift;\n  my $tot = shift;\n  if ($tot != 0) {\n    return sprintf(\"%.1f%%\", $num * 100.0 / $tot);\n  } else {\n    return ($num == 0) ? \"nan\" : (($num > 0) ? \"+inf\" : \"-inf\");\n  }\n}\n\n# Generate pretty-printed form of number\nsub Unparse {\n  my $num = shift;\n  if ($main::profile_type eq 'heap' || $main::profile_type eq 'growth') {\n    if ($main::opt_inuse_objects || $main::opt_alloc_objects) {\n      return sprintf(\"%d\", $num);\n    } else {\n      if ($main::opt_show_bytes) {\n        return sprintf(\"%d\", $num);\n      } else {\n        return sprintf(\"%.1f\", $num / 1048576.0);\n      }\n    }\n  } elsif ($main::profile_type eq 'contention' && !$main::opt_contentions) {\n    return sprintf(\"%.3f\", $num / 1e9); # Convert nanoseconds to seconds\n  } else {\n    return sprintf(\"%d\", $num);\n  }\n}\n\n# Alternate pretty-printed form: 0 maps to \".\"\nsub UnparseAlt {\n  my $num = shift;\n  if ($num == 0) {\n    return \".\";\n  } else {\n    return Unparse($num);\n  }\n}\n\n# Alternate pretty-printed form: 0 maps to \"\"\nsub HtmlPrintNumber {\n  my $num = shift;\n  if ($num == 0) {\n    return \"\";\n  } else {\n    return Unparse($num);\n  }\n}\n\n# Return output units\nsub Units {\n  if ($main::profile_type eq 'heap' || $main::profile_type eq 'growth') {\n    if ($main::opt_inuse_objects || $main::opt_alloc_objects) {\n      return \"objects\";\n    } else {\n      if ($main::opt_show_bytes) {\n        return \"B\";\n      } else {\n        return \"MB\";\n      }\n    }\n  } elsif ($main::profile_type eq 'contention' && !$main::opt_contentions) {\n    return \"seconds\";\n  } else {\n    return \"samples\";\n  }\n}\n\n##### Profile manipulation code #####\n\n# Generate flattened profile:\n# If count is charged to stack [a,b,c,d], in generated profile,\n# it will be charged to [a]\nsub FlatProfile {\n  my $profile = shift;\n  my $result = {};\n  foreach my $k (keys(%{$profile})) {\n    my $count = $profile->{$k};\n    my @addrs = split(/\\n/, $k);\n    if ($#addrs >= 0) {\n      AddEntry($result, $addrs[0], $count);\n    }\n  }\n  return $result;\n}\n\n# Generate cumulative profile:\n# If count is charged to stack [a,b,c,d], in generated profile,\n# it will be charged to [a], [b], [c], [d]\nsub CumulativeProfile {\n  my $profile = shift;\n  my $result = {};\n  foreach my $k (keys(%{$profile})) {\n    my $count = $profile->{$k};\n    my @addrs = split(/\\n/, $k);\n    foreach my $a (@addrs) {\n      AddEntry($result, $a, $count);\n    }\n  }\n  return $result;\n}\n\n# If the second-youngest PC on the stack is always the same, returns\n# that pc.  Otherwise, returns undef.\nsub IsSecondPcAlwaysTheSame {\n  my $profile = shift;\n\n  my $second_pc = undef;\n  foreach my $k (keys(%{$profile})) {\n    my @addrs = split(/\\n/, $k);\n    if ($#addrs < 1) {\n      return undef;\n    }\n    if (not defined $second_pc) {\n      $second_pc = $addrs[1];\n    } else {\n      if ($second_pc ne $addrs[1]) {\n        return undef;\n      }\n    }\n  }\n  return $second_pc;\n}\n\nsub ExtractSymbolLocation {\n  my $symbols = shift;\n  my $address = shift;\n  # 'addr2line' outputs \"??:0\" for unknown locations; we do the\n  # same to be consistent.\n  my $location = \"??:0:unknown\";\n  if (exists $symbols->{$address}) {\n    my $file = $symbols->{$address}->[1];\n    if ($file eq \"?\") {\n      $file = \"??:0\"\n    }\n    $location = $file . \":\" . $symbols->{$address}->[0];\n  }\n  return $location;\n}\n\n# Extracts a graph of calls.\nsub ExtractCalls {\n  my $symbols = shift;\n  my $profile = shift;\n\n  my $calls = {};\n  while( my ($stack_trace, $count) = each %$profile ) {\n    my @address = split(/\\n/, $stack_trace);\n    my $destination = ExtractSymbolLocation($symbols, $address[0]);\n    AddEntry($calls, $destination, $count);\n    for (my $i = 1; $i <= $#address; $i++) {\n      my $source = ExtractSymbolLocation($symbols, $address[$i]);\n      my $call = \"$source -> $destination\";\n      AddEntry($calls, $call, $count);\n      $destination = $source;\n    }\n  }\n\n  return $calls;\n}\n\nsub FilterFrames {\n  my $symbols = shift;\n  my $profile = shift;\n\n  if ($main::opt_retain eq '' && $main::opt_exclude eq '') {\n    return $profile;\n  }\n\n  my $result = {};\n  foreach my $k (keys(%{$profile})) {\n    my $count = $profile->{$k};\n    my @addrs = split(/\\n/, $k);\n    my @path = ();\n    foreach my $a (@addrs) {\n      my $sym;\n      if (exists($symbols->{$a})) {\n        $sym = $symbols->{$a}->[0];\n      } else {\n        $sym = $a;\n      }\n      if ($main::opt_retain ne '' && $sym !~ m/$main::opt_retain/) {\n        next;\n      }\n      if ($main::opt_exclude ne '' && $sym =~ m/$main::opt_exclude/) {\n        next;\n      }\n      push(@path, $a);\n    }\n    if (scalar(@path) > 0) {\n      my $reduced_path = join(\"\\n\", @path);\n      AddEntry($result, $reduced_path, $count);\n    }\n  }\n\n  return $result;\n}\n\nsub RemoveUninterestingFrames {\n  my $symbols = shift;\n  my $profile = shift;\n\n  # List of function names to skip\n  my %skip = ();\n  my $skip_regexp = 'NOMATCH';\n  if ($main::profile_type eq 'heap' || $main::profile_type eq 'growth') {\n    foreach my $name ('@JEMALLOC_PREFIX@calloc',\n                      'cfree',\n                      '@JEMALLOC_PREFIX@malloc',\n                      '@JEMALLOC_PREFIX@free',\n                      '@JEMALLOC_PREFIX@memalign',\n                      '@JEMALLOC_PREFIX@posix_memalign',\n                      '@JEMALLOC_PREFIX@aligned_alloc',\n                      'pvalloc',\n                      '@JEMALLOC_PREFIX@valloc',\n                      '@JEMALLOC_PREFIX@realloc',\n                      '@JEMALLOC_PREFIX@mallocx',\n                      '@JEMALLOC_PREFIX@rallocx',\n                      '@JEMALLOC_PREFIX@xallocx',\n                      '@JEMALLOC_PREFIX@dallocx',\n                      '@JEMALLOC_PREFIX@sdallocx',\n                      'tc_calloc',\n                      'tc_cfree',\n                      'tc_malloc',\n                      'tc_free',\n                      'tc_memalign',\n                      'tc_posix_memalign',\n                      'tc_pvalloc',\n                      'tc_valloc',\n                      'tc_realloc',\n                      'tc_new',\n                      'tc_delete',\n                      'tc_newarray',\n                      'tc_deletearray',\n                      'tc_new_nothrow',\n                      'tc_newarray_nothrow',\n                      'do_malloc',\n                      '::do_malloc',   # new name -- got moved to an unnamed ns\n                      '::do_malloc_or_cpp_alloc',\n                      'DoSampledAllocation',\n                      'simple_alloc::allocate',\n                      '__malloc_alloc_template::allocate',\n                      '__builtin_delete',\n                      '__builtin_new',\n                      '__builtin_vec_delete',\n                      '__builtin_vec_new',\n                      'operator new',\n                      'operator new[]',\n                      # The entry to our memory-allocation routines on OS X\n                      'malloc_zone_malloc',\n                      'malloc_zone_calloc',\n                      'malloc_zone_valloc',\n                      'malloc_zone_realloc',\n                      'malloc_zone_memalign',\n                      'malloc_zone_free',\n                      # These mark the beginning/end of our custom sections\n                      '__start_google_malloc',\n                      '__stop_google_malloc',\n                      '__start_malloc_hook',\n                      '__stop_malloc_hook') {\n      $skip{$name} = 1;\n      $skip{\"_\" . $name} = 1;   # Mach (OS X) adds a _ prefix to everything\n    }\n    # TODO: Remove TCMalloc once everything has been\n    # moved into the tcmalloc:: namespace and we have flushed\n    # old code out of the system.\n    $skip_regexp = \"TCMalloc|^tcmalloc::\";\n  } elsif ($main::profile_type eq 'contention') {\n    foreach my $vname ('base::RecordLockProfileData',\n                       'base::SubmitMutexProfileData',\n                       'base::SubmitSpinLockProfileData',\n                       'Mutex::Unlock',\n                       'Mutex::UnlockSlow',\n                       'Mutex::ReaderUnlock',\n                       'MutexLock::~MutexLock',\n                       'SpinLock::Unlock',\n                       'SpinLock::SlowUnlock',\n                       'SpinLockHolder::~SpinLockHolder') {\n      $skip{$vname} = 1;\n    }\n  } elsif ($main::profile_type eq 'cpu') {\n    # Drop signal handlers used for CPU profile collection\n    # TODO(dpeng): this should not be necessary; it's taken\n    # care of by the general 2nd-pc mechanism below.\n    foreach my $name ('ProfileData::Add',           # historical\n                      'ProfileData::prof_handler',  # historical\n                      'CpuProfiler::prof_handler',\n                      '__FRAME_END__',\n                      '__pthread_sighandler',\n                      '__restore') {\n      $skip{$name} = 1;\n    }\n  } else {\n    # Nothing skipped for unknown types\n  }\n\n  if ($main::profile_type eq 'cpu') {\n    # If all the second-youngest program counters are the same,\n    # this STRONGLY suggests that it is an artifact of measurement,\n    # i.e., stack frames pushed by the CPU profiler signal handler.\n    # Hence, we delete them.\n    # (The topmost PC is read from the signal structure, not from\n    # the stack, so it does not get involved.)\n    while (my $second_pc = IsSecondPcAlwaysTheSame($profile)) {\n      my $result = {};\n      my $func = '';\n      if (exists($symbols->{$second_pc})) {\n        $second_pc = $symbols->{$second_pc}->[0];\n      }\n      print STDERR \"Removing $second_pc from all stack traces.\\n\";\n      foreach my $k (keys(%{$profile})) {\n        my $count = $profile->{$k};\n        my @addrs = split(/\\n/, $k);\n        splice @addrs, 1, 1;\n        my $reduced_path = join(\"\\n\", @addrs);\n        AddEntry($result, $reduced_path, $count);\n      }\n      $profile = $result;\n    }\n  }\n\n  my $result = {};\n  foreach my $k (keys(%{$profile})) {\n    my $count = $profile->{$k};\n    my @addrs = split(/\\n/, $k);\n    my @path = ();\n    foreach my $a (@addrs) {\n      if (exists($symbols->{$a})) {\n        my $func = $symbols->{$a}->[0];\n        if ($skip{$func} || ($func =~ m/$skip_regexp/)) {\n          # Throw away the portion of the backtrace seen so far, under the\n          # assumption that previous frames were for functions internal to the\n          # allocator.\n          @path = ();\n          next;\n        }\n      }\n      push(@path, $a);\n    }\n    my $reduced_path = join(\"\\n\", @path);\n    AddEntry($result, $reduced_path, $count);\n  }\n\n  $result = FilterFrames($symbols, $result);\n\n  return $result;\n}\n\n# Reduce profile to granularity given by user\nsub ReduceProfile {\n  my $symbols = shift;\n  my $profile = shift;\n  my $result = {};\n  my $fullname_to_shortname_map = {};\n  FillFullnameToShortnameMap($symbols, $fullname_to_shortname_map);\n  foreach my $k (keys(%{$profile})) {\n    my $count = $profile->{$k};\n    my @translated = TranslateStack($symbols, $fullname_to_shortname_map, $k);\n    my @path = ();\n    my %seen = ();\n    $seen{''} = 1;      # So that empty keys are skipped\n    foreach my $e (@translated) {\n      # To avoid double-counting due to recursion, skip a stack-trace\n      # entry if it has already been seen\n      if (!$seen{$e}) {\n        $seen{$e} = 1;\n        push(@path, $e);\n      }\n    }\n    my $reduced_path = join(\"\\n\", @path);\n    AddEntry($result, $reduced_path, $count);\n  }\n  return $result;\n}\n\n# Does the specified symbol array match the regexp?\nsub SymbolMatches {\n  my $sym = shift;\n  my $re = shift;\n  if (defined($sym)) {\n    for (my $i = 0; $i < $#{$sym}; $i += 3) {\n      if ($sym->[$i] =~ m/$re/ || $sym->[$i+1] =~ m/$re/) {\n        return 1;\n      }\n    }\n  }\n  return 0;\n}\n\n# Focus only on paths involving specified regexps\nsub FocusProfile {\n  my $symbols = shift;\n  my $profile = shift;\n  my $focus = shift;\n  my $result = {};\n  foreach my $k (keys(%{$profile})) {\n    my $count = $profile->{$k};\n    my @addrs = split(/\\n/, $k);\n    foreach my $a (@addrs) {\n      # Reply if it matches either the address/shortname/fileline\n      if (($a =~ m/$focus/) || SymbolMatches($symbols->{$a}, $focus)) {\n        AddEntry($result, $k, $count);\n        last;\n      }\n    }\n  }\n  return $result;\n}\n\n# Focus only on paths not involving specified regexps\nsub IgnoreProfile {\n  my $symbols = shift;\n  my $profile = shift;\n  my $ignore = shift;\n  my $result = {};\n  foreach my $k (keys(%{$profile})) {\n    my $count = $profile->{$k};\n    my @addrs = split(/\\n/, $k);\n    my $matched = 0;\n    foreach my $a (@addrs) {\n      # Reply if it matches either the address/shortname/fileline\n      if (($a =~ m/$ignore/) || SymbolMatches($symbols->{$a}, $ignore)) {\n        $matched = 1;\n        last;\n      }\n    }\n    if (!$matched) {\n      AddEntry($result, $k, $count);\n    }\n  }\n  return $result;\n}\n\n# Get total count in profile\nsub TotalProfile {\n  my $profile = shift;\n  my $result = 0;\n  foreach my $k (keys(%{$profile})) {\n    $result += $profile->{$k};\n  }\n  return $result;\n}\n\n# Add A to B\nsub AddProfile {\n  my $A = shift;\n  my $B = shift;\n\n  my $R = {};\n  # add all keys in A\n  foreach my $k (keys(%{$A})) {\n    my $v = $A->{$k};\n    AddEntry($R, $k, $v);\n  }\n  # add all keys in B\n  foreach my $k (keys(%{$B})) {\n    my $v = $B->{$k};\n    AddEntry($R, $k, $v);\n  }\n  return $R;\n}\n\n# Merges symbol maps\nsub MergeSymbols {\n  my $A = shift;\n  my $B = shift;\n\n  my $R = {};\n  foreach my $k (keys(%{$A})) {\n    $R->{$k} = $A->{$k};\n  }\n  if (defined($B)) {\n    foreach my $k (keys(%{$B})) {\n      $R->{$k} = $B->{$k};\n    }\n  }\n  return $R;\n}\n\n\n# Add A to B\nsub AddPcs {\n  my $A = shift;\n  my $B = shift;\n\n  my $R = {};\n  # add all keys in A\n  foreach my $k (keys(%{$A})) {\n    $R->{$k} = 1\n  }\n  # add all keys in B\n  foreach my $k (keys(%{$B})) {\n    $R->{$k} = 1\n  }\n  return $R;\n}\n\n# Subtract B from A\nsub SubtractProfile {\n  my $A = shift;\n  my $B = shift;\n\n  my $R = {};\n  foreach my $k (keys(%{$A})) {\n    my $v = $A->{$k} - GetEntry($B, $k);\n    if ($v < 0 && $main::opt_drop_negative) {\n      $v = 0;\n    }\n    AddEntry($R, $k, $v);\n  }\n  if (!$main::opt_drop_negative) {\n    # Take care of when subtracted profile has more entries\n    foreach my $k (keys(%{$B})) {\n      if (!exists($A->{$k})) {\n        AddEntry($R, $k, 0 - $B->{$k});\n      }\n    }\n  }\n  return $R;\n}\n\n# Get entry from profile; zero if not present\nsub GetEntry {\n  my $profile = shift;\n  my $k = shift;\n  if (exists($profile->{$k})) {\n    return $profile->{$k};\n  } else {\n    return 0;\n  }\n}\n\n# Add entry to specified profile\nsub AddEntry {\n  my $profile = shift;\n  my $k = shift;\n  my $n = shift;\n  if (!exists($profile->{$k})) {\n    $profile->{$k} = 0;\n  }\n  $profile->{$k} += $n;\n}\n\n# Add a stack of entries to specified profile, and add them to the $pcs\n# list.\nsub AddEntries {\n  my $profile = shift;\n  my $pcs = shift;\n  my $stack = shift;\n  my $count = shift;\n  my @k = ();\n\n  foreach my $e (split(/\\s+/, $stack)) {\n    my $pc = HexExtend($e);\n    $pcs->{$pc} = 1;\n    push @k, $pc;\n  }\n  AddEntry($profile, (join \"\\n\", @k), $count);\n}\n\n##### Code to profile a server dynamically #####\n\nsub CheckSymbolPage {\n  my $url = SymbolPageURL();\n  my $command = ShellEscape(@URL_FETCHER, $url);\n  open(SYMBOL, \"$command |\") or error($command);\n  my $line = <SYMBOL>;\n  $line =~ s/\\r//g;         # turn windows-looking lines into unix-looking lines\n  close(SYMBOL);\n  unless (defined($line)) {\n    error(\"$url doesn't exist\\n\");\n  }\n\n  if ($line =~ /^num_symbols:\\s+(\\d+)$/) {\n    if ($1 == 0) {\n      error(\"Stripped binary. No symbols available.\\n\");\n    }\n  } else {\n    error(\"Failed to get the number of symbols from $url\\n\");\n  }\n}\n\nsub IsProfileURL {\n  my $profile_name = shift;\n  if (-f $profile_name) {\n    printf STDERR \"Using local file $profile_name.\\n\";\n    return 0;\n  }\n  return 1;\n}\n\nsub ParseProfileURL {\n  my $profile_name = shift;\n\n  if (!defined($profile_name) || $profile_name eq \"\") {\n    return ();\n  }\n\n  # Split profile URL - matches all non-empty strings, so no test.\n  $profile_name =~ m,^(https?://)?([^/]+)(.*?)(/|$PROFILES)?$,;\n\n  my $proto = $1 || \"http://\";\n  my $hostport = $2;\n  my $prefix = $3;\n  my $profile = $4 || \"/\";\n\n  my $host = $hostport;\n  $host =~ s/:.*//;\n\n  my $baseurl = \"$proto$hostport$prefix\";\n  return ($host, $baseurl, $profile);\n}\n\n# We fetch symbols from the first profile argument.\nsub SymbolPageURL {\n  my ($host, $baseURL, $path) = ParseProfileURL($main::pfile_args[0]);\n  return \"$baseURL$SYMBOL_PAGE\";\n}\n\nsub FetchProgramName() {\n  my ($host, $baseURL, $path) = ParseProfileURL($main::pfile_args[0]);\n  my $url = \"$baseURL$PROGRAM_NAME_PAGE\";\n  my $command_line = ShellEscape(@URL_FETCHER, $url);\n  open(CMDLINE, \"$command_line |\") or error($command_line);\n  my $cmdline = <CMDLINE>;\n  $cmdline =~ s/\\r//g;   # turn windows-looking lines into unix-looking lines\n  close(CMDLINE);\n  error(\"Failed to get program name from $url\\n\") unless defined($cmdline);\n  $cmdline =~ s/\\x00.+//;  # Remove argv[1] and latters.\n  $cmdline =~ s!\\n!!g;  # Remove LFs.\n  return $cmdline;\n}\n\n# Gee, curl's -L (--location) option isn't reliable at least\n# with its 7.12.3 version.  Curl will forget to post data if\n# there is a redirection.  This function is a workaround for\n# curl.  Redirection happens on borg hosts.\nsub ResolveRedirectionForCurl {\n  my $url = shift;\n  my $command_line = ShellEscape(@URL_FETCHER, \"--head\", $url);\n  open(CMDLINE, \"$command_line |\") or error($command_line);\n  while (<CMDLINE>) {\n    s/\\r//g;         # turn windows-looking lines into unix-looking lines\n    if (/^Location: (.*)/) {\n      $url = $1;\n    }\n  }\n  close(CMDLINE);\n  return $url;\n}\n\n# Add a timeout flat to URL_FETCHER.  Returns a new list.\nsub AddFetchTimeout {\n  my $timeout = shift;\n  my @fetcher = @_;\n  if (defined($timeout)) {\n    if (join(\" \", @fetcher) =~ m/\\bcurl -s/) {\n      push(@fetcher, \"--max-time\", sprintf(\"%d\", $timeout));\n    } elsif (join(\" \", @fetcher) =~ m/\\brpcget\\b/) {\n      push(@fetcher, sprintf(\"--deadline=%d\", $timeout));\n    }\n  }\n  return @fetcher;\n}\n\n# Reads a symbol map from the file handle name given as $1, returning\n# the resulting symbol map.  Also processes variables relating to symbols.\n# Currently, the only variable processed is 'binary=<value>' which updates\n# $main::prog to have the correct program name.\nsub ReadSymbols {\n  my $in = shift;\n  my $map = {};\n  while (<$in>) {\n    s/\\r//g;         # turn windows-looking lines into unix-looking lines\n    # Removes all the leading zeroes from the symbols, see comment below.\n    if (m/^0x0*([0-9a-f]+)\\s+(.+)/) {\n      $map->{$1} = $2;\n    } elsif (m/^---/) {\n      last;\n    } elsif (m/^([a-z][^=]*)=(.*)$/ ) {\n      my ($variable, $value) = ($1, $2);\n      for ($variable, $value) {\n        s/^\\s+//;\n        s/\\s+$//;\n      }\n      if ($variable eq \"binary\") {\n        if ($main::prog ne $UNKNOWN_BINARY && $main::prog ne $value) {\n          printf STDERR (\"Warning: Mismatched binary name '%s', using '%s'.\\n\",\n                         $main::prog, $value);\n        }\n        $main::prog = $value;\n      } else {\n        printf STDERR (\"Ignoring unknown variable in symbols list: \" .\n            \"'%s' = '%s'\\n\", $variable, $value);\n      }\n    }\n  }\n  return $map;\n}\n\nsub URLEncode {\n  my $str = shift;\n  $str =~ s/([^A-Za-z0-9\\-_.!~*'()])/ sprintf \"%%%02x\", ord $1 /eg;\n  return $str;\n}\n\nsub AppendSymbolFilterParams {\n  my $url = shift;\n  my @params = ();\n  if ($main::opt_retain ne '') {\n    push(@params, sprintf(\"retain=%s\", URLEncode($main::opt_retain)));\n  }\n  if ($main::opt_exclude ne '') {\n    push(@params, sprintf(\"exclude=%s\", URLEncode($main::opt_exclude)));\n  }\n  if (scalar @params > 0) {\n    $url = sprintf(\"%s?%s\", $url, join(\"&\", @params));\n  }\n  return $url;\n}\n\n# Fetches and processes symbols to prepare them for use in the profile output\n# code.  If the optional 'symbol_map' arg is not given, fetches symbols from\n# $SYMBOL_PAGE for all PC values found in profile.  Otherwise, the raw symbols\n# are assumed to have already been fetched into 'symbol_map' and are simply\n# extracted and processed.\nsub FetchSymbols {\n  my $pcset = shift;\n  my $symbol_map = shift;\n\n  my %seen = ();\n  my @pcs = grep { !$seen{$_}++ } keys(%$pcset);  # uniq\n\n  if (!defined($symbol_map)) {\n    my $post_data = join(\"+\", sort((map {\"0x\" . \"$_\"} @pcs)));\n\n    open(POSTFILE, \">$main::tmpfile_sym\");\n    print POSTFILE $post_data;\n    close(POSTFILE);\n\n    my $url = SymbolPageURL();\n\n    my $command_line;\n    if (join(\" \", @URL_FETCHER) =~ m/\\bcurl -s/) {\n      $url = ResolveRedirectionForCurl($url);\n      $url = AppendSymbolFilterParams($url);\n      $command_line = ShellEscape(@URL_FETCHER, \"-d\", \"\\@$main::tmpfile_sym\",\n                                  $url);\n    } else {\n      $url = AppendSymbolFilterParams($url);\n      $command_line = (ShellEscape(@URL_FETCHER, \"--post\", $url)\n                       . \" < \" . ShellEscape($main::tmpfile_sym));\n    }\n    # We use c++filt in case $SYMBOL_PAGE gives us mangled symbols.\n    my $escaped_cppfilt = ShellEscape($obj_tool_map{\"c++filt\"});\n    open(SYMBOL, \"$command_line | $escaped_cppfilt |\") or error($command_line);\n    $symbol_map = ReadSymbols(*SYMBOL{IO});\n    close(SYMBOL);\n  }\n\n  my $symbols = {};\n  foreach my $pc (@pcs) {\n    my $fullname;\n    # For 64 bits binaries, symbols are extracted with 8 leading zeroes.\n    # Then /symbol reads the long symbols in as uint64, and outputs\n    # the result with a \"0x%08llx\" format which get rid of the zeroes.\n    # By removing all the leading zeroes in both $pc and the symbols from\n    # /symbol, the symbols match and are retrievable from the map.\n    my $shortpc = $pc;\n    $shortpc =~ s/^0*//;\n    # Each line may have a list of names, which includes the function\n    # and also other functions it has inlined.  They are separated (in\n    # PrintSymbolizedProfile), by --, which is illegal in function names.\n    my $fullnames;\n    if (defined($symbol_map->{$shortpc})) {\n      $fullnames = $symbol_map->{$shortpc};\n    } else {\n      $fullnames = \"0x\" . $pc;  # Just use addresses\n    }\n    my $sym = [];\n    $symbols->{$pc} = $sym;\n    foreach my $fullname (split(\"--\", $fullnames)) {\n      my $name = ShortFunctionName($fullname);\n      push(@{$sym}, $name, \"?\", $fullname);\n    }\n  }\n  return $symbols;\n}\n\nsub BaseName {\n  my $file_name = shift;\n  $file_name =~ s!^.*/!!;  # Remove directory name\n  return $file_name;\n}\n\nsub MakeProfileBaseName {\n  my ($binary_name, $profile_name) = @_;\n  my ($host, $baseURL, $path) = ParseProfileURL($profile_name);\n  my $binary_shortname = BaseName($binary_name);\n  return sprintf(\"%s.%s.%s\",\n                 $binary_shortname, $main::op_time, $host);\n}\n\nsub FetchDynamicProfile {\n  my $binary_name = shift;\n  my $profile_name = shift;\n  my $fetch_name_only = shift;\n  my $encourage_patience = shift;\n\n  if (!IsProfileURL($profile_name)) {\n    return $profile_name;\n  } else {\n    my ($host, $baseURL, $path) = ParseProfileURL($profile_name);\n    if ($path eq \"\" || $path eq \"/\") {\n      # Missing type specifier defaults to cpu-profile\n      $path = $PROFILE_PAGE;\n    }\n\n    my $profile_file = MakeProfileBaseName($binary_name, $profile_name);\n\n    my $url = \"$baseURL$path\";\n    my $fetch_timeout = undef;\n    if ($path =~ m/$PROFILE_PAGE|$PMUPROFILE_PAGE/) {\n      if ($path =~ m/[?]/) {\n        $url .= \"&\";\n      } else {\n        $url .= \"?\";\n      }\n      $url .= sprintf(\"seconds=%d\", $main::opt_seconds);\n      $fetch_timeout = $main::opt_seconds * 1.01 + 60;\n      # Set $profile_type for consumption by PrintSymbolizedProfile.\n      $main::profile_type = 'cpu';\n    } else {\n      # For non-CPU profiles, we add a type-extension to\n      # the target profile file name.\n      my $suffix = $path;\n      $suffix =~ s,/,.,g;\n      $profile_file .= $suffix;\n      # Set $profile_type for consumption by PrintSymbolizedProfile.\n      if ($path =~ m/$HEAP_PAGE/) {\n        $main::profile_type = 'heap';\n      } elsif ($path =~ m/$GROWTH_PAGE/) {\n        $main::profile_type = 'growth';\n      } elsif ($path =~ m/$CONTENTION_PAGE/) {\n        $main::profile_type = 'contention';\n      }\n    }\n\n    my $profile_dir = $ENV{\"JEPROF_TMPDIR\"} || ($ENV{HOME} . \"/jeprof\");\n    if (! -d $profile_dir) {\n      mkdir($profile_dir)\n          || die(\"Unable to create profile directory $profile_dir: $!\\n\");\n    }\n    my $tmp_profile = \"$profile_dir/.tmp.$profile_file\";\n    my $real_profile = \"$profile_dir/$profile_file\";\n\n    if ($fetch_name_only > 0) {\n      return $real_profile;\n    }\n\n    my @fetcher = AddFetchTimeout($fetch_timeout, @URL_FETCHER);\n    my $cmd = ShellEscape(@fetcher, $url) . \" > \" . ShellEscape($tmp_profile);\n    if ($path =~ m/$PROFILE_PAGE|$PMUPROFILE_PAGE|$CENSUSPROFILE_PAGE/){\n      print STDERR \"Gathering CPU profile from $url for $main::opt_seconds seconds to\\n  ${real_profile}\\n\";\n      if ($encourage_patience) {\n        print STDERR \"Be patient...\\n\";\n      }\n    } else {\n      print STDERR \"Fetching $path profile from $url to\\n  ${real_profile}\\n\";\n    }\n\n    (system($cmd) == 0) || error(\"Failed to get profile: $cmd: $!\\n\");\n    (system(\"mv\", $tmp_profile, $real_profile) == 0) || error(\"Unable to rename profile\\n\");\n    print STDERR \"Wrote profile to $real_profile\\n\";\n    $main::collected_profile = $real_profile;\n    return $main::collected_profile;\n  }\n}\n\n# Collect profiles in parallel\nsub FetchDynamicProfiles {\n  my $items = scalar(@main::pfile_args);\n  my $levels = log($items) / log(2);\n\n  if ($items == 1) {\n    $main::profile_files[0] = FetchDynamicProfile($main::prog, $main::pfile_args[0], 0, 1);\n  } else {\n    # math rounding issues\n    if ((2 ** $levels) < $items) {\n     $levels++;\n    }\n    my $count = scalar(@main::pfile_args);\n    for (my $i = 0; $i < $count; $i++) {\n      $main::profile_files[$i] = FetchDynamicProfile($main::prog, $main::pfile_args[$i], 1, 0);\n    }\n    print STDERR \"Fetching $count profiles, Be patient...\\n\";\n    FetchDynamicProfilesRecurse($levels, 0, 0);\n    $main::collected_profile = join(\" \\\\\\n    \", @main::profile_files);\n  }\n}\n\n# Recursively fork a process to get enough processes\n# collecting profiles\nsub FetchDynamicProfilesRecurse {\n  my $maxlevel = shift;\n  my $level = shift;\n  my $position = shift;\n\n  if (my $pid = fork()) {\n    $position = 0 | ($position << 1);\n    TryCollectProfile($maxlevel, $level, $position);\n    wait;\n  } else {\n    $position = 1 | ($position << 1);\n    TryCollectProfile($maxlevel, $level, $position);\n    cleanup();\n    exit(0);\n  }\n}\n\n# Collect a single profile\nsub TryCollectProfile {\n  my $maxlevel = shift;\n  my $level = shift;\n  my $position = shift;\n\n  if ($level >= ($maxlevel - 1)) {\n    if ($position < scalar(@main::pfile_args)) {\n      FetchDynamicProfile($main::prog, $main::pfile_args[$position], 0, 0);\n    }\n  } else {\n    FetchDynamicProfilesRecurse($maxlevel, $level+1, $position);\n  }\n}\n\n##### Parsing code #####\n\n# Provide a small streaming-read module to handle very large\n# cpu-profile files.  Stream in chunks along a sliding window.\n# Provides an interface to get one 'slot', correctly handling\n# endian-ness differences.  A slot is one 32-bit or 64-bit word\n# (depending on the input profile).  We tell endianness and bit-size\n# for the profile by looking at the first 8 bytes: in cpu profiles,\n# the second slot is always 3 (we'll accept anything that's not 0).\nBEGIN {\n  package CpuProfileStream;\n\n  sub new {\n    my ($class, $file, $fname) = @_;\n    my $self = { file        => $file,\n                 base        => 0,\n                 stride      => 512 * 1024,   # must be a multiple of bitsize/8\n                 slots       => [],\n                 unpack_code => \"\",           # N for big-endian, V for little\n                 perl_is_64bit => 1,          # matters if profile is 64-bit\n    };\n    bless $self, $class;\n    # Let unittests adjust the stride\n    if ($main::opt_test_stride > 0) {\n      $self->{stride} = $main::opt_test_stride;\n    }\n    # Read the first two slots to figure out bitsize and endianness.\n    my $slots = $self->{slots};\n    my $str;\n    read($self->{file}, $str, 8);\n    # Set the global $address_length based on what we see here.\n    # 8 is 32-bit (8 hexadecimal chars); 16 is 64-bit (16 hexadecimal chars).\n    $address_length = ($str eq (chr(0)x8)) ? 16 : 8;\n    if ($address_length == 8) {\n      if (substr($str, 6, 2) eq chr(0)x2) {\n        $self->{unpack_code} = 'V';  # Little-endian.\n      } elsif (substr($str, 4, 2) eq chr(0)x2) {\n        $self->{unpack_code} = 'N';  # Big-endian\n      } else {\n        ::error(\"$fname: header size >= 2**16\\n\");\n      }\n      @$slots = unpack($self->{unpack_code} . \"*\", $str);\n    } else {\n      # If we're a 64-bit profile, check if we're a 64-bit-capable\n      # perl.  Otherwise, each slot will be represented as a float\n      # instead of an int64, losing precision and making all the\n      # 64-bit addresses wrong.  We won't complain yet, but will\n      # later if we ever see a value that doesn't fit in 32 bits.\n      my $has_q = 0;\n      eval { $has_q = pack(\"Q\", \"1\") ? 1 : 1; };\n      if (!$has_q) {\n        $self->{perl_is_64bit} = 0;\n      }\n      read($self->{file}, $str, 8);\n      if (substr($str, 4, 4) eq chr(0)x4) {\n        # We'd love to use 'Q', but it's a) not universal, b) not endian-proof.\n        $self->{unpack_code} = 'V';  # Little-endian.\n      } elsif (substr($str, 0, 4) eq chr(0)x4) {\n        $self->{unpack_code} = 'N';  # Big-endian\n      } else {\n        ::error(\"$fname: header size >= 2**32\\n\");\n      }\n      my @pair = unpack($self->{unpack_code} . \"*\", $str);\n      # Since we know one of the pair is 0, it's fine to just add them.\n      @$slots = (0, $pair[0] + $pair[1]);\n    }\n    return $self;\n  }\n\n  # Load more data when we access slots->get(X) which is not yet in memory.\n  sub overflow {\n    my ($self) = @_;\n    my $slots = $self->{slots};\n    $self->{base} += $#$slots + 1;   # skip over data we're replacing\n    my $str;\n    read($self->{file}, $str, $self->{stride});\n    if ($address_length == 8) {      # the 32-bit case\n      # This is the easy case: unpack provides 32-bit unpacking primitives.\n      @$slots = unpack($self->{unpack_code} . \"*\", $str);\n    } else {\n      # We need to unpack 32 bits at a time and combine.\n      my @b32_values = unpack($self->{unpack_code} . \"*\", $str);\n      my @b64_values = ();\n      for (my $i = 0; $i < $#b32_values; $i += 2) {\n        # TODO(csilvers): if this is a 32-bit perl, the math below\n        #    could end up in a too-large int, which perl will promote\n        #    to a double, losing necessary precision.  Deal with that.\n        #    Right now, we just die.\n        my ($lo, $hi) = ($b32_values[$i], $b32_values[$i+1]);\n        if ($self->{unpack_code} eq 'N') {    # big-endian\n          ($lo, $hi) = ($hi, $lo);\n        }\n        my $value = $lo + $hi * (2**32);\n        if (!$self->{perl_is_64bit} &&   # check value is exactly represented\n            (($value % (2**32)) != $lo || int($value / (2**32)) != $hi)) {\n          ::error(\"Need a 64-bit perl to process this 64-bit profile.\\n\");\n        }\n        push(@b64_values, $value);\n      }\n      @$slots = @b64_values;\n    }\n  }\n\n  # Access the i-th long in the file (logically), or -1 at EOF.\n  sub get {\n    my ($self, $idx) = @_;\n    my $slots = $self->{slots};\n    while ($#$slots >= 0) {\n      if ($idx < $self->{base}) {\n        # The only time we expect a reference to $slots[$i - something]\n        # after referencing $slots[$i] is reading the very first header.\n        # Since $stride > |header|, that shouldn't cause any lookback\n        # errors.  And everything after the header is sequential.\n        print STDERR \"Unexpected look-back reading CPU profile\";\n        return -1;   # shrug, don't know what better to return\n      } elsif ($idx > $self->{base} + $#$slots) {\n        $self->overflow();\n      } else {\n        return $slots->[$idx - $self->{base}];\n      }\n    }\n    # If we get here, $slots is [], which means we've reached EOF\n    return -1;  # unique since slots is supposed to hold unsigned numbers\n  }\n}\n\n# Reads the top, 'header' section of a profile, and returns the last\n# line of the header, commonly called a 'header line'.  The header\n# section of a profile consists of zero or more 'command' lines that\n# are instructions to jeprof, which jeprof executes when reading the\n# header.  All 'command' lines start with a %.  After the command\n# lines is the 'header line', which is a profile-specific line that\n# indicates what type of profile it is, and perhaps other global\n# information about the profile.  For instance, here's a header line\n# for a heap profile:\n#   heap profile:     53:    38236 [  5525:  1284029] @ heapprofile\n# For historical reasons, the CPU profile does not contain a text-\n# readable header line.  If the profile looks like a CPU profile,\n# this function returns \"\".  If no header line could be found, this\n# function returns undef.\n#\n# The following commands are recognized:\n#   %warn -- emit the rest of this line to stderr, prefixed by 'WARNING:'\n#\n# The input file should be in binmode.\nsub ReadProfileHeader {\n  local *PROFILE = shift;\n  my $firstchar = \"\";\n  my $line = \"\";\n  read(PROFILE, $firstchar, 1);\n  seek(PROFILE, -1, 1);                    # unread the firstchar\n  if ($firstchar !~ /[[:print:]]/) {       # is not a text character\n    return \"\";\n  }\n  while (defined($line = <PROFILE>)) {\n    $line =~ s/\\r//g;   # turn windows-looking lines into unix-looking lines\n    if ($line =~ /^%warn\\s+(.*)/) {        # 'warn' command\n      # Note this matches both '%warn blah\\n' and '%warn\\n'.\n      print STDERR \"WARNING: $1\\n\";        # print the rest of the line\n    } elsif ($line =~ /^%/) {\n      print STDERR \"Ignoring unknown command from profile header: $line\";\n    } else {\n      # End of commands, must be the header line.\n      return $line;\n    }\n  }\n  return undef;     # got to EOF without seeing a header line\n}\n\nsub IsSymbolizedProfileFile {\n  my $file_name = shift;\n  if (!(-e $file_name) || !(-r $file_name)) {\n    return 0;\n  }\n  # Check if the file contains a symbol-section marker.\n  open(TFILE, \"<$file_name\");\n  binmode TFILE;\n  my $firstline = ReadProfileHeader(*TFILE);\n  close(TFILE);\n  if (!$firstline) {\n    return 0;\n  }\n  $SYMBOL_PAGE =~ m,[^/]+$,;    # matches everything after the last slash\n  my $symbol_marker = $&;\n  return $firstline =~ /^--- *$symbol_marker/;\n}\n\n# Parse profile generated by common/profiler.cc and return a reference\n# to a map:\n#      $result->{version}     Version number of profile file\n#      $result->{period}      Sampling period (in microseconds)\n#      $result->{profile}     Profile object\n#      $result->{threads}     Map of thread IDs to profile objects\n#      $result->{map}         Memory map info from profile\n#      $result->{pcs}         Hash of all PC values seen, key is hex address\nsub ReadProfile {\n  my $prog = shift;\n  my $fname = shift;\n  my $result;            # return value\n\n  $CONTENTION_PAGE =~ m,[^/]+$,;    # matches everything after the last slash\n  my $contention_marker = $&;\n  $GROWTH_PAGE  =~ m,[^/]+$,;    # matches everything after the last slash\n  my $growth_marker = $&;\n  $SYMBOL_PAGE =~ m,[^/]+$,;    # matches everything after the last slash\n  my $symbol_marker = $&;\n  $PROFILE_PAGE =~ m,[^/]+$,;    # matches everything after the last slash\n  my $profile_marker = $&;\n  $HEAP_PAGE =~ m,[^/]+$,;    # matches everything after the last slash\n  my $heap_marker = $&;\n\n  # Look at first line to see if it is a heap or a CPU profile.\n  # CPU profile may start with no header at all, and just binary data\n  # (starting with \\0\\0\\0\\0) -- in that case, don't try to read the\n  # whole firstline, since it may be gigabytes(!) of data.\n  open(PROFILE, \"<$fname\") || error(\"$fname: $!\\n\");\n  binmode PROFILE;      # New perls do UTF-8 processing\n  my $header = ReadProfileHeader(*PROFILE);\n  if (!defined($header)) {   # means \"at EOF\"\n    error(\"Profile is empty.\\n\");\n  }\n\n  my $symbols;\n  if ($header =~ m/^--- *$symbol_marker/o) {\n    # Verify that the user asked for a symbolized profile\n    if (!$main::use_symbolized_profile) {\n      # we have both a binary and symbolized profiles, abort\n      error(\"FATAL ERROR: Symbolized profile\\n   $fname\\ncannot be used with \" .\n            \"a binary arg. Try again without passing\\n   $prog\\n\");\n    }\n    # Read the symbol section of the symbolized profile file.\n    $symbols = ReadSymbols(*PROFILE{IO});\n    # Read the next line to get the header for the remaining profile.\n    $header = ReadProfileHeader(*PROFILE) || \"\";\n  }\n\n  if ($header =~ m/^--- *($heap_marker|$growth_marker)/o) {\n    # Skip \"--- ...\" line for profile types that have their own headers.\n    $header = ReadProfileHeader(*PROFILE) || \"\";\n  }\n\n  $main::profile_type = '';\n\n  if ($header =~ m/^heap profile:.*$growth_marker/o) {\n    $main::profile_type = 'growth';\n    $result =  ReadHeapProfile($prog, *PROFILE, $header);\n  } elsif ($header =~ m/^heap profile:/) {\n    $main::profile_type = 'heap';\n    $result =  ReadHeapProfile($prog, *PROFILE, $header);\n  } elsif ($header =~ m/^heap/) {\n    $main::profile_type = 'heap';\n    $result = ReadThreadedHeapProfile($prog, $fname, $header);\n  } elsif ($header =~ m/^--- *$contention_marker/o) {\n    $main::profile_type = 'contention';\n    $result = ReadSynchProfile($prog, *PROFILE);\n  } elsif ($header =~ m/^--- *Stacks:/) {\n    print STDERR\n      \"Old format contention profile: mistakenly reports \" .\n      \"condition variable signals as lock contentions.\\n\";\n    $main::profile_type = 'contention';\n    $result = ReadSynchProfile($prog, *PROFILE);\n  } elsif ($header =~ m/^--- *$profile_marker/) {\n    # the binary cpu profile data starts immediately after this line\n    $main::profile_type = 'cpu';\n    $result = ReadCPUProfile($prog, $fname, *PROFILE);\n  } else {\n    if (defined($symbols)) {\n      # a symbolized profile contains a format we don't recognize, bail out\n      error(\"$fname: Cannot recognize profile section after symbols.\\n\");\n    }\n    # no ascii header present -- must be a CPU profile\n    $main::profile_type = 'cpu';\n    $result = ReadCPUProfile($prog, $fname, *PROFILE);\n  }\n\n  close(PROFILE);\n\n  # if we got symbols along with the profile, return those as well\n  if (defined($symbols)) {\n    $result->{symbols} = $symbols;\n  }\n\n  return $result;\n}\n\n# Subtract one from caller pc so we map back to call instr.\n# However, don't do this if we're reading a symbolized profile\n# file, in which case the subtract-one was done when the file\n# was written.\n#\n# We apply the same logic to all readers, though ReadCPUProfile uses an\n# independent implementation.\nsub FixCallerAddresses {\n  my $stack = shift;\n  # --raw/http: Always subtract one from pc's, because PrintSymbolizedProfile()\n  # dumps unadjusted profiles.\n  {\n    $stack =~ /(\\s)/;\n    my $delimiter = $1;\n    my @addrs = split(' ', $stack);\n    my @fixedaddrs;\n    $#fixedaddrs = $#addrs;\n    if ($#addrs >= 0) {\n      $fixedaddrs[0] = $addrs[0];\n    }\n    for (my $i = 1; $i <= $#addrs; $i++) {\n      $fixedaddrs[$i] = AddressSub($addrs[$i], \"0x1\");\n    }\n    return join $delimiter, @fixedaddrs;\n  }\n}\n\n# CPU profile reader\nsub ReadCPUProfile {\n  my $prog = shift;\n  my $fname = shift;       # just used for logging\n  local *PROFILE = shift;\n  my $version;\n  my $period;\n  my $i;\n  my $profile = {};\n  my $pcs = {};\n\n  # Parse string into array of slots.\n  my $slots = CpuProfileStream->new(*PROFILE, $fname);\n\n  # Read header.  The current header version is a 5-element structure\n  # containing:\n  #   0: header count (always 0)\n  #   1: header \"words\" (after this one: 3)\n  #   2: format version (0)\n  #   3: sampling period (usec)\n  #   4: unused padding (always 0)\n  if ($slots->get(0) != 0 ) {\n    error(\"$fname: not a profile file, or old format profile file\\n\");\n  }\n  $i = 2 + $slots->get(1);\n  $version = $slots->get(2);\n  $period = $slots->get(3);\n  # Do some sanity checking on these header values.\n  if ($version > (2**32) || $period > (2**32) || $i > (2**32) || $i < 5) {\n    error(\"$fname: not a profile file, or corrupted profile file\\n\");\n  }\n\n  # Parse profile\n  while ($slots->get($i) != -1) {\n    my $n = $slots->get($i++);\n    my $d = $slots->get($i++);\n    if ($d > (2**16)) {  # TODO(csilvers): what's a reasonable max-stack-depth?\n      my $addr = sprintf(\"0%o\", $i * ($address_length == 8 ? 4 : 8));\n      print STDERR \"At index $i (address $addr):\\n\";\n      error(\"$fname: stack trace depth >= 2**32\\n\");\n    }\n    if ($slots->get($i) == 0) {\n      # End of profile data marker\n      $i += $d;\n      last;\n    }\n\n    # Make key out of the stack entries\n    my @k = ();\n    for (my $j = 0; $j < $d; $j++) {\n      my $pc = $slots->get($i+$j);\n      # Subtract one from caller pc so we map back to call instr.\n      $pc--;\n      $pc = sprintf(\"%0*x\", $address_length, $pc);\n      $pcs->{$pc} = 1;\n      push @k, $pc;\n    }\n\n    AddEntry($profile, (join \"\\n\", @k), $n);\n    $i += $d;\n  }\n\n  # Parse map\n  my $map = '';\n  seek(PROFILE, $i * 4, 0);\n  read(PROFILE, $map, (stat PROFILE)[7]);\n\n  my $r = {};\n  $r->{version} = $version;\n  $r->{period} = $period;\n  $r->{profile} = $profile;\n  $r->{libs} = ParseLibraries($prog, $map, $pcs);\n  $r->{pcs} = $pcs;\n\n  return $r;\n}\n\nsub HeapProfileIndex {\n  my $index = 1;\n  if ($main::opt_inuse_space) {\n    $index = 1;\n  } elsif ($main::opt_inuse_objects) {\n    $index = 0;\n  } elsif ($main::opt_alloc_space) {\n    $index = 3;\n  } elsif ($main::opt_alloc_objects) {\n    $index = 2;\n  }\n  return $index;\n}\n\nsub ReadMappedLibraries {\n  my $fh = shift;\n  my $map = \"\";\n  # Read the /proc/self/maps data\n  while (<$fh>) {\n    s/\\r//g;         # turn windows-looking lines into unix-looking lines\n    $map .= $_;\n  }\n  return $map;\n}\n\nsub ReadMemoryMap {\n  my $fh = shift;\n  my $map = \"\";\n  # Read /proc/self/maps data as formatted by DumpAddressMap()\n  my $buildvar = \"\";\n  while (<PROFILE>) {\n    s/\\r//g;         # turn windows-looking lines into unix-looking lines\n    # Parse \"build=<dir>\" specification if supplied\n    if (m/^\\s*build=(.*)\\n/) {\n      $buildvar = $1;\n    }\n\n    # Expand \"$build\" variable if available\n    $_ =~ s/\\$build\\b/$buildvar/g;\n\n    $map .= $_;\n  }\n  return $map;\n}\n\nsub AdjustSamples {\n  my ($sample_adjustment, $sampling_algorithm, $n1, $s1, $n2, $s2) = @_;\n  if ($sample_adjustment) {\n    if ($sampling_algorithm == 2) {\n      # Remote-heap version 2\n      # The sampling frequency is the rate of a Poisson process.\n      # This means that the probability of sampling an allocation of\n      # size X with sampling rate Y is 1 - exp(-X/Y)\n      if ($n1 != 0) {\n        my $ratio = (($s1*1.0)/$n1)/($sample_adjustment);\n        my $scale_factor = 1/(1 - exp(-$ratio));\n        $n1 *= $scale_factor;\n        $s1 *= $scale_factor;\n      }\n      if ($n2 != 0) {\n        my $ratio = (($s2*1.0)/$n2)/($sample_adjustment);\n        my $scale_factor = 1/(1 - exp(-$ratio));\n        $n2 *= $scale_factor;\n        $s2 *= $scale_factor;\n      }\n    } else {\n      # Remote-heap version 1\n      my $ratio;\n      $ratio = (($s1*1.0)/$n1)/($sample_adjustment);\n      if ($ratio < 1) {\n        $n1 /= $ratio;\n        $s1 /= $ratio;\n      }\n      $ratio = (($s2*1.0)/$n2)/($sample_adjustment);\n      if ($ratio < 1) {\n        $n2 /= $ratio;\n        $s2 /= $ratio;\n      }\n    }\n  }\n  return ($n1, $s1, $n2, $s2);\n}\n\nsub ReadHeapProfile {\n  my $prog = shift;\n  local *PROFILE = shift;\n  my $header = shift;\n\n  my $index = HeapProfileIndex();\n\n  # Find the type of this profile.  The header line looks like:\n  #    heap profile:   1246:  8800744 [  1246:  8800744] @ <heap-url>/266053\n  # There are two pairs <count: size>, the first inuse objects/space, and the\n  # second allocated objects/space.  This is followed optionally by a profile\n  # type, and if that is present, optionally by a sampling frequency.\n  # For remote heap profiles (v1):\n  # The interpretation of the sampling frequency is that the profiler, for\n  # each sample, calculates a uniformly distributed random integer less than\n  # the given value, and records the next sample after that many bytes have\n  # been allocated.  Therefore, the expected sample interval is half of the\n  # given frequency.  By default, if not specified, the expected sample\n  # interval is 128KB.  Only remote-heap-page profiles are adjusted for\n  # sample size.\n  # For remote heap profiles (v2):\n  # The sampling frequency is the rate of a Poisson process. This means that\n  # the probability of sampling an allocation of size X with sampling rate Y\n  # is 1 - exp(-X/Y)\n  # For version 2, a typical header line might look like this:\n  # heap profile:   1922: 127792360 [  1922: 127792360] @ <heap-url>_v2/524288\n  # the trailing number (524288) is the sampling rate. (Version 1 showed\n  # double the 'rate' here)\n  my $sampling_algorithm = 0;\n  my $sample_adjustment = 0;\n  chomp($header);\n  my $type = \"unknown\";\n  if ($header =~ m\"^heap profile:\\s*(\\d+):\\s+(\\d+)\\s+\\[\\s*(\\d+):\\s+(\\d+)\\](\\s*@\\s*([^/]*)(/(\\d+))?)?\") {\n    if (defined($6) && ($6 ne '')) {\n      $type = $6;\n      my $sample_period = $8;\n      # $type is \"heapprofile\" for profiles generated by the\n      # heap-profiler, and either \"heap\" or \"heap_v2\" for profiles\n      # generated by sampling directly within tcmalloc.  It can also\n      # be \"growth\" for heap-growth profiles.  The first is typically\n      # found for profiles generated locally, and the others for\n      # remote profiles.\n      if (($type eq \"heapprofile\") || ($type !~ /heap/) ) {\n        # No need to adjust for the sampling rate with heap-profiler-derived data\n        $sampling_algorithm = 0;\n      } elsif ($type =~ /_v2/) {\n        $sampling_algorithm = 2;     # version 2 sampling\n        if (defined($sample_period) && ($sample_period ne '')) {\n          $sample_adjustment = int($sample_period);\n        }\n      } else {\n        $sampling_algorithm = 1;     # version 1 sampling\n        if (defined($sample_period) && ($sample_period ne '')) {\n          $sample_adjustment = int($sample_period)/2;\n        }\n      }\n    } else {\n      # We detect whether or not this is a remote-heap profile by checking\n      # that the total-allocated stats ($n2,$s2) are exactly the\n      # same as the in-use stats ($n1,$s1).  It is remotely conceivable\n      # that a non-remote-heap profile may pass this check, but it is hard\n      # to imagine how that could happen.\n      # In this case it's so old it's guaranteed to be remote-heap version 1.\n      my ($n1, $s1, $n2, $s2) = ($1, $2, $3, $4);\n      if (($n1 == $n2) && ($s1 == $s2)) {\n        # This is likely to be a remote-heap based sample profile\n        $sampling_algorithm = 1;\n      }\n    }\n  }\n\n  if ($sampling_algorithm > 0) {\n    # For remote-heap generated profiles, adjust the counts and sizes to\n    # account for the sample rate (we sample once every 128KB by default).\n    if ($sample_adjustment == 0) {\n      # Turn on profile adjustment.\n      $sample_adjustment = 128*1024;\n      print STDERR \"Adjusting heap profiles for 1-in-128KB sampling rate\\n\";\n    } else {\n      printf STDERR (\"Adjusting heap profiles for 1-in-%d sampling rate\\n\",\n                     $sample_adjustment);\n    }\n    if ($sampling_algorithm > 1) {\n      # We don't bother printing anything for the original version (version 1)\n      printf STDERR \"Heap version $sampling_algorithm\\n\";\n    }\n  }\n\n  my $profile = {};\n  my $pcs = {};\n  my $map = \"\";\n\n  while (<PROFILE>) {\n    s/\\r//g;         # turn windows-looking lines into unix-looking lines\n    if (/^MAPPED_LIBRARIES:/) {\n      $map .= ReadMappedLibraries(*PROFILE);\n      last;\n    }\n\n    if (/^--- Memory map:/) {\n      $map .= ReadMemoryMap(*PROFILE);\n      last;\n    }\n\n    # Read entry of the form:\n    #  <count1>: <bytes1> [<count2>: <bytes2>] @ a1 a2 a3 ... an\n    s/^\\s*//;\n    s/\\s*$//;\n    if (m/^\\s*(\\d+):\\s+(\\d+)\\s+\\[\\s*(\\d+):\\s+(\\d+)\\]\\s+@\\s+(.*)$/) {\n      my $stack = $5;\n      my ($n1, $s1, $n2, $s2) = ($1, $2, $3, $4);\n      my @counts = AdjustSamples($sample_adjustment, $sampling_algorithm,\n                                 $n1, $s1, $n2, $s2);\n      AddEntries($profile, $pcs, FixCallerAddresses($stack), $counts[$index]);\n    }\n  }\n\n  my $r = {};\n  $r->{version} = \"heap\";\n  $r->{period} = 1;\n  $r->{profile} = $profile;\n  $r->{libs} = ParseLibraries($prog, $map, $pcs);\n  $r->{pcs} = $pcs;\n  return $r;\n}\n\nsub ReadThreadedHeapProfile {\n  my ($prog, $fname, $header) = @_;\n\n  my $index = HeapProfileIndex();\n  my $sampling_algorithm = 0;\n  my $sample_adjustment = 0;\n  chomp($header);\n  my $type = \"unknown\";\n  # Assuming a very specific type of header for now.\n  if ($header =~ m\"^heap_v2/(\\d+)\") {\n    $type = \"_v2\";\n    $sampling_algorithm = 2;\n    $sample_adjustment = int($1);\n  }\n  if ($type ne \"_v2\" || !defined($sample_adjustment)) {\n    die \"Threaded heap profiles require v2 sampling with a sample rate\\n\";\n  }\n\n  my $profile = {};\n  my $thread_profiles = {};\n  my $pcs = {};\n  my $map = \"\";\n  my $stack = \"\";\n\n  while (<PROFILE>) {\n    s/\\r//g;\n    if (/^MAPPED_LIBRARIES:/) {\n      $map .= ReadMappedLibraries(*PROFILE);\n      last;\n    }\n\n    if (/^--- Memory map:/) {\n      $map .= ReadMemoryMap(*PROFILE);\n      last;\n    }\n\n    # Read entry of the form:\n    # @ a1 a2 ... an\n    #   t*: <count1>: <bytes1> [<count2>: <bytes2>]\n    #   t1: <count1>: <bytes1> [<count2>: <bytes2>]\n    #     ...\n    #   tn: <count1>: <bytes1> [<count2>: <bytes2>]\n    s/^\\s*//;\n    s/\\s*$//;\n    if (m/^@\\s+(.*)$/) {\n      $stack = $1;\n    } elsif (m/^\\s*(t(\\*|\\d+)):\\s+(\\d+):\\s+(\\d+)\\s+\\[\\s*(\\d+):\\s+(\\d+)\\]$/) {\n      if ($stack eq \"\") {\n        # Still in the header, so this is just a per-thread summary.\n        next;\n      }\n      my $thread = $2;\n      my ($n1, $s1, $n2, $s2) = ($3, $4, $5, $6);\n      my @counts = AdjustSamples($sample_adjustment, $sampling_algorithm,\n                                 $n1, $s1, $n2, $s2);\n      if ($thread eq \"*\") {\n        AddEntries($profile, $pcs, FixCallerAddresses($stack), $counts[$index]);\n      } else {\n        if (!exists($thread_profiles->{$thread})) {\n          $thread_profiles->{$thread} = {};\n        }\n        AddEntries($thread_profiles->{$thread}, $pcs,\n                   FixCallerAddresses($stack), $counts[$index]);\n      }\n    }\n  }\n\n  my $r = {};\n  $r->{version} = \"heap\";\n  $r->{period} = 1;\n  $r->{profile} = $profile;\n  $r->{threads} = $thread_profiles;\n  $r->{libs} = ParseLibraries($prog, $map, $pcs);\n  $r->{pcs} = $pcs;\n  return $r;\n}\n\nsub ReadSynchProfile {\n  my $prog = shift;\n  local *PROFILE = shift;\n  my $header = shift;\n\n  my $map = '';\n  my $profile = {};\n  my $pcs = {};\n  my $sampling_period = 1;\n  my $cyclespernanosec = 2.8;   # Default assumption for old binaries\n  my $seen_clockrate = 0;\n  my $line;\n\n  my $index = 0;\n  if ($main::opt_total_delay) {\n    $index = 0;\n  } elsif ($main::opt_contentions) {\n    $index = 1;\n  } elsif ($main::opt_mean_delay) {\n    $index = 2;\n  }\n\n  while ( $line = <PROFILE> ) {\n    $line =~ s/\\r//g;      # turn windows-looking lines into unix-looking lines\n    if ( $line =~ /^\\s*(\\d+)\\s+(\\d+) \\@\\s*(.*?)\\s*$/ ) {\n      my ($cycles, $count, $stack) = ($1, $2, $3);\n\n      # Convert cycles to nanoseconds\n      $cycles /= $cyclespernanosec;\n\n      # Adjust for sampling done by application\n      $cycles *= $sampling_period;\n      $count *= $sampling_period;\n\n      my @values = ($cycles, $count, $cycles / $count);\n      AddEntries($profile, $pcs, FixCallerAddresses($stack), $values[$index]);\n\n    } elsif ( $line =~ /^(slow release).*thread \\d+  \\@\\s*(.*?)\\s*$/ ||\n              $line =~ /^\\s*(\\d+) \\@\\s*(.*?)\\s*$/ ) {\n      my ($cycles, $stack) = ($1, $2);\n      if ($cycles !~ /^\\d+$/) {\n        next;\n      }\n\n      # Convert cycles to nanoseconds\n      $cycles /= $cyclespernanosec;\n\n      # Adjust for sampling done by application\n      $cycles *= $sampling_period;\n\n      AddEntries($profile, $pcs, FixCallerAddresses($stack), $cycles);\n\n    } elsif ( $line =~ m/^([a-z][^=]*)=(.*)$/ ) {\n      my ($variable, $value) = ($1,$2);\n      for ($variable, $value) {\n        s/^\\s+//;\n        s/\\s+$//;\n      }\n      if ($variable eq \"cycles/second\") {\n        $cyclespernanosec = $value / 1e9;\n        $seen_clockrate = 1;\n      } elsif ($variable eq \"sampling period\") {\n        $sampling_period = $value;\n      } elsif ($variable eq \"ms since reset\") {\n        # Currently nothing is done with this value in jeprof\n        # So we just silently ignore it for now\n      } elsif ($variable eq \"discarded samples\") {\n        # Currently nothing is done with this value in jeprof\n        # So we just silently ignore it for now\n      } else {\n        printf STDERR (\"Ignoring unnknown variable in /contention output: \" .\n                       \"'%s' = '%s'\\n\",$variable,$value);\n      }\n    } else {\n      # Memory map entry\n      $map .= $line;\n    }\n  }\n\n  if (!$seen_clockrate) {\n    printf STDERR (\"No cycles/second entry in profile; Guessing %.1f GHz\\n\",\n                   $cyclespernanosec);\n  }\n\n  my $r = {};\n  $r->{version} = 0;\n  $r->{period} = $sampling_period;\n  $r->{profile} = $profile;\n  $r->{libs} = ParseLibraries($prog, $map, $pcs);\n  $r->{pcs} = $pcs;\n  return $r;\n}\n\n# Given a hex value in the form \"0x1abcd\" or \"1abcd\", return either\n# \"0001abcd\" or \"000000000001abcd\", depending on the current (global)\n# address length.\nsub HexExtend {\n  my $addr = shift;\n\n  $addr =~ s/^(0x)?0*//;\n  my $zeros_needed = $address_length - length($addr);\n  if ($zeros_needed < 0) {\n    printf STDERR \"Warning: address $addr is longer than address length $address_length\\n\";\n    return $addr;\n  }\n  return (\"0\" x $zeros_needed) . $addr;\n}\n\n##### Symbol extraction #####\n\n# Aggressively search the lib_prefix values for the given library\n# If all else fails, just return the name of the library unmodified.\n# If the lib_prefix is \"/my/path,/other/path\" and $file is \"/lib/dir/mylib.so\"\n# it will search the following locations in this order, until it finds a file:\n#   /my/path/lib/dir/mylib.so\n#   /other/path/lib/dir/mylib.so\n#   /my/path/dir/mylib.so\n#   /other/path/dir/mylib.so\n#   /my/path/mylib.so\n#   /other/path/mylib.so\n#   /lib/dir/mylib.so              (returned as last resort)\nsub FindLibrary {\n  my $file = shift;\n  my $suffix = $file;\n\n  # Search for the library as described above\n  do {\n    foreach my $prefix (@prefix_list) {\n      my $fullpath = $prefix . $suffix;\n      if (-e $fullpath) {\n        return $fullpath;\n      }\n    }\n  } while ($suffix =~ s|^/[^/]+/|/|);\n  return $file;\n}\n\n# Return path to library with debugging symbols.\n# For libc libraries, the copy in /usr/lib/debug contains debugging symbols\nsub DebuggingLibrary {\n  my $file = shift;\n  if ($file =~ m|^/|) {\n      if (-f \"/usr/lib/debug$file\") {\n        return \"/usr/lib/debug$file\";\n      } elsif (-f \"/usr/lib/debug$file.debug\") {\n        return \"/usr/lib/debug$file.debug\";\n      }\n  }\n  return undef;\n}\n\n# Parse text section header of a library using objdump\nsub ParseTextSectionHeaderFromObjdump {\n  my $lib = shift;\n\n  my $size = undef;\n  my $vma;\n  my $file_offset;\n  # Get objdump output from the library file to figure out how to\n  # map between mapped addresses and addresses in the library.\n  my $cmd = ShellEscape($obj_tool_map{\"objdump\"}, \"-h\", $lib);\n  open(OBJDUMP, \"$cmd |\") || error(\"$cmd: $!\\n\");\n  while (<OBJDUMP>) {\n    s/\\r//g;         # turn windows-looking lines into unix-looking lines\n    # Idx Name          Size      VMA       LMA       File off  Algn\n    #  10 .text         00104b2c  420156f0  420156f0  000156f0  2**4\n    # For 64-bit objects, VMA and LMA will be 16 hex digits, size and file\n    # offset may still be 8.  But AddressSub below will still handle that.\n    my @x = split;\n    if (($#x >= 6) && ($x[1] eq '.text')) {\n      $size = $x[2];\n      $vma = $x[3];\n      $file_offset = $x[5];\n      last;\n    }\n  }\n  close(OBJDUMP);\n\n  if (!defined($size)) {\n    return undef;\n  }\n\n  my $r = {};\n  $r->{size} = $size;\n  $r->{vma} = $vma;\n  $r->{file_offset} = $file_offset;\n\n  return $r;\n}\n\n# Parse text section header of a library using otool (on OS X)\nsub ParseTextSectionHeaderFromOtool {\n  my $lib = shift;\n\n  my $size = undef;\n  my $vma = undef;\n  my $file_offset = undef;\n  # Get otool output from the library file to figure out how to\n  # map between mapped addresses and addresses in the library.\n  my $command = ShellEscape($obj_tool_map{\"otool\"}, \"-l\", $lib);\n  open(OTOOL, \"$command |\") || error(\"$command: $!\\n\");\n  my $cmd = \"\";\n  my $sectname = \"\";\n  my $segname = \"\";\n  foreach my $line (<OTOOL>) {\n    $line =~ s/\\r//g;      # turn windows-looking lines into unix-looking lines\n    # Load command <#>\n    #       cmd LC_SEGMENT\n    # [...]\n    # Section\n    #   sectname __text\n    #    segname __TEXT\n    #       addr 0x000009f8\n    #       size 0x00018b9e\n    #     offset 2552\n    #      align 2^2 (4)\n    # We will need to strip off the leading 0x from the hex addresses,\n    # and convert the offset into hex.\n    if ($line =~ /Load command/) {\n      $cmd = \"\";\n      $sectname = \"\";\n      $segname = \"\";\n    } elsif ($line =~ /Section/) {\n      $sectname = \"\";\n      $segname = \"\";\n    } elsif ($line =~ /cmd (\\w+)/) {\n      $cmd = $1;\n    } elsif ($line =~ /sectname (\\w+)/) {\n      $sectname = $1;\n    } elsif ($line =~ /segname (\\w+)/) {\n      $segname = $1;\n    } elsif (!(($cmd eq \"LC_SEGMENT\" || $cmd eq \"LC_SEGMENT_64\") &&\n               $sectname eq \"__text\" &&\n               $segname eq \"__TEXT\")) {\n      next;\n    } elsif ($line =~ /\\baddr 0x([0-9a-fA-F]+)/) {\n      $vma = $1;\n    } elsif ($line =~ /\\bsize 0x([0-9a-fA-F]+)/) {\n      $size = $1;\n    } elsif ($line =~ /\\boffset ([0-9]+)/) {\n      $file_offset = sprintf(\"%016x\", $1);\n    }\n    if (defined($vma) && defined($size) && defined($file_offset)) {\n      last;\n    }\n  }\n  close(OTOOL);\n\n  if (!defined($vma) || !defined($size) || !defined($file_offset)) {\n     return undef;\n  }\n\n  my $r = {};\n  $r->{size} = $size;\n  $r->{vma} = $vma;\n  $r->{file_offset} = $file_offset;\n\n  return $r;\n}\n\nsub ParseTextSectionHeader {\n  # obj_tool_map(\"otool\") is only defined if we're in a Mach-O environment\n  if (defined($obj_tool_map{\"otool\"})) {\n    my $r = ParseTextSectionHeaderFromOtool(@_);\n    if (defined($r)){\n      return $r;\n    }\n  }\n  # If otool doesn't work, or we don't have it, fall back to objdump\n  return ParseTextSectionHeaderFromObjdump(@_);\n}\n\n# Split /proc/pid/maps dump into a list of libraries\nsub ParseLibraries {\n  return if $main::use_symbol_page;  # We don't need libraries info.\n  my $prog = Cwd::abs_path(shift);\n  my $map = shift;\n  my $pcs = shift;\n\n  my $result = [];\n  my $h = \"[a-f0-9]+\";\n  my $zero_offset = HexExtend(\"0\");\n\n  my $buildvar = \"\";\n  foreach my $l (split(\"\\n\", $map)) {\n    if ($l =~ m/^\\s*build=(.*)$/) {\n      $buildvar = $1;\n    }\n\n    my $start;\n    my $finish;\n    my $offset;\n    my $lib;\n    if ($l =~ /^($h)-($h)\\s+..x.\\s+($h)\\s+\\S+:\\S+\\s+\\d+\\s+(\\S+\\.(so|dll|dylib|bundle)((\\.\\d+)+\\w*(\\.\\d+){0,3})?)$/i) {\n      # Full line from /proc/self/maps.  Example:\n      #   40000000-40015000 r-xp 00000000 03:01 12845071   /lib/ld-2.3.2.so\n      $start = HexExtend($1);\n      $finish = HexExtend($2);\n      $offset = HexExtend($3);\n      $lib = $4;\n      $lib =~ s|\\\\|/|g;     # turn windows-style paths into unix-style paths\n    } elsif ($l =~ /^\\s*($h)-($h):\\s*(\\S+\\.so(\\.\\d+)*)/) {\n      # Cooked line from DumpAddressMap.  Example:\n      #   40000000-40015000: /lib/ld-2.3.2.so\n      $start = HexExtend($1);\n      $finish = HexExtend($2);\n      $offset = $zero_offset;\n      $lib = $3;\n    } elsif (($l =~ /^($h)-($h)\\s+..x.\\s+($h)\\s+\\S+:\\S+\\s+\\d+\\s+(\\S+)$/i) && ($4 eq $prog)) {\n      # PIEs and address space randomization do not play well with our\n      # default assumption that main executable is at lowest\n      # addresses. So we're detecting main executable in\n      # /proc/self/maps as well.\n      $start = HexExtend($1);\n      $finish = HexExtend($2);\n      $offset = HexExtend($3);\n      $lib = $4;\n      $lib =~ s|\\\\|/|g;     # turn windows-style paths into unix-style paths\n    }\n    # FreeBSD 10.0 virtual memory map /proc/curproc/map as defined in\n    # function procfs_doprocmap (sys/fs/procfs/procfs_map.c)\n    #\n    # Example:\n    # 0x800600000 0x80061a000 26 0 0xfffff800035a0000 r-x 75 33 0x1004 COW NC vnode /libexec/ld-elf.s\n    # o.1 NCH -1\n    elsif ($l =~ /^(0x$h)\\s(0x$h)\\s\\d+\\s\\d+\\s0x$h\\sr-x\\s\\d+\\s\\d+\\s0x\\d+\\s(COW|NCO)\\s(NC|NNC)\\svnode\\s(\\S+\\.so(\\.\\d+)*)/) {\n      $start = HexExtend($1);\n      $finish = HexExtend($2);\n      $offset = $zero_offset;\n      $lib = FindLibrary($5);\n\n    } else {\n      next;\n    }\n\n    # Expand \"$build\" variable if available\n    $lib =~ s/\\$build\\b/$buildvar/g;\n\n    $lib = FindLibrary($lib);\n\n    # Check for pre-relocated libraries, which use pre-relocated symbol tables\n    # and thus require adjusting the offset that we'll use to translate\n    # VM addresses into symbol table addresses.\n    # Only do this if we're not going to fetch the symbol table from a\n    # debugging copy of the library.\n    if (!DebuggingLibrary($lib)) {\n      my $text = ParseTextSectionHeader($lib);\n      if (defined($text)) {\n         my $vma_offset = AddressSub($text->{vma}, $text->{file_offset});\n         $offset = AddressAdd($offset, $vma_offset);\n      }\n    }\n\n    if($main::opt_debug) { printf STDERR \"$start:$finish ($offset) $lib\\n\"; }\n    push(@{$result}, [$lib, $start, $finish, $offset]);\n  }\n\n  # Append special entry for additional library (not relocated)\n  if ($main::opt_lib ne \"\") {\n    my $text = ParseTextSectionHeader($main::opt_lib);\n    if (defined($text)) {\n       my $start = $text->{vma};\n       my $finish = AddressAdd($start, $text->{size});\n\n       push(@{$result}, [$main::opt_lib, $start, $finish, $start]);\n    }\n  }\n\n  # Append special entry for the main program.  This covers\n  # 0..max_pc_value_seen, so that we assume pc values not found in one\n  # of the library ranges will be treated as coming from the main\n  # program binary.\n  my $min_pc = HexExtend(\"0\");\n  my $max_pc = $min_pc;          # find the maximal PC value in any sample\n  foreach my $pc (keys(%{$pcs})) {\n    if (HexExtend($pc) gt $max_pc) { $max_pc = HexExtend($pc); }\n  }\n  push(@{$result}, [$prog, $min_pc, $max_pc, $zero_offset]);\n\n  return $result;\n}\n\n# Add two hex addresses of length $address_length.\n# Run jeprof --test for unit test if this is changed.\nsub AddressAdd {\n  my $addr1 = shift;\n  my $addr2 = shift;\n  my $sum;\n\n  if ($address_length == 8) {\n    # Perl doesn't cope with wraparound arithmetic, so do it explicitly:\n    $sum = (hex($addr1)+hex($addr2)) % (0x10000000 * 16);\n    return sprintf(\"%08x\", $sum);\n\n  } else {\n    # Do the addition in 7-nibble chunks to trivialize carry handling.\n\n    if ($main::opt_debug and $main::opt_test) {\n      print STDERR \"AddressAdd $addr1 + $addr2 = \";\n    }\n\n    my $a1 = substr($addr1,-7);\n    $addr1 = substr($addr1,0,-7);\n    my $a2 = substr($addr2,-7);\n    $addr2 = substr($addr2,0,-7);\n    $sum = hex($a1) + hex($a2);\n    my $c = 0;\n    if ($sum > 0xfffffff) {\n      $c = 1;\n      $sum -= 0x10000000;\n    }\n    my $r = sprintf(\"%07x\", $sum);\n\n    $a1 = substr($addr1,-7);\n    $addr1 = substr($addr1,0,-7);\n    $a2 = substr($addr2,-7);\n    $addr2 = substr($addr2,0,-7);\n    $sum = hex($a1) + hex($a2) + $c;\n    $c = 0;\n    if ($sum > 0xfffffff) {\n      $c = 1;\n      $sum -= 0x10000000;\n    }\n    $r = sprintf(\"%07x\", $sum) . $r;\n\n    $sum = hex($addr1) + hex($addr2) + $c;\n    if ($sum > 0xff) { $sum -= 0x100; }\n    $r = sprintf(\"%02x\", $sum) . $r;\n\n    if ($main::opt_debug and $main::opt_test) { print STDERR \"$r\\n\"; }\n\n    return $r;\n  }\n}\n\n\n# Subtract two hex addresses of length $address_length.\n# Run jeprof --test for unit test if this is changed.\nsub AddressSub {\n  my $addr1 = shift;\n  my $addr2 = shift;\n  my $diff;\n\n  if ($address_length == 8) {\n    # Perl doesn't cope with wraparound arithmetic, so do it explicitly:\n    $diff = (hex($addr1)-hex($addr2)) % (0x10000000 * 16);\n    return sprintf(\"%08x\", $diff);\n\n  } else {\n    # Do the addition in 7-nibble chunks to trivialize borrow handling.\n    # if ($main::opt_debug) { print STDERR \"AddressSub $addr1 - $addr2 = \"; }\n\n    my $a1 = hex(substr($addr1,-7));\n    $addr1 = substr($addr1,0,-7);\n    my $a2 = hex(substr($addr2,-7));\n    $addr2 = substr($addr2,0,-7);\n    my $b = 0;\n    if ($a2 > $a1) {\n      $b = 1;\n      $a1 += 0x10000000;\n    }\n    $diff = $a1 - $a2;\n    my $r = sprintf(\"%07x\", $diff);\n\n    $a1 = hex(substr($addr1,-7));\n    $addr1 = substr($addr1,0,-7);\n    $a2 = hex(substr($addr2,-7)) + $b;\n    $addr2 = substr($addr2,0,-7);\n    $b = 0;\n    if ($a2 > $a1) {\n      $b = 1;\n      $a1 += 0x10000000;\n    }\n    $diff = $a1 - $a2;\n    $r = sprintf(\"%07x\", $diff) . $r;\n\n    $a1 = hex($addr1);\n    $a2 = hex($addr2) + $b;\n    if ($a2 > $a1) { $a1 += 0x100; }\n    $diff = $a1 - $a2;\n    $r = sprintf(\"%02x\", $diff) . $r;\n\n    # if ($main::opt_debug) { print STDERR \"$r\\n\"; }\n\n    return $r;\n  }\n}\n\n# Increment a hex addresses of length $address_length.\n# Run jeprof --test for unit test if this is changed.\nsub AddressInc {\n  my $addr = shift;\n  my $sum;\n\n  if ($address_length == 8) {\n    # Perl doesn't cope with wraparound arithmetic, so do it explicitly:\n    $sum = (hex($addr)+1) % (0x10000000 * 16);\n    return sprintf(\"%08x\", $sum);\n\n  } else {\n    # Do the addition in 7-nibble chunks to trivialize carry handling.\n    # We are always doing this to step through the addresses in a function,\n    # and will almost never overflow the first chunk, so we check for this\n    # case and exit early.\n\n    # if ($main::opt_debug) { print STDERR \"AddressInc $addr1 = \"; }\n\n    my $a1 = substr($addr,-7);\n    $addr = substr($addr,0,-7);\n    $sum = hex($a1) + 1;\n    my $r = sprintf(\"%07x\", $sum);\n    if ($sum <= 0xfffffff) {\n      $r = $addr . $r;\n      # if ($main::opt_debug) { print STDERR \"$r\\n\"; }\n      return HexExtend($r);\n    } else {\n      $r = \"0000000\";\n    }\n\n    $a1 = substr($addr,-7);\n    $addr = substr($addr,0,-7);\n    $sum = hex($a1) + 1;\n    $r = sprintf(\"%07x\", $sum) . $r;\n    if ($sum <= 0xfffffff) {\n      $r = $addr . $r;\n      # if ($main::opt_debug) { print STDERR \"$r\\n\"; }\n      return HexExtend($r);\n    } else {\n      $r = \"00000000000000\";\n    }\n\n    $sum = hex($addr) + 1;\n    if ($sum > 0xff) { $sum -= 0x100; }\n    $r = sprintf(\"%02x\", $sum) . $r;\n\n    # if ($main::opt_debug) { print STDERR \"$r\\n\"; }\n    return $r;\n  }\n}\n\n# Extract symbols for all PC values found in profile\nsub ExtractSymbols {\n  my $libs = shift;\n  my $pcset = shift;\n\n  my $symbols = {};\n\n  # Map each PC value to the containing library.  To make this faster,\n  # we sort libraries by their starting pc value (highest first), and\n  # advance through the libraries as we advance the pc.  Sometimes the\n  # addresses of libraries may overlap with the addresses of the main\n  # binary, so to make sure the libraries 'win', we iterate over the\n  # libraries in reverse order (which assumes the binary doesn't start\n  # in the middle of a library, which seems a fair assumption).\n  my @pcs = (sort { $a cmp $b } keys(%{$pcset}));  # pcset is 0-extended strings\n  foreach my $lib (sort {$b->[1] cmp $a->[1]} @{$libs}) {\n    my $libname = $lib->[0];\n    my $start = $lib->[1];\n    my $finish = $lib->[2];\n    my $offset = $lib->[3];\n\n    # Use debug library if it exists\n    my $debug_libname = DebuggingLibrary($libname);\n    if ($debug_libname) {\n        $libname = $debug_libname;\n    }\n\n    # Get list of pcs that belong in this library.\n    my $contained = [];\n    my ($start_pc_index, $finish_pc_index);\n    # Find smallest finish_pc_index such that $finish < $pc[$finish_pc_index].\n    for ($finish_pc_index = $#pcs + 1; $finish_pc_index > 0;\n         $finish_pc_index--) {\n      last if $pcs[$finish_pc_index - 1] le $finish;\n    }\n    # Find smallest start_pc_index such that $start <= $pc[$start_pc_index].\n    for ($start_pc_index = $finish_pc_index; $start_pc_index > 0;\n         $start_pc_index--) {\n      last if $pcs[$start_pc_index - 1] lt $start;\n    }\n    # This keeps PC values higher than $pc[$finish_pc_index] in @pcs,\n    # in case there are overlaps in libraries and the main binary.\n    @{$contained} = splice(@pcs, $start_pc_index,\n                           $finish_pc_index - $start_pc_index);\n    # Map to symbols\n    MapToSymbols($libname, AddressSub($start, $offset), $contained, $symbols);\n  }\n\n  return $symbols;\n}\n\n# Map list of PC values to symbols for a given image\nsub MapToSymbols {\n  my $image = shift;\n  my $offset = shift;\n  my $pclist = shift;\n  my $symbols = shift;\n\n  my $debug = 0;\n\n  # Ignore empty binaries\n  if ($#{$pclist} < 0) { return; }\n\n  # Figure out the addr2line command to use\n  my $addr2line = $obj_tool_map{\"addr2line\"};\n  my $cmd = ShellEscape($addr2line, \"-f\", \"-C\", \"-e\", $image);\n  if (exists $obj_tool_map{\"addr2line_pdb\"}) {\n    $addr2line = $obj_tool_map{\"addr2line_pdb\"};\n    $cmd = ShellEscape($addr2line, \"--demangle\", \"-f\", \"-C\", \"-e\", $image);\n  }\n\n  # If \"addr2line\" isn't installed on the system at all, just use\n  # nm to get what info we can (function names, but not line numbers).\n  if (system(ShellEscape($addr2line, \"--help\") . \" >$dev_null 2>&1\") != 0) {\n    MapSymbolsWithNM($image, $offset, $pclist, $symbols);\n    return;\n  }\n\n  # \"addr2line -i\" can produce a variable number of lines per input\n  # address, with no separator that allows us to tell when data for\n  # the next address starts.  So we find the address for a special\n  # symbol (_fini) and interleave this address between all real\n  # addresses passed to addr2line.  The name of this special symbol\n  # can then be used as a separator.\n  $sep_address = undef;  # May be filled in by MapSymbolsWithNM()\n  my $nm_symbols = {};\n  MapSymbolsWithNM($image, $offset, $pclist, $nm_symbols);\n  if (defined($sep_address)) {\n    # Only add \" -i\" to addr2line if the binary supports it.\n    # addr2line --help returns 0, but not if it sees an unknown flag first.\n    if (system(\"$cmd -i --help >$dev_null 2>&1\") == 0) {\n      $cmd .= \" -i\";\n    } else {\n      $sep_address = undef;   # no need for sep_address if we don't support -i\n    }\n  }\n\n  # Make file with all PC values with intervening 'sep_address' so\n  # that we can reliably detect the end of inlined function list\n  open(ADDRESSES, \">$main::tmpfile_sym\") || error(\"$main::tmpfile_sym: $!\\n\");\n  if ($debug) { print(\"---- $image ---\\n\"); }\n  for (my $i = 0; $i <= $#{$pclist}; $i++) {\n    # addr2line always reads hex addresses, and does not need '0x' prefix.\n    if ($debug) { printf STDERR (\"%s\\n\", $pclist->[$i]); }\n    printf ADDRESSES (\"%s\\n\", AddressSub($pclist->[$i], $offset));\n    if (defined($sep_address)) {\n      printf ADDRESSES (\"%s\\n\", $sep_address);\n    }\n  }\n  close(ADDRESSES);\n  if ($debug) {\n    print(\"----\\n\");\n    system(\"cat\", $main::tmpfile_sym);\n    print(\"----\\n\");\n    system(\"$cmd < \" . ShellEscape($main::tmpfile_sym));\n    print(\"----\\n\");\n  }\n\n  open(SYMBOLS, \"$cmd <\" . ShellEscape($main::tmpfile_sym) . \" |\")\n      || error(\"$cmd: $!\\n\");\n  my $count = 0;   # Index in pclist\n  while (<SYMBOLS>) {\n    # Read fullfunction and filelineinfo from next pair of lines\n    s/\\r?\\n$//g;\n    my $fullfunction = $_;\n    $_ = <SYMBOLS>;\n    s/\\r?\\n$//g;\n    my $filelinenum = $_;\n\n    if (defined($sep_address) && $fullfunction eq $sep_symbol) {\n      # Terminating marker for data for this address\n      $count++;\n      next;\n    }\n\n    $filelinenum =~ s|\\\\|/|g; # turn windows-style paths into unix-style paths\n\n    my $pcstr = $pclist->[$count];\n    my $function = ShortFunctionName($fullfunction);\n    my $nms = $nm_symbols->{$pcstr};\n    if (defined($nms)) {\n      if ($fullfunction eq '??') {\n        # nm found a symbol for us.\n        $function = $nms->[0];\n        $fullfunction = $nms->[2];\n      } else {\n\t# MapSymbolsWithNM tags each routine with its starting address,\n\t# useful in case the image has multiple occurrences of this\n\t# routine.  (It uses a syntax that resembles template paramters,\n\t# that are automatically stripped out by ShortFunctionName().)\n\t# addr2line does not provide the same information.  So we check\n\t# if nm disambiguated our symbol, and if so take the annotated\n\t# (nm) version of the routine-name.  TODO(csilvers): this won't\n\t# catch overloaded, inlined symbols, which nm doesn't see.\n\t# Better would be to do a check similar to nm's, in this fn.\n\tif ($nms->[2] =~ m/^\\Q$function\\E/) {  # sanity check it's the right fn\n\t  $function = $nms->[0];\n\t  $fullfunction = $nms->[2];\n\t}\n      }\n    }\n\n    # Prepend to accumulated symbols for pcstr\n    # (so that caller comes before callee)\n    my $sym = $symbols->{$pcstr};\n    if (!defined($sym)) {\n      $sym = [];\n      $symbols->{$pcstr} = $sym;\n    }\n    unshift(@{$sym}, $function, $filelinenum, $fullfunction);\n    if ($debug) { printf STDERR (\"%s => [%s]\\n\", $pcstr, join(\" \", @{$sym})); }\n    if (!defined($sep_address)) {\n      # Inlining is off, so this entry ends immediately\n      $count++;\n    }\n  }\n  close(SYMBOLS);\n}\n\n# Use nm to map the list of referenced PCs to symbols.  Return true iff we\n# are able to read procedure information via nm.\nsub MapSymbolsWithNM {\n  my $image = shift;\n  my $offset = shift;\n  my $pclist = shift;\n  my $symbols = shift;\n\n  # Get nm output sorted by increasing address\n  my $symbol_table = GetProcedureBoundaries($image, \".\");\n  if (!%{$symbol_table}) {\n    return 0;\n  }\n  # Start addresses are already the right length (8 or 16 hex digits).\n  my @names = sort { $symbol_table->{$a}->[0] cmp $symbol_table->{$b}->[0] }\n    keys(%{$symbol_table});\n\n  if ($#names < 0) {\n    # No symbols: just use addresses\n    foreach my $pc (@{$pclist}) {\n      my $pcstr = \"0x\" . $pc;\n      $symbols->{$pc} = [$pcstr, \"?\", $pcstr];\n    }\n    return 0;\n  }\n\n  # Sort addresses so we can do a join against nm output\n  my $index = 0;\n  my $fullname = $names[0];\n  my $name = ShortFunctionName($fullname);\n  foreach my $pc (sort { $a cmp $b } @{$pclist}) {\n    # Adjust for mapped offset\n    my $mpc = AddressSub($pc, $offset);\n    while (($index < $#names) && ($mpc ge $symbol_table->{$fullname}->[1])){\n      $index++;\n      $fullname = $names[$index];\n      $name = ShortFunctionName($fullname);\n    }\n    if ($mpc lt $symbol_table->{$fullname}->[1]) {\n      $symbols->{$pc} = [$name, \"?\", $fullname];\n    } else {\n      my $pcstr = \"0x\" . $pc;\n      $symbols->{$pc} = [$pcstr, \"?\", $pcstr];\n    }\n  }\n  return 1;\n}\n\nsub ShortFunctionName {\n  my $function = shift;\n  while ($function =~ s/\\([^()]*\\)(\\s*const)?//g) { }   # Argument types\n  while ($function =~ s/<[^<>]*>//g)  { }    # Remove template arguments\n  $function =~ s/^.*\\s+(\\w+::)/$1/;          # Remove leading type\n  return $function;\n}\n\n# Trim overly long symbols found in disassembler output\nsub CleanDisassembly {\n  my $d = shift;\n  while ($d =~ s/\\([^()%]*\\)(\\s*const)?//g) { } # Argument types, not (%rax)\n  while ($d =~ s/(\\w+)<[^<>]*>/$1/g)  { }       # Remove template arguments\n  return $d;\n}\n\n# Clean file name for display\nsub CleanFileName {\n  my ($f) = @_;\n  $f =~ s|^/proc/self/cwd/||;\n  $f =~ s|^\\./||;\n  return $f;\n}\n\n# Make address relative to section and clean up for display\nsub UnparseAddress {\n  my ($offset, $address) = @_;\n  $address = AddressSub($address, $offset);\n  $address =~ s/^0x//;\n  $address =~ s/^0*//;\n  return $address;\n}\n\n##### Miscellaneous #####\n\n# Find the right versions of the above object tools to use.  The\n# argument is the program file being analyzed, and should be an ELF\n# 32-bit or ELF 64-bit executable file.  The location of the tools\n# is determined by considering the following options in this order:\n#   1) --tools option, if set\n#   2) JEPROF_TOOLS environment variable, if set\n#   3) the environment\nsub ConfigureObjTools {\n  my $prog_file = shift;\n\n  # Check for the existence of $prog_file because /usr/bin/file does not\n  # predictably return error status in prod.\n  (-e $prog_file)  || error(\"$prog_file does not exist.\\n\");\n\n  my $file_type = undef;\n  if (-e \"/usr/bin/file\") {\n    # Follow symlinks (at least for systems where \"file\" supports that).\n    my $escaped_prog_file = ShellEscape($prog_file);\n    $file_type = `/usr/bin/file -L $escaped_prog_file 2>$dev_null ||\n                  /usr/bin/file $escaped_prog_file`;\n  } elsif ($^O == \"MSWin32\") {\n    $file_type = \"MS Windows\";\n  } else {\n    print STDERR \"WARNING: Can't determine the file type of $prog_file\";\n  }\n\n  if ($file_type =~ /64-bit/) {\n    # Change $address_length to 16 if the program file is ELF 64-bit.\n    # We can't detect this from many (most?) heap or lock contention\n    # profiles, since the actual addresses referenced are generally in low\n    # memory even for 64-bit programs.\n    $address_length = 16;\n  }\n\n  if ($file_type =~ /MS Windows/) {\n    # For windows, we provide a version of nm and addr2line as part of\n    # the opensource release, which is capable of parsing\n    # Windows-style PDB executables.  It should live in the path, or\n    # in the same directory as jeprof.\n    $obj_tool_map{\"nm_pdb\"} = \"nm-pdb\";\n    $obj_tool_map{\"addr2line_pdb\"} = \"addr2line-pdb\";\n  }\n\n  if ($file_type =~ /Mach-O/) {\n    # OS X uses otool to examine Mach-O files, rather than objdump.\n    $obj_tool_map{\"otool\"} = \"otool\";\n    $obj_tool_map{\"addr2line\"} = \"false\";  # no addr2line\n    $obj_tool_map{\"objdump\"} = \"false\";  # no objdump\n  }\n\n  # Go fill in %obj_tool_map with the pathnames to use:\n  foreach my $tool (keys %obj_tool_map) {\n    $obj_tool_map{$tool} = ConfigureTool($obj_tool_map{$tool});\n  }\n}\n\n# Returns the path of a caller-specified object tool.  If --tools or\n# JEPROF_TOOLS are specified, then returns the full path to the tool\n# with that prefix.  Otherwise, returns the path unmodified (which\n# means we will look for it on PATH).\nsub ConfigureTool {\n  my $tool = shift;\n  my $path;\n\n  # --tools (or $JEPROF_TOOLS) is a comma separated list, where each\n  # item is either a) a pathname prefix, or b) a map of the form\n  # <tool>:<path>.  First we look for an entry of type (b) for our\n  # tool.  If one is found, we use it.  Otherwise, we consider all the\n  # pathname prefixes in turn, until one yields an existing file.  If\n  # none does, we use a default path.\n  my $tools = $main::opt_tools || $ENV{\"JEPROF_TOOLS\"} || \"\";\n  if ($tools =~ m/(,|^)\\Q$tool\\E:([^,]*)/) {\n    $path = $2;\n    # TODO(csilvers): sanity-check that $path exists?  Hard if it's relative.\n  } elsif ($tools ne '') {\n    foreach my $prefix (split(',', $tools)) {\n      next if ($prefix =~ /:/);    # ignore \"tool:fullpath\" entries in the list\n      if (-x $prefix . $tool) {\n        $path = $prefix . $tool;\n        last;\n      }\n    }\n    if (!$path) {\n      error(\"No '$tool' found with prefix specified by \" .\n            \"--tools (or \\$JEPROF_TOOLS) '$tools'\\n\");\n    }\n  } else {\n    # ... otherwise use the version that exists in the same directory as\n    # jeprof.  If there's nothing there, use $PATH.\n    $0 =~ m,[^/]*$,;     # this is everything after the last slash\n    my $dirname = $`;    # this is everything up to and including the last slash\n    if (-x \"$dirname$tool\") {\n      $path = \"$dirname$tool\";\n    } else {\n      $path = $tool;\n    }\n  }\n  if ($main::opt_debug) { print STDERR \"Using '$path' for '$tool'.\\n\"; }\n  return $path;\n}\n\nsub ShellEscape {\n  my @escaped_words = ();\n  foreach my $word (@_) {\n    my $escaped_word = $word;\n    if ($word =~ m![^a-zA-Z0-9/.,_=-]!) {  # check for anything not in whitelist\n      $escaped_word =~ s/'/'\\\\''/;\n      $escaped_word = \"'$escaped_word'\";\n    }\n    push(@escaped_words, $escaped_word);\n  }\n  return join(\" \", @escaped_words);\n}\n\nsub cleanup {\n  unlink($main::tmpfile_sym);\n  unlink(keys %main::tempnames);\n\n  # We leave any collected profiles in $HOME/jeprof in case the user wants\n  # to look at them later.  We print a message informing them of this.\n  if ((scalar(@main::profile_files) > 0) &&\n      defined($main::collected_profile)) {\n    if (scalar(@main::profile_files) == 1) {\n      print STDERR \"Dynamically gathered profile is in $main::collected_profile\\n\";\n    }\n    print STDERR \"If you want to investigate this profile further, you can do:\\n\";\n    print STDERR \"\\n\";\n    print STDERR \"  jeprof \\\\\\n\";\n    print STDERR \"    $main::prog \\\\\\n\";\n    print STDERR \"    $main::collected_profile\\n\";\n    print STDERR \"\\n\";\n  }\n}\n\nsub sighandler {\n  cleanup();\n  exit(1);\n}\n\nsub error {\n  my $msg = shift;\n  print STDERR $msg;\n  cleanup();\n  exit(1);\n}\n\n\n# Run $nm_command and get all the resulting procedure boundaries whose\n# names match \"$regexp\" and returns them in a hashtable mapping from\n# procedure name to a two-element vector of [start address, end address]\nsub GetProcedureBoundariesViaNm {\n  my $escaped_nm_command = shift;    # shell-escaped\n  my $regexp = shift;\n\n  my $symbol_table = {};\n  open(NM, \"$escaped_nm_command |\") || error(\"$escaped_nm_command: $!\\n\");\n  my $last_start = \"0\";\n  my $routine = \"\";\n  while (<NM>) {\n    s/\\r//g;         # turn windows-looking lines into unix-looking lines\n    if (m/^\\s*([0-9a-f]+) (.) (..*)/) {\n      my $start_val = $1;\n      my $type = $2;\n      my $this_routine = $3;\n\n      # It's possible for two symbols to share the same address, if\n      # one is a zero-length variable (like __start_google_malloc) or\n      # one symbol is a weak alias to another (like __libc_malloc).\n      # In such cases, we want to ignore all values except for the\n      # actual symbol, which in nm-speak has type \"T\".  The logic\n      # below does this, though it's a bit tricky: what happens when\n      # we have a series of lines with the same address, is the first\n      # one gets queued up to be processed.  However, it won't\n      # *actually* be processed until later, when we read a line with\n      # a different address.  That means that as long as we're reading\n      # lines with the same address, we have a chance to replace that\n      # item in the queue, which we do whenever we see a 'T' entry --\n      # that is, a line with type 'T'.  If we never see a 'T' entry,\n      # we'll just go ahead and process the first entry (which never\n      # got touched in the queue), and ignore the others.\n      if ($start_val eq $last_start && $type =~ /t/i) {\n        # We are the 'T' symbol at this address, replace previous symbol.\n        $routine = $this_routine;\n        next;\n      } elsif ($start_val eq $last_start) {\n        # We're not the 'T' symbol at this address, so ignore us.\n        next;\n      }\n\n      if ($this_routine eq $sep_symbol) {\n        $sep_address = HexExtend($start_val);\n      }\n\n      # Tag this routine with the starting address in case the image\n      # has multiple occurrences of this routine.  We use a syntax\n      # that resembles template parameters that are automatically\n      # stripped out by ShortFunctionName()\n      $this_routine .= \"<$start_val>\";\n\n      if (defined($routine) && $routine =~ m/$regexp/) {\n        $symbol_table->{$routine} = [HexExtend($last_start),\n                                     HexExtend($start_val)];\n      }\n      $last_start = $start_val;\n      $routine = $this_routine;\n    } elsif (m/^Loaded image name: (.+)/) {\n      # The win32 nm workalike emits information about the binary it is using.\n      if ($main::opt_debug) { print STDERR \"Using Image $1\\n\"; }\n    } elsif (m/^PDB file name: (.+)/) {\n      # The win32 nm workalike emits information about the pdb it is using.\n      if ($main::opt_debug) { print STDERR \"Using PDB $1\\n\"; }\n    }\n  }\n  close(NM);\n  # Handle the last line in the nm output.  Unfortunately, we don't know\n  # how big this last symbol is, because we don't know how big the file\n  # is.  For now, we just give it a size of 0.\n  # TODO(csilvers): do better here.\n  if (defined($routine) && $routine =~ m/$regexp/) {\n    $symbol_table->{$routine} = [HexExtend($last_start),\n                                 HexExtend($last_start)];\n  }\n  return $symbol_table;\n}\n\n# Gets the procedure boundaries for all routines in \"$image\" whose names\n# match \"$regexp\" and returns them in a hashtable mapping from procedure\n# name to a two-element vector of [start address, end address].\n# Will return an empty map if nm is not installed or not working properly.\nsub GetProcedureBoundaries {\n  my $image = shift;\n  my $regexp = shift;\n\n  # If $image doesn't start with /, then put ./ in front of it.  This works\n  # around an obnoxious bug in our probing of nm -f behavior.\n  # \"nm -f $image\" is supposed to fail on GNU nm, but if:\n  #\n  # a. $image starts with [BbSsPp] (for example, bin/foo/bar), AND\n  # b. you have a.out in your current directory (a not uncommon occurence)\n  #\n  # then \"nm -f $image\" succeeds because -f only looks at the first letter of\n  # the argument, which looks valid because it's [BbSsPp], and then since\n  # there's no image provided, it looks for a.out and finds it.\n  #\n  # This regex makes sure that $image starts with . or /, forcing the -f\n  # parsing to fail since . and / are not valid formats.\n  $image =~ s#^[^/]#./$&#;\n\n  # For libc libraries, the copy in /usr/lib/debug contains debugging symbols\n  my $debugging = DebuggingLibrary($image);\n  if ($debugging) {\n    $image = $debugging;\n  }\n\n  my $nm = $obj_tool_map{\"nm\"};\n  my $cppfilt = $obj_tool_map{\"c++filt\"};\n\n  # nm can fail for two reasons: 1) $image isn't a debug library; 2) nm\n  # binary doesn't support --demangle.  In addition, for OS X we need\n  # to use the -f flag to get 'flat' nm output (otherwise we don't sort\n  # properly and get incorrect results).  Unfortunately, GNU nm uses -f\n  # in an incompatible way.  So first we test whether our nm supports\n  # --demangle and -f.\n  my $demangle_flag = \"\";\n  my $cppfilt_flag = \"\";\n  my $to_devnull = \">$dev_null 2>&1\";\n  if (system(ShellEscape($nm, \"--demangle\", \"image\") . $to_devnull) == 0) {\n    # In this mode, we do \"nm --demangle <foo>\"\n    $demangle_flag = \"--demangle\";\n    $cppfilt_flag = \"\";\n  } elsif (system(ShellEscape($cppfilt, $image) . $to_devnull) == 0) {\n    # In this mode, we do \"nm <foo> | c++filt\"\n    $cppfilt_flag = \" | \" . ShellEscape($cppfilt);\n  };\n  my $flatten_flag = \"\";\n  if (system(ShellEscape($nm, \"-f\", $image) . $to_devnull) == 0) {\n    $flatten_flag = \"-f\";\n  }\n\n  # Finally, in the case $imagie isn't a debug library, we try again with\n  # -D to at least get *exported* symbols.  If we can't use --demangle,\n  # we use c++filt instead, if it exists on this system.\n  my @nm_commands = (ShellEscape($nm, \"-n\", $flatten_flag, $demangle_flag,\n                                 $image) . \" 2>$dev_null $cppfilt_flag\",\n                     ShellEscape($nm, \"-D\", \"-n\", $flatten_flag, $demangle_flag,\n                                 $image) . \" 2>$dev_null $cppfilt_flag\",\n                     # 6nm is for Go binaries\n                     ShellEscape(\"6nm\", \"$image\") . \" 2>$dev_null | sort\",\n                     );\n\n  # If the executable is an MS Windows PDB-format executable, we'll\n  # have set up obj_tool_map(\"nm_pdb\").  In this case, we actually\n  # want to use both unix nm and windows-specific nm_pdb, since\n  # PDB-format executables can apparently include dwarf .o files.\n  if (exists $obj_tool_map{\"nm_pdb\"}) {\n    push(@nm_commands,\n         ShellEscape($obj_tool_map{\"nm_pdb\"}, \"--demangle\", $image)\n         . \" 2>$dev_null\");\n  }\n\n  foreach my $nm_command (@nm_commands) {\n    my $symbol_table = GetProcedureBoundariesViaNm($nm_command, $regexp);\n    return $symbol_table if (%{$symbol_table});\n  }\n  my $symbol_table = {};\n  return $symbol_table;\n}\n\n\n# The test vectors for AddressAdd/Sub/Inc are 8-16-nibble hex strings.\n# To make them more readable, we add underscores at interesting places.\n# This routine removes the underscores, producing the canonical representation\n# used by jeprof to represent addresses, particularly in the tested routines.\nsub CanonicalHex {\n  my $arg = shift;\n  return join '', (split '_',$arg);\n}\n\n\n# Unit test for AddressAdd:\nsub AddressAddUnitTest {\n  my $test_data_8 = shift;\n  my $test_data_16 = shift;\n  my $error_count = 0;\n  my $fail_count = 0;\n  my $pass_count = 0;\n  # print STDERR \"AddressAddUnitTest: \", 1+$#{$test_data_8}, \" tests\\n\";\n\n  # First a few 8-nibble addresses.  Note that this implementation uses\n  # plain old arithmetic, so a quick sanity check along with verifying what\n  # happens to overflow (we want it to wrap):\n  $address_length = 8;\n  foreach my $row (@{$test_data_8}) {\n    if ($main::opt_debug and $main::opt_test) { print STDERR \"@{$row}\\n\"; }\n    my $sum = AddressAdd ($row->[0], $row->[1]);\n    if ($sum ne $row->[2]) {\n      printf STDERR \"ERROR: %s != %s + %s = %s\\n\", $sum,\n             $row->[0], $row->[1], $row->[2];\n      ++$fail_count;\n    } else {\n      ++$pass_count;\n    }\n  }\n  printf STDERR \"AddressAdd 32-bit tests: %d passes, %d failures\\n\",\n         $pass_count, $fail_count;\n  $error_count = $fail_count;\n  $fail_count = 0;\n  $pass_count = 0;\n\n  # Now 16-nibble addresses.\n  $address_length = 16;\n  foreach my $row (@{$test_data_16}) {\n    if ($main::opt_debug and $main::opt_test) { print STDERR \"@{$row}\\n\"; }\n    my $sum = AddressAdd (CanonicalHex($row->[0]), CanonicalHex($row->[1]));\n    my $expected = join '', (split '_',$row->[2]);\n    if ($sum ne CanonicalHex($row->[2])) {\n      printf STDERR \"ERROR: %s != %s + %s = %s\\n\", $sum,\n             $row->[0], $row->[1], $row->[2];\n      ++$fail_count;\n    } else {\n      ++$pass_count;\n    }\n  }\n  printf STDERR \"AddressAdd 64-bit tests: %d passes, %d failures\\n\",\n         $pass_count, $fail_count;\n  $error_count += $fail_count;\n\n  return $error_count;\n}\n\n\n# Unit test for AddressSub:\nsub AddressSubUnitTest {\n  my $test_data_8 = shift;\n  my $test_data_16 = shift;\n  my $error_count = 0;\n  my $fail_count = 0;\n  my $pass_count = 0;\n  # print STDERR \"AddressSubUnitTest: \", 1+$#{$test_data_8}, \" tests\\n\";\n\n  # First a few 8-nibble addresses.  Note that this implementation uses\n  # plain old arithmetic, so a quick sanity check along with verifying what\n  # happens to overflow (we want it to wrap):\n  $address_length = 8;\n  foreach my $row (@{$test_data_8}) {\n    if ($main::opt_debug and $main::opt_test) { print STDERR \"@{$row}\\n\"; }\n    my $sum = AddressSub ($row->[0], $row->[1]);\n    if ($sum ne $row->[3]) {\n      printf STDERR \"ERROR: %s != %s - %s = %s\\n\", $sum,\n             $row->[0], $row->[1], $row->[3];\n      ++$fail_count;\n    } else {\n      ++$pass_count;\n    }\n  }\n  printf STDERR \"AddressSub 32-bit tests: %d passes, %d failures\\n\",\n         $pass_count, $fail_count;\n  $error_count = $fail_count;\n  $fail_count = 0;\n  $pass_count = 0;\n\n  # Now 16-nibble addresses.\n  $address_length = 16;\n  foreach my $row (@{$test_data_16}) {\n    if ($main::opt_debug and $main::opt_test) { print STDERR \"@{$row}\\n\"; }\n    my $sum = AddressSub (CanonicalHex($row->[0]), CanonicalHex($row->[1]));\n    if ($sum ne CanonicalHex($row->[3])) {\n      printf STDERR \"ERROR: %s != %s - %s = %s\\n\", $sum,\n             $row->[0], $row->[1], $row->[3];\n      ++$fail_count;\n    } else {\n      ++$pass_count;\n    }\n  }\n  printf STDERR \"AddressSub 64-bit tests: %d passes, %d failures\\n\",\n         $pass_count, $fail_count;\n  $error_count += $fail_count;\n\n  return $error_count;\n}\n\n\n# Unit test for AddressInc:\nsub AddressIncUnitTest {\n  my $test_data_8 = shift;\n  my $test_data_16 = shift;\n  my $error_count = 0;\n  my $fail_count = 0;\n  my $pass_count = 0;\n  # print STDERR \"AddressIncUnitTest: \", 1+$#{$test_data_8}, \" tests\\n\";\n\n  # First a few 8-nibble addresses.  Note that this implementation uses\n  # plain old arithmetic, so a quick sanity check along with verifying what\n  # happens to overflow (we want it to wrap):\n  $address_length = 8;\n  foreach my $row (@{$test_data_8}) {\n    if ($main::opt_debug and $main::opt_test) { print STDERR \"@{$row}\\n\"; }\n    my $sum = AddressInc ($row->[0]);\n    if ($sum ne $row->[4]) {\n      printf STDERR \"ERROR: %s != %s + 1 = %s\\n\", $sum,\n             $row->[0], $row->[4];\n      ++$fail_count;\n    } else {\n      ++$pass_count;\n    }\n  }\n  printf STDERR \"AddressInc 32-bit tests: %d passes, %d failures\\n\",\n         $pass_count, $fail_count;\n  $error_count = $fail_count;\n  $fail_count = 0;\n  $pass_count = 0;\n\n  # Now 16-nibble addresses.\n  $address_length = 16;\n  foreach my $row (@{$test_data_16}) {\n    if ($main::opt_debug and $main::opt_test) { print STDERR \"@{$row}\\n\"; }\n    my $sum = AddressInc (CanonicalHex($row->[0]));\n    if ($sum ne CanonicalHex($row->[4])) {\n      printf STDERR \"ERROR: %s != %s + 1 = %s\\n\", $sum,\n             $row->[0], $row->[4];\n      ++$fail_count;\n    } else {\n      ++$pass_count;\n    }\n  }\n  printf STDERR \"AddressInc 64-bit tests: %d passes, %d failures\\n\",\n         $pass_count, $fail_count;\n  $error_count += $fail_count;\n\n  return $error_count;\n}\n\n\n# Driver for unit tests.\n# Currently just the address add/subtract/increment routines for 64-bit.\nsub RunUnitTests {\n  my $error_count = 0;\n\n  # This is a list of tuples [a, b, a+b, a-b, a+1]\n  my $unit_test_data_8 = [\n    [qw(aaaaaaaa 50505050 fafafafa 5a5a5a5a aaaaaaab)],\n    [qw(50505050 aaaaaaaa fafafafa a5a5a5a6 50505051)],\n    [qw(ffffffff aaaaaaaa aaaaaaa9 55555555 00000000)],\n    [qw(00000001 ffffffff 00000000 00000002 00000002)],\n    [qw(00000001 fffffff0 fffffff1 00000011 00000002)],\n  ];\n  my $unit_test_data_16 = [\n    # The implementation handles data in 7-nibble chunks, so those are the\n    # interesting boundaries.\n    [qw(aaaaaaaa 50505050\n        00_000000f_afafafa 00_0000005_a5a5a5a 00_000000a_aaaaaab)],\n    [qw(50505050 aaaaaaaa\n        00_000000f_afafafa ff_ffffffa_5a5a5a6 00_0000005_0505051)],\n    [qw(ffffffff aaaaaaaa\n        00_000001a_aaaaaa9 00_0000005_5555555 00_0000010_0000000)],\n    [qw(00000001 ffffffff\n        00_0000010_0000000 ff_ffffff0_0000002 00_0000000_0000002)],\n    [qw(00000001 fffffff0\n        00_000000f_ffffff1 ff_ffffff0_0000011 00_0000000_0000002)],\n\n    [qw(00_a00000a_aaaaaaa 50505050\n        00_a00000f_afafafa 00_a000005_a5a5a5a 00_a00000a_aaaaaab)],\n    [qw(0f_fff0005_0505050 aaaaaaaa\n        0f_fff000f_afafafa 0f_ffefffa_5a5a5a6 0f_fff0005_0505051)],\n    [qw(00_000000f_fffffff 01_800000a_aaaaaaa\n        01_800001a_aaaaaa9 fe_8000005_5555555 00_0000010_0000000)],\n    [qw(00_0000000_0000001 ff_fffffff_fffffff\n        00_0000000_0000000 00_0000000_0000002 00_0000000_0000002)],\n    [qw(00_0000000_0000001 ff_fffffff_ffffff0\n        ff_fffffff_ffffff1 00_0000000_0000011 00_0000000_0000002)],\n  ];\n\n  $error_count += AddressAddUnitTest($unit_test_data_8, $unit_test_data_16);\n  $error_count += AddressSubUnitTest($unit_test_data_8, $unit_test_data_16);\n  $error_count += AddressIncUnitTest($unit_test_data_8, $unit_test_data_16);\n  if ($error_count > 0) {\n    print STDERR $error_count, \" errors: FAILED\\n\";\n  } else {\n    print STDERR \"PASS\\n\";\n  }\n  exit ($error_count);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/build-aux/config.guess",
    "content": "#! /bin/sh\n# Attempt to guess a canonical system name.\n#   Copyright 1992-2016 Free Software Foundation, Inc.\n\ntimestamp='2016-10-02'\n\n# This file is free software; you can redistribute it and/or modify it\n# under the terms of the GNU General Public License as published by\n# the Free Software Foundation; either version 3 of the License, or\n# (at your option) any later version.\n#\n# This program is distributed in the hope that it will be useful, but\n# WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n# General Public License for more details.\n#\n# You should have received a copy of the GNU General Public License\n# along with this program; if not, see <http://www.gnu.org/licenses/>.\n#\n# As a special exception to the GNU General Public License, if you\n# distribute this file as part of a program that contains a\n# configuration script generated by Autoconf, you may include it under\n# the same distribution terms that you use for the rest of that\n# program.  This Exception is an additional permission under section 7\n# of the GNU General Public License, version 3 (\"GPLv3\").\n#\n# Originally written by Per Bothner; maintained since 2000 by Ben Elliston.\n#\n# You can get the latest version of this script from:\n# http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess\n#\n# Please send patches to <config-patches@gnu.org>.\n\n\nme=`echo \"$0\" | sed -e 's,.*/,,'`\n\nusage=\"\\\nUsage: $0 [OPTION]\n\nOutput the configuration name of the system \\`$me' is run on.\n\nOperation modes:\n  -h, --help         print this help, then exit\n  -t, --time-stamp   print date of last modification, then exit\n  -v, --version      print version number, then exit\n\nReport bugs and patches to <config-patches@gnu.org>.\"\n\nversion=\"\\\nGNU config.guess ($timestamp)\n\nOriginally written by Per Bothner.\nCopyright 1992-2016 Free Software Foundation, Inc.\n\nThis is free software; see the source for copying conditions.  There is NO\nwarranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\"\n\nhelp=\"\nTry \\`$me --help' for more information.\"\n\n# Parse command line\nwhile test $# -gt 0 ; do\n  case $1 in\n    --time-stamp | --time* | -t )\n       echo \"$timestamp\" ; exit ;;\n    --version | -v )\n       echo \"$version\" ; exit ;;\n    --help | --h* | -h )\n       echo \"$usage\"; exit ;;\n    -- )     # Stop option processing\n       shift; break ;;\n    - )\t# Use stdin as input.\n       break ;;\n    -* )\n       echo \"$me: invalid option $1$help\" >&2\n       exit 1 ;;\n    * )\n       break ;;\n  esac\ndone\n\nif test $# != 0; then\n  echo \"$me: too many arguments$help\" >&2\n  exit 1\nfi\n\ntrap 'exit 1' 1 2 15\n\n# CC_FOR_BUILD -- compiler used by this script. Note that the use of a\n# compiler to aid in system detection is discouraged as it requires\n# temporary files to be created and, as you can see below, it is a\n# headache to deal with in a portable fashion.\n\n# Historically, `CC_FOR_BUILD' used to be named `HOST_CC'. We still\n# use `HOST_CC' if defined, but it is deprecated.\n\n# Portable tmp directory creation inspired by the Autoconf team.\n\nset_cc_for_build='\ntrap \"exitcode=\\$?; (rm -f \\$tmpfiles 2>/dev/null; rmdir \\$tmp 2>/dev/null) && exit \\$exitcode\" 0 ;\ntrap \"rm -f \\$tmpfiles 2>/dev/null; rmdir \\$tmp 2>/dev/null; exit 1\" 1 2 13 15 ;\n: ${TMPDIR=/tmp} ;\n { tmp=`(umask 077 && mktemp -d \"$TMPDIR/cgXXXXXX\") 2>/dev/null` && test -n \"$tmp\" && test -d \"$tmp\" ; } ||\n { test -n \"$RANDOM\" && tmp=$TMPDIR/cg$$-$RANDOM && (umask 077 && mkdir $tmp) ; } ||\n { tmp=$TMPDIR/cg-$$ && (umask 077 && mkdir $tmp) && echo \"Warning: creating insecure temp directory\" >&2 ; } ||\n { echo \"$me: cannot create a temporary directory in $TMPDIR\" >&2 ; exit 1 ; } ;\ndummy=$tmp/dummy ;\ntmpfiles=\"$dummy.c $dummy.o $dummy.rel $dummy\" ;\ncase $CC_FOR_BUILD,$HOST_CC,$CC in\n ,,)    echo \"int x;\" > $dummy.c ;\n\tfor c in cc gcc c89 c99 ; do\n\t  if ($c -c -o $dummy.o $dummy.c) >/dev/null 2>&1 ; then\n\t     CC_FOR_BUILD=\"$c\"; break ;\n\t  fi ;\n\tdone ;\n\tif test x\"$CC_FOR_BUILD\" = x ; then\n\t  CC_FOR_BUILD=no_compiler_found ;\n\tfi\n\t;;\n ,,*)   CC_FOR_BUILD=$CC ;;\n ,*,*)  CC_FOR_BUILD=$HOST_CC ;;\nesac ; set_cc_for_build= ;'\n\n# This is needed to find uname on a Pyramid OSx when run in the BSD universe.\n# (ghazi@noc.rutgers.edu 1994-08-24)\nif (test -f /.attbin/uname) >/dev/null 2>&1 ; then\n\tPATH=$PATH:/.attbin ; export PATH\nfi\n\nUNAME_MACHINE=`(uname -m) 2>/dev/null` || UNAME_MACHINE=unknown\nUNAME_RELEASE=`(uname -r) 2>/dev/null` || UNAME_RELEASE=unknown\nUNAME_SYSTEM=`(uname -s) 2>/dev/null`  || UNAME_SYSTEM=unknown\nUNAME_VERSION=`(uname -v) 2>/dev/null` || UNAME_VERSION=unknown\n\ncase \"${UNAME_SYSTEM}\" in\nLinux|GNU|GNU/*)\n\t# If the system lacks a compiler, then just pick glibc.\n\t# We could probably try harder.\n\tLIBC=gnu\n\n\teval $set_cc_for_build\n\tcat <<-EOF > $dummy.c\n\t#include <features.h>\n\t#if defined(__UCLIBC__)\n\tLIBC=uclibc\n\t#elif defined(__dietlibc__)\n\tLIBC=dietlibc\n\t#else\n\tLIBC=gnu\n\t#endif\n\tEOF\n\teval `$CC_FOR_BUILD -E $dummy.c 2>/dev/null | grep '^LIBC' | sed 's, ,,g'`\n\t;;\nesac\n\n# Note: order is significant - the case branches are not exclusive.\n\ncase \"${UNAME_MACHINE}:${UNAME_SYSTEM}:${UNAME_RELEASE}:${UNAME_VERSION}\" in\n    *:NetBSD:*:*)\n\t# NetBSD (nbsd) targets should (where applicable) match one or\n\t# more of the tuples: *-*-netbsdelf*, *-*-netbsdaout*,\n\t# *-*-netbsdecoff* and *-*-netbsd*.  For targets that recently\n\t# switched to ELF, *-*-netbsd* would select the old\n\t# object file format.  This provides both forward\n\t# compatibility and a consistent mechanism for selecting the\n\t# object file format.\n\t#\n\t# Note: NetBSD doesn't particularly care about the vendor\n\t# portion of the name.  We always set it to \"unknown\".\n\tsysctl=\"sysctl -n hw.machine_arch\"\n\tUNAME_MACHINE_ARCH=`(uname -p 2>/dev/null || \\\n\t    /sbin/$sysctl 2>/dev/null || \\\n\t    /usr/sbin/$sysctl 2>/dev/null || \\\n\t    echo unknown)`\n\tcase \"${UNAME_MACHINE_ARCH}\" in\n\t    armeb) machine=armeb-unknown ;;\n\t    arm*) machine=arm-unknown ;;\n\t    sh3el) machine=shl-unknown ;;\n\t    sh3eb) machine=sh-unknown ;;\n\t    sh5el) machine=sh5le-unknown ;;\n\t    earmv*)\n\t\tarch=`echo ${UNAME_MACHINE_ARCH} | sed -e 's,^e\\(armv[0-9]\\).*$,\\1,'`\n\t\tendian=`echo ${UNAME_MACHINE_ARCH} | sed -ne 's,^.*\\(eb\\)$,\\1,p'`\n\t\tmachine=${arch}${endian}-unknown\n\t\t;;\n\t    *) machine=${UNAME_MACHINE_ARCH}-unknown ;;\n\tesac\n\t# The Operating System including object format, if it has switched\n\t# to ELF recently (or will in the future) and ABI.\n\tcase \"${UNAME_MACHINE_ARCH}\" in\n\t    earm*)\n\t\tos=netbsdelf\n\t\t;;\n\t    arm*|i386|m68k|ns32k|sh3*|sparc|vax)\n\t\teval $set_cc_for_build\n\t\tif echo __ELF__ | $CC_FOR_BUILD -E - 2>/dev/null \\\n\t\t\t| grep -q __ELF__\n\t\tthen\n\t\t    # Once all utilities can be ECOFF (netbsdecoff) or a.out (netbsdaout).\n\t\t    # Return netbsd for either.  FIX?\n\t\t    os=netbsd\n\t\telse\n\t\t    os=netbsdelf\n\t\tfi\n\t\t;;\n\t    *)\n\t\tos=netbsd\n\t\t;;\n\tesac\n\t# Determine ABI tags.\n\tcase \"${UNAME_MACHINE_ARCH}\" in\n\t    earm*)\n\t\texpr='s/^earmv[0-9]/-eabi/;s/eb$//'\n\t\tabi=`echo ${UNAME_MACHINE_ARCH} | sed -e \"$expr\"`\n\t\t;;\n\tesac\n\t# The OS release\n\t# Debian GNU/NetBSD machines have a different userland, and\n\t# thus, need a distinct triplet. However, they do not need\n\t# kernel version information, so it can be replaced with a\n\t# suitable tag, in the style of linux-gnu.\n\tcase \"${UNAME_VERSION}\" in\n\t    Debian*)\n\t\trelease='-gnu'\n\t\t;;\n\t    *)\n\t\trelease=`echo ${UNAME_RELEASE} | sed -e 's/[-_].*//' | cut -d. -f1,2`\n\t\t;;\n\tesac\n\t# Since CPU_TYPE-MANUFACTURER-KERNEL-OPERATING_SYSTEM:\n\t# contains redundant information, the shorter form:\n\t# CPU_TYPE-MANUFACTURER-OPERATING_SYSTEM is used.\n\techo \"${machine}-${os}${release}${abi}\"\n\texit ;;\n    *:Bitrig:*:*)\n\tUNAME_MACHINE_ARCH=`arch | sed 's/Bitrig.//'`\n\techo ${UNAME_MACHINE_ARCH}-unknown-bitrig${UNAME_RELEASE}\n\texit ;;\n    *:OpenBSD:*:*)\n\tUNAME_MACHINE_ARCH=`arch | sed 's/OpenBSD.//'`\n\techo ${UNAME_MACHINE_ARCH}-unknown-openbsd${UNAME_RELEASE}\n\texit ;;\n    *:LibertyBSD:*:*)\n\tUNAME_MACHINE_ARCH=`arch | sed 's/^.*BSD\\.//'`\n\techo ${UNAME_MACHINE_ARCH}-unknown-libertybsd${UNAME_RELEASE}\n\texit ;;\n    *:ekkoBSD:*:*)\n\techo ${UNAME_MACHINE}-unknown-ekkobsd${UNAME_RELEASE}\n\texit ;;\n    *:SolidBSD:*:*)\n\techo ${UNAME_MACHINE}-unknown-solidbsd${UNAME_RELEASE}\n\texit ;;\n    macppc:MirBSD:*:*)\n\techo powerpc-unknown-mirbsd${UNAME_RELEASE}\n\texit ;;\n    *:MirBSD:*:*)\n\techo ${UNAME_MACHINE}-unknown-mirbsd${UNAME_RELEASE}\n\texit ;;\n    *:Sortix:*:*)\n\techo ${UNAME_MACHINE}-unknown-sortix\n\texit ;;\n    alpha:OSF1:*:*)\n\tcase $UNAME_RELEASE in\n\t*4.0)\n\t\tUNAME_RELEASE=`/usr/sbin/sizer -v | awk '{print $3}'`\n\t\t;;\n\t*5.*)\n\t\tUNAME_RELEASE=`/usr/sbin/sizer -v | awk '{print $4}'`\n\t\t;;\n\tesac\n\t# According to Compaq, /usr/sbin/psrinfo has been available on\n\t# OSF/1 and Tru64 systems produced since 1995.  I hope that\n\t# covers most systems running today.  This code pipes the CPU\n\t# types through head -n 1, so we only detect the type of CPU 0.\n\tALPHA_CPU_TYPE=`/usr/sbin/psrinfo -v | sed -n -e 's/^  The alpha \\(.*\\) processor.*$/\\1/p' | head -n 1`\n\tcase \"$ALPHA_CPU_TYPE\" in\n\t    \"EV4 (21064)\")\n\t\tUNAME_MACHINE=alpha ;;\n\t    \"EV4.5 (21064)\")\n\t\tUNAME_MACHINE=alpha ;;\n\t    \"LCA4 (21066/21068)\")\n\t\tUNAME_MACHINE=alpha ;;\n\t    \"EV5 (21164)\")\n\t\tUNAME_MACHINE=alphaev5 ;;\n\t    \"EV5.6 (21164A)\")\n\t\tUNAME_MACHINE=alphaev56 ;;\n\t    \"EV5.6 (21164PC)\")\n\t\tUNAME_MACHINE=alphapca56 ;;\n\t    \"EV5.7 (21164PC)\")\n\t\tUNAME_MACHINE=alphapca57 ;;\n\t    \"EV6 (21264)\")\n\t\tUNAME_MACHINE=alphaev6 ;;\n\t    \"EV6.7 (21264A)\")\n\t\tUNAME_MACHINE=alphaev67 ;;\n\t    \"EV6.8CB (21264C)\")\n\t\tUNAME_MACHINE=alphaev68 ;;\n\t    \"EV6.8AL (21264B)\")\n\t\tUNAME_MACHINE=alphaev68 ;;\n\t    \"EV6.8CX (21264D)\")\n\t\tUNAME_MACHINE=alphaev68 ;;\n\t    \"EV6.9A (21264/EV69A)\")\n\t\tUNAME_MACHINE=alphaev69 ;;\n\t    \"EV7 (21364)\")\n\t\tUNAME_MACHINE=alphaev7 ;;\n\t    \"EV7.9 (21364A)\")\n\t\tUNAME_MACHINE=alphaev79 ;;\n\tesac\n\t# A Pn.n version is a patched version.\n\t# A Vn.n version is a released version.\n\t# A Tn.n version is a released field test version.\n\t# A Xn.n version is an unreleased experimental baselevel.\n\t# 1.2 uses \"1.2\" for uname -r.\n\techo ${UNAME_MACHINE}-dec-osf`echo ${UNAME_RELEASE} | sed -e 's/^[PVTX]//' | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz`\n\t# Reset EXIT trap before exiting to avoid spurious non-zero exit code.\n\texitcode=$?\n\ttrap '' 0\n\texit $exitcode ;;\n    Alpha\\ *:Windows_NT*:*)\n\t# How do we know it's Interix rather than the generic POSIX subsystem?\n\t# Should we change UNAME_MACHINE based on the output of uname instead\n\t# of the specific Alpha model?\n\techo alpha-pc-interix\n\texit ;;\n    21064:Windows_NT:50:3)\n\techo alpha-dec-winnt3.5\n\texit ;;\n    Amiga*:UNIX_System_V:4.0:*)\n\techo m68k-unknown-sysv4\n\texit ;;\n    *:[Aa]miga[Oo][Ss]:*:*)\n\techo ${UNAME_MACHINE}-unknown-amigaos\n\texit ;;\n    *:[Mm]orph[Oo][Ss]:*:*)\n\techo ${UNAME_MACHINE}-unknown-morphos\n\texit ;;\n    *:OS/390:*:*)\n\techo i370-ibm-openedition\n\texit ;;\n    *:z/VM:*:*)\n\techo s390-ibm-zvmoe\n\texit ;;\n    *:OS400:*:*)\n\techo powerpc-ibm-os400\n\texit ;;\n    arm:RISC*:1.[012]*:*|arm:riscix:1.[012]*:*)\n\techo arm-acorn-riscix${UNAME_RELEASE}\n\texit ;;\n    arm*:riscos:*:*|arm*:RISCOS:*:*)\n\techo arm-unknown-riscos\n\texit ;;\n    SR2?01:HI-UX/MPP:*:* | SR8000:HI-UX/MPP:*:*)\n\techo hppa1.1-hitachi-hiuxmpp\n\texit ;;\n    Pyramid*:OSx*:*:* | MIS*:OSx*:*:* | MIS*:SMP_DC-OSx*:*:*)\n\t# akee@wpdis03.wpafb.af.mil (Earle F. Ake) contributed MIS and NILE.\n\tif test \"`(/bin/universe) 2>/dev/null`\" = att ; then\n\t\techo pyramid-pyramid-sysv3\n\telse\n\t\techo pyramid-pyramid-bsd\n\tfi\n\texit ;;\n    NILE*:*:*:dcosx)\n\techo pyramid-pyramid-svr4\n\texit ;;\n    DRS?6000:unix:4.0:6*)\n\techo sparc-icl-nx6\n\texit ;;\n    DRS?6000:UNIX_SV:4.2*:7* | DRS?6000:isis:4.2*:7*)\n\tcase `/usr/bin/uname -p` in\n\t    sparc) echo sparc-icl-nx7; exit ;;\n\tesac ;;\n    s390x:SunOS:*:*)\n\techo ${UNAME_MACHINE}-ibm-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`\n\texit ;;\n    sun4H:SunOS:5.*:*)\n\techo sparc-hal-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`\n\texit ;;\n    sun4*:SunOS:5.*:* | tadpole*:SunOS:5.*:*)\n\techo sparc-sun-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`\n\texit ;;\n    i86pc:AuroraUX:5.*:* | i86xen:AuroraUX:5.*:*)\n\techo i386-pc-auroraux${UNAME_RELEASE}\n\texit ;;\n    i86pc:SunOS:5.*:* | i86xen:SunOS:5.*:*)\n\teval $set_cc_for_build\n\tSUN_ARCH=i386\n\t# If there is a compiler, see if it is configured for 64-bit objects.\n\t# Note that the Sun cc does not turn __LP64__ into 1 like gcc does.\n\t# This test works for both compilers.\n\tif [ \"$CC_FOR_BUILD\" != no_compiler_found ]; then\n\t    if (echo '#ifdef __amd64'; echo IS_64BIT_ARCH; echo '#endif') | \\\n\t\t(CCOPTS=\"\" $CC_FOR_BUILD -E - 2>/dev/null) | \\\n\t\tgrep IS_64BIT_ARCH >/dev/null\n\t    then\n\t\tSUN_ARCH=x86_64\n\t    fi\n\tfi\n\techo ${SUN_ARCH}-pc-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`\n\texit ;;\n    sun4*:SunOS:6*:*)\n\t# According to config.sub, this is the proper way to canonicalize\n\t# SunOS6.  Hard to guess exactly what SunOS6 will be like, but\n\t# it's likely to be more like Solaris than SunOS4.\n\techo sparc-sun-solaris3`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`\n\texit ;;\n    sun4*:SunOS:*:*)\n\tcase \"`/usr/bin/arch -k`\" in\n\t    Series*|S4*)\n\t\tUNAME_RELEASE=`uname -v`\n\t\t;;\n\tesac\n\t# Japanese Language versions have a version number like `4.1.3-JL'.\n\techo sparc-sun-sunos`echo ${UNAME_RELEASE}|sed -e 's/-/_/'`\n\texit ;;\n    sun3*:SunOS:*:*)\n\techo m68k-sun-sunos${UNAME_RELEASE}\n\texit ;;\n    sun*:*:4.2BSD:*)\n\tUNAME_RELEASE=`(sed 1q /etc/motd | awk '{print substr($5,1,3)}') 2>/dev/null`\n\ttest \"x${UNAME_RELEASE}\" = x && UNAME_RELEASE=3\n\tcase \"`/bin/arch`\" in\n\t    sun3)\n\t\techo m68k-sun-sunos${UNAME_RELEASE}\n\t\t;;\n\t    sun4)\n\t\techo sparc-sun-sunos${UNAME_RELEASE}\n\t\t;;\n\tesac\n\texit ;;\n    aushp:SunOS:*:*)\n\techo sparc-auspex-sunos${UNAME_RELEASE}\n\texit ;;\n    # The situation for MiNT is a little confusing.  The machine name\n    # can be virtually everything (everything which is not\n    # \"atarist\" or \"atariste\" at least should have a processor\n    # > m68000).  The system name ranges from \"MiNT\" over \"FreeMiNT\"\n    # to the lowercase version \"mint\" (or \"freemint\").  Finally\n    # the system name \"TOS\" denotes a system which is actually not\n    # MiNT.  But MiNT is downward compatible to TOS, so this should\n    # be no problem.\n    atarist[e]:*MiNT:*:* | atarist[e]:*mint:*:* | atarist[e]:*TOS:*:*)\n\techo m68k-atari-mint${UNAME_RELEASE}\n\texit ;;\n    atari*:*MiNT:*:* | atari*:*mint:*:* | atarist[e]:*TOS:*:*)\n\techo m68k-atari-mint${UNAME_RELEASE}\n\texit ;;\n    *falcon*:*MiNT:*:* | *falcon*:*mint:*:* | *falcon*:*TOS:*:*)\n\techo m68k-atari-mint${UNAME_RELEASE}\n\texit ;;\n    milan*:*MiNT:*:* | milan*:*mint:*:* | *milan*:*TOS:*:*)\n\techo m68k-milan-mint${UNAME_RELEASE}\n\texit ;;\n    hades*:*MiNT:*:* | hades*:*mint:*:* | *hades*:*TOS:*:*)\n\techo m68k-hades-mint${UNAME_RELEASE}\n\texit ;;\n    *:*MiNT:*:* | *:*mint:*:* | *:*TOS:*:*)\n\techo m68k-unknown-mint${UNAME_RELEASE}\n\texit ;;\n    m68k:machten:*:*)\n\techo m68k-apple-machten${UNAME_RELEASE}\n\texit ;;\n    powerpc:machten:*:*)\n\techo powerpc-apple-machten${UNAME_RELEASE}\n\texit ;;\n    RISC*:Mach:*:*)\n\techo mips-dec-mach_bsd4.3\n\texit ;;\n    RISC*:ULTRIX:*:*)\n\techo mips-dec-ultrix${UNAME_RELEASE}\n\texit ;;\n    VAX*:ULTRIX*:*:*)\n\techo vax-dec-ultrix${UNAME_RELEASE}\n\texit ;;\n    2020:CLIX:*:* | 2430:CLIX:*:*)\n\techo clipper-intergraph-clix${UNAME_RELEASE}\n\texit ;;\n    mips:*:*:UMIPS | mips:*:*:RISCos)\n\teval $set_cc_for_build\n\tsed 's/^\t//' << EOF >$dummy.c\n#ifdef __cplusplus\n#include <stdio.h>  /* for printf() prototype */\n\tint main (int argc, char *argv[]) {\n#else\n\tint main (argc, argv) int argc; char *argv[]; {\n#endif\n\t#if defined (host_mips) && defined (MIPSEB)\n\t#if defined (SYSTYPE_SYSV)\n\t  printf (\"mips-mips-riscos%ssysv\\n\", argv[1]); exit (0);\n\t#endif\n\t#if defined (SYSTYPE_SVR4)\n\t  printf (\"mips-mips-riscos%ssvr4\\n\", argv[1]); exit (0);\n\t#endif\n\t#if defined (SYSTYPE_BSD43) || defined(SYSTYPE_BSD)\n\t  printf (\"mips-mips-riscos%sbsd\\n\", argv[1]); exit (0);\n\t#endif\n\t#endif\n\t  exit (-1);\n\t}\nEOF\n\t$CC_FOR_BUILD -o $dummy $dummy.c &&\n\t  dummyarg=`echo \"${UNAME_RELEASE}\" | sed -n 's/\\([0-9]*\\).*/\\1/p'` &&\n\t  SYSTEM_NAME=`$dummy $dummyarg` &&\n\t    { echo \"$SYSTEM_NAME\"; exit; }\n\techo mips-mips-riscos${UNAME_RELEASE}\n\texit ;;\n    Motorola:PowerMAX_OS:*:*)\n\techo powerpc-motorola-powermax\n\texit ;;\n    Motorola:*:4.3:PL8-*)\n\techo powerpc-harris-powermax\n\texit ;;\n    Night_Hawk:*:*:PowerMAX_OS | Synergy:PowerMAX_OS:*:*)\n\techo powerpc-harris-powermax\n\texit ;;\n    Night_Hawk:Power_UNIX:*:*)\n\techo powerpc-harris-powerunix\n\texit ;;\n    m88k:CX/UX:7*:*)\n\techo m88k-harris-cxux7\n\texit ;;\n    m88k:*:4*:R4*)\n\techo m88k-motorola-sysv4\n\texit ;;\n    m88k:*:3*:R3*)\n\techo m88k-motorola-sysv3\n\texit ;;\n    AViiON:dgux:*:*)\n\t# DG/UX returns AViiON for all architectures\n\tUNAME_PROCESSOR=`/usr/bin/uname -p`\n\tif [ $UNAME_PROCESSOR = mc88100 ] || [ $UNAME_PROCESSOR = mc88110 ]\n\tthen\n\t    if [ ${TARGET_BINARY_INTERFACE}x = m88kdguxelfx ] || \\\n\t       [ ${TARGET_BINARY_INTERFACE}x = x ]\n\t    then\n\t\techo m88k-dg-dgux${UNAME_RELEASE}\n\t    else\n\t\techo m88k-dg-dguxbcs${UNAME_RELEASE}\n\t    fi\n\telse\n\t    echo i586-dg-dgux${UNAME_RELEASE}\n\tfi\n\texit ;;\n    M88*:DolphinOS:*:*)\t# DolphinOS (SVR3)\n\techo m88k-dolphin-sysv3\n\texit ;;\n    M88*:*:R3*:*)\n\t# Delta 88k system running SVR3\n\techo m88k-motorola-sysv3\n\texit ;;\n    XD88*:*:*:*) # Tektronix XD88 system running UTekV (SVR3)\n\techo m88k-tektronix-sysv3\n\texit ;;\n    Tek43[0-9][0-9]:UTek:*:*) # Tektronix 4300 system running UTek (BSD)\n\techo m68k-tektronix-bsd\n\texit ;;\n    *:IRIX*:*:*)\n\techo mips-sgi-irix`echo ${UNAME_RELEASE}|sed -e 's/-/_/g'`\n\texit ;;\n    ????????:AIX?:[12].1:2)   # AIX 2.2.1 or AIX 2.1.1 is RT/PC AIX.\n\techo romp-ibm-aix     # uname -m gives an 8 hex-code CPU id\n\texit ;;               # Note that: echo \"'`uname -s`'\" gives 'AIX '\n    i*86:AIX:*:*)\n\techo i386-ibm-aix\n\texit ;;\n    ia64:AIX:*:*)\n\tif [ -x /usr/bin/oslevel ] ; then\n\t\tIBM_REV=`/usr/bin/oslevel`\n\telse\n\t\tIBM_REV=${UNAME_VERSION}.${UNAME_RELEASE}\n\tfi\n\techo ${UNAME_MACHINE}-ibm-aix${IBM_REV}\n\texit ;;\n    *:AIX:2:3)\n\tif grep bos325 /usr/include/stdio.h >/dev/null 2>&1; then\n\t\teval $set_cc_for_build\n\t\tsed 's/^\t\t//' << EOF >$dummy.c\n\t\t#include <sys/systemcfg.h>\n\n\t\tmain()\n\t\t\t{\n\t\t\tif (!__power_pc())\n\t\t\t\texit(1);\n\t\t\tputs(\"powerpc-ibm-aix3.2.5\");\n\t\t\texit(0);\n\t\t\t}\nEOF\n\t\tif $CC_FOR_BUILD -o $dummy $dummy.c && SYSTEM_NAME=`$dummy`\n\t\tthen\n\t\t\techo \"$SYSTEM_NAME\"\n\t\telse\n\t\t\techo rs6000-ibm-aix3.2.5\n\t\tfi\n\telif grep bos324 /usr/include/stdio.h >/dev/null 2>&1; then\n\t\techo rs6000-ibm-aix3.2.4\n\telse\n\t\techo rs6000-ibm-aix3.2\n\tfi\n\texit ;;\n    *:AIX:*:[4567])\n\tIBM_CPU_ID=`/usr/sbin/lsdev -C -c processor -S available | sed 1q | awk '{ print $1 }'`\n\tif /usr/sbin/lsattr -El ${IBM_CPU_ID} | grep ' POWER' >/dev/null 2>&1; then\n\t\tIBM_ARCH=rs6000\n\telse\n\t\tIBM_ARCH=powerpc\n\tfi\n\tif [ -x /usr/bin/lslpp ] ; then\n\t\tIBM_REV=`/usr/bin/lslpp -Lqc bos.rte.libc |\n\t\t\t   awk -F: '{ print $3 }' | sed s/[0-9]*$/0/`\n\telse\n\t\tIBM_REV=${UNAME_VERSION}.${UNAME_RELEASE}\n\tfi\n\techo ${IBM_ARCH}-ibm-aix${IBM_REV}\n\texit ;;\n    *:AIX:*:*)\n\techo rs6000-ibm-aix\n\texit ;;\n    ibmrt:4.4BSD:*|romp-ibm:BSD:*)\n\techo romp-ibm-bsd4.4\n\texit ;;\n    ibmrt:*BSD:*|romp-ibm:BSD:*)            # covers RT/PC BSD and\n\techo romp-ibm-bsd${UNAME_RELEASE}   # 4.3 with uname added to\n\texit ;;                             # report: romp-ibm BSD 4.3\n    *:BOSX:*:*)\n\techo rs6000-bull-bosx\n\texit ;;\n    DPX/2?00:B.O.S.:*:*)\n\techo m68k-bull-sysv3\n\texit ;;\n    9000/[34]??:4.3bsd:1.*:*)\n\techo m68k-hp-bsd\n\texit ;;\n    hp300:4.4BSD:*:* | 9000/[34]??:4.3bsd:2.*:*)\n\techo m68k-hp-bsd4.4\n\texit ;;\n    9000/[34678]??:HP-UX:*:*)\n\tHPUX_REV=`echo ${UNAME_RELEASE}|sed -e 's/[^.]*.[0B]*//'`\n\tcase \"${UNAME_MACHINE}\" in\n\t    9000/31? )            HP_ARCH=m68000 ;;\n\t    9000/[34]?? )         HP_ARCH=m68k ;;\n\t    9000/[678][0-9][0-9])\n\t\tif [ -x /usr/bin/getconf ]; then\n\t\t    sc_cpu_version=`/usr/bin/getconf SC_CPU_VERSION 2>/dev/null`\n\t\t    sc_kernel_bits=`/usr/bin/getconf SC_KERNEL_BITS 2>/dev/null`\n\t\t    case \"${sc_cpu_version}\" in\n\t\t      523) HP_ARCH=hppa1.0 ;; # CPU_PA_RISC1_0\n\t\t      528) HP_ARCH=hppa1.1 ;; # CPU_PA_RISC1_1\n\t\t      532)                      # CPU_PA_RISC2_0\n\t\t\tcase \"${sc_kernel_bits}\" in\n\t\t\t  32) HP_ARCH=hppa2.0n ;;\n\t\t\t  64) HP_ARCH=hppa2.0w ;;\n\t\t\t  '') HP_ARCH=hppa2.0 ;;   # HP-UX 10.20\n\t\t\tesac ;;\n\t\t    esac\n\t\tfi\n\t\tif [ \"${HP_ARCH}\" = \"\" ]; then\n\t\t    eval $set_cc_for_build\n\t\t    sed 's/^\t\t//' << EOF >$dummy.c\n\n\t\t#define _HPUX_SOURCE\n\t\t#include <stdlib.h>\n\t\t#include <unistd.h>\n\n\t\tint main ()\n\t\t{\n\t\t#if defined(_SC_KERNEL_BITS)\n\t\t    long bits = sysconf(_SC_KERNEL_BITS);\n\t\t#endif\n\t\t    long cpu  = sysconf (_SC_CPU_VERSION);\n\n\t\t    switch (cpu)\n\t\t\t{\n\t\t\tcase CPU_PA_RISC1_0: puts (\"hppa1.0\"); break;\n\t\t\tcase CPU_PA_RISC1_1: puts (\"hppa1.1\"); break;\n\t\t\tcase CPU_PA_RISC2_0:\n\t\t#if defined(_SC_KERNEL_BITS)\n\t\t\t    switch (bits)\n\t\t\t\t{\n\t\t\t\tcase 64: puts (\"hppa2.0w\"); break;\n\t\t\t\tcase 32: puts (\"hppa2.0n\"); break;\n\t\t\t\tdefault: puts (\"hppa2.0\"); break;\n\t\t\t\t} break;\n\t\t#else  /* !defined(_SC_KERNEL_BITS) */\n\t\t\t    puts (\"hppa2.0\"); break;\n\t\t#endif\n\t\t\tdefault: puts (\"hppa1.0\"); break;\n\t\t\t}\n\t\t    exit (0);\n\t\t}\nEOF\n\t\t    (CCOPTS=\"\" $CC_FOR_BUILD -o $dummy $dummy.c 2>/dev/null) && HP_ARCH=`$dummy`\n\t\t    test -z \"$HP_ARCH\" && HP_ARCH=hppa\n\t\tfi ;;\n\tesac\n\tif [ ${HP_ARCH} = hppa2.0w ]\n\tthen\n\t    eval $set_cc_for_build\n\n\t    # hppa2.0w-hp-hpux* has a 64-bit kernel and a compiler generating\n\t    # 32-bit code.  hppa64-hp-hpux* has the same kernel and a compiler\n\t    # generating 64-bit code.  GNU and HP use different nomenclature:\n\t    #\n\t    # $ CC_FOR_BUILD=cc ./config.guess\n\t    # => hppa2.0w-hp-hpux11.23\n\t    # $ CC_FOR_BUILD=\"cc +DA2.0w\" ./config.guess\n\t    # => hppa64-hp-hpux11.23\n\n\t    if echo __LP64__ | (CCOPTS=\"\" $CC_FOR_BUILD -E - 2>/dev/null) |\n\t\tgrep -q __LP64__\n\t    then\n\t\tHP_ARCH=hppa2.0w\n\t    else\n\t\tHP_ARCH=hppa64\n\t    fi\n\tfi\n\techo ${HP_ARCH}-hp-hpux${HPUX_REV}\n\texit ;;\n    ia64:HP-UX:*:*)\n\tHPUX_REV=`echo ${UNAME_RELEASE}|sed -e 's/[^.]*.[0B]*//'`\n\techo ia64-hp-hpux${HPUX_REV}\n\texit ;;\n    3050*:HI-UX:*:*)\n\teval $set_cc_for_build\n\tsed 's/^\t//' << EOF >$dummy.c\n\t#include <unistd.h>\n\tint\n\tmain ()\n\t{\n\t  long cpu = sysconf (_SC_CPU_VERSION);\n\t  /* The order matters, because CPU_IS_HP_MC68K erroneously returns\n\t     true for CPU_PA_RISC1_0.  CPU_IS_PA_RISC returns correct\n\t     results, however.  */\n\t  if (CPU_IS_PA_RISC (cpu))\n\t    {\n\t      switch (cpu)\n\t\t{\n\t\t  case CPU_PA_RISC1_0: puts (\"hppa1.0-hitachi-hiuxwe2\"); break;\n\t\t  case CPU_PA_RISC1_1: puts (\"hppa1.1-hitachi-hiuxwe2\"); break;\n\t\t  case CPU_PA_RISC2_0: puts (\"hppa2.0-hitachi-hiuxwe2\"); break;\n\t\t  default: puts (\"hppa-hitachi-hiuxwe2\"); break;\n\t\t}\n\t    }\n\t  else if (CPU_IS_HP_MC68K (cpu))\n\t    puts (\"m68k-hitachi-hiuxwe2\");\n\t  else puts (\"unknown-hitachi-hiuxwe2\");\n\t  exit (0);\n\t}\nEOF\n\t$CC_FOR_BUILD -o $dummy $dummy.c && SYSTEM_NAME=`$dummy` &&\n\t\t{ echo \"$SYSTEM_NAME\"; exit; }\n\techo unknown-hitachi-hiuxwe2\n\texit ;;\n    9000/7??:4.3bsd:*:* | 9000/8?[79]:4.3bsd:*:* )\n\techo hppa1.1-hp-bsd\n\texit ;;\n    9000/8??:4.3bsd:*:*)\n\techo hppa1.0-hp-bsd\n\texit ;;\n    *9??*:MPE/iX:*:* | *3000*:MPE/iX:*:*)\n\techo hppa1.0-hp-mpeix\n\texit ;;\n    hp7??:OSF1:*:* | hp8?[79]:OSF1:*:* )\n\techo hppa1.1-hp-osf\n\texit ;;\n    hp8??:OSF1:*:*)\n\techo hppa1.0-hp-osf\n\texit ;;\n    i*86:OSF1:*:*)\n\tif [ -x /usr/sbin/sysversion ] ; then\n\t    echo ${UNAME_MACHINE}-unknown-osf1mk\n\telse\n\t    echo ${UNAME_MACHINE}-unknown-osf1\n\tfi\n\texit ;;\n    parisc*:Lites*:*:*)\n\techo hppa1.1-hp-lites\n\texit ;;\n    C1*:ConvexOS:*:* | convex:ConvexOS:C1*:*)\n\techo c1-convex-bsd\n\texit ;;\n    C2*:ConvexOS:*:* | convex:ConvexOS:C2*:*)\n\tif getsysinfo -f scalar_acc\n\tthen echo c32-convex-bsd\n\telse echo c2-convex-bsd\n\tfi\n\texit ;;\n    C34*:ConvexOS:*:* | convex:ConvexOS:C34*:*)\n\techo c34-convex-bsd\n\texit ;;\n    C38*:ConvexOS:*:* | convex:ConvexOS:C38*:*)\n\techo c38-convex-bsd\n\texit ;;\n    C4*:ConvexOS:*:* | convex:ConvexOS:C4*:*)\n\techo c4-convex-bsd\n\texit ;;\n    CRAY*Y-MP:*:*:*)\n\techo ymp-cray-unicos${UNAME_RELEASE} | sed -e 's/\\.[^.]*$/.X/'\n\texit ;;\n    CRAY*[A-Z]90:*:*:*)\n\techo ${UNAME_MACHINE}-cray-unicos${UNAME_RELEASE} \\\n\t| sed -e 's/CRAY.*\\([A-Z]90\\)/\\1/' \\\n\t      -e y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/ \\\n\t      -e 's/\\.[^.]*$/.X/'\n\texit ;;\n    CRAY*TS:*:*:*)\n\techo t90-cray-unicos${UNAME_RELEASE} | sed -e 's/\\.[^.]*$/.X/'\n\texit ;;\n    CRAY*T3E:*:*:*)\n\techo alphaev5-cray-unicosmk${UNAME_RELEASE} | sed -e 's/\\.[^.]*$/.X/'\n\texit ;;\n    CRAY*SV1:*:*:*)\n\techo sv1-cray-unicos${UNAME_RELEASE} | sed -e 's/\\.[^.]*$/.X/'\n\texit ;;\n    *:UNICOS/mp:*:*)\n\techo craynv-cray-unicosmp${UNAME_RELEASE} | sed -e 's/\\.[^.]*$/.X/'\n\texit ;;\n    F30[01]:UNIX_System_V:*:* | F700:UNIX_System_V:*:*)\n\tFUJITSU_PROC=`uname -m | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz`\n\tFUJITSU_SYS=`uname -p | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz | sed -e 's/\\///'`\n\tFUJITSU_REL=`echo ${UNAME_RELEASE} | sed -e 's/ /_/'`\n\techo \"${FUJITSU_PROC}-fujitsu-${FUJITSU_SYS}${FUJITSU_REL}\"\n\texit ;;\n    5000:UNIX_System_V:4.*:*)\n\tFUJITSU_SYS=`uname -p | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz | sed -e 's/\\///'`\n\tFUJITSU_REL=`echo ${UNAME_RELEASE} | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz | sed -e 's/ /_/'`\n\techo \"sparc-fujitsu-${FUJITSU_SYS}${FUJITSU_REL}\"\n\texit ;;\n    i*86:BSD/386:*:* | i*86:BSD/OS:*:* | *:Ascend\\ Embedded/OS:*:*)\n\techo ${UNAME_MACHINE}-pc-bsdi${UNAME_RELEASE}\n\texit ;;\n    sparc*:BSD/OS:*:*)\n\techo sparc-unknown-bsdi${UNAME_RELEASE}\n\texit ;;\n    *:BSD/OS:*:*)\n\techo ${UNAME_MACHINE}-unknown-bsdi${UNAME_RELEASE}\n\texit ;;\n    *:FreeBSD:*:*)\n\tUNAME_PROCESSOR=`/usr/bin/uname -p`\n\tcase ${UNAME_PROCESSOR} in\n\t    amd64)\n\t\techo x86_64-unknown-freebsd`echo ${UNAME_RELEASE}|sed -e 's/[-(].*//'` ;;\n\t    *)\n\t\techo ${UNAME_PROCESSOR}-unknown-freebsd`echo ${UNAME_RELEASE}|sed -e 's/[-(].*//'` ;;\n\tesac\n\texit ;;\n    i*:CYGWIN*:*)\n\techo ${UNAME_MACHINE}-pc-cygwin\n\texit ;;\n    *:MINGW64*:*)\n\techo ${UNAME_MACHINE}-pc-mingw64\n\texit ;;\n    *:MINGW*:*)\n\techo ${UNAME_MACHINE}-pc-mingw32\n\texit ;;\n    *:MSYS*:*)\n\techo ${UNAME_MACHINE}-pc-msys\n\texit ;;\n    i*:windows32*:*)\n\t# uname -m includes \"-pc\" on this system.\n\techo ${UNAME_MACHINE}-mingw32\n\texit ;;\n    i*:PW*:*)\n\techo ${UNAME_MACHINE}-pc-pw32\n\texit ;;\n    *:Interix*:*)\n\tcase ${UNAME_MACHINE} in\n\t    x86)\n\t\techo i586-pc-interix${UNAME_RELEASE}\n\t\texit ;;\n\t    authenticamd | genuineintel | EM64T)\n\t\techo x86_64-unknown-interix${UNAME_RELEASE}\n\t\texit ;;\n\t    IA64)\n\t\techo ia64-unknown-interix${UNAME_RELEASE}\n\t\texit ;;\n\tesac ;;\n    [345]86:Windows_95:* | [345]86:Windows_98:* | [345]86:Windows_NT:*)\n\techo i${UNAME_MACHINE}-pc-mks\n\texit ;;\n    8664:Windows_NT:*)\n\techo x86_64-pc-mks\n\texit ;;\n    i*:Windows_NT*:* | Pentium*:Windows_NT*:*)\n\t# How do we know it's Interix rather than the generic POSIX subsystem?\n\t# It also conflicts with pre-2.0 versions of AT&T UWIN. Should we\n\t# UNAME_MACHINE based on the output of uname instead of i386?\n\techo i586-pc-interix\n\texit ;;\n    i*:UWIN*:*)\n\techo ${UNAME_MACHINE}-pc-uwin\n\texit ;;\n    amd64:CYGWIN*:*:* | x86_64:CYGWIN*:*:*)\n\techo x86_64-unknown-cygwin\n\texit ;;\n    p*:CYGWIN*:*)\n\techo powerpcle-unknown-cygwin\n\texit ;;\n    prep*:SunOS:5.*:*)\n\techo powerpcle-unknown-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'`\n\texit ;;\n    *:GNU:*:*)\n\t# the GNU system\n\techo `echo ${UNAME_MACHINE}|sed -e 's,[-/].*$,,'`-unknown-${LIBC}`echo ${UNAME_RELEASE}|sed -e 's,/.*$,,'`\n\texit ;;\n    *:GNU/*:*:*)\n\t# other systems with GNU libc and userland\n\techo ${UNAME_MACHINE}-unknown-`echo ${UNAME_SYSTEM} | sed 's,^[^/]*/,,' | tr \"[:upper:]\" \"[:lower:]\"``echo ${UNAME_RELEASE}|sed -e 's/[-(].*//'`-${LIBC}\n\texit ;;\n    i*86:Minix:*:*)\n\techo ${UNAME_MACHINE}-pc-minix\n\texit ;;\n    aarch64:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    aarch64_be:Linux:*:*)\n\tUNAME_MACHINE=aarch64_be\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    alpha:Linux:*:*)\n\tcase `sed -n '/^cpu model/s/^.*: \\(.*\\)/\\1/p' < /proc/cpuinfo` in\n\t  EV5)   UNAME_MACHINE=alphaev5 ;;\n\t  EV56)  UNAME_MACHINE=alphaev56 ;;\n\t  PCA56) UNAME_MACHINE=alphapca56 ;;\n\t  PCA57) UNAME_MACHINE=alphapca56 ;;\n\t  EV6)   UNAME_MACHINE=alphaev6 ;;\n\t  EV67)  UNAME_MACHINE=alphaev67 ;;\n\t  EV68*) UNAME_MACHINE=alphaev68 ;;\n\tesac\n\tobjdump --private-headers /bin/sh | grep -q ld.so.1\n\tif test \"$?\" = 0 ; then LIBC=gnulibc1 ; fi\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    arc:Linux:*:* | arceb:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    arm*:Linux:*:*)\n\teval $set_cc_for_build\n\tif echo __ARM_EABI__ | $CC_FOR_BUILD -E - 2>/dev/null \\\n\t    | grep -q __ARM_EABI__\n\tthen\n\t    echo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\telse\n\t    if echo __ARM_PCS_VFP | $CC_FOR_BUILD -E - 2>/dev/null \\\n\t\t| grep -q __ARM_PCS_VFP\n\t    then\n\t\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}eabi\n\t    else\n\t\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}eabihf\n\t    fi\n\tfi\n\texit ;;\n    avr32*:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    cris:Linux:*:*)\n\techo ${UNAME_MACHINE}-axis-linux-${LIBC}\n\texit ;;\n    crisv32:Linux:*:*)\n\techo ${UNAME_MACHINE}-axis-linux-${LIBC}\n\texit ;;\n    e2k:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    frv:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    hexagon:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    i*86:Linux:*:*)\n\techo ${UNAME_MACHINE}-pc-linux-${LIBC}\n\texit ;;\n    ia64:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    k1om:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    m32r*:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    m68*:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    mips:Linux:*:* | mips64:Linux:*:*)\n\teval $set_cc_for_build\n\tsed 's/^\t//' << EOF >$dummy.c\n\t#undef CPU\n\t#undef ${UNAME_MACHINE}\n\t#undef ${UNAME_MACHINE}el\n\t#if defined(__MIPSEL__) || defined(__MIPSEL) || defined(_MIPSEL) || defined(MIPSEL)\n\tCPU=${UNAME_MACHINE}el\n\t#else\n\t#if defined(__MIPSEB__) || defined(__MIPSEB) || defined(_MIPSEB) || defined(MIPSEB)\n\tCPU=${UNAME_MACHINE}\n\t#else\n\tCPU=\n\t#endif\n\t#endif\nEOF\n\teval `$CC_FOR_BUILD -E $dummy.c 2>/dev/null | grep '^CPU'`\n\ttest x\"${CPU}\" != x && { echo \"${CPU}-unknown-linux-${LIBC}\"; exit; }\n\t;;\n    mips64el:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    openrisc*:Linux:*:*)\n\techo or1k-unknown-linux-${LIBC}\n\texit ;;\n    or32:Linux:*:* | or1k*:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    padre:Linux:*:*)\n\techo sparc-unknown-linux-${LIBC}\n\texit ;;\n    parisc64:Linux:*:* | hppa64:Linux:*:*)\n\techo hppa64-unknown-linux-${LIBC}\n\texit ;;\n    parisc:Linux:*:* | hppa:Linux:*:*)\n\t# Look for CPU level\n\tcase `grep '^cpu[^a-z]*:' /proc/cpuinfo 2>/dev/null | cut -d' ' -f2` in\n\t  PA7*) echo hppa1.1-unknown-linux-${LIBC} ;;\n\t  PA8*) echo hppa2.0-unknown-linux-${LIBC} ;;\n\t  *)    echo hppa-unknown-linux-${LIBC} ;;\n\tesac\n\texit ;;\n    ppc64:Linux:*:*)\n\techo powerpc64-unknown-linux-${LIBC}\n\texit ;;\n    ppc:Linux:*:*)\n\techo powerpc-unknown-linux-${LIBC}\n\texit ;;\n    ppc64le:Linux:*:*)\n\techo powerpc64le-unknown-linux-${LIBC}\n\texit ;;\n    ppcle:Linux:*:*)\n\techo powerpcle-unknown-linux-${LIBC}\n\texit ;;\n    riscv32:Linux:*:* | riscv64:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    s390:Linux:*:* | s390x:Linux:*:*)\n\techo ${UNAME_MACHINE}-ibm-linux-${LIBC}\n\texit ;;\n    sh64*:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    sh*:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    sparc:Linux:*:* | sparc64:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    tile*:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    vax:Linux:*:*)\n\techo ${UNAME_MACHINE}-dec-linux-${LIBC}\n\texit ;;\n    x86_64:Linux:*:*)\n\techo ${UNAME_MACHINE}-pc-linux-${LIBC}\n\texit ;;\n    xtensa*:Linux:*:*)\n\techo ${UNAME_MACHINE}-unknown-linux-${LIBC}\n\texit ;;\n    i*86:DYNIX/ptx:4*:*)\n\t# ptx 4.0 does uname -s correctly, with DYNIX/ptx in there.\n\t# earlier versions are messed up and put the nodename in both\n\t# sysname and nodename.\n\techo i386-sequent-sysv4\n\texit ;;\n    i*86:UNIX_SV:4.2MP:2.*)\n\t# Unixware is an offshoot of SVR4, but it has its own version\n\t# number series starting with 2...\n\t# I am not positive that other SVR4 systems won't match this,\n\t# I just have to hope.  -- rms.\n\t# Use sysv4.2uw... so that sysv4* matches it.\n\techo ${UNAME_MACHINE}-pc-sysv4.2uw${UNAME_VERSION}\n\texit ;;\n    i*86:OS/2:*:*)\n\t# If we were able to find `uname', then EMX Unix compatibility\n\t# is probably installed.\n\techo ${UNAME_MACHINE}-pc-os2-emx\n\texit ;;\n    i*86:XTS-300:*:STOP)\n\techo ${UNAME_MACHINE}-unknown-stop\n\texit ;;\n    i*86:atheos:*:*)\n\techo ${UNAME_MACHINE}-unknown-atheos\n\texit ;;\n    i*86:syllable:*:*)\n\techo ${UNAME_MACHINE}-pc-syllable\n\texit ;;\n    i*86:LynxOS:2.*:* | i*86:LynxOS:3.[01]*:* | i*86:LynxOS:4.[02]*:*)\n\techo i386-unknown-lynxos${UNAME_RELEASE}\n\texit ;;\n    i*86:*DOS:*:*)\n\techo ${UNAME_MACHINE}-pc-msdosdjgpp\n\texit ;;\n    i*86:*:4.*:* | i*86:SYSTEM_V:4.*:*)\n\tUNAME_REL=`echo ${UNAME_RELEASE} | sed 's/\\/MP$//'`\n\tif grep Novell /usr/include/link.h >/dev/null 2>/dev/null; then\n\t\techo ${UNAME_MACHINE}-univel-sysv${UNAME_REL}\n\telse\n\t\techo ${UNAME_MACHINE}-pc-sysv${UNAME_REL}\n\tfi\n\texit ;;\n    i*86:*:5:[678]*)\n\t# UnixWare 7.x, OpenUNIX and OpenServer 6.\n\tcase `/bin/uname -X | grep \"^Machine\"` in\n\t    *486*)\t     UNAME_MACHINE=i486 ;;\n\t    *Pentium)\t     UNAME_MACHINE=i586 ;;\n\t    *Pent*|*Celeron) UNAME_MACHINE=i686 ;;\n\tesac\n\techo ${UNAME_MACHINE}-unknown-sysv${UNAME_RELEASE}${UNAME_SYSTEM}${UNAME_VERSION}\n\texit ;;\n    i*86:*:3.2:*)\n\tif test -f /usr/options/cb.name; then\n\t\tUNAME_REL=`sed -n 's/.*Version //p' </usr/options/cb.name`\n\t\techo ${UNAME_MACHINE}-pc-isc$UNAME_REL\n\telif /bin/uname -X 2>/dev/null >/dev/null ; then\n\t\tUNAME_REL=`(/bin/uname -X|grep Release|sed -e 's/.*= //')`\n\t\t(/bin/uname -X|grep i80486 >/dev/null) && UNAME_MACHINE=i486\n\t\t(/bin/uname -X|grep '^Machine.*Pentium' >/dev/null) \\\n\t\t\t&& UNAME_MACHINE=i586\n\t\t(/bin/uname -X|grep '^Machine.*Pent *II' >/dev/null) \\\n\t\t\t&& UNAME_MACHINE=i686\n\t\t(/bin/uname -X|grep '^Machine.*Pentium Pro' >/dev/null) \\\n\t\t\t&& UNAME_MACHINE=i686\n\t\techo ${UNAME_MACHINE}-pc-sco$UNAME_REL\n\telse\n\t\techo ${UNAME_MACHINE}-pc-sysv32\n\tfi\n\texit ;;\n    pc:*:*:*)\n\t# Left here for compatibility:\n\t# uname -m prints for DJGPP always 'pc', but it prints nothing about\n\t# the processor, so we play safe by assuming i586.\n\t# Note: whatever this is, it MUST be the same as what config.sub\n\t# prints for the \"djgpp\" host, or else GDB configure will decide that\n\t# this is a cross-build.\n\techo i586-pc-msdosdjgpp\n\texit ;;\n    Intel:Mach:3*:*)\n\techo i386-pc-mach3\n\texit ;;\n    paragon:*:*:*)\n\techo i860-intel-osf1\n\texit ;;\n    i860:*:4.*:*) # i860-SVR4\n\tif grep Stardent /usr/include/sys/uadmin.h >/dev/null 2>&1 ; then\n\t  echo i860-stardent-sysv${UNAME_RELEASE} # Stardent Vistra i860-SVR4\n\telse # Add other i860-SVR4 vendors below as they are discovered.\n\t  echo i860-unknown-sysv${UNAME_RELEASE}  # Unknown i860-SVR4\n\tfi\n\texit ;;\n    mini*:CTIX:SYS*5:*)\n\t# \"miniframe\"\n\techo m68010-convergent-sysv\n\texit ;;\n    mc68k:UNIX:SYSTEM5:3.51m)\n\techo m68k-convergent-sysv\n\texit ;;\n    M680?0:D-NIX:5.3:*)\n\techo m68k-diab-dnix\n\texit ;;\n    M68*:*:R3V[5678]*:*)\n\ttest -r /sysV68 && { echo 'm68k-motorola-sysv'; exit; } ;;\n    3[345]??:*:4.0:3.0 | 3[34]??A:*:4.0:3.0 | 3[34]??,*:*:4.0:3.0 | 3[34]??/*:*:4.0:3.0 | 4400:*:4.0:3.0 | 4850:*:4.0:3.0 | SKA40:*:4.0:3.0 | SDS2:*:4.0:3.0 | SHG2:*:4.0:3.0 | S7501*:*:4.0:3.0)\n\tOS_REL=''\n\ttest -r /etc/.relid \\\n\t&& OS_REL=.`sed -n 's/[^ ]* [^ ]* \\([0-9][0-9]\\).*/\\1/p' < /etc/.relid`\n\t/bin/uname -p 2>/dev/null | grep 86 >/dev/null \\\n\t  && { echo i486-ncr-sysv4.3${OS_REL}; exit; }\n\t/bin/uname -p 2>/dev/null | /bin/grep entium >/dev/null \\\n\t  && { echo i586-ncr-sysv4.3${OS_REL}; exit; } ;;\n    3[34]??:*:4.0:* | 3[34]??,*:*:4.0:*)\n\t/bin/uname -p 2>/dev/null | grep 86 >/dev/null \\\n\t  && { echo i486-ncr-sysv4; exit; } ;;\n    NCR*:*:4.2:* | MPRAS*:*:4.2:*)\n\tOS_REL='.3'\n\ttest -r /etc/.relid \\\n\t    && OS_REL=.`sed -n 's/[^ ]* [^ ]* \\([0-9][0-9]\\).*/\\1/p' < /etc/.relid`\n\t/bin/uname -p 2>/dev/null | grep 86 >/dev/null \\\n\t    && { echo i486-ncr-sysv4.3${OS_REL}; exit; }\n\t/bin/uname -p 2>/dev/null | /bin/grep entium >/dev/null \\\n\t    && { echo i586-ncr-sysv4.3${OS_REL}; exit; }\n\t/bin/uname -p 2>/dev/null | /bin/grep pteron >/dev/null \\\n\t    && { echo i586-ncr-sysv4.3${OS_REL}; exit; } ;;\n    m68*:LynxOS:2.*:* | m68*:LynxOS:3.0*:*)\n\techo m68k-unknown-lynxos${UNAME_RELEASE}\n\texit ;;\n    mc68030:UNIX_System_V:4.*:*)\n\techo m68k-atari-sysv4\n\texit ;;\n    TSUNAMI:LynxOS:2.*:*)\n\techo sparc-unknown-lynxos${UNAME_RELEASE}\n\texit ;;\n    rs6000:LynxOS:2.*:*)\n\techo rs6000-unknown-lynxos${UNAME_RELEASE}\n\texit ;;\n    PowerPC:LynxOS:2.*:* | PowerPC:LynxOS:3.[01]*:* | PowerPC:LynxOS:4.[02]*:*)\n\techo powerpc-unknown-lynxos${UNAME_RELEASE}\n\texit ;;\n    SM[BE]S:UNIX_SV:*:*)\n\techo mips-dde-sysv${UNAME_RELEASE}\n\texit ;;\n    RM*:ReliantUNIX-*:*:*)\n\techo mips-sni-sysv4\n\texit ;;\n    RM*:SINIX-*:*:*)\n\techo mips-sni-sysv4\n\texit ;;\n    *:SINIX-*:*:*)\n\tif uname -p 2>/dev/null >/dev/null ; then\n\t\tUNAME_MACHINE=`(uname -p) 2>/dev/null`\n\t\techo ${UNAME_MACHINE}-sni-sysv4\n\telse\n\t\techo ns32k-sni-sysv\n\tfi\n\texit ;;\n    PENTIUM:*:4.0*:*)\t# Unisys `ClearPath HMP IX 4000' SVR4/MP effort\n\t\t\t# says <Richard.M.Bartel@ccMail.Census.GOV>\n\techo i586-unisys-sysv4\n\texit ;;\n    *:UNIX_System_V:4*:FTX*)\n\t# From Gerald Hewes <hewes@openmarket.com>.\n\t# How about differentiating between stratus architectures? -djm\n\techo hppa1.1-stratus-sysv4\n\texit ;;\n    *:*:*:FTX*)\n\t# From seanf@swdc.stratus.com.\n\techo i860-stratus-sysv4\n\texit ;;\n    i*86:VOS:*:*)\n\t# From Paul.Green@stratus.com.\n\techo ${UNAME_MACHINE}-stratus-vos\n\texit ;;\n    *:VOS:*:*)\n\t# From Paul.Green@stratus.com.\n\techo hppa1.1-stratus-vos\n\texit ;;\n    mc68*:A/UX:*:*)\n\techo m68k-apple-aux${UNAME_RELEASE}\n\texit ;;\n    news*:NEWS-OS:6*:*)\n\techo mips-sony-newsos6\n\texit ;;\n    R[34]000:*System_V*:*:* | R4000:UNIX_SYSV:*:* | R*000:UNIX_SV:*:*)\n\tif [ -d /usr/nec ]; then\n\t\techo mips-nec-sysv${UNAME_RELEASE}\n\telse\n\t\techo mips-unknown-sysv${UNAME_RELEASE}\n\tfi\n\texit ;;\n    BeBox:BeOS:*:*)\t# BeOS running on hardware made by Be, PPC only.\n\techo powerpc-be-beos\n\texit ;;\n    BeMac:BeOS:*:*)\t# BeOS running on Mac or Mac clone, PPC only.\n\techo powerpc-apple-beos\n\texit ;;\n    BePC:BeOS:*:*)\t# BeOS running on Intel PC compatible.\n\techo i586-pc-beos\n\texit ;;\n    BePC:Haiku:*:*)\t# Haiku running on Intel PC compatible.\n\techo i586-pc-haiku\n\texit ;;\n    x86_64:Haiku:*:*)\n\techo x86_64-unknown-haiku\n\texit ;;\n    SX-4:SUPER-UX:*:*)\n\techo sx4-nec-superux${UNAME_RELEASE}\n\texit ;;\n    SX-5:SUPER-UX:*:*)\n\techo sx5-nec-superux${UNAME_RELEASE}\n\texit ;;\n    SX-6:SUPER-UX:*:*)\n\techo sx6-nec-superux${UNAME_RELEASE}\n\texit ;;\n    SX-7:SUPER-UX:*:*)\n\techo sx7-nec-superux${UNAME_RELEASE}\n\texit ;;\n    SX-8:SUPER-UX:*:*)\n\techo sx8-nec-superux${UNAME_RELEASE}\n\texit ;;\n    SX-8R:SUPER-UX:*:*)\n\techo sx8r-nec-superux${UNAME_RELEASE}\n\texit ;;\n    SX-ACE:SUPER-UX:*:*)\n\techo sxace-nec-superux${UNAME_RELEASE}\n\texit ;;\n    Power*:Rhapsody:*:*)\n\techo powerpc-apple-rhapsody${UNAME_RELEASE}\n\texit ;;\n    *:Rhapsody:*:*)\n\techo ${UNAME_MACHINE}-apple-rhapsody${UNAME_RELEASE}\n\texit ;;\n    *:Darwin:*:*)\n\tUNAME_PROCESSOR=`uname -p` || UNAME_PROCESSOR=unknown\n\teval $set_cc_for_build\n\tif test \"$UNAME_PROCESSOR\" = unknown ; then\n\t    UNAME_PROCESSOR=powerpc\n\tfi\n\tif test `echo \"$UNAME_RELEASE\" | sed -e 's/\\..*//'` -le 10 ; then\n\t    if [ \"$CC_FOR_BUILD\" != no_compiler_found ]; then\n\t\tif (echo '#ifdef __LP64__'; echo IS_64BIT_ARCH; echo '#endif') | \\\n\t\t    (CCOPTS=\"\" $CC_FOR_BUILD -E - 2>/dev/null) | \\\n\t\t    grep IS_64BIT_ARCH >/dev/null\n\t\tthen\n\t\t    case $UNAME_PROCESSOR in\n\t\t\ti386) UNAME_PROCESSOR=x86_64 ;;\n\t\t\tpowerpc) UNAME_PROCESSOR=powerpc64 ;;\n\t\t    esac\n\t\tfi\n\t    fi\n\telif test \"$UNAME_PROCESSOR\" = i386 ; then\n\t    # Avoid executing cc on OS X 10.9, as it ships with a stub\n\t    # that puts up a graphical alert prompting to install\n\t    # developer tools.  Any system running Mac OS X 10.7 or\n\t    # later (Darwin 11 and later) is required to have a 64-bit\n\t    # processor. This is not true of the ARM version of Darwin\n\t    # that Apple uses in portable devices.\n\t    UNAME_PROCESSOR=x86_64\n\tfi\n\techo ${UNAME_PROCESSOR}-apple-darwin${UNAME_RELEASE}\n\texit ;;\n    *:procnto*:*:* | *:QNX:[0123456789]*:*)\n\tUNAME_PROCESSOR=`uname -p`\n\tif test \"$UNAME_PROCESSOR\" = x86; then\n\t\tUNAME_PROCESSOR=i386\n\t\tUNAME_MACHINE=pc\n\tfi\n\techo ${UNAME_PROCESSOR}-${UNAME_MACHINE}-nto-qnx${UNAME_RELEASE}\n\texit ;;\n    *:QNX:*:4*)\n\techo i386-pc-qnx\n\texit ;;\n    NEO-?:NONSTOP_KERNEL:*:*)\n\techo neo-tandem-nsk${UNAME_RELEASE}\n\texit ;;\n    NSE-*:NONSTOP_KERNEL:*:*)\n\techo nse-tandem-nsk${UNAME_RELEASE}\n\texit ;;\n    NSR-?:NONSTOP_KERNEL:*:*)\n\techo nsr-tandem-nsk${UNAME_RELEASE}\n\texit ;;\n    *:NonStop-UX:*:*)\n\techo mips-compaq-nonstopux\n\texit ;;\n    BS2000:POSIX*:*:*)\n\techo bs2000-siemens-sysv\n\texit ;;\n    DS/*:UNIX_System_V:*:*)\n\techo ${UNAME_MACHINE}-${UNAME_SYSTEM}-${UNAME_RELEASE}\n\texit ;;\n    *:Plan9:*:*)\n\t# \"uname -m\" is not consistent, so use $cputype instead. 386\n\t# is converted to i386 for consistency with other x86\n\t# operating systems.\n\tif test \"$cputype\" = 386; then\n\t    UNAME_MACHINE=i386\n\telse\n\t    UNAME_MACHINE=\"$cputype\"\n\tfi\n\techo ${UNAME_MACHINE}-unknown-plan9\n\texit ;;\n    *:TOPS-10:*:*)\n\techo pdp10-unknown-tops10\n\texit ;;\n    *:TENEX:*:*)\n\techo pdp10-unknown-tenex\n\texit ;;\n    KS10:TOPS-20:*:* | KL10:TOPS-20:*:* | TYPE4:TOPS-20:*:*)\n\techo pdp10-dec-tops20\n\texit ;;\n    XKL-1:TOPS-20:*:* | TYPE5:TOPS-20:*:*)\n\techo pdp10-xkl-tops20\n\texit ;;\n    *:TOPS-20:*:*)\n\techo pdp10-unknown-tops20\n\texit ;;\n    *:ITS:*:*)\n\techo pdp10-unknown-its\n\texit ;;\n    SEI:*:*:SEIUX)\n\techo mips-sei-seiux${UNAME_RELEASE}\n\texit ;;\n    *:DragonFly:*:*)\n\techo ${UNAME_MACHINE}-unknown-dragonfly`echo ${UNAME_RELEASE}|sed -e 's/[-(].*//'`\n\texit ;;\n    *:*VMS:*:*)\n\tUNAME_MACHINE=`(uname -p) 2>/dev/null`\n\tcase \"${UNAME_MACHINE}\" in\n\t    A*) echo alpha-dec-vms ; exit ;;\n\t    I*) echo ia64-dec-vms ; exit ;;\n\t    V*) echo vax-dec-vms ; exit ;;\n\tesac ;;\n    *:XENIX:*:SysV)\n\techo i386-pc-xenix\n\texit ;;\n    i*86:skyos:*:*)\n\techo ${UNAME_MACHINE}-pc-skyos`echo ${UNAME_RELEASE} | sed -e 's/ .*$//'`\n\texit ;;\n    i*86:rdos:*:*)\n\techo ${UNAME_MACHINE}-pc-rdos\n\texit ;;\n    i*86:AROS:*:*)\n\techo ${UNAME_MACHINE}-pc-aros\n\texit ;;\n    x86_64:VMkernel:*:*)\n\techo ${UNAME_MACHINE}-unknown-esx\n\texit ;;\n    amd64:Isilon\\ OneFS:*:*)\n\techo x86_64-unknown-onefs\n\texit ;;\nesac\n\ncat >&2 <<EOF\n$0: unable to guess system type\n\nThis script (version $timestamp), has failed to recognize the\noperating system you are using. If your script is old, overwrite\nconfig.guess and config.sub with the latest versions from:\n\n  http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess\nand\n  http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub\n\nIf $0 has already been updated, send the following data and any\ninformation you think might be pertinent to config-patches@gnu.org to\nprovide the necessary information to handle your system.\n\nconfig.guess timestamp = $timestamp\n\nuname -m = `(uname -m) 2>/dev/null || echo unknown`\nuname -r = `(uname -r) 2>/dev/null || echo unknown`\nuname -s = `(uname -s) 2>/dev/null || echo unknown`\nuname -v = `(uname -v) 2>/dev/null || echo unknown`\n\n/usr/bin/uname -p = `(/usr/bin/uname -p) 2>/dev/null`\n/bin/uname -X     = `(/bin/uname -X) 2>/dev/null`\n\nhostinfo               = `(hostinfo) 2>/dev/null`\n/bin/universe          = `(/bin/universe) 2>/dev/null`\n/usr/bin/arch -k       = `(/usr/bin/arch -k) 2>/dev/null`\n/bin/arch              = `(/bin/arch) 2>/dev/null`\n/usr/bin/oslevel       = `(/usr/bin/oslevel) 2>/dev/null`\n/usr/convex/getsysinfo = `(/usr/convex/getsysinfo) 2>/dev/null`\n\nUNAME_MACHINE = ${UNAME_MACHINE}\nUNAME_RELEASE = ${UNAME_RELEASE}\nUNAME_SYSTEM  = ${UNAME_SYSTEM}\nUNAME_VERSION = ${UNAME_VERSION}\nEOF\n\nexit 1\n\n# Local variables:\n# eval: (add-hook 'write-file-hooks 'time-stamp)\n# time-stamp-start: \"timestamp='\"\n# time-stamp-format: \"%:y-%02m-%02d\"\n# time-stamp-end: \"'\"\n# End:\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/build-aux/config.sub",
    "content": "#! /bin/sh\n# Configuration validation subroutine script.\n#   Copyright 1992-2016 Free Software Foundation, Inc.\n\ntimestamp='2016-11-04'\n\n# This file is free software; you can redistribute it and/or modify it\n# under the terms of the GNU General Public License as published by\n# the Free Software Foundation; either version 3 of the License, or\n# (at your option) any later version.\n#\n# This program is distributed in the hope that it will be useful, but\n# WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n# General Public License for more details.\n#\n# You should have received a copy of the GNU General Public License\n# along with this program; if not, see <http://www.gnu.org/licenses/>.\n#\n# As a special exception to the GNU General Public License, if you\n# distribute this file as part of a program that contains a\n# configuration script generated by Autoconf, you may include it under\n# the same distribution terms that you use for the rest of that\n# program.  This Exception is an additional permission under section 7\n# of the GNU General Public License, version 3 (\"GPLv3\").\n\n\n# Please send patches to <config-patches@gnu.org>.\n#\n# Configuration subroutine to validate and canonicalize a configuration type.\n# Supply the specified configuration type as an argument.\n# If it is invalid, we print an error message on stderr and exit with code 1.\n# Otherwise, we print the canonical config type on stdout and succeed.\n\n# You can get the latest version of this script from:\n# http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub\n\n# This file is supposed to be the same for all GNU packages\n# and recognize all the CPU types, system types and aliases\n# that are meaningful with *any* GNU software.\n# Each package is responsible for reporting which valid configurations\n# it does not support.  The user should be able to distinguish\n# a failure to support a valid configuration from a meaningless\n# configuration.\n\n# The goal of this file is to map all the various variations of a given\n# machine specification into a single specification in the form:\n#\tCPU_TYPE-MANUFACTURER-OPERATING_SYSTEM\n# or in some cases, the newer four-part form:\n#\tCPU_TYPE-MANUFACTURER-KERNEL-OPERATING_SYSTEM\n# It is wrong to echo any other type of specification.\n\nme=`echo \"$0\" | sed -e 's,.*/,,'`\n\nusage=\"\\\nUsage: $0 [OPTION] CPU-MFR-OPSYS or ALIAS\n\nCanonicalize a configuration name.\n\nOperation modes:\n  -h, --help         print this help, then exit\n  -t, --time-stamp   print date of last modification, then exit\n  -v, --version      print version number, then exit\n\nReport bugs and patches to <config-patches@gnu.org>.\"\n\nversion=\"\\\nGNU config.sub ($timestamp)\n\nCopyright 1992-2016 Free Software Foundation, Inc.\n\nThis is free software; see the source for copying conditions.  There is NO\nwarranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\"\n\nhelp=\"\nTry \\`$me --help' for more information.\"\n\n# Parse command line\nwhile test $# -gt 0 ; do\n  case $1 in\n    --time-stamp | --time* | -t )\n       echo \"$timestamp\" ; exit ;;\n    --version | -v )\n       echo \"$version\" ; exit ;;\n    --help | --h* | -h )\n       echo \"$usage\"; exit ;;\n    -- )     # Stop option processing\n       shift; break ;;\n    - )\t# Use stdin as input.\n       break ;;\n    -* )\n       echo \"$me: invalid option $1$help\"\n       exit 1 ;;\n\n    *local*)\n       # First pass through any local machine types.\n       echo $1\n       exit ;;\n\n    * )\n       break ;;\n  esac\ndone\n\ncase $# in\n 0) echo \"$me: missing argument$help\" >&2\n    exit 1;;\n 1) ;;\n *) echo \"$me: too many arguments$help\" >&2\n    exit 1;;\nesac\n\n# Separate what the user gave into CPU-COMPANY and OS or KERNEL-OS (if any).\n# Here we must recognize all the valid KERNEL-OS combinations.\nmaybe_os=`echo $1 | sed 's/^\\(.*\\)-\\([^-]*-[^-]*\\)$/\\2/'`\ncase $maybe_os in\n  nto-qnx* | linux-gnu* | linux-android* | linux-dietlibc | linux-newlib* | \\\n  linux-musl* | linux-uclibc* | uclinux-uclibc* | uclinux-gnu* | kfreebsd*-gnu* | \\\n  knetbsd*-gnu* | netbsd*-gnu* | netbsd*-eabi* | \\\n  kopensolaris*-gnu* | cloudabi*-eabi* | \\\n  storm-chaos* | os2-emx* | rtmk-nova*)\n    os=-$maybe_os\n    basic_machine=`echo $1 | sed 's/^\\(.*\\)-\\([^-]*-[^-]*\\)$/\\1/'`\n    ;;\n  android-linux)\n    os=-linux-android\n    basic_machine=`echo $1 | sed 's/^\\(.*\\)-\\([^-]*-[^-]*\\)$/\\1/'`-unknown\n    ;;\n  *)\n    basic_machine=`echo $1 | sed 's/-[^-]*$//'`\n    if [ $basic_machine != $1 ]\n    then os=`echo $1 | sed 's/.*-/-/'`\n    else os=; fi\n    ;;\nesac\n\n### Let's recognize common machines as not being operating systems so\n### that things like config.sub decstation-3100 work.  We also\n### recognize some manufacturers as not being operating systems, so we\n### can provide default operating systems below.\ncase $os in\n\t-sun*os*)\n\t\t# Prevent following clause from handling this invalid input.\n\t\t;;\n\t-dec* | -mips* | -sequent* | -encore* | -pc532* | -sgi* | -sony* | \\\n\t-att* | -7300* | -3300* | -delta* | -motorola* | -sun[234]* | \\\n\t-unicom* | -ibm* | -next | -hp | -isi* | -apollo | -altos* | \\\n\t-convergent* | -ncr* | -news | -32* | -3600* | -3100* | -hitachi* |\\\n\t-c[123]* | -convex* | -sun | -crds | -omron* | -dg | -ultra | -tti* | \\\n\t-harris | -dolphin | -highlevel | -gould | -cbm | -ns | -masscomp | \\\n\t-apple | -axis | -knuth | -cray | -microblaze*)\n\t\tos=\n\t\tbasic_machine=$1\n\t\t;;\n\t-bluegene*)\n\t\tos=-cnk\n\t\t;;\n\t-sim | -cisco | -oki | -wec | -winbond)\n\t\tos=\n\t\tbasic_machine=$1\n\t\t;;\n\t-scout)\n\t\t;;\n\t-wrs)\n\t\tos=-vxworks\n\t\tbasic_machine=$1\n\t\t;;\n\t-chorusos*)\n\t\tos=-chorusos\n\t\tbasic_machine=$1\n\t\t;;\n\t-chorusrdb)\n\t\tos=-chorusrdb\n\t\tbasic_machine=$1\n\t\t;;\n\t-hiux*)\n\t\tos=-hiuxwe2\n\t\t;;\n\t-sco6)\n\t\tos=-sco5v6\n\t\tbasic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`\n\t\t;;\n\t-sco5)\n\t\tos=-sco3.2v5\n\t\tbasic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`\n\t\t;;\n\t-sco4)\n\t\tos=-sco3.2v4\n\t\tbasic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`\n\t\t;;\n\t-sco3.2.[4-9]*)\n\t\tos=`echo $os | sed -e 's/sco3.2./sco3.2v/'`\n\t\tbasic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`\n\t\t;;\n\t-sco3.2v[4-9]*)\n\t\t# Don't forget version if it is 3.2v4 or newer.\n\t\tbasic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`\n\t\t;;\n\t-sco5v6*)\n\t\t# Don't forget version if it is 3.2v4 or newer.\n\t\tbasic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`\n\t\t;;\n\t-sco*)\n\t\tos=-sco3.2v2\n\t\tbasic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`\n\t\t;;\n\t-udk*)\n\t\tbasic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`\n\t\t;;\n\t-isc)\n\t\tos=-isc2.2\n\t\tbasic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`\n\t\t;;\n\t-clix*)\n\t\tbasic_machine=clipper-intergraph\n\t\t;;\n\t-isc*)\n\t\tbasic_machine=`echo $1 | sed -e 's/86-.*/86-pc/'`\n\t\t;;\n\t-lynx*178)\n\t\tos=-lynxos178\n\t\t;;\n\t-lynx*5)\n\t\tos=-lynxos5\n\t\t;;\n\t-lynx*)\n\t\tos=-lynxos\n\t\t;;\n\t-ptx*)\n\t\tbasic_machine=`echo $1 | sed -e 's/86-.*/86-sequent/'`\n\t\t;;\n\t-windowsnt*)\n\t\tos=`echo $os | sed -e 's/windowsnt/winnt/'`\n\t\t;;\n\t-psos*)\n\t\tos=-psos\n\t\t;;\n\t-mint | -mint[0-9]*)\n\t\tbasic_machine=m68k-atari\n\t\tos=-mint\n\t\t;;\nesac\n\n# Decode aliases for certain CPU-COMPANY combinations.\ncase $basic_machine in\n\t# Recognize the basic CPU types without company name.\n\t# Some are omitted here because they have special meanings below.\n\t1750a | 580 \\\n\t| a29k \\\n\t| aarch64 | aarch64_be \\\n\t| alpha | alphaev[4-8] | alphaev56 | alphaev6[78] | alphapca5[67] \\\n\t| alpha64 | alpha64ev[4-8] | alpha64ev56 | alpha64ev6[78] | alpha64pca5[67] \\\n\t| am33_2.0 \\\n\t| arc | arceb \\\n\t| arm | arm[bl]e | arme[lb] | armv[2-8] | armv[3-8][lb] | armv7[arm] \\\n\t| avr | avr32 \\\n\t| ba \\\n\t| be32 | be64 \\\n\t| bfin \\\n\t| c4x | c8051 | clipper \\\n\t| d10v | d30v | dlx | dsp16xx \\\n\t| e2k | epiphany \\\n\t| fido | fr30 | frv | ft32 \\\n\t| h8300 | h8500 | hppa | hppa1.[01] | hppa2.0 | hppa2.0[nw] | hppa64 \\\n\t| hexagon \\\n\t| i370 | i860 | i960 | ia64 \\\n\t| ip2k | iq2000 \\\n\t| k1om \\\n\t| le32 | le64 \\\n\t| lm32 \\\n\t| m32c | m32r | m32rle | m68000 | m68k | m88k \\\n\t| maxq | mb | microblaze | microblazeel | mcore | mep | metag \\\n\t| mips | mipsbe | mipseb | mipsel | mipsle \\\n\t| mips16 \\\n\t| mips64 | mips64el \\\n\t| mips64octeon | mips64octeonel \\\n\t| mips64orion | mips64orionel \\\n\t| mips64r5900 | mips64r5900el \\\n\t| mips64vr | mips64vrel \\\n\t| mips64vr4100 | mips64vr4100el \\\n\t| mips64vr4300 | mips64vr4300el \\\n\t| mips64vr5000 | mips64vr5000el \\\n\t| mips64vr5900 | mips64vr5900el \\\n\t| mipsisa32 | mipsisa32el \\\n\t| mipsisa32r2 | mipsisa32r2el \\\n\t| mipsisa32r6 | mipsisa32r6el \\\n\t| mipsisa64 | mipsisa64el \\\n\t| mipsisa64r2 | mipsisa64r2el \\\n\t| mipsisa64r6 | mipsisa64r6el \\\n\t| mipsisa64sb1 | mipsisa64sb1el \\\n\t| mipsisa64sr71k | mipsisa64sr71kel \\\n\t| mipsr5900 | mipsr5900el \\\n\t| mipstx39 | mipstx39el \\\n\t| mn10200 | mn10300 \\\n\t| moxie \\\n\t| mt \\\n\t| msp430 \\\n\t| nds32 | nds32le | nds32be \\\n\t| nios | nios2 | nios2eb | nios2el \\\n\t| ns16k | ns32k \\\n\t| open8 | or1k | or1knd | or32 \\\n\t| pdp10 | pdp11 | pj | pjl \\\n\t| powerpc | powerpc64 | powerpc64le | powerpcle \\\n\t| pru \\\n\t| pyramid \\\n\t| riscv32 | riscv64 \\\n\t| rl78 | rx \\\n\t| score \\\n\t| sh | sh[1234] | sh[24]a | sh[24]aeb | sh[23]e | sh[234]eb | sheb | shbe | shle | sh[1234]le | sh3ele \\\n\t| sh64 | sh64le \\\n\t| sparc | sparc64 | sparc64b | sparc64v | sparc86x | sparclet | sparclite \\\n\t| sparcv8 | sparcv9 | sparcv9b | sparcv9v \\\n\t| spu \\\n\t| tahoe | tic4x | tic54x | tic55x | tic6x | tic80 | tron \\\n\t| ubicom32 \\\n\t| v850 | v850e | v850e1 | v850e2 | v850es | v850e2v3 \\\n\t| visium \\\n\t| we32k \\\n\t| x86 | xc16x | xstormy16 | xtensa \\\n\t| z8k | z80)\n\t\tbasic_machine=$basic_machine-unknown\n\t\t;;\n\tc54x)\n\t\tbasic_machine=tic54x-unknown\n\t\t;;\n\tc55x)\n\t\tbasic_machine=tic55x-unknown\n\t\t;;\n\tc6x)\n\t\tbasic_machine=tic6x-unknown\n\t\t;;\n\tleon|leon[3-9])\n\t\tbasic_machine=sparc-$basic_machine\n\t\t;;\n\tm6811 | m68hc11 | m6812 | m68hc12 | m68hcs12x | nvptx | picochip)\n\t\tbasic_machine=$basic_machine-unknown\n\t\tos=-none\n\t\t;;\n\tm88110 | m680[12346]0 | m683?2 | m68360 | m5200 | v70 | w65 | z8k)\n\t\t;;\n\tms1)\n\t\tbasic_machine=mt-unknown\n\t\t;;\n\n\tstrongarm | thumb | xscale)\n\t\tbasic_machine=arm-unknown\n\t\t;;\n\txgate)\n\t\tbasic_machine=$basic_machine-unknown\n\t\tos=-none\n\t\t;;\n\txscaleeb)\n\t\tbasic_machine=armeb-unknown\n\t\t;;\n\n\txscaleel)\n\t\tbasic_machine=armel-unknown\n\t\t;;\n\n\t# We use `pc' rather than `unknown'\n\t# because (1) that's what they normally are, and\n\t# (2) the word \"unknown\" tends to confuse beginning users.\n\ti*86 | x86_64)\n\t  basic_machine=$basic_machine-pc\n\t  ;;\n\t# Object if more than one company name word.\n\t*-*-*)\n\t\techo Invalid configuration \\`$1\\': machine \\`$basic_machine\\' not recognized 1>&2\n\t\texit 1\n\t\t;;\n\t# Recognize the basic CPU types with company name.\n\t580-* \\\n\t| a29k-* \\\n\t| aarch64-* | aarch64_be-* \\\n\t| alpha-* | alphaev[4-8]-* | alphaev56-* | alphaev6[78]-* \\\n\t| alpha64-* | alpha64ev[4-8]-* | alpha64ev56-* | alpha64ev6[78]-* \\\n\t| alphapca5[67]-* | alpha64pca5[67]-* | arc-* | arceb-* \\\n\t| arm-*  | armbe-* | armle-* | armeb-* | armv*-* \\\n\t| avr-* | avr32-* \\\n\t| ba-* \\\n\t| be32-* | be64-* \\\n\t| bfin-* | bs2000-* \\\n\t| c[123]* | c30-* | [cjt]90-* | c4x-* \\\n\t| c8051-* | clipper-* | craynv-* | cydra-* \\\n\t| d10v-* | d30v-* | dlx-* \\\n\t| e2k-* | elxsi-* \\\n\t| f30[01]-* | f700-* | fido-* | fr30-* | frv-* | fx80-* \\\n\t| h8300-* | h8500-* \\\n\t| hppa-* | hppa1.[01]-* | hppa2.0-* | hppa2.0[nw]-* | hppa64-* \\\n\t| hexagon-* \\\n\t| i*86-* | i860-* | i960-* | ia64-* \\\n\t| ip2k-* | iq2000-* \\\n\t| k1om-* \\\n\t| le32-* | le64-* \\\n\t| lm32-* \\\n\t| m32c-* | m32r-* | m32rle-* \\\n\t| m68000-* | m680[012346]0-* | m68360-* | m683?2-* | m68k-* \\\n\t| m88110-* | m88k-* | maxq-* | mcore-* | metag-* \\\n\t| microblaze-* | microblazeel-* \\\n\t| mips-* | mipsbe-* | mipseb-* | mipsel-* | mipsle-* \\\n\t| mips16-* \\\n\t| mips64-* | mips64el-* \\\n\t| mips64octeon-* | mips64octeonel-* \\\n\t| mips64orion-* | mips64orionel-* \\\n\t| mips64r5900-* | mips64r5900el-* \\\n\t| mips64vr-* | mips64vrel-* \\\n\t| mips64vr4100-* | mips64vr4100el-* \\\n\t| mips64vr4300-* | mips64vr4300el-* \\\n\t| mips64vr5000-* | mips64vr5000el-* \\\n\t| mips64vr5900-* | mips64vr5900el-* \\\n\t| mipsisa32-* | mipsisa32el-* \\\n\t| mipsisa32r2-* | mipsisa32r2el-* \\\n\t| mipsisa32r6-* | mipsisa32r6el-* \\\n\t| mipsisa64-* | mipsisa64el-* \\\n\t| mipsisa64r2-* | mipsisa64r2el-* \\\n\t| mipsisa64r6-* | mipsisa64r6el-* \\\n\t| mipsisa64sb1-* | mipsisa64sb1el-* \\\n\t| mipsisa64sr71k-* | mipsisa64sr71kel-* \\\n\t| mipsr5900-* | mipsr5900el-* \\\n\t| mipstx39-* | mipstx39el-* \\\n\t| mmix-* \\\n\t| mt-* \\\n\t| msp430-* \\\n\t| nds32-* | nds32le-* | nds32be-* \\\n\t| nios-* | nios2-* | nios2eb-* | nios2el-* \\\n\t| none-* | np1-* | ns16k-* | ns32k-* \\\n\t| open8-* \\\n\t| or1k*-* \\\n\t| orion-* \\\n\t| pdp10-* | pdp11-* | pj-* | pjl-* | pn-* | power-* \\\n\t| powerpc-* | powerpc64-* | powerpc64le-* | powerpcle-* \\\n\t| pru-* \\\n\t| pyramid-* \\\n\t| riscv32-* | riscv64-* \\\n\t| rl78-* | romp-* | rs6000-* | rx-* \\\n\t| sh-* | sh[1234]-* | sh[24]a-* | sh[24]aeb-* | sh[23]e-* | sh[34]eb-* | sheb-* | shbe-* \\\n\t| shle-* | sh[1234]le-* | sh3ele-* | sh64-* | sh64le-* \\\n\t| sparc-* | sparc64-* | sparc64b-* | sparc64v-* | sparc86x-* | sparclet-* \\\n\t| sparclite-* \\\n\t| sparcv8-* | sparcv9-* | sparcv9b-* | sparcv9v-* | sv1-* | sx*-* \\\n\t| tahoe-* \\\n\t| tic30-* | tic4x-* | tic54x-* | tic55x-* | tic6x-* | tic80-* \\\n\t| tile*-* \\\n\t| tron-* \\\n\t| ubicom32-* \\\n\t| v850-* | v850e-* | v850e1-* | v850es-* | v850e2-* | v850e2v3-* \\\n\t| vax-* \\\n\t| visium-* \\\n\t| we32k-* \\\n\t| x86-* | x86_64-* | xc16x-* | xps100-* \\\n\t| xstormy16-* | xtensa*-* \\\n\t| ymp-* \\\n\t| z8k-* | z80-*)\n\t\t;;\n\t# Recognize the basic CPU types without company name, with glob match.\n\txtensa*)\n\t\tbasic_machine=$basic_machine-unknown\n\t\t;;\n\t# Recognize the various machine names and aliases which stand\n\t# for a CPU type and a company and sometimes even an OS.\n\t386bsd)\n\t\tbasic_machine=i386-unknown\n\t\tos=-bsd\n\t\t;;\n\t3b1 | 7300 | 7300-att | att-7300 | pc7300 | safari | unixpc)\n\t\tbasic_machine=m68000-att\n\t\t;;\n\t3b*)\n\t\tbasic_machine=we32k-att\n\t\t;;\n\ta29khif)\n\t\tbasic_machine=a29k-amd\n\t\tos=-udi\n\t\t;;\n\tabacus)\n\t\tbasic_machine=abacus-unknown\n\t\t;;\n\tadobe68k)\n\t\tbasic_machine=m68010-adobe\n\t\tos=-scout\n\t\t;;\n\talliant | fx80)\n\t\tbasic_machine=fx80-alliant\n\t\t;;\n\taltos | altos3068)\n\t\tbasic_machine=m68k-altos\n\t\t;;\n\tam29k)\n\t\tbasic_machine=a29k-none\n\t\tos=-bsd\n\t\t;;\n\tamd64)\n\t\tbasic_machine=x86_64-pc\n\t\t;;\n\tamd64-*)\n\t\tbasic_machine=x86_64-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tamdahl)\n\t\tbasic_machine=580-amdahl\n\t\tos=-sysv\n\t\t;;\n\tamiga | amiga-*)\n\t\tbasic_machine=m68k-unknown\n\t\t;;\n\tamigaos | amigados)\n\t\tbasic_machine=m68k-unknown\n\t\tos=-amigaos\n\t\t;;\n\tamigaunix | amix)\n\t\tbasic_machine=m68k-unknown\n\t\tos=-sysv4\n\t\t;;\n\tapollo68)\n\t\tbasic_machine=m68k-apollo\n\t\tos=-sysv\n\t\t;;\n\tapollo68bsd)\n\t\tbasic_machine=m68k-apollo\n\t\tos=-bsd\n\t\t;;\n\taros)\n\t\tbasic_machine=i386-pc\n\t\tos=-aros\n\t\t;;\n\tasmjs)\n\t\tbasic_machine=asmjs-unknown\n\t\t;;\n\taux)\n\t\tbasic_machine=m68k-apple\n\t\tos=-aux\n\t\t;;\n\tbalance)\n\t\tbasic_machine=ns32k-sequent\n\t\tos=-dynix\n\t\t;;\n\tblackfin)\n\t\tbasic_machine=bfin-unknown\n\t\tos=-linux\n\t\t;;\n\tblackfin-*)\n\t\tbasic_machine=bfin-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\tos=-linux\n\t\t;;\n\tbluegene*)\n\t\tbasic_machine=powerpc-ibm\n\t\tos=-cnk\n\t\t;;\n\tc54x-*)\n\t\tbasic_machine=tic54x-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tc55x-*)\n\t\tbasic_machine=tic55x-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tc6x-*)\n\t\tbasic_machine=tic6x-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tc90)\n\t\tbasic_machine=c90-cray\n\t\tos=-unicos\n\t\t;;\n\tcegcc)\n\t\tbasic_machine=arm-unknown\n\t\tos=-cegcc\n\t\t;;\n\tconvex-c1)\n\t\tbasic_machine=c1-convex\n\t\tos=-bsd\n\t\t;;\n\tconvex-c2)\n\t\tbasic_machine=c2-convex\n\t\tos=-bsd\n\t\t;;\n\tconvex-c32)\n\t\tbasic_machine=c32-convex\n\t\tos=-bsd\n\t\t;;\n\tconvex-c34)\n\t\tbasic_machine=c34-convex\n\t\tos=-bsd\n\t\t;;\n\tconvex-c38)\n\t\tbasic_machine=c38-convex\n\t\tos=-bsd\n\t\t;;\n\tcray | j90)\n\t\tbasic_machine=j90-cray\n\t\tos=-unicos\n\t\t;;\n\tcraynv)\n\t\tbasic_machine=craynv-cray\n\t\tos=-unicosmp\n\t\t;;\n\tcr16 | cr16-*)\n\t\tbasic_machine=cr16-unknown\n\t\tos=-elf\n\t\t;;\n\tcrds | unos)\n\t\tbasic_machine=m68k-crds\n\t\t;;\n\tcrisv32 | crisv32-* | etraxfs*)\n\t\tbasic_machine=crisv32-axis\n\t\t;;\n\tcris | cris-* | etrax*)\n\t\tbasic_machine=cris-axis\n\t\t;;\n\tcrx)\n\t\tbasic_machine=crx-unknown\n\t\tos=-elf\n\t\t;;\n\tda30 | da30-*)\n\t\tbasic_machine=m68k-da30\n\t\t;;\n\tdecstation | decstation-3100 | pmax | pmax-* | pmin | dec3100 | decstatn)\n\t\tbasic_machine=mips-dec\n\t\t;;\n\tdecsystem10* | dec10*)\n\t\tbasic_machine=pdp10-dec\n\t\tos=-tops10\n\t\t;;\n\tdecsystem20* | dec20*)\n\t\tbasic_machine=pdp10-dec\n\t\tos=-tops20\n\t\t;;\n\tdelta | 3300 | motorola-3300 | motorola-delta \\\n\t      | 3300-motorola | delta-motorola)\n\t\tbasic_machine=m68k-motorola\n\t\t;;\n\tdelta88)\n\t\tbasic_machine=m88k-motorola\n\t\tos=-sysv3\n\t\t;;\n\tdicos)\n\t\tbasic_machine=i686-pc\n\t\tos=-dicos\n\t\t;;\n\tdjgpp)\n\t\tbasic_machine=i586-pc\n\t\tos=-msdosdjgpp\n\t\t;;\n\tdpx20 | dpx20-*)\n\t\tbasic_machine=rs6000-bull\n\t\tos=-bosx\n\t\t;;\n\tdpx2* | dpx2*-bull)\n\t\tbasic_machine=m68k-bull\n\t\tos=-sysv3\n\t\t;;\n\te500v[12])\n\t\tbasic_machine=powerpc-unknown\n\t\tos=$os\"spe\"\n\t\t;;\n\te500v[12]-*)\n\t\tbasic_machine=powerpc-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\tos=$os\"spe\"\n\t\t;;\n\tebmon29k)\n\t\tbasic_machine=a29k-amd\n\t\tos=-ebmon\n\t\t;;\n\telxsi)\n\t\tbasic_machine=elxsi-elxsi\n\t\tos=-bsd\n\t\t;;\n\tencore | umax | mmax)\n\t\tbasic_machine=ns32k-encore\n\t\t;;\n\tes1800 | OSE68k | ose68k | ose | OSE)\n\t\tbasic_machine=m68k-ericsson\n\t\tos=-ose\n\t\t;;\n\tfx2800)\n\t\tbasic_machine=i860-alliant\n\t\t;;\n\tgenix)\n\t\tbasic_machine=ns32k-ns\n\t\t;;\n\tgmicro)\n\t\tbasic_machine=tron-gmicro\n\t\tos=-sysv\n\t\t;;\n\tgo32)\n\t\tbasic_machine=i386-pc\n\t\tos=-go32\n\t\t;;\n\th3050r* | hiux*)\n\t\tbasic_machine=hppa1.1-hitachi\n\t\tos=-hiuxwe2\n\t\t;;\n\th8300hms)\n\t\tbasic_machine=h8300-hitachi\n\t\tos=-hms\n\t\t;;\n\th8300xray)\n\t\tbasic_machine=h8300-hitachi\n\t\tos=-xray\n\t\t;;\n\th8500hms)\n\t\tbasic_machine=h8500-hitachi\n\t\tos=-hms\n\t\t;;\n\tharris)\n\t\tbasic_machine=m88k-harris\n\t\tos=-sysv3\n\t\t;;\n\thp300-*)\n\t\tbasic_machine=m68k-hp\n\t\t;;\n\thp300bsd)\n\t\tbasic_machine=m68k-hp\n\t\tos=-bsd\n\t\t;;\n\thp300hpux)\n\t\tbasic_machine=m68k-hp\n\t\tos=-hpux\n\t\t;;\n\thp3k9[0-9][0-9] | hp9[0-9][0-9])\n\t\tbasic_machine=hppa1.0-hp\n\t\t;;\n\thp9k2[0-9][0-9] | hp9k31[0-9])\n\t\tbasic_machine=m68000-hp\n\t\t;;\n\thp9k3[2-9][0-9])\n\t\tbasic_machine=m68k-hp\n\t\t;;\n\thp9k6[0-9][0-9] | hp6[0-9][0-9])\n\t\tbasic_machine=hppa1.0-hp\n\t\t;;\n\thp9k7[0-79][0-9] | hp7[0-79][0-9])\n\t\tbasic_machine=hppa1.1-hp\n\t\t;;\n\thp9k78[0-9] | hp78[0-9])\n\t\t# FIXME: really hppa2.0-hp\n\t\tbasic_machine=hppa1.1-hp\n\t\t;;\n\thp9k8[67]1 | hp8[67]1 | hp9k80[24] | hp80[24] | hp9k8[78]9 | hp8[78]9 | hp9k893 | hp893)\n\t\t# FIXME: really hppa2.0-hp\n\t\tbasic_machine=hppa1.1-hp\n\t\t;;\n\thp9k8[0-9][13679] | hp8[0-9][13679])\n\t\tbasic_machine=hppa1.1-hp\n\t\t;;\n\thp9k8[0-9][0-9] | hp8[0-9][0-9])\n\t\tbasic_machine=hppa1.0-hp\n\t\t;;\n\thppa-next)\n\t\tos=-nextstep3\n\t\t;;\n\thppaosf)\n\t\tbasic_machine=hppa1.1-hp\n\t\tos=-osf\n\t\t;;\n\thppro)\n\t\tbasic_machine=hppa1.1-hp\n\t\tos=-proelf\n\t\t;;\n\ti370-ibm* | ibm*)\n\t\tbasic_machine=i370-ibm\n\t\t;;\n\ti*86v32)\n\t\tbasic_machine=`echo $1 | sed -e 's/86.*/86-pc/'`\n\t\tos=-sysv32\n\t\t;;\n\ti*86v4*)\n\t\tbasic_machine=`echo $1 | sed -e 's/86.*/86-pc/'`\n\t\tos=-sysv4\n\t\t;;\n\ti*86v)\n\t\tbasic_machine=`echo $1 | sed -e 's/86.*/86-pc/'`\n\t\tos=-sysv\n\t\t;;\n\ti*86sol2)\n\t\tbasic_machine=`echo $1 | sed -e 's/86.*/86-pc/'`\n\t\tos=-solaris2\n\t\t;;\n\ti386mach)\n\t\tbasic_machine=i386-mach\n\t\tos=-mach\n\t\t;;\n\ti386-vsta | vsta)\n\t\tbasic_machine=i386-unknown\n\t\tos=-vsta\n\t\t;;\n\tiris | iris4d)\n\t\tbasic_machine=mips-sgi\n\t\tcase $os in\n\t\t    -irix*)\n\t\t\t;;\n\t\t    *)\n\t\t\tos=-irix4\n\t\t\t;;\n\t\tesac\n\t\t;;\n\tisi68 | isi)\n\t\tbasic_machine=m68k-isi\n\t\tos=-sysv\n\t\t;;\n\tleon-*|leon[3-9]-*)\n\t\tbasic_machine=sparc-`echo $basic_machine | sed 's/-.*//'`\n\t\t;;\n\tm68knommu)\n\t\tbasic_machine=m68k-unknown\n\t\tos=-linux\n\t\t;;\n\tm68knommu-*)\n\t\tbasic_machine=m68k-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\tos=-linux\n\t\t;;\n\tm88k-omron*)\n\t\tbasic_machine=m88k-omron\n\t\t;;\n\tmagnum | m3230)\n\t\tbasic_machine=mips-mips\n\t\tos=-sysv\n\t\t;;\n\tmerlin)\n\t\tbasic_machine=ns32k-utek\n\t\tos=-sysv\n\t\t;;\n\tmicroblaze*)\n\t\tbasic_machine=microblaze-xilinx\n\t\t;;\n\tmingw64)\n\t\tbasic_machine=x86_64-pc\n\t\tos=-mingw64\n\t\t;;\n\tmingw32)\n\t\tbasic_machine=i686-pc\n\t\tos=-mingw32\n\t\t;;\n\tmingw32ce)\n\t\tbasic_machine=arm-unknown\n\t\tos=-mingw32ce\n\t\t;;\n\tminiframe)\n\t\tbasic_machine=m68000-convergent\n\t\t;;\n\t*mint | -mint[0-9]* | *MiNT | *MiNT[0-9]*)\n\t\tbasic_machine=m68k-atari\n\t\tos=-mint\n\t\t;;\n\tmips3*-*)\n\t\tbasic_machine=`echo $basic_machine | sed -e 's/mips3/mips64/'`\n\t\t;;\n\tmips3*)\n\t\tbasic_machine=`echo $basic_machine | sed -e 's/mips3/mips64/'`-unknown\n\t\t;;\n\tmonitor)\n\t\tbasic_machine=m68k-rom68k\n\t\tos=-coff\n\t\t;;\n\tmorphos)\n\t\tbasic_machine=powerpc-unknown\n\t\tos=-morphos\n\t\t;;\n\tmoxiebox)\n\t\tbasic_machine=moxie-unknown\n\t\tos=-moxiebox\n\t\t;;\n\tmsdos)\n\t\tbasic_machine=i386-pc\n\t\tos=-msdos\n\t\t;;\n\tms1-*)\n\t\tbasic_machine=`echo $basic_machine | sed -e 's/ms1-/mt-/'`\n\t\t;;\n\tmsys)\n\t\tbasic_machine=i686-pc\n\t\tos=-msys\n\t\t;;\n\tmvs)\n\t\tbasic_machine=i370-ibm\n\t\tos=-mvs\n\t\t;;\n\tnacl)\n\t\tbasic_machine=le32-unknown\n\t\tos=-nacl\n\t\t;;\n\tncr3000)\n\t\tbasic_machine=i486-ncr\n\t\tos=-sysv4\n\t\t;;\n\tnetbsd386)\n\t\tbasic_machine=i386-unknown\n\t\tos=-netbsd\n\t\t;;\n\tnetwinder)\n\t\tbasic_machine=armv4l-rebel\n\t\tos=-linux\n\t\t;;\n\tnews | news700 | news800 | news900)\n\t\tbasic_machine=m68k-sony\n\t\tos=-newsos\n\t\t;;\n\tnews1000)\n\t\tbasic_machine=m68030-sony\n\t\tos=-newsos\n\t\t;;\n\tnews-3600 | risc-news)\n\t\tbasic_machine=mips-sony\n\t\tos=-newsos\n\t\t;;\n\tnecv70)\n\t\tbasic_machine=v70-nec\n\t\tos=-sysv\n\t\t;;\n\tnext | m*-next )\n\t\tbasic_machine=m68k-next\n\t\tcase $os in\n\t\t    -nextstep* )\n\t\t\t;;\n\t\t    -ns2*)\n\t\t      os=-nextstep2\n\t\t\t;;\n\t\t    *)\n\t\t      os=-nextstep3\n\t\t\t;;\n\t\tesac\n\t\t;;\n\tnh3000)\n\t\tbasic_machine=m68k-harris\n\t\tos=-cxux\n\t\t;;\n\tnh[45]000)\n\t\tbasic_machine=m88k-harris\n\t\tos=-cxux\n\t\t;;\n\tnindy960)\n\t\tbasic_machine=i960-intel\n\t\tos=-nindy\n\t\t;;\n\tmon960)\n\t\tbasic_machine=i960-intel\n\t\tos=-mon960\n\t\t;;\n\tnonstopux)\n\t\tbasic_machine=mips-compaq\n\t\tos=-nonstopux\n\t\t;;\n\tnp1)\n\t\tbasic_machine=np1-gould\n\t\t;;\n\tneo-tandem)\n\t\tbasic_machine=neo-tandem\n\t\t;;\n\tnse-tandem)\n\t\tbasic_machine=nse-tandem\n\t\t;;\n\tnsr-tandem)\n\t\tbasic_machine=nsr-tandem\n\t\t;;\n\top50n-* | op60c-*)\n\t\tbasic_machine=hppa1.1-oki\n\t\tos=-proelf\n\t\t;;\n\topenrisc | openrisc-*)\n\t\tbasic_machine=or32-unknown\n\t\t;;\n\tos400)\n\t\tbasic_machine=powerpc-ibm\n\t\tos=-os400\n\t\t;;\n\tOSE68000 | ose68000)\n\t\tbasic_machine=m68000-ericsson\n\t\tos=-ose\n\t\t;;\n\tos68k)\n\t\tbasic_machine=m68k-none\n\t\tos=-os68k\n\t\t;;\n\tpa-hitachi)\n\t\tbasic_machine=hppa1.1-hitachi\n\t\tos=-hiuxwe2\n\t\t;;\n\tparagon)\n\t\tbasic_machine=i860-intel\n\t\tos=-osf\n\t\t;;\n\tparisc)\n\t\tbasic_machine=hppa-unknown\n\t\tos=-linux\n\t\t;;\n\tparisc-*)\n\t\tbasic_machine=hppa-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\tos=-linux\n\t\t;;\n\tpbd)\n\t\tbasic_machine=sparc-tti\n\t\t;;\n\tpbb)\n\t\tbasic_machine=m68k-tti\n\t\t;;\n\tpc532 | pc532-*)\n\t\tbasic_machine=ns32k-pc532\n\t\t;;\n\tpc98)\n\t\tbasic_machine=i386-pc\n\t\t;;\n\tpc98-*)\n\t\tbasic_machine=i386-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tpentium | p5 | k5 | k6 | nexgen | viac3)\n\t\tbasic_machine=i586-pc\n\t\t;;\n\tpentiumpro | p6 | 6x86 | athlon | athlon_*)\n\t\tbasic_machine=i686-pc\n\t\t;;\n\tpentiumii | pentium2 | pentiumiii | pentium3)\n\t\tbasic_machine=i686-pc\n\t\t;;\n\tpentium4)\n\t\tbasic_machine=i786-pc\n\t\t;;\n\tpentium-* | p5-* | k5-* | k6-* | nexgen-* | viac3-*)\n\t\tbasic_machine=i586-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tpentiumpro-* | p6-* | 6x86-* | athlon-*)\n\t\tbasic_machine=i686-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tpentiumii-* | pentium2-* | pentiumiii-* | pentium3-*)\n\t\tbasic_machine=i686-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tpentium4-*)\n\t\tbasic_machine=i786-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tpn)\n\t\tbasic_machine=pn-gould\n\t\t;;\n\tpower)\tbasic_machine=power-ibm\n\t\t;;\n\tppc | ppcbe)\tbasic_machine=powerpc-unknown\n\t\t;;\n\tppc-* | ppcbe-*)\n\t\tbasic_machine=powerpc-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tppcle | powerpclittle)\n\t\tbasic_machine=powerpcle-unknown\n\t\t;;\n\tppcle-* | powerpclittle-*)\n\t\tbasic_machine=powerpcle-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tppc64)\tbasic_machine=powerpc64-unknown\n\t\t;;\n\tppc64-*) basic_machine=powerpc64-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tppc64le | powerpc64little)\n\t\tbasic_machine=powerpc64le-unknown\n\t\t;;\n\tppc64le-* | powerpc64little-*)\n\t\tbasic_machine=powerpc64le-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tps2)\n\t\tbasic_machine=i386-ibm\n\t\t;;\n\tpw32)\n\t\tbasic_machine=i586-unknown\n\t\tos=-pw32\n\t\t;;\n\trdos | rdos64)\n\t\tbasic_machine=x86_64-pc\n\t\tos=-rdos\n\t\t;;\n\trdos32)\n\t\tbasic_machine=i386-pc\n\t\tos=-rdos\n\t\t;;\n\trom68k)\n\t\tbasic_machine=m68k-rom68k\n\t\tos=-coff\n\t\t;;\n\trm[46]00)\n\t\tbasic_machine=mips-siemens\n\t\t;;\n\trtpc | rtpc-*)\n\t\tbasic_machine=romp-ibm\n\t\t;;\n\ts390 | s390-*)\n\t\tbasic_machine=s390-ibm\n\t\t;;\n\ts390x | s390x-*)\n\t\tbasic_machine=s390x-ibm\n\t\t;;\n\tsa29200)\n\t\tbasic_machine=a29k-amd\n\t\tos=-udi\n\t\t;;\n\tsb1)\n\t\tbasic_machine=mipsisa64sb1-unknown\n\t\t;;\n\tsb1el)\n\t\tbasic_machine=mipsisa64sb1el-unknown\n\t\t;;\n\tsde)\n\t\tbasic_machine=mipsisa32-sde\n\t\tos=-elf\n\t\t;;\n\tsei)\n\t\tbasic_machine=mips-sei\n\t\tos=-seiux\n\t\t;;\n\tsequent)\n\t\tbasic_machine=i386-sequent\n\t\t;;\n\tsh)\n\t\tbasic_machine=sh-hitachi\n\t\tos=-hms\n\t\t;;\n\tsh5el)\n\t\tbasic_machine=sh5le-unknown\n\t\t;;\n\tsh64)\n\t\tbasic_machine=sh64-unknown\n\t\t;;\n\tsparclite-wrs | simso-wrs)\n\t\tbasic_machine=sparclite-wrs\n\t\tos=-vxworks\n\t\t;;\n\tsps7)\n\t\tbasic_machine=m68k-bull\n\t\tos=-sysv2\n\t\t;;\n\tspur)\n\t\tbasic_machine=spur-unknown\n\t\t;;\n\tst2000)\n\t\tbasic_machine=m68k-tandem\n\t\t;;\n\tstratus)\n\t\tbasic_machine=i860-stratus\n\t\tos=-sysv4\n\t\t;;\n\tstrongarm-* | thumb-*)\n\t\tbasic_machine=arm-`echo $basic_machine | sed 's/^[^-]*-//'`\n\t\t;;\n\tsun2)\n\t\tbasic_machine=m68000-sun\n\t\t;;\n\tsun2os3)\n\t\tbasic_machine=m68000-sun\n\t\tos=-sunos3\n\t\t;;\n\tsun2os4)\n\t\tbasic_machine=m68000-sun\n\t\tos=-sunos4\n\t\t;;\n\tsun3os3)\n\t\tbasic_machine=m68k-sun\n\t\tos=-sunos3\n\t\t;;\n\tsun3os4)\n\t\tbasic_machine=m68k-sun\n\t\tos=-sunos4\n\t\t;;\n\tsun4os3)\n\t\tbasic_machine=sparc-sun\n\t\tos=-sunos3\n\t\t;;\n\tsun4os4)\n\t\tbasic_machine=sparc-sun\n\t\tos=-sunos4\n\t\t;;\n\tsun4sol2)\n\t\tbasic_machine=sparc-sun\n\t\tos=-solaris2\n\t\t;;\n\tsun3 | sun3-*)\n\t\tbasic_machine=m68k-sun\n\t\t;;\n\tsun4)\n\t\tbasic_machine=sparc-sun\n\t\t;;\n\tsun386 | sun386i | roadrunner)\n\t\tbasic_machine=i386-sun\n\t\t;;\n\tsv1)\n\t\tbasic_machine=sv1-cray\n\t\tos=-unicos\n\t\t;;\n\tsymmetry)\n\t\tbasic_machine=i386-sequent\n\t\tos=-dynix\n\t\t;;\n\tt3e)\n\t\tbasic_machine=alphaev5-cray\n\t\tos=-unicos\n\t\t;;\n\tt90)\n\t\tbasic_machine=t90-cray\n\t\tos=-unicos\n\t\t;;\n\ttile*)\n\t\tbasic_machine=$basic_machine-unknown\n\t\tos=-linux-gnu\n\t\t;;\n\ttx39)\n\t\tbasic_machine=mipstx39-unknown\n\t\t;;\n\ttx39el)\n\t\tbasic_machine=mipstx39el-unknown\n\t\t;;\n\ttoad1)\n\t\tbasic_machine=pdp10-xkl\n\t\tos=-tops20\n\t\t;;\n\ttower | tower-32)\n\t\tbasic_machine=m68k-ncr\n\t\t;;\n\ttpf)\n\t\tbasic_machine=s390x-ibm\n\t\tos=-tpf\n\t\t;;\n\tudi29k)\n\t\tbasic_machine=a29k-amd\n\t\tos=-udi\n\t\t;;\n\tultra3)\n\t\tbasic_machine=a29k-nyu\n\t\tos=-sym1\n\t\t;;\n\tv810 | necv810)\n\t\tbasic_machine=v810-nec\n\t\tos=-none\n\t\t;;\n\tvaxv)\n\t\tbasic_machine=vax-dec\n\t\tos=-sysv\n\t\t;;\n\tvms)\n\t\tbasic_machine=vax-dec\n\t\tos=-vms\n\t\t;;\n\tvpp*|vx|vx-*)\n\t\tbasic_machine=f301-fujitsu\n\t\t;;\n\tvxworks960)\n\t\tbasic_machine=i960-wrs\n\t\tos=-vxworks\n\t\t;;\n\tvxworks68)\n\t\tbasic_machine=m68k-wrs\n\t\tos=-vxworks\n\t\t;;\n\tvxworks29k)\n\t\tbasic_machine=a29k-wrs\n\t\tos=-vxworks\n\t\t;;\n\tw65*)\n\t\tbasic_machine=w65-wdc\n\t\tos=-none\n\t\t;;\n\tw89k-*)\n\t\tbasic_machine=hppa1.1-winbond\n\t\tos=-proelf\n\t\t;;\n\txbox)\n\t\tbasic_machine=i686-pc\n\t\tos=-mingw32\n\t\t;;\n\txps | xps100)\n\t\tbasic_machine=xps100-honeywell\n\t\t;;\n\txscale-* | xscalee[bl]-*)\n\t\tbasic_machine=`echo $basic_machine | sed 's/^xscale/arm/'`\n\t\t;;\n\tymp)\n\t\tbasic_machine=ymp-cray\n\t\tos=-unicos\n\t\t;;\n\tz8k-*-coff)\n\t\tbasic_machine=z8k-unknown\n\t\tos=-sim\n\t\t;;\n\tz80-*-coff)\n\t\tbasic_machine=z80-unknown\n\t\tos=-sim\n\t\t;;\n\tnone)\n\t\tbasic_machine=none-none\n\t\tos=-none\n\t\t;;\n\n# Here we handle the default manufacturer of certain CPU types.  It is in\n# some cases the only manufacturer, in others, it is the most popular.\n\tw89k)\n\t\tbasic_machine=hppa1.1-winbond\n\t\t;;\n\top50n)\n\t\tbasic_machine=hppa1.1-oki\n\t\t;;\n\top60c)\n\t\tbasic_machine=hppa1.1-oki\n\t\t;;\n\tromp)\n\t\tbasic_machine=romp-ibm\n\t\t;;\n\tmmix)\n\t\tbasic_machine=mmix-knuth\n\t\t;;\n\trs6000)\n\t\tbasic_machine=rs6000-ibm\n\t\t;;\n\tvax)\n\t\tbasic_machine=vax-dec\n\t\t;;\n\tpdp10)\n\t\t# there are many clones, so DEC is not a safe bet\n\t\tbasic_machine=pdp10-unknown\n\t\t;;\n\tpdp11)\n\t\tbasic_machine=pdp11-dec\n\t\t;;\n\twe32k)\n\t\tbasic_machine=we32k-att\n\t\t;;\n\tsh[1234] | sh[24]a | sh[24]aeb | sh[34]eb | sh[1234]le | sh[23]ele)\n\t\tbasic_machine=sh-unknown\n\t\t;;\n\tsparc | sparcv8 | sparcv9 | sparcv9b | sparcv9v)\n\t\tbasic_machine=sparc-sun\n\t\t;;\n\tcydra)\n\t\tbasic_machine=cydra-cydrome\n\t\t;;\n\torion)\n\t\tbasic_machine=orion-highlevel\n\t\t;;\n\torion105)\n\t\tbasic_machine=clipper-highlevel\n\t\t;;\n\tmac | mpw | mac-mpw)\n\t\tbasic_machine=m68k-apple\n\t\t;;\n\tpmac | pmac-mpw)\n\t\tbasic_machine=powerpc-apple\n\t\t;;\n\t*-unknown)\n\t\t# Make sure to match an already-canonicalized machine name.\n\t\t;;\n\t*)\n\t\techo Invalid configuration \\`$1\\': machine \\`$basic_machine\\' not recognized 1>&2\n\t\texit 1\n\t\t;;\nesac\n\n# Here we canonicalize certain aliases for manufacturers.\ncase $basic_machine in\n\t*-digital*)\n\t\tbasic_machine=`echo $basic_machine | sed 's/digital.*/dec/'`\n\t\t;;\n\t*-commodore*)\n\t\tbasic_machine=`echo $basic_machine | sed 's/commodore.*/cbm/'`\n\t\t;;\n\t*)\n\t\t;;\nesac\n\n# Decode manufacturer-specific aliases for certain operating systems.\n\nif [ x\"$os\" != x\"\" ]\nthen\ncase $os in\n\t# First match some system type aliases\n\t# that might get confused with valid system types.\n\t# -solaris* is a basic system type, with this one exception.\n\t-auroraux)\n\t\tos=-auroraux\n\t\t;;\n\t-solaris1 | -solaris1.*)\n\t\tos=`echo $os | sed -e 's|solaris1|sunos4|'`\n\t\t;;\n\t-solaris)\n\t\tos=-solaris2\n\t\t;;\n\t-svr4*)\n\t\tos=-sysv4\n\t\t;;\n\t-unixware*)\n\t\tos=-sysv4.2uw\n\t\t;;\n\t-gnu/linux*)\n\t\tos=`echo $os | sed -e 's|gnu/linux|linux-gnu|'`\n\t\t;;\n\t# First accept the basic system types.\n\t# The portable systems comes first.\n\t# Each alternative MUST END IN A *, to match a version number.\n\t# -sysv* is not here because it comes later, after sysvr4.\n\t-gnu* | -bsd* | -mach* | -minix* | -genix* | -ultrix* | -irix* \\\n\t      | -*vms* | -sco* | -esix* | -isc* | -aix* | -cnk* | -sunos | -sunos[34]*\\\n\t      | -hpux* | -unos* | -osf* | -luna* | -dgux* | -auroraux* | -solaris* \\\n\t      | -sym* | -kopensolaris* | -plan9* \\\n\t      | -amigaos* | -amigados* | -msdos* | -newsos* | -unicos* | -aof* \\\n\t      | -aos* | -aros* | -cloudabi* | -sortix* \\\n\t      | -nindy* | -vxsim* | -vxworks* | -ebmon* | -hms* | -mvs* \\\n\t      | -clix* | -riscos* | -uniplus* | -iris* | -rtu* | -xenix* \\\n\t      | -hiux* | -386bsd* | -knetbsd* | -mirbsd* | -netbsd* \\\n\t      | -bitrig* | -openbsd* | -solidbsd* | -libertybsd* \\\n\t      | -ekkobsd* | -kfreebsd* | -freebsd* | -riscix* | -lynxos* \\\n\t      | -bosx* | -nextstep* | -cxux* | -aout* | -elf* | -oabi* \\\n\t      | -ptx* | -coff* | -ecoff* | -winnt* | -domain* | -vsta* \\\n\t      | -udi* | -eabi* | -lites* | -ieee* | -go32* | -aux* \\\n\t      | -chorusos* | -chorusrdb* | -cegcc* \\\n\t      | -cygwin* | -msys* | -pe* | -psos* | -moss* | -proelf* | -rtems* \\\n\t      | -midipix* | -mingw32* | -mingw64* | -linux-gnu* | -linux-android* \\\n\t      | -linux-newlib* | -linux-musl* | -linux-uclibc* \\\n\t      | -uxpv* | -beos* | -mpeix* | -udk* | -moxiebox* \\\n\t      | -interix* | -uwin* | -mks* | -rhapsody* | -darwin* | -opened* \\\n\t      | -openstep* | -oskit* | -conix* | -pw32* | -nonstopux* \\\n\t      | -storm-chaos* | -tops10* | -tenex* | -tops20* | -its* \\\n\t      | -os2* | -vos* | -palmos* | -uclinux* | -nucleus* \\\n\t      | -morphos* | -superux* | -rtmk* | -rtmk-nova* | -windiss* \\\n\t      | -powermax* | -dnix* | -nx6 | -nx7 | -sei* | -dragonfly* \\\n\t      | -skyos* | -haiku* | -rdos* | -toppers* | -drops* | -es* \\\n\t      | -onefs* | -tirtos* | -phoenix* | -fuchsia*)\n\t# Remember, each alternative MUST END IN *, to match a version number.\n\t\t;;\n\t-qnx*)\n\t\tcase $basic_machine in\n\t\t    x86-* | i*86-*)\n\t\t\t;;\n\t\t    *)\n\t\t\tos=-nto$os\n\t\t\t;;\n\t\tesac\n\t\t;;\n\t-nto-qnx*)\n\t\t;;\n\t-nto*)\n\t\tos=`echo $os | sed -e 's|nto|nto-qnx|'`\n\t\t;;\n\t-sim | -es1800* | -hms* | -xray | -os68k* | -none* | -v88r* \\\n\t      | -windows* | -osx | -abug | -netware* | -os9* | -beos* | -haiku* \\\n\t      | -macos* | -mpw* | -magic* | -mmixware* | -mon960* | -lnews*)\n\t\t;;\n\t-mac*)\n\t\tos=`echo $os | sed -e 's|mac|macos|'`\n\t\t;;\n\t-linux-dietlibc)\n\t\tos=-linux-dietlibc\n\t\t;;\n\t-linux*)\n\t\tos=`echo $os | sed -e 's|linux|linux-gnu|'`\n\t\t;;\n\t-sunos5*)\n\t\tos=`echo $os | sed -e 's|sunos5|solaris2|'`\n\t\t;;\n\t-sunos6*)\n\t\tos=`echo $os | sed -e 's|sunos6|solaris3|'`\n\t\t;;\n\t-opened*)\n\t\tos=-openedition\n\t\t;;\n\t-os400*)\n\t\tos=-os400\n\t\t;;\n\t-wince*)\n\t\tos=-wince\n\t\t;;\n\t-osfrose*)\n\t\tos=-osfrose\n\t\t;;\n\t-osf*)\n\t\tos=-osf\n\t\t;;\n\t-utek*)\n\t\tos=-bsd\n\t\t;;\n\t-dynix*)\n\t\tos=-bsd\n\t\t;;\n\t-acis*)\n\t\tos=-aos\n\t\t;;\n\t-atheos*)\n\t\tos=-atheos\n\t\t;;\n\t-syllable*)\n\t\tos=-syllable\n\t\t;;\n\t-386bsd)\n\t\tos=-bsd\n\t\t;;\n\t-ctix* | -uts*)\n\t\tos=-sysv\n\t\t;;\n\t-nova*)\n\t\tos=-rtmk-nova\n\t\t;;\n\t-ns2 )\n\t\tos=-nextstep2\n\t\t;;\n\t-nsk*)\n\t\tos=-nsk\n\t\t;;\n\t# Preserve the version number of sinix5.\n\t-sinix5.*)\n\t\tos=`echo $os | sed -e 's|sinix|sysv|'`\n\t\t;;\n\t-sinix*)\n\t\tos=-sysv4\n\t\t;;\n\t-tpf*)\n\t\tos=-tpf\n\t\t;;\n\t-triton*)\n\t\tos=-sysv3\n\t\t;;\n\t-oss*)\n\t\tos=-sysv3\n\t\t;;\n\t-svr4)\n\t\tos=-sysv4\n\t\t;;\n\t-svr3)\n\t\tos=-sysv3\n\t\t;;\n\t-sysvr4)\n\t\tos=-sysv4\n\t\t;;\n\t# This must come after -sysvr4.\n\t-sysv*)\n\t\t;;\n\t-ose*)\n\t\tos=-ose\n\t\t;;\n\t-es1800*)\n\t\tos=-ose\n\t\t;;\n\t-xenix)\n\t\tos=-xenix\n\t\t;;\n\t-*mint | -mint[0-9]* | -*MiNT | -MiNT[0-9]*)\n\t\tos=-mint\n\t\t;;\n\t-aros*)\n\t\tos=-aros\n\t\t;;\n\t-zvmoe)\n\t\tos=-zvmoe\n\t\t;;\n\t-dicos*)\n\t\tos=-dicos\n\t\t;;\n\t-nacl*)\n\t\t;;\n\t-ios)\n\t\t;;\n\t-none)\n\t\t;;\n\t*)\n\t\t# Get rid of the `-' at the beginning of $os.\n\t\tos=`echo $os | sed 's/[^-]*-//'`\n\t\techo Invalid configuration \\`$1\\': system \\`$os\\' not recognized 1>&2\n\t\texit 1\n\t\t;;\nesac\nelse\n\n# Here we handle the default operating systems that come with various machines.\n# The value should be what the vendor currently ships out the door with their\n# machine or put another way, the most popular os provided with the machine.\n\n# Note that if you're going to try to match \"-MANUFACTURER\" here (say,\n# \"-sun\"), then you have to tell the case statement up towards the top\n# that MANUFACTURER isn't an operating system.  Otherwise, code above\n# will signal an error saying that MANUFACTURER isn't an operating\n# system, and we'll never get to this point.\n\ncase $basic_machine in\n\tscore-*)\n\t\tos=-elf\n\t\t;;\n\tspu-*)\n\t\tos=-elf\n\t\t;;\n\t*-acorn)\n\t\tos=-riscix1.2\n\t\t;;\n\tarm*-rebel)\n\t\tos=-linux\n\t\t;;\n\tarm*-semi)\n\t\tos=-aout\n\t\t;;\n\tc4x-* | tic4x-*)\n\t\tos=-coff\n\t\t;;\n\tc8051-*)\n\t\tos=-elf\n\t\t;;\n\thexagon-*)\n\t\tos=-elf\n\t\t;;\n\ttic54x-*)\n\t\tos=-coff\n\t\t;;\n\ttic55x-*)\n\t\tos=-coff\n\t\t;;\n\ttic6x-*)\n\t\tos=-coff\n\t\t;;\n\t# This must come before the *-dec entry.\n\tpdp10-*)\n\t\tos=-tops20\n\t\t;;\n\tpdp11-*)\n\t\tos=-none\n\t\t;;\n\t*-dec | vax-*)\n\t\tos=-ultrix4.2\n\t\t;;\n\tm68*-apollo)\n\t\tos=-domain\n\t\t;;\n\ti386-sun)\n\t\tos=-sunos4.0.2\n\t\t;;\n\tm68000-sun)\n\t\tos=-sunos3\n\t\t;;\n\tm68*-cisco)\n\t\tos=-aout\n\t\t;;\n\tmep-*)\n\t\tos=-elf\n\t\t;;\n\tmips*-cisco)\n\t\tos=-elf\n\t\t;;\n\tmips*-*)\n\t\tos=-elf\n\t\t;;\n\tor32-*)\n\t\tos=-coff\n\t\t;;\n\t*-tti)\t# must be before sparc entry or we get the wrong os.\n\t\tos=-sysv3\n\t\t;;\n\tsparc-* | *-sun)\n\t\tos=-sunos4.1.1\n\t\t;;\n\t*-be)\n\t\tos=-beos\n\t\t;;\n\t*-haiku)\n\t\tos=-haiku\n\t\t;;\n\t*-ibm)\n\t\tos=-aix\n\t\t;;\n\t*-knuth)\n\t\tos=-mmixware\n\t\t;;\n\t*-wec)\n\t\tos=-proelf\n\t\t;;\n\t*-winbond)\n\t\tos=-proelf\n\t\t;;\n\t*-oki)\n\t\tos=-proelf\n\t\t;;\n\t*-hp)\n\t\tos=-hpux\n\t\t;;\n\t*-hitachi)\n\t\tos=-hiux\n\t\t;;\n\ti860-* | *-att | *-ncr | *-altos | *-motorola | *-convergent)\n\t\tos=-sysv\n\t\t;;\n\t*-cbm)\n\t\tos=-amigaos\n\t\t;;\n\t*-dg)\n\t\tos=-dgux\n\t\t;;\n\t*-dolphin)\n\t\tos=-sysv3\n\t\t;;\n\tm68k-ccur)\n\t\tos=-rtu\n\t\t;;\n\tm88k-omron*)\n\t\tos=-luna\n\t\t;;\n\t*-next )\n\t\tos=-nextstep\n\t\t;;\n\t*-sequent)\n\t\tos=-ptx\n\t\t;;\n\t*-crds)\n\t\tos=-unos\n\t\t;;\n\t*-ns)\n\t\tos=-genix\n\t\t;;\n\ti370-*)\n\t\tos=-mvs\n\t\t;;\n\t*-next)\n\t\tos=-nextstep3\n\t\t;;\n\t*-gould)\n\t\tos=-sysv\n\t\t;;\n\t*-highlevel)\n\t\tos=-bsd\n\t\t;;\n\t*-encore)\n\t\tos=-bsd\n\t\t;;\n\t*-sgi)\n\t\tos=-irix\n\t\t;;\n\t*-siemens)\n\t\tos=-sysv4\n\t\t;;\n\t*-masscomp)\n\t\tos=-rtu\n\t\t;;\n\tf30[01]-fujitsu | f700-fujitsu)\n\t\tos=-uxpv\n\t\t;;\n\t*-rom68k)\n\t\tos=-coff\n\t\t;;\n\t*-*bug)\n\t\tos=-coff\n\t\t;;\n\t*-apple)\n\t\tos=-macos\n\t\t;;\n\t*-atari*)\n\t\tos=-mint\n\t\t;;\n\t*)\n\t\tos=-none\n\t\t;;\nesac\nfi\n\n# Here we handle the case where we know the os, and the CPU type, but not the\n# manufacturer.  We pick the logical manufacturer.\nvendor=unknown\ncase $basic_machine in\n\t*-unknown)\n\t\tcase $os in\n\t\t\t-riscix*)\n\t\t\t\tvendor=acorn\n\t\t\t\t;;\n\t\t\t-sunos*)\n\t\t\t\tvendor=sun\n\t\t\t\t;;\n\t\t\t-cnk*|-aix*)\n\t\t\t\tvendor=ibm\n\t\t\t\t;;\n\t\t\t-beos*)\n\t\t\t\tvendor=be\n\t\t\t\t;;\n\t\t\t-hpux*)\n\t\t\t\tvendor=hp\n\t\t\t\t;;\n\t\t\t-mpeix*)\n\t\t\t\tvendor=hp\n\t\t\t\t;;\n\t\t\t-hiux*)\n\t\t\t\tvendor=hitachi\n\t\t\t\t;;\n\t\t\t-unos*)\n\t\t\t\tvendor=crds\n\t\t\t\t;;\n\t\t\t-dgux*)\n\t\t\t\tvendor=dg\n\t\t\t\t;;\n\t\t\t-luna*)\n\t\t\t\tvendor=omron\n\t\t\t\t;;\n\t\t\t-genix*)\n\t\t\t\tvendor=ns\n\t\t\t\t;;\n\t\t\t-mvs* | -opened*)\n\t\t\t\tvendor=ibm\n\t\t\t\t;;\n\t\t\t-os400*)\n\t\t\t\tvendor=ibm\n\t\t\t\t;;\n\t\t\t-ptx*)\n\t\t\t\tvendor=sequent\n\t\t\t\t;;\n\t\t\t-tpf*)\n\t\t\t\tvendor=ibm\n\t\t\t\t;;\n\t\t\t-vxsim* | -vxworks* | -windiss*)\n\t\t\t\tvendor=wrs\n\t\t\t\t;;\n\t\t\t-aux*)\n\t\t\t\tvendor=apple\n\t\t\t\t;;\n\t\t\t-hms*)\n\t\t\t\tvendor=hitachi\n\t\t\t\t;;\n\t\t\t-mpw* | -macos*)\n\t\t\t\tvendor=apple\n\t\t\t\t;;\n\t\t\t-*mint | -mint[0-9]* | -*MiNT | -MiNT[0-9]*)\n\t\t\t\tvendor=atari\n\t\t\t\t;;\n\t\t\t-vos*)\n\t\t\t\tvendor=stratus\n\t\t\t\t;;\n\t\tesac\n\t\tbasic_machine=`echo $basic_machine | sed \"s/unknown/$vendor/\"`\n\t\t;;\nesac\n\necho $basic_machine$os\nexit\n\n# Local variables:\n# eval: (add-hook 'write-file-hooks 'time-stamp)\n# time-stamp-start: \"timestamp='\"\n# time-stamp-format: \"%:y-%02m-%02d\"\n# time-stamp-end: \"'\"\n# End:\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/build-aux/install-sh",
    "content": "#! /bin/sh\n#\n# install - install a program, script, or datafile\n# This comes from X11R5 (mit/util/scripts/install.sh).\n#\n# Copyright 1991 by the Massachusetts Institute of Technology\n#\n# Permission to use, copy, modify, distribute, and sell this software and its\n# documentation for any purpose is hereby granted without fee, provided that\n# the above copyright notice appear in all copies and that both that\n# copyright notice and this permission notice appear in supporting\n# documentation, and that the name of M.I.T. not be used in advertising or\n# publicity pertaining to distribution of the software without specific,\n# written prior permission.  M.I.T. makes no representations about the\n# suitability of this software for any purpose.  It is provided \"as is\"\n# without express or implied warranty.\n#\n# Calling this script install-sh is preferred over install.sh, to prevent\n# `make' implicit rules from creating a file called install from it\n# when there is no Makefile.\n#\n# This script is compatible with the BSD install script, but was written\n# from scratch.  It can only install one file at a time, a restriction\n# shared with many OS's install programs.\n\n\n# set DOITPROG to echo to test this script\n\n# Don't use :- since 4.3BSD and earlier shells don't like it.\ndoit=\"${DOITPROG-}\"\n\n\n# put in absolute paths if you don't have them in your path; or use env. vars.\n\nmvprog=\"${MVPROG-mv}\"\ncpprog=\"${CPPROG-cp}\"\nchmodprog=\"${CHMODPROG-chmod}\"\nchownprog=\"${CHOWNPROG-chown}\"\nchgrpprog=\"${CHGRPPROG-chgrp}\"\nstripprog=\"${STRIPPROG-strip}\"\nrmprog=\"${RMPROG-rm}\"\nmkdirprog=\"${MKDIRPROG-mkdir}\"\n\ntransformbasename=\"\"\ntransform_arg=\"\"\ninstcmd=\"$mvprog\"\nchmodcmd=\"$chmodprog 0755\"\nchowncmd=\"\"\nchgrpcmd=\"\"\nstripcmd=\"\"\nrmcmd=\"$rmprog -f\"\nmvcmd=\"$mvprog\"\nsrc=\"\"\ndst=\"\"\ndir_arg=\"\"\n\nwhile [ x\"$1\" != x ]; do\n    case $1 in\n\t-c) instcmd=\"$cpprog\"\n\t    shift\n\t    continue;;\n\n\t-d) dir_arg=true\n\t    shift\n\t    continue;;\n\n\t-m) chmodcmd=\"$chmodprog $2\"\n\t    shift\n\t    shift\n\t    continue;;\n\n\t-o) chowncmd=\"$chownprog $2\"\n\t    shift\n\t    shift\n\t    continue;;\n\n\t-g) chgrpcmd=\"$chgrpprog $2\"\n\t    shift\n\t    shift\n\t    continue;;\n\n\t-s) stripcmd=\"$stripprog\"\n\t    shift\n\t    continue;;\n\n\t-t=*) transformarg=`echo $1 | sed 's/-t=//'`\n\t    shift\n\t    continue;;\n\n\t-b=*) transformbasename=`echo $1 | sed 's/-b=//'`\n\t    shift\n\t    continue;;\n\n\t*)  if [ x\"$src\" = x ]\n\t    then\n\t\tsrc=$1\n\t    else\n\t\t# this colon is to work around a 386BSD /bin/sh bug\n\t\t:\n\t\tdst=$1\n\t    fi\n\t    shift\n\t    continue;;\n    esac\ndone\n\nif [ x\"$src\" = x ]\nthen\n\techo \"install:\tno input file specified\"\n\texit 1\nelse\n\ttrue\nfi\n\nif [ x\"$dir_arg\" != x ]; then\n\tdst=$src\n\tsrc=\"\"\n\t\n\tif [ -d $dst ]; then\n\t\tinstcmd=:\n\telse\n\t\tinstcmd=mkdir\n\tfi\nelse\n\n# Waiting for this to be detected by the \"$instcmd $src $dsttmp\" command\n# might cause directories to be created, which would be especially bad \n# if $src (and thus $dsttmp) contains '*'.\n\n\tif [ -f $src -o -d $src ]\n\tthen\n\t\ttrue\n\telse\n\t\techo \"install:  $src does not exist\"\n\t\texit 1\n\tfi\n\t\n\tif [ x\"$dst\" = x ]\n\tthen\n\t\techo \"install:\tno destination specified\"\n\t\texit 1\n\telse\n\t\ttrue\n\tfi\n\n# If destination is a directory, append the input filename; if your system\n# does not like double slashes in filenames, you may need to add some logic\n\n\tif [ -d $dst ]\n\tthen\n\t\tdst=\"$dst\"/`basename $src`\n\telse\n\t\ttrue\n\tfi\nfi\n\n## this sed command emulates the dirname command\ndstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`\n\n# Make sure that the destination directory exists.\n#  this part is taken from Noah Friedman's mkinstalldirs script\n\n# Skip lots of stat calls in the usual case.\nif [ ! -d \"$dstdir\" ]; then\ndefaultIFS='\t\n'\nIFS=\"${IFS-${defaultIFS}}\"\n\noIFS=\"${IFS}\"\n# Some sh's can't handle IFS=/ for some reason.\nIFS='%'\nset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`\nIFS=\"${oIFS}\"\n\npathcomp=''\n\nwhile [ $# -ne 0 ] ; do\n\tpathcomp=\"${pathcomp}${1}\"\n\tshift\n\n\tif [ ! -d \"${pathcomp}\" ] ;\n        then\n\t\t$mkdirprog \"${pathcomp}\"\n\telse\n\t\ttrue\n\tfi\n\n\tpathcomp=\"${pathcomp}/\"\ndone\nfi\n\nif [ x\"$dir_arg\" != x ]\nthen\n\t$doit $instcmd $dst &&\n\n\tif [ x\"$chowncmd\" != x ]; then $doit $chowncmd $dst; else true ; fi &&\n\tif [ x\"$chgrpcmd\" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&\n\tif [ x\"$stripcmd\" != x ]; then $doit $stripcmd $dst; else true ; fi &&\n\tif [ x\"$chmodcmd\" != x ]; then $doit $chmodcmd $dst; else true ; fi\nelse\n\n# If we're going to rename the final executable, determine the name now.\n\n\tif [ x\"$transformarg\" = x ] \n\tthen\n\t\tdstfile=`basename $dst`\n\telse\n\t\tdstfile=`basename $dst $transformbasename | \n\t\t\tsed $transformarg`$transformbasename\n\tfi\n\n# don't allow the sed command to completely eliminate the filename\n\n\tif [ x\"$dstfile\" = x ] \n\tthen\n\t\tdstfile=`basename $dst`\n\telse\n\t\ttrue\n\tfi\n\n# Make a temp file name in the proper directory.\n\n\tdsttmp=$dstdir/#inst.$$#\n\n# Move or copy the file name to the temp name\n\n\t$doit $instcmd $src $dsttmp &&\n\n\ttrap \"rm -f ${dsttmp}\" 0 &&\n\n# and set any options; do chmod last to preserve setuid bits\n\n# If any of these fail, we abort the whole thing.  If we want to\n# ignore errors from any of these, just make sure not to ignore\n# errors from the above \"$doit $instcmd $src $dsttmp\" command.\n\n\tif [ x\"$chowncmd\" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&\n\tif [ x\"$chgrpcmd\" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&\n\tif [ x\"$stripcmd\" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&\n\tif [ x\"$chmodcmd\" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&\n\n# Now rename the file to the real destination.\n\n\t$doit $rmcmd -f $dstdir/$dstfile &&\n\t$doit $mvcmd $dsttmp $dstdir/$dstfile \n\nfi &&\n\n\nexit 0\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/config.stamp.in",
    "content": ""
  },
  {
    "path": "deps/memkind/src/jemalloc/configure.ac",
    "content": "dnl Process this file with autoconf to produce a configure script.\nAC_INIT([Makefile.in])\n\nAC_CONFIG_AUX_DIR([build-aux])\n\ndnl ============================================================================\ndnl Custom macro definitions.\n\ndnl JE_CONCAT_VVV(r, a, b)\ndnl \ndnl Set $r to the concatenation of $a and $b, with a space separating them iff\ndnl both $a and $b are non-emty.\nAC_DEFUN([JE_CONCAT_VVV],\nif test \"x[$]{$2}\" = \"x\" -o \"x[$]{$3}\" = \"x\" ; then\n  $1=\"[$]{$2}[$]{$3}\"\nelse\n  $1=\"[$]{$2} [$]{$3}\"\nfi\n)\n\ndnl JE_APPEND_VS(a, b)\ndnl \ndnl Set $a to the concatenation of $a and b, with a space separating them iff\ndnl both $a and b are non-empty.\nAC_DEFUN([JE_APPEND_VS],\n  T_APPEND_V=$2\n  JE_CONCAT_VVV($1, $1, T_APPEND_V)\n)\n\nCONFIGURE_CFLAGS=\nSPECIFIED_CFLAGS=\"${CFLAGS}\"\ndnl JE_CFLAGS_ADD(cflag)\ndnl \ndnl CFLAGS is the concatenation of CONFIGURE_CFLAGS and SPECIFIED_CFLAGS\ndnl (ignoring EXTRA_CFLAGS, which does not impact configure tests.  This macro\ndnl appends to CONFIGURE_CFLAGS and regenerates CFLAGS.\nAC_DEFUN([JE_CFLAGS_ADD],\n[\nAC_MSG_CHECKING([whether compiler supports $1])\nT_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\nJE_APPEND_VS(CONFIGURE_CFLAGS, $1)\nJE_CONCAT_VVV(CFLAGS, CONFIGURE_CFLAGS, SPECIFIED_CFLAGS)\nAC_COMPILE_IFELSE([AC_LANG_PROGRAM(\n[[\n]], [[\n    return 0;\n]])],\n              [je_cv_cflags_added=$1]\n              AC_MSG_RESULT([yes]),\n              [je_cv_cflags_added=]\n              AC_MSG_RESULT([no])\n              [CONFIGURE_CFLAGS=\"${T_CONFIGURE_CFLAGS}\"]\n)\nJE_CONCAT_VVV(CFLAGS, CONFIGURE_CFLAGS, SPECIFIED_CFLAGS)\n])\n\ndnl JE_CFLAGS_SAVE()\ndnl JE_CFLAGS_RESTORE()\ndnl \ndnl Save/restore CFLAGS.  Nesting is not supported.\nAC_DEFUN([JE_CFLAGS_SAVE],\nSAVED_CONFIGURE_CFLAGS=\"${CONFIGURE_CFLAGS}\"\n)\nAC_DEFUN([JE_CFLAGS_RESTORE],\nCONFIGURE_CFLAGS=\"${SAVED_CONFIGURE_CFLAGS}\"\nJE_CONCAT_VVV(CFLAGS, CONFIGURE_CFLAGS, SPECIFIED_CFLAGS)\n)\n\nCONFIGURE_CXXFLAGS=\nSPECIFIED_CXXFLAGS=\"${CXXFLAGS}\"\ndnl JE_CXXFLAGS_ADD(cxxflag)\nAC_DEFUN([JE_CXXFLAGS_ADD],\n[\nAC_MSG_CHECKING([whether compiler supports $1])\nT_CONFIGURE_CXXFLAGS=\"${CONFIGURE_CXXFLAGS}\"\nJE_APPEND_VS(CONFIGURE_CXXFLAGS, $1)\nJE_CONCAT_VVV(CXXFLAGS, CONFIGURE_CXXFLAGS, SPECIFIED_CXXFLAGS)\nAC_COMPILE_IFELSE([AC_LANG_PROGRAM(\n[[\n]], [[\n    return 0;\n]])],\n              [je_cv_cxxflags_added=$1]\n              AC_MSG_RESULT([yes]),\n              [je_cv_cxxflags_added=]\n              AC_MSG_RESULT([no])\n              [CONFIGURE_CXXFLAGS=\"${T_CONFIGURE_CXXFLAGS}\"]\n)\nJE_CONCAT_VVV(CXXFLAGS, CONFIGURE_CXXFLAGS, SPECIFIED_CXXFLAGS)\n])\n\ndnl JE_COMPILABLE(label, hcode, mcode, rvar)\ndnl \ndnl Use AC_LINK_IFELSE() rather than AC_COMPILE_IFELSE() so that linker errors\ndnl cause failure.\nAC_DEFUN([JE_COMPILABLE],\n[\nAC_CACHE_CHECK([whether $1 is compilable],\n               [$4],\n               [AC_LINK_IFELSE([AC_LANG_PROGRAM([$2],\n                                                [$3])],\n                               [$4=yes],\n                               [$4=no])])\n])\n\ndnl ============================================================================\n\nCONFIG=`echo ${ac_configure_args} | sed -e 's#'\"'\"'\\([^ ]*\\)'\"'\"'#\\1#g'`\nAC_SUBST([CONFIG])\n\ndnl Library revision.\nrev=2\nAC_SUBST([rev])\n\nsrcroot=$srcdir\nif test \"x${srcroot}\" = \"x.\" ; then\n  srcroot=\"\"\nelse\n  srcroot=\"${srcroot}/\"\nfi\nAC_SUBST([srcroot])\nabs_srcroot=\"`cd \\\"${srcdir}\\\"; pwd`/\"\nAC_SUBST([abs_srcroot])\n\nobjroot=\"\"\nAC_SUBST([objroot])\nabs_objroot=\"`pwd`/\"\nAC_SUBST([abs_objroot])\n\ndnl Munge install path variables.\nif test \"x$prefix\" = \"xNONE\" ; then\n  prefix=\"/usr/local\"\nfi\nif test \"x$exec_prefix\" = \"xNONE\" ; then\n  exec_prefix=$prefix\nfi\nPREFIX=$prefix\nAC_SUBST([PREFIX])\nBINDIR=`eval echo $bindir`\nBINDIR=`eval echo $BINDIR`\nAC_SUBST([BINDIR])\nINCLUDEDIR=`eval echo $includedir`\nINCLUDEDIR=`eval echo $INCLUDEDIR`\nAC_SUBST([INCLUDEDIR])\nLIBDIR=`eval echo $libdir`\nLIBDIR=`eval echo $LIBDIR`\nAC_SUBST([LIBDIR])\nDATADIR=`eval echo $datadir`\nDATADIR=`eval echo $DATADIR`\nAC_SUBST([DATADIR])\nMANDIR=`eval echo $mandir`\nMANDIR=`eval echo $MANDIR`\nAC_SUBST([MANDIR])\n\ndnl Support for building documentation.\nAC_PATH_PROG([XSLTPROC], [xsltproc], [false], [$PATH])\nif test -d \"/usr/share/xml/docbook/stylesheet/docbook-xsl\" ; then\n  DEFAULT_XSLROOT=\"/usr/share/xml/docbook/stylesheet/docbook-xsl\"\nelif test -d \"/usr/share/sgml/docbook/xsl-stylesheets\" ; then\n  DEFAULT_XSLROOT=\"/usr/share/sgml/docbook/xsl-stylesheets\"\nelse\n  dnl Documentation building will fail if this default gets used.\n  DEFAULT_XSLROOT=\"\"\nfi\nAC_ARG_WITH([xslroot],\n  [AS_HELP_STRING([--with-xslroot=<path>], [XSL stylesheet root path])], [\nif test \"x$with_xslroot\" = \"xno\" ; then\n  XSLROOT=\"${DEFAULT_XSLROOT}\"\nelse\n  XSLROOT=\"${with_xslroot}\"\nfi\n],\n  XSLROOT=\"${DEFAULT_XSLROOT}\"\n)\nAC_SUBST([XSLROOT])\n\ndnl If CFLAGS isn't defined, set CFLAGS to something reasonable.  Otherwise,\ndnl just prevent autoconf from molesting CFLAGS.\nCFLAGS=$CFLAGS\nAC_PROG_CC\n\nif test \"x$GCC\" != \"xyes\" ; then\n  AC_CACHE_CHECK([whether compiler is MSVC],\n                 [je_cv_msvc],\n                 [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],\n                                                     [\n#ifndef _MSC_VER\n  int fail[-1];\n#endif\n])],\n                               [je_cv_msvc=yes],\n                               [je_cv_msvc=no])])\nfi\n\ndnl check if a cray prgenv wrapper compiler is being used\nje_cv_cray_prgenv_wrapper=\"\"\nif test \"x${PE_ENV}\" != \"x\" ; then\n  case \"${CC}\" in\n    CC|cc)\n\tje_cv_cray_prgenv_wrapper=\"yes\"\n\t;;\n    *)\n       ;;\n  esac\nfi\n\nAC_CACHE_CHECK([whether compiler is cray],\n              [je_cv_cray],\n              [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],\n                                                  [\n#ifndef _CRAYC\n  int fail[-1];\n#endif\n])],\n                            [je_cv_cray=yes],\n                            [je_cv_cray=no])])\n\nif test \"x${je_cv_cray}\" = \"xyes\" ; then\n  AC_CACHE_CHECK([whether cray compiler version is 8.4],\n                [je_cv_cray_84],\n                [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],\n                                                      [\n#if !(_RELEASE_MAJOR == 8 && _RELEASE_MINOR == 4)\n  int fail[-1];\n#endif\n])],\n                              [je_cv_cray_84=yes],\n                              [je_cv_cray_84=no])])\nfi\n\nif test \"x$GCC\" = \"xyes\" ; then\n  JE_CFLAGS_ADD([-std=gnu11])\n  if test \"x$je_cv_cflags_added\" = \"x-std=gnu11\" ; then\n    AC_DEFINE_UNQUOTED([JEMALLOC_HAS_RESTRICT])\n  else\n    JE_CFLAGS_ADD([-std=gnu99])\n    if test \"x$je_cv_cflags_added\" = \"x-std=gnu99\" ; then\n      AC_DEFINE_UNQUOTED([JEMALLOC_HAS_RESTRICT])\n    fi\n  fi\n  JE_CFLAGS_ADD([-Wall])\n  JE_CFLAGS_ADD([-Wshorten-64-to-32])\n  JE_CFLAGS_ADD([-Wsign-compare])\n  JE_CFLAGS_ADD([-Wundef])\n  JE_CFLAGS_ADD([-pipe])\n  JE_CFLAGS_ADD([-g3])\nelif test \"x$je_cv_msvc\" = \"xyes\" ; then\n  CC=\"$CC -nologo\"\n  JE_CFLAGS_ADD([-Zi])\n  JE_CFLAGS_ADD([-MT])\n  JE_CFLAGS_ADD([-W3])\n  JE_CFLAGS_ADD([-FS])\n  JE_APPEND_VS(CPPFLAGS, -I${srcdir}/include/msvc_compat)\nfi\nif test \"x$je_cv_cray\" = \"xyes\" ; then\n  dnl cray compiler 8.4 has an inlining bug\n  if test \"x$je_cv_cray_84\" = \"xyes\" ; then\n    JE_CFLAGS_ADD([-hipa2])\n    JE_CFLAGS_ADD([-hnognu])\n  fi\n  dnl ignore unreachable code warning\n  JE_CFLAGS_ADD([-hnomessage=128])\n  dnl ignore redefinition of \"malloc\", \"free\", etc warning\n  JE_CFLAGS_ADD([-hnomessage=1357])\nfi\nAC_SUBST([CONFIGURE_CFLAGS])\nAC_SUBST([SPECIFIED_CFLAGS])\nAC_SUBST([EXTRA_CFLAGS])\nAC_PROG_CPP\n\nAC_ARG_ENABLE([cxx],\n  [AS_HELP_STRING([--disable-cxx], [Disable C++ integration])],\nif test \"x$enable_cxx\" = \"xno\" ; then\n  enable_cxx=\"0\"\nelse\n  enable_cxx=\"1\"\nfi\n,\nenable_cxx=\"1\"\n)\nif test \"x$enable_cxx\" = \"x1\" ; then\n  dnl Require at least c++14, which is the first version to support sized\n  dnl deallocation.  C++ support is not compiled otherwise.\n  m4_include([m4/ax_cxx_compile_stdcxx.m4])\n  AX_CXX_COMPILE_STDCXX([14], [noext], [optional])\n  if test \"x${HAVE_CXX14}\" = \"x1\" ; then\n    JE_CXXFLAGS_ADD([-Wall])\n    JE_CXXFLAGS_ADD([-g3])\n\n    SAVED_LIBS=\"${LIBS}\"\n    JE_APPEND_VS(LIBS, -lstdc++)\n    JE_COMPILABLE([libstdc++ linkage], [\n#include <stdlib.h>\n], [[\n\tint *arr = (int *)malloc(sizeof(int) * 42);\n\tif (arr == NULL)\n\t\treturn 1;\n]], [je_cv_libstdcxx])\n    if test \"x${je_cv_libstdcxx}\" = \"xno\" ; then\n      LIBS=\"${SAVED_LIBS}\"\n    fi\n  else\n    enable_cxx=\"0\"\n  fi\nfi\nAC_SUBST([enable_cxx])\nAC_SUBST([CONFIGURE_CXXFLAGS])\nAC_SUBST([SPECIFIED_CXXFLAGS])\nAC_SUBST([EXTRA_CXXFLAGS])\n\nAC_C_BIGENDIAN([ac_cv_big_endian=1], [ac_cv_big_endian=0])\nif test \"x${ac_cv_big_endian}\" = \"x1\" ; then\n  AC_DEFINE_UNQUOTED([JEMALLOC_BIG_ENDIAN], [ ])\nfi\n\nif test \"x${je_cv_msvc}\" = \"xyes\" -a \"x${ac_cv_header_inttypes_h}\" = \"xno\"; then\n  JE_APPEND_VS(CPPFLAGS, -I${srcdir}/include/msvc_compat/C99)\nfi\n\nif test \"x${je_cv_msvc}\" = \"xyes\" ; then\n  LG_SIZEOF_PTR=LG_SIZEOF_PTR_WIN\n  AC_MSG_RESULT([Using a predefined value for sizeof(void *): 4 for 32-bit, 8 for 64-bit])\nelse\n  AC_CHECK_SIZEOF([void *])\n  if test \"x${ac_cv_sizeof_void_p}\" = \"x8\" ; then\n    LG_SIZEOF_PTR=3\n  elif test \"x${ac_cv_sizeof_void_p}\" = \"x4\" ; then\n    LG_SIZEOF_PTR=2\n  else\n    AC_MSG_ERROR([Unsupported pointer size: ${ac_cv_sizeof_void_p}])\n  fi\nfi\nAC_DEFINE_UNQUOTED([LG_SIZEOF_PTR], [$LG_SIZEOF_PTR])\n\nAC_CHECK_SIZEOF([int])\nif test \"x${ac_cv_sizeof_int}\" = \"x8\" ; then\n  LG_SIZEOF_INT=3\nelif test \"x${ac_cv_sizeof_int}\" = \"x4\" ; then\n  LG_SIZEOF_INT=2\nelse\n  AC_MSG_ERROR([Unsupported int size: ${ac_cv_sizeof_int}])\nfi\nAC_DEFINE_UNQUOTED([LG_SIZEOF_INT], [$LG_SIZEOF_INT])\n\nAC_CHECK_SIZEOF([long])\nif test \"x${ac_cv_sizeof_long}\" = \"x8\" ; then\n  LG_SIZEOF_LONG=3\nelif test \"x${ac_cv_sizeof_long}\" = \"x4\" ; then\n  LG_SIZEOF_LONG=2\nelse\n  AC_MSG_ERROR([Unsupported long size: ${ac_cv_sizeof_long}])\nfi\nAC_DEFINE_UNQUOTED([LG_SIZEOF_LONG], [$LG_SIZEOF_LONG])\n\nAC_CHECK_SIZEOF([long long])\nif test \"x${ac_cv_sizeof_long_long}\" = \"x8\" ; then\n  LG_SIZEOF_LONG_LONG=3\nelif test \"x${ac_cv_sizeof_long_long}\" = \"x4\" ; then\n  LG_SIZEOF_LONG_LONG=2\nelse\n  AC_MSG_ERROR([Unsupported long long size: ${ac_cv_sizeof_long_long}])\nfi\nAC_DEFINE_UNQUOTED([LG_SIZEOF_LONG_LONG], [$LG_SIZEOF_LONG_LONG])\n\nAC_CHECK_SIZEOF([intmax_t])\nif test \"x${ac_cv_sizeof_intmax_t}\" = \"x16\" ; then\n  LG_SIZEOF_INTMAX_T=4\nelif test \"x${ac_cv_sizeof_intmax_t}\" = \"x8\" ; then\n  LG_SIZEOF_INTMAX_T=3\nelif test \"x${ac_cv_sizeof_intmax_t}\" = \"x4\" ; then\n  LG_SIZEOF_INTMAX_T=2\nelse\n  AC_MSG_ERROR([Unsupported intmax_t size: ${ac_cv_sizeof_intmax_t}])\nfi\nAC_DEFINE_UNQUOTED([LG_SIZEOF_INTMAX_T], [$LG_SIZEOF_INTMAX_T])\n\nAC_CANONICAL_HOST\ndnl CPU-specific settings.\nCPU_SPINWAIT=\"\"\ncase \"${host_cpu}\" in\n  i686|x86_64)\n\tif test \"x${je_cv_msvc}\" = \"xyes\" ; then\n\t    AC_CACHE_VAL([je_cv_pause_msvc],\n\t      [JE_COMPILABLE([pause instruction MSVC], [],\n\t\t\t\t\t[[_mm_pause(); return 0;]],\n\t\t\t\t\t[je_cv_pause_msvc])])\n\t    if test \"x${je_cv_pause_msvc}\" = \"xyes\" ; then\n\t\tCPU_SPINWAIT='_mm_pause()'\n\t    fi\n\telse\n\t    AC_CACHE_VAL([je_cv_pause],\n\t      [JE_COMPILABLE([pause instruction], [],\n\t\t\t\t\t[[__asm__ volatile(\"pause\"); return 0;]],\n\t\t\t\t\t[je_cv_pause])])\n\t    if test \"x${je_cv_pause}\" = \"xyes\" ; then\n\t\tCPU_SPINWAIT='__asm__ volatile(\"pause\")'\n\t    fi\n\tfi\n\t;;\n  powerpc*)\n\tAC_DEFINE_UNQUOTED([HAVE_ALTIVEC], [ ])\n\tCPU_SPINWAIT='__asm__ volatile(\"or 31,31,31\")'\n\t;;\n  *)\n\t;;\nesac\nAC_DEFINE_UNQUOTED([CPU_SPINWAIT], [$CPU_SPINWAIT])\n\ncase \"${host_cpu}\" in\n  aarch64)\n    AC_MSG_CHECKING([number of significant virtual address bits])\n    LG_VADDR=48\n    AC_MSG_RESULT([$LG_VADDR])\n    ;;\n  x86_64)\n    AC_CACHE_CHECK([number of significant virtual address bits],\n                   [je_cv_lg_vaddr],\n                   AC_RUN_IFELSE([AC_LANG_PROGRAM(\n[[\n#include <stdio.h>\n#ifdef _WIN32\n#include <limits.h>\n#include <intrin.h>\ntypedef unsigned __int32 uint32_t;\n#else\n#include <stdint.h>\n#endif\n]], [[\n\tuint32_t r[[4]];\n\tuint32_t eax_in = 0x80000008U;\n#ifdef _WIN32\n\t__cpuid((int *)r, (int)eax_in);\n#else\n\tasm volatile (\"cpuid\"\n\t    : \"=a\" (r[[0]]), \"=b\" (r[[1]]), \"=c\" (r[[2]]), \"=d\" (r[[3]])\n\t    : \"a\" (eax_in), \"c\" (0)\n\t);\n#endif\n\tuint32_t eax_out = r[[0]];\n\tuint32_t vaddr = ((eax_out & 0x0000ff00U) >> 8);\n\tFILE *f = fopen(\"conftest.out\", \"w\");\n\tif (f == NULL) {\n\t\treturn 1;\n\t}\n\tif (vaddr > (sizeof(void *) << 3)) {\n\t\tvaddr = sizeof(void *) << 3;\n\t}\n\tfprintf(f, \"%u\", vaddr);\n\tfclose(f);\n\treturn 0;\n]])],\n                   [je_cv_lg_vaddr=`cat conftest.out`],\n                   [je_cv_lg_vaddr=error],\n                   [je_cv_lg_vaddr=57]))\n    if test \"x${je_cv_lg_vaddr}\" != \"x\" ; then\n      LG_VADDR=\"${je_cv_lg_vaddr}\"\n    fi\n    if test \"x${LG_VADDR}\" != \"xerror\" ; then\n      AC_DEFINE_UNQUOTED([LG_VADDR], [$LG_VADDR])\n    else\n      AC_MSG_ERROR([cannot determine number of significant virtual address bits])\n    fi\n    ;;\n  *)\n    AC_MSG_CHECKING([number of significant virtual address bits])\n    if test \"x${LG_SIZEOF_PTR}\" = \"x3\" ; then\n      LG_VADDR=64\n    elif test \"x${LG_SIZEOF_PTR}\" = \"x2\" ; then\n      LG_VADDR=32\n    elif test \"x${LG_SIZEOF_PTR}\" = \"xLG_SIZEOF_PTR_WIN\" ; then\n      LG_VADDR=\"(1U << (LG_SIZEOF_PTR_WIN+3))\"\n    else\n      AC_MSG_ERROR([Unsupported lg(pointer size): ${LG_SIZEOF_PTR}])\n    fi\n    AC_MSG_RESULT([$LG_VADDR])\n    ;;\nesac\nAC_DEFINE_UNQUOTED([LG_VADDR], [$LG_VADDR])\n\nLD_PRELOAD_VAR=\"LD_PRELOAD\"\nso=\"so\"\nimportlib=\"${so}\"\no=\"$ac_objext\"\na=\"a\"\nexe=\"$ac_exeext\"\nlibprefix=\"lib\"\nlink_whole_archive=\"0\"\nDSO_LDFLAGS='-shared -Wl,-soname,$(@F)'\nRPATH='-Wl,-rpath,$(1)'\nSOREV=\"${so}.${rev}\"\nPIC_CFLAGS='-fPIC -DPIC'\nCTARGET='-o $@'\nLDTARGET='-o $@'\nTEST_LD_MODE=\nEXTRA_LDFLAGS=\nARFLAGS='crus'\nAROUT=' $@'\nCC_MM=1\n\nif test \"x$je_cv_cray_prgenv_wrapper\" = \"xyes\" ; then\n  TEST_LD_MODE='-dynamic'\nfi\n\nif test \"x${je_cv_cray}\" = \"xyes\" ; then\n  CC_MM=\nfi\n\nAN_MAKEVAR([AR], [AC_PROG_AR])\nAN_PROGRAM([ar], [AC_PROG_AR])\nAC_DEFUN([AC_PROG_AR], [AC_CHECK_TOOL(AR, ar, :)])\nAC_PROG_AR\n\nAC_PROG_AWK\n\ndnl Platform-specific settings.  abi and RPATH can probably be determined\ndnl programmatically, but doing so is error-prone, which makes it generally\ndnl not worth the trouble.\ndnl \ndnl Define cpp macros in CPPFLAGS, rather than doing AC_DEFINE(macro), since the\ndnl definitions need to be seen before any headers are included, which is a pain\ndnl to make happen otherwise.\ndefault_retain=\"0\"\nmaps_coalesce=\"1\"\nDUMP_SYMS=\"nm -a\"\nSYM_PREFIX=\"\"\ncase \"${host}\" in\n  *-*-darwin* | *-*-ios*)\n\tabi=\"macho\"\n\tRPATH=\"\"\n\tLD_PRELOAD_VAR=\"DYLD_INSERT_LIBRARIES\"\n\tso=\"dylib\"\n\timportlib=\"${so}\"\n\tforce_tls=\"0\"\n\tDSO_LDFLAGS='-shared -Wl,-install_name,$(LIBDIR)/$(@F)'\n\tSOREV=\"${rev}.${so}\"\n\tsbrk_deprecated=\"1\"\n\tSYM_PREFIX=\"_\"\n\t;;\n  *-*-freebsd*)\n\tabi=\"elf\"\n\tAC_DEFINE([JEMALLOC_SYSCTL_VM_OVERCOMMIT], [ ])\n\tforce_lazy_lock=\"1\"\n\t;;\n  *-*-dragonfly*)\n\tabi=\"elf\"\n\t;;\n  *-*-openbsd*)\n\tabi=\"elf\"\n\tforce_tls=\"0\"\n\t;;\n  *-*-bitrig*)\n\tabi=\"elf\"\n\t;;\n  *-*-linux-android)\n\tdnl syscall(2) and secure_getenv(3) are exposed by _GNU_SOURCE.\n\tJE_APPEND_VS(CPPFLAGS, -D_GNU_SOURCE)\n\tabi=\"elf\"\n\tAC_DEFINE([JEMALLOC_PURGE_MADVISE_DONTNEED_ZEROS])\n\tAC_DEFINE([JEMALLOC_HAS_ALLOCA_H])\n\tAC_DEFINE([JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY], [ ])\n\tAC_DEFINE([JEMALLOC_THREADED_INIT], [ ])\n\tAC_DEFINE([JEMALLOC_C11_ATOMICS])\n\tforce_tls=\"0\"\n\tif test \"${LG_SIZEOF_PTR}\" = \"3\"; then\n\t  default_retain=\"1\"\n\tfi\n\t;;\n  *-*-linux* | *-*-kfreebsd*)\n\tdnl syscall(2) and secure_getenv(3) are exposed by _GNU_SOURCE.\n\tJE_APPEND_VS(CPPFLAGS, -D_GNU_SOURCE)\n\tabi=\"elf\"\n\tAC_DEFINE([JEMALLOC_PURGE_MADVISE_DONTNEED_ZEROS])\n\tAC_DEFINE([JEMALLOC_HAS_ALLOCA_H])\n\tAC_DEFINE([JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY], [ ])\n\tAC_DEFINE([JEMALLOC_THREADED_INIT], [ ])\n\tAC_DEFINE([JEMALLOC_USE_CXX_THROW], [ ])\n\tif test \"${LG_SIZEOF_PTR}\" = \"3\"; then\n\t  default_retain=\"1\"\n\tfi\n\t;;\n  *-*-netbsd*)\n\tAC_MSG_CHECKING([ABI])\n        AC_COMPILE_IFELSE([AC_LANG_PROGRAM(\n[[#ifdef __ELF__\n/* ELF */\n#else\n#error aout\n#endif\n]])],\n                          [abi=\"elf\"],\n                          [abi=\"aout\"])\n\tAC_MSG_RESULT([$abi])\n\t;;\n  *-*-solaris2*)\n\tabi=\"elf\"\n\tRPATH='-Wl,-R,$(1)'\n\tdnl Solaris needs this for sigwait().\n\tJE_APPEND_VS(CPPFLAGS, -D_POSIX_PTHREAD_SEMANTICS)\n\tJE_APPEND_VS(LIBS, -lposix4 -lsocket -lnsl)\n\t;;\n  *-ibm-aix*)\n\tif test \"${LG_SIZEOF_PTR}\" = \"3\"; then\n\t  dnl 64bit AIX\n\t  LD_PRELOAD_VAR=\"LDR_PRELOAD64\"\n\telse\n\t  dnl 32bit AIX\n\t  LD_PRELOAD_VAR=\"LDR_PRELOAD\"\n\tfi\n\tabi=\"xcoff\"\n\t;;\n  *-*-mingw* | *-*-cygwin*)\n\tabi=\"pecoff\"\n\tforce_tls=\"0\"\n\tmaps_coalesce=\"0\"\n\tRPATH=\"\"\n\tso=\"dll\"\n\tif test \"x$je_cv_msvc\" = \"xyes\" ; then\n\t  importlib=\"lib\"\n\t  DSO_LDFLAGS=\"-LD\"\n\t  EXTRA_LDFLAGS=\"-link -DEBUG\"\n\t  CTARGET='-Fo$@'\n\t  LDTARGET='-Fe$@'\n\t  AR='lib'\n\t  ARFLAGS='-nologo -out:'\n\t  AROUT='$@'\n\t  CC_MM=\n        else\n\t  importlib=\"${so}\"\n\t  DSO_LDFLAGS=\"-shared\"\n\t  link_whole_archive=\"1\"\n\tfi\n\tDUMP_SYMS=\"dumpbin /SYMBOLS\"\n\ta=\"lib\"\n\tlibprefix=\"\"\n\tSOREV=\"${so}\"\n\tPIC_CFLAGS=\"\"\n\t;;\n  *)\n\tAC_MSG_RESULT([Unsupported operating system: ${host}])\n\tabi=\"elf\"\n\t;;\nesac\n\nJEMALLOC_USABLE_SIZE_CONST=const\nAC_CHECK_HEADERS([malloc.h], [\n  AC_MSG_CHECKING([whether malloc_usable_size definition can use const argument])\n  AC_COMPILE_IFELSE([AC_LANG_PROGRAM(\n    [#include <malloc.h>\n     #include <stddef.h>\n    size_t malloc_usable_size(const void *ptr);\n    ],\n    [])],[\n                AC_MSG_RESULT([yes])\n         ],[\n                JEMALLOC_USABLE_SIZE_CONST=\n                AC_MSG_RESULT([no])\n         ])\n])\nAC_DEFINE_UNQUOTED([JEMALLOC_USABLE_SIZE_CONST], [$JEMALLOC_USABLE_SIZE_CONST])\nAC_SUBST([abi])\nAC_SUBST([RPATH])\nAC_SUBST([LD_PRELOAD_VAR])\nAC_SUBST([so])\nAC_SUBST([importlib])\nAC_SUBST([o])\nAC_SUBST([a])\nAC_SUBST([exe])\nAC_SUBST([libprefix])\nAC_SUBST([link_whole_archive])\nAC_SUBST([DSO_LDFLAGS])\nAC_SUBST([EXTRA_LDFLAGS])\nAC_SUBST([SOREV])\nAC_SUBST([PIC_CFLAGS])\nAC_SUBST([CTARGET])\nAC_SUBST([LDTARGET])\nAC_SUBST([TEST_LD_MODE])\nAC_SUBST([MKLIB])\nAC_SUBST([ARFLAGS])\nAC_SUBST([AROUT])\nAC_SUBST([DUMP_SYMS])\nAC_SUBST([CC_MM])\n\ndnl Determine whether libm must be linked to use e.g. log(3).\nAC_SEARCH_LIBS([log], [m], , [AC_MSG_ERROR([Missing math functions])])\nif test \"x$ac_cv_search_log\" != \"xnone required\" ; then\n  LM=\"$ac_cv_search_log\"\nelse\n  LM=\nfi\nAC_SUBST(LM)\n\nJE_COMPILABLE([__attribute__ syntax],\n              [static __attribute__((unused)) void foo(void){}],\n              [],\n              [je_cv_attribute])\nif test \"x${je_cv_attribute}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_ATTR], [ ])\n  if test \"x${GCC}\" = \"xyes\" -a \"x${abi}\" = \"xelf\"; then\n    JE_CFLAGS_ADD([-fvisibility=hidden])\n    JE_CXXFLAGS_ADD([-fvisibility=hidden])\n  fi\nfi\ndnl Check for tls-dialect support.\nJE_CFLAGS_ADD([-mtls-dialect=gnu2])\nif test \"x$je_cv_cflags_added\" = \"x-mtls-dialect=gnu2\" ; then\n  je_cv_tls_dialect_gnu2=yes\nelse\n  je_cv_tls_dialect_gnu2=no\nfi\nJE_CXXFLAGS_ADD([-mtls-dialect=gnu2])\ndnl Check for tls_model attribute support (clang 3.0 still lacks support).\nJE_CFLAGS_SAVE()\nJE_CFLAGS_ADD([-Werror])\nJE_CFLAGS_ADD([-herror_on_warning])\nJE_COMPILABLE([tls_model attribute], [],\n              [static __thread int\n               __attribute__((tls_model(\"initial-exec\"), unused)) foo;\n               foo = 0;],\n              [je_cv_tls_model])\nJE_CFLAGS_RESTORE()\nif test \"x${je_cv_tls_dialect_gnu2}\" = \"xno\" \\\n -a \"x${je_cv_tls_model}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_TLS_MODEL],\n            [__attribute__((tls_model(\"initial-exec\")))])\nelse\n  AC_DEFINE([JEMALLOC_TLS_MODEL], [ ])\nfi\ndnl Check for alloc_size attribute support.\nJE_CFLAGS_SAVE()\nJE_CFLAGS_ADD([-Werror])\nJE_CFLAGS_ADD([-herror_on_warning])\nJE_COMPILABLE([alloc_size attribute], [#include <stdlib.h>],\n              [void *foo(size_t size) __attribute__((alloc_size(1)));],\n              [je_cv_alloc_size])\nJE_CFLAGS_RESTORE()\nif test \"x${je_cv_alloc_size}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_ATTR_ALLOC_SIZE], [ ])\nfi\ndnl Check for format(gnu_printf, ...) attribute support.\nJE_CFLAGS_SAVE()\nJE_CFLAGS_ADD([-Werror])\nJE_CFLAGS_ADD([-herror_on_warning])\nJE_COMPILABLE([format(gnu_printf, ...) attribute], [#include <stdlib.h>],\n              [void *foo(const char *format, ...) __attribute__((format(gnu_printf, 1, 2)));],\n              [je_cv_format_gnu_printf])\nJE_CFLAGS_RESTORE()\nif test \"x${je_cv_format_gnu_printf}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_ATTR_FORMAT_GNU_PRINTF], [ ])\nfi\ndnl Check for format(printf, ...) attribute support.\nJE_CFLAGS_SAVE()\nJE_CFLAGS_ADD([-Werror])\nJE_CFLAGS_ADD([-herror_on_warning])\nJE_COMPILABLE([format(printf, ...) attribute], [#include <stdlib.h>],\n              [void *foo(const char *format, ...) __attribute__((format(printf, 1, 2)));],\n              [je_cv_format_printf])\nJE_CFLAGS_RESTORE()\nif test \"x${je_cv_format_printf}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_ATTR_FORMAT_PRINTF], [ ])\nfi\n\ndnl Support optional additions to rpath.\nAC_ARG_WITH([rpath],\n  [AS_HELP_STRING([--with-rpath=<rpath>], [Colon-separated rpath (ELF systems only)])],\nif test \"x$with_rpath\" = \"xno\" ; then\n  RPATH_EXTRA=\nelse\n  RPATH_EXTRA=\"`echo $with_rpath | tr \\\":\\\" \\\" \\\"`\"\nfi,\n  RPATH_EXTRA=\n)\nAC_SUBST([RPATH_EXTRA])\n\ndnl Disable rules that do automatic regeneration of configure output by default.\nAC_ARG_ENABLE([autogen],\n  [AS_HELP_STRING([--enable-autogen], [Automatically regenerate configure output])],\nif test \"x$enable_autogen\" = \"xno\" ; then\n  enable_autogen=\"0\"\nelse\n  enable_autogen=\"1\"\nfi\n,\nenable_autogen=\"0\"\n)\nAC_SUBST([enable_autogen])\n\nAC_PROG_INSTALL\nAC_PROG_RANLIB\nAC_PATH_PROG([LD], [ld], [false], [$PATH])\nAC_PATH_PROG([AUTOCONF], [autoconf], [false], [$PATH])\n\ndnl Perform no name mangling by default.\nAC_ARG_WITH([mangling],\n  [AS_HELP_STRING([--with-mangling=<map>], [Mangle symbols in <map>])],\n  [mangling_map=\"$with_mangling\"], [mangling_map=\"\"])\n\ndnl Do not prefix public APIs by default.\nAC_ARG_WITH([jemalloc_prefix],\n  [AS_HELP_STRING([--with-jemalloc-prefix=<prefix>], [Prefix to prepend to all public APIs])],\n  [JEMALLOC_PREFIX=\"$with_jemalloc_prefix\"],\n  [if test \"x$abi\" != \"xmacho\" -a \"x$abi\" != \"xpecoff\"; then\n  JEMALLOC_PREFIX=\"\"\nelse\n  JEMALLOC_PREFIX=\"je_\"\nfi]\n)\nif test \"x$JEMALLOC_PREFIX\" = \"x\" ; then\n  AC_DEFINE([JEMALLOC_IS_MALLOC])\nelse\n  JEMALLOC_CPREFIX=`echo ${JEMALLOC_PREFIX} | tr \"a-z\" \"A-Z\"`\n  AC_DEFINE_UNQUOTED([JEMALLOC_PREFIX], [\"$JEMALLOC_PREFIX\"])\n  AC_DEFINE_UNQUOTED([JEMALLOC_CPREFIX], [\"$JEMALLOC_CPREFIX\"])\nfi\nAC_SUBST([JEMALLOC_PREFIX])\nAC_SUBST([JEMALLOC_CPREFIX])\n\nAC_ARG_WITH([export],\n  [AS_HELP_STRING([--without-export], [disable exporting jemalloc public APIs])],\n  [if test \"x$with_export\" = \"xno\"; then\n  AC_DEFINE([JEMALLOC_EXPORT],[])\nfi]\n)\n\npublic_syms=\"aligned_alloc calloc dallocx free mallctl mallctlbymib mallctlnametomib malloc malloc_conf malloc_message malloc_stats_print malloc_usable_size mallocx nallocx posix_memalign rallocx realloc sallocx sdallocx xallocx\"\ndnl Check for additional platform-specific public API functions.\nAC_CHECK_FUNC([memalign],\n\t      [AC_DEFINE([JEMALLOC_OVERRIDE_MEMALIGN], [ ])\n\t       public_syms=\"${public_syms} memalign\"])\nAC_CHECK_FUNC([valloc],\n\t      [AC_DEFINE([JEMALLOC_OVERRIDE_VALLOC], [ ])\n\t       public_syms=\"${public_syms} valloc\"])\n\ndnl Check for allocator-related functions that should be wrapped.\nwrap_syms=\nif test \"x${JEMALLOC_PREFIX}\" = \"x\" ; then\n  AC_CHECK_FUNC([__libc_calloc],\n\t\t[AC_DEFINE([JEMALLOC_OVERRIDE___LIBC_CALLOC], [ ])\n\t\t wrap_syms=\"${wrap_syms} __libc_calloc\"])\n  AC_CHECK_FUNC([__libc_free],\n\t\t[AC_DEFINE([JEMALLOC_OVERRIDE___LIBC_FREE], [ ])\n\t\t wrap_syms=\"${wrap_syms} __libc_free\"])\n  AC_CHECK_FUNC([__libc_malloc],\n\t\t[AC_DEFINE([JEMALLOC_OVERRIDE___LIBC_MALLOC], [ ])\n\t\t wrap_syms=\"${wrap_syms} __libc_malloc\"])\n  AC_CHECK_FUNC([__libc_memalign],\n\t\t[AC_DEFINE([JEMALLOC_OVERRIDE___LIBC_MEMALIGN], [ ])\n\t\t wrap_syms=\"${wrap_syms} __libc_memalign\"])\n  AC_CHECK_FUNC([__libc_realloc],\n\t\t[AC_DEFINE([JEMALLOC_OVERRIDE___LIBC_REALLOC], [ ])\n\t\t wrap_syms=\"${wrap_syms} __libc_realloc\"])\n  AC_CHECK_FUNC([__libc_valloc],\n\t\t[AC_DEFINE([JEMALLOC_OVERRIDE___LIBC_VALLOC], [ ])\n\t\t wrap_syms=\"${wrap_syms} __libc_valloc\"])\n  AC_CHECK_FUNC([__posix_memalign],\n\t\t[AC_DEFINE([JEMALLOC_OVERRIDE___POSIX_MEMALIGN], [ ])\n\t\t wrap_syms=\"${wrap_syms} __posix_memalign\"])\nfi\n\ncase \"${host}\" in\n  *-*-mingw* | *-*-cygwin*)\n    wrap_syms=\"${wrap_syms} tls_callback\"\n    ;;\n  *)\n    ;;\nesac\n\ndnl Mangle library-private APIs.\nAC_ARG_WITH([private_namespace],\n  [AS_HELP_STRING([--with-private-namespace=<prefix>], [Prefix to prepend to all library-private APIs])],\n  [JEMALLOC_PRIVATE_NAMESPACE=\"${with_private_namespace}je_\"],\n  [JEMALLOC_PRIVATE_NAMESPACE=\"je_\"]\n)\nAC_DEFINE_UNQUOTED([JEMALLOC_PRIVATE_NAMESPACE], [$JEMALLOC_PRIVATE_NAMESPACE])\nprivate_namespace=\"$JEMALLOC_PRIVATE_NAMESPACE\"\nAC_SUBST([private_namespace])\n\ndnl Do not add suffix to installed files by default.\nAC_ARG_WITH([install_suffix],\n  [AS_HELP_STRING([--with-install-suffix=<suffix>], [Suffix to append to all installed files])],\n  [INSTALL_SUFFIX=\"$with_install_suffix\"],\n  [INSTALL_SUFFIX=]\n)\ninstall_suffix=\"$INSTALL_SUFFIX\"\nAC_SUBST([install_suffix])\n\ndnl Specify default malloc_conf.\nAC_ARG_WITH([malloc_conf],\n  [AS_HELP_STRING([--with-malloc-conf=<malloc_conf>], [config.malloc_conf options string])],\n  [JEMALLOC_CONFIG_MALLOC_CONF=\"$with_malloc_conf\"],\n  [JEMALLOC_CONFIG_MALLOC_CONF=\"\"]\n)\nconfig_malloc_conf=\"$JEMALLOC_CONFIG_MALLOC_CONF\"\nAC_DEFINE_UNQUOTED([JEMALLOC_CONFIG_MALLOC_CONF], [\"$config_malloc_conf\"])\n\ndnl Substitute @je_@ in jemalloc_protos.h.in, primarily to make generation of\ndnl jemalloc_protos_jet.h easy.\nje_=\"je_\"\nAC_SUBST([je_])\n\ncfgoutputs_in=\"Makefile.in\"\ncfgoutputs_in=\"${cfgoutputs_in} jemalloc.pc.in\"\ncfgoutputs_in=\"${cfgoutputs_in} doc/html.xsl.in\"\ncfgoutputs_in=\"${cfgoutputs_in} doc/manpages.xsl.in\"\ncfgoutputs_in=\"${cfgoutputs_in} doc/jemalloc.xml.in\"\ncfgoutputs_in=\"${cfgoutputs_in} include/jemalloc/jemalloc_macros.h.in\"\ncfgoutputs_in=\"${cfgoutputs_in} include/jemalloc/jemalloc_protos.h.in\"\ncfgoutputs_in=\"${cfgoutputs_in} include/jemalloc/jemalloc_typedefs.h.in\"\ncfgoutputs_in=\"${cfgoutputs_in} include/jemalloc/internal/jemalloc_preamble.h.in\"\ncfgoutputs_in=\"${cfgoutputs_in} test/test.sh.in\"\ncfgoutputs_in=\"${cfgoutputs_in} test/include/test/jemalloc_test.h.in\"\n\ncfgoutputs_out=\"Makefile\"\ncfgoutputs_out=\"${cfgoutputs_out} jemalloc.pc\"\ncfgoutputs_out=\"${cfgoutputs_out} doc/html.xsl\"\ncfgoutputs_out=\"${cfgoutputs_out} doc/manpages.xsl\"\ncfgoutputs_out=\"${cfgoutputs_out} doc/jemalloc.xml\"\ncfgoutputs_out=\"${cfgoutputs_out} include/jemalloc/jemalloc_macros.h\"\ncfgoutputs_out=\"${cfgoutputs_out} include/jemalloc/jemalloc_protos.h\"\ncfgoutputs_out=\"${cfgoutputs_out} include/jemalloc/jemalloc_typedefs.h\"\ncfgoutputs_out=\"${cfgoutputs_out} include/jemalloc/internal/jemalloc_preamble.h\"\ncfgoutputs_out=\"${cfgoutputs_out} test/test.sh\"\ncfgoutputs_out=\"${cfgoutputs_out} test/include/test/jemalloc_test.h\"\n\ncfgoutputs_tup=\"Makefile\"\ncfgoutputs_tup=\"${cfgoutputs_tup} jemalloc.pc:jemalloc.pc.in\"\ncfgoutputs_tup=\"${cfgoutputs_tup} doc/html.xsl:doc/html.xsl.in\"\ncfgoutputs_tup=\"${cfgoutputs_tup} doc/manpages.xsl:doc/manpages.xsl.in\"\ncfgoutputs_tup=\"${cfgoutputs_tup} doc/jemalloc.xml:doc/jemalloc.xml.in\"\ncfgoutputs_tup=\"${cfgoutputs_tup} include/jemalloc/jemalloc_macros.h:include/jemalloc/jemalloc_macros.h.in\"\ncfgoutputs_tup=\"${cfgoutputs_tup} include/jemalloc/jemalloc_protos.h:include/jemalloc/jemalloc_protos.h.in\"\ncfgoutputs_tup=\"${cfgoutputs_tup} include/jemalloc/jemalloc_typedefs.h:include/jemalloc/jemalloc_typedefs.h.in\"\ncfgoutputs_tup=\"${cfgoutputs_tup} include/jemalloc/internal/jemalloc_preamble.h\"\ncfgoutputs_tup=\"${cfgoutputs_tup} test/test.sh:test/test.sh.in\"\ncfgoutputs_tup=\"${cfgoutputs_tup} test/include/test/jemalloc_test.h:test/include/test/jemalloc_test.h.in\"\n\ncfghdrs_in=\"include/jemalloc/jemalloc_defs.h.in\"\ncfghdrs_in=\"${cfghdrs_in} include/jemalloc/internal/jemalloc_internal_defs.h.in\"\ncfghdrs_in=\"${cfghdrs_in} include/jemalloc/internal/private_symbols.sh\"\ncfghdrs_in=\"${cfghdrs_in} include/jemalloc/internal/private_namespace.sh\"\ncfghdrs_in=\"${cfghdrs_in} include/jemalloc/internal/public_namespace.sh\"\ncfghdrs_in=\"${cfghdrs_in} include/jemalloc/internal/public_unnamespace.sh\"\ncfghdrs_in=\"${cfghdrs_in} include/jemalloc/internal/size_classes.sh\"\ncfghdrs_in=\"${cfghdrs_in} include/jemalloc/jemalloc_rename.sh\"\ncfghdrs_in=\"${cfghdrs_in} include/jemalloc/jemalloc_mangle.sh\"\ncfghdrs_in=\"${cfghdrs_in} include/jemalloc/jemalloc.sh\"\ncfghdrs_in=\"${cfghdrs_in} test/include/test/jemalloc_test_defs.h.in\"\n\ncfghdrs_out=\"include/jemalloc/jemalloc_defs.h\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/jemalloc${install_suffix}.h\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/internal/private_symbols.awk\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/internal/private_symbols_jet.awk\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/internal/public_symbols.txt\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/internal/public_namespace.h\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/internal/public_unnamespace.h\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/internal/size_classes.h\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/jemalloc_protos_jet.h\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/jemalloc_rename.h\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/jemalloc_mangle.h\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/jemalloc_mangle_jet.h\"\ncfghdrs_out=\"${cfghdrs_out} include/jemalloc/internal/jemalloc_internal_defs.h\"\ncfghdrs_out=\"${cfghdrs_out} test/include/test/jemalloc_test_defs.h\"\n\ncfghdrs_tup=\"include/jemalloc/jemalloc_defs.h:include/jemalloc/jemalloc_defs.h.in\"\ncfghdrs_tup=\"${cfghdrs_tup} include/jemalloc/internal/jemalloc_internal_defs.h:include/jemalloc/internal/jemalloc_internal_defs.h.in\"\ncfghdrs_tup=\"${cfghdrs_tup} test/include/test/jemalloc_test_defs.h:test/include/test/jemalloc_test_defs.h.in\"\n\ndnl Do not compile with debugging by default.\nAC_ARG_ENABLE([debug],\n  [AS_HELP_STRING([--enable-debug],\n                  [Build debugging code])],\n[if test \"x$enable_debug\" = \"xno\" ; then\n  enable_debug=\"0\"\nelse\n  enable_debug=\"1\"\nfi\n],\n[enable_debug=\"0\"]\n)\nif test \"x$enable_debug\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_DEBUG], [ ])\nfi\nif test \"x$enable_debug\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_DEBUG], [ ])\nfi\nAC_SUBST([enable_debug])\n\ndnl Only optimize if not debugging.\nif test \"x$enable_debug\" = \"x0\" ; then\n  if test \"x$GCC\" = \"xyes\" ; then\n    JE_CFLAGS_ADD([-O3])\n    JE_CXXFLAGS_ADD([-O3])\n    JE_CFLAGS_ADD([-funroll-loops])\n  elif test \"x$je_cv_msvc\" = \"xyes\" ; then\n    JE_CFLAGS_ADD([-O2])\n    JE_CXXFLAGS_ADD([-O2])\n  else\n    JE_CFLAGS_ADD([-O])\n    JE_CXXFLAGS_ADD([-O])\n  fi\nfi\n\ndnl Enable statistics calculation by default.\nAC_ARG_ENABLE([stats],\n  [AS_HELP_STRING([--disable-stats],\n                  [Disable statistics calculation/reporting])],\n[if test \"x$enable_stats\" = \"xno\" ; then\n  enable_stats=\"0\"\nelse\n  enable_stats=\"1\"\nfi\n],\n[enable_stats=\"1\"]\n)\nif test \"x$enable_stats\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_STATS], [ ])\nfi\nAC_SUBST([enable_stats])\n\ndnl Do not enable profiling by default.\nAC_ARG_ENABLE([prof],\n  [AS_HELP_STRING([--enable-prof], [Enable allocation profiling])],\n[if test \"x$enable_prof\" = \"xno\" ; then\n  enable_prof=\"0\"\nelse\n  enable_prof=\"1\"\nfi\n],\n[enable_prof=\"0\"]\n)\nif test \"x$enable_prof\" = \"x1\" ; then\n  backtrace_method=\"\"\nelse\n  backtrace_method=\"N/A\"\nfi\n\nAC_ARG_ENABLE([prof-libunwind],\n  [AS_HELP_STRING([--enable-prof-libunwind], [Use libunwind for backtracing])],\n[if test \"x$enable_prof_libunwind\" = \"xno\" ; then\n  enable_prof_libunwind=\"0\"\nelse\n  enable_prof_libunwind=\"1\"\nfi\n],\n[enable_prof_libunwind=\"0\"]\n)\nAC_ARG_WITH([static_libunwind],\n  [AS_HELP_STRING([--with-static-libunwind=<libunwind.a>],\n  [Path to static libunwind library; use rather than dynamically linking])],\nif test \"x$with_static_libunwind\" = \"xno\" ; then\n  LUNWIND=\"-lunwind\"\nelse\n  if test ! -f \"$with_static_libunwind\" ; then\n    AC_MSG_ERROR([Static libunwind not found: $with_static_libunwind])\n  fi\n  LUNWIND=\"$with_static_libunwind\"\nfi,\n  LUNWIND=\"-lunwind\"\n)\nif test \"x$backtrace_method\" = \"x\" -a \"x$enable_prof_libunwind\" = \"x1\" ; then\n  AC_CHECK_HEADERS([libunwind.h], , [enable_prof_libunwind=\"0\"])\n  if test \"x$LUNWIND\" = \"x-lunwind\" ; then\n    AC_CHECK_LIB([unwind], [unw_backtrace], [JE_APPEND_VS(LIBS, $LUNWIND)],\n                 [enable_prof_libunwind=\"0\"])\n  else\n    JE_APPEND_VS(LIBS, $LUNWIND)\n  fi\n  if test \"x${enable_prof_libunwind}\" = \"x1\" ; then\n    backtrace_method=\"libunwind\"\n    AC_DEFINE([JEMALLOC_PROF_LIBUNWIND], [ ])\n  fi\nfi\n\nAC_ARG_ENABLE([prof-libgcc],\n  [AS_HELP_STRING([--disable-prof-libgcc],\n  [Do not use libgcc for backtracing])],\n[if test \"x$enable_prof_libgcc\" = \"xno\" ; then\n  enable_prof_libgcc=\"0\"\nelse\n  enable_prof_libgcc=\"1\"\nfi\n],\n[enable_prof_libgcc=\"1\"]\n)\nif test \"x$backtrace_method\" = \"x\" -a \"x$enable_prof_libgcc\" = \"x1\" \\\n     -a \"x$GCC\" = \"xyes\" ; then\n  AC_CHECK_HEADERS([unwind.h], , [enable_prof_libgcc=\"0\"])\n  if test \"x${enable_prof_libgcc}\" = \"x1\" ; then\n    AC_CHECK_LIB([gcc], [_Unwind_Backtrace], [JE_APPEND_VS(LIBS, -lgcc)], [enable_prof_libgcc=\"0\"])\n  fi\n  if test \"x${enable_prof_libgcc}\" = \"x1\" ; then\n    backtrace_method=\"libgcc\"\n    AC_DEFINE([JEMALLOC_PROF_LIBGCC], [ ])\n  fi\nelse\n  enable_prof_libgcc=\"0\"\nfi\n\nAC_ARG_ENABLE([prof-gcc],\n  [AS_HELP_STRING([--disable-prof-gcc],\n  [Do not use gcc intrinsics for backtracing])],\n[if test \"x$enable_prof_gcc\" = \"xno\" ; then\n  enable_prof_gcc=\"0\"\nelse\n  enable_prof_gcc=\"1\"\nfi\n],\n[enable_prof_gcc=\"1\"]\n)\nif test \"x$backtrace_method\" = \"x\" -a \"x$enable_prof_gcc\" = \"x1\" \\\n     -a \"x$GCC\" = \"xyes\" ; then\n  JE_CFLAGS_ADD([-fno-omit-frame-pointer])\n  backtrace_method=\"gcc intrinsics\"\n  AC_DEFINE([JEMALLOC_PROF_GCC], [ ])\nelse\n  enable_prof_gcc=\"0\"\nfi\n\nif test \"x$backtrace_method\" = \"x\" ; then\n  backtrace_method=\"none (disabling profiling)\"\n  enable_prof=\"0\"\nfi\nAC_MSG_CHECKING([configured backtracing method])\nAC_MSG_RESULT([$backtrace_method])\nif test \"x$enable_prof\" = \"x1\" ; then\n  dnl Heap profiling uses the log(3) function.\n  JE_APPEND_VS(LIBS, $LM)\n\n  AC_DEFINE([JEMALLOC_PROF], [ ])\nfi\nAC_SUBST([enable_prof])\n\ndnl Indicate whether adjacent virtual memory mappings automatically coalesce\ndnl (and fragment on demand).\nif test \"x${maps_coalesce}\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_MAPS_COALESCE], [ ])\nfi\n\ndnl Indicate whether to retain memory (rather than using munmap()) by default.\nif test \"x$default_retain\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_RETAIN], [ ])\nfi\n\ndnl Enable allocation from DSS if supported by the OS.\nhave_dss=\"1\"\ndnl Check whether the BSD/SUSv1 sbrk() exists.  If not, disable DSS support.\nAC_CHECK_FUNC([sbrk], [have_sbrk=\"1\"], [have_sbrk=\"0\"])\nif test \"x$have_sbrk\" = \"x1\" ; then\n  if test \"x$sbrk_deprecated\" = \"x1\" ; then\n    AC_MSG_RESULT([Disabling dss allocation because sbrk is deprecated])\n    have_dss=\"0\"\n  fi\nelse\n  have_dss=\"0\"\nfi\n\nif test \"x$have_dss\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_DSS], [ ])\nfi\n\ndnl Support the junk/zero filling option by default.\nAC_ARG_ENABLE([fill],\n  [AS_HELP_STRING([--disable-fill], [Disable support for junk/zero filling])],\n[if test \"x$enable_fill\" = \"xno\" ; then\n  enable_fill=\"0\"\nelse\n  enable_fill=\"1\"\nfi\n],\n[enable_fill=\"1\"]\n)\nif test \"x$enable_fill\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_FILL], [ ])\nfi\nAC_SUBST([enable_fill])\n\ndnl Disable utrace(2)-based tracing by default.\nAC_ARG_ENABLE([utrace],\n  [AS_HELP_STRING([--enable-utrace], [Enable utrace(2)-based tracing])],\n[if test \"x$enable_utrace\" = \"xno\" ; then\n  enable_utrace=\"0\"\nelse\n  enable_utrace=\"1\"\nfi\n],\n[enable_utrace=\"0\"]\n)\nJE_COMPILABLE([utrace(2)], [\n#include <sys/types.h>\n#include <sys/param.h>\n#include <sys/time.h>\n#include <sys/uio.h>\n#include <sys/ktrace.h>\n], [\n\tutrace((void *)0, 0);\n], [je_cv_utrace])\nif test \"x${je_cv_utrace}\" = \"xno\" ; then\n  enable_utrace=\"0\"\nfi\nif test \"x$enable_utrace\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_UTRACE], [ ])\nfi\nAC_SUBST([enable_utrace])\n\ndnl Do not support the xmalloc option by default.\nAC_ARG_ENABLE([xmalloc],\n  [AS_HELP_STRING([--enable-xmalloc], [Support xmalloc option])],\n[if test \"x$enable_xmalloc\" = \"xno\" ; then\n  enable_xmalloc=\"0\"\nelse\n  enable_xmalloc=\"1\"\nfi\n],\n[enable_xmalloc=\"0\"]\n)\nif test \"x$enable_xmalloc\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_XMALLOC], [ ])\nfi\nAC_SUBST([enable_xmalloc])\n\ndnl Support cache-oblivious allocation alignment by default.\nAC_ARG_ENABLE([cache-oblivious],\n  [AS_HELP_STRING([--disable-cache-oblivious],\n                  [Disable support for cache-oblivious allocation alignment])],\n[if test \"x$enable_cache_oblivious\" = \"xno\" ; then\n  enable_cache_oblivious=\"0\"\nelse\n  enable_cache_oblivious=\"1\"\nfi\n],\n[enable_cache_oblivious=\"1\"]\n)\nif test \"x$enable_cache_oblivious\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_CACHE_OBLIVIOUS], [ ])\nfi\nAC_SUBST([enable_cache_oblivious])\n\n\n\nJE_COMPILABLE([a program using __builtin_unreachable], [\nvoid foo (void) {\n  __builtin_unreachable();\n}\n], [\n\t{\n\t\tfoo();\n\t}\n], [je_cv_gcc_builtin_unreachable])\nif test \"x${je_cv_gcc_builtin_unreachable}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_INTERNAL_UNREACHABLE], [__builtin_unreachable])\nelse\n  AC_DEFINE([JEMALLOC_INTERNAL_UNREACHABLE], [abort])\nfi\n\ndnl ============================================================================\ndnl Check for  __builtin_ffsl(), then ffsl(3), and fail if neither are found.\ndnl One of those two functions should (theoretically) exist on all platforms\ndnl that jemalloc currently has a chance of functioning on without modification.\ndnl We additionally assume ffs[ll]() or __builtin_ffs[ll]() are defined if\ndnl ffsl() or __builtin_ffsl() are defined, respectively.\nJE_COMPILABLE([a program using __builtin_ffsl], [\n#include <stdio.h>\n#include <strings.h>\n#include <string.h>\n], [\n\t{\n\t\tint rv = __builtin_ffsl(0x08);\n\t\tprintf(\"%d\\n\", rv);\n\t}\n], [je_cv_gcc_builtin_ffsl])\nif test \"x${je_cv_gcc_builtin_ffsl}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_INTERNAL_FFSLL], [__builtin_ffsll])\n  AC_DEFINE([JEMALLOC_INTERNAL_FFSL], [__builtin_ffsl])\n  AC_DEFINE([JEMALLOC_INTERNAL_FFS], [__builtin_ffs])\nelse\n  JE_COMPILABLE([a program using ffsl], [\n  #include <stdio.h>\n  #include <strings.h>\n  #include <string.h>\n  ], [\n\t{\n\t\tint rv = ffsl(0x08);\n\t\tprintf(\"%d\\n\", rv);\n\t}\n  ], [je_cv_function_ffsl])\n  if test \"x${je_cv_function_ffsl}\" = \"xyes\" ; then\n    AC_DEFINE([JEMALLOC_INTERNAL_FFSLL], [ffsll])\n    AC_DEFINE([JEMALLOC_INTERNAL_FFSL], [ffsl])\n    AC_DEFINE([JEMALLOC_INTERNAL_FFS], [ffs])\n  else\n    AC_MSG_ERROR([Cannot build without ffsl(3) or __builtin_ffsl()])\n  fi\nfi\n\nAC_ARG_WITH([lg_quantum],\n  [AS_HELP_STRING([--with-lg-quantum=<lg-quantum>],\n   [Base 2 log of minimum allocation alignment])],\n  [LG_QUANTA=\"$with_lg_quantum\"],\n  [LG_QUANTA=\"3 4\"])\nif test \"x$with_lg_quantum\" != \"x\" ; then\n  AC_DEFINE_UNQUOTED([LG_QUANTUM], [$with_lg_quantum])\nfi\n\nAC_ARG_WITH([lg_page],\n  [AS_HELP_STRING([--with-lg-page=<lg-page>], [Base 2 log of system page size])],\n  [LG_PAGE=\"$with_lg_page\"], [LG_PAGE=\"detect\"])\nif test \"x$LG_PAGE\" = \"xdetect\"; then\n  AC_CACHE_CHECK([LG_PAGE],\n               [je_cv_lg_page],\n               AC_RUN_IFELSE([AC_LANG_PROGRAM(\n[[\n#include <strings.h>\n#ifdef _WIN32\n#include <windows.h>\n#else\n#include <unistd.h>\n#endif\n#include <stdio.h>\n]],\n[[\n    int result;\n    FILE *f;\n\n#ifdef _WIN32\n    SYSTEM_INFO si;\n    GetSystemInfo(&si);\n    result = si.dwPageSize;\n#else\n    result = sysconf(_SC_PAGESIZE);\n#endif\n    if (result == -1) {\n\treturn 1;\n    }\n    result = JEMALLOC_INTERNAL_FFSL(result) - 1;\n\n    f = fopen(\"conftest.out\", \"w\");\n    if (f == NULL) {\n\treturn 1;\n    }\n    fprintf(f, \"%d\", result);\n    fclose(f);\n\n    return 0;\n]])],\n                             [je_cv_lg_page=`cat conftest.out`],\n                             [je_cv_lg_page=undefined],\n                             [je_cv_lg_page=12]))\nfi\nif test \"x${je_cv_lg_page}\" != \"x\" ; then\n  LG_PAGE=\"${je_cv_lg_page}\"\nfi\nif test \"x${LG_PAGE}\" != \"xundefined\" ; then\n   AC_DEFINE_UNQUOTED([LG_PAGE], [$LG_PAGE])\nelse\n   AC_MSG_ERROR([cannot determine value for LG_PAGE])\nfi\n\nAC_ARG_WITH([lg_hugepage],\n  [AS_HELP_STRING([--with-lg-hugepage=<lg-hugepage>],\n   [Base 2 log of system huge page size])],\n  [je_cv_lg_hugepage=\"${with_lg_hugepage}\"],\n  [je_cv_lg_hugepage=\"\"])\nif test \"x${je_cv_lg_hugepage}\" = \"x\" ; then\n  dnl Look in /proc/meminfo (Linux-specific) for information on the default huge\n  dnl page size, if any.  The relevant line looks like:\n  dnl\n  dnl   Hugepagesize:       2048 kB\n  if test -e \"/proc/meminfo\" ; then\n    hpsk=[`cat /proc/meminfo 2>/dev/null | \\\n          grep -e '^Hugepagesize:[[:space:]]\\+[0-9]\\+[[:space:]]kB$' | \\\n          awk '{print $2}'`]\n    if test \"x${hpsk}\" != \"x\" ; then\n      je_cv_lg_hugepage=10\n      while test \"${hpsk}\" -gt 1 ; do\n        hpsk=\"$((hpsk / 2))\"\n        je_cv_lg_hugepage=\"$((je_cv_lg_hugepage + 1))\"\n      done\n    fi\n  fi\n\n  dnl Set default if unable to automatically configure.\n  if test \"x${je_cv_lg_hugepage}\" = \"x\" ; then\n    je_cv_lg_hugepage=21\n  fi\nfi\nAC_DEFINE_UNQUOTED([LG_HUGEPAGE], [${je_cv_lg_hugepage}])\n\nAC_ARG_WITH([lg_page_sizes],\n  [AS_HELP_STRING([--with-lg-page-sizes=<lg-page-sizes>],\n   [Base 2 logs of system page sizes to support])],\n  [LG_PAGE_SIZES=\"$with_lg_page_sizes\"], [LG_PAGE_SIZES=\"$LG_PAGE\"])\n\ndnl ============================================================================\ndnl jemalloc configuration.\ndnl \n\nAC_ARG_WITH([version],\n  [AS_HELP_STRING([--with-version=<major>.<minor>.<bugfix>-<nrev>-g<gid>],\n   [Version string])],\n  [\n    echo \"${with_version}\" | grep ['^[0-9]\\+\\.[0-9]\\+\\.[0-9]\\+-[0-9]\\+-g[0-9a-f]\\+$'] 2>&1 1>/dev/null\n    if test $? -eq 0 ; then\n      echo \"$with_version\" > \"${objroot}VERSION\"\n    else\n      echo \"${with_version}\" | grep ['^VERSION$'] 2>&1 1>/dev/null\n      if test $? -ne 0 ; then\n        AC_MSG_ERROR([${with_version} does not match <major>.<minor>.<bugfix>-<nrev>-g<gid> or VERSION])\n      fi\n    fi\n  ], [\n    dnl Set VERSION if source directory is inside a git repository.\n    if test \"x`test ! \\\"${srcroot}\\\" && cd \\\"${srcroot}\\\"; git rev-parse --is-inside-work-tree 2>/dev/null`\" = \"xtrue\" ; then\n      dnl Pattern globs aren't powerful enough to match both single- and\n      dnl double-digit version numbers, so iterate over patterns to support up\n      dnl to version 99.99.99 without any accidental matches.\n      for pattern in ['[0-9].[0-9].[0-9]' '[0-9].[0-9].[0-9][0-9]' \\\n                     '[0-9].[0-9][0-9].[0-9]' '[0-9].[0-9][0-9].[0-9][0-9]' \\\n                     '[0-9][0-9].[0-9].[0-9]' '[0-9][0-9].[0-9].[0-9][0-9]' \\\n                     '[0-9][0-9].[0-9][0-9].[0-9]' \\\n                     '[0-9][0-9].[0-9][0-9].[0-9][0-9]']; do\n        (test ! \"${srcroot}\" && cd \"${srcroot}\"; git describe --long --abbrev=40 --match=\"${pattern}\") > \"${objroot}VERSION.tmp\" 2>/dev/null\n        if test $? -eq 0 ; then\n          mv \"${objroot}VERSION.tmp\" \"${objroot}VERSION\"\n          break\n        fi\n      done\n    fi\n    rm -f \"${objroot}VERSION.tmp\"\n  ])\n\nif test ! -e \"${objroot}VERSION\" ; then\n  if test ! -e \"${srcroot}VERSION\" ; then\n    AC_MSG_RESULT(\n      [Missing VERSION file, and unable to generate it; creating bogus VERSION])\n    echo \"0.0.0-0-g0000000000000000000000000000000000000000\" > \"${objroot}VERSION\"\n  else\n    cp ${srcroot}VERSION ${objroot}VERSION\n  fi\nfi\njemalloc_version=`cat \"${objroot}VERSION\"`\njemalloc_version_major=`echo ${jemalloc_version} | tr \".g-\" \" \" | awk '{print [$]1}'`\njemalloc_version_minor=`echo ${jemalloc_version} | tr \".g-\" \" \" | awk '{print [$]2}'`\njemalloc_version_bugfix=`echo ${jemalloc_version} | tr \".g-\" \" \" | awk '{print [$]3}'`\njemalloc_version_nrev=`echo ${jemalloc_version} | tr \".g-\" \" \" | awk '{print [$]4}'`\njemalloc_version_gid=`echo ${jemalloc_version} | tr \".g-\" \" \" | awk '{print [$]5}'`\nAC_SUBST([jemalloc_version])\nAC_SUBST([jemalloc_version_major])\nAC_SUBST([jemalloc_version_minor])\nAC_SUBST([jemalloc_version_bugfix])\nAC_SUBST([jemalloc_version_nrev])\nAC_SUBST([jemalloc_version_gid])\n\ndnl ============================================================================\ndnl Configure pthreads.\n\nif test \"x$abi\" != \"xpecoff\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_PTHREAD], [ ])\n  AC_CHECK_HEADERS([pthread.h], , [AC_MSG_ERROR([pthread.h is missing])])\n  dnl Some systems may embed pthreads functionality in libc; check for libpthread\n  dnl first, but try libc too before failing.\n  AC_CHECK_LIB([pthread], [pthread_create], [JE_APPEND_VS(LIBS, -lpthread)],\n               [AC_SEARCH_LIBS([pthread_create], , ,\n                               AC_MSG_ERROR([libpthread is missing]))])\n  wrap_syms=\"${wrap_syms} pthread_create\"\n  have_pthread=\"1\"\n  dnl Check if we have dlsym support.\n  have_dlsym=\"1\"\n  AC_CHECK_HEADERS([dlfcn.h],\n    AC_CHECK_FUNC([dlsym], [],\n      [AC_CHECK_LIB([dl], [dlsym], [LIBS=\"$LIBS -ldl\"], [have_dlsym=\"0\"])]),\n    [have_dlsym=\"0\"])\n  if test \"x$have_dlsym\" = \"x1\" ; then\n    AC_DEFINE([JEMALLOC_HAVE_DLSYM], [ ])\n  fi\n  JE_COMPILABLE([pthread_atfork(3)], [\n#include <pthread.h>\n], [\n  pthread_atfork((void *)0, (void *)0, (void *)0);\n], [je_cv_pthread_atfork])\n  if test \"x${je_cv_pthread_atfork}\" = \"xyes\" ; then\n    AC_DEFINE([JEMALLOC_HAVE_PTHREAD_ATFORK], [ ])\n  fi\nfi\n\nJE_APPEND_VS(CPPFLAGS, -D_REENTRANT)\n\ndnl Check whether clock_gettime(2) is in libc or librt.\nAC_SEARCH_LIBS([clock_gettime], [rt])\n\ndnl Cray wrapper compiler often adds `-lrt` when using `-static`. Check with\ndnl `-dynamic` as well in case a user tries to dynamically link in jemalloc\nif test \"x$je_cv_cray_prgenv_wrapper\" = \"xyes\" ; then\n  if test \"$ac_cv_search_clock_gettime\" != \"-lrt\"; then\n    JE_CFLAGS_SAVE()\n\n    unset ac_cv_search_clock_gettime\n    JE_CFLAGS_ADD([-dynamic])\n    AC_SEARCH_LIBS([clock_gettime], [rt])\n\n    JE_CFLAGS_RESTORE()\n  fi\nfi\n\ndnl check for CLOCK_MONOTONIC_COARSE (Linux-specific).\nJE_COMPILABLE([clock_gettime(CLOCK_MONOTONIC_COARSE, ...)], [\n#include <time.h>\n], [\n\tstruct timespec ts;\n\n\tclock_gettime(CLOCK_MONOTONIC_COARSE, &ts);\n], [je_cv_clock_monotonic_coarse])\nif test \"x${je_cv_clock_monotonic_coarse}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_CLOCK_MONOTONIC_COARSE])\nfi\n\ndnl check for CLOCK_MONOTONIC.\nJE_COMPILABLE([clock_gettime(CLOCK_MONOTONIC, ...)], [\n#include <unistd.h>\n#include <time.h>\n], [\n\tstruct timespec ts;\n\n\tclock_gettime(CLOCK_MONOTONIC, &ts);\n#if !defined(_POSIX_MONOTONIC_CLOCK) || _POSIX_MONOTONIC_CLOCK < 0\n#  error _POSIX_MONOTONIC_CLOCK missing/invalid\n#endif\n], [je_cv_clock_monotonic])\nif test \"x${je_cv_clock_monotonic}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_CLOCK_MONOTONIC])\nfi\n\ndnl Check for mach_absolute_time().\nJE_COMPILABLE([mach_absolute_time()], [\n#include <mach/mach_time.h>\n], [\n\tmach_absolute_time();\n], [je_cv_mach_absolute_time])\nif test \"x${je_cv_mach_absolute_time}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_MACH_ABSOLUTE_TIME])\nfi\n\ndnl Use syscall(2) (if available) by default.\nAC_ARG_ENABLE([syscall],\n  [AS_HELP_STRING([--disable-syscall], [Disable use of syscall(2)])],\n[if test \"x$enable_syscall\" = \"xno\" ; then\n  enable_syscall=\"0\"\nelse\n  enable_syscall=\"1\"\nfi\n],\n[enable_syscall=\"1\"]\n)\nif test \"x$enable_syscall\" = \"x1\" ; then\n  dnl Check if syscall(2) is usable.  Treat warnings as errors, so that e.g. OS\n  dnl X 10.12's deprecation warning prevents use.\n  JE_CFLAGS_SAVE()\n  JE_CFLAGS_ADD([-Werror])\n  JE_COMPILABLE([syscall(2)], [\n#include <sys/syscall.h>\n#include <unistd.h>\n], [\n\tsyscall(SYS_write, 2, \"hello\", 5);\n],\n                [je_cv_syscall])\n  JE_CFLAGS_RESTORE()\n  if test \"x$je_cv_syscall\" = \"xyes\" ; then\n    AC_DEFINE([JEMALLOC_USE_SYSCALL], [ ])\n  fi\nfi\n\ndnl Check if the GNU-specific secure_getenv function exists.\nAC_CHECK_FUNC([secure_getenv],\n              [have_secure_getenv=\"1\"],\n              [have_secure_getenv=\"0\"]\n             )\nif test \"x$have_secure_getenv\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_SECURE_GETENV], [ ])\nfi\n\ndnl Check if the GNU-specific sched_getcpu function exists.\nAC_CHECK_FUNC([sched_getcpu],\n              [have_sched_getcpu=\"1\"],\n              [have_sched_getcpu=\"0\"]\n             )\nif test \"x$have_sched_getcpu\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_SCHED_GETCPU], [ ])\nfi\n\ndnl Check if the GNU-specific sched_setaffinity function exists.\nAC_CHECK_FUNC([sched_setaffinity],\n              [have_sched_setaffinity=\"1\"],\n              [have_sched_setaffinity=\"0\"]\n             )\nif test \"x$have_sched_setaffinity\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_SCHED_SETAFFINITY], [ ])\nfi\n\ndnl Check if the Solaris/BSD issetugid function exists.\nAC_CHECK_FUNC([issetugid],\n              [have_issetugid=\"1\"],\n              [have_issetugid=\"0\"]\n             )\nif test \"x$have_issetugid\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_ISSETUGID], [ ])\nfi\n\ndnl Check whether the BSD-specific _malloc_thread_cleanup() exists.  If so, use\ndnl it rather than pthreads TSD cleanup functions to support cleanup during\ndnl thread exit, in order to avoid pthreads library recursion during\ndnl bootstrapping.\nAC_CHECK_FUNC([_malloc_thread_cleanup],\n              [have__malloc_thread_cleanup=\"1\"],\n              [have__malloc_thread_cleanup=\"0\"]\n             )\nif test \"x$have__malloc_thread_cleanup\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_MALLOC_THREAD_CLEANUP], [ ])\n  wrap_syms=\"${wrap_syms} _malloc_thread_cleanup\"\n  force_tls=\"1\"\nfi\n\ndnl Check whether the BSD-specific _pthread_mutex_init_calloc_cb() exists.  If\ndnl so, mutex initialization causes allocation, and we need to implement this\ndnl callback function in order to prevent recursive allocation.\nAC_CHECK_FUNC([_pthread_mutex_init_calloc_cb],\n              [have__pthread_mutex_init_calloc_cb=\"1\"],\n              [have__pthread_mutex_init_calloc_cb=\"0\"]\n             )\nif test \"x$have__pthread_mutex_init_calloc_cb\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_MUTEX_INIT_CB])\n  wrap_syms=\"${wrap_syms} _malloc_prefork _malloc_postfork\"\nfi\n\ndnl Disable lazy locking by default.\nAC_ARG_ENABLE([lazy_lock],\n  [AS_HELP_STRING([--enable-lazy-lock],\n  [Enable lazy locking (only lock when multi-threaded)])],\n[if test \"x$enable_lazy_lock\" = \"xno\" ; then\n  enable_lazy_lock=\"0\"\nelse\n  enable_lazy_lock=\"1\"\nfi\n],\n[enable_lazy_lock=\"\"]\n)\nif test \"x${enable_lazy_lock}\" = \"x\" ; then\n  if test \"x${force_lazy_lock}\" = \"x1\" ; then\n    AC_MSG_RESULT([Forcing lazy-lock to avoid allocator/threading bootstrap issues])\n    enable_lazy_lock=\"1\"\n  else\n    enable_lazy_lock=\"0\"\n  fi\nfi\nif test \"x${enable_lazy_lock}\" = \"x1\" -a \"x${abi}\" = \"xpecoff\" ; then\n  AC_MSG_RESULT([Forcing no lazy-lock because thread creation monitoring is unimplemented])\n  enable_lazy_lock=\"0\"\nfi\nif test \"x$enable_lazy_lock\" = \"x1\" ; then\n  if test \"x$have_dlsym\" = \"x1\" ; then\n    AC_DEFINE([JEMALLOC_LAZY_LOCK], [ ])\n  else\n    AC_MSG_ERROR([Missing dlsym support: lazy-lock cannot be enabled.])\n  fi\nfi\nAC_SUBST([enable_lazy_lock])\n\ndnl Automatically configure TLS.\nif test \"x${force_tls}\" = \"x1\" ; then\n  enable_tls=\"1\"\nelif test \"x${force_tls}\" = \"x0\" ; then\n  enable_tls=\"0\"\nelse\n  enable_tls=\"1\"\nfi\nif test \"x${enable_tls}\" = \"x1\" ; then\nAC_MSG_CHECKING([for TLS])\nAC_COMPILE_IFELSE([AC_LANG_PROGRAM(\n[[\n    __thread int x;\n]], [[\n    x = 42;\n\n    return 0;\n]])],\n              AC_MSG_RESULT([yes]),\n              AC_MSG_RESULT([no])\n              enable_tls=\"0\")\nelse\n  enable_tls=\"0\"\nfi\nAC_SUBST([enable_tls])\nif test \"x${enable_tls}\" = \"x1\" ; then\n  AC_DEFINE_UNQUOTED([JEMALLOC_TLS], [ ])\nfi\n\ndnl ============================================================================\ndnl Check for C11 atomics.\n\nJE_COMPILABLE([C11 atomics], [\n#include <stdint.h>\n#if (__STDC_VERSION__ >= 201112L) && !defined(__STDC_NO_ATOMICS__)\n#include <stdatomic.h>\n#else\n#error Atomics not available\n#endif\n], [\n    uint64_t *p = (uint64_t *)0;\n    uint64_t x = 1;\n    volatile atomic_uint_least64_t *a = (volatile atomic_uint_least64_t *)p;\n    uint64_t r = atomic_fetch_add(a, x) + x;\n    return r == 0;\n], [je_cv_c11_atomics])\nif test \"x${je_cv_c11_atomics}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_C11_ATOMICS])\nfi\n\ndnl ============================================================================\ndnl Check for GCC-style __atomic atomics.\n\nJE_COMPILABLE([GCC __atomic atomics], [\n], [\n    int x = 0;\n    int val = 1;\n    int y = __atomic_fetch_add(&x, val, __ATOMIC_RELAXED);\n    int after_add = x;\n    return after_add == 1;\n], [je_cv_gcc_atomic_atomics])\nif test \"x${je_cv_gcc_atomic_atomics}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_GCC_ATOMIC_ATOMICS])\nfi\n\ndnl ============================================================================\ndnl Check for GCC-style __sync atomics.\n\nJE_COMPILABLE([GCC __sync atomics], [\n], [\n    int x = 0;\n    int before_add = __sync_fetch_and_add(&x, 1);\n    int after_add = x;\n    return (before_add == 0) && (after_add == 1);\n], [je_cv_gcc_sync_atomics])\nif test \"x${je_cv_gcc_sync_atomics}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_GCC_SYNC_ATOMICS])\nfi\n\ndnl ============================================================================\ndnl Check for atomic(3) operations as provided on Darwin.\ndnl We need this not for the atomic operations (which are provided above), but\ndnl rather for the OSSpinLock type it exposes.\n\nJE_COMPILABLE([Darwin OSAtomic*()], [\n#include <libkern/OSAtomic.h>\n#include <inttypes.h>\n], [\n\t{\n\t\tint32_t x32 = 0;\n\t\tvolatile int32_t *x32p = &x32;\n\t\tOSAtomicAdd32(1, x32p);\n\t}\n\t{\n\t\tint64_t x64 = 0;\n\t\tvolatile int64_t *x64p = &x64;\n\t\tOSAtomicAdd64(1, x64p);\n\t}\n], [je_cv_osatomic])\nif test \"x${je_cv_osatomic}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_OSATOMIC], [ ])\nfi\n\ndnl ============================================================================\ndnl Check for madvise(2).\n\nJE_COMPILABLE([madvise(2)], [\n#include <sys/mman.h>\n], [\n\tmadvise((void *)0, 0, 0);\n], [je_cv_madvise])\nif test \"x${je_cv_madvise}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_MADVISE], [ ])\n\n  dnl Check for madvise(..., MADV_FREE).\n  JE_COMPILABLE([madvise(..., MADV_FREE)], [\n#include <sys/mman.h>\n], [\n\tmadvise((void *)0, 0, MADV_FREE);\n], [je_cv_madv_free])\n  if test \"x${je_cv_madv_free}\" = \"xyes\" ; then\n    AC_DEFINE([JEMALLOC_PURGE_MADVISE_FREE], [ ])\n  fi\n\n  dnl Check for madvise(..., MADV_DONTNEED).\n  JE_COMPILABLE([madvise(..., MADV_DONTNEED)], [\n#include <sys/mman.h>\n], [\n\tmadvise((void *)0, 0, MADV_DONTNEED);\n], [je_cv_madv_dontneed])\n  if test \"x${je_cv_madv_dontneed}\" = \"xyes\" ; then\n    AC_DEFINE([JEMALLOC_PURGE_MADVISE_DONTNEED], [ ])\n  fi\n\n  dnl Check for madvise(..., MADV_[NO]HUGEPAGE).\n  JE_COMPILABLE([madvise(..., MADV_[[NO]]HUGEPAGE)], [\n#include <sys/mman.h>\n], [\n\tmadvise((void *)0, 0, MADV_HUGEPAGE);\n\tmadvise((void *)0, 0, MADV_NOHUGEPAGE);\n], [je_cv_thp])\nfi\n\ndnl Enable transparent huge page support by default.\nAC_ARG_ENABLE([thp],\n  [AS_HELP_STRING([--disable-thp],\n                  [Disable transparent huge page support])],\n[if test \"x$enable_thp\" = \"xno\" -o \"x${je_cv_thp}\" != \"xyes\" ; then\n  enable_thp=\"0\"\nelse\n  enable_thp=\"1\"\nfi\n],\n[if test \"x${je_cv_thp}\" = \"xyes\" ; then\n  enable_thp=\"1\"\nelse\n  enable_thp=\"0\"\nfi\n])\nif test \"x$enable_thp\" = \"x1\" ; then\n  AC_DEFINE([JEMALLOC_THP], [ ])\nfi\nAC_SUBST([enable_thp])\n\ndnl ============================================================================\ndnl Check whether __sync_{add,sub}_and_fetch() are available despite\ndnl __GCC_HAVE_SYNC_COMPARE_AND_SWAP_n macros being undefined.\n\nAC_DEFUN([JE_SYNC_COMPARE_AND_SWAP_CHECK],[\n  AC_CACHE_CHECK([whether to force $1-bit __sync_{add,sub}_and_fetch()],\n               [je_cv_sync_compare_and_swap_$2],\n               [AC_LINK_IFELSE([AC_LANG_PROGRAM([\n                                                 #include <stdint.h>\n                                                ],\n                                                [\n                                                 #ifndef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_$2\n                                                 {\n                                                    uint$1_t x$1 = 0;\n                                                    __sync_add_and_fetch(&x$1, 42);\n                                                    __sync_sub_and_fetch(&x$1, 1);\n                                                 }\n                                                 #else\n                                                 #error __GCC_HAVE_SYNC_COMPARE_AND_SWAP_$2 is defined, no need to force\n                                                 #endif\n                                                ])],\n                               [je_cv_sync_compare_and_swap_$2=yes],\n                               [je_cv_sync_compare_and_swap_$2=no])])\n\n  if test \"x${je_cv_sync_compare_and_swap_$2}\" = \"xyes\" ; then\n    AC_DEFINE([JE_FORCE_SYNC_COMPARE_AND_SWAP_$2], [ ])\n  fi\n])\n\nif test \"x${je_cv_atomic9}\" != \"xyes\" -a \"x${je_cv_osatomic}\" != \"xyes\" ; then\n  JE_SYNC_COMPARE_AND_SWAP_CHECK(32, 4)\n  JE_SYNC_COMPARE_AND_SWAP_CHECK(64, 8)\nfi\n\ndnl ============================================================================\ndnl Check for __builtin_clz() and __builtin_clzl().\n\nAC_CACHE_CHECK([for __builtin_clz],\n               [je_cv_builtin_clz],\n               [AC_LINK_IFELSE([AC_LANG_PROGRAM([],\n                                                [\n                                                {\n                                                        unsigned x = 0;\n                                                        int y = __builtin_clz(x);\n                                                }\n                                                {\n                                                        unsigned long x = 0;\n                                                        int y = __builtin_clzl(x);\n                                                }\n                                                ])],\n                               [je_cv_builtin_clz=yes],\n                               [je_cv_builtin_clz=no])])\n\nif test \"x${je_cv_builtin_clz}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_BUILTIN_CLZ], [ ])\nfi\n\ndnl ============================================================================\ndnl Check for os_unfair_lock operations as provided on Darwin.\n\nJE_COMPILABLE([Darwin os_unfair_lock_*()], [\n#include <os/lock.h>\n#include <AvailabilityMacros.h>\n], [\n\t#if MAC_OS_X_VERSION_MIN_REQUIRED < 101200\n\t#error \"os_unfair_lock is not supported\"\n\t#else\n\tos_unfair_lock lock = OS_UNFAIR_LOCK_INIT;\n\tos_unfair_lock_lock(&lock);\n\tos_unfair_lock_unlock(&lock);\n\t#endif\n], [je_cv_os_unfair_lock])\nif test \"x${je_cv_os_unfair_lock}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_OS_UNFAIR_LOCK], [ ])\nfi\n\ndnl ============================================================================\ndnl Check for spinlock(3) operations as provided on Darwin.\n\nJE_COMPILABLE([Darwin OSSpin*()], [\n#include <libkern/OSAtomic.h>\n#include <inttypes.h>\n], [\n\tOSSpinLock lock = 0;\n\tOSSpinLockLock(&lock);\n\tOSSpinLockUnlock(&lock);\n], [je_cv_osspin])\nif test \"x${je_cv_osspin}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_OSSPIN], [ ])\nfi\n\ndnl ============================================================================\ndnl Darwin-related configuration.\n\nAC_ARG_ENABLE([zone-allocator],\n  [AS_HELP_STRING([--disable-zone-allocator],\n                  [Disable zone allocator for Darwin])],\n[if test \"x$enable_zone_allocator\" = \"xno\" ; then\n  enable_zone_allocator=\"0\"\nelse\n  enable_zone_allocator=\"1\"\nfi\n],\n[if test \"x${abi}\" = \"xmacho\"; then\n  enable_zone_allocator=\"1\"\nfi\n]\n)\nAC_SUBST([enable_zone_allocator])\n\nif test \"x${enable_zone_allocator}\" = \"x1\" ; then\n  if test \"x${abi}\" != \"xmacho\"; then\n    AC_MSG_ERROR([--enable-zone-allocator is only supported on Darwin])\n  fi\n  AC_DEFINE([JEMALLOC_ZONE], [ ])\nfi\n\ndnl ============================================================================\ndnl Enable background threads if possible.\n\nif test \"x${have_pthread}\" = \"x1\" -a \"x${have_dlsym}\" = \"x1\" \\\n    -a \"x${je_cv_os_unfair_lock}\" != \"xyes\" \\\n    -a \"x${je_cv_osspin}\" != \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_BACKGROUND_THREAD])\nfi\n\ndnl ============================================================================\ndnl Check for glibc malloc hooks\n\nJE_COMPILABLE([glibc malloc hook], [\n#include <stddef.h>\n\nextern void (* __free_hook)(void *ptr);\nextern void *(* __malloc_hook)(size_t size);\nextern void *(* __realloc_hook)(void *ptr, size_t size);\n], [\n  void *ptr = 0L;\n  if (__malloc_hook) ptr = __malloc_hook(1);\n  if (__realloc_hook) ptr = __realloc_hook(ptr, 2);\n  if (__free_hook && ptr) __free_hook(ptr);\n], [je_cv_glibc_malloc_hook])\nif test \"x${je_cv_glibc_malloc_hook}\" = \"xyes\" ; then\n  if test \"x${JEMALLOC_PREFIX}\" = \"x\" ; then\n    AC_DEFINE([JEMALLOC_GLIBC_MALLOC_HOOK], [ ])\n    wrap_syms=\"${wrap_syms} __free_hook __malloc_hook __realloc_hook\"\n  fi\nfi\n\nJE_COMPILABLE([glibc memalign hook], [\n#include <stddef.h>\n\nextern void *(* __memalign_hook)(size_t alignment, size_t size);\n], [\n  void *ptr = 0L;\n  if (__memalign_hook) ptr = __memalign_hook(16, 7);\n], [je_cv_glibc_memalign_hook])\nif test \"x${je_cv_glibc_memalign_hook}\" = \"xyes\" ; then\n  if test \"x${JEMALLOC_PREFIX}\" = \"x\" ; then\n    AC_DEFINE([JEMALLOC_GLIBC_MEMALIGN_HOOK], [ ])\n    wrap_syms=\"${wrap_syms} __memalign_hook\"\n  fi\nfi\n\nJE_COMPILABLE([pthreads adaptive mutexes], [\n#include <pthread.h>\n], [\n  pthread_mutexattr_t attr;\n  pthread_mutexattr_init(&attr);\n  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP);\n  pthread_mutexattr_destroy(&attr);\n], [je_cv_pthread_mutex_adaptive_np])\nif test \"x${je_cv_pthread_mutex_adaptive_np}\" = \"xyes\" ; then\n  AC_DEFINE([JEMALLOC_HAVE_PTHREAD_MUTEX_ADAPTIVE_NP], [ ])\nfi\n\ndnl ============================================================================\ndnl Check for typedefs, structures, and compiler characteristics.\nAC_HEADER_STDBOOL\n\ndnl ============================================================================\ndnl Define commands that generate output files.\n\nAC_CONFIG_COMMANDS([include/jemalloc/internal/public_symbols.txt], [\n  f=\"${objroot}include/jemalloc/internal/public_symbols.txt\"\n  mkdir -p \"${objroot}include/jemalloc/internal\"\n  cp /dev/null \"${f}\"\n  for nm in `echo ${mangling_map} |tr ',' ' '` ; do\n    n=`echo ${nm} |tr ':' ' ' |awk '{print $[]1}'`\n    m=`echo ${nm} |tr ':' ' ' |awk '{print $[]2}'`\n    echo \"${n}:${m}\" >> \"${f}\"\n    dnl Remove name from public_syms so that it isn't redefined later.\n    public_syms=`for sym in ${public_syms}; do echo \"${sym}\"; done |grep -v \"^${n}\\$\" |tr '\\n' ' '`\n  done\n  for sym in ${public_syms} ; do\n    n=\"${sym}\"\n    m=\"${JEMALLOC_PREFIX}${sym}\"\n    echo \"${n}:${m}\" >> \"${f}\"\n  done\n], [\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n  mangling_map=\"${mangling_map}\"\n  public_syms=\"${public_syms}\"\n  JEMALLOC_PREFIX=\"${JEMALLOC_PREFIX}\"\n])\nAC_CONFIG_COMMANDS([include/jemalloc/internal/private_symbols.awk], [\n  f=\"${objroot}include/jemalloc/internal/private_symbols.awk\"\n  mkdir -p \"${objroot}include/jemalloc/internal\"\n  export_syms=`for sym in ${public_syms}; do echo \"${JEMALLOC_PREFIX}${sym}\"; done; for sym in ${wrap_syms}; do echo \"${sym}\"; done;`\n  \"${srcdir}/include/jemalloc/internal/private_symbols.sh\" \"${SYM_PREFIX}\" ${export_syms} > \"${objroot}include/jemalloc/internal/private_symbols.awk\"\n], [\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n  public_syms=\"${public_syms}\"\n  wrap_syms=\"${wrap_syms}\"\n  SYM_PREFIX=\"${SYM_PREFIX}\"\n  JEMALLOC_PREFIX=\"${JEMALLOC_PREFIX}\"\n])\nAC_CONFIG_COMMANDS([include/jemalloc/internal/private_symbols_jet.awk], [\n  f=\"${objroot}include/jemalloc/internal/private_symbols_jet.awk\"\n  mkdir -p \"${objroot}include/jemalloc/internal\"\n  export_syms=`for sym in ${public_syms}; do echo \"jet_${sym}\"; done; for sym in ${wrap_syms}; do echo \"${sym}\"; done;`\n  \"${srcdir}/include/jemalloc/internal/private_symbols.sh\" \"${SYM_PREFIX}\" ${export_syms} > \"${objroot}include/jemalloc/internal/private_symbols_jet.awk\"\n], [\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n  public_syms=\"${public_syms}\"\n  wrap_syms=\"${wrap_syms}\"\n  SYM_PREFIX=\"${SYM_PREFIX}\"\n])\nAC_CONFIG_COMMANDS([include/jemalloc/internal/public_namespace.h], [\n  mkdir -p \"${objroot}include/jemalloc/internal\"\n  \"${srcdir}/include/jemalloc/internal/public_namespace.sh\" \"${objroot}include/jemalloc/internal/public_symbols.txt\" > \"${objroot}include/jemalloc/internal/public_namespace.h\"\n], [\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n])\nAC_CONFIG_COMMANDS([include/jemalloc/internal/public_unnamespace.h], [\n  mkdir -p \"${objroot}include/jemalloc/internal\"\n  \"${srcdir}/include/jemalloc/internal/public_unnamespace.sh\" \"${objroot}include/jemalloc/internal/public_symbols.txt\" > \"${objroot}include/jemalloc/internal/public_unnamespace.h\"\n], [\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n])\nAC_CONFIG_COMMANDS([include/jemalloc/internal/size_classes.h], [\n  mkdir -p \"${objroot}include/jemalloc/internal\"\n  \"${SHELL}\" \"${srcdir}/include/jemalloc/internal/size_classes.sh\" \"${LG_QUANTA}\" 3 \"${LG_PAGE_SIZES}\" 2 > \"${objroot}include/jemalloc/internal/size_classes.h\"\n], [\n  SHELL=\"${SHELL}\"\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n  LG_QUANTA=\"${LG_QUANTA}\"\n  LG_PAGE_SIZES=\"${LG_PAGE_SIZES}\"\n])\nAC_CONFIG_COMMANDS([include/jemalloc/jemalloc_protos_jet.h], [\n  mkdir -p \"${objroot}include/jemalloc\"\n  cat \"${srcdir}/include/jemalloc/jemalloc_protos.h.in\" | sed -e 's/@je_@/jet_/g' > \"${objroot}include/jemalloc/jemalloc_protos_jet.h\"\n], [\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n])\nAC_CONFIG_COMMANDS([include/jemalloc/jemalloc_rename.h], [\n  mkdir -p \"${objroot}include/jemalloc\"\n  \"${srcdir}/include/jemalloc/jemalloc_rename.sh\" \"${objroot}include/jemalloc/internal/public_symbols.txt\" > \"${objroot}include/jemalloc/jemalloc_rename.h\"\n], [\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n])\nAC_CONFIG_COMMANDS([include/jemalloc/jemalloc_mangle.h], [\n  mkdir -p \"${objroot}include/jemalloc\"\n  \"${srcdir}/include/jemalloc/jemalloc_mangle.sh\" \"${objroot}include/jemalloc/internal/public_symbols.txt\" je_ > \"${objroot}include/jemalloc/jemalloc_mangle.h\"\n], [\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n])\nAC_CONFIG_COMMANDS([include/jemalloc/jemalloc_mangle_jet.h], [\n  mkdir -p \"${objroot}include/jemalloc\"\n  \"${srcdir}/include/jemalloc/jemalloc_mangle.sh\" \"${objroot}include/jemalloc/internal/public_symbols.txt\" jet_ > \"${objroot}include/jemalloc/jemalloc_mangle_jet.h\"\n], [\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n])\nAC_CONFIG_COMMANDS([include/jemalloc/jemalloc.h], [\n  mkdir -p \"${objroot}include/jemalloc\"\n  \"${srcdir}/include/jemalloc/jemalloc.sh\" \"${objroot}\" > \"${objroot}include/jemalloc/jemalloc${install_suffix}.h\"\n], [\n  srcdir=\"${srcdir}\"\n  objroot=\"${objroot}\"\n  install_suffix=\"${install_suffix}\"\n])\n\ndnl Process .in files.\nAC_SUBST([cfghdrs_in])\nAC_SUBST([cfghdrs_out])\nAC_CONFIG_HEADERS([$cfghdrs_tup])\n\ndnl ============================================================================\ndnl Generate outputs.\n\nAC_CONFIG_FILES([$cfgoutputs_tup config.stamp bin/jemalloc-config bin/jemalloc.sh bin/jeprof])\nAC_SUBST([cfgoutputs_in])\nAC_SUBST([cfgoutputs_out])\nAC_OUTPUT\n\ndnl ============================================================================\ndnl Print out the results of configuration.\nAC_MSG_RESULT([===============================================================================])\nAC_MSG_RESULT([jemalloc version   : ${jemalloc_version}])\nAC_MSG_RESULT([library revision   : ${rev}])\nAC_MSG_RESULT([])\nAC_MSG_RESULT([CONFIG             : ${CONFIG}])\nAC_MSG_RESULT([CC                 : ${CC}])\nAC_MSG_RESULT([CONFIGURE_CFLAGS   : ${CONFIGURE_CFLAGS}])\nAC_MSG_RESULT([SPECIFIED_CFLAGS   : ${SPECIFIED_CFLAGS}])\nAC_MSG_RESULT([EXTRA_CFLAGS       : ${EXTRA_CFLAGS}])\nAC_MSG_RESULT([CPPFLAGS           : ${CPPFLAGS}])\nAC_MSG_RESULT([CXX                : ${CXX}])\nAC_MSG_RESULT([CONFIGURE_CXXFLAGS : ${CONFIGURE_CXXFLAGS}])\nAC_MSG_RESULT([SPECIFIED_CXXFLAGS : ${SPECIFIED_CXXFLAGS}])\nAC_MSG_RESULT([EXTRA_CXXFLAGS     : ${EXTRA_CXXFLAGS}])\nAC_MSG_RESULT([LDFLAGS            : ${LDFLAGS}])\nAC_MSG_RESULT([EXTRA_LDFLAGS      : ${EXTRA_LDFLAGS}])\nAC_MSG_RESULT([DSO_LDFLAGS        : ${DSO_LDFLAGS}])\nAC_MSG_RESULT([LIBS               : ${LIBS}])\nAC_MSG_RESULT([RPATH_EXTRA        : ${RPATH_EXTRA}])\nAC_MSG_RESULT([])\nAC_MSG_RESULT([XSLTPROC           : ${XSLTPROC}])\nAC_MSG_RESULT([XSLROOT            : ${XSLROOT}])\nAC_MSG_RESULT([])\nAC_MSG_RESULT([PREFIX             : ${PREFIX}])\nAC_MSG_RESULT([BINDIR             : ${BINDIR}])\nAC_MSG_RESULT([DATADIR            : ${DATADIR}])\nAC_MSG_RESULT([INCLUDEDIR         : ${INCLUDEDIR}])\nAC_MSG_RESULT([LIBDIR             : ${LIBDIR}])\nAC_MSG_RESULT([MANDIR             : ${MANDIR}])\nAC_MSG_RESULT([])\nAC_MSG_RESULT([srcroot            : ${srcroot}])\nAC_MSG_RESULT([abs_srcroot        : ${abs_srcroot}])\nAC_MSG_RESULT([objroot            : ${objroot}])\nAC_MSG_RESULT([abs_objroot        : ${abs_objroot}])\nAC_MSG_RESULT([])\nAC_MSG_RESULT([JEMALLOC_PREFIX    : ${JEMALLOC_PREFIX}])\nAC_MSG_RESULT([JEMALLOC_PRIVATE_NAMESPACE])\nAC_MSG_RESULT([                   : ${JEMALLOC_PRIVATE_NAMESPACE}])\nAC_MSG_RESULT([install_suffix     : ${install_suffix}])\nAC_MSG_RESULT([malloc_conf        : ${config_malloc_conf}])\nAC_MSG_RESULT([autogen            : ${enable_autogen}])\nAC_MSG_RESULT([debug              : ${enable_debug}])\nAC_MSG_RESULT([stats              : ${enable_stats}])\nAC_MSG_RESULT([prof               : ${enable_prof}])\nAC_MSG_RESULT([prof-libunwind     : ${enable_prof_libunwind}])\nAC_MSG_RESULT([prof-libgcc        : ${enable_prof_libgcc}])\nAC_MSG_RESULT([prof-gcc           : ${enable_prof_gcc}])\nAC_MSG_RESULT([thp                : ${enable_thp}])\nAC_MSG_RESULT([fill               : ${enable_fill}])\nAC_MSG_RESULT([utrace             : ${enable_utrace}])\nAC_MSG_RESULT([xmalloc            : ${enable_xmalloc}])\nAC_MSG_RESULT([lazy_lock          : ${enable_lazy_lock}])\nAC_MSG_RESULT([cache-oblivious    : ${enable_cache_oblivious}])\nAC_MSG_RESULT([cxx                : ${enable_cxx}])\nAC_MSG_RESULT([===============================================================================])\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/doc/html.xsl.in",
    "content": "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">\n  <xsl:import href=\"@XSLROOT@/html/docbook.xsl\"/>\n  <xsl:import href=\"@abs_srcroot@doc/stylesheet.xsl\"/>\n  <xsl:output method=\"xml\" encoding=\"utf-8\"/>\n</xsl:stylesheet>\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/doc/jemalloc.xml.in",
    "content": "<?xml version='1.0' encoding='UTF-8'?>\n<?xml-stylesheet type=\"text/xsl\"\n        href=\"http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl\"?>\n<!DOCTYPE refentry PUBLIC \"-//OASIS//DTD DocBook XML V4.4//EN\"\n        \"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd\" [\n]>\n\n<refentry>\n  <refentryinfo>\n    <title>User Manual</title>\n    <productname>jemalloc</productname>\n    <releaseinfo role=\"version\">@jemalloc_version@</releaseinfo>\n    <authorgroup>\n      <author>\n        <firstname>Jason</firstname>\n        <surname>Evans</surname>\n        <personblurb>Author</personblurb>\n      </author>\n    </authorgroup>\n  </refentryinfo>\n  <refmeta>\n    <refentrytitle>JEMALLOC</refentrytitle>\n    <manvolnum>3</manvolnum>\n  </refmeta>\n  <refnamediv>\n    <refdescriptor>jemalloc</refdescriptor>\n    <refname>jemalloc</refname>\n    <!-- Each refname causes a man page file to be created.  Only if this were\n         the system malloc(3) implementation would these files be appropriate.\n    <refname>malloc</refname>\n    <refname>calloc</refname>\n    <refname>posix_memalign</refname>\n    <refname>aligned_alloc</refname>\n    <refname>realloc</refname>\n    <refname>free</refname>\n    <refname>mallocx</refname>\n    <refname>rallocx</refname>\n    <refname>xallocx</refname>\n    <refname>sallocx</refname>\n    <refname>dallocx</refname>\n    <refname>sdallocx</refname>\n    <refname>nallocx</refname>\n    <refname>mallctl</refname>\n    <refname>mallctlnametomib</refname>\n    <refname>mallctlbymib</refname>\n    <refname>malloc_stats_print</refname>\n    <refname>malloc_usable_size</refname>\n    -->\n    <refpurpose>general purpose memory allocation functions</refpurpose>\n  </refnamediv>\n  <refsect1 id=\"library\">\n    <title>LIBRARY</title>\n    <para>This manual describes jemalloc @jemalloc_version@.  More information\n    can be found at the <ulink\n    url=\"http://jemalloc.net/\">jemalloc website</ulink>.</para>\n  </refsect1>\n  <refsynopsisdiv>\n    <title>SYNOPSIS</title>\n    <funcsynopsis>\n      <funcsynopsisinfo>#include &lt;<filename class=\"headerfile\">jemalloc/jemalloc.h</filename>&gt;</funcsynopsisinfo>\n      <refsect2>\n        <title>Standard API</title>\n        <funcprototype>\n          <funcdef>void *<function>malloc</function></funcdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>void *<function>calloc</function></funcdef>\n          <paramdef>size_t <parameter>number</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>int <function>posix_memalign</function></funcdef>\n          <paramdef>void **<parameter>ptr</parameter></paramdef>\n          <paramdef>size_t <parameter>alignment</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>void *<function>aligned_alloc</function></funcdef>\n          <paramdef>size_t <parameter>alignment</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>void *<function>realloc</function></funcdef>\n          <paramdef>void *<parameter>ptr</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>void <function>free</function></funcdef>\n          <paramdef>void *<parameter>ptr</parameter></paramdef>\n        </funcprototype>\n      </refsect2>\n      <refsect2>\n        <title>Non-standard API</title>\n        <funcprototype>\n          <funcdef>void *<function>mallocx</function></funcdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n          <paramdef>int <parameter>flags</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>void *<function>rallocx</function></funcdef>\n          <paramdef>void *<parameter>ptr</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n          <paramdef>int <parameter>flags</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>size_t <function>xallocx</function></funcdef>\n          <paramdef>void *<parameter>ptr</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n          <paramdef>size_t <parameter>extra</parameter></paramdef>\n          <paramdef>int <parameter>flags</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>size_t <function>sallocx</function></funcdef>\n          <paramdef>void *<parameter>ptr</parameter></paramdef>\n          <paramdef>int <parameter>flags</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>void <function>dallocx</function></funcdef>\n          <paramdef>void *<parameter>ptr</parameter></paramdef>\n          <paramdef>int <parameter>flags</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>void <function>sdallocx</function></funcdef>\n          <paramdef>void *<parameter>ptr</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n          <paramdef>int <parameter>flags</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>size_t <function>nallocx</function></funcdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n          <paramdef>int <parameter>flags</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>int <function>mallctl</function></funcdef>\n          <paramdef>const char *<parameter>name</parameter></paramdef>\n          <paramdef>void *<parameter>oldp</parameter></paramdef>\n          <paramdef>size_t *<parameter>oldlenp</parameter></paramdef>\n          <paramdef>void *<parameter>newp</parameter></paramdef>\n          <paramdef>size_t <parameter>newlen</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>int <function>mallctlnametomib</function></funcdef>\n          <paramdef>const char *<parameter>name</parameter></paramdef>\n          <paramdef>size_t *<parameter>mibp</parameter></paramdef>\n          <paramdef>size_t *<parameter>miblenp</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>int <function>mallctlbymib</function></funcdef>\n          <paramdef>const size_t *<parameter>mib</parameter></paramdef>\n          <paramdef>size_t <parameter>miblen</parameter></paramdef>\n          <paramdef>void *<parameter>oldp</parameter></paramdef>\n          <paramdef>size_t *<parameter>oldlenp</parameter></paramdef>\n          <paramdef>void *<parameter>newp</parameter></paramdef>\n          <paramdef>size_t <parameter>newlen</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>void <function>malloc_stats_print</function></funcdef>\n          <paramdef>void <parameter>(*write_cb)</parameter>\n            <funcparams>void *, const char *</funcparams>\n          </paramdef>\n          <paramdef>void *<parameter>cbopaque</parameter></paramdef>\n          <paramdef>const char *<parameter>opts</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>size_t <function>malloc_usable_size</function></funcdef>\n          <paramdef>const void *<parameter>ptr</parameter></paramdef>\n        </funcprototype>\n        <funcprototype>\n          <funcdef>void <function>(*malloc_message)</function></funcdef>\n          <paramdef>void *<parameter>cbopaque</parameter></paramdef>\n          <paramdef>const char *<parameter>s</parameter></paramdef>\n        </funcprototype>\n        <para><type>const char *</type><varname>malloc_conf</varname>;</para>\n      </refsect2>\n    </funcsynopsis>\n  </refsynopsisdiv>\n  <refsect1 id=\"description\">\n    <title>DESCRIPTION</title>\n    <refsect2>\n      <title>Standard API</title>\n\n      <para>The <function>malloc()</function> function allocates\n      <parameter>size</parameter> bytes of uninitialized memory.  The allocated\n      space is suitably aligned (after possible pointer coercion) for storage\n      of any type of object.</para>\n\n      <para>The <function>calloc()</function> function allocates\n      space for <parameter>number</parameter> objects, each\n      <parameter>size</parameter> bytes in length.  The result is identical to\n      calling <function>malloc()</function> with an argument of\n      <parameter>number</parameter> * <parameter>size</parameter>, with the\n      exception that the allocated memory is explicitly initialized to zero\n      bytes.</para>\n\n      <para>The <function>posix_memalign()</function> function\n      allocates <parameter>size</parameter> bytes of memory such that the\n      allocation's base address is a multiple of\n      <parameter>alignment</parameter>, and returns the allocation in the value\n      pointed to by <parameter>ptr</parameter>.  The requested\n      <parameter>alignment</parameter> must be a power of 2 at least as large as\n      <code language=\"C\">sizeof(<type>void *</type>)</code>.</para>\n\n      <para>The <function>aligned_alloc()</function> function\n      allocates <parameter>size</parameter> bytes of memory such that the\n      allocation's base address is a multiple of\n      <parameter>alignment</parameter>.  The requested\n      <parameter>alignment</parameter> must be a power of 2.  Behavior is\n      undefined if <parameter>size</parameter> is not an integral multiple of\n      <parameter>alignment</parameter>.</para>\n\n      <para>The <function>realloc()</function> function changes the\n      size of the previously allocated memory referenced by\n      <parameter>ptr</parameter> to <parameter>size</parameter> bytes.  The\n      contents of the memory are unchanged up to the lesser of the new and old\n      sizes.  If the new size is larger, the contents of the newly allocated\n      portion of the memory are undefined.  Upon success, the memory referenced\n      by <parameter>ptr</parameter> is freed and a pointer to the newly\n      allocated memory is returned.  Note that\n      <function>realloc()</function> may move the memory allocation,\n      resulting in a different return value than <parameter>ptr</parameter>.\n      If <parameter>ptr</parameter> is <constant>NULL</constant>, the\n      <function>realloc()</function> function behaves identically to\n      <function>malloc()</function> for the specified size.</para>\n\n      <para>The <function>free()</function> function causes the\n      allocated memory referenced by <parameter>ptr</parameter> to be made\n      available for future allocations.  If <parameter>ptr</parameter> is\n      <constant>NULL</constant>, no action occurs.</para>\n    </refsect2>\n    <refsect2>\n      <title>Non-standard API</title>\n      <para>The <function>mallocx()</function>,\n      <function>rallocx()</function>,\n      <function>xallocx()</function>,\n      <function>sallocx()</function>,\n      <function>dallocx()</function>,\n      <function>sdallocx()</function>, and\n      <function>nallocx()</function> functions all have a\n      <parameter>flags</parameter> argument that can be used to specify\n      options.  The functions only check the options that are contextually\n      relevant.  Use bitwise or (<code language=\"C\">|</code>) operations to\n      specify one or more of the following:\n        <variablelist>\n          <varlistentry id=\"MALLOCX_LG_ALIGN\">\n            <term><constant>MALLOCX_LG_ALIGN(<parameter>la</parameter>)\n            </constant></term>\n\n            <listitem><para>Align the memory allocation to start at an address\n            that is a multiple of <code language=\"C\">(1 &lt;&lt;\n            <parameter>la</parameter>)</code>.  This macro does not validate\n            that <parameter>la</parameter> is within the valid\n            range.</para></listitem>\n          </varlistentry>\n          <varlistentry id=\"MALLOCX_ALIGN\">\n            <term><constant>MALLOCX_ALIGN(<parameter>a</parameter>)\n            </constant></term>\n\n            <listitem><para>Align the memory allocation to start at an address\n            that is a multiple of <parameter>a</parameter>, where\n            <parameter>a</parameter> is a power of two.  This macro does not\n            validate that <parameter>a</parameter> is a power of 2.\n            </para></listitem>\n          </varlistentry>\n          <varlistentry id=\"MALLOCX_ZERO\">\n            <term><constant>MALLOCX_ZERO</constant></term>\n\n            <listitem><para>Initialize newly allocated memory to contain zero\n            bytes.  In the growing reallocation case, the real size prior to\n            reallocation defines the boundary between untouched bytes and those\n            that are initialized to contain zero bytes.  If this macro is\n            absent, newly allocated memory is uninitialized.</para></listitem>\n          </varlistentry>\n          <varlistentry id=\"MALLOCX_TCACHE\">\n            <term><constant>MALLOCX_TCACHE(<parameter>tc</parameter>)\n            </constant></term>\n\n            <listitem><para>Use the thread-specific cache (tcache) specified by\n            the identifier <parameter>tc</parameter>, which must have been\n            acquired via the <link\n            linkend=\"tcache.create\"><mallctl>tcache.create</mallctl></link>\n            mallctl.  This macro does not validate that\n            <parameter>tc</parameter> specifies a valid\n            identifier.</para></listitem>\n          </varlistentry>\n          <varlistentry id=\"MALLOC_TCACHE_NONE\">\n            <term><constant>MALLOCX_TCACHE_NONE</constant></term>\n\n            <listitem><para>Do not use a thread-specific cache (tcache).  Unless\n            <constant>MALLOCX_TCACHE(<parameter>tc</parameter>)</constant> or\n            <constant>MALLOCX_TCACHE_NONE</constant> is specified, an\n            automatically managed tcache will be used under many circumstances.\n            This macro cannot be used in the same <parameter>flags</parameter>\n            argument as\n            <constant>MALLOCX_TCACHE(<parameter>tc</parameter>)</constant>.</para></listitem>\n          </varlistentry>\n          <varlistentry id=\"MALLOCX_ARENA\">\n            <term><constant>MALLOCX_ARENA(<parameter>a</parameter>)\n            </constant></term>\n\n            <listitem><para>Use the arena specified by the index\n            <parameter>a</parameter>.  This macro has no effect for regions that\n            were allocated via an arena other than the one specified.  This\n            macro does not validate that <parameter>a</parameter> specifies an\n            arena index in the valid range.</para></listitem>\n          </varlistentry>\n        </variablelist>\n      </para>\n\n      <para>The <function>mallocx()</function> function allocates at\n      least <parameter>size</parameter> bytes of memory, and returns a pointer\n      to the base address of the allocation.  Behavior is undefined if\n      <parameter>size</parameter> is <constant>0</constant>.</para>\n\n      <para>The <function>rallocx()</function> function resizes the\n      allocation at <parameter>ptr</parameter> to be at least\n      <parameter>size</parameter> bytes, and returns a pointer to the base\n      address of the resulting allocation, which may or may not have moved from\n      its original location.  Behavior is undefined if\n      <parameter>size</parameter> is <constant>0</constant>.</para>\n\n      <para>The <function>xallocx()</function> function resizes the\n      allocation at <parameter>ptr</parameter> in place to be at least\n      <parameter>size</parameter> bytes, and returns the real size of the\n      allocation.  If <parameter>extra</parameter> is non-zero, an attempt is\n      made to resize the allocation to be at least <code\n      language=\"C\">(<parameter>size</parameter> +\n      <parameter>extra</parameter>)</code> bytes, though inability to allocate\n      the extra byte(s) will not by itself result in failure to resize.\n      Behavior is undefined if <parameter>size</parameter> is\n      <constant>0</constant>, or if <code\n      language=\"C\">(<parameter>size</parameter> + <parameter>extra</parameter>\n      &gt; <constant>SIZE_T_MAX</constant>)</code>.</para>\n\n      <para>The <function>sallocx()</function> function returns the\n      real size of the allocation at <parameter>ptr</parameter>.</para>\n\n      <para>The <function>dallocx()</function> function causes the\n      memory referenced by <parameter>ptr</parameter> to be made available for\n      future allocations.</para>\n\n      <para>The <function>sdallocx()</function> function is an\n      extension of <function>dallocx()</function> with a\n      <parameter>size</parameter> parameter to allow the caller to pass in the\n      allocation size as an optimization.  The minimum valid input size is the\n      original requested size of the allocation, and the maximum valid input\n      size is the corresponding value returned by\n      <function>nallocx()</function> or\n      <function>sallocx()</function>.</para>\n\n      <para>The <function>nallocx()</function> function allocates no\n      memory, but it performs the same size computation as the\n      <function>mallocx()</function> function, and returns the real\n      size of the allocation that would result from the equivalent\n      <function>mallocx()</function> function call, or\n      <constant>0</constant> if the inputs exceed the maximum supported size\n      class and/or alignment.  Behavior is undefined if\n      <parameter>size</parameter> is <constant>0</constant>.</para>\n\n      <para>The <function>mallctl()</function> function provides a\n      general interface for introspecting the memory allocator, as well as\n      setting modifiable parameters and triggering actions.  The\n      period-separated <parameter>name</parameter> argument specifies a\n      location in a tree-structured namespace; see the <xref\n      linkend=\"mallctl_namespace\" xrefstyle=\"template:%t\"/> section for\n      documentation on the tree contents.  To read a value, pass a pointer via\n      <parameter>oldp</parameter> to adequate space to contain the value, and a\n      pointer to its length via <parameter>oldlenp</parameter>; otherwise pass\n      <constant>NULL</constant> and <constant>NULL</constant>.  Similarly, to\n      write a value, pass a pointer to the value via\n      <parameter>newp</parameter>, and its length via\n      <parameter>newlen</parameter>; otherwise pass <constant>NULL</constant>\n      and <constant>0</constant>.</para>\n\n      <para>The <function>mallctlnametomib()</function> function\n      provides a way to avoid repeated name lookups for applications that\n      repeatedly query the same portion of the namespace, by translating a name\n      to a <quote>Management Information Base</quote> (MIB) that can be passed\n      repeatedly to <function>mallctlbymib()</function>.  Upon\n      successful return from <function>mallctlnametomib()</function>,\n      <parameter>mibp</parameter> contains an array of\n      <parameter>*miblenp</parameter> integers, where\n      <parameter>*miblenp</parameter> is the lesser of the number of components\n      in <parameter>name</parameter> and the input value of\n      <parameter>*miblenp</parameter>.  Thus it is possible to pass a\n      <parameter>*miblenp</parameter> that is smaller than the number of\n      period-separated name components, which results in a partial MIB that can\n      be used as the basis for constructing a complete MIB.  For name\n      components that are integers (e.g. the 2 in\n      <link\n      linkend=\"arenas.bin.i.size\"><mallctl>arenas.bin.2.size</mallctl></link>),\n      the corresponding MIB component will always be that integer.  Therefore,\n      it is legitimate to construct code like the following: <programlisting\n      language=\"C\"><![CDATA[\nunsigned nbins, i;\nsize_t mib[4];\nsize_t len, miblen;\n\nlen = sizeof(nbins);\nmallctl(\"arenas.nbins\", &nbins, &len, NULL, 0);\n\nmiblen = 4;\nmallctlnametomib(\"arenas.bin.0.size\", mib, &miblen);\nfor (i = 0; i < nbins; i++) {\n\tsize_t bin_size;\n\n\tmib[2] = i;\n\tlen = sizeof(bin_size);\n\tmallctlbymib(mib, miblen, (void *)&bin_size, &len, NULL, 0);\n\t/* Do something with bin_size... */\n}]]></programlisting></para>\n\n      <varlistentry id=\"malloc_stats_print_opts\">\n      </varlistentry>\n      <para>The <function>malloc_stats_print()</function> function writes\n      summary statistics via the <parameter>write_cb</parameter> callback\n      function pointer and <parameter>cbopaque</parameter> data passed to\n      <parameter>write_cb</parameter>, or <function>malloc_message()</function>\n      if <parameter>write_cb</parameter> is <constant>NULL</constant>.  The\n      statistics are presented in human-readable form unless <quote>J</quote> is\n      specified as a character within the <parameter>opts</parameter> string, in\n      which case the statistics are presented in <ulink\n      url=\"http://www.json.org/\">JSON format</ulink>.  This function can be\n      called repeatedly.  General information that never changes during\n      execution can be omitted by specifying <quote>g</quote> as a character\n      within the <parameter>opts</parameter> string.  Note that\n      <function>malloc_message()</function> uses the\n      <function>mallctl*()</function> functions internally, so inconsistent\n      statistics can be reported if multiple threads use these functions\n      simultaneously.  If <option>--enable-stats</option> is specified during\n      configuration, <quote>m</quote>, <quote>d</quote>, and <quote>a</quote>\n      can be specified to omit merged arena, destroyed merged arena, and per\n      arena statistics, respectively; <quote>b</quote> and <quote>l</quote> can\n      be specified to omit per size class statistics for bins and large objects,\n      respectively; <quote>x</quote> can be specified to omit all mutex\n      statistics.  Unrecognized characters are silently ignored.  Note that\n      thread caching may prevent some statistics from being completely up to\n      date, since extra locking would be required to merge counters that track\n      thread cache operations.</para>\n\n      <para>The <function>malloc_usable_size()</function> function\n      returns the usable size of the allocation pointed to by\n      <parameter>ptr</parameter>.  The return value may be larger than the size\n      that was requested during allocation.  The\n      <function>malloc_usable_size()</function> function is not a\n      mechanism for in-place <function>realloc()</function>; rather\n      it is provided solely as a tool for introspection purposes.  Any\n      discrepancy between the requested allocation size and the size reported\n      by <function>malloc_usable_size()</function> should not be\n      depended on, since such behavior is entirely implementation-dependent.\n      </para>\n    </refsect2>\n  </refsect1>\n  <refsect1 id=\"tuning\">\n    <title>TUNING</title>\n    <para>Once, when the first call is made to one of the memory allocation\n    routines, the allocator initializes its internals based in part on various\n    options that can be specified at compile- or run-time.</para>\n\n    <para>The string specified via <option>--with-malloc-conf</option>, the\n    string pointed to by the global variable <varname>malloc_conf</varname>, the\n    <quote>name</quote> of the file referenced by the symbolic link named\n    <filename class=\"symlink\">/etc/malloc.conf</filename>, and the value of the\n    environment variable <envar>MALLOC_CONF</envar>, will be interpreted, in\n    that order, from left to right as options.  Note that\n    <varname>malloc_conf</varname> may be read before\n    <function>main()</function> is entered, so the declaration of\n    <varname>malloc_conf</varname> should specify an initializer that contains\n    the final value to be read by jemalloc.  <option>--with-malloc-conf</option>\n    and <varname>malloc_conf</varname> are compile-time mechanisms, whereas\n    <filename class=\"symlink\">/etc/malloc.conf</filename> and\n    <envar>MALLOC_CONF</envar> can be safely set any time prior to program\n    invocation.</para>\n\n    <para>An options string is a comma-separated list of option:value pairs.\n    There is one key corresponding to each <link\n    linkend=\"opt.abort\"><mallctl>opt.*</mallctl></link> mallctl (see the <xref\n    linkend=\"mallctl_namespace\" xrefstyle=\"template:%t\"/> section for options\n    documentation).  For example, <literal>abort:true,narenas:1</literal> sets\n    the <link linkend=\"opt.abort\"><mallctl>opt.abort</mallctl></link> and <link\n    linkend=\"opt.narenas\"><mallctl>opt.narenas</mallctl></link> options.  Some\n    options have boolean values (true/false), others have integer values (base\n    8, 10, or 16, depending on prefix), and yet others have raw string\n    values.</para>\n  </refsect1>\n  <refsect1 id=\"implementation_notes\">\n    <title>IMPLEMENTATION NOTES</title>\n    <para>Traditionally, allocators have used\n    <citerefentry><refentrytitle>sbrk</refentrytitle>\n    <manvolnum>2</manvolnum></citerefentry> to obtain memory, which is\n    suboptimal for several reasons, including race conditions, increased\n    fragmentation, and artificial limitations on maximum usable memory.  If\n    <citerefentry><refentrytitle>sbrk</refentrytitle>\n    <manvolnum>2</manvolnum></citerefentry> is supported by the operating\n    system, this allocator uses both\n    <citerefentry><refentrytitle>mmap</refentrytitle>\n    <manvolnum>2</manvolnum></citerefentry> and\n    <citerefentry><refentrytitle>sbrk</refentrytitle>\n    <manvolnum>2</manvolnum></citerefentry>, in that order of preference;\n    otherwise only <citerefentry><refentrytitle>mmap</refentrytitle>\n    <manvolnum>2</manvolnum></citerefentry> is used.</para>\n\n    <para>This allocator uses multiple arenas in order to reduce lock\n    contention for threaded programs on multi-processor systems.  This works\n    well with regard to threading scalability, but incurs some costs.  There is\n    a small fixed per-arena overhead, and additionally, arenas manage memory\n    completely independently of each other, which means a small fixed increase\n    in overall memory fragmentation.  These overheads are not generally an\n    issue, given the number of arenas normally used.  Note that using\n    substantially more arenas than the default is not likely to improve\n    performance, mainly due to reduced cache performance.  However, it may make\n    sense to reduce the number of arenas if an application does not make much\n    use of the allocation functions.</para>\n\n    <para>In addition to multiple arenas, this allocator supports\n    thread-specific caching, in order to make it possible to completely avoid\n    synchronization for most allocation requests.  Such caching allows very fast\n    allocation in the common case, but it increases memory usage and\n    fragmentation, since a bounded number of objects can remain allocated in\n    each thread cache.</para>\n\n    <para>Memory is conceptually broken into extents.  Extents are always\n    aligned to multiples of the page size.  This alignment makes it possible to\n    find metadata for user objects quickly.  User objects are broken into two\n    categories according to size: small and large.  Contiguous small objects\n    comprise a slab, which resides within a single extent, whereas large objects\n    each have their own extents backing them.</para>\n\n    <para>Small objects are managed in groups by slabs.  Each slab maintains\n    a bitmap to track which regions are in use.  Allocation requests that are no\n    more than half the quantum (8 or 16, depending on architecture) are rounded\n    up to the nearest power of two that is at least <code\n    language=\"C\">sizeof(<type>double</type>)</code>.  All other object size\n    classes are multiples of the quantum, spaced such that there are four size\n    classes for each doubling in size, which limits internal fragmentation to\n    approximately 20% for all but the smallest size classes.  Small size classes\n    are smaller than four times the page size, and large size classes extend\n    from four times the page size up to the largest size class that does not\n    exceed <constant>PTRDIFF_MAX</constant>.</para>\n\n    <para>Allocations are packed tightly together, which can be an issue for\n    multi-threaded applications.  If you need to assure that allocations do not\n    suffer from cacheline sharing, round your allocation requests up to the\n    nearest multiple of the cacheline size, or specify cacheline alignment when\n    allocating.</para>\n\n    <para>The <function>realloc()</function>,\n    <function>rallocx()</function>, and\n    <function>xallocx()</function> functions may resize allocations\n    without moving them under limited circumstances.  Unlike the\n    <function>*allocx()</function> API, the standard API does not\n    officially round up the usable size of an allocation to the nearest size\n    class, so technically it is necessary to call\n    <function>realloc()</function> to grow e.g. a 9-byte allocation to\n    16 bytes, or shrink a 16-byte allocation to 9 bytes.  Growth and shrinkage\n    trivially succeeds in place as long as the pre-size and post-size both round\n    up to the same size class.  No other API guarantees are made regarding\n    in-place resizing, but the current implementation also tries to resize large\n    allocations in place, as long as the pre-size and post-size are both large.\n    For shrinkage to succeed, the extent allocator must support splitting (see\n    <link\n    linkend=\"arena.i.extent_hooks\"><mallctl>arena.&lt;i&gt;.extent_hooks</mallctl></link>).\n    Growth only succeeds if the trailing memory is currently available, and the\n    extent allocator supports merging.</para>\n\n    <para>Assuming 4 KiB pages and a 16-byte quantum on a 64-bit system, the\n    size classes in each category are as shown in <xref linkend=\"size_classes\"\n    xrefstyle=\"template:Table %n\"/>.</para>\n\n    <table xml:id=\"size_classes\" frame=\"all\">\n      <title>Size classes</title>\n      <tgroup cols=\"3\" colsep=\"1\" rowsep=\"1\">\n      <colspec colname=\"c1\" align=\"left\"/>\n      <colspec colname=\"c2\" align=\"right\"/>\n      <colspec colname=\"c3\" align=\"left\"/>\n      <thead>\n        <row>\n          <entry>Category</entry>\n          <entry>Spacing</entry>\n          <entry>Size</entry>\n        </row>\n      </thead>\n      <tbody>\n        <row>\n          <entry morerows=\"8\">Small</entry>\n          <entry>lg</entry>\n          <entry>[8]</entry>\n        </row>\n        <row>\n          <entry>16</entry>\n          <entry>[16, 32, 48, 64, 80, 96, 112, 128]</entry>\n        </row>\n        <row>\n          <entry>32</entry>\n          <entry>[160, 192, 224, 256]</entry>\n        </row>\n        <row>\n          <entry>64</entry>\n          <entry>[320, 384, 448, 512]</entry>\n        </row>\n        <row>\n          <entry>128</entry>\n          <entry>[640, 768, 896, 1024]</entry>\n        </row>\n        <row>\n          <entry>256</entry>\n          <entry>[1280, 1536, 1792, 2048]</entry>\n        </row>\n        <row>\n          <entry>512</entry>\n          <entry>[2560, 3072, 3584, 4096]</entry>\n        </row>\n        <row>\n          <entry>1 KiB</entry>\n          <entry>[5 KiB, 6 KiB, 7 KiB, 8 KiB]</entry>\n        </row>\n        <row>\n          <entry>2 KiB</entry>\n          <entry>[10 KiB, 12 KiB, 14 KiB]</entry>\n        </row>\n        <row>\n          <entry morerows=\"15\">Large</entry>\n          <entry>2 KiB</entry>\n          <entry>[16 KiB]</entry>\n        </row>\n        <row>\n          <entry>4 KiB</entry>\n          <entry>[20 KiB, 24 KiB, 28 KiB, 32 KiB]</entry>\n        </row>\n        <row>\n          <entry>8 KiB</entry>\n          <entry>[40 KiB, 48 KiB, 54 KiB, 64 KiB]</entry>\n        </row>\n        <row>\n          <entry>16 KiB</entry>\n          <entry>[80 KiB, 96 KiB, 112 KiB, 128 KiB]</entry>\n        </row>\n        <row>\n          <entry>32 KiB</entry>\n          <entry>[160 KiB, 192 KiB, 224 KiB, 256 KiB]</entry>\n        </row>\n        <row>\n          <entry>64 KiB</entry>\n          <entry>[320 KiB, 384 KiB, 448 KiB, 512 KiB]</entry>\n        </row>\n        <row>\n          <entry>128 KiB</entry>\n          <entry>[640 KiB, 768 KiB, 896 KiB, 1 MiB]</entry>\n        </row>\n        <row>\n          <entry>256 KiB</entry>\n          <entry>[1280 KiB, 1536 KiB, 1792 KiB, 2 MiB]</entry>\n        </row>\n        <row>\n          <entry>512 KiB</entry>\n          <entry>[2560 KiB, 3 MiB, 3584 KiB, 4 MiB]</entry>\n        </row>\n        <row>\n          <entry>1 MiB</entry>\n          <entry>[5 MiB, 6 MiB, 7 MiB, 8 MiB]</entry>\n        </row>\n        <row>\n          <entry>2 MiB</entry>\n          <entry>[10 MiB, 12 MiB, 14 MiB, 16 MiB]</entry>\n        </row>\n        <row>\n          <entry>4 MiB</entry>\n          <entry>[20 MiB, 24 MiB, 28 MiB, 32 MiB]</entry>\n        </row>\n        <row>\n          <entry>8 MiB</entry>\n          <entry>[40 MiB, 48 MiB, 56 MiB, 64 MiB]</entry>\n        </row>\n        <row>\n          <entry>...</entry>\n          <entry>...</entry>\n        </row>\n        <row>\n          <entry>512 PiB</entry>\n          <entry>[2560 PiB, 3 EiB, 3584 PiB, 4 EiB]</entry>\n        </row>\n        <row>\n          <entry>1 EiB</entry>\n          <entry>[5 EiB, 6 EiB, 7 EiB]</entry>\n        </row>\n      </tbody>\n      </tgroup>\n    </table>\n  </refsect1>\n  <refsect1 id=\"mallctl_namespace\">\n    <title>MALLCTL NAMESPACE</title>\n    <para>The following names are defined in the namespace accessible via the\n    <function>mallctl*()</function> functions.  Value types are specified in\n    parentheses, their readable/writable statuses are encoded as\n    <literal>rw</literal>, <literal>r-</literal>, <literal>-w</literal>, or\n    <literal>--</literal>, and required build configuration flags follow, if\n    any.  A name element encoded as <literal>&lt;i&gt;</literal> or\n    <literal>&lt;j&gt;</literal> indicates an integer component, where the\n    integer varies from 0 to some upper value that must be determined via\n    introspection.  In the case of <mallctl>stats.arenas.&lt;i&gt;.*</mallctl>\n    and <mallctl>arena.&lt;i&gt;.{initialized,purge,decay,dss}</mallctl>,\n    <literal>&lt;i&gt;</literal> equal to\n    <constant>MALLCTL_ARENAS_ALL</constant> can be used to operate on all arenas\n    or access the summation of statistics from all arenas; similarly\n    <literal>&lt;i&gt;</literal> equal to\n    <constant>MALLCTL_ARENAS_DESTROYED</constant> can be used to access the\n    summation of statistics from all destroyed arenas.  These constants can be\n    utilized either via <function>mallctlnametomib()</function> followed by\n    <function>mallctlbymib()</function>, or via code such as the following:\n    <programlisting language=\"C\"><![CDATA[\n#define STRINGIFY_HELPER(x) #x\n#define STRINGIFY(x) STRINGIFY_HELPER(x)\n\nmallctl(\"arena.\" STRINGIFY(MALLCTL_ARENAS_ALL) \".decay\",\n    NULL, NULL, NULL, 0);]]></programlisting>\n    Take special note of the <link\n    linkend=\"epoch\"><mallctl>epoch</mallctl></link> mallctl, which controls\n    refreshing of cached dynamic statistics.</para>\n\n    <variablelist>\n      <varlistentry id=\"version\">\n        <term>\n          <mallctl>version</mallctl>\n          (<type>const char *</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Return the jemalloc version string.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"epoch\">\n        <term>\n          <mallctl>epoch</mallctl>\n          (<type>uint64_t</type>)\n          <literal>rw</literal>\n        </term>\n        <listitem><para>If a value is passed in, refresh the data from which\n        the <function>mallctl*()</function> functions report values,\n        and increment the epoch.  Return the current epoch.  This is useful for\n        detecting whether another thread caused a refresh.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"background_thread\">\n        <term>\n          <mallctl>background_thread</mallctl>\n          (<type>bool</type>)\n          <literal>rw</literal>\n        </term>\n        <listitem><para>Enable/disable internal background worker threads.  When\n        set to true, background threads are created on demand (the number of\n        background threads will be no more than the number of CPUs or active\n        arenas).  Threads run periodically, and handle <link\n        linkend=\"arena.i.decay\">purging</link> asynchronously.  When switching\n        off, background threads are terminated synchronously.  Note that after\n        <citerefentry><refentrytitle>fork</refentrytitle><manvolnum>2</manvolnum></citerefentry>\n        function, the state in the child process will be disabled regardless\n        the state in parent process. See <link\n        linkend=\"stats.background_thread.num_threads\"><mallctl>stats.background_thread</mallctl></link>\n        for related stats.  <link\n        linkend=\"opt.background_thread\"><mallctl>opt.background_thread</mallctl></link>\n        can be used to set the default option.  This option is only available on\n        selected pthread-based platforms.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"config.cache_oblivious\">\n        <term>\n          <mallctl>config.cache_oblivious</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para><option>--enable-cache-oblivious</option> was specified\n        during build configuration.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"config.debug\">\n        <term>\n          <mallctl>config.debug</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para><option>--enable-debug</option> was specified during\n        build configuration.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"config.fill\">\n        <term>\n          <mallctl>config.fill</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para><option>--enable-fill</option> was specified during\n        build configuration.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"config.lazy_lock\">\n        <term>\n          <mallctl>config.lazy_lock</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para><option>--enable-lazy-lock</option> was specified\n        during build configuration.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"config.malloc_conf\">\n        <term>\n          <mallctl>config.malloc_conf</mallctl>\n          (<type>const char *</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Embedded configure-time-specified run-time options\n        string, empty unless <option>--with-malloc-conf</option> was specified\n        during build configuration.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"config.prof\">\n        <term>\n          <mallctl>config.prof</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para><option>--enable-prof</option> was specified during\n        build configuration.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"config.prof_libgcc\">\n        <term>\n          <mallctl>config.prof_libgcc</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para><option>--disable-prof-libgcc</option> was not\n        specified during build configuration.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"config.prof_libunwind\">\n        <term>\n          <mallctl>config.prof_libunwind</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para><option>--enable-prof-libunwind</option> was specified\n        during build configuration.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"config.stats\">\n        <term>\n          <mallctl>config.stats</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para><option>--enable-stats</option> was specified during\n        build configuration.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"config.thp\">\n        <term>\n          <mallctl>config.thp</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para><option>--disable-thp</option> was not specified\n        during build configuration, and the system supports transparent huge\n        page manipulation.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"config.utrace\">\n        <term>\n          <mallctl>config.utrace</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para><option>--enable-utrace</option> was specified during\n        build configuration.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"config.xmalloc\">\n        <term>\n          <mallctl>config.xmalloc</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para><option>--enable-xmalloc</option> was specified during\n        build configuration.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.abort\">\n        <term>\n          <mallctl>opt.abort</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Abort-on-warning enabled/disabled.  If true, most\n        warnings are fatal.  Note that runtime option warnings are not included\n        (see <link\n        linkend=\"opt.abort_conf\"><mallctl>opt.abort_conf</mallctl></link> for\n        that). The process will call\n        <citerefentry><refentrytitle>abort</refentrytitle>\n        <manvolnum>3</manvolnum></citerefentry> in these cases.  This option is\n        disabled by default unless <option>--enable-debug</option> is\n        specified during configuration, in which case it is enabled by default.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.abort_conf\">\n        <term>\n          <mallctl>opt.abort_conf</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Abort-on-invalid-configuration enabled/disabled.  If\n        true, invalid runtime options are fatal.  The process will call\n        <citerefentry><refentrytitle>abort</refentrytitle>\n        <manvolnum>3</manvolnum></citerefentry> in these cases.  This option is\n        disabled by default unless <option>--enable-debug</option> is\n        specified during configuration, in which case it is enabled by default.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.retain\">\n        <term>\n          <mallctl>opt.retain</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>If true, retain unused virtual memory for later reuse\n        rather than discarding it by calling\n        <citerefentry><refentrytitle>munmap</refentrytitle>\n        <manvolnum>2</manvolnum></citerefentry> or equivalent (see <link\n        linkend=\"stats.retained\">stats.retained</link> for related details).\n        This option is disabled by default unless discarding virtual memory is\n        known to trigger\n        platform-specific performance problems, e.g. for [64-bit] Linux, which\n        has a quirk in its virtual memory allocation algorithm that causes\n        semi-permanent VM map holes under normal jemalloc operation.  Although\n        <citerefentry><refentrytitle>munmap</refentrytitle>\n        <manvolnum>2</manvolnum></citerefentry> causes issues on 32-bit Linux as\n        well, retaining virtual memory for 32-bit Linux is disabled by default\n        due to the practical possibility of address space exhaustion.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.dss\">\n        <term>\n          <mallctl>opt.dss</mallctl>\n          (<type>const char *</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>dss (<citerefentry><refentrytitle>sbrk</refentrytitle>\n        <manvolnum>2</manvolnum></citerefentry>) allocation precedence as\n        related to <citerefentry><refentrytitle>mmap</refentrytitle>\n        <manvolnum>2</manvolnum></citerefentry> allocation.  The following\n        settings are supported if\n        <citerefentry><refentrytitle>sbrk</refentrytitle>\n        <manvolnum>2</manvolnum></citerefentry> is supported by the operating\n        system: <quote>disabled</quote>, <quote>primary</quote>, and\n        <quote>secondary</quote>; otherwise only <quote>disabled</quote> is\n        supported.  The default is <quote>secondary</quote> if\n        <citerefentry><refentrytitle>sbrk</refentrytitle>\n        <manvolnum>2</manvolnum></citerefentry> is supported by the operating\n        system; <quote>disabled</quote> otherwise.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.narenas\">\n        <term>\n          <mallctl>opt.narenas</mallctl>\n          (<type>unsigned</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Maximum number of arenas to use for automatic\n        multiplexing of threads and arenas.  The default is four times the\n        number of CPUs, or one if there is a single CPU.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.percpu_arena\">\n        <term>\n          <mallctl>opt.percpu_arena</mallctl>\n          (<type>const char *</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Per CPU arena mode.  Use the <quote>percpu</quote>\n        setting to enable this feature, which uses number of CPUs to determine\n        number of arenas, and bind threads to arenas dynamically based on the\n        CPU the thread runs on currently.  <quote>phycpu</quote> setting uses\n        one arena per physical CPU, which means the two hyper threads on the\n        same CPU share one arena.  Note that no runtime checking regarding the\n        availability of hyper threading is done at the moment.  When set to\n        <quote>disabled</quote>, narenas and thread to arena association will\n        not be impacted by this option.  The default is <quote>disabled</quote>.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.background_thread\">\n        <term>\n          <mallctl>opt.background_thread</mallctl>\n          (<type>const bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Internal background worker threads enabled/disabled. See\n        <link linkend=\"background_thread\">background_thread</link> for dynamic\n        control options and details.  This option is disabled by\n        default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.dirty_decay_ms\">\n        <term>\n          <mallctl>opt.dirty_decay_ms</mallctl>\n          (<type>ssize_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Approximate time in milliseconds from the creation of a\n        set of unused dirty pages until an equivalent set of unused dirty pages\n        is purged (i.e. converted to muzzy via e.g.\n        <function>madvise(<parameter>...</parameter><parameter><constant>MADV_FREE</constant></parameter>)</function>\n        if supported by the operating system, or converted to clean otherwise)\n        and/or reused.  Dirty pages are defined as previously having been\n        potentially written to by the application, and therefore consuming\n        physical memory, yet having no current use.  The pages are incrementally\n        purged according to a sigmoidal decay curve that starts and ends with\n        zero purge rate.  A decay time of 0 causes all unused dirty pages to be\n        purged immediately upon creation.  A decay time of -1 disables purging.\n        The default decay time is 10 seconds.  See <link\n        linkend=\"arenas.dirty_decay_ms\"><mallctl>arenas.dirty_decay_ms</mallctl></link>\n        and <link\n        linkend=\"arena.i.muzzy_decay_ms\"><mallctl>arena.&lt;i&gt;.muzzy_decay_ms</mallctl></link>\n        for related dynamic control options.  See <link\n        linkend=\"opt.muzzy_decay_ms\"><mallctl>opt.muzzy_decay_ms</mallctl></link>\n        for a description of muzzy pages.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.muzzy_decay_ms\">\n        <term>\n          <mallctl>opt.muzzy_decay_ms</mallctl>\n          (<type>ssize_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Approximate time in milliseconds from the creation of a\n        set of unused muzzy pages until an equivalent set of unused muzzy pages\n        is purged (i.e. converted to clean) and/or reused.  Muzzy pages are\n        defined as previously having been unused dirty pages that were\n        subsequently purged in a manner that left them subject to the\n        reclamation whims of the operating system (e.g.\n        <function>madvise(<parameter>...</parameter><parameter><constant>MADV_FREE</constant></parameter>)</function>),\n        and therefore in an indeterminate state.  The pages are incrementally\n        purged according to a sigmoidal decay curve that starts and ends with\n        zero purge rate.  A decay time of 0 causes all unused muzzy pages to be\n        purged immediately upon creation.  A decay time of -1 disables purging.\n        The default decay time is 10 seconds.  See <link\n        linkend=\"arenas.muzzy_decay_ms\"><mallctl>arenas.muzzy_decay_ms</mallctl></link>\n        and <link\n        linkend=\"arena.i.muzzy_decay_ms\"><mallctl>arena.&lt;i&gt;.muzzy_decay_ms</mallctl></link>\n        for related dynamic control options.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.stats_print\">\n        <term>\n          <mallctl>opt.stats_print</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Enable/disable statistics printing at exit.  If\n        enabled, the <function>malloc_stats_print()</function>\n        function is called at program exit via an\n        <citerefentry><refentrytitle>atexit</refentrytitle>\n        <manvolnum>3</manvolnum></citerefentry> function.  <link\n        linkend=\"opt.stats_print_opts\"><mallctl>opt.stats_print_opts</mallctl></link>\n        can be combined to specify output options. If\n        <option>--enable-stats</option> is specified during configuration, this\n        has the potential to cause deadlock for a multi-threaded process that\n        exits while one or more threads are executing in the memory allocation\n        functions.  Furthermore, <function>atexit()</function> may\n        allocate memory during application initialization and then deadlock\n        internally when jemalloc in turn calls\n        <function>atexit()</function>, so this option is not\n        universally usable (though the application can register its own\n        <function>atexit()</function> function with equivalent\n        functionality).  Therefore, this option should only be used with care;\n        it is primarily intended as a performance tuning aid during application\n        development.  This option is disabled by default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.stats_print_opts\">\n        <term>\n          <mallctl>opt.stats_print_opts</mallctl>\n          (<type>const char *</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Options (the <parameter>opts</parameter> string) to pass\n        to the <function>malloc_stats_print()</function> at exit (enabled\n        through <link\n        linkend=\"opt.stats_print\"><mallctl>opt.stats_print</mallctl></link>). See\n        available options in <link\n        linkend=\"malloc_stats_print_opts\"><function>malloc_stats_print()</function></link>.\n        Has no effect unless <link\n        linkend=\"opt.stats_print\"><mallctl>opt.stats_print</mallctl></link> is\n        enabled.  The default is <quote></quote>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.junk\">\n        <term>\n          <mallctl>opt.junk</mallctl>\n          (<type>const char *</type>)\n          <literal>r-</literal>\n          [<option>--enable-fill</option>]\n        </term>\n        <listitem><para>Junk filling.  If set to <quote>alloc</quote>, each byte\n        of uninitialized allocated memory will be initialized to\n        <literal>0xa5</literal>.  If set to <quote>free</quote>, all deallocated\n        memory will be initialized to <literal>0x5a</literal>.  If set to\n        <quote>true</quote>, both allocated and deallocated memory will be\n        initialized, and if set to <quote>false</quote>, junk filling be\n        disabled entirely.  This is intended for debugging and will impact\n        performance negatively.  This option is <quote>false</quote> by default\n        unless <option>--enable-debug</option> is specified during\n        configuration, in which case it is <quote>true</quote> by\n        default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.zero\">\n        <term>\n          <mallctl>opt.zero</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n          [<option>--enable-fill</option>]\n        </term>\n        <listitem><para>Zero filling enabled/disabled.  If enabled, each byte\n        of uninitialized allocated memory will be initialized to 0.  Note that\n        this initialization only happens once for each byte, so\n        <function>realloc()</function> and\n        <function>rallocx()</function> calls do not zero memory that\n        was previously allocated.  This is intended for debugging and will\n        impact performance negatively.  This option is disabled by default.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.utrace\">\n        <term>\n          <mallctl>opt.utrace</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n          [<option>--enable-utrace</option>]\n        </term>\n        <listitem><para>Allocation tracing based on\n        <citerefentry><refentrytitle>utrace</refentrytitle>\n        <manvolnum>2</manvolnum></citerefentry> enabled/disabled.  This option\n        is disabled by default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.xmalloc\">\n        <term>\n          <mallctl>opt.xmalloc</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n          [<option>--enable-xmalloc</option>]\n        </term>\n        <listitem><para>Abort-on-out-of-memory enabled/disabled.  If enabled,\n        rather than returning failure for any allocation function, display a\n        diagnostic message on <constant>STDERR_FILENO</constant> and cause the\n        program to drop core (using\n        <citerefentry><refentrytitle>abort</refentrytitle>\n        <manvolnum>3</manvolnum></citerefentry>).  If an application is\n        designed to depend on this behavior, set the option at compile time by\n        including the following in the source code:\n        <programlisting language=\"C\"><![CDATA[\nmalloc_conf = \"xmalloc:true\";]]></programlisting>\n        This option is disabled by default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.tcache\">\n        <term>\n          <mallctl>opt.tcache</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Thread-specific caching (tcache) enabled/disabled.  When\n        there are multiple threads, each thread uses a tcache for objects up to\n        a certain size.  Thread-specific caching allows many allocations to be\n        satisfied without performing any thread synchronization, at the cost of\n        increased memory use.  See the <link\n        linkend=\"opt.lg_tcache_max\"><mallctl>opt.lg_tcache_max</mallctl></link>\n        option for related tuning information.  This option is enabled by\n        default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.lg_tcache_max\">\n        <term>\n          <mallctl>opt.lg_tcache_max</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Maximum size class (log base 2) to cache in the\n        thread-specific cache (tcache).  At a minimum, all small size classes\n        are cached, and at a maximum all large size classes are cached.  The\n        default maximum is 32 KiB (2^15).</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.prof\">\n        <term>\n          <mallctl>opt.prof</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Memory profiling enabled/disabled.  If enabled, profile\n        memory allocation activity.  See the <link\n        linkend=\"opt.prof_active\"><mallctl>opt.prof_active</mallctl></link>\n        option for on-the-fly activation/deactivation.  See the <link\n        linkend=\"opt.lg_prof_sample\"><mallctl>opt.lg_prof_sample</mallctl></link>\n        option for probabilistic sampling control.  See the <link\n        linkend=\"opt.prof_accum\"><mallctl>opt.prof_accum</mallctl></link>\n        option for control of cumulative sample reporting.  See the <link\n        linkend=\"opt.lg_prof_interval\"><mallctl>opt.lg_prof_interval</mallctl></link>\n        option for information on interval-triggered profile dumping, the <link\n        linkend=\"opt.prof_gdump\"><mallctl>opt.prof_gdump</mallctl></link>\n        option for information on high-water-triggered profile dumping, and the\n        <link linkend=\"opt.prof_final\"><mallctl>opt.prof_final</mallctl></link>\n        option for final profile dumping.  Profile output is compatible with\n        the <command>jeprof</command> command, which is based on the\n        <command>pprof</command> that is developed as part of the <ulink\n        url=\"http://code.google.com/p/gperftools/\">gperftools\n        package</ulink>.  See <link linkend=\"heap_profile_format\">HEAP PROFILE\n        FORMAT</link> for heap profile format documentation.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.prof_prefix\">\n        <term>\n          <mallctl>opt.prof_prefix</mallctl>\n          (<type>const char *</type>)\n          <literal>r-</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Filename prefix for profile dumps.  If the prefix is\n        set to the empty string, no automatic dumps will occur; this is\n        primarily useful for disabling the automatic final heap dump (which\n        also disables leak reporting, if enabled).  The default prefix is\n        <filename>jeprof</filename>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.prof_active\">\n        <term>\n          <mallctl>opt.prof_active</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Profiling activated/deactivated.  This is a secondary\n        control mechanism that makes it possible to start the application with\n        profiling enabled (see the <link\n        linkend=\"opt.prof\"><mallctl>opt.prof</mallctl></link> option) but\n        inactive, then toggle profiling at any time during program execution\n        with the <link\n        linkend=\"prof.active\"><mallctl>prof.active</mallctl></link> mallctl.\n        This option is enabled by default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.prof_thread_active_init\">\n        <term>\n          <mallctl>opt.prof_thread_active_init</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Initial setting for <link\n        linkend=\"thread.prof.active\"><mallctl>thread.prof.active</mallctl></link>\n        in newly created threads.  The initial setting for newly created threads\n        can also be changed during execution via the <link\n        linkend=\"prof.thread_active_init\"><mallctl>prof.thread_active_init</mallctl></link>\n        mallctl.  This option is enabled by default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.lg_prof_sample\">\n        <term>\n          <mallctl>opt.lg_prof_sample</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Average interval (log base 2) between allocation\n        samples, as measured in bytes of allocation activity.  Increasing the\n        sampling interval decreases profile fidelity, but also decreases the\n        computational overhead.  The default sample interval is 512 KiB (2^19\n        B).</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.prof_accum\">\n        <term>\n          <mallctl>opt.prof_accum</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Reporting of cumulative object/byte counts in profile\n        dumps enabled/disabled.  If this option is enabled, every unique\n        backtrace must be stored for the duration of execution.  Depending on\n        the application, this can impose a large memory overhead, and the\n        cumulative counts are not always of interest.  This option is disabled\n        by default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.lg_prof_interval\">\n        <term>\n          <mallctl>opt.lg_prof_interval</mallctl>\n          (<type>ssize_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Average interval (log base 2) between memory profile\n        dumps, as measured in bytes of allocation activity.  The actual\n        interval between dumps may be sporadic because decentralized allocation\n        counters are used to avoid synchronization bottlenecks.  Profiles are\n        dumped to files named according to the pattern\n        <filename>&lt;prefix&gt;.&lt;pid&gt;.&lt;seq&gt;.i&lt;iseq&gt;.heap</filename>,\n        where <literal>&lt;prefix&gt;</literal> is controlled by the\n        <link\n        linkend=\"opt.prof_prefix\"><mallctl>opt.prof_prefix</mallctl></link>\n        option.  By default, interval-triggered profile dumping is disabled\n        (encoded as -1).\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.prof_gdump\">\n        <term>\n          <mallctl>opt.prof_gdump</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Set the initial state of <link\n        linkend=\"prof.gdump\"><mallctl>prof.gdump</mallctl></link>, which when\n        enabled triggers a memory profile dump every time the total virtual\n        memory exceeds the previous maximum.  This option is disabled by\n        default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.prof_final\">\n        <term>\n          <mallctl>opt.prof_final</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Use an\n        <citerefentry><refentrytitle>atexit</refentrytitle>\n        <manvolnum>3</manvolnum></citerefentry> function to dump final memory\n        usage to a file named according to the pattern\n        <filename>&lt;prefix&gt;.&lt;pid&gt;.&lt;seq&gt;.f.heap</filename>,\n        where <literal>&lt;prefix&gt;</literal> is controlled by the <link\n        linkend=\"opt.prof_prefix\"><mallctl>opt.prof_prefix</mallctl></link>\n        option.  Note that <function>atexit()</function> may allocate\n        memory during application initialization and then deadlock internally\n        when jemalloc in turn calls <function>atexit()</function>, so\n        this option is not universally usable (though the application can\n        register its own <function>atexit()</function> function with\n        equivalent functionality).  This option is disabled by\n        default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"opt.prof_leak\">\n        <term>\n          <mallctl>opt.prof_leak</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Leak reporting enabled/disabled.  If enabled, use an\n        <citerefentry><refentrytitle>atexit</refentrytitle>\n        <manvolnum>3</manvolnum></citerefentry> function to report memory leaks\n        detected by allocation sampling.  See the\n        <link linkend=\"opt.prof\"><mallctl>opt.prof</mallctl></link> option for\n        information on analyzing heap profile output.  This option is disabled\n        by default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"thread.arena\">\n        <term>\n          <mallctl>thread.arena</mallctl>\n          (<type>unsigned</type>)\n          <literal>rw</literal>\n        </term>\n        <listitem><para>Get or set the arena associated with the calling\n        thread.  If the specified arena was not initialized beforehand (see the\n        <link\n        linkend=\"arena.i.initialized\"><mallctl>arena.i.initialized</mallctl></link>\n        mallctl), it will be automatically initialized as a side effect of\n        calling this interface.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"thread.allocated\">\n        <term>\n          <mallctl>thread.allocated</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Get the total number of bytes ever allocated by the\n        calling thread.  This counter has the potential to wrap around; it is\n        up to the application to appropriately interpret the counter in such\n        cases.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"thread.allocatedp\">\n        <term>\n          <mallctl>thread.allocatedp</mallctl>\n          (<type>uint64_t *</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Get a pointer to the the value that is returned by the\n        <link\n        linkend=\"thread.allocated\"><mallctl>thread.allocated</mallctl></link>\n        mallctl.  This is useful for avoiding the overhead of repeated\n        <function>mallctl*()</function> calls.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"thread.deallocated\">\n        <term>\n          <mallctl>thread.deallocated</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Get the total number of bytes ever deallocated by the\n        calling thread.  This counter has the potential to wrap around; it is\n        up to the application to appropriately interpret the counter in such\n        cases.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"thread.deallocatedp\">\n        <term>\n          <mallctl>thread.deallocatedp</mallctl>\n          (<type>uint64_t *</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Get a pointer to the the value that is returned by the\n        <link\n        linkend=\"thread.deallocated\"><mallctl>thread.deallocated</mallctl></link>\n        mallctl.  This is useful for avoiding the overhead of repeated\n        <function>mallctl*()</function> calls.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"thread.tcache.enabled\">\n        <term>\n          <mallctl>thread.tcache.enabled</mallctl>\n          (<type>bool</type>)\n          <literal>rw</literal>\n        </term>\n        <listitem><para>Enable/disable calling thread's tcache.  The tcache is\n        implicitly flushed as a side effect of becoming\n        disabled (see <link\n        linkend=\"thread.tcache.flush\"><mallctl>thread.tcache.flush</mallctl></link>).\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"thread.tcache.flush\">\n        <term>\n          <mallctl>thread.tcache.flush</mallctl>\n          (<type>void</type>)\n          <literal>--</literal>\n        </term>\n        <listitem><para>Flush calling thread's thread-specific cache (tcache).\n        This interface releases all cached objects and internal data structures\n        associated with the calling thread's tcache.  Ordinarily, this interface\n        need not be called, since automatic periodic incremental garbage\n        collection occurs, and the thread cache is automatically discarded when\n        a thread exits.  However, garbage collection is triggered by allocation\n        activity, so it is possible for a thread that stops\n        allocating/deallocating to retain its cache indefinitely, in which case\n        the developer may find manual flushing useful.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"thread.prof.name\">\n        <term>\n          <mallctl>thread.prof.name</mallctl>\n          (<type>const char *</type>)\n          <literal>r-</literal> or\n          <literal>-w</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Get/set the descriptive name associated with the calling\n        thread in memory profile dumps.  An internal copy of the name string is\n        created, so the input string need not be maintained after this interface\n        completes execution.  The output string of this interface should be\n        copied for non-ephemeral uses, because multiple implementation details\n        can cause asynchronous string deallocation.  Furthermore, each\n        invocation of this interface can only read or write; simultaneous\n        read/write is not supported due to string lifetime limitations.  The\n        name string must be nil-terminated and comprised only of characters in\n        the sets recognized\n        by <citerefentry><refentrytitle>isgraph</refentrytitle>\n        <manvolnum>3</manvolnum></citerefentry> and\n        <citerefentry><refentrytitle>isblank</refentrytitle>\n        <manvolnum>3</manvolnum></citerefentry>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"thread.prof.active\">\n        <term>\n          <mallctl>thread.prof.active</mallctl>\n          (<type>bool</type>)\n          <literal>rw</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Control whether sampling is currently active for the\n        calling thread.  This is an activation mechanism in addition to <link\n        linkend=\"prof.active\"><mallctl>prof.active</mallctl></link>; both must\n        be active for the calling thread to sample.  This flag is enabled by\n        default.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"tcache.create\">\n        <term>\n          <mallctl>tcache.create</mallctl>\n          (<type>unsigned</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Create an explicit thread-specific cache (tcache) and\n        return an identifier that can be passed to the <link\n        linkend=\"MALLOCX_TCACHE\"><constant>MALLOCX_TCACHE(<parameter>tc</parameter>)</constant></link>\n        macro to explicitly use the specified cache rather than the\n        automatically managed one that is used by default.  Each explicit cache\n        can be used by only one thread at a time; the application must assure\n        that this constraint holds.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"tcache.flush\">\n        <term>\n          <mallctl>tcache.flush</mallctl>\n          (<type>unsigned</type>)\n          <literal>-w</literal>\n        </term>\n        <listitem><para>Flush the specified thread-specific cache (tcache).  The\n        same considerations apply to this interface as to <link\n        linkend=\"thread.tcache.flush\"><mallctl>thread.tcache.flush</mallctl></link>,\n        except that the tcache will never be automatically discarded.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"tcache.destroy\">\n        <term>\n          <mallctl>tcache.destroy</mallctl>\n          (<type>unsigned</type>)\n          <literal>-w</literal>\n        </term>\n        <listitem><para>Flush the specified thread-specific cache (tcache) and\n        make the identifier available for use during a future tcache creation.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arena.i.initialized\">\n        <term>\n          <mallctl>arena.&lt;i&gt;.initialized</mallctl>\n          (<type>bool</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Get whether the specified arena's statistics are\n        initialized (i.e. the arena was initialized prior to the current epoch).\n        This interface can also be nominally used to query whether the merged\n        statistics corresponding to <constant>MALLCTL_ARENAS_ALL</constant> are\n        initialized (always true).</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arena.i.decay\">\n        <term>\n          <mallctl>arena.&lt;i&gt;.decay</mallctl>\n          (<type>void</type>)\n          <literal>--</literal>\n        </term>\n        <listitem><para>Trigger decay-based purging of unused dirty/muzzy pages\n        for arena &lt;i&gt;, or for all arenas if &lt;i&gt; equals\n        <constant>MALLCTL_ARENAS_ALL</constant>.  The proportion of unused\n        dirty/muzzy pages to be purged depends on the current time; see <link\n        linkend=\"opt.dirty_decay_ms\"><mallctl>opt.dirty_decay_ms</mallctl></link>\n        and <link\n        linkend=\"opt.muzzy_decay_ms\"><mallctl>opt.muzy_decay_ms</mallctl></link>\n        for details.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arena.i.purge\">\n        <term>\n          <mallctl>arena.&lt;i&gt;.purge</mallctl>\n          (<type>void</type>)\n          <literal>--</literal>\n        </term>\n        <listitem><para>Purge all unused dirty pages for arena &lt;i&gt;, or for\n        all arenas if &lt;i&gt; equals <constant>MALLCTL_ARENAS_ALL</constant>.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arena.i.reset\">\n        <term>\n          <mallctl>arena.&lt;i&gt;.reset</mallctl>\n          (<type>void</type>)\n          <literal>--</literal>\n        </term>\n        <listitem><para>Discard all of the arena's extant allocations.  This\n        interface can only be used with arenas explicitly created via <link\n        linkend=\"arenas.create\"><mallctl>arenas.create</mallctl></link>.  None\n        of the arena's discarded/cached allocations may accessed afterward.  As\n        part of this requirement, all thread caches which were used to\n        allocate/deallocate in conjunction with the arena must be flushed\n        beforehand.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arena.i.destroy\">\n        <term>\n          <mallctl>arena.&lt;i&gt;.destroy</mallctl>\n          (<type>void</type>)\n          <literal>--</literal>\n        </term>\n        <listitem><para>Destroy the arena.  Discard all of the arena's extant\n        allocations using the same mechanism as for <link\n        linkend=\"arena.i.reset\"><mallctl>arena.&lt;i&gt;.reset</mallctl></link>\n        (with all the same constraints and side effects), merge the arena stats\n        into those accessible at arena index\n        <constant>MALLCTL_ARENAS_DESTROYED</constant>, and then completely\n        discard all metadata associated with the arena.  Future calls to <link\n        linkend=\"arenas.create\"><mallctl>arenas.create</mallctl></link> may\n        recycle the arena index.  Destruction will fail if any threads are\n        currently associated with the arena as a result of calls to <link\n        linkend=\"thread.arena\"><mallctl>thread.arena</mallctl></link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arena.i.dss\">\n        <term>\n          <mallctl>arena.&lt;i&gt;.dss</mallctl>\n          (<type>const char *</type>)\n          <literal>rw</literal>\n        </term>\n        <listitem><para>Set the precedence of dss allocation as related to mmap\n        allocation for arena &lt;i&gt;, or for all arenas if &lt;i&gt; equals\n        <constant>MALLCTL_ARENAS_ALL</constant>.  See <link\n        linkend=\"opt.dss\"><mallctl>opt.dss</mallctl></link> for supported\n        settings.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arena.i.dirty_decay_ms\">\n        <term>\n          <mallctl>arena.&lt;i&gt;.dirty_decay_ms</mallctl>\n          (<type>ssize_t</type>)\n          <literal>rw</literal>\n        </term>\n        <listitem><para>Current per-arena approximate time in milliseconds from\n        the creation of a set of unused dirty pages until an equivalent set of\n        unused dirty pages is purged and/or reused.  Each time this interface is\n        set, all currently unused dirty pages are considered to have fully\n        decayed, which causes immediate purging of all unused dirty pages unless\n        the decay time is set to -1 (i.e. purging disabled).  See <link\n        linkend=\"opt.dirty_decay_ms\"><mallctl>opt.dirty_decay_ms</mallctl></link>\n        for additional information.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arena.i.muzzy_decay_ms\">\n        <term>\n          <mallctl>arena.&lt;i&gt;.muzzy_decay_ms</mallctl>\n          (<type>ssize_t</type>)\n          <literal>rw</literal>\n        </term>\n        <listitem><para>Current per-arena approximate time in milliseconds from\n        the creation of a set of unused muzzy pages until an equivalent set of\n        unused muzzy pages is purged and/or reused.  Each time this interface is\n        set, all currently unused muzzy pages are considered to have fully\n        decayed, which causes immediate purging of all unused muzzy pages unless\n        the decay time is set to -1 (i.e. purging disabled).  See <link\n        linkend=\"opt.muzzy_decay_ms\"><mallctl>opt.muzzy_decay_ms</mallctl></link>\n        for additional information.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arena.i.extent_hooks\">\n        <term>\n          <mallctl>arena.&lt;i&gt;.extent_hooks</mallctl>\n          (<type>extent_hooks_t *</type>)\n          <literal>rw</literal>\n        </term>\n        <listitem><para>Get or set the extent management hook functions for\n        arena &lt;i&gt;.  The functions must be capable of operating on all\n        extant extents associated with arena &lt;i&gt;, usually by passing\n        unknown extents to the replaced functions.  In practice, it is feasible\n        to control allocation for arenas explicitly created via <link\n        linkend=\"arenas.create\"><mallctl>arenas.create</mallctl></link> such\n        that all extents originate from an application-supplied extent allocator\n        (by specifying the custom extent hook functions during arena creation),\n        but the automatically created arenas will have already created extents\n        prior to the application having an opportunity to take over extent\n        allocation.</para>\n\n        <programlisting language=\"C\"><![CDATA[\ntypedef extent_hooks_s extent_hooks_t;\nstruct extent_hooks_s {\n\textent_alloc_t\t\t*alloc;\n\textent_dalloc_t\t\t*dalloc;\n\textent_destroy_t\t*destroy;\n\textent_commit_t\t\t*commit;\n\textent_decommit_t\t*decommit;\n\textent_purge_t\t\t*purge_lazy;\n\textent_purge_t\t\t*purge_forced;\n\textent_split_t\t\t*split;\n\textent_merge_t\t\t*merge;\n};]]></programlisting>\n        <para>The <type>extent_hooks_t</type> structure comprises function\n        pointers which are described individually below.  jemalloc uses these\n        functions to manage extent lifetime, which starts off with allocation of\n        mapped committed memory, in the simplest case followed by deallocation.\n        However, there are performance and platform reasons to retain extents\n        for later reuse.  Cleanup attempts cascade from deallocation to decommit\n        to forced purging to lazy purging, which gives the extent management\n        functions opportunities to reject the most permanent cleanup operations\n        in favor of less permanent (and often less costly) operations.  All\n        operations except allocation can be universally opted out of by setting\n        the hook pointers to <constant>NULL</constant>, or selectively opted out\n        of by returning failure.</para>\n\n        <funcsynopsis><funcprototype>\n          <funcdef>typedef void *<function>(extent_alloc_t)</function></funcdef>\n          <paramdef>extent_hooks_t *<parameter>extent_hooks</parameter></paramdef>\n          <paramdef>void *<parameter>new_addr</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n          <paramdef>size_t <parameter>alignment</parameter></paramdef>\n          <paramdef>bool *<parameter>zero</parameter></paramdef>\n          <paramdef>bool *<parameter>commit</parameter></paramdef>\n          <paramdef>unsigned <parameter>arena_ind</parameter></paramdef>\n        </funcprototype></funcsynopsis>\n        <literallayout></literallayout>\n        <para>An extent allocation function conforms to the\n        <type>extent_alloc_t</type> type and upon success returns a pointer to\n        <parameter>size</parameter> bytes of mapped memory on behalf of arena\n        <parameter>arena_ind</parameter> such that the extent's base address is\n        a multiple of <parameter>alignment</parameter>, as well as setting\n        <parameter>*zero</parameter> to indicate whether the extent is zeroed\n        and <parameter>*commit</parameter> to indicate whether the extent is\n        committed.  Upon error the function returns <constant>NULL</constant>\n        and leaves <parameter>*zero</parameter> and\n        <parameter>*commit</parameter> unmodified.  The\n        <parameter>size</parameter> parameter is always a multiple of the page\n        size.  The <parameter>alignment</parameter> parameter is always a power\n        of two at least as large as the page size.  Zeroing is mandatory if\n        <parameter>*zero</parameter> is true upon function entry.  Committing is\n        mandatory if <parameter>*commit</parameter> is true upon function entry.\n        If <parameter>new_addr</parameter> is not <constant>NULL</constant>, the\n        returned pointer must be <parameter>new_addr</parameter> on success or\n        <constant>NULL</constant> on error.  Committed memory may be committed\n        in absolute terms as on a system that does not overcommit, or in\n        implicit terms as on a system that overcommits and satisfies physical\n        memory needs on demand via soft page faults.  Note that replacing the\n        default extent allocation function makes the arena's <link\n        linkend=\"arena.i.dss\"><mallctl>arena.&lt;i&gt;.dss</mallctl></link>\n        setting irrelevant.</para>\n\n        <funcsynopsis><funcprototype>\n          <funcdef>typedef bool <function>(extent_dalloc_t)</function></funcdef>\n          <paramdef>extent_hooks_t *<parameter>extent_hooks</parameter></paramdef>\n          <paramdef>void *<parameter>addr</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n          <paramdef>bool <parameter>committed</parameter></paramdef>\n          <paramdef>unsigned <parameter>arena_ind</parameter></paramdef>\n        </funcprototype></funcsynopsis>\n        <literallayout></literallayout>\n        <para>\n        An extent deallocation function conforms to the\n        <type>extent_dalloc_t</type> type and deallocates an extent at given\n        <parameter>addr</parameter> and <parameter>size</parameter> with\n        <parameter>committed</parameter>/decommited memory as indicated, on\n        behalf of arena <parameter>arena_ind</parameter>, returning false upon\n        success.  If the function returns true, this indicates opt-out from\n        deallocation; the virtual memory mapping associated with the extent\n        remains mapped, in the same commit state, and available for future use,\n        in which case it will be automatically retained for later reuse.</para>\n\n        <funcsynopsis><funcprototype>\n          <funcdef>typedef void <function>(extent_destroy_t)</function></funcdef>\n          <paramdef>extent_hooks_t *<parameter>extent_hooks</parameter></paramdef>\n          <paramdef>void *<parameter>addr</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n          <paramdef>bool <parameter>committed</parameter></paramdef>\n          <paramdef>unsigned <parameter>arena_ind</parameter></paramdef>\n        </funcprototype></funcsynopsis>\n        <literallayout></literallayout>\n        <para>\n        An extent destruction function conforms to the\n        <type>extent_destroy_t</type> type and unconditionally destroys an\n        extent at given <parameter>addr</parameter> and\n        <parameter>size</parameter> with\n        <parameter>committed</parameter>/decommited memory as indicated, on\n        behalf of arena <parameter>arena_ind</parameter>.  This function may be\n        called to destroy retained extents during arena destruction (see <link\n        linkend=\"arena.i.destroy\"><mallctl>arena.&lt;i&gt;.destroy</mallctl></link>).</para>\n\n        <funcsynopsis><funcprototype>\n          <funcdef>typedef bool <function>(extent_commit_t)</function></funcdef>\n          <paramdef>extent_hooks_t *<parameter>extent_hooks</parameter></paramdef>\n          <paramdef>void *<parameter>addr</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n          <paramdef>size_t <parameter>offset</parameter></paramdef>\n          <paramdef>size_t <parameter>length</parameter></paramdef>\n          <paramdef>unsigned <parameter>arena_ind</parameter></paramdef>\n        </funcprototype></funcsynopsis>\n        <literallayout></literallayout>\n        <para>An extent commit function conforms to the\n        <type>extent_commit_t</type> type and commits zeroed physical memory to\n        back pages within an extent at given <parameter>addr</parameter> and\n        <parameter>size</parameter> at <parameter>offset</parameter> bytes,\n        extending for <parameter>length</parameter> on behalf of arena\n        <parameter>arena_ind</parameter>, returning false upon success.\n        Committed memory may be committed in absolute terms as on a system that\n        does not overcommit, or in implicit terms as on a system that\n        overcommits and satisfies physical memory needs on demand via soft page\n        faults. If the function returns true, this indicates insufficient\n        physical memory to satisfy the request.</para>\n\n        <funcsynopsis><funcprototype>\n          <funcdef>typedef bool <function>(extent_decommit_t)</function></funcdef>\n          <paramdef>extent_hooks_t *<parameter>extent_hooks</parameter></paramdef>\n          <paramdef>void *<parameter>addr</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n          <paramdef>size_t <parameter>offset</parameter></paramdef>\n          <paramdef>size_t <parameter>length</parameter></paramdef>\n          <paramdef>unsigned <parameter>arena_ind</parameter></paramdef>\n        </funcprototype></funcsynopsis>\n        <literallayout></literallayout>\n        <para>An extent decommit function conforms to the\n        <type>extent_decommit_t</type> type and decommits any physical memory\n        that is backing pages within an extent at given\n        <parameter>addr</parameter> and <parameter>size</parameter> at\n        <parameter>offset</parameter> bytes, extending for\n        <parameter>length</parameter> on behalf of arena\n        <parameter>arena_ind</parameter>, returning false upon success, in which\n        case the pages will be committed via the extent commit function before\n        being reused.  If the function returns true, this indicates opt-out from\n        decommit; the memory remains committed and available for future use, in\n        which case it will be automatically retained for later reuse.</para>\n\n        <funcsynopsis><funcprototype>\n          <funcdef>typedef bool <function>(extent_purge_t)</function></funcdef>\n          <paramdef>extent_hooks_t *<parameter>extent_hooks</parameter></paramdef>\n          <paramdef>void *<parameter>addr</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n          <paramdef>size_t <parameter>offset</parameter></paramdef>\n          <paramdef>size_t <parameter>length</parameter></paramdef>\n          <paramdef>unsigned <parameter>arena_ind</parameter></paramdef>\n        </funcprototype></funcsynopsis>\n        <literallayout></literallayout>\n        <para>An extent purge function conforms to the\n        <type>extent_purge_t</type> type and discards physical pages\n        within the virtual memory mapping associated with an extent at given\n        <parameter>addr</parameter> and <parameter>size</parameter> at\n        <parameter>offset</parameter> bytes, extending for\n        <parameter>length</parameter> on behalf of arena\n        <parameter>arena_ind</parameter>.  A lazy extent purge function (e.g.\n        implemented via\n        <function>madvise(<parameter>...</parameter><parameter><constant>MADV_FREE</constant></parameter>)</function>)\n        can delay purging indefinitely and leave the pages within the purged\n        virtual memory range in an indeterminite state, whereas a forced extent\n        purge function immediately purges, and the pages within the virtual\n        memory range will be zero-filled the next time they are accessed.  If\n        the function returns true, this indicates failure to purge.</para>\n\n        <funcsynopsis><funcprototype>\n          <funcdef>typedef bool <function>(extent_split_t)</function></funcdef>\n          <paramdef>extent_hooks_t *<parameter>extent_hooks</parameter></paramdef>\n          <paramdef>void *<parameter>addr</parameter></paramdef>\n          <paramdef>size_t <parameter>size</parameter></paramdef>\n          <paramdef>size_t <parameter>size_a</parameter></paramdef>\n          <paramdef>size_t <parameter>size_b</parameter></paramdef>\n          <paramdef>bool <parameter>committed</parameter></paramdef>\n          <paramdef>unsigned <parameter>arena_ind</parameter></paramdef>\n        </funcprototype></funcsynopsis>\n        <literallayout></literallayout>\n        <para>An extent split function conforms to the\n        <type>extent_split_t</type> type and optionally splits an extent at\n        given <parameter>addr</parameter> and <parameter>size</parameter> into\n        two adjacent extents, the first of <parameter>size_a</parameter> bytes,\n        and the second of <parameter>size_b</parameter> bytes, operating on\n        <parameter>committed</parameter>/decommitted memory as indicated, on\n        behalf of arena <parameter>arena_ind</parameter>, returning false upon\n        success.  If the function returns true, this indicates that the extent\n        remains unsplit and therefore should continue to be operated on as a\n        whole.</para>\n\n        <funcsynopsis><funcprototype>\n          <funcdef>typedef bool <function>(extent_merge_t)</function></funcdef>\n          <paramdef>extent_hooks_t *<parameter>extent_hooks</parameter></paramdef>\n          <paramdef>void *<parameter>addr_a</parameter></paramdef>\n          <paramdef>size_t <parameter>size_a</parameter></paramdef>\n          <paramdef>void *<parameter>addr_b</parameter></paramdef>\n          <paramdef>size_t <parameter>size_b</parameter></paramdef>\n          <paramdef>bool <parameter>committed</parameter></paramdef>\n          <paramdef>unsigned <parameter>arena_ind</parameter></paramdef>\n        </funcprototype></funcsynopsis>\n        <literallayout></literallayout>\n        <para>An extent merge function conforms to the\n        <type>extent_merge_t</type> type and optionally merges adjacent extents,\n        at given <parameter>addr_a</parameter> and <parameter>size_a</parameter>\n        with given <parameter>addr_b</parameter> and\n        <parameter>size_b</parameter> into one contiguous extent, operating on\n        <parameter>committed</parameter>/decommitted memory as indicated, on\n        behalf of arena <parameter>arena_ind</parameter>, returning false upon\n        success.  If the function returns true, this indicates that the extents\n        remain distinct mappings and therefore should continue to be operated on\n        independently.</para>\n        </listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.narenas\">\n        <term>\n          <mallctl>arenas.narenas</mallctl>\n          (<type>unsigned</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Current limit on number of arenas.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.dirty_decay_ms\">\n        <term>\n          <mallctl>arenas.dirty_decay_ms</mallctl>\n          (<type>ssize_t</type>)\n          <literal>rw</literal>\n        </term>\n        <listitem><para>Current default per-arena approximate time in\n        milliseconds from the creation of a set of unused dirty pages until an\n        equivalent set of unused dirty pages is purged and/or reused, used to\n        initialize <link\n        linkend=\"arena.i.dirty_decay_ms\"><mallctl>arena.&lt;i&gt;.dirty_decay_ms</mallctl></link>\n        during arena creation.  See <link\n        linkend=\"opt.dirty_decay_ms\"><mallctl>opt.dirty_decay_ms</mallctl></link>\n        for additional information.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.muzzy_decay_ms\">\n        <term>\n          <mallctl>arenas.muzzy_decay_ms</mallctl>\n          (<type>ssize_t</type>)\n          <literal>rw</literal>\n        </term>\n        <listitem><para>Current default per-arena approximate time in\n        milliseconds from the creation of a set of unused muzzy pages until an\n        equivalent set of unused muzzy pages is purged and/or reused, used to\n        initialize <link\n        linkend=\"arena.i.muzzy_decay_ms\"><mallctl>arena.&lt;i&gt;.muzzy_decay_ms</mallctl></link>\n        during arena creation.  See <link\n        linkend=\"opt.muzzy_decay_ms\"><mallctl>opt.muzzy_decay_ms</mallctl></link>\n        for additional information.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.quantum\">\n        <term>\n          <mallctl>arenas.quantum</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Quantum size.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.page\">\n        <term>\n          <mallctl>arenas.page</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Page size.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.tcache_max\">\n        <term>\n          <mallctl>arenas.tcache_max</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Maximum thread-cached size class.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.nbins\">\n        <term>\n          <mallctl>arenas.nbins</mallctl>\n          (<type>unsigned</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Number of bin size classes.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.nhbins\">\n        <term>\n          <mallctl>arenas.nhbins</mallctl>\n          (<type>unsigned</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Total number of thread cache bin size\n        classes.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.bin.i.size\">\n        <term>\n          <mallctl>arenas.bin.&lt;i&gt;.size</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Maximum size supported by size class.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.bin.i.nregs\">\n        <term>\n          <mallctl>arenas.bin.&lt;i&gt;.nregs</mallctl>\n          (<type>uint32_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Number of regions per slab.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.bin.i.slab_size\">\n        <term>\n          <mallctl>arenas.bin.&lt;i&gt;.slab_size</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Number of bytes per slab.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.nlextents\">\n        <term>\n          <mallctl>arenas.nlextents</mallctl>\n          (<type>unsigned</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Total number of large size classes.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.lextent.i.size\">\n        <term>\n          <mallctl>arenas.lextent.&lt;i&gt;.size</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Maximum size supported by this large size\n        class.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.create\">\n        <term>\n          <mallctl>arenas.create</mallctl>\n          (<type>unsigned</type>, <type>extent_hooks_t *</type>)\n          <literal>rw</literal>\n        </term>\n        <listitem><para>Explicitly create a new arena outside the range of\n        automatically managed arenas, with optionally specified extent hooks,\n        and return the new arena index.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"arenas.lookup\">\n        <term>\n          <mallctl>arenas.lookup</mallctl>\n            (<type>unsigned</type>, <type>void*</type>)\n            <literal>rw</literal>\n        </term>\n        <listitem><para>Index of the arena to which an allocation belongs to.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"prof.thread_active_init\">\n        <term>\n          <mallctl>prof.thread_active_init</mallctl>\n          (<type>bool</type>)\n          <literal>rw</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Control the initial setting for <link\n        linkend=\"thread.prof.active\"><mallctl>thread.prof.active</mallctl></link>\n        in newly created threads.  See the <link\n        linkend=\"opt.prof_thread_active_init\"><mallctl>opt.prof_thread_active_init</mallctl></link>\n        option for additional information.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"prof.active\">\n        <term>\n          <mallctl>prof.active</mallctl>\n          (<type>bool</type>)\n          <literal>rw</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Control whether sampling is currently active.  See the\n        <link\n        linkend=\"opt.prof_active\"><mallctl>opt.prof_active</mallctl></link>\n        option for additional information, as well as the interrelated <link\n        linkend=\"thread.prof.active\"><mallctl>thread.prof.active</mallctl></link>\n        mallctl.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"prof.dump\">\n        <term>\n          <mallctl>prof.dump</mallctl>\n          (<type>const char *</type>)\n          <literal>-w</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Dump a memory profile to the specified file, or if NULL\n        is specified, to a file according to the pattern\n        <filename>&lt;prefix&gt;.&lt;pid&gt;.&lt;seq&gt;.m&lt;mseq&gt;.heap</filename>,\n        where <literal>&lt;prefix&gt;</literal> is controlled by the\n        <link\n        linkend=\"opt.prof_prefix\"><mallctl>opt.prof_prefix</mallctl></link>\n        option.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"prof.gdump\">\n        <term>\n          <mallctl>prof.gdump</mallctl>\n          (<type>bool</type>)\n          <literal>rw</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>When enabled, trigger a memory profile dump every time\n        the total virtual memory exceeds the previous maximum.  Profiles are\n        dumped to files named according to the pattern\n        <filename>&lt;prefix&gt;.&lt;pid&gt;.&lt;seq&gt;.u&lt;useq&gt;.heap</filename>,\n        where <literal>&lt;prefix&gt;</literal> is controlled by the <link\n        linkend=\"opt.prof_prefix\"><mallctl>opt.prof_prefix</mallctl></link>\n        option.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"prof.reset\">\n        <term>\n          <mallctl>prof.reset</mallctl>\n          (<type>size_t</type>)\n          <literal>-w</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Reset all memory profile statistics, and optionally\n        update the sample rate (see <link\n        linkend=\"opt.lg_prof_sample\"><mallctl>opt.lg_prof_sample</mallctl></link>\n        and <link\n        linkend=\"prof.lg_sample\"><mallctl>prof.lg_sample</mallctl></link>).\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"prof.lg_sample\">\n        <term>\n          <mallctl>prof.lg_sample</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Get the current sample rate (see <link\n        linkend=\"opt.lg_prof_sample\"><mallctl>opt.lg_prof_sample</mallctl></link>).\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"prof.interval\">\n        <term>\n          <mallctl>prof.interval</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-prof</option>]\n        </term>\n        <listitem><para>Average number of bytes allocated between\n        interval-based profile dumps.  See the\n        <link\n        linkend=\"opt.lg_prof_interval\"><mallctl>opt.lg_prof_interval</mallctl></link>\n        option for additional information.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.allocated\">\n        <term>\n          <mallctl>stats.allocated</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Total number of bytes allocated by the\n        application.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.active\">\n        <term>\n          <mallctl>stats.active</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Total number of bytes in active pages allocated by the\n        application.  This is a multiple of the page size, and greater than or\n        equal to <link\n        linkend=\"stats.allocated\"><mallctl>stats.allocated</mallctl></link>.\n        This does not include <link linkend=\"stats.arenas.i.pdirty\">\n        <mallctl>stats.arenas.&lt;i&gt;.pdirty</mallctl></link>,\n        <link linkend=\"stats.arenas.i.pmuzzy\">\n        <mallctl>stats.arenas.&lt;i&gt;.pmuzzy</mallctl></link>, nor pages\n        entirely devoted to allocator metadata.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.metadata\">\n        <term>\n          <mallctl>stats.metadata</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Total number of bytes dedicated to metadata, which\n        comprise base allocations used for bootstrap-sensitive allocator\n        metadata structures (see <link\n        linkend=\"stats.arenas.i.base\"><mallctl>stats.arenas.&lt;i&gt;.base</mallctl></link>)\n        and internal allocations (see <link\n        linkend=\"stats.arenas.i.internal\"><mallctl>stats.arenas.&lt;i&gt;.internal</mallctl></link>).</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.resident\">\n        <term>\n          <mallctl>stats.resident</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Maximum number of bytes in physically resident data\n        pages mapped by the allocator, comprising all pages dedicated to\n        allocator metadata, pages backing active allocations, and unused dirty\n        pages.  This is a maximum rather than precise because pages may not\n        actually be physically resident if they correspond to demand-zeroed\n        virtual memory that has not yet been touched.  This is a multiple of the\n        page size, and is larger than <link\n        linkend=\"stats.active\"><mallctl>stats.active</mallctl></link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.mapped\">\n        <term>\n          <mallctl>stats.mapped</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Total number of bytes in active extents mapped by the\n        allocator.  This is larger than <link\n        linkend=\"stats.active\"><mallctl>stats.active</mallctl></link>.  This\n        does not include inactive extents, even those that contain unused dirty\n        pages, which means that there is no strict ordering between this and\n        <link\n        linkend=\"stats.resident\"><mallctl>stats.resident</mallctl></link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.retained\">\n        <term>\n          <mallctl>stats.retained</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Total number of bytes in virtual memory mappings that\n        were retained rather than being returned to the operating system via\n        e.g. <citerefentry><refentrytitle>munmap</refentrytitle>\n        <manvolnum>2</manvolnum></citerefentry> or similar.  Retained virtual\n        memory is typically untouched, decommitted, or purged, so it has no\n        strongly associated physical memory (see <link\n        linkend=\"arena.i.extent_hooks\">extent hooks</link> for details).\n        Retained memory is excluded from mapped memory statistics, e.g. <link\n        linkend=\"stats.mapped\"><mallctl>stats.mapped</mallctl></link>.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.background_thread.num_threads\">\n        <term>\n          <mallctl>stats.background_thread.num_threads</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para> Number of <link linkend=\"background_thread\">background\n        threads</link> running currently.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.background_thread.num_runs\">\n        <term>\n          <mallctl>stats.background_thread.num_runs</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para> Total number of runs from all <link\n        linkend=\"background_thread\">background threads</link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.background_thread.run_interval\">\n        <term>\n          <mallctl>stats.background_thread.run_interval</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para> Average run interval in nanoseconds of <link\n        linkend=\"background_thread\">background threads</link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.mutexes.ctl\">\n        <term>\n          <mallctl>stats.mutexes.ctl.{counter};</mallctl>\n          (<type>counter specific type</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Statistics on <varname>ctl</varname> mutex (global\n        scope; mallctl related).  <mallctl>{counter}</mallctl> is one of the\n        counters below:</para>\n        <varlistentry id=\"mutex_counters\">\n          <listitem><para><varname>num_ops</varname> (<type>uint64_t</type>):\n          Total number of lock acquisition operations on this mutex.</para>\n\n\t  <para><varname>num_spin_acq</varname> (<type>uint64_t</type>): Number\n\t  of times the mutex was spin-acquired.  When the mutex is currently\n\t  locked and cannot be acquired immediately, a short period of\n\t  spin-retry within jemalloc will be performed.  Acquired through spin\n\t  generally means the contention was lightweight and not causing context\n\t  switches.</para>\n\n\t  <para><varname>num_wait</varname> (<type>uint64_t</type>): Number of\n\t  times the mutex was wait-acquired, which means the mutex contention\n\t  was not solved by spin-retry, and blocking operation was likely\n\t  involved in order to acquire the mutex.  This event generally implies\n\t  higher cost / longer delay, and should be investigated if it happens\n\t  often.</para>\n\n\t  <para><varname>max_wait_time</varname> (<type>uint64_t</type>):\n\t  Maximum length of time in nanoseconds spent on a single wait-acquired\n\t  lock operation.  Note that to avoid profiling overhead on the common\n\t  path, this does not consider spin-acquired cases.</para>\n\n\t  <para><varname>total_wait_time</varname> (<type>uint64_t</type>):\n\t  Cumulative time in nanoseconds spent on wait-acquired lock operations.\n\t  Similarly, spin-acquired cases are not considered.</para>\n\n\t  <para><varname>max_num_thds</varname> (<type>uint32_t</type>): Maximum\n\t  number of threads waiting on this mutex simultaneously.  Similarly,\n\t  spin-acquired cases are not considered.</para>\n\n\t  <para><varname>num_owner_switch</varname> (<type>uint64_t</type>):\n\t  Number of times the current mutex owner is different from the previous\n\t  one.  This event does not generally imply an issue; rather it is an\n\t  indicator of how often the protected data are accessed by different\n\t  threads.\n\t  </para>\n\t  </listitem>\n\t</varlistentry>\n\t</listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.mutexes.background_thread\">\n        <term>\n          <mallctl>stats.mutexes.background_thread.{counter}</mallctl>\n\t  (<type>counter specific type</type>) <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Statistics on <varname>background_thread</varname> mutex\n        (global scope; <link\n        linkend=\"background_thread\"><mallctl>background_thread</mallctl></link>\n        related).  <mallctl>{counter}</mallctl> is one of the counters in <link\n        linkend=\"mutex_counters\">mutex profiling\n        counters</link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.mutexes.prof\">\n        <term>\n          <mallctl>stats.mutexes.prof.{counter}</mallctl>\n\t  (<type>counter specific type</type>) <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Statistics on <varname>prof</varname> mutex (global\n        scope; profiling related).  <mallctl>{counter}</mallctl> is one of the\n        counters in <link linkend=\"mutex_counters\">mutex profiling\n        counters</link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.mutexes.reset\">\n        <term>\n          <mallctl>stats.mutexes.reset</mallctl>\n\t  (<type>void</type>) <literal>--</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Reset all mutex profile statistics, including global\n        mutexes, arena mutexes and bin mutexes.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.dss\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.dss</mallctl>\n          (<type>const char *</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>dss (<citerefentry><refentrytitle>sbrk</refentrytitle>\n        <manvolnum>2</manvolnum></citerefentry>) allocation precedence as\n        related to <citerefentry><refentrytitle>mmap</refentrytitle>\n        <manvolnum>2</manvolnum></citerefentry> allocation.  See <link\n        linkend=\"opt.dss\"><mallctl>opt.dss</mallctl></link> for details.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.dirty_decay_ms\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.dirty_decay_ms</mallctl>\n          (<type>ssize_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Approximate time in milliseconds from the creation of a\n        set of unused dirty pages until an equivalent set of unused dirty pages\n        is purged and/or reused.  See <link\n        linkend=\"opt.dirty_decay_ms\"><mallctl>opt.dirty_decay_ms</mallctl></link>\n        for details.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.muzzy_decay_ms\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.muzzy_decay_ms</mallctl>\n          (<type>ssize_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Approximate time in milliseconds from the creation of a\n        set of unused muzzy pages until an equivalent set of unused muzzy pages\n        is purged and/or reused.  See <link\n        linkend=\"opt.muzzy_decay_ms\"><mallctl>opt.muzzy_decay_ms</mallctl></link>\n        for details.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.nthreads\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.nthreads</mallctl>\n          (<type>unsigned</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Number of threads currently assigned to\n        arena.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.uptime\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.uptime</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Time elapsed (in nanoseconds) since the arena was\n        created.  If &lt;i&gt; equals <constant>0</constant> or\n        <constant>MALLCTL_ARENAS_ALL</constant>, this is the uptime since malloc\n        initialization.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.pactive\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.pactive</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Number of pages in active extents.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.pdirty\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.pdirty</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Number of pages within unused extents that are\n        potentially dirty, and for which <function>madvise()</function> or\n        similar has not been called.  See <link\n        linkend=\"opt.dirty_decay_ms\"><mallctl>opt.dirty_decay_ms</mallctl></link>\n        for a description of dirty pages.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.pmuzzy\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.pmuzzy</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Number of pages within unused extents that are muzzy.\n        See <link\n        linkend=\"opt.muzzy_decay_ms\"><mallctl>opt.muzzy_decay_ms</mallctl></link>\n        for a description of muzzy pages.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.mapped\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.mapped</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Number of mapped bytes.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.retained\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.retained</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Number of retained bytes.  See <link\n        linkend=\"stats.retained\"><mallctl>stats.retained</mallctl></link> for\n        details.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.base\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.base</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>\n        Number of bytes dedicated to bootstrap-sensitive allocator metadata\n        structures.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.internal\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.internal</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Number of bytes dedicated to internal allocations.\n        Internal allocations differ from application-originated allocations in\n        that they are for internal use, and that they are omitted from heap\n        profiles.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.resident\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.resident</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Maximum number of bytes in physically resident data\n        pages mapped by the arena, comprising all pages dedicated to allocator\n        metadata, pages backing active allocations, and unused dirty pages.\n        This is a maximum rather than precise because pages may not actually be\n        physically resident if they correspond to demand-zeroed virtual memory\n        that has not yet been touched.  This is a multiple of the page\n        size.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.dirty_npurge\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.dirty_npurge</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Number of dirty page purge sweeps performed.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.dirty_nmadvise\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.dirty_nmadvise</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Number of <function>madvise()</function> or similar\n        calls made to purge dirty pages.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.dirty_purged\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.dirty_purged</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Number of dirty pages purged.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.muzzy_npurge\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.muzzy_npurge</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Number of muzzy page purge sweeps performed.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.muzzy_nmadvise\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.muzzy_nmadvise</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Number of <function>madvise()</function> or similar\n        calls made to purge muzzy pages.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.muzzy_purged\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.muzzy_purged</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Number of muzzy pages purged.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.small.allocated\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.small.allocated</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Number of bytes currently allocated by small objects.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.small.nmalloc\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.small.nmalloc</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of times a small allocation was\n        requested from the arena's bins, whether to fill the relevant tcache if\n        <link linkend=\"opt.tcache\"><mallctl>opt.tcache</mallctl></link> is\n        enabled, or to directly satisfy an allocation request\n        otherwise.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.small.ndalloc\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.small.ndalloc</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of times a small allocation was\n        returned to the arena's bins, whether to flush the relevant tcache if\n        <link linkend=\"opt.tcache\"><mallctl>opt.tcache</mallctl></link> is\n        enabled, or to directly deallocate an allocation\n        otherwise.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.small.nrequests\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.small.nrequests</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of allocation requests satisfied by\n        all bin size classes.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.large.allocated\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.large.allocated</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Number of bytes currently allocated by large objects.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.large.nmalloc\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.large.nmalloc</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of times a large extent was allocated\n        from the arena, whether to fill the relevant tcache if <link\n        linkend=\"opt.tcache\"><mallctl>opt.tcache</mallctl></link> is enabled and\n        the size class is within the range being cached, or to directly satisfy\n        an allocation request otherwise.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.large.ndalloc\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.large.ndalloc</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of times a large extent was returned\n        to the arena, whether to flush the relevant tcache if <link\n        linkend=\"opt.tcache\"><mallctl>opt.tcache</mallctl></link> is enabled and\n        the size class is within the range being cached, or to directly\n        deallocate an allocation otherwise.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.large.nrequests\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.large.nrequests</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of allocation requests satisfied by\n        all large size classes.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.bins.j.nmalloc\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.bins.&lt;j&gt;.nmalloc</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of times a bin region of the\n        corresponding size class was allocated from the arena, whether to fill\n        the relevant tcache if <link\n        linkend=\"opt.tcache\"><mallctl>opt.tcache</mallctl></link> is enabled, or\n        to directly satisfy an allocation request otherwise.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.bins.j.ndalloc\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.bins.&lt;j&gt;.ndalloc</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of times a bin region of the\n        corresponding size class was returned to the arena, whether to flush the\n        relevant tcache if <link\n        linkend=\"opt.tcache\"><mallctl>opt.tcache</mallctl></link> is enabled, or\n        to directly deallocate an allocation otherwise.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.bins.j.nrequests\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.bins.&lt;j&gt;.nrequests</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of allocation requests satisfied by\n        bin regions of the corresponding size class.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.bins.j.curregs\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.bins.&lt;j&gt;.curregs</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Current number of regions for this size\n        class.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.bins.j.nfills\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.bins.&lt;j&gt;.nfills</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Cumulative number of tcache fills.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.bins.j.nflushes\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.bins.&lt;j&gt;.nflushes</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n        </term>\n        <listitem><para>Cumulative number of tcache flushes.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.bins.j.nslabs\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.bins.&lt;j&gt;.nslabs</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of slabs created.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.bins.j.nreslabs\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.bins.&lt;j&gt;.nreslabs</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of times the current slab from which\n        to allocate changed.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.bins.j.curslabs\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.bins.&lt;j&gt;.curslabs</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Current number of slabs.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.bins.mutex\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.bins.&lt;j&gt;.mutex.{counter}</mallctl>\n          (<type>counter specific type</type>) <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Statistics on\n        <varname>arena.&lt;i&gt;.bins.&lt;j&gt;</varname> mutex (arena bin\n        scope; bin operation related).  <mallctl>{counter}</mallctl> is one of\n        the counters in <link linkend=\"mutex_counters\">mutex profiling\n        counters</link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.lextents.j.nmalloc\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.lextents.&lt;j&gt;.nmalloc</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of times a large extent of the\n        corresponding size class was allocated from the arena, whether to fill\n        the relevant tcache if <link\n        linkend=\"opt.tcache\"><mallctl>opt.tcache</mallctl></link> is enabled and\n        the size class is within the range being cached, or to directly satisfy\n        an allocation request otherwise.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.lextents.j.ndalloc\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.lextents.&lt;j&gt;.ndalloc</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of times a large extent of the\n        corresponding size class was returned to the arena, whether to flush the\n        relevant tcache if <link\n        linkend=\"opt.tcache\"><mallctl>opt.tcache</mallctl></link> is enabled and\n        the size class is within the range being cached, or to directly\n        deallocate an allocation otherwise.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.lextents.j.nrequests\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.lextents.&lt;j&gt;.nrequests</mallctl>\n          (<type>uint64_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Cumulative number of allocation requests satisfied by\n        large extents of the corresponding size class.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.lextents.j.curlextents\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.lextents.&lt;j&gt;.curlextents</mallctl>\n          (<type>size_t</type>)\n          <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Current number of large allocations for this size class.\n        </para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.mutexes.large\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.mutexes.large.{counter}</mallctl>\n          (<type>counter specific type</type>) <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Statistics on <varname>arena.&lt;i&gt;.large</varname>\n        mutex (arena scope; large allocation related).\n        <mallctl>{counter}</mallctl> is one of the counters in <link\n        linkend=\"mutex_counters\">mutex profiling\n        counters</link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.mutexes.extent_avail\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.mutexes.extent_avail.{counter}</mallctl>\n          (<type>counter specific type</type>) <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Statistics on <varname>arena.&lt;i&gt;.extent_avail\n        </varname> mutex (arena scope; extent avail related).\n        <mallctl>{counter}</mallctl> is one of the counters in <link\n        linkend=\"mutex_counters\">mutex profiling\n        counters</link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.mutexes.extents_dirty\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.mutexes.extents_dirty.{counter}</mallctl>\n          (<type>counter specific type</type>) <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Statistics on <varname>arena.&lt;i&gt;.extents_dirty\n        </varname> mutex (arena scope; dirty extents related).\n        <mallctl>{counter}</mallctl> is one of the counters in <link\n        linkend=\"mutex_counters\">mutex profiling\n        counters</link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.mutexes.extents_muzzy\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.mutexes.extents_muzzy.{counter}</mallctl>\n          (<type>counter specific type</type>) <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Statistics on <varname>arena.&lt;i&gt;.extents_muzzy\n        </varname> mutex (arena scope; muzzy extents related).\n        <mallctl>{counter}</mallctl> is one of the counters in <link\n        linkend=\"mutex_counters\">mutex profiling\n        counters</link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.mutexes.extents_retained\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.mutexes.extents_retained.{counter}</mallctl>\n          (<type>counter specific type</type>) <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Statistics on <varname>arena.&lt;i&gt;.extents_retained\n        </varname> mutex (arena scope; retained extents related).\n        <mallctl>{counter}</mallctl> is one of the counters in <link\n        linkend=\"mutex_counters\">mutex profiling\n        counters</link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.mutexes.decay_dirty\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.mutexes.decay_dirty.{counter}</mallctl>\n          (<type>counter specific type</type>) <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Statistics on <varname>arena.&lt;i&gt;.decay_dirty\n        </varname> mutex (arena scope; decay for dirty pages related).\n        <mallctl>{counter}</mallctl> is one of the counters in <link\n        linkend=\"mutex_counters\">mutex profiling\n        counters</link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.mutexes.decay_muzzy\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.mutexes.decay_muzzy.{counter}</mallctl>\n          (<type>counter specific type</type>) <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Statistics on <varname>arena.&lt;i&gt;.decay_muzzy\n        </varname> mutex (arena scope; decay for muzzy pages related).\n        <mallctl>{counter}</mallctl> is one of the counters in <link\n        linkend=\"mutex_counters\">mutex profiling\n        counters</link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.mutexes.base\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.mutexes.base.{counter}</mallctl>\n          (<type>counter specific type</type>) <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Statistics on <varname>arena.&lt;i&gt;.base</varname>\n        mutex (arena scope; base allocator related).\n        <mallctl>{counter}</mallctl> is one of the counters in <link\n        linkend=\"mutex_counters\">mutex profiling\n        counters</link>.</para></listitem>\n      </varlistentry>\n\n      <varlistentry id=\"stats.arenas.i.mutexes.tcache_list\">\n        <term>\n          <mallctl>stats.arenas.&lt;i&gt;.mutexes.tcache_list.{counter}</mallctl>\n          (<type>counter specific type</type>) <literal>r-</literal>\n          [<option>--enable-stats</option>]\n        </term>\n        <listitem><para>Statistics on\n        <varname>arena.&lt;i&gt;.tcache_list</varname> mutex (arena scope;\n        tcache to arena association related).  This mutex is expected to be\n        accessed less often.  <mallctl>{counter}</mallctl> is one of the\n        counters in <link linkend=\"mutex_counters\">mutex profiling\n        counters</link>.</para></listitem>\n      </varlistentry>\n\n    </variablelist>\n  </refsect1>\n  <refsect1 id=\"heap_profile_format\">\n    <title>HEAP PROFILE FORMAT</title>\n    <para>Although the heap profiling functionality was originally designed to\n    be compatible with the\n    <command>pprof</command> command that is developed as part of the <ulink\n    url=\"http://code.google.com/p/gperftools/\">gperftools\n    package</ulink>, the addition of per thread heap profiling functionality\n    required a different heap profile format.  The <command>jeprof</command>\n    command is derived from <command>pprof</command>, with enhancements to\n    support the heap profile format described here.</para>\n\n    <para>In the following hypothetical heap profile, <constant>[...]</constant>\n    indicates elision for the sake of compactness.  <programlisting><![CDATA[\nheap_v2/524288\n  t*: 28106: 56637512 [0: 0]\n  [...]\n  t3: 352: 16777344 [0: 0]\n  [...]\n  t99: 17754: 29341640 [0: 0]\n  [...]\n@ 0x5f86da8 0x5f5a1dc [...] 0x29e4d4e 0xa200316 0xabb2988 [...]\n  t*: 13: 6688 [0: 0]\n  t3: 12: 6496 [0: ]\n  t99: 1: 192 [0: 0]\n[...]\n\nMAPPED_LIBRARIES:\n[...]]]></programlisting> The following matches the above heap profile, but most\ntokens are replaced with <constant>&lt;description&gt;</constant> to indicate\ndescriptions of the corresponding fields.  <programlisting><![CDATA[\n<heap_profile_format_version>/<mean_sample_interval>\n  <aggregate>: <curobjs>: <curbytes> [<cumobjs>: <cumbytes>]\n  [...]\n  <thread_3_aggregate>: <curobjs>: <curbytes>[<cumobjs>: <cumbytes>]\n  [...]\n  <thread_99_aggregate>: <curobjs>: <curbytes>[<cumobjs>: <cumbytes>]\n  [...]\n@ <top_frame> <frame> [...] <frame> <frame> <frame> [...]\n  <backtrace_aggregate>: <curobjs>: <curbytes> [<cumobjs>: <cumbytes>]\n  <backtrace_thread_3>: <curobjs>: <curbytes> [<cumobjs>: <cumbytes>]\n  <backtrace_thread_99>: <curobjs>: <curbytes> [<cumobjs>: <cumbytes>]\n[...]\n\nMAPPED_LIBRARIES:\n</proc/<pid>/maps>]]></programlisting></para>\n  </refsect1>\n\n  <refsect1 id=\"debugging_malloc_problems\">\n    <title>DEBUGGING MALLOC PROBLEMS</title>\n    <para>When debugging, it is a good idea to configure/build jemalloc with\n    the <option>--enable-debug</option> and <option>--enable-fill</option>\n    options, and recompile the program with suitable options and symbols for\n    debugger support.  When so configured, jemalloc incorporates a wide variety\n    of run-time assertions that catch application errors such as double-free,\n    write-after-free, etc.</para>\n\n    <para>Programs often accidentally depend on <quote>uninitialized</quote>\n    memory actually being filled with zero bytes.  Junk filling\n    (see the <link linkend=\"opt.junk\"><mallctl>opt.junk</mallctl></link>\n    option) tends to expose such bugs in the form of obviously incorrect\n    results and/or coredumps.  Conversely, zero\n    filling (see the <link\n    linkend=\"opt.zero\"><mallctl>opt.zero</mallctl></link> option) eliminates\n    the symptoms of such bugs.  Between these two options, it is usually\n    possible to quickly detect, diagnose, and eliminate such bugs.</para>\n\n    <para>This implementation does not provide much detail about the problems\n    it detects, because the performance impact for storing such information\n    would be prohibitive.</para>\n  </refsect1>\n  <refsect1 id=\"diagnostic_messages\">\n    <title>DIAGNOSTIC MESSAGES</title>\n    <para>If any of the memory allocation/deallocation functions detect an\n    error or warning condition, a message will be printed to file descriptor\n    <constant>STDERR_FILENO</constant>.  Errors will result in the process\n    dumping core.  If the <link\n    linkend=\"opt.abort\"><mallctl>opt.abort</mallctl></link> option is set, most\n    warnings are treated as errors.</para>\n\n    <para>The <varname>malloc_message</varname> variable allows the programmer\n    to override the function which emits the text strings forming the errors\n    and warnings if for some reason the <constant>STDERR_FILENO</constant> file\n    descriptor is not suitable for this.\n    <function>malloc_message()</function> takes the\n    <parameter>cbopaque</parameter> pointer argument that is\n    <constant>NULL</constant> unless overridden by the arguments in a call to\n    <function>malloc_stats_print()</function>, followed by a string\n    pointer.  Please note that doing anything which tries to allocate memory in\n    this function is likely to result in a crash or deadlock.</para>\n\n    <para>All messages are prefixed by\n    <quote><computeroutput>&lt;jemalloc&gt;: </computeroutput></quote>.</para>\n  </refsect1>\n  <refsect1 id=\"return_values\">\n    <title>RETURN VALUES</title>\n    <refsect2>\n      <title>Standard API</title>\n      <para>The <function>malloc()</function> and\n      <function>calloc()</function> functions return a pointer to the\n      allocated memory if successful; otherwise a <constant>NULL</constant>\n      pointer is returned and <varname>errno</varname> is set to\n      <errorname>ENOMEM</errorname>.</para>\n\n      <para>The <function>posix_memalign()</function> function\n      returns the value 0 if successful; otherwise it returns an error value.\n      The <function>posix_memalign()</function> function will fail\n      if:\n        <variablelist>\n          <varlistentry>\n            <term><errorname>EINVAL</errorname></term>\n\n            <listitem><para>The <parameter>alignment</parameter> parameter is\n            not a power of 2 at least as large as\n            <code language=\"C\">sizeof(<type>void *</type>)</code>.\n            </para></listitem>\n          </varlistentry>\n          <varlistentry>\n            <term><errorname>ENOMEM</errorname></term>\n\n            <listitem><para>Memory allocation error.</para></listitem>\n          </varlistentry>\n        </variablelist>\n      </para>\n\n      <para>The <function>aligned_alloc()</function> function returns\n      a pointer to the allocated memory if successful; otherwise a\n      <constant>NULL</constant> pointer is returned and\n      <varname>errno</varname> is set.  The\n      <function>aligned_alloc()</function> function will fail if:\n        <variablelist>\n          <varlistentry>\n            <term><errorname>EINVAL</errorname></term>\n\n            <listitem><para>The <parameter>alignment</parameter> parameter is\n            not a power of 2.\n            </para></listitem>\n          </varlistentry>\n          <varlistentry>\n            <term><errorname>ENOMEM</errorname></term>\n\n            <listitem><para>Memory allocation error.</para></listitem>\n          </varlistentry>\n        </variablelist>\n      </para>\n\n      <para>The <function>realloc()</function> function returns a\n      pointer, possibly identical to <parameter>ptr</parameter>, to the\n      allocated memory if successful; otherwise a <constant>NULL</constant>\n      pointer is returned, and <varname>errno</varname> is set to\n      <errorname>ENOMEM</errorname> if the error was the result of an\n      allocation failure.  The <function>realloc()</function>\n      function always leaves the original buffer intact when an error occurs.\n      </para>\n\n      <para>The <function>free()</function> function returns no\n      value.</para>\n    </refsect2>\n    <refsect2>\n      <title>Non-standard API</title>\n      <para>The <function>mallocx()</function> and\n      <function>rallocx()</function> functions return a pointer to\n      the allocated memory if successful; otherwise a <constant>NULL</constant>\n      pointer is returned to indicate insufficient contiguous memory was\n      available to service the allocation request.  </para>\n\n      <para>The <function>xallocx()</function> function returns the\n      real size of the resulting resized allocation pointed to by\n      <parameter>ptr</parameter>, which is a value less than\n      <parameter>size</parameter> if the allocation could not be adequately\n      grown in place.  </para>\n\n      <para>The <function>sallocx()</function> function returns the\n      real size of the allocation pointed to by <parameter>ptr</parameter>.\n      </para>\n\n      <para>The <function>nallocx()</function> returns the real size\n      that would result from a successful equivalent\n      <function>mallocx()</function> function call, or zero if\n      insufficient memory is available to perform the size computation.  </para>\n\n      <para>The <function>mallctl()</function>,\n      <function>mallctlnametomib()</function>, and\n      <function>mallctlbymib()</function> functions return 0 on\n      success; otherwise they return an error value.  The functions will fail\n      if:\n        <variablelist>\n          <varlistentry>\n            <term><errorname>EINVAL</errorname></term>\n\n            <listitem><para><parameter>newp</parameter> is not\n            <constant>NULL</constant>, and <parameter>newlen</parameter> is too\n            large or too small.  Alternatively, <parameter>*oldlenp</parameter>\n            is too large or too small; in this case as much data as possible\n            are read despite the error.</para></listitem>\n          </varlistentry>\n          <varlistentry>\n            <term><errorname>ENOENT</errorname></term>\n\n            <listitem><para><parameter>name</parameter> or\n            <parameter>mib</parameter> specifies an unknown/invalid\n            value.</para></listitem>\n          </varlistentry>\n          <varlistentry>\n            <term><errorname>EPERM</errorname></term>\n\n            <listitem><para>Attempt to read or write void value, or attempt to\n            write read-only value.</para></listitem>\n          </varlistentry>\n          <varlistentry>\n            <term><errorname>EAGAIN</errorname></term>\n\n            <listitem><para>A memory allocation failure\n            occurred.</para></listitem>\n          </varlistentry>\n          <varlistentry>\n            <term><errorname>EFAULT</errorname></term>\n\n            <listitem><para>An interface with side effects failed in some way\n            not directly related to <function>mallctl*()</function>\n            read/write processing.</para></listitem>\n          </varlistentry>\n        </variablelist>\n      </para>\n\n      <para>The <function>malloc_usable_size()</function> function\n      returns the usable size of the allocation pointed to by\n      <parameter>ptr</parameter>.  </para>\n    </refsect2>\n  </refsect1>\n  <refsect1 id=\"environment\">\n    <title>ENVIRONMENT</title>\n    <para>The following environment variable affects the execution of the\n    allocation functions:\n      <variablelist>\n        <varlistentry>\n          <term><envar>MALLOC_CONF</envar></term>\n\n          <listitem><para>If the environment variable\n          <envar>MALLOC_CONF</envar> is set, the characters it contains\n          will be interpreted as options.</para></listitem>\n        </varlistentry>\n      </variablelist>\n    </para>\n  </refsect1>\n  <refsect1 id=\"examples\">\n    <title>EXAMPLES</title>\n    <para>To dump core whenever a problem occurs:\n      <screen>ln -s 'abort:true' /etc/malloc.conf</screen>\n    </para>\n    <para>To specify in the source that only one arena should be automatically\n    created:\n      <programlisting language=\"C\"><![CDATA[\nmalloc_conf = \"narenas:1\";]]></programlisting></para>\n  </refsect1>\n  <refsect1 id=\"see_also\">\n    <title>SEE ALSO</title>\n    <para><citerefentry><refentrytitle>madvise</refentrytitle>\n    <manvolnum>2</manvolnum></citerefentry>,\n    <citerefentry><refentrytitle>mmap</refentrytitle>\n    <manvolnum>2</manvolnum></citerefentry>,\n    <citerefentry><refentrytitle>sbrk</refentrytitle>\n    <manvolnum>2</manvolnum></citerefentry>,\n    <citerefentry><refentrytitle>utrace</refentrytitle>\n    <manvolnum>2</manvolnum></citerefentry>,\n    <citerefentry><refentrytitle>alloca</refentrytitle>\n    <manvolnum>3</manvolnum></citerefentry>,\n    <citerefentry><refentrytitle>atexit</refentrytitle>\n    <manvolnum>3</manvolnum></citerefentry>,\n    <citerefentry><refentrytitle>getpagesize</refentrytitle>\n    <manvolnum>3</manvolnum></citerefentry></para>\n  </refsect1>\n  <refsect1 id=\"standards\">\n    <title>STANDARDS</title>\n    <para>The <function>malloc()</function>,\n    <function>calloc()</function>,\n    <function>realloc()</function>, and\n    <function>free()</function> functions conform to ISO/IEC\n    9899:1990 (<quote>ISO C90</quote>).</para>\n\n    <para>The <function>posix_memalign()</function> function conforms\n    to IEEE Std 1003.1-2001 (<quote>POSIX.1</quote>).</para>\n  </refsect1>\n</refentry>\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/doc/manpages.xsl.in",
    "content": "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">\n  <xsl:import href=\"@XSLROOT@/manpages/docbook.xsl\"/>\n  <xsl:import href=\"@abs_srcroot@doc/stylesheet.xsl\"/>\n</xsl:stylesheet>\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/doc/stylesheet.xsl",
    "content": "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">\n  <xsl:param name=\"funcsynopsis.style\">ansi</xsl:param>\n  <xsl:param name=\"function.parens\" select=\"0\"/>\n  <xsl:template match=\"function\">\n    <xsl:call-template name=\"inline.monoseq\"/>\n  </xsl:template>\n  <xsl:template match=\"mallctl\">\n    <quote><xsl:call-template name=\"inline.monoseq\"/></quote>\n  </xsl:template>\n</xsl:stylesheet>\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/arena_externs.h",
    "content": "#ifndef JEMALLOC_INTERNAL_ARENA_EXTERNS_H\n#define JEMALLOC_INTERNAL_ARENA_EXTERNS_H\n\n#include \"jemalloc/internal/extent_dss.h\"\n#include \"jemalloc/internal/pages.h\"\n#include \"jemalloc/internal/size_classes.h\"\n#include \"jemalloc/internal/stats.h\"\n\nextern ssize_t opt_dirty_decay_ms;\nextern ssize_t opt_muzzy_decay_ms;\n\nextern const arena_bin_info_t arena_bin_info[NBINS];\n\nextern percpu_arena_mode_t opt_percpu_arena;\nextern const char *percpu_arena_mode_names[];\n\nextern const uint64_t h_steps[SMOOTHSTEP_NSTEPS];\nextern malloc_mutex_t arenas_lock;\n\nvoid arena_stats_large_nrequests_add(tsdn_t *tsdn, arena_stats_t *arena_stats,\n    szind_t szind, uint64_t nrequests);\nvoid arena_stats_mapped_add(tsdn_t *tsdn, arena_stats_t *arena_stats,\n    size_t size);\nvoid arena_basic_stats_merge(tsdn_t *tsdn, arena_t *arena,\n    unsigned *nthreads, const char **dss, ssize_t *dirty_decay_ms,\n    ssize_t *muzzy_decay_ms, size_t *nactive, size_t *ndirty, size_t *nmuzzy);\nvoid arena_stats_merge(tsdn_t *tsdn, arena_t *arena, unsigned *nthreads,\n    const char **dss, ssize_t *dirty_decay_ms, ssize_t *muzzy_decay_ms,\n    size_t *nactive, size_t *ndirty, size_t *nmuzzy, arena_stats_t *astats,\n    malloc_bin_stats_t *bstats, malloc_large_stats_t *lstats);\nvoid arena_extents_dirty_dalloc(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent);\n#ifdef JEMALLOC_JET\nsize_t arena_slab_regind(extent_t *slab, szind_t binind, const void *ptr);\n#endif\nextent_t *arena_extent_alloc_large(tsdn_t *tsdn, arena_t *arena,\n    size_t usize, size_t alignment, bool *zero);\nvoid arena_extent_dalloc_large_prep(tsdn_t *tsdn, arena_t *arena,\n    extent_t *extent);\nvoid arena_extent_ralloc_large_shrink(tsdn_t *tsdn, arena_t *arena,\n    extent_t *extent, size_t oldsize);\nvoid arena_extent_ralloc_large_expand(tsdn_t *tsdn, arena_t *arena,\n    extent_t *extent, size_t oldsize);\nssize_t arena_dirty_decay_ms_get(arena_t *arena);\nbool arena_dirty_decay_ms_set(tsdn_t *tsdn, arena_t *arena, ssize_t decay_ms);\nssize_t arena_muzzy_decay_ms_get(arena_t *arena);\nbool arena_muzzy_decay_ms_set(tsdn_t *tsdn, arena_t *arena, ssize_t decay_ms);\nvoid arena_decay(tsdn_t *tsdn, arena_t *arena, bool is_background_thread,\n    bool all);\nvoid arena_reset(tsd_t *tsd, arena_t *arena);\nvoid arena_destroy(tsd_t *tsd, arena_t *arena);\nvoid arena_tcache_fill_small(tsdn_t *tsdn, arena_t *arena, tcache_t *tcache,\n    tcache_bin_t *tbin, szind_t binind, uint64_t prof_accumbytes);\nvoid arena_alloc_junk_small(void *ptr, const arena_bin_info_t *bin_info,\n    bool zero);\n\ntypedef void (arena_dalloc_junk_small_t)(void *, const arena_bin_info_t *);\nextern arena_dalloc_junk_small_t *JET_MUTABLE arena_dalloc_junk_small;\n\nvoid *arena_malloc_hard(tsdn_t *tsdn, arena_t *arena, size_t size,\n    szind_t ind, bool zero);\nvoid *arena_palloc(tsdn_t *tsdn, arena_t *arena, size_t usize,\n    size_t alignment, bool zero, tcache_t *tcache);\nvoid arena_prof_promote(tsdn_t *tsdn, const void *ptr, size_t usize);\nvoid arena_dalloc_promoted(tsdn_t *tsdn, void *ptr, tcache_t *tcache,\n    bool slow_path);\nvoid arena_dalloc_bin_junked_locked(tsdn_t *tsdn, arena_t *arena,\n    extent_t *extent, void *ptr);\nvoid arena_dalloc_small(tsdn_t *tsdn, void *ptr);\nbool arena_ralloc_no_move(tsdn_t *tsdn, void *ptr, size_t oldsize, size_t size,\n    size_t extra, bool zero);\nvoid *arena_ralloc(tsdn_t *tsdn, arena_t *arena, void *ptr, size_t oldsize,\n    size_t size, size_t alignment, bool zero, tcache_t *tcache);\ndss_prec_t arena_dss_prec_get(arena_t *arena);\nbool arena_dss_prec_set(arena_t *arena, dss_prec_t dss_prec);\nssize_t arena_dirty_decay_ms_default_get(void);\nbool arena_dirty_decay_ms_default_set(ssize_t decay_ms);\nssize_t arena_muzzy_decay_ms_default_get(void);\nbool arena_muzzy_decay_ms_default_set(ssize_t decay_ms);\nunsigned arena_nthreads_get(arena_t *arena, bool internal);\nvoid arena_nthreads_inc(arena_t *arena, bool internal);\nvoid arena_nthreads_dec(arena_t *arena, bool internal);\nsize_t arena_extent_sn_next(arena_t *arena);\narena_t *arena_new(tsdn_t *tsdn, unsigned ind, extent_hooks_t *extent_hooks);\nvoid arena_boot(void);\nvoid arena_prefork0(tsdn_t *tsdn, arena_t *arena);\nvoid arena_prefork1(tsdn_t *tsdn, arena_t *arena);\nvoid arena_prefork2(tsdn_t *tsdn, arena_t *arena);\nvoid arena_prefork3(tsdn_t *tsdn, arena_t *arena);\nvoid arena_prefork4(tsdn_t *tsdn, arena_t *arena);\nvoid arena_prefork5(tsdn_t *tsdn, arena_t *arena);\nvoid arena_prefork6(tsdn_t *tsdn, arena_t *arena);\nvoid arena_postfork_parent(tsdn_t *tsdn, arena_t *arena);\nvoid arena_postfork_child(tsdn_t *tsdn, arena_t *arena);\n\n#endif /* JEMALLOC_INTERNAL_ARENA_EXTERNS_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/arena_inlines_a.h",
    "content": "#ifndef JEMALLOC_INTERNAL_ARENA_INLINES_A_H\n#define JEMALLOC_INTERNAL_ARENA_INLINES_A_H\n\nstatic inline unsigned\narena_ind_get(const arena_t *arena) {\n\treturn base_ind_get(arena->base);\n}\n\nstatic inline void\narena_internal_add(arena_t *arena, size_t size) {\n\tatomic_fetch_add_zu(&arena->stats.internal, size, ATOMIC_RELAXED);\n}\n\nstatic inline void\narena_internal_sub(arena_t *arena, size_t size) {\n\tatomic_fetch_sub_zu(&arena->stats.internal, size, ATOMIC_RELAXED);\n}\n\nstatic inline size_t\narena_internal_get(arena_t *arena) {\n\treturn atomic_load_zu(&arena->stats.internal, ATOMIC_RELAXED);\n}\n\nstatic inline bool\narena_prof_accum(tsdn_t *tsdn, arena_t *arena, uint64_t accumbytes) {\n\tcassert(config_prof);\n\n\tif (likely(prof_interval == 0)) {\n\t\treturn false;\n\t}\n\n\treturn prof_accum_add(tsdn, &arena->prof_accum, accumbytes);\n}\n\nstatic inline void\npercpu_arena_update(tsd_t *tsd, unsigned cpu) {\n\tassert(have_percpu_arena);\n\tarena_t *oldarena = tsd_arena_get(tsd);\n\tassert(oldarena != NULL);\n\tunsigned oldind = arena_ind_get(oldarena);\n\n\tif (oldind != cpu) {\n\t\tunsigned newind = cpu;\n\t\tarena_t *newarena = arena_get(tsd_tsdn(tsd), newind, true);\n\t\tassert(newarena != NULL);\n\n\t\t/* Set new arena/tcache associations. */\n\t\tarena_migrate(tsd, oldind, newind);\n\t\ttcache_t *tcache = tcache_get(tsd);\n\t\tif (tcache != NULL) {\n\t\t\ttcache_arena_reassociate(tsd_tsdn(tsd), tcache,\n\t\t\t    newarena);\n\t\t}\n\t}\n}\n\n#endif /* JEMALLOC_INTERNAL_ARENA_INLINES_A_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/arena_inlines_b.h",
    "content": "#ifndef JEMALLOC_INTERNAL_ARENA_INLINES_B_H\n#define JEMALLOC_INTERNAL_ARENA_INLINES_B_H\n\n#include \"jemalloc/internal/jemalloc_internal_types.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/rtree.h\"\n#include \"jemalloc/internal/size_classes.h\"\n#include \"jemalloc/internal/sz.h\"\n#include \"jemalloc/internal/ticker.h\"\n\nstatic inline szind_t\narena_bin_index(arena_t *arena, arena_bin_t *bin) {\n\tszind_t binind = (szind_t)(bin - arena->bins);\n\tassert(binind < NBINS);\n\treturn binind;\n}\n\nJEMALLOC_ALWAYS_INLINE prof_tctx_t *\narena_prof_tctx_get(tsdn_t *tsdn, const void *ptr, alloc_ctx_t *alloc_ctx) {\n\tcassert(config_prof);\n\tassert(ptr != NULL);\n\n\t/* Static check. */\n\tif (alloc_ctx == NULL) {\n\t\tconst extent_t *extent = iealloc(tsdn, ptr);\n\t\tif (unlikely(!extent_slab_get(extent))) {\n\t\t\treturn large_prof_tctx_get(tsdn, extent);\n\t\t}\n\t} else {\n\t\tif (unlikely(!alloc_ctx->slab)) {\n\t\t\treturn large_prof_tctx_get(tsdn, iealloc(tsdn, ptr));\n\t\t}\n\t}\n\treturn (prof_tctx_t *)(uintptr_t)1U;\n}\n\nJEMALLOC_ALWAYS_INLINE void\narena_prof_tctx_set(tsdn_t *tsdn, const void *ptr, size_t usize,\n    alloc_ctx_t *alloc_ctx, prof_tctx_t *tctx) {\n\tcassert(config_prof);\n\tassert(ptr != NULL);\n\n\t/* Static check. */\n\tif (alloc_ctx == NULL) {\n\t\textent_t *extent = iealloc(tsdn, ptr);\n\t\tif (unlikely(!extent_slab_get(extent))) {\n\t\t\tlarge_prof_tctx_set(tsdn, extent, tctx);\n\t\t}\n\t} else {\n\t\tif (unlikely(!alloc_ctx->slab)) {\n\t\t\tlarge_prof_tctx_set(tsdn, iealloc(tsdn, ptr), tctx);\n\t\t}\n\t}\n}\n\nstatic inline void\narena_prof_tctx_reset(tsdn_t *tsdn, const void *ptr, prof_tctx_t *tctx) {\n\tcassert(config_prof);\n\tassert(ptr != NULL);\n\n\textent_t *extent = iealloc(tsdn, ptr);\n\tassert(!extent_slab_get(extent));\n\n\tlarge_prof_tctx_reset(tsdn, extent);\n}\n\nJEMALLOC_ALWAYS_INLINE void\narena_decay_ticks(tsdn_t *tsdn, arena_t *arena, unsigned nticks) {\n\ttsd_t *tsd;\n\tticker_t *decay_ticker;\n\n\tif (unlikely(tsdn_null(tsdn))) {\n\t\treturn;\n\t}\n\ttsd = tsdn_tsd(tsdn);\n\tdecay_ticker = decay_ticker_get(tsd, arena_ind_get(arena));\n\tif (unlikely(decay_ticker == NULL)) {\n\t\treturn;\n\t}\n\tif (unlikely(ticker_ticks(decay_ticker, nticks))) {\n\t\tarena_decay(tsdn, arena, false, false);\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE void\narena_decay_tick(tsdn_t *tsdn, arena_t *arena) {\n\tmalloc_mutex_assert_not_owner(tsdn, &arena->decay_dirty.mtx);\n\tmalloc_mutex_assert_not_owner(tsdn, &arena->decay_muzzy.mtx);\n\n\tarena_decay_ticks(tsdn, arena, 1);\n}\n\nJEMALLOC_ALWAYS_INLINE void *\narena_malloc(tsdn_t *tsdn, arena_t *arena, size_t size, szind_t ind, bool zero,\n    tcache_t *tcache, bool slow_path) {\n\tassert(!tsdn_null(tsdn) || tcache == NULL);\n\tassert(size != 0);\n\n\tif (likely(tcache != NULL)) {\n\t\tif (likely(size <= SMALL_MAXCLASS)) {\n\t\t\treturn tcache_alloc_small(tsdn_tsd(tsdn), arena,\n\t\t\t    tcache, size, ind, zero, slow_path);\n\t\t}\n\t\tif (likely(size <= tcache_maxclass)) {\n\t\t\treturn tcache_alloc_large(tsdn_tsd(tsdn), arena,\n\t\t\t    tcache, size, ind, zero, slow_path);\n\t\t}\n\t\t/* (size > tcache_maxclass) case falls through. */\n\t\tassert(size > tcache_maxclass);\n\t}\n\n\treturn arena_malloc_hard(tsdn, arena, size, ind, zero);\n}\n\nJEMALLOC_ALWAYS_INLINE arena_t *\narena_aalloc(tsdn_t *tsdn, const void *ptr) {\n\treturn extent_arena_get(iealloc(tsdn, ptr));\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\narena_salloc(tsdn_t *tsdn, const void *ptr) {\n\tassert(ptr != NULL);\n\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\n\tszind_t szind = rtree_szind_read(tsdn, &extents_rtree, rtree_ctx,\n\t    (uintptr_t)ptr, true);\n\tassert(szind != NSIZES);\n\n\treturn sz_index2size(szind);\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\narena_vsalloc(tsdn_t *tsdn, const void *ptr) {\n\t/*\n\t * Return 0 if ptr is not within an extent managed by jemalloc.  This\n\t * function has two extra costs relative to isalloc():\n\t * - The rtree calls cannot claim to be dependent lookups, which induces\n\t *   rtree lookup load dependencies.\n\t * - The lookup may fail, so there is an extra branch to check for\n\t *   failure.\n\t */\n\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\n\textent_t *extent;\n\tszind_t szind;\n\tif (rtree_extent_szind_read(tsdn, &extents_rtree, rtree_ctx,\n\t    (uintptr_t)ptr, false, &extent, &szind)) {\n\t\treturn 0;\n\t}\n\n\tif (extent == NULL) {\n\t\treturn 0;\n\t}\n\tassert(extent_state_get(extent) == extent_state_active);\n\t/* Only slab members should be looked up via interior pointers. */\n\tassert(extent_addr_get(extent) == ptr || extent_slab_get(extent));\n\n\tassert(szind != NSIZES);\n\n\treturn sz_index2size(szind);\n}\n\nstatic inline void\narena_dalloc_no_tcache(tsdn_t *tsdn, void *ptr) {\n\tassert(ptr != NULL);\n\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\n\tszind_t szind;\n\tbool slab;\n\trtree_szind_slab_read(tsdn, &extents_rtree, rtree_ctx, (uintptr_t)ptr,\n\t    true, &szind, &slab);\n\n\tif (config_debug) {\n\t\textent_t *extent = rtree_extent_read(tsdn, &extents_rtree,\n\t\t    rtree_ctx, (uintptr_t)ptr, true);\n\t\tassert(szind == extent_szind_get(extent));\n\t\tassert(szind < NSIZES);\n\t\tassert(slab == extent_slab_get(extent));\n\t}\n\n\tif (likely(slab)) {\n\t\t/* Small allocation. */\n\t\tarena_dalloc_small(tsdn, ptr);\n\t} else {\n\t\textent_t *extent = iealloc(tsdn, ptr);\n\t\tlarge_dalloc(tsdn, extent);\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE void\narena_dalloc(tsdn_t *tsdn, void *ptr, tcache_t *tcache,\n    alloc_ctx_t *alloc_ctx, bool slow_path) {\n\tassert(!tsdn_null(tsdn) || tcache == NULL);\n\tassert(ptr != NULL);\n\n\tif (unlikely(tcache == NULL)) {\n\t\tarena_dalloc_no_tcache(tsdn, ptr);\n\t\treturn;\n\t}\n\n\tszind_t szind;\n\tbool slab;\n\trtree_ctx_t *rtree_ctx;\n\tif (alloc_ctx != NULL) {\n\t\tszind = alloc_ctx->szind;\n\t\tslab = alloc_ctx->slab;\n\t\tassert(szind != NSIZES);\n\t} else {\n\t\trtree_ctx = tsd_rtree_ctx(tsdn_tsd(tsdn));\n\t\trtree_szind_slab_read(tsdn, &extents_rtree, rtree_ctx,\n\t\t    (uintptr_t)ptr, true, &szind, &slab);\n\t}\n\n\tif (config_debug) {\n\t\trtree_ctx = tsd_rtree_ctx(tsdn_tsd(tsdn));\n\t\textent_t *extent = rtree_extent_read(tsdn, &extents_rtree,\n\t\t    rtree_ctx, (uintptr_t)ptr, true);\n\t\tassert(szind == extent_szind_get(extent));\n\t\tassert(szind < NSIZES);\n\t\tassert(slab == extent_slab_get(extent));\n\t}\n\n\tif (likely(slab)) {\n\t\t/* Small allocation. */\n\t\ttcache_dalloc_small(tsdn_tsd(tsdn), tcache, ptr, szind,\n\t\t    slow_path);\n\t} else {\n\t\tif (szind < nhbins) {\n\t\t\tif (config_prof && unlikely(szind < NBINS)) {\n\t\t\t\tarena_dalloc_promoted(tsdn, ptr, tcache,\n\t\t\t\t    slow_path);\n\t\t\t} else {\n\t\t\t\ttcache_dalloc_large(tsdn_tsd(tsdn), tcache, ptr,\n\t\t\t\t    szind, slow_path);\n\t\t\t}\n\t\t} else {\n\t\t\textent_t *extent = iealloc(tsdn, ptr);\n\t\t\tlarge_dalloc(tsdn, extent);\n\t\t}\n\t}\n}\n\nstatic inline void\narena_sdalloc_no_tcache(tsdn_t *tsdn, void *ptr, size_t size) {\n\tassert(ptr != NULL);\n\tassert(size <= LARGE_MAXCLASS);\n\n\tszind_t szind;\n\tbool slab;\n\tif (!config_prof || !opt_prof) {\n\t\t/*\n\t\t * There is no risk of being confused by a promoted sampled\n\t\t * object, so base szind and slab on the given size.\n\t\t */\n\t\tszind = sz_size2index(size);\n\t\tslab = (szind < NBINS);\n\t}\n\n\tif ((config_prof && opt_prof) || config_debug) {\n\t\trtree_ctx_t rtree_ctx_fallback;\n\t\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn,\n\t\t    &rtree_ctx_fallback);\n\n\t\trtree_szind_slab_read(tsdn, &extents_rtree, rtree_ctx,\n\t\t    (uintptr_t)ptr, true, &szind, &slab);\n\n\t\tassert(szind == sz_size2index(size));\n\t\tassert((config_prof && opt_prof) || slab == (szind < NBINS));\n\n\t\tif (config_debug) {\n\t\t\textent_t *extent = rtree_extent_read(tsdn,\n\t\t\t    &extents_rtree, rtree_ctx, (uintptr_t)ptr, true);\n\t\t\tassert(szind == extent_szind_get(extent));\n\t\t\tassert(slab == extent_slab_get(extent));\n\t\t}\n\t}\n\n\tif (likely(slab)) {\n\t\t/* Small allocation. */\n\t\tarena_dalloc_small(tsdn, ptr);\n\t} else {\n\t\textent_t *extent = iealloc(tsdn, ptr);\n\t\tlarge_dalloc(tsdn, extent);\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE void\narena_sdalloc(tsdn_t *tsdn, void *ptr, size_t size, tcache_t *tcache,\n    alloc_ctx_t *alloc_ctx, bool slow_path) {\n\tassert(!tsdn_null(tsdn) || tcache == NULL);\n\tassert(ptr != NULL);\n\tassert(size <= LARGE_MAXCLASS);\n\n\tif (unlikely(tcache == NULL)) {\n\t\tarena_sdalloc_no_tcache(tsdn, ptr, size);\n\t\treturn;\n\t}\n\n\tszind_t szind;\n\tbool slab;\n\tUNUSED alloc_ctx_t local_ctx;\n\tif (config_prof && opt_prof) {\n\t\tif (alloc_ctx == NULL) {\n\t\t\t/* Uncommon case and should be a static check. */\n\t\t\trtree_ctx_t rtree_ctx_fallback;\n\t\t\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn,\n\t\t\t    &rtree_ctx_fallback);\n\t\t\trtree_szind_slab_read(tsdn, &extents_rtree, rtree_ctx,\n\t\t\t    (uintptr_t)ptr, true, &local_ctx.szind,\n\t\t\t    &local_ctx.slab);\n\t\t\tassert(local_ctx.szind == sz_size2index(size));\n\t\t\talloc_ctx = &local_ctx;\n\t\t}\n\t\tslab = alloc_ctx->slab;\n\t\tszind = alloc_ctx->szind;\n\t} else {\n\t\t/*\n\t\t * There is no risk of being confused by a promoted sampled\n\t\t * object, so base szind and slab on the given size.\n\t\t */\n\t\tszind = sz_size2index(size);\n\t\tslab = (szind < NBINS);\n\t}\n\n\tif (config_debug) {\n\t\trtree_ctx_t *rtree_ctx = tsd_rtree_ctx(tsdn_tsd(tsdn));\n\t\trtree_szind_slab_read(tsdn, &extents_rtree, rtree_ctx,\n\t\t    (uintptr_t)ptr, true, &szind, &slab);\n\t\textent_t *extent = rtree_extent_read(tsdn,\n\t\t    &extents_rtree, rtree_ctx, (uintptr_t)ptr, true);\n\t\tassert(szind == extent_szind_get(extent));\n\t\tassert(slab == extent_slab_get(extent));\n\t}\n\n\tif (likely(slab)) {\n\t\t/* Small allocation. */\n\t\ttcache_dalloc_small(tsdn_tsd(tsdn), tcache, ptr, szind,\n\t\t    slow_path);\n\t} else {\n\t\tif (szind < nhbins) {\n\t\t\tif (config_prof && unlikely(szind < NBINS)) {\n\t\t\t\tarena_dalloc_promoted(tsdn, ptr, tcache,\n\t\t\t\t    slow_path);\n\t\t\t} else {\n\t\t\t\ttcache_dalloc_large(tsdn_tsd(tsdn),\n\t\t\t\t    tcache, ptr, szind, slow_path);\n\t\t\t}\n\t\t} else {\n\t\t\textent_t *extent = iealloc(tsdn, ptr);\n\t\t\tlarge_dalloc(tsdn, extent);\n\t\t}\n\t}\n}\n\n#endif /* JEMALLOC_INTERNAL_ARENA_INLINES_B_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/arena_structs_a.h",
    "content": "#ifndef JEMALLOC_INTERNAL_ARENA_STRUCTS_A_H\n#define JEMALLOC_INTERNAL_ARENA_STRUCTS_A_H\n\n#include \"jemalloc/internal/bitmap.h\"\n\nstruct arena_slab_data_s {\n\t/* Per region allocated/deallocated bitmap. */\n\tbitmap_t\tbitmap[BITMAP_GROUPS_MAX];\n};\n\n#endif /* JEMALLOC_INTERNAL_ARENA_STRUCTS_A_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/arena_structs_b.h",
    "content": "#ifndef JEMALLOC_INTERNAL_ARENA_STRUCTS_B_H\n#define JEMALLOC_INTERNAL_ARENA_STRUCTS_B_H\n\n#include \"jemalloc/internal/atomic.h\"\n#include \"jemalloc/internal/bitmap.h\"\n#include \"jemalloc/internal/extent_dss.h\"\n#include \"jemalloc/internal/jemalloc_internal_types.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/nstime.h\"\n#include \"jemalloc/internal/ql.h\"\n#include \"jemalloc/internal/size_classes.h\"\n#include \"jemalloc/internal/smoothstep.h\"\n#include \"jemalloc/internal/stats.h\"\n#include \"jemalloc/internal/ticker.h\"\n\n/*\n * Read-only information associated with each element of arena_t's bins array\n * is stored separately, partly to reduce memory usage (only one copy, rather\n * than one per arena), but mainly to avoid false cacheline sharing.\n *\n * Each slab has the following layout:\n *\n *   /--------------------\\\n *   | region 0           |\n *   |--------------------|\n *   | region 1           |\n *   |--------------------|\n *   | ...                |\n *   | ...                |\n *   | ...                |\n *   |--------------------|\n *   | region nregs-1     |\n *   \\--------------------/\n */\nstruct arena_bin_info_s {\n\t/* Size of regions in a slab for this bin's size class. */\n\tsize_t\t\t\treg_size;\n\n\t/* Total size of a slab for this bin's size class. */\n\tsize_t\t\t\tslab_size;\n\n\t/* Total number of regions in a slab for this bin's size class. */\n\tuint32_t\t\tnregs;\n\n\t/*\n\t * Metadata used to manipulate bitmaps for slabs associated with this\n\t * bin.\n\t */\n\tbitmap_info_t\t\tbitmap_info;\n};\n\nstruct arena_decay_s {\n\t/* Synchronizes all non-atomic fields. */\n\tmalloc_mutex_t\t\tmtx;\n\t/*\n\t * True if a thread is currently purging the extents associated with\n\t * this decay structure.\n\t */\n\tbool\t\t\tpurging;\n\t/*\n\t * Approximate time in milliseconds from the creation of a set of unused\n\t * dirty pages until an equivalent set of unused dirty pages is purged\n\t * and/or reused.\n\t */\n\tatomic_zd_t\t\ttime_ms;\n\t/* time / SMOOTHSTEP_NSTEPS. */\n\tnstime_t\t\tinterval;\n\t/*\n\t * Time at which the current decay interval logically started.  We do\n\t * not actually advance to a new epoch until sometime after it starts\n\t * because of scheduling and computation delays, and it is even possible\n\t * to completely skip epochs.  In all cases, during epoch advancement we\n\t * merge all relevant activity into the most recently recorded epoch.\n\t */\n\tnstime_t\t\tepoch;\n\t/* Deadline randomness generator. */\n\tuint64_t\t\tjitter_state;\n\t/*\n\t * Deadline for current epoch.  This is the sum of interval and per\n\t * epoch jitter which is a uniform random variable in [0..interval).\n\t * Epochs always advance by precise multiples of interval, but we\n\t * randomize the deadline to reduce the likelihood of arenas purging in\n\t * lockstep.\n\t */\n\tnstime_t\t\tdeadline;\n\t/*\n\t * Number of unpurged pages at beginning of current epoch.  During epoch\n\t * advancement we use the delta between arena->decay_*.nunpurged and\n\t * extents_npages_get(&arena->extents_*) to determine how many dirty\n\t * pages, if any, were generated.\n\t */\n\tsize_t\t\t\tnunpurged;\n\t/*\n\t * Trailing log of how many unused dirty pages were generated during\n\t * each of the past SMOOTHSTEP_NSTEPS decay epochs, where the last\n\t * element is the most recent epoch.  Corresponding epoch times are\n\t * relative to epoch.\n\t */\n\tsize_t\t\t\tbacklog[SMOOTHSTEP_NSTEPS];\n\n\t/*\n\t * Pointer to associated stats.  These stats are embedded directly in\n\t * the arena's stats due to how stats structures are shared between the\n\t * arena and ctl code.\n\t *\n\t * Synchronization: Same as associated arena's stats field. */\n\tdecay_stats_t\t\t*stats;\n\t/* Peak number of pages in associated extents.  Used for debug only. */\n\tuint64_t\t\tceil_npages;\n};\n\nstruct arena_bin_s {\n\t/* All operations on arena_bin_t fields require lock ownership. */\n\tmalloc_mutex_t\t\tlock;\n\n\t/*\n\t * Current slab being used to service allocations of this bin's size\n\t * class.  slabcur is independent of slabs_{nonfull,full}; whenever\n\t * slabcur is reassigned, the previous slab must be deallocated or\n\t * inserted into slabs_{nonfull,full}.\n\t */\n\textent_t\t\t*slabcur;\n\n\t/*\n\t * Heap of non-full slabs.  This heap is used to assure that new\n\t * allocations come from the non-full slab that is oldest/lowest in\n\t * memory.\n\t */\n\textent_heap_t\t\tslabs_nonfull;\n\n\t/* List used to track full slabs. */\n\textent_list_t\t\tslabs_full;\n\n\t/* Bin statistics. */\n\tmalloc_bin_stats_t\tstats;\n};\n\nstruct arena_s {\n\t/*\n\t * Number of threads currently assigned to this arena.  Each thread has\n\t * two distinct assignments, one for application-serving allocation, and\n\t * the other for internal metadata allocation.  Internal metadata must\n\t * not be allocated from arenas explicitly created via the arenas.create\n\t * mallctl, because the arena.<i>.reset mallctl indiscriminately\n\t * discards all allocations for the affected arena.\n\t *\n\t *   0: Application allocation.\n\t *   1: Internal metadata allocation.\n\t *\n\t * Synchronization: atomic.\n\t */\n\tatomic_u_t\t\tnthreads[2];\n\n\t/*\n\t * When percpu_arena is enabled, to amortize the cost of reading /\n\t * updating the current CPU id, track the most recent thread accessing\n\t * this arena, and only read CPU if there is a mismatch.\n\t */\n\ttsdn_t\t\t*last_thd;\n\n\t/* Synchronization: internal. */\n\tarena_stats_t\t\tstats;\n\n\t/*\n\t * List of tcaches for extant threads associated with this arena.\n\t * Stats from these are merged incrementally, and at exit if\n\t * opt_stats_print is enabled.\n\t *\n\t * Synchronization: tcache_ql_mtx.\n\t */\n\tql_head(tcache_t)\ttcache_ql;\n\tmalloc_mutex_t\t\ttcache_ql_mtx;\n\n\t/* Synchronization: internal. */\n\tprof_accum_t\t\tprof_accum;\n\tuint64_t\t\tprof_accumbytes;\n\n\t/*\n\t * PRNG state for cache index randomization of large allocation base\n\t * pointers.\n\t *\n\t * Synchronization: atomic.\n\t */\n\tatomic_zu_t\t\toffset_state;\n\n\t/*\n\t * Extent serial number generator state.\n\t *\n\t * Synchronization: atomic.\n\t */\n\tatomic_zu_t\t\textent_sn_next;\n\n\t/*\n\t * Represents a dss_prec_t, but atomically.\n\t *\n\t * Synchronization: atomic.\n\t */\n\tatomic_u_t\t\tdss_prec;\n\n\t/*\n\t * Number of pages in active extents.\n\t *\n\t * Synchronization: atomic.\n\t */\n\tatomic_zu_t\t\tnactive;\n\n\t/*\n\t * Extant large allocations.\n\t *\n\t * Synchronization: large_mtx.\n\t */\n\textent_list_t\t\tlarge;\n\t/* Synchronizes all large allocation/update/deallocation. */\n\tmalloc_mutex_t\t\tlarge_mtx;\n\n\t/*\n\t * Collections of extents that were previously allocated.  These are\n\t * used when allocating extents, in an attempt to re-use address space.\n\t *\n\t * Synchronization: internal.\n\t */\n\textents_t\t\textents_dirty;\n\textents_t\t\textents_muzzy;\n\textents_t\t\textents_retained;\n\n\t/*\n\t * Decay-based purging state, responsible for scheduling extent state\n\t * transitions.\n\t *\n\t * Synchronization: internal.\n\t */\n\tarena_decay_t\t\tdecay_dirty; /* dirty --> muzzy */\n\tarena_decay_t\t\tdecay_muzzy; /* muzzy --> retained */\n\n\t/*\n\t * Next extent size class in a growing series to use when satisfying a\n\t * request via the extent hooks (only if opt_retain).  This limits the\n\t * number of disjoint virtual memory ranges so that extent merging can\n\t * be effective even if multiple arenas' extent allocation requests are\n\t * highly interleaved.\n\t *\n\t * Synchronization: extent_grow_mtx\n\t */\n\tpszind_t\t\textent_grow_next;\n\tmalloc_mutex_t\t\textent_grow_mtx;\n\n\t/*\n\t * Available extent structures that were allocated via\n\t * base_alloc_extent().\n\t *\n\t * Synchronization: extent_avail_mtx.\n\t */\n\textent_tree_t\t\textent_avail;\n\tmalloc_mutex_t\t\textent_avail_mtx;\n\n\t/*\n\t * bins is used to store heaps of free regions.\n\t *\n\t * Synchronization: internal.\n\t */\n\tarena_bin_t\t\tbins[NBINS];\n\n\t/*\n\t * Base allocator, from which arena metadata are allocated.\n\t *\n\t * Synchronization: internal.\n\t */\n\tbase_t\t\t\t*base;\n\t/* Used to determine uptime.  Read-only after initialization. */\n\tnstime_t\t\tcreate_time;\n};\n\n/* Used in conjunction with tsd for fast arena-related context lookup. */\nstruct arena_tdata_s {\n\tticker_t\t\tdecay_ticker;\n};\n\n/* Used to pass rtree lookup context down the path. */\nstruct alloc_ctx_s {\n\tszind_t szind;\n\tbool slab;\n};\n\n#endif /* JEMALLOC_INTERNAL_ARENA_STRUCTS_B_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/arena_types.h",
    "content": "#ifndef JEMALLOC_INTERNAL_ARENA_TYPES_H\n#define JEMALLOC_INTERNAL_ARENA_TYPES_H\n\n/* Maximum number of regions in one slab. */\n#define LG_SLAB_MAXREGS\t\t(LG_PAGE - LG_TINY_MIN)\n#define SLAB_MAXREGS\t\t(1U << LG_SLAB_MAXREGS)\n\n/* Default decay times in milliseconds. */\n#define DIRTY_DECAY_MS_DEFAULT\tZD(10 * 1000)\n#define MUZZY_DECAY_MS_DEFAULT\tZD(10 * 1000)\n/* Number of event ticks between time checks. */\n#define DECAY_NTICKS_PER_UPDATE\t1000\n\ntypedef struct arena_slab_data_s arena_slab_data_t;\ntypedef struct arena_bin_info_s arena_bin_info_t;\ntypedef struct arena_decay_s arena_decay_t;\ntypedef struct arena_bin_s arena_bin_t;\ntypedef struct arena_s arena_t;\ntypedef struct arena_tdata_s arena_tdata_t;\ntypedef struct alloc_ctx_s alloc_ctx_t;\n\ntypedef enum {\n\tpercpu_arena_mode_names_base   = 0, /* Used for options processing. */\n\n\t/*\n\t * *_uninit are used only during bootstrapping, and must correspond\n\t * to initialized variant plus percpu_arena_mode_enabled_base.\n\t */\n\tpercpu_arena_uninit            = 0,\n\tper_phycpu_arena_uninit        = 1,\n\n\t/* All non-disabled modes must come after percpu_arena_disabled. */\n\tpercpu_arena_disabled          = 2,\n\n\tpercpu_arena_mode_names_limit  = 3, /* Used for options processing. */\n\tpercpu_arena_mode_enabled_base = 3,\n\n\tpercpu_arena                   = 3,\n\tper_phycpu_arena               = 4  /* Hyper threads share arena. */\n} percpu_arena_mode_t;\n\n#define PERCPU_ARENA_ENABLED(m)\t((m) >= percpu_arena_mode_enabled_base)\n#define PERCPU_ARENA_DEFAULT\tpercpu_arena_disabled\n\n#endif /* JEMALLOC_INTERNAL_ARENA_TYPES_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/assert.h",
    "content": "#include \"jemalloc/internal/malloc_io.h\"\n#include \"jemalloc/internal/util.h\"\n\n/*\n * Define a custom assert() in order to reduce the chances of deadlock during\n * assertion failure.\n */\n#ifndef assert\n#define assert(e) do {\t\t\t\t\t\t\t\\\n\tif (unlikely(config_debug && !(e))) {\t\t\t\t\\\n\t\tmalloc_printf(\t\t\t\t\t\t\\\n\t\t    \"<jemalloc>: %s:%d: Failed assertion: \\\"%s\\\"\\n\",\t\\\n\t\t    __FILE__, __LINE__, #e);\t\t\t\t\\\n\t\tabort();\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n#endif\n\n#ifndef not_reached\n#define not_reached() do {\t\t\t\t\t\t\\\n\tif (config_debug) {\t\t\t\t\t\t\\\n\t\tmalloc_printf(\t\t\t\t\t\t\\\n\t\t    \"<jemalloc>: %s:%d: Unreachable code reached\\n\",\t\\\n\t\t    __FILE__, __LINE__);\t\t\t\t\\\n\t\tabort();\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tunreachable();\t\t\t\t\t\t\t\\\n} while (0)\n#endif\n\n#ifndef not_implemented\n#define not_implemented() do {\t\t\t\t\t\t\\\n\tif (config_debug) {\t\t\t\t\t\t\\\n\t\tmalloc_printf(\"<jemalloc>: %s:%d: Not implemented\\n\",\t\\\n\t\t    __FILE__, __LINE__);\t\t\t\t\\\n\t\tabort();\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n#endif\n\n#ifndef assert_not_implemented\n#define assert_not_implemented(e) do {\t\t\t\t\t\\\n\tif (unlikely(config_debug && !(e))) {\t\t\t\t\\\n\t\tnot_implemented();\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n#endif\n\n/* Use to assert a particular configuration, e.g., cassert(config_debug). */\n#ifndef cassert\n#define cassert(c) do {\t\t\t\t\t\t\t\\\n\tif (unlikely(!(c))) {\t\t\t\t\t\t\\\n\t\tnot_reached();\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n#endif\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/atomic.h",
    "content": "#ifndef JEMALLOC_INTERNAL_ATOMIC_H\n#define JEMALLOC_INTERNAL_ATOMIC_H\n\n#define ATOMIC_INLINE static inline\n\n#if defined(JEMALLOC_GCC_ATOMIC_ATOMICS)\n#  include \"jemalloc/internal/atomic_gcc_atomic.h\"\n#elif defined(JEMALLOC_GCC_SYNC_ATOMICS)\n#  include \"jemalloc/internal/atomic_gcc_sync.h\"\n#elif defined(_MSC_VER)\n#  include \"jemalloc/internal/atomic_msvc.h\"\n#elif defined(JEMALLOC_C11_ATOMICS)\n#  include \"jemalloc/internal/atomic_c11.h\"\n#else\n#  error \"Don't have atomics implemented on this platform.\"\n#endif\n\n/*\n * This header gives more or less a backport of C11 atomics. The user can write\n * JEMALLOC_GENERATE_ATOMICS(type, short_type, lg_sizeof_type); to generate\n * counterparts of the C11 atomic functions for type, as so:\n *   JEMALLOC_GENERATE_ATOMICS(int *, pi, 3);\n * and then write things like:\n *   int *some_ptr;\n *   atomic_pi_t atomic_ptr_to_int;\n *   atomic_store_pi(&atomic_ptr_to_int, some_ptr, ATOMIC_RELAXED);\n *   int *prev_value = atomic_exchange_pi(&ptr_to_int, NULL, ATOMIC_ACQ_REL);\n *   assert(some_ptr == prev_value);\n * and expect things to work in the obvious way.\n *\n * Also included (with naming differences to avoid conflicts with the standard\n * library):\n *   atomic_fence(atomic_memory_order_t) (mimics C11's atomic_thread_fence).\n *   ATOMIC_INIT (mimics C11's ATOMIC_VAR_INIT).\n */\n\n/*\n * Pure convenience, so that we don't have to type \"atomic_memory_order_\"\n * quite so often.\n */\n#define ATOMIC_RELAXED atomic_memory_order_relaxed\n#define ATOMIC_ACQUIRE atomic_memory_order_acquire\n#define ATOMIC_RELEASE atomic_memory_order_release\n#define ATOMIC_ACQ_REL atomic_memory_order_acq_rel\n#define ATOMIC_SEQ_CST atomic_memory_order_seq_cst\n\n/*\n * Not all platforms have 64-bit atomics.  If we do, this #define exposes that\n * fact.\n */\n#if (LG_SIZEOF_PTR == 3 || LG_SIZEOF_INT == 3)\n#  define JEMALLOC_ATOMIC_U64\n#endif\n\nJEMALLOC_GENERATE_ATOMICS(void *, p, LG_SIZEOF_PTR)\n\n/*\n * There's no actual guarantee that sizeof(bool) == 1, but it's true on the only\n * platform that actually needs to know the size, MSVC.\n */\nJEMALLOC_GENERATE_ATOMICS(bool, b, 0)\n\nJEMALLOC_GENERATE_INT_ATOMICS(unsigned, u, LG_SIZEOF_INT)\n\nJEMALLOC_GENERATE_INT_ATOMICS(size_t, zu, LG_SIZEOF_PTR)\n\nJEMALLOC_GENERATE_INT_ATOMICS(ssize_t, zd, LG_SIZEOF_PTR)\n\nJEMALLOC_GENERATE_INT_ATOMICS(uint32_t, u32, 2)\n\n#ifdef JEMALLOC_ATOMIC_U64\nJEMALLOC_GENERATE_INT_ATOMICS(uint64_t, u64, 3)\n#endif\n\n#undef ATOMIC_INLINE\n\n#endif /* JEMALLOC_INTERNAL_ATOMIC_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/atomic_c11.h",
    "content": "#ifndef JEMALLOC_INTERNAL_ATOMIC_C11_H\n#define JEMALLOC_INTERNAL_ATOMIC_C11_H\n\n#include <stdatomic.h>\n\n#define ATOMIC_INIT(...) ATOMIC_VAR_INIT(__VA_ARGS__)\n\n#define atomic_memory_order_t memory_order\n#define atomic_memory_order_relaxed memory_order_relaxed\n#define atomic_memory_order_acquire memory_order_acquire\n#define atomic_memory_order_release memory_order_release\n#define atomic_memory_order_acq_rel memory_order_acq_rel\n#define atomic_memory_order_seq_cst memory_order_seq_cst\n\n#define atomic_fence atomic_thread_fence\n\n#define JEMALLOC_GENERATE_ATOMICS(type, short_type,\t\t\t\\\n    /* unused */ lg_size)\t\t\t\t\t\t\\\ntypedef _Atomic(type) atomic_##short_type##_t;\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_load_##short_type(const atomic_##short_type##_t *a,\t\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\t/*\t\t\t\t\t\t\t\t\\\n\t * A strict interpretation of the C standard prevents\t\t\\\n\t * atomic_load from taking a const argument, but it's\t\t\\\n\t * convenient for our purposes. This cast is a workaround.\t\\\n\t */\t\t\t\t\t\t\t\t\\\n\tatomic_##short_type##_t* a_nonconst =\t\t\t\t\\\n\t    (atomic_##short_type##_t*)a;\t\t\t\t\\\n\treturn atomic_load_explicit(a_nonconst, mo);\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE void\t\t\t\t\t\t\t\\\natomic_store_##short_type(atomic_##short_type##_t *a,\t\t\t\\\n    type val, atomic_memory_order_t mo) {\t\t\t\t\\\n\tatomic_store_explicit(a, val, mo);\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_exchange_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\treturn atomic_exchange_explicit(a, val, mo);\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE bool\t\t\t\t\t\t\t\\\natomic_compare_exchange_weak_##short_type(atomic_##short_type##_t *a,\t\\\n    type *expected, type desired, atomic_memory_order_t success_mo,\t\\\n    atomic_memory_order_t failure_mo) {\t\t\t\t\t\\\n\treturn atomic_compare_exchange_weak_explicit(a, expected,\t\\\n\t    desired, success_mo, failure_mo);\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE bool\t\t\t\t\t\t\t\\\natomic_compare_exchange_strong_##short_type(atomic_##short_type##_t *a,\t\\\n    type *expected, type desired, atomic_memory_order_t success_mo,\t\\\n    atomic_memory_order_t failure_mo) {\t\t\t\t\t\\\n\treturn atomic_compare_exchange_strong_explicit(a, expected,\t\\\n\t    desired, success_mo, failure_mo);\t\t\t\t\\\n}\n\n/*\n * Integral types have some special operations available that non-integral ones\n * lack.\n */\n#define JEMALLOC_GENERATE_INT_ATOMICS(type, short_type, \t\t\\\n    /* unused */ lg_size)\t\t\t\t\t\t\\\nJEMALLOC_GENERATE_ATOMICS(type, short_type, /* unused */ lg_size)\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_add_##short_type(atomic_##short_type##_t *a,\t\t\\\n    type val, atomic_memory_order_t mo) {\t\t\t\t\\\n\treturn atomic_fetch_add_explicit(a, val, mo);\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_sub_##short_type(atomic_##short_type##_t *a,\t\t\\\n    type val, atomic_memory_order_t mo) {\t\t\t\t\\\n\treturn atomic_fetch_sub_explicit(a, val, mo);\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_and_##short_type(atomic_##short_type##_t *a,\t\t\\\n    type val, atomic_memory_order_t mo) {\t\t\t\t\\\n\treturn atomic_fetch_and_explicit(a, val, mo);\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_or_##short_type(atomic_##short_type##_t *a,\t\t\\\n    type val, atomic_memory_order_t mo) {\t\t\t\t\\\n\treturn atomic_fetch_or_explicit(a, val, mo);\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_xor_##short_type(atomic_##short_type##_t *a,\t\t\\\n    type val, atomic_memory_order_t mo) {\t\t\t\t\\\n\treturn atomic_fetch_xor_explicit(a, val, mo);\t\t\t\\\n}\n\n#endif /* JEMALLOC_INTERNAL_ATOMIC_C11_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/atomic_gcc_atomic.h",
    "content": "#ifndef JEMALLOC_INTERNAL_ATOMIC_GCC_ATOMIC_H\n#define JEMALLOC_INTERNAL_ATOMIC_GCC_ATOMIC_H\n\n#include \"jemalloc/internal/assert.h\"\n\n#define ATOMIC_INIT(...) {__VA_ARGS__}\n\ntypedef enum {\n\tatomic_memory_order_relaxed,\n\tatomic_memory_order_acquire,\n\tatomic_memory_order_release,\n\tatomic_memory_order_acq_rel,\n\tatomic_memory_order_seq_cst\n} atomic_memory_order_t;\n\nATOMIC_INLINE int\natomic_enum_to_builtin(atomic_memory_order_t mo) {\n\tswitch (mo) {\n\tcase atomic_memory_order_relaxed:\n\t\treturn __ATOMIC_RELAXED;\n\tcase atomic_memory_order_acquire:\n\t\treturn __ATOMIC_ACQUIRE;\n\tcase atomic_memory_order_release:\n\t\treturn __ATOMIC_RELEASE;\n\tcase atomic_memory_order_acq_rel:\n\t\treturn __ATOMIC_ACQ_REL;\n\tcase atomic_memory_order_seq_cst:\n\t\treturn __ATOMIC_SEQ_CST;\n\t}\n\t/* Can't happen; the switch is exhaustive. */\n\tnot_reached();\n}\n\nATOMIC_INLINE void\natomic_fence(atomic_memory_order_t mo) {\n\t__atomic_thread_fence(atomic_enum_to_builtin(mo));\n}\n\n#define JEMALLOC_GENERATE_ATOMICS(type, short_type,\t\t\t\\\n    /* unused */ lg_size)\t\t\t\t\t\t\\\ntypedef struct {\t\t\t\t\t\t\t\\\n\ttype repr;\t\t\t\t\t\t\t\\\n} atomic_##short_type##_t;\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_load_##short_type(const atomic_##short_type##_t *a,\t\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\ttype result;\t\t\t\t\t\t\t\\\n\t__atomic_load(&a->repr, &result, atomic_enum_to_builtin(mo));\t\\\n\treturn result;\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE void\t\t\t\t\t\t\t\\\natomic_store_##short_type(atomic_##short_type##_t *a, type val,\t\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\t__atomic_store(&a->repr, &val, atomic_enum_to_builtin(mo));\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_exchange_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\ttype result;\t\t\t\t\t\t\t\\\n\t__atomic_exchange(&a->repr, &val, &result,\t\t\t\\\n\t    atomic_enum_to_builtin(mo));\t\t\t\t\\\n\treturn result;\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE bool\t\t\t\t\t\t\t\\\natomic_compare_exchange_weak_##short_type(atomic_##short_type##_t *a,\t\\\n    type *expected, type desired, atomic_memory_order_t success_mo,\t\\\n    atomic_memory_order_t failure_mo) {\t\t\t\t\t\\\n\treturn __atomic_compare_exchange(&a->repr, expected, &desired,\t\\\n\t    true, atomic_enum_to_builtin(success_mo),\t\t\t\\\n\t    atomic_enum_to_builtin(failure_mo));\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE bool\t\t\t\t\t\t\t\\\natomic_compare_exchange_strong_##short_type(atomic_##short_type##_t *a,\t\\\n    type *expected, type desired, atomic_memory_order_t success_mo,\t\\\n    atomic_memory_order_t failure_mo) {\t\t\t\t\t\\\n\treturn __atomic_compare_exchange(&a->repr, expected, &desired,\t\\\n\t    false,\t\t\t\t\t\t\t\\\n\t    atomic_enum_to_builtin(success_mo),\t\t\t\t\\\n\t    atomic_enum_to_builtin(failure_mo));\t\t\t\\\n}\n\n\n#define JEMALLOC_GENERATE_INT_ATOMICS(type, short_type,\t\t\t\\\n    /* unused */ lg_size)\t\t\t\t\t\t\\\nJEMALLOC_GENERATE_ATOMICS(type, short_type, /* unused */ lg_size)\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_add_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\treturn __atomic_fetch_add(&a->repr, val,\t\t\t\\\n\t    atomic_enum_to_builtin(mo));\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_sub_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\treturn __atomic_fetch_sub(&a->repr, val,\t\t\t\\\n\t    atomic_enum_to_builtin(mo));\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_and_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\treturn __atomic_fetch_and(&a->repr, val,\t\t\t\\\n\t    atomic_enum_to_builtin(mo));\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_or_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\treturn __atomic_fetch_or(&a->repr, val,\t\t\t\t\\\n\t    atomic_enum_to_builtin(mo));\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_xor_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\treturn __atomic_fetch_xor(&a->repr, val,\t\t\t\\\n\t    atomic_enum_to_builtin(mo));\t\t\t\t\\\n}\n\n#endif /* JEMALLOC_INTERNAL_ATOMIC_GCC_ATOMIC_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/atomic_gcc_sync.h",
    "content": "#ifndef JEMALLOC_INTERNAL_ATOMIC_GCC_SYNC_H\n#define JEMALLOC_INTERNAL_ATOMIC_GCC_SYNC_H\n\n#define ATOMIC_INIT(...) {__VA_ARGS__}\n\ntypedef enum {\n\tatomic_memory_order_relaxed,\n\tatomic_memory_order_acquire,\n\tatomic_memory_order_release,\n\tatomic_memory_order_acq_rel,\n\tatomic_memory_order_seq_cst\n} atomic_memory_order_t;\n\nATOMIC_INLINE void\natomic_fence(atomic_memory_order_t mo) {\n\t/* Easy cases first: no barrier, and full barrier. */\n\tif (mo == atomic_memory_order_relaxed) {\n\t\tasm volatile(\"\" ::: \"memory\");\n\t\treturn;\n\t}\n\tif (mo == atomic_memory_order_seq_cst) {\n\t\tasm volatile(\"\" ::: \"memory\");\n\t\t__sync_synchronize();\n\t\tasm volatile(\"\" ::: \"memory\");\n\t\treturn;\n\t}\n\tasm volatile(\"\" ::: \"memory\");\n#  if defined(__i386__) || defined(__x86_64__)\n\t/* This is implicit on x86. */\n#  elif defined(__ppc__)\n\tasm volatile(\"lwsync\");\n#  elif defined(__sparc__) && defined(__arch64__)\n\tif (mo == atomic_memory_order_acquire) {\n\t\tasm volatile(\"membar #LoadLoad | #LoadStore\");\n\t} else if (mo == atomic_memory_order_release) {\n\t\tasm volatile(\"membar #LoadStore | #StoreStore\");\n\t} else {\n\t\tasm volatile(\"membar #LoadLoad | #LoadStore | #StoreStore\");\n\t}\n#  else\n\t__sync_synchronize();\n#  endif\n\tasm volatile(\"\" ::: \"memory\");\n}\n\n/*\n * A correct implementation of seq_cst loads and stores on weakly ordered\n * architectures could do either of the following:\n *   1. store() is weak-fence -> store -> strong fence, load() is load ->\n *      strong-fence.\n *   2. store() is strong-fence -> store, load() is strong-fence -> load ->\n *      weak-fence.\n * The tricky thing is, load() and store() above can be the load or store\n * portions of a gcc __sync builtin, so we have to follow GCC's lead, which\n * means going with strategy 2.\n * On strongly ordered architectures, the natural strategy is to stick a strong\n * fence after seq_cst stores, and have naked loads.  So we want the strong\n * fences in different places on different architectures.\n * atomic_pre_sc_load_fence and atomic_post_sc_store_fence allow us to\n * accomplish this.\n */\n\nATOMIC_INLINE void\natomic_pre_sc_load_fence() {\n#  if defined(__i386__) || defined(__x86_64__) ||\t\t\t\\\n    (defined(__sparc__) && defined(__arch64__))\n\tatomic_fence(atomic_memory_order_relaxed);\n#  else\n\tatomic_fence(atomic_memory_order_seq_cst);\n#  endif\n}\n\nATOMIC_INLINE void\natomic_post_sc_store_fence() {\n#  if defined(__i386__) || defined(__x86_64__) ||\t\t\t\\\n    (defined(__sparc__) && defined(__arch64__))\n\tatomic_fence(atomic_memory_order_seq_cst);\n#  else\n\tatomic_fence(atomic_memory_order_relaxed);\n#  endif\n\n}\n\n#define JEMALLOC_GENERATE_ATOMICS(type, short_type,\t\t\t\\\n    /* unused */ lg_size)\t\t\t\t\t\t\\\ntypedef struct {\t\t\t\t\t\t\t\\\n\ttype volatile repr;\t\t\t\t\t\t\\\n} atomic_##short_type##_t;\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_load_##short_type(const atomic_##short_type##_t *a,\t\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\tif (mo == atomic_memory_order_seq_cst) {\t\t\t\\\n\t\tatomic_pre_sc_load_fence();\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\ttype result = a->repr;\t\t\t\t\t\t\\\n\tif (mo != atomic_memory_order_relaxed) {\t\t\t\\\n\t\tatomic_fence(atomic_memory_order_acquire);\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\treturn result;\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE void\t\t\t\t\t\t\t\\\natomic_store_##short_type(atomic_##short_type##_t *a,\t\t\t\\\n    type val, atomic_memory_order_t mo) {\t\t\t\t\\\n\tif (mo != atomic_memory_order_relaxed) {\t\t\t\\\n\t\tatomic_fence(atomic_memory_order_release);\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\ta->repr = val;\t\t\t\t\t\t\t\\\n\tif (mo == atomic_memory_order_seq_cst) {\t\t\t\\\n\t\tatomic_post_sc_store_fence();\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_exchange_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\t/*\t\t\t\t\t\t\t\t\\\n\t * Because of FreeBSD, we care about gcc 4.2, which doesn't have\\\n\t * an atomic exchange builtin.  We fake it with a CAS loop.\t\\\n\t */\t\t\t\t\t\t\t\t\\\n\twhile (true) {\t\t\t\t\t\t\t\\\n\t\ttype old = a->repr;\t\t\t\t\t\\\n\t\tif (__sync_bool_compare_and_swap(&a->repr, old, val)) {\t\\\n\t\t\treturn old;\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE bool\t\t\t\t\t\t\t\\\natomic_compare_exchange_weak_##short_type(atomic_##short_type##_t *a,\t\\\n    type *expected, type desired, atomic_memory_order_t success_mo,\t\\\n    atomic_memory_order_t failure_mo) {\t\t\t\t\t\\\n\ttype prev = __sync_val_compare_and_swap(&a->repr, *expected,\t\\\n\t    desired);\t\t\t\t\t\t\t\\\n\tif (prev == *expected) {\t\t\t\t\t\\\n\t\treturn true;\t\t\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t\t*expected = prev;\t\t\t\t\t\\\n\t\treturn false;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE bool\t\t\t\t\t\t\t\\\natomic_compare_exchange_strong_##short_type(atomic_##short_type##_t *a,\t\\\n    type *expected, type desired, atomic_memory_order_t success_mo,\t\\\n    atomic_memory_order_t failure_mo) {\t\t\t\t\t\\\n\ttype prev = __sync_val_compare_and_swap(&a->repr, *expected,\t\\\n\t    desired);\t\t\t\t\t\t\t\\\n\tif (prev == *expected) {\t\t\t\t\t\\\n\t\treturn true;\t\t\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t\t*expected = prev;\t\t\t\t\t\\\n\t\treturn false;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n}\n\n#define JEMALLOC_GENERATE_INT_ATOMICS(type, short_type,\t\t\t\\\n    /* unused */ lg_size)\t\t\t\t\t\t\\\nJEMALLOC_GENERATE_ATOMICS(type, short_type, /* unused */ lg_size)\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_add_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\treturn __sync_fetch_and_add(&a->repr, val);\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_sub_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\treturn __sync_fetch_and_sub(&a->repr, val);\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_and_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\treturn __sync_fetch_and_and(&a->repr, val);\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_or_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\treturn __sync_fetch_and_or(&a->repr, val);\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_xor_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\treturn __sync_fetch_and_xor(&a->repr, val);\t\t\t\\\n}\n\n#endif /* JEMALLOC_INTERNAL_ATOMIC_GCC_SYNC_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/atomic_msvc.h",
    "content": "#ifndef JEMALLOC_INTERNAL_ATOMIC_MSVC_H\n#define JEMALLOC_INTERNAL_ATOMIC_MSVC_H\n\n#define ATOMIC_INIT(...) {__VA_ARGS__}\n\ntypedef enum {\n\tatomic_memory_order_relaxed,\n\tatomic_memory_order_acquire,\n\tatomic_memory_order_release,\n\tatomic_memory_order_acq_rel,\n\tatomic_memory_order_seq_cst\n} atomic_memory_order_t;\n\ntypedef char atomic_repr_0_t;\ntypedef short atomic_repr_1_t;\ntypedef long atomic_repr_2_t;\ntypedef __int64 atomic_repr_3_t;\n\nATOMIC_INLINE void\natomic_fence(atomic_memory_order_t mo) {\n\t_ReadWriteBarrier();\n#  if defined(_M_ARM) || defined(_M_ARM64)\n\t/* ARM needs a barrier for everything but relaxed. */\n\tif (mo != atomic_memory_order_relaxed) {\n\t\tMemoryBarrier();\n\t}\n#  elif defined(_M_IX86) || defined (_M_X64)\n\t/* x86 needs a barrier only for seq_cst. */\n\tif (mo == atomic_memory_order_seq_cst) {\n\t\tMemoryBarrier();\n\t}\n#  else\n#  error \"Don't know how to create atomics for this platform for MSVC.\"\n#  endif\n\t_ReadWriteBarrier();\n}\n\n#define ATOMIC_INTERLOCKED_REPR(lg_size) atomic_repr_ ## lg_size ## _t\n\n#define ATOMIC_CONCAT(a, b) ATOMIC_RAW_CONCAT(a, b)\n#define ATOMIC_RAW_CONCAT(a, b) a ## b\n\n#define ATOMIC_INTERLOCKED_NAME(base_name, lg_size) ATOMIC_CONCAT(\t\\\n    base_name, ATOMIC_INTERLOCKED_SUFFIX(lg_size))\n\n#define ATOMIC_INTERLOCKED_SUFFIX(lg_size)\t\t\t\t\\\n    ATOMIC_CONCAT(ATOMIC_INTERLOCKED_SUFFIX_, lg_size)\n\n#define ATOMIC_INTERLOCKED_SUFFIX_0 8\n#define ATOMIC_INTERLOCKED_SUFFIX_1 16\n#define ATOMIC_INTERLOCKED_SUFFIX_2\n#define ATOMIC_INTERLOCKED_SUFFIX_3 64\n\n#define JEMALLOC_GENERATE_ATOMICS(type, short_type, lg_size)\t\t\\\ntypedef struct {\t\t\t\t\t\t\t\\\n\tATOMIC_INTERLOCKED_REPR(lg_size) repr;\t\t\t\t\\\n} atomic_##short_type##_t;\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_load_##short_type(const atomic_##short_type##_t *a,\t\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\tATOMIC_INTERLOCKED_REPR(lg_size) ret = a->repr;\t\t\t\\\n\tif (mo != atomic_memory_order_relaxed) {\t\t\t\\\n\t\tatomic_fence(atomic_memory_order_acquire);\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\treturn (type) ret;\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE void\t\t\t\t\t\t\t\\\natomic_store_##short_type(atomic_##short_type##_t *a,\t\t\t\\\n    type val, atomic_memory_order_t mo) {\t\t\t\t\\\n\tif (mo != atomic_memory_order_relaxed) {\t\t\t\\\n\t\tatomic_fence(atomic_memory_order_release);\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\ta->repr = (ATOMIC_INTERLOCKED_REPR(lg_size)) val;\t\t\\\n\tif (mo == atomic_memory_order_seq_cst) {\t\t\t\\\n\t\tatomic_fence(atomic_memory_order_seq_cst);\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_exchange_##short_type(atomic_##short_type##_t *a, type val,\t\\\n    atomic_memory_order_t mo) {\t\t\t\t\t\t\\\n\treturn (type)ATOMIC_INTERLOCKED_NAME(_InterlockedExchange,\t\\\n\t    lg_size)(&a->repr, (ATOMIC_INTERLOCKED_REPR(lg_size))val);\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE bool\t\t\t\t\t\t\t\\\natomic_compare_exchange_weak_##short_type(atomic_##short_type##_t *a,\t\\\n    type *expected, type desired, atomic_memory_order_t success_mo,\t\\\n    atomic_memory_order_t failure_mo) {\t\t\t\t\t\\\n\tATOMIC_INTERLOCKED_REPR(lg_size) e =\t\t\t\t\\\n\t    (ATOMIC_INTERLOCKED_REPR(lg_size))*expected;\t\t\\\n\tATOMIC_INTERLOCKED_REPR(lg_size) d =\t\t\t\t\\\n\t    (ATOMIC_INTERLOCKED_REPR(lg_size))desired;\t\t\t\\\n\tATOMIC_INTERLOCKED_REPR(lg_size) old =\t\t\t\t\\\n\t    ATOMIC_INTERLOCKED_NAME(_InterlockedCompareExchange, \t\\\n\t\tlg_size)(&a->repr, d, e);\t\t\t\t\\\n\tif (old == e) {\t\t\t\t\t\t\t\\\n\t\treturn true;\t\t\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t\t*expected = (type)old;\t\t\t\t\t\\\n\t\treturn false;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE bool\t\t\t\t\t\t\t\\\natomic_compare_exchange_strong_##short_type(atomic_##short_type##_t *a,\t\\\n    type *expected, type desired, atomic_memory_order_t success_mo,\t\\\n    atomic_memory_order_t failure_mo) {\t\t\t\t\t\\\n\t/* We implement the weak version with strong semantics. */\t\\\n\treturn atomic_compare_exchange_weak_##short_type(a, expected,\t\\\n\t    desired, success_mo, failure_mo);\t\t\t\t\\\n}\n\n\n#define JEMALLOC_GENERATE_INT_ATOMICS(type, short_type, lg_size)\t\\\nJEMALLOC_GENERATE_ATOMICS(type, short_type, lg_size)\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_add_##short_type(atomic_##short_type##_t *a,\t\t\\\n    type val, atomic_memory_order_t mo) {\t\t\t\t\\\n\treturn (type)ATOMIC_INTERLOCKED_NAME(_InterlockedExchangeAdd,\t\\\n\t    lg_size)(&a->repr, (ATOMIC_INTERLOCKED_REPR(lg_size))val);\t\\\n}\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_sub_##short_type(atomic_##short_type##_t *a,\t\t\\\n    type val, atomic_memory_order_t mo) {\t\t\t\t\\\n\t/*\t\t\t\t\t\t\t\t\\\n\t * MSVC warns on negation of unsigned operands, but for us it\t\\\n\t * gives exactly the right semantics (MAX_TYPE + 1 - operand).\t\\\n\t */\t\t\t\t\t\t\t\t\\\n\t__pragma(warning(push))\t\t\t\t\t\t\\\n\t__pragma(warning(disable: 4146))\t\t\t\t\\\n\treturn atomic_fetch_add_##short_type(a, -val, mo);\t\t\\\n\t__pragma(warning(pop))\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_and_##short_type(atomic_##short_type##_t *a,\t\t\\\n    type val, atomic_memory_order_t mo) {\t\t\t\t\\\n\treturn (type)ATOMIC_INTERLOCKED_NAME(_InterlockedAnd, lg_size)(\t\\\n\t    &a->repr, (ATOMIC_INTERLOCKED_REPR(lg_size))val);\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_or_##short_type(atomic_##short_type##_t *a,\t\t\\\n    type val, atomic_memory_order_t mo) {\t\t\t\t\\\n\treturn (type)ATOMIC_INTERLOCKED_NAME(_InterlockedOr, lg_size)(\t\\\n\t    &a->repr, (ATOMIC_INTERLOCKED_REPR(lg_size))val);\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\nATOMIC_INLINE type\t\t\t\t\t\t\t\\\natomic_fetch_xor_##short_type(atomic_##short_type##_t *a,\t\t\\\n    type val, atomic_memory_order_t mo) {\t\t\t\t\\\n\treturn (type)ATOMIC_INTERLOCKED_NAME(_InterlockedXor, lg_size)(\t\\\n\t    &a->repr, (ATOMIC_INTERLOCKED_REPR(lg_size))val);\t\t\\\n}\n\n#endif /* JEMALLOC_INTERNAL_ATOMIC_MSVC_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/background_thread_externs.h",
    "content": "#ifndef JEMALLOC_INTERNAL_BACKGROUND_THREAD_EXTERNS_H\n#define JEMALLOC_INTERNAL_BACKGROUND_THREAD_EXTERNS_H\n\nextern bool opt_background_thread;\nextern malloc_mutex_t background_thread_lock;\nextern atomic_b_t background_thread_enabled_state;\nextern size_t n_background_threads;\nextern background_thread_info_t *background_thread_info;\nextern bool can_enable_background_thread;\n\nbool background_thread_create(tsd_t *tsd, unsigned arena_ind);\nbool background_threads_enable(tsd_t *tsd);\nbool background_threads_disable(tsd_t *tsd);\nvoid background_thread_interval_check(tsdn_t *tsdn, arena_t *arena,\n    arena_decay_t *decay, size_t npages_new);\nvoid background_thread_prefork0(tsdn_t *tsdn);\nvoid background_thread_prefork1(tsdn_t *tsdn);\nvoid background_thread_postfork_parent(tsdn_t *tsdn);\nvoid background_thread_postfork_child(tsdn_t *tsdn);\nbool background_thread_stats_read(tsdn_t *tsdn,\n    background_thread_stats_t *stats);\nvoid background_thread_ctl_init(tsdn_t *tsdn);\n\n#ifdef JEMALLOC_PTHREAD_CREATE_WRAPPER\nextern int pthread_create_wrapper(pthread_t *__restrict, const pthread_attr_t *,\n    void *(*)(void *), void *__restrict);\n#endif\nbool background_thread_boot0(void);\nbool background_thread_boot1(tsdn_t *tsdn);\n\n#endif /* JEMALLOC_INTERNAL_BACKGROUND_THREAD_EXTERNS_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/background_thread_inlines.h",
    "content": "#ifndef JEMALLOC_INTERNAL_BACKGROUND_THREAD_INLINES_H\n#define JEMALLOC_INTERNAL_BACKGROUND_THREAD_INLINES_H\n\nJEMALLOC_ALWAYS_INLINE bool\nbackground_thread_enabled(void) {\n\treturn atomic_load_b(&background_thread_enabled_state, ATOMIC_RELAXED);\n}\n\nJEMALLOC_ALWAYS_INLINE void\nbackground_thread_enabled_set(tsdn_t *tsdn, bool state) {\n\tmalloc_mutex_assert_owner(tsdn, &background_thread_lock);\n\tatomic_store_b(&background_thread_enabled_state, state, ATOMIC_RELAXED);\n}\n\nJEMALLOC_ALWAYS_INLINE background_thread_info_t *\narena_background_thread_info_get(arena_t *arena) {\n\tunsigned arena_ind = arena_ind_get(arena);\n\treturn &background_thread_info[arena_ind % ncpus];\n}\n\nJEMALLOC_ALWAYS_INLINE uint64_t\nbackground_thread_wakeup_time_get(background_thread_info_t *info) {\n\tuint64_t next_wakeup = nstime_ns(&info->next_wakeup);\n\tassert(atomic_load_b(&info->indefinite_sleep, ATOMIC_ACQUIRE) ==\n\t    (next_wakeup == BACKGROUND_THREAD_INDEFINITE_SLEEP));\n\treturn next_wakeup;\n}\n\nJEMALLOC_ALWAYS_INLINE void\nbackground_thread_wakeup_time_set(tsdn_t *tsdn, background_thread_info_t *info,\n    uint64_t wakeup_time) {\n\tmalloc_mutex_assert_owner(tsdn, &info->mtx);\n\tatomic_store_b(&info->indefinite_sleep,\n\t    wakeup_time == BACKGROUND_THREAD_INDEFINITE_SLEEP, ATOMIC_RELEASE);\n\tnstime_init(&info->next_wakeup, wakeup_time);\n}\n\nJEMALLOC_ALWAYS_INLINE bool\nbackground_thread_indefinite_sleep(background_thread_info_t *info) {\n\treturn atomic_load_b(&info->indefinite_sleep, ATOMIC_ACQUIRE);\n}\n\nJEMALLOC_ALWAYS_INLINE void\narena_background_thread_inactivity_check(tsdn_t *tsdn, arena_t *arena,\n    bool is_background_thread) {\n\tif (!background_thread_enabled() || is_background_thread) {\n\t\treturn;\n\t}\n\tbackground_thread_info_t *info =\n\t    arena_background_thread_info_get(arena);\n\tif (background_thread_indefinite_sleep(info)) {\n\t\tbackground_thread_interval_check(tsdn, arena,\n\t\t    &arena->decay_dirty, 0);\n\t}\n}\n\n#endif /* JEMALLOC_INTERNAL_BACKGROUND_THREAD_INLINES_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/background_thread_structs.h",
    "content": "#ifndef JEMALLOC_INTERNAL_BACKGROUND_THREAD_STRUCTS_H\n#define JEMALLOC_INTERNAL_BACKGROUND_THREAD_STRUCTS_H\n\n/* This file really combines \"structs\" and \"types\", but only transitionally. */\n\n#if defined(JEMALLOC_BACKGROUND_THREAD) || defined(JEMALLOC_LAZY_LOCK)\n#  define JEMALLOC_PTHREAD_CREATE_WRAPPER\n#endif\n\n#define BACKGROUND_THREAD_INDEFINITE_SLEEP UINT64_MAX\n\ntypedef enum {\n\tbackground_thread_stopped,\n\tbackground_thread_started,\n\t/* Thread waits on the global lock when paused (for arena_reset). */\n\tbackground_thread_paused,\n} background_thread_state_t;\n\nstruct background_thread_info_s {\n#ifdef JEMALLOC_BACKGROUND_THREAD\n\t/* Background thread is pthread specific. */\n\tpthread_t\t\tthread;\n\tpthread_cond_t\t\tcond;\n#endif\n\tmalloc_mutex_t\t\tmtx;\n\tbackground_thread_state_t\tstate;\n\t/* When true, it means no wakeup scheduled. */\n\tatomic_b_t\t\tindefinite_sleep;\n\t/* Next scheduled wakeup time (absolute time in ns). */\n\tnstime_t\t\tnext_wakeup;\n\t/*\n\t *  Since the last background thread run, newly added number of pages\n\t *  that need to be purged by the next wakeup.  This is adjusted on\n\t *  epoch advance, and is used to determine whether we should signal the\n\t *  background thread to wake up earlier.\n\t */\n\tsize_t\t\t\tnpages_to_purge_new;\n\t/* Stats: total number of runs since started. */\n\tuint64_t\t\ttot_n_runs;\n\t/* Stats: total sleep time since started. */\n\tnstime_t\t\ttot_sleep_time;\n};\ntypedef struct background_thread_info_s background_thread_info_t;\n\nstruct background_thread_stats_s {\n\tsize_t num_threads;\n\tuint64_t num_runs;\n\tnstime_t run_interval;\n};\ntypedef struct background_thread_stats_s background_thread_stats_t;\n\n#endif /* JEMALLOC_INTERNAL_BACKGROUND_THREAD_STRUCTS_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/base_externs.h",
    "content": "#ifndef JEMALLOC_INTERNAL_BASE_EXTERNS_H\n#define JEMALLOC_INTERNAL_BASE_EXTERNS_H\n\nbase_t *b0get(void);\nbase_t *base_new(tsdn_t *tsdn, unsigned ind, extent_hooks_t *extent_hooks);\nvoid base_delete(base_t *base);\nextent_hooks_t *base_extent_hooks_get(base_t *base);\nextent_hooks_t *base_extent_hooks_set(base_t *base,\n    extent_hooks_t *extent_hooks);\nvoid *base_alloc(tsdn_t *tsdn, base_t *base, size_t size, size_t alignment);\nextent_t *base_alloc_extent(tsdn_t *tsdn, base_t *base);\nvoid base_stats_get(tsdn_t *tsdn, base_t *base, size_t *allocated,\n    size_t *resident, size_t *mapped);\nvoid base_prefork(tsdn_t *tsdn, base_t *base);\nvoid base_postfork_parent(tsdn_t *tsdn, base_t *base);\nvoid base_postfork_child(tsdn_t *tsdn, base_t *base);\nbool base_boot(tsdn_t *tsdn);\n\n#endif /* JEMALLOC_INTERNAL_BASE_EXTERNS_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/base_inlines.h",
    "content": "#ifndef JEMALLOC_INTERNAL_BASE_INLINES_H\n#define JEMALLOC_INTERNAL_BASE_INLINES_H\n\nstatic inline unsigned\nbase_ind_get(const base_t *base) {\n\treturn base->ind;\n}\n\n#endif /* JEMALLOC_INTERNAL_BASE_INLINES_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/base_structs.h",
    "content": "#ifndef JEMALLOC_INTERNAL_BASE_STRUCTS_H\n#define JEMALLOC_INTERNAL_BASE_STRUCTS_H\n\n#include \"jemalloc/internal/jemalloc_internal_types.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/size_classes.h\"\n\n/* Embedded at the beginning of every block of base-managed virtual memory. */\nstruct base_block_s {\n\t/* Total size of block's virtual memory mapping. */\n\tsize_t\t\tsize;\n\n\t/* Next block in list of base's blocks. */\n\tbase_block_t\t*next;\n\n\t/* Tracks unused trailing space. */\n\textent_t\textent;\n};\n\nstruct base_s {\n\t/* Associated arena's index within the arenas array. */\n\tunsigned\tind;\n\n\t/*\n\t * User-configurable extent hook functions.  Points to an\n\t * extent_hooks_t.\n\t */\n\tatomic_p_t\textent_hooks;\n\n\t/* Protects base_alloc() and base_stats_get() operations. */\n\tmalloc_mutex_t\tmtx;\n\n\t/*\n\t * Most recent size class in the series of increasingly large base\n\t * extents.  Logarithmic spacing between subsequent allocations ensures\n\t * that the total number of distinct mappings remains small.\n\t */\n\tpszind_t\tpind_last;\n\n\t/* Serial number generation state. */\n\tsize_t\t\textent_sn_next;\n\n\t/* Chain of all blocks associated with base. */\n\tbase_block_t\t*blocks;\n\n\t/* Heap of extents that track unused trailing space within blocks. */\n\textent_heap_t\tavail[NSIZES];\n\n\t/* Stats, only maintained if config_stats. */\n\tsize_t\t\tallocated;\n\tsize_t\t\tresident;\n\tsize_t\t\tmapped;\n};\n\n#endif /* JEMALLOC_INTERNAL_BASE_STRUCTS_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/base_types.h",
    "content": "#ifndef JEMALLOC_INTERNAL_BASE_TYPES_H\n#define JEMALLOC_INTERNAL_BASE_TYPES_H\n\ntypedef struct base_block_s base_block_t;\ntypedef struct base_s base_t;\n\n#endif /* JEMALLOC_INTERNAL_BASE_TYPES_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/bit_util.h",
    "content": "#ifndef JEMALLOC_INTERNAL_BIT_UTIL_H\n#define JEMALLOC_INTERNAL_BIT_UTIL_H\n\n#include \"jemalloc/internal/assert.h\"\n\n#define BIT_UTIL_INLINE static inline\n\n/* Sanity check. */\n#if !defined(JEMALLOC_INTERNAL_FFSLL) || !defined(JEMALLOC_INTERNAL_FFSL) \\\n    || !defined(JEMALLOC_INTERNAL_FFS)\n#  error JEMALLOC_INTERNAL_FFS{,L,LL} should have been defined by configure\n#endif\n\n\nBIT_UTIL_INLINE unsigned\nffs_llu(unsigned long long bitmap) {\n\treturn JEMALLOC_INTERNAL_FFSLL(bitmap);\n}\n\nBIT_UTIL_INLINE unsigned\nffs_lu(unsigned long bitmap) {\n\treturn JEMALLOC_INTERNAL_FFSL(bitmap);\n}\n\nBIT_UTIL_INLINE unsigned\nffs_u(unsigned bitmap) {\n\treturn JEMALLOC_INTERNAL_FFS(bitmap);\n}\n\nBIT_UTIL_INLINE unsigned\nffs_zu(size_t bitmap) {\n#if LG_SIZEOF_PTR == LG_SIZEOF_INT\n\treturn ffs_u(bitmap);\n#elif LG_SIZEOF_PTR == LG_SIZEOF_LONG\n\treturn ffs_lu(bitmap);\n#elif LG_SIZEOF_PTR == LG_SIZEOF_LONG_LONG\n\treturn ffs_llu(bitmap);\n#else\n#error No implementation for size_t ffs()\n#endif\n}\n\nBIT_UTIL_INLINE unsigned\nffs_u64(uint64_t bitmap) {\n#if LG_SIZEOF_LONG == 3\n\treturn ffs_lu(bitmap);\n#elif LG_SIZEOF_LONG_LONG == 3\n\treturn ffs_llu(bitmap);\n#else\n#error No implementation for 64-bit ffs()\n#endif\n}\n\nBIT_UTIL_INLINE unsigned\nffs_u32(uint32_t bitmap) {\n#if LG_SIZEOF_INT == 2\n\treturn ffs_u(bitmap);\n#else\n#error No implementation for 32-bit ffs()\n#endif\n\treturn ffs_u(bitmap);\n}\n\nBIT_UTIL_INLINE uint64_t\npow2_ceil_u64(uint64_t x) {\n\tx--;\n\tx |= x >> 1;\n\tx |= x >> 2;\n\tx |= x >> 4;\n\tx |= x >> 8;\n\tx |= x >> 16;\n\tx |= x >> 32;\n\tx++;\n\treturn x;\n}\n\nBIT_UTIL_INLINE uint32_t\npow2_ceil_u32(uint32_t x) {\n\tx--;\n\tx |= x >> 1;\n\tx |= x >> 2;\n\tx |= x >> 4;\n\tx |= x >> 8;\n\tx |= x >> 16;\n\tx++;\n\treturn x;\n}\n\n/* Compute the smallest power of 2 that is >= x. */\nBIT_UTIL_INLINE size_t\npow2_ceil_zu(size_t x) {\n#if (LG_SIZEOF_PTR == 3)\n\treturn pow2_ceil_u64(x);\n#else\n\treturn pow2_ceil_u32(x);\n#endif\n}\n\n#if (defined(__i386__) || defined(__amd64__) || defined(__x86_64__))\nBIT_UTIL_INLINE unsigned\nlg_floor(size_t x) {\n\tsize_t ret;\n\tassert(x != 0);\n\n\tasm (\"bsr %1, %0\"\n\t    : \"=r\"(ret) // Outputs.\n\t    : \"r\"(x)    // Inputs.\n\t    );\n\tassert(ret < UINT_MAX);\n\treturn (unsigned)ret;\n}\n#elif (defined(_MSC_VER))\nBIT_UTIL_INLINE unsigned\nlg_floor(size_t x) {\n\tunsigned long ret;\n\n\tassert(x != 0);\n\n#if (LG_SIZEOF_PTR == 3)\n\t_BitScanReverse64(&ret, x);\n#elif (LG_SIZEOF_PTR == 2)\n\t_BitScanReverse(&ret, x);\n#else\n#  error \"Unsupported type size for lg_floor()\"\n#endif\n\tassert(ret < UINT_MAX);\n\treturn (unsigned)ret;\n}\n#elif (defined(JEMALLOC_HAVE_BUILTIN_CLZ))\nBIT_UTIL_INLINE unsigned\nlg_floor(size_t x) {\n\tassert(x != 0);\n\n#if (LG_SIZEOF_PTR == LG_SIZEOF_INT)\n\treturn ((8 << LG_SIZEOF_PTR) - 1) - __builtin_clz(x);\n#elif (LG_SIZEOF_PTR == LG_SIZEOF_LONG)\n\treturn ((8 << LG_SIZEOF_PTR) - 1) - __builtin_clzl(x);\n#else\n#  error \"Unsupported type size for lg_floor()\"\n#endif\n}\n#else\nBIT_UTIL_INLINE unsigned\nlg_floor(size_t x) {\n\tassert(x != 0);\n\n\tx |= (x >> 1);\n\tx |= (x >> 2);\n\tx |= (x >> 4);\n\tx |= (x >> 8);\n\tx |= (x >> 16);\n#if (LG_SIZEOF_PTR == 3)\n\tx |= (x >> 32);\n#endif\n\tif (x == SIZE_T_MAX) {\n\t\treturn (8 << LG_SIZEOF_PTR) - 1;\n\t}\n\tx++;\n\treturn ffs_zu(x) - 2;\n}\n#endif\n\n#undef BIT_UTIL_INLINE\n\n#endif /* JEMALLOC_INTERNAL_BIT_UTIL_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/bitmap.h",
    "content": "#ifndef JEMALLOC_INTERNAL_BITMAP_H\n#define JEMALLOC_INTERNAL_BITMAP_H\n\n#include \"jemalloc/internal/arena_types.h\"\n#include \"jemalloc/internal/bit_util.h\"\n#include \"jemalloc/internal/size_classes.h\"\n\ntypedef unsigned long bitmap_t;\n#define LG_SIZEOF_BITMAP\tLG_SIZEOF_LONG\n\n/* Maximum bitmap bit count is 2^LG_BITMAP_MAXBITS. */\n#if LG_SLAB_MAXREGS > LG_CEIL_NSIZES\n/* Maximum bitmap bit count is determined by maximum regions per slab. */\n#  define LG_BITMAP_MAXBITS\tLG_SLAB_MAXREGS\n#else\n/* Maximum bitmap bit count is determined by number of extent size classes. */\n#  define LG_BITMAP_MAXBITS\tLG_CEIL_NSIZES\n#endif\n#define BITMAP_MAXBITS\t\t(ZU(1) << LG_BITMAP_MAXBITS)\n\n/* Number of bits per group. */\n#define LG_BITMAP_GROUP_NBITS\t\t(LG_SIZEOF_BITMAP + 3)\n#define BITMAP_GROUP_NBITS\t\t(1U << LG_BITMAP_GROUP_NBITS)\n#define BITMAP_GROUP_NBITS_MASK\t\t(BITMAP_GROUP_NBITS-1)\n\n/*\n * Do some analysis on how big the bitmap is before we use a tree.  For a brute\n * force linear search, if we would have to call ffs_lu() more than 2^3 times,\n * use a tree instead.\n */\n#if LG_BITMAP_MAXBITS - LG_BITMAP_GROUP_NBITS > 3\n#  define BITMAP_USE_TREE\n#endif\n\n/* Number of groups required to store a given number of bits. */\n#define BITMAP_BITS2GROUPS(nbits)\t\t\t\t\t\\\n    (((nbits) + BITMAP_GROUP_NBITS_MASK) >> LG_BITMAP_GROUP_NBITS)\n\n/*\n * Number of groups required at a particular level for a given number of bits.\n */\n#define BITMAP_GROUPS_L0(nbits)\t\t\t\t\t\t\\\n    BITMAP_BITS2GROUPS(nbits)\n#define BITMAP_GROUPS_L1(nbits)\t\t\t\t\t\t\\\n    BITMAP_BITS2GROUPS(BITMAP_BITS2GROUPS(nbits))\n#define BITMAP_GROUPS_L2(nbits)\t\t\t\t\t\t\\\n    BITMAP_BITS2GROUPS(BITMAP_BITS2GROUPS(BITMAP_BITS2GROUPS((nbits))))\n#define BITMAP_GROUPS_L3(nbits)\t\t\t\t\t\t\\\n    BITMAP_BITS2GROUPS(BITMAP_BITS2GROUPS(BITMAP_BITS2GROUPS(\t\t\\\n\tBITMAP_BITS2GROUPS((nbits)))))\n#define BITMAP_GROUPS_L4(nbits)\t\t\t\t\t\t\\\n    BITMAP_BITS2GROUPS(BITMAP_BITS2GROUPS(BITMAP_BITS2GROUPS(\t\t\\\n\tBITMAP_BITS2GROUPS(BITMAP_BITS2GROUPS((nbits))))))\n\n/*\n * Assuming the number of levels, number of groups required for a given number\n * of bits.\n */\n#define BITMAP_GROUPS_1_LEVEL(nbits)\t\t\t\t\t\\\n    BITMAP_GROUPS_L0(nbits)\n#define BITMAP_GROUPS_2_LEVEL(nbits)\t\t\t\t\t\\\n    (BITMAP_GROUPS_1_LEVEL(nbits) + BITMAP_GROUPS_L1(nbits))\n#define BITMAP_GROUPS_3_LEVEL(nbits)\t\t\t\t\t\\\n    (BITMAP_GROUPS_2_LEVEL(nbits) + BITMAP_GROUPS_L2(nbits))\n#define BITMAP_GROUPS_4_LEVEL(nbits)\t\t\t\t\t\\\n    (BITMAP_GROUPS_3_LEVEL(nbits) + BITMAP_GROUPS_L3(nbits))\n#define BITMAP_GROUPS_5_LEVEL(nbits)\t\t\t\t\t\\\n    (BITMAP_GROUPS_4_LEVEL(nbits) + BITMAP_GROUPS_L4(nbits))\n\n/*\n * Maximum number of groups required to support LG_BITMAP_MAXBITS.\n */\n#ifdef BITMAP_USE_TREE\n\n#if LG_BITMAP_MAXBITS <= LG_BITMAP_GROUP_NBITS\n#  define BITMAP_GROUPS(nbits)\tBITMAP_GROUPS_1_LEVEL(nbits)\n#  define BITMAP_GROUPS_MAX\tBITMAP_GROUPS_1_LEVEL(BITMAP_MAXBITS)\n#elif LG_BITMAP_MAXBITS <= LG_BITMAP_GROUP_NBITS * 2\n#  define BITMAP_GROUPS(nbits)\tBITMAP_GROUPS_2_LEVEL(nbits)\n#  define BITMAP_GROUPS_MAX\tBITMAP_GROUPS_2_LEVEL(BITMAP_MAXBITS)\n#elif LG_BITMAP_MAXBITS <= LG_BITMAP_GROUP_NBITS * 3\n#  define BITMAP_GROUPS(nbits)\tBITMAP_GROUPS_3_LEVEL(nbits)\n#  define BITMAP_GROUPS_MAX\tBITMAP_GROUPS_3_LEVEL(BITMAP_MAXBITS)\n#elif LG_BITMAP_MAXBITS <= LG_BITMAP_GROUP_NBITS * 4\n#  define BITMAP_GROUPS(nbits)\tBITMAP_GROUPS_4_LEVEL(nbits)\n#  define BITMAP_GROUPS_MAX\tBITMAP_GROUPS_4_LEVEL(BITMAP_MAXBITS)\n#elif LG_BITMAP_MAXBITS <= LG_BITMAP_GROUP_NBITS * 5\n#  define BITMAP_GROUPS(nbits)\tBITMAP_GROUPS_5_LEVEL(nbits)\n#  define BITMAP_GROUPS_MAX\tBITMAP_GROUPS_5_LEVEL(BITMAP_MAXBITS)\n#else\n#  error \"Unsupported bitmap size\"\n#endif\n\n/*\n * Maximum number of levels possible.  This could be statically computed based\n * on LG_BITMAP_MAXBITS:\n *\n * #define BITMAP_MAX_LEVELS \\\n *     (LG_BITMAP_MAXBITS / LG_SIZEOF_BITMAP) \\\n *     + !!(LG_BITMAP_MAXBITS % LG_SIZEOF_BITMAP)\n *\n * However, that would not allow the generic BITMAP_INFO_INITIALIZER() macro, so\n * instead hardcode BITMAP_MAX_LEVELS to the largest number supported by the\n * various cascading macros.  The only additional cost this incurs is some\n * unused trailing entries in bitmap_info_t structures; the bitmaps themselves\n * are not impacted.\n */\n#define BITMAP_MAX_LEVELS\t5\n\n#define BITMAP_INFO_INITIALIZER(nbits) {\t\t\t\t\\\n\t/* nbits. */\t\t\t\t\t\t\t\\\n\tnbits,\t\t\t\t\t\t\t\t\\\n\t/* nlevels. */\t\t\t\t\t\t\t\\\n\t(BITMAP_GROUPS_L0(nbits) > BITMAP_GROUPS_L1(nbits)) +\t\t\\\n\t    (BITMAP_GROUPS_L1(nbits) > BITMAP_GROUPS_L2(nbits)) +\t\\\n\t    (BITMAP_GROUPS_L2(nbits) > BITMAP_GROUPS_L3(nbits)) +\t\\\n\t    (BITMAP_GROUPS_L3(nbits) > BITMAP_GROUPS_L4(nbits)) + 1,\t\\\n\t/* levels. */\t\t\t\t\t\t\t\\\n\t{\t\t\t\t\t\t\t\t\\\n\t\t{0},\t\t\t\t\t\t\t\\\n\t\t{BITMAP_GROUPS_L0(nbits)},\t\t\t\t\\\n\t\t{BITMAP_GROUPS_L1(nbits) + BITMAP_GROUPS_L0(nbits)},\t\\\n\t\t{BITMAP_GROUPS_L2(nbits) + BITMAP_GROUPS_L1(nbits) +\t\\\n\t\t    BITMAP_GROUPS_L0(nbits)},\t\t\t\t\\\n\t\t{BITMAP_GROUPS_L3(nbits) + BITMAP_GROUPS_L2(nbits) +\t\\\n\t\t    BITMAP_GROUPS_L1(nbits) + BITMAP_GROUPS_L0(nbits)},\t\\\n\t\t{BITMAP_GROUPS_L4(nbits) + BITMAP_GROUPS_L3(nbits) +\t\\\n\t\t     BITMAP_GROUPS_L2(nbits) + BITMAP_GROUPS_L1(nbits)\t\\\n\t\t     + BITMAP_GROUPS_L0(nbits)}\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n}\n\n#else /* BITMAP_USE_TREE */\n\n#define BITMAP_GROUPS(nbits)\tBITMAP_BITS2GROUPS(nbits)\n#define BITMAP_GROUPS_MAX\tBITMAP_BITS2GROUPS(BITMAP_MAXBITS)\n\n#define BITMAP_INFO_INITIALIZER(nbits) {\t\t\t\t\\\n\t/* nbits. */\t\t\t\t\t\t\t\\\n\tnbits,\t\t\t\t\t\t\t\t\\\n\t/* ngroups. */\t\t\t\t\t\t\t\\\n\tBITMAP_BITS2GROUPS(nbits)\t\t\t\t\t\\\n}\n\n#endif /* BITMAP_USE_TREE */\n\ntypedef struct bitmap_level_s {\n\t/* Offset of this level's groups within the array of groups. */\n\tsize_t group_offset;\n} bitmap_level_t;\n\ntypedef struct bitmap_info_s {\n\t/* Logical number of bits in bitmap (stored at bottom level). */\n\tsize_t nbits;\n\n#ifdef BITMAP_USE_TREE\n\t/* Number of levels necessary for nbits. */\n\tunsigned nlevels;\n\n\t/*\n\t * Only the first (nlevels+1) elements are used, and levels are ordered\n\t * bottom to top (e.g. the bottom level is stored in levels[0]).\n\t */\n\tbitmap_level_t levels[BITMAP_MAX_LEVELS+1];\n#else /* BITMAP_USE_TREE */\n\t/* Number of groups necessary for nbits. */\n\tsize_t ngroups;\n#endif /* BITMAP_USE_TREE */\n} bitmap_info_t;\n\nvoid bitmap_info_init(bitmap_info_t *binfo, size_t nbits);\nvoid bitmap_init(bitmap_t *bitmap, const bitmap_info_t *binfo, bool fill);\nsize_t bitmap_size(const bitmap_info_t *binfo);\n\nstatic inline bool\nbitmap_full(bitmap_t *bitmap, const bitmap_info_t *binfo) {\n#ifdef BITMAP_USE_TREE\n\tsize_t rgoff = binfo->levels[binfo->nlevels].group_offset - 1;\n\tbitmap_t rg = bitmap[rgoff];\n\t/* The bitmap is full iff the root group is 0. */\n\treturn (rg == 0);\n#else\n\tsize_t i;\n\n\tfor (i = 0; i < binfo->ngroups; i++) {\n\t\tif (bitmap[i] != 0) {\n\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n#endif\n}\n\nstatic inline bool\nbitmap_get(bitmap_t *bitmap, const bitmap_info_t *binfo, size_t bit) {\n\tsize_t goff;\n\tbitmap_t g;\n\n\tassert(bit < binfo->nbits);\n\tgoff = bit >> LG_BITMAP_GROUP_NBITS;\n\tg = bitmap[goff];\n\treturn !(g & (ZU(1) << (bit & BITMAP_GROUP_NBITS_MASK)));\n}\n\nstatic inline void\nbitmap_set(bitmap_t *bitmap, const bitmap_info_t *binfo, size_t bit) {\n\tsize_t goff;\n\tbitmap_t *gp;\n\tbitmap_t g;\n\n\tassert(bit < binfo->nbits);\n\tassert(!bitmap_get(bitmap, binfo, bit));\n\tgoff = bit >> LG_BITMAP_GROUP_NBITS;\n\tgp = &bitmap[goff];\n\tg = *gp;\n\tassert(g & (ZU(1) << (bit & BITMAP_GROUP_NBITS_MASK)));\n\tg ^= ZU(1) << (bit & BITMAP_GROUP_NBITS_MASK);\n\t*gp = g;\n\tassert(bitmap_get(bitmap, binfo, bit));\n#ifdef BITMAP_USE_TREE\n\t/* Propagate group state transitions up the tree. */\n\tif (g == 0) {\n\t\tunsigned i;\n\t\tfor (i = 1; i < binfo->nlevels; i++) {\n\t\t\tbit = goff;\n\t\t\tgoff = bit >> LG_BITMAP_GROUP_NBITS;\n\t\t\tgp = &bitmap[binfo->levels[i].group_offset + goff];\n\t\t\tg = *gp;\n\t\t\tassert(g & (ZU(1) << (bit & BITMAP_GROUP_NBITS_MASK)));\n\t\t\tg ^= ZU(1) << (bit & BITMAP_GROUP_NBITS_MASK);\n\t\t\t*gp = g;\n\t\t\tif (g != 0) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n#endif\n}\n\n/* ffu: find first unset >= bit. */\nstatic inline size_t\nbitmap_ffu(const bitmap_t *bitmap, const bitmap_info_t *binfo, size_t min_bit) {\n\tassert(min_bit < binfo->nbits);\n\n#ifdef BITMAP_USE_TREE\n\tsize_t bit = 0;\n\tfor (unsigned level = binfo->nlevels; level--;) {\n\t\tsize_t lg_bits_per_group = (LG_BITMAP_GROUP_NBITS * (level +\n\t\t    1));\n\t\tbitmap_t group = bitmap[binfo->levels[level].group_offset + (bit\n\t\t    >> lg_bits_per_group)];\n\t\tunsigned group_nmask = (unsigned)(((min_bit > bit) ? (min_bit -\n\t\t    bit) : 0) >> (lg_bits_per_group - LG_BITMAP_GROUP_NBITS));\n\t\tassert(group_nmask <= BITMAP_GROUP_NBITS);\n\t\tbitmap_t group_mask = ~((1LU << group_nmask) - 1);\n\t\tbitmap_t group_masked = group & group_mask;\n\t\tif (group_masked == 0LU) {\n\t\t\tif (group == 0LU) {\n\t\t\t\treturn binfo->nbits;\n\t\t\t}\n\t\t\t/*\n\t\t\t * min_bit was preceded by one or more unset bits in\n\t\t\t * this group, but there are no other unset bits in this\n\t\t\t * group.  Try again starting at the first bit of the\n\t\t\t * next sibling.  This will recurse at most once per\n\t\t\t * non-root level.\n\t\t\t */\n\t\t\tsize_t sib_base = bit + (ZU(1) << lg_bits_per_group);\n\t\t\tassert(sib_base > min_bit);\n\t\t\tassert(sib_base > bit);\n\t\t\tif (sib_base >= binfo->nbits) {\n\t\t\t\treturn binfo->nbits;\n\t\t\t}\n\t\t\treturn bitmap_ffu(bitmap, binfo, sib_base);\n\t\t}\n\t\tbit += ((size_t)(ffs_lu(group_masked) - 1)) <<\n\t\t    (lg_bits_per_group - LG_BITMAP_GROUP_NBITS);\n\t}\n\tassert(bit >= min_bit);\n\tassert(bit < binfo->nbits);\n\treturn bit;\n#else\n\tsize_t i = min_bit >> LG_BITMAP_GROUP_NBITS;\n\tbitmap_t g = bitmap[i] & ~((1LU << (min_bit & BITMAP_GROUP_NBITS_MASK))\n\t    - 1);\n\tsize_t bit;\n\tdo {\n\t\tbit = ffs_lu(g);\n\t\tif (bit != 0) {\n\t\t\treturn (i << LG_BITMAP_GROUP_NBITS) + (bit - 1);\n\t\t}\n\t\ti++;\n\t\tg = bitmap[i];\n\t} while (i < binfo->ngroups);\n\treturn binfo->nbits;\n#endif\n}\n\n/* sfu: set first unset. */\nstatic inline size_t\nbitmap_sfu(bitmap_t *bitmap, const bitmap_info_t *binfo) {\n\tsize_t bit;\n\tbitmap_t g;\n\tunsigned i;\n\n\tassert(!bitmap_full(bitmap, binfo));\n\n#ifdef BITMAP_USE_TREE\n\ti = binfo->nlevels - 1;\n\tg = bitmap[binfo->levels[i].group_offset];\n\tbit = ffs_lu(g) - 1;\n\twhile (i > 0) {\n\t\ti--;\n\t\tg = bitmap[binfo->levels[i].group_offset + bit];\n\t\tbit = (bit << LG_BITMAP_GROUP_NBITS) + (ffs_lu(g) - 1);\n\t}\n#else\n\ti = 0;\n\tg = bitmap[0];\n\twhile ((bit = ffs_lu(g)) == 0) {\n\t\ti++;\n\t\tg = bitmap[i];\n\t}\n\tbit = (i << LG_BITMAP_GROUP_NBITS) + (bit - 1);\n#endif\n\tbitmap_set(bitmap, binfo, bit);\n\treturn bit;\n}\n\nstatic inline void\nbitmap_unset(bitmap_t *bitmap, const bitmap_info_t *binfo, size_t bit) {\n\tsize_t goff;\n\tbitmap_t *gp;\n\tbitmap_t g;\n\tUNUSED bool propagate;\n\n\tassert(bit < binfo->nbits);\n\tassert(bitmap_get(bitmap, binfo, bit));\n\tgoff = bit >> LG_BITMAP_GROUP_NBITS;\n\tgp = &bitmap[goff];\n\tg = *gp;\n\tpropagate = (g == 0);\n\tassert((g & (ZU(1) << (bit & BITMAP_GROUP_NBITS_MASK))) == 0);\n\tg ^= ZU(1) << (bit & BITMAP_GROUP_NBITS_MASK);\n\t*gp = g;\n\tassert(!bitmap_get(bitmap, binfo, bit));\n#ifdef BITMAP_USE_TREE\n\t/* Propagate group state transitions up the tree. */\n\tif (propagate) {\n\t\tunsigned i;\n\t\tfor (i = 1; i < binfo->nlevels; i++) {\n\t\t\tbit = goff;\n\t\t\tgoff = bit >> LG_BITMAP_GROUP_NBITS;\n\t\t\tgp = &bitmap[binfo->levels[i].group_offset + goff];\n\t\t\tg = *gp;\n\t\t\tpropagate = (g == 0);\n\t\t\tassert((g & (ZU(1) << (bit & BITMAP_GROUP_NBITS_MASK)))\n\t\t\t    == 0);\n\t\t\tg ^= ZU(1) << (bit & BITMAP_GROUP_NBITS_MASK);\n\t\t\t*gp = g;\n\t\t\tif (!propagate) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n#endif /* BITMAP_USE_TREE */\n}\n\n#endif /* JEMALLOC_INTERNAL_BITMAP_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/ckh.h",
    "content": "#ifndef JEMALLOC_INTERNAL_CKH_H\n#define JEMALLOC_INTERNAL_CKH_H\n\n#include \"jemalloc/internal/tsd.h\"\n\n/* Cuckoo hashing implementation.  Skip to the end for the interface. */\n\n/******************************************************************************/\n/* INTERNAL DEFINITIONS -- IGNORE */\n/******************************************************************************/\n\n/* Maintain counters used to get an idea of performance. */\n/* #define CKH_COUNT */\n/* Print counter values in ckh_delete() (requires CKH_COUNT). */\n/* #define CKH_VERBOSE */\n\n/*\n * There are 2^LG_CKH_BUCKET_CELLS cells in each hash table bucket.  Try to fit\n * one bucket per L1 cache line.\n */\n#define LG_CKH_BUCKET_CELLS (LG_CACHELINE - LG_SIZEOF_PTR - 1)\n\n/* Typedefs to allow easy function pointer passing. */\ntypedef void ckh_hash_t (const void *, size_t[2]);\ntypedef bool ckh_keycomp_t (const void *, const void *);\n\n/* Hash table cell. */\ntypedef struct {\n\tconst void *key;\n\tconst void *data;\n} ckhc_t;\n\n/* The hash table itself. */\ntypedef struct {\n#ifdef CKH_COUNT\n\t/* Counters used to get an idea of performance. */\n\tuint64_t ngrows;\n\tuint64_t nshrinks;\n\tuint64_t nshrinkfails;\n\tuint64_t ninserts;\n\tuint64_t nrelocs;\n#endif\n\n\t/* Used for pseudo-random number generation. */\n\tuint64_t prng_state;\n\n\t/* Total number of items. */\n\tsize_t count;\n\n\t/*\n\t * Minimum and current number of hash table buckets.  There are\n\t * 2^LG_CKH_BUCKET_CELLS cells per bucket.\n\t */\n\tunsigned lg_minbuckets;\n\tunsigned lg_curbuckets;\n\n\t/* Hash and comparison functions. */\n\tckh_hash_t *hash;\n\tckh_keycomp_t *keycomp;\n\n\t/* Hash table with 2^lg_curbuckets buckets. */\n\tckhc_t *tab;\n} ckh_t;\n\n/******************************************************************************/\n/* BEGIN PUBLIC API */\n/******************************************************************************/\n\n/* Lifetime management.  Minitems is the initial capacity. */\nbool ckh_new(tsd_t *tsd, ckh_t *ckh, size_t minitems, ckh_hash_t *hash,\n    ckh_keycomp_t *keycomp);\nvoid ckh_delete(tsd_t *tsd, ckh_t *ckh);\n\n/* Get the number of elements in the set. */\nsize_t ckh_count(ckh_t *ckh);\n\n/*\n * To iterate over the elements in the table, initialize *tabind to 0 and call\n * this function until it returns true.  Each call that returns false will\n * update *key and *data to the next element in the table, assuming the pointers\n * are non-NULL.\n */\nbool ckh_iter(ckh_t *ckh, size_t *tabind, void **key, void **data);\n\n/*\n * Basic hash table operations -- insert, removal, lookup.  For ckh_remove and\n * ckh_search, key or data can be NULL.  The hash-table only stores pointers to\n * the key and value, and doesn't do any lifetime management.\n */\nbool ckh_insert(tsd_t *tsd, ckh_t *ckh, const void *key, const void *data);\nbool ckh_remove(tsd_t *tsd, ckh_t *ckh, const void *searchkey, void **key,\n    void **data);\nbool ckh_search(ckh_t *ckh, const void *searchkey, void **key, void **data);\n\n/* Some useful hash and comparison functions for strings and pointers. */\nvoid ckh_string_hash(const void *key, size_t r_hash[2]);\nbool ckh_string_keycomp(const void *k1, const void *k2);\nvoid ckh_pointer_hash(const void *key, size_t r_hash[2]);\nbool ckh_pointer_keycomp(const void *k1, const void *k2);\n\n#endif /* JEMALLOC_INTERNAL_CKH_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/ctl.h",
    "content": "#ifndef JEMALLOC_INTERNAL_CTL_H\n#define JEMALLOC_INTERNAL_CTL_H\n\n#include \"jemalloc/internal/jemalloc_internal_types.h\"\n#include \"jemalloc/internal/malloc_io.h\"\n#include \"jemalloc/internal/mutex_prof.h\"\n#include \"jemalloc/internal/ql.h\"\n#include \"jemalloc/internal/size_classes.h\"\n#include \"jemalloc/internal/stats.h\"\n\n/* Maximum ctl tree depth. */\n#define CTL_MAX_DEPTH\t7\n\ntypedef struct ctl_node_s {\n\tbool named;\n} ctl_node_t;\n\ntypedef struct ctl_named_node_s {\n\tctl_node_t node;\n\tconst char *name;\n\t/* If (nchildren == 0), this is a terminal node. */\n\tsize_t nchildren;\n\tconst ctl_node_t *children;\n\tint (*ctl)(tsd_t *, const size_t *, size_t, void *, size_t *, void *,\n\t    size_t);\n} ctl_named_node_t;\n\ntypedef struct ctl_indexed_node_s {\n\tstruct ctl_node_s node;\n\tconst ctl_named_node_t *(*index)(tsdn_t *, const size_t *, size_t,\n\t    size_t);\n} ctl_indexed_node_t;\n\ntypedef struct ctl_arena_stats_s {\n\tarena_stats_t astats;\n\n\t/* Aggregate stats for small size classes, based on bin stats. */\n\tsize_t allocated_small;\n\tuint64_t nmalloc_small;\n\tuint64_t ndalloc_small;\n\tuint64_t nrequests_small;\n\n\tmalloc_bin_stats_t bstats[NBINS];\n\tmalloc_large_stats_t lstats[NSIZES - NBINS];\n} ctl_arena_stats_t;\n\ntypedef struct ctl_stats_s {\n\tsize_t allocated;\n\tsize_t active;\n\tsize_t metadata;\n\tsize_t resident;\n\tsize_t mapped;\n\tsize_t retained;\n\n\tbackground_thread_stats_t background_thread;\n\tmutex_prof_data_t mutex_prof_data[mutex_prof_num_global_mutexes];\n} ctl_stats_t;\n\ntypedef struct ctl_arena_s ctl_arena_t;\nstruct ctl_arena_s {\n\tunsigned arena_ind;\n\tbool initialized;\n\tql_elm(ctl_arena_t) destroyed_link;\n\n\t/* Basic stats, supported even if !config_stats. */\n\tunsigned nthreads;\n\tconst char *dss;\n\tssize_t dirty_decay_ms;\n\tssize_t muzzy_decay_ms;\n\tsize_t pactive;\n\tsize_t pdirty;\n\tsize_t pmuzzy;\n\n\t/* NULL if !config_stats. */\n\tctl_arena_stats_t *astats;\n};\n\ntypedef struct ctl_arenas_s {\n\tuint64_t epoch;\n\tunsigned narenas;\n\tql_head(ctl_arena_t) destroyed;\n\n\t/*\n\t * Element 0 corresponds to merged stats for extant arenas (accessed via\n\t * MALLCTL_ARENAS_ALL), element 1 corresponds to merged stats for\n\t * destroyed arenas (accessed via MALLCTL_ARENAS_DESTROYED), and the\n\t * remaining MALLOCX_ARENA_LIMIT elements correspond to arenas.\n\t */\n\tctl_arena_t *arenas[2 + MALLOCX_ARENA_LIMIT];\n} ctl_arenas_t;\n\nint ctl_byname(tsd_t *tsd, const char *name, void *oldp, size_t *oldlenp,\n    void *newp, size_t newlen);\nint ctl_nametomib(tsdn_t *tsdn, const char *name, size_t *mibp,\n    size_t *miblenp);\n\nint ctl_bymib(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen);\nbool ctl_boot(void);\nvoid ctl_prefork(tsdn_t *tsdn);\nvoid ctl_postfork_parent(tsdn_t *tsdn);\nvoid ctl_postfork_child(tsdn_t *tsdn);\n\n#define xmallctl(name, oldp, oldlenp, newp, newlen) do {\t\t\\\n\tif (je_mallctl(name, oldp, oldlenp, newp, newlen)\t\t\\\n\t    != 0) {\t\t\t\t\t\t\t\\\n\t\tmalloc_printf(\t\t\t\t\t\t\\\n\t\t    \"<jemalloc>: Failure in xmallctl(\\\"%s\\\", ...)\\n\",\t\\\n\t\t    name);\t\t\t\t\t\t\\\n\t\tabort();\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define xmallctlnametomib(name, mibp, miblenp) do {\t\t\t\\\n\tif (je_mallctlnametomib(name, mibp, miblenp) != 0) {\t\t\\\n\t\tmalloc_printf(\"<jemalloc>: Failure in \"\t\t\t\\\n\t\t    \"xmallctlnametomib(\\\"%s\\\", ...)\\n\", name);\t\t\\\n\t\tabort();\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define xmallctlbymib(mib, miblen, oldp, oldlenp, newp, newlen) do {\t\\\n\tif (je_mallctlbymib(mib, miblen, oldp, oldlenp, newp,\t\t\\\n\t    newlen) != 0) {\t\t\t\t\t\t\\\n\t\tmalloc_write(\t\t\t\t\t\t\\\n\t\t    \"<jemalloc>: Failure in xmallctlbymib()\\n\");\t\\\n\t\tabort();\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#endif /* JEMALLOC_INTERNAL_CTL_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/extent_dss.h",
    "content": "#ifndef JEMALLOC_INTERNAL_EXTENT_DSS_H\n#define JEMALLOC_INTERNAL_EXTENT_DSS_H\n\ntypedef enum {\n\tdss_prec_disabled  = 0,\n\tdss_prec_primary   = 1,\n\tdss_prec_secondary = 2,\n\n\tdss_prec_limit     = 3\n} dss_prec_t;\n#define DSS_PREC_DEFAULT dss_prec_secondary\n#define DSS_DEFAULT \"secondary\"\n\nextern const char *dss_prec_names[];\n\nextern const char *opt_dss;\n\ndss_prec_t extent_dss_prec_get(void);\nbool extent_dss_prec_set(dss_prec_t dss_prec);\nvoid *extent_alloc_dss(tsdn_t *tsdn, arena_t *arena, void *new_addr,\n    size_t size, size_t alignment, bool *zero, bool *commit);\nbool extent_in_dss(void *addr);\nbool extent_dss_mergeable(void *addr_a, void *addr_b);\nvoid extent_dss_boot(void);\n\n#endif /* JEMALLOC_INTERNAL_EXTENT_DSS_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/extent_externs.h",
    "content": "#ifndef JEMALLOC_INTERNAL_EXTENT_EXTERNS_H\n#define JEMALLOC_INTERNAL_EXTENT_EXTERNS_H\n\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/mutex_pool.h\"\n#include \"jemalloc/internal/ph.h\"\n#include \"jemalloc/internal/rb.h\"\n#include \"jemalloc/internal/rtree.h\"\n\nextern rtree_t\t\t\textents_rtree;\nextern const extent_hooks_t\textent_hooks_default;\nextern mutex_pool_t\t\textent_mutex_pool;\n\nextent_t *extent_alloc(tsdn_t *tsdn, arena_t *arena);\nvoid extent_dalloc(tsdn_t *tsdn, arena_t *arena, extent_t *extent);\n\nextent_hooks_t *extent_hooks_get(arena_t *arena);\nextent_hooks_t *extent_hooks_set(tsd_t *tsd, arena_t *arena,\n    extent_hooks_t *extent_hooks);\n\n#ifdef JEMALLOC_JET\nsize_t extent_size_quantize_floor(size_t size);\nsize_t extent_size_quantize_ceil(size_t size);\n#endif\n\nrb_proto(, extent_avail_, extent_tree_t, extent_t)\nph_proto(, extent_heap_, extent_heap_t, extent_t)\n\nbool extents_init(tsdn_t *tsdn, extents_t *extents, extent_state_t state,\n    bool delay_coalesce);\nextent_state_t extents_state_get(const extents_t *extents);\nsize_t extents_npages_get(extents_t *extents);\nextent_t *extents_alloc(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extents_t *extents, void *new_addr,\n    size_t size, size_t pad, size_t alignment, bool slab, szind_t szind,\n    bool *zero, bool *commit);\nvoid extents_dalloc(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extents_t *extents, extent_t *extent);\nextent_t *extents_evict(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extents_t *extents, size_t npages_min);\nvoid extents_prefork(tsdn_t *tsdn, extents_t *extents);\nvoid extents_postfork_parent(tsdn_t *tsdn, extents_t *extents);\nvoid extents_postfork_child(tsdn_t *tsdn, extents_t *extents);\nextent_t *extent_alloc_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, void *new_addr, size_t size, size_t pad,\n    size_t alignment, bool slab, szind_t szind, bool *zero, bool *commit);\nvoid extent_dalloc_gap(tsdn_t *tsdn, arena_t *arena, extent_t *extent);\nvoid extent_dalloc_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent);\nvoid extent_destroy_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent);\nbool extent_commit_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length);\nbool extent_decommit_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length);\nbool extent_purge_lazy_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length);\nbool extent_purge_forced_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length);\nextent_t *extent_split_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t size_a,\n    szind_t szind_a, bool slab_a, size_t size_b, szind_t szind_b, bool slab_b);\nbool extent_merge_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *a, extent_t *b);\n\nbool extent_boot(void);\n\n#endif /* JEMALLOC_INTERNAL_EXTENT_EXTERNS_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/extent_inlines.h",
    "content": "#ifndef JEMALLOC_INTERNAL_EXTENT_INLINES_H\n#define JEMALLOC_INTERNAL_EXTENT_INLINES_H\n\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/mutex_pool.h\"\n#include \"jemalloc/internal/pages.h\"\n#include \"jemalloc/internal/prng.h\"\n#include \"jemalloc/internal/ql.h\"\n#include \"jemalloc/internal/sz.h\"\n\nstatic inline void\nextent_lock(tsdn_t *tsdn, extent_t *extent) {\n\tassert(extent != NULL);\n\tmutex_pool_lock(tsdn, &extent_mutex_pool, (uintptr_t)extent);\n}\n\nstatic inline void\nextent_unlock(tsdn_t *tsdn, extent_t *extent) {\n\tassert(extent != NULL);\n\tmutex_pool_unlock(tsdn, &extent_mutex_pool, (uintptr_t)extent);\n}\n\nstatic inline void\nextent_lock2(tsdn_t *tsdn, extent_t *extent1, extent_t *extent2) {\n\tassert(extent1 != NULL && extent2 != NULL);\n\tmutex_pool_lock2(tsdn, &extent_mutex_pool, (uintptr_t)extent1,\n\t    (uintptr_t)extent2);\n}\n\nstatic inline void\nextent_unlock2(tsdn_t *tsdn, extent_t *extent1, extent_t *extent2) {\n\tassert(extent1 != NULL && extent2 != NULL);\n\tmutex_pool_unlock2(tsdn, &extent_mutex_pool, (uintptr_t)extent1,\n\t    (uintptr_t)extent2);\n}\n\nstatic inline arena_t *\nextent_arena_get(const extent_t *extent) {\n\tunsigned arena_ind = (unsigned)((extent->e_bits &\n\t    EXTENT_BITS_ARENA_MASK) >> EXTENT_BITS_ARENA_SHIFT);\n\t/*\n\t * The following check is omitted because we should never actually read\n\t * a NULL arena pointer.\n\t */\n\tif (false && arena_ind >= MALLOCX_ARENA_LIMIT) {\n\t\treturn NULL;\n\t}\n\tassert(arena_ind < MALLOCX_ARENA_LIMIT);\n\treturn (arena_t *)atomic_load_p(&arenas[arena_ind], ATOMIC_ACQUIRE);\n}\n\nstatic inline szind_t\nextent_szind_get_maybe_invalid(const extent_t *extent) {\n\tszind_t szind = (szind_t)((extent->e_bits & EXTENT_BITS_SZIND_MASK) >>\n\t    EXTENT_BITS_SZIND_SHIFT);\n\tassert(szind <= NSIZES);\n\treturn szind;\n}\n\nstatic inline szind_t\nextent_szind_get(const extent_t *extent) {\n\tszind_t szind = extent_szind_get_maybe_invalid(extent);\n\tassert(szind < NSIZES); /* Never call when \"invalid\". */\n\treturn szind;\n}\n\nstatic inline size_t\nextent_usize_get(const extent_t *extent) {\n\treturn sz_index2size(extent_szind_get(extent));\n}\n\nstatic inline size_t\nextent_sn_get(const extent_t *extent) {\n\treturn (size_t)((extent->e_bits & EXTENT_BITS_SN_MASK) >>\n\t    EXTENT_BITS_SN_SHIFT);\n}\n\nstatic inline extent_state_t\nextent_state_get(const extent_t *extent) {\n\treturn (extent_state_t)((extent->e_bits & EXTENT_BITS_STATE_MASK) >>\n\t    EXTENT_BITS_STATE_SHIFT);\n}\n\nstatic inline bool\nextent_zeroed_get(const extent_t *extent) {\n\treturn (bool)((extent->e_bits & EXTENT_BITS_ZEROED_MASK) >>\n\t    EXTENT_BITS_ZEROED_SHIFT);\n}\n\nstatic inline bool\nextent_committed_get(const extent_t *extent) {\n\treturn (bool)((extent->e_bits & EXTENT_BITS_COMMITTED_MASK) >>\n\t    EXTENT_BITS_COMMITTED_SHIFT);\n}\n\nstatic inline bool\nextent_slab_get(const extent_t *extent) {\n\treturn (bool)((extent->e_bits & EXTENT_BITS_SLAB_MASK) >>\n\t    EXTENT_BITS_SLAB_SHIFT);\n}\n\nstatic inline unsigned\nextent_nfree_get(const extent_t *extent) {\n\tassert(extent_slab_get(extent));\n\treturn (unsigned)((extent->e_bits & EXTENT_BITS_NFREE_MASK) >>\n\t    EXTENT_BITS_NFREE_SHIFT);\n}\n\nstatic inline void *\nextent_base_get(const extent_t *extent) {\n\tassert(extent->e_addr == PAGE_ADDR2BASE(extent->e_addr) ||\n\t    !extent_slab_get(extent));\n\treturn PAGE_ADDR2BASE(extent->e_addr);\n}\n\nstatic inline void *\nextent_addr_get(const extent_t *extent) {\n\tassert(extent->e_addr == PAGE_ADDR2BASE(extent->e_addr) ||\n\t    !extent_slab_get(extent));\n\treturn extent->e_addr;\n}\n\nstatic inline size_t\nextent_size_get(const extent_t *extent) {\n\treturn (extent->e_size_esn & EXTENT_SIZE_MASK);\n}\n\nstatic inline size_t\nextent_esn_get(const extent_t *extent) {\n\treturn (extent->e_size_esn & EXTENT_ESN_MASK);\n}\n\nstatic inline size_t\nextent_bsize_get(const extent_t *extent) {\n\treturn extent->e_bsize;\n}\n\nstatic inline void *\nextent_before_get(const extent_t *extent) {\n\treturn (void *)((uintptr_t)extent_base_get(extent) - PAGE);\n}\n\nstatic inline void *\nextent_last_get(const extent_t *extent) {\n\treturn (void *)((uintptr_t)extent_base_get(extent) +\n\t    extent_size_get(extent) - PAGE);\n}\n\nstatic inline void *\nextent_past_get(const extent_t *extent) {\n\treturn (void *)((uintptr_t)extent_base_get(extent) +\n\t    extent_size_get(extent));\n}\n\nstatic inline arena_slab_data_t *\nextent_slab_data_get(extent_t *extent) {\n\tassert(extent_slab_get(extent));\n\treturn &extent->e_slab_data;\n}\n\nstatic inline const arena_slab_data_t *\nextent_slab_data_get_const(const extent_t *extent) {\n\tassert(extent_slab_get(extent));\n\treturn &extent->e_slab_data;\n}\n\nstatic inline prof_tctx_t *\nextent_prof_tctx_get(const extent_t *extent) {\n\treturn (prof_tctx_t *)atomic_load_p(&extent->e_prof_tctx,\n\t    ATOMIC_ACQUIRE);\n}\n\nstatic inline void\nextent_arena_set(extent_t *extent, arena_t *arena) {\n\tunsigned arena_ind = (arena != NULL) ? arena_ind_get(arena) : ((1U <<\n\t    MALLOCX_ARENA_BITS) - 1);\n\textent->e_bits = (extent->e_bits & ~EXTENT_BITS_ARENA_MASK) |\n\t    ((uint64_t)arena_ind << EXTENT_BITS_ARENA_SHIFT);\n}\n\nstatic inline void\nextent_addr_set(extent_t *extent, void *addr) {\n\textent->e_addr = addr;\n}\n\nstatic inline void\nextent_addr_randomize(tsdn_t *tsdn, extent_t *extent, size_t alignment) {\n\tassert(extent_base_get(extent) == extent_addr_get(extent));\n\n\tif (alignment < PAGE) {\n\t\tunsigned lg_range = LG_PAGE -\n\t\t    lg_floor(CACHELINE_CEILING(alignment));\n\t\tsize_t r =\n\t\t    prng_lg_range_zu(&extent_arena_get(extent)->offset_state,\n\t\t    lg_range, true);\n\t\tuintptr_t random_offset = ((uintptr_t)r) << (LG_PAGE -\n\t\t    lg_range);\n\t\textent->e_addr = (void *)((uintptr_t)extent->e_addr +\n\t\t    random_offset);\n\t\tassert(ALIGNMENT_ADDR2BASE(extent->e_addr, alignment) ==\n\t\t    extent->e_addr);\n\t}\n}\n\nstatic inline void\nextent_size_set(extent_t *extent, size_t size) {\n\tassert((size & ~EXTENT_SIZE_MASK) == 0);\n\textent->e_size_esn = size | (extent->e_size_esn & ~EXTENT_SIZE_MASK);\n}\n\nstatic inline void\nextent_esn_set(extent_t *extent, size_t esn) {\n\textent->e_size_esn = (extent->e_size_esn & ~EXTENT_ESN_MASK) | (esn &\n\t    EXTENT_ESN_MASK);\n}\n\nstatic inline void\nextent_bsize_set(extent_t *extent, size_t bsize) {\n\textent->e_bsize = bsize;\n}\n\nstatic inline void\nextent_szind_set(extent_t *extent, szind_t szind) {\n\tassert(szind <= NSIZES); /* NSIZES means \"invalid\". */\n\textent->e_bits = (extent->e_bits & ~EXTENT_BITS_SZIND_MASK) |\n\t    ((uint64_t)szind << EXTENT_BITS_SZIND_SHIFT);\n}\n\nstatic inline void\nextent_nfree_set(extent_t *extent, unsigned nfree) {\n\tassert(extent_slab_get(extent));\n\textent->e_bits = (extent->e_bits & ~EXTENT_BITS_NFREE_MASK) |\n\t    ((uint64_t)nfree << EXTENT_BITS_NFREE_SHIFT);\n}\n\nstatic inline void\nextent_nfree_inc(extent_t *extent) {\n\tassert(extent_slab_get(extent));\n\textent->e_bits += ((uint64_t)1U << EXTENT_BITS_NFREE_SHIFT);\n}\n\nstatic inline void\nextent_nfree_dec(extent_t *extent) {\n\tassert(extent_slab_get(extent));\n\textent->e_bits -= ((uint64_t)1U << EXTENT_BITS_NFREE_SHIFT);\n}\n\nstatic inline void\nextent_sn_set(extent_t *extent, size_t sn) {\n\textent->e_bits = (extent->e_bits & ~EXTENT_BITS_SN_MASK) |\n\t    ((uint64_t)sn << EXTENT_BITS_SN_SHIFT);\n}\n\nstatic inline void\nextent_state_set(extent_t *extent, extent_state_t state) {\n\textent->e_bits = (extent->e_bits & ~EXTENT_BITS_STATE_MASK) |\n\t    ((uint64_t)state << EXTENT_BITS_STATE_SHIFT);\n}\n\nstatic inline void\nextent_zeroed_set(extent_t *extent, bool zeroed) {\n\textent->e_bits = (extent->e_bits & ~EXTENT_BITS_ZEROED_MASK) |\n\t    ((uint64_t)zeroed << EXTENT_BITS_ZEROED_SHIFT);\n}\n\nstatic inline void\nextent_committed_set(extent_t *extent, bool committed) {\n\textent->e_bits = (extent->e_bits & ~EXTENT_BITS_COMMITTED_MASK) |\n\t    ((uint64_t)committed << EXTENT_BITS_COMMITTED_SHIFT);\n}\n\nstatic inline void\nextent_slab_set(extent_t *extent, bool slab) {\n\textent->e_bits = (extent->e_bits & ~EXTENT_BITS_SLAB_MASK) |\n\t    ((uint64_t)slab << EXTENT_BITS_SLAB_SHIFT);\n}\n\nstatic inline void\nextent_prof_tctx_set(extent_t *extent, prof_tctx_t *tctx) {\n\tatomic_store_p(&extent->e_prof_tctx, tctx, ATOMIC_RELEASE);\n}\n\nstatic inline void\nextent_init(extent_t *extent, arena_t *arena, void *addr, size_t size,\n    bool slab, szind_t szind, size_t sn, extent_state_t state, bool zeroed,\n    bool committed) {\n\tassert(addr == PAGE_ADDR2BASE(addr) || !slab);\n\n\textent_arena_set(extent, arena);\n\textent_addr_set(extent, addr);\n\textent_size_set(extent, size);\n\textent_slab_set(extent, slab);\n\textent_szind_set(extent, szind);\n\textent_sn_set(extent, sn);\n\textent_state_set(extent, state);\n\textent_zeroed_set(extent, zeroed);\n\textent_committed_set(extent, committed);\n\tql_elm_new(extent, ql_link);\n\tif (config_prof) {\n\t\textent_prof_tctx_set(extent, NULL);\n\t}\n}\n\nstatic inline void\nextent_binit(extent_t *extent, void *addr, size_t bsize, size_t sn) {\n\textent_arena_set(extent, NULL);\n\textent_addr_set(extent, addr);\n\textent_bsize_set(extent, bsize);\n\textent_slab_set(extent, false);\n\textent_szind_set(extent, NSIZES);\n\textent_sn_set(extent, sn);\n\textent_state_set(extent, extent_state_active);\n\textent_zeroed_set(extent, true);\n\textent_committed_set(extent, true);\n}\n\nstatic inline void\nextent_list_init(extent_list_t *list) {\n\tql_new(list);\n}\n\nstatic inline extent_t *\nextent_list_first(const extent_list_t *list) {\n\treturn ql_first(list);\n}\n\nstatic inline extent_t *\nextent_list_last(const extent_list_t *list) {\n\treturn ql_last(list, ql_link);\n}\n\nstatic inline void\nextent_list_append(extent_list_t *list, extent_t *extent) {\n\tql_tail_insert(list, extent, ql_link);\n}\n\nstatic inline void\nextent_list_replace(extent_list_t *list, extent_t *to_remove,\n    extent_t *to_insert) {\n\tql_after_insert(to_remove, to_insert, ql_link);\n\tql_remove(list, to_remove, ql_link);\n}\n\nstatic inline void\nextent_list_remove(extent_list_t *list, extent_t *extent) {\n\tql_remove(list, extent, ql_link);\n}\n\nstatic inline int\nextent_sn_comp(const extent_t *a, const extent_t *b) {\n\tsize_t a_sn = extent_sn_get(a);\n\tsize_t b_sn = extent_sn_get(b);\n\n\treturn (a_sn > b_sn) - (a_sn < b_sn);\n}\n\nstatic inline int\nextent_esn_comp(const extent_t *a, const extent_t *b) {\n\tsize_t a_esn = extent_esn_get(a);\n\tsize_t b_esn = extent_esn_get(b);\n\n\treturn (a_esn > b_esn) - (a_esn < b_esn);\n}\n\nstatic inline int\nextent_ad_comp(const extent_t *a, const extent_t *b) {\n\tuintptr_t a_addr = (uintptr_t)extent_addr_get(a);\n\tuintptr_t b_addr = (uintptr_t)extent_addr_get(b);\n\n\treturn (a_addr > b_addr) - (a_addr < b_addr);\n}\n\nstatic inline int\nextent_ead_comp(const extent_t *a, const extent_t *b) {\n\tuintptr_t a_eaddr = (uintptr_t)a;\n\tuintptr_t b_eaddr = (uintptr_t)b;\n\n\treturn (a_eaddr > b_eaddr) - (a_eaddr < b_eaddr);\n}\n\nstatic inline int\nextent_snad_comp(const extent_t *a, const extent_t *b) {\n\tint ret;\n\n\tret = extent_sn_comp(a, b);\n\tif (ret != 0) {\n\t\treturn ret;\n\t}\n\n\tret = extent_ad_comp(a, b);\n\treturn ret;\n}\n\nstatic inline int\nextent_esnead_comp(const extent_t *a, const extent_t *b) {\n\tint ret;\n\n\tret = extent_esn_comp(a, b);\n\tif (ret != 0) {\n\t\treturn ret;\n\t}\n\n\tret = extent_ead_comp(a, b);\n\treturn ret;\n}\n\n#endif /* JEMALLOC_INTERNAL_EXTENT_INLINES_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/extent_mmap.h",
    "content": "#ifndef JEMALLOC_INTERNAL_EXTENT_MMAP_EXTERNS_H\n#define JEMALLOC_INTERNAL_EXTENT_MMAP_EXTERNS_H\n\nextern bool opt_retain;\n\nvoid *extent_alloc_mmap(void *new_addr, size_t size, size_t alignment,\n    bool *zero, bool *commit);\nbool extent_dalloc_mmap(void *addr, size_t size);\n\n#endif /* JEMALLOC_INTERNAL_EXTENT_MMAP_EXTERNS_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/extent_structs.h",
    "content": "#ifndef JEMALLOC_INTERNAL_EXTENT_STRUCTS_H\n#define JEMALLOC_INTERNAL_EXTENT_STRUCTS_H\n\n#include \"jemalloc/internal/atomic.h\"\n#include \"jemalloc/internal/bitmap.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/ql.h\"\n#include \"jemalloc/internal/rb.h\"\n#include \"jemalloc/internal/ph.h\"\n#include \"jemalloc/internal/size_classes.h\"\n\ntypedef enum {\n\textent_state_active   = 0,\n\textent_state_dirty    = 1,\n\textent_state_muzzy    = 2,\n\textent_state_retained = 3\n} extent_state_t;\n\n/* Extent (span of pages).  Use accessor functions for e_* fields. */\nstruct extent_s {\n\t/*\n\t * Bitfield containing several fields:\n\t *\n\t * a: arena_ind\n\t * b: slab\n\t * c: committed\n\t * z: zeroed\n\t * t: state\n\t * i: szind\n\t * f: nfree\n\t * n: sn\n\t *\n\t * nnnnnnnn ... nnnnnfff fffffffi iiiiiiit tzcbaaaa aaaaaaaa\n\t *\n\t * arena_ind: Arena from which this extent came, or all 1 bits if\n\t *            unassociated.\n\t *\n\t * slab: The slab flag indicates whether the extent is used for a slab\n\t *       of small regions.  This helps differentiate small size classes,\n\t *       and it indicates whether interior pointers can be looked up via\n\t *       iealloc().\n\t *\n\t * committed: The committed flag indicates whether physical memory is\n\t *            committed to the extent, whether explicitly or implicitly\n\t *            as on a system that overcommits and satisfies physical\n\t *            memory needs on demand via soft page faults.\n\t *\n\t * zeroed: The zeroed flag is used by extent recycling code to track\n\t *         whether memory is zero-filled.\n\t *\n\t * state: The state flag is an extent_state_t.\n\t *\n\t * szind: The szind flag indicates usable size class index for\n\t *        allocations residing in this extent, regardless of whether the\n\t *        extent is a slab.  Extent size and usable size often differ\n\t *        even for non-slabs, either due to sz_large_pad or promotion of\n\t *        sampled small regions.\n\t *\n\t * nfree: Number of free regions in slab.\n\t *\n\t * sn: Serial number (potentially non-unique).\n\t *\n\t *     Serial numbers may wrap around if !opt_retain, but as long as\n\t *     comparison functions fall back on address comparison for equal\n\t *     serial numbers, stable (if imperfect) ordering is maintained.\n\t *\n\t *     Serial numbers may not be unique even in the absence of\n\t *     wrap-around, e.g. when splitting an extent and assigning the same\n\t *     serial number to both resulting adjacent extents.\n\t */\n\tuint64_t\t\te_bits;\n#define EXTENT_BITS_ARENA_SHIFT\t\t0\n#define EXTENT_BITS_ARENA_MASK \\\n    (((uint64_t)(1U << MALLOCX_ARENA_BITS) - 1) << EXTENT_BITS_ARENA_SHIFT)\n\n#define EXTENT_BITS_SLAB_SHIFT\t\tMALLOCX_ARENA_BITS\n#define EXTENT_BITS_SLAB_MASK \\\n    ((uint64_t)0x1U << EXTENT_BITS_SLAB_SHIFT)\n\n#define EXTENT_BITS_COMMITTED_SHIFT\t(MALLOCX_ARENA_BITS + 1)\n#define EXTENT_BITS_COMMITTED_MASK \\\n    ((uint64_t)0x1U << EXTENT_BITS_COMMITTED_SHIFT)\n\n#define EXTENT_BITS_ZEROED_SHIFT\t(MALLOCX_ARENA_BITS + 2)\n#define EXTENT_BITS_ZEROED_MASK \\\n    ((uint64_t)0x1U << EXTENT_BITS_ZEROED_SHIFT)\n\n#define EXTENT_BITS_STATE_SHIFT\t\t(MALLOCX_ARENA_BITS + 3)\n#define EXTENT_BITS_STATE_MASK \\\n    ((uint64_t)0x3U << EXTENT_BITS_STATE_SHIFT)\n\n#define EXTENT_BITS_SZIND_SHIFT\t\t(MALLOCX_ARENA_BITS + 5)\n#define EXTENT_BITS_SZIND_MASK \\\n    (((uint64_t)(1U << LG_CEIL_NSIZES) - 1) << EXTENT_BITS_SZIND_SHIFT)\n\n#define EXTENT_BITS_NFREE_SHIFT \\\n    (MALLOCX_ARENA_BITS + 5 + LG_CEIL_NSIZES)\n#define EXTENT_BITS_NFREE_MASK \\\n    ((uint64_t)((1U << (LG_SLAB_MAXREGS + 1)) - 1) << EXTENT_BITS_NFREE_SHIFT)\n\n#define EXTENT_BITS_SN_SHIFT \\\n    (MALLOCX_ARENA_BITS + 5 + LG_CEIL_NSIZES + (LG_SLAB_MAXREGS + 1))\n#define EXTENT_BITS_SN_MASK\t\t(UINT64_MAX << EXTENT_BITS_SN_SHIFT)\n\n\t/* Pointer to the extent that this structure is responsible for. */\n\tvoid\t\t\t*e_addr;\n\n\tunion {\n\t\t/*\n\t\t * Extent size and serial number associated with the extent\n\t\t * structure (different than the serial number for the extent at\n\t\t * e_addr).\n\t\t *\n\t\t * ssssssss [...] ssssssss ssssnnnn nnnnnnnn\n\t\t */\n\t\tsize_t\t\t\te_size_esn;\n\t#define EXTENT_SIZE_MASK\t((size_t)~(PAGE-1))\n\t#define EXTENT_ESN_MASK\t\t((size_t)PAGE-1)\n\t\t/* Base extent size, which may not be a multiple of PAGE. */\n\t\tsize_t\t\t\te_bsize;\n\t};\n\n\tunion {\n\t\t/*\n\t\t * List linkage, used by a variety of lists:\n\t\t * - arena_bin_t's slabs_full\n\t\t * - extents_t's LRU\n\t\t * - stashed dirty extents\n\t\t * - arena's large allocations\n\t\t */\n\t\tql_elm(extent_t)\tql_link;\n\t\t/* Red-black tree linkage, used by arena's extent_avail. */\n\t\trb_node(extent_t)\trb_link;\n\t};\n\n\t/* Linkage for per size class sn/address-ordered heaps. */\n\tphn(extent_t)\t\tph_link;\n\n\tunion {\n\t\t/* Small region slab metadata. */\n\t\tarena_slab_data_t\te_slab_data;\n\n\t\t/*\n\t\t * Profile counters, used for large objects.  Points to a\n\t\t * prof_tctx_t.\n\t\t */\n\t\tatomic_p_t\t\te_prof_tctx;\n\t};\n};\ntypedef ql_head(extent_t) extent_list_t;\ntypedef rb_tree(extent_t) extent_tree_t;\ntypedef ph(extent_t) extent_heap_t;\n\n/* Quantized collection of extents, with built-in LRU queue. */\nstruct extents_s {\n\tmalloc_mutex_t\t\tmtx;\n\n\t/*\n\t * Quantized per size class heaps of extents.\n\t *\n\t * Synchronization: mtx.\n\t */\n\textent_heap_t\t\theaps[NPSIZES+1];\n\n\t/*\n\t * Bitmap for which set bits correspond to non-empty heaps.\n\t *\n\t * Synchronization: mtx.\n\t */\n\tbitmap_t\t\tbitmap[BITMAP_GROUPS(NPSIZES+1)];\n\n\t/*\n\t * LRU of all extents in heaps.\n\t *\n\t * Synchronization: mtx.\n\t */\n\textent_list_t\t\tlru;\n\n\t/*\n\t * Page sum for all extents in heaps.\n\t *\n\t * The synchronization here is a little tricky.  Modifications to npages\n\t * must hold mtx, but reads need not (though, a reader who sees npages\n\t * without holding the mutex can't assume anything about the rest of the\n\t * state of the extents_t).\n\t */\n\tatomic_zu_t\t\tnpages;\n\n\t/* All stored extents must be in the same state. */\n\textent_state_t\t\tstate;\n\n\t/*\n\t * If true, delay coalescing until eviction; otherwise coalesce during\n\t * deallocation.\n\t */\n\tbool\t\t\tdelay_coalesce;\n};\n\n#endif /* JEMALLOC_INTERNAL_EXTENT_STRUCTS_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/extent_types.h",
    "content": "#ifndef JEMALLOC_INTERNAL_EXTENT_TYPES_H\n#define JEMALLOC_INTERNAL_EXTENT_TYPES_H\n\ntypedef struct extent_s extent_t;\ntypedef struct extents_s extents_t;\n\n#define EXTENT_HOOKS_INITIALIZER\tNULL\n\n#endif /* JEMALLOC_INTERNAL_EXTENT_TYPES_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/hash.h",
    "content": "#ifndef JEMALLOC_INTERNAL_HASH_H\n#define JEMALLOC_INTERNAL_HASH_H\n\n#include \"jemalloc/internal/assert.h\"\n\n/*\n * The following hash function is based on MurmurHash3, placed into the public\n * domain by Austin Appleby.  See https://github.com/aappleby/smhasher for\n * details.\n */\n\n/******************************************************************************/\n/* Internal implementation. */\nstatic inline uint32_t\nhash_rotl_32(uint32_t x, int8_t r) {\n\treturn ((x << r) | (x >> (32 - r)));\n}\n\nstatic inline uint64_t\nhash_rotl_64(uint64_t x, int8_t r) {\n\treturn ((x << r) | (x >> (64 - r)));\n}\n\nstatic inline uint32_t\nhash_get_block_32(const uint32_t *p, int i) {\n\t/* Handle unaligned read. */\n\tif (unlikely((uintptr_t)p & (sizeof(uint32_t)-1)) != 0) {\n\t\tuint32_t ret;\n\n\t\tmemcpy(&ret, (uint8_t *)(p + i), sizeof(uint32_t));\n\t\treturn ret;\n\t}\n\n\treturn p[i];\n}\n\nstatic inline uint64_t\nhash_get_block_64(const uint64_t *p, int i) {\n\t/* Handle unaligned read. */\n\tif (unlikely((uintptr_t)p & (sizeof(uint64_t)-1)) != 0) {\n\t\tuint64_t ret;\n\n\t\tmemcpy(&ret, (uint8_t *)(p + i), sizeof(uint64_t));\n\t\treturn ret;\n\t}\n\n\treturn p[i];\n}\n\nstatic inline uint32_t\nhash_fmix_32(uint32_t h) {\n\th ^= h >> 16;\n\th *= 0x85ebca6b;\n\th ^= h >> 13;\n\th *= 0xc2b2ae35;\n\th ^= h >> 16;\n\n\treturn h;\n}\n\nstatic inline uint64_t\nhash_fmix_64(uint64_t k) {\n\tk ^= k >> 33;\n\tk *= KQU(0xff51afd7ed558ccd);\n\tk ^= k >> 33;\n\tk *= KQU(0xc4ceb9fe1a85ec53);\n\tk ^= k >> 33;\n\n\treturn k;\n}\n\nstatic inline uint32_t\nhash_x86_32(const void *key, int len, uint32_t seed) {\n\tconst uint8_t *data = (const uint8_t *) key;\n\tconst int nblocks = len / 4;\n\n\tuint32_t h1 = seed;\n\n\tconst uint32_t c1 = 0xcc9e2d51;\n\tconst uint32_t c2 = 0x1b873593;\n\n\t/* body */\n\t{\n\t\tconst uint32_t *blocks = (const uint32_t *) (data + nblocks*4);\n\t\tint i;\n\n\t\tfor (i = -nblocks; i; i++) {\n\t\t\tuint32_t k1 = hash_get_block_32(blocks, i);\n\n\t\t\tk1 *= c1;\n\t\t\tk1 = hash_rotl_32(k1, 15);\n\t\t\tk1 *= c2;\n\n\t\t\th1 ^= k1;\n\t\t\th1 = hash_rotl_32(h1, 13);\n\t\t\th1 = h1*5 + 0xe6546b64;\n\t\t}\n\t}\n\n\t/* tail */\n\t{\n\t\tconst uint8_t *tail = (const uint8_t *) (data + nblocks*4);\n\n\t\tuint32_t k1 = 0;\n\n\t\tswitch (len & 3) {\n\t\tcase 3: k1 ^= tail[2] << 16;\n\t\tcase 2: k1 ^= tail[1] << 8;\n\t\tcase 1: k1 ^= tail[0]; k1 *= c1; k1 = hash_rotl_32(k1, 15);\n\t\t\tk1 *= c2; h1 ^= k1;\n\t\t}\n\t}\n\n\t/* finalization */\n\th1 ^= len;\n\n\th1 = hash_fmix_32(h1);\n\n\treturn h1;\n}\n\nUNUSED static inline void\nhash_x86_128(const void *key, const int len, uint32_t seed,\n    uint64_t r_out[2]) {\n\tconst uint8_t * data = (const uint8_t *) key;\n\tconst int nblocks = len / 16;\n\n\tuint32_t h1 = seed;\n\tuint32_t h2 = seed;\n\tuint32_t h3 = seed;\n\tuint32_t h4 = seed;\n\n\tconst uint32_t c1 = 0x239b961b;\n\tconst uint32_t c2 = 0xab0e9789;\n\tconst uint32_t c3 = 0x38b34ae5;\n\tconst uint32_t c4 = 0xa1e38b93;\n\n\t/* body */\n\t{\n\t\tconst uint32_t *blocks = (const uint32_t *) (data + nblocks*16);\n\t\tint i;\n\n\t\tfor (i = -nblocks; i; i++) {\n\t\t\tuint32_t k1 = hash_get_block_32(blocks, i*4 + 0);\n\t\t\tuint32_t k2 = hash_get_block_32(blocks, i*4 + 1);\n\t\t\tuint32_t k3 = hash_get_block_32(blocks, i*4 + 2);\n\t\t\tuint32_t k4 = hash_get_block_32(blocks, i*4 + 3);\n\n\t\t\tk1 *= c1; k1 = hash_rotl_32(k1, 15); k1 *= c2; h1 ^= k1;\n\n\t\t\th1 = hash_rotl_32(h1, 19); h1 += h2;\n\t\t\th1 = h1*5 + 0x561ccd1b;\n\n\t\t\tk2 *= c2; k2 = hash_rotl_32(k2, 16); k2 *= c3; h2 ^= k2;\n\n\t\t\th2 = hash_rotl_32(h2, 17); h2 += h3;\n\t\t\th2 = h2*5 + 0x0bcaa747;\n\n\t\t\tk3 *= c3; k3 = hash_rotl_32(k3, 17); k3 *= c4; h3 ^= k3;\n\n\t\t\th3 = hash_rotl_32(h3, 15); h3 += h4;\n\t\t\th3 = h3*5 + 0x96cd1c35;\n\n\t\t\tk4 *= c4; k4 = hash_rotl_32(k4, 18); k4 *= c1; h4 ^= k4;\n\n\t\t\th4 = hash_rotl_32(h4, 13); h4 += h1;\n\t\t\th4 = h4*5 + 0x32ac3b17;\n\t\t}\n\t}\n\n\t/* tail */\n\t{\n\t\tconst uint8_t *tail = (const uint8_t *) (data + nblocks*16);\n\t\tuint32_t k1 = 0;\n\t\tuint32_t k2 = 0;\n\t\tuint32_t k3 = 0;\n\t\tuint32_t k4 = 0;\n\n\t\tswitch (len & 15) {\n\t\tcase 15: k4 ^= tail[14] << 16;\n\t\tcase 14: k4 ^= tail[13] << 8;\n\t\tcase 13: k4 ^= tail[12] << 0;\n\t\t\tk4 *= c4; k4 = hash_rotl_32(k4, 18); k4 *= c1; h4 ^= k4;\n\n\t\tcase 12: k3 ^= tail[11] << 24;\n\t\tcase 11: k3 ^= tail[10] << 16;\n\t\tcase 10: k3 ^= tail[ 9] << 8;\n\t\tcase  9: k3 ^= tail[ 8] << 0;\n\t\t     k3 *= c3; k3 = hash_rotl_32(k3, 17); k3 *= c4; h3 ^= k3;\n\n\t\tcase  8: k2 ^= tail[ 7] << 24;\n\t\tcase  7: k2 ^= tail[ 6] << 16;\n\t\tcase  6: k2 ^= tail[ 5] << 8;\n\t\tcase  5: k2 ^= tail[ 4] << 0;\n\t\t\tk2 *= c2; k2 = hash_rotl_32(k2, 16); k2 *= c3; h2 ^= k2;\n\n\t\tcase  4: k1 ^= tail[ 3] << 24;\n\t\tcase  3: k1 ^= tail[ 2] << 16;\n\t\tcase  2: k1 ^= tail[ 1] << 8;\n\t\tcase  1: k1 ^= tail[ 0] << 0;\n\t\t\tk1 *= c1; k1 = hash_rotl_32(k1, 15); k1 *= c2; h1 ^= k1;\n\t\t}\n\t}\n\n\t/* finalization */\n\th1 ^= len; h2 ^= len; h3 ^= len; h4 ^= len;\n\n\th1 += h2; h1 += h3; h1 += h4;\n\th2 += h1; h3 += h1; h4 += h1;\n\n\th1 = hash_fmix_32(h1);\n\th2 = hash_fmix_32(h2);\n\th3 = hash_fmix_32(h3);\n\th4 = hash_fmix_32(h4);\n\n\th1 += h2; h1 += h3; h1 += h4;\n\th2 += h1; h3 += h1; h4 += h1;\n\n\tr_out[0] = (((uint64_t) h2) << 32) | h1;\n\tr_out[1] = (((uint64_t) h4) << 32) | h3;\n}\n\nUNUSED static inline void\nhash_x64_128(const void *key, const int len, const uint32_t seed,\n    uint64_t r_out[2]) {\n\tconst uint8_t *data = (const uint8_t *) key;\n\tconst int nblocks = len / 16;\n\n\tuint64_t h1 = seed;\n\tuint64_t h2 = seed;\n\n\tconst uint64_t c1 = KQU(0x87c37b91114253d5);\n\tconst uint64_t c2 = KQU(0x4cf5ad432745937f);\n\n\t/* body */\n\t{\n\t\tconst uint64_t *blocks = (const uint64_t *) (data);\n\t\tint i;\n\n\t\tfor (i = 0; i < nblocks; i++) {\n\t\t\tuint64_t k1 = hash_get_block_64(blocks, i*2 + 0);\n\t\t\tuint64_t k2 = hash_get_block_64(blocks, i*2 + 1);\n\n\t\t\tk1 *= c1; k1 = hash_rotl_64(k1, 31); k1 *= c2; h1 ^= k1;\n\n\t\t\th1 = hash_rotl_64(h1, 27); h1 += h2;\n\t\t\th1 = h1*5 + 0x52dce729;\n\n\t\t\tk2 *= c2; k2 = hash_rotl_64(k2, 33); k2 *= c1; h2 ^= k2;\n\n\t\t\th2 = hash_rotl_64(h2, 31); h2 += h1;\n\t\t\th2 = h2*5 + 0x38495ab5;\n\t\t}\n\t}\n\n\t/* tail */\n\t{\n\t\tconst uint8_t *tail = (const uint8_t*)(data + nblocks*16);\n\t\tuint64_t k1 = 0;\n\t\tuint64_t k2 = 0;\n\n\t\tswitch (len & 15) {\n\t\tcase 15: k2 ^= ((uint64_t)(tail[14])) << 48;\n\t\tcase 14: k2 ^= ((uint64_t)(tail[13])) << 40;\n\t\tcase 13: k2 ^= ((uint64_t)(tail[12])) << 32;\n\t\tcase 12: k2 ^= ((uint64_t)(tail[11])) << 24;\n\t\tcase 11: k2 ^= ((uint64_t)(tail[10])) << 16;\n\t\tcase 10: k2 ^= ((uint64_t)(tail[ 9])) << 8;\n\t\tcase  9: k2 ^= ((uint64_t)(tail[ 8])) << 0;\n\t\t\tk2 *= c2; k2 = hash_rotl_64(k2, 33); k2 *= c1; h2 ^= k2;\n\n\t\tcase  8: k1 ^= ((uint64_t)(tail[ 7])) << 56;\n\t\tcase  7: k1 ^= ((uint64_t)(tail[ 6])) << 48;\n\t\tcase  6: k1 ^= ((uint64_t)(tail[ 5])) << 40;\n\t\tcase  5: k1 ^= ((uint64_t)(tail[ 4])) << 32;\n\t\tcase  4: k1 ^= ((uint64_t)(tail[ 3])) << 24;\n\t\tcase  3: k1 ^= ((uint64_t)(tail[ 2])) << 16;\n\t\tcase  2: k1 ^= ((uint64_t)(tail[ 1])) << 8;\n\t\tcase  1: k1 ^= ((uint64_t)(tail[ 0])) << 0;\n\t\t\tk1 *= c1; k1 = hash_rotl_64(k1, 31); k1 *= c2; h1 ^= k1;\n\t\t}\n\t}\n\n\t/* finalization */\n\th1 ^= len; h2 ^= len;\n\n\th1 += h2;\n\th2 += h1;\n\n\th1 = hash_fmix_64(h1);\n\th2 = hash_fmix_64(h2);\n\n\th1 += h2;\n\th2 += h1;\n\n\tr_out[0] = h1;\n\tr_out[1] = h2;\n}\n\n/******************************************************************************/\n/* API. */\nstatic inline void\nhash(const void *key, size_t len, const uint32_t seed, size_t r_hash[2]) {\n\tassert(len <= INT_MAX); /* Unfortunate implementation limitation. */\n\n#if (LG_SIZEOF_PTR == 3 && !defined(JEMALLOC_BIG_ENDIAN))\n\thash_x64_128(key, (int)len, seed, (uint64_t *)r_hash);\n#else\n\t{\n\t\tuint64_t hashes[2];\n\t\thash_x86_128(key, (int)len, seed, hashes);\n\t\tr_hash[0] = (size_t)hashes[0];\n\t\tr_hash[1] = (size_t)hashes[1];\n\t}\n#endif\n}\n\n#endif /* JEMALLOC_INTERNAL_HASH_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/hooks.h",
    "content": "#ifndef JEMALLOC_INTERNAL_HOOKS_H\n#define JEMALLOC_INTERNAL_HOOKS_H\n\nextern JEMALLOC_EXPORT void (*hooks_arena_new_hook)();\nextern JEMALLOC_EXPORT void (*hooks_libc_hook)();\n\n#define JEMALLOC_HOOK(fn, hook) ((void)(hook != NULL && (hook(), 0)), fn)\n\n#define open JEMALLOC_HOOK(open, hooks_libc_hook)\n#define read JEMALLOC_HOOK(read, hooks_libc_hook)\n#define write JEMALLOC_HOOK(write, hooks_libc_hook)\n#define readlink JEMALLOC_HOOK(readlink, hooks_libc_hook)\n#define close JEMALLOC_HOOK(close, hooks_libc_hook)\n#define creat JEMALLOC_HOOK(creat, hooks_libc_hook)\n#define secure_getenv JEMALLOC_HOOK(secure_getenv, hooks_libc_hook)\n/* Note that this is undef'd and re-define'd in src/prof.c. */\n#define _Unwind_Backtrace JEMALLOC_HOOK(_Unwind_Backtrace, hooks_libc_hook)\n\n#endif /* JEMALLOC_INTERNAL_HOOKS_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/jemalloc_internal_decls.h",
    "content": "#ifndef JEMALLOC_INTERNAL_DECLS_H\n#define JEMALLOC_INTERNAL_DECLS_H\n\n#include <math.h>\n#ifdef _WIN32\n#  include <windows.h>\n#  include \"msvc_compat/windows_extra.h\"\n\n#else\n#  include <sys/param.h>\n#  include <sys/mman.h>\n#  if !defined(__pnacl__) && !defined(__native_client__)\n#    include <sys/syscall.h>\n#    if !defined(SYS_write) && defined(__NR_write)\n#      define SYS_write __NR_write\n#    endif\n#    if defined(SYS_open) && defined(__aarch64__)\n       /* Android headers may define SYS_open to __NR_open even though\n        * __NR_open may not exist on AArch64 (superseded by __NR_openat). */\n#      undef SYS_open\n#    endif\n#    include <sys/uio.h>\n#  endif\n#  include <pthread.h>\n#  include <signal.h>\n#  ifdef JEMALLOC_OS_UNFAIR_LOCK\n#    include <os/lock.h>\n#  endif\n#  ifdef JEMALLOC_GLIBC_MALLOC_HOOK\n#    include <sched.h>\n#  endif\n#  include <errno.h>\n#  include <sys/time.h>\n#  include <time.h>\n#  ifdef JEMALLOC_HAVE_MACH_ABSOLUTE_TIME\n#    include <mach/mach_time.h>\n#  endif\n#endif\n#include <sys/types.h>\n\n#include <limits.h>\n#ifndef SIZE_T_MAX\n#  define SIZE_T_MAX\tSIZE_MAX\n#endif\n#ifndef SSIZE_MAX\n#  define SSIZE_MAX\t((ssize_t)(SIZE_T_MAX >> 1))\n#endif\n#include <stdarg.h>\n#include <stdbool.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <stdint.h>\n#include <stddef.h>\n#ifndef offsetof\n#  define offsetof(type, member)\t((size_t)&(((type *)NULL)->member))\n#endif\n#include <string.h>\n#include <strings.h>\n#include <ctype.h>\n#ifdef _MSC_VER\n#  include <io.h>\ntypedef intptr_t ssize_t;\n#  define PATH_MAX 1024\n#  define STDERR_FILENO 2\n#  define __func__ __FUNCTION__\n#  ifdef JEMALLOC_HAS_RESTRICT\n#    define restrict __restrict\n#  endif\n/* Disable warnings about deprecated system functions. */\n#  pragma warning(disable: 4996)\n#if _MSC_VER < 1800\nstatic int\nisblank(int c) {\n\treturn (c == '\\t' || c == ' ');\n}\n#endif\n#else\n#  include <unistd.h>\n#endif\n#include <fcntl.h>\n\n#endif /* JEMALLOC_INTERNAL_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/jemalloc_internal_defs.h.in",
    "content": "#ifndef JEMALLOC_INTERNAL_DEFS_H_\n#define JEMALLOC_INTERNAL_DEFS_H_\n/*\n * If JEMALLOC_PREFIX is defined via --with-jemalloc-prefix, it will cause all\n * public APIs to be prefixed.  This makes it possible, with some care, to use\n * multiple allocators simultaneously.\n */\n#undef JEMALLOC_PREFIX\n#undef JEMALLOC_CPREFIX\n\n/*\n * Define overrides for non-standard allocator-related functions if they are\n * present on the system.\n */\n#undef JEMALLOC_OVERRIDE___LIBC_CALLOC\n#undef JEMALLOC_OVERRIDE___LIBC_FREE\n#undef JEMALLOC_OVERRIDE___LIBC_MALLOC\n#undef JEMALLOC_OVERRIDE___LIBC_MEMALIGN\n#undef JEMALLOC_OVERRIDE___LIBC_REALLOC\n#undef JEMALLOC_OVERRIDE___LIBC_VALLOC\n#undef JEMALLOC_OVERRIDE___POSIX_MEMALIGN\n\n/*\n * JEMALLOC_PRIVATE_NAMESPACE is used as a prefix for all library-private APIs.\n * For shared libraries, symbol visibility mechanisms prevent these symbols\n * from being exported, but for static libraries, naming collisions are a real\n * possibility.\n */\n#undef JEMALLOC_PRIVATE_NAMESPACE\n\n/*\n * Hyper-threaded CPUs may need a special instruction inside spin loops in\n * order to yield to another virtual CPU.\n */\n#undef CPU_SPINWAIT\n\n/*\n * Number of significant bits in virtual addresses.  This may be less than the\n * total number of bits in a pointer, e.g. on x64, for which the uppermost 16\n * bits are the same as bit 47.\n */\n#undef LG_VADDR\n\n/* Defined if C11 atomics are available. */\n#undef JEMALLOC_C11_ATOMICS\n\n/* Defined if GCC __atomic atomics are available. */\n#undef JEMALLOC_GCC_ATOMIC_ATOMICS\n\n/* Defined if GCC __sync atomics are available. */\n#undef JEMALLOC_GCC_SYNC_ATOMICS\n\n/*\n * Defined if __sync_add_and_fetch(uint32_t *, uint32_t) and\n * __sync_sub_and_fetch(uint32_t *, uint32_t) are available, despite\n * __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4 not being defined (which means the\n * functions are defined in libgcc instead of being inlines).\n */\n#undef JE_FORCE_SYNC_COMPARE_AND_SWAP_4\n\n/*\n * Defined if __sync_add_and_fetch(uint64_t *, uint64_t) and\n * __sync_sub_and_fetch(uint64_t *, uint64_t) are available, despite\n * __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8 not being defined (which means the\n * functions are defined in libgcc instead of being inlines).\n */\n#undef JE_FORCE_SYNC_COMPARE_AND_SWAP_8\n\n/*\n * Defined if __builtin_clz() and __builtin_clzl() are available.\n */\n#undef JEMALLOC_HAVE_BUILTIN_CLZ\n\n/*\n * Defined if os_unfair_lock_*() functions are available, as provided by Darwin.\n */\n#undef JEMALLOC_OS_UNFAIR_LOCK\n\n/*\n * Defined if OSSpin*() functions are available, as provided by Darwin, and\n * documented in the spinlock(3) manual page.\n */\n#undef JEMALLOC_OSSPIN\n\n/* Defined if syscall(2) is usable. */\n#undef JEMALLOC_USE_SYSCALL\n\n/*\n * Defined if secure_getenv(3) is available.\n */\n#undef JEMALLOC_HAVE_SECURE_GETENV\n\n/*\n * Defined if issetugid(2) is available.\n */\n#undef JEMALLOC_HAVE_ISSETUGID\n\n/* Defined if pthread_atfork(3) is available. */\n#undef JEMALLOC_HAVE_PTHREAD_ATFORK\n\n/*\n * Defined if clock_gettime(CLOCK_MONOTONIC_COARSE, ...) is available.\n */\n#undef JEMALLOC_HAVE_CLOCK_MONOTONIC_COARSE\n\n/*\n * Defined if clock_gettime(CLOCK_MONOTONIC, ...) is available.\n */\n#undef JEMALLOC_HAVE_CLOCK_MONOTONIC\n\n/*\n * Defined if mach_absolute_time() is available.\n */\n#undef JEMALLOC_HAVE_MACH_ABSOLUTE_TIME\n\n/*\n * Defined if _malloc_thread_cleanup() exists.  At least in the case of\n * FreeBSD, pthread_key_create() allocates, which if used during malloc\n * bootstrapping will cause recursion into the pthreads library.  Therefore, if\n * _malloc_thread_cleanup() exists, use it as the basis for thread cleanup in\n * malloc_tsd.\n */\n#undef JEMALLOC_MALLOC_THREAD_CLEANUP\n\n/*\n * Defined if threaded initialization is known to be safe on this platform.\n * Among other things, it must be possible to initialize a mutex without\n * triggering allocation in order for threaded allocation to be safe.\n */\n#undef JEMALLOC_THREADED_INIT\n\n/*\n * Defined if the pthreads implementation defines\n * _pthread_mutex_init_calloc_cb(), in which case the function is used in order\n * to avoid recursive allocation during mutex initialization.\n */\n#undef JEMALLOC_MUTEX_INIT_CB\n\n/* Non-empty if the tls_model attribute is supported. */\n#undef JEMALLOC_TLS_MODEL\n\n/*\n * JEMALLOC_DEBUG enables assertions and other sanity checks, and disables\n * inline functions.\n */\n#undef JEMALLOC_DEBUG\n\n/* JEMALLOC_STATS enables statistics calculation. */\n#undef JEMALLOC_STATS\n\n/* JEMALLOC_PROF enables allocation profiling. */\n#undef JEMALLOC_PROF\n\n/* Use libunwind for profile backtracing if defined. */\n#undef JEMALLOC_PROF_LIBUNWIND\n\n/* Use libgcc for profile backtracing if defined. */\n#undef JEMALLOC_PROF_LIBGCC\n\n/* Use gcc intrinsics for profile backtracing if defined. */\n#undef JEMALLOC_PROF_GCC\n\n/*\n * JEMALLOC_DSS enables use of sbrk(2) to allocate extents from the data storage\n * segment (DSS).\n */\n#undef JEMALLOC_DSS\n\n/* Support memory filling (junk/zero). */\n#undef JEMALLOC_FILL\n\n/* Support utrace(2)-based tracing. */\n#undef JEMALLOC_UTRACE\n\n/* Support optional abort() on OOM. */\n#undef JEMALLOC_XMALLOC\n\n/* Support lazy locking (avoid locking unless a second thread is launched). */\n#undef JEMALLOC_LAZY_LOCK\n\n/*\n * Minimum allocation alignment is 2^LG_QUANTUM bytes (ignoring tiny size\n * classes).\n */\n#undef LG_QUANTUM\n\n/* One page is 2^LG_PAGE bytes. */\n#undef LG_PAGE\n\n/*\n * One huge page is 2^LG_HUGEPAGE bytes.  Note that this is defined even if the\n * system does not explicitly support huge pages; system calls that require\n * explicit huge page support are separately configured.\n */\n#undef LG_HUGEPAGE\n\n/*\n * If defined, adjacent virtual memory mappings with identical attributes\n * automatically coalesce, and they fragment when changes are made to subranges.\n * This is the normal order of things for mmap()/munmap(), but on Windows\n * VirtualAlloc()/VirtualFree() operations must be precisely matched, i.e.\n * mappings do *not* coalesce/fragment.\n */\n#undef JEMALLOC_MAPS_COALESCE\n\n/*\n * If defined, retain memory for later reuse by default rather than using e.g.\n * munmap() to unmap freed extents.  This is enabled on 64-bit Linux because\n * common sequences of mmap()/munmap() calls will cause virtual memory map\n * holes.\n */\n#undef JEMALLOC_RETAIN\n\n/* TLS is used to map arenas and magazine caches to threads. */\n#undef JEMALLOC_TLS\n\n/*\n * Used to mark unreachable code to quiet \"end of non-void\" compiler warnings.\n * Don't use this directly; instead use unreachable() from util.h\n */\n#undef JEMALLOC_INTERNAL_UNREACHABLE\n\n/*\n * ffs*() functions to use for bitmapping.  Don't use these directly; instead,\n * use ffs_*() from util.h.\n */\n#undef JEMALLOC_INTERNAL_FFSLL\n#undef JEMALLOC_INTERNAL_FFSL\n#undef JEMALLOC_INTERNAL_FFS\n\n/*\n * If defined, explicitly attempt to more uniformly distribute large allocation\n * pointer alignments across all cache indices.\n */\n#undef JEMALLOC_CACHE_OBLIVIOUS\n\n/*\n * Darwin (OS X) uses zones to work around Mach-O symbol override shortcomings.\n */\n#undef JEMALLOC_ZONE\n\n/*\n * Methods for determining whether the OS overcommits.\n * JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY: Linux's\n *                                         /proc/sys/vm.overcommit_memory file.\n * JEMALLOC_SYSCTL_VM_OVERCOMMIT: FreeBSD's vm.overcommit sysctl.\n */\n#undef JEMALLOC_SYSCTL_VM_OVERCOMMIT\n#undef JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY\n\n/* Defined if madvise(2) is available. */\n#undef JEMALLOC_HAVE_MADVISE\n\n/*\n * Methods for purging unused pages differ between operating systems.\n *\n *   madvise(..., MADV_FREE) : This marks pages as being unused, such that they\n *                             will be discarded rather than swapped out.\n *   madvise(..., MADV_DONTNEED) : If JEMALLOC_PURGE_MADVISE_DONTNEED_ZEROS is\n *                                 defined, this immediately discards pages,\n *                                 such that new pages will be demand-zeroed if\n *                                 the address region is later touched;\n *                                 otherwise this behaves similarly to\n *                                 MADV_FREE, though typically with higher\n *                                 system overhead.\n */\n#undef JEMALLOC_PURGE_MADVISE_FREE\n#undef JEMALLOC_PURGE_MADVISE_DONTNEED\n#undef JEMALLOC_PURGE_MADVISE_DONTNEED_ZEROS\n\n/*\n * Defined if transparent huge pages (THPs) are supported via the\n * MADV_[NO]HUGEPAGE arguments to madvise(2), and THP support is enabled.\n */\n#undef JEMALLOC_THP\n\n/* Define if operating system has alloca.h header. */\n#undef JEMALLOC_HAS_ALLOCA_H\n\n/* C99 restrict keyword supported. */\n#undef JEMALLOC_HAS_RESTRICT\n\n/* For use by hash code. */\n#undef JEMALLOC_BIG_ENDIAN\n\n/* sizeof(int) == 2^LG_SIZEOF_INT. */\n#undef LG_SIZEOF_INT\n\n/* sizeof(long) == 2^LG_SIZEOF_LONG. */\n#undef LG_SIZEOF_LONG\n\n/* sizeof(long long) == 2^LG_SIZEOF_LONG_LONG. */\n#undef LG_SIZEOF_LONG_LONG\n\n/* sizeof(intmax_t) == 2^LG_SIZEOF_INTMAX_T. */\n#undef LG_SIZEOF_INTMAX_T\n\n/* glibc malloc hooks (__malloc_hook, __realloc_hook, __free_hook). */\n#undef JEMALLOC_GLIBC_MALLOC_HOOK\n\n/* glibc memalign hook. */\n#undef JEMALLOC_GLIBC_MEMALIGN_HOOK\n\n/* pthread support */\n#undef JEMALLOC_HAVE_PTHREAD\n\n/* dlsym() support */\n#undef JEMALLOC_HAVE_DLSYM\n\n/* Adaptive mutex support in pthreads. */\n#undef JEMALLOC_HAVE_PTHREAD_MUTEX_ADAPTIVE_NP\n\n/* GNU specific sched_getcpu support */\n#undef JEMALLOC_HAVE_SCHED_GETCPU\n\n/* GNU specific sched_setaffinity support */\n#undef JEMALLOC_HAVE_SCHED_SETAFFINITY\n\n/*\n * If defined, all the features necessary for background threads are present.\n */\n#undef JEMALLOC_BACKGROUND_THREAD\n\n/*\n * If defined, jemalloc symbols are not exported (doesn't work when\n * JEMALLOC_PREFIX is not defined).\n */\n#undef JEMALLOC_EXPORT\n\n/* config.malloc_conf options string. */\n#undef JEMALLOC_CONFIG_MALLOC_CONF\n\n/* If defined, jemalloc takes the malloc/free/etc. symbol names. */\n#undef JEMALLOC_IS_MALLOC\n\n#endif /* JEMALLOC_INTERNAL_DEFS_H_ */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/jemalloc_internal_externs.h",
    "content": "#ifndef JEMALLOC_INTERNAL_EXTERNS_H\n#define JEMALLOC_INTERNAL_EXTERNS_H\n\n#include \"jemalloc/internal/atomic.h\"\n#include \"jemalloc/internal/size_classes.h\"\n#include \"jemalloc/internal/tsd_types.h\"\n\n/* TSD checks this to set thread local slow state accordingly. */\nextern bool malloc_slow;\n\n/* Run-time options. */\nextern bool opt_abort;\nextern bool opt_abort_conf;\nextern const char *opt_junk;\nextern bool opt_junk_alloc;\nextern bool opt_junk_free;\nextern bool opt_utrace;\nextern bool opt_xmalloc;\nextern bool opt_zero;\nextern unsigned opt_narenas;\n\n/* Number of CPUs. */\nextern unsigned ncpus;\n\n/* Number of arenas used for automatic multiplexing of threads and arenas. */\nextern unsigned narenas_auto;\n\n/*\n * Arenas that are used to service external requests.  Not all elements of the\n * arenas array are necessarily used; arenas are created lazily as needed.\n */\nextern atomic_p_t arenas[];\n\nvoid *a0malloc(size_t size);\nvoid a0dalloc(void *ptr);\nvoid *bootstrap_malloc(size_t size);\nvoid *bootstrap_calloc(size_t num, size_t size);\nvoid bootstrap_free(void *ptr);\nvoid arena_set(unsigned ind, arena_t *arena);\nunsigned narenas_total_get(void);\narena_t *arena_init(tsdn_t *tsdn, unsigned ind, extent_hooks_t *extent_hooks);\narena_tdata_t *arena_tdata_get_hard(tsd_t *tsd, unsigned ind);\narena_t *arena_choose_hard(tsd_t *tsd, bool internal);\nvoid arena_migrate(tsd_t *tsd, unsigned oldind, unsigned newind);\nvoid iarena_cleanup(tsd_t *tsd);\nvoid arena_cleanup(tsd_t *tsd);\nvoid arenas_tdata_cleanup(tsd_t *tsd);\nvoid jemalloc_prefork(void);\nvoid jemalloc_postfork_parent(void);\nvoid jemalloc_postfork_child(void);\nbool malloc_initialized(void);\n\n#endif /* JEMALLOC_INTERNAL_EXTERNS_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/jemalloc_internal_includes.h",
    "content": "#ifndef JEMALLOC_INTERNAL_INCLUDES_H\n#define JEMALLOC_INTERNAL_INCLUDES_H\n\n/*\n * jemalloc can conceptually be broken into components (arena, tcache, etc.),\n * but there are circular dependencies that cannot be broken without\n * substantial performance degradation.\n *\n * Historically, we dealt with this by each header into four sections (types,\n * structs, externs, and inlines), and included each header file multiple times\n * in this file, picking out the portion we want on each pass using the\n * following #defines:\n *   JEMALLOC_H_TYPES   : Preprocessor-defined constants and psuedo-opaque data\n *                        types.\n *   JEMALLOC_H_STRUCTS : Data structures.\n *   JEMALLOC_H_EXTERNS : Extern data declarations and function prototypes.\n *   JEMALLOC_H_INLINES : Inline functions.\n *\n * We're moving toward a world in which the dependencies are explicit; each file\n * will #include the headers it depends on (rather than relying on them being\n * implicitly available via this file including every header file in the\n * project).\n *\n * We're now in an intermediate state: we've broken up the header files to avoid\n * having to include each one multiple times, but have not yet moved the\n * dependency information into the header files (i.e. we still rely on the\n * ordering in this file to ensure all a header's dependencies are available in\n * its translation unit).  Each component is now broken up into multiple header\n * files, corresponding to the sections above (e.g. instead of \"foo.h\", we now\n * have \"foo_types.h\", \"foo_structs.h\", \"foo_externs.h\", \"foo_inlines.h\").\n *\n * Those files which have been converted to explicitly include their\n * inter-component dependencies are now in the initial HERMETIC HEADERS\n * section.  All headers may still rely on jemalloc_preamble.h (which, by fiat,\n * must be included first in every translation unit) for system headers and\n * global jemalloc definitions, however.\n */\n\n/******************************************************************************/\n/* TYPES */\n/******************************************************************************/\n\n#include \"jemalloc/internal/extent_types.h\"\n#include \"jemalloc/internal/base_types.h\"\n#include \"jemalloc/internal/arena_types.h\"\n#include \"jemalloc/internal/tcache_types.h\"\n#include \"jemalloc/internal/prof_types.h\"\n\n/******************************************************************************/\n/* STRUCTS */\n/******************************************************************************/\n\n#include \"jemalloc/internal/arena_structs_a.h\"\n#include \"jemalloc/internal/extent_structs.h\"\n#include \"jemalloc/internal/base_structs.h\"\n#include \"jemalloc/internal/prof_structs.h\"\n#include \"jemalloc/internal/arena_structs_b.h\"\n#include \"jemalloc/internal/tcache_structs.h\"\n#include \"jemalloc/internal/background_thread_structs.h\"\n\n/******************************************************************************/\n/* EXTERNS */\n/******************************************************************************/\n\n#include \"jemalloc/internal/jemalloc_internal_externs.h\"\n#include \"jemalloc/internal/extent_externs.h\"\n#include \"jemalloc/internal/base_externs.h\"\n#include \"jemalloc/internal/arena_externs.h\"\n#include \"jemalloc/internal/large_externs.h\"\n#include \"jemalloc/internal/tcache_externs.h\"\n#include \"jemalloc/internal/prof_externs.h\"\n#include \"jemalloc/internal/background_thread_externs.h\"\n\n/******************************************************************************/\n/* INLINES */\n/******************************************************************************/\n\n#include \"jemalloc/internal/jemalloc_internal_inlines_a.h\"\n#include \"jemalloc/internal/base_inlines.h\"\n/*\n * Include portions of arena code interleaved with tcache code in order to\n * resolve circular dependencies.\n */\n#include \"jemalloc/internal/prof_inlines_a.h\"\n#include \"jemalloc/internal/arena_inlines_a.h\"\n#include \"jemalloc/internal/extent_inlines.h\"\n#include \"jemalloc/internal/jemalloc_internal_inlines_b.h\"\n#include \"jemalloc/internal/tcache_inlines.h\"\n#include \"jemalloc/internal/arena_inlines_b.h\"\n#include \"jemalloc/internal/jemalloc_internal_inlines_c.h\"\n#include \"jemalloc/internal/prof_inlines_b.h\"\n#include \"jemalloc/internal/background_thread_inlines.h\"\n\n#endif /* JEMALLOC_INTERNAL_INCLUDES_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/jemalloc_internal_inlines_a.h",
    "content": "#ifndef JEMALLOC_INTERNAL_INLINES_A_H\n#define JEMALLOC_INTERNAL_INLINES_A_H\n\n#include \"jemalloc/internal/atomic.h\"\n#include \"jemalloc/internal/bit_util.h\"\n#include \"jemalloc/internal/jemalloc_internal_types.h\"\n#include \"jemalloc/internal/size_classes.h\"\n#include \"jemalloc/internal/ticker.h\"\n\nJEMALLOC_ALWAYS_INLINE malloc_cpuid_t\nmalloc_getcpu(void) {\n\tassert(have_percpu_arena);\n#if defined(JEMALLOC_HAVE_SCHED_GETCPU)\n\treturn (malloc_cpuid_t)sched_getcpu();\n#else\n\tnot_reached();\n\treturn -1;\n#endif\n}\n\n/* Return the chosen arena index based on current cpu. */\nJEMALLOC_ALWAYS_INLINE unsigned\npercpu_arena_choose(void) {\n\tassert(have_percpu_arena && PERCPU_ARENA_ENABLED(opt_percpu_arena));\n\n\tmalloc_cpuid_t cpuid = malloc_getcpu();\n\tassert(cpuid >= 0);\n\n\tunsigned arena_ind;\n\tif ((opt_percpu_arena == percpu_arena) || ((unsigned)cpuid < ncpus /\n\t    2)) {\n\t\tarena_ind = cpuid;\n\t} else {\n\t\tassert(opt_percpu_arena == per_phycpu_arena);\n\t\t/* Hyper threads on the same physical CPU share arena. */\n\t\tarena_ind = cpuid - ncpus / 2;\n\t}\n\n\treturn arena_ind;\n}\n\n/* Return the limit of percpu auto arena range, i.e. arenas[0...ind_limit). */\nJEMALLOC_ALWAYS_INLINE unsigned\npercpu_arena_ind_limit(percpu_arena_mode_t mode) {\n\tassert(have_percpu_arena && PERCPU_ARENA_ENABLED(mode));\n\tif (mode == per_phycpu_arena && ncpus > 1) {\n\t\tif (ncpus % 2) {\n\t\t\t/* This likely means a misconfig. */\n\t\t\treturn ncpus / 2 + 1;\n\t\t}\n\t\treturn ncpus / 2;\n\t} else {\n\t\treturn ncpus;\n\t}\n}\n\nstatic inline arena_tdata_t *\narena_tdata_get(tsd_t *tsd, unsigned ind, bool refresh_if_missing) {\n\tarena_tdata_t *tdata;\n\tarena_tdata_t *arenas_tdata = tsd_arenas_tdata_get(tsd);\n\n\tif (unlikely(arenas_tdata == NULL)) {\n\t\t/* arenas_tdata hasn't been initialized yet. */\n\t\treturn arena_tdata_get_hard(tsd, ind);\n\t}\n\tif (unlikely(ind >= tsd_narenas_tdata_get(tsd))) {\n\t\t/*\n\t\t * ind is invalid, cache is old (too small), or tdata to be\n\t\t * initialized.\n\t\t */\n\t\treturn (refresh_if_missing ? arena_tdata_get_hard(tsd, ind) :\n\t\t    NULL);\n\t}\n\n\ttdata = &arenas_tdata[ind];\n\tif (likely(tdata != NULL) || !refresh_if_missing) {\n\t\treturn tdata;\n\t}\n\treturn arena_tdata_get_hard(tsd, ind);\n}\n\nstatic inline arena_t *\narena_get(tsdn_t *tsdn, unsigned ind, bool init_if_missing) {\n\tarena_t *ret;\n\n\tassert(ind < MALLOCX_ARENA_LIMIT);\n\n\tret = (arena_t *)atomic_load_p(&arenas[ind], ATOMIC_ACQUIRE);\n\tif (unlikely(ret == NULL)) {\n\t\tif (init_if_missing) {\n\t\t\tret = arena_init(tsdn, ind,\n\t\t\t    (extent_hooks_t *)&extent_hooks_default);\n\t\t}\n\t}\n\treturn ret;\n}\n\nstatic inline ticker_t *\ndecay_ticker_get(tsd_t *tsd, unsigned ind) {\n\tarena_tdata_t *tdata;\n\n\ttdata = arena_tdata_get(tsd, ind, true);\n\tif (unlikely(tdata == NULL)) {\n\t\treturn NULL;\n\t}\n\treturn &tdata->decay_ticker;\n}\n\nJEMALLOC_ALWAYS_INLINE tcache_bin_t *\ntcache_small_bin_get(tcache_t *tcache, szind_t binind) {\n\tassert(binind < NBINS);\n\treturn &tcache->tbins_small[binind];\n}\n\nJEMALLOC_ALWAYS_INLINE tcache_bin_t *\ntcache_large_bin_get(tcache_t *tcache, szind_t binind) {\n\tassert(binind >= NBINS &&binind < nhbins);\n\treturn &tcache->tbins_large[binind - NBINS];\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntcache_available(tsd_t *tsd) {\n\t/*\n\t * Thread specific auto tcache might be unavailable if: 1) during tcache\n\t * initialization, or 2) disabled through thread.tcache.enabled mallctl\n\t * or config options.  This check covers all cases.\n\t */\n\tif (likely(tsd_tcache_enabled_get(tsd))) {\n\t\t/* Associated arena == NULL implies tcache init in progress. */\n\t\tassert(tsd_tcachep_get(tsd)->arena == NULL ||\n\t\t    tcache_small_bin_get(tsd_tcachep_get(tsd), 0)->avail !=\n\t\t    NULL);\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\nJEMALLOC_ALWAYS_INLINE tcache_t *\ntcache_get(tsd_t *tsd) {\n\tif (!tcache_available(tsd)) {\n\t\treturn NULL;\n\t}\n\n\treturn tsd_tcachep_get(tsd);\n}\n\nstatic inline void\npre_reentrancy(tsd_t *tsd) {\n\tbool fast = tsd_fast(tsd);\n\t++*tsd_reentrancy_levelp_get(tsd);\n\tif (fast) {\n\t\t/* Prepare slow path for reentrancy. */\n\t\ttsd_slow_update(tsd);\n\t\tassert(tsd->state == tsd_state_nominal_slow);\n\t}\n}\n\nstatic inline void\npost_reentrancy(tsd_t *tsd) {\n\tint8_t *reentrancy_level = tsd_reentrancy_levelp_get(tsd);\n\tassert(*reentrancy_level > 0);\n\tif (--*reentrancy_level == 0) {\n\t\ttsd_slow_update(tsd);\n\t}\n}\n\n#endif /* JEMALLOC_INTERNAL_INLINES_A_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/jemalloc_internal_inlines_b.h",
    "content": "#ifndef JEMALLOC_INTERNAL_INLINES_B_H\n#define JEMALLOC_INTERNAL_INLINES_B_H\n\n#include \"jemalloc/internal/rtree.h\"\n\n/* Choose an arena based on a per-thread value. */\nstatic inline arena_t *\narena_choose_impl(tsd_t *tsd, arena_t *arena, bool internal) {\n\tarena_t *ret;\n\n\tif (arena != NULL) {\n\t\treturn arena;\n\t}\n\n\t/* During reentrancy, arena 0 is the safest bet. */\n\tif (unlikely(tsd_reentrancy_level_get(tsd) > 0)) {\n\t\treturn arena_get(tsd_tsdn(tsd), 0, true);\n\t}\n\n\tret = internal ? tsd_iarena_get(tsd) : tsd_arena_get(tsd);\n\tif (unlikely(ret == NULL)) {\n\t\tret = arena_choose_hard(tsd, internal);\n\t\tassert(ret);\n\t\tif (tcache_available(tsd)) {\n\t\t\ttcache_t *tcache = tcache_get(tsd);\n\t\t\tif (tcache->arena != NULL) {\n\t\t\t\t/* See comments in tcache_data_init().*/\n\t\t\t\tassert(tcache->arena ==\n\t\t\t\t    arena_get(tsd_tsdn(tsd), 0, false));\n\t\t\t\tif (tcache->arena != ret) {\n\t\t\t\t\ttcache_arena_reassociate(tsd_tsdn(tsd),\n\t\t\t\t\t    tcache, ret);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\ttcache_arena_associate(tsd_tsdn(tsd), tcache,\n\t\t\t\t    ret);\n\t\t\t}\n\t\t}\n\t}\n\n\t/*\n\t * Note that for percpu arena, if the current arena is outside of the\n\t * auto percpu arena range, (i.e. thread is assigned to a manually\n\t * managed arena), then percpu arena is skipped.\n\t */\n\tif (have_percpu_arena && PERCPU_ARENA_ENABLED(opt_percpu_arena) &&\n\t    !internal && (arena_ind_get(ret) <\n\t    percpu_arena_ind_limit(opt_percpu_arena)) && (ret->last_thd !=\n\t    tsd_tsdn(tsd))) {\n\t\tunsigned ind = percpu_arena_choose();\n\t\tif (arena_ind_get(ret) != ind) {\n\t\t\tpercpu_arena_update(tsd, ind);\n\t\t\tret = tsd_arena_get(tsd);\n\t\t}\n\t\tret->last_thd = tsd_tsdn(tsd);\n\t}\n\n\treturn ret;\n}\n\nstatic inline arena_t *\narena_choose(tsd_t *tsd, arena_t *arena) {\n\treturn arena_choose_impl(tsd, arena, false);\n}\n\nstatic inline arena_t *\narena_ichoose(tsd_t *tsd, arena_t *arena) {\n\treturn arena_choose_impl(tsd, arena, true);\n}\n\nstatic inline bool\narena_is_auto(arena_t *arena) {\n\tassert(narenas_auto > 0);\n\treturn (arena_ind_get(arena) < narenas_auto);\n}\n\nJEMALLOC_ALWAYS_INLINE extent_t *\niealloc(tsdn_t *tsdn, const void *ptr) {\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\n\treturn rtree_extent_read(tsdn, &extents_rtree, rtree_ctx,\n\t    (uintptr_t)ptr, true);\n}\n\n#endif /* JEMALLOC_INTERNAL_INLINES_B_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/jemalloc_internal_inlines_c.h",
    "content": "#ifndef JEMALLOC_INTERNAL_INLINES_C_H\n#define JEMALLOC_INTERNAL_INLINES_C_H\n\n#include \"jemalloc/internal/jemalloc_internal_types.h\"\n#include \"jemalloc/internal/sz.h\"\n#include \"jemalloc/internal/witness.h\"\n\nJEMALLOC_ALWAYS_INLINE arena_t *\niaalloc(tsdn_t *tsdn, const void *ptr) {\n\tassert(ptr != NULL);\n\n\treturn arena_aalloc(tsdn, ptr);\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\nisalloc(tsdn_t *tsdn, const void *ptr) {\n\tassert(ptr != NULL);\n\n\treturn arena_salloc(tsdn, ptr);\n}\n\nJEMALLOC_ALWAYS_INLINE void *\niallocztm(tsdn_t *tsdn, size_t size, szind_t ind, bool zero, tcache_t *tcache,\n    bool is_internal, arena_t *arena, bool slow_path) {\n\tvoid *ret;\n\n\tassert(size != 0);\n\tassert(!is_internal || tcache == NULL);\n\tassert(!is_internal || arena == NULL || arena_is_auto(arena));\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\tret = arena_malloc(tsdn, arena, size, ind, zero, tcache, slow_path);\n\tif (config_stats && is_internal && likely(ret != NULL)) {\n\t\tarena_internal_add(iaalloc(tsdn, ret), isalloc(tsdn, ret));\n\t}\n\treturn ret;\n}\n\nJEMALLOC_ALWAYS_INLINE void *\nialloc(tsd_t *tsd, size_t size, szind_t ind, bool zero, bool slow_path) {\n\treturn iallocztm(tsd_tsdn(tsd), size, ind, zero, tcache_get(tsd), false,\n\t    NULL, slow_path);\n}\n\nJEMALLOC_ALWAYS_INLINE void *\nipallocztm(tsdn_t *tsdn, size_t usize, size_t alignment, bool zero,\n    tcache_t *tcache, bool is_internal, arena_t *arena) {\n\tvoid *ret;\n\n\tassert(usize != 0);\n\tassert(usize == sz_sa2u(usize, alignment));\n\tassert(!is_internal || tcache == NULL);\n\tassert(!is_internal || arena == NULL || arena_is_auto(arena));\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\tret = arena_palloc(tsdn, arena, usize, alignment, zero, tcache);\n\tassert(ALIGNMENT_ADDR2BASE(ret, alignment) == ret);\n\tif (config_stats && is_internal && likely(ret != NULL)) {\n\t\tarena_internal_add(iaalloc(tsdn, ret), isalloc(tsdn, ret));\n\t}\n\treturn ret;\n}\n\nJEMALLOC_ALWAYS_INLINE void *\nipalloct(tsdn_t *tsdn, size_t usize, size_t alignment, bool zero,\n    tcache_t *tcache, arena_t *arena) {\n\treturn ipallocztm(tsdn, usize, alignment, zero, tcache, false, arena);\n}\n\nJEMALLOC_ALWAYS_INLINE void *\nipalloc(tsd_t *tsd, size_t usize, size_t alignment, bool zero) {\n\treturn ipallocztm(tsd_tsdn(tsd), usize, alignment, zero,\n\t    tcache_get(tsd), false, NULL);\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\nivsalloc(tsdn_t *tsdn, const void *ptr) {\n\treturn arena_vsalloc(tsdn, ptr);\n}\n\nJEMALLOC_ALWAYS_INLINE void\nidalloctm(tsdn_t *tsdn, void *ptr, tcache_t *tcache, alloc_ctx_t *alloc_ctx,\n    bool is_internal, bool slow_path) {\n\tassert(ptr != NULL);\n\tassert(!is_internal || tcache == NULL);\n\tassert(!is_internal || arena_is_auto(iaalloc(tsdn, ptr)));\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\tif (config_stats && is_internal) {\n\t\tarena_internal_sub(iaalloc(tsdn, ptr), isalloc(tsdn, ptr));\n\t}\n\tif (!is_internal && tsd_reentrancy_level_get(tsdn_tsd(tsdn)) != 0) {\n\t\tassert(tcache == NULL);\n\t}\n\tarena_dalloc(tsdn, ptr, tcache, alloc_ctx, slow_path);\n}\n\nJEMALLOC_ALWAYS_INLINE void\nidalloc(tsd_t *tsd, void *ptr) {\n\tidalloctm(tsd_tsdn(tsd), ptr, tcache_get(tsd), NULL, false, true);\n}\n\nJEMALLOC_ALWAYS_INLINE void\nisdalloct(tsdn_t *tsdn, void *ptr, size_t size, tcache_t *tcache,\n    alloc_ctx_t *alloc_ctx, bool slow_path) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\tarena_sdalloc(tsdn, ptr, size, tcache, alloc_ctx, slow_path);\n}\n\nJEMALLOC_ALWAYS_INLINE void *\niralloct_realign(tsdn_t *tsdn, void *ptr, size_t oldsize, size_t size,\n    size_t extra, size_t alignment, bool zero, tcache_t *tcache,\n    arena_t *arena) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\tvoid *p;\n\tsize_t usize, copysize;\n\n\tusize = sz_sa2u(size + extra, alignment);\n\tif (unlikely(usize == 0 || usize > LARGE_MAXCLASS)) {\n\t\treturn NULL;\n\t}\n\tp = ipalloct(tsdn, usize, alignment, zero, tcache, arena);\n\tif (p == NULL) {\n\t\tif (extra == 0) {\n\t\t\treturn NULL;\n\t\t}\n\t\t/* Try again, without extra this time. */\n\t\tusize = sz_sa2u(size, alignment);\n\t\tif (unlikely(usize == 0 || usize > LARGE_MAXCLASS)) {\n\t\t\treturn NULL;\n\t\t}\n\t\tp = ipalloct(tsdn, usize, alignment, zero, tcache, arena);\n\t\tif (p == NULL) {\n\t\t\treturn NULL;\n\t\t}\n\t}\n\t/*\n\t * Copy at most size bytes (not size+extra), since the caller has no\n\t * expectation that the extra bytes will be reliably preserved.\n\t */\n\tcopysize = (size < oldsize) ? size : oldsize;\n\tmemcpy(p, ptr, copysize);\n\tisdalloct(tsdn, ptr, oldsize, tcache, NULL, true);\n\treturn p;\n}\n\nJEMALLOC_ALWAYS_INLINE void *\niralloct(tsdn_t *tsdn, void *ptr, size_t oldsize, size_t size, size_t alignment,\n    bool zero, tcache_t *tcache, arena_t *arena) {\n\tassert(ptr != NULL);\n\tassert(size != 0);\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\tif (alignment != 0 && ((uintptr_t)ptr & ((uintptr_t)alignment-1))\n\t    != 0) {\n\t\t/*\n\t\t * Existing object alignment is inadequate; allocate new space\n\t\t * and copy.\n\t\t */\n\t\treturn iralloct_realign(tsdn, ptr, oldsize, size, 0, alignment,\n\t\t    zero, tcache, arena);\n\t}\n\n\treturn arena_ralloc(tsdn, arena, ptr, oldsize, size, alignment, zero,\n\t    tcache);\n}\n\nJEMALLOC_ALWAYS_INLINE void *\niralloc(tsd_t *tsd, void *ptr, size_t oldsize, size_t size, size_t alignment,\n    bool zero) {\n\treturn iralloct(tsd_tsdn(tsd), ptr, oldsize, size, alignment, zero,\n\t    tcache_get(tsd), NULL);\n}\n\nJEMALLOC_ALWAYS_INLINE bool\nixalloc(tsdn_t *tsdn, void *ptr, size_t oldsize, size_t size, size_t extra,\n    size_t alignment, bool zero) {\n\tassert(ptr != NULL);\n\tassert(size != 0);\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\tif (alignment != 0 && ((uintptr_t)ptr & ((uintptr_t)alignment-1))\n\t    != 0) {\n\t\t/* Existing object alignment is inadequate. */\n\t\treturn true;\n\t}\n\n\treturn arena_ralloc_no_move(tsdn, ptr, oldsize, size, extra, zero);\n}\n\nJEMALLOC_ALWAYS_INLINE int\niget_defrag_hint(tsdn_t *tsdn, void* ptr, int *bin_util, int *run_util) {\n\tint defrag = 0;\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\tszind_t szind;\n\tbool is_slab;\n\trtree_szind_slab_read(tsdn, &extents_rtree, rtree_ctx, (uintptr_t)ptr, true, &szind, &is_slab);\n\tif (likely(is_slab)) {\n\t\t/* Small allocation. */\n\t\textent_t *slab = iealloc(tsdn, ptr);\n\t\tarena_t *arena = extent_arena_get(slab);\n\t\tszind_t binind = extent_szind_get(slab);\n\t\tarena_bin_t *bin = &arena->bins[binind];\n\t\tmalloc_mutex_lock(tsdn, &bin->lock);\n\t\t/* don't bother moving allocations from the slab currently used for new allocations */\n\t\tif (slab != bin->slabcur) {\n\t\t\tconst arena_bin_info_t *bin_info = &arena_bin_info[binind];\n\t\t\tsize_t availregs = bin_info->nregs * bin->stats.curslabs;\n\t\t\t*bin_util = ((long long)bin->stats.curregs<<16) / availregs;\n\t\t\t*run_util = ((long long)(bin_info->nregs - extent_nfree_get(slab))<<16) / bin_info->nregs;\n\t\t\tdefrag = 1;\n\t\t}\n\t\tmalloc_mutex_unlock(tsdn, &bin->lock);\n\t}\n\treturn defrag;\n}\n\n#endif /* JEMALLOC_INTERNAL_INLINES_C_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/jemalloc_internal_macros.h",
    "content": "#ifndef JEMALLOC_INTERNAL_MACROS_H\n#define JEMALLOC_INTERNAL_MACROS_H\n\n#ifdef JEMALLOC_DEBUG\n#  define JEMALLOC_ALWAYS_INLINE static inline\n#else\n#  define JEMALLOC_ALWAYS_INLINE JEMALLOC_ATTR(always_inline) static inline\n#endif\n#ifdef _MSC_VER\n#  define inline _inline\n#endif\n\n#define UNUSED JEMALLOC_ATTR(unused)\n\n#define ZU(z)\t((size_t)z)\n#define ZD(z)\t((ssize_t)z)\n#define QU(q)\t((uint64_t)q)\n#define QD(q)\t((int64_t)q)\n\n#define KZU(z)\tZU(z##ULL)\n#define KZD(z)\tZD(z##LL)\n#define KQU(q)\tQU(q##ULL)\n#define KQD(q)\tQI(q##LL)\n\n#ifndef __DECONST\n#  define\t__DECONST(type, var)\t((type)(uintptr_t)(const void *)(var))\n#endif\n\n#if !defined(JEMALLOC_HAS_RESTRICT) || defined(__cplusplus)\n#  define restrict\n#endif\n\n/* Various function pointers are statick and immutable except during testing. */\n#ifdef JEMALLOC_JET\n#  define JET_MUTABLE\n#else\n#  define JET_MUTABLE const\n#endif\n\n#endif /* JEMALLOC_INTERNAL_MACROS_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/jemalloc_internal_types.h",
    "content": "#ifndef JEMALLOC_INTERNAL_TYPES_H\n#define JEMALLOC_INTERNAL_TYPES_H\n\n/* Page size index type. */\ntypedef unsigned pszind_t;\n\n/* Size class index type. */\ntypedef unsigned szind_t;\n\n/* Processor / core id type. */\ntypedef int malloc_cpuid_t;\n\n/*\n * Flags bits:\n *\n * a: arena\n * t: tcache\n * 0: unused\n * z: zero\n * n: alignment\n *\n * aaaaaaaa aaaatttt tttttttt 0znnnnnn\n */\n#define MALLOCX_ARENA_BITS\t12\n#define MALLOCX_TCACHE_BITS\t12\n#define MALLOCX_LG_ALIGN_BITS\t6\n#define MALLOCX_ARENA_SHIFT\t20\n#define MALLOCX_TCACHE_SHIFT\t8\n#define MALLOCX_ARENA_MASK \\\n    (((1 << MALLOCX_ARENA_BITS) - 1) << MALLOCX_ARENA_SHIFT)\n/* NB: Arena index bias decreases the maximum number of arenas by 1. */\n#define MALLOCX_ARENA_LIMIT\t((1 << MALLOCX_ARENA_BITS) - 1)\n#define MALLOCX_TCACHE_MASK \\\n    (((1 << MALLOCX_TCACHE_BITS) - 1) << MALLOCX_TCACHE_SHIFT)\n#define MALLOCX_TCACHE_MAX\t((1 << MALLOCX_TCACHE_BITS) - 3)\n#define MALLOCX_LG_ALIGN_MASK\t((1 << MALLOCX_LG_ALIGN_BITS) - 1)\n/* Use MALLOCX_ALIGN_GET() if alignment may not be specified in flags. */\n#define MALLOCX_ALIGN_GET_SPECIFIED(flags)\t\t\t\t\\\n    (ZU(1) << (flags & MALLOCX_LG_ALIGN_MASK))\n#define MALLOCX_ALIGN_GET(flags)\t\t\t\t\t\\\n    (MALLOCX_ALIGN_GET_SPECIFIED(flags) & (SIZE_T_MAX-1))\n#define MALLOCX_ZERO_GET(flags)\t\t\t\t\t\t\\\n    ((bool)(flags & MALLOCX_ZERO))\n\n#define MALLOCX_TCACHE_GET(flags)\t\t\t\t\t\\\n    (((unsigned)((flags & MALLOCX_TCACHE_MASK) >> MALLOCX_TCACHE_SHIFT)) - 2)\n#define MALLOCX_ARENA_GET(flags)\t\t\t\t\t\\\n    (((unsigned)(((unsigned)flags) >> MALLOCX_ARENA_SHIFT)) - 1)\n\n/* Smallest size class to support. */\n#define TINY_MIN\t\t(1U << LG_TINY_MIN)\n\n/*\n * Minimum allocation alignment is 2^LG_QUANTUM bytes (ignoring tiny size\n * classes).\n */\n#ifndef LG_QUANTUM\n#  if (defined(__i386__) || defined(_M_IX86))\n#    define LG_QUANTUM\t\t4\n#  endif\n#  ifdef __ia64__\n#    define LG_QUANTUM\t\t4\n#  endif\n#  ifdef __alpha__\n#    define LG_QUANTUM\t\t4\n#  endif\n#  if (defined(__sparc64__) || defined(__sparcv9) || defined(__sparc_v9__))\n#    define LG_QUANTUM\t\t4\n#  endif\n#  if (defined(__amd64__) || defined(__x86_64__) || defined(_M_X64))\n#    define LG_QUANTUM\t\t4\n#  endif\n#  ifdef __arm__\n#    define LG_QUANTUM\t\t3\n#  endif\n#  ifdef __aarch64__\n#    define LG_QUANTUM\t\t4\n#  endif\n#  ifdef __hppa__\n#    define LG_QUANTUM\t\t4\n#  endif\n#  ifdef __mips__\n#    define LG_QUANTUM\t\t3\n#  endif\n#  ifdef __or1k__\n#    define LG_QUANTUM\t\t3\n#  endif\n#  ifdef __powerpc__\n#    define LG_QUANTUM\t\t4\n#  endif\n#  ifdef __riscv__\n#    define LG_QUANTUM\t\t4\n#  endif\n#  ifdef __s390__\n#    define LG_QUANTUM\t\t4\n#  endif\n#  ifdef __SH4__\n#    define LG_QUANTUM\t\t4\n#  endif\n#  ifdef __tile__\n#    define LG_QUANTUM\t\t4\n#  endif\n#  ifdef __le32__\n#    define LG_QUANTUM\t\t4\n#  endif\n#  ifndef LG_QUANTUM\n#    error \"Unknown minimum alignment for architecture; specify via \"\n\t \"--with-lg-quantum\"\n#  endif\n#endif\n\n#define QUANTUM\t\t\t((size_t)(1U << LG_QUANTUM))\n#define QUANTUM_MASK\t\t(QUANTUM - 1)\n\n/* Return the smallest quantum multiple that is >= a. */\n#define QUANTUM_CEILING(a)\t\t\t\t\t\t\\\n\t(((a) + QUANTUM_MASK) & ~QUANTUM_MASK)\n\n#define LONG\t\t\t((size_t)(1U << LG_SIZEOF_LONG))\n#define LONG_MASK\t\t(LONG - 1)\n\n/* Return the smallest long multiple that is >= a. */\n#define LONG_CEILING(a)\t\t\t\t\t\t\t\\\n\t(((a) + LONG_MASK) & ~LONG_MASK)\n\n#define SIZEOF_PTR\t\t(1U << LG_SIZEOF_PTR)\n#define PTR_MASK\t\t(SIZEOF_PTR - 1)\n\n/* Return the smallest (void *) multiple that is >= a. */\n#define PTR_CEILING(a)\t\t\t\t\t\t\t\\\n\t(((a) + PTR_MASK) & ~PTR_MASK)\n\n/*\n * Maximum size of L1 cache line.  This is used to avoid cache line aliasing.\n * In addition, this controls the spacing of cacheline-spaced size classes.\n *\n * CACHELINE cannot be based on LG_CACHELINE because __declspec(align()) can\n * only handle raw constants.\n */\n#define LG_CACHELINE\t\t6\n#define CACHELINE\t\t64\n#define CACHELINE_MASK\t\t(CACHELINE - 1)\n\n/* Return the smallest cacheline multiple that is >= s. */\n#define CACHELINE_CEILING(s)\t\t\t\t\t\t\\\n\t(((s) + CACHELINE_MASK) & ~CACHELINE_MASK)\n\n/* Return the nearest aligned address at or below a. */\n#define ALIGNMENT_ADDR2BASE(a, alignment)\t\t\t\t\\\n\t((void *)((uintptr_t)(a) & ((~(alignment)) + 1)))\n\n/* Return the offset between a and the nearest aligned address at or below a. */\n#define ALIGNMENT_ADDR2OFFSET(a, alignment)\t\t\t\t\\\n\t((size_t)((uintptr_t)(a) & (alignment - 1)))\n\n/* Return the smallest alignment multiple that is >= s. */\n#define ALIGNMENT_CEILING(s, alignment)\t\t\t\t\t\\\n\t(((s) + (alignment - 1)) & ((~(alignment)) + 1))\n\n/* Declare a variable-length array. */\n#if __STDC_VERSION__ < 199901L\n#  ifdef _MSC_VER\n#    include <malloc.h>\n#    define alloca _alloca\n#  else\n#    ifdef JEMALLOC_HAS_ALLOCA_H\n#      include <alloca.h>\n#    else\n#      include <stdlib.h>\n#    endif\n#  endif\n#  define VARIABLE_ARRAY(type, name, count) \\\n\ttype *name = alloca(sizeof(type) * (count))\n#else\n#  define VARIABLE_ARRAY(type, name, count) type name[(count)]\n#endif\n\n#endif /* JEMALLOC_INTERNAL_TYPES_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/jemalloc_preamble.h.in",
    "content": "#ifndef JEMALLOC_PREAMBLE_H\n#define JEMALLOC_PREAMBLE_H\n\n#include \"jemalloc_internal_defs.h\"\n#include \"jemalloc/internal/jemalloc_internal_decls.h\"\n\n#ifdef JEMALLOC_UTRACE\n#include <sys/ktrace.h>\n#endif\n\n#define JEMALLOC_NO_DEMANGLE\n#ifdef JEMALLOC_JET\n#  undef JEMALLOC_IS_MALLOC\n#  define JEMALLOC_N(n) jet_##n\n#  include \"jemalloc/internal/public_namespace.h\"\n#  define JEMALLOC_NO_RENAME\n#  include \"../jemalloc@install_suffix@.h\"\n#  undef JEMALLOC_NO_RENAME\n#else\n#  define JEMALLOC_N(n) @private_namespace@##n\n#  include \"../jemalloc@install_suffix@.h\"\n#endif\n\n#if (defined(JEMALLOC_OSATOMIC) || defined(JEMALLOC_OSSPIN))\n#include <libkern/OSAtomic.h>\n#endif\n\n#ifdef JEMALLOC_ZONE\n#include <mach/mach_error.h>\n#include <mach/mach_init.h>\n#include <mach/vm_map.h>\n#endif\n\n#include \"jemalloc/internal/jemalloc_internal_macros.h\"\n\n/*\n * Note that the ordering matters here; the hook itself is name-mangled.  We\n * want the inclusion of hooks to happen early, so that we hook as much as\n * possible.\n */\n#ifndef JEMALLOC_NO_PRIVATE_NAMESPACE\n#  ifndef JEMALLOC_JET\n#    include \"jemalloc/internal/private_namespace.h\"\n#  else\n#    include \"jemalloc/internal/private_namespace_jet.h\"\n#  endif\n#endif\n#include \"jemalloc/internal/hooks.h\"\n\nstatic const bool config_debug =\n#ifdef JEMALLOC_DEBUG\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool have_dss =\n#ifdef JEMALLOC_DSS\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool config_fill =\n#ifdef JEMALLOC_FILL\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool config_lazy_lock =\n#ifdef JEMALLOC_LAZY_LOCK\n    true\n#else\n    false\n#endif\n    ;\nstatic const char * const config_malloc_conf = JEMALLOC_CONFIG_MALLOC_CONF;\nstatic const bool config_prof =\n#ifdef JEMALLOC_PROF\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool config_prof_libgcc =\n#ifdef JEMALLOC_PROF_LIBGCC\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool config_prof_libunwind =\n#ifdef JEMALLOC_PROF_LIBUNWIND\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool maps_coalesce =\n#ifdef JEMALLOC_MAPS_COALESCE\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool config_stats =\n#ifdef JEMALLOC_STATS\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool config_thp =\n#ifdef JEMALLOC_THP\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool config_tls =\n#ifdef JEMALLOC_TLS\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool config_utrace =\n#ifdef JEMALLOC_UTRACE\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool config_xmalloc =\n#ifdef JEMALLOC_XMALLOC\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool config_cache_oblivious =\n#ifdef JEMALLOC_CACHE_OBLIVIOUS\n    true\n#else\n    false\n#endif\n    ;\n#ifdef JEMALLOC_HAVE_SCHED_GETCPU\n/* Currently percpu_arena depends on sched_getcpu. */\n#define JEMALLOC_PERCPU_ARENA\n#endif\nstatic const bool have_percpu_arena =\n#ifdef JEMALLOC_PERCPU_ARENA\n    true\n#else\n    false\n#endif\n    ;\n/*\n * Undocumented, and not recommended; the application should take full\n * responsibility for tracking provenance.\n */\nstatic const bool force_ivsalloc =\n#ifdef JEMALLOC_FORCE_IVSALLOC\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool have_background_thread =\n#ifdef JEMALLOC_BACKGROUND_THREAD\n    true\n#else\n    false\n#endif\n    ;\n\n#endif /* JEMALLOC_PREAMBLE_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/large_externs.h",
    "content": "#ifndef JEMALLOC_INTERNAL_LARGE_EXTERNS_H\n#define JEMALLOC_INTERNAL_LARGE_EXTERNS_H\n\nvoid *large_malloc(tsdn_t *tsdn, arena_t *arena, size_t usize, bool zero);\nvoid *large_palloc(tsdn_t *tsdn, arena_t *arena, size_t usize, size_t alignment,\n    bool zero);\nbool large_ralloc_no_move(tsdn_t *tsdn, extent_t *extent, size_t usize_min,\n    size_t usize_max, bool zero);\nvoid *large_ralloc(tsdn_t *tsdn, arena_t *arena, extent_t *extent, size_t usize,\n    size_t alignment, bool zero, tcache_t *tcache);\n\ntypedef void (large_dalloc_junk_t)(void *, size_t);\nextern large_dalloc_junk_t *JET_MUTABLE large_dalloc_junk;\n\ntypedef void (large_dalloc_maybe_junk_t)(void *, size_t);\nextern large_dalloc_maybe_junk_t *JET_MUTABLE large_dalloc_maybe_junk;\n\nvoid large_dalloc_prep_junked_locked(tsdn_t *tsdn, extent_t *extent);\nvoid large_dalloc_finish(tsdn_t *tsdn, extent_t *extent);\nvoid large_dalloc(tsdn_t *tsdn, extent_t *extent);\nsize_t large_salloc(tsdn_t *tsdn, const extent_t *extent);\nprof_tctx_t *large_prof_tctx_get(tsdn_t *tsdn, const extent_t *extent);\nvoid large_prof_tctx_set(tsdn_t *tsdn, extent_t *extent, prof_tctx_t *tctx);\nvoid large_prof_tctx_reset(tsdn_t *tsdn, extent_t *extent);\n\n#endif /* JEMALLOC_INTERNAL_LARGE_EXTERNS_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/malloc_io.h",
    "content": "#ifndef JEMALLOC_INTERNAL_MALLOC_IO_H\n#define JEMALLOC_INTERNAL_MALLOC_IO_H\n\n#ifdef _WIN32\n#  ifdef _WIN64\n#    define FMT64_PREFIX \"ll\"\n#    define FMTPTR_PREFIX \"ll\"\n#  else\n#    define FMT64_PREFIX \"ll\"\n#    define FMTPTR_PREFIX \"\"\n#  endif\n#  define FMTd32 \"d\"\n#  define FMTu32 \"u\"\n#  define FMTx32 \"x\"\n#  define FMTd64 FMT64_PREFIX \"d\"\n#  define FMTu64 FMT64_PREFIX \"u\"\n#  define FMTx64 FMT64_PREFIX \"x\"\n#  define FMTdPTR FMTPTR_PREFIX \"d\"\n#  define FMTuPTR FMTPTR_PREFIX \"u\"\n#  define FMTxPTR FMTPTR_PREFIX \"x\"\n#else\n#  include <inttypes.h>\n#  define FMTd32 PRId32\n#  define FMTu32 PRIu32\n#  define FMTx32 PRIx32\n#  define FMTd64 PRId64\n#  define FMTu64 PRIu64\n#  define FMTx64 PRIx64\n#  define FMTdPTR PRIdPTR\n#  define FMTuPTR PRIuPTR\n#  define FMTxPTR PRIxPTR\n#endif\n\n/* Size of stack-allocated buffer passed to buferror(). */\n#define BUFERROR_BUF\t\t64\n\n/*\n * Size of stack-allocated buffer used by malloc_{,v,vc}printf().  This must be\n * large enough for all possible uses within jemalloc.\n */\n#define MALLOC_PRINTF_BUFSIZE\t4096\n\nint buferror(int err, char *buf, size_t buflen);\nuintmax_t malloc_strtoumax(const char *restrict nptr, char **restrict endptr,\n    int base);\nvoid malloc_write(const char *s);\n\n/*\n * malloc_vsnprintf() supports a subset of snprintf(3) that avoids floating\n * point math.\n */\nsize_t malloc_vsnprintf(char *str, size_t size, const char *format,\n    va_list ap);\nsize_t malloc_snprintf(char *str, size_t size, const char *format, ...)\n    JEMALLOC_FORMAT_PRINTF(3, 4);\nvoid malloc_vcprintf(void (*write_cb)(void *, const char *), void *cbopaque,\n    const char *format, va_list ap);\nvoid malloc_cprintf(void (*write_cb)(void *, const char *), void *cbopaque,\n    const char *format, ...) JEMALLOC_FORMAT_PRINTF(3, 4);\nvoid malloc_printf(const char *format, ...) JEMALLOC_FORMAT_PRINTF(1, 2);\n\n#endif /* JEMALLOC_INTERNAL_MALLOC_IO_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/mutex.h",
    "content": "#ifndef JEMALLOC_INTERNAL_MUTEX_H\n#define JEMALLOC_INTERNAL_MUTEX_H\n\n#include \"jemalloc/internal/atomic.h\"\n#include \"jemalloc/internal/mutex_prof.h\"\n#include \"jemalloc/internal/tsd.h\"\n#include \"jemalloc/internal/witness.h\"\n\ntypedef enum {\n\t/* Can only acquire one mutex of a given witness rank at a time. */\n\tmalloc_mutex_rank_exclusive,\n\t/*\n\t * Can acquire multiple mutexes of the same witness rank, but in\n\t * address-ascending order only.\n\t */\n\tmalloc_mutex_address_ordered\n} malloc_mutex_lock_order_t;\n\ntypedef struct malloc_mutex_s malloc_mutex_t;\nstruct malloc_mutex_s {\n\tunion {\n\t\tstruct {\n\t\t\t/*\n\t\t\t * prof_data is defined first to reduce cacheline\n\t\t\t * bouncing: the data is not touched by the mutex holder\n\t\t\t * during unlocking, while might be modified by\n\t\t\t * contenders.  Having it before the mutex itself could\n\t\t\t * avoid prefetching a modified cacheline (for the\n\t\t\t * unlocking thread).\n\t\t\t */\n\t\t\tmutex_prof_data_t\tprof_data;\n#ifdef _WIN32\n#  if _WIN32_WINNT >= 0x0600\n\t\t\tSRWLOCK         \tlock;\n#  else\n\t\t\tCRITICAL_SECTION\tlock;\n#  endif\n#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))\n\t\t\tos_unfair_lock\t\tlock;\n#elif (defined(JEMALLOC_OSSPIN))\n\t\t\tOSSpinLock\t\tlock;\n#elif (defined(JEMALLOC_MUTEX_INIT_CB))\n\t\t\tpthread_mutex_t\t\tlock;\n\t\t\tmalloc_mutex_t\t\t*postponed_next;\n#else\n\t\t\tpthread_mutex_t\t\tlock;\n#endif\n\t\t};\n\t\t/*\n\t\t * We only touch witness when configured w/ debug.  However we\n\t\t * keep the field in a union when !debug so that we don't have\n\t\t * to pollute the code base with #ifdefs, while avoid paying the\n\t\t * memory cost.\n\t\t */\n#if !defined(JEMALLOC_DEBUG)\n\t\twitness_t\t\t\twitness;\n\t\tmalloc_mutex_lock_order_t\tlock_order;\n#endif\n\t};\n\n#if defined(JEMALLOC_DEBUG)\n\twitness_t\t\t\twitness;\n\tmalloc_mutex_lock_order_t\tlock_order;\n#endif\n};\n\n/*\n * Based on benchmark results, a fixed spin with this amount of retries works\n * well for our critical sections.\n */\n#define MALLOC_MUTEX_MAX_SPIN 250\n\n#ifdef _WIN32\n#  if _WIN32_WINNT >= 0x0600\n#    define MALLOC_MUTEX_LOCK(m)    AcquireSRWLockExclusive(&(m)->lock)\n#    define MALLOC_MUTEX_UNLOCK(m)  ReleaseSRWLockExclusive(&(m)->lock)\n#    define MALLOC_MUTEX_TRYLOCK(m) (!TryAcquireSRWLockExclusive(&(m)->lock))\n#  else\n#    define MALLOC_MUTEX_LOCK(m)    EnterCriticalSection(&(m)->lock)\n#    define MALLOC_MUTEX_UNLOCK(m)  LeaveCriticalSection(&(m)->lock)\n#    define MALLOC_MUTEX_TRYLOCK(m) (!TryEnterCriticalSection(&(m)->lock))\n#  endif\n#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))\n#    define MALLOC_MUTEX_LOCK(m)    os_unfair_lock_lock(&(m)->lock)\n#    define MALLOC_MUTEX_UNLOCK(m)  os_unfair_lock_unlock(&(m)->lock)\n#    define MALLOC_MUTEX_TRYLOCK(m) (!os_unfair_lock_trylock(&(m)->lock))\n#elif (defined(JEMALLOC_OSSPIN))\n#    define MALLOC_MUTEX_LOCK(m)    OSSpinLockLock(&(m)->lock)\n#    define MALLOC_MUTEX_UNLOCK(m)  OSSpinLockUnlock(&(m)->lock)\n#    define MALLOC_MUTEX_TRYLOCK(m) (!OSSpinLockTry(&(m)->lock))\n#else\n#    define MALLOC_MUTEX_LOCK(m)    pthread_mutex_lock(&(m)->lock)\n#    define MALLOC_MUTEX_UNLOCK(m)  pthread_mutex_unlock(&(m)->lock)\n#    define MALLOC_MUTEX_TRYLOCK(m) (pthread_mutex_trylock(&(m)->lock) != 0)\n#endif\n\n#define LOCK_PROF_DATA_INITIALIZER\t\t\t\t\t\\\n    {NSTIME_ZERO_INITIALIZER, NSTIME_ZERO_INITIALIZER, 0, 0, 0,\t\t\\\n\t    ATOMIC_INIT(0), 0, NULL, 0}\n\n#ifdef _WIN32\n#  define MALLOC_MUTEX_INITIALIZER\n#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))\n#  define MALLOC_MUTEX_INITIALIZER\t\t\t\t\t\\\n     {{{LOCK_PROF_DATA_INITIALIZER, OS_UNFAIR_LOCK_INIT}},\t\t\\\n      WITNESS_INITIALIZER(\"mutex\", WITNESS_RANK_OMIT)}\n#elif (defined(JEMALLOC_OSSPIN))\n#  define MALLOC_MUTEX_INITIALIZER\t\t\t\t\t\\\n     {{{LOCK_PROF_DATA_INITIALIZER, 0}},\t\t\t\t\\\n      WITNESS_INITIALIZER(\"mutex\", WITNESS_RANK_OMIT)}\n#elif (defined(JEMALLOC_MUTEX_INIT_CB))\n#  define MALLOC_MUTEX_INITIALIZER\t\t\t\t\t\\\n     {{{LOCK_PROF_DATA_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, NULL}},\t\\\n      WITNESS_INITIALIZER(\"mutex\", WITNESS_RANK_OMIT)}\n#else\n#    define MALLOC_MUTEX_TYPE PTHREAD_MUTEX_DEFAULT\n#    define MALLOC_MUTEX_INITIALIZER\t\t\t\t\t\\\n       {{{LOCK_PROF_DATA_INITIALIZER, PTHREAD_MUTEX_INITIALIZER}},\t\\\n        WITNESS_INITIALIZER(\"mutex\", WITNESS_RANK_OMIT)}\n#endif\n\n#ifdef JEMALLOC_LAZY_LOCK\nextern bool isthreaded;\n#else\n#  undef isthreaded /* Undo private_namespace.h definition. */\n#  define isthreaded true\n#endif\n\nbool malloc_mutex_init(malloc_mutex_t *mutex, const char *name,\n    witness_rank_t rank, malloc_mutex_lock_order_t lock_order);\nvoid malloc_mutex_prefork(tsdn_t *tsdn, malloc_mutex_t *mutex);\nvoid malloc_mutex_postfork_parent(tsdn_t *tsdn, malloc_mutex_t *mutex);\nvoid malloc_mutex_postfork_child(tsdn_t *tsdn, malloc_mutex_t *mutex);\nbool malloc_mutex_boot(void);\nvoid malloc_mutex_prof_data_reset(tsdn_t *tsdn, malloc_mutex_t *mutex);\n\nvoid malloc_mutex_lock_slow(malloc_mutex_t *mutex);\n\nstatic inline void\nmalloc_mutex_lock_final(malloc_mutex_t *mutex) {\n\tMALLOC_MUTEX_LOCK(mutex);\n}\n\nstatic inline bool\nmalloc_mutex_trylock_final(malloc_mutex_t *mutex) {\n\treturn MALLOC_MUTEX_TRYLOCK(mutex);\n}\n\nstatic inline void\nmutex_owner_stats_update(tsdn_t *tsdn, malloc_mutex_t *mutex) {\n\tif (config_stats) {\n\t\tmutex_prof_data_t *data = &mutex->prof_data;\n\t\tdata->n_lock_ops++;\n\t\tif (data->prev_owner != tsdn) {\n\t\t\tdata->prev_owner = tsdn;\n\t\t\tdata->n_owner_switches++;\n\t\t}\n\t}\n}\n\n/* Trylock: return false if the lock is successfully acquired. */\nstatic inline bool\nmalloc_mutex_trylock(tsdn_t *tsdn, malloc_mutex_t *mutex) {\n\twitness_assert_not_owner(tsdn_witness_tsdp_get(tsdn), &mutex->witness);\n\tif (isthreaded) {\n\t\tif (malloc_mutex_trylock_final(mutex)) {\n\t\t\treturn true;\n\t\t}\n\t\tmutex_owner_stats_update(tsdn, mutex);\n\t}\n\twitness_lock(tsdn_witness_tsdp_get(tsdn), &mutex->witness);\n\n\treturn false;\n}\n\n/* Aggregate lock prof data. */\nstatic inline void\nmalloc_mutex_prof_merge(mutex_prof_data_t *sum, mutex_prof_data_t *data) {\n\tnstime_add(&sum->tot_wait_time, &data->tot_wait_time);\n\tif (nstime_compare(&sum->max_wait_time, &data->max_wait_time) < 0) {\n\t\tnstime_copy(&sum->max_wait_time, &data->max_wait_time);\n\t}\n\n\tsum->n_wait_times += data->n_wait_times;\n\tsum->n_spin_acquired += data->n_spin_acquired;\n\n\tif (sum->max_n_thds < data->max_n_thds) {\n\t\tsum->max_n_thds = data->max_n_thds;\n\t}\n\tuint32_t cur_n_waiting_thds = atomic_load_u32(&sum->n_waiting_thds,\n\t    ATOMIC_RELAXED);\n\tuint32_t new_n_waiting_thds = cur_n_waiting_thds + atomic_load_u32(\n\t    &data->n_waiting_thds, ATOMIC_RELAXED);\n\tatomic_store_u32(&sum->n_waiting_thds, new_n_waiting_thds,\n\t    ATOMIC_RELAXED);\n\tsum->n_owner_switches += data->n_owner_switches;\n\tsum->n_lock_ops += data->n_lock_ops;\n}\n\nstatic inline void\nmalloc_mutex_lock(tsdn_t *tsdn, malloc_mutex_t *mutex) {\n\twitness_assert_not_owner(tsdn_witness_tsdp_get(tsdn), &mutex->witness);\n\tif (isthreaded) {\n\t\tif (malloc_mutex_trylock_final(mutex)) {\n\t\t\tmalloc_mutex_lock_slow(mutex);\n\t\t}\n\t\tmutex_owner_stats_update(tsdn, mutex);\n\t}\n\twitness_lock(tsdn_witness_tsdp_get(tsdn), &mutex->witness);\n}\n\nstatic inline void\nmalloc_mutex_unlock(tsdn_t *tsdn, malloc_mutex_t *mutex) {\n\twitness_unlock(tsdn_witness_tsdp_get(tsdn), &mutex->witness);\n\tif (isthreaded) {\n\t\tMALLOC_MUTEX_UNLOCK(mutex);\n\t}\n}\n\nstatic inline void\nmalloc_mutex_assert_owner(tsdn_t *tsdn, malloc_mutex_t *mutex) {\n\twitness_assert_owner(tsdn_witness_tsdp_get(tsdn), &mutex->witness);\n}\n\nstatic inline void\nmalloc_mutex_assert_not_owner(tsdn_t *tsdn, malloc_mutex_t *mutex) {\n\twitness_assert_not_owner(tsdn_witness_tsdp_get(tsdn), &mutex->witness);\n}\n\n/* Copy the prof data from mutex for processing. */\nstatic inline void\nmalloc_mutex_prof_read(tsdn_t *tsdn, mutex_prof_data_t *data,\n    malloc_mutex_t *mutex) {\n\tmutex_prof_data_t *source = &mutex->prof_data;\n\t/* Can only read holding the mutex. */\n\tmalloc_mutex_assert_owner(tsdn, mutex);\n\n\t/*\n\t * Not *really* allowed (we shouldn't be doing non-atomic loads of\n\t * atomic data), but the mutex protection makes this safe, and writing\n\t * a member-for-member copy is tedious for this situation.\n\t */\n\t*data = *source;\n\t/* n_wait_thds is not reported (modified w/o locking). */\n\tatomic_store_u32(&data->n_waiting_thds, 0, ATOMIC_RELAXED);\n}\n\n#endif /* JEMALLOC_INTERNAL_MUTEX_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/mutex_pool.h",
    "content": "#ifndef JEMALLOC_INTERNAL_MUTEX_POOL_H\n#define JEMALLOC_INTERNAL_MUTEX_POOL_H\n\n#include \"jemalloc/internal/hash.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/witness.h\"\n\n/* We do mod reductions by this value, so it should be kept a power of 2. */\n#define MUTEX_POOL_SIZE 256\n\ntypedef struct mutex_pool_s mutex_pool_t;\nstruct mutex_pool_s {\n\tmalloc_mutex_t mutexes[MUTEX_POOL_SIZE];\n};\n\nbool mutex_pool_init(mutex_pool_t *pool, const char *name, witness_rank_t rank);\n\n/* Internal helper - not meant to be called outside this module. */\nstatic inline malloc_mutex_t *\nmutex_pool_mutex(mutex_pool_t *pool, uintptr_t key) {\n\tsize_t hash_result[2];\n\thash(&key, sizeof(key), 0xd50dcc1b, hash_result);\n\treturn &pool->mutexes[hash_result[0] % MUTEX_POOL_SIZE];\n}\n\nstatic inline void\nmutex_pool_assert_not_held(tsdn_t *tsdn, mutex_pool_t *pool) {\n\tfor (int i = 0; i < MUTEX_POOL_SIZE; i++) {\n\t\tmalloc_mutex_assert_not_owner(tsdn, &pool->mutexes[i]);\n\t}\n}\n\n/*\n * Note that a mutex pool doesn't work exactly the way an embdedded mutex would.\n * You're not allowed to acquire mutexes in the pool one at a time.  You have to\n * acquire all the mutexes you'll need in a single function call, and then\n * release them all in a single function call.\n */\n\nstatic inline void\nmutex_pool_lock(tsdn_t *tsdn, mutex_pool_t *pool, uintptr_t key) {\n\tmutex_pool_assert_not_held(tsdn, pool);\n\n\tmalloc_mutex_t *mutex = mutex_pool_mutex(pool, key);\n\tmalloc_mutex_lock(tsdn, mutex);\n}\n\nstatic inline void\nmutex_pool_unlock(tsdn_t *tsdn, mutex_pool_t *pool, uintptr_t key) {\n\tmalloc_mutex_t *mutex = mutex_pool_mutex(pool, key);\n\tmalloc_mutex_unlock(tsdn, mutex);\n\n\tmutex_pool_assert_not_held(tsdn, pool);\n}\n\nstatic inline void\nmutex_pool_lock2(tsdn_t *tsdn, mutex_pool_t *pool, uintptr_t key1,\n    uintptr_t key2) {\n\tmutex_pool_assert_not_held(tsdn, pool);\n\n\tmalloc_mutex_t *mutex1 = mutex_pool_mutex(pool, key1);\n\tmalloc_mutex_t *mutex2 = mutex_pool_mutex(pool, key2);\n\tif ((uintptr_t)mutex1 < (uintptr_t)mutex2) {\n\t\tmalloc_mutex_lock(tsdn, mutex1);\n\t\tmalloc_mutex_lock(tsdn, mutex2);\n\t} else if ((uintptr_t)mutex1 == (uintptr_t)mutex2) {\n\t\tmalloc_mutex_lock(tsdn, mutex1);\n\t} else {\n\t\tmalloc_mutex_lock(tsdn, mutex2);\n\t\tmalloc_mutex_lock(tsdn, mutex1);\n\t}\n}\n\nstatic inline void\nmutex_pool_unlock2(tsdn_t *tsdn, mutex_pool_t *pool, uintptr_t key1,\n    uintptr_t key2) {\n\tmalloc_mutex_t *mutex1 = mutex_pool_mutex(pool, key1);\n\tmalloc_mutex_t *mutex2 = mutex_pool_mutex(pool, key2);\n\tif (mutex1 == mutex2) {\n\t\tmalloc_mutex_unlock(tsdn, mutex1);\n\t} else {\n\t\tmalloc_mutex_unlock(tsdn, mutex1);\n\t\tmalloc_mutex_unlock(tsdn, mutex2);\n\t}\n\n\tmutex_pool_assert_not_held(tsdn, pool);\n}\n\nstatic inline void\nmutex_pool_assert_owner(tsdn_t *tsdn, mutex_pool_t *pool, uintptr_t key) {\n\tmalloc_mutex_assert_owner(tsdn, mutex_pool_mutex(pool, key));\n}\n\n#endif /* JEMALLOC_INTERNAL_MUTEX_POOL_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/mutex_prof.h",
    "content": "#ifndef JEMALLOC_INTERNAL_MUTEX_PROF_H\n#define JEMALLOC_INTERNAL_MUTEX_PROF_H\n\n#include \"jemalloc/internal/atomic.h\"\n#include \"jemalloc/internal/nstime.h\"\n#include \"jemalloc/internal/tsd_types.h\"\n\n#define MUTEX_PROF_GLOBAL_MUTEXES\t\t\t\t\t\\\n    OP(background_thread)\t\t\t\t\t\t\\\n    OP(ctl)\t\t\t\t\t\t\t\t\\\n    OP(prof)\n\ntypedef enum {\n#define OP(mtx) global_prof_mutex_##mtx,\n\tMUTEX_PROF_GLOBAL_MUTEXES\n#undef OP\n\tmutex_prof_num_global_mutexes\n} mutex_prof_global_ind_t;\n\n#define MUTEX_PROF_ARENA_MUTEXES\t\t\t\t\t\\\n    OP(large)\t\t\t\t\t\t\t\t\\\n    OP(extent_avail)\t\t\t\t\t\t\t\\\n    OP(extents_dirty)\t\t\t\t\t\t\t\\\n    OP(extents_muzzy)\t\t\t\t\t\t\t\\\n    OP(extents_retained)\t\t\t\t\t\t\\\n    OP(decay_dirty)\t\t\t\t\t\t\t\\\n    OP(decay_muzzy)\t\t\t\t\t\t\t\\\n    OP(base)\t\t\t\t\t\t\t\t\\\n    OP(tcache_list)\n\ntypedef enum {\n#define OP(mtx) arena_prof_mutex_##mtx,\n\tMUTEX_PROF_ARENA_MUTEXES\n#undef OP\n\tmutex_prof_num_arena_mutexes\n} mutex_prof_arena_ind_t;\n\n#define MUTEX_PROF_COUNTERS\t\t\t\t\t\t\\\n    OP(num_ops, uint64_t)\t\t\t\t\t\t\\\n    OP(num_wait, uint64_t)\t\t\t\t\t\t\\\n    OP(num_spin_acq, uint64_t)\t\t\t\t\t\t\\\n    OP(num_owner_switch, uint64_t)\t\t\t\t\t\\\n    OP(total_wait_time, uint64_t)\t\t\t\t\t\\\n    OP(max_wait_time, uint64_t)\t\t\t\t\t\t\\\n    OP(max_num_thds, uint32_t)\n\ntypedef enum {\n#define OP(counter, type) mutex_counter_##counter,\n\tMUTEX_PROF_COUNTERS\n#undef OP\n\tmutex_prof_num_counters\n} mutex_prof_counter_ind_t;\n\ntypedef struct {\n\t/*\n\t * Counters touched on the slow path, i.e. when there is lock\n\t * contention.  We update them once we have the lock.\n\t */\n\t/* Total time (in nano seconds) spent waiting on this mutex. */\n\tnstime_t\t\ttot_wait_time;\n\t/* Max time (in nano seconds) spent on a single lock operation. */\n\tnstime_t\t\tmax_wait_time;\n\t/* # of times have to wait for this mutex (after spinning). */\n\tuint64_t\t\tn_wait_times;\n\t/* # of times acquired the mutex through local spinning. */\n\tuint64_t\t\tn_spin_acquired;\n\t/* Max # of threads waiting for the mutex at the same time. */\n\tuint32_t\t\tmax_n_thds;\n\t/* Current # of threads waiting on the lock.  Atomic synced. */\n\tatomic_u32_t\t\tn_waiting_thds;\n\n\t/*\n\t * Data touched on the fast path.  These are modified right after we\n\t * grab the lock, so it's placed closest to the end (i.e. right before\n\t * the lock) so that we have a higher chance of them being on the same\n\t * cacheline.\n\t */\n\t/* # of times the mutex holder is different than the previous one. */\n\tuint64_t\t\tn_owner_switches;\n\t/* Previous mutex holder, to facilitate n_owner_switches. */\n\ttsdn_t\t\t\t*prev_owner;\n\t/* # of lock() operations in total. */\n\tuint64_t\t\tn_lock_ops;\n} mutex_prof_data_t;\n\n#endif /* JEMALLOC_INTERNAL_MUTEX_PROF_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/nstime.h",
    "content": "#ifndef JEMALLOC_INTERNAL_NSTIME_H\n#define JEMALLOC_INTERNAL_NSTIME_H\n\n/* Maximum supported number of seconds (~584 years). */\n#define NSTIME_SEC_MAX KQU(18446744072)\n#define NSTIME_ZERO_INITIALIZER {0}\n\ntypedef struct {\n\tuint64_t ns;\n} nstime_t;\n\nvoid nstime_init(nstime_t *time, uint64_t ns);\nvoid nstime_init2(nstime_t *time, uint64_t sec, uint64_t nsec);\nuint64_t nstime_ns(const nstime_t *time);\nuint64_t nstime_sec(const nstime_t *time);\nuint64_t nstime_msec(const nstime_t *time);\nuint64_t nstime_nsec(const nstime_t *time);\nvoid nstime_copy(nstime_t *time, const nstime_t *source);\nint nstime_compare(const nstime_t *a, const nstime_t *b);\nvoid nstime_add(nstime_t *time, const nstime_t *addend);\nvoid nstime_iadd(nstime_t *time, uint64_t addend);\nvoid nstime_subtract(nstime_t *time, const nstime_t *subtrahend);\nvoid nstime_isubtract(nstime_t *time, uint64_t subtrahend);\nvoid nstime_imultiply(nstime_t *time, uint64_t multiplier);\nvoid nstime_idivide(nstime_t *time, uint64_t divisor);\nuint64_t nstime_divide(const nstime_t *time, const nstime_t *divisor);\n\ntypedef bool (nstime_monotonic_t)(void);\nextern nstime_monotonic_t *JET_MUTABLE nstime_monotonic;\n\ntypedef bool (nstime_update_t)(nstime_t *);\nextern nstime_update_t *JET_MUTABLE nstime_update;\n\n#endif /* JEMALLOC_INTERNAL_NSTIME_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/pages.h",
    "content": "#ifndef JEMALLOC_INTERNAL_PAGES_EXTERNS_H\n#define JEMALLOC_INTERNAL_PAGES_EXTERNS_H\n\n/* Page size.  LG_PAGE is determined by the configure script. */\n#ifdef PAGE_MASK\n#  undef PAGE_MASK\n#endif\n#define PAGE\t\t((size_t)(1U << LG_PAGE))\n#define PAGE_MASK\t((size_t)(PAGE - 1))\n/* Return the page base address for the page containing address a. */\n#define PAGE_ADDR2BASE(a)\t\t\t\t\t\t\\\n\t((void *)((uintptr_t)(a) & ~PAGE_MASK))\n/* Return the smallest pagesize multiple that is >= s. */\n#define PAGE_CEILING(s)\t\t\t\t\t\t\t\\\n\t(((s) + PAGE_MASK) & ~PAGE_MASK)\n\n/* Huge page size.  LG_HUGEPAGE is determined by the configure script. */\n#define HUGEPAGE\t((size_t)(1U << LG_HUGEPAGE))\n#define HUGEPAGE_MASK\t((size_t)(HUGEPAGE - 1))\n/* Return the huge page base address for the huge page containing address a. */\n#define HUGEPAGE_ADDR2BASE(a)\t\t\t\t\t\t\\\n\t((void *)((uintptr_t)(a) & ~HUGEPAGE_MASK))\n/* Return the smallest pagesize multiple that is >= s. */\n#define HUGEPAGE_CEILING(s)\t\t\t\t\t\t\\\n\t(((s) + HUGEPAGE_MASK) & ~HUGEPAGE_MASK)\n\n/* PAGES_CAN_PURGE_LAZY is defined if lazy purging is supported. */\n#if defined(_WIN32) || defined(JEMALLOC_PURGE_MADVISE_FREE)\n#  define PAGES_CAN_PURGE_LAZY\n#endif\n/*\n * PAGES_CAN_PURGE_FORCED is defined if forced purging is supported.\n *\n * The only supported way to hard-purge on Windows is to decommit and then\n * re-commit, but doing so is racy, and if re-commit fails it's a pain to\n * propagate the \"poisoned\" memory state.  Since we typically decommit as the\n * next step after purging on Windows anyway, there's no point in adding such\n * complexity.\n */\n#if !defined(_WIN32) && ((defined(JEMALLOC_PURGE_MADVISE_DONTNEED) && \\\n    defined(JEMALLOC_PURGE_MADVISE_DONTNEED_ZEROS)) || \\\n    defined(JEMALLOC_MAPS_COALESCE))\n#  define PAGES_CAN_PURGE_FORCED\n#endif\n\nstatic const bool pages_can_purge_lazy =\n#ifdef PAGES_CAN_PURGE_LAZY\n    true\n#else\n    false\n#endif\n    ;\nstatic const bool pages_can_purge_forced =\n#ifdef PAGES_CAN_PURGE_FORCED\n    true\n#else\n    false\n#endif\n    ;\n\nvoid *pages_map(void *addr, size_t size, size_t alignment, bool *commit);\nvoid pages_unmap(void *addr, size_t size);\nbool pages_commit(void *addr, size_t size);\nbool pages_decommit(void *addr, size_t size);\nbool pages_purge_lazy(void *addr, size_t size);\nbool pages_purge_forced(void *addr, size_t size);\nbool pages_huge(void *addr, size_t size);\nbool pages_nohuge(void *addr, size_t size);\nbool pages_boot(void);\n\n#endif /* JEMALLOC_INTERNAL_PAGES_EXTERNS_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/ph.h",
    "content": "/*\n * A Pairing Heap implementation.\n *\n * \"The Pairing Heap: A New Form of Self-Adjusting Heap\"\n * https://www.cs.cmu.edu/~sleator/papers/pairing-heaps.pdf\n *\n * With auxiliary twopass list, described in a follow on paper.\n *\n * \"Pairing Heaps: Experiments and Analysis\"\n * http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.106.2988&rep=rep1&type=pdf\n *\n *******************************************************************************\n */\n\n#ifndef PH_H_\n#define PH_H_\n\n/* Node structure. */\n#define phn(a_type)\t\t\t\t\t\t\t\\\nstruct {\t\t\t\t\t\t\t\t\\\n\ta_type\t*phn_prev;\t\t\t\t\t\t\\\n\ta_type\t*phn_next;\t\t\t\t\t\t\\\n\ta_type\t*phn_lchild;\t\t\t\t\t\t\\\n}\n\n/* Root structure. */\n#define ph(a_type)\t\t\t\t\t\t\t\\\nstruct {\t\t\t\t\t\t\t\t\\\n\ta_type\t*ph_root;\t\t\t\t\t\t\\\n}\n\n/* Internal utility macros. */\n#define phn_lchild_get(a_type, a_field, a_phn)\t\t\t\t\\\n\t(a_phn->a_field.phn_lchild)\n#define phn_lchild_set(a_type, a_field, a_phn, a_lchild) do {\t\t\\\n\ta_phn->a_field.phn_lchild = a_lchild;\t\t\t\t\\\n} while (0)\n\n#define phn_next_get(a_type, a_field, a_phn)\t\t\t\t\\\n\t(a_phn->a_field.phn_next)\n#define phn_prev_set(a_type, a_field, a_phn, a_prev) do {\t\t\\\n\ta_phn->a_field.phn_prev = a_prev;\t\t\t\t\\\n} while (0)\n\n#define phn_prev_get(a_type, a_field, a_phn)\t\t\t\t\\\n\t(a_phn->a_field.phn_prev)\n#define phn_next_set(a_type, a_field, a_phn, a_next) do {\t\t\\\n\ta_phn->a_field.phn_next = a_next;\t\t\t\t\\\n} while (0)\n\n#define phn_merge_ordered(a_type, a_field, a_phn0, a_phn1, a_cmp) do {\t\\\n\ta_type *phn0child;\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tassert(a_phn0 != NULL);\t\t\t\t\t\t\\\n\tassert(a_phn1 != NULL);\t\t\t\t\t\t\\\n\tassert(a_cmp(a_phn0, a_phn1) <= 0);\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tphn_prev_set(a_type, a_field, a_phn1, a_phn0);\t\t\t\\\n\tphn0child = phn_lchild_get(a_type, a_field, a_phn0);\t\t\\\n\tphn_next_set(a_type, a_field, a_phn1, phn0child);\t\t\\\n\tif (phn0child != NULL) {\t\t\t\t\t\\\n\t\tphn_prev_set(a_type, a_field, phn0child, a_phn1);\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tphn_lchild_set(a_type, a_field, a_phn0, a_phn1);\t\t\\\n} while (0)\n\n#define phn_merge(a_type, a_field, a_phn0, a_phn1, a_cmp, r_phn) do {\t\\\n\tif (a_phn0 == NULL) {\t\t\t\t\t\t\\\n\t\tr_phn = a_phn1;\t\t\t\t\t\t\\\n\t} else if (a_phn1 == NULL) {\t\t\t\t\t\\\n\t\tr_phn = a_phn0;\t\t\t\t\t\t\\\n\t} else if (a_cmp(a_phn0, a_phn1) < 0) {\t\t\t\t\\\n\t\tphn_merge_ordered(a_type, a_field, a_phn0, a_phn1,\t\\\n\t\t    a_cmp);\t\t\t\t\t\t\\\n\t\tr_phn = a_phn0;\t\t\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t\tphn_merge_ordered(a_type, a_field, a_phn1, a_phn0,\t\\\n\t\t    a_cmp);\t\t\t\t\t\t\\\n\t\tr_phn = a_phn1;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define ph_merge_siblings(a_type, a_field, a_phn, a_cmp, r_phn) do {\t\\\n\ta_type *head = NULL;\t\t\t\t\t\t\\\n\ta_type *tail = NULL;\t\t\t\t\t\t\\\n\ta_type *phn0 = a_phn;\t\t\t\t\t\t\\\n\ta_type *phn1 = phn_next_get(a_type, a_field, phn0);\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t/*\t\t\t\t\t\t\t\t\\\n\t * Multipass merge, wherein the first two elements of a FIFO\t\\\n\t * are repeatedly merged, and each result is appended to the\t\\\n\t * singly linked FIFO, until the FIFO contains only a single\t\\\n\t * element.  We start with a sibling list but no reference to\t\\\n\t * its tail, so we do a single pass over the sibling list to\t\\\n\t * populate the FIFO.\t\t\t\t\t\t\\\n\t */\t\t\t\t\t\t\t\t\\\n\tif (phn1 != NULL) {\t\t\t\t\t\t\\\n\t\ta_type *phnrest = phn_next_get(a_type, a_field, phn1);\t\\\n\t\tif (phnrest != NULL) {\t\t\t\t\t\\\n\t\t\tphn_prev_set(a_type, a_field, phnrest, NULL);\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\tphn_prev_set(a_type, a_field, phn0, NULL);\t\t\\\n\t\tphn_next_set(a_type, a_field, phn0, NULL);\t\t\\\n\t\tphn_prev_set(a_type, a_field, phn1, NULL);\t\t\\\n\t\tphn_next_set(a_type, a_field, phn1, NULL);\t\t\\\n\t\tphn_merge(a_type, a_field, phn0, phn1, a_cmp, phn0);\t\\\n\t\thead = tail = phn0;\t\t\t\t\t\\\n\t\tphn0 = phnrest;\t\t\t\t\t\t\\\n\t\twhile (phn0 != NULL) {\t\t\t\t\t\\\n\t\t\tphn1 = phn_next_get(a_type, a_field, phn0);\t\\\n\t\t\tif (phn1 != NULL) {\t\t\t\t\\\n\t\t\t\tphnrest = phn_next_get(a_type, a_field,\t\\\n\t\t\t\t    phn1);\t\t\t\t\\\n\t\t\t\tif (phnrest != NULL) {\t\t\t\\\n\t\t\t\t\tphn_prev_set(a_type, a_field,\t\\\n\t\t\t\t\t    phnrest, NULL);\t\t\\\n\t\t\t\t}\t\t\t\t\t\\\n\t\t\t\tphn_prev_set(a_type, a_field, phn0,\t\\\n\t\t\t\t    NULL);\t\t\t\t\\\n\t\t\t\tphn_next_set(a_type, a_field, phn0,\t\\\n\t\t\t\t    NULL);\t\t\t\t\\\n\t\t\t\tphn_prev_set(a_type, a_field, phn1,\t\\\n\t\t\t\t    NULL);\t\t\t\t\\\n\t\t\t\tphn_next_set(a_type, a_field, phn1,\t\\\n\t\t\t\t    NULL);\t\t\t\t\\\n\t\t\t\tphn_merge(a_type, a_field, phn0, phn1,\t\\\n\t\t\t\t    a_cmp, phn0);\t\t\t\\\n\t\t\t\tphn_next_set(a_type, a_field, tail,\t\\\n\t\t\t\t    phn0);\t\t\t\t\\\n\t\t\t\ttail = phn0;\t\t\t\t\\\n\t\t\t\tphn0 = phnrest;\t\t\t\t\\\n\t\t\t} else {\t\t\t\t\t\\\n\t\t\t\tphn_next_set(a_type, a_field, tail,\t\\\n\t\t\t\t    phn0);\t\t\t\t\\\n\t\t\t\ttail = phn0;\t\t\t\t\\\n\t\t\t\tphn0 = NULL;\t\t\t\t\\\n\t\t\t}\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\tphn0 = head;\t\t\t\t\t\t\\\n\t\tphn1 = phn_next_get(a_type, a_field, phn0);\t\t\\\n\t\tif (phn1 != NULL) {\t\t\t\t\t\\\n\t\t\twhile (true) {\t\t\t\t\t\\\n\t\t\t\thead = phn_next_get(a_type, a_field,\t\\\n\t\t\t\t    phn1);\t\t\t\t\\\n\t\t\t\tassert(phn_prev_get(a_type, a_field,\t\\\n\t\t\t\t    phn0) == NULL);\t\t\t\\\n\t\t\t\tphn_next_set(a_type, a_field, phn0,\t\\\n\t\t\t\t    NULL);\t\t\t\t\\\n\t\t\t\tassert(phn_prev_get(a_type, a_field,\t\\\n\t\t\t\t    phn1) == NULL);\t\t\t\\\n\t\t\t\tphn_next_set(a_type, a_field, phn1,\t\\\n\t\t\t\t    NULL);\t\t\t\t\\\n\t\t\t\tphn_merge(a_type, a_field, phn0, phn1,\t\\\n\t\t\t\t    a_cmp, phn0);\t\t\t\\\n\t\t\t\tif (head == NULL) {\t\t\t\\\n\t\t\t\t\tbreak;\t\t\t\t\\\n\t\t\t\t}\t\t\t\t\t\\\n\t\t\t\tphn_next_set(a_type, a_field, tail,\t\\\n\t\t\t\t    phn0);\t\t\t\t\\\n\t\t\t\ttail = phn0;\t\t\t\t\\\n\t\t\t\tphn0 = head;\t\t\t\t\\\n\t\t\t\tphn1 = phn_next_get(a_type, a_field,\t\\\n\t\t\t\t    phn0);\t\t\t\t\\\n\t\t\t}\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tr_phn = phn0;\t\t\t\t\t\t\t\\\n} while (0)\n\n#define ph_merge_aux(a_type, a_field, a_ph, a_cmp) do {\t\t\t\\\n\ta_type *phn = phn_next_get(a_type, a_field, a_ph->ph_root);\t\\\n\tif (phn != NULL) {\t\t\t\t\t\t\\\n\t\tphn_prev_set(a_type, a_field, a_ph->ph_root, NULL);\t\\\n\t\tphn_next_set(a_type, a_field, a_ph->ph_root, NULL);\t\\\n\t\tphn_prev_set(a_type, a_field, phn, NULL);\t\t\\\n\t\tph_merge_siblings(a_type, a_field, phn, a_cmp, phn);\t\\\n\t\tassert(phn_next_get(a_type, a_field, phn) == NULL);\t\\\n\t\tphn_merge(a_type, a_field, a_ph->ph_root, phn, a_cmp,\t\\\n\t\t    a_ph->ph_root);\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define ph_merge_children(a_type, a_field, a_phn, a_cmp, r_phn) do {\t\\\n\ta_type *lchild = phn_lchild_get(a_type, a_field, a_phn);\t\\\n\tif (lchild == NULL) {\t\t\t\t\t\t\\\n\t\tr_phn = NULL;\t\t\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t\tph_merge_siblings(a_type, a_field, lchild, a_cmp,\t\\\n\t\t    r_phn);\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n/*\n * The ph_proto() macro generates function prototypes that correspond to the\n * functions generated by an equivalently parameterized call to ph_gen().\n */\n#define ph_proto(a_attr, a_prefix, a_ph_type, a_type)\t\t\t\\\na_attr void\ta_prefix##new(a_ph_type *ph);\t\t\t\t\\\na_attr bool\ta_prefix##empty(a_ph_type *ph);\t\t\t\t\\\na_attr a_type\t*a_prefix##first(a_ph_type *ph);\t\t\t\\\na_attr a_type\t*a_prefix##any(a_ph_type *ph);\t\t\t\t\\\na_attr void\ta_prefix##insert(a_ph_type *ph, a_type *phn);\t\t\\\na_attr a_type\t*a_prefix##remove_first(a_ph_type *ph);\t\t\t\\\na_attr a_type\t*a_prefix##remove_any(a_ph_type *ph);\t\t\t\\\na_attr void\ta_prefix##remove(a_ph_type *ph, a_type *phn);\n\n/*\n * The ph_gen() macro generates a type-specific pairing heap implementation,\n * based on the above cpp macros.\n */\n#define ph_gen(a_attr, a_prefix, a_ph_type, a_type, a_field, a_cmp)\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##new(a_ph_type *ph) {\t\t\t\t\t\t\\\n\tmemset(ph, 0, sizeof(ph(a_type)));\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr bool\t\t\t\t\t\t\t\t\\\na_prefix##empty(a_ph_type *ph) {\t\t\t\t\t\\\n\treturn (ph->ph_root == NULL);\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##first(a_ph_type *ph) {\t\t\t\t\t\\\n\tif (ph->ph_root == NULL) {\t\t\t\t\t\\\n\t\treturn NULL;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tph_merge_aux(a_type, a_field, ph, a_cmp);\t\t\t\\\n\treturn ph->ph_root;\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##any(a_ph_type *ph) {\t\t\t\t\t\t\\\n\tif (ph->ph_root == NULL) {\t\t\t\t\t\\\n\t\treturn NULL;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\ta_type *aux = phn_next_get(a_type, a_field, ph->ph_root);\t\\\n\tif (aux != NULL) {\t\t\t\t\t\t\\\n\t\treturn aux;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\treturn ph->ph_root;\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##insert(a_ph_type *ph, a_type *phn) {\t\t\t\t\\\n\tmemset(&phn->a_field, 0, sizeof(phn(a_type)));\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t/*\t\t\t\t\t\t\t\t\\\n\t * Treat the root as an aux list during insertion, and lazily\t\\\n\t * merge during a_prefix##remove_first().  For elements that\t\\\n\t * are inserted, then removed via a_prefix##remove() before the\t\\\n\t * aux list is ever processed, this makes insert/remove\t\t\\\n\t * constant-time, whereas eager merging would make insert\t\\\n\t * O(log n).\t\t\t\t\t\t\t\\\n\t */\t\t\t\t\t\t\t\t\\\n\tif (ph->ph_root == NULL) {\t\t\t\t\t\\\n\t\tph->ph_root = phn;\t\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t\tphn_next_set(a_type, a_field, phn, phn_next_get(a_type,\t\\\n\t\t    a_field, ph->ph_root));\t\t\t\t\\\n\t\tif (phn_next_get(a_type, a_field, ph->ph_root) !=\t\\\n\t\t    NULL) {\t\t\t\t\t\t\\\n\t\t\tphn_prev_set(a_type, a_field,\t\t\t\\\n\t\t\t    phn_next_get(a_type, a_field, ph->ph_root),\t\\\n\t\t\t    phn);\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\tphn_prev_set(a_type, a_field, phn, ph->ph_root);\t\\\n\t\tphn_next_set(a_type, a_field, ph->ph_root, phn);\t\\\n\t}\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##remove_first(a_ph_type *ph) {\t\t\t\t\t\\\n\ta_type *ret;\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tif (ph->ph_root == NULL) {\t\t\t\t\t\\\n\t\treturn NULL;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tph_merge_aux(a_type, a_field, ph, a_cmp);\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tret = ph->ph_root;\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tph_merge_children(a_type, a_field, ph->ph_root, a_cmp,\t\t\\\n\t    ph->ph_root);\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\treturn ret;\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##remove_any(a_ph_type *ph) {\t\t\t\t\t\\\n\t/*\t\t\t\t\t\t\t\t\\\n\t * Remove the most recently inserted aux list element, or the\t\\\n\t * root if the aux list is empty.  This has the effect of\t\\\n\t * behaving as a LIFO (and insertion/removal is therefore\t\\\n\t * constant-time) if a_prefix##[remove_]first() are never\t\\\n\t * called.\t\t\t\t\t\t\t\\\n\t */\t\t\t\t\t\t\t\t\\\n\tif (ph->ph_root == NULL) {\t\t\t\t\t\\\n\t\treturn NULL;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\ta_type *ret = phn_next_get(a_type, a_field, ph->ph_root);\t\\\n\tif (ret != NULL) {\t\t\t\t\t\t\\\n\t\ta_type *aux = phn_next_get(a_type, a_field, ret);\t\\\n\t\tphn_next_set(a_type, a_field, ph->ph_root, aux);\t\\\n\t\tif (aux != NULL) {\t\t\t\t\t\\\n\t\t\tphn_prev_set(a_type, a_field, aux,\t\t\\\n\t\t\t    ph->ph_root);\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\treturn ret;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tret = ph->ph_root;\t\t\t\t\t\t\\\n\tph_merge_children(a_type, a_field, ph->ph_root, a_cmp,\t\t\\\n\t    ph->ph_root);\t\t\t\t\t\t\\\n\treturn ret;\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##remove(a_ph_type *ph, a_type *phn) {\t\t\t\t\\\n\ta_type *replace, *parent;\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tif (ph->ph_root == phn) {\t\t\t\t\t\\\n\t\t/*\t\t\t\t\t\t\t\\\n\t\t * We can delete from aux list without merging it, but\t\\\n\t\t * we need to merge if we are dealing with the root\t\\\n\t\t * node and it has children.\t\t\t\t\\\n\t\t */\t\t\t\t\t\t\t\\\n\t\tif (phn_lchild_get(a_type, a_field, phn) == NULL) {\t\\\n\t\t\tph->ph_root = phn_next_get(a_type, a_field,\t\\\n\t\t\t    phn);\t\t\t\t\t\\\n\t\t\tif (ph->ph_root != NULL) {\t\t\t\\\n\t\t\t\tphn_prev_set(a_type, a_field,\t\t\\\n\t\t\t\t    ph->ph_root, NULL);\t\t\t\\\n\t\t\t}\t\t\t\t\t\t\\\n\t\t\treturn;\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\tph_merge_aux(a_type, a_field, ph, a_cmp);\t\t\\\n\t\tif (ph->ph_root == phn) {\t\t\t\t\\\n\t\t\tph_merge_children(a_type, a_field, ph->ph_root,\t\\\n\t\t\t    a_cmp, ph->ph_root);\t\t\t\\\n\t\t\treturn;\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t/* Get parent (if phn is leftmost child) before mutating. */\t\\\n\tif ((parent = phn_prev_get(a_type, a_field, phn)) != NULL) {\t\\\n\t\tif (phn_lchild_get(a_type, a_field, parent) != phn) {\t\\\n\t\t\tparent = NULL;\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\t/* Find a possible replacement node, and link to parent. */\t\\\n\tph_merge_children(a_type, a_field, phn, a_cmp, replace);\t\\\n\t/* Set next/prev for sibling linked list. */\t\t\t\\\n\tif (replace != NULL) {\t\t\t\t\t\t\\\n\t\tif (parent != NULL) {\t\t\t\t\t\\\n\t\t\tphn_prev_set(a_type, a_field, replace, parent);\t\\\n\t\t\tphn_lchild_set(a_type, a_field, parent,\t\t\\\n\t\t\t    replace);\t\t\t\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t\tphn_prev_set(a_type, a_field, replace,\t\t\\\n\t\t\t    phn_prev_get(a_type, a_field, phn));\t\\\n\t\t\tif (phn_prev_get(a_type, a_field, phn) !=\t\\\n\t\t\t    NULL) {\t\t\t\t\t\\\n\t\t\t\tphn_next_set(a_type, a_field,\t\t\\\n\t\t\t\t    phn_prev_get(a_type, a_field, phn),\t\\\n\t\t\t\t    replace);\t\t\t\t\\\n\t\t\t}\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\tphn_next_set(a_type, a_field, replace,\t\t\t\\\n\t\t    phn_next_get(a_type, a_field, phn));\t\t\\\n\t\tif (phn_next_get(a_type, a_field, phn) != NULL) {\t\\\n\t\t\tphn_prev_set(a_type, a_field,\t\t\t\\\n\t\t\t    phn_next_get(a_type, a_field, phn),\t\t\\\n\t\t\t    replace);\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t\tif (parent != NULL) {\t\t\t\t\t\\\n\t\t\ta_type *next = phn_next_get(a_type, a_field,\t\\\n\t\t\t    phn);\t\t\t\t\t\\\n\t\t\tphn_lchild_set(a_type, a_field, parent, next);\t\\\n\t\t\tif (next != NULL) {\t\t\t\t\\\n\t\t\t\tphn_prev_set(a_type, a_field, next,\t\\\n\t\t\t\t    parent);\t\t\t\t\\\n\t\t\t}\t\t\t\t\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t\tassert(phn_prev_get(a_type, a_field, phn) !=\t\\\n\t\t\t    NULL);\t\t\t\t\t\\\n\t\t\tphn_next_set(a_type, a_field,\t\t\t\\\n\t\t\t    phn_prev_get(a_type, a_field, phn),\t\t\\\n\t\t\t    phn_next_get(a_type, a_field, phn));\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\tif (phn_next_get(a_type, a_field, phn) != NULL) {\t\\\n\t\t\tphn_prev_set(a_type, a_field,\t\t\t\\\n\t\t\t    phn_next_get(a_type, a_field, phn),\t\t\\\n\t\t\t    phn_prev_get(a_type, a_field, phn));\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n}\n\n#endif /* PH_H_ */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/private_namespace.sh",
    "content": "#!/bin/sh\n\nfor symbol in `cat \"$@\"` ; do\n  echo \"#define ${symbol} JEMALLOC_N(${symbol})\"\ndone\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/private_symbols.sh",
    "content": "#!/bin/sh\n#\n# Generate private_symbols[_jet].awk.\n#\n# Usage: private_symbols.sh <sym_prefix> <sym>*\n#\n# <sym_prefix> is typically \"\" or \"_\".\n\nsym_prefix=$1\nshift\n\ncat <<EOF\n#!/usr/bin/env awk -f\n\nBEGIN {\n  sym_prefix = \"${sym_prefix}\"\n  split(\"\\\\\nEOF\n\nfor public_sym in \"$@\" ; do\n  cat <<EOF\n        ${sym_prefix}${public_sym} \\\\\nEOF\ndone\n\ncat <<\"EOF\"\n        \", exported_symbol_names)\n  # Store exported symbol names as keys in exported_symbols.\n  for (i in exported_symbol_names) {\n    exported_symbols[exported_symbol_names[i]] = 1\n  }\n}\n\n# Process 'nm -a <c_source.o>' output.\n#\n# Handle lines like:\n#   0000000000000008 D opt_junk\n#   0000000000007574 T malloc_initialized\n(NF == 3 && $2 ~ /^[ABCDGRSTVW]$/ && !($3 in exported_symbols) && $3 ~ /^[A-Za-z0-9_]+$/) {\n  print substr($3, 1+length(sym_prefix), length($3)-length(sym_prefix))\n}\n\n# Process 'dumpbin /SYMBOLS <c_source.obj>' output.\n#\n# Handle lines like:\n#   353 00008098 SECT4  notype       External     | opt_junk\n#   3F1 00000000 SECT7  notype ()    External     | malloc_initialized\n($3 ~ /^SECT[0-9]+/ && $(NF-2) == \"External\" && !($NF in exported_symbols)) {\n  print $NF\n}\nEOF\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/prng.h",
    "content": "#ifndef JEMALLOC_INTERNAL_PRNG_H\n#define JEMALLOC_INTERNAL_PRNG_H\n\n#include \"jemalloc/internal/atomic.h\"\n#include \"jemalloc/internal/bit_util.h\"\n\n/*\n * Simple linear congruential pseudo-random number generator:\n *\n *   prng(y) = (a*x + c) % m\n *\n * where the following constants ensure maximal period:\n *\n *   a == Odd number (relatively prime to 2^n), and (a-1) is a multiple of 4.\n *   c == Odd number (relatively prime to 2^n).\n *   m == 2^32\n *\n * See Knuth's TAOCP 3rd Ed., Vol. 2, pg. 17 for details on these constraints.\n *\n * This choice of m has the disadvantage that the quality of the bits is\n * proportional to bit position.  For example, the lowest bit has a cycle of 2,\n * the next has a cycle of 4, etc.  For this reason, we prefer to use the upper\n * bits.\n */\n\n/******************************************************************************/\n/* INTERNAL DEFINITIONS -- IGNORE */\n/******************************************************************************/\n#define PRNG_A_32\tUINT32_C(1103515241)\n#define PRNG_C_32\tUINT32_C(12347)\n\n#define PRNG_A_64\tUINT64_C(6364136223846793005)\n#define PRNG_C_64\tUINT64_C(1442695040888963407)\n\nJEMALLOC_ALWAYS_INLINE uint32_t\nprng_state_next_u32(uint32_t state) {\n\treturn (state * PRNG_A_32) + PRNG_C_32;\n}\n\nJEMALLOC_ALWAYS_INLINE uint64_t\nprng_state_next_u64(uint64_t state) {\n\treturn (state * PRNG_A_64) + PRNG_C_64;\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\nprng_state_next_zu(size_t state) {\n#if LG_SIZEOF_PTR == 2\n\treturn (state * PRNG_A_32) + PRNG_C_32;\n#elif LG_SIZEOF_PTR == 3\n\treturn (state * PRNG_A_64) + PRNG_C_64;\n#else\n#error Unsupported pointer size\n#endif\n}\n\n/******************************************************************************/\n/* BEGIN PUBLIC API */\n/******************************************************************************/\n\n/*\n * The prng_lg_range functions give a uniform int in the half-open range [0,\n * 2**lg_range).  If atomic is true, they do so safely from multiple threads.\n * Multithreaded 64-bit prngs aren't supported.\n */\n\nJEMALLOC_ALWAYS_INLINE uint32_t\nprng_lg_range_u32(atomic_u32_t *state, unsigned lg_range, bool atomic) {\n\tuint32_t ret, state0, state1;\n\n\tassert(lg_range > 0);\n\tassert(lg_range <= 32);\n\n\tstate0 = atomic_load_u32(state, ATOMIC_RELAXED);\n\n\tif (atomic) {\n\t\tdo {\n\t\t\tstate1 = prng_state_next_u32(state0);\n\t\t} while (!atomic_compare_exchange_weak_u32(state, &state0,\n\t\t    state1, ATOMIC_RELAXED, ATOMIC_RELAXED));\n\t} else {\n\t\tstate1 = prng_state_next_u32(state0);\n\t\tatomic_store_u32(state, state1, ATOMIC_RELAXED);\n\t}\n\tret = state1 >> (32 - lg_range);\n\n\treturn ret;\n}\n\nJEMALLOC_ALWAYS_INLINE uint64_t\nprng_lg_range_u64(uint64_t *state, unsigned lg_range) {\n\tuint64_t ret, state1;\n\n\tassert(lg_range > 0);\n\tassert(lg_range <= 64);\n\n\tstate1 = prng_state_next_u64(*state);\n\t*state = state1;\n\tret = state1 >> (64 - lg_range);\n\n\treturn ret;\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\nprng_lg_range_zu(atomic_zu_t *state, unsigned lg_range, bool atomic) {\n\tsize_t ret, state0, state1;\n\n\tassert(lg_range > 0);\n\tassert(lg_range <= ZU(1) << (3 + LG_SIZEOF_PTR));\n\n\tstate0 = atomic_load_zu(state, ATOMIC_RELAXED);\n\n\tif (atomic) {\n\t\tdo {\n\t\t\tstate1 = prng_state_next_zu(state0);\n\t\t} while (atomic_compare_exchange_weak_zu(state, &state0,\n\t\t    state1, ATOMIC_RELAXED, ATOMIC_RELAXED));\n\t} else {\n\t\tstate1 = prng_state_next_zu(state0);\n\t\tatomic_store_zu(state, state1, ATOMIC_RELAXED);\n\t}\n\tret = state1 >> ((ZU(1) << (3 + LG_SIZEOF_PTR)) - lg_range);\n\n\treturn ret;\n}\n\n/*\n * The prng_range functions behave like the prng_lg_range, but return a result\n * in [0, range) instead of [0, 2**lg_range).\n */\n\nJEMALLOC_ALWAYS_INLINE uint32_t\nprng_range_u32(atomic_u32_t *state, uint32_t range, bool atomic) {\n\tuint32_t ret;\n\tunsigned lg_range;\n\n\tassert(range > 1);\n\n\t/* Compute the ceiling of lg(range). */\n\tlg_range = ffs_u32(pow2_ceil_u32(range)) - 1;\n\n\t/* Generate a result in [0..range) via repeated trial. */\n\tdo {\n\t\tret = prng_lg_range_u32(state, lg_range, atomic);\n\t} while (ret >= range);\n\n\treturn ret;\n}\n\nJEMALLOC_ALWAYS_INLINE uint64_t\nprng_range_u64(uint64_t *state, uint64_t range) {\n\tuint64_t ret;\n\tunsigned lg_range;\n\n\tassert(range > 1);\n\n\t/* Compute the ceiling of lg(range). */\n\tlg_range = ffs_u64(pow2_ceil_u64(range)) - 1;\n\n\t/* Generate a result in [0..range) via repeated trial. */\n\tdo {\n\t\tret = prng_lg_range_u64(state, lg_range);\n\t} while (ret >= range);\n\n\treturn ret;\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\nprng_range_zu(atomic_zu_t *state, size_t range, bool atomic) {\n\tsize_t ret;\n\tunsigned lg_range;\n\n\tassert(range > 1);\n\n\t/* Compute the ceiling of lg(range). */\n\tlg_range = ffs_u64(pow2_ceil_u64(range)) - 1;\n\n\t/* Generate a result in [0..range) via repeated trial. */\n\tdo {\n\t\tret = prng_lg_range_zu(state, lg_range, atomic);\n\t} while (ret >= range);\n\n\treturn ret;\n}\n\n#endif /* JEMALLOC_INTERNAL_PRNG_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/prof_externs.h",
    "content": "#ifndef JEMALLOC_INTERNAL_PROF_EXTERNS_H\n#define JEMALLOC_INTERNAL_PROF_EXTERNS_H\n\n#include \"jemalloc/internal/mutex.h\"\n\nextern malloc_mutex_t\tbt2gctx_mtx;\n\nextern bool\topt_prof;\nextern bool\topt_prof_active;\nextern bool\topt_prof_thread_active_init;\nextern size_t\topt_lg_prof_sample;   /* Mean bytes between samples. */\nextern ssize_t\topt_lg_prof_interval; /* lg(prof_interval). */\nextern bool\topt_prof_gdump;       /* High-water memory dumping. */\nextern bool\topt_prof_final;       /* Final profile dumping. */\nextern bool\topt_prof_leak;        /* Dump leak summary at exit. */\nextern bool\topt_prof_accum;       /* Report cumulative bytes. */\nextern char\topt_prof_prefix[\n    /* Minimize memory bloat for non-prof builds. */\n#ifdef JEMALLOC_PROF\n    PATH_MAX +\n#endif\n    1];\n\n/* Accessed via prof_active_[gs]et{_unlocked,}(). */\nextern bool\tprof_active;\n\n/* Accessed via prof_gdump_[gs]et{_unlocked,}(). */\nextern bool\tprof_gdump_val;\n\n/*\n * Profile dump interval, measured in bytes allocated.  Each arena triggers a\n * profile dump when it reaches this threshold.  The effect is that the\n * interval between profile dumps averages prof_interval, though the actual\n * interval between dumps will tend to be sporadic, and the interval will be a\n * maximum of approximately (prof_interval * narenas).\n */\nextern uint64_t\tprof_interval;\n\n/*\n * Initialized as opt_lg_prof_sample, and potentially modified during profiling\n * resets.\n */\nextern size_t\tlg_prof_sample;\n\nvoid prof_alloc_rollback(tsd_t *tsd, prof_tctx_t *tctx, bool updated);\nvoid prof_malloc_sample_object(tsdn_t *tsdn, const void *ptr, size_t usize,\n    prof_tctx_t *tctx);\nvoid prof_free_sampled_object(tsd_t *tsd, size_t usize, prof_tctx_t *tctx);\nvoid bt_init(prof_bt_t *bt, void **vec);\nvoid prof_backtrace(prof_bt_t *bt);\nprof_tctx_t *prof_lookup(tsd_t *tsd, prof_bt_t *bt);\n#ifdef JEMALLOC_JET\nsize_t prof_tdata_count(void);\nsize_t prof_bt_count(void);\n#endif\ntypedef int (prof_dump_open_t)(bool, const char *);\nextern prof_dump_open_t *JET_MUTABLE prof_dump_open;\n\ntypedef bool (prof_dump_header_t)(tsdn_t *, bool, const prof_cnt_t *);\nextern prof_dump_header_t *JET_MUTABLE prof_dump_header;\n#ifdef JEMALLOC_JET\nvoid prof_cnt_all(uint64_t *curobjs, uint64_t *curbytes, uint64_t *accumobjs,\n    uint64_t *accumbytes);\n#endif\nbool prof_accum_init(tsdn_t *tsdn, prof_accum_t *prof_accum);\nvoid prof_idump(tsdn_t *tsdn);\nbool prof_mdump(tsd_t *tsd, const char *filename);\nvoid prof_gdump(tsdn_t *tsdn);\nprof_tdata_t *prof_tdata_init(tsd_t *tsd);\nprof_tdata_t *prof_tdata_reinit(tsd_t *tsd, prof_tdata_t *tdata);\nvoid prof_reset(tsd_t *tsd, size_t lg_sample);\nvoid prof_tdata_cleanup(tsd_t *tsd);\nbool prof_active_get(tsdn_t *tsdn);\nbool prof_active_set(tsdn_t *tsdn, bool active);\nconst char *prof_thread_name_get(tsd_t *tsd);\nint prof_thread_name_set(tsd_t *tsd, const char *thread_name);\nbool prof_thread_active_get(tsd_t *tsd);\nbool prof_thread_active_set(tsd_t *tsd, bool active);\nbool prof_thread_active_init_get(tsdn_t *tsdn);\nbool prof_thread_active_init_set(tsdn_t *tsdn, bool active_init);\nbool prof_gdump_get(tsdn_t *tsdn);\nbool prof_gdump_set(tsdn_t *tsdn, bool active);\nvoid prof_boot0(void);\nvoid prof_boot1(void);\nbool prof_boot2(tsd_t *tsd);\nvoid prof_prefork0(tsdn_t *tsdn);\nvoid prof_prefork1(tsdn_t *tsdn);\nvoid prof_postfork_parent(tsdn_t *tsdn);\nvoid prof_postfork_child(tsdn_t *tsdn);\nvoid prof_sample_threshold_update(prof_tdata_t *tdata);\n\n#endif /* JEMALLOC_INTERNAL_PROF_EXTERNS_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/prof_inlines_a.h",
    "content": "#ifndef JEMALLOC_INTERNAL_PROF_INLINES_A_H\n#define JEMALLOC_INTERNAL_PROF_INLINES_A_H\n\n#include \"jemalloc/internal/mutex.h\"\n\nstatic inline bool\nprof_accum_add(tsdn_t *tsdn, prof_accum_t *prof_accum, uint64_t accumbytes) {\n\tcassert(config_prof);\n\n\tbool overflow;\n\tuint64_t a0, a1;\n\n\t/*\n\t * If the application allocates fast enough (and/or if idump is slow\n\t * enough), extreme overflow here (a1 >= prof_interval * 2) can cause\n\t * idump trigger coalescing.  This is an intentional mechanism that\n\t * avoids rate-limiting allocation.\n\t */\n#ifdef JEMALLOC_ATOMIC_U64\n\ta0 = atomic_load_u64(&prof_accum->accumbytes, ATOMIC_RELAXED);\n\tdo {\n\t\ta1 = a0 + accumbytes;\n\t\tassert(a1 >= a0);\n\t\toverflow = (a1 >= prof_interval);\n\t\tif (overflow) {\n\t\t\ta1 %= prof_interval;\n\t\t}\n\t} while (!atomic_compare_exchange_weak_u64(&prof_accum->accumbytes, &a0,\n\t    a1, ATOMIC_RELAXED, ATOMIC_RELAXED));\n#else\n\tmalloc_mutex_lock(tsdn, &prof_accum->mtx);\n\ta0 = prof_accum->accumbytes;\n\ta1 = a0 + accumbytes;\n\toverflow = (a1 >= prof_interval);\n\tif (overflow) {\n\t\ta1 %= prof_interval;\n\t}\n\tprof_accum->accumbytes = a1;\n\tmalloc_mutex_unlock(tsdn, &prof_accum->mtx);\n#endif\n\treturn overflow;\n}\n\nstatic inline void\nprof_accum_cancel(tsdn_t *tsdn, prof_accum_t *prof_accum, size_t usize) {\n\tcassert(config_prof);\n\n\t/*\n\t * Cancel out as much of the excessive prof_accumbytes increase as\n\t * possible without underflowing.  Interval-triggered dumps occur\n\t * slightly more often than intended as a result of incomplete\n\t * canceling.\n\t */\n\tuint64_t a0, a1;\n#ifdef JEMALLOC_ATOMIC_U64\n\ta0 = atomic_load_u64(&prof_accum->accumbytes, ATOMIC_RELAXED);\n\tdo {\n\t\ta1 = (a0 >= LARGE_MINCLASS - usize) ?  a0 - (LARGE_MINCLASS -\n\t\t    usize) : 0;\n\t} while (!atomic_compare_exchange_weak_u64(&prof_accum->accumbytes, &a0,\n\t    a1, ATOMIC_RELAXED, ATOMIC_RELAXED));\n#else\n\tmalloc_mutex_lock(tsdn, &prof_accum->mtx);\n\ta0 = prof_accum->accumbytes;\n\ta1 = (a0 >= LARGE_MINCLASS - usize) ?  a0 - (LARGE_MINCLASS - usize) :\n\t    0;\n\tprof_accum->accumbytes = a1;\n\tmalloc_mutex_unlock(tsdn, &prof_accum->mtx);\n#endif\n}\n\n#endif /* JEMALLOC_INTERNAL_PROF_INLINES_A_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/prof_inlines_b.h",
    "content": "#ifndef JEMALLOC_INTERNAL_PROF_INLINES_B_H\n#define JEMALLOC_INTERNAL_PROF_INLINES_B_H\n\n#include \"jemalloc/internal/sz.h\"\n\nJEMALLOC_ALWAYS_INLINE bool\nprof_active_get_unlocked(void) {\n\t/*\n\t * Even if opt_prof is true, sampling can be temporarily disabled by\n\t * setting prof_active to false.  No locking is used when reading\n\t * prof_active in the fast path, so there are no guarantees regarding\n\t * how long it will take for all threads to notice state changes.\n\t */\n\treturn prof_active;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\nprof_gdump_get_unlocked(void) {\n\t/*\n\t * No locking is used when reading prof_gdump_val in the fast path, so\n\t * there are no guarantees regarding how long it will take for all\n\t * threads to notice state changes.\n\t */\n\treturn prof_gdump_val;\n}\n\nJEMALLOC_ALWAYS_INLINE prof_tdata_t *\nprof_tdata_get(tsd_t *tsd, bool create) {\n\tprof_tdata_t *tdata;\n\n\tcassert(config_prof);\n\n\ttdata = tsd_prof_tdata_get(tsd);\n\tif (create) {\n\t\tif (unlikely(tdata == NULL)) {\n\t\t\tif (tsd_nominal(tsd)) {\n\t\t\t\ttdata = prof_tdata_init(tsd);\n\t\t\t\ttsd_prof_tdata_set(tsd, tdata);\n\t\t\t}\n\t\t} else if (unlikely(tdata->expired)) {\n\t\t\ttdata = prof_tdata_reinit(tsd, tdata);\n\t\t\ttsd_prof_tdata_set(tsd, tdata);\n\t\t}\n\t\tassert(tdata == NULL || tdata->attached);\n\t}\n\n\treturn tdata;\n}\n\nJEMALLOC_ALWAYS_INLINE prof_tctx_t *\nprof_tctx_get(tsdn_t *tsdn, const void *ptr, alloc_ctx_t *alloc_ctx) {\n\tcassert(config_prof);\n\tassert(ptr != NULL);\n\n\treturn arena_prof_tctx_get(tsdn, ptr, alloc_ctx);\n}\n\nJEMALLOC_ALWAYS_INLINE void\nprof_tctx_set(tsdn_t *tsdn, const void *ptr, size_t usize,\n    alloc_ctx_t *alloc_ctx, prof_tctx_t *tctx) {\n\tcassert(config_prof);\n\tassert(ptr != NULL);\n\n\tarena_prof_tctx_set(tsdn, ptr, usize, alloc_ctx, tctx);\n}\n\nJEMALLOC_ALWAYS_INLINE void\nprof_tctx_reset(tsdn_t *tsdn, const void *ptr, prof_tctx_t *tctx) {\n\tcassert(config_prof);\n\tassert(ptr != NULL);\n\n\tarena_prof_tctx_reset(tsdn, ptr, tctx);\n}\n\nJEMALLOC_ALWAYS_INLINE bool\nprof_sample_accum_update(tsd_t *tsd, size_t usize, bool update,\n    prof_tdata_t **tdata_out) {\n\tprof_tdata_t *tdata;\n\n\tcassert(config_prof);\n\n\ttdata = prof_tdata_get(tsd, true);\n\tif (unlikely((uintptr_t)tdata <= (uintptr_t)PROF_TDATA_STATE_MAX)) {\n\t\ttdata = NULL;\n\t}\n\n\tif (tdata_out != NULL) {\n\t\t*tdata_out = tdata;\n\t}\n\n\tif (unlikely(tdata == NULL)) {\n\t\treturn true;\n\t}\n\n\tif (likely(tdata->bytes_until_sample >= usize)) {\n\t\tif (update) {\n\t\t\ttdata->bytes_until_sample -= usize;\n\t\t}\n\t\treturn true;\n\t} else {\n\t\tif (tsd_reentrancy_level_get(tsd) > 0) {\n\t\t\treturn true;\n\t\t}\n\t\t/* Compute new sample threshold. */\n\t\tif (update) {\n\t\t\tprof_sample_threshold_update(tdata);\n\t\t}\n\t\treturn !tdata->active;\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE prof_tctx_t *\nprof_alloc_prep(tsd_t *tsd, size_t usize, bool prof_active, bool update) {\n\tprof_tctx_t *ret;\n\tprof_tdata_t *tdata;\n\tprof_bt_t bt;\n\n\tassert(usize == sz_s2u(usize));\n\n\tif (!prof_active || likely(prof_sample_accum_update(tsd, usize, update,\n\t    &tdata))) {\n\t\tret = (prof_tctx_t *)(uintptr_t)1U;\n\t} else {\n\t\tbt_init(&bt, tdata->vec);\n\t\tprof_backtrace(&bt);\n\t\tret = prof_lookup(tsd, &bt);\n\t}\n\n\treturn ret;\n}\n\nJEMALLOC_ALWAYS_INLINE void\nprof_malloc(tsdn_t *tsdn, const void *ptr, size_t usize, alloc_ctx_t *alloc_ctx,\n    prof_tctx_t *tctx) {\n\tcassert(config_prof);\n\tassert(ptr != NULL);\n\tassert(usize == isalloc(tsdn, ptr));\n\n\tif (unlikely((uintptr_t)tctx > (uintptr_t)1U)) {\n\t\tprof_malloc_sample_object(tsdn, ptr, usize, tctx);\n\t} else {\n\t\tprof_tctx_set(tsdn, ptr, usize, alloc_ctx,\n\t\t    (prof_tctx_t *)(uintptr_t)1U);\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE void\nprof_realloc(tsd_t *tsd, const void *ptr, size_t usize, prof_tctx_t *tctx,\n    bool prof_active, bool updated, const void *old_ptr, size_t old_usize,\n    prof_tctx_t *old_tctx) {\n\tbool sampled, old_sampled, moved;\n\n\tcassert(config_prof);\n\tassert(ptr != NULL || (uintptr_t)tctx <= (uintptr_t)1U);\n\n\tif (prof_active && !updated && ptr != NULL) {\n\t\tassert(usize == isalloc(tsd_tsdn(tsd), ptr));\n\t\tif (prof_sample_accum_update(tsd, usize, true, NULL)) {\n\t\t\t/*\n\t\t\t * Don't sample.  The usize passed to prof_alloc_prep()\n\t\t\t * was larger than what actually got allocated, so a\n\t\t\t * backtrace was captured for this allocation, even\n\t\t\t * though its actual usize was insufficient to cross the\n\t\t\t * sample threshold.\n\t\t\t */\n\t\t\tprof_alloc_rollback(tsd, tctx, true);\n\t\t\ttctx = (prof_tctx_t *)(uintptr_t)1U;\n\t\t}\n\t}\n\n\tsampled = ((uintptr_t)tctx > (uintptr_t)1U);\n\told_sampled = ((uintptr_t)old_tctx > (uintptr_t)1U);\n\tmoved = (ptr != old_ptr);\n\n\tif (unlikely(sampled)) {\n\t\tprof_malloc_sample_object(tsd_tsdn(tsd), ptr, usize, tctx);\n\t} else if (moved) {\n\t\tprof_tctx_set(tsd_tsdn(tsd), ptr, usize, NULL,\n\t\t    (prof_tctx_t *)(uintptr_t)1U);\n\t} else if (unlikely(old_sampled)) {\n\t\t/*\n\t\t * prof_tctx_set() would work for the !moved case as well, but\n\t\t * prof_tctx_reset() is slightly cheaper, and the proper thing\n\t\t * to do here in the presence of explicit knowledge re: moved\n\t\t * state.\n\t\t */\n\t\tprof_tctx_reset(tsd_tsdn(tsd), ptr, tctx);\n\t} else {\n\t\tassert((uintptr_t)prof_tctx_get(tsd_tsdn(tsd), ptr, NULL) ==\n\t\t    (uintptr_t)1U);\n\t}\n\n\t/*\n\t * The prof_free_sampled_object() call must come after the\n\t * prof_malloc_sample_object() call, because tctx and old_tctx may be\n\t * the same, in which case reversing the call order could cause the tctx\n\t * to be prematurely destroyed as a side effect of momentarily zeroed\n\t * counters.\n\t */\n\tif (unlikely(old_sampled)) {\n\t\tprof_free_sampled_object(tsd, old_usize, old_tctx);\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE void\nprof_free(tsd_t *tsd, const void *ptr, size_t usize, alloc_ctx_t *alloc_ctx) {\n\tprof_tctx_t *tctx = prof_tctx_get(tsd_tsdn(tsd), ptr, alloc_ctx);\n\n\tcassert(config_prof);\n\tassert(usize == isalloc(tsd_tsdn(tsd), ptr));\n\n\tif (unlikely((uintptr_t)tctx > (uintptr_t)1U)) {\n\t\tprof_free_sampled_object(tsd, usize, tctx);\n\t}\n}\n\n#endif /* JEMALLOC_INTERNAL_PROF_INLINES_B_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/prof_structs.h",
    "content": "#ifndef JEMALLOC_INTERNAL_PROF_STRUCTS_H\n#define JEMALLOC_INTERNAL_PROF_STRUCTS_H\n\n#include \"jemalloc/internal/ckh.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/prng.h\"\n#include \"jemalloc/internal/rb.h\"\n\nstruct prof_bt_s {\n\t/* Backtrace, stored as len program counters. */\n\tvoid\t\t**vec;\n\tunsigned\tlen;\n};\n\n#ifdef JEMALLOC_PROF_LIBGCC\n/* Data structure passed to libgcc _Unwind_Backtrace() callback functions. */\ntypedef struct {\n\tprof_bt_t\t*bt;\n\tunsigned\tmax;\n} prof_unwind_data_t;\n#endif\n\nstruct prof_accum_s {\n#ifndef JEMALLOC_ATOMIC_U64\n\tmalloc_mutex_t\tmtx;\n\tuint64_t\taccumbytes;\n#else\n\tatomic_u64_t\taccumbytes;\n#endif\n};\n\nstruct prof_cnt_s {\n\t/* Profiling counters. */\n\tuint64_t\tcurobjs;\n\tuint64_t\tcurbytes;\n\tuint64_t\taccumobjs;\n\tuint64_t\taccumbytes;\n};\n\ntypedef enum {\n\tprof_tctx_state_initializing,\n\tprof_tctx_state_nominal,\n\tprof_tctx_state_dumping,\n\tprof_tctx_state_purgatory /* Dumper must finish destroying. */\n} prof_tctx_state_t;\n\nstruct prof_tctx_s {\n\t/* Thread data for thread that performed the allocation. */\n\tprof_tdata_t\t\t*tdata;\n\n\t/*\n\t * Copy of tdata->thr_{uid,discrim}, necessary because tdata may be\n\t * defunct during teardown.\n\t */\n\tuint64_t\t\tthr_uid;\n\tuint64_t\t\tthr_discrim;\n\n\t/* Profiling counters, protected by tdata->lock. */\n\tprof_cnt_t\t\tcnts;\n\n\t/* Associated global context. */\n\tprof_gctx_t\t\t*gctx;\n\n\t/*\n\t * UID that distinguishes multiple tctx's created by the same thread,\n\t * but coexisting in gctx->tctxs.  There are two ways that such\n\t * coexistence can occur:\n\t * - A dumper thread can cause a tctx to be retained in the purgatory\n\t *   state.\n\t * - Although a single \"producer\" thread must create all tctx's which\n\t *   share the same thr_uid, multiple \"consumers\" can each concurrently\n\t *   execute portions of prof_tctx_destroy().  prof_tctx_destroy() only\n\t *   gets called once each time cnts.cur{objs,bytes} drop to 0, but this\n\t *   threshold can be hit again before the first consumer finishes\n\t *   executing prof_tctx_destroy().\n\t */\n\tuint64_t\t\ttctx_uid;\n\n\t/* Linkage into gctx's tctxs. */\n\trb_node(prof_tctx_t)\ttctx_link;\n\n\t/*\n\t * True during prof_alloc_prep()..prof_malloc_sample_object(), prevents\n\t * sample vs destroy race.\n\t */\n\tbool\t\t\tprepared;\n\n\t/* Current dump-related state, protected by gctx->lock. */\n\tprof_tctx_state_t\tstate;\n\n\t/*\n\t * Copy of cnts snapshotted during early dump phase, protected by\n\t * dump_mtx.\n\t */\n\tprof_cnt_t\t\tdump_cnts;\n};\ntypedef rb_tree(prof_tctx_t) prof_tctx_tree_t;\n\nstruct prof_gctx_s {\n\t/* Protects nlimbo, cnt_summed, and tctxs. */\n\tmalloc_mutex_t\t\t*lock;\n\n\t/*\n\t * Number of threads that currently cause this gctx to be in a state of\n\t * limbo due to one of:\n\t *   - Initializing this gctx.\n\t *   - Initializing per thread counters associated with this gctx.\n\t *   - Preparing to destroy this gctx.\n\t *   - Dumping a heap profile that includes this gctx.\n\t * nlimbo must be 1 (single destroyer) in order to safely destroy the\n\t * gctx.\n\t */\n\tunsigned\t\tnlimbo;\n\n\t/*\n\t * Tree of profile counters, one for each thread that has allocated in\n\t * this context.\n\t */\n\tprof_tctx_tree_t\ttctxs;\n\n\t/* Linkage for tree of contexts to be dumped. */\n\trb_node(prof_gctx_t)\tdump_link;\n\n\t/* Temporary storage for summation during dump. */\n\tprof_cnt_t\t\tcnt_summed;\n\n\t/* Associated backtrace. */\n\tprof_bt_t\t\tbt;\n\n\t/* Backtrace vector, variable size, referred to by bt. */\n\tvoid\t\t\t*vec[1];\n};\ntypedef rb_tree(prof_gctx_t) prof_gctx_tree_t;\n\nstruct prof_tdata_s {\n\tmalloc_mutex_t\t\t*lock;\n\n\t/* Monotonically increasing unique thread identifier. */\n\tuint64_t\t\tthr_uid;\n\n\t/*\n\t * Monotonically increasing discriminator among tdata structures\n\t * associated with the same thr_uid.\n\t */\n\tuint64_t\t\tthr_discrim;\n\n\t/* Included in heap profile dumps if non-NULL. */\n\tchar\t\t\t*thread_name;\n\n\tbool\t\t\tattached;\n\tbool\t\t\texpired;\n\n\trb_node(prof_tdata_t)\ttdata_link;\n\n\t/*\n\t * Counter used to initialize prof_tctx_t's tctx_uid.  No locking is\n\t * necessary when incrementing this field, because only one thread ever\n\t * does so.\n\t */\n\tuint64_t\t\ttctx_uid_next;\n\n\t/*\n\t * Hash of (prof_bt_t *)-->(prof_tctx_t *).  Each thread tracks\n\t * backtraces for which it has non-zero allocation/deallocation counters\n\t * associated with thread-specific prof_tctx_t objects.  Other threads\n\t * may write to prof_tctx_t contents when freeing associated objects.\n\t */\n\tckh_t\t\t\tbt2tctx;\n\n\t/* Sampling state. */\n\tuint64_t\t\tprng_state;\n\tuint64_t\t\tbytes_until_sample;\n\n\t/* State used to avoid dumping while operating on prof internals. */\n\tbool\t\t\tenq;\n\tbool\t\t\tenq_idump;\n\tbool\t\t\tenq_gdump;\n\n\t/*\n\t * Set to true during an early dump phase for tdata's which are\n\t * currently being dumped.  New threads' tdata's have this initialized\n\t * to false so that they aren't accidentally included in later dump\n\t * phases.\n\t */\n\tbool\t\t\tdumping;\n\n\t/*\n\t * True if profiling is active for this tdata's thread\n\t * (thread.prof.active mallctl).\n\t */\n\tbool\t\t\tactive;\n\n\t/* Temporary storage for summation during dump. */\n\tprof_cnt_t\t\tcnt_summed;\n\n\t/* Backtrace vector, used for calls to prof_backtrace(). */\n\tvoid\t\t\t*vec[PROF_BT_MAX];\n};\ntypedef rb_tree(prof_tdata_t) prof_tdata_tree_t;\n\n#endif /* JEMALLOC_INTERNAL_PROF_STRUCTS_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/prof_types.h",
    "content": "#ifndef JEMALLOC_INTERNAL_PROF_TYPES_H\n#define JEMALLOC_INTERNAL_PROF_TYPES_H\n\ntypedef struct prof_bt_s prof_bt_t;\ntypedef struct prof_accum_s prof_accum_t;\ntypedef struct prof_cnt_s prof_cnt_t;\ntypedef struct prof_tctx_s prof_tctx_t;\ntypedef struct prof_gctx_s prof_gctx_t;\ntypedef struct prof_tdata_s prof_tdata_t;\n\n/* Option defaults. */\n#ifdef JEMALLOC_PROF\n#  define PROF_PREFIX_DEFAULT\t\t\"jeprof\"\n#else\n#  define PROF_PREFIX_DEFAULT\t\t\"\"\n#endif\n#define LG_PROF_SAMPLE_DEFAULT\t\t19\n#define LG_PROF_INTERVAL_DEFAULT\t-1\n\n/*\n * Hard limit on stack backtrace depth.  The version of prof_backtrace() that\n * is based on __builtin_return_address() necessarily has a hard-coded number\n * of backtrace frame handlers, and should be kept in sync with this setting.\n */\n#define PROF_BT_MAX\t\t\t128\n\n/* Initial hash table size. */\n#define PROF_CKH_MINITEMS\t\t64\n\n/* Size of memory buffer to use when writing dump files. */\n#define PROF_DUMP_BUFSIZE\t\t65536\n\n/* Size of stack-allocated buffer used by prof_printf(). */\n#define PROF_PRINTF_BUFSIZE\t\t128\n\n/*\n * Number of mutexes shared among all gctx's.  No space is allocated for these\n * unless profiling is enabled, so it's okay to over-provision.\n */\n#define PROF_NCTX_LOCKS\t\t\t1024\n\n/*\n * Number of mutexes shared among all tdata's.  No space is allocated for these\n * unless profiling is enabled, so it's okay to over-provision.\n */\n#define PROF_NTDATA_LOCKS\t\t256\n\n/*\n * prof_tdata pointers close to NULL are used to encode state information that\n * is used for cleaning up during thread shutdown.\n */\n#define PROF_TDATA_STATE_REINCARNATED\t((prof_tdata_t *)(uintptr_t)1)\n#define PROF_TDATA_STATE_PURGATORY\t((prof_tdata_t *)(uintptr_t)2)\n#define PROF_TDATA_STATE_MAX\t\tPROF_TDATA_STATE_PURGATORY\n\n#endif /* JEMALLOC_INTERNAL_PROF_TYPES_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/public_namespace.sh",
    "content": "#!/bin/sh\n\nfor nm in `cat $1` ; do\n  n=`echo ${nm} |tr ':' ' ' |awk '{print $1}'`\n  echo \"#define je_${n} JEMALLOC_N(${n})\"\ndone\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/public_unnamespace.sh",
    "content": "#!/bin/sh\n\nfor nm in `cat $1` ; do\n  n=`echo ${nm} |tr ':' ' ' |awk '{print $1}'`\n  echo \"#undef je_${n}\"\ndone\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/ql.h",
    "content": "#ifndef JEMALLOC_INTERNAL_QL_H\n#define JEMALLOC_INTERNAL_QL_H\n\n#include \"jemalloc/internal/qr.h\"\n\n/* List definitions. */\n#define ql_head(a_type)\t\t\t\t\t\t\t\\\nstruct {\t\t\t\t\t\t\t\t\\\n\ta_type *qlh_first;\t\t\t\t\t\t\\\n}\n\n#define ql_head_initializer(a_head) {NULL}\n\n#define ql_elm(a_type)\tqr(a_type)\n\n/* List functions. */\n#define ql_new(a_head) do {\t\t\t\t\t\t\\\n\t(a_head)->qlh_first = NULL;\t\t\t\t\t\\\n} while (0)\n\n#define ql_elm_new(a_elm, a_field) qr_new((a_elm), a_field)\n\n#define ql_first(a_head) ((a_head)->qlh_first)\n\n#define ql_last(a_head, a_field)\t\t\t\t\t\\\n\t((ql_first(a_head) != NULL)\t\t\t\t\t\\\n\t    ? qr_prev(ql_first(a_head), a_field) : NULL)\n\n#define ql_next(a_head, a_elm, a_field)\t\t\t\t\t\\\n\t((ql_last(a_head, a_field) != (a_elm))\t\t\t\t\\\n\t    ? qr_next((a_elm), a_field)\t: NULL)\n\n#define ql_prev(a_head, a_elm, a_field)\t\t\t\t\t\\\n\t((ql_first(a_head) != (a_elm)) ? qr_prev((a_elm), a_field)\t\\\n\t\t\t\t       : NULL)\n\n#define ql_before_insert(a_head, a_qlelm, a_elm, a_field) do {\t\t\\\n\tqr_before_insert((a_qlelm), (a_elm), a_field);\t\t\t\\\n\tif (ql_first(a_head) == (a_qlelm)) {\t\t\t\t\\\n\t\tql_first(a_head) = (a_elm);\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define ql_after_insert(a_qlelm, a_elm, a_field)\t\t\t\\\n\tqr_after_insert((a_qlelm), (a_elm), a_field)\n\n#define ql_head_insert(a_head, a_elm, a_field) do {\t\t\t\\\n\tif (ql_first(a_head) != NULL) {\t\t\t\t\t\\\n\t\tqr_before_insert(ql_first(a_head), (a_elm), a_field);\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tql_first(a_head) = (a_elm);\t\t\t\t\t\\\n} while (0)\n\n#define ql_tail_insert(a_head, a_elm, a_field) do {\t\t\t\\\n\tif (ql_first(a_head) != NULL) {\t\t\t\t\t\\\n\t\tqr_before_insert(ql_first(a_head), (a_elm), a_field);\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tql_first(a_head) = qr_next((a_elm), a_field);\t\t\t\\\n} while (0)\n\n#define ql_remove(a_head, a_elm, a_field) do {\t\t\t\t\\\n\tif (ql_first(a_head) == (a_elm)) {\t\t\t\t\\\n\t\tql_first(a_head) = qr_next(ql_first(a_head), a_field);\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tif (ql_first(a_head) != (a_elm)) {\t\t\t\t\\\n\t\tqr_remove((a_elm), a_field);\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t\tql_first(a_head) = NULL;\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define ql_head_remove(a_head, a_type, a_field) do {\t\t\t\\\n\ta_type *t = ql_first(a_head);\t\t\t\t\t\\\n\tql_remove((a_head), t, a_field);\t\t\t\t\\\n} while (0)\n\n#define ql_tail_remove(a_head, a_type, a_field) do {\t\t\t\\\n\ta_type *t = ql_last(a_head, a_field);\t\t\t\t\\\n\tql_remove((a_head), t, a_field);\t\t\t\t\\\n} while (0)\n\n#define ql_foreach(a_var, a_head, a_field)\t\t\t\t\\\n\tqr_foreach((a_var), ql_first(a_head), a_field)\n\n#define ql_reverse_foreach(a_var, a_head, a_field)\t\t\t\\\n\tqr_reverse_foreach((a_var), ql_first(a_head), a_field)\n\n#endif /* JEMALLOC_INTERNAL_QL_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/qr.h",
    "content": "#ifndef JEMALLOC_INTERNAL_QR_H\n#define JEMALLOC_INTERNAL_QR_H\n\n/* Ring definitions. */\n#define qr(a_type)\t\t\t\t\t\t\t\\\nstruct {\t\t\t\t\t\t\t\t\\\n\ta_type\t*qre_next;\t\t\t\t\t\t\\\n\ta_type\t*qre_prev;\t\t\t\t\t\t\\\n}\n\n/* Ring functions. */\n#define qr_new(a_qr, a_field) do {\t\t\t\t\t\\\n\t(a_qr)->a_field.qre_next = (a_qr);\t\t\t\t\\\n\t(a_qr)->a_field.qre_prev = (a_qr);\t\t\t\t\\\n} while (0)\n\n#define qr_next(a_qr, a_field) ((a_qr)->a_field.qre_next)\n\n#define qr_prev(a_qr, a_field) ((a_qr)->a_field.qre_prev)\n\n#define qr_before_insert(a_qrelm, a_qr, a_field) do {\t\t\t\\\n\t(a_qr)->a_field.qre_prev = (a_qrelm)->a_field.qre_prev;\t\t\\\n\t(a_qr)->a_field.qre_next = (a_qrelm);\t\t\t\t\\\n\t(a_qr)->a_field.qre_prev->a_field.qre_next = (a_qr);\t\t\\\n\t(a_qrelm)->a_field.qre_prev = (a_qr);\t\t\t\t\\\n} while (0)\n\n#define qr_after_insert(a_qrelm, a_qr, a_field) do {\t\t\t\\\n\t(a_qr)->a_field.qre_next = (a_qrelm)->a_field.qre_next;\t\t\\\n\t(a_qr)->a_field.qre_prev = (a_qrelm);\t\t\t\t\\\n\t(a_qr)->a_field.qre_next->a_field.qre_prev = (a_qr);\t\t\\\n\t(a_qrelm)->a_field.qre_next = (a_qr);\t\t\t\t\\\n} while (0)\n\n#define qr_meld(a_qr_a, a_qr_b, a_type, a_field) do {\t\t\t\\\n\ta_type *t;\t\t\t\t\t\t\t\\\n\t(a_qr_a)->a_field.qre_prev->a_field.qre_next = (a_qr_b);\t\\\n\t(a_qr_b)->a_field.qre_prev->a_field.qre_next = (a_qr_a);\t\\\n\tt = (a_qr_a)->a_field.qre_prev;\t\t\t\t\t\\\n\t(a_qr_a)->a_field.qre_prev = (a_qr_b)->a_field.qre_prev;\t\\\n\t(a_qr_b)->a_field.qre_prev = t;\t\t\t\t\t\\\n} while (0)\n\n/*\n * qr_meld() and qr_split() are functionally equivalent, so there's no need to\n * have two copies of the code.\n */\n#define qr_split(a_qr_a, a_qr_b, a_type, a_field)\t\t\t\\\n\tqr_meld((a_qr_a), (a_qr_b), a_type, a_field)\n\n#define qr_remove(a_qr, a_field) do {\t\t\t\t\t\\\n\t(a_qr)->a_field.qre_prev->a_field.qre_next\t\t\t\\\n\t    = (a_qr)->a_field.qre_next;\t\t\t\t\t\\\n\t(a_qr)->a_field.qre_next->a_field.qre_prev\t\t\t\\\n\t    = (a_qr)->a_field.qre_prev;\t\t\t\t\t\\\n\t(a_qr)->a_field.qre_next = (a_qr);\t\t\t\t\\\n\t(a_qr)->a_field.qre_prev = (a_qr);\t\t\t\t\\\n} while (0)\n\n#define qr_foreach(var, a_qr, a_field)\t\t\t\t\t\\\n\tfor ((var) = (a_qr);\t\t\t\t\t\t\\\n\t    (var) != NULL;\t\t\t\t\t\t\\\n\t    (var) = (((var)->a_field.qre_next != (a_qr))\t\t\\\n\t    ? (var)->a_field.qre_next : NULL))\n\n#define qr_reverse_foreach(var, a_qr, a_field)\t\t\t\t\\\n\tfor ((var) = ((a_qr) != NULL) ? qr_prev(a_qr, a_field) : NULL;\t\\\n\t    (var) != NULL;\t\t\t\t\t\t\\\n\t    (var) = (((var) != (a_qr))\t\t\t\t\t\\\n\t    ? (var)->a_field.qre_prev : NULL))\n\n#endif /* JEMALLOC_INTERNAL_QR_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/rb.h",
    "content": "/*-\n *******************************************************************************\n *\n * cpp macro implementation of left-leaning 2-3 red-black trees.  Parent\n * pointers are not used, and color bits are stored in the least significant\n * bit of right-child pointers (if RB_COMPACT is defined), thus making node\n * linkage as compact as is possible for red-black trees.\n *\n * Usage:\n *\n *   #include <stdint.h>\n *   #include <stdbool.h>\n *   #define NDEBUG // (Optional, see assert(3).)\n *   #include <assert.h>\n *   #define RB_COMPACT // (Optional, embed color bits in right-child pointers.)\n *   #include <rb.h>\n *   ...\n *\n *******************************************************************************\n */\n\n#ifndef RB_H_\n#define RB_H_\n\n#ifndef __PGI\n#define RB_COMPACT\n#endif\n\n#ifdef RB_COMPACT\n/* Node structure. */\n#define rb_node(a_type)\t\t\t\t\t\t\t\\\nstruct {\t\t\t\t\t\t\t\t\\\n    a_type *rbn_left;\t\t\t\t\t\t\t\\\n    a_type *rbn_right_red;\t\t\t\t\t\t\\\n}\n#else\n#define rb_node(a_type)\t\t\t\t\t\t\t\\\nstruct {\t\t\t\t\t\t\t\t\\\n    a_type *rbn_left;\t\t\t\t\t\t\t\\\n    a_type *rbn_right;\t\t\t\t\t\t\t\\\n    bool rbn_red;\t\t\t\t\t\t\t\\\n}\n#endif\n\n/* Root structure. */\n#define rb_tree(a_type)\t\t\t\t\t\t\t\\\nstruct {\t\t\t\t\t\t\t\t\\\n    a_type *rbt_root;\t\t\t\t\t\t\t\\\n}\n\n/* Left accessors. */\n#define rbtn_left_get(a_type, a_field, a_node)\t\t\t\t\\\n    ((a_node)->a_field.rbn_left)\n#define rbtn_left_set(a_type, a_field, a_node, a_left) do {\t\t\\\n    (a_node)->a_field.rbn_left = a_left;\t\t\t\t\\\n} while (0)\n\n#ifdef RB_COMPACT\n/* Right accessors. */\n#define rbtn_right_get(a_type, a_field, a_node)\t\t\t\t\\\n    ((a_type *) (((intptr_t) (a_node)->a_field.rbn_right_red)\t\t\\\n      & ((ssize_t)-2)))\n#define rbtn_right_set(a_type, a_field, a_node, a_right) do {\t\t\\\n    (a_node)->a_field.rbn_right_red = (a_type *) (((uintptr_t) a_right)\t\\\n      | (((uintptr_t) (a_node)->a_field.rbn_right_red) & ((size_t)1)));\t\\\n} while (0)\n\n/* Color accessors. */\n#define rbtn_red_get(a_type, a_field, a_node)\t\t\t\t\\\n    ((bool) (((uintptr_t) (a_node)->a_field.rbn_right_red)\t\t\\\n      & ((size_t)1)))\n#define rbtn_color_set(a_type, a_field, a_node, a_red) do {\t\t\\\n    (a_node)->a_field.rbn_right_red = (a_type *) ((((intptr_t)\t\t\\\n      (a_node)->a_field.rbn_right_red) & ((ssize_t)-2))\t\t\t\\\n      | ((ssize_t)a_red));\t\t\t\t\t\t\\\n} while (0)\n#define rbtn_red_set(a_type, a_field, a_node) do {\t\t\t\\\n    (a_node)->a_field.rbn_right_red = (a_type *) (((uintptr_t)\t\t\\\n      (a_node)->a_field.rbn_right_red) | ((size_t)1));\t\t\t\\\n} while (0)\n#define rbtn_black_set(a_type, a_field, a_node) do {\t\t\t\\\n    (a_node)->a_field.rbn_right_red = (a_type *) (((intptr_t)\t\t\\\n      (a_node)->a_field.rbn_right_red) & ((ssize_t)-2));\t\t\\\n} while (0)\n\n/* Node initializer. */\n#define rbt_node_new(a_type, a_field, a_rbt, a_node) do {\t\t\\\n    /* Bookkeeping bit cannot be used by node pointer. */\t\t\\\n    assert(((uintptr_t)(a_node) & 0x1) == 0);\t\t\t\t\\\n    rbtn_left_set(a_type, a_field, (a_node), NULL);\t\\\n    rbtn_right_set(a_type, a_field, (a_node), NULL);\t\\\n    rbtn_red_set(a_type, a_field, (a_node));\t\t\t\t\\\n} while (0)\n#else\n/* Right accessors. */\n#define rbtn_right_get(a_type, a_field, a_node)\t\t\t\t\\\n    ((a_node)->a_field.rbn_right)\n#define rbtn_right_set(a_type, a_field, a_node, a_right) do {\t\t\\\n    (a_node)->a_field.rbn_right = a_right;\t\t\t\t\\\n} while (0)\n\n/* Color accessors. */\n#define rbtn_red_get(a_type, a_field, a_node)\t\t\t\t\\\n    ((a_node)->a_field.rbn_red)\n#define rbtn_color_set(a_type, a_field, a_node, a_red) do {\t\t\\\n    (a_node)->a_field.rbn_red = (a_red);\t\t\t\t\\\n} while (0)\n#define rbtn_red_set(a_type, a_field, a_node) do {\t\t\t\\\n    (a_node)->a_field.rbn_red = true;\t\t\t\t\t\\\n} while (0)\n#define rbtn_black_set(a_type, a_field, a_node) do {\t\t\t\\\n    (a_node)->a_field.rbn_red = false;\t\t\t\t\t\\\n} while (0)\n\n/* Node initializer. */\n#define rbt_node_new(a_type, a_field, a_rbt, a_node) do {\t\t\\\n    rbtn_left_set(a_type, a_field, (a_node), NULL);\t\\\n    rbtn_right_set(a_type, a_field, (a_node), NULL);\t\\\n    rbtn_red_set(a_type, a_field, (a_node));\t\t\t\t\\\n} while (0)\n#endif\n\n/* Tree initializer. */\n#define rb_new(a_type, a_field, a_rbt) do {\t\t\t\t\\\n    (a_rbt)->rbt_root = NULL;\t\t\t\t\t\t\\\n} while (0)\n\n/* Internal utility macros. */\n#define rbtn_first(a_type, a_field, a_rbt, a_root, r_node) do {\t\t\\\n    (r_node) = (a_root);\t\t\t\t\t\t\\\n    if ((r_node) != NULL) {\t\t\t\t\t\t\\\n\tfor (;\t\t\t\t\t\t\t\t\\\n\t  rbtn_left_get(a_type, a_field, (r_node)) != NULL;\t\t\\\n\t  (r_node) = rbtn_left_get(a_type, a_field, (r_node))) {\t\\\n\t}\t\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define rbtn_last(a_type, a_field, a_rbt, a_root, r_node) do {\t\t\\\n    (r_node) = (a_root);\t\t\t\t\t\t\\\n    if ((r_node) != NULL) {\t\t\t\t\t\t\\\n\tfor (; rbtn_right_get(a_type, a_field, (r_node)) != NULL;\t\\\n\t  (r_node) = rbtn_right_get(a_type, a_field, (r_node))) {\t\\\n\t}\t\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define rbtn_rotate_left(a_type, a_field, a_node, r_node) do {\t\t\\\n    (r_node) = rbtn_right_get(a_type, a_field, (a_node));\t\t\\\n    rbtn_right_set(a_type, a_field, (a_node),\t\t\t\t\\\n      rbtn_left_get(a_type, a_field, (r_node)));\t\t\t\\\n    rbtn_left_set(a_type, a_field, (r_node), (a_node));\t\t\t\\\n} while (0)\n\n#define rbtn_rotate_right(a_type, a_field, a_node, r_node) do {\t\t\\\n    (r_node) = rbtn_left_get(a_type, a_field, (a_node));\t\t\\\n    rbtn_left_set(a_type, a_field, (a_node),\t\t\t\t\\\n      rbtn_right_get(a_type, a_field, (r_node)));\t\t\t\\\n    rbtn_right_set(a_type, a_field, (r_node), (a_node));\t\t\\\n} while (0)\n\n/*\n * The rb_proto() macro generates function prototypes that correspond to the\n * functions generated by an equivalently parameterized call to rb_gen().\n */\n\n#define rb_proto(a_attr, a_prefix, a_rbt_type, a_type)\t\t\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##new(a_rbt_type *rbtree);\t\t\t\t\t\\\na_attr bool\t\t\t\t\t\t\t\t\\\na_prefix##empty(a_rbt_type *rbtree);\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##first(a_rbt_type *rbtree);\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##last(a_rbt_type *rbtree);\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##next(a_rbt_type *rbtree, a_type *node);\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##prev(a_rbt_type *rbtree, a_type *node);\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##search(a_rbt_type *rbtree, const a_type *key);\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##nsearch(a_rbt_type *rbtree, const a_type *key);\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##psearch(a_rbt_type *rbtree, const a_type *key);\t\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##insert(a_rbt_type *rbtree, a_type *node);\t\t\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##remove(a_rbt_type *rbtree, a_type *node);\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##iter(a_rbt_type *rbtree, a_type *start, a_type *(*cb)(\t\\\n  a_rbt_type *, a_type *, void *), void *arg);\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##reverse_iter(a_rbt_type *rbtree, a_type *start,\t\t\\\n  a_type *(*cb)(a_rbt_type *, a_type *, void *), void *arg);\t\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##destroy(a_rbt_type *rbtree, void (*cb)(a_type *, void *),\t\\\n  void *arg);\n\n/*\n * The rb_gen() macro generates a type-specific red-black tree implementation,\n * based on the above cpp macros.\n *\n * Arguments:\n *\n *   a_attr    : Function attribute for generated functions (ex: static).\n *   a_prefix  : Prefix for generated functions (ex: ex_).\n *   a_rb_type : Type for red-black tree data structure (ex: ex_t).\n *   a_type    : Type for red-black tree node data structure (ex: ex_node_t).\n *   a_field   : Name of red-black tree node linkage (ex: ex_link).\n *   a_cmp     : Node comparison function name, with the following prototype:\n *                 int (a_cmp *)(a_type *a_node, a_type *a_other);\n *                                       ^^^^^^\n *                                    or a_key\n *               Interpretation of comparison function return values:\n *                 -1 : a_node <  a_other\n *                  0 : a_node == a_other\n *                  1 : a_node >  a_other\n *               In all cases, the a_node or a_key macro argument is the first\n *               argument to the comparison function, which makes it possible\n *               to write comparison functions that treat the first argument\n *               specially.\n *\n * Assuming the following setup:\n *\n *   typedef struct ex_node_s ex_node_t;\n *   struct ex_node_s {\n *       rb_node(ex_node_t) ex_link;\n *   };\n *   typedef rb_tree(ex_node_t) ex_t;\n *   rb_gen(static, ex_, ex_t, ex_node_t, ex_link, ex_cmp)\n *\n * The following API is generated:\n *\n *   static void\n *   ex_new(ex_t *tree);\n *       Description: Initialize a red-black tree structure.\n *       Args:\n *         tree: Pointer to an uninitialized red-black tree object.\n *\n *   static bool\n *   ex_empty(ex_t *tree);\n *       Description: Determine whether tree is empty.\n *       Args:\n *         tree: Pointer to an initialized red-black tree object.\n *       Ret: True if tree is empty, false otherwise.\n *\n *   static ex_node_t *\n *   ex_first(ex_t *tree);\n *   static ex_node_t *\n *   ex_last(ex_t *tree);\n *       Description: Get the first/last node in tree.\n *       Args:\n *         tree: Pointer to an initialized red-black tree object.\n *       Ret: First/last node in tree, or NULL if tree is empty.\n *\n *   static ex_node_t *\n *   ex_next(ex_t *tree, ex_node_t *node);\n *   static ex_node_t *\n *   ex_prev(ex_t *tree, ex_node_t *node);\n *       Description: Get node's successor/predecessor.\n *       Args:\n *         tree: Pointer to an initialized red-black tree object.\n *         node: A node in tree.\n *       Ret: node's successor/predecessor in tree, or NULL if node is\n *            last/first.\n *\n *   static ex_node_t *\n *   ex_search(ex_t *tree, const ex_node_t *key);\n *       Description: Search for node that matches key.\n *       Args:\n *         tree: Pointer to an initialized red-black tree object.\n *         key : Search key.\n *       Ret: Node in tree that matches key, or NULL if no match.\n *\n *   static ex_node_t *\n *   ex_nsearch(ex_t *tree, const ex_node_t *key);\n *   static ex_node_t *\n *   ex_psearch(ex_t *tree, const ex_node_t *key);\n *       Description: Search for node that matches key.  If no match is found,\n *                    return what would be key's successor/predecessor, were\n *                    key in tree.\n *       Args:\n *         tree: Pointer to an initialized red-black tree object.\n *         key : Search key.\n *       Ret: Node in tree that matches key, or if no match, hypothetical node's\n *            successor/predecessor (NULL if no successor/predecessor).\n *\n *   static void\n *   ex_insert(ex_t *tree, ex_node_t *node);\n *       Description: Insert node into tree.\n *       Args:\n *         tree: Pointer to an initialized red-black tree object.\n *         node: Node to be inserted into tree.\n *\n *   static void\n *   ex_remove(ex_t *tree, ex_node_t *node);\n *       Description: Remove node from tree.\n *       Args:\n *         tree: Pointer to an initialized red-black tree object.\n *         node: Node in tree to be removed.\n *\n *   static ex_node_t *\n *   ex_iter(ex_t *tree, ex_node_t *start, ex_node_t *(*cb)(ex_t *,\n *     ex_node_t *, void *), void *arg);\n *   static ex_node_t *\n *   ex_reverse_iter(ex_t *tree, ex_node_t *start, ex_node *(*cb)(ex_t *,\n *     ex_node_t *, void *), void *arg);\n *       Description: Iterate forward/backward over tree, starting at node.  If\n *                    tree is modified, iteration must be immediately\n *                    terminated by the callback function that causes the\n *                    modification.\n *       Args:\n *         tree : Pointer to an initialized red-black tree object.\n *         start: Node at which to start iteration, or NULL to start at\n *                first/last node.\n *         cb   : Callback function, which is called for each node during\n *                iteration.  Under normal circumstances the callback function\n *                should return NULL, which causes iteration to continue.  If a\n *                callback function returns non-NULL, iteration is immediately\n *                terminated and the non-NULL return value is returned by the\n *                iterator.  This is useful for re-starting iteration after\n *                modifying tree.\n *         arg  : Opaque pointer passed to cb().\n *       Ret: NULL if iteration completed, or the non-NULL callback return value\n *            that caused termination of the iteration.\n *\n *   static void\n *   ex_destroy(ex_t *tree, void (*cb)(ex_node_t *, void *), void *arg);\n *       Description: Iterate over the tree with post-order traversal, remove\n *                    each node, and run the callback if non-null.  This is\n *                    used for destroying a tree without paying the cost to\n *                    rebalance it.  The tree must not be otherwise altered\n *                    during traversal.\n *       Args:\n *         tree: Pointer to an initialized red-black tree object.\n *         cb  : Callback function, which, if non-null, is called for each node\n *               during iteration.  There is no way to stop iteration once it\n *               has begun.\n *         arg : Opaque pointer passed to cb().\n */\n#define rb_gen(a_attr, a_prefix, a_rbt_type, a_type, a_field, a_cmp)\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##new(a_rbt_type *rbtree) {\t\t\t\t\t\\\n    rb_new(a_type, a_field, rbtree);\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr bool\t\t\t\t\t\t\t\t\\\na_prefix##empty(a_rbt_type *rbtree) {\t\t\t\t\t\\\n    return (rbtree->rbt_root == NULL);\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##first(a_rbt_type *rbtree) {\t\t\t\t\t\\\n    a_type *ret;\t\t\t\t\t\t\t\\\n    rbtn_first(a_type, a_field, rbtree, rbtree->rbt_root, ret);\t\t\\\n    return ret;\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##last(a_rbt_type *rbtree) {\t\t\t\t\t\\\n    a_type *ret;\t\t\t\t\t\t\t\\\n    rbtn_last(a_type, a_field, rbtree, rbtree->rbt_root, ret);\t\t\\\n    return ret;\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##next(a_rbt_type *rbtree, a_type *node) {\t\t\t\\\n    a_type *ret;\t\t\t\t\t\t\t\\\n    if (rbtn_right_get(a_type, a_field, node) != NULL) {\t\t\\\n\trbtn_first(a_type, a_field, rbtree, rbtn_right_get(a_type,\t\\\n\t  a_field, node), ret);\t\t\t\t\t\t\\\n    } else {\t\t\t\t\t\t\t\t\\\n\ta_type *tnode = rbtree->rbt_root;\t\t\t\t\\\n\tassert(tnode != NULL);\t\t\t\t\t\t\\\n\tret = NULL;\t\t\t\t\t\t\t\\\n\twhile (true) {\t\t\t\t\t\t\t\\\n\t    int cmp = (a_cmp)(node, tnode);\t\t\t\t\\\n\t    if (cmp < 0) {\t\t\t\t\t\t\\\n\t\tret = tnode;\t\t\t\t\t\t\\\n\t\ttnode = rbtn_left_get(a_type, a_field, tnode);\t\t\\\n\t    } else if (cmp > 0) {\t\t\t\t\t\\\n\t\ttnode = rbtn_right_get(a_type, a_field, tnode);\t\t\\\n\t    } else {\t\t\t\t\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\t    }\t\t\t\t\t\t\t\t\\\n\t    assert(tnode != NULL);\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n    return ret;\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##prev(a_rbt_type *rbtree, a_type *node) {\t\t\t\\\n    a_type *ret;\t\t\t\t\t\t\t\\\n    if (rbtn_left_get(a_type, a_field, node) != NULL) {\t\t\t\\\n\trbtn_last(a_type, a_field, rbtree, rbtn_left_get(a_type,\t\\\n\t  a_field, node), ret);\t\t\t\t\t\t\\\n    } else {\t\t\t\t\t\t\t\t\\\n\ta_type *tnode = rbtree->rbt_root;\t\t\t\t\\\n\tassert(tnode != NULL);\t\t\t\t\t\t\\\n\tret = NULL;\t\t\t\t\t\t\t\\\n\twhile (true) {\t\t\t\t\t\t\t\\\n\t    int cmp = (a_cmp)(node, tnode);\t\t\t\t\\\n\t    if (cmp < 0) {\t\t\t\t\t\t\\\n\t\ttnode = rbtn_left_get(a_type, a_field, tnode);\t\t\\\n\t    } else if (cmp > 0) {\t\t\t\t\t\\\n\t\tret = tnode;\t\t\t\t\t\t\\\n\t\ttnode = rbtn_right_get(a_type, a_field, tnode);\t\t\\\n\t    } else {\t\t\t\t\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\t    }\t\t\t\t\t\t\t\t\\\n\t    assert(tnode != NULL);\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n    return ret;\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##search(a_rbt_type *rbtree, const a_type *key) {\t\t\\\n    a_type *ret;\t\t\t\t\t\t\t\\\n    int cmp;\t\t\t\t\t\t\t\t\\\n    ret = rbtree->rbt_root;\t\t\t\t\t\t\\\n    while (ret != NULL\t\t\t\t\t\t\t\\\n      && (cmp = (a_cmp)(key, ret)) != 0) {\t\t\t\t\\\n\tif (cmp < 0) {\t\t\t\t\t\t\t\\\n\t    ret = rbtn_left_get(a_type, a_field, ret);\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t    ret = rbtn_right_get(a_type, a_field, ret);\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n    return ret;\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##nsearch(a_rbt_type *rbtree, const a_type *key) {\t\t\\\n    a_type *ret;\t\t\t\t\t\t\t\\\n    a_type *tnode = rbtree->rbt_root;\t\t\t\t\t\\\n    ret = NULL;\t\t\t\t\t\t\t\t\\\n    while (tnode != NULL) {\t\t\t\t\t\t\\\n\tint cmp = (a_cmp)(key, tnode);\t\t\t\t\t\\\n\tif (cmp < 0) {\t\t\t\t\t\t\t\\\n\t    ret = tnode;\t\t\t\t\t\t\\\n\t    tnode = rbtn_left_get(a_type, a_field, tnode);\t\t\\\n\t} else if (cmp > 0) {\t\t\t\t\t\t\\\n\t    tnode = rbtn_right_get(a_type, a_field, tnode);\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t    ret = tnode;\t\t\t\t\t\t\\\n\t    break;\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n    return ret;\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##psearch(a_rbt_type *rbtree, const a_type *key) {\t\t\\\n    a_type *ret;\t\t\t\t\t\t\t\\\n    a_type *tnode = rbtree->rbt_root;\t\t\t\t\t\\\n    ret = NULL;\t\t\t\t\t\t\t\t\\\n    while (tnode != NULL) {\t\t\t\t\t\t\\\n\tint cmp = (a_cmp)(key, tnode);\t\t\t\t\t\\\n\tif (cmp < 0) {\t\t\t\t\t\t\t\\\n\t    tnode = rbtn_left_get(a_type, a_field, tnode);\t\t\\\n\t} else if (cmp > 0) {\t\t\t\t\t\t\\\n\t    ret = tnode;\t\t\t\t\t\t\\\n\t    tnode = rbtn_right_get(a_type, a_field, tnode);\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t    ret = tnode;\t\t\t\t\t\t\\\n\t    break;\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n    return ret;\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##insert(a_rbt_type *rbtree, a_type *node) {\t\t\t\\\n    struct {\t\t\t\t\t\t\t\t\\\n\ta_type *node;\t\t\t\t\t\t\t\\\n\tint cmp;\t\t\t\t\t\t\t\\\n    } path[sizeof(void *) << 4], *pathp;\t\t\t\t\\\n    rbt_node_new(a_type, a_field, rbtree, node);\t\t\t\\\n    /* Wind. */\t\t\t\t\t\t\t\t\\\n    path->node = rbtree->rbt_root;\t\t\t\t\t\\\n    for (pathp = path; pathp->node != NULL; pathp++) {\t\t\t\\\n\tint cmp = pathp->cmp = a_cmp(node, pathp->node);\t\t\\\n\tassert(cmp != 0);\t\t\t\t\t\t\\\n\tif (cmp < 0) {\t\t\t\t\t\t\t\\\n\t    pathp[1].node = rbtn_left_get(a_type, a_field,\t\t\\\n\t      pathp->node);\t\t\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t    pathp[1].node = rbtn_right_get(a_type, a_field,\t\t\\\n\t      pathp->node);\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n    pathp->node = node;\t\t\t\t\t\t\t\\\n    /* Unwind. */\t\t\t\t\t\t\t\\\n    for (pathp--; (uintptr_t)pathp >= (uintptr_t)path; pathp--) {\t\\\n\ta_type *cnode = pathp->node;\t\t\t\t\t\\\n\tif (pathp->cmp < 0) {\t\t\t\t\t\t\\\n\t    a_type *left = pathp[1].node;\t\t\t\t\\\n\t    rbtn_left_set(a_type, a_field, cnode, left);\t\t\\\n\t    if (rbtn_red_get(a_type, a_field, left)) {\t\t\t\\\n\t\ta_type *leftleft = rbtn_left_get(a_type, a_field, left);\\\n\t\tif (leftleft != NULL && rbtn_red_get(a_type, a_field,\t\\\n\t\t  leftleft)) {\t\t\t\t\t\t\\\n\t\t    /* Fix up 4-node. */\t\t\t\t\\\n\t\t    a_type *tnode;\t\t\t\t\t\\\n\t\t    rbtn_black_set(a_type, a_field, leftleft);\t\t\\\n\t\t    rbtn_rotate_right(a_type, a_field, cnode, tnode);\t\\\n\t\t    cnode = tnode;\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t    } else {\t\t\t\t\t\t\t\\\n\t\treturn;\t\t\t\t\t\t\t\\\n\t    }\t\t\t\t\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t    a_type *right = pathp[1].node;\t\t\t\t\\\n\t    rbtn_right_set(a_type, a_field, cnode, right);\t\t\\\n\t    if (rbtn_red_get(a_type, a_field, right)) {\t\t\t\\\n\t\ta_type *left = rbtn_left_get(a_type, a_field, cnode);\t\\\n\t\tif (left != NULL && rbtn_red_get(a_type, a_field,\t\\\n\t\t  left)) {\t\t\t\t\t\t\\\n\t\t    /* Split 4-node. */\t\t\t\t\t\\\n\t\t    rbtn_black_set(a_type, a_field, left);\t\t\\\n\t\t    rbtn_black_set(a_type, a_field, right);\t\t\\\n\t\t    rbtn_red_set(a_type, a_field, cnode);\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t    /* Lean left. */\t\t\t\t\t\\\n\t\t    a_type *tnode;\t\t\t\t\t\\\n\t\t    bool tred = rbtn_red_get(a_type, a_field, cnode);\t\\\n\t\t    rbtn_rotate_left(a_type, a_field, cnode, tnode);\t\\\n\t\t    rbtn_color_set(a_type, a_field, tnode, tred);\t\\\n\t\t    rbtn_red_set(a_type, a_field, cnode);\t\t\\\n\t\t    cnode = tnode;\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t    } else {\t\t\t\t\t\t\t\\\n\t\treturn;\t\t\t\t\t\t\t\\\n\t    }\t\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tpathp->node = cnode;\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n    /* Set root, and make it black. */\t\t\t\t\t\\\n    rbtree->rbt_root = path->node;\t\t\t\t\t\\\n    rbtn_black_set(a_type, a_field, rbtree->rbt_root);\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##remove(a_rbt_type *rbtree, a_type *node) {\t\t\t\\\n    struct {\t\t\t\t\t\t\t\t\\\n\ta_type *node;\t\t\t\t\t\t\t\\\n\tint cmp;\t\t\t\t\t\t\t\\\n    } *pathp, *nodep, path[sizeof(void *) << 4];\t\t\t\\\n    /* Wind. */\t\t\t\t\t\t\t\t\\\n    nodep = NULL; /* Silence compiler warning. */\t\t\t\\\n    path->node = rbtree->rbt_root;\t\t\t\t\t\\\n    for (pathp = path; pathp->node != NULL; pathp++) {\t\t\t\\\n\tint cmp = pathp->cmp = a_cmp(node, pathp->node);\t\t\\\n\tif (cmp < 0) {\t\t\t\t\t\t\t\\\n\t    pathp[1].node = rbtn_left_get(a_type, a_field,\t\t\\\n\t      pathp->node);\t\t\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t    pathp[1].node = rbtn_right_get(a_type, a_field,\t\t\\\n\t      pathp->node);\t\t\t\t\t\t\\\n\t    if (cmp == 0) {\t\t\t\t\t\t\\\n\t        /* Find node's successor, in preparation for swap. */\t\\\n\t\tpathp->cmp = 1;\t\t\t\t\t\t\\\n\t\tnodep = pathp;\t\t\t\t\t\t\\\n\t\tfor (pathp++; pathp->node != NULL; pathp++) {\t\t\\\n\t\t    pathp->cmp = -1;\t\t\t\t\t\\\n\t\t    pathp[1].node = rbtn_left_get(a_type, a_field,\t\\\n\t\t      pathp->node);\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\t    }\t\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n    assert(nodep->node == node);\t\t\t\t\t\\\n    pathp--;\t\t\t\t\t\t\t\t\\\n    if (pathp->node != node) {\t\t\t\t\t\t\\\n\t/* Swap node with its successor. */\t\t\t\t\\\n\tbool tred = rbtn_red_get(a_type, a_field, pathp->node);\t\t\\\n\trbtn_color_set(a_type, a_field, pathp->node,\t\t\t\\\n\t  rbtn_red_get(a_type, a_field, node));\t\t\t\t\\\n\trbtn_left_set(a_type, a_field, pathp->node,\t\t\t\\\n\t  rbtn_left_get(a_type, a_field, node));\t\t\t\\\n\t/* If node's successor is its right child, the following code */\\\n\t/* will do the wrong thing for the right child pointer.       */\\\n\t/* However, it doesn't matter, because the pointer will be    */\\\n\t/* properly set when the successor is pruned.                 */\\\n\trbtn_right_set(a_type, a_field, pathp->node,\t\t\t\\\n\t  rbtn_right_get(a_type, a_field, node));\t\t\t\\\n\trbtn_color_set(a_type, a_field, node, tred);\t\t\t\\\n\t/* The pruned leaf node's child pointers are never accessed   */\\\n\t/* again, so don't bother setting them to nil.                */\\\n\tnodep->node = pathp->node;\t\t\t\t\t\\\n\tpathp->node = node;\t\t\t\t\t\t\\\n\tif (nodep == path) {\t\t\t\t\t\t\\\n\t    rbtree->rbt_root = nodep->node;\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t    if (nodep[-1].cmp < 0) {\t\t\t\t\t\\\n\t\trbtn_left_set(a_type, a_field, nodep[-1].node,\t\t\\\n\t\t  nodep->node);\t\t\t\t\t\t\\\n\t    } else {\t\t\t\t\t\t\t\\\n\t\trbtn_right_set(a_type, a_field, nodep[-1].node,\t\t\\\n\t\t  nodep->node);\t\t\t\t\t\t\\\n\t    }\t\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n    } else {\t\t\t\t\t\t\t\t\\\n\ta_type *left = rbtn_left_get(a_type, a_field, node);\t\t\\\n\tif (left != NULL) {\t\t\t\t\t\t\\\n\t    /* node has no successor, but it has a left child.        */\\\n\t    /* Splice node out, without losing the left child.        */\\\n\t    assert(!rbtn_red_get(a_type, a_field, node));\t\t\\\n\t    assert(rbtn_red_get(a_type, a_field, left));\t\t\\\n\t    rbtn_black_set(a_type, a_field, left);\t\t\t\\\n\t    if (pathp == path) {\t\t\t\t\t\\\n\t\trbtree->rbt_root = left;\t\t\t\t\\\n\t    } else {\t\t\t\t\t\t\t\\\n\t\tif (pathp[-1].cmp < 0) {\t\t\t\t\\\n\t\t    rbtn_left_set(a_type, a_field, pathp[-1].node,\t\\\n\t\t      left);\t\t\t\t\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t    rbtn_right_set(a_type, a_field, pathp[-1].node,\t\\\n\t\t      left);\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t    }\t\t\t\t\t\t\t\t\\\n\t    return;\t\t\t\t\t\t\t\\\n\t} else if (pathp == path) {\t\t\t\t\t\\\n\t    /* The tree only contained one node. */\t\t\t\\\n\t    rbtree->rbt_root = NULL;\t\t\t\t\t\\\n\t    return;\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n    if (rbtn_red_get(a_type, a_field, pathp->node)) {\t\t\t\\\n\t/* Prune red node, which requires no fixup. */\t\t\t\\\n\tassert(pathp[-1].cmp < 0);\t\t\t\t\t\\\n\trbtn_left_set(a_type, a_field, pathp[-1].node, NULL);\t\t\\\n\treturn;\t\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n    /* The node to be pruned is black, so unwind until balance is     */\\\n    /* restored.                                                      */\\\n    pathp->node = NULL;\t\t\t\t\t\t\t\\\n    for (pathp--; (uintptr_t)pathp >= (uintptr_t)path; pathp--) {\t\\\n\tassert(pathp->cmp != 0);\t\t\t\t\t\\\n\tif (pathp->cmp < 0) {\t\t\t\t\t\t\\\n\t    rbtn_left_set(a_type, a_field, pathp->node,\t\t\t\\\n\t      pathp[1].node);\t\t\t\t\t\t\\\n\t    if (rbtn_red_get(a_type, a_field, pathp->node)) {\t\t\\\n\t\ta_type *right = rbtn_right_get(a_type, a_field,\t\t\\\n\t\t  pathp->node);\t\t\t\t\t\t\\\n\t\ta_type *rightleft = rbtn_left_get(a_type, a_field,\t\\\n\t\t  right);\t\t\t\t\t\t\\\n\t\ta_type *tnode;\t\t\t\t\t\t\\\n\t\tif (rightleft != NULL && rbtn_red_get(a_type, a_field,\t\\\n\t\t  rightleft)) {\t\t\t\t\t\t\\\n\t\t    /* In the following diagrams, ||, //, and \\\\      */\\\n\t\t    /* indicate the path to the removed node.         */\\\n\t\t    /*                                                */\\\n\t\t    /*      ||                                        */\\\n\t\t    /*    pathp(r)                                    */\\\n\t\t    /*  //        \\                                   */\\\n\t\t    /* (b)        (b)                                 */\\\n\t\t    /*           /                                    */\\\n\t\t    /*          (r)                                   */\\\n\t\t    /*                                                */\\\n\t\t    rbtn_black_set(a_type, a_field, pathp->node);\t\\\n\t\t    rbtn_rotate_right(a_type, a_field, right, tnode);\t\\\n\t\t    rbtn_right_set(a_type, a_field, pathp->node, tnode);\\\n\t\t    rbtn_rotate_left(a_type, a_field, pathp->node,\t\\\n\t\t      tnode);\t\t\t\t\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t    /*      ||                                        */\\\n\t\t    /*    pathp(r)                                    */\\\n\t\t    /*  //        \\                                   */\\\n\t\t    /* (b)        (b)                                 */\\\n\t\t    /*           /                                    */\\\n\t\t    /*          (b)                                   */\\\n\t\t    /*                                                */\\\n\t\t    rbtn_rotate_left(a_type, a_field, pathp->node,\t\\\n\t\t      tnode);\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\t/* Balance restored, but rotation modified subtree    */\\\n\t\t/* root.                                              */\\\n\t\tassert((uintptr_t)pathp > (uintptr_t)path);\t\t\\\n\t\tif (pathp[-1].cmp < 0) {\t\t\t\t\\\n\t\t    rbtn_left_set(a_type, a_field, pathp[-1].node,\t\\\n\t\t      tnode);\t\t\t\t\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t    rbtn_right_set(a_type, a_field, pathp[-1].node,\t\\\n\t\t      tnode);\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\treturn;\t\t\t\t\t\t\t\\\n\t    } else {\t\t\t\t\t\t\t\\\n\t\ta_type *right = rbtn_right_get(a_type, a_field,\t\t\\\n\t\t  pathp->node);\t\t\t\t\t\t\\\n\t\ta_type *rightleft = rbtn_left_get(a_type, a_field,\t\\\n\t\t  right);\t\t\t\t\t\t\\\n\t\tif (rightleft != NULL && rbtn_red_get(a_type, a_field,\t\\\n\t\t  rightleft)) {\t\t\t\t\t\t\\\n\t\t    /*      ||                                        */\\\n\t\t    /*    pathp(b)                                    */\\\n\t\t    /*  //        \\                                   */\\\n\t\t    /* (b)        (b)                                 */\\\n\t\t    /*           /                                    */\\\n\t\t    /*          (r)                                   */\\\n\t\t    a_type *tnode;\t\t\t\t\t\\\n\t\t    rbtn_black_set(a_type, a_field, rightleft);\t\t\\\n\t\t    rbtn_rotate_right(a_type, a_field, right, tnode);\t\\\n\t\t    rbtn_right_set(a_type, a_field, pathp->node, tnode);\\\n\t\t    rbtn_rotate_left(a_type, a_field, pathp->node,\t\\\n\t\t      tnode);\t\t\t\t\t\t\\\n\t\t    /* Balance restored, but rotation modified        */\\\n\t\t    /* subtree root, which may actually be the tree   */\\\n\t\t    /* root.                                          */\\\n\t\t    if (pathp == path) {\t\t\t\t\\\n\t\t\t/* Set root. */\t\t\t\t\t\\\n\t\t\trbtree->rbt_root = tnode;\t\t\t\\\n\t\t    } else {\t\t\t\t\t\t\\\n\t\t\tif (pathp[-1].cmp < 0) {\t\t\t\\\n\t\t\t    rbtn_left_set(a_type, a_field,\t\t\\\n\t\t\t      pathp[-1].node, tnode);\t\t\t\\\n\t\t\t} else {\t\t\t\t\t\\\n\t\t\t    rbtn_right_set(a_type, a_field,\t\t\\\n\t\t\t      pathp[-1].node, tnode);\t\t\t\\\n\t\t\t}\t\t\t\t\t\t\\\n\t\t    }\t\t\t\t\t\t\t\\\n\t\t    return;\t\t\t\t\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t    /*      ||                                        */\\\n\t\t    /*    pathp(b)                                    */\\\n\t\t    /*  //        \\                                   */\\\n\t\t    /* (b)        (b)                                 */\\\n\t\t    /*           /                                    */\\\n\t\t    /*          (b)                                   */\\\n\t\t    a_type *tnode;\t\t\t\t\t\\\n\t\t    rbtn_red_set(a_type, a_field, pathp->node);\t\t\\\n\t\t    rbtn_rotate_left(a_type, a_field, pathp->node,\t\\\n\t\t      tnode);\t\t\t\t\t\t\\\n\t\t    pathp->node = tnode;\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t    }\t\t\t\t\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t    a_type *left;\t\t\t\t\t\t\\\n\t    rbtn_right_set(a_type, a_field, pathp->node,\t\t\\\n\t      pathp[1].node);\t\t\t\t\t\t\\\n\t    left = rbtn_left_get(a_type, a_field, pathp->node);\t\t\\\n\t    if (rbtn_red_get(a_type, a_field, left)) {\t\t\t\\\n\t\ta_type *tnode;\t\t\t\t\t\t\\\n\t\ta_type *leftright = rbtn_right_get(a_type, a_field,\t\\\n\t\t  left);\t\t\t\t\t\t\\\n\t\ta_type *leftrightleft = rbtn_left_get(a_type, a_field,\t\\\n\t\t  leftright);\t\t\t\t\t\t\\\n\t\tif (leftrightleft != NULL && rbtn_red_get(a_type,\t\\\n\t\t  a_field, leftrightleft)) {\t\t\t\t\\\n\t\t    /*      ||                                        */\\\n\t\t    /*    pathp(b)                                    */\\\n\t\t    /*   /        \\\\                                  */\\\n\t\t    /* (r)        (b)                                 */\\\n\t\t    /*   \\                                            */\\\n\t\t    /*   (b)                                          */\\\n\t\t    /*   /                                            */\\\n\t\t    /* (r)                                            */\\\n\t\t    a_type *unode;\t\t\t\t\t\\\n\t\t    rbtn_black_set(a_type, a_field, leftrightleft);\t\\\n\t\t    rbtn_rotate_right(a_type, a_field, pathp->node,\t\\\n\t\t      unode);\t\t\t\t\t\t\\\n\t\t    rbtn_rotate_right(a_type, a_field, pathp->node,\t\\\n\t\t      tnode);\t\t\t\t\t\t\\\n\t\t    rbtn_right_set(a_type, a_field, unode, tnode);\t\\\n\t\t    rbtn_rotate_left(a_type, a_field, unode, tnode);\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t    /*      ||                                        */\\\n\t\t    /*    pathp(b)                                    */\\\n\t\t    /*   /        \\\\                                  */\\\n\t\t    /* (r)        (b)                                 */\\\n\t\t    /*   \\                                            */\\\n\t\t    /*   (b)                                          */\\\n\t\t    /*   /                                            */\\\n\t\t    /* (b)                                            */\\\n\t\t    assert(leftright != NULL);\t\t\t\t\\\n\t\t    rbtn_red_set(a_type, a_field, leftright);\t\t\\\n\t\t    rbtn_rotate_right(a_type, a_field, pathp->node,\t\\\n\t\t      tnode);\t\t\t\t\t\t\\\n\t\t    rbtn_black_set(a_type, a_field, tnode);\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\t/* Balance restored, but rotation modified subtree    */\\\n\t\t/* root, which may actually be the tree root.         */\\\n\t\tif (pathp == path) {\t\t\t\t\t\\\n\t\t    /* Set root. */\t\t\t\t\t\\\n\t\t    rbtree->rbt_root = tnode;\t\t\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t    if (pathp[-1].cmp < 0) {\t\t\t\t\\\n\t\t\trbtn_left_set(a_type, a_field, pathp[-1].node,\t\\\n\t\t\t  tnode);\t\t\t\t\t\\\n\t\t    } else {\t\t\t\t\t\t\\\n\t\t\trbtn_right_set(a_type, a_field, pathp[-1].node,\t\\\n\t\t\t  tnode);\t\t\t\t\t\\\n\t\t    }\t\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\treturn;\t\t\t\t\t\t\t\\\n\t    } else if (rbtn_red_get(a_type, a_field, pathp->node)) {\t\\\n\t\ta_type *leftleft = rbtn_left_get(a_type, a_field, left);\\\n\t\tif (leftleft != NULL && rbtn_red_get(a_type, a_field,\t\\\n\t\t  leftleft)) {\t\t\t\t\t\t\\\n\t\t    /*        ||                                      */\\\n\t\t    /*      pathp(r)                                  */\\\n\t\t    /*     /        \\\\                                */\\\n\t\t    /*   (b)        (b)                               */\\\n\t\t    /*   /                                            */\\\n\t\t    /* (r)                                            */\\\n\t\t    a_type *tnode;\t\t\t\t\t\\\n\t\t    rbtn_black_set(a_type, a_field, pathp->node);\t\\\n\t\t    rbtn_red_set(a_type, a_field, left);\t\t\\\n\t\t    rbtn_black_set(a_type, a_field, leftleft);\t\t\\\n\t\t    rbtn_rotate_right(a_type, a_field, pathp->node,\t\\\n\t\t      tnode);\t\t\t\t\t\t\\\n\t\t    /* Balance restored, but rotation modified        */\\\n\t\t    /* subtree root.                                  */\\\n\t\t    assert((uintptr_t)pathp > (uintptr_t)path);\t\t\\\n\t\t    if (pathp[-1].cmp < 0) {\t\t\t\t\\\n\t\t\trbtn_left_set(a_type, a_field, pathp[-1].node,\t\\\n\t\t\t  tnode);\t\t\t\t\t\\\n\t\t    } else {\t\t\t\t\t\t\\\n\t\t\trbtn_right_set(a_type, a_field, pathp[-1].node,\t\\\n\t\t\t  tnode);\t\t\t\t\t\\\n\t\t    }\t\t\t\t\t\t\t\\\n\t\t    return;\t\t\t\t\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t    /*        ||                                      */\\\n\t\t    /*      pathp(r)                                  */\\\n\t\t    /*     /        \\\\                                */\\\n\t\t    /*   (b)        (b)                               */\\\n\t\t    /*   /                                            */\\\n\t\t    /* (b)                                            */\\\n\t\t    rbtn_red_set(a_type, a_field, left);\t\t\\\n\t\t    rbtn_black_set(a_type, a_field, pathp->node);\t\\\n\t\t    /* Balance restored. */\t\t\t\t\\\n\t\t    return;\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t    } else {\t\t\t\t\t\t\t\\\n\t\ta_type *leftleft = rbtn_left_get(a_type, a_field, left);\\\n\t\tif (leftleft != NULL && rbtn_red_get(a_type, a_field,\t\\\n\t\t  leftleft)) {\t\t\t\t\t\t\\\n\t\t    /*               ||                               */\\\n\t\t    /*             pathp(b)                           */\\\n\t\t    /*            /        \\\\                         */\\\n\t\t    /*          (b)        (b)                        */\\\n\t\t    /*          /                                     */\\\n\t\t    /*        (r)                                     */\\\n\t\t    a_type *tnode;\t\t\t\t\t\\\n\t\t    rbtn_black_set(a_type, a_field, leftleft);\t\t\\\n\t\t    rbtn_rotate_right(a_type, a_field, pathp->node,\t\\\n\t\t      tnode);\t\t\t\t\t\t\\\n\t\t    /* Balance restored, but rotation modified        */\\\n\t\t    /* subtree root, which may actually be the tree   */\\\n\t\t    /* root.                                          */\\\n\t\t    if (pathp == path) {\t\t\t\t\\\n\t\t\t/* Set root. */\t\t\t\t\t\\\n\t\t\trbtree->rbt_root = tnode;\t\t\t\\\n\t\t    } else {\t\t\t\t\t\t\\\n\t\t\tif (pathp[-1].cmp < 0) {\t\t\t\\\n\t\t\t    rbtn_left_set(a_type, a_field,\t\t\\\n\t\t\t      pathp[-1].node, tnode);\t\t\t\\\n\t\t\t} else {\t\t\t\t\t\\\n\t\t\t    rbtn_right_set(a_type, a_field,\t\t\\\n\t\t\t      pathp[-1].node, tnode);\t\t\t\\\n\t\t\t}\t\t\t\t\t\t\\\n\t\t    }\t\t\t\t\t\t\t\\\n\t\t    return;\t\t\t\t\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t    /*               ||                               */\\\n\t\t    /*             pathp(b)                           */\\\n\t\t    /*            /        \\\\                         */\\\n\t\t    /*          (b)        (b)                        */\\\n\t\t    /*          /                                     */\\\n\t\t    /*        (b)                                     */\\\n\t\t    rbtn_red_set(a_type, a_field, left);\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t    }\t\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n    /* Set root. */\t\t\t\t\t\t\t\\\n    rbtree->rbt_root = path->node;\t\t\t\t\t\\\n    assert(!rbtn_red_get(a_type, a_field, rbtree->rbt_root));\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##iter_recurse(a_rbt_type *rbtree, a_type *node,\t\t\\\n  a_type *(*cb)(a_rbt_type *, a_type *, void *), void *arg) {\t\t\\\n    if (node == NULL) {\t\t\t\t\t\t\t\\\n\treturn NULL;\t\t\t\t\t\t\t\\\n    } else {\t\t\t\t\t\t\t\t\\\n\ta_type *ret;\t\t\t\t\t\t\t\\\n\tif ((ret = a_prefix##iter_recurse(rbtree, rbtn_left_get(a_type,\t\\\n\t  a_field, node), cb, arg)) != NULL || (ret = cb(rbtree, node,\t\\\n\t  arg)) != NULL) {\t\t\t\t\t\t\\\n\t    return ret;\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\treturn a_prefix##iter_recurse(rbtree, rbtn_right_get(a_type,\t\\\n\t  a_field, node), cb, arg);\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##iter_start(a_rbt_type *rbtree, a_type *start, a_type *node,\t\\\n  a_type *(*cb)(a_rbt_type *, a_type *, void *), void *arg) {\t\t\\\n    int cmp = a_cmp(start, node);\t\t\t\t\t\\\n    if (cmp < 0) {\t\t\t\t\t\t\t\\\n\ta_type *ret;\t\t\t\t\t\t\t\\\n\tif ((ret = a_prefix##iter_start(rbtree, start,\t\t\t\\\n\t  rbtn_left_get(a_type, a_field, node), cb, arg)) != NULL ||\t\\\n\t  (ret = cb(rbtree, node, arg)) != NULL) {\t\t\t\\\n\t    return ret;\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\treturn a_prefix##iter_recurse(rbtree, rbtn_right_get(a_type,\t\\\n\t  a_field, node), cb, arg);\t\t\t\t\t\\\n    } else if (cmp > 0) {\t\t\t\t\t\t\\\n\treturn a_prefix##iter_start(rbtree, start,\t\t\t\\\n\t  rbtn_right_get(a_type, a_field, node), cb, arg);\t\t\\\n    } else {\t\t\t\t\t\t\t\t\\\n\ta_type *ret;\t\t\t\t\t\t\t\\\n\tif ((ret = cb(rbtree, node, arg)) != NULL) {\t\t\t\\\n\t    return ret;\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\treturn a_prefix##iter_recurse(rbtree, rbtn_right_get(a_type,\t\\\n\t  a_field, node), cb, arg);\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##iter(a_rbt_type *rbtree, a_type *start, a_type *(*cb)(\t\\\n  a_rbt_type *, a_type *, void *), void *arg) {\t\t\t\t\\\n    a_type *ret;\t\t\t\t\t\t\t\\\n    if (start != NULL) {\t\t\t\t\t\t\\\n\tret = a_prefix##iter_start(rbtree, start, rbtree->rbt_root,\t\\\n\t  cb, arg);\t\t\t\t\t\t\t\\\n    } else {\t\t\t\t\t\t\t\t\\\n\tret = a_prefix##iter_recurse(rbtree, rbtree->rbt_root, cb, arg);\\\n    }\t\t\t\t\t\t\t\t\t\\\n    return ret;\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##reverse_iter_recurse(a_rbt_type *rbtree, a_type *node,\t\\\n  a_type *(*cb)(a_rbt_type *, a_type *, void *), void *arg) {\t\t\\\n    if (node == NULL) {\t\t\t\t\t\t\t\\\n\treturn NULL;\t\t\t\t\t\t\t\\\n    } else {\t\t\t\t\t\t\t\t\\\n\ta_type *ret;\t\t\t\t\t\t\t\\\n\tif ((ret = a_prefix##reverse_iter_recurse(rbtree,\t\t\\\n\t  rbtn_right_get(a_type, a_field, node), cb, arg)) != NULL ||\t\\\n\t  (ret = cb(rbtree, node, arg)) != NULL) {\t\t\t\\\n\t    return ret;\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\treturn a_prefix##reverse_iter_recurse(rbtree,\t\t\t\\\n\t  rbtn_left_get(a_type, a_field, node), cb, arg);\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##reverse_iter_start(a_rbt_type *rbtree, a_type *start,\t\t\\\n  a_type *node, a_type *(*cb)(a_rbt_type *, a_type *, void *),\t\t\\\n  void *arg) {\t\t\t\t\t\t\t\t\\\n    int cmp = a_cmp(start, node);\t\t\t\t\t\\\n    if (cmp > 0) {\t\t\t\t\t\t\t\\\n\ta_type *ret;\t\t\t\t\t\t\t\\\n\tif ((ret = a_prefix##reverse_iter_start(rbtree, start,\t\t\\\n\t  rbtn_right_get(a_type, a_field, node), cb, arg)) != NULL ||\t\\\n\t  (ret = cb(rbtree, node, arg)) != NULL) {\t\t\t\\\n\t    return ret;\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\treturn a_prefix##reverse_iter_recurse(rbtree,\t\t\t\\\n\t  rbtn_left_get(a_type, a_field, node), cb, arg);\t\t\\\n    } else if (cmp < 0) {\t\t\t\t\t\t\\\n\treturn a_prefix##reverse_iter_start(rbtree, start,\t\t\\\n\t  rbtn_left_get(a_type, a_field, node), cb, arg);\t\t\\\n    } else {\t\t\t\t\t\t\t\t\\\n\ta_type *ret;\t\t\t\t\t\t\t\\\n\tif ((ret = cb(rbtree, node, arg)) != NULL) {\t\t\t\\\n\t    return ret;\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\treturn a_prefix##reverse_iter_recurse(rbtree,\t\t\t\\\n\t  rbtn_left_get(a_type, a_field, node), cb, arg);\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_type *\t\t\t\t\t\t\t\t\\\na_prefix##reverse_iter(a_rbt_type *rbtree, a_type *start,\t\t\\\n  a_type *(*cb)(a_rbt_type *, a_type *, void *), void *arg) {\t\t\\\n    a_type *ret;\t\t\t\t\t\t\t\\\n    if (start != NULL) {\t\t\t\t\t\t\\\n\tret = a_prefix##reverse_iter_start(rbtree, start,\t\t\\\n\t  rbtree->rbt_root, cb, arg);\t\t\t\t\t\\\n    } else {\t\t\t\t\t\t\t\t\\\n\tret = a_prefix##reverse_iter_recurse(rbtree, rbtree->rbt_root,\t\\\n\t  cb, arg);\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n    return ret;\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##destroy_recurse(a_rbt_type *rbtree, a_type *node, void (*cb)(\t\\\n  a_type *, void *), void *arg) {\t\t\t\t\t\\\n    if (node == NULL) {\t\t\t\t\t\t\t\\\n\treturn;\t\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n    a_prefix##destroy_recurse(rbtree, rbtn_left_get(a_type, a_field,\t\\\n      node), cb, arg);\t\t\t\t\t\t\t\\\n    rbtn_left_set(a_type, a_field, (node), NULL);\t\t\t\\\n    a_prefix##destroy_recurse(rbtree, rbtn_right_get(a_type, a_field,\t\\\n      node), cb, arg);\t\t\t\t\t\t\t\\\n    rbtn_right_set(a_type, a_field, (node), NULL);\t\t\t\\\n    if (cb) {\t\t\t\t\t\t\t\t\\\n\tcb(node, arg);\t\t\t\t\t\t\t\\\n    }\t\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##destroy(a_rbt_type *rbtree, void (*cb)(a_type *, void *),\t\\\n  void *arg) {\t\t\t\t\t\t\t\t\\\n    a_prefix##destroy_recurse(rbtree, rbtree->rbt_root, cb, arg);\t\\\n    rbtree->rbt_root = NULL;\t\t\t\t\t\t\\\n}\n\n#endif /* RB_H_ */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/rtree.h",
    "content": "#ifndef JEMALLOC_INTERNAL_RTREE_H\n#define JEMALLOC_INTERNAL_RTREE_H\n\n#include \"jemalloc/internal/atomic.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/rtree_tsd.h\"\n#include \"jemalloc/internal/size_classes.h\"\n#include \"jemalloc/internal/tsd.h\"\n\n/*\n * This radix tree implementation is tailored to the singular purpose of\n * associating metadata with extents that are currently owned by jemalloc.\n *\n *******************************************************************************\n */\n\n/* Number of high insignificant bits. */\n#define RTREE_NHIB ((1U << (LG_SIZEOF_PTR+3)) - LG_VADDR)\n/* Number of low insigificant bits. */\n#define RTREE_NLIB LG_PAGE\n/* Number of significant bits. */\n#define RTREE_NSB (LG_VADDR - RTREE_NLIB)\n/* Number of levels in radix tree. */\n#if RTREE_NSB <= 10\n#  define RTREE_HEIGHT 1\n#elif RTREE_NSB <= 36\n#  define RTREE_HEIGHT 2\n#elif RTREE_NSB <= 52\n#  define RTREE_HEIGHT 3\n#else\n#  error Unsupported number of significant virtual address bits\n#endif\n/* Use compact leaf representation if virtual address encoding allows. */\n#if RTREE_NHIB >= LG_CEIL_NSIZES\n#  define RTREE_LEAF_COMPACT\n#endif\n\n/* Needed for initialization only. */\n#define RTREE_LEAFKEY_INVALID ((uintptr_t)1)\n\ntypedef struct rtree_node_elm_s rtree_node_elm_t;\nstruct rtree_node_elm_s {\n\tatomic_p_t\tchild; /* (rtree_{node,leaf}_elm_t *) */\n};\n\nstruct rtree_leaf_elm_s {\n#ifdef RTREE_LEAF_COMPACT\n\t/*\n\t * Single pointer-width field containing all three leaf element fields.\n\t * For example, on a 64-bit x64 system with 48 significant virtual\n\t * memory address bits, the index, extent, and slab fields are packed as\n\t * such:\n\t *\n\t * x: index\n\t * e: extent\n\t * b: slab\n\t *\n\t *   00000000 xxxxxxxx eeeeeeee [...] eeeeeeee eeee000b\n\t */\n\tatomic_p_t\tle_bits;\n#else\n\tatomic_p_t\tle_extent; /* (extent_t *) */\n\tatomic_u_t\tle_szind; /* (szind_t) */\n\tatomic_b_t\tle_slab; /* (bool) */\n#endif\n};\n\ntypedef struct rtree_level_s rtree_level_t;\nstruct rtree_level_s {\n\t/* Number of key bits distinguished by this level. */\n\tunsigned\t\tbits;\n\t/*\n\t * Cumulative number of key bits distinguished by traversing to\n\t * corresponding tree level.\n\t */\n\tunsigned\t\tcumbits;\n};\n\ntypedef struct rtree_s rtree_t;\nstruct rtree_s {\n\tmalloc_mutex_t\t\tinit_lock;\n\t/* Number of elements based on rtree_levels[0].bits. */\n#if RTREE_HEIGHT > 1\n\trtree_node_elm_t\troot[1U << (RTREE_NSB/RTREE_HEIGHT)];\n#else\n\trtree_leaf_elm_t\troot[1U << (RTREE_NSB/RTREE_HEIGHT)];\n#endif\n};\n\n/*\n * Split the bits into one to three partitions depending on number of\n * significant bits.  It the number of bits does not divide evenly into the\n * number of levels, place one remainder bit per level starting at the leaf\n * level.\n */\nstatic const rtree_level_t rtree_levels[] = {\n#if RTREE_HEIGHT == 1\n\t{RTREE_NSB, RTREE_NHIB + RTREE_NSB}\n#elif RTREE_HEIGHT == 2\n\t{RTREE_NSB/2, RTREE_NHIB + RTREE_NSB/2},\n\t{RTREE_NSB/2 + RTREE_NSB%2, RTREE_NHIB + RTREE_NSB}\n#elif RTREE_HEIGHT == 3\n\t{RTREE_NSB/3, RTREE_NHIB + RTREE_NSB/3},\n\t{RTREE_NSB/3 + RTREE_NSB%3/2,\n\t    RTREE_NHIB + RTREE_NSB/3*2 + RTREE_NSB%3/2},\n\t{RTREE_NSB/3 + RTREE_NSB%3 - RTREE_NSB%3/2, RTREE_NHIB + RTREE_NSB}\n#else\n#  error Unsupported rtree height\n#endif\n};\n\nbool rtree_new(rtree_t *rtree, bool zeroed);\n\ntypedef rtree_node_elm_t *(rtree_node_alloc_t)(tsdn_t *, rtree_t *, size_t);\nextern rtree_node_alloc_t *JET_MUTABLE rtree_node_alloc;\n\ntypedef rtree_leaf_elm_t *(rtree_leaf_alloc_t)(tsdn_t *, rtree_t *, size_t);\nextern rtree_leaf_alloc_t *JET_MUTABLE rtree_leaf_alloc;\n\ntypedef void (rtree_node_dalloc_t)(tsdn_t *, rtree_t *, rtree_node_elm_t *);\nextern rtree_node_dalloc_t *JET_MUTABLE rtree_node_dalloc;\n\ntypedef void (rtree_leaf_dalloc_t)(tsdn_t *, rtree_t *, rtree_leaf_elm_t *);\nextern rtree_leaf_dalloc_t *JET_MUTABLE rtree_leaf_dalloc;\n#ifdef JEMALLOC_JET\nvoid rtree_delete(tsdn_t *tsdn, rtree_t *rtree);\n#endif\nrtree_leaf_elm_t *rtree_leaf_elm_lookup_hard(tsdn_t *tsdn, rtree_t *rtree,\n    rtree_ctx_t *rtree_ctx, uintptr_t key, bool dependent, bool init_missing);\n\nJEMALLOC_ALWAYS_INLINE uintptr_t\nrtree_leafkey(uintptr_t key) {\n\tunsigned ptrbits = ZU(1) << (LG_SIZEOF_PTR+3);\n\tunsigned cumbits = (rtree_levels[RTREE_HEIGHT-1].cumbits -\n\t    rtree_levels[RTREE_HEIGHT-1].bits);\n\tunsigned maskbits = ptrbits - cumbits;\n\tuintptr_t mask = ~((ZU(1) << maskbits) - 1);\n\treturn (key & mask);\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\nrtree_cache_direct_map(uintptr_t key) {\n\tunsigned ptrbits = ZU(1) << (LG_SIZEOF_PTR+3);\n\tunsigned cumbits = (rtree_levels[RTREE_HEIGHT-1].cumbits -\n\t    rtree_levels[RTREE_HEIGHT-1].bits);\n\tunsigned maskbits = ptrbits - cumbits;\n\treturn (size_t)((key >> maskbits) & (RTREE_CTX_NCACHE - 1));\n}\n\nJEMALLOC_ALWAYS_INLINE uintptr_t\nrtree_subkey(uintptr_t key, unsigned level) {\n\tunsigned ptrbits = ZU(1) << (LG_SIZEOF_PTR+3);\n\tunsigned cumbits = rtree_levels[level].cumbits;\n\tunsigned shiftbits = ptrbits - cumbits;\n\tunsigned maskbits = rtree_levels[level].bits;\n\tuintptr_t mask = (ZU(1) << maskbits) - 1;\n\treturn ((key >> shiftbits) & mask);\n}\n\n/*\n * Atomic getters.\n *\n * dependent: Reading a value on behalf of a pointer to a valid allocation\n *            is guaranteed to be a clean read even without synchronization,\n *            because the rtree update became visible in memory before the\n *            pointer came into existence.\n * !dependent: An arbitrary read, e.g. on behalf of ivsalloc(), may not be\n *             dependent on a previous rtree write, which means a stale read\n *             could result if synchronization were omitted here.\n */\n#  ifdef RTREE_LEAF_COMPACT\nJEMALLOC_ALWAYS_INLINE uintptr_t\nrtree_leaf_elm_bits_read(tsdn_t *tsdn, rtree_t *rtree, rtree_leaf_elm_t *elm,\n    bool dependent) {\n\treturn (uintptr_t)atomic_load_p(&elm->le_bits, dependent\n\t    ? ATOMIC_RELAXED : ATOMIC_ACQUIRE);\n}\n\nJEMALLOC_ALWAYS_INLINE extent_t *\nrtree_leaf_elm_bits_extent_get(uintptr_t bits) {\n\t/* Restore sign-extended high bits, mask slab bit. */\n\treturn (extent_t *)((uintptr_t)((intptr_t)(bits << RTREE_NHIB) >>\n\t    RTREE_NHIB) & ~((uintptr_t)0x1));\n}\n\nJEMALLOC_ALWAYS_INLINE szind_t\nrtree_leaf_elm_bits_szind_get(uintptr_t bits) {\n\treturn (szind_t)(bits >> LG_VADDR);\n}\n\nJEMALLOC_ALWAYS_INLINE bool\nrtree_leaf_elm_bits_slab_get(uintptr_t bits) {\n\treturn (bool)(bits & (uintptr_t)0x1);\n}\n\n#  endif\n\nJEMALLOC_ALWAYS_INLINE extent_t *\nrtree_leaf_elm_extent_read(tsdn_t *tsdn, rtree_t *rtree, rtree_leaf_elm_t *elm,\n    bool dependent) {\n#ifdef RTREE_LEAF_COMPACT\n\tuintptr_t bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm, dependent);\n\treturn rtree_leaf_elm_bits_extent_get(bits);\n#else\n\textent_t *extent = (extent_t *)atomic_load_p(&elm->le_extent, dependent\n\t    ? ATOMIC_RELAXED : ATOMIC_ACQUIRE);\n\treturn extent;\n#endif\n}\n\nJEMALLOC_ALWAYS_INLINE szind_t\nrtree_leaf_elm_szind_read(tsdn_t *tsdn, rtree_t *rtree, rtree_leaf_elm_t *elm,\n    bool dependent) {\n#ifdef RTREE_LEAF_COMPACT\n\tuintptr_t bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm, dependent);\n\treturn rtree_leaf_elm_bits_szind_get(bits);\n#else\n\treturn (szind_t)atomic_load_u(&elm->le_szind, dependent ? ATOMIC_RELAXED\n\t    : ATOMIC_ACQUIRE);\n#endif\n}\n\nJEMALLOC_ALWAYS_INLINE bool\nrtree_leaf_elm_slab_read(tsdn_t *tsdn, rtree_t *rtree, rtree_leaf_elm_t *elm,\n    bool dependent) {\n#ifdef RTREE_LEAF_COMPACT\n\tuintptr_t bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm, dependent);\n\treturn rtree_leaf_elm_bits_slab_get(bits);\n#else\n\treturn atomic_load_b(&elm->le_slab, dependent ? ATOMIC_RELAXED :\n\t    ATOMIC_ACQUIRE);\n#endif\n}\n\nstatic inline void\nrtree_leaf_elm_extent_write(tsdn_t *tsdn, rtree_t *rtree, rtree_leaf_elm_t *elm,\n    extent_t *extent) {\n#ifdef RTREE_LEAF_COMPACT\n\tuintptr_t old_bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm, true);\n\tuintptr_t bits = ((uintptr_t)rtree_leaf_elm_bits_szind_get(old_bits) <<\n\t    LG_VADDR) | ((uintptr_t)extent & (((uintptr_t)0x1 << LG_VADDR) - 1))\n\t    | ((uintptr_t)rtree_leaf_elm_bits_slab_get(old_bits));\n\tatomic_store_p(&elm->le_bits, (void *)bits, ATOMIC_RELEASE);\n#else\n\tatomic_store_p(&elm->le_extent, extent, ATOMIC_RELEASE);\n#endif\n}\n\nstatic inline void\nrtree_leaf_elm_szind_write(tsdn_t *tsdn, rtree_t *rtree, rtree_leaf_elm_t *elm,\n    szind_t szind) {\n\tassert(szind <= NSIZES);\n\n#ifdef RTREE_LEAF_COMPACT\n\tuintptr_t old_bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm,\n\t    true);\n\tuintptr_t bits = ((uintptr_t)szind << LG_VADDR) |\n\t    ((uintptr_t)rtree_leaf_elm_bits_extent_get(old_bits) &\n\t    (((uintptr_t)0x1 << LG_VADDR) - 1)) |\n\t    ((uintptr_t)rtree_leaf_elm_bits_slab_get(old_bits));\n\tatomic_store_p(&elm->le_bits, (void *)bits, ATOMIC_RELEASE);\n#else\n\tatomic_store_u(&elm->le_szind, szind, ATOMIC_RELEASE);\n#endif\n}\n\nstatic inline void\nrtree_leaf_elm_slab_write(tsdn_t *tsdn, rtree_t *rtree, rtree_leaf_elm_t *elm,\n     bool slab) {\n#ifdef RTREE_LEAF_COMPACT\n\tuintptr_t old_bits = rtree_leaf_elm_bits_read(tsdn, rtree, elm,\n\t    true);\n\tuintptr_t bits = ((uintptr_t)rtree_leaf_elm_bits_szind_get(old_bits) <<\n\t    LG_VADDR) | ((uintptr_t)rtree_leaf_elm_bits_extent_get(old_bits) &\n\t    (((uintptr_t)0x1 << LG_VADDR) - 1)) | ((uintptr_t)slab);\n\tatomic_store_p(&elm->le_bits, (void *)bits, ATOMIC_RELEASE);\n#else\n\tatomic_store_b(&elm->le_slab, slab, ATOMIC_RELEASE);\n#endif\n}\n\nstatic inline void\nrtree_leaf_elm_write(tsdn_t *tsdn, rtree_t *rtree, rtree_leaf_elm_t *elm,\n    extent_t *extent, szind_t szind, bool slab) {\n#ifdef RTREE_LEAF_COMPACT\n\tuintptr_t bits = ((uintptr_t)szind << LG_VADDR) |\n\t    ((uintptr_t)extent & (((uintptr_t)0x1 << LG_VADDR) - 1)) |\n\t    ((uintptr_t)slab);\n\tatomic_store_p(&elm->le_bits, (void *)bits, ATOMIC_RELEASE);\n#else\n\trtree_leaf_elm_slab_write(tsdn, rtree, elm, slab);\n\trtree_leaf_elm_szind_write(tsdn, rtree, elm, szind);\n\t/*\n\t * Write extent last, since the element is atomically considered valid\n\t * as soon as the extent field is non-NULL.\n\t */\n\trtree_leaf_elm_extent_write(tsdn, rtree, elm, extent);\n#endif\n}\n\nstatic inline void\nrtree_leaf_elm_szind_slab_update(tsdn_t *tsdn, rtree_t *rtree,\n    rtree_leaf_elm_t *elm, szind_t szind, bool slab) {\n\tassert(!slab || szind < NBINS);\n\n\t/*\n\t * The caller implicitly assures that it is the only writer to the szind\n\t * and slab fields, and that the extent field cannot currently change.\n\t */\n\trtree_leaf_elm_slab_write(tsdn, rtree, elm, slab);\n\trtree_leaf_elm_szind_write(tsdn, rtree, elm, szind);\n}\n\nJEMALLOC_ALWAYS_INLINE rtree_leaf_elm_t *\nrtree_leaf_elm_lookup(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,\n    uintptr_t key, bool dependent, bool init_missing) {\n\tassert(key != 0);\n\tassert(!dependent || !init_missing);\n\n\tsize_t slot = rtree_cache_direct_map(key);\n\tuintptr_t leafkey = rtree_leafkey(key);\n\tassert(leafkey != RTREE_LEAFKEY_INVALID);\n\n\t/* Fast path: L1 direct mapped cache. */\n\tif (likely(rtree_ctx->cache[slot].leafkey == leafkey)) {\n\t\trtree_leaf_elm_t *leaf = rtree_ctx->cache[slot].leaf;\n\t\tassert(leaf != NULL);\n\t\tuintptr_t subkey = rtree_subkey(key, RTREE_HEIGHT-1);\n\t\treturn &leaf[subkey];\n\t}\n\t/*\n\t * Search the L2 LRU cache.  On hit, swap the matching element into the\n\t * slot in L1 cache, and move the position in L2 up by 1.\n\t */\n#define RTREE_CACHE_CHECK_L2(i) do {\t\t\t\t\t\\\n\tif (likely(rtree_ctx->l2_cache[i].leafkey == leafkey)) {\t\\\n\t\trtree_leaf_elm_t *leaf = rtree_ctx->l2_cache[i].leaf;\t\\\n\t\tassert(leaf != NULL);\t\t\t\t\t\\\n\t\tif (i > 0) {\t\t\t\t\t\t\\\n\t\t\t/* Bubble up by one. */\t\t\t\t\\\n\t\t\trtree_ctx->l2_cache[i].leafkey =\t\t\\\n\t\t\t\trtree_ctx->l2_cache[i - 1].leafkey;\t\\\n\t\t\trtree_ctx->l2_cache[i].leaf =\t\t\t\\\n\t\t\t\trtree_ctx->l2_cache[i - 1].leaf;\t\\\n\t\t\trtree_ctx->l2_cache[i - 1].leafkey =\t\t\\\n\t\t\t    rtree_ctx->cache[slot].leafkey;\t\t\\\n\t\t\trtree_ctx->l2_cache[i - 1].leaf =\t\t\\\n\t\t\t    rtree_ctx->cache[slot].leaf;\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t\trtree_ctx->l2_cache[0].leafkey =\t\t\\\n\t\t\t    rtree_ctx->cache[slot].leafkey;\t\t\\\n\t\t\trtree_ctx->l2_cache[0].leaf =\t\t\t\\\n\t\t\t    rtree_ctx->cache[slot].leaf;\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\trtree_ctx->cache[slot].leafkey = leafkey;\t\t\\\n\t\trtree_ctx->cache[slot].leaf = leaf;\t\t\t\\\n\t\tuintptr_t subkey = rtree_subkey(key, RTREE_HEIGHT-1);\t\\\n\t\treturn &leaf[subkey];\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\t/* Check the first cache entry. */\n\tRTREE_CACHE_CHECK_L2(0);\n\t/* Search the remaining cache elements. */\n\tfor (unsigned i = 1; i < RTREE_CTX_NCACHE_L2; i++) {\n\t\tRTREE_CACHE_CHECK_L2(i);\n\t}\n#undef RTREE_CACHE_CHECK_L2\n\n\treturn rtree_leaf_elm_lookup_hard(tsdn, rtree, rtree_ctx, key,\n\t    dependent, init_missing);\n}\n\nstatic inline bool\nrtree_write(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx, uintptr_t key,\n    extent_t *extent, szind_t szind, bool slab) {\n\t/* Use rtree_clear() to set the extent to NULL. */\n\tassert(extent != NULL);\n\n\trtree_leaf_elm_t *elm = rtree_leaf_elm_lookup(tsdn, rtree, rtree_ctx,\n\t    key, false, true);\n\tif (elm == NULL) {\n\t\treturn true;\n\t}\n\n\tassert(rtree_leaf_elm_extent_read(tsdn, rtree, elm, false) == NULL);\n\trtree_leaf_elm_write(tsdn, rtree, elm, extent, szind, slab);\n\n\treturn false;\n}\n\nJEMALLOC_ALWAYS_INLINE rtree_leaf_elm_t *\nrtree_read(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx, uintptr_t key,\n    bool dependent) {\n\trtree_leaf_elm_t *elm = rtree_leaf_elm_lookup(tsdn, rtree, rtree_ctx,\n\t    key, dependent, false);\n\tif (!dependent && elm == NULL) {\n\t\treturn NULL;\n\t}\n\tassert(elm != NULL);\n\treturn elm;\n}\n\nJEMALLOC_ALWAYS_INLINE extent_t *\nrtree_extent_read(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,\n    uintptr_t key, bool dependent) {\n\trtree_leaf_elm_t *elm = rtree_read(tsdn, rtree, rtree_ctx, key,\n\t    dependent);\n\tif (!dependent && elm == NULL) {\n\t\treturn NULL;\n\t}\n\treturn rtree_leaf_elm_extent_read(tsdn, rtree, elm, dependent);\n}\n\nJEMALLOC_ALWAYS_INLINE szind_t\nrtree_szind_read(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,\n    uintptr_t key, bool dependent) {\n\trtree_leaf_elm_t *elm = rtree_read(tsdn, rtree, rtree_ctx, key,\n\t    dependent);\n\tif (!dependent && elm == NULL) {\n\t\treturn NSIZES;\n\t}\n\treturn rtree_leaf_elm_szind_read(tsdn, rtree, elm, dependent);\n}\n\n/*\n * rtree_slab_read() is intentionally omitted because slab is always read in\n * conjunction with szind, which makes rtree_szind_slab_read() a better choice.\n */\n\nJEMALLOC_ALWAYS_INLINE bool\nrtree_extent_szind_read(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,\n    uintptr_t key, bool dependent, extent_t **r_extent, szind_t *r_szind) {\n\trtree_leaf_elm_t *elm = rtree_read(tsdn, rtree, rtree_ctx, key,\n\t    dependent);\n\tif (!dependent && elm == NULL) {\n\t\treturn true;\n\t}\n\t*r_extent = rtree_leaf_elm_extent_read(tsdn, rtree, elm, dependent);\n\t*r_szind = rtree_leaf_elm_szind_read(tsdn, rtree, elm, dependent);\n\treturn false;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\nrtree_szind_slab_read(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,\n    uintptr_t key, bool dependent, szind_t *r_szind, bool *r_slab) {\n\trtree_leaf_elm_t *elm = rtree_read(tsdn, rtree, rtree_ctx, key,\n\t    dependent);\n\tif (!dependent && elm == NULL) {\n\t\treturn true;\n\t}\n\t*r_szind = rtree_leaf_elm_szind_read(tsdn, rtree, elm, dependent);\n\t*r_slab = rtree_leaf_elm_slab_read(tsdn, rtree, elm, dependent);\n\treturn false;\n}\n\nstatic inline void\nrtree_szind_slab_update(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,\n    uintptr_t key, szind_t szind, bool slab) {\n\tassert(!slab || szind < NBINS);\n\n\trtree_leaf_elm_t *elm = rtree_read(tsdn, rtree, rtree_ctx, key, true);\n\trtree_leaf_elm_szind_slab_update(tsdn, rtree, elm, szind, slab);\n}\n\nstatic inline void\nrtree_clear(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,\n    uintptr_t key) {\n\trtree_leaf_elm_t *elm = rtree_read(tsdn, rtree, rtree_ctx, key, true);\n\tassert(rtree_leaf_elm_extent_read(tsdn, rtree, elm, false) !=\n\t    NULL);\n\trtree_leaf_elm_write(tsdn, rtree, elm, NULL, NSIZES, false);\n}\n\n#endif /* JEMALLOC_INTERNAL_RTREE_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/rtree_tsd.h",
    "content": "#ifndef JEMALLOC_INTERNAL_RTREE_CTX_H\n#define JEMALLOC_INTERNAL_RTREE_CTX_H\n\n/*\n * Number of leafkey/leaf pairs to cache in L1 and L2 level respectively.  Each\n * entry supports an entire leaf, so the cache hit rate is typically high even\n * with a small number of entries.  In rare cases extent activity will straddle\n * the boundary between two leaf nodes.  Furthermore, an arena may use a\n * combination of dss and mmap.  Note that as memory usage grows past the amount\n * that this cache can directly cover, the cache will become less effective if\n * locality of reference is low, but the consequence is merely cache misses\n * while traversing the tree nodes.\n *\n * The L1 direct mapped cache offers consistent and low cost on cache hit.\n * However collision could affect hit rate negatively.  This is resolved by\n * combining with a L2 LRU cache, which requires linear search and re-ordering\n * on access but suffers no collision.  Note that, the cache will itself suffer\n * cache misses if made overly large, plus the cost of linear search in the LRU\n * cache.\n */\n#define RTREE_CTX_LG_NCACHE 4\n#define RTREE_CTX_NCACHE (1 << RTREE_CTX_LG_NCACHE)\n#define RTREE_CTX_NCACHE_L2 8\n\n/*\n * Zero initializer required for tsd initialization only.  Proper initialization\n * done via rtree_ctx_data_init().\n */\n#define RTREE_CTX_ZERO_INITIALIZER {{{0}}}\n\n\ntypedef struct rtree_leaf_elm_s rtree_leaf_elm_t;\n\ntypedef struct rtree_ctx_cache_elm_s rtree_ctx_cache_elm_t;\nstruct rtree_ctx_cache_elm_s {\n\tuintptr_t\t\tleafkey;\n\trtree_leaf_elm_t\t*leaf;\n};\n\ntypedef struct rtree_ctx_s rtree_ctx_t;\nstruct rtree_ctx_s {\n\t/* Direct mapped cache. */\n\trtree_ctx_cache_elm_t\tcache[RTREE_CTX_NCACHE];\n\t/* L2 LRU cache. */\n\trtree_ctx_cache_elm_t\tl2_cache[RTREE_CTX_NCACHE_L2];\n};\n\nvoid rtree_ctx_data_init(rtree_ctx_t *ctx);\n\n#endif /* JEMALLOC_INTERNAL_RTREE_CTX_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/size_classes.sh",
    "content": "#!/bin/sh\n#\n# Usage: size_classes.sh <lg_qarr> <lg_tmin> <lg_parr> <lg_g>\n\n# The following limits are chosen such that they cover all supported platforms.\n\n# Pointer sizes.\nlg_zarr=\"2 3\"\n\n# Quanta.\nlg_qarr=$1\n\n# The range of tiny size classes is [2^lg_tmin..2^(lg_q-1)].\nlg_tmin=$2\n\n# Maximum lookup size.\nlg_kmax=12\n\n# Page sizes.\nlg_parr=`echo $3 | tr ',' ' '`\n\n# Size class group size (number of size classes for each size doubling).\nlg_g=$4\n\npow2() {\n  e=$1\n  pow2_result=1\n  while [ ${e} -gt 0 ] ; do\n    pow2_result=$((${pow2_result} + ${pow2_result}))\n    e=$((${e} - 1))\n  done\n}\n\nlg() {\n  x=$1\n  lg_result=0\n  while [ ${x} -gt 1 ] ; do\n    lg_result=$((${lg_result} + 1))\n    x=$((${x} / 2))\n  done\n}\n\nlg_ceil() {\n  y=$1\n  lg ${y}; lg_floor=${lg_result}\n  pow2 ${lg_floor}; pow2_floor=${pow2_result}\n  if [ ${pow2_floor} -lt ${y} ] ; then\n    lg_ceil_result=$((${lg_floor} + 1))\n  else\n    lg_ceil_result=${lg_floor}\n  fi\n}\n\nreg_size_compute() {\n  lg_grp=$1\n  lg_delta=$2\n  ndelta=$3\n\n  pow2 ${lg_grp}; grp=${pow2_result}\n  pow2 ${lg_delta}; delta=${pow2_result}\n  reg_size=$((${grp} + ${delta}*${ndelta}))\n}\n\nslab_size() {\n  lg_p=$1\n  lg_grp=$2\n  lg_delta=$3\n  ndelta=$4\n\n  pow2 ${lg_p}; p=${pow2_result}\n  reg_size_compute ${lg_grp} ${lg_delta} ${ndelta}\n\n  # Compute smallest slab size that is an integer multiple of reg_size.\n  try_slab_size=${p}\n  try_nregs=$((${try_slab_size} / ${reg_size}))\n  perfect=0\n  while [ ${perfect} -eq 0 ] ; do\n    perfect_slab_size=${try_slab_size}\n    perfect_nregs=${try_nregs}\n\n    try_slab_size=$((${try_slab_size} + ${p}))\n    try_nregs=$((${try_slab_size} / ${reg_size}))\n    if [ ${perfect_slab_size} -eq $((${perfect_nregs} * ${reg_size})) ] ; then\n      perfect=1\n    fi\n  done\n\n  slab_size_pgs=$((${perfect_slab_size} / ${p}))\n}\n\nsize_class() {\n  index=$1\n  lg_grp=$2\n  lg_delta=$3\n  ndelta=$4\n  lg_p=$5\n  lg_kmax=$6\n\n  if [ ${lg_delta} -ge ${lg_p} ] ; then\n    psz=\"yes\"\n  else\n    pow2 ${lg_p}; p=${pow2_result}\n    pow2 ${lg_grp}; grp=${pow2_result}\n    pow2 ${lg_delta}; delta=${pow2_result}\n    sz=$((${grp} + ${delta} * ${ndelta}))\n    npgs=$((${sz} / ${p}))\n    if [ ${sz} -eq $((${npgs} * ${p})) ] ; then\n      psz=\"yes\"\n    else\n      psz=\"no\"\n    fi\n  fi\n\n  lg ${ndelta}; lg_ndelta=${lg_result}; pow2 ${lg_ndelta}\n  if [ ${pow2_result} -lt ${ndelta} ] ; then\n    rem=\"yes\"\n  else\n    rem=\"no\"\n  fi\n\n  lg_size=${lg_grp}\n  if [ $((${lg_delta} + ${lg_ndelta})) -eq ${lg_grp} ] ; then\n    lg_size=$((${lg_grp} + 1))\n  else\n    lg_size=${lg_grp}\n    rem=\"yes\"\n  fi\n\n  if [ ${lg_size} -lt $((${lg_p} + ${lg_g})) ] ; then\n    bin=\"yes\"\n    slab_size ${lg_p} ${lg_grp} ${lg_delta} ${ndelta}; pgs=${slab_size_pgs}\n  else\n    bin=\"no\"\n    pgs=0\n  fi\n  if [ ${lg_size} -lt ${lg_kmax} \\\n      -o ${lg_size} -eq ${lg_kmax} -a ${rem} = \"no\" ] ; then\n    lg_delta_lookup=${lg_delta}\n  else\n    lg_delta_lookup=\"no\"\n  fi\n  printf '    SC(%3d, %6d, %8d, %6d, %3s, %3s, %3d, %2s) \\\\\\n' ${index} ${lg_grp} ${lg_delta} ${ndelta} ${psz} ${bin} ${pgs} ${lg_delta_lookup}\n  # Defined upon return:\n  # - psz (\"yes\" or \"no\")\n  # - bin (\"yes\" or \"no\")\n  # - pgs\n  # - lg_delta_lookup (${lg_delta} or \"no\")\n}\n\nsep_line() {\n  echo \"                                                         \\\\\"\n}\n\nsize_classes() {\n  lg_z=$1\n  lg_q=$2\n  lg_t=$3\n  lg_p=$4\n  lg_g=$5\n\n  pow2 $((${lg_z} + 3)); ptr_bits=${pow2_result}\n  pow2 ${lg_g}; g=${pow2_result}\n\n  echo \"#define SIZE_CLASSES \\\\\"\n  echo \"  /* index, lg_grp, lg_delta, ndelta, psz, bin, pgs, lg_delta_lookup */ \\\\\"\n\n  ntbins=0\n  nlbins=0\n  lg_tiny_maxclass='\"NA\"'\n  nbins=0\n  npsizes=0\n\n  # Tiny size classes.\n  ndelta=0\n  index=0\n  lg_grp=${lg_t}\n  lg_delta=${lg_grp}\n  while [ ${lg_grp} -lt ${lg_q} ] ; do\n    size_class ${index} ${lg_grp} ${lg_delta} ${ndelta} ${lg_p} ${lg_kmax}\n    if [ ${lg_delta_lookup} != \"no\" ] ; then\n      nlbins=$((${index} + 1))\n    fi\n    if [ ${psz} = \"yes\" ] ; then\n      npsizes=$((${npsizes} + 1))\n    fi\n    if [ ${bin} != \"no\" ] ; then\n      nbins=$((${index} + 1))\n    fi\n    ntbins=$((${ntbins} + 1))\n    lg_tiny_maxclass=${lg_grp} # Final written value is correct.\n    index=$((${index} + 1))\n    lg_delta=${lg_grp}\n    lg_grp=$((${lg_grp} + 1))\n  done\n\n  # First non-tiny group.\n  if [ ${ntbins} -gt 0 ] ; then\n    sep_line\n    # The first size class has an unusual encoding, because the size has to be\n    # split between grp and delta*ndelta.\n    lg_grp=$((${lg_grp} - 1))\n    ndelta=1\n    size_class ${index} ${lg_grp} ${lg_delta} ${ndelta} ${lg_p} ${lg_kmax}\n    index=$((${index} + 1))\n    lg_grp=$((${lg_grp} + 1))\n    lg_delta=$((${lg_delta} + 1))\n    if [ ${psz} = \"yes\" ] ; then\n      npsizes=$((${npsizes} + 1))\n    fi\n  fi\n  while [ ${ndelta} -lt ${g} ] ; do\n    size_class ${index} ${lg_grp} ${lg_delta} ${ndelta} ${lg_p} ${lg_kmax}\n    index=$((${index} + 1))\n    ndelta=$((${ndelta} + 1))\n    if [ ${psz} = \"yes\" ] ; then\n      npsizes=$((${npsizes} + 1))\n    fi\n  done\n\n  # All remaining groups.\n  lg_grp=$((${lg_grp} + ${lg_g}))\n  while [ ${lg_grp} -lt $((${ptr_bits} - 1)) ] ; do\n    sep_line\n    ndelta=1\n    if [ ${lg_grp} -eq $((${ptr_bits} - 2)) ] ; then\n      ndelta_limit=$((${g} - 1))\n    else\n      ndelta_limit=${g}\n    fi\n    while [ ${ndelta} -le ${ndelta_limit} ] ; do\n      size_class ${index} ${lg_grp} ${lg_delta} ${ndelta} ${lg_p} ${lg_kmax}\n      if [ ${lg_delta_lookup} != \"no\" ] ; then\n        nlbins=$((${index} + 1))\n        # Final written value is correct:\n        lookup_maxclass=\"((((size_t)1) << ${lg_grp}) + (((size_t)${ndelta}) << ${lg_delta}))\"\n      fi\n      if [ ${psz} = \"yes\" ] ; then\n        npsizes=$((${npsizes} + 1))\n      fi\n      if [ ${bin} != \"no\" ] ; then\n        nbins=$((${index} + 1))\n        # Final written value is correct:\n        small_maxclass=\"((((size_t)1) << ${lg_grp}) + (((size_t)${ndelta}) << ${lg_delta}))\"\n        if [ ${lg_g} -gt 0 ] ; then\n          lg_large_minclass=$((${lg_grp} + 1))\n        else\n          lg_large_minclass=$((${lg_grp} + 2))\n        fi\n      fi\n      # Final written value is correct:\n      large_maxclass=\"((((size_t)1) << ${lg_grp}) + (((size_t)${ndelta}) << ${lg_delta}))\"\n      index=$((${index} + 1))\n      ndelta=$((${ndelta} + 1))\n    done\n    lg_grp=$((${lg_grp} + 1))\n    lg_delta=$((${lg_delta} + 1))\n  done\n  echo\n  nsizes=${index}\n  lg_ceil ${nsizes}; lg_ceil_nsizes=${lg_ceil_result}\n\n  # Defined upon completion:\n  # - ntbins\n  # - nlbins\n  # - nbins\n  # - nsizes\n  # - lg_ceil_nsizes\n  # - npsizes\n  # - lg_tiny_maxclass\n  # - lookup_maxclass\n  # - small_maxclass\n  # - lg_large_minclass\n  # - large_maxclass\n}\n\ncat <<EOF\n#ifndef JEMALLOC_INTERNAL_SIZE_CLASSES_H\n#define JEMALLOC_INTERNAL_SIZE_CLASSES_H\n\n/* This file was automatically generated by size_classes.sh. */\n\n#include \"jemalloc/internal/jemalloc_internal_types.h\"\n\n/*\n * This header file defines:\n *\n *   LG_SIZE_CLASS_GROUP: Lg of size class count for each size doubling.\n *   LG_TINY_MIN: Lg of minimum size class to support.\n *   SIZE_CLASSES: Complete table of SC(index, lg_grp, lg_delta, ndelta, psz,\n *                 bin, pgs, lg_delta_lookup) tuples.\n *     index: Size class index.\n *     lg_grp: Lg group base size (no deltas added).\n *     lg_delta: Lg delta to previous size class.\n *     ndelta: Delta multiplier.  size == 1<<lg_grp + ndelta<<lg_delta\n *     psz: 'yes' if a multiple of the page size, 'no' otherwise.\n *     bin: 'yes' if a small bin size class, 'no' otherwise.\n *     pgs: Slab page count if a small bin size class, 0 otherwise.\n *     lg_delta_lookup: Same as lg_delta if a lookup table size class, 'no'\n *                      otherwise.\n *   NTBINS: Number of tiny bins.\n *   NLBINS: Number of bins supported by the lookup table.\n *   NBINS: Number of small size class bins.\n *   NSIZES: Number of size classes.\n *   LG_CEIL_NSIZES: Number of bits required to store NSIZES.\n *   NPSIZES: Number of size classes that are a multiple of (1U << LG_PAGE).\n *   LG_TINY_MAXCLASS: Lg of maximum tiny size class.\n *   LOOKUP_MAXCLASS: Maximum size class included in lookup table.\n *   SMALL_MAXCLASS: Maximum small size class.\n *   LG_LARGE_MINCLASS: Lg of minimum large size class.\n *   LARGE_MAXCLASS: Maximum (large) size class.\n */\n\n#define LG_SIZE_CLASS_GROUP\t${lg_g}\n#define LG_TINY_MIN\t\t${lg_tmin}\n\nEOF\n\nfor lg_z in ${lg_zarr} ; do\n  for lg_q in ${lg_qarr} ; do\n    lg_t=${lg_tmin}\n    while [ ${lg_t} -le ${lg_q} ] ; do\n      # Iterate through page sizes and compute how many bins there are.\n      for lg_p in ${lg_parr} ; do\n        echo \"#if (LG_SIZEOF_PTR == ${lg_z} && LG_TINY_MIN == ${lg_t} && LG_QUANTUM == ${lg_q} && LG_PAGE == ${lg_p})\"\n        size_classes ${lg_z} ${lg_q} ${lg_t} ${lg_p} ${lg_g}\n        echo \"#define SIZE_CLASSES_DEFINED\"\n        echo \"#define NTBINS\t\t\t${ntbins}\"\n        echo \"#define NLBINS\t\t\t${nlbins}\"\n        echo \"#define NBINS\t\t\t${nbins}\"\n        echo \"#define NSIZES\t\t\t${nsizes}\"\n        echo \"#define LG_CEIL_NSIZES\t\t${lg_ceil_nsizes}\"\n        echo \"#define NPSIZES\t\t\t${npsizes}\"\n        echo \"#define LG_TINY_MAXCLASS\t${lg_tiny_maxclass}\"\n        echo \"#define LOOKUP_MAXCLASS\t\t${lookup_maxclass}\"\n        echo \"#define SMALL_MAXCLASS\t\t${small_maxclass}\"\n        echo \"#define LG_LARGE_MINCLASS\t${lg_large_minclass}\"\n        echo \"#define LARGE_MINCLASS\t\t(ZU(1) << LG_LARGE_MINCLASS)\"\n        echo \"#define LARGE_MAXCLASS\t\t${large_maxclass}\"\n        echo \"#endif\"\n        echo\n      done\n      lg_t=$((${lg_t} + 1))\n    done\n  done\ndone\n\ncat <<EOF\n#ifndef SIZE_CLASSES_DEFINED\n#  error \"No size class definitions match configuration\"\n#endif\n#undef SIZE_CLASSES_DEFINED\n/*\n * The size2index_tab lookup table uses uint8_t to encode each bin index, so we\n * cannot support more than 256 small size classes.\n */\n#if (NBINS > 256)\n#  error \"Too many small size classes\"\n#endif\n\n#endif /* JEMALLOC_INTERNAL_SIZE_CLASSES_H */\nEOF\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/smoothstep.h",
    "content": "#ifndef JEMALLOC_INTERNAL_SMOOTHSTEP_H\n#define JEMALLOC_INTERNAL_SMOOTHSTEP_H\n\n/*\n * This file was generated by the following command:\n *   sh smoothstep.sh smoother 200 24 3 15\n */\n/******************************************************************************/\n\n/*\n * This header defines a precomputed table based on the smoothstep family of\n * sigmoidal curves (https://en.wikipedia.org/wiki/Smoothstep) that grow from 0\n * to 1 in 0 <= x <= 1.  The table is stored as integer fixed point values so\n * that floating point math can be avoided.\n *\n *                      3     2\n *   smoothstep(x) = -2x  + 3x\n *\n *                       5      4      3\n *   smootherstep(x) = 6x  - 15x  + 10x\n *\n *                          7      6      5      4\n *   smootheststep(x) = -20x  + 70x  - 84x  + 35x\n */\n\n#define SMOOTHSTEP_VARIANT\t\"smoother\"\n#define SMOOTHSTEP_NSTEPS\t200\n#define SMOOTHSTEP_BFP\t\t24\n#define SMOOTHSTEP \\\n /* STEP(step, h,                            x,     y) */ \\\n    STEP(   1, UINT64_C(0x0000000000000014), 0.005, 0.000001240643750) \\\n    STEP(   2, UINT64_C(0x00000000000000a5), 0.010, 0.000009850600000) \\\n    STEP(   3, UINT64_C(0x0000000000000229), 0.015, 0.000032995181250) \\\n    STEP(   4, UINT64_C(0x0000000000000516), 0.020, 0.000077619200000) \\\n    STEP(   5, UINT64_C(0x00000000000009dc), 0.025, 0.000150449218750) \\\n    STEP(   6, UINT64_C(0x00000000000010e8), 0.030, 0.000257995800000) \\\n    STEP(   7, UINT64_C(0x0000000000001aa4), 0.035, 0.000406555756250) \\\n    STEP(   8, UINT64_C(0x0000000000002777), 0.040, 0.000602214400000) \\\n    STEP(   9, UINT64_C(0x00000000000037c2), 0.045, 0.000850847793750) \\\n    STEP(  10, UINT64_C(0x0000000000004be6), 0.050, 0.001158125000000) \\\n    STEP(  11, UINT64_C(0x000000000000643c), 0.055, 0.001529510331250) \\\n    STEP(  12, UINT64_C(0x000000000000811f), 0.060, 0.001970265600000) \\\n    STEP(  13, UINT64_C(0x000000000000a2e2), 0.065, 0.002485452368750) \\\n    STEP(  14, UINT64_C(0x000000000000c9d8), 0.070, 0.003079934200000) \\\n    STEP(  15, UINT64_C(0x000000000000f64f), 0.075, 0.003758378906250) \\\n    STEP(  16, UINT64_C(0x0000000000012891), 0.080, 0.004525260800000) \\\n    STEP(  17, UINT64_C(0x00000000000160e7), 0.085, 0.005384862943750) \\\n    STEP(  18, UINT64_C(0x0000000000019f95), 0.090, 0.006341279400000) \\\n    STEP(  19, UINT64_C(0x000000000001e4dc), 0.095, 0.007398417481250) \\\n    STEP(  20, UINT64_C(0x00000000000230fc), 0.100, 0.008560000000000) \\\n    STEP(  21, UINT64_C(0x0000000000028430), 0.105, 0.009829567518750) \\\n    STEP(  22, UINT64_C(0x000000000002deb0), 0.110, 0.011210480600000) \\\n    STEP(  23, UINT64_C(0x00000000000340b1), 0.115, 0.012705922056250) \\\n    STEP(  24, UINT64_C(0x000000000003aa67), 0.120, 0.014318899200000) \\\n    STEP(  25, UINT64_C(0x0000000000041c00), 0.125, 0.016052246093750) \\\n    STEP(  26, UINT64_C(0x00000000000495a8), 0.130, 0.017908625800000) \\\n    STEP(  27, UINT64_C(0x000000000005178b), 0.135, 0.019890532631250) \\\n    STEP(  28, UINT64_C(0x000000000005a1cf), 0.140, 0.022000294400000) \\\n    STEP(  29, UINT64_C(0x0000000000063498), 0.145, 0.024240074668750) \\\n    STEP(  30, UINT64_C(0x000000000006d009), 0.150, 0.026611875000000) \\\n    STEP(  31, UINT64_C(0x000000000007743f), 0.155, 0.029117537206250) \\\n    STEP(  32, UINT64_C(0x0000000000082157), 0.160, 0.031758745600000) \\\n    STEP(  33, UINT64_C(0x000000000008d76b), 0.165, 0.034537029243750) \\\n    STEP(  34, UINT64_C(0x0000000000099691), 0.170, 0.037453764200000) \\\n    STEP(  35, UINT64_C(0x00000000000a5edf), 0.175, 0.040510175781250) \\\n    STEP(  36, UINT64_C(0x00000000000b3067), 0.180, 0.043707340800000) \\\n    STEP(  37, UINT64_C(0x00000000000c0b38), 0.185, 0.047046189818750) \\\n    STEP(  38, UINT64_C(0x00000000000cef5e), 0.190, 0.050527509400000) \\\n    STEP(  39, UINT64_C(0x00000000000ddce6), 0.195, 0.054151944356250) \\\n    STEP(  40, UINT64_C(0x00000000000ed3d8), 0.200, 0.057920000000000) \\\n    STEP(  41, UINT64_C(0x00000000000fd439), 0.205, 0.061832044393750) \\\n    STEP(  42, UINT64_C(0x000000000010de0e), 0.210, 0.065888310600000) \\\n    STEP(  43, UINT64_C(0x000000000011f158), 0.215, 0.070088898931250) \\\n    STEP(  44, UINT64_C(0x0000000000130e17), 0.220, 0.074433779200000) \\\n    STEP(  45, UINT64_C(0x0000000000143448), 0.225, 0.078922792968750) \\\n    STEP(  46, UINT64_C(0x00000000001563e7), 0.230, 0.083555655800000) \\\n    STEP(  47, UINT64_C(0x0000000000169cec), 0.235, 0.088331959506250) \\\n    STEP(  48, UINT64_C(0x000000000017df4f), 0.240, 0.093251174400000) \\\n    STEP(  49, UINT64_C(0x0000000000192b04), 0.245, 0.098312651543750) \\\n    STEP(  50, UINT64_C(0x00000000001a8000), 0.250, 0.103515625000000) \\\n    STEP(  51, UINT64_C(0x00000000001bde32), 0.255, 0.108859214081250) \\\n    STEP(  52, UINT64_C(0x00000000001d458b), 0.260, 0.114342425600000) \\\n    STEP(  53, UINT64_C(0x00000000001eb5f8), 0.265, 0.119964156118750) \\\n    STEP(  54, UINT64_C(0x0000000000202f65), 0.270, 0.125723194200000) \\\n    STEP(  55, UINT64_C(0x000000000021b1bb), 0.275, 0.131618222656250) \\\n    STEP(  56, UINT64_C(0x0000000000233ce3), 0.280, 0.137647820800000) \\\n    STEP(  57, UINT64_C(0x000000000024d0c3), 0.285, 0.143810466693750) \\\n    STEP(  58, UINT64_C(0x0000000000266d40), 0.290, 0.150104539400000) \\\n    STEP(  59, UINT64_C(0x000000000028123d), 0.295, 0.156528321231250) \\\n    STEP(  60, UINT64_C(0x000000000029bf9c), 0.300, 0.163080000000000) \\\n    STEP(  61, UINT64_C(0x00000000002b753d), 0.305, 0.169757671268750) \\\n    STEP(  62, UINT64_C(0x00000000002d32fe), 0.310, 0.176559340600000) \\\n    STEP(  63, UINT64_C(0x00000000002ef8bc), 0.315, 0.183482925806250) \\\n    STEP(  64, UINT64_C(0x000000000030c654), 0.320, 0.190526259200000) \\\n    STEP(  65, UINT64_C(0x0000000000329b9f), 0.325, 0.197687089843750) \\\n    STEP(  66, UINT64_C(0x0000000000347875), 0.330, 0.204963085800000) \\\n    STEP(  67, UINT64_C(0x0000000000365cb0), 0.335, 0.212351836381250) \\\n    STEP(  68, UINT64_C(0x0000000000384825), 0.340, 0.219850854400000) \\\n    STEP(  69, UINT64_C(0x00000000003a3aa8), 0.345, 0.227457578418750) \\\n    STEP(  70, UINT64_C(0x00000000003c340f), 0.350, 0.235169375000000) \\\n    STEP(  71, UINT64_C(0x00000000003e342b), 0.355, 0.242983540956250) \\\n    STEP(  72, UINT64_C(0x0000000000403ace), 0.360, 0.250897305600000) \\\n    STEP(  73, UINT64_C(0x00000000004247c8), 0.365, 0.258907832993750) \\\n    STEP(  74, UINT64_C(0x0000000000445ae9), 0.370, 0.267012224200000) \\\n    STEP(  75, UINT64_C(0x0000000000467400), 0.375, 0.275207519531250) \\\n    STEP(  76, UINT64_C(0x00000000004892d8), 0.380, 0.283490700800000) \\\n    STEP(  77, UINT64_C(0x00000000004ab740), 0.385, 0.291858693568750) \\\n    STEP(  78, UINT64_C(0x00000000004ce102), 0.390, 0.300308369400000) \\\n    STEP(  79, UINT64_C(0x00000000004f0fe9), 0.395, 0.308836548106250) \\\n    STEP(  80, UINT64_C(0x00000000005143bf), 0.400, 0.317440000000000) \\\n    STEP(  81, UINT64_C(0x0000000000537c4d), 0.405, 0.326115448143750) \\\n    STEP(  82, UINT64_C(0x000000000055b95b), 0.410, 0.334859570600000) \\\n    STEP(  83, UINT64_C(0x000000000057fab1), 0.415, 0.343669002681250) \\\n    STEP(  84, UINT64_C(0x00000000005a4015), 0.420, 0.352540339200000) \\\n    STEP(  85, UINT64_C(0x00000000005c894e), 0.425, 0.361470136718750) \\\n    STEP(  86, UINT64_C(0x00000000005ed622), 0.430, 0.370454915800000) \\\n    STEP(  87, UINT64_C(0x0000000000612655), 0.435, 0.379491163256250) \\\n    STEP(  88, UINT64_C(0x00000000006379ac), 0.440, 0.388575334400000) \\\n    STEP(  89, UINT64_C(0x000000000065cfeb), 0.445, 0.397703855293750) \\\n    STEP(  90, UINT64_C(0x00000000006828d6), 0.450, 0.406873125000000) \\\n    STEP(  91, UINT64_C(0x00000000006a842f), 0.455, 0.416079517831250) \\\n    STEP(  92, UINT64_C(0x00000000006ce1bb), 0.460, 0.425319385600000) \\\n    STEP(  93, UINT64_C(0x00000000006f413a), 0.465, 0.434589059868750) \\\n    STEP(  94, UINT64_C(0x000000000071a270), 0.470, 0.443884854200000) \\\n    STEP(  95, UINT64_C(0x000000000074051d), 0.475, 0.453203066406250) \\\n    STEP(  96, UINT64_C(0x0000000000766905), 0.480, 0.462539980800000) \\\n    STEP(  97, UINT64_C(0x000000000078cde7), 0.485, 0.471891870443750) \\\n    STEP(  98, UINT64_C(0x00000000007b3387), 0.490, 0.481254999400000) \\\n    STEP(  99, UINT64_C(0x00000000007d99a4), 0.495, 0.490625624981250) \\\n    STEP( 100, UINT64_C(0x0000000000800000), 0.500, 0.500000000000000) \\\n    STEP( 101, UINT64_C(0x000000000082665b), 0.505, 0.509374375018750) \\\n    STEP( 102, UINT64_C(0x000000000084cc78), 0.510, 0.518745000600000) \\\n    STEP( 103, UINT64_C(0x0000000000873218), 0.515, 0.528108129556250) \\\n    STEP( 104, UINT64_C(0x00000000008996fa), 0.520, 0.537460019200000) \\\n    STEP( 105, UINT64_C(0x00000000008bfae2), 0.525, 0.546796933593750) \\\n    STEP( 106, UINT64_C(0x00000000008e5d8f), 0.530, 0.556115145800000) \\\n    STEP( 107, UINT64_C(0x000000000090bec5), 0.535, 0.565410940131250) \\\n    STEP( 108, UINT64_C(0x0000000000931e44), 0.540, 0.574680614400000) \\\n    STEP( 109, UINT64_C(0x0000000000957bd0), 0.545, 0.583920482168750) \\\n    STEP( 110, UINT64_C(0x000000000097d729), 0.550, 0.593126875000000) \\\n    STEP( 111, UINT64_C(0x00000000009a3014), 0.555, 0.602296144706250) \\\n    STEP( 112, UINT64_C(0x00000000009c8653), 0.560, 0.611424665600000) \\\n    STEP( 113, UINT64_C(0x00000000009ed9aa), 0.565, 0.620508836743750) \\\n    STEP( 114, UINT64_C(0x0000000000a129dd), 0.570, 0.629545084200000) \\\n    STEP( 115, UINT64_C(0x0000000000a376b1), 0.575, 0.638529863281250) \\\n    STEP( 116, UINT64_C(0x0000000000a5bfea), 0.580, 0.647459660800000) \\\n    STEP( 117, UINT64_C(0x0000000000a8054e), 0.585, 0.656330997318750) \\\n    STEP( 118, UINT64_C(0x0000000000aa46a4), 0.590, 0.665140429400000) \\\n    STEP( 119, UINT64_C(0x0000000000ac83b2), 0.595, 0.673884551856250) \\\n    STEP( 120, UINT64_C(0x0000000000aebc40), 0.600, 0.682560000000000) \\\n    STEP( 121, UINT64_C(0x0000000000b0f016), 0.605, 0.691163451893750) \\\n    STEP( 122, UINT64_C(0x0000000000b31efd), 0.610, 0.699691630600000) \\\n    STEP( 123, UINT64_C(0x0000000000b548bf), 0.615, 0.708141306431250) \\\n    STEP( 124, UINT64_C(0x0000000000b76d27), 0.620, 0.716509299200000) \\\n    STEP( 125, UINT64_C(0x0000000000b98c00), 0.625, 0.724792480468750) \\\n    STEP( 126, UINT64_C(0x0000000000bba516), 0.630, 0.732987775800000) \\\n    STEP( 127, UINT64_C(0x0000000000bdb837), 0.635, 0.741092167006250) \\\n    STEP( 128, UINT64_C(0x0000000000bfc531), 0.640, 0.749102694400000) \\\n    STEP( 129, UINT64_C(0x0000000000c1cbd4), 0.645, 0.757016459043750) \\\n    STEP( 130, UINT64_C(0x0000000000c3cbf0), 0.650, 0.764830625000000) \\\n    STEP( 131, UINT64_C(0x0000000000c5c557), 0.655, 0.772542421581250) \\\n    STEP( 132, UINT64_C(0x0000000000c7b7da), 0.660, 0.780149145600000) \\\n    STEP( 133, UINT64_C(0x0000000000c9a34f), 0.665, 0.787648163618750) \\\n    STEP( 134, UINT64_C(0x0000000000cb878a), 0.670, 0.795036914200000) \\\n    STEP( 135, UINT64_C(0x0000000000cd6460), 0.675, 0.802312910156250) \\\n    STEP( 136, UINT64_C(0x0000000000cf39ab), 0.680, 0.809473740800000) \\\n    STEP( 137, UINT64_C(0x0000000000d10743), 0.685, 0.816517074193750) \\\n    STEP( 138, UINT64_C(0x0000000000d2cd01), 0.690, 0.823440659400000) \\\n    STEP( 139, UINT64_C(0x0000000000d48ac2), 0.695, 0.830242328731250) \\\n    STEP( 140, UINT64_C(0x0000000000d64063), 0.700, 0.836920000000000) \\\n    STEP( 141, UINT64_C(0x0000000000d7edc2), 0.705, 0.843471678768750) \\\n    STEP( 142, UINT64_C(0x0000000000d992bf), 0.710, 0.849895460600000) \\\n    STEP( 143, UINT64_C(0x0000000000db2f3c), 0.715, 0.856189533306250) \\\n    STEP( 144, UINT64_C(0x0000000000dcc31c), 0.720, 0.862352179200000) \\\n    STEP( 145, UINT64_C(0x0000000000de4e44), 0.725, 0.868381777343750) \\\n    STEP( 146, UINT64_C(0x0000000000dfd09a), 0.730, 0.874276805800000) \\\n    STEP( 147, UINT64_C(0x0000000000e14a07), 0.735, 0.880035843881250) \\\n    STEP( 148, UINT64_C(0x0000000000e2ba74), 0.740, 0.885657574400000) \\\n    STEP( 149, UINT64_C(0x0000000000e421cd), 0.745, 0.891140785918750) \\\n    STEP( 150, UINT64_C(0x0000000000e58000), 0.750, 0.896484375000000) \\\n    STEP( 151, UINT64_C(0x0000000000e6d4fb), 0.755, 0.901687348456250) \\\n    STEP( 152, UINT64_C(0x0000000000e820b0), 0.760, 0.906748825600000) \\\n    STEP( 153, UINT64_C(0x0000000000e96313), 0.765, 0.911668040493750) \\\n    STEP( 154, UINT64_C(0x0000000000ea9c18), 0.770, 0.916444344200000) \\\n    STEP( 155, UINT64_C(0x0000000000ebcbb7), 0.775, 0.921077207031250) \\\n    STEP( 156, UINT64_C(0x0000000000ecf1e8), 0.780, 0.925566220800000) \\\n    STEP( 157, UINT64_C(0x0000000000ee0ea7), 0.785, 0.929911101068750) \\\n    STEP( 158, UINT64_C(0x0000000000ef21f1), 0.790, 0.934111689400000) \\\n    STEP( 159, UINT64_C(0x0000000000f02bc6), 0.795, 0.938167955606250) \\\n    STEP( 160, UINT64_C(0x0000000000f12c27), 0.800, 0.942080000000000) \\\n    STEP( 161, UINT64_C(0x0000000000f22319), 0.805, 0.945848055643750) \\\n    STEP( 162, UINT64_C(0x0000000000f310a1), 0.810, 0.949472490600000) \\\n    STEP( 163, UINT64_C(0x0000000000f3f4c7), 0.815, 0.952953810181250) \\\n    STEP( 164, UINT64_C(0x0000000000f4cf98), 0.820, 0.956292659200000) \\\n    STEP( 165, UINT64_C(0x0000000000f5a120), 0.825, 0.959489824218750) \\\n    STEP( 166, UINT64_C(0x0000000000f6696e), 0.830, 0.962546235800000) \\\n    STEP( 167, UINT64_C(0x0000000000f72894), 0.835, 0.965462970756250) \\\n    STEP( 168, UINT64_C(0x0000000000f7dea8), 0.840, 0.968241254400000) \\\n    STEP( 169, UINT64_C(0x0000000000f88bc0), 0.845, 0.970882462793750) \\\n    STEP( 170, UINT64_C(0x0000000000f92ff6), 0.850, 0.973388125000000) \\\n    STEP( 171, UINT64_C(0x0000000000f9cb67), 0.855, 0.975759925331250) \\\n    STEP( 172, UINT64_C(0x0000000000fa5e30), 0.860, 0.977999705600000) \\\n    STEP( 173, UINT64_C(0x0000000000fae874), 0.865, 0.980109467368750) \\\n    STEP( 174, UINT64_C(0x0000000000fb6a57), 0.870, 0.982091374200000) \\\n    STEP( 175, UINT64_C(0x0000000000fbe400), 0.875, 0.983947753906250) \\\n    STEP( 176, UINT64_C(0x0000000000fc5598), 0.880, 0.985681100800000) \\\n    STEP( 177, UINT64_C(0x0000000000fcbf4e), 0.885, 0.987294077943750) \\\n    STEP( 178, UINT64_C(0x0000000000fd214f), 0.890, 0.988789519400000) \\\n    STEP( 179, UINT64_C(0x0000000000fd7bcf), 0.895, 0.990170432481250) \\\n    STEP( 180, UINT64_C(0x0000000000fdcf03), 0.900, 0.991440000000000) \\\n    STEP( 181, UINT64_C(0x0000000000fe1b23), 0.905, 0.992601582518750) \\\n    STEP( 182, UINT64_C(0x0000000000fe606a), 0.910, 0.993658720600000) \\\n    STEP( 183, UINT64_C(0x0000000000fe9f18), 0.915, 0.994615137056250) \\\n    STEP( 184, UINT64_C(0x0000000000fed76e), 0.920, 0.995474739200000) \\\n    STEP( 185, UINT64_C(0x0000000000ff09b0), 0.925, 0.996241621093750) \\\n    STEP( 186, UINT64_C(0x0000000000ff3627), 0.930, 0.996920065800000) \\\n    STEP( 187, UINT64_C(0x0000000000ff5d1d), 0.935, 0.997514547631250) \\\n    STEP( 188, UINT64_C(0x0000000000ff7ee0), 0.940, 0.998029734400000) \\\n    STEP( 189, UINT64_C(0x0000000000ff9bc3), 0.945, 0.998470489668750) \\\n    STEP( 190, UINT64_C(0x0000000000ffb419), 0.950, 0.998841875000000) \\\n    STEP( 191, UINT64_C(0x0000000000ffc83d), 0.955, 0.999149152206250) \\\n    STEP( 192, UINT64_C(0x0000000000ffd888), 0.960, 0.999397785600000) \\\n    STEP( 193, UINT64_C(0x0000000000ffe55b), 0.965, 0.999593444243750) \\\n    STEP( 194, UINT64_C(0x0000000000ffef17), 0.970, 0.999742004200000) \\\n    STEP( 195, UINT64_C(0x0000000000fff623), 0.975, 0.999849550781250) \\\n    STEP( 196, UINT64_C(0x0000000000fffae9), 0.980, 0.999922380800000) \\\n    STEP( 197, UINT64_C(0x0000000000fffdd6), 0.985, 0.999967004818750) \\\n    STEP( 198, UINT64_C(0x0000000000ffff5a), 0.990, 0.999990149400000) \\\n    STEP( 199, UINT64_C(0x0000000000ffffeb), 0.995, 0.999998759356250) \\\n    STEP( 200, UINT64_C(0x0000000001000000), 1.000, 1.000000000000000) \\\n\n#endif /* JEMALLOC_INTERNAL_SMOOTHSTEP_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/smoothstep.sh",
    "content": "#!/bin/sh\n#\n# Generate a discrete lookup table for a sigmoid function in the smoothstep\n# family (https://en.wikipedia.org/wiki/Smoothstep), where the lookup table\n# entries correspond to x in [1/nsteps, 2/nsteps, ..., nsteps/nsteps].  Encode\n# the entries using a binary fixed point representation.\n#\n# Usage: smoothstep.sh <variant> <nsteps> <bfp> <xprec> <yprec>\n#\n#        <variant> is in {smooth, smoother, smoothest}.\n#        <nsteps> must be greater than zero.\n#        <bfp> must be in [0..62]; reasonable values are roughly [10..30].\n#        <xprec> is x decimal precision.\n#        <yprec> is y decimal precision.\n\n#set -x\n\ncmd=\"sh smoothstep.sh $*\"\nvariant=$1\nnsteps=$2\nbfp=$3\nxprec=$4\nyprec=$5\n\ncase \"${variant}\" in\n  smooth)\n    ;;\n  smoother)\n    ;;\n  smoothest)\n    ;;\n  *)\n    echo \"Unsupported variant\"\n    exit 1\n    ;;\nesac\n\nsmooth() {\n  step=$1\n  y=`echo ${yprec} k ${step} ${nsteps} / sx _2 lx 3 ^ '*' 3 lx 2 ^ '*' + p | dc | tr -d '\\\\\\\\\\n' | sed -e 's#^\\.#0.#g'`\n  h=`echo ${yprec} k 2 ${bfp} ^ ${y} '*' p | dc | tr -d '\\\\\\\\\\n' | sed -e 's#^\\.#0.#g' | tr '.' ' ' | awk '{print $1}' `\n}\n\nsmoother() {\n  step=$1\n  y=`echo ${yprec} k ${step} ${nsteps} / sx 6 lx 5 ^ '*' _15 lx 4 ^ '*' + 10 lx 3 ^ '*' + p | dc | tr -d '\\\\\\\\\\n' | sed -e 's#^\\.#0.#g'`\n  h=`echo ${yprec} k 2 ${bfp} ^ ${y} '*' p | dc | tr -d '\\\\\\\\\\n' | sed -e 's#^\\.#0.#g' | tr '.' ' ' | awk '{print $1}' `\n}\n\nsmoothest() {\n  step=$1\n  y=`echo ${yprec} k ${step} ${nsteps} / sx _20 lx 7 ^ '*' 70 lx 6 ^ '*' + _84 lx 5 ^ '*' + 35 lx 4 ^ '*' + p | dc | tr -d '\\\\\\\\\\n' | sed -e 's#^\\.#0.#g'`\n  h=`echo ${yprec} k 2 ${bfp} ^ ${y} '*' p | dc | tr -d '\\\\\\\\\\n' | sed -e 's#^\\.#0.#g' | tr '.' ' ' | awk '{print $1}' `\n}\n\ncat <<EOF\n#ifndef JEMALLOC_INTERNAL_SMOOTHSTEP_H\n#define JEMALLOC_INTERNAL_SMOOTHSTEP_H\n\n/*\n * This file was generated by the following command:\n *   $cmd\n */\n/******************************************************************************/\n\n/*\n * This header defines a precomputed table based on the smoothstep family of\n * sigmoidal curves (https://en.wikipedia.org/wiki/Smoothstep) that grow from 0\n * to 1 in 0 <= x <= 1.  The table is stored as integer fixed point values so\n * that floating point math can be avoided.\n *\n *                      3     2\n *   smoothstep(x) = -2x  + 3x\n *\n *                       5      4      3\n *   smootherstep(x) = 6x  - 15x  + 10x\n *\n *                          7      6      5      4\n *   smootheststep(x) = -20x  + 70x  - 84x  + 35x\n */\n\n#define SMOOTHSTEP_VARIANT\t\"${variant}\"\n#define SMOOTHSTEP_NSTEPS\t${nsteps}\n#define SMOOTHSTEP_BFP\t\t${bfp}\n#define SMOOTHSTEP \\\\\n /* STEP(step, h,                            x,     y) */ \\\\\nEOF\n\ns=1\nwhile [ $s -le $nsteps ] ; do\n  $variant ${s}\n  x=`echo ${xprec} k ${s} ${nsteps} / p | dc | tr -d '\\\\\\\\\\n' | sed -e 's#^\\.#0.#g'`\n  printf '    STEP(%4d, UINT64_C(0x%016x), %s, %s) \\\\\\n' ${s} ${h} ${x} ${y}\n\n  s=$((s+1))\ndone\necho\n\ncat <<EOF\n#endif /* JEMALLOC_INTERNAL_SMOOTHSTEP_H */\nEOF\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/spin.h",
    "content": "#ifndef JEMALLOC_INTERNAL_SPIN_H\n#define JEMALLOC_INTERNAL_SPIN_H\n\n#ifdef JEMALLOC_SPIN_C_\n#  define SPIN_INLINE extern inline\n#else\n#  define SPIN_INLINE inline\n#endif\n\n#define SPIN_INITIALIZER {0U}\n\ntypedef struct {\n\tunsigned iteration;\n} spin_t;\n\nSPIN_INLINE void\nspin_adaptive(spin_t *spin) {\n\tvolatile uint32_t i;\n\n\tif (spin->iteration < 5) {\n\t\tfor (i = 0; i < (1U << spin->iteration); i++) {\n\t\t\tCPU_SPINWAIT;\n\t\t}\n\t\tspin->iteration++;\n\t} else {\n#ifdef _WIN32\n\t\tSwitchToThread();\n#else\n\t\tsched_yield();\n#endif\n\t}\n}\n\n#undef SPIN_INLINE\n\n#endif /* JEMALLOC_INTERNAL_SPIN_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/stats.h",
    "content": "#ifndef JEMALLOC_INTERNAL_STATS_H\n#define JEMALLOC_INTERNAL_STATS_H\n\n#include \"jemalloc/internal/atomic.h\"\n#include \"jemalloc/internal/mutex_prof.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/size_classes.h\"\n#include \"jemalloc/internal/stats_tsd.h\"\n\n/*  OPTION(opt,\t\tvar_name,\tdefault,\tset_value_to) */\n#define STATS_PRINT_OPTIONS\t\t\t\t\t\t\\\n    OPTION('J',\t\tjson,\t\tfalse,\t\ttrue)\t\t\\\n    OPTION('g',\t\tgeneral,\ttrue,\t\tfalse)\t\t\\\n    OPTION('m',\t\tmerged,\t\tconfig_stats,\tfalse)\t\t\\\n    OPTION('d',\t\tdestroyed,\tconfig_stats,\tfalse)\t\t\\\n    OPTION('a',\t\tunmerged,\tconfig_stats,\tfalse)\t\t\\\n    OPTION('b',\t\tbins,\t\ttrue,\t\tfalse)\t\t\\\n    OPTION('l',\t\tlarge,\t\ttrue,\t\tfalse)\t\t\\\n    OPTION('x',\t\tmutex,\t\ttrue,\t\tfalse)\n\nenum {\n#define OPTION(o, v, d, s) stats_print_option_num_##v,\n    STATS_PRINT_OPTIONS\n#undef OPTION\n    stats_print_tot_num_options\n};\n\n/* Options for stats_print. */\nextern bool opt_stats_print;\nextern char opt_stats_print_opts[stats_print_tot_num_options+1];\n\n/* Implements je_malloc_stats_print. */\nvoid stats_print(void (*write_cb)(void *, const char *), void *cbopaque,\n    const char *opts);\n\n/*\n * In those architectures that support 64-bit atomics, we use atomic updates for\n * our 64-bit values.  Otherwise, we use a plain uint64_t and synchronize\n * externally.\n */\n#ifdef JEMALLOC_ATOMIC_U64\ntypedef atomic_u64_t arena_stats_u64_t;\n#else\n/* Must hold the arena stats mutex while reading atomically. */\ntypedef uint64_t arena_stats_u64_t;\n#endif\n\ntypedef struct malloc_bin_stats_s {\n\t/*\n\t * Total number of allocation/deallocation requests served directly by\n\t * the bin.  Note that tcache may allocate an object, then recycle it\n\t * many times, resulting many increments to nrequests, but only one\n\t * each to nmalloc and ndalloc.\n\t */\n\tuint64_t\tnmalloc;\n\tuint64_t\tndalloc;\n\n\t/*\n\t * Number of allocation requests that correspond to the size of this\n\t * bin.  This includes requests served by tcache, though tcache only\n\t * periodically merges into this counter.\n\t */\n\tuint64_t\tnrequests;\n\n\t/*\n\t * Current number of regions of this size class, including regions\n\t * currently cached by tcache.\n\t */\n\tsize_t\t\tcurregs;\n\n\t/* Number of tcache fills from this bin. */\n\tuint64_t\tnfills;\n\n\t/* Number of tcache flushes to this bin. */\n\tuint64_t\tnflushes;\n\n\t/* Total number of slabs created for this bin's size class. */\n\tuint64_t\tnslabs;\n\n\t/*\n\t * Total number of slabs reused by extracting them from the slabs heap\n\t * for this bin's size class.\n\t */\n\tuint64_t\treslabs;\n\n\t/* Current number of slabs in this bin. */\n\tsize_t\t\tcurslabs;\n\n\tmutex_prof_data_t mutex_data;\n} malloc_bin_stats_t;\n\ntypedef struct malloc_large_stats_s {\n\t/*\n\t * Total number of allocation/deallocation requests served directly by\n\t * the arena.\n\t */\n\tarena_stats_u64_t\tnmalloc;\n\tarena_stats_u64_t\tndalloc;\n\n\t/*\n\t * Number of allocation requests that correspond to this size class.\n\t * This includes requests served by tcache, though tcache only\n\t * periodically merges into this counter.\n\t */\n\tarena_stats_u64_t\tnrequests; /* Partially derived. */\n\n\t/* Current number of allocations of this size class. */\n\tsize_t\t\tcurlextents; /* Derived. */\n} malloc_large_stats_t;\n\ntypedef struct decay_stats_s {\n\t/* Total number of purge sweeps. */\n\tarena_stats_u64_t\tnpurge;\n\t/* Total number of madvise calls made. */\n\tarena_stats_u64_t\tnmadvise;\n\t/* Total number of pages purged. */\n\tarena_stats_u64_t\tpurged;\n} decay_stats_t;\n\n/*\n * Arena stats.  Note that fields marked \"derived\" are not directly maintained\n * within the arena code; rather their values are derived during stats merge\n * requests.\n */\ntypedef struct arena_stats_s {\n#ifndef JEMALLOC_ATOMIC_U64\n\tmalloc_mutex_t\t\tmtx;\n#endif\n\n\t/* Number of bytes currently mapped, excluding retained memory. */\n\tatomic_zu_t\t\tmapped; /* Partially derived. */\n\n\t/*\n\t * Number of unused virtual memory bytes currently retained.  Retained\n\t * bytes are technically mapped (though always decommitted or purged),\n\t * but they are excluded from the mapped statistic (above).\n\t */\n\tatomic_zu_t\t\tretained; /* Derived. */\n\n\tdecay_stats_t\t\tdecay_dirty;\n\tdecay_stats_t\t\tdecay_muzzy;\n\n\tatomic_zu_t\t\tbase; /* Derived. */\n\tatomic_zu_t\t\tinternal;\n\tatomic_zu_t\t\tresident; /* Derived. */\n\n\tatomic_zu_t\t\tallocated_large; /* Derived. */\n\tarena_stats_u64_t\tnmalloc_large; /* Derived. */\n\tarena_stats_u64_t\tndalloc_large; /* Derived. */\n\tarena_stats_u64_t\tnrequests_large; /* Derived. */\n\n\t/* Number of bytes cached in tcache associated with this arena. */\n\tatomic_zu_t\t\ttcache_bytes; /* Derived. */\n\n\tmutex_prof_data_t mutex_prof_data[mutex_prof_num_arena_mutexes];\n\n\t/* One element for each large size class. */\n\tmalloc_large_stats_t\tlstats[NSIZES - NBINS];\n\n\t/* Arena uptime. */\n\tnstime_t\t\tuptime;\n} arena_stats_t;\n\n#endif /* JEMALLOC_INTERNAL_STATS_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/stats_tsd.h",
    "content": "#ifndef JEMALLOC_INTERNAL_STATS_TSD_H\n#define JEMALLOC_INTERNAL_STATS_TSD_H\n\ntypedef struct tcache_bin_stats_s {\n\t/*\n\t * Number of allocation requests that corresponded to the size of this\n\t * bin.\n\t */\n\tuint64_t\tnrequests;\n} tcache_bin_stats_t;\n\n#endif /* JEMALLOC_INTERNAL_STATS_TSD_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/sz.h",
    "content": "#ifndef JEMALLOC_INTERNAL_SIZE_H\n#define JEMALLOC_INTERNAL_SIZE_H\n\n#include \"jemalloc/internal/bit_util.h\"\n#include \"jemalloc/internal/pages.h\"\n#include \"jemalloc/internal/size_classes.h\"\n#include \"jemalloc/internal/util.h\"\n\n/*\n * sz module: Size computations.\n *\n * Some abbreviations used here:\n *   p: Page\n *   ind: Index\n *   s, sz: Size\n *   u: Usable size\n *   a: Aligned\n *\n * These are not always used completely consistently, but should be enough to\n * interpret function names.  E.g. sz_psz2ind converts page size to page size\n * index; sz_sa2u converts a (size, alignment) allocation request to the usable\n * size that would result from such an allocation.\n */\n\n/*\n * sz_pind2sz_tab encodes the same information as could be computed by\n * sz_pind2sz_compute().\n */\nextern size_t const sz_pind2sz_tab[NPSIZES+1];\n/*\n * sz_index2size_tab encodes the same information as could be computed (at\n * unacceptable cost in some code paths) by sz_index2size_compute().\n */\nextern size_t const sz_index2size_tab[NSIZES];\n/*\n * sz_size2index_tab is a compact lookup table that rounds request sizes up to\n * size classes.  In order to reduce cache footprint, the table is compressed,\n * and all accesses are via sz_size2index().\n */\nextern uint8_t const sz_size2index_tab[];\n\nstatic const size_t sz_large_pad =\n#ifdef JEMALLOC_CACHE_OBLIVIOUS\n    PAGE\n#else\n    0\n#endif\n    ;\n\nJEMALLOC_ALWAYS_INLINE pszind_t\nsz_psz2ind(size_t psz) {\n\tif (unlikely(psz > LARGE_MAXCLASS)) {\n\t\treturn NPSIZES;\n\t}\n\t{\n\t\tpszind_t x = lg_floor((psz<<1)-1);\n\t\tpszind_t shift = (x < LG_SIZE_CLASS_GROUP + LG_PAGE) ? 0 : x -\n\t\t    (LG_SIZE_CLASS_GROUP + LG_PAGE);\n\t\tpszind_t grp = shift << LG_SIZE_CLASS_GROUP;\n\n\t\tpszind_t lg_delta = (x < LG_SIZE_CLASS_GROUP + LG_PAGE + 1) ?\n\t\t    LG_PAGE : x - LG_SIZE_CLASS_GROUP - 1;\n\n\t\tsize_t delta_inverse_mask = ZD(-1) << lg_delta;\n\t\tpszind_t mod = ((((psz-1) & delta_inverse_mask) >> lg_delta)) &\n\t\t    ((ZU(1) << LG_SIZE_CLASS_GROUP) - 1);\n\n\t\tpszind_t ind = grp + mod;\n\t\treturn ind;\n\t}\n}\n\nstatic inline size_t\nsz_pind2sz_compute(pszind_t pind) {\n\tif (unlikely(pind == NPSIZES)) {\n\t\treturn LARGE_MAXCLASS + PAGE;\n\t}\n\t{\n\t\tsize_t grp = pind >> LG_SIZE_CLASS_GROUP;\n\t\tsize_t mod = pind & ((ZU(1) << LG_SIZE_CLASS_GROUP) - 1);\n\n\t\tsize_t grp_size_mask = ~((!!grp)-1);\n\t\tsize_t grp_size = ((ZU(1) << (LG_PAGE +\n\t\t    (LG_SIZE_CLASS_GROUP-1))) << grp) & grp_size_mask;\n\n\t\tsize_t shift = (grp == 0) ? 1 : grp;\n\t\tsize_t lg_delta = shift + (LG_PAGE-1);\n\t\tsize_t mod_size = (mod+1) << lg_delta;\n\n\t\tsize_t sz = grp_size + mod_size;\n\t\treturn sz;\n\t}\n}\n\nstatic inline size_t\nsz_pind2sz_lookup(pszind_t pind) {\n\tsize_t ret = (size_t)sz_pind2sz_tab[pind];\n\tassert(ret == sz_pind2sz_compute(pind));\n\treturn ret;\n}\n\nstatic inline size_t\nsz_pind2sz(pszind_t pind) {\n\tassert(pind < NPSIZES+1);\n\treturn sz_pind2sz_lookup(pind);\n}\n\nstatic inline size_t\nsz_psz2u(size_t psz) {\n\tif (unlikely(psz > LARGE_MAXCLASS)) {\n\t\treturn LARGE_MAXCLASS + PAGE;\n\t}\n\t{\n\t\tsize_t x = lg_floor((psz<<1)-1);\n\t\tsize_t lg_delta = (x < LG_SIZE_CLASS_GROUP + LG_PAGE + 1) ?\n\t\t    LG_PAGE : x - LG_SIZE_CLASS_GROUP - 1;\n\t\tsize_t delta = ZU(1) << lg_delta;\n\t\tsize_t delta_mask = delta - 1;\n\t\tsize_t usize = (psz + delta_mask) & ~delta_mask;\n\t\treturn usize;\n\t}\n}\n\nstatic inline szind_t\nsz_size2index_compute(size_t size) {\n\tif (unlikely(size > LARGE_MAXCLASS)) {\n\t\treturn NSIZES;\n\t}\n#if (NTBINS != 0)\n\tif (size <= (ZU(1) << LG_TINY_MAXCLASS)) {\n\t\tszind_t lg_tmin = LG_TINY_MAXCLASS - NTBINS + 1;\n\t\tszind_t lg_ceil = lg_floor(pow2_ceil_zu(size));\n\t\treturn (lg_ceil < lg_tmin ? 0 : lg_ceil - lg_tmin);\n\t}\n#endif\n\t{\n\t\tszind_t x = lg_floor((size<<1)-1);\n\t\tszind_t shift = (x < LG_SIZE_CLASS_GROUP + LG_QUANTUM) ? 0 :\n\t\t    x - (LG_SIZE_CLASS_GROUP + LG_QUANTUM);\n\t\tszind_t grp = shift << LG_SIZE_CLASS_GROUP;\n\n\t\tszind_t lg_delta = (x < LG_SIZE_CLASS_GROUP + LG_QUANTUM + 1)\n\t\t    ? LG_QUANTUM : x - LG_SIZE_CLASS_GROUP - 1;\n\n\t\tsize_t delta_inverse_mask = ZD(-1) << lg_delta;\n\t\tszind_t mod = ((((size-1) & delta_inverse_mask) >> lg_delta)) &\n\t\t    ((ZU(1) << LG_SIZE_CLASS_GROUP) - 1);\n\n\t\tszind_t index = NTBINS + grp + mod;\n\t\treturn index;\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE szind_t\nsz_size2index_lookup(size_t size) {\n\tassert(size <= LOOKUP_MAXCLASS);\n\t{\n\t\tszind_t ret = (sz_size2index_tab[(size-1) >> LG_TINY_MIN]);\n\t\tassert(ret == sz_size2index_compute(size));\n\t\treturn ret;\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE szind_t\nsz_size2index(size_t size) {\n\tassert(size > 0);\n\tif (likely(size <= LOOKUP_MAXCLASS)) {\n\t\treturn sz_size2index_lookup(size);\n\t}\n\treturn sz_size2index_compute(size);\n}\n\nstatic inline size_t\nsz_index2size_compute(szind_t index) {\n#if (NTBINS > 0)\n\tif (index < NTBINS) {\n\t\treturn (ZU(1) << (LG_TINY_MAXCLASS - NTBINS + 1 + index));\n\t}\n#endif\n\t{\n\t\tsize_t reduced_index = index - NTBINS;\n\t\tsize_t grp = reduced_index >> LG_SIZE_CLASS_GROUP;\n\t\tsize_t mod = reduced_index & ((ZU(1) << LG_SIZE_CLASS_GROUP) -\n\t\t    1);\n\n\t\tsize_t grp_size_mask = ~((!!grp)-1);\n\t\tsize_t grp_size = ((ZU(1) << (LG_QUANTUM +\n\t\t    (LG_SIZE_CLASS_GROUP-1))) << grp) & grp_size_mask;\n\n\t\tsize_t shift = (grp == 0) ? 1 : grp;\n\t\tsize_t lg_delta = shift + (LG_QUANTUM-1);\n\t\tsize_t mod_size = (mod+1) << lg_delta;\n\n\t\tsize_t usize = grp_size + mod_size;\n\t\treturn usize;\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\nsz_index2size_lookup(szind_t index) {\n\tsize_t ret = (size_t)sz_index2size_tab[index];\n\tassert(ret == sz_index2size_compute(index));\n\treturn ret;\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\nsz_index2size(szind_t index) {\n\tassert(index < NSIZES);\n\treturn sz_index2size_lookup(index);\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\nsz_s2u_compute(size_t size) {\n\tif (unlikely(size > LARGE_MAXCLASS)) {\n\t\treturn 0;\n\t}\n#if (NTBINS > 0)\n\tif (size <= (ZU(1) << LG_TINY_MAXCLASS)) {\n\t\tsize_t lg_tmin = LG_TINY_MAXCLASS - NTBINS + 1;\n\t\tsize_t lg_ceil = lg_floor(pow2_ceil_zu(size));\n\t\treturn (lg_ceil < lg_tmin ? (ZU(1) << lg_tmin) :\n\t\t    (ZU(1) << lg_ceil));\n\t}\n#endif\n\t{\n\t\tsize_t x = lg_floor((size<<1)-1);\n\t\tsize_t lg_delta = (x < LG_SIZE_CLASS_GROUP + LG_QUANTUM + 1)\n\t\t    ?  LG_QUANTUM : x - LG_SIZE_CLASS_GROUP - 1;\n\t\tsize_t delta = ZU(1) << lg_delta;\n\t\tsize_t delta_mask = delta - 1;\n\t\tsize_t usize = (size + delta_mask) & ~delta_mask;\n\t\treturn usize;\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\nsz_s2u_lookup(size_t size) {\n\tsize_t ret = sz_index2size_lookup(sz_size2index_lookup(size));\n\n\tassert(ret == sz_s2u_compute(size));\n\treturn ret;\n}\n\n/*\n * Compute usable size that would result from allocating an object with the\n * specified size.\n */\nJEMALLOC_ALWAYS_INLINE size_t\nsz_s2u(size_t size) {\n\tassert(size > 0);\n\tif (likely(size <= LOOKUP_MAXCLASS)) {\n\t\treturn sz_s2u_lookup(size);\n\t}\n\treturn sz_s2u_compute(size);\n}\n\n/*\n * Compute usable size that would result from allocating an object with the\n * specified size and alignment.\n */\nJEMALLOC_ALWAYS_INLINE size_t\nsz_sa2u(size_t size, size_t alignment) {\n\tsize_t usize;\n\n\tassert(alignment != 0 && ((alignment - 1) & alignment) == 0);\n\n\t/* Try for a small size class. */\n\tif (size <= SMALL_MAXCLASS && alignment < PAGE) {\n\t\t/*\n\t\t * Round size up to the nearest multiple of alignment.\n\t\t *\n\t\t * This done, we can take advantage of the fact that for each\n\t\t * small size class, every object is aligned at the smallest\n\t\t * power of two that is non-zero in the base two representation\n\t\t * of the size.  For example:\n\t\t *\n\t\t *   Size |   Base 2 | Minimum alignment\n\t\t *   -----+----------+------------------\n\t\t *     96 |  1100000 |  32\n\t\t *    144 | 10100000 |  32\n\t\t *    192 | 11000000 |  64\n\t\t */\n\t\tusize = sz_s2u(ALIGNMENT_CEILING(size, alignment));\n\t\tif (usize < LARGE_MINCLASS) {\n\t\t\treturn usize;\n\t\t}\n\t}\n\n\t/* Large size class.  Beware of overflow. */\n\n\tif (unlikely(alignment > LARGE_MAXCLASS)) {\n\t\treturn 0;\n\t}\n\n\t/* Make sure result is a large size class. */\n\tif (size <= LARGE_MINCLASS) {\n\t\tusize = LARGE_MINCLASS;\n\t} else {\n\t\tusize = sz_s2u(size);\n\t\tif (usize < size) {\n\t\t\t/* size_t overflow. */\n\t\t\treturn 0;\n\t\t}\n\t}\n\n\t/*\n\t * Calculate the multi-page mapping that large_palloc() would need in\n\t * order to guarantee the alignment.\n\t */\n\tif (usize + sz_large_pad + PAGE_CEILING(alignment) - PAGE < usize) {\n\t\t/* size_t overflow. */\n\t\treturn 0;\n\t}\n\treturn usize;\n}\n\n#endif /* JEMALLOC_INTERNAL_SIZE_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/tcache_externs.h",
    "content": "#ifndef JEMALLOC_INTERNAL_TCACHE_EXTERNS_H\n#define JEMALLOC_INTERNAL_TCACHE_EXTERNS_H\n\n#include \"jemalloc/internal/size_classes.h\"\n\nextern bool\topt_tcache;\nextern ssize_t\topt_lg_tcache_max;\n\nextern tcache_bin_info_t\t*tcache_bin_info;\n\n/*\n * Number of tcache bins.  There are NBINS small-object bins, plus 0 or more\n * large-object bins.\n */\nextern unsigned\tnhbins;\n\n/* Maximum cached size class. */\nextern size_t\ttcache_maxclass;\n\n/*\n * Explicit tcaches, managed via the tcache.{create,flush,destroy} mallctls and\n * usable via the MALLOCX_TCACHE() flag.  The automatic per thread tcaches are\n * completely disjoint from this data structure.  tcaches starts off as a sparse\n * array, so it has no physical memory footprint until individual pages are\n * touched.  This allows the entire array to be allocated the first time an\n * explicit tcache is created without a disproportionate impact on memory usage.\n */\nextern tcaches_t\t*tcaches;\n\nsize_t\ttcache_salloc(tsdn_t *tsdn, const void *ptr);\nvoid\ttcache_event_hard(tsd_t *tsd, tcache_t *tcache);\nvoid\t*tcache_alloc_small_hard(tsdn_t *tsdn, arena_t *arena, tcache_t *tcache,\n    tcache_bin_t *tbin, szind_t binind, bool *tcache_success);\nvoid\ttcache_bin_flush_small(tsd_t *tsd, tcache_t *tcache, tcache_bin_t *tbin,\n    szind_t binind, unsigned rem);\nvoid\ttcache_bin_flush_large(tsd_t *tsd, tcache_bin_t *tbin, szind_t binind,\n    unsigned rem, tcache_t *tcache);\nvoid\ttcache_arena_reassociate(tsdn_t *tsdn, tcache_t *tcache,\n    arena_t *arena);\ntcache_t *tcache_create_explicit(tsd_t *tsd);\nvoid\ttcache_cleanup(tsd_t *tsd);\nvoid\ttcache_stats_merge(tsdn_t *tsdn, tcache_t *tcache, arena_t *arena);\nbool\ttcaches_create(tsd_t *tsd, unsigned *r_ind);\nvoid\ttcaches_flush(tsd_t *tsd, unsigned ind);\nvoid\ttcaches_destroy(tsd_t *tsd, unsigned ind);\nbool\ttcache_boot(tsdn_t *tsdn);\nvoid tcache_arena_associate(tsdn_t *tsdn, tcache_t *tcache, arena_t *arena);\nvoid tcache_prefork(tsdn_t *tsdn);\nvoid tcache_postfork_parent(tsdn_t *tsdn);\nvoid tcache_postfork_child(tsdn_t *tsdn);\nvoid tcache_flush(tsd_t *tsd);\nbool tsd_tcache_data_init(tsd_t *tsd);\nbool tsd_tcache_enabled_data_init(tsd_t *tsd);\n\n#endif /* JEMALLOC_INTERNAL_TCACHE_EXTERNS_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/tcache_inlines.h",
    "content": "#ifndef JEMALLOC_INTERNAL_TCACHE_INLINES_H\n#define JEMALLOC_INTERNAL_TCACHE_INLINES_H\n\n#include \"jemalloc/internal/jemalloc_internal_types.h\"\n#include \"jemalloc/internal/size_classes.h\"\n#include \"jemalloc/internal/sz.h\"\n#include \"jemalloc/internal/ticker.h\"\n#include \"jemalloc/internal/util.h\"\n\nstatic inline bool\ntcache_enabled_get(tsd_t *tsd) {\n\treturn tsd_tcache_enabled_get(tsd);\n}\n\nstatic inline void\ntcache_enabled_set(tsd_t *tsd, bool enabled) {\n\tbool was_enabled = tsd_tcache_enabled_get(tsd);\n\n\tif (!was_enabled && enabled) {\n\t\ttsd_tcache_data_init(tsd);\n\t} else if (was_enabled && !enabled) {\n\t\ttcache_cleanup(tsd);\n\t}\n\t/* Commit the state last.  Above calls check current state. */\n\ttsd_tcache_enabled_set(tsd, enabled);\n\ttsd_slow_update(tsd);\n}\n\nJEMALLOC_ALWAYS_INLINE void\ntcache_event(tsd_t *tsd, tcache_t *tcache) {\n\tif (TCACHE_GC_INCR == 0) {\n\t\treturn;\n\t}\n\n\tif (unlikely(ticker_tick(&tcache->gc_ticker))) {\n\t\ttcache_event_hard(tsd, tcache);\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE void *\ntcache_alloc_easy(tcache_bin_t *tbin, bool *tcache_success) {\n\tvoid *ret;\n\n\tif (unlikely(tbin->ncached == 0)) {\n\t\ttbin->low_water = -1;\n\t\t*tcache_success = false;\n\t\treturn NULL;\n\t}\n\t/*\n\t * tcache_success (instead of ret) should be checked upon the return of\n\t * this function.  We avoid checking (ret == NULL) because there is\n\t * never a null stored on the avail stack (which is unknown to the\n\t * compiler), and eagerly checking ret would cause pipeline stall\n\t * (waiting for the cacheline).\n\t */\n\t*tcache_success = true;\n\tret = *(tbin->avail - tbin->ncached);\n\ttbin->ncached--;\n\n\tif (unlikely((low_water_t)tbin->ncached < tbin->low_water)) {\n\t\ttbin->low_water = tbin->ncached;\n\t}\n\n\treturn ret;\n}\n\nJEMALLOC_ALWAYS_INLINE void *\ntcache_alloc_small(tsd_t *tsd, arena_t *arena, tcache_t *tcache, size_t size,\n    szind_t binind, bool zero, bool slow_path) {\n\tvoid *ret;\n\ttcache_bin_t *tbin;\n\tbool tcache_success;\n\tsize_t usize JEMALLOC_CC_SILENCE_INIT(0);\n\n\tassert(binind < NBINS);\n\ttbin = tcache_small_bin_get(tcache, binind);\n\tret = tcache_alloc_easy(tbin, &tcache_success);\n\tassert(tcache_success == (ret != NULL));\n\tif (unlikely(!tcache_success)) {\n\t\tbool tcache_hard_success;\n\t\tarena = arena_choose(tsd, arena);\n\t\tif (unlikely(arena == NULL)) {\n\t\t\treturn NULL;\n\t\t}\n\n\t\tret = tcache_alloc_small_hard(tsd_tsdn(tsd), arena, tcache,\n\t\t    tbin, binind, &tcache_hard_success);\n\t\tif (tcache_hard_success == false) {\n\t\t\treturn NULL;\n\t\t}\n\t}\n\n\tassert(ret);\n\t/*\n\t * Only compute usize if required.  The checks in the following if\n\t * statement are all static.\n\t */\n\tif (config_prof || (slow_path && config_fill) || unlikely(zero)) {\n\t\tusize = sz_index2size(binind);\n\t\tassert(tcache_salloc(tsd_tsdn(tsd), ret) == usize);\n\t}\n\n\tif (likely(!zero)) {\n\t\tif (slow_path && config_fill) {\n\t\t\tif (unlikely(opt_junk_alloc)) {\n\t\t\t\tarena_alloc_junk_small(ret,\n\t\t\t\t    &arena_bin_info[binind], false);\n\t\t\t} else if (unlikely(opt_zero)) {\n\t\t\t\tmemset(ret, 0, usize);\n\t\t\t}\n\t\t}\n\t} else {\n\t\tif (slow_path && config_fill && unlikely(opt_junk_alloc)) {\n\t\t\tarena_alloc_junk_small(ret, &arena_bin_info[binind],\n\t\t\t    true);\n\t\t}\n\t\tmemset(ret, 0, usize);\n\t}\n\n\tif (config_stats) {\n\t\ttbin->tstats.nrequests++;\n\t}\n\tif (config_prof) {\n\t\ttcache->prof_accumbytes += usize;\n\t}\n\ttcache_event(tsd, tcache);\n\treturn ret;\n}\n\nJEMALLOC_ALWAYS_INLINE void *\ntcache_alloc_large(tsd_t *tsd, arena_t *arena, tcache_t *tcache, size_t size,\n    szind_t binind, bool zero, bool slow_path) {\n\tvoid *ret;\n\ttcache_bin_t *tbin;\n\tbool tcache_success;\n\n\tassert(binind >= NBINS &&binind < nhbins);\n\ttbin = tcache_large_bin_get(tcache, binind);\n\tret = tcache_alloc_easy(tbin, &tcache_success);\n\tassert(tcache_success == (ret != NULL));\n\tif (unlikely(!tcache_success)) {\n\t\t/*\n\t\t * Only allocate one large object at a time, because it's quite\n\t\t * expensive to create one and not use it.\n\t\t */\n\t\tarena = arena_choose(tsd, arena);\n\t\tif (unlikely(arena == NULL)) {\n\t\t\treturn NULL;\n\t\t}\n\n\t\tret = large_malloc(tsd_tsdn(tsd), arena, sz_s2u(size), zero);\n\t\tif (ret == NULL) {\n\t\t\treturn NULL;\n\t\t}\n\t} else {\n\t\tsize_t usize JEMALLOC_CC_SILENCE_INIT(0);\n\n\t\t/* Only compute usize on demand */\n\t\tif (config_prof || (slow_path && config_fill) ||\n\t\t    unlikely(zero)) {\n\t\t\tusize = sz_index2size(binind);\n\t\t\tassert(usize <= tcache_maxclass);\n\t\t}\n\n\t\tif (likely(!zero)) {\n\t\t\tif (slow_path && config_fill) {\n\t\t\t\tif (unlikely(opt_junk_alloc)) {\n\t\t\t\t\tmemset(ret, JEMALLOC_ALLOC_JUNK,\n\t\t\t\t\t    usize);\n\t\t\t\t} else if (unlikely(opt_zero)) {\n\t\t\t\t\tmemset(ret, 0, usize);\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tmemset(ret, 0, usize);\n\t\t}\n\n\t\tif (config_stats) {\n\t\t\ttbin->tstats.nrequests++;\n\t\t}\n\t\tif (config_prof) {\n\t\t\ttcache->prof_accumbytes += usize;\n\t\t}\n\t}\n\n\ttcache_event(tsd, tcache);\n\treturn ret;\n}\n\nJEMALLOC_ALWAYS_INLINE void\ntcache_dalloc_small(tsd_t *tsd, tcache_t *tcache, void *ptr, szind_t binind,\n    bool slow_path) {\n\ttcache_bin_t *tbin;\n\ttcache_bin_info_t *tbin_info;\n\n\tassert(tcache_salloc(tsd_tsdn(tsd), ptr) <= SMALL_MAXCLASS);\n\n\tif (slow_path && config_fill && unlikely(opt_junk_free)) {\n\t\tarena_dalloc_junk_small(ptr, &arena_bin_info[binind]);\n\t}\n\n\ttbin = tcache_small_bin_get(tcache, binind);\n\ttbin_info = &tcache_bin_info[binind];\n\tif (unlikely(tbin->ncached == tbin_info->ncached_max)) {\n\t\ttcache_bin_flush_small(tsd, tcache, tbin, binind,\n\t\t    (tbin_info->ncached_max >> 1));\n\t}\n\tassert(tbin->ncached < tbin_info->ncached_max);\n\ttbin->ncached++;\n\t*(tbin->avail - tbin->ncached) = ptr;\n\n\ttcache_event(tsd, tcache);\n}\n\nJEMALLOC_ALWAYS_INLINE void\ntcache_dalloc_large(tsd_t *tsd, tcache_t *tcache, void *ptr, szind_t binind,\n    bool slow_path) {\n\ttcache_bin_t *tbin;\n\ttcache_bin_info_t *tbin_info;\n\n\tassert(tcache_salloc(tsd_tsdn(tsd), ptr) > SMALL_MAXCLASS);\n\tassert(tcache_salloc(tsd_tsdn(tsd), ptr) <= tcache_maxclass);\n\n\tif (slow_path && config_fill && unlikely(opt_junk_free)) {\n\t\tlarge_dalloc_junk(ptr, sz_index2size(binind));\n\t}\n\n\ttbin = tcache_large_bin_get(tcache, binind);\n\ttbin_info = &tcache_bin_info[binind];\n\tif (unlikely(tbin->ncached == tbin_info->ncached_max)) {\n\t\ttcache_bin_flush_large(tsd, tbin, binind,\n\t\t    (tbin_info->ncached_max >> 1), tcache);\n\t}\n\tassert(tbin->ncached < tbin_info->ncached_max);\n\ttbin->ncached++;\n\t*(tbin->avail - tbin->ncached) = ptr;\n\n\ttcache_event(tsd, tcache);\n}\n\nJEMALLOC_ALWAYS_INLINE tcache_t *\ntcaches_get(tsd_t *tsd, unsigned ind) {\n\ttcaches_t *elm = &tcaches[ind];\n\tif (unlikely(elm->tcache == NULL)) {\n\t\telm->tcache = tcache_create_explicit(tsd);\n\t}\n\treturn elm->tcache;\n}\n\n#endif /* JEMALLOC_INTERNAL_TCACHE_INLINES_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/tcache_structs.h",
    "content": "#ifndef JEMALLOC_INTERNAL_TCACHE_STRUCTS_H\n#define JEMALLOC_INTERNAL_TCACHE_STRUCTS_H\n\n#include \"jemalloc/internal/ql.h\"\n#include \"jemalloc/internal/size_classes.h\"\n#include \"jemalloc/internal/stats_tsd.h\"\n#include \"jemalloc/internal/ticker.h\"\n\n/*\n * Read-only information associated with each element of tcache_t's tbins array\n * is stored separately, mainly to reduce memory usage.\n */\nstruct tcache_bin_info_s {\n\tunsigned\tncached_max;\t/* Upper limit on ncached. */\n};\n\nstruct tcache_bin_s {\n\tlow_water_t\tlow_water;\t/* Min # cached since last GC. */\n\tuint32_t\tncached;\t/* # of cached objects. */\n\t/*\n\t * ncached and stats are both modified frequently.  Let's keep them\n\t * close so that they have a higher chance of being on the same\n\t * cacheline, thus less write-backs.\n\t */\n\ttcache_bin_stats_t tstats;\n\t/*\n\t * To make use of adjacent cacheline prefetch, the items in the avail\n\t * stack goes to higher address for newer allocations.  avail points\n\t * just above the available space, which means that\n\t * avail[-ncached, ... -1] are available items and the lowest item will\n\t * be allocated first.\n\t */\n\tvoid\t\t**avail;\t/* Stack of available objects. */\n};\n\nstruct tcache_s {\n\t/* Data accessed frequently first: prof, ticker and small bins. */\n\tuint64_t\tprof_accumbytes;/* Cleared after arena_prof_accum(). */\n\tticker_t\tgc_ticker;\t/* Drives incremental GC. */\n\t/*\n\t * The pointer stacks associated with tbins follow as a contiguous\n\t * array.  During tcache initialization, the avail pointer in each\n\t * element of tbins is initialized to point to the proper offset within\n\t * this array.\n\t */\n\ttcache_bin_t\ttbins_small[NBINS];\n\t/* Data accessed less often below. */\n\tql_elm(tcache_t) link;\t\t/* Used for aggregating stats. */\n\tarena_t\t\t*arena;\t\t/* Associated arena. */\n\tszind_t\t\tnext_gc_bin;\t/* Next bin to GC. */\n\t/* For small bins, fill (ncached_max >> lg_fill_div). */\n\tuint8_t\t\tlg_fill_div[NBINS];\n\ttcache_bin_t\ttbins_large[NSIZES-NBINS];\n};\n\n/* Linkage for list of available (previously used) explicit tcache IDs. */\nstruct tcaches_s {\n\tunion {\n\t\ttcache_t\t*tcache;\n\t\ttcaches_t\t*next;\n\t};\n};\n\n#endif /* JEMALLOC_INTERNAL_TCACHE_STRUCTS_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/tcache_types.h",
    "content": "#ifndef JEMALLOC_INTERNAL_TCACHE_TYPES_H\n#define JEMALLOC_INTERNAL_TCACHE_TYPES_H\n\n#include \"jemalloc/internal/size_classes.h\"\n\ntypedef struct tcache_bin_info_s tcache_bin_info_t;\ntypedef struct tcache_bin_s tcache_bin_t;\ntypedef struct tcache_s tcache_t;\ntypedef struct tcaches_s tcaches_t;\n\n/* ncached is cast to this type for comparison. */\ntypedef int32_t low_water_t;\n\n/*\n * tcache pointers close to NULL are used to encode state information that is\n * used for two purposes: preventing thread caching on a per thread basis and\n * cleaning up during thread shutdown.\n */\n#define TCACHE_STATE_DISABLED\t\t((tcache_t *)(uintptr_t)1)\n#define TCACHE_STATE_REINCARNATED\t((tcache_t *)(uintptr_t)2)\n#define TCACHE_STATE_PURGATORY\t\t((tcache_t *)(uintptr_t)3)\n#define TCACHE_STATE_MAX\t\tTCACHE_STATE_PURGATORY\n\n/*\n * Absolute minimum number of cache slots for each small bin.\n */\n#define TCACHE_NSLOTS_SMALL_MIN\t\t20\n\n/*\n * Absolute maximum number of cache slots for each small bin in the thread\n * cache.  This is an additional constraint beyond that imposed as: twice the\n * number of regions per slab for this size class.\n *\n * This constant must be an even number.\n */\n#define TCACHE_NSLOTS_SMALL_MAX\t\t200\n\n/* Number of cache slots for large size classes. */\n#define TCACHE_NSLOTS_LARGE\t\t20\n\n/* (1U << opt_lg_tcache_max) is used to compute tcache_maxclass. */\n#define LG_TCACHE_MAXCLASS_DEFAULT\t15\n\n/*\n * TCACHE_GC_SWEEP is the approximate number of allocation events between\n * full GC sweeps.  Integer rounding may cause the actual number to be\n * slightly higher, since GC is performed incrementally.\n */\n#define TCACHE_GC_SWEEP\t\t\t8192\n\n/* Number of tcache allocation/deallocation events between incremental GCs. */\n#define TCACHE_GC_INCR\t\t\t\t\t\t\t\\\n    ((TCACHE_GC_SWEEP / NBINS) + ((TCACHE_GC_SWEEP / NBINS == 0) ? 0 : 1))\n\n/* Used in TSD static initializer only. Real init in tcache_data_init(). */\n#define TCACHE_ZERO_INITIALIZER {0}\n\n/* Used in TSD static initializer only. Will be initialized to opt_tcache. */\n#define TCACHE_ENABLED_ZERO_INITIALIZER false\n\n#endif /* JEMALLOC_INTERNAL_TCACHE_TYPES_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/ticker.h",
    "content": "#ifndef JEMALLOC_INTERNAL_TICKER_H\n#define JEMALLOC_INTERNAL_TICKER_H\n\n#include \"jemalloc/internal/util.h\"\n\n/**\n * A ticker makes it easy to count-down events until some limit.  You\n * ticker_init the ticker to trigger every nticks events.  You then notify it\n * that an event has occurred with calls to ticker_tick (or that nticks events\n * have occurred with a call to ticker_ticks), which will return true (and reset\n * the counter) if the countdown hit zero.\n */\n\ntypedef struct {\n\tint32_t tick;\n\tint32_t nticks;\n} ticker_t;\n\nstatic inline void\nticker_init(ticker_t *ticker, int32_t nticks) {\n\tticker->tick = nticks;\n\tticker->nticks = nticks;\n}\n\nstatic inline void\nticker_copy(ticker_t *ticker, const ticker_t *other) {\n\t*ticker = *other;\n}\n\nstatic inline int32_t\nticker_read(const ticker_t *ticker) {\n\treturn ticker->tick;\n}\n\nstatic inline bool\nticker_ticks(ticker_t *ticker, int32_t nticks) {\n\tif (unlikely(ticker->tick < nticks)) {\n\t\tticker->tick = ticker->nticks;\n\t\treturn true;\n\t}\n\tticker->tick -= nticks;\n\treturn(false);\n}\n\nstatic inline bool\nticker_tick(ticker_t *ticker) {\n\treturn ticker_ticks(ticker, 1);\n}\n\n#endif /* JEMALLOC_INTERNAL_TICKER_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/tsd.h",
    "content": "#ifndef JEMALLOC_INTERNAL_TSD_H\n#define JEMALLOC_INTERNAL_TSD_H\n\n#include \"jemalloc/internal/arena_types.h\"\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/jemalloc_internal_externs.h\"\n#include \"jemalloc/internal/prof_types.h\"\n#include \"jemalloc/internal/ql.h\"\n#include \"jemalloc/internal/rtree_tsd.h\"\n#include \"jemalloc/internal/tcache_types.h\"\n#include \"jemalloc/internal/tcache_structs.h\"\n#include \"jemalloc/internal/util.h\"\n#include \"jemalloc/internal/witness.h\"\n\n/*\n * Thread-Specific-Data layout\n * --- data accessed on tcache fast path: state, rtree_ctx, stats, prof ---\n * s: state\n * e: tcache_enabled\n * m: thread_allocated (config_stats)\n * f: thread_deallocated (config_stats)\n * p: prof_tdata (config_prof)\n * c: rtree_ctx (rtree cache accessed on deallocation)\n * t: tcache\n * --- data not accessed on tcache fast path: arena-related fields ---\n * d: arenas_tdata_bypass\n * r: reentrancy_level\n * x: narenas_tdata\n * i: iarena\n * a: arena\n * o: arenas_tdata\n * Loading TSD data is on the critical path of basically all malloc operations.\n * In particular, tcache and rtree_ctx rely on hot CPU cache to be effective.\n * Use a compact layout to reduce cache footprint.\n * +--- 64-bit and 64B cacheline; 1B each letter; First byte on the left. ---+\n * |----------------------------  1st cacheline  ----------------------------|\n * | sedrxxxx mmmmmmmm ffffffff pppppppp [c * 32  ........ ........ .......] |\n * |----------------------------  2nd cacheline  ----------------------------|\n * | [c * 64  ........ ........ ........ ........ ........ ........ .......] |\n * |----------------------------  3nd cacheline  ----------------------------|\n * | [c * 32  ........ ........ .......] iiiiiiii aaaaaaaa oooooooo [t...... |\n * +-------------------------------------------------------------------------+\n * Note: the entire tcache is embedded into TSD and spans multiple cachelines.\n *\n * The last 3 members (i, a and o) before tcache isn't really needed on tcache\n * fast path.  However we have a number of unused tcache bins and witnesses\n * (never touched unless config_debug) at the end of tcache, so we place them\n * there to avoid breaking the cachelines and possibly paging in an extra page.\n */\n#ifdef JEMALLOC_JET\ntypedef void (*test_callback_t)(int *);\n#  define MALLOC_TSD_TEST_DATA_INIT 0x72b65c10\n#  define MALLOC_TEST_TSD \\\n    O(test_data,\t\tint,\t\t\tint)\t\t\\\n    O(test_callback,\t\ttest_callback_t,\tint)\n#  define MALLOC_TEST_TSD_INITIALIZER , MALLOC_TSD_TEST_DATA_INIT, NULL\n#else\n#  define MALLOC_TEST_TSD\n#  define MALLOC_TEST_TSD_INITIALIZER\n#endif\n\n/*  O(name,\t\t\ttype,\t\t\tnullable type */\n#define MALLOC_TSD\t\t\t\t\t\t\t\\\n    O(tcache_enabled,\t\tbool,\t\t\tbool)\t\t\\\n    O(arenas_tdata_bypass,\tbool,\t\t\tbool)\t\t\\\n    O(reentrancy_level,\t\tint8_t,\t\t\tint8_t)\t\t\\\n    O(narenas_tdata,\t\tuint32_t,\t\tuint32_t)\t\\\n    O(thread_allocated,\t\tuint64_t,\t\tuint64_t)\t\\\n    O(thread_deallocated,\tuint64_t,\t\tuint64_t)\t\\\n    O(prof_tdata,\t\tprof_tdata_t *,\t\tprof_tdata_t *)\t\\\n    O(rtree_ctx,\t\trtree_ctx_t,\t\trtree_ctx_t)\t\\\n    O(iarena,\t\t\tarena_t *,\t\tarena_t *)\t\\\n    O(arena,\t\t\tarena_t *,\t\tarena_t *)\t\\\n    O(arenas_tdata,\t\tarena_tdata_t *,\tarena_tdata_t *)\\\n    O(tcache,\t\t\ttcache_t,\t\ttcache_t)\t\\\n    O(witness_tsd,              witness_tsd_t,\t\twitness_tsdn_t)\t\\\n    MALLOC_TEST_TSD\n\n#define TSD_INITIALIZER {\t\t\t\t\t\t\\\n    tsd_state_uninitialized,\t\t\t\t\t\t\\\n    TCACHE_ENABLED_ZERO_INITIALIZER,\t\t\t\t\t\\\n    false,\t\t\t\t\t\t\t\t\\\n    0,\t\t\t\t\t\t\t\t\t\\\n    0,\t\t\t\t\t\t\t\t\t\\\n    0,\t\t\t\t\t\t\t\t\t\\\n    0,\t\t\t\t\t\t\t\t\t\\\n    NULL,\t\t\t\t\t\t\t\t\\\n    RTREE_CTX_ZERO_INITIALIZER,\t\t\t\t\t\t\\\n    NULL,\t\t\t\t\t\t\t\t\\\n    NULL,\t\t\t\t\t\t\t\t\\\n    NULL,\t\t\t\t\t\t\t\t\\\n    TCACHE_ZERO_INITIALIZER,\t\t\t\t\t\t\\\n    WITNESS_TSD_INITIALIZER\t\t\t\t\t\t\\\n    MALLOC_TEST_TSD_INITIALIZER\t\t\t\t\t\t\\\n}\n\nenum {\n\ttsd_state_nominal = 0, /* Common case --> jnz. */\n\ttsd_state_nominal_slow = 1, /* Initialized but on slow path. */\n\t/* the above 2 nominal states should be lower values. */\n\ttsd_state_nominal_max = 1, /* used for comparison only. */\n\ttsd_state_minimal_initialized = 2,\n\ttsd_state_purgatory = 3,\n\ttsd_state_reincarnated = 4,\n\ttsd_state_uninitialized = 5\n};\n\n/* Manually limit tsd_state_t to a single byte. */\ntypedef uint8_t tsd_state_t;\n\n/* The actual tsd. */\nstruct tsd_s {\n\t/*\n\t * The contents should be treated as totally opaque outside the tsd\n\t * module.  Access any thread-local state through the getters and\n\t * setters below.\n\t */\n\ttsd_state_t\tstate;\n#define O(n, t, nt)\t\t\t\t\t\t\t\\\n\tt use_a_getter_or_setter_instead_##n;\nMALLOC_TSD\n#undef O\n};\n\n/*\n * Wrapper around tsd_t that makes it possible to avoid implicit conversion\n * between tsd_t and tsdn_t, where tsdn_t is \"nullable\" and has to be\n * explicitly converted to tsd_t, which is non-nullable.\n */\nstruct tsdn_s {\n\ttsd_t tsd;\n};\n#define TSDN_NULL ((tsdn_t *)0)\nJEMALLOC_ALWAYS_INLINE tsdn_t *\ntsd_tsdn(tsd_t *tsd) {\n\treturn (tsdn_t *)tsd;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsdn_null(const tsdn_t *tsdn) {\n\treturn tsdn == NULL;\n}\n\nJEMALLOC_ALWAYS_INLINE tsd_t *\ntsdn_tsd(tsdn_t *tsdn) {\n\tassert(!tsdn_null(tsdn));\n\n\treturn &tsdn->tsd;\n}\n\nvoid *malloc_tsd_malloc(size_t size);\nvoid malloc_tsd_dalloc(void *wrapper);\nvoid malloc_tsd_cleanup_register(bool (*f)(void));\ntsd_t *malloc_tsd_boot0(void);\nvoid malloc_tsd_boot1(void);\nvoid tsd_cleanup(void *arg);\ntsd_t *tsd_fetch_slow(tsd_t *tsd, bool internal);\nvoid tsd_slow_update(tsd_t *tsd);\n\n/*\n * We put the platform-specific data declarations and inlines into their own\n * header files to avoid cluttering this file.  They define tsd_boot0,\n * tsd_boot1, tsd_boot, tsd_booted_get, tsd_get_allocates, tsd_get, and tsd_set.\n */\n#ifdef JEMALLOC_MALLOC_THREAD_CLEANUP\n#include \"jemalloc/internal/tsd_malloc_thread_cleanup.h\"\n#elif (defined(JEMALLOC_TLS))\n#include \"jemalloc/internal/tsd_tls.h\"\n#elif (defined(_WIN32))\n#include \"jemalloc/internal/tsd_win.h\"\n#else\n#include \"jemalloc/internal/tsd_generic.h\"\n#endif\n\n/*\n * tsd_foop_get_unsafe(tsd) returns a pointer to the thread-local instance of\n * foo.  This omits some safety checks, and so can be used during tsd\n * initialization and cleanup.\n */\n#define O(n, t, nt)\t\t\t\t\t\t\t\\\nJEMALLOC_ALWAYS_INLINE t *\t\t\t\t\t\t\\\ntsd_##n##p_get_unsafe(tsd_t *tsd) {\t\t\t\t\t\\\n\treturn &tsd->use_a_getter_or_setter_instead_##n;\t\t\\\n}\nMALLOC_TSD\n#undef O\n\n/* tsd_foop_get(tsd) returns a pointer to the thread-local instance of foo. */\n#define O(n, t, nt)\t\t\t\t\t\t\t\\\nJEMALLOC_ALWAYS_INLINE t *\t\t\t\t\t\t\\\ntsd_##n##p_get(tsd_t *tsd) {\t\t\t\t\t\t\\\n\tassert(tsd->state == tsd_state_nominal ||\t\t\t\\\n\t    tsd->state == tsd_state_nominal_slow ||\t\t\t\\\n\t    tsd->state == tsd_state_reincarnated ||\t\t\t\\\n\t    tsd->state == tsd_state_minimal_initialized);\t\t\\\n\treturn tsd_##n##p_get_unsafe(tsd);\t\t\t\t\\\n}\nMALLOC_TSD\n#undef O\n\n/*\n * tsdn_foop_get(tsdn) returns either the thread-local instance of foo (if tsdn\n * isn't NULL), or NULL (if tsdn is NULL), cast to the nullable pointer type.\n */\n#define O(n, t, nt)\t\t\t\t\t\t\t\\\nJEMALLOC_ALWAYS_INLINE nt *\t\t\t\t\t\t\\\ntsdn_##n##p_get(tsdn_t *tsdn) {\t\t\t\t\t\t\\\n\tif (tsdn_null(tsdn)) {\t\t\t\t\t\t\\\n\t\treturn NULL;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\ttsd_t *tsd = tsdn_tsd(tsdn);\t\t\t\t\t\\\n\treturn (nt *)tsd_##n##p_get(tsd);\t\t\t\t\\\n}\nMALLOC_TSD\n#undef O\n\n/* tsd_foo_get(tsd) returns the value of the thread-local instance of foo. */\n#define O(n, t, nt)\t\t\t\t\t\t\t\\\nJEMALLOC_ALWAYS_INLINE t\t\t\t\t\t\t\\\ntsd_##n##_get(tsd_t *tsd) {\t\t\t\t\t\t\\\n\treturn *tsd_##n##p_get(tsd);\t\t\t\t\t\\\n}\nMALLOC_TSD\n#undef O\n\n/* tsd_foo_set(tsd, val) updates the thread-local instance of foo to be val. */\n#define O(n, t, nt)\t\t\t\t\t\t\t\\\nJEMALLOC_ALWAYS_INLINE void\t\t\t\t\t\t\\\ntsd_##n##_set(tsd_t *tsd, t val) {\t\t\t\t\t\\\n\tassert(tsd->state != tsd_state_reincarnated &&\t\t\t\\\n\t    tsd->state != tsd_state_minimal_initialized);\t\t\\\n\t*tsd_##n##p_get(tsd) = val;\t\t\t\t\t\\\n}\nMALLOC_TSD\n#undef O\n\nJEMALLOC_ALWAYS_INLINE void\ntsd_assert_fast(tsd_t *tsd) {\n\tassert(!malloc_slow && tsd_tcache_enabled_get(tsd) &&\n\t    tsd_reentrancy_level_get(tsd) == 0);\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_fast(tsd_t *tsd) {\n\tbool fast = (tsd->state == tsd_state_nominal);\n\tif (fast) {\n\t\ttsd_assert_fast(tsd);\n\t}\n\n\treturn fast;\n}\n\nJEMALLOC_ALWAYS_INLINE tsd_t *\ntsd_fetch_impl(bool init, bool minimal) {\n\ttsd_t *tsd = tsd_get(init);\n\n\tif (!init && tsd_get_allocates() && tsd == NULL) {\n\t\treturn NULL;\n\t}\n\tassert(tsd != NULL);\n\n\tif (unlikely(tsd->state != tsd_state_nominal)) {\n\t\treturn tsd_fetch_slow(tsd, minimal);\n\t}\n\tassert(tsd_fast(tsd));\n\ttsd_assert_fast(tsd);\n\n\treturn tsd;\n}\n\n/* Get a minimal TSD that requires no cleanup.  See comments in free(). */\nJEMALLOC_ALWAYS_INLINE tsd_t *\ntsd_fetch_min(void) {\n\treturn tsd_fetch_impl(true, true);\n}\n\n/* For internal background threads use only. */\nJEMALLOC_ALWAYS_INLINE tsd_t *\ntsd_internal_fetch(void) {\n\ttsd_t *tsd = tsd_fetch_min();\n\t/* Use reincarnated state to prevent full initialization. */\n\ttsd->state = tsd_state_reincarnated;\n\n\treturn tsd;\n}\n\nJEMALLOC_ALWAYS_INLINE tsd_t *\ntsd_fetch(void) {\n\treturn tsd_fetch_impl(true, false);\n}\n\nstatic inline bool\ntsd_nominal(tsd_t *tsd) {\n\treturn (tsd->state <= tsd_state_nominal_max);\n}\n\nJEMALLOC_ALWAYS_INLINE tsdn_t *\ntsdn_fetch(void) {\n\tif (!tsd_booted_get()) {\n\t\treturn NULL;\n\t}\n\n\treturn tsd_tsdn(tsd_fetch_impl(false, false));\n}\n\nJEMALLOC_ALWAYS_INLINE rtree_ctx_t *\ntsd_rtree_ctx(tsd_t *tsd) {\n\treturn tsd_rtree_ctxp_get(tsd);\n}\n\nJEMALLOC_ALWAYS_INLINE rtree_ctx_t *\ntsdn_rtree_ctx(tsdn_t *tsdn, rtree_ctx_t *fallback) {\n\t/*\n\t * If tsd cannot be accessed, initialize the fallback rtree_ctx and\n\t * return a pointer to it.\n\t */\n\tif (unlikely(tsdn_null(tsdn))) {\n\t\trtree_ctx_data_init(fallback);\n\t\treturn fallback;\n\t}\n\treturn tsd_rtree_ctx(tsdn_tsd(tsdn));\n}\n\n#endif /* JEMALLOC_INTERNAL_TSD_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/tsd_generic.h",
    "content": "#ifdef JEMALLOC_INTERNAL_TSD_GENERIC_H\n#error This file should be included only once, by tsd.h.\n#endif\n#define JEMALLOC_INTERNAL_TSD_GENERIC_H\n\ntypedef struct tsd_init_block_s tsd_init_block_t;\nstruct tsd_init_block_s {\n\tql_elm(tsd_init_block_t) link;\n\tpthread_t thread;\n\tvoid *data;\n};\n\n/* Defined in tsd.c, to allow the mutex headers to have tsd dependencies. */\ntypedef struct tsd_init_head_s tsd_init_head_t;\n\ntypedef struct {\n\tbool initialized;\n\ttsd_t val;\n} tsd_wrapper_t;\n\nvoid *tsd_init_check_recursion(tsd_init_head_t *head,\n    tsd_init_block_t *block);\nvoid tsd_init_finish(tsd_init_head_t *head, tsd_init_block_t *block);\n\nextern pthread_key_t tsd_tsd;\nextern tsd_init_head_t tsd_init_head;\nextern tsd_wrapper_t tsd_boot_wrapper;\nextern bool tsd_booted;\n\n/* Initialization/cleanup. */\nJEMALLOC_ALWAYS_INLINE void\ntsd_cleanup_wrapper(void *arg) {\n\ttsd_wrapper_t *wrapper = (tsd_wrapper_t *)arg;\n\n\tif (wrapper->initialized) {\n\t\twrapper->initialized = false;\n\t\ttsd_cleanup(&wrapper->val);\n\t\tif (wrapper->initialized) {\n\t\t\t/* Trigger another cleanup round. */\n\t\t\tif (pthread_setspecific(tsd_tsd, (void *)wrapper) != 0)\n\t\t\t{\n\t\t\t\tmalloc_write(\"<jemalloc>: Error setting TSD\\n\");\n\t\t\t\tif (opt_abort) {\n\t\t\t\t\tabort();\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t}\n\tmalloc_tsd_dalloc(wrapper);\n}\n\nJEMALLOC_ALWAYS_INLINE void\ntsd_wrapper_set(tsd_wrapper_t *wrapper) {\n\tif (pthread_setspecific(tsd_tsd, (void *)wrapper) != 0) {\n\t\tmalloc_write(\"<jemalloc>: Error setting TSD\\n\");\n\t\tabort();\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE tsd_wrapper_t *\ntsd_wrapper_get(bool init) {\n\ttsd_wrapper_t *wrapper = (tsd_wrapper_t *)pthread_getspecific(tsd_tsd);\n\n\tif (init && unlikely(wrapper == NULL)) {\n\t\ttsd_init_block_t block;\n\t\twrapper = (tsd_wrapper_t *)\n\t\t    tsd_init_check_recursion(&tsd_init_head, &block);\n\t\tif (wrapper) {\n\t\t\treturn wrapper;\n\t\t}\n\t\twrapper = (tsd_wrapper_t *)\n\t\t    malloc_tsd_malloc(sizeof(tsd_wrapper_t));\n\t\tblock.data = (void *)wrapper;\n\t\tif (wrapper == NULL) {\n\t\t\tmalloc_write(\"<jemalloc>: Error allocating TSD\\n\");\n\t\t\tabort();\n\t\t} else {\n\t\t\twrapper->initialized = false;\n\t\t\ttsd_t initializer = TSD_INITIALIZER;\n\t\t\twrapper->val = initializer;\n\t\t}\n\t\ttsd_wrapper_set(wrapper);\n\t\ttsd_init_finish(&tsd_init_head, &block);\n\t}\n\treturn wrapper;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_boot0(void) {\n\tif (pthread_key_create(&tsd_tsd, tsd_cleanup_wrapper) != 0) {\n\t\treturn true;\n\t}\n\ttsd_wrapper_set(&tsd_boot_wrapper);\n\ttsd_booted = true;\n\treturn false;\n}\n\nJEMALLOC_ALWAYS_INLINE void\ntsd_boot1(void) {\n\ttsd_wrapper_t *wrapper;\n\twrapper = (tsd_wrapper_t *)malloc_tsd_malloc(sizeof(tsd_wrapper_t));\n\tif (wrapper == NULL) {\n\t\tmalloc_write(\"<jemalloc>: Error allocating TSD\\n\");\n\t\tabort();\n\t}\n\ttsd_boot_wrapper.initialized = false;\n\ttsd_cleanup(&tsd_boot_wrapper.val);\n\twrapper->initialized = false;\n\ttsd_t initializer = TSD_INITIALIZER;\n\twrapper->val = initializer;\n\ttsd_wrapper_set(wrapper);\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_boot(void) {\n\tif (tsd_boot0()) {\n\t\treturn true;\n\t}\n\ttsd_boot1();\n\treturn false;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_booted_get(void) {\n\treturn tsd_booted;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_get_allocates(void) {\n\treturn true;\n}\n\n/* Get/set. */\nJEMALLOC_ALWAYS_INLINE tsd_t *\ntsd_get(bool init) {\n\ttsd_wrapper_t *wrapper;\n\n\tassert(tsd_booted);\n\twrapper = tsd_wrapper_get(init);\n\tif (tsd_get_allocates() && !init && wrapper == NULL) {\n\t\treturn NULL;\n\t}\n\treturn &wrapper->val;\n}\n\nJEMALLOC_ALWAYS_INLINE void\ntsd_set(tsd_t *val) {\n\ttsd_wrapper_t *wrapper;\n\n\tassert(tsd_booted);\n\twrapper = tsd_wrapper_get(true);\n\tif (likely(&wrapper->val != val)) {\n\t\twrapper->val = *(val);\n\t}\n\twrapper->initialized = true;\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/tsd_malloc_thread_cleanup.h",
    "content": "#ifdef JEMALLOC_INTERNAL_TSD_MALLOC_THREAD_CLEANUP_H\n#error This file should be included only once, by tsd.h.\n#endif\n#define JEMALLOC_INTERNAL_TSD_MALLOC_THREAD_CLEANUP_H\n\nextern __thread tsd_t tsd_tls;\nextern __thread bool tsd_initialized;\nextern bool tsd_booted;\n\n/* Initialization/cleanup. */\nJEMALLOC_ALWAYS_INLINE bool\ntsd_cleanup_wrapper(void) {\n\tif (tsd_initialized) {\n\t\ttsd_initialized = false;\n\t\ttsd_cleanup(&tsd_tls);\n\t}\n\treturn tsd_initialized;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_boot0(void) {\n\tmalloc_tsd_cleanup_register(&tsd_cleanup_wrapper);\n\ttsd_booted = true;\n\treturn false;\n}\n\nJEMALLOC_ALWAYS_INLINE void\ntsd_boot1(void) {\n\t/* Do nothing. */\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_boot(void) {\n\treturn tsd_boot0();\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_booted_get(void) {\n\treturn tsd_booted;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_get_allocates(void) {\n\treturn false;\n}\n\n/* Get/set. */\nJEMALLOC_ALWAYS_INLINE tsd_t *\ntsd_get(bool init) {\n\tassert(tsd_booted);\n\treturn &tsd_tls;\n}\nJEMALLOC_ALWAYS_INLINE void\ntsd_set(tsd_t *val) {\n\tassert(tsd_booted);\n\tif (likely(&tsd_tls != val)) {\n\t\ttsd_tls = (*val);\n\t}\n\ttsd_initialized = true;\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/tsd_tls.h",
    "content": "#ifdef JEMALLOC_INTERNAL_TSD_TLS_H\n#error This file should be included only once, by tsd.h.\n#endif\n#define JEMALLOC_INTERNAL_TSD_TLS_H\n\nextern __thread tsd_t tsd_tls;\nextern pthread_key_t tsd_tsd;\nextern bool tsd_booted;\n\n/* Initialization/cleanup. */\nJEMALLOC_ALWAYS_INLINE bool\ntsd_boot0(void) {\n\tif (pthread_key_create(&tsd_tsd, &tsd_cleanup) != 0) {\n\t\treturn true;\n\t}\n\ttsd_booted = true;\n\treturn false;\n}\n\nJEMALLOC_ALWAYS_INLINE void\ntsd_boot1(void) {\n\t/* Do nothing. */\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_boot(void) {\n\treturn tsd_boot0();\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_booted_get(void) {\n\treturn tsd_booted;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_get_allocates(void) {\n\treturn false;\n}\n\n/* Get/set. */\nJEMALLOC_ALWAYS_INLINE tsd_t *\ntsd_get(bool init) {\n\tassert(tsd_booted);\n\treturn &tsd_tls;\n}\n\nJEMALLOC_ALWAYS_INLINE void\ntsd_set(tsd_t *val) {\n\tassert(tsd_booted);\n\tif (likely(&tsd_tls != val)) {\n\t\ttsd_tls = (*val);\n\t}\n\tif (pthread_setspecific(tsd_tsd, (void *)(&tsd_tls)) != 0) {\n\t\tmalloc_write(\"<jemalloc>: Error setting tsd.\\n\");\n\t\tif (opt_abort) {\n\t\t\tabort();\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/tsd_types.h",
    "content": "#ifndef JEMALLOC_INTERNAL_TSD_TYPES_H\n#define JEMALLOC_INTERNAL_TSD_TYPES_H\n\n#define MALLOC_TSD_CLEANUPS_MAX\t2\n\ntypedef struct tsd_s tsd_t;\ntypedef struct tsdn_s tsdn_t;\ntypedef bool (*malloc_tsd_cleanup_t)(void);\n\n#endif /* JEMALLOC_INTERNAL_TSD_TYPES_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/tsd_win.h",
    "content": "#ifdef JEMALLOC_INTERNAL_TSD_WIN_H\n#error This file should be included only once, by tsd.h.\n#endif\n#define JEMALLOC_INTERNAL_TSD_WIN_H\n\ntypedef struct {\n\tbool initialized;\n\ttsd_t val;\n} tsd_wrapper_t;\n\nextern DWORD tsd_tsd;\nextern tsd_wrapper_t tsd_boot_wrapper;\nextern bool tsd_booted;\n\n/* Initialization/cleanup. */\nJEMALLOC_ALWAYS_INLINE bool\ntsd_cleanup_wrapper(void) {\n\tDWORD error = GetLastError();\n\ttsd_wrapper_t *wrapper = (tsd_wrapper_t *)TlsGetValue(tsd_tsd);\n\tSetLastError(error);\n\n\tif (wrapper == NULL) {\n\t\treturn false;\n\t}\n\n\tif (wrapper->initialized) {\n\t\twrapper->initialized = false;\n\t\ttsd_cleanup(&wrapper->val);\n\t\tif (wrapper->initialized) {\n\t\t\t/* Trigger another cleanup round. */\n\t\t\treturn true;\n\t\t}\n\t}\n\tmalloc_tsd_dalloc(wrapper);\n\treturn false;\n}\n\nJEMALLOC_ALWAYS_INLINE void\ntsd_wrapper_set(tsd_wrapper_t *wrapper) {\n\tif (!TlsSetValue(tsd_tsd, (void *)wrapper)) {\n\t\tmalloc_write(\"<jemalloc>: Error setting TSD\\n\");\n\t\tabort();\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE tsd_wrapper_t *\ntsd_wrapper_get(bool init) {\n\tDWORD error = GetLastError();\n\ttsd_wrapper_t *wrapper = (tsd_wrapper_t *) TlsGetValue(tsd_tsd);\n\tSetLastError(error);\n\n\tif (init && unlikely(wrapper == NULL)) {\n\t\twrapper = (tsd_wrapper_t *)\n\t\t    malloc_tsd_malloc(sizeof(tsd_wrapper_t));\n\t\tif (wrapper == NULL) {\n\t\t\tmalloc_write(\"<jemalloc>: Error allocating TSD\\n\");\n\t\t\tabort();\n\t\t} else {\n\t\t\twrapper->initialized = false;\n\t\t\t/* MSVC is finicky about aggregate initialization. */\n\t\t\ttsd_t tsd_initializer = TSD_INITIALIZER;\n\t\t\twrapper->val = tsd_initializer;\n\t\t}\n\t\ttsd_wrapper_set(wrapper);\n\t}\n\treturn wrapper;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_boot0(void) {\n\ttsd_tsd = TlsAlloc();\n\tif (tsd_tsd == TLS_OUT_OF_INDEXES) {\n\t\treturn true;\n\t}\n\tmalloc_tsd_cleanup_register(&tsd_cleanup_wrapper);\n\ttsd_wrapper_set(&tsd_boot_wrapper);\n\ttsd_booted = true;\n\treturn false;\n}\n\nJEMALLOC_ALWAYS_INLINE void\ntsd_boot1(void) {\n\ttsd_wrapper_t *wrapper;\n\twrapper = (tsd_wrapper_t *)\n\t    malloc_tsd_malloc(sizeof(tsd_wrapper_t));\n\tif (wrapper == NULL) {\n\t\tmalloc_write(\"<jemalloc>: Error allocating TSD\\n\");\n\t\tabort();\n\t}\n\ttsd_boot_wrapper.initialized = false;\n\ttsd_cleanup(&tsd_boot_wrapper.val);\n\twrapper->initialized = false;\n\ttsd_t initializer = TSD_INITIALIZER;\n\twrapper->val = initializer;\n\ttsd_wrapper_set(wrapper);\n}\nJEMALLOC_ALWAYS_INLINE bool\ntsd_boot(void) {\n\tif (tsd_boot0()) {\n\t\treturn true;\n\t}\n\ttsd_boot1();\n\treturn false;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_booted_get(void) {\n\treturn tsd_booted;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\ntsd_get_allocates(void) {\n\treturn true;\n}\n\n/* Get/set. */\nJEMALLOC_ALWAYS_INLINE tsd_t *\ntsd_get(bool init) {\n\ttsd_wrapper_t *wrapper;\n\n\tassert(tsd_booted);\n\twrapper = tsd_wrapper_get(init);\n\tif (tsd_get_allocates() && !init && wrapper == NULL) {\n\t\treturn NULL;\n\t}\n\treturn &wrapper->val;\n}\n\nJEMALLOC_ALWAYS_INLINE void\ntsd_set(tsd_t *val) {\n\ttsd_wrapper_t *wrapper;\n\n\tassert(tsd_booted);\n\twrapper = tsd_wrapper_get(true);\n\tif (likely(&wrapper->val != val)) {\n\t\twrapper->val = *(val);\n\t}\n\twrapper->initialized = true;\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/util.h",
    "content": "#ifndef JEMALLOC_INTERNAL_UTIL_H\n#define JEMALLOC_INTERNAL_UTIL_H\n\n#define UTIL_INLINE static inline\n\n/* Junk fill patterns. */\n#ifndef JEMALLOC_ALLOC_JUNK\n#  define JEMALLOC_ALLOC_JUNK\t((uint8_t)0xa5)\n#endif\n#ifndef JEMALLOC_FREE_JUNK\n#  define JEMALLOC_FREE_JUNK\t((uint8_t)0x5a)\n#endif\n\n/*\n * Wrap a cpp argument that contains commas such that it isn't broken up into\n * multiple arguments.\n */\n#define JEMALLOC_ARG_CONCAT(...) __VA_ARGS__\n\n/* cpp macro definition stringification. */\n#define STRINGIFY_HELPER(x) #x\n#define STRINGIFY(x) STRINGIFY_HELPER(x)\n\n/*\n * Silence compiler warnings due to uninitialized values.  This is used\n * wherever the compiler fails to recognize that the variable is never used\n * uninitialized.\n */\n#define JEMALLOC_CC_SILENCE_INIT(v) = v\n\n#ifdef __GNUC__\n#  define likely(x)   __builtin_expect(!!(x), 1)\n#  define unlikely(x) __builtin_expect(!!(x), 0)\n#else\n#  define likely(x)   !!(x)\n#  define unlikely(x) !!(x)\n#endif\n\n#if !defined(JEMALLOC_INTERNAL_UNREACHABLE)\n#  error JEMALLOC_INTERNAL_UNREACHABLE should have been defined by configure\n#endif\n\n#define unreachable() JEMALLOC_INTERNAL_UNREACHABLE()\n\n/* Set error code. */\nUTIL_INLINE void\nset_errno(int errnum) {\n#ifdef _WIN32\n\tSetLastError(errnum);\n#else\n\terrno = errnum;\n#endif\n}\n\n/* Get last error code. */\nUTIL_INLINE int\nget_errno(void) {\n#ifdef _WIN32\n\treturn GetLastError();\n#else\n\treturn errno;\n#endif\n}\n\n#undef UTIL_INLINE\n\n#endif /* JEMALLOC_INTERNAL_UTIL_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/internal/witness.h",
    "content": "#ifndef JEMALLOC_INTERNAL_WITNESS_H\n#define JEMALLOC_INTERNAL_WITNESS_H\n\n#include \"jemalloc/internal/ql.h\"\n\n/******************************************************************************/\n/* LOCK RANKS */\n/******************************************************************************/\n\n/*\n * Witnesses with rank WITNESS_RANK_OMIT are completely ignored by the witness\n * machinery.\n */\n\n#define WITNESS_RANK_OMIT\t\t0U\n\n#define WITNESS_RANK_MIN\t\t1U\n\n#define WITNESS_RANK_INIT\t\t1U\n#define WITNESS_RANK_CTL\t\t1U\n#define WITNESS_RANK_TCACHES\t\t2U\n#define WITNESS_RANK_ARENAS\t\t3U\n\n#define WITNESS_RANK_BACKGROUND_THREAD_GLOBAL\t4U\n\n#define WITNESS_RANK_PROF_DUMP\t\t5U\n#define WITNESS_RANK_PROF_BT2GCTX\t6U\n#define WITNESS_RANK_PROF_TDATAS\t7U\n#define WITNESS_RANK_PROF_TDATA\t\t8U\n#define WITNESS_RANK_PROF_GCTX\t\t9U\n\n#define WITNESS_RANK_BACKGROUND_THREAD\t10U\n\n/*\n * Used as an argument to witness_assert_depth_to_rank() in order to validate\n * depth excluding non-core locks with lower ranks.  Since the rank argument to\n * witness_assert_depth_to_rank() is inclusive rather than exclusive, this\n * definition can have the same value as the minimally ranked core lock.\n */\n#define WITNESS_RANK_CORE\t\t11U\n\n#define WITNESS_RANK_DECAY\t\t11U\n#define WITNESS_RANK_TCACHE_QL\t\t12U\n#define WITNESS_RANK_EXTENT_GROW\t13U\n#define WITNESS_RANK_EXTENTS\t\t14U\n#define WITNESS_RANK_EXTENT_AVAIL\t15U\n\n#define WITNESS_RANK_EXTENT_POOL\t16U\n#define WITNESS_RANK_RTREE\t\t17U\n#define WITNESS_RANK_BASE\t\t18U\n#define WITNESS_RANK_ARENA_LARGE\t19U\n\n#define WITNESS_RANK_LEAF\t\t0xffffffffU\n#define WITNESS_RANK_ARENA_BIN\t\tWITNESS_RANK_LEAF\n#define WITNESS_RANK_ARENA_STATS\tWITNESS_RANK_LEAF\n#define WITNESS_RANK_DSS\t\tWITNESS_RANK_LEAF\n#define WITNESS_RANK_PROF_ACTIVE\tWITNESS_RANK_LEAF\n#define WITNESS_RANK_PROF_ACCUM\t\tWITNESS_RANK_LEAF\n#define WITNESS_RANK_PROF_DUMP_SEQ\tWITNESS_RANK_LEAF\n#define WITNESS_RANK_PROF_GDUMP\t\tWITNESS_RANK_LEAF\n#define WITNESS_RANK_PROF_NEXT_THR_UID\tWITNESS_RANK_LEAF\n#define WITNESS_RANK_PROF_THREAD_ACTIVE_INIT\tWITNESS_RANK_LEAF\n\n/******************************************************************************/\n/* PER-WITNESS DATA */\n/******************************************************************************/\n#if defined(JEMALLOC_DEBUG)\n#  define WITNESS_INITIALIZER(name, rank) {name, rank, NULL, NULL, {NULL, NULL}}\n#else\n#  define WITNESS_INITIALIZER(name, rank)\n#endif\n\ntypedef struct witness_s witness_t;\ntypedef unsigned witness_rank_t;\ntypedef ql_head(witness_t) witness_list_t;\ntypedef int witness_comp_t (const witness_t *, void *, const witness_t *,\n    void *);\n\nstruct witness_s {\n\t/* Name, used for printing lock order reversal messages. */\n\tconst char\t\t*name;\n\n\t/*\n\t * Witness rank, where 0 is lowest and UINT_MAX is highest.  Witnesses\n\t * must be acquired in order of increasing rank.\n\t */\n\twitness_rank_t\t\trank;\n\n\t/*\n\t * If two witnesses are of equal rank and they have the samp comp\n\t * function pointer, it is called as a last attempt to differentiate\n\t * between witnesses of equal rank.\n\t */\n\twitness_comp_t\t\t*comp;\n\n\t/* Opaque data, passed to comp(). */\n\tvoid\t\t\t*opaque;\n\n\t/* Linkage for thread's currently owned locks. */\n\tql_elm(witness_t)\tlink;\n};\n\n/******************************************************************************/\n/* PER-THREAD DATA */\n/******************************************************************************/\ntypedef struct witness_tsd_s witness_tsd_t;\nstruct witness_tsd_s {\n\twitness_list_t witnesses;\n\tbool forking;\n};\n\n#define WITNESS_TSD_INITIALIZER { ql_head_initializer(witnesses), false }\n#define WITNESS_TSDN_NULL ((witness_tsdn_t *)0)\n\n/******************************************************************************/\n/* (PER-THREAD) NULLABILITY HELPERS */\n/******************************************************************************/\ntypedef struct witness_tsdn_s witness_tsdn_t;\nstruct witness_tsdn_s {\n\twitness_tsd_t witness_tsd;\n};\n\nJEMALLOC_ALWAYS_INLINE witness_tsdn_t *\nwitness_tsd_tsdn(witness_tsd_t *witness_tsd) {\n\treturn (witness_tsdn_t *)witness_tsd;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\nwitness_tsdn_null(witness_tsdn_t *witness_tsdn) {\n\treturn witness_tsdn == NULL;\n}\n\nJEMALLOC_ALWAYS_INLINE witness_tsd_t *\nwitness_tsdn_tsd(witness_tsdn_t *witness_tsdn) {\n\tassert(!witness_tsdn_null(witness_tsdn));\n\treturn &witness_tsdn->witness_tsd;\n}\n\n/******************************************************************************/\n/* API */\n/******************************************************************************/\nvoid witness_init(witness_t *witness, const char *name, witness_rank_t rank,\n    witness_comp_t *comp, void *opaque);\n\ntypedef void (witness_lock_error_t)(const witness_list_t *, const witness_t *);\nextern witness_lock_error_t *JET_MUTABLE witness_lock_error;\n\ntypedef void (witness_owner_error_t)(const witness_t *);\nextern witness_owner_error_t *JET_MUTABLE witness_owner_error;\n\ntypedef void (witness_not_owner_error_t)(const witness_t *);\nextern witness_not_owner_error_t *JET_MUTABLE witness_not_owner_error;\n\ntypedef void (witness_depth_error_t)(const witness_list_t *,\n    witness_rank_t rank_inclusive, unsigned depth);\nextern witness_depth_error_t *JET_MUTABLE witness_depth_error;\n\nvoid witnesses_cleanup(witness_tsd_t *witness_tsd);\nvoid witness_prefork(witness_tsd_t *witness_tsd);\nvoid witness_postfork_parent(witness_tsd_t *witness_tsd);\nvoid witness_postfork_child(witness_tsd_t *witness_tsd);\n\n/* Helper, not intended for direct use. */\nstatic inline bool\nwitness_owner(witness_tsd_t *witness_tsd, const witness_t *witness) {\n\twitness_list_t *witnesses;\n\twitness_t *w;\n\n\tcassert(config_debug);\n\n\twitnesses = &witness_tsd->witnesses;\n\tql_foreach(w, witnesses, link) {\n\t\tif (w == witness) {\n\t\t\treturn true;\n\t\t}\n\t}\n\n\treturn false;\n}\n\nstatic inline void\nwitness_assert_owner(witness_tsdn_t *witness_tsdn, const witness_t *witness) {\n\twitness_tsd_t *witness_tsd;\n\n\tif (!config_debug) {\n\t\treturn;\n\t}\n\n\tif (witness_tsdn_null(witness_tsdn)) {\n\t\treturn;\n\t}\n\twitness_tsd = witness_tsdn_tsd(witness_tsdn);\n\tif (witness->rank == WITNESS_RANK_OMIT) {\n\t\treturn;\n\t}\n\n\tif (witness_owner(witness_tsd, witness)) {\n\t\treturn;\n\t}\n\twitness_owner_error(witness);\n}\n\nstatic inline void\nwitness_assert_not_owner(witness_tsdn_t *witness_tsdn,\n    const witness_t *witness) {\n\twitness_tsd_t *witness_tsd;\n\twitness_list_t *witnesses;\n\twitness_t *w;\n\n\tif (!config_debug) {\n\t\treturn;\n\t}\n\n\tif (witness_tsdn_null(witness_tsdn)) {\n\t\treturn;\n\t}\n\twitness_tsd = witness_tsdn_tsd(witness_tsdn);\n\tif (witness->rank == WITNESS_RANK_OMIT) {\n\t\treturn;\n\t}\n\n\twitnesses = &witness_tsd->witnesses;\n\tql_foreach(w, witnesses, link) {\n\t\tif (w == witness) {\n\t\t\twitness_not_owner_error(witness);\n\t\t}\n\t}\n}\n\nstatic inline void\nwitness_assert_depth_to_rank(witness_tsdn_t *witness_tsdn,\n    witness_rank_t rank_inclusive, unsigned depth) {\n\twitness_tsd_t *witness_tsd;\n\tunsigned d;\n\twitness_list_t *witnesses;\n\twitness_t *w;\n\n\tif (!config_debug) {\n\t\treturn;\n\t}\n\n\tif (witness_tsdn_null(witness_tsdn)) {\n\t\treturn;\n\t}\n\twitness_tsd = witness_tsdn_tsd(witness_tsdn);\n\n\td = 0;\n\twitnesses = &witness_tsd->witnesses;\n\tw = ql_last(witnesses, link);\n\tif (w != NULL) {\n\t\tql_reverse_foreach(w, witnesses, link) {\n\t\t\tif (w->rank < rank_inclusive) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\td++;\n\t\t}\n\t}\n\tif (d != depth) {\n\t\twitness_depth_error(witnesses, rank_inclusive, depth);\n\t}\n}\n\nstatic inline void\nwitness_assert_depth(witness_tsdn_t *witness_tsdn, unsigned depth) {\n\twitness_assert_depth_to_rank(witness_tsdn, WITNESS_RANK_MIN, depth);\n}\n\nstatic inline void\nwitness_assert_lockless(witness_tsdn_t *witness_tsdn) {\n\twitness_assert_depth(witness_tsdn, 0);\n}\n\nstatic inline void\nwitness_lock(witness_tsdn_t *witness_tsdn, witness_t *witness) {\n\twitness_tsd_t *witness_tsd;\n\twitness_list_t *witnesses;\n\twitness_t *w;\n\n\tif (!config_debug) {\n\t\treturn;\n\t}\n\n\tif (witness_tsdn_null(witness_tsdn)) {\n\t\treturn;\n\t}\n\twitness_tsd = witness_tsdn_tsd(witness_tsdn);\n\tif (witness->rank == WITNESS_RANK_OMIT) {\n\t\treturn;\n\t}\n\n\twitness_assert_not_owner(witness_tsdn, witness);\n\n\twitnesses = &witness_tsd->witnesses;\n\tw = ql_last(witnesses, link);\n\tif (w == NULL) {\n\t\t/* No other locks; do nothing. */\n\t} else if (witness_tsd->forking && w->rank <= witness->rank) {\n\t\t/* Forking, and relaxed ranking satisfied. */\n\t} else if (w->rank > witness->rank) {\n\t\t/* Not forking, rank order reversal. */\n\t\twitness_lock_error(witnesses, witness);\n\t} else if (w->rank == witness->rank && (w->comp == NULL || w->comp !=\n\t    witness->comp || w->comp(w, w->opaque, witness, witness->opaque) >\n\t    0)) {\n\t\t/*\n\t\t * Missing/incompatible comparison function, or comparison\n\t\t * function indicates rank order reversal.\n\t\t */\n\t\twitness_lock_error(witnesses, witness);\n\t}\n\n\tql_elm_new(witness, link);\n\tql_tail_insert(witnesses, witness, link);\n}\n\nstatic inline void\nwitness_unlock(witness_tsdn_t *witness_tsdn, witness_t *witness) {\n\twitness_tsd_t *witness_tsd;\n\twitness_list_t *witnesses;\n\n\tif (!config_debug) {\n\t\treturn;\n\t}\n\n\tif (witness_tsdn_null(witness_tsdn)) {\n\t\treturn;\n\t}\n\twitness_tsd = witness_tsdn_tsd(witness_tsdn);\n\tif (witness->rank == WITNESS_RANK_OMIT) {\n\t\treturn;\n\t}\n\n\t/*\n\t * Check whether owner before removal, rather than relying on\n\t * witness_assert_owner() to abort, so that unit tests can test this\n\t * function's failure mode without causing undefined behavior.\n\t */\n\tif (witness_owner(witness_tsd, witness)) {\n\t\twitnesses = &witness_tsd->witnesses;\n\t\tql_remove(witnesses, witness, link);\n\t} else {\n\t\twitness_assert_owner(witness_tsdn, witness);\n\t}\n}\n\n#endif /* JEMALLOC_INTERNAL_WITNESS_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/jemalloc.sh",
    "content": "#!/bin/sh\n\nobjroot=$1\n\ncat <<EOF\n#ifndef JEMALLOC_H_\n#define JEMALLOC_H_\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\nEOF\n\nfor hdr in jemalloc_defs.h jemalloc_rename.h jemalloc_macros.h \\\n           jemalloc_protos.h jemalloc_typedefs.h jemalloc_mangle.h ; do\n  cat \"${objroot}include/jemalloc/${hdr}\" \\\n      | grep -v 'Generated from .* by configure\\.' \\\n      | sed -e 's/ $//g'\n  echo\ndone\n\ncat <<EOF\n#ifdef __cplusplus\n}\n#endif\n#endif /* JEMALLOC_H_ */\nEOF\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/jemalloc_defs.h.in",
    "content": "/* Defined if __attribute__((...)) syntax is supported. */\n#undef JEMALLOC_HAVE_ATTR\n\n/* Defined if alloc_size attribute is supported. */\n#undef JEMALLOC_HAVE_ATTR_ALLOC_SIZE\n\n/* Defined if format(gnu_printf, ...) attribute is supported. */\n#undef JEMALLOC_HAVE_ATTR_FORMAT_GNU_PRINTF\n\n/* Defined if format(printf, ...) attribute is supported. */\n#undef JEMALLOC_HAVE_ATTR_FORMAT_PRINTF\n\n/*\n * Define overrides for non-standard allocator-related functions if they are\n * present on the system.\n */\n#undef JEMALLOC_OVERRIDE_MEMALIGN\n#undef JEMALLOC_OVERRIDE_VALLOC\n\n/*\n * At least Linux omits the \"const\" in:\n *\n *   size_t malloc_usable_size(const void *ptr);\n *\n * Match the operating system's prototype.\n */\n#undef JEMALLOC_USABLE_SIZE_CONST\n\n/*\n * If defined, specify throw() for the public function prototypes when compiling\n * with C++.  The only justification for this is to match the prototypes that\n * glibc defines.\n */\n#undef JEMALLOC_USE_CXX_THROW\n\n#ifdef _MSC_VER\n#  ifdef _WIN64\n#    define LG_SIZEOF_PTR_WIN 3\n#  else\n#    define LG_SIZEOF_PTR_WIN 2\n#  endif\n#endif\n\n/* sizeof(void *) == 2^LG_SIZEOF_PTR. */\n#undef LG_SIZEOF_PTR\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/jemalloc_macros.h.in",
    "content": "#include <stdlib.h>\n#include <stdbool.h>\n#include <stdint.h>\n#include <limits.h>\n#include <strings.h>\n\n#define JEMALLOC_VERSION \"@jemalloc_version@\"\n#define JEMALLOC_VERSION_MAJOR @jemalloc_version_major@\n#define JEMALLOC_VERSION_MINOR @jemalloc_version_minor@\n#define JEMALLOC_VERSION_BUGFIX @jemalloc_version_bugfix@\n#define JEMALLOC_VERSION_NREV @jemalloc_version_nrev@\n#define JEMALLOC_VERSION_GID \"@jemalloc_version_gid@\"\n\n#define MALLOCX_LG_ALIGN(la)\t((int)(la))\n#if LG_SIZEOF_PTR == 2\n#  define MALLOCX_ALIGN(a)\t((int)(ffs((int)(a))-1))\n#else\n#  define MALLOCX_ALIGN(a)\t\t\t\t\t\t\\\n     ((int)(((size_t)(a) < (size_t)INT_MAX) ? ffs((int)(a))-1 :\t\\\n     ffs((int)(((size_t)(a))>>32))+31))\n#endif\n#define MALLOCX_ZERO\t((int)0x40)\n/*\n * Bias tcache index bits so that 0 encodes \"automatic tcache management\", and 1\n * encodes MALLOCX_TCACHE_NONE.\n */\n#define MALLOCX_TCACHE(tc)\t((int)(((tc)+2) << 8))\n#define MALLOCX_TCACHE_NONE\tMALLOCX_TCACHE(-1)\n/*\n * Bias arena index bits so that 0 encodes \"use an automatically chosen arena\".\n */\n#define MALLOCX_ARENA(a)\t((((int)(a))+1) << 20)\n\n/*\n * Use as arena index in \"arena.<i>.{purge,decay,dss}\" and\n * \"stats.arenas.<i>.*\" mallctl interfaces to select all arenas.  This\n * definition is intentionally specified in raw decimal format to support\n * cpp-based string concatenation, e.g.\n *\n *   #define STRINGIFY_HELPER(x) #x\n *   #define STRINGIFY(x) STRINGIFY_HELPER(x)\n *\n *   mallctl(\"arena.\" STRINGIFY(MALLCTL_ARENAS_ALL) \".purge\", NULL, NULL, NULL,\n *       0);\n */\n#define MALLCTL_ARENAS_ALL\t4096\n/*\n * Use as arena index in \"stats.arenas.<i>.*\" mallctl interfaces to select\n * destroyed arenas.\n */\n#define MALLCTL_ARENAS_DESTROYED\t4097\n\n#if defined(__cplusplus) && defined(JEMALLOC_USE_CXX_THROW)\n#  define JEMALLOC_CXX_THROW throw()\n#else\n#  define JEMALLOC_CXX_THROW\n#endif\n\n#if defined(_MSC_VER)\n#  define JEMALLOC_ATTR(s)\n#  define JEMALLOC_ALIGNED(s) __declspec(align(s))\n#  define JEMALLOC_ALLOC_SIZE(s)\n#  define JEMALLOC_ALLOC_SIZE2(s1, s2)\n#  ifndef JEMALLOC_EXPORT\n#    ifdef DLLEXPORT\n#      define JEMALLOC_EXPORT __declspec(dllexport)\n#    else\n#      define JEMALLOC_EXPORT __declspec(dllimport)\n#    endif\n#  endif\n#  define JEMALLOC_FORMAT_PRINTF(s, i)\n#  define JEMALLOC_NOINLINE __declspec(noinline)\n#  ifdef __cplusplus\n#    define JEMALLOC_NOTHROW __declspec(nothrow)\n#  else\n#    define JEMALLOC_NOTHROW\n#  endif\n#  define JEMALLOC_SECTION(s) __declspec(allocate(s))\n#  define JEMALLOC_RESTRICT_RETURN __declspec(restrict)\n#  if _MSC_VER >= 1900 && !defined(__EDG__)\n#    define JEMALLOC_ALLOCATOR __declspec(allocator)\n#  else\n#    define JEMALLOC_ALLOCATOR\n#  endif\n#elif defined(JEMALLOC_HAVE_ATTR)\n#  define JEMALLOC_ATTR(s) __attribute__((s))\n#  define JEMALLOC_ALIGNED(s) JEMALLOC_ATTR(aligned(s))\n#  ifdef JEMALLOC_HAVE_ATTR_ALLOC_SIZE\n#    define JEMALLOC_ALLOC_SIZE(s) JEMALLOC_ATTR(alloc_size(s))\n#    define JEMALLOC_ALLOC_SIZE2(s1, s2) JEMALLOC_ATTR(alloc_size(s1, s2))\n#  else\n#    define JEMALLOC_ALLOC_SIZE(s)\n#    define JEMALLOC_ALLOC_SIZE2(s1, s2)\n#  endif\n#  ifndef JEMALLOC_EXPORT\n#    define JEMALLOC_EXPORT JEMALLOC_ATTR(visibility(\"default\"))\n#  endif\n#  ifdef JEMALLOC_HAVE_ATTR_FORMAT_GNU_PRINTF\n#    define JEMALLOC_FORMAT_PRINTF(s, i) JEMALLOC_ATTR(format(gnu_printf, s, i))\n#  elif defined(JEMALLOC_HAVE_ATTR_FORMAT_PRINTF)\n#    define JEMALLOC_FORMAT_PRINTF(s, i) JEMALLOC_ATTR(format(printf, s, i))\n#  else\n#    define JEMALLOC_FORMAT_PRINTF(s, i)\n#  endif\n#  define JEMALLOC_NOINLINE JEMALLOC_ATTR(noinline)\n#  define JEMALLOC_NOTHROW JEMALLOC_ATTR(nothrow)\n#  define JEMALLOC_SECTION(s) JEMALLOC_ATTR(section(s))\n#  define JEMALLOC_RESTRICT_RETURN\n#  define JEMALLOC_ALLOCATOR\n#else\n#  define JEMALLOC_ATTR(s)\n#  define JEMALLOC_ALIGNED(s)\n#  define JEMALLOC_ALLOC_SIZE(s)\n#  define JEMALLOC_ALLOC_SIZE2(s1, s2)\n#  define JEMALLOC_EXPORT\n#  define JEMALLOC_FORMAT_PRINTF(s, i)\n#  define JEMALLOC_NOINLINE\n#  define JEMALLOC_NOTHROW\n#  define JEMALLOC_SECTION(s)\n#  define JEMALLOC_RESTRICT_RETURN\n#  define JEMALLOC_ALLOCATOR\n#endif\n\n\n/* This version of Jemalloc, modified for Redis, has the je_get_defrag_hint()\n * function. */\n#define JEMALLOC_FRAG_HINT\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/jemalloc_mangle.sh",
    "content": "#!/bin/sh\n\npublic_symbols_txt=$1\nsymbol_prefix=$2\n\ncat <<EOF\n/*\n * By default application code must explicitly refer to mangled symbol names,\n * so that it is possible to use jemalloc in conjunction with another allocator\n * in the same application.  Define JEMALLOC_MANGLE in order to cause automatic\n * name mangling that matches the API prefixing that happened as a result of\n * --with-mangling and/or --with-jemalloc-prefix configuration settings.\n */\n#ifdef JEMALLOC_MANGLE\n#  ifndef JEMALLOC_NO_DEMANGLE\n#    define JEMALLOC_NO_DEMANGLE\n#  endif\nEOF\n\nfor nm in `cat ${public_symbols_txt}` ; do\n  n=`echo ${nm} |tr ':' ' ' |awk '{print $1}'`\n  echo \"#  define ${n} ${symbol_prefix}${n}\"\ndone\n\ncat <<EOF\n#endif\n\n/*\n * The ${symbol_prefix}* macros can be used as stable alternative names for the\n * public jemalloc API if JEMALLOC_NO_DEMANGLE is defined.  This is primarily\n * meant for use in jemalloc itself, but it can be used by application code to\n * provide isolation from the name mangling specified via --with-mangling\n * and/or --with-jemalloc-prefix.\n */\n#ifndef JEMALLOC_NO_DEMANGLE\nEOF\n\nfor nm in `cat ${public_symbols_txt}` ; do\n  n=`echo ${nm} |tr ':' ' ' |awk '{print $1}'`\n  echo \"#  undef ${symbol_prefix}${n}\"\ndone\n\ncat <<EOF\n#endif\nEOF\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/jemalloc_protos.h.in",
    "content": "/*\n * The @je_@ prefix on the following public symbol declarations is an artifact\n * of namespace management, and should be omitted in application code unless\n * JEMALLOC_NO_DEMANGLE is defined (see jemalloc_mangle@install_suffix@.h).\n */\nextern JEMALLOC_EXPORT const char\t*@je_@malloc_conf;\nextern JEMALLOC_EXPORT void\t\t(*@je_@malloc_message)(void *cbopaque,\n    const char *s);\n\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\n    void JEMALLOC_NOTHROW\t*@je_@malloc(size_t size)\n    JEMALLOC_CXX_THROW JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE(1);\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\n    void JEMALLOC_NOTHROW\t*@je_@calloc(size_t num, size_t size)\n    JEMALLOC_CXX_THROW JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE2(1, 2);\nJEMALLOC_EXPORT int JEMALLOC_NOTHROW\t@je_@posix_memalign(void **memptr,\n    size_t alignment, size_t size) JEMALLOC_CXX_THROW JEMALLOC_ATTR(nonnull(1));\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\n    void JEMALLOC_NOTHROW\t*@je_@aligned_alloc(size_t alignment,\n    size_t size) JEMALLOC_CXX_THROW JEMALLOC_ATTR(malloc)\n    JEMALLOC_ALLOC_SIZE(2);\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\n    void JEMALLOC_NOTHROW\t*@je_@realloc(void *ptr, size_t size)\n    JEMALLOC_CXX_THROW JEMALLOC_ALLOC_SIZE(2);\nJEMALLOC_EXPORT void JEMALLOC_NOTHROW\t@je_@free(void *ptr)\n    JEMALLOC_CXX_THROW;\n\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\n    void JEMALLOC_NOTHROW\t*@je_@mallocx(size_t size, int flags)\n    JEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE(1);\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\n    void JEMALLOC_NOTHROW\t*@je_@rallocx(void *ptr, size_t size,\n    int flags) JEMALLOC_ALLOC_SIZE(2);\nJEMALLOC_EXPORT size_t JEMALLOC_NOTHROW\t@je_@xallocx(void *ptr, size_t size,\n    size_t extra, int flags);\nJEMALLOC_EXPORT size_t JEMALLOC_NOTHROW\t@je_@sallocx(const void *ptr,\n    int flags) JEMALLOC_ATTR(pure);\nJEMALLOC_EXPORT void JEMALLOC_NOTHROW\t@je_@dallocx(void *ptr, int flags);\nJEMALLOC_EXPORT void JEMALLOC_NOTHROW\t@je_@sdallocx(void *ptr, size_t size,\n    int flags);\nJEMALLOC_EXPORT size_t JEMALLOC_NOTHROW\t@je_@nallocx(size_t size, int flags)\n    JEMALLOC_ATTR(pure);\n\nJEMALLOC_EXPORT int JEMALLOC_NOTHROW\t@je_@mallctl(const char *name,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen);\nJEMALLOC_EXPORT int JEMALLOC_NOTHROW\t@je_@mallctlnametomib(const char *name,\n    size_t *mibp, size_t *miblenp);\nJEMALLOC_EXPORT int JEMALLOC_NOTHROW\t@je_@mallctlbymib(const size_t *mib,\n    size_t miblen, void *oldp, size_t *oldlenp, void *newp, size_t newlen);\nJEMALLOC_EXPORT void JEMALLOC_NOTHROW\t@je_@malloc_stats_print(\n    void (*write_cb)(void *, const char *), void *@je_@cbopaque,\n    const char *opts);\nJEMALLOC_EXPORT size_t JEMALLOC_NOTHROW\t@je_@malloc_usable_size(\n    JEMALLOC_USABLE_SIZE_CONST void *ptr) JEMALLOC_CXX_THROW;\n\n#ifdef JEMALLOC_OVERRIDE_MEMALIGN\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\n    void JEMALLOC_NOTHROW\t*@je_@memalign(size_t alignment, size_t size)\n    JEMALLOC_CXX_THROW JEMALLOC_ATTR(malloc);\n#endif\n\n#ifdef JEMALLOC_OVERRIDE_VALLOC\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\n    void JEMALLOC_NOTHROW\t*@je_@valloc(size_t size) JEMALLOC_CXX_THROW\n    JEMALLOC_ATTR(malloc);\n#endif\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/jemalloc_rename.sh",
    "content": "#!/bin/sh\n\npublic_symbols_txt=$1\n\ncat <<EOF\n/*\n * Name mangling for public symbols is controlled by --with-mangling and\n * --with-jemalloc-prefix.  With default settings the je_ prefix is stripped by\n * these macro definitions.\n */\n#ifndef JEMALLOC_NO_RENAME\nEOF\n\nfor nm in `cat ${public_symbols_txt}` ; do\n  n=`echo ${nm} |tr ':' ' ' |awk '{print $1}'`\n  m=`echo ${nm} |tr ':' ' ' |awk '{print $2}'`\n  echo \"#  define je_${n} ${m}\"\ndone\n\ncat <<EOF\n#endif\nEOF\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/jemalloc/jemalloc_typedefs.h.in",
    "content": "typedef struct extent_hooks_s extent_hooks_t;\n\n/*\n * void *\n * extent_alloc(extent_hooks_t *extent_hooks, void *new_addr, size_t size,\n *     size_t alignment, bool *zero, bool *commit, unsigned arena_ind);\n */\ntypedef void *(extent_alloc_t)(extent_hooks_t *, void *, size_t, size_t, bool *,\n    bool *, unsigned);\n\n/*\n * bool\n * extent_dalloc(extent_hooks_t *extent_hooks, void *addr, size_t size,\n *     bool committed, unsigned arena_ind);\n */\ntypedef bool (extent_dalloc_t)(extent_hooks_t *, void *, size_t, bool,\n    unsigned);\n\n/*\n * void\n * extent_destroy(extent_hooks_t *extent_hooks, void *addr, size_t size,\n *     bool committed, unsigned arena_ind);\n */\ntypedef void (extent_destroy_t)(extent_hooks_t *, void *, size_t, bool,\n    unsigned);\n\n/*\n * bool\n * extent_commit(extent_hooks_t *extent_hooks, void *addr, size_t size,\n *     size_t offset, size_t length, unsigned arena_ind);\n */\ntypedef bool (extent_commit_t)(extent_hooks_t *, void *, size_t, size_t, size_t,\n    unsigned);\n\n/*\n * bool\n * extent_decommit(extent_hooks_t *extent_hooks, void *addr, size_t size,\n *     size_t offset, size_t length, unsigned arena_ind);\n */\ntypedef bool (extent_decommit_t)(extent_hooks_t *, void *, size_t, size_t,\n    size_t, unsigned);\n\n/*\n * bool\n * extent_purge(extent_hooks_t *extent_hooks, void *addr, size_t size,\n *     size_t offset, size_t length, unsigned arena_ind);\n */\ntypedef bool (extent_purge_t)(extent_hooks_t *, void *, size_t, size_t, size_t,\n    unsigned);\n\n/*\n * bool\n * extent_split(extent_hooks_t *extent_hooks, void *addr, size_t size,\n *     size_t size_a, size_t size_b, bool committed, unsigned arena_ind);\n */\ntypedef bool (extent_split_t)(extent_hooks_t *, void *, size_t, size_t, size_t,\n    bool, unsigned);\n\n/*\n * bool\n * extent_merge(extent_hooks_t *extent_hooks, void *addr_a, size_t size_a,\n *     void *addr_b, size_t size_b, bool committed, unsigned arena_ind);\n */\ntypedef bool (extent_merge_t)(extent_hooks_t *, void *, size_t, void *, size_t,\n    bool, unsigned);\n\nstruct extent_hooks_s {\n\textent_alloc_t\t\t*alloc;\n\textent_dalloc_t\t\t*dalloc;\n\textent_destroy_t\t*destroy;\n\textent_commit_t\t\t*commit;\n\textent_decommit_t\t*decommit;\n\textent_purge_t\t\t*purge_lazy;\n\textent_purge_t\t\t*purge_forced;\n\textent_split_t\t\t*split;\n\textent_merge_t\t\t*merge;\n};\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/msvc_compat/C99/stdbool.h",
    "content": "#ifndef stdbool_h\n#define stdbool_h\n\n#include <wtypes.h>\n\n/* MSVC doesn't define _Bool or bool in C, but does have BOOL */\n/* Note this doesn't pass autoconf's test because (bool) 0.5 != true */\n/* Clang-cl uses MSVC headers, so needs msvc_compat, but has _Bool as\n * a built-in type. */\n#ifndef __clang__\ntypedef BOOL _Bool;\n#endif\n\n#define bool _Bool\n#define true 1\n#define false 0\n\n#define __bool_true_false_are_defined 1\n\n#endif /* stdbool_h */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/msvc_compat/C99/stdint.h",
    "content": "// ISO C9x  compliant stdint.h for Microsoft Visual Studio\n// Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124 \n// \n//  Copyright (c) 2006-2008 Alexander Chemeris\n// \n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are met:\n// \n//   1. Redistributions of source code must retain the above copyright notice,\n//      this list of conditions and the following disclaimer.\n// \n//   2. Redistributions in binary form must reproduce the above copyright\n//      notice, this list of conditions and the following disclaimer in the\n//      documentation and/or other materials provided with the distribution.\n// \n//   3. The name of the author may be used to endorse or promote products\n//      derived from this software without specific prior written permission.\n// \n// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED\n// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO\n// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\n// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;\n// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, \n// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR\n// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n// \n///////////////////////////////////////////////////////////////////////////////\n\n#ifndef _MSC_VER // [\n#error \"Use this header only with Microsoft Visual C++ compilers!\"\n#endif // _MSC_VER ]\n\n#ifndef _MSC_STDINT_H_ // [\n#define _MSC_STDINT_H_\n\n#if _MSC_VER > 1000\n#pragma once\n#endif\n\n#include <limits.h>\n\n// For Visual Studio 6 in C++ mode and for many Visual Studio versions when\n// compiling for ARM we should wrap <wchar.h> include with 'extern \"C++\" {}'\n// or compiler give many errors like this:\n//   error C2733: second C linkage of overloaded function 'wmemchr' not allowed\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n#  include <wchar.h>\n#ifdef __cplusplus\n}\n#endif\n\n// Define _W64 macros to mark types changing their size, like intptr_t.\n#ifndef _W64\n#  if !defined(__midl) && (defined(_X86_) || defined(_M_IX86)) && _MSC_VER >= 1300\n#     define _W64 __w64\n#  else\n#     define _W64\n#  endif\n#endif\n\n\n// 7.18.1 Integer types\n\n// 7.18.1.1 Exact-width integer types\n\n// Visual Studio 6 and Embedded Visual C++ 4 doesn't\n// realize that, e.g. char has the same size as __int8\n// so we give up on __intX for them.\n#if (_MSC_VER < 1300)\n   typedef signed char       int8_t;\n   typedef signed short      int16_t;\n   typedef signed int        int32_t;\n   typedef unsigned char     uint8_t;\n   typedef unsigned short    uint16_t;\n   typedef unsigned int      uint32_t;\n#else\n   typedef signed __int8     int8_t;\n   typedef signed __int16    int16_t;\n   typedef signed __int32    int32_t;\n   typedef unsigned __int8   uint8_t;\n   typedef unsigned __int16  uint16_t;\n   typedef unsigned __int32  uint32_t;\n#endif\ntypedef signed __int64       int64_t;\ntypedef unsigned __int64     uint64_t;\n\n\n// 7.18.1.2 Minimum-width integer types\ntypedef int8_t    int_least8_t;\ntypedef int16_t   int_least16_t;\ntypedef int32_t   int_least32_t;\ntypedef int64_t   int_least64_t;\ntypedef uint8_t   uint_least8_t;\ntypedef uint16_t  uint_least16_t;\ntypedef uint32_t  uint_least32_t;\ntypedef uint64_t  uint_least64_t;\n\n// 7.18.1.3 Fastest minimum-width integer types\ntypedef int8_t    int_fast8_t;\ntypedef int16_t   int_fast16_t;\ntypedef int32_t   int_fast32_t;\ntypedef int64_t   int_fast64_t;\ntypedef uint8_t   uint_fast8_t;\ntypedef uint16_t  uint_fast16_t;\ntypedef uint32_t  uint_fast32_t;\ntypedef uint64_t  uint_fast64_t;\n\n// 7.18.1.4 Integer types capable of holding object pointers\n#ifdef _WIN64 // [\n   typedef signed __int64    intptr_t;\n   typedef unsigned __int64  uintptr_t;\n#else // _WIN64 ][\n   typedef _W64 signed int   intptr_t;\n   typedef _W64 unsigned int uintptr_t;\n#endif // _WIN64 ]\n\n// 7.18.1.5 Greatest-width integer types\ntypedef int64_t   intmax_t;\ntypedef uint64_t  uintmax_t;\n\n\n// 7.18.2 Limits of specified-width integer types\n\n#if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS) // [   See footnote 220 at page 257 and footnote 221 at page 259\n\n// 7.18.2.1 Limits of exact-width integer types\n#define INT8_MIN     ((int8_t)_I8_MIN)\n#define INT8_MAX     _I8_MAX\n#define INT16_MIN    ((int16_t)_I16_MIN)\n#define INT16_MAX    _I16_MAX\n#define INT32_MIN    ((int32_t)_I32_MIN)\n#define INT32_MAX    _I32_MAX\n#define INT64_MIN    ((int64_t)_I64_MIN)\n#define INT64_MAX    _I64_MAX\n#define UINT8_MAX    _UI8_MAX\n#define UINT16_MAX   _UI16_MAX\n#define UINT32_MAX   _UI32_MAX\n#define UINT64_MAX   _UI64_MAX\n\n// 7.18.2.2 Limits of minimum-width integer types\n#define INT_LEAST8_MIN    INT8_MIN\n#define INT_LEAST8_MAX    INT8_MAX\n#define INT_LEAST16_MIN   INT16_MIN\n#define INT_LEAST16_MAX   INT16_MAX\n#define INT_LEAST32_MIN   INT32_MIN\n#define INT_LEAST32_MAX   INT32_MAX\n#define INT_LEAST64_MIN   INT64_MIN\n#define INT_LEAST64_MAX   INT64_MAX\n#define UINT_LEAST8_MAX   UINT8_MAX\n#define UINT_LEAST16_MAX  UINT16_MAX\n#define UINT_LEAST32_MAX  UINT32_MAX\n#define UINT_LEAST64_MAX  UINT64_MAX\n\n// 7.18.2.3 Limits of fastest minimum-width integer types\n#define INT_FAST8_MIN    INT8_MIN\n#define INT_FAST8_MAX    INT8_MAX\n#define INT_FAST16_MIN   INT16_MIN\n#define INT_FAST16_MAX   INT16_MAX\n#define INT_FAST32_MIN   INT32_MIN\n#define INT_FAST32_MAX   INT32_MAX\n#define INT_FAST64_MIN   INT64_MIN\n#define INT_FAST64_MAX   INT64_MAX\n#define UINT_FAST8_MAX   UINT8_MAX\n#define UINT_FAST16_MAX  UINT16_MAX\n#define UINT_FAST32_MAX  UINT32_MAX\n#define UINT_FAST64_MAX  UINT64_MAX\n\n// 7.18.2.4 Limits of integer types capable of holding object pointers\n#ifdef _WIN64 // [\n#  define INTPTR_MIN   INT64_MIN\n#  define INTPTR_MAX   INT64_MAX\n#  define UINTPTR_MAX  UINT64_MAX\n#else // _WIN64 ][\n#  define INTPTR_MIN   INT32_MIN\n#  define INTPTR_MAX   INT32_MAX\n#  define UINTPTR_MAX  UINT32_MAX\n#endif // _WIN64 ]\n\n// 7.18.2.5 Limits of greatest-width integer types\n#define INTMAX_MIN   INT64_MIN\n#define INTMAX_MAX   INT64_MAX\n#define UINTMAX_MAX  UINT64_MAX\n\n// 7.18.3 Limits of other integer types\n\n#ifdef _WIN64 // [\n#  define PTRDIFF_MIN  _I64_MIN\n#  define PTRDIFF_MAX  _I64_MAX\n#else  // _WIN64 ][\n#  define PTRDIFF_MIN  _I32_MIN\n#  define PTRDIFF_MAX  _I32_MAX\n#endif  // _WIN64 ]\n\n#define SIG_ATOMIC_MIN  INT_MIN\n#define SIG_ATOMIC_MAX  INT_MAX\n\n#ifndef SIZE_MAX // [\n#  ifdef _WIN64 // [\n#     define SIZE_MAX  _UI64_MAX\n#  else // _WIN64 ][\n#     define SIZE_MAX  _UI32_MAX\n#  endif // _WIN64 ]\n#endif // SIZE_MAX ]\n\n// WCHAR_MIN and WCHAR_MAX are also defined in <wchar.h>\n#ifndef WCHAR_MIN // [\n#  define WCHAR_MIN  0\n#endif  // WCHAR_MIN ]\n#ifndef WCHAR_MAX // [\n#  define WCHAR_MAX  _UI16_MAX\n#endif  // WCHAR_MAX ]\n\n#define WINT_MIN  0\n#define WINT_MAX  _UI16_MAX\n\n#endif // __STDC_LIMIT_MACROS ]\n\n\n// 7.18.4 Limits of other integer types\n\n#if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) // [   See footnote 224 at page 260\n\n// 7.18.4.1 Macros for minimum-width integer constants\n\n#define INT8_C(val)  val##i8\n#define INT16_C(val) val##i16\n#define INT32_C(val) val##i32\n#define INT64_C(val) val##i64\n\n#define UINT8_C(val)  val##ui8\n#define UINT16_C(val) val##ui16\n#define UINT32_C(val) val##ui32\n#define UINT64_C(val) val##ui64\n\n// 7.18.4.2 Macros for greatest-width integer constants\n#define INTMAX_C   INT64_C\n#define UINTMAX_C  UINT64_C\n\n#endif // __STDC_CONSTANT_MACROS ]\n\n\n#endif // _MSC_STDINT_H_ ]\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/msvc_compat/strings.h",
    "content": "#ifndef strings_h\n#define strings_h\n\n/* MSVC doesn't define ffs/ffsl. This dummy strings.h header is provided\n * for both */\n#ifdef _MSC_VER\n#  include <intrin.h>\n#  pragma intrinsic(_BitScanForward)\nstatic __forceinline int ffsl(long x) {\n\tunsigned long i;\n\n\tif (_BitScanForward(&i, x)) {\n\t\treturn i + 1;\n\t}\n\treturn 0;\n}\n\nstatic __forceinline int ffs(int x) {\n\treturn ffsl(x);\n}\n\n#  ifdef  _M_X64\n#    pragma intrinsic(_BitScanForward64)\n#  endif\n\nstatic __forceinline int ffsll(unsigned __int64 x) {\n\tunsigned long i;\n#ifdef  _M_X64\n\tif (_BitScanForward64(&i, x)) {\n\t\treturn i + 1;\n\t}\n\treturn 0;\n#else\n// Fallback for 32-bit build where 64-bit version not available\n// assuming little endian\n\tunion {\n\t\tunsigned __int64 ll;\n\t\tunsigned   long l[2];\n\t} s;\n\n\ts.ll = x;\n\n\tif (_BitScanForward(&i, s.l[0])) {\n\t\treturn i + 1;\n\t} else if(_BitScanForward(&i, s.l[1])) {\n\t\treturn i + 33;\n\t}\n\treturn 0;\n#endif\n}\n\n#else\n#  define ffsll(x) __builtin_ffsll(x)\n#  define ffsl(x) __builtin_ffsl(x)\n#  define ffs(x) __builtin_ffs(x)\n#endif\n\n#endif /* strings_h */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/include/msvc_compat/windows_extra.h",
    "content": "#ifndef MSVC_COMPAT_WINDOWS_EXTRA_H\n#define MSVC_COMPAT_WINDOWS_EXTRA_H\n\n#include <errno.h>\n\n#endif /* MSVC_COMPAT_WINDOWS_EXTRA_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/jemalloc.pc.in",
    "content": "prefix=@prefix@\nexec_prefix=@exec_prefix@\nlibdir=@libdir@\nincludedir=@includedir@\ninstall_suffix=@install_suffix@\n\nName: jemalloc\nDescription: A general purpose malloc(3) implementation that emphasizes fragmentation avoidance and scalable concurrency support.\nURL: http://jemalloc.net/\nVersion: @jemalloc_version@\nCflags: -I${includedir}\nLibs: -L${libdir} -ljemalloc${install_suffix}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/m4/ax_cxx_compile_stdcxx.m4",
    "content": "# ===========================================================================\n#   http://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx.html\n# ===========================================================================\n#\n# SYNOPSIS\n#\n#   AX_CXX_COMPILE_STDCXX(VERSION, [ext|noext], [mandatory|optional])\n#\n# DESCRIPTION\n#\n#   Check for baseline language coverage in the compiler for the specified\n#   version of the C++ standard.  If necessary, add switches to CXX and\n#   CXXCPP to enable support.  VERSION may be '11' (for the C++11 standard)\n#   or '14' (for the C++14 standard).\n#\n#   The second argument, if specified, indicates whether you insist on an\n#   extended mode (e.g. -std=gnu++11) or a strict conformance mode (e.g.\n#   -std=c++11).  If neither is specified, you get whatever works, with\n#   preference for an extended mode.\n#\n#   The third argument, if specified 'mandatory' or if left unspecified,\n#   indicates that baseline support for the specified C++ standard is\n#   required and that the macro should error out if no mode with that\n#   support is found.  If specified 'optional', then configuration proceeds\n#   regardless, after defining HAVE_CXX${VERSION} if and only if a\n#   supporting mode is found.\n#\n# LICENSE\n#\n#   Copyright (c) 2008 Benjamin Kosnik <bkoz@redhat.com>\n#   Copyright (c) 2012 Zack Weinberg <zackw@panix.com>\n#   Copyright (c) 2013 Roy Stogner <roystgnr@ices.utexas.edu>\n#   Copyright (c) 2014, 2015 Google Inc.; contributed by Alexey Sokolov <sokolov@google.com>\n#   Copyright (c) 2015 Paul Norman <penorman@mac.com>\n#   Copyright (c) 2015 Moritz Klammler <moritz@klammler.eu>\n#\n#   Copying and distribution of this file, with or without modification, are\n#   permitted in any medium without royalty provided the copyright notice\n#   and this notice are preserved.  This file is offered as-is, without any\n#   warranty.\n\n#serial 4\n\ndnl  This macro is based on the code from the AX_CXX_COMPILE_STDCXX_11 macro\ndnl  (serial version number 13).\n\nAC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl\n  m4_if([$1], [11], [],\n        [$1], [14], [],\n        [$1], [17], [m4_fatal([support for C++17 not yet implemented in AX_CXX_COMPILE_STDCXX])],\n        [m4_fatal([invalid first argument `$1' to AX_CXX_COMPILE_STDCXX])])dnl\n  m4_if([$2], [], [],\n        [$2], [ext], [],\n        [$2], [noext], [],\n        [m4_fatal([invalid second argument `$2' to AX_CXX_COMPILE_STDCXX])])dnl\n  m4_if([$3], [], [ax_cxx_compile_cxx$1_required=true],\n        [$3], [mandatory], [ax_cxx_compile_cxx$1_required=true],\n        [$3], [optional], [ax_cxx_compile_cxx$1_required=false],\n        [m4_fatal([invalid third argument `$3' to AX_CXX_COMPILE_STDCXX])])\n  AC_LANG_PUSH([C++])dnl\n  ac_success=no\n  AC_CACHE_CHECK(whether $CXX supports C++$1 features by default,\n  ax_cv_cxx_compile_cxx$1,\n  [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],\n    [ax_cv_cxx_compile_cxx$1=yes],\n    [ax_cv_cxx_compile_cxx$1=no])])\n  if test x$ax_cv_cxx_compile_cxx$1 = xyes; then\n    ac_success=yes\n  fi\n\n  m4_if([$2], [noext], [], [dnl\n  if test x$ac_success = xno; then\n    for switch in -std=gnu++$1 -std=gnu++0x; do\n      cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])\n      AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,\n                     $cachevar,\n        [ac_save_CXX=\"$CXX\"\n         CXX=\"$CXX $switch\"\n         AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],\n          [eval $cachevar=yes],\n          [eval $cachevar=no])\n         CXX=\"$ac_save_CXX\"])\n      if eval test x\\$$cachevar = xyes; then\n        CXX=\"$CXX $switch\"\n        if test -n \"$CXXCPP\" ; then\n          CXXCPP=\"$CXXCPP $switch\"\n        fi\n        ac_success=yes\n        break\n      fi\n    done\n  fi])\n\n  m4_if([$2], [ext], [], [dnl\n  if test x$ac_success = xno; then\n    dnl HP's aCC needs +std=c++11 according to:\n    dnl http://h21007.www2.hp.com/portal/download/files/unprot/aCxx/PDF_Release_Notes/769149-001.pdf\n    dnl Cray's crayCC needs \"-h std=c++11\"\n    for switch in -std=c++$1 -std=c++0x +std=c++$1 \"-h std=c++$1\"; do\n      cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])\n      AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,\n                     $cachevar,\n        [ac_save_CXX=\"$CXX\"\n         CXX=\"$CXX $switch\"\n         AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],\n          [eval $cachevar=yes],\n          [eval $cachevar=no])\n         CXX=\"$ac_save_CXX\"])\n      if eval test x\\$$cachevar = xyes; then\n        CXX=\"$CXX $switch\"\n        if test -n \"$CXXCPP\" ; then\n          CXXCPP=\"$CXXCPP $switch\"\n        fi\n        ac_success=yes\n        break\n      fi\n    done\n  fi])\n  AC_LANG_POP([C++])\n  if test x$ax_cxx_compile_cxx$1_required = xtrue; then\n    if test x$ac_success = xno; then\n      AC_MSG_ERROR([*** A compiler with support for C++$1 language features is required.])\n    fi\n  fi\n  if test x$ac_success = xno; then\n    HAVE_CXX$1=0\n    AC_MSG_NOTICE([No compiler with C++$1 support was found])\n  else\n    HAVE_CXX$1=1\n    AC_DEFINE(HAVE_CXX$1,1,\n              [define if the compiler supports basic C++$1 syntax])\n  fi\n  AC_SUBST(HAVE_CXX$1)\n])\n\n\ndnl  Test body for checking C++11 support\n\nm4_define([_AX_CXX_COMPILE_STDCXX_testbody_11],\n  _AX_CXX_COMPILE_STDCXX_testbody_new_in_11\n)\n\n\ndnl  Test body for checking C++14 support\n\nm4_define([_AX_CXX_COMPILE_STDCXX_testbody_14],\n  _AX_CXX_COMPILE_STDCXX_testbody_new_in_11\n  _AX_CXX_COMPILE_STDCXX_testbody_new_in_14\n)\n\n\ndnl  Tests for new features in C++11\n\nm4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_11], [[\n\n// If the compiler admits that it is not ready for C++11, why torture it?\n// Hopefully, this will speed up the test.\n\n#ifndef __cplusplus\n\n#error \"This is not a C++ compiler\"\n\n#elif __cplusplus < 201103L\n\n#error \"This is not a C++11 compiler\"\n\n#else\n\nnamespace cxx11\n{\n\n  namespace test_static_assert\n  {\n\n    template <typename T>\n    struct check\n    {\n      static_assert(sizeof(int) <= sizeof(T), \"not big enough\");\n    };\n\n  }\n\n  namespace test_final_override\n  {\n\n    struct Base\n    {\n      virtual void f() {}\n    };\n\n    struct Derived : public Base\n    {\n      virtual void f() override {}\n    };\n\n  }\n\n  namespace test_double_right_angle_brackets\n  {\n\n    template < typename T >\n    struct check {};\n\n    typedef check<void> single_type;\n    typedef check<check<void>> double_type;\n    typedef check<check<check<void>>> triple_type;\n    typedef check<check<check<check<void>>>> quadruple_type;\n\n  }\n\n  namespace test_decltype\n  {\n\n    int\n    f()\n    {\n      int a = 1;\n      decltype(a) b = 2;\n      return a + b;\n    }\n\n  }\n\n  namespace test_type_deduction\n  {\n\n    template < typename T1, typename T2 >\n    struct is_same\n    {\n      static const bool value = false;\n    };\n\n    template < typename T >\n    struct is_same<T, T>\n    {\n      static const bool value = true;\n    };\n\n    template < typename T1, typename T2 >\n    auto\n    add(T1 a1, T2 a2) -> decltype(a1 + a2)\n    {\n      return a1 + a2;\n    }\n\n    int\n    test(const int c, volatile int v)\n    {\n      static_assert(is_same<int, decltype(0)>::value == true, \"\");\n      static_assert(is_same<int, decltype(c)>::value == false, \"\");\n      static_assert(is_same<int, decltype(v)>::value == false, \"\");\n      auto ac = c;\n      auto av = v;\n      auto sumi = ac + av + 'x';\n      auto sumf = ac + av + 1.0;\n      static_assert(is_same<int, decltype(ac)>::value == true, \"\");\n      static_assert(is_same<int, decltype(av)>::value == true, \"\");\n      static_assert(is_same<int, decltype(sumi)>::value == true, \"\");\n      static_assert(is_same<int, decltype(sumf)>::value == false, \"\");\n      static_assert(is_same<int, decltype(add(c, v))>::value == true, \"\");\n      return (sumf > 0.0) ? sumi : add(c, v);\n    }\n\n  }\n\n  namespace test_noexcept\n  {\n\n    int f() { return 0; }\n    int g() noexcept { return 0; }\n\n    static_assert(noexcept(f()) == false, \"\");\n    static_assert(noexcept(g()) == true, \"\");\n\n  }\n\n  namespace test_constexpr\n  {\n\n    template < typename CharT >\n    unsigned long constexpr\n    strlen_c_r(const CharT *const s, const unsigned long acc) noexcept\n    {\n      return *s ? strlen_c_r(s + 1, acc + 1) : acc;\n    }\n\n    template < typename CharT >\n    unsigned long constexpr\n    strlen_c(const CharT *const s) noexcept\n    {\n      return strlen_c_r(s, 0UL);\n    }\n\n    static_assert(strlen_c(\"\") == 0UL, \"\");\n    static_assert(strlen_c(\"1\") == 1UL, \"\");\n    static_assert(strlen_c(\"example\") == 7UL, \"\");\n    static_assert(strlen_c(\"another\\0example\") == 7UL, \"\");\n\n  }\n\n  namespace test_rvalue_references\n  {\n\n    template < int N >\n    struct answer\n    {\n      static constexpr int value = N;\n    };\n\n    answer<1> f(int&)       { return answer<1>(); }\n    answer<2> f(const int&) { return answer<2>(); }\n    answer<3> f(int&&)      { return answer<3>(); }\n\n    void\n    test()\n    {\n      int i = 0;\n      const int c = 0;\n      static_assert(decltype(f(i))::value == 1, \"\");\n      static_assert(decltype(f(c))::value == 2, \"\");\n      static_assert(decltype(f(0))::value == 3, \"\");\n    }\n\n  }\n\n  namespace test_uniform_initialization\n  {\n\n    struct test\n    {\n      static const int zero {};\n      static const int one {1};\n    };\n\n    static_assert(test::zero == 0, \"\");\n    static_assert(test::one == 1, \"\");\n\n  }\n\n  namespace test_lambdas\n  {\n\n    void\n    test1()\n    {\n      auto lambda1 = [](){};\n      auto lambda2 = lambda1;\n      lambda1();\n      lambda2();\n    }\n\n    int\n    test2()\n    {\n      auto a = [](int i, int j){ return i + j; }(1, 2);\n      auto b = []() -> int { return '0'; }();\n      auto c = [=](){ return a + b; }();\n      auto d = [&](){ return c; }();\n      auto e = [a, &b](int x) mutable {\n        const auto identity = [](int y){ return y; };\n        for (auto i = 0; i < a; ++i)\n          a += b--;\n        return x + identity(a + b);\n      }(0);\n      return a + b + c + d + e;\n    }\n\n    int\n    test3()\n    {\n      const auto nullary = [](){ return 0; };\n      const auto unary = [](int x){ return x; };\n      using nullary_t = decltype(nullary);\n      using unary_t = decltype(unary);\n      const auto higher1st = [](nullary_t f){ return f(); };\n      const auto higher2nd = [unary](nullary_t f1){\n        return [unary, f1](unary_t f2){ return f2(unary(f1())); };\n      };\n      return higher1st(nullary) + higher2nd(nullary)(unary);\n    }\n\n  }\n\n  namespace test_variadic_templates\n  {\n\n    template <int...>\n    struct sum;\n\n    template <int N0, int... N1toN>\n    struct sum<N0, N1toN...>\n    {\n      static constexpr auto value = N0 + sum<N1toN...>::value;\n    };\n\n    template <>\n    struct sum<>\n    {\n      static constexpr auto value = 0;\n    };\n\n    static_assert(sum<>::value == 0, \"\");\n    static_assert(sum<1>::value == 1, \"\");\n    static_assert(sum<23>::value == 23, \"\");\n    static_assert(sum<1, 2>::value == 3, \"\");\n    static_assert(sum<5, 5, 11>::value == 21, \"\");\n    static_assert(sum<2, 3, 5, 7, 11, 13>::value == 41, \"\");\n\n  }\n\n  // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae\n  // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function\n  // because of this.\n  namespace test_template_alias_sfinae\n  {\n\n    struct foo {};\n\n    template<typename T>\n    using member = typename T::member_type;\n\n    template<typename T>\n    void func(...) {}\n\n    template<typename T>\n    void func(member<T>*) {}\n\n    void test();\n\n    void test() { func<foo>(0); }\n\n  }\n\n}  // namespace cxx11\n\n#endif  // __cplusplus >= 201103L\n\n]])\n\n\ndnl  Tests for new features in C++14\n\nm4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_14], [[\n\n// If the compiler admits that it is not ready for C++14, why torture it?\n// Hopefully, this will speed up the test.\n\n#ifndef __cplusplus\n\n#error \"This is not a C++ compiler\"\n\n#elif __cplusplus < 201402L\n\n#error \"This is not a C++14 compiler\"\n\n#else\n\nnamespace cxx14\n{\n\n  namespace test_polymorphic_lambdas\n  {\n\n    int\n    test()\n    {\n      const auto lambda = [](auto&&... args){\n        const auto istiny = [](auto x){\n          return (sizeof(x) == 1UL) ? 1 : 0;\n        };\n        const int aretiny[] = { istiny(args)... };\n        return aretiny[0];\n      };\n      return lambda(1, 1L, 1.0f, '1');\n    }\n\n  }\n\n  namespace test_binary_literals\n  {\n\n    constexpr auto ivii = 0b0000000000101010;\n    static_assert(ivii == 42, \"wrong value\");\n\n  }\n\n  namespace test_generalized_constexpr\n  {\n\n    template < typename CharT >\n    constexpr unsigned long\n    strlen_c(const CharT *const s) noexcept\n    {\n      auto length = 0UL;\n      for (auto p = s; *p; ++p)\n        ++length;\n      return length;\n    }\n\n    static_assert(strlen_c(\"\") == 0UL, \"\");\n    static_assert(strlen_c(\"x\") == 1UL, \"\");\n    static_assert(strlen_c(\"test\") == 4UL, \"\");\n    static_assert(strlen_c(\"another\\0test\") == 7UL, \"\");\n\n  }\n\n  namespace test_lambda_init_capture\n  {\n\n    int\n    test()\n    {\n      auto x = 0;\n      const auto lambda1 = [a = x](int b){ return a + b; };\n      const auto lambda2 = [a = lambda1(x)](){ return a; };\n      return lambda2();\n    }\n\n  }\n\n  namespace test_digit_seperators\n  {\n\n    constexpr auto ten_million = 100'000'000;\n    static_assert(ten_million == 100000000, \"\");\n\n  }\n\n  namespace test_return_type_deduction\n  {\n\n    auto f(int& x) { return x; }\n    decltype(auto) g(int& x) { return x; }\n\n    template < typename T1, typename T2 >\n    struct is_same\n    {\n      static constexpr auto value = false;\n    };\n\n    template < typename T >\n    struct is_same<T, T>\n    {\n      static constexpr auto value = true;\n    };\n\n    int\n    test()\n    {\n      auto x = 0;\n      static_assert(is_same<int, decltype(f(x))>::value, \"\");\n      static_assert(is_same<int&, decltype(g(x))>::value, \"\");\n      return x;\n    }\n\n  }\n\n}  // namespace cxx14\n\n#endif  // __cplusplus >= 201402L\n\n]])\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/msvc/ReadMe.txt",
    "content": "\nHow to build jemalloc for Windows\n=================================\n\n1. Install Cygwin with at least the following packages:\n   * autoconf\n   * autogen\n   * gawk\n   * grep\n   * sed\n\n2. Install Visual Studio 2015 with Visual C++\n\n3. Add Cygwin\\bin to the PATH environment variable\n\n4. Open \"VS2015 x86 Native Tools Command Prompt\"\n   (note: x86/x64 doesn't matter at this point)\n\n5. Generate header files:\n   sh -c \"CC=cl ./autogen.sh\"\n\n6. Now the project can be opened and built in Visual Studio:\n   msvc\\jemalloc_vc2015.sln\n\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/msvc/jemalloc_vc2015.sln",
    "content": "﻿\nMicrosoft Visual Studio Solution File, Format Version 12.00\n# Visual Studio 14\nVisualStudioVersion = 14.0.24720.0\nMinimumVisualStudioVersion = 10.0.40219.1\nProject(\"{2150E333-8FDC-42A3-9474-1A3956D46DE8}\") = \"Solution Items\", \"Solution Items\", \"{70A99006-6DE9-472B-8F83-4CEE6C616DF3}\"\n\tProjectSection(SolutionItems) = preProject\n\t\tReadMe.txt = ReadMe.txt\n\tEndProjectSection\nEndProject\nProject(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \"jemalloc\", \"projects\\vc2015\\jemalloc\\jemalloc.vcxproj\", \"{8D6BB292-9E1C-413D-9F98-4864BDC1514A}\"\nEndProject\nProject(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \"test_threads\", \"projects\\vc2015\\test_threads\\test_threads.vcxproj\", \"{09028CFD-4EB7-491D-869C-0708DB97ED44}\"\nEndProject\nGlobal\n\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\n\t\tDebug|x64 = Debug|x64\n\t\tDebug|x86 = Debug|x86\n\t\tDebug-static|x64 = Debug-static|x64\n\t\tDebug-static|x86 = Debug-static|x86\n\t\tRelease|x64 = Release|x64\n\t\tRelease|x86 = Release|x86\n\t\tRelease-static|x64 = Release-static|x64\n\t\tRelease-static|x86 = Release-static|x86\n\tEndGlobalSection\n\tGlobalSection(ProjectConfigurationPlatforms) = postSolution\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Debug|x64.ActiveCfg = Debug|x64\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Debug|x64.Build.0 = Debug|x64\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Debug|x86.ActiveCfg = Debug|Win32\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Debug|x86.Build.0 = Debug|Win32\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Debug-static|x64.ActiveCfg = Debug-static|x64\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Debug-static|x64.Build.0 = Debug-static|x64\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Debug-static|x86.ActiveCfg = Debug-static|Win32\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Debug-static|x86.Build.0 = Debug-static|Win32\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Release|x64.ActiveCfg = Release|x64\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Release|x64.Build.0 = Release|x64\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Release|x86.ActiveCfg = Release|Win32\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Release|x86.Build.0 = Release|Win32\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Release-static|x64.ActiveCfg = Release-static|x64\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Release-static|x64.Build.0 = Release-static|x64\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Release-static|x86.ActiveCfg = Release-static|Win32\n\t\t{8D6BB292-9E1C-413D-9F98-4864BDC1514A}.Release-static|x86.Build.0 = Release-static|Win32\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Debug|x64.ActiveCfg = Debug|x64\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Debug|x64.Build.0 = Debug|x64\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Debug|x86.ActiveCfg = Debug|Win32\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Debug|x86.Build.0 = Debug|Win32\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Debug-static|x64.ActiveCfg = Debug-static|x64\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Debug-static|x64.Build.0 = Debug-static|x64\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Debug-static|x86.ActiveCfg = Debug-static|Win32\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Debug-static|x86.Build.0 = Debug-static|Win32\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Release|x64.ActiveCfg = Release|x64\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Release|x64.Build.0 = Release|x64\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Release|x86.ActiveCfg = Release|Win32\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Release|x86.Build.0 = Release|Win32\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Release-static|x64.ActiveCfg = Release-static|x64\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Release-static|x64.Build.0 = Release-static|x64\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Release-static|x86.ActiveCfg = Release-static|Win32\n\t\t{09028CFD-4EB7-491D-869C-0708DB97ED44}.Release-static|x86.Build.0 = Release-static|Win32\n\tEndGlobalSection\n\tGlobalSection(SolutionProperties) = preSolution\n\t\tHideSolutionNode = FALSE\n\tEndGlobalSection\nEndGlobal\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/msvc/projects/vc2015/jemalloc/jemalloc.vcxproj",
    "content": "﻿<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<Project DefaultTargets=\"Build\" ToolsVersion=\"14.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n  <ItemGroup Label=\"ProjectConfigurations\">\n    <ProjectConfiguration Include=\"Debug-static|Win32\">\n      <Configuration>Debug-static</Configuration>\n      <Platform>Win32</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Debug-static|x64\">\n      <Configuration>Debug-static</Configuration>\n      <Platform>x64</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Debug|Win32\">\n      <Configuration>Debug</Configuration>\n      <Platform>Win32</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Release-static|Win32\">\n      <Configuration>Release-static</Configuration>\n      <Platform>Win32</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Release-static|x64\">\n      <Configuration>Release-static</Configuration>\n      <Platform>x64</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Release|Win32\">\n      <Configuration>Release</Configuration>\n      <Platform>Win32</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Debug|x64\">\n      <Configuration>Debug</Configuration>\n      <Platform>x64</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Release|x64\">\n      <Configuration>Release</Configuration>\n      <Platform>x64</Platform>\n    </ProjectConfiguration>\n  </ItemGroup>\n  <ItemGroup>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\arena.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\background_thread.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\base.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\bitmap.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\ckh.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\ctl.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\extent.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\extent_dss.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\extent_mmap.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\hash.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\hooks.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\jemalloc.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\large.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\malloc_io.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\mutex.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\mutex_pool.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\nstime.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\pages.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\prng.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\prof.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\rtree.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\spin.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\stats.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\sz.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\tcache.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\ticker.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\tsd.c\" />\n    <ClCompile Include=\"..\\..\\..\\..\\src\\witness.c\" />\n  </ItemGroup>\n  <PropertyGroup Label=\"Globals\">\n    <ProjectGuid>{8D6BB292-9E1C-413D-9F98-4864BDC1514A}</ProjectGuid>\n    <Keyword>Win32Proj</Keyword>\n    <RootNamespace>jemalloc</RootNamespace>\n    <WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>\n  </PropertyGroup>\n  <Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\" Label=\"Configuration\">\n    <ConfigurationType>DynamicLibrary</ConfigurationType>\n    <UseDebugLibraries>true</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|Win32'\" Label=\"Configuration\">\n    <ConfigurationType>StaticLibrary</ConfigurationType>\n    <UseDebugLibraries>true</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\" Label=\"Configuration\">\n    <ConfigurationType>DynamicLibrary</ConfigurationType>\n    <UseDebugLibraries>false</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <WholeProgramOptimization>true</WholeProgramOptimization>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|Win32'\" Label=\"Configuration\">\n    <ConfigurationType>StaticLibrary</ConfigurationType>\n    <UseDebugLibraries>false</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <WholeProgramOptimization>true</WholeProgramOptimization>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\" Label=\"Configuration\">\n    <ConfigurationType>DynamicLibrary</ConfigurationType>\n    <UseDebugLibraries>true</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|x64'\" Label=\"Configuration\">\n    <ConfigurationType>StaticLibrary</ConfigurationType>\n    <UseDebugLibraries>true</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\" Label=\"Configuration\">\n    <ConfigurationType>DynamicLibrary</ConfigurationType>\n    <UseDebugLibraries>false</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <WholeProgramOptimization>true</WholeProgramOptimization>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|x64'\" Label=\"Configuration\">\n    <ConfigurationType>StaticLibrary</ConfigurationType>\n    <UseDebugLibraries>false</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <WholeProgramOptimization>true</WholeProgramOptimization>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.props\" />\n  <ImportGroup Label=\"ExtensionSettings\">\n  </ImportGroup>\n  <ImportGroup Label=\"Shared\">\n  </ImportGroup>\n  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|Win32'\" Label=\"PropertySheets\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|Win32'\" Label=\"PropertySheets\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|x64'\" Label=\"PropertySheets\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|x64'\" Label=\"PropertySheets\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <PropertyGroup Label=\"UserMacros\" />\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <TargetName>$(ProjectName)d</TargetName>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|Win32'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <TargetName>$(ProjectName)-$(PlatformToolset)-$(Configuration)</TargetName>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|Win32'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <TargetName>$(ProjectName)-$(PlatformToolset)-$(Configuration)</TargetName>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <TargetName>$(ProjectName)d</TargetName>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|x64'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <TargetName>$(ProjectName)-vc$(PlatformToolsetVersion)-$(Configuration)</TargetName>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|x64'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <TargetName>$(ProjectName)-vc$(PlatformToolsetVersion)-$(Configuration)</TargetName>\n  </PropertyGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\">\n    <ClCompile>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <WarningLevel>Level3</WarningLevel>\n      <Optimization>Disabled</Optimization>\n      <PreprocessorDefinitions>_REENTRANT;_WINDLL;DLLEXPORT;JEMALLOC_DEBUG;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <DisableSpecificWarnings>4090;4146;4267;4334</DisableSpecificWarnings>\n      <ProgramDataBaseFileName>$(OutputPath)$(TargetName).pdb</ProgramDataBaseFileName>\n    </ClCompile>\n    <Link>\n      <SubSystem>Windows</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|Win32'\">\n    <ClCompile>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <WarningLevel>Level3</WarningLevel>\n      <Optimization>Disabled</Optimization>\n      <PreprocessorDefinitions>JEMALLOC_DEBUG;_REENTRANT;JEMALLOC_EXPORT=;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>\n      <DisableSpecificWarnings>4090;4146;4267;4334</DisableSpecificWarnings>\n      <ProgramDataBaseFileName>$(OutputPath)$(TargetName).pdb</ProgramDataBaseFileName>\n    </ClCompile>\n    <Link>\n      <SubSystem>Windows</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\">\n    <ClCompile>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <WarningLevel>Level3</WarningLevel>\n      <Optimization>Disabled</Optimization>\n      <PreprocessorDefinitions>JEMALLOC_NO_PRIVATE_NAMESPACE;_REENTRANT;_WINDLL;DLLEXPORT;JEMALLOC_DEBUG;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <DisableSpecificWarnings>4090;4146;4267;4334</DisableSpecificWarnings>\n      <ProgramDataBaseFileName>$(OutputPath)$(TargetName).pdb</ProgramDataBaseFileName>\n    </ClCompile>\n    <Link>\n      <SubSystem>Windows</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|x64'\">\n    <ClCompile>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <WarningLevel>Level3</WarningLevel>\n      <Optimization>Disabled</Optimization>\n      <PreprocessorDefinitions>JEMALLOC_NO_PRIVATE_NAMESPACE;JEMALLOC_DEBUG;_REENTRANT;JEMALLOC_EXPORT=;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>\n      <DisableSpecificWarnings>4090;4146;4267;4334</DisableSpecificWarnings>\n      <DebugInformationFormat>OldStyle</DebugInformationFormat>\n      <MinimalRebuild>false</MinimalRebuild>\n    </ClCompile>\n    <Link>\n      <SubSystem>Windows</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\">\n    <ClCompile>\n      <WarningLevel>Level3</WarningLevel>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <Optimization>MaxSpeed</Optimization>\n      <FunctionLevelLinking>true</FunctionLevelLinking>\n      <IntrinsicFunctions>true</IntrinsicFunctions>\n      <PreprocessorDefinitions>_REENTRANT;_WINDLL;DLLEXPORT;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <DisableSpecificWarnings>4090;4146;4267;4334</DisableSpecificWarnings>\n      <ProgramDataBaseFileName>$(OutputPath)$(TargetName).pdb</ProgramDataBaseFileName>\n    </ClCompile>\n    <Link>\n      <SubSystem>Windows</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <EnableCOMDATFolding>true</EnableCOMDATFolding>\n      <OptimizeReferences>true</OptimizeReferences>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|Win32'\">\n    <ClCompile>\n      <WarningLevel>Level3</WarningLevel>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <Optimization>MaxSpeed</Optimization>\n      <FunctionLevelLinking>true</FunctionLevelLinking>\n      <IntrinsicFunctions>true</IntrinsicFunctions>\n      <PreprocessorDefinitions>_REENTRANT;JEMALLOC_EXPORT=;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <RuntimeLibrary>MultiThreaded</RuntimeLibrary>\n      <DisableSpecificWarnings>4090;4146;4267;4334</DisableSpecificWarnings>\n      <ProgramDataBaseFileName>$(OutputPath)$(TargetName).pdb</ProgramDataBaseFileName>\n    </ClCompile>\n    <Link>\n      <SubSystem>Windows</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <EnableCOMDATFolding>true</EnableCOMDATFolding>\n      <OptimizeReferences>true</OptimizeReferences>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\">\n    <ClCompile>\n      <WarningLevel>Level3</WarningLevel>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <Optimization>MaxSpeed</Optimization>\n      <FunctionLevelLinking>true</FunctionLevelLinking>\n      <IntrinsicFunctions>true</IntrinsicFunctions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <PreprocessorDefinitions>JEMALLOC_NO_PRIVATE_NAMESPACE;_REENTRANT;_WINDLL;DLLEXPORT;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <DisableSpecificWarnings>4090;4146;4267;4334</DisableSpecificWarnings>\n      <ProgramDataBaseFileName>$(OutputPath)$(TargetName).pdb</ProgramDataBaseFileName>\n    </ClCompile>\n    <Link>\n      <SubSystem>Windows</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <EnableCOMDATFolding>true</EnableCOMDATFolding>\n      <OptimizeReferences>true</OptimizeReferences>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|x64'\">\n    <ClCompile>\n      <WarningLevel>Level3</WarningLevel>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <Optimization>MaxSpeed</Optimization>\n      <FunctionLevelLinking>true</FunctionLevelLinking>\n      <IntrinsicFunctions>true</IntrinsicFunctions>\n      <PreprocessorDefinitions>JEMALLOC_NO_PRIVATE_NAMESPACE;_REENTRANT;JEMALLOC_EXPORT=;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <RuntimeLibrary>MultiThreaded</RuntimeLibrary>\n      <DisableSpecificWarnings>4090;4146;4267;4334</DisableSpecificWarnings>\n      <DebugInformationFormat>OldStyle</DebugInformationFormat>\n    </ClCompile>\n    <Link>\n      <SubSystem>Windows</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <EnableCOMDATFolding>true</EnableCOMDATFolding>\n      <OptimizeReferences>true</OptimizeReferences>\n    </Link>\n  </ItemDefinitionGroup>\n  <Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.targets\" />\n  <ImportGroup Label=\"ExtensionTargets\">\n  </ImportGroup>\n</Project>"
  },
  {
    "path": "deps/memkind/src/jemalloc/msvc/projects/vc2015/jemalloc/jemalloc.vcxproj.filters",
    "content": "﻿<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<Project ToolsVersion=\"4.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n  <ItemGroup>\n    <Filter Include=\"Source Files\">\n      <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>\n      <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>\n    </Filter>\n  </ItemGroup>\n  <ItemGroup>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\arena.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\background_thread.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\base.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\bitmap.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\ckh.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\ctl.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\extent.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\extent_dss.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\extent_mmap.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\hash.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\hooks.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\jemalloc.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\large.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\malloc_io.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\mutex.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\mutex_pool.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\nstime.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\pages.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\prng.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\prof.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\rtree.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\spin.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\stats.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\sz.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\tcache.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\ticker.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\tsd.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"..\\..\\..\\..\\src\\witness.c\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n  </ItemGroup>\n</Project>"
  },
  {
    "path": "deps/memkind/src/jemalloc/msvc/projects/vc2015/test_threads/test_threads.cpp",
    "content": "// jemalloc C++ threaded test\n// Author: Rustam Abdullaev\n// Public Domain\n\n#include <atomic>\n#include <functional>\n#include <future>\n#include <random>\n#include <thread>\n#include <vector>\n#include <stdio.h>\n#include <jemalloc/jemalloc.h>\n\nusing std::vector;\nusing std::thread;\nusing std::uniform_int_distribution;\nusing std::minstd_rand;\n\nint test_threads() {\n  je_malloc_conf = \"narenas:3\";\n  int narenas = 0;\n  size_t sz = sizeof(narenas);\n  je_mallctl(\"opt.narenas\", (void *)&narenas, &sz, NULL, 0);\n  if (narenas != 3) {\n    printf(\"Error: unexpected number of arenas: %d\\n\", narenas);\n    return 1;\n  }\n  static const int sizes[] = { 7, 16, 32, 60, 91, 100, 120, 144, 169, 199, 255, 400, 670, 900, 917, 1025, 3333, 5190, 13131, 49192, 99999, 123123, 255265, 2333111 };\n  static const int numSizes = (int)(sizeof(sizes) / sizeof(sizes[0]));\n  vector<thread> workers;\n  static const int numThreads = narenas + 1, numAllocsMax = 25, numIter1 = 50, numIter2 = 50;\n  je_malloc_stats_print(NULL, NULL, NULL);\n  size_t allocated1;\n  size_t sz1 = sizeof(allocated1);\n  je_mallctl(\"stats.active\", (void *)&allocated1, &sz1, NULL, 0);\n  printf(\"\\nPress Enter to start threads...\\n\");\n  getchar();\n  printf(\"Starting %d threads x %d x %d iterations...\\n\", numThreads, numIter1, numIter2);\n  for (int i = 0; i < numThreads; i++) {\n    workers.emplace_back([tid=i]() {\n      uniform_int_distribution<int> sizeDist(0, numSizes - 1);\n      minstd_rand rnd(tid * 17);\n      uint8_t* ptrs[numAllocsMax];\n      int ptrsz[numAllocsMax];\n      for (int i = 0; i < numIter1; ++i) {\n        thread t([&]() {\n          for (int i = 0; i < numIter2; ++i) {\n            const int numAllocs = numAllocsMax - sizeDist(rnd);\n            for (int j = 0; j < numAllocs; j += 64) {\n              const int x = sizeDist(rnd);\n              const int sz = sizes[x];\n              ptrsz[j] = sz;\n              ptrs[j] = (uint8_t*)je_malloc(sz);\n              if (!ptrs[j]) {\n                printf(\"Unable to allocate %d bytes in thread %d, iter %d, alloc %d. %d\\n\", sz, tid, i, j, x);\n                exit(1);\n              }\n              for (int k = 0; k < sz; k++)\n                ptrs[j][k] = tid + k;\n            }\n            for (int j = 0; j < numAllocs; j += 64) {\n              for (int k = 0, sz = ptrsz[j]; k < sz; k++)\n                if (ptrs[j][k] != (uint8_t)(tid + k)) {\n                  printf(\"Memory error in thread %d, iter %d, alloc %d @ %d : %02X!=%02X\\n\", tid, i, j, k, ptrs[j][k], (uint8_t)(tid + k));\n                  exit(1);\n                }\n              je_free(ptrs[j]);\n            }\n          }\n        });\n        t.join();\n      }\n    });\n  }\n  for (thread& t : workers) {\n    t.join();\n  }\n  je_malloc_stats_print(NULL, NULL, NULL);\n  size_t allocated2;\n  je_mallctl(\"stats.active\", (void *)&allocated2, &sz1, NULL, 0);\n  size_t leaked = allocated2 - allocated1;\n  printf(\"\\nDone. Leaked: %zd bytes\\n\", leaked);\n  bool failed = leaked > 65536; // in case C++ runtime allocated something (e.g. iostream locale or facet)\n  printf(\"\\nTest %s!\\n\", (failed ? \"FAILED\" : \"successful\"));\n  printf(\"\\nPress Enter to continue...\\n\");\n  getchar();\n  return failed ? 1 : 0;\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/msvc/projects/vc2015/test_threads/test_threads.h",
    "content": "#pragma once\n\nint test_threads();\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/msvc/projects/vc2015/test_threads/test_threads.vcxproj",
    "content": "﻿<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<Project DefaultTargets=\"Build\" ToolsVersion=\"14.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n  <ItemGroup Label=\"ProjectConfigurations\">\n    <ProjectConfiguration Include=\"Debug-static|Win32\">\n      <Configuration>Debug-static</Configuration>\n      <Platform>Win32</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Debug-static|x64\">\n      <Configuration>Debug-static</Configuration>\n      <Platform>x64</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Debug|Win32\">\n      <Configuration>Debug</Configuration>\n      <Platform>Win32</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Release-static|Win32\">\n      <Configuration>Release-static</Configuration>\n      <Platform>Win32</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Release-static|x64\">\n      <Configuration>Release-static</Configuration>\n      <Platform>x64</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Release|Win32\">\n      <Configuration>Release</Configuration>\n      <Platform>Win32</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Debug|x64\">\n      <Configuration>Debug</Configuration>\n      <Platform>x64</Platform>\n    </ProjectConfiguration>\n    <ProjectConfiguration Include=\"Release|x64\">\n      <Configuration>Release</Configuration>\n      <Platform>x64</Platform>\n    </ProjectConfiguration>\n  </ItemGroup>\n  <PropertyGroup Label=\"Globals\">\n    <ProjectGuid>{09028CFD-4EB7-491D-869C-0708DB97ED44}</ProjectGuid>\n    <Keyword>Win32Proj</Keyword>\n    <RootNamespace>test_threads</RootNamespace>\n    <WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>\n  </PropertyGroup>\n  <Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\" Label=\"Configuration\">\n    <ConfigurationType>Application</ConfigurationType>\n    <UseDebugLibraries>true</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|Win32'\" Label=\"Configuration\">\n    <ConfigurationType>Application</ConfigurationType>\n    <UseDebugLibraries>true</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\" Label=\"Configuration\">\n    <ConfigurationType>Application</ConfigurationType>\n    <UseDebugLibraries>false</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <WholeProgramOptimization>true</WholeProgramOptimization>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|Win32'\" Label=\"Configuration\">\n    <ConfigurationType>Application</ConfigurationType>\n    <UseDebugLibraries>false</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <WholeProgramOptimization>true</WholeProgramOptimization>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\" Label=\"Configuration\">\n    <ConfigurationType>Application</ConfigurationType>\n    <UseDebugLibraries>true</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|x64'\" Label=\"Configuration\">\n    <ConfigurationType>Application</ConfigurationType>\n    <UseDebugLibraries>true</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\" Label=\"Configuration\">\n    <ConfigurationType>Application</ConfigurationType>\n    <UseDebugLibraries>false</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <WholeProgramOptimization>true</WholeProgramOptimization>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|x64'\" Label=\"Configuration\">\n    <ConfigurationType>Application</ConfigurationType>\n    <UseDebugLibraries>false</UseDebugLibraries>\n    <PlatformToolset>v140</PlatformToolset>\n    <WholeProgramOptimization>true</WholeProgramOptimization>\n    <CharacterSet>MultiByte</CharacterSet>\n  </PropertyGroup>\n  <Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.props\" />\n  <ImportGroup Label=\"ExtensionSettings\">\n  </ImportGroup>\n  <ImportGroup Label=\"Shared\">\n  </ImportGroup>\n  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|Win32'\" Label=\"PropertySheets\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|Win32'\" Label=\"PropertySheets\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|x64'\" Label=\"PropertySheets\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Label=\"PropertySheets\" Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <ImportGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|x64'\" Label=\"PropertySheets\">\n    <Import Project=\"$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props\" Condition=\"exists('$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props')\" Label=\"LocalAppDataPlatform\" />\n  </ImportGroup>\n  <PropertyGroup Label=\"UserMacros\" />\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <LinkIncremental>true</LinkIncremental>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|Win32'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <LinkIncremental>true</LinkIncremental>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\">\n    <LinkIncremental>true</LinkIncremental>\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|x64'\">\n    <LinkIncremental>true</LinkIncremental>\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <LinkIncremental>false</LinkIncremental>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|Win32'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <LinkIncremental>false</LinkIncremental>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <LinkIncremental>false</LinkIncremental>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|x64'\">\n    <OutDir>$(SolutionDir)$(Platform)\\$(Configuration)\\</OutDir>\n    <IntDir>$(Platform)\\$(Configuration)\\</IntDir>\n    <LinkIncremental>false</LinkIncremental>\n  </PropertyGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|Win32'\">\n    <ClCompile>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <WarningLevel>Level3</WarningLevel>\n      <Optimization>Disabled</Optimization>\n      <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\test\\include;..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n    </ClCompile>\n    <Link>\n      <SubSystem>Console</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <AdditionalLibraryDirectories>$(SolutionDir)$(Platform)\\$(Configuration)</AdditionalLibraryDirectories>\n      <AdditionalDependencies>jemallocd.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|Win32'\">\n    <ClCompile>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <WarningLevel>Level3</WarningLevel>\n      <Optimization>Disabled</Optimization>\n      <PreprocessorDefinitions>JEMALLOC_EXPORT=;JEMALLOC_STATIC;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\test\\include;..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>\n    </ClCompile>\n    <Link>\n      <SubSystem>Console</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <AdditionalLibraryDirectories>$(SolutionDir)$(Platform)\\$(Configuration)</AdditionalLibraryDirectories>\n      <AdditionalDependencies>jemalloc-$(PlatformToolset)-$(Configuration).lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\">\n    <ClCompile>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <WarningLevel>Level3</WarningLevel>\n      <Optimization>Disabled</Optimization>\n      <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\test\\include;..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n    </ClCompile>\n    <Link>\n      <SubSystem>Console</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <AdditionalDependencies>jemallocd.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>\n      <AdditionalLibraryDirectories>$(SolutionDir)$(Platform)\\$(Configuration)</AdditionalLibraryDirectories>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug-static|x64'\">\n    <ClCompile>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <WarningLevel>Level3</WarningLevel>\n      <Optimization>Disabled</Optimization>\n      <PreprocessorDefinitions>JEMALLOC_EXPORT=;JEMALLOC_STATIC;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\test\\include;..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>\n    </ClCompile>\n    <Link>\n      <SubSystem>Console</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <AdditionalDependencies>jemalloc-vc$(PlatformToolsetVersion)-$(Configuration).lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>\n      <AdditionalLibraryDirectories>$(SolutionDir)$(Platform)\\$(Configuration)</AdditionalLibraryDirectories>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|Win32'\">\n    <ClCompile>\n      <WarningLevel>Level3</WarningLevel>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <Optimization>MaxSpeed</Optimization>\n      <FunctionLevelLinking>true</FunctionLevelLinking>\n      <IntrinsicFunctions>true</IntrinsicFunctions>\n      <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\test\\include;..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n    </ClCompile>\n    <Link>\n      <SubSystem>Console</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <EnableCOMDATFolding>true</EnableCOMDATFolding>\n      <OptimizeReferences>true</OptimizeReferences>\n      <AdditionalLibraryDirectories>$(SolutionDir)$(Platform)\\$(Configuration)</AdditionalLibraryDirectories>\n      <AdditionalDependencies>jemalloc.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|Win32'\">\n    <ClCompile>\n      <WarningLevel>Level3</WarningLevel>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <Optimization>MaxSpeed</Optimization>\n      <FunctionLevelLinking>true</FunctionLevelLinking>\n      <IntrinsicFunctions>true</IntrinsicFunctions>\n      <PreprocessorDefinitions>JEMALLOC_EXPORT=;JEMALLOC_STATIC;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\test\\include;..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <RuntimeLibrary>MultiThreaded</RuntimeLibrary>\n    </ClCompile>\n    <Link>\n      <SubSystem>Console</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <EnableCOMDATFolding>true</EnableCOMDATFolding>\n      <OptimizeReferences>true</OptimizeReferences>\n      <AdditionalLibraryDirectories>$(SolutionDir)$(Platform)\\$(Configuration)</AdditionalLibraryDirectories>\n      <AdditionalDependencies>jemalloc-$(PlatformToolset)-$(Configuration).lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\">\n    <ClCompile>\n      <WarningLevel>Level3</WarningLevel>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <Optimization>MaxSpeed</Optimization>\n      <FunctionLevelLinking>true</FunctionLevelLinking>\n      <IntrinsicFunctions>true</IntrinsicFunctions>\n      <PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\test\\include;..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n    </ClCompile>\n    <Link>\n      <SubSystem>Console</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <EnableCOMDATFolding>true</EnableCOMDATFolding>\n      <OptimizeReferences>true</OptimizeReferences>\n      <AdditionalLibraryDirectories>$(SolutionDir)$(Platform)\\$(Configuration)</AdditionalLibraryDirectories>\n      <AdditionalDependencies>jemalloc.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemDefinitionGroup Condition=\"'$(Configuration)|$(Platform)'=='Release-static|x64'\">\n    <ClCompile>\n      <WarningLevel>Level3</WarningLevel>\n      <PrecompiledHeader>\n      </PrecompiledHeader>\n      <Optimization>MaxSpeed</Optimization>\n      <FunctionLevelLinking>true</FunctionLevelLinking>\n      <IntrinsicFunctions>true</IntrinsicFunctions>\n      <PreprocessorDefinitions>JEMALLOC_EXPORT=;JEMALLOC_STATIC;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>\n      <AdditionalIncludeDirectories>..\\..\\..\\..\\test\\include;..\\..\\..\\..\\include;..\\..\\..\\..\\include\\msvc_compat;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>\n      <RuntimeLibrary>MultiThreaded</RuntimeLibrary>\n    </ClCompile>\n    <Link>\n      <SubSystem>Console</SubSystem>\n      <GenerateDebugInformation>true</GenerateDebugInformation>\n      <EnableCOMDATFolding>true</EnableCOMDATFolding>\n      <OptimizeReferences>true</OptimizeReferences>\n      <AdditionalLibraryDirectories>$(SolutionDir)$(Platform)\\$(Configuration)</AdditionalLibraryDirectories>\n      <AdditionalDependencies>jemalloc-vc$(PlatformToolsetVersion)-$(Configuration).lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>\n    </Link>\n  </ItemDefinitionGroup>\n  <ItemGroup>\n    <ClCompile Include=\"test_threads.cpp\" />\n    <ClCompile Include=\"test_threads_main.cpp\" />\n  </ItemGroup>\n  <ItemGroup>\n    <ProjectReference Include=\"..\\jemalloc\\jemalloc.vcxproj\">\n      <Project>{8d6bb292-9e1c-413d-9f98-4864bdc1514a}</Project>\n    </ProjectReference>\n  </ItemGroup>\n  <ItemGroup>\n    <ClInclude Include=\"test_threads.h\" />\n  </ItemGroup>\n  <Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.targets\" />\n  <ImportGroup Label=\"ExtensionTargets\">\n  </ImportGroup>\n</Project>"
  },
  {
    "path": "deps/memkind/src/jemalloc/msvc/projects/vc2015/test_threads/test_threads.vcxproj.filters",
    "content": "﻿<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<Project ToolsVersion=\"4.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n  <ItemGroup>\n    <Filter Include=\"Source Files\">\n      <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>\n      <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>\n    </Filter>\n    <Filter Include=\"Header Files\">\n      <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>\n      <Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>\n    </Filter>\n  </ItemGroup>\n  <ItemGroup>\n    <ClCompile Include=\"test_threads.cpp\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n    <ClCompile Include=\"test_threads_main.cpp\">\n      <Filter>Source Files</Filter>\n    </ClCompile>\n  </ItemGroup>\n  <ItemGroup>\n    <ClInclude Include=\"test_threads.h\">\n      <Filter>Header Files</Filter>\n    </ClInclude>\n  </ItemGroup>\n</Project>"
  },
  {
    "path": "deps/memkind/src/jemalloc/msvc/projects/vc2015/test_threads/test_threads_main.cpp",
    "content": "#include \"test_threads.h\"\n#include <future>\n#include <functional>\n#include <chrono>\n\nusing namespace std::chrono_literals;\n\nint main(int argc, char** argv) {\n  int rc = test_threads();\n  return rc;\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/run_tests.sh",
    "content": "$(dirname \"$)\")/scripts/gen_run_tests.py | bash\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/scripts/gen_run_tests.py",
    "content": "#!/usr/bin/env python\n\nfrom itertools import combinations\nfrom os import uname\nfrom multiprocessing import cpu_count\n\nnparallel = cpu_count() * 2\n\nuname = uname()[0]\n\ndef powerset(items):\n    result = []\n    for i in xrange(len(items) + 1):\n        result += combinations(items, i)\n    return result\n\npossible_compilers = [('gcc', 'g++'), ('clang', 'clang++')]\npossible_compiler_opts = [\n    '-m32',\n]\npossible_config_opts = [\n    '--enable-debug',\n    '--enable-prof',\n    '--disable-stats',\n    '--with-malloc-conf=tcache:false',\n]\npossible_malloc_conf_opts = [\n    'tcache:false',\n    'dss:primary',\n    'percpu_arena:percpu',\n    'background_thread:true',\n]\n\nprint 'set -e'\nprint 'if [ -f Makefile ] ; then make relclean ; fi'\nprint 'autoconf'\nprint 'rm -rf run_tests.out'\nprint 'mkdir run_tests.out'\nprint 'cd run_tests.out'\n\nind = 0\nfor cc, cxx in possible_compilers:\n    for compiler_opts in powerset(possible_compiler_opts):\n        for config_opts in powerset(possible_config_opts):\n            for malloc_conf_opts in powerset(possible_malloc_conf_opts):\n                if cc is 'clang' \\\n                  and '-m32' in possible_compiler_opts \\\n                  and '--enable-prof' in config_opts:\n                    continue\n                config_line = (\n                    'EXTRA_CFLAGS=-Werror EXTRA_CXXFLAGS=-Werror '\n                    + 'CC=\"{} {}\" '.format(cc, \" \".join(compiler_opts))\n                    + 'CXX=\"{} {}\" '.format(cxx, \" \".join(compiler_opts))\n                    + '../../configure '\n                    + \" \".join(config_opts) + (' --with-malloc-conf=' +\n                    \",\".join(malloc_conf_opts) if len(malloc_conf_opts) > 0\n                    else '')\n                )\n\n                # Per CPU arenas are only supported on Linux.\n                linux_supported = ('percpu_arena:percpu' in malloc_conf_opts \\\n                  or 'background_thread:true' in malloc_conf_opts)\n                # Heap profiling and dss are not supported on OS X.\n                darwin_unsupported = ('--enable-prof' in config_opts or \\\n                  'dss:primary' in malloc_conf_opts)\n                if (uname == 'Linux' and linux_supported) \\\n                  or (not linux_supported and (uname != 'Darwin' or \\\n                  not darwin_unsupported)):\n                    print \"\"\"cat <<EOF > run_test_%(ind)d.sh\n#!/bin/sh\n\nset -e\n\nabort() {\n    echo \"==> Error\" >> run_test.log\n    echo \"Error; see run_tests.out/run_test_%(ind)d.out/run_test.log\"\n    exit 255 # Special exit code tells xargs to terminate.\n}\n\n# Environment variables are not supported.\nrun_cmd() {\n    echo \"==> \\$@\" >> run_test.log\n    \\$@ >> run_test.log 2>&1 || abort\n}\n\necho \"=> run_test_%(ind)d: %(config_line)s\"\nmkdir run_test_%(ind)d.out\ncd run_test_%(ind)d.out\n\necho \"==> %(config_line)s\" >> run_test.log\n%(config_line)s >> run_test.log 2>&1 || abort\n\nrun_cmd make all tests\nrun_cmd make check\nrun_cmd make distclean\nEOF\nchmod 755 run_test_%(ind)d.sh\"\"\" % {'ind': ind, 'config_line': config_line}\n                    ind += 1\n\nprint 'for i in `seq 0 %(last_ind)d` ; do echo run_test_${i}.sh ; done | xargs -P %(nparallel)d -n 1 sh' % {'last_ind': ind-1, 'nparallel': nparallel}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/scripts/gen_travis.py",
    "content": "#!/usr/bin/env python\n\nfrom itertools import combinations\n\ntravis_template = \"\"\"\\\nlanguage: generic\n\nmatrix:\n  include:\n%s\n\nbefore_script:\n  - autoconf\n  - ./configure ${COMPILER_FLAGS:+ \\\n      CC=\"$CC $COMPILER_FLAGS\" \\\n      CXX=\"$CXX $COMPILER_FLAGS\" } \\\n      $CONFIGURE_FLAGS\n  - make -j3\n  - make -j3 tests\n\nscript:\n  - make check\n\"\"\"\n\n# The 'default' configuration is gcc, on linux, with no compiler or configure\n# flags.  We also test with clang, -m32, --enable-debug, --enable-prof,\n# --disable-stats, and --with-malloc-conf=tcache:false.  To avoid abusing\n# travis though, we don't test all 2**7 = 128 possible combinations of these;\n# instead, we only test combinations of up to 2 'unusual' settings, under the\n# hope that bugs involving interactions of such settings are rare.\n# Things at once, for C(7, 0) + C(7, 1) + C(7, 2) = 29\nMAX_UNUSUAL_OPTIONS = 2\n\nos_default = 'linux'\nos_unusual = 'osx'\n\ncompilers_default = 'CC=gcc CXX=g++'\ncompilers_unusual = 'CC=clang CXX=clang++'\n\ncompiler_flag_unusuals = ['-m32']\n\nconfigure_flag_unusuals = [\n    '--enable-debug',\n    '--enable-prof',\n    '--disable-stats',\n]\n\nmalloc_conf_unusuals = [\n    'tcache:false',\n    'dss:primary',\n    'percpu_arena:percpu',\n    'background_thread:true',\n]\n\nall_unusuals = (\n    [os_unusual] + [compilers_unusual] + compiler_flag_unusuals\n    + configure_flag_unusuals + malloc_conf_unusuals\n)\n\nunusual_combinations_to_test = []\nfor i in xrange(MAX_UNUSUAL_OPTIONS + 1):\n    unusual_combinations_to_test += combinations(all_unusuals, i)\n\ninclude_rows = \"\"\nfor unusual_combination in unusual_combinations_to_test:\n    os = os_default\n    if os_unusual in unusual_combination:\n        os = os_unusual\n\n    compilers = compilers_default\n    if compilers_unusual in unusual_combination:\n        compilers = compilers_unusual\n\n    compiler_flags = [\n        x for x in unusual_combination if x in compiler_flag_unusuals]\n\n    configure_flags = [\n        x for x in unusual_combination if x in configure_flag_unusuals]\n\n    malloc_conf = [\n        x for x in unusual_combination if x in malloc_conf_unusuals]\n    # Filter out unsupported configurations on OS X.\n    if os == 'osx' and ('dss:primary' in malloc_conf or \\\n      'percpu_arena:percpu' in malloc_conf or 'background_thread:true' \\\n      in malloc_conf):\n        continue\n    if len(malloc_conf) > 0:\n        configure_flags.append('--with-malloc-conf=' + \",\".join(malloc_conf))\n\n    # Filter out an unsupported configuration - heap profiling on OS X.\n    if os == 'osx' and '--enable-prof' in configure_flags:\n        continue\n\n    # We get some spurious errors when -Warray-bounds is enabled.\n    env_string = ('{} COMPILER_FLAGS=\"{}\" CONFIGURE_FLAGS=\"{}\" '\n\t'EXTRA_CFLAGS=\"-Werror -Wno-array-bounds\"').format(\n        compilers, \" \".join(compiler_flags), \" \".join(configure_flags))\n\n    include_rows += '    - os: %s\\n' % os\n    include_rows += '      env: %s\\n' % env_string\n    if '-m32' in unusual_combination and os == 'linux':\n        include_rows += '      addons:\\n'\n\tinclude_rows += '        apt:\\n'\n\tinclude_rows += '          packages:\\n'\n\tinclude_rows += '            - gcc-multilib\\n'\n\nprint travis_template % include_rows\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/arena.c",
    "content": "#define JEMALLOC_ARENA_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/extent_dss.h\"\n#include \"jemalloc/internal/extent_mmap.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/rtree.h\"\n#include \"jemalloc/internal/size_classes.h\"\n#include \"jemalloc/internal/util.h\"\n\n/******************************************************************************/\n/* Data. */\n\n/*\n * Define names for both unininitialized and initialized phases, so that\n * options and mallctl processing are straightforward.\n */\nconst char *percpu_arena_mode_names[] = {\n\t\"percpu\",\n\t\"phycpu\",\n\t\"disabled\",\n\t\"percpu\",\n\t\"phycpu\"\n};\npercpu_arena_mode_t opt_percpu_arena = PERCPU_ARENA_DEFAULT;\n\nssize_t opt_dirty_decay_ms = DIRTY_DECAY_MS_DEFAULT;\nssize_t opt_muzzy_decay_ms = MUZZY_DECAY_MS_DEFAULT;\n\nstatic atomic_zd_t dirty_decay_ms_default;\nstatic atomic_zd_t muzzy_decay_ms_default;\n\nconst arena_bin_info_t arena_bin_info[NBINS] = {\n#define BIN_INFO_bin_yes(reg_size, slab_size, nregs)\t\t\t\\\n\t{reg_size, slab_size, nregs, BITMAP_INFO_INITIALIZER(nregs)},\n#define BIN_INFO_bin_no(reg_size, slab_size, nregs)\n#define SC(index, lg_grp, lg_delta, ndelta, psz, bin, pgs,\t\t\\\n    lg_delta_lookup)\t\t\t\t\t\t\t\\\n\tBIN_INFO_bin_##bin((1U<<lg_grp) + (ndelta<<lg_delta),\t\t\\\n\t    (pgs << LG_PAGE), (pgs << LG_PAGE) / ((1U<<lg_grp) +\t\\\n\t    (ndelta<<lg_delta)))\n\tSIZE_CLASSES\n#undef BIN_INFO_bin_yes\n#undef BIN_INFO_bin_no\n#undef SC\n};\n\nconst uint64_t h_steps[SMOOTHSTEP_NSTEPS] = {\n#define STEP(step, h, x, y)\t\t\t\\\n\t\th,\n\t\tSMOOTHSTEP\n#undef STEP\n};\n\n/******************************************************************************/\n/*\n * Function prototypes for static functions that are referenced prior to\n * definition.\n */\n\nstatic void arena_decay_to_limit(tsdn_t *tsdn, arena_t *arena,\n    arena_decay_t *decay, extents_t *extents, bool all, size_t npages_limit,\n    bool is_background_thread);\nstatic bool arena_decay_dirty(tsdn_t *tsdn, arena_t *arena,\n    bool is_background_thread, bool all);\nstatic void arena_dalloc_bin_slab(tsdn_t *tsdn, arena_t *arena, extent_t *slab,\n    arena_bin_t *bin);\nstatic void arena_bin_lower_slab(tsdn_t *tsdn, arena_t *arena, extent_t *slab,\n    arena_bin_t *bin);\n\n/******************************************************************************/\n\nstatic bool\narena_stats_init(tsdn_t *tsdn, arena_stats_t *arena_stats) {\n\tif (config_debug) {\n\t\tfor (size_t i = 0; i < sizeof(arena_stats_t); i++) {\n\t\t\tassert(((char *)arena_stats)[i] == 0);\n\t\t}\n\t}\n#ifndef JEMALLOC_ATOMIC_U64\n\tif (malloc_mutex_init(&arena_stats->mtx, \"arena_stats\",\n\t    WITNESS_RANK_ARENA_STATS, malloc_mutex_rank_exclusive)) {\n\t\treturn true;\n\t}\n#endif\n\t/* Memory is zeroed, so there is no need to clear stats. */\n\treturn false;\n}\n\nstatic void\narena_stats_lock(tsdn_t *tsdn, arena_stats_t *arena_stats) {\n#ifndef JEMALLOC_ATOMIC_U64\n\tmalloc_mutex_lock(tsdn, &arena_stats->mtx);\n#endif\n}\n\nstatic void\narena_stats_unlock(tsdn_t *tsdn, arena_stats_t *arena_stats) {\n#ifndef JEMALLOC_ATOMIC_U64\n\tmalloc_mutex_unlock(tsdn, &arena_stats->mtx);\n#endif\n}\n\nstatic uint64_t\narena_stats_read_u64(tsdn_t *tsdn, arena_stats_t *arena_stats,\n    arena_stats_u64_t *p) {\n#ifdef JEMALLOC_ATOMIC_U64\n\treturn atomic_load_u64(p, ATOMIC_RELAXED);\n#else\n\tmalloc_mutex_assert_owner(tsdn, &arena_stats->mtx);\n\treturn *p;\n#endif\n}\n\nstatic void\narena_stats_add_u64(tsdn_t *tsdn, arena_stats_t *arena_stats,\n    arena_stats_u64_t *p, uint64_t x) {\n#ifdef JEMALLOC_ATOMIC_U64\n\tatomic_fetch_add_u64(p, x, ATOMIC_RELAXED);\n#else\n\tmalloc_mutex_assert_owner(tsdn, &arena_stats->mtx);\n\t*p += x;\n#endif\n}\n\nUNUSED static void\narena_stats_sub_u64(tsdn_t *tsdn, arena_stats_t *arena_stats,\n    arena_stats_u64_t *p, uint64_t x) {\n#ifdef JEMALLOC_ATOMIC_U64\n\tUNUSED uint64_t r = atomic_fetch_sub_u64(p, x, ATOMIC_RELAXED);\n\tassert(r - x <= r);\n#else\n\tmalloc_mutex_assert_owner(tsdn, &arena_stats->mtx);\n\t*p -= x;\n\tassert(*p + x >= *p);\n#endif\n}\n\n/*\n * Non-atomically sets *dst += src.  *dst needs external synchronization.\n * This lets us avoid the cost of a fetch_add when its unnecessary (note that\n * the types here are atomic).\n */\nstatic void\narena_stats_accum_u64(arena_stats_u64_t *dst, uint64_t src) {\n#ifdef JEMALLOC_ATOMIC_U64\n\tuint64_t cur_dst = atomic_load_u64(dst, ATOMIC_RELAXED);\n\tatomic_store_u64(dst, src + cur_dst, ATOMIC_RELAXED);\n#else\n\t*dst += src;\n#endif\n}\n\nstatic size_t\narena_stats_read_zu(tsdn_t *tsdn, arena_stats_t *arena_stats, atomic_zu_t *p) {\n#ifdef JEMALLOC_ATOMIC_U64\n\treturn atomic_load_zu(p, ATOMIC_RELAXED);\n#else\n\tmalloc_mutex_assert_owner(tsdn, &arena_stats->mtx);\n\treturn atomic_load_zu(p, ATOMIC_RELAXED);\n#endif\n}\n\nstatic void\narena_stats_add_zu(tsdn_t *tsdn, arena_stats_t *arena_stats, atomic_zu_t *p,\n    size_t x) {\n#ifdef JEMALLOC_ATOMIC_U64\n\tatomic_fetch_add_zu(p, x, ATOMIC_RELAXED);\n#else\n\tmalloc_mutex_assert_owner(tsdn, &arena_stats->mtx);\n\tsize_t cur = atomic_load_zu(p, ATOMIC_RELAXED);\n\tatomic_store_zu(p, cur + x, ATOMIC_RELAXED);\n#endif\n}\n\nstatic void\narena_stats_sub_zu(tsdn_t *tsdn, arena_stats_t *arena_stats, atomic_zu_t *p,\n    size_t x) {\n#ifdef JEMALLOC_ATOMIC_U64\n\tUNUSED size_t r = atomic_fetch_sub_zu(p, x, ATOMIC_RELAXED);\n\tassert(r - x <= r);\n#else\n\tmalloc_mutex_assert_owner(tsdn, &arena_stats->mtx);\n\tsize_t cur = atomic_load_zu(p, ATOMIC_RELAXED);\n\tatomic_store_zu(p, cur - x, ATOMIC_RELAXED);\n#endif\n}\n\n/* Like the _u64 variant, needs an externally synchronized *dst. */\nstatic void\narena_stats_accum_zu(atomic_zu_t *dst, size_t src) {\n\tsize_t cur_dst = atomic_load_zu(dst, ATOMIC_RELAXED);\n\tatomic_store_zu(dst, src + cur_dst, ATOMIC_RELAXED);\n}\n\nvoid\narena_stats_large_nrequests_add(tsdn_t *tsdn, arena_stats_t *arena_stats,\n    szind_t szind, uint64_t nrequests) {\n\tarena_stats_lock(tsdn, arena_stats);\n\tarena_stats_add_u64(tsdn, arena_stats, &arena_stats->lstats[szind -\n\t    NBINS].nrequests, nrequests);\n\tarena_stats_unlock(tsdn, arena_stats);\n}\n\nvoid\narena_stats_mapped_add(tsdn_t *tsdn, arena_stats_t *arena_stats, size_t size) {\n\tarena_stats_lock(tsdn, arena_stats);\n\tarena_stats_add_zu(tsdn, arena_stats, &arena_stats->mapped, size);\n\tarena_stats_unlock(tsdn, arena_stats);\n}\n\nvoid\narena_basic_stats_merge(tsdn_t *tsdn, arena_t *arena, unsigned *nthreads,\n    const char **dss, ssize_t *dirty_decay_ms, ssize_t *muzzy_decay_ms,\n    size_t *nactive, size_t *ndirty, size_t *nmuzzy) {\n\t*nthreads += arena_nthreads_get(arena, false);\n\t*dss = dss_prec_names[arena_dss_prec_get(arena)];\n\t*dirty_decay_ms = arena_dirty_decay_ms_get(arena);\n\t*muzzy_decay_ms = arena_muzzy_decay_ms_get(arena);\n\t*nactive += atomic_load_zu(&arena->nactive, ATOMIC_RELAXED);\n\t*ndirty += extents_npages_get(&arena->extents_dirty);\n\t*nmuzzy += extents_npages_get(&arena->extents_muzzy);\n}\n\nvoid\narena_stats_merge(tsdn_t *tsdn, arena_t *arena, unsigned *nthreads,\n    const char **dss, ssize_t *dirty_decay_ms, ssize_t *muzzy_decay_ms,\n    size_t *nactive, size_t *ndirty, size_t *nmuzzy, arena_stats_t *astats,\n    malloc_bin_stats_t *bstats, malloc_large_stats_t *lstats) {\n\tcassert(config_stats);\n\n\tarena_basic_stats_merge(tsdn, arena, nthreads, dss, dirty_decay_ms,\n\t    muzzy_decay_ms, nactive, ndirty, nmuzzy);\n\n\tsize_t base_allocated, base_resident, base_mapped;\n\tbase_stats_get(tsdn, arena->base, &base_allocated, &base_resident,\n\t    &base_mapped);\n\n\tarena_stats_lock(tsdn, &arena->stats);\n\n\tarena_stats_accum_zu(&astats->mapped, base_mapped\n\t    + arena_stats_read_zu(tsdn, &arena->stats, &arena->stats.mapped));\n\tarena_stats_accum_zu(&astats->retained,\n\t    extents_npages_get(&arena->extents_retained) << LG_PAGE);\n\n\tarena_stats_accum_u64(&astats->decay_dirty.npurge,\n\t    arena_stats_read_u64(tsdn, &arena->stats,\n\t    &arena->stats.decay_dirty.npurge));\n\tarena_stats_accum_u64(&astats->decay_dirty.nmadvise,\n\t    arena_stats_read_u64(tsdn, &arena->stats,\n\t    &arena->stats.decay_dirty.nmadvise));\n\tarena_stats_accum_u64(&astats->decay_dirty.purged,\n\t    arena_stats_read_u64(tsdn, &arena->stats,\n\t    &arena->stats.decay_dirty.purged));\n\n\tarena_stats_accum_u64(&astats->decay_muzzy.npurge,\n\t    arena_stats_read_u64(tsdn, &arena->stats,\n\t    &arena->stats.decay_muzzy.npurge));\n\tarena_stats_accum_u64(&astats->decay_muzzy.nmadvise,\n\t    arena_stats_read_u64(tsdn, &arena->stats,\n\t    &arena->stats.decay_muzzy.nmadvise));\n\tarena_stats_accum_u64(&astats->decay_muzzy.purged,\n\t    arena_stats_read_u64(tsdn, &arena->stats,\n\t    &arena->stats.decay_muzzy.purged));\n\n\tarena_stats_accum_zu(&astats->base, base_allocated);\n\tarena_stats_accum_zu(&astats->internal, arena_internal_get(arena));\n\tarena_stats_accum_zu(&astats->resident, base_resident +\n\t    (((atomic_load_zu(&arena->nactive, ATOMIC_RELAXED) +\n\t    extents_npages_get(&arena->extents_dirty) +\n\t    extents_npages_get(&arena->extents_muzzy)) << LG_PAGE)));\n\n\tfor (szind_t i = 0; i < NSIZES - NBINS; i++) {\n\t\tuint64_t nmalloc = arena_stats_read_u64(tsdn, &arena->stats,\n\t\t    &arena->stats.lstats[i].nmalloc);\n\t\tarena_stats_accum_u64(&lstats[i].nmalloc, nmalloc);\n\t\tarena_stats_accum_u64(&astats->nmalloc_large, nmalloc);\n\n\t\tuint64_t ndalloc = arena_stats_read_u64(tsdn, &arena->stats,\n\t\t    &arena->stats.lstats[i].ndalloc);\n\t\tarena_stats_accum_u64(&lstats[i].ndalloc, ndalloc);\n\t\tarena_stats_accum_u64(&astats->ndalloc_large, ndalloc);\n\n\t\tuint64_t nrequests = arena_stats_read_u64(tsdn, &arena->stats,\n\t\t    &arena->stats.lstats[i].nrequests);\n\t\tarena_stats_accum_u64(&lstats[i].nrequests,\n\t\t    nmalloc + nrequests);\n\t\tarena_stats_accum_u64(&astats->nrequests_large,\n\t\t    nmalloc + nrequests);\n\n\t\tassert(nmalloc >= ndalloc);\n\t\tassert(nmalloc - ndalloc <= SIZE_T_MAX);\n\t\tsize_t curlextents = (size_t)(nmalloc - ndalloc);\n\t\tlstats[i].curlextents += curlextents;\n\t\tarena_stats_accum_zu(&astats->allocated_large,\n\t\t    curlextents * sz_index2size(NBINS + i));\n\t}\n\n\tarena_stats_unlock(tsdn, &arena->stats);\n\n\t/* tcache_bytes counts currently cached bytes. */\n\tatomic_store_zu(&astats->tcache_bytes, 0, ATOMIC_RELAXED);\n\tmalloc_mutex_lock(tsdn, &arena->tcache_ql_mtx);\n\ttcache_t *tcache;\n\tql_foreach(tcache, &arena->tcache_ql, link) {\n\t\tszind_t i = 0;\n\t\tfor (; i < NBINS; i++) {\n\t\t\ttcache_bin_t *tbin = tcache_small_bin_get(tcache, i);\n\t\t\tarena_stats_accum_zu(&astats->tcache_bytes,\n\t\t\t    tbin->ncached * sz_index2size(i));\n\t\t}\n\t\tfor (; i < nhbins; i++) {\n\t\t\ttcache_bin_t *tbin = tcache_large_bin_get(tcache, i);\n\t\t\tarena_stats_accum_zu(&astats->tcache_bytes,\n\t\t\t    tbin->ncached * sz_index2size(i));\n\t\t}\n\t}\n\tmalloc_mutex_prof_read(tsdn,\n\t    &astats->mutex_prof_data[arena_prof_mutex_tcache_list],\n\t    &arena->tcache_ql_mtx);\n\tmalloc_mutex_unlock(tsdn, &arena->tcache_ql_mtx);\n\n#define READ_ARENA_MUTEX_PROF_DATA(mtx, ind)\t\t\t\t\\\n    malloc_mutex_lock(tsdn, &arena->mtx);\t\t\t\t\\\n    malloc_mutex_prof_read(tsdn, &astats->mutex_prof_data[ind],\t\t\\\n        &arena->mtx);\t\t\t\t\t\t\t\\\n    malloc_mutex_unlock(tsdn, &arena->mtx);\n\n\t/* Gather per arena mutex profiling data. */\n\tREAD_ARENA_MUTEX_PROF_DATA(large_mtx, arena_prof_mutex_large);\n\tREAD_ARENA_MUTEX_PROF_DATA(extent_avail_mtx,\n\t    arena_prof_mutex_extent_avail)\n\tREAD_ARENA_MUTEX_PROF_DATA(extents_dirty.mtx,\n\t    arena_prof_mutex_extents_dirty)\n\tREAD_ARENA_MUTEX_PROF_DATA(extents_muzzy.mtx,\n\t    arena_prof_mutex_extents_muzzy)\n\tREAD_ARENA_MUTEX_PROF_DATA(extents_retained.mtx,\n\t    arena_prof_mutex_extents_retained)\n\tREAD_ARENA_MUTEX_PROF_DATA(decay_dirty.mtx,\n\t    arena_prof_mutex_decay_dirty)\n\tREAD_ARENA_MUTEX_PROF_DATA(decay_muzzy.mtx,\n\t    arena_prof_mutex_decay_muzzy)\n\tREAD_ARENA_MUTEX_PROF_DATA(base->mtx,\n\t    arena_prof_mutex_base)\n#undef READ_ARENA_MUTEX_PROF_DATA\n\n\tnstime_copy(&astats->uptime, &arena->create_time);\n\tnstime_update(&astats->uptime);\n\tnstime_subtract(&astats->uptime, &arena->create_time);\n\n\tfor (szind_t i = 0; i < NBINS; i++) {\n\t\tarena_bin_t *bin = &arena->bins[i];\n\n\t\tmalloc_mutex_lock(tsdn, &bin->lock);\n\t\tmalloc_mutex_prof_read(tsdn, &bstats[i].mutex_data, &bin->lock);\n\t\tbstats[i].nmalloc += bin->stats.nmalloc;\n\t\tbstats[i].ndalloc += bin->stats.ndalloc;\n\t\tbstats[i].nrequests += bin->stats.nrequests;\n\t\tbstats[i].curregs += bin->stats.curregs;\n\t\tbstats[i].nfills += bin->stats.nfills;\n\t\tbstats[i].nflushes += bin->stats.nflushes;\n\t\tbstats[i].nslabs += bin->stats.nslabs;\n\t\tbstats[i].reslabs += bin->stats.reslabs;\n\t\tbstats[i].curslabs += bin->stats.curslabs;\n\t\tmalloc_mutex_unlock(tsdn, &bin->lock);\n\t}\n}\n\nvoid\narena_extents_dirty_dalloc(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\textents_dalloc(tsdn, arena, r_extent_hooks, &arena->extents_dirty,\n\t    extent);\n\tif (arena_dirty_decay_ms_get(arena) == 0) {\n\t\tarena_decay_dirty(tsdn, arena, false, true);\n\t} else {\n\t\tarena_background_thread_inactivity_check(tsdn, arena, false);\n\t}\n}\n\nstatic void *\narena_slab_reg_alloc(tsdn_t *tsdn, extent_t *slab,\n    const arena_bin_info_t *bin_info) {\n\tvoid *ret;\n\tarena_slab_data_t *slab_data = extent_slab_data_get(slab);\n\tsize_t regind;\n\n\tassert(extent_nfree_get(slab) > 0);\n\tassert(!bitmap_full(slab_data->bitmap, &bin_info->bitmap_info));\n\n\tregind = bitmap_sfu(slab_data->bitmap, &bin_info->bitmap_info);\n\tret = (void *)((uintptr_t)extent_addr_get(slab) +\n\t    (uintptr_t)(bin_info->reg_size * regind));\n\textent_nfree_dec(slab);\n\treturn ret;\n}\n\n#ifndef JEMALLOC_JET\nstatic\n#endif\nsize_t\narena_slab_regind(extent_t *slab, szind_t binind, const void *ptr) {\n\tsize_t diff, regind;\n\n\t/* Freeing a pointer outside the slab can cause assertion failure. */\n\tassert((uintptr_t)ptr >= (uintptr_t)extent_addr_get(slab));\n\tassert((uintptr_t)ptr < (uintptr_t)extent_past_get(slab));\n\t/* Freeing an interior pointer can cause assertion failure. */\n\tassert(((uintptr_t)ptr - (uintptr_t)extent_addr_get(slab)) %\n\t    (uintptr_t)arena_bin_info[binind].reg_size == 0);\n\n\t/* Avoid doing division with a variable divisor. */\n\tdiff = (size_t)((uintptr_t)ptr - (uintptr_t)extent_addr_get(slab));\n\tswitch (binind) {\n#define REGIND_bin_yes(index, reg_size)\t\t\t\t\t\\\n\tcase index:\t\t\t\t\t\t\t\\\n\t\tregind = diff / (reg_size);\t\t\t\t\\\n\t\tassert(diff == regind * (reg_size));\t\t\t\\\n\t\tbreak;\n#define REGIND_bin_no(index, reg_size)\n#define SC(index, lg_grp, lg_delta, ndelta, psz, bin, pgs,\t\t\\\n    lg_delta_lookup)\t\t\t\t\t\t\t\\\n\tREGIND_bin_##bin(index, (1U<<lg_grp) + (ndelta<<lg_delta))\n\tSIZE_CLASSES\n#undef REGIND_bin_yes\n#undef REGIND_bin_no\n#undef SC\n\tdefault: not_reached();\n\t}\n\n\tassert(regind < arena_bin_info[binind].nregs);\n\n\treturn regind;\n}\n\nstatic void\narena_slab_reg_dalloc(tsdn_t *tsdn, extent_t *slab,\n    arena_slab_data_t *slab_data, void *ptr) {\n\tszind_t binind = extent_szind_get(slab);\n\tconst arena_bin_info_t *bin_info = &arena_bin_info[binind];\n\tsize_t regind = arena_slab_regind(slab, binind, ptr);\n\n\tassert(extent_nfree_get(slab) < bin_info->nregs);\n\t/* Freeing an unallocated pointer can cause assertion failure. */\n\tassert(bitmap_get(slab_data->bitmap, &bin_info->bitmap_info, regind));\n\n\tbitmap_unset(slab_data->bitmap, &bin_info->bitmap_info, regind);\n\textent_nfree_inc(slab);\n}\n\nstatic void\narena_nactive_add(arena_t *arena, size_t add_pages) {\n\tatomic_fetch_add_zu(&arena->nactive, add_pages, ATOMIC_RELAXED);\n}\n\nstatic void\narena_nactive_sub(arena_t *arena, size_t sub_pages) {\n\tassert(atomic_load_zu(&arena->nactive, ATOMIC_RELAXED) >= sub_pages);\n\tatomic_fetch_sub_zu(&arena->nactive, sub_pages, ATOMIC_RELAXED);\n}\n\nstatic void\narena_large_malloc_stats_update(tsdn_t *tsdn, arena_t *arena, size_t usize) {\n\tszind_t index, hindex;\n\n\tcassert(config_stats);\n\n\tif (usize < LARGE_MINCLASS) {\n\t\tusize = LARGE_MINCLASS;\n\t}\n\tindex = sz_size2index(usize);\n\thindex = (index >= NBINS) ? index - NBINS : 0;\n\n\tarena_stats_add_u64(tsdn, &arena->stats,\n\t    &arena->stats.lstats[hindex].nmalloc, 1);\n}\n\nstatic void\narena_large_dalloc_stats_update(tsdn_t *tsdn, arena_t *arena, size_t usize) {\n\tszind_t index, hindex;\n\n\tcassert(config_stats);\n\n\tif (usize < LARGE_MINCLASS) {\n\t\tusize = LARGE_MINCLASS;\n\t}\n\tindex = sz_size2index(usize);\n\thindex = (index >= NBINS) ? index - NBINS : 0;\n\n\tarena_stats_add_u64(tsdn, &arena->stats,\n\t    &arena->stats.lstats[hindex].ndalloc, 1);\n}\n\nstatic void\narena_large_ralloc_stats_update(tsdn_t *tsdn, arena_t *arena, size_t oldusize,\n    size_t usize) {\n\tarena_large_dalloc_stats_update(tsdn, arena, oldusize);\n\tarena_large_malloc_stats_update(tsdn, arena, usize);\n}\n\nextent_t *\narena_extent_alloc_large(tsdn_t *tsdn, arena_t *arena, size_t usize,\n    size_t alignment, bool *zero) {\n\textent_hooks_t *extent_hooks = EXTENT_HOOKS_INITIALIZER;\n\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\tszind_t szind = sz_size2index(usize);\n\tsize_t mapped_add;\n\tbool commit = true;\n\textent_t *extent = extents_alloc(tsdn, arena, &extent_hooks,\n\t    &arena->extents_dirty, NULL, usize, sz_large_pad, alignment, false,\n\t    szind, zero, &commit);\n\tif (extent == NULL) {\n\t\textent = extents_alloc(tsdn, arena, &extent_hooks,\n\t\t    &arena->extents_muzzy, NULL, usize, sz_large_pad, alignment,\n\t\t    false, szind, zero, &commit);\n\t}\n\tsize_t size = usize + sz_large_pad;\n\tif (extent == NULL) {\n\t\textent = extent_alloc_wrapper(tsdn, arena, &extent_hooks, NULL,\n\t\t    usize, sz_large_pad, alignment, false, szind, zero,\n\t\t    &commit);\n\t\tif (config_stats) {\n\t\t\t/*\n\t\t\t * extent may be NULL on OOM, but in that case\n\t\t\t * mapped_add isn't used below, so there's no need to\n\t\t\t * conditionlly set it to 0 here.\n\t\t\t */\n\t\t\tmapped_add = size;\n\t\t}\n\t} else if (config_stats) {\n\t\tmapped_add = 0;\n\t}\n\n\tif (extent != NULL) {\n\t\tif (config_stats) {\n\t\t\tarena_stats_lock(tsdn, &arena->stats);\n\t\t\tarena_large_malloc_stats_update(tsdn, arena, usize);\n\t\t\tif (mapped_add != 0) {\n\t\t\t\tarena_stats_add_zu(tsdn, &arena->stats,\n\t\t\t\t    &arena->stats.mapped, mapped_add);\n\t\t\t}\n\t\t\tarena_stats_unlock(tsdn, &arena->stats);\n\t\t}\n\t\tarena_nactive_add(arena, size >> LG_PAGE);\n\t}\n\n\treturn extent;\n}\n\nvoid\narena_extent_dalloc_large_prep(tsdn_t *tsdn, arena_t *arena, extent_t *extent) {\n\tif (config_stats) {\n\t\tarena_stats_lock(tsdn, &arena->stats);\n\t\tarena_large_dalloc_stats_update(tsdn, arena,\n\t\t    extent_usize_get(extent));\n\t\tarena_stats_unlock(tsdn, &arena->stats);\n\t}\n\tarena_nactive_sub(arena, extent_size_get(extent) >> LG_PAGE);\n}\n\nvoid\narena_extent_ralloc_large_shrink(tsdn_t *tsdn, arena_t *arena, extent_t *extent,\n    size_t oldusize) {\n\tsize_t usize = extent_usize_get(extent);\n\tsize_t udiff = oldusize - usize;\n\n\tif (config_stats) {\n\t\tarena_stats_lock(tsdn, &arena->stats);\n\t\tarena_large_ralloc_stats_update(tsdn, arena, oldusize, usize);\n\t\tarena_stats_unlock(tsdn, &arena->stats);\n\t}\n\tarena_nactive_sub(arena, udiff >> LG_PAGE);\n}\n\nvoid\narena_extent_ralloc_large_expand(tsdn_t *tsdn, arena_t *arena, extent_t *extent,\n    size_t oldusize) {\n\tsize_t usize = extent_usize_get(extent);\n\tsize_t udiff = usize - oldusize;\n\n\tif (config_stats) {\n\t\tarena_stats_lock(tsdn, &arena->stats);\n\t\tarena_large_ralloc_stats_update(tsdn, arena, oldusize, usize);\n\t\tarena_stats_unlock(tsdn, &arena->stats);\n\t}\n\tarena_nactive_add(arena, udiff >> LG_PAGE);\n}\n\nstatic ssize_t\narena_decay_ms_read(arena_decay_t *decay) {\n\treturn atomic_load_zd(&decay->time_ms, ATOMIC_RELAXED);\n}\n\nstatic void\narena_decay_ms_write(arena_decay_t *decay, ssize_t decay_ms) {\n\tatomic_store_zd(&decay->time_ms, decay_ms, ATOMIC_RELAXED);\n}\n\nstatic void\narena_decay_deadline_init(arena_decay_t *decay) {\n\t/*\n\t * Generate a new deadline that is uniformly random within the next\n\t * epoch after the current one.\n\t */\n\tnstime_copy(&decay->deadline, &decay->epoch);\n\tnstime_add(&decay->deadline, &decay->interval);\n\tif (arena_decay_ms_read(decay) > 0) {\n\t\tnstime_t jitter;\n\n\t\tnstime_init(&jitter, prng_range_u64(&decay->jitter_state,\n\t\t    nstime_ns(&decay->interval)));\n\t\tnstime_add(&decay->deadline, &jitter);\n\t}\n}\n\nstatic bool\narena_decay_deadline_reached(const arena_decay_t *decay, const nstime_t *time) {\n\treturn (nstime_compare(&decay->deadline, time) <= 0);\n}\n\nstatic size_t\narena_decay_backlog_npages_limit(const arena_decay_t *decay) {\n\tuint64_t sum;\n\tsize_t npages_limit_backlog;\n\tunsigned i;\n\n\t/*\n\t * For each element of decay_backlog, multiply by the corresponding\n\t * fixed-point smoothstep decay factor.  Sum the products, then divide\n\t * to round down to the nearest whole number of pages.\n\t */\n\tsum = 0;\n\tfor (i = 0; i < SMOOTHSTEP_NSTEPS; i++) {\n\t\tsum += decay->backlog[i] * h_steps[i];\n\t}\n\tnpages_limit_backlog = (size_t)(sum >> SMOOTHSTEP_BFP);\n\n\treturn npages_limit_backlog;\n}\n\nstatic void\narena_decay_backlog_update_last(arena_decay_t *decay, size_t current_npages) {\n\tsize_t npages_delta = (current_npages > decay->nunpurged) ?\n\t    current_npages - decay->nunpurged : 0;\n\tdecay->backlog[SMOOTHSTEP_NSTEPS-1] = npages_delta;\n\n\tif (config_debug) {\n\t\tif (current_npages > decay->ceil_npages) {\n\t\t\tdecay->ceil_npages = current_npages;\n\t\t}\n\t\tsize_t npages_limit = arena_decay_backlog_npages_limit(decay);\n\t\tassert(decay->ceil_npages >= npages_limit);\n\t\tif (decay->ceil_npages > npages_limit) {\n\t\t\tdecay->ceil_npages = npages_limit;\n\t\t}\n\t}\n}\n\nstatic void\narena_decay_backlog_update(arena_decay_t *decay, uint64_t nadvance_u64,\n    size_t current_npages) {\n\tif (nadvance_u64 >= SMOOTHSTEP_NSTEPS) {\n\t\tmemset(decay->backlog, 0, (SMOOTHSTEP_NSTEPS-1) *\n\t\t    sizeof(size_t));\n\t} else {\n\t\tsize_t nadvance_z = (size_t)nadvance_u64;\n\n\t\tassert((uint64_t)nadvance_z == nadvance_u64);\n\n\t\tmemmove(decay->backlog, &decay->backlog[nadvance_z],\n\t\t    (SMOOTHSTEP_NSTEPS - nadvance_z) * sizeof(size_t));\n\t\tif (nadvance_z > 1) {\n\t\t\tmemset(&decay->backlog[SMOOTHSTEP_NSTEPS -\n\t\t\t    nadvance_z], 0, (nadvance_z-1) * sizeof(size_t));\n\t\t}\n\t}\n\n\tarena_decay_backlog_update_last(decay, current_npages);\n}\n\nstatic void\narena_decay_try_purge(tsdn_t *tsdn, arena_t *arena, arena_decay_t *decay,\n    extents_t *extents, size_t current_npages, size_t npages_limit,\n    bool is_background_thread) {\n\tif (current_npages > npages_limit) {\n\t\tarena_decay_to_limit(tsdn, arena, decay, extents, false,\n\t\t    npages_limit, is_background_thread);\n\t}\n}\n\nstatic void\narena_decay_epoch_advance_helper(arena_decay_t *decay, const nstime_t *time,\n    size_t current_npages) {\n\tassert(arena_decay_deadline_reached(decay, time));\n\n\tnstime_t delta;\n\tnstime_copy(&delta, time);\n\tnstime_subtract(&delta, &decay->epoch);\n\n\tuint64_t nadvance_u64 = nstime_divide(&delta, &decay->interval);\n\tassert(nadvance_u64 > 0);\n\n\t/* Add nadvance_u64 decay intervals to epoch. */\n\tnstime_copy(&delta, &decay->interval);\n\tnstime_imultiply(&delta, nadvance_u64);\n\tnstime_add(&decay->epoch, &delta);\n\n\t/* Set a new deadline. */\n\tarena_decay_deadline_init(decay);\n\n\t/* Update the backlog. */\n\tarena_decay_backlog_update(decay, nadvance_u64, current_npages);\n}\n\nstatic void\narena_decay_epoch_advance(tsdn_t *tsdn, arena_t *arena, arena_decay_t *decay,\n    extents_t *extents, const nstime_t *time, bool is_background_thread) {\n\tsize_t current_npages = extents_npages_get(extents);\n\tarena_decay_epoch_advance_helper(decay, time, current_npages);\n\n\tsize_t npages_limit = arena_decay_backlog_npages_limit(decay);\n\t/* We may unlock decay->mtx when try_purge(). Finish logging first. */\n\tdecay->nunpurged = (npages_limit > current_npages) ? npages_limit :\n\t    current_npages;\n\n\tif (!background_thread_enabled() || is_background_thread) {\n\t\tarena_decay_try_purge(tsdn, arena, decay, extents,\n\t\t    current_npages, npages_limit, is_background_thread);\n\t}\n}\n\nstatic void\narena_decay_reinit(arena_decay_t *decay, extents_t *extents, ssize_t decay_ms) {\n\tarena_decay_ms_write(decay, decay_ms);\n\tif (decay_ms > 0) {\n\t\tnstime_init(&decay->interval, (uint64_t)decay_ms *\n\t\t    KQU(1000000));\n\t\tnstime_idivide(&decay->interval, SMOOTHSTEP_NSTEPS);\n\t}\n\n\tnstime_init(&decay->epoch, 0);\n\tnstime_update(&decay->epoch);\n\tdecay->jitter_state = (uint64_t)(uintptr_t)decay;\n\tarena_decay_deadline_init(decay);\n\tdecay->nunpurged = 0;\n\tmemset(decay->backlog, 0, SMOOTHSTEP_NSTEPS * sizeof(size_t));\n}\n\nstatic bool\narena_decay_init(arena_decay_t *decay, extents_t *extents, ssize_t decay_ms,\n    decay_stats_t *stats) {\n\tif (config_debug) {\n\t\tfor (size_t i = 0; i < sizeof(arena_decay_t); i++) {\n\t\t\tassert(((char *)decay)[i] == 0);\n\t\t}\n\t\tdecay->ceil_npages = 0;\n\t}\n\tif (malloc_mutex_init(&decay->mtx, \"decay\", WITNESS_RANK_DECAY,\n\t    malloc_mutex_rank_exclusive)) {\n\t\treturn true;\n\t}\n\tdecay->purging = false;\n\tarena_decay_reinit(decay, extents, decay_ms);\n\t/* Memory is zeroed, so there is no need to clear stats. */\n\tif (config_stats) {\n\t\tdecay->stats = stats;\n\t}\n\treturn false;\n}\n\nstatic bool\narena_decay_ms_valid(ssize_t decay_ms) {\n\tif (decay_ms < -1) {\n\t\treturn false;\n\t}\n\tif (decay_ms == -1 || (uint64_t)decay_ms <= NSTIME_SEC_MAX *\n\t    KQU(1000)) {\n\t\treturn true;\n\t}\n\treturn false;\n}\n\nstatic bool\narena_maybe_decay(tsdn_t *tsdn, arena_t *arena, arena_decay_t *decay,\n    extents_t *extents, bool is_background_thread) {\n\tmalloc_mutex_assert_owner(tsdn, &decay->mtx);\n\n\t/* Purge all or nothing if the option is disabled. */\n\tssize_t decay_ms = arena_decay_ms_read(decay);\n\tif (decay_ms <= 0) {\n\t\tif (decay_ms == 0) {\n\t\t\tarena_decay_to_limit(tsdn, arena, decay, extents, false,\n\t\t\t    0, is_background_thread);\n\t\t}\n\t\treturn false;\n\t}\n\n\tnstime_t time;\n\tnstime_init(&time, 0);\n\tnstime_update(&time);\n\tif (unlikely(!nstime_monotonic() && nstime_compare(&decay->epoch, &time)\n\t    > 0)) {\n\t\t/*\n\t\t * Time went backwards.  Move the epoch back in time and\n\t\t * generate a new deadline, with the expectation that time\n\t\t * typically flows forward for long enough periods of time that\n\t\t * epochs complete.  Unfortunately, this strategy is susceptible\n\t\t * to clock jitter triggering premature epoch advances, but\n\t\t * clock jitter estimation and compensation isn't feasible here\n\t\t * because calls into this code are event-driven.\n\t\t */\n\t\tnstime_copy(&decay->epoch, &time);\n\t\tarena_decay_deadline_init(decay);\n\t} else {\n\t\t/* Verify that time does not go backwards. */\n\t\tassert(nstime_compare(&decay->epoch, &time) <= 0);\n\t}\n\n\t/*\n\t * If the deadline has been reached, advance to the current epoch and\n\t * purge to the new limit if necessary.  Note that dirty pages created\n\t * during the current epoch are not subject to purge until a future\n\t * epoch, so as a result purging only happens during epoch advances, or\n\t * being triggered by background threads (scheduled event).\n\t */\n\tbool advance_epoch = arena_decay_deadline_reached(decay, &time);\n\tif (advance_epoch) {\n\t\tarena_decay_epoch_advance(tsdn, arena, decay, extents, &time,\n\t\t    is_background_thread);\n\t} else if (is_background_thread) {\n\t\tarena_decay_try_purge(tsdn, arena, decay, extents,\n\t\t    extents_npages_get(extents),\n\t\t    arena_decay_backlog_npages_limit(decay),\n\t\t    is_background_thread);\n\t}\n\n\treturn advance_epoch;\n}\n\nstatic ssize_t\narena_decay_ms_get(arena_decay_t *decay) {\n\treturn arena_decay_ms_read(decay);\n}\n\nssize_t\narena_dirty_decay_ms_get(arena_t *arena) {\n\treturn arena_decay_ms_get(&arena->decay_dirty);\n}\n\nssize_t\narena_muzzy_decay_ms_get(arena_t *arena) {\n\treturn arena_decay_ms_get(&arena->decay_muzzy);\n}\n\nstatic bool\narena_decay_ms_set(tsdn_t *tsdn, arena_t *arena, arena_decay_t *decay,\n    extents_t *extents, ssize_t decay_ms) {\n\tif (!arena_decay_ms_valid(decay_ms)) {\n\t\treturn true;\n\t}\n\n\tmalloc_mutex_lock(tsdn, &decay->mtx);\n\t/*\n\t * Restart decay backlog from scratch, which may cause many dirty pages\n\t * to be immediately purged.  It would conceptually be possible to map\n\t * the old backlog onto the new backlog, but there is no justification\n\t * for such complexity since decay_ms changes are intended to be\n\t * infrequent, either between the {-1, 0, >0} states, or a one-time\n\t * arbitrary change during initial arena configuration.\n\t */\n\tarena_decay_reinit(decay, extents, decay_ms);\n\tarena_maybe_decay(tsdn, arena, decay, extents, false);\n\tmalloc_mutex_unlock(tsdn, &decay->mtx);\n\n\treturn false;\n}\n\nbool\narena_dirty_decay_ms_set(tsdn_t *tsdn, arena_t *arena,\n    ssize_t decay_ms) {\n\treturn arena_decay_ms_set(tsdn, arena, &arena->decay_dirty,\n\t    &arena->extents_dirty, decay_ms);\n}\n\nbool\narena_muzzy_decay_ms_set(tsdn_t *tsdn, arena_t *arena,\n    ssize_t decay_ms) {\n\treturn arena_decay_ms_set(tsdn, arena, &arena->decay_muzzy,\n\t    &arena->extents_muzzy, decay_ms);\n}\n\nstatic size_t\narena_stash_decayed(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extents_t *extents, size_t npages_limit,\n    extent_list_t *decay_extents) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\t/* Stash extents according to npages_limit. */\n\tsize_t nstashed = 0;\n\textent_t *extent;\n\twhile ((extent = extents_evict(tsdn, arena, r_extent_hooks, extents,\n\t    npages_limit)) != NULL) {\n\t\textent_list_append(decay_extents, extent);\n\t\tnstashed += extent_size_get(extent) >> LG_PAGE;\n\t}\n\treturn nstashed;\n}\n\nstatic size_t\narena_decay_stashed(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, arena_decay_t *decay, extents_t *extents,\n    bool all, extent_list_t *decay_extents, bool is_background_thread) {\n\tUNUSED size_t nmadvise, nunmapped;\n\tsize_t npurged;\n\n\tif (config_stats) {\n\t\tnmadvise = 0;\n\t\tnunmapped = 0;\n\t}\n\tnpurged = 0;\n\n\tssize_t muzzy_decay_ms = arena_muzzy_decay_ms_get(arena);\n\tfor (extent_t *extent = extent_list_first(decay_extents); extent !=\n\t    NULL; extent = extent_list_first(decay_extents)) {\n\t\tif (config_stats) {\n\t\t\tnmadvise++;\n\t\t}\n\t\tsize_t npages = extent_size_get(extent) >> LG_PAGE;\n\t\tnpurged += npages;\n\t\textent_list_remove(decay_extents, extent);\n\t\tswitch (extents_state_get(extents)) {\n\t\tcase extent_state_active:\n\t\t\tnot_reached();\n\t\tcase extent_state_dirty:\n\t\t\tif (!all && muzzy_decay_ms != 0 &&\n\t\t\t    !extent_purge_lazy_wrapper(tsdn, arena,\n\t\t\t    r_extent_hooks, extent, 0,\n\t\t\t    extent_size_get(extent))) {\n\t\t\t\textents_dalloc(tsdn, arena, r_extent_hooks,\n\t\t\t\t    &arena->extents_muzzy, extent);\n\t\t\t\tarena_background_thread_inactivity_check(tsdn,\n\t\t\t\t    arena, is_background_thread);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\t/* Fall through. */\n\t\tcase extent_state_muzzy:\n\t\t\textent_dalloc_wrapper(tsdn, arena, r_extent_hooks,\n\t\t\t    extent);\n\t\t\tif (config_stats) {\n\t\t\t\tnunmapped += npages;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase extent_state_retained:\n\t\tdefault:\n\t\t\tnot_reached();\n\t\t}\n\t}\n\n\tif (config_stats) {\n\t\tarena_stats_lock(tsdn, &arena->stats);\n\t\tarena_stats_add_u64(tsdn, &arena->stats, &decay->stats->npurge,\n\t\t    1);\n\t\tarena_stats_add_u64(tsdn, &arena->stats,\n\t\t    &decay->stats->nmadvise, nmadvise);\n\t\tarena_stats_add_u64(tsdn, &arena->stats, &decay->stats->purged,\n\t\t    npurged);\n\t\tarena_stats_sub_zu(tsdn, &arena->stats, &arena->stats.mapped,\n\t\t    nunmapped << LG_PAGE);\n\t\tarena_stats_unlock(tsdn, &arena->stats);\n\t}\n\n\treturn npurged;\n}\n\n/*\n * npages_limit: Decay as many dirty extents as possible without violating the\n * invariant: (extents_npages_get(extents) >= npages_limit)\n */\nstatic void\narena_decay_to_limit(tsdn_t *tsdn, arena_t *arena, arena_decay_t *decay,\n    extents_t *extents, bool all, size_t npages_limit,\n    bool is_background_thread) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 1);\n\tmalloc_mutex_assert_owner(tsdn, &decay->mtx);\n\n\tif (decay->purging) {\n\t\treturn;\n\t}\n\tdecay->purging = true;\n\tmalloc_mutex_unlock(tsdn, &decay->mtx);\n\n\textent_hooks_t *extent_hooks = extent_hooks_get(arena);\n\n\textent_list_t decay_extents;\n\textent_list_init(&decay_extents);\n\n\tsize_t npurge = arena_stash_decayed(tsdn, arena, &extent_hooks, extents,\n\t    npages_limit, &decay_extents);\n\tif (npurge != 0) {\n\t\tUNUSED size_t npurged = arena_decay_stashed(tsdn, arena,\n\t\t    &extent_hooks, decay, extents, all, &decay_extents,\n\t\t    is_background_thread);\n\t\tassert(npurged == npurge);\n\t}\n\n\tmalloc_mutex_lock(tsdn, &decay->mtx);\n\tdecay->purging = false;\n}\n\nstatic bool\narena_decay_impl(tsdn_t *tsdn, arena_t *arena, arena_decay_t *decay,\n    extents_t *extents, bool is_background_thread, bool all) {\n\tif (all) {\n\t\tmalloc_mutex_lock(tsdn, &decay->mtx);\n\t\tarena_decay_to_limit(tsdn, arena, decay, extents, all, 0,\n\t\t    is_background_thread);\n\t\tmalloc_mutex_unlock(tsdn, &decay->mtx);\n\n\t\treturn false;\n\t}\n\n\tif (malloc_mutex_trylock(tsdn, &decay->mtx)) {\n\t\t/* No need to wait if another thread is in progress. */\n\t\treturn true;\n\t}\n\n\tbool epoch_advanced = arena_maybe_decay(tsdn, arena, decay, extents,\n\t    is_background_thread);\n\tsize_t npages_new;\n\tif (epoch_advanced) {\n\t\t/* Backlog is updated on epoch advance. */\n\t\tnpages_new = decay->backlog[SMOOTHSTEP_NSTEPS-1];\n\t}\n\tmalloc_mutex_unlock(tsdn, &decay->mtx);\n\n\tif (have_background_thread && background_thread_enabled() &&\n\t    epoch_advanced && !is_background_thread) {\n\t\tbackground_thread_interval_check(tsdn, arena, decay, npages_new);\n\t}\n\n\treturn false;\n}\n\nstatic bool\narena_decay_dirty(tsdn_t *tsdn, arena_t *arena, bool is_background_thread,\n    bool all) {\n\treturn arena_decay_impl(tsdn, arena, &arena->decay_dirty,\n\t    &arena->extents_dirty, is_background_thread, all);\n}\n\nstatic bool\narena_decay_muzzy(tsdn_t *tsdn, arena_t *arena, bool is_background_thread,\n    bool all) {\n\treturn arena_decay_impl(tsdn, arena, &arena->decay_muzzy,\n\t    &arena->extents_muzzy, is_background_thread, all);\n}\n\nvoid\narena_decay(tsdn_t *tsdn, arena_t *arena, bool is_background_thread, bool all) {\n\tif (arena_decay_dirty(tsdn, arena, is_background_thread, all)) {\n\t\treturn;\n\t}\n\tarena_decay_muzzy(tsdn, arena, is_background_thread, all);\n}\n\nstatic void\narena_slab_dalloc(tsdn_t *tsdn, arena_t *arena, extent_t *slab) {\n\tarena_nactive_sub(arena, extent_size_get(slab) >> LG_PAGE);\n\n\textent_hooks_t *extent_hooks = EXTENT_HOOKS_INITIALIZER;\n\tarena_extents_dirty_dalloc(tsdn, arena, &extent_hooks, slab);\n}\n\nstatic void\narena_bin_slabs_nonfull_insert(arena_bin_t *bin, extent_t *slab) {\n\tassert(extent_nfree_get(slab) > 0);\n\textent_heap_insert(&bin->slabs_nonfull, slab);\n}\n\nstatic void\narena_bin_slabs_nonfull_remove(arena_bin_t *bin, extent_t *slab) {\n\textent_heap_remove(&bin->slabs_nonfull, slab);\n}\n\nstatic extent_t *\narena_bin_slabs_nonfull_tryget(arena_bin_t *bin) {\n\textent_t *slab = extent_heap_remove_first(&bin->slabs_nonfull);\n\tif (slab == NULL) {\n\t\treturn NULL;\n\t}\n\tif (config_stats) {\n\t\tbin->stats.reslabs++;\n\t}\n\treturn slab;\n}\n\nstatic void\narena_bin_slabs_full_insert(arena_t *arena, arena_bin_t *bin, extent_t *slab) {\n\tassert(extent_nfree_get(slab) == 0);\n\t/*\n\t *  Tracking extents is required by arena_reset, which is not allowed\n\t *  for auto arenas.  Bypass this step to avoid touching the extent\n\t *  linkage (often results in cache misses) for auto arenas.\n\t */\n\tif (arena_is_auto(arena)) {\n\t\treturn;\n\t}\n\textent_list_append(&bin->slabs_full, slab);\n}\n\nstatic void\narena_bin_slabs_full_remove(arena_t *arena, arena_bin_t *bin, extent_t *slab) {\n\tif (arena_is_auto(arena)) {\n\t\treturn;\n\t}\n\textent_list_remove(&bin->slabs_full, slab);\n}\n\nvoid\narena_reset(tsd_t *tsd, arena_t *arena) {\n\t/*\n\t * Locking in this function is unintuitive.  The caller guarantees that\n\t * no concurrent operations are happening in this arena, but there are\n\t * still reasons that some locking is necessary:\n\t *\n\t * - Some of the functions in the transitive closure of calls assume\n\t *   appropriate locks are held, and in some cases these locks are\n\t *   temporarily dropped to avoid lock order reversal or deadlock due to\n\t *   reentry.\n\t * - mallctl(\"epoch\", ...) may concurrently refresh stats.  While\n\t *   strictly speaking this is a \"concurrent operation\", disallowing\n\t *   stats refreshes would impose an inconvenient burden.\n\t */\n\n\t/* Large allocations. */\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &arena->large_mtx);\n\n\tfor (extent_t *extent = extent_list_first(&arena->large); extent !=\n\t    NULL; extent = extent_list_first(&arena->large)) {\n\t\tvoid *ptr = extent_base_get(extent);\n\t\tsize_t usize;\n\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &arena->large_mtx);\n\t\talloc_ctx_t alloc_ctx;\n\t\trtree_ctx_t *rtree_ctx = tsd_rtree_ctx(tsd);\n\t\trtree_szind_slab_read(tsd_tsdn(tsd), &extents_rtree, rtree_ctx,\n\t\t    (uintptr_t)ptr, true, &alloc_ctx.szind, &alloc_ctx.slab);\n\t\tassert(alloc_ctx.szind != NSIZES);\n\n\t\tif (config_stats || (config_prof && opt_prof)) {\n\t\t\tusize = sz_index2size(alloc_ctx.szind);\n\t\t\tassert(usize == isalloc(tsd_tsdn(tsd), ptr));\n\t\t}\n\t\t/* Remove large allocation from prof sample set. */\n\t\tif (config_prof && opt_prof) {\n\t\t\tprof_free(tsd, ptr, usize, &alloc_ctx);\n\t\t}\n\t\tlarge_dalloc(tsd_tsdn(tsd), extent);\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &arena->large_mtx);\n\t}\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &arena->large_mtx);\n\n\t/* Bins. */\n\tfor (unsigned i = 0; i < NBINS; i++) {\n\t\textent_t *slab;\n\t\tarena_bin_t *bin = &arena->bins[i];\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &bin->lock);\n\t\tif (bin->slabcur != NULL) {\n\t\t\tslab = bin->slabcur;\n\t\t\tbin->slabcur = NULL;\n\t\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &bin->lock);\n\t\t\tarena_slab_dalloc(tsd_tsdn(tsd), arena, slab);\n\t\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &bin->lock);\n\t\t}\n\t\twhile ((slab = extent_heap_remove_first(&bin->slabs_nonfull)) !=\n\t\t    NULL) {\n\t\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &bin->lock);\n\t\t\tarena_slab_dalloc(tsd_tsdn(tsd), arena, slab);\n\t\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &bin->lock);\n\t\t}\n\t\tfor (slab = extent_list_first(&bin->slabs_full); slab != NULL;\n\t\t    slab = extent_list_first(&bin->slabs_full)) {\n\t\t\tarena_bin_slabs_full_remove(arena, bin, slab);\n\t\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &bin->lock);\n\t\t\tarena_slab_dalloc(tsd_tsdn(tsd), arena, slab);\n\t\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &bin->lock);\n\t\t}\n\t\tif (config_stats) {\n\t\t\tbin->stats.curregs = 0;\n\t\t\tbin->stats.curslabs = 0;\n\t\t}\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &bin->lock);\n\t}\n\n\tatomic_store_zu(&arena->nactive, 0, ATOMIC_RELAXED);\n}\n\nstatic void\narena_destroy_retained(tsdn_t *tsdn, arena_t *arena) {\n\t/*\n\t * Iterate over the retained extents and destroy them.  This gives the\n\t * extent allocator underlying the extent hooks an opportunity to unmap\n\t * all retained memory without having to keep its own metadata\n\t * structures.  In practice, virtual memory for dss-allocated extents is\n\t * leaked here, so best practice is to avoid dss for arenas to be\n\t * destroyed, or provide custom extent hooks that track retained\n\t * dss-based extents for later reuse.\n\t */\n\textent_hooks_t *extent_hooks = extent_hooks_get(arena);\n\textent_t *extent;\n\twhile ((extent = extents_evict(tsdn, arena, &extent_hooks,\n\t    &arena->extents_retained, 0)) != NULL) {\n\t\textent_destroy_wrapper(tsdn, arena, &extent_hooks, extent);\n\t}\n}\n\nvoid\narena_destroy(tsd_t *tsd, arena_t *arena) {\n\tassert(base_ind_get(arena->base) >= narenas_auto);\n\tassert(arena_nthreads_get(arena, false) == 0);\n\tassert(arena_nthreads_get(arena, true) == 0);\n\n\t/*\n\t * No allocations have occurred since arena_reset() was called.\n\t * Furthermore, the caller (arena_i_destroy_ctl()) purged all cached\n\t * extents, so only retained extents may remain.\n\t */\n\tassert(extents_npages_get(&arena->extents_dirty) == 0);\n\tassert(extents_npages_get(&arena->extents_muzzy) == 0);\n\n\t/* Deallocate retained memory. */\n\tarena_destroy_retained(tsd_tsdn(tsd), arena);\n\n\t/*\n\t * Remove the arena pointer from the arenas array.  We rely on the fact\n\t * that there is no way for the application to get a dirty read from the\n\t * arenas array unless there is an inherent race in the application\n\t * involving access of an arena being concurrently destroyed.  The\n\t * application must synchronize knowledge of the arena's validity, so as\n\t * long as we use an atomic write to update the arenas array, the\n\t * application will get a clean read any time after it synchronizes\n\t * knowledge that the arena is no longer valid.\n\t */\n\tarena_set(base_ind_get(arena->base), NULL);\n\n\t/*\n\t * Destroy the base allocator, which manages all metadata ever mapped by\n\t * this arena.\n\t */\n\tbase_delete(arena->base);\n}\n\nstatic extent_t *\narena_slab_alloc_hard(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, const arena_bin_info_t *bin_info,\n    szind_t szind) {\n\textent_t *slab;\n\tbool zero, commit;\n\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\tzero = false;\n\tcommit = true;\n\tslab = extent_alloc_wrapper(tsdn, arena, r_extent_hooks, NULL,\n\t    bin_info->slab_size, 0, PAGE, true, szind, &zero, &commit);\n\n\tif (config_stats && slab != NULL) {\n\t\tarena_stats_mapped_add(tsdn, &arena->stats,\n\t\t    bin_info->slab_size);\n\t}\n\n\treturn slab;\n}\n\nstatic extent_t *\narena_slab_alloc(tsdn_t *tsdn, arena_t *arena, szind_t binind,\n    const arena_bin_info_t *bin_info) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\textent_hooks_t *extent_hooks = EXTENT_HOOKS_INITIALIZER;\n\tszind_t szind = sz_size2index(bin_info->reg_size);\n\tbool zero = false;\n\tbool commit = true;\n\textent_t *slab = extents_alloc(tsdn, arena, &extent_hooks,\n\t    &arena->extents_dirty, NULL, bin_info->slab_size, 0, PAGE, true,\n\t    binind, &zero, &commit);\n\tif (slab == NULL) {\n\t\tslab = extents_alloc(tsdn, arena, &extent_hooks,\n\t\t    &arena->extents_muzzy, NULL, bin_info->slab_size, 0, PAGE,\n\t\t    true, binind, &zero, &commit);\n\t}\n\tif (slab == NULL) {\n\t\tslab = arena_slab_alloc_hard(tsdn, arena, &extent_hooks,\n\t\t    bin_info, szind);\n\t\tif (slab == NULL) {\n\t\t\treturn NULL;\n\t\t}\n\t}\n\tassert(extent_slab_get(slab));\n\n\t/* Initialize slab internals. */\n\tarena_slab_data_t *slab_data = extent_slab_data_get(slab);\n\textent_nfree_set(slab, bin_info->nregs);\n\tbitmap_init(slab_data->bitmap, &bin_info->bitmap_info, false);\n\n\tarena_nactive_add(arena, extent_size_get(slab) >> LG_PAGE);\n\n\treturn slab;\n}\n\nstatic extent_t *\narena_bin_nonfull_slab_get(tsdn_t *tsdn, arena_t *arena, arena_bin_t *bin,\n    szind_t binind) {\n\textent_t *slab;\n\tconst arena_bin_info_t *bin_info;\n\n\t/* Look for a usable slab. */\n\tslab = arena_bin_slabs_nonfull_tryget(bin);\n\tif (slab != NULL) {\n\t\treturn slab;\n\t}\n\t/* No existing slabs have any space available. */\n\n\tbin_info = &arena_bin_info[binind];\n\n\t/* Allocate a new slab. */\n\tmalloc_mutex_unlock(tsdn, &bin->lock);\n\t/******************************/\n\tslab = arena_slab_alloc(tsdn, arena, binind, bin_info);\n\t/********************************/\n\tmalloc_mutex_lock(tsdn, &bin->lock);\n\tif (slab != NULL) {\n\t\tif (config_stats) {\n\t\t\tbin->stats.nslabs++;\n\t\t\tbin->stats.curslabs++;\n\t\t}\n\t\treturn slab;\n\t}\n\n\t/*\n\t * arena_slab_alloc() failed, but another thread may have made\n\t * sufficient memory available while this one dropped bin->lock above,\n\t * so search one more time.\n\t */\n\tslab = arena_bin_slabs_nonfull_tryget(bin);\n\tif (slab != NULL) {\n\t\treturn slab;\n\t}\n\n\treturn NULL;\n}\n\n/* Re-fill bin->slabcur, then call arena_slab_reg_alloc(). */\nstatic void *\narena_bin_malloc_hard(tsdn_t *tsdn, arena_t *arena, arena_bin_t *bin,\n    szind_t binind) {\n\tconst arena_bin_info_t *bin_info;\n\textent_t *slab;\n\n\tbin_info = &arena_bin_info[binind];\n\tif (!arena_is_auto(arena) && bin->slabcur != NULL) {\n\t\tarena_bin_slabs_full_insert(arena, bin, bin->slabcur);\n\t\tbin->slabcur = NULL;\n\t}\n\tslab = arena_bin_nonfull_slab_get(tsdn, arena, bin, binind);\n\tif (bin->slabcur != NULL) {\n\t\t/*\n\t\t * Another thread updated slabcur while this one ran without the\n\t\t * bin lock in arena_bin_nonfull_slab_get().\n\t\t */\n\t\tif (extent_nfree_get(bin->slabcur) > 0) {\n\t\t\tvoid *ret = arena_slab_reg_alloc(tsdn, bin->slabcur,\n\t\t\t    bin_info);\n\t\t\tif (slab != NULL) {\n\t\t\t\t/*\n\t\t\t\t * arena_slab_alloc() may have allocated slab,\n\t\t\t\t * or it may have been pulled from\n\t\t\t\t * slabs_nonfull.  Therefore it is unsafe to\n\t\t\t\t * make any assumptions about how slab has\n\t\t\t\t * previously been used, and\n\t\t\t\t * arena_bin_lower_slab() must be called, as if\n\t\t\t\t * a region were just deallocated from the slab.\n\t\t\t\t */\n\t\t\t\tif (extent_nfree_get(slab) == bin_info->nregs) {\n\t\t\t\t\tarena_dalloc_bin_slab(tsdn, arena, slab,\n\t\t\t\t\t    bin);\n\t\t\t\t} else {\n\t\t\t\t\tarena_bin_lower_slab(tsdn, arena, slab,\n\t\t\t\t\t    bin);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn ret;\n\t\t}\n\n\t\tarena_bin_slabs_full_insert(arena, bin, bin->slabcur);\n\t\tbin->slabcur = NULL;\n\t}\n\n\tif (slab == NULL) {\n\t\treturn NULL;\n\t}\n\tbin->slabcur = slab;\n\n\tassert(extent_nfree_get(bin->slabcur) > 0);\n\n\treturn arena_slab_reg_alloc(tsdn, slab, bin_info);\n}\n\nvoid\narena_tcache_fill_small(tsdn_t *tsdn, arena_t *arena, tcache_t *tcache,\n    tcache_bin_t *tbin, szind_t binind, uint64_t prof_accumbytes) {\n\tunsigned i, nfill;\n\tarena_bin_t *bin;\n\n\tassert(tbin->ncached == 0);\n\n\tif (config_prof && arena_prof_accum(tsdn, arena, prof_accumbytes)) {\n\t\tprof_idump(tsdn);\n\t}\n\tbin = &arena->bins[binind];\n\tmalloc_mutex_lock(tsdn, &bin->lock);\n\tfor (i = 0, nfill = (tcache_bin_info[binind].ncached_max >>\n\t    tcache->lg_fill_div[binind]); i < nfill; i++) {\n\t\textent_t *slab;\n\t\tvoid *ptr;\n\t\tif ((slab = bin->slabcur) != NULL && extent_nfree_get(slab) >\n\t\t    0) {\n\t\t\tptr = arena_slab_reg_alloc(tsdn, slab,\n\t\t\t    &arena_bin_info[binind]);\n\t\t} else {\n\t\t\tptr = arena_bin_malloc_hard(tsdn, arena, bin, binind);\n\t\t}\n\t\tif (ptr == NULL) {\n\t\t\t/*\n\t\t\t * OOM.  tbin->avail isn't yet filled down to its first\n\t\t\t * element, so the successful allocations (if any) must\n\t\t\t * be moved just before tbin->avail before bailing out.\n\t\t\t */\n\t\t\tif (i > 0) {\n\t\t\t\tmemmove(tbin->avail - i, tbin->avail - nfill,\n\t\t\t\t    i * sizeof(void *));\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t\tif (config_fill && unlikely(opt_junk_alloc)) {\n\t\t\tarena_alloc_junk_small(ptr, &arena_bin_info[binind],\n\t\t\t    true);\n\t\t}\n\t\t/* Insert such that low regions get used first. */\n\t\t*(tbin->avail - nfill + i) = ptr;\n\t}\n\tif (config_stats) {\n\t\tbin->stats.nmalloc += i;\n\t\tbin->stats.nrequests += tbin->tstats.nrequests;\n\t\tbin->stats.curregs += i;\n\t\tbin->stats.nfills++;\n\t\ttbin->tstats.nrequests = 0;\n\t}\n\tmalloc_mutex_unlock(tsdn, &bin->lock);\n\ttbin->ncached = i;\n\tarena_decay_tick(tsdn, arena);\n}\n\nvoid\narena_alloc_junk_small(void *ptr, const arena_bin_info_t *bin_info, bool zero) {\n\tif (!zero) {\n\t\tmemset(ptr, JEMALLOC_ALLOC_JUNK, bin_info->reg_size);\n\t}\n}\n\nstatic void\narena_dalloc_junk_small_impl(void *ptr, const arena_bin_info_t *bin_info) {\n\tmemset(ptr, JEMALLOC_FREE_JUNK, bin_info->reg_size);\n}\narena_dalloc_junk_small_t *JET_MUTABLE arena_dalloc_junk_small =\n    arena_dalloc_junk_small_impl;\n\nstatic void *\narena_malloc_small(tsdn_t *tsdn, arena_t *arena, szind_t binind, bool zero) {\n\tvoid *ret;\n\tarena_bin_t *bin;\n\tsize_t usize;\n\textent_t *slab;\n\n\tassert(binind < NBINS);\n\tbin = &arena->bins[binind];\n\tusize = sz_index2size(binind);\n\n\tmalloc_mutex_lock(tsdn, &bin->lock);\n\tif ((slab = bin->slabcur) != NULL && extent_nfree_get(slab) > 0) {\n\t\tret = arena_slab_reg_alloc(tsdn, slab, &arena_bin_info[binind]);\n\t} else {\n\t\tret = arena_bin_malloc_hard(tsdn, arena, bin, binind);\n\t}\n\n\tif (ret == NULL) {\n\t\tmalloc_mutex_unlock(tsdn, &bin->lock);\n\t\treturn NULL;\n\t}\n\n\tif (config_stats) {\n\t\tbin->stats.nmalloc++;\n\t\tbin->stats.nrequests++;\n\t\tbin->stats.curregs++;\n\t}\n\tmalloc_mutex_unlock(tsdn, &bin->lock);\n\tif (config_prof && arena_prof_accum(tsdn, arena, usize)) {\n\t\tprof_idump(tsdn);\n\t}\n\n\tif (!zero) {\n\t\tif (config_fill) {\n\t\t\tif (unlikely(opt_junk_alloc)) {\n\t\t\t\tarena_alloc_junk_small(ret,\n\t\t\t\t    &arena_bin_info[binind], false);\n\t\t\t} else if (unlikely(opt_zero)) {\n\t\t\t\tmemset(ret, 0, usize);\n\t\t\t}\n\t\t}\n\t} else {\n\t\tif (config_fill && unlikely(opt_junk_alloc)) {\n\t\t\tarena_alloc_junk_small(ret, &arena_bin_info[binind],\n\t\t\t    true);\n\t\t}\n\t\tmemset(ret, 0, usize);\n\t}\n\n\tarena_decay_tick(tsdn, arena);\n\treturn ret;\n}\n\nvoid *\narena_malloc_hard(tsdn_t *tsdn, arena_t *arena, size_t size, szind_t ind,\n    bool zero) {\n\tassert(!tsdn_null(tsdn) || arena != NULL);\n\n\tif (likely(!tsdn_null(tsdn))) {\n\t\tarena = arena_choose(tsdn_tsd(tsdn), arena);\n\t}\n\tif (unlikely(arena == NULL)) {\n\t\treturn NULL;\n\t}\n\n\tif (likely(size <= SMALL_MAXCLASS)) {\n\t\treturn arena_malloc_small(tsdn, arena, ind, zero);\n\t}\n\treturn large_malloc(tsdn, arena, sz_index2size(ind), zero);\n}\n\nvoid *\narena_palloc(tsdn_t *tsdn, arena_t *arena, size_t usize, size_t alignment,\n    bool zero, tcache_t *tcache) {\n\tvoid *ret;\n\n\tif (usize <= SMALL_MAXCLASS && (alignment < PAGE || (alignment == PAGE\n\t    && (usize & PAGE_MASK) == 0))) {\n\t\t/* Small; alignment doesn't require special slab placement. */\n\t\tret = arena_malloc(tsdn, arena, usize, sz_size2index(usize),\n\t\t    zero, tcache, true);\n\t} else {\n\t\tif (likely(alignment <= CACHELINE)) {\n\t\t\tret = large_malloc(tsdn, arena, usize, zero);\n\t\t} else {\n\t\t\tret = large_palloc(tsdn, arena, usize, alignment, zero);\n\t\t}\n\t}\n\treturn ret;\n}\n\nvoid\narena_prof_promote(tsdn_t *tsdn, const void *ptr, size_t usize) {\n\tcassert(config_prof);\n\tassert(ptr != NULL);\n\tassert(isalloc(tsdn, ptr) == LARGE_MINCLASS);\n\tassert(usize <= SMALL_MAXCLASS);\n\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\n\textent_t *extent = rtree_extent_read(tsdn, &extents_rtree, rtree_ctx,\n\t    (uintptr_t)ptr, true);\n\tarena_t *arena = extent_arena_get(extent);\n\n\tszind_t szind = sz_size2index(usize);\n\textent_szind_set(extent, szind);\n\trtree_szind_slab_update(tsdn, &extents_rtree, rtree_ctx, (uintptr_t)ptr,\n\t    szind, false);\n\n\tprof_accum_cancel(tsdn, &arena->prof_accum, usize);\n\n\tassert(isalloc(tsdn, ptr) == usize);\n}\n\nstatic size_t\narena_prof_demote(tsdn_t *tsdn, extent_t *extent, const void *ptr) {\n\tcassert(config_prof);\n\tassert(ptr != NULL);\n\n\textent_szind_set(extent, NBINS);\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\trtree_szind_slab_update(tsdn, &extents_rtree, rtree_ctx, (uintptr_t)ptr,\n\t    NBINS, false);\n\n\tassert(isalloc(tsdn, ptr) == LARGE_MINCLASS);\n\n\treturn LARGE_MINCLASS;\n}\n\nvoid\narena_dalloc_promoted(tsdn_t *tsdn, void *ptr, tcache_t *tcache,\n    bool slow_path) {\n\tcassert(config_prof);\n\tassert(opt_prof);\n\n\textent_t *extent = iealloc(tsdn, ptr);\n\tsize_t usize = arena_prof_demote(tsdn, extent, ptr);\n\tif (usize <= tcache_maxclass) {\n\t\ttcache_dalloc_large(tsdn_tsd(tsdn), tcache, ptr,\n\t\t    sz_size2index(usize), slow_path);\n\t} else {\n\t\tlarge_dalloc(tsdn, extent);\n\t}\n}\n\nstatic void\narena_dissociate_bin_slab(arena_t *arena, extent_t *slab, arena_bin_t *bin) {\n\t/* Dissociate slab from bin. */\n\tif (slab == bin->slabcur) {\n\t\tbin->slabcur = NULL;\n\t} else {\n\t\tszind_t binind = extent_szind_get(slab);\n\t\tconst arena_bin_info_t *bin_info = &arena_bin_info[binind];\n\n\t\t/*\n\t\t * The following block's conditional is necessary because if the\n\t\t * slab only contains one region, then it never gets inserted\n\t\t * into the non-full slabs heap.\n\t\t */\n\t\tif (bin_info->nregs == 1) {\n\t\t\tarena_bin_slabs_full_remove(arena, bin, slab);\n\t\t} else {\n\t\t\tarena_bin_slabs_nonfull_remove(bin, slab);\n\t\t}\n\t}\n}\n\nstatic void\narena_dalloc_bin_slab(tsdn_t *tsdn, arena_t *arena, extent_t *slab,\n    arena_bin_t *bin) {\n\tassert(slab != bin->slabcur);\n\n\tmalloc_mutex_unlock(tsdn, &bin->lock);\n\t/******************************/\n\tarena_slab_dalloc(tsdn, arena, slab);\n\t/****************************/\n\tmalloc_mutex_lock(tsdn, &bin->lock);\n\tif (config_stats) {\n\t\tbin->stats.curslabs--;\n\t}\n}\n\nstatic void\narena_bin_lower_slab(tsdn_t *tsdn, arena_t *arena, extent_t *slab,\n    arena_bin_t *bin) {\n\tassert(extent_nfree_get(slab) > 0);\n\n\t/*\n\t * Make sure that if bin->slabcur is non-NULL, it refers to the\n\t * oldest/lowest non-full slab.  It is okay to NULL slabcur out rather\n\t * than proactively keeping it pointing at the oldest/lowest non-full\n\t * slab.\n\t */\n\tif (bin->slabcur != NULL && extent_snad_comp(bin->slabcur, slab) > 0) {\n\t\t/* Switch slabcur. */\n\t\tif (extent_nfree_get(bin->slabcur) > 0) {\n\t\t\tarena_bin_slabs_nonfull_insert(bin, bin->slabcur);\n\t\t} else {\n\t\t\tarena_bin_slabs_full_insert(arena, bin, bin->slabcur);\n\t\t}\n\t\tbin->slabcur = slab;\n\t\tif (config_stats) {\n\t\t\tbin->stats.reslabs++;\n\t\t}\n\t} else {\n\t\tarena_bin_slabs_nonfull_insert(bin, slab);\n\t}\n}\n\nstatic void\narena_dalloc_bin_locked_impl(tsdn_t *tsdn, arena_t *arena, extent_t *slab,\n    void *ptr, bool junked) {\n\tarena_slab_data_t *slab_data = extent_slab_data_get(slab);\n\tszind_t binind = extent_szind_get(slab);\n\tarena_bin_t *bin = &arena->bins[binind];\n\tconst arena_bin_info_t *bin_info = &arena_bin_info[binind];\n\n\tif (!junked && config_fill && unlikely(opt_junk_free)) {\n\t\tarena_dalloc_junk_small(ptr, bin_info);\n\t}\n\n\tarena_slab_reg_dalloc(tsdn, slab, slab_data, ptr);\n\tunsigned nfree = extent_nfree_get(slab);\n\tif (nfree == bin_info->nregs) {\n\t\tarena_dissociate_bin_slab(arena, slab, bin);\n\t\tarena_dalloc_bin_slab(tsdn, arena, slab, bin);\n\t} else if (nfree == 1 && slab != bin->slabcur) {\n\t\tarena_bin_slabs_full_remove(arena, bin, slab);\n\t\tarena_bin_lower_slab(tsdn, arena, slab, bin);\n\t}\n\n\tif (config_stats) {\n\t\tbin->stats.ndalloc++;\n\t\tbin->stats.curregs--;\n\t}\n}\n\nvoid\narena_dalloc_bin_junked_locked(tsdn_t *tsdn, arena_t *arena, extent_t *extent,\n    void *ptr) {\n\tarena_dalloc_bin_locked_impl(tsdn, arena, extent, ptr, true);\n}\n\nstatic void\narena_dalloc_bin(tsdn_t *tsdn, arena_t *arena, extent_t *extent, void *ptr) {\n\tszind_t binind = extent_szind_get(extent);\n\tarena_bin_t *bin = &arena->bins[binind];\n\n\tmalloc_mutex_lock(tsdn, &bin->lock);\n\tarena_dalloc_bin_locked_impl(tsdn, arena, extent, ptr, false);\n\tmalloc_mutex_unlock(tsdn, &bin->lock);\n}\n\nvoid\narena_dalloc_small(tsdn_t *tsdn, void *ptr) {\n\textent_t *extent = iealloc(tsdn, ptr);\n\tarena_t *arena = extent_arena_get(extent);\n\n\tarena_dalloc_bin(tsdn, arena, extent, ptr);\n\tarena_decay_tick(tsdn, arena);\n}\n\nbool\narena_ralloc_no_move(tsdn_t *tsdn, void *ptr, size_t oldsize, size_t size,\n    size_t extra, bool zero) {\n\t/* Calls with non-zero extra had to clamp extra. */\n\tassert(extra == 0 || size + extra <= LARGE_MAXCLASS);\n\n\tif (unlikely(size > LARGE_MAXCLASS)) {\n\t\treturn true;\n\t}\n\n\textent_t *extent = iealloc(tsdn, ptr);\n\tsize_t usize_min = sz_s2u(size);\n\tsize_t usize_max = sz_s2u(size + extra);\n\tif (likely(oldsize <= SMALL_MAXCLASS && usize_min <= SMALL_MAXCLASS)) {\n\t\t/*\n\t\t * Avoid moving the allocation if the size class can be left the\n\t\t * same.\n\t\t */\n\t\tassert(arena_bin_info[sz_size2index(oldsize)].reg_size ==\n\t\t    oldsize);\n\t\tif ((usize_max > SMALL_MAXCLASS || sz_size2index(usize_max) !=\n\t\t    sz_size2index(oldsize)) && (size > oldsize || usize_max <\n\t\t    oldsize)) {\n\t\t\treturn true;\n\t\t}\n\n\t\tarena_decay_tick(tsdn, extent_arena_get(extent));\n\t\treturn false;\n\t} else if (oldsize >= LARGE_MINCLASS && usize_max >= LARGE_MINCLASS) {\n\t\treturn large_ralloc_no_move(tsdn, extent, usize_min, usize_max,\n\t\t    zero);\n\t}\n\n\treturn true;\n}\n\nstatic void *\narena_ralloc_move_helper(tsdn_t *tsdn, arena_t *arena, size_t usize,\n    size_t alignment, bool zero, tcache_t *tcache) {\n\tif (alignment == 0) {\n\t\treturn arena_malloc(tsdn, arena, usize, sz_size2index(usize),\n\t\t    zero, tcache, true);\n\t}\n\tusize = sz_sa2u(usize, alignment);\n\tif (unlikely(usize == 0 || usize > LARGE_MAXCLASS)) {\n\t\treturn NULL;\n\t}\n\treturn ipalloct(tsdn, usize, alignment, zero, tcache, arena);\n}\n\nvoid *\narena_ralloc(tsdn_t *tsdn, arena_t *arena, void *ptr, size_t oldsize,\n    size_t size, size_t alignment, bool zero, tcache_t *tcache) {\n\tsize_t usize = sz_s2u(size);\n\tif (unlikely(usize == 0 || size > LARGE_MAXCLASS)) {\n\t\treturn NULL;\n\t}\n\n\tif (likely(usize <= SMALL_MAXCLASS)) {\n\t\t/* Try to avoid moving the allocation. */\n\t\tif (!arena_ralloc_no_move(tsdn, ptr, oldsize, usize, 0, zero)) {\n\t\t\treturn ptr;\n\t\t}\n\t}\n\n\tif (oldsize >= LARGE_MINCLASS && usize >= LARGE_MINCLASS) {\n\t\treturn large_ralloc(tsdn, arena, iealloc(tsdn, ptr), usize,\n\t\t    alignment, zero, tcache);\n\t}\n\n\t/*\n\t * size and oldsize are different enough that we need to move the\n\t * object.  In that case, fall back to allocating new space and copying.\n\t */\n\tvoid *ret = arena_ralloc_move_helper(tsdn, arena, usize, alignment,\n\t    zero, tcache);\n\tif (ret == NULL) {\n\t\treturn NULL;\n\t}\n\n\t/*\n\t * Junk/zero-filling were already done by\n\t * ipalloc()/arena_malloc().\n\t */\n\n\tsize_t copysize = (usize < oldsize) ? usize : oldsize;\n\tmemcpy(ret, ptr, copysize);\n\tisdalloct(tsdn, ptr, oldsize, tcache, NULL, true);\n\treturn ret;\n}\n\ndss_prec_t\narena_dss_prec_get(arena_t *arena) {\n\treturn (dss_prec_t)atomic_load_u(&arena->dss_prec, ATOMIC_ACQUIRE);\n}\n\nbool\narena_dss_prec_set(arena_t *arena, dss_prec_t dss_prec) {\n\tif (!have_dss) {\n\t\treturn (dss_prec != dss_prec_disabled);\n\t}\n\tatomic_store_u(&arena->dss_prec, (unsigned)dss_prec, ATOMIC_RELEASE);\n\treturn false;\n}\n\nssize_t\narena_dirty_decay_ms_default_get(void) {\n\treturn atomic_load_zd(&dirty_decay_ms_default, ATOMIC_RELAXED);\n}\n\nbool\narena_dirty_decay_ms_default_set(ssize_t decay_ms) {\n\tif (!arena_decay_ms_valid(decay_ms)) {\n\t\treturn true;\n\t}\n\tatomic_store_zd(&dirty_decay_ms_default, decay_ms, ATOMIC_RELAXED);\n\treturn false;\n}\n\nssize_t\narena_muzzy_decay_ms_default_get(void) {\n\treturn atomic_load_zd(&muzzy_decay_ms_default, ATOMIC_RELAXED);\n}\n\nbool\narena_muzzy_decay_ms_default_set(ssize_t decay_ms) {\n\tif (!arena_decay_ms_valid(decay_ms)) {\n\t\treturn true;\n\t}\n\tatomic_store_zd(&muzzy_decay_ms_default, decay_ms, ATOMIC_RELAXED);\n\treturn false;\n}\n\nunsigned\narena_nthreads_get(arena_t *arena, bool internal) {\n\treturn atomic_load_u(&arena->nthreads[internal], ATOMIC_RELAXED);\n}\n\nvoid\narena_nthreads_inc(arena_t *arena, bool internal) {\n\tatomic_fetch_add_u(&arena->nthreads[internal], 1, ATOMIC_RELAXED);\n}\n\nvoid\narena_nthreads_dec(arena_t *arena, bool internal) {\n\tatomic_fetch_sub_u(&arena->nthreads[internal], 1, ATOMIC_RELAXED);\n}\n\nsize_t\narena_extent_sn_next(arena_t *arena) {\n\treturn atomic_fetch_add_zu(&arena->extent_sn_next, 1, ATOMIC_RELAXED);\n}\n\narena_t *\narena_new(tsdn_t *tsdn, unsigned ind, extent_hooks_t *extent_hooks) {\n\tarena_t *arena;\n\tbase_t *base;\n\tunsigned i;\n\n\tif (ind == 0) {\n\t\tbase = b0get();\n\t} else {\n\t\tbase = base_new(tsdn, ind, extent_hooks);\n\t\tif (base == NULL) {\n\t\t\treturn NULL;\n\t\t}\n\t}\n\n\tarena = (arena_t *)base_alloc(tsdn, base, sizeof(arena_t), CACHELINE);\n\tif (arena == NULL) {\n\t\tgoto label_error;\n\t}\n\n\tatomic_store_u(&arena->nthreads[0], 0, ATOMIC_RELAXED);\n\tatomic_store_u(&arena->nthreads[1], 0, ATOMIC_RELAXED);\n\tarena->last_thd = NULL;\n\n\tif (config_stats) {\n\t\tif (arena_stats_init(tsdn, &arena->stats)) {\n\t\t\tgoto label_error;\n\t\t}\n\n\t\tql_new(&arena->tcache_ql);\n\t\tif (malloc_mutex_init(&arena->tcache_ql_mtx, \"tcache_ql\",\n\t\t    WITNESS_RANK_TCACHE_QL, malloc_mutex_rank_exclusive)) {\n\t\t\tgoto label_error;\n\t\t}\n\t}\n\n\tif (config_prof) {\n\t\tif (prof_accum_init(tsdn, &arena->prof_accum)) {\n\t\t\tgoto label_error;\n\t\t}\n\t}\n\n\tif (config_cache_oblivious) {\n\t\t/*\n\t\t * A nondeterministic seed based on the address of arena reduces\n\t\t * the likelihood of lockstep non-uniform cache index\n\t\t * utilization among identical concurrent processes, but at the\n\t\t * cost of test repeatability.  For debug builds, instead use a\n\t\t * deterministic seed.\n\t\t */\n\t\tatomic_store_zu(&arena->offset_state, config_debug ? ind :\n\t\t    (size_t)(uintptr_t)arena, ATOMIC_RELAXED);\n\t}\n\n\tatomic_store_zu(&arena->extent_sn_next, 0, ATOMIC_RELAXED);\n\n\tatomic_store_u(&arena->dss_prec, (unsigned)extent_dss_prec_get(),\n\t    ATOMIC_RELAXED);\n\n\tatomic_store_zu(&arena->nactive, 0, ATOMIC_RELAXED);\n\n\textent_list_init(&arena->large);\n\tif (malloc_mutex_init(&arena->large_mtx, \"arena_large\",\n\t    WITNESS_RANK_ARENA_LARGE, malloc_mutex_rank_exclusive)) {\n\t\tgoto label_error;\n\t}\n\n\t/*\n\t * Delay coalescing for dirty extents despite the disruptive effect on\n\t * memory layout for best-fit extent allocation, since cached extents\n\t * are likely to be reused soon after deallocation, and the cost of\n\t * merging/splitting extents is non-trivial.\n\t */\n\tif (extents_init(tsdn, &arena->extents_dirty, extent_state_dirty,\n\t    true)) {\n\t\tgoto label_error;\n\t}\n\t/*\n\t * Coalesce muzzy extents immediately, because operations on them are in\n\t * the critical path much less often than for dirty extents.\n\t */\n\tif (extents_init(tsdn, &arena->extents_muzzy, extent_state_muzzy,\n\t    false)) {\n\t\tgoto label_error;\n\t}\n\t/*\n\t * Coalesce retained extents immediately, in part because they will\n\t * never be evicted (and therefore there's no opportunity for delayed\n\t * coalescing), but also because operations on retained extents are not\n\t * in the critical path.\n\t */\n\tif (extents_init(tsdn, &arena->extents_retained, extent_state_retained,\n\t    false)) {\n\t\tgoto label_error;\n\t}\n\n\tif (arena_decay_init(&arena->decay_dirty, &arena->extents_dirty,\n\t    arena_dirty_decay_ms_default_get(), &arena->stats.decay_dirty)) {\n\t\tgoto label_error;\n\t}\n\tif (arena_decay_init(&arena->decay_muzzy, &arena->extents_muzzy,\n\t    arena_muzzy_decay_ms_default_get(), &arena->stats.decay_muzzy)) {\n\t\tgoto label_error;\n\t}\n\n\tarena->extent_grow_next = sz_psz2ind(HUGEPAGE);\n\tif (malloc_mutex_init(&arena->extent_grow_mtx, \"extent_grow\",\n\t    WITNESS_RANK_EXTENT_GROW, malloc_mutex_rank_exclusive)) {\n\t\tgoto label_error;\n\t}\n\n\textent_avail_new(&arena->extent_avail);\n\tif (malloc_mutex_init(&arena->extent_avail_mtx, \"extent_avail\",\n\t    WITNESS_RANK_EXTENT_AVAIL, malloc_mutex_rank_exclusive)) {\n\t\tgoto label_error;\n\t}\n\n\t/* Initialize bins. */\n\tfor (i = 0; i < NBINS; i++) {\n\t\tarena_bin_t *bin = &arena->bins[i];\n\t\tif (malloc_mutex_init(&bin->lock, \"arena_bin\",\n\t\t    WITNESS_RANK_ARENA_BIN, malloc_mutex_rank_exclusive)) {\n\t\t\tgoto label_error;\n\t\t}\n\t\tbin->slabcur = NULL;\n\t\textent_heap_new(&bin->slabs_nonfull);\n\t\textent_list_init(&bin->slabs_full);\n\t\tif (config_stats) {\n\t\t\tmemset(&bin->stats, 0, sizeof(malloc_bin_stats_t));\n\t\t}\n\t}\n\n\tarena->base = base;\n\t/* Set arena before creating background threads. */\n\tarena_set(ind, arena);\n\n\tnstime_init(&arena->create_time, 0);\n\tnstime_update(&arena->create_time);\n\n\t/* We don't support reentrancy for arena 0 bootstrapping. */\n\tif (ind != 0) {\n\t\t/*\n\t\t * If we're here, then arena 0 already exists, so bootstrapping\n\t\t * is done enough that we should have tsd.\n\t\t */\n\t\tassert(!tsdn_null(tsdn));\n\t\tpre_reentrancy(tsdn_tsd(tsdn));\n\t\tif (hooks_arena_new_hook) {\n\t\t\thooks_arena_new_hook();\n\t\t}\n\t\tpost_reentrancy(tsdn_tsd(tsdn));\n\t}\n\n\treturn arena;\nlabel_error:\n\tif (ind != 0) {\n\t\tbase_delete(base);\n\t}\n\treturn NULL;\n}\n\nvoid\narena_boot(void) {\n\tarena_dirty_decay_ms_default_set(opt_dirty_decay_ms);\n\tarena_muzzy_decay_ms_default_set(opt_muzzy_decay_ms);\n}\n\nvoid\narena_prefork0(tsdn_t *tsdn, arena_t *arena) {\n\tmalloc_mutex_prefork(tsdn, &arena->decay_dirty.mtx);\n\tmalloc_mutex_prefork(tsdn, &arena->decay_muzzy.mtx);\n}\n\nvoid\narena_prefork1(tsdn_t *tsdn, arena_t *arena) {\n\tif (config_stats) {\n\t\tmalloc_mutex_prefork(tsdn, &arena->tcache_ql_mtx);\n\t}\n}\n\nvoid\narena_prefork2(tsdn_t *tsdn, arena_t *arena) {\n\textents_prefork(tsdn, &arena->extents_dirty);\n\textents_prefork(tsdn, &arena->extents_muzzy);\n\textents_prefork(tsdn, &arena->extents_retained);\n}\n\nvoid\narena_prefork3(tsdn_t *tsdn, arena_t *arena) {\n\tmalloc_mutex_prefork(tsdn, &arena->extent_avail_mtx);\n}\n\nvoid\narena_prefork4(tsdn_t *tsdn, arena_t *arena) {\n\tbase_prefork(tsdn, arena->base);\n}\n\nvoid\narena_prefork5(tsdn_t *tsdn, arena_t *arena) {\n\tmalloc_mutex_prefork(tsdn, &arena->large_mtx);\n}\n\nvoid\narena_prefork6(tsdn_t *tsdn, arena_t *arena) {\n\tfor (unsigned i = 0; i < NBINS; i++) {\n\t\tmalloc_mutex_prefork(tsdn, &arena->bins[i].lock);\n\t}\n}\n\nvoid\narena_postfork_parent(tsdn_t *tsdn, arena_t *arena) {\n\tunsigned i;\n\n\tfor (i = 0; i < NBINS; i++) {\n\t\tmalloc_mutex_postfork_parent(tsdn, &arena->bins[i].lock);\n\t}\n\tmalloc_mutex_postfork_parent(tsdn, &arena->large_mtx);\n\tbase_postfork_parent(tsdn, arena->base);\n\tmalloc_mutex_postfork_parent(tsdn, &arena->extent_avail_mtx);\n\textents_postfork_parent(tsdn, &arena->extents_dirty);\n\textents_postfork_parent(tsdn, &arena->extents_muzzy);\n\textents_postfork_parent(tsdn, &arena->extents_retained);\n\tmalloc_mutex_postfork_parent(tsdn, &arena->decay_dirty.mtx);\n\tmalloc_mutex_postfork_parent(tsdn, &arena->decay_muzzy.mtx);\n\tif (config_stats) {\n\t\tmalloc_mutex_postfork_parent(tsdn, &arena->tcache_ql_mtx);\n\t}\n}\n\nvoid\narena_postfork_child(tsdn_t *tsdn, arena_t *arena) {\n\tunsigned i;\n\n\tatomic_store_u(&arena->nthreads[0], 0, ATOMIC_RELAXED);\n\tatomic_store_u(&arena->nthreads[1], 0, ATOMIC_RELAXED);\n\tif (tsd_arena_get(tsdn_tsd(tsdn)) == arena) {\n\t\tarena_nthreads_inc(arena, false);\n\t}\n\tif (tsd_iarena_get(tsdn_tsd(tsdn)) == arena) {\n\t\tarena_nthreads_inc(arena, true);\n\t}\n\tif (config_stats) {\n\t\tql_new(&arena->tcache_ql);\n\t\ttcache_t *tcache = tcache_get(tsdn_tsd(tsdn));\n\t\tif (tcache != NULL && tcache->arena == arena) {\n\t\t\tql_elm_new(tcache, link);\n\t\t\tql_tail_insert(&arena->tcache_ql, tcache, link);\n\t\t}\n\t}\n\n\tfor (i = 0; i < NBINS; i++) {\n\t\tmalloc_mutex_postfork_child(tsdn, &arena->bins[i].lock);\n\t}\n\tmalloc_mutex_postfork_child(tsdn, &arena->large_mtx);\n\tbase_postfork_child(tsdn, arena->base);\n\tmalloc_mutex_postfork_child(tsdn, &arena->extent_avail_mtx);\n\textents_postfork_child(tsdn, &arena->extents_dirty);\n\textents_postfork_child(tsdn, &arena->extents_muzzy);\n\textents_postfork_child(tsdn, &arena->extents_retained);\n\tmalloc_mutex_postfork_child(tsdn, &arena->decay_dirty.mtx);\n\tmalloc_mutex_postfork_child(tsdn, &arena->decay_muzzy.mtx);\n\tif (config_stats) {\n\t\tmalloc_mutex_postfork_child(tsdn, &arena->tcache_ql_mtx);\n\t}\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/background_thread.c",
    "content": "#define JEMALLOC_BACKGROUND_THREAD_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n\n/******************************************************************************/\n/* Data. */\n\n/* This option should be opt-in only. */\n#define BACKGROUND_THREAD_DEFAULT false\n/* Read-only after initialization. */\nbool opt_background_thread = BACKGROUND_THREAD_DEFAULT;\n\n/* Used for thread creation, termination and stats. */\nmalloc_mutex_t background_thread_lock;\n/* Indicates global state.  Atomic because decay reads this w/o locking. */\natomic_b_t background_thread_enabled_state;\nsize_t n_background_threads;\n/* Thread info per-index. */\nbackground_thread_info_t *background_thread_info;\n\n/* False if no necessary runtime support. */\nbool can_enable_background_thread;\n\n/******************************************************************************/\n\n#ifdef JEMALLOC_PTHREAD_CREATE_WRAPPER\n#include <dlfcn.h>\n\nstatic int (*pthread_create_fptr)(pthread_t *__restrict, const pthread_attr_t *,\n    void *(*)(void *), void *__restrict);\nstatic pthread_once_t once_control = PTHREAD_ONCE_INIT;\n\nstatic void\npthread_create_wrapper_once(void) {\n#ifdef JEMALLOC_LAZY_LOCK\n\tisthreaded = true;\n#endif\n}\n\nint\npthread_create_wrapper(pthread_t *__restrict thread, const pthread_attr_t *attr,\n    void *(*start_routine)(void *), void *__restrict arg) {\n\tpthread_once(&once_control, pthread_create_wrapper_once);\n\n\treturn pthread_create_fptr(thread, attr, start_routine, arg);\n}\n#endif /* JEMALLOC_PTHREAD_CREATE_WRAPPER */\n\n#ifndef JEMALLOC_BACKGROUND_THREAD\n#define NOT_REACHED { not_reached(); }\nbool background_thread_create(tsd_t *tsd, unsigned arena_ind) NOT_REACHED\nbool background_threads_enable(tsd_t *tsd) NOT_REACHED\nbool background_threads_disable(tsd_t *tsd) NOT_REACHED\nvoid background_thread_interval_check(tsdn_t *tsdn, arena_t *arena,\n    arena_decay_t *decay, size_t npages_new) NOT_REACHED\nvoid background_thread_prefork0(tsdn_t *tsdn) NOT_REACHED\nvoid background_thread_prefork1(tsdn_t *tsdn) NOT_REACHED\nvoid background_thread_postfork_parent(tsdn_t *tsdn) NOT_REACHED\nvoid background_thread_postfork_child(tsdn_t *tsdn) NOT_REACHED\nbool background_thread_stats_read(tsdn_t *tsdn,\n    background_thread_stats_t *stats) NOT_REACHED\nvoid background_thread_ctl_init(tsdn_t *tsdn) NOT_REACHED\n#undef NOT_REACHED\n#else\n\nstatic bool background_thread_enabled_at_fork;\n\nstatic void\nbackground_thread_info_init(tsdn_t *tsdn, background_thread_info_t *info) {\n\tbackground_thread_wakeup_time_set(tsdn, info, 0);\n\tinfo->npages_to_purge_new = 0;\n\tif (config_stats) {\n\t\tinfo->tot_n_runs = 0;\n\t\tnstime_init(&info->tot_sleep_time, 0);\n\t}\n}\n\nstatic inline bool\nset_current_thread_affinity(UNUSED int cpu) {\n#if defined(JEMALLOC_HAVE_SCHED_SETAFFINITY)\n\tcpu_set_t cpuset;\n\tCPU_ZERO(&cpuset);\n\tCPU_SET(cpu, &cpuset);\n\tint ret = sched_setaffinity(0, sizeof(cpu_set_t), &cpuset);\n\n\treturn (ret != 0);\n#else\n\treturn false;\n#endif\n}\n\n/* Threshold for determining when to wake up the background thread. */\n#define BACKGROUND_THREAD_NPAGES_THRESHOLD UINT64_C(1024)\n#define BILLION UINT64_C(1000000000)\n/* Minimal sleep interval 100 ms. */\n#define BACKGROUND_THREAD_MIN_INTERVAL_NS (BILLION / 10)\n\nstatic inline size_t\ndecay_npurge_after_interval(arena_decay_t *decay, size_t interval) {\n\tsize_t i;\n\tuint64_t sum = 0;\n\tfor (i = 0; i < interval; i++) {\n\t\tsum += decay->backlog[i] * h_steps[i];\n\t}\n\tfor (; i < SMOOTHSTEP_NSTEPS; i++) {\n\t\tsum += decay->backlog[i] * (h_steps[i] - h_steps[i - interval]);\n\t}\n\n\treturn (size_t)(sum >> SMOOTHSTEP_BFP);\n}\n\nstatic uint64_t\narena_decay_compute_purge_interval_impl(tsdn_t *tsdn, arena_decay_t *decay,\n    extents_t *extents) {\n\tif (malloc_mutex_trylock(tsdn, &decay->mtx)) {\n\t\t/* Use minimal interval if decay is contended. */\n\t\treturn BACKGROUND_THREAD_MIN_INTERVAL_NS;\n\t}\n\n\tuint64_t interval;\n\tssize_t decay_time = atomic_load_zd(&decay->time_ms, ATOMIC_RELAXED);\n\tif (decay_time <= 0) {\n\t\t/* Purging is eagerly done or disabled currently. */\n\t\tinterval = BACKGROUND_THREAD_INDEFINITE_SLEEP;\n\t\tgoto label_done;\n\t}\n\n\tuint64_t decay_interval_ns = nstime_ns(&decay->interval);\n\tassert(decay_interval_ns > 0);\n\tsize_t npages = extents_npages_get(extents);\n\tif (npages == 0) {\n\t\tunsigned i;\n\t\tfor (i = 0; i < SMOOTHSTEP_NSTEPS; i++) {\n\t\t\tif (decay->backlog[i] > 0) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif (i == SMOOTHSTEP_NSTEPS) {\n\t\t\t/* No dirty pages recorded.  Sleep indefinitely. */\n\t\t\tinterval = BACKGROUND_THREAD_INDEFINITE_SLEEP;\n\t\t\tgoto label_done;\n\t\t}\n\t}\n\tif (npages <= BACKGROUND_THREAD_NPAGES_THRESHOLD) {\n\t\t/* Use max interval. */\n\t\tinterval = decay_interval_ns * SMOOTHSTEP_NSTEPS;\n\t\tgoto label_done;\n\t}\n\n\tsize_t lb = BACKGROUND_THREAD_MIN_INTERVAL_NS / decay_interval_ns;\n\tsize_t ub = SMOOTHSTEP_NSTEPS;\n\t/* Minimal 2 intervals to ensure reaching next epoch deadline. */\n\tlb = (lb < 2) ? 2 : lb;\n\tif ((decay_interval_ns * ub <= BACKGROUND_THREAD_MIN_INTERVAL_NS) ||\n\t    (lb + 2 > ub)) {\n\t\tinterval = BACKGROUND_THREAD_MIN_INTERVAL_NS;\n\t\tgoto label_done;\n\t}\n\n\tassert(lb + 2 <= ub);\n\tsize_t npurge_lb, npurge_ub;\n\tnpurge_lb = decay_npurge_after_interval(decay, lb);\n\tif (npurge_lb > BACKGROUND_THREAD_NPAGES_THRESHOLD) {\n\t\tinterval = decay_interval_ns * lb;\n\t\tgoto label_done;\n\t}\n\tnpurge_ub = decay_npurge_after_interval(decay, ub);\n\tif (npurge_ub < BACKGROUND_THREAD_NPAGES_THRESHOLD) {\n\t\tinterval = decay_interval_ns * ub;\n\t\tgoto label_done;\n\t}\n\n\tunsigned n_search = 0;\n\tsize_t target, npurge;\n\twhile ((npurge_lb + BACKGROUND_THREAD_NPAGES_THRESHOLD < npurge_ub)\n\t    && (lb + 2 < ub)) {\n\t\ttarget = (lb + ub) / 2;\n\t\tnpurge = decay_npurge_after_interval(decay, target);\n\t\tif (npurge > BACKGROUND_THREAD_NPAGES_THRESHOLD) {\n\t\t\tub = target;\n\t\t\tnpurge_ub = npurge;\n\t\t} else {\n\t\t\tlb = target;\n\t\t\tnpurge_lb = npurge;\n\t\t}\n\t\tassert(n_search++ < lg_floor(SMOOTHSTEP_NSTEPS) + 1);\n\t}\n\tinterval = decay_interval_ns * (ub + lb) / 2;\nlabel_done:\n\tinterval = (interval < BACKGROUND_THREAD_MIN_INTERVAL_NS) ?\n\t    BACKGROUND_THREAD_MIN_INTERVAL_NS : interval;\n\tmalloc_mutex_unlock(tsdn, &decay->mtx);\n\n\treturn interval;\n}\n\n/* Compute purge interval for background threads. */\nstatic uint64_t\narena_decay_compute_purge_interval(tsdn_t *tsdn, arena_t *arena) {\n\tuint64_t i1, i2;\n\ti1 = arena_decay_compute_purge_interval_impl(tsdn, &arena->decay_dirty,\n\t    &arena->extents_dirty);\n\tif (i1 == BACKGROUND_THREAD_MIN_INTERVAL_NS) {\n\t\treturn i1;\n\t}\n\ti2 = arena_decay_compute_purge_interval_impl(tsdn, &arena->decay_muzzy,\n\t    &arena->extents_muzzy);\n\n\treturn i1 < i2 ? i1 : i2;\n}\n\nstatic void\nbackground_thread_sleep(tsdn_t *tsdn, background_thread_info_t *info,\n    uint64_t interval) {\n\tif (config_stats) {\n\t\tinfo->tot_n_runs++;\n\t}\n\tinfo->npages_to_purge_new = 0;\n\n\tstruct timeval tv;\n\t/* Specific clock required by timedwait. */\n\tgettimeofday(&tv, NULL);\n\tnstime_t before_sleep;\n\tnstime_init2(&before_sleep, tv.tv_sec, tv.tv_usec * 1000);\n\n\tint ret;\n\tif (interval == BACKGROUND_THREAD_INDEFINITE_SLEEP) {\n\t\tassert(background_thread_indefinite_sleep(info));\n\t\tret = pthread_cond_wait(&info->cond, &info->mtx.lock);\n\t\tassert(ret == 0);\n\t} else {\n\t\tassert(interval >= BACKGROUND_THREAD_MIN_INTERVAL_NS &&\n\t\t    interval <= BACKGROUND_THREAD_INDEFINITE_SLEEP);\n\t\t/* We need malloc clock (can be different from tv). */\n\t\tnstime_t next_wakeup;\n\t\tnstime_init(&next_wakeup, 0);\n\t\tnstime_update(&next_wakeup);\n\t\tnstime_iadd(&next_wakeup, interval);\n\t\tassert(nstime_ns(&next_wakeup) <\n\t\t    BACKGROUND_THREAD_INDEFINITE_SLEEP);\n\t\tbackground_thread_wakeup_time_set(tsdn, info,\n\t\t    nstime_ns(&next_wakeup));\n\n\t\tnstime_t ts_wakeup;\n\t\tnstime_copy(&ts_wakeup, &before_sleep);\n\t\tnstime_iadd(&ts_wakeup, interval);\n\t\tstruct timespec ts;\n\t\tts.tv_sec = (size_t)nstime_sec(&ts_wakeup);\n\t\tts.tv_nsec = (size_t)nstime_nsec(&ts_wakeup);\n\n\t\tassert(!background_thread_indefinite_sleep(info));\n\t\tret = pthread_cond_timedwait(&info->cond, &info->mtx.lock, &ts);\n\t\tassert(ret == ETIMEDOUT || ret == 0);\n\t\tbackground_thread_wakeup_time_set(tsdn, info,\n\t\t    BACKGROUND_THREAD_INDEFINITE_SLEEP);\n\t}\n\tif (config_stats) {\n\t\tgettimeofday(&tv, NULL);\n\t\tnstime_t after_sleep;\n\t\tnstime_init2(&after_sleep, tv.tv_sec, tv.tv_usec * 1000);\n\t\tif (nstime_compare(&after_sleep, &before_sleep) > 0) {\n\t\t\tnstime_subtract(&after_sleep, &before_sleep);\n\t\t\tnstime_add(&info->tot_sleep_time, &after_sleep);\n\t\t}\n\t}\n}\n\nstatic bool\nbackground_thread_pause_check(tsdn_t *tsdn, background_thread_info_t *info) {\n\tif (unlikely(info->state == background_thread_paused)) {\n\t\tmalloc_mutex_unlock(tsdn, &info->mtx);\n\t\t/* Wait on global lock to update status. */\n\t\tmalloc_mutex_lock(tsdn, &background_thread_lock);\n\t\tmalloc_mutex_unlock(tsdn, &background_thread_lock);\n\t\tmalloc_mutex_lock(tsdn, &info->mtx);\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\nstatic inline void\nbackground_work_sleep_once(tsdn_t *tsdn, background_thread_info_t *info, unsigned ind) {\n\tuint64_t min_interval = BACKGROUND_THREAD_INDEFINITE_SLEEP;\n\tunsigned narenas = narenas_total_get();\n\n\tfor (unsigned i = ind; i < narenas; i += ncpus) {\n\t\tarena_t *arena = arena_get(tsdn, i, false);\n\t\tif (!arena) {\n\t\t\tcontinue;\n\t\t}\n\t\tarena_decay(tsdn, arena, true, false);\n\t\tif (min_interval == BACKGROUND_THREAD_MIN_INTERVAL_NS) {\n\t\t\t/* Min interval will be used. */\n\t\t\tcontinue;\n\t\t}\n\t\tuint64_t interval = arena_decay_compute_purge_interval(tsdn,\n\t\t    arena);\n\t\tassert(interval >= BACKGROUND_THREAD_MIN_INTERVAL_NS);\n\t\tif (min_interval > interval) {\n\t\t\tmin_interval = interval;\n\t\t}\n\t}\n\tbackground_thread_sleep(tsdn, info, min_interval);\n}\n\nstatic bool\nbackground_threads_disable_single(tsd_t *tsd, background_thread_info_t *info) {\n\tif (info == &background_thread_info[0]) {\n\t\tmalloc_mutex_assert_owner(tsd_tsdn(tsd),\n\t\t    &background_thread_lock);\n\t} else {\n\t\tmalloc_mutex_assert_not_owner(tsd_tsdn(tsd),\n\t\t    &background_thread_lock);\n\t}\n\n\tpre_reentrancy(tsd);\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);\n\tbool has_thread;\n\tassert(info->state != background_thread_paused);\n\tif (info->state == background_thread_started) {\n\t\thas_thread = true;\n\t\tinfo->state = background_thread_stopped;\n\t\tpthread_cond_signal(&info->cond);\n\t} else {\n\t\thas_thread = false;\n\t}\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);\n\n\tif (!has_thread) {\n\t\tpost_reentrancy(tsd);\n\t\treturn false;\n\t}\n\tvoid *ret;\n\tif (pthread_join(info->thread, &ret)) {\n\t\tpost_reentrancy(tsd);\n\t\treturn true;\n\t}\n\tassert(ret == NULL);\n\tn_background_threads--;\n\tpost_reentrancy(tsd);\n\n\treturn false;\n}\n\nstatic void *background_thread_entry(void *ind_arg);\n\nstatic int\nbackground_thread_create_signals_masked(pthread_t *thread,\n    const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg) {\n\t/*\n\t * Mask signals during thread creation so that the thread inherits\n\t * an empty signal set.\n\t */\n\tsigset_t set;\n\tsigemptyset(&set);\n\tsigset_t oldset;\n\tint mask_err = pthread_sigmask(SIG_SETMASK, &set, &oldset);\n\tif (mask_err != 0) {\n\t\treturn mask_err;\n\t}\n\tint create_err = pthread_create_wrapper(thread, attr, start_routine,\n\t    arg);\n\t/*\n\t * Restore the signal mask.  Failure to restore the signal mask here\n\t * changes program behavior.\n\t */\n\tint restore_err = pthread_sigmask(SIG_SETMASK, &oldset, NULL);\n\tif (restore_err != 0) {\n\t\tmalloc_printf(\"<jemalloc>: background thread creation \"\n\t\t    \"failed (%d), and signal mask restoration failed \"\n\t\t    \"(%d)\\n\", create_err, restore_err);\n\t\tif (opt_abort) {\n\t\t\tabort();\n\t\t}\n\t}\n\treturn create_err;\n}\n\nstatic void\ncheck_background_thread_creation(tsd_t *tsd, unsigned *n_created,\n    bool *created_threads) {\n\tif (likely(*n_created == n_background_threads)) {\n\t\treturn;\n\t}\n\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &background_thread_info[0].mtx);\nlabel_restart:\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &background_thread_lock);\n\tfor (unsigned i = 1; i < ncpus; i++) {\n\t\tif (created_threads[i]) {\n\t\t\tcontinue;\n\t\t}\n\t\tbackground_thread_info_t *info = &background_thread_info[i];\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);\n\t\tassert(info->state != background_thread_paused);\n\t\tbool create = (info->state == background_thread_started);\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);\n\t\tif (!create) {\n\t\t\tcontinue;\n\t\t}\n\n\t\t/*\n\t\t * To avoid deadlock with prefork handlers (which waits for the\n\t\t * mutex held here), unlock before calling pthread_create().\n\t\t */\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &background_thread_lock);\n\n\t\tpre_reentrancy(tsd);\n\t\tint err = background_thread_create_signals_masked(&info->thread,\n\t\t    NULL, background_thread_entry, (void *)(uintptr_t)i);\n\t\tpost_reentrancy(tsd);\n\n\t\tif (err == 0) {\n\t\t\t(*n_created)++;\n\t\t\tcreated_threads[i] = true;\n\t\t} else {\n\t\t\tmalloc_printf(\"<jemalloc>: background thread \"\n\t\t\t    \"creation failed (%d)\\n\", err);\n\t\t\tif (opt_abort) {\n\t\t\t\tabort();\n\t\t\t}\n\t\t}\n\t\t/* Restart since we unlocked. */\n\t\tgoto label_restart;\n\t}\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &background_thread_info[0].mtx);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &background_thread_lock);\n}\n\nstatic void\nbackground_thread0_work(tsd_t *tsd) {\n\t/* Thread0 is also responsible for launching / terminating threads. */\n\tVARIABLE_ARRAY(bool, created_threads, ncpus);\n\tunsigned i;\n\tfor (i = 1; i < ncpus; i++) {\n\t\tcreated_threads[i] = false;\n\t}\n\t/* Start working, and create more threads when asked. */\n\tunsigned n_created = 1;\n\twhile (background_thread_info[0].state != background_thread_stopped) {\n\t\tif (background_thread_pause_check(tsd_tsdn(tsd),\n\t\t    &background_thread_info[0])) {\n\t\t\tcontinue;\n\t\t}\n\t\tcheck_background_thread_creation(tsd, &n_created,\n\t\t    (bool *)&created_threads);\n\t\tbackground_work_sleep_once(tsd_tsdn(tsd),\n\t\t    &background_thread_info[0], 0);\n\t}\n\n\t/*\n\t * Shut down other threads at exit.  Note that the ctl thread is holding\n\t * the global background_thread mutex (and is waiting) for us.\n\t */\n\tassert(!background_thread_enabled());\n\tfor (i = 1; i < ncpus; i++) {\n\t\tbackground_thread_info_t *info = &background_thread_info[i];\n\t\tassert(info->state != background_thread_paused);\n\t\tif (created_threads[i]) {\n\t\t\tbackground_threads_disable_single(tsd, info);\n\t\t} else {\n\t\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);\n\t\t\t/* Clear in case the thread wasn't created. */\n\t\t\tinfo->state = background_thread_stopped;\n\t\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);\n\t\t}\n\t}\n\tbackground_thread_info[0].state = background_thread_stopped;\n\tassert(n_background_threads == 1);\n}\n\nstatic void\nbackground_work(tsd_t *tsd, unsigned ind) {\n\tbackground_thread_info_t *info = &background_thread_info[ind];\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);\n\tbackground_thread_wakeup_time_set(tsd_tsdn(tsd), info,\n\t    BACKGROUND_THREAD_INDEFINITE_SLEEP);\n\tif (ind == 0) {\n\t\tbackground_thread0_work(tsd);\n\t} else {\n\t\twhile (info->state != background_thread_stopped) {\n\t\t\tif (background_thread_pause_check(tsd_tsdn(tsd),\n\t\t\t    info)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tbackground_work_sleep_once(tsd_tsdn(tsd), info, ind);\n\t\t}\n\t}\n\tassert(info->state == background_thread_stopped);\n\tbackground_thread_wakeup_time_set(tsd_tsdn(tsd), info, 0);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);\n}\n\nstatic void *\nbackground_thread_entry(void *ind_arg) {\n\tunsigned thread_ind = (unsigned)(uintptr_t)ind_arg;\n\tassert(thread_ind < ncpus);\n\n\tif (opt_percpu_arena != percpu_arena_disabled) {\n\t\tset_current_thread_affinity((int)thread_ind);\n\t}\n\t/*\n\t * Start periodic background work.  We use internal tsd which avoids\n\t * side effects, for example triggering new arena creation (which in\n\t * turn triggers another background thread creation).\n\t */\n\tbackground_work(tsd_internal_fetch(), thread_ind);\n\tassert(pthread_equal(pthread_self(),\n\t    background_thread_info[thread_ind].thread));\n\n\treturn NULL;\n}\n\nstatic void\nbackground_thread_init(tsd_t *tsd, background_thread_info_t *info) {\n\tmalloc_mutex_assert_owner(tsd_tsdn(tsd), &background_thread_lock);\n\tinfo->state = background_thread_started;\n\tbackground_thread_info_init(tsd_tsdn(tsd), info);\n\tn_background_threads++;\n}\n\n/* Create a new background thread if needed. */\nbool\nbackground_thread_create(tsd_t *tsd, unsigned arena_ind) {\n\tassert(have_background_thread);\n\tmalloc_mutex_assert_owner(tsd_tsdn(tsd), &background_thread_lock);\n\n\t/* We create at most NCPUs threads. */\n\tsize_t thread_ind = arena_ind % ncpus;\n\tbackground_thread_info_t *info = &background_thread_info[thread_ind];\n\n\tbool need_new_thread;\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);\n\tneed_new_thread = background_thread_enabled() &&\n\t    (info->state == background_thread_stopped);\n\tif (need_new_thread) {\n\t\tbackground_thread_init(tsd, info);\n\t}\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);\n\tif (!need_new_thread) {\n\t\treturn false;\n\t}\n\tif (arena_ind != 0) {\n\t\t/* Threads are created asynchronously by Thread 0. */\n\t\tbackground_thread_info_t *t0 = &background_thread_info[0];\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &t0->mtx);\n\t\tassert(t0->state == background_thread_started);\n\t\tpthread_cond_signal(&t0->cond);\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &t0->mtx);\n\n\t\treturn false;\n\t}\n\n\tpre_reentrancy(tsd);\n\t/*\n\t * To avoid complications (besides reentrancy), create internal\n\t * background threads with the underlying pthread_create.\n\t */\n\tint err = background_thread_create_signals_masked(&info->thread, NULL,\n\t    background_thread_entry, (void *)thread_ind);\n\tpost_reentrancy(tsd);\n\n\tif (err != 0) {\n\t\tmalloc_printf(\"<jemalloc>: arena 0 background thread creation \"\n\t\t    \"failed (%d)\\n\", err);\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);\n\t\tinfo->state = background_thread_stopped;\n\t\tn_background_threads--;\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);\n\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\nbool\nbackground_threads_enable(tsd_t *tsd) {\n\tassert(n_background_threads == 0);\n\tassert(background_thread_enabled());\n\tmalloc_mutex_assert_owner(tsd_tsdn(tsd), &background_thread_lock);\n\n\tVARIABLE_ARRAY(bool, marked, ncpus);\n\tunsigned i, nmarked;\n\tfor (i = 0; i < ncpus; i++) {\n\t\tmarked[i] = false;\n\t}\n\tnmarked = 0;\n\t/* Mark the threads we need to create for thread 0. */\n\tunsigned n = narenas_total_get();\n\tfor (i = 1; i < n; i++) {\n\t\tif (marked[i % ncpus] ||\n\t\t    arena_get(tsd_tsdn(tsd), i, false) == NULL) {\n\t\t\tcontinue;\n\t\t}\n\t\tbackground_thread_info_t *info = &background_thread_info[i];\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);\n\t\tassert(info->state == background_thread_stopped);\n\t\tbackground_thread_init(tsd, info);\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);\n\t\tmarked[i % ncpus] = true;\n\t\tif (++nmarked == ncpus) {\n\t\t\tbreak;\n\t\t}\n\t}\n\n\treturn background_thread_create(tsd, 0);\n}\n\nbool\nbackground_threads_disable(tsd_t *tsd) {\n\tassert(!background_thread_enabled());\n\tmalloc_mutex_assert_owner(tsd_tsdn(tsd), &background_thread_lock);\n\n\t/* Thread 0 will be responsible for terminating other threads. */\n\tif (background_threads_disable_single(tsd,\n\t    &background_thread_info[0])) {\n\t\treturn true;\n\t}\n\tassert(n_background_threads == 0);\n\n\treturn false;\n}\n\n/* Check if we need to signal the background thread early. */\nvoid\nbackground_thread_interval_check(tsdn_t *tsdn, arena_t *arena,\n    arena_decay_t *decay, size_t npages_new) {\n\tbackground_thread_info_t *info = arena_background_thread_info_get(\n\t    arena);\n\tif (malloc_mutex_trylock(tsdn, &info->mtx)) {\n\t\t/*\n\t\t * Background thread may hold the mutex for a long period of\n\t\t * time.  We'd like to avoid the variance on application\n\t\t * threads.  So keep this non-blocking, and leave the work to a\n\t\t * future epoch.\n\t\t */\n\t\treturn;\n\t}\n\n\tif (info->state != background_thread_started) {\n\t\tgoto label_done;\n\t}\n\tif (malloc_mutex_trylock(tsdn, &decay->mtx)) {\n\t\tgoto label_done;\n\t}\n\n\tssize_t decay_time = atomic_load_zd(&decay->time_ms, ATOMIC_RELAXED);\n\tif (decay_time <= 0) {\n\t\t/* Purging is eagerly done or disabled currently. */\n\t\tgoto label_done_unlock2;\n\t}\n\tuint64_t decay_interval_ns = nstime_ns(&decay->interval);\n\tassert(decay_interval_ns > 0);\n\n\tnstime_t diff;\n\tnstime_init(&diff, background_thread_wakeup_time_get(info));\n\tif (nstime_compare(&diff, &decay->epoch) <= 0) {\n\t\tgoto label_done_unlock2;\n\t}\n\tnstime_subtract(&diff, &decay->epoch);\n\tif (nstime_ns(&diff) < BACKGROUND_THREAD_MIN_INTERVAL_NS) {\n\t\tgoto label_done_unlock2;\n\t}\n\n\tif (npages_new > 0) {\n\t\tsize_t n_epoch = (size_t)(nstime_ns(&diff) / decay_interval_ns);\n\t\t/*\n\t\t * Compute how many new pages we would need to purge by the next\n\t\t * wakeup, which is used to determine if we should signal the\n\t\t * background thread.\n\t\t */\n\t\tuint64_t npurge_new;\n\t\tif (n_epoch >= SMOOTHSTEP_NSTEPS) {\n\t\t\tnpurge_new = npages_new;\n\t\t} else {\n\t\t\tuint64_t h_steps_max = h_steps[SMOOTHSTEP_NSTEPS - 1];\n\t\t\tassert(h_steps_max >=\n\t\t\t    h_steps[SMOOTHSTEP_NSTEPS - 1 - n_epoch]);\n\t\t\tnpurge_new = npages_new * (h_steps_max -\n\t\t\t    h_steps[SMOOTHSTEP_NSTEPS - 1 - n_epoch]);\n\t\t\tnpurge_new >>= SMOOTHSTEP_BFP;\n\t\t}\n\t\tinfo->npages_to_purge_new += npurge_new;\n\t}\n\n\tbool should_signal;\n\tif (info->npages_to_purge_new > BACKGROUND_THREAD_NPAGES_THRESHOLD) {\n\t\tshould_signal = true;\n\t} else if (unlikely(background_thread_indefinite_sleep(info)) &&\n\t    (extents_npages_get(&arena->extents_dirty) > 0 ||\n\t    extents_npages_get(&arena->extents_muzzy) > 0 ||\n\t    info->npages_to_purge_new > 0)) {\n\t\tshould_signal = true;\n\t} else {\n\t\tshould_signal = false;\n\t}\n\n\tif (should_signal) {\n\t\tinfo->npages_to_purge_new = 0;\n\t\tpthread_cond_signal(&info->cond);\n\t}\nlabel_done_unlock2:\n\tmalloc_mutex_unlock(tsdn, &decay->mtx);\nlabel_done:\n\tmalloc_mutex_unlock(tsdn, &info->mtx);\n}\n\nvoid\nbackground_thread_prefork0(tsdn_t *tsdn) {\n\tmalloc_mutex_prefork(tsdn, &background_thread_lock);\n\tbackground_thread_enabled_at_fork = background_thread_enabled();\n}\n\nvoid\nbackground_thread_prefork1(tsdn_t *tsdn) {\n\tfor (unsigned i = 0; i < ncpus; i++) {\n\t\tmalloc_mutex_prefork(tsdn, &background_thread_info[i].mtx);\n\t}\n}\n\nvoid\nbackground_thread_postfork_parent(tsdn_t *tsdn) {\n\tfor (unsigned i = 0; i < ncpus; i++) {\n\t\tmalloc_mutex_postfork_parent(tsdn,\n\t\t    &background_thread_info[i].mtx);\n\t}\n\tmalloc_mutex_postfork_parent(tsdn, &background_thread_lock);\n}\n\nvoid\nbackground_thread_postfork_child(tsdn_t *tsdn) {\n\tfor (unsigned i = 0; i < ncpus; i++) {\n\t\tmalloc_mutex_postfork_child(tsdn,\n\t\t    &background_thread_info[i].mtx);\n\t}\n\tmalloc_mutex_postfork_child(tsdn, &background_thread_lock);\n\tif (!background_thread_enabled_at_fork) {\n\t\treturn;\n\t}\n\n\t/* Clear background_thread state (reset to disabled for child). */\n\tmalloc_mutex_lock(tsdn, &background_thread_lock);\n\tn_background_threads = 0;\n\tbackground_thread_enabled_set(tsdn, false);\n\tfor (unsigned i = 0; i < ncpus; i++) {\n\t\tbackground_thread_info_t *info = &background_thread_info[i];\n\t\tmalloc_mutex_lock(tsdn, &info->mtx);\n\t\tinfo->state = background_thread_stopped;\n\t\tint ret = pthread_cond_init(&info->cond, NULL);\n\t\tassert(ret == 0);\n\t\tbackground_thread_info_init(tsdn, info);\n\t\tmalloc_mutex_unlock(tsdn, &info->mtx);\n\t}\n\tmalloc_mutex_unlock(tsdn, &background_thread_lock);\n}\n\nbool\nbackground_thread_stats_read(tsdn_t *tsdn, background_thread_stats_t *stats) {\n\tassert(config_stats);\n\tmalloc_mutex_lock(tsdn, &background_thread_lock);\n\tif (!background_thread_enabled()) {\n\t\tmalloc_mutex_unlock(tsdn, &background_thread_lock);\n\t\treturn true;\n\t}\n\n\tstats->num_threads = n_background_threads;\n\tuint64_t num_runs = 0;\n\tnstime_init(&stats->run_interval, 0);\n\tfor (unsigned i = 0; i < ncpus; i++) {\n\t\tbackground_thread_info_t *info = &background_thread_info[i];\n\t\tmalloc_mutex_lock(tsdn, &info->mtx);\n\t\tif (info->state != background_thread_stopped) {\n\t\t\tnum_runs += info->tot_n_runs;\n\t\t\tnstime_add(&stats->run_interval, &info->tot_sleep_time);\n\t\t}\n\t\tmalloc_mutex_unlock(tsdn, &info->mtx);\n\t}\n\tstats->num_runs = num_runs;\n\tif (num_runs > 0) {\n\t\tnstime_idivide(&stats->run_interval, num_runs);\n\t}\n\tmalloc_mutex_unlock(tsdn, &background_thread_lock);\n\n\treturn false;\n}\n\n#undef BACKGROUND_THREAD_NPAGES_THRESHOLD\n#undef BILLION\n#undef BACKGROUND_THREAD_MIN_INTERVAL_NS\n\n/*\n * When lazy lock is enabled, we need to make sure setting isthreaded before\n * taking any background_thread locks.  This is called early in ctl (instead of\n * wait for the pthread_create calls to trigger) because the mutex is required\n * before creating background threads.\n */\nvoid\nbackground_thread_ctl_init(tsdn_t *tsdn) {\n\tmalloc_mutex_assert_not_owner(tsdn, &background_thread_lock);\n#ifdef JEMALLOC_PTHREAD_CREATE_WRAPPER\n\tpthread_once(&once_control, pthread_create_wrapper_once);\n#endif\n}\n\n#endif /* defined(JEMALLOC_BACKGROUND_THREAD) */\n\nbool\nbackground_thread_boot0(void) {\n\tif (!have_background_thread && opt_background_thread) {\n\t\tmalloc_printf(\"<jemalloc>: option background_thread currently \"\n\t\t    \"supports pthread only\\n\");\n\t\treturn true;\n\t}\n\n#ifdef JEMALLOC_PTHREAD_CREATE_WRAPPER\n\tpthread_create_fptr = dlsym(RTLD_NEXT, \"pthread_create\");\n\tif (pthread_create_fptr == NULL) {\n\t\tcan_enable_background_thread = false;\n\t\tif (config_lazy_lock || opt_background_thread) {\n\t\t\tmalloc_write(\"<jemalloc>: Error in dlsym(RTLD_NEXT, \"\n\t\t\t    \"\\\"pthread_create\\\")\\n\");\n\t\t\tabort();\n\t\t}\n\t} else {\n\t\tcan_enable_background_thread = true;\n\t}\n#endif\n\treturn false;\n}\n\nbool\nbackground_thread_boot1(tsdn_t *tsdn) {\n#ifdef JEMALLOC_BACKGROUND_THREAD\n\tassert(have_background_thread);\n\tassert(narenas_total_get() > 0);\n\n\tbackground_thread_enabled_set(tsdn, opt_background_thread);\n\tif (malloc_mutex_init(&background_thread_lock,\n\t    \"background_thread_global\",\n\t    WITNESS_RANK_BACKGROUND_THREAD_GLOBAL,\n\t    malloc_mutex_rank_exclusive)) {\n\t\treturn true;\n\t}\n\tif (opt_background_thread) {\n\t\tbackground_thread_ctl_init(tsdn);\n\t}\n\n\tbackground_thread_info = (background_thread_info_t *)base_alloc(tsdn,\n\t    b0get(), ncpus * sizeof(background_thread_info_t), CACHELINE);\n\tif (background_thread_info == NULL) {\n\t\treturn true;\n\t}\n\n\tfor (unsigned i = 0; i < ncpus; i++) {\n\t\tbackground_thread_info_t *info = &background_thread_info[i];\n\t\t/* Thread mutex is rank_inclusive because of thread0. */\n\t\tif (malloc_mutex_init(&info->mtx, \"background_thread\",\n\t\t    WITNESS_RANK_BACKGROUND_THREAD,\n\t\t    malloc_mutex_address_ordered)) {\n\t\t\treturn true;\n\t\t}\n\t\tif (pthread_cond_init(&info->cond, NULL)) {\n\t\t\treturn true;\n\t\t}\n\t\tmalloc_mutex_lock(tsdn, &info->mtx);\n\t\tinfo->state = background_thread_stopped;\n\t\tbackground_thread_info_init(tsdn, info);\n\t\tmalloc_mutex_unlock(tsdn, &info->mtx);\n\t}\n#endif\n\n\treturn false;\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/base.c",
    "content": "#define JEMALLOC_BASE_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/extent_mmap.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/sz.h\"\n\n/******************************************************************************/\n/* Data. */\n\nstatic base_t\t*b0;\n\n/******************************************************************************/\n\nstatic void *\nbase_map(extent_hooks_t *extent_hooks, unsigned ind, size_t size) {\n\tvoid *addr;\n\tbool zero = true;\n\tbool commit = true;\n\n\tassert(size == HUGEPAGE_CEILING(size));\n\n\tif (extent_hooks == &extent_hooks_default) {\n\t\taddr = extent_alloc_mmap(NULL, size, PAGE, &zero, &commit);\n\t} else {\n\t\taddr = extent_hooks->alloc(extent_hooks, NULL, size, PAGE,\n\t\t    &zero, &commit, ind);\n\t}\n\n\treturn addr;\n}\n\nstatic void\nbase_unmap(extent_hooks_t *extent_hooks, unsigned ind, void *addr,\n    size_t size) {\n\t/*\n\t * Cascade through dalloc, decommit, purge_forced, and purge_lazy,\n\t * stopping at first success.  This cascade is performed for consistency\n\t * with the cascade in extent_dalloc_wrapper() because an application's\n\t * custom hooks may not support e.g. dalloc.  This function is only ever\n\t * called as a side effect of arena destruction, so although it might\n\t * seem pointless to do anything besides dalloc here, the application\n\t * may in fact want the end state of all associated virtual memory to be\n\t * in some consistent-but-allocated state.\n\t */\n\tif (extent_hooks == &extent_hooks_default) {\n\t\tif (!extent_dalloc_mmap(addr, size)) {\n\t\t\treturn;\n\t\t}\n\t\tif (!pages_decommit(addr, size)) {\n\t\t\treturn;\n\t\t}\n\t\tif (!pages_purge_forced(addr, size)) {\n\t\t\treturn;\n\t\t}\n\t\tif (!pages_purge_lazy(addr, size)) {\n\t\t\treturn;\n\t\t}\n\t\t/* Nothing worked.  This should never happen. */\n\t\tnot_reached();\n\t} else {\n\t\tif (extent_hooks->dalloc != NULL &&\n\t\t    !extent_hooks->dalloc(extent_hooks, addr, size, true,\n\t\t    ind)) {\n\t\t\treturn;\n\t\t}\n\t\tif (extent_hooks->decommit != NULL &&\n\t\t    !extent_hooks->decommit(extent_hooks, addr, size, 0, size,\n\t\t    ind)) {\n\t\t\treturn;\n\t\t}\n\t\tif (extent_hooks->purge_forced != NULL &&\n\t\t    !extent_hooks->purge_forced(extent_hooks, addr, size, 0,\n\t\t    size, ind)) {\n\t\t\treturn;\n\t\t}\n\t\tif (extent_hooks->purge_lazy != NULL &&\n\t\t    !extent_hooks->purge_lazy(extent_hooks, addr, size, 0, size,\n\t\t    ind)) {\n\t\t\treturn;\n\t\t}\n\t\t/* Nothing worked.  That's the application's problem. */\n\t}\n}\n\nstatic void\nbase_extent_init(size_t *extent_sn_next, extent_t *extent, void *addr,\n    size_t size) {\n\tsize_t sn;\n\n\tsn = *extent_sn_next;\n\t(*extent_sn_next)++;\n\n\textent_binit(extent, addr, size, sn);\n}\n\nstatic void *\nbase_extent_bump_alloc_helper(extent_t *extent, size_t *gap_size, size_t size,\n    size_t alignment) {\n\tvoid *ret;\n\n\tassert(alignment == ALIGNMENT_CEILING(alignment, QUANTUM));\n\tassert(size == ALIGNMENT_CEILING(size, alignment));\n\n\t*gap_size = ALIGNMENT_CEILING((uintptr_t)extent_addr_get(extent),\n\t    alignment) - (uintptr_t)extent_addr_get(extent);\n\tret = (void *)((uintptr_t)extent_addr_get(extent) + *gap_size);\n\tassert(extent_bsize_get(extent) >= *gap_size + size);\n\textent_binit(extent, (void *)((uintptr_t)extent_addr_get(extent) +\n\t    *gap_size + size), extent_bsize_get(extent) - *gap_size - size,\n\t    extent_sn_get(extent));\n\treturn ret;\n}\n\nstatic void\nbase_extent_bump_alloc_post(tsdn_t *tsdn, base_t *base, extent_t *extent,\n    size_t gap_size, void *addr, size_t size) {\n\tif (extent_bsize_get(extent) > 0) {\n\t\t/*\n\t\t * Compute the index for the largest size class that does not\n\t\t * exceed extent's size.\n\t\t */\n\t\tszind_t index_floor =\n\t\t    sz_size2index(extent_bsize_get(extent) + 1) - 1;\n\t\textent_heap_insert(&base->avail[index_floor], extent);\n\t}\n\n\tif (config_stats) {\n\t\tbase->allocated += size;\n\t\t/*\n\t\t * Add one PAGE to base_resident for every page boundary that is\n\t\t * crossed by the new allocation.\n\t\t */\n\t\tbase->resident += PAGE_CEILING((uintptr_t)addr + size) -\n\t\t    PAGE_CEILING((uintptr_t)addr - gap_size);\n\t\tassert(base->allocated <= base->resident);\n\t\tassert(base->resident <= base->mapped);\n\t}\n}\n\nstatic void *\nbase_extent_bump_alloc(tsdn_t *tsdn, base_t *base, extent_t *extent,\n    size_t size, size_t alignment) {\n\tvoid *ret;\n\tsize_t gap_size;\n\n\tret = base_extent_bump_alloc_helper(extent, &gap_size, size, alignment);\n\tbase_extent_bump_alloc_post(tsdn, base, extent, gap_size, ret, size);\n\treturn ret;\n}\n\n/*\n * Allocate a block of virtual memory that is large enough to start with a\n * base_block_t header, followed by an object of specified size and alignment.\n * On success a pointer to the initialized base_block_t header is returned.\n */\nstatic base_block_t *\nbase_block_alloc(extent_hooks_t *extent_hooks, unsigned ind,\n    pszind_t *pind_last, size_t *extent_sn_next, size_t size,\n    size_t alignment) {\n\talignment = ALIGNMENT_CEILING(alignment, QUANTUM);\n\tsize_t usize = ALIGNMENT_CEILING(size, alignment);\n\tsize_t header_size = sizeof(base_block_t);\n\tsize_t gap_size = ALIGNMENT_CEILING(header_size, alignment) -\n\t    header_size;\n\t/*\n\t * Create increasingly larger blocks in order to limit the total number\n\t * of disjoint virtual memory ranges.  Choose the next size in the page\n\t * size class series (skipping size classes that are not a multiple of\n\t * HUGEPAGE), or a size large enough to satisfy the requested size and\n\t * alignment, whichever is larger.\n\t */\n\tsize_t min_block_size = HUGEPAGE_CEILING(sz_psz2u(header_size + gap_size\n\t    + usize));\n\tpszind_t pind_next = (*pind_last + 1 < NPSIZES) ? *pind_last + 1 :\n\t    *pind_last;\n\tsize_t next_block_size = HUGEPAGE_CEILING(sz_pind2sz(pind_next));\n\tsize_t block_size = (min_block_size > next_block_size) ? min_block_size\n\t    : next_block_size;\n\tbase_block_t *block = (base_block_t *)base_map(extent_hooks, ind,\n\t    block_size);\n\tif (block == NULL) {\n\t\treturn NULL;\n\t}\n\t*pind_last = sz_psz2ind(block_size);\n\tblock->size = block_size;\n\tblock->next = NULL;\n\tassert(block_size >= header_size);\n\tbase_extent_init(extent_sn_next, &block->extent,\n\t    (void *)((uintptr_t)block + header_size), block_size - header_size);\n\treturn block;\n}\n\n/*\n * Allocate an extent that is at least as large as specified size, with\n * specified alignment.\n */\nstatic extent_t *\nbase_extent_alloc(tsdn_t *tsdn, base_t *base, size_t size, size_t alignment) {\n\tmalloc_mutex_assert_owner(tsdn, &base->mtx);\n\n\textent_hooks_t *extent_hooks = base_extent_hooks_get(base);\n\t/*\n\t * Drop mutex during base_block_alloc(), because an extent hook will be\n\t * called.\n\t */\n\tmalloc_mutex_unlock(tsdn, &base->mtx);\n\tbase_block_t *block = base_block_alloc(extent_hooks, base_ind_get(base),\n\t    &base->pind_last, &base->extent_sn_next, size, alignment);\n\tmalloc_mutex_lock(tsdn, &base->mtx);\n\tif (block == NULL) {\n\t\treturn NULL;\n\t}\n\tblock->next = base->blocks;\n\tbase->blocks = block;\n\tif (config_stats) {\n\t\tbase->allocated += sizeof(base_block_t);\n\t\tbase->resident += PAGE_CEILING(sizeof(base_block_t));\n\t\tbase->mapped += block->size;\n\t\tassert(base->allocated <= base->resident);\n\t\tassert(base->resident <= base->mapped);\n\t}\n\treturn &block->extent;\n}\n\nbase_t *\nb0get(void) {\n\treturn b0;\n}\n\nbase_t *\nbase_new(tsdn_t *tsdn, unsigned ind, extent_hooks_t *extent_hooks) {\n\tpszind_t pind_last = 0;\n\tsize_t extent_sn_next = 0;\n\tbase_block_t *block = base_block_alloc(extent_hooks, ind, &pind_last,\n\t    &extent_sn_next, sizeof(base_t), QUANTUM);\n\tif (block == NULL) {\n\t\treturn NULL;\n\t}\n\n\tsize_t gap_size;\n\tsize_t base_alignment = CACHELINE;\n\tsize_t base_size = ALIGNMENT_CEILING(sizeof(base_t), base_alignment);\n\tbase_t *base = (base_t *)base_extent_bump_alloc_helper(&block->extent,\n\t    &gap_size, base_size, base_alignment);\n\tbase->ind = ind;\n\tatomic_store_p(&base->extent_hooks, extent_hooks, ATOMIC_RELAXED);\n\tif (malloc_mutex_init(&base->mtx, \"base\", WITNESS_RANK_BASE,\n\t    malloc_mutex_rank_exclusive)) {\n\t\tbase_unmap(extent_hooks, ind, block, block->size);\n\t\treturn NULL;\n\t}\n\tbase->pind_last = pind_last;\n\tbase->extent_sn_next = extent_sn_next;\n\tbase->blocks = block;\n\tfor (szind_t i = 0; i < NSIZES; i++) {\n\t\textent_heap_new(&base->avail[i]);\n\t}\n\tif (config_stats) {\n\t\tbase->allocated = sizeof(base_block_t);\n\t\tbase->resident = PAGE_CEILING(sizeof(base_block_t));\n\t\tbase->mapped = block->size;\n\t\tassert(base->allocated <= base->resident);\n\t\tassert(base->resident <= base->mapped);\n\t}\n\tbase_extent_bump_alloc_post(tsdn, base, &block->extent, gap_size, base,\n\t    base_size);\n\n\treturn base;\n}\n\nvoid\nbase_delete(base_t *base) {\n\textent_hooks_t *extent_hooks = base_extent_hooks_get(base);\n\tbase_block_t *next = base->blocks;\n\tdo {\n\t\tbase_block_t *block = next;\n\t\tnext = block->next;\n\t\tbase_unmap(extent_hooks, base_ind_get(base), block,\n\t\t    block->size);\n\t} while (next != NULL);\n}\n\nextent_hooks_t *\nbase_extent_hooks_get(base_t *base) {\n\treturn (extent_hooks_t *)atomic_load_p(&base->extent_hooks,\n\t    ATOMIC_ACQUIRE);\n}\n\nextent_hooks_t *\nbase_extent_hooks_set(base_t *base, extent_hooks_t *extent_hooks) {\n\textent_hooks_t *old_extent_hooks = base_extent_hooks_get(base);\n\tatomic_store_p(&base->extent_hooks, extent_hooks, ATOMIC_RELEASE);\n\treturn old_extent_hooks;\n}\n\nstatic void *\nbase_alloc_impl(tsdn_t *tsdn, base_t *base, size_t size, size_t alignment,\n    size_t *esn) {\n\talignment = QUANTUM_CEILING(alignment);\n\tsize_t usize = ALIGNMENT_CEILING(size, alignment);\n\tsize_t asize = usize + alignment - QUANTUM;\n\n\textent_t *extent = NULL;\n\tmalloc_mutex_lock(tsdn, &base->mtx);\n\tfor (szind_t i = sz_size2index(asize); i < NSIZES; i++) {\n\t\textent = extent_heap_remove_first(&base->avail[i]);\n\t\tif (extent != NULL) {\n\t\t\t/* Use existing space. */\n\t\t\tbreak;\n\t\t}\n\t}\n\tif (extent == NULL) {\n\t\t/* Try to allocate more space. */\n\t\textent = base_extent_alloc(tsdn, base, usize, alignment);\n\t}\n\tvoid *ret;\n\tif (extent == NULL) {\n\t\tret = NULL;\n\t\tgoto label_return;\n\t}\n\n\tret = base_extent_bump_alloc(tsdn, base, extent, usize, alignment);\n\tif (esn != NULL) {\n\t\t*esn = extent_sn_get(extent);\n\t}\nlabel_return:\n\tmalloc_mutex_unlock(tsdn, &base->mtx);\n\treturn ret;\n}\n\n/*\n * base_alloc() returns zeroed memory, which is always demand-zeroed for the\n * auto arenas, in order to make multi-page sparse data structures such as radix\n * tree nodes efficient with respect to physical memory usage.  Upon success a\n * pointer to at least size bytes with specified alignment is returned.  Note\n * that size is rounded up to the nearest multiple of alignment to avoid false\n * sharing.\n */\nvoid *\nbase_alloc(tsdn_t *tsdn, base_t *base, size_t size, size_t alignment) {\n\treturn base_alloc_impl(tsdn, base, size, alignment, NULL);\n}\n\nextent_t *\nbase_alloc_extent(tsdn_t *tsdn, base_t *base) {\n\tsize_t esn;\n\textent_t *extent = base_alloc_impl(tsdn, base, sizeof(extent_t),\n\t    CACHELINE, &esn);\n\tif (extent == NULL) {\n\t\treturn NULL;\n\t}\n\textent_esn_set(extent, esn);\n\treturn extent;\n}\n\nvoid\nbase_stats_get(tsdn_t *tsdn, base_t *base, size_t *allocated, size_t *resident,\n    size_t *mapped) {\n\tcassert(config_stats);\n\n\tmalloc_mutex_lock(tsdn, &base->mtx);\n\tassert(base->allocated <= base->resident);\n\tassert(base->resident <= base->mapped);\n\t*allocated = base->allocated;\n\t*resident = base->resident;\n\t*mapped = base->mapped;\n\tmalloc_mutex_unlock(tsdn, &base->mtx);\n}\n\nvoid\nbase_prefork(tsdn_t *tsdn, base_t *base) {\n\tmalloc_mutex_prefork(tsdn, &base->mtx);\n}\n\nvoid\nbase_postfork_parent(tsdn_t *tsdn, base_t *base) {\n\tmalloc_mutex_postfork_parent(tsdn, &base->mtx);\n}\n\nvoid\nbase_postfork_child(tsdn_t *tsdn, base_t *base) {\n\tmalloc_mutex_postfork_child(tsdn, &base->mtx);\n}\n\nbool\nbase_boot(tsdn_t *tsdn) {\n\tb0 = base_new(tsdn, 0, (extent_hooks_t *)&extent_hooks_default);\n\treturn (b0 == NULL);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/bitmap.c",
    "content": "#define JEMALLOC_BITMAP_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n\n/******************************************************************************/\n\n#ifdef BITMAP_USE_TREE\n\nvoid\nbitmap_info_init(bitmap_info_t *binfo, size_t nbits) {\n\tunsigned i;\n\tsize_t group_count;\n\n\tassert(nbits > 0);\n\tassert(nbits <= (ZU(1) << LG_BITMAP_MAXBITS));\n\n\t/*\n\t * Compute the number of groups necessary to store nbits bits, and\n\t * progressively work upward through the levels until reaching a level\n\t * that requires only one group.\n\t */\n\tbinfo->levels[0].group_offset = 0;\n\tgroup_count = BITMAP_BITS2GROUPS(nbits);\n\tfor (i = 1; group_count > 1; i++) {\n\t\tassert(i < BITMAP_MAX_LEVELS);\n\t\tbinfo->levels[i].group_offset = binfo->levels[i-1].group_offset\n\t\t    + group_count;\n\t\tgroup_count = BITMAP_BITS2GROUPS(group_count);\n\t}\n\tbinfo->levels[i].group_offset = binfo->levels[i-1].group_offset\n\t    + group_count;\n\tassert(binfo->levels[i].group_offset <= BITMAP_GROUPS_MAX);\n\tbinfo->nlevels = i;\n\tbinfo->nbits = nbits;\n}\n\nstatic size_t\nbitmap_info_ngroups(const bitmap_info_t *binfo) {\n\treturn binfo->levels[binfo->nlevels].group_offset;\n}\n\nvoid\nbitmap_init(bitmap_t *bitmap, const bitmap_info_t *binfo, bool fill) {\n\tsize_t extra;\n\tunsigned i;\n\n\t/*\n\t * Bits are actually inverted with regard to the external bitmap\n\t * interface.\n\t */\n\n\tif (fill) {\n\t\t/* The \"filled\" bitmap starts out with all 0 bits. */\n\t\tmemset(bitmap, 0, bitmap_size(binfo));\n\t\treturn;\n\t}\n\n\t/*\n\t * The \"empty\" bitmap starts out with all 1 bits, except for trailing\n\t * unused bits (if any).  Note that each group uses bit 0 to correspond\n\t * to the first logical bit in the group, so extra bits are the most\n\t * significant bits of the last group.\n\t */\n\tmemset(bitmap, 0xffU, bitmap_size(binfo));\n\textra = (BITMAP_GROUP_NBITS - (binfo->nbits & BITMAP_GROUP_NBITS_MASK))\n\t    & BITMAP_GROUP_NBITS_MASK;\n\tif (extra != 0) {\n\t\tbitmap[binfo->levels[1].group_offset - 1] >>= extra;\n\t}\n\tfor (i = 1; i < binfo->nlevels; i++) {\n\t\tsize_t group_count = binfo->levels[i].group_offset -\n\t\t    binfo->levels[i-1].group_offset;\n\t\textra = (BITMAP_GROUP_NBITS - (group_count &\n\t\t    BITMAP_GROUP_NBITS_MASK)) & BITMAP_GROUP_NBITS_MASK;\n\t\tif (extra != 0) {\n\t\t\tbitmap[binfo->levels[i+1].group_offset - 1] >>= extra;\n\t\t}\n\t}\n}\n\n#else /* BITMAP_USE_TREE */\n\nvoid\nbitmap_info_init(bitmap_info_t *binfo, size_t nbits) {\n\tassert(nbits > 0);\n\tassert(nbits <= (ZU(1) << LG_BITMAP_MAXBITS));\n\n\tbinfo->ngroups = BITMAP_BITS2GROUPS(nbits);\n\tbinfo->nbits = nbits;\n}\n\nstatic size_t\nbitmap_info_ngroups(const bitmap_info_t *binfo) {\n\treturn binfo->ngroups;\n}\n\nvoid\nbitmap_init(bitmap_t *bitmap, const bitmap_info_t *binfo, bool fill) {\n\tsize_t extra;\n\n\tif (fill) {\n\t\tmemset(bitmap, 0, bitmap_size(binfo));\n\t\treturn;\n\t}\n\n\tmemset(bitmap, 0xffU, bitmap_size(binfo));\n\textra = (BITMAP_GROUP_NBITS - (binfo->nbits & BITMAP_GROUP_NBITS_MASK))\n\t    & BITMAP_GROUP_NBITS_MASK;\n\tif (extra != 0) {\n\t\tbitmap[binfo->ngroups - 1] >>= extra;\n\t}\n}\n\n#endif /* BITMAP_USE_TREE */\n\nsize_t\nbitmap_size(const bitmap_info_t *binfo) {\n\treturn (bitmap_info_ngroups(binfo) << LG_SIZEOF_BITMAP);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/ckh.c",
    "content": "/*\n *******************************************************************************\n * Implementation of (2^1+,2) cuckoo hashing, where 2^1+ indicates that each\n * hash bucket contains 2^n cells, for n >= 1, and 2 indicates that two hash\n * functions are employed.  The original cuckoo hashing algorithm was described\n * in:\n *\n *   Pagh, R., F.F. Rodler (2004) Cuckoo Hashing.  Journal of Algorithms\n *     51(2):122-144.\n *\n * Generalization of cuckoo hashing was discussed in:\n *\n *   Erlingsson, U., M. Manasse, F. McSherry (2006) A cool and practical\n *     alternative to traditional hash tables.  In Proceedings of the 7th\n *     Workshop on Distributed Data and Structures (WDAS'06), Santa Clara, CA,\n *     January 2006.\n *\n * This implementation uses precisely two hash functions because that is the\n * fewest that can work, and supporting multiple hashes is an implementation\n * burden.  Here is a reproduction of Figure 1 from Erlingsson et al. (2006)\n * that shows approximate expected maximum load factors for various\n * configurations:\n *\n *           |         #cells/bucket         |\n *   #hashes |   1   |   2   |   4   |   8   |\n *   --------+-------+-------+-------+-------+\n *         1 | 0.006 | 0.006 | 0.03  | 0.12  |\n *         2 | 0.49  | 0.86  |>0.93< |>0.96< |\n *         3 | 0.91  | 0.97  | 0.98  | 0.999 |\n *         4 | 0.97  | 0.99  | 0.999 |       |\n *\n * The number of cells per bucket is chosen such that a bucket fits in one cache\n * line.  So, on 32- and 64-bit systems, we use (8,2) and (4,2) cuckoo hashing,\n * respectively.\n *\n ******************************************************************************/\n#define JEMALLOC_CKH_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n\n#include \"jemalloc/internal/ckh.h\"\n\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/hash.h\"\n#include \"jemalloc/internal/malloc_io.h\"\n#include \"jemalloc/internal/prng.h\"\n#include \"jemalloc/internal/util.h\"\n\n/******************************************************************************/\n/* Function prototypes for non-inline static functions. */\n\nstatic bool\tckh_grow(tsd_t *tsd, ckh_t *ckh);\nstatic void\tckh_shrink(tsd_t *tsd, ckh_t *ckh);\n\n/******************************************************************************/\n\n/*\n * Search bucket for key and return the cell number if found; SIZE_T_MAX\n * otherwise.\n */\nstatic size_t\nckh_bucket_search(ckh_t *ckh, size_t bucket, const void *key) {\n\tckhc_t *cell;\n\tunsigned i;\n\n\tfor (i = 0; i < (ZU(1) << LG_CKH_BUCKET_CELLS); i++) {\n\t\tcell = &ckh->tab[(bucket << LG_CKH_BUCKET_CELLS) + i];\n\t\tif (cell->key != NULL && ckh->keycomp(key, cell->key)) {\n\t\t\treturn (bucket << LG_CKH_BUCKET_CELLS) + i;\n\t\t}\n\t}\n\n\treturn SIZE_T_MAX;\n}\n\n/*\n * Search table for key and return cell number if found; SIZE_T_MAX otherwise.\n */\nstatic size_t\nckh_isearch(ckh_t *ckh, const void *key) {\n\tsize_t hashes[2], bucket, cell;\n\n\tassert(ckh != NULL);\n\n\tckh->hash(key, hashes);\n\n\t/* Search primary bucket. */\n\tbucket = hashes[0] & ((ZU(1) << ckh->lg_curbuckets) - 1);\n\tcell = ckh_bucket_search(ckh, bucket, key);\n\tif (cell != SIZE_T_MAX) {\n\t\treturn cell;\n\t}\n\n\t/* Search secondary bucket. */\n\tbucket = hashes[1] & ((ZU(1) << ckh->lg_curbuckets) - 1);\n\tcell = ckh_bucket_search(ckh, bucket, key);\n\treturn cell;\n}\n\nstatic bool\nckh_try_bucket_insert(ckh_t *ckh, size_t bucket, const void *key,\n    const void *data) {\n\tckhc_t *cell;\n\tunsigned offset, i;\n\n\t/*\n\t * Cycle through the cells in the bucket, starting at a random position.\n\t * The randomness avoids worst-case search overhead as buckets fill up.\n\t */\n\toffset = (unsigned)prng_lg_range_u64(&ckh->prng_state,\n\t    LG_CKH_BUCKET_CELLS);\n\tfor (i = 0; i < (ZU(1) << LG_CKH_BUCKET_CELLS); i++) {\n\t\tcell = &ckh->tab[(bucket << LG_CKH_BUCKET_CELLS) +\n\t\t    ((i + offset) & ((ZU(1) << LG_CKH_BUCKET_CELLS) - 1))];\n\t\tif (cell->key == NULL) {\n\t\t\tcell->key = key;\n\t\t\tcell->data = data;\n\t\t\tckh->count++;\n\t\t\treturn false;\n\t\t}\n\t}\n\n\treturn true;\n}\n\n/*\n * No space is available in bucket.  Randomly evict an item, then try to find an\n * alternate location for that item.  Iteratively repeat this\n * eviction/relocation procedure until either success or detection of an\n * eviction/relocation bucket cycle.\n */\nstatic bool\nckh_evict_reloc_insert(ckh_t *ckh, size_t argbucket, void const **argkey,\n    void const **argdata) {\n\tconst void *key, *data, *tkey, *tdata;\n\tckhc_t *cell;\n\tsize_t hashes[2], bucket, tbucket;\n\tunsigned i;\n\n\tbucket = argbucket;\n\tkey = *argkey;\n\tdata = *argdata;\n\twhile (true) {\n\t\t/*\n\t\t * Choose a random item within the bucket to evict.  This is\n\t\t * critical to correct function, because without (eventually)\n\t\t * evicting all items within a bucket during iteration, it\n\t\t * would be possible to get stuck in an infinite loop if there\n\t\t * were an item for which both hashes indicated the same\n\t\t * bucket.\n\t\t */\n\t\ti = (unsigned)prng_lg_range_u64(&ckh->prng_state,\n\t\t    LG_CKH_BUCKET_CELLS);\n\t\tcell = &ckh->tab[(bucket << LG_CKH_BUCKET_CELLS) + i];\n\t\tassert(cell->key != NULL);\n\n\t\t/* Swap cell->{key,data} and {key,data} (evict). */\n\t\ttkey = cell->key; tdata = cell->data;\n\t\tcell->key = key; cell->data = data;\n\t\tkey = tkey; data = tdata;\n\n#ifdef CKH_COUNT\n\t\tckh->nrelocs++;\n#endif\n\n\t\t/* Find the alternate bucket for the evicted item. */\n\t\tckh->hash(key, hashes);\n\t\ttbucket = hashes[1] & ((ZU(1) << ckh->lg_curbuckets) - 1);\n\t\tif (tbucket == bucket) {\n\t\t\ttbucket = hashes[0] & ((ZU(1) << ckh->lg_curbuckets)\n\t\t\t    - 1);\n\t\t\t/*\n\t\t\t * It may be that (tbucket == bucket) still, if the\n\t\t\t * item's hashes both indicate this bucket.  However,\n\t\t\t * we are guaranteed to eventually escape this bucket\n\t\t\t * during iteration, assuming pseudo-random item\n\t\t\t * selection (true randomness would make infinite\n\t\t\t * looping a remote possibility).  The reason we can\n\t\t\t * never get trapped forever is that there are two\n\t\t\t * cases:\n\t\t\t *\n\t\t\t * 1) This bucket == argbucket, so we will quickly\n\t\t\t *    detect an eviction cycle and terminate.\n\t\t\t * 2) An item was evicted to this bucket from another,\n\t\t\t *    which means that at least one item in this bucket\n\t\t\t *    has hashes that indicate distinct buckets.\n\t\t\t */\n\t\t}\n\t\t/* Check for a cycle. */\n\t\tif (tbucket == argbucket) {\n\t\t\t*argkey = key;\n\t\t\t*argdata = data;\n\t\t\treturn true;\n\t\t}\n\n\t\tbucket = tbucket;\n\t\tif (!ckh_try_bucket_insert(ckh, bucket, key, data)) {\n\t\t\treturn false;\n\t\t}\n\t}\n}\n\nstatic bool\nckh_try_insert(ckh_t *ckh, void const**argkey, void const**argdata) {\n\tsize_t hashes[2], bucket;\n\tconst void *key = *argkey;\n\tconst void *data = *argdata;\n\n\tckh->hash(key, hashes);\n\n\t/* Try to insert in primary bucket. */\n\tbucket = hashes[0] & ((ZU(1) << ckh->lg_curbuckets) - 1);\n\tif (!ckh_try_bucket_insert(ckh, bucket, key, data)) {\n\t\treturn false;\n\t}\n\n\t/* Try to insert in secondary bucket. */\n\tbucket = hashes[1] & ((ZU(1) << ckh->lg_curbuckets) - 1);\n\tif (!ckh_try_bucket_insert(ckh, bucket, key, data)) {\n\t\treturn false;\n\t}\n\n\t/*\n\t * Try to find a place for this item via iterative eviction/relocation.\n\t */\n\treturn ckh_evict_reloc_insert(ckh, bucket, argkey, argdata);\n}\n\n/*\n * Try to rebuild the hash table from scratch by inserting all items from the\n * old table into the new.\n */\nstatic bool\nckh_rebuild(ckh_t *ckh, ckhc_t *aTab) {\n\tsize_t count, i, nins;\n\tconst void *key, *data;\n\n\tcount = ckh->count;\n\tckh->count = 0;\n\tfor (i = nins = 0; nins < count; i++) {\n\t\tif (aTab[i].key != NULL) {\n\t\t\tkey = aTab[i].key;\n\t\t\tdata = aTab[i].data;\n\t\t\tif (ckh_try_insert(ckh, &key, &data)) {\n\t\t\t\tckh->count = count;\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tnins++;\n\t\t}\n\t}\n\n\treturn false;\n}\n\nstatic bool\nckh_grow(tsd_t *tsd, ckh_t *ckh) {\n\tbool ret;\n\tckhc_t *tab, *ttab;\n\tunsigned lg_prevbuckets, lg_curcells;\n\n#ifdef CKH_COUNT\n\tckh->ngrows++;\n#endif\n\n\t/*\n\t * It is possible (though unlikely, given well behaved hashes) that the\n\t * table will have to be doubled more than once in order to create a\n\t * usable table.\n\t */\n\tlg_prevbuckets = ckh->lg_curbuckets;\n\tlg_curcells = ckh->lg_curbuckets + LG_CKH_BUCKET_CELLS;\n\twhile (true) {\n\t\tsize_t usize;\n\n\t\tlg_curcells++;\n\t\tusize = sz_sa2u(sizeof(ckhc_t) << lg_curcells, CACHELINE);\n\t\tif (unlikely(usize == 0 || usize > LARGE_MAXCLASS)) {\n\t\t\tret = true;\n\t\t\tgoto label_return;\n\t\t}\n\t\ttab = (ckhc_t *)ipallocztm(tsd_tsdn(tsd), usize, CACHELINE,\n\t\t    true, NULL, true, arena_ichoose(tsd, NULL));\n\t\tif (tab == NULL) {\n\t\t\tret = true;\n\t\t\tgoto label_return;\n\t\t}\n\t\t/* Swap in new table. */\n\t\tttab = ckh->tab;\n\t\tckh->tab = tab;\n\t\ttab = ttab;\n\t\tckh->lg_curbuckets = lg_curcells - LG_CKH_BUCKET_CELLS;\n\n\t\tif (!ckh_rebuild(ckh, tab)) {\n\t\t\tidalloctm(tsd_tsdn(tsd), tab, NULL, NULL, true, true);\n\t\t\tbreak;\n\t\t}\n\n\t\t/* Rebuilding failed, so back out partially rebuilt table. */\n\t\tidalloctm(tsd_tsdn(tsd), ckh->tab, NULL, NULL, true, true);\n\t\tckh->tab = tab;\n\t\tckh->lg_curbuckets = lg_prevbuckets;\n\t}\n\n\tret = false;\nlabel_return:\n\treturn ret;\n}\n\nstatic void\nckh_shrink(tsd_t *tsd, ckh_t *ckh) {\n\tckhc_t *tab, *ttab;\n\tsize_t usize;\n\tunsigned lg_prevbuckets, lg_curcells;\n\n\t/*\n\t * It is possible (though unlikely, given well behaved hashes) that the\n\t * table rebuild will fail.\n\t */\n\tlg_prevbuckets = ckh->lg_curbuckets;\n\tlg_curcells = ckh->lg_curbuckets + LG_CKH_BUCKET_CELLS - 1;\n\tusize = sz_sa2u(sizeof(ckhc_t) << lg_curcells, CACHELINE);\n\tif (unlikely(usize == 0 || usize > LARGE_MAXCLASS)) {\n\t\treturn;\n\t}\n\ttab = (ckhc_t *)ipallocztm(tsd_tsdn(tsd), usize, CACHELINE, true, NULL,\n\t    true, arena_ichoose(tsd, NULL));\n\tif (tab == NULL) {\n\t\t/*\n\t\t * An OOM error isn't worth propagating, since it doesn't\n\t\t * prevent this or future operations from proceeding.\n\t\t */\n\t\treturn;\n\t}\n\t/* Swap in new table. */\n\tttab = ckh->tab;\n\tckh->tab = tab;\n\ttab = ttab;\n\tckh->lg_curbuckets = lg_curcells - LG_CKH_BUCKET_CELLS;\n\n\tif (!ckh_rebuild(ckh, tab)) {\n\t\tidalloctm(tsd_tsdn(tsd), tab, NULL, NULL, true, true);\n#ifdef CKH_COUNT\n\t\tckh->nshrinks++;\n#endif\n\t\treturn;\n\t}\n\n\t/* Rebuilding failed, so back out partially rebuilt table. */\n\tidalloctm(tsd_tsdn(tsd), ckh->tab, NULL, NULL, true, true);\n\tckh->tab = tab;\n\tckh->lg_curbuckets = lg_prevbuckets;\n#ifdef CKH_COUNT\n\tckh->nshrinkfails++;\n#endif\n}\n\nbool\nckh_new(tsd_t *tsd, ckh_t *ckh, size_t minitems, ckh_hash_t *hash,\n    ckh_keycomp_t *keycomp) {\n\tbool ret;\n\tsize_t mincells, usize;\n\tunsigned lg_mincells;\n\n\tassert(minitems > 0);\n\tassert(hash != NULL);\n\tassert(keycomp != NULL);\n\n#ifdef CKH_COUNT\n\tckh->ngrows = 0;\n\tckh->nshrinks = 0;\n\tckh->nshrinkfails = 0;\n\tckh->ninserts = 0;\n\tckh->nrelocs = 0;\n#endif\n\tckh->prng_state = 42; /* Value doesn't really matter. */\n\tckh->count = 0;\n\n\t/*\n\t * Find the minimum power of 2 that is large enough to fit minitems\n\t * entries.  We are using (2+,2) cuckoo hashing, which has an expected\n\t * maximum load factor of at least ~0.86, so 0.75 is a conservative load\n\t * factor that will typically allow mincells items to fit without ever\n\t * growing the table.\n\t */\n\tassert(LG_CKH_BUCKET_CELLS > 0);\n\tmincells = ((minitems + (3 - (minitems % 3))) / 3) << 2;\n\tfor (lg_mincells = LG_CKH_BUCKET_CELLS;\n\t    (ZU(1) << lg_mincells) < mincells;\n\t    lg_mincells++) {\n\t\t/* Do nothing. */\n\t}\n\tckh->lg_minbuckets = lg_mincells - LG_CKH_BUCKET_CELLS;\n\tckh->lg_curbuckets = lg_mincells - LG_CKH_BUCKET_CELLS;\n\tckh->hash = hash;\n\tckh->keycomp = keycomp;\n\n\tusize = sz_sa2u(sizeof(ckhc_t) << lg_mincells, CACHELINE);\n\tif (unlikely(usize == 0 || usize > LARGE_MAXCLASS)) {\n\t\tret = true;\n\t\tgoto label_return;\n\t}\n\tckh->tab = (ckhc_t *)ipallocztm(tsd_tsdn(tsd), usize, CACHELINE, true,\n\t    NULL, true, arena_ichoose(tsd, NULL));\n\tif (ckh->tab == NULL) {\n\t\tret = true;\n\t\tgoto label_return;\n\t}\n\n\tret = false;\nlabel_return:\n\treturn ret;\n}\n\nvoid\nckh_delete(tsd_t *tsd, ckh_t *ckh) {\n\tassert(ckh != NULL);\n\n#ifdef CKH_VERBOSE\n\tmalloc_printf(\n\t    \"%s(%p): ngrows: %\"FMTu64\", nshrinks: %\"FMTu64\",\"\n\t    \" nshrinkfails: %\"FMTu64\", ninserts: %\"FMTu64\",\"\n\t    \" nrelocs: %\"FMTu64\"\\n\", __func__, ckh,\n\t    (unsigned long long)ckh->ngrows,\n\t    (unsigned long long)ckh->nshrinks,\n\t    (unsigned long long)ckh->nshrinkfails,\n\t    (unsigned long long)ckh->ninserts,\n\t    (unsigned long long)ckh->nrelocs);\n#endif\n\n\tidalloctm(tsd_tsdn(tsd), ckh->tab, NULL, NULL, true, true);\n\tif (config_debug) {\n\t\tmemset(ckh, JEMALLOC_FREE_JUNK, sizeof(ckh_t));\n\t}\n}\n\nsize_t\nckh_count(ckh_t *ckh) {\n\tassert(ckh != NULL);\n\n\treturn ckh->count;\n}\n\nbool\nckh_iter(ckh_t *ckh, size_t *tabind, void **key, void **data) {\n\tsize_t i, ncells;\n\n\tfor (i = *tabind, ncells = (ZU(1) << (ckh->lg_curbuckets +\n\t    LG_CKH_BUCKET_CELLS)); i < ncells; i++) {\n\t\tif (ckh->tab[i].key != NULL) {\n\t\t\tif (key != NULL) {\n\t\t\t\t*key = (void *)ckh->tab[i].key;\n\t\t\t}\n\t\t\tif (data != NULL) {\n\t\t\t\t*data = (void *)ckh->tab[i].data;\n\t\t\t}\n\t\t\t*tabind = i + 1;\n\t\t\treturn false;\n\t\t}\n\t}\n\n\treturn true;\n}\n\nbool\nckh_insert(tsd_t *tsd, ckh_t *ckh, const void *key, const void *data) {\n\tbool ret;\n\n\tassert(ckh != NULL);\n\tassert(ckh_search(ckh, key, NULL, NULL));\n\n#ifdef CKH_COUNT\n\tckh->ninserts++;\n#endif\n\n\twhile (ckh_try_insert(ckh, &key, &data)) {\n\t\tif (ckh_grow(tsd, ckh)) {\n\t\t\tret = true;\n\t\t\tgoto label_return;\n\t\t}\n\t}\n\n\tret = false;\nlabel_return:\n\treturn ret;\n}\n\nbool\nckh_remove(tsd_t *tsd, ckh_t *ckh, const void *searchkey, void **key,\n    void **data) {\n\tsize_t cell;\n\n\tassert(ckh != NULL);\n\n\tcell = ckh_isearch(ckh, searchkey);\n\tif (cell != SIZE_T_MAX) {\n\t\tif (key != NULL) {\n\t\t\t*key = (void *)ckh->tab[cell].key;\n\t\t}\n\t\tif (data != NULL) {\n\t\t\t*data = (void *)ckh->tab[cell].data;\n\t\t}\n\t\tckh->tab[cell].key = NULL;\n\t\tckh->tab[cell].data = NULL; /* Not necessary. */\n\n\t\tckh->count--;\n\t\t/* Try to halve the table if it is less than 1/4 full. */\n\t\tif (ckh->count < (ZU(1) << (ckh->lg_curbuckets\n\t\t    + LG_CKH_BUCKET_CELLS - 2)) && ckh->lg_curbuckets\n\t\t    > ckh->lg_minbuckets) {\n\t\t\t/* Ignore error due to OOM. */\n\t\t\tckh_shrink(tsd, ckh);\n\t\t}\n\n\t\treturn false;\n\t}\n\n\treturn true;\n}\n\nbool\nckh_search(ckh_t *ckh, const void *searchkey, void **key, void **data) {\n\tsize_t cell;\n\n\tassert(ckh != NULL);\n\n\tcell = ckh_isearch(ckh, searchkey);\n\tif (cell != SIZE_T_MAX) {\n\t\tif (key != NULL) {\n\t\t\t*key = (void *)ckh->tab[cell].key;\n\t\t}\n\t\tif (data != NULL) {\n\t\t\t*data = (void *)ckh->tab[cell].data;\n\t\t}\n\t\treturn false;\n\t}\n\n\treturn true;\n}\n\nvoid\nckh_string_hash(const void *key, size_t r_hash[2]) {\n\thash(key, strlen((const char *)key), 0x94122f33U, r_hash);\n}\n\nbool\nckh_string_keycomp(const void *k1, const void *k2) {\n\tassert(k1 != NULL);\n\tassert(k2 != NULL);\n\n\treturn !strcmp((char *)k1, (char *)k2);\n}\n\nvoid\nckh_pointer_hash(const void *key, size_t r_hash[2]) {\n\tunion {\n\t\tconst void\t*v;\n\t\tsize_t\t\ti;\n\t} u;\n\n\tassert(sizeof(u.v) == sizeof(u.i));\n\tu.v = key;\n\thash(&u.i, sizeof(u.i), 0xd983396eU, r_hash);\n}\n\nbool\nckh_pointer_keycomp(const void *k1, const void *k2) {\n\treturn (k1 == k2);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/ctl.c",
    "content": "#define JEMALLOC_CTL_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/ctl.h\"\n#include \"jemalloc/internal/extent_dss.h\"\n#include \"jemalloc/internal/extent_mmap.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/nstime.h\"\n#include \"jemalloc/internal/size_classes.h\"\n#include \"jemalloc/internal/util.h\"\n\n/******************************************************************************/\n/* Data. */\n\n/*\n * ctl_mtx protects the following:\n * - ctl_stats->*\n */\nstatic malloc_mutex_t\tctl_mtx;\nstatic bool\t\tctl_initialized;\nstatic ctl_stats_t\t*ctl_stats;\nstatic ctl_arenas_t\t*ctl_arenas;\n\n/******************************************************************************/\n/* Helpers for named and indexed nodes. */\n\nstatic const ctl_named_node_t *\nctl_named_node(const ctl_node_t *node) {\n\treturn ((node->named) ? (const ctl_named_node_t *)node : NULL);\n}\n\nstatic const ctl_named_node_t *\nctl_named_children(const ctl_named_node_t *node, size_t index) {\n\tconst ctl_named_node_t *children = ctl_named_node(node->children);\n\n\treturn (children ? &children[index] : NULL);\n}\n\nstatic const ctl_indexed_node_t *\nctl_indexed_node(const ctl_node_t *node) {\n\treturn (!node->named ? (const ctl_indexed_node_t *)node : NULL);\n}\n\n/******************************************************************************/\n/* Function prototypes for non-inline static functions. */\n\n#define CTL_PROTO(n)\t\t\t\t\t\t\t\\\nstatic int\tn##_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\t\\\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen);\n\n#define INDEX_PROTO(n)\t\t\t\t\t\t\t\\\nstatic const ctl_named_node_t\t*n##_index(tsdn_t *tsdn,\t\t\\\n    const size_t *mib, size_t miblen, size_t i);\n\nCTL_PROTO(version)\nCTL_PROTO(epoch)\nCTL_PROTO(background_thread)\nCTL_PROTO(thread_tcache_enabled)\nCTL_PROTO(thread_tcache_flush)\nCTL_PROTO(thread_prof_name)\nCTL_PROTO(thread_prof_active)\nCTL_PROTO(thread_arena)\nCTL_PROTO(thread_allocated)\nCTL_PROTO(thread_allocatedp)\nCTL_PROTO(thread_deallocated)\nCTL_PROTO(thread_deallocatedp)\nCTL_PROTO(config_cache_oblivious)\nCTL_PROTO(config_debug)\nCTL_PROTO(config_fill)\nCTL_PROTO(config_lazy_lock)\nCTL_PROTO(config_malloc_conf)\nCTL_PROTO(config_prof)\nCTL_PROTO(config_prof_libgcc)\nCTL_PROTO(config_prof_libunwind)\nCTL_PROTO(config_stats)\nCTL_PROTO(config_thp)\nCTL_PROTO(config_utrace)\nCTL_PROTO(config_xmalloc)\nCTL_PROTO(opt_abort)\nCTL_PROTO(opt_abort_conf)\nCTL_PROTO(opt_retain)\nCTL_PROTO(opt_dss)\nCTL_PROTO(opt_narenas)\nCTL_PROTO(opt_percpu_arena)\nCTL_PROTO(opt_background_thread)\nCTL_PROTO(opt_dirty_decay_ms)\nCTL_PROTO(opt_muzzy_decay_ms)\nCTL_PROTO(opt_stats_print)\nCTL_PROTO(opt_stats_print_opts)\nCTL_PROTO(opt_junk)\nCTL_PROTO(opt_zero)\nCTL_PROTO(opt_utrace)\nCTL_PROTO(opt_xmalloc)\nCTL_PROTO(opt_tcache)\nCTL_PROTO(opt_lg_tcache_max)\nCTL_PROTO(opt_prof)\nCTL_PROTO(opt_prof_prefix)\nCTL_PROTO(opt_prof_active)\nCTL_PROTO(opt_prof_thread_active_init)\nCTL_PROTO(opt_lg_prof_sample)\nCTL_PROTO(opt_lg_prof_interval)\nCTL_PROTO(opt_prof_gdump)\nCTL_PROTO(opt_prof_final)\nCTL_PROTO(opt_prof_leak)\nCTL_PROTO(opt_prof_accum)\nCTL_PROTO(tcache_create)\nCTL_PROTO(tcache_flush)\nCTL_PROTO(tcache_destroy)\nCTL_PROTO(arena_i_initialized)\nCTL_PROTO(arena_i_decay)\nCTL_PROTO(arena_i_purge)\nCTL_PROTO(arena_i_reset)\nCTL_PROTO(arena_i_destroy)\nCTL_PROTO(arena_i_dss)\nCTL_PROTO(arena_i_dirty_decay_ms)\nCTL_PROTO(arena_i_muzzy_decay_ms)\nCTL_PROTO(arena_i_extent_hooks)\nINDEX_PROTO(arena_i)\nCTL_PROTO(arenas_bin_i_size)\nCTL_PROTO(arenas_bin_i_nregs)\nCTL_PROTO(arenas_bin_i_slab_size)\nINDEX_PROTO(arenas_bin_i)\nCTL_PROTO(arenas_lextent_i_size)\nINDEX_PROTO(arenas_lextent_i)\nCTL_PROTO(arenas_narenas)\nCTL_PROTO(arenas_dirty_decay_ms)\nCTL_PROTO(arenas_muzzy_decay_ms)\nCTL_PROTO(arenas_quantum)\nCTL_PROTO(arenas_page)\nCTL_PROTO(arenas_tcache_max)\nCTL_PROTO(arenas_nbins)\nCTL_PROTO(arenas_nhbins)\nCTL_PROTO(arenas_nlextents)\nCTL_PROTO(arenas_create)\nCTL_PROTO(arenas_lookup)\nCTL_PROTO(prof_thread_active_init)\nCTL_PROTO(prof_active)\nCTL_PROTO(prof_dump)\nCTL_PROTO(prof_gdump)\nCTL_PROTO(prof_reset)\nCTL_PROTO(prof_interval)\nCTL_PROTO(lg_prof_sample)\nCTL_PROTO(stats_arenas_i_small_allocated)\nCTL_PROTO(stats_arenas_i_small_nmalloc)\nCTL_PROTO(stats_arenas_i_small_ndalloc)\nCTL_PROTO(stats_arenas_i_small_nrequests)\nCTL_PROTO(stats_arenas_i_large_allocated)\nCTL_PROTO(stats_arenas_i_large_nmalloc)\nCTL_PROTO(stats_arenas_i_large_ndalloc)\nCTL_PROTO(stats_arenas_i_large_nrequests)\nCTL_PROTO(stats_arenas_i_bins_j_nmalloc)\nCTL_PROTO(stats_arenas_i_bins_j_ndalloc)\nCTL_PROTO(stats_arenas_i_bins_j_nrequests)\nCTL_PROTO(stats_arenas_i_bins_j_curregs)\nCTL_PROTO(stats_arenas_i_bins_j_nfills)\nCTL_PROTO(stats_arenas_i_bins_j_nflushes)\nCTL_PROTO(stats_arenas_i_bins_j_nslabs)\nCTL_PROTO(stats_arenas_i_bins_j_nreslabs)\nCTL_PROTO(stats_arenas_i_bins_j_curslabs)\nINDEX_PROTO(stats_arenas_i_bins_j)\nCTL_PROTO(stats_arenas_i_lextents_j_nmalloc)\nCTL_PROTO(stats_arenas_i_lextents_j_ndalloc)\nCTL_PROTO(stats_arenas_i_lextents_j_nrequests)\nCTL_PROTO(stats_arenas_i_lextents_j_curlextents)\nINDEX_PROTO(stats_arenas_i_lextents_j)\nCTL_PROTO(stats_arenas_i_nthreads)\nCTL_PROTO(stats_arenas_i_uptime)\nCTL_PROTO(stats_arenas_i_dss)\nCTL_PROTO(stats_arenas_i_dirty_decay_ms)\nCTL_PROTO(stats_arenas_i_muzzy_decay_ms)\nCTL_PROTO(stats_arenas_i_pactive)\nCTL_PROTO(stats_arenas_i_pdirty)\nCTL_PROTO(stats_arenas_i_pmuzzy)\nCTL_PROTO(stats_arenas_i_mapped)\nCTL_PROTO(stats_arenas_i_retained)\nCTL_PROTO(stats_arenas_i_dirty_npurge)\nCTL_PROTO(stats_arenas_i_dirty_nmadvise)\nCTL_PROTO(stats_arenas_i_dirty_purged)\nCTL_PROTO(stats_arenas_i_muzzy_npurge)\nCTL_PROTO(stats_arenas_i_muzzy_nmadvise)\nCTL_PROTO(stats_arenas_i_muzzy_purged)\nCTL_PROTO(stats_arenas_i_base)\nCTL_PROTO(stats_arenas_i_internal)\nCTL_PROTO(stats_arenas_i_tcache_bytes)\nCTL_PROTO(stats_arenas_i_resident)\nINDEX_PROTO(stats_arenas_i)\nCTL_PROTO(stats_allocated)\nCTL_PROTO(stats_active)\nCTL_PROTO(stats_background_thread_num_threads)\nCTL_PROTO(stats_background_thread_num_runs)\nCTL_PROTO(stats_background_thread_run_interval)\nCTL_PROTO(stats_metadata)\nCTL_PROTO(stats_resident)\nCTL_PROTO(stats_mapped)\nCTL_PROTO(stats_retained)\n\n#define MUTEX_STATS_CTL_PROTO_GEN(n)\t\t\t\t\t\\\nCTL_PROTO(stats_##n##_num_ops)\t\t\t\t\t\t\\\nCTL_PROTO(stats_##n##_num_wait)\t\t\t\t\t\t\\\nCTL_PROTO(stats_##n##_num_spin_acq)\t\t\t\t\t\\\nCTL_PROTO(stats_##n##_num_owner_switch)\t\t\t\t\t\\\nCTL_PROTO(stats_##n##_total_wait_time)\t\t\t\t\t\\\nCTL_PROTO(stats_##n##_max_wait_time)\t\t\t\t\t\\\nCTL_PROTO(stats_##n##_max_num_thds)\n\n/* Global mutexes. */\n#define OP(mtx) MUTEX_STATS_CTL_PROTO_GEN(mutexes_##mtx)\nMUTEX_PROF_GLOBAL_MUTEXES\n#undef OP\n\n/* Per arena mutexes. */\n#define OP(mtx) MUTEX_STATS_CTL_PROTO_GEN(arenas_i_mutexes_##mtx)\nMUTEX_PROF_ARENA_MUTEXES\n#undef OP\n\n/* Arena bin mutexes. */\nMUTEX_STATS_CTL_PROTO_GEN(arenas_i_bins_j_mutex)\n#undef MUTEX_STATS_CTL_PROTO_GEN\n\nCTL_PROTO(stats_mutexes_reset)\n\n/******************************************************************************/\n/* mallctl tree. */\n\n#define NAME(n)\t{true},\tn\n#define CHILD(t, c)\t\t\t\t\t\t\t\\\n\tsizeof(c##_node) / sizeof(ctl_##t##_node_t),\t\t\t\\\n\t(ctl_node_t *)c##_node,\t\t\t\t\t\t\\\n\tNULL\n#define CTL(c)\t0, NULL, c##_ctl\n\n/*\n * Only handles internal indexed nodes, since there are currently no external\n * ones.\n */\n#define INDEX(i)\t{false},\ti##_index\n\nstatic const ctl_named_node_t\tthread_tcache_node[] = {\n\t{NAME(\"enabled\"),\tCTL(thread_tcache_enabled)},\n\t{NAME(\"flush\"),\t\tCTL(thread_tcache_flush)}\n};\n\nstatic const ctl_named_node_t\tthread_prof_node[] = {\n\t{NAME(\"name\"),\t\tCTL(thread_prof_name)},\n\t{NAME(\"active\"),\tCTL(thread_prof_active)}\n};\n\nstatic const ctl_named_node_t\tthread_node[] = {\n\t{NAME(\"arena\"),\t\tCTL(thread_arena)},\n\t{NAME(\"allocated\"),\tCTL(thread_allocated)},\n\t{NAME(\"allocatedp\"),\tCTL(thread_allocatedp)},\n\t{NAME(\"deallocated\"),\tCTL(thread_deallocated)},\n\t{NAME(\"deallocatedp\"),\tCTL(thread_deallocatedp)},\n\t{NAME(\"tcache\"),\tCHILD(named, thread_tcache)},\n\t{NAME(\"prof\"),\t\tCHILD(named, thread_prof)}\n};\n\nstatic const ctl_named_node_t\tconfig_node[] = {\n\t{NAME(\"cache_oblivious\"), CTL(config_cache_oblivious)},\n\t{NAME(\"debug\"),\t\tCTL(config_debug)},\n\t{NAME(\"fill\"),\t\tCTL(config_fill)},\n\t{NAME(\"lazy_lock\"),\tCTL(config_lazy_lock)},\n\t{NAME(\"malloc_conf\"),\tCTL(config_malloc_conf)},\n\t{NAME(\"prof\"),\t\tCTL(config_prof)},\n\t{NAME(\"prof_libgcc\"),\tCTL(config_prof_libgcc)},\n\t{NAME(\"prof_libunwind\"), CTL(config_prof_libunwind)},\n\t{NAME(\"stats\"),\t\tCTL(config_stats)},\n\t{NAME(\"thp\"),\t\tCTL(config_thp)},\n\t{NAME(\"utrace\"),\tCTL(config_utrace)},\n\t{NAME(\"xmalloc\"),\tCTL(config_xmalloc)}\n};\n\nstatic const ctl_named_node_t opt_node[] = {\n\t{NAME(\"abort\"),\t\tCTL(opt_abort)},\n\t{NAME(\"abort_conf\"),\tCTL(opt_abort_conf)},\n\t{NAME(\"retain\"),\tCTL(opt_retain)},\n\t{NAME(\"dss\"),\t\tCTL(opt_dss)},\n\t{NAME(\"narenas\"),\tCTL(opt_narenas)},\n\t{NAME(\"percpu_arena\"),\tCTL(opt_percpu_arena)},\n\t{NAME(\"background_thread\"),\tCTL(opt_background_thread)},\n\t{NAME(\"dirty_decay_ms\"), CTL(opt_dirty_decay_ms)},\n\t{NAME(\"muzzy_decay_ms\"), CTL(opt_muzzy_decay_ms)},\n\t{NAME(\"stats_print\"),\tCTL(opt_stats_print)},\n\t{NAME(\"stats_print_opts\"),\tCTL(opt_stats_print_opts)},\n\t{NAME(\"junk\"),\t\tCTL(opt_junk)},\n\t{NAME(\"zero\"),\t\tCTL(opt_zero)},\n\t{NAME(\"utrace\"),\tCTL(opt_utrace)},\n\t{NAME(\"xmalloc\"),\tCTL(opt_xmalloc)},\n\t{NAME(\"tcache\"),\tCTL(opt_tcache)},\n\t{NAME(\"lg_tcache_max\"),\tCTL(opt_lg_tcache_max)},\n\t{NAME(\"prof\"),\t\tCTL(opt_prof)},\n\t{NAME(\"prof_prefix\"),\tCTL(opt_prof_prefix)},\n\t{NAME(\"prof_active\"),\tCTL(opt_prof_active)},\n\t{NAME(\"prof_thread_active_init\"), CTL(opt_prof_thread_active_init)},\n\t{NAME(\"lg_prof_sample\"), CTL(opt_lg_prof_sample)},\n\t{NAME(\"lg_prof_interval\"), CTL(opt_lg_prof_interval)},\n\t{NAME(\"prof_gdump\"),\tCTL(opt_prof_gdump)},\n\t{NAME(\"prof_final\"),\tCTL(opt_prof_final)},\n\t{NAME(\"prof_leak\"),\tCTL(opt_prof_leak)},\n\t{NAME(\"prof_accum\"),\tCTL(opt_prof_accum)}\n};\n\nstatic const ctl_named_node_t\ttcache_node[] = {\n\t{NAME(\"create\"),\tCTL(tcache_create)},\n\t{NAME(\"flush\"),\t\tCTL(tcache_flush)},\n\t{NAME(\"destroy\"),\tCTL(tcache_destroy)}\n};\n\nstatic const ctl_named_node_t arena_i_node[] = {\n\t{NAME(\"initialized\"),\tCTL(arena_i_initialized)},\n\t{NAME(\"decay\"),\t\tCTL(arena_i_decay)},\n\t{NAME(\"purge\"),\t\tCTL(arena_i_purge)},\n\t{NAME(\"reset\"),\t\tCTL(arena_i_reset)},\n\t{NAME(\"destroy\"),\tCTL(arena_i_destroy)},\n\t{NAME(\"dss\"),\t\tCTL(arena_i_dss)},\n\t{NAME(\"dirty_decay_ms\"), CTL(arena_i_dirty_decay_ms)},\n\t{NAME(\"muzzy_decay_ms\"), CTL(arena_i_muzzy_decay_ms)},\n\t{NAME(\"extent_hooks\"),\tCTL(arena_i_extent_hooks)}\n};\nstatic const ctl_named_node_t super_arena_i_node[] = {\n\t{NAME(\"\"),\t\tCHILD(named, arena_i)}\n};\n\nstatic const ctl_indexed_node_t arena_node[] = {\n\t{INDEX(arena_i)}\n};\n\nstatic const ctl_named_node_t arenas_bin_i_node[] = {\n\t{NAME(\"size\"),\t\tCTL(arenas_bin_i_size)},\n\t{NAME(\"nregs\"),\t\tCTL(arenas_bin_i_nregs)},\n\t{NAME(\"slab_size\"),\tCTL(arenas_bin_i_slab_size)}\n};\nstatic const ctl_named_node_t super_arenas_bin_i_node[] = {\n\t{NAME(\"\"),\t\tCHILD(named, arenas_bin_i)}\n};\n\nstatic const ctl_indexed_node_t arenas_bin_node[] = {\n\t{INDEX(arenas_bin_i)}\n};\n\nstatic const ctl_named_node_t arenas_lextent_i_node[] = {\n\t{NAME(\"size\"),\t\tCTL(arenas_lextent_i_size)}\n};\nstatic const ctl_named_node_t super_arenas_lextent_i_node[] = {\n\t{NAME(\"\"),\t\tCHILD(named, arenas_lextent_i)}\n};\n\nstatic const ctl_indexed_node_t arenas_lextent_node[] = {\n\t{INDEX(arenas_lextent_i)}\n};\n\nstatic const ctl_named_node_t arenas_node[] = {\n\t{NAME(\"narenas\"),\tCTL(arenas_narenas)},\n\t{NAME(\"dirty_decay_ms\"), CTL(arenas_dirty_decay_ms)},\n\t{NAME(\"muzzy_decay_ms\"), CTL(arenas_muzzy_decay_ms)},\n\t{NAME(\"quantum\"),\tCTL(arenas_quantum)},\n\t{NAME(\"page\"),\t\tCTL(arenas_page)},\n\t{NAME(\"tcache_max\"),\tCTL(arenas_tcache_max)},\n\t{NAME(\"nbins\"),\t\tCTL(arenas_nbins)},\n\t{NAME(\"nhbins\"),\tCTL(arenas_nhbins)},\n\t{NAME(\"bin\"),\t\tCHILD(indexed, arenas_bin)},\n\t{NAME(\"nlextents\"),\tCTL(arenas_nlextents)},\n\t{NAME(\"lextent\"),\tCHILD(indexed, arenas_lextent)},\n\t{NAME(\"create\"),\tCTL(arenas_create)},\n\t{NAME(\"lookup\"),\tCTL(arenas_lookup)}\n};\n\nstatic const ctl_named_node_t\tprof_node[] = {\n\t{NAME(\"thread_active_init\"), CTL(prof_thread_active_init)},\n\t{NAME(\"active\"),\tCTL(prof_active)},\n\t{NAME(\"dump\"),\t\tCTL(prof_dump)},\n\t{NAME(\"gdump\"),\t\tCTL(prof_gdump)},\n\t{NAME(\"reset\"),\t\tCTL(prof_reset)},\n\t{NAME(\"interval\"),\tCTL(prof_interval)},\n\t{NAME(\"lg_sample\"),\tCTL(lg_prof_sample)}\n};\n\nstatic const ctl_named_node_t stats_arenas_i_small_node[] = {\n\t{NAME(\"allocated\"),\tCTL(stats_arenas_i_small_allocated)},\n\t{NAME(\"nmalloc\"),\tCTL(stats_arenas_i_small_nmalloc)},\n\t{NAME(\"ndalloc\"),\tCTL(stats_arenas_i_small_ndalloc)},\n\t{NAME(\"nrequests\"),\tCTL(stats_arenas_i_small_nrequests)}\n};\n\nstatic const ctl_named_node_t stats_arenas_i_large_node[] = {\n\t{NAME(\"allocated\"),\tCTL(stats_arenas_i_large_allocated)},\n\t{NAME(\"nmalloc\"),\tCTL(stats_arenas_i_large_nmalloc)},\n\t{NAME(\"ndalloc\"),\tCTL(stats_arenas_i_large_ndalloc)},\n\t{NAME(\"nrequests\"),\tCTL(stats_arenas_i_large_nrequests)}\n};\n\n#define MUTEX_PROF_DATA_NODE(prefix)\t\t\t\t\t\\\nstatic const ctl_named_node_t stats_##prefix##_node[] = {\t\t\\\n\t{NAME(\"num_ops\"),\t\t\t\t\t\t\\\n\t CTL(stats_##prefix##_num_ops)},\t\t\t\t\\\n\t{NAME(\"num_wait\"),\t\t\t\t\t\t\\\n\t CTL(stats_##prefix##_num_wait)},\t\t\t\t\\\n\t{NAME(\"num_spin_acq\"),\t\t\t\t\t\t\\\n\t CTL(stats_##prefix##_num_spin_acq)},\t\t\t\t\\\n\t{NAME(\"num_owner_switch\"),\t\t\t\t\t\\\n\t CTL(stats_##prefix##_num_owner_switch)},\t\t\t\\\n\t{NAME(\"total_wait_time\"),\t\t\t\t\t\\\n\t CTL(stats_##prefix##_total_wait_time)},\t\t\t\\\n\t{NAME(\"max_wait_time\"),\t\t\t\t\t\t\\\n\t CTL(stats_##prefix##_max_wait_time)},\t\t\t\t\\\n\t{NAME(\"max_num_thds\"),\t\t\t\t\t\t\\\n\t CTL(stats_##prefix##_max_num_thds)}\t\t\t\t\\\n\t/* Note that # of current waiting thread not provided. */\t\\\n};\n\nMUTEX_PROF_DATA_NODE(arenas_i_bins_j_mutex)\n\nstatic const ctl_named_node_t stats_arenas_i_bins_j_node[] = {\n\t{NAME(\"nmalloc\"),\tCTL(stats_arenas_i_bins_j_nmalloc)},\n\t{NAME(\"ndalloc\"),\tCTL(stats_arenas_i_bins_j_ndalloc)},\n\t{NAME(\"nrequests\"),\tCTL(stats_arenas_i_bins_j_nrequests)},\n\t{NAME(\"curregs\"),\tCTL(stats_arenas_i_bins_j_curregs)},\n\t{NAME(\"nfills\"),\tCTL(stats_arenas_i_bins_j_nfills)},\n\t{NAME(\"nflushes\"),\tCTL(stats_arenas_i_bins_j_nflushes)},\n\t{NAME(\"nslabs\"),\tCTL(stats_arenas_i_bins_j_nslabs)},\n\t{NAME(\"nreslabs\"),\tCTL(stats_arenas_i_bins_j_nreslabs)},\n\t{NAME(\"curslabs\"),\tCTL(stats_arenas_i_bins_j_curslabs)},\n\t{NAME(\"mutex\"),\t\tCHILD(named, stats_arenas_i_bins_j_mutex)}\n};\n\nstatic const ctl_named_node_t super_stats_arenas_i_bins_j_node[] = {\n\t{NAME(\"\"),\t\tCHILD(named, stats_arenas_i_bins_j)}\n};\n\nstatic const ctl_indexed_node_t stats_arenas_i_bins_node[] = {\n\t{INDEX(stats_arenas_i_bins_j)}\n};\n\nstatic const ctl_named_node_t stats_arenas_i_lextents_j_node[] = {\n\t{NAME(\"nmalloc\"),\tCTL(stats_arenas_i_lextents_j_nmalloc)},\n\t{NAME(\"ndalloc\"),\tCTL(stats_arenas_i_lextents_j_ndalloc)},\n\t{NAME(\"nrequests\"),\tCTL(stats_arenas_i_lextents_j_nrequests)},\n\t{NAME(\"curlextents\"),\tCTL(stats_arenas_i_lextents_j_curlextents)}\n};\nstatic const ctl_named_node_t super_stats_arenas_i_lextents_j_node[] = {\n\t{NAME(\"\"),\t\tCHILD(named, stats_arenas_i_lextents_j)}\n};\n\nstatic const ctl_indexed_node_t stats_arenas_i_lextents_node[] = {\n\t{INDEX(stats_arenas_i_lextents_j)}\n};\n\n#define OP(mtx)  MUTEX_PROF_DATA_NODE(arenas_i_mutexes_##mtx)\nMUTEX_PROF_ARENA_MUTEXES\n#undef OP\n\nstatic const ctl_named_node_t stats_arenas_i_mutexes_node[] = {\n#define OP(mtx) {NAME(#mtx), CHILD(named, stats_arenas_i_mutexes_##mtx)},\nMUTEX_PROF_ARENA_MUTEXES\n#undef OP\n};\n\nstatic const ctl_named_node_t stats_arenas_i_node[] = {\n\t{NAME(\"nthreads\"),\tCTL(stats_arenas_i_nthreads)},\n\t{NAME(\"uptime\"),\tCTL(stats_arenas_i_uptime)},\n\t{NAME(\"dss\"),\t\tCTL(stats_arenas_i_dss)},\n\t{NAME(\"dirty_decay_ms\"), CTL(stats_arenas_i_dirty_decay_ms)},\n\t{NAME(\"muzzy_decay_ms\"), CTL(stats_arenas_i_muzzy_decay_ms)},\n\t{NAME(\"pactive\"),\tCTL(stats_arenas_i_pactive)},\n\t{NAME(\"pdirty\"),\tCTL(stats_arenas_i_pdirty)},\n\t{NAME(\"pmuzzy\"),\tCTL(stats_arenas_i_pmuzzy)},\n\t{NAME(\"mapped\"),\tCTL(stats_arenas_i_mapped)},\n\t{NAME(\"retained\"),\tCTL(stats_arenas_i_retained)},\n\t{NAME(\"dirty_npurge\"),\tCTL(stats_arenas_i_dirty_npurge)},\n\t{NAME(\"dirty_nmadvise\"), CTL(stats_arenas_i_dirty_nmadvise)},\n\t{NAME(\"dirty_purged\"),\tCTL(stats_arenas_i_dirty_purged)},\n\t{NAME(\"muzzy_npurge\"),\tCTL(stats_arenas_i_muzzy_npurge)},\n\t{NAME(\"muzzy_nmadvise\"), CTL(stats_arenas_i_muzzy_nmadvise)},\n\t{NAME(\"muzzy_purged\"),\tCTL(stats_arenas_i_muzzy_purged)},\n\t{NAME(\"base\"),\t\tCTL(stats_arenas_i_base)},\n\t{NAME(\"internal\"),\tCTL(stats_arenas_i_internal)},\n\t{NAME(\"tcache_bytes\"),\tCTL(stats_arenas_i_tcache_bytes)},\n\t{NAME(\"resident\"),\tCTL(stats_arenas_i_resident)},\n\t{NAME(\"small\"),\t\tCHILD(named, stats_arenas_i_small)},\n\t{NAME(\"large\"),\t\tCHILD(named, stats_arenas_i_large)},\n\t{NAME(\"bins\"),\t\tCHILD(indexed, stats_arenas_i_bins)},\n\t{NAME(\"lextents\"),\tCHILD(indexed, stats_arenas_i_lextents)},\n\t{NAME(\"mutexes\"),\tCHILD(named, stats_arenas_i_mutexes)}\n};\nstatic const ctl_named_node_t super_stats_arenas_i_node[] = {\n\t{NAME(\"\"),\t\tCHILD(named, stats_arenas_i)}\n};\n\nstatic const ctl_indexed_node_t stats_arenas_node[] = {\n\t{INDEX(stats_arenas_i)}\n};\n\nstatic const ctl_named_node_t stats_background_thread_node[] = {\n\t{NAME(\"num_threads\"),\tCTL(stats_background_thread_num_threads)},\n\t{NAME(\"num_runs\"),\tCTL(stats_background_thread_num_runs)},\n\t{NAME(\"run_interval\"),\tCTL(stats_background_thread_run_interval)}\n};\n\n#define OP(mtx) MUTEX_PROF_DATA_NODE(mutexes_##mtx)\nMUTEX_PROF_GLOBAL_MUTEXES\n#undef OP\n\nstatic const ctl_named_node_t stats_mutexes_node[] = {\n#define OP(mtx) {NAME(#mtx), CHILD(named, stats_mutexes_##mtx)},\nMUTEX_PROF_GLOBAL_MUTEXES\n#undef OP\n\t{NAME(\"reset\"),\t\tCTL(stats_mutexes_reset)}\n};\n#undef MUTEX_PROF_DATA_NODE\n\nstatic const ctl_named_node_t stats_node[] = {\n\t{NAME(\"allocated\"),\tCTL(stats_allocated)},\n\t{NAME(\"active\"),\tCTL(stats_active)},\n\t{NAME(\"metadata\"),\tCTL(stats_metadata)},\n\t{NAME(\"resident\"),\tCTL(stats_resident)},\n\t{NAME(\"mapped\"),\tCTL(stats_mapped)},\n\t{NAME(\"retained\"),\tCTL(stats_retained)},\n\t{NAME(\"background_thread\"),\n\t CHILD(named, stats_background_thread)},\n\t{NAME(\"mutexes\"),\tCHILD(named, stats_mutexes)},\n\t{NAME(\"arenas\"),\tCHILD(indexed, stats_arenas)}\n};\n\nstatic const ctl_named_node_t\troot_node[] = {\n\t{NAME(\"version\"),\tCTL(version)},\n\t{NAME(\"epoch\"),\t\tCTL(epoch)},\n\t{NAME(\"background_thread\"),\tCTL(background_thread)},\n\t{NAME(\"thread\"),\tCHILD(named, thread)},\n\t{NAME(\"config\"),\tCHILD(named, config)},\n\t{NAME(\"opt\"),\t\tCHILD(named, opt)},\n\t{NAME(\"tcache\"),\tCHILD(named, tcache)},\n\t{NAME(\"arena\"),\t\tCHILD(indexed, arena)},\n\t{NAME(\"arenas\"),\tCHILD(named, arenas)},\n\t{NAME(\"prof\"),\t\tCHILD(named, prof)},\n\t{NAME(\"stats\"),\t\tCHILD(named, stats)}\n};\nstatic const ctl_named_node_t super_root_node[] = {\n\t{NAME(\"\"),\t\tCHILD(named, root)}\n};\n\n#undef NAME\n#undef CHILD\n#undef CTL\n#undef INDEX\n\n/******************************************************************************/\n\n/*\n * Sets *dst + *src non-atomically.  This is safe, since everything is\n * synchronized by the ctl mutex.\n */\nstatic void\naccum_arena_stats_u64(arena_stats_u64_t *dst, arena_stats_u64_t *src) {\n#ifdef JEMALLOC_ATOMIC_U64\n\tuint64_t cur_dst = atomic_load_u64(dst, ATOMIC_RELAXED);\n\tuint64_t cur_src = atomic_load_u64(src, ATOMIC_RELAXED);\n\tatomic_store_u64(dst, cur_dst + cur_src, ATOMIC_RELAXED);\n#else\n\t*dst += *src;\n#endif\n}\n\n/* Likewise: with ctl mutex synchronization, reading is simple. */\nstatic uint64_t\narena_stats_read_u64(arena_stats_u64_t *p) {\n#ifdef JEMALLOC_ATOMIC_U64\n\treturn atomic_load_u64(p, ATOMIC_RELAXED);\n#else\n\treturn *p;\n#endif\n}\n\nstatic void accum_atomic_zu(atomic_zu_t *dst, atomic_zu_t *src) {\n\tsize_t cur_dst = atomic_load_zu(dst, ATOMIC_RELAXED);\n\tsize_t cur_src = atomic_load_zu(src, ATOMIC_RELAXED);\n\tatomic_store_zu(dst, cur_dst + cur_src, ATOMIC_RELAXED);\n}\n\n/******************************************************************************/\n\nstatic unsigned\narenas_i2a_impl(size_t i, bool compat, bool validate) {\n\tunsigned a;\n\n\tswitch (i) {\n\tcase MALLCTL_ARENAS_ALL:\n\t\ta = 0;\n\t\tbreak;\n\tcase MALLCTL_ARENAS_DESTROYED:\n\t\ta = 1;\n\t\tbreak;\n\tdefault:\n\t\tif (compat && i == ctl_arenas->narenas) {\n\t\t\t/*\n\t\t\t * Provide deprecated backward compatibility for\n\t\t\t * accessing the merged stats at index narenas rather\n\t\t\t * than via MALLCTL_ARENAS_ALL.  This is scheduled for\n\t\t\t * removal in 6.0.0.\n\t\t\t */\n\t\t\ta = 0;\n\t\t} else if (validate && i >= ctl_arenas->narenas) {\n\t\t\ta = UINT_MAX;\n\t\t} else {\n\t\t\t/*\n\t\t\t * This function should never be called for an index\n\t\t\t * more than one past the range of indices that have\n\t\t\t * initialized ctl data.\n\t\t\t */\n\t\t\tassert(i < ctl_arenas->narenas || (!validate && i ==\n\t\t\t    ctl_arenas->narenas));\n\t\t\ta = (unsigned)i + 2;\n\t\t}\n\t\tbreak;\n\t}\n\n\treturn a;\n}\n\nstatic unsigned\narenas_i2a(size_t i) {\n\treturn arenas_i2a_impl(i, true, false);\n}\n\nstatic ctl_arena_t *\narenas_i_impl(tsdn_t *tsdn, size_t i, bool compat, bool init) {\n\tctl_arena_t *ret;\n\n\tassert(!compat || !init);\n\n\tret = ctl_arenas->arenas[arenas_i2a_impl(i, compat, false)];\n\tif (init && ret == NULL) {\n\t\tif (config_stats) {\n\t\t\tstruct container_s {\n\t\t\t\tctl_arena_t\t\tctl_arena;\n\t\t\t\tctl_arena_stats_t\tastats;\n\t\t\t};\n\t\t\tstruct container_s *cont =\n\t\t\t    (struct container_s *)base_alloc(tsdn, b0get(),\n\t\t\t    sizeof(struct container_s), QUANTUM);\n\t\t\tif (cont == NULL) {\n\t\t\t\treturn NULL;\n\t\t\t}\n\t\t\tret = &cont->ctl_arena;\n\t\t\tret->astats = &cont->astats;\n\t\t} else {\n\t\t\tret = (ctl_arena_t *)base_alloc(tsdn, b0get(),\n\t\t\t    sizeof(ctl_arena_t), QUANTUM);\n\t\t\tif (ret == NULL) {\n\t\t\t\treturn NULL;\n\t\t\t}\n\t\t}\n\t\tret->arena_ind = (unsigned)i;\n\t\tctl_arenas->arenas[arenas_i2a_impl(i, compat, false)] = ret;\n\t}\n\n\tassert(ret == NULL || arenas_i2a(ret->arena_ind) == arenas_i2a(i));\n\treturn ret;\n}\n\nstatic ctl_arena_t *\narenas_i(size_t i) {\n\tctl_arena_t *ret = arenas_i_impl(TSDN_NULL, i, true, false);\n\tassert(ret != NULL);\n\treturn ret;\n}\n\nstatic void\nctl_arena_clear(ctl_arena_t *ctl_arena) {\n\tctl_arena->nthreads = 0;\n\tctl_arena->dss = dss_prec_names[dss_prec_limit];\n\tctl_arena->dirty_decay_ms = -1;\n\tctl_arena->muzzy_decay_ms = -1;\n\tctl_arena->pactive = 0;\n\tctl_arena->pdirty = 0;\n\tctl_arena->pmuzzy = 0;\n\tif (config_stats) {\n\t\tmemset(&ctl_arena->astats->astats, 0, sizeof(arena_stats_t));\n\t\tctl_arena->astats->allocated_small = 0;\n\t\tctl_arena->astats->nmalloc_small = 0;\n\t\tctl_arena->astats->ndalloc_small = 0;\n\t\tctl_arena->astats->nrequests_small = 0;\n\t\tmemset(ctl_arena->astats->bstats, 0, NBINS *\n\t\t    sizeof(malloc_bin_stats_t));\n\t\tmemset(ctl_arena->astats->lstats, 0, (NSIZES - NBINS) *\n\t\t    sizeof(malloc_large_stats_t));\n\t}\n}\n\nstatic void\nctl_arena_stats_amerge(tsdn_t *tsdn, ctl_arena_t *ctl_arena, arena_t *arena) {\n\tunsigned i;\n\n\tif (config_stats) {\n\t\tarena_stats_merge(tsdn, arena, &ctl_arena->nthreads,\n\t\t    &ctl_arena->dss, &ctl_arena->dirty_decay_ms,\n\t\t    &ctl_arena->muzzy_decay_ms, &ctl_arena->pactive,\n\t\t    &ctl_arena->pdirty, &ctl_arena->pmuzzy,\n\t\t    &ctl_arena->astats->astats, ctl_arena->astats->bstats,\n\t\t    ctl_arena->astats->lstats);\n\n\t\tfor (i = 0; i < NBINS; i++) {\n\t\t\tctl_arena->astats->allocated_small +=\n\t\t\t    ctl_arena->astats->bstats[i].curregs *\n\t\t\t    sz_index2size(i);\n\t\t\tctl_arena->astats->nmalloc_small +=\n\t\t\t    ctl_arena->astats->bstats[i].nmalloc;\n\t\t\tctl_arena->astats->ndalloc_small +=\n\t\t\t    ctl_arena->astats->bstats[i].ndalloc;\n\t\t\tctl_arena->astats->nrequests_small +=\n\t\t\t    ctl_arena->astats->bstats[i].nrequests;\n\t\t}\n\t} else {\n\t\tarena_basic_stats_merge(tsdn, arena, &ctl_arena->nthreads,\n\t\t    &ctl_arena->dss, &ctl_arena->dirty_decay_ms,\n\t\t    &ctl_arena->muzzy_decay_ms, &ctl_arena->pactive,\n\t\t    &ctl_arena->pdirty, &ctl_arena->pmuzzy);\n\t}\n}\n\nstatic void\nctl_arena_stats_sdmerge(ctl_arena_t *ctl_sdarena, ctl_arena_t *ctl_arena,\n    bool destroyed) {\n\tunsigned i;\n\n\tif (!destroyed) {\n\t\tctl_sdarena->nthreads += ctl_arena->nthreads;\n\t\tctl_sdarena->pactive += ctl_arena->pactive;\n\t\tctl_sdarena->pdirty += ctl_arena->pdirty;\n\t\tctl_sdarena->pmuzzy += ctl_arena->pmuzzy;\n\t} else {\n\t\tassert(ctl_arena->nthreads == 0);\n\t\tassert(ctl_arena->pactive == 0);\n\t\tassert(ctl_arena->pdirty == 0);\n\t\tassert(ctl_arena->pmuzzy == 0);\n\t}\n\n\tif (config_stats) {\n\t\tctl_arena_stats_t *sdstats = ctl_sdarena->astats;\n\t\tctl_arena_stats_t *astats = ctl_arena->astats;\n\n\t\tif (!destroyed) {\n\t\t\taccum_atomic_zu(&sdstats->astats.mapped,\n\t\t\t    &astats->astats.mapped);\n\t\t\taccum_atomic_zu(&sdstats->astats.retained,\n\t\t\t    &astats->astats.retained);\n\t\t}\n\n\t\taccum_arena_stats_u64(&sdstats->astats.decay_dirty.npurge,\n\t\t    &astats->astats.decay_dirty.npurge);\n\t\taccum_arena_stats_u64(&sdstats->astats.decay_dirty.nmadvise,\n\t\t    &astats->astats.decay_dirty.nmadvise);\n\t\taccum_arena_stats_u64(&sdstats->astats.decay_dirty.purged,\n\t\t    &astats->astats.decay_dirty.purged);\n\n\t\taccum_arena_stats_u64(&sdstats->astats.decay_muzzy.npurge,\n\t\t    &astats->astats.decay_muzzy.npurge);\n\t\taccum_arena_stats_u64(&sdstats->astats.decay_muzzy.nmadvise,\n\t\t    &astats->astats.decay_muzzy.nmadvise);\n\t\taccum_arena_stats_u64(&sdstats->astats.decay_muzzy.purged,\n\t\t    &astats->astats.decay_muzzy.purged);\n\n#define OP(mtx) malloc_mutex_prof_merge(\t\t\t\t\\\n\t\t    &(sdstats->astats.mutex_prof_data[\t\t\t\\\n\t\t        arena_prof_mutex_##mtx]),\t\t\t\\\n\t\t    &(astats->astats.mutex_prof_data[\t\t\t\\\n\t\t        arena_prof_mutex_##mtx]));\nMUTEX_PROF_ARENA_MUTEXES\n#undef OP\n\t\tif (!destroyed) {\n\t\t\taccum_atomic_zu(&sdstats->astats.base,\n\t\t\t    &astats->astats.base);\n\t\t\taccum_atomic_zu(&sdstats->astats.internal,\n\t\t\t    &astats->astats.internal);\n\t\t\taccum_atomic_zu(&sdstats->astats.resident,\n\t\t\t    &astats->astats.resident);\n\t\t} else {\n\t\t\tassert(atomic_load_zu(\n\t\t\t    &astats->astats.internal, ATOMIC_RELAXED) == 0);\n\t\t}\n\n\t\tif (!destroyed) {\n\t\t\tsdstats->allocated_small += astats->allocated_small;\n\t\t} else {\n\t\t\tassert(astats->allocated_small == 0);\n\t\t}\n\t\tsdstats->nmalloc_small += astats->nmalloc_small;\n\t\tsdstats->ndalloc_small += astats->ndalloc_small;\n\t\tsdstats->nrequests_small += astats->nrequests_small;\n\n\t\tif (!destroyed) {\n\t\t\taccum_atomic_zu(&sdstats->astats.allocated_large,\n\t\t\t    &astats->astats.allocated_large);\n\t\t} else {\n\t\t\tassert(atomic_load_zu(&astats->astats.allocated_large,\n\t\t\t    ATOMIC_RELAXED) == 0);\n\t\t}\n\t\taccum_arena_stats_u64(&sdstats->astats.nmalloc_large,\n\t\t    &astats->astats.nmalloc_large);\n\t\taccum_arena_stats_u64(&sdstats->astats.ndalloc_large,\n\t\t    &astats->astats.ndalloc_large);\n\t\taccum_arena_stats_u64(&sdstats->astats.nrequests_large,\n\t\t    &astats->astats.nrequests_large);\n\n\t\taccum_atomic_zu(&sdstats->astats.tcache_bytes,\n\t\t    &astats->astats.tcache_bytes);\n\n\t\tif (ctl_arena->arena_ind == 0) {\n\t\t\tsdstats->astats.uptime = astats->astats.uptime;\n\t\t}\n\n\t\tfor (i = 0; i < NBINS; i++) {\n\t\t\tsdstats->bstats[i].nmalloc += astats->bstats[i].nmalloc;\n\t\t\tsdstats->bstats[i].ndalloc += astats->bstats[i].ndalloc;\n\t\t\tsdstats->bstats[i].nrequests +=\n\t\t\t    astats->bstats[i].nrequests;\n\t\t\tif (!destroyed) {\n\t\t\t\tsdstats->bstats[i].curregs +=\n\t\t\t\t    astats->bstats[i].curregs;\n\t\t\t} else {\n\t\t\t\tassert(astats->bstats[i].curregs == 0);\n\t\t\t}\n\t\t\tsdstats->bstats[i].nfills += astats->bstats[i].nfills;\n\t\t\tsdstats->bstats[i].nflushes +=\n\t\t\t    astats->bstats[i].nflushes;\n\t\t\tsdstats->bstats[i].nslabs += astats->bstats[i].nslabs;\n\t\t\tsdstats->bstats[i].reslabs += astats->bstats[i].reslabs;\n\t\t\tif (!destroyed) {\n\t\t\t\tsdstats->bstats[i].curslabs +=\n\t\t\t\t    astats->bstats[i].curslabs;\n\t\t\t} else {\n\t\t\t\tassert(astats->bstats[i].curslabs == 0);\n\t\t\t}\n\t\t\tmalloc_mutex_prof_merge(&sdstats->bstats[i].mutex_data,\n\t\t\t    &astats->bstats[i].mutex_data);\n\t\t}\n\n\t\tfor (i = 0; i < NSIZES - NBINS; i++) {\n\t\t\taccum_arena_stats_u64(&sdstats->lstats[i].nmalloc,\n\t\t\t    &astats->lstats[i].nmalloc);\n\t\t\taccum_arena_stats_u64(&sdstats->lstats[i].ndalloc,\n\t\t\t    &astats->lstats[i].ndalloc);\n\t\t\taccum_arena_stats_u64(&sdstats->lstats[i].nrequests,\n\t\t\t    &astats->lstats[i].nrequests);\n\t\t\tif (!destroyed) {\n\t\t\t\tsdstats->lstats[i].curlextents +=\n\t\t\t\t    astats->lstats[i].curlextents;\n\t\t\t} else {\n\t\t\t\tassert(astats->lstats[i].curlextents == 0);\n\t\t\t}\n\t\t}\n\t}\n}\n\nstatic void\nctl_arena_refresh(tsdn_t *tsdn, arena_t *arena, ctl_arena_t *ctl_sdarena,\n    unsigned i, bool destroyed) {\n\tctl_arena_t *ctl_arena = arenas_i(i);\n\n\tctl_arena_clear(ctl_arena);\n\tctl_arena_stats_amerge(tsdn, ctl_arena, arena);\n\t/* Merge into sum stats as well. */\n\tctl_arena_stats_sdmerge(ctl_sdarena, ctl_arena, destroyed);\n}\n\nstatic unsigned\nctl_arena_init(tsdn_t *tsdn, extent_hooks_t *extent_hooks) {\n\tunsigned arena_ind;\n\tctl_arena_t *ctl_arena;\n\n\tif ((ctl_arena = ql_last(&ctl_arenas->destroyed, destroyed_link)) !=\n\t    NULL) {\n\t\tql_remove(&ctl_arenas->destroyed, ctl_arena, destroyed_link);\n\t\tarena_ind = ctl_arena->arena_ind;\n\t} else {\n\t\tarena_ind = ctl_arenas->narenas;\n\t}\n\n\t/* Trigger stats allocation. */\n\tif (arenas_i_impl(tsdn, arena_ind, false, true) == NULL) {\n\t\treturn UINT_MAX;\n\t}\n\n\t/* Initialize new arena. */\n\tif (arena_init(tsdn, arena_ind, extent_hooks) == NULL) {\n\t\treturn UINT_MAX;\n\t}\n\n\tif (arena_ind == ctl_arenas->narenas) {\n\t\tctl_arenas->narenas++;\n\t}\n\n\treturn arena_ind;\n}\n\nstatic void\nctl_background_thread_stats_read(tsdn_t *tsdn) {\n\tbackground_thread_stats_t *stats = &ctl_stats->background_thread;\n\tif (!have_background_thread ||\n\t    background_thread_stats_read(tsdn, stats)) {\n\t\tmemset(stats, 0, sizeof(background_thread_stats_t));\n\t\tnstime_init(&stats->run_interval, 0);\n\t}\n}\n\nstatic void\nctl_refresh(tsdn_t *tsdn) {\n\tunsigned i;\n\tctl_arena_t *ctl_sarena = arenas_i(MALLCTL_ARENAS_ALL);\n\tVARIABLE_ARRAY(arena_t *, tarenas, ctl_arenas->narenas);\n\n\t/*\n\t * Clear sum stats, since they will be merged into by\n\t * ctl_arena_refresh().\n\t */\n\tctl_arena_clear(ctl_sarena);\n\n\tfor (i = 0; i < ctl_arenas->narenas; i++) {\n\t\ttarenas[i] = arena_get(tsdn, i, false);\n\t}\n\n\tfor (i = 0; i < ctl_arenas->narenas; i++) {\n\t\tctl_arena_t *ctl_arena = arenas_i(i);\n\t\tbool initialized = (tarenas[i] != NULL);\n\n\t\tctl_arena->initialized = initialized;\n\t\tif (initialized) {\n\t\t\tctl_arena_refresh(tsdn, tarenas[i], ctl_sarena, i,\n\t\t\t    false);\n\t\t}\n\t}\n\n\tif (config_stats) {\n\t\tctl_stats->allocated = ctl_sarena->astats->allocated_small +\n\t\t    atomic_load_zu(&ctl_sarena->astats->astats.allocated_large,\n\t\t\tATOMIC_RELAXED);\n\t\tctl_stats->active = (ctl_sarena->pactive << LG_PAGE);\n\t\tctl_stats->metadata = atomic_load_zu(\n\t\t    &ctl_sarena->astats->astats.base, ATOMIC_RELAXED) +\n\t\t    atomic_load_zu(&ctl_sarena->astats->astats.internal,\n\t\t\tATOMIC_RELAXED);\n\t\tctl_stats->resident = atomic_load_zu(\n\t\t    &ctl_sarena->astats->astats.resident, ATOMIC_RELAXED);\n\t\tctl_stats->mapped = atomic_load_zu(\n\t\t    &ctl_sarena->astats->astats.mapped, ATOMIC_RELAXED);\n\t\tctl_stats->retained = atomic_load_zu(\n\t\t    &ctl_sarena->astats->astats.retained, ATOMIC_RELAXED);\n\n\t\tctl_background_thread_stats_read(tsdn);\n\n#define READ_GLOBAL_MUTEX_PROF_DATA(i, mtx)\t\t\t\t\\\n    malloc_mutex_lock(tsdn, &mtx);\t\t\t\t\t\\\n    malloc_mutex_prof_read(tsdn, &ctl_stats->mutex_prof_data[i], &mtx);\t\\\n    malloc_mutex_unlock(tsdn, &mtx);\n\n\t\tif (config_prof && opt_prof) {\n\t\t\tREAD_GLOBAL_MUTEX_PROF_DATA(global_prof_mutex_prof,\n\t\t\t    bt2gctx_mtx);\n\t\t}\n\t\tif (have_background_thread) {\n\t\t\tREAD_GLOBAL_MUTEX_PROF_DATA(\n\t\t\t    global_prof_mutex_background_thread,\n\t\t\t    background_thread_lock);\n\t\t} else {\n\t\t\tmemset(&ctl_stats->mutex_prof_data[\n\t\t\t    global_prof_mutex_background_thread], 0,\n\t\t\t    sizeof(mutex_prof_data_t));\n\t\t}\n\t\t/* We own ctl mutex already. */\n\t\tmalloc_mutex_prof_read(tsdn,\n\t\t    &ctl_stats->mutex_prof_data[global_prof_mutex_ctl],\n\t\t    &ctl_mtx);\n#undef READ_GLOBAL_MUTEX_PROF_DATA\n\t}\n\tctl_arenas->epoch++;\n}\n\nstatic bool\nctl_init(tsdn_t *tsdn) {\n\tbool ret;\n\n\tmalloc_mutex_lock(tsdn, &ctl_mtx);\n\tif (!ctl_initialized) {\n\t\tctl_arena_t *ctl_sarena, *ctl_darena;\n\t\tunsigned i;\n\n\t\t/*\n\t\t * Allocate demand-zeroed space for pointers to the full\n\t\t * range of supported arena indices.\n\t\t */\n\t\tif (ctl_arenas == NULL) {\n\t\t\tctl_arenas = (ctl_arenas_t *)base_alloc(tsdn,\n\t\t\t    b0get(), sizeof(ctl_arenas_t), QUANTUM);\n\t\t\tif (ctl_arenas == NULL) {\n\t\t\t\tret = true;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\t\t}\n\n\t\tif (config_stats && ctl_stats == NULL) {\n\t\t\tctl_stats = (ctl_stats_t *)base_alloc(tsdn, b0get(),\n\t\t\t    sizeof(ctl_stats_t), QUANTUM);\n\t\t\tif (ctl_stats == NULL) {\n\t\t\t\tret = true;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\t\t}\n\n\t\t/*\n\t\t * Allocate space for the current full range of arenas\n\t\t * here rather than doing it lazily elsewhere, in order\n\t\t * to limit when OOM-caused errors can occur.\n\t\t */\n\t\tif ((ctl_sarena = arenas_i_impl(tsdn, MALLCTL_ARENAS_ALL, false,\n\t\t    true)) == NULL) {\n\t\t\tret = true;\n\t\t\tgoto label_return;\n\t\t}\n\t\tctl_sarena->initialized = true;\n\n\t\tif ((ctl_darena = arenas_i_impl(tsdn, MALLCTL_ARENAS_DESTROYED,\n\t\t    false, true)) == NULL) {\n\t\t\tret = true;\n\t\t\tgoto label_return;\n\t\t}\n\t\tctl_arena_clear(ctl_darena);\n\t\t/*\n\t\t * Don't toggle ctl_darena to initialized until an arena is\n\t\t * actually destroyed, so that arena.<i>.initialized can be used\n\t\t * to query whether the stats are relevant.\n\t\t */\n\n\t\tctl_arenas->narenas = narenas_total_get();\n\t\tfor (i = 0; i < ctl_arenas->narenas; i++) {\n\t\t\tif (arenas_i_impl(tsdn, i, false, true) == NULL) {\n\t\t\t\tret = true;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\t\t}\n\n\t\tql_new(&ctl_arenas->destroyed);\n\t\tctl_refresh(tsdn);\n\n\t\tctl_initialized = true;\n\t}\n\n\tret = false;\nlabel_return:\n\tmalloc_mutex_unlock(tsdn, &ctl_mtx);\n\treturn ret;\n}\n\nstatic int\nctl_lookup(tsdn_t *tsdn, const char *name, ctl_node_t const **nodesp,\n    size_t *mibp, size_t *depthp) {\n\tint ret;\n\tconst char *elm, *tdot, *dot;\n\tsize_t elen, i, j;\n\tconst ctl_named_node_t *node;\n\n\telm = name;\n\t/* Equivalent to strchrnul(). */\n\tdot = ((tdot = strchr(elm, '.')) != NULL) ? tdot : strchr(elm, '\\0');\n\telen = (size_t)((uintptr_t)dot - (uintptr_t)elm);\n\tif (elen == 0) {\n\t\tret = ENOENT;\n\t\tgoto label_return;\n\t}\n\tnode = super_root_node;\n\tfor (i = 0; i < *depthp; i++) {\n\t\tassert(node);\n\t\tassert(node->nchildren > 0);\n\t\tif (ctl_named_node(node->children) != NULL) {\n\t\t\tconst ctl_named_node_t *pnode = node;\n\n\t\t\t/* Children are named. */\n\t\t\tfor (j = 0; j < node->nchildren; j++) {\n\t\t\t\tconst ctl_named_node_t *child =\n\t\t\t\t    ctl_named_children(node, j);\n\t\t\t\tif (strlen(child->name) == elen &&\n\t\t\t\t    strncmp(elm, child->name, elen) == 0) {\n\t\t\t\t\tnode = child;\n\t\t\t\t\tif (nodesp != NULL) {\n\t\t\t\t\t\tnodesp[i] =\n\t\t\t\t\t\t    (const ctl_node_t *)node;\n\t\t\t\t\t}\n\t\t\t\t\tmibp[i] = j;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (node == pnode) {\n\t\t\t\tret = ENOENT;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\t\t} else {\n\t\t\tuintmax_t index;\n\t\t\tconst ctl_indexed_node_t *inode;\n\n\t\t\t/* Children are indexed. */\n\t\t\tindex = malloc_strtoumax(elm, NULL, 10);\n\t\t\tif (index == UINTMAX_MAX || index > SIZE_T_MAX) {\n\t\t\t\tret = ENOENT;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\n\t\t\tinode = ctl_indexed_node(node->children);\n\t\t\tnode = inode->index(tsdn, mibp, *depthp, (size_t)index);\n\t\t\tif (node == NULL) {\n\t\t\t\tret = ENOENT;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\n\t\t\tif (nodesp != NULL) {\n\t\t\t\tnodesp[i] = (const ctl_node_t *)node;\n\t\t\t}\n\t\t\tmibp[i] = (size_t)index;\n\t\t}\n\n\t\tif (node->ctl != NULL) {\n\t\t\t/* Terminal node. */\n\t\t\tif (*dot != '\\0') {\n\t\t\t\t/*\n\t\t\t\t * The name contains more elements than are\n\t\t\t\t * in this path through the tree.\n\t\t\t\t */\n\t\t\t\tret = ENOENT;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\t\t\t/* Complete lookup successful. */\n\t\t\t*depthp = i + 1;\n\t\t\tbreak;\n\t\t}\n\n\t\t/* Update elm. */\n\t\tif (*dot == '\\0') {\n\t\t\t/* No more elements. */\n\t\t\tret = ENOENT;\n\t\t\tgoto label_return;\n\t\t}\n\t\telm = &dot[1];\n\t\tdot = ((tdot = strchr(elm, '.')) != NULL) ? tdot :\n\t\t    strchr(elm, '\\0');\n\t\telen = (size_t)((uintptr_t)dot - (uintptr_t)elm);\n\t}\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nint\nctl_byname(tsd_t *tsd, const char *name, void *oldp, size_t *oldlenp,\n    void *newp, size_t newlen) {\n\tint ret;\n\tsize_t depth;\n\tctl_node_t const *nodes[CTL_MAX_DEPTH];\n\tsize_t mib[CTL_MAX_DEPTH];\n\tconst ctl_named_node_t *node;\n\n\tif (!ctl_initialized && ctl_init(tsd_tsdn(tsd))) {\n\t\tret = EAGAIN;\n\t\tgoto label_return;\n\t}\n\n\tdepth = CTL_MAX_DEPTH;\n\tret = ctl_lookup(tsd_tsdn(tsd), name, nodes, mib, &depth);\n\tif (ret != 0) {\n\t\tgoto label_return;\n\t}\n\n\tnode = ctl_named_node(nodes[depth-1]);\n\tif (node != NULL && node->ctl) {\n\t\tret = node->ctl(tsd, mib, depth, oldp, oldlenp, newp, newlen);\n\t} else {\n\t\t/* The name refers to a partial path through the ctl tree. */\n\t\tret = ENOENT;\n\t}\n\nlabel_return:\n\treturn(ret);\n}\n\nint\nctl_nametomib(tsdn_t *tsdn, const char *name, size_t *mibp, size_t *miblenp) {\n\tint ret;\n\n\tif (!ctl_initialized && ctl_init(tsdn)) {\n\t\tret = EAGAIN;\n\t\tgoto label_return;\n\t}\n\n\tret = ctl_lookup(tsdn, name, NULL, mibp, miblenp);\nlabel_return:\n\treturn(ret);\n}\n\nint\nctl_bymib(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tconst ctl_named_node_t *node;\n\tsize_t i;\n\n\tif (!ctl_initialized && ctl_init(tsd_tsdn(tsd))) {\n\t\tret = EAGAIN;\n\t\tgoto label_return;\n\t}\n\n\t/* Iterate down the tree. */\n\tnode = super_root_node;\n\tfor (i = 0; i < miblen; i++) {\n\t\tassert(node);\n\t\tassert(node->nchildren > 0);\n\t\tif (ctl_named_node(node->children) != NULL) {\n\t\t\t/* Children are named. */\n\t\t\tif (node->nchildren <= mib[i]) {\n\t\t\t\tret = ENOENT;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\t\t\tnode = ctl_named_children(node, mib[i]);\n\t\t} else {\n\t\t\tconst ctl_indexed_node_t *inode;\n\n\t\t\t/* Indexed element. */\n\t\t\tinode = ctl_indexed_node(node->children);\n\t\t\tnode = inode->index(tsd_tsdn(tsd), mib, miblen, mib[i]);\n\t\t\tif (node == NULL) {\n\t\t\t\tret = ENOENT;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\t\t}\n\t}\n\n\t/* Call the ctl function. */\n\tif (node && node->ctl) {\n\t\tret = node->ctl(tsd, mib, miblen, oldp, oldlenp, newp, newlen);\n\t} else {\n\t\t/* Partial MIB. */\n\t\tret = ENOENT;\n\t}\n\nlabel_return:\n\treturn(ret);\n}\n\nbool\nctl_boot(void) {\n\tif (malloc_mutex_init(&ctl_mtx, \"ctl\", WITNESS_RANK_CTL,\n\t    malloc_mutex_rank_exclusive)) {\n\t\treturn true;\n\t}\n\n\tctl_initialized = false;\n\n\treturn false;\n}\n\nvoid\nctl_prefork(tsdn_t *tsdn) {\n\tmalloc_mutex_prefork(tsdn, &ctl_mtx);\n}\n\nvoid\nctl_postfork_parent(tsdn_t *tsdn) {\n\tmalloc_mutex_postfork_parent(tsdn, &ctl_mtx);\n}\n\nvoid\nctl_postfork_child(tsdn_t *tsdn) {\n\tmalloc_mutex_postfork_child(tsdn, &ctl_mtx);\n}\n\n/******************************************************************************/\n/* *_ctl() functions. */\n\n#define READONLY()\tdo {\t\t\t\t\t\t\\\n\tif (newp != NULL || newlen != 0) {\t\t\t\t\\\n\t\tret = EPERM;\t\t\t\t\t\t\\\n\t\tgoto label_return;\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define WRITEONLY()\tdo {\t\t\t\t\t\t\\\n\tif (oldp != NULL || oldlenp != NULL) {\t\t\t\t\\\n\t\tret = EPERM;\t\t\t\t\t\t\\\n\t\tgoto label_return;\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define READ_XOR_WRITE()\tdo {\t\t\t\t\t\\\n\tif ((oldp != NULL && oldlenp != NULL) && (newp != NULL ||\t\\\n\t    newlen != 0)) {\t\t\t\t\t\t\\\n\t\tret = EPERM;\t\t\t\t\t\t\\\n\t\tgoto label_return;\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define READ(v, t)\tdo {\t\t\t\t\t\t\\\n\tif (oldp != NULL && oldlenp != NULL) {\t\t\t\t\\\n\t\tif (*oldlenp != sizeof(t)) {\t\t\t\t\\\n\t\t\tsize_t\tcopylen = (sizeof(t) <= *oldlenp)\t\\\n\t\t\t    ? sizeof(t) : *oldlenp;\t\t\t\\\n\t\t\tmemcpy(oldp, (void *)&(v), copylen);\t\t\\\n\t\t\tret = EINVAL;\t\t\t\t\t\\\n\t\t\tgoto label_return;\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\t*(t *)oldp = (v);\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define WRITE(v, t)\tdo {\t\t\t\t\t\t\\\n\tif (newp != NULL) {\t\t\t\t\t\t\\\n\t\tif (newlen != sizeof(t)) {\t\t\t\t\\\n\t\t\tret = EINVAL;\t\t\t\t\t\\\n\t\t\tgoto label_return;\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\t(v) = *(t *)newp;\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define MIB_UNSIGNED(v, i) do {\t\t\t\t\t\t\\\n\tif (mib[i] > UINT_MAX) {\t\t\t\t\t\\\n\t\tret = EFAULT;\t\t\t\t\t\t\\\n\t\tgoto label_return;\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tv = (unsigned)mib[i];\t\t\t\t\t\t\\\n} while (0)\n\n/*\n * There's a lot of code duplication in the following macros due to limitations\n * in how nested cpp macros are expanded.\n */\n#define CTL_RO_CLGEN(c, l, n, v, t)\t\t\t\t\t\\\nstatic int\t\t\t\t\t\t\t\t\\\nn##_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\t\\\n    size_t *oldlenp, void *newp, size_t newlen) {\t\t\t\\\n\tint ret;\t\t\t\t\t\t\t\\\n\tt oldval;\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tif (!(c)) {\t\t\t\t\t\t\t\\\n\t\treturn ENOENT;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tif (l) {\t\t\t\t\t\t\t\\\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &ctl_mtx);\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tREADONLY();\t\t\t\t\t\t\t\\\n\toldval = (v);\t\t\t\t\t\t\t\\\n\tREAD(oldval, t);\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tret = 0;\t\t\t\t\t\t\t\\\nlabel_return:\t\t\t\t\t\t\t\t\\\n\tif (l) {\t\t\t\t\t\t\t\\\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &ctl_mtx);\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\treturn ret;\t\t\t\t\t\t\t\\\n}\n\n#define CTL_RO_CGEN(c, n, v, t)\t\t\t\t\t\t\\\nstatic int\t\t\t\t\t\t\t\t\\\nn##_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\t\\\n    size_t *oldlenp, void *newp, size_t newlen) {\t\t\t\\\n\tint ret;\t\t\t\t\t\t\t\\\n\tt oldval;\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tif (!(c)) {\t\t\t\t\t\t\t\\\n\t\treturn ENOENT;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &ctl_mtx);\t\t\t\\\n\tREADONLY();\t\t\t\t\t\t\t\\\n\toldval = (v);\t\t\t\t\t\t\t\\\n\tREAD(oldval, t);\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tret = 0;\t\t\t\t\t\t\t\\\nlabel_return:\t\t\t\t\t\t\t\t\\\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &ctl_mtx);\t\t\t\\\n\treturn ret;\t\t\t\t\t\t\t\\\n}\n\n#define CTL_RO_GEN(n, v, t)\t\t\t\t\t\t\\\nstatic int\t\t\t\t\t\t\t\t\\\nn##_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\t\\\n    size_t *oldlenp, void *newp, size_t newlen) {\t\t\t\\\n\tint ret;\t\t\t\t\t\t\t\\\n\tt oldval;\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &ctl_mtx);\t\t\t\\\n\tREADONLY();\t\t\t\t\t\t\t\\\n\toldval = (v);\t\t\t\t\t\t\t\\\n\tREAD(oldval, t);\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tret = 0;\t\t\t\t\t\t\t\\\nlabel_return:\t\t\t\t\t\t\t\t\\\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &ctl_mtx);\t\t\t\\\n\treturn ret;\t\t\t\t\t\t\t\\\n}\n\n/*\n * ctl_mtx is not acquired, under the assumption that no pertinent data will\n * mutate during the call.\n */\n#define CTL_RO_NL_CGEN(c, n, v, t)\t\t\t\t\t\\\nstatic int\t\t\t\t\t\t\t\t\\\nn##_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\t\\\n    size_t *oldlenp, void *newp, size_t newlen) {\t\t\t\\\n\tint ret;\t\t\t\t\t\t\t\\\n\tt oldval;\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tif (!(c)) {\t\t\t\t\t\t\t\\\n\t\treturn ENOENT;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tREADONLY();\t\t\t\t\t\t\t\\\n\toldval = (v);\t\t\t\t\t\t\t\\\n\tREAD(oldval, t);\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tret = 0;\t\t\t\t\t\t\t\\\nlabel_return:\t\t\t\t\t\t\t\t\\\n\treturn ret;\t\t\t\t\t\t\t\\\n}\n\n#define CTL_RO_NL_GEN(n, v, t)\t\t\t\t\t\t\\\nstatic int\t\t\t\t\t\t\t\t\\\nn##_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\t\\\n    size_t *oldlenp, void *newp, size_t newlen) {\t\t\t\\\n\tint ret;\t\t\t\t\t\t\t\\\n\tt oldval;\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tREADONLY();\t\t\t\t\t\t\t\\\n\toldval = (v);\t\t\t\t\t\t\t\\\n\tREAD(oldval, t);\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tret = 0;\t\t\t\t\t\t\t\\\nlabel_return:\t\t\t\t\t\t\t\t\\\n\treturn ret;\t\t\t\t\t\t\t\\\n}\n\n#define CTL_TSD_RO_NL_CGEN(c, n, m, t)\t\t\t\t\t\\\nstatic int\t\t\t\t\t\t\t\t\\\nn##_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\t\\\n    size_t *oldlenp, void *newp, size_t newlen) {\t\t\t\\\n\tint ret;\t\t\t\t\t\t\t\\\n\tt oldval;\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tif (!(c)) {\t\t\t\t\t\t\t\\\n\t\treturn ENOENT;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tREADONLY();\t\t\t\t\t\t\t\\\n\toldval = (m(tsd));\t\t\t\t\t\t\\\n\tREAD(oldval, t);\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tret = 0;\t\t\t\t\t\t\t\\\nlabel_return:\t\t\t\t\t\t\t\t\\\n\treturn ret;\t\t\t\t\t\t\t\\\n}\n\n#define CTL_RO_CONFIG_GEN(n, t)\t\t\t\t\t\t\\\nstatic int\t\t\t\t\t\t\t\t\\\nn##_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\t\\\n    size_t *oldlenp, void *newp, size_t newlen) {\t\t\t\\\n\tint ret;\t\t\t\t\t\t\t\\\n\tt oldval;\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tREADONLY();\t\t\t\t\t\t\t\\\n\toldval = n;\t\t\t\t\t\t\t\\\n\tREAD(oldval, t);\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tret = 0;\t\t\t\t\t\t\t\\\nlabel_return:\t\t\t\t\t\t\t\t\\\n\treturn ret;\t\t\t\t\t\t\t\\\n}\n\n/******************************************************************************/\n\nCTL_RO_NL_GEN(version, JEMALLOC_VERSION, const char *)\n\nstatic int\nepoch_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tUNUSED uint64_t newval;\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &ctl_mtx);\n\tWRITE(newval, uint64_t);\n\tif (newp != NULL) {\n\t\tctl_refresh(tsd_tsdn(tsd));\n\t}\n\tREAD(ctl_arenas->epoch, uint64_t);\n\n\tret = 0;\nlabel_return:\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &ctl_mtx);\n\treturn ret;\n}\n\nstatic int\nbackground_thread_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tbool oldval;\n\n\tif (!have_background_thread) {\n\t\treturn ENOENT;\n\t}\n\tbackground_thread_ctl_init(tsd_tsdn(tsd));\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &ctl_mtx);\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &background_thread_lock);\n\tif (newp == NULL) {\n\t\toldval = background_thread_enabled();\n\t\tREAD(oldval, bool);\n\t} else {\n\t\tif (newlen != sizeof(bool)) {\n\t\t\tret = EINVAL;\n\t\t\tgoto label_return;\n\t\t}\n\t\toldval = background_thread_enabled();\n\t\tREAD(oldval, bool);\n\n\t\tbool newval = *(bool *)newp;\n\t\tif (newval == oldval) {\n\t\t\tret = 0;\n\t\t\tgoto label_return;\n\t\t}\n\n\t\tbackground_thread_enabled_set(tsd_tsdn(tsd), newval);\n\t\tif (newval) {\n\t\t\tif (!can_enable_background_thread) {\n\t\t\t\tmalloc_printf(\"<jemalloc>: Error in dlsym(\"\n\t\t\t            \"RTLD_NEXT, \\\"pthread_create\\\"). Cannot \"\n\t\t\t\t    \"enable background_thread\\n\");\n\t\t\t\tret = EFAULT;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\t\t\tif (background_threads_enable(tsd)) {\n\t\t\t\tret = EFAULT;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\t\t} else {\n\t\t\tif (background_threads_disable(tsd)) {\n\t\t\t\tret = EFAULT;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\t\t}\n\t}\n\tret = 0;\nlabel_return:\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &background_thread_lock);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &ctl_mtx);\n\n\treturn ret;\n}\n\n/******************************************************************************/\n\nCTL_RO_CONFIG_GEN(config_cache_oblivious, bool)\nCTL_RO_CONFIG_GEN(config_debug, bool)\nCTL_RO_CONFIG_GEN(config_fill, bool)\nCTL_RO_CONFIG_GEN(config_lazy_lock, bool)\nCTL_RO_CONFIG_GEN(config_malloc_conf, const char *)\nCTL_RO_CONFIG_GEN(config_prof, bool)\nCTL_RO_CONFIG_GEN(config_prof_libgcc, bool)\nCTL_RO_CONFIG_GEN(config_prof_libunwind, bool)\nCTL_RO_CONFIG_GEN(config_stats, bool)\nCTL_RO_CONFIG_GEN(config_thp, bool)\nCTL_RO_CONFIG_GEN(config_utrace, bool)\nCTL_RO_CONFIG_GEN(config_xmalloc, bool)\n\n/******************************************************************************/\n\nCTL_RO_NL_GEN(opt_abort, opt_abort, bool)\nCTL_RO_NL_GEN(opt_abort_conf, opt_abort_conf, bool)\nCTL_RO_NL_GEN(opt_retain, opt_retain, bool)\nCTL_RO_NL_GEN(opt_dss, opt_dss, const char *)\nCTL_RO_NL_GEN(opt_narenas, opt_narenas, unsigned)\nCTL_RO_NL_GEN(opt_percpu_arena, percpu_arena_mode_names[opt_percpu_arena],\n    const char *)\nCTL_RO_NL_GEN(opt_background_thread, opt_background_thread, bool)\nCTL_RO_NL_GEN(opt_dirty_decay_ms, opt_dirty_decay_ms, ssize_t)\nCTL_RO_NL_GEN(opt_muzzy_decay_ms, opt_muzzy_decay_ms, ssize_t)\nCTL_RO_NL_GEN(opt_stats_print, opt_stats_print, bool)\nCTL_RO_NL_GEN(opt_stats_print_opts, opt_stats_print_opts, const char *)\nCTL_RO_NL_CGEN(config_fill, opt_junk, opt_junk, const char *)\nCTL_RO_NL_CGEN(config_fill, opt_zero, opt_zero, bool)\nCTL_RO_NL_CGEN(config_utrace, opt_utrace, opt_utrace, bool)\nCTL_RO_NL_CGEN(config_xmalloc, opt_xmalloc, opt_xmalloc, bool)\nCTL_RO_NL_GEN(opt_tcache, opt_tcache, bool)\nCTL_RO_NL_GEN(opt_lg_tcache_max, opt_lg_tcache_max, ssize_t)\nCTL_RO_NL_CGEN(config_prof, opt_prof, opt_prof, bool)\nCTL_RO_NL_CGEN(config_prof, opt_prof_prefix, opt_prof_prefix, const char *)\nCTL_RO_NL_CGEN(config_prof, opt_prof_active, opt_prof_active, bool)\nCTL_RO_NL_CGEN(config_prof, opt_prof_thread_active_init,\n    opt_prof_thread_active_init, bool)\nCTL_RO_NL_CGEN(config_prof, opt_lg_prof_sample, opt_lg_prof_sample, size_t)\nCTL_RO_NL_CGEN(config_prof, opt_prof_accum, opt_prof_accum, bool)\nCTL_RO_NL_CGEN(config_prof, opt_lg_prof_interval, opt_lg_prof_interval, ssize_t)\nCTL_RO_NL_CGEN(config_prof, opt_prof_gdump, opt_prof_gdump, bool)\nCTL_RO_NL_CGEN(config_prof, opt_prof_final, opt_prof_final, bool)\nCTL_RO_NL_CGEN(config_prof, opt_prof_leak, opt_prof_leak, bool)\n\n/******************************************************************************/\n\nstatic int\nthread_arena_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tarena_t *oldarena;\n\tunsigned newind, oldind;\n\n\toldarena = arena_choose(tsd, NULL);\n\tif (oldarena == NULL) {\n\t\treturn EAGAIN;\n\t}\n\tnewind = oldind = arena_ind_get(oldarena);\n\tWRITE(newind, unsigned);\n\tREAD(oldind, unsigned);\n\n\tif (newind != oldind) {\n\t\tarena_t *newarena;\n\n\t\tif (newind >= narenas_total_get()) {\n\t\t\t/* New arena index is out of range. */\n\t\t\tret = EFAULT;\n\t\t\tgoto label_return;\n\t\t}\n\n\t\tif (have_percpu_arena &&\n\t\t    PERCPU_ARENA_ENABLED(opt_percpu_arena)) {\n\t\t\tif (newind < percpu_arena_ind_limit(opt_percpu_arena)) {\n\t\t\t\t/*\n\t\t\t\t * If perCPU arena is enabled, thread_arena\n\t\t\t\t * control is not allowed for the auto arena\n\t\t\t\t * range.\n\t\t\t\t */\n\t\t\t\tret = EPERM;\n\t\t\t\tgoto label_return;\n\t\t\t}\n\t\t}\n\n\t\t/* Initialize arena if necessary. */\n\t\tnewarena = arena_get(tsd_tsdn(tsd), newind, true);\n\t\tif (newarena == NULL) {\n\t\t\tret = EAGAIN;\n\t\t\tgoto label_return;\n\t\t}\n\t\t/* Set new arena/tcache associations. */\n\t\tarena_migrate(tsd, oldind, newind);\n\t\tif (tcache_available(tsd)) {\n\t\t\ttcache_arena_reassociate(tsd_tsdn(tsd),\n\t\t\t    tsd_tcachep_get(tsd), newarena);\n\t\t}\n\t}\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nCTL_TSD_RO_NL_CGEN(config_stats, thread_allocated, tsd_thread_allocated_get,\n    uint64_t)\nCTL_TSD_RO_NL_CGEN(config_stats, thread_allocatedp, tsd_thread_allocatedp_get,\n    uint64_t *)\nCTL_TSD_RO_NL_CGEN(config_stats, thread_deallocated, tsd_thread_deallocated_get,\n    uint64_t)\nCTL_TSD_RO_NL_CGEN(config_stats, thread_deallocatedp,\n    tsd_thread_deallocatedp_get, uint64_t *)\n\nstatic int\nthread_tcache_enabled_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tbool oldval;\n\n\toldval = tcache_enabled_get(tsd);\n\tif (newp != NULL) {\n\t\tif (newlen != sizeof(bool)) {\n\t\t\tret = EINVAL;\n\t\t\tgoto label_return;\n\t\t}\n\t\ttcache_enabled_set(tsd, *(bool *)newp);\n\t}\n\tREAD(oldval, bool);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\nthread_tcache_flush_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\n\tif (!tcache_available(tsd)) {\n\t\tret = EFAULT;\n\t\tgoto label_return;\n\t}\n\n\tREADONLY();\n\tWRITEONLY();\n\n\ttcache_flush(tsd);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\nthread_prof_name_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\n\tif (!config_prof) {\n\t\treturn ENOENT;\n\t}\n\n\tREAD_XOR_WRITE();\n\n\tif (newp != NULL) {\n\t\tif (newlen != sizeof(const char *)) {\n\t\t\tret = EINVAL;\n\t\t\tgoto label_return;\n\t\t}\n\n\t\tif ((ret = prof_thread_name_set(tsd, *(const char **)newp)) !=\n\t\t    0) {\n\t\t\tgoto label_return;\n\t\t}\n\t} else {\n\t\tconst char *oldname = prof_thread_name_get(tsd);\n\t\tREAD(oldname, const char *);\n\t}\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\nthread_prof_active_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tbool oldval;\n\n\tif (!config_prof) {\n\t\treturn ENOENT;\n\t}\n\n\toldval = prof_thread_active_get(tsd);\n\tif (newp != NULL) {\n\t\tif (newlen != sizeof(bool)) {\n\t\t\tret = EINVAL;\n\t\t\tgoto label_return;\n\t\t}\n\t\tif (prof_thread_active_set(tsd, *(bool *)newp)) {\n\t\t\tret = EAGAIN;\n\t\t\tgoto label_return;\n\t\t}\n\t}\n\tREAD(oldval, bool);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\n/******************************************************************************/\n\nstatic int\ntcache_create_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tunsigned tcache_ind;\n\n\tREADONLY();\n\tif (tcaches_create(tsd, &tcache_ind)) {\n\t\tret = EFAULT;\n\t\tgoto label_return;\n\t}\n\tREAD(tcache_ind, unsigned);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\ntcache_flush_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tunsigned tcache_ind;\n\n\tWRITEONLY();\n\ttcache_ind = UINT_MAX;\n\tWRITE(tcache_ind, unsigned);\n\tif (tcache_ind == UINT_MAX) {\n\t\tret = EFAULT;\n\t\tgoto label_return;\n\t}\n\ttcaches_flush(tsd, tcache_ind);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\ntcache_destroy_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tunsigned tcache_ind;\n\n\tWRITEONLY();\n\ttcache_ind = UINT_MAX;\n\tWRITE(tcache_ind, unsigned);\n\tif (tcache_ind == UINT_MAX) {\n\t\tret = EFAULT;\n\t\tgoto label_return;\n\t}\n\ttcaches_destroy(tsd, tcache_ind);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\n/******************************************************************************/\n\nstatic int\narena_i_initialized_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\ttsdn_t *tsdn = tsd_tsdn(tsd);\n\tunsigned arena_ind;\n\tbool initialized;\n\n\tREADONLY();\n\tMIB_UNSIGNED(arena_ind, 1);\n\n\tmalloc_mutex_lock(tsdn, &ctl_mtx);\n\tinitialized = arenas_i(arena_ind)->initialized;\n\tmalloc_mutex_unlock(tsdn, &ctl_mtx);\n\n\tREAD(initialized, bool);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic void\narena_i_decay(tsdn_t *tsdn, unsigned arena_ind, bool all) {\n\tmalloc_mutex_lock(tsdn, &ctl_mtx);\n\t{\n\t\tunsigned narenas = ctl_arenas->narenas;\n\n\t\t/*\n\t\t * Access via index narenas is deprecated, and scheduled for\n\t\t * removal in 6.0.0.\n\t\t */\n\t\tif (arena_ind == MALLCTL_ARENAS_ALL || arena_ind == narenas) {\n\t\t\tunsigned i;\n\t\t\tVARIABLE_ARRAY(arena_t *, tarenas, narenas);\n\n\t\t\tfor (i = 0; i < narenas; i++) {\n\t\t\t\ttarenas[i] = arena_get(tsdn, i, false);\n\t\t\t}\n\n\t\t\t/*\n\t\t\t * No further need to hold ctl_mtx, since narenas and\n\t\t\t * tarenas contain everything needed below.\n\t\t\t */\n\t\t\tmalloc_mutex_unlock(tsdn, &ctl_mtx);\n\n\t\t\tfor (i = 0; i < narenas; i++) {\n\t\t\t\tif (tarenas[i] != NULL) {\n\t\t\t\t\tarena_decay(tsdn, tarenas[i], false,\n\t\t\t\t\t    all);\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tarena_t *tarena;\n\n\t\t\tassert(arena_ind < narenas);\n\n\t\t\ttarena = arena_get(tsdn, arena_ind, false);\n\n\t\t\t/* No further need to hold ctl_mtx. */\n\t\t\tmalloc_mutex_unlock(tsdn, &ctl_mtx);\n\n\t\t\tif (tarena != NULL) {\n\t\t\t\tarena_decay(tsdn, tarena, false, all);\n\t\t\t}\n\t\t}\n\t}\n}\n\nstatic int\narena_i_decay_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tunsigned arena_ind;\n\n\tREADONLY();\n\tWRITEONLY();\n\tMIB_UNSIGNED(arena_ind, 1);\n\tarena_i_decay(tsd_tsdn(tsd), arena_ind, false);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\narena_i_purge_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tunsigned arena_ind;\n\n\tREADONLY();\n\tWRITEONLY();\n\tMIB_UNSIGNED(arena_ind, 1);\n\tarena_i_decay(tsd_tsdn(tsd), arena_ind, true);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\narena_i_reset_destroy_helper(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen, unsigned *arena_ind,\n    arena_t **arena) {\n\tint ret;\n\n\tREADONLY();\n\tWRITEONLY();\n\tMIB_UNSIGNED(*arena_ind, 1);\n\n\t*arena = arena_get(tsd_tsdn(tsd), *arena_ind, false);\n\tif (*arena == NULL || arena_is_auto(*arena)) {\n\t\tret = EFAULT;\n\t\tgoto label_return;\n\t}\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic void\narena_reset_prepare_background_thread(tsd_t *tsd, unsigned arena_ind) {\n\t/* Temporarily disable the background thread during arena reset. */\n\tif (have_background_thread) {\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &background_thread_lock);\n\t\tif (background_thread_enabled()) {\n\t\t\tunsigned ind = arena_ind % ncpus;\n\t\t\tbackground_thread_info_t *info =\n\t\t\t    &background_thread_info[ind];\n\t\t\tassert(info->state == background_thread_started);\n\t\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);\n\t\t\tinfo->state = background_thread_paused;\n\t\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);\n\t\t}\n\t}\n}\n\nstatic void\narena_reset_finish_background_thread(tsd_t *tsd, unsigned arena_ind) {\n\tif (have_background_thread) {\n\t\tif (background_thread_enabled()) {\n\t\t\tunsigned ind = arena_ind % ncpus;\n\t\t\tbackground_thread_info_t *info =\n\t\t\t    &background_thread_info[ind];\n\t\t\tassert(info->state = background_thread_paused);\n\t\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);\n\t\t\tinfo->state = background_thread_started;\n\t\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);\n\t\t}\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &background_thread_lock);\n\t}\n}\n\nstatic int\narena_i_reset_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tunsigned arena_ind;\n\tarena_t *arena;\n\n\tret = arena_i_reset_destroy_helper(tsd, mib, miblen, oldp, oldlenp,\n\t    newp, newlen, &arena_ind, &arena);\n\tif (ret != 0) {\n\t\treturn ret;\n\t}\n\n\tarena_reset_prepare_background_thread(tsd, arena_ind);\n\tarena_reset(tsd, arena);\n\tarena_reset_finish_background_thread(tsd, arena_ind);\n\n\treturn ret;\n}\n\nstatic int\narena_i_destroy_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tunsigned arena_ind;\n\tarena_t *arena;\n\tctl_arena_t *ctl_darena, *ctl_arena;\n\n\tret = arena_i_reset_destroy_helper(tsd, mib, miblen, oldp, oldlenp,\n\t    newp, newlen, &arena_ind, &arena);\n\tif (ret != 0) {\n\t\tgoto label_return;\n\t}\n\n\tif (arena_nthreads_get(arena, false) != 0 || arena_nthreads_get(arena,\n\t    true) != 0) {\n\t\tret = EFAULT;\n\t\tgoto label_return;\n\t}\n\n\tarena_reset_prepare_background_thread(tsd, arena_ind);\n\t/* Merge stats after resetting and purging arena. */\n\tarena_reset(tsd, arena);\n\tarena_decay(tsd_tsdn(tsd), arena, false, true);\n\tctl_darena = arenas_i(MALLCTL_ARENAS_DESTROYED);\n\tctl_darena->initialized = true;\n\tctl_arena_refresh(tsd_tsdn(tsd), arena, ctl_darena, arena_ind, true);\n\t/* Destroy arena. */\n\tarena_destroy(tsd, arena);\n\tctl_arena = arenas_i(arena_ind);\n\tctl_arena->initialized = false;\n\t/* Record arena index for later recycling via arenas.create. */\n\tql_elm_new(ctl_arena, destroyed_link);\n\tql_tail_insert(&ctl_arenas->destroyed, ctl_arena, destroyed_link);\n\tarena_reset_finish_background_thread(tsd, arena_ind);\n\n\tassert(ret == 0);\nlabel_return:\n\treturn ret;\n}\n\nstatic int\narena_i_dss_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tconst char *dss = NULL;\n\tunsigned arena_ind;\n\tdss_prec_t dss_prec_old = dss_prec_limit;\n\tdss_prec_t dss_prec = dss_prec_limit;\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &ctl_mtx);\n\tWRITE(dss, const char *);\n\tMIB_UNSIGNED(arena_ind, 1);\n\tif (dss != NULL) {\n\t\tint i;\n\t\tbool match = false;\n\n\t\tfor (i = 0; i < dss_prec_limit; i++) {\n\t\t\tif (strcmp(dss_prec_names[i], dss) == 0) {\n\t\t\t\tdss_prec = i;\n\t\t\t\tmatch = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif (!match) {\n\t\t\tret = EINVAL;\n\t\t\tgoto label_return;\n\t\t}\n\t}\n\n\t/*\n\t * Access via index narenas is deprecated, and scheduled for removal in\n\t * 6.0.0.\n\t */\n\tif (arena_ind == MALLCTL_ARENAS_ALL || arena_ind ==\n\t    ctl_arenas->narenas) {\n\t\tif (dss_prec != dss_prec_limit &&\n\t\t    extent_dss_prec_set(dss_prec)) {\n\t\t\tret = EFAULT;\n\t\t\tgoto label_return;\n\t\t}\n\t\tdss_prec_old = extent_dss_prec_get();\n\t} else {\n\t\tarena_t *arena = arena_get(tsd_tsdn(tsd), arena_ind, false);\n\t\tif (arena == NULL || (dss_prec != dss_prec_limit &&\n\t\t    arena_dss_prec_set(arena, dss_prec))) {\n\t\t\tret = EFAULT;\n\t\t\tgoto label_return;\n\t\t}\n\t\tdss_prec_old = arena_dss_prec_get(arena);\n\t}\n\n\tdss = dss_prec_names[dss_prec_old];\n\tREAD(dss, const char *);\n\n\tret = 0;\nlabel_return:\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &ctl_mtx);\n\treturn ret;\n}\n\nstatic int\narena_i_decay_ms_ctl_impl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen, bool dirty) {\n\tint ret;\n\tunsigned arena_ind;\n\tarena_t *arena;\n\n\tMIB_UNSIGNED(arena_ind, 1);\n\tarena = arena_get(tsd_tsdn(tsd), arena_ind, false);\n\tif (arena == NULL) {\n\t\tret = EFAULT;\n\t\tgoto label_return;\n\t}\n\n\tif (oldp != NULL && oldlenp != NULL) {\n\t\tsize_t oldval = dirty ? arena_dirty_decay_ms_get(arena) :\n\t\t    arena_muzzy_decay_ms_get(arena);\n\t\tREAD(oldval, ssize_t);\n\t}\n\tif (newp != NULL) {\n\t\tif (newlen != sizeof(ssize_t)) {\n\t\t\tret = EINVAL;\n\t\t\tgoto label_return;\n\t\t}\n\t\tif (dirty ? arena_dirty_decay_ms_set(tsd_tsdn(tsd), arena,\n\t\t    *(ssize_t *)newp) : arena_muzzy_decay_ms_set(tsd_tsdn(tsd),\n\t\t    arena, *(ssize_t *)newp)) {\n\t\t\tret = EFAULT;\n\t\t\tgoto label_return;\n\t\t}\n\t}\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\narena_i_dirty_decay_ms_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\treturn arena_i_decay_ms_ctl_impl(tsd, mib, miblen, oldp, oldlenp, newp,\n\t    newlen, true);\n}\n\nstatic int\narena_i_muzzy_decay_ms_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\treturn arena_i_decay_ms_ctl_impl(tsd, mib, miblen, oldp, oldlenp, newp,\n\t    newlen, false);\n}\n\nstatic int\narena_i_extent_hooks_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tunsigned arena_ind;\n\tarena_t *arena;\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &ctl_mtx);\n\tMIB_UNSIGNED(arena_ind, 1);\n\tif (arena_ind < narenas_total_get() && (arena =\n\t    arena_get(tsd_tsdn(tsd), arena_ind, false)) != NULL) {\n\t\tif (newp != NULL) {\n\t\t\textent_hooks_t *old_extent_hooks;\n\t\t\textent_hooks_t *new_extent_hooks\n\t\t\t    JEMALLOC_CC_SILENCE_INIT(NULL);\n\t\t\tWRITE(new_extent_hooks, extent_hooks_t *);\n\t\t\told_extent_hooks = extent_hooks_set(tsd, arena,\n\t\t\t    new_extent_hooks);\n\t\t\tREAD(old_extent_hooks, extent_hooks_t *);\n\t\t} else {\n\t\t\textent_hooks_t *old_extent_hooks =\n\t\t\t    extent_hooks_get(arena);\n\t\t\tREAD(old_extent_hooks, extent_hooks_t *);\n\t\t}\n\t} else {\n\t\tret = EFAULT;\n\t\tgoto label_return;\n\t}\n\tret = 0;\nlabel_return:\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &ctl_mtx);\n\treturn ret;\n}\n\nstatic const ctl_named_node_t *\narena_i_index(tsdn_t *tsdn, const size_t *mib, size_t miblen, size_t i) {\n\tconst ctl_named_node_t *ret;\n\n\tmalloc_mutex_lock(tsdn, &ctl_mtx);\n\tswitch (i) {\n\tcase MALLCTL_ARENAS_ALL:\n\tcase MALLCTL_ARENAS_DESTROYED:\n\t\tbreak;\n\tdefault:\n\t\tif (i > ctl_arenas->narenas) {\n\t\t\tret = NULL;\n\t\t\tgoto label_return;\n\t\t}\n\t\tbreak;\n\t}\n\n\tret = super_arena_i_node;\nlabel_return:\n\tmalloc_mutex_unlock(tsdn, &ctl_mtx);\n\treturn ret;\n}\n\n/******************************************************************************/\n\nstatic int\narenas_narenas_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tunsigned narenas;\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &ctl_mtx);\n\tREADONLY();\n\tif (*oldlenp != sizeof(unsigned)) {\n\t\tret = EINVAL;\n\t\tgoto label_return;\n\t}\n\tnarenas = ctl_arenas->narenas;\n\tREAD(narenas, unsigned);\n\n\tret = 0;\nlabel_return:\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &ctl_mtx);\n\treturn ret;\n}\n\nstatic int\narenas_decay_ms_ctl_impl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen, bool dirty) {\n\tint ret;\n\n\tif (oldp != NULL && oldlenp != NULL) {\n\t\tsize_t oldval = (dirty ? arena_dirty_decay_ms_default_get() :\n\t\t    arena_muzzy_decay_ms_default_get());\n\t\tREAD(oldval, ssize_t);\n\t}\n\tif (newp != NULL) {\n\t\tif (newlen != sizeof(ssize_t)) {\n\t\t\tret = EINVAL;\n\t\t\tgoto label_return;\n\t\t}\n\t\tif (dirty ?  arena_dirty_decay_ms_default_set(*(ssize_t *)newp)\n\t\t    : arena_muzzy_decay_ms_default_set(*(ssize_t *)newp)) {\n\t\t\tret = EFAULT;\n\t\t\tgoto label_return;\n\t\t}\n\t}\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\narenas_dirty_decay_ms_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\treturn arenas_decay_ms_ctl_impl(tsd, mib, miblen, oldp, oldlenp, newp,\n\t    newlen, true);\n}\n\nstatic int\narenas_muzzy_decay_ms_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\treturn arenas_decay_ms_ctl_impl(tsd, mib, miblen, oldp, oldlenp, newp,\n\t    newlen, false);\n}\n\nCTL_RO_NL_GEN(arenas_quantum, QUANTUM, size_t)\nCTL_RO_NL_GEN(arenas_page, PAGE, size_t)\nCTL_RO_NL_GEN(arenas_tcache_max, tcache_maxclass, size_t)\nCTL_RO_NL_GEN(arenas_nbins, NBINS, unsigned)\nCTL_RO_NL_GEN(arenas_nhbins, nhbins, unsigned)\nCTL_RO_NL_GEN(arenas_bin_i_size, arena_bin_info[mib[2]].reg_size, size_t)\nCTL_RO_NL_GEN(arenas_bin_i_nregs, arena_bin_info[mib[2]].nregs, uint32_t)\nCTL_RO_NL_GEN(arenas_bin_i_slab_size, arena_bin_info[mib[2]].slab_size, size_t)\nstatic const ctl_named_node_t *\narenas_bin_i_index(tsdn_t *tsdn, const size_t *mib, size_t miblen, size_t i) {\n\tif (i > NBINS) {\n\t\treturn NULL;\n\t}\n\treturn super_arenas_bin_i_node;\n}\n\nCTL_RO_NL_GEN(arenas_nlextents, NSIZES - NBINS, unsigned)\nCTL_RO_NL_GEN(arenas_lextent_i_size, sz_index2size(NBINS+(szind_t)mib[2]),\n    size_t)\nstatic const ctl_named_node_t *\narenas_lextent_i_index(tsdn_t *tsdn, const size_t *mib, size_t miblen,\n    size_t i) {\n\tif (i > NSIZES - NBINS) {\n\t\treturn NULL;\n\t}\n\treturn super_arenas_lextent_i_node;\n}\n\nstatic int\narenas_create_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\textent_hooks_t *extent_hooks;\n\tunsigned arena_ind;\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &ctl_mtx);\n\n\textent_hooks = (extent_hooks_t *)&extent_hooks_default;\n\tWRITE(extent_hooks, extent_hooks_t *);\n\tif ((arena_ind = ctl_arena_init(tsd_tsdn(tsd), extent_hooks)) ==\n\t    UINT_MAX) {\n\t\tret = EAGAIN;\n\t\tgoto label_return;\n\t}\n\tREAD(arena_ind, unsigned);\n\n\tret = 0;\nlabel_return:\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &ctl_mtx);\n\treturn ret;\n}\n\n\nstatic int\narenas_lookup_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tunsigned arena_ind;\n\tvoid *ptr;\n\textent_t *extent;\n\tarena_t *arena;\n\tptr = NULL;\n\tret = EINVAL;\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &ctl_mtx);\n\tWRITE(ptr, void *);\n\textent = iealloc(tsd_tsdn(tsd), ptr);\n\tif (extent == NULL)\n\t\tgoto label_return;\n\tarena = extent_arena_get(extent);\n\tif (arena == NULL)\n\t\tgoto label_return;\n\tarena_ind = arena_ind_get(arena);\n\tREAD(arena_ind, unsigned);\n\tret = 0;\nlabel_return:\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &ctl_mtx);\n\treturn ret;\n}\n\n/******************************************************************************/\n\nstatic int\nprof_thread_active_init_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tbool oldval;\n\n\tif (!config_prof) {\n\t\treturn ENOENT;\n\t}\n\n\tif (newp != NULL) {\n\t\tif (newlen != sizeof(bool)) {\n\t\t\tret = EINVAL;\n\t\t\tgoto label_return;\n\t\t}\n\t\toldval = prof_thread_active_init_set(tsd_tsdn(tsd),\n\t\t    *(bool *)newp);\n\t} else {\n\t\toldval = prof_thread_active_init_get(tsd_tsdn(tsd));\n\t}\n\tREAD(oldval, bool);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\nprof_active_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tbool oldval;\n\n\tif (!config_prof) {\n\t\treturn ENOENT;\n\t}\n\n\tif (newp != NULL) {\n\t\tif (newlen != sizeof(bool)) {\n\t\t\tret = EINVAL;\n\t\t\tgoto label_return;\n\t\t}\n\t\toldval = prof_active_set(tsd_tsdn(tsd), *(bool *)newp);\n\t} else {\n\t\toldval = prof_active_get(tsd_tsdn(tsd));\n\t}\n\tREAD(oldval, bool);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\nprof_dump_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tconst char *filename = NULL;\n\n\tif (!config_prof) {\n\t\treturn ENOENT;\n\t}\n\n\tWRITEONLY();\n\tWRITE(filename, const char *);\n\n\tif (prof_mdump(tsd, filename)) {\n\t\tret = EFAULT;\n\t\tgoto label_return;\n\t}\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\nprof_gdump_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tbool oldval;\n\n\tif (!config_prof) {\n\t\treturn ENOENT;\n\t}\n\n\tif (newp != NULL) {\n\t\tif (newlen != sizeof(bool)) {\n\t\t\tret = EINVAL;\n\t\t\tgoto label_return;\n\t\t}\n\t\toldval = prof_gdump_set(tsd_tsdn(tsd), *(bool *)newp);\n\t} else {\n\t\toldval = prof_gdump_get(tsd_tsdn(tsd));\n\t}\n\tREAD(oldval, bool);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nstatic int\nprof_reset_ctl(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,\n    size_t *oldlenp, void *newp, size_t newlen) {\n\tint ret;\n\tsize_t lg_sample = lg_prof_sample;\n\n\tif (!config_prof) {\n\t\treturn ENOENT;\n\t}\n\n\tWRITEONLY();\n\tWRITE(lg_sample, size_t);\n\tif (lg_sample >= (sizeof(uint64_t) << 3)) {\n\t\tlg_sample = (sizeof(uint64_t) << 3) - 1;\n\t}\n\n\tprof_reset(tsd, lg_sample);\n\n\tret = 0;\nlabel_return:\n\treturn ret;\n}\n\nCTL_RO_NL_CGEN(config_prof, prof_interval, prof_interval, uint64_t)\nCTL_RO_NL_CGEN(config_prof, lg_prof_sample, lg_prof_sample, size_t)\n\n/******************************************************************************/\n\nCTL_RO_CGEN(config_stats, stats_allocated, ctl_stats->allocated, size_t)\nCTL_RO_CGEN(config_stats, stats_active, ctl_stats->active, size_t)\nCTL_RO_CGEN(config_stats, stats_metadata, ctl_stats->metadata, size_t)\nCTL_RO_CGEN(config_stats, stats_resident, ctl_stats->resident, size_t)\nCTL_RO_CGEN(config_stats, stats_mapped, ctl_stats->mapped, size_t)\nCTL_RO_CGEN(config_stats, stats_retained, ctl_stats->retained, size_t)\n\nCTL_RO_CGEN(config_stats, stats_background_thread_num_threads,\n    ctl_stats->background_thread.num_threads, size_t)\nCTL_RO_CGEN(config_stats, stats_background_thread_num_runs,\n    ctl_stats->background_thread.num_runs, uint64_t)\nCTL_RO_CGEN(config_stats, stats_background_thread_run_interval,\n    nstime_ns(&ctl_stats->background_thread.run_interval), uint64_t)\n\nCTL_RO_GEN(stats_arenas_i_dss, arenas_i(mib[2])->dss, const char *)\nCTL_RO_GEN(stats_arenas_i_dirty_decay_ms, arenas_i(mib[2])->dirty_decay_ms,\n    ssize_t)\nCTL_RO_GEN(stats_arenas_i_muzzy_decay_ms, arenas_i(mib[2])->muzzy_decay_ms,\n    ssize_t)\nCTL_RO_GEN(stats_arenas_i_nthreads, arenas_i(mib[2])->nthreads, unsigned)\nCTL_RO_GEN(stats_arenas_i_uptime,\n    nstime_ns(&arenas_i(mib[2])->astats->astats.uptime), uint64_t)\nCTL_RO_GEN(stats_arenas_i_pactive, arenas_i(mib[2])->pactive, size_t)\nCTL_RO_GEN(stats_arenas_i_pdirty, arenas_i(mib[2])->pdirty, size_t)\nCTL_RO_GEN(stats_arenas_i_pmuzzy, arenas_i(mib[2])->pmuzzy, size_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_mapped,\n    atomic_load_zu(&arenas_i(mib[2])->astats->astats.mapped, ATOMIC_RELAXED),\n    size_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_retained,\n    atomic_load_zu(&arenas_i(mib[2])->astats->astats.retained, ATOMIC_RELAXED),\n    size_t)\n\nCTL_RO_CGEN(config_stats, stats_arenas_i_dirty_npurge,\n    arena_stats_read_u64(&arenas_i(mib[2])->astats->astats.decay_dirty.npurge),\n    uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_dirty_nmadvise,\n    arena_stats_read_u64(\n    &arenas_i(mib[2])->astats->astats.decay_dirty.nmadvise), uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_dirty_purged,\n    arena_stats_read_u64(&arenas_i(mib[2])->astats->astats.decay_dirty.purged),\n    uint64_t)\n\nCTL_RO_CGEN(config_stats, stats_arenas_i_muzzy_npurge,\n    arena_stats_read_u64(&arenas_i(mib[2])->astats->astats.decay_muzzy.npurge),\n    uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_muzzy_nmadvise,\n    arena_stats_read_u64(\n    &arenas_i(mib[2])->astats->astats.decay_muzzy.nmadvise), uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_muzzy_purged,\n    arena_stats_read_u64(&arenas_i(mib[2])->astats->astats.decay_muzzy.purged),\n    uint64_t)\n\nCTL_RO_CGEN(config_stats, stats_arenas_i_base,\n    atomic_load_zu(&arenas_i(mib[2])->astats->astats.base, ATOMIC_RELAXED),\n    size_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_internal,\n    atomic_load_zu(&arenas_i(mib[2])->astats->astats.internal, ATOMIC_RELAXED),\n    size_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_tcache_bytes,\n    atomic_load_zu(&arenas_i(mib[2])->astats->astats.tcache_bytes,\n    ATOMIC_RELAXED), size_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_resident,\n    atomic_load_zu(&arenas_i(mib[2])->astats->astats.resident, ATOMIC_RELAXED),\n    size_t)\n\nCTL_RO_CGEN(config_stats, stats_arenas_i_small_allocated,\n    arenas_i(mib[2])->astats->allocated_small, size_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_small_nmalloc,\n    arenas_i(mib[2])->astats->nmalloc_small, uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_small_ndalloc,\n    arenas_i(mib[2])->astats->ndalloc_small, uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_small_nrequests,\n    arenas_i(mib[2])->astats->nrequests_small, uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_large_allocated,\n    atomic_load_zu(&arenas_i(mib[2])->astats->astats.allocated_large,\n    ATOMIC_RELAXED), size_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_large_nmalloc,\n    arena_stats_read_u64(&arenas_i(mib[2])->astats->astats.nmalloc_large),\n    uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_large_ndalloc,\n    arena_stats_read_u64(&arenas_i(mib[2])->astats->astats.ndalloc_large),\n    uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_large_nrequests,\n    arena_stats_read_u64(&arenas_i(mib[2])->astats->astats.nmalloc_large),\n    uint64_t) /* Intentional. */\n\n/* Lock profiling related APIs below. */\n#define RO_MUTEX_CTL_GEN(n, l)\t\t\t\t\t\t\\\nCTL_RO_CGEN(config_stats, stats_##n##_num_ops,\t\t\t\t\\\n    l.n_lock_ops, uint64_t)\t\t\t\t\t\t\\\nCTL_RO_CGEN(config_stats, stats_##n##_num_wait,\t\t\t\t\\\n    l.n_wait_times, uint64_t)\t\t\t\t\t\t\\\nCTL_RO_CGEN(config_stats, stats_##n##_num_spin_acq,\t\t\t\\\n    l.n_spin_acquired, uint64_t)\t\t\t\t\t\\\nCTL_RO_CGEN(config_stats, stats_##n##_num_owner_switch,\t\t\t\\\n    l.n_owner_switches, uint64_t) \t\t\t\t\t\\\nCTL_RO_CGEN(config_stats, stats_##n##_total_wait_time,\t\t\t\\\n    nstime_ns(&l.tot_wait_time), uint64_t)\t\t\t\t\\\nCTL_RO_CGEN(config_stats, stats_##n##_max_wait_time,\t\t\t\\\n    nstime_ns(&l.max_wait_time), uint64_t)\t\t\t\t\\\nCTL_RO_CGEN(config_stats, stats_##n##_max_num_thds,\t\t\t\\\n    l.max_n_thds, uint32_t)\n\n/* Global mutexes. */\n#define OP(mtx)\t\t\t\t\t\t\t\t\\\n    RO_MUTEX_CTL_GEN(mutexes_##mtx,\t\t\t\t\t\\\n        ctl_stats->mutex_prof_data[global_prof_mutex_##mtx])\nMUTEX_PROF_GLOBAL_MUTEXES\n#undef OP\n\n/* Per arena mutexes */\n#define OP(mtx) RO_MUTEX_CTL_GEN(arenas_i_mutexes_##mtx,\t\t\\\n    arenas_i(mib[2])->astats->astats.mutex_prof_data[arena_prof_mutex_##mtx])\nMUTEX_PROF_ARENA_MUTEXES\n#undef OP\n\n/* tcache bin mutex */\nRO_MUTEX_CTL_GEN(arenas_i_bins_j_mutex,\n    arenas_i(mib[2])->astats->bstats[mib[4]].mutex_data)\n#undef RO_MUTEX_CTL_GEN\n\n/* Resets all mutex stats, including global, arena and bin mutexes. */\nstatic int\nstats_mutexes_reset_ctl(tsd_t *tsd, const size_t *mib, size_t miblen,\n    void *oldp, size_t *oldlenp, void *newp, size_t newlen) {\n\tif (!config_stats) {\n\t\treturn ENOENT;\n\t}\n\n\ttsdn_t *tsdn = tsd_tsdn(tsd);\n\n#define MUTEX_PROF_RESET(mtx)\t\t\t\t\t\t\\\n    malloc_mutex_lock(tsdn, &mtx);\t\t\t\t\t\\\n    malloc_mutex_prof_data_reset(tsdn, &mtx);\t\t\t\t\\\n    malloc_mutex_unlock(tsdn, &mtx);\n\n\t/* Global mutexes: ctl and prof. */\n\tMUTEX_PROF_RESET(ctl_mtx);\n\tif (have_background_thread) {\n\t\tMUTEX_PROF_RESET(background_thread_lock);\n\t}\n\tif (config_prof && opt_prof) {\n\t\tMUTEX_PROF_RESET(bt2gctx_mtx);\n\t}\n\n\n\t/* Per arena mutexes. */\n\tunsigned n = narenas_total_get();\n\n\tfor (unsigned i = 0; i < n; i++) {\n\t\tarena_t *arena = arena_get(tsdn, i, false);\n\t\tif (!arena) {\n\t\t\tcontinue;\n\t\t}\n\t\tMUTEX_PROF_RESET(arena->large_mtx);\n\t\tMUTEX_PROF_RESET(arena->extent_avail_mtx);\n\t\tMUTEX_PROF_RESET(arena->extents_dirty.mtx);\n\t\tMUTEX_PROF_RESET(arena->extents_muzzy.mtx);\n\t\tMUTEX_PROF_RESET(arena->extents_retained.mtx);\n\t\tMUTEX_PROF_RESET(arena->decay_dirty.mtx);\n\t\tMUTEX_PROF_RESET(arena->decay_muzzy.mtx);\n\t\tMUTEX_PROF_RESET(arena->tcache_ql_mtx);\n\t\tMUTEX_PROF_RESET(arena->base->mtx);\n\n\t\tfor (szind_t i = 0; i < NBINS; i++) {\n\t\t\tarena_bin_t *bin = &arena->bins[i];\n\t\t\tMUTEX_PROF_RESET(bin->lock);\n\t\t}\n\t}\n#undef MUTEX_PROF_RESET\n\treturn 0;\n}\n\nCTL_RO_CGEN(config_stats, stats_arenas_i_bins_j_nmalloc,\n    arenas_i(mib[2])->astats->bstats[mib[4]].nmalloc, uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_bins_j_ndalloc,\n    arenas_i(mib[2])->astats->bstats[mib[4]].ndalloc, uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_bins_j_nrequests,\n    arenas_i(mib[2])->astats->bstats[mib[4]].nrequests, uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_bins_j_curregs,\n    arenas_i(mib[2])->astats->bstats[mib[4]].curregs, size_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_bins_j_nfills,\n    arenas_i(mib[2])->astats->bstats[mib[4]].nfills, uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_bins_j_nflushes,\n    arenas_i(mib[2])->astats->bstats[mib[4]].nflushes, uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_bins_j_nslabs,\n    arenas_i(mib[2])->astats->bstats[mib[4]].nslabs, uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_bins_j_nreslabs,\n    arenas_i(mib[2])->astats->bstats[mib[4]].reslabs, uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_bins_j_curslabs,\n    arenas_i(mib[2])->astats->bstats[mib[4]].curslabs, size_t)\n\nstatic const ctl_named_node_t *\nstats_arenas_i_bins_j_index(tsdn_t *tsdn, const size_t *mib, size_t miblen,\n    size_t j) {\n\tif (j > NBINS) {\n\t\treturn NULL;\n\t}\n\treturn super_stats_arenas_i_bins_j_node;\n}\n\nCTL_RO_CGEN(config_stats, stats_arenas_i_lextents_j_nmalloc,\n    arena_stats_read_u64(&arenas_i(mib[2])->astats->lstats[mib[4]].nmalloc),\n    uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_lextents_j_ndalloc,\n    arena_stats_read_u64(&arenas_i(mib[2])->astats->lstats[mib[4]].ndalloc),\n    uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_lextents_j_nrequests,\n    arena_stats_read_u64(&arenas_i(mib[2])->astats->lstats[mib[4]].nrequests),\n    uint64_t)\nCTL_RO_CGEN(config_stats, stats_arenas_i_lextents_j_curlextents,\n    arenas_i(mib[2])->astats->lstats[mib[4]].curlextents, size_t)\n\nstatic const ctl_named_node_t *\nstats_arenas_i_lextents_j_index(tsdn_t *tsdn, const size_t *mib, size_t miblen,\n    size_t j) {\n\tif (j > NSIZES - NBINS) {\n\t\treturn NULL;\n\t}\n\treturn super_stats_arenas_i_lextents_j_node;\n}\n\nstatic const ctl_named_node_t *\nstats_arenas_i_index(tsdn_t *tsdn, const size_t *mib, size_t miblen, size_t i) {\n\tconst ctl_named_node_t *ret;\n\tsize_t a;\n\n\tmalloc_mutex_lock(tsdn, &ctl_mtx);\n\ta = arenas_i2a_impl(i, true, true);\n\tif (a == UINT_MAX || !ctl_arenas->arenas[a]->initialized) {\n\t\tret = NULL;\n\t\tgoto label_return;\n\t}\n\n\tret = super_stats_arenas_i_node;\nlabel_return:\n\tmalloc_mutex_unlock(tsdn, &ctl_mtx);\n\treturn ret;\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/extent.c",
    "content": "#define JEMALLOC_EXTENT_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/extent_dss.h\"\n#include \"jemalloc/internal/extent_mmap.h\"\n#include \"jemalloc/internal/ph.h\"\n#include \"jemalloc/internal/rtree.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/mutex_pool.h\"\n\n/******************************************************************************/\n/* Data. */\n\nrtree_t\t\textents_rtree;\n/* Keyed by the address of the extent_t being protected. */\nmutex_pool_t\textent_mutex_pool;\n\nstatic const bitmap_info_t extents_bitmap_info =\n    BITMAP_INFO_INITIALIZER(NPSIZES+1);\n\nstatic void *extent_alloc_default(extent_hooks_t *extent_hooks, void *new_addr,\n    size_t size, size_t alignment, bool *zero, bool *commit,\n    unsigned arena_ind);\nstatic bool extent_dalloc_default(extent_hooks_t *extent_hooks, void *addr,\n    size_t size, bool committed, unsigned arena_ind);\nstatic void extent_destroy_default(extent_hooks_t *extent_hooks, void *addr,\n    size_t size, bool committed, unsigned arena_ind);\nstatic bool extent_commit_default(extent_hooks_t *extent_hooks, void *addr,\n    size_t size, size_t offset, size_t length, unsigned arena_ind);\nstatic bool extent_commit_impl(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length, bool growing_retained);\nstatic bool extent_decommit_default(extent_hooks_t *extent_hooks,\n    void *addr, size_t size, size_t offset, size_t length, unsigned arena_ind);\n#ifdef PAGES_CAN_PURGE_LAZY\nstatic bool extent_purge_lazy_default(extent_hooks_t *extent_hooks, void *addr,\n    size_t size, size_t offset, size_t length, unsigned arena_ind);\n#endif\nstatic bool extent_purge_lazy_impl(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length, bool growing_retained);\n#ifdef PAGES_CAN_PURGE_FORCED\nstatic bool extent_purge_forced_default(extent_hooks_t *extent_hooks,\n    void *addr, size_t size, size_t offset, size_t length, unsigned arena_ind);\n#endif\nstatic bool extent_purge_forced_impl(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length, bool growing_retained);\n#ifdef JEMALLOC_MAPS_COALESCE\nstatic bool extent_split_default(extent_hooks_t *extent_hooks, void *addr,\n    size_t size, size_t size_a, size_t size_b, bool committed,\n    unsigned arena_ind);\n#endif\nstatic extent_t *extent_split_impl(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t size_a,\n    szind_t szind_a, bool slab_a, size_t size_b, szind_t szind_b, bool slab_b,\n    bool growing_retained);\n#ifdef JEMALLOC_MAPS_COALESCE\nstatic bool extent_merge_default(extent_hooks_t *extent_hooks, void *addr_a,\n    size_t size_a, void *addr_b, size_t size_b, bool committed,\n    unsigned arena_ind);\n#endif\nstatic bool extent_merge_impl(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *a, extent_t *b,\n    bool growing_retained);\n\nconst extent_hooks_t\textent_hooks_default = {\n\textent_alloc_default,\n\textent_dalloc_default,\n\textent_destroy_default,\n\textent_commit_default,\n\textent_decommit_default\n#ifdef PAGES_CAN_PURGE_LAZY\n\t,\n\textent_purge_lazy_default\n#else\n\t,\n\tNULL\n#endif\n#ifdef PAGES_CAN_PURGE_FORCED\n\t,\n\textent_purge_forced_default\n#else\n\t,\n\tNULL\n#endif\n#ifdef JEMALLOC_MAPS_COALESCE\n\t,\n\textent_split_default,\n\textent_merge_default\n#endif\n};\n\n/* Used exclusively for gdump triggering. */\nstatic atomic_zu_t curpages;\nstatic atomic_zu_t highpages;\n\n/******************************************************************************/\n/*\n * Function prototypes for static functions that are referenced prior to\n * definition.\n */\n\nstatic void extent_deregister(tsdn_t *tsdn, extent_t *extent);\nstatic extent_t *extent_recycle(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extents_t *extents, void *new_addr,\n    size_t usize, size_t pad, size_t alignment, bool slab, szind_t szind,\n    bool *zero, bool *commit, bool growing_retained);\nstatic extent_t *extent_try_coalesce(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, rtree_ctx_t *rtree_ctx, extents_t *extents,\n    extent_t *extent, bool *coalesced, bool growing_retained);\nstatic void extent_record(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extents_t *extents, extent_t *extent,\n    bool growing_retained);\n\n/******************************************************************************/\n\nrb_gen(UNUSED, extent_avail_, extent_tree_t, extent_t, rb_link,\n    extent_esnead_comp)\n\ntypedef enum {\n\tlock_result_success,\n\tlock_result_failure,\n\tlock_result_no_extent\n} lock_result_t;\n\nstatic lock_result_t\nextent_rtree_leaf_elm_try_lock(tsdn_t *tsdn, rtree_leaf_elm_t *elm,\n    extent_t **result) {\n\textent_t *extent1 = rtree_leaf_elm_extent_read(tsdn, &extents_rtree,\n\t    elm, true);\n\n\tif (extent1 == NULL) {\n\t\treturn lock_result_no_extent;\n\t}\n\t/*\n\t * It's possible that the extent changed out from under us, and with it\n\t * the leaf->extent mapping.  We have to recheck while holding the lock.\n\t */\n\textent_lock(tsdn, extent1);\n\textent_t *extent2 = rtree_leaf_elm_extent_read(tsdn,\n\t    &extents_rtree, elm, true);\n\n\tif (extent1 == extent2) {\n\t\t*result = extent1;\n\t\treturn lock_result_success;\n\t} else {\n\t\textent_unlock(tsdn, extent1);\n\t\treturn lock_result_failure;\n\t}\n}\n\n/*\n * Returns a pool-locked extent_t * if there's one associated with the given\n * address, and NULL otherwise.\n */\nstatic extent_t *\nextent_lock_from_addr(tsdn_t *tsdn, rtree_ctx_t *rtree_ctx, void *addr) {\n\textent_t *ret = NULL;\n\trtree_leaf_elm_t *elm = rtree_leaf_elm_lookup(tsdn, &extents_rtree,\n\t    rtree_ctx, (uintptr_t)addr, false, false);\n\tif (elm == NULL) {\n\t\treturn NULL;\n\t}\n\tlock_result_t lock_result;\n\tdo {\n\t\tlock_result = extent_rtree_leaf_elm_try_lock(tsdn, elm, &ret);\n\t} while (lock_result == lock_result_failure);\n\treturn ret;\n}\n\nextent_t *\nextent_alloc(tsdn_t *tsdn, arena_t *arena) {\n\tmalloc_mutex_lock(tsdn, &arena->extent_avail_mtx);\n\textent_t *extent = extent_avail_first(&arena->extent_avail);\n\tif (extent == NULL) {\n\t\tmalloc_mutex_unlock(tsdn, &arena->extent_avail_mtx);\n\t\treturn base_alloc_extent(tsdn, arena->base);\n\t}\n\textent_avail_remove(&arena->extent_avail, extent);\n\tmalloc_mutex_unlock(tsdn, &arena->extent_avail_mtx);\n\treturn extent;\n}\n\nvoid\nextent_dalloc(tsdn_t *tsdn, arena_t *arena, extent_t *extent) {\n\tmalloc_mutex_lock(tsdn, &arena->extent_avail_mtx);\n\textent_avail_insert(&arena->extent_avail, extent);\n\tmalloc_mutex_unlock(tsdn, &arena->extent_avail_mtx);\n}\n\nextent_hooks_t *\nextent_hooks_get(arena_t *arena) {\n\treturn base_extent_hooks_get(arena->base);\n}\n\nextent_hooks_t *\nextent_hooks_set(tsd_t *tsd, arena_t *arena, extent_hooks_t *extent_hooks) {\n\tbackground_thread_info_t *info;\n\tif (have_background_thread) {\n\t\tinfo = arena_background_thread_info_get(arena);\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);\n\t}\n\textent_hooks_t *ret = base_extent_hooks_set(arena->base, extent_hooks);\n\tif (have_background_thread) {\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);\n\t}\n\n\treturn ret;\n}\n\nstatic void\nextent_hooks_assure_initialized(arena_t *arena,\n    extent_hooks_t **r_extent_hooks) {\n\tif (*r_extent_hooks == EXTENT_HOOKS_INITIALIZER) {\n\t\t*r_extent_hooks = extent_hooks_get(arena);\n\t}\n}\n\n#ifndef JEMALLOC_JET\nstatic\n#endif\nsize_t\nextent_size_quantize_floor(size_t size) {\n\tsize_t ret;\n\tpszind_t pind;\n\n\tassert(size > 0);\n\tassert((size & PAGE_MASK) == 0);\n\n\tpind = sz_psz2ind(size - sz_large_pad + 1);\n\tif (pind == 0) {\n\t\t/*\n\t\t * Avoid underflow.  This short-circuit would also do the right\n\t\t * thing for all sizes in the range for which there are\n\t\t * PAGE-spaced size classes, but it's simplest to just handle\n\t\t * the one case that would cause erroneous results.\n\t\t */\n\t\treturn size;\n\t}\n\tret = sz_pind2sz(pind - 1) + sz_large_pad;\n\tassert(ret <= size);\n\treturn ret;\n}\n\n#ifndef JEMALLOC_JET\nstatic\n#endif\nsize_t\nextent_size_quantize_ceil(size_t size) {\n\tsize_t ret;\n\n\tassert(size > 0);\n\tassert(size - sz_large_pad <= LARGE_MAXCLASS);\n\tassert((size & PAGE_MASK) == 0);\n\n\tret = extent_size_quantize_floor(size);\n\tif (ret < size) {\n\t\t/*\n\t\t * Skip a quantization that may have an adequately large extent,\n\t\t * because under-sized extents may be mixed in.  This only\n\t\t * happens when an unusual size is requested, i.e. for aligned\n\t\t * allocation, and is just one of several places where linear\n\t\t * search would potentially find sufficiently aligned available\n\t\t * memory somewhere lower.\n\t\t */\n\t\tret = sz_pind2sz(sz_psz2ind(ret - sz_large_pad + 1)) +\n\t\t    sz_large_pad;\n\t}\n\treturn ret;\n}\n\n/* Generate pairing heap functions. */\nph_gen(, extent_heap_, extent_heap_t, extent_t, ph_link, extent_snad_comp)\n\nbool\nextents_init(tsdn_t *tsdn, extents_t *extents, extent_state_t state,\n    bool delay_coalesce) {\n\tif (malloc_mutex_init(&extents->mtx, \"extents\", WITNESS_RANK_EXTENTS,\n\t    malloc_mutex_rank_exclusive)) {\n\t\treturn true;\n\t}\n\tfor (unsigned i = 0; i < NPSIZES+1; i++) {\n\t\textent_heap_new(&extents->heaps[i]);\n\t}\n\tbitmap_init(extents->bitmap, &extents_bitmap_info, true);\n\textent_list_init(&extents->lru);\n\tatomic_store_zu(&extents->npages, 0, ATOMIC_RELAXED);\n\textents->state = state;\n\textents->delay_coalesce = delay_coalesce;\n\treturn false;\n}\n\nextent_state_t\nextents_state_get(const extents_t *extents) {\n\treturn extents->state;\n}\n\nsize_t\nextents_npages_get(extents_t *extents) {\n\treturn atomic_load_zu(&extents->npages, ATOMIC_RELAXED);\n}\n\nstatic void\nextents_insert_locked(tsdn_t *tsdn, extents_t *extents, extent_t *extent,\n    bool preserve_lru) {\n\tmalloc_mutex_assert_owner(tsdn, &extents->mtx);\n\tassert(extent_state_get(extent) == extents->state);\n\n\tsize_t size = extent_size_get(extent);\n\tsize_t psz = extent_size_quantize_floor(size);\n\tpszind_t pind = sz_psz2ind(psz);\n\tif (extent_heap_empty(&extents->heaps[pind])) {\n\t\tbitmap_unset(extents->bitmap, &extents_bitmap_info,\n\t\t    (size_t)pind);\n\t}\n\textent_heap_insert(&extents->heaps[pind], extent);\n\tif (!preserve_lru) {\n\t\textent_list_append(&extents->lru, extent);\n\t}\n\tsize_t npages = size >> LG_PAGE;\n\t/*\n\t * All modifications to npages hold the mutex (as asserted above), so we\n\t * don't need an atomic fetch-add; we can get by with a load followed by\n\t * a store.\n\t */\n\tsize_t cur_extents_npages =\n\t    atomic_load_zu(&extents->npages, ATOMIC_RELAXED);\n\tatomic_store_zu(&extents->npages, cur_extents_npages + npages,\n\t    ATOMIC_RELAXED);\n}\n\nstatic void\nextents_remove_locked(tsdn_t *tsdn, extents_t *extents, extent_t *extent,\n    bool preserve_lru) {\n\tmalloc_mutex_assert_owner(tsdn, &extents->mtx);\n\tassert(extent_state_get(extent) == extents->state);\n\n\tsize_t size = extent_size_get(extent);\n\tsize_t psz = extent_size_quantize_floor(size);\n\tpszind_t pind = sz_psz2ind(psz);\n\textent_heap_remove(&extents->heaps[pind], extent);\n\tif (extent_heap_empty(&extents->heaps[pind])) {\n\t\tbitmap_set(extents->bitmap, &extents_bitmap_info,\n\t\t    (size_t)pind);\n\t}\n\tif (!preserve_lru) {\n\t\textent_list_remove(&extents->lru, extent);\n\t}\n\tsize_t npages = size >> LG_PAGE;\n\t/*\n\t * As in extents_insert_locked, we hold extents->mtx and so don't need\n\t * atomic operations for updating extents->npages.\n\t */\n\tsize_t cur_extents_npages =\n\t    atomic_load_zu(&extents->npages, ATOMIC_RELAXED);\n\tassert(cur_extents_npages >= npages);\n\tatomic_store_zu(&extents->npages,\n\t    cur_extents_npages - (size >> LG_PAGE), ATOMIC_RELAXED);\n}\n\n/* Do any-best-fit extent selection, i.e. select any extent that best fits. */\nstatic extent_t *\nextents_best_fit_locked(tsdn_t *tsdn, arena_t *arena, extents_t *extents,\n    size_t size) {\n\tpszind_t pind = sz_psz2ind(extent_size_quantize_ceil(size));\n\tpszind_t i = (pszind_t)bitmap_ffu(extents->bitmap, &extents_bitmap_info,\n\t    (size_t)pind);\n\tif (i < NPSIZES+1) {\n\t\tassert(!extent_heap_empty(&extents->heaps[i]));\n\t\textent_t *extent = extent_heap_any(&extents->heaps[i]);\n\t\tassert(extent_size_get(extent) >= size);\n\t\treturn extent;\n\t}\n\n\treturn NULL;\n}\n\n/*\n * Do first-fit extent selection, i.e. select the oldest/lowest extent that is\n * large enough.\n */\nstatic extent_t *\nextents_first_fit_locked(tsdn_t *tsdn, arena_t *arena, extents_t *extents,\n    size_t size) {\n\textent_t *ret = NULL;\n\n\tpszind_t pind = sz_psz2ind(extent_size_quantize_ceil(size));\n\tfor (pszind_t i = (pszind_t)bitmap_ffu(extents->bitmap,\n\t    &extents_bitmap_info, (size_t)pind); i < NPSIZES+1; i =\n\t    (pszind_t)bitmap_ffu(extents->bitmap, &extents_bitmap_info,\n\t    (size_t)i+1)) {\n\t\tassert(!extent_heap_empty(&extents->heaps[i]));\n\t\textent_t *extent = extent_heap_first(&extents->heaps[i]);\n\t\tassert(extent_size_get(extent) >= size);\n\t\tif (ret == NULL || extent_snad_comp(extent, ret) < 0) {\n\t\t\tret = extent;\n\t\t}\n\t\tif (i == NPSIZES) {\n\t\t\tbreak;\n\t\t}\n\t\tassert(i < NPSIZES);\n\t}\n\n\treturn ret;\n}\n\n/*\n * Do {best,first}-fit extent selection, where the selection policy choice is\n * based on extents->delay_coalesce.  Best-fit selection requires less\n * searching, but its layout policy is less stable and may cause higher virtual\n * memory fragmentation as a side effect.\n */\nstatic extent_t *\nextents_fit_locked(tsdn_t *tsdn, arena_t *arena, extents_t *extents,\n    size_t size) {\n\tmalloc_mutex_assert_owner(tsdn, &extents->mtx);\n\n\treturn extents->delay_coalesce ? extents_best_fit_locked(tsdn, arena,\n\t    extents, size) : extents_first_fit_locked(tsdn, arena, extents,\n\t    size);\n}\n\nstatic bool\nextent_try_delayed_coalesce(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, rtree_ctx_t *rtree_ctx, extents_t *extents,\n    extent_t *extent) {\n\textent_state_set(extent, extent_state_active);\n\tbool coalesced;\n\textent = extent_try_coalesce(tsdn, arena, r_extent_hooks, rtree_ctx,\n\t    extents, extent, &coalesced, false);\n\textent_state_set(extent, extents_state_get(extents));\n\n\tif (!coalesced) {\n\t\treturn true;\n\t}\n\textents_insert_locked(tsdn, extents, extent, true);\n\treturn false;\n}\n\nextent_t *\nextents_alloc(tsdn_t *tsdn, arena_t *arena, extent_hooks_t **r_extent_hooks,\n    extents_t *extents, void *new_addr, size_t size, size_t pad,\n    size_t alignment, bool slab, szind_t szind, bool *zero, bool *commit) {\n\tassert(size + pad != 0);\n\tassert(alignment != 0);\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\treturn extent_recycle(tsdn, arena, r_extent_hooks, extents, new_addr,\n\t    size, pad, alignment, slab, szind, zero, commit, false);\n}\n\nvoid\nextents_dalloc(tsdn_t *tsdn, arena_t *arena, extent_hooks_t **r_extent_hooks,\n    extents_t *extents, extent_t *extent) {\n\tassert(extent_base_get(extent) != NULL);\n\tassert(extent_size_get(extent) != 0);\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\textent_addr_set(extent, extent_base_get(extent));\n\textent_zeroed_set(extent, false);\n\n\textent_record(tsdn, arena, r_extent_hooks, extents, extent, false);\n}\n\nextent_t *\nextents_evict(tsdn_t *tsdn, arena_t *arena, extent_hooks_t **r_extent_hooks,\n    extents_t *extents, size_t npages_min) {\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\n\tmalloc_mutex_lock(tsdn, &extents->mtx);\n\n\t/*\n\t * Get the LRU coalesced extent, if any.  If coalescing was delayed,\n\t * the loop will iterate until the LRU extent is fully coalesced.\n\t */\n\textent_t *extent;\n\twhile (true) {\n\t\t/* Get the LRU extent, if any. */\n\t\textent = extent_list_first(&extents->lru);\n\t\tif (extent == NULL) {\n\t\t\tgoto label_return;\n\t\t}\n\t\t/* Check the eviction limit. */\n\t\tsize_t npages = extent_size_get(extent) >> LG_PAGE;\n\t\tsize_t extents_npages = atomic_load_zu(&extents->npages,\n\t\t    ATOMIC_RELAXED);\n\t\tif (extents_npages - npages < npages_min) {\n\t\t\textent = NULL;\n\t\t\tgoto label_return;\n\t\t}\n\t\textents_remove_locked(tsdn, extents, extent, false);\n\t\tif (!extents->delay_coalesce) {\n\t\t\tbreak;\n\t\t}\n\t\t/* Try to coalesce. */\n\t\tif (extent_try_delayed_coalesce(tsdn, arena, r_extent_hooks,\n\t\t    rtree_ctx, extents, extent)) {\n\t\t\tbreak;\n\t\t}\n\t\t/*\n\t\t * The LRU extent was just coalesced and the result placed in\n\t\t * the LRU at its neighbor's position.  Start over.\n\t\t */\n\t}\n\n\t/*\n\t * Either mark the extent active or deregister it to protect against\n\t * concurrent operations.\n\t */\n\tswitch (extents_state_get(extents)) {\n\tcase extent_state_active:\n\t\tnot_reached();\n\tcase extent_state_dirty:\n\tcase extent_state_muzzy:\n\t\textent_state_set(extent, extent_state_active);\n\t\tbreak;\n\tcase extent_state_retained:\n\t\textent_deregister(tsdn, extent);\n\t\tbreak;\n\tdefault:\n\t\tnot_reached();\n\t}\n\nlabel_return:\n\tmalloc_mutex_unlock(tsdn, &extents->mtx);\n\treturn extent;\n}\n\nstatic void\nextents_leak(tsdn_t *tsdn, arena_t *arena, extent_hooks_t **r_extent_hooks,\n    extents_t *extents, extent_t *extent, bool growing_retained) {\n\t/*\n\t * Leak extent after making sure its pages have already been purged, so\n\t * that this is only a virtual memory leak.\n\t */\n\tif (extents_state_get(extents) == extent_state_dirty) {\n\t\tif (extent_purge_lazy_impl(tsdn, arena, r_extent_hooks,\n\t\t    extent, 0, extent_size_get(extent), growing_retained)) {\n\t\t\textent_purge_forced_impl(tsdn, arena, r_extent_hooks,\n\t\t\t    extent, 0, extent_size_get(extent),\n\t\t\t    growing_retained);\n\t\t}\n\t}\n\textent_dalloc(tsdn, arena, extent);\n}\n\nvoid\nextents_prefork(tsdn_t *tsdn, extents_t *extents) {\n\tmalloc_mutex_prefork(tsdn, &extents->mtx);\n}\n\nvoid\nextents_postfork_parent(tsdn_t *tsdn, extents_t *extents) {\n\tmalloc_mutex_postfork_parent(tsdn, &extents->mtx);\n}\n\nvoid\nextents_postfork_child(tsdn_t *tsdn, extents_t *extents) {\n\tmalloc_mutex_postfork_child(tsdn, &extents->mtx);\n}\n\nstatic void\nextent_deactivate_locked(tsdn_t *tsdn, arena_t *arena, extents_t *extents,\n    extent_t *extent, bool preserve_lru) {\n\tassert(extent_arena_get(extent) == arena);\n\tassert(extent_state_get(extent) == extent_state_active);\n\n\textent_state_set(extent, extents_state_get(extents));\n\textents_insert_locked(tsdn, extents, extent, preserve_lru);\n}\n\nstatic void\nextent_deactivate(tsdn_t *tsdn, arena_t *arena, extents_t *extents,\n    extent_t *extent, bool preserve_lru) {\n\tmalloc_mutex_lock(tsdn, &extents->mtx);\n\textent_deactivate_locked(tsdn, arena, extents, extent, preserve_lru);\n\tmalloc_mutex_unlock(tsdn, &extents->mtx);\n}\n\nstatic void\nextent_activate_locked(tsdn_t *tsdn, arena_t *arena, extents_t *extents,\n    extent_t *extent, bool preserve_lru) {\n\tassert(extent_arena_get(extent) == arena);\n\tassert(extent_state_get(extent) == extents_state_get(extents));\n\n\textents_remove_locked(tsdn, extents, extent, preserve_lru);\n\textent_state_set(extent, extent_state_active);\n}\n\nstatic bool\nextent_rtree_leaf_elms_lookup(tsdn_t *tsdn, rtree_ctx_t *rtree_ctx,\n    const extent_t *extent, bool dependent, bool init_missing,\n    rtree_leaf_elm_t **r_elm_a, rtree_leaf_elm_t **r_elm_b) {\n\t*r_elm_a = rtree_leaf_elm_lookup(tsdn, &extents_rtree, rtree_ctx,\n\t    (uintptr_t)extent_base_get(extent), dependent, init_missing);\n\tif (!dependent && *r_elm_a == NULL) {\n\t\treturn true;\n\t}\n\tassert(*r_elm_a != NULL);\n\n\t*r_elm_b = rtree_leaf_elm_lookup(tsdn, &extents_rtree, rtree_ctx,\n\t    (uintptr_t)extent_last_get(extent), dependent, init_missing);\n\tif (!dependent && *r_elm_b == NULL) {\n\t\treturn true;\n\t}\n\tassert(*r_elm_b != NULL);\n\n\treturn false;\n}\n\nstatic void\nextent_rtree_write_acquired(tsdn_t *tsdn, rtree_leaf_elm_t *elm_a,\n    rtree_leaf_elm_t *elm_b, extent_t *extent, szind_t szind, bool slab) {\n\trtree_leaf_elm_write(tsdn, &extents_rtree, elm_a, extent, szind, slab);\n\tif (elm_b != NULL) {\n\t\trtree_leaf_elm_write(tsdn, &extents_rtree, elm_b, extent, szind,\n\t\t    slab);\n\t}\n}\n\nstatic void\nextent_interior_register(tsdn_t *tsdn, rtree_ctx_t *rtree_ctx, extent_t *extent,\n    szind_t szind) {\n\tassert(extent_slab_get(extent));\n\n\t/* Register interior. */\n\tfor (size_t i = 1; i < (extent_size_get(extent) >> LG_PAGE) - 1; i++) {\n\t\trtree_write(tsdn, &extents_rtree, rtree_ctx,\n\t\t    (uintptr_t)extent_base_get(extent) + (uintptr_t)(i <<\n\t\t    LG_PAGE), extent, szind, true);\n\t}\n}\n\nstatic void\nextent_gdump_add(tsdn_t *tsdn, const extent_t *extent) {\n\tcassert(config_prof);\n\t/* prof_gdump() requirement. */\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\tif (opt_prof && extent_state_get(extent) == extent_state_active) {\n\t\tsize_t nadd = extent_size_get(extent) >> LG_PAGE;\n\t\tsize_t cur = atomic_fetch_add_zu(&curpages, nadd,\n\t\t    ATOMIC_RELAXED) + nadd;\n\t\tsize_t high = atomic_load_zu(&highpages, ATOMIC_RELAXED);\n\t\twhile (cur > high && !atomic_compare_exchange_weak_zu(\n\t\t    &highpages, &high, cur, ATOMIC_RELAXED, ATOMIC_RELAXED)) {\n\t\t\t/*\n\t\t\t * Don't refresh cur, because it may have decreased\n\t\t\t * since this thread lost the highpages update race.\n\t\t\t * Note that high is updated in case of CAS failure.\n\t\t\t */\n\t\t}\n\t\tif (cur > high && prof_gdump_get_unlocked()) {\n\t\t\tprof_gdump(tsdn);\n\t\t}\n\t}\n}\n\nstatic void\nextent_gdump_sub(tsdn_t *tsdn, const extent_t *extent) {\n\tcassert(config_prof);\n\n\tif (opt_prof && extent_state_get(extent) == extent_state_active) {\n\t\tsize_t nsub = extent_size_get(extent) >> LG_PAGE;\n\t\tassert(atomic_load_zu(&curpages, ATOMIC_RELAXED) >= nsub);\n\t\tatomic_fetch_sub_zu(&curpages, nsub, ATOMIC_RELAXED);\n\t}\n}\n\nstatic bool\nextent_register_impl(tsdn_t *tsdn, extent_t *extent, bool gdump_add) {\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\trtree_leaf_elm_t *elm_a, *elm_b;\n\n\t/*\n\t * We need to hold the lock to protect against a concurrent coalesce\n\t * operation that sees us in a partial state.\n\t */\n\textent_lock(tsdn, extent);\n\n\tif (extent_rtree_leaf_elms_lookup(tsdn, rtree_ctx, extent, false, true,\n\t    &elm_a, &elm_b)) {\n\t\treturn true;\n\t}\n\n\tszind_t szind = extent_szind_get_maybe_invalid(extent);\n\tbool slab = extent_slab_get(extent);\n\textent_rtree_write_acquired(tsdn, elm_a, elm_b, extent, szind, slab);\n\tif (slab) {\n\t\textent_interior_register(tsdn, rtree_ctx, extent, szind);\n\t}\n\n\textent_unlock(tsdn, extent);\n\n\tif (config_prof && gdump_add) {\n\t\textent_gdump_add(tsdn, extent);\n\t}\n\n\treturn false;\n}\n\nstatic bool\nextent_register(tsdn_t *tsdn, extent_t *extent) {\n\treturn extent_register_impl(tsdn, extent, true);\n}\n\nstatic bool\nextent_register_no_gdump_add(tsdn_t *tsdn, extent_t *extent) {\n\treturn extent_register_impl(tsdn, extent, false);\n}\n\nstatic void\nextent_reregister(tsdn_t *tsdn, extent_t *extent) {\n\tbool err = extent_register(tsdn, extent);\n\tassert(!err);\n}\n\nstatic void\nextent_interior_deregister(tsdn_t *tsdn, rtree_ctx_t *rtree_ctx,\n    extent_t *extent) {\n\tsize_t i;\n\n\tassert(extent_slab_get(extent));\n\n\tfor (i = 1; i < (extent_size_get(extent) >> LG_PAGE) - 1; i++) {\n\t\trtree_clear(tsdn, &extents_rtree, rtree_ctx,\n\t\t    (uintptr_t)extent_base_get(extent) + (uintptr_t)(i <<\n\t\t    LG_PAGE));\n\t}\n}\n\nstatic void\nextent_deregister(tsdn_t *tsdn, extent_t *extent) {\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\trtree_leaf_elm_t *elm_a, *elm_b;\n\textent_rtree_leaf_elms_lookup(tsdn, rtree_ctx, extent, true, false,\n\t    &elm_a, &elm_b);\n\n\textent_lock(tsdn, extent);\n\n\textent_rtree_write_acquired(tsdn, elm_a, elm_b, NULL, NSIZES, false);\n\tif (extent_slab_get(extent)) {\n\t\textent_interior_deregister(tsdn, rtree_ctx, extent);\n\t\textent_slab_set(extent, false);\n\t}\n\n\textent_unlock(tsdn, extent);\n\n\tif (config_prof) {\n\t\textent_gdump_sub(tsdn, extent);\n\t}\n}\n\nstatic extent_t *\nextent_recycle_extract(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, rtree_ctx_t *rtree_ctx, extents_t *extents,\n    void *new_addr, size_t size, size_t pad, size_t alignment, bool slab,\n    bool *zero, bool *commit, bool growing_retained) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, growing_retained ? 1 : 0);\n\tassert(alignment > 0);\n\tif (config_debug && new_addr != NULL) {\n\t\t/*\n\t\t * Non-NULL new_addr has two use cases:\n\t\t *\n\t\t *   1) Recycle a known-extant extent, e.g. during purging.\n\t\t *   2) Perform in-place expanding reallocation.\n\t\t *\n\t\t * Regardless of use case, new_addr must either refer to a\n\t\t * non-existing extent, or to the base of an extant extent,\n\t\t * since only active slabs support interior lookups (which of\n\t\t * course cannot be recycled).\n\t\t */\n\t\tassert(PAGE_ADDR2BASE(new_addr) == new_addr);\n\t\tassert(pad == 0);\n\t\tassert(alignment <= PAGE);\n\t}\n\n\tsize_t esize = size + pad;\n\tsize_t alloc_size = esize + PAGE_CEILING(alignment) - PAGE;\n\t/* Beware size_t wrap-around. */\n\tif (alloc_size < esize) {\n\t\treturn NULL;\n\t}\n\tmalloc_mutex_lock(tsdn, &extents->mtx);\n\textent_hooks_assure_initialized(arena, r_extent_hooks);\n\textent_t *extent;\n\tif (new_addr != NULL) {\n\t\textent = extent_lock_from_addr(tsdn, rtree_ctx, new_addr);\n\t\tif (extent != NULL) {\n\t\t\t/*\n\t\t\t * We might null-out extent to report an error, but we\n\t\t\t * still need to unlock the associated mutex after.\n\t\t\t */\n\t\t\textent_t *unlock_extent = extent;\n\t\t\tassert(extent_base_get(extent) == new_addr);\n\t\t\tif (extent_arena_get(extent) != arena ||\n\t\t\t    extent_size_get(extent) < esize ||\n\t\t\t    extent_state_get(extent) !=\n\t\t\t    extents_state_get(extents)) {\n\t\t\t\textent = NULL;\n\t\t\t}\n\t\t\textent_unlock(tsdn, unlock_extent);\n\t\t}\n\t} else {\n\t\textent = extents_fit_locked(tsdn, arena, extents, alloc_size);\n\t}\n\tif (extent == NULL) {\n\t\tmalloc_mutex_unlock(tsdn, &extents->mtx);\n\t\treturn NULL;\n\t}\n\n\textent_activate_locked(tsdn, arena, extents, extent, false);\n\tmalloc_mutex_unlock(tsdn, &extents->mtx);\n\n\tif (extent_zeroed_get(extent)) {\n\t\t*zero = true;\n\t}\n\tif (extent_committed_get(extent)) {\n\t\t*commit = true;\n\t}\n\n\treturn extent;\n}\n\nstatic extent_t *\nextent_recycle_split(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, rtree_ctx_t *rtree_ctx, extents_t *extents,\n    void *new_addr, size_t size, size_t pad, size_t alignment, bool slab,\n    szind_t szind, extent_t *extent, bool growing_retained) {\n\tsize_t esize = size + pad;\n\tsize_t leadsize = ALIGNMENT_CEILING((uintptr_t)extent_base_get(extent),\n\t    PAGE_CEILING(alignment)) - (uintptr_t)extent_base_get(extent);\n\tassert(new_addr == NULL || leadsize == 0);\n\tassert(extent_size_get(extent) >= leadsize + esize);\n\tsize_t trailsize = extent_size_get(extent) - leadsize - esize;\n\n\t/* Split the lead. */\n\tif (leadsize != 0) {\n\t\textent_t *lead = extent;\n\t\textent = extent_split_impl(tsdn, arena, r_extent_hooks,\n\t\t    lead, leadsize, NSIZES, false, esize + trailsize, szind,\n\t\t    slab, growing_retained);\n\t\tif (extent == NULL) {\n\t\t\textent_deregister(tsdn, lead);\n\t\t\textents_leak(tsdn, arena, r_extent_hooks, extents,\n\t\t\t    lead, growing_retained);\n\t\t\treturn NULL;\n\t\t}\n\t\textent_deactivate(tsdn, arena, extents, lead, false);\n\t}\n\n\t/* Split the trail. */\n\tif (trailsize != 0) {\n\t\textent_t *trail = extent_split_impl(tsdn, arena,\n\t\t    r_extent_hooks, extent, esize, szind, slab, trailsize,\n\t\t    NSIZES, false, growing_retained);\n\t\tif (trail == NULL) {\n\t\t\textent_deregister(tsdn, extent);\n\t\t\textents_leak(tsdn, arena, r_extent_hooks, extents,\n\t\t\t    extent, growing_retained);\n\t\t\treturn NULL;\n\t\t}\n\t\textent_deactivate(tsdn, arena, extents, trail, false);\n\t} else if (leadsize == 0) {\n\t\t/*\n\t\t * Splitting causes szind to be set as a side effect, but no\n\t\t * splitting occurred.\n\t\t */\n\t\textent_szind_set(extent, szind);\n\t\tif (szind != NSIZES) {\n\t\t\trtree_szind_slab_update(tsdn, &extents_rtree, rtree_ctx,\n\t\t\t    (uintptr_t)extent_addr_get(extent), szind, slab);\n\t\t\tif (slab && extent_size_get(extent) > PAGE) {\n\t\t\t\trtree_szind_slab_update(tsdn, &extents_rtree,\n\t\t\t\t    rtree_ctx,\n\t\t\t\t    (uintptr_t)extent_past_get(extent) -\n\t\t\t\t    (uintptr_t)PAGE, szind, slab);\n\t\t\t}\n\t\t}\n\t}\n\n\treturn extent;\n}\n\nstatic extent_t *\nextent_recycle(tsdn_t *tsdn, arena_t *arena, extent_hooks_t **r_extent_hooks,\n    extents_t *extents, void *new_addr, size_t size, size_t pad,\n    size_t alignment, bool slab, szind_t szind, bool *zero, bool *commit,\n    bool growing_retained) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, growing_retained ? 1 : 0);\n\tassert(new_addr == NULL || !slab);\n\tassert(pad == 0 || !slab);\n\tassert(!*zero || !slab);\n\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\n\tbool committed = false;\n\textent_t *extent = extent_recycle_extract(tsdn, arena, r_extent_hooks,\n\t    rtree_ctx, extents, new_addr, size, pad, alignment, slab, zero,\n\t    &committed, growing_retained);\n\tif (extent == NULL) {\n\t\treturn NULL;\n\t}\n\tif (committed) {\n\t\t*commit = true;\n\t}\n\n\textent = extent_recycle_split(tsdn, arena, r_extent_hooks, rtree_ctx,\n\t    extents, new_addr, size, pad, alignment, slab, szind, extent,\n\t    growing_retained);\n\tif (extent == NULL) {\n\t\treturn NULL;\n\t}\n\n\tif (*commit && !extent_committed_get(extent)) {\n\t\tif (extent_commit_impl(tsdn, arena, r_extent_hooks, extent,\n\t\t    0, extent_size_get(extent), growing_retained)) {\n\t\t\textent_record(tsdn, arena, r_extent_hooks, extents,\n\t\t\t    extent, growing_retained);\n\t\t\treturn NULL;\n\t\t}\n\t\textent_zeroed_set(extent, true);\n\t}\n\n\tif (pad != 0) {\n\t\textent_addr_randomize(tsdn, extent, alignment);\n\t}\n\tassert(extent_state_get(extent) == extent_state_active);\n\tif (slab) {\n\t\textent_slab_set(extent, slab);\n\t\textent_interior_register(tsdn, rtree_ctx, extent, szind);\n\t}\n\n\tif (*zero) {\n\t\tvoid *addr = extent_base_get(extent);\n\t\tsize_t size = extent_size_get(extent);\n\t\tif (!extent_zeroed_get(extent)) {\n\t\t\tif (pages_purge_forced(addr, size)) {\n\t\t\t\tmemset(addr, 0, size);\n\t\t\t}\n\t\t} else if (config_debug) {\n\t\t\tsize_t *p = (size_t *)(uintptr_t)addr;\n\t\t\tfor (size_t i = 0; i < size / sizeof(size_t); i++) {\n\t\t\t\tassert(p[i] == 0);\n\t\t\t}\n\t\t}\n\t}\n\treturn extent;\n}\n\n/*\n * If the caller specifies (!*zero), it is still possible to receive zeroed\n * memory, in which case *zero is toggled to true.  arena_extent_alloc() takes\n * advantage of this to avoid demanding zeroed extents, but taking advantage of\n * them if they are returned.\n */\nstatic void *\nextent_alloc_core(tsdn_t *tsdn, arena_t *arena, void *new_addr, size_t size,\n    size_t alignment, bool *zero, bool *commit, dss_prec_t dss_prec) {\n\tvoid *ret;\n\n\tassert(size != 0);\n\tassert(alignment != 0);\n\n\t/* \"primary\" dss. */\n\tif (have_dss && dss_prec == dss_prec_primary && (ret =\n\t    extent_alloc_dss(tsdn, arena, new_addr, size, alignment, zero,\n\t    commit)) != NULL) {\n\t\treturn ret;\n\t}\n\t/* mmap. */\n\tif ((ret = extent_alloc_mmap(new_addr, size, alignment, zero, commit))\n\t    != NULL) {\n\t\treturn ret;\n\t}\n\t/* \"secondary\" dss. */\n\tif (have_dss && dss_prec == dss_prec_secondary && (ret =\n\t    extent_alloc_dss(tsdn, arena, new_addr, size, alignment, zero,\n\t    commit)) != NULL) {\n\t\treturn ret;\n\t}\n\n\t/* All strategies for allocation failed. */\n\treturn NULL;\n}\n\nstatic void *\nextent_alloc_default_impl(tsdn_t *tsdn, arena_t *arena, void *new_addr,\n    size_t size, size_t alignment, bool *zero, bool *commit) {\n\tvoid *ret;\n\n\tret = extent_alloc_core(tsdn, arena, new_addr, size, alignment, zero,\n\t    commit, (dss_prec_t)atomic_load_u(&arena->dss_prec,\n\t    ATOMIC_RELAXED));\n\treturn ret;\n}\n\nstatic void *\nextent_alloc_default(extent_hooks_t *extent_hooks, void *new_addr, size_t size,\n    size_t alignment, bool *zero, bool *commit, unsigned arena_ind) {\n\ttsdn_t *tsdn;\n\tarena_t *arena;\n\n\ttsdn = tsdn_fetch();\n\tarena = arena_get(tsdn, arena_ind, false);\n\t/*\n\t * The arena we're allocating on behalf of must have been initialized\n\t * already.\n\t */\n\tassert(arena != NULL);\n\n\treturn extent_alloc_default_impl(tsdn, arena, new_addr, size,\n\t    alignment, zero, commit);\n}\n\n/*\n * If virtual memory is retained, create increasingly larger extents from which\n * to split requested extents in order to limit the total number of disjoint\n * virtual memory ranges retained by each arena.\n */\nstatic extent_t *\nextent_grow_retained(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, size_t size, size_t pad, size_t alignment,\n    bool slab, szind_t szind, bool *zero, bool *commit) {\n\tmalloc_mutex_assert_owner(tsdn, &arena->extent_grow_mtx);\n\tassert(pad == 0 || !slab);\n\tassert(!*zero || !slab);\n\n\tsize_t esize = size + pad;\n\tsize_t alloc_size_min = esize + PAGE_CEILING(alignment) - PAGE;\n\t/* Beware size_t wrap-around. */\n\tif (alloc_size_min < esize) {\n\t\tgoto label_err;\n\t}\n\t/*\n\t * Find the next extent size in the series that would be large enough to\n\t * satisfy this request.\n\t */\n\tpszind_t egn_skip = 0;\n\tsize_t alloc_size = sz_pind2sz(arena->extent_grow_next + egn_skip);\n\twhile (alloc_size < alloc_size_min) {\n\t\tegn_skip++;\n\t\tif (arena->extent_grow_next + egn_skip == NPSIZES) {\n\t\t\t/* Outside legal range. */\n\t\t\tgoto label_err;\n\t\t}\n\t\tassert(arena->extent_grow_next + egn_skip < NPSIZES);\n\t\talloc_size = sz_pind2sz(arena->extent_grow_next + egn_skip);\n\t}\n\n\textent_t *extent = extent_alloc(tsdn, arena);\n\tif (extent == NULL) {\n\t\tgoto label_err;\n\t}\n\tbool zeroed = false;\n\tbool committed = false;\n\n\tvoid *ptr;\n\tif (*r_extent_hooks == &extent_hooks_default) {\n\t\tptr = extent_alloc_core(tsdn, arena, NULL, alloc_size, PAGE,\n\t\t    &zeroed, &committed, (dss_prec_t)atomic_load_u(\n\t\t    &arena->dss_prec, ATOMIC_RELAXED));\n\t} else {\n\t\tptr = (*r_extent_hooks)->alloc(*r_extent_hooks, NULL,\n\t\t    alloc_size, PAGE, &zeroed, &committed,\n\t\t    arena_ind_get(arena));\n\t}\n\n\textent_init(extent, arena, ptr, alloc_size, false, NSIZES,\n\t    arena_extent_sn_next(arena), extent_state_active, zeroed,\n\t    committed);\n\tif (ptr == NULL) {\n\t\textent_dalloc(tsdn, arena, extent);\n\t\tgoto label_err;\n\t}\n\tif (extent_register_no_gdump_add(tsdn, extent)) {\n\t\textents_leak(tsdn, arena, r_extent_hooks,\n\t\t    &arena->extents_retained, extent, true);\n\t\tgoto label_err;\n\t}\n\n\tsize_t leadsize = ALIGNMENT_CEILING((uintptr_t)ptr,\n\t    PAGE_CEILING(alignment)) - (uintptr_t)ptr;\n\tassert(alloc_size >= leadsize + esize);\n\tsize_t trailsize = alloc_size - leadsize - esize;\n\tif (extent_zeroed_get(extent) && extent_committed_get(extent)) {\n\t\t*zero = true;\n\t}\n\tif (extent_committed_get(extent)) {\n\t\t*commit = true;\n\t}\n\n\t/* Split the lead. */\n\tif (leadsize != 0) {\n\t\textent_t *lead = extent;\n\t\textent = extent_split_impl(tsdn, arena, r_extent_hooks, lead,\n\t\t    leadsize, NSIZES, false, esize + trailsize, szind, slab,\n\t\t    true);\n\t\tif (extent == NULL) {\n\t\t\textent_deregister(tsdn, lead);\n\t\t\textents_leak(tsdn, arena, r_extent_hooks,\n\t\t\t    &arena->extents_retained, lead, true);\n\t\t\tgoto label_err;\n\t\t}\n\t\textent_record(tsdn, arena, r_extent_hooks,\n\t\t    &arena->extents_retained, lead, true);\n\t}\n\n\t/* Split the trail. */\n\tif (trailsize != 0) {\n\t\textent_t *trail = extent_split_impl(tsdn, arena, r_extent_hooks,\n\t\t    extent, esize, szind, slab, trailsize, NSIZES, false, true);\n\t\tif (trail == NULL) {\n\t\t\textent_deregister(tsdn, extent);\n\t\t\textents_leak(tsdn, arena, r_extent_hooks,\n\t\t\t    &arena->extents_retained, extent, true);\n\t\t\tgoto label_err;\n\t\t}\n\t\textent_record(tsdn, arena, r_extent_hooks,\n\t\t    &arena->extents_retained, trail, true);\n\t} else if (leadsize == 0) {\n\t\t/*\n\t\t * Splitting causes szind to be set as a side effect, but no\n\t\t * splitting occurred.\n\t\t */\n\t\trtree_ctx_t rtree_ctx_fallback;\n\t\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn,\n\t\t    &rtree_ctx_fallback);\n\n\t\textent_szind_set(extent, szind);\n\t\tif (szind != NSIZES) {\n\t\t\trtree_szind_slab_update(tsdn, &extents_rtree, rtree_ctx,\n\t\t\t    (uintptr_t)extent_addr_get(extent), szind, slab);\n\t\t\tif (slab && extent_size_get(extent) > PAGE) {\n\t\t\t\trtree_szind_slab_update(tsdn, &extents_rtree,\n\t\t\t\t    rtree_ctx,\n\t\t\t\t    (uintptr_t)extent_past_get(extent) -\n\t\t\t\t    (uintptr_t)PAGE, szind, slab);\n\t\t\t}\n\t\t}\n\t}\n\n\tif (*commit && !extent_committed_get(extent)) {\n\t\tif (extent_commit_impl(tsdn, arena, r_extent_hooks, extent, 0,\n\t\t    extent_size_get(extent), true)) {\n\t\t\textent_record(tsdn, arena, r_extent_hooks,\n\t\t\t    &arena->extents_retained, extent, true);\n\t\t\tgoto label_err;\n\t\t}\n\t\textent_zeroed_set(extent, true);\n\t}\n\n\t/*\n\t * Increment extent_grow_next if doing so wouldn't exceed the legal\n\t * range.\n\t */\n\tif (arena->extent_grow_next + egn_skip + 1 < NPSIZES) {\n\t\tarena->extent_grow_next += egn_skip + 1;\n\t} else {\n\t\tarena->extent_grow_next = NPSIZES - 1;\n\t}\n\t/* All opportunities for failure are past. */\n\tmalloc_mutex_unlock(tsdn, &arena->extent_grow_mtx);\n\n\tif (config_prof) {\n\t\t/* Adjust gdump stats now that extent is final size. */\n\t\textent_gdump_add(tsdn, extent);\n\t}\n\tif (pad != 0) {\n\t\textent_addr_randomize(tsdn, extent, alignment);\n\t}\n\tif (slab) {\n\t\trtree_ctx_t rtree_ctx_fallback;\n\t\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn,\n\t\t    &rtree_ctx_fallback);\n\n\t\textent_slab_set(extent, true);\n\t\textent_interior_register(tsdn, rtree_ctx, extent, szind);\n\t}\n\tif (*zero && !extent_zeroed_get(extent)) {\n\t\tvoid *addr = extent_base_get(extent);\n\t\tsize_t size = extent_size_get(extent);\n\t\tif (pages_purge_forced(addr, size)) {\n\t\t\tmemset(addr, 0, size);\n\t\t}\n\t}\n\n\treturn extent;\nlabel_err:\n\tmalloc_mutex_unlock(tsdn, &arena->extent_grow_mtx);\n\treturn NULL;\n}\n\nstatic extent_t *\nextent_alloc_retained(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, void *new_addr, size_t size, size_t pad,\n    size_t alignment, bool slab, szind_t szind, bool *zero, bool *commit) {\n\tassert(size != 0);\n\tassert(alignment != 0);\n\n\tmalloc_mutex_lock(tsdn, &arena->extent_grow_mtx);\n\n\textent_t *extent = extent_recycle(tsdn, arena, r_extent_hooks,\n\t    &arena->extents_retained, new_addr, size, pad, alignment, slab,\n\t    szind, zero, commit, true);\n\tif (extent != NULL) {\n\t\tmalloc_mutex_unlock(tsdn, &arena->extent_grow_mtx);\n\t\tif (config_prof) {\n\t\t\textent_gdump_add(tsdn, extent);\n\t\t}\n\t} else if (opt_retain && new_addr == NULL) {\n\t\textent = extent_grow_retained(tsdn, arena, r_extent_hooks, size,\n\t\t    pad, alignment, slab, szind, zero, commit);\n\t\t/* extent_grow_retained() always releases extent_grow_mtx. */\n\t} else {\n\t\tmalloc_mutex_unlock(tsdn, &arena->extent_grow_mtx);\n\t}\n\tmalloc_mutex_assert_not_owner(tsdn, &arena->extent_grow_mtx);\n\n\treturn extent;\n}\n\nstatic extent_t *\nextent_alloc_wrapper_hard(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, void *new_addr, size_t size, size_t pad,\n    size_t alignment, bool slab, szind_t szind, bool *zero, bool *commit) {\n\tsize_t esize = size + pad;\n\textent_t *extent = extent_alloc(tsdn, arena);\n\tif (extent == NULL) {\n\t\treturn NULL;\n\t}\n\tvoid *addr;\n\tif (*r_extent_hooks == &extent_hooks_default) {\n\t\t/* Call directly to propagate tsdn. */\n\t\taddr = extent_alloc_default_impl(tsdn, arena, new_addr, esize,\n\t\t    alignment, zero, commit);\n\t} else {\n\t\taddr = (*r_extent_hooks)->alloc(*r_extent_hooks, new_addr,\n\t\t    esize, alignment, zero, commit, arena_ind_get(arena));\n\t}\n\tif (addr == NULL) {\n\t\textent_dalloc(tsdn, arena, extent);\n\t\treturn NULL;\n\t}\n\textent_init(extent, arena, addr, esize, slab, szind,\n\t    arena_extent_sn_next(arena), extent_state_active, zero, commit);\n\tif (pad != 0) {\n\t\textent_addr_randomize(tsdn, extent, alignment);\n\t}\n\tif (extent_register(tsdn, extent)) {\n\t\textents_leak(tsdn, arena, r_extent_hooks,\n\t\t    &arena->extents_retained, extent, false);\n\t\treturn NULL;\n\t}\n\n\treturn extent;\n}\n\nextent_t *\nextent_alloc_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, void *new_addr, size_t size, size_t pad,\n    size_t alignment, bool slab, szind_t szind, bool *zero, bool *commit) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\textent_hooks_assure_initialized(arena, r_extent_hooks);\n\n\textent_t *extent = extent_alloc_retained(tsdn, arena, r_extent_hooks,\n\t    new_addr, size, pad, alignment, slab, szind, zero, commit);\n\tif (extent == NULL) {\n\t\textent = extent_alloc_wrapper_hard(tsdn, arena, r_extent_hooks,\n\t\t    new_addr, size, pad, alignment, slab, szind, zero, commit);\n\t}\n\n\treturn extent;\n}\n\nstatic bool\nextent_can_coalesce(arena_t *arena, extents_t *extents, const extent_t *inner,\n    const extent_t *outer) {\n\tassert(extent_arena_get(inner) == arena);\n\tif (extent_arena_get(outer) != arena) {\n\t\treturn false;\n\t}\n\n\tassert(extent_state_get(inner) == extent_state_active);\n\tif (extent_state_get(outer) != extents->state) {\n\t\treturn false;\n\t}\n\n\tif (extent_committed_get(inner) != extent_committed_get(outer)) {\n\t\treturn false;\n\t}\n\n\treturn true;\n}\n\nstatic bool\nextent_coalesce(tsdn_t *tsdn, arena_t *arena, extent_hooks_t **r_extent_hooks,\n    extents_t *extents, extent_t *inner, extent_t *outer, bool forward,\n    bool growing_retained) {\n\tassert(extent_can_coalesce(arena, extents, inner, outer));\n\n\tif (forward && extents->delay_coalesce) {\n\t\t/*\n\t\t * The extent that remains after coalescing must occupy the\n\t\t * outer extent's position in the LRU.  For forward coalescing,\n\t\t * swap the inner extent into the LRU.\n\t\t */\n\t\textent_list_replace(&extents->lru, outer, inner);\n\t}\n\textent_activate_locked(tsdn, arena, extents, outer,\n\t    extents->delay_coalesce);\n\n\tmalloc_mutex_unlock(tsdn, &extents->mtx);\n\tbool err = extent_merge_impl(tsdn, arena, r_extent_hooks,\n\t    forward ? inner : outer, forward ? outer : inner, growing_retained);\n\tmalloc_mutex_lock(tsdn, &extents->mtx);\n\n\tif (err) {\n\t\tif (forward && extents->delay_coalesce) {\n\t\t\textent_list_replace(&extents->lru, inner, outer);\n\t\t}\n\t\textent_deactivate_locked(tsdn, arena, extents, outer,\n\t\t    extents->delay_coalesce);\n\t}\n\n\treturn err;\n}\n\nstatic extent_t *\nextent_try_coalesce(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, rtree_ctx_t *rtree_ctx, extents_t *extents,\n    extent_t *extent, bool *coalesced, bool growing_retained) {\n\t/*\n\t * Continue attempting to coalesce until failure, to protect against\n\t * races with other threads that are thwarted by this one.\n\t */\n\tbool again;\n\tdo {\n\t\tagain = false;\n\n\t\t/* Try to coalesce forward. */\n\t\textent_t *next = extent_lock_from_addr(tsdn, rtree_ctx,\n\t\t    extent_past_get(extent));\n\t\tif (next != NULL) {\n\t\t\t/*\n\t\t\t * extents->mtx only protects against races for\n\t\t\t * like-state extents, so call extent_can_coalesce()\n\t\t\t * before releasing next's pool lock.\n\t\t\t */\n\t\t\tbool can_coalesce = extent_can_coalesce(arena, extents,\n\t\t\t    extent, next);\n\n\t\t\textent_unlock(tsdn, next);\n\n\t\t\tif (can_coalesce && !extent_coalesce(tsdn, arena,\n\t\t\t    r_extent_hooks, extents, extent, next, true,\n\t\t\t    growing_retained)) {\n\t\t\t\tif (extents->delay_coalesce) {\n\t\t\t\t\t/* Do minimal coalescing. */\n\t\t\t\t\t*coalesced = true;\n\t\t\t\t\treturn extent;\n\t\t\t\t}\n\t\t\t\tagain = true;\n\t\t\t}\n\t\t}\n\n\t\t/* Try to coalesce backward. */\n\t\textent_t *prev = extent_lock_from_addr(tsdn, rtree_ctx,\n\t\t    extent_before_get(extent));\n\t\tif (prev != NULL) {\n\t\t\tbool can_coalesce = extent_can_coalesce(arena, extents,\n\t\t\t    extent, prev);\n\t\t\textent_unlock(tsdn, prev);\n\n\t\t\tif (can_coalesce && !extent_coalesce(tsdn, arena,\n\t\t\t    r_extent_hooks, extents, extent, prev, false,\n\t\t\t    growing_retained)) {\n\t\t\t\textent = prev;\n\t\t\t\tif (extents->delay_coalesce) {\n\t\t\t\t\t/* Do minimal coalescing. */\n\t\t\t\t\t*coalesced = true;\n\t\t\t\t\treturn extent;\n\t\t\t\t}\n\t\t\t\tagain = true;\n\t\t\t}\n\t\t}\n\t} while (again);\n\n\tif (extents->delay_coalesce) {\n\t\t*coalesced = false;\n\t}\n\treturn extent;\n}\n\nstatic void\nextent_record(tsdn_t *tsdn, arena_t *arena, extent_hooks_t **r_extent_hooks,\n    extents_t *extents, extent_t *extent, bool growing_retained) {\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\n\tassert((extents_state_get(extents) != extent_state_dirty &&\n\t    extents_state_get(extents) != extent_state_muzzy) ||\n\t    !extent_zeroed_get(extent));\n\n\tmalloc_mutex_lock(tsdn, &extents->mtx);\n\textent_hooks_assure_initialized(arena, r_extent_hooks);\n\n\textent_szind_set(extent, NSIZES);\n\tif (extent_slab_get(extent)) {\n\t\textent_interior_deregister(tsdn, rtree_ctx, extent);\n\t\textent_slab_set(extent, false);\n\t}\n\n\tassert(rtree_extent_read(tsdn, &extents_rtree, rtree_ctx,\n\t    (uintptr_t)extent_base_get(extent), true) == extent);\n\n\tif (!extents->delay_coalesce) {\n\t\textent = extent_try_coalesce(tsdn, arena, r_extent_hooks,\n\t\t    rtree_ctx, extents, extent, NULL, growing_retained);\n\t}\n\n\textent_deactivate_locked(tsdn, arena, extents, extent, false);\n\n\tmalloc_mutex_unlock(tsdn, &extents->mtx);\n}\n\nvoid\nextent_dalloc_gap(tsdn_t *tsdn, arena_t *arena, extent_t *extent) {\n\textent_hooks_t *extent_hooks = EXTENT_HOOKS_INITIALIZER;\n\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\tif (extent_register(tsdn, extent)) {\n\t\textents_leak(tsdn, arena, &extent_hooks,\n\t\t    &arena->extents_retained, extent, false);\n\t\treturn;\n\t}\n\textent_dalloc_wrapper(tsdn, arena, &extent_hooks, extent);\n}\n\nstatic bool\nextent_dalloc_default_impl(void *addr, size_t size) {\n\tif (!have_dss || !extent_in_dss(addr)) {\n\t\treturn extent_dalloc_mmap(addr, size);\n\t}\n\treturn true;\n}\n\nstatic bool\nextent_dalloc_default(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    bool committed, unsigned arena_ind) {\n\treturn extent_dalloc_default_impl(addr, size);\n}\n\nstatic bool\nextent_dalloc_wrapper_try(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent) {\n\tbool err;\n\n\tassert(extent_base_get(extent) != NULL);\n\tassert(extent_size_get(extent) != 0);\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\textent_addr_set(extent, extent_base_get(extent));\n\n\textent_hooks_assure_initialized(arena, r_extent_hooks);\n\t/* Try to deallocate. */\n\tif (*r_extent_hooks == &extent_hooks_default) {\n\t\t/* Call directly to propagate tsdn. */\n\t\terr = extent_dalloc_default_impl(extent_base_get(extent),\n\t\t    extent_size_get(extent));\n\t} else {\n\t\terr = ((*r_extent_hooks)->dalloc == NULL ||\n\t\t    (*r_extent_hooks)->dalloc(*r_extent_hooks,\n\t\t    extent_base_get(extent), extent_size_get(extent),\n\t\t    extent_committed_get(extent), arena_ind_get(arena)));\n\t}\n\n\tif (!err) {\n\t\textent_dalloc(tsdn, arena, extent);\n\t}\n\n\treturn err;\n}\n\nvoid\nextent_dalloc_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\t/*\n\t * Deregister first to avoid a race with other allocating threads, and\n\t * reregister if deallocation fails.\n\t */\n\textent_deregister(tsdn, extent);\n\tif (!extent_dalloc_wrapper_try(tsdn, arena, r_extent_hooks, extent)) {\n\t\treturn;\n\t}\n\n\textent_reregister(tsdn, extent);\n\t/* Try to decommit; purge if that fails. */\n\tbool zeroed;\n\tif (!extent_committed_get(extent)) {\n\t\tzeroed = true;\n\t} else if (!extent_decommit_wrapper(tsdn, arena, r_extent_hooks, extent,\n\t    0, extent_size_get(extent))) {\n\t\tzeroed = true;\n\t} else if ((*r_extent_hooks)->purge_forced != NULL &&\n\t    !(*r_extent_hooks)->purge_forced(*r_extent_hooks,\n\t    extent_base_get(extent), extent_size_get(extent), 0,\n\t    extent_size_get(extent), arena_ind_get(arena))) {\n\t\tzeroed = true;\n\t} else if (extent_state_get(extent) == extent_state_muzzy ||\n\t    ((*r_extent_hooks)->purge_lazy != NULL &&\n\t    !(*r_extent_hooks)->purge_lazy(*r_extent_hooks,\n\t    extent_base_get(extent), extent_size_get(extent), 0,\n\t    extent_size_get(extent), arena_ind_get(arena)))) {\n\t\tzeroed = false;\n\t} else {\n\t\tzeroed = false;\n\t}\n\textent_zeroed_set(extent, zeroed);\n\n\tif (config_prof) {\n\t\textent_gdump_sub(tsdn, extent);\n\t}\n\n\textent_record(tsdn, arena, r_extent_hooks, &arena->extents_retained,\n\t    extent, false);\n}\n\nstatic void\nextent_destroy_default_impl(void *addr, size_t size) {\n\tif (!have_dss || !extent_in_dss(addr)) {\n\t\tpages_unmap(addr, size);\n\t}\n}\n\nstatic void\nextent_destroy_default(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    bool committed, unsigned arena_ind) {\n\textent_destroy_default_impl(addr, size);\n}\n\nvoid\nextent_destroy_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent) {\n\tassert(extent_base_get(extent) != NULL);\n\tassert(extent_size_get(extent) != 0);\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\t/* Deregister first to avoid a race with other allocating threads. */\n\textent_deregister(tsdn, extent);\n\n\textent_addr_set(extent, extent_base_get(extent));\n\n\textent_hooks_assure_initialized(arena, r_extent_hooks);\n\t/* Try to destroy; silently fail otherwise. */\n\tif (*r_extent_hooks == &extent_hooks_default) {\n\t\t/* Call directly to propagate tsdn. */\n\t\textent_destroy_default_impl(extent_base_get(extent),\n\t\t    extent_size_get(extent));\n\t} else if ((*r_extent_hooks)->destroy != NULL) {\n\t\t(*r_extent_hooks)->destroy(*r_extent_hooks,\n\t\t    extent_base_get(extent), extent_size_get(extent),\n\t\t    extent_committed_get(extent), arena_ind_get(arena));\n\t}\n\n\textent_dalloc(tsdn, arena, extent);\n}\n\nstatic bool\nextent_commit_default(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    size_t offset, size_t length, unsigned arena_ind) {\n\treturn pages_commit((void *)((uintptr_t)addr + (uintptr_t)offset),\n\t    length);\n}\n\nstatic bool\nextent_commit_impl(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length, bool growing_retained) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, growing_retained ? 1 : 0);\n\n\textent_hooks_assure_initialized(arena, r_extent_hooks);\n\tbool err = ((*r_extent_hooks)->commit == NULL ||\n\t    (*r_extent_hooks)->commit(*r_extent_hooks, extent_base_get(extent),\n\t    extent_size_get(extent), offset, length, arena_ind_get(arena)));\n\textent_committed_set(extent, extent_committed_get(extent) || !err);\n\treturn err;\n}\n\nbool\nextent_commit_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length) {\n\treturn extent_commit_impl(tsdn, arena, r_extent_hooks, extent, offset,\n\t    length, false);\n}\n\nstatic bool\nextent_decommit_default(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    size_t offset, size_t length, unsigned arena_ind) {\n\treturn pages_decommit((void *)((uintptr_t)addr + (uintptr_t)offset),\n\t    length);\n}\n\nbool\nextent_decommit_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, 0);\n\n\textent_hooks_assure_initialized(arena, r_extent_hooks);\n\n\tbool err = ((*r_extent_hooks)->decommit == NULL ||\n\t    (*r_extent_hooks)->decommit(*r_extent_hooks,\n\t    extent_base_get(extent), extent_size_get(extent), offset, length,\n\t    arena_ind_get(arena)));\n\textent_committed_set(extent, extent_committed_get(extent) && err);\n\treturn err;\n}\n\n#ifdef PAGES_CAN_PURGE_LAZY\nstatic bool\nextent_purge_lazy_default(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    size_t offset, size_t length, unsigned arena_ind) {\n\tassert(addr != NULL);\n\tassert((offset & PAGE_MASK) == 0);\n\tassert(length != 0);\n\tassert((length & PAGE_MASK) == 0);\n\n\treturn pages_purge_lazy((void *)((uintptr_t)addr + (uintptr_t)offset),\n\t    length);\n}\n#endif\n\nstatic bool\nextent_purge_lazy_impl(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length, bool growing_retained) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, growing_retained ? 1 : 0);\n\n\textent_hooks_assure_initialized(arena, r_extent_hooks);\n\treturn ((*r_extent_hooks)->purge_lazy == NULL ||\n\t    (*r_extent_hooks)->purge_lazy(*r_extent_hooks,\n\t    extent_base_get(extent), extent_size_get(extent), offset, length,\n\t    arena_ind_get(arena)));\n}\n\nbool\nextent_purge_lazy_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length) {\n\treturn extent_purge_lazy_impl(tsdn, arena, r_extent_hooks, extent,\n\t    offset, length, false);\n}\n\n#ifdef PAGES_CAN_PURGE_FORCED\nstatic bool\nextent_purge_forced_default(extent_hooks_t *extent_hooks, void *addr,\n    size_t size, size_t offset, size_t length, unsigned arena_ind) {\n\tassert(addr != NULL);\n\tassert((offset & PAGE_MASK) == 0);\n\tassert(length != 0);\n\tassert((length & PAGE_MASK) == 0);\n\n\treturn pages_purge_forced((void *)((uintptr_t)addr +\n\t    (uintptr_t)offset), length);\n}\n#endif\n\nstatic bool\nextent_purge_forced_impl(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length, bool growing_retained) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, growing_retained ? 1 : 0);\n\n\textent_hooks_assure_initialized(arena, r_extent_hooks);\n\treturn ((*r_extent_hooks)->purge_forced == NULL ||\n\t    (*r_extent_hooks)->purge_forced(*r_extent_hooks,\n\t    extent_base_get(extent), extent_size_get(extent), offset, length,\n\t    arena_ind_get(arena)));\n}\n\nbool\nextent_purge_forced_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t offset,\n    size_t length) {\n\treturn extent_purge_forced_impl(tsdn, arena, r_extent_hooks, extent,\n\t    offset, length, false);\n}\n\n#ifdef JEMALLOC_MAPS_COALESCE\nstatic bool\nextent_split_default(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    size_t size_a, size_t size_b, bool committed, unsigned arena_ind) {\n\treturn !maps_coalesce;\n}\n#endif\n\nstatic extent_t *\nextent_split_impl(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t size_a,\n    szind_t szind_a, bool slab_a, size_t size_b, szind_t szind_b, bool slab_b,\n    bool growing_retained) {\n\tassert(extent_size_get(extent) == size_a + size_b);\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, growing_retained ? 1 : 0);\n\n\textent_hooks_assure_initialized(arena, r_extent_hooks);\n\n\tif ((*r_extent_hooks)->split == NULL) {\n\t\treturn NULL;\n\t}\n\n\textent_t *trail = extent_alloc(tsdn, arena);\n\tif (trail == NULL) {\n\t\tgoto label_error_a;\n\t}\n\n\textent_init(trail, arena, (void *)((uintptr_t)extent_base_get(extent) +\n\t    size_a), size_b, slab_b, szind_b, extent_sn_get(extent),\n\t    extent_state_get(extent), extent_zeroed_get(extent),\n\t    extent_committed_get(extent));\n\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\trtree_leaf_elm_t *lead_elm_a, *lead_elm_b;\n\t{\n\t\textent_t lead;\n\n\t\textent_init(&lead, arena, extent_addr_get(extent), size_a,\n\t\t    slab_a, szind_a, extent_sn_get(extent),\n\t\t    extent_state_get(extent), extent_zeroed_get(extent),\n\t\t    extent_committed_get(extent));\n\n\t\textent_rtree_leaf_elms_lookup(tsdn, rtree_ctx, &lead, false,\n\t\t    true, &lead_elm_a, &lead_elm_b);\n\t}\n\trtree_leaf_elm_t *trail_elm_a, *trail_elm_b;\n\textent_rtree_leaf_elms_lookup(tsdn, rtree_ctx, trail, false, true,\n\t    &trail_elm_a, &trail_elm_b);\n\n\tif (lead_elm_a == NULL || lead_elm_b == NULL || trail_elm_a == NULL\n\t    || trail_elm_b == NULL) {\n\t\tgoto label_error_b;\n\t}\n\n\textent_lock2(tsdn, extent, trail);\n\n\tif ((*r_extent_hooks)->split(*r_extent_hooks, extent_base_get(extent),\n\t    size_a + size_b, size_a, size_b, extent_committed_get(extent),\n\t    arena_ind_get(arena))) {\n\t\tgoto label_error_c;\n\t}\n\n\textent_size_set(extent, size_a);\n\textent_szind_set(extent, szind_a);\n\n\textent_rtree_write_acquired(tsdn, lead_elm_a, lead_elm_b, extent,\n\t    szind_a, slab_a);\n\textent_rtree_write_acquired(tsdn, trail_elm_a, trail_elm_b, trail,\n\t    szind_b, slab_b);\n\n\textent_unlock2(tsdn, extent, trail);\n\n\treturn trail;\nlabel_error_c:\n\textent_unlock2(tsdn, extent, trail);\nlabel_error_b:\n\textent_dalloc(tsdn, arena, trail);\nlabel_error_a:\n\treturn NULL;\n}\n\nextent_t *\nextent_split_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *extent, size_t size_a,\n    szind_t szind_a, bool slab_a, size_t size_b, szind_t szind_b, bool slab_b) {\n\treturn extent_split_impl(tsdn, arena, r_extent_hooks, extent, size_a,\n\t    szind_a, slab_a, size_b, szind_b, slab_b, false);\n}\n\nstatic bool\nextent_merge_default_impl(void *addr_a, void *addr_b) {\n\tif (!maps_coalesce) {\n\t\treturn true;\n\t}\n\tif (have_dss && !extent_dss_mergeable(addr_a, addr_b)) {\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\n#ifdef JEMALLOC_MAPS_COALESCE\nstatic bool\nextent_merge_default(extent_hooks_t *extent_hooks, void *addr_a, size_t size_a,\n    void *addr_b, size_t size_b, bool committed, unsigned arena_ind) {\n\treturn extent_merge_default_impl(addr_a, addr_b);\n}\n#endif\n\nstatic bool\nextent_merge_impl(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *a, extent_t *b,\n    bool growing_retained) {\n\twitness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),\n\t    WITNESS_RANK_CORE, growing_retained ? 1 : 0);\n\n\textent_hooks_assure_initialized(arena, r_extent_hooks);\n\n\tif ((*r_extent_hooks)->merge == NULL) {\n\t\treturn true;\n\t}\n\n\tbool err;\n\tif (*r_extent_hooks == &extent_hooks_default) {\n\t\t/* Call directly to propagate tsdn. */\n\t\terr = extent_merge_default_impl(extent_base_get(a),\n\t\t    extent_base_get(b));\n\t} else {\n\t\terr = (*r_extent_hooks)->merge(*r_extent_hooks,\n\t\t    extent_base_get(a), extent_size_get(a), extent_base_get(b),\n\t\t    extent_size_get(b), extent_committed_get(a),\n\t\t    arena_ind_get(arena));\n\t}\n\n\tif (err) {\n\t\treturn true;\n\t}\n\n\t/*\n\t * The rtree writes must happen while all the relevant elements are\n\t * owned, so the following code uses decomposed helper functions rather\n\t * than extent_{,de}register() to do things in the right order.\n\t */\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\trtree_leaf_elm_t *a_elm_a, *a_elm_b, *b_elm_a, *b_elm_b;\n\textent_rtree_leaf_elms_lookup(tsdn, rtree_ctx, a, true, false, &a_elm_a,\n\t    &a_elm_b);\n\textent_rtree_leaf_elms_lookup(tsdn, rtree_ctx, b, true, false, &b_elm_a,\n\t    &b_elm_b);\n\n\textent_lock2(tsdn, a, b);\n\n\tif (a_elm_b != NULL) {\n\t\trtree_leaf_elm_write(tsdn, &extents_rtree, a_elm_b, NULL,\n\t\t    NSIZES, false);\n\t}\n\tif (b_elm_b != NULL) {\n\t\trtree_leaf_elm_write(tsdn, &extents_rtree, b_elm_a, NULL,\n\t\t    NSIZES, false);\n\t} else {\n\t\tb_elm_b = b_elm_a;\n\t}\n\n\textent_size_set(a, extent_size_get(a) + extent_size_get(b));\n\textent_szind_set(a, NSIZES);\n\textent_sn_set(a, (extent_sn_get(a) < extent_sn_get(b)) ?\n\t    extent_sn_get(a) : extent_sn_get(b));\n\textent_zeroed_set(a, extent_zeroed_get(a) && extent_zeroed_get(b));\n\n\textent_rtree_write_acquired(tsdn, a_elm_a, b_elm_b, a, NSIZES, false);\n\n\textent_unlock2(tsdn, a, b);\n\n\textent_dalloc(tsdn, extent_arena_get(b), b);\n\n\treturn false;\n}\n\nbool\nextent_merge_wrapper(tsdn_t *tsdn, arena_t *arena,\n    extent_hooks_t **r_extent_hooks, extent_t *a, extent_t *b) {\n\treturn extent_merge_impl(tsdn, arena, r_extent_hooks, a, b, false);\n}\n\nbool\nextent_boot(void) {\n\tif (rtree_new(&extents_rtree, true)) {\n\t\treturn true;\n\t}\n\n\tif (mutex_pool_init(&extent_mutex_pool, \"extent_mutex_pool\",\n\t    WITNESS_RANK_EXTENT_POOL)) {\n\t\treturn true;\n\t}\n\n\tif (have_dss) {\n\t\textent_dss_boot();\n\t}\n\n\treturn false;\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/extent_dss.c",
    "content": "#define JEMALLOC_EXTENT_DSS_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/extent_dss.h\"\n#include \"jemalloc/internal/spin.h\"\n\n/******************************************************************************/\n/* Data. */\n\nconst char\t*opt_dss = DSS_DEFAULT;\n\nconst char\t*dss_prec_names[] = {\n\t\"disabled\",\n\t\"primary\",\n\t\"secondary\",\n\t\"N/A\"\n};\n\n/*\n * Current dss precedence default, used when creating new arenas.  NB: This is\n * stored as unsigned rather than dss_prec_t because in principle there's no\n * guarantee that sizeof(dss_prec_t) is the same as sizeof(unsigned), and we use\n * atomic operations to synchronize the setting.\n */\nstatic atomic_u_t\tdss_prec_default = ATOMIC_INIT(\n    (unsigned)DSS_PREC_DEFAULT);\n\n/* Base address of the DSS. */\nstatic void\t\t*dss_base;\n/* Atomic boolean indicating whether a thread is currently extending DSS. */\nstatic atomic_b_t\tdss_extending;\n/* Atomic boolean indicating whether the DSS is exhausted. */\nstatic atomic_b_t\tdss_exhausted;\n/* Atomic current upper limit on DSS addresses. */\nstatic atomic_p_t\tdss_max;\n\n/******************************************************************************/\n\nstatic void *\nextent_dss_sbrk(intptr_t increment) {\n#ifdef JEMALLOC_DSS\n\treturn sbrk(increment);\n#else\n\tnot_implemented();\n\treturn NULL;\n#endif\n}\n\ndss_prec_t\nextent_dss_prec_get(void) {\n\tdss_prec_t ret;\n\n\tif (!have_dss) {\n\t\treturn dss_prec_disabled;\n\t}\n\tret = (dss_prec_t)atomic_load_u(&dss_prec_default, ATOMIC_ACQUIRE);\n\treturn ret;\n}\n\nbool\nextent_dss_prec_set(dss_prec_t dss_prec) {\n\tif (!have_dss) {\n\t\treturn (dss_prec != dss_prec_disabled);\n\t}\n\tatomic_store_u(&dss_prec_default, (unsigned)dss_prec, ATOMIC_RELEASE);\n\treturn false;\n}\n\nstatic void\nextent_dss_extending_start(void) {\n\tspin_t spinner = SPIN_INITIALIZER;\n\twhile (true) {\n\t\tbool expected = false;\n\t\tif (atomic_compare_exchange_weak_b(&dss_extending, &expected,\n\t\t    true, ATOMIC_ACQ_REL, ATOMIC_RELAXED)) {\n\t\t\tbreak;\n\t\t}\n\t\tspin_adaptive(&spinner);\n\t}\n}\n\nstatic void\nextent_dss_extending_finish(void) {\n\tassert(atomic_load_b(&dss_extending, ATOMIC_RELAXED));\n\n\tatomic_store_b(&dss_extending, false, ATOMIC_RELEASE);\n}\n\nstatic void *\nextent_dss_max_update(void *new_addr) {\n\t/*\n\t * Get the current end of the DSS as max_cur and assure that dss_max is\n\t * up to date.\n\t */\n\tvoid *max_cur = extent_dss_sbrk(0);\n\tif (max_cur == (void *)-1) {\n\t\treturn NULL;\n\t}\n\tatomic_store_p(&dss_max, max_cur, ATOMIC_RELEASE);\n\t/* Fixed new_addr can only be supported if it is at the edge of DSS. */\n\tif (new_addr != NULL && max_cur != new_addr) {\n\t\treturn NULL;\n\t}\n\treturn max_cur;\n}\n\nvoid *\nextent_alloc_dss(tsdn_t *tsdn, arena_t *arena, void *new_addr, size_t size,\n    size_t alignment, bool *zero, bool *commit) {\n\textent_t *gap;\n\n\tcassert(have_dss);\n\tassert(size > 0);\n\tassert(alignment > 0);\n\n\t/*\n\t * sbrk() uses a signed increment argument, so take care not to\n\t * interpret a large allocation request as a negative increment.\n\t */\n\tif ((intptr_t)size < 0) {\n\t\treturn NULL;\n\t}\n\n\tgap = extent_alloc(tsdn, arena);\n\tif (gap == NULL) {\n\t\treturn NULL;\n\t}\n\n\textent_dss_extending_start();\n\tif (!atomic_load_b(&dss_exhausted, ATOMIC_ACQUIRE)) {\n\t\t/*\n\t\t * The loop is necessary to recover from races with other\n\t\t * threads that are using the DSS for something other than\n\t\t * malloc.\n\t\t */\n\t\twhile (true) {\n\t\t\tvoid *max_cur = extent_dss_max_update(new_addr);\n\t\t\tif (max_cur == NULL) {\n\t\t\t\tgoto label_oom;\n\t\t\t}\n\n\t\t\t/*\n\t\t\t * Compute how much page-aligned gap space (if any) is\n\t\t\t * necessary to satisfy alignment.  This space can be\n\t\t\t * recycled for later use.\n\t\t\t */\n\t\t\tvoid *gap_addr_page = (void *)(PAGE_CEILING(\n\t\t\t    (uintptr_t)max_cur));\n\t\t\tvoid *ret = (void *)ALIGNMENT_CEILING(\n\t\t\t    (uintptr_t)gap_addr_page, alignment);\n\t\t\tsize_t gap_size_page = (uintptr_t)ret -\n\t\t\t    (uintptr_t)gap_addr_page;\n\t\t\tif (gap_size_page != 0) {\n\t\t\t\textent_init(gap, arena, gap_addr_page,\n\t\t\t\t    gap_size_page, false, NSIZES,\n\t\t\t\t    arena_extent_sn_next(arena),\n\t\t\t\t    extent_state_active, false, true);\n\t\t\t}\n\t\t\t/*\n\t\t\t * Compute the address just past the end of the desired\n\t\t\t * allocation space.\n\t\t\t */\n\t\t\tvoid *dss_next = (void *)((uintptr_t)ret + size);\n\t\t\tif ((uintptr_t)ret < (uintptr_t)max_cur ||\n\t\t\t    (uintptr_t)dss_next < (uintptr_t)max_cur) {\n\t\t\t\tgoto label_oom; /* Wrap-around. */\n\t\t\t}\n\t\t\t/* Compute the increment, including subpage bytes. */\n\t\t\tvoid *gap_addr_subpage = max_cur;\n\t\t\tsize_t gap_size_subpage = (uintptr_t)ret -\n\t\t\t    (uintptr_t)gap_addr_subpage;\n\t\t\tintptr_t incr = gap_size_subpage + size;\n\n\t\t\tassert((uintptr_t)max_cur + incr == (uintptr_t)ret +\n\t\t\t    size);\n\n\t\t\t/* Try to allocate. */\n\t\t\tvoid *dss_prev = extent_dss_sbrk(incr);\n\t\t\tif (dss_prev == max_cur) {\n\t\t\t\t/* Success. */\n\t\t\t\tatomic_store_p(&dss_max, dss_next,\n\t\t\t\t    ATOMIC_RELEASE);\n\t\t\t\textent_dss_extending_finish();\n\n\t\t\t\tif (gap_size_page != 0) {\n\t\t\t\t\textent_dalloc_gap(tsdn, arena, gap);\n\t\t\t\t} else {\n\t\t\t\t\textent_dalloc(tsdn, arena, gap);\n\t\t\t\t}\n\t\t\t\tif (!*commit) {\n\t\t\t\t\t*commit = pages_decommit(ret, size);\n\t\t\t\t}\n\t\t\t\tif (*zero && *commit) {\n\t\t\t\t\textent_hooks_t *extent_hooks =\n\t\t\t\t\t    EXTENT_HOOKS_INITIALIZER;\n\t\t\t\t\textent_t extent;\n\n\t\t\t\t\textent_init(&extent, arena, ret, size,\n\t\t\t\t\t    size, false, NSIZES,\n\t\t\t\t\t    extent_state_active, false, true);\n\t\t\t\t\tif (extent_purge_forced_wrapper(tsdn,\n\t\t\t\t\t    arena, &extent_hooks, &extent, 0,\n\t\t\t\t\t    size)) {\n\t\t\t\t\t\tmemset(ret, 0, size);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\treturn ret;\n\t\t\t}\n\t\t\t/*\n\t\t\t * Failure, whether due to OOM or a race with a raw\n\t\t\t * sbrk() call from outside the allocator.\n\t\t\t */\n\t\t\tif (dss_prev == (void *)-1) {\n\t\t\t\t/* OOM. */\n\t\t\t\tatomic_store_b(&dss_exhausted, true,\n\t\t\t\t    ATOMIC_RELEASE);\n\t\t\t\tgoto label_oom;\n\t\t\t}\n\t\t}\n\t}\nlabel_oom:\n\textent_dss_extending_finish();\n\textent_dalloc(tsdn, arena, gap);\n\treturn NULL;\n}\n\nstatic bool\nextent_in_dss_helper(void *addr, void *max) {\n\treturn ((uintptr_t)addr >= (uintptr_t)dss_base && (uintptr_t)addr <\n\t    (uintptr_t)max);\n}\n\nbool\nextent_in_dss(void *addr) {\n\tcassert(have_dss);\n\n\treturn extent_in_dss_helper(addr, atomic_load_p(&dss_max,\n\t    ATOMIC_ACQUIRE));\n}\n\nbool\nextent_dss_mergeable(void *addr_a, void *addr_b) {\n\tvoid *max;\n\n\tcassert(have_dss);\n\n\tif ((uintptr_t)addr_a < (uintptr_t)dss_base && (uintptr_t)addr_b <\n\t    (uintptr_t)dss_base) {\n\t\treturn true;\n\t}\n\n\tmax = atomic_load_p(&dss_max, ATOMIC_ACQUIRE);\n\treturn (extent_in_dss_helper(addr_a, max) ==\n\t    extent_in_dss_helper(addr_b, max));\n}\n\nvoid\nextent_dss_boot(void) {\n\tcassert(have_dss);\n\n\tdss_base = extent_dss_sbrk(0);\n\tatomic_store_b(&dss_extending, false, ATOMIC_RELAXED);\n\tatomic_store_b(&dss_exhausted, dss_base == (void *)-1, ATOMIC_RELAXED);\n\tatomic_store_p(&dss_max, dss_base, ATOMIC_RELAXED);\n}\n\n/******************************************************************************/\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/extent_mmap.c",
    "content": "#define JEMALLOC_EXTENT_MMAP_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/extent_mmap.h\"\n\n/******************************************************************************/\n/* Data. */\n\nbool\topt_retain =\n#ifdef JEMALLOC_RETAIN\n    true\n#else\n    false\n#endif\n    ;\n\n/******************************************************************************/\n\nvoid *\nextent_alloc_mmap(void *new_addr, size_t size, size_t alignment, bool *zero,\n    bool *commit) {\n\tvoid *ret = pages_map(new_addr, size, ALIGNMENT_CEILING(alignment,\n\t    PAGE), commit);\n\tif (ret == NULL) {\n\t\treturn NULL;\n\t}\n\tassert(ret != NULL);\n\tif (*commit) {\n\t\t*zero = true;\n\t}\n\treturn ret;\n}\n\nbool\nextent_dalloc_mmap(void *addr, size_t size) {\n\tif (!opt_retain) {\n\t\tpages_unmap(addr, size);\n\t}\n\treturn opt_retain;\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/hash.c",
    "content": "#define JEMALLOC_HASH_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/hooks.c",
    "content": "#include \"jemalloc/internal/jemalloc_preamble.h\"\n\n/*\n * The hooks are a little bit screwy -- they're not genuinely exported in the\n * sense that we want them available to end-users, but we do want them visible\n * from outside the generated library, so that we can use them in test code.\n */\nJEMALLOC_EXPORT\nvoid (*hooks_arena_new_hook)() = NULL;\n\nJEMALLOC_EXPORT\nvoid (*hooks_libc_hook)() = NULL;\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/jemalloc.c",
    "content": "#define JEMALLOC_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/atomic.h\"\n#include \"jemalloc/internal/ctl.h\"\n#include \"jemalloc/internal/extent_dss.h\"\n#include \"jemalloc/internal/extent_mmap.h\"\n#include \"jemalloc/internal/jemalloc_internal_types.h\"\n#include \"jemalloc/internal/malloc_io.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/rtree.h\"\n#include \"jemalloc/internal/size_classes.h\"\n#include \"jemalloc/internal/spin.h\"\n#include \"jemalloc/internal/sz.h\"\n#include \"jemalloc/internal/ticker.h\"\n#include \"jemalloc/internal/util.h\"\n\n/******************************************************************************/\n/* Data. */\n\n/* Runtime configuration options. */\nconst char\t*je_malloc_conf\n#ifndef _WIN32\n    JEMALLOC_ATTR(weak)\n#endif\n    ;\nbool\topt_abort =\n#ifdef JEMALLOC_DEBUG\n    true\n#else\n    false\n#endif\n    ;\nbool\topt_abort_conf =\n#ifdef JEMALLOC_DEBUG\n    true\n#else\n    false\n#endif\n    ;\nconst char\t*opt_junk =\n#if (defined(JEMALLOC_DEBUG) && defined(JEMALLOC_FILL))\n    \"true\"\n#else\n    \"false\"\n#endif\n    ;\nbool\topt_junk_alloc =\n#if (defined(JEMALLOC_DEBUG) && defined(JEMALLOC_FILL))\n    true\n#else\n    false\n#endif\n    ;\nbool\topt_junk_free =\n#if (defined(JEMALLOC_DEBUG) && defined(JEMALLOC_FILL))\n    true\n#else\n    false\n#endif\n    ;\n\nbool\topt_utrace = false;\nbool\topt_xmalloc = false;\nbool\topt_zero = false;\nunsigned\topt_narenas = 0;\n\nunsigned\tncpus;\n\n/* Protects arenas initialization. */\nmalloc_mutex_t arenas_lock;\n/*\n * Arenas that are used to service external requests.  Not all elements of the\n * arenas array are necessarily used; arenas are created lazily as needed.\n *\n * arenas[0..narenas_auto) are used for automatic multiplexing of threads and\n * arenas.  arenas[narenas_auto..narenas_total) are only used if the application\n * takes some action to create them and allocate from them.\n *\n * Points to an arena_t.\n */\nJEMALLOC_ALIGNED(CACHELINE)\natomic_p_t\t\tarenas[MALLOCX_ARENA_LIMIT];\nstatic atomic_u_t\tnarenas_total; /* Use narenas_total_*(). */\nstatic arena_t\t\t*a0; /* arenas[0]; read-only after initialization. */\nunsigned\t\tnarenas_auto; /* Read-only after initialization. */\n\ntypedef enum {\n\tmalloc_init_uninitialized\t= 3,\n\tmalloc_init_a0_initialized\t= 2,\n\tmalloc_init_recursible\t\t= 1,\n\tmalloc_init_initialized\t\t= 0 /* Common case --> jnz. */\n} malloc_init_t;\nstatic malloc_init_t\tmalloc_init_state = malloc_init_uninitialized;\n\n/* False should be the common case.  Set to true to trigger initialization. */\nbool\t\t\tmalloc_slow = true;\n\n/* When malloc_slow is true, set the corresponding bits for sanity check. */\nenum {\n\tflag_opt_junk_alloc\t= (1U),\n\tflag_opt_junk_free\t= (1U << 1),\n\tflag_opt_zero\t\t= (1U << 2),\n\tflag_opt_utrace\t\t= (1U << 3),\n\tflag_opt_xmalloc\t= (1U << 4)\n};\nstatic uint8_t\tmalloc_slow_flags;\n\n#ifdef JEMALLOC_THREADED_INIT\n/* Used to let the initializing thread recursively allocate. */\n#  define NO_INITIALIZER\t((unsigned long)0)\n#  define INITIALIZER\t\tpthread_self()\n#  define IS_INITIALIZER\t(malloc_initializer == pthread_self())\nstatic pthread_t\t\tmalloc_initializer = NO_INITIALIZER;\n#else\n#  define NO_INITIALIZER\tfalse\n#  define INITIALIZER\t\ttrue\n#  define IS_INITIALIZER\tmalloc_initializer\nstatic bool\t\t\tmalloc_initializer = NO_INITIALIZER;\n#endif\n\n/* Used to avoid initialization races. */\n#ifdef _WIN32\n#if _WIN32_WINNT >= 0x0600\nstatic malloc_mutex_t\tinit_lock = SRWLOCK_INIT;\n#else\nstatic malloc_mutex_t\tinit_lock;\nstatic bool init_lock_initialized = false;\n\nJEMALLOC_ATTR(constructor)\nstatic void WINAPI\n_init_init_lock(void) {\n\t/*\n\t * If another constructor in the same binary is using mallctl to e.g.\n\t * set up extent hooks, it may end up running before this one, and\n\t * malloc_init_hard will crash trying to lock the uninitialized lock. So\n\t * we force an initialization of the lock in malloc_init_hard as well.\n\t * We don't try to care about atomicity of the accessed to the\n\t * init_lock_initialized boolean, since it really only matters early in\n\t * the process creation, before any separate thread normally starts\n\t * doing anything.\n\t */\n\tif (!init_lock_initialized) {\n\t\tmalloc_mutex_init(&init_lock, \"init\", WITNESS_RANK_INIT,\n\t\t    malloc_mutex_rank_exclusive);\n\t}\n\tinit_lock_initialized = true;\n}\n\n#ifdef _MSC_VER\n#  pragma section(\".CRT$XCU\", read)\nJEMALLOC_SECTION(\".CRT$XCU\") JEMALLOC_ATTR(used)\nstatic const void (WINAPI *init_init_lock)(void) = _init_init_lock;\n#endif\n#endif\n#else\nstatic malloc_mutex_t\tinit_lock = MALLOC_MUTEX_INITIALIZER;\n#endif\n\ntypedef struct {\n\tvoid\t*p;\t/* Input pointer (as in realloc(p, s)). */\n\tsize_t\ts;\t/* Request size. */\n\tvoid\t*r;\t/* Result pointer. */\n} malloc_utrace_t;\n\n#ifdef JEMALLOC_UTRACE\n#  define UTRACE(a, b, c) do {\t\t\t\t\t\t\\\n\tif (unlikely(opt_utrace)) {\t\t\t\t\t\\\n\t\tint utrace_serrno = errno;\t\t\t\t\\\n\t\tmalloc_utrace_t ut;\t\t\t\t\t\\\n\t\tut.p = (a);\t\t\t\t\t\t\\\n\t\tut.s = (b);\t\t\t\t\t\t\\\n\t\tut.r = (c);\t\t\t\t\t\t\\\n\t\tutrace(&ut, sizeof(ut));\t\t\t\t\\\n\t\terrno = utrace_serrno;\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n#else\n#  define UTRACE(a, b, c)\n#endif\n\n/* Whether encountered any invalid config options. */\nstatic bool had_conf_error = false;\n\n/******************************************************************************/\n/*\n * Function prototypes for static functions that are referenced prior to\n * definition.\n */\n\nstatic bool\tmalloc_init_hard_a0(void);\nstatic bool\tmalloc_init_hard(void);\n\n/******************************************************************************/\n/*\n * Begin miscellaneous support functions.\n */\n\nbool\nmalloc_initialized(void) {\n\treturn (malloc_init_state == malloc_init_initialized);\n}\n\nJEMALLOC_ALWAYS_INLINE bool\nmalloc_init_a0(void) {\n\tif (unlikely(malloc_init_state == malloc_init_uninitialized)) {\n\t\treturn malloc_init_hard_a0();\n\t}\n\treturn false;\n}\n\nJEMALLOC_ALWAYS_INLINE bool\nmalloc_init(void) {\n\tif (unlikely(!malloc_initialized()) && malloc_init_hard()) {\n\t\treturn true;\n\t}\n\treturn false;\n}\n\n/*\n * The a0*() functions are used instead of i{d,}alloc() in situations that\n * cannot tolerate TLS variable access.\n */\n\nstatic void *\na0ialloc(size_t size, bool zero, bool is_internal) {\n\tif (unlikely(malloc_init_a0())) {\n\t\treturn NULL;\n\t}\n\n\treturn iallocztm(TSDN_NULL, size, sz_size2index(size), zero, NULL,\n\t    is_internal, arena_get(TSDN_NULL, 0, true), true);\n}\n\nstatic void\na0idalloc(void *ptr, bool is_internal) {\n\tidalloctm(TSDN_NULL, ptr, NULL, NULL, is_internal, true);\n}\n\nvoid *\na0malloc(size_t size) {\n\treturn a0ialloc(size, false, true);\n}\n\nvoid\na0dalloc(void *ptr) {\n\ta0idalloc(ptr, true);\n}\n\n/*\n * FreeBSD's libc uses the bootstrap_*() functions in bootstrap-senstive\n * situations that cannot tolerate TLS variable access (TLS allocation and very\n * early internal data structure initialization).\n */\n\nvoid *\nbootstrap_malloc(size_t size) {\n\tif (unlikely(size == 0)) {\n\t\tsize = 1;\n\t}\n\n\treturn a0ialloc(size, false, false);\n}\n\nvoid *\nbootstrap_calloc(size_t num, size_t size) {\n\tsize_t num_size;\n\n\tnum_size = num * size;\n\tif (unlikely(num_size == 0)) {\n\t\tassert(num == 0 || size == 0);\n\t\tnum_size = 1;\n\t}\n\n\treturn a0ialloc(num_size, true, false);\n}\n\nvoid\nbootstrap_free(void *ptr) {\n\tif (unlikely(ptr == NULL)) {\n\t\treturn;\n\t}\n\n\ta0idalloc(ptr, false);\n}\n\nvoid\narena_set(unsigned ind, arena_t *arena) {\n\tatomic_store_p(&arenas[ind], arena, ATOMIC_RELEASE);\n}\n\nstatic void\nnarenas_total_set(unsigned narenas) {\n\tatomic_store_u(&narenas_total, narenas, ATOMIC_RELEASE);\n}\n\nstatic void\nnarenas_total_inc(void) {\n\tatomic_fetch_add_u(&narenas_total, 1, ATOMIC_RELEASE);\n}\n\nunsigned\nnarenas_total_get(void) {\n\treturn atomic_load_u(&narenas_total, ATOMIC_ACQUIRE);\n}\n\n/* Create a new arena and insert it into the arenas array at index ind. */\nstatic arena_t *\narena_init_locked(tsdn_t *tsdn, unsigned ind, extent_hooks_t *extent_hooks) {\n\tarena_t *arena;\n\n\tassert(ind <= narenas_total_get());\n\tif (ind >= MALLOCX_ARENA_LIMIT) {\n\t\treturn NULL;\n\t}\n\tif (ind == narenas_total_get()) {\n\t\tnarenas_total_inc();\n\t}\n\n\t/*\n\t * Another thread may have already initialized arenas[ind] if it's an\n\t * auto arena.\n\t */\n\tarena = arena_get(tsdn, ind, false);\n\tif (arena != NULL) {\n\t\tassert(ind < narenas_auto);\n\t\treturn arena;\n\t}\n\n\t/* Actually initialize the arena. */\n\tarena = arena_new(tsdn, ind, extent_hooks);\n\n\treturn arena;\n}\n\nstatic void\narena_new_create_background_thread(tsdn_t *tsdn, unsigned ind) {\n\tif (ind == 0) {\n\t\treturn;\n\t}\n\tif (have_background_thread) {\n\t\tbool err;\n\t\tmalloc_mutex_lock(tsdn, &background_thread_lock);\n\t\terr = background_thread_create(tsdn_tsd(tsdn), ind);\n\t\tmalloc_mutex_unlock(tsdn, &background_thread_lock);\n\t\tif (err) {\n\t\t\tmalloc_printf(\"<jemalloc>: error in background thread \"\n\t\t\t\t      \"creation for arena %u. Abort.\\n\", ind);\n\t\t\tabort();\n\t\t}\n\t}\n}\n\narena_t *\narena_init(tsdn_t *tsdn, unsigned ind, extent_hooks_t *extent_hooks) {\n\tarena_t *arena;\n\n\tmalloc_mutex_lock(tsdn, &arenas_lock);\n\tarena = arena_init_locked(tsdn, ind, extent_hooks);\n\tmalloc_mutex_unlock(tsdn, &arenas_lock);\n\n\tarena_new_create_background_thread(tsdn, ind);\n\n\treturn arena;\n}\n\nstatic void\narena_bind(tsd_t *tsd, unsigned ind, bool internal) {\n\tarena_t *arena = arena_get(tsd_tsdn(tsd), ind, false);\n\tarena_nthreads_inc(arena, internal);\n\n\tif (internal) {\n\t\ttsd_iarena_set(tsd, arena);\n\t} else {\n\t\ttsd_arena_set(tsd, arena);\n\t}\n}\n\nvoid\narena_migrate(tsd_t *tsd, unsigned oldind, unsigned newind) {\n\tarena_t *oldarena, *newarena;\n\n\toldarena = arena_get(tsd_tsdn(tsd), oldind, false);\n\tnewarena = arena_get(tsd_tsdn(tsd), newind, false);\n\tarena_nthreads_dec(oldarena, false);\n\tarena_nthreads_inc(newarena, false);\n\ttsd_arena_set(tsd, newarena);\n}\n\nstatic void\narena_unbind(tsd_t *tsd, unsigned ind, bool internal) {\n\tarena_t *arena;\n\n\tarena = arena_get(tsd_tsdn(tsd), ind, false);\n\tarena_nthreads_dec(arena, internal);\n\n\tif (internal) {\n\t\ttsd_iarena_set(tsd, NULL);\n\t} else {\n\t\ttsd_arena_set(tsd, NULL);\n\t}\n}\n\narena_tdata_t *\narena_tdata_get_hard(tsd_t *tsd, unsigned ind) {\n\tarena_tdata_t *tdata, *arenas_tdata_old;\n\tarena_tdata_t *arenas_tdata = tsd_arenas_tdata_get(tsd);\n\tunsigned narenas_tdata_old, i;\n\tunsigned narenas_tdata = tsd_narenas_tdata_get(tsd);\n\tunsigned narenas_actual = narenas_total_get();\n\n\t/*\n\t * Dissociate old tdata array (and set up for deallocation upon return)\n\t * if it's too small.\n\t */\n\tif (arenas_tdata != NULL && narenas_tdata < narenas_actual) {\n\t\tarenas_tdata_old = arenas_tdata;\n\t\tnarenas_tdata_old = narenas_tdata;\n\t\tarenas_tdata = NULL;\n\t\tnarenas_tdata = 0;\n\t\ttsd_arenas_tdata_set(tsd, arenas_tdata);\n\t\ttsd_narenas_tdata_set(tsd, narenas_tdata);\n\t} else {\n\t\tarenas_tdata_old = NULL;\n\t\tnarenas_tdata_old = 0;\n\t}\n\n\t/* Allocate tdata array if it's missing. */\n\tif (arenas_tdata == NULL) {\n\t\tbool *arenas_tdata_bypassp = tsd_arenas_tdata_bypassp_get(tsd);\n\t\tnarenas_tdata = (ind < narenas_actual) ? narenas_actual : ind+1;\n\n\t\tif (tsd_nominal(tsd) && !*arenas_tdata_bypassp) {\n\t\t\t*arenas_tdata_bypassp = true;\n\t\t\tarenas_tdata = (arena_tdata_t *)a0malloc(\n\t\t\t    sizeof(arena_tdata_t) * narenas_tdata);\n\t\t\t*arenas_tdata_bypassp = false;\n\t\t}\n\t\tif (arenas_tdata == NULL) {\n\t\t\ttdata = NULL;\n\t\t\tgoto label_return;\n\t\t}\n\t\tassert(tsd_nominal(tsd) && !*arenas_tdata_bypassp);\n\t\ttsd_arenas_tdata_set(tsd, arenas_tdata);\n\t\ttsd_narenas_tdata_set(tsd, narenas_tdata);\n\t}\n\n\t/*\n\t * Copy to tdata array.  It's possible that the actual number of arenas\n\t * has increased since narenas_total_get() was called above, but that\n\t * causes no correctness issues unless two threads concurrently execute\n\t * the arenas.create mallctl, which we trust mallctl synchronization to\n\t * prevent.\n\t */\n\n\t/* Copy/initialize tickers. */\n\tfor (i = 0; i < narenas_actual; i++) {\n\t\tif (i < narenas_tdata_old) {\n\t\t\tticker_copy(&arenas_tdata[i].decay_ticker,\n\t\t\t    &arenas_tdata_old[i].decay_ticker);\n\t\t} else {\n\t\t\tticker_init(&arenas_tdata[i].decay_ticker,\n\t\t\t    DECAY_NTICKS_PER_UPDATE);\n\t\t}\n\t}\n\tif (narenas_tdata > narenas_actual) {\n\t\tmemset(&arenas_tdata[narenas_actual], 0, sizeof(arena_tdata_t)\n\t\t    * (narenas_tdata - narenas_actual));\n\t}\n\n\t/* Read the refreshed tdata array. */\n\ttdata = &arenas_tdata[ind];\nlabel_return:\n\tif (arenas_tdata_old != NULL) {\n\t\ta0dalloc(arenas_tdata_old);\n\t}\n\treturn tdata;\n}\n\n/* Slow path, called only by arena_choose(). */\narena_t *\narena_choose_hard(tsd_t *tsd, bool internal) {\n\tarena_t *ret JEMALLOC_CC_SILENCE_INIT(NULL);\n\n\tif (have_percpu_arena && PERCPU_ARENA_ENABLED(opt_percpu_arena)) {\n\t\tunsigned choose = percpu_arena_choose();\n\t\tret = arena_get(tsd_tsdn(tsd), choose, true);\n\t\tassert(ret != NULL);\n\t\tarena_bind(tsd, arena_ind_get(ret), false);\n\t\tarena_bind(tsd, arena_ind_get(ret), true);\n\n\t\treturn ret;\n\t}\n\n\tif (narenas_auto > 1) {\n\t\tunsigned i, j, choose[2], first_null;\n\t\tbool is_new_arena[2];\n\n\t\t/*\n\t\t * Determine binding for both non-internal and internal\n\t\t * allocation.\n\t\t *\n\t\t *   choose[0]: For application allocation.\n\t\t *   choose[1]: For internal metadata allocation.\n\t\t */\n\n\t\tfor (j = 0; j < 2; j++) {\n\t\t\tchoose[j] = 0;\n\t\t\tis_new_arena[j] = false;\n\t\t}\n\n\t\tfirst_null = narenas_auto;\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &arenas_lock);\n\t\tassert(arena_get(tsd_tsdn(tsd), 0, false) != NULL);\n\t\tfor (i = 1; i < narenas_auto; i++) {\n\t\t\tif (arena_get(tsd_tsdn(tsd), i, false) != NULL) {\n\t\t\t\t/*\n\t\t\t\t * Choose the first arena that has the lowest\n\t\t\t\t * number of threads assigned to it.\n\t\t\t\t */\n\t\t\t\tfor (j = 0; j < 2; j++) {\n\t\t\t\t\tif (arena_nthreads_get(arena_get(\n\t\t\t\t\t    tsd_tsdn(tsd), i, false), !!j) <\n\t\t\t\t\t    arena_nthreads_get(arena_get(\n\t\t\t\t\t    tsd_tsdn(tsd), choose[j], false),\n\t\t\t\t\t    !!j)) {\n\t\t\t\t\t\tchoose[j] = i;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if (first_null == narenas_auto) {\n\t\t\t\t/*\n\t\t\t\t * Record the index of the first uninitialized\n\t\t\t\t * arena, in case all extant arenas are in use.\n\t\t\t\t *\n\t\t\t\t * NB: It is possible for there to be\n\t\t\t\t * discontinuities in terms of initialized\n\t\t\t\t * versus uninitialized arenas, due to the\n\t\t\t\t * \"thread.arena\" mallctl.\n\t\t\t\t */\n\t\t\t\tfirst_null = i;\n\t\t\t}\n\t\t}\n\n\t\tfor (j = 0; j < 2; j++) {\n\t\t\tif (arena_nthreads_get(arena_get(tsd_tsdn(tsd),\n\t\t\t    choose[j], false), !!j) == 0 || first_null ==\n\t\t\t    narenas_auto) {\n\t\t\t\t/*\n\t\t\t\t * Use an unloaded arena, or the least loaded\n\t\t\t\t * arena if all arenas are already initialized.\n\t\t\t\t */\n\t\t\t\tif (!!j == internal) {\n\t\t\t\t\tret = arena_get(tsd_tsdn(tsd),\n\t\t\t\t\t    choose[j], false);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tarena_t *arena;\n\n\t\t\t\t/* Initialize a new arena. */\n\t\t\t\tchoose[j] = first_null;\n\t\t\t\tarena = arena_init_locked(tsd_tsdn(tsd),\n\t\t\t\t    choose[j],\n\t\t\t\t    (extent_hooks_t *)&extent_hooks_default);\n\t\t\t\tif (arena == NULL) {\n\t\t\t\t\tmalloc_mutex_unlock(tsd_tsdn(tsd),\n\t\t\t\t\t    &arenas_lock);\n\t\t\t\t\treturn NULL;\n\t\t\t\t}\n\t\t\t\tis_new_arena[j] = true;\n\t\t\t\tif (!!j == internal) {\n\t\t\t\t\tret = arena;\n\t\t\t\t}\n\t\t\t}\n\t\t\tarena_bind(tsd, choose[j], !!j);\n\t\t}\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &arenas_lock);\n\n\t\tfor (j = 0; j < 2; j++) {\n\t\t\tif (is_new_arena[j]) {\n\t\t\t\tassert(choose[j] > 0);\n\t\t\t\tarena_new_create_background_thread(\n\t\t\t\t    tsd_tsdn(tsd), choose[j]);\n\t\t\t}\n\t\t}\n\n\t} else {\n\t\tret = arena_get(tsd_tsdn(tsd), 0, false);\n\t\tarena_bind(tsd, 0, false);\n\t\tarena_bind(tsd, 0, true);\n\t}\n\n\treturn ret;\n}\n\nvoid\niarena_cleanup(tsd_t *tsd) {\n\tarena_t *iarena;\n\n\tiarena = tsd_iarena_get(tsd);\n\tif (iarena != NULL) {\n\t\tarena_unbind(tsd, arena_ind_get(iarena), true);\n\t}\n}\n\nvoid\narena_cleanup(tsd_t *tsd) {\n\tarena_t *arena;\n\n\tarena = tsd_arena_get(tsd);\n\tif (arena != NULL) {\n\t\tarena_unbind(tsd, arena_ind_get(arena), false);\n\t}\n}\n\nvoid\narenas_tdata_cleanup(tsd_t *tsd) {\n\tarena_tdata_t *arenas_tdata;\n\n\t/* Prevent tsd->arenas_tdata from being (re)created. */\n\t*tsd_arenas_tdata_bypassp_get(tsd) = true;\n\n\tarenas_tdata = tsd_arenas_tdata_get(tsd);\n\tif (arenas_tdata != NULL) {\n\t\ttsd_arenas_tdata_set(tsd, NULL);\n\t\ta0dalloc(arenas_tdata);\n\t}\n}\n\nstatic void\nstats_print_atexit(void) {\n\tif (config_stats) {\n\t\ttsdn_t *tsdn;\n\t\tunsigned narenas, i;\n\n\t\ttsdn = tsdn_fetch();\n\n\t\t/*\n\t\t * Merge stats from extant threads.  This is racy, since\n\t\t * individual threads do not lock when recording tcache stats\n\t\t * events.  As a consequence, the final stats may be slightly\n\t\t * out of date by the time they are reported, if other threads\n\t\t * continue to allocate.\n\t\t */\n\t\tfor (i = 0, narenas = narenas_total_get(); i < narenas; i++) {\n\t\t\tarena_t *arena = arena_get(tsdn, i, false);\n\t\t\tif (arena != NULL) {\n\t\t\t\ttcache_t *tcache;\n\n\t\t\t\tmalloc_mutex_lock(tsdn, &arena->tcache_ql_mtx);\n\t\t\t\tql_foreach(tcache, &arena->tcache_ql, link) {\n\t\t\t\t\ttcache_stats_merge(tsdn, tcache, arena);\n\t\t\t\t}\n\t\t\t\tmalloc_mutex_unlock(tsdn,\n\t\t\t\t    &arena->tcache_ql_mtx);\n\t\t\t}\n\t\t}\n\t}\n\tje_malloc_stats_print(NULL, NULL, opt_stats_print_opts);\n}\n\n/*\n * Ensure that we don't hold any locks upon entry to or exit from allocator\n * code (in a \"broad\" sense that doesn't count a reentrant allocation as an\n * entrance or exit).\n */\nJEMALLOC_ALWAYS_INLINE void\ncheck_entry_exit_locking(tsdn_t *tsdn) {\n\tif (!config_debug) {\n\t\treturn;\n\t}\n\tif (tsdn_null(tsdn)) {\n\t\treturn;\n\t}\n\ttsd_t *tsd = tsdn_tsd(tsdn);\n\t/*\n\t * It's possible we hold locks at entry/exit if we're in a nested\n\t * allocation.\n\t */\n\tint8_t reentrancy_level = tsd_reentrancy_level_get(tsd);\n\tif (reentrancy_level != 0) {\n\t\treturn;\n\t}\n\twitness_assert_lockless(tsdn_witness_tsdp_get(tsdn));\n}\n\n/*\n * End miscellaneous support functions.\n */\n/******************************************************************************/\n/*\n * Begin initialization functions.\n */\n\nstatic char *\njemalloc_secure_getenv(const char *name) {\n#ifdef JEMALLOC_HAVE_SECURE_GETENV\n\treturn secure_getenv(name);\n#else\n#  ifdef JEMALLOC_HAVE_ISSETUGID\n\tif (issetugid() != 0) {\n\t\treturn NULL;\n\t}\n#  endif\n\treturn getenv(name);\n#endif\n}\n\nstatic unsigned\nmalloc_ncpus(void) {\n\tlong result;\n\n#ifdef _WIN32\n\tSYSTEM_INFO si;\n\tGetSystemInfo(&si);\n\tresult = si.dwNumberOfProcessors;\n#elif defined(JEMALLOC_GLIBC_MALLOC_HOOK) && defined(CPU_COUNT)\n\t/*\n\t * glibc >= 2.6 has the CPU_COUNT macro.\n\t *\n\t * glibc's sysconf() uses isspace().  glibc allocates for the first time\n\t * *before* setting up the isspace tables.  Therefore we need a\n\t * different method to get the number of CPUs.\n\t */\n\t{\n\t\tcpu_set_t set;\n\n\t\tpthread_getaffinity_np(pthread_self(), sizeof(set), &set);\n\t\tresult = CPU_COUNT(&set);\n\t}\n#else\n\tresult = sysconf(_SC_NPROCESSORS_ONLN);\n#endif\n\treturn ((result == -1) ? 1 : (unsigned)result);\n}\n\nstatic void\ninit_opt_stats_print_opts(const char *v, size_t vlen) {\n\tsize_t opts_len = strlen(opt_stats_print_opts);\n\tassert(opts_len <= stats_print_tot_num_options);\n\n\tfor (size_t i = 0; i < vlen; i++) {\n\t\tswitch (v[i]) {\n#define OPTION(o, v, d, s) case o: break;\n\t\t\tSTATS_PRINT_OPTIONS\n#undef OPTION\n\t\tdefault: continue;\n\t\t}\n\n\t\tif (strchr(opt_stats_print_opts, v[i]) != NULL) {\n\t\t\t/* Ignore repeated. */\n\t\t\tcontinue;\n\t\t}\n\n\t\topt_stats_print_opts[opts_len++] = v[i];\n\t\topt_stats_print_opts[opts_len] = '\\0';\n\t\tassert(opts_len <= stats_print_tot_num_options);\n\t}\n\tassert(opts_len == strlen(opt_stats_print_opts));\n}\n\nstatic bool\nmalloc_conf_next(char const **opts_p, char const **k_p, size_t *klen_p,\n    char const **v_p, size_t *vlen_p) {\n\tbool accept;\n\tconst char *opts = *opts_p;\n\n\t*k_p = opts;\n\n\tfor (accept = false; !accept;) {\n\t\tswitch (*opts) {\n\t\tcase 'A': case 'B': case 'C': case 'D': case 'E': case 'F':\n\t\tcase 'G': case 'H': case 'I': case 'J': case 'K': case 'L':\n\t\tcase 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':\n\t\tcase 'S': case 'T': case 'U': case 'V': case 'W': case 'X':\n\t\tcase 'Y': case 'Z':\n\t\tcase 'a': case 'b': case 'c': case 'd': case 'e': case 'f':\n\t\tcase 'g': case 'h': case 'i': case 'j': case 'k': case 'l':\n\t\tcase 'm': case 'n': case 'o': case 'p': case 'q': case 'r':\n\t\tcase 's': case 't': case 'u': case 'v': case 'w': case 'x':\n\t\tcase 'y': case 'z':\n\t\tcase '0': case '1': case '2': case '3': case '4': case '5':\n\t\tcase '6': case '7': case '8': case '9':\n\t\tcase '_':\n\t\t\topts++;\n\t\t\tbreak;\n\t\tcase ':':\n\t\t\topts++;\n\t\t\t*klen_p = (uintptr_t)opts - 1 - (uintptr_t)*k_p;\n\t\t\t*v_p = opts;\n\t\t\taccept = true;\n\t\t\tbreak;\n\t\tcase '\\0':\n\t\t\tif (opts != *opts_p) {\n\t\t\t\tmalloc_write(\"<jemalloc>: Conf string ends \"\n\t\t\t\t    \"with key\\n\");\n\t\t\t}\n\t\t\treturn true;\n\t\tdefault:\n\t\t\tmalloc_write(\"<jemalloc>: Malformed conf string\\n\");\n\t\t\treturn true;\n\t\t}\n\t}\n\n\tfor (accept = false; !accept;) {\n\t\tswitch (*opts) {\n\t\tcase ',':\n\t\t\topts++;\n\t\t\t/*\n\t\t\t * Look ahead one character here, because the next time\n\t\t\t * this function is called, it will assume that end of\n\t\t\t * input has been cleanly reached if no input remains,\n\t\t\t * but we have optimistically already consumed the\n\t\t\t * comma if one exists.\n\t\t\t */\n\t\t\tif (*opts == '\\0') {\n\t\t\t\tmalloc_write(\"<jemalloc>: Conf string ends \"\n\t\t\t\t    \"with comma\\n\");\n\t\t\t}\n\t\t\t*vlen_p = (uintptr_t)opts - 1 - (uintptr_t)*v_p;\n\t\t\taccept = true;\n\t\t\tbreak;\n\t\tcase '\\0':\n\t\t\t*vlen_p = (uintptr_t)opts - (uintptr_t)*v_p;\n\t\t\taccept = true;\n\t\t\tbreak;\n\t\tdefault:\n\t\t\topts++;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\t*opts_p = opts;\n\treturn false;\n}\n\nstatic void\nmalloc_abort_invalid_conf(void) {\n\tassert(opt_abort_conf);\n\tmalloc_printf(\"<jemalloc>: Abort (abort_conf:true) on invalid conf \"\n\t    \"value (see above).\\n\");\n\tabort();\n}\n\nstatic void\nmalloc_conf_error(const char *msg, const char *k, size_t klen, const char *v,\n    size_t vlen) {\n\tmalloc_printf(\"<jemalloc>: %s: %.*s:%.*s\\n\", msg, (int)klen, k,\n\t    (int)vlen, v);\n\thad_conf_error = true;\n\tif (opt_abort_conf) {\n\t\tmalloc_abort_invalid_conf();\n\t}\n}\n\nstatic void\nmalloc_slow_flag_init(void) {\n\t/*\n\t * Combine the runtime options into malloc_slow for fast path.  Called\n\t * after processing all the options.\n\t */\n\tmalloc_slow_flags |= (opt_junk_alloc ? flag_opt_junk_alloc : 0)\n\t    | (opt_junk_free ? flag_opt_junk_free : 0)\n\t    | (opt_zero ? flag_opt_zero : 0)\n\t    | (opt_utrace ? flag_opt_utrace : 0)\n\t    | (opt_xmalloc ? flag_opt_xmalloc : 0);\n\n\tmalloc_slow = (malloc_slow_flags != 0);\n}\n\nstatic void\nmalloc_conf_init(void) {\n\tunsigned i;\n\tchar buf[PATH_MAX + 1];\n\tconst char *opts, *k, *v;\n\tsize_t klen, vlen;\n\n\tfor (i = 0; i < 4; i++) {\n\t\t/* Get runtime configuration. */\n\t\tswitch (i) {\n\t\tcase 0:\n\t\t\topts = config_malloc_conf;\n\t\t\tbreak;\n\t\tcase 1:\n\t\t\tif (je_malloc_conf != NULL) {\n\t\t\t\t/*\n\t\t\t\t * Use options that were compiled into the\n\t\t\t\t * program.\n\t\t\t\t */\n\t\t\t\topts = je_malloc_conf;\n\t\t\t} else {\n\t\t\t\t/* No configuration specified. */\n\t\t\t\tbuf[0] = '\\0';\n\t\t\t\topts = buf;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase 2: {\n\t\t\tssize_t linklen = 0;\n#ifndef _WIN32\n\t\t\tint saved_errno = errno;\n\t\t\tconst char *linkname =\n#  ifdef JEMALLOC_PREFIX\n\t\t\t    \"/etc/\"JEMALLOC_PREFIX\"malloc.conf\"\n#  else\n\t\t\t    \"/etc/malloc.conf\"\n#  endif\n\t\t\t    ;\n\n\t\t\t/*\n\t\t\t * Try to use the contents of the \"/etc/malloc.conf\"\n\t\t\t * symbolic link's name.\n\t\t\t */\n\t\t\tlinklen = readlink(linkname, buf, sizeof(buf) - 1);\n\t\t\tif (linklen == -1) {\n\t\t\t\t/* No configuration specified. */\n\t\t\t\tlinklen = 0;\n\t\t\t\t/* Restore errno. */\n\t\t\t\tset_errno(saved_errno);\n\t\t\t}\n#endif\n\t\t\tbuf[linklen] = '\\0';\n\t\t\topts = buf;\n\t\t\tbreak;\n\t\t} case 3: {\n\t\t\tconst char *envname =\n#ifdef JEMALLOC_PREFIX\n\t\t\t    JEMALLOC_CPREFIX\"MALLOC_CONF\"\n#else\n\t\t\t    \"MALLOC_CONF\"\n#endif\n\t\t\t    ;\n\n\t\t\tif ((opts = jemalloc_secure_getenv(envname)) != NULL) {\n\t\t\t\t/*\n\t\t\t\t * Do nothing; opts is already initialized to\n\t\t\t\t * the value of the MALLOC_CONF environment\n\t\t\t\t * variable.\n\t\t\t\t */\n\t\t\t} else {\n\t\t\t\t/* No configuration specified. */\n\t\t\t\tbuf[0] = '\\0';\n\t\t\t\topts = buf;\n\t\t\t}\n\t\t\tbreak;\n\t\t} default:\n\t\t\tnot_reached();\n\t\t\tbuf[0] = '\\0';\n\t\t\topts = buf;\n\t\t}\n\n\t\twhile (*opts != '\\0' && !malloc_conf_next(&opts, &k, &klen, &v,\n\t\t    &vlen)) {\n#define CONF_MATCH(n)\t\t\t\t\t\t\t\\\n\t(sizeof(n)-1 == klen && strncmp(n, k, klen) == 0)\n#define CONF_MATCH_VALUE(n)\t\t\t\t\t\t\\\n\t(sizeof(n)-1 == vlen && strncmp(n, v, vlen) == 0)\n#define CONF_HANDLE_BOOL(o, n)\t\t\t\t\t\t\\\n\t\t\tif (CONF_MATCH(n)) {\t\t\t\t\\\n\t\t\t\tif (CONF_MATCH_VALUE(\"true\")) {\t\t\\\n\t\t\t\t\to = true;\t\t\t\\\n\t\t\t\t} else if (CONF_MATCH_VALUE(\"false\")) {\t\\\n\t\t\t\t\to = false;\t\t\t\\\n\t\t\t\t} else {\t\t\t\t\\\n\t\t\t\t\tmalloc_conf_error(\t\t\\\n\t\t\t\t\t    \"Invalid conf value\",\t\\\n\t\t\t\t\t    k, klen, v, vlen);\t\t\\\n\t\t\t\t}\t\t\t\t\t\\\n\t\t\t\tcontinue;\t\t\t\t\\\n\t\t\t}\n#define CONF_MIN_no(um, min)\tfalse\n#define CONF_MIN_yes(um, min)\t((um) < (min))\n#define CONF_MAX_no(um, max)\tfalse\n#define CONF_MAX_yes(um, max)\t((um) > (max))\n#define CONF_HANDLE_T_U(t, o, n, min, max, check_min, check_max, clip)\t\\\n\t\t\tif (CONF_MATCH(n)) {\t\t\t\t\\\n\t\t\t\tuintmax_t um;\t\t\t\t\\\n\t\t\t\tchar *end;\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t\t\t\tset_errno(0);\t\t\t\t\\\n\t\t\t\tum = malloc_strtoumax(v, &end, 0);\t\\\n\t\t\t\tif (get_errno() != 0 || (uintptr_t)end -\\\n\t\t\t\t    (uintptr_t)v != vlen) {\t\t\\\n\t\t\t\t\tmalloc_conf_error(\t\t\\\n\t\t\t\t\t    \"Invalid conf value\",\t\\\n\t\t\t\t\t    k, klen, v, vlen);\t\t\\\n\t\t\t\t} else if (clip) {\t\t\t\\\n\t\t\t\t\tif (CONF_MIN_##check_min(um,\t\\\n\t\t\t\t\t    (t)(min))) {\t\t\\\n\t\t\t\t\t\to = (t)(min);\t\t\\\n\t\t\t\t\t} else if (\t\t\t\\\n\t\t\t\t\t    CONF_MAX_##check_max(um,\t\\\n\t\t\t\t\t    (t)(max))) {\t\t\\\n\t\t\t\t\t\to = (t)(max);\t\t\\\n\t\t\t\t\t} else {\t\t\t\\\n\t\t\t\t\t\to = (t)um;\t\t\\\n\t\t\t\t\t}\t\t\t\t\\\n\t\t\t\t} else {\t\t\t\t\\\n\t\t\t\t\tif (CONF_MIN_##check_min(um,\t\\\n\t\t\t\t\t    (t)(min)) ||\t\t\\\n\t\t\t\t\t    CONF_MAX_##check_max(um,\t\\\n\t\t\t\t\t    (t)(max))) {\t\t\\\n\t\t\t\t\t\tmalloc_conf_error(\t\\\n\t\t\t\t\t\t    \"Out-of-range \"\t\\\n\t\t\t\t\t\t    \"conf value\",\t\\\n\t\t\t\t\t\t    k, klen, v, vlen);\t\\\n\t\t\t\t\t} else {\t\t\t\\\n\t\t\t\t\t\to = (t)um;\t\t\\\n\t\t\t\t\t}\t\t\t\t\\\n\t\t\t\t}\t\t\t\t\t\\\n\t\t\t\tcontinue;\t\t\t\t\\\n\t\t\t}\n#define CONF_HANDLE_UNSIGNED(o, n, min, max, check_min, check_max,\t\\\n    clip)\t\t\t\t\t\t\t\t\\\n\t\t\tCONF_HANDLE_T_U(unsigned, o, n, min, max,\t\\\n\t\t\t    check_min, check_max, clip)\n#define CONF_HANDLE_SIZE_T(o, n, min, max, check_min, check_max, clip)\t\\\n\t\t\tCONF_HANDLE_T_U(size_t, o, n, min, max,\t\t\\\n\t\t\t    check_min, check_max, clip)\n#define CONF_HANDLE_SSIZE_T(o, n, min, max)\t\t\t\t\\\n\t\t\tif (CONF_MATCH(n)) {\t\t\t\t\\\n\t\t\t\tlong l;\t\t\t\t\t\\\n\t\t\t\tchar *end;\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t\t\t\tset_errno(0);\t\t\t\t\\\n\t\t\t\tl = strtol(v, &end, 0);\t\t\t\\\n\t\t\t\tif (get_errno() != 0 || (uintptr_t)end -\\\n\t\t\t\t    (uintptr_t)v != vlen) {\t\t\\\n\t\t\t\t\tmalloc_conf_error(\t\t\\\n\t\t\t\t\t    \"Invalid conf value\",\t\\\n\t\t\t\t\t    k, klen, v, vlen);\t\t\\\n\t\t\t\t} else if (l < (ssize_t)(min) || l >\t\\\n\t\t\t\t    (ssize_t)(max)) {\t\t\t\\\n\t\t\t\t\tmalloc_conf_error(\t\t\\\n\t\t\t\t\t    \"Out-of-range conf value\",\t\\\n\t\t\t\t\t    k, klen, v, vlen);\t\t\\\n\t\t\t\t} else {\t\t\t\t\\\n\t\t\t\t\to = l;\t\t\t\t\\\n\t\t\t\t}\t\t\t\t\t\\\n\t\t\t\tcontinue;\t\t\t\t\\\n\t\t\t}\n#define CONF_HANDLE_CHAR_P(o, n, d)\t\t\t\t\t\\\n\t\t\tif (CONF_MATCH(n)) {\t\t\t\t\\\n\t\t\t\tsize_t cpylen = (vlen <=\t\t\\\n\t\t\t\t    sizeof(o)-1) ? vlen :\t\t\\\n\t\t\t\t    sizeof(o)-1;\t\t\t\\\n\t\t\t\tstrncpy(o, v, cpylen);\t\t\t\\\n\t\t\t\to[cpylen] = '\\0';\t\t\t\\\n\t\t\t\tcontinue;\t\t\t\t\\\n\t\t\t}\n\n\t\t\tCONF_HANDLE_BOOL(opt_abort, \"abort\")\n\t\t\tCONF_HANDLE_BOOL(opt_abort_conf, \"abort_conf\")\n\t\t\tif (opt_abort_conf && had_conf_error) {\n\t\t\t\tmalloc_abort_invalid_conf();\n\t\t\t}\n\t\t\tCONF_HANDLE_BOOL(opt_retain, \"retain\")\n\t\t\tif (strncmp(\"dss\", k, klen) == 0) {\n\t\t\t\tint i;\n\t\t\t\tbool match = false;\n\t\t\t\tfor (i = 0; i < dss_prec_limit; i++) {\n\t\t\t\t\tif (strncmp(dss_prec_names[i], v, vlen)\n\t\t\t\t\t    == 0) {\n\t\t\t\t\t\tif (extent_dss_prec_set(i)) {\n\t\t\t\t\t\t\tmalloc_conf_error(\n\t\t\t\t\t\t\t    \"Error setting dss\",\n\t\t\t\t\t\t\t    k, klen, v, vlen);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\topt_dss =\n\t\t\t\t\t\t\t    dss_prec_names[i];\n\t\t\t\t\t\t\tmatch = true;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (!match) {\n\t\t\t\t\tmalloc_conf_error(\"Invalid conf value\",\n\t\t\t\t\t    k, klen, v, vlen);\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tCONF_HANDLE_UNSIGNED(opt_narenas, \"narenas\", 1,\n\t\t\t    UINT_MAX, yes, no, false)\n\t\t\tCONF_HANDLE_SSIZE_T(opt_dirty_decay_ms,\n\t\t\t    \"dirty_decay_ms\", -1, NSTIME_SEC_MAX * KQU(1000) <\n\t\t\t    QU(SSIZE_MAX) ? NSTIME_SEC_MAX * KQU(1000) :\n\t\t\t    SSIZE_MAX);\n\t\t\tCONF_HANDLE_SSIZE_T(opt_muzzy_decay_ms,\n\t\t\t    \"muzzy_decay_ms\", -1, NSTIME_SEC_MAX * KQU(1000) <\n\t\t\t    QU(SSIZE_MAX) ? NSTIME_SEC_MAX * KQU(1000) :\n\t\t\t    SSIZE_MAX);\n\t\t\tCONF_HANDLE_BOOL(opt_stats_print, \"stats_print\")\n\t\t\tif (CONF_MATCH(\"stats_print_opts\")) {\n\t\t\t\tinit_opt_stats_print_opts(v, vlen);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (config_fill) {\n\t\t\t\tif (CONF_MATCH(\"junk\")) {\n\t\t\t\t\tif (CONF_MATCH_VALUE(\"true\")) {\n\t\t\t\t\t\topt_junk = \"true\";\n\t\t\t\t\t\topt_junk_alloc = opt_junk_free =\n\t\t\t\t\t\t    true;\n\t\t\t\t\t} else if (CONF_MATCH_VALUE(\"false\")) {\n\t\t\t\t\t\topt_junk = \"false\";\n\t\t\t\t\t\topt_junk_alloc = opt_junk_free =\n\t\t\t\t\t\t    false;\n\t\t\t\t\t} else if (CONF_MATCH_VALUE(\"alloc\")) {\n\t\t\t\t\t\topt_junk = \"alloc\";\n\t\t\t\t\t\topt_junk_alloc = true;\n\t\t\t\t\t\topt_junk_free = false;\n\t\t\t\t\t} else if (CONF_MATCH_VALUE(\"free\")) {\n\t\t\t\t\t\topt_junk = \"free\";\n\t\t\t\t\t\topt_junk_alloc = false;\n\t\t\t\t\t\topt_junk_free = true;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tmalloc_conf_error(\n\t\t\t\t\t\t    \"Invalid conf value\", k,\n\t\t\t\t\t\t    klen, v, vlen);\n\t\t\t\t\t}\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tCONF_HANDLE_BOOL(opt_zero, \"zero\")\n\t\t\t}\n\t\t\tif (config_utrace) {\n\t\t\t\tCONF_HANDLE_BOOL(opt_utrace, \"utrace\")\n\t\t\t}\n\t\t\tif (config_xmalloc) {\n\t\t\t\tCONF_HANDLE_BOOL(opt_xmalloc, \"xmalloc\")\n\t\t\t}\n\t\t\tCONF_HANDLE_BOOL(opt_tcache, \"tcache\")\n\t\t\tCONF_HANDLE_SSIZE_T(opt_lg_tcache_max, \"lg_tcache_max\",\n\t\t\t    -1, (sizeof(size_t) << 3) - 1)\n\t\t\tif (strncmp(\"percpu_arena\", k, klen) == 0) {\n\t\t\t\tint i;\n\t\t\t\tbool match = false;\n\t\t\t\tfor (i = percpu_arena_mode_names_base; i <\n\t\t\t\t    percpu_arena_mode_names_limit; i++) {\n\t\t\t\t\tif (strncmp(percpu_arena_mode_names[i],\n\t\t\t\t\t    v, vlen) == 0) {\n\t\t\t\t\t\tif (!have_percpu_arena) {\n\t\t\t\t\t\t\tmalloc_conf_error(\n\t\t\t\t\t\t\t    \"No getcpu support\",\n\t\t\t\t\t\t\t    k, klen, v, vlen);\n\t\t\t\t\t\t}\n\t\t\t\t\t\topt_percpu_arena = i;\n\t\t\t\t\t\tmatch = true;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (!match) {\n\t\t\t\t\tmalloc_conf_error(\"Invalid conf value\",\n\t\t\t\t\t    k, klen, v, vlen);\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tCONF_HANDLE_BOOL(opt_background_thread,\n\t\t\t    \"background_thread\");\n\t\t\tif (config_prof) {\n\t\t\t\tCONF_HANDLE_BOOL(opt_prof, \"prof\")\n\t\t\t\tCONF_HANDLE_CHAR_P(opt_prof_prefix,\n\t\t\t\t    \"prof_prefix\", \"jeprof\")\n\t\t\t\tCONF_HANDLE_BOOL(opt_prof_active, \"prof_active\")\n\t\t\t\tCONF_HANDLE_BOOL(opt_prof_thread_active_init,\n\t\t\t\t    \"prof_thread_active_init\")\n\t\t\t\tCONF_HANDLE_SIZE_T(opt_lg_prof_sample,\n\t\t\t\t    \"lg_prof_sample\", 0, (sizeof(uint64_t) << 3)\n\t\t\t\t    - 1, no, yes, true)\n\t\t\t\tCONF_HANDLE_BOOL(opt_prof_accum, \"prof_accum\")\n\t\t\t\tCONF_HANDLE_SSIZE_T(opt_lg_prof_interval,\n\t\t\t\t    \"lg_prof_interval\", -1,\n\t\t\t\t    (sizeof(uint64_t) << 3) - 1)\n\t\t\t\tCONF_HANDLE_BOOL(opt_prof_gdump, \"prof_gdump\")\n\t\t\t\tCONF_HANDLE_BOOL(opt_prof_final, \"prof_final\")\n\t\t\t\tCONF_HANDLE_BOOL(opt_prof_leak, \"prof_leak\")\n\t\t\t}\n\t\t\tmalloc_conf_error(\"Invalid conf pair\", k, klen, v,\n\t\t\t    vlen);\n#undef CONF_MATCH\n#undef CONF_MATCH_VALUE\n#undef CONF_HANDLE_BOOL\n#undef CONF_MIN_no\n#undef CONF_MIN_yes\n#undef CONF_MAX_no\n#undef CONF_MAX_yes\n#undef CONF_HANDLE_T_U\n#undef CONF_HANDLE_UNSIGNED\n#undef CONF_HANDLE_SIZE_T\n#undef CONF_HANDLE_SSIZE_T\n#undef CONF_HANDLE_CHAR_P\n\t\t}\n\t}\n}\n\nstatic bool\nmalloc_init_hard_needed(void) {\n\tif (malloc_initialized() || (IS_INITIALIZER && malloc_init_state ==\n\t    malloc_init_recursible)) {\n\t\t/*\n\t\t * Another thread initialized the allocator before this one\n\t\t * acquired init_lock, or this thread is the initializing\n\t\t * thread, and it is recursively allocating.\n\t\t */\n\t\treturn false;\n\t}\n#ifdef JEMALLOC_THREADED_INIT\n\tif (malloc_initializer != NO_INITIALIZER && !IS_INITIALIZER) {\n\t\t/* Busy-wait until the initializing thread completes. */\n\t\tspin_t spinner = SPIN_INITIALIZER;\n\t\tdo {\n\t\t\tmalloc_mutex_unlock(TSDN_NULL, &init_lock);\n\t\t\tspin_adaptive(&spinner);\n\t\t\tmalloc_mutex_lock(TSDN_NULL, &init_lock);\n\t\t} while (!malloc_initialized());\n\t\treturn false;\n\t}\n#endif\n\treturn true;\n}\n\nstatic bool\nmalloc_init_hard_a0_locked() {\n\tmalloc_initializer = INITIALIZER;\n\n\tif (config_prof) {\n\t\tprof_boot0();\n\t}\n\tmalloc_conf_init();\n\tif (opt_stats_print) {\n\t\t/* Print statistics at exit. */\n\t\tif (atexit(stats_print_atexit) != 0) {\n\t\t\tmalloc_write(\"<jemalloc>: Error in atexit()\\n\");\n\t\t\tif (opt_abort) {\n\t\t\t\tabort();\n\t\t\t}\n\t\t}\n\t}\n\tif (pages_boot()) {\n\t\treturn true;\n\t}\n\tif (base_boot(TSDN_NULL)) {\n\t\treturn true;\n\t}\n\tif (extent_boot()) {\n\t\treturn true;\n\t}\n\tif (ctl_boot()) {\n\t\treturn true;\n\t}\n\tif (config_prof) {\n\t\tprof_boot1();\n\t}\n\tarena_boot();\n\tif (tcache_boot(TSDN_NULL)) {\n\t\treturn true;\n\t}\n\tif (malloc_mutex_init(&arenas_lock, \"arenas\", WITNESS_RANK_ARENAS,\n\t    malloc_mutex_rank_exclusive)) {\n\t\treturn true;\n\t}\n\t/*\n\t * Create enough scaffolding to allow recursive allocation in\n\t * malloc_ncpus().\n\t */\n\tnarenas_auto = 1;\n\tmemset(arenas, 0, sizeof(arena_t *) * narenas_auto);\n\t/*\n\t * Initialize one arena here.  The rest are lazily created in\n\t * arena_choose_hard().\n\t */\n\tif (arena_init(TSDN_NULL, 0, (extent_hooks_t *)&extent_hooks_default)\n\t    == NULL) {\n\t\treturn true;\n\t}\n\ta0 = arena_get(TSDN_NULL, 0, false);\n\tmalloc_init_state = malloc_init_a0_initialized;\n\n\treturn false;\n}\n\nstatic bool\nmalloc_init_hard_a0(void) {\n\tbool ret;\n\n\tmalloc_mutex_lock(TSDN_NULL, &init_lock);\n\tret = malloc_init_hard_a0_locked();\n\tmalloc_mutex_unlock(TSDN_NULL, &init_lock);\n\treturn ret;\n}\n\n/* Initialize data structures which may trigger recursive allocation. */\nstatic bool\nmalloc_init_hard_recursible(void) {\n\tmalloc_init_state = malloc_init_recursible;\n\n\tncpus = malloc_ncpus();\n\n#if (defined(JEMALLOC_HAVE_PTHREAD_ATFORK) && !defined(JEMALLOC_MUTEX_INIT_CB) \\\n    && !defined(JEMALLOC_ZONE) && !defined(_WIN32) && \\\n    !defined(__native_client__))\n\t/* LinuxThreads' pthread_atfork() allocates. */\n\tif (pthread_atfork(jemalloc_prefork, jemalloc_postfork_parent,\n\t    jemalloc_postfork_child) != 0) {\n\t\tmalloc_write(\"<jemalloc>: Error in pthread_atfork()\\n\");\n\t\tif (opt_abort) {\n\t\t\tabort();\n\t\t}\n\t\treturn true;\n\t}\n#endif\n\n\tif (background_thread_boot0()) {\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\nstatic unsigned\nmalloc_narenas_default(void) {\n\tassert(ncpus > 0);\n\t/*\n\t * For SMP systems, create more than one arena per CPU by\n\t * default.\n\t */\n\tif (ncpus > 1) {\n\t\treturn ncpus << 2;\n\t} else {\n\t\treturn 1;\n\t}\n}\n\nstatic percpu_arena_mode_t\npercpu_arena_as_initialized(percpu_arena_mode_t mode) {\n\tassert(!malloc_initialized());\n\tassert(mode <= percpu_arena_disabled);\n\n\tif (mode != percpu_arena_disabled) {\n\t\tmode += percpu_arena_mode_enabled_base;\n\t}\n\n\treturn mode;\n}\n\nstatic bool\nmalloc_init_narenas(void) {\n\tassert(ncpus > 0);\n\n\tif (opt_percpu_arena != percpu_arena_disabled) {\n\t\tif (!have_percpu_arena || malloc_getcpu() < 0) {\n\t\t\topt_percpu_arena = percpu_arena_disabled;\n\t\t\tmalloc_printf(\"<jemalloc>: perCPU arena getcpu() not \"\n\t\t\t    \"available. Setting narenas to %u.\\n\", opt_narenas ?\n\t\t\t    opt_narenas : malloc_narenas_default());\n\t\t\tif (opt_abort) {\n\t\t\t\tabort();\n\t\t\t}\n\t\t} else {\n\t\t\tif (ncpus >= MALLOCX_ARENA_LIMIT) {\n\t\t\t\tmalloc_printf(\"<jemalloc>: narenas w/ percpu\"\n\t\t\t\t    \"arena beyond limit (%d)\\n\", ncpus);\n\t\t\t\tif (opt_abort) {\n\t\t\t\t\tabort();\n\t\t\t\t}\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\t/* NB: opt_percpu_arena isn't fully initialized yet. */\n\t\t\tif (percpu_arena_as_initialized(opt_percpu_arena) ==\n\t\t\t    per_phycpu_arena && ncpus % 2 != 0) {\n\t\t\t\tmalloc_printf(\"<jemalloc>: invalid \"\n\t\t\t\t    \"configuration -- per physical CPU arena \"\n\t\t\t\t    \"with odd number (%u) of CPUs (no hyper \"\n\t\t\t\t    \"threading?).\\n\", ncpus);\n\t\t\t\tif (opt_abort)\n\t\t\t\t\tabort();\n\t\t\t}\n\t\t\tunsigned n = percpu_arena_ind_limit(\n\t\t\t    percpu_arena_as_initialized(opt_percpu_arena));\n\t\t\tif (opt_narenas < n) {\n\t\t\t\t/*\n\t\t\t\t * If narenas is specified with percpu_arena\n\t\t\t\t * enabled, actual narenas is set as the greater\n\t\t\t\t * of the two. percpu_arena_choose will be free\n\t\t\t\t * to use any of the arenas based on CPU\n\t\t\t\t * id. This is conservative (at a small cost)\n\t\t\t\t * but ensures correctness.\n\t\t\t\t *\n\t\t\t\t * If for some reason the ncpus determined at\n\t\t\t\t * boot is not the actual number (e.g. because\n\t\t\t\t * of affinity setting from numactl), reserving\n\t\t\t\t * narenas this way provides a workaround for\n\t\t\t\t * percpu_arena.\n\t\t\t\t */\n\t\t\t\topt_narenas = n;\n\t\t\t}\n\t\t}\n\t}\n\tif (opt_narenas == 0) {\n\t\topt_narenas = malloc_narenas_default();\n\t}\n\tassert(opt_narenas > 0);\n\n\tnarenas_auto = opt_narenas;\n\t/*\n\t * Limit the number of arenas to the indexing range of MALLOCX_ARENA().\n\t */\n\tif (narenas_auto >= MALLOCX_ARENA_LIMIT) {\n\t\tnarenas_auto = MALLOCX_ARENA_LIMIT - 1;\n\t\tmalloc_printf(\"<jemalloc>: Reducing narenas to limit (%d)\\n\",\n\t\t    narenas_auto);\n\t}\n\tnarenas_total_set(narenas_auto);\n\n\treturn false;\n}\n\nstatic void\nmalloc_init_percpu(void) {\n\topt_percpu_arena = percpu_arena_as_initialized(opt_percpu_arena);\n}\n\nstatic bool\nmalloc_init_hard_finish(void) {\n\tif (malloc_mutex_boot()) {\n\t\treturn true;\n\t}\n\n\tmalloc_init_state = malloc_init_initialized;\n\tmalloc_slow_flag_init();\n\n\treturn false;\n}\n\nstatic void\nmalloc_init_hard_cleanup(tsdn_t *tsdn, bool reentrancy_set) {\n\tmalloc_mutex_assert_owner(tsdn, &init_lock);\n\tmalloc_mutex_unlock(tsdn, &init_lock);\n\tif (reentrancy_set) {\n\t\tassert(!tsdn_null(tsdn));\n\t\ttsd_t *tsd = tsdn_tsd(tsdn);\n\t\tassert(tsd_reentrancy_level_get(tsd) > 0);\n\t\tpost_reentrancy(tsd);\n\t}\n}\n\nstatic bool\nmalloc_init_hard(void) {\n\ttsd_t *tsd;\n\n#if defined(_WIN32) && _WIN32_WINNT < 0x0600\n\t_init_init_lock();\n#endif\n\tmalloc_mutex_lock(TSDN_NULL, &init_lock);\n\n#define UNLOCK_RETURN(tsdn, ret, reentrancy)\t\t\\\n\tmalloc_init_hard_cleanup(tsdn, reentrancy);\t\\\n\treturn ret;\n\n\tif (!malloc_init_hard_needed()) {\n\t\tUNLOCK_RETURN(TSDN_NULL, false, false)\n\t}\n\n\tif (malloc_init_state != malloc_init_a0_initialized &&\n\t    malloc_init_hard_a0_locked()) {\n\t\tUNLOCK_RETURN(TSDN_NULL, true, false)\n\t}\n\n\tmalloc_mutex_unlock(TSDN_NULL, &init_lock);\n\t/* Recursive allocation relies on functional tsd. */\n\ttsd = malloc_tsd_boot0();\n\tif (tsd == NULL) {\n\t\treturn true;\n\t}\n\tif (malloc_init_hard_recursible()) {\n\t\treturn true;\n\t}\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &init_lock);\n\t/* Set reentrancy level to 1 during init. */\n\tpre_reentrancy(tsd);\n\t/* Initialize narenas before prof_boot2 (for allocation). */\n\tif (malloc_init_narenas() || background_thread_boot1(tsd_tsdn(tsd))) {\n\t\tUNLOCK_RETURN(tsd_tsdn(tsd), true, true)\n\t}\n\tif (config_prof && prof_boot2(tsd)) {\n\t\tUNLOCK_RETURN(tsd_tsdn(tsd), true, true)\n\t}\n\n\tmalloc_init_percpu();\n\n\tif (malloc_init_hard_finish()) {\n\t\tUNLOCK_RETURN(tsd_tsdn(tsd), true, true)\n\t}\n\tpost_reentrancy(tsd);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &init_lock);\n\n\tmalloc_tsd_boot1();\n\t/* Update TSD after tsd_boot1. */\n\ttsd = tsd_fetch();\n\tif (opt_background_thread) {\n\t\tassert(have_background_thread);\n\t\t/*\n\t\t * Need to finish init & unlock first before creating background\n\t\t * threads (pthread_create depends on malloc).\n\t\t */\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &background_thread_lock);\n\t\tbool err = background_thread_create(tsd, 0);\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &background_thread_lock);\n\t\tif (err) {\n\t\t\treturn true;\n\t\t}\n\t}\n#undef UNLOCK_RETURN\n\treturn false;\n}\n\n/*\n * End initialization functions.\n */\n/******************************************************************************/\n/*\n * Begin allocation-path internal functions and data structures.\n */\n\n/*\n * Settings determined by the documented behavior of the allocation functions.\n */\ntypedef struct static_opts_s static_opts_t;\nstruct static_opts_s {\n\t/* Whether or not allocation size may overflow. */\n\tbool may_overflow;\n\t/* Whether or not allocations of size 0 should be treated as size 1. */\n\tbool bump_empty_alloc;\n\t/*\n\t * Whether to assert that allocations are not of size 0 (after any\n\t * bumping).\n\t */\n\tbool assert_nonempty_alloc;\n\n\t/*\n\t * Whether or not to modify the 'result' argument to malloc in case of\n\t * error.\n\t */\n\tbool null_out_result_on_error;\n\t/* Whether to set errno when we encounter an error condition. */\n\tbool set_errno_on_error;\n\n\t/*\n\t * The minimum valid alignment for functions requesting aligned storage.\n\t */\n\tsize_t min_alignment;\n\n\t/* The error string to use if we oom. */\n\tconst char *oom_string;\n\t/* The error string to use if the passed-in alignment is invalid. */\n\tconst char *invalid_alignment_string;\n\n\t/*\n\t * False if we're configured to skip some time-consuming operations.\n\t *\n\t * This isn't really a malloc \"behavior\", but it acts as a useful\n\t * summary of several other static (or at least, static after program\n\t * initialization) options.\n\t */\n\tbool slow;\n};\n\nJEMALLOC_ALWAYS_INLINE void\nstatic_opts_init(static_opts_t *static_opts) {\n\tstatic_opts->may_overflow = false;\n\tstatic_opts->bump_empty_alloc = false;\n\tstatic_opts->assert_nonempty_alloc = false;\n\tstatic_opts->null_out_result_on_error = false;\n\tstatic_opts->set_errno_on_error = false;\n\tstatic_opts->min_alignment = 0;\n\tstatic_opts->oom_string = \"\";\n\tstatic_opts->invalid_alignment_string = \"\";\n\tstatic_opts->slow = false;\n}\n\n/*\n * These correspond to the macros in jemalloc/jemalloc_macros.h.  Broadly, we\n * should have one constant here per magic value there.  Note however that the\n * representations need not be related.\n */\n#define TCACHE_IND_NONE ((unsigned)-1)\n#define TCACHE_IND_AUTOMATIC ((unsigned)-2)\n#define ARENA_IND_AUTOMATIC ((unsigned)-1)\n\ntypedef struct dynamic_opts_s dynamic_opts_t;\nstruct dynamic_opts_s {\n\tvoid **result;\n\tsize_t num_items;\n\tsize_t item_size;\n\tsize_t alignment;\n\tbool zero;\n\tunsigned tcache_ind;\n\tunsigned arena_ind;\n};\n\nJEMALLOC_ALWAYS_INLINE void\ndynamic_opts_init(dynamic_opts_t *dynamic_opts) {\n\tdynamic_opts->result = NULL;\n\tdynamic_opts->num_items = 0;\n\tdynamic_opts->item_size = 0;\n\tdynamic_opts->alignment = 0;\n\tdynamic_opts->zero = false;\n\tdynamic_opts->tcache_ind = TCACHE_IND_AUTOMATIC;\n\tdynamic_opts->arena_ind = ARENA_IND_AUTOMATIC;\n}\n\n/* ind is ignored if dopts->alignment > 0. */\nJEMALLOC_ALWAYS_INLINE void *\nimalloc_no_sample(static_opts_t *sopts, dynamic_opts_t *dopts, tsd_t *tsd,\n    size_t size, size_t usize, szind_t ind) {\n\ttcache_t *tcache;\n\tarena_t *arena;\n\n\t/* Fill in the tcache. */\n\tif (dopts->tcache_ind == TCACHE_IND_AUTOMATIC) {\n\t\tif (likely(!sopts->slow)) {\n\t\t\t/* Getting tcache ptr unconditionally. */\n\t\t\ttcache = tsd_tcachep_get(tsd);\n\t\t\tassert(tcache == tcache_get(tsd));\n\t\t} else {\n\t\t\ttcache = tcache_get(tsd);\n\t\t}\n\t} else if (dopts->tcache_ind == TCACHE_IND_NONE) {\n\t\ttcache = NULL;\n\t} else {\n\t\ttcache = tcaches_get(tsd, dopts->tcache_ind);\n\t}\n\n\t/* Fill in the arena. */\n\tif (dopts->arena_ind == ARENA_IND_AUTOMATIC) {\n\t\t/*\n\t\t * In case of automatic arena management, we defer arena\n\t\t * computation until as late as we can, hoping to fill the\n\t\t * allocation out of the tcache.\n\t\t */\n\t\tarena = NULL;\n\t} else {\n\t\tarena = arena_get(tsd_tsdn(tsd), dopts->arena_ind, true);\n\t}\n\n\tif (unlikely(dopts->alignment != 0)) {\n\t\treturn ipalloct(tsd_tsdn(tsd), usize, dopts->alignment,\n\t\t    dopts->zero, tcache, arena);\n\t}\n\n\treturn iallocztm(tsd_tsdn(tsd), size, ind, dopts->zero, tcache, false,\n\t    arena, sopts->slow);\n}\n\nJEMALLOC_ALWAYS_INLINE void *\nimalloc_sample(static_opts_t *sopts, dynamic_opts_t *dopts, tsd_t *tsd,\n    size_t usize, szind_t ind) {\n\tvoid *ret;\n\n\t/*\n\t * For small allocations, sampling bumps the usize.  If so, we allocate\n\t * from the ind_large bucket.\n\t */\n\tszind_t ind_large;\n\tsize_t bumped_usize = usize;\n\n\tif (usize <= SMALL_MAXCLASS) {\n\t\tassert(((dopts->alignment == 0) ? sz_s2u(LARGE_MINCLASS) :\n\t\t    sz_sa2u(LARGE_MINCLASS, dopts->alignment))\n\t\t    == LARGE_MINCLASS);\n\t\tind_large = sz_size2index(LARGE_MINCLASS);\n\t\tbumped_usize = sz_s2u(LARGE_MINCLASS);\n\t\tret = imalloc_no_sample(sopts, dopts, tsd, bumped_usize,\n\t\t    bumped_usize, ind_large);\n\t\tif (unlikely(ret == NULL)) {\n\t\t\treturn NULL;\n\t\t}\n\t\tarena_prof_promote(tsd_tsdn(tsd), ret, usize);\n\t} else {\n\t\tret = imalloc_no_sample(sopts, dopts, tsd, usize, usize, ind);\n\t}\n\n\treturn ret;\n}\n\n/*\n * Returns true if the allocation will overflow, and false otherwise.  Sets\n * *size to the product either way.\n */\nJEMALLOC_ALWAYS_INLINE bool\ncompute_size_with_overflow(bool may_overflow, dynamic_opts_t *dopts,\n    size_t *size) {\n\t/*\n\t * This function is just num_items * item_size, except that we may have\n\t * to check for overflow.\n\t */\n\n\tif (!may_overflow) {\n\t\tassert(dopts->num_items == 1);\n\t\t*size = dopts->item_size;\n\t\treturn false;\n\t}\n\n\t/* A size_t with its high-half bits all set to 1. */\n\tconst static size_t high_bits = SIZE_T_MAX << (sizeof(size_t) * 8 / 2);\n\n\t*size = dopts->item_size * dopts->num_items;\n\n\tif (unlikely(*size == 0)) {\n\t\treturn (dopts->num_items != 0 && dopts->item_size != 0);\n\t}\n\n\t/*\n\t * We got a non-zero size, but we don't know if we overflowed to get\n\t * there.  To avoid having to do a divide, we'll be clever and note that\n\t * if both A and B can be represented in N/2 bits, then their product\n\t * can be represented in N bits (without the possibility of overflow).\n\t */\n\tif (likely((high_bits & (dopts->num_items | dopts->item_size)) == 0)) {\n\t\treturn false;\n\t}\n\tif (likely(*size / dopts->item_size == dopts->num_items)) {\n\t\treturn false;\n\t}\n\treturn true;\n}\n\nJEMALLOC_ALWAYS_INLINE int\nimalloc_body(static_opts_t *sopts, dynamic_opts_t *dopts, tsd_t *tsd) {\n\t/* Where the actual allocated memory will live. */\n\tvoid *allocation = NULL;\n\t/* Filled in by compute_size_with_overflow below. */\n\tsize_t size = 0;\n\t/*\n\t * For unaligned allocations, we need only ind.  For aligned\n\t * allocations, or in case of stats or profiling we need usize.\n\t *\n\t * These are actually dead stores, in that their values are reset before\n\t * any branch on their value is taken.  Sometimes though, it's\n\t * convenient to pass them as arguments before this point.  To avoid\n\t * undefined behavior then, we initialize them with dummy stores.\n\t */\n\tszind_t ind = 0;\n\tsize_t usize = 0;\n\n\t/* Reentrancy is only checked on slow path. */\n\tint8_t reentrancy_level;\n\n\t/* Compute the amount of memory the user wants. */\n\tif (unlikely(compute_size_with_overflow(sopts->may_overflow, dopts,\n\t    &size))) {\n\t\tgoto label_oom;\n\t}\n\n\t/* Validate the user input. */\n\tif (sopts->bump_empty_alloc) {\n\t\tif (unlikely(size == 0)) {\n\t\t\tsize = 1;\n\t\t}\n\t}\n\n\tif (sopts->assert_nonempty_alloc) {\n\t\tassert (size != 0);\n\t}\n\n\tif (unlikely(dopts->alignment < sopts->min_alignment\n\t    || (dopts->alignment & (dopts->alignment - 1)) != 0)) {\n\t\tgoto label_invalid_alignment;\n\t}\n\n\t/* This is the beginning of the \"core\" algorithm. */\n\n\tif (dopts->alignment == 0) {\n\t\tind = sz_size2index(size);\n\t\tif (unlikely(ind >= NSIZES)) {\n\t\t\tgoto label_oom;\n\t\t}\n\t\tif (config_stats || (config_prof && opt_prof)) {\n\t\t\tusize = sz_index2size(ind);\n\t\t\tassert(usize > 0 && usize <= LARGE_MAXCLASS);\n\t\t}\n\t} else {\n\t\tusize = sz_sa2u(size, dopts->alignment);\n\t\tif (unlikely(usize == 0 || usize > LARGE_MAXCLASS)) {\n\t\t\tgoto label_oom;\n\t\t}\n\t}\n\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\n\t/*\n\t * If we need to handle reentrancy, we can do it out of a\n\t * known-initialized arena (i.e. arena 0).\n\t */\n\treentrancy_level = tsd_reentrancy_level_get(tsd);\n\tif (sopts->slow && unlikely(reentrancy_level > 0)) {\n\t\t/*\n\t\t * We should never specify particular arenas or tcaches from\n\t\t * within our internal allocations.\n\t\t */\n\t\tassert(dopts->tcache_ind == TCACHE_IND_AUTOMATIC ||\n\t\t    dopts->tcache_ind == TCACHE_IND_NONE);\n\t\tassert(dopts->arena_ind = ARENA_IND_AUTOMATIC);\n\t\tdopts->tcache_ind = TCACHE_IND_NONE;\n\t\t/* We know that arena 0 has already been initialized. */\n\t\tdopts->arena_ind = 0;\n\t}\n\n\t/* If profiling is on, get our profiling context. */\n\tif (config_prof && opt_prof) {\n\t\t/*\n\t\t * Note that if we're going down this path, usize must have been\n\t\t * initialized in the previous if statement.\n\t\t */\n\t\tprof_tctx_t *tctx = prof_alloc_prep(\n\t\t    tsd, usize, prof_active_get_unlocked(), true);\n\n\t\talloc_ctx_t alloc_ctx;\n\t\tif (likely((uintptr_t)tctx == (uintptr_t)1U)) {\n\t\t\talloc_ctx.slab = (usize <= SMALL_MAXCLASS);\n\t\t\tallocation = imalloc_no_sample(\n\t\t\t    sopts, dopts, tsd, usize, usize, ind);\n\t\t} else if ((uintptr_t)tctx > (uintptr_t)1U) {\n\t\t\t/*\n\t\t\t * Note that ind might still be 0 here.  This is fine;\n\t\t\t * imalloc_sample ignores ind if dopts->alignment > 0.\n\t\t\t */\n\t\t\tallocation = imalloc_sample(\n\t\t\t    sopts, dopts, tsd, usize, ind);\n\t\t\talloc_ctx.slab = false;\n\t\t} else {\n\t\t\tallocation = NULL;\n\t\t}\n\n\t\tif (unlikely(allocation == NULL)) {\n\t\t\tprof_alloc_rollback(tsd, tctx, true);\n\t\t\tgoto label_oom;\n\t\t}\n\t\tprof_malloc(tsd_tsdn(tsd), allocation, usize, &alloc_ctx, tctx);\n\t} else {\n\t\t/*\n\t\t * If dopts->alignment > 0, then ind is still 0, but usize was\n\t\t * computed in the previous if statement.  Down the positive\n\t\t * alignment path, imalloc_no_sample ignores ind and size\n\t\t * (relying only on usize).\n\t\t */\n\t\tallocation = imalloc_no_sample(sopts, dopts, tsd, size, usize,\n\t\t    ind);\n\t\tif (unlikely(allocation == NULL)) {\n\t\t\tgoto label_oom;\n\t\t}\n\t}\n\n\t/*\n\t * Allocation has been done at this point.  We still have some\n\t * post-allocation work to do though.\n\t */\n\tassert(dopts->alignment == 0\n\t    || ((uintptr_t)allocation & (dopts->alignment - 1)) == ZU(0));\n\n\tif (config_stats) {\n\t\tassert(usize == isalloc(tsd_tsdn(tsd), allocation));\n\t\t*tsd_thread_allocatedp_get(tsd) += usize;\n\t}\n\n\tif (sopts->slow) {\n\t\tUTRACE(0, size, allocation);\n\t}\n\n\t/* Success! */\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\t*dopts->result = allocation;\n\treturn 0;\n\nlabel_oom:\n\tif (unlikely(sopts->slow) && config_xmalloc && unlikely(opt_xmalloc)) {\n\t\tmalloc_write(sopts->oom_string);\n\t\tabort();\n\t}\n\n\tif (sopts->slow) {\n\t\tUTRACE(NULL, size, NULL);\n\t}\n\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\n\tif (sopts->set_errno_on_error) {\n\t\tset_errno(ENOMEM);\n\t}\n\n\tif (sopts->null_out_result_on_error) {\n\t\t*dopts->result = NULL;\n\t}\n\n\treturn ENOMEM;\n\n\t/*\n\t * This label is only jumped to by one goto; we move it out of line\n\t * anyways to avoid obscuring the non-error paths, and for symmetry with\n\t * the oom case.\n\t */\nlabel_invalid_alignment:\n\tif (config_xmalloc && unlikely(opt_xmalloc)) {\n\t\tmalloc_write(sopts->invalid_alignment_string);\n\t\tabort();\n\t}\n\n\tif (sopts->set_errno_on_error) {\n\t\tset_errno(EINVAL);\n\t}\n\n\tif (sopts->slow) {\n\t\tUTRACE(NULL, size, NULL);\n\t}\n\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\n\tif (sopts->null_out_result_on_error) {\n\t\t*dopts->result = NULL;\n\t}\n\n\treturn EINVAL;\n}\n\n/* Returns the errno-style error code of the allocation. */\nJEMALLOC_ALWAYS_INLINE int\nimalloc(static_opts_t *sopts, dynamic_opts_t *dopts) {\n\tif (unlikely(!malloc_initialized()) && unlikely(malloc_init())) {\n\t\tif (config_xmalloc && unlikely(opt_xmalloc)) {\n\t\t\tmalloc_write(sopts->oom_string);\n\t\t\tabort();\n\t\t}\n\t\tUTRACE(NULL, dopts->num_items * dopts->item_size, NULL);\n\t\tset_errno(ENOMEM);\n\t\t*dopts->result = NULL;\n\n\t\treturn ENOMEM;\n\t}\n\n\t/* We always need the tsd.  Let's grab it right away. */\n\ttsd_t *tsd = tsd_fetch();\n\tassert(tsd);\n\tif (likely(tsd_fast(tsd))) {\n\t\t/* Fast and common path. */\n\t\ttsd_assert_fast(tsd);\n\t\tsopts->slow = false;\n\t\treturn imalloc_body(sopts, dopts, tsd);\n\t} else {\n\t\tsopts->slow = true;\n\t\treturn imalloc_body(sopts, dopts, tsd);\n\t}\n}\n/******************************************************************************/\n/*\n * Begin malloc(3)-compatible functions.\n */\n\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\nvoid JEMALLOC_NOTHROW *\nJEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE(1)\nje_malloc(size_t size) {\n\tvoid *ret;\n\tstatic_opts_t sopts;\n\tdynamic_opts_t dopts;\n\n\tstatic_opts_init(&sopts);\n\tdynamic_opts_init(&dopts);\n\n\tsopts.bump_empty_alloc = true;\n\tsopts.null_out_result_on_error = true;\n\tsopts.set_errno_on_error = true;\n\tsopts.oom_string = \"<jemalloc>: Error in malloc(): out of memory\\n\";\n\n\tdopts.result = &ret;\n\tdopts.num_items = 1;\n\tdopts.item_size = size;\n\n\timalloc(&sopts, &dopts);\n\n\treturn ret;\n}\n\nJEMALLOC_EXPORT int JEMALLOC_NOTHROW\nJEMALLOC_ATTR(nonnull(1))\nje_posix_memalign(void **memptr, size_t alignment, size_t size) {\n\tint ret;\n\tstatic_opts_t sopts;\n\tdynamic_opts_t dopts;\n\n\tstatic_opts_init(&sopts);\n\tdynamic_opts_init(&dopts);\n\n\tsopts.bump_empty_alloc = true;\n\tsopts.min_alignment = sizeof(void *);\n\tsopts.oom_string =\n\t    \"<jemalloc>: Error allocating aligned memory: out of memory\\n\";\n\tsopts.invalid_alignment_string =\n\t    \"<jemalloc>: Error allocating aligned memory: invalid alignment\\n\";\n\n\tdopts.result = memptr;\n\tdopts.num_items = 1;\n\tdopts.item_size = size;\n\tdopts.alignment = alignment;\n\n\tret = imalloc(&sopts, &dopts);\n\treturn ret;\n}\n\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\nvoid JEMALLOC_NOTHROW *\nJEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE(2)\nje_aligned_alloc(size_t alignment, size_t size) {\n\tvoid *ret;\n\n\tstatic_opts_t sopts;\n\tdynamic_opts_t dopts;\n\n\tstatic_opts_init(&sopts);\n\tdynamic_opts_init(&dopts);\n\n\tsopts.bump_empty_alloc = true;\n\tsopts.null_out_result_on_error = true;\n\tsopts.set_errno_on_error = true;\n\tsopts.min_alignment = 1;\n\tsopts.oom_string =\n\t    \"<jemalloc>: Error allocating aligned memory: out of memory\\n\";\n\tsopts.invalid_alignment_string =\n\t    \"<jemalloc>: Error allocating aligned memory: invalid alignment\\n\";\n\n\tdopts.result = &ret;\n\tdopts.num_items = 1;\n\tdopts.item_size = size;\n\tdopts.alignment = alignment;\n\n\timalloc(&sopts, &dopts);\n\treturn ret;\n}\n\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\nvoid JEMALLOC_NOTHROW *\nJEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE2(1, 2)\nje_calloc(size_t num, size_t size) {\n\tvoid *ret;\n\tstatic_opts_t sopts;\n\tdynamic_opts_t dopts;\n\n\tstatic_opts_init(&sopts);\n\tdynamic_opts_init(&dopts);\n\n\tsopts.may_overflow = true;\n\tsopts.bump_empty_alloc = true;\n\tsopts.null_out_result_on_error = true;\n\tsopts.set_errno_on_error = true;\n\tsopts.oom_string = \"<jemalloc>: Error in calloc(): out of memory\\n\";\n\n\tdopts.result = &ret;\n\tdopts.num_items = num;\n\tdopts.item_size = size;\n\tdopts.zero = true;\n\n\timalloc(&sopts, &dopts);\n\n\treturn ret;\n}\n\nstatic void *\nirealloc_prof_sample(tsd_t *tsd, void *old_ptr, size_t old_usize, size_t usize,\n    prof_tctx_t *tctx) {\n\tvoid *p;\n\n\tif (tctx == NULL) {\n\t\treturn NULL;\n\t}\n\tif (usize <= SMALL_MAXCLASS) {\n\t\tp = iralloc(tsd, old_ptr, old_usize, LARGE_MINCLASS, 0, false);\n\t\tif (p == NULL) {\n\t\t\treturn NULL;\n\t\t}\n\t\tarena_prof_promote(tsd_tsdn(tsd), p, usize);\n\t} else {\n\t\tp = iralloc(tsd, old_ptr, old_usize, usize, 0, false);\n\t}\n\n\treturn p;\n}\n\nJEMALLOC_ALWAYS_INLINE void *\nirealloc_prof(tsd_t *tsd, void *old_ptr, size_t old_usize, size_t usize,\n   alloc_ctx_t *alloc_ctx) {\n\tvoid *p;\n\tbool prof_active;\n\tprof_tctx_t *old_tctx, *tctx;\n\n\tprof_active = prof_active_get_unlocked();\n\told_tctx = prof_tctx_get(tsd_tsdn(tsd), old_ptr, alloc_ctx);\n\ttctx = prof_alloc_prep(tsd, usize, prof_active, true);\n\tif (unlikely((uintptr_t)tctx != (uintptr_t)1U)) {\n\t\tp = irealloc_prof_sample(tsd, old_ptr, old_usize, usize, tctx);\n\t} else {\n\t\tp = iralloc(tsd, old_ptr, old_usize, usize, 0, false);\n\t}\n\tif (unlikely(p == NULL)) {\n\t\tprof_alloc_rollback(tsd, tctx, true);\n\t\treturn NULL;\n\t}\n\tprof_realloc(tsd, p, usize, tctx, prof_active, true, old_ptr, old_usize,\n\t    old_tctx);\n\n\treturn p;\n}\n\nJEMALLOC_ALWAYS_INLINE void\nifree(tsd_t *tsd, void *ptr, tcache_t *tcache, bool slow_path) {\n\tif (!slow_path) {\n\t\ttsd_assert_fast(tsd);\n\t}\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\tif (tsd_reentrancy_level_get(tsd) != 0) {\n\t\tassert(slow_path);\n\t}\n\n\tassert(ptr != NULL);\n\tassert(malloc_initialized() || IS_INITIALIZER);\n\n\talloc_ctx_t alloc_ctx;\n\trtree_ctx_t *rtree_ctx = tsd_rtree_ctx(tsd);\n\trtree_szind_slab_read(tsd_tsdn(tsd), &extents_rtree, rtree_ctx,\n\t    (uintptr_t)ptr, true, &alloc_ctx.szind, &alloc_ctx.slab);\n\tassert(alloc_ctx.szind != NSIZES);\n\n\tsize_t usize;\n\tif (config_prof && opt_prof) {\n\t\tusize = sz_index2size(alloc_ctx.szind);\n\t\tprof_free(tsd, ptr, usize, &alloc_ctx);\n\t} else if (config_stats) {\n\t\tusize = sz_index2size(alloc_ctx.szind);\n\t}\n\tif (config_stats) {\n\t\t*tsd_thread_deallocatedp_get(tsd) += usize;\n\t}\n\n\tif (likely(!slow_path)) {\n\t\tidalloctm(tsd_tsdn(tsd), ptr, tcache, &alloc_ctx, false,\n\t\t    false);\n\t} else {\n\t\tidalloctm(tsd_tsdn(tsd), ptr, tcache, &alloc_ctx, false,\n\t\t    true);\n\t}\n}\n\nJEMALLOC_ALWAYS_INLINE void\nisfree(tsd_t *tsd, void *ptr, size_t usize, tcache_t *tcache, bool slow_path) {\n\tif (!slow_path) {\n\t\ttsd_assert_fast(tsd);\n\t}\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\tif (tsd_reentrancy_level_get(tsd) != 0) {\n\t\tassert(slow_path);\n\t}\n\n\tassert(ptr != NULL);\n\tassert(malloc_initialized() || IS_INITIALIZER);\n\n\talloc_ctx_t alloc_ctx, *ctx;\n\tif (config_prof && opt_prof) {\n\t\trtree_ctx_t *rtree_ctx = tsd_rtree_ctx(tsd);\n\t\trtree_szind_slab_read(tsd_tsdn(tsd), &extents_rtree, rtree_ctx,\n\t\t    (uintptr_t)ptr, true, &alloc_ctx.szind, &alloc_ctx.slab);\n\t\tassert(alloc_ctx.szind == sz_size2index(usize));\n\t\tctx = &alloc_ctx;\n\t\tprof_free(tsd, ptr, usize, ctx);\n\t} else {\n\t\tctx = NULL;\n\t}\n\n\tif (config_stats) {\n\t\t*tsd_thread_deallocatedp_get(tsd) += usize;\n\t}\n\n\tif (likely(!slow_path)) {\n\t\tisdalloct(tsd_tsdn(tsd), ptr, usize, tcache, ctx, false);\n\t} else {\n\t\tisdalloct(tsd_tsdn(tsd), ptr, usize, tcache, ctx, true);\n\t}\n}\n\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\nvoid JEMALLOC_NOTHROW *\nJEMALLOC_ALLOC_SIZE(2)\nje_realloc(void *ptr, size_t size) {\n\tvoid *ret;\n\ttsdn_t *tsdn JEMALLOC_CC_SILENCE_INIT(NULL);\n\tsize_t usize JEMALLOC_CC_SILENCE_INIT(0);\n\tsize_t old_usize = 0;\n\n\tif (unlikely(size == 0)) {\n\t\tif (ptr != NULL) {\n\t\t\t/* realloc(ptr, 0) is equivalent to free(ptr). */\n\t\t\tUTRACE(ptr, 0, 0);\n\t\t\ttcache_t *tcache;\n\t\t\ttsd_t *tsd = tsd_fetch();\n\t\t\tif (tsd_reentrancy_level_get(tsd) == 0) {\n\t\t\t\ttcache = tcache_get(tsd);\n\t\t\t} else {\n\t\t\t\ttcache = NULL;\n\t\t\t}\n\t\t\tifree(tsd, ptr, tcache, true);\n\t\t\treturn NULL;\n\t\t}\n\t\tsize = 1;\n\t}\n\n\tif (likely(ptr != NULL)) {\n\t\tassert(malloc_initialized() || IS_INITIALIZER);\n\t\ttsd_t *tsd = tsd_fetch();\n\n\t\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\n\t\talloc_ctx_t alloc_ctx;\n\t\trtree_ctx_t *rtree_ctx = tsd_rtree_ctx(tsd);\n\t\trtree_szind_slab_read(tsd_tsdn(tsd), &extents_rtree, rtree_ctx,\n\t\t    (uintptr_t)ptr, true, &alloc_ctx.szind, &alloc_ctx.slab);\n\t\tassert(alloc_ctx.szind != NSIZES);\n\t\told_usize = sz_index2size(alloc_ctx.szind);\n\t\tassert(old_usize == isalloc(tsd_tsdn(tsd), ptr));\n\t\tif (config_prof && opt_prof) {\n\t\t\tusize = sz_s2u(size);\n\t\t\tret = unlikely(usize == 0 || usize > LARGE_MAXCLASS) ?\n\t\t\t    NULL : irealloc_prof(tsd, ptr, old_usize, usize,\n\t\t\t    &alloc_ctx);\n\t\t} else {\n\t\t\tif (config_stats) {\n\t\t\t\tusize = sz_s2u(size);\n\t\t\t}\n\t\t\tret = iralloc(tsd, ptr, old_usize, size, 0, false);\n\t\t}\n\t\ttsdn = tsd_tsdn(tsd);\n\t} else {\n\t\t/* realloc(NULL, size) is equivalent to malloc(size). */\n\t\treturn je_malloc(size);\n\t}\n\n\tif (unlikely(ret == NULL)) {\n\t\tif (config_xmalloc && unlikely(opt_xmalloc)) {\n\t\t\tmalloc_write(\"<jemalloc>: Error in realloc(): \"\n\t\t\t    \"out of memory\\n\");\n\t\t\tabort();\n\t\t}\n\t\tset_errno(ENOMEM);\n\t}\n\tif (config_stats && likely(ret != NULL)) {\n\t\ttsd_t *tsd;\n\n\t\tassert(usize == isalloc(tsdn, ret));\n\t\ttsd = tsdn_tsd(tsdn);\n\t\t*tsd_thread_allocatedp_get(tsd) += usize;\n\t\t*tsd_thread_deallocatedp_get(tsd) += old_usize;\n\t}\n\tUTRACE(ptr, size, ret);\n\tcheck_entry_exit_locking(tsdn);\n\treturn ret;\n}\n\nJEMALLOC_EXPORT void JEMALLOC_NOTHROW\nje_free(void *ptr) {\n\tUTRACE(ptr, 0, 0);\n\tif (likely(ptr != NULL)) {\n\t\t/*\n\t\t * We avoid setting up tsd fully (e.g. tcache, arena binding)\n\t\t * based on only free() calls -- other activities trigger the\n\t\t * minimal to full transition.  This is because free() may\n\t\t * happen during thread shutdown after tls deallocation: if a\n\t\t * thread never had any malloc activities until then, a\n\t\t * fully-setup tsd won't be destructed properly.\n\t\t */\n\t\ttsd_t *tsd = tsd_fetch_min();\n\t\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\n\t\ttcache_t *tcache;\n\t\tif (likely(tsd_fast(tsd))) {\n\t\t\ttsd_assert_fast(tsd);\n\t\t\t/* Unconditionally get tcache ptr on fast path. */\n\t\t\ttcache = tsd_tcachep_get(tsd);\n\t\t\tifree(tsd, ptr, tcache, false);\n\t\t} else {\n\t\t\tif (likely(tsd_reentrancy_level_get(tsd) == 0)) {\n\t\t\t\ttcache = tcache_get(tsd);\n\t\t\t} else {\n\t\t\t\ttcache = NULL;\n\t\t\t}\n\t\t\tifree(tsd, ptr, tcache, true);\n\t\t}\n\t\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\t}\n}\n\n/*\n * End malloc(3)-compatible functions.\n */\n/******************************************************************************/\n/*\n * Begin non-standard override functions.\n */\n\n#ifdef JEMALLOC_OVERRIDE_MEMALIGN\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\nvoid JEMALLOC_NOTHROW *\nJEMALLOC_ATTR(malloc)\nje_memalign(size_t alignment, size_t size) {\n\tvoid *ret;\n\tstatic_opts_t sopts;\n\tdynamic_opts_t dopts;\n\n\tstatic_opts_init(&sopts);\n\tdynamic_opts_init(&dopts);\n\n\tsopts.bump_empty_alloc = true;\n\tsopts.min_alignment = 1;\n\tsopts.oom_string =\n\t    \"<jemalloc>: Error allocating aligned memory: out of memory\\n\";\n\tsopts.invalid_alignment_string =\n\t    \"<jemalloc>: Error allocating aligned memory: invalid alignment\\n\";\n\tsopts.null_out_result_on_error = true;\n\n\tdopts.result = &ret;\n\tdopts.num_items = 1;\n\tdopts.item_size = size;\n\tdopts.alignment = alignment;\n\n\timalloc(&sopts, &dopts);\n\treturn ret;\n}\n#endif\n\n#ifdef JEMALLOC_OVERRIDE_VALLOC\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\nvoid JEMALLOC_NOTHROW *\nJEMALLOC_ATTR(malloc)\nje_valloc(size_t size) {\n\tvoid *ret;\n\n\tstatic_opts_t sopts;\n\tdynamic_opts_t dopts;\n\n\tstatic_opts_init(&sopts);\n\tdynamic_opts_init(&dopts);\n\n\tsopts.bump_empty_alloc = true;\n\tsopts.null_out_result_on_error = true;\n\tsopts.min_alignment = PAGE;\n\tsopts.oom_string =\n\t    \"<jemalloc>: Error allocating aligned memory: out of memory\\n\";\n\tsopts.invalid_alignment_string =\n\t    \"<jemalloc>: Error allocating aligned memory: invalid alignment\\n\";\n\n\tdopts.result = &ret;\n\tdopts.num_items = 1;\n\tdopts.item_size = size;\n\tdopts.alignment = PAGE;\n\n\timalloc(&sopts, &dopts);\n\n\treturn ret;\n}\n#endif\n\n#if defined(JEMALLOC_IS_MALLOC) && defined(JEMALLOC_GLIBC_MALLOC_HOOK)\n/*\n * glibc provides the RTLD_DEEPBIND flag for dlopen which can make it possible\n * to inconsistently reference libc's malloc(3)-compatible functions\n * (https://bugzilla.mozilla.org/show_bug.cgi?id=493541).\n *\n * These definitions interpose hooks in glibc.  The functions are actually\n * passed an extra argument for the caller return address, which will be\n * ignored.\n */\nJEMALLOC_EXPORT void (*__free_hook)(void *ptr) = je_free;\nJEMALLOC_EXPORT void *(*__malloc_hook)(size_t size) = je_malloc;\nJEMALLOC_EXPORT void *(*__realloc_hook)(void *ptr, size_t size) = je_realloc;\n#  ifdef JEMALLOC_GLIBC_MEMALIGN_HOOK\nJEMALLOC_EXPORT void *(*__memalign_hook)(size_t alignment, size_t size) =\n    je_memalign;\n#  endif\n\n#  ifdef CPU_COUNT\n/*\n * To enable static linking with glibc, the libc specific malloc interface must\n * be implemented also, so none of glibc's malloc.o functions are added to the\n * link.\n */\n#    define ALIAS(je_fn)\t__attribute__((alias (#je_fn), used))\n/* To force macro expansion of je_ prefix before stringification. */\n#    define PREALIAS(je_fn)\tALIAS(je_fn)\n#    ifdef JEMALLOC_OVERRIDE___LIBC_CALLOC\nvoid *__libc_calloc(size_t n, size_t size) PREALIAS(je_calloc);\n#    endif\n#    ifdef JEMALLOC_OVERRIDE___LIBC_FREE\nvoid __libc_free(void* ptr) PREALIAS(je_free);\n#    endif\n#    ifdef JEMALLOC_OVERRIDE___LIBC_MALLOC\nvoid *__libc_malloc(size_t size) PREALIAS(je_malloc);\n#    endif\n#    ifdef JEMALLOC_OVERRIDE___LIBC_MEMALIGN\nvoid *__libc_memalign(size_t align, size_t s) PREALIAS(je_memalign);\n#    endif\n#    ifdef JEMALLOC_OVERRIDE___LIBC_REALLOC\nvoid *__libc_realloc(void* ptr, size_t size) PREALIAS(je_realloc);\n#    endif\n#    ifdef JEMALLOC_OVERRIDE___LIBC_VALLOC\nvoid *__libc_valloc(size_t size) PREALIAS(je_valloc);\n#    endif\n#    ifdef JEMALLOC_OVERRIDE___POSIX_MEMALIGN\nint __posix_memalign(void** r, size_t a, size_t s) PREALIAS(je_posix_memalign);\n#    endif\n#    undef PREALIAS\n#    undef ALIAS\n#  endif\n#endif\n\n/*\n * End non-standard override functions.\n */\n/******************************************************************************/\n/*\n * Begin non-standard functions.\n */\n\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\nvoid JEMALLOC_NOTHROW *\nJEMALLOC_ATTR(malloc) JEMALLOC_ALLOC_SIZE(1)\nje_mallocx(size_t size, int flags) {\n\tvoid *ret;\n\tstatic_opts_t sopts;\n\tdynamic_opts_t dopts;\n\n\tstatic_opts_init(&sopts);\n\tdynamic_opts_init(&dopts);\n\n\tsopts.assert_nonempty_alloc = true;\n\tsopts.null_out_result_on_error = true;\n\tsopts.oom_string = \"<jemalloc>: Error in mallocx(): out of memory\\n\";\n\n\tdopts.result = &ret;\n\tdopts.num_items = 1;\n\tdopts.item_size = size;\n\tif (unlikely(flags != 0)) {\n\t\tif ((flags & MALLOCX_LG_ALIGN_MASK) != 0) {\n\t\t\tdopts.alignment = MALLOCX_ALIGN_GET_SPECIFIED(flags);\n\t\t}\n\n\t\tdopts.zero = MALLOCX_ZERO_GET(flags);\n\n\t\tif ((flags & MALLOCX_TCACHE_MASK) != 0) {\n\t\t\tif ((flags & MALLOCX_TCACHE_MASK)\n\t\t\t    == MALLOCX_TCACHE_NONE) {\n\t\t\t\tdopts.tcache_ind = TCACHE_IND_NONE;\n\t\t\t} else {\n\t\t\t\tdopts.tcache_ind = MALLOCX_TCACHE_GET(flags);\n\t\t\t}\n\t\t} else {\n\t\t\tdopts.tcache_ind = TCACHE_IND_AUTOMATIC;\n\t\t}\n\n\t\tif ((flags & MALLOCX_ARENA_MASK) != 0)\n\t\t\tdopts.arena_ind = MALLOCX_ARENA_GET(flags);\n\t}\n\n\timalloc(&sopts, &dopts);\n\treturn ret;\n}\n\nstatic void *\nirallocx_prof_sample(tsdn_t *tsdn, void *old_ptr, size_t old_usize,\n    size_t usize, size_t alignment, bool zero, tcache_t *tcache, arena_t *arena,\n    prof_tctx_t *tctx) {\n\tvoid *p;\n\n\tif (tctx == NULL) {\n\t\treturn NULL;\n\t}\n\tif (usize <= SMALL_MAXCLASS) {\n\t\tp = iralloct(tsdn, old_ptr, old_usize, LARGE_MINCLASS,\n\t\t    alignment, zero, tcache, arena);\n\t\tif (p == NULL) {\n\t\t\treturn NULL;\n\t\t}\n\t\tarena_prof_promote(tsdn, p, usize);\n\t} else {\n\t\tp = iralloct(tsdn, old_ptr, old_usize, usize, alignment, zero,\n\t\t    tcache, arena);\n\t}\n\n\treturn p;\n}\n\nJEMALLOC_ALWAYS_INLINE void *\nirallocx_prof(tsd_t *tsd, void *old_ptr, size_t old_usize, size_t size,\n    size_t alignment, size_t *usize, bool zero, tcache_t *tcache,\n    arena_t *arena, alloc_ctx_t *alloc_ctx) {\n\tvoid *p;\n\tbool prof_active;\n\tprof_tctx_t *old_tctx, *tctx;\n\n\tprof_active = prof_active_get_unlocked();\n\told_tctx = prof_tctx_get(tsd_tsdn(tsd), old_ptr, alloc_ctx);\n\ttctx = prof_alloc_prep(tsd, *usize, prof_active, false);\n\tif (unlikely((uintptr_t)tctx != (uintptr_t)1U)) {\n\t\tp = irallocx_prof_sample(tsd_tsdn(tsd), old_ptr, old_usize,\n\t\t    *usize, alignment, zero, tcache, arena, tctx);\n\t} else {\n\t\tp = iralloct(tsd_tsdn(tsd), old_ptr, old_usize, size, alignment,\n\t\t    zero, tcache, arena);\n\t}\n\tif (unlikely(p == NULL)) {\n\t\tprof_alloc_rollback(tsd, tctx, false);\n\t\treturn NULL;\n\t}\n\n\tif (p == old_ptr && alignment != 0) {\n\t\t/*\n\t\t * The allocation did not move, so it is possible that the size\n\t\t * class is smaller than would guarantee the requested\n\t\t * alignment, and that the alignment constraint was\n\t\t * serendipitously satisfied.  Additionally, old_usize may not\n\t\t * be the same as the current usize because of in-place large\n\t\t * reallocation.  Therefore, query the actual value of usize.\n\t\t */\n\t\t*usize = isalloc(tsd_tsdn(tsd), p);\n\t}\n\tprof_realloc(tsd, p, *usize, tctx, prof_active, false, old_ptr,\n\t    old_usize, old_tctx);\n\n\treturn p;\n}\n\nJEMALLOC_EXPORT JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN\nvoid JEMALLOC_NOTHROW *\nJEMALLOC_ALLOC_SIZE(2)\nje_rallocx(void *ptr, size_t size, int flags) {\n\tvoid *p;\n\ttsd_t *tsd;\n\tsize_t usize;\n\tsize_t old_usize;\n\tsize_t alignment = MALLOCX_ALIGN_GET(flags);\n\tbool zero = flags & MALLOCX_ZERO;\n\tarena_t *arena;\n\ttcache_t *tcache;\n\n\tassert(ptr != NULL);\n\tassert(size != 0);\n\tassert(malloc_initialized() || IS_INITIALIZER);\n\ttsd = tsd_fetch();\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\n\tif (unlikely((flags & MALLOCX_ARENA_MASK) != 0)) {\n\t\tunsigned arena_ind = MALLOCX_ARENA_GET(flags);\n\t\tarena = arena_get(tsd_tsdn(tsd), arena_ind, true);\n\t\tif (unlikely(arena == NULL)) {\n\t\t\tgoto label_oom;\n\t\t}\n\t} else {\n\t\tarena = NULL;\n\t}\n\n\tif (unlikely((flags & MALLOCX_TCACHE_MASK) != 0)) {\n\t\tif ((flags & MALLOCX_TCACHE_MASK) == MALLOCX_TCACHE_NONE) {\n\t\t\ttcache = NULL;\n\t\t} else {\n\t\t\ttcache = tcaches_get(tsd, MALLOCX_TCACHE_GET(flags));\n\t\t}\n\t} else {\n\t\ttcache = tcache_get(tsd);\n\t}\n\n\talloc_ctx_t alloc_ctx;\n\trtree_ctx_t *rtree_ctx = tsd_rtree_ctx(tsd);\n\trtree_szind_slab_read(tsd_tsdn(tsd), &extents_rtree, rtree_ctx,\n\t    (uintptr_t)ptr, true, &alloc_ctx.szind, &alloc_ctx.slab);\n\tassert(alloc_ctx.szind != NSIZES);\n\told_usize = sz_index2size(alloc_ctx.szind);\n\tassert(old_usize == isalloc(tsd_tsdn(tsd), ptr));\n\tif (config_prof && opt_prof) {\n\t\tusize = (alignment == 0) ?\n\t\t    sz_s2u(size) : sz_sa2u(size, alignment);\n\t\tif (unlikely(usize == 0 || usize > LARGE_MAXCLASS)) {\n\t\t\tgoto label_oom;\n\t\t}\n\t\tp = irallocx_prof(tsd, ptr, old_usize, size, alignment, &usize,\n\t\t    zero, tcache, arena, &alloc_ctx);\n\t\tif (unlikely(p == NULL)) {\n\t\t\tgoto label_oom;\n\t\t}\n\t} else {\n\t\tp = iralloct(tsd_tsdn(tsd), ptr, old_usize, size, alignment,\n\t\t    zero, tcache, arena);\n\t\tif (unlikely(p == NULL)) {\n\t\t\tgoto label_oom;\n\t\t}\n\t\tif (config_stats) {\n\t\t\tusize = isalloc(tsd_tsdn(tsd), p);\n\t\t}\n\t}\n\tassert(alignment == 0 || ((uintptr_t)p & (alignment - 1)) == ZU(0));\n\n\tif (config_stats) {\n\t\t*tsd_thread_allocatedp_get(tsd) += usize;\n\t\t*tsd_thread_deallocatedp_get(tsd) += old_usize;\n\t}\n\tUTRACE(ptr, size, p);\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\treturn p;\nlabel_oom:\n\tif (config_xmalloc && unlikely(opt_xmalloc)) {\n\t\tmalloc_write(\"<jemalloc>: Error in rallocx(): out of memory\\n\");\n\t\tabort();\n\t}\n\tUTRACE(ptr, size, 0);\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\treturn NULL;\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\nixallocx_helper(tsdn_t *tsdn, void *ptr, size_t old_usize, size_t size,\n    size_t extra, size_t alignment, bool zero) {\n\tsize_t usize;\n\n\tif (ixalloc(tsdn, ptr, old_usize, size, extra, alignment, zero)) {\n\t\treturn old_usize;\n\t}\n\tusize = isalloc(tsdn, ptr);\n\n\treturn usize;\n}\n\nstatic size_t\nixallocx_prof_sample(tsdn_t *tsdn, void *ptr, size_t old_usize, size_t size,\n    size_t extra, size_t alignment, bool zero, prof_tctx_t *tctx) {\n\tsize_t usize;\n\n\tif (tctx == NULL) {\n\t\treturn old_usize;\n\t}\n\tusize = ixallocx_helper(tsdn, ptr, old_usize, size, extra, alignment,\n\t    zero);\n\n\treturn usize;\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\nixallocx_prof(tsd_t *tsd, void *ptr, size_t old_usize, size_t size,\n    size_t extra, size_t alignment, bool zero, alloc_ctx_t *alloc_ctx) {\n\tsize_t usize_max, usize;\n\tbool prof_active;\n\tprof_tctx_t *old_tctx, *tctx;\n\n\tprof_active = prof_active_get_unlocked();\n\told_tctx = prof_tctx_get(tsd_tsdn(tsd), ptr, alloc_ctx);\n\t/*\n\t * usize isn't knowable before ixalloc() returns when extra is non-zero.\n\t * Therefore, compute its maximum possible value and use that in\n\t * prof_alloc_prep() to decide whether to capture a backtrace.\n\t * prof_realloc() will use the actual usize to decide whether to sample.\n\t */\n\tif (alignment == 0) {\n\t\tusize_max = sz_s2u(size+extra);\n\t\tassert(usize_max > 0 && usize_max <= LARGE_MAXCLASS);\n\t} else {\n\t\tusize_max = sz_sa2u(size+extra, alignment);\n\t\tif (unlikely(usize_max == 0 || usize_max > LARGE_MAXCLASS)) {\n\t\t\t/*\n\t\t\t * usize_max is out of range, and chances are that\n\t\t\t * allocation will fail, but use the maximum possible\n\t\t\t * value and carry on with prof_alloc_prep(), just in\n\t\t\t * case allocation succeeds.\n\t\t\t */\n\t\t\tusize_max = LARGE_MAXCLASS;\n\t\t}\n\t}\n\ttctx = prof_alloc_prep(tsd, usize_max, prof_active, false);\n\n\tif (unlikely((uintptr_t)tctx != (uintptr_t)1U)) {\n\t\tusize = ixallocx_prof_sample(tsd_tsdn(tsd), ptr, old_usize,\n\t\t    size, extra, alignment, zero, tctx);\n\t} else {\n\t\tusize = ixallocx_helper(tsd_tsdn(tsd), ptr, old_usize, size,\n\t\t    extra, alignment, zero);\n\t}\n\tif (usize == old_usize) {\n\t\tprof_alloc_rollback(tsd, tctx, false);\n\t\treturn usize;\n\t}\n\tprof_realloc(tsd, ptr, usize, tctx, prof_active, false, ptr, old_usize,\n\t    old_tctx);\n\n\treturn usize;\n}\n\nJEMALLOC_EXPORT size_t JEMALLOC_NOTHROW\nje_xallocx(void *ptr, size_t size, size_t extra, int flags) {\n\ttsd_t *tsd;\n\tsize_t usize, old_usize;\n\tsize_t alignment = MALLOCX_ALIGN_GET(flags);\n\tbool zero = flags & MALLOCX_ZERO;\n\n\tassert(ptr != NULL);\n\tassert(size != 0);\n\tassert(SIZE_T_MAX - size >= extra);\n\tassert(malloc_initialized() || IS_INITIALIZER);\n\ttsd = tsd_fetch();\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\n\talloc_ctx_t alloc_ctx;\n\trtree_ctx_t *rtree_ctx = tsd_rtree_ctx(tsd);\n\trtree_szind_slab_read(tsd_tsdn(tsd), &extents_rtree, rtree_ctx,\n\t    (uintptr_t)ptr, true, &alloc_ctx.szind, &alloc_ctx.slab);\n\tassert(alloc_ctx.szind != NSIZES);\n\told_usize = sz_index2size(alloc_ctx.szind);\n\tassert(old_usize == isalloc(tsd_tsdn(tsd), ptr));\n\t/*\n\t * The API explicitly absolves itself of protecting against (size +\n\t * extra) numerical overflow, but we may need to clamp extra to avoid\n\t * exceeding LARGE_MAXCLASS.\n\t *\n\t * Ordinarily, size limit checking is handled deeper down, but here we\n\t * have to check as part of (size + extra) clamping, since we need the\n\t * clamped value in the above helper functions.\n\t */\n\tif (unlikely(size > LARGE_MAXCLASS)) {\n\t\tusize = old_usize;\n\t\tgoto label_not_resized;\n\t}\n\tif (unlikely(LARGE_MAXCLASS - size < extra)) {\n\t\textra = LARGE_MAXCLASS - size;\n\t}\n\n\tif (config_prof && opt_prof) {\n\t\tusize = ixallocx_prof(tsd, ptr, old_usize, size, extra,\n\t\t    alignment, zero, &alloc_ctx);\n\t} else {\n\t\tusize = ixallocx_helper(tsd_tsdn(tsd), ptr, old_usize, size,\n\t\t    extra, alignment, zero);\n\t}\n\tif (unlikely(usize == old_usize)) {\n\t\tgoto label_not_resized;\n\t}\n\n\tif (config_stats) {\n\t\t*tsd_thread_allocatedp_get(tsd) += usize;\n\t\t*tsd_thread_deallocatedp_get(tsd) += old_usize;\n\t}\nlabel_not_resized:\n\tUTRACE(ptr, size, ptr);\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\treturn usize;\n}\n\nJEMALLOC_EXPORT size_t JEMALLOC_NOTHROW\nJEMALLOC_ATTR(pure)\nje_sallocx(const void *ptr, int flags) {\n\tsize_t usize;\n\ttsdn_t *tsdn;\n\n\tassert(malloc_initialized() || IS_INITIALIZER);\n\tassert(ptr != NULL);\n\n\ttsdn = tsdn_fetch();\n\tcheck_entry_exit_locking(tsdn);\n\n\tif (config_debug || force_ivsalloc) {\n\t\tusize = ivsalloc(tsdn, ptr);\n\t\tassert(force_ivsalloc || usize != 0);\n\t} else {\n\t\tusize = isalloc(tsdn, ptr);\n\t}\n\n\tcheck_entry_exit_locking(tsdn);\n\treturn usize;\n}\n\nJEMALLOC_EXPORT void JEMALLOC_NOTHROW\nje_dallocx(void *ptr, int flags) {\n\tassert(ptr != NULL);\n\tassert(malloc_initialized() || IS_INITIALIZER);\n\n\ttsd_t *tsd = tsd_fetch();\n\tbool fast = tsd_fast(tsd);\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\n\ttcache_t *tcache;\n\tif (unlikely((flags & MALLOCX_TCACHE_MASK) != 0)) {\n\t\t/* Not allowed to be reentrant and specify a custom tcache. */\n\t\tassert(tsd_reentrancy_level_get(tsd) == 0);\n\t\tif ((flags & MALLOCX_TCACHE_MASK) == MALLOCX_TCACHE_NONE) {\n\t\t\ttcache = NULL;\n\t\t} else {\n\t\t\ttcache = tcaches_get(tsd, MALLOCX_TCACHE_GET(flags));\n\t\t}\n\t} else {\n\t\tif (likely(fast)) {\n\t\t\ttcache = tsd_tcachep_get(tsd);\n\t\t\tassert(tcache == tcache_get(tsd));\n\t\t} else {\n\t\t\tif (likely(tsd_reentrancy_level_get(tsd) == 0)) {\n\t\t\t\ttcache = tcache_get(tsd);\n\t\t\t}  else {\n\t\t\t\ttcache = NULL;\n\t\t\t}\n\t\t}\n\t}\n\n\tUTRACE(ptr, 0, 0);\n\tif (likely(fast)) {\n\t\ttsd_assert_fast(tsd);\n\t\tifree(tsd, ptr, tcache, false);\n\t} else {\n\t\tifree(tsd, ptr, tcache, true);\n\t}\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n}\n\nJEMALLOC_ALWAYS_INLINE size_t\ninallocx(tsdn_t *tsdn, size_t size, int flags) {\n\tcheck_entry_exit_locking(tsdn);\n\n\tsize_t usize;\n\tif (likely((flags & MALLOCX_LG_ALIGN_MASK) == 0)) {\n\t\tusize = sz_s2u(size);\n\t} else {\n\t\tusize = sz_sa2u(size, MALLOCX_ALIGN_GET_SPECIFIED(flags));\n\t}\n\tcheck_entry_exit_locking(tsdn);\n\treturn usize;\n}\n\nJEMALLOC_EXPORT void JEMALLOC_NOTHROW\nje_sdallocx(void *ptr, size_t size, int flags) {\n\tassert(ptr != NULL);\n\tassert(malloc_initialized() || IS_INITIALIZER);\n\n\ttsd_t *tsd = tsd_fetch();\n\tbool fast = tsd_fast(tsd);\n\tsize_t usize = inallocx(tsd_tsdn(tsd), size, flags);\n\tassert(usize == isalloc(tsd_tsdn(tsd), ptr));\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\n\ttcache_t *tcache;\n\tif (unlikely((flags & MALLOCX_TCACHE_MASK) != 0)) {\n\t\t/* Not allowed to be reentrant and specify a custom tcache. */\n\t\tassert(tsd_reentrancy_level_get(tsd) == 0);\n\t\tif ((flags & MALLOCX_TCACHE_MASK) == MALLOCX_TCACHE_NONE) {\n\t\t\ttcache = NULL;\n\t\t} else {\n\t\t\ttcache = tcaches_get(tsd, MALLOCX_TCACHE_GET(flags));\n\t\t}\n\t} else {\n\t\tif (likely(fast)) {\n\t\t\ttcache = tsd_tcachep_get(tsd);\n\t\t\tassert(tcache == tcache_get(tsd));\n\t\t} else {\n\t\t\tif (likely(tsd_reentrancy_level_get(tsd) == 0)) {\n\t\t\t\ttcache = tcache_get(tsd);\n\t\t\t} else {\n\t\t\t\ttcache = NULL;\n\t\t\t}\n\t\t}\n\t}\n\n\tUTRACE(ptr, 0, 0);\n\tif (likely(fast)) {\n\t\ttsd_assert_fast(tsd);\n\t\tisfree(tsd, ptr, usize, tcache, false);\n\t} else {\n\t\tisfree(tsd, ptr, usize, tcache, true);\n\t}\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n}\n\nJEMALLOC_EXPORT size_t JEMALLOC_NOTHROW\nJEMALLOC_ATTR(pure)\nje_nallocx(size_t size, int flags) {\n\tsize_t usize;\n\ttsdn_t *tsdn;\n\n\tassert(size != 0);\n\n\tif (unlikely(malloc_init())) {\n\t\treturn 0;\n\t}\n\n\ttsdn = tsdn_fetch();\n\tcheck_entry_exit_locking(tsdn);\n\n\tusize = inallocx(tsdn, size, flags);\n\tif (unlikely(usize > LARGE_MAXCLASS)) {\n\t\treturn 0;\n\t}\n\n\tcheck_entry_exit_locking(tsdn);\n\treturn usize;\n}\n\nJEMALLOC_EXPORT int JEMALLOC_NOTHROW\nje_mallctl(const char *name, void *oldp, size_t *oldlenp, void *newp,\n    size_t newlen) {\n\tint ret;\n\ttsd_t *tsd;\n\n\tif (unlikely(malloc_init())) {\n\t\treturn EAGAIN;\n\t}\n\n\ttsd = tsd_fetch();\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\tret = ctl_byname(tsd, name, oldp, oldlenp, newp, newlen);\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\treturn ret;\n}\n\nJEMALLOC_EXPORT int JEMALLOC_NOTHROW\nje_mallctlnametomib(const char *name, size_t *mibp, size_t *miblenp) {\n\tint ret;\n\ttsdn_t *tsdn;\n\n\tif (unlikely(malloc_init())) {\n\t\treturn EAGAIN;\n\t}\n\n\ttsdn = tsdn_fetch();\n\tcheck_entry_exit_locking(tsdn);\n\tret = ctl_nametomib(tsdn, name, mibp, miblenp);\n\tcheck_entry_exit_locking(tsdn);\n\treturn ret;\n}\n\nJEMALLOC_EXPORT int JEMALLOC_NOTHROW\nje_mallctlbymib(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp,\n  void *newp, size_t newlen) {\n\tint ret;\n\ttsd_t *tsd;\n\n\tif (unlikely(malloc_init())) {\n\t\treturn EAGAIN;\n\t}\n\n\ttsd = tsd_fetch();\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\tret = ctl_bymib(tsd, mib, miblen, oldp, oldlenp, newp, newlen);\n\tcheck_entry_exit_locking(tsd_tsdn(tsd));\n\treturn ret;\n}\n\nJEMALLOC_EXPORT void JEMALLOC_NOTHROW\nje_malloc_stats_print(void (*write_cb)(void *, const char *), void *cbopaque,\n    const char *opts) {\n\ttsdn_t *tsdn;\n\n\ttsdn = tsdn_fetch();\n\tcheck_entry_exit_locking(tsdn);\n\tstats_print(write_cb, cbopaque, opts);\n\tcheck_entry_exit_locking(tsdn);\n}\n\nJEMALLOC_EXPORT size_t JEMALLOC_NOTHROW\nje_malloc_usable_size(JEMALLOC_USABLE_SIZE_CONST void *ptr) {\n\tsize_t ret;\n\ttsdn_t *tsdn;\n\n\tassert(malloc_initialized() || IS_INITIALIZER);\n\n\ttsdn = tsdn_fetch();\n\tcheck_entry_exit_locking(tsdn);\n\n\tif (unlikely(ptr == NULL)) {\n\t\tret = 0;\n\t} else {\n\t\tif (config_debug || force_ivsalloc) {\n\t\t\tret = ivsalloc(tsdn, ptr);\n\t\t\tassert(force_ivsalloc || ret != 0);\n\t\t} else {\n\t\t\tret = isalloc(tsdn, ptr);\n\t\t}\n\t}\n\n\tcheck_entry_exit_locking(tsdn);\n\treturn ret;\n}\n\n/*\n * End non-standard functions.\n */\n/******************************************************************************/\n/*\n * The following functions are used by threading libraries for protection of\n * malloc during fork().\n */\n\n/*\n * If an application creates a thread before doing any allocation in the main\n * thread, then calls fork(2) in the main thread followed by memory allocation\n * in the child process, a race can occur that results in deadlock within the\n * child: the main thread may have forked while the created thread had\n * partially initialized the allocator.  Ordinarily jemalloc prevents\n * fork/malloc races via the following functions it registers during\n * initialization using pthread_atfork(), but of course that does no good if\n * the allocator isn't fully initialized at fork time.  The following library\n * constructor is a partial solution to this problem.  It may still be possible\n * to trigger the deadlock described above, but doing so would involve forking\n * via a library constructor that runs before jemalloc's runs.\n */\n#ifndef JEMALLOC_JET\nJEMALLOC_ATTR(constructor)\nstatic void\njemalloc_constructor(void) {\n\tmalloc_init();\n}\n#endif\n\n#ifndef JEMALLOC_MUTEX_INIT_CB\nvoid\njemalloc_prefork(void)\n#else\nJEMALLOC_EXPORT void\n_malloc_prefork(void)\n#endif\n{\n\ttsd_t *tsd;\n\tunsigned i, j, narenas;\n\tarena_t *arena;\n\n#ifdef JEMALLOC_MUTEX_INIT_CB\n\tif (!malloc_initialized()) {\n\t\treturn;\n\t}\n#endif\n\tassert(malloc_initialized());\n\n\ttsd = tsd_fetch();\n\n\tnarenas = narenas_total_get();\n\n\twitness_prefork(tsd_witness_tsdp_get(tsd));\n\t/* Acquire all mutexes in a safe order. */\n\tctl_prefork(tsd_tsdn(tsd));\n\ttcache_prefork(tsd_tsdn(tsd));\n\tmalloc_mutex_prefork(tsd_tsdn(tsd), &arenas_lock);\n\tif (have_background_thread) {\n\t\tbackground_thread_prefork0(tsd_tsdn(tsd));\n\t}\n\tprof_prefork0(tsd_tsdn(tsd));\n\tif (have_background_thread) {\n\t\tbackground_thread_prefork1(tsd_tsdn(tsd));\n\t}\n\t/* Break arena prefork into stages to preserve lock order. */\n\tfor (i = 0; i < 7; i++) {\n\t\tfor (j = 0; j < narenas; j++) {\n\t\t\tif ((arena = arena_get(tsd_tsdn(tsd), j, false)) !=\n\t\t\t    NULL) {\n\t\t\t\tswitch (i) {\n\t\t\t\tcase 0:\n\t\t\t\t\tarena_prefork0(tsd_tsdn(tsd), arena);\n\t\t\t\t\tbreak;\n\t\t\t\tcase 1:\n\t\t\t\t\tarena_prefork1(tsd_tsdn(tsd), arena);\n\t\t\t\t\tbreak;\n\t\t\t\tcase 2:\n\t\t\t\t\tarena_prefork2(tsd_tsdn(tsd), arena);\n\t\t\t\t\tbreak;\n\t\t\t\tcase 3:\n\t\t\t\t\tarena_prefork3(tsd_tsdn(tsd), arena);\n\t\t\t\t\tbreak;\n\t\t\t\tcase 4:\n\t\t\t\t\tarena_prefork4(tsd_tsdn(tsd), arena);\n\t\t\t\t\tbreak;\n\t\t\t\tcase 5:\n\t\t\t\t\tarena_prefork5(tsd_tsdn(tsd), arena);\n\t\t\t\t\tbreak;\n\t\t\t\tcase 6:\n\t\t\t\t\tarena_prefork6(tsd_tsdn(tsd), arena);\n\t\t\t\t\tbreak;\n\t\t\t\tdefault: not_reached();\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tprof_prefork1(tsd_tsdn(tsd));\n}\n\n#ifndef JEMALLOC_MUTEX_INIT_CB\nvoid\njemalloc_postfork_parent(void)\n#else\nJEMALLOC_EXPORT void\n_malloc_postfork(void)\n#endif\n{\n\ttsd_t *tsd;\n\tunsigned i, narenas;\n\n#ifdef JEMALLOC_MUTEX_INIT_CB\n\tif (!malloc_initialized()) {\n\t\treturn;\n\t}\n#endif\n\tassert(malloc_initialized());\n\n\ttsd = tsd_fetch();\n\n\twitness_postfork_parent(tsd_witness_tsdp_get(tsd));\n\t/* Release all mutexes, now that fork() has completed. */\n\tfor (i = 0, narenas = narenas_total_get(); i < narenas; i++) {\n\t\tarena_t *arena;\n\n\t\tif ((arena = arena_get(tsd_tsdn(tsd), i, false)) != NULL) {\n\t\t\tarena_postfork_parent(tsd_tsdn(tsd), arena);\n\t\t}\n\t}\n\tprof_postfork_parent(tsd_tsdn(tsd));\n\tif (have_background_thread) {\n\t\tbackground_thread_postfork_parent(tsd_tsdn(tsd));\n\t}\n\tmalloc_mutex_postfork_parent(tsd_tsdn(tsd), &arenas_lock);\n\ttcache_postfork_parent(tsd_tsdn(tsd));\n\tctl_postfork_parent(tsd_tsdn(tsd));\n}\n\nvoid\njemalloc_postfork_child(void) {\n\ttsd_t *tsd;\n\tunsigned i, narenas;\n\n\tassert(malloc_initialized());\n\n\ttsd = tsd_fetch();\n\n\twitness_postfork_child(tsd_witness_tsdp_get(tsd));\n\t/* Release all mutexes, now that fork() has completed. */\n\tfor (i = 0, narenas = narenas_total_get(); i < narenas; i++) {\n\t\tarena_t *arena;\n\n\t\tif ((arena = arena_get(tsd_tsdn(tsd), i, false)) != NULL) {\n\t\t\tarena_postfork_child(tsd_tsdn(tsd), arena);\n\t\t}\n\t}\n\tprof_postfork_child(tsd_tsdn(tsd));\n\tif (have_background_thread) {\n\t\tbackground_thread_postfork_child(tsd_tsdn(tsd));\n\t}\n\tmalloc_mutex_postfork_child(tsd_tsdn(tsd), &arenas_lock);\n\ttcache_postfork_child(tsd_tsdn(tsd));\n\tctl_postfork_child(tsd_tsdn(tsd));\n}\n\n/******************************************************************************/\n\n/* Helps the application decide if a pointer is worth re-allocating in order to reduce fragmentation.\n * returns 0 if the allocation is in the currently active run,\n * or when it is not causing any frag issue (large or huge bin)\n * returns the bin utilization and run utilization both in fixed point 16:16.\n * If the application decides to re-allocate it should use MALLOCX_TCACHE_NONE when doing so. */\nJEMALLOC_EXPORT int JEMALLOC_NOTHROW\nget_defrag_hint(void* ptr, int *bin_util, int *run_util) {\n\tassert(ptr != NULL);\n\treturn iget_defrag_hint(TSDN_NULL, ptr, bin_util, run_util);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/jemalloc_cpp.cpp",
    "content": "#include <mutex>\n#include <new>\n\n#define JEMALLOC_CPP_CPP_\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#ifdef __cplusplus\n}\n#endif\n\n// All operators in this file are exported.\n\n// Possibly alias hidden versions of malloc and sdallocx to avoid an extra plt\n// thunk?\n//\n// extern __typeof (sdallocx) sdallocx_int\n//  __attribute ((alias (\"sdallocx\"),\n//\t\tvisibility (\"hidden\")));\n//\n// ... but it needs to work with jemalloc namespaces.\n\nvoid\t*operator new(std::size_t size);\nvoid\t*operator new[](std::size_t size);\nvoid\t*operator new(std::size_t size, const std::nothrow_t &) noexcept;\nvoid\t*operator new[](std::size_t size, const std::nothrow_t &) noexcept;\nvoid\toperator delete(void *ptr) noexcept;\nvoid\toperator delete[](void *ptr) noexcept;\nvoid\toperator delete(void *ptr, const std::nothrow_t &) noexcept;\nvoid\toperator delete[](void *ptr, const std::nothrow_t &) noexcept;\n\n#if __cpp_sized_deallocation >= 201309\n/* C++14's sized-delete operators. */\nvoid\toperator delete(void *ptr, std::size_t size) noexcept;\nvoid\toperator delete[](void *ptr, std::size_t size) noexcept;\n#endif\n\ntemplate <bool IsNoExcept>\nvoid *\nnewImpl(std::size_t size) noexcept(IsNoExcept) {\n\tvoid *ptr = je_malloc(size);\n\tif (likely(ptr != nullptr))\n\t\treturn ptr;\n\n\twhile (ptr == nullptr) {\n\t\tstd::new_handler handler;\n\t\t// GCC-4.8 and clang 4.0 do not have std::get_new_handler.\n\t\t{\n\t\t\tstatic std::mutex mtx;\n\t\t\tstd::lock_guard<std::mutex> lock(mtx);\n\n\t\t\thandler = std::set_new_handler(nullptr);\n\t\t\tstd::set_new_handler(handler);\n\t\t}\n\t\tif (handler == nullptr)\n\t\t\tbreak;\n\n\t\ttry {\n\t\t\thandler();\n\t\t} catch (const std::bad_alloc &) {\n\t\t\tbreak;\n\t\t}\n\n\t\tptr = je_malloc(size);\n\t}\n\n\tif (ptr == nullptr && !IsNoExcept)\n\t\tstd::__throw_bad_alloc();\n\treturn ptr;\n}\n\nvoid *\noperator new(std::size_t size) {\n\treturn newImpl<false>(size);\n}\n\nvoid *\noperator new[](std::size_t size) {\n\treturn newImpl<false>(size);\n}\n\nvoid *\noperator new(std::size_t size, const std::nothrow_t &) noexcept {\n\treturn newImpl<true>(size);\n}\n\nvoid *\noperator new[](std::size_t size, const std::nothrow_t &) noexcept {\n\treturn newImpl<true>(size);\n}\n\nvoid\noperator delete(void *ptr) noexcept {\n\tje_free(ptr);\n}\n\nvoid\noperator delete[](void *ptr) noexcept {\n\tje_free(ptr);\n}\n\nvoid\noperator delete(void *ptr, const std::nothrow_t &) noexcept {\n\tje_free(ptr);\n}\n\nvoid operator delete[](void *ptr, const std::nothrow_t &) noexcept {\n\tje_free(ptr);\n}\n\n#if __cpp_sized_deallocation >= 201309\n\nvoid\noperator delete(void *ptr, std::size_t size) noexcept {\n\tif (unlikely(ptr == nullptr)) {\n\t\treturn;\n\t}\n\tje_sdallocx(ptr, size, /*flags=*/0);\n}\n\nvoid operator delete[](void *ptr, std::size_t size) noexcept {\n\tif (unlikely(ptr == nullptr)) {\n\t\treturn;\n\t}\n\tje_sdallocx(ptr, size, /*flags=*/0);\n}\n\n#endif  // __cpp_sized_deallocation\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/large.c",
    "content": "#define JEMALLOC_LARGE_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/extent_mmap.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/rtree.h\"\n#include \"jemalloc/internal/util.h\"\n\n/******************************************************************************/\n\nvoid *\nlarge_malloc(tsdn_t *tsdn, arena_t *arena, size_t usize, bool zero) {\n\tassert(usize == sz_s2u(usize));\n\n\treturn large_palloc(tsdn, arena, usize, CACHELINE, zero);\n}\n\nvoid *\nlarge_palloc(tsdn_t *tsdn, arena_t *arena, size_t usize, size_t alignment,\n    bool zero) {\n\tsize_t ausize;\n\textent_t *extent;\n\tbool is_zeroed;\n\tUNUSED bool idump JEMALLOC_CC_SILENCE_INIT(false);\n\n\tassert(!tsdn_null(tsdn) || arena != NULL);\n\n\tausize = sz_sa2u(usize, alignment);\n\tif (unlikely(ausize == 0 || ausize > LARGE_MAXCLASS)) {\n\t\treturn NULL;\n\t}\n\n\tif (config_fill && unlikely(opt_zero)) {\n\t\tzero = true;\n\t}\n\t/*\n\t * Copy zero into is_zeroed and pass the copy when allocating the\n\t * extent, so that it is possible to make correct junk/zero fill\n\t * decisions below, even if is_zeroed ends up true when zero is false.\n\t */\n\tis_zeroed = zero;\n\tif (likely(!tsdn_null(tsdn))) {\n\t\tarena = arena_choose(tsdn_tsd(tsdn), arena);\n\t}\n\tif (unlikely(arena == NULL) || (extent = arena_extent_alloc_large(tsdn,\n\t    arena, usize, alignment, &is_zeroed)) == NULL) {\n\t\treturn NULL;\n\t}\n\n\t/* See comments in arena_bin_slabs_full_insert(). */\n\tif (!arena_is_auto(arena)) {\n\t\t/* Insert extent into large. */\n\t\tmalloc_mutex_lock(tsdn, &arena->large_mtx);\n\t\textent_list_append(&arena->large, extent);\n\t\tmalloc_mutex_unlock(tsdn, &arena->large_mtx);\n\t}\n\tif (config_prof && arena_prof_accum(tsdn, arena, usize)) {\n\t\tprof_idump(tsdn);\n\t}\n\n\tif (zero) {\n\t\tassert(is_zeroed);\n\t} else if (config_fill && unlikely(opt_junk_alloc)) {\n\t\tmemset(extent_addr_get(extent), JEMALLOC_ALLOC_JUNK,\n\t\t    extent_usize_get(extent));\n\t}\n\n\tarena_decay_tick(tsdn, arena);\n\treturn extent_addr_get(extent);\n}\n\nstatic void\nlarge_dalloc_junk_impl(void *ptr, size_t size) {\n\tmemset(ptr, JEMALLOC_FREE_JUNK, size);\n}\nlarge_dalloc_junk_t *JET_MUTABLE large_dalloc_junk = large_dalloc_junk_impl;\n\nstatic void\nlarge_dalloc_maybe_junk_impl(void *ptr, size_t size) {\n\tif (config_fill && have_dss && unlikely(opt_junk_free)) {\n\t\t/*\n\t\t * Only bother junk filling if the extent isn't about to be\n\t\t * unmapped.\n\t\t */\n\t\tif (opt_retain || (have_dss && extent_in_dss(ptr))) {\n\t\t\tlarge_dalloc_junk(ptr, size);\n\t\t}\n\t}\n}\nlarge_dalloc_maybe_junk_t *JET_MUTABLE large_dalloc_maybe_junk =\n    large_dalloc_maybe_junk_impl;\n\nstatic bool\nlarge_ralloc_no_move_shrink(tsdn_t *tsdn, extent_t *extent, size_t usize) {\n\tarena_t *arena = extent_arena_get(extent);\n\tsize_t oldusize = extent_usize_get(extent);\n\textent_hooks_t *extent_hooks = extent_hooks_get(arena);\n\tsize_t diff = extent_size_get(extent) - (usize + sz_large_pad);\n\n\tassert(oldusize > usize);\n\n\tif (extent_hooks->split == NULL) {\n\t\treturn true;\n\t}\n\n\t/* Split excess pages. */\n\tif (diff != 0) {\n\t\textent_t *trail = extent_split_wrapper(tsdn, arena,\n\t\t    &extent_hooks, extent, usize + sz_large_pad,\n\t\t    sz_size2index(usize), false, diff, NSIZES, false);\n\t\tif (trail == NULL) {\n\t\t\treturn true;\n\t\t}\n\n\t\tif (config_fill && unlikely(opt_junk_free)) {\n\t\t\tlarge_dalloc_maybe_junk(extent_addr_get(trail),\n\t\t\t    extent_size_get(trail));\n\t\t}\n\n\t\tarena_extents_dirty_dalloc(tsdn, arena, &extent_hooks, trail);\n\t}\n\n\tarena_extent_ralloc_large_shrink(tsdn, arena, extent, oldusize);\n\n\treturn false;\n}\n\nstatic bool\nlarge_ralloc_no_move_expand(tsdn_t *tsdn, extent_t *extent, size_t usize,\n    bool zero) {\n\tarena_t *arena = extent_arena_get(extent);\n\tsize_t oldusize = extent_usize_get(extent);\n\textent_hooks_t *extent_hooks = extent_hooks_get(arena);\n\tsize_t trailsize = usize - oldusize;\n\n\tif (extent_hooks->merge == NULL) {\n\t\treturn true;\n\t}\n\n\tif (config_fill && unlikely(opt_zero)) {\n\t\tzero = true;\n\t}\n\t/*\n\t * Copy zero into is_zeroed_trail and pass the copy when allocating the\n\t * extent, so that it is possible to make correct junk/zero fill\n\t * decisions below, even if is_zeroed_trail ends up true when zero is\n\t * false.\n\t */\n\tbool is_zeroed_trail = zero;\n\tbool commit = true;\n\textent_t *trail;\n\tbool new_mapping;\n\tif ((trail = extents_alloc(tsdn, arena, &extent_hooks,\n\t    &arena->extents_dirty, extent_past_get(extent), trailsize, 0,\n\t    CACHELINE, false, NSIZES, &is_zeroed_trail, &commit)) != NULL\n\t    || (trail = extents_alloc(tsdn, arena, &extent_hooks,\n\t    &arena->extents_muzzy, extent_past_get(extent), trailsize, 0,\n\t    CACHELINE, false, NSIZES, &is_zeroed_trail, &commit)) != NULL) {\n\t\tif (config_stats) {\n\t\t\tnew_mapping = false;\n\t\t}\n\t} else {\n\t\tif ((trail = extent_alloc_wrapper(tsdn, arena, &extent_hooks,\n\t\t    extent_past_get(extent), trailsize, 0, CACHELINE, false,\n\t\t    NSIZES, &is_zeroed_trail, &commit)) == NULL) {\n\t\t\treturn true;\n\t\t}\n\t\tif (config_stats) {\n\t\t\tnew_mapping = true;\n\t\t}\n\t}\n\n\tif (extent_merge_wrapper(tsdn, arena, &extent_hooks, extent, trail)) {\n\t\textent_dalloc_wrapper(tsdn, arena, &extent_hooks, trail);\n\t\treturn true;\n\t}\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\tszind_t szind = sz_size2index(usize);\n\textent_szind_set(extent, szind);\n\trtree_szind_slab_update(tsdn, &extents_rtree, rtree_ctx,\n\t    (uintptr_t)extent_addr_get(extent), szind, false);\n\n\tif (config_stats && new_mapping) {\n\t\tarena_stats_mapped_add(tsdn, &arena->stats, trailsize);\n\t}\n\n\tif (zero) {\n\t\tif (config_cache_oblivious) {\n\t\t\t/*\n\t\t\t * Zero the trailing bytes of the original allocation's\n\t\t\t * last page, since they are in an indeterminate state.\n\t\t\t * There will always be trailing bytes, because ptr's\n\t\t\t * offset from the beginning of the extent is a multiple\n\t\t\t * of CACHELINE in [0 .. PAGE).\n\t\t\t */\n\t\t\tvoid *zbase = (void *)\n\t\t\t    ((uintptr_t)extent_addr_get(extent) + oldusize);\n\t\t\tvoid *zpast = PAGE_ADDR2BASE((void *)((uintptr_t)zbase +\n\t\t\t    PAGE));\n\t\t\tsize_t nzero = (uintptr_t)zpast - (uintptr_t)zbase;\n\t\t\tassert(nzero > 0);\n\t\t\tmemset(zbase, 0, nzero);\n\t\t}\n\t\tassert(is_zeroed_trail);\n\t} else if (config_fill && unlikely(opt_junk_alloc)) {\n\t\tmemset((void *)((uintptr_t)extent_addr_get(extent) + oldusize),\n\t\t    JEMALLOC_ALLOC_JUNK, usize - oldusize);\n\t}\n\n\tarena_extent_ralloc_large_expand(tsdn, arena, extent, oldusize);\n\n\treturn false;\n}\n\nbool\nlarge_ralloc_no_move(tsdn_t *tsdn, extent_t *extent, size_t usize_min,\n    size_t usize_max, bool zero) {\n\tsize_t oldusize = extent_usize_get(extent);\n\n\t/* The following should have been caught by callers. */\n\tassert(usize_min > 0 && usize_max <= LARGE_MAXCLASS);\n\t/* Both allocation sizes must be large to avoid a move. */\n\tassert(oldusize >= LARGE_MINCLASS && usize_max >= LARGE_MINCLASS);\n\n\tif (usize_max > oldusize) {\n\t\t/* Attempt to expand the allocation in-place. */\n\t\tif (!large_ralloc_no_move_expand(tsdn, extent, usize_max,\n\t\t    zero)) {\n\t\t\tarena_decay_tick(tsdn, extent_arena_get(extent));\n\t\t\treturn false;\n\t\t}\n\t\t/* Try again, this time with usize_min. */\n\t\tif (usize_min < usize_max && usize_min > oldusize &&\n\t\t    large_ralloc_no_move_expand(tsdn, extent, usize_min,\n\t\t    zero)) {\n\t\t\tarena_decay_tick(tsdn, extent_arena_get(extent));\n\t\t\treturn false;\n\t\t}\n\t}\n\n\t/*\n\t * Avoid moving the allocation if the existing extent size accommodates\n\t * the new size.\n\t */\n\tif (oldusize >= usize_min && oldusize <= usize_max) {\n\t\tarena_decay_tick(tsdn, extent_arena_get(extent));\n\t\treturn false;\n\t}\n\n\t/* Attempt to shrink the allocation in-place. */\n\tif (oldusize > usize_max) {\n\t\tif (!large_ralloc_no_move_shrink(tsdn, extent, usize_max)) {\n\t\t\tarena_decay_tick(tsdn, extent_arena_get(extent));\n\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n}\n\nstatic void *\nlarge_ralloc_move_helper(tsdn_t *tsdn, arena_t *arena, size_t usize,\n    size_t alignment, bool zero) {\n\tif (alignment <= CACHELINE) {\n\t\treturn large_malloc(tsdn, arena, usize, zero);\n\t}\n\treturn large_palloc(tsdn, arena, usize, alignment, zero);\n}\n\nvoid *\nlarge_ralloc(tsdn_t *tsdn, arena_t *arena, extent_t *extent, size_t usize,\n    size_t alignment, bool zero, tcache_t *tcache) {\n\tsize_t oldusize = extent_usize_get(extent);\n\n\t/* The following should have been caught by callers. */\n\tassert(usize > 0 && usize <= LARGE_MAXCLASS);\n\t/* Both allocation sizes must be large to avoid a move. */\n\tassert(oldusize >= LARGE_MINCLASS && usize >= LARGE_MINCLASS);\n\n\t/* Try to avoid moving the allocation. */\n\tif (!large_ralloc_no_move(tsdn, extent, usize, usize, zero)) {\n\t\treturn extent_addr_get(extent);\n\t}\n\n\t/*\n\t * usize and old size are different enough that we need to use a\n\t * different size class.  In that case, fall back to allocating new\n\t * space and copying.\n\t */\n\tvoid *ret = large_ralloc_move_helper(tsdn, arena, usize, alignment,\n\t    zero);\n\tif (ret == NULL) {\n\t\treturn NULL;\n\t}\n\n\tsize_t copysize = (usize < oldusize) ? usize : oldusize;\n\tmemcpy(ret, extent_addr_get(extent), copysize);\n\tisdalloct(tsdn, extent_addr_get(extent), oldusize, tcache, NULL, true);\n\treturn ret;\n}\n\n/*\n * junked_locked indicates whether the extent's data have been junk-filled, and\n * whether the arena's large_mtx is currently held.\n */\nstatic void\nlarge_dalloc_prep_impl(tsdn_t *tsdn, arena_t *arena, extent_t *extent,\n    bool junked_locked) {\n\tif (!junked_locked) {\n\t\t/* See comments in arena_bin_slabs_full_insert(). */\n\t\tif (!arena_is_auto(arena)) {\n\t\t\tmalloc_mutex_lock(tsdn, &arena->large_mtx);\n\t\t\textent_list_remove(&arena->large, extent);\n\t\t\tmalloc_mutex_unlock(tsdn, &arena->large_mtx);\n\t\t}\n\t\tlarge_dalloc_maybe_junk(extent_addr_get(extent),\n\t\t    extent_usize_get(extent));\n\t} else {\n\t\tmalloc_mutex_assert_owner(tsdn, &arena->large_mtx);\n\t\tif (!arena_is_auto(arena)) {\n\t\t\textent_list_remove(&arena->large, extent);\n\t\t}\n\t}\n\tarena_extent_dalloc_large_prep(tsdn, arena, extent);\n}\n\nstatic void\nlarge_dalloc_finish_impl(tsdn_t *tsdn, arena_t *arena, extent_t *extent) {\n\textent_hooks_t *extent_hooks = EXTENT_HOOKS_INITIALIZER;\n\tarena_extents_dirty_dalloc(tsdn, arena, &extent_hooks, extent);\n}\n\nvoid\nlarge_dalloc_prep_junked_locked(tsdn_t *tsdn, extent_t *extent) {\n\tlarge_dalloc_prep_impl(tsdn, extent_arena_get(extent), extent, true);\n}\n\nvoid\nlarge_dalloc_finish(tsdn_t *tsdn, extent_t *extent) {\n\tlarge_dalloc_finish_impl(tsdn, extent_arena_get(extent), extent);\n}\n\nvoid\nlarge_dalloc(tsdn_t *tsdn, extent_t *extent) {\n\tarena_t *arena = extent_arena_get(extent);\n\tlarge_dalloc_prep_impl(tsdn, arena, extent, false);\n\tlarge_dalloc_finish_impl(tsdn, arena, extent);\n\tarena_decay_tick(tsdn, arena);\n}\n\nsize_t\nlarge_salloc(tsdn_t *tsdn, const extent_t *extent) {\n\treturn extent_usize_get(extent);\n}\n\nprof_tctx_t *\nlarge_prof_tctx_get(tsdn_t *tsdn, const extent_t *extent) {\n\treturn extent_prof_tctx_get(extent);\n}\n\nvoid\nlarge_prof_tctx_set(tsdn_t *tsdn, extent_t *extent, prof_tctx_t *tctx) {\n\textent_prof_tctx_set(extent, tctx);\n}\n\nvoid\nlarge_prof_tctx_reset(tsdn_t *tsdn, extent_t *extent) {\n\tlarge_prof_tctx_set(tsdn, extent, (prof_tctx_t *)(uintptr_t)1U);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/malloc_io.c",
    "content": "#define JEMALLOC_MALLOC_IO_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/malloc_io.h\"\n#include \"jemalloc/internal/util.h\"\n\n#ifdef assert\n#  undef assert\n#endif\n#ifdef not_reached\n#  undef not_reached\n#endif\n#ifdef not_implemented\n#  undef not_implemented\n#endif\n#ifdef assert_not_implemented\n#  undef assert_not_implemented\n#endif\n\n/*\n * Define simple versions of assertion macros that won't recurse in case\n * of assertion failures in malloc_*printf().\n */\n#define assert(e) do {\t\t\t\t\t\t\t\\\n\tif (config_debug && !(e)) {\t\t\t\t\t\\\n\t\tmalloc_write(\"<jemalloc>: Failed assertion\\n\");\t\t\\\n\t\tabort();\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define not_reached() do {\t\t\t\t\t\t\\\n\tif (config_debug) {\t\t\t\t\t\t\\\n\t\tmalloc_write(\"<jemalloc>: Unreachable code reached\\n\");\t\\\n\t\tabort();\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tunreachable();\t\t\t\t\t\t\t\\\n} while (0)\n\n#define not_implemented() do {\t\t\t\t\t\t\\\n\tif (config_debug) {\t\t\t\t\t\t\\\n\t\tmalloc_write(\"<jemalloc>: Not implemented\\n\");\t\t\\\n\t\tabort();\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define assert_not_implemented(e) do {\t\t\t\t\t\\\n\tif (unlikely(config_debug && !(e))) {\t\t\t\t\\\n\t\tnot_implemented();\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n/******************************************************************************/\n/* Function prototypes for non-inline static functions. */\n\nstatic void wrtmessage(void *cbopaque, const char *s);\n#define U2S_BUFSIZE ((1U << (LG_SIZEOF_INTMAX_T + 3)) + 1)\nstatic char *u2s(uintmax_t x, unsigned base, bool uppercase, char *s,\n    size_t *slen_p);\n#define D2S_BUFSIZE (1 + U2S_BUFSIZE)\nstatic char *d2s(intmax_t x, char sign, char *s, size_t *slen_p);\n#define O2S_BUFSIZE (1 + U2S_BUFSIZE)\nstatic char *o2s(uintmax_t x, bool alt_form, char *s, size_t *slen_p);\n#define X2S_BUFSIZE (2 + U2S_BUFSIZE)\nstatic char *x2s(uintmax_t x, bool alt_form, bool uppercase, char *s,\n    size_t *slen_p);\n\n/******************************************************************************/\n\n/* malloc_message() setup. */\nstatic void\nwrtmessage(void *cbopaque, const char *s) {\n#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_write)\n\t/*\n\t * Use syscall(2) rather than write(2) when possible in order to avoid\n\t * the possibility of memory allocation within libc.  This is necessary\n\t * on FreeBSD; most operating systems do not have this problem though.\n\t *\n\t * syscall() returns long or int, depending on platform, so capture the\n\t * unused result in the widest plausible type to avoid compiler\n\t * warnings.\n\t */\n\tUNUSED long result = syscall(SYS_write, STDERR_FILENO, s, strlen(s));\n#else\n\tUNUSED ssize_t result = write(STDERR_FILENO, s, strlen(s));\n#endif\n}\n\nJEMALLOC_EXPORT void\t(*je_malloc_message)(void *, const char *s);\n\n/*\n * Wrapper around malloc_message() that avoids the need for\n * je_malloc_message(...) throughout the code.\n */\nvoid\nmalloc_write(const char *s) {\n\tif (je_malloc_message != NULL) {\n\t\tje_malloc_message(NULL, s);\n\t} else {\n\t\twrtmessage(NULL, s);\n\t}\n}\n\n/*\n * glibc provides a non-standard strerror_r() when _GNU_SOURCE is defined, so\n * provide a wrapper.\n */\nint\nbuferror(int err, char *buf, size_t buflen) {\n#ifdef _WIN32\n\tFormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, 0,\n\t    (LPSTR)buf, (DWORD)buflen, NULL);\n\treturn 0;\n#elif defined(__GLIBC__) && defined(_GNU_SOURCE)\n\tchar *b = strerror_r(err, buf, buflen);\n\tif (b != buf) {\n\t\tstrncpy(buf, b, buflen);\n\t\tbuf[buflen-1] = '\\0';\n\t}\n\treturn 0;\n#else\n\treturn strerror_r(err, buf, buflen);\n#endif\n}\n\nuintmax_t\nmalloc_strtoumax(const char *restrict nptr, char **restrict endptr, int base) {\n\tuintmax_t ret, digit;\n\tunsigned b;\n\tbool neg;\n\tconst char *p, *ns;\n\n\tp = nptr;\n\tif (base < 0 || base == 1 || base > 36) {\n\t\tns = p;\n\t\tset_errno(EINVAL);\n\t\tret = UINTMAX_MAX;\n\t\tgoto label_return;\n\t}\n\tb = base;\n\n\t/* Swallow leading whitespace and get sign, if any. */\n\tneg = false;\n\twhile (true) {\n\t\tswitch (*p) {\n\t\tcase '\\t': case '\\n': case '\\v': case '\\f': case '\\r': case ' ':\n\t\t\tp++;\n\t\t\tbreak;\n\t\tcase '-':\n\t\t\tneg = true;\n\t\t\t/* Fall through. */\n\t\tcase '+':\n\t\t\tp++;\n\t\t\t/* Fall through. */\n\t\tdefault:\n\t\t\tgoto label_prefix;\n\t\t}\n\t}\n\n\t/* Get prefix, if any. */\n\tlabel_prefix:\n\t/*\n\t * Note where the first non-whitespace/sign character is so that it is\n\t * possible to tell whether any digits are consumed (e.g., \"  0\" vs.\n\t * \"  -x\").\n\t */\n\tns = p;\n\tif (*p == '0') {\n\t\tswitch (p[1]) {\n\t\tcase '0': case '1': case '2': case '3': case '4': case '5':\n\t\tcase '6': case '7':\n\t\t\tif (b == 0) {\n\t\t\t\tb = 8;\n\t\t\t}\n\t\t\tif (b == 8) {\n\t\t\t\tp++;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase 'X': case 'x':\n\t\t\tswitch (p[2]) {\n\t\t\tcase '0': case '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9':\n\t\t\tcase 'A': case 'B': case 'C': case 'D': case 'E':\n\t\t\tcase 'F':\n\t\t\tcase 'a': case 'b': case 'c': case 'd': case 'e':\n\t\t\tcase 'f':\n\t\t\t\tif (b == 0) {\n\t\t\t\t\tb = 16;\n\t\t\t\t}\n\t\t\t\tif (b == 16) {\n\t\t\t\t\tp += 2;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tp++;\n\t\t\tret = 0;\n\t\t\tgoto label_return;\n\t\t}\n\t}\n\tif (b == 0) {\n\t\tb = 10;\n\t}\n\n\t/* Convert. */\n\tret = 0;\n\twhile ((*p >= '0' && *p <= '9' && (digit = *p - '0') < b)\n\t    || (*p >= 'A' && *p <= 'Z' && (digit = 10 + *p - 'A') < b)\n\t    || (*p >= 'a' && *p <= 'z' && (digit = 10 + *p - 'a') < b)) {\n\t\tuintmax_t pret = ret;\n\t\tret *= b;\n\t\tret += digit;\n\t\tif (ret < pret) {\n\t\t\t/* Overflow. */\n\t\t\tset_errno(ERANGE);\n\t\t\tret = UINTMAX_MAX;\n\t\t\tgoto label_return;\n\t\t}\n\t\tp++;\n\t}\n\tif (neg) {\n\t\tret = (uintmax_t)(-((intmax_t)ret));\n\t}\n\n\tif (p == ns) {\n\t\t/* No conversion performed. */\n\t\tset_errno(EINVAL);\n\t\tret = UINTMAX_MAX;\n\t\tgoto label_return;\n\t}\n\nlabel_return:\n\tif (endptr != NULL) {\n\t\tif (p == ns) {\n\t\t\t/* No characters were converted. */\n\t\t\t*endptr = (char *)nptr;\n\t\t} else {\n\t\t\t*endptr = (char *)p;\n\t\t}\n\t}\n\treturn ret;\n}\n\nstatic char *\nu2s(uintmax_t x, unsigned base, bool uppercase, char *s, size_t *slen_p) {\n\tunsigned i;\n\n\ti = U2S_BUFSIZE - 1;\n\ts[i] = '\\0';\n\tswitch (base) {\n\tcase 10:\n\t\tdo {\n\t\t\ti--;\n\t\t\ts[i] = \"0123456789\"[x % (uint64_t)10];\n\t\t\tx /= (uint64_t)10;\n\t\t} while (x > 0);\n\t\tbreak;\n\tcase 16: {\n\t\tconst char *digits = (uppercase)\n\t\t    ? \"0123456789ABCDEF\"\n\t\t    : \"0123456789abcdef\";\n\n\t\tdo {\n\t\t\ti--;\n\t\t\ts[i] = digits[x & 0xf];\n\t\t\tx >>= 4;\n\t\t} while (x > 0);\n\t\tbreak;\n\t} default: {\n\t\tconst char *digits = (uppercase)\n\t\t    ? \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n\t\t    : \"0123456789abcdefghijklmnopqrstuvwxyz\";\n\n\t\tassert(base >= 2 && base <= 36);\n\t\tdo {\n\t\t\ti--;\n\t\t\ts[i] = digits[x % (uint64_t)base];\n\t\t\tx /= (uint64_t)base;\n\t\t} while (x > 0);\n\t}}\n\n\t*slen_p = U2S_BUFSIZE - 1 - i;\n\treturn &s[i];\n}\n\nstatic char *\nd2s(intmax_t x, char sign, char *s, size_t *slen_p) {\n\tbool neg;\n\n\tif ((neg = (x < 0))) {\n\t\tx = -x;\n\t}\n\ts = u2s(x, 10, false, s, slen_p);\n\tif (neg) {\n\t\tsign = '-';\n\t}\n\tswitch (sign) {\n\tcase '-':\n\t\tif (!neg) {\n\t\t\tbreak;\n\t\t}\n\t\t/* Fall through. */\n\tcase ' ':\n\tcase '+':\n\t\ts--;\n\t\t(*slen_p)++;\n\t\t*s = sign;\n\t\tbreak;\n\tdefault: not_reached();\n\t}\n\treturn s;\n}\n\nstatic char *\no2s(uintmax_t x, bool alt_form, char *s, size_t *slen_p) {\n\ts = u2s(x, 8, false, s, slen_p);\n\tif (alt_form && *s != '0') {\n\t\ts--;\n\t\t(*slen_p)++;\n\t\t*s = '0';\n\t}\n\treturn s;\n}\n\nstatic char *\nx2s(uintmax_t x, bool alt_form, bool uppercase, char *s, size_t *slen_p) {\n\ts = u2s(x, 16, uppercase, s, slen_p);\n\tif (alt_form) {\n\t\ts -= 2;\n\t\t(*slen_p) += 2;\n\t\tmemcpy(s, uppercase ? \"0X\" : \"0x\", 2);\n\t}\n\treturn s;\n}\n\nsize_t\nmalloc_vsnprintf(char *str, size_t size, const char *format, va_list ap) {\n\tsize_t i;\n\tconst char *f;\n\n#define APPEND_C(c) do {\t\t\t\t\t\t\\\n\tif (i < size) {\t\t\t\t\t\t\t\\\n\t\tstr[i] = (c);\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\ti++;\t\t\t\t\t\t\t\t\\\n} while (0)\n#define APPEND_S(s, slen) do {\t\t\t\t\t\t\\\n\tif (i < size) {\t\t\t\t\t\t\t\\\n\t\tsize_t cpylen = (slen <= size - i) ? slen : size - i;\t\\\n\t\tmemcpy(&str[i], s, cpylen);\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\ti += slen;\t\t\t\t\t\t\t\\\n} while (0)\n#define APPEND_PADDED_S(s, slen, width, left_justify) do {\t\t\\\n\t/* Left padding. */\t\t\t\t\t\t\\\n\tsize_t pad_len = (width == -1) ? 0 : ((slen < (size_t)width) ?\t\\\n\t    (size_t)width - slen : 0);\t\t\t\t\t\\\n\tif (!left_justify && pad_len != 0) {\t\t\t\t\\\n\t\tsize_t j;\t\t\t\t\t\t\\\n\t\tfor (j = 0; j < pad_len; j++) {\t\t\t\t\\\n\t\t\tAPPEND_C(' ');\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\t/* Value. */\t\t\t\t\t\t\t\\\n\tAPPEND_S(s, slen);\t\t\t\t\t\t\\\n\t/* Right padding. */\t\t\t\t\t\t\\\n\tif (left_justify && pad_len != 0) {\t\t\t\t\\\n\t\tsize_t j;\t\t\t\t\t\t\\\n\t\tfor (j = 0; j < pad_len; j++) {\t\t\t\t\\\n\t\t\tAPPEND_C(' ');\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n#define GET_ARG_NUMERIC(val, len) do {\t\t\t\t\t\\\n\tswitch (len) {\t\t\t\t\t\t\t\\\n\tcase '?':\t\t\t\t\t\t\t\\\n\t\tval = va_arg(ap, int);\t\t\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\tcase '?' | 0x80:\t\t\t\t\t\t\\\n\t\tval = va_arg(ap, unsigned int);\t\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\tcase 'l':\t\t\t\t\t\t\t\\\n\t\tval = va_arg(ap, long);\t\t\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\tcase 'l' | 0x80:\t\t\t\t\t\t\\\n\t\tval = va_arg(ap, unsigned long);\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\tcase 'q':\t\t\t\t\t\t\t\\\n\t\tval = va_arg(ap, long long);\t\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\tcase 'q' | 0x80:\t\t\t\t\t\t\\\n\t\tval = va_arg(ap, unsigned long long);\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\tcase 'j':\t\t\t\t\t\t\t\\\n\t\tval = va_arg(ap, intmax_t);\t\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\tcase 'j' | 0x80:\t\t\t\t\t\t\\\n\t\tval = va_arg(ap, uintmax_t);\t\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\tcase 't':\t\t\t\t\t\t\t\\\n\t\tval = va_arg(ap, ptrdiff_t);\t\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\tcase 'z':\t\t\t\t\t\t\t\\\n\t\tval = va_arg(ap, ssize_t);\t\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\tcase 'z' | 0x80:\t\t\t\t\t\t\\\n\t\tval = va_arg(ap, size_t);\t\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\tcase 'p': /* Synthetic; used for %p. */\t\t\t\t\\\n\t\tval = va_arg(ap, uintptr_t);\t\t\t\t\\\n\t\tbreak;\t\t\t\t\t\t\t\\\n\tdefault:\t\t\t\t\t\t\t\\\n\t\tnot_reached();\t\t\t\t\t\t\\\n\t\tval = 0;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n\ti = 0;\n\tf = format;\n\twhile (true) {\n\t\tswitch (*f) {\n\t\tcase '\\0': goto label_out;\n\t\tcase '%': {\n\t\t\tbool alt_form = false;\n\t\t\tbool left_justify = false;\n\t\t\tbool plus_space = false;\n\t\t\tbool plus_plus = false;\n\t\t\tint prec = -1;\n\t\t\tint width = -1;\n\t\t\tunsigned char len = '?';\n\t\t\tchar *s;\n\t\t\tsize_t slen;\n\n\t\t\tf++;\n\t\t\t/* Flags. */\n\t\t\twhile (true) {\n\t\t\t\tswitch (*f) {\n\t\t\t\tcase '#':\n\t\t\t\t\tassert(!alt_form);\n\t\t\t\t\talt_form = true;\n\t\t\t\t\tbreak;\n\t\t\t\tcase '-':\n\t\t\t\t\tassert(!left_justify);\n\t\t\t\t\tleft_justify = true;\n\t\t\t\t\tbreak;\n\t\t\t\tcase ' ':\n\t\t\t\t\tassert(!plus_space);\n\t\t\t\t\tplus_space = true;\n\t\t\t\t\tbreak;\n\t\t\t\tcase '+':\n\t\t\t\t\tassert(!plus_plus);\n\t\t\t\t\tplus_plus = true;\n\t\t\t\t\tbreak;\n\t\t\t\tdefault: goto label_width;\n\t\t\t\t}\n\t\t\t\tf++;\n\t\t\t}\n\t\t\t/* Width. */\n\t\t\tlabel_width:\n\t\t\tswitch (*f) {\n\t\t\tcase '*':\n\t\t\t\twidth = va_arg(ap, int);\n\t\t\t\tf++;\n\t\t\t\tif (width < 0) {\n\t\t\t\t\tleft_justify = true;\n\t\t\t\t\twidth = -width;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase '0': case '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9': {\n\t\t\t\tuintmax_t uwidth;\n\t\t\t\tset_errno(0);\n\t\t\t\tuwidth = malloc_strtoumax(f, (char **)&f, 10);\n\t\t\t\tassert(uwidth != UINTMAX_MAX || get_errno() !=\n\t\t\t\t    ERANGE);\n\t\t\t\twidth = (int)uwidth;\n\t\t\t\tbreak;\n\t\t\t} default:\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\t/* Width/precision separator. */\n\t\t\tif (*f == '.') {\n\t\t\t\tf++;\n\t\t\t} else {\n\t\t\t\tgoto label_length;\n\t\t\t}\n\t\t\t/* Precision. */\n\t\t\tswitch (*f) {\n\t\t\tcase '*':\n\t\t\t\tprec = va_arg(ap, int);\n\t\t\t\tf++;\n\t\t\t\tbreak;\n\t\t\tcase '0': case '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9': {\n\t\t\t\tuintmax_t uprec;\n\t\t\t\tset_errno(0);\n\t\t\t\tuprec = malloc_strtoumax(f, (char **)&f, 10);\n\t\t\t\tassert(uprec != UINTMAX_MAX || get_errno() !=\n\t\t\t\t    ERANGE);\n\t\t\t\tprec = (int)uprec;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tdefault: break;\n\t\t\t}\n\t\t\t/* Length. */\n\t\t\tlabel_length:\n\t\t\tswitch (*f) {\n\t\t\tcase 'l':\n\t\t\t\tf++;\n\t\t\t\tif (*f == 'l') {\n\t\t\t\t\tlen = 'q';\n\t\t\t\t\tf++;\n\t\t\t\t} else {\n\t\t\t\t\tlen = 'l';\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase 'q': case 'j': case 't': case 'z':\n\t\t\t\tlen = *f;\n\t\t\t\tf++;\n\t\t\t\tbreak;\n\t\t\tdefault: break;\n\t\t\t}\n\t\t\t/* Conversion specifier. */\n\t\t\tswitch (*f) {\n\t\t\tcase '%':\n\t\t\t\t/* %% */\n\t\t\t\tAPPEND_C(*f);\n\t\t\t\tf++;\n\t\t\t\tbreak;\n\t\t\tcase 'd': case 'i': {\n\t\t\t\tintmax_t val JEMALLOC_CC_SILENCE_INIT(0);\n\t\t\t\tchar buf[D2S_BUFSIZE];\n\n\t\t\t\tGET_ARG_NUMERIC(val, len);\n\t\t\t\ts = d2s(val, (plus_plus ? '+' : (plus_space ?\n\t\t\t\t    ' ' : '-')), buf, &slen);\n\t\t\t\tAPPEND_PADDED_S(s, slen, width, left_justify);\n\t\t\t\tf++;\n\t\t\t\tbreak;\n\t\t\t} case 'o': {\n\t\t\t\tuintmax_t val JEMALLOC_CC_SILENCE_INIT(0);\n\t\t\t\tchar buf[O2S_BUFSIZE];\n\n\t\t\t\tGET_ARG_NUMERIC(val, len | 0x80);\n\t\t\t\ts = o2s(val, alt_form, buf, &slen);\n\t\t\t\tAPPEND_PADDED_S(s, slen, width, left_justify);\n\t\t\t\tf++;\n\t\t\t\tbreak;\n\t\t\t} case 'u': {\n\t\t\t\tuintmax_t val JEMALLOC_CC_SILENCE_INIT(0);\n\t\t\t\tchar buf[U2S_BUFSIZE];\n\n\t\t\t\tGET_ARG_NUMERIC(val, len | 0x80);\n\t\t\t\ts = u2s(val, 10, false, buf, &slen);\n\t\t\t\tAPPEND_PADDED_S(s, slen, width, left_justify);\n\t\t\t\tf++;\n\t\t\t\tbreak;\n\t\t\t} case 'x': case 'X': {\n\t\t\t\tuintmax_t val JEMALLOC_CC_SILENCE_INIT(0);\n\t\t\t\tchar buf[X2S_BUFSIZE];\n\n\t\t\t\tGET_ARG_NUMERIC(val, len | 0x80);\n\t\t\t\ts = x2s(val, alt_form, *f == 'X', buf, &slen);\n\t\t\t\tAPPEND_PADDED_S(s, slen, width, left_justify);\n\t\t\t\tf++;\n\t\t\t\tbreak;\n\t\t\t} case 'c': {\n\t\t\t\tunsigned char val;\n\t\t\t\tchar buf[2];\n\n\t\t\t\tassert(len == '?' || len == 'l');\n\t\t\t\tassert_not_implemented(len != 'l');\n\t\t\t\tval = va_arg(ap, int);\n\t\t\t\tbuf[0] = val;\n\t\t\t\tbuf[1] = '\\0';\n\t\t\t\tAPPEND_PADDED_S(buf, 1, width, left_justify);\n\t\t\t\tf++;\n\t\t\t\tbreak;\n\t\t\t} case 's':\n\t\t\t\tassert(len == '?' || len == 'l');\n\t\t\t\tassert_not_implemented(len != 'l');\n\t\t\t\ts = va_arg(ap, char *);\n\t\t\t\tslen = (prec < 0) ? strlen(s) : (size_t)prec;\n\t\t\t\tAPPEND_PADDED_S(s, slen, width, left_justify);\n\t\t\t\tf++;\n\t\t\t\tbreak;\n\t\t\tcase 'p': {\n\t\t\t\tuintmax_t val;\n\t\t\t\tchar buf[X2S_BUFSIZE];\n\n\t\t\t\tGET_ARG_NUMERIC(val, 'p');\n\t\t\t\ts = x2s(val, true, false, buf, &slen);\n\t\t\t\tAPPEND_PADDED_S(s, slen, width, left_justify);\n\t\t\t\tf++;\n\t\t\t\tbreak;\n\t\t\t} default: not_reached();\n\t\t\t}\n\t\t\tbreak;\n\t\t} default: {\n\t\t\tAPPEND_C(*f);\n\t\t\tf++;\n\t\t\tbreak;\n\t\t}}\n\t}\n\tlabel_out:\n\tif (i < size) {\n\t\tstr[i] = '\\0';\n\t} else {\n\t\tstr[size - 1] = '\\0';\n\t}\n\n#undef APPEND_C\n#undef APPEND_S\n#undef APPEND_PADDED_S\n#undef GET_ARG_NUMERIC\n\treturn i;\n}\n\nJEMALLOC_FORMAT_PRINTF(3, 4)\nsize_t\nmalloc_snprintf(char *str, size_t size, const char *format, ...) {\n\tsize_t ret;\n\tva_list ap;\n\n\tva_start(ap, format);\n\tret = malloc_vsnprintf(str, size, format, ap);\n\tva_end(ap);\n\n\treturn ret;\n}\n\nvoid\nmalloc_vcprintf(void (*write_cb)(void *, const char *), void *cbopaque,\n    const char *format, va_list ap) {\n\tchar buf[MALLOC_PRINTF_BUFSIZE];\n\n\tif (write_cb == NULL) {\n\t\t/*\n\t\t * The caller did not provide an alternate write_cb callback\n\t\t * function, so use the default one.  malloc_write() is an\n\t\t * inline function, so use malloc_message() directly here.\n\t\t */\n\t\twrite_cb = (je_malloc_message != NULL) ? je_malloc_message :\n\t\t    wrtmessage;\n\t\tcbopaque = NULL;\n\t}\n\n\tmalloc_vsnprintf(buf, sizeof(buf), format, ap);\n\twrite_cb(cbopaque, buf);\n}\n\n/*\n * Print to a callback function in such a way as to (hopefully) avoid memory\n * allocation.\n */\nJEMALLOC_FORMAT_PRINTF(3, 4)\nvoid\nmalloc_cprintf(void (*write_cb)(void *, const char *), void *cbopaque,\n    const char *format, ...) {\n\tva_list ap;\n\n\tva_start(ap, format);\n\tmalloc_vcprintf(write_cb, cbopaque, format, ap);\n\tva_end(ap);\n}\n\n/* Print to stderr in such a way as to avoid memory allocation. */\nJEMALLOC_FORMAT_PRINTF(1, 2)\nvoid\nmalloc_printf(const char *format, ...) {\n\tva_list ap;\n\n\tva_start(ap, format);\n\tmalloc_vcprintf(NULL, NULL, format, ap);\n\tva_end(ap);\n}\n\n/*\n * Restore normal assertion macros, in order to make it possible to compile all\n * C files as a single concatenation.\n */\n#undef assert\n#undef not_reached\n#undef not_implemented\n#undef assert_not_implemented\n#include \"jemalloc/internal/assert.h\"\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/mutex.c",
    "content": "#define JEMALLOC_MUTEX_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/malloc_io.h\"\n\n#ifndef _CRT_SPINCOUNT\n#define _CRT_SPINCOUNT 4000\n#endif\n\n/******************************************************************************/\n/* Data. */\n\n#ifdef JEMALLOC_LAZY_LOCK\nbool isthreaded = false;\n#endif\n#ifdef JEMALLOC_MUTEX_INIT_CB\nstatic bool\t\tpostpone_init = true;\nstatic malloc_mutex_t\t*postponed_mutexes = NULL;\n#endif\n\n/******************************************************************************/\n/*\n * We intercept pthread_create() calls in order to toggle isthreaded if the\n * process goes multi-threaded.\n */\n\n#if defined(JEMALLOC_LAZY_LOCK) && !defined(_WIN32)\nJEMALLOC_EXPORT int\npthread_create(pthread_t *__restrict thread,\n    const pthread_attr_t *__restrict attr, void *(*start_routine)(void *),\n    void *__restrict arg) {\n\treturn pthread_create_wrapper(thread, attr, start_routine, arg);\n}\n#endif\n\n/******************************************************************************/\n\n#ifdef JEMALLOC_MUTEX_INIT_CB\nJEMALLOC_EXPORT int\t_pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex,\n    void *(calloc_cb)(size_t, size_t));\n#endif\n\nvoid\nmalloc_mutex_lock_slow(malloc_mutex_t *mutex) {\n\tmutex_prof_data_t *data = &mutex->prof_data;\n\tUNUSED nstime_t before = NSTIME_ZERO_INITIALIZER;\n\n\tif (ncpus == 1) {\n\t\tgoto label_spin_done;\n\t}\n\n\tint cnt = 0, max_cnt = MALLOC_MUTEX_MAX_SPIN;\n\tdo {\n\t\tCPU_SPINWAIT;\n\t\tif (!malloc_mutex_trylock_final(mutex)) {\n\t\t\tdata->n_spin_acquired++;\n\t\t\treturn;\n\t\t}\n\t} while (cnt++ < max_cnt);\n\n\tif (!config_stats) {\n\t\t/* Only spin is useful when stats is off. */\n\t\tmalloc_mutex_lock_final(mutex);\n\t\treturn;\n\t}\nlabel_spin_done:\n\tnstime_update(&before);\n\t/* Copy before to after to avoid clock skews. */\n\tnstime_t after;\n\tnstime_copy(&after, &before);\n\tuint32_t n_thds = atomic_fetch_add_u32(&data->n_waiting_thds, 1,\n\t    ATOMIC_RELAXED) + 1;\n\t/* One last try as above two calls may take quite some cycles. */\n\tif (!malloc_mutex_trylock_final(mutex)) {\n\t\tatomic_fetch_sub_u32(&data->n_waiting_thds, 1, ATOMIC_RELAXED);\n\t\tdata->n_spin_acquired++;\n\t\treturn;\n\t}\n\n\t/* True slow path. */\n\tmalloc_mutex_lock_final(mutex);\n\t/* Update more slow-path only counters. */\n\tatomic_fetch_sub_u32(&data->n_waiting_thds, 1, ATOMIC_RELAXED);\n\tnstime_update(&after);\n\n\tnstime_t delta;\n\tnstime_copy(&delta, &after);\n\tnstime_subtract(&delta, &before);\n\n\tdata->n_wait_times++;\n\tnstime_add(&data->tot_wait_time, &delta);\n\tif (nstime_compare(&data->max_wait_time, &delta) < 0) {\n\t\tnstime_copy(&data->max_wait_time, &delta);\n\t}\n\tif (n_thds > data->max_n_thds) {\n\t\tdata->max_n_thds = n_thds;\n\t}\n}\n\nstatic void\nmutex_prof_data_init(mutex_prof_data_t *data) {\n\tmemset(data, 0, sizeof(mutex_prof_data_t));\n\tnstime_init(&data->max_wait_time, 0);\n\tnstime_init(&data->tot_wait_time, 0);\n\tdata->prev_owner = NULL;\n}\n\nvoid\nmalloc_mutex_prof_data_reset(tsdn_t *tsdn, malloc_mutex_t *mutex) {\n\tmalloc_mutex_assert_owner(tsdn, mutex);\n\tmutex_prof_data_init(&mutex->prof_data);\n}\n\nstatic int\nmutex_addr_comp(const witness_t *witness1, void *mutex1,\n    const witness_t *witness2, void *mutex2) {\n\tassert(mutex1 != NULL);\n\tassert(mutex2 != NULL);\n\tuintptr_t mu1int = (uintptr_t)mutex1;\n\tuintptr_t mu2int = (uintptr_t)mutex2;\n\tif (mu1int < mu2int) {\n\t\treturn -1;\n\t} else if (mu1int == mu2int) {\n\t\treturn 0;\n\t} else {\n\t\treturn 1;\n\t}\n}\n\nbool\nmalloc_mutex_init(malloc_mutex_t *mutex, const char *name,\n    witness_rank_t rank, malloc_mutex_lock_order_t lock_order) {\n\tmutex_prof_data_init(&mutex->prof_data);\n#ifdef _WIN32\n#  if _WIN32_WINNT >= 0x0600\n\tInitializeSRWLock(&mutex->lock);\n#  else\n\tif (!InitializeCriticalSectionAndSpinCount(&mutex->lock,\n\t    _CRT_SPINCOUNT)) {\n\t\treturn true;\n\t}\n#  endif\n#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))\n\tmutex->lock = OS_UNFAIR_LOCK_INIT;\n#elif (defined(JEMALLOC_OSSPIN))\n\tmutex->lock = 0;\n#elif (defined(JEMALLOC_MUTEX_INIT_CB))\n\tif (postpone_init) {\n\t\tmutex->postponed_next = postponed_mutexes;\n\t\tpostponed_mutexes = mutex;\n\t} else {\n\t\tif (_pthread_mutex_init_calloc_cb(&mutex->lock,\n\t\t    bootstrap_calloc) != 0) {\n\t\t\treturn true;\n\t\t}\n\t}\n#else\n\tpthread_mutexattr_t attr;\n\n\tif (pthread_mutexattr_init(&attr) != 0) {\n\t\treturn true;\n\t}\n\tpthread_mutexattr_settype(&attr, MALLOC_MUTEX_TYPE);\n\tif (pthread_mutex_init(&mutex->lock, &attr) != 0) {\n\t\tpthread_mutexattr_destroy(&attr);\n\t\treturn true;\n\t}\n\tpthread_mutexattr_destroy(&attr);\n#endif\n\tif (config_debug) {\n\t\tmutex->lock_order = lock_order;\n\t\tif (lock_order == malloc_mutex_address_ordered) {\n\t\t\twitness_init(&mutex->witness, name, rank,\n\t\t\t    mutex_addr_comp, &mutex);\n\t\t} else {\n\t\t\twitness_init(&mutex->witness, name, rank, NULL, NULL);\n\t\t}\n\t}\n\treturn false;\n}\n\nvoid\nmalloc_mutex_prefork(tsdn_t *tsdn, malloc_mutex_t *mutex) {\n\tmalloc_mutex_lock(tsdn, mutex);\n}\n\nvoid\nmalloc_mutex_postfork_parent(tsdn_t *tsdn, malloc_mutex_t *mutex) {\n\tmalloc_mutex_unlock(tsdn, mutex);\n}\n\nvoid\nmalloc_mutex_postfork_child(tsdn_t *tsdn, malloc_mutex_t *mutex) {\n#ifdef JEMALLOC_MUTEX_INIT_CB\n\tmalloc_mutex_unlock(tsdn, mutex);\n#else\n\tif (malloc_mutex_init(mutex, mutex->witness.name,\n\t    mutex->witness.rank, mutex->lock_order)) {\n\t\tmalloc_printf(\"<jemalloc>: Error re-initializing mutex in \"\n\t\t    \"child\\n\");\n\t\tif (opt_abort) {\n\t\t\tabort();\n\t\t}\n\t}\n#endif\n}\n\nbool\nmalloc_mutex_boot(void) {\n#ifdef JEMALLOC_MUTEX_INIT_CB\n\tpostpone_init = false;\n\twhile (postponed_mutexes != NULL) {\n\t\tif (_pthread_mutex_init_calloc_cb(&postponed_mutexes->lock,\n\t\t    bootstrap_calloc) != 0) {\n\t\t\treturn true;\n\t\t}\n\t\tpostponed_mutexes = postponed_mutexes->postponed_next;\n\t}\n#endif\n\treturn false;\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/mutex_pool.c",
    "content": "#define JEMALLOC_MUTEX_POOL_C_\n\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/mutex_pool.h\"\n\nbool\nmutex_pool_init(mutex_pool_t *pool, const char *name, witness_rank_t rank) {\n\tfor (int i = 0; i < MUTEX_POOL_SIZE; ++i) {\n\t\tif (malloc_mutex_init(&pool->mutexes[i], name, rank,\n\t\t    malloc_mutex_address_ordered)) {\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/nstime.c",
    "content": "#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/nstime.h\"\n\n#include \"jemalloc/internal/assert.h\"\n\n#define BILLION\tUINT64_C(1000000000)\n#define MILLION\tUINT64_C(1000000)\n\nvoid\nnstime_init(nstime_t *time, uint64_t ns) {\n\ttime->ns = ns;\n}\n\nvoid\nnstime_init2(nstime_t *time, uint64_t sec, uint64_t nsec) {\n\ttime->ns = sec * BILLION + nsec;\n}\n\nuint64_t\nnstime_ns(const nstime_t *time) {\n\treturn time->ns;\n}\n\nuint64_t\nnstime_msec(const nstime_t *time) {\n\treturn time->ns / MILLION;\n}\n\nuint64_t\nnstime_sec(const nstime_t *time) {\n\treturn time->ns / BILLION;\n}\n\nuint64_t\nnstime_nsec(const nstime_t *time) {\n\treturn time->ns % BILLION;\n}\n\nvoid\nnstime_copy(nstime_t *time, const nstime_t *source) {\n\t*time = *source;\n}\n\nint\nnstime_compare(const nstime_t *a, const nstime_t *b) {\n\treturn (a->ns > b->ns) - (a->ns < b->ns);\n}\n\nvoid\nnstime_add(nstime_t *time, const nstime_t *addend) {\n\tassert(UINT64_MAX - time->ns >= addend->ns);\n\n\ttime->ns += addend->ns;\n}\n\nvoid\nnstime_iadd(nstime_t *time, uint64_t addend) {\n\tassert(UINT64_MAX - time->ns >= addend);\n\n\ttime->ns += addend;\n}\n\nvoid\nnstime_subtract(nstime_t *time, const nstime_t *subtrahend) {\n\tassert(nstime_compare(time, subtrahend) >= 0);\n\n\ttime->ns -= subtrahend->ns;\n}\n\nvoid\nnstime_isubtract(nstime_t *time, uint64_t subtrahend) {\n\tassert(time->ns >= subtrahend);\n\n\ttime->ns -= subtrahend;\n}\n\nvoid\nnstime_imultiply(nstime_t *time, uint64_t multiplier) {\n\tassert((((time->ns | multiplier) & (UINT64_MAX << (sizeof(uint64_t) <<\n\t    2))) == 0) || ((time->ns * multiplier) / multiplier == time->ns));\n\n\ttime->ns *= multiplier;\n}\n\nvoid\nnstime_idivide(nstime_t *time, uint64_t divisor) {\n\tassert(divisor != 0);\n\n\ttime->ns /= divisor;\n}\n\nuint64_t\nnstime_divide(const nstime_t *time, const nstime_t *divisor) {\n\tassert(divisor->ns != 0);\n\n\treturn time->ns / divisor->ns;\n}\n\n#ifdef _WIN32\n#  define NSTIME_MONOTONIC true\nstatic void\nnstime_get(nstime_t *time) {\n\tFILETIME ft;\n\tuint64_t ticks_100ns;\n\n\tGetSystemTimeAsFileTime(&ft);\n\tticks_100ns = (((uint64_t)ft.dwHighDateTime) << 32) | ft.dwLowDateTime;\n\n\tnstime_init(time, ticks_100ns * 100);\n}\n#elif defined(JEMALLOC_HAVE_CLOCK_MONOTONIC_COARSE)\n#  define NSTIME_MONOTONIC true\nstatic void\nnstime_get(nstime_t *time) {\n\tstruct timespec ts;\n\n\tclock_gettime(CLOCK_MONOTONIC_COARSE, &ts);\n\tnstime_init2(time, ts.tv_sec, ts.tv_nsec);\n}\n#elif defined(JEMALLOC_HAVE_CLOCK_MONOTONIC)\n#  define NSTIME_MONOTONIC true\nstatic void\nnstime_get(nstime_t *time) {\n\tstruct timespec ts;\n\n\tclock_gettime(CLOCK_MONOTONIC, &ts);\n\tnstime_init2(time, ts.tv_sec, ts.tv_nsec);\n}\n#elif defined(JEMALLOC_HAVE_MACH_ABSOLUTE_TIME)\n#  define NSTIME_MONOTONIC true\nstatic void\nnstime_get(nstime_t *time) {\n\tnstime_init(time, mach_absolute_time());\n}\n#else\n#  define NSTIME_MONOTONIC false\nstatic void\nnstime_get(nstime_t *time) {\n\tstruct timeval tv;\n\n\tgettimeofday(&tv, NULL);\n\tnstime_init2(time, tv.tv_sec, tv.tv_usec * 1000);\n}\n#endif\n\nstatic bool\nnstime_monotonic_impl(void) {\n\treturn NSTIME_MONOTONIC;\n#undef NSTIME_MONOTONIC\n}\nnstime_monotonic_t *JET_MUTABLE nstime_monotonic = nstime_monotonic_impl;\n\nstatic bool\nnstime_update_impl(nstime_t *time) {\n\tnstime_t old_time;\n\n\tnstime_copy(&old_time, time);\n\tnstime_get(time);\n\n\t/* Handle non-monotonic clocks. */\n\tif (unlikely(nstime_compare(&old_time, time) > 0)) {\n\t\tnstime_copy(time, &old_time);\n\t\treturn true;\n\t}\n\n\treturn false;\n}\nnstime_update_t *JET_MUTABLE nstime_update = nstime_update_impl;\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/pages.c",
    "content": "#define JEMALLOC_PAGES_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n\n#include \"jemalloc/internal/pages.h\"\n\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/malloc_io.h\"\n\n#ifdef JEMALLOC_SYSCTL_VM_OVERCOMMIT\n#include <sys/sysctl.h>\n#endif\n\n/******************************************************************************/\n/* Data. */\n\n/* Actual operating system page size, detected during bootstrap, <= PAGE. */\nstatic size_t\tos_page;\n\n#ifndef _WIN32\n#  define PAGES_PROT_COMMIT (PROT_READ | PROT_WRITE)\n#  define PAGES_PROT_DECOMMIT (PROT_NONE)\nstatic int\tmmap_flags;\n#endif\nstatic bool\tos_overcommits;\n\n/******************************************************************************/\n/*\n * Function prototypes for static functions that are referenced prior to\n * definition.\n */\n\nstatic void os_pages_unmap(void *addr, size_t size);\n\n/******************************************************************************/\n\nstatic void *\nos_pages_map(void *addr, size_t size, size_t alignment, bool *commit) {\n\tassert(ALIGNMENT_ADDR2BASE(addr, os_page) == addr);\n\tassert(ALIGNMENT_CEILING(size, os_page) == size);\n\tassert(size != 0);\n\n\tif (os_overcommits) {\n\t\t*commit = true;\n\t}\n\n\tvoid *ret;\n#ifdef _WIN32\n\t/*\n\t * If VirtualAlloc can't allocate at the given address when one is\n\t * given, it fails and returns NULL.\n\t */\n\tret = VirtualAlloc(addr, size, MEM_RESERVE | (*commit ? MEM_COMMIT : 0),\n\t    PAGE_READWRITE);\n#else\n\t/*\n\t * We don't use MAP_FIXED here, because it can cause the *replacement*\n\t * of existing mappings, and we only want to create new mappings.\n\t */\n\t{\n\t\tint prot = *commit ? PAGES_PROT_COMMIT : PAGES_PROT_DECOMMIT;\n\n\t\tret = mmap(addr, size, prot, mmap_flags, -1, 0);\n\t}\n\tassert(ret != NULL);\n\n\tif (ret == MAP_FAILED) {\n\t\tret = NULL;\n\t} else if (addr != NULL && ret != addr) {\n\t\t/*\n\t\t * We succeeded in mapping memory, but not in the right place.\n\t\t */\n\t\tos_pages_unmap(ret, size);\n\t\tret = NULL;\n\t}\n#endif\n\tassert(ret == NULL || (addr == NULL && ret != addr) || (addr != NULL &&\n\t    ret == addr));\n\treturn ret;\n}\n\nstatic void *\nos_pages_trim(void *addr, size_t alloc_size, size_t leadsize, size_t size,\n    bool *commit) {\n\tvoid *ret = (void *)((uintptr_t)addr + leadsize);\n\n\tassert(alloc_size >= leadsize + size);\n#ifdef _WIN32\n\tos_pages_unmap(addr, alloc_size);\n\tvoid *new_addr = os_pages_map(ret, size, PAGE, commit);\n\tif (new_addr == ret) {\n\t\treturn ret;\n\t}\n\tif (new_addr != NULL) {\n\t\tos_pages_unmap(new_addr, size);\n\t}\n\treturn NULL;\n#else\n\tsize_t trailsize = alloc_size - leadsize - size;\n\n\tif (leadsize != 0) {\n\t\tos_pages_unmap(addr, leadsize);\n\t}\n\tif (trailsize != 0) {\n\t\tos_pages_unmap((void *)((uintptr_t)ret + size), trailsize);\n\t}\n\treturn ret;\n#endif\n}\n\nstatic void\nos_pages_unmap(void *addr, size_t size) {\n\tassert(ALIGNMENT_ADDR2BASE(addr, os_page) == addr);\n\tassert(ALIGNMENT_CEILING(size, os_page) == size);\n\n#ifdef _WIN32\n\tif (VirtualFree(addr, 0, MEM_RELEASE) == 0)\n#else\n\tif (munmap(addr, size) == -1)\n#endif\n\t{\n\t\tchar buf[BUFERROR_BUF];\n\n\t\tbuferror(get_errno(), buf, sizeof(buf));\n\t\tmalloc_printf(\"<jemalloc>: Error in \"\n#ifdef _WIN32\n\t\t    \"VirtualFree\"\n#else\n\t\t    \"munmap\"\n#endif\n\t\t    \"(): %s\\n\", buf);\n\t\tif (opt_abort) {\n\t\t\tabort();\n\t\t}\n\t}\n}\n\nstatic void *\npages_map_slow(size_t size, size_t alignment, bool *commit) {\n\tsize_t alloc_size = size + alignment - os_page;\n\t/* Beware size_t wrap-around. */\n\tif (alloc_size < size) {\n\t\treturn NULL;\n\t}\n\n\tvoid *ret;\n\tdo {\n\t\tvoid *pages = os_pages_map(NULL, alloc_size, alignment, commit);\n\t\tif (pages == NULL) {\n\t\t\treturn NULL;\n\t\t}\n\t\tsize_t leadsize = ALIGNMENT_CEILING((uintptr_t)pages, alignment)\n\t\t    - (uintptr_t)pages;\n\t\tret = os_pages_trim(pages, alloc_size, leadsize, size, commit);\n\t} while (ret == NULL);\n\n\tassert(ret != NULL);\n\tassert(PAGE_ADDR2BASE(ret) == ret);\n\treturn ret;\n}\n\nvoid *\npages_map(void *addr, size_t size, size_t alignment, bool *commit) {\n\tassert(alignment >= PAGE);\n\tassert(ALIGNMENT_ADDR2BASE(addr, alignment) == addr);\n\n\t/*\n\t * Ideally, there would be a way to specify alignment to mmap() (like\n\t * NetBSD has), but in the absence of such a feature, we have to work\n\t * hard to efficiently create aligned mappings.  The reliable, but\n\t * slow method is to create a mapping that is over-sized, then trim the\n\t * excess.  However, that always results in one or two calls to\n\t * os_pages_unmap(), and it can leave holes in the process's virtual\n\t * memory map if memory grows downward.\n\t *\n\t * Optimistically try mapping precisely the right amount before falling\n\t * back to the slow method, with the expectation that the optimistic\n\t * approach works most of the time.\n\t */\n\n\tvoid *ret = os_pages_map(addr, size, os_page, commit);\n\tif (ret == NULL || ret == addr) {\n\t\treturn ret;\n\t}\n\tassert(addr == NULL);\n\tif (ALIGNMENT_ADDR2OFFSET(ret, alignment) != 0) {\n\t\tos_pages_unmap(ret, size);\n\t\treturn pages_map_slow(size, alignment, commit);\n\t}\n\n\tassert(PAGE_ADDR2BASE(ret) == ret);\n\treturn ret;\n}\n\nvoid\npages_unmap(void *addr, size_t size) {\n\tassert(PAGE_ADDR2BASE(addr) == addr);\n\tassert(PAGE_CEILING(size) == size);\n\n\tos_pages_unmap(addr, size);\n}\n\nstatic bool\npages_commit_impl(void *addr, size_t size, bool commit) {\n\tassert(PAGE_ADDR2BASE(addr) == addr);\n\tassert(PAGE_CEILING(size) == size);\n\n\tif (os_overcommits) {\n\t\treturn true;\n\t}\n\n#ifdef _WIN32\n\treturn (commit ? (addr != VirtualAlloc(addr, size, MEM_COMMIT,\n\t    PAGE_READWRITE)) : (!VirtualFree(addr, size, MEM_DECOMMIT)));\n#else\n\t{\n\t\tint prot = commit ? PAGES_PROT_COMMIT : PAGES_PROT_DECOMMIT;\n\t\tvoid *result = mmap(addr, size, prot, mmap_flags | MAP_FIXED,\n\t\t    -1, 0);\n\t\tif (result == MAP_FAILED) {\n\t\t\treturn true;\n\t\t}\n\t\tif (result != addr) {\n\t\t\t/*\n\t\t\t * We succeeded in mapping memory, but not in the right\n\t\t\t * place.\n\t\t\t */\n\t\t\tos_pages_unmap(result, size);\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n#endif\n}\n\nbool\npages_commit(void *addr, size_t size) {\n\treturn pages_commit_impl(addr, size, true);\n}\n\nbool\npages_decommit(void *addr, size_t size) {\n\treturn pages_commit_impl(addr, size, false);\n}\n\nbool\npages_purge_lazy(void *addr, size_t size) {\n\tassert(PAGE_ADDR2BASE(addr) == addr);\n\tassert(PAGE_CEILING(size) == size);\n\n\tif (!pages_can_purge_lazy) {\n\t\treturn true;\n\t}\n\n#ifdef _WIN32\n\tVirtualAlloc(addr, size, MEM_RESET, PAGE_READWRITE);\n\treturn false;\n#elif defined(JEMALLOC_PURGE_MADVISE_FREE)\n\treturn (madvise(addr, size, MADV_FREE) != 0);\n#elif defined(JEMALLOC_PURGE_MADVISE_DONTNEED) && \\\n    !defined(JEMALLOC_PURGE_MADVISE_DONTNEED_ZEROS)\n\treturn (madvise(addr, size, MADV_DONTNEED) != 0);\n#else\n\tnot_reached();\n#endif\n}\n\nbool\npages_purge_forced(void *addr, size_t size) {\n\tassert(PAGE_ADDR2BASE(addr) == addr);\n\tassert(PAGE_CEILING(size) == size);\n\n\tif (!pages_can_purge_forced) {\n\t\treturn true;\n\t}\n\n#if defined(JEMALLOC_PURGE_MADVISE_DONTNEED) && \\\n    defined(JEMALLOC_PURGE_MADVISE_DONTNEED_ZEROS)\n\treturn (madvise(addr, size, MADV_DONTNEED) != 0);\n#elif defined(JEMALLOC_MAPS_COALESCE)\n\t/* Try to overlay a new demand-zeroed mapping. */\n\treturn pages_commit(addr, size);\n#else\n\tnot_reached();\n#endif\n}\n\nbool\npages_huge(void *addr, size_t size) {\n\tassert(HUGEPAGE_ADDR2BASE(addr) == addr);\n\tassert(HUGEPAGE_CEILING(size) == size);\n\n#ifdef JEMALLOC_THP\n\treturn (madvise(addr, size, MADV_HUGEPAGE) != 0);\n#else\n\treturn true;\n#endif\n}\n\nbool\npages_nohuge(void *addr, size_t size) {\n\tassert(HUGEPAGE_ADDR2BASE(addr) == addr);\n\tassert(HUGEPAGE_CEILING(size) == size);\n\n#ifdef JEMALLOC_THP\n\treturn (madvise(addr, size, MADV_NOHUGEPAGE) != 0);\n#else\n\treturn false;\n#endif\n}\n\nstatic size_t\nos_page_detect(void) {\n#ifdef _WIN32\n\tSYSTEM_INFO si;\n\tGetSystemInfo(&si);\n\treturn si.dwPageSize;\n#else\n\tlong result = sysconf(_SC_PAGESIZE);\n\tif (result == -1) {\n\t\treturn LG_PAGE;\n\t}\n\treturn (size_t)result;\n#endif\n}\n\n#ifdef JEMALLOC_SYSCTL_VM_OVERCOMMIT\nstatic bool\nos_overcommits_sysctl(void) {\n\tint vm_overcommit;\n\tsize_t sz;\n\n\tsz = sizeof(vm_overcommit);\n\tif (sysctlbyname(\"vm.overcommit\", &vm_overcommit, &sz, NULL, 0) != 0) {\n\t\treturn false; /* Error. */\n\t}\n\n\treturn ((vm_overcommit & 0x3) == 0);\n}\n#endif\n\n#ifdef JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY\n/*\n * Use syscall(2) rather than {open,read,close}(2) when possible to avoid\n * reentry during bootstrapping if another library has interposed system call\n * wrappers.\n */\nstatic bool\nos_overcommits_proc(void) {\n\tint fd;\n\tchar buf[1];\n\tssize_t nread;\n\n#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_open)\n\tfd = (int)syscall(SYS_open, \"/proc/sys/vm/overcommit_memory\", O_RDONLY |\n\t    O_CLOEXEC);\n#elif defined(JEMALLOC_USE_SYSCALL) && defined(SYS_openat)\n\tfd = (int)syscall(SYS_openat,\n\t    AT_FDCWD, \"/proc/sys/vm/overcommit_memory\", O_RDONLY | O_CLOEXEC);\n#else\n\tfd = open(\"/proc/sys/vm/overcommit_memory\", O_RDONLY | O_CLOEXEC);\n#endif\n\tif (fd == -1) {\n\t\treturn false; /* Error. */\n\t}\n\n#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_read)\n\tnread = (ssize_t)syscall(SYS_read, fd, &buf, sizeof(buf));\n#else\n\tnread = read(fd, &buf, sizeof(buf));\n#endif\n\n#if defined(JEMALLOC_USE_SYSCALL) && defined(SYS_close)\n\tsyscall(SYS_close, fd);\n#else\n\tclose(fd);\n#endif\n\n\tif (nread < 1) {\n\t\treturn false; /* Error. */\n\t}\n\t/*\n\t * /proc/sys/vm/overcommit_memory meanings:\n\t * 0: Heuristic overcommit.\n\t * 1: Always overcommit.\n\t * 2: Never overcommit.\n\t */\n\treturn (buf[0] == '0' || buf[0] == '1');\n}\n#endif\n\nbool\npages_boot(void) {\n\tos_page = os_page_detect();\n\tif (os_page > PAGE) {\n\t\tmalloc_write(\"<jemalloc>: Unsupported system page size\\n\");\n\t\tif (opt_abort) {\n\t\t\tabort();\n\t\t}\n\t\treturn true;\n\t}\n\n#ifndef _WIN32\n\tmmap_flags = MAP_PRIVATE | MAP_ANON;\n#endif\n\n#ifdef JEMALLOC_SYSCTL_VM_OVERCOMMIT\n\tos_overcommits = os_overcommits_sysctl();\n#elif defined(JEMALLOC_PROC_SYS_VM_OVERCOMMIT_MEMORY)\n\tos_overcommits = os_overcommits_proc();\n#  ifdef MAP_NORESERVE\n\tif (os_overcommits) {\n\t\tmmap_flags |= MAP_NORESERVE;\n\t}\n#  endif\n#else\n\tos_overcommits = false;\n#endif\n\n\treturn false;\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/prng.c",
    "content": "#define JEMALLOC_PRNG_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/prof.c",
    "content": "#define JEMALLOC_PROF_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/ckh.h\"\n#include \"jemalloc/internal/hash.h\"\n#include \"jemalloc/internal/malloc_io.h\"\n#include \"jemalloc/internal/mutex.h\"\n\n/******************************************************************************/\n\n#ifdef JEMALLOC_PROF_LIBUNWIND\n#define UNW_LOCAL_ONLY\n#include <libunwind.h>\n#endif\n\n#ifdef JEMALLOC_PROF_LIBGCC\n/*\n * We have a circular dependency -- jemalloc_internal.h tells us if we should\n * use libgcc's unwinding functionality, but after we've included that, we've\n * already hooked _Unwind_Backtrace.  We'll temporarily disable hooking.\n */\n#undef _Unwind_Backtrace\n#include <unwind.h>\n#define _Unwind_Backtrace JEMALLOC_HOOK(_Unwind_Backtrace, hooks_libc_hook)\n#endif\n\n/******************************************************************************/\n/* Data. */\n\nbool\t\topt_prof = false;\nbool\t\topt_prof_active = true;\nbool\t\topt_prof_thread_active_init = true;\nsize_t\t\topt_lg_prof_sample = LG_PROF_SAMPLE_DEFAULT;\nssize_t\t\topt_lg_prof_interval = LG_PROF_INTERVAL_DEFAULT;\nbool\t\topt_prof_gdump = false;\nbool\t\topt_prof_final = false;\nbool\t\topt_prof_leak = false;\nbool\t\topt_prof_accum = false;\nchar\t\topt_prof_prefix[\n    /* Minimize memory bloat for non-prof builds. */\n#ifdef JEMALLOC_PROF\n    PATH_MAX +\n#endif\n    1];\n\n/*\n * Initialized as opt_prof_active, and accessed via\n * prof_active_[gs]et{_unlocked,}().\n */\nbool\t\t\tprof_active;\nstatic malloc_mutex_t\tprof_active_mtx;\n\n/*\n * Initialized as opt_prof_thread_active_init, and accessed via\n * prof_thread_active_init_[gs]et().\n */\nstatic bool\t\tprof_thread_active_init;\nstatic malloc_mutex_t\tprof_thread_active_init_mtx;\n\n/*\n * Initialized as opt_prof_gdump, and accessed via\n * prof_gdump_[gs]et{_unlocked,}().\n */\nbool\t\t\tprof_gdump_val;\nstatic malloc_mutex_t\tprof_gdump_mtx;\n\nuint64_t\tprof_interval = 0;\n\nsize_t\t\tlg_prof_sample;\n\n/*\n * Table of mutexes that are shared among gctx's.  These are leaf locks, so\n * there is no problem with using them for more than one gctx at the same time.\n * The primary motivation for this sharing though is that gctx's are ephemeral,\n * and destroying mutexes causes complications for systems that allocate when\n * creating/destroying mutexes.\n */\nstatic malloc_mutex_t\t*gctx_locks;\nstatic atomic_u_t\tcum_gctxs; /* Atomic counter. */\n\n/*\n * Table of mutexes that are shared among tdata's.  No operations require\n * holding multiple tdata locks, so there is no problem with using them for more\n * than one tdata at the same time, even though a gctx lock may be acquired\n * while holding a tdata lock.\n */\nstatic malloc_mutex_t\t*tdata_locks;\n\n/*\n * Global hash of (prof_bt_t *)-->(prof_gctx_t *).  This is the master data\n * structure that knows about all backtraces currently captured.\n */\nstatic ckh_t\t\tbt2gctx;\n/* Non static to enable profiling. */\nmalloc_mutex_t\t\tbt2gctx_mtx;\n\n/*\n * Tree of all extant prof_tdata_t structures, regardless of state,\n * {attached,detached,expired}.\n */\nstatic prof_tdata_tree_t\ttdatas;\nstatic malloc_mutex_t\ttdatas_mtx;\n\nstatic uint64_t\t\tnext_thr_uid;\nstatic malloc_mutex_t\tnext_thr_uid_mtx;\n\nstatic malloc_mutex_t\tprof_dump_seq_mtx;\nstatic uint64_t\t\tprof_dump_seq;\nstatic uint64_t\t\tprof_dump_iseq;\nstatic uint64_t\t\tprof_dump_mseq;\nstatic uint64_t\t\tprof_dump_useq;\n\n/*\n * This buffer is rather large for stack allocation, so use a single buffer for\n * all profile dumps.\n */\nstatic malloc_mutex_t\tprof_dump_mtx;\nstatic char\t\tprof_dump_buf[\n    /* Minimize memory bloat for non-prof builds. */\n#ifdef JEMALLOC_PROF\n    PROF_DUMP_BUFSIZE\n#else\n    1\n#endif\n];\nstatic size_t\t\tprof_dump_buf_end;\nstatic int\t\tprof_dump_fd;\n\n/* Do not dump any profiles until bootstrapping is complete. */\nstatic bool\t\tprof_booted = false;\n\n/******************************************************************************/\n/*\n * Function prototypes for static functions that are referenced prior to\n * definition.\n */\n\nstatic bool\tprof_tctx_should_destroy(tsdn_t *tsdn, prof_tctx_t *tctx);\nstatic void\tprof_tctx_destroy(tsd_t *tsd, prof_tctx_t *tctx);\nstatic bool\tprof_tdata_should_destroy(tsdn_t *tsdn, prof_tdata_t *tdata,\n    bool even_if_attached);\nstatic void\tprof_tdata_destroy(tsd_t *tsd, prof_tdata_t *tdata,\n    bool even_if_attached);\nstatic char\t*prof_thread_name_alloc(tsdn_t *tsdn, const char *thread_name);\n\n/******************************************************************************/\n/* Red-black trees. */\n\nstatic int\nprof_tctx_comp(const prof_tctx_t *a, const prof_tctx_t *b) {\n\tuint64_t a_thr_uid = a->thr_uid;\n\tuint64_t b_thr_uid = b->thr_uid;\n\tint ret = (a_thr_uid > b_thr_uid) - (a_thr_uid < b_thr_uid);\n\tif (ret == 0) {\n\t\tuint64_t a_thr_discrim = a->thr_discrim;\n\t\tuint64_t b_thr_discrim = b->thr_discrim;\n\t\tret = (a_thr_discrim > b_thr_discrim) - (a_thr_discrim <\n\t\t    b_thr_discrim);\n\t\tif (ret == 0) {\n\t\t\tuint64_t a_tctx_uid = a->tctx_uid;\n\t\t\tuint64_t b_tctx_uid = b->tctx_uid;\n\t\t\tret = (a_tctx_uid > b_tctx_uid) - (a_tctx_uid <\n\t\t\t    b_tctx_uid);\n\t\t}\n\t}\n\treturn ret;\n}\n\nrb_gen(static UNUSED, tctx_tree_, prof_tctx_tree_t, prof_tctx_t,\n    tctx_link, prof_tctx_comp)\n\nstatic int\nprof_gctx_comp(const prof_gctx_t *a, const prof_gctx_t *b) {\n\tunsigned a_len = a->bt.len;\n\tunsigned b_len = b->bt.len;\n\tunsigned comp_len = (a_len < b_len) ? a_len : b_len;\n\tint ret = memcmp(a->bt.vec, b->bt.vec, comp_len * sizeof(void *));\n\tif (ret == 0) {\n\t\tret = (a_len > b_len) - (a_len < b_len);\n\t}\n\treturn ret;\n}\n\nrb_gen(static UNUSED, gctx_tree_, prof_gctx_tree_t, prof_gctx_t, dump_link,\n    prof_gctx_comp)\n\nstatic int\nprof_tdata_comp(const prof_tdata_t *a, const prof_tdata_t *b) {\n\tint ret;\n\tuint64_t a_uid = a->thr_uid;\n\tuint64_t b_uid = b->thr_uid;\n\n\tret = ((a_uid > b_uid) - (a_uid < b_uid));\n\tif (ret == 0) {\n\t\tuint64_t a_discrim = a->thr_discrim;\n\t\tuint64_t b_discrim = b->thr_discrim;\n\n\t\tret = ((a_discrim > b_discrim) - (a_discrim < b_discrim));\n\t}\n\treturn ret;\n}\n\nrb_gen(static UNUSED, tdata_tree_, prof_tdata_tree_t, prof_tdata_t, tdata_link,\n    prof_tdata_comp)\n\n/******************************************************************************/\n\nvoid\nprof_alloc_rollback(tsd_t *tsd, prof_tctx_t *tctx, bool updated) {\n\tprof_tdata_t *tdata;\n\n\tcassert(config_prof);\n\n\tif (updated) {\n\t\t/*\n\t\t * Compute a new sample threshold.  This isn't very important in\n\t\t * practice, because this function is rarely executed, so the\n\t\t * potential for sample bias is minimal except in contrived\n\t\t * programs.\n\t\t */\n\t\ttdata = prof_tdata_get(tsd, true);\n\t\tif (tdata != NULL) {\n\t\t\tprof_sample_threshold_update(tdata);\n\t\t}\n\t}\n\n\tif ((uintptr_t)tctx > (uintptr_t)1U) {\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), tctx->tdata->lock);\n\t\ttctx->prepared = false;\n\t\tif (prof_tctx_should_destroy(tsd_tsdn(tsd), tctx)) {\n\t\t\tprof_tctx_destroy(tsd, tctx);\n\t\t} else {\n\t\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), tctx->tdata->lock);\n\t\t}\n\t}\n}\n\nvoid\nprof_malloc_sample_object(tsdn_t *tsdn, const void *ptr, size_t usize,\n    prof_tctx_t *tctx) {\n\tprof_tctx_set(tsdn, ptr, usize, NULL, tctx);\n\n\tmalloc_mutex_lock(tsdn, tctx->tdata->lock);\n\ttctx->cnts.curobjs++;\n\ttctx->cnts.curbytes += usize;\n\tif (opt_prof_accum) {\n\t\ttctx->cnts.accumobjs++;\n\t\ttctx->cnts.accumbytes += usize;\n\t}\n\ttctx->prepared = false;\n\tmalloc_mutex_unlock(tsdn, tctx->tdata->lock);\n}\n\nvoid\nprof_free_sampled_object(tsd_t *tsd, size_t usize, prof_tctx_t *tctx) {\n\tmalloc_mutex_lock(tsd_tsdn(tsd), tctx->tdata->lock);\n\tassert(tctx->cnts.curobjs > 0);\n\tassert(tctx->cnts.curbytes >= usize);\n\ttctx->cnts.curobjs--;\n\ttctx->cnts.curbytes -= usize;\n\n\tif (prof_tctx_should_destroy(tsd_tsdn(tsd), tctx)) {\n\t\tprof_tctx_destroy(tsd, tctx);\n\t} else {\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), tctx->tdata->lock);\n\t}\n}\n\nvoid\nbt_init(prof_bt_t *bt, void **vec) {\n\tcassert(config_prof);\n\n\tbt->vec = vec;\n\tbt->len = 0;\n}\n\nstatic void\nprof_enter(tsd_t *tsd, prof_tdata_t *tdata) {\n\tcassert(config_prof);\n\tassert(tdata == prof_tdata_get(tsd, false));\n\n\tif (tdata != NULL) {\n\t\tassert(!tdata->enq);\n\t\ttdata->enq = true;\n\t}\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &bt2gctx_mtx);\n}\n\nstatic void\nprof_leave(tsd_t *tsd, prof_tdata_t *tdata) {\n\tcassert(config_prof);\n\tassert(tdata == prof_tdata_get(tsd, false));\n\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &bt2gctx_mtx);\n\n\tif (tdata != NULL) {\n\t\tbool idump, gdump;\n\n\t\tassert(tdata->enq);\n\t\ttdata->enq = false;\n\t\tidump = tdata->enq_idump;\n\t\ttdata->enq_idump = false;\n\t\tgdump = tdata->enq_gdump;\n\t\ttdata->enq_gdump = false;\n\n\t\tif (idump) {\n\t\t\tprof_idump(tsd_tsdn(tsd));\n\t\t}\n\t\tif (gdump) {\n\t\t\tprof_gdump(tsd_tsdn(tsd));\n\t\t}\n\t}\n}\n\n#ifdef JEMALLOC_PROF_LIBUNWIND\nvoid\nprof_backtrace(prof_bt_t *bt) {\n\tint nframes;\n\n\tcassert(config_prof);\n\tassert(bt->len == 0);\n\tassert(bt->vec != NULL);\n\n\tnframes = unw_backtrace(bt->vec, PROF_BT_MAX);\n\tif (nframes <= 0) {\n\t\treturn;\n\t}\n\tbt->len = nframes;\n}\n#elif (defined(JEMALLOC_PROF_LIBGCC))\nstatic _Unwind_Reason_Code\nprof_unwind_init_callback(struct _Unwind_Context *context, void *arg) {\n\tcassert(config_prof);\n\n\treturn _URC_NO_REASON;\n}\n\nstatic _Unwind_Reason_Code\nprof_unwind_callback(struct _Unwind_Context *context, void *arg) {\n\tprof_unwind_data_t *data = (prof_unwind_data_t *)arg;\n\tvoid *ip;\n\n\tcassert(config_prof);\n\n\tip = (void *)_Unwind_GetIP(context);\n\tif (ip == NULL) {\n\t\treturn _URC_END_OF_STACK;\n\t}\n\tdata->bt->vec[data->bt->len] = ip;\n\tdata->bt->len++;\n\tif (data->bt->len == data->max) {\n\t\treturn _URC_END_OF_STACK;\n\t}\n\n\treturn _URC_NO_REASON;\n}\n\nvoid\nprof_backtrace(prof_bt_t *bt) {\n\tprof_unwind_data_t data = {bt, PROF_BT_MAX};\n\n\tcassert(config_prof);\n\n\t_Unwind_Backtrace(prof_unwind_callback, &data);\n}\n#elif (defined(JEMALLOC_PROF_GCC))\nvoid\nprof_backtrace(prof_bt_t *bt) {\n#define BT_FRAME(i)\t\t\t\t\t\t\t\\\n\tif ((i) < PROF_BT_MAX) {\t\t\t\t\t\\\n\t\tvoid *p;\t\t\t\t\t\t\\\n\t\tif (__builtin_frame_address(i) == 0) {\t\t\t\\\n\t\t\treturn;\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\tp = __builtin_return_address(i);\t\t\t\\\n\t\tif (p == NULL) {\t\t\t\t\t\\\n\t\t\treturn;\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\tbt->vec[(i)] = p;\t\t\t\t\t\\\n\t\tbt->len = (i) + 1;\t\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t\treturn;\t\t\t\t\t\t\t\\\n\t}\n\n\tcassert(config_prof);\n\n\tBT_FRAME(0)\n\tBT_FRAME(1)\n\tBT_FRAME(2)\n\tBT_FRAME(3)\n\tBT_FRAME(4)\n\tBT_FRAME(5)\n\tBT_FRAME(6)\n\tBT_FRAME(7)\n\tBT_FRAME(8)\n\tBT_FRAME(9)\n\n\tBT_FRAME(10)\n\tBT_FRAME(11)\n\tBT_FRAME(12)\n\tBT_FRAME(13)\n\tBT_FRAME(14)\n\tBT_FRAME(15)\n\tBT_FRAME(16)\n\tBT_FRAME(17)\n\tBT_FRAME(18)\n\tBT_FRAME(19)\n\n\tBT_FRAME(20)\n\tBT_FRAME(21)\n\tBT_FRAME(22)\n\tBT_FRAME(23)\n\tBT_FRAME(24)\n\tBT_FRAME(25)\n\tBT_FRAME(26)\n\tBT_FRAME(27)\n\tBT_FRAME(28)\n\tBT_FRAME(29)\n\n\tBT_FRAME(30)\n\tBT_FRAME(31)\n\tBT_FRAME(32)\n\tBT_FRAME(33)\n\tBT_FRAME(34)\n\tBT_FRAME(35)\n\tBT_FRAME(36)\n\tBT_FRAME(37)\n\tBT_FRAME(38)\n\tBT_FRAME(39)\n\n\tBT_FRAME(40)\n\tBT_FRAME(41)\n\tBT_FRAME(42)\n\tBT_FRAME(43)\n\tBT_FRAME(44)\n\tBT_FRAME(45)\n\tBT_FRAME(46)\n\tBT_FRAME(47)\n\tBT_FRAME(48)\n\tBT_FRAME(49)\n\n\tBT_FRAME(50)\n\tBT_FRAME(51)\n\tBT_FRAME(52)\n\tBT_FRAME(53)\n\tBT_FRAME(54)\n\tBT_FRAME(55)\n\tBT_FRAME(56)\n\tBT_FRAME(57)\n\tBT_FRAME(58)\n\tBT_FRAME(59)\n\n\tBT_FRAME(60)\n\tBT_FRAME(61)\n\tBT_FRAME(62)\n\tBT_FRAME(63)\n\tBT_FRAME(64)\n\tBT_FRAME(65)\n\tBT_FRAME(66)\n\tBT_FRAME(67)\n\tBT_FRAME(68)\n\tBT_FRAME(69)\n\n\tBT_FRAME(70)\n\tBT_FRAME(71)\n\tBT_FRAME(72)\n\tBT_FRAME(73)\n\tBT_FRAME(74)\n\tBT_FRAME(75)\n\tBT_FRAME(76)\n\tBT_FRAME(77)\n\tBT_FRAME(78)\n\tBT_FRAME(79)\n\n\tBT_FRAME(80)\n\tBT_FRAME(81)\n\tBT_FRAME(82)\n\tBT_FRAME(83)\n\tBT_FRAME(84)\n\tBT_FRAME(85)\n\tBT_FRAME(86)\n\tBT_FRAME(87)\n\tBT_FRAME(88)\n\tBT_FRAME(89)\n\n\tBT_FRAME(90)\n\tBT_FRAME(91)\n\tBT_FRAME(92)\n\tBT_FRAME(93)\n\tBT_FRAME(94)\n\tBT_FRAME(95)\n\tBT_FRAME(96)\n\tBT_FRAME(97)\n\tBT_FRAME(98)\n\tBT_FRAME(99)\n\n\tBT_FRAME(100)\n\tBT_FRAME(101)\n\tBT_FRAME(102)\n\tBT_FRAME(103)\n\tBT_FRAME(104)\n\tBT_FRAME(105)\n\tBT_FRAME(106)\n\tBT_FRAME(107)\n\tBT_FRAME(108)\n\tBT_FRAME(109)\n\n\tBT_FRAME(110)\n\tBT_FRAME(111)\n\tBT_FRAME(112)\n\tBT_FRAME(113)\n\tBT_FRAME(114)\n\tBT_FRAME(115)\n\tBT_FRAME(116)\n\tBT_FRAME(117)\n\tBT_FRAME(118)\n\tBT_FRAME(119)\n\n\tBT_FRAME(120)\n\tBT_FRAME(121)\n\tBT_FRAME(122)\n\tBT_FRAME(123)\n\tBT_FRAME(124)\n\tBT_FRAME(125)\n\tBT_FRAME(126)\n\tBT_FRAME(127)\n#undef BT_FRAME\n}\n#else\nvoid\nprof_backtrace(prof_bt_t *bt) {\n\tcassert(config_prof);\n\tnot_reached();\n}\n#endif\n\nstatic malloc_mutex_t *\nprof_gctx_mutex_choose(void) {\n\tunsigned ngctxs = atomic_fetch_add_u(&cum_gctxs, 1, ATOMIC_RELAXED);\n\n\treturn &gctx_locks[(ngctxs - 1) % PROF_NCTX_LOCKS];\n}\n\nstatic malloc_mutex_t *\nprof_tdata_mutex_choose(uint64_t thr_uid) {\n\treturn &tdata_locks[thr_uid % PROF_NTDATA_LOCKS];\n}\n\nstatic prof_gctx_t *\nprof_gctx_create(tsdn_t *tsdn, prof_bt_t *bt) {\n\t/*\n\t * Create a single allocation that has space for vec of length bt->len.\n\t */\n\tsize_t size = offsetof(prof_gctx_t, vec) + (bt->len * sizeof(void *));\n\tprof_gctx_t *gctx = (prof_gctx_t *)iallocztm(tsdn, size,\n\t    sz_size2index(size), false, NULL, true, arena_get(TSDN_NULL, 0, true),\n\t    true);\n\tif (gctx == NULL) {\n\t\treturn NULL;\n\t}\n\tgctx->lock = prof_gctx_mutex_choose();\n\t/*\n\t * Set nlimbo to 1, in order to avoid a race condition with\n\t * prof_tctx_destroy()/prof_gctx_try_destroy().\n\t */\n\tgctx->nlimbo = 1;\n\ttctx_tree_new(&gctx->tctxs);\n\t/* Duplicate bt. */\n\tmemcpy(gctx->vec, bt->vec, bt->len * sizeof(void *));\n\tgctx->bt.vec = gctx->vec;\n\tgctx->bt.len = bt->len;\n\treturn gctx;\n}\n\nstatic void\nprof_gctx_try_destroy(tsd_t *tsd, prof_tdata_t *tdata_self, prof_gctx_t *gctx,\n    prof_tdata_t *tdata) {\n\tcassert(config_prof);\n\n\t/*\n\t * Check that gctx is still unused by any thread cache before destroying\n\t * it.  prof_lookup() increments gctx->nlimbo in order to avoid a race\n\t * condition with this function, as does prof_tctx_destroy() in order to\n\t * avoid a race between the main body of prof_tctx_destroy() and entry\n\t * into this function.\n\t */\n\tprof_enter(tsd, tdata_self);\n\tmalloc_mutex_lock(tsd_tsdn(tsd), gctx->lock);\n\tassert(gctx->nlimbo != 0);\n\tif (tctx_tree_empty(&gctx->tctxs) && gctx->nlimbo == 1) {\n\t\t/* Remove gctx from bt2gctx. */\n\t\tif (ckh_remove(tsd, &bt2gctx, &gctx->bt, NULL, NULL)) {\n\t\t\tnot_reached();\n\t\t}\n\t\tprof_leave(tsd, tdata_self);\n\t\t/* Destroy gctx. */\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock);\n\t\tidalloctm(tsd_tsdn(tsd), gctx, NULL, NULL, true, true);\n\t} else {\n\t\t/*\n\t\t * Compensate for increment in prof_tctx_destroy() or\n\t\t * prof_lookup().\n\t\t */\n\t\tgctx->nlimbo--;\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock);\n\t\tprof_leave(tsd, tdata_self);\n\t}\n}\n\nstatic bool\nprof_tctx_should_destroy(tsdn_t *tsdn, prof_tctx_t *tctx) {\n\tmalloc_mutex_assert_owner(tsdn, tctx->tdata->lock);\n\n\tif (opt_prof_accum) {\n\t\treturn false;\n\t}\n\tif (tctx->cnts.curobjs != 0) {\n\t\treturn false;\n\t}\n\tif (tctx->prepared) {\n\t\treturn false;\n\t}\n\treturn true;\n}\n\nstatic bool\nprof_gctx_should_destroy(prof_gctx_t *gctx) {\n\tif (opt_prof_accum) {\n\t\treturn false;\n\t}\n\tif (!tctx_tree_empty(&gctx->tctxs)) {\n\t\treturn false;\n\t}\n\tif (gctx->nlimbo != 0) {\n\t\treturn false;\n\t}\n\treturn true;\n}\n\nstatic void\nprof_tctx_destroy(tsd_t *tsd, prof_tctx_t *tctx) {\n\tprof_tdata_t *tdata = tctx->tdata;\n\tprof_gctx_t *gctx = tctx->gctx;\n\tbool destroy_tdata, destroy_tctx, destroy_gctx;\n\n\tmalloc_mutex_assert_owner(tsd_tsdn(tsd), tctx->tdata->lock);\n\n\tassert(tctx->cnts.curobjs == 0);\n\tassert(tctx->cnts.curbytes == 0);\n\tassert(!opt_prof_accum);\n\tassert(tctx->cnts.accumobjs == 0);\n\tassert(tctx->cnts.accumbytes == 0);\n\n\tckh_remove(tsd, &tdata->bt2tctx, &gctx->bt, NULL, NULL);\n\tdestroy_tdata = prof_tdata_should_destroy(tsd_tsdn(tsd), tdata, false);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), tdata->lock);\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), gctx->lock);\n\tswitch (tctx->state) {\n\tcase prof_tctx_state_nominal:\n\t\ttctx_tree_remove(&gctx->tctxs, tctx);\n\t\tdestroy_tctx = true;\n\t\tif (prof_gctx_should_destroy(gctx)) {\n\t\t\t/*\n\t\t\t * Increment gctx->nlimbo in order to keep another\n\t\t\t * thread from winning the race to destroy gctx while\n\t\t\t * this one has gctx->lock dropped.  Without this, it\n\t\t\t * would be possible for another thread to:\n\t\t\t *\n\t\t\t * 1) Sample an allocation associated with gctx.\n\t\t\t * 2) Deallocate the sampled object.\n\t\t\t * 3) Successfully prof_gctx_try_destroy(gctx).\n\t\t\t *\n\t\t\t * The result would be that gctx no longer exists by the\n\t\t\t * time this thread accesses it in\n\t\t\t * prof_gctx_try_destroy().\n\t\t\t */\n\t\t\tgctx->nlimbo++;\n\t\t\tdestroy_gctx = true;\n\t\t} else {\n\t\t\tdestroy_gctx = false;\n\t\t}\n\t\tbreak;\n\tcase prof_tctx_state_dumping:\n\t\t/*\n\t\t * A dumping thread needs tctx to remain valid until dumping\n\t\t * has finished.  Change state such that the dumping thread will\n\t\t * complete destruction during a late dump iteration phase.\n\t\t */\n\t\ttctx->state = prof_tctx_state_purgatory;\n\t\tdestroy_tctx = false;\n\t\tdestroy_gctx = false;\n\t\tbreak;\n\tdefault:\n\t\tnot_reached();\n\t\tdestroy_tctx = false;\n\t\tdestroy_gctx = false;\n\t}\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock);\n\tif (destroy_gctx) {\n\t\tprof_gctx_try_destroy(tsd, prof_tdata_get(tsd, false), gctx,\n\t\t    tdata);\n\t}\n\n\tmalloc_mutex_assert_not_owner(tsd_tsdn(tsd), tctx->tdata->lock);\n\n\tif (destroy_tdata) {\n\t\tprof_tdata_destroy(tsd, tdata, false);\n\t}\n\n\tif (destroy_tctx) {\n\t\tidalloctm(tsd_tsdn(tsd), tctx, NULL, NULL, true, true);\n\t}\n}\n\nstatic bool\nprof_lookup_global(tsd_t *tsd, prof_bt_t *bt, prof_tdata_t *tdata,\n    void **p_btkey, prof_gctx_t **p_gctx, bool *p_new_gctx) {\n\tunion {\n\t\tprof_gctx_t\t*p;\n\t\tvoid\t\t*v;\n\t} gctx, tgctx;\n\tunion {\n\t\tprof_bt_t\t*p;\n\t\tvoid\t\t*v;\n\t} btkey;\n\tbool new_gctx;\n\n\tprof_enter(tsd, tdata);\n\tif (ckh_search(&bt2gctx, bt, &btkey.v, &gctx.v)) {\n\t\t/* bt has never been seen before.  Insert it. */\n\t\tprof_leave(tsd, tdata);\n\t\ttgctx.p = prof_gctx_create(tsd_tsdn(tsd), bt);\n\t\tif (tgctx.v == NULL) {\n\t\t\treturn true;\n\t\t}\n\t\tprof_enter(tsd, tdata);\n\t\tif (ckh_search(&bt2gctx, bt, &btkey.v, &gctx.v)) {\n\t\t\tgctx.p = tgctx.p;\n\t\t\tbtkey.p = &gctx.p->bt;\n\t\t\tif (ckh_insert(tsd, &bt2gctx, btkey.v, gctx.v)) {\n\t\t\t\t/* OOM. */\n\t\t\t\tprof_leave(tsd, tdata);\n\t\t\t\tidalloctm(tsd_tsdn(tsd), gctx.v, NULL, NULL,\n\t\t\t\t    true, true);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tnew_gctx = true;\n\t\t} else {\n\t\t\tnew_gctx = false;\n\t\t}\n\t} else {\n\t\ttgctx.v = NULL;\n\t\tnew_gctx = false;\n\t}\n\n\tif (!new_gctx) {\n\t\t/*\n\t\t * Increment nlimbo, in order to avoid a race condition with\n\t\t * prof_tctx_destroy()/prof_gctx_try_destroy().\n\t\t */\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), gctx.p->lock);\n\t\tgctx.p->nlimbo++;\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), gctx.p->lock);\n\t\tnew_gctx = false;\n\n\t\tif (tgctx.v != NULL) {\n\t\t\t/* Lost race to insert. */\n\t\t\tidalloctm(tsd_tsdn(tsd), tgctx.v, NULL, NULL, true,\n\t\t\t    true);\n\t\t}\n\t}\n\tprof_leave(tsd, tdata);\n\n\t*p_btkey = btkey.v;\n\t*p_gctx = gctx.p;\n\t*p_new_gctx = new_gctx;\n\treturn false;\n}\n\nprof_tctx_t *\nprof_lookup(tsd_t *tsd, prof_bt_t *bt) {\n\tunion {\n\t\tprof_tctx_t\t*p;\n\t\tvoid\t\t*v;\n\t} ret;\n\tprof_tdata_t *tdata;\n\tbool not_found;\n\n\tcassert(config_prof);\n\n\ttdata = prof_tdata_get(tsd, false);\n\tif (tdata == NULL) {\n\t\treturn NULL;\n\t}\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), tdata->lock);\n\tnot_found = ckh_search(&tdata->bt2tctx, bt, NULL, &ret.v);\n\tif (!not_found) { /* Note double negative! */\n\t\tret.p->prepared = true;\n\t}\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), tdata->lock);\n\tif (not_found) {\n\t\tvoid *btkey;\n\t\tprof_gctx_t *gctx;\n\t\tbool new_gctx, error;\n\n\t\t/*\n\t\t * This thread's cache lacks bt.  Look for it in the global\n\t\t * cache.\n\t\t */\n\t\tif (prof_lookup_global(tsd, bt, tdata, &btkey, &gctx,\n\t\t    &new_gctx)) {\n\t\t\treturn NULL;\n\t\t}\n\n\t\t/* Link a prof_tctx_t into gctx for this thread. */\n\t\tret.v = iallocztm(tsd_tsdn(tsd), sizeof(prof_tctx_t),\n\t\t    sz_size2index(sizeof(prof_tctx_t)), false, NULL, true,\n\t\t    arena_ichoose(tsd, NULL), true);\n\t\tif (ret.p == NULL) {\n\t\t\tif (new_gctx) {\n\t\t\t\tprof_gctx_try_destroy(tsd, tdata, gctx, tdata);\n\t\t\t}\n\t\t\treturn NULL;\n\t\t}\n\t\tret.p->tdata = tdata;\n\t\tret.p->thr_uid = tdata->thr_uid;\n\t\tret.p->thr_discrim = tdata->thr_discrim;\n\t\tmemset(&ret.p->cnts, 0, sizeof(prof_cnt_t));\n\t\tret.p->gctx = gctx;\n\t\tret.p->tctx_uid = tdata->tctx_uid_next++;\n\t\tret.p->prepared = true;\n\t\tret.p->state = prof_tctx_state_initializing;\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), tdata->lock);\n\t\terror = ckh_insert(tsd, &tdata->bt2tctx, btkey, ret.v);\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), tdata->lock);\n\t\tif (error) {\n\t\t\tif (new_gctx) {\n\t\t\t\tprof_gctx_try_destroy(tsd, tdata, gctx, tdata);\n\t\t\t}\n\t\t\tidalloctm(tsd_tsdn(tsd), ret.v, NULL, NULL, true, true);\n\t\t\treturn NULL;\n\t\t}\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), gctx->lock);\n\t\tret.p->state = prof_tctx_state_nominal;\n\t\ttctx_tree_insert(&gctx->tctxs, ret.p);\n\t\tgctx->nlimbo--;\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock);\n\t}\n\n\treturn ret.p;\n}\n\n/*\n * The bodies of this function and prof_leakcheck() are compiled out unless heap\n * profiling is enabled, so that it is possible to compile jemalloc with\n * floating point support completely disabled.  Avoiding floating point code is\n * important on memory-constrained systems, but it also enables a workaround for\n * versions of glibc that don't properly save/restore floating point registers\n * during dynamic lazy symbol loading (which internally calls into whatever\n * malloc implementation happens to be integrated into the application).  Note\n * that some compilers (e.g.  gcc 4.8) may use floating point registers for fast\n * memory moves, so jemalloc must be compiled with such optimizations disabled\n * (e.g.\n * -mno-sse) in order for the workaround to be complete.\n */\nvoid\nprof_sample_threshold_update(prof_tdata_t *tdata) {\n#ifdef JEMALLOC_PROF\n\tuint64_t r;\n\tdouble u;\n\n\tif (!config_prof) {\n\t\treturn;\n\t}\n\n\tif (lg_prof_sample == 0) {\n\t\ttdata->bytes_until_sample = 0;\n\t\treturn;\n\t}\n\n\t/*\n\t * Compute sample interval as a geometrically distributed random\n\t * variable with mean (2^lg_prof_sample).\n\t *\n\t *                             __        __\n\t *                             |  log(u)  |                     1\n\t * tdata->bytes_until_sample = | -------- |, where p = ---------------\n\t *                             | log(1-p) |             lg_prof_sample\n\t *                                                     2\n\t *\n\t * For more information on the math, see:\n\t *\n\t *   Non-Uniform Random Variate Generation\n\t *   Luc Devroye\n\t *   Springer-Verlag, New York, 1986\n\t *   pp 500\n\t *   (http://luc.devroye.org/rnbookindex.html)\n\t */\n\tr = prng_lg_range_u64(&tdata->prng_state, 53);\n\tu = (double)r * (1.0/9007199254740992.0L);\n\ttdata->bytes_until_sample = (uint64_t)(log(u) /\n\t    log(1.0 - (1.0 / (double)((uint64_t)1U << lg_prof_sample))))\n\t    + (uint64_t)1U;\n#endif\n}\n\n#ifdef JEMALLOC_JET\nstatic prof_tdata_t *\nprof_tdata_count_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata,\n    void *arg) {\n\tsize_t *tdata_count = (size_t *)arg;\n\n\t(*tdata_count)++;\n\n\treturn NULL;\n}\n\nsize_t\nprof_tdata_count(void) {\n\tsize_t tdata_count = 0;\n\ttsdn_t *tsdn;\n\n\ttsdn = tsdn_fetch();\n\tmalloc_mutex_lock(tsdn, &tdatas_mtx);\n\ttdata_tree_iter(&tdatas, NULL, prof_tdata_count_iter,\n\t    (void *)&tdata_count);\n\tmalloc_mutex_unlock(tsdn, &tdatas_mtx);\n\n\treturn tdata_count;\n}\n\nsize_t\nprof_bt_count(void) {\n\tsize_t bt_count;\n\ttsd_t *tsd;\n\tprof_tdata_t *tdata;\n\n\ttsd = tsd_fetch();\n\ttdata = prof_tdata_get(tsd, false);\n\tif (tdata == NULL) {\n\t\treturn 0;\n\t}\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &bt2gctx_mtx);\n\tbt_count = ckh_count(&bt2gctx);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &bt2gctx_mtx);\n\n\treturn bt_count;\n}\n#endif\n\nstatic int\nprof_dump_open_impl(bool propagate_err, const char *filename) {\n\tint fd;\n\n\tfd = creat(filename, 0644);\n\tif (fd == -1 && !propagate_err) {\n\t\tmalloc_printf(\"<jemalloc>: creat(\\\"%s\\\"), 0644) failed\\n\",\n\t\t    filename);\n\t\tif (opt_abort) {\n\t\t\tabort();\n\t\t}\n\t}\n\n\treturn fd;\n}\nprof_dump_open_t *JET_MUTABLE prof_dump_open = prof_dump_open_impl;\n\nstatic bool\nprof_dump_flush(bool propagate_err) {\n\tbool ret = false;\n\tssize_t err;\n\n\tcassert(config_prof);\n\n\terr = write(prof_dump_fd, prof_dump_buf, prof_dump_buf_end);\n\tif (err == -1) {\n\t\tif (!propagate_err) {\n\t\t\tmalloc_write(\"<jemalloc>: write() failed during heap \"\n\t\t\t    \"profile flush\\n\");\n\t\t\tif (opt_abort) {\n\t\t\t\tabort();\n\t\t\t}\n\t\t}\n\t\tret = true;\n\t}\n\tprof_dump_buf_end = 0;\n\n\treturn ret;\n}\n\nstatic bool\nprof_dump_close(bool propagate_err) {\n\tbool ret;\n\n\tassert(prof_dump_fd != -1);\n\tret = prof_dump_flush(propagate_err);\n\tclose(prof_dump_fd);\n\tprof_dump_fd = -1;\n\n\treturn ret;\n}\n\nstatic bool\nprof_dump_write(bool propagate_err, const char *s) {\n\tsize_t i, slen, n;\n\n\tcassert(config_prof);\n\n\ti = 0;\n\tslen = strlen(s);\n\twhile (i < slen) {\n\t\t/* Flush the buffer if it is full. */\n\t\tif (prof_dump_buf_end == PROF_DUMP_BUFSIZE) {\n\t\t\tif (prof_dump_flush(propagate_err) && propagate_err) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\n\t\tif (prof_dump_buf_end + slen <= PROF_DUMP_BUFSIZE) {\n\t\t\t/* Finish writing. */\n\t\t\tn = slen - i;\n\t\t} else {\n\t\t\t/* Write as much of s as will fit. */\n\t\t\tn = PROF_DUMP_BUFSIZE - prof_dump_buf_end;\n\t\t}\n\t\tmemcpy(&prof_dump_buf[prof_dump_buf_end], &s[i], n);\n\t\tprof_dump_buf_end += n;\n\t\ti += n;\n\t}\n\n\treturn false;\n}\n\nJEMALLOC_FORMAT_PRINTF(2, 3)\nstatic bool\nprof_dump_printf(bool propagate_err, const char *format, ...) {\n\tbool ret;\n\tva_list ap;\n\tchar buf[PROF_PRINTF_BUFSIZE];\n\n\tva_start(ap, format);\n\tmalloc_vsnprintf(buf, sizeof(buf), format, ap);\n\tva_end(ap);\n\tret = prof_dump_write(propagate_err, buf);\n\n\treturn ret;\n}\n\nstatic void\nprof_tctx_merge_tdata(tsdn_t *tsdn, prof_tctx_t *tctx, prof_tdata_t *tdata) {\n\tmalloc_mutex_assert_owner(tsdn, tctx->tdata->lock);\n\n\tmalloc_mutex_lock(tsdn, tctx->gctx->lock);\n\n\tswitch (tctx->state) {\n\tcase prof_tctx_state_initializing:\n\t\tmalloc_mutex_unlock(tsdn, tctx->gctx->lock);\n\t\treturn;\n\tcase prof_tctx_state_nominal:\n\t\ttctx->state = prof_tctx_state_dumping;\n\t\tmalloc_mutex_unlock(tsdn, tctx->gctx->lock);\n\n\t\tmemcpy(&tctx->dump_cnts, &tctx->cnts, sizeof(prof_cnt_t));\n\n\t\ttdata->cnt_summed.curobjs += tctx->dump_cnts.curobjs;\n\t\ttdata->cnt_summed.curbytes += tctx->dump_cnts.curbytes;\n\t\tif (opt_prof_accum) {\n\t\t\ttdata->cnt_summed.accumobjs +=\n\t\t\t    tctx->dump_cnts.accumobjs;\n\t\t\ttdata->cnt_summed.accumbytes +=\n\t\t\t    tctx->dump_cnts.accumbytes;\n\t\t}\n\t\tbreak;\n\tcase prof_tctx_state_dumping:\n\tcase prof_tctx_state_purgatory:\n\t\tnot_reached();\n\t}\n}\n\nstatic void\nprof_tctx_merge_gctx(tsdn_t *tsdn, prof_tctx_t *tctx, prof_gctx_t *gctx) {\n\tmalloc_mutex_assert_owner(tsdn, gctx->lock);\n\n\tgctx->cnt_summed.curobjs += tctx->dump_cnts.curobjs;\n\tgctx->cnt_summed.curbytes += tctx->dump_cnts.curbytes;\n\tif (opt_prof_accum) {\n\t\tgctx->cnt_summed.accumobjs += tctx->dump_cnts.accumobjs;\n\t\tgctx->cnt_summed.accumbytes += tctx->dump_cnts.accumbytes;\n\t}\n}\n\nstatic prof_tctx_t *\nprof_tctx_merge_iter(prof_tctx_tree_t *tctxs, prof_tctx_t *tctx, void *arg) {\n\ttsdn_t *tsdn = (tsdn_t *)arg;\n\n\tmalloc_mutex_assert_owner(tsdn, tctx->gctx->lock);\n\n\tswitch (tctx->state) {\n\tcase prof_tctx_state_nominal:\n\t\t/* New since dumping started; ignore. */\n\t\tbreak;\n\tcase prof_tctx_state_dumping:\n\tcase prof_tctx_state_purgatory:\n\t\tprof_tctx_merge_gctx(tsdn, tctx, tctx->gctx);\n\t\tbreak;\n\tdefault:\n\t\tnot_reached();\n\t}\n\n\treturn NULL;\n}\n\nstruct prof_tctx_dump_iter_arg_s {\n\ttsdn_t\t*tsdn;\n\tbool\tpropagate_err;\n};\n\nstatic prof_tctx_t *\nprof_tctx_dump_iter(prof_tctx_tree_t *tctxs, prof_tctx_t *tctx, void *opaque) {\n\tstruct prof_tctx_dump_iter_arg_s *arg =\n\t    (struct prof_tctx_dump_iter_arg_s *)opaque;\n\n\tmalloc_mutex_assert_owner(arg->tsdn, tctx->gctx->lock);\n\n\tswitch (tctx->state) {\n\tcase prof_tctx_state_initializing:\n\tcase prof_tctx_state_nominal:\n\t\t/* Not captured by this dump. */\n\t\tbreak;\n\tcase prof_tctx_state_dumping:\n\tcase prof_tctx_state_purgatory:\n\t\tif (prof_dump_printf(arg->propagate_err,\n\t\t    \"  t%\"FMTu64\": %\"FMTu64\": %\"FMTu64\" [%\"FMTu64\": \"\n\t\t    \"%\"FMTu64\"]\\n\", tctx->thr_uid, tctx->dump_cnts.curobjs,\n\t\t    tctx->dump_cnts.curbytes, tctx->dump_cnts.accumobjs,\n\t\t    tctx->dump_cnts.accumbytes)) {\n\t\t\treturn tctx;\n\t\t}\n\t\tbreak;\n\tdefault:\n\t\tnot_reached();\n\t}\n\treturn NULL;\n}\n\nstatic prof_tctx_t *\nprof_tctx_finish_iter(prof_tctx_tree_t *tctxs, prof_tctx_t *tctx, void *arg) {\n\ttsdn_t *tsdn = (tsdn_t *)arg;\n\tprof_tctx_t *ret;\n\n\tmalloc_mutex_assert_owner(tsdn, tctx->gctx->lock);\n\n\tswitch (tctx->state) {\n\tcase prof_tctx_state_nominal:\n\t\t/* New since dumping started; ignore. */\n\t\tbreak;\n\tcase prof_tctx_state_dumping:\n\t\ttctx->state = prof_tctx_state_nominal;\n\t\tbreak;\n\tcase prof_tctx_state_purgatory:\n\t\tret = tctx;\n\t\tgoto label_return;\n\tdefault:\n\t\tnot_reached();\n\t}\n\n\tret = NULL;\nlabel_return:\n\treturn ret;\n}\n\nstatic void\nprof_dump_gctx_prep(tsdn_t *tsdn, prof_gctx_t *gctx, prof_gctx_tree_t *gctxs) {\n\tcassert(config_prof);\n\n\tmalloc_mutex_lock(tsdn, gctx->lock);\n\n\t/*\n\t * Increment nlimbo so that gctx won't go away before dump.\n\t * Additionally, link gctx into the dump list so that it is included in\n\t * prof_dump()'s second pass.\n\t */\n\tgctx->nlimbo++;\n\tgctx_tree_insert(gctxs, gctx);\n\n\tmemset(&gctx->cnt_summed, 0, sizeof(prof_cnt_t));\n\n\tmalloc_mutex_unlock(tsdn, gctx->lock);\n}\n\nstruct prof_gctx_merge_iter_arg_s {\n\ttsdn_t\t*tsdn;\n\tsize_t\tleak_ngctx;\n};\n\nstatic prof_gctx_t *\nprof_gctx_merge_iter(prof_gctx_tree_t *gctxs, prof_gctx_t *gctx, void *opaque) {\n\tstruct prof_gctx_merge_iter_arg_s *arg =\n\t    (struct prof_gctx_merge_iter_arg_s *)opaque;\n\n\tmalloc_mutex_lock(arg->tsdn, gctx->lock);\n\ttctx_tree_iter(&gctx->tctxs, NULL, prof_tctx_merge_iter,\n\t    (void *)arg->tsdn);\n\tif (gctx->cnt_summed.curobjs != 0) {\n\t\targ->leak_ngctx++;\n\t}\n\tmalloc_mutex_unlock(arg->tsdn, gctx->lock);\n\n\treturn NULL;\n}\n\nstatic void\nprof_gctx_finish(tsd_t *tsd, prof_gctx_tree_t *gctxs) {\n\tprof_tdata_t *tdata = prof_tdata_get(tsd, false);\n\tprof_gctx_t *gctx;\n\n\t/*\n\t * Standard tree iteration won't work here, because as soon as we\n\t * decrement gctx->nlimbo and unlock gctx, another thread can\n\t * concurrently destroy it, which will corrupt the tree.  Therefore,\n\t * tear down the tree one node at a time during iteration.\n\t */\n\twhile ((gctx = gctx_tree_first(gctxs)) != NULL) {\n\t\tgctx_tree_remove(gctxs, gctx);\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), gctx->lock);\n\t\t{\n\t\t\tprof_tctx_t *next;\n\n\t\t\tnext = NULL;\n\t\t\tdo {\n\t\t\t\tprof_tctx_t *to_destroy =\n\t\t\t\t    tctx_tree_iter(&gctx->tctxs, next,\n\t\t\t\t    prof_tctx_finish_iter,\n\t\t\t\t    (void *)tsd_tsdn(tsd));\n\t\t\t\tif (to_destroy != NULL) {\n\t\t\t\t\tnext = tctx_tree_next(&gctx->tctxs,\n\t\t\t\t\t    to_destroy);\n\t\t\t\t\ttctx_tree_remove(&gctx->tctxs,\n\t\t\t\t\t    to_destroy);\n\t\t\t\t\tidalloctm(tsd_tsdn(tsd), to_destroy,\n\t\t\t\t\t    NULL, NULL, true, true);\n\t\t\t\t} else {\n\t\t\t\t\tnext = NULL;\n\t\t\t\t}\n\t\t\t} while (next != NULL);\n\t\t}\n\t\tgctx->nlimbo--;\n\t\tif (prof_gctx_should_destroy(gctx)) {\n\t\t\tgctx->nlimbo++;\n\t\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock);\n\t\t\tprof_gctx_try_destroy(tsd, tdata, gctx, tdata);\n\t\t} else {\n\t\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), gctx->lock);\n\t\t}\n\t}\n}\n\nstruct prof_tdata_merge_iter_arg_s {\n\ttsdn_t\t\t*tsdn;\n\tprof_cnt_t\tcnt_all;\n};\n\nstatic prof_tdata_t *\nprof_tdata_merge_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata,\n    void *opaque) {\n\tstruct prof_tdata_merge_iter_arg_s *arg =\n\t    (struct prof_tdata_merge_iter_arg_s *)opaque;\n\n\tmalloc_mutex_lock(arg->tsdn, tdata->lock);\n\tif (!tdata->expired) {\n\t\tsize_t tabind;\n\t\tunion {\n\t\t\tprof_tctx_t\t*p;\n\t\t\tvoid\t\t*v;\n\t\t} tctx;\n\n\t\ttdata->dumping = true;\n\t\tmemset(&tdata->cnt_summed, 0, sizeof(prof_cnt_t));\n\t\tfor (tabind = 0; !ckh_iter(&tdata->bt2tctx, &tabind, NULL,\n\t\t    &tctx.v);) {\n\t\t\tprof_tctx_merge_tdata(arg->tsdn, tctx.p, tdata);\n\t\t}\n\n\t\targ->cnt_all.curobjs += tdata->cnt_summed.curobjs;\n\t\targ->cnt_all.curbytes += tdata->cnt_summed.curbytes;\n\t\tif (opt_prof_accum) {\n\t\t\targ->cnt_all.accumobjs += tdata->cnt_summed.accumobjs;\n\t\t\targ->cnt_all.accumbytes += tdata->cnt_summed.accumbytes;\n\t\t}\n\t} else {\n\t\ttdata->dumping = false;\n\t}\n\tmalloc_mutex_unlock(arg->tsdn, tdata->lock);\n\n\treturn NULL;\n}\n\nstatic prof_tdata_t *\nprof_tdata_dump_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata,\n    void *arg) {\n\tbool propagate_err = *(bool *)arg;\n\n\tif (!tdata->dumping) {\n\t\treturn NULL;\n\t}\n\n\tif (prof_dump_printf(propagate_err,\n\t    \"  t%\"FMTu64\": %\"FMTu64\": %\"FMTu64\" [%\"FMTu64\": %\"FMTu64\"]%s%s\\n\",\n\t    tdata->thr_uid, tdata->cnt_summed.curobjs,\n\t    tdata->cnt_summed.curbytes, tdata->cnt_summed.accumobjs,\n\t    tdata->cnt_summed.accumbytes,\n\t    (tdata->thread_name != NULL) ? \" \" : \"\",\n\t    (tdata->thread_name != NULL) ? tdata->thread_name : \"\")) {\n\t\treturn tdata;\n\t}\n\treturn NULL;\n}\n\nstatic bool\nprof_dump_header_impl(tsdn_t *tsdn, bool propagate_err,\n    const prof_cnt_t *cnt_all) {\n\tbool ret;\n\n\tif (prof_dump_printf(propagate_err,\n\t    \"heap_v2/%\"FMTu64\"\\n\"\n\t    \"  t*: %\"FMTu64\": %\"FMTu64\" [%\"FMTu64\": %\"FMTu64\"]\\n\",\n\t    ((uint64_t)1U << lg_prof_sample), cnt_all->curobjs,\n\t    cnt_all->curbytes, cnt_all->accumobjs, cnt_all->accumbytes)) {\n\t\treturn true;\n\t}\n\n\tmalloc_mutex_lock(tsdn, &tdatas_mtx);\n\tret = (tdata_tree_iter(&tdatas, NULL, prof_tdata_dump_iter,\n\t    (void *)&propagate_err) != NULL);\n\tmalloc_mutex_unlock(tsdn, &tdatas_mtx);\n\treturn ret;\n}\nprof_dump_header_t *JET_MUTABLE prof_dump_header = prof_dump_header_impl;\n\nstatic bool\nprof_dump_gctx(tsdn_t *tsdn, bool propagate_err, prof_gctx_t *gctx,\n    const prof_bt_t *bt, prof_gctx_tree_t *gctxs) {\n\tbool ret;\n\tunsigned i;\n\tstruct prof_tctx_dump_iter_arg_s prof_tctx_dump_iter_arg;\n\n\tcassert(config_prof);\n\tmalloc_mutex_assert_owner(tsdn, gctx->lock);\n\n\t/* Avoid dumping such gctx's that have no useful data. */\n\tif ((!opt_prof_accum && gctx->cnt_summed.curobjs == 0) ||\n\t    (opt_prof_accum && gctx->cnt_summed.accumobjs == 0)) {\n\t\tassert(gctx->cnt_summed.curobjs == 0);\n\t\tassert(gctx->cnt_summed.curbytes == 0);\n\t\tassert(gctx->cnt_summed.accumobjs == 0);\n\t\tassert(gctx->cnt_summed.accumbytes == 0);\n\t\tret = false;\n\t\tgoto label_return;\n\t}\n\n\tif (prof_dump_printf(propagate_err, \"@\")) {\n\t\tret = true;\n\t\tgoto label_return;\n\t}\n\tfor (i = 0; i < bt->len; i++) {\n\t\tif (prof_dump_printf(propagate_err, \" %#\"FMTxPTR,\n\t\t    (uintptr_t)bt->vec[i])) {\n\t\t\tret = true;\n\t\t\tgoto label_return;\n\t\t}\n\t}\n\n\tif (prof_dump_printf(propagate_err,\n\t    \"\\n\"\n\t    \"  t*: %\"FMTu64\": %\"FMTu64\" [%\"FMTu64\": %\"FMTu64\"]\\n\",\n\t    gctx->cnt_summed.curobjs, gctx->cnt_summed.curbytes,\n\t    gctx->cnt_summed.accumobjs, gctx->cnt_summed.accumbytes)) {\n\t\tret = true;\n\t\tgoto label_return;\n\t}\n\n\tprof_tctx_dump_iter_arg.tsdn = tsdn;\n\tprof_tctx_dump_iter_arg.propagate_err = propagate_err;\n\tif (tctx_tree_iter(&gctx->tctxs, NULL, prof_tctx_dump_iter,\n\t    (void *)&prof_tctx_dump_iter_arg) != NULL) {\n\t\tret = true;\n\t\tgoto label_return;\n\t}\n\n\tret = false;\nlabel_return:\n\treturn ret;\n}\n\n#ifndef _WIN32\nJEMALLOC_FORMAT_PRINTF(1, 2)\nstatic int\nprof_open_maps(const char *format, ...) {\n\tint mfd;\n\tva_list ap;\n\tchar filename[PATH_MAX + 1];\n\n\tva_start(ap, format);\n\tmalloc_vsnprintf(filename, sizeof(filename), format, ap);\n\tva_end(ap);\n\tmfd = open(filename, O_RDONLY | O_CLOEXEC);\n\n\treturn mfd;\n}\n#endif\n\nstatic int\nprof_getpid(void) {\n#ifdef _WIN32\n\treturn GetCurrentProcessId();\n#else\n\treturn getpid();\n#endif\n}\n\nstatic bool\nprof_dump_maps(bool propagate_err) {\n\tbool ret;\n\tint mfd;\n\n\tcassert(config_prof);\n#ifdef __FreeBSD__\n\tmfd = prof_open_maps(\"/proc/curproc/map\");\n#elif defined(_WIN32)\n\tmfd = -1; // Not implemented\n#else\n\t{\n\t\tint pid = prof_getpid();\n\n\t\tmfd = prof_open_maps(\"/proc/%d/task/%d/maps\", pid, pid);\n\t\tif (mfd == -1) {\n\t\t\tmfd = prof_open_maps(\"/proc/%d/maps\", pid);\n\t\t}\n\t}\n#endif\n\tif (mfd != -1) {\n\t\tssize_t nread;\n\n\t\tif (prof_dump_write(propagate_err, \"\\nMAPPED_LIBRARIES:\\n\") &&\n\t\t    propagate_err) {\n\t\t\tret = true;\n\t\t\tgoto label_return;\n\t\t}\n\t\tnread = 0;\n\t\tdo {\n\t\t\tprof_dump_buf_end += nread;\n\t\t\tif (prof_dump_buf_end == PROF_DUMP_BUFSIZE) {\n\t\t\t\t/* Make space in prof_dump_buf before read(). */\n\t\t\t\tif (prof_dump_flush(propagate_err) &&\n\t\t\t\t    propagate_err) {\n\t\t\t\t\tret = true;\n\t\t\t\t\tgoto label_return;\n\t\t\t\t}\n\t\t\t}\n\t\t\tnread = read(mfd, &prof_dump_buf[prof_dump_buf_end],\n\t\t\t    PROF_DUMP_BUFSIZE - prof_dump_buf_end);\n\t\t} while (nread > 0);\n\t} else {\n\t\tret = true;\n\t\tgoto label_return;\n\t}\n\n\tret = false;\nlabel_return:\n\tif (mfd != -1) {\n\t\tclose(mfd);\n\t}\n\treturn ret;\n}\n\n/*\n * See prof_sample_threshold_update() comment for why the body of this function\n * is conditionally compiled.\n */\nstatic void\nprof_leakcheck(const prof_cnt_t *cnt_all, size_t leak_ngctx,\n    const char *filename) {\n#ifdef JEMALLOC_PROF\n\t/*\n\t * Scaling is equivalent AdjustSamples() in jeprof, but the result may\n\t * differ slightly from what jeprof reports, because here we scale the\n\t * summary values, whereas jeprof scales each context individually and\n\t * reports the sums of the scaled values.\n\t */\n\tif (cnt_all->curbytes != 0) {\n\t\tdouble sample_period = (double)((uint64_t)1 << lg_prof_sample);\n\t\tdouble ratio = (((double)cnt_all->curbytes) /\n\t\t    (double)cnt_all->curobjs) / sample_period;\n\t\tdouble scale_factor = 1.0 / (1.0 - exp(-ratio));\n\t\tuint64_t curbytes = (uint64_t)round(((double)cnt_all->curbytes)\n\t\t    * scale_factor);\n\t\tuint64_t curobjs = (uint64_t)round(((double)cnt_all->curobjs) *\n\t\t    scale_factor);\n\n\t\tmalloc_printf(\"<jemalloc>: Leak approximation summary: ~%\"FMTu64\n\t\t    \" byte%s, ~%\"FMTu64\" object%s, >= %zu context%s\\n\",\n\t\t    curbytes, (curbytes != 1) ? \"s\" : \"\", curobjs, (curobjs !=\n\t\t    1) ? \"s\" : \"\", leak_ngctx, (leak_ngctx != 1) ? \"s\" : \"\");\n\t\tmalloc_printf(\n\t\t    \"<jemalloc>: Run jeprof on \\\"%s\\\" for leak detail\\n\",\n\t\t    filename);\n\t}\n#endif\n}\n\nstruct prof_gctx_dump_iter_arg_s {\n\ttsdn_t\t*tsdn;\n\tbool\tpropagate_err;\n};\n\nstatic prof_gctx_t *\nprof_gctx_dump_iter(prof_gctx_tree_t *gctxs, prof_gctx_t *gctx, void *opaque) {\n\tprof_gctx_t *ret;\n\tstruct prof_gctx_dump_iter_arg_s *arg =\n\t    (struct prof_gctx_dump_iter_arg_s *)opaque;\n\n\tmalloc_mutex_lock(arg->tsdn, gctx->lock);\n\n\tif (prof_dump_gctx(arg->tsdn, arg->propagate_err, gctx, &gctx->bt,\n\t    gctxs)) {\n\t\tret = gctx;\n\t\tgoto label_return;\n\t}\n\n\tret = NULL;\nlabel_return:\n\tmalloc_mutex_unlock(arg->tsdn, gctx->lock);\n\treturn ret;\n}\n\nstatic void\nprof_dump_prep(tsd_t *tsd, prof_tdata_t *tdata,\n    struct prof_tdata_merge_iter_arg_s *prof_tdata_merge_iter_arg,\n    struct prof_gctx_merge_iter_arg_s *prof_gctx_merge_iter_arg,\n    prof_gctx_tree_t *gctxs) {\n\tsize_t tabind;\n\tunion {\n\t\tprof_gctx_t\t*p;\n\t\tvoid\t\t*v;\n\t} gctx;\n\n\tprof_enter(tsd, tdata);\n\n\t/*\n\t * Put gctx's in limbo and clear their counters in preparation for\n\t * summing.\n\t */\n\tgctx_tree_new(gctxs);\n\tfor (tabind = 0; !ckh_iter(&bt2gctx, &tabind, NULL, &gctx.v);) {\n\t\tprof_dump_gctx_prep(tsd_tsdn(tsd), gctx.p, gctxs);\n\t}\n\n\t/*\n\t * Iterate over tdatas, and for the non-expired ones snapshot their tctx\n\t * stats and merge them into the associated gctx's.\n\t */\n\tprof_tdata_merge_iter_arg->tsdn = tsd_tsdn(tsd);\n\tmemset(&prof_tdata_merge_iter_arg->cnt_all, 0, sizeof(prof_cnt_t));\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &tdatas_mtx);\n\ttdata_tree_iter(&tdatas, NULL, prof_tdata_merge_iter,\n\t    (void *)prof_tdata_merge_iter_arg);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &tdatas_mtx);\n\n\t/* Merge tctx stats into gctx's. */\n\tprof_gctx_merge_iter_arg->tsdn = tsd_tsdn(tsd);\n\tprof_gctx_merge_iter_arg->leak_ngctx = 0;\n\tgctx_tree_iter(gctxs, NULL, prof_gctx_merge_iter,\n\t    (void *)prof_gctx_merge_iter_arg);\n\n\tprof_leave(tsd, tdata);\n}\n\nstatic bool\nprof_dump_file(tsd_t *tsd, bool propagate_err, const char *filename,\n    bool leakcheck, prof_tdata_t *tdata,\n    struct prof_tdata_merge_iter_arg_s *prof_tdata_merge_iter_arg,\n    struct prof_gctx_merge_iter_arg_s *prof_gctx_merge_iter_arg,\n    struct prof_gctx_dump_iter_arg_s *prof_gctx_dump_iter_arg,\n    prof_gctx_tree_t *gctxs) {\n\t/* Create dump file. */\n\tif ((prof_dump_fd = prof_dump_open(propagate_err, filename)) == -1) {\n\t\treturn true;\n\t}\n\n\t/* Dump profile header. */\n\tif (prof_dump_header(tsd_tsdn(tsd), propagate_err,\n\t    &prof_tdata_merge_iter_arg->cnt_all)) {\n\t\tgoto label_write_error;\n\t}\n\n\t/* Dump per gctx profile stats. */\n\tprof_gctx_dump_iter_arg->tsdn = tsd_tsdn(tsd);\n\tprof_gctx_dump_iter_arg->propagate_err = propagate_err;\n\tif (gctx_tree_iter(gctxs, NULL, prof_gctx_dump_iter,\n\t    (void *)prof_gctx_dump_iter_arg) != NULL) {\n\t\tgoto label_write_error;\n\t}\n\n\t/* Dump /proc/<pid>/maps if possible. */\n\tif (prof_dump_maps(propagate_err)) {\n\t\tgoto label_write_error;\n\t}\n\n\tif (prof_dump_close(propagate_err)) {\n\t\treturn true;\n\t}\n\n\treturn false;\nlabel_write_error:\n\tprof_dump_close(propagate_err);\n\treturn true;\n}\n\nstatic bool\nprof_dump(tsd_t *tsd, bool propagate_err, const char *filename,\n    bool leakcheck) {\n\tcassert(config_prof);\n\tassert(tsd_reentrancy_level_get(tsd) == 0);\n\n\tprof_tdata_t * tdata = prof_tdata_get(tsd, true);\n\tif (tdata == NULL) {\n\t\treturn true;\n\t}\n\n\tpre_reentrancy(tsd);\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &prof_dump_mtx);\n\n\tprof_gctx_tree_t gctxs;\n\tstruct prof_tdata_merge_iter_arg_s prof_tdata_merge_iter_arg;\n\tstruct prof_gctx_merge_iter_arg_s prof_gctx_merge_iter_arg;\n\tstruct prof_gctx_dump_iter_arg_s prof_gctx_dump_iter_arg;\n\tprof_dump_prep(tsd, tdata, &prof_tdata_merge_iter_arg,\n\t    &prof_gctx_merge_iter_arg, &gctxs);\n\tbool err = prof_dump_file(tsd, propagate_err, filename, leakcheck, tdata,\n\t    &prof_tdata_merge_iter_arg, &prof_gctx_merge_iter_arg,\n\t    &prof_gctx_dump_iter_arg, &gctxs);\n\tprof_gctx_finish(tsd, &gctxs);\n\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &prof_dump_mtx);\n\tpost_reentrancy(tsd);\n\n\tif (err) {\n\t\treturn true;\n\t}\n\n\tif (leakcheck) {\n\t\tprof_leakcheck(&prof_tdata_merge_iter_arg.cnt_all,\n\t\t    prof_gctx_merge_iter_arg.leak_ngctx, filename);\n\t}\n\treturn false;\n}\n\n#ifdef JEMALLOC_JET\nvoid\nprof_cnt_all(uint64_t *curobjs, uint64_t *curbytes, uint64_t *accumobjs,\n    uint64_t *accumbytes) {\n\ttsd_t *tsd;\n\tprof_tdata_t *tdata;\n\tstruct prof_tdata_merge_iter_arg_s prof_tdata_merge_iter_arg;\n\tstruct prof_gctx_merge_iter_arg_s prof_gctx_merge_iter_arg;\n\tprof_gctx_tree_t gctxs;\n\n\ttsd = tsd_fetch();\n\ttdata = prof_tdata_get(tsd, false);\n\tif (tdata == NULL) {\n\t\tif (curobjs != NULL) {\n\t\t\t*curobjs = 0;\n\t\t}\n\t\tif (curbytes != NULL) {\n\t\t\t*curbytes = 0;\n\t\t}\n\t\tif (accumobjs != NULL) {\n\t\t\t*accumobjs = 0;\n\t\t}\n\t\tif (accumbytes != NULL) {\n\t\t\t*accumbytes = 0;\n\t\t}\n\t\treturn;\n\t}\n\n\tprof_dump_prep(tsd, tdata, &prof_tdata_merge_iter_arg,\n\t    &prof_gctx_merge_iter_arg, &gctxs);\n\tprof_gctx_finish(tsd, &gctxs);\n\n\tif (curobjs != NULL) {\n\t\t*curobjs = prof_tdata_merge_iter_arg.cnt_all.curobjs;\n\t}\n\tif (curbytes != NULL) {\n\t\t*curbytes = prof_tdata_merge_iter_arg.cnt_all.curbytes;\n\t}\n\tif (accumobjs != NULL) {\n\t\t*accumobjs = prof_tdata_merge_iter_arg.cnt_all.accumobjs;\n\t}\n\tif (accumbytes != NULL) {\n\t\t*accumbytes = prof_tdata_merge_iter_arg.cnt_all.accumbytes;\n\t}\n}\n#endif\n\n#define DUMP_FILENAME_BUFSIZE\t(PATH_MAX + 1)\n#define VSEQ_INVALID\t\tUINT64_C(0xffffffffffffffff)\nstatic void\nprof_dump_filename(char *filename, char v, uint64_t vseq) {\n\tcassert(config_prof);\n\n\tif (vseq != VSEQ_INVALID) {\n\t        /* \"<prefix>.<pid>.<seq>.v<vseq>.heap\" */\n\t\tmalloc_snprintf(filename, DUMP_FILENAME_BUFSIZE,\n\t\t    \"%s.%d.%\"FMTu64\".%c%\"FMTu64\".heap\",\n\t\t    opt_prof_prefix, prof_getpid(), prof_dump_seq, v, vseq);\n\t} else {\n\t        /* \"<prefix>.<pid>.<seq>.<v>.heap\" */\n\t\tmalloc_snprintf(filename, DUMP_FILENAME_BUFSIZE,\n\t\t    \"%s.%d.%\"FMTu64\".%c.heap\",\n\t\t    opt_prof_prefix, prof_getpid(), prof_dump_seq, v);\n\t}\n\tprof_dump_seq++;\n}\n\nstatic void\nprof_fdump(void) {\n\ttsd_t *tsd;\n\tchar filename[DUMP_FILENAME_BUFSIZE];\n\n\tcassert(config_prof);\n\tassert(opt_prof_final);\n\tassert(opt_prof_prefix[0] != '\\0');\n\n\tif (!prof_booted) {\n\t\treturn;\n\t}\n\ttsd = tsd_fetch();\n\tassert(tsd_reentrancy_level_get(tsd) == 0);\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &prof_dump_seq_mtx);\n\tprof_dump_filename(filename, 'f', VSEQ_INVALID);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &prof_dump_seq_mtx);\n\tprof_dump(tsd, false, filename, opt_prof_leak);\n}\n\nbool\nprof_accum_init(tsdn_t *tsdn, prof_accum_t *prof_accum) {\n\tcassert(config_prof);\n\n#ifndef JEMALLOC_ATOMIC_U64\n\tif (malloc_mutex_init(&prof_accum->mtx, \"prof_accum\",\n\t    WITNESS_RANK_PROF_ACCUM, malloc_mutex_rank_exclusive)) {\n\t\treturn true;\n\t}\n\tprof_accum->accumbytes = 0;\n#else\n\tatomic_store_u64(&prof_accum->accumbytes, 0, ATOMIC_RELAXED);\n#endif\n\treturn false;\n}\n\nvoid\nprof_idump(tsdn_t *tsdn) {\n\ttsd_t *tsd;\n\tprof_tdata_t *tdata;\n\n\tcassert(config_prof);\n\n\tif (!prof_booted || tsdn_null(tsdn)) {\n\t\treturn;\n\t}\n\ttsd = tsdn_tsd(tsdn);\n\tif (tsd_reentrancy_level_get(tsd) > 0) {\n\t\treturn;\n\t}\n\n\ttdata = prof_tdata_get(tsd, false);\n\tif (tdata == NULL) {\n\t\treturn;\n\t}\n\tif (tdata->enq) {\n\t\ttdata->enq_idump = true;\n\t\treturn;\n\t}\n\n\tif (opt_prof_prefix[0] != '\\0') {\n\t\tchar filename[PATH_MAX + 1];\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &prof_dump_seq_mtx);\n\t\tprof_dump_filename(filename, 'i', prof_dump_iseq);\n\t\tprof_dump_iseq++;\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &prof_dump_seq_mtx);\n\t\tprof_dump(tsd, false, filename, false);\n\t}\n}\n\nbool\nprof_mdump(tsd_t *tsd, const char *filename) {\n\tcassert(config_prof);\n\tassert(tsd_reentrancy_level_get(tsd) == 0);\n\n\tif (!opt_prof || !prof_booted) {\n\t\treturn true;\n\t}\n\tchar filename_buf[DUMP_FILENAME_BUFSIZE];\n\tif (filename == NULL) {\n\t\t/* No filename specified, so automatically generate one. */\n\t\tif (opt_prof_prefix[0] == '\\0') {\n\t\t\treturn true;\n\t\t}\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &prof_dump_seq_mtx);\n\t\tprof_dump_filename(filename_buf, 'm', prof_dump_mseq);\n\t\tprof_dump_mseq++;\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &prof_dump_seq_mtx);\n\t\tfilename = filename_buf;\n\t}\n\treturn prof_dump(tsd, true, filename, false);\n}\n\nvoid\nprof_gdump(tsdn_t *tsdn) {\n\ttsd_t *tsd;\n\tprof_tdata_t *tdata;\n\n\tcassert(config_prof);\n\n\tif (!prof_booted || tsdn_null(tsdn)) {\n\t\treturn;\n\t}\n\ttsd = tsdn_tsd(tsdn);\n\tif (tsd_reentrancy_level_get(tsd) > 0) {\n\t\treturn;\n\t}\n\n\ttdata = prof_tdata_get(tsd, false);\n\tif (tdata == NULL) {\n\t\treturn;\n\t}\n\tif (tdata->enq) {\n\t\ttdata->enq_gdump = true;\n\t\treturn;\n\t}\n\n\tif (opt_prof_prefix[0] != '\\0') {\n\t\tchar filename[DUMP_FILENAME_BUFSIZE];\n\t\tmalloc_mutex_lock(tsdn, &prof_dump_seq_mtx);\n\t\tprof_dump_filename(filename, 'u', prof_dump_useq);\n\t\tprof_dump_useq++;\n\t\tmalloc_mutex_unlock(tsdn, &prof_dump_seq_mtx);\n\t\tprof_dump(tsd, false, filename, false);\n\t}\n}\n\nstatic void\nprof_bt_hash(const void *key, size_t r_hash[2]) {\n\tprof_bt_t *bt = (prof_bt_t *)key;\n\n\tcassert(config_prof);\n\n\thash(bt->vec, bt->len * sizeof(void *), 0x94122f33U, r_hash);\n}\n\nstatic bool\nprof_bt_keycomp(const void *k1, const void *k2) {\n\tconst prof_bt_t *bt1 = (prof_bt_t *)k1;\n\tconst prof_bt_t *bt2 = (prof_bt_t *)k2;\n\n\tcassert(config_prof);\n\n\tif (bt1->len != bt2->len) {\n\t\treturn false;\n\t}\n\treturn (memcmp(bt1->vec, bt2->vec, bt1->len * sizeof(void *)) == 0);\n}\n\nstatic uint64_t\nprof_thr_uid_alloc(tsdn_t *tsdn) {\n\tuint64_t thr_uid;\n\n\tmalloc_mutex_lock(tsdn, &next_thr_uid_mtx);\n\tthr_uid = next_thr_uid;\n\tnext_thr_uid++;\n\tmalloc_mutex_unlock(tsdn, &next_thr_uid_mtx);\n\n\treturn thr_uid;\n}\n\nstatic prof_tdata_t *\nprof_tdata_init_impl(tsd_t *tsd, uint64_t thr_uid, uint64_t thr_discrim,\n    char *thread_name, bool active) {\n\tprof_tdata_t *tdata;\n\n\tcassert(config_prof);\n\n\t/* Initialize an empty cache for this thread. */\n\ttdata = (prof_tdata_t *)iallocztm(tsd_tsdn(tsd), sizeof(prof_tdata_t),\n\t    sz_size2index(sizeof(prof_tdata_t)), false, NULL, true,\n\t    arena_get(TSDN_NULL, 0, true), true);\n\tif (tdata == NULL) {\n\t\treturn NULL;\n\t}\n\n\ttdata->lock = prof_tdata_mutex_choose(thr_uid);\n\ttdata->thr_uid = thr_uid;\n\ttdata->thr_discrim = thr_discrim;\n\ttdata->thread_name = thread_name;\n\ttdata->attached = true;\n\ttdata->expired = false;\n\ttdata->tctx_uid_next = 0;\n\n\tif (ckh_new(tsd, &tdata->bt2tctx, PROF_CKH_MINITEMS, prof_bt_hash,\n\t    prof_bt_keycomp)) {\n\t\tidalloctm(tsd_tsdn(tsd), tdata, NULL, NULL, true, true);\n\t\treturn NULL;\n\t}\n\n\ttdata->prng_state = (uint64_t)(uintptr_t)tdata;\n\tprof_sample_threshold_update(tdata);\n\n\ttdata->enq = false;\n\ttdata->enq_idump = false;\n\ttdata->enq_gdump = false;\n\n\ttdata->dumping = false;\n\ttdata->active = active;\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &tdatas_mtx);\n\ttdata_tree_insert(&tdatas, tdata);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &tdatas_mtx);\n\n\treturn tdata;\n}\n\nprof_tdata_t *\nprof_tdata_init(tsd_t *tsd) {\n\treturn prof_tdata_init_impl(tsd, prof_thr_uid_alloc(tsd_tsdn(tsd)), 0,\n\t    NULL, prof_thread_active_init_get(tsd_tsdn(tsd)));\n}\n\nstatic bool\nprof_tdata_should_destroy_unlocked(prof_tdata_t *tdata, bool even_if_attached) {\n\tif (tdata->attached && !even_if_attached) {\n\t\treturn false;\n\t}\n\tif (ckh_count(&tdata->bt2tctx) != 0) {\n\t\treturn false;\n\t}\n\treturn true;\n}\n\nstatic bool\nprof_tdata_should_destroy(tsdn_t *tsdn, prof_tdata_t *tdata,\n    bool even_if_attached) {\n\tmalloc_mutex_assert_owner(tsdn, tdata->lock);\n\n\treturn prof_tdata_should_destroy_unlocked(tdata, even_if_attached);\n}\n\nstatic void\nprof_tdata_destroy_locked(tsd_t *tsd, prof_tdata_t *tdata,\n    bool even_if_attached) {\n\tmalloc_mutex_assert_owner(tsd_tsdn(tsd), &tdatas_mtx);\n\n\ttdata_tree_remove(&tdatas, tdata);\n\n\tassert(prof_tdata_should_destroy_unlocked(tdata, even_if_attached));\n\n\tif (tdata->thread_name != NULL) {\n\t\tidalloctm(tsd_tsdn(tsd), tdata->thread_name, NULL, NULL, true,\n\t\t    true);\n\t}\n\tckh_delete(tsd, &tdata->bt2tctx);\n\tidalloctm(tsd_tsdn(tsd), tdata, NULL, NULL, true, true);\n}\n\nstatic void\nprof_tdata_destroy(tsd_t *tsd, prof_tdata_t *tdata, bool even_if_attached) {\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &tdatas_mtx);\n\tprof_tdata_destroy_locked(tsd, tdata, even_if_attached);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &tdatas_mtx);\n}\n\nstatic void\nprof_tdata_detach(tsd_t *tsd, prof_tdata_t *tdata) {\n\tbool destroy_tdata;\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), tdata->lock);\n\tif (tdata->attached) {\n\t\tdestroy_tdata = prof_tdata_should_destroy(tsd_tsdn(tsd), tdata,\n\t\t    true);\n\t\t/*\n\t\t * Only detach if !destroy_tdata, because detaching would allow\n\t\t * another thread to win the race to destroy tdata.\n\t\t */\n\t\tif (!destroy_tdata) {\n\t\t\ttdata->attached = false;\n\t\t}\n\t\ttsd_prof_tdata_set(tsd, NULL);\n\t} else {\n\t\tdestroy_tdata = false;\n\t}\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), tdata->lock);\n\tif (destroy_tdata) {\n\t\tprof_tdata_destroy(tsd, tdata, true);\n\t}\n}\n\nprof_tdata_t *\nprof_tdata_reinit(tsd_t *tsd, prof_tdata_t *tdata) {\n\tuint64_t thr_uid = tdata->thr_uid;\n\tuint64_t thr_discrim = tdata->thr_discrim + 1;\n\tchar *thread_name = (tdata->thread_name != NULL) ?\n\t    prof_thread_name_alloc(tsd_tsdn(tsd), tdata->thread_name) : NULL;\n\tbool active = tdata->active;\n\n\tprof_tdata_detach(tsd, tdata);\n\treturn prof_tdata_init_impl(tsd, thr_uid, thr_discrim, thread_name,\n\t    active);\n}\n\nstatic bool\nprof_tdata_expire(tsdn_t *tsdn, prof_tdata_t *tdata) {\n\tbool destroy_tdata;\n\n\tmalloc_mutex_lock(tsdn, tdata->lock);\n\tif (!tdata->expired) {\n\t\ttdata->expired = true;\n\t\tdestroy_tdata = tdata->attached ? false :\n\t\t    prof_tdata_should_destroy(tsdn, tdata, false);\n\t} else {\n\t\tdestroy_tdata = false;\n\t}\n\tmalloc_mutex_unlock(tsdn, tdata->lock);\n\n\treturn destroy_tdata;\n}\n\nstatic prof_tdata_t *\nprof_tdata_reset_iter(prof_tdata_tree_t *tdatas, prof_tdata_t *tdata,\n    void *arg) {\n\ttsdn_t *tsdn = (tsdn_t *)arg;\n\n\treturn (prof_tdata_expire(tsdn, tdata) ? tdata : NULL);\n}\n\nvoid\nprof_reset(tsd_t *tsd, size_t lg_sample) {\n\tprof_tdata_t *next;\n\n\tassert(lg_sample < (sizeof(uint64_t) << 3));\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &prof_dump_mtx);\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &tdatas_mtx);\n\n\tlg_prof_sample = lg_sample;\n\n\tnext = NULL;\n\tdo {\n\t\tprof_tdata_t *to_destroy = tdata_tree_iter(&tdatas, next,\n\t\t    prof_tdata_reset_iter, (void *)tsd);\n\t\tif (to_destroy != NULL) {\n\t\t\tnext = tdata_tree_next(&tdatas, to_destroy);\n\t\t\tprof_tdata_destroy_locked(tsd, to_destroy, false);\n\t\t} else {\n\t\t\tnext = NULL;\n\t\t}\n\t} while (next != NULL);\n\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &tdatas_mtx);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &prof_dump_mtx);\n}\n\nvoid\nprof_tdata_cleanup(tsd_t *tsd) {\n\tprof_tdata_t *tdata;\n\n\tif (!config_prof) {\n\t\treturn;\n\t}\n\n\ttdata = tsd_prof_tdata_get(tsd);\n\tif (tdata != NULL) {\n\t\tprof_tdata_detach(tsd, tdata);\n\t}\n}\n\nbool\nprof_active_get(tsdn_t *tsdn) {\n\tbool prof_active_current;\n\n\tmalloc_mutex_lock(tsdn, &prof_active_mtx);\n\tprof_active_current = prof_active;\n\tmalloc_mutex_unlock(tsdn, &prof_active_mtx);\n\treturn prof_active_current;\n}\n\nbool\nprof_active_set(tsdn_t *tsdn, bool active) {\n\tbool prof_active_old;\n\n\tmalloc_mutex_lock(tsdn, &prof_active_mtx);\n\tprof_active_old = prof_active;\n\tprof_active = active;\n\tmalloc_mutex_unlock(tsdn, &prof_active_mtx);\n\treturn prof_active_old;\n}\n\nconst char *\nprof_thread_name_get(tsd_t *tsd) {\n\tprof_tdata_t *tdata;\n\n\ttdata = prof_tdata_get(tsd, true);\n\tif (tdata == NULL) {\n\t\treturn \"\";\n\t}\n\treturn (tdata->thread_name != NULL ? tdata->thread_name : \"\");\n}\n\nstatic char *\nprof_thread_name_alloc(tsdn_t *tsdn, const char *thread_name) {\n\tchar *ret;\n\tsize_t size;\n\n\tif (thread_name == NULL) {\n\t\treturn NULL;\n\t}\n\n\tsize = strlen(thread_name) + 1;\n\tif (size == 1) {\n\t\treturn \"\";\n\t}\n\n\tret = iallocztm(tsdn, size, sz_size2index(size), false, NULL, true,\n\t    arena_get(TSDN_NULL, 0, true), true);\n\tif (ret == NULL) {\n\t\treturn NULL;\n\t}\n\tmemcpy(ret, thread_name, size);\n\treturn ret;\n}\n\nint\nprof_thread_name_set(tsd_t *tsd, const char *thread_name) {\n\tprof_tdata_t *tdata;\n\tunsigned i;\n\tchar *s;\n\n\ttdata = prof_tdata_get(tsd, true);\n\tif (tdata == NULL) {\n\t\treturn EAGAIN;\n\t}\n\n\t/* Validate input. */\n\tif (thread_name == NULL) {\n\t\treturn EFAULT;\n\t}\n\tfor (i = 0; thread_name[i] != '\\0'; i++) {\n\t\tchar c = thread_name[i];\n\t\tif (!isgraph(c) && !isblank(c)) {\n\t\t\treturn EFAULT;\n\t\t}\n\t}\n\n\ts = prof_thread_name_alloc(tsd_tsdn(tsd), thread_name);\n\tif (s == NULL) {\n\t\treturn EAGAIN;\n\t}\n\n\tif (tdata->thread_name != NULL) {\n\t\tidalloctm(tsd_tsdn(tsd), tdata->thread_name, NULL, NULL, true,\n\t\t    true);\n\t\ttdata->thread_name = NULL;\n\t}\n\tif (strlen(s) > 0) {\n\t\ttdata->thread_name = s;\n\t}\n\treturn 0;\n}\n\nbool\nprof_thread_active_get(tsd_t *tsd) {\n\tprof_tdata_t *tdata;\n\n\ttdata = prof_tdata_get(tsd, true);\n\tif (tdata == NULL) {\n\t\treturn false;\n\t}\n\treturn tdata->active;\n}\n\nbool\nprof_thread_active_set(tsd_t *tsd, bool active) {\n\tprof_tdata_t *tdata;\n\n\ttdata = prof_tdata_get(tsd, true);\n\tif (tdata == NULL) {\n\t\treturn true;\n\t}\n\ttdata->active = active;\n\treturn false;\n}\n\nbool\nprof_thread_active_init_get(tsdn_t *tsdn) {\n\tbool active_init;\n\n\tmalloc_mutex_lock(tsdn, &prof_thread_active_init_mtx);\n\tactive_init = prof_thread_active_init;\n\tmalloc_mutex_unlock(tsdn, &prof_thread_active_init_mtx);\n\treturn active_init;\n}\n\nbool\nprof_thread_active_init_set(tsdn_t *tsdn, bool active_init) {\n\tbool active_init_old;\n\n\tmalloc_mutex_lock(tsdn, &prof_thread_active_init_mtx);\n\tactive_init_old = prof_thread_active_init;\n\tprof_thread_active_init = active_init;\n\tmalloc_mutex_unlock(tsdn, &prof_thread_active_init_mtx);\n\treturn active_init_old;\n}\n\nbool\nprof_gdump_get(tsdn_t *tsdn) {\n\tbool prof_gdump_current;\n\n\tmalloc_mutex_lock(tsdn, &prof_gdump_mtx);\n\tprof_gdump_current = prof_gdump_val;\n\tmalloc_mutex_unlock(tsdn, &prof_gdump_mtx);\n\treturn prof_gdump_current;\n}\n\nbool\nprof_gdump_set(tsdn_t *tsdn, bool gdump) {\n\tbool prof_gdump_old;\n\n\tmalloc_mutex_lock(tsdn, &prof_gdump_mtx);\n\tprof_gdump_old = prof_gdump_val;\n\tprof_gdump_val = gdump;\n\tmalloc_mutex_unlock(tsdn, &prof_gdump_mtx);\n\treturn prof_gdump_old;\n}\n\nvoid\nprof_boot0(void) {\n\tcassert(config_prof);\n\n\tmemcpy(opt_prof_prefix, PROF_PREFIX_DEFAULT,\n\t    sizeof(PROF_PREFIX_DEFAULT));\n}\n\nvoid\nprof_boot1(void) {\n\tcassert(config_prof);\n\n\t/*\n\t * opt_prof must be in its final state before any arenas are\n\t * initialized, so this function must be executed early.\n\t */\n\n\tif (opt_prof_leak && !opt_prof) {\n\t\t/*\n\t\t * Enable opt_prof, but in such a way that profiles are never\n\t\t * automatically dumped.\n\t\t */\n\t\topt_prof = true;\n\t\topt_prof_gdump = false;\n\t} else if (opt_prof) {\n\t\tif (opt_lg_prof_interval >= 0) {\n\t\t\tprof_interval = (((uint64_t)1U) <<\n\t\t\t    opt_lg_prof_interval);\n\t\t}\n\t}\n}\n\nbool\nprof_boot2(tsd_t *tsd) {\n\tcassert(config_prof);\n\n\tif (opt_prof) {\n\t\tunsigned i;\n\n\t\tlg_prof_sample = opt_lg_prof_sample;\n\n\t\tprof_active = opt_prof_active;\n\t\tif (malloc_mutex_init(&prof_active_mtx, \"prof_active\",\n\t\t    WITNESS_RANK_PROF_ACTIVE, malloc_mutex_rank_exclusive)) {\n\t\t\treturn true;\n\t\t}\n\n\t\tprof_gdump_val = opt_prof_gdump;\n\t\tif (malloc_mutex_init(&prof_gdump_mtx, \"prof_gdump\",\n\t\t    WITNESS_RANK_PROF_GDUMP, malloc_mutex_rank_exclusive)) {\n\t\t\treturn true;\n\t\t}\n\n\t\tprof_thread_active_init = opt_prof_thread_active_init;\n\t\tif (malloc_mutex_init(&prof_thread_active_init_mtx,\n\t\t    \"prof_thread_active_init\",\n\t\t    WITNESS_RANK_PROF_THREAD_ACTIVE_INIT,\n\t\t    malloc_mutex_rank_exclusive)) {\n\t\t\treturn true;\n\t\t}\n\n\t\tif (ckh_new(tsd, &bt2gctx, PROF_CKH_MINITEMS, prof_bt_hash,\n\t\t    prof_bt_keycomp)) {\n\t\t\treturn true;\n\t\t}\n\t\tif (malloc_mutex_init(&bt2gctx_mtx, \"prof_bt2gctx\",\n\t\t    WITNESS_RANK_PROF_BT2GCTX, malloc_mutex_rank_exclusive)) {\n\t\t\treturn true;\n\t\t}\n\n\t\ttdata_tree_new(&tdatas);\n\t\tif (malloc_mutex_init(&tdatas_mtx, \"prof_tdatas\",\n\t\t    WITNESS_RANK_PROF_TDATAS, malloc_mutex_rank_exclusive)) {\n\t\t\treturn true;\n\t\t}\n\n\t\tnext_thr_uid = 0;\n\t\tif (malloc_mutex_init(&next_thr_uid_mtx, \"prof_next_thr_uid\",\n\t\t    WITNESS_RANK_PROF_NEXT_THR_UID, malloc_mutex_rank_exclusive)) {\n\t\t\treturn true;\n\t\t}\n\n\t\tif (malloc_mutex_init(&prof_dump_seq_mtx, \"prof_dump_seq\",\n\t\t    WITNESS_RANK_PROF_DUMP_SEQ, malloc_mutex_rank_exclusive)) {\n\t\t\treturn true;\n\t\t}\n\t\tif (malloc_mutex_init(&prof_dump_mtx, \"prof_dump\",\n\t\t    WITNESS_RANK_PROF_DUMP, malloc_mutex_rank_exclusive)) {\n\t\t\treturn true;\n\t\t}\n\n\t\tif (opt_prof_final && opt_prof_prefix[0] != '\\0' &&\n\t\t    atexit(prof_fdump) != 0) {\n\t\t\tmalloc_write(\"<jemalloc>: Error in atexit()\\n\");\n\t\t\tif (opt_abort) {\n\t\t\t\tabort();\n\t\t\t}\n\t\t}\n\n\t\tgctx_locks = (malloc_mutex_t *)base_alloc(tsd_tsdn(tsd),\n\t\t    b0get(), PROF_NCTX_LOCKS * sizeof(malloc_mutex_t),\n\t\t    CACHELINE);\n\t\tif (gctx_locks == NULL) {\n\t\t\treturn true;\n\t\t}\n\t\tfor (i = 0; i < PROF_NCTX_LOCKS; i++) {\n\t\t\tif (malloc_mutex_init(&gctx_locks[i], \"prof_gctx\",\n\t\t\t    WITNESS_RANK_PROF_GCTX,\n\t\t\t    malloc_mutex_rank_exclusive)) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\n\t\ttdata_locks = (malloc_mutex_t *)base_alloc(tsd_tsdn(tsd),\n\t\t    b0get(), PROF_NTDATA_LOCKS * sizeof(malloc_mutex_t),\n\t\t    CACHELINE);\n\t\tif (tdata_locks == NULL) {\n\t\t\treturn true;\n\t\t}\n\t\tfor (i = 0; i < PROF_NTDATA_LOCKS; i++) {\n\t\t\tif (malloc_mutex_init(&tdata_locks[i], \"prof_tdata\",\n\t\t\t    WITNESS_RANK_PROF_TDATA,\n\t\t\t    malloc_mutex_rank_exclusive)) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t}\n\n#ifdef JEMALLOC_PROF_LIBGCC\n\t/*\n\t * Cause the backtracing machinery to allocate its internal state\n\t * before enabling profiling.\n\t */\n\t_Unwind_Backtrace(prof_unwind_init_callback, NULL);\n#endif\n\n\tprof_booted = true;\n\n\treturn false;\n}\n\nvoid\nprof_prefork0(tsdn_t *tsdn) {\n\tif (config_prof && opt_prof) {\n\t\tunsigned i;\n\n\t\tmalloc_mutex_prefork(tsdn, &prof_dump_mtx);\n\t\tmalloc_mutex_prefork(tsdn, &bt2gctx_mtx);\n\t\tmalloc_mutex_prefork(tsdn, &tdatas_mtx);\n\t\tfor (i = 0; i < PROF_NTDATA_LOCKS; i++) {\n\t\t\tmalloc_mutex_prefork(tsdn, &tdata_locks[i]);\n\t\t}\n\t\tfor (i = 0; i < PROF_NCTX_LOCKS; i++) {\n\t\t\tmalloc_mutex_prefork(tsdn, &gctx_locks[i]);\n\t\t}\n\t}\n}\n\nvoid\nprof_prefork1(tsdn_t *tsdn) {\n\tif (config_prof && opt_prof) {\n\t\tmalloc_mutex_prefork(tsdn, &prof_active_mtx);\n\t\tmalloc_mutex_prefork(tsdn, &prof_dump_seq_mtx);\n\t\tmalloc_mutex_prefork(tsdn, &prof_gdump_mtx);\n\t\tmalloc_mutex_prefork(tsdn, &next_thr_uid_mtx);\n\t\tmalloc_mutex_prefork(tsdn, &prof_thread_active_init_mtx);\n\t}\n}\n\nvoid\nprof_postfork_parent(tsdn_t *tsdn) {\n\tif (config_prof && opt_prof) {\n\t\tunsigned i;\n\n\t\tmalloc_mutex_postfork_parent(tsdn,\n\t\t    &prof_thread_active_init_mtx);\n\t\tmalloc_mutex_postfork_parent(tsdn, &next_thr_uid_mtx);\n\t\tmalloc_mutex_postfork_parent(tsdn, &prof_gdump_mtx);\n\t\tmalloc_mutex_postfork_parent(tsdn, &prof_dump_seq_mtx);\n\t\tmalloc_mutex_postfork_parent(tsdn, &prof_active_mtx);\n\t\tfor (i = 0; i < PROF_NCTX_LOCKS; i++) {\n\t\t\tmalloc_mutex_postfork_parent(tsdn, &gctx_locks[i]);\n\t\t}\n\t\tfor (i = 0; i < PROF_NTDATA_LOCKS; i++) {\n\t\t\tmalloc_mutex_postfork_parent(tsdn, &tdata_locks[i]);\n\t\t}\n\t\tmalloc_mutex_postfork_parent(tsdn, &tdatas_mtx);\n\t\tmalloc_mutex_postfork_parent(tsdn, &bt2gctx_mtx);\n\t\tmalloc_mutex_postfork_parent(tsdn, &prof_dump_mtx);\n\t}\n}\n\nvoid\nprof_postfork_child(tsdn_t *tsdn) {\n\tif (config_prof && opt_prof) {\n\t\tunsigned i;\n\n\t\tmalloc_mutex_postfork_child(tsdn, &prof_thread_active_init_mtx);\n\t\tmalloc_mutex_postfork_child(tsdn, &next_thr_uid_mtx);\n\t\tmalloc_mutex_postfork_child(tsdn, &prof_gdump_mtx);\n\t\tmalloc_mutex_postfork_child(tsdn, &prof_dump_seq_mtx);\n\t\tmalloc_mutex_postfork_child(tsdn, &prof_active_mtx);\n\t\tfor (i = 0; i < PROF_NCTX_LOCKS; i++) {\n\t\t\tmalloc_mutex_postfork_child(tsdn, &gctx_locks[i]);\n\t\t}\n\t\tfor (i = 0; i < PROF_NTDATA_LOCKS; i++) {\n\t\t\tmalloc_mutex_postfork_child(tsdn, &tdata_locks[i]);\n\t\t}\n\t\tmalloc_mutex_postfork_child(tsdn, &tdatas_mtx);\n\t\tmalloc_mutex_postfork_child(tsdn, &bt2gctx_mtx);\n\t\tmalloc_mutex_postfork_child(tsdn, &prof_dump_mtx);\n\t}\n}\n\n/******************************************************************************/\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/rtree.c",
    "content": "#define JEMALLOC_RTREE_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/mutex.h\"\n\n/*\n * Only the most significant bits of keys passed to rtree_{read,write}() are\n * used.\n */\nbool\nrtree_new(rtree_t *rtree, bool zeroed) {\n#ifdef JEMALLOC_JET\n\tif (!zeroed) {\n\t\tmemset(rtree, 0, sizeof(rtree_t)); /* Clear root. */\n\t}\n#else\n\tassert(zeroed);\n#endif\n\n\tif (malloc_mutex_init(&rtree->init_lock, \"rtree\", WITNESS_RANK_RTREE,\n\t    malloc_mutex_rank_exclusive)) {\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\nstatic rtree_node_elm_t *\nrtree_node_alloc_impl(tsdn_t *tsdn, rtree_t *rtree, size_t nelms) {\n\treturn (rtree_node_elm_t *)base_alloc(tsdn, b0get(), nelms *\n\t    sizeof(rtree_node_elm_t), CACHELINE);\n}\nrtree_node_alloc_t *JET_MUTABLE rtree_node_alloc = rtree_node_alloc_impl;\n\nstatic void\nrtree_node_dalloc_impl(tsdn_t *tsdn, rtree_t *rtree, rtree_node_elm_t *node) {\n\t/* Nodes are never deleted during normal operation. */\n\tnot_reached();\n}\nUNUSED rtree_node_dalloc_t *JET_MUTABLE rtree_node_dalloc =\n    rtree_node_dalloc_impl;\n\nstatic rtree_leaf_elm_t *\nrtree_leaf_alloc_impl(tsdn_t *tsdn, rtree_t *rtree, size_t nelms) {\n\treturn (rtree_leaf_elm_t *)base_alloc(tsdn, b0get(), nelms *\n\t    sizeof(rtree_leaf_elm_t), CACHELINE);\n}\nrtree_leaf_alloc_t *JET_MUTABLE rtree_leaf_alloc = rtree_leaf_alloc_impl;\n\nstatic void\nrtree_leaf_dalloc_impl(tsdn_t *tsdn, rtree_t *rtree, rtree_leaf_elm_t *leaf) {\n\t/* Leaves are never deleted during normal operation. */\n\tnot_reached();\n}\nUNUSED rtree_leaf_dalloc_t *JET_MUTABLE rtree_leaf_dalloc =\n    rtree_leaf_dalloc_impl;\n\n#ifdef JEMALLOC_JET\n#  if RTREE_HEIGHT > 1\nstatic void\nrtree_delete_subtree(tsdn_t *tsdn, rtree_t *rtree, rtree_node_elm_t *subtree,\n    unsigned level) {\n\tsize_t nchildren = ZU(1) << rtree_levels[level].bits;\n\tif (level + 2 < RTREE_HEIGHT) {\n\t\tfor (size_t i = 0; i < nchildren; i++) {\n\t\t\trtree_node_elm_t *node =\n\t\t\t    (rtree_node_elm_t *)atomic_load_p(&subtree[i].child,\n\t\t\t    ATOMIC_RELAXED);\n\t\t\tif (node != NULL) {\n\t\t\t\trtree_delete_subtree(tsdn, rtree, node, level +\n\t\t\t\t    1);\n\t\t\t}\n\t\t}\n\t} else {\n\t\tfor (size_t i = 0; i < nchildren; i++) {\n\t\t\trtree_leaf_elm_t *leaf =\n\t\t\t    (rtree_leaf_elm_t *)atomic_load_p(&subtree[i].child,\n\t\t\t    ATOMIC_RELAXED);\n\t\t\tif (leaf != NULL) {\n\t\t\t\trtree_leaf_dalloc(tsdn, rtree, leaf);\n\t\t\t}\n\t\t}\n\t}\n\n\tif (subtree != rtree->root) {\n\t\trtree_node_dalloc(tsdn, rtree, subtree);\n\t}\n}\n#  endif\n\nvoid\nrtree_delete(tsdn_t *tsdn, rtree_t *rtree) {\n#  if RTREE_HEIGHT > 1\n\trtree_delete_subtree(tsdn, rtree, rtree->root, 0);\n#  endif\n}\n#endif\n\nstatic rtree_node_elm_t *\nrtree_node_init(tsdn_t *tsdn, rtree_t *rtree, unsigned level,\n    atomic_p_t *elmp) {\n\tmalloc_mutex_lock(tsdn, &rtree->init_lock);\n\t/*\n\t * If *elmp is non-null, then it was initialized with the init lock\n\t * held, so we can get by with 'relaxed' here.\n\t */\n\trtree_node_elm_t *node = atomic_load_p(elmp, ATOMIC_RELAXED);\n\tif (node == NULL) {\n\t\tnode = rtree_node_alloc(tsdn, rtree, ZU(1) <<\n\t\t    rtree_levels[level].bits);\n\t\tif (node == NULL) {\n\t\t\tmalloc_mutex_unlock(tsdn, &rtree->init_lock);\n\t\t\treturn NULL;\n\t\t}\n\t\t/*\n\t\t * Even though we hold the lock, a later reader might not; we\n\t\t * need release semantics.\n\t\t */\n\t\tatomic_store_p(elmp, node, ATOMIC_RELEASE);\n\t}\n\tmalloc_mutex_unlock(tsdn, &rtree->init_lock);\n\n\treturn node;\n}\n\nstatic rtree_leaf_elm_t *\nrtree_leaf_init(tsdn_t *tsdn, rtree_t *rtree, atomic_p_t *elmp) {\n\tmalloc_mutex_lock(tsdn, &rtree->init_lock);\n\t/*\n\t * If *elmp is non-null, then it was initialized with the init lock\n\t * held, so we can get by with 'relaxed' here.\n\t */\n\trtree_leaf_elm_t *leaf = atomic_load_p(elmp, ATOMIC_RELAXED);\n\tif (leaf == NULL) {\n\t\tleaf = rtree_leaf_alloc(tsdn, rtree, ZU(1) <<\n\t\t    rtree_levels[RTREE_HEIGHT-1].bits);\n\t\tif (leaf == NULL) {\n\t\t\tmalloc_mutex_unlock(tsdn, &rtree->init_lock);\n\t\t\treturn NULL;\n\t\t}\n\t\t/*\n\t\t * Even though we hold the lock, a later reader might not; we\n\t\t * need release semantics.\n\t\t */\n\t\tatomic_store_p(elmp, leaf, ATOMIC_RELEASE);\n\t}\n\tmalloc_mutex_unlock(tsdn, &rtree->init_lock);\n\n\treturn leaf;\n}\n\nstatic bool\nrtree_node_valid(rtree_node_elm_t *node) {\n\treturn ((uintptr_t)node != (uintptr_t)0);\n}\n\nstatic bool\nrtree_leaf_valid(rtree_leaf_elm_t *leaf) {\n\treturn ((uintptr_t)leaf != (uintptr_t)0);\n}\n\nstatic rtree_node_elm_t *\nrtree_child_node_tryread(rtree_node_elm_t *elm, bool dependent) {\n\trtree_node_elm_t *node;\n\n\tif (dependent) {\n\t\tnode = (rtree_node_elm_t *)atomic_load_p(&elm->child,\n\t\t    ATOMIC_RELAXED);\n\t} else {\n\t\tnode = (rtree_node_elm_t *)atomic_load_p(&elm->child,\n\t\t    ATOMIC_ACQUIRE);\n\t}\n\n\tassert(!dependent || node != NULL);\n\treturn node;\n}\n\nstatic rtree_node_elm_t *\nrtree_child_node_read(tsdn_t *tsdn, rtree_t *rtree, rtree_node_elm_t *elm,\n    unsigned level, bool dependent) {\n\trtree_node_elm_t *node;\n\n\tnode = rtree_child_node_tryread(elm, dependent);\n\tif (!dependent && unlikely(!rtree_node_valid(node))) {\n\t\tnode = rtree_node_init(tsdn, rtree, level + 1, &elm->child);\n\t}\n\tassert(!dependent || node != NULL);\n\treturn node;\n}\n\nstatic rtree_leaf_elm_t *\nrtree_child_leaf_tryread(rtree_node_elm_t *elm, bool dependent) {\n\trtree_leaf_elm_t *leaf;\n\n\tif (dependent) {\n\t\tleaf = (rtree_leaf_elm_t *)atomic_load_p(&elm->child,\n\t\t    ATOMIC_RELAXED);\n\t} else {\n\t\tleaf = (rtree_leaf_elm_t *)atomic_load_p(&elm->child,\n\t\t    ATOMIC_ACQUIRE);\n\t}\n\n\tassert(!dependent || leaf != NULL);\n\treturn leaf;\n}\n\nstatic rtree_leaf_elm_t *\nrtree_child_leaf_read(tsdn_t *tsdn, rtree_t *rtree, rtree_node_elm_t *elm,\n    unsigned level, bool dependent) {\n\trtree_leaf_elm_t *leaf;\n\n\tleaf = rtree_child_leaf_tryread(elm, dependent);\n\tif (!dependent && unlikely(!rtree_leaf_valid(leaf))) {\n\t\tleaf = rtree_leaf_init(tsdn, rtree, &elm->child);\n\t}\n\tassert(!dependent || leaf != NULL);\n\treturn leaf;\n}\n\nrtree_leaf_elm_t *\nrtree_leaf_elm_lookup_hard(tsdn_t *tsdn, rtree_t *rtree, rtree_ctx_t *rtree_ctx,\n    uintptr_t key, bool dependent, bool init_missing) {\n\trtree_node_elm_t *node;\n\trtree_leaf_elm_t *leaf;\n#if RTREE_HEIGHT > 1\n\tnode = rtree->root;\n#else\n\tleaf = rtree->root;\n#endif\n\n\tif (config_debug) {\n\t\tuintptr_t leafkey = rtree_leafkey(key);\n\t\tfor (unsigned i = 0; i < RTREE_CTX_NCACHE; i++) {\n\t\t\tassert(rtree_ctx->cache[i].leafkey != leafkey);\n\t\t}\n\t\tfor (unsigned i = 0; i < RTREE_CTX_NCACHE_L2; i++) {\n\t\t\tassert(rtree_ctx->l2_cache[i].leafkey != leafkey);\n\t\t}\n\t}\n\n#define RTREE_GET_CHILD(level) {\t\t\t\t\t\\\n\t\tassert(level < RTREE_HEIGHT-1);\t\t\t\t\\\n\t\tif (level != 0 && !dependent &&\t\t\t\t\\\n\t\t    unlikely(!rtree_node_valid(node))) {\t\t\\\n\t\t\treturn NULL;\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\tuintptr_t subkey = rtree_subkey(key, level);\t\t\\\n\t\tif (level + 2 < RTREE_HEIGHT) {\t\t\t\t\\\n\t\t\tnode = init_missing ?\t\t\t\t\\\n\t\t\t    rtree_child_node_read(tsdn, rtree,\t\t\\\n\t\t\t    &node[subkey], level, dependent) :\t\t\\\n\t\t\t    rtree_child_node_tryread(&node[subkey],\t\\\n\t\t\t    dependent);\t\t\t\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t\tleaf = init_missing ?\t\t\t\t\\\n\t\t\t    rtree_child_leaf_read(tsdn, rtree,\t\t\\\n\t\t\t    &node[subkey], level, dependent) :\t\t\\\n\t\t\t    rtree_child_leaf_tryread(&node[subkey],\t\\\n\t\t\t    dependent);\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\n\t/*\n\t * Cache replacement upon hard lookup (i.e. L1 & L2 rtree cache miss):\n\t * (1) evict last entry in L2 cache; (2) move the collision slot from L1\n\t * cache down to L2; and 3) fill L1.\n\t */\n#define RTREE_GET_LEAF(level) {\t\t\t\t\t\t\\\n\t\tassert(level == RTREE_HEIGHT-1);\t\t\t\\\n\t\tif (!dependent && unlikely(!rtree_leaf_valid(leaf))) {\t\\\n\t\t\treturn NULL;\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\tif (RTREE_CTX_NCACHE_L2 > 1) {\t\t\t\t\\\n\t\t\tmemmove(&rtree_ctx->l2_cache[1],\t\t\\\n\t\t\t    &rtree_ctx->l2_cache[0],\t\t\t\\\n\t\t\t    sizeof(rtree_ctx_cache_elm_t) *\t\t\\\n\t\t\t    (RTREE_CTX_NCACHE_L2 - 1));\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\tsize_t slot = rtree_cache_direct_map(key);\t\t\\\n\t\trtree_ctx->l2_cache[0].leafkey =\t\t\t\\\n\t\t    rtree_ctx->cache[slot].leafkey;\t\t\t\\\n\t\trtree_ctx->l2_cache[0].leaf =\t\t\t\t\\\n\t\t    rtree_ctx->cache[slot].leaf;\t\t\t\\\n\t\tuintptr_t leafkey = rtree_leafkey(key);\t\t\t\\\n\t\trtree_ctx->cache[slot].leafkey = leafkey;\t\t\\\n\t\trtree_ctx->cache[slot].leaf = leaf;\t\t\t\\\n\t\tuintptr_t subkey = rtree_subkey(key, level);\t\t\\\n\t\treturn &leaf[subkey];\t\t\t\t\t\\\n\t}\n\tif (RTREE_HEIGHT > 1) {\n\t\tRTREE_GET_CHILD(0)\n\t}\n\tif (RTREE_HEIGHT > 2) {\n\t\tRTREE_GET_CHILD(1)\n\t}\n\tif (RTREE_HEIGHT > 3) {\n\t\tfor (unsigned i = 2; i < RTREE_HEIGHT-1; i++) {\n\t\t\tRTREE_GET_CHILD(i)\n\t\t}\n\t}\n\tRTREE_GET_LEAF(RTREE_HEIGHT-1)\n#undef RTREE_GET_CHILD\n#undef RTREE_GET_LEAF\n\tnot_reached();\n}\n\nvoid\nrtree_ctx_data_init(rtree_ctx_t *ctx) {\n\tfor (unsigned i = 0; i < RTREE_CTX_NCACHE; i++) {\n\t\trtree_ctx_cache_elm_t *cache = &ctx->cache[i];\n\t\tcache->leafkey = RTREE_LEAFKEY_INVALID;\n\t\tcache->leaf = NULL;\n\t}\n\tfor (unsigned i = 0; i < RTREE_CTX_NCACHE_L2; i++) {\n\t\trtree_ctx_cache_elm_t *cache = &ctx->l2_cache[i];\n\t\tcache->leafkey = RTREE_LEAFKEY_INVALID;\n\t\tcache->leaf = NULL;\n\t}\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/spin.c",
    "content": "#define JEMALLOC_SPIN_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n\n#include \"jemalloc/internal/spin.h\"\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/stats.c",
    "content": "#define JEMALLOC_STATS_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/ctl.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/mutex_prof.h\"\n\nconst char *global_mutex_names[mutex_prof_num_global_mutexes] = {\n#define OP(mtx) #mtx,\n\tMUTEX_PROF_GLOBAL_MUTEXES\n#undef OP\n};\n\nconst char *arena_mutex_names[mutex_prof_num_arena_mutexes] = {\n#define OP(mtx) #mtx,\n\tMUTEX_PROF_ARENA_MUTEXES\n#undef OP\n};\n\n#define CTL_GET(n, v, t) do {\t\t\t\t\t\t\\\n\tsize_t sz = sizeof(t);\t\t\t\t\t\t\\\n\txmallctl(n, (void *)v, &sz, NULL, 0);\t\t\t\t\\\n} while (0)\n\n#define CTL_M2_GET(n, i, v, t) do {\t\t\t\t\t\\\n\tsize_t mib[CTL_MAX_DEPTH];\t\t\t\t\t\\\n\tsize_t miblen = sizeof(mib) / sizeof(size_t);\t\t\t\\\n\tsize_t sz = sizeof(t);\t\t\t\t\t\t\\\n\txmallctlnametomib(n, mib, &miblen);\t\t\t\t\\\n\tmib[2] = (i);\t\t\t\t\t\t\t\\\n\txmallctlbymib(mib, miblen, (void *)v, &sz, NULL, 0);\t\t\\\n} while (0)\n\n#define CTL_M2_M4_GET(n, i, j, v, t) do {\t\t\t\t\\\n\tsize_t mib[CTL_MAX_DEPTH];\t\t\t\t\t\\\n\tsize_t miblen = sizeof(mib) / sizeof(size_t);\t\t\t\\\n\tsize_t sz = sizeof(t);\t\t\t\t\t\t\\\n\txmallctlnametomib(n, mib, &miblen);\t\t\t\t\\\n\tmib[2] = (i);\t\t\t\t\t\t\t\\\n\tmib[4] = (j);\t\t\t\t\t\t\t\\\n\txmallctlbymib(mib, miblen, (void *)v, &sz, NULL, 0);\t\t\\\n} while (0)\n\n/******************************************************************************/\n/* Data. */\n\nbool opt_stats_print = false;\nchar opt_stats_print_opts[stats_print_tot_num_options+1] = \"\";\n\n/******************************************************************************/\n\n/* Calculate x.yyy and output a string (takes a fixed sized char array). */\nstatic bool\nget_rate_str(uint64_t dividend, uint64_t divisor, char str[6]) {\n\tif (divisor == 0 || dividend > divisor) {\n\t\t/* The rate is not supposed to be greater than 1. */\n\t\treturn true;\n\t}\n\tif (dividend > 0) {\n\t\tassert(UINT64_MAX / dividend >= 1000);\n\t}\n\n\tunsigned n = (unsigned)((dividend * 1000) / divisor);\n\tif (n < 10) {\n\t\tmalloc_snprintf(str, 6, \"0.00%u\", n);\n\t} else if (n < 100) {\n\t\tmalloc_snprintf(str, 6, \"0.0%u\", n);\n\t} else if (n < 1000) {\n\t\tmalloc_snprintf(str, 6, \"0.%u\", n);\n\t} else {\n\t\tmalloc_snprintf(str, 6, \"1\");\n\t}\n\n\treturn false;\n}\n\n#define MUTEX_CTL_STR_MAX_LENGTH 128\nstatic void\ngen_mutex_ctl_str(char *str, size_t buf_len, const char *prefix,\n    const char *mutex, const char *counter) {\n\tmalloc_snprintf(str, buf_len, \"stats.%s.%s.%s\", prefix, mutex, counter);\n}\n\nstatic void\nread_arena_bin_mutex_stats(unsigned arena_ind, unsigned bin_ind,\n    uint64_t results[mutex_prof_num_counters]) {\n\tchar cmd[MUTEX_CTL_STR_MAX_LENGTH];\n#define OP(c, t)\t\t\t\t\t\t\t\\\n    gen_mutex_ctl_str(cmd, MUTEX_CTL_STR_MAX_LENGTH,\t\t\t\\\n        \"arenas.0.bins.0\",\"mutex\", #c);\t\t\t\t\t\\\n    CTL_M2_M4_GET(cmd, arena_ind, bin_ind,\t\t\t\t\\\n        (t *)&results[mutex_counter_##c], t);\nMUTEX_PROF_COUNTERS\n#undef OP\n}\n\nstatic void\nmutex_stats_output_json(void (*write_cb)(void *, const char *), void *cbopaque,\n    const char *name, uint64_t stats[mutex_prof_num_counters],\n    const char *json_indent, bool last) {\n\tmalloc_cprintf(write_cb, cbopaque, \"%s\\\"%s\\\": {\\n\", json_indent, name);\n\n\tmutex_prof_counter_ind_t k = 0;\n\tchar *fmt_str[2] = {\"%s\\t\\\"%s\\\": %\"FMTu32\"%s\\n\",\n\t    \"%s\\t\\\"%s\\\": %\"FMTu64\"%s\\n\"};\n#define OP(c, t)\t\t\t\t\t\t\t\\\n\tmalloc_cprintf(write_cb, cbopaque,\t\t\t\t\\\n\t    fmt_str[sizeof(t) / sizeof(uint32_t) - 1], \t\t\t\\\n\t    json_indent, #c, (t)stats[mutex_counter_##c],\t\t\\\n\t    (++k == mutex_prof_num_counters) ? \"\" : \",\");\nMUTEX_PROF_COUNTERS\n#undef OP\n\tmalloc_cprintf(write_cb, cbopaque, \"%s}%s\\n\", json_indent,\n\t    last ? \"\" : \",\");\n}\n\nstatic void\nstats_arena_bins_print(void (*write_cb)(void *, const char *), void *cbopaque,\n    bool json, bool large, bool mutex, unsigned i) {\n\tsize_t page;\n\tbool in_gap, in_gap_prev;\n\tunsigned nbins, j;\n\n\tCTL_GET(\"arenas.page\", &page, size_t);\n\n\tCTL_GET(\"arenas.nbins\", &nbins, unsigned);\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"bins\\\": [\\n\");\n\t} else {\n\t\tchar *mutex_counters = \"   n_lock_ops    n_waiting\"\n\t\t    \"   n_spin_acq  total_wait_ns  max_wait_ns\\n\";\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"bins:           size ind    allocated      nmalloc\"\n\t\t    \"      ndalloc    nrequests      curregs     curslabs regs\"\n\t\t    \" pgs  util       nfills     nflushes     newslabs\"\n\t\t    \"      reslabs%s\", mutex ? mutex_counters : \"\\n\");\n\t}\n\tfor (j = 0, in_gap = false; j < nbins; j++) {\n\t\tuint64_t nslabs;\n\t\tsize_t reg_size, slab_size, curregs;\n\t\tsize_t curslabs;\n\t\tuint32_t nregs;\n\t\tuint64_t nmalloc, ndalloc, nrequests, nfills, nflushes;\n\t\tuint64_t nreslabs;\n\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.bins.0.nslabs\", i, j, &nslabs,\n\t\t    uint64_t);\n\t\tin_gap_prev = in_gap;\n\t\tin_gap = (nslabs == 0);\n\n\t\tif (!json && in_gap_prev && !in_gap) {\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t    \"                     ---\\n\");\n\t\t}\n\n\t\tCTL_M2_GET(\"arenas.bin.0.size\", j, &reg_size, size_t);\n\t\tCTL_M2_GET(\"arenas.bin.0.nregs\", j, &nregs, uint32_t);\n\t\tCTL_M2_GET(\"arenas.bin.0.slab_size\", j, &slab_size, size_t);\n\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.bins.0.nmalloc\", i, j, &nmalloc,\n\t\t    uint64_t);\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.bins.0.ndalloc\", i, j, &ndalloc,\n\t\t    uint64_t);\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.bins.0.curregs\", i, j, &curregs,\n\t\t    size_t);\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.bins.0.nrequests\", i, j,\n\t\t    &nrequests, uint64_t);\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.bins.0.nfills\", i, j, &nfills,\n\t\t    uint64_t);\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.bins.0.nflushes\", i, j, &nflushes,\n\t\t    uint64_t);\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.bins.0.nreslabs\", i, j, &nreslabs,\n\t\t    uint64_t);\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.bins.0.curslabs\", i, j, &curslabs,\n\t\t    size_t);\n\n\t\tif (json) {\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t    \"\\t\\t\\t\\t\\t{\\n\"\n\t\t\t    \"\\t\\t\\t\\t\\t\\t\\\"nmalloc\\\": %\"FMTu64\",\\n\"\n\t\t\t    \"\\t\\t\\t\\t\\t\\t\\\"ndalloc\\\": %\"FMTu64\",\\n\"\n\t\t\t    \"\\t\\t\\t\\t\\t\\t\\\"curregs\\\": %zu,\\n\"\n\t\t\t    \"\\t\\t\\t\\t\\t\\t\\\"nrequests\\\": %\"FMTu64\",\\n\"\n\t\t\t    \"\\t\\t\\t\\t\\t\\t\\\"nfills\\\": %\"FMTu64\",\\n\"\n\t\t\t    \"\\t\\t\\t\\t\\t\\t\\\"nflushes\\\": %\"FMTu64\",\\n\"\n\t\t\t    \"\\t\\t\\t\\t\\t\\t\\\"nreslabs\\\": %\"FMTu64\",\\n\"\n\t\t\t    \"\\t\\t\\t\\t\\t\\t\\\"curslabs\\\": %zu%s\\n\",\n\t\t\t    nmalloc, ndalloc, curregs, nrequests, nfills,\n\t\t\t    nflushes, nreslabs, curslabs, mutex ? \",\" : \"\");\n\t\t\tif (mutex) {\n\t\t\t\tuint64_t mutex_stats[mutex_prof_num_counters];\n\t\t\t\tread_arena_bin_mutex_stats(i, j, mutex_stats);\n\t\t\t\tmutex_stats_output_json(write_cb, cbopaque,\n\t\t\t\t    \"mutex\", mutex_stats, \"\\t\\t\\t\\t\\t\\t\", true);\n\t\t\t}\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t    \"\\t\\t\\t\\t\\t}%s\\n\",\n\t\t\t    (j + 1 < nbins) ? \",\" : \"\");\n\t\t} else if (!in_gap) {\n\t\t\tsize_t availregs = nregs * curslabs;\n\t\t\tchar util[6];\n\t\t\tif (get_rate_str((uint64_t)curregs, (uint64_t)availregs,\n\t\t\t    util)) {\n\t\t\t\tif (availregs == 0) {\n\t\t\t\t\tmalloc_snprintf(util, sizeof(util),\n\t\t\t\t\t    \"1\");\n\t\t\t\t} else if (curregs > availregs) {\n\t\t\t\t\t/*\n\t\t\t\t\t * Race detected: the counters were read\n\t\t\t\t\t * in separate mallctl calls and\n\t\t\t\t\t * concurrent operations happened in\n\t\t\t\t\t * between. In this case no meaningful\n\t\t\t\t\t * utilization can be computed.\n\t\t\t\t\t */\n\t\t\t\t\tmalloc_snprintf(util, sizeof(util),\n\t\t\t\t\t    \" race\");\n\t\t\t\t} else {\n\t\t\t\t\tnot_reached();\n\t\t\t\t}\n\t\t\t}\n\t\t\tuint64_t mutex_stats[mutex_prof_num_counters];\n\t\t\tif (mutex) {\n\t\t\t\tread_arena_bin_mutex_stats(i, j, mutex_stats);\n\t\t\t}\n\n\t\t\tmalloc_cprintf(write_cb, cbopaque, \"%20zu %3u %12zu %12\"\n\t\t\t    FMTu64\" %12\"FMTu64\" %12\"FMTu64\" %12zu %12zu %4u\"\n\t\t\t    \" %3zu %-5s %12\"FMTu64\" %12\"FMTu64\" %12\"FMTu64\n\t\t\t    \" %12\"FMTu64, reg_size, j, curregs * reg_size,\n\t\t\t    nmalloc, ndalloc, nrequests, curregs, curslabs,\n\t\t\t    nregs, slab_size / page, util, nfills, nflushes,\n\t\t\t    nslabs, nreslabs);\n\n\t\t\t/* Output less info for bin mutexes to save space. */\n\t\t\tif (mutex) {\n\t\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t\t    \" %12\"FMTu64\" %12\"FMTu64\" %12\"FMTu64\n\t\t\t\t    \" %14\"FMTu64\" %12\"FMTu64\"\\n\",\n\t\t\t\t    mutex_stats[mutex_counter_num_ops],\n\t\t\t\t    mutex_stats[mutex_counter_num_wait],\n\t\t\t\t    mutex_stats[mutex_counter_num_spin_acq],\n\t\t\t\t    mutex_stats[mutex_counter_total_wait_time],\n\t\t\t\t    mutex_stats[mutex_counter_max_wait_time]);\n\t\t\t} else {\n\t\t\t\tmalloc_cprintf(write_cb, cbopaque, \"\\n\");\n\t\t\t}\n\t\t}\n\t}\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t]%s\\n\", large ? \",\" : \"\");\n\t} else {\n\t\tif (in_gap) {\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t    \"                     ---\\n\");\n\t\t}\n\t}\n}\n\nstatic void\nstats_arena_lextents_print(void (*write_cb)(void *, const char *),\n    void *cbopaque, bool json, unsigned i) {\n\tunsigned nbins, nlextents, j;\n\tbool in_gap, in_gap_prev;\n\n\tCTL_GET(\"arenas.nbins\", &nbins, unsigned);\n\tCTL_GET(\"arenas.nlextents\", &nlextents, unsigned);\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"lextents\\\": [\\n\");\n\t} else {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"large:          size ind    allocated      nmalloc\"\n\t\t    \"      ndalloc    nrequests  curlextents\\n\");\n\t}\n\tfor (j = 0, in_gap = false; j < nlextents; j++) {\n\t\tuint64_t nmalloc, ndalloc, nrequests;\n\t\tsize_t lextent_size, curlextents;\n\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.lextents.0.nmalloc\", i, j,\n\t\t    &nmalloc, uint64_t);\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.lextents.0.ndalloc\", i, j,\n\t\t    &ndalloc, uint64_t);\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.lextents.0.nrequests\", i, j,\n\t\t    &nrequests, uint64_t);\n\t\tin_gap_prev = in_gap;\n\t\tin_gap = (nrequests == 0);\n\n\t\tif (!json && in_gap_prev && !in_gap) {\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t    \"                     ---\\n\");\n\t\t}\n\n\t\tCTL_M2_GET(\"arenas.lextent.0.size\", j, &lextent_size, size_t);\n\t\tCTL_M2_M4_GET(\"stats.arenas.0.lextents.0.curlextents\", i, j,\n\t\t    &curlextents, size_t);\n\t\tif (json) {\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t    \"\\t\\t\\t\\t\\t{\\n\"\n\t\t\t    \"\\t\\t\\t\\t\\t\\t\\\"curlextents\\\": %zu\\n\"\n\t\t\t    \"\\t\\t\\t\\t\\t}%s\\n\",\n\t\t\t    curlextents,\n\t\t\t    (j + 1 < nlextents) ? \",\" : \"\");\n\t\t} else if (!in_gap) {\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t    \"%20zu %3u %12zu %12\"FMTu64\" %12\"FMTu64\n\t\t\t    \" %12\"FMTu64\" %12zu\\n\",\n\t\t\t    lextent_size, nbins + j,\n\t\t\t    curlextents * lextent_size, nmalloc, ndalloc,\n\t\t\t    nrequests, curlextents);\n\t\t}\n\t}\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t]\\n\");\n\t} else {\n\t\tif (in_gap) {\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t    \"                     ---\\n\");\n\t\t}\n\t}\n}\n\nstatic void\nread_arena_mutex_stats(unsigned arena_ind,\n    uint64_t results[mutex_prof_num_arena_mutexes][mutex_prof_num_counters]) {\n\tchar cmd[MUTEX_CTL_STR_MAX_LENGTH];\n\n\tmutex_prof_arena_ind_t i;\n\tfor (i = 0; i < mutex_prof_num_arena_mutexes; i++) {\n#define OP(c, t)\t\t\t\t\t\t\t\\\n\t\tgen_mutex_ctl_str(cmd, MUTEX_CTL_STR_MAX_LENGTH,\t\\\n\t\t    \"arenas.0.mutexes\",\tarena_mutex_names[i], #c);\t\\\n\t\tCTL_M2_GET(cmd, arena_ind,\t\t\t\t\\\n\t\t    (t *)&results[i][mutex_counter_##c], t);\nMUTEX_PROF_COUNTERS\n#undef OP\n\t}\n}\n\nstatic void\nmutex_stats_output(void (*write_cb)(void *, const char *), void *cbopaque,\n    const char *name, uint64_t stats[mutex_prof_num_counters],\n    bool first_mutex) {\n\tif (first_mutex) {\n\t\t/* Print title. */\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"                           n_lock_ops       n_waiting\"\n\t\t    \"      n_spin_acq  n_owner_switch   total_wait_ns\"\n\t\t    \"     max_wait_ns  max_n_thds\\n\");\n\t}\n\n\tmalloc_cprintf(write_cb, cbopaque, \"%s\", name);\n\tmalloc_cprintf(write_cb, cbopaque, \":%*c\",\n\t    (int)(20 - strlen(name)), ' ');\n\n\tchar *fmt_str[2] = {\"%12\"FMTu32, \"%16\"FMTu64};\n#define OP(c, t)\t\t\t\t\t\t\t\\\n\tmalloc_cprintf(write_cb, cbopaque,\t\t\t\t\\\n\t    fmt_str[sizeof(t) / sizeof(uint32_t) - 1],\t\t\t\\\n\t    (t)stats[mutex_counter_##c]);\nMUTEX_PROF_COUNTERS\n#undef OP\n\tmalloc_cprintf(write_cb, cbopaque, \"\\n\");\n}\n\nstatic void\nstats_arena_mutexes_print(void (*write_cb)(void *, const char *),\n    void *cbopaque, bool json, bool json_end, unsigned arena_ind) {\n\tuint64_t mutex_stats[mutex_prof_num_arena_mutexes][mutex_prof_num_counters];\n\tread_arena_mutex_stats(arena_ind, mutex_stats);\n\n\t/* Output mutex stats. */\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque, \"\\t\\t\\t\\t\\\"mutexes\\\": {\\n\");\n\t\tmutex_prof_arena_ind_t i, last_mutex;\n\t\tlast_mutex = mutex_prof_num_arena_mutexes - 1;\n\t\tfor (i = 0; i < mutex_prof_num_arena_mutexes; i++) {\n\t\t\tmutex_stats_output_json(write_cb, cbopaque,\n\t\t\t    arena_mutex_names[i], mutex_stats[i],\n\t\t\t    \"\\t\\t\\t\\t\\t\", (i == last_mutex));\n\t\t}\n\t\tmalloc_cprintf(write_cb, cbopaque, \"\\t\\t\\t\\t}%s\\n\",\n\t\t    json_end ? \"\" : \",\");\n\t} else {\n\t\tmutex_prof_arena_ind_t i;\n\t\tfor (i = 0; i < mutex_prof_num_arena_mutexes; i++) {\n\t\t\tmutex_stats_output(write_cb, cbopaque,\n\t\t\t    arena_mutex_names[i], mutex_stats[i], i == 0);\n\t\t}\n\t}\n}\n\nstatic void\nstats_arena_print(void (*write_cb)(void *, const char *), void *cbopaque,\n    bool json, unsigned i, bool bins, bool large, bool mutex) {\n\tunsigned nthreads;\n\tconst char *dss;\n\tssize_t dirty_decay_ms, muzzy_decay_ms;\n\tsize_t page, pactive, pdirty, pmuzzy, mapped, retained;\n\tsize_t base, internal, resident;\n\tuint64_t dirty_npurge, dirty_nmadvise, dirty_purged;\n\tuint64_t muzzy_npurge, muzzy_nmadvise, muzzy_purged;\n\tsize_t small_allocated;\n\tuint64_t small_nmalloc, small_ndalloc, small_nrequests;\n\tsize_t large_allocated;\n\tuint64_t large_nmalloc, large_ndalloc, large_nrequests;\n\tsize_t tcache_bytes;\n\tuint64_t uptime;\n\n\tCTL_GET(\"arenas.page\", &page, size_t);\n\n\tCTL_M2_GET(\"stats.arenas.0.nthreads\", i, &nthreads, unsigned);\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"nthreads\\\": %u,\\n\", nthreads);\n\t} else {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"assigned threads: %u\\n\", nthreads);\n\t}\n\n\tCTL_M2_GET(\"stats.arenas.0.uptime\", i, &uptime, uint64_t);\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"uptime_ns\\\": %\"FMTu64\",\\n\", uptime);\n\t} else {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"uptime: %\"FMTu64\"\\n\", uptime);\n\t}\n\n\tCTL_M2_GET(\"stats.arenas.0.dss\", i, &dss, const char *);\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"dss\\\": \\\"%s\\\",\\n\", dss);\n\t} else {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"dss allocation precedence: %s\\n\", dss);\n\t}\n\n\tCTL_M2_GET(\"stats.arenas.0.dirty_decay_ms\", i, &dirty_decay_ms,\n\t    ssize_t);\n\tCTL_M2_GET(\"stats.arenas.0.muzzy_decay_ms\", i, &muzzy_decay_ms,\n\t    ssize_t);\n\tCTL_M2_GET(\"stats.arenas.0.pactive\", i, &pactive, size_t);\n\tCTL_M2_GET(\"stats.arenas.0.pdirty\", i, &pdirty, size_t);\n\tCTL_M2_GET(\"stats.arenas.0.pmuzzy\", i, &pmuzzy, size_t);\n\tCTL_M2_GET(\"stats.arenas.0.dirty_npurge\", i, &dirty_npurge, uint64_t);\n\tCTL_M2_GET(\"stats.arenas.0.dirty_nmadvise\", i, &dirty_nmadvise,\n\t    uint64_t);\n\tCTL_M2_GET(\"stats.arenas.0.dirty_purged\", i, &dirty_purged, uint64_t);\n\tCTL_M2_GET(\"stats.arenas.0.muzzy_npurge\", i, &muzzy_npurge, uint64_t);\n\tCTL_M2_GET(\"stats.arenas.0.muzzy_nmadvise\", i, &muzzy_nmadvise,\n\t    uint64_t);\n\tCTL_M2_GET(\"stats.arenas.0.muzzy_purged\", i, &muzzy_purged, uint64_t);\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"dirty_decay_ms\\\": %zd,\\n\", dirty_decay_ms);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"muzzy_decay_ms\\\": %zd,\\n\", muzzy_decay_ms);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"pactive\\\": %zu,\\n\", pactive);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"pdirty\\\": %zu,\\n\", pdirty);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"pmuzzy\\\": %zu,\\n\", pmuzzy);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"dirty_npurge\\\": %\"FMTu64\",\\n\", dirty_npurge);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"dirty_nmadvise\\\": %\"FMTu64\",\\n\", dirty_nmadvise);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"dirty_purged\\\": %\"FMTu64\",\\n\", dirty_purged);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"muzzy_npurge\\\": %\"FMTu64\",\\n\", muzzy_npurge);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"muzzy_nmadvise\\\": %\"FMTu64\",\\n\", muzzy_nmadvise);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"muzzy_purged\\\": %\"FMTu64\",\\n\", muzzy_purged);\n\t} else {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"decaying:  time       npages       sweeps     madvises\"\n\t\t    \"       purged\\n\");\n\t\tif (dirty_decay_ms >= 0) {\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t    \"   dirty: %5zd %12zu %12\"FMTu64\" %12\"FMTu64\" %12\"\n\t\t\t    FMTu64\"\\n\", dirty_decay_ms, pdirty, dirty_npurge,\n\t\t\t    dirty_nmadvise, dirty_purged);\n\t\t} else {\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t    \"   dirty:   N/A %12zu %12\"FMTu64\" %12\"FMTu64\" %12\"\n\t\t\t    FMTu64\"\\n\", pdirty, dirty_npurge, dirty_nmadvise,\n\t\t\t    dirty_purged);\n\t\t}\n\t\tif (muzzy_decay_ms >= 0) {\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t    \"   muzzy: %5zd %12zu %12\"FMTu64\" %12\"FMTu64\" %12\"\n\t\t\t    FMTu64\"\\n\", muzzy_decay_ms, pmuzzy, muzzy_npurge,\n\t\t\t    muzzy_nmadvise, muzzy_purged);\n\t\t} else {\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t    \"   muzzy:   N/A %12zu %12\"FMTu64\" %12\"FMTu64\" %12\"\n\t\t\t    FMTu64\"\\n\", pmuzzy, muzzy_npurge, muzzy_nmadvise,\n\t\t\t    muzzy_purged);\n\t\t}\n\t}\n\n\tCTL_M2_GET(\"stats.arenas.0.small.allocated\", i, &small_allocated,\n\t    size_t);\n\tCTL_M2_GET(\"stats.arenas.0.small.nmalloc\", i, &small_nmalloc, uint64_t);\n\tCTL_M2_GET(\"stats.arenas.0.small.ndalloc\", i, &small_ndalloc, uint64_t);\n\tCTL_M2_GET(\"stats.arenas.0.small.nrequests\", i, &small_nrequests,\n\t    uint64_t);\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"small\\\": {\\n\");\n\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\t\\\"allocated\\\": %zu,\\n\", small_allocated);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\t\\\"nmalloc\\\": %\"FMTu64\",\\n\", small_nmalloc);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\t\\\"ndalloc\\\": %\"FMTu64\",\\n\", small_ndalloc);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\t\\\"nrequests\\\": %\"FMTu64\"\\n\", small_nrequests);\n\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t},\\n\");\n\t} else {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"                            allocated      nmalloc\"\n\t\t    \"      ndalloc    nrequests\\n\");\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"small:                   %12zu %12\"FMTu64\" %12\"FMTu64\n\t\t    \" %12\"FMTu64\"\\n\",\n\t\t    small_allocated, small_nmalloc, small_ndalloc,\n\t\t    small_nrequests);\n\t}\n\n\tCTL_M2_GET(\"stats.arenas.0.large.allocated\", i, &large_allocated,\n\t    size_t);\n\tCTL_M2_GET(\"stats.arenas.0.large.nmalloc\", i, &large_nmalloc, uint64_t);\n\tCTL_M2_GET(\"stats.arenas.0.large.ndalloc\", i, &large_ndalloc, uint64_t);\n\tCTL_M2_GET(\"stats.arenas.0.large.nrequests\", i, &large_nrequests,\n\t    uint64_t);\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"large\\\": {\\n\");\n\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\t\\\"allocated\\\": %zu,\\n\", large_allocated);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\t\\\"nmalloc\\\": %\"FMTu64\",\\n\", large_nmalloc);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\t\\\"ndalloc\\\": %\"FMTu64\",\\n\", large_ndalloc);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\t\\\"nrequests\\\": %\"FMTu64\"\\n\", large_nrequests);\n\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t},\\n\");\n\t} else {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"large:                   %12zu %12\"FMTu64\" %12\"FMTu64\n\t\t    \" %12\"FMTu64\"\\n\",\n\t\t    large_allocated, large_nmalloc, large_ndalloc,\n\t\t    large_nrequests);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"total:                   %12zu %12\"FMTu64\" %12\"FMTu64\n\t\t    \" %12\"FMTu64\"\\n\",\n\t\t    small_allocated + large_allocated, small_nmalloc +\n\t\t    large_nmalloc, small_ndalloc + large_ndalloc,\n\t\t    small_nrequests + large_nrequests);\n\t}\n\tif (!json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"active:                  %12zu\\n\", pactive * page);\n\t}\n\n\tCTL_M2_GET(\"stats.arenas.0.mapped\", i, &mapped, size_t);\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"mapped\\\": %zu,\\n\", mapped);\n\t} else {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"mapped:                  %12zu\\n\", mapped);\n\t}\n\n\tCTL_M2_GET(\"stats.arenas.0.retained\", i, &retained, size_t);\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"retained\\\": %zu,\\n\", retained);\n\t} else {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"retained:                %12zu\\n\", retained);\n\t}\n\n\tCTL_M2_GET(\"stats.arenas.0.base\", i, &base, size_t);\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"base\\\": %zu,\\n\", base);\n\t} else {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"base:                    %12zu\\n\", base);\n\t}\n\n\tCTL_M2_GET(\"stats.arenas.0.internal\", i, &internal, size_t);\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"internal\\\": %zu,\\n\", internal);\n\t} else {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"internal:                %12zu\\n\", internal);\n\t}\n\n\tCTL_M2_GET(\"stats.arenas.0.tcache_bytes\", i, &tcache_bytes, size_t);\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"tcache\\\": %zu,\\n\", tcache_bytes);\n\t} else {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"tcache:                  %12zu\\n\", tcache_bytes);\n\t}\n\n\tCTL_M2_GET(\"stats.arenas.0.resident\", i, &resident, size_t);\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"resident\\\": %zu%s\\n\", resident,\n\t\t    (bins || large || mutex) ? \",\" : \"\");\n\t} else {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"resident:                %12zu\\n\", resident);\n\t}\n\n\tif (mutex) {\n\t\tstats_arena_mutexes_print(write_cb, cbopaque, json,\n\t\t    !(bins || large), i);\n\t}\n\tif (bins) {\n\t\tstats_arena_bins_print(write_cb, cbopaque, json, large, mutex,\n\t\t    i);\n\t}\n\tif (large) {\n\t\tstats_arena_lextents_print(write_cb, cbopaque, json, i);\n\t}\n}\n\nstatic void\nstats_general_print(void (*write_cb)(void *, const char *), void *cbopaque,\n    bool json, bool more) {\n\tconst char *cpv;\n\tbool bv;\n\tunsigned uv;\n\tuint32_t u32v;\n\tuint64_t u64v;\n\tssize_t ssv;\n\tsize_t sv, bsz, usz, ssz, sssz, cpsz;\n\n\tbsz = sizeof(bool);\n\tusz = sizeof(unsigned);\n\tssz = sizeof(size_t);\n\tsssz = sizeof(ssize_t);\n\tcpsz = sizeof(const char *);\n\n\tCTL_GET(\"version\", &cpv, const char *);\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\"\\t\\t\\\"version\\\": \\\"%s\\\",\\n\", cpv);\n\t} else {\n\t\tmalloc_cprintf(write_cb, cbopaque, \"Version: %s\\n\", cpv);\n\t}\n\n\t/* config. */\n#define CONFIG_WRITE_BOOL_JSON(n, c)\t\t\t\t\t\\\n\tif (json) {\t\t\t\t\t\t\t\\\n\t\tCTL_GET(\"config.\"#n, &bv, bool);\t\t\t\\\n\t\tmalloc_cprintf(write_cb, cbopaque,\t\t\t\\\n\t\t    \"\\t\\t\\t\\\"\"#n\"\\\": %s%s\\n\", bv ? \"true\" : \"false\",\t\\\n\t\t    (c));\t\t\t\t\t\t\\\n\t}\n\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\\"config\\\": {\\n\");\n\t}\n\n\tCONFIG_WRITE_BOOL_JSON(cache_oblivious, \",\")\n\n\tCTL_GET(\"config.debug\", &bv, bool);\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\\"debug\\\": %s,\\n\", bv ? \"true\" : \"false\");\n\t} else {\n\t\tmalloc_cprintf(write_cb, cbopaque, \"Assertions %s\\n\",\n\t\t    bv ? \"enabled\" : \"disabled\");\n\t}\n\n\tCONFIG_WRITE_BOOL_JSON(fill, \",\")\n\tCONFIG_WRITE_BOOL_JSON(lazy_lock, \",\")\n\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\\"malloc_conf\\\": \\\"%s\\\",\\n\",\n\t\t    config_malloc_conf);\n\t} else {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"config.malloc_conf: \\\"%s\\\"\\n\", config_malloc_conf);\n\t}\n\n\tCONFIG_WRITE_BOOL_JSON(prof, \",\")\n\tCONFIG_WRITE_BOOL_JSON(prof_libgcc, \",\")\n\tCONFIG_WRITE_BOOL_JSON(prof_libunwind, \",\")\n\tCONFIG_WRITE_BOOL_JSON(stats, \",\")\n\tCONFIG_WRITE_BOOL_JSON(thp, \",\")\n\tCONFIG_WRITE_BOOL_JSON(utrace, \",\")\n\tCONFIG_WRITE_BOOL_JSON(xmalloc, \"\")\n\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t},\\n\");\n\t}\n#undef CONFIG_WRITE_BOOL_JSON\n\n\t/* opt. */\n#define OPT_WRITE_BOOL(n, c)\t\t\t\t\t\t\\\n\tif (je_mallctl(\"opt.\"#n, (void *)&bv, &bsz, NULL, 0) == 0) {\t\\\n\t\tif (json) {\t\t\t\t\t\t\\\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\t\t\\\n\t\t\t    \"\\t\\t\\t\\\"\"#n\"\\\": %s%s\\n\", bv ? \"true\" :\t\\\n\t\t\t    \"false\", (c));\t\t\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\t\t\\\n\t\t\t    \"  opt.\"#n\": %s\\n\", bv ? \"true\" : \"false\");\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\n#define OPT_WRITE_BOOL_MUTABLE(n, m, c) {\t\t\t\t\\\n\tbool bv2;\t\t\t\t\t\t\t\\\n\tif (je_mallctl(\"opt.\"#n, (void *)&bv, &bsz, NULL, 0) == 0 &&\t\\\n\t    je_mallctl(#m, (void *)&bv2, &bsz, NULL, 0) == 0) {\t\t\\\n\t\tif (json) {\t\t\t\t\t\t\\\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\t\t\\\n\t\t\t    \"\\t\\t\\t\\\"\"#n\"\\\": %s%s\\n\", bv ? \"true\" :\t\\\n\t\t\t    \"false\", (c));\t\t\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\t\t\\\n\t\t\t    \"  opt.\"#n\": %s (\"#m\": %s)\\n\", bv ? \"true\"\t\\\n\t\t\t    : \"false\", bv2 ? \"true\" : \"false\");\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n}\n#define OPT_WRITE_UNSIGNED(n, c)\t\t\t\t\t\\\n\tif (je_mallctl(\"opt.\"#n, (void *)&uv, &usz, NULL, 0) == 0) {\t\\\n\t\tif (json) {\t\t\t\t\t\t\\\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\t\t\\\n\t\t\t    \"\\t\\t\\t\\\"\"#n\"\\\": %u%s\\n\", uv, (c));\t\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\t\t\\\n\t\t\t\"  opt.\"#n\": %u\\n\", uv);\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\n#define OPT_WRITE_SSIZE_T(n, c)\t\t\t\t\t\t\\\n\tif (je_mallctl(\"opt.\"#n, (void *)&ssv, &sssz, NULL, 0) == 0) {\t\\\n\t\tif (json) {\t\t\t\t\t\t\\\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\t\t\\\n\t\t\t    \"\\t\\t\\t\\\"\"#n\"\\\": %zd%s\\n\", ssv, (c));\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\t\t\\\n\t\t\t    \"  opt.\"#n\": %zd\\n\", ssv);\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\n#define OPT_WRITE_SSIZE_T_MUTABLE(n, m, c) {\t\t\t\t\\\n\tssize_t ssv2;\t\t\t\t\t\t\t\\\n\tif (je_mallctl(\"opt.\"#n, (void *)&ssv, &sssz, NULL, 0) == 0 &&\t\\\n\t    je_mallctl(#m, (void *)&ssv2, &sssz, NULL, 0) == 0) {\t\\\n\t\tif (json) {\t\t\t\t\t\t\\\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\t\t\\\n\t\t\t    \"\\t\\t\\t\\\"\"#n\"\\\": %zd%s\\n\", ssv, (c));\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\t\t\\\n\t\t\t    \"  opt.\"#n\": %zd (\"#m\": %zd)\\n\",\t\t\\\n\t\t\t    ssv, ssv2);\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n}\n#define OPT_WRITE_CHAR_P(n, c)\t\t\t\t\t\t\\\n\tif (je_mallctl(\"opt.\"#n, (void *)&cpv, &cpsz, NULL, 0) == 0) {\t\\\n\t\tif (json) {\t\t\t\t\t\t\\\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\t\t\\\n\t\t\t    \"\\t\\t\\t\\\"\"#n\"\\\": \\\"%s\\\"%s\\n\", cpv, (c));\t\\\n\t\t} else {\t\t\t\t\t\t\\\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\t\t\\\n\t\t\t    \"  opt.\"#n\": \\\"%s\\\"\\n\", cpv);\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\n\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\\"opt\\\": {\\n\");\n\t} else {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"Run-time option settings:\\n\");\n\t}\n\tOPT_WRITE_BOOL(abort, \",\")\n\tOPT_WRITE_BOOL(abort_conf, \",\")\n\tOPT_WRITE_BOOL(retain, \",\")\n\tOPT_WRITE_CHAR_P(dss, \",\")\n\tOPT_WRITE_UNSIGNED(narenas, \",\")\n\tOPT_WRITE_CHAR_P(percpu_arena, \",\")\n\tOPT_WRITE_BOOL_MUTABLE(background_thread, background_thread, \",\")\n\tOPT_WRITE_SSIZE_T_MUTABLE(dirty_decay_ms, arenas.dirty_decay_ms, \",\")\n\tOPT_WRITE_SSIZE_T_MUTABLE(muzzy_decay_ms, arenas.muzzy_decay_ms, \",\")\n\tOPT_WRITE_CHAR_P(junk, \",\")\n\tOPT_WRITE_BOOL(zero, \",\")\n\tOPT_WRITE_BOOL(utrace, \",\")\n\tOPT_WRITE_BOOL(xmalloc, \",\")\n\tOPT_WRITE_BOOL(tcache, \",\")\n\tOPT_WRITE_SSIZE_T(lg_tcache_max, \",\")\n\tOPT_WRITE_BOOL(prof, \",\")\n\tOPT_WRITE_CHAR_P(prof_prefix, \",\")\n\tOPT_WRITE_BOOL_MUTABLE(prof_active, prof.active, \",\")\n\tOPT_WRITE_BOOL_MUTABLE(prof_thread_active_init, prof.thread_active_init,\n\t    \",\")\n\tOPT_WRITE_SSIZE_T_MUTABLE(lg_prof_sample, prof.lg_sample, \",\")\n\tOPT_WRITE_BOOL(prof_accum, \",\")\n\tOPT_WRITE_SSIZE_T(lg_prof_interval, \",\")\n\tOPT_WRITE_BOOL(prof_gdump, \",\")\n\tOPT_WRITE_BOOL(prof_final, \",\")\n\tOPT_WRITE_BOOL(prof_leak, \",\")\n\tOPT_WRITE_BOOL(stats_print, \",\")\n\tif (json || opt_stats_print) {\n\t\t/*\n\t\t * stats_print_opts is always emitted for JSON, so as long as it\n\t\t * comes last it's safe to unconditionally omit the comma here\n\t\t * (rather than having to conditionally omit it elsewhere\n\t\t * depending on configuration).\n\t\t */\n\t\tOPT_WRITE_CHAR_P(stats_print_opts, \"\")\n\t}\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t},\\n\");\n\t}\n\n#undef OPT_WRITE_BOOL\n#undef OPT_WRITE_BOOL_MUTABLE\n#undef OPT_WRITE_SSIZE_T\n#undef OPT_WRITE_CHAR_P\n\n\t/* arenas. */\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\\"arenas\\\": {\\n\");\n\t}\n\n\tCTL_GET(\"arenas.narenas\", &uv, unsigned);\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\\"narenas\\\": %u,\\n\", uv);\n\t} else {\n\t\tmalloc_cprintf(write_cb, cbopaque, \"Arenas: %u\\n\", uv);\n\t}\n\n\tif (json) {\n\t\tCTL_GET(\"arenas.dirty_decay_ms\", &ssv, ssize_t);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\\"dirty_decay_ms\\\": %zd,\\n\", ssv);\n\n\t\tCTL_GET(\"arenas.muzzy_decay_ms\", &ssv, ssize_t);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\\"muzzy_decay_ms\\\": %zd,\\n\", ssv);\n\t}\n\n\tCTL_GET(\"arenas.quantum\", &sv, size_t);\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\\"quantum\\\": %zu,\\n\", sv);\n\t} else {\n\t\tmalloc_cprintf(write_cb, cbopaque, \"Quantum size: %zu\\n\", sv);\n\t}\n\n\tCTL_GET(\"arenas.page\", &sv, size_t);\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\\"page\\\": %zu,\\n\", sv);\n\t} else {\n\t\tmalloc_cprintf(write_cb, cbopaque, \"Page size: %zu\\n\", sv);\n\t}\n\n\tif (je_mallctl(\"arenas.tcache_max\", (void *)&sv, &ssz, NULL, 0) == 0) {\n\t\tif (json) {\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t    \"\\t\\t\\t\\\"tcache_max\\\": %zu,\\n\", sv);\n\t\t} else {\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t    \"Maximum thread-cached size class: %zu\\n\", sv);\n\t\t}\n\t}\n\n\tif (json) {\n\t\tunsigned nbins, nlextents, i;\n\n\t\tCTL_GET(\"arenas.nbins\", &nbins, unsigned);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\\"nbins\\\": %u,\\n\", nbins);\n\n\t\tCTL_GET(\"arenas.nhbins\", &uv, unsigned);\n\t\tmalloc_cprintf(write_cb, cbopaque, \"\\t\\t\\t\\\"nhbins\\\": %u,\\n\",\n\t\t    uv);\n\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\\"bin\\\": [\\n\");\n\t\tfor (i = 0; i < nbins; i++) {\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t    \"\\t\\t\\t\\t{\\n\");\n\n\t\t\tCTL_M2_GET(\"arenas.bin.0.size\", i, &sv, size_t);\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t    \"\\t\\t\\t\\t\\t\\\"size\\\": %zu,\\n\", sv);\n\n\t\t\tCTL_M2_GET(\"arenas.bin.0.nregs\", i, &u32v, uint32_t);\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t    \"\\t\\t\\t\\t\\t\\\"nregs\\\": %\"FMTu32\",\\n\", u32v);\n\n\t\t\tCTL_M2_GET(\"arenas.bin.0.slab_size\", i, &sv, size_t);\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t    \"\\t\\t\\t\\t\\t\\\"slab_size\\\": %zu\\n\", sv);\n\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t    \"\\t\\t\\t\\t}%s\\n\", (i + 1 < nbins) ? \",\" : \"\");\n\t\t}\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t],\\n\");\n\n\t\tCTL_GET(\"arenas.nlextents\", &nlextents, unsigned);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\\"nlextents\\\": %u,\\n\", nlextents);\n\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\\"lextent\\\": [\\n\");\n\t\tfor (i = 0; i < nlextents; i++) {\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t    \"\\t\\t\\t\\t{\\n\");\n\n\t\t\tCTL_M2_GET(\"arenas.lextent.0.size\", i, &sv, size_t);\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t    \"\\t\\t\\t\\t\\t\\\"size\\\": %zu\\n\", sv);\n\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t    \"\\t\\t\\t\\t}%s\\n\", (i + 1 < nlextents) ? \",\" : \"\");\n\t\t}\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t]\\n\");\n\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t}%s\\n\", (config_prof || more) ? \",\" : \"\");\n\t}\n\n\t/* prof. */\n\tif (config_prof && json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\\"prof\\\": {\\n\");\n\n\t\tCTL_GET(\"prof.thread_active_init\", &bv, bool);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\\"thread_active_init\\\": %s,\\n\", bv ? \"true\" :\n\t\t    \"false\");\n\n\t\tCTL_GET(\"prof.active\", &bv, bool);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\\"active\\\": %s,\\n\", bv ? \"true\" : \"false\");\n\n\t\tCTL_GET(\"prof.gdump\", &bv, bool);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\\"gdump\\\": %s,\\n\", bv ? \"true\" : \"false\");\n\n\t\tCTL_GET(\"prof.interval\", &u64v, uint64_t);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\\"interval\\\": %\"FMTu64\",\\n\", u64v);\n\n\t\tCTL_GET(\"prof.lg_sample\", &ssv, ssize_t);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\\"lg_sample\\\": %zd\\n\", ssv);\n\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t}%s\\n\", more ? \",\" : \"\");\n\t}\n}\n\nstatic void\nread_global_mutex_stats(\n    uint64_t results[mutex_prof_num_global_mutexes][mutex_prof_num_counters]) {\n\tchar cmd[MUTEX_CTL_STR_MAX_LENGTH];\n\n\tmutex_prof_global_ind_t i;\n\tfor (i = 0; i < mutex_prof_num_global_mutexes; i++) {\n#define OP(c, t)\t\t\t\t\t\t\t\\\n\t\tgen_mutex_ctl_str(cmd, MUTEX_CTL_STR_MAX_LENGTH,\t\\\n\t\t    \"mutexes\", global_mutex_names[i], #c);\t\t\\\n\t\tCTL_GET(cmd, (t *)&results[i][mutex_counter_##c], t);\nMUTEX_PROF_COUNTERS\n#undef OP\n\t}\n}\n\nstatic void\nstats_print_helper(void (*write_cb)(void *, const char *), void *cbopaque,\n    bool json, bool merged, bool destroyed, bool unmerged, bool bins,\n    bool large, bool mutex) {\n\tsize_t allocated, active, metadata, resident, mapped, retained;\n\tsize_t num_background_threads;\n\tuint64_t background_thread_num_runs, background_thread_run_interval;\n\n\tCTL_GET(\"stats.allocated\", &allocated, size_t);\n\tCTL_GET(\"stats.active\", &active, size_t);\n\tCTL_GET(\"stats.metadata\", &metadata, size_t);\n\tCTL_GET(\"stats.resident\", &resident, size_t);\n\tCTL_GET(\"stats.mapped\", &mapped, size_t);\n\tCTL_GET(\"stats.retained\", &retained, size_t);\n\n\tuint64_t mutex_stats[mutex_prof_num_global_mutexes][mutex_prof_num_counters];\n\tif (mutex) {\n\t\tread_global_mutex_stats(mutex_stats);\n\t}\n\n\tif (have_background_thread) {\n\t\tCTL_GET(\"stats.background_thread.num_threads\",\n\t\t    &num_background_threads, size_t);\n\t\tCTL_GET(\"stats.background_thread.num_runs\",\n\t\t    &background_thread_num_runs, uint64_t);\n\t\tCTL_GET(\"stats.background_thread.run_interval\",\n\t\t    &background_thread_run_interval, uint64_t);\n\t} else {\n\t\tnum_background_threads = 0;\n\t\tbackground_thread_num_runs = 0;\n\t\tbackground_thread_run_interval = 0;\n\t}\n\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\\"stats\\\": {\\n\");\n\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\\"allocated\\\": %zu,\\n\", allocated);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\\"active\\\": %zu,\\n\", active);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\\"metadata\\\": %zu,\\n\", metadata);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\\"resident\\\": %zu,\\n\", resident);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\\"mapped\\\": %zu,\\n\", mapped);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\\"retained\\\": %zu,\\n\", retained);\n\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\\"background_thread\\\": {\\n\");\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"num_threads\\\": %zu,\\n\", num_background_threads);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"num_runs\\\": %\"FMTu64\",\\n\",\n\t\t    background_thread_num_runs);\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t\\t\\t\\\"run_interval\\\": %\"FMTu64\"\\n\",\n\t\t    background_thread_run_interval);\n\t\tmalloc_cprintf(write_cb, cbopaque, \"\\t\\t\\t}%s\\n\",\n\t\t    mutex ? \",\" : \"\");\n\n\t\tif (mutex) {\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t    \"\\t\\t\\t\\\"mutexes\\\": {\\n\");\n\t\t\tmutex_prof_global_ind_t i;\n\t\t\tfor (i = 0; i < mutex_prof_num_global_mutexes; i++) {\n\t\t\t\tmutex_stats_output_json(write_cb, cbopaque,\n\t\t\t\t    global_mutex_names[i], mutex_stats[i],\n\t\t\t\t    \"\\t\\t\\t\\t\",\n\t\t\t\t    i == mutex_prof_num_global_mutexes - 1);\n\t\t\t}\n\t\t\tmalloc_cprintf(write_cb, cbopaque, \"\\t\\t\\t}\\n\");\n\t\t}\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t\\t}%s\\n\", (merged || unmerged || destroyed) ? \",\" : \"\");\n\t} else {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"Allocated: %zu, active: %zu, metadata: %zu,\"\n\t\t    \" resident: %zu, mapped: %zu, retained: %zu\\n\",\n\t\t    allocated, active, metadata, resident, mapped, retained);\n\n\t\tif (have_background_thread && num_background_threads > 0) {\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t    \"Background threads: %zu, num_runs: %\"FMTu64\", \"\n\t\t\t    \"run_interval: %\"FMTu64\" ns\\n\",\n\t\t\t    num_background_threads,\n\t\t\t    background_thread_num_runs,\n\t\t\t    background_thread_run_interval);\n\t\t}\n\t\tif (mutex) {\n\t\t\tmutex_prof_global_ind_t i;\n\t\t\tfor (i = 0; i < mutex_prof_num_global_mutexes; i++) {\n\t\t\t\tmutex_stats_output(write_cb, cbopaque,\n\t\t\t\t    global_mutex_names[i], mutex_stats[i],\n\t\t\t\t    i == 0);\n\t\t\t}\n\t\t}\n\t}\n\n\tif (merged || destroyed || unmerged) {\n\t\tunsigned narenas;\n\n\t\tif (json) {\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t    \"\\t\\t\\\"stats.arenas\\\": {\\n\");\n\t\t}\n\n\t\tCTL_GET(\"arenas.narenas\", &narenas, unsigned);\n\t\t{\n\t\t\tsize_t mib[3];\n\t\t\tsize_t miblen = sizeof(mib) / sizeof(size_t);\n\t\t\tsize_t sz;\n\t\t\tVARIABLE_ARRAY(bool, initialized, narenas);\n\t\t\tbool destroyed_initialized;\n\t\t\tunsigned i, j, ninitialized;\n\n\t\t\txmallctlnametomib(\"arena.0.initialized\", mib, &miblen);\n\t\t\tfor (i = ninitialized = 0; i < narenas; i++) {\n\t\t\t\tmib[1] = i;\n\t\t\t\tsz = sizeof(bool);\n\t\t\t\txmallctlbymib(mib, miblen, &initialized[i], &sz,\n\t\t\t\t    NULL, 0);\n\t\t\t\tif (initialized[i]) {\n\t\t\t\t\tninitialized++;\n\t\t\t\t}\n\t\t\t}\n\t\t\tmib[1] = MALLCTL_ARENAS_DESTROYED;\n\t\t\tsz = sizeof(bool);\n\t\t\txmallctlbymib(mib, miblen, &destroyed_initialized, &sz,\n\t\t\t    NULL, 0);\n\n\t\t\t/* Merged stats. */\n\t\t\tif (merged && (ninitialized > 1 || !unmerged)) {\n\t\t\t\t/* Print merged arena stats. */\n\t\t\t\tif (json) {\n\t\t\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t\t\t    \"\\t\\t\\t\\\"merged\\\": {\\n\");\n\t\t\t\t} else {\n\t\t\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t\t\t    \"\\nMerged arenas stats:\\n\");\n\t\t\t\t}\n\t\t\t\tstats_arena_print(write_cb, cbopaque, json,\n\t\t\t\t    MALLCTL_ARENAS_ALL, bins, large, mutex);\n\t\t\t\tif (json) {\n\t\t\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t\t\t    \"\\t\\t\\t}%s\\n\",\n\t\t\t\t\t    ((destroyed_initialized &&\n\t\t\t\t\t    destroyed) || unmerged) ?  \",\" :\n\t\t\t\t\t    \"\");\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t/* Destroyed stats. */\n\t\t\tif (destroyed_initialized && destroyed) {\n\t\t\t\t/* Print destroyed arena stats. */\n\t\t\t\tif (json) {\n\t\t\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t\t\t    \"\\t\\t\\t\\\"destroyed\\\": {\\n\");\n\t\t\t\t} else {\n\t\t\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t\t\t    \"\\nDestroyed arenas stats:\\n\");\n\t\t\t\t}\n\t\t\t\tstats_arena_print(write_cb, cbopaque, json,\n\t\t\t\t    MALLCTL_ARENAS_DESTROYED, bins, large,\n\t\t\t\t    mutex);\n\t\t\t\tif (json) {\n\t\t\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t\t\t    \"\\t\\t\\t}%s\\n\", unmerged ?  \",\" :\n\t\t\t\t\t    \"\");\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t/* Unmerged stats. */\n\t\t\tif (unmerged) {\n\t\t\t\tfor (i = j = 0; i < narenas; i++) {\n\t\t\t\t\tif (initialized[i]) {\n\t\t\t\t\t\tif (json) {\n\t\t\t\t\t\t\tj++;\n\t\t\t\t\t\t\tmalloc_cprintf(write_cb,\n\t\t\t\t\t\t\t    cbopaque,\n\t\t\t\t\t\t\t    \"\\t\\t\\t\\\"%u\\\": {\\n\",\n\t\t\t\t\t\t\t    i);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tmalloc_cprintf(write_cb,\n\t\t\t\t\t\t\t    cbopaque,\n\t\t\t\t\t\t\t    \"\\narenas[%u]:\\n\",\n\t\t\t\t\t\t\t    i);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tstats_arena_print(write_cb,\n\t\t\t\t\t\t    cbopaque, json, i, bins,\n\t\t\t\t\t\t    large, mutex);\n\t\t\t\t\t\tif (json) {\n\t\t\t\t\t\t\tmalloc_cprintf(write_cb,\n\t\t\t\t\t\t\t    cbopaque,\n\t\t\t\t\t\t\t    \"\\t\\t\\t}%s\\n\", (j <\n\t\t\t\t\t\t\t    ninitialized) ? \",\"\n\t\t\t\t\t\t\t    : \"\");\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (json) {\n\t\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t\t    \"\\t\\t}\\n\");\n\t\t}\n\t}\n}\n\nvoid\nstats_print(void (*write_cb)(void *, const char *), void *cbopaque,\n    const char *opts) {\n\tint err;\n\tuint64_t epoch;\n\tsize_t u64sz;\n#define OPTION(o, v, d, s) bool v = d;\n\tSTATS_PRINT_OPTIONS\n#undef OPTION\n\n\t/*\n\t * Refresh stats, in case mallctl() was called by the application.\n\t *\n\t * Check for OOM here, since refreshing the ctl cache can trigger\n\t * allocation.  In practice, none of the subsequent mallctl()-related\n\t * calls in this function will cause OOM if this one succeeds.\n\t * */\n\tepoch = 1;\n\tu64sz = sizeof(uint64_t);\n\terr = je_mallctl(\"epoch\", (void *)&epoch, &u64sz, (void *)&epoch,\n\t    sizeof(uint64_t));\n\tif (err != 0) {\n\t\tif (err == EAGAIN) {\n\t\t\tmalloc_write(\"<jemalloc>: Memory allocation failure in \"\n\t\t\t    \"mallctl(\\\"epoch\\\", ...)\\n\");\n\t\t\treturn;\n\t\t}\n\t\tmalloc_write(\"<jemalloc>: Failure in mallctl(\\\"epoch\\\", \"\n\t\t    \"...)\\n\");\n\t\tabort();\n\t}\n\n\tif (opts != NULL) {\n\t\tfor (unsigned i = 0; opts[i] != '\\0'; i++) {\n\t\t\tswitch (opts[i]) {\n#define OPTION(o, v, d, s) case o: v = s; break;\n\t\t\t\tSTATS_PRINT_OPTIONS\n#undef OPTION\n\t\t\tdefault:;\n\t\t\t}\n\t\t}\n\t}\n\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"{\\n\"\n\t\t    \"\\t\\\"jemalloc\\\": {\\n\");\n\t} else {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"___ Begin jemalloc statistics ___\\n\");\n\t}\n\n\tif (general) {\n\t\tstats_general_print(write_cb, cbopaque, json, config_stats);\n\t}\n\tif (config_stats) {\n\t\tstats_print_helper(write_cb, cbopaque, json, merged, destroyed,\n\t\t    unmerged, bins, large, mutex);\n\t}\n\n\tif (json) {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"\\t}\\n\"\n\t\t    \"}\\n\");\n\t} else {\n\t\tmalloc_cprintf(write_cb, cbopaque,\n\t\t    \"--- End jemalloc statistics ---\\n\");\n\t}\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/sz.c",
    "content": "#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/sz.h\"\n\nJEMALLOC_ALIGNED(CACHELINE)\nconst size_t sz_pind2sz_tab[NPSIZES+1] = {\n#define PSZ_yes(lg_grp, ndelta, lg_delta)\t\t\t\t\\\n\t(((ZU(1)<<lg_grp) + (ZU(ndelta)<<lg_delta))),\n#define PSZ_no(lg_grp, ndelta, lg_delta)\n#define SC(index, lg_grp, lg_delta, ndelta, psz, bin, pgs, lg_delta_lookup) \\\n\tPSZ_##psz(lg_grp, ndelta, lg_delta)\n\tSIZE_CLASSES\n#undef PSZ_yes\n#undef PSZ_no\n#undef SC\n\t(LARGE_MAXCLASS + PAGE)\n};\n\nJEMALLOC_ALIGNED(CACHELINE)\nconst size_t sz_index2size_tab[NSIZES] = {\n#define SC(index, lg_grp, lg_delta, ndelta, psz, bin, pgs, lg_delta_lookup) \\\n\t((ZU(1)<<lg_grp) + (ZU(ndelta)<<lg_delta)),\n\tSIZE_CLASSES\n#undef SC\n};\n\nJEMALLOC_ALIGNED(CACHELINE)\nconst uint8_t sz_size2index_tab[] = {\n#if LG_TINY_MIN == 0\n#warning \"Dangerous LG_TINY_MIN\"\n#define S2B_0(i)\ti,\n#elif LG_TINY_MIN == 1\n#warning \"Dangerous LG_TINY_MIN\"\n#define S2B_1(i)\ti,\n#elif LG_TINY_MIN == 2\n#warning \"Dangerous LG_TINY_MIN\"\n#define S2B_2(i)\ti,\n#elif LG_TINY_MIN == 3\n#define S2B_3(i)\ti,\n#elif LG_TINY_MIN == 4\n#define S2B_4(i)\ti,\n#elif LG_TINY_MIN == 5\n#define S2B_5(i)\ti,\n#elif LG_TINY_MIN == 6\n#define S2B_6(i)\ti,\n#elif LG_TINY_MIN == 7\n#define S2B_7(i)\ti,\n#elif LG_TINY_MIN == 8\n#define S2B_8(i)\ti,\n#elif LG_TINY_MIN == 9\n#define S2B_9(i)\ti,\n#elif LG_TINY_MIN == 10\n#define S2B_10(i)\ti,\n#elif LG_TINY_MIN == 11\n#define S2B_11(i)\ti,\n#else\n#error \"Unsupported LG_TINY_MIN\"\n#endif\n#if LG_TINY_MIN < 1\n#define S2B_1(i)\tS2B_0(i) S2B_0(i)\n#endif\n#if LG_TINY_MIN < 2\n#define S2B_2(i)\tS2B_1(i) S2B_1(i)\n#endif\n#if LG_TINY_MIN < 3\n#define S2B_3(i)\tS2B_2(i) S2B_2(i)\n#endif\n#if LG_TINY_MIN < 4\n#define S2B_4(i)\tS2B_3(i) S2B_3(i)\n#endif\n#if LG_TINY_MIN < 5\n#define S2B_5(i)\tS2B_4(i) S2B_4(i)\n#endif\n#if LG_TINY_MIN < 6\n#define S2B_6(i)\tS2B_5(i) S2B_5(i)\n#endif\n#if LG_TINY_MIN < 7\n#define S2B_7(i)\tS2B_6(i) S2B_6(i)\n#endif\n#if LG_TINY_MIN < 8\n#define S2B_8(i)\tS2B_7(i) S2B_7(i)\n#endif\n#if LG_TINY_MIN < 9\n#define S2B_9(i)\tS2B_8(i) S2B_8(i)\n#endif\n#if LG_TINY_MIN < 10\n#define S2B_10(i)\tS2B_9(i) S2B_9(i)\n#endif\n#if LG_TINY_MIN < 11\n#define S2B_11(i)\tS2B_10(i) S2B_10(i)\n#endif\n#define S2B_no(i)\n#define SC(index, lg_grp, lg_delta, ndelta, psz, bin, pgs, lg_delta_lookup) \\\n\tS2B_##lg_delta_lookup(index)\n\tSIZE_CLASSES\n#undef S2B_3\n#undef S2B_4\n#undef S2B_5\n#undef S2B_6\n#undef S2B_7\n#undef S2B_8\n#undef S2B_9\n#undef S2B_10\n#undef S2B_11\n#undef S2B_no\n#undef SC\n};\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/tcache.c",
    "content": "#define JEMALLOC_TCACHE_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/size_classes.h\"\n\n/******************************************************************************/\n/* Data. */\n\nbool\topt_tcache = true;\nssize_t\topt_lg_tcache_max = LG_TCACHE_MAXCLASS_DEFAULT;\n\ntcache_bin_info_t\t*tcache_bin_info;\nstatic unsigned\t\tstack_nelms; /* Total stack elms per tcache. */\n\nunsigned\t\tnhbins;\nsize_t\t\t\ttcache_maxclass;\n\ntcaches_t\t\t*tcaches;\n\n/* Index of first element within tcaches that has never been used. */\nstatic unsigned\t\ttcaches_past;\n\n/* Head of singly linked list tracking available tcaches elements. */\nstatic tcaches_t\t*tcaches_avail;\n\n/* Protects tcaches{,_past,_avail}. */\nstatic malloc_mutex_t\ttcaches_mtx;\n\n/******************************************************************************/\n\nsize_t\ntcache_salloc(tsdn_t *tsdn, const void *ptr) {\n\treturn arena_salloc(tsdn, ptr);\n}\n\nvoid\ntcache_event_hard(tsd_t *tsd, tcache_t *tcache) {\n\tszind_t binind = tcache->next_gc_bin;\n\n\ttcache_bin_t *tbin;\n\tif (binind < NBINS) {\n\t\ttbin = tcache_small_bin_get(tcache, binind);\n\t} else {\n\t\ttbin = tcache_large_bin_get(tcache, binind);\n\t}\n\tif (tbin->low_water > 0) {\n\t\t/*\n\t\t * Flush (ceiling) 3/4 of the objects below the low water mark.\n\t\t */\n\t\tif (binind < NBINS) {\n\t\t\ttcache_bin_flush_small(tsd, tcache, tbin, binind,\n\t\t\t    tbin->ncached - tbin->low_water + (tbin->low_water\n\t\t\t    >> 2));\n\t\t\t/*\n\t\t\t * Reduce fill count by 2X.  Limit lg_fill_div such that\n\t\t\t * the fill count is always at least 1.\n\t\t\t */\n\t\t\ttcache_bin_info_t *tbin_info = &tcache_bin_info[binind];\n\t\t\tif ((tbin_info->ncached_max >>\n\t\t\t     (tcache->lg_fill_div[binind] + 1)) >= 1) {\n\t\t\t\ttcache->lg_fill_div[binind]++;\n\t\t\t}\n\t\t} else {\n\t\t\ttcache_bin_flush_large(tsd, tbin, binind, tbin->ncached\n\t\t\t    - tbin->low_water + (tbin->low_water >> 2), tcache);\n\t\t}\n\t} else if (tbin->low_water < 0) {\n\t\t/*\n\t\t * Increase fill count by 2X for small bins.  Make sure\n\t\t * lg_fill_div stays greater than 0.\n\t\t */\n\t\tif (binind < NBINS && tcache->lg_fill_div[binind] > 1) {\n\t\t\ttcache->lg_fill_div[binind]--;\n\t\t}\n\t}\n\ttbin->low_water = tbin->ncached;\n\n\ttcache->next_gc_bin++;\n\tif (tcache->next_gc_bin == nhbins) {\n\t\ttcache->next_gc_bin = 0;\n\t}\n}\n\nvoid *\ntcache_alloc_small_hard(tsdn_t *tsdn, arena_t *arena, tcache_t *tcache,\n    tcache_bin_t *tbin, szind_t binind, bool *tcache_success) {\n\tvoid *ret;\n\n\tassert(tcache->arena != NULL);\n\tarena_tcache_fill_small(tsdn, arena, tcache, tbin, binind,\n\t    config_prof ? tcache->prof_accumbytes : 0);\n\tif (config_prof) {\n\t\ttcache->prof_accumbytes = 0;\n\t}\n\tret = tcache_alloc_easy(tbin, tcache_success);\n\n\treturn ret;\n}\n\nvoid\ntcache_bin_flush_small(tsd_t *tsd, tcache_t *tcache, tcache_bin_t *tbin,\n    szind_t binind, unsigned rem) {\n\tbool merged_stats = false;\n\n\tassert(binind < NBINS);\n\tassert(rem <= tbin->ncached);\n\n\tarena_t *arena = tcache->arena;\n\tassert(arena != NULL);\n\tunsigned nflush = tbin->ncached - rem;\n\tVARIABLE_ARRAY(extent_t *, item_extent, nflush);\n\t/* Look up extent once per item. */\n\tfor (unsigned i = 0 ; i < nflush; i++) {\n\t\titem_extent[i] = iealloc(tsd_tsdn(tsd), *(tbin->avail - 1 - i));\n\t}\n\n\twhile (nflush > 0) {\n\t\t/* Lock the arena bin associated with the first object. */\n\t\textent_t *extent = item_extent[0];\n\t\tarena_t *bin_arena = extent_arena_get(extent);\n\t\tarena_bin_t *bin = &bin_arena->bins[binind];\n\n\t\tif (config_prof && bin_arena == arena) {\n\t\t\tif (arena_prof_accum(tsd_tsdn(tsd), arena,\n\t\t\t    tcache->prof_accumbytes)) {\n\t\t\t\tprof_idump(tsd_tsdn(tsd));\n\t\t\t}\n\t\t\ttcache->prof_accumbytes = 0;\n\t\t}\n\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &bin->lock);\n\t\tif (config_stats && bin_arena == arena) {\n\t\t\tassert(!merged_stats);\n\t\t\tmerged_stats = true;\n\t\t\tbin->stats.nflushes++;\n\t\t\tbin->stats.nrequests += tbin->tstats.nrequests;\n\t\t\ttbin->tstats.nrequests = 0;\n\t\t}\n\t\tunsigned ndeferred = 0;\n\t\tfor (unsigned i = 0; i < nflush; i++) {\n\t\t\tvoid *ptr = *(tbin->avail - 1 - i);\n\t\t\textent = item_extent[i];\n\t\t\tassert(ptr != NULL && extent != NULL);\n\n\t\t\tif (extent_arena_get(extent) == bin_arena) {\n\t\t\t\tarena_dalloc_bin_junked_locked(tsd_tsdn(tsd),\n\t\t\t\t    bin_arena, extent, ptr);\n\t\t\t} else {\n\t\t\t\t/*\n\t\t\t\t * This object was allocated via a different\n\t\t\t\t * arena bin than the one that is currently\n\t\t\t\t * locked.  Stash the object, so that it can be\n\t\t\t\t * handled in a future pass.\n\t\t\t\t */\n\t\t\t\t*(tbin->avail - 1 - ndeferred) = ptr;\n\t\t\t\titem_extent[ndeferred] = extent;\n\t\t\t\tndeferred++;\n\t\t\t}\n\t\t}\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &bin->lock);\n\t\tarena_decay_ticks(tsd_tsdn(tsd), bin_arena, nflush - ndeferred);\n\t\tnflush = ndeferred;\n\t}\n\tif (config_stats && !merged_stats) {\n\t\t/*\n\t\t * The flush loop didn't happen to flush to this thread's\n\t\t * arena, so the stats didn't get merged.  Manually do so now.\n\t\t */\n\t\tarena_bin_t *bin = &arena->bins[binind];\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &bin->lock);\n\t\tbin->stats.nflushes++;\n\t\tbin->stats.nrequests += tbin->tstats.nrequests;\n\t\ttbin->tstats.nrequests = 0;\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &bin->lock);\n\t}\n\n\tmemmove(tbin->avail - rem, tbin->avail - tbin->ncached, rem *\n\t    sizeof(void *));\n\ttbin->ncached = rem;\n\tif ((low_water_t)tbin->ncached < tbin->low_water) {\n\t\ttbin->low_water = tbin->ncached;\n\t}\n}\n\nvoid\ntcache_bin_flush_large(tsd_t *tsd, tcache_bin_t *tbin, szind_t binind,\n    unsigned rem, tcache_t *tcache) {\n\tbool merged_stats = false;\n\n\tassert(binind < nhbins);\n\tassert(rem <= tbin->ncached);\n\n\tarena_t *arena = tcache->arena;\n\tassert(arena != NULL);\n\tunsigned nflush = tbin->ncached - rem;\n\tVARIABLE_ARRAY(extent_t *, item_extent, nflush);\n\t/* Look up extent once per item. */\n\tfor (unsigned i = 0 ; i < nflush; i++) {\n\t\titem_extent[i] = iealloc(tsd_tsdn(tsd), *(tbin->avail - 1 - i));\n\t}\n\n\twhile (nflush > 0) {\n\t\t/* Lock the arena associated with the first object. */\n\t\textent_t *extent = item_extent[0];\n\t\tarena_t *locked_arena = extent_arena_get(extent);\n\t\tUNUSED bool idump;\n\n\t\tif (config_prof) {\n\t\t\tidump = false;\n\t\t}\n\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &locked_arena->large_mtx);\n\t\tfor (unsigned i = 0; i < nflush; i++) {\n\t\t\tvoid *ptr = *(tbin->avail - 1 - i);\n\t\t\tassert(ptr != NULL);\n\t\t\textent = item_extent[i];\n\t\t\tif (extent_arena_get(extent) == locked_arena) {\n\t\t\t\tlarge_dalloc_prep_junked_locked(tsd_tsdn(tsd),\n\t\t\t\t    extent);\n\t\t\t}\n\t\t}\n\t\tif ((config_prof || config_stats) && locked_arena == arena) {\n\t\t\tif (config_prof) {\n\t\t\t\tidump = arena_prof_accum(tsd_tsdn(tsd), arena,\n\t\t\t\t    tcache->prof_accumbytes);\n\t\t\t\ttcache->prof_accumbytes = 0;\n\t\t\t}\n\t\t\tif (config_stats) {\n\t\t\t\tmerged_stats = true;\n\t\t\t\tarena_stats_large_nrequests_add(tsd_tsdn(tsd),\n\t\t\t\t    &arena->stats, binind,\n\t\t\t\t    tbin->tstats.nrequests);\n\t\t\t\ttbin->tstats.nrequests = 0;\n\t\t\t}\n\t\t}\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &locked_arena->large_mtx);\n\n\t\tunsigned ndeferred = 0;\n\t\tfor (unsigned i = 0; i < nflush; i++) {\n\t\t\tvoid *ptr = *(tbin->avail - 1 - i);\n\t\t\textent = item_extent[i];\n\t\t\tassert(ptr != NULL && extent != NULL);\n\n\t\t\tif (extent_arena_get(extent) == locked_arena) {\n\t\t\t\tlarge_dalloc_finish(tsd_tsdn(tsd), extent);\n\t\t\t} else {\n\t\t\t\t/*\n\t\t\t\t * This object was allocated via a different\n\t\t\t\t * arena than the one that is currently locked.\n\t\t\t\t * Stash the object, so that it can be handled\n\t\t\t\t * in a future pass.\n\t\t\t\t */\n\t\t\t\t*(tbin->avail - 1 - ndeferred) = ptr;\n\t\t\t\titem_extent[ndeferred] = extent;\n\t\t\t\tndeferred++;\n\t\t\t}\n\t\t}\n\t\tif (config_prof && idump) {\n\t\t\tprof_idump(tsd_tsdn(tsd));\n\t\t}\n\t\tarena_decay_ticks(tsd_tsdn(tsd), locked_arena, nflush -\n\t\t    ndeferred);\n\t\tnflush = ndeferred;\n\t}\n\tif (config_stats && !merged_stats) {\n\t\t/*\n\t\t * The flush loop didn't happen to flush to this thread's\n\t\t * arena, so the stats didn't get merged.  Manually do so now.\n\t\t */\n\t\tarena_stats_large_nrequests_add(tsd_tsdn(tsd), &arena->stats,\n\t\t    binind, tbin->tstats.nrequests);\n\t\ttbin->tstats.nrequests = 0;\n\t}\n\n\tmemmove(tbin->avail - rem, tbin->avail - tbin->ncached, rem *\n\t    sizeof(void *));\n\ttbin->ncached = rem;\n\tif ((low_water_t)tbin->ncached < tbin->low_water) {\n\t\ttbin->low_water = tbin->ncached;\n\t}\n}\n\nvoid\ntcache_arena_associate(tsdn_t *tsdn, tcache_t *tcache, arena_t *arena) {\n\tassert(tcache->arena == NULL);\n\ttcache->arena = arena;\n\n\tif (config_stats) {\n\t\t/* Link into list of extant tcaches. */\n\t\tmalloc_mutex_lock(tsdn, &arena->tcache_ql_mtx);\n\t\tql_elm_new(tcache, link);\n\t\tql_tail_insert(&arena->tcache_ql, tcache, link);\n\t\tmalloc_mutex_unlock(tsdn, &arena->tcache_ql_mtx);\n\t}\n}\n\nstatic void\ntcache_arena_dissociate(tsdn_t *tsdn, tcache_t *tcache) {\n\tarena_t *arena = tcache->arena;\n\tassert(arena != NULL);\n\tif (config_stats) {\n\t\t/* Unlink from list of extant tcaches. */\n\t\tmalloc_mutex_lock(tsdn, &arena->tcache_ql_mtx);\n\t\tif (config_debug) {\n\t\t\tbool in_ql = false;\n\t\t\ttcache_t *iter;\n\t\t\tql_foreach(iter, &arena->tcache_ql, link) {\n\t\t\t\tif (iter == tcache) {\n\t\t\t\t\tin_ql = true;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tassert(in_ql);\n\t\t}\n\t\tql_remove(&arena->tcache_ql, tcache, link);\n\t\ttcache_stats_merge(tsdn, tcache, arena);\n\t\tmalloc_mutex_unlock(tsdn, &arena->tcache_ql_mtx);\n\t}\n\ttcache->arena = NULL;\n}\n\nvoid\ntcache_arena_reassociate(tsdn_t *tsdn, tcache_t *tcache, arena_t *arena) {\n\ttcache_arena_dissociate(tsdn, tcache);\n\ttcache_arena_associate(tsdn, tcache, arena);\n}\n\nbool\ntsd_tcache_enabled_data_init(tsd_t *tsd) {\n\t/* Called upon tsd initialization. */\n\ttsd_tcache_enabled_set(tsd, opt_tcache);\n\ttsd_slow_update(tsd);\n\n\tif (opt_tcache) {\n\t\t/* Trigger tcache init. */\n\t\ttsd_tcache_data_init(tsd);\n\t}\n\n\treturn false;\n}\n\n/* Initialize auto tcache (embedded in TSD). */\nstatic void\ntcache_init(tsd_t *tsd, tcache_t *tcache, void *avail_stack) {\n\tmemset(&tcache->link, 0, sizeof(ql_elm(tcache_t)));\n\ttcache->prof_accumbytes = 0;\n\ttcache->next_gc_bin = 0;\n\ttcache->arena = NULL;\n\n\tticker_init(&tcache->gc_ticker, TCACHE_GC_INCR);\n\n\tsize_t stack_offset = 0;\n\tassert((TCACHE_NSLOTS_SMALL_MAX & 1U) == 0);\n\tmemset(tcache->tbins_small, 0, sizeof(tcache_bin_t) * NBINS);\n\tmemset(tcache->tbins_large, 0, sizeof(tcache_bin_t) * (nhbins - NBINS));\n\tunsigned i = 0;\n\tfor (; i < NBINS; i++) {\n\t\ttcache->lg_fill_div[i] = 1;\n\t\tstack_offset += tcache_bin_info[i].ncached_max * sizeof(void *);\n\t\t/*\n\t\t * avail points past the available space.  Allocations will\n\t\t * access the slots toward higher addresses (for the benefit of\n\t\t * prefetch).\n\t\t */\n\t\ttcache_small_bin_get(tcache, i)->avail =\n\t\t    (void **)((uintptr_t)avail_stack + (uintptr_t)stack_offset);\n\t}\n\tfor (; i < nhbins; i++) {\n\t\tstack_offset += tcache_bin_info[i].ncached_max * sizeof(void *);\n\t\ttcache_large_bin_get(tcache, i)->avail =\n\t\t    (void **)((uintptr_t)avail_stack + (uintptr_t)stack_offset);\n\t}\n\tassert(stack_offset == stack_nelms * sizeof(void *));\n}\n\n/* Initialize auto tcache (embedded in TSD). */\nbool\ntsd_tcache_data_init(tsd_t *tsd) {\n\ttcache_t *tcache = tsd_tcachep_get_unsafe(tsd);\n\tassert(tcache_small_bin_get(tcache, 0)->avail == NULL);\n\tsize_t size = stack_nelms * sizeof(void *);\n\t/* Avoid false cacheline sharing. */\n\tsize = sz_sa2u(size, CACHELINE);\n\n\tvoid *avail_array = ipallocztm(tsd_tsdn(tsd), size, CACHELINE, true,\n\t    NULL, true, arena_get(TSDN_NULL, 0, true));\n\tif (avail_array == NULL) {\n\t\treturn true;\n\t}\n\n\ttcache_init(tsd, tcache, avail_array);\n\t/*\n\t * Initialization is a bit tricky here.  After malloc init is done, all\n\t * threads can rely on arena_choose and associate tcache accordingly.\n\t * However, the thread that does actual malloc bootstrapping relies on\n\t * functional tsd, and it can only rely on a0.  In that case, we\n\t * associate its tcache to a0 temporarily, and later on\n\t * arena_choose_hard() will re-associate properly.\n\t */\n\ttcache->arena = NULL;\n\tarena_t *arena;\n\tif (!malloc_initialized()) {\n\t\t/* If in initialization, assign to a0. */\n\t\tarena = arena_get(tsd_tsdn(tsd), 0, false);\n\t\ttcache_arena_associate(tsd_tsdn(tsd), tcache, arena);\n\t} else {\n\t\tarena = arena_choose(tsd, NULL);\n\t\t/* This may happen if thread.tcache.enabled is used. */\n\t\tif (tcache->arena == NULL) {\n\t\t\ttcache_arena_associate(tsd_tsdn(tsd), tcache, arena);\n\t\t}\n\t}\n\tassert(arena == tcache->arena);\n\n\treturn false;\n}\n\n/* Created manual tcache for tcache.create mallctl. */\ntcache_t *\ntcache_create_explicit(tsd_t *tsd) {\n\ttcache_t *tcache;\n\tsize_t size, stack_offset;\n\n\tsize = sizeof(tcache_t);\n\t/* Naturally align the pointer stacks. */\n\tsize = PTR_CEILING(size);\n\tstack_offset = size;\n\tsize += stack_nelms * sizeof(void *);\n\t/* Avoid false cacheline sharing. */\n\tsize = sz_sa2u(size, CACHELINE);\n\n\ttcache = ipallocztm(tsd_tsdn(tsd), size, CACHELINE, true, NULL, true,\n\t    arena_get(TSDN_NULL, 0, true));\n\tif (tcache == NULL) {\n\t\treturn NULL;\n\t}\n\n\ttcache_init(tsd, tcache,\n\t    (void *)((uintptr_t)tcache + (uintptr_t)stack_offset));\n\ttcache_arena_associate(tsd_tsdn(tsd), tcache, arena_ichoose(tsd, NULL));\n\n\treturn tcache;\n}\n\nstatic void\ntcache_flush_cache(tsd_t *tsd, tcache_t *tcache) {\n\tassert(tcache->arena != NULL);\n\n\tfor (unsigned i = 0; i < NBINS; i++) {\n\t\ttcache_bin_t *tbin = tcache_small_bin_get(tcache, i);\n\t\ttcache_bin_flush_small(tsd, tcache, tbin, i, 0);\n\n\t\tif (config_stats) {\n\t\t\tassert(tbin->tstats.nrequests == 0);\n\t\t}\n\t}\n\tfor (unsigned i = NBINS; i < nhbins; i++) {\n\t\ttcache_bin_t *tbin = tcache_large_bin_get(tcache, i);\n\t\ttcache_bin_flush_large(tsd, tbin, i, 0, tcache);\n\n\t\tif (config_stats) {\n\t\t\tassert(tbin->tstats.nrequests == 0);\n\t\t}\n\t}\n\n\tif (config_prof && tcache->prof_accumbytes > 0 &&\n\t    arena_prof_accum(tsd_tsdn(tsd), tcache->arena,\n\t    tcache->prof_accumbytes)) {\n\t\tprof_idump(tsd_tsdn(tsd));\n\t}\n}\n\nvoid\ntcache_flush(tsd_t *tsd) {\n\tassert(tcache_available(tsd));\n\ttcache_flush_cache(tsd, tsd_tcachep_get(tsd));\n}\n\nstatic void\ntcache_destroy(tsd_t *tsd, tcache_t *tcache, bool tsd_tcache) {\n\ttcache_flush_cache(tsd, tcache);\n\ttcache_arena_dissociate(tsd_tsdn(tsd), tcache);\n\n\tif (tsd_tcache) {\n\t\t/* Release the avail array for the TSD embedded auto tcache. */\n\t\tvoid *avail_array =\n\t\t    (void *)((uintptr_t)tcache_small_bin_get(tcache, 0)->avail -\n\t\t    (uintptr_t)tcache_bin_info[0].ncached_max * sizeof(void *));\n\t\tidalloctm(tsd_tsdn(tsd), avail_array, NULL, NULL, true, true);\n\t} else {\n\t\t/* Release both the tcache struct and avail array. */\n\t\tidalloctm(tsd_tsdn(tsd), tcache, NULL, NULL, true, true);\n\t}\n}\n\n/* For auto tcache (embedded in TSD) only. */\nvoid\ntcache_cleanup(tsd_t *tsd) {\n\ttcache_t *tcache = tsd_tcachep_get(tsd);\n\tif (!tcache_available(tsd)) {\n\t\tassert(tsd_tcache_enabled_get(tsd) == false);\n\t\tif (config_debug) {\n\t\t\tassert(tcache_small_bin_get(tcache, 0)->avail == NULL);\n\t\t}\n\t\treturn;\n\t}\n\tassert(tsd_tcache_enabled_get(tsd));\n\tassert(tcache_small_bin_get(tcache, 0)->avail != NULL);\n\n\ttcache_destroy(tsd, tcache, true);\n\tif (config_debug) {\n\t\ttcache_small_bin_get(tcache, 0)->avail = NULL;\n\t}\n}\n\nvoid\ntcache_stats_merge(tsdn_t *tsdn, tcache_t *tcache, arena_t *arena) {\n\tunsigned i;\n\n\tcassert(config_stats);\n\n\t/* Merge and reset tcache stats. */\n\tfor (i = 0; i < NBINS; i++) {\n\t\tarena_bin_t *bin = &arena->bins[i];\n\t\ttcache_bin_t *tbin = tcache_small_bin_get(tcache, i);\n\t\tmalloc_mutex_lock(tsdn, &bin->lock);\n\t\tbin->stats.nrequests += tbin->tstats.nrequests;\n\t\tmalloc_mutex_unlock(tsdn, &bin->lock);\n\t\ttbin->tstats.nrequests = 0;\n\t}\n\n\tfor (; i < nhbins; i++) {\n\t\ttcache_bin_t *tbin = tcache_large_bin_get(tcache, i);\n\t\tarena_stats_large_nrequests_add(tsdn, &arena->stats, i,\n\t\t    tbin->tstats.nrequests);\n\t\ttbin->tstats.nrequests = 0;\n\t}\n}\n\nstatic bool\ntcaches_create_prep(tsd_t *tsd) {\n\tbool err;\n\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &tcaches_mtx);\n\n\tif (tcaches == NULL) {\n\t\ttcaches = base_alloc(tsd_tsdn(tsd), b0get(), sizeof(tcache_t *)\n\t\t    * (MALLOCX_TCACHE_MAX+1), CACHELINE);\n\t\tif (tcaches == NULL) {\n\t\t\terr = true;\n\t\t\tgoto label_return;\n\t\t}\n\t}\n\n\tif (tcaches_avail == NULL && tcaches_past > MALLOCX_TCACHE_MAX) {\n\t\terr = true;\n\t\tgoto label_return;\n\t}\n\n\terr = false;\nlabel_return:\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &tcaches_mtx);\n\treturn err;\n}\n\nbool\ntcaches_create(tsd_t *tsd, unsigned *r_ind) {\n\twitness_assert_depth(tsdn_witness_tsdp_get(tsd_tsdn(tsd)), 0);\n\n\tbool err;\n\n\tif (tcaches_create_prep(tsd)) {\n\t\terr = true;\n\t\tgoto label_return;\n\t}\n\n\ttcache_t *tcache = tcache_create_explicit(tsd);\n\tif (tcache == NULL) {\n\t\terr = true;\n\t\tgoto label_return;\n\t}\n\n\ttcaches_t *elm;\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &tcaches_mtx);\n\tif (tcaches_avail != NULL) {\n\t\telm = tcaches_avail;\n\t\ttcaches_avail = tcaches_avail->next;\n\t\telm->tcache = tcache;\n\t\t*r_ind = (unsigned)(elm - tcaches);\n\t} else {\n\t\telm = &tcaches[tcaches_past];\n\t\telm->tcache = tcache;\n\t\t*r_ind = tcaches_past;\n\t\ttcaches_past++;\n\t}\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &tcaches_mtx);\n\n\terr = false;\nlabel_return:\n\twitness_assert_depth(tsdn_witness_tsdp_get(tsd_tsdn(tsd)), 0);\n\treturn err;\n}\n\nstatic tcache_t *\ntcaches_elm_remove(tsd_t *tsd, tcaches_t *elm) {\n\tmalloc_mutex_assert_owner(tsd_tsdn(tsd), &tcaches_mtx);\n\n\tif (elm->tcache == NULL) {\n\t\treturn NULL;\n\t}\n\ttcache_t *tcache = elm->tcache;\n\telm->tcache = NULL;\n\treturn tcache;\n}\n\nvoid\ntcaches_flush(tsd_t *tsd, unsigned ind) {\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &tcaches_mtx);\n\ttcache_t *tcache = tcaches_elm_remove(tsd, &tcaches[ind]);\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &tcaches_mtx);\n\tif (tcache != NULL) {\n\t\ttcache_destroy(tsd, tcache, false);\n\t}\n}\n\nvoid\ntcaches_destroy(tsd_t *tsd, unsigned ind) {\n\tmalloc_mutex_lock(tsd_tsdn(tsd), &tcaches_mtx);\n\ttcaches_t *elm = &tcaches[ind];\n\ttcache_t *tcache = tcaches_elm_remove(tsd, elm);\n\telm->next = tcaches_avail;\n\ttcaches_avail = elm;\n\tmalloc_mutex_unlock(tsd_tsdn(tsd), &tcaches_mtx);\n\tif (tcache != NULL) {\n\t\ttcache_destroy(tsd, tcache, false);\n\t}\n}\n\nbool\ntcache_boot(tsdn_t *tsdn) {\n\t/* If necessary, clamp opt_lg_tcache_max. */\n\tif (opt_lg_tcache_max < 0 || (ZU(1) << opt_lg_tcache_max) <\n\t    SMALL_MAXCLASS) {\n\t\ttcache_maxclass = SMALL_MAXCLASS;\n\t} else {\n\t\ttcache_maxclass = (ZU(1) << opt_lg_tcache_max);\n\t}\n\n\tif (malloc_mutex_init(&tcaches_mtx, \"tcaches\", WITNESS_RANK_TCACHES,\n\t    malloc_mutex_rank_exclusive)) {\n\t\treturn true;\n\t}\n\n\tnhbins = sz_size2index(tcache_maxclass) + 1;\n\n\t/* Initialize tcache_bin_info. */\n\ttcache_bin_info = (tcache_bin_info_t *)base_alloc(tsdn, b0get(), nhbins\n\t    * sizeof(tcache_bin_info_t), CACHELINE);\n\tif (tcache_bin_info == NULL) {\n\t\treturn true;\n\t}\n\tstack_nelms = 0;\n\tunsigned i;\n\tfor (i = 0; i < NBINS; i++) {\n\t\tif ((arena_bin_info[i].nregs << 1) <= TCACHE_NSLOTS_SMALL_MIN) {\n\t\t\ttcache_bin_info[i].ncached_max =\n\t\t\t    TCACHE_NSLOTS_SMALL_MIN;\n\t\t} else if ((arena_bin_info[i].nregs << 1) <=\n\t\t    TCACHE_NSLOTS_SMALL_MAX) {\n\t\t\ttcache_bin_info[i].ncached_max =\n\t\t\t    (arena_bin_info[i].nregs << 1);\n\t\t} else {\n\t\t\ttcache_bin_info[i].ncached_max =\n\t\t\t    TCACHE_NSLOTS_SMALL_MAX;\n\t\t}\n\t\tstack_nelms += tcache_bin_info[i].ncached_max;\n\t}\n\tfor (; i < nhbins; i++) {\n\t\ttcache_bin_info[i].ncached_max = TCACHE_NSLOTS_LARGE;\n\t\tstack_nelms += tcache_bin_info[i].ncached_max;\n\t}\n\n\treturn false;\n}\n\nvoid\ntcache_prefork(tsdn_t *tsdn) {\n\tif (!config_prof && opt_tcache) {\n\t\tmalloc_mutex_prefork(tsdn, &tcaches_mtx);\n\t}\n}\n\nvoid\ntcache_postfork_parent(tsdn_t *tsdn) {\n\tif (!config_prof && opt_tcache) {\n\t\tmalloc_mutex_postfork_parent(tsdn, &tcaches_mtx);\n\t}\n}\n\nvoid\ntcache_postfork_child(tsdn_t *tsdn) {\n\tif (!config_prof && opt_tcache) {\n\t\tmalloc_mutex_postfork_child(tsdn, &tcaches_mtx);\n\t}\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/ticker.c",
    "content": "#define JEMALLOC_TICKER_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/tsd.c",
    "content": "#define JEMALLOC_TSD_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/mutex.h\"\n#include \"jemalloc/internal/rtree.h\"\n\n/******************************************************************************/\n/* Data. */\n\nstatic unsigned ncleanups;\nstatic malloc_tsd_cleanup_t cleanups[MALLOC_TSD_CLEANUPS_MAX];\n\n#ifdef JEMALLOC_MALLOC_THREAD_CLEANUP\n__thread tsd_t JEMALLOC_TLS_MODEL tsd_tls = TSD_INITIALIZER;\n__thread bool JEMALLOC_TLS_MODEL tsd_initialized = false;\nbool tsd_booted = false;\n#elif (defined(JEMALLOC_TLS))\n__thread tsd_t JEMALLOC_TLS_MODEL tsd_tls = TSD_INITIALIZER;\npthread_key_t tsd_tsd;\nbool tsd_booted = false;\n#elif (defined(_WIN32))\nDWORD tsd_tsd;\ntsd_wrapper_t tsd_boot_wrapper = {false, TSD_INITIALIZER};\nbool tsd_booted = false;\n#else\n\n/*\n * This contains a mutex, but it's pretty convenient to allow the mutex code to\n * have a dependency on tsd.  So we define the struct here, and only refer to it\n * by pointer in the header.\n */\nstruct tsd_init_head_s {\n\tql_head(tsd_init_block_t) blocks;\n\tmalloc_mutex_t lock;\n};\n\npthread_key_t tsd_tsd;\ntsd_init_head_t\ttsd_init_head = {\n\tql_head_initializer(blocks),\n\tMALLOC_MUTEX_INITIALIZER\n};\ntsd_wrapper_t tsd_boot_wrapper = {\n\tfalse,\n\tTSD_INITIALIZER\n};\nbool tsd_booted = false;\n#endif\n\n\n/******************************************************************************/\n\nvoid\ntsd_slow_update(tsd_t *tsd) {\n\tif (tsd_nominal(tsd)) {\n\t\tif (malloc_slow || !tsd_tcache_enabled_get(tsd) ||\n\t\t    tsd_reentrancy_level_get(tsd) > 0) {\n\t\t\ttsd->state = tsd_state_nominal_slow;\n\t\t} else {\n\t\t\ttsd->state = tsd_state_nominal;\n\t\t}\n\t}\n}\n\nstatic bool\ntsd_data_init(tsd_t *tsd) {\n\t/*\n\t * We initialize the rtree context first (before the tcache), since the\n\t * tcache initialization depends on it.\n\t */\n\trtree_ctx_data_init(tsd_rtree_ctxp_get_unsafe(tsd));\n\n\treturn tsd_tcache_enabled_data_init(tsd);\n}\n\nstatic void\nassert_tsd_data_cleanup_done(tsd_t *tsd) {\n\tassert(!tsd_nominal(tsd));\n\tassert(*tsd_arenap_get_unsafe(tsd) == NULL);\n\tassert(*tsd_iarenap_get_unsafe(tsd) == NULL);\n\tassert(*tsd_arenas_tdata_bypassp_get_unsafe(tsd) == true);\n\tassert(*tsd_arenas_tdatap_get_unsafe(tsd) == NULL);\n\tassert(*tsd_tcache_enabledp_get_unsafe(tsd) == false);\n\tassert(*tsd_prof_tdatap_get_unsafe(tsd) == NULL);\n}\n\nstatic bool\ntsd_data_init_nocleanup(tsd_t *tsd) {\n\tassert(tsd->state == tsd_state_reincarnated ||\n\t    tsd->state == tsd_state_minimal_initialized);\n\t/*\n\t * During reincarnation, there is no guarantee that the cleanup function\n\t * will be called (deallocation may happen after all tsd destructors).\n\t * We set up tsd in a way that no cleanup is needed.\n\t */\n\trtree_ctx_data_init(tsd_rtree_ctxp_get_unsafe(tsd));\n\t*tsd_arenas_tdata_bypassp_get(tsd) = true;\n\t*tsd_tcache_enabledp_get_unsafe(tsd) = false;\n\t*tsd_reentrancy_levelp_get(tsd) = 1;\n\tassert_tsd_data_cleanup_done(tsd);\n\n\treturn false;\n}\n\ntsd_t *\ntsd_fetch_slow(tsd_t *tsd, bool minimal) {\n\tassert(!tsd_fast(tsd));\n\n\tif (tsd->state == tsd_state_nominal_slow) {\n\t\t/* On slow path but no work needed. */\n\t\tassert(malloc_slow || !tsd_tcache_enabled_get(tsd) ||\n\t\t    tsd_reentrancy_level_get(tsd) > 0 ||\n\t\t    *tsd_arenas_tdata_bypassp_get(tsd));\n\t} else if (tsd->state == tsd_state_uninitialized) {\n\t\tif (!minimal) {\n\t\t\ttsd->state = tsd_state_nominal;\n\t\t\ttsd_slow_update(tsd);\n\t\t\t/* Trigger cleanup handler registration. */\n\t\t\ttsd_set(tsd);\n\t\t\ttsd_data_init(tsd);\n\t\t} else {\n\t\t\ttsd->state = tsd_state_minimal_initialized;\n\t\t\ttsd_set(tsd);\n\t\t\ttsd_data_init_nocleanup(tsd);\n\t\t}\n\t} else if (tsd->state == tsd_state_minimal_initialized) {\n\t\tif (!minimal) {\n\t\t\t/* Switch to fully initialized. */\n\t\t\ttsd->state = tsd_state_nominal;\n\t\t\tassert(*tsd_reentrancy_levelp_get(tsd) >= 1);\n\t\t\t(*tsd_reentrancy_levelp_get(tsd))--;\n\t\t\ttsd_slow_update(tsd);\n\t\t\ttsd_data_init(tsd);\n\t\t} else {\n\t\t\tassert_tsd_data_cleanup_done(tsd);\n\t\t}\n\t} else if (tsd->state == tsd_state_purgatory) {\n\t\ttsd->state = tsd_state_reincarnated;\n\t\ttsd_set(tsd);\n\t\ttsd_data_init_nocleanup(tsd);\n\t} else {\n\t\tassert(tsd->state == tsd_state_reincarnated);\n\t}\n\n\treturn tsd;\n}\n\nvoid *\nmalloc_tsd_malloc(size_t size) {\n\treturn a0malloc(CACHELINE_CEILING(size));\n}\n\nvoid\nmalloc_tsd_dalloc(void *wrapper) {\n\ta0dalloc(wrapper);\n}\n\n#if defined(JEMALLOC_MALLOC_THREAD_CLEANUP) || defined(_WIN32)\n#ifndef _WIN32\nJEMALLOC_EXPORT\n#endif\nvoid\n_malloc_thread_cleanup(void) {\n\tbool pending[MALLOC_TSD_CLEANUPS_MAX], again;\n\tunsigned i;\n\n\tfor (i = 0; i < ncleanups; i++) {\n\t\tpending[i] = true;\n\t}\n\n\tdo {\n\t\tagain = false;\n\t\tfor (i = 0; i < ncleanups; i++) {\n\t\t\tif (pending[i]) {\n\t\t\t\tpending[i] = cleanups[i]();\n\t\t\t\tif (pending[i]) {\n\t\t\t\t\tagain = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t} while (again);\n}\n#endif\n\nvoid\nmalloc_tsd_cleanup_register(bool (*f)(void)) {\n\tassert(ncleanups < MALLOC_TSD_CLEANUPS_MAX);\n\tcleanups[ncleanups] = f;\n\tncleanups++;\n}\n\nstatic void\ntsd_do_data_cleanup(tsd_t *tsd) {\n\tprof_tdata_cleanup(tsd);\n\tiarena_cleanup(tsd);\n\tarena_cleanup(tsd);\n\tarenas_tdata_cleanup(tsd);\n\ttcache_cleanup(tsd);\n\twitnesses_cleanup(tsd_witness_tsdp_get_unsafe(tsd));\n}\n\nvoid\ntsd_cleanup(void *arg) {\n\ttsd_t *tsd = (tsd_t *)arg;\n\n\tswitch (tsd->state) {\n\tcase tsd_state_uninitialized:\n\t\t/* Do nothing. */\n\t\tbreak;\n\tcase tsd_state_minimal_initialized:\n\t\t/* This implies the thread only did free() in its life time. */\n\t\t/* Fall through. */\n\tcase tsd_state_reincarnated:\n\t\t/*\n\t\t * Reincarnated means another destructor deallocated memory\n\t\t * after the destructor was called.  Cleanup isn't required but\n\t\t * is still called for testing and completeness.\n\t\t */\n\t\tassert_tsd_data_cleanup_done(tsd);\n\t\t/* Fall through. */\n\tcase tsd_state_nominal:\n\tcase tsd_state_nominal_slow:\n\t\ttsd_do_data_cleanup(tsd);\n\t\ttsd->state = tsd_state_purgatory;\n\t\ttsd_set(tsd);\n\t\tbreak;\n\tcase tsd_state_purgatory:\n\t\t/*\n\t\t * The previous time this destructor was called, we set the\n\t\t * state to tsd_state_purgatory so that other destructors\n\t\t * wouldn't cause re-creation of the tsd.  This time, do\n\t\t * nothing, and do not request another callback.\n\t\t */\n\t\tbreak;\n\tdefault:\n\t\tnot_reached();\n\t}\n#ifdef JEMALLOC_JET\n\ttest_callback_t test_callback = *tsd_test_callbackp_get_unsafe(tsd);\n\tint *data = tsd_test_datap_get_unsafe(tsd);\n\tif (test_callback != NULL) {\n\t\ttest_callback(data);\n\t}\n#endif\n}\n\ntsd_t *\nmalloc_tsd_boot0(void) {\n\ttsd_t *tsd;\n\n\tncleanups = 0;\n\tif (tsd_boot0()) {\n\t\treturn NULL;\n\t}\n\ttsd = tsd_fetch();\n\t*tsd_arenas_tdata_bypassp_get(tsd) = true;\n\treturn tsd;\n}\n\nvoid\nmalloc_tsd_boot1(void) {\n\ttsd_boot1();\n\ttsd_t *tsd = tsd_fetch();\n\t/* malloc_slow has been set properly.  Update tsd_slow. */\n\ttsd_slow_update(tsd);\n\t*tsd_arenas_tdata_bypassp_get(tsd) = false;\n}\n\n#ifdef _WIN32\nstatic BOOL WINAPI\n_tls_callback(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {\n\tswitch (fdwReason) {\n#ifdef JEMALLOC_LAZY_LOCK\n\tcase DLL_THREAD_ATTACH:\n\t\tisthreaded = true;\n\t\tbreak;\n#endif\n\tcase DLL_THREAD_DETACH:\n\t\t_malloc_thread_cleanup();\n\t\tbreak;\n\tdefault:\n\t\tbreak;\n\t}\n\treturn true;\n}\n\n/*\n * We need to be able to say \"read\" here (in the \"pragma section\"), but have\n * hooked \"read\". We won't read for the rest of the file, so we can get away\n * with unhooking.\n */\n#ifdef read\n#  undef read\n#endif\n\n#ifdef _MSC_VER\n#  ifdef _M_IX86\n#    pragma comment(linker, \"/INCLUDE:__tls_used\")\n#    pragma comment(linker, \"/INCLUDE:_tls_callback\")\n#  else\n#    pragma comment(linker, \"/INCLUDE:_tls_used\")\n#    pragma comment(linker, \"/INCLUDE:tls_callback\")\n#  endif\n#  pragma section(\".CRT$XLY\",long,read)\n#endif\nJEMALLOC_SECTION(\".CRT$XLY\") JEMALLOC_ATTR(used)\nBOOL\t(WINAPI *const tls_callback)(HINSTANCE hinstDLL,\n    DWORD fdwReason, LPVOID lpvReserved) = _tls_callback;\n#endif\n\n#if (!defined(JEMALLOC_MALLOC_THREAD_CLEANUP) && !defined(JEMALLOC_TLS) && \\\n    !defined(_WIN32))\nvoid *\ntsd_init_check_recursion(tsd_init_head_t *head, tsd_init_block_t *block) {\n\tpthread_t self = pthread_self();\n\ttsd_init_block_t *iter;\n\n\t/* Check whether this thread has already inserted into the list. */\n\tmalloc_mutex_lock(TSDN_NULL, &head->lock);\n\tql_foreach(iter, &head->blocks, link) {\n\t\tif (iter->thread == self) {\n\t\t\tmalloc_mutex_unlock(TSDN_NULL, &head->lock);\n\t\t\treturn iter->data;\n\t\t}\n\t}\n\t/* Insert block into list. */\n\tql_elm_new(block, link);\n\tblock->thread = self;\n\tql_tail_insert(&head->blocks, block, link);\n\tmalloc_mutex_unlock(TSDN_NULL, &head->lock);\n\treturn NULL;\n}\n\nvoid\ntsd_init_finish(tsd_init_head_t *head, tsd_init_block_t *block) {\n\tmalloc_mutex_lock(TSDN_NULL, &head->lock);\n\tql_remove(&head->blocks, block, link);\n\tmalloc_mutex_unlock(TSDN_NULL, &head->lock);\n}\n#endif\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/witness.c",
    "content": "#define JEMALLOC_WITNESS_C_\n#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n#include \"jemalloc/internal/malloc_io.h\"\n\nvoid\nwitness_init(witness_t *witness, const char *name, witness_rank_t rank,\n    witness_comp_t *comp, void *opaque) {\n\twitness->name = name;\n\twitness->rank = rank;\n\twitness->comp = comp;\n\twitness->opaque = opaque;\n}\n\nstatic void\nwitness_lock_error_impl(const witness_list_t *witnesses,\n    const witness_t *witness) {\n\twitness_t *w;\n\n\tmalloc_printf(\"<jemalloc>: Lock rank order reversal:\");\n\tql_foreach(w, witnesses, link) {\n\t\tmalloc_printf(\" %s(%u)\", w->name, w->rank);\n\t}\n\tmalloc_printf(\" %s(%u)\\n\", witness->name, witness->rank);\n\tabort();\n}\nwitness_lock_error_t *JET_MUTABLE witness_lock_error = witness_lock_error_impl;\n\nstatic void\nwitness_owner_error_impl(const witness_t *witness) {\n\tmalloc_printf(\"<jemalloc>: Should own %s(%u)\\n\", witness->name,\n\t    witness->rank);\n\tabort();\n}\nwitness_owner_error_t *JET_MUTABLE witness_owner_error =\n    witness_owner_error_impl;\n\nstatic void\nwitness_not_owner_error_impl(const witness_t *witness) {\n\tmalloc_printf(\"<jemalloc>: Should not own %s(%u)\\n\", witness->name,\n\t    witness->rank);\n\tabort();\n}\nwitness_not_owner_error_t *JET_MUTABLE witness_not_owner_error =\n    witness_not_owner_error_impl;\n\nstatic void\nwitness_depth_error_impl(const witness_list_t *witnesses,\n    witness_rank_t rank_inclusive, unsigned depth) {\n\twitness_t *w;\n\n\tmalloc_printf(\"<jemalloc>: Should own %u lock%s of rank >= %u:\", depth,\n\t    (depth != 1) ?  \"s\" : \"\", rank_inclusive);\n\tql_foreach(w, witnesses, link) {\n\t\tmalloc_printf(\" %s(%u)\", w->name, w->rank);\n\t}\n\tmalloc_printf(\"\\n\");\n\tabort();\n}\nwitness_depth_error_t *JET_MUTABLE witness_depth_error =\n    witness_depth_error_impl;\n\nvoid\nwitnesses_cleanup(witness_tsd_t *witness_tsd) {\n\twitness_assert_lockless(witness_tsd_tsdn(witness_tsd));\n\n\t/* Do nothing. */\n}\n\nvoid\nwitness_prefork(witness_tsd_t *witness_tsd) {\n\tif (!config_debug) {\n\t\treturn;\n\t}\n\twitness_tsd->forking = true;\n}\n\nvoid\nwitness_postfork_parent(witness_tsd_t *witness_tsd) {\n\tif (!config_debug) {\n\t\treturn;\n\t}\n\twitness_tsd->forking = false;\n}\n\nvoid\nwitness_postfork_child(witness_tsd_t *witness_tsd) {\n\tif (!config_debug) {\n\t\treturn;\n\t}\n#ifndef JEMALLOC_MUTEX_INIT_CB\n\twitness_list_t *witnesses;\n\n\twitnesses = &witness_tsd->witnesses;\n\tql_new(witnesses);\n#endif\n\twitness_tsd->forking = false;\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/src/zone.c",
    "content": "#include \"jemalloc/internal/jemalloc_preamble.h\"\n#include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n#include \"jemalloc/internal/assert.h\"\n\n#ifndef JEMALLOC_ZONE\n#  error \"This source file is for zones on Darwin (OS X).\"\n#endif\n\n/* Definitions of the following structs in malloc/malloc.h might be too old\n * for the built binary to run on newer versions of OSX. So use the newest\n * possible version of those structs.\n */\ntypedef struct _malloc_zone_t {\n\tvoid *reserved1;\n\tvoid *reserved2;\n\tsize_t (*size)(struct _malloc_zone_t *, const void *);\n\tvoid *(*malloc)(struct _malloc_zone_t *, size_t);\n\tvoid *(*calloc)(struct _malloc_zone_t *, size_t, size_t);\n\tvoid *(*valloc)(struct _malloc_zone_t *, size_t);\n\tvoid (*free)(struct _malloc_zone_t *, void *);\n\tvoid *(*realloc)(struct _malloc_zone_t *, void *, size_t);\n\tvoid (*destroy)(struct _malloc_zone_t *);\n\tconst char *zone_name;\n\tunsigned (*batch_malloc)(struct _malloc_zone_t *, size_t, void **, unsigned);\n\tvoid (*batch_free)(struct _malloc_zone_t *, void **, unsigned);\n\tstruct malloc_introspection_t *introspect;\n\tunsigned version;\n\tvoid *(*memalign)(struct _malloc_zone_t *, size_t, size_t);\n\tvoid (*free_definite_size)(struct _malloc_zone_t *, void *, size_t);\n\tsize_t (*pressure_relief)(struct _malloc_zone_t *, size_t);\n} malloc_zone_t;\n\ntypedef struct {\n\tvm_address_t address;\n\tvm_size_t size;\n} vm_range_t;\n\ntypedef struct malloc_statistics_t {\n\tunsigned blocks_in_use;\n\tsize_t size_in_use;\n\tsize_t max_size_in_use;\n\tsize_t size_allocated;\n} malloc_statistics_t;\n\ntypedef kern_return_t memory_reader_t(task_t, vm_address_t, vm_size_t, void **);\n\ntypedef void vm_range_recorder_t(task_t, void *, unsigned type, vm_range_t *, unsigned);\n\ntypedef struct malloc_introspection_t {\n\tkern_return_t (*enumerator)(task_t, void *, unsigned, vm_address_t, memory_reader_t, vm_range_recorder_t);\n\tsize_t (*good_size)(malloc_zone_t *, size_t);\n\tboolean_t (*check)(malloc_zone_t *);\n\tvoid (*print)(malloc_zone_t *, boolean_t);\n\tvoid (*log)(malloc_zone_t *, void *);\n\tvoid (*force_lock)(malloc_zone_t *);\n\tvoid (*force_unlock)(malloc_zone_t *);\n\tvoid (*statistics)(malloc_zone_t *, malloc_statistics_t *);\n\tboolean_t (*zone_locked)(malloc_zone_t *);\n\tboolean_t (*enable_discharge_checking)(malloc_zone_t *);\n\tboolean_t (*disable_discharge_checking)(malloc_zone_t *);\n\tvoid (*discharge)(malloc_zone_t *, void *);\n#ifdef __BLOCKS__\n\tvoid (*enumerate_discharged_pointers)(malloc_zone_t *, void (^)(void *, void *));\n#else\n\tvoid *enumerate_unavailable_without_blocks;\n#endif\n\tvoid (*reinit_lock)(malloc_zone_t *);\n} malloc_introspection_t;\n\nextern kern_return_t malloc_get_all_zones(task_t, memory_reader_t, vm_address_t **, unsigned *);\n\nextern malloc_zone_t *malloc_default_zone(void);\n\nextern void malloc_zone_register(malloc_zone_t *zone);\n\nextern void malloc_zone_unregister(malloc_zone_t *zone);\n\n/*\n * The malloc_default_purgeable_zone() function is only available on >= 10.6.\n * We need to check whether it is present at runtime, thus the weak_import.\n */\nextern malloc_zone_t *malloc_default_purgeable_zone(void)\nJEMALLOC_ATTR(weak_import);\n\n/******************************************************************************/\n/* Data. */\n\nstatic malloc_zone_t *default_zone, *purgeable_zone;\nstatic malloc_zone_t jemalloc_zone;\nstatic struct malloc_introspection_t jemalloc_zone_introspect;\n\n/******************************************************************************/\n/* Function prototypes for non-inline static functions. */\n\nstatic size_t\tzone_size(malloc_zone_t *zone, const void *ptr);\nstatic void\t*zone_malloc(malloc_zone_t *zone, size_t size);\nstatic void\t*zone_calloc(malloc_zone_t *zone, size_t num, size_t size);\nstatic void\t*zone_valloc(malloc_zone_t *zone, size_t size);\nstatic void\tzone_free(malloc_zone_t *zone, void *ptr);\nstatic void\t*zone_realloc(malloc_zone_t *zone, void *ptr, size_t size);\nstatic void\t*zone_memalign(malloc_zone_t *zone, size_t alignment,\n    size_t size);\nstatic void\tzone_free_definite_size(malloc_zone_t *zone, void *ptr,\n    size_t size);\nstatic void\tzone_destroy(malloc_zone_t *zone);\nstatic unsigned\tzone_batch_malloc(struct _malloc_zone_t *zone, size_t size,\n    void **results, unsigned num_requested);\nstatic void\tzone_batch_free(struct _malloc_zone_t *zone,\n    void **to_be_freed, unsigned num_to_be_freed);\nstatic size_t\tzone_pressure_relief(struct _malloc_zone_t *zone, size_t goal);\nstatic size_t\tzone_good_size(malloc_zone_t *zone, size_t size);\nstatic kern_return_t\tzone_enumerator(task_t task, void *data, unsigned type_mask,\n    vm_address_t zone_address, memory_reader_t reader,\n    vm_range_recorder_t recorder);\nstatic boolean_t\tzone_check(malloc_zone_t *zone);\nstatic void\tzone_print(malloc_zone_t *zone, boolean_t verbose);\nstatic void\tzone_log(malloc_zone_t *zone, void *address);\nstatic void\tzone_force_lock(malloc_zone_t *zone);\nstatic void\tzone_force_unlock(malloc_zone_t *zone);\nstatic void\tzone_statistics(malloc_zone_t *zone,\n    malloc_statistics_t *stats);\nstatic boolean_t\tzone_locked(malloc_zone_t *zone);\nstatic void\tzone_reinit_lock(malloc_zone_t *zone);\n\n/******************************************************************************/\n/*\n * Functions.\n */\n\nstatic size_t\nzone_size(malloc_zone_t *zone, const void *ptr) {\n\t/*\n\t * There appear to be places within Darwin (such as setenv(3)) that\n\t * cause calls to this function with pointers that *no* zone owns.  If\n\t * we knew that all pointers were owned by *some* zone, we could split\n\t * our zone into two parts, and use one as the default allocator and\n\t * the other as the default deallocator/reallocator.  Since that will\n\t * not work in practice, we must check all pointers to assure that they\n\t * reside within a mapped extent before determining size.\n\t */\n\treturn ivsalloc(tsdn_fetch(), ptr);\n}\n\nstatic void *\nzone_malloc(malloc_zone_t *zone, size_t size) {\n\treturn je_malloc(size);\n}\n\nstatic void *\nzone_calloc(malloc_zone_t *zone, size_t num, size_t size) {\n\treturn je_calloc(num, size);\n}\n\nstatic void *\nzone_valloc(malloc_zone_t *zone, size_t size) {\n\tvoid *ret = NULL; /* Assignment avoids useless compiler warning. */\n\n\tje_posix_memalign(&ret, PAGE, size);\n\n\treturn ret;\n}\n\nstatic void\nzone_free(malloc_zone_t *zone, void *ptr) {\n\tif (ivsalloc(tsdn_fetch(), ptr) != 0) {\n\t\tje_free(ptr);\n\t\treturn;\n\t}\n\n\tfree(ptr);\n}\n\nstatic void *\nzone_realloc(malloc_zone_t *zone, void *ptr, size_t size) {\n\tif (ivsalloc(tsdn_fetch(), ptr) != 0) {\n\t\treturn je_realloc(ptr, size);\n\t}\n\n\treturn realloc(ptr, size);\n}\n\nstatic void *\nzone_memalign(malloc_zone_t *zone, size_t alignment, size_t size) {\n\tvoid *ret = NULL; /* Assignment avoids useless compiler warning. */\n\n\tje_posix_memalign(&ret, alignment, size);\n\n\treturn ret;\n}\n\nstatic void\nzone_free_definite_size(malloc_zone_t *zone, void *ptr, size_t size) {\n\tsize_t alloc_size;\n\n\talloc_size = ivsalloc(tsdn_fetch(), ptr);\n\tif (alloc_size != 0) {\n\t\tassert(alloc_size == size);\n\t\tje_free(ptr);\n\t\treturn;\n\t}\n\n\tfree(ptr);\n}\n\nstatic void\nzone_destroy(malloc_zone_t *zone) {\n\t/* This function should never be called. */\n\tnot_reached();\n}\n\nstatic unsigned\nzone_batch_malloc(struct _malloc_zone_t *zone, size_t size, void **results,\n    unsigned num_requested) {\n\tunsigned i;\n\n\tfor (i = 0; i < num_requested; i++) {\n\t\tresults[i] = je_malloc(size);\n\t\tif (!results[i])\n\t\t\tbreak;\n\t}\n\n\treturn i;\n}\n\nstatic void\nzone_batch_free(struct _malloc_zone_t *zone, void **to_be_freed,\n    unsigned num_to_be_freed) {\n\tunsigned i;\n\n\tfor (i = 0; i < num_to_be_freed; i++) {\n\t\tzone_free(zone, to_be_freed[i]);\n\t\tto_be_freed[i] = NULL;\n\t}\n}\n\nstatic size_t\nzone_pressure_relief(struct _malloc_zone_t *zone, size_t goal) {\n\treturn 0;\n}\n\nstatic size_t\nzone_good_size(malloc_zone_t *zone, size_t size) {\n\tif (size == 0) {\n\t\tsize = 1;\n\t}\n\treturn sz_s2u(size);\n}\n\nstatic kern_return_t\nzone_enumerator(task_t task, void *data, unsigned type_mask,\n    vm_address_t zone_address, memory_reader_t reader,\n    vm_range_recorder_t recorder) {\n\treturn KERN_SUCCESS;\n}\n\nstatic boolean_t\nzone_check(malloc_zone_t *zone) {\n\treturn true;\n}\n\nstatic void\nzone_print(malloc_zone_t *zone, boolean_t verbose) {\n}\n\nstatic void\nzone_log(malloc_zone_t *zone, void *address) {\n}\n\nstatic void\nzone_force_lock(malloc_zone_t *zone) {\n\tif (isthreaded) {\n\t\tjemalloc_prefork();\n\t}\n}\n\nstatic void\nzone_force_unlock(malloc_zone_t *zone) {\n\t/*\n\t * Call jemalloc_postfork_child() rather than\n\t * jemalloc_postfork_parent(), because this function is executed by both\n\t * parent and child.  The parent can tolerate having state\n\t * reinitialized, but the child cannot unlock mutexes that were locked\n\t * by the parent.\n\t */\n\tif (isthreaded) {\n\t\tjemalloc_postfork_child();\n\t}\n}\n\nstatic void\nzone_statistics(malloc_zone_t *zone, malloc_statistics_t *stats) {\n\t/* We make no effort to actually fill the values */\n\tstats->blocks_in_use = 0;\n\tstats->size_in_use = 0;\n\tstats->max_size_in_use = 0;\n\tstats->size_allocated = 0;\n}\n\nstatic boolean_t\nzone_locked(malloc_zone_t *zone) {\n\t/* Pretend no lock is being held */\n\treturn false;\n}\n\nstatic void\nzone_reinit_lock(malloc_zone_t *zone) {\n\t/* As of OSX 10.12, this function is only used when force_unlock would\n\t * be used if the zone version were < 9. So just use force_unlock. */\n\tzone_force_unlock(zone);\n}\n\nstatic void\nzone_init(void) {\n\tjemalloc_zone.size = zone_size;\n\tjemalloc_zone.malloc = zone_malloc;\n\tjemalloc_zone.calloc = zone_calloc;\n\tjemalloc_zone.valloc = zone_valloc;\n\tjemalloc_zone.free = zone_free;\n\tjemalloc_zone.realloc = zone_realloc;\n\tjemalloc_zone.destroy = zone_destroy;\n\tjemalloc_zone.zone_name = \"jemalloc_zone\";\n\tjemalloc_zone.batch_malloc = zone_batch_malloc;\n\tjemalloc_zone.batch_free = zone_batch_free;\n\tjemalloc_zone.introspect = &jemalloc_zone_introspect;\n\tjemalloc_zone.version = 9;\n\tjemalloc_zone.memalign = zone_memalign;\n\tjemalloc_zone.free_definite_size = zone_free_definite_size;\n\tjemalloc_zone.pressure_relief = zone_pressure_relief;\n\n\tjemalloc_zone_introspect.enumerator = zone_enumerator;\n\tjemalloc_zone_introspect.good_size = zone_good_size;\n\tjemalloc_zone_introspect.check = zone_check;\n\tjemalloc_zone_introspect.print = zone_print;\n\tjemalloc_zone_introspect.log = zone_log;\n\tjemalloc_zone_introspect.force_lock = zone_force_lock;\n\tjemalloc_zone_introspect.force_unlock = zone_force_unlock;\n\tjemalloc_zone_introspect.statistics = zone_statistics;\n\tjemalloc_zone_introspect.zone_locked = zone_locked;\n\tjemalloc_zone_introspect.enable_discharge_checking = NULL;\n\tjemalloc_zone_introspect.disable_discharge_checking = NULL;\n\tjemalloc_zone_introspect.discharge = NULL;\n#ifdef __BLOCKS__\n\tjemalloc_zone_introspect.enumerate_discharged_pointers = NULL;\n#else\n\tjemalloc_zone_introspect.enumerate_unavailable_without_blocks = NULL;\n#endif\n\tjemalloc_zone_introspect.reinit_lock = zone_reinit_lock;\n}\n\nstatic malloc_zone_t *\nzone_default_get(void) {\n\tmalloc_zone_t **zones = NULL;\n\tunsigned int num_zones = 0;\n\n\t/*\n\t * On OSX 10.12, malloc_default_zone returns a special zone that is not\n\t * present in the list of registered zones. That zone uses a \"lite zone\"\n\t * if one is present (apparently enabled when malloc stack logging is\n\t * enabled), or the first registered zone otherwise. In practice this\n\t * means unless malloc stack logging is enabled, the first registered\n\t * zone is the default.  So get the list of zones to get the first one,\n\t * instead of relying on malloc_default_zone.\n\t */\n\tif (KERN_SUCCESS != malloc_get_all_zones(0, NULL,\n\t    (vm_address_t**)&zones, &num_zones)) {\n\t\t/*\n\t\t * Reset the value in case the failure happened after it was\n\t\t * set.\n\t\t */\n\t\tnum_zones = 0;\n\t}\n\n\tif (num_zones) {\n\t\treturn zones[0];\n\t}\n\n\treturn malloc_default_zone();\n}\n\n/* As written, this function can only promote jemalloc_zone. */\nstatic void\nzone_promote(void) {\n\tmalloc_zone_t *zone;\n\n\tdo {\n\t\t/*\n\t\t * Unregister and reregister the default zone.  On OSX >= 10.6,\n\t\t * unregistering takes the last registered zone and places it\n\t\t * at the location of the specified zone.  Unregistering the\n\t\t * default zone thus makes the last registered one the default.\n\t\t * On OSX < 10.6, unregistering shifts all registered zones.\n\t\t * The first registered zone then becomes the default.\n\t\t */\n\t\tmalloc_zone_unregister(default_zone);\n\t\tmalloc_zone_register(default_zone);\n\n\t\t/*\n\t\t * On OSX 10.6, having the default purgeable zone appear before\n\t\t * the default zone makes some things crash because it thinks it\n\t\t * owns the default zone allocated pointers.  We thus\n\t\t * unregister/re-register it in order to ensure it's always\n\t\t * after the default zone.  On OSX < 10.6, there is no purgeable\n\t\t * zone, so this does nothing.  On OSX >= 10.6, unregistering\n\t\t * replaces the purgeable zone with the last registered zone\n\t\t * above, i.e. the default zone.  Registering it again then puts\n\t\t * it at the end, obviously after the default zone.\n\t\t */\n\t\tif (purgeable_zone != NULL) {\n\t\t\tmalloc_zone_unregister(purgeable_zone);\n\t\t\tmalloc_zone_register(purgeable_zone);\n\t\t}\n\n\t\tzone = zone_default_get();\n\t} while (zone != &jemalloc_zone);\n}\n\nJEMALLOC_ATTR(constructor)\nvoid\nzone_register(void) {\n\t/*\n\t * If something else replaced the system default zone allocator, don't\n\t * register jemalloc's.\n\t */\n\tdefault_zone = zone_default_get();\n\tif (!default_zone->zone_name || strcmp(default_zone->zone_name,\n\t    \"DefaultMallocZone\") != 0) {\n\t\treturn;\n\t}\n\n\t/*\n\t * The default purgeable zone is created lazily by OSX's libc.  It uses\n\t * the default zone when it is created for \"small\" allocations\n\t * (< 15 KiB), but assumes the default zone is a scalable_zone.  This\n\t * obviously fails when the default zone is the jemalloc zone, so\n\t * malloc_default_purgeable_zone() is called beforehand so that the\n\t * default purgeable zone is created when the default zone is still\n\t * a scalable_zone.  As purgeable zones only exist on >= 10.6, we need\n\t * to check for the existence of malloc_default_purgeable_zone() at\n\t * run time.\n\t */\n\tpurgeable_zone = (malloc_default_purgeable_zone == NULL) ? NULL :\n\t    malloc_default_purgeable_zone();\n\n\t/* Register the custom zone.  At this point it won't be the default. */\n\tzone_init();\n\tmalloc_zone_register(&jemalloc_zone);\n\n\t/* Promote the custom zone to be default. */\n\tzone_promote();\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/include/test/SFMT-alti.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n/**\n * @file SFMT-alti.h\n *\n * @brief SIMD oriented Fast Mersenne Twister(SFMT)\n * pseudorandom number generator\n *\n * @author Mutsuo Saito (Hiroshima University)\n * @author Makoto Matsumoto (Hiroshima University)\n *\n * Copyright (C) 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n * University. All rights reserved.\n *\n * The new BSD License is applied to this software.\n * see LICENSE.txt\n */\n\n#ifndef SFMT_ALTI_H\n#define SFMT_ALTI_H\n\n/**\n * This function represents the recursion formula in AltiVec and BIG ENDIAN.\n * @param a a 128-bit part of the interal state array\n * @param b a 128-bit part of the interal state array\n * @param c a 128-bit part of the interal state array\n * @param d a 128-bit part of the interal state array\n * @return output\n */\nJEMALLOC_ALWAYS_INLINE\nvector unsigned int vec_recursion(vector unsigned int a,\n\t\t\t\t\t\tvector unsigned int b,\n\t\t\t\t\t\tvector unsigned int c,\n\t\t\t\t\t\tvector unsigned int d) {\n\n    const vector unsigned int sl1 = ALTI_SL1;\n    const vector unsigned int sr1 = ALTI_SR1;\n#ifdef ONLY64\n    const vector unsigned int mask = ALTI_MSK64;\n    const vector unsigned char perm_sl = ALTI_SL2_PERM64;\n    const vector unsigned char perm_sr = ALTI_SR2_PERM64;\n#else\n    const vector unsigned int mask = ALTI_MSK;\n    const vector unsigned char perm_sl = ALTI_SL2_PERM;\n    const vector unsigned char perm_sr = ALTI_SR2_PERM;\n#endif\n    vector unsigned int v, w, x, y, z;\n    x = vec_perm(a, (vector unsigned int)perm_sl, perm_sl);\n    v = a;\n    y = vec_sr(b, sr1);\n    z = vec_perm(c, (vector unsigned int)perm_sr, perm_sr);\n    w = vec_sl(d, sl1);\n    z = vec_xor(z, w);\n    y = vec_and(y, mask);\n    v = vec_xor(v, x);\n    z = vec_xor(z, y);\n    z = vec_xor(z, v);\n    return z;\n}\n\n/**\n * This function fills the internal state array with pseudorandom\n * integers.\n */\nstatic inline void gen_rand_all(sfmt_t *ctx) {\n    int i;\n    vector unsigned int r, r1, r2;\n\n    r1 = ctx->sfmt[N - 2].s;\n    r2 = ctx->sfmt[N - 1].s;\n    for (i = 0; i < N - POS1; i++) {\n\tr = vec_recursion(ctx->sfmt[i].s, ctx->sfmt[i + POS1].s, r1, r2);\n\tctx->sfmt[i].s = r;\n\tr1 = r2;\n\tr2 = r;\n    }\n    for (; i < N; i++) {\n\tr = vec_recursion(ctx->sfmt[i].s, ctx->sfmt[i + POS1 - N].s, r1, r2);\n\tctx->sfmt[i].s = r;\n\tr1 = r2;\n\tr2 = r;\n    }\n}\n\n/**\n * This function fills the user-specified array with pseudorandom\n * integers.\n *\n * @param array an 128-bit array to be filled by pseudorandom numbers.\n * @param size number of 128-bit pesudorandom numbers to be generated.\n */\nstatic inline void gen_rand_array(sfmt_t *ctx, w128_t *array, int size) {\n    int i, j;\n    vector unsigned int r, r1, r2;\n\n    r1 = ctx->sfmt[N - 2].s;\n    r2 = ctx->sfmt[N - 1].s;\n    for (i = 0; i < N - POS1; i++) {\n\tr = vec_recursion(ctx->sfmt[i].s, ctx->sfmt[i + POS1].s, r1, r2);\n\tarray[i].s = r;\n\tr1 = r2;\n\tr2 = r;\n    }\n    for (; i < N; i++) {\n\tr = vec_recursion(ctx->sfmt[i].s, array[i + POS1 - N].s, r1, r2);\n\tarray[i].s = r;\n\tr1 = r2;\n\tr2 = r;\n    }\n    /* main loop */\n    for (; i < size - N; i++) {\n\tr = vec_recursion(array[i - N].s, array[i + POS1 - N].s, r1, r2);\n\tarray[i].s = r;\n\tr1 = r2;\n\tr2 = r;\n    }\n    for (j = 0; j < 2 * N - size; j++) {\n\tctx->sfmt[j].s = array[j + size - N].s;\n    }\n    for (; i < size; i++) {\n\tr = vec_recursion(array[i - N].s, array[i + POS1 - N].s, r1, r2);\n\tarray[i].s = r;\n\tctx->sfmt[j++].s = r;\n\tr1 = r2;\n\tr2 = r;\n    }\n}\n\n#ifndef ONLY64\n#if defined(__APPLE__)\n#define ALTI_SWAP (vector unsigned char) \\\n\t(4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11)\n#else\n#define ALTI_SWAP {4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11}\n#endif\n/**\n * This function swaps high and low 32-bit of 64-bit integers in user\n * specified array.\n *\n * @param array an 128-bit array to be swaped.\n * @param size size of 128-bit array.\n */\nstatic inline void swap(w128_t *array, int size) {\n    int i;\n    const vector unsigned char perm = ALTI_SWAP;\n\n    for (i = 0; i < size; i++) {\n\tarray[i].s = vec_perm(array[i].s, (vector unsigned int)perm, perm);\n    }\n}\n#endif\n\n#endif\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/include/test/SFMT-params.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#ifndef SFMT_PARAMS_H\n#define SFMT_PARAMS_H\n\n#if !defined(MEXP)\n#ifdef __GNUC__\n  #warning \"MEXP is not defined. I assume MEXP is 19937.\"\n#endif\n  #define MEXP 19937\n#endif\n/*-----------------\n  BASIC DEFINITIONS\n  -----------------*/\n/** Mersenne Exponent. The period of the sequence \n *  is a multiple of 2^MEXP-1.\n * #define MEXP 19937 */\n/** SFMT generator has an internal state array of 128-bit integers,\n * and N is its size. */\n#define N (MEXP / 128 + 1)\n/** N32 is the size of internal state array when regarded as an array\n * of 32-bit integers.*/\n#define N32 (N * 4)\n/** N64 is the size of internal state array when regarded as an array\n * of 64-bit integers.*/\n#define N64 (N * 2)\n\n/*----------------------\n  the parameters of SFMT\n  following definitions are in paramsXXXX.h file.\n  ----------------------*/\n/** the pick up position of the array.\n#define POS1 122 \n*/\n\n/** the parameter of shift left as four 32-bit registers.\n#define SL1 18\n */\n\n/** the parameter of shift left as one 128-bit register. \n * The 128-bit integer is shifted by (SL2 * 8) bits. \n#define SL2 1 \n*/\n\n/** the parameter of shift right as four 32-bit registers.\n#define SR1 11\n*/\n\n/** the parameter of shift right as one 128-bit register. \n * The 128-bit integer is shifted by (SL2 * 8) bits. \n#define SR2 1 \n*/\n\n/** A bitmask, used in the recursion.  These parameters are introduced\n * to break symmetry of SIMD.\n#define MSK1 0xdfffffefU\n#define MSK2 0xddfecb7fU\n#define MSK3 0xbffaffffU\n#define MSK4 0xbffffff6U \n*/\n\n/** These definitions are part of a 128-bit period certification vector.\n#define PARITY1\t0x00000001U\n#define PARITY2\t0x00000000U\n#define PARITY3\t0x00000000U\n#define PARITY4\t0xc98e126aU\n*/\n\n#if MEXP == 607\n  #include \"test/SFMT-params607.h\"\n#elif MEXP == 1279\n  #include \"test/SFMT-params1279.h\"\n#elif MEXP == 2281\n  #include \"test/SFMT-params2281.h\"\n#elif MEXP == 4253\n  #include \"test/SFMT-params4253.h\"\n#elif MEXP == 11213\n  #include \"test/SFMT-params11213.h\"\n#elif MEXP == 19937\n  #include \"test/SFMT-params19937.h\"\n#elif MEXP == 44497\n  #include \"test/SFMT-params44497.h\"\n#elif MEXP == 86243\n  #include \"test/SFMT-params86243.h\"\n#elif MEXP == 132049\n  #include \"test/SFMT-params132049.h\"\n#elif MEXP == 216091\n  #include \"test/SFMT-params216091.h\"\n#else\n#ifdef __GNUC__\n  #error \"MEXP is not valid.\"\n  #undef MEXP\n#else\n  #undef MEXP\n#endif\n\n#endif\n\n#endif /* SFMT_PARAMS_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/include/test/SFMT-params11213.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#ifndef SFMT_PARAMS11213_H\n#define SFMT_PARAMS11213_H\n\n#define POS1\t68\n#define SL1\t14\n#define SL2\t3\n#define SR1\t7\n#define SR2\t3\n#define MSK1\t0xeffff7fbU\n#define MSK2\t0xffffffefU\n#define MSK3\t0xdfdfbfffU\n#define MSK4\t0x7fffdbfdU\n#define PARITY1\t0x00000001U\n#define PARITY2\t0x00000000U\n#define PARITY3\t0xe8148000U\n#define PARITY4\t0xd0c7afa3U\n\n\n/* PARAMETERS FOR ALTIVEC */\n#if defined(__APPLE__)\t/* For OSX */\n    #define ALTI_SL1\t(vector unsigned int)(SL1, SL1, SL1, SL1)\n    #define ALTI_SR1\t(vector unsigned int)(SR1, SR1, SR1, SR1)\n    #define ALTI_MSK\t(vector unsigned int)(MSK1, MSK2, MSK3, MSK4)\n    #define ALTI_MSK64 \\\n\t(vector unsigned int)(MSK2, MSK1, MSK4, MSK3)\n    #define ALTI_SL2_PERM \\\n\t(vector unsigned char)(3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10)\n    #define ALTI_SL2_PERM64 \\\n\t(vector unsigned char)(3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2)\n    #define ALTI_SR2_PERM \\\n\t(vector unsigned char)(5,6,7,0,9,10,11,4,13,14,15,8,19,19,19,12)\n    #define ALTI_SR2_PERM64 \\\n\t(vector unsigned char)(13,14,15,0,1,2,3,4,19,19,19,8,9,10,11,12)\n#else\t/* For OTHER OSs(Linux?) */\n    #define ALTI_SL1\t{SL1, SL1, SL1, SL1}\n    #define ALTI_SR1\t{SR1, SR1, SR1, SR1}\n    #define ALTI_MSK\t{MSK1, MSK2, MSK3, MSK4}\n    #define ALTI_MSK64\t{MSK2, MSK1, MSK4, MSK3}\n    #define ALTI_SL2_PERM\t{3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10}\n    #define ALTI_SL2_PERM64\t{3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2}\n    #define ALTI_SR2_PERM\t{5,6,7,0,9,10,11,4,13,14,15,8,19,19,19,12}\n    #define ALTI_SR2_PERM64\t{13,14,15,0,1,2,3,4,19,19,19,8,9,10,11,12}\n#endif\t/* For OSX */\n#define IDSTR\t\"SFMT-11213:68-14-3-7-3:effff7fb-ffffffef-dfdfbfff-7fffdbfd\"\n\n#endif /* SFMT_PARAMS11213_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/include/test/SFMT-params1279.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#ifndef SFMT_PARAMS1279_H\n#define SFMT_PARAMS1279_H\n\n#define POS1\t7\n#define SL1\t14\n#define SL2\t3\n#define SR1\t5\n#define SR2\t1\n#define MSK1\t0xf7fefffdU\n#define MSK2\t0x7fefcfffU\n#define MSK3\t0xaff3ef3fU\n#define MSK4\t0xb5ffff7fU\n#define PARITY1\t0x00000001U\n#define PARITY2\t0x00000000U\n#define PARITY3\t0x00000000U\n#define PARITY4\t0x20000000U\n\n\n/* PARAMETERS FOR ALTIVEC */\n#if defined(__APPLE__)\t/* For OSX */\n    #define ALTI_SL1\t(vector unsigned int)(SL1, SL1, SL1, SL1)\n    #define ALTI_SR1\t(vector unsigned int)(SR1, SR1, SR1, SR1)\n    #define ALTI_MSK\t(vector unsigned int)(MSK1, MSK2, MSK3, MSK4)\n    #define ALTI_MSK64 \\\n\t(vector unsigned int)(MSK2, MSK1, MSK4, MSK3)\n    #define ALTI_SL2_PERM \\\n\t(vector unsigned char)(3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10)\n    #define ALTI_SL2_PERM64 \\\n\t(vector unsigned char)(3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2)\n    #define ALTI_SR2_PERM \\\n\t(vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14)\n    #define ALTI_SR2_PERM64 \\\n\t(vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14)\n#else\t/* For OTHER OSs(Linux?) */\n    #define ALTI_SL1\t{SL1, SL1, SL1, SL1}\n    #define ALTI_SR1\t{SR1, SR1, SR1, SR1}\n    #define ALTI_MSK\t{MSK1, MSK2, MSK3, MSK4}\n    #define ALTI_MSK64\t{MSK2, MSK1, MSK4, MSK3}\n    #define ALTI_SL2_PERM\t{3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10}\n    #define ALTI_SL2_PERM64\t{3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2}\n    #define ALTI_SR2_PERM\t{7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14}\n    #define ALTI_SR2_PERM64\t{15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14}\n#endif\t/* For OSX */\n#define IDSTR\t\"SFMT-1279:7-14-3-5-1:f7fefffd-7fefcfff-aff3ef3f-b5ffff7f\"\n\n#endif /* SFMT_PARAMS1279_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/include/test/SFMT-params132049.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#ifndef SFMT_PARAMS132049_H\n#define SFMT_PARAMS132049_H\n\n#define POS1\t110\n#define SL1\t19\n#define SL2\t1\n#define SR1\t21\n#define SR2\t1\n#define MSK1\t0xffffbb5fU\n#define MSK2\t0xfb6ebf95U\n#define MSK3\t0xfffefffaU\n#define MSK4\t0xcff77fffU\n#define PARITY1\t0x00000001U\n#define PARITY2\t0x00000000U\n#define PARITY3\t0xcb520000U\n#define PARITY4\t0xc7e91c7dU\n\n\n/* PARAMETERS FOR ALTIVEC */\n#if defined(__APPLE__)\t/* For OSX */\n    #define ALTI_SL1\t(vector unsigned int)(SL1, SL1, SL1, SL1)\n    #define ALTI_SR1\t(vector unsigned int)(SR1, SR1, SR1, SR1)\n    #define ALTI_MSK\t(vector unsigned int)(MSK1, MSK2, MSK3, MSK4)\n    #define ALTI_MSK64 \\\n\t(vector unsigned int)(MSK2, MSK1, MSK4, MSK3)\n    #define ALTI_SL2_PERM \\\n\t(vector unsigned char)(1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8)\n    #define ALTI_SL2_PERM64 \\\n\t(vector unsigned char)(1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0)\n    #define ALTI_SR2_PERM \\\n\t(vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14)\n    #define ALTI_SR2_PERM64 \\\n\t(vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14)\n#else\t/* For OTHER OSs(Linux?) */\n    #define ALTI_SL1\t{SL1, SL1, SL1, SL1}\n    #define ALTI_SR1\t{SR1, SR1, SR1, SR1}\n    #define ALTI_MSK\t{MSK1, MSK2, MSK3, MSK4}\n    #define ALTI_MSK64\t{MSK2, MSK1, MSK4, MSK3}\n    #define ALTI_SL2_PERM\t{1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8}\n    #define ALTI_SL2_PERM64\t{1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0}\n    #define ALTI_SR2_PERM\t{7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14}\n    #define ALTI_SR2_PERM64\t{15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14}\n#endif\t/* For OSX */\n#define IDSTR\t\"SFMT-132049:110-19-1-21-1:ffffbb5f-fb6ebf95-fffefffa-cff77fff\"\n\n#endif /* SFMT_PARAMS132049_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/include/test/SFMT-params19937.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#ifndef SFMT_PARAMS19937_H\n#define SFMT_PARAMS19937_H\n\n#define POS1\t122\n#define SL1\t18\n#define SL2\t1\n#define SR1\t11\n#define SR2\t1\n#define MSK1\t0xdfffffefU\n#define MSK2\t0xddfecb7fU\n#define MSK3\t0xbffaffffU\n#define MSK4\t0xbffffff6U\n#define PARITY1\t0x00000001U\n#define PARITY2\t0x00000000U\n#define PARITY3\t0x00000000U\n#define PARITY4\t0x13c9e684U\n\n\n/* PARAMETERS FOR ALTIVEC */\n#if defined(__APPLE__)\t/* For OSX */\n    #define ALTI_SL1\t(vector unsigned int)(SL1, SL1, SL1, SL1)\n    #define ALTI_SR1\t(vector unsigned int)(SR1, SR1, SR1, SR1)\n    #define ALTI_MSK\t(vector unsigned int)(MSK1, MSK2, MSK3, MSK4)\n    #define ALTI_MSK64 \\\n\t(vector unsigned int)(MSK2, MSK1, MSK4, MSK3)\n    #define ALTI_SL2_PERM \\\n\t(vector unsigned char)(1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8)\n    #define ALTI_SL2_PERM64 \\\n\t(vector unsigned char)(1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0)\n    #define ALTI_SR2_PERM \\\n\t(vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14)\n    #define ALTI_SR2_PERM64 \\\n\t(vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14)\n#else\t/* For OTHER OSs(Linux?) */\n    #define ALTI_SL1\t{SL1, SL1, SL1, SL1}\n    #define ALTI_SR1\t{SR1, SR1, SR1, SR1}\n    #define ALTI_MSK\t{MSK1, MSK2, MSK3, MSK4}\n    #define ALTI_MSK64\t{MSK2, MSK1, MSK4, MSK3}\n    #define ALTI_SL2_PERM\t{1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8}\n    #define ALTI_SL2_PERM64\t{1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0}\n    #define ALTI_SR2_PERM\t{7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14}\n    #define ALTI_SR2_PERM64\t{15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14}\n#endif\t/* For OSX */\n#define IDSTR\t\"SFMT-19937:122-18-1-11-1:dfffffef-ddfecb7f-bffaffff-bffffff6\"\n\n#endif /* SFMT_PARAMS19937_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/include/test/SFMT-params216091.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#ifndef SFMT_PARAMS216091_H\n#define SFMT_PARAMS216091_H\n\n#define POS1\t627\n#define SL1\t11\n#define SL2\t3\n#define SR1\t10\n#define SR2\t1\n#define MSK1\t0xbff7bff7U\n#define MSK2\t0xbfffffffU\n#define MSK3\t0xbffffa7fU\n#define MSK4\t0xffddfbfbU\n#define PARITY1\t0xf8000001U\n#define PARITY2\t0x89e80709U\n#define PARITY3\t0x3bd2b64bU\n#define PARITY4\t0x0c64b1e4U\n\n\n/* PARAMETERS FOR ALTIVEC */\n#if defined(__APPLE__)\t/* For OSX */\n    #define ALTI_SL1\t(vector unsigned int)(SL1, SL1, SL1, SL1)\n    #define ALTI_SR1\t(vector unsigned int)(SR1, SR1, SR1, SR1)\n    #define ALTI_MSK\t(vector unsigned int)(MSK1, MSK2, MSK3, MSK4)\n    #define ALTI_MSK64 \\\n\t(vector unsigned int)(MSK2, MSK1, MSK4, MSK3)\n    #define ALTI_SL2_PERM \\\n\t(vector unsigned char)(3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10)\n    #define ALTI_SL2_PERM64 \\\n\t(vector unsigned char)(3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2)\n    #define ALTI_SR2_PERM \\\n\t(vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14)\n    #define ALTI_SR2_PERM64 \\\n\t(vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14)\n#else\t/* For OTHER OSs(Linux?) */\n    #define ALTI_SL1\t{SL1, SL1, SL1, SL1}\n    #define ALTI_SR1\t{SR1, SR1, SR1, SR1}\n    #define ALTI_MSK\t{MSK1, MSK2, MSK3, MSK4}\n    #define ALTI_MSK64\t{MSK2, MSK1, MSK4, MSK3}\n    #define ALTI_SL2_PERM\t{3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10}\n    #define ALTI_SL2_PERM64\t{3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2}\n    #define ALTI_SR2_PERM\t{7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14}\n    #define ALTI_SR2_PERM64\t{15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14}\n#endif\t/* For OSX */\n#define IDSTR\t\"SFMT-216091:627-11-3-10-1:bff7bff7-bfffffff-bffffa7f-ffddfbfb\"\n\n#endif /* SFMT_PARAMS216091_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/include/test/SFMT-params2281.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#ifndef SFMT_PARAMS2281_H\n#define SFMT_PARAMS2281_H\n\n#define POS1\t12\n#define SL1\t19\n#define SL2\t1\n#define SR1\t5\n#define SR2\t1\n#define MSK1\t0xbff7ffbfU\n#define MSK2\t0xfdfffffeU\n#define MSK3\t0xf7ffef7fU\n#define MSK4\t0xf2f7cbbfU\n#define PARITY1\t0x00000001U\n#define PARITY2\t0x00000000U\n#define PARITY3\t0x00000000U\n#define PARITY4\t0x41dfa600U\n\n\n/* PARAMETERS FOR ALTIVEC */\n#if defined(__APPLE__)\t/* For OSX */\n    #define ALTI_SL1\t(vector unsigned int)(SL1, SL1, SL1, SL1)\n    #define ALTI_SR1\t(vector unsigned int)(SR1, SR1, SR1, SR1)\n    #define ALTI_MSK\t(vector unsigned int)(MSK1, MSK2, MSK3, MSK4)\n    #define ALTI_MSK64 \\\n\t(vector unsigned int)(MSK2, MSK1, MSK4, MSK3)\n    #define ALTI_SL2_PERM \\\n\t(vector unsigned char)(1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8)\n    #define ALTI_SL2_PERM64 \\\n\t(vector unsigned char)(1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0)\n    #define ALTI_SR2_PERM \\\n\t(vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14)\n    #define ALTI_SR2_PERM64 \\\n\t(vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14)\n#else\t/* For OTHER OSs(Linux?) */\n    #define ALTI_SL1\t{SL1, SL1, SL1, SL1}\n    #define ALTI_SR1\t{SR1, SR1, SR1, SR1}\n    #define ALTI_MSK\t{MSK1, MSK2, MSK3, MSK4}\n    #define ALTI_MSK64\t{MSK2, MSK1, MSK4, MSK3}\n    #define ALTI_SL2_PERM\t{1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8}\n    #define ALTI_SL2_PERM64\t{1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0}\n    #define ALTI_SR2_PERM\t{7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14}\n    #define ALTI_SR2_PERM64\t{15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14}\n#endif\t/* For OSX */\n#define IDSTR\t\"SFMT-2281:12-19-1-5-1:bff7ffbf-fdfffffe-f7ffef7f-f2f7cbbf\"\n\n#endif /* SFMT_PARAMS2281_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/include/test/SFMT-params4253.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#ifndef SFMT_PARAMS4253_H\n#define SFMT_PARAMS4253_H\n\n#define POS1\t17\n#define SL1\t20\n#define SL2\t1\n#define SR1\t7\n#define SR2\t1\n#define MSK1\t0x9f7bffffU\n#define MSK2\t0x9fffff5fU\n#define MSK3\t0x3efffffbU\n#define MSK4\t0xfffff7bbU\n#define PARITY1\t0xa8000001U\n#define PARITY2\t0xaf5390a3U\n#define PARITY3\t0xb740b3f8U\n#define PARITY4\t0x6c11486dU\n\n\n/* PARAMETERS FOR ALTIVEC */\n#if defined(__APPLE__)\t/* For OSX */\n    #define ALTI_SL1\t(vector unsigned int)(SL1, SL1, SL1, SL1)\n    #define ALTI_SR1\t(vector unsigned int)(SR1, SR1, SR1, SR1)\n    #define ALTI_MSK\t(vector unsigned int)(MSK1, MSK2, MSK3, MSK4)\n    #define ALTI_MSK64 \\\n\t(vector unsigned int)(MSK2, MSK1, MSK4, MSK3)\n    #define ALTI_SL2_PERM \\\n\t(vector unsigned char)(1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8)\n    #define ALTI_SL2_PERM64 \\\n\t(vector unsigned char)(1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0)\n    #define ALTI_SR2_PERM \\\n\t(vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14)\n    #define ALTI_SR2_PERM64 \\\n\t(vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14)\n#else\t/* For OTHER OSs(Linux?) */\n    #define ALTI_SL1\t{SL1, SL1, SL1, SL1}\n    #define ALTI_SR1\t{SR1, SR1, SR1, SR1}\n    #define ALTI_MSK\t{MSK1, MSK2, MSK3, MSK4}\n    #define ALTI_MSK64\t{MSK2, MSK1, MSK4, MSK3}\n    #define ALTI_SL2_PERM\t{1,2,3,23,5,6,7,0,9,10,11,4,13,14,15,8}\n    #define ALTI_SL2_PERM64\t{1,2,3,4,5,6,7,31,9,10,11,12,13,14,15,0}\n    #define ALTI_SR2_PERM\t{7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14}\n    #define ALTI_SR2_PERM64\t{15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14}\n#endif\t/* For OSX */\n#define IDSTR\t\"SFMT-4253:17-20-1-7-1:9f7bffff-9fffff5f-3efffffb-fffff7bb\"\n\n#endif /* SFMT_PARAMS4253_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/include/test/SFMT-params44497.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#ifndef SFMT_PARAMS44497_H\n#define SFMT_PARAMS44497_H\n\n#define POS1\t330\n#define SL1\t5\n#define SL2\t3\n#define SR1\t9\n#define SR2\t3\n#define MSK1\t0xeffffffbU\n#define MSK2\t0xdfbebfffU\n#define MSK3\t0xbfbf7befU\n#define MSK4\t0x9ffd7bffU\n#define PARITY1\t0x00000001U\n#define PARITY2\t0x00000000U\n#define PARITY3\t0xa3ac4000U\n#define PARITY4\t0xecc1327aU\n\n\n/* PARAMETERS FOR ALTIVEC */\n#if defined(__APPLE__)\t/* For OSX */\n    #define ALTI_SL1\t(vector unsigned int)(SL1, SL1, SL1, SL1)\n    #define ALTI_SR1\t(vector unsigned int)(SR1, SR1, SR1, SR1)\n    #define ALTI_MSK\t(vector unsigned int)(MSK1, MSK2, MSK3, MSK4)\n    #define ALTI_MSK64 \\\n\t(vector unsigned int)(MSK2, MSK1, MSK4, MSK3)\n    #define ALTI_SL2_PERM \\\n\t(vector unsigned char)(3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10)\n    #define ALTI_SL2_PERM64 \\\n\t(vector unsigned char)(3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2)\n    #define ALTI_SR2_PERM \\\n\t(vector unsigned char)(5,6,7,0,9,10,11,4,13,14,15,8,19,19,19,12)\n    #define ALTI_SR2_PERM64 \\\n\t(vector unsigned char)(13,14,15,0,1,2,3,4,19,19,19,8,9,10,11,12)\n#else\t/* For OTHER OSs(Linux?) */\n    #define ALTI_SL1\t{SL1, SL1, SL1, SL1}\n    #define ALTI_SR1\t{SR1, SR1, SR1, SR1}\n    #define ALTI_MSK\t{MSK1, MSK2, MSK3, MSK4}\n    #define ALTI_MSK64\t{MSK2, MSK1, MSK4, MSK3}\n    #define ALTI_SL2_PERM\t{3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10}\n    #define ALTI_SL2_PERM64\t{3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2}\n    #define ALTI_SR2_PERM\t{5,6,7,0,9,10,11,4,13,14,15,8,19,19,19,12}\n    #define ALTI_SR2_PERM64\t{13,14,15,0,1,2,3,4,19,19,19,8,9,10,11,12}\n#endif\t/* For OSX */\n#define IDSTR\t\"SFMT-44497:330-5-3-9-3:effffffb-dfbebfff-bfbf7bef-9ffd7bff\"\n\n#endif /* SFMT_PARAMS44497_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/include/test/SFMT-params607.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#ifndef SFMT_PARAMS607_H\n#define SFMT_PARAMS607_H\n\n#define POS1\t2\n#define SL1\t15\n#define SL2\t3\n#define SR1\t13\n#define SR2\t3\n#define MSK1\t0xfdff37ffU\n#define MSK2\t0xef7f3f7dU\n#define MSK3\t0xff777b7dU\n#define MSK4\t0x7ff7fb2fU\n#define PARITY1\t0x00000001U\n#define PARITY2\t0x00000000U\n#define PARITY3\t0x00000000U\n#define PARITY4\t0x5986f054U\n\n\n/* PARAMETERS FOR ALTIVEC */\n#if defined(__APPLE__)\t/* For OSX */\n    #define ALTI_SL1\t(vector unsigned int)(SL1, SL1, SL1, SL1)\n    #define ALTI_SR1\t(vector unsigned int)(SR1, SR1, SR1, SR1)\n    #define ALTI_MSK\t(vector unsigned int)(MSK1, MSK2, MSK3, MSK4)\n    #define ALTI_MSK64 \\\n\t(vector unsigned int)(MSK2, MSK1, MSK4, MSK3)\n    #define ALTI_SL2_PERM \\\n\t(vector unsigned char)(3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10)\n    #define ALTI_SL2_PERM64 \\\n\t(vector unsigned char)(3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2)\n    #define ALTI_SR2_PERM \\\n\t(vector unsigned char)(5,6,7,0,9,10,11,4,13,14,15,8,19,19,19,12)\n    #define ALTI_SR2_PERM64 \\\n\t(vector unsigned char)(13,14,15,0,1,2,3,4,19,19,19,8,9,10,11,12)\n#else\t/* For OTHER OSs(Linux?) */\n    #define ALTI_SL1\t{SL1, SL1, SL1, SL1}\n    #define ALTI_SR1\t{SR1, SR1, SR1, SR1}\n    #define ALTI_MSK\t{MSK1, MSK2, MSK3, MSK4}\n    #define ALTI_MSK64\t{MSK2, MSK1, MSK4, MSK3}\n    #define ALTI_SL2_PERM\t{3,21,21,21,7,0,1,2,11,4,5,6,15,8,9,10}\n    #define ALTI_SL2_PERM64\t{3,4,5,6,7,29,29,29,11,12,13,14,15,0,1,2}\n    #define ALTI_SR2_PERM\t{5,6,7,0,9,10,11,4,13,14,15,8,19,19,19,12}\n    #define ALTI_SR2_PERM64\t{13,14,15,0,1,2,3,4,19,19,19,8,9,10,11,12}\n#endif\t/* For OSX */\n#define IDSTR\t\"SFMT-607:2-15-3-13-3:fdff37ff-ef7f3f7d-ff777b7d-7ff7fb2f\"\n\n#endif /* SFMT_PARAMS607_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/include/test/SFMT-params86243.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#ifndef SFMT_PARAMS86243_H\n#define SFMT_PARAMS86243_H\n\n#define POS1\t366\n#define SL1\t6\n#define SL2\t7\n#define SR1\t19\n#define SR2\t1\n#define MSK1\t0xfdbffbffU\n#define MSK2\t0xbff7ff3fU\n#define MSK3\t0xfd77efffU\n#define MSK4\t0xbf9ff3ffU\n#define PARITY1\t0x00000001U\n#define PARITY2\t0x00000000U\n#define PARITY3\t0x00000000U\n#define PARITY4\t0xe9528d85U\n\n\n/* PARAMETERS FOR ALTIVEC */\n#if defined(__APPLE__)\t/* For OSX */\n    #define ALTI_SL1\t(vector unsigned int)(SL1, SL1, SL1, SL1)\n    #define ALTI_SR1\t(vector unsigned int)(SR1, SR1, SR1, SR1)\n    #define ALTI_MSK\t(vector unsigned int)(MSK1, MSK2, MSK3, MSK4)\n    #define ALTI_MSK64 \\\n\t(vector unsigned int)(MSK2, MSK1, MSK4, MSK3)\n    #define ALTI_SL2_PERM \\\n\t(vector unsigned char)(25,25,25,25,3,25,25,25,7,0,1,2,11,4,5,6)\n    #define ALTI_SL2_PERM64 \\\n\t(vector unsigned char)(7,25,25,25,25,25,25,25,15,0,1,2,3,4,5,6)\n    #define ALTI_SR2_PERM \\\n\t(vector unsigned char)(7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14)\n    #define ALTI_SR2_PERM64 \\\n\t(vector unsigned char)(15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14)\n#else\t/* For OTHER OSs(Linux?) */\n    #define ALTI_SL1\t{SL1, SL1, SL1, SL1}\n    #define ALTI_SR1\t{SR1, SR1, SR1, SR1}\n    #define ALTI_MSK\t{MSK1, MSK2, MSK3, MSK4}\n    #define ALTI_MSK64\t{MSK2, MSK1, MSK4, MSK3}\n    #define ALTI_SL2_PERM\t{25,25,25,25,3,25,25,25,7,0,1,2,11,4,5,6}\n    #define ALTI_SL2_PERM64\t{7,25,25,25,25,25,25,25,15,0,1,2,3,4,5,6}\n    #define ALTI_SR2_PERM\t{7,0,1,2,11,4,5,6,15,8,9,10,17,12,13,14}\n    #define ALTI_SR2_PERM64\t{15,0,1,2,3,4,5,6,17,8,9,10,11,12,13,14}\n#endif\t/* For OSX */\n#define IDSTR\t\"SFMT-86243:366-6-7-19-1:fdbffbff-bff7ff3f-fd77efff-bf9ff3ff\"\n\n#endif /* SFMT_PARAMS86243_H */\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/include/test/SFMT-sse2.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n/**\n * @file  SFMT-sse2.h\n * @brief SIMD oriented Fast Mersenne Twister(SFMT) for Intel SSE2\n *\n * @author Mutsuo Saito (Hiroshima University)\n * @author Makoto Matsumoto (Hiroshima University)\n *\n * @note We assume LITTLE ENDIAN in this file\n *\n * Copyright (C) 2006, 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n * University. All rights reserved.\n *\n * The new BSD License is applied to this software, see LICENSE.txt\n */\n\n#ifndef SFMT_SSE2_H\n#define SFMT_SSE2_H\n\n/**\n * This function represents the recursion formula.\n * @param a a 128-bit part of the interal state array\n * @param b a 128-bit part of the interal state array\n * @param c a 128-bit part of the interal state array\n * @param d a 128-bit part of the interal state array\n * @param mask 128-bit mask\n * @return output\n */\nJEMALLOC_ALWAYS_INLINE __m128i mm_recursion(__m128i *a, __m128i *b,\n\t\t\t\t   __m128i c, __m128i d, __m128i mask) {\n    __m128i v, x, y, z;\n\n    x = _mm_load_si128(a);\n    y = _mm_srli_epi32(*b, SR1);\n    z = _mm_srli_si128(c, SR2);\n    v = _mm_slli_epi32(d, SL1);\n    z = _mm_xor_si128(z, x);\n    z = _mm_xor_si128(z, v);\n    x = _mm_slli_si128(x, SL2);\n    y = _mm_and_si128(y, mask);\n    z = _mm_xor_si128(z, x);\n    z = _mm_xor_si128(z, y);\n    return z;\n}\n\n/**\n * This function fills the internal state array with pseudorandom\n * integers.\n */\nstatic inline void gen_rand_all(sfmt_t *ctx) {\n    int i;\n    __m128i r, r1, r2, mask;\n    mask = _mm_set_epi32(MSK4, MSK3, MSK2, MSK1);\n\n    r1 = _mm_load_si128(&ctx->sfmt[N - 2].si);\n    r2 = _mm_load_si128(&ctx->sfmt[N - 1].si);\n    for (i = 0; i < N - POS1; i++) {\n\tr = mm_recursion(&ctx->sfmt[i].si, &ctx->sfmt[i + POS1].si, r1, r2,\n\t  mask);\n\t_mm_store_si128(&ctx->sfmt[i].si, r);\n\tr1 = r2;\n\tr2 = r;\n    }\n    for (; i < N; i++) {\n\tr = mm_recursion(&ctx->sfmt[i].si, &ctx->sfmt[i + POS1 - N].si, r1, r2,\n\t  mask);\n\t_mm_store_si128(&ctx->sfmt[i].si, r);\n\tr1 = r2;\n\tr2 = r;\n    }\n}\n\n/**\n * This function fills the user-specified array with pseudorandom\n * integers.\n *\n * @param array an 128-bit array to be filled by pseudorandom numbers.\n * @param size number of 128-bit pesudorandom numbers to be generated.\n */\nstatic inline void gen_rand_array(sfmt_t *ctx, w128_t *array, int size) {\n    int i, j;\n    __m128i r, r1, r2, mask;\n    mask = _mm_set_epi32(MSK4, MSK3, MSK2, MSK1);\n\n    r1 = _mm_load_si128(&ctx->sfmt[N - 2].si);\n    r2 = _mm_load_si128(&ctx->sfmt[N - 1].si);\n    for (i = 0; i < N - POS1; i++) {\n\tr = mm_recursion(&ctx->sfmt[i].si, &ctx->sfmt[i + POS1].si, r1, r2,\n\t  mask);\n\t_mm_store_si128(&array[i].si, r);\n\tr1 = r2;\n\tr2 = r;\n    }\n    for (; i < N; i++) {\n\tr = mm_recursion(&ctx->sfmt[i].si, &array[i + POS1 - N].si, r1, r2,\n\t  mask);\n\t_mm_store_si128(&array[i].si, r);\n\tr1 = r2;\n\tr2 = r;\n    }\n    /* main loop */\n    for (; i < size - N; i++) {\n\tr = mm_recursion(&array[i - N].si, &array[i + POS1 - N].si, r1, r2,\n\t\t\t mask);\n\t_mm_store_si128(&array[i].si, r);\n\tr1 = r2;\n\tr2 = r;\n    }\n    for (j = 0; j < 2 * N - size; j++) {\n\tr = _mm_load_si128(&array[j + size - N].si);\n\t_mm_store_si128(&ctx->sfmt[j].si, r);\n    }\n    for (; i < size; i++) {\n\tr = mm_recursion(&array[i - N].si, &array[i + POS1 - N].si, r1, r2,\n\t\t\t mask);\n\t_mm_store_si128(&array[i].si, r);\n\t_mm_store_si128(&ctx->sfmt[j++].si, r);\n\tr1 = r2;\n\tr2 = r;\n    }\n}\n\n#endif\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/include/test/SFMT.h",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n/** \n * @file SFMT.h \n *\n * @brief SIMD oriented Fast Mersenne Twister(SFMT) pseudorandom\n * number generator\n *\n * @author Mutsuo Saito (Hiroshima University)\n * @author Makoto Matsumoto (Hiroshima University)\n *\n * Copyright (C) 2006, 2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n * University. All rights reserved.\n *\n * The new BSD License is applied to this software.\n * see LICENSE.txt\n *\n * @note We assume that your system has inttypes.h.  If your system\n * doesn't have inttypes.h, you have to typedef uint32_t and uint64_t,\n * and you have to define PRIu64 and PRIx64 in this file as follows:\n * @verbatim\n typedef unsigned int uint32_t\n typedef unsigned long long uint64_t  \n #define PRIu64 \"llu\"\n #define PRIx64 \"llx\"\n@endverbatim\n * uint32_t must be exactly 32-bit unsigned integer type (no more, no\n * less), and uint64_t must be exactly 64-bit unsigned integer type.\n * PRIu64 and PRIx64 are used for printf function to print 64-bit\n * unsigned int and 64-bit unsigned int in hexadecimal format.\n */\n\n#ifndef SFMT_H\n#define SFMT_H\n\ntypedef struct sfmt_s sfmt_t;\n\nuint32_t gen_rand32(sfmt_t *ctx);\nuint32_t gen_rand32_range(sfmt_t *ctx, uint32_t limit);\nuint64_t gen_rand64(sfmt_t *ctx);\nuint64_t gen_rand64_range(sfmt_t *ctx, uint64_t limit);\nvoid fill_array32(sfmt_t *ctx, uint32_t *array, int size);\nvoid fill_array64(sfmt_t *ctx, uint64_t *array, int size);\nsfmt_t *init_gen_rand(uint32_t seed);\nsfmt_t *init_by_array(uint32_t *init_key, int key_length);\nvoid fini_gen_rand(sfmt_t *ctx);\nconst char *get_idstring(void);\nint get_min_array_size32(void);\nint get_min_array_size64(void);\n\n/* These real versions are due to Isaku Wada */\n/** generates a random number on [0,1]-real-interval */\nstatic inline double to_real1(uint32_t v) {\n    return v * (1.0/4294967295.0); \n    /* divided by 2^32-1 */ \n}\n\n/** generates a random number on [0,1]-real-interval */\nstatic inline double genrand_real1(sfmt_t *ctx) {\n    return to_real1(gen_rand32(ctx));\n}\n\n/** generates a random number on [0,1)-real-interval */\nstatic inline double to_real2(uint32_t v) {\n    return v * (1.0/4294967296.0); \n    /* divided by 2^32 */\n}\n\n/** generates a random number on [0,1)-real-interval */\nstatic inline double genrand_real2(sfmt_t *ctx) {\n    return to_real2(gen_rand32(ctx));\n}\n\n/** generates a random number on (0,1)-real-interval */\nstatic inline double to_real3(uint32_t v) {\n    return (((double)v) + 0.5)*(1.0/4294967296.0); \n    /* divided by 2^32 */\n}\n\n/** generates a random number on (0,1)-real-interval */\nstatic inline double genrand_real3(sfmt_t *ctx) {\n    return to_real3(gen_rand32(ctx));\n}\n/** These real versions are due to Isaku Wada */\n\n/** generates a random number on [0,1) with 53-bit resolution*/\nstatic inline double to_res53(uint64_t v) {\n    return v * (1.0/18446744073709551616.0L);\n}\n\n/** generates a random number on [0,1) with 53-bit resolution from two\n * 32 bit integers */\nstatic inline double to_res53_mix(uint32_t x, uint32_t y) {\n    return to_res53(x | ((uint64_t)y << 32));\n}\n\n/** generates a random number on [0,1) with 53-bit resolution\n */\nstatic inline double genrand_res53(sfmt_t *ctx) {\n    return to_res53(gen_rand64(ctx));\n}\n\n/** generates a random number on [0,1) with 53-bit resolution\n    using 32bit integer.\n */\nstatic inline double genrand_res53_mix(sfmt_t *ctx) {\n    uint32_t x, y;\n\n    x = gen_rand32(ctx);\n    y = gen_rand32(ctx);\n    return to_res53_mix(x, y);\n}\n#endif\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/include/test/btalloc.h",
    "content": "/* btalloc() provides a mechanism for allocating via permuted backtraces. */\nvoid\t*btalloc(size_t size, unsigned bits);\n\n#define btalloc_n_proto(n)\t\t\t\t\t\t\\\nvoid\t*btalloc_##n(size_t size, unsigned bits);\nbtalloc_n_proto(0)\nbtalloc_n_proto(1)\n\n#define btalloc_n_gen(n)\t\t\t\t\t\t\\\nvoid *\t\t\t\t\t\t\t\t\t\\\nbtalloc_##n(size_t size, unsigned bits) {\t\t\t\t\\\n\tvoid *p;\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tif (bits == 0) {\t\t\t\t\t\t\\\n\t\tp = mallocx(size, 0);\t\t\t\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t\tswitch (bits & 0x1U) {\t\t\t\t\t\\\n\t\tcase 0:\t\t\t\t\t\t\t\\\n\t\t\tp = (btalloc_0(size, bits >> 1));\t\t\\\n\t\t\tbreak;\t\t\t\t\t\t\\\n\t\tcase 1:\t\t\t\t\t\t\t\\\n\t\t\tp = (btalloc_1(size, bits >> 1));\t\t\\\n\t\t\tbreak;\t\t\t\t\t\t\\\n\t\tdefault: not_reached();\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\t/* Intentionally sabotage tail call optimization. */\t\t\\\n\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\t\t\\\n\treturn p;\t\t\t\t\t\t\t\\\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/include/test/extent_hooks.h",
    "content": "/*\n * Boilerplate code used for testing extent hooks via interception and\n * passthrough.\n */\n\nstatic void\t*extent_alloc_hook(extent_hooks_t *extent_hooks, void *new_addr,\n    size_t size, size_t alignment, bool *zero, bool *commit,\n    unsigned arena_ind);\nstatic bool\textent_dalloc_hook(extent_hooks_t *extent_hooks, void *addr,\n    size_t size, bool committed, unsigned arena_ind);\nstatic void\textent_destroy_hook(extent_hooks_t *extent_hooks, void *addr,\n    size_t size, bool committed, unsigned arena_ind);\nstatic bool\textent_commit_hook(extent_hooks_t *extent_hooks, void *addr,\n    size_t size, size_t offset, size_t length, unsigned arena_ind);\nstatic bool\textent_decommit_hook(extent_hooks_t *extent_hooks, void *addr,\n    size_t size, size_t offset, size_t length, unsigned arena_ind);\nstatic bool\textent_purge_lazy_hook(extent_hooks_t *extent_hooks, void *addr,\n    size_t size, size_t offset, size_t length, unsigned arena_ind);\nstatic bool\textent_purge_forced_hook(extent_hooks_t *extent_hooks,\n    void *addr, size_t size, size_t offset, size_t length, unsigned arena_ind);\nstatic bool\textent_split_hook(extent_hooks_t *extent_hooks, void *addr,\n    size_t size, size_t size_a, size_t size_b, bool committed,\n    unsigned arena_ind);\nstatic bool\textent_merge_hook(extent_hooks_t *extent_hooks, void *addr_a,\n    size_t size_a, void *addr_b, size_t size_b, bool committed,\n    unsigned arena_ind);\n\nstatic extent_hooks_t *default_hooks;\nstatic extent_hooks_t hooks = {\n\textent_alloc_hook,\n\textent_dalloc_hook,\n\textent_destroy_hook,\n\textent_commit_hook,\n\textent_decommit_hook,\n\textent_purge_lazy_hook,\n\textent_purge_forced_hook,\n\textent_split_hook,\n\textent_merge_hook\n};\n\n/* Control whether hook functions pass calls through to default hooks. */\nstatic bool try_alloc = true;\nstatic bool try_dalloc = true;\nstatic bool try_destroy = true;\nstatic bool try_commit = true;\nstatic bool try_decommit = true;\nstatic bool try_purge_lazy = true;\nstatic bool try_purge_forced = true;\nstatic bool try_split = true;\nstatic bool try_merge = true;\n\n/* Set to false prior to operations, then introspect after operations. */\nstatic bool called_alloc;\nstatic bool called_dalloc;\nstatic bool called_destroy;\nstatic bool called_commit;\nstatic bool called_decommit;\nstatic bool called_purge_lazy;\nstatic bool called_purge_forced;\nstatic bool called_split;\nstatic bool called_merge;\n\n/* Set to false prior to operations, then introspect after operations. */\nstatic bool did_alloc;\nstatic bool did_dalloc;\nstatic bool did_destroy;\nstatic bool did_commit;\nstatic bool did_decommit;\nstatic bool did_purge_lazy;\nstatic bool did_purge_forced;\nstatic bool did_split;\nstatic bool did_merge;\n\n#if 0\n#  define TRACE_HOOK(fmt, ...) malloc_printf(fmt, __VA_ARGS__)\n#else\n#  define TRACE_HOOK(fmt, ...)\n#endif\n\nstatic void *\nextent_alloc_hook(extent_hooks_t *extent_hooks, void *new_addr, size_t size,\n    size_t alignment, bool *zero, bool *commit, unsigned arena_ind) {\n\tvoid *ret;\n\n\tTRACE_HOOK(\"%s(extent_hooks=%p, new_addr=%p, size=%zu, alignment=%zu, \"\n\t    \"*zero=%s, *commit=%s, arena_ind=%u)\\n\", __func__, extent_hooks,\n\t    new_addr, size, alignment, *zero ?  \"true\" : \"false\", *commit ?\n\t    \"true\" : \"false\", arena_ind);\n\tassert_ptr_eq(extent_hooks, &hooks,\n\t    \"extent_hooks should be same as pointer used to set hooks\");\n\tassert_ptr_eq(extent_hooks->alloc, extent_alloc_hook,\n\t    \"Wrong hook function\");\n\tcalled_alloc = true;\n\tif (!try_alloc) {\n\t\treturn NULL;\n\t}\n\tret = default_hooks->alloc(default_hooks, new_addr, size, alignment,\n\t    zero, commit, 0);\n\tdid_alloc = (ret != NULL);\n\treturn ret;\n}\n\nstatic bool\nextent_dalloc_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    bool committed, unsigned arena_ind) {\n\tbool err;\n\n\tTRACE_HOOK(\"%s(extent_hooks=%p, addr=%p, size=%zu, committed=%s, \"\n\t    \"arena_ind=%u)\\n\", __func__, extent_hooks, addr, size, committed ?\n\t    \"true\" : \"false\", arena_ind);\n\tassert_ptr_eq(extent_hooks, &hooks,\n\t    \"extent_hooks should be same as pointer used to set hooks\");\n\tassert_ptr_eq(extent_hooks->dalloc, extent_dalloc_hook,\n\t    \"Wrong hook function\");\n\tcalled_dalloc = true;\n\tif (!try_dalloc) {\n\t\treturn true;\n\t}\n\terr = default_hooks->dalloc(default_hooks, addr, size, committed, 0);\n\tdid_dalloc = !err;\n\treturn err;\n}\n\nstatic void\nextent_destroy_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    bool committed, unsigned arena_ind) {\n\tTRACE_HOOK(\"%s(extent_hooks=%p, addr=%p, size=%zu, committed=%s, \"\n\t    \"arena_ind=%u)\\n\", __func__, extent_hooks, addr, size, committed ?\n\t    \"true\" : \"false\", arena_ind);\n\tassert_ptr_eq(extent_hooks, &hooks,\n\t    \"extent_hooks should be same as pointer used to set hooks\");\n\tassert_ptr_eq(extent_hooks->destroy, extent_destroy_hook,\n\t    \"Wrong hook function\");\n\tcalled_destroy = true;\n\tif (!try_destroy) {\n\t\treturn;\n\t}\n\tdefault_hooks->destroy(default_hooks, addr, size, committed, 0);\n\tdid_destroy = true;\n}\n\nstatic bool\nextent_commit_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    size_t offset, size_t length, unsigned arena_ind) {\n\tbool err;\n\n\tTRACE_HOOK(\"%s(extent_hooks=%p, addr=%p, size=%zu, offset=%zu, \"\n\t    \"length=%zu, arena_ind=%u)\\n\", __func__, extent_hooks, addr, size,\n\t    offset, length, arena_ind);\n\tassert_ptr_eq(extent_hooks, &hooks,\n\t    \"extent_hooks should be same as pointer used to set hooks\");\n\tassert_ptr_eq(extent_hooks->commit, extent_commit_hook,\n\t    \"Wrong hook function\");\n\tcalled_commit = true;\n\tif (!try_commit) {\n\t\treturn true;\n\t}\n\terr = default_hooks->commit(default_hooks, addr, size, offset, length,\n\t    0);\n\tdid_commit = !err;\n\treturn err;\n}\n\nstatic bool\nextent_decommit_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    size_t offset, size_t length, unsigned arena_ind) {\n\tbool err;\n\n\tTRACE_HOOK(\"%s(extent_hooks=%p, addr=%p, size=%zu, offset=%zu, \"\n\t    \"length=%zu, arena_ind=%u)\\n\", __func__, extent_hooks, addr, size,\n\t    offset, length, arena_ind);\n\tassert_ptr_eq(extent_hooks, &hooks,\n\t    \"extent_hooks should be same as pointer used to set hooks\");\n\tassert_ptr_eq(extent_hooks->decommit, extent_decommit_hook,\n\t    \"Wrong hook function\");\n\tcalled_decommit = true;\n\tif (!try_decommit) {\n\t\treturn true;\n\t}\n\terr = default_hooks->decommit(default_hooks, addr, size, offset, length,\n\t    0);\n\tdid_decommit = !err;\n\treturn err;\n}\n\nstatic bool\nextent_purge_lazy_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    size_t offset, size_t length, unsigned arena_ind) {\n\tbool err;\n\n\tTRACE_HOOK(\"%s(extent_hooks=%p, addr=%p, size=%zu, offset=%zu, \"\n\t    \"length=%zu arena_ind=%u)\\n\", __func__, extent_hooks, addr, size,\n\t    offset, length, arena_ind);\n\tassert_ptr_eq(extent_hooks, &hooks,\n\t    \"extent_hooks should be same as pointer used to set hooks\");\n\tassert_ptr_eq(extent_hooks->purge_lazy, extent_purge_lazy_hook,\n\t    \"Wrong hook function\");\n\tcalled_purge_lazy = true;\n\tif (!try_purge_lazy) {\n\t\treturn true;\n\t}\n\terr = default_hooks->purge_lazy == NULL ||\n\t    default_hooks->purge_lazy(default_hooks, addr, size, offset, length,\n\t    0);\n\tdid_purge_lazy = !err;\n\treturn err;\n}\n\nstatic bool\nextent_purge_forced_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    size_t offset, size_t length, unsigned arena_ind) {\n\tbool err;\n\n\tTRACE_HOOK(\"%s(extent_hooks=%p, addr=%p, size=%zu, offset=%zu, \"\n\t    \"length=%zu arena_ind=%u)\\n\", __func__, extent_hooks, addr, size,\n\t    offset, length, arena_ind);\n\tassert_ptr_eq(extent_hooks, &hooks,\n\t    \"extent_hooks should be same as pointer used to set hooks\");\n\tassert_ptr_eq(extent_hooks->purge_forced, extent_purge_forced_hook,\n\t    \"Wrong hook function\");\n\tcalled_purge_forced = true;\n\tif (!try_purge_forced) {\n\t\treturn true;\n\t}\n\terr = default_hooks->purge_forced == NULL ||\n\t    default_hooks->purge_forced(default_hooks, addr, size, offset,\n\t    length, 0);\n\tdid_purge_forced = !err;\n\treturn err;\n}\n\nstatic bool\nextent_split_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    size_t size_a, size_t size_b, bool committed, unsigned arena_ind) {\n\tbool err;\n\n\tTRACE_HOOK(\"%s(extent_hooks=%p, addr=%p, size=%zu, size_a=%zu, \"\n\t    \"size_b=%zu, committed=%s, arena_ind=%u)\\n\", __func__, extent_hooks,\n\t    addr, size, size_a, size_b, committed ? \"true\" : \"false\",\n\t    arena_ind);\n\tassert_ptr_eq(extent_hooks, &hooks,\n\t    \"extent_hooks should be same as pointer used to set hooks\");\n\tassert_ptr_eq(extent_hooks->split, extent_split_hook,\n\t    \"Wrong hook function\");\n\tcalled_split = true;\n\tif (!try_split) {\n\t\treturn true;\n\t}\n\terr = (default_hooks->split == NULL ||\n\t    default_hooks->split(default_hooks, addr, size, size_a, size_b,\n\t    committed, 0));\n\tdid_split = !err;\n\treturn err;\n}\n\nstatic bool\nextent_merge_hook(extent_hooks_t *extent_hooks, void *addr_a, size_t size_a,\n    void *addr_b, size_t size_b, bool committed, unsigned arena_ind) {\n\tbool err;\n\n\tTRACE_HOOK(\"%s(extent_hooks=%p, addr_a=%p, size_a=%zu, addr_b=%p \"\n\t    \"size_b=%zu, committed=%s, arena_ind=%u)\\n\", __func__, extent_hooks,\n\t    addr_a, size_a, addr_b, size_b, committed ? \"true\" : \"false\",\n\t    arena_ind);\n\tassert_ptr_eq(extent_hooks, &hooks,\n\t    \"extent_hooks should be same as pointer used to set hooks\");\n\tassert_ptr_eq(extent_hooks->merge, extent_merge_hook,\n\t    \"Wrong hook function\");\n\tcalled_merge = true;\n\tif (!try_merge) {\n\t\treturn true;\n\t}\n\terr = (default_hooks->merge == NULL ||\n\t    default_hooks->merge(default_hooks, addr_a, size_a, addr_b, size_b,\n\t    committed, 0));\n\tdid_merge = !err;\n\treturn err;\n}\n\nstatic void\nextent_hooks_prep(void) {\n\tsize_t sz;\n\n\tsz = sizeof(default_hooks);\n\tassert_d_eq(mallctl(\"arena.0.extent_hooks\", (void *)&default_hooks, &sz,\n\t    NULL, 0), 0, \"Unexpected mallctl() error\");\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/include/test/jemalloc_test.h.in",
    "content": "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#include <limits.h>\n#ifndef SIZE_T_MAX\n#  define SIZE_T_MAX\tSIZE_MAX\n#endif\n#include <stdlib.h>\n#include <stdarg.h>\n#include <stdbool.h>\n#include <errno.h>\n#include <math.h>\n#include <string.h>\n#ifdef _WIN32\n#  include \"msvc_compat/strings.h\"\n#endif\n\n#ifdef _WIN32\n#  include <windows.h>\n#  include \"msvc_compat/windows_extra.h\"\n#else\n#  include <pthread.h>\n#endif\n\n#include \"test/jemalloc_test_defs.h\"\n\n#ifdef JEMALLOC_OSSPIN\n#  include <libkern/OSAtomic.h>\n#endif\n\n#if defined(HAVE_ALTIVEC) && !defined(__APPLE__)\n#  include <altivec.h>\n#endif\n#ifdef HAVE_SSE2\n#  include <emmintrin.h>\n#endif\n\n/******************************************************************************/\n/*\n * For unit tests, expose all public and private interfaces.\n */\n#ifdef JEMALLOC_UNIT_TEST\n#  define JEMALLOC_JET\n#  define JEMALLOC_MANGLE\n#  include \"jemalloc/internal/jemalloc_preamble.h\"\n#  include \"jemalloc/internal/jemalloc_internal_includes.h\"\n\n/******************************************************************************/\n/*\n * For integration tests, expose the public jemalloc interfaces, but only\n * expose the minimum necessary internal utility code (to avoid re-implementing\n * essentially identical code within the test infrastructure).\n */\n#elif defined(JEMALLOC_INTEGRATION_TEST) || \\\n    defined(JEMALLOC_INTEGRATION_CPP_TEST)\n#  define JEMALLOC_MANGLE\n#  include \"jemalloc/jemalloc@install_suffix@.h\"\n#  include \"jemalloc/internal/jemalloc_internal_defs.h\"\n#  include \"jemalloc/internal/jemalloc_internal_macros.h\"\n\nstatic const bool config_debug =\n#ifdef JEMALLOC_DEBUG\n    true\n#else\n    false\n#endif\n    ;\n\n#  define JEMALLOC_N(n) @private_namespace@##n\n#  include \"jemalloc/internal/private_namespace.h\"\n#  include \"jemalloc/internal/hooks.h\"\n\n/* Hermetic headers. */\n#  include \"jemalloc/internal/assert.h\"\n#  include \"jemalloc/internal/malloc_io.h\"\n#  include \"jemalloc/internal/nstime.h\"\n#  include \"jemalloc/internal/util.h\"\n\n/* Non-hermetic headers. */\n#  include \"jemalloc/internal/qr.h\"\n#  include \"jemalloc/internal/ql.h\"\n\n/******************************************************************************/\n/*\n * For stress tests, expose the public jemalloc interfaces with name mangling\n * so that they can be tested as e.g. malloc() and free().  Also expose the\n * public jemalloc interfaces with jet_ prefixes, so that stress tests can use\n * a separate allocator for their internal data structures.\n */\n#elif defined(JEMALLOC_STRESS_TEST)\n#  include \"jemalloc/jemalloc@install_suffix@.h\"\n\n#  include \"jemalloc/jemalloc_protos_jet.h\"\n\n#  define JEMALLOC_JET\n#  include \"jemalloc/internal/jemalloc_preamble.h\"\n#  include \"jemalloc/internal/jemalloc_internal_includes.h\"\n#  include \"jemalloc/internal/public_unnamespace.h\"\n#  undef JEMALLOC_JET\n\n#  include \"jemalloc/jemalloc_rename.h\"\n#  define JEMALLOC_MANGLE\n#  ifdef JEMALLOC_STRESS_TESTLIB\n#    include \"jemalloc/jemalloc_mangle_jet.h\"\n#  else\n#    include \"jemalloc/jemalloc_mangle.h\"\n#  endif\n\n/******************************************************************************/\n/*\n * This header does dangerous things, the effects of which only test code\n * should be subject to.\n */\n#else\n#  error \"This header cannot be included outside a testing context\"\n#endif\n\n/******************************************************************************/\n/*\n * Common test utilities.\n */\n#include \"test/btalloc.h\"\n#include \"test/math.h\"\n#include \"test/mtx.h\"\n#include \"test/mq.h\"\n#include \"test/test.h\"\n#include \"test/timer.h\"\n#include \"test/thd.h\"\n#define MEXP 19937\n#include \"test/SFMT.h\"\n\n/******************************************************************************/\n/*\n * Define always-enabled assertion macros, so that test assertions execute even\n * if assertions are disabled in the library code.\n */\n#undef assert\n#undef not_reached\n#undef not_implemented\n#undef assert_not_implemented\n\n#define assert(e) do {\t\t\t\t\t\t\t\\\n\tif (!(e)) {\t\t\t\t\t\t\t\\\n\t\tmalloc_printf(\t\t\t\t\t\t\\\n\t\t    \"<jemalloc>: %s:%d: Failed assertion: \\\"%s\\\"\\n\",\t\\\n\t\t    __FILE__, __LINE__, #e);\t\t\t\t\\\n\t\tabort();\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define not_reached() do {\t\t\t\t\t\t\\\n\tmalloc_printf(\t\t\t\t\t\t\t\\\n\t    \"<jemalloc>: %s:%d: Unreachable code reached\\n\",\t\t\\\n\t    __FILE__, __LINE__);\t\t\t\t\t\\\n\tabort();\t\t\t\t\t\t\t\\\n} while (0)\n\n#define not_implemented() do {\t\t\t\t\t\t\\\n\tmalloc_printf(\"<jemalloc>: %s:%d: Not implemented\\n\",\t\t\\\n\t    __FILE__, __LINE__);\t\t\t\t\t\\\n\tabort();\t\t\t\t\t\t\t\\\n} while (0)\n\n#define assert_not_implemented(e) do {\t\t\t\t\t\\\n\tif (!(e)) {\t\t\t\t\t\t\t\\\n\t\tnot_implemented();\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#ifdef __cplusplus\n}\n#endif\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/include/test/jemalloc_test_defs.h.in",
    "content": "#include \"jemalloc/internal/jemalloc_internal_defs.h\"\n#include \"jemalloc/internal/jemalloc_internal_decls.h\"\n\n/*\n * For use by SFMT.  configure.ac doesn't actually define HAVE_SSE2 because its\n * dependencies are notoriously unportable in practice.\n */\n#undef HAVE_SSE2\n#undef HAVE_ALTIVEC\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/include/test/math.h",
    "content": "/*\n * Compute the natural log of Gamma(x), accurate to 10 decimal places.\n *\n * This implementation is based on:\n *\n *   Pike, M.C., I.D. Hill (1966) Algorithm 291: Logarithm of Gamma function\n *   [S14].  Communications of the ACM 9(9):684.\n */\nstatic inline double\nln_gamma(double x) {\n\tdouble f, z;\n\n\tassert(x > 0.0);\n\n\tif (x < 7.0) {\n\t\tf = 1.0;\n\t\tz = x;\n\t\twhile (z < 7.0) {\n\t\t\tf *= z;\n\t\t\tz += 1.0;\n\t\t}\n\t\tx = z;\n\t\tf = -log(f);\n\t} else {\n\t\tf = 0.0;\n\t}\n\n\tz = 1.0 / (x * x);\n\n\treturn f + (x-0.5) * log(x) - x + 0.918938533204673 +\n\t    (((-0.000595238095238 * z + 0.000793650793651) * z -\n\t    0.002777777777778) * z + 0.083333333333333) / x;\n}\n\n/*\n * Compute the incomplete Gamma ratio for [0..x], where p is the shape\n * parameter, and ln_gamma_p is ln_gamma(p).\n *\n * This implementation is based on:\n *\n *   Bhattacharjee, G.P. (1970) Algorithm AS 32: The incomplete Gamma integral.\n *   Applied Statistics 19:285-287.\n */\nstatic inline double\ni_gamma(double x, double p, double ln_gamma_p) {\n\tdouble acu, factor, oflo, gin, term, rn, a, b, an, dif;\n\tdouble pn[6];\n\tunsigned i;\n\n\tassert(p > 0.0);\n\tassert(x >= 0.0);\n\n\tif (x == 0.0) {\n\t\treturn 0.0;\n\t}\n\n\tacu = 1.0e-10;\n\toflo = 1.0e30;\n\tgin = 0.0;\n\tfactor = exp(p * log(x) - x - ln_gamma_p);\n\n\tif (x <= 1.0 || x < p) {\n\t\t/* Calculation by series expansion. */\n\t\tgin = 1.0;\n\t\tterm = 1.0;\n\t\trn = p;\n\n\t\twhile (true) {\n\t\t\trn += 1.0;\n\t\t\tterm *= x / rn;\n\t\t\tgin += term;\n\t\t\tif (term <= acu) {\n\t\t\t\tgin *= factor / p;\n\t\t\t\treturn gin;\n\t\t\t}\n\t\t}\n\t} else {\n\t\t/* Calculation by continued fraction. */\n\t\ta = 1.0 - p;\n\t\tb = a + x + 1.0;\n\t\tterm = 0.0;\n\t\tpn[0] = 1.0;\n\t\tpn[1] = x;\n\t\tpn[2] = x + 1.0;\n\t\tpn[3] = x * b;\n\t\tgin = pn[2] / pn[3];\n\n\t\twhile (true) {\n\t\t\ta += 1.0;\n\t\t\tb += 2.0;\n\t\t\tterm += 1.0;\n\t\t\tan = a * term;\n\t\t\tfor (i = 0; i < 2; i++) {\n\t\t\t\tpn[i+4] = b * pn[i+2] - an * pn[i];\n\t\t\t}\n\t\t\tif (pn[5] != 0.0) {\n\t\t\t\trn = pn[4] / pn[5];\n\t\t\t\tdif = fabs(gin - rn);\n\t\t\t\tif (dif <= acu && dif <= acu * rn) {\n\t\t\t\t\tgin = 1.0 - factor * gin;\n\t\t\t\t\treturn gin;\n\t\t\t\t}\n\t\t\t\tgin = rn;\n\t\t\t}\n\t\t\tfor (i = 0; i < 4; i++) {\n\t\t\t\tpn[i] = pn[i+2];\n\t\t\t}\n\n\t\t\tif (fabs(pn[4]) >= oflo) {\n\t\t\t\tfor (i = 0; i < 4; i++) {\n\t\t\t\t\tpn[i] /= oflo;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n\n/*\n * Given a value p in [0..1] of the lower tail area of the normal distribution,\n * compute the limit on the definite integral from [-inf..z] that satisfies p,\n * accurate to 16 decimal places.\n *\n * This implementation is based on:\n *\n *   Wichura, M.J. (1988) Algorithm AS 241: The percentage points of the normal\n *   distribution.  Applied Statistics 37(3):477-484.\n */\nstatic inline double\npt_norm(double p) {\n\tdouble q, r, ret;\n\n\tassert(p > 0.0 && p < 1.0);\n\n\tq = p - 0.5;\n\tif (fabs(q) <= 0.425) {\n\t\t/* p close to 1/2. */\n\t\tr = 0.180625 - q * q;\n\t\treturn q * (((((((2.5090809287301226727e3 * r +\n\t\t    3.3430575583588128105e4) * r + 6.7265770927008700853e4) * r\n\t\t    + 4.5921953931549871457e4) * r + 1.3731693765509461125e4) *\n\t\t    r + 1.9715909503065514427e3) * r + 1.3314166789178437745e2)\n\t\t    * r + 3.3871328727963666080e0) /\n\t\t    (((((((5.2264952788528545610e3 * r +\n\t\t    2.8729085735721942674e4) * r + 3.9307895800092710610e4) * r\n\t\t    + 2.1213794301586595867e4) * r + 5.3941960214247511077e3) *\n\t\t    r + 6.8718700749205790830e2) * r + 4.2313330701600911252e1)\n\t\t    * r + 1.0);\n\t} else {\n\t\tif (q < 0.0) {\n\t\t\tr = p;\n\t\t} else {\n\t\t\tr = 1.0 - p;\n\t\t}\n\t\tassert(r > 0.0);\n\n\t\tr = sqrt(-log(r));\n\t\tif (r <= 5.0) {\n\t\t\t/* p neither close to 1/2 nor 0 or 1. */\n\t\t\tr -= 1.6;\n\t\t\tret = ((((((((7.74545014278341407640e-4 * r +\n\t\t\t    2.27238449892691845833e-2) * r +\n\t\t\t    2.41780725177450611770e-1) * r +\n\t\t\t    1.27045825245236838258e0) * r +\n\t\t\t    3.64784832476320460504e0) * r +\n\t\t\t    5.76949722146069140550e0) * r +\n\t\t\t    4.63033784615654529590e0) * r +\n\t\t\t    1.42343711074968357734e0) /\n\t\t\t    (((((((1.05075007164441684324e-9 * r +\n\t\t\t    5.47593808499534494600e-4) * r +\n\t\t\t    1.51986665636164571966e-2)\n\t\t\t    * r + 1.48103976427480074590e-1) * r +\n\t\t\t    6.89767334985100004550e-1) * r +\n\t\t\t    1.67638483018380384940e0) * r +\n\t\t\t    2.05319162663775882187e0) * r + 1.0));\n\t\t} else {\n\t\t\t/* p near 0 or 1. */\n\t\t\tr -= 5.0;\n\t\t\tret = ((((((((2.01033439929228813265e-7 * r +\n\t\t\t    2.71155556874348757815e-5) * r +\n\t\t\t    1.24266094738807843860e-3) * r +\n\t\t\t    2.65321895265761230930e-2) * r +\n\t\t\t    2.96560571828504891230e-1) * r +\n\t\t\t    1.78482653991729133580e0) * r +\n\t\t\t    5.46378491116411436990e0) * r +\n\t\t\t    6.65790464350110377720e0) /\n\t\t\t    (((((((2.04426310338993978564e-15 * r +\n\t\t\t    1.42151175831644588870e-7) * r +\n\t\t\t    1.84631831751005468180e-5) * r +\n\t\t\t    7.86869131145613259100e-4) * r +\n\t\t\t    1.48753612908506148525e-2) * r +\n\t\t\t    1.36929880922735805310e-1) * r +\n\t\t\t    5.99832206555887937690e-1)\n\t\t\t    * r + 1.0));\n\t\t}\n\t\tif (q < 0.0) {\n\t\t\tret = -ret;\n\t\t}\n\t\treturn ret;\n\t}\n}\n\n/*\n * Given a value p in [0..1] of the lower tail area of the Chi^2 distribution\n * with df degrees of freedom, where ln_gamma_df_2 is ln_gamma(df/2.0), compute\n * the upper limit on the definite integral from [0..z] that satisfies p,\n * accurate to 12 decimal places.\n *\n * This implementation is based on:\n *\n *   Best, D.J., D.E. Roberts (1975) Algorithm AS 91: The percentage points of\n *   the Chi^2 distribution.  Applied Statistics 24(3):385-388.\n *\n *   Shea, B.L. (1991) Algorithm AS R85: A remark on AS 91: The percentage\n *   points of the Chi^2 distribution.  Applied Statistics 40(1):233-235.\n */\nstatic inline double\npt_chi2(double p, double df, double ln_gamma_df_2) {\n\tdouble e, aa, xx, c, ch, a, q, p1, p2, t, x, b, s1, s2, s3, s4, s5, s6;\n\tunsigned i;\n\n\tassert(p >= 0.0 && p < 1.0);\n\tassert(df > 0.0);\n\n\te = 5.0e-7;\n\taa = 0.6931471805;\n\n\txx = 0.5 * df;\n\tc = xx - 1.0;\n\n\tif (df < -1.24 * log(p)) {\n\t\t/* Starting approximation for small Chi^2. */\n\t\tch = pow(p * xx * exp(ln_gamma_df_2 + xx * aa), 1.0 / xx);\n\t\tif (ch - e < 0.0) {\n\t\t\treturn ch;\n\t\t}\n\t} else {\n\t\tif (df > 0.32) {\n\t\t\tx = pt_norm(p);\n\t\t\t/*\n\t\t\t * Starting approximation using Wilson and Hilferty\n\t\t\t * estimate.\n\t\t\t */\n\t\t\tp1 = 0.222222 / df;\n\t\t\tch = df * pow(x * sqrt(p1) + 1.0 - p1, 3.0);\n\t\t\t/* Starting approximation for p tending to 1. */\n\t\t\tif (ch > 2.2 * df + 6.0) {\n\t\t\t\tch = -2.0 * (log(1.0 - p) - c * log(0.5 * ch) +\n\t\t\t\t    ln_gamma_df_2);\n\t\t\t}\n\t\t} else {\n\t\t\tch = 0.4;\n\t\t\ta = log(1.0 - p);\n\t\t\twhile (true) {\n\t\t\t\tq = ch;\n\t\t\t\tp1 = 1.0 + ch * (4.67 + ch);\n\t\t\t\tp2 = ch * (6.73 + ch * (6.66 + ch));\n\t\t\t\tt = -0.5 + (4.67 + 2.0 * ch) / p1 - (6.73 + ch\n\t\t\t\t    * (13.32 + 3.0 * ch)) / p2;\n\t\t\t\tch -= (1.0 - exp(a + ln_gamma_df_2 + 0.5 * ch +\n\t\t\t\t    c * aa) * p2 / p1) / t;\n\t\t\t\tif (fabs(q / ch - 1.0) - 0.01 <= 0.0) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tfor (i = 0; i < 20; i++) {\n\t\t/* Calculation of seven-term Taylor series. */\n\t\tq = ch;\n\t\tp1 = 0.5 * ch;\n\t\tif (p1 < 0.0) {\n\t\t\treturn -1.0;\n\t\t}\n\t\tp2 = p - i_gamma(p1, xx, ln_gamma_df_2);\n\t\tt = p2 * exp(xx * aa + ln_gamma_df_2 + p1 - c * log(ch));\n\t\tb = t / ch;\n\t\ta = 0.5 * t - b * c;\n\t\ts1 = (210.0 + a * (140.0 + a * (105.0 + a * (84.0 + a * (70.0 +\n\t\t    60.0 * a))))) / 420.0;\n\t\ts2 = (420.0 + a * (735.0 + a * (966.0 + a * (1141.0 + 1278.0 *\n\t\t    a)))) / 2520.0;\n\t\ts3 = (210.0 + a * (462.0 + a * (707.0 + 932.0 * a))) / 2520.0;\n\t\ts4 = (252.0 + a * (672.0 + 1182.0 * a) + c * (294.0 + a *\n\t\t    (889.0 + 1740.0 * a))) / 5040.0;\n\t\ts5 = (84.0 + 264.0 * a + c * (175.0 + 606.0 * a)) / 2520.0;\n\t\ts6 = (120.0 + c * (346.0 + 127.0 * c)) / 5040.0;\n\t\tch += t * (1.0 + 0.5 * t * s1 - b * c * (s1 - b * (s2 - b * (s3\n\t\t    - b * (s4 - b * (s5 - b * s6))))));\n\t\tif (fabs(q / ch - 1.0) <= e) {\n\t\t\tbreak;\n\t\t}\n\t}\n\n\treturn ch;\n}\n\n/*\n * Given a value p in [0..1] and Gamma distribution shape and scale parameters,\n * compute the upper limit on the definite integral from [0..z] that satisfies\n * p.\n */\nstatic inline double\npt_gamma(double p, double shape, double scale, double ln_gamma_shape) {\n\treturn pt_chi2(p, shape * 2.0, ln_gamma_shape) * 0.5 * scale;\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/include/test/mq.h",
    "content": "void\tmq_nanosleep(unsigned ns);\n\n/*\n * Simple templated message queue implementation that relies on only mutexes for\n * synchronization (which reduces portability issues).  Given the following\n * setup:\n *\n *   typedef struct mq_msg_s mq_msg_t;\n *   struct mq_msg_s {\n *           mq_msg(mq_msg_t) link;\n *           [message data]\n *   };\n *   mq_gen(, mq_, mq_t, mq_msg_t, link)\n *\n * The API is as follows:\n *\n *   bool mq_init(mq_t *mq);\n *   void mq_fini(mq_t *mq);\n *   unsigned mq_count(mq_t *mq);\n *   mq_msg_t *mq_tryget(mq_t *mq);\n *   mq_msg_t *mq_get(mq_t *mq);\n *   void mq_put(mq_t *mq, mq_msg_t *msg);\n *\n * The message queue linkage embedded in each message is to be treated as\n * externally opaque (no need to initialize or clean up externally).  mq_fini()\n * does not perform any cleanup of messages, since it knows nothing of their\n * payloads.\n */\n#define mq_msg(a_mq_msg_type)\tql_elm(a_mq_msg_type)\n\n#define mq_gen(a_attr, a_prefix, a_mq_type, a_mq_msg_type, a_field)\t\\\ntypedef struct {\t\t\t\t\t\t\t\\\n\tmtx_t\t\t\tlock;\t\t\t\t\t\\\n\tql_head(a_mq_msg_type)\tmsgs;\t\t\t\t\t\\\n\tunsigned\t\tcount;\t\t\t\t\t\\\n} a_mq_type;\t\t\t\t\t\t\t\t\\\na_attr bool\t\t\t\t\t\t\t\t\\\na_prefix##init(a_mq_type *mq) {\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tif (mtx_init(&mq->lock)) {\t\t\t\t\t\\\n\t\treturn true;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tql_new(&mq->msgs);\t\t\t\t\t\t\\\n\tmq->count = 0;\t\t\t\t\t\t\t\\\n\treturn false;\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##fini(a_mq_type *mq) {\t\t\t\t\t\t\\\n\tmtx_fini(&mq->lock);\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr unsigned\t\t\t\t\t\t\t\t\\\na_prefix##count(a_mq_type *mq) {\t\t\t\t\t\\\n\tunsigned count;\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tmtx_lock(&mq->lock);\t\t\t\t\t\t\\\n\tcount = mq->count;\t\t\t\t\t\t\\\n\tmtx_unlock(&mq->lock);\t\t\t\t\t\t\\\n\treturn count;\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_mq_msg_type *\t\t\t\t\t\t\t\\\na_prefix##tryget(a_mq_type *mq) {\t\t\t\t\t\\\n\ta_mq_msg_type *msg;\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tmtx_lock(&mq->lock);\t\t\t\t\t\t\\\n\tmsg = ql_first(&mq->msgs);\t\t\t\t\t\\\n\tif (msg != NULL) {\t\t\t\t\t\t\\\n\t\tql_head_remove(&mq->msgs, a_mq_msg_type, a_field);\t\\\n\t\tmq->count--;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tmtx_unlock(&mq->lock);\t\t\t\t\t\t\\\n\treturn msg;\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr a_mq_msg_type *\t\t\t\t\t\t\t\\\na_prefix##get(a_mq_type *mq) {\t\t\t\t\t\t\\\n\ta_mq_msg_type *msg;\t\t\t\t\t\t\\\n\tunsigned ns;\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tmsg = a_prefix##tryget(mq);\t\t\t\t\t\\\n\tif (msg != NULL) {\t\t\t\t\t\t\\\n\t\treturn msg;\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tns = 1;\t\t\t\t\t\t\t\t\\\n\twhile (true) {\t\t\t\t\t\t\t\\\n\t\tmq_nanosleep(ns);\t\t\t\t\t\\\n\t\tmsg = a_prefix##tryget(mq);\t\t\t\t\\\n\t\tif (msg != NULL) {\t\t\t\t\t\\\n\t\t\treturn msg;\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t\tif (ns < 1000*1000*1000) {\t\t\t\t\\\n\t\t\t/* Double sleep time, up to max 1 second. */\t\\\n\t\t\tns <<= 1;\t\t\t\t\t\\\n\t\t\tif (ns > 1000*1000*1000) {\t\t\t\\\n\t\t\t\tns = 1000*1000*1000;\t\t\t\\\n\t\t\t}\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n}\t\t\t\t\t\t\t\t\t\\\na_attr void\t\t\t\t\t\t\t\t\\\na_prefix##put(a_mq_type *mq, a_mq_msg_type *msg) {\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tmtx_lock(&mq->lock);\t\t\t\t\t\t\\\n\tql_elm_new(msg, a_field);\t\t\t\t\t\\\n\tql_tail_insert(&mq->msgs, msg, a_field);\t\t\t\\\n\tmq->count++;\t\t\t\t\t\t\t\\\n\tmtx_unlock(&mq->lock);\t\t\t\t\t\t\\\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/include/test/mtx.h",
    "content": "/*\n * mtx is a slightly simplified version of malloc_mutex.  This code duplication\n * is unfortunate, but there are allocator bootstrapping considerations that\n * would leak into the test infrastructure if malloc_mutex were used directly\n * in tests.\n */\n\ntypedef struct {\n#ifdef _WIN32\n\tCRITICAL_SECTION\tlock;\n#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))\n\tos_unfair_lock\t\tlock;\n#elif (defined(JEMALLOC_OSSPIN))\n\tOSSpinLock\t\tlock;\n#else\n\tpthread_mutex_t\t\tlock;\n#endif\n} mtx_t;\n\nbool\tmtx_init(mtx_t *mtx);\nvoid\tmtx_fini(mtx_t *mtx);\nvoid\tmtx_lock(mtx_t *mtx);\nvoid\tmtx_unlock(mtx_t *mtx);\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/include/test/test.h",
    "content": "#define ASSERT_BUFSIZE\t256\n\n#define assert_cmp(t, a, b, cmp, neg_cmp, pri, ...) do {\t\t\\\n\tt a_ = (a);\t\t\t\t\t\t\t\\\n\tt b_ = (b);\t\t\t\t\t\t\t\\\n\tif (!(a_ cmp b_)) {\t\t\t\t\t\t\\\n\t\tchar prefix[ASSERT_BUFSIZE];\t\t\t\t\\\n\t\tchar message[ASSERT_BUFSIZE];\t\t\t\t\\\n\t\tmalloc_snprintf(prefix, sizeof(prefix),\t\t\t\\\n\t\t    \"%s:%s:%d: Failed assertion: \"\t\t\t\\\n\t\t    \"(%s) \" #cmp \" (%s) --> \"\t\t\t\t\\\n\t\t    \"%\" pri \" \" #neg_cmp \" %\" pri \": \",\t\t\t\\\n\t\t    __func__, __FILE__, __LINE__,\t\t\t\\\n\t\t    #a, #b, a_, b_);\t\t\t\t\t\\\n\t\tmalloc_snprintf(message, sizeof(message), __VA_ARGS__);\t\\\n\t\tp_test_fail(prefix, message);\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define assert_ptr_eq(a, b, ...)\tassert_cmp(void *, a, b, ==,\t\\\n    !=, \"p\", __VA_ARGS__)\n#define assert_ptr_ne(a, b, ...)\tassert_cmp(void *, a, b, !=,\t\\\n    ==, \"p\", __VA_ARGS__)\n#define assert_ptr_null(a, ...)\t\tassert_cmp(void *, a, NULL, ==,\t\\\n    !=, \"p\", __VA_ARGS__)\n#define assert_ptr_not_null(a, ...)\tassert_cmp(void *, a, NULL, !=,\t\\\n    ==, \"p\", __VA_ARGS__)\n\n#define assert_c_eq(a, b, ...)\tassert_cmp(char, a, b, ==, !=, \"c\", __VA_ARGS__)\n#define assert_c_ne(a, b, ...)\tassert_cmp(char, a, b, !=, ==, \"c\", __VA_ARGS__)\n#define assert_c_lt(a, b, ...)\tassert_cmp(char, a, b, <, >=, \"c\", __VA_ARGS__)\n#define assert_c_le(a, b, ...)\tassert_cmp(char, a, b, <=, >, \"c\", __VA_ARGS__)\n#define assert_c_ge(a, b, ...)\tassert_cmp(char, a, b, >=, <, \"c\", __VA_ARGS__)\n#define assert_c_gt(a, b, ...)\tassert_cmp(char, a, b, >, <=, \"c\", __VA_ARGS__)\n\n#define assert_x_eq(a, b, ...)\tassert_cmp(int, a, b, ==, !=, \"#x\", __VA_ARGS__)\n#define assert_x_ne(a, b, ...)\tassert_cmp(int, a, b, !=, ==, \"#x\", __VA_ARGS__)\n#define assert_x_lt(a, b, ...)\tassert_cmp(int, a, b, <, >=, \"#x\", __VA_ARGS__)\n#define assert_x_le(a, b, ...)\tassert_cmp(int, a, b, <=, >, \"#x\", __VA_ARGS__)\n#define assert_x_ge(a, b, ...)\tassert_cmp(int, a, b, >=, <, \"#x\", __VA_ARGS__)\n#define assert_x_gt(a, b, ...)\tassert_cmp(int, a, b, >, <=, \"#x\", __VA_ARGS__)\n\n#define assert_d_eq(a, b, ...)\tassert_cmp(int, a, b, ==, !=, \"d\", __VA_ARGS__)\n#define assert_d_ne(a, b, ...)\tassert_cmp(int, a, b, !=, ==, \"d\", __VA_ARGS__)\n#define assert_d_lt(a, b, ...)\tassert_cmp(int, a, b, <, >=, \"d\", __VA_ARGS__)\n#define assert_d_le(a, b, ...)\tassert_cmp(int, a, b, <=, >, \"d\", __VA_ARGS__)\n#define assert_d_ge(a, b, ...)\tassert_cmp(int, a, b, >=, <, \"d\", __VA_ARGS__)\n#define assert_d_gt(a, b, ...)\tassert_cmp(int, a, b, >, <=, \"d\", __VA_ARGS__)\n\n#define assert_u_eq(a, b, ...)\tassert_cmp(int, a, b, ==, !=, \"u\", __VA_ARGS__)\n#define assert_u_ne(a, b, ...)\tassert_cmp(int, a, b, !=, ==, \"u\", __VA_ARGS__)\n#define assert_u_lt(a, b, ...)\tassert_cmp(int, a, b, <, >=, \"u\", __VA_ARGS__)\n#define assert_u_le(a, b, ...)\tassert_cmp(int, a, b, <=, >, \"u\", __VA_ARGS__)\n#define assert_u_ge(a, b, ...)\tassert_cmp(int, a, b, >=, <, \"u\", __VA_ARGS__)\n#define assert_u_gt(a, b, ...)\tassert_cmp(int, a, b, >, <=, \"u\", __VA_ARGS__)\n\n#define assert_ld_eq(a, b, ...)\tassert_cmp(long, a, b, ==,\t\\\n    !=, \"ld\", __VA_ARGS__)\n#define assert_ld_ne(a, b, ...)\tassert_cmp(long, a, b, !=,\t\\\n    ==, \"ld\", __VA_ARGS__)\n#define assert_ld_lt(a, b, ...)\tassert_cmp(long, a, b, <,\t\\\n    >=, \"ld\", __VA_ARGS__)\n#define assert_ld_le(a, b, ...)\tassert_cmp(long, a, b, <=,\t\\\n    >, \"ld\", __VA_ARGS__)\n#define assert_ld_ge(a, b, ...)\tassert_cmp(long, a, b, >=,\t\\\n    <, \"ld\", __VA_ARGS__)\n#define assert_ld_gt(a, b, ...)\tassert_cmp(long, a, b, >,\t\\\n    <=, \"ld\", __VA_ARGS__)\n\n#define assert_lu_eq(a, b, ...)\tassert_cmp(unsigned long,\t\\\n    a, b, ==, !=, \"lu\", __VA_ARGS__)\n#define assert_lu_ne(a, b, ...)\tassert_cmp(unsigned long,\t\\\n    a, b, !=, ==, \"lu\", __VA_ARGS__)\n#define assert_lu_lt(a, b, ...)\tassert_cmp(unsigned long,\t\\\n    a, b, <, >=, \"lu\", __VA_ARGS__)\n#define assert_lu_le(a, b, ...)\tassert_cmp(unsigned long,\t\\\n    a, b, <=, >, \"lu\", __VA_ARGS__)\n#define assert_lu_ge(a, b, ...)\tassert_cmp(unsigned long,\t\\\n    a, b, >=, <, \"lu\", __VA_ARGS__)\n#define assert_lu_gt(a, b, ...)\tassert_cmp(unsigned long,\t\\\n    a, b, >, <=, \"lu\", __VA_ARGS__)\n\n#define assert_qd_eq(a, b, ...)\tassert_cmp(long long, a, b, ==,\t\\\n    !=, \"qd\", __VA_ARGS__)\n#define assert_qd_ne(a, b, ...)\tassert_cmp(long long, a, b, !=,\t\\\n    ==, \"qd\", __VA_ARGS__)\n#define assert_qd_lt(a, b, ...)\tassert_cmp(long long, a, b, <,\t\\\n    >=, \"qd\", __VA_ARGS__)\n#define assert_qd_le(a, b, ...)\tassert_cmp(long long, a, b, <=,\t\\\n    >, \"qd\", __VA_ARGS__)\n#define assert_qd_ge(a, b, ...)\tassert_cmp(long long, a, b, >=,\t\\\n    <, \"qd\", __VA_ARGS__)\n#define assert_qd_gt(a, b, ...)\tassert_cmp(long long, a, b, >,\t\\\n    <=, \"qd\", __VA_ARGS__)\n\n#define assert_qu_eq(a, b, ...)\tassert_cmp(unsigned long long,\t\\\n    a, b, ==, !=, \"qu\", __VA_ARGS__)\n#define assert_qu_ne(a, b, ...)\tassert_cmp(unsigned long long,\t\\\n    a, b, !=, ==, \"qu\", __VA_ARGS__)\n#define assert_qu_lt(a, b, ...)\tassert_cmp(unsigned long long,\t\\\n    a, b, <, >=, \"qu\", __VA_ARGS__)\n#define assert_qu_le(a, b, ...)\tassert_cmp(unsigned long long,\t\\\n    a, b, <=, >, \"qu\", __VA_ARGS__)\n#define assert_qu_ge(a, b, ...)\tassert_cmp(unsigned long long,\t\\\n    a, b, >=, <, \"qu\", __VA_ARGS__)\n#define assert_qu_gt(a, b, ...)\tassert_cmp(unsigned long long,\t\\\n    a, b, >, <=, \"qu\", __VA_ARGS__)\n\n#define assert_jd_eq(a, b, ...)\tassert_cmp(intmax_t, a, b, ==,\t\\\n    !=, \"jd\", __VA_ARGS__)\n#define assert_jd_ne(a, b, ...)\tassert_cmp(intmax_t, a, b, !=,\t\\\n    ==, \"jd\", __VA_ARGS__)\n#define assert_jd_lt(a, b, ...)\tassert_cmp(intmax_t, a, b, <,\t\\\n    >=, \"jd\", __VA_ARGS__)\n#define assert_jd_le(a, b, ...)\tassert_cmp(intmax_t, a, b, <=,\t\\\n    >, \"jd\", __VA_ARGS__)\n#define assert_jd_ge(a, b, ...)\tassert_cmp(intmax_t, a, b, >=,\t\\\n    <, \"jd\", __VA_ARGS__)\n#define assert_jd_gt(a, b, ...)\tassert_cmp(intmax_t, a, b, >,\t\\\n    <=, \"jd\", __VA_ARGS__)\n\n#define assert_ju_eq(a, b, ...)\tassert_cmp(uintmax_t, a, b, ==,\t\\\n    !=, \"ju\", __VA_ARGS__)\n#define assert_ju_ne(a, b, ...)\tassert_cmp(uintmax_t, a, b, !=,\t\\\n    ==, \"ju\", __VA_ARGS__)\n#define assert_ju_lt(a, b, ...)\tassert_cmp(uintmax_t, a, b, <,\t\\\n    >=, \"ju\", __VA_ARGS__)\n#define assert_ju_le(a, b, ...)\tassert_cmp(uintmax_t, a, b, <=,\t\\\n    >, \"ju\", __VA_ARGS__)\n#define assert_ju_ge(a, b, ...)\tassert_cmp(uintmax_t, a, b, >=,\t\\\n    <, \"ju\", __VA_ARGS__)\n#define assert_ju_gt(a, b, ...)\tassert_cmp(uintmax_t, a, b, >,\t\\\n    <=, \"ju\", __VA_ARGS__)\n\n#define assert_zd_eq(a, b, ...)\tassert_cmp(ssize_t, a, b, ==,\t\\\n    !=, \"zd\", __VA_ARGS__)\n#define assert_zd_ne(a, b, ...)\tassert_cmp(ssize_t, a, b, !=,\t\\\n    ==, \"zd\", __VA_ARGS__)\n#define assert_zd_lt(a, b, ...)\tassert_cmp(ssize_t, a, b, <,\t\\\n    >=, \"zd\", __VA_ARGS__)\n#define assert_zd_le(a, b, ...)\tassert_cmp(ssize_t, a, b, <=,\t\\\n    >, \"zd\", __VA_ARGS__)\n#define assert_zd_ge(a, b, ...)\tassert_cmp(ssize_t, a, b, >=,\t\\\n    <, \"zd\", __VA_ARGS__)\n#define assert_zd_gt(a, b, ...)\tassert_cmp(ssize_t, a, b, >,\t\\\n    <=, \"zd\", __VA_ARGS__)\n\n#define assert_zu_eq(a, b, ...)\tassert_cmp(size_t, a, b, ==,\t\\\n    !=, \"zu\", __VA_ARGS__)\n#define assert_zu_ne(a, b, ...)\tassert_cmp(size_t, a, b, !=,\t\\\n    ==, \"zu\", __VA_ARGS__)\n#define assert_zu_lt(a, b, ...)\tassert_cmp(size_t, a, b, <,\t\\\n    >=, \"zu\", __VA_ARGS__)\n#define assert_zu_le(a, b, ...)\tassert_cmp(size_t, a, b, <=,\t\\\n    >, \"zu\", __VA_ARGS__)\n#define assert_zu_ge(a, b, ...)\tassert_cmp(size_t, a, b, >=,\t\\\n    <, \"zu\", __VA_ARGS__)\n#define assert_zu_gt(a, b, ...)\tassert_cmp(size_t, a, b, >,\t\\\n    <=, \"zu\", __VA_ARGS__)\n\n#define assert_d32_eq(a, b, ...)\tassert_cmp(int32_t, a, b, ==,\t\\\n    !=, FMTd32, __VA_ARGS__)\n#define assert_d32_ne(a, b, ...)\tassert_cmp(int32_t, a, b, !=,\t\\\n    ==, FMTd32, __VA_ARGS__)\n#define assert_d32_lt(a, b, ...)\tassert_cmp(int32_t, a, b, <,\t\\\n    >=, FMTd32, __VA_ARGS__)\n#define assert_d32_le(a, b, ...)\tassert_cmp(int32_t, a, b, <=,\t\\\n    >, FMTd32, __VA_ARGS__)\n#define assert_d32_ge(a, b, ...)\tassert_cmp(int32_t, a, b, >=,\t\\\n    <, FMTd32, __VA_ARGS__)\n#define assert_d32_gt(a, b, ...)\tassert_cmp(int32_t, a, b, >,\t\\\n    <=, FMTd32, __VA_ARGS__)\n\n#define assert_u32_eq(a, b, ...)\tassert_cmp(uint32_t, a, b, ==,\t\\\n    !=, FMTu32, __VA_ARGS__)\n#define assert_u32_ne(a, b, ...)\tassert_cmp(uint32_t, a, b, !=,\t\\\n    ==, FMTu32, __VA_ARGS__)\n#define assert_u32_lt(a, b, ...)\tassert_cmp(uint32_t, a, b, <,\t\\\n    >=, FMTu32, __VA_ARGS__)\n#define assert_u32_le(a, b, ...)\tassert_cmp(uint32_t, a, b, <=,\t\\\n    >, FMTu32, __VA_ARGS__)\n#define assert_u32_ge(a, b, ...)\tassert_cmp(uint32_t, a, b, >=,\t\\\n    <, FMTu32, __VA_ARGS__)\n#define assert_u32_gt(a, b, ...)\tassert_cmp(uint32_t, a, b, >,\t\\\n    <=, FMTu32, __VA_ARGS__)\n\n#define assert_d64_eq(a, b, ...)\tassert_cmp(int64_t, a, b, ==,\t\\\n    !=, FMTd64, __VA_ARGS__)\n#define assert_d64_ne(a, b, ...)\tassert_cmp(int64_t, a, b, !=,\t\\\n    ==, FMTd64, __VA_ARGS__)\n#define assert_d64_lt(a, b, ...)\tassert_cmp(int64_t, a, b, <,\t\\\n    >=, FMTd64, __VA_ARGS__)\n#define assert_d64_le(a, b, ...)\tassert_cmp(int64_t, a, b, <=,\t\\\n    >, FMTd64, __VA_ARGS__)\n#define assert_d64_ge(a, b, ...)\tassert_cmp(int64_t, a, b, >=,\t\\\n    <, FMTd64, __VA_ARGS__)\n#define assert_d64_gt(a, b, ...)\tassert_cmp(int64_t, a, b, >,\t\\\n    <=, FMTd64, __VA_ARGS__)\n\n#define assert_u64_eq(a, b, ...)\tassert_cmp(uint64_t, a, b, ==,\t\\\n    !=, FMTu64, __VA_ARGS__)\n#define assert_u64_ne(a, b, ...)\tassert_cmp(uint64_t, a, b, !=,\t\\\n    ==, FMTu64, __VA_ARGS__)\n#define assert_u64_lt(a, b, ...)\tassert_cmp(uint64_t, a, b, <,\t\\\n    >=, FMTu64, __VA_ARGS__)\n#define assert_u64_le(a, b, ...)\tassert_cmp(uint64_t, a, b, <=,\t\\\n    >, FMTu64, __VA_ARGS__)\n#define assert_u64_ge(a, b, ...)\tassert_cmp(uint64_t, a, b, >=,\t\\\n    <, FMTu64, __VA_ARGS__)\n#define assert_u64_gt(a, b, ...)\tassert_cmp(uint64_t, a, b, >,\t\\\n    <=, FMTu64, __VA_ARGS__)\n\n#define assert_b_eq(a, b, ...) do {\t\t\t\t\t\\\n\tbool a_ = (a);\t\t\t\t\t\t\t\\\n\tbool b_ = (b);\t\t\t\t\t\t\t\\\n\tif (!(a_ == b_)) {\t\t\t\t\t\t\\\n\t\tchar prefix[ASSERT_BUFSIZE];\t\t\t\t\\\n\t\tchar message[ASSERT_BUFSIZE];\t\t\t\t\\\n\t\tmalloc_snprintf(prefix, sizeof(prefix),\t\t\t\\\n\t\t    \"%s:%s:%d: Failed assertion: \"\t\t\t\\\n\t\t    \"(%s) == (%s) --> %s != %s: \",\t\t\t\\\n\t\t    __func__, __FILE__, __LINE__,\t\t\t\\\n\t\t    #a, #b, a_ ? \"true\" : \"false\",\t\t\t\\\n\t\t    b_ ? \"true\" : \"false\");\t\t\t\t\\\n\t\tmalloc_snprintf(message, sizeof(message), __VA_ARGS__);\t\\\n\t\tp_test_fail(prefix, message);\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n#define assert_b_ne(a, b, ...) do {\t\t\t\t\t\\\n\tbool a_ = (a);\t\t\t\t\t\t\t\\\n\tbool b_ = (b);\t\t\t\t\t\t\t\\\n\tif (!(a_ != b_)) {\t\t\t\t\t\t\\\n\t\tchar prefix[ASSERT_BUFSIZE];\t\t\t\t\\\n\t\tchar message[ASSERT_BUFSIZE];\t\t\t\t\\\n\t\tmalloc_snprintf(prefix, sizeof(prefix),\t\t\t\\\n\t\t    \"%s:%s:%d: Failed assertion: \"\t\t\t\\\n\t\t    \"(%s) != (%s) --> %s == %s: \",\t\t\t\\\n\t\t    __func__, __FILE__, __LINE__,\t\t\t\\\n\t\t    #a, #b, a_ ? \"true\" : \"false\",\t\t\t\\\n\t\t    b_ ? \"true\" : \"false\");\t\t\t\t\\\n\t\tmalloc_snprintf(message, sizeof(message), __VA_ARGS__);\t\\\n\t\tp_test_fail(prefix, message);\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n#define assert_true(a, ...)\tassert_b_eq(a, true, __VA_ARGS__)\n#define assert_false(a, ...)\tassert_b_eq(a, false, __VA_ARGS__)\n\n#define assert_str_eq(a, b, ...) do {\t\t\t\t\\\n\tif (strcmp((a), (b))) {\t\t\t\t\t\t\\\n\t\tchar prefix[ASSERT_BUFSIZE];\t\t\t\t\\\n\t\tchar message[ASSERT_BUFSIZE];\t\t\t\t\\\n\t\tmalloc_snprintf(prefix, sizeof(prefix),\t\t\t\\\n\t\t    \"%s:%s:%d: Failed assertion: \"\t\t\t\\\n\t\t    \"(%s) same as (%s) --> \"\t\t\t\t\\\n\t\t    \"\\\"%s\\\" differs from \\\"%s\\\": \",\t\t\t\\\n\t\t    __func__, __FILE__, __LINE__, #a, #b, a, b);\t\\\n\t\tmalloc_snprintf(message, sizeof(message), __VA_ARGS__);\t\\\n\t\tp_test_fail(prefix, message);\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n#define assert_str_ne(a, b, ...) do {\t\t\t\t\\\n\tif (!strcmp((a), (b))) {\t\t\t\t\t\\\n\t\tchar prefix[ASSERT_BUFSIZE];\t\t\t\t\\\n\t\tchar message[ASSERT_BUFSIZE];\t\t\t\t\\\n\t\tmalloc_snprintf(prefix, sizeof(prefix),\t\t\t\\\n\t\t    \"%s:%s:%d: Failed assertion: \"\t\t\t\\\n\t\t    \"(%s) differs from (%s) --> \"\t\t\t\\\n\t\t    \"\\\"%s\\\" same as \\\"%s\\\": \",\t\t\t\t\\\n\t\t    __func__, __FILE__, __LINE__, #a, #b, a, b);\t\\\n\t\tmalloc_snprintf(message, sizeof(message), __VA_ARGS__);\t\\\n\t\tp_test_fail(prefix, message);\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define assert_not_reached(...) do {\t\t\t\t\t\\\n\tchar prefix[ASSERT_BUFSIZE];\t\t\t\t\t\\\n\tchar message[ASSERT_BUFSIZE];\t\t\t\t\t\\\n\tmalloc_snprintf(prefix, sizeof(prefix),\t\t\t\t\\\n\t    \"%s:%s:%d: Unreachable code reached: \",\t\t\t\\\n\t    __func__, __FILE__, __LINE__);\t\t\t\t\\\n\tmalloc_snprintf(message, sizeof(message), __VA_ARGS__);\t\t\\\n\tp_test_fail(prefix, message);\t\t\t\t\t\\\n} while (0)\n\n/*\n * If this enum changes, corresponding changes in test/test.sh.in are also\n * necessary.\n */\ntypedef enum {\n\ttest_status_pass = 0,\n\ttest_status_skip = 1,\n\ttest_status_fail = 2,\n\n\ttest_status_count = 3\n} test_status_t;\n\ntypedef void (test_t)(void);\n\n#define TEST_BEGIN(f)\t\t\t\t\t\t\t\\\nstatic void\t\t\t\t\t\t\t\t\\\nf(void) {\t\t\t\t\t\t\t\t\\\n\tp_test_init(#f);\n\n#define TEST_END\t\t\t\t\t\t\t\\\n\tgoto label_test_end;\t\t\t\t\t\t\\\nlabel_test_end:\t\t\t\t\t\t\t\t\\\n\tp_test_fini();\t\t\t\t\t\t\t\\\n}\n\n#define test(...)\t\t\t\t\t\t\t\\\n\tp_test(__VA_ARGS__, NULL)\n\n#define test_no_reentrancy(...)\t\t\t\t\t\t\t\\\n\tp_test_no_reentrancy(__VA_ARGS__, NULL)\n\n#define test_no_malloc_init(...)\t\t\t\t\t\\\n\tp_test_no_malloc_init(__VA_ARGS__, NULL)\n\n#define test_skip_if(e) do {\t\t\t\t\t\t\\\n\tif (e) {\t\t\t\t\t\t\t\\\n\t\ttest_skip(\"%s:%s:%d: Test skipped: (%s)\",\t\t\\\n\t\t    __func__, __FILE__, __LINE__, #e);\t\t\t\\\n\t\tgoto label_test_end;\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\nbool test_is_reentrant();\n\nvoid\ttest_skip(const char *format, ...) JEMALLOC_FORMAT_PRINTF(1, 2);\nvoid\ttest_fail(const char *format, ...) JEMALLOC_FORMAT_PRINTF(1, 2);\n\n/* For private use by macros. */\ntest_status_t\tp_test(test_t *t, ...);\ntest_status_t\tp_test_no_reentrancy(test_t *t, ...);\ntest_status_t\tp_test_no_malloc_init(test_t *t, ...);\nvoid\tp_test_init(const char *name);\nvoid\tp_test_fini(void);\nvoid\tp_test_fail(const char *prefix, const char *message);\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/include/test/thd.h",
    "content": "/* Abstraction layer for threading in tests. */\n#ifdef _WIN32\ntypedef HANDLE thd_t;\n#else\ntypedef pthread_t thd_t;\n#endif\n\nvoid\tthd_create(thd_t *thd, void *(*proc)(void *), void *arg);\nvoid\tthd_join(thd_t thd, void **ret);\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/include/test/timer.h",
    "content": "/* Simple timer, for use in benchmark reporting. */\n\ntypedef struct {\n\tnstime_t t0;\n\tnstime_t t1;\n} timedelta_t;\n\nvoid\ttimer_start(timedelta_t *timer);\nvoid\ttimer_stop(timedelta_t *timer);\nuint64_t\ttimer_usec(const timedelta_t *timer);\nvoid\ttimer_ratio(timedelta_t *a, timedelta_t *b, char *buf, size_t buflen);\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/integration/MALLOCX_ARENA.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#define NTHREADS 10\n\nstatic bool have_dss =\n#ifdef JEMALLOC_DSS\n    true\n#else\n    false\n#endif\n    ;\n\nvoid *\nthd_start(void *arg) {\n\tunsigned thread_ind = (unsigned)(uintptr_t)arg;\n\tunsigned arena_ind;\n\tvoid *p;\n\tsize_t sz;\n\n\tsz = sizeof(arena_ind);\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena_ind, &sz, NULL, 0),\n\t    0, \"Error in arenas.create\");\n\n\tif (thread_ind % 4 != 3) {\n\t\tsize_t mib[3];\n\t\tsize_t miblen = sizeof(mib) / sizeof(size_t);\n\t\tconst char *dss_precs[] = {\"disabled\", \"primary\", \"secondary\"};\n\t\tunsigned prec_ind = thread_ind %\n\t\t    (sizeof(dss_precs)/sizeof(char*));\n\t\tconst char *dss = dss_precs[prec_ind];\n\t\tint expected_err = (have_dss || prec_ind == 0) ? 0 : EFAULT;\n\t\tassert_d_eq(mallctlnametomib(\"arena.0.dss\", mib, &miblen), 0,\n\t\t    \"Error in mallctlnametomib()\");\n\t\tmib[1] = arena_ind;\n\t\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, (void *)&dss,\n\t\t    sizeof(const char *)), expected_err,\n\t\t    \"Error in mallctlbymib()\");\n\t}\n\n\tp = mallocx(1, MALLOCX_ARENA(arena_ind));\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\tdallocx(p, 0);\n\n\treturn NULL;\n}\n\nTEST_BEGIN(test_MALLOCX_ARENA) {\n\tthd_t thds[NTHREADS];\n\tunsigned i;\n\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tthd_create(&thds[i], thd_start,\n\t\t    (void *)(uintptr_t)i);\n\t}\n\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tthd_join(thds[i], NULL);\n\t}\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_MALLOCX_ARENA);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/integration/aligned_alloc.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#define MAXALIGN (((size_t)1) << 23)\n\n/*\n * On systems which can't merge extents, tests that call this function generate\n * a lot of dirty memory very quickly.  Purging between cycles mitigates\n * potential OOM on e.g. 32-bit Windows.\n */\nstatic void\npurge(void) {\n\tassert_d_eq(mallctl(\"arena.0.purge\", NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctl error\");\n}\n\nTEST_BEGIN(test_alignment_errors) {\n\tsize_t alignment;\n\tvoid *p;\n\n\talignment = 0;\n\tset_errno(0);\n\tp = aligned_alloc(alignment, 1);\n\tassert_false(p != NULL || get_errno() != EINVAL,\n\t    \"Expected error for invalid alignment %zu\", alignment);\n\n\tfor (alignment = sizeof(size_t); alignment < MAXALIGN;\n\t    alignment <<= 1) {\n\t\tset_errno(0);\n\t\tp = aligned_alloc(alignment + 1, 1);\n\t\tassert_false(p != NULL || get_errno() != EINVAL,\n\t\t    \"Expected error for invalid alignment %zu\",\n\t\t    alignment + 1);\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_oom_errors) {\n\tsize_t alignment, size;\n\tvoid *p;\n\n#if LG_SIZEOF_PTR == 3\n\talignment = UINT64_C(0x8000000000000000);\n\tsize      = UINT64_C(0x8000000000000000);\n#else\n\talignment = 0x80000000LU;\n\tsize      = 0x80000000LU;\n#endif\n\tset_errno(0);\n\tp = aligned_alloc(alignment, size);\n\tassert_false(p != NULL || get_errno() != ENOMEM,\n\t    \"Expected error for aligned_alloc(%zu, %zu)\",\n\t    alignment, size);\n\n#if LG_SIZEOF_PTR == 3\n\talignment = UINT64_C(0x4000000000000000);\n\tsize      = UINT64_C(0xc000000000000001);\n#else\n\talignment = 0x40000000LU;\n\tsize      = 0xc0000001LU;\n#endif\n\tset_errno(0);\n\tp = aligned_alloc(alignment, size);\n\tassert_false(p != NULL || get_errno() != ENOMEM,\n\t    \"Expected error for aligned_alloc(%zu, %zu)\",\n\t    alignment, size);\n\n\talignment = 0x10LU;\n#if LG_SIZEOF_PTR == 3\n\tsize = UINT64_C(0xfffffffffffffff0);\n#else\n\tsize = 0xfffffff0LU;\n#endif\n\tset_errno(0);\n\tp = aligned_alloc(alignment, size);\n\tassert_false(p != NULL || get_errno() != ENOMEM,\n\t    \"Expected error for aligned_alloc(&p, %zu, %zu)\",\n\t    alignment, size);\n}\nTEST_END\n\nTEST_BEGIN(test_alignment_and_size) {\n#define NITER 4\n\tsize_t alignment, size, total;\n\tunsigned i;\n\tvoid *ps[NITER];\n\n\tfor (i = 0; i < NITER; i++) {\n\t\tps[i] = NULL;\n\t}\n\n\tfor (alignment = 8;\n\t    alignment <= MAXALIGN;\n\t    alignment <<= 1) {\n\t\ttotal = 0;\n\t\tfor (size = 1;\n\t\t    size < 3 * alignment && size < (1U << 31);\n\t\t    size += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {\n\t\t\tfor (i = 0; i < NITER; i++) {\n\t\t\t\tps[i] = aligned_alloc(alignment, size);\n\t\t\t\tif (ps[i] == NULL) {\n\t\t\t\t\tchar buf[BUFERROR_BUF];\n\n\t\t\t\t\tbuferror(get_errno(), buf, sizeof(buf));\n\t\t\t\t\ttest_fail(\n\t\t\t\t\t    \"Error for alignment=%zu, \"\n\t\t\t\t\t    \"size=%zu (%#zx): %s\",\n\t\t\t\t\t    alignment, size, size, buf);\n\t\t\t\t}\n\t\t\t\ttotal += malloc_usable_size(ps[i]);\n\t\t\t\tif (total >= (MAXALIGN << 1)) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (i = 0; i < NITER; i++) {\n\t\t\t\tif (ps[i] != NULL) {\n\t\t\t\t\tfree(ps[i]);\n\t\t\t\t\tps[i] = NULL;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tpurge();\n\t}\n#undef NITER\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_alignment_errors,\n\t    test_oom_errors,\n\t    test_alignment_and_size);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/integration/allocated.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic const bool config_stats =\n#ifdef JEMALLOC_STATS\n    true\n#else\n    false\n#endif\n    ;\n\nvoid *\nthd_start(void *arg) {\n\tint err;\n\tvoid *p;\n\tuint64_t a0, a1, d0, d1;\n\tuint64_t *ap0, *ap1, *dp0, *dp1;\n\tsize_t sz, usize;\n\n\tsz = sizeof(a0);\n\tif ((err = mallctl(\"thread.allocated\", (void *)&a0, &sz, NULL, 0))) {\n\t\tif (err == ENOENT) {\n\t\t\tgoto label_ENOENT;\n\t\t}\n\t\ttest_fail(\"%s(): Error in mallctl(): %s\", __func__,\n\t\t    strerror(err));\n\t}\n\tsz = sizeof(ap0);\n\tif ((err = mallctl(\"thread.allocatedp\", (void *)&ap0, &sz, NULL, 0))) {\n\t\tif (err == ENOENT) {\n\t\t\tgoto label_ENOENT;\n\t\t}\n\t\ttest_fail(\"%s(): Error in mallctl(): %s\", __func__,\n\t\t    strerror(err));\n\t}\n\tassert_u64_eq(*ap0, a0,\n\t    \"\\\"thread.allocatedp\\\" should provide a pointer to internal \"\n\t    \"storage\");\n\n\tsz = sizeof(d0);\n\tif ((err = mallctl(\"thread.deallocated\", (void *)&d0, &sz, NULL, 0))) {\n\t\tif (err == ENOENT) {\n\t\t\tgoto label_ENOENT;\n\t\t}\n\t\ttest_fail(\"%s(): Error in mallctl(): %s\", __func__,\n\t\t    strerror(err));\n\t}\n\tsz = sizeof(dp0);\n\tif ((err = mallctl(\"thread.deallocatedp\", (void *)&dp0, &sz, NULL,\n\t    0))) {\n\t\tif (err == ENOENT) {\n\t\t\tgoto label_ENOENT;\n\t\t}\n\t\ttest_fail(\"%s(): Error in mallctl(): %s\", __func__,\n\t\t    strerror(err));\n\t}\n\tassert_u64_eq(*dp0, d0,\n\t    \"\\\"thread.deallocatedp\\\" should provide a pointer to internal \"\n\t    \"storage\");\n\n\tp = malloc(1);\n\tassert_ptr_not_null(p, \"Unexpected malloc() error\");\n\n\tsz = sizeof(a1);\n\tmallctl(\"thread.allocated\", (void *)&a1, &sz, NULL, 0);\n\tsz = sizeof(ap1);\n\tmallctl(\"thread.allocatedp\", (void *)&ap1, &sz, NULL, 0);\n\tassert_u64_eq(*ap1, a1,\n\t    \"Dereferenced \\\"thread.allocatedp\\\" value should equal \"\n\t    \"\\\"thread.allocated\\\" value\");\n\tassert_ptr_eq(ap0, ap1,\n\t    \"Pointer returned by \\\"thread.allocatedp\\\" should not change\");\n\n\tusize = malloc_usable_size(p);\n\tassert_u64_le(a0 + usize, a1,\n\t    \"Allocated memory counter should increase by at least the amount \"\n\t    \"explicitly allocated\");\n\n\tfree(p);\n\n\tsz = sizeof(d1);\n\tmallctl(\"thread.deallocated\", (void *)&d1, &sz, NULL, 0);\n\tsz = sizeof(dp1);\n\tmallctl(\"thread.deallocatedp\", (void *)&dp1, &sz, NULL, 0);\n\tassert_u64_eq(*dp1, d1,\n\t    \"Dereferenced \\\"thread.deallocatedp\\\" value should equal \"\n\t    \"\\\"thread.deallocated\\\" value\");\n\tassert_ptr_eq(dp0, dp1,\n\t    \"Pointer returned by \\\"thread.deallocatedp\\\" should not change\");\n\n\tassert_u64_le(d0 + usize, d1,\n\t    \"Deallocated memory counter should increase by at least the amount \"\n\t    \"explicitly deallocated\");\n\n\treturn NULL;\nlabel_ENOENT:\n\tassert_false(config_stats,\n\t    \"ENOENT should only be returned if stats are disabled\");\n\ttest_skip(\"\\\"thread.allocated\\\" mallctl not available\");\n\treturn NULL;\n}\n\nTEST_BEGIN(test_main_thread) {\n\tthd_start(NULL);\n}\nTEST_END\n\nTEST_BEGIN(test_subthread) {\n\tthd_t thd;\n\n\tthd_create(&thd, thd_start, NULL);\n\tthd_join(thd, NULL);\n}\nTEST_END\n\nint\nmain(void) {\n\t/* Run tests multiple times to check for bad interactions. */\n\treturn test(\n\t    test_main_thread,\n\t    test_subthread,\n\t    test_main_thread,\n\t    test_subthread,\n\t    test_main_thread);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/integration/extent.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"test/extent_hooks.h\"\n\nstatic bool\ncheck_background_thread_enabled(void) {\n\tbool enabled;\n\tsize_t sz = sizeof(bool);\n\tint ret = mallctl(\"background_thread\", (void *)&enabled, &sz, NULL,0);\n\tif (ret == ENOENT) {\n\t\treturn false;\n\t}\n\tassert_d_eq(ret, 0, \"Unexpected mallctl error\");\n\treturn enabled;\n}\n\nstatic void\ntest_extent_body(unsigned arena_ind) {\n\tvoid *p;\n\tsize_t large0, large1, large2, sz;\n\tsize_t purge_mib[3];\n\tsize_t purge_miblen;\n\tint flags;\n\tbool xallocx_success_a, xallocx_success_b, xallocx_success_c;\n\n\tflags = MALLOCX_ARENA(arena_ind) | MALLOCX_TCACHE_NONE;\n\n\t/* Get large size classes. */\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctl(\"arenas.lextent.0.size\", (void *)&large0, &sz, NULL,\n\t    0), 0, \"Unexpected arenas.lextent.0.size failure\");\n\tassert_d_eq(mallctl(\"arenas.lextent.1.size\", (void *)&large1, &sz, NULL,\n\t    0), 0, \"Unexpected arenas.lextent.1.size failure\");\n\tassert_d_eq(mallctl(\"arenas.lextent.2.size\", (void *)&large2, &sz, NULL,\n\t    0), 0, \"Unexpected arenas.lextent.2.size failure\");\n\n\t/* Test dalloc/decommit/purge cascade. */\n\tpurge_miblen = sizeof(purge_mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"arena.0.purge\", purge_mib, &purge_miblen),\n\t    0, \"Unexpected mallctlnametomib() failure\");\n\tpurge_mib[1] = (size_t)arena_ind;\n\tcalled_alloc = false;\n\ttry_alloc = true;\n\ttry_dalloc = false;\n\ttry_decommit = false;\n\tp = mallocx(large0 * 2, flags);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\tassert_true(called_alloc, \"Expected alloc call\");\n\tcalled_dalloc = false;\n\tcalled_decommit = false;\n\tdid_purge_lazy = false;\n\tdid_purge_forced = false;\n\tcalled_split = false;\n\txallocx_success_a = (xallocx(p, large0, 0, flags) == large0);\n\tassert_d_eq(mallctlbymib(purge_mib, purge_miblen, NULL, NULL, NULL, 0),\n\t    0, \"Unexpected arena.%u.purge error\", arena_ind);\n\tif (xallocx_success_a) {\n\t\tassert_true(called_dalloc, \"Expected dalloc call\");\n\t\tassert_true(called_decommit, \"Expected decommit call\");\n\t\tassert_true(did_purge_lazy || did_purge_forced,\n\t\t    \"Expected purge\");\n\t}\n\tassert_true(called_split, \"Expected split call\");\n\tdallocx(p, flags);\n\ttry_dalloc = true;\n\n\t/* Test decommit/commit and observe split/merge. */\n\ttry_dalloc = false;\n\ttry_decommit = true;\n\tp = mallocx(large0 * 2, flags);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\tdid_decommit = false;\n\tdid_commit = false;\n\tcalled_split = false;\n\tdid_split = false;\n\tdid_merge = false;\n\txallocx_success_b = (xallocx(p, large0, 0, flags) == large0);\n\tassert_d_eq(mallctlbymib(purge_mib, purge_miblen, NULL, NULL, NULL, 0),\n\t    0, \"Unexpected arena.%u.purge error\", arena_ind);\n\tif (xallocx_success_b) {\n\t\tassert_true(did_split, \"Expected split\");\n\t}\n\txallocx_success_c = (xallocx(p, large0 * 2, 0, flags) == large0 * 2);\n\tif (did_split) {\n\t\tassert_b_eq(did_decommit, did_commit,\n\t\t    \"Expected decommit/commit match\");\n\t}\n\tif (xallocx_success_b && xallocx_success_c) {\n\t\tassert_true(did_merge, \"Expected merge\");\n\t}\n\tdallocx(p, flags);\n\ttry_dalloc = true;\n\ttry_decommit = false;\n\n\t/* Make sure non-large allocation succeeds. */\n\tp = mallocx(42, flags);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\tdallocx(p, flags);\n}\n\nTEST_BEGIN(test_extent_manual_hook) {\n\tunsigned arena_ind;\n\tsize_t old_size, new_size, sz;\n\tsize_t hooks_mib[3];\n\tsize_t hooks_miblen;\n\textent_hooks_t *new_hooks, *old_hooks;\n\n\textent_hooks_prep();\n\n\tsz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena_ind, &sz, NULL, 0),\n\t    0, \"Unexpected mallctl() failure\");\n\n\t/* Install custom extent hooks. */\n\thooks_miblen = sizeof(hooks_mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"arena.0.extent_hooks\", hooks_mib,\n\t    &hooks_miblen), 0, \"Unexpected mallctlnametomib() failure\");\n\thooks_mib[1] = (size_t)arena_ind;\n\told_size = sizeof(extent_hooks_t *);\n\tnew_hooks = &hooks;\n\tnew_size = sizeof(extent_hooks_t *);\n\tassert_d_eq(mallctlbymib(hooks_mib, hooks_miblen, (void *)&old_hooks,\n\t    &old_size, (void *)&new_hooks, new_size), 0,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_ne(old_hooks->alloc, extent_alloc_hook,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_ne(old_hooks->dalloc, extent_dalloc_hook,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_ne(old_hooks->commit, extent_commit_hook,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_ne(old_hooks->decommit, extent_decommit_hook,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_ne(old_hooks->purge_lazy, extent_purge_lazy_hook,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_ne(old_hooks->purge_forced, extent_purge_forced_hook,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_ne(old_hooks->split, extent_split_hook,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_ne(old_hooks->merge, extent_merge_hook,\n\t    \"Unexpected extent_hooks error\");\n\n\ttest_skip_if(check_background_thread_enabled());\n\ttest_extent_body(arena_ind);\n\n\t/* Restore extent hooks. */\n\tassert_d_eq(mallctlbymib(hooks_mib, hooks_miblen, NULL, NULL,\n\t    (void *)&old_hooks, new_size), 0, \"Unexpected extent_hooks error\");\n\tassert_d_eq(mallctlbymib(hooks_mib, hooks_miblen, (void *)&old_hooks,\n\t    &old_size, NULL, 0), 0, \"Unexpected extent_hooks error\");\n\tassert_ptr_eq(old_hooks, default_hooks, \"Unexpected extent_hooks error\");\n\tassert_ptr_eq(old_hooks->alloc, default_hooks->alloc,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_eq(old_hooks->dalloc, default_hooks->dalloc,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_eq(old_hooks->commit, default_hooks->commit,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_eq(old_hooks->decommit, default_hooks->decommit,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_eq(old_hooks->purge_lazy, default_hooks->purge_lazy,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_eq(old_hooks->purge_forced, default_hooks->purge_forced,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_eq(old_hooks->split, default_hooks->split,\n\t    \"Unexpected extent_hooks error\");\n\tassert_ptr_eq(old_hooks->merge, default_hooks->merge,\n\t    \"Unexpected extent_hooks error\");\n}\nTEST_END\n\nTEST_BEGIN(test_extent_auto_hook) {\n\tunsigned arena_ind;\n\tsize_t new_size, sz;\n\textent_hooks_t *new_hooks;\n\n\textent_hooks_prep();\n\n\tsz = sizeof(unsigned);\n\tnew_hooks = &hooks;\n\tnew_size = sizeof(extent_hooks_t *);\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena_ind, &sz,\n\t    (void *)&new_hooks, new_size), 0, \"Unexpected mallctl() failure\");\n\n\ttest_skip_if(check_background_thread_enabled());\n\ttest_extent_body(arena_ind);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_extent_manual_hook,\n\t    test_extent_auto_hook);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/integration/extent.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_fill}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"junk:false\"\nfi\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/integration/mallocx.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic unsigned\nget_nsizes_impl(const char *cmd) {\n\tunsigned ret;\n\tsize_t z;\n\n\tz = sizeof(unsigned);\n\tassert_d_eq(mallctl(cmd, (void *)&ret, &z, NULL, 0), 0,\n\t    \"Unexpected mallctl(\\\"%s\\\", ...) failure\", cmd);\n\n\treturn ret;\n}\n\nstatic unsigned\nget_nlarge(void) {\n\treturn get_nsizes_impl(\"arenas.nlextents\");\n}\n\nstatic size_t\nget_size_impl(const char *cmd, size_t ind) {\n\tsize_t ret;\n\tsize_t z;\n\tsize_t mib[4];\n\tsize_t miblen = 4;\n\n\tz = sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(cmd, mib, &miblen),\n\t    0, \"Unexpected mallctlnametomib(\\\"%s\\\", ...) failure\", cmd);\n\tmib[2] = ind;\n\tz = sizeof(size_t);\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&ret, &z, NULL, 0),\n\t    0, \"Unexpected mallctlbymib([\\\"%s\\\", %zu], ...) failure\", cmd, ind);\n\n\treturn ret;\n}\n\nstatic size_t\nget_large_size(size_t ind) {\n\treturn get_size_impl(\"arenas.lextent.0.size\", ind);\n}\n\n/*\n * On systems which can't merge extents, tests that call this function generate\n * a lot of dirty memory very quickly.  Purging between cycles mitigates\n * potential OOM on e.g. 32-bit Windows.\n */\nstatic void\npurge(void) {\n\tassert_d_eq(mallctl(\"arena.0.purge\", NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctl error\");\n}\n\nTEST_BEGIN(test_overflow) {\n\tsize_t largemax;\n\n\tlargemax = get_large_size(get_nlarge()-1);\n\n\tassert_ptr_null(mallocx(largemax+1, 0),\n\t    \"Expected OOM for mallocx(size=%#zx, 0)\", largemax+1);\n\n\tassert_ptr_null(mallocx(ZU(PTRDIFF_MAX)+1, 0),\n\t    \"Expected OOM for mallocx(size=%#zx, 0)\", ZU(PTRDIFF_MAX)+1);\n\n\tassert_ptr_null(mallocx(SIZE_T_MAX, 0),\n\t    \"Expected OOM for mallocx(size=%#zx, 0)\", SIZE_T_MAX);\n\n\tassert_ptr_null(mallocx(1, MALLOCX_ALIGN(ZU(PTRDIFF_MAX)+1)),\n\t    \"Expected OOM for mallocx(size=1, MALLOCX_ALIGN(%#zx))\",\n\t    ZU(PTRDIFF_MAX)+1);\n}\nTEST_END\n\nTEST_BEGIN(test_oom) {\n\tsize_t largemax;\n\tbool oom;\n\tvoid *ptrs[3];\n\tunsigned i;\n\n\t/*\n\t * It should be impossible to allocate three objects that each consume\n\t * nearly half the virtual address space.\n\t */\n\tlargemax = get_large_size(get_nlarge()-1);\n\toom = false;\n\tfor (i = 0; i < sizeof(ptrs) / sizeof(void *); i++) {\n\t\tptrs[i] = mallocx(largemax, 0);\n\t\tif (ptrs[i] == NULL) {\n\t\t\toom = true;\n\t\t}\n\t}\n\tassert_true(oom,\n\t    \"Expected OOM during series of calls to mallocx(size=%zu, 0)\",\n\t    largemax);\n\tfor (i = 0; i < sizeof(ptrs) / sizeof(void *); i++) {\n\t\tif (ptrs[i] != NULL) {\n\t\t\tdallocx(ptrs[i], 0);\n\t\t}\n\t}\n\tpurge();\n\n#if LG_SIZEOF_PTR == 3\n\tassert_ptr_null(mallocx(0x8000000000000000ULL,\n\t    MALLOCX_ALIGN(0x8000000000000000ULL)),\n\t    \"Expected OOM for mallocx()\");\n\tassert_ptr_null(mallocx(0x8000000000000000ULL,\n\t    MALLOCX_ALIGN(0x80000000)),\n\t    \"Expected OOM for mallocx()\");\n#else\n\tassert_ptr_null(mallocx(0x80000000UL, MALLOCX_ALIGN(0x80000000UL)),\n\t    \"Expected OOM for mallocx()\");\n#endif\n}\nTEST_END\n\nTEST_BEGIN(test_basic) {\n#define MAXSZ (((size_t)1) << 23)\n\tsize_t sz;\n\n\tfor (sz = 1; sz < MAXSZ; sz = nallocx(sz, 0) + 1) {\n\t\tsize_t nsz, rsz;\n\t\tvoid *p;\n\t\tnsz = nallocx(sz, 0);\n\t\tassert_zu_ne(nsz, 0, \"Unexpected nallocx() error\");\n\t\tp = mallocx(sz, 0);\n\t\tassert_ptr_not_null(p,\n\t\t    \"Unexpected mallocx(size=%zx, flags=0) error\", sz);\n\t\trsz = sallocx(p, 0);\n\t\tassert_zu_ge(rsz, sz, \"Real size smaller than expected\");\n\t\tassert_zu_eq(nsz, rsz, \"nallocx()/sallocx() size mismatch\");\n\t\tdallocx(p, 0);\n\n\t\tp = mallocx(sz, 0);\n\t\tassert_ptr_not_null(p,\n\t\t    \"Unexpected mallocx(size=%zx, flags=0) error\", sz);\n\t\tdallocx(p, 0);\n\n\t\tnsz = nallocx(sz, MALLOCX_ZERO);\n\t\tassert_zu_ne(nsz, 0, \"Unexpected nallocx() error\");\n\t\tp = mallocx(sz, MALLOCX_ZERO);\n\t\tassert_ptr_not_null(p,\n\t\t    \"Unexpected mallocx(size=%zx, flags=MALLOCX_ZERO) error\",\n\t\t    nsz);\n\t\trsz = sallocx(p, 0);\n\t\tassert_zu_eq(nsz, rsz, \"nallocx()/sallocx() rsize mismatch\");\n\t\tdallocx(p, 0);\n\t\tpurge();\n\t}\n#undef MAXSZ\n}\nTEST_END\n\nTEST_BEGIN(test_alignment_and_size) {\n#define MAXALIGN (((size_t)1) << 23)\n#define NITER 4\n\tsize_t nsz, rsz, sz, alignment, total;\n\tunsigned i;\n\tvoid *ps[NITER];\n\n\tfor (i = 0; i < NITER; i++) {\n\t\tps[i] = NULL;\n\t}\n\n\tfor (alignment = 8;\n\t    alignment <= MAXALIGN;\n\t    alignment <<= 1) {\n\t\ttotal = 0;\n\t\tfor (sz = 1;\n\t\t    sz < 3 * alignment && sz < (1U << 31);\n\t\t    sz += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {\n\t\t\tfor (i = 0; i < NITER; i++) {\n\t\t\t\tnsz = nallocx(sz, MALLOCX_ALIGN(alignment) |\n\t\t\t\t    MALLOCX_ZERO);\n\t\t\t\tassert_zu_ne(nsz, 0,\n\t\t\t\t    \"nallocx() error for alignment=%zu, \"\n\t\t\t\t    \"size=%zu (%#zx)\", alignment, sz, sz);\n\t\t\t\tps[i] = mallocx(sz, MALLOCX_ALIGN(alignment) |\n\t\t\t\t    MALLOCX_ZERO);\n\t\t\t\tassert_ptr_not_null(ps[i],\n\t\t\t\t    \"mallocx() error for alignment=%zu, \"\n\t\t\t\t    \"size=%zu (%#zx)\", alignment, sz, sz);\n\t\t\t\trsz = sallocx(ps[i], 0);\n\t\t\t\tassert_zu_ge(rsz, sz,\n\t\t\t\t    \"Real size smaller than expected for \"\n\t\t\t\t    \"alignment=%zu, size=%zu\", alignment, sz);\n\t\t\t\tassert_zu_eq(nsz, rsz,\n\t\t\t\t    \"nallocx()/sallocx() size mismatch for \"\n\t\t\t\t    \"alignment=%zu, size=%zu\", alignment, sz);\n\t\t\t\tassert_ptr_null(\n\t\t\t\t    (void *)((uintptr_t)ps[i] & (alignment-1)),\n\t\t\t\t    \"%p inadequately aligned for\"\n\t\t\t\t    \" alignment=%zu, size=%zu\", ps[i],\n\t\t\t\t    alignment, sz);\n\t\t\t\ttotal += rsz;\n\t\t\t\tif (total >= (MAXALIGN << 1)) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (i = 0; i < NITER; i++) {\n\t\t\t\tif (ps[i] != NULL) {\n\t\t\t\t\tdallocx(ps[i], 0);\n\t\t\t\t\tps[i] = NULL;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tpurge();\n\t}\n#undef MAXALIGN\n#undef NITER\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_overflow,\n\t    test_oom,\n\t    test_basic,\n\t    test_alignment_and_size);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/integration/mallocx.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_fill}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"junk:false\"\nfi\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/integration/overflow.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nTEST_BEGIN(test_overflow) {\n\tunsigned nlextents;\n\tsize_t mib[4];\n\tsize_t sz, miblen, max_size_class;\n\tvoid *p;\n\n\tsz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.nlextents\", (void *)&nlextents, &sz, NULL,\n\t    0), 0, \"Unexpected mallctl() error\");\n\n\tmiblen = sizeof(mib) / sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"arenas.lextent.0.size\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() error\");\n\tmib[2] = nlextents - 1;\n\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&max_size_class, &sz,\n\t    NULL, 0), 0, \"Unexpected mallctlbymib() error\");\n\n\tassert_ptr_null(malloc(max_size_class + 1),\n\t    \"Expected OOM due to over-sized allocation request\");\n\tassert_ptr_null(malloc(SIZE_T_MAX),\n\t    \"Expected OOM due to over-sized allocation request\");\n\n\tassert_ptr_null(calloc(1, max_size_class + 1),\n\t    \"Expected OOM due to over-sized allocation request\");\n\tassert_ptr_null(calloc(1, SIZE_T_MAX),\n\t    \"Expected OOM due to over-sized allocation request\");\n\n\tp = malloc(1);\n\tassert_ptr_not_null(p, \"Unexpected malloc() OOM\");\n\tassert_ptr_null(realloc(p, max_size_class + 1),\n\t    \"Expected OOM due to over-sized allocation request\");\n\tassert_ptr_null(realloc(p, SIZE_T_MAX),\n\t    \"Expected OOM due to over-sized allocation request\");\n\tfree(p);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_overflow);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/integration/posix_memalign.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#define MAXALIGN (((size_t)1) << 23)\n\n/*\n * On systems which can't merge extents, tests that call this function generate\n * a lot of dirty memory very quickly.  Purging between cycles mitigates\n * potential OOM on e.g. 32-bit Windows.\n */\nstatic void\npurge(void) {\n\tassert_d_eq(mallctl(\"arena.0.purge\", NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctl error\");\n}\n\nTEST_BEGIN(test_alignment_errors) {\n\tsize_t alignment;\n\tvoid *p;\n\n\tfor (alignment = 0; alignment < sizeof(void *); alignment++) {\n\t\tassert_d_eq(posix_memalign(&p, alignment, 1), EINVAL,\n\t\t    \"Expected error for invalid alignment %zu\",\n\t\t    alignment);\n\t}\n\n\tfor (alignment = sizeof(size_t); alignment < MAXALIGN;\n\t    alignment <<= 1) {\n\t\tassert_d_ne(posix_memalign(&p, alignment + 1, 1), 0,\n\t\t    \"Expected error for invalid alignment %zu\",\n\t\t    alignment + 1);\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_oom_errors) {\n\tsize_t alignment, size;\n\tvoid *p;\n\n#if LG_SIZEOF_PTR == 3\n\talignment = UINT64_C(0x8000000000000000);\n\tsize      = UINT64_C(0x8000000000000000);\n#else\n\talignment = 0x80000000LU;\n\tsize      = 0x80000000LU;\n#endif\n\tassert_d_ne(posix_memalign(&p, alignment, size), 0,\n\t    \"Expected error for posix_memalign(&p, %zu, %zu)\",\n\t    alignment, size);\n\n#if LG_SIZEOF_PTR == 3\n\talignment = UINT64_C(0x4000000000000000);\n\tsize      = UINT64_C(0xc000000000000001);\n#else\n\talignment = 0x40000000LU;\n\tsize      = 0xc0000001LU;\n#endif\n\tassert_d_ne(posix_memalign(&p, alignment, size), 0,\n\t    \"Expected error for posix_memalign(&p, %zu, %zu)\",\n\t    alignment, size);\n\n\talignment = 0x10LU;\n#if LG_SIZEOF_PTR == 3\n\tsize = UINT64_C(0xfffffffffffffff0);\n#else\n\tsize = 0xfffffff0LU;\n#endif\n\tassert_d_ne(posix_memalign(&p, alignment, size), 0,\n\t    \"Expected error for posix_memalign(&p, %zu, %zu)\",\n\t    alignment, size);\n}\nTEST_END\n\nTEST_BEGIN(test_alignment_and_size) {\n#define NITER 4\n\tsize_t alignment, size, total;\n\tunsigned i;\n\tint err;\n\tvoid *ps[NITER];\n\n\tfor (i = 0; i < NITER; i++) {\n\t\tps[i] = NULL;\n\t}\n\n\tfor (alignment = 8;\n\t    alignment <= MAXALIGN;\n\t    alignment <<= 1) {\n\t\ttotal = 0;\n\t\tfor (size = 1;\n\t\t    size < 3 * alignment && size < (1U << 31);\n\t\t    size += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {\n\t\t\tfor (i = 0; i < NITER; i++) {\n\t\t\t\terr = posix_memalign(&ps[i],\n\t\t\t\t    alignment, size);\n\t\t\t\tif (err) {\n\t\t\t\t\tchar buf[BUFERROR_BUF];\n\n\t\t\t\t\tbuferror(get_errno(), buf, sizeof(buf));\n\t\t\t\t\ttest_fail(\n\t\t\t\t\t    \"Error for alignment=%zu, \"\n\t\t\t\t\t    \"size=%zu (%#zx): %s\",\n\t\t\t\t\t    alignment, size, size, buf);\n\t\t\t\t}\n\t\t\t\ttotal += malloc_usable_size(ps[i]);\n\t\t\t\tif (total >= (MAXALIGN << 1)) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (i = 0; i < NITER; i++) {\n\t\t\t\tif (ps[i] != NULL) {\n\t\t\t\t\tfree(ps[i]);\n\t\t\t\t\tps[i] = NULL;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tpurge();\n\t}\n#undef NITER\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_alignment_errors,\n\t    test_oom_errors,\n\t    test_alignment_and_size);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/integration/rallocx.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic unsigned\nget_nsizes_impl(const char *cmd) {\n\tunsigned ret;\n\tsize_t z;\n\n\tz = sizeof(unsigned);\n\tassert_d_eq(mallctl(cmd, (void *)&ret, &z, NULL, 0), 0,\n\t    \"Unexpected mallctl(\\\"%s\\\", ...) failure\", cmd);\n\n\treturn ret;\n}\n\nstatic unsigned\nget_nlarge(void) {\n\treturn get_nsizes_impl(\"arenas.nlextents\");\n}\n\nstatic size_t\nget_size_impl(const char *cmd, size_t ind) {\n\tsize_t ret;\n\tsize_t z;\n\tsize_t mib[4];\n\tsize_t miblen = 4;\n\n\tz = sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(cmd, mib, &miblen),\n\t    0, \"Unexpected mallctlnametomib(\\\"%s\\\", ...) failure\", cmd);\n\tmib[2] = ind;\n\tz = sizeof(size_t);\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&ret, &z, NULL, 0),\n\t    0, \"Unexpected mallctlbymib([\\\"%s\\\", %zu], ...) failure\", cmd, ind);\n\n\treturn ret;\n}\n\nstatic size_t\nget_large_size(size_t ind) {\n\treturn get_size_impl(\"arenas.lextent.0.size\", ind);\n}\n\nTEST_BEGIN(test_grow_and_shrink) {\n\tvoid *p, *q;\n\tsize_t tsz;\n#define NCYCLES 3\n\tunsigned i, j;\n#define NSZS 1024\n\tsize_t szs[NSZS];\n#define MAXSZ ZU(12 * 1024 * 1024)\n\n\tp = mallocx(1, 0);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\tszs[0] = sallocx(p, 0);\n\n\tfor (i = 0; i < NCYCLES; i++) {\n\t\tfor (j = 1; j < NSZS && szs[j-1] < MAXSZ; j++) {\n\t\t\tq = rallocx(p, szs[j-1]+1, 0);\n\t\t\tassert_ptr_not_null(q,\n\t\t\t    \"Unexpected rallocx() error for size=%zu-->%zu\",\n\t\t\t    szs[j-1], szs[j-1]+1);\n\t\t\tszs[j] = sallocx(q, 0);\n\t\t\tassert_zu_ne(szs[j], szs[j-1]+1,\n\t\t\t    \"Expected size to be at least: %zu\", szs[j-1]+1);\n\t\t\tp = q;\n\t\t}\n\n\t\tfor (j--; j > 0; j--) {\n\t\t\tq = rallocx(p, szs[j-1], 0);\n\t\t\tassert_ptr_not_null(q,\n\t\t\t    \"Unexpected rallocx() error for size=%zu-->%zu\",\n\t\t\t    szs[j], szs[j-1]);\n\t\t\ttsz = sallocx(q, 0);\n\t\t\tassert_zu_eq(tsz, szs[j-1],\n\t\t\t    \"Expected size=%zu, got size=%zu\", szs[j-1], tsz);\n\t\t\tp = q;\n\t\t}\n\t}\n\n\tdallocx(p, 0);\n#undef MAXSZ\n#undef NSZS\n#undef NCYCLES\n}\nTEST_END\n\nstatic bool\nvalidate_fill(const void *p, uint8_t c, size_t offset, size_t len) {\n\tbool ret = false;\n\tconst uint8_t *buf = (const uint8_t *)p;\n\tsize_t i;\n\n\tfor (i = 0; i < len; i++) {\n\t\tuint8_t b = buf[offset+i];\n\t\tif (b != c) {\n\t\t\ttest_fail(\"Allocation at %p (len=%zu) contains %#x \"\n\t\t\t    \"rather than %#x at offset %zu\", p, len, b, c,\n\t\t\t    offset+i);\n\t\t\tret = true;\n\t\t}\n\t}\n\n\treturn ret;\n}\n\nTEST_BEGIN(test_zero) {\n\tvoid *p, *q;\n\tsize_t psz, qsz, i, j;\n\tsize_t start_sizes[] = {1, 3*1024, 63*1024, 4095*1024};\n#define FILL_BYTE 0xaaU\n#define RANGE 2048\n\n\tfor (i = 0; i < sizeof(start_sizes)/sizeof(size_t); i++) {\n\t\tsize_t start_size = start_sizes[i];\n\t\tp = mallocx(start_size, MALLOCX_ZERO);\n\t\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\t\tpsz = sallocx(p, 0);\n\n\t\tassert_false(validate_fill(p, 0, 0, psz),\n\t\t    \"Expected zeroed memory\");\n\t\tmemset(p, FILL_BYTE, psz);\n\t\tassert_false(validate_fill(p, FILL_BYTE, 0, psz),\n\t\t    \"Expected filled memory\");\n\n\t\tfor (j = 1; j < RANGE; j++) {\n\t\t\tq = rallocx(p, start_size+j, MALLOCX_ZERO);\n\t\t\tassert_ptr_not_null(q, \"Unexpected rallocx() error\");\n\t\t\tqsz = sallocx(q, 0);\n\t\t\tif (q != p || qsz != psz) {\n\t\t\t\tassert_false(validate_fill(q, FILL_BYTE, 0,\n\t\t\t\t    psz), \"Expected filled memory\");\n\t\t\t\tassert_false(validate_fill(q, 0, psz, qsz-psz),\n\t\t\t\t    \"Expected zeroed memory\");\n\t\t\t}\n\t\t\tif (psz != qsz) {\n\t\t\t\tmemset((void *)((uintptr_t)q+psz), FILL_BYTE,\n\t\t\t\t    qsz-psz);\n\t\t\t\tpsz = qsz;\n\t\t\t}\n\t\t\tp = q;\n\t\t}\n\t\tassert_false(validate_fill(p, FILL_BYTE, 0, psz),\n\t\t    \"Expected filled memory\");\n\t\tdallocx(p, 0);\n\t}\n#undef FILL_BYTE\n}\nTEST_END\n\nTEST_BEGIN(test_align) {\n\tvoid *p, *q;\n\tsize_t align;\n#define MAX_ALIGN (ZU(1) << 25)\n\n\talign = ZU(1);\n\tp = mallocx(1, MALLOCX_ALIGN(align));\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\n\tfor (align <<= 1; align <= MAX_ALIGN; align <<= 1) {\n\t\tq = rallocx(p, 1, MALLOCX_ALIGN(align));\n\t\tassert_ptr_not_null(q,\n\t\t    \"Unexpected rallocx() error for align=%zu\", align);\n\t\tassert_ptr_null(\n\t\t    (void *)((uintptr_t)q & (align-1)),\n\t\t    \"%p inadequately aligned for align=%zu\",\n\t\t    q, align);\n\t\tp = q;\n\t}\n\tdallocx(p, 0);\n#undef MAX_ALIGN\n}\nTEST_END\n\nTEST_BEGIN(test_lg_align_and_zero) {\n\tvoid *p, *q;\n\tunsigned lg_align;\n\tsize_t sz;\n#define MAX_LG_ALIGN 25\n#define MAX_VALIDATE (ZU(1) << 22)\n\n\tlg_align = 0;\n\tp = mallocx(1, MALLOCX_LG_ALIGN(lg_align)|MALLOCX_ZERO);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\n\tfor (lg_align++; lg_align <= MAX_LG_ALIGN; lg_align++) {\n\t\tq = rallocx(p, 1, MALLOCX_LG_ALIGN(lg_align)|MALLOCX_ZERO);\n\t\tassert_ptr_not_null(q,\n\t\t    \"Unexpected rallocx() error for lg_align=%u\", lg_align);\n\t\tassert_ptr_null(\n\t\t    (void *)((uintptr_t)q & ((ZU(1) << lg_align)-1)),\n\t\t    \"%p inadequately aligned for lg_align=%u\", q, lg_align);\n\t\tsz = sallocx(q, 0);\n\t\tif ((sz << 1) <= MAX_VALIDATE) {\n\t\t\tassert_false(validate_fill(q, 0, 0, sz),\n\t\t\t    \"Expected zeroed memory\");\n\t\t} else {\n\t\t\tassert_false(validate_fill(q, 0, 0, MAX_VALIDATE),\n\t\t\t    \"Expected zeroed memory\");\n\t\t\tassert_false(validate_fill(\n\t\t\t    (void *)((uintptr_t)q+sz-MAX_VALIDATE),\n\t\t\t    0, 0, MAX_VALIDATE), \"Expected zeroed memory\");\n\t\t}\n\t\tp = q;\n\t}\n\tdallocx(p, 0);\n#undef MAX_VALIDATE\n#undef MAX_LG_ALIGN\n}\nTEST_END\n\nTEST_BEGIN(test_overflow) {\n\tsize_t largemax;\n\tvoid *p;\n\n\tlargemax = get_large_size(get_nlarge()-1);\n\n\tp = mallocx(1, 0);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\n\tassert_ptr_null(rallocx(p, largemax+1, 0),\n\t    \"Expected OOM for rallocx(p, size=%#zx, 0)\", largemax+1);\n\n\tassert_ptr_null(rallocx(p, ZU(PTRDIFF_MAX)+1, 0),\n\t    \"Expected OOM for rallocx(p, size=%#zx, 0)\", ZU(PTRDIFF_MAX)+1);\n\n\tassert_ptr_null(rallocx(p, SIZE_T_MAX, 0),\n\t    \"Expected OOM for rallocx(p, size=%#zx, 0)\", SIZE_T_MAX);\n\n\tassert_ptr_null(rallocx(p, 1, MALLOCX_ALIGN(ZU(PTRDIFF_MAX)+1)),\n\t    \"Expected OOM for rallocx(p, size=1, MALLOCX_ALIGN(%#zx))\",\n\t    ZU(PTRDIFF_MAX)+1);\n\n\tdallocx(p, 0);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_grow_and_shrink,\n\t    test_zero,\n\t    test_align,\n\t    test_lg_align_and_zero,\n\t    test_overflow);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/integration/sdallocx.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#define MAXALIGN (((size_t)1) << 22)\n#define NITER 3\n\nTEST_BEGIN(test_basic) {\n\tvoid *ptr = mallocx(64, 0);\n\tsdallocx(ptr, 64, 0);\n}\nTEST_END\n\nTEST_BEGIN(test_alignment_and_size) {\n\tsize_t nsz, sz, alignment, total;\n\tunsigned i;\n\tvoid *ps[NITER];\n\n\tfor (i = 0; i < NITER; i++) {\n\t\tps[i] = NULL;\n\t}\n\n\tfor (alignment = 8;\n\t    alignment <= MAXALIGN;\n\t    alignment <<= 1) {\n\t\ttotal = 0;\n\t\tfor (sz = 1;\n\t\t    sz < 3 * alignment && sz < (1U << 31);\n\t\t    sz += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {\n\t\t\tfor (i = 0; i < NITER; i++) {\n\t\t\t\tnsz = nallocx(sz, MALLOCX_ALIGN(alignment) |\n\t\t\t\t    MALLOCX_ZERO);\n\t\t\t\tps[i] = mallocx(sz, MALLOCX_ALIGN(alignment) |\n\t\t\t\t    MALLOCX_ZERO);\n\t\t\t\ttotal += nsz;\n\t\t\t\tif (total >= (MAXALIGN << 1)) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (i = 0; i < NITER; i++) {\n\t\t\t\tif (ps[i] != NULL) {\n\t\t\t\t\tsdallocx(ps[i], sz,\n\t\t\t\t\t    MALLOCX_ALIGN(alignment));\n\t\t\t\t\tps[i] = NULL;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_basic,\n\t    test_alignment_and_size);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/integration/thread_arena.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#define NTHREADS 10\n\nvoid *\nthd_start(void *arg) {\n\tunsigned main_arena_ind = *(unsigned *)arg;\n\tvoid *p;\n\tunsigned arena_ind;\n\tsize_t size;\n\tint err;\n\n\tp = malloc(1);\n\tassert_ptr_not_null(p, \"Error in malloc()\");\n\tfree(p);\n\n\tsize = sizeof(arena_ind);\n\tif ((err = mallctl(\"thread.arena\", (void *)&arena_ind, &size,\n\t    (void *)&main_arena_ind, sizeof(main_arena_ind)))) {\n\t\tchar buf[BUFERROR_BUF];\n\n\t\tbuferror(err, buf, sizeof(buf));\n\t\ttest_fail(\"Error in mallctl(): %s\", buf);\n\t}\n\n\tsize = sizeof(arena_ind);\n\tif ((err = mallctl(\"thread.arena\", (void *)&arena_ind, &size, NULL,\n\t    0))) {\n\t\tchar buf[BUFERROR_BUF];\n\n\t\tbuferror(err, buf, sizeof(buf));\n\t\ttest_fail(\"Error in mallctl(): %s\", buf);\n\t}\n\tassert_u_eq(arena_ind, main_arena_ind,\n\t    \"Arena index should be same as for main thread\");\n\n\treturn NULL;\n}\n\nstatic void\nmallctl_failure(int err) {\n\tchar buf[BUFERROR_BUF];\n\n\tbuferror(err, buf, sizeof(buf));\n\ttest_fail(\"Error in mallctl(): %s\", buf);\n}\n\nTEST_BEGIN(test_thread_arena) {\n\tvoid *p;\n\tint err;\n\tthd_t thds[NTHREADS];\n\tunsigned i;\n\n\tp = malloc(1);\n\tassert_ptr_not_null(p, \"Error in malloc()\");\n\n\tunsigned arena_ind, old_arena_ind;\n\tsize_t sz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena_ind, &sz, NULL, 0),\n\t    0, \"Arena creation failure\");\n\n\tsize_t size = sizeof(arena_ind);\n\tif ((err = mallctl(\"thread.arena\", (void *)&old_arena_ind, &size,\n\t    (void *)&arena_ind, sizeof(arena_ind))) != 0) {\n\t\tmallctl_failure(err);\n\t}\n\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tthd_create(&thds[i], thd_start,\n\t\t    (void *)&arena_ind);\n\t}\n\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tintptr_t join_ret;\n\t\tthd_join(thds[i], (void *)&join_ret);\n\t\tassert_zd_eq(join_ret, 0, \"Unexpected thread join error\");\n\t}\n\tfree(p);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_thread_arena);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/integration/thread_tcache_enabled.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nvoid *\nthd_start(void *arg) {\n\tbool e0, e1;\n\tsize_t sz = sizeof(bool);\n\tassert_d_eq(mallctl(\"thread.tcache.enabled\", (void *)&e0, &sz, NULL,\n\t    0), 0, \"Unexpected mallctl failure\");\n\n\tif (e0) {\n\t\te1 = false;\n\t\tassert_d_eq(mallctl(\"thread.tcache.enabled\", (void *)&e0, &sz,\n\t\t    (void *)&e1, sz), 0, \"Unexpected mallctl() error\");\n\t\tassert_true(e0, \"tcache should be enabled\");\n\t}\n\n\te1 = true;\n\tassert_d_eq(mallctl(\"thread.tcache.enabled\", (void *)&e0, &sz,\n\t    (void *)&e1, sz), 0, \"Unexpected mallctl() error\");\n\tassert_false(e0, \"tcache should be disabled\");\n\n\te1 = true;\n\tassert_d_eq(mallctl(\"thread.tcache.enabled\", (void *)&e0, &sz,\n\t    (void *)&e1, sz), 0, \"Unexpected mallctl() error\");\n\tassert_true(e0, \"tcache should be enabled\");\n\n\te1 = false;\n\tassert_d_eq(mallctl(\"thread.tcache.enabled\", (void *)&e0, &sz,\n\t    (void *)&e1, sz), 0, \"Unexpected mallctl() error\");\n\tassert_true(e0, \"tcache should be enabled\");\n\n\te1 = false;\n\tassert_d_eq(mallctl(\"thread.tcache.enabled\", (void *)&e0, &sz,\n\t    (void *)&e1, sz), 0, \"Unexpected mallctl() error\");\n\tassert_false(e0, \"tcache should be disabled\");\n\n\tfree(malloc(1));\n\te1 = true;\n\tassert_d_eq(mallctl(\"thread.tcache.enabled\", (void *)&e0, &sz,\n\t    (void *)&e1, sz), 0, \"Unexpected mallctl() error\");\n\tassert_false(e0, \"tcache should be disabled\");\n\n\tfree(malloc(1));\n\te1 = true;\n\tassert_d_eq(mallctl(\"thread.tcache.enabled\", (void *)&e0, &sz,\n\t    (void *)&e1, sz), 0, \"Unexpected mallctl() error\");\n\tassert_true(e0, \"tcache should be enabled\");\n\n\tfree(malloc(1));\n\te1 = false;\n\tassert_d_eq(mallctl(\"thread.tcache.enabled\", (void *)&e0, &sz,\n\t    (void *)&e1, sz), 0, \"Unexpected mallctl() error\");\n\tassert_true(e0, \"tcache should be enabled\");\n\n\tfree(malloc(1));\n\te1 = false;\n\tassert_d_eq(mallctl(\"thread.tcache.enabled\", (void *)&e0, &sz,\n\t    (void *)&e1, sz), 0, \"Unexpected mallctl() error\");\n\tassert_false(e0, \"tcache should be disabled\");\n\n\tfree(malloc(1));\n\treturn NULL;\n\ttest_skip(\"\\\"thread.tcache.enabled\\\" mallctl not available\");\n\treturn NULL;\n}\n\nTEST_BEGIN(test_main_thread) {\n\tthd_start(NULL);\n}\nTEST_END\n\nTEST_BEGIN(test_subthread) {\n\tthd_t thd;\n\n\tthd_create(&thd, thd_start, NULL);\n\tthd_join(thd, NULL);\n}\nTEST_END\n\nint\nmain(void) {\n\t/* Run tests multiple times to check for bad interactions. */\n\treturn test(\n\t    test_main_thread,\n\t    test_subthread,\n\t    test_main_thread,\n\t    test_subthread,\n\t    test_main_thread);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/integration/xallocx.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n/*\n * Use a separate arena for xallocx() extension/contraction tests so that\n * internal allocation e.g. by heap profiling can't interpose allocations where\n * xallocx() would ordinarily be able to extend.\n */\nstatic unsigned\narena_ind(void) {\n\tstatic unsigned ind = 0;\n\n\tif (ind == 0) {\n\t\tsize_t sz = sizeof(ind);\n\t\tassert_d_eq(mallctl(\"arenas.create\", (void *)&ind, &sz, NULL,\n\t\t    0), 0, \"Unexpected mallctl failure creating arena\");\n\t}\n\n\treturn ind;\n}\n\nTEST_BEGIN(test_same_size) {\n\tvoid *p;\n\tsize_t sz, tsz;\n\n\tp = mallocx(42, 0);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\tsz = sallocx(p, 0);\n\n\ttsz = xallocx(p, sz, 0, 0);\n\tassert_zu_eq(tsz, sz, \"Unexpected size change: %zu --> %zu\", sz, tsz);\n\n\tdallocx(p, 0);\n}\nTEST_END\n\nTEST_BEGIN(test_extra_no_move) {\n\tvoid *p;\n\tsize_t sz, tsz;\n\n\tp = mallocx(42, 0);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\tsz = sallocx(p, 0);\n\n\ttsz = xallocx(p, sz, sz-42, 0);\n\tassert_zu_eq(tsz, sz, \"Unexpected size change: %zu --> %zu\", sz, tsz);\n\n\tdallocx(p, 0);\n}\nTEST_END\n\nTEST_BEGIN(test_no_move_fail) {\n\tvoid *p;\n\tsize_t sz, tsz;\n\n\tp = mallocx(42, 0);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\tsz = sallocx(p, 0);\n\n\ttsz = xallocx(p, sz + 5, 0, 0);\n\tassert_zu_eq(tsz, sz, \"Unexpected size change: %zu --> %zu\", sz, tsz);\n\n\tdallocx(p, 0);\n}\nTEST_END\n\nstatic unsigned\nget_nsizes_impl(const char *cmd) {\n\tunsigned ret;\n\tsize_t z;\n\n\tz = sizeof(unsigned);\n\tassert_d_eq(mallctl(cmd, (void *)&ret, &z, NULL, 0), 0,\n\t    \"Unexpected mallctl(\\\"%s\\\", ...) failure\", cmd);\n\n\treturn ret;\n}\n\nstatic unsigned\nget_nsmall(void) {\n\treturn get_nsizes_impl(\"arenas.nbins\");\n}\n\nstatic unsigned\nget_nlarge(void) {\n\treturn get_nsizes_impl(\"arenas.nlextents\");\n}\n\nstatic size_t\nget_size_impl(const char *cmd, size_t ind) {\n\tsize_t ret;\n\tsize_t z;\n\tsize_t mib[4];\n\tsize_t miblen = 4;\n\n\tz = sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(cmd, mib, &miblen),\n\t    0, \"Unexpected mallctlnametomib(\\\"%s\\\", ...) failure\", cmd);\n\tmib[2] = ind;\n\tz = sizeof(size_t);\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&ret, &z, NULL, 0),\n\t    0, \"Unexpected mallctlbymib([\\\"%s\\\", %zu], ...) failure\", cmd, ind);\n\n\treturn ret;\n}\n\nstatic size_t\nget_small_size(size_t ind) {\n\treturn get_size_impl(\"arenas.bin.0.size\", ind);\n}\n\nstatic size_t\nget_large_size(size_t ind) {\n\treturn get_size_impl(\"arenas.lextent.0.size\", ind);\n}\n\nTEST_BEGIN(test_size) {\n\tsize_t small0, largemax;\n\tvoid *p;\n\n\t/* Get size classes. */\n\tsmall0 = get_small_size(0);\n\tlargemax = get_large_size(get_nlarge()-1);\n\n\tp = mallocx(small0, 0);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\n\t/* Test smallest supported size. */\n\tassert_zu_eq(xallocx(p, 1, 0, 0), small0,\n\t    \"Unexpected xallocx() behavior\");\n\n\t/* Test largest supported size. */\n\tassert_zu_le(xallocx(p, largemax, 0, 0), largemax,\n\t    \"Unexpected xallocx() behavior\");\n\n\t/* Test size overflow. */\n\tassert_zu_le(xallocx(p, largemax+1, 0, 0), largemax,\n\t    \"Unexpected xallocx() behavior\");\n\tassert_zu_le(xallocx(p, SIZE_T_MAX, 0, 0), largemax,\n\t    \"Unexpected xallocx() behavior\");\n\n\tdallocx(p, 0);\n}\nTEST_END\n\nTEST_BEGIN(test_size_extra_overflow) {\n\tsize_t small0, largemax;\n\tvoid *p;\n\n\t/* Get size classes. */\n\tsmall0 = get_small_size(0);\n\tlargemax = get_large_size(get_nlarge()-1);\n\n\tp = mallocx(small0, 0);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\n\t/* Test overflows that can be resolved by clamping extra. */\n\tassert_zu_le(xallocx(p, largemax-1, 2, 0), largemax,\n\t    \"Unexpected xallocx() behavior\");\n\tassert_zu_le(xallocx(p, largemax, 1, 0), largemax,\n\t    \"Unexpected xallocx() behavior\");\n\n\t/* Test overflow such that largemax-size underflows. */\n\tassert_zu_le(xallocx(p, largemax+1, 2, 0), largemax,\n\t    \"Unexpected xallocx() behavior\");\n\tassert_zu_le(xallocx(p, largemax+2, 3, 0), largemax,\n\t    \"Unexpected xallocx() behavior\");\n\tassert_zu_le(xallocx(p, SIZE_T_MAX-2, 2, 0), largemax,\n\t    \"Unexpected xallocx() behavior\");\n\tassert_zu_le(xallocx(p, SIZE_T_MAX-1, 1, 0), largemax,\n\t    \"Unexpected xallocx() behavior\");\n\n\tdallocx(p, 0);\n}\nTEST_END\n\nTEST_BEGIN(test_extra_small) {\n\tsize_t small0, small1, largemax;\n\tvoid *p;\n\n\t/* Get size classes. */\n\tsmall0 = get_small_size(0);\n\tsmall1 = get_small_size(1);\n\tlargemax = get_large_size(get_nlarge()-1);\n\n\tp = mallocx(small0, 0);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\n\tassert_zu_eq(xallocx(p, small1, 0, 0), small0,\n\t    \"Unexpected xallocx() behavior\");\n\n\tassert_zu_eq(xallocx(p, small1, 0, 0), small0,\n\t    \"Unexpected xallocx() behavior\");\n\n\tassert_zu_eq(xallocx(p, small0, small1 - small0, 0), small0,\n\t    \"Unexpected xallocx() behavior\");\n\n\t/* Test size+extra overflow. */\n\tassert_zu_eq(xallocx(p, small0, largemax - small0 + 1, 0), small0,\n\t    \"Unexpected xallocx() behavior\");\n\tassert_zu_eq(xallocx(p, small0, SIZE_T_MAX - small0, 0), small0,\n\t    \"Unexpected xallocx() behavior\");\n\n\tdallocx(p, 0);\n}\nTEST_END\n\nTEST_BEGIN(test_extra_large) {\n\tint flags = MALLOCX_ARENA(arena_ind());\n\tsize_t smallmax, large1, large2, large3, largemax;\n\tvoid *p;\n\n\t/* Get size classes. */\n\tsmallmax = get_small_size(get_nsmall()-1);\n\tlarge1 = get_large_size(1);\n\tlarge2 = get_large_size(2);\n\tlarge3 = get_large_size(3);\n\tlargemax = get_large_size(get_nlarge()-1);\n\n\tp = mallocx(large3, flags);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\n\tassert_zu_eq(xallocx(p, large3, 0, flags), large3,\n\t    \"Unexpected xallocx() behavior\");\n\t/* Test size decrease with zero extra. */\n\tassert_zu_ge(xallocx(p, large1, 0, flags), large1,\n\t    \"Unexpected xallocx() behavior\");\n\tassert_zu_ge(xallocx(p, smallmax, 0, flags), large1,\n\t    \"Unexpected xallocx() behavior\");\n\n\tif (xallocx(p, large3, 0, flags) != large3) {\n\t\tp = rallocx(p, large3, flags);\n\t\tassert_ptr_not_null(p, \"Unexpected rallocx() failure\");\n\t}\n\t/* Test size decrease with non-zero extra. */\n\tassert_zu_eq(xallocx(p, large1, large3 - large1, flags), large3,\n\t    \"Unexpected xallocx() behavior\");\n\tassert_zu_eq(xallocx(p, large2, large3 - large2, flags), large3,\n\t    \"Unexpected xallocx() behavior\");\n\tassert_zu_ge(xallocx(p, large1, large2 - large1, flags), large2,\n\t    \"Unexpected xallocx() behavior\");\n\tassert_zu_ge(xallocx(p, smallmax, large1 - smallmax, flags), large1,\n\t    \"Unexpected xallocx() behavior\");\n\n\tassert_zu_ge(xallocx(p, large1, 0, flags), large1,\n\t    \"Unexpected xallocx() behavior\");\n\t/* Test size increase with zero extra. */\n\tassert_zu_le(xallocx(p, large3, 0, flags), large3,\n\t    \"Unexpected xallocx() behavior\");\n\tassert_zu_le(xallocx(p, largemax+1, 0, flags), large3,\n\t    \"Unexpected xallocx() behavior\");\n\n\tassert_zu_ge(xallocx(p, large1, 0, flags), large1,\n\t    \"Unexpected xallocx() behavior\");\n\t/* Test size increase with non-zero extra. */\n\tassert_zu_le(xallocx(p, large1, SIZE_T_MAX - large1, flags), largemax,\n\t    \"Unexpected xallocx() behavior\");\n\n\tassert_zu_ge(xallocx(p, large1, 0, flags), large1,\n\t    \"Unexpected xallocx() behavior\");\n\t/* Test size increase with non-zero extra. */\n\tassert_zu_le(xallocx(p, large1, large3 - large1, flags), large3,\n\t    \"Unexpected xallocx() behavior\");\n\n\tif (xallocx(p, large3, 0, flags) != large3) {\n\t\tp = rallocx(p, large3, flags);\n\t\tassert_ptr_not_null(p, \"Unexpected rallocx() failure\");\n\t}\n\t/* Test size+extra overflow. */\n\tassert_zu_le(xallocx(p, large3, largemax - large3 + 1, flags), largemax,\n\t    \"Unexpected xallocx() behavior\");\n\n\tdallocx(p, flags);\n}\nTEST_END\n\nstatic void\nprint_filled_extents(const void *p, uint8_t c, size_t len) {\n\tconst uint8_t *pc = (const uint8_t *)p;\n\tsize_t i, range0;\n\tuint8_t c0;\n\n\tmalloc_printf(\"  p=%p, c=%#x, len=%zu:\", p, c, len);\n\trange0 = 0;\n\tc0 = pc[0];\n\tfor (i = 0; i < len; i++) {\n\t\tif (pc[i] != c0) {\n\t\t\tmalloc_printf(\" %#x[%zu..%zu)\", c0, range0, i);\n\t\t\trange0 = i;\n\t\t\tc0 = pc[i];\n\t\t}\n\t}\n\tmalloc_printf(\" %#x[%zu..%zu)\\n\", c0, range0, i);\n}\n\nstatic bool\nvalidate_fill(const void *p, uint8_t c, size_t offset, size_t len) {\n\tconst uint8_t *pc = (const uint8_t *)p;\n\tbool err;\n\tsize_t i;\n\n\tfor (i = offset, err = false; i < offset+len; i++) {\n\t\tif (pc[i] != c) {\n\t\t\terr = true;\n\t\t}\n\t}\n\n\tif (err) {\n\t\tprint_filled_extents(p, c, offset + len);\n\t}\n\n\treturn err;\n}\n\nstatic void\ntest_zero(size_t szmin, size_t szmax) {\n\tint flags = MALLOCX_ARENA(arena_ind()) | MALLOCX_ZERO;\n\tsize_t sz, nsz;\n\tvoid *p;\n#define FILL_BYTE 0x7aU\n\n\tsz = szmax;\n\tp = mallocx(sz, flags);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() error\");\n\tassert_false(validate_fill(p, 0x00, 0, sz), \"Memory not filled: sz=%zu\",\n\t    sz);\n\n\t/*\n\t * Fill with non-zero so that non-debug builds are more likely to detect\n\t * errors.\n\t */\n\tmemset(p, FILL_BYTE, sz);\n\tassert_false(validate_fill(p, FILL_BYTE, 0, sz),\n\t    \"Memory not filled: sz=%zu\", sz);\n\n\t/* Shrink in place so that we can expect growing in place to succeed. */\n\tsz = szmin;\n\tif (xallocx(p, sz, 0, flags) != sz) {\n\t\tp = rallocx(p, sz, flags);\n\t\tassert_ptr_not_null(p, \"Unexpected rallocx() failure\");\n\t}\n\tassert_false(validate_fill(p, FILL_BYTE, 0, sz),\n\t    \"Memory not filled: sz=%zu\", sz);\n\n\tfor (sz = szmin; sz < szmax; sz = nsz) {\n\t\tnsz = nallocx(sz+1, flags);\n\t\tif (xallocx(p, sz+1, 0, flags) != nsz) {\n\t\t\tp = rallocx(p, sz+1, flags);\n\t\t\tassert_ptr_not_null(p, \"Unexpected rallocx() failure\");\n\t\t}\n\t\tassert_false(validate_fill(p, FILL_BYTE, 0, sz),\n\t\t    \"Memory not filled: sz=%zu\", sz);\n\t\tassert_false(validate_fill(p, 0x00, sz, nsz-sz),\n\t\t    \"Memory not filled: sz=%zu, nsz-sz=%zu\", sz, nsz-sz);\n\t\tmemset((void *)((uintptr_t)p + sz), FILL_BYTE, nsz-sz);\n\t\tassert_false(validate_fill(p, FILL_BYTE, 0, nsz),\n\t\t    \"Memory not filled: nsz=%zu\", nsz);\n\t}\n\n\tdallocx(p, flags);\n}\n\nTEST_BEGIN(test_zero_large) {\n\tsize_t large0, large1;\n\n\t/* Get size classes. */\n\tlarge0 = get_large_size(0);\n\tlarge1 = get_large_size(1);\n\n\ttest_zero(large1, large0 * 2);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_same_size,\n\t    test_extra_no_move,\n\t    test_no_move_fail,\n\t    test_size,\n\t    test_size_extra_overflow,\n\t    test_extra_small,\n\t    test_extra_large,\n\t    test_zero_large);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/integration/xallocx.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_fill}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"junk:false\"\nfi\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/src/SFMT.c",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n/**\n * @file  SFMT.c\n * @brief SIMD oriented Fast Mersenne Twister(SFMT)\n *\n * @author Mutsuo Saito (Hiroshima University)\n * @author Makoto Matsumoto (Hiroshima University)\n *\n * Copyright (C) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n * University. All rights reserved.\n *\n * The new BSD License is applied to this software, see LICENSE.txt\n */\n#define SFMT_C_\n#include \"test/jemalloc_test.h\"\n#include \"test/SFMT-params.h\"\n\n#if defined(JEMALLOC_BIG_ENDIAN) && !defined(BIG_ENDIAN64)\n#define BIG_ENDIAN64 1\n#endif\n#if defined(__BIG_ENDIAN__) && !defined(__amd64) && !defined(BIG_ENDIAN64)\n#define BIG_ENDIAN64 1\n#endif\n#if defined(HAVE_ALTIVEC) && !defined(BIG_ENDIAN64)\n#define BIG_ENDIAN64 1\n#endif\n#if defined(ONLY64) && !defined(BIG_ENDIAN64)\n  #if defined(__GNUC__)\n    #error \"-DONLY64 must be specified with -DBIG_ENDIAN64\"\n  #endif\n#undef ONLY64\n#endif\n/*------------------------------------------------------\n  128-bit SIMD data type for Altivec, SSE2 or standard C\n  ------------------------------------------------------*/\n#if defined(HAVE_ALTIVEC)\n/** 128-bit data structure */\nunion W128_T {\n    vector unsigned int s;\n    uint32_t u[4];\n};\n/** 128-bit data type */\ntypedef union W128_T w128_t;\n\n#elif defined(HAVE_SSE2)\n/** 128-bit data structure */\nunion W128_T {\n    __m128i si;\n    uint32_t u[4];\n};\n/** 128-bit data type */\ntypedef union W128_T w128_t;\n\n#else\n\n/** 128-bit data structure */\nstruct W128_T {\n    uint32_t u[4];\n};\n/** 128-bit data type */\ntypedef struct W128_T w128_t;\n\n#endif\n\nstruct sfmt_s {\n    /** the 128-bit internal state array */\n    w128_t sfmt[N];\n    /** index counter to the 32-bit internal state array */\n    int idx;\n    /** a flag: it is 0 if and only if the internal state is not yet\n     * initialized. */\n    int initialized;\n};\n\n/*--------------------------------------\n  FILE GLOBAL VARIABLES\n  internal state, index counter and flag\n  --------------------------------------*/\n\n/** a parity check vector which certificate the period of 2^{MEXP} */\nstatic uint32_t parity[4] = {PARITY1, PARITY2, PARITY3, PARITY4};\n\n/*----------------\n  STATIC FUNCTIONS\n  ----------------*/\nstatic inline int idxof(int i);\n#if (!defined(HAVE_ALTIVEC)) && (!defined(HAVE_SSE2))\nstatic inline void rshift128(w128_t *out,  w128_t const *in, int shift);\nstatic inline void lshift128(w128_t *out,  w128_t const *in, int shift);\n#endif\nstatic inline void gen_rand_all(sfmt_t *ctx);\nstatic inline void gen_rand_array(sfmt_t *ctx, w128_t *array, int size);\nstatic inline uint32_t func1(uint32_t x);\nstatic inline uint32_t func2(uint32_t x);\nstatic void period_certification(sfmt_t *ctx);\n#if defined(BIG_ENDIAN64) && !defined(ONLY64)\nstatic inline void swap(w128_t *array, int size);\n#endif\n\n#if defined(HAVE_ALTIVEC)\n  #include \"test/SFMT-alti.h\"\n#elif defined(HAVE_SSE2)\n  #include \"test/SFMT-sse2.h\"\n#endif\n\n/**\n * This function simulate a 64-bit index of LITTLE ENDIAN\n * in BIG ENDIAN machine.\n */\n#ifdef ONLY64\nstatic inline int idxof(int i) {\n    return i ^ 1;\n}\n#else\nstatic inline int idxof(int i) {\n    return i;\n}\n#endif\n/**\n * This function simulates SIMD 128-bit right shift by the standard C.\n * The 128-bit integer given in in is shifted by (shift * 8) bits.\n * This function simulates the LITTLE ENDIAN SIMD.\n * @param out the output of this function\n * @param in the 128-bit data to be shifted\n * @param shift the shift value\n */\n#if (!defined(HAVE_ALTIVEC)) && (!defined(HAVE_SSE2))\n#ifdef ONLY64\nstatic inline void rshift128(w128_t *out, w128_t const *in, int shift) {\n    uint64_t th, tl, oh, ol;\n\n    th = ((uint64_t)in->u[2] << 32) | ((uint64_t)in->u[3]);\n    tl = ((uint64_t)in->u[0] << 32) | ((uint64_t)in->u[1]);\n\n    oh = th >> (shift * 8);\n    ol = tl >> (shift * 8);\n    ol |= th << (64 - shift * 8);\n    out->u[0] = (uint32_t)(ol >> 32);\n    out->u[1] = (uint32_t)ol;\n    out->u[2] = (uint32_t)(oh >> 32);\n    out->u[3] = (uint32_t)oh;\n}\n#else\nstatic inline void rshift128(w128_t *out, w128_t const *in, int shift) {\n    uint64_t th, tl, oh, ol;\n\n    th = ((uint64_t)in->u[3] << 32) | ((uint64_t)in->u[2]);\n    tl = ((uint64_t)in->u[1] << 32) | ((uint64_t)in->u[0]);\n\n    oh = th >> (shift * 8);\n    ol = tl >> (shift * 8);\n    ol |= th << (64 - shift * 8);\n    out->u[1] = (uint32_t)(ol >> 32);\n    out->u[0] = (uint32_t)ol;\n    out->u[3] = (uint32_t)(oh >> 32);\n    out->u[2] = (uint32_t)oh;\n}\n#endif\n/**\n * This function simulates SIMD 128-bit left shift by the standard C.\n * The 128-bit integer given in in is shifted by (shift * 8) bits.\n * This function simulates the LITTLE ENDIAN SIMD.\n * @param out the output of this function\n * @param in the 128-bit data to be shifted\n * @param shift the shift value\n */\n#ifdef ONLY64\nstatic inline void lshift128(w128_t *out, w128_t const *in, int shift) {\n    uint64_t th, tl, oh, ol;\n\n    th = ((uint64_t)in->u[2] << 32) | ((uint64_t)in->u[3]);\n    tl = ((uint64_t)in->u[0] << 32) | ((uint64_t)in->u[1]);\n\n    oh = th << (shift * 8);\n    ol = tl << (shift * 8);\n    oh |= tl >> (64 - shift * 8);\n    out->u[0] = (uint32_t)(ol >> 32);\n    out->u[1] = (uint32_t)ol;\n    out->u[2] = (uint32_t)(oh >> 32);\n    out->u[3] = (uint32_t)oh;\n}\n#else\nstatic inline void lshift128(w128_t *out, w128_t const *in, int shift) {\n    uint64_t th, tl, oh, ol;\n\n    th = ((uint64_t)in->u[3] << 32) | ((uint64_t)in->u[2]);\n    tl = ((uint64_t)in->u[1] << 32) | ((uint64_t)in->u[0]);\n\n    oh = th << (shift * 8);\n    ol = tl << (shift * 8);\n    oh |= tl >> (64 - shift * 8);\n    out->u[1] = (uint32_t)(ol >> 32);\n    out->u[0] = (uint32_t)ol;\n    out->u[3] = (uint32_t)(oh >> 32);\n    out->u[2] = (uint32_t)oh;\n}\n#endif\n#endif\n\n/**\n * This function represents the recursion formula.\n * @param r output\n * @param a a 128-bit part of the internal state array\n * @param b a 128-bit part of the internal state array\n * @param c a 128-bit part of the internal state array\n * @param d a 128-bit part of the internal state array\n */\n#if (!defined(HAVE_ALTIVEC)) && (!defined(HAVE_SSE2))\n#ifdef ONLY64\nstatic inline void do_recursion(w128_t *r, w128_t *a, w128_t *b, w128_t *c,\n\t\t\t\tw128_t *d) {\n    w128_t x;\n    w128_t y;\n\n    lshift128(&x, a, SL2);\n    rshift128(&y, c, SR2);\n    r->u[0] = a->u[0] ^ x.u[0] ^ ((b->u[0] >> SR1) & MSK2) ^ y.u[0]\n\t^ (d->u[0] << SL1);\n    r->u[1] = a->u[1] ^ x.u[1] ^ ((b->u[1] >> SR1) & MSK1) ^ y.u[1]\n\t^ (d->u[1] << SL1);\n    r->u[2] = a->u[2] ^ x.u[2] ^ ((b->u[2] >> SR1) & MSK4) ^ y.u[2]\n\t^ (d->u[2] << SL1);\n    r->u[3] = a->u[3] ^ x.u[3] ^ ((b->u[3] >> SR1) & MSK3) ^ y.u[3]\n\t^ (d->u[3] << SL1);\n}\n#else\nstatic inline void do_recursion(w128_t *r, w128_t *a, w128_t *b, w128_t *c,\n\t\t\t\tw128_t *d) {\n    w128_t x;\n    w128_t y;\n\n    lshift128(&x, a, SL2);\n    rshift128(&y, c, SR2);\n    r->u[0] = a->u[0] ^ x.u[0] ^ ((b->u[0] >> SR1) & MSK1) ^ y.u[0]\n\t^ (d->u[0] << SL1);\n    r->u[1] = a->u[1] ^ x.u[1] ^ ((b->u[1] >> SR1) & MSK2) ^ y.u[1]\n\t^ (d->u[1] << SL1);\n    r->u[2] = a->u[2] ^ x.u[2] ^ ((b->u[2] >> SR1) & MSK3) ^ y.u[2]\n\t^ (d->u[2] << SL1);\n    r->u[3] = a->u[3] ^ x.u[3] ^ ((b->u[3] >> SR1) & MSK4) ^ y.u[3]\n\t^ (d->u[3] << SL1);\n}\n#endif\n#endif\n\n#if (!defined(HAVE_ALTIVEC)) && (!defined(HAVE_SSE2))\n/**\n * This function fills the internal state array with pseudorandom\n * integers.\n */\nstatic inline void gen_rand_all(sfmt_t *ctx) {\n    int i;\n    w128_t *r1, *r2;\n\n    r1 = &ctx->sfmt[N - 2];\n    r2 = &ctx->sfmt[N - 1];\n    for (i = 0; i < N - POS1; i++) {\n\tdo_recursion(&ctx->sfmt[i], &ctx->sfmt[i], &ctx->sfmt[i + POS1], r1,\n\t  r2);\n\tr1 = r2;\n\tr2 = &ctx->sfmt[i];\n    }\n    for (; i < N; i++) {\n\tdo_recursion(&ctx->sfmt[i], &ctx->sfmt[i], &ctx->sfmt[i + POS1 - N], r1,\n\t  r2);\n\tr1 = r2;\n\tr2 = &ctx->sfmt[i];\n    }\n}\n\n/**\n * This function fills the user-specified array with pseudorandom\n * integers.\n *\n * @param array an 128-bit array to be filled by pseudorandom numbers.\n * @param size number of 128-bit pseudorandom numbers to be generated.\n */\nstatic inline void gen_rand_array(sfmt_t *ctx, w128_t *array, int size) {\n    int i, j;\n    w128_t *r1, *r2;\n\n    r1 = &ctx->sfmt[N - 2];\n    r2 = &ctx->sfmt[N - 1];\n    for (i = 0; i < N - POS1; i++) {\n\tdo_recursion(&array[i], &ctx->sfmt[i], &ctx->sfmt[i + POS1], r1, r2);\n\tr1 = r2;\n\tr2 = &array[i];\n    }\n    for (; i < N; i++) {\n\tdo_recursion(&array[i], &ctx->sfmt[i], &array[i + POS1 - N], r1, r2);\n\tr1 = r2;\n\tr2 = &array[i];\n    }\n    for (; i < size - N; i++) {\n\tdo_recursion(&array[i], &array[i - N], &array[i + POS1 - N], r1, r2);\n\tr1 = r2;\n\tr2 = &array[i];\n    }\n    for (j = 0; j < 2 * N - size; j++) {\n\tctx->sfmt[j] = array[j + size - N];\n    }\n    for (; i < size; i++, j++) {\n\tdo_recursion(&array[i], &array[i - N], &array[i + POS1 - N], r1, r2);\n\tr1 = r2;\n\tr2 = &array[i];\n\tctx->sfmt[j] = array[i];\n    }\n}\n#endif\n\n#if defined(BIG_ENDIAN64) && !defined(ONLY64) && !defined(HAVE_ALTIVEC)\nstatic inline void swap(w128_t *array, int size) {\n    int i;\n    uint32_t x, y;\n\n    for (i = 0; i < size; i++) {\n\tx = array[i].u[0];\n\ty = array[i].u[2];\n\tarray[i].u[0] = array[i].u[1];\n\tarray[i].u[2] = array[i].u[3];\n\tarray[i].u[1] = x;\n\tarray[i].u[3] = y;\n    }\n}\n#endif\n/**\n * This function represents a function used in the initialization\n * by init_by_array\n * @param x 32-bit integer\n * @return 32-bit integer\n */\nstatic uint32_t func1(uint32_t x) {\n    return (x ^ (x >> 27)) * (uint32_t)1664525UL;\n}\n\n/**\n * This function represents a function used in the initialization\n * by init_by_array\n * @param x 32-bit integer\n * @return 32-bit integer\n */\nstatic uint32_t func2(uint32_t x) {\n    return (x ^ (x >> 27)) * (uint32_t)1566083941UL;\n}\n\n/**\n * This function certificate the period of 2^{MEXP}\n */\nstatic void period_certification(sfmt_t *ctx) {\n    int inner = 0;\n    int i, j;\n    uint32_t work;\n    uint32_t *psfmt32 = &ctx->sfmt[0].u[0];\n\n    for (i = 0; i < 4; i++)\n\tinner ^= psfmt32[idxof(i)] & parity[i];\n    for (i = 16; i > 0; i >>= 1)\n\tinner ^= inner >> i;\n    inner &= 1;\n    /* check OK */\n    if (inner == 1) {\n\treturn;\n    }\n    /* check NG, and modification */\n    for (i = 0; i < 4; i++) {\n\twork = 1;\n\tfor (j = 0; j < 32; j++) {\n\t    if ((work & parity[i]) != 0) {\n\t\tpsfmt32[idxof(i)] ^= work;\n\t\treturn;\n\t    }\n\t    work = work << 1;\n\t}\n    }\n}\n\n/*----------------\n  PUBLIC FUNCTIONS\n  ----------------*/\n/**\n * This function returns the identification string.\n * The string shows the word size, the Mersenne exponent,\n * and all parameters of this generator.\n */\nconst char *get_idstring(void) {\n    return IDSTR;\n}\n\n/**\n * This function returns the minimum size of array used for \\b\n * fill_array32() function.\n * @return minimum size of array used for fill_array32() function.\n */\nint get_min_array_size32(void) {\n    return N32;\n}\n\n/**\n * This function returns the minimum size of array used for \\b\n * fill_array64() function.\n * @return minimum size of array used for fill_array64() function.\n */\nint get_min_array_size64(void) {\n    return N64;\n}\n\n#ifndef ONLY64\n/**\n * This function generates and returns 32-bit pseudorandom number.\n * init_gen_rand or init_by_array must be called before this function.\n * @return 32-bit pseudorandom number\n */\nuint32_t gen_rand32(sfmt_t *ctx) {\n    uint32_t r;\n    uint32_t *psfmt32 = &ctx->sfmt[0].u[0];\n\n    assert(ctx->initialized);\n    if (ctx->idx >= N32) {\n\tgen_rand_all(ctx);\n\tctx->idx = 0;\n    }\n    r = psfmt32[ctx->idx++];\n    return r;\n}\n\n/* Generate a random integer in [0..limit). */\nuint32_t gen_rand32_range(sfmt_t *ctx, uint32_t limit) {\n    uint32_t ret, above;\n\n    above = 0xffffffffU - (0xffffffffU % limit);\n    while (1) {\n\tret = gen_rand32(ctx);\n\tif (ret < above) {\n\t    ret %= limit;\n\t    break;\n\t}\n    }\n    return ret;\n}\n#endif\n/**\n * This function generates and returns 64-bit pseudorandom number.\n * init_gen_rand or init_by_array must be called before this function.\n * The function gen_rand64 should not be called after gen_rand32,\n * unless an initialization is again executed.\n * @return 64-bit pseudorandom number\n */\nuint64_t gen_rand64(sfmt_t *ctx) {\n#if defined(BIG_ENDIAN64) && !defined(ONLY64)\n    uint32_t r1, r2;\n    uint32_t *psfmt32 = &ctx->sfmt[0].u[0];\n#else\n    uint64_t r;\n    uint64_t *psfmt64 = (uint64_t *)&ctx->sfmt[0].u[0];\n#endif\n\n    assert(ctx->initialized);\n    assert(ctx->idx % 2 == 0);\n\n    if (ctx->idx >= N32) {\n\tgen_rand_all(ctx);\n\tctx->idx = 0;\n    }\n#if defined(BIG_ENDIAN64) && !defined(ONLY64)\n    r1 = psfmt32[ctx->idx];\n    r2 = psfmt32[ctx->idx + 1];\n    ctx->idx += 2;\n    return ((uint64_t)r2 << 32) | r1;\n#else\n    r = psfmt64[ctx->idx / 2];\n    ctx->idx += 2;\n    return r;\n#endif\n}\n\n/* Generate a random integer in [0..limit). */\nuint64_t gen_rand64_range(sfmt_t *ctx, uint64_t limit) {\n    uint64_t ret, above;\n\n    above = KQU(0xffffffffffffffff) - (KQU(0xffffffffffffffff) % limit);\n    while (1) {\n\tret = gen_rand64(ctx);\n\tif (ret < above) {\n\t    ret %= limit;\n\t    break;\n\t}\n    }\n    return ret;\n}\n\n#ifndef ONLY64\n/**\n * This function generates pseudorandom 32-bit integers in the\n * specified array[] by one call. The number of pseudorandom integers\n * is specified by the argument size, which must be at least 624 and a\n * multiple of four.  The generation by this function is much faster\n * than the following gen_rand function.\n *\n * For initialization, init_gen_rand or init_by_array must be called\n * before the first call of this function. This function can not be\n * used after calling gen_rand function, without initialization.\n *\n * @param array an array where pseudorandom 32-bit integers are filled\n * by this function.  The pointer to the array must be \\b \"aligned\"\n * (namely, must be a multiple of 16) in the SIMD version, since it\n * refers to the address of a 128-bit integer.  In the standard C\n * version, the pointer is arbitrary.\n *\n * @param size the number of 32-bit pseudorandom integers to be\n * generated.  size must be a multiple of 4, and greater than or equal\n * to (MEXP / 128 + 1) * 4.\n *\n * @note \\b memalign or \\b posix_memalign is available to get aligned\n * memory. Mac OSX doesn't have these functions, but \\b malloc of OSX\n * returns the pointer to the aligned memory block.\n */\nvoid fill_array32(sfmt_t *ctx, uint32_t *array, int size) {\n    assert(ctx->initialized);\n    assert(ctx->idx == N32);\n    assert(size % 4 == 0);\n    assert(size >= N32);\n\n    gen_rand_array(ctx, (w128_t *)array, size / 4);\n    ctx->idx = N32;\n}\n#endif\n\n/**\n * This function generates pseudorandom 64-bit integers in the\n * specified array[] by one call. The number of pseudorandom integers\n * is specified by the argument size, which must be at least 312 and a\n * multiple of two.  The generation by this function is much faster\n * than the following gen_rand function.\n *\n * For initialization, init_gen_rand or init_by_array must be called\n * before the first call of this function. This function can not be\n * used after calling gen_rand function, without initialization.\n *\n * @param array an array where pseudorandom 64-bit integers are filled\n * by this function.  The pointer to the array must be \"aligned\"\n * (namely, must be a multiple of 16) in the SIMD version, since it\n * refers to the address of a 128-bit integer.  In the standard C\n * version, the pointer is arbitrary.\n *\n * @param size the number of 64-bit pseudorandom integers to be\n * generated.  size must be a multiple of 2, and greater than or equal\n * to (MEXP / 128 + 1) * 2\n *\n * @note \\b memalign or \\b posix_memalign is available to get aligned\n * memory. Mac OSX doesn't have these functions, but \\b malloc of OSX\n * returns the pointer to the aligned memory block.\n */\nvoid fill_array64(sfmt_t *ctx, uint64_t *array, int size) {\n    assert(ctx->initialized);\n    assert(ctx->idx == N32);\n    assert(size % 2 == 0);\n    assert(size >= N64);\n\n    gen_rand_array(ctx, (w128_t *)array, size / 2);\n    ctx->idx = N32;\n\n#if defined(BIG_ENDIAN64) && !defined(ONLY64)\n    swap((w128_t *)array, size /2);\n#endif\n}\n\n/**\n * This function initializes the internal state array with a 32-bit\n * integer seed.\n *\n * @param seed a 32-bit integer used as the seed.\n */\nsfmt_t *init_gen_rand(uint32_t seed) {\n    void *p;\n    sfmt_t *ctx;\n    int i;\n    uint32_t *psfmt32;\n\n    if (posix_memalign(&p, sizeof(w128_t), sizeof(sfmt_t)) != 0) {\n\treturn NULL;\n    }\n    ctx = (sfmt_t *)p;\n    psfmt32 = &ctx->sfmt[0].u[0];\n\n    psfmt32[idxof(0)] = seed;\n    for (i = 1; i < N32; i++) {\n\tpsfmt32[idxof(i)] = 1812433253UL * (psfmt32[idxof(i - 1)]\n\t\t\t\t\t    ^ (psfmt32[idxof(i - 1)] >> 30))\n\t    + i;\n    }\n    ctx->idx = N32;\n    period_certification(ctx);\n    ctx->initialized = 1;\n\n    return ctx;\n}\n\n/**\n * This function initializes the internal state array,\n * with an array of 32-bit integers used as the seeds\n * @param init_key the array of 32-bit integers, used as a seed.\n * @param key_length the length of init_key.\n */\nsfmt_t *init_by_array(uint32_t *init_key, int key_length) {\n    void *p;\n    sfmt_t *ctx;\n    int i, j, count;\n    uint32_t r;\n    int lag;\n    int mid;\n    int size = N * 4;\n    uint32_t *psfmt32;\n\n    if (posix_memalign(&p, sizeof(w128_t), sizeof(sfmt_t)) != 0) {\n\treturn NULL;\n    }\n    ctx = (sfmt_t *)p;\n    psfmt32 = &ctx->sfmt[0].u[0];\n\n    if (size >= 623) {\n\tlag = 11;\n    } else if (size >= 68) {\n\tlag = 7;\n    } else if (size >= 39) {\n\tlag = 5;\n    } else {\n\tlag = 3;\n    }\n    mid = (size - lag) / 2;\n\n    memset(ctx->sfmt, 0x8b, sizeof(ctx->sfmt));\n    if (key_length + 1 > N32) {\n\tcount = key_length + 1;\n    } else {\n\tcount = N32;\n    }\n    r = func1(psfmt32[idxof(0)] ^ psfmt32[idxof(mid)]\n\t      ^ psfmt32[idxof(N32 - 1)]);\n    psfmt32[idxof(mid)] += r;\n    r += key_length;\n    psfmt32[idxof(mid + lag)] += r;\n    psfmt32[idxof(0)] = r;\n\n    count--;\n    for (i = 1, j = 0; (j < count) && (j < key_length); j++) {\n\tr = func1(psfmt32[idxof(i)] ^ psfmt32[idxof((i + mid) % N32)]\n\t\t  ^ psfmt32[idxof((i + N32 - 1) % N32)]);\n\tpsfmt32[idxof((i + mid) % N32)] += r;\n\tr += init_key[j] + i;\n\tpsfmt32[idxof((i + mid + lag) % N32)] += r;\n\tpsfmt32[idxof(i)] = r;\n\ti = (i + 1) % N32;\n    }\n    for (; j < count; j++) {\n\tr = func1(psfmt32[idxof(i)] ^ psfmt32[idxof((i + mid) % N32)]\n\t\t  ^ psfmt32[idxof((i + N32 - 1) % N32)]);\n\tpsfmt32[idxof((i + mid) % N32)] += r;\n\tr += i;\n\tpsfmt32[idxof((i + mid + lag) % N32)] += r;\n\tpsfmt32[idxof(i)] = r;\n\ti = (i + 1) % N32;\n    }\n    for (j = 0; j < N32; j++) {\n\tr = func2(psfmt32[idxof(i)] + psfmt32[idxof((i + mid) % N32)]\n\t\t  + psfmt32[idxof((i + N32 - 1) % N32)]);\n\tpsfmt32[idxof((i + mid) % N32)] ^= r;\n\tr -= i;\n\tpsfmt32[idxof((i + mid + lag) % N32)] ^= r;\n\tpsfmt32[idxof(i)] = r;\n\ti = (i + 1) % N32;\n    }\n\n    ctx->idx = N32;\n    period_certification(ctx);\n    ctx->initialized = 1;\n\n    return ctx;\n}\n\nvoid fini_gen_rand(sfmt_t *ctx) {\n    assert(ctx != NULL);\n\n    ctx->initialized = 0;\n    free(ctx);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/src/btalloc.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nvoid *\nbtalloc(size_t size, unsigned bits) {\n\treturn btalloc_0(size, bits);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/src/btalloc_0.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nbtalloc_n_gen(0)\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/src/btalloc_1.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nbtalloc_n_gen(1)\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/src/math.c",
    "content": "#define MATH_C_\n#include \"test/jemalloc_test.h\"\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/src/mq.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n/*\n * Sleep for approximately ns nanoseconds.  No lower *nor* upper bound on sleep\n * time is guaranteed.\n */\nvoid\nmq_nanosleep(unsigned ns) {\n\tassert(ns <= 1000*1000*1000);\n\n#ifdef _WIN32\n\tSleep(ns / 1000);\n#else\n\t{\n\t\tstruct timespec timeout;\n\n\t\tif (ns < 1000*1000*1000) {\n\t\t\ttimeout.tv_sec = 0;\n\t\t\ttimeout.tv_nsec = ns;\n\t\t} else {\n\t\t\ttimeout.tv_sec = 1;\n\t\t\ttimeout.tv_nsec = 0;\n\t\t}\n\t\tnanosleep(&timeout, NULL);\n\t}\n#endif\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/src/mtx.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#ifndef _CRT_SPINCOUNT\n#define _CRT_SPINCOUNT 4000\n#endif\n\nbool\nmtx_init(mtx_t *mtx) {\n#ifdef _WIN32\n\tif (!InitializeCriticalSectionAndSpinCount(&mtx->lock,\n\t    _CRT_SPINCOUNT)) {\n\t\treturn true;\n\t}\n#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))\n\tmtx->lock = OS_UNFAIR_LOCK_INIT;\n#elif (defined(JEMALLOC_OSSPIN))\n\tmtx->lock = 0;\n#else\n\tpthread_mutexattr_t attr;\n\n\tif (pthread_mutexattr_init(&attr) != 0) {\n\t\treturn true;\n\t}\n\tpthread_mutexattr_settype(&attr, PTHREAD_MUTEX_DEFAULT);\n\tif (pthread_mutex_init(&mtx->lock, &attr) != 0) {\n\t\tpthread_mutexattr_destroy(&attr);\n\t\treturn true;\n\t}\n\tpthread_mutexattr_destroy(&attr);\n#endif\n\treturn false;\n}\n\nvoid\nmtx_fini(mtx_t *mtx) {\n#ifdef _WIN32\n#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))\n#elif (defined(JEMALLOC_OSSPIN))\n#else\n\tpthread_mutex_destroy(&mtx->lock);\n#endif\n}\n\nvoid\nmtx_lock(mtx_t *mtx) {\n#ifdef _WIN32\n\tEnterCriticalSection(&mtx->lock);\n#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))\n\tos_unfair_lock_lock(&mtx->lock);\n#elif (defined(JEMALLOC_OSSPIN))\n\tOSSpinLockLock(&mtx->lock);\n#else\n\tpthread_mutex_lock(&mtx->lock);\n#endif\n}\n\nvoid\nmtx_unlock(mtx_t *mtx) {\n#ifdef _WIN32\n\tLeaveCriticalSection(&mtx->lock);\n#elif (defined(JEMALLOC_OS_UNFAIR_LOCK))\n\tos_unfair_lock_unlock(&mtx->lock);\n#elif (defined(JEMALLOC_OSSPIN))\n\tOSSpinLockUnlock(&mtx->lock);\n#else\n\tpthread_mutex_unlock(&mtx->lock);\n#endif\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/src/test.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n/* Test status state. */\n\nstatic unsigned\t\ttest_count = 0;\nstatic test_status_t\ttest_counts[test_status_count] = {0, 0, 0};\nstatic test_status_t\ttest_status = test_status_pass;\nstatic const char *\ttest_name = \"\";\n\n/* Reentrancy testing helpers. */\n\n#define NUM_REENTRANT_ALLOCS 20\ntypedef enum {\n\tnon_reentrant = 0,\n\tlibc_reentrant = 1,\n\tarena_new_reentrant = 2\n} reentrancy_t;\nstatic reentrancy_t reentrancy;\n\nstatic bool libc_hook_ran = false;\nstatic bool arena_new_hook_ran = false;\n\nstatic const char *\nreentrancy_t_str(reentrancy_t r) {\n\tswitch (r) {\n\tcase non_reentrant:\n\t\treturn \"non-reentrant\";\n\tcase libc_reentrant:\n\t\treturn \"libc-reentrant\";\n\tcase arena_new_reentrant:\n\t\treturn \"arena_new-reentrant\";\n\tdefault:\n\t\tunreachable();\n\t}\n}\n\nstatic void\ndo_hook(bool *hook_ran, void (**hook)()) {\n\t*hook_ran = true;\n\t*hook = NULL;\n\n\tsize_t alloc_size = 1;\n\tfor (int i = 0; i < NUM_REENTRANT_ALLOCS; i++) {\n\t\tfree(malloc(alloc_size));\n\t\talloc_size *= 2;\n\t}\n}\n\nstatic void\nlibc_reentrancy_hook() {\n\tdo_hook(&libc_hook_ran, &hooks_libc_hook);\n}\n\nstatic void\narena_new_reentrancy_hook() {\n\tdo_hook(&arena_new_hook_ran, &hooks_arena_new_hook);\n}\n\n/* Actual test infrastructure. */\nbool\ntest_is_reentrant() {\n\treturn reentrancy != non_reentrant;\n}\n\nJEMALLOC_FORMAT_PRINTF(1, 2)\nvoid\ntest_skip(const char *format, ...) {\n\tva_list ap;\n\n\tva_start(ap, format);\n\tmalloc_vcprintf(NULL, NULL, format, ap);\n\tva_end(ap);\n\tmalloc_printf(\"\\n\");\n\ttest_status = test_status_skip;\n}\n\nJEMALLOC_FORMAT_PRINTF(1, 2)\nvoid\ntest_fail(const char *format, ...) {\n\tva_list ap;\n\n\tva_start(ap, format);\n\tmalloc_vcprintf(NULL, NULL, format, ap);\n\tva_end(ap);\n\tmalloc_printf(\"\\n\");\n\ttest_status = test_status_fail;\n}\n\nstatic const char *\ntest_status_string(test_status_t test_status) {\n\tswitch (test_status) {\n\tcase test_status_pass: return \"pass\";\n\tcase test_status_skip: return \"skip\";\n\tcase test_status_fail: return \"fail\";\n\tdefault: not_reached();\n\t}\n}\n\nvoid\np_test_init(const char *name) {\n\ttest_count++;\n\ttest_status = test_status_pass;\n\ttest_name = name;\n}\n\nvoid\np_test_fini(void) {\n\ttest_counts[test_status]++;\n\tmalloc_printf(\"%s (%s): %s\\n\", test_name, reentrancy_t_str(reentrancy),\n\t    test_status_string(test_status));\n}\n\nstatic test_status_t\np_test_impl(bool do_malloc_init, bool do_reentrant, test_t *t, va_list ap) {\n\ttest_status_t ret;\n\n\tif (do_malloc_init) {\n\t\t/*\n\t\t * Make sure initialization occurs prior to running tests.\n\t\t * Tests are special because they may use internal facilities\n\t\t * prior to triggering initialization as a side effect of\n\t\t * calling into the public API.\n\t\t */\n\t\tif (nallocx(1, 0) == 0) {\n\t\t\tmalloc_printf(\"Initialization error\");\n\t\t\treturn test_status_fail;\n\t\t}\n\t}\n\n\tret = test_status_pass;\n\tfor (; t != NULL; t = va_arg(ap, test_t *)) {\n\t\t/* Non-reentrant run. */\n\t\treentrancy = non_reentrant;\n\t\thooks_arena_new_hook = hooks_libc_hook = NULL;\n\t\tt();\n\t\tif (test_status > ret) {\n\t\t\tret = test_status;\n\t\t}\n\t\t/* Reentrant run. */\n\t\tif (do_reentrant) {\n\t\t\treentrancy = libc_reentrant;\n\t\t\thooks_arena_new_hook = NULL;\n\t\t\thooks_libc_hook = &libc_reentrancy_hook;\n\t\t\tt();\n\t\t\tif (test_status > ret) {\n\t\t\t\tret = test_status;\n\t\t\t}\n\n\t\t\treentrancy = arena_new_reentrant;\n\t\t\thooks_libc_hook = NULL;\n\t\t\thooks_arena_new_hook = &arena_new_reentrancy_hook;\n\t\t\tt();\n\t\t\tif (test_status > ret) {\n\t\t\t\tret = test_status;\n\t\t\t}\n\t\t}\n\t}\n\n\tmalloc_printf(\"--- %s: %u/%u, %s: %u/%u, %s: %u/%u ---\\n\",\n\t    test_status_string(test_status_pass),\n\t    test_counts[test_status_pass], test_count,\n\t    test_status_string(test_status_skip),\n\t    test_counts[test_status_skip], test_count,\n\t    test_status_string(test_status_fail),\n\t    test_counts[test_status_fail], test_count);\n\n\treturn ret;\n}\n\ntest_status_t\np_test(test_t *t, ...) {\n\ttest_status_t ret;\n\tva_list ap;\n\n\tret = test_status_pass;\n\tva_start(ap, t);\n\tret = p_test_impl(true, true, t, ap);\n\tva_end(ap);\n\n\treturn ret;\n}\n\ntest_status_t\np_test_no_reentrancy(test_t *t, ...) {\n\ttest_status_t ret;\n\tva_list ap;\n\n\tret = test_status_pass;\n\tva_start(ap, t);\n\tret = p_test_impl(true, false, t, ap);\n\tva_end(ap);\n\n\treturn ret;\n}\n\ntest_status_t\np_test_no_malloc_init(test_t *t, ...) {\n\ttest_status_t ret;\n\tva_list ap;\n\n\tret = test_status_pass;\n\tva_start(ap, t);\n\t/*\n\t * We also omit reentrancy from bootstrapping tests, since we don't\n\t * (yet) care about general reentrancy during bootstrapping.\n\t */\n\tret = p_test_impl(false, false, t, ap);\n\tva_end(ap);\n\n\treturn ret;\n}\n\nvoid\np_test_fail(const char *prefix, const char *message) {\n\tmalloc_cprintf(NULL, NULL, \"%s%s\\n\", prefix, message);\n\ttest_status = test_status_fail;\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/src/thd.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#ifdef _WIN32\nvoid\nthd_create(thd_t *thd, void *(*proc)(void *), void *arg) {\n\tLPTHREAD_START_ROUTINE routine = (LPTHREAD_START_ROUTINE)proc;\n\t*thd = CreateThread(NULL, 0, routine, arg, 0, NULL);\n\tif (*thd == NULL) {\n\t\ttest_fail(\"Error in CreateThread()\\n\");\n\t}\n}\n\nvoid\nthd_join(thd_t thd, void **ret) {\n\tif (WaitForSingleObject(thd, INFINITE) == WAIT_OBJECT_0 && ret) {\n\t\tDWORD exit_code;\n\t\tGetExitCodeThread(thd, (LPDWORD) &exit_code);\n\t\t*ret = (void *)(uintptr_t)exit_code;\n\t}\n}\n\n#else\nvoid\nthd_create(thd_t *thd, void *(*proc)(void *), void *arg) {\n\tif (pthread_create(thd, NULL, proc, arg) != 0) {\n\t\ttest_fail(\"Error in pthread_create()\\n\");\n\t}\n}\n\nvoid\nthd_join(thd_t thd, void **ret) {\n\tpthread_join(thd, ret);\n}\n#endif\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/src/timer.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nvoid\ntimer_start(timedelta_t *timer) {\n\tnstime_init(&timer->t0, 0);\n\tnstime_update(&timer->t0);\n}\n\nvoid\ntimer_stop(timedelta_t *timer) {\n\tnstime_copy(&timer->t1, &timer->t0);\n\tnstime_update(&timer->t1);\n}\n\nuint64_t\ntimer_usec(const timedelta_t *timer) {\n\tnstime_t delta;\n\n\tnstime_copy(&delta, &timer->t1);\n\tnstime_subtract(&delta, &timer->t0);\n\treturn nstime_ns(&delta) / 1000;\n}\n\nvoid\ntimer_ratio(timedelta_t *a, timedelta_t *b, char *buf, size_t buflen) {\n\tuint64_t t0 = timer_usec(a);\n\tuint64_t t1 = timer_usec(b);\n\tuint64_t mult;\n\tsize_t i = 0;\n\tsize_t j, n;\n\n\t/* Whole. */\n\tn = malloc_snprintf(&buf[i], buflen-i, \"%\"FMTu64, t0 / t1);\n\ti += n;\n\tif (i >= buflen) {\n\t\treturn;\n\t}\n\tmult = 1;\n\tfor (j = 0; j < n; j++) {\n\t\tmult *= 10;\n\t}\n\n\t/* Decimal. */\n\tn = malloc_snprintf(&buf[i], buflen-i, \".\");\n\ti += n;\n\n\t/* Fraction. */\n\twhile (i < buflen-1) {\n\t\tuint64_t round = (i+1 == buflen-1 && ((t0 * mult * 10 / t1) % 10\n\t\t    >= 5)) ? 1 : 0;\n\t\tn = malloc_snprintf(&buf[i], buflen-i,\n\t\t    \"%\"FMTu64, (t0 * mult / t1) % 10 + round);\n\t\ti += n;\n\t\tmult *= 10;\n\t}\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/stress/microbench.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic inline void\ntime_func(timedelta_t *timer, uint64_t nwarmup, uint64_t niter,\n    void (*func)(void)) {\n\tuint64_t i;\n\n\tfor (i = 0; i < nwarmup; i++) {\n\t\tfunc();\n\t}\n\ttimer_start(timer);\n\tfor (i = 0; i < niter; i++) {\n\t\tfunc();\n\t}\n\ttimer_stop(timer);\n}\n\nvoid\ncompare_funcs(uint64_t nwarmup, uint64_t niter, const char *name_a,\n    void (*func_a), const char *name_b, void (*func_b)) {\n\ttimedelta_t timer_a, timer_b;\n\tchar ratio_buf[6];\n\tvoid *p;\n\n\tp = mallocx(1, 0);\n\tif (p == NULL) {\n\t\ttest_fail(\"Unexpected mallocx() failure\");\n\t\treturn;\n\t}\n\n\ttime_func(&timer_a, nwarmup, niter, func_a);\n\ttime_func(&timer_b, nwarmup, niter, func_b);\n\n\ttimer_ratio(&timer_a, &timer_b, ratio_buf, sizeof(ratio_buf));\n\tmalloc_printf(\"%\"FMTu64\" iterations, %s=%\"FMTu64\"us, \"\n\t    \"%s=%\"FMTu64\"us, ratio=1:%s\\n\",\n\t    niter, name_a, timer_usec(&timer_a), name_b, timer_usec(&timer_b),\n\t    ratio_buf);\n\n\tdallocx(p, 0);\n}\n\nstatic void\nmalloc_free(void) {\n\t/* The compiler can optimize away free(malloc(1))! */\n\tvoid *p = malloc(1);\n\tif (p == NULL) {\n\t\ttest_fail(\"Unexpected malloc() failure\");\n\t\treturn;\n\t}\n\tfree(p);\n}\n\nstatic void\nmallocx_free(void) {\n\tvoid *p = mallocx(1, 0);\n\tif (p == NULL) {\n\t\ttest_fail(\"Unexpected mallocx() failure\");\n\t\treturn;\n\t}\n\tfree(p);\n}\n\nTEST_BEGIN(test_malloc_vs_mallocx) {\n\tcompare_funcs(10*1000*1000, 100*1000*1000, \"malloc\",\n\t    malloc_free, \"mallocx\", mallocx_free);\n}\nTEST_END\n\nstatic void\nmalloc_dallocx(void) {\n\tvoid *p = malloc(1);\n\tif (p == NULL) {\n\t\ttest_fail(\"Unexpected malloc() failure\");\n\t\treturn;\n\t}\n\tdallocx(p, 0);\n}\n\nstatic void\nmalloc_sdallocx(void) {\n\tvoid *p = malloc(1);\n\tif (p == NULL) {\n\t\ttest_fail(\"Unexpected malloc() failure\");\n\t\treturn;\n\t}\n\tsdallocx(p, 1, 0);\n}\n\nTEST_BEGIN(test_free_vs_dallocx) {\n\tcompare_funcs(10*1000*1000, 100*1000*1000, \"free\", malloc_free,\n\t    \"dallocx\", malloc_dallocx);\n}\nTEST_END\n\nTEST_BEGIN(test_dallocx_vs_sdallocx) {\n\tcompare_funcs(10*1000*1000, 100*1000*1000, \"dallocx\", malloc_dallocx,\n\t    \"sdallocx\", malloc_sdallocx);\n}\nTEST_END\n\nstatic void\nmalloc_mus_free(void) {\n\tvoid *p;\n\n\tp = malloc(1);\n\tif (p == NULL) {\n\t\ttest_fail(\"Unexpected malloc() failure\");\n\t\treturn;\n\t}\n\tmalloc_usable_size(p);\n\tfree(p);\n}\n\nstatic void\nmalloc_sallocx_free(void) {\n\tvoid *p;\n\n\tp = malloc(1);\n\tif (p == NULL) {\n\t\ttest_fail(\"Unexpected malloc() failure\");\n\t\treturn;\n\t}\n\tif (sallocx(p, 0) < 1) {\n\t\ttest_fail(\"Unexpected sallocx() failure\");\n\t}\n\tfree(p);\n}\n\nTEST_BEGIN(test_mus_vs_sallocx) {\n\tcompare_funcs(10*1000*1000, 100*1000*1000, \"malloc_usable_size\",\n\t    malloc_mus_free, \"sallocx\", malloc_sallocx_free);\n}\nTEST_END\n\nstatic void\nmalloc_nallocx_free(void) {\n\tvoid *p;\n\n\tp = malloc(1);\n\tif (p == NULL) {\n\t\ttest_fail(\"Unexpected malloc() failure\");\n\t\treturn;\n\t}\n\tif (nallocx(1, 0) < 1) {\n\t\ttest_fail(\"Unexpected nallocx() failure\");\n\t}\n\tfree(p);\n}\n\nTEST_BEGIN(test_sallocx_vs_nallocx) {\n\tcompare_funcs(10*1000*1000, 100*1000*1000, \"sallocx\",\n\t    malloc_sallocx_free, \"nallocx\", malloc_nallocx_free);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test_no_reentrancy(\n\t    test_malloc_vs_mallocx,\n\t    test_free_vs_dallocx,\n\t    test_dallocx_vs_sdallocx,\n\t    test_mus_vs_sallocx,\n\t    test_sallocx_vs_nallocx);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/test.sh.in",
    "content": "#!/bin/sh\n\ncase @abi@ in\n  macho)\n    export DYLD_FALLBACK_LIBRARY_PATH=\"@objroot@lib\"\n    ;;\n  pecoff)\n    export PATH=\"${PATH}:@objroot@lib\"\n    ;;\n  *)\n    ;;\nesac\n\n# Make a copy of the @JEMALLOC_CPREFIX@MALLOC_CONF passed in to this script, so\n# it can be repeatedly concatenated with per test settings.\nexport MALLOC_CONF_ALL=${@JEMALLOC_CPREFIX@MALLOC_CONF}\n# Concatenate the individual test's MALLOC_CONF and MALLOC_CONF_ALL.\nexport_malloc_conf() {\n  if [ \"x${MALLOC_CONF}\" != \"x\" -a \"x${MALLOC_CONF_ALL}\" != \"x\" ] ; then\n    export @JEMALLOC_CPREFIX@MALLOC_CONF=\"${MALLOC_CONF},${MALLOC_CONF_ALL}\"\n  else\n    export @JEMALLOC_CPREFIX@MALLOC_CONF=\"${MALLOC_CONF}${MALLOC_CONF_ALL}\"\n  fi\n}\n\n# Corresponds to test_status_t.\npass_code=0\nskip_code=1\nfail_code=2\n\npass_count=0\nskip_count=0\nfail_count=0\nfor t in $@; do\n  if [ $pass_count -ne 0 -o $skip_count -ne 0 -o $fail_count != 0 ] ; then\n    echo\n  fi\n  echo \"=== ${t} ===\"\n  if [ -e \"@srcroot@${t}.sh\" ] ; then\n    # Source the shell script corresponding to the test in a subshell and\n    # execute the test.  This allows the shell script to set MALLOC_CONF, which\n    # is then used to set @JEMALLOC_CPREFIX@MALLOC_CONF (thus allowing the\n    # per test shell script to ignore the @JEMALLOC_CPREFIX@ detail).\n    enable_fill=@enable_fill@ \\\n    enable_prof=@enable_prof@ \\\n    . @srcroot@${t}.sh && \\\n    export_malloc_conf && \\\n    $JEMALLOC_TEST_PREFIX ${t}@exe@ @abs_srcroot@ @abs_objroot@\n  else\n    export MALLOC_CONF= && \\\n    export_malloc_conf && \\\n    $JEMALLOC_TEST_PREFIX ${t}@exe@ @abs_srcroot@ @abs_objroot@\n  fi\n  result_code=$?\n  case ${result_code} in\n    ${pass_code})\n      pass_count=$((pass_count+1))\n      ;;\n    ${skip_code})\n      skip_count=$((skip_count+1))\n      ;;\n    ${fail_code})\n      fail_count=$((fail_count+1))\n      ;;\n    *)\n      echo \"Test harness error: ${t} w/ MALLOC_CONF=\\\"${MALLOC_CONF}\\\"\" 1>&2\n      echo \"Use prefix to debug, e.g. JEMALLOC_TEST_PREFIX=\\\"gdb --args\\\" sh test/test.sh ${t}\" 1>&2\n      exit 1\n  esac\ndone\n\ntotal_count=`expr ${pass_count} + ${skip_count} + ${fail_count}`\necho\necho \"Test suite summary: pass: ${pass_count}/${total_count}, skip: ${skip_count}/${total_count}, fail: ${fail_count}/${total_count}\"\n\nif [ ${fail_count} -eq 0 ] ; then\n  exit 0\nelse\n  exit 1\nfi\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/SFMT.c",
    "content": "/*\n * This file derives from SFMT 1.3.3\n * (http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/index.html), which was\n * released under the terms of the following license:\n *\n *   Copyright (c) 2006,2007 Mutsuo Saito, Makoto Matsumoto and Hiroshima\n *   University. All rights reserved.\n *\n *   Redistribution and use in source and binary forms, with or without\n *   modification, are permitted provided that the following conditions are\n *   met:\n *\n *       * Redistributions of source code must retain the above copyright\n *         notice, this list of conditions and the following disclaimer.\n *       * Redistributions in binary form must reproduce the above\n *         copyright notice, this list of conditions and the following\n *         disclaimer in the documentation and/or other materials provided\n *         with the distribution.\n *       * Neither the name of the Hiroshima University nor the names of\n *         its contributors may be used to endorse or promote products\n *         derived from this software without specific prior written\n *         permission.\n *\n *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n *   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#include \"test/jemalloc_test.h\"\n\n#define BLOCK_SIZE 10000\n#define BLOCK_SIZE64 (BLOCK_SIZE / 2)\n#define COUNT_1 1000\n#define COUNT_2 700\n\nstatic const uint32_t init_gen_rand_32_expected[] = {\n\t3440181298U, 1564997079U, 1510669302U, 2930277156U, 1452439940U,\n\t3796268453U,  423124208U, 2143818589U, 3827219408U, 2987036003U,\n\t2674978610U, 1536842514U, 2027035537U, 2534897563U, 1686527725U,\n\t 545368292U, 1489013321U, 1370534252U, 4231012796U, 3994803019U,\n\t1764869045U,  824597505U,  862581900U, 2469764249U,  812862514U,\n\t 359318673U,  116957936U, 3367389672U, 2327178354U, 1898245200U,\n\t3206507879U, 2378925033U, 1040214787U, 2524778605U, 3088428700U,\n\t1417665896U,  964324147U, 2282797708U, 2456269299U,  313400376U,\n\t2245093271U, 1015729427U, 2694465011U, 3246975184U, 1992793635U,\n\t 463679346U, 3721104591U, 3475064196U,  856141236U, 1499559719U,\n\t3522818941U, 3721533109U, 1954826617U, 1282044024U, 1543279136U,\n\t1301863085U, 2669145051U, 4221477354U, 3896016841U, 3392740262U,\n\t 462466863U, 1037679449U, 1228140306U,  922298197U, 1205109853U,\n\t1872938061U, 3102547608U, 2742766808U, 1888626088U, 4028039414U,\n\t 157593879U, 1136901695U, 4038377686U, 3572517236U, 4231706728U,\n\t2997311961U, 1189931652U, 3981543765U, 2826166703U,   87159245U,\n\t1721379072U, 3897926942U, 1790395498U, 2569178939U, 1047368729U,\n\t2340259131U, 3144212906U, 2301169789U, 2442885464U, 3034046771U,\n\t3667880593U, 3935928400U, 2372805237U, 1666397115U, 2460584504U,\n\t 513866770U, 3810869743U, 2147400037U, 2792078025U, 2941761810U,\n\t3212265810U,  984692259U,  346590253U, 1804179199U, 3298543443U,\n\t 750108141U, 2880257022U,  243310542U, 1869036465U, 1588062513U,\n\t2983949551U, 1931450364U, 4034505847U, 2735030199U, 1628461061U,\n\t2539522841U,  127965585U, 3992448871U,  913388237U,  559130076U,\n\t1202933193U, 4087643167U, 2590021067U, 2256240196U, 1746697293U,\n\t1013913783U, 1155864921U, 2715773730U,  915061862U, 1948766573U,\n\t2322882854U, 3761119102U, 1343405684U, 3078711943U, 3067431651U,\n\t3245156316U, 3588354584U, 3484623306U, 3899621563U, 4156689741U,\n\t3237090058U, 3880063844U,  862416318U, 4039923869U, 2303788317U,\n\t3073590536U,  701653667U, 2131530884U, 3169309950U, 2028486980U,\n\t 747196777U, 3620218225U,  432016035U, 1449580595U, 2772266392U,\n\t 444224948U, 1662832057U, 3184055582U, 3028331792U, 1861686254U,\n\t1104864179U,  342430307U, 1350510923U, 3024656237U, 1028417492U,\n\t2870772950U,  290847558U, 3675663500U,  508431529U, 4264340390U,\n\t2263569913U, 1669302976U,  519511383U, 2706411211U, 3764615828U,\n\t3883162495U, 4051445305U, 2412729798U, 3299405164U, 3991911166U,\n\t2348767304U, 2664054906U, 3763609282U,  593943581U, 3757090046U,\n\t2075338894U, 2020550814U, 4287452920U, 4290140003U, 1422957317U,\n\t2512716667U, 2003485045U, 2307520103U, 2288472169U, 3940751663U,\n\t4204638664U, 2892583423U, 1710068300U, 3904755993U, 2363243951U,\n\t3038334120U,  547099465U,  771105860U, 3199983734U, 4282046461U,\n\t2298388363U,  934810218U, 2837827901U, 3952500708U, 2095130248U,\n\t3083335297U,   26885281U, 3932155283U, 1531751116U, 1425227133U,\n\t 495654159U, 3279634176U, 3855562207U, 3957195338U, 4159985527U,\n\t 893375062U, 1875515536U, 1327247422U, 3754140693U, 1028923197U,\n\t1729880440U,  805571298U,  448971099U, 2726757106U, 2749436461U,\n\t2485987104U,  175337042U, 3235477922U, 3882114302U, 2020970972U,\n\t 943926109U, 2762587195U, 1904195558U, 3452650564U,  108432281U,\n\t3893463573U, 3977583081U, 2636504348U, 1110673525U, 3548479841U,\n\t4258854744U,  980047703U, 4057175418U, 3890008292U,  145653646U,\n\t3141868989U, 3293216228U, 1194331837U, 1254570642U, 3049934521U,\n\t2868313360U, 2886032750U, 1110873820U,  279553524U, 3007258565U,\n\t1104807822U, 3186961098U,  315764646U, 2163680838U, 3574508994U,\n\t3099755655U,  191957684U, 3642656737U, 3317946149U, 3522087636U,\n\t 444526410U,  779157624U, 1088229627U, 1092460223U, 1856013765U,\n\t3659877367U,  368270451U,  503570716U, 3000984671U, 2742789647U,\n\t 928097709U, 2914109539U,  308843566U, 2816161253U, 3667192079U,\n\t2762679057U, 3395240989U, 2928925038U, 1491465914U, 3458702834U,\n\t3787782576U, 2894104823U, 1296880455U, 1253636503U,  989959407U,\n\t2291560361U, 2776790436U, 1913178042U, 1584677829U,  689637520U,\n\t1898406878U,  688391508U, 3385234998U,  845493284U, 1943591856U,\n\t2720472050U,  222695101U, 1653320868U, 2904632120U, 4084936008U,\n\t1080720688U, 3938032556U,  387896427U, 2650839632U,   99042991U,\n\t1720913794U, 1047186003U, 1877048040U, 2090457659U,  517087501U,\n\t4172014665U, 2129713163U, 2413533132U, 2760285054U, 4129272496U,\n\t1317737175U, 2309566414U, 2228873332U, 3889671280U, 1110864630U,\n\t3576797776U, 2074552772U,  832002644U, 3097122623U, 2464859298U,\n\t2679603822U, 1667489885U, 3237652716U, 1478413938U, 1719340335U,\n\t2306631119U,  639727358U, 3369698270U,  226902796U, 2099920751U,\n\t1892289957U, 2201594097U, 3508197013U, 3495811856U, 3900381493U,\n\t 841660320U, 3974501451U, 3360949056U, 1676829340U,  728899254U,\n\t2047809627U, 2390948962U,  670165943U, 3412951831U, 4189320049U,\n\t1911595255U, 2055363086U,  507170575U,  418219594U, 4141495280U,\n\t2692088692U, 4203630654U, 3540093932U,  791986533U, 2237921051U,\n\t2526864324U, 2956616642U, 1394958700U, 1983768223U, 1893373266U,\n\t 591653646U,  228432437U, 1611046598U, 3007736357U, 1040040725U,\n\t2726180733U, 2789804360U, 4263568405U,  829098158U, 3847722805U,\n\t1123578029U, 1804276347U,  997971319U, 4203797076U, 4185199713U,\n\t2811733626U, 2343642194U, 2985262313U, 1417930827U, 3759587724U,\n\t1967077982U, 1585223204U, 1097475516U, 1903944948U,  740382444U,\n\t1114142065U, 1541796065U, 1718384172U, 1544076191U, 1134682254U,\n\t3519754455U, 2866243923U,  341865437U,  645498576U, 2690735853U,\n\t1046963033U, 2493178460U, 1187604696U, 1619577821U,  488503634U,\n\t3255768161U, 2306666149U, 1630514044U, 2377698367U, 2751503746U,\n\t3794467088U, 1796415981U, 3657173746U,  409136296U, 1387122342U,\n\t1297726519U,  219544855U, 4270285558U,  437578827U, 1444698679U,\n\t2258519491U,  963109892U, 3982244073U, 3351535275U,  385328496U,\n\t1804784013U,  698059346U, 3920535147U,  708331212U,  784338163U,\n\t 785678147U, 1238376158U, 1557298846U, 2037809321U,  271576218U,\n\t4145155269U, 1913481602U, 2763691931U,  588981080U, 1201098051U,\n\t3717640232U, 1509206239U,  662536967U, 3180523616U, 1133105435U,\n\t2963500837U, 2253971215U, 3153642623U, 1066925709U, 2582781958U,\n\t3034720222U, 1090798544U, 2942170004U, 4036187520U,  686972531U,\n\t2610990302U, 2641437026U, 1837562420U,  722096247U, 1315333033U,\n\t2102231203U, 3402389208U, 3403698140U, 1312402831U, 2898426558U,\n\t 814384596U,  385649582U, 1916643285U, 1924625106U, 2512905582U,\n\t2501170304U, 4275223366U, 2841225246U, 1467663688U, 3563567847U,\n\t2969208552U,  884750901U,  102992576U,  227844301U, 3681442994U,\n\t3502881894U, 4034693299U, 1166727018U, 1697460687U, 1737778332U,\n\t1787161139U, 1053003655U, 1215024478U, 2791616766U, 2525841204U,\n\t1629323443U,    3233815U, 2003823032U, 3083834263U, 2379264872U,\n\t3752392312U, 1287475550U, 3770904171U, 3004244617U, 1502117784U,\n\t 918698423U, 2419857538U, 3864502062U, 1751322107U, 2188775056U,\n\t4018728324U,  983712955U,  440071928U, 3710838677U, 2001027698U,\n\t3994702151U,   22493119U, 3584400918U, 3446253670U, 4254789085U,\n\t1405447860U, 1240245579U, 1800644159U, 1661363424U, 3278326132U,\n\t3403623451U,   67092802U, 2609352193U, 3914150340U, 1814842761U,\n\t3610830847U,  591531412U, 3880232807U, 1673505890U, 2585326991U,\n\t1678544474U, 3148435887U, 3457217359U, 1193226330U, 2816576908U,\n\t 154025329U,  121678860U, 1164915738U,  973873761U,  269116100U,\n\t  52087970U,  744015362U,  498556057U,   94298882U, 1563271621U,\n\t2383059628U, 4197367290U, 3958472990U, 2592083636U, 2906408439U,\n\t1097742433U, 3924840517U,  264557272U, 2292287003U, 3203307984U,\n\t4047038857U, 3820609705U, 2333416067U, 1839206046U, 3600944252U,\n\t3412254904U,  583538222U, 2390557166U, 4140459427U, 2810357445U,\n\t 226777499U, 2496151295U, 2207301712U, 3283683112U,  611630281U,\n\t1933218215U, 3315610954U, 3889441987U, 3719454256U, 3957190521U,\n\t1313998161U, 2365383016U, 3146941060U, 1801206260U,  796124080U,\n\t2076248581U, 1747472464U, 3254365145U,  595543130U, 3573909503U,\n\t3758250204U, 2020768540U, 2439254210U,   93368951U, 3155792250U,\n\t2600232980U, 3709198295U, 3894900440U, 2971850836U, 1578909644U,\n\t1443493395U, 2581621665U, 3086506297U, 2443465861U,  558107211U,\n\t1519367835U,  249149686U,  908102264U, 2588765675U, 1232743965U,\n\t1001330373U, 3561331654U, 2259301289U, 1564977624U, 3835077093U,\n\t 727244906U, 4255738067U, 1214133513U, 2570786021U, 3899704621U,\n\t1633861986U, 1636979509U, 1438500431U,   58463278U, 2823485629U,\n\t2297430187U, 2926781924U, 3371352948U, 1864009023U, 2722267973U,\n\t1444292075U,  437703973U, 1060414512U,  189705863U,  910018135U,\n\t4077357964U,  884213423U, 2644986052U, 3973488374U, 1187906116U,\n\t2331207875U,  780463700U, 3713351662U, 3854611290U,  412805574U,\n\t2978462572U, 2176222820U,  829424696U, 2790788332U, 2750819108U,\n\t1594611657U, 3899878394U, 3032870364U, 1702887682U, 1948167778U,\n\t  14130042U,  192292500U,  947227076U,   90719497U, 3854230320U,\n\t 784028434U, 2142399787U, 1563449646U, 2844400217U,  819143172U,\n\t2883302356U, 2328055304U, 1328532246U, 2603885363U, 3375188924U,\n\t 933941291U, 3627039714U, 2129697284U, 2167253953U, 2506905438U,\n\t1412424497U, 2981395985U, 1418359660U, 2925902456U,   52752784U,\n\t3713667988U, 3924669405U,  648975707U, 1145520213U, 4018650664U,\n\t3805915440U, 2380542088U, 2013260958U, 3262572197U, 2465078101U,\n\t1114540067U, 3728768081U, 2396958768U,  590672271U,  904818725U,\n\t4263660715U,  700754408U, 1042601829U, 4094111823U, 4274838909U,\n\t2512692617U, 2774300207U, 2057306915U, 3470942453U,   99333088U,\n\t1142661026U, 2889931380U,   14316674U, 2201179167U,  415289459U,\n\t 448265759U, 3515142743U, 3254903683U,  246633281U, 1184307224U,\n\t2418347830U, 2092967314U, 2682072314U, 2558750234U, 2000352263U,\n\t1544150531U,  399010405U, 1513946097U,  499682937U,  461167460U,\n\t3045570638U, 1633669705U,  851492362U, 4052801922U, 2055266765U,\n\t 635556996U,  368266356U, 2385737383U, 3218202352U, 2603772408U,\n\t 349178792U,  226482567U, 3102426060U, 3575998268U, 2103001871U,\n\t3243137071U,  225500688U, 1634718593U, 4283311431U, 4292122923U,\n\t3842802787U,  811735523U,  105712518U,  663434053U, 1855889273U,\n\t2847972595U, 1196355421U, 2552150115U, 4254510614U, 3752181265U,\n\t3430721819U, 3828705396U, 3436287905U, 3441964937U, 4123670631U,\n\t 353001539U,  459496439U, 3799690868U, 1293777660U, 2761079737U,\n\t 498096339U, 3398433374U, 4080378380U, 2304691596U, 2995729055U,\n\t4134660419U, 3903444024U, 3576494993U,  203682175U, 3321164857U,\n\t2747963611U,   79749085U, 2992890370U, 1240278549U, 1772175713U,\n\t2111331972U, 2655023449U, 1683896345U, 2836027212U, 3482868021U,\n\t2489884874U,  756853961U, 2298874501U, 4013448667U, 4143996022U,\n\t2948306858U, 4132920035U, 1283299272U,  995592228U, 3450508595U,\n\t1027845759U, 1766942720U, 3861411826U, 1446861231U,   95974993U,\n\t3502263554U, 1487532194U,  601502472U, 4129619129U,  250131773U,\n\t2050079547U, 3198903947U, 3105589778U, 4066481316U, 3026383978U,\n\t2276901713U,  365637751U, 2260718426U, 1394775634U, 1791172338U,\n\t2690503163U, 2952737846U, 1568710462U,  732623190U, 2980358000U,\n\t1053631832U, 1432426951U, 3229149635U, 1854113985U, 3719733532U,\n\t3204031934U,  735775531U,  107468620U, 3734611984U,  631009402U,\n\t3083622457U, 4109580626U,  159373458U, 1301970201U, 4132389302U,\n\t1293255004U,  847182752U, 4170022737U,   96712900U, 2641406755U,\n\t1381727755U,  405608287U, 4287919625U, 1703554290U, 3589580244U,\n\t2911403488U,    2166565U, 2647306451U, 2330535117U, 1200815358U,\n\t1165916754U,  245060911U, 4040679071U, 3684908771U, 2452834126U,\n\t2486872773U, 2318678365U, 2940627908U, 1837837240U, 3447897409U,\n\t4270484676U, 1495388728U, 3754288477U, 4204167884U, 1386977705U,\n\t2692224733U, 3076249689U, 4109568048U, 4170955115U, 4167531356U,\n\t4020189950U, 4261855038U, 3036907575U, 3410399885U, 3076395737U,\n\t1046178638U,  144496770U,  230725846U, 3349637149U,   17065717U,\n\t2809932048U, 2054581785U, 3608424964U, 3259628808U,  134897388U,\n\t3743067463U,  257685904U, 3795656590U, 1562468719U, 3589103904U,\n\t3120404710U,  254684547U, 2653661580U, 3663904795U, 2631942758U,\n\t1063234347U, 2609732900U, 2332080715U, 3521125233U, 1180599599U,\n\t1935868586U, 4110970440U,  296706371U, 2128666368U, 1319875791U,\n\t1570900197U, 3096025483U, 1799882517U, 1928302007U, 1163707758U,\n\t1244491489U, 3533770203U,  567496053U, 2757924305U, 2781639343U,\n\t2818420107U,  560404889U, 2619609724U, 4176035430U, 2511289753U,\n\t2521842019U, 3910553502U, 2926149387U, 3302078172U, 4237118867U,\n\t 330725126U,  367400677U,  888239854U,  545570454U, 4259590525U,\n\t 134343617U, 1102169784U, 1647463719U, 3260979784U, 1518840883U,\n\t3631537963U, 3342671457U, 1301549147U, 2083739356U,  146593792U,\n\t3217959080U,  652755743U, 2032187193U, 3898758414U, 1021358093U,\n\t4037409230U, 2176407931U, 3427391950U, 2883553603U,  985613827U,\n\t3105265092U, 3423168427U, 3387507672U,  467170288U, 2141266163U,\n\t3723870208U,  916410914U, 1293987799U, 2652584950U,  769160137U,\n\t3205292896U, 1561287359U, 1684510084U, 3136055621U, 3765171391U,\n\t 639683232U, 2639569327U, 1218546948U, 4263586685U, 3058215773U,\n\t2352279820U,  401870217U, 2625822463U, 1529125296U, 2981801895U,\n\t1191285226U, 4027725437U, 3432700217U, 4098835661U,  971182783U,\n\t2443861173U, 3881457123U, 3874386651U,  457276199U, 2638294160U,\n\t4002809368U,  421169044U, 1112642589U, 3076213779U, 3387033971U,\n\t2499610950U, 3057240914U, 1662679783U,  461224431U, 1168395933U\n};\nstatic const uint32_t init_by_array_32_expected[] = {\n\t2920711183U, 3885745737U, 3501893680U,  856470934U, 1421864068U,\n\t 277361036U, 1518638004U, 2328404353U, 3355513634U,   64329189U,\n\t1624587673U, 3508467182U, 2481792141U, 3706480799U, 1925859037U,\n\t2913275699U,  882658412U,  384641219U,  422202002U, 1873384891U,\n\t2006084383U, 3924929912U, 1636718106U, 3108838742U, 1245465724U,\n\t4195470535U,  779207191U, 1577721373U, 1390469554U, 2928648150U,\n\t 121399709U, 3170839019U, 4044347501U,  953953814U, 3821710850U,\n\t3085591323U, 3666535579U, 3577837737U, 2012008410U, 3565417471U,\n\t4044408017U,  433600965U, 1637785608U, 1798509764U,  860770589U,\n\t3081466273U, 3982393409U, 2451928325U, 3437124742U, 4093828739U,\n\t3357389386U, 2154596123U,  496568176U, 2650035164U, 2472361850U,\n\t   3438299U, 2150366101U, 1577256676U, 3802546413U, 1787774626U,\n\t4078331588U, 3706103141U,  170391138U, 3806085154U, 1680970100U,\n\t1961637521U, 3316029766U,  890610272U, 1453751581U, 1430283664U,\n\t3051057411U, 3597003186U,  542563954U, 3796490244U, 1690016688U,\n\t3448752238U,  440702173U,  347290497U, 1121336647U, 2540588620U,\n\t 280881896U, 2495136428U,  213707396U,   15104824U, 2946180358U,\n\t 659000016U,  566379385U, 2614030979U, 2855760170U,  334526548U,\n\t2315569495U, 2729518615U,  564745877U, 1263517638U, 3157185798U,\n\t1604852056U, 1011639885U, 2950579535U, 2524219188U,  312951012U,\n\t1528896652U, 1327861054U, 2846910138U, 3966855905U, 2536721582U,\n\t 855353911U, 1685434729U, 3303978929U, 1624872055U, 4020329649U,\n\t3164802143U, 1642802700U, 1957727869U, 1792352426U, 3334618929U,\n\t2631577923U, 3027156164U,  842334259U, 3353446843U, 1226432104U,\n\t1742801369U, 3552852535U, 3471698828U, 1653910186U, 3380330939U,\n\t2313782701U, 3351007196U, 2129839995U, 1800682418U, 4085884420U,\n\t1625156629U, 3669701987U,  615211810U, 3294791649U, 4131143784U,\n\t2590843588U, 3207422808U, 3275066464U,  561592872U, 3957205738U,\n\t3396578098U,   48410678U, 3505556445U, 1005764855U, 3920606528U,\n\t2936980473U, 2378918600U, 2404449845U, 1649515163U,  701203563U,\n\t3705256349U,   83714199U, 3586854132U,  922978446U, 2863406304U,\n\t3523398907U, 2606864832U, 2385399361U, 3171757816U, 4262841009U,\n\t3645837721U, 1169579486U, 3666433897U, 3174689479U, 1457866976U,\n\t3803895110U, 3346639145U, 1907224409U, 1978473712U, 1036712794U,\n\t 980754888U, 1302782359U, 1765252468U,  459245755U, 3728923860U,\n\t1512894209U, 2046491914U,  207860527U,  514188684U, 2288713615U,\n\t1597354672U, 3349636117U, 2357291114U, 3995796221U,  945364213U,\n\t1893326518U, 3770814016U, 1691552714U, 2397527410U,  967486361U,\n\t 776416472U, 4197661421U,  951150819U, 1852770983U, 4044624181U,\n\t1399439738U, 4194455275U, 2284037669U, 1550734958U, 3321078108U,\n\t1865235926U, 2912129961U, 2664980877U, 1357572033U, 2600196436U,\n\t2486728200U, 2372668724U, 1567316966U, 2374111491U, 1839843570U,\n\t  20815612U, 3727008608U, 3871996229U,  824061249U, 1932503978U,\n\t3404541726U,  758428924U, 2609331364U, 1223966026U, 1299179808U,\n\t 648499352U, 2180134401U,  880821170U, 3781130950U,  113491270U,\n\t1032413764U, 4185884695U, 2490396037U, 1201932817U, 4060951446U,\n\t4165586898U, 1629813212U, 2887821158U,  415045333U,  628926856U,\n\t2193466079U, 3391843445U, 2227540681U, 1907099846U, 2848448395U,\n\t1717828221U, 1372704537U, 1707549841U, 2294058813U, 2101214437U,\n\t2052479531U, 1695809164U, 3176587306U, 2632770465U,   81634404U,\n\t1603220563U,  644238487U,  302857763U,  897352968U, 2613146653U,\n\t1391730149U, 4245717312U, 4191828749U, 1948492526U, 2618174230U,\n\t3992984522U, 2178852787U, 3596044509U, 3445573503U, 2026614616U,\n\t 915763564U, 3415689334U, 2532153403U, 3879661562U, 2215027417U,\n\t3111154986U, 2929478371U,  668346391U, 1152241381U, 2632029711U,\n\t3004150659U, 2135025926U,  948690501U, 2799119116U, 4228829406U,\n\t1981197489U, 4209064138U,  684318751U, 3459397845U,  201790843U,\n\t4022541136U, 3043635877U,  492509624U, 3263466772U, 1509148086U,\n\t 921459029U, 3198857146U,  705479721U, 3835966910U, 3603356465U,\n\t 576159741U, 1742849431U,  594214882U, 2055294343U, 3634861861U,\n\t 449571793U, 3246390646U, 3868232151U, 1479156585U, 2900125656U,\n\t2464815318U, 3960178104U, 1784261920U,   18311476U, 3627135050U,\n\t 644609697U,  424968996U,  919890700U, 2986824110U,  816423214U,\n\t4003562844U, 1392714305U, 1757384428U, 2569030598U,  995949559U,\n\t3875659880U, 2933807823U, 2752536860U, 2993858466U, 4030558899U,\n\t2770783427U, 2775406005U, 2777781742U, 1931292655U,  472147933U,\n\t3865853827U, 2726470545U, 2668412860U, 2887008249U,  408979190U,\n\t3578063323U, 3242082049U, 1778193530U,   27981909U, 2362826515U,\n\t 389875677U, 1043878156U,  581653903U, 3830568952U,  389535942U,\n\t3713523185U, 2768373359U, 2526101582U, 1998618197U, 1160859704U,\n\t3951172488U, 1098005003U,  906275699U, 3446228002U, 2220677963U,\n\t2059306445U,  132199571U,  476838790U, 1868039399U, 3097344807U,\n\t 857300945U,  396345050U, 2835919916U, 1782168828U, 1419519470U,\n\t4288137521U,  819087232U,  596301494U,  872823172U, 1526888217U,\n\t 805161465U, 1116186205U, 2829002754U, 2352620120U,  620121516U,\n\t 354159268U, 3601949785U,  209568138U, 1352371732U, 2145977349U,\n\t4236871834U, 1539414078U, 3558126206U, 3224857093U, 4164166682U,\n\t3817553440U, 3301780278U, 2682696837U, 3734994768U, 1370950260U,\n\t1477421202U, 2521315749U, 1330148125U, 1261554731U, 2769143688U,\n\t3554756293U, 4235882678U, 3254686059U, 3530579953U, 1215452615U,\n\t3574970923U, 4057131421U,  589224178U, 1000098193U,  171190718U,\n\t2521852045U, 2351447494U, 2284441580U, 2646685513U, 3486933563U,\n\t3789864960U, 1190528160U, 1702536782U, 1534105589U, 4262946827U,\n\t2726686826U, 3584544841U, 2348270128U, 2145092281U, 2502718509U,\n\t1027832411U, 3571171153U, 1287361161U, 4011474411U, 3241215351U,\n\t2419700818U,  971242709U, 1361975763U, 1096842482U, 3271045537U,\n\t  81165449U,  612438025U, 3912966678U, 1356929810U,  733545735U,\n\t 537003843U, 1282953084U,  884458241U,  588930090U, 3930269801U,\n\t2961472450U, 1219535534U, 3632251943U,  268183903U, 1441240533U,\n\t3653903360U, 3854473319U, 2259087390U, 2548293048U, 2022641195U,\n\t2105543911U, 1764085217U, 3246183186U,  482438805U,  888317895U,\n\t2628314765U, 2466219854U,  717546004U, 2322237039U,  416725234U,\n\t1544049923U, 1797944973U, 3398652364U, 3111909456U,  485742908U,\n\t2277491072U, 1056355088U, 3181001278U,  129695079U, 2693624550U,\n\t1764438564U, 3797785470U,  195503713U, 3266519725U, 2053389444U,\n\t1961527818U, 3400226523U, 3777903038U, 2597274307U, 4235851091U,\n\t4094406648U, 2171410785U, 1781151386U, 1378577117U,  654643266U,\n\t3424024173U, 3385813322U,  679385799U,  479380913U,  681715441U,\n\t3096225905U,  276813409U, 3854398070U, 2721105350U,  831263315U,\n\t3276280337U, 2628301522U, 3984868494U, 1466099834U, 2104922114U,\n\t1412672743U,  820330404U, 3491501010U,  942735832U,  710652807U,\n\t3972652090U,  679881088U,   40577009U, 3705286397U, 2815423480U,\n\t3566262429U,  663396513U, 3777887429U, 4016670678U,  404539370U,\n\t1142712925U, 1140173408U, 2913248352U, 2872321286U,  263751841U,\n\t3175196073U, 3162557581U, 2878996619U,   75498548U, 3836833140U,\n\t3284664959U, 1157523805U,  112847376U,  207855609U, 1337979698U,\n\t1222578451U,  157107174U,  901174378U, 3883717063U, 1618632639U,\n\t1767889440U, 4264698824U, 1582999313U,  884471997U, 2508825098U,\n\t3756370771U, 2457213553U, 3565776881U, 3709583214U,  915609601U,\n\t 460833524U, 1091049576U,   85522880U,    2553251U,  132102809U,\n\t2429882442U, 2562084610U, 1386507633U, 4112471229U,   21965213U,\n\t1981516006U, 2418435617U, 3054872091U, 4251511224U, 2025783543U,\n\t1916911512U, 2454491136U, 3938440891U, 3825869115U, 1121698605U,\n\t3463052265U,  802340101U, 1912886800U, 4031997367U, 3550640406U,\n\t1596096923U,  610150600U,  431464457U, 2541325046U,  486478003U,\n\t 739704936U, 2862696430U, 3037903166U, 1129749694U, 2611481261U,\n\t1228993498U,  510075548U, 3424962587U, 2458689681U,  818934833U,\n\t4233309125U, 1608196251U, 3419476016U, 1858543939U, 2682166524U,\n\t3317854285U,  631986188U, 3008214764U,  613826412U, 3567358221U,\n\t3512343882U, 1552467474U, 3316162670U, 1275841024U, 4142173454U,\n\t 565267881U,  768644821U,  198310105U, 2396688616U, 1837659011U,\n\t 203429334U,  854539004U, 4235811518U, 3338304926U, 3730418692U,\n\t3852254981U, 3032046452U, 2329811860U, 2303590566U, 2696092212U,\n\t3894665932U,  145835667U,  249563655U, 1932210840U, 2431696407U,\n\t3312636759U,  214962629U, 2092026914U, 3020145527U, 4073039873U,\n\t2739105705U, 1308336752U,  855104522U, 2391715321U,   67448785U,\n\t 547989482U,  854411802U, 3608633740U,  431731530U,  537375589U,\n\t3888005760U,  696099141U,  397343236U, 1864511780U,   44029739U,\n\t1729526891U, 1993398655U, 2010173426U, 2591546756U,  275223291U,\n\t1503900299U, 4217765081U, 2185635252U, 1122436015U, 3550155364U,\n\t 681707194U, 3260479338U,  933579397U, 2983029282U, 2505504587U,\n\t2667410393U, 2962684490U, 4139721708U, 2658172284U, 2452602383U,\n\t2607631612U, 1344296217U, 3075398709U, 2949785295U, 1049956168U,\n\t3917185129U, 2155660174U, 3280524475U, 1503827867U,  674380765U,\n\t1918468193U, 3843983676U,  634358221U, 2538335643U, 1873351298U,\n\t3368723763U, 2129144130U, 3203528633U, 3087174986U, 2691698871U,\n\t2516284287U,   24437745U, 1118381474U, 2816314867U, 2448576035U,\n\t4281989654U,  217287825U,  165872888U, 2628995722U, 3533525116U,\n\t2721669106U,  872340568U, 3429930655U, 3309047304U, 3916704967U,\n\t3270160355U, 1348884255U, 1634797670U,  881214967U, 4259633554U,\n\t 174613027U, 1103974314U, 1625224232U, 2678368291U, 1133866707U,\n\t3853082619U, 4073196549U, 1189620777U,  637238656U,  930241537U,\n\t4042750792U, 3842136042U, 2417007212U, 2524907510U, 1243036827U,\n\t1282059441U, 3764588774U, 1394459615U, 2323620015U, 1166152231U,\n\t3307479609U, 3849322257U, 3507445699U, 4247696636U,  758393720U,\n\t 967665141U, 1095244571U, 1319812152U,  407678762U, 2640605208U,\n\t2170766134U, 3663594275U, 4039329364U, 2512175520U,  725523154U,\n\t2249807004U, 3312617979U, 2414634172U, 1278482215U,  349206484U,\n\t1573063308U, 1196429124U, 3873264116U, 2400067801U,  268795167U,\n\t 226175489U, 2961367263U, 1968719665U,   42656370U, 1010790699U,\n\t 561600615U, 2422453992U, 3082197735U, 1636700484U, 3977715296U,\n\t3125350482U, 3478021514U, 2227819446U, 1540868045U, 3061908980U,\n\t1087362407U, 3625200291U,  361937537U,  580441897U, 1520043666U,\n\t2270875402U, 1009161260U, 2502355842U, 4278769785U,  473902412U,\n\t1057239083U, 1905829039U, 1483781177U, 2080011417U, 1207494246U,\n\t1806991954U, 2194674403U, 3455972205U,  807207678U, 3655655687U,\n\t 674112918U,  195425752U, 3917890095U, 1874364234U, 1837892715U,\n\t3663478166U, 1548892014U, 2570748714U, 2049929836U, 2167029704U,\n\t 697543767U, 3499545023U, 3342496315U, 1725251190U, 3561387469U,\n\t2905606616U, 1580182447U, 3934525927U, 4103172792U, 1365672522U,\n\t1534795737U, 3308667416U, 2841911405U, 3943182730U, 4072020313U,\n\t3494770452U, 3332626671U,   55327267U,  478030603U,  411080625U,\n\t3419529010U, 1604767823U, 3513468014U,  570668510U,  913790824U,\n\t2283967995U,  695159462U, 3825542932U, 4150698144U, 1829758699U,\n\t 202895590U, 1609122645U, 1267651008U, 2910315509U, 2511475445U,\n\t2477423819U, 3932081579U,  900879979U, 2145588390U, 2670007504U,\n\t 580819444U, 1864996828U, 2526325979U, 1019124258U,  815508628U,\n\t2765933989U, 1277301341U, 3006021786U,  855540956U,  288025710U,\n\t1919594237U, 2331223864U,  177452412U, 2475870369U, 2689291749U,\n\t 865194284U,  253432152U, 2628531804U, 2861208555U, 2361597573U,\n\t1653952120U, 1039661024U, 2159959078U, 3709040440U, 3564718533U,\n\t2596878672U, 2041442161U,   31164696U, 2662962485U, 3665637339U,\n\t1678115244U, 2699839832U, 3651968520U, 3521595541U,  458433303U,\n\t2423096824U,   21831741U,  380011703U, 2498168716U,  861806087U,\n\t1673574843U, 4188794405U, 2520563651U, 2632279153U, 2170465525U,\n\t4171949898U, 3886039621U, 1661344005U, 3424285243U,  992588372U,\n\t2500984144U, 2993248497U, 3590193895U, 1535327365U,  515645636U,\n\t 131633450U, 3729760261U, 1613045101U, 3254194278U,   15889678U,\n\t1493590689U,  244148718U, 2991472662U, 1401629333U,  777349878U,\n\t2501401703U, 4285518317U, 3794656178U,  955526526U, 3442142820U,\n\t3970298374U,  736025417U, 2737370764U, 1271509744U,  440570731U,\n\t 136141826U, 1596189518U,  923399175U,  257541519U, 3505774281U,\n\t2194358432U, 2518162991U, 1379893637U, 2667767062U, 3748146247U,\n\t1821712620U, 3923161384U, 1947811444U, 2392527197U, 4127419685U,\n\t1423694998U, 4156576871U, 1382885582U, 3420127279U, 3617499534U,\n\t2994377493U, 4038063986U, 1918458672U, 2983166794U, 4200449033U,\n\t 353294540U, 1609232588U,  243926648U, 2332803291U,  507996832U,\n\t2392838793U, 4075145196U, 2060984340U, 4287475136U,   88232602U,\n\t2491531140U, 4159725633U, 2272075455U,  759298618U,  201384554U,\n\t 838356250U, 1416268324U,  674476934U,   90795364U,  141672229U,\n\t3660399588U, 4196417251U, 3249270244U, 3774530247U,   59587265U,\n\t3683164208U,   19392575U, 1463123697U, 1882205379U,  293780489U,\n\t2553160622U, 2933904694U,  675638239U, 2851336944U, 1435238743U,\n\t2448730183U,  804436302U, 2119845972U,  322560608U, 4097732704U,\n\t2987802540U,  641492617U, 2575442710U, 4217822703U, 3271835300U,\n\t2836418300U, 3739921620U, 2138378768U, 2879771855U, 4294903423U,\n\t3121097946U, 2603440486U, 2560820391U, 1012930944U, 2313499967U,\n\t 584489368U, 3431165766U,  897384869U, 2062537737U, 2847889234U,\n\t3742362450U, 2951174585U, 4204621084U, 1109373893U, 3668075775U,\n\t2750138839U, 3518055702U,  733072558U, 4169325400U,  788493625U\n};\nstatic const uint64_t init_gen_rand_64_expected[] = {\n\tKQU(16924766246869039260), KQU( 8201438687333352714),\n\tKQU( 2265290287015001750), KQU(18397264611805473832),\n\tKQU( 3375255223302384358), KQU( 6345559975416828796),\n\tKQU(18229739242790328073), KQU( 7596792742098800905),\n\tKQU(  255338647169685981), KQU( 2052747240048610300),\n\tKQU(18328151576097299343), KQU(12472905421133796567),\n\tKQU(11315245349717600863), KQU(16594110197775871209),\n\tKQU(15708751964632456450), KQU(10452031272054632535),\n\tKQU(11097646720811454386), KQU( 4556090668445745441),\n\tKQU(17116187693090663106), KQU(14931526836144510645),\n\tKQU( 9190752218020552591), KQU( 9625800285771901401),\n\tKQU(13995141077659972832), KQU( 5194209094927829625),\n\tKQU( 4156788379151063303), KQU( 8523452593770139494),\n\tKQU(14082382103049296727), KQU( 2462601863986088483),\n\tKQU( 3030583461592840678), KQU( 5221622077872827681),\n\tKQU( 3084210671228981236), KQU(13956758381389953823),\n\tKQU(13503889856213423831), KQU(15696904024189836170),\n\tKQU( 4612584152877036206), KQU( 6231135538447867881),\n\tKQU(10172457294158869468), KQU( 6452258628466708150),\n\tKQU(14044432824917330221), KQU(  370168364480044279),\n\tKQU(10102144686427193359), KQU(  667870489994776076),\n\tKQU( 2732271956925885858), KQU(18027788905977284151),\n\tKQU(15009842788582923859), KQU( 7136357960180199542),\n\tKQU(15901736243475578127), KQU(16951293785352615701),\n\tKQU(10551492125243691632), KQU(17668869969146434804),\n\tKQU(13646002971174390445), KQU( 9804471050759613248),\n\tKQU( 5511670439655935493), KQU(18103342091070400926),\n\tKQU(17224512747665137533), KQU(15534627482992618168),\n\tKQU( 1423813266186582647), KQU(15821176807932930024),\n\tKQU(   30323369733607156), KQU(11599382494723479403),\n\tKQU(  653856076586810062), KQU( 3176437395144899659),\n\tKQU(14028076268147963917), KQU(16156398271809666195),\n\tKQU( 3166955484848201676), KQU( 5746805620136919390),\n\tKQU(17297845208891256593), KQU(11691653183226428483),\n\tKQU(17900026146506981577), KQU(15387382115755971042),\n\tKQU(16923567681040845943), KQU( 8039057517199388606),\n\tKQU(11748409241468629263), KQU(  794358245539076095),\n\tKQU(13438501964693401242), KQU(14036803236515618962),\n\tKQU( 5252311215205424721), KQU(17806589612915509081),\n\tKQU( 6802767092397596006), KQU(14212120431184557140),\n\tKQU( 1072951366761385712), KQU(13098491780722836296),\n\tKQU( 9466676828710797353), KQU(12673056849042830081),\n\tKQU(12763726623645357580), KQU(16468961652999309493),\n\tKQU(15305979875636438926), KQU(17444713151223449734),\n\tKQU( 5692214267627883674), KQU(13049589139196151505),\n\tKQU(  880115207831670745), KQU( 1776529075789695498),\n\tKQU(16695225897801466485), KQU(10666901778795346845),\n\tKQU( 6164389346722833869), KQU( 2863817793264300475),\n\tKQU( 9464049921886304754), KQU( 3993566636740015468),\n\tKQU( 9983749692528514136), KQU(16375286075057755211),\n\tKQU(16042643417005440820), KQU(11445419662923489877),\n\tKQU( 7999038846885158836), KQU( 6721913661721511535),\n\tKQU( 5363052654139357320), KQU( 1817788761173584205),\n\tKQU(13290974386445856444), KQU( 4650350818937984680),\n\tKQU( 8219183528102484836), KQU( 1569862923500819899),\n\tKQU( 4189359732136641860), KQU(14202822961683148583),\n\tKQU( 4457498315309429058), KQU(13089067387019074834),\n\tKQU(11075517153328927293), KQU(10277016248336668389),\n\tKQU( 7070509725324401122), KQU(17808892017780289380),\n\tKQU(13143367339909287349), KQU( 1377743745360085151),\n\tKQU( 5749341807421286485), KQU(14832814616770931325),\n\tKQU( 7688820635324359492), KQU(10960474011539770045),\n\tKQU(   81970066653179790), KQU(12619476072607878022),\n\tKQU( 4419566616271201744), KQU(15147917311750568503),\n\tKQU( 5549739182852706345), KQU( 7308198397975204770),\n\tKQU(13580425496671289278), KQU(17070764785210130301),\n\tKQU( 8202832846285604405), KQU( 6873046287640887249),\n\tKQU( 6927424434308206114), KQU( 6139014645937224874),\n\tKQU(10290373645978487639), KQU(15904261291701523804),\n\tKQU( 9628743442057826883), KQU(18383429096255546714),\n\tKQU( 4977413265753686967), KQU( 7714317492425012869),\n\tKQU( 9025232586309926193), KQU(14627338359776709107),\n\tKQU(14759849896467790763), KQU(10931129435864423252),\n\tKQU( 4588456988775014359), KQU(10699388531797056724),\n\tKQU(  468652268869238792), KQU( 5755943035328078086),\n\tKQU( 2102437379988580216), KQU( 9986312786506674028),\n\tKQU( 2654207180040945604), KQU( 8726634790559960062),\n\tKQU(  100497234871808137), KQU( 2800137176951425819),\n\tKQU( 6076627612918553487), KQU( 5780186919186152796),\n\tKQU( 8179183595769929098), KQU( 6009426283716221169),\n\tKQU( 2796662551397449358), KQU( 1756961367041986764),\n\tKQU( 6972897917355606205), KQU(14524774345368968243),\n\tKQU( 2773529684745706940), KQU( 4853632376213075959),\n\tKQU( 4198177923731358102), KQU( 8271224913084139776),\n\tKQU( 2741753121611092226), KQU(16782366145996731181),\n\tKQU(15426125238972640790), KQU(13595497100671260342),\n\tKQU( 3173531022836259898), KQU( 6573264560319511662),\n\tKQU(18041111951511157441), KQU( 2351433581833135952),\n\tKQU( 3113255578908173487), KQU( 1739371330877858784),\n\tKQU(16046126562789165480), KQU( 8072101652214192925),\n\tKQU(15267091584090664910), KQU( 9309579200403648940),\n\tKQU( 5218892439752408722), KQU(14492477246004337115),\n\tKQU(17431037586679770619), KQU( 7385248135963250480),\n\tKQU( 9580144956565560660), KQU( 4919546228040008720),\n\tKQU(15261542469145035584), KQU(18233297270822253102),\n\tKQU( 5453248417992302857), KQU( 9309519155931460285),\n\tKQU(10342813012345291756), KQU(15676085186784762381),\n\tKQU(15912092950691300645), KQU( 9371053121499003195),\n\tKQU( 9897186478226866746), KQU(14061858287188196327),\n\tKQU(  122575971620788119), KQU(12146750969116317754),\n\tKQU( 4438317272813245201), KQU( 8332576791009527119),\n\tKQU(13907785691786542057), KQU(10374194887283287467),\n\tKQU( 2098798755649059566), KQU( 3416235197748288894),\n\tKQU( 8688269957320773484), KQU( 7503964602397371571),\n\tKQU(16724977015147478236), KQU( 9461512855439858184),\n\tKQU(13259049744534534727), KQU( 3583094952542899294),\n\tKQU( 8764245731305528292), KQU(13240823595462088985),\n\tKQU(13716141617617910448), KQU(18114969519935960955),\n\tKQU( 2297553615798302206), KQU( 4585521442944663362),\n\tKQU(17776858680630198686), KQU( 4685873229192163363),\n\tKQU(  152558080671135627), KQU(15424900540842670088),\n\tKQU(13229630297130024108), KQU(17530268788245718717),\n\tKQU(16675633913065714144), KQU( 3158912717897568068),\n\tKQU(15399132185380087288), KQU( 7401418744515677872),\n\tKQU(13135412922344398535), KQU( 6385314346100509511),\n\tKQU(13962867001134161139), KQU(10272780155442671999),\n\tKQU(12894856086597769142), KQU(13340877795287554994),\n\tKQU(12913630602094607396), KQU(12543167911119793857),\n\tKQU(17343570372251873096), KQU(10959487764494150545),\n\tKQU( 6966737953093821128), KQU(13780699135496988601),\n\tKQU( 4405070719380142046), KQU(14923788365607284982),\n\tKQU( 2869487678905148380), KQU( 6416272754197188403),\n\tKQU(15017380475943612591), KQU( 1995636220918429487),\n\tKQU( 3402016804620122716), KQU(15800188663407057080),\n\tKQU(11362369990390932882), KQU(15262183501637986147),\n\tKQU(10239175385387371494), KQU( 9352042420365748334),\n\tKQU( 1682457034285119875), KQU( 1724710651376289644),\n\tKQU( 2038157098893817966), KQU( 9897825558324608773),\n\tKQU( 1477666236519164736), KQU(16835397314511233640),\n\tKQU(10370866327005346508), KQU(10157504370660621982),\n\tKQU(12113904045335882069), KQU(13326444439742783008),\n\tKQU(11302769043000765804), KQU(13594979923955228484),\n\tKQU(11779351762613475968), KQU( 3786101619539298383),\n\tKQU( 8021122969180846063), KQU(15745904401162500495),\n\tKQU(10762168465993897267), KQU(13552058957896319026),\n\tKQU(11200228655252462013), KQU( 5035370357337441226),\n\tKQU( 7593918984545500013), KQU( 5418554918361528700),\n\tKQU( 4858270799405446371), KQU( 9974659566876282544),\n\tKQU(18227595922273957859), KQU( 2772778443635656220),\n\tKQU(14285143053182085385), KQU( 9939700992429600469),\n\tKQU(12756185904545598068), KQU( 2020783375367345262),\n\tKQU(   57026775058331227), KQU(  950827867930065454),\n\tKQU( 6602279670145371217), KQU( 2291171535443566929),\n\tKQU( 5832380724425010313), KQU( 1220343904715982285),\n\tKQU(17045542598598037633), KQU(15460481779702820971),\n\tKQU(13948388779949365130), KQU(13975040175430829518),\n\tKQU(17477538238425541763), KQU(11104663041851745725),\n\tKQU(15860992957141157587), KQU(14529434633012950138),\n\tKQU( 2504838019075394203), KQU( 7512113882611121886),\n\tKQU( 4859973559980886617), KQU( 1258601555703250219),\n\tKQU(15594548157514316394), KQU( 4516730171963773048),\n\tKQU(11380103193905031983), KQU( 6809282239982353344),\n\tKQU(18045256930420065002), KQU( 2453702683108791859),\n\tKQU(  977214582986981460), KQU( 2006410402232713466),\n\tKQU( 6192236267216378358), KQU( 3429468402195675253),\n\tKQU(18146933153017348921), KQU(17369978576367231139),\n\tKQU( 1246940717230386603), KQU(11335758870083327110),\n\tKQU(14166488801730353682), KQU( 9008573127269635732),\n\tKQU(10776025389820643815), KQU(15087605441903942962),\n\tKQU( 1359542462712147922), KQU(13898874411226454206),\n\tKQU(17911176066536804411), KQU( 9435590428600085274),\n\tKQU(  294488509967864007), KQU( 8890111397567922046),\n\tKQU( 7987823476034328778), KQU(13263827582440967651),\n\tKQU( 7503774813106751573), KQU(14974747296185646837),\n\tKQU( 8504765037032103375), KQU(17340303357444536213),\n\tKQU( 7704610912964485743), KQU( 8107533670327205061),\n\tKQU( 9062969835083315985), KQU(16968963142126734184),\n\tKQU(12958041214190810180), KQU( 2720170147759570200),\n\tKQU( 2986358963942189566), KQU(14884226322219356580),\n\tKQU(  286224325144368520), KQU(11313800433154279797),\n\tKQU(18366849528439673248), KQU(17899725929482368789),\n\tKQU( 3730004284609106799), KQU( 1654474302052767205),\n\tKQU( 5006698007047077032), KQU( 8196893913601182838),\n\tKQU(15214541774425211640), KQU(17391346045606626073),\n\tKQU( 8369003584076969089), KQU( 3939046733368550293),\n\tKQU(10178639720308707785), KQU( 2180248669304388697),\n\tKQU(   62894391300126322), KQU( 9205708961736223191),\n\tKQU( 6837431058165360438), KQU( 3150743890848308214),\n\tKQU(17849330658111464583), KQU(12214815643135450865),\n\tKQU(13410713840519603402), KQU( 3200778126692046802),\n\tKQU(13354780043041779313), KQU(  800850022756886036),\n\tKQU(15660052933953067433), KQU( 6572823544154375676),\n\tKQU(11030281857015819266), KQU(12682241941471433835),\n\tKQU(11654136407300274693), KQU( 4517795492388641109),\n\tKQU( 9757017371504524244), KQU(17833043400781889277),\n\tKQU(12685085201747792227), KQU(10408057728835019573),\n\tKQU(   98370418513455221), KQU( 6732663555696848598),\n\tKQU(13248530959948529780), KQU( 3530441401230622826),\n\tKQU(18188251992895660615), KQU( 1847918354186383756),\n\tKQU( 1127392190402660921), KQU(11293734643143819463),\n\tKQU( 3015506344578682982), KQU(13852645444071153329),\n\tKQU( 2121359659091349142), KQU( 1294604376116677694),\n\tKQU( 5616576231286352318), KQU( 7112502442954235625),\n\tKQU(11676228199551561689), KQU(12925182803007305359),\n\tKQU( 7852375518160493082), KQU( 1136513130539296154),\n\tKQU( 5636923900916593195), KQU( 3221077517612607747),\n\tKQU(17784790465798152513), KQU( 3554210049056995938),\n\tKQU(17476839685878225874), KQU( 3206836372585575732),\n\tKQU( 2765333945644823430), KQU(10080070903718799528),\n\tKQU( 5412370818878286353), KQU( 9689685887726257728),\n\tKQU( 8236117509123533998), KQU( 1951139137165040214),\n\tKQU( 4492205209227980349), KQU(16541291230861602967),\n\tKQU( 1424371548301437940), KQU( 9117562079669206794),\n\tKQU(14374681563251691625), KQU(13873164030199921303),\n\tKQU( 6680317946770936731), KQU(15586334026918276214),\n\tKQU(10896213950976109802), KQU( 9506261949596413689),\n\tKQU( 9903949574308040616), KQU( 6038397344557204470),\n\tKQU(  174601465422373648), KQU(15946141191338238030),\n\tKQU(17142225620992044937), KQU( 7552030283784477064),\n\tKQU( 2947372384532947997), KQU(  510797021688197711),\n\tKQU( 4962499439249363461), KQU(   23770320158385357),\n\tKQU(  959774499105138124), KQU( 1468396011518788276),\n\tKQU( 2015698006852312308), KQU( 4149400718489980136),\n\tKQU( 5992916099522371188), KQU(10819182935265531076),\n\tKQU(16189787999192351131), KQU(  342833961790261950),\n\tKQU(12470830319550495336), KQU(18128495041912812501),\n\tKQU( 1193600899723524337), KQU( 9056793666590079770),\n\tKQU( 2154021227041669041), KQU( 4963570213951235735),\n\tKQU( 4865075960209211409), KQU( 2097724599039942963),\n\tKQU( 2024080278583179845), KQU(11527054549196576736),\n\tKQU(10650256084182390252), KQU( 4808408648695766755),\n\tKQU( 1642839215013788844), KQU(10607187948250398390),\n\tKQU( 7076868166085913508), KQU(  730522571106887032),\n\tKQU(12500579240208524895), KQU( 4484390097311355324),\n\tKQU(15145801330700623870), KQU( 8055827661392944028),\n\tKQU( 5865092976832712268), KQU(15159212508053625143),\n\tKQU( 3560964582876483341), KQU( 4070052741344438280),\n\tKQU( 6032585709886855634), KQU(15643262320904604873),\n\tKQU( 2565119772293371111), KQU(  318314293065348260),\n\tKQU(15047458749141511872), KQU( 7772788389811528730),\n\tKQU( 7081187494343801976), KQU( 6465136009467253947),\n\tKQU(10425940692543362069), KQU(  554608190318339115),\n\tKQU(14796699860302125214), KQU( 1638153134431111443),\n\tKQU(10336967447052276248), KQU( 8412308070396592958),\n\tKQU( 4004557277152051226), KQU( 8143598997278774834),\n\tKQU(16413323996508783221), KQU(13139418758033994949),\n\tKQU( 9772709138335006667), KQU( 2818167159287157659),\n\tKQU(17091740573832523669), KQU(14629199013130751608),\n\tKQU(18268322711500338185), KQU( 8290963415675493063),\n\tKQU( 8830864907452542588), KQU( 1614839084637494849),\n\tKQU(14855358500870422231), KQU( 3472996748392519937),\n\tKQU(15317151166268877716), KQU( 5825895018698400362),\n\tKQU(16730208429367544129), KQU(10481156578141202800),\n\tKQU( 4746166512382823750), KQU(12720876014472464998),\n\tKQU( 8825177124486735972), KQU(13733447296837467838),\n\tKQU( 6412293741681359625), KQU( 8313213138756135033),\n\tKQU(11421481194803712517), KQU( 7997007691544174032),\n\tKQU( 6812963847917605930), KQU( 9683091901227558641),\n\tKQU(14703594165860324713), KQU( 1775476144519618309),\n\tKQU( 2724283288516469519), KQU(  717642555185856868),\n\tKQU( 8736402192215092346), KQU(11878800336431381021),\n\tKQU( 4348816066017061293), KQU( 6115112756583631307),\n\tKQU( 9176597239667142976), KQU(12615622714894259204),\n\tKQU(10283406711301385987), KQU( 5111762509485379420),\n\tKQU( 3118290051198688449), KQU( 7345123071632232145),\n\tKQU( 9176423451688682359), KQU( 4843865456157868971),\n\tKQU(12008036363752566088), KQU(12058837181919397720),\n\tKQU( 2145073958457347366), KQU( 1526504881672818067),\n\tKQU( 3488830105567134848), KQU(13208362960674805143),\n\tKQU( 4077549672899572192), KQU( 7770995684693818365),\n\tKQU( 1398532341546313593), KQU(12711859908703927840),\n\tKQU( 1417561172594446813), KQU(17045191024194170604),\n\tKQU( 4101933177604931713), KQU(14708428834203480320),\n\tKQU(17447509264469407724), KQU(14314821973983434255),\n\tKQU(17990472271061617265), KQU( 5087756685841673942),\n\tKQU(12797820586893859939), KQU( 1778128952671092879),\n\tKQU( 3535918530508665898), KQU( 9035729701042481301),\n\tKQU(14808661568277079962), KQU(14587345077537747914),\n\tKQU(11920080002323122708), KQU( 6426515805197278753),\n\tKQU( 3295612216725984831), KQU(11040722532100876120),\n\tKQU(12305952936387598754), KQU(16097391899742004253),\n\tKQU( 4908537335606182208), KQU(12446674552196795504),\n\tKQU(16010497855816895177), KQU( 9194378874788615551),\n\tKQU( 3382957529567613384), KQU( 5154647600754974077),\n\tKQU( 9801822865328396141), KQU( 9023662173919288143),\n\tKQU(17623115353825147868), KQU( 8238115767443015816),\n\tKQU(15811444159859002560), KQU( 9085612528904059661),\n\tKQU( 6888601089398614254), KQU(  258252992894160189),\n\tKQU( 6704363880792428622), KQU( 6114966032147235763),\n\tKQU(11075393882690261875), KQU( 8797664238933620407),\n\tKQU( 5901892006476726920), KQU( 5309780159285518958),\n\tKQU(14940808387240817367), KQU(14642032021449656698),\n\tKQU( 9808256672068504139), KQU( 3670135111380607658),\n\tKQU(11211211097845960152), KQU( 1474304506716695808),\n\tKQU(15843166204506876239), KQU( 7661051252471780561),\n\tKQU(10170905502249418476), KQU( 7801416045582028589),\n\tKQU( 2763981484737053050), KQU( 9491377905499253054),\n\tKQU(16201395896336915095), KQU( 9256513756442782198),\n\tKQU( 5411283157972456034), KQU( 5059433122288321676),\n\tKQU( 4327408006721123357), KQU( 9278544078834433377),\n\tKQU( 7601527110882281612), KQU(11848295896975505251),\n\tKQU(12096998801094735560), KQU(14773480339823506413),\n\tKQU(15586227433895802149), KQU(12786541257830242872),\n\tKQU( 6904692985140503067), KQU( 5309011515263103959),\n\tKQU(12105257191179371066), KQU(14654380212442225037),\n\tKQU( 2556774974190695009), KQU( 4461297399927600261),\n\tKQU(14888225660915118646), KQU(14915459341148291824),\n\tKQU( 2738802166252327631), KQU( 6047155789239131512),\n\tKQU(12920545353217010338), KQU(10697617257007840205),\n\tKQU( 2751585253158203504), KQU(13252729159780047496),\n\tKQU(14700326134672815469), KQU(14082527904374600529),\n\tKQU(16852962273496542070), KQU(17446675504235853907),\n\tKQU(15019600398527572311), KQU(12312781346344081551),\n\tKQU(14524667935039810450), KQU( 5634005663377195738),\n\tKQU(11375574739525000569), KQU( 2423665396433260040),\n\tKQU( 5222836914796015410), KQU( 4397666386492647387),\n\tKQU( 4619294441691707638), KQU(  665088602354770716),\n\tKQU(13246495665281593610), KQU( 6564144270549729409),\n\tKQU(10223216188145661688), KQU( 3961556907299230585),\n\tKQU(11543262515492439914), KQU(16118031437285993790),\n\tKQU( 7143417964520166465), KQU(13295053515909486772),\n\tKQU(   40434666004899675), KQU(17127804194038347164),\n\tKQU( 8599165966560586269), KQU( 8214016749011284903),\n\tKQU(13725130352140465239), KQU( 5467254474431726291),\n\tKQU( 7748584297438219877), KQU(16933551114829772472),\n\tKQU( 2169618439506799400), KQU( 2169787627665113463),\n\tKQU(17314493571267943764), KQU(18053575102911354912),\n\tKQU(11928303275378476973), KQU(11593850925061715550),\n\tKQU(17782269923473589362), KQU( 3280235307704747039),\n\tKQU( 6145343578598685149), KQU(17080117031114086090),\n\tKQU(18066839902983594755), KQU( 6517508430331020706),\n\tKQU( 8092908893950411541), KQU(12558378233386153732),\n\tKQU( 4476532167973132976), KQU(16081642430367025016),\n\tKQU( 4233154094369139361), KQU( 8693630486693161027),\n\tKQU(11244959343027742285), KQU(12273503967768513508),\n\tKQU(14108978636385284876), KQU( 7242414665378826984),\n\tKQU( 6561316938846562432), KQU( 8601038474994665795),\n\tKQU(17532942353612365904), KQU(17940076637020912186),\n\tKQU( 7340260368823171304), KQU( 7061807613916067905),\n\tKQU(10561734935039519326), KQU(17990796503724650862),\n\tKQU( 6208732943911827159), KQU(  359077562804090617),\n\tKQU(14177751537784403113), KQU(10659599444915362902),\n\tKQU(15081727220615085833), KQU(13417573895659757486),\n\tKQU(15513842342017811524), KQU(11814141516204288231),\n\tKQU( 1827312513875101814), KQU( 2804611699894603103),\n\tKQU(17116500469975602763), KQU(12270191815211952087),\n\tKQU(12256358467786024988), KQU(18435021722453971267),\n\tKQU(  671330264390865618), KQU(  476504300460286050),\n\tKQU(16465470901027093441), KQU( 4047724406247136402),\n\tKQU( 1322305451411883346), KQU( 1388308688834322280),\n\tKQU( 7303989085269758176), KQU( 9323792664765233642),\n\tKQU( 4542762575316368936), KQU(17342696132794337618),\n\tKQU( 4588025054768498379), KQU(13415475057390330804),\n\tKQU(17880279491733405570), KQU(10610553400618620353),\n\tKQU( 3180842072658960139), KQU(13002966655454270120),\n\tKQU( 1665301181064982826), KQU( 7083673946791258979),\n\tKQU(  190522247122496820), KQU(17388280237250677740),\n\tKQU( 8430770379923642945), KQU(12987180971921668584),\n\tKQU( 2311086108365390642), KQU( 2870984383579822345),\n\tKQU(14014682609164653318), KQU(14467187293062251484),\n\tKQU(  192186361147413298), KQU(15171951713531796524),\n\tKQU( 9900305495015948728), KQU(17958004775615466344),\n\tKQU(14346380954498606514), KQU(18040047357617407096),\n\tKQU( 5035237584833424532), KQU(15089555460613972287),\n\tKQU( 4131411873749729831), KQU( 1329013581168250330),\n\tKQU(10095353333051193949), KQU(10749518561022462716),\n\tKQU( 9050611429810755847), KQU(15022028840236655649),\n\tKQU( 8775554279239748298), KQU(13105754025489230502),\n\tKQU(15471300118574167585), KQU(   89864764002355628),\n\tKQU( 8776416323420466637), KQU( 5280258630612040891),\n\tKQU( 2719174488591862912), KQU( 7599309137399661994),\n\tKQU(15012887256778039979), KQU(14062981725630928925),\n\tKQU(12038536286991689603), KQU( 7089756544681775245),\n\tKQU(10376661532744718039), KQU( 1265198725901533130),\n\tKQU(13807996727081142408), KQU( 2935019626765036403),\n\tKQU( 7651672460680700141), KQU( 3644093016200370795),\n\tKQU( 2840982578090080674), KQU(17956262740157449201),\n\tKQU(18267979450492880548), KQU(11799503659796848070),\n\tKQU( 9942537025669672388), KQU(11886606816406990297),\n\tKQU( 5488594946437447576), KQU( 7226714353282744302),\n\tKQU( 3784851653123877043), KQU(  878018453244803041),\n\tKQU(12110022586268616085), KQU(  734072179404675123),\n\tKQU(11869573627998248542), KQU(  469150421297783998),\n\tKQU(  260151124912803804), KQU(11639179410120968649),\n\tKQU( 9318165193840846253), KQU(12795671722734758075),\n\tKQU(15318410297267253933), KQU(  691524703570062620),\n\tKQU( 5837129010576994601), KQU(15045963859726941052),\n\tKQU( 5850056944932238169), KQU(12017434144750943807),\n\tKQU( 7447139064928956574), KQU( 3101711812658245019),\n\tKQU(16052940704474982954), KQU(18195745945986994042),\n\tKQU( 8932252132785575659), KQU(13390817488106794834),\n\tKQU(11582771836502517453), KQU( 4964411326683611686),\n\tKQU( 2195093981702694011), KQU(14145229538389675669),\n\tKQU(16459605532062271798), KQU(  866316924816482864),\n\tKQU( 4593041209937286377), KQU( 8415491391910972138),\n\tKQU( 4171236715600528969), KQU(16637569303336782889),\n\tKQU( 2002011073439212680), KQU(17695124661097601411),\n\tKQU( 4627687053598611702), KQU( 7895831936020190403),\n\tKQU( 8455951300917267802), KQU( 2923861649108534854),\n\tKQU( 8344557563927786255), KQU( 6408671940373352556),\n\tKQU(12210227354536675772), KQU(14294804157294222295),\n\tKQU(10103022425071085127), KQU(10092959489504123771),\n\tKQU( 6554774405376736268), KQU(12629917718410641774),\n\tKQU( 6260933257596067126), KQU( 2460827021439369673),\n\tKQU( 2541962996717103668), KQU(  597377203127351475),\n\tKQU( 5316984203117315309), KQU( 4811211393563241961),\n\tKQU(13119698597255811641), KQU( 8048691512862388981),\n\tKQU(10216818971194073842), KQU( 4612229970165291764),\n\tKQU(10000980798419974770), KQU( 6877640812402540687),\n\tKQU( 1488727563290436992), KQU( 2227774069895697318),\n\tKQU(11237754507523316593), KQU(13478948605382290972),\n\tKQU( 1963583846976858124), KQU( 5512309205269276457),\n\tKQU( 3972770164717652347), KQU( 3841751276198975037),\n\tKQU(10283343042181903117), KQU( 8564001259792872199),\n\tKQU(16472187244722489221), KQU( 8953493499268945921),\n\tKQU( 3518747340357279580), KQU( 4003157546223963073),\n\tKQU( 3270305958289814590), KQU( 3966704458129482496),\n\tKQU( 8122141865926661939), KQU(14627734748099506653),\n\tKQU(13064426990862560568), KQU( 2414079187889870829),\n\tKQU( 5378461209354225306), KQU(10841985740128255566),\n\tKQU(  538582442885401738), KQU( 7535089183482905946),\n\tKQU(16117559957598879095), KQU( 8477890721414539741),\n\tKQU( 1459127491209533386), KQU(17035126360733620462),\n\tKQU( 8517668552872379126), KQU(10292151468337355014),\n\tKQU(17081267732745344157), KQU(13751455337946087178),\n\tKQU(14026945459523832966), KQU( 6653278775061723516),\n\tKQU(10619085543856390441), KQU( 2196343631481122885),\n\tKQU(10045966074702826136), KQU(10082317330452718282),\n\tKQU( 5920859259504831242), KQU( 9951879073426540617),\n\tKQU( 7074696649151414158), KQU(15808193543879464318),\n\tKQU( 7385247772746953374), KQU( 3192003544283864292),\n\tKQU(18153684490917593847), KQU(12423498260668568905),\n\tKQU(10957758099756378169), KQU(11488762179911016040),\n\tKQU( 2099931186465333782), KQU(11180979581250294432),\n\tKQU( 8098916250668367933), KQU( 3529200436790763465),\n\tKQU(12988418908674681745), KQU( 6147567275954808580),\n\tKQU( 3207503344604030989), KQU(10761592604898615360),\n\tKQU(  229854861031893504), KQU( 8809853962667144291),\n\tKQU(13957364469005693860), KQU( 7634287665224495886),\n\tKQU(12353487366976556874), KQU( 1134423796317152034),\n\tKQU( 2088992471334107068), KQU( 7393372127190799698),\n\tKQU( 1845367839871058391), KQU(  207922563987322884),\n\tKQU(11960870813159944976), KQU(12182120053317317363),\n\tKQU(17307358132571709283), KQU(13871081155552824936),\n\tKQU(18304446751741566262), KQU( 7178705220184302849),\n\tKQU(10929605677758824425), KQU(16446976977835806844),\n\tKQU(13723874412159769044), KQU( 6942854352100915216),\n\tKQU( 1726308474365729390), KQU( 2150078766445323155),\n\tKQU(15345558947919656626), KQU(12145453828874527201),\n\tKQU( 2054448620739726849), KQU( 2740102003352628137),\n\tKQU(11294462163577610655), KQU(  756164283387413743),\n\tKQU(17841144758438810880), KQU(10802406021185415861),\n\tKQU( 8716455530476737846), KQU( 6321788834517649606),\n\tKQU(14681322910577468426), KQU(17330043563884336387),\n\tKQU(12701802180050071614), KQU(14695105111079727151),\n\tKQU( 5112098511654172830), KQU( 4957505496794139973),\n\tKQU( 8270979451952045982), KQU(12307685939199120969),\n\tKQU(12425799408953443032), KQU( 8376410143634796588),\n\tKQU(16621778679680060464), KQU( 3580497854566660073),\n\tKQU( 1122515747803382416), KQU(  857664980960597599),\n\tKQU( 6343640119895925918), KQU(12878473260854462891),\n\tKQU(10036813920765722626), KQU(14451335468363173812),\n\tKQU( 5476809692401102807), KQU(16442255173514366342),\n\tKQU(13060203194757167104), KQU(14354124071243177715),\n\tKQU(15961249405696125227), KQU(13703893649690872584),\n\tKQU(  363907326340340064), KQU( 6247455540491754842),\n\tKQU(12242249332757832361), KQU(  156065475679796717),\n\tKQU( 9351116235749732355), KQU( 4590350628677701405),\n\tKQU( 1671195940982350389), KQU(13501398458898451905),\n\tKQU( 6526341991225002255), KQU( 1689782913778157592),\n\tKQU( 7439222350869010334), KQU(13975150263226478308),\n\tKQU(11411961169932682710), KQU(17204271834833847277),\n\tKQU(  541534742544435367), KQU( 6591191931218949684),\n\tKQU( 2645454775478232486), KQU( 4322857481256485321),\n\tKQU( 8477416487553065110), KQU(12902505428548435048),\n\tKQU(  971445777981341415), KQU(14995104682744976712),\n\tKQU( 4243341648807158063), KQU( 8695061252721927661),\n\tKQU( 5028202003270177222), KQU( 2289257340915567840),\n\tKQU(13870416345121866007), KQU(13994481698072092233),\n\tKQU( 6912785400753196481), KQU( 2278309315841980139),\n\tKQU( 4329765449648304839), KQU( 5963108095785485298),\n\tKQU( 4880024847478722478), KQU(16015608779890240947),\n\tKQU( 1866679034261393544), KQU(  914821179919731519),\n\tKQU( 9643404035648760131), KQU( 2418114953615593915),\n\tKQU(  944756836073702374), KQU(15186388048737296834),\n\tKQU( 7723355336128442206), KQU( 7500747479679599691),\n\tKQU(18013961306453293634), KQU( 2315274808095756456),\n\tKQU(13655308255424029566), KQU(17203800273561677098),\n\tKQU( 1382158694422087756), KQU( 5090390250309588976),\n\tKQU(  517170818384213989), KQU( 1612709252627729621),\n\tKQU( 1330118955572449606), KQU(  300922478056709885),\n\tKQU(18115693291289091987), KQU(13491407109725238321),\n\tKQU(15293714633593827320), KQU( 5151539373053314504),\n\tKQU( 5951523243743139207), KQU(14459112015249527975),\n\tKQU( 5456113959000700739), KQU( 3877918438464873016),\n\tKQU(12534071654260163555), KQU(15871678376893555041),\n\tKQU(11005484805712025549), KQU(16353066973143374252),\n\tKQU( 4358331472063256685), KQU( 8268349332210859288),\n\tKQU(12485161590939658075), KQU(13955993592854471343),\n\tKQU( 5911446886848367039), KQU(14925834086813706974),\n\tKQU( 6590362597857994805), KQU( 1280544923533661875),\n\tKQU( 1637756018947988164), KQU( 4734090064512686329),\n\tKQU(16693705263131485912), KQU( 6834882340494360958),\n\tKQU( 8120732176159658505), KQU( 2244371958905329346),\n\tKQU(10447499707729734021), KQU( 7318742361446942194),\n\tKQU( 8032857516355555296), KQU(14023605983059313116),\n\tKQU( 1032336061815461376), KQU( 9840995337876562612),\n\tKQU( 9869256223029203587), KQU(12227975697177267636),\n\tKQU(12728115115844186033), KQU( 7752058479783205470),\n\tKQU(  729733219713393087), KQU(12954017801239007622)\n};\nstatic const uint64_t init_by_array_64_expected[] = {\n\tKQU( 2100341266307895239), KQU( 8344256300489757943),\n\tKQU(15687933285484243894), KQU( 8268620370277076319),\n\tKQU(12371852309826545459), KQU( 8800491541730110238),\n\tKQU(18113268950100835773), KQU( 2886823658884438119),\n\tKQU( 3293667307248180724), KQU( 9307928143300172731),\n\tKQU( 7688082017574293629), KQU(  900986224735166665),\n\tKQU( 9977972710722265039), KQU( 6008205004994830552),\n\tKQU(  546909104521689292), KQU( 7428471521869107594),\n\tKQU(14777563419314721179), KQU(16116143076567350053),\n\tKQU( 5322685342003142329), KQU( 4200427048445863473),\n\tKQU( 4693092150132559146), KQU(13671425863759338582),\n\tKQU( 6747117460737639916), KQU( 4732666080236551150),\n\tKQU( 5912839950611941263), KQU( 3903717554504704909),\n\tKQU( 2615667650256786818), KQU(10844129913887006352),\n\tKQU(13786467861810997820), KQU(14267853002994021570),\n\tKQU(13767807302847237439), KQU(16407963253707224617),\n\tKQU( 4802498363698583497), KQU( 2523802839317209764),\n\tKQU( 3822579397797475589), KQU( 8950320572212130610),\n\tKQU( 3745623504978342534), KQU(16092609066068482806),\n\tKQU( 9817016950274642398), KQU(10591660660323829098),\n\tKQU(11751606650792815920), KQU( 5122873818577122211),\n\tKQU(17209553764913936624), KQU( 6249057709284380343),\n\tKQU(15088791264695071830), KQU(15344673071709851930),\n\tKQU( 4345751415293646084), KQU( 2542865750703067928),\n\tKQU(13520525127852368784), KQU(18294188662880997241),\n\tKQU( 3871781938044881523), KQU( 2873487268122812184),\n\tKQU(15099676759482679005), KQU(15442599127239350490),\n\tKQU( 6311893274367710888), KQU( 3286118760484672933),\n\tKQU( 4146067961333542189), KQU(13303942567897208770),\n\tKQU( 8196013722255630418), KQU( 4437815439340979989),\n\tKQU(15433791533450605135), KQU( 4254828956815687049),\n\tKQU( 1310903207708286015), KQU(10529182764462398549),\n\tKQU(14900231311660638810), KQU( 9727017277104609793),\n\tKQU( 1821308310948199033), KQU(11628861435066772084),\n\tKQU( 9469019138491546924), KQU( 3145812670532604988),\n\tKQU( 9938468915045491919), KQU( 1562447430672662142),\n\tKQU(13963995266697989134), KQU( 3356884357625028695),\n\tKQU( 4499850304584309747), KQU( 8456825817023658122),\n\tKQU(10859039922814285279), KQU( 8099512337972526555),\n\tKQU(  348006375109672149), KQU(11919893998241688603),\n\tKQU( 1104199577402948826), KQU(16689191854356060289),\n\tKQU(10992552041730168078), KQU( 7243733172705465836),\n\tKQU( 5668075606180319560), KQU(18182847037333286970),\n\tKQU( 4290215357664631322), KQU( 4061414220791828613),\n\tKQU(13006291061652989604), KQU( 7140491178917128798),\n\tKQU(12703446217663283481), KQU( 5500220597564558267),\n\tKQU(10330551509971296358), KQU(15958554768648714492),\n\tKQU( 5174555954515360045), KQU( 1731318837687577735),\n\tKQU( 3557700801048354857), KQU(13764012341928616198),\n\tKQU(13115166194379119043), KQU( 7989321021560255519),\n\tKQU( 2103584280905877040), KQU( 9230788662155228488),\n\tKQU(16396629323325547654), KQU(  657926409811318051),\n\tKQU(15046700264391400727), KQU( 5120132858771880830),\n\tKQU( 7934160097989028561), KQU( 6963121488531976245),\n\tKQU(17412329602621742089), KQU(15144843053931774092),\n\tKQU(17204176651763054532), KQU(13166595387554065870),\n\tKQU( 8590377810513960213), KQU( 5834365135373991938),\n\tKQU( 7640913007182226243), KQU( 3479394703859418425),\n\tKQU(16402784452644521040), KQU( 4993979809687083980),\n\tKQU(13254522168097688865), KQU(15643659095244365219),\n\tKQU( 5881437660538424982), KQU(11174892200618987379),\n\tKQU(  254409966159711077), KQU(17158413043140549909),\n\tKQU( 3638048789290376272), KQU( 1376816930299489190),\n\tKQU( 4622462095217761923), KQU(15086407973010263515),\n\tKQU(13253971772784692238), KQU( 5270549043541649236),\n\tKQU(11182714186805411604), KQU(12283846437495577140),\n\tKQU( 5297647149908953219), KQU(10047451738316836654),\n\tKQU( 4938228100367874746), KQU(12328523025304077923),\n\tKQU( 3601049438595312361), KQU( 9313624118352733770),\n\tKQU(13322966086117661798), KQU(16660005705644029394),\n\tKQU(11337677526988872373), KQU(13869299102574417795),\n\tKQU(15642043183045645437), KQU( 3021755569085880019),\n\tKQU( 4979741767761188161), KQU(13679979092079279587),\n\tKQU( 3344685842861071743), KQU(13947960059899588104),\n\tKQU(  305806934293368007), KQU( 5749173929201650029),\n\tKQU(11123724852118844098), KQU(15128987688788879802),\n\tKQU(15251651211024665009), KQU( 7689925933816577776),\n\tKQU(16732804392695859449), KQU(17087345401014078468),\n\tKQU(14315108589159048871), KQU( 4820700266619778917),\n\tKQU(16709637539357958441), KQU( 4936227875177351374),\n\tKQU( 2137907697912987247), KQU(11628565601408395420),\n\tKQU( 2333250549241556786), KQU( 5711200379577778637),\n\tKQU( 5170680131529031729), KQU(12620392043061335164),\n\tKQU(   95363390101096078), KQU( 5487981914081709462),\n\tKQU( 1763109823981838620), KQU( 3395861271473224396),\n\tKQU( 1300496844282213595), KQU( 6894316212820232902),\n\tKQU(10673859651135576674), KQU( 5911839658857903252),\n\tKQU(17407110743387299102), KQU( 8257427154623140385),\n\tKQU(11389003026741800267), KQU( 4070043211095013717),\n\tKQU(11663806997145259025), KQU(15265598950648798210),\n\tKQU(  630585789434030934), KQU( 3524446529213587334),\n\tKQU( 7186424168495184211), KQU(10806585451386379021),\n\tKQU(11120017753500499273), KQU( 1586837651387701301),\n\tKQU(17530454400954415544), KQU( 9991670045077880430),\n\tKQU( 7550997268990730180), KQU( 8640249196597379304),\n\tKQU( 3522203892786893823), KQU(10401116549878854788),\n\tKQU(13690285544733124852), KQU( 8295785675455774586),\n\tKQU(15535716172155117603), KQU( 3112108583723722511),\n\tKQU(17633179955339271113), KQU(18154208056063759375),\n\tKQU( 1866409236285815666), KQU(13326075895396412882),\n\tKQU( 8756261842948020025), KQU( 6281852999868439131),\n\tKQU(15087653361275292858), KQU(10333923911152949397),\n\tKQU( 5265567645757408500), KQU(12728041843210352184),\n\tKQU( 6347959327507828759), KQU(  154112802625564758),\n\tKQU(18235228308679780218), KQU( 3253805274673352418),\n\tKQU( 4849171610689031197), KQU(17948529398340432518),\n\tKQU(13803510475637409167), KQU(13506570190409883095),\n\tKQU(15870801273282960805), KQU( 8451286481299170773),\n\tKQU( 9562190620034457541), KQU( 8518905387449138364),\n\tKQU(12681306401363385655), KQU( 3788073690559762558),\n\tKQU( 5256820289573487769), KQU( 2752021372314875467),\n\tKQU( 6354035166862520716), KQU( 4328956378309739069),\n\tKQU(  449087441228269600), KQU( 5533508742653090868),\n\tKQU( 1260389420404746988), KQU(18175394473289055097),\n\tKQU( 1535467109660399420), KQU( 8818894282874061442),\n\tKQU(12140873243824811213), KQU(15031386653823014946),\n\tKQU( 1286028221456149232), KQU( 6329608889367858784),\n\tKQU( 9419654354945132725), KQU( 6094576547061672379),\n\tKQU(17706217251847450255), KQU( 1733495073065878126),\n\tKQU(16918923754607552663), KQU( 8881949849954945044),\n\tKQU(12938977706896313891), KQU(14043628638299793407),\n\tKQU(18393874581723718233), KQU( 6886318534846892044),\n\tKQU(14577870878038334081), KQU(13541558383439414119),\n\tKQU(13570472158807588273), KQU(18300760537910283361),\n\tKQU(  818368572800609205), KQU( 1417000585112573219),\n\tKQU(12337533143867683655), KQU(12433180994702314480),\n\tKQU(  778190005829189083), KQU(13667356216206524711),\n\tKQU( 9866149895295225230), KQU(11043240490417111999),\n\tKQU( 1123933826541378598), KQU( 6469631933605123610),\n\tKQU(14508554074431980040), KQU(13918931242962026714),\n\tKQU( 2870785929342348285), KQU(14786362626740736974),\n\tKQU(13176680060902695786), KQU( 9591778613541679456),\n\tKQU( 9097662885117436706), KQU(  749262234240924947),\n\tKQU( 1944844067793307093), KQU( 4339214904577487742),\n\tKQU( 8009584152961946551), KQU(16073159501225501777),\n\tKQU( 3335870590499306217), KQU(17088312653151202847),\n\tKQU( 3108893142681931848), KQU(16636841767202792021),\n\tKQU(10423316431118400637), KQU( 8008357368674443506),\n\tKQU(11340015231914677875), KQU(17687896501594936090),\n\tKQU(15173627921763199958), KQU(  542569482243721959),\n\tKQU(15071714982769812975), KQU( 4466624872151386956),\n\tKQU( 1901780715602332461), KQU( 9822227742154351098),\n\tKQU( 1479332892928648780), KQU( 6981611948382474400),\n\tKQU( 7620824924456077376), KQU(14095973329429406782),\n\tKQU( 7902744005696185404), KQU(15830577219375036920),\n\tKQU(10287076667317764416), KQU(12334872764071724025),\n\tKQU( 4419302088133544331), KQU(14455842851266090520),\n\tKQU(12488077416504654222), KQU( 7953892017701886766),\n\tKQU( 6331484925529519007), KQU( 4902145853785030022),\n\tKQU(17010159216096443073), KQU(11945354668653886087),\n\tKQU(15112022728645230829), KQU(17363484484522986742),\n\tKQU( 4423497825896692887), KQU( 8155489510809067471),\n\tKQU(  258966605622576285), KQU( 5462958075742020534),\n\tKQU( 6763710214913276228), KQU( 2368935183451109054),\n\tKQU(14209506165246453811), KQU( 2646257040978514881),\n\tKQU( 3776001911922207672), KQU( 1419304601390147631),\n\tKQU(14987366598022458284), KQU( 3977770701065815721),\n\tKQU(  730820417451838898), KQU( 3982991703612885327),\n\tKQU( 2803544519671388477), KQU(17067667221114424649),\n\tKQU( 2922555119737867166), KQU( 1989477584121460932),\n\tKQU(15020387605892337354), KQU( 9293277796427533547),\n\tKQU(10722181424063557247), KQU(16704542332047511651),\n\tKQU( 5008286236142089514), KQU(16174732308747382540),\n\tKQU(17597019485798338402), KQU(13081745199110622093),\n\tKQU( 8850305883842258115), KQU(12723629125624589005),\n\tKQU( 8140566453402805978), KQU(15356684607680935061),\n\tKQU(14222190387342648650), KQU(11134610460665975178),\n\tKQU( 1259799058620984266), KQU(13281656268025610041),\n\tKQU(  298262561068153992), KQU(12277871700239212922),\n\tKQU(13911297774719779438), KQU(16556727962761474934),\n\tKQU(17903010316654728010), KQU( 9682617699648434744),\n\tKQU(14757681836838592850), KQU( 1327242446558524473),\n\tKQU(11126645098780572792), KQU( 1883602329313221774),\n\tKQU( 2543897783922776873), KQU(15029168513767772842),\n\tKQU(12710270651039129878), KQU(16118202956069604504),\n\tKQU(15010759372168680524), KQU( 2296827082251923948),\n\tKQU(10793729742623518101), KQU(13829764151845413046),\n\tKQU(17769301223184451213), KQU( 3118268169210783372),\n\tKQU(17626204544105123127), KQU( 7416718488974352644),\n\tKQU(10450751996212925994), KQU( 9352529519128770586),\n\tKQU(  259347569641110140), KQU( 8048588892269692697),\n\tKQU( 1774414152306494058), KQU(10669548347214355622),\n\tKQU(13061992253816795081), KQU(18432677803063861659),\n\tKQU( 8879191055593984333), KQU(12433753195199268041),\n\tKQU(14919392415439730602), KQU( 6612848378595332963),\n\tKQU( 6320986812036143628), KQU(10465592420226092859),\n\tKQU( 4196009278962570808), KQU( 3747816564473572224),\n\tKQU(17941203486133732898), KQU( 2350310037040505198),\n\tKQU( 5811779859134370113), KQU(10492109599506195126),\n\tKQU( 7699650690179541274), KQU( 1954338494306022961),\n\tKQU(14095816969027231152), KQU( 5841346919964852061),\n\tKQU(14945969510148214735), KQU( 3680200305887550992),\n\tKQU( 6218047466131695792), KQU( 8242165745175775096),\n\tKQU(11021371934053307357), KQU( 1265099502753169797),\n\tKQU( 4644347436111321718), KQU( 3609296916782832859),\n\tKQU( 8109807992218521571), KQU(18387884215648662020),\n\tKQU(14656324896296392902), KQU(17386819091238216751),\n\tKQU(17788300878582317152), KQU( 7919446259742399591),\n\tKQU( 4466613134576358004), KQU(12928181023667938509),\n\tKQU(13147446154454932030), KQU(16552129038252734620),\n\tKQU( 8395299403738822450), KQU(11313817655275361164),\n\tKQU(  434258809499511718), KQU( 2074882104954788676),\n\tKQU( 7929892178759395518), KQU( 9006461629105745388),\n\tKQU( 5176475650000323086), KQU(11128357033468341069),\n\tKQU(12026158851559118955), KQU(14699716249471156500),\n\tKQU(  448982497120206757), KQU( 4156475356685519900),\n\tKQU( 6063816103417215727), KQU(10073289387954971479),\n\tKQU( 8174466846138590962), KQU( 2675777452363449006),\n\tKQU( 9090685420572474281), KQU( 6659652652765562060),\n\tKQU(12923120304018106621), KQU(11117480560334526775),\n\tKQU(  937910473424587511), KQU( 1838692113502346645),\n\tKQU(11133914074648726180), KQU( 7922600945143884053),\n\tKQU(13435287702700959550), KQU( 5287964921251123332),\n\tKQU(11354875374575318947), KQU(17955724760748238133),\n\tKQU(13728617396297106512), KQU( 4107449660118101255),\n\tKQU( 1210269794886589623), KQU(11408687205733456282),\n\tKQU( 4538354710392677887), KQU(13566803319341319267),\n\tKQU(17870798107734050771), KQU( 3354318982568089135),\n\tKQU( 9034450839405133651), KQU(13087431795753424314),\n\tKQU(  950333102820688239), KQU( 1968360654535604116),\n\tKQU(16840551645563314995), KQU( 8867501803892924995),\n\tKQU(11395388644490626845), KQU( 1529815836300732204),\n\tKQU(13330848522996608842), KQU( 1813432878817504265),\n\tKQU( 2336867432693429560), KQU(15192805445973385902),\n\tKQU( 2528593071076407877), KQU(  128459777936689248),\n\tKQU( 9976345382867214866), KQU( 6208885766767996043),\n\tKQU(14982349522273141706), KQU( 3099654362410737822),\n\tKQU(13776700761947297661), KQU( 8806185470684925550),\n\tKQU( 8151717890410585321), KQU(  640860591588072925),\n\tKQU(14592096303937307465), KQU( 9056472419613564846),\n\tKQU(14861544647742266352), KQU(12703771500398470216),\n\tKQU( 3142372800384138465), KQU( 6201105606917248196),\n\tKQU(18337516409359270184), KQU(15042268695665115339),\n\tKQU(15188246541383283846), KQU(12800028693090114519),\n\tKQU( 5992859621101493472), KQU(18278043971816803521),\n\tKQU( 9002773075219424560), KQU( 7325707116943598353),\n\tKQU( 7930571931248040822), KQU( 5645275869617023448),\n\tKQU( 7266107455295958487), KQU( 4363664528273524411),\n\tKQU(14313875763787479809), KQU(17059695613553486802),\n\tKQU( 9247761425889940932), KQU(13704726459237593128),\n\tKQU( 2701312427328909832), KQU(17235532008287243115),\n\tKQU(14093147761491729538), KQU( 6247352273768386516),\n\tKQU( 8268710048153268415), KQU( 7985295214477182083),\n\tKQU(15624495190888896807), KQU( 3772753430045262788),\n\tKQU( 9133991620474991698), KQU( 5665791943316256028),\n\tKQU( 7551996832462193473), KQU(13163729206798953877),\n\tKQU( 9263532074153846374), KQU( 1015460703698618353),\n\tKQU(17929874696989519390), KQU(18257884721466153847),\n\tKQU(16271867543011222991), KQU( 3905971519021791941),\n\tKQU(16814488397137052085), KQU( 1321197685504621613),\n\tKQU( 2870359191894002181), KQU(14317282970323395450),\n\tKQU(13663920845511074366), KQU( 2052463995796539594),\n\tKQU(14126345686431444337), KQU( 1727572121947022534),\n\tKQU(17793552254485594241), KQU( 6738857418849205750),\n\tKQU( 1282987123157442952), KQU(16655480021581159251),\n\tKQU( 6784587032080183866), KQU(14726758805359965162),\n\tKQU( 7577995933961987349), KQU(12539609320311114036),\n\tKQU(10789773033385439494), KQU( 8517001497411158227),\n\tKQU(10075543932136339710), KQU(14838152340938811081),\n\tKQU( 9560840631794044194), KQU(17445736541454117475),\n\tKQU(10633026464336393186), KQU(15705729708242246293),\n\tKQU( 1117517596891411098), KQU( 4305657943415886942),\n\tKQU( 4948856840533979263), KQU(16071681989041789593),\n\tKQU(13723031429272486527), KQU( 7639567622306509462),\n\tKQU(12670424537483090390), KQU( 9715223453097197134),\n\tKQU( 5457173389992686394), KQU(  289857129276135145),\n\tKQU(17048610270521972512), KQU(  692768013309835485),\n\tKQU(14823232360546632057), KQU(18218002361317895936),\n\tKQU( 3281724260212650204), KQU(16453957266549513795),\n\tKQU( 8592711109774511881), KQU(  929825123473369579),\n\tKQU(15966784769764367791), KQU( 9627344291450607588),\n\tKQU(10849555504977813287), KQU( 9234566913936339275),\n\tKQU( 6413807690366911210), KQU(10862389016184219267),\n\tKQU(13842504799335374048), KQU( 1531994113376881174),\n\tKQU( 2081314867544364459), KQU(16430628791616959932),\n\tKQU( 8314714038654394368), KQU( 9155473892098431813),\n\tKQU(12577843786670475704), KQU( 4399161106452401017),\n\tKQU( 1668083091682623186), KQU( 1741383777203714216),\n\tKQU( 2162597285417794374), KQU(15841980159165218736),\n\tKQU( 1971354603551467079), KQU( 1206714764913205968),\n\tKQU( 4790860439591272330), KQU(14699375615594055799),\n\tKQU( 8374423871657449988), KQU(10950685736472937738),\n\tKQU(  697344331343267176), KQU(10084998763118059810),\n\tKQU(12897369539795983124), KQU(12351260292144383605),\n\tKQU( 1268810970176811234), KQU( 7406287800414582768),\n\tKQU(  516169557043807831), KQU( 5077568278710520380),\n\tKQU( 3828791738309039304), KQU( 7721974069946943610),\n\tKQU( 3534670260981096460), KQU( 4865792189600584891),\n\tKQU(16892578493734337298), KQU( 9161499464278042590),\n\tKQU(11976149624067055931), KQU(13219479887277343990),\n\tKQU(14161556738111500680), KQU(14670715255011223056),\n\tKQU( 4671205678403576558), KQU(12633022931454259781),\n\tKQU(14821376219869187646), KQU(  751181776484317028),\n\tKQU( 2192211308839047070), KQU(11787306362361245189),\n\tKQU(10672375120744095707), KQU( 4601972328345244467),\n\tKQU(15457217788831125879), KQU( 8464345256775460809),\n\tKQU(10191938789487159478), KQU( 6184348739615197613),\n\tKQU(11425436778806882100), KQU( 2739227089124319793),\n\tKQU(  461464518456000551), KQU( 4689850170029177442),\n\tKQU( 6120307814374078625), KQU(11153579230681708671),\n\tKQU( 7891721473905347926), KQU(10281646937824872400),\n\tKQU( 3026099648191332248), KQU( 8666750296953273818),\n\tKQU(14978499698844363232), KQU(13303395102890132065),\n\tKQU( 8182358205292864080), KQU(10560547713972971291),\n\tKQU(11981635489418959093), KQU( 3134621354935288409),\n\tKQU(11580681977404383968), KQU(14205530317404088650),\n\tKQU( 5997789011854923157), KQU(13659151593432238041),\n\tKQU(11664332114338865086), KQU( 7490351383220929386),\n\tKQU( 7189290499881530378), KQU(15039262734271020220),\n\tKQU( 2057217285976980055), KQU(  555570804905355739),\n\tKQU(11235311968348555110), KQU(13824557146269603217),\n\tKQU(16906788840653099693), KQU( 7222878245455661677),\n\tKQU( 5245139444332423756), KQU( 4723748462805674292),\n\tKQU(12216509815698568612), KQU(17402362976648951187),\n\tKQU(17389614836810366768), KQU( 4880936484146667711),\n\tKQU( 9085007839292639880), KQU(13837353458498535449),\n\tKQU(11914419854360366677), KQU(16595890135313864103),\n\tKQU( 6313969847197627222), KQU(18296909792163910431),\n\tKQU(10041780113382084042), KQU( 2499478551172884794),\n\tKQU(11057894246241189489), KQU( 9742243032389068555),\n\tKQU(12838934582673196228), KQU(13437023235248490367),\n\tKQU(13372420669446163240), KQU( 6752564244716909224),\n\tKQU( 7157333073400313737), KQU(12230281516370654308),\n\tKQU( 1182884552219419117), KQU( 2955125381312499218),\n\tKQU(10308827097079443249), KQU( 1337648572986534958),\n\tKQU(16378788590020343939), KQU(  108619126514420935),\n\tKQU( 3990981009621629188), KQU( 5460953070230946410),\n\tKQU( 9703328329366531883), KQU(13166631489188077236),\n\tKQU( 1104768831213675170), KQU( 3447930458553877908),\n\tKQU( 8067172487769945676), KQU( 5445802098190775347),\n\tKQU( 3244840981648973873), KQU(17314668322981950060),\n\tKQU( 5006812527827763807), KQU(18158695070225526260),\n\tKQU( 2824536478852417853), KQU(13974775809127519886),\n\tKQU( 9814362769074067392), KQU(17276205156374862128),\n\tKQU(11361680725379306967), KQU( 3422581970382012542),\n\tKQU(11003189603753241266), KQU(11194292945277862261),\n\tKQU( 6839623313908521348), KQU(11935326462707324634),\n\tKQU( 1611456788685878444), KQU(13112620989475558907),\n\tKQU(  517659108904450427), KQU(13558114318574407624),\n\tKQU(15699089742731633077), KQU( 4988979278862685458),\n\tKQU( 8111373583056521297), KQU( 3891258746615399627),\n\tKQU( 8137298251469718086), KQU(12748663295624701649),\n\tKQU( 4389835683495292062), KQU( 5775217872128831729),\n\tKQU( 9462091896405534927), KQU( 8498124108820263989),\n\tKQU( 8059131278842839525), KQU(10503167994254090892),\n\tKQU(11613153541070396656), KQU(18069248738504647790),\n\tKQU(  570657419109768508), KQU( 3950574167771159665),\n\tKQU( 5514655599604313077), KQU( 2908460854428484165),\n\tKQU(10777722615935663114), KQU(12007363304839279486),\n\tKQU( 9800646187569484767), KQU( 8795423564889864287),\n\tKQU(14257396680131028419), KQU( 6405465117315096498),\n\tKQU( 7939411072208774878), KQU(17577572378528990006),\n\tKQU(14785873806715994850), KQU(16770572680854747390),\n\tKQU(18127549474419396481), KQU(11637013449455757750),\n\tKQU(14371851933996761086), KQU( 3601181063650110280),\n\tKQU( 4126442845019316144), KQU(10198287239244320669),\n\tKQU(18000169628555379659), KQU(18392482400739978269),\n\tKQU( 6219919037686919957), KQU( 3610085377719446052),\n\tKQU( 2513925039981776336), KQU(16679413537926716955),\n\tKQU(12903302131714909434), KQU( 5581145789762985009),\n\tKQU(12325955044293303233), KQU(17216111180742141204),\n\tKQU( 6321919595276545740), KQU( 3507521147216174501),\n\tKQU( 9659194593319481840), KQU(11473976005975358326),\n\tKQU(14742730101435987026), KQU(  492845897709954780),\n\tKQU(16976371186162599676), KQU(17712703422837648655),\n\tKQU( 9881254778587061697), KQU( 8413223156302299551),\n\tKQU( 1563841828254089168), KQU( 9996032758786671975),\n\tKQU(  138877700583772667), KQU(13003043368574995989),\n\tKQU( 4390573668650456587), KQU( 8610287390568126755),\n\tKQU(15126904974266642199), KQU( 6703637238986057662),\n\tKQU( 2873075592956810157), KQU( 6035080933946049418),\n\tKQU(13382846581202353014), KQU( 7303971031814642463),\n\tKQU(18418024405307444267), KQU( 5847096731675404647),\n\tKQU( 4035880699639842500), KQU(11525348625112218478),\n\tKQU( 3041162365459574102), KQU( 2604734487727986558),\n\tKQU(15526341771636983145), KQU(14556052310697370254),\n\tKQU(12997787077930808155), KQU( 9601806501755554499),\n\tKQU(11349677952521423389), KQU(14956777807644899350),\n\tKQU(16559736957742852721), KQU(12360828274778140726),\n\tKQU( 6685373272009662513), KQU(16932258748055324130),\n\tKQU(15918051131954158508), KQU( 1692312913140790144),\n\tKQU(  546653826801637367), KQU( 5341587076045986652),\n\tKQU(14975057236342585662), KQU(12374976357340622412),\n\tKQU(10328833995181940552), KQU(12831807101710443149),\n\tKQU(10548514914382545716), KQU( 2217806727199715993),\n\tKQU(12627067369242845138), KQU( 4598965364035438158),\n\tKQU(  150923352751318171), KQU(14274109544442257283),\n\tKQU( 4696661475093863031), KQU( 1505764114384654516),\n\tKQU(10699185831891495147), KQU( 2392353847713620519),\n\tKQU( 3652870166711788383), KQU( 8640653276221911108),\n\tKQU( 3894077592275889704), KQU( 4918592872135964845),\n\tKQU(16379121273281400789), KQU(12058465483591683656),\n\tKQU(11250106829302924945), KQU( 1147537556296983005),\n\tKQU( 6376342756004613268), KQU(14967128191709280506),\n\tKQU(18007449949790627628), KQU( 9497178279316537841),\n\tKQU( 7920174844809394893), KQU(10037752595255719907),\n\tKQU(15875342784985217697), KQU(15311615921712850696),\n\tKQU( 9552902652110992950), KQU(14054979450099721140),\n\tKQU( 5998709773566417349), KQU(18027910339276320187),\n\tKQU( 8223099053868585554), KQU( 7842270354824999767),\n\tKQU( 4896315688770080292), KQU(12969320296569787895),\n\tKQU( 2674321489185759961), KQU( 4053615936864718439),\n\tKQU(11349775270588617578), KQU( 4743019256284553975),\n\tKQU( 5602100217469723769), KQU(14398995691411527813),\n\tKQU( 7412170493796825470), KQU(  836262406131744846),\n\tKQU( 8231086633845153022), KQU( 5161377920438552287),\n\tKQU( 8828731196169924949), KQU(16211142246465502680),\n\tKQU( 3307990879253687818), KQU( 5193405406899782022),\n\tKQU( 8510842117467566693), KQU( 6070955181022405365),\n\tKQU(14482950231361409799), KQU(12585159371331138077),\n\tKQU( 3511537678933588148), KQU( 2041849474531116417),\n\tKQU(10944936685095345792), KQU(18303116923079107729),\n\tKQU( 2720566371239725320), KQU( 4958672473562397622),\n\tKQU( 3032326668253243412), KQU(13689418691726908338),\n\tKQU( 1895205511728843996), KQU( 8146303515271990527),\n\tKQU(16507343500056113480), KQU(  473996939105902919),\n\tKQU( 9897686885246881481), KQU(14606433762712790575),\n\tKQU( 6732796251605566368), KQU( 1399778120855368916),\n\tKQU(  935023885182833777), KQU(16066282816186753477),\n\tKQU( 7291270991820612055), KQU(17530230393129853844),\n\tKQU(10223493623477451366), KQU(15841725630495676683),\n\tKQU(17379567246435515824), KQU( 8588251429375561971),\n\tKQU(18339511210887206423), KQU(17349587430725976100),\n\tKQU(12244876521394838088), KQU( 6382187714147161259),\n\tKQU(12335807181848950831), KQU(16948885622305460665),\n\tKQU(13755097796371520506), KQU(14806740373324947801),\n\tKQU( 4828699633859287703), KQU( 8209879281452301604),\n\tKQU(12435716669553736437), KQU(13970976859588452131),\n\tKQU( 6233960842566773148), KQU(12507096267900505759),\n\tKQU( 1198713114381279421), KQU(14989862731124149015),\n\tKQU(15932189508707978949), KQU( 2526406641432708722),\n\tKQU(   29187427817271982), KQU( 1499802773054556353),\n\tKQU(10816638187021897173), KQU( 5436139270839738132),\n\tKQU( 6659882287036010082), KQU( 2154048955317173697),\n\tKQU(10887317019333757642), KQU(16281091802634424955),\n\tKQU(10754549879915384901), KQU(10760611745769249815),\n\tKQU( 2161505946972504002), KQU( 5243132808986265107),\n\tKQU(10129852179873415416), KQU(  710339480008649081),\n\tKQU( 7802129453068808528), KQU(17967213567178907213),\n\tKQU(15730859124668605599), KQU(13058356168962376502),\n\tKQU( 3701224985413645909), KQU(14464065869149109264),\n\tKQU( 9959272418844311646), KQU(10157426099515958752),\n\tKQU(14013736814538268528), KQU(17797456992065653951),\n\tKQU(17418878140257344806), KQU(15457429073540561521),\n\tKQU( 2184426881360949378), KQU( 2062193041154712416),\n\tKQU( 8553463347406931661), KQU( 4913057625202871854),\n\tKQU( 2668943682126618425), KQU(17064444737891172288),\n\tKQU( 4997115903913298637), KQU(12019402608892327416),\n\tKQU(17603584559765897352), KQU(11367529582073647975),\n\tKQU( 8211476043518436050), KQU( 8676849804070323674),\n\tKQU(18431829230394475730), KQU(10490177861361247904),\n\tKQU( 9508720602025651349), KQU( 7409627448555722700),\n\tKQU( 5804047018862729008), KQU(11943858176893142594),\n\tKQU(11908095418933847092), KQU( 5415449345715887652),\n\tKQU( 1554022699166156407), KQU( 9073322106406017161),\n\tKQU( 7080630967969047082), KQU(18049736940860732943),\n\tKQU(12748714242594196794), KQU( 1226992415735156741),\n\tKQU(17900981019609531193), KQU(11720739744008710999),\n\tKQU( 3006400683394775434), KQU(11347974011751996028),\n\tKQU( 3316999628257954608), KQU( 8384484563557639101),\n\tKQU(18117794685961729767), KQU( 1900145025596618194),\n\tKQU(17459527840632892676), KQU( 5634784101865710994),\n\tKQU( 7918619300292897158), KQU( 3146577625026301350),\n\tKQU( 9955212856499068767), KQU( 1873995843681746975),\n\tKQU( 1561487759967972194), KQU( 8322718804375878474),\n\tKQU(11300284215327028366), KQU( 4667391032508998982),\n\tKQU( 9820104494306625580), KQU(17922397968599970610),\n\tKQU( 1784690461886786712), KQU(14940365084341346821),\n\tKQU( 5348719575594186181), KQU(10720419084507855261),\n\tKQU(14210394354145143274), KQU( 2426468692164000131),\n\tKQU(16271062114607059202), KQU(14851904092357070247),\n\tKQU( 6524493015693121897), KQU( 9825473835127138531),\n\tKQU(14222500616268569578), KQU(15521484052007487468),\n\tKQU(14462579404124614699), KQU(11012375590820665520),\n\tKQU(11625327350536084927), KQU(14452017765243785417),\n\tKQU( 9989342263518766305), KQU( 3640105471101803790),\n\tKQU( 4749866455897513242), KQU(13963064946736312044),\n\tKQU(10007416591973223791), KQU(18314132234717431115),\n\tKQU( 3286596588617483450), KQU( 7726163455370818765),\n\tKQU( 7575454721115379328), KQU( 5308331576437663422),\n\tKQU(18288821894903530934), KQU( 8028405805410554106),\n\tKQU(15744019832103296628), KQU(  149765559630932100),\n\tKQU( 6137705557200071977), KQU(14513416315434803615),\n\tKQU(11665702820128984473), KQU(  218926670505601386),\n\tKQU( 6868675028717769519), KQU(15282016569441512302),\n\tKQU( 5707000497782960236), KQU( 6671120586555079567),\n\tKQU( 2194098052618985448), KQU(16849577895477330978),\n\tKQU(12957148471017466283), KQU( 1997805535404859393),\n\tKQU( 1180721060263860490), KQU(13206391310193756958),\n\tKQU(12980208674461861797), KQU( 3825967775058875366),\n\tKQU(17543433670782042631), KQU( 1518339070120322730),\n\tKQU(16344584340890991669), KQU( 2611327165318529819),\n\tKQU(11265022723283422529), KQU( 4001552800373196817),\n\tKQU(14509595890079346161), KQU( 3528717165416234562),\n\tKQU(18153222571501914072), KQU( 9387182977209744425),\n\tKQU(10064342315985580021), KQU(11373678413215253977),\n\tKQU( 2308457853228798099), KQU( 9729042942839545302),\n\tKQU( 7833785471140127746), KQU( 6351049900319844436),\n\tKQU(14454610627133496067), KQU(12533175683634819111),\n\tKQU(15570163926716513029), KQU(13356980519185762498)\n};\n\nTEST_BEGIN(test_gen_rand_32) {\n\tuint32_t array32[BLOCK_SIZE] JEMALLOC_ATTR(aligned(16));\n\tuint32_t array32_2[BLOCK_SIZE] JEMALLOC_ATTR(aligned(16));\n\tint i;\n\tuint32_t r32;\n\tsfmt_t *ctx;\n\n\tassert_d_le(get_min_array_size32(), BLOCK_SIZE,\n\t    \"Array size too small\");\n\tctx = init_gen_rand(1234);\n\tfill_array32(ctx, array32, BLOCK_SIZE);\n\tfill_array32(ctx, array32_2, BLOCK_SIZE);\n\tfini_gen_rand(ctx);\n\n\tctx = init_gen_rand(1234);\n\tfor (i = 0; i < BLOCK_SIZE; i++) {\n\t\tif (i < COUNT_1) {\n\t\t\tassert_u32_eq(array32[i], init_gen_rand_32_expected[i],\n\t\t\t    \"Output mismatch for i=%d\", i);\n\t\t}\n\t\tr32 = gen_rand32(ctx);\n\t\tassert_u32_eq(r32, array32[i],\n\t\t    \"Mismatch at array32[%d]=%x, gen=%x\", i, array32[i], r32);\n\t}\n\tfor (i = 0; i < COUNT_2; i++) {\n\t\tr32 = gen_rand32(ctx);\n\t\tassert_u32_eq(r32, array32_2[i],\n\t\t    \"Mismatch at array32_2[%d]=%x, gen=%x\", i, array32_2[i],\n\t\t    r32);\n\t}\n\tfini_gen_rand(ctx);\n}\nTEST_END\n\nTEST_BEGIN(test_by_array_32) {\n\tuint32_t array32[BLOCK_SIZE] JEMALLOC_ATTR(aligned(16));\n\tuint32_t array32_2[BLOCK_SIZE] JEMALLOC_ATTR(aligned(16));\n\tint i;\n\tuint32_t ini[4] = {0x1234, 0x5678, 0x9abc, 0xdef0};\n\tuint32_t r32;\n\tsfmt_t *ctx;\n\n\tassert_d_le(get_min_array_size32(), BLOCK_SIZE,\n\t    \"Array size too small\");\n\tctx = init_by_array(ini, 4);\n\tfill_array32(ctx, array32, BLOCK_SIZE);\n\tfill_array32(ctx, array32_2, BLOCK_SIZE);\n\tfini_gen_rand(ctx);\n\n\tctx = init_by_array(ini, 4);\n\tfor (i = 0; i < BLOCK_SIZE; i++) {\n\t\tif (i < COUNT_1) {\n\t\t\tassert_u32_eq(array32[i], init_by_array_32_expected[i],\n\t\t\t    \"Output mismatch for i=%d\", i);\n\t\t}\n\t\tr32 = gen_rand32(ctx);\n\t\tassert_u32_eq(r32, array32[i],\n\t\t    \"Mismatch at array32[%d]=%x, gen=%x\", i, array32[i], r32);\n\t}\n\tfor (i = 0; i < COUNT_2; i++) {\n\t\tr32 = gen_rand32(ctx);\n\t\tassert_u32_eq(r32, array32_2[i],\n\t\t    \"Mismatch at array32_2[%d]=%x, gen=%x\", i, array32_2[i],\n\t\t    r32);\n\t}\n\tfini_gen_rand(ctx);\n}\nTEST_END\n\nTEST_BEGIN(test_gen_rand_64) {\n\tuint64_t array64[BLOCK_SIZE64] JEMALLOC_ATTR(aligned(16));\n\tuint64_t array64_2[BLOCK_SIZE64] JEMALLOC_ATTR(aligned(16));\n\tint i;\n\tuint64_t r;\n\tsfmt_t *ctx;\n\n\tassert_d_le(get_min_array_size64(), BLOCK_SIZE64,\n\t    \"Array size too small\");\n\tctx = init_gen_rand(4321);\n\tfill_array64(ctx, array64, BLOCK_SIZE64);\n\tfill_array64(ctx, array64_2, BLOCK_SIZE64);\n\tfini_gen_rand(ctx);\n\n\tctx = init_gen_rand(4321);\n\tfor (i = 0; i < BLOCK_SIZE64; i++) {\n\t\tif (i < COUNT_1) {\n\t\t\tassert_u64_eq(array64[i], init_gen_rand_64_expected[i],\n\t\t\t    \"Output mismatch for i=%d\", i);\n\t\t}\n\t\tr = gen_rand64(ctx);\n\t\tassert_u64_eq(r, array64[i],\n\t\t    \"Mismatch at array64[%d]=%\"FMTx64\", gen=%\"FMTx64, i,\n\t\t    array64[i], r);\n\t}\n\tfor (i = 0; i < COUNT_2; i++) {\n\t\tr = gen_rand64(ctx);\n\t\tassert_u64_eq(r, array64_2[i],\n\t\t    \"Mismatch at array64_2[%d]=%\"FMTx64\" gen=%\"FMTx64\"\", i,\n\t\t    array64_2[i], r);\n\t}\n\tfini_gen_rand(ctx);\n}\nTEST_END\n\nTEST_BEGIN(test_by_array_64) {\n\tuint64_t array64[BLOCK_SIZE64] JEMALLOC_ATTR(aligned(16));\n\tuint64_t array64_2[BLOCK_SIZE64] JEMALLOC_ATTR(aligned(16));\n\tint i;\n\tuint64_t r;\n\tuint32_t ini[] = {5, 4, 3, 2, 1};\n\tsfmt_t *ctx;\n\n\tassert_d_le(get_min_array_size64(), BLOCK_SIZE64,\n\t    \"Array size too small\");\n\tctx = init_by_array(ini, 5);\n\tfill_array64(ctx, array64, BLOCK_SIZE64);\n\tfill_array64(ctx, array64_2, BLOCK_SIZE64);\n\tfini_gen_rand(ctx);\n\n\tctx = init_by_array(ini, 5);\n\tfor (i = 0; i < BLOCK_SIZE64; i++) {\n\t\tif (i < COUNT_1) {\n\t\t\tassert_u64_eq(array64[i], init_by_array_64_expected[i],\n\t\t\t    \"Output mismatch for i=%d\", i);\n\t\t}\n\t\tr = gen_rand64(ctx);\n\t\tassert_u64_eq(r, array64[i],\n\t\t    \"Mismatch at array64[%d]=%\"FMTx64\" gen=%\"FMTx64, i,\n\t\t    array64[i], r);\n\t}\n\tfor (i = 0; i < COUNT_2; i++) {\n\t\tr = gen_rand64(ctx);\n\t\tassert_u64_eq(r, array64_2[i],\n\t\t    \"Mismatch at array64_2[%d]=%\"FMTx64\" gen=%\"FMTx64, i,\n\t\t    array64_2[i], r);\n\t}\n\tfini_gen_rand(ctx);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_gen_rand_32,\n\t    test_by_array_32,\n\t    test_gen_rand_64,\n\t    test_by_array_64);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/a0.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nTEST_BEGIN(test_a0) {\n\tvoid *p;\n\n\tp = a0malloc(1);\n\tassert_ptr_not_null(p, \"Unexpected a0malloc() error\");\n\ta0dalloc(p);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test_no_malloc_init(\n\t    test_a0);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/arena_reset.c",
    "content": "#ifndef ARENA_RESET_PROF_C_\n#include \"test/jemalloc_test.h\"\n#endif\n\n#include \"jemalloc/internal/extent_mmap.h\"\n#include \"jemalloc/internal/rtree.h\"\n\n#include \"test/extent_hooks.h\"\n\nstatic unsigned\nget_nsizes_impl(const char *cmd) {\n\tunsigned ret;\n\tsize_t z;\n\n\tz = sizeof(unsigned);\n\tassert_d_eq(mallctl(cmd, (void *)&ret, &z, NULL, 0), 0,\n\t    \"Unexpected mallctl(\\\"%s\\\", ...) failure\", cmd);\n\n\treturn ret;\n}\n\nstatic unsigned\nget_nsmall(void) {\n\treturn get_nsizes_impl(\"arenas.nbins\");\n}\n\nstatic unsigned\nget_nlarge(void) {\n\treturn get_nsizes_impl(\"arenas.nlextents\");\n}\n\nstatic size_t\nget_size_impl(const char *cmd, size_t ind) {\n\tsize_t ret;\n\tsize_t z;\n\tsize_t mib[4];\n\tsize_t miblen = 4;\n\n\tz = sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(cmd, mib, &miblen),\n\t    0, \"Unexpected mallctlnametomib(\\\"%s\\\", ...) failure\", cmd);\n\tmib[2] = ind;\n\tz = sizeof(size_t);\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&ret, &z, NULL, 0),\n\t    0, \"Unexpected mallctlbymib([\\\"%s\\\", %zu], ...) failure\", cmd, ind);\n\n\treturn ret;\n}\n\nstatic size_t\nget_small_size(size_t ind) {\n\treturn get_size_impl(\"arenas.bin.0.size\", ind);\n}\n\nstatic size_t\nget_large_size(size_t ind) {\n\treturn get_size_impl(\"arenas.lextent.0.size\", ind);\n}\n\n/* Like ivsalloc(), but safe to call on discarded allocations. */\nstatic size_t\nvsalloc(tsdn_t *tsdn, const void *ptr) {\n\trtree_ctx_t rtree_ctx_fallback;\n\trtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);\n\n\textent_t *extent;\n\tszind_t szind;\n\tif (rtree_extent_szind_read(tsdn, &extents_rtree, rtree_ctx,\n\t    (uintptr_t)ptr, false, &extent, &szind)) {\n\t\treturn 0;\n\t}\n\n\tif (extent == NULL) {\n\t\treturn 0;\n\t}\n\tif (extent_state_get(extent) != extent_state_active) {\n\t\treturn 0;\n\t}\n\n\tif (szind == NSIZES) {\n\t\treturn 0;\n\t}\n\n\treturn sz_index2size(szind);\n}\n\nstatic unsigned\ndo_arena_create(extent_hooks_t *h) {\n\tunsigned arena_ind;\n\tsize_t sz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena_ind, &sz,\n\t    (void *)(h != NULL ? &h : NULL), (h != NULL ? sizeof(h) : 0)), 0,\n\t    \"Unexpected mallctl() failure\");\n\treturn arena_ind;\n}\n\nstatic void\ndo_arena_reset_pre(unsigned arena_ind, void ***ptrs, unsigned *nptrs) {\n#define NLARGE\t32\n\tunsigned nsmall, nlarge, i;\n\tsize_t sz;\n\tint flags;\n\ttsdn_t *tsdn;\n\n\tflags = MALLOCX_ARENA(arena_ind) | MALLOCX_TCACHE_NONE;\n\n\tnsmall = get_nsmall();\n\tnlarge = get_nlarge() > NLARGE ? NLARGE : get_nlarge();\n\t*nptrs = nsmall + nlarge;\n\t*ptrs = (void **)malloc(*nptrs * sizeof(void *));\n\tassert_ptr_not_null(*ptrs, \"Unexpected malloc() failure\");\n\n\t/* Allocate objects with a wide range of sizes. */\n\tfor (i = 0; i < nsmall; i++) {\n\t\tsz = get_small_size(i);\n\t\t(*ptrs)[i] = mallocx(sz, flags);\n\t\tassert_ptr_not_null((*ptrs)[i],\n\t\t    \"Unexpected mallocx(%zu, %#x) failure\", sz, flags);\n\t}\n\tfor (i = 0; i < nlarge; i++) {\n\t\tsz = get_large_size(i);\n\t\t(*ptrs)[nsmall + i] = mallocx(sz, flags);\n\t\tassert_ptr_not_null((*ptrs)[i],\n\t\t    \"Unexpected mallocx(%zu, %#x) failure\", sz, flags);\n\t}\n\n\ttsdn = tsdn_fetch();\n\n\t/* Verify allocations. */\n\tfor (i = 0; i < *nptrs; i++) {\n\t\tassert_zu_gt(ivsalloc(tsdn, (*ptrs)[i]), 0,\n\t\t    \"Allocation should have queryable size\");\n\t}\n}\n\nstatic void\ndo_arena_reset_post(void **ptrs, unsigned nptrs, unsigned arena_ind) {\n\ttsdn_t *tsdn;\n\tunsigned i;\n\n\ttsdn = tsdn_fetch();\n\n\tif (have_background_thread) {\n\t\tmalloc_mutex_lock(tsdn,\n\t\t    &background_thread_info[arena_ind % ncpus].mtx);\n\t}\n\t/* Verify allocations no longer exist. */\n\tfor (i = 0; i < nptrs; i++) {\n\t\tassert_zu_eq(vsalloc(tsdn, ptrs[i]), 0,\n\t\t    \"Allocation should no longer exist\");\n\t}\n\tif (have_background_thread) {\n\t\tmalloc_mutex_unlock(tsdn,\n\t\t    &background_thread_info[arena_ind % ncpus].mtx);\n\t}\n\n\tfree(ptrs);\n}\n\nstatic void\ndo_arena_reset_destroy(const char *name, unsigned arena_ind) {\n\tsize_t mib[3];\n\tsize_t miblen;\n\n\tmiblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(name, mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tmib[1] = (size_t)arena_ind;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n}\n\nstatic void\ndo_arena_reset(unsigned arena_ind) {\n\tdo_arena_reset_destroy(\"arena.0.reset\", arena_ind);\n}\n\nstatic void\ndo_arena_destroy(unsigned arena_ind) {\n\tdo_arena_reset_destroy(\"arena.0.destroy\", arena_ind);\n}\n\nTEST_BEGIN(test_arena_reset) {\n\tunsigned arena_ind;\n\tvoid **ptrs;\n\tunsigned nptrs;\n\n\tarena_ind = do_arena_create(NULL);\n\tdo_arena_reset_pre(arena_ind, &ptrs, &nptrs);\n\tdo_arena_reset(arena_ind);\n\tdo_arena_reset_post(ptrs, nptrs, arena_ind);\n}\nTEST_END\n\nstatic bool\narena_i_initialized(unsigned arena_ind, bool refresh) {\n\tbool initialized;\n\tsize_t mib[3];\n\tsize_t miblen, sz;\n\n\tif (refresh) {\n\t\tuint64_t epoch = 1;\n\t\tassert_d_eq(mallctl(\"epoch\", NULL, NULL, (void *)&epoch,\n\t\t    sizeof(epoch)), 0, \"Unexpected mallctl() failure\");\n\t}\n\n\tmiblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"arena.0.initialized\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tmib[1] = (size_t)arena_ind;\n\tsz = sizeof(initialized);\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&initialized, &sz, NULL,\n\t    0), 0, \"Unexpected mallctlbymib() failure\");\n\n\treturn initialized;\n}\n\nTEST_BEGIN(test_arena_destroy_initial) {\n\tassert_false(arena_i_initialized(MALLCTL_ARENAS_DESTROYED, false),\n\t    \"Destroyed arena stats should not be initialized\");\n}\nTEST_END\n\nTEST_BEGIN(test_arena_destroy_hooks_default) {\n\tunsigned arena_ind, arena_ind_another, arena_ind_prev;\n\tvoid **ptrs;\n\tunsigned nptrs;\n\n\tarena_ind = do_arena_create(NULL);\n\tdo_arena_reset_pre(arena_ind, &ptrs, &nptrs);\n\n\tassert_false(arena_i_initialized(arena_ind, false),\n\t    \"Arena stats should not be initialized\");\n\tassert_true(arena_i_initialized(arena_ind, true),\n\t    \"Arena stats should be initialized\");\n\n\t/*\n\t * Create another arena before destroying one, to better verify arena\n\t * index reuse.\n\t */\n\tarena_ind_another = do_arena_create(NULL);\n\n\tdo_arena_destroy(arena_ind);\n\n\tassert_false(arena_i_initialized(arena_ind, true),\n\t    \"Arena stats should not be initialized\");\n\tassert_true(arena_i_initialized(MALLCTL_ARENAS_DESTROYED, false),\n\t    \"Destroyed arena stats should be initialized\");\n\n\tdo_arena_reset_post(ptrs, nptrs, arena_ind);\n\n\tarena_ind_prev = arena_ind;\n\tarena_ind = do_arena_create(NULL);\n\tdo_arena_reset_pre(arena_ind, &ptrs, &nptrs);\n\tassert_u_eq(arena_ind, arena_ind_prev,\n\t    \"Arena index should have been recycled\");\n\tdo_arena_destroy(arena_ind);\n\tdo_arena_reset_post(ptrs, nptrs, arena_ind);\n\n\tdo_arena_destroy(arena_ind_another);\n}\nTEST_END\n\n/*\n * Actually unmap extents, regardless of opt_retain, so that attempts to access\n * a destroyed arena's memory will segfault.\n */\nstatic bool\nextent_dalloc_unmap(extent_hooks_t *extent_hooks, void *addr, size_t size,\n    bool committed, unsigned arena_ind) {\n\tTRACE_HOOK(\"%s(extent_hooks=%p, addr=%p, size=%zu, committed=%s, \"\n\t    \"arena_ind=%u)\\n\", __func__, extent_hooks, addr, size, committed ?\n\t    \"true\" : \"false\", arena_ind);\n\tassert_ptr_eq(extent_hooks, &hooks,\n\t    \"extent_hooks should be same as pointer used to set hooks\");\n\tassert_ptr_eq(extent_hooks->dalloc, extent_dalloc_unmap,\n\t    \"Wrong hook function\");\n\tcalled_dalloc = true;\n\tif (!try_dalloc) {\n\t\treturn true;\n\t}\n\tpages_unmap(addr, size);\n\tdid_dalloc = true;\n\treturn false;\n}\n\nstatic extent_hooks_t hooks_orig;\n\nstatic extent_hooks_t hooks_unmap = {\n\textent_alloc_hook,\n\textent_dalloc_unmap, /* dalloc */\n\textent_destroy_hook,\n\textent_commit_hook,\n\textent_decommit_hook,\n\textent_purge_lazy_hook,\n\textent_purge_forced_hook,\n\textent_split_hook,\n\textent_merge_hook\n};\n\nTEST_BEGIN(test_arena_destroy_hooks_unmap) {\n\tunsigned arena_ind;\n\tvoid **ptrs;\n\tunsigned nptrs;\n\n\textent_hooks_prep();\n\ttry_decommit = false;\n\tmemcpy(&hooks_orig, &hooks, sizeof(extent_hooks_t));\n\tmemcpy(&hooks, &hooks_unmap, sizeof(extent_hooks_t));\n\n\tdid_alloc = false;\n\tarena_ind = do_arena_create(&hooks);\n\tdo_arena_reset_pre(arena_ind, &ptrs, &nptrs);\n\n\tassert_true(did_alloc, \"Expected alloc\");\n\n\tassert_false(arena_i_initialized(arena_ind, false),\n\t    \"Arena stats should not be initialized\");\n\tassert_true(arena_i_initialized(arena_ind, true),\n\t    \"Arena stats should be initialized\");\n\n\tdid_dalloc = false;\n\tdo_arena_destroy(arena_ind);\n\tassert_true(did_dalloc, \"Expected dalloc\");\n\n\tassert_false(arena_i_initialized(arena_ind, true),\n\t    \"Arena stats should not be initialized\");\n\tassert_true(arena_i_initialized(MALLCTL_ARENAS_DESTROYED, false),\n\t    \"Destroyed arena stats should be initialized\");\n\n\tdo_arena_reset_post(ptrs, nptrs, arena_ind);\n\n\tmemcpy(&hooks, &hooks_orig, sizeof(extent_hooks_t));\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_arena_reset,\n\t    test_arena_destroy_initial,\n\t    test_arena_destroy_hooks_default,\n\t    test_arena_destroy_hooks_unmap);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/arena_reset_prof.c",
    "content": "#include \"test/jemalloc_test.h\"\n#define ARENA_RESET_PROF_C_\n\n#include \"arena_reset.c\"\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/arena_reset_prof.sh",
    "content": "#!/bin/sh\n\nexport MALLOC_CONF=\"prof:true,lg_prof_sample:0\"\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/atomic.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n/*\n * We *almost* have consistent short names (e.g. \"u32\" for uint32_t, \"b\" for\n * bool, etc.  The one exception is that the short name for void * is \"p\" in\n * some places and \"ptr\" in others.  In the long run it would be nice to unify\n * these, but in the short run we'll use this shim.\n */\n#define assert_p_eq assert_ptr_eq\n\n/*\n * t: the non-atomic type, like \"uint32_t\".\n * ta: the short name for the type, like \"u32\".\n * val[1,2,3]: Values of the given type.  The CAS tests use val2 for expected,\n * and val3 for desired.\n */\n\n#define DO_TESTS(t, ta, val1, val2, val3) do {\t\t\t\t\\\n\tt val;\t\t\t\t\t\t\t\t\\\n\tt expected;\t\t\t\t\t\t\t\\\n\tbool success;\t\t\t\t\t\t\t\\\n\t/* This (along with the load below) also tests ATOMIC_LOAD. */\t\\\n\tatomic_##ta##_t atom = ATOMIC_INIT(val1);\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t/* ATOMIC_INIT and load. */\t\t\t\t\t\\\n\tval = atomic_load_##ta(&atom, ATOMIC_RELAXED);\t\t\t\\\n\tassert_##ta##_eq(val1, val, \"Load or init failed\");\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t/* Store. */\t\t\t\t\t\t\t\\\n\tatomic_store_##ta(&atom, val1, ATOMIC_RELAXED);\t\t\t\\\n\tatomic_store_##ta(&atom, val2, ATOMIC_RELAXED);\t\t\t\\\n\tval = atomic_load_##ta(&atom, ATOMIC_RELAXED);\t\t\t\\\n\tassert_##ta##_eq(val2, val, \"Store failed\");\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t/* Exchange. */\t\t\t\t\t\t\t\\\n\tatomic_store_##ta(&atom, val1, ATOMIC_RELAXED);\t\t\t\\\n\tval = atomic_exchange_##ta(&atom, val2, ATOMIC_RELAXED);\t\\\n\tassert_##ta##_eq(val1, val, \"Exchange returned invalid value\");\t\\\n\tval = atomic_load_##ta(&atom, ATOMIC_RELAXED);\t\t\t\\\n\tassert_##ta##_eq(val2, val, \"Exchange store invalid value\");\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t/* \t\t\t\t\t\t\t\t\\\n\t * Weak CAS.  Spurious failures are allowed, so we loop a few\t\\\n\t * times.\t\t\t\t\t\t\t\\\n\t */\t\t\t\t\t\t\t\t\\\n\tatomic_store_##ta(&atom, val1, ATOMIC_RELAXED);\t\t\t\\\n\tsuccess = false;\t\t\t\t\t\t\\\n\tfor (int i = 0; i < 10 && !success; i++) {\t\t\t\\\n\t\texpected = val2;\t\t\t\t\t\\\n\t\tsuccess = atomic_compare_exchange_weak_##ta(&atom,\t\\\n\t\t    &expected, val3, ATOMIC_RELAXED, ATOMIC_RELAXED);\t\\\n\t\tassert_##ta##_eq(val1, expected, \t\t\t\\\n\t\t    \"CAS should update expected\");\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\tassert_b_eq(val1 == val2, success,\t\t\t\t\\\n\t    \"Weak CAS did the wrong state update\");\t\t\t\\\n\tval = atomic_load_##ta(&atom, ATOMIC_RELAXED);\t\t\t\\\n\tif (success) {\t\t\t\t\t\t\t\\\n\t\tassert_##ta##_eq(val3, val,\t\t\t\t\\\n\t\t    \"Successful CAS should update atomic\");\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t\tassert_##ta##_eq(val1, val,\t\t\t\t\\\n\t\t    \"Unsuccessful CAS should not update atomic\");\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t/* Strong CAS. */\t\t\t\t\t\t\\\n\tatomic_store_##ta(&atom, val1, ATOMIC_RELAXED);\t\t\t\\\n\texpected = val2;\t\t\t\t\t\t\\\n\tsuccess = atomic_compare_exchange_strong_##ta(&atom, &expected,\t\\\n\t    val3, ATOMIC_RELAXED, ATOMIC_RELAXED);\t\t\t\\\n\tassert_b_eq(val1 == val2, success,\t\t\t\t\\\n\t    \"Strong CAS did the wrong state update\");\t\t\t\\\n\tval = atomic_load_##ta(&atom, ATOMIC_RELAXED);\t\t\t\\\n\tif (success) {\t\t\t\t\t\t\t\\\n\t\tassert_##ta##_eq(val3, val,\t\t\t\t\\\n\t\t    \"Successful CAS should update atomic\");\t\t\\\n\t} else {\t\t\t\t\t\t\t\\\n\t\tassert_##ta##_eq(val1, val,\t\t\t\t\\\n\t\t    \"Unsuccessful CAS should not update atomic\");\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define DO_INTEGER_TESTS(t, ta, val1, val2) do {\t\t\t\\\n\tatomic_##ta##_t atom;\t\t\t\t\t\t\\\n\tt val;\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t/* Fetch-add. */\t\t\t\t\t\t\\\n\tatomic_store_##ta(&atom, val1, ATOMIC_RELAXED);\t\t\t\\\n\tval = atomic_fetch_add_##ta(&atom, val2, ATOMIC_RELAXED);\t\\\n\tassert_##ta##_eq(val1, val,\t\t\t\t\t\\\n\t    \"Fetch-add should return previous value\");\t\t\t\\\n\tval = atomic_load_##ta(&atom, ATOMIC_RELAXED);\t\t\t\\\n\tassert_##ta##_eq(val1 + val2, val,\t\t\t\t\\\n\t    \"Fetch-add should update atomic\");\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t/* Fetch-sub. */\t\t\t\t\t\t\\\n\tatomic_store_##ta(&atom, val1, ATOMIC_RELAXED);\t\t\t\\\n\tval = atomic_fetch_sub_##ta(&atom, val2, ATOMIC_RELAXED);\t\\\n\tassert_##ta##_eq(val1, val,\t\t\t\t\t\\\n\t    \"Fetch-sub should return previous value\");\t\t\t\\\n\tval = atomic_load_##ta(&atom, ATOMIC_RELAXED);\t\t\t\\\n\tassert_##ta##_eq(val1 - val2, val,\t\t\t\t\\\n\t    \"Fetch-sub should update atomic\");\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t/* Fetch-and. */\t\t\t\t\t\t\\\n\tatomic_store_##ta(&atom, val1, ATOMIC_RELAXED);\t\t\t\\\n\tval = atomic_fetch_and_##ta(&atom, val2, ATOMIC_RELAXED);\t\\\n\tassert_##ta##_eq(val1, val,\t\t\t\t\t\\\n\t    \"Fetch-and should return previous value\");\t\t\t\\\n\tval = atomic_load_##ta(&atom, ATOMIC_RELAXED);\t\t\t\\\n\tassert_##ta##_eq(val1 & val2, val,\t\t\t\t\\\n\t    \"Fetch-and should update atomic\");\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t/* Fetch-or. */\t\t\t\t\t\t\t\\\n\tatomic_store_##ta(&atom, val1, ATOMIC_RELAXED);\t\t\t\\\n\tval = atomic_fetch_or_##ta(&atom, val2, ATOMIC_RELAXED);\t\\\n\tassert_##ta##_eq(val1, val,\t\t\t\t\t\\\n\t    \"Fetch-or should return previous value\");\t\t\t\\\n\tval = atomic_load_##ta(&atom, ATOMIC_RELAXED);\t\t\t\\\n\tassert_##ta##_eq(val1 | val2, val,\t\t\t\t\\\n\t    \"Fetch-or should update atomic\");\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t/* Fetch-xor. */\t\t\t\t\t\t\\\n\tatomic_store_##ta(&atom, val1, ATOMIC_RELAXED);\t\t\t\\\n\tval = atomic_fetch_xor_##ta(&atom, val2, ATOMIC_RELAXED);\t\\\n\tassert_##ta##_eq(val1, val,\t\t\t\t\t\\\n\t    \"Fetch-xor should return previous value\");\t\t\t\\\n\tval = atomic_load_##ta(&atom, ATOMIC_RELAXED);\t\t\t\\\n\tassert_##ta##_eq(val1 ^ val2, val,\t\t\t\t\\\n\t    \"Fetch-xor should update atomic\");\t\t\t\t\\\n} while (0)\n\n#define TEST_STRUCT(t, ta)\t\t\t\t\t\t\\\ntypedef struct {\t\t\t\t\t\t\t\\\n\tt val1;\t\t\t\t\t\t\t\t\\\n\tt val2;\t\t\t\t\t\t\t\t\\\n\tt val3;\t\t\t\t\t\t\t\t\\\n} ta##_test_t;\n\n#define TEST_CASES(t) {\t\t\t\t\t\t\t\\\n\t{(t)-1, (t)-1, (t)-2},\t\t\t\t\t\t\\\n\t{(t)-1, (t) 0, (t)-2},\t\t\t\t\t\t\\\n\t{(t)-1, (t) 1, (t)-2},\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t{(t) 0, (t)-1, (t)-2},\t\t\t\t\t\t\\\n\t{(t) 0, (t) 0, (t)-2},\t\t\t\t\t\t\\\n\t{(t) 0, (t) 1, (t)-2},\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t{(t) 1, (t)-1, (t)-2},\t\t\t\t\t\t\\\n\t{(t) 1, (t) 0, (t)-2},\t\t\t\t\t\t\\\n\t{(t) 1, (t) 1, (t)-2},\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\t{(t)0, (t)-(1 << 22), (t)-2},\t\t\t\t\t\\\n\t{(t)0, (t)(1 << 22), (t)-2},\t\t\t\t\t\\\n\t{(t)(1 << 22), (t)-(1 << 22), (t)-2},\t\t\t\t\\\n\t{(t)(1 << 22), (t)(1 << 22), (t)-2}\t\t\t\t\\\n}\n\n#define TEST_BODY(t, ta) do {\t\t\t\t\t\t\\\n\tconst ta##_test_t tests[] = TEST_CASES(t);\t\t\t\\\n\tfor (unsigned i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {\t\\\n\t\tta##_test_t test = tests[i];\t\t\t\t\\\n\t\tDO_TESTS(t, ta, test.val1, test.val2, test.val3);\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\n#define INTEGER_TEST_BODY(t, ta) do {\t\t\t\t\t\\\n\tconst ta##_test_t tests[] = TEST_CASES(t);\t\t\t\\\n\tfor (unsigned i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {\t\\\n\t\tta##_test_t test = tests[i];\t\t\t\t\\\n\t\tDO_TESTS(t, ta, test.val1, test.val2, test.val3);\t\\\n\t\tDO_INTEGER_TESTS(t, ta, test.val1, test.val2);\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\nTEST_STRUCT(uint64_t, u64);\nTEST_BEGIN(test_atomic_u64) {\n#if !(LG_SIZEOF_PTR == 3 || LG_SIZEOF_INT == 3)\n\ttest_skip(\"64-bit atomic operations not supported\");\n#else\n\tINTEGER_TEST_BODY(uint64_t, u64);\n#endif\n}\nTEST_END\n\n\nTEST_STRUCT(uint32_t, u32);\nTEST_BEGIN(test_atomic_u32) {\n\tINTEGER_TEST_BODY(uint32_t, u32);\n}\nTEST_END\n\nTEST_STRUCT(void *, p);\nTEST_BEGIN(test_atomic_p) {\n\tTEST_BODY(void *, p);\n}\nTEST_END\n\nTEST_STRUCT(size_t, zu);\nTEST_BEGIN(test_atomic_zu) {\n\tINTEGER_TEST_BODY(size_t, zu);\n}\nTEST_END\n\nTEST_STRUCT(ssize_t, zd);\nTEST_BEGIN(test_atomic_zd) {\n\tINTEGER_TEST_BODY(ssize_t, zd);\n}\nTEST_END\n\n\nTEST_STRUCT(unsigned, u);\nTEST_BEGIN(test_atomic_u) {\n\tINTEGER_TEST_BODY(unsigned, u);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_atomic_u64,\n\t    test_atomic_u32,\n\t    test_atomic_p,\n\t    test_atomic_zu,\n\t    test_atomic_zd,\n\t    test_atomic_u);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/background_thread.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/util.h\"\n\nstatic void\ntest_switch_background_thread_ctl(bool new_val) {\n\tbool e0, e1;\n\tsize_t sz = sizeof(bool);\n\n\te1 = new_val;\n\tassert_d_eq(mallctl(\"background_thread\", (void *)&e0, &sz,\n\t    &e1, sz), 0, \"Unexpected mallctl() failure\");\n\tassert_b_eq(e0, !e1,\n\t    \"background_thread should be %d before.\\n\", !e1);\n\tif (e1) {\n\t\tassert_zu_gt(n_background_threads, 0,\n\t\t    \"Number of background threads should be non zero.\\n\");\n\t} else {\n\t\tassert_zu_eq(n_background_threads, 0,\n\t\t    \"Number of background threads should be zero.\\n\");\n\t}\n}\n\nstatic void\ntest_repeat_background_thread_ctl(bool before) {\n\tbool e0, e1;\n\tsize_t sz = sizeof(bool);\n\n\te1 = before;\n\tassert_d_eq(mallctl(\"background_thread\", (void *)&e0, &sz,\n\t    &e1, sz), 0, \"Unexpected mallctl() failure\");\n\tassert_b_eq(e0, before,\n\t    \"background_thread should be %d.\\n\", before);\n\tif (e1) {\n\t\tassert_zu_gt(n_background_threads, 0,\n\t\t    \"Number of background threads should be non zero.\\n\");\n\t} else {\n\t\tassert_zu_eq(n_background_threads, 0,\n\t\t    \"Number of background threads should be zero.\\n\");\n\t}\n}\n\nTEST_BEGIN(test_background_thread_ctl) {\n\ttest_skip_if(!have_background_thread);\n\n\tbool e0, e1;\n\tsize_t sz = sizeof(bool);\n\n\tassert_d_eq(mallctl(\"opt.background_thread\", (void *)&e0, &sz,\n\t    NULL, 0), 0, \"Unexpected mallctl() failure\");\n\tassert_d_eq(mallctl(\"background_thread\", (void *)&e1, &sz,\n\t    NULL, 0), 0, \"Unexpected mallctl() failure\");\n\tassert_b_eq(e0, e1,\n\t    \"Default and opt.background_thread does not match.\\n\");\n\tif (e0) {\n\t\ttest_switch_background_thread_ctl(false);\n\t}\n\tassert_zu_eq(n_background_threads, 0,\n\t    \"Number of background threads should be 0.\\n\");\n\n\tfor (unsigned i = 0; i < 4; i++) {\n\t\ttest_switch_background_thread_ctl(true);\n\t\ttest_repeat_background_thread_ctl(true);\n\t\ttest_repeat_background_thread_ctl(true);\n\n\t\ttest_switch_background_thread_ctl(false);\n\t\ttest_repeat_background_thread_ctl(false);\n\t\ttest_repeat_background_thread_ctl(false);\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_background_thread_running) {\n\ttest_skip_if(!have_background_thread);\n\ttest_skip_if(!config_stats);\n\n#if defined(JEMALLOC_BACKGROUND_THREAD)\n\ttsd_t *tsd = tsd_fetch();\n\tbackground_thread_info_t *info = &background_thread_info[0];\n\n\ttest_repeat_background_thread_ctl(false);\n\ttest_switch_background_thread_ctl(true);\n\tassert_b_eq(info->state, background_thread_started,\n\t    \"Background_thread did not start.\\n\");\n\n\tnstime_t start, now;\n\tnstime_init(&start, 0);\n\tnstime_update(&start);\n\n\tbool ran = false;\n\twhile (true) {\n\t\tmalloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);\n\t\tif (info->tot_n_runs > 0) {\n\t\t\tran = true;\n\t\t}\n\t\tmalloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);\n\t\tif (ran) {\n\t\t\tbreak;\n\t\t}\n\n\t\tnstime_init(&now, 0);\n\t\tnstime_update(&now);\n\t\tnstime_subtract(&now, &start);\n\t\tassert_u64_lt(nstime_sec(&now), 1000,\n\t\t    \"Background threads did not run for 1000 seconds.\");\n\t\tsleep(1);\n\t}\n\ttest_switch_background_thread_ctl(false);\n#endif\n}\nTEST_END\n\nint\nmain(void) {\n\t/* Background_thread creation tests reentrancy naturally. */\n\treturn test_no_reentrancy(\n\t    test_background_thread_ctl,\n\t    test_background_thread_running);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/base.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"test/extent_hooks.h\"\n\nstatic extent_hooks_t hooks_null = {\n\textent_alloc_hook,\n\tNULL, /* dalloc */\n\tNULL, /* destroy */\n\tNULL, /* commit */\n\tNULL, /* decommit */\n\tNULL, /* purge_lazy */\n\tNULL, /* purge_forced */\n\tNULL, /* split */\n\tNULL /* merge */\n};\n\nstatic extent_hooks_t hooks_not_null = {\n\textent_alloc_hook,\n\textent_dalloc_hook,\n\textent_destroy_hook,\n\tNULL, /* commit */\n\textent_decommit_hook,\n\textent_purge_lazy_hook,\n\textent_purge_forced_hook,\n\tNULL, /* split */\n\tNULL /* merge */\n};\n\nTEST_BEGIN(test_base_hooks_default) {\n\ttsdn_t *tsdn;\n\tbase_t *base;\n\tsize_t allocated0, allocated1, resident, mapped;\n\n\ttsdn = tsdn_fetch();\n\tbase = base_new(tsdn, 0, (extent_hooks_t *)&extent_hooks_default);\n\n\tif (config_stats) {\n\t\tbase_stats_get(tsdn, base, &allocated0, &resident, &mapped);\n\t\tassert_zu_ge(allocated0, sizeof(base_t),\n\t\t    \"Base header should count as allocated\");\n\t}\n\n\tassert_ptr_not_null(base_alloc(tsdn, base, 42, 1),\n\t    \"Unexpected base_alloc() failure\");\n\n\tif (config_stats) {\n\t\tbase_stats_get(tsdn, base, &allocated1, &resident, &mapped);\n\t\tassert_zu_ge(allocated1 - allocated0, 42,\n\t\t    \"At least 42 bytes were allocated by base_alloc()\");\n\t}\n\n\tbase_delete(base);\n}\nTEST_END\n\nTEST_BEGIN(test_base_hooks_null) {\n\textent_hooks_t hooks_orig;\n\ttsdn_t *tsdn;\n\tbase_t *base;\n\tsize_t allocated0, allocated1, resident, mapped;\n\n\textent_hooks_prep();\n\ttry_dalloc = false;\n\ttry_destroy = true;\n\ttry_decommit = false;\n\ttry_purge_lazy = false;\n\ttry_purge_forced = false;\n\tmemcpy(&hooks_orig, &hooks, sizeof(extent_hooks_t));\n\tmemcpy(&hooks, &hooks_null, sizeof(extent_hooks_t));\n\n\ttsdn = tsdn_fetch();\n\tbase = base_new(tsdn, 0, &hooks);\n\tassert_ptr_not_null(base, \"Unexpected base_new() failure\");\n\n\tif (config_stats) {\n\t\tbase_stats_get(tsdn, base, &allocated0, &resident, &mapped);\n\t\tassert_zu_ge(allocated0, sizeof(base_t),\n\t\t    \"Base header should count as allocated\");\n\t}\n\n\tassert_ptr_not_null(base_alloc(tsdn, base, 42, 1),\n\t    \"Unexpected base_alloc() failure\");\n\n\tif (config_stats) {\n\t\tbase_stats_get(tsdn, base, &allocated1, &resident, &mapped);\n\t\tassert_zu_ge(allocated1 - allocated0, 42,\n\t\t    \"At least 42 bytes were allocated by base_alloc()\");\n\t}\n\n\tbase_delete(base);\n\n\tmemcpy(&hooks, &hooks_orig, sizeof(extent_hooks_t));\n}\nTEST_END\n\nTEST_BEGIN(test_base_hooks_not_null) {\n\textent_hooks_t hooks_orig;\n\ttsdn_t *tsdn;\n\tbase_t *base;\n\tvoid *p, *q, *r, *r_exp;\n\n\textent_hooks_prep();\n\ttry_dalloc = false;\n\ttry_destroy = true;\n\ttry_decommit = false;\n\ttry_purge_lazy = false;\n\ttry_purge_forced = false;\n\tmemcpy(&hooks_orig, &hooks, sizeof(extent_hooks_t));\n\tmemcpy(&hooks, &hooks_not_null, sizeof(extent_hooks_t));\n\n\ttsdn = tsdn_fetch();\n\tdid_alloc = false;\n\tbase = base_new(tsdn, 0, &hooks);\n\tassert_ptr_not_null(base, \"Unexpected base_new() failure\");\n\tassert_true(did_alloc, \"Expected alloc\");\n\n\t/*\n\t * Check for tight packing at specified alignment under simple\n\t * conditions.\n\t */\n\t{\n\t\tconst size_t alignments[] = {\n\t\t\t1,\n\t\t\tQUANTUM,\n\t\t\tQUANTUM << 1,\n\t\t\tCACHELINE,\n\t\t\tCACHELINE << 1,\n\t\t};\n\t\tunsigned i;\n\n\t\tfor (i = 0; i < sizeof(alignments) / sizeof(size_t); i++) {\n\t\t\tsize_t alignment = alignments[i];\n\t\t\tsize_t align_ceil = ALIGNMENT_CEILING(alignment,\n\t\t\t    QUANTUM);\n\t\t\tp = base_alloc(tsdn, base, 1, alignment);\n\t\t\tassert_ptr_not_null(p,\n\t\t\t    \"Unexpected base_alloc() failure\");\n\t\t\tassert_ptr_eq(p,\n\t\t\t    (void *)(ALIGNMENT_CEILING((uintptr_t)p,\n\t\t\t    alignment)), \"Expected quantum alignment\");\n\t\t\tq = base_alloc(tsdn, base, alignment, alignment);\n\t\t\tassert_ptr_not_null(q,\n\t\t\t    \"Unexpected base_alloc() failure\");\n\t\t\tassert_ptr_eq((void *)((uintptr_t)p + align_ceil), q,\n\t\t\t    \"Minimal allocation should take up %zu bytes\",\n\t\t\t    align_ceil);\n\t\t\tr = base_alloc(tsdn, base, 1, alignment);\n\t\t\tassert_ptr_not_null(r,\n\t\t\t    \"Unexpected base_alloc() failure\");\n\t\t\tassert_ptr_eq((void *)((uintptr_t)q + align_ceil), r,\n\t\t\t    \"Minimal allocation should take up %zu bytes\",\n\t\t\t    align_ceil);\n\t\t}\n\t}\n\n\t/*\n\t * Allocate an object that cannot fit in the first block, then verify\n\t * that the first block's remaining space is considered for subsequent\n\t * allocation.\n\t */\n\tassert_zu_ge(extent_bsize_get(&base->blocks->extent), QUANTUM,\n\t    \"Remainder insufficient for test\");\n\t/* Use up all but one quantum of block. */\n\twhile (extent_bsize_get(&base->blocks->extent) > QUANTUM) {\n\t\tp = base_alloc(tsdn, base, QUANTUM, QUANTUM);\n\t\tassert_ptr_not_null(p, \"Unexpected base_alloc() failure\");\n\t}\n\tr_exp = extent_addr_get(&base->blocks->extent);\n\tassert_zu_eq(base->extent_sn_next, 1, \"One extant block expected\");\n\tq = base_alloc(tsdn, base, QUANTUM + 1, QUANTUM);\n\tassert_ptr_not_null(q, \"Unexpected base_alloc() failure\");\n\tassert_ptr_ne(q, r_exp, \"Expected allocation from new block\");\n\tassert_zu_eq(base->extent_sn_next, 2, \"Two extant blocks expected\");\n\tr = base_alloc(tsdn, base, QUANTUM, QUANTUM);\n\tassert_ptr_not_null(r, \"Unexpected base_alloc() failure\");\n\tassert_ptr_eq(r, r_exp, \"Expected allocation from first block\");\n\tassert_zu_eq(base->extent_sn_next, 2, \"Two extant blocks expected\");\n\n\t/*\n\t * Check for proper alignment support when normal blocks are too small.\n\t */\n\t{\n\t\tconst size_t alignments[] = {\n\t\t\tHUGEPAGE,\n\t\t\tHUGEPAGE << 1\n\t\t};\n\t\tunsigned i;\n\n\t\tfor (i = 0; i < sizeof(alignments) / sizeof(size_t); i++) {\n\t\t\tsize_t alignment = alignments[i];\n\t\t\tp = base_alloc(tsdn, base, QUANTUM, alignment);\n\t\t\tassert_ptr_not_null(p,\n\t\t\t    \"Unexpected base_alloc() failure\");\n\t\t\tassert_ptr_eq(p,\n\t\t\t    (void *)(ALIGNMENT_CEILING((uintptr_t)p,\n\t\t\t    alignment)), \"Expected %zu-byte alignment\",\n\t\t\t    alignment);\n\t\t}\n\t}\n\n\tcalled_dalloc = called_destroy = called_decommit = called_purge_lazy =\n\t    called_purge_forced = false;\n\tbase_delete(base);\n\tassert_true(called_dalloc, \"Expected dalloc call\");\n\tassert_true(!called_destroy, \"Unexpected destroy call\");\n\tassert_true(called_decommit, \"Expected decommit call\");\n\tassert_true(called_purge_lazy, \"Expected purge_lazy call\");\n\tassert_true(called_purge_forced, \"Expected purge_forced call\");\n\n\ttry_dalloc = true;\n\ttry_destroy = true;\n\ttry_decommit = true;\n\ttry_purge_lazy = true;\n\ttry_purge_forced = true;\n\tmemcpy(&hooks, &hooks_orig, sizeof(extent_hooks_t));\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_base_hooks_default,\n\t    test_base_hooks_null,\n\t    test_base_hooks_not_null);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/bit_util.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/bit_util.h\"\n\n#define TEST_POW2_CEIL(t, suf, pri) do {\t\t\t\t\\\n\tunsigned i, pow2;\t\t\t\t\t\t\\\n\tt x;\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tassert_##suf##_eq(pow2_ceil_##suf(0), 0, \"Unexpected result\");\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tfor (i = 0; i < sizeof(t) * 8; i++) {\t\t\t\t\\\n\t\tassert_##suf##_eq(pow2_ceil_##suf(((t)1) << i), ((t)1)\t\\\n\t\t    << i, \"Unexpected result\");\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tfor (i = 2; i < sizeof(t) * 8; i++) {\t\t\t\t\\\n\t\tassert_##suf##_eq(pow2_ceil_##suf((((t)1) << i) - 1),\t\\\n\t\t    ((t)1) << i, \"Unexpected result\");\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tfor (i = 0; i < sizeof(t) * 8 - 1; i++) {\t\t\t\\\n\t\tassert_##suf##_eq(pow2_ceil_##suf((((t)1) << i) + 1),\t\\\n\t\t    ((t)1) << (i+1), \"Unexpected result\");\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n\t\t\t\t\t\t\t\t\t\\\n\tfor (pow2 = 1; pow2 < 25; pow2++) {\t\t\t\t\\\n\t\tfor (x = (((t)1) << (pow2-1)) + 1; x <= ((t)1) << pow2;\t\\\n\t\t    x++) {\t\t\t\t\t\t\\\n\t\t\tassert_##suf##_eq(pow2_ceil_##suf(x),\t\t\\\n\t\t\t    ((t)1) << pow2,\t\t\t\t\\\n\t\t\t    \"Unexpected result, x=%\"pri, x);\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\nTEST_BEGIN(test_pow2_ceil_u64) {\n\tTEST_POW2_CEIL(uint64_t, u64, FMTu64);\n}\nTEST_END\n\nTEST_BEGIN(test_pow2_ceil_u32) {\n\tTEST_POW2_CEIL(uint32_t, u32, FMTu32);\n}\nTEST_END\n\nTEST_BEGIN(test_pow2_ceil_zu) {\n\tTEST_POW2_CEIL(size_t, zu, \"zu\");\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_pow2_ceil_u64,\n\t    test_pow2_ceil_u32,\n\t    test_pow2_ceil_zu);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/bitmap.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#define NBITS_TAB \\\n    NB( 1) \\\n    NB( 2) \\\n    NB( 3) \\\n    NB( 4) \\\n    NB( 5) \\\n    NB( 6) \\\n    NB( 7) \\\n    NB( 8) \\\n    NB( 9) \\\n    NB(10) \\\n    NB(11) \\\n    NB(12) \\\n    NB(13) \\\n    NB(14) \\\n    NB(15) \\\n    NB(16) \\\n    NB(17) \\\n    NB(18) \\\n    NB(19) \\\n    NB(20) \\\n    NB(21) \\\n    NB(22) \\\n    NB(23) \\\n    NB(24) \\\n    NB(25) \\\n    NB(26) \\\n    NB(27) \\\n    NB(28) \\\n    NB(29) \\\n    NB(30) \\\n    NB(31) \\\n    NB(32) \\\n    \\\n    NB(33) \\\n    NB(34) \\\n    NB(35) \\\n    NB(36) \\\n    NB(37) \\\n    NB(38) \\\n    NB(39) \\\n    NB(40) \\\n    NB(41) \\\n    NB(42) \\\n    NB(43) \\\n    NB(44) \\\n    NB(45) \\\n    NB(46) \\\n    NB(47) \\\n    NB(48) \\\n    NB(49) \\\n    NB(50) \\\n    NB(51) \\\n    NB(52) \\\n    NB(53) \\\n    NB(54) \\\n    NB(55) \\\n    NB(56) \\\n    NB(57) \\\n    NB(58) \\\n    NB(59) \\\n    NB(60) \\\n    NB(61) \\\n    NB(62) \\\n    NB(63) \\\n    NB(64) \\\n    NB(65) \\\n    \\\n    NB(126) \\\n    NB(127) \\\n    NB(128) \\\n    NB(129) \\\n    NB(130) \\\n    \\\n    NB(254) \\\n    NB(255) \\\n    NB(256) \\\n    NB(257) \\\n    NB(258) \\\n    \\\n    NB(510) \\\n    NB(511) \\\n    NB(512) \\\n    NB(513) \\\n    NB(514) \\\n    \\\n    NB(1024) \\\n    NB(2048) \\\n    NB(4096) \\\n    NB(8192) \\\n    NB(16384) \\\n\nstatic void\ntest_bitmap_initializer_body(const bitmap_info_t *binfo, size_t nbits) {\n\tbitmap_info_t binfo_dyn;\n\tbitmap_info_init(&binfo_dyn, nbits);\n\n\tassert_zu_eq(bitmap_size(binfo), bitmap_size(&binfo_dyn),\n\t    \"Unexpected difference between static and dynamic initialization, \"\n\t    \"nbits=%zu\", nbits);\n\tassert_zu_eq(binfo->nbits, binfo_dyn.nbits,\n\t    \"Unexpected difference between static and dynamic initialization, \"\n\t    \"nbits=%zu\", nbits);\n#ifdef BITMAP_USE_TREE\n\tassert_u_eq(binfo->nlevels, binfo_dyn.nlevels,\n\t    \"Unexpected difference between static and dynamic initialization, \"\n\t    \"nbits=%zu\", nbits);\n\t{\n\t\tunsigned i;\n\n\t\tfor (i = 0; i < binfo->nlevels; i++) {\n\t\t\tassert_zu_eq(binfo->levels[i].group_offset,\n\t\t\t    binfo_dyn.levels[i].group_offset,\n\t\t\t    \"Unexpected difference between static and dynamic \"\n\t\t\t    \"initialization, nbits=%zu, level=%u\", nbits, i);\n\t\t}\n\t}\n#else\n\tassert_zu_eq(binfo->ngroups, binfo_dyn.ngroups,\n\t    \"Unexpected difference between static and dynamic initialization\");\n#endif\n}\n\nTEST_BEGIN(test_bitmap_initializer) {\n#define NB(nbits) {\t\t\t\t\t\t\t\\\n\t\tif (nbits <= BITMAP_MAXBITS) {\t\t\t\t\\\n\t\t\tbitmap_info_t binfo =\t\t\t\t\\\n\t\t\t    BITMAP_INFO_INITIALIZER(nbits);\t\t\\\n\t\t\ttest_bitmap_initializer_body(&binfo, nbits);\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\n\tNBITS_TAB\n#undef NB\n}\nTEST_END\n\nstatic size_t\ntest_bitmap_size_body(const bitmap_info_t *binfo, size_t nbits,\n    size_t prev_size) {\n\tsize_t size = bitmap_size(binfo);\n\tassert_zu_ge(size, (nbits >> 3),\n\t    \"Bitmap size is smaller than expected\");\n\tassert_zu_ge(size, prev_size, \"Bitmap size is smaller than expected\");\n\treturn size;\n}\n\nTEST_BEGIN(test_bitmap_size) {\n\tsize_t nbits, prev_size;\n\n\tprev_size = 0;\n\tfor (nbits = 1; nbits <= BITMAP_MAXBITS; nbits++) {\n\t\tbitmap_info_t binfo;\n\t\tbitmap_info_init(&binfo, nbits);\n\t\tprev_size = test_bitmap_size_body(&binfo, nbits, prev_size);\n\t}\n#define NB(nbits) {\t\t\t\t\t\t\t\\\n\t\tbitmap_info_t binfo = BITMAP_INFO_INITIALIZER(nbits);\t\\\n\t\tprev_size = test_bitmap_size_body(&binfo, nbits,\t\\\n\t\t    prev_size);\t\t\t\t\t\t\\\n\t}\n\tprev_size = 0;\n\tNBITS_TAB\n#undef NB\n}\nTEST_END\n\nstatic void\ntest_bitmap_init_body(const bitmap_info_t *binfo, size_t nbits) {\n\tsize_t i;\n\tbitmap_t *bitmap = (bitmap_t *)malloc(bitmap_size(binfo));\n\tassert_ptr_not_null(bitmap, \"Unexpected malloc() failure\");\n\n\tbitmap_init(bitmap, binfo, false);\n\tfor (i = 0; i < nbits; i++) {\n\t\tassert_false(bitmap_get(bitmap, binfo, i),\n\t\t    \"Bit should be unset\");\n\t}\n\n\tbitmap_init(bitmap, binfo, true);\n\tfor (i = 0; i < nbits; i++) {\n\t\tassert_true(bitmap_get(bitmap, binfo, i), \"Bit should be set\");\n\t}\n\n\tfree(bitmap);\n}\n\nTEST_BEGIN(test_bitmap_init) {\n\tsize_t nbits;\n\n\tfor (nbits = 1; nbits <= BITMAP_MAXBITS; nbits++) {\n\t\tbitmap_info_t binfo;\n\t\tbitmap_info_init(&binfo, nbits);\n\t\ttest_bitmap_init_body(&binfo, nbits);\n\t}\n#define NB(nbits) {\t\t\t\t\t\t\t\\\n\t\tbitmap_info_t binfo = BITMAP_INFO_INITIALIZER(nbits);\t\\\n\t\ttest_bitmap_init_body(&binfo, nbits);\t\t\t\\\n\t}\n\tNBITS_TAB\n#undef NB\n}\nTEST_END\n\nstatic void\ntest_bitmap_set_body(const bitmap_info_t *binfo, size_t nbits) {\n\tsize_t i;\n\tbitmap_t *bitmap = (bitmap_t *)malloc(bitmap_size(binfo));\n\tassert_ptr_not_null(bitmap, \"Unexpected malloc() failure\");\n\tbitmap_init(bitmap, binfo, false);\n\n\tfor (i = 0; i < nbits; i++) {\n\t\tbitmap_set(bitmap, binfo, i);\n\t}\n\tassert_true(bitmap_full(bitmap, binfo), \"All bits should be set\");\n\tfree(bitmap);\n}\n\nTEST_BEGIN(test_bitmap_set) {\n\tsize_t nbits;\n\n\tfor (nbits = 1; nbits <= BITMAP_MAXBITS; nbits++) {\n\t\tbitmap_info_t binfo;\n\t\tbitmap_info_init(&binfo, nbits);\n\t\ttest_bitmap_set_body(&binfo, nbits);\n\t}\n#define NB(nbits) {\t\t\t\t\t\t\t\\\n\t\tbitmap_info_t binfo = BITMAP_INFO_INITIALIZER(nbits);\t\\\n\t\ttest_bitmap_set_body(&binfo, nbits);\t\t\t\\\n\t}\n\tNBITS_TAB\n#undef NB\n}\nTEST_END\n\nstatic void\ntest_bitmap_unset_body(const bitmap_info_t *binfo, size_t nbits) {\n\tsize_t i;\n\tbitmap_t *bitmap = (bitmap_t *)malloc(bitmap_size(binfo));\n\tassert_ptr_not_null(bitmap, \"Unexpected malloc() failure\");\n\tbitmap_init(bitmap, binfo, false);\n\n\tfor (i = 0; i < nbits; i++) {\n\t\tbitmap_set(bitmap, binfo, i);\n\t}\n\tassert_true(bitmap_full(bitmap, binfo), \"All bits should be set\");\n\tfor (i = 0; i < nbits; i++) {\n\t\tbitmap_unset(bitmap, binfo, i);\n\t}\n\tfor (i = 0; i < nbits; i++) {\n\t\tbitmap_set(bitmap, binfo, i);\n\t}\n\tassert_true(bitmap_full(bitmap, binfo), \"All bits should be set\");\n\tfree(bitmap);\n}\n\nTEST_BEGIN(test_bitmap_unset) {\n\tsize_t nbits;\n\n\tfor (nbits = 1; nbits <= BITMAP_MAXBITS; nbits++) {\n\t\tbitmap_info_t binfo;\n\t\tbitmap_info_init(&binfo, nbits);\n\t\ttest_bitmap_unset_body(&binfo, nbits);\n\t}\n#define NB(nbits) {\t\t\t\t\t\t\t\\\n\t\tbitmap_info_t binfo = BITMAP_INFO_INITIALIZER(nbits);\t\\\n\t\ttest_bitmap_unset_body(&binfo, nbits);\t\t\t\\\n\t}\n\tNBITS_TAB\n#undef NB\n}\nTEST_END\n\nstatic void\ntest_bitmap_xfu_body(const bitmap_info_t *binfo, size_t nbits) {\n\tbitmap_t *bitmap = (bitmap_t *)malloc(bitmap_size(binfo));\n\tassert_ptr_not_null(bitmap, \"Unexpected malloc() failure\");\n\tbitmap_init(bitmap, binfo, false);\n\n\t/* Iteratively set bits starting at the beginning. */\n\tfor (size_t i = 0; i < nbits; i++) {\n\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, 0), i,\n\t\t    \"First unset bit should be just after previous first unset \"\n\t\t    \"bit\");\n\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, (i > 0) ? i-1 : i), i,\n\t\t    \"First unset bit should be just after previous first unset \"\n\t\t    \"bit\");\n\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, i), i,\n\t\t    \"First unset bit should be just after previous first unset \"\n\t\t    \"bit\");\n\t\tassert_zu_eq(bitmap_sfu(bitmap, binfo), i,\n\t\t    \"First unset bit should be just after previous first unset \"\n\t\t    \"bit\");\n\t}\n\tassert_true(bitmap_full(bitmap, binfo), \"All bits should be set\");\n\n\t/*\n\t * Iteratively unset bits starting at the end, and verify that\n\t * bitmap_sfu() reaches the unset bits.\n\t */\n\tfor (size_t i = nbits - 1; i < nbits; i--) { /* (nbits..0] */\n\t\tbitmap_unset(bitmap, binfo, i);\n\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, 0), i,\n\t\t    \"First unset bit should the bit previously unset\");\n\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, (i > 0) ? i-1 : i), i,\n\t\t    \"First unset bit should the bit previously unset\");\n\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, i), i,\n\t\t    \"First unset bit should the bit previously unset\");\n\t\tassert_zu_eq(bitmap_sfu(bitmap, binfo), i,\n\t\t    \"First unset bit should the bit previously unset\");\n\t\tbitmap_unset(bitmap, binfo, i);\n\t}\n\tassert_false(bitmap_get(bitmap, binfo, 0), \"Bit should be unset\");\n\n\t/*\n\t * Iteratively set bits starting at the beginning, and verify that\n\t * bitmap_sfu() looks past them.\n\t */\n\tfor (size_t i = 1; i < nbits; i++) {\n\t\tbitmap_set(bitmap, binfo, i - 1);\n\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, 0), i,\n\t\t    \"First unset bit should be just after the bit previously \"\n\t\t    \"set\");\n\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, (i > 0) ? i-1 : i), i,\n\t\t    \"First unset bit should be just after the bit previously \"\n\t\t    \"set\");\n\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, i), i,\n\t\t    \"First unset bit should be just after the bit previously \"\n\t\t    \"set\");\n\t\tassert_zu_eq(bitmap_sfu(bitmap, binfo), i,\n\t\t    \"First unset bit should be just after the bit previously \"\n\t\t    \"set\");\n\t\tbitmap_unset(bitmap, binfo, i);\n\t}\n\tassert_zu_eq(bitmap_ffu(bitmap, binfo, 0), nbits - 1,\n\t    \"First unset bit should be the last bit\");\n\tassert_zu_eq(bitmap_ffu(bitmap, binfo, (nbits > 1) ? nbits-2 : nbits-1),\n\t    nbits - 1, \"First unset bit should be the last bit\");\n\tassert_zu_eq(bitmap_ffu(bitmap, binfo, nbits - 1), nbits - 1,\n\t    \"First unset bit should be the last bit\");\n\tassert_zu_eq(bitmap_sfu(bitmap, binfo), nbits - 1,\n\t    \"First unset bit should be the last bit\");\n\tassert_true(bitmap_full(bitmap, binfo), \"All bits should be set\");\n\n\t/*\n\t * Bubble a \"usu\" pattern through the bitmap and verify that\n\t * bitmap_ffu() finds the correct bit for all five min_bit cases.\n\t */\n\tif (nbits >= 3) {\n\t\tfor (size_t i = 0; i < nbits-2; i++) {\n\t\t\tbitmap_unset(bitmap, binfo, i);\n\t\t\tbitmap_unset(bitmap, binfo, i+2);\n\t\t\tif (i > 0) {\n\t\t\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, i-1), i,\n\t\t\t\t    \"Unexpected first unset bit\");\n\t\t\t}\n\t\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, i), i,\n\t\t\t    \"Unexpected first unset bit\");\n\t\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, i+1), i+2,\n\t\t\t    \"Unexpected first unset bit\");\n\t\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, i+2), i+2,\n\t\t\t    \"Unexpected first unset bit\");\n\t\t\tif (i + 3 < nbits) {\n\t\t\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, i+3),\n\t\t\t\t    nbits, \"Unexpected first unset bit\");\n\t\t\t}\n\t\t\tassert_zu_eq(bitmap_sfu(bitmap, binfo), i,\n\t\t\t    \"Unexpected first unset bit\");\n\t\t\tassert_zu_eq(bitmap_sfu(bitmap, binfo), i+2,\n\t\t\t    \"Unexpected first unset bit\");\n\t\t}\n\t}\n\n\t/*\n\t * Unset the last bit, bubble another unset bit through the bitmap, and\n\t * verify that bitmap_ffu() finds the correct bit for all four min_bit\n\t * cases.\n\t */\n\tif (nbits >= 3) {\n\t\tbitmap_unset(bitmap, binfo, nbits-1);\n\t\tfor (size_t i = 0; i < nbits-1; i++) {\n\t\t\tbitmap_unset(bitmap, binfo, i);\n\t\t\tif (i > 0) {\n\t\t\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, i-1), i,\n\t\t\t\t    \"Unexpected first unset bit\");\n\t\t\t}\n\t\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, i), i,\n\t\t\t    \"Unexpected first unset bit\");\n\t\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, i+1), nbits-1,\n\t\t\t    \"Unexpected first unset bit\");\n\t\t\tassert_zu_eq(bitmap_ffu(bitmap, binfo, nbits-1),\n\t\t\t    nbits-1, \"Unexpected first unset bit\");\n\n\t\t\tassert_zu_eq(bitmap_sfu(bitmap, binfo), i,\n\t\t\t    \"Unexpected first unset bit\");\n\t\t}\n\t\tassert_zu_eq(bitmap_sfu(bitmap, binfo), nbits-1,\n\t\t    \"Unexpected first unset bit\");\n\t}\n\n\tfree(bitmap);\n}\n\nTEST_BEGIN(test_bitmap_xfu) {\n\tsize_t nbits;\n\n\tfor (nbits = 1; nbits <= BITMAP_MAXBITS; nbits++) {\n\t\tbitmap_info_t binfo;\n\t\tbitmap_info_init(&binfo, nbits);\n\t\ttest_bitmap_xfu_body(&binfo, nbits);\n\t}\n#define NB(nbits) {\t\t\t\t\t\t\t\\\n\t\tbitmap_info_t binfo = BITMAP_INFO_INITIALIZER(nbits);\t\\\n\t\ttest_bitmap_xfu_body(&binfo, nbits);\t\t\t\\\n\t}\n\tNBITS_TAB\n#undef NB\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_bitmap_initializer,\n\t    test_bitmap_size,\n\t    test_bitmap_init,\n\t    test_bitmap_set,\n\t    test_bitmap_unset,\n\t    test_bitmap_xfu);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/ckh.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nTEST_BEGIN(test_new_delete) {\n\ttsd_t *tsd;\n\tckh_t ckh;\n\n\ttsd = tsd_fetch();\n\n\tassert_false(ckh_new(tsd, &ckh, 2, ckh_string_hash,\n\t    ckh_string_keycomp), \"Unexpected ckh_new() error\");\n\tckh_delete(tsd, &ckh);\n\n\tassert_false(ckh_new(tsd, &ckh, 3, ckh_pointer_hash,\n\t    ckh_pointer_keycomp), \"Unexpected ckh_new() error\");\n\tckh_delete(tsd, &ckh);\n}\nTEST_END\n\nTEST_BEGIN(test_count_insert_search_remove) {\n\ttsd_t *tsd;\n\tckh_t ckh;\n\tconst char *strs[] = {\n\t    \"a string\",\n\t    \"A string\",\n\t    \"a string.\",\n\t    \"A string.\"\n\t};\n\tconst char *missing = \"A string not in the hash table.\";\n\tsize_t i;\n\n\ttsd = tsd_fetch();\n\n\tassert_false(ckh_new(tsd, &ckh, 2, ckh_string_hash,\n\t    ckh_string_keycomp), \"Unexpected ckh_new() error\");\n\tassert_zu_eq(ckh_count(&ckh), 0,\n\t    \"ckh_count() should return %zu, but it returned %zu\", ZU(0),\n\t    ckh_count(&ckh));\n\n\t/* Insert. */\n\tfor (i = 0; i < sizeof(strs)/sizeof(const char *); i++) {\n\t\tckh_insert(tsd, &ckh, strs[i], strs[i]);\n\t\tassert_zu_eq(ckh_count(&ckh), i+1,\n\t\t    \"ckh_count() should return %zu, but it returned %zu\", i+1,\n\t\t    ckh_count(&ckh));\n\t}\n\n\t/* Search. */\n\tfor (i = 0; i < sizeof(strs)/sizeof(const char *); i++) {\n\t\tunion {\n\t\t\tvoid *p;\n\t\t\tconst char *s;\n\t\t} k, v;\n\t\tvoid **kp, **vp;\n\t\tconst char *ks, *vs;\n\n\t\tkp = (i & 1) ? &k.p : NULL;\n\t\tvp = (i & 2) ? &v.p : NULL;\n\t\tk.p = NULL;\n\t\tv.p = NULL;\n\t\tassert_false(ckh_search(&ckh, strs[i], kp, vp),\n\t\t    \"Unexpected ckh_search() error\");\n\n\t\tks = (i & 1) ? strs[i] : (const char *)NULL;\n\t\tvs = (i & 2) ? strs[i] : (const char *)NULL;\n\t\tassert_ptr_eq((void *)ks, (void *)k.s, \"Key mismatch, i=%zu\",\n\t\t    i);\n\t\tassert_ptr_eq((void *)vs, (void *)v.s, \"Value mismatch, i=%zu\",\n\t\t    i);\n\t}\n\tassert_true(ckh_search(&ckh, missing, NULL, NULL),\n\t    \"Unexpected ckh_search() success\");\n\n\t/* Remove. */\n\tfor (i = 0; i < sizeof(strs)/sizeof(const char *); i++) {\n\t\tunion {\n\t\t\tvoid *p;\n\t\t\tconst char *s;\n\t\t} k, v;\n\t\tvoid **kp, **vp;\n\t\tconst char *ks, *vs;\n\n\t\tkp = (i & 1) ? &k.p : NULL;\n\t\tvp = (i & 2) ? &v.p : NULL;\n\t\tk.p = NULL;\n\t\tv.p = NULL;\n\t\tassert_false(ckh_remove(tsd, &ckh, strs[i], kp, vp),\n\t\t    \"Unexpected ckh_remove() error\");\n\n\t\tks = (i & 1) ? strs[i] : (const char *)NULL;\n\t\tvs = (i & 2) ? strs[i] : (const char *)NULL;\n\t\tassert_ptr_eq((void *)ks, (void *)k.s, \"Key mismatch, i=%zu\",\n\t\t    i);\n\t\tassert_ptr_eq((void *)vs, (void *)v.s, \"Value mismatch, i=%zu\",\n\t\t    i);\n\t\tassert_zu_eq(ckh_count(&ckh),\n\t\t    sizeof(strs)/sizeof(const char *) - i - 1,\n\t\t    \"ckh_count() should return %zu, but it returned %zu\",\n\t\t        sizeof(strs)/sizeof(const char *) - i - 1,\n\t\t    ckh_count(&ckh));\n\t}\n\n\tckh_delete(tsd, &ckh);\n}\nTEST_END\n\nTEST_BEGIN(test_insert_iter_remove) {\n#define NITEMS ZU(1000)\n\ttsd_t *tsd;\n\tckh_t ckh;\n\tvoid **p[NITEMS];\n\tvoid *q, *r;\n\tsize_t i;\n\n\ttsd = tsd_fetch();\n\n\tassert_false(ckh_new(tsd, &ckh, 2, ckh_pointer_hash,\n\t    ckh_pointer_keycomp), \"Unexpected ckh_new() error\");\n\n\tfor (i = 0; i < NITEMS; i++) {\n\t\tp[i] = mallocx(i+1, 0);\n\t\tassert_ptr_not_null(p[i], \"Unexpected mallocx() failure\");\n\t}\n\n\tfor (i = 0; i < NITEMS; i++) {\n\t\tsize_t j;\n\n\t\tfor (j = i; j < NITEMS; j++) {\n\t\t\tassert_false(ckh_insert(tsd, &ckh, p[j], p[j]),\n\t\t\t    \"Unexpected ckh_insert() failure\");\n\t\t\tassert_false(ckh_search(&ckh, p[j], &q, &r),\n\t\t\t    \"Unexpected ckh_search() failure\");\n\t\t\tassert_ptr_eq(p[j], q, \"Key pointer mismatch\");\n\t\t\tassert_ptr_eq(p[j], r, \"Value pointer mismatch\");\n\t\t}\n\n\t\tassert_zu_eq(ckh_count(&ckh), NITEMS,\n\t\t    \"ckh_count() should return %zu, but it returned %zu\",\n\t\t    NITEMS, ckh_count(&ckh));\n\n\t\tfor (j = i + 1; j < NITEMS; j++) {\n\t\t\tassert_false(ckh_search(&ckh, p[j], NULL, NULL),\n\t\t\t    \"Unexpected ckh_search() failure\");\n\t\t\tassert_false(ckh_remove(tsd, &ckh, p[j], &q, &r),\n\t\t\t    \"Unexpected ckh_remove() failure\");\n\t\t\tassert_ptr_eq(p[j], q, \"Key pointer mismatch\");\n\t\t\tassert_ptr_eq(p[j], r, \"Value pointer mismatch\");\n\t\t\tassert_true(ckh_search(&ckh, p[j], NULL, NULL),\n\t\t\t    \"Unexpected ckh_search() success\");\n\t\t\tassert_true(ckh_remove(tsd, &ckh, p[j], &q, &r),\n\t\t\t    \"Unexpected ckh_remove() success\");\n\t\t}\n\n\t\t{\n\t\t\tbool seen[NITEMS];\n\t\t\tsize_t tabind;\n\n\t\t\tmemset(seen, 0, sizeof(seen));\n\n\t\t\tfor (tabind = 0; !ckh_iter(&ckh, &tabind, &q, &r);) {\n\t\t\t\tsize_t k;\n\n\t\t\t\tassert_ptr_eq(q, r, \"Key and val not equal\");\n\n\t\t\t\tfor (k = 0; k < NITEMS; k++) {\n\t\t\t\t\tif (p[k] == q) {\n\t\t\t\t\t\tassert_false(seen[k],\n\t\t\t\t\t\t    \"Item %zu already seen\", k);\n\t\t\t\t\t\tseen[k] = true;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfor (j = 0; j < i + 1; j++) {\n\t\t\t\tassert_true(seen[j], \"Item %zu not seen\", j);\n\t\t\t}\n\t\t\tfor (; j < NITEMS; j++) {\n\t\t\t\tassert_false(seen[j], \"Item %zu seen\", j);\n\t\t\t}\n\t\t}\n\t}\n\n\tfor (i = 0; i < NITEMS; i++) {\n\t\tassert_false(ckh_search(&ckh, p[i], NULL, NULL),\n\t\t    \"Unexpected ckh_search() failure\");\n\t\tassert_false(ckh_remove(tsd, &ckh, p[i], &q, &r),\n\t\t    \"Unexpected ckh_remove() failure\");\n\t\tassert_ptr_eq(p[i], q, \"Key pointer mismatch\");\n\t\tassert_ptr_eq(p[i], r, \"Value pointer mismatch\");\n\t\tassert_true(ckh_search(&ckh, p[i], NULL, NULL),\n\t\t    \"Unexpected ckh_search() success\");\n\t\tassert_true(ckh_remove(tsd, &ckh, p[i], &q, &r),\n\t\t    \"Unexpected ckh_remove() success\");\n\t\tdallocx(p[i], 0);\n\t}\n\n\tassert_zu_eq(ckh_count(&ckh), 0,\n\t    \"ckh_count() should return %zu, but it returned %zu\",\n\t    ZU(0), ckh_count(&ckh));\n\tckh_delete(tsd, &ckh);\n#undef NITEMS\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_new_delete,\n\t    test_count_insert_search_remove,\n\t    test_insert_iter_remove);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/decay.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/ticker.h\"\n\nstatic nstime_monotonic_t *nstime_monotonic_orig;\nstatic nstime_update_t *nstime_update_orig;\n\nstatic unsigned nupdates_mock;\nstatic nstime_t time_mock;\nstatic bool monotonic_mock;\n\nstatic bool\ncheck_background_thread_enabled(void) {\n\tbool enabled;\n\tsize_t sz = sizeof(bool);\n\tint ret = mallctl(\"background_thread\", (void *)&enabled, &sz, NULL,0);\n\tif (ret == ENOENT) {\n\t\treturn false;\n\t}\n\tassert_d_eq(ret, 0, \"Unexpected mallctl error\");\n\treturn enabled;\n}\n\nstatic bool\nnstime_monotonic_mock(void) {\n\treturn monotonic_mock;\n}\n\nstatic bool\nnstime_update_mock(nstime_t *time) {\n\tnupdates_mock++;\n\tif (monotonic_mock) {\n\t\tnstime_copy(time, &time_mock);\n\t}\n\treturn !monotonic_mock;\n}\n\nstatic unsigned\ndo_arena_create(ssize_t dirty_decay_ms, ssize_t muzzy_decay_ms) {\n\tunsigned arena_ind;\n\tsize_t sz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena_ind, &sz, NULL, 0),\n\t    0, \"Unexpected mallctl() failure\");\n\tsize_t mib[3];\n\tsize_t miblen = sizeof(mib)/sizeof(size_t);\n\n\tassert_d_eq(mallctlnametomib(\"arena.0.dirty_decay_ms\", mib, &miblen),\n\t    0, \"Unexpected mallctlnametomib() failure\");\n\tmib[1] = (size_t)arena_ind;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL,\n\t    (void *)&dirty_decay_ms, sizeof(dirty_decay_ms)), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n\n\tassert_d_eq(mallctlnametomib(\"arena.0.muzzy_decay_ms\", mib, &miblen),\n\t    0, \"Unexpected mallctlnametomib() failure\");\n\tmib[1] = (size_t)arena_ind;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL,\n\t    (void *)&muzzy_decay_ms, sizeof(muzzy_decay_ms)), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n\n\treturn arena_ind;\n}\n\nstatic void\ndo_arena_destroy(unsigned arena_ind) {\n\tsize_t mib[3];\n\tsize_t miblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"arena.0.destroy\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tmib[1] = (size_t)arena_ind;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n}\n\nvoid\ndo_epoch(void) {\n\tuint64_t epoch = 1;\n\tassert_d_eq(mallctl(\"epoch\", NULL, NULL, (void *)&epoch, sizeof(epoch)),\n\t    0, \"Unexpected mallctl() failure\");\n}\n\nvoid\ndo_purge(unsigned arena_ind) {\n\tsize_t mib[3];\n\tsize_t miblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"arena.0.purge\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tmib[1] = (size_t)arena_ind;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n}\n\nvoid\ndo_decay(unsigned arena_ind) {\n\tsize_t mib[3];\n\tsize_t miblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"arena.0.decay\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tmib[1] = (size_t)arena_ind;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n}\n\nstatic uint64_t\nget_arena_npurge_impl(const char *mibname, unsigned arena_ind) {\n\tsize_t mib[4];\n\tsize_t miblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(mibname, mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tmib[2] = (size_t)arena_ind;\n\tuint64_t npurge = 0;\n\tsize_t sz = sizeof(npurge);\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&npurge, &sz, NULL, 0),\n\t    config_stats ? 0 : ENOENT, \"Unexpected mallctlbymib() failure\");\n\treturn npurge;\n}\n\nstatic uint64_t\nget_arena_dirty_npurge(unsigned arena_ind) {\n\tdo_epoch();\n\treturn get_arena_npurge_impl(\"stats.arenas.0.dirty_npurge\", arena_ind);\n}\n\nstatic uint64_t\nget_arena_muzzy_npurge(unsigned arena_ind) {\n\tdo_epoch();\n\treturn get_arena_npurge_impl(\"stats.arenas.0.muzzy_npurge\", arena_ind);\n}\n\nstatic uint64_t\nget_arena_npurge(unsigned arena_ind) {\n\tdo_epoch();\n\treturn get_arena_npurge_impl(\"stats.arenas.0.dirty_npurge\", arena_ind) +\n\t    get_arena_npurge_impl(\"stats.arenas.0.muzzy_npurge\", arena_ind);\n}\n\nstatic size_t\nget_arena_pdirty(unsigned arena_ind) {\n\tdo_epoch();\n\tsize_t mib[4];\n\tsize_t miblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"stats.arenas.0.pdirty\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tmib[2] = (size_t)arena_ind;\n\tsize_t pdirty;\n\tsize_t sz = sizeof(pdirty);\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&pdirty, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n\treturn pdirty;\n}\n\nstatic size_t\nget_arena_pmuzzy(unsigned arena_ind) {\n\tdo_epoch();\n\tsize_t mib[4];\n\tsize_t miblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"stats.arenas.0.pmuzzy\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tmib[2] = (size_t)arena_ind;\n\tsize_t pmuzzy;\n\tsize_t sz = sizeof(pmuzzy);\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&pmuzzy, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n\treturn pmuzzy;\n}\n\nstatic void *\ndo_mallocx(size_t size, int flags) {\n\tvoid *p = mallocx(size, flags);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\treturn p;\n}\n\nstatic void\ngenerate_dirty(unsigned arena_ind, size_t size) {\n\tint flags = MALLOCX_ARENA(arena_ind) | MALLOCX_TCACHE_NONE;\n\tvoid *p = do_mallocx(size, flags);\n\tdallocx(p, flags);\n}\n\nTEST_BEGIN(test_decay_ticks) {\n\ttest_skip_if(check_background_thread_enabled());\n\n\tticker_t *decay_ticker;\n\tunsigned tick0, tick1, arena_ind;\n\tsize_t sz, large0;\n\tvoid *p;\n\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctl(\"arenas.lextent.0.size\", (void *)&large0, &sz, NULL,\n\t    0), 0, \"Unexpected mallctl failure\");\n\n\t/* Set up a manually managed arena for test. */\n\tarena_ind = do_arena_create(0, 0);\n\n\t/* Migrate to the new arena, and get the ticker. */\n\tunsigned old_arena_ind;\n\tsize_t sz_arena_ind = sizeof(old_arena_ind);\n\tassert_d_eq(mallctl(\"thread.arena\", (void *)&old_arena_ind,\n\t    &sz_arena_ind, (void *)&arena_ind, sizeof(arena_ind)), 0,\n\t    \"Unexpected mallctl() failure\");\n\tdecay_ticker = decay_ticker_get(tsd_fetch(), arena_ind);\n\tassert_ptr_not_null(decay_ticker,\n\t    \"Unexpected failure getting decay ticker\");\n\n\t/*\n\t * Test the standard APIs using a large size class, since we can't\n\t * control tcache interactions for small size classes (except by\n\t * completely disabling tcache for the entire test program).\n\t */\n\n\t/* malloc(). */\n\ttick0 = ticker_read(decay_ticker);\n\tp = malloc(large0);\n\tassert_ptr_not_null(p, \"Unexpected malloc() failure\");\n\ttick1 = ticker_read(decay_ticker);\n\tassert_u32_ne(tick1, tick0, \"Expected ticker to tick during malloc()\");\n\t/* free(). */\n\ttick0 = ticker_read(decay_ticker);\n\tfree(p);\n\ttick1 = ticker_read(decay_ticker);\n\tassert_u32_ne(tick1, tick0, \"Expected ticker to tick during free()\");\n\n\t/* calloc(). */\n\ttick0 = ticker_read(decay_ticker);\n\tp = calloc(1, large0);\n\tassert_ptr_not_null(p, \"Unexpected calloc() failure\");\n\ttick1 = ticker_read(decay_ticker);\n\tassert_u32_ne(tick1, tick0, \"Expected ticker to tick during calloc()\");\n\tfree(p);\n\n\t/* posix_memalign(). */\n\ttick0 = ticker_read(decay_ticker);\n\tassert_d_eq(posix_memalign(&p, sizeof(size_t), large0), 0,\n\t    \"Unexpected posix_memalign() failure\");\n\ttick1 = ticker_read(decay_ticker);\n\tassert_u32_ne(tick1, tick0,\n\t    \"Expected ticker to tick during posix_memalign()\");\n\tfree(p);\n\n\t/* aligned_alloc(). */\n\ttick0 = ticker_read(decay_ticker);\n\tp = aligned_alloc(sizeof(size_t), large0);\n\tassert_ptr_not_null(p, \"Unexpected aligned_alloc() failure\");\n\ttick1 = ticker_read(decay_ticker);\n\tassert_u32_ne(tick1, tick0,\n\t    \"Expected ticker to tick during aligned_alloc()\");\n\tfree(p);\n\n\t/* realloc(). */\n\t/* Allocate. */\n\ttick0 = ticker_read(decay_ticker);\n\tp = realloc(NULL, large0);\n\tassert_ptr_not_null(p, \"Unexpected realloc() failure\");\n\ttick1 = ticker_read(decay_ticker);\n\tassert_u32_ne(tick1, tick0, \"Expected ticker to tick during realloc()\");\n\t/* Reallocate. */\n\ttick0 = ticker_read(decay_ticker);\n\tp = realloc(p, large0);\n\tassert_ptr_not_null(p, \"Unexpected realloc() failure\");\n\ttick1 = ticker_read(decay_ticker);\n\tassert_u32_ne(tick1, tick0, \"Expected ticker to tick during realloc()\");\n\t/* Deallocate. */\n\ttick0 = ticker_read(decay_ticker);\n\trealloc(p, 0);\n\ttick1 = ticker_read(decay_ticker);\n\tassert_u32_ne(tick1, tick0, \"Expected ticker to tick during realloc()\");\n\n\t/*\n\t * Test the *allocx() APIs using large and small size classes, with\n\t * tcache explicitly disabled.\n\t */\n\t{\n\t\tunsigned i;\n\t\tsize_t allocx_sizes[2];\n\t\tallocx_sizes[0] = large0;\n\t\tallocx_sizes[1] = 1;\n\n\t\tfor (i = 0; i < sizeof(allocx_sizes) / sizeof(size_t); i++) {\n\t\t\tsz = allocx_sizes[i];\n\n\t\t\t/* mallocx(). */\n\t\t\ttick0 = ticker_read(decay_ticker);\n\t\t\tp = mallocx(sz, MALLOCX_TCACHE_NONE);\n\t\t\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\t\t\ttick1 = ticker_read(decay_ticker);\n\t\t\tassert_u32_ne(tick1, tick0,\n\t\t\t    \"Expected ticker to tick during mallocx() (sz=%zu)\",\n\t\t\t    sz);\n\t\t\t/* rallocx(). */\n\t\t\ttick0 = ticker_read(decay_ticker);\n\t\t\tp = rallocx(p, sz, MALLOCX_TCACHE_NONE);\n\t\t\tassert_ptr_not_null(p, \"Unexpected rallocx() failure\");\n\t\t\ttick1 = ticker_read(decay_ticker);\n\t\t\tassert_u32_ne(tick1, tick0,\n\t\t\t    \"Expected ticker to tick during rallocx() (sz=%zu)\",\n\t\t\t    sz);\n\t\t\t/* xallocx(). */\n\t\t\ttick0 = ticker_read(decay_ticker);\n\t\t\txallocx(p, sz, 0, MALLOCX_TCACHE_NONE);\n\t\t\ttick1 = ticker_read(decay_ticker);\n\t\t\tassert_u32_ne(tick1, tick0,\n\t\t\t    \"Expected ticker to tick during xallocx() (sz=%zu)\",\n\t\t\t    sz);\n\t\t\t/* dallocx(). */\n\t\t\ttick0 = ticker_read(decay_ticker);\n\t\t\tdallocx(p, MALLOCX_TCACHE_NONE);\n\t\t\ttick1 = ticker_read(decay_ticker);\n\t\t\tassert_u32_ne(tick1, tick0,\n\t\t\t    \"Expected ticker to tick during dallocx() (sz=%zu)\",\n\t\t\t    sz);\n\t\t\t/* sdallocx(). */\n\t\t\tp = mallocx(sz, MALLOCX_TCACHE_NONE);\n\t\t\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\t\t\ttick0 = ticker_read(decay_ticker);\n\t\t\tsdallocx(p, sz, MALLOCX_TCACHE_NONE);\n\t\t\ttick1 = ticker_read(decay_ticker);\n\t\t\tassert_u32_ne(tick1, tick0,\n\t\t\t    \"Expected ticker to tick during sdallocx() \"\n\t\t\t    \"(sz=%zu)\", sz);\n\t\t}\n\t}\n\n\t/*\n\t * Test tcache fill/flush interactions for large and small size classes,\n\t * using an explicit tcache.\n\t */\n\tunsigned tcache_ind, i;\n\tsize_t tcache_sizes[2];\n\ttcache_sizes[0] = large0;\n\ttcache_sizes[1] = 1;\n\n\tsize_t tcache_max, sz_tcache_max;\n\tsz_tcache_max = sizeof(tcache_max);\n\tassert_d_eq(mallctl(\"arenas.tcache_max\", (void *)&tcache_max,\n\t    &sz_tcache_max, NULL, 0), 0, \"Unexpected mallctl() failure\");\n\n\tsz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"tcache.create\", (void *)&tcache_ind, &sz,\n\t    NULL, 0), 0, \"Unexpected mallctl failure\");\n\n\tfor (i = 0; i < sizeof(tcache_sizes) / sizeof(size_t); i++) {\n\t\tsz = tcache_sizes[i];\n\n\t\t/* tcache fill. */\n\t\ttick0 = ticker_read(decay_ticker);\n\t\tp = mallocx(sz, MALLOCX_TCACHE(tcache_ind));\n\t\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\t\ttick1 = ticker_read(decay_ticker);\n\t\tassert_u32_ne(tick1, tick0,\n\t\t    \"Expected ticker to tick during tcache fill \"\n\t\t    \"(sz=%zu)\", sz);\n\t\t/* tcache flush. */\n\t\tdallocx(p, MALLOCX_TCACHE(tcache_ind));\n\t\ttick0 = ticker_read(decay_ticker);\n\t\tassert_d_eq(mallctl(\"tcache.flush\", NULL, NULL,\n\t\t    (void *)&tcache_ind, sizeof(unsigned)), 0,\n\t\t    \"Unexpected mallctl failure\");\n\t\ttick1 = ticker_read(decay_ticker);\n\n\t\t/* Will only tick if it's in tcache. */\n\t\tif (sz <= tcache_max) {\n\t\t\tassert_u32_ne(tick1, tick0,\n\t\t\t    \"Expected ticker to tick during tcache \"\n\t\t\t    \"flush (sz=%zu)\", sz);\n\t\t} else {\n\t\t\tassert_u32_eq(tick1, tick0,\n\t\t\t    \"Unexpected ticker tick during tcache \"\n\t\t\t    \"flush (sz=%zu)\", sz);\n\t\t}\n\t}\n}\nTEST_END\n\nstatic void\ndecay_ticker_helper(unsigned arena_ind, int flags, bool dirty, ssize_t dt,\n    uint64_t dirty_npurge0, uint64_t muzzy_npurge0, bool terminate_asap) {\n#define NINTERVALS 101\n\tnstime_t time, update_interval, decay_ms, deadline;\n\n\tnstime_init(&time, 0);\n\tnstime_update(&time);\n\n\tnstime_init2(&decay_ms, dt, 0);\n\tnstime_copy(&deadline, &time);\n\tnstime_add(&deadline, &decay_ms);\n\n\tnstime_init2(&update_interval, dt, 0);\n\tnstime_idivide(&update_interval, NINTERVALS);\n\n\t/*\n\t * Keep q's slab from being deallocated during the looping below.  If a\n\t * cached slab were to repeatedly come and go during looping, it could\n\t * prevent the decay backlog ever becoming empty.\n\t */\n\tvoid *p = do_mallocx(1, flags);\n\tuint64_t dirty_npurge1, muzzy_npurge1;\n\tdo {\n\t\tfor (unsigned i = 0; i < DECAY_NTICKS_PER_UPDATE / 2;\n\t\t    i++) {\n\t\t\tvoid *q = do_mallocx(1, flags);\n\t\t\tdallocx(q, flags);\n\t\t}\n\t\tdirty_npurge1 = get_arena_dirty_npurge(arena_ind);\n\t\tmuzzy_npurge1 = get_arena_muzzy_npurge(arena_ind);\n\n\t\tnstime_add(&time_mock, &update_interval);\n\t\tnstime_update(&time);\n\t} while (nstime_compare(&time, &deadline) <= 0 && ((dirty_npurge1 ==\n\t    dirty_npurge0 && muzzy_npurge1 == muzzy_npurge0) ||\n\t    !terminate_asap));\n\tdallocx(p, flags);\n\n\tif (config_stats) {\n\t\tassert_u64_gt(dirty_npurge1 + muzzy_npurge1, dirty_npurge0 +\n\t\t    muzzy_npurge0, \"Expected purging to occur\");\n\t}\n#undef NINTERVALS\n}\n\nTEST_BEGIN(test_decay_ticker) {\n\ttest_skip_if(check_background_thread_enabled());\n#define NPS 2048\n\tssize_t ddt = opt_dirty_decay_ms;\n\tssize_t mdt = opt_muzzy_decay_ms;\n\tunsigned arena_ind = do_arena_create(ddt, mdt);\n\tint flags = (MALLOCX_ARENA(arena_ind) | MALLOCX_TCACHE_NONE);\n\tvoid *ps[NPS];\n\tsize_t large;\n\n\t/*\n\t * Allocate a bunch of large objects, pause the clock, deallocate every\n\t * other object (to fragment virtual memory), restore the clock, then\n\t * [md]allocx() in a tight loop while advancing time rapidly to verify\n\t * the ticker triggers purging.\n\t */\n\n\tsize_t tcache_max;\n\tsize_t sz = sizeof(size_t);\n\tassert_d_eq(mallctl(\"arenas.tcache_max\", (void *)&tcache_max, &sz, NULL,\n\t    0), 0, \"Unexpected mallctl failure\");\n\tlarge = nallocx(tcache_max + 1, flags);\n\n\tdo_purge(arena_ind);\n\tuint64_t dirty_npurge0 = get_arena_dirty_npurge(arena_ind);\n\tuint64_t muzzy_npurge0 = get_arena_muzzy_npurge(arena_ind);\n\n\tfor (unsigned i = 0; i < NPS; i++) {\n\t\tps[i] = do_mallocx(large, flags);\n\t}\n\n\tnupdates_mock = 0;\n\tnstime_init(&time_mock, 0);\n\tnstime_update(&time_mock);\n\tmonotonic_mock = true;\n\n\tnstime_monotonic_orig = nstime_monotonic;\n\tnstime_update_orig = nstime_update;\n\tnstime_monotonic = nstime_monotonic_mock;\n\tnstime_update = nstime_update_mock;\n\n\tfor (unsigned i = 0; i < NPS; i += 2) {\n\t\tdallocx(ps[i], flags);\n\t\tunsigned nupdates0 = nupdates_mock;\n\t\tdo_decay(arena_ind);\n\t\tassert_u_gt(nupdates_mock, nupdates0,\n\t\t    \"Expected nstime_update() to be called\");\n\t}\n\n\tdecay_ticker_helper(arena_ind, flags, true, ddt, dirty_npurge0,\n\t    muzzy_npurge0, true);\n\tdecay_ticker_helper(arena_ind, flags, false, ddt+mdt, dirty_npurge0,\n\t    muzzy_npurge0, false);\n\n\tdo_arena_destroy(arena_ind);\n\n\tnstime_monotonic = nstime_monotonic_orig;\n\tnstime_update = nstime_update_orig;\n#undef NPS\n}\nTEST_END\n\nTEST_BEGIN(test_decay_nonmonotonic) {\n\ttest_skip_if(check_background_thread_enabled());\n#define NPS (SMOOTHSTEP_NSTEPS + 1)\n\tint flags = (MALLOCX_ARENA(0) | MALLOCX_TCACHE_NONE);\n\tvoid *ps[NPS];\n\tuint64_t npurge0 = 0;\n\tuint64_t npurge1 = 0;\n\tsize_t sz, large0;\n\tunsigned i, nupdates0;\n\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctl(\"arenas.lextent.0.size\", (void *)&large0, &sz, NULL,\n\t    0), 0, \"Unexpected mallctl failure\");\n\n\tassert_d_eq(mallctl(\"arena.0.purge\", NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctl failure\");\n\tdo_epoch();\n\tsz = sizeof(uint64_t);\n\tnpurge0 = get_arena_npurge(0);\n\n\tnupdates_mock = 0;\n\tnstime_init(&time_mock, 0);\n\tnstime_update(&time_mock);\n\tmonotonic_mock = false;\n\n\tnstime_monotonic_orig = nstime_monotonic;\n\tnstime_update_orig = nstime_update;\n\tnstime_monotonic = nstime_monotonic_mock;\n\tnstime_update = nstime_update_mock;\n\n\tfor (i = 0; i < NPS; i++) {\n\t\tps[i] = mallocx(large0, flags);\n\t\tassert_ptr_not_null(ps[i], \"Unexpected mallocx() failure\");\n\t}\n\n\tfor (i = 0; i < NPS; i++) {\n\t\tdallocx(ps[i], flags);\n\t\tnupdates0 = nupdates_mock;\n\t\tassert_d_eq(mallctl(\"arena.0.decay\", NULL, NULL, NULL, 0), 0,\n\t\t    \"Unexpected arena.0.decay failure\");\n\t\tassert_u_gt(nupdates_mock, nupdates0,\n\t\t    \"Expected nstime_update() to be called\");\n\t}\n\n\tdo_epoch();\n\tsz = sizeof(uint64_t);\n\tnpurge1 = get_arena_npurge(0);\n\n\tif (config_stats) {\n\t\tassert_u64_eq(npurge0, npurge1, \"Unexpected purging occurred\");\n\t}\n\n\tnstime_monotonic = nstime_monotonic_orig;\n\tnstime_update = nstime_update_orig;\n#undef NPS\n}\nTEST_END\n\nTEST_BEGIN(test_decay_now) {\n\ttest_skip_if(check_background_thread_enabled());\n\n\tunsigned arena_ind = do_arena_create(0, 0);\n\tassert_zu_eq(get_arena_pdirty(arena_ind), 0, \"Unexpected dirty pages\");\n\tassert_zu_eq(get_arena_pmuzzy(arena_ind), 0, \"Unexpected muzzy pages\");\n\tsize_t sizes[] = {16, PAGE<<2, HUGEPAGE<<2};\n\t/* Verify that dirty/muzzy pages never linger after deallocation. */\n\tfor (unsigned i = 0; i < sizeof(sizes)/sizeof(size_t); i++) {\n\t\tsize_t size = sizes[i];\n\t\tgenerate_dirty(arena_ind, size);\n\t\tassert_zu_eq(get_arena_pdirty(arena_ind), 0,\n\t\t    \"Unexpected dirty pages\");\n\t\tassert_zu_eq(get_arena_pmuzzy(arena_ind), 0,\n\t\t    \"Unexpected muzzy pages\");\n\t}\n\tdo_arena_destroy(arena_ind);\n}\nTEST_END\n\nTEST_BEGIN(test_decay_never) {\n\ttest_skip_if(check_background_thread_enabled());\n\n\tunsigned arena_ind = do_arena_create(-1, -1);\n\tint flags = MALLOCX_ARENA(arena_ind) | MALLOCX_TCACHE_NONE;\n\tassert_zu_eq(get_arena_pdirty(arena_ind), 0, \"Unexpected dirty pages\");\n\tassert_zu_eq(get_arena_pmuzzy(arena_ind), 0, \"Unexpected muzzy pages\");\n\tsize_t sizes[] = {16, PAGE<<2, HUGEPAGE<<2};\n\tvoid *ptrs[sizeof(sizes)/sizeof(size_t)];\n\tfor (unsigned i = 0; i < sizeof(sizes)/sizeof(size_t); i++) {\n\t\tptrs[i] = do_mallocx(sizes[i], flags);\n\t}\n\t/* Verify that each deallocation generates additional dirty pages. */\n\tsize_t pdirty_prev = get_arena_pdirty(arena_ind);\n\tsize_t pmuzzy_prev = get_arena_pmuzzy(arena_ind);\n\tassert_zu_eq(pdirty_prev, 0, \"Unexpected dirty pages\");\n\tassert_zu_eq(pmuzzy_prev, 0, \"Unexpected muzzy pages\");\n\tfor (unsigned i = 0; i < sizeof(sizes)/sizeof(size_t); i++) {\n\t\tdallocx(ptrs[i], flags);\n\t\tsize_t pdirty = get_arena_pdirty(arena_ind);\n\t\tsize_t pmuzzy = get_arena_pmuzzy(arena_ind);\n\t\tassert_zu_gt(pdirty, pdirty_prev,\n\t\t    \"Expected dirty pages to increase.\");\n\t\tassert_zu_eq(pmuzzy, 0, \"Unexpected muzzy pages\");\n\t\tpdirty_prev = pdirty;\n\t}\n\tdo_arena_destroy(arena_ind);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_decay_ticks,\n\t    test_decay_ticker,\n\t    test_decay_nonmonotonic,\n\t    test_decay_now,\n\t    test_decay_never);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/decay.sh",
    "content": "#!/bin/sh\n\nexport MALLOC_CONF=\"dirty_decay_ms:1000,muzzy_decay_ms:1000,lg_tcache_max:0\"\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/extent_quantize.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nTEST_BEGIN(test_small_extent_size) {\n\tunsigned nbins, i;\n\tsize_t sz, extent_size;\n\tsize_t mib[4];\n\tsize_t miblen = sizeof(mib) / sizeof(size_t);\n\n\t/*\n\t * Iterate over all small size classes, get their extent sizes, and\n\t * verify that the quantized size is the same as the extent size.\n\t */\n\n\tsz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.nbins\", (void *)&nbins, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl failure\");\n\n\tassert_d_eq(mallctlnametomib(\"arenas.bin.0.slab_size\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib failure\");\n\tfor (i = 0; i < nbins; i++) {\n\t\tmib[2] = i;\n\t\tsz = sizeof(size_t);\n\t\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&extent_size, &sz,\n\t\t    NULL, 0), 0, \"Unexpected mallctlbymib failure\");\n\t\tassert_zu_eq(extent_size,\n\t\t    extent_size_quantize_floor(extent_size),\n\t\t    \"Small extent quantization should be a no-op \"\n\t\t    \"(extent_size=%zu)\", extent_size);\n\t\tassert_zu_eq(extent_size,\n\t\t    extent_size_quantize_ceil(extent_size),\n\t\t    \"Small extent quantization should be a no-op \"\n\t\t    \"(extent_size=%zu)\", extent_size);\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_large_extent_size) {\n\tbool cache_oblivious;\n\tunsigned nlextents, i;\n\tsize_t sz, extent_size_prev, ceil_prev;\n\tsize_t mib[4];\n\tsize_t miblen = sizeof(mib) / sizeof(size_t);\n\n\t/*\n\t * Iterate over all large size classes, get their extent sizes, and\n\t * verify that the quantized size is the same as the extent size.\n\t */\n\n\tsz = sizeof(bool);\n\tassert_d_eq(mallctl(\"config.cache_oblivious\", (void *)&cache_oblivious,\n\t    &sz, NULL, 0), 0, \"Unexpected mallctl failure\");\n\n\tsz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.nlextents\", (void *)&nlextents, &sz, NULL,\n\t    0), 0, \"Unexpected mallctl failure\");\n\n\tassert_d_eq(mallctlnametomib(\"arenas.lextent.0.size\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib failure\");\n\tfor (i = 0; i < nlextents; i++) {\n\t\tsize_t lextent_size, extent_size, floor, ceil;\n\n\t\tmib[2] = i;\n\t\tsz = sizeof(size_t);\n\t\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&lextent_size,\n\t\t    &sz, NULL, 0), 0, \"Unexpected mallctlbymib failure\");\n\t\textent_size = cache_oblivious ? lextent_size + PAGE :\n\t\t    lextent_size;\n\t\tfloor = extent_size_quantize_floor(extent_size);\n\t\tceil = extent_size_quantize_ceil(extent_size);\n\n\t\tassert_zu_eq(extent_size, floor,\n\t\t    \"Extent quantization should be a no-op for precise size \"\n\t\t    \"(lextent_size=%zu, extent_size=%zu)\", lextent_size,\n\t\t    extent_size);\n\t\tassert_zu_eq(extent_size, ceil,\n\t\t    \"Extent quantization should be a no-op for precise size \"\n\t\t    \"(lextent_size=%zu, extent_size=%zu)\", lextent_size,\n\t\t    extent_size);\n\n\t\tif (i > 0) {\n\t\t\tassert_zu_eq(extent_size_prev,\n\t\t\t    extent_size_quantize_floor(extent_size - PAGE),\n\t\t\t    \"Floor should be a precise size\");\n\t\t\tif (extent_size_prev < ceil_prev) {\n\t\t\t\tassert_zu_eq(ceil_prev, extent_size,\n\t\t\t\t    \"Ceiling should be a precise size \"\n\t\t\t\t    \"(extent_size_prev=%zu, ceil_prev=%zu, \"\n\t\t\t\t    \"extent_size=%zu)\", extent_size_prev,\n\t\t\t\t    ceil_prev, extent_size);\n\t\t\t}\n\t\t}\n\t\tif (i + 1 < nlextents) {\n\t\t\textent_size_prev = floor;\n\t\t\tceil_prev = extent_size_quantize_ceil(extent_size +\n\t\t\t    PAGE);\n\t\t}\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_monotonic) {\n#define SZ_MAX\tZU(4 * 1024 * 1024)\n\tunsigned i;\n\tsize_t floor_prev, ceil_prev;\n\n\tfloor_prev = 0;\n\tceil_prev = 0;\n\tfor (i = 1; i <= SZ_MAX >> LG_PAGE; i++) {\n\t\tsize_t extent_size, floor, ceil;\n\n\t\textent_size = i << LG_PAGE;\n\t\tfloor = extent_size_quantize_floor(extent_size);\n\t\tceil = extent_size_quantize_ceil(extent_size);\n\n\t\tassert_zu_le(floor, extent_size,\n\t\t    \"Floor should be <= (floor=%zu, extent_size=%zu, ceil=%zu)\",\n\t\t    floor, extent_size, ceil);\n\t\tassert_zu_ge(ceil, extent_size,\n\t\t    \"Ceiling should be >= (floor=%zu, extent_size=%zu, \"\n\t\t    \"ceil=%zu)\", floor, extent_size, ceil);\n\n\t\tassert_zu_le(floor_prev, floor, \"Floor should be monotonic \"\n\t\t    \"(floor_prev=%zu, floor=%zu, extent_size=%zu, ceil=%zu)\",\n\t\t    floor_prev, floor, extent_size, ceil);\n\t\tassert_zu_le(ceil_prev, ceil, \"Ceiling should be monotonic \"\n\t\t    \"(floor=%zu, extent_size=%zu, ceil_prev=%zu, ceil=%zu)\",\n\t\t    floor, extent_size, ceil_prev, ceil);\n\n\t\tfloor_prev = floor;\n\t\tceil_prev = ceil;\n\t}\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_small_extent_size,\n\t    test_large_extent_size,\n\t    test_monotonic);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/fork.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#ifndef _WIN32\n#include <sys/wait.h>\n#endif\n\nTEST_BEGIN(test_fork) {\n#ifndef _WIN32\n\tvoid *p;\n\tpid_t pid;\n\n\t/* Set up a manually managed arena for test. */\n\tunsigned arena_ind;\n\tsize_t sz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena_ind, &sz, NULL, 0),\n\t    0, \"Unexpected mallctl() failure\");\n\n\t/* Migrate to the new arena. */\n\tunsigned old_arena_ind;\n\tsz = sizeof(old_arena_ind);\n\tassert_d_eq(mallctl(\"thread.arena\", (void *)&old_arena_ind, &sz,\n\t    (void *)&arena_ind, sizeof(arena_ind)), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tp = malloc(1);\n\tassert_ptr_not_null(p, \"Unexpected malloc() failure\");\n\n\tpid = fork();\n\n\tfree(p);\n\n\tp = malloc(64);\n\tassert_ptr_not_null(p, \"Unexpected malloc() failure\");\n\tfree(p);\n\n\tif (pid == -1) {\n\t\t/* Error. */\n\t\ttest_fail(\"Unexpected fork() failure\");\n\t} else if (pid == 0) {\n\t\t/* Child. */\n\t\t_exit(0);\n\t} else {\n\t\tint status;\n\n\t\t/* Parent. */\n\t\twhile (true) {\n\t\t\tif (waitpid(pid, &status, 0) == -1) {\n\t\t\t\ttest_fail(\"Unexpected waitpid() failure\");\n\t\t\t}\n\t\t\tif (WIFSIGNALED(status)) {\n\t\t\t\ttest_fail(\"Unexpected child termination due to \"\n\t\t\t\t    \"signal %d\", WTERMSIG(status));\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tif (WIFEXITED(status)) {\n\t\t\t\tif (WEXITSTATUS(status) != 0) {\n\t\t\t\t\ttest_fail(\n\t\t\t\t\t    \"Unexpected child exit value %d\",\n\t\t\t\t\t    WEXITSTATUS(status));\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n#else\n\ttest_skip(\"fork(2) is irrelevant to Windows\");\n#endif\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_fork);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/hash.c",
    "content": "/*\n * This file is based on code that is part of SMHasher\n * (https://code.google.com/p/smhasher/), and is subject to the MIT license\n * (http://www.opensource.org/licenses/mit-license.php).  Both email addresses\n * associated with the source code's revision history belong to Austin Appleby,\n * and the revision history ranges from 2010 to 2012.  Therefore the copyright\n * and license are here taken to be:\n *\n * Copyright (c) 2010-2012 Austin Appleby\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in\n * all copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n * THE SOFTWARE.\n */\n\n#include \"test/jemalloc_test.h\"\n#include \"jemalloc/internal/hash.h\"\n\ntypedef enum {\n\thash_variant_x86_32,\n\thash_variant_x86_128,\n\thash_variant_x64_128\n} hash_variant_t;\n\nstatic int\nhash_variant_bits(hash_variant_t variant) {\n\tswitch (variant) {\n\tcase hash_variant_x86_32: return 32;\n\tcase hash_variant_x86_128: return 128;\n\tcase hash_variant_x64_128: return 128;\n\tdefault: not_reached();\n\t}\n}\n\nstatic const char *\nhash_variant_string(hash_variant_t variant) {\n\tswitch (variant) {\n\tcase hash_variant_x86_32: return \"hash_x86_32\";\n\tcase hash_variant_x86_128: return \"hash_x86_128\";\n\tcase hash_variant_x64_128: return \"hash_x64_128\";\n\tdefault: not_reached();\n\t}\n}\n\n#define KEY_SIZE\t256\nstatic void\nhash_variant_verify_key(hash_variant_t variant, uint8_t *key) {\n\tconst int hashbytes = hash_variant_bits(variant) / 8;\n\tconst int hashes_size = hashbytes * 256;\n\tVARIABLE_ARRAY(uint8_t, hashes, hashes_size);\n\tVARIABLE_ARRAY(uint8_t, final, hashbytes);\n\tunsigned i;\n\tuint32_t computed, expected;\n\n\tmemset(key, 0, KEY_SIZE);\n\tmemset(hashes, 0, hashes_size);\n\tmemset(final, 0, hashbytes);\n\n\t/*\n\t * Hash keys of the form {0}, {0,1}, {0,1,2}, ..., {0,1,...,255} as the\n\t * seed.\n\t */\n\tfor (i = 0; i < 256; i++) {\n\t\tkey[i] = (uint8_t)i;\n\t\tswitch (variant) {\n\t\tcase hash_variant_x86_32: {\n\t\t\tuint32_t out;\n\t\t\tout = hash_x86_32(key, i, 256-i);\n\t\t\tmemcpy(&hashes[i*hashbytes], &out, hashbytes);\n\t\t\tbreak;\n\t\t} case hash_variant_x86_128: {\n\t\t\tuint64_t out[2];\n\t\t\thash_x86_128(key, i, 256-i, out);\n\t\t\tmemcpy(&hashes[i*hashbytes], out, hashbytes);\n\t\t\tbreak;\n\t\t} case hash_variant_x64_128: {\n\t\t\tuint64_t out[2];\n\t\t\thash_x64_128(key, i, 256-i, out);\n\t\t\tmemcpy(&hashes[i*hashbytes], out, hashbytes);\n\t\t\tbreak;\n\t\t} default: not_reached();\n\t\t}\n\t}\n\n\t/* Hash the result array. */\n\tswitch (variant) {\n\tcase hash_variant_x86_32: {\n\t\tuint32_t out = hash_x86_32(hashes, hashes_size, 0);\n\t\tmemcpy(final, &out, sizeof(out));\n\t\tbreak;\n\t} case hash_variant_x86_128: {\n\t\tuint64_t out[2];\n\t\thash_x86_128(hashes, hashes_size, 0, out);\n\t\tmemcpy(final, out, sizeof(out));\n\t\tbreak;\n\t} case hash_variant_x64_128: {\n\t\tuint64_t out[2];\n\t\thash_x64_128(hashes, hashes_size, 0, out);\n\t\tmemcpy(final, out, sizeof(out));\n\t\tbreak;\n\t} default: not_reached();\n\t}\n\n\tcomputed = (final[0] << 0) | (final[1] << 8) | (final[2] << 16) |\n\t    (final[3] << 24);\n\n\tswitch (variant) {\n#ifdef JEMALLOC_BIG_ENDIAN\n\tcase hash_variant_x86_32: expected = 0x6213303eU; break;\n\tcase hash_variant_x86_128: expected = 0x266820caU; break;\n\tcase hash_variant_x64_128: expected = 0xcc622b6fU; break;\n#else\n\tcase hash_variant_x86_32: expected = 0xb0f57ee3U; break;\n\tcase hash_variant_x86_128: expected = 0xb3ece62aU; break;\n\tcase hash_variant_x64_128: expected = 0x6384ba69U; break;\n#endif\n\tdefault: not_reached();\n\t}\n\n\tassert_u32_eq(computed, expected,\n\t    \"Hash mismatch for %s(): expected %#x but got %#x\",\n\t    hash_variant_string(variant), expected, computed);\n}\n\nstatic void\nhash_variant_verify(hash_variant_t variant) {\n#define MAX_ALIGN\t16\n\tuint8_t key[KEY_SIZE + (MAX_ALIGN - 1)];\n\tunsigned i;\n\n\tfor (i = 0; i < MAX_ALIGN; i++) {\n\t\thash_variant_verify_key(variant, &key[i]);\n\t}\n#undef MAX_ALIGN\n}\n#undef KEY_SIZE\n\nTEST_BEGIN(test_hash_x86_32) {\n\thash_variant_verify(hash_variant_x86_32);\n}\nTEST_END\n\nTEST_BEGIN(test_hash_x86_128) {\n\thash_variant_verify(hash_variant_x86_128);\n}\nTEST_END\n\nTEST_BEGIN(test_hash_x64_128) {\n\thash_variant_verify(hash_variant_x64_128);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_hash_x86_32,\n\t    test_hash_x86_128,\n\t    test_hash_x64_128);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/hooks.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic bool hook_called = false;\n\nstatic void\nhook() {\n\thook_called = true;\n}\n\nstatic int\nfunc_to_hook(int arg1, int arg2) {\n\treturn arg1 + arg2;\n}\n\n#define func_to_hook JEMALLOC_HOOK(func_to_hook, hooks_libc_hook)\n\nTEST_BEGIN(unhooked_call) {\n\thooks_libc_hook = NULL;\n\thook_called = false;\n\tassert_d_eq(3, func_to_hook(1, 2), \"Hooking changed return value.\");\n\tassert_false(hook_called, \"Nulling out hook didn't take.\");\n}\nTEST_END\n\nTEST_BEGIN(hooked_call) {\n\thooks_libc_hook = &hook;\n\thook_called = false;\n\tassert_d_eq(3, func_to_hook(1, 2), \"Hooking changed return value.\");\n\tassert_true(hook_called, \"Hook should have executed.\");\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    unhooked_call,\n\t    hooked_call);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/junk.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/util.h\"\n\nstatic arena_dalloc_junk_small_t *arena_dalloc_junk_small_orig;\nstatic large_dalloc_junk_t *large_dalloc_junk_orig;\nstatic large_dalloc_maybe_junk_t *large_dalloc_maybe_junk_orig;\nstatic void *watch_for_junking;\nstatic bool saw_junking;\n\nstatic void\nwatch_junking(void *p) {\n\twatch_for_junking = p;\n\tsaw_junking = false;\n}\n\nstatic void\narena_dalloc_junk_small_intercept(void *ptr, const arena_bin_info_t *bin_info) {\n\tsize_t i;\n\n\tarena_dalloc_junk_small_orig(ptr, bin_info);\n\tfor (i = 0; i < bin_info->reg_size; i++) {\n\t\tassert_u_eq(((uint8_t *)ptr)[i], JEMALLOC_FREE_JUNK,\n\t\t    \"Missing junk fill for byte %zu/%zu of deallocated region\",\n\t\t    i, bin_info->reg_size);\n\t}\n\tif (ptr == watch_for_junking) {\n\t\tsaw_junking = true;\n\t}\n}\n\nstatic void\nlarge_dalloc_junk_intercept(void *ptr, size_t usize) {\n\tsize_t i;\n\n\tlarge_dalloc_junk_orig(ptr, usize);\n\tfor (i = 0; i < usize; i++) {\n\t\tassert_u_eq(((uint8_t *)ptr)[i], JEMALLOC_FREE_JUNK,\n\t\t    \"Missing junk fill for byte %zu/%zu of deallocated region\",\n\t\t    i, usize);\n\t}\n\tif (ptr == watch_for_junking) {\n\t\tsaw_junking = true;\n\t}\n}\n\nstatic void\nlarge_dalloc_maybe_junk_intercept(void *ptr, size_t usize) {\n\tlarge_dalloc_maybe_junk_orig(ptr, usize);\n\tif (ptr == watch_for_junking) {\n\t\tsaw_junking = true;\n\t}\n}\n\nstatic void\ntest_junk(size_t sz_min, size_t sz_max) {\n\tuint8_t *s;\n\tsize_t sz_prev, sz, i;\n\n\tif (opt_junk_free) {\n\t\tarena_dalloc_junk_small_orig = arena_dalloc_junk_small;\n\t\tarena_dalloc_junk_small = arena_dalloc_junk_small_intercept;\n\t\tlarge_dalloc_junk_orig = large_dalloc_junk;\n\t\tlarge_dalloc_junk = large_dalloc_junk_intercept;\n\t\tlarge_dalloc_maybe_junk_orig = large_dalloc_maybe_junk;\n\t\tlarge_dalloc_maybe_junk = large_dalloc_maybe_junk_intercept;\n\t}\n\n\tsz_prev = 0;\n\ts = (uint8_t *)mallocx(sz_min, 0);\n\tassert_ptr_not_null((void *)s, \"Unexpected mallocx() failure\");\n\n\tfor (sz = sallocx(s, 0); sz <= sz_max;\n\t    sz_prev = sz, sz = sallocx(s, 0)) {\n\t\tif (sz_prev > 0) {\n\t\t\tassert_u_eq(s[0], 'a',\n\t\t\t    \"Previously allocated byte %zu/%zu is corrupted\",\n\t\t\t    ZU(0), sz_prev);\n\t\t\tassert_u_eq(s[sz_prev-1], 'a',\n\t\t\t    \"Previously allocated byte %zu/%zu is corrupted\",\n\t\t\t    sz_prev-1, sz_prev);\n\t\t}\n\n\t\tfor (i = sz_prev; i < sz; i++) {\n\t\t\tif (opt_junk_alloc) {\n\t\t\t\tassert_u_eq(s[i], JEMALLOC_ALLOC_JUNK,\n\t\t\t\t    \"Newly allocated byte %zu/%zu isn't \"\n\t\t\t\t    \"junk-filled\", i, sz);\n\t\t\t}\n\t\t\ts[i] = 'a';\n\t\t}\n\n\t\tif (xallocx(s, sz+1, 0, 0) == sz) {\n\t\t\tuint8_t *t;\n\t\t\twatch_junking(s);\n\t\t\tt = (uint8_t *)rallocx(s, sz+1, 0);\n\t\t\tassert_ptr_not_null((void *)t,\n\t\t\t    \"Unexpected rallocx() failure\");\n\t\t\tassert_zu_ge(sallocx(t, 0), sz+1,\n\t\t\t    \"Unexpectedly small rallocx() result\");\n\t\t\tif (!background_thread_enabled()) {\n\t\t\t\tassert_ptr_ne(s, t,\n\t\t\t\t    \"Unexpected in-place rallocx()\");\n\t\t\t\tassert_true(!opt_junk_free || saw_junking,\n\t\t\t\t    \"Expected region of size %zu to be \"\n\t\t\t\t    \"junk-filled\", sz);\n\t\t\t}\n\t\t\ts = t;\n\t\t}\n\t}\n\n\twatch_junking(s);\n\tdallocx(s, 0);\n\tassert_true(!opt_junk_free || saw_junking,\n\t    \"Expected region of size %zu to be junk-filled\", sz);\n\n\tif (opt_junk_free) {\n\t\tarena_dalloc_junk_small = arena_dalloc_junk_small_orig;\n\t\tlarge_dalloc_junk = large_dalloc_junk_orig;\n\t\tlarge_dalloc_maybe_junk = large_dalloc_maybe_junk_orig;\n\t}\n}\n\nTEST_BEGIN(test_junk_small) {\n\ttest_skip_if(!config_fill);\n\ttest_junk(1, SMALL_MAXCLASS-1);\n}\nTEST_END\n\nTEST_BEGIN(test_junk_large) {\n\ttest_skip_if(!config_fill);\n\ttest_junk(SMALL_MAXCLASS+1, (1U << (LG_LARGE_MINCLASS+1)));\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_junk_small,\n\t    test_junk_large);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/junk.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_fill}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"abort:false,zero:false,junk:true\"\nfi\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/junk_alloc.c",
    "content": "#include \"junk.c\"\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/junk_alloc.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_fill}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"abort:false,zero:false,junk:alloc\"\nfi\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/junk_free.c",
    "content": "#include \"junk.c\"\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/junk_free.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_fill}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"abort:false,zero:false,junk:free\"\nfi\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/mallctl.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/util.h\"\n\nTEST_BEGIN(test_mallctl_errors) {\n\tuint64_t epoch;\n\tsize_t sz;\n\n\tassert_d_eq(mallctl(\"no_such_name\", NULL, NULL, NULL, 0), ENOENT,\n\t    \"mallctl() should return ENOENT for non-existent names\");\n\n\tassert_d_eq(mallctl(\"version\", NULL, NULL, \"0.0.0\", strlen(\"0.0.0\")),\n\t    EPERM, \"mallctl() should return EPERM on attempt to write \"\n\t    \"read-only value\");\n\n\tassert_d_eq(mallctl(\"epoch\", NULL, NULL, (void *)&epoch,\n\t    sizeof(epoch)-1), EINVAL,\n\t    \"mallctl() should return EINVAL for input size mismatch\");\n\tassert_d_eq(mallctl(\"epoch\", NULL, NULL, (void *)&epoch,\n\t    sizeof(epoch)+1), EINVAL,\n\t    \"mallctl() should return EINVAL for input size mismatch\");\n\n\tsz = sizeof(epoch)-1;\n\tassert_d_eq(mallctl(\"epoch\", (void *)&epoch, &sz, NULL, 0), EINVAL,\n\t    \"mallctl() should return EINVAL for output size mismatch\");\n\tsz = sizeof(epoch)+1;\n\tassert_d_eq(mallctl(\"epoch\", (void *)&epoch, &sz, NULL, 0), EINVAL,\n\t    \"mallctl() should return EINVAL for output size mismatch\");\n}\nTEST_END\n\nTEST_BEGIN(test_mallctlnametomib_errors) {\n\tsize_t mib[1];\n\tsize_t miblen;\n\n\tmiblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"no_such_name\", mib, &miblen), ENOENT,\n\t    \"mallctlnametomib() should return ENOENT for non-existent names\");\n}\nTEST_END\n\nTEST_BEGIN(test_mallctlbymib_errors) {\n\tuint64_t epoch;\n\tsize_t sz;\n\tsize_t mib[1];\n\tsize_t miblen;\n\n\tmiblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"version\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, \"0.0.0\",\n\t    strlen(\"0.0.0\")), EPERM, \"mallctl() should return EPERM on \"\n\t    \"attempt to write read-only value\");\n\n\tmiblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"epoch\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, (void *)&epoch,\n\t    sizeof(epoch)-1), EINVAL,\n\t    \"mallctlbymib() should return EINVAL for input size mismatch\");\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, (void *)&epoch,\n\t    sizeof(epoch)+1), EINVAL,\n\t    \"mallctlbymib() should return EINVAL for input size mismatch\");\n\n\tsz = sizeof(epoch)-1;\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&epoch, &sz, NULL, 0),\n\t    EINVAL,\n\t    \"mallctlbymib() should return EINVAL for output size mismatch\");\n\tsz = sizeof(epoch)+1;\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&epoch, &sz, NULL, 0),\n\t    EINVAL,\n\t    \"mallctlbymib() should return EINVAL for output size mismatch\");\n}\nTEST_END\n\nTEST_BEGIN(test_mallctl_read_write) {\n\tuint64_t old_epoch, new_epoch;\n\tsize_t sz = sizeof(old_epoch);\n\n\t/* Blind. */\n\tassert_d_eq(mallctl(\"epoch\", NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\tassert_zu_eq(sz, sizeof(old_epoch), \"Unexpected output size\");\n\n\t/* Read. */\n\tassert_d_eq(mallctl(\"epoch\", (void *)&old_epoch, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\tassert_zu_eq(sz, sizeof(old_epoch), \"Unexpected output size\");\n\n\t/* Write. */\n\tassert_d_eq(mallctl(\"epoch\", NULL, NULL, (void *)&new_epoch,\n\t    sizeof(new_epoch)), 0, \"Unexpected mallctl() failure\");\n\tassert_zu_eq(sz, sizeof(old_epoch), \"Unexpected output size\");\n\n\t/* Read+write. */\n\tassert_d_eq(mallctl(\"epoch\", (void *)&old_epoch, &sz,\n\t    (void *)&new_epoch, sizeof(new_epoch)), 0,\n\t    \"Unexpected mallctl() failure\");\n\tassert_zu_eq(sz, sizeof(old_epoch), \"Unexpected output size\");\n}\nTEST_END\n\nTEST_BEGIN(test_mallctlnametomib_short_mib) {\n\tsize_t mib[4];\n\tsize_t miblen;\n\n\tmiblen = 3;\n\tmib[3] = 42;\n\tassert_d_eq(mallctlnametomib(\"arenas.bin.0.nregs\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tassert_zu_eq(miblen, 3, \"Unexpected mib output length\");\n\tassert_zu_eq(mib[3], 42,\n\t    \"mallctlnametomib() wrote past the end of the input mib\");\n}\nTEST_END\n\nTEST_BEGIN(test_mallctl_config) {\n#define TEST_MALLCTL_CONFIG(config, t) do {\t\t\t\t\\\n\tt oldval;\t\t\t\t\t\t\t\\\n\tsize_t sz = sizeof(oldval);\t\t\t\t\t\\\n\tassert_d_eq(mallctl(\"config.\"#config, (void *)&oldval, &sz,\t\\\n\t    NULL, 0), 0, \"Unexpected mallctl() failure\");\t\t\\\n\tassert_b_eq(oldval, config_##config, \"Incorrect config value\");\t\\\n\tassert_zu_eq(sz, sizeof(oldval), \"Unexpected output size\");\t\\\n} while (0)\n\n\tTEST_MALLCTL_CONFIG(cache_oblivious, bool);\n\tTEST_MALLCTL_CONFIG(debug, bool);\n\tTEST_MALLCTL_CONFIG(fill, bool);\n\tTEST_MALLCTL_CONFIG(lazy_lock, bool);\n\tTEST_MALLCTL_CONFIG(malloc_conf, const char *);\n\tTEST_MALLCTL_CONFIG(prof, bool);\n\tTEST_MALLCTL_CONFIG(prof_libgcc, bool);\n\tTEST_MALLCTL_CONFIG(prof_libunwind, bool);\n\tTEST_MALLCTL_CONFIG(stats, bool);\n\tTEST_MALLCTL_CONFIG(utrace, bool);\n\tTEST_MALLCTL_CONFIG(xmalloc, bool);\n\n#undef TEST_MALLCTL_CONFIG\n}\nTEST_END\n\nTEST_BEGIN(test_mallctl_opt) {\n\tbool config_always = true;\n\n#define TEST_MALLCTL_OPT(t, opt, config) do {\t\t\t\t\\\n\tt oldval;\t\t\t\t\t\t\t\\\n\tsize_t sz = sizeof(oldval);\t\t\t\t\t\\\n\tint expected = config_##config ? 0 : ENOENT;\t\t\t\\\n\tint result = mallctl(\"opt.\"#opt, (void *)&oldval, &sz, NULL,\t\\\n\t    0);\t\t\t\t\t\t\t\t\\\n\tassert_d_eq(result, expected,\t\t\t\t\t\\\n\t    \"Unexpected mallctl() result for opt.\"#opt);\t\t\\\n\tassert_zu_eq(sz, sizeof(oldval), \"Unexpected output size\");\t\\\n} while (0)\n\n\tTEST_MALLCTL_OPT(bool, abort, always);\n\tTEST_MALLCTL_OPT(bool, retain, always);\n\tTEST_MALLCTL_OPT(const char *, dss, always);\n\tTEST_MALLCTL_OPT(unsigned, narenas, always);\n\tTEST_MALLCTL_OPT(const char *, percpu_arena, always);\n\tTEST_MALLCTL_OPT(bool, background_thread, always);\n\tTEST_MALLCTL_OPT(ssize_t, dirty_decay_ms, always);\n\tTEST_MALLCTL_OPT(ssize_t, muzzy_decay_ms, always);\n\tTEST_MALLCTL_OPT(bool, stats_print, always);\n\tTEST_MALLCTL_OPT(const char *, junk, fill);\n\tTEST_MALLCTL_OPT(bool, zero, fill);\n\tTEST_MALLCTL_OPT(bool, utrace, utrace);\n\tTEST_MALLCTL_OPT(bool, xmalloc, xmalloc);\n\tTEST_MALLCTL_OPT(bool, tcache, always);\n\tTEST_MALLCTL_OPT(size_t, lg_tcache_max, always);\n\tTEST_MALLCTL_OPT(bool, prof, prof);\n\tTEST_MALLCTL_OPT(const char *, prof_prefix, prof);\n\tTEST_MALLCTL_OPT(bool, prof_active, prof);\n\tTEST_MALLCTL_OPT(ssize_t, lg_prof_sample, prof);\n\tTEST_MALLCTL_OPT(bool, prof_accum, prof);\n\tTEST_MALLCTL_OPT(ssize_t, lg_prof_interval, prof);\n\tTEST_MALLCTL_OPT(bool, prof_gdump, prof);\n\tTEST_MALLCTL_OPT(bool, prof_final, prof);\n\tTEST_MALLCTL_OPT(bool, prof_leak, prof);\n\n#undef TEST_MALLCTL_OPT\n}\nTEST_END\n\nTEST_BEGIN(test_manpage_example) {\n\tunsigned nbins, i;\n\tsize_t mib[4];\n\tsize_t len, miblen;\n\n\tlen = sizeof(nbins);\n\tassert_d_eq(mallctl(\"arenas.nbins\", (void *)&nbins, &len, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tmiblen = 4;\n\tassert_d_eq(mallctlnametomib(\"arenas.bin.0.size\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tfor (i = 0; i < nbins; i++) {\n\t\tsize_t bin_size;\n\n\t\tmib[2] = i;\n\t\tlen = sizeof(bin_size);\n\t\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&bin_size, &len,\n\t\t    NULL, 0), 0, \"Unexpected mallctlbymib() failure\");\n\t\t/* Do something with bin_size... */\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_tcache_none) {\n\ttest_skip_if(!opt_tcache);\n\n\t/* Allocate p and q. */\n\tvoid *p0 = mallocx(42, 0);\n\tassert_ptr_not_null(p0, \"Unexpected mallocx() failure\");\n\tvoid *q = mallocx(42, 0);\n\tassert_ptr_not_null(q, \"Unexpected mallocx() failure\");\n\n\t/* Deallocate p and q, but bypass the tcache for q. */\n\tdallocx(p0, 0);\n\tdallocx(q, MALLOCX_TCACHE_NONE);\n\n\t/* Make sure that tcache-based allocation returns p, not q. */\n\tvoid *p1 = mallocx(42, 0);\n\tassert_ptr_not_null(p1, \"Unexpected mallocx() failure\");\n\tassert_ptr_eq(p0, p1, \"Expected tcache to allocate cached region\");\n\n\t/* Clean up. */\n\tdallocx(p1, MALLOCX_TCACHE_NONE);\n}\nTEST_END\n\nTEST_BEGIN(test_tcache) {\n#define NTCACHES\t10\n\tunsigned tis[NTCACHES];\n\tvoid *ps[NTCACHES];\n\tvoid *qs[NTCACHES];\n\tunsigned i;\n\tsize_t sz, psz, qsz;\n\n\tpsz = 42;\n\tqsz = nallocx(psz, 0) + 1;\n\n\t/* Create tcaches. */\n\tfor (i = 0; i < NTCACHES; i++) {\n\t\tsz = sizeof(unsigned);\n\t\tassert_d_eq(mallctl(\"tcache.create\", (void *)&tis[i], &sz, NULL,\n\t\t    0), 0, \"Unexpected mallctl() failure, i=%u\", i);\n\t}\n\n\t/* Exercise tcache ID recycling. */\n\tfor (i = 0; i < NTCACHES; i++) {\n\t\tassert_d_eq(mallctl(\"tcache.destroy\", NULL, NULL,\n\t\t    (void *)&tis[i], sizeof(unsigned)), 0,\n\t\t    \"Unexpected mallctl() failure, i=%u\", i);\n\t}\n\tfor (i = 0; i < NTCACHES; i++) {\n\t\tsz = sizeof(unsigned);\n\t\tassert_d_eq(mallctl(\"tcache.create\", (void *)&tis[i], &sz, NULL,\n\t\t    0), 0, \"Unexpected mallctl() failure, i=%u\", i);\n\t}\n\n\t/* Flush empty tcaches. */\n\tfor (i = 0; i < NTCACHES; i++) {\n\t\tassert_d_eq(mallctl(\"tcache.flush\", NULL, NULL, (void *)&tis[i],\n\t\t    sizeof(unsigned)), 0, \"Unexpected mallctl() failure, i=%u\",\n\t\t    i);\n\t}\n\n\t/* Cache some allocations. */\n\tfor (i = 0; i < NTCACHES; i++) {\n\t\tps[i] = mallocx(psz, MALLOCX_TCACHE(tis[i]));\n\t\tassert_ptr_not_null(ps[i], \"Unexpected mallocx() failure, i=%u\",\n\t\t    i);\n\t\tdallocx(ps[i], MALLOCX_TCACHE(tis[i]));\n\n\t\tqs[i] = mallocx(qsz, MALLOCX_TCACHE(tis[i]));\n\t\tassert_ptr_not_null(qs[i], \"Unexpected mallocx() failure, i=%u\",\n\t\t    i);\n\t\tdallocx(qs[i], MALLOCX_TCACHE(tis[i]));\n\t}\n\n\t/* Verify that tcaches allocate cached regions. */\n\tfor (i = 0; i < NTCACHES; i++) {\n\t\tvoid *p0 = ps[i];\n\t\tps[i] = mallocx(psz, MALLOCX_TCACHE(tis[i]));\n\t\tassert_ptr_not_null(ps[i], \"Unexpected mallocx() failure, i=%u\",\n\t\t    i);\n\t\tassert_ptr_eq(ps[i], p0,\n\t\t    \"Expected mallocx() to allocate cached region, i=%u\", i);\n\t}\n\n\t/* Verify that reallocation uses cached regions. */\n\tfor (i = 0; i < NTCACHES; i++) {\n\t\tvoid *q0 = qs[i];\n\t\tqs[i] = rallocx(ps[i], qsz, MALLOCX_TCACHE(tis[i]));\n\t\tassert_ptr_not_null(qs[i], \"Unexpected rallocx() failure, i=%u\",\n\t\t    i);\n\t\tassert_ptr_eq(qs[i], q0,\n\t\t    \"Expected rallocx() to allocate cached region, i=%u\", i);\n\t\t/* Avoid undefined behavior in case of test failure. */\n\t\tif (qs[i] == NULL) {\n\t\t\tqs[i] = ps[i];\n\t\t}\n\t}\n\tfor (i = 0; i < NTCACHES; i++) {\n\t\tdallocx(qs[i], MALLOCX_TCACHE(tis[i]));\n\t}\n\n\t/* Flush some non-empty tcaches. */\n\tfor (i = 0; i < NTCACHES/2; i++) {\n\t\tassert_d_eq(mallctl(\"tcache.flush\", NULL, NULL, (void *)&tis[i],\n\t\t    sizeof(unsigned)), 0, \"Unexpected mallctl() failure, i=%u\",\n\t\t    i);\n\t}\n\n\t/* Destroy tcaches. */\n\tfor (i = 0; i < NTCACHES; i++) {\n\t\tassert_d_eq(mallctl(\"tcache.destroy\", NULL, NULL,\n\t\t    (void *)&tis[i], sizeof(unsigned)), 0,\n\t\t    \"Unexpected mallctl() failure, i=%u\", i);\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_thread_arena) {\n\tunsigned old_arena_ind, new_arena_ind, narenas;\n\n\tconst char *opa;\n\tsize_t sz = sizeof(opa);\n\tassert_d_eq(mallctl(\"opt.percpu_arena\", &opa, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tsz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.narenas\", (void *)&narenas, &sz, NULL, 0),\n\t    0, \"Unexpected mallctl() failure\");\n\tassert_u_eq(narenas, opt_narenas, \"Number of arenas incorrect\");\n\n\tif (strcmp(opa, \"disabled\") == 0) {\n\t\tnew_arena_ind = narenas - 1;\n\t\tassert_d_eq(mallctl(\"thread.arena\", (void *)&old_arena_ind, &sz,\n\t\t    (void *)&new_arena_ind, sizeof(unsigned)), 0,\n\t\t    \"Unexpected mallctl() failure\");\n\t\tnew_arena_ind = 0;\n\t\tassert_d_eq(mallctl(\"thread.arena\", (void *)&old_arena_ind, &sz,\n\t\t    (void *)&new_arena_ind, sizeof(unsigned)), 0,\n\t\t    \"Unexpected mallctl() failure\");\n\t} else {\n\t\tassert_d_eq(mallctl(\"thread.arena\", (void *)&old_arena_ind, &sz,\n\t\t    NULL, 0), 0, \"Unexpected mallctl() failure\");\n\t\tnew_arena_ind = percpu_arena_ind_limit(opt_percpu_arena) - 1;\n\t\tif (old_arena_ind != new_arena_ind) {\n\t\t\tassert_d_eq(mallctl(\"thread.arena\",\n\t\t\t    (void *)&old_arena_ind, &sz, (void *)&new_arena_ind,\n\t\t\t    sizeof(unsigned)), EPERM, \"thread.arena ctl \"\n\t\t\t    \"should not be allowed with percpu arena\");\n\t\t}\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_arena_i_initialized) {\n\tunsigned narenas, i;\n\tsize_t sz;\n\tsize_t mib[3];\n\tsize_t miblen = sizeof(mib) / sizeof(size_t);\n\tbool initialized;\n\n\tsz = sizeof(narenas);\n\tassert_d_eq(mallctl(\"arenas.narenas\", (void *)&narenas, &sz, NULL, 0),\n\t    0, \"Unexpected mallctl() failure\");\n\n\tassert_d_eq(mallctlnametomib(\"arena.0.initialized\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tfor (i = 0; i < narenas; i++) {\n\t\tmib[1] = i;\n\t\tsz = sizeof(initialized);\n\t\tassert_d_eq(mallctlbymib(mib, miblen, &initialized, &sz, NULL,\n\t\t    0), 0, \"Unexpected mallctl() failure\");\n\t}\n\n\tmib[1] = MALLCTL_ARENAS_ALL;\n\tsz = sizeof(initialized);\n\tassert_d_eq(mallctlbymib(mib, miblen, &initialized, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\tassert_true(initialized,\n\t    \"Merged arena statistics should always be initialized\");\n\n\t/* Equivalent to the above but using mallctl() directly. */\n\tsz = sizeof(initialized);\n\tassert_d_eq(mallctl(\n\t    \"arena.\" STRINGIFY(MALLCTL_ARENAS_ALL) \".initialized\",\n\t    (void *)&initialized, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\tassert_true(initialized,\n\t    \"Merged arena statistics should always be initialized\");\n}\nTEST_END\n\nTEST_BEGIN(test_arena_i_dirty_decay_ms) {\n\tssize_t dirty_decay_ms, orig_dirty_decay_ms, prev_dirty_decay_ms;\n\tsize_t sz = sizeof(ssize_t);\n\n\tassert_d_eq(mallctl(\"arena.0.dirty_decay_ms\",\n\t    (void *)&orig_dirty_decay_ms, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tdirty_decay_ms = -2;\n\tassert_d_eq(mallctl(\"arena.0.dirty_decay_ms\", NULL, NULL,\n\t    (void *)&dirty_decay_ms, sizeof(ssize_t)), EFAULT,\n\t    \"Unexpected mallctl() success\");\n\n\tdirty_decay_ms = 0x7fffffff;\n\tassert_d_eq(mallctl(\"arena.0.dirty_decay_ms\", NULL, NULL,\n\t    (void *)&dirty_decay_ms, sizeof(ssize_t)), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tfor (prev_dirty_decay_ms = dirty_decay_ms, dirty_decay_ms = -1;\n\t    dirty_decay_ms < 20; prev_dirty_decay_ms = dirty_decay_ms,\n\t    dirty_decay_ms++) {\n\t\tssize_t old_dirty_decay_ms;\n\n\t\tassert_d_eq(mallctl(\"arena.0.dirty_decay_ms\",\n\t\t    (void *)&old_dirty_decay_ms, &sz, (void *)&dirty_decay_ms,\n\t\t    sizeof(ssize_t)), 0, \"Unexpected mallctl() failure\");\n\t\tassert_zd_eq(old_dirty_decay_ms, prev_dirty_decay_ms,\n\t\t    \"Unexpected old arena.0.dirty_decay_ms\");\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_arena_i_muzzy_decay_ms) {\n\tssize_t muzzy_decay_ms, orig_muzzy_decay_ms, prev_muzzy_decay_ms;\n\tsize_t sz = sizeof(ssize_t);\n\n\tassert_d_eq(mallctl(\"arena.0.muzzy_decay_ms\",\n\t    (void *)&orig_muzzy_decay_ms, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tmuzzy_decay_ms = -2;\n\tassert_d_eq(mallctl(\"arena.0.muzzy_decay_ms\", NULL, NULL,\n\t    (void *)&muzzy_decay_ms, sizeof(ssize_t)), EFAULT,\n\t    \"Unexpected mallctl() success\");\n\n\tmuzzy_decay_ms = 0x7fffffff;\n\tassert_d_eq(mallctl(\"arena.0.muzzy_decay_ms\", NULL, NULL,\n\t    (void *)&muzzy_decay_ms, sizeof(ssize_t)), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tfor (prev_muzzy_decay_ms = muzzy_decay_ms, muzzy_decay_ms = -1;\n\t    muzzy_decay_ms < 20; prev_muzzy_decay_ms = muzzy_decay_ms,\n\t    muzzy_decay_ms++) {\n\t\tssize_t old_muzzy_decay_ms;\n\n\t\tassert_d_eq(mallctl(\"arena.0.muzzy_decay_ms\",\n\t\t    (void *)&old_muzzy_decay_ms, &sz, (void *)&muzzy_decay_ms,\n\t\t    sizeof(ssize_t)), 0, \"Unexpected mallctl() failure\");\n\t\tassert_zd_eq(old_muzzy_decay_ms, prev_muzzy_decay_ms,\n\t\t    \"Unexpected old arena.0.muzzy_decay_ms\");\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_arena_i_purge) {\n\tunsigned narenas;\n\tsize_t sz = sizeof(unsigned);\n\tsize_t mib[3];\n\tsize_t miblen = 3;\n\n\tassert_d_eq(mallctl(\"arena.0.purge\", NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tassert_d_eq(mallctl(\"arenas.narenas\", (void *)&narenas, &sz, NULL, 0),\n\t    0, \"Unexpected mallctl() failure\");\n\tassert_d_eq(mallctlnametomib(\"arena.0.purge\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tmib[1] = narenas;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n\n\tmib[1] = MALLCTL_ARENAS_ALL;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n}\nTEST_END\n\nTEST_BEGIN(test_arena_i_decay) {\n\tunsigned narenas;\n\tsize_t sz = sizeof(unsigned);\n\tsize_t mib[3];\n\tsize_t miblen = 3;\n\n\tassert_d_eq(mallctl(\"arena.0.decay\", NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tassert_d_eq(mallctl(\"arenas.narenas\", (void *)&narenas, &sz, NULL, 0),\n\t    0, \"Unexpected mallctl() failure\");\n\tassert_d_eq(mallctlnametomib(\"arena.0.decay\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tmib[1] = narenas;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n\n\tmib[1] = MALLCTL_ARENAS_ALL;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n}\nTEST_END\n\nTEST_BEGIN(test_arena_i_dss) {\n\tconst char *dss_prec_old, *dss_prec_new;\n\tsize_t sz = sizeof(dss_prec_old);\n\tsize_t mib[3];\n\tsize_t miblen;\n\n\tmiblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"arena.0.dss\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() error\");\n\n\tdss_prec_new = \"disabled\";\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&dss_prec_old, &sz,\n\t    (void *)&dss_prec_new, sizeof(dss_prec_new)), 0,\n\t    \"Unexpected mallctl() failure\");\n\tassert_str_ne(dss_prec_old, \"primary\",\n\t    \"Unexpected default for dss precedence\");\n\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&dss_prec_new, &sz,\n\t    (void *)&dss_prec_old, sizeof(dss_prec_old)), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&dss_prec_old, &sz, NULL,\n\t    0), 0, \"Unexpected mallctl() failure\");\n\tassert_str_ne(dss_prec_old, \"primary\",\n\t    \"Unexpected value for dss precedence\");\n\n\tmib[1] = narenas_total_get();\n\tdss_prec_new = \"disabled\";\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&dss_prec_old, &sz,\n\t    (void *)&dss_prec_new, sizeof(dss_prec_new)), 0,\n\t    \"Unexpected mallctl() failure\");\n\tassert_str_ne(dss_prec_old, \"primary\",\n\t    \"Unexpected default for dss precedence\");\n\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&dss_prec_new, &sz,\n\t    (void *)&dss_prec_old, sizeof(dss_prec_new)), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&dss_prec_old, &sz, NULL,\n\t    0), 0, \"Unexpected mallctl() failure\");\n\tassert_str_ne(dss_prec_old, \"primary\",\n\t    \"Unexpected value for dss precedence\");\n}\nTEST_END\n\nTEST_BEGIN(test_arenas_dirty_decay_ms) {\n\tssize_t dirty_decay_ms, orig_dirty_decay_ms, prev_dirty_decay_ms;\n\tsize_t sz = sizeof(ssize_t);\n\n\tassert_d_eq(mallctl(\"arenas.dirty_decay_ms\",\n\t    (void *)&orig_dirty_decay_ms, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tdirty_decay_ms = -2;\n\tassert_d_eq(mallctl(\"arenas.dirty_decay_ms\", NULL, NULL,\n\t    (void *)&dirty_decay_ms, sizeof(ssize_t)), EFAULT,\n\t    \"Unexpected mallctl() success\");\n\n\tdirty_decay_ms = 0x7fffffff;\n\tassert_d_eq(mallctl(\"arenas.dirty_decay_ms\", NULL, NULL,\n\t    (void *)&dirty_decay_ms, sizeof(ssize_t)), 0,\n\t    \"Expected mallctl() failure\");\n\n\tfor (prev_dirty_decay_ms = dirty_decay_ms, dirty_decay_ms = -1;\n\t    dirty_decay_ms < 20; prev_dirty_decay_ms = dirty_decay_ms,\n\t    dirty_decay_ms++) {\n\t\tssize_t old_dirty_decay_ms;\n\n\t\tassert_d_eq(mallctl(\"arenas.dirty_decay_ms\",\n\t\t    (void *)&old_dirty_decay_ms, &sz, (void *)&dirty_decay_ms,\n\t\t    sizeof(ssize_t)), 0, \"Unexpected mallctl() failure\");\n\t\tassert_zd_eq(old_dirty_decay_ms, prev_dirty_decay_ms,\n\t\t    \"Unexpected old arenas.dirty_decay_ms\");\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_arenas_muzzy_decay_ms) {\n\tssize_t muzzy_decay_ms, orig_muzzy_decay_ms, prev_muzzy_decay_ms;\n\tsize_t sz = sizeof(ssize_t);\n\n\tassert_d_eq(mallctl(\"arenas.muzzy_decay_ms\",\n\t    (void *)&orig_muzzy_decay_ms, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tmuzzy_decay_ms = -2;\n\tassert_d_eq(mallctl(\"arenas.muzzy_decay_ms\", NULL, NULL,\n\t    (void *)&muzzy_decay_ms, sizeof(ssize_t)), EFAULT,\n\t    \"Unexpected mallctl() success\");\n\n\tmuzzy_decay_ms = 0x7fffffff;\n\tassert_d_eq(mallctl(\"arenas.muzzy_decay_ms\", NULL, NULL,\n\t    (void *)&muzzy_decay_ms, sizeof(ssize_t)), 0,\n\t    \"Expected mallctl() failure\");\n\n\tfor (prev_muzzy_decay_ms = muzzy_decay_ms, muzzy_decay_ms = -1;\n\t    muzzy_decay_ms < 20; prev_muzzy_decay_ms = muzzy_decay_ms,\n\t    muzzy_decay_ms++) {\n\t\tssize_t old_muzzy_decay_ms;\n\n\t\tassert_d_eq(mallctl(\"arenas.muzzy_decay_ms\",\n\t\t    (void *)&old_muzzy_decay_ms, &sz, (void *)&muzzy_decay_ms,\n\t\t    sizeof(ssize_t)), 0, \"Unexpected mallctl() failure\");\n\t\tassert_zd_eq(old_muzzy_decay_ms, prev_muzzy_decay_ms,\n\t\t    \"Unexpected old arenas.muzzy_decay_ms\");\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_arenas_constants) {\n#define TEST_ARENAS_CONSTANT(t, name, expected) do {\t\t\t\\\n\tt name;\t\t\t\t\t\t\t\t\\\n\tsize_t sz = sizeof(t);\t\t\t\t\t\t\\\n\tassert_d_eq(mallctl(\"arenas.\"#name, (void *)&name, &sz, NULL,\t\\\n\t    0), 0, \"Unexpected mallctl() failure\");\t\t\t\\\n\tassert_zu_eq(name, expected, \"Incorrect \"#name\" size\");\t\t\\\n} while (0)\n\n\tTEST_ARENAS_CONSTANT(size_t, quantum, QUANTUM);\n\tTEST_ARENAS_CONSTANT(size_t, page, PAGE);\n\tTEST_ARENAS_CONSTANT(unsigned, nbins, NBINS);\n\tTEST_ARENAS_CONSTANT(unsigned, nlextents, NSIZES - NBINS);\n\n#undef TEST_ARENAS_CONSTANT\n}\nTEST_END\n\nTEST_BEGIN(test_arenas_bin_constants) {\n#define TEST_ARENAS_BIN_CONSTANT(t, name, expected) do {\t\t\\\n\tt name;\t\t\t\t\t\t\t\t\\\n\tsize_t sz = sizeof(t);\t\t\t\t\t\t\\\n\tassert_d_eq(mallctl(\"arenas.bin.0.\"#name, (void *)&name, &sz,\t\\\n\t    NULL, 0), 0, \"Unexpected mallctl() failure\");\t\t\\\n\tassert_zu_eq(name, expected, \"Incorrect \"#name\" size\");\t\t\\\n} while (0)\n\n\tTEST_ARENAS_BIN_CONSTANT(size_t, size, arena_bin_info[0].reg_size);\n\tTEST_ARENAS_BIN_CONSTANT(uint32_t, nregs, arena_bin_info[0].nregs);\n\tTEST_ARENAS_BIN_CONSTANT(size_t, slab_size,\n\t    arena_bin_info[0].slab_size);\n\n#undef TEST_ARENAS_BIN_CONSTANT\n}\nTEST_END\n\nTEST_BEGIN(test_arenas_lextent_constants) {\n#define TEST_ARENAS_LEXTENT_CONSTANT(t, name, expected) do {\t\t\\\n\tt name;\t\t\t\t\t\t\t\t\\\n\tsize_t sz = sizeof(t);\t\t\t\t\t\t\\\n\tassert_d_eq(mallctl(\"arenas.lextent.0.\"#name, (void *)&name,\t\\\n\t    &sz, NULL, 0), 0, \"Unexpected mallctl() failure\");\t\t\\\n\tassert_zu_eq(name, expected, \"Incorrect \"#name\" size\");\t\t\\\n} while (0)\n\n\tTEST_ARENAS_LEXTENT_CONSTANT(size_t, size, LARGE_MINCLASS);\n\n#undef TEST_ARENAS_LEXTENT_CONSTANT\n}\nTEST_END\n\nTEST_BEGIN(test_arenas_create) {\n\tunsigned narenas_before, arena, narenas_after;\n\tsize_t sz = sizeof(unsigned);\n\n\tassert_d_eq(mallctl(\"arenas.narenas\", (void *)&narenas_before, &sz,\n\t    NULL, 0), 0, \"Unexpected mallctl() failure\");\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\tassert_d_eq(mallctl(\"arenas.narenas\", (void *)&narenas_after, &sz, NULL,\n\t    0), 0, \"Unexpected mallctl() failure\");\n\n\tassert_u_eq(narenas_before+1, narenas_after,\n\t    \"Unexpected number of arenas before versus after extension\");\n\tassert_u_eq(arena, narenas_after-1, \"Unexpected arena index\");\n}\nTEST_END\n\nTEST_BEGIN(test_arenas_lookup) {\n\tunsigned arena, arena1;\n\tvoid *ptr;\n\tsize_t sz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\tptr = mallocx(42, MALLOCX_ARENA(arena) | MALLOCX_TCACHE_NONE);\n\tassert_ptr_not_null(ptr, \"Unexpected mallocx() failure\");\n\tassert_d_eq(mallctl(\"arenas.lookup\", &arena1, &sz, &ptr, sizeof(ptr)),\n\t    0, \"Unexpected mallctl() failure\");\n\tassert_u_eq(arena, arena1, \"Unexpected arena index\");\n\tdallocx(ptr, 0);\n}\nTEST_END\n\nTEST_BEGIN(test_stats_arenas) {\n#define TEST_STATS_ARENAS(t, name) do {\t\t\t\t\t\\\n\tt name;\t\t\t\t\t\t\t\t\\\n\tsize_t sz = sizeof(t);\t\t\t\t\t\t\\\n\tassert_d_eq(mallctl(\"stats.arenas.0.\"#name, (void *)&name, &sz,\t\\\n\t    NULL, 0), 0, \"Unexpected mallctl() failure\");\t\t\\\n} while (0)\n\n\tTEST_STATS_ARENAS(unsigned, nthreads);\n\tTEST_STATS_ARENAS(const char *, dss);\n\tTEST_STATS_ARENAS(ssize_t, dirty_decay_ms);\n\tTEST_STATS_ARENAS(ssize_t, muzzy_decay_ms);\n\tTEST_STATS_ARENAS(size_t, pactive);\n\tTEST_STATS_ARENAS(size_t, pdirty);\n\n#undef TEST_STATS_ARENAS\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_mallctl_errors,\n\t    test_mallctlnametomib_errors,\n\t    test_mallctlbymib_errors,\n\t    test_mallctl_read_write,\n\t    test_mallctlnametomib_short_mib,\n\t    test_mallctl_config,\n\t    test_mallctl_opt,\n\t    test_manpage_example,\n\t    test_tcache_none,\n\t    test_tcache,\n\t    test_thread_arena,\n\t    test_arena_i_initialized,\n\t    test_arena_i_dirty_decay_ms,\n\t    test_arena_i_muzzy_decay_ms,\n\t    test_arena_i_purge,\n\t    test_arena_i_decay,\n\t    test_arena_i_dss,\n\t    test_arenas_dirty_decay_ms,\n\t    test_arenas_muzzy_decay_ms,\n\t    test_arenas_constants,\n\t    test_arenas_bin_constants,\n\t    test_arenas_lextent_constants,\n\t    test_arenas_create,\n\t    test_arenas_lookup,\n\t    test_stats_arenas);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/malloc_io.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nTEST_BEGIN(test_malloc_strtoumax_no_endptr) {\n\tint err;\n\n\tset_errno(0);\n\tassert_ju_eq(malloc_strtoumax(\"0\", NULL, 0), 0, \"Unexpected result\");\n\terr = get_errno();\n\tassert_d_eq(err, 0, \"Unexpected failure\");\n}\nTEST_END\n\nTEST_BEGIN(test_malloc_strtoumax) {\n\tstruct test_s {\n\t\tconst char *input;\n\t\tconst char *expected_remainder;\n\t\tint base;\n\t\tint expected_errno;\n\t\tconst char *expected_errno_name;\n\t\tuintmax_t expected_x;\n\t};\n#define ERR(e)\t\te, #e\n#define KUMAX(x)\t((uintmax_t)x##ULL)\n#define KSMAX(x)\t((uintmax_t)(intmax_t)x##LL)\n\tstruct test_s tests[] = {\n\t\t{\"0\",\t\t\"0\",\t-1,\tERR(EINVAL),\tUINTMAX_MAX},\n\t\t{\"0\",\t\t\"0\",\t1,\tERR(EINVAL),\tUINTMAX_MAX},\n\t\t{\"0\",\t\t\"0\",\t37,\tERR(EINVAL),\tUINTMAX_MAX},\n\n\t\t{\"\",\t\t\"\",\t0,\tERR(EINVAL),\tUINTMAX_MAX},\n\t\t{\"+\",\t\t\"+\",\t0,\tERR(EINVAL),\tUINTMAX_MAX},\n\t\t{\"++3\",\t\t\"++3\",\t0,\tERR(EINVAL),\tUINTMAX_MAX},\n\t\t{\"-\",\t\t\"-\",\t0,\tERR(EINVAL),\tUINTMAX_MAX},\n\n\t\t{\"42\",\t\t\"\",\t0,\tERR(0),\t\tKUMAX(42)},\n\t\t{\"+42\",\t\t\"\",\t0,\tERR(0),\t\tKUMAX(42)},\n\t\t{\"-42\",\t\t\"\",\t0,\tERR(0),\t\tKSMAX(-42)},\n\t\t{\"042\",\t\t\"\",\t0,\tERR(0),\t\tKUMAX(042)},\n\t\t{\"+042\",\t\"\",\t0,\tERR(0),\t\tKUMAX(042)},\n\t\t{\"-042\",\t\"\",\t0,\tERR(0),\t\tKSMAX(-042)},\n\t\t{\"0x42\",\t\"\",\t0,\tERR(0),\t\tKUMAX(0x42)},\n\t\t{\"+0x42\",\t\"\",\t0,\tERR(0),\t\tKUMAX(0x42)},\n\t\t{\"-0x42\",\t\"\",\t0,\tERR(0),\t\tKSMAX(-0x42)},\n\n\t\t{\"0\",\t\t\"\",\t0,\tERR(0),\t\tKUMAX(0)},\n\t\t{\"1\",\t\t\"\",\t0,\tERR(0),\t\tKUMAX(1)},\n\n\t\t{\"42\",\t\t\"\",\t0,\tERR(0),\t\tKUMAX(42)},\n\t\t{\" 42\",\t\t\"\",\t0,\tERR(0),\t\tKUMAX(42)},\n\t\t{\"42 \",\t\t\" \",\t0,\tERR(0),\t\tKUMAX(42)},\n\t\t{\"0x\",\t\t\"x\",\t0,\tERR(0),\t\tKUMAX(0)},\n\t\t{\"42x\",\t\t\"x\",\t0,\tERR(0),\t\tKUMAX(42)},\n\n\t\t{\"07\",\t\t\"\",\t0,\tERR(0),\t\tKUMAX(7)},\n\t\t{\"010\",\t\t\"\",\t0,\tERR(0),\t\tKUMAX(8)},\n\t\t{\"08\",\t\t\"8\",\t0,\tERR(0),\t\tKUMAX(0)},\n\t\t{\"0_\",\t\t\"_\",\t0,\tERR(0),\t\tKUMAX(0)},\n\n\t\t{\"0x\",\t\t\"x\",\t0,\tERR(0),\t\tKUMAX(0)},\n\t\t{\"0X\",\t\t\"X\",\t0,\tERR(0),\t\tKUMAX(0)},\n\t\t{\"0xg\",\t\t\"xg\",\t0,\tERR(0),\t\tKUMAX(0)},\n\t\t{\"0XA\",\t\t\"\",\t0,\tERR(0),\t\tKUMAX(10)},\n\n\t\t{\"010\",\t\t\"\",\t10,\tERR(0),\t\tKUMAX(10)},\n\t\t{\"0x3\",\t\t\"x3\",\t10,\tERR(0),\t\tKUMAX(0)},\n\n\t\t{\"12\",\t\t\"2\",\t2,\tERR(0),\t\tKUMAX(1)},\n\t\t{\"78\",\t\t\"8\",\t8,\tERR(0),\t\tKUMAX(7)},\n\t\t{\"9a\",\t\t\"a\",\t10,\tERR(0),\t\tKUMAX(9)},\n\t\t{\"9A\",\t\t\"A\",\t10,\tERR(0),\t\tKUMAX(9)},\n\t\t{\"fg\",\t\t\"g\",\t16,\tERR(0),\t\tKUMAX(15)},\n\t\t{\"FG\",\t\t\"G\",\t16,\tERR(0),\t\tKUMAX(15)},\n\t\t{\"0xfg\",\t\"g\",\t16,\tERR(0),\t\tKUMAX(15)},\n\t\t{\"0XFG\",\t\"G\",\t16,\tERR(0),\t\tKUMAX(15)},\n\t\t{\"z_\",\t\t\"_\",\t36,\tERR(0),\t\tKUMAX(35)},\n\t\t{\"Z_\",\t\t\"_\",\t36,\tERR(0),\t\tKUMAX(35)}\n\t};\n#undef ERR\n#undef KUMAX\n#undef KSMAX\n\tunsigned i;\n\n\tfor (i = 0; i < sizeof(tests)/sizeof(struct test_s); i++) {\n\t\tstruct test_s *test = &tests[i];\n\t\tint err;\n\t\tuintmax_t result;\n\t\tchar *remainder;\n\n\t\tset_errno(0);\n\t\tresult = malloc_strtoumax(test->input, &remainder, test->base);\n\t\terr = get_errno();\n\t\tassert_d_eq(err, test->expected_errno,\n\t\t    \"Expected errno %s for \\\"%s\\\", base %d\",\n\t\t    test->expected_errno_name, test->input, test->base);\n\t\tassert_str_eq(remainder, test->expected_remainder,\n\t\t    \"Unexpected remainder for \\\"%s\\\", base %d\",\n\t\t    test->input, test->base);\n\t\tif (err == 0) {\n\t\t\tassert_ju_eq(result, test->expected_x,\n\t\t\t    \"Unexpected result for \\\"%s\\\", base %d\",\n\t\t\t    test->input, test->base);\n\t\t}\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_malloc_snprintf_truncated) {\n#define BUFLEN\t15\n\tchar buf[BUFLEN];\n\tsize_t result;\n\tsize_t len;\n#define TEST(expected_str_untruncated, ...) do {\t\t\t\\\n\tresult = malloc_snprintf(buf, len, __VA_ARGS__);\t\t\\\n\tassert_d_eq(strncmp(buf, expected_str_untruncated, len-1), 0,\t\\\n\t    \"Unexpected string inequality (\\\"%s\\\" vs \\\"%s\\\")\",\t\t\\\n\t    buf, expected_str_untruncated);\t\t\t\t\\\n\tassert_zu_eq(result, strlen(expected_str_untruncated),\t\t\\\n\t    \"Unexpected result\");\t\t\t\t\t\\\n} while (0)\n\n\tfor (len = 1; len < BUFLEN; len++) {\n\t\tTEST(\"012346789\",\t\"012346789\");\n\t\tTEST(\"a0123b\",\t\t\"a%sb\", \"0123\");\n\t\tTEST(\"a01234567\",\t\"a%s%s\", \"0123\", \"4567\");\n\t\tTEST(\"a0123  \",\t\t\"a%-6s\", \"0123\");\n\t\tTEST(\"a  0123\",\t\t\"a%6s\", \"0123\");\n\t\tTEST(\"a   012\",\t\t\"a%6.3s\", \"0123\");\n\t\tTEST(\"a   012\",\t\t\"a%*.*s\", 6, 3, \"0123\");\n\t\tTEST(\"a 123b\",\t\t\"a% db\", 123);\n\t\tTEST(\"a123b\",\t\t\"a%-db\", 123);\n\t\tTEST(\"a-123b\",\t\t\"a%-db\", -123);\n\t\tTEST(\"a+123b\",\t\t\"a%+db\", 123);\n\t}\n#undef BUFLEN\n#undef TEST\n}\nTEST_END\n\nTEST_BEGIN(test_malloc_snprintf) {\n#define BUFLEN\t128\n\tchar buf[BUFLEN];\n\tsize_t result;\n#define TEST(expected_str, ...) do {\t\t\t\t\t\\\n\tresult = malloc_snprintf(buf, sizeof(buf), __VA_ARGS__);\t\\\n\tassert_str_eq(buf, expected_str, \"Unexpected output\");\t\t\\\n\tassert_zu_eq(result, strlen(expected_str), \"Unexpected result\");\\\n} while (0)\n\n\tTEST(\"hello\", \"hello\");\n\n\tTEST(\"50%, 100%\", \"50%%, %d%%\", 100);\n\n\tTEST(\"a0123b\", \"a%sb\", \"0123\");\n\n\tTEST(\"a 0123b\", \"a%5sb\", \"0123\");\n\tTEST(\"a 0123b\", \"a%*sb\", 5, \"0123\");\n\n\tTEST(\"a0123 b\", \"a%-5sb\", \"0123\");\n\tTEST(\"a0123b\", \"a%*sb\", -1, \"0123\");\n\tTEST(\"a0123 b\", \"a%*sb\", -5, \"0123\");\n\tTEST(\"a0123 b\", \"a%-*sb\", -5, \"0123\");\n\n\tTEST(\"a012b\", \"a%.3sb\", \"0123\");\n\tTEST(\"a012b\", \"a%.*sb\", 3, \"0123\");\n\tTEST(\"a0123b\", \"a%.*sb\", -3, \"0123\");\n\n\tTEST(\"a  012b\", \"a%5.3sb\", \"0123\");\n\tTEST(\"a  012b\", \"a%5.*sb\", 3, \"0123\");\n\tTEST(\"a  012b\", \"a%*.3sb\", 5, \"0123\");\n\tTEST(\"a  012b\", \"a%*.*sb\", 5, 3, \"0123\");\n\tTEST(\"a 0123b\", \"a%*.*sb\", 5, -3, \"0123\");\n\n\tTEST(\"_abcd_\", \"_%x_\", 0xabcd);\n\tTEST(\"_0xabcd_\", \"_%#x_\", 0xabcd);\n\tTEST(\"_1234_\", \"_%o_\", 01234);\n\tTEST(\"_01234_\", \"_%#o_\", 01234);\n\tTEST(\"_1234_\", \"_%u_\", 1234);\n\n\tTEST(\"_1234_\", \"_%d_\", 1234);\n\tTEST(\"_ 1234_\", \"_% d_\", 1234);\n\tTEST(\"_+1234_\", \"_%+d_\", 1234);\n\tTEST(\"_-1234_\", \"_%d_\", -1234);\n\tTEST(\"_-1234_\", \"_% d_\", -1234);\n\tTEST(\"_-1234_\", \"_%+d_\", -1234);\n\n\tTEST(\"_-1234_\", \"_%d_\", -1234);\n\tTEST(\"_1234_\", \"_%d_\", 1234);\n\tTEST(\"_-1234_\", \"_%i_\", -1234);\n\tTEST(\"_1234_\", \"_%i_\", 1234);\n\tTEST(\"_01234_\", \"_%#o_\", 01234);\n\tTEST(\"_1234_\", \"_%u_\", 1234);\n\tTEST(\"_0x1234abc_\", \"_%#x_\", 0x1234abc);\n\tTEST(\"_0X1234ABC_\", \"_%#X_\", 0x1234abc);\n\tTEST(\"_c_\", \"_%c_\", 'c');\n\tTEST(\"_string_\", \"_%s_\", \"string\");\n\tTEST(\"_0x42_\", \"_%p_\", ((void *)0x42));\n\n\tTEST(\"_-1234_\", \"_%ld_\", ((long)-1234));\n\tTEST(\"_1234_\", \"_%ld_\", ((long)1234));\n\tTEST(\"_-1234_\", \"_%li_\", ((long)-1234));\n\tTEST(\"_1234_\", \"_%li_\", ((long)1234));\n\tTEST(\"_01234_\", \"_%#lo_\", ((long)01234));\n\tTEST(\"_1234_\", \"_%lu_\", ((long)1234));\n\tTEST(\"_0x1234abc_\", \"_%#lx_\", ((long)0x1234abc));\n\tTEST(\"_0X1234ABC_\", \"_%#lX_\", ((long)0x1234ABC));\n\n\tTEST(\"_-1234_\", \"_%lld_\", ((long long)-1234));\n\tTEST(\"_1234_\", \"_%lld_\", ((long long)1234));\n\tTEST(\"_-1234_\", \"_%lli_\", ((long long)-1234));\n\tTEST(\"_1234_\", \"_%lli_\", ((long long)1234));\n\tTEST(\"_01234_\", \"_%#llo_\", ((long long)01234));\n\tTEST(\"_1234_\", \"_%llu_\", ((long long)1234));\n\tTEST(\"_0x1234abc_\", \"_%#llx_\", ((long long)0x1234abc));\n\tTEST(\"_0X1234ABC_\", \"_%#llX_\", ((long long)0x1234ABC));\n\n\tTEST(\"_-1234_\", \"_%qd_\", ((long long)-1234));\n\tTEST(\"_1234_\", \"_%qd_\", ((long long)1234));\n\tTEST(\"_-1234_\", \"_%qi_\", ((long long)-1234));\n\tTEST(\"_1234_\", \"_%qi_\", ((long long)1234));\n\tTEST(\"_01234_\", \"_%#qo_\", ((long long)01234));\n\tTEST(\"_1234_\", \"_%qu_\", ((long long)1234));\n\tTEST(\"_0x1234abc_\", \"_%#qx_\", ((long long)0x1234abc));\n\tTEST(\"_0X1234ABC_\", \"_%#qX_\", ((long long)0x1234ABC));\n\n\tTEST(\"_-1234_\", \"_%jd_\", ((intmax_t)-1234));\n\tTEST(\"_1234_\", \"_%jd_\", ((intmax_t)1234));\n\tTEST(\"_-1234_\", \"_%ji_\", ((intmax_t)-1234));\n\tTEST(\"_1234_\", \"_%ji_\", ((intmax_t)1234));\n\tTEST(\"_01234_\", \"_%#jo_\", ((intmax_t)01234));\n\tTEST(\"_1234_\", \"_%ju_\", ((intmax_t)1234));\n\tTEST(\"_0x1234abc_\", \"_%#jx_\", ((intmax_t)0x1234abc));\n\tTEST(\"_0X1234ABC_\", \"_%#jX_\", ((intmax_t)0x1234ABC));\n\n\tTEST(\"_1234_\", \"_%td_\", ((ptrdiff_t)1234));\n\tTEST(\"_-1234_\", \"_%td_\", ((ptrdiff_t)-1234));\n\tTEST(\"_1234_\", \"_%ti_\", ((ptrdiff_t)1234));\n\tTEST(\"_-1234_\", \"_%ti_\", ((ptrdiff_t)-1234));\n\n\tTEST(\"_-1234_\", \"_%zd_\", ((ssize_t)-1234));\n\tTEST(\"_1234_\", \"_%zd_\", ((ssize_t)1234));\n\tTEST(\"_-1234_\", \"_%zi_\", ((ssize_t)-1234));\n\tTEST(\"_1234_\", \"_%zi_\", ((ssize_t)1234));\n\tTEST(\"_01234_\", \"_%#zo_\", ((ssize_t)01234));\n\tTEST(\"_1234_\", \"_%zu_\", ((ssize_t)1234));\n\tTEST(\"_0x1234abc_\", \"_%#zx_\", ((ssize_t)0x1234abc));\n\tTEST(\"_0X1234ABC_\", \"_%#zX_\", ((ssize_t)0x1234ABC));\n#undef BUFLEN\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_malloc_strtoumax_no_endptr,\n\t    test_malloc_strtoumax,\n\t    test_malloc_snprintf_truncated,\n\t    test_malloc_snprintf);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/math.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#define MAX_REL_ERR 1.0e-9\n#define MAX_ABS_ERR 1.0e-9\n\n#include <float.h>\n\n#ifdef __PGI\n#undef INFINITY\n#endif\n\n#ifndef INFINITY\n#define INFINITY (DBL_MAX + DBL_MAX)\n#endif\n\nstatic bool\ndouble_eq_rel(double a, double b, double max_rel_err, double max_abs_err) {\n\tdouble rel_err;\n\n\tif (fabs(a - b) < max_abs_err) {\n\t\treturn true;\n\t}\n\trel_err = (fabs(b) > fabs(a)) ? fabs((a-b)/b) : fabs((a-b)/a);\n\treturn (rel_err < max_rel_err);\n}\n\nstatic uint64_t\nfactorial(unsigned x) {\n\tuint64_t ret = 1;\n\tunsigned i;\n\n\tfor (i = 2; i <= x; i++) {\n\t\tret *= (uint64_t)i;\n\t}\n\n\treturn ret;\n}\n\nTEST_BEGIN(test_ln_gamma_factorial) {\n\tunsigned x;\n\n\t/* exp(ln_gamma(x)) == (x-1)! for integer x. */\n\tfor (x = 1; x <= 21; x++) {\n\t\tassert_true(double_eq_rel(exp(ln_gamma(x)),\n\t\t    (double)factorial(x-1), MAX_REL_ERR, MAX_ABS_ERR),\n\t\t    \"Incorrect factorial result for x=%u\", x);\n\t}\n}\nTEST_END\n\n/* Expected ln_gamma([0.0..100.0] increment=0.25). */\nstatic const double ln_gamma_misc_expected[] = {\n\tINFINITY,\n\t1.28802252469807743, 0.57236494292470008, 0.20328095143129538,\n\t0.00000000000000000, -0.09827183642181320, -0.12078223763524518,\n\t-0.08440112102048555, 0.00000000000000000, 0.12487171489239651,\n\t0.28468287047291918, 0.47521466691493719, 0.69314718055994529,\n\t0.93580193110872523, 1.20097360234707429, 1.48681557859341718,\n\t1.79175946922805496, 2.11445692745037128, 2.45373657084244234,\n\t2.80857141857573644, 3.17805383034794575, 3.56137591038669710,\n\t3.95781396761871651, 4.36671603662228680, 4.78749174278204581,\n\t5.21960398699022932, 5.66256205985714178, 6.11591589143154568,\n\t6.57925121201010121, 7.05218545073853953, 7.53436423675873268,\n\t8.02545839631598312, 8.52516136106541467, 9.03318691960512332,\n\t9.54926725730099690, 10.07315123968123949, 10.60460290274525086,\n\t11.14340011995171231, 11.68933342079726856, 12.24220494005076176,\n\t12.80182748008146909, 13.36802367147604720, 13.94062521940376342,\n\t14.51947222506051816, 15.10441257307551943, 15.69530137706046524,\n\t16.29200047656724237, 16.89437797963419285, 17.50230784587389010,\n\t18.11566950571089407, 18.73434751193644843, 19.35823122022435427,\n\t19.98721449566188468, 20.62119544270163018, 21.26007615624470048,\n\t21.90376249182879320, 22.55216385312342098, 23.20519299513386002,\n\t23.86276584168908954, 24.52480131594137802, 25.19122118273868338,\n\t25.86194990184851861, 26.53691449111561340, 27.21604439872720604,\n\t27.89927138384089389, 28.58652940490193828, 29.27775451504081516,\n\t29.97288476399884871, 30.67186010608067548, 31.37462231367769050,\n\t32.08111489594735843, 32.79128302226991565, 33.50507345013689076,\n\t34.22243445715505317, 34.94331577687681545, 35.66766853819134298,\n\t36.39544520803305261, 37.12659953718355865, 37.86108650896109395,\n\t38.59886229060776230, 39.33988418719949465, 40.08411059791735198,\n\t40.83150097453079752, 41.58201578195490100, 42.33561646075348506,\n\t43.09226539146988699, 43.85192586067515208, 44.61456202863158893,\n\t45.38013889847690052, 46.14862228684032885, 46.91997879580877395,\n\t47.69417578616628361, 48.47118135183522014, 49.25096429545256882,\n\t50.03349410501914463, 50.81874093156324790, 51.60667556776436982,\n\t52.39726942748592364, 53.19049452616926743, 53.98632346204390586,\n\t54.78472939811231157, 55.58568604486942633, 56.38916764371992940,\n\t57.19514895105859864, 58.00360522298051080, 58.81451220059079787,\n\t59.62784609588432261, 60.44358357816834371, 61.26170176100199427,\n\t62.08217818962842927, 62.90499082887649962, 63.73011805151035958,\n\t64.55753862700632340, 65.38723171073768015, 66.21917683354901385,\n\t67.05335389170279825, 67.88974313718154008, 68.72832516833013017,\n\t69.56908092082363737, 70.41199165894616385, 71.25703896716800045,\n\t72.10420474200799390, 72.95347118416940191, 73.80482079093779646,\n\t74.65823634883015814, 75.51370092648485866, 76.37119786778275454,\n\t77.23071078519033961, 78.09222355331530707, 78.95572030266725960,\n\t79.82118541361435859, 80.68860351052903468, 81.55795945611502873,\n\t82.42923834590904164, 83.30242550295004378, 84.17750647261028973,\n\t85.05446701758152983, 85.93329311301090456, 86.81397094178107920,\n\t87.69648688992882057, 88.58082754219766741, 89.46697967771913795,\n\t90.35493026581838194, 91.24466646193963015, 92.13617560368709292,\n\t93.02944520697742803, 93.92446296229978486, 94.82121673107967297,\n\t95.71969454214321615, 96.61988458827809723, 97.52177522288820910,\n\t98.42535495673848800, 99.33061245478741341, 100.23753653310367895,\n\t101.14611615586458981, 102.05634043243354370, 102.96819861451382394,\n\t103.88168009337621811, 104.79677439715833032, 105.71347118823287303,\n\t106.63176026064346047, 107.55163153760463501, 108.47307506906540198,\n\t109.39608102933323153, 110.32063971475740516, 111.24674154146920557,\n\t112.17437704317786995, 113.10353686902013237, 114.03421178146170689,\n\t114.96639265424990128, 115.90007047041454769, 116.83523632031698014,\n\t117.77188139974506953, 118.70999700805310795, 119.64957454634490830,\n\t120.59060551569974962, 121.53308151543865279, 122.47699424143097247,\n\t123.42233548443955726, 124.36909712850338394, 125.31727114935689826,\n\t126.26684961288492559, 127.21782467361175861, 128.17018857322420899,\n\t129.12393363912724453, 130.07905228303084755, 131.03553699956862033,\n\t131.99338036494577864, 132.95257503561629164, 133.91311374698926784,\n\t134.87498931216194364, 135.83819462068046846, 136.80272263732638294,\n\t137.76856640092901785, 138.73571902320256299, 139.70417368760718091,\n\t140.67392364823425055, 141.64496222871400732, 142.61728282114600574,\n\t143.59087888505104047, 144.56574394634486680, 145.54187159633210058,\n\t146.51925549072063859, 147.49788934865566148, 148.47776695177302031,\n\t149.45888214327129617, 150.44122882700193600, 151.42480096657754984,\n\t152.40959258449737490, 153.39559776128982094, 154.38281063467164245,\n\t155.37122539872302696, 156.36083630307879844, 157.35163765213474107,\n\t158.34362380426921391, 159.33678917107920370, 160.33112821663092973,\n\t161.32663545672428995, 162.32330545817117695, 163.32113283808695314,\n\t164.32011226319519892, 165.32023844914485267, 166.32150615984036790,\n\t167.32391020678358018, 168.32744544842768164, 169.33210678954270634,\n\t170.33788918059275375, 171.34478761712384198, 172.35279713916281707,\n\t173.36191283062726143, 174.37212981874515094, 175.38344327348534080,\n\t176.39584840699734514, 177.40934047306160437, 178.42391476654847793,\n\t179.43956662288721304, 180.45629141754378111, 181.47408456550741107,\n\t182.49294152078630304, 183.51285777591152737, 184.53382886144947861,\n\t185.55585034552262869, 186.57891783333786861, 187.60302696672312095,\n\t188.62817342367162610, 189.65435291789341932, 190.68156119837468054,\n\t191.70979404894376330, 192.73904728784492590, 193.76931676731820176,\n\t194.80059837318714244, 195.83288802445184729, 196.86618167288995096,\n\t197.90047530266301123, 198.93576492992946214, 199.97204660246373464,\n\t201.00931639928148797, 202.04757043027063901, 203.08680483582807597,\n\t204.12701578650228385, 205.16819948264117102, 206.21035215404597807,\n\t207.25347005962987623, 208.29754948708190909, 209.34258675253678916,\n\t210.38857820024875878, 211.43552020227099320, 212.48340915813977858,\n\t213.53224149456323744, 214.58201366511514152, 215.63272214993284592,\n\t216.68436345542014010, 217.73693411395422004, 218.79043068359703739,\n\t219.84484974781133815, 220.90018791517996988, 221.95644181913033322,\n\t223.01360811766215875, 224.07168349307951871, 225.13066465172661879,\n\t226.19054832372759734, 227.25133126272962159, 228.31301024565024704,\n\t229.37558207242807384, 230.43904356577689896, 231.50339157094342113,\n\t232.56862295546847008, 233.63473460895144740, 234.70172344281823484,\n\t235.76958639009222907, 236.83832040516844586, 237.90792246359117712,\n\t238.97838956183431947, 240.04971871708477238, 241.12190696702904802,\n\t242.19495136964280846, 243.26884900298270509, 244.34359696498191283,\n\t245.41919237324782443, 246.49563236486270057, 247.57291409618682110,\n\t248.65103474266476269, 249.72999149863338175, 250.80978157713354904,\n\t251.89040220972316320, 252.97185064629374551, 254.05412415488834199,\n\t255.13722002152300661, 256.22113555000953511, 257.30586806178126835,\n\t258.39141489572085675, 259.47777340799029844, 260.56494097186322279,\n\t261.65291497755913497, 262.74169283208021852, 263.83127195904967266,\n\t264.92164979855277807, 266.01282380697938379, 267.10479145686849733,\n\t268.19755023675537586, 269.29109765101975427, 270.38543121973674488,\n\t271.48054847852881721, 272.57644697842033565, 273.67312428569374561,\n\t274.77057798174683967, 275.86880566295326389, 276.96780494052313770,\n\t278.06757344036617496, 279.16810880295668085, 280.26940868320008349,\n\t281.37147075030043197, 282.47429268763045229, 283.57787219260217171,\n\t284.68220697654078322, 285.78729476455760050, 286.89313329542699194,\n\t287.99972032146268930, 289.10705360839756395, 290.21513093526289140,\n\t291.32395009427028754, 292.43350889069523646, 293.54380514276073200,\n\t294.65483668152336350, 295.76660135076059532, 296.87909700685889902,\n\t297.99232151870342022, 299.10627276756946458, 300.22094864701409733,\n\t301.33634706277030091, 302.45246593264130297, 303.56930318639643929,\n\t304.68685676566872189, 305.80512462385280514, 306.92410472600477078,\n\t308.04379504874236773, 309.16419358014690033, 310.28529831966631036,\n\t311.40710727801865687, 312.52961847709792664, 313.65282994987899201,\n\t314.77673974032603610, 315.90134590329950015, 317.02664650446632777,\n\t318.15263962020929966, 319.27932333753892635, 320.40669575400545455,\n\t321.53475497761127144, 322.66349912672620803, 323.79292633000159185,\n\t324.92303472628691452, 326.05382246454587403, 327.18528770377525916,\n\t328.31742861292224234, 329.45024337080525356, 330.58373016603343331,\n\t331.71788719692847280, 332.85271267144611329, 333.98820480709991898,\n\t335.12436183088397001, 336.26118197919845443, 337.39866349777429377,\n\t338.53680464159958774, 339.67560367484657036, 340.81505887079896411,\n\t341.95516851178109619, 343.09593088908627578, 344.23734430290727460,\n\t345.37940706226686416, 346.52211748494903532, 347.66547389743118401,\n\t348.80947463481720661, 349.95411804077025408, 351.09940246744753267,\n\t352.24532627543504759, 353.39188783368263103, 354.53908551944078908,\n\t355.68691771819692349, 356.83538282361303118, 357.98447923746385868,\n\t359.13420536957539753\n};\n\nTEST_BEGIN(test_ln_gamma_misc) {\n\tunsigned i;\n\n\tfor (i = 1; i < sizeof(ln_gamma_misc_expected)/sizeof(double); i++) {\n\t\tdouble x = (double)i * 0.25;\n\t\tassert_true(double_eq_rel(ln_gamma(x),\n\t\t    ln_gamma_misc_expected[i], MAX_REL_ERR, MAX_ABS_ERR),\n\t\t    \"Incorrect ln_gamma result for i=%u\", i);\n\t}\n}\nTEST_END\n\n/* Expected pt_norm([0.01..0.99] increment=0.01). */\nstatic const double pt_norm_expected[] = {\n\t-INFINITY,\n\t-2.32634787404084076, -2.05374891063182252, -1.88079360815125085,\n\t-1.75068607125216946, -1.64485362695147264, -1.55477359459685305,\n\t-1.47579102817917063, -1.40507156030963221, -1.34075503369021654,\n\t-1.28155156554460081, -1.22652812003661049, -1.17498679206608991,\n\t-1.12639112903880045, -1.08031934081495606, -1.03643338949378938,\n\t-0.99445788320975281, -0.95416525314619416, -0.91536508784281390,\n\t-0.87789629505122846, -0.84162123357291418, -0.80642124701824025,\n\t-0.77219321418868492, -0.73884684918521371, -0.70630256284008752,\n\t-0.67448975019608171, -0.64334540539291685, -0.61281299101662701,\n\t-0.58284150727121620, -0.55338471955567281, -0.52440051270804067,\n\t-0.49585034734745320, -0.46769879911450812, -0.43991316567323380,\n\t-0.41246312944140462, -0.38532046640756751, -0.35845879325119373,\n\t-0.33185334643681652, -0.30548078809939738, -0.27931903444745404,\n\t-0.25334710313579978, -0.22754497664114931, -0.20189347914185077,\n\t-0.17637416478086135, -0.15096921549677725, -0.12566134685507399,\n\t-0.10043372051146975, -0.07526986209982976, -0.05015358346473352,\n\t-0.02506890825871106, 0.00000000000000000, 0.02506890825871106,\n\t0.05015358346473366, 0.07526986209982990, 0.10043372051146990,\n\t0.12566134685507413, 0.15096921549677739, 0.17637416478086146,\n\t0.20189347914185105, 0.22754497664114931, 0.25334710313579978,\n\t0.27931903444745404, 0.30548078809939738, 0.33185334643681652,\n\t0.35845879325119373, 0.38532046640756762, 0.41246312944140484,\n\t0.43991316567323391, 0.46769879911450835, 0.49585034734745348,\n\t0.52440051270804111, 0.55338471955567303, 0.58284150727121620,\n\t0.61281299101662701, 0.64334540539291685, 0.67448975019608171,\n\t0.70630256284008752, 0.73884684918521371, 0.77219321418868492,\n\t0.80642124701824036, 0.84162123357291441, 0.87789629505122879,\n\t0.91536508784281423, 0.95416525314619460, 0.99445788320975348,\n\t1.03643338949378938, 1.08031934081495606, 1.12639112903880045,\n\t1.17498679206608991, 1.22652812003661049, 1.28155156554460081,\n\t1.34075503369021654, 1.40507156030963265, 1.47579102817917085,\n\t1.55477359459685394, 1.64485362695147308, 1.75068607125217102,\n\t1.88079360815125041, 2.05374891063182208, 2.32634787404084076\n};\n\nTEST_BEGIN(test_pt_norm) {\n\tunsigned i;\n\n\tfor (i = 1; i < sizeof(pt_norm_expected)/sizeof(double); i++) {\n\t\tdouble p = (double)i * 0.01;\n\t\tassert_true(double_eq_rel(pt_norm(p), pt_norm_expected[i],\n\t\t    MAX_REL_ERR, MAX_ABS_ERR),\n\t\t    \"Incorrect pt_norm result for i=%u\", i);\n\t}\n}\nTEST_END\n\n/*\n * Expected pt_chi2(p=[0.01..0.99] increment=0.07,\n *                  df={0.1, 1.1, 10.1, 100.1, 1000.1}).\n */\nstatic const double pt_chi2_df[] = {0.1, 1.1, 10.1, 100.1, 1000.1};\nstatic const double pt_chi2_expected[] = {\n\t1.168926411457320e-40, 1.347680397072034e-22, 3.886980416666260e-17,\n\t8.245951724356564e-14, 2.068936347497604e-11, 1.562561743309233e-09,\n\t5.459543043426564e-08, 1.114775688149252e-06, 1.532101202364371e-05,\n\t1.553884683726585e-04, 1.239396954915939e-03, 8.153872320255721e-03,\n\t4.631183739647523e-02, 2.473187311701327e-01, 2.175254800183617e+00,\n\n\t0.0003729887888876379, 0.0164409238228929513, 0.0521523015190650113,\n\t0.1064701372271216612, 0.1800913735793082115, 0.2748704281195626931,\n\t0.3939246282787986497, 0.5420727552260817816, 0.7267265822221973259,\n\t0.9596554296000253670, 1.2607440376386165326, 1.6671185084541604304,\n\t2.2604828984738705167, 3.2868613342148607082, 6.9298574921692139839,\n\n\t2.606673548632508, 4.602913725294877, 5.646152813924212,\n\t6.488971315540869, 7.249823275816285, 7.977314231410841,\n\t8.700354939944047, 9.441728024225892, 10.224338321374127,\n\t11.076435368801061, 12.039320937038386, 13.183878752697167,\n\t14.657791935084575, 16.885728216339373, 23.361991680031817,\n\n\t70.14844087392152, 80.92379498849355, 85.53325420085891,\n\t88.94433120715347, 91.83732712857017, 94.46719943606301,\n\t96.96896479994635, 99.43412843510363, 101.94074719829733,\n\t104.57228644307247, 107.43900093448734, 110.71844673417287,\n\t114.76616819871325, 120.57422505959563, 135.92318818757556,\n\n\t899.0072447849649, 937.9271278858220, 953.8117189560207,\n\t965.3079371501154, 974.8974061207954, 983.4936235182347,\n\t991.5691170518946, 999.4334123954690, 1007.3391826856553,\n\t1015.5445154999951, 1024.3777075619569, 1034.3538789836223,\n\t1046.4872561869577, 1063.5717461999654, 1107.0741966053859\n};\n\nTEST_BEGIN(test_pt_chi2) {\n\tunsigned i, j;\n\tunsigned e = 0;\n\n\tfor (i = 0; i < sizeof(pt_chi2_df)/sizeof(double); i++) {\n\t\tdouble df = pt_chi2_df[i];\n\t\tdouble ln_gamma_df = ln_gamma(df * 0.5);\n\t\tfor (j = 1; j < 100; j += 7) {\n\t\t\tdouble p = (double)j * 0.01;\n\t\t\tassert_true(double_eq_rel(pt_chi2(p, df, ln_gamma_df),\n\t\t\t    pt_chi2_expected[e], MAX_REL_ERR, MAX_ABS_ERR),\n\t\t\t    \"Incorrect pt_chi2 result for i=%u, j=%u\", i, j);\n\t\t\te++;\n\t\t}\n\t}\n}\nTEST_END\n\n/*\n * Expected pt_gamma(p=[0.1..0.99] increment=0.07,\n *                   shape=[0.5..3.0] increment=0.5).\n */\nstatic const double pt_gamma_shape[] = {0.5, 1.0, 1.5, 2.0, 2.5, 3.0};\nstatic const double pt_gamma_expected[] = {\n\t7.854392895485103e-05, 5.043466107888016e-03, 1.788288957794883e-02,\n\t3.900956150232906e-02, 6.913847560638034e-02, 1.093710833465766e-01,\n\t1.613412523825817e-01, 2.274682115597864e-01, 3.114117323127083e-01,\n\t4.189466220207417e-01, 5.598106789059246e-01, 7.521856146202706e-01,\n\t1.036125427911119e+00, 1.532450860038180e+00, 3.317448300510606e+00,\n\n\t0.01005033585350144, 0.08338160893905107, 0.16251892949777497,\n\t0.24846135929849966, 0.34249030894677596, 0.44628710262841947,\n\t0.56211891815354142, 0.69314718055994529, 0.84397007029452920,\n\t1.02165124753198167, 1.23787435600161766, 1.51412773262977574,\n\t1.89711998488588196, 2.52572864430825783, 4.60517018598809091,\n\n\t0.05741590094955853, 0.24747378084860744, 0.39888572212236084,\n\t0.54394139997444901, 0.69048812513915159, 0.84311389861296104,\n\t1.00580622221479898, 1.18298694218766931, 1.38038096305861213,\n\t1.60627736383027453, 1.87396970522337947, 2.20749220408081070,\n\t2.65852391865854942, 3.37934630984842244, 5.67243336507218476,\n\n\t0.1485547402532659, 0.4657458011640391, 0.6832386130709406,\n\t0.8794297834672100, 1.0700752852474524, 1.2629614217350744,\n\t1.4638400448580779, 1.6783469900166610, 1.9132338090606940,\n\t2.1778589228618777, 2.4868823970010991, 2.8664695666264195,\n\t3.3724415436062114, 4.1682658512758071, 6.6383520679938108,\n\n\t0.2771490383641385, 0.7195001279643727, 0.9969081732265243,\n\t1.2383497880608061, 1.4675206597269927, 1.6953064251816552,\n\t1.9291243435606809, 2.1757300955477641, 2.4428032131216391,\n\t2.7406534569230616, 3.0851445039665513, 3.5043101122033367,\n\t4.0575997065264637, 4.9182956424675286, 7.5431362346944937,\n\n\t0.4360451650782932, 0.9983600902486267, 1.3306365880734528,\n\t1.6129750834753802, 1.8767241606994294, 2.1357032436097660,\n\t2.3988853336865565, 2.6740603137235603, 2.9697561737517959,\n\t3.2971457713883265, 3.6731795898504660, 4.1275751617770631,\n\t4.7230515633946677, 5.6417477865306020, 8.4059469148854635\n};\n\nTEST_BEGIN(test_pt_gamma_shape) {\n\tunsigned i, j;\n\tunsigned e = 0;\n\n\tfor (i = 0; i < sizeof(pt_gamma_shape)/sizeof(double); i++) {\n\t\tdouble shape = pt_gamma_shape[i];\n\t\tdouble ln_gamma_shape = ln_gamma(shape);\n\t\tfor (j = 1; j < 100; j += 7) {\n\t\t\tdouble p = (double)j * 0.01;\n\t\t\tassert_true(double_eq_rel(pt_gamma(p, shape, 1.0,\n\t\t\t    ln_gamma_shape), pt_gamma_expected[e], MAX_REL_ERR,\n\t\t\t    MAX_ABS_ERR),\n\t\t\t    \"Incorrect pt_gamma result for i=%u, j=%u\", i, j);\n\t\t\te++;\n\t\t}\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_pt_gamma_scale) {\n\tdouble shape = 1.0;\n\tdouble ln_gamma_shape = ln_gamma(shape);\n\n\tassert_true(double_eq_rel(\n\t    pt_gamma(0.5, shape, 1.0, ln_gamma_shape) * 10.0,\n\t    pt_gamma(0.5, shape, 10.0, ln_gamma_shape), MAX_REL_ERR,\n\t    MAX_ABS_ERR),\n\t    \"Scale should be trivially equivalent to external multiplication\");\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_ln_gamma_factorial,\n\t    test_ln_gamma_misc,\n\t    test_pt_norm,\n\t    test_pt_chi2,\n\t    test_pt_gamma_shape,\n\t    test_pt_gamma_scale);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/mq.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#define NSENDERS\t3\n#define NMSGS\t\t100000\n\ntypedef struct mq_msg_s mq_msg_t;\nstruct mq_msg_s {\n\tmq_msg(mq_msg_t)\tlink;\n};\nmq_gen(static, mq_, mq_t, mq_msg_t, link)\n\nTEST_BEGIN(test_mq_basic) {\n\tmq_t mq;\n\tmq_msg_t msg;\n\n\tassert_false(mq_init(&mq), \"Unexpected mq_init() failure\");\n\tassert_u_eq(mq_count(&mq), 0, \"mq should be empty\");\n\tassert_ptr_null(mq_tryget(&mq),\n\t    \"mq_tryget() should fail when the queue is empty\");\n\n\tmq_put(&mq, &msg);\n\tassert_u_eq(mq_count(&mq), 1, \"mq should contain one message\");\n\tassert_ptr_eq(mq_tryget(&mq), &msg, \"mq_tryget() should return msg\");\n\n\tmq_put(&mq, &msg);\n\tassert_ptr_eq(mq_get(&mq), &msg, \"mq_get() should return msg\");\n\n\tmq_fini(&mq);\n}\nTEST_END\n\nstatic void *\nthd_receiver_start(void *arg) {\n\tmq_t *mq = (mq_t *)arg;\n\tunsigned i;\n\n\tfor (i = 0; i < (NSENDERS * NMSGS); i++) {\n\t\tmq_msg_t *msg = mq_get(mq);\n\t\tassert_ptr_not_null(msg, \"mq_get() should never return NULL\");\n\t\tdallocx(msg, 0);\n\t}\n\treturn NULL;\n}\n\nstatic void *\nthd_sender_start(void *arg) {\n\tmq_t *mq = (mq_t *)arg;\n\tunsigned i;\n\n\tfor (i = 0; i < NMSGS; i++) {\n\t\tmq_msg_t *msg;\n\t\tvoid *p;\n\t\tp = mallocx(sizeof(mq_msg_t), 0);\n\t\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\t\tmsg = (mq_msg_t *)p;\n\t\tmq_put(mq, msg);\n\t}\n\treturn NULL;\n}\n\nTEST_BEGIN(test_mq_threaded) {\n\tmq_t mq;\n\tthd_t receiver;\n\tthd_t senders[NSENDERS];\n\tunsigned i;\n\n\tassert_false(mq_init(&mq), \"Unexpected mq_init() failure\");\n\n\tthd_create(&receiver, thd_receiver_start, (void *)&mq);\n\tfor (i = 0; i < NSENDERS; i++) {\n\t\tthd_create(&senders[i], thd_sender_start, (void *)&mq);\n\t}\n\n\tthd_join(receiver, NULL);\n\tfor (i = 0; i < NSENDERS; i++) {\n\t\tthd_join(senders[i], NULL);\n\t}\n\n\tmq_fini(&mq);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_mq_basic,\n\t    test_mq_threaded);\n}\n\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/mtx.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#define NTHREADS\t2\n#define NINCRS\t\t2000000\n\nTEST_BEGIN(test_mtx_basic) {\n\tmtx_t mtx;\n\n\tassert_false(mtx_init(&mtx), \"Unexpected mtx_init() failure\");\n\tmtx_lock(&mtx);\n\tmtx_unlock(&mtx);\n\tmtx_fini(&mtx);\n}\nTEST_END\n\ntypedef struct {\n\tmtx_t\t\tmtx;\n\tunsigned\tx;\n} thd_start_arg_t;\n\nstatic void *\nthd_start(void *varg) {\n\tthd_start_arg_t *arg = (thd_start_arg_t *)varg;\n\tunsigned i;\n\n\tfor (i = 0; i < NINCRS; i++) {\n\t\tmtx_lock(&arg->mtx);\n\t\targ->x++;\n\t\tmtx_unlock(&arg->mtx);\n\t}\n\treturn NULL;\n}\n\nTEST_BEGIN(test_mtx_race) {\n\tthd_start_arg_t arg;\n\tthd_t thds[NTHREADS];\n\tunsigned i;\n\n\tassert_false(mtx_init(&arg.mtx), \"Unexpected mtx_init() failure\");\n\targ.x = 0;\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tthd_create(&thds[i], thd_start, (void *)&arg);\n\t}\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tthd_join(thds[i], NULL);\n\t}\n\tassert_u_eq(arg.x, NTHREADS * NINCRS,\n\t    \"Race-related counter corruption\");\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_mtx_basic,\n\t    test_mtx_race);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/nstime.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#define BILLION\tUINT64_C(1000000000)\n\nTEST_BEGIN(test_nstime_init) {\n\tnstime_t nst;\n\n\tnstime_init(&nst, 42000000043);\n\tassert_u64_eq(nstime_ns(&nst), 42000000043, \"ns incorrectly read\");\n\tassert_u64_eq(nstime_sec(&nst), 42, \"sec incorrectly read\");\n\tassert_u64_eq(nstime_nsec(&nst), 43, \"nsec incorrectly read\");\n}\nTEST_END\n\nTEST_BEGIN(test_nstime_init2) {\n\tnstime_t nst;\n\n\tnstime_init2(&nst, 42, 43);\n\tassert_u64_eq(nstime_sec(&nst), 42, \"sec incorrectly read\");\n\tassert_u64_eq(nstime_nsec(&nst), 43, \"nsec incorrectly read\");\n}\nTEST_END\n\nTEST_BEGIN(test_nstime_copy) {\n\tnstime_t nsta, nstb;\n\n\tnstime_init2(&nsta, 42, 43);\n\tnstime_init(&nstb, 0);\n\tnstime_copy(&nstb, &nsta);\n\tassert_u64_eq(nstime_sec(&nstb), 42, \"sec incorrectly copied\");\n\tassert_u64_eq(nstime_nsec(&nstb), 43, \"nsec incorrectly copied\");\n}\nTEST_END\n\nTEST_BEGIN(test_nstime_compare) {\n\tnstime_t nsta, nstb;\n\n\tnstime_init2(&nsta, 42, 43);\n\tnstime_copy(&nstb, &nsta);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 0, \"Times should be equal\");\n\tassert_d_eq(nstime_compare(&nstb, &nsta), 0, \"Times should be equal\");\n\n\tnstime_init2(&nstb, 42, 42);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 1,\n\t    \"nsta should be greater than nstb\");\n\tassert_d_eq(nstime_compare(&nstb, &nsta), -1,\n\t    \"nstb should be less than nsta\");\n\n\tnstime_init2(&nstb, 42, 44);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), -1,\n\t    \"nsta should be less than nstb\");\n\tassert_d_eq(nstime_compare(&nstb, &nsta), 1,\n\t    \"nstb should be greater than nsta\");\n\n\tnstime_init2(&nstb, 41, BILLION - 1);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 1,\n\t    \"nsta should be greater than nstb\");\n\tassert_d_eq(nstime_compare(&nstb, &nsta), -1,\n\t    \"nstb should be less than nsta\");\n\n\tnstime_init2(&nstb, 43, 0);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), -1,\n\t    \"nsta should be less than nstb\");\n\tassert_d_eq(nstime_compare(&nstb, &nsta), 1,\n\t    \"nstb should be greater than nsta\");\n}\nTEST_END\n\nTEST_BEGIN(test_nstime_add) {\n\tnstime_t nsta, nstb;\n\n\tnstime_init2(&nsta, 42, 43);\n\tnstime_copy(&nstb, &nsta);\n\tnstime_add(&nsta, &nstb);\n\tnstime_init2(&nstb, 84, 86);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 0,\n\t    \"Incorrect addition result\");\n\n\tnstime_init2(&nsta, 42, BILLION - 1);\n\tnstime_copy(&nstb, &nsta);\n\tnstime_add(&nsta, &nstb);\n\tnstime_init2(&nstb, 85, BILLION - 2);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 0,\n\t    \"Incorrect addition result\");\n}\nTEST_END\n\nTEST_BEGIN(test_nstime_iadd) {\n\tnstime_t nsta, nstb;\n\n\tnstime_init2(&nsta, 42, BILLION - 1);\n\tnstime_iadd(&nsta, 1);\n\tnstime_init2(&nstb, 43, 0);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 0,\n\t    \"Incorrect addition result\");\n\n\tnstime_init2(&nsta, 42, 1);\n\tnstime_iadd(&nsta, BILLION + 1);\n\tnstime_init2(&nstb, 43, 2);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 0,\n\t    \"Incorrect addition result\");\n}\nTEST_END\n\nTEST_BEGIN(test_nstime_subtract) {\n\tnstime_t nsta, nstb;\n\n\tnstime_init2(&nsta, 42, 43);\n\tnstime_copy(&nstb, &nsta);\n\tnstime_subtract(&nsta, &nstb);\n\tnstime_init(&nstb, 0);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 0,\n\t    \"Incorrect subtraction result\");\n\n\tnstime_init2(&nsta, 42, 43);\n\tnstime_init2(&nstb, 41, 44);\n\tnstime_subtract(&nsta, &nstb);\n\tnstime_init2(&nstb, 0, BILLION - 1);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 0,\n\t    \"Incorrect subtraction result\");\n}\nTEST_END\n\nTEST_BEGIN(test_nstime_isubtract) {\n\tnstime_t nsta, nstb;\n\n\tnstime_init2(&nsta, 42, 43);\n\tnstime_isubtract(&nsta, 42*BILLION + 43);\n\tnstime_init(&nstb, 0);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 0,\n\t    \"Incorrect subtraction result\");\n\n\tnstime_init2(&nsta, 42, 43);\n\tnstime_isubtract(&nsta, 41*BILLION + 44);\n\tnstime_init2(&nstb, 0, BILLION - 1);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 0,\n\t    \"Incorrect subtraction result\");\n}\nTEST_END\n\nTEST_BEGIN(test_nstime_imultiply) {\n\tnstime_t nsta, nstb;\n\n\tnstime_init2(&nsta, 42, 43);\n\tnstime_imultiply(&nsta, 10);\n\tnstime_init2(&nstb, 420, 430);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 0,\n\t    \"Incorrect multiplication result\");\n\n\tnstime_init2(&nsta, 42, 666666666);\n\tnstime_imultiply(&nsta, 3);\n\tnstime_init2(&nstb, 127, 999999998);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 0,\n\t    \"Incorrect multiplication result\");\n}\nTEST_END\n\nTEST_BEGIN(test_nstime_idivide) {\n\tnstime_t nsta, nstb;\n\n\tnstime_init2(&nsta, 42, 43);\n\tnstime_copy(&nstb, &nsta);\n\tnstime_imultiply(&nsta, 10);\n\tnstime_idivide(&nsta, 10);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 0,\n\t    \"Incorrect division result\");\n\n\tnstime_init2(&nsta, 42, 666666666);\n\tnstime_copy(&nstb, &nsta);\n\tnstime_imultiply(&nsta, 3);\n\tnstime_idivide(&nsta, 3);\n\tassert_d_eq(nstime_compare(&nsta, &nstb), 0,\n\t    \"Incorrect division result\");\n}\nTEST_END\n\nTEST_BEGIN(test_nstime_divide) {\n\tnstime_t nsta, nstb, nstc;\n\n\tnstime_init2(&nsta, 42, 43);\n\tnstime_copy(&nstb, &nsta);\n\tnstime_imultiply(&nsta, 10);\n\tassert_u64_eq(nstime_divide(&nsta, &nstb), 10,\n\t    \"Incorrect division result\");\n\n\tnstime_init2(&nsta, 42, 43);\n\tnstime_copy(&nstb, &nsta);\n\tnstime_imultiply(&nsta, 10);\n\tnstime_init(&nstc, 1);\n\tnstime_add(&nsta, &nstc);\n\tassert_u64_eq(nstime_divide(&nsta, &nstb), 10,\n\t    \"Incorrect division result\");\n\n\tnstime_init2(&nsta, 42, 43);\n\tnstime_copy(&nstb, &nsta);\n\tnstime_imultiply(&nsta, 10);\n\tnstime_init(&nstc, 1);\n\tnstime_subtract(&nsta, &nstc);\n\tassert_u64_eq(nstime_divide(&nsta, &nstb), 9,\n\t    \"Incorrect division result\");\n}\nTEST_END\n\nTEST_BEGIN(test_nstime_monotonic) {\n\tnstime_monotonic();\n}\nTEST_END\n\nTEST_BEGIN(test_nstime_update) {\n\tnstime_t nst;\n\n\tnstime_init(&nst, 0);\n\n\tassert_false(nstime_update(&nst), \"Basic time update failed.\");\n\n\t/* Only Rip Van Winkle sleeps this long. */\n\t{\n\t\tnstime_t addend;\n\t\tnstime_init2(&addend, 631152000, 0);\n\t\tnstime_add(&nst, &addend);\n\t}\n\t{\n\t\tnstime_t nst0;\n\t\tnstime_copy(&nst0, &nst);\n\t\tassert_true(nstime_update(&nst),\n\t\t    \"Update should detect time roll-back.\");\n\t\tassert_d_eq(nstime_compare(&nst, &nst0), 0,\n\t\t    \"Time should not have been modified\");\n\t}\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_nstime_init,\n\t    test_nstime_init2,\n\t    test_nstime_copy,\n\t    test_nstime_compare,\n\t    test_nstime_add,\n\t    test_nstime_iadd,\n\t    test_nstime_subtract,\n\t    test_nstime_isubtract,\n\t    test_nstime_imultiply,\n\t    test_nstime_idivide,\n\t    test_nstime_divide,\n\t    test_nstime_monotonic,\n\t    test_nstime_update);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/pack.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n/*\n * Size class that is a divisor of the page size, ideally 4+ regions per run.\n */\n#if LG_PAGE <= 14\n#define SZ\t(ZU(1) << (LG_PAGE - 2))\n#else\n#define SZ\tZU(4096)\n#endif\n\n/*\n * Number of slabs to consume at high water mark.  Should be at least 2 so that\n * if mmap()ed memory grows downward, downward growth of mmap()ed memory is\n * tested.\n */\n#define NSLABS\t8\n\nstatic unsigned\nbinind_compute(void) {\n\tsize_t sz;\n\tunsigned nbins, i;\n\n\tsz = sizeof(nbins);\n\tassert_d_eq(mallctl(\"arenas.nbins\", (void *)&nbins, &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl failure\");\n\n\tfor (i = 0; i < nbins; i++) {\n\t\tsize_t mib[4];\n\t\tsize_t miblen = sizeof(mib)/sizeof(size_t);\n\t\tsize_t size;\n\n\t\tassert_d_eq(mallctlnametomib(\"arenas.bin.0.size\", mib,\n\t\t    &miblen), 0, \"Unexpected mallctlnametomb failure\");\n\t\tmib[2] = (size_t)i;\n\n\t\tsz = sizeof(size);\n\t\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&size, &sz, NULL,\n\t\t    0), 0, \"Unexpected mallctlbymib failure\");\n\t\tif (size == SZ) {\n\t\t\treturn i;\n\t\t}\n\t}\n\n\ttest_fail(\"Unable to compute nregs_per_run\");\n\treturn 0;\n}\n\nstatic size_t\nnregs_per_run_compute(void) {\n\tuint32_t nregs;\n\tsize_t sz;\n\tunsigned binind = binind_compute();\n\tsize_t mib[4];\n\tsize_t miblen = sizeof(mib)/sizeof(size_t);\n\n\tassert_d_eq(mallctlnametomib(\"arenas.bin.0.nregs\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomb failure\");\n\tmib[2] = (size_t)binind;\n\tsz = sizeof(nregs);\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&nregs, &sz, NULL,\n\t    0), 0, \"Unexpected mallctlbymib failure\");\n\treturn nregs;\n}\n\nstatic unsigned\narenas_create_mallctl(void) {\n\tunsigned arena_ind;\n\tsize_t sz;\n\n\tsz = sizeof(arena_ind);\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena_ind, &sz, NULL, 0),\n\t    0, \"Error in arenas.create\");\n\n\treturn arena_ind;\n}\n\nstatic void\narena_reset_mallctl(unsigned arena_ind) {\n\tsize_t mib[3];\n\tsize_t miblen = sizeof(mib)/sizeof(size_t);\n\n\tassert_d_eq(mallctlnametomib(\"arena.0.reset\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tmib[1] = (size_t)arena_ind;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n}\n\nTEST_BEGIN(test_pack) {\n\tunsigned arena_ind = arenas_create_mallctl();\n\tsize_t nregs_per_run = nregs_per_run_compute();\n\tsize_t nregs = nregs_per_run * NSLABS;\n\tVARIABLE_ARRAY(void *, ptrs, nregs);\n\tsize_t i, j, offset;\n\n\t/* Fill matrix. */\n\tfor (i = offset = 0; i < NSLABS; i++) {\n\t\tfor (j = 0; j < nregs_per_run; j++) {\n\t\t\tvoid *p = mallocx(SZ, MALLOCX_ARENA(arena_ind) |\n\t\t\t    MALLOCX_TCACHE_NONE);\n\t\t\tassert_ptr_not_null(p,\n\t\t\t    \"Unexpected mallocx(%zu, MALLOCX_ARENA(%u) |\"\n\t\t\t    \" MALLOCX_TCACHE_NONE) failure, run=%zu, reg=%zu\",\n\t\t\t    SZ, arena_ind, i, j);\n\t\t\tptrs[(i * nregs_per_run) + j] = p;\n\t\t}\n\t}\n\n\t/*\n\t * Free all but one region of each run, but rotate which region is\n\t * preserved, so that subsequent allocations exercise the within-run\n\t * layout policy.\n\t */\n\toffset = 0;\n\tfor (i = offset = 0;\n\t    i < NSLABS;\n\t    i++, offset = (offset + 1) % nregs_per_run) {\n\t\tfor (j = 0; j < nregs_per_run; j++) {\n\t\t\tvoid *p = ptrs[(i * nregs_per_run) + j];\n\t\t\tif (offset == j) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tdallocx(p, MALLOCX_ARENA(arena_ind) |\n\t\t\t    MALLOCX_TCACHE_NONE);\n\t\t}\n\t}\n\n\t/*\n\t * Logically refill matrix, skipping preserved regions and verifying\n\t * that the matrix is unmodified.\n\t */\n\toffset = 0;\n\tfor (i = offset = 0;\n\t    i < NSLABS;\n\t    i++, offset = (offset + 1) % nregs_per_run) {\n\t\tfor (j = 0; j < nregs_per_run; j++) {\n\t\t\tvoid *p;\n\n\t\t\tif (offset == j) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tp = mallocx(SZ, MALLOCX_ARENA(arena_ind) |\n\t\t\t    MALLOCX_TCACHE_NONE);\n\t\t\tassert_ptr_eq(p, ptrs[(i * nregs_per_run) + j],\n\t\t\t    \"Unexpected refill discrepancy, run=%zu, reg=%zu\\n\",\n\t\t\t    i, j);\n\t\t}\n\t}\n\n\t/* Clean up. */\n\tarena_reset_mallctl(arena_ind);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_pack);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/pack.sh",
    "content": "#!/bin/sh\n\n# Immediately purge to minimize fragmentation.\nexport MALLOC_CONF=\"dirty_decay_ms:0,muzzy_decay_ms:0\"\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/pages.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nTEST_BEGIN(test_pages_huge) {\n\tsize_t alloc_size;\n\tbool commit;\n\tvoid *pages, *hugepage;\n\n\talloc_size = HUGEPAGE * 2 - PAGE;\n\tcommit = true;\n\tpages = pages_map(NULL, alloc_size, PAGE, &commit);\n\tassert_ptr_not_null(pages, \"Unexpected pages_map() error\");\n\n\thugepage = (void *)(ALIGNMENT_CEILING((uintptr_t)pages, HUGEPAGE));\n\tassert_b_ne(pages_huge(hugepage, HUGEPAGE), config_thp,\n\t    \"Unexpected pages_huge() result\");\n\tassert_false(pages_nohuge(hugepage, HUGEPAGE),\n\t    \"Unexpected pages_nohuge() result\");\n\n\tpages_unmap(pages, alloc_size);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_pages_huge);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/ph.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/ph.h\"\n\ntypedef struct node_s node_t;\n\nstruct node_s {\n#define NODE_MAGIC 0x9823af7e\n\tuint32_t magic;\n\tphn(node_t) link;\n\tuint64_t key;\n};\n\nstatic int\nnode_cmp(const node_t *a, const node_t *b) {\n\tint ret;\n\n\tret = (a->key > b->key) - (a->key < b->key);\n\tif (ret == 0) {\n\t\t/*\n\t\t * Duplicates are not allowed in the heap, so force an\n\t\t * arbitrary ordering for non-identical items with equal keys.\n\t\t */\n\t\tret = (((uintptr_t)a) > ((uintptr_t)b))\n\t\t    - (((uintptr_t)a) < ((uintptr_t)b));\n\t}\n\treturn ret;\n}\n\nstatic int\nnode_cmp_magic(const node_t *a, const node_t *b) {\n\n\tassert_u32_eq(a->magic, NODE_MAGIC, \"Bad magic\");\n\tassert_u32_eq(b->magic, NODE_MAGIC, \"Bad magic\");\n\n\treturn node_cmp(a, b);\n}\n\ntypedef ph(node_t) heap_t;\nph_gen(static, heap_, heap_t, node_t, link, node_cmp_magic);\n\nstatic void\nnode_print(const node_t *node, unsigned depth) {\n\tunsigned i;\n\tnode_t *leftmost_child, *sibling;\n\n\tfor (i = 0; i < depth; i++) {\n\t\tmalloc_printf(\"\\t\");\n\t}\n\tmalloc_printf(\"%2\"FMTu64\"\\n\", node->key);\n\n\tleftmost_child = phn_lchild_get(node_t, link, node);\n\tif (leftmost_child == NULL) {\n\t\treturn;\n\t}\n\tnode_print(leftmost_child, depth + 1);\n\n\tfor (sibling = phn_next_get(node_t, link, leftmost_child); sibling !=\n\t    NULL; sibling = phn_next_get(node_t, link, sibling)) {\n\t\tnode_print(sibling, depth + 1);\n\t}\n}\n\nstatic void\nheap_print(const heap_t *heap) {\n\tnode_t *auxelm;\n\n\tmalloc_printf(\"vvv heap %p vvv\\n\", heap);\n\tif (heap->ph_root == NULL) {\n\t\tgoto label_return;\n\t}\n\n\tnode_print(heap->ph_root, 0);\n\n\tfor (auxelm = phn_next_get(node_t, link, heap->ph_root); auxelm != NULL;\n\t    auxelm = phn_next_get(node_t, link, auxelm)) {\n\t\tassert_ptr_eq(phn_next_get(node_t, link, phn_prev_get(node_t,\n\t\t    link, auxelm)), auxelm,\n\t\t    \"auxelm's prev doesn't link to auxelm\");\n\t\tnode_print(auxelm, 0);\n\t}\n\nlabel_return:\n\tmalloc_printf(\"^^^ heap %p ^^^\\n\", heap);\n}\n\nstatic unsigned\nnode_validate(const node_t *node, const node_t *parent) {\n\tunsigned nnodes = 1;\n\tnode_t *leftmost_child, *sibling;\n\n\tif (parent != NULL) {\n\t\tassert_d_ge(node_cmp_magic(node, parent), 0,\n\t\t    \"Child is less than parent\");\n\t}\n\n\tleftmost_child = phn_lchild_get(node_t, link, node);\n\tif (leftmost_child == NULL) {\n\t\treturn nnodes;\n\t}\n\tassert_ptr_eq((void *)phn_prev_get(node_t, link, leftmost_child),\n\t    (void *)node, \"Leftmost child does not link to node\");\n\tnnodes += node_validate(leftmost_child, node);\n\n\tfor (sibling = phn_next_get(node_t, link, leftmost_child); sibling !=\n\t    NULL; sibling = phn_next_get(node_t, link, sibling)) {\n\t\tassert_ptr_eq(phn_next_get(node_t, link, phn_prev_get(node_t,\n\t\t    link, sibling)), sibling,\n\t\t    \"sibling's prev doesn't link to sibling\");\n\t\tnnodes += node_validate(sibling, node);\n\t}\n\treturn nnodes;\n}\n\nstatic unsigned\nheap_validate(const heap_t *heap) {\n\tunsigned nnodes = 0;\n\tnode_t *auxelm;\n\n\tif (heap->ph_root == NULL) {\n\t\tgoto label_return;\n\t}\n\n\tnnodes += node_validate(heap->ph_root, NULL);\n\n\tfor (auxelm = phn_next_get(node_t, link, heap->ph_root); auxelm != NULL;\n\t    auxelm = phn_next_get(node_t, link, auxelm)) {\n\t\tassert_ptr_eq(phn_next_get(node_t, link, phn_prev_get(node_t,\n\t\t    link, auxelm)), auxelm,\n\t\t    \"auxelm's prev doesn't link to auxelm\");\n\t\tnnodes += node_validate(auxelm, NULL);\n\t}\n\nlabel_return:\n\tif (false) {\n\t\theap_print(heap);\n\t}\n\treturn nnodes;\n}\n\nTEST_BEGIN(test_ph_empty) {\n\theap_t heap;\n\n\theap_new(&heap);\n\tassert_true(heap_empty(&heap), \"Heap should be empty\");\n\tassert_ptr_null(heap_first(&heap), \"Unexpected node\");\n\tassert_ptr_null(heap_any(&heap), \"Unexpected node\");\n}\nTEST_END\n\nstatic void\nnode_remove(heap_t *heap, node_t *node) {\n\theap_remove(heap, node);\n\n\tnode->magic = 0;\n}\n\nstatic node_t *\nnode_remove_first(heap_t *heap) {\n\tnode_t *node = heap_remove_first(heap);\n\tnode->magic = 0;\n\treturn node;\n}\n\nstatic node_t *\nnode_remove_any(heap_t *heap) {\n\tnode_t *node = heap_remove_any(heap);\n\tnode->magic = 0;\n\treturn node;\n}\n\nTEST_BEGIN(test_ph_random) {\n#define NNODES 25\n#define NBAGS 250\n#define SEED 42\n\tsfmt_t *sfmt;\n\tuint64_t bag[NNODES];\n\theap_t heap;\n\tnode_t nodes[NNODES];\n\tunsigned i, j, k;\n\n\tsfmt = init_gen_rand(SEED);\n\tfor (i = 0; i < NBAGS; i++) {\n\t\tswitch (i) {\n\t\tcase 0:\n\t\t\t/* Insert in order. */\n\t\t\tfor (j = 0; j < NNODES; j++) {\n\t\t\t\tbag[j] = j;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase 1:\n\t\t\t/* Insert in reverse order. */\n\t\t\tfor (j = 0; j < NNODES; j++) {\n\t\t\t\tbag[j] = NNODES - j - 1;\n\t\t\t}\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tfor (j = 0; j < NNODES; j++) {\n\t\t\t\tbag[j] = gen_rand64_range(sfmt, NNODES);\n\t\t\t}\n\t\t}\n\n\t\tfor (j = 1; j <= NNODES; j++) {\n\t\t\t/* Initialize heap and nodes. */\n\t\t\theap_new(&heap);\n\t\t\tassert_u_eq(heap_validate(&heap), 0,\n\t\t\t    \"Incorrect node count\");\n\t\t\tfor (k = 0; k < j; k++) {\n\t\t\t\tnodes[k].magic = NODE_MAGIC;\n\t\t\t\tnodes[k].key = bag[k];\n\t\t\t}\n\n\t\t\t/* Insert nodes. */\n\t\t\tfor (k = 0; k < j; k++) {\n\t\t\t\theap_insert(&heap, &nodes[k]);\n\t\t\t\tif (i % 13 == 12) {\n\t\t\t\t\tassert_ptr_not_null(heap_any(&heap),\n\t\t\t\t\t    \"Heap should not be empty\");\n\t\t\t\t\t/* Trigger merging. */\n\t\t\t\t\tassert_ptr_not_null(heap_first(&heap),\n\t\t\t\t\t    \"Heap should not be empty\");\n\t\t\t\t}\n\t\t\t\tassert_u_eq(heap_validate(&heap), k + 1,\n\t\t\t\t    \"Incorrect node count\");\n\t\t\t}\n\n\t\t\tassert_false(heap_empty(&heap),\n\t\t\t    \"Heap should not be empty\");\n\n\t\t\t/* Remove nodes. */\n\t\t\tswitch (i % 6) {\n\t\t\tcase 0:\n\t\t\t\tfor (k = 0; k < j; k++) {\n\t\t\t\t\tassert_u_eq(heap_validate(&heap), j - k,\n\t\t\t\t\t    \"Incorrect node count\");\n\t\t\t\t\tnode_remove(&heap, &nodes[k]);\n\t\t\t\t\tassert_u_eq(heap_validate(&heap), j - k\n\t\t\t\t\t    - 1, \"Incorrect node count\");\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase 1:\n\t\t\t\tfor (k = j; k > 0; k--) {\n\t\t\t\t\tnode_remove(&heap, &nodes[k-1]);\n\t\t\t\t\tassert_u_eq(heap_validate(&heap), k - 1,\n\t\t\t\t\t    \"Incorrect node count\");\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase 2: {\n\t\t\t\tnode_t *prev = NULL;\n\t\t\t\tfor (k = 0; k < j; k++) {\n\t\t\t\t\tnode_t *node = node_remove_first(&heap);\n\t\t\t\t\tassert_u_eq(heap_validate(&heap), j - k\n\t\t\t\t\t    - 1, \"Incorrect node count\");\n\t\t\t\t\tif (prev != NULL) {\n\t\t\t\t\t\tassert_d_ge(node_cmp(node,\n\t\t\t\t\t\t    prev), 0,\n\t\t\t\t\t\t    \"Bad removal order\");\n\t\t\t\t\t}\n\t\t\t\t\tprev = node;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t} case 3: {\n\t\t\t\tnode_t *prev = NULL;\n\t\t\t\tfor (k = 0; k < j; k++) {\n\t\t\t\t\tnode_t *node = heap_first(&heap);\n\t\t\t\t\tassert_u_eq(heap_validate(&heap), j - k,\n\t\t\t\t\t    \"Incorrect node count\");\n\t\t\t\t\tif (prev != NULL) {\n\t\t\t\t\t\tassert_d_ge(node_cmp(node,\n\t\t\t\t\t\t    prev), 0,\n\t\t\t\t\t\t    \"Bad removal order\");\n\t\t\t\t\t}\n\t\t\t\t\tnode_remove(&heap, node);\n\t\t\t\t\tassert_u_eq(heap_validate(&heap), j - k\n\t\t\t\t\t    - 1, \"Incorrect node count\");\n\t\t\t\t\tprev = node;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t} case 4: {\n\t\t\t\tfor (k = 0; k < j; k++) {\n\t\t\t\t\tnode_remove_any(&heap);\n\t\t\t\t\tassert_u_eq(heap_validate(&heap), j - k\n\t\t\t\t\t    - 1, \"Incorrect node count\");\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t} case 5: {\n\t\t\t\tfor (k = 0; k < j; k++) {\n\t\t\t\t\tnode_t *node = heap_any(&heap);\n\t\t\t\t\tassert_u_eq(heap_validate(&heap), j - k,\n\t\t\t\t\t    \"Incorrect node count\");\n\t\t\t\t\tnode_remove(&heap, node);\n\t\t\t\t\tassert_u_eq(heap_validate(&heap), j - k\n\t\t\t\t\t    - 1, \"Incorrect node count\");\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t} default:\n\t\t\t\tnot_reached();\n\t\t\t}\n\n\t\t\tassert_ptr_null(heap_first(&heap),\n\t\t\t    \"Heap should be empty\");\n\t\t\tassert_ptr_null(heap_any(&heap),\n\t\t\t    \"Heap should be empty\");\n\t\t\tassert_true(heap_empty(&heap), \"Heap should be empty\");\n\t\t}\n\t}\n\tfini_gen_rand(sfmt);\n#undef NNODES\n#undef SEED\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_ph_empty,\n\t    test_ph_random);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/prng.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic void\ntest_prng_lg_range_u32(bool atomic) {\n\tatomic_u32_t sa, sb;\n\tuint32_t ra, rb;\n\tunsigned lg_range;\n\n\tatomic_store_u32(&sa, 42, ATOMIC_RELAXED);\n\tra = prng_lg_range_u32(&sa, 32, atomic);\n\tatomic_store_u32(&sa, 42, ATOMIC_RELAXED);\n\trb = prng_lg_range_u32(&sa, 32, atomic);\n\tassert_u32_eq(ra, rb,\n\t    \"Repeated generation should produce repeated results\");\n\n\tatomic_store_u32(&sb, 42, ATOMIC_RELAXED);\n\trb = prng_lg_range_u32(&sb, 32, atomic);\n\tassert_u32_eq(ra, rb,\n\t    \"Equivalent generation should produce equivalent results\");\n\n\tatomic_store_u32(&sa, 42, ATOMIC_RELAXED);\n\tra = prng_lg_range_u32(&sa, 32, atomic);\n\trb = prng_lg_range_u32(&sa, 32, atomic);\n\tassert_u32_ne(ra, rb,\n\t    \"Full-width results must not immediately repeat\");\n\n\tatomic_store_u32(&sa, 42, ATOMIC_RELAXED);\n\tra = prng_lg_range_u32(&sa, 32, atomic);\n\tfor (lg_range = 31; lg_range > 0; lg_range--) {\n\t\tatomic_store_u32(&sb, 42, ATOMIC_RELAXED);\n\t\trb = prng_lg_range_u32(&sb, lg_range, atomic);\n\t\tassert_u32_eq((rb & (UINT32_C(0xffffffff) << lg_range)),\n\t\t    0, \"High order bits should be 0, lg_range=%u\", lg_range);\n\t\tassert_u32_eq(rb, (ra >> (32 - lg_range)),\n\t\t    \"Expected high order bits of full-width result, \"\n\t\t    \"lg_range=%u\", lg_range);\n\t}\n}\n\nstatic void\ntest_prng_lg_range_u64(void) {\n\tuint64_t sa, sb, ra, rb;\n\tunsigned lg_range;\n\n\tsa = 42;\n\tra = prng_lg_range_u64(&sa, 64);\n\tsa = 42;\n\trb = prng_lg_range_u64(&sa, 64);\n\tassert_u64_eq(ra, rb,\n\t    \"Repeated generation should produce repeated results\");\n\n\tsb = 42;\n\trb = prng_lg_range_u64(&sb, 64);\n\tassert_u64_eq(ra, rb,\n\t    \"Equivalent generation should produce equivalent results\");\n\n\tsa = 42;\n\tra = prng_lg_range_u64(&sa, 64);\n\trb = prng_lg_range_u64(&sa, 64);\n\tassert_u64_ne(ra, rb,\n\t    \"Full-width results must not immediately repeat\");\n\n\tsa = 42;\n\tra = prng_lg_range_u64(&sa, 64);\n\tfor (lg_range = 63; lg_range > 0; lg_range--) {\n\t\tsb = 42;\n\t\trb = prng_lg_range_u64(&sb, lg_range);\n\t\tassert_u64_eq((rb & (UINT64_C(0xffffffffffffffff) << lg_range)),\n\t\t    0, \"High order bits should be 0, lg_range=%u\", lg_range);\n\t\tassert_u64_eq(rb, (ra >> (64 - lg_range)),\n\t\t    \"Expected high order bits of full-width result, \"\n\t\t    \"lg_range=%u\", lg_range);\n\t}\n}\n\nstatic void\ntest_prng_lg_range_zu(bool atomic) {\n\tatomic_zu_t sa, sb;\n\tsize_t ra, rb;\n\tunsigned lg_range;\n\n\tatomic_store_zu(&sa, 42, ATOMIC_RELAXED);\n\tra = prng_lg_range_zu(&sa, ZU(1) << (3 + LG_SIZEOF_PTR), atomic);\n\tatomic_store_zu(&sa, 42, ATOMIC_RELAXED);\n\trb = prng_lg_range_zu(&sa, ZU(1) << (3 + LG_SIZEOF_PTR), atomic);\n\tassert_zu_eq(ra, rb,\n\t    \"Repeated generation should produce repeated results\");\n\n\tatomic_store_zu(&sb, 42, ATOMIC_RELAXED);\n\trb = prng_lg_range_zu(&sb, ZU(1) << (3 + LG_SIZEOF_PTR), atomic);\n\tassert_zu_eq(ra, rb,\n\t    \"Equivalent generation should produce equivalent results\");\n\n\tatomic_store_zu(&sa, 42, ATOMIC_RELAXED);\n\tra = prng_lg_range_zu(&sa, ZU(1) << (3 + LG_SIZEOF_PTR), atomic);\n\trb = prng_lg_range_zu(&sa, ZU(1) << (3 + LG_SIZEOF_PTR), atomic);\n\tassert_zu_ne(ra, rb,\n\t    \"Full-width results must not immediately repeat\");\n\n\tatomic_store_zu(&sa, 42, ATOMIC_RELAXED);\n\tra = prng_lg_range_zu(&sa, ZU(1) << (3 + LG_SIZEOF_PTR), atomic);\n\tfor (lg_range = (ZU(1) << (3 + LG_SIZEOF_PTR)) - 1; lg_range > 0;\n\t    lg_range--) {\n\t\tatomic_store_zu(&sb, 42, ATOMIC_RELAXED);\n\t\trb = prng_lg_range_zu(&sb, lg_range, atomic);\n\t\tassert_zu_eq((rb & (SIZE_T_MAX << lg_range)),\n\t\t    0, \"High order bits should be 0, lg_range=%u\", lg_range);\n\t\tassert_zu_eq(rb, (ra >> ((ZU(1) << (3 + LG_SIZEOF_PTR)) -\n\t\t    lg_range)), \"Expected high order bits of full-width \"\n\t\t    \"result, lg_range=%u\", lg_range);\n\t}\n}\n\nTEST_BEGIN(test_prng_lg_range_u32_nonatomic) {\n\ttest_prng_lg_range_u32(false);\n}\nTEST_END\n\nTEST_BEGIN(test_prng_lg_range_u32_atomic) {\n\ttest_prng_lg_range_u32(true);\n}\nTEST_END\n\nTEST_BEGIN(test_prng_lg_range_u64_nonatomic) {\n\ttest_prng_lg_range_u64();\n}\nTEST_END\n\nTEST_BEGIN(test_prng_lg_range_zu_nonatomic) {\n\ttest_prng_lg_range_zu(false);\n}\nTEST_END\n\nTEST_BEGIN(test_prng_lg_range_zu_atomic) {\n\ttest_prng_lg_range_zu(true);\n}\nTEST_END\n\nstatic void\ntest_prng_range_u32(bool atomic) {\n\tuint32_t range;\n#define MAX_RANGE\t10000000\n#define RANGE_STEP\t97\n#define NREPS\t\t10\n\n\tfor (range = 2; range < MAX_RANGE; range += RANGE_STEP) {\n\t\tatomic_u32_t s;\n\t\tunsigned rep;\n\n\t\tatomic_store_u32(&s, range, ATOMIC_RELAXED);\n\t\tfor (rep = 0; rep < NREPS; rep++) {\n\t\t\tuint32_t r = prng_range_u32(&s, range, atomic);\n\n\t\t\tassert_u32_lt(r, range, \"Out of range\");\n\t\t}\n\t}\n}\n\nstatic void\ntest_prng_range_u64(void) {\n\tuint64_t range;\n#define MAX_RANGE\t10000000\n#define RANGE_STEP\t97\n#define NREPS\t\t10\n\n\tfor (range = 2; range < MAX_RANGE; range += RANGE_STEP) {\n\t\tuint64_t s;\n\t\tunsigned rep;\n\n\t\ts = range;\n\t\tfor (rep = 0; rep < NREPS; rep++) {\n\t\t\tuint64_t r = prng_range_u64(&s, range);\n\n\t\t\tassert_u64_lt(r, range, \"Out of range\");\n\t\t}\n\t}\n}\n\nstatic void\ntest_prng_range_zu(bool atomic) {\n\tsize_t range;\n#define MAX_RANGE\t10000000\n#define RANGE_STEP\t97\n#define NREPS\t\t10\n\n\tfor (range = 2; range < MAX_RANGE; range += RANGE_STEP) {\n\t\tatomic_zu_t s;\n\t\tunsigned rep;\n\n\t\tatomic_store_zu(&s, range, ATOMIC_RELAXED);\n\t\tfor (rep = 0; rep < NREPS; rep++) {\n\t\t\tsize_t r = prng_range_zu(&s, range, atomic);\n\n\t\t\tassert_zu_lt(r, range, \"Out of range\");\n\t\t}\n\t}\n}\n\nTEST_BEGIN(test_prng_range_u32_nonatomic) {\n\ttest_prng_range_u32(false);\n}\nTEST_END\n\nTEST_BEGIN(test_prng_range_u32_atomic) {\n\ttest_prng_range_u32(true);\n}\nTEST_END\n\nTEST_BEGIN(test_prng_range_u64_nonatomic) {\n\ttest_prng_range_u64();\n}\nTEST_END\n\nTEST_BEGIN(test_prng_range_zu_nonatomic) {\n\ttest_prng_range_zu(false);\n}\nTEST_END\n\nTEST_BEGIN(test_prng_range_zu_atomic) {\n\ttest_prng_range_zu(true);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_prng_lg_range_u32_nonatomic,\n\t    test_prng_lg_range_u32_atomic,\n\t    test_prng_lg_range_u64_nonatomic,\n\t    test_prng_lg_range_zu_nonatomic,\n\t    test_prng_lg_range_zu_atomic,\n\t    test_prng_range_u32_nonatomic,\n\t    test_prng_range_u32_atomic,\n\t    test_prng_range_u64_nonatomic,\n\t    test_prng_range_zu_nonatomic,\n\t    test_prng_range_zu_atomic);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/prof_accum.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#define NTHREADS\t\t4\n#define NALLOCS_PER_THREAD\t50\n#define DUMP_INTERVAL\t\t1\n#define BT_COUNT_CHECK_INTERVAL\t5\n\nstatic int\nprof_dump_open_intercept(bool propagate_err, const char *filename) {\n\tint fd;\n\n\tfd = open(\"/dev/null\", O_WRONLY);\n\tassert_d_ne(fd, -1, \"Unexpected open() failure\");\n\n\treturn fd;\n}\n\nstatic void *\nalloc_from_permuted_backtrace(unsigned thd_ind, unsigned iteration) {\n\treturn btalloc(1, thd_ind*NALLOCS_PER_THREAD + iteration);\n}\n\nstatic void *\nthd_start(void *varg) {\n\tunsigned thd_ind = *(unsigned *)varg;\n\tsize_t bt_count_prev, bt_count;\n\tunsigned i_prev, i;\n\n\ti_prev = 0;\n\tbt_count_prev = 0;\n\tfor (i = 0; i < NALLOCS_PER_THREAD; i++) {\n\t\tvoid *p = alloc_from_permuted_backtrace(thd_ind, i);\n\t\tdallocx(p, 0);\n\t\tif (i % DUMP_INTERVAL == 0) {\n\t\t\tassert_d_eq(mallctl(\"prof.dump\", NULL, NULL, NULL, 0),\n\t\t\t    0, \"Unexpected error while dumping heap profile\");\n\t\t}\n\n\t\tif (i % BT_COUNT_CHECK_INTERVAL == 0 ||\n\t\t    i+1 == NALLOCS_PER_THREAD) {\n\t\t\tbt_count = prof_bt_count();\n\t\t\tassert_zu_le(bt_count_prev+(i-i_prev), bt_count,\n\t\t\t    \"Expected larger backtrace count increase\");\n\t\t\ti_prev = i;\n\t\t\tbt_count_prev = bt_count;\n\t\t}\n\t}\n\n\treturn NULL;\n}\n\nTEST_BEGIN(test_idump) {\n\tbool active;\n\tthd_t thds[NTHREADS];\n\tunsigned thd_args[NTHREADS];\n\tunsigned i;\n\n\ttest_skip_if(!config_prof);\n\n\tactive = true;\n\tassert_d_eq(mallctl(\"prof.active\", NULL, NULL, (void *)&active,\n\t    sizeof(active)), 0,\n\t    \"Unexpected mallctl failure while activating profiling\");\n\n\tprof_dump_open = prof_dump_open_intercept;\n\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tthd_args[i] = i;\n\t\tthd_create(&thds[i], thd_start, (void *)&thd_args[i]);\n\t}\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tthd_join(thds[i], NULL);\n\t}\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test_no_reentrancy(\n\t    test_idump);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/prof_accum.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_prof}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"prof:true,prof_accum:true,prof_active:false,lg_prof_sample:0\"\nfi\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/prof_active.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic void\nmallctl_bool_get(const char *name, bool expected, const char *func, int line) {\n\tbool old;\n\tsize_t sz;\n\n\tsz = sizeof(old);\n\tassert_d_eq(mallctl(name, (void *)&old, &sz, NULL, 0), 0,\n\t    \"%s():%d: Unexpected mallctl failure reading %s\", func, line, name);\n\tassert_b_eq(old, expected, \"%s():%d: Unexpected %s value\", func, line,\n\t    name);\n}\n\nstatic void\nmallctl_bool_set(const char *name, bool old_expected, bool val_new,\n    const char *func, int line) {\n\tbool old;\n\tsize_t sz;\n\n\tsz = sizeof(old);\n\tassert_d_eq(mallctl(name, (void *)&old, &sz, (void *)&val_new,\n\t    sizeof(val_new)), 0,\n\t    \"%s():%d: Unexpected mallctl failure reading/writing %s\", func,\n\t    line, name);\n\tassert_b_eq(old, old_expected, \"%s():%d: Unexpected %s value\", func,\n\t    line, name);\n}\n\nstatic void\nmallctl_prof_active_get_impl(bool prof_active_old_expected, const char *func,\n    int line) {\n\tmallctl_bool_get(\"prof.active\", prof_active_old_expected, func, line);\n}\n#define mallctl_prof_active_get(a)\t\t\t\t\t\\\n\tmallctl_prof_active_get_impl(a, __func__, __LINE__)\n\nstatic void\nmallctl_prof_active_set_impl(bool prof_active_old_expected,\n    bool prof_active_new, const char *func, int line) {\n\tmallctl_bool_set(\"prof.active\", prof_active_old_expected,\n\t    prof_active_new, func, line);\n}\n#define mallctl_prof_active_set(a, b)\t\t\t\t\t\\\n\tmallctl_prof_active_set_impl(a, b, __func__, __LINE__)\n\nstatic void\nmallctl_thread_prof_active_get_impl(bool thread_prof_active_old_expected,\n    const char *func, int line) {\n\tmallctl_bool_get(\"thread.prof.active\", thread_prof_active_old_expected,\n\t    func, line);\n}\n#define mallctl_thread_prof_active_get(a)\t\t\t\t\\\n\tmallctl_thread_prof_active_get_impl(a, __func__, __LINE__)\n\nstatic void\nmallctl_thread_prof_active_set_impl(bool thread_prof_active_old_expected,\n    bool thread_prof_active_new, const char *func, int line) {\n\tmallctl_bool_set(\"thread.prof.active\", thread_prof_active_old_expected,\n\t    thread_prof_active_new, func, line);\n}\n#define mallctl_thread_prof_active_set(a, b)\t\t\t\t\\\n\tmallctl_thread_prof_active_set_impl(a, b, __func__, __LINE__)\n\nstatic void\nprof_sampling_probe_impl(bool expect_sample, const char *func, int line) {\n\tvoid *p;\n\tsize_t expected_backtraces = expect_sample ? 1 : 0;\n\n\tassert_zu_eq(prof_bt_count(), 0, \"%s():%d: Expected 0 backtraces\", func,\n\t    line);\n\tp = mallocx(1, 0);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\tassert_zu_eq(prof_bt_count(), expected_backtraces,\n\t    \"%s():%d: Unexpected backtrace count\", func, line);\n\tdallocx(p, 0);\n}\n#define prof_sampling_probe(a)\t\t\t\t\t\t\\\n\tprof_sampling_probe_impl(a, __func__, __LINE__)\n\nTEST_BEGIN(test_prof_active) {\n\ttest_skip_if(!config_prof);\n\n\tmallctl_prof_active_get(true);\n\tmallctl_thread_prof_active_get(false);\n\n\tmallctl_prof_active_set(true, true);\n\tmallctl_thread_prof_active_set(false, false);\n\t/* prof.active, !thread.prof.active. */\n\tprof_sampling_probe(false);\n\n\tmallctl_prof_active_set(true, false);\n\tmallctl_thread_prof_active_set(false, false);\n\t/* !prof.active, !thread.prof.active. */\n\tprof_sampling_probe(false);\n\n\tmallctl_prof_active_set(false, false);\n\tmallctl_thread_prof_active_set(false, true);\n\t/* !prof.active, thread.prof.active. */\n\tprof_sampling_probe(false);\n\n\tmallctl_prof_active_set(false, true);\n\tmallctl_thread_prof_active_set(true, true);\n\t/* prof.active, thread.prof.active. */\n\tprof_sampling_probe(true);\n\n\t/* Restore settings. */\n\tmallctl_prof_active_set(true, true);\n\tmallctl_thread_prof_active_set(true, false);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test_no_reentrancy(\n\t    test_prof_active);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/prof_active.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_prof}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"prof:true,prof_thread_active_init:false,lg_prof_sample:0\"\nfi\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/prof_gdump.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic bool did_prof_dump_open;\n\nstatic int\nprof_dump_open_intercept(bool propagate_err, const char *filename) {\n\tint fd;\n\n\tdid_prof_dump_open = true;\n\n\tfd = open(\"/dev/null\", O_WRONLY);\n\tassert_d_ne(fd, -1, \"Unexpected open() failure\");\n\n\treturn fd;\n}\n\nTEST_BEGIN(test_gdump) {\n\tbool active, gdump, gdump_old;\n\tvoid *p, *q, *r, *s;\n\tsize_t sz;\n\n\ttest_skip_if(!config_prof);\n\n\tactive = true;\n\tassert_d_eq(mallctl(\"prof.active\", NULL, NULL, (void *)&active,\n\t    sizeof(active)), 0,\n\t    \"Unexpected mallctl failure while activating profiling\");\n\n\tprof_dump_open = prof_dump_open_intercept;\n\n\tdid_prof_dump_open = false;\n\tp = mallocx((1U << LG_LARGE_MINCLASS), 0);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\tassert_true(did_prof_dump_open, \"Expected a profile dump\");\n\n\tdid_prof_dump_open = false;\n\tq = mallocx((1U << LG_LARGE_MINCLASS), 0);\n\tassert_ptr_not_null(q, \"Unexpected mallocx() failure\");\n\tassert_true(did_prof_dump_open, \"Expected a profile dump\");\n\n\tgdump = false;\n\tsz = sizeof(gdump_old);\n\tassert_d_eq(mallctl(\"prof.gdump\", (void *)&gdump_old, &sz,\n\t    (void *)&gdump, sizeof(gdump)), 0,\n\t    \"Unexpected mallctl failure while disabling prof.gdump\");\n\tassert(gdump_old);\n\tdid_prof_dump_open = false;\n\tr = mallocx((1U << LG_LARGE_MINCLASS), 0);\n\tassert_ptr_not_null(q, \"Unexpected mallocx() failure\");\n\tassert_false(did_prof_dump_open, \"Unexpected profile dump\");\n\n\tgdump = true;\n\tsz = sizeof(gdump_old);\n\tassert_d_eq(mallctl(\"prof.gdump\", (void *)&gdump_old, &sz,\n\t    (void *)&gdump, sizeof(gdump)), 0,\n\t    \"Unexpected mallctl failure while enabling prof.gdump\");\n\tassert(!gdump_old);\n\tdid_prof_dump_open = false;\n\ts = mallocx((1U << LG_LARGE_MINCLASS), 0);\n\tassert_ptr_not_null(q, \"Unexpected mallocx() failure\");\n\tassert_true(did_prof_dump_open, \"Expected a profile dump\");\n\n\tdallocx(p, 0);\n\tdallocx(q, 0);\n\tdallocx(r, 0);\n\tdallocx(s, 0);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test_no_reentrancy(\n\t    test_gdump);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/prof_gdump.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_prof}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"prof:true,prof_active:false,prof_gdump:true\"\nfi\n\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/prof_idump.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic bool did_prof_dump_open;\n\nstatic int\nprof_dump_open_intercept(bool propagate_err, const char *filename) {\n\tint fd;\n\n\tdid_prof_dump_open = true;\n\n\tfd = open(\"/dev/null\", O_WRONLY);\n\tassert_d_ne(fd, -1, \"Unexpected open() failure\");\n\n\treturn fd;\n}\n\nTEST_BEGIN(test_idump) {\n\tbool active;\n\tvoid *p;\n\n\ttest_skip_if(!config_prof);\n\n\tactive = true;\n\tassert_d_eq(mallctl(\"prof.active\", NULL, NULL, (void *)&active,\n\t    sizeof(active)), 0,\n\t    \"Unexpected mallctl failure while activating profiling\");\n\n\tprof_dump_open = prof_dump_open_intercept;\n\n\tdid_prof_dump_open = false;\n\tp = mallocx(1, 0);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\tdallocx(p, 0);\n\tassert_true(did_prof_dump_open, \"Expected a profile dump\");\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_idump);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/prof_idump.sh",
    "content": "#!/bin/sh\n\nexport MALLOC_CONF=\"tcache:false\"\nif [ \"x${enable_prof}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"${MALLOC_CONF},prof:true,prof_accum:true,prof_active:false,lg_prof_sample:0,lg_prof_interval:0\"\nfi\n\n\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/prof_reset.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic int\nprof_dump_open_intercept(bool propagate_err, const char *filename) {\n\tint fd;\n\n\tfd = open(\"/dev/null\", O_WRONLY);\n\tassert_d_ne(fd, -1, \"Unexpected open() failure\");\n\n\treturn fd;\n}\n\nstatic void\nset_prof_active(bool active) {\n\tassert_d_eq(mallctl(\"prof.active\", NULL, NULL, (void *)&active,\n\t    sizeof(active)), 0, \"Unexpected mallctl failure\");\n}\n\nstatic size_t\nget_lg_prof_sample(void) {\n\tsize_t lg_prof_sample;\n\tsize_t sz = sizeof(size_t);\n\n\tassert_d_eq(mallctl(\"prof.lg_sample\", (void *)&lg_prof_sample, &sz,\n\t    NULL, 0), 0,\n\t    \"Unexpected mallctl failure while reading profiling sample rate\");\n\treturn lg_prof_sample;\n}\n\nstatic void\ndo_prof_reset(size_t lg_prof_sample) {\n\tassert_d_eq(mallctl(\"prof.reset\", NULL, NULL,\n\t    (void *)&lg_prof_sample, sizeof(size_t)), 0,\n\t    \"Unexpected mallctl failure while resetting profile data\");\n\tassert_zu_eq(lg_prof_sample, get_lg_prof_sample(),\n\t    \"Expected profile sample rate change\");\n}\n\nTEST_BEGIN(test_prof_reset_basic) {\n\tsize_t lg_prof_sample_orig, lg_prof_sample, lg_prof_sample_next;\n\tsize_t sz;\n\tunsigned i;\n\n\ttest_skip_if(!config_prof);\n\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctl(\"opt.lg_prof_sample\", (void *)&lg_prof_sample_orig,\n\t    &sz, NULL, 0), 0,\n\t    \"Unexpected mallctl failure while reading profiling sample rate\");\n\tassert_zu_eq(lg_prof_sample_orig, 0,\n\t    \"Unexpected profiling sample rate\");\n\tlg_prof_sample = get_lg_prof_sample();\n\tassert_zu_eq(lg_prof_sample_orig, lg_prof_sample,\n\t    \"Unexpected disagreement between \\\"opt.lg_prof_sample\\\" and \"\n\t    \"\\\"prof.lg_sample\\\"\");\n\n\t/* Test simple resets. */\n\tfor (i = 0; i < 2; i++) {\n\t\tassert_d_eq(mallctl(\"prof.reset\", NULL, NULL, NULL, 0), 0,\n\t\t    \"Unexpected mallctl failure while resetting profile data\");\n\t\tlg_prof_sample = get_lg_prof_sample();\n\t\tassert_zu_eq(lg_prof_sample_orig, lg_prof_sample,\n\t\t    \"Unexpected profile sample rate change\");\n\t}\n\n\t/* Test resets with prof.lg_sample changes. */\n\tlg_prof_sample_next = 1;\n\tfor (i = 0; i < 2; i++) {\n\t\tdo_prof_reset(lg_prof_sample_next);\n\t\tlg_prof_sample = get_lg_prof_sample();\n\t\tassert_zu_eq(lg_prof_sample, lg_prof_sample_next,\n\t\t    \"Expected profile sample rate change\");\n\t\tlg_prof_sample_next = lg_prof_sample_orig;\n\t}\n\n\t/* Make sure the test code restored prof.lg_sample. */\n\tlg_prof_sample = get_lg_prof_sample();\n\tassert_zu_eq(lg_prof_sample_orig, lg_prof_sample,\n\t    \"Unexpected disagreement between \\\"opt.lg_prof_sample\\\" and \"\n\t    \"\\\"prof.lg_sample\\\"\");\n}\nTEST_END\n\nbool prof_dump_header_intercepted = false;\nprof_cnt_t cnt_all_copy = {0, 0, 0, 0};\nstatic bool\nprof_dump_header_intercept(tsdn_t *tsdn, bool propagate_err,\n    const prof_cnt_t *cnt_all) {\n\tprof_dump_header_intercepted = true;\n\tmemcpy(&cnt_all_copy, cnt_all, sizeof(prof_cnt_t));\n\n\treturn false;\n}\n\nTEST_BEGIN(test_prof_reset_cleanup) {\n\tvoid *p;\n\tprof_dump_header_t *prof_dump_header_orig;\n\n\ttest_skip_if(!config_prof);\n\n\tset_prof_active(true);\n\n\tassert_zu_eq(prof_bt_count(), 0, \"Expected 0 backtraces\");\n\tp = mallocx(1, 0);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\tassert_zu_eq(prof_bt_count(), 1, \"Expected 1 backtrace\");\n\n\tprof_dump_header_orig = prof_dump_header;\n\tprof_dump_header = prof_dump_header_intercept;\n\tassert_false(prof_dump_header_intercepted, \"Unexpected intercept\");\n\n\tassert_d_eq(mallctl(\"prof.dump\", NULL, NULL, NULL, 0),\n\t    0, \"Unexpected error while dumping heap profile\");\n\tassert_true(prof_dump_header_intercepted, \"Expected intercept\");\n\tassert_u64_eq(cnt_all_copy.curobjs, 1, \"Expected 1 allocation\");\n\n\tassert_d_eq(mallctl(\"prof.reset\", NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected error while resetting heap profile data\");\n\tassert_d_eq(mallctl(\"prof.dump\", NULL, NULL, NULL, 0),\n\t    0, \"Unexpected error while dumping heap profile\");\n\tassert_u64_eq(cnt_all_copy.curobjs, 0, \"Expected 0 allocations\");\n\tassert_zu_eq(prof_bt_count(), 1, \"Expected 1 backtrace\");\n\n\tprof_dump_header = prof_dump_header_orig;\n\n\tdallocx(p, 0);\n\tassert_zu_eq(prof_bt_count(), 0, \"Expected 0 backtraces\");\n\n\tset_prof_active(false);\n}\nTEST_END\n\n#define NTHREADS\t\t4\n#define NALLOCS_PER_THREAD\t(1U << 13)\n#define OBJ_RING_BUF_COUNT\t1531\n#define RESET_INTERVAL\t\t(1U << 10)\n#define DUMP_INTERVAL\t\t3677\nstatic void *\nthd_start(void *varg) {\n\tunsigned thd_ind = *(unsigned *)varg;\n\tunsigned i;\n\tvoid *objs[OBJ_RING_BUF_COUNT];\n\n\tmemset(objs, 0, sizeof(objs));\n\n\tfor (i = 0; i < NALLOCS_PER_THREAD; i++) {\n\t\tif (i % RESET_INTERVAL == 0) {\n\t\t\tassert_d_eq(mallctl(\"prof.reset\", NULL, NULL, NULL, 0),\n\t\t\t    0, \"Unexpected error while resetting heap profile \"\n\t\t\t    \"data\");\n\t\t}\n\n\t\tif (i % DUMP_INTERVAL == 0) {\n\t\t\tassert_d_eq(mallctl(\"prof.dump\", NULL, NULL, NULL, 0),\n\t\t\t    0, \"Unexpected error while dumping heap profile\");\n\t\t}\n\n\t\t{\n\t\t\tvoid **pp = &objs[i % OBJ_RING_BUF_COUNT];\n\t\t\tif (*pp != NULL) {\n\t\t\t\tdallocx(*pp, 0);\n\t\t\t\t*pp = NULL;\n\t\t\t}\n\t\t\t*pp = btalloc(1, thd_ind*NALLOCS_PER_THREAD + i);\n\t\t\tassert_ptr_not_null(*pp,\n\t\t\t    \"Unexpected btalloc() failure\");\n\t\t}\n\t}\n\n\t/* Clean up any remaining objects. */\n\tfor (i = 0; i < OBJ_RING_BUF_COUNT; i++) {\n\t\tvoid **pp = &objs[i % OBJ_RING_BUF_COUNT];\n\t\tif (*pp != NULL) {\n\t\t\tdallocx(*pp, 0);\n\t\t\t*pp = NULL;\n\t\t}\n\t}\n\n\treturn NULL;\n}\n\nTEST_BEGIN(test_prof_reset) {\n\tsize_t lg_prof_sample_orig;\n\tthd_t thds[NTHREADS];\n\tunsigned thd_args[NTHREADS];\n\tunsigned i;\n\tsize_t bt_count, tdata_count;\n\n\ttest_skip_if(!config_prof);\n\n\tbt_count = prof_bt_count();\n\tassert_zu_eq(bt_count, 0,\n\t    \"Unexpected pre-existing tdata structures\");\n\ttdata_count = prof_tdata_count();\n\n\tlg_prof_sample_orig = get_lg_prof_sample();\n\tdo_prof_reset(5);\n\n\tset_prof_active(true);\n\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tthd_args[i] = i;\n\t\tthd_create(&thds[i], thd_start, (void *)&thd_args[i]);\n\t}\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tthd_join(thds[i], NULL);\n\t}\n\n\tassert_zu_eq(prof_bt_count(), bt_count,\n\t    \"Unexpected bactrace count change\");\n\tassert_zu_eq(prof_tdata_count(), tdata_count,\n\t    \"Unexpected remaining tdata structures\");\n\n\tset_prof_active(false);\n\n\tdo_prof_reset(lg_prof_sample_orig);\n}\nTEST_END\n#undef NTHREADS\n#undef NALLOCS_PER_THREAD\n#undef OBJ_RING_BUF_COUNT\n#undef RESET_INTERVAL\n#undef DUMP_INTERVAL\n\n/* Test sampling at the same allocation site across resets. */\n#define NITER 10\nTEST_BEGIN(test_xallocx) {\n\tsize_t lg_prof_sample_orig;\n\tunsigned i;\n\tvoid *ptrs[NITER];\n\n\ttest_skip_if(!config_prof);\n\n\tlg_prof_sample_orig = get_lg_prof_sample();\n\tset_prof_active(true);\n\n\t/* Reset profiling. */\n\tdo_prof_reset(0);\n\n\tfor (i = 0; i < NITER; i++) {\n\t\tvoid *p;\n\t\tsize_t sz, nsz;\n\n\t\t/* Reset profiling. */\n\t\tdo_prof_reset(0);\n\n\t\t/* Allocate small object (which will be promoted). */\n\t\tp = ptrs[i] = mallocx(1, 0);\n\t\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\n\t\t/* Reset profiling. */\n\t\tdo_prof_reset(0);\n\n\t\t/* Perform successful xallocx(). */\n\t\tsz = sallocx(p, 0);\n\t\tassert_zu_eq(xallocx(p, sz, 0, 0), sz,\n\t\t    \"Unexpected xallocx() failure\");\n\n\t\t/* Perform unsuccessful xallocx(). */\n\t\tnsz = nallocx(sz+1, 0);\n\t\tassert_zu_eq(xallocx(p, nsz, 0, 0), sz,\n\t\t    \"Unexpected xallocx() success\");\n\t}\n\n\tfor (i = 0; i < NITER; i++) {\n\t\t/* dallocx. */\n\t\tdallocx(ptrs[i], 0);\n\t}\n\n\tset_prof_active(false);\n\tdo_prof_reset(lg_prof_sample_orig);\n}\nTEST_END\n#undef NITER\n\nint\nmain(void) {\n\t/* Intercept dumping prior to running any tests. */\n\tprof_dump_open = prof_dump_open_intercept;\n\n\treturn test_no_reentrancy(\n\t    test_prof_reset_basic,\n\t    test_prof_reset_cleanup,\n\t    test_prof_reset,\n\t    test_xallocx);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/prof_reset.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_prof}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"prof:true,prof_active:false,lg_prof_sample:0\"\nfi\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/prof_tctx.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nTEST_BEGIN(test_prof_realloc) {\n\ttsdn_t *tsdn;\n\tint flags;\n\tvoid *p, *q;\n\tprof_tctx_t *tctx_p, *tctx_q;\n\tuint64_t curobjs_0, curobjs_1, curobjs_2, curobjs_3;\n\n\ttest_skip_if(!config_prof);\n\n\ttsdn = tsdn_fetch();\n\tflags = MALLOCX_TCACHE_NONE;\n\n\tprof_cnt_all(&curobjs_0, NULL, NULL, NULL);\n\tp = mallocx(1024, flags);\n\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\ttctx_p = prof_tctx_get(tsdn, p, NULL);\n\tassert_ptr_ne(tctx_p, (prof_tctx_t *)(uintptr_t)1U,\n\t    \"Expected valid tctx\");\n\tprof_cnt_all(&curobjs_1, NULL, NULL, NULL);\n\tassert_u64_eq(curobjs_0 + 1, curobjs_1,\n\t    \"Allocation should have increased sample size\");\n\n\tq = rallocx(p, 2048, flags);\n\tassert_ptr_ne(p, q, \"Expected move\");\n\tassert_ptr_not_null(p, \"Unexpected rmallocx() failure\");\n\ttctx_q = prof_tctx_get(tsdn, q, NULL);\n\tassert_ptr_ne(tctx_q, (prof_tctx_t *)(uintptr_t)1U,\n\t    \"Expected valid tctx\");\n\tprof_cnt_all(&curobjs_2, NULL, NULL, NULL);\n\tassert_u64_eq(curobjs_1, curobjs_2,\n\t    \"Reallocation should not have changed sample size\");\n\n\tdallocx(q, flags);\n\tprof_cnt_all(&curobjs_3, NULL, NULL, NULL);\n\tassert_u64_eq(curobjs_0, curobjs_3,\n\t    \"Sample size should have returned to base level\");\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test_no_reentrancy(\n\t    test_prof_realloc);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/prof_tctx.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_prof}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"prof:true,lg_prof_sample:0\"\nfi\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/prof_thread_name.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic void\nmallctl_thread_name_get_impl(const char *thread_name_expected, const char *func,\n    int line) {\n\tconst char *thread_name_old;\n\tsize_t sz;\n\n\tsz = sizeof(thread_name_old);\n\tassert_d_eq(mallctl(\"thread.prof.name\", (void *)&thread_name_old, &sz,\n\t    NULL, 0), 0,\n\t    \"%s():%d: Unexpected mallctl failure reading thread.prof.name\",\n\t    func, line);\n\tassert_str_eq(thread_name_old, thread_name_expected,\n\t    \"%s():%d: Unexpected thread.prof.name value\", func, line);\n}\n#define mallctl_thread_name_get(a)\t\t\t\t\t\\\n\tmallctl_thread_name_get_impl(a, __func__, __LINE__)\n\nstatic void\nmallctl_thread_name_set_impl(const char *thread_name, const char *func,\n    int line) {\n\tassert_d_eq(mallctl(\"thread.prof.name\", NULL, NULL,\n\t    (void *)&thread_name, sizeof(thread_name)), 0,\n\t    \"%s():%d: Unexpected mallctl failure reading thread.prof.name\",\n\t    func, line);\n\tmallctl_thread_name_get_impl(thread_name, func, line);\n}\n#define mallctl_thread_name_set(a)\t\t\t\t\t\\\n\tmallctl_thread_name_set_impl(a, __func__, __LINE__)\n\nTEST_BEGIN(test_prof_thread_name_validation) {\n\tconst char *thread_name;\n\n\ttest_skip_if(!config_prof);\n\n\tmallctl_thread_name_get(\"\");\n\tmallctl_thread_name_set(\"hi there\");\n\n\t/* NULL input shouldn't be allowed. */\n\tthread_name = NULL;\n\tassert_d_eq(mallctl(\"thread.prof.name\", NULL, NULL,\n\t    (void *)&thread_name, sizeof(thread_name)), EFAULT,\n\t    \"Unexpected mallctl result writing \\\"%s\\\" to thread.prof.name\",\n\t    thread_name);\n\n\t/* '\\n' shouldn't be allowed. */\n\tthread_name = \"hi\\nthere\";\n\tassert_d_eq(mallctl(\"thread.prof.name\", NULL, NULL,\n\t    (void *)&thread_name, sizeof(thread_name)), EFAULT,\n\t    \"Unexpected mallctl result writing \\\"%s\\\" to thread.prof.name\",\n\t    thread_name);\n\n\t/* Simultaneous read/write shouldn't be allowed. */\n\t{\n\t\tconst char *thread_name_old;\n\t\tsize_t sz;\n\n\t\tsz = sizeof(thread_name_old);\n\t\tassert_d_eq(mallctl(\"thread.prof.name\",\n\t\t    (void *)&thread_name_old, &sz, (void *)&thread_name,\n\t\t    sizeof(thread_name)), EPERM,\n\t\t    \"Unexpected mallctl result writing \\\"%s\\\" to \"\n\t\t    \"thread.prof.name\", thread_name);\n\t}\n\n\tmallctl_thread_name_set(\"\");\n}\nTEST_END\n\n#define NTHREADS\t4\n#define NRESET\t\t25\nstatic void *\nthd_start(void *varg) {\n\tunsigned thd_ind = *(unsigned *)varg;\n\tchar thread_name[16] = \"\";\n\tunsigned i;\n\n\tmalloc_snprintf(thread_name, sizeof(thread_name), \"thread %u\", thd_ind);\n\n\tmallctl_thread_name_get(\"\");\n\tmallctl_thread_name_set(thread_name);\n\n\tfor (i = 0; i < NRESET; i++) {\n\t\tassert_d_eq(mallctl(\"prof.reset\", NULL, NULL, NULL, 0), 0,\n\t\t    \"Unexpected error while resetting heap profile data\");\n\t\tmallctl_thread_name_get(thread_name);\n\t}\n\n\tmallctl_thread_name_set(thread_name);\n\tmallctl_thread_name_set(\"\");\n\n\treturn NULL;\n}\n\nTEST_BEGIN(test_prof_thread_name_threaded) {\n\tthd_t thds[NTHREADS];\n\tunsigned thd_args[NTHREADS];\n\tunsigned i;\n\n\ttest_skip_if(!config_prof);\n\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tthd_args[i] = i;\n\t\tthd_create(&thds[i], thd_start, (void *)&thd_args[i]);\n\t}\n\tfor (i = 0; i < NTHREADS; i++) {\n\t\tthd_join(thds[i], NULL);\n\t}\n}\nTEST_END\n#undef NTHREADS\n#undef NRESET\n\nint\nmain(void) {\n\treturn test(\n\t    test_prof_thread_name_validation,\n\t    test_prof_thread_name_threaded);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/prof_thread_name.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_prof}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"prof:true,prof_active:false\"\nfi\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/ql.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/ql.h\"\n\n/* Number of ring entries, in [2..26]. */\n#define NENTRIES 9\n\ntypedef struct list_s list_t;\ntypedef ql_head(list_t) list_head_t;\n\nstruct list_s {\n\tql_elm(list_t) link;\n\tchar id;\n};\n\nstatic void\ntest_empty_list(list_head_t *head) {\n\tlist_t *t;\n\tunsigned i;\n\n\tassert_ptr_null(ql_first(head), \"Unexpected element for empty list\");\n\tassert_ptr_null(ql_last(head, link),\n\t    \"Unexpected element for empty list\");\n\n\ti = 0;\n\tql_foreach(t, head, link) {\n\t\ti++;\n\t}\n\tassert_u_eq(i, 0, \"Unexpected element for empty list\");\n\n\ti = 0;\n\tql_reverse_foreach(t, head, link) {\n\t\ti++;\n\t}\n\tassert_u_eq(i, 0, \"Unexpected element for empty list\");\n}\n\nTEST_BEGIN(test_ql_empty) {\n\tlist_head_t head;\n\n\tql_new(&head);\n\ttest_empty_list(&head);\n}\nTEST_END\n\nstatic void\ninit_entries(list_t *entries, unsigned nentries) {\n\tunsigned i;\n\n\tfor (i = 0; i < nentries; i++) {\n\t\tentries[i].id = 'a' + i;\n\t\tql_elm_new(&entries[i], link);\n\t}\n}\n\nstatic void\ntest_entries_list(list_head_t *head, list_t *entries, unsigned nentries) {\n\tlist_t *t;\n\tunsigned i;\n\n\tassert_c_eq(ql_first(head)->id, entries[0].id, \"Element id mismatch\");\n\tassert_c_eq(ql_last(head, link)->id, entries[nentries-1].id,\n\t    \"Element id mismatch\");\n\n\ti = 0;\n\tql_foreach(t, head, link) {\n\t\tassert_c_eq(t->id, entries[i].id, \"Element id mismatch\");\n\t\ti++;\n\t}\n\n\ti = 0;\n\tql_reverse_foreach(t, head, link) {\n\t\tassert_c_eq(t->id, entries[nentries-i-1].id,\n\t\t    \"Element id mismatch\");\n\t\ti++;\n\t}\n\n\tfor (i = 0; i < nentries-1; i++) {\n\t\tt = ql_next(head, &entries[i], link);\n\t\tassert_c_eq(t->id, entries[i+1].id, \"Element id mismatch\");\n\t}\n\tassert_ptr_null(ql_next(head, &entries[nentries-1], link),\n\t    \"Unexpected element\");\n\n\tassert_ptr_null(ql_prev(head, &entries[0], link), \"Unexpected element\");\n\tfor (i = 1; i < nentries; i++) {\n\t\tt = ql_prev(head, &entries[i], link);\n\t\tassert_c_eq(t->id, entries[i-1].id, \"Element id mismatch\");\n\t}\n}\n\nTEST_BEGIN(test_ql_tail_insert) {\n\tlist_head_t head;\n\tlist_t entries[NENTRIES];\n\tunsigned i;\n\n\tql_new(&head);\n\tinit_entries(entries, sizeof(entries)/sizeof(list_t));\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tql_tail_insert(&head, &entries[i], link);\n\t}\n\n\ttest_entries_list(&head, entries, NENTRIES);\n}\nTEST_END\n\nTEST_BEGIN(test_ql_tail_remove) {\n\tlist_head_t head;\n\tlist_t entries[NENTRIES];\n\tunsigned i;\n\n\tql_new(&head);\n\tinit_entries(entries, sizeof(entries)/sizeof(list_t));\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tql_tail_insert(&head, &entries[i], link);\n\t}\n\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\ttest_entries_list(&head, entries, NENTRIES-i);\n\t\tql_tail_remove(&head, list_t, link);\n\t}\n\ttest_empty_list(&head);\n}\nTEST_END\n\nTEST_BEGIN(test_ql_head_insert) {\n\tlist_head_t head;\n\tlist_t entries[NENTRIES];\n\tunsigned i;\n\n\tql_new(&head);\n\tinit_entries(entries, sizeof(entries)/sizeof(list_t));\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tql_head_insert(&head, &entries[NENTRIES-i-1], link);\n\t}\n\n\ttest_entries_list(&head, entries, NENTRIES);\n}\nTEST_END\n\nTEST_BEGIN(test_ql_head_remove) {\n\tlist_head_t head;\n\tlist_t entries[NENTRIES];\n\tunsigned i;\n\n\tql_new(&head);\n\tinit_entries(entries, sizeof(entries)/sizeof(list_t));\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tql_head_insert(&head, &entries[NENTRIES-i-1], link);\n\t}\n\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\ttest_entries_list(&head, &entries[i], NENTRIES-i);\n\t\tql_head_remove(&head, list_t, link);\n\t}\n\ttest_empty_list(&head);\n}\nTEST_END\n\nTEST_BEGIN(test_ql_insert) {\n\tlist_head_t head;\n\tlist_t entries[8];\n\tlist_t *a, *b, *c, *d, *e, *f, *g, *h;\n\n\tql_new(&head);\n\tinit_entries(entries, sizeof(entries)/sizeof(list_t));\n\ta = &entries[0];\n\tb = &entries[1];\n\tc = &entries[2];\n\td = &entries[3];\n\te = &entries[4];\n\tf = &entries[5];\n\tg = &entries[6];\n\th = &entries[7];\n\n\t/*\n\t * ql_remove(), ql_before_insert(), and ql_after_insert() are used\n\t * internally by other macros that are already tested, so there's no\n\t * need to test them completely.  However, insertion/deletion from the\n\t * middle of lists is not otherwise tested; do so here.\n\t */\n\tql_tail_insert(&head, f, link);\n\tql_before_insert(&head, f, b, link);\n\tql_before_insert(&head, f, c, link);\n\tql_after_insert(f, h, link);\n\tql_after_insert(f, g, link);\n\tql_before_insert(&head, b, a, link);\n\tql_after_insert(c, d, link);\n\tql_before_insert(&head, f, e, link);\n\n\ttest_entries_list(&head, entries, sizeof(entries)/sizeof(list_t));\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_ql_empty,\n\t    test_ql_tail_insert,\n\t    test_ql_tail_remove,\n\t    test_ql_head_insert,\n\t    test_ql_head_remove,\n\t    test_ql_insert);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/qr.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/qr.h\"\n\n/* Number of ring entries, in [2..26]. */\n#define NENTRIES 9\n/* Split index, in [1..NENTRIES). */\n#define SPLIT_INDEX 5\n\ntypedef struct ring_s ring_t;\n\nstruct ring_s {\n\tqr(ring_t) link;\n\tchar id;\n};\n\nstatic void\ninit_entries(ring_t *entries) {\n\tunsigned i;\n\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tqr_new(&entries[i], link);\n\t\tentries[i].id = 'a' + i;\n\t}\n}\n\nstatic void\ntest_independent_entries(ring_t *entries) {\n\tring_t *t;\n\tunsigned i, j;\n\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tj = 0;\n\t\tqr_foreach(t, &entries[i], link) {\n\t\t\tj++;\n\t\t}\n\t\tassert_u_eq(j, 1,\n\t\t    \"Iteration over single-element ring should visit precisely \"\n\t\t    \"one element\");\n\t}\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tj = 0;\n\t\tqr_reverse_foreach(t, &entries[i], link) {\n\t\t\tj++;\n\t\t}\n\t\tassert_u_eq(j, 1,\n\t\t    \"Iteration over single-element ring should visit precisely \"\n\t\t    \"one element\");\n\t}\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tt = qr_next(&entries[i], link);\n\t\tassert_ptr_eq(t, &entries[i],\n\t\t    \"Next element in single-element ring should be same as \"\n\t\t    \"current element\");\n\t}\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tt = qr_prev(&entries[i], link);\n\t\tassert_ptr_eq(t, &entries[i],\n\t\t    \"Previous element in single-element ring should be same as \"\n\t\t    \"current element\");\n\t}\n}\n\nTEST_BEGIN(test_qr_one) {\n\tring_t entries[NENTRIES];\n\n\tinit_entries(entries);\n\ttest_independent_entries(entries);\n}\nTEST_END\n\nstatic void\ntest_entries_ring(ring_t *entries) {\n\tring_t *t;\n\tunsigned i, j;\n\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tj = 0;\n\t\tqr_foreach(t, &entries[i], link) {\n\t\t\tassert_c_eq(t->id, entries[(i+j) % NENTRIES].id,\n\t\t\t    \"Element id mismatch\");\n\t\t\tj++;\n\t\t}\n\t}\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tj = 0;\n\t\tqr_reverse_foreach(t, &entries[i], link) {\n\t\t\tassert_c_eq(t->id, entries[(NENTRIES+i-j-1) %\n\t\t\t    NENTRIES].id, \"Element id mismatch\");\n\t\t\tj++;\n\t\t}\n\t}\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tt = qr_next(&entries[i], link);\n\t\tassert_c_eq(t->id, entries[(i+1) % NENTRIES].id,\n\t\t    \"Element id mismatch\");\n\t}\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tt = qr_prev(&entries[i], link);\n\t\tassert_c_eq(t->id, entries[(NENTRIES+i-1) % NENTRIES].id,\n\t\t    \"Element id mismatch\");\n\t}\n}\n\nTEST_BEGIN(test_qr_after_insert) {\n\tring_t entries[NENTRIES];\n\tunsigned i;\n\n\tinit_entries(entries);\n\tfor (i = 1; i < NENTRIES; i++) {\n\t\tqr_after_insert(&entries[i - 1], &entries[i], link);\n\t}\n\ttest_entries_ring(entries);\n}\nTEST_END\n\nTEST_BEGIN(test_qr_remove) {\n\tring_t entries[NENTRIES];\n\tring_t *t;\n\tunsigned i, j;\n\n\tinit_entries(entries);\n\tfor (i = 1; i < NENTRIES; i++) {\n\t\tqr_after_insert(&entries[i - 1], &entries[i], link);\n\t}\n\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tj = 0;\n\t\tqr_foreach(t, &entries[i], link) {\n\t\t\tassert_c_eq(t->id, entries[i+j].id,\n\t\t\t    \"Element id mismatch\");\n\t\t\tj++;\n\t\t}\n\t\tj = 0;\n\t\tqr_reverse_foreach(t, &entries[i], link) {\n\t\t\tassert_c_eq(t->id, entries[NENTRIES - 1 - j].id,\n\t\t\t\"Element id mismatch\");\n\t\t\tj++;\n\t\t}\n\t\tqr_remove(&entries[i], link);\n\t}\n\ttest_independent_entries(entries);\n}\nTEST_END\n\nTEST_BEGIN(test_qr_before_insert) {\n\tring_t entries[NENTRIES];\n\tring_t *t;\n\tunsigned i, j;\n\n\tinit_entries(entries);\n\tfor (i = 1; i < NENTRIES; i++) {\n\t\tqr_before_insert(&entries[i - 1], &entries[i], link);\n\t}\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tj = 0;\n\t\tqr_foreach(t, &entries[i], link) {\n\t\t\tassert_c_eq(t->id, entries[(NENTRIES+i-j) %\n\t\t\t    NENTRIES].id, \"Element id mismatch\");\n\t\t\tj++;\n\t\t}\n\t}\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tj = 0;\n\t\tqr_reverse_foreach(t, &entries[i], link) {\n\t\t\tassert_c_eq(t->id, entries[(i+j+1) % NENTRIES].id,\n\t\t\t    \"Element id mismatch\");\n\t\t\tj++;\n\t\t}\n\t}\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tt = qr_next(&entries[i], link);\n\t\tassert_c_eq(t->id, entries[(NENTRIES+i-1) % NENTRIES].id,\n\t\t    \"Element id mismatch\");\n\t}\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tt = qr_prev(&entries[i], link);\n\t\tassert_c_eq(t->id, entries[(i+1) % NENTRIES].id,\n\t\t    \"Element id mismatch\");\n\t}\n}\nTEST_END\n\nstatic void\ntest_split_entries(ring_t *entries) {\n\tring_t *t;\n\tunsigned i, j;\n\n\tfor (i = 0; i < NENTRIES; i++) {\n\t\tj = 0;\n\t\tqr_foreach(t, &entries[i], link) {\n\t\t\tif (i < SPLIT_INDEX) {\n\t\t\t\tassert_c_eq(t->id,\n\t\t\t\t    entries[(i+j) % SPLIT_INDEX].id,\n\t\t\t\t    \"Element id mismatch\");\n\t\t\t} else {\n\t\t\t\tassert_c_eq(t->id, entries[(i+j-SPLIT_INDEX) %\n\t\t\t\t    (NENTRIES-SPLIT_INDEX) + SPLIT_INDEX].id,\n\t\t\t\t    \"Element id mismatch\");\n\t\t\t}\n\t\t\tj++;\n\t\t}\n\t}\n}\n\nTEST_BEGIN(test_qr_meld_split) {\n\tring_t entries[NENTRIES];\n\tunsigned i;\n\n\tinit_entries(entries);\n\tfor (i = 1; i < NENTRIES; i++) {\n\t\tqr_after_insert(&entries[i - 1], &entries[i], link);\n\t}\n\n\tqr_split(&entries[0], &entries[SPLIT_INDEX], ring_t, link);\n\ttest_split_entries(entries);\n\n\tqr_meld(&entries[0], &entries[SPLIT_INDEX], ring_t, link);\n\ttest_entries_ring(entries);\n\n\tqr_meld(&entries[0], &entries[SPLIT_INDEX], ring_t, link);\n\ttest_split_entries(entries);\n\n\tqr_split(&entries[0], &entries[SPLIT_INDEX], ring_t, link);\n\ttest_entries_ring(entries);\n\n\tqr_split(&entries[0], &entries[0], ring_t, link);\n\ttest_entries_ring(entries);\n\n\tqr_meld(&entries[0], &entries[0], ring_t, link);\n\ttest_entries_ring(entries);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_qr_one,\n\t    test_qr_after_insert,\n\t    test_qr_remove,\n\t    test_qr_before_insert,\n\t    test_qr_meld_split);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/rb.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/rb.h\"\n\n#define rbtn_black_height(a_type, a_field, a_rbt, r_height) do {\t\\\n\ta_type *rbp_bh_t;\t\t\t\t\t\t\\\n\tfor (rbp_bh_t = (a_rbt)->rbt_root, (r_height) = 0; rbp_bh_t !=\t\\\n\t    NULL; rbp_bh_t = rbtn_left_get(a_type, a_field,\t\t\\\n\t    rbp_bh_t)) {\t\t\t\t\t\t\\\n\t\tif (!rbtn_red_get(a_type, a_field, rbp_bh_t)) {\t\t\\\n\t\t(r_height)++;\t\t\t\t\t\t\\\n\t\t}\t\t\t\t\t\t\t\\\n\t}\t\t\t\t\t\t\t\t\\\n} while (0)\n\ntypedef struct node_s node_t;\n\nstruct node_s {\n#define NODE_MAGIC 0x9823af7e\n\tuint32_t magic;\n\trb_node(node_t) link;\n\tuint64_t key;\n};\n\nstatic int\nnode_cmp(const node_t *a, const node_t *b) {\n\tint ret;\n\n\tassert_u32_eq(a->magic, NODE_MAGIC, \"Bad magic\");\n\tassert_u32_eq(b->magic, NODE_MAGIC, \"Bad magic\");\n\n\tret = (a->key > b->key) - (a->key < b->key);\n\tif (ret == 0) {\n\t\t/*\n\t\t * Duplicates are not allowed in the tree, so force an\n\t\t * arbitrary ordering for non-identical items with equal keys.\n\t\t */\n\t\tret = (((uintptr_t)a) > ((uintptr_t)b))\n\t\t    - (((uintptr_t)a) < ((uintptr_t)b));\n\t}\n\treturn ret;\n}\n\ntypedef rb_tree(node_t) tree_t;\nrb_gen(static, tree_, tree_t, node_t, link, node_cmp);\n\nTEST_BEGIN(test_rb_empty) {\n\ttree_t tree;\n\tnode_t key;\n\n\ttree_new(&tree);\n\n\tassert_true(tree_empty(&tree), \"Tree should be empty\");\n\tassert_ptr_null(tree_first(&tree), \"Unexpected node\");\n\tassert_ptr_null(tree_last(&tree), \"Unexpected node\");\n\n\tkey.key = 0;\n\tkey.magic = NODE_MAGIC;\n\tassert_ptr_null(tree_search(&tree, &key), \"Unexpected node\");\n\n\tkey.key = 0;\n\tkey.magic = NODE_MAGIC;\n\tassert_ptr_null(tree_nsearch(&tree, &key), \"Unexpected node\");\n\n\tkey.key = 0;\n\tkey.magic = NODE_MAGIC;\n\tassert_ptr_null(tree_psearch(&tree, &key), \"Unexpected node\");\n}\nTEST_END\n\nstatic unsigned\ntree_recurse(node_t *node, unsigned black_height, unsigned black_depth) {\n\tunsigned ret = 0;\n\tnode_t *left_node;\n\tnode_t *right_node;\n\n\tif (node == NULL) {\n\t\treturn ret;\n\t}\n\n\tleft_node = rbtn_left_get(node_t, link, node);\n\tright_node = rbtn_right_get(node_t, link, node);\n\n\tif (!rbtn_red_get(node_t, link, node)) {\n\t\tblack_depth++;\n\t}\n\n\t/* Red nodes must be interleaved with black nodes. */\n\tif (rbtn_red_get(node_t, link, node)) {\n\t\tif (left_node != NULL) {\n\t\t\tassert_false(rbtn_red_get(node_t, link, left_node),\n\t\t\t\t\"Node should be black\");\n\t\t}\n\t\tif (right_node != NULL) {\n\t\t\tassert_false(rbtn_red_get(node_t, link, right_node),\n\t\t\t    \"Node should be black\");\n\t\t}\n\t}\n\n\t/* Self. */\n\tassert_u32_eq(node->magic, NODE_MAGIC, \"Bad magic\");\n\n\t/* Left subtree. */\n\tif (left_node != NULL) {\n\t\tret += tree_recurse(left_node, black_height, black_depth);\n\t} else {\n\t\tret += (black_depth != black_height);\n\t}\n\n\t/* Right subtree. */\n\tif (right_node != NULL) {\n\t\tret += tree_recurse(right_node, black_height, black_depth);\n\t} else {\n\t\tret += (black_depth != black_height);\n\t}\n\n\treturn ret;\n}\n\nstatic node_t *\ntree_iterate_cb(tree_t *tree, node_t *node, void *data) {\n\tunsigned *i = (unsigned *)data;\n\tnode_t *search_node;\n\n\tassert_u32_eq(node->magic, NODE_MAGIC, \"Bad magic\");\n\n\t/* Test rb_search(). */\n\tsearch_node = tree_search(tree, node);\n\tassert_ptr_eq(search_node, node,\n\t    \"tree_search() returned unexpected node\");\n\n\t/* Test rb_nsearch(). */\n\tsearch_node = tree_nsearch(tree, node);\n\tassert_ptr_eq(search_node, node,\n\t    \"tree_nsearch() returned unexpected node\");\n\n\t/* Test rb_psearch(). */\n\tsearch_node = tree_psearch(tree, node);\n\tassert_ptr_eq(search_node, node,\n\t    \"tree_psearch() returned unexpected node\");\n\n\t(*i)++;\n\n\treturn NULL;\n}\n\nstatic unsigned\ntree_iterate(tree_t *tree) {\n\tunsigned i;\n\n\ti = 0;\n\ttree_iter(tree, NULL, tree_iterate_cb, (void *)&i);\n\n\treturn i;\n}\n\nstatic unsigned\ntree_iterate_reverse(tree_t *tree) {\n\tunsigned i;\n\n\ti = 0;\n\ttree_reverse_iter(tree, NULL, tree_iterate_cb, (void *)&i);\n\n\treturn i;\n}\n\nstatic void\nnode_remove(tree_t *tree, node_t *node, unsigned nnodes) {\n\tnode_t *search_node;\n\tunsigned black_height, imbalances;\n\n\ttree_remove(tree, node);\n\n\t/* Test rb_nsearch(). */\n\tsearch_node = tree_nsearch(tree, node);\n\tif (search_node != NULL) {\n\t\tassert_u64_ge(search_node->key, node->key,\n\t\t    \"Key ordering error\");\n\t}\n\n\t/* Test rb_psearch(). */\n\tsearch_node = tree_psearch(tree, node);\n\tif (search_node != NULL) {\n\t\tassert_u64_le(search_node->key, node->key,\n\t\t    \"Key ordering error\");\n\t}\n\n\tnode->magic = 0;\n\n\trbtn_black_height(node_t, link, tree, black_height);\n\timbalances = tree_recurse(tree->rbt_root, black_height, 0);\n\tassert_u_eq(imbalances, 0, \"Tree is unbalanced\");\n\tassert_u_eq(tree_iterate(tree), nnodes-1,\n\t    \"Unexpected node iteration count\");\n\tassert_u_eq(tree_iterate_reverse(tree), nnodes-1,\n\t    \"Unexpected node iteration count\");\n}\n\nstatic node_t *\nremove_iterate_cb(tree_t *tree, node_t *node, void *data) {\n\tunsigned *nnodes = (unsigned *)data;\n\tnode_t *ret = tree_next(tree, node);\n\n\tnode_remove(tree, node, *nnodes);\n\n\treturn ret;\n}\n\nstatic node_t *\nremove_reverse_iterate_cb(tree_t *tree, node_t *node, void *data) {\n\tunsigned *nnodes = (unsigned *)data;\n\tnode_t *ret = tree_prev(tree, node);\n\n\tnode_remove(tree, node, *nnodes);\n\n\treturn ret;\n}\n\nstatic void\ndestroy_cb(node_t *node, void *data) {\n\tunsigned *nnodes = (unsigned *)data;\n\n\tassert_u_gt(*nnodes, 0, \"Destruction removed too many nodes\");\n\t(*nnodes)--;\n}\n\nTEST_BEGIN(test_rb_random) {\n#define NNODES 25\n#define NBAGS 250\n#define SEED 42\n\tsfmt_t *sfmt;\n\tuint64_t bag[NNODES];\n\ttree_t tree;\n\tnode_t nodes[NNODES];\n\tunsigned i, j, k, black_height, imbalances;\n\n\tsfmt = init_gen_rand(SEED);\n\tfor (i = 0; i < NBAGS; i++) {\n\t\tswitch (i) {\n\t\tcase 0:\n\t\t\t/* Insert in order. */\n\t\t\tfor (j = 0; j < NNODES; j++) {\n\t\t\t\tbag[j] = j;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase 1:\n\t\t\t/* Insert in reverse order. */\n\t\t\tfor (j = 0; j < NNODES; j++) {\n\t\t\t\tbag[j] = NNODES - j - 1;\n\t\t\t}\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tfor (j = 0; j < NNODES; j++) {\n\t\t\t\tbag[j] = gen_rand64_range(sfmt, NNODES);\n\t\t\t}\n\t\t}\n\n\t\tfor (j = 1; j <= NNODES; j++) {\n\t\t\t/* Initialize tree and nodes. */\n\t\t\ttree_new(&tree);\n\t\t\tfor (k = 0; k < j; k++) {\n\t\t\t\tnodes[k].magic = NODE_MAGIC;\n\t\t\t\tnodes[k].key = bag[k];\n\t\t\t}\n\n\t\t\t/* Insert nodes. */\n\t\t\tfor (k = 0; k < j; k++) {\n\t\t\t\ttree_insert(&tree, &nodes[k]);\n\n\t\t\t\trbtn_black_height(node_t, link, &tree,\n\t\t\t\t    black_height);\n\t\t\t\timbalances = tree_recurse(tree.rbt_root,\n\t\t\t\t    black_height, 0);\n\t\t\t\tassert_u_eq(imbalances, 0,\n\t\t\t\t    \"Tree is unbalanced\");\n\n\t\t\t\tassert_u_eq(tree_iterate(&tree), k+1,\n\t\t\t\t    \"Unexpected node iteration count\");\n\t\t\t\tassert_u_eq(tree_iterate_reverse(&tree), k+1,\n\t\t\t\t    \"Unexpected node iteration count\");\n\n\t\t\t\tassert_false(tree_empty(&tree),\n\t\t\t\t    \"Tree should not be empty\");\n\t\t\t\tassert_ptr_not_null(tree_first(&tree),\n\t\t\t\t    \"Tree should not be empty\");\n\t\t\t\tassert_ptr_not_null(tree_last(&tree),\n\t\t\t\t    \"Tree should not be empty\");\n\n\t\t\t\ttree_next(&tree, &nodes[k]);\n\t\t\t\ttree_prev(&tree, &nodes[k]);\n\t\t\t}\n\n\t\t\t/* Remove nodes. */\n\t\t\tswitch (i % 5) {\n\t\t\tcase 0:\n\t\t\t\tfor (k = 0; k < j; k++) {\n\t\t\t\t\tnode_remove(&tree, &nodes[k], j - k);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase 1:\n\t\t\t\tfor (k = j; k > 0; k--) {\n\t\t\t\t\tnode_remove(&tree, &nodes[k-1], k);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase 2: {\n\t\t\t\tnode_t *start;\n\t\t\t\tunsigned nnodes = j;\n\n\t\t\t\tstart = NULL;\n\t\t\t\tdo {\n\t\t\t\t\tstart = tree_iter(&tree, start,\n\t\t\t\t\t    remove_iterate_cb, (void *)&nnodes);\n\t\t\t\t\tnnodes--;\n\t\t\t\t} while (start != NULL);\n\t\t\t\tassert_u_eq(nnodes, 0,\n\t\t\t\t    \"Removal terminated early\");\n\t\t\t\tbreak;\n\t\t\t} case 3: {\n\t\t\t\tnode_t *start;\n\t\t\t\tunsigned nnodes = j;\n\n\t\t\t\tstart = NULL;\n\t\t\t\tdo {\n\t\t\t\t\tstart = tree_reverse_iter(&tree, start,\n\t\t\t\t\t    remove_reverse_iterate_cb,\n\t\t\t\t\t    (void *)&nnodes);\n\t\t\t\t\tnnodes--;\n\t\t\t\t} while (start != NULL);\n\t\t\t\tassert_u_eq(nnodes, 0,\n\t\t\t\t    \"Removal terminated early\");\n\t\t\t\tbreak;\n\t\t\t} case 4: {\n\t\t\t\tunsigned nnodes = j;\n\t\t\t\ttree_destroy(&tree, destroy_cb, &nnodes);\n\t\t\t\tassert_u_eq(nnodes, 0,\n\t\t\t\t    \"Destruction terminated early\");\n\t\t\t\tbreak;\n\t\t\t} default:\n\t\t\t\tnot_reached();\n\t\t\t}\n\t\t}\n\t}\n\tfini_gen_rand(sfmt);\n#undef NNODES\n#undef NBAGS\n#undef SEED\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_rb_empty,\n\t    test_rb_random);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/retained.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/spin.h\"\n\nstatic unsigned\t\tarena_ind;\nstatic size_t\t\tsz;\nstatic size_t\t\tesz;\n#define NEPOCHS\t\t8\n#define PER_THD_NALLOCS\t1\nstatic atomic_u_t\tepoch;\nstatic atomic_u_t\tnfinished;\n\nstatic unsigned\ndo_arena_create(extent_hooks_t *h) {\n\tunsigned arena_ind;\n\tsize_t sz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena_ind, &sz,\n\t    (void *)(h != NULL ? &h : NULL), (h != NULL ? sizeof(h) : 0)), 0,\n\t    \"Unexpected mallctl() failure\");\n\treturn arena_ind;\n}\n\nstatic void\ndo_arena_destroy(unsigned arena_ind) {\n\tsize_t mib[3];\n\tsize_t miblen;\n\n\tmiblen = sizeof(mib)/sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"arena.0.destroy\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() failure\");\n\tmib[1] = (size_t)arena_ind;\n\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctlbymib() failure\");\n}\n\nstatic void\ndo_refresh(void) {\n\tuint64_t epoch = 1;\n\tassert_d_eq(mallctl(\"epoch\", NULL, NULL, (void *)&epoch,\n\t    sizeof(epoch)), 0, \"Unexpected mallctl() failure\");\n}\n\nstatic size_t\ndo_get_size_impl(const char *cmd, unsigned arena_ind) {\n\tsize_t mib[4];\n\tsize_t miblen = sizeof(mib) / sizeof(size_t);\n\tsize_t z = sizeof(size_t);\n\n\tassert_d_eq(mallctlnametomib(cmd, mib, &miblen),\n\t    0, \"Unexpected mallctlnametomib(\\\"%s\\\", ...) failure\", cmd);\n\tmib[2] = arena_ind;\n\tsize_t size;\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&size, &z, NULL, 0),\n\t    0, \"Unexpected mallctlbymib([\\\"%s\\\"], ...) failure\", cmd);\n\n\treturn size;\n}\n\nstatic size_t\ndo_get_active(unsigned arena_ind) {\n\treturn do_get_size_impl(\"stats.arenas.0.pactive\", arena_ind) * PAGE;\n}\n\nstatic size_t\ndo_get_mapped(unsigned arena_ind) {\n\treturn do_get_size_impl(\"stats.arenas.0.mapped\", arena_ind);\n}\n\nstatic void *\nthd_start(void *arg) {\n\tfor (unsigned next_epoch = 1; next_epoch < NEPOCHS; next_epoch++) {\n\t\t/* Busy-wait for next epoch. */\n\t\tunsigned cur_epoch;\n\t\tspin_t spinner = SPIN_INITIALIZER;\n\t\twhile ((cur_epoch = atomic_load_u(&epoch, ATOMIC_ACQUIRE)) !=\n\t\t    next_epoch) {\n\t\t\tspin_adaptive(&spinner);\n\t\t}\n\t\tassert_u_eq(cur_epoch, next_epoch, \"Unexpected epoch\");\n\n\t\t/*\n\t\t * Allocate.  The main thread will reset the arena, so there's\n\t\t * no need to deallocate.\n\t\t */\n\t\tfor (unsigned i = 0; i < PER_THD_NALLOCS; i++) {\n\t\t\tvoid *p = mallocx(sz, MALLOCX_ARENA(arena_ind) |\n\t\t\t    MALLOCX_TCACHE_NONE\n\t\t\t    );\n\t\t\tassert_ptr_not_null(p,\n\t\t\t    \"Unexpected mallocx() failure\\n\");\n\t\t}\n\n\t\t/* Let the main thread know we've finished this iteration. */\n\t\tatomic_fetch_add_u(&nfinished, 1, ATOMIC_RELEASE);\n\t}\n\n\treturn NULL;\n}\n\nTEST_BEGIN(test_retained) {\n\ttest_skip_if(!config_stats);\n\n\tarena_ind = do_arena_create(NULL);\n\tsz = nallocx(HUGEPAGE, 0);\n\tesz = sz + sz_large_pad;\n\n\tatomic_store_u(&epoch, 0, ATOMIC_RELAXED);\n\n\tunsigned nthreads = ncpus * 2;\n\tVARIABLE_ARRAY(thd_t, threads, nthreads);\n\tfor (unsigned i = 0; i < nthreads; i++) {\n\t\tthd_create(&threads[i], thd_start, NULL);\n\t}\n\n\tfor (unsigned e = 1; e < NEPOCHS; e++) {\n\t\tatomic_store_u(&nfinished, 0, ATOMIC_RELEASE);\n\t\tatomic_store_u(&epoch, e, ATOMIC_RELEASE);\n\n\t\t/* Wait for threads to finish allocating. */\n\t\tspin_t spinner = SPIN_INITIALIZER;\n\t\twhile (atomic_load_u(&nfinished, ATOMIC_ACQUIRE) < nthreads) {\n\t\t\tspin_adaptive(&spinner);\n\t\t}\n\n\t\t/*\n\t\t * Assert that retained is no more than the sum of size classes\n\t\t * that should have been used to satisfy the worker threads'\n\t\t * requests, discounting per growth fragmentation.\n\t\t */\n\t\tdo_refresh();\n\n\t\tsize_t allocated = esz * nthreads * PER_THD_NALLOCS;\n\t\tsize_t active = do_get_active(arena_ind);\n\t\tassert_zu_le(allocated, active, \"Unexpected active memory\");\n\t\tsize_t mapped = do_get_mapped(arena_ind);\n\t\tassert_zu_le(active, mapped, \"Unexpected mapped memory\");\n\n\t\tarena_t *arena = arena_get(tsdn_fetch(), arena_ind, false);\n\t\tsize_t usable = 0;\n\t\tsize_t fragmented = 0;\n\t\tfor (pszind_t pind = sz_psz2ind(HUGEPAGE); pind <\n\t\t    arena->extent_grow_next; pind++) {\n\t\t\tsize_t psz = sz_pind2sz(pind);\n\t\t\tsize_t psz_fragmented = psz % esz;\n\t\t\tsize_t psz_usable = psz - psz_fragmented;\n\t\t\t/*\n\t\t\t * Only consider size classes that wouldn't be skipped.\n\t\t\t */\n\t\t\tif (psz_usable > 0) {\n\t\t\t\tassert_zu_lt(usable, allocated,\n\t\t\t\t    \"Excessive retained memory \"\n\t\t\t\t    \"(%#zx[+%#zx] > %#zx)\", usable, psz_usable,\n\t\t\t\t    allocated);\n\t\t\t\tfragmented += psz_fragmented;\n\t\t\t\tusable += psz_usable;\n\t\t\t}\n\t\t}\n\n\t\t/*\n\t\t * Clean up arena.  Destroying and recreating the arena\n\t\t * is simpler that specifying extent hooks that deallocate\n\t\t * (rather than retaining) during reset.\n\t\t */\n\t\tdo_arena_destroy(arena_ind);\n\t\tassert_u_eq(do_arena_create(NULL), arena_ind,\n\t\t    \"Unexpected arena index\");\n\t}\n\n\tfor (unsigned i = 0; i < nthreads; i++) {\n\t\tthd_join(threads[i], NULL);\n\t}\n\n\tdo_arena_destroy(arena_ind);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_retained);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/rtree.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/rtree.h\"\n\nrtree_node_alloc_t *rtree_node_alloc_orig;\nrtree_node_dalloc_t *rtree_node_dalloc_orig;\nrtree_leaf_alloc_t *rtree_leaf_alloc_orig;\nrtree_leaf_dalloc_t *rtree_leaf_dalloc_orig;\n\n/* Potentially too large to safely place on the stack. */\nrtree_t test_rtree;\n\nstatic rtree_node_elm_t *\nrtree_node_alloc_intercept(tsdn_t *tsdn, rtree_t *rtree, size_t nelms) {\n\trtree_node_elm_t *node;\n\n\tif (rtree != &test_rtree) {\n\t\treturn rtree_node_alloc_orig(tsdn, rtree, nelms);\n\t}\n\n\tmalloc_mutex_unlock(tsdn, &rtree->init_lock);\n\tnode = (rtree_node_elm_t *)calloc(nelms, sizeof(rtree_node_elm_t));\n\tassert_ptr_not_null(node, \"Unexpected calloc() failure\");\n\tmalloc_mutex_lock(tsdn, &rtree->init_lock);\n\n\treturn node;\n}\n\nstatic void\nrtree_node_dalloc_intercept(tsdn_t *tsdn, rtree_t *rtree,\n    rtree_node_elm_t *node) {\n\tif (rtree != &test_rtree) {\n\t\trtree_node_dalloc_orig(tsdn, rtree, node);\n\t\treturn;\n\t}\n\n\tfree(node);\n}\n\nstatic rtree_leaf_elm_t *\nrtree_leaf_alloc_intercept(tsdn_t *tsdn, rtree_t *rtree, size_t nelms) {\n\trtree_leaf_elm_t *leaf;\n\n\tif (rtree != &test_rtree) {\n\t\treturn rtree_leaf_alloc_orig(tsdn, rtree, nelms);\n\t}\n\n\tmalloc_mutex_unlock(tsdn, &rtree->init_lock);\n\tleaf = (rtree_leaf_elm_t *)calloc(nelms, sizeof(rtree_leaf_elm_t));\n\tassert_ptr_not_null(leaf, \"Unexpected calloc() failure\");\n\tmalloc_mutex_lock(tsdn, &rtree->init_lock);\n\n\treturn leaf;\n}\n\nstatic void\nrtree_leaf_dalloc_intercept(tsdn_t *tsdn, rtree_t *rtree,\n    rtree_leaf_elm_t *leaf) {\n\tif (rtree != &test_rtree) {\n\t\trtree_leaf_dalloc_orig(tsdn, rtree, leaf);\n\t\treturn;\n\t}\n\n\tfree(leaf);\n}\n\nTEST_BEGIN(test_rtree_read_empty) {\n\ttsdn_t *tsdn;\n\n\ttsdn = tsdn_fetch();\n\n\trtree_t *rtree = &test_rtree;\n\trtree_ctx_t rtree_ctx;\n\trtree_ctx_data_init(&rtree_ctx);\n\tassert_false(rtree_new(rtree, false), \"Unexpected rtree_new() failure\");\n\tassert_ptr_null(rtree_extent_read(tsdn, rtree, &rtree_ctx, PAGE,\n\t    false), \"rtree_extent_read() should return NULL for empty tree\");\n\trtree_delete(tsdn, rtree);\n}\nTEST_END\n\n#undef NTHREADS\n#undef NITERS\n#undef SEED\n\nTEST_BEGIN(test_rtree_extrema) {\n\textent_t extent_a, extent_b;\n\textent_init(&extent_a, NULL, NULL, LARGE_MINCLASS, false,\n\t    sz_size2index(LARGE_MINCLASS), 0, extent_state_active, false,\n\t    false);\n\textent_init(&extent_b, NULL, NULL, 0, false, NSIZES, 0,\n\t    extent_state_active, false, false);\n\n\ttsdn_t *tsdn = tsdn_fetch();\n\n\trtree_t *rtree = &test_rtree;\n\trtree_ctx_t rtree_ctx;\n\trtree_ctx_data_init(&rtree_ctx);\n\tassert_false(rtree_new(rtree, false), \"Unexpected rtree_new() failure\");\n\n\tassert_false(rtree_write(tsdn, rtree, &rtree_ctx, PAGE, &extent_a,\n\t    extent_szind_get(&extent_a), extent_slab_get(&extent_a)),\n\t    \"Unexpected rtree_write() failure\");\n\trtree_szind_slab_update(tsdn, rtree, &rtree_ctx, PAGE,\n\t    extent_szind_get(&extent_a), extent_slab_get(&extent_a));\n\tassert_ptr_eq(rtree_extent_read(tsdn, rtree, &rtree_ctx, PAGE, true),\n\t    &extent_a,\n\t    \"rtree_extent_read() should return previously set value\");\n\n\tassert_false(rtree_write(tsdn, rtree, &rtree_ctx, ~((uintptr_t)0),\n\t    &extent_b, extent_szind_get_maybe_invalid(&extent_b),\n\t    extent_slab_get(&extent_b)), \"Unexpected rtree_write() failure\");\n\tassert_ptr_eq(rtree_extent_read(tsdn, rtree, &rtree_ctx,\n\t    ~((uintptr_t)0), true), &extent_b,\n\t    \"rtree_extent_read() should return previously set value\");\n\n\trtree_delete(tsdn, rtree);\n}\nTEST_END\n\nTEST_BEGIN(test_rtree_bits) {\n\ttsdn_t *tsdn = tsdn_fetch();\n\n\tuintptr_t keys[] = {PAGE, PAGE + 1,\n\t    PAGE + (((uintptr_t)1) << LG_PAGE) - 1};\n\n\textent_t extent;\n\textent_init(&extent, NULL, NULL, 0, false, NSIZES, 0,\n\t    extent_state_active, false, false);\n\n\trtree_t *rtree = &test_rtree;\n\trtree_ctx_t rtree_ctx;\n\trtree_ctx_data_init(&rtree_ctx);\n\tassert_false(rtree_new(rtree, false), \"Unexpected rtree_new() failure\");\n\n\tfor (unsigned i = 0; i < sizeof(keys)/sizeof(uintptr_t); i++) {\n\t\tassert_false(rtree_write(tsdn, rtree, &rtree_ctx, keys[i],\n\t\t    &extent, NSIZES, false),\n\t\t    \"Unexpected rtree_write() failure\");\n\t\tfor (unsigned j = 0; j < sizeof(keys)/sizeof(uintptr_t); j++) {\n\t\t\tassert_ptr_eq(rtree_extent_read(tsdn, rtree, &rtree_ctx,\n\t\t\t    keys[j], true), &extent,\n\t\t\t    \"rtree_extent_read() should return previously set \"\n\t\t\t    \"value and ignore insignificant key bits; i=%u, \"\n\t\t\t    \"j=%u, set key=%#\"FMTxPTR\", get key=%#\"FMTxPTR, i,\n\t\t\t    j, keys[i], keys[j]);\n\t\t}\n\t\tassert_ptr_null(rtree_extent_read(tsdn, rtree, &rtree_ctx,\n\t\t    (((uintptr_t)2) << LG_PAGE), false),\n\t\t    \"Only leftmost rtree leaf should be set; i=%u\", i);\n\t\trtree_clear(tsdn, rtree, &rtree_ctx, keys[i]);\n\t}\n\n\trtree_delete(tsdn, rtree);\n}\nTEST_END\n\nTEST_BEGIN(test_rtree_random) {\n#define NSET 16\n#define SEED 42\n\tsfmt_t *sfmt = init_gen_rand(SEED);\n\ttsdn_t *tsdn = tsdn_fetch();\n\tuintptr_t keys[NSET];\n\trtree_t *rtree = &test_rtree;\n\trtree_ctx_t rtree_ctx;\n\trtree_ctx_data_init(&rtree_ctx);\n\n\textent_t extent;\n\textent_init(&extent, NULL, NULL, 0, false, NSIZES, 0,\n\t    extent_state_active, false, false);\n\n\tassert_false(rtree_new(rtree, false), \"Unexpected rtree_new() failure\");\n\n\tfor (unsigned i = 0; i < NSET; i++) {\n\t\tkeys[i] = (uintptr_t)gen_rand64(sfmt);\n\t\trtree_leaf_elm_t *elm = rtree_leaf_elm_lookup(tsdn, rtree,\n\t\t    &rtree_ctx, keys[i], false, true);\n\t\tassert_ptr_not_null(elm,\n\t\t    \"Unexpected rtree_leaf_elm_lookup() failure\");\n\t\trtree_leaf_elm_write(tsdn, rtree, elm, &extent, NSIZES, false);\n\t\tassert_ptr_eq(rtree_extent_read(tsdn, rtree, &rtree_ctx,\n\t\t    keys[i], true), &extent,\n\t\t    \"rtree_extent_read() should return previously set value\");\n\t}\n\tfor (unsigned i = 0; i < NSET; i++) {\n\t\tassert_ptr_eq(rtree_extent_read(tsdn, rtree, &rtree_ctx,\n\t\t    keys[i], true), &extent,\n\t\t    \"rtree_extent_read() should return previously set value, \"\n\t\t    \"i=%u\", i);\n\t}\n\n\tfor (unsigned i = 0; i < NSET; i++) {\n\t\trtree_clear(tsdn, rtree, &rtree_ctx, keys[i]);\n\t\tassert_ptr_null(rtree_extent_read(tsdn, rtree, &rtree_ctx,\n\t\t    keys[i], true),\n\t\t   \"rtree_extent_read() should return previously set value\");\n\t}\n\tfor (unsigned i = 0; i < NSET; i++) {\n\t\tassert_ptr_null(rtree_extent_read(tsdn, rtree, &rtree_ctx,\n\t\t    keys[i], true),\n\t\t    \"rtree_extent_read() should return previously set value\");\n\t}\n\n\trtree_delete(tsdn, rtree);\n\tfini_gen_rand(sfmt);\n#undef NSET\n#undef SEED\n}\nTEST_END\n\nint\nmain(void) {\n\trtree_node_alloc_orig = rtree_node_alloc;\n\trtree_node_alloc = rtree_node_alloc_intercept;\n\trtree_node_dalloc_orig = rtree_node_dalloc;\n\trtree_node_dalloc = rtree_node_dalloc_intercept;\n\trtree_leaf_alloc_orig = rtree_leaf_alloc;\n\trtree_leaf_alloc = rtree_leaf_alloc_intercept;\n\trtree_leaf_dalloc_orig = rtree_leaf_dalloc;\n\trtree_leaf_dalloc = rtree_leaf_dalloc_intercept;\n\n\treturn test(\n\t    test_rtree_read_empty,\n\t    test_rtree_extrema,\n\t    test_rtree_bits,\n\t    test_rtree_random);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/size_classes.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic size_t\nget_max_size_class(void) {\n\tunsigned nlextents;\n\tsize_t mib[4];\n\tsize_t sz, miblen, max_size_class;\n\n\tsz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.nlextents\", (void *)&nlextents, &sz, NULL,\n\t    0), 0, \"Unexpected mallctl() error\");\n\n\tmiblen = sizeof(mib) / sizeof(size_t);\n\tassert_d_eq(mallctlnametomib(\"arenas.lextent.0.size\", mib, &miblen), 0,\n\t    \"Unexpected mallctlnametomib() error\");\n\tmib[2] = nlextents - 1;\n\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctlbymib(mib, miblen, (void *)&max_size_class, &sz,\n\t    NULL, 0), 0, \"Unexpected mallctlbymib() error\");\n\n\treturn max_size_class;\n}\n\nTEST_BEGIN(test_size_classes) {\n\tsize_t size_class, max_size_class;\n\tszind_t index, max_index;\n\n\tmax_size_class = get_max_size_class();\n\tmax_index = sz_size2index(max_size_class);\n\n\tfor (index = 0, size_class = sz_index2size(index); index < max_index ||\n\t    size_class < max_size_class; index++, size_class =\n\t    sz_index2size(index)) {\n\t\tassert_true(index < max_index,\n\t\t    \"Loop conditionals should be equivalent; index=%u, \"\n\t\t    \"size_class=%zu (%#zx)\", index, size_class, size_class);\n\t\tassert_true(size_class < max_size_class,\n\t\t    \"Loop conditionals should be equivalent; index=%u, \"\n\t\t    \"size_class=%zu (%#zx)\", index, size_class, size_class);\n\n\t\tassert_u_eq(index, sz_size2index(size_class),\n\t\t    \"sz_size2index() does not reverse sz_index2size(): index=%u\"\n\t\t    \" --> size_class=%zu --> index=%u --> size_class=%zu\",\n\t\t    index, size_class, sz_size2index(size_class),\n\t\t    sz_index2size(sz_size2index(size_class)));\n\t\tassert_zu_eq(size_class,\n\t\t    sz_index2size(sz_size2index(size_class)),\n\t\t    \"sz_index2size() does not reverse sz_size2index(): index=%u\"\n\t\t    \" --> size_class=%zu --> index=%u --> size_class=%zu\",\n\t\t    index, size_class, sz_size2index(size_class),\n\t\t    sz_index2size(sz_size2index(size_class)));\n\n\t\tassert_u_eq(index+1, sz_size2index(size_class+1),\n\t\t    \"Next size_class does not round up properly\");\n\n\t\tassert_zu_eq(size_class, (index > 0) ?\n\t\t    sz_s2u(sz_index2size(index-1)+1) : sz_s2u(1),\n\t\t    \"sz_s2u() does not round up to size class\");\n\t\tassert_zu_eq(size_class, sz_s2u(size_class-1),\n\t\t    \"sz_s2u() does not round up to size class\");\n\t\tassert_zu_eq(size_class, sz_s2u(size_class),\n\t\t    \"sz_s2u() does not compute same size class\");\n\t\tassert_zu_eq(sz_s2u(size_class+1), sz_index2size(index+1),\n\t\t    \"sz_s2u() does not round up to next size class\");\n\t}\n\n\tassert_u_eq(index, sz_size2index(sz_index2size(index)),\n\t    \"sz_size2index() does not reverse sz_index2size()\");\n\tassert_zu_eq(max_size_class, sz_index2size(\n\t    sz_size2index(max_size_class)),\n\t    \"sz_index2size() does not reverse sz_size2index()\");\n\n\tassert_zu_eq(size_class, sz_s2u(sz_index2size(index-1)+1),\n\t    \"sz_s2u() does not round up to size class\");\n\tassert_zu_eq(size_class, sz_s2u(size_class-1),\n\t    \"sz_s2u() does not round up to size class\");\n\tassert_zu_eq(size_class, sz_s2u(size_class),\n\t    \"sz_s2u() does not compute same size class\");\n}\nTEST_END\n\nTEST_BEGIN(test_psize_classes) {\n\tsize_t size_class, max_psz;\n\tpszind_t pind, max_pind;\n\n\tmax_psz = get_max_size_class() + PAGE;\n\tmax_pind = sz_psz2ind(max_psz);\n\n\tfor (pind = 0, size_class = sz_pind2sz(pind);\n\t    pind < max_pind || size_class < max_psz;\n\t    pind++, size_class = sz_pind2sz(pind)) {\n\t\tassert_true(pind < max_pind,\n\t\t    \"Loop conditionals should be equivalent; pind=%u, \"\n\t\t    \"size_class=%zu (%#zx)\", pind, size_class, size_class);\n\t\tassert_true(size_class < max_psz,\n\t\t    \"Loop conditionals should be equivalent; pind=%u, \"\n\t\t    \"size_class=%zu (%#zx)\", pind, size_class, size_class);\n\n\t\tassert_u_eq(pind, sz_psz2ind(size_class),\n\t\t    \"sz_psz2ind() does not reverse sz_pind2sz(): pind=%u -->\"\n\t\t    \" size_class=%zu --> pind=%u --> size_class=%zu\", pind,\n\t\t    size_class, sz_psz2ind(size_class),\n\t\t    sz_pind2sz(sz_psz2ind(size_class)));\n\t\tassert_zu_eq(size_class, sz_pind2sz(sz_psz2ind(size_class)),\n\t\t    \"sz_pind2sz() does not reverse sz_psz2ind(): pind=%u -->\"\n\t\t    \" size_class=%zu --> pind=%u --> size_class=%zu\", pind,\n\t\t    size_class, sz_psz2ind(size_class),\n\t\t    sz_pind2sz(sz_psz2ind(size_class)));\n\n\t\tassert_u_eq(pind+1, sz_psz2ind(size_class+1),\n\t\t    \"Next size_class does not round up properly\");\n\n\t\tassert_zu_eq(size_class, (pind > 0) ?\n\t\t    sz_psz2u(sz_pind2sz(pind-1)+1) : sz_psz2u(1),\n\t\t    \"sz_psz2u() does not round up to size class\");\n\t\tassert_zu_eq(size_class, sz_psz2u(size_class-1),\n\t\t    \"sz_psz2u() does not round up to size class\");\n\t\tassert_zu_eq(size_class, sz_psz2u(size_class),\n\t\t    \"sz_psz2u() does not compute same size class\");\n\t\tassert_zu_eq(sz_psz2u(size_class+1), sz_pind2sz(pind+1),\n\t\t    \"sz_psz2u() does not round up to next size class\");\n\t}\n\n\tassert_u_eq(pind, sz_psz2ind(sz_pind2sz(pind)),\n\t    \"sz_psz2ind() does not reverse sz_pind2sz()\");\n\tassert_zu_eq(max_psz, sz_pind2sz(sz_psz2ind(max_psz)),\n\t    \"sz_pind2sz() does not reverse sz_psz2ind()\");\n\n\tassert_zu_eq(size_class, sz_psz2u(sz_pind2sz(pind-1)+1),\n\t    \"sz_psz2u() does not round up to size class\");\n\tassert_zu_eq(size_class, sz_psz2u(size_class-1),\n\t    \"sz_psz2u() does not round up to size class\");\n\tassert_zu_eq(size_class, sz_psz2u(size_class),\n\t    \"sz_psz2u() does not compute same size class\");\n}\nTEST_END\n\nTEST_BEGIN(test_overflow) {\n\tsize_t max_size_class, max_psz;\n\n\tmax_size_class = get_max_size_class();\n\tmax_psz = max_size_class + PAGE;\n\n\tassert_u_eq(sz_size2index(max_size_class+1), NSIZES,\n\t    \"sz_size2index() should return NSIZES on overflow\");\n\tassert_u_eq(sz_size2index(ZU(PTRDIFF_MAX)+1), NSIZES,\n\t    \"sz_size2index() should return NSIZES on overflow\");\n\tassert_u_eq(sz_size2index(SIZE_T_MAX), NSIZES,\n\t    \"sz_size2index() should return NSIZES on overflow\");\n\n\tassert_zu_eq(sz_s2u(max_size_class+1), 0,\n\t    \"sz_s2u() should return 0 for unsupported size\");\n\tassert_zu_eq(sz_s2u(ZU(PTRDIFF_MAX)+1), 0,\n\t    \"sz_s2u() should return 0 for unsupported size\");\n\tassert_zu_eq(sz_s2u(SIZE_T_MAX), 0,\n\t    \"sz_s2u() should return 0 on overflow\");\n\n\tassert_u_eq(sz_psz2ind(max_size_class+1), NPSIZES,\n\t    \"sz_psz2ind() should return NPSIZES on overflow\");\n\tassert_u_eq(sz_psz2ind(ZU(PTRDIFF_MAX)+1), NPSIZES,\n\t    \"sz_psz2ind() should return NPSIZES on overflow\");\n\tassert_u_eq(sz_psz2ind(SIZE_T_MAX), NPSIZES,\n\t    \"sz_psz2ind() should return NPSIZES on overflow\");\n\n\tassert_zu_eq(sz_psz2u(max_size_class+1), max_psz,\n\t    \"sz_psz2u() should return (LARGE_MAXCLASS + PAGE) for unsupported\"\n\t    \" size\");\n\tassert_zu_eq(sz_psz2u(ZU(PTRDIFF_MAX)+1), max_psz,\n\t    \"sz_psz2u() should return (LARGE_MAXCLASS + PAGE) for unsupported \"\n\t    \"size\");\n\tassert_zu_eq(sz_psz2u(SIZE_T_MAX), max_psz,\n\t    \"sz_psz2u() should return (LARGE_MAXCLASS + PAGE) on overflow\");\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_size_classes,\n\t    test_psize_classes,\n\t    test_overflow);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/slab.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nTEST_BEGIN(test_arena_slab_regind) {\n\tszind_t binind;\n\n\tfor (binind = 0; binind < NBINS; binind++) {\n\t\tsize_t regind;\n\t\textent_t slab;\n\t\tconst arena_bin_info_t *bin_info = &arena_bin_info[binind];\n\t\textent_init(&slab, NULL, mallocx(bin_info->slab_size,\n\t\t    MALLOCX_LG_ALIGN(LG_PAGE)), bin_info->slab_size, true,\n\t\t    binind, 0, extent_state_active, false, true);\n\t\tassert_ptr_not_null(extent_addr_get(&slab),\n\t\t    \"Unexpected malloc() failure\");\n\t\tfor (regind = 0; regind < bin_info->nregs; regind++) {\n\t\t\tvoid *reg = (void *)((uintptr_t)extent_addr_get(&slab) +\n\t\t\t    (bin_info->reg_size * regind));\n\t\t\tassert_zu_eq(arena_slab_regind(&slab, binind, reg),\n\t\t\t    regind,\n\t\t\t    \"Incorrect region index computed for size %zu\",\n\t\t\t    bin_info->reg_size);\n\t\t}\n\t\tfree(extent_addr_get(&slab));\n\t}\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_arena_slab_regind);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/smoothstep.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic const uint64_t smoothstep_tab[] = {\n#define STEP(step, h, x, y)\t\t\t\\\n\th,\n\tSMOOTHSTEP\n#undef STEP\n};\n\nTEST_BEGIN(test_smoothstep_integral) {\n\tuint64_t sum, min, max;\n\tunsigned i;\n\n\t/*\n\t * The integral of smoothstep in the [0..1] range equals 1/2.  Verify\n\t * that the fixed point representation's integral is no more than\n\t * rounding error distant from 1/2.  Regarding rounding, each table\n\t * element is rounded down to the nearest fixed point value, so the\n\t * integral may be off by as much as SMOOTHSTEP_NSTEPS ulps.\n\t */\n\tsum = 0;\n\tfor (i = 0; i < SMOOTHSTEP_NSTEPS; i++) {\n\t\tsum += smoothstep_tab[i];\n\t}\n\n\tmax = (KQU(1) << (SMOOTHSTEP_BFP-1)) * (SMOOTHSTEP_NSTEPS+1);\n\tmin = max - SMOOTHSTEP_NSTEPS;\n\n\tassert_u64_ge(sum, min,\n\t    \"Integral too small, even accounting for truncation\");\n\tassert_u64_le(sum, max, \"Integral exceeds 1/2\");\n\tif (false) {\n\t\tmalloc_printf(\"%\"FMTu64\" ulps under 1/2 (limit %d)\\n\",\n\t\t    max - sum, SMOOTHSTEP_NSTEPS);\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_smoothstep_monotonic) {\n\tuint64_t prev_h;\n\tunsigned i;\n\n\t/*\n\t * The smoothstep function is monotonic in [0..1], i.e. its slope is\n\t * non-negative.  In practice we want to parametrize table generation\n\t * such that piecewise slope is greater than zero, but do not require\n\t * that here.\n\t */\n\tprev_h = 0;\n\tfor (i = 0; i < SMOOTHSTEP_NSTEPS; i++) {\n\t\tuint64_t h = smoothstep_tab[i];\n\t\tassert_u64_ge(h, prev_h, \"Piecewise non-monotonic, i=%u\", i);\n\t\tprev_h = h;\n\t}\n\tassert_u64_eq(smoothstep_tab[SMOOTHSTEP_NSTEPS-1],\n\t    (KQU(1) << SMOOTHSTEP_BFP), \"Last step must equal 1\");\n}\nTEST_END\n\nTEST_BEGIN(test_smoothstep_slope) {\n\tuint64_t prev_h, prev_delta;\n\tunsigned i;\n\n\t/*\n\t * The smoothstep slope strictly increases until x=0.5, and then\n\t * strictly decreases until x=1.0.  Verify the slightly weaker\n\t * requirement of monotonicity, so that inadequate table precision does\n\t * not cause false test failures.\n\t */\n\tprev_h = 0;\n\tprev_delta = 0;\n\tfor (i = 0; i < SMOOTHSTEP_NSTEPS / 2 + SMOOTHSTEP_NSTEPS % 2; i++) {\n\t\tuint64_t h = smoothstep_tab[i];\n\t\tuint64_t delta = h - prev_h;\n\t\tassert_u64_ge(delta, prev_delta,\n\t\t    \"Slope must monotonically increase in 0.0 <= x <= 0.5, \"\n\t\t    \"i=%u\", i);\n\t\tprev_h = h;\n\t\tprev_delta = delta;\n\t}\n\n\tprev_h = KQU(1) << SMOOTHSTEP_BFP;\n\tprev_delta = 0;\n\tfor (i = SMOOTHSTEP_NSTEPS-1; i >= SMOOTHSTEP_NSTEPS / 2; i--) {\n\t\tuint64_t h = smoothstep_tab[i];\n\t\tuint64_t delta = prev_h - h;\n\t\tassert_u64_ge(delta, prev_delta,\n\t\t    \"Slope must monotonically decrease in 0.5 <= x <= 1.0, \"\n\t\t    \"i=%u\", i);\n\t\tprev_h = h;\n\t\tprev_delta = delta;\n\t}\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_smoothstep_integral,\n\t    test_smoothstep_monotonic,\n\t    test_smoothstep_slope);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/spin.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/spin.h\"\n\nTEST_BEGIN(test_spin) {\n\tspin_t spinner = SPIN_INITIALIZER;\n\n\tfor (unsigned i = 0; i < 100; i++) {\n\t\tspin_adaptive(&spinner);\n\t}\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_spin);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/stats.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nTEST_BEGIN(test_stats_summary) {\n\tsize_t sz, allocated, active, resident, mapped;\n\tint expected = config_stats ? 0 : ENOENT;\n\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctl(\"stats.allocated\", (void *)&allocated, &sz, NULL,\n\t    0), expected, \"Unexpected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.active\", (void *)&active, &sz, NULL, 0),\n\t    expected, \"Unexpected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.resident\", (void *)&resident, &sz, NULL, 0),\n\t    expected, \"Unexpected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.mapped\", (void *)&mapped, &sz, NULL, 0),\n\t    expected, \"Unexpected mallctl() result\");\n\n\tif (config_stats) {\n\t\tassert_zu_le(allocated, active,\n\t\t    \"allocated should be no larger than active\");\n\t\tassert_zu_lt(active, resident,\n\t\t    \"active should be less than resident\");\n\t\tassert_zu_lt(active, mapped,\n\t\t    \"active should be less than mapped\");\n\t}\n}\nTEST_END\n\nTEST_BEGIN(test_stats_large) {\n\tvoid *p;\n\tuint64_t epoch;\n\tsize_t allocated;\n\tuint64_t nmalloc, ndalloc, nrequests;\n\tsize_t sz;\n\tint expected = config_stats ? 0 : ENOENT;\n\n\tp = mallocx(SMALL_MAXCLASS+1, MALLOCX_ARENA(0));\n\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\n\tassert_d_eq(mallctl(\"epoch\", NULL, NULL, (void *)&epoch, sizeof(epoch)),\n\t    0, \"Unexpected mallctl() failure\");\n\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctl(\"stats.arenas.0.large.allocated\",\n\t    (void *)&allocated, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\tsz = sizeof(uint64_t);\n\tassert_d_eq(mallctl(\"stats.arenas.0.large.nmalloc\", (void *)&nmalloc,\n\t    &sz, NULL, 0), expected, \"Unexpected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.arenas.0.large.ndalloc\", (void *)&ndalloc,\n\t    &sz, NULL, 0), expected, \"Unexpected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.arenas.0.large.nrequests\",\n\t    (void *)&nrequests, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\n\tif (config_stats) {\n\t\tassert_zu_gt(allocated, 0,\n\t\t    \"allocated should be greater than zero\");\n\t\tassert_u64_ge(nmalloc, ndalloc,\n\t\t    \"nmalloc should be at least as large as ndalloc\");\n\t\tassert_u64_le(nmalloc, nrequests,\n\t\t    \"nmalloc should no larger than nrequests\");\n\t}\n\n\tdallocx(p, 0);\n}\nTEST_END\n\nTEST_BEGIN(test_stats_arenas_summary) {\n\tvoid *little, *large;\n\tuint64_t epoch;\n\tsize_t sz;\n\tint expected = config_stats ? 0 : ENOENT;\n\tsize_t mapped;\n\tuint64_t dirty_npurge, dirty_nmadvise, dirty_purged;\n\tuint64_t muzzy_npurge, muzzy_nmadvise, muzzy_purged;\n\n\tlittle = mallocx(SMALL_MAXCLASS, MALLOCX_ARENA(0));\n\tassert_ptr_not_null(little, \"Unexpected mallocx() failure\");\n\tlarge = mallocx((1U << LG_LARGE_MINCLASS), MALLOCX_ARENA(0));\n\tassert_ptr_not_null(large, \"Unexpected mallocx() failure\");\n\n\tdallocx(little, 0);\n\tdallocx(large, 0);\n\n\tassert_d_eq(mallctl(\"thread.tcache.flush\", NULL, NULL, NULL, 0),\n\t    opt_tcache ? 0 : EFAULT, \"Unexpected mallctl() result\");\n\tassert_d_eq(mallctl(\"arena.0.purge\", NULL, NULL, NULL, 0), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tassert_d_eq(mallctl(\"epoch\", NULL, NULL, (void *)&epoch, sizeof(epoch)),\n\t    0, \"Unexpected mallctl() failure\");\n\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctl(\"stats.arenas.0.mapped\", (void *)&mapped, &sz, NULL,\n\t    0), expected, \"Unexepected mallctl() result\");\n\n\tsz = sizeof(uint64_t);\n\tassert_d_eq(mallctl(\"stats.arenas.0.dirty_npurge\",\n\t    (void *)&dirty_npurge, &sz, NULL, 0), expected,\n\t    \"Unexepected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.arenas.0.dirty_nmadvise\",\n\t    (void *)&dirty_nmadvise, &sz, NULL, 0), expected,\n\t    \"Unexepected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.arenas.0.dirty_purged\",\n\t    (void *)&dirty_purged, &sz, NULL, 0), expected,\n\t    \"Unexepected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.arenas.0.muzzy_npurge\",\n\t    (void *)&muzzy_npurge, &sz, NULL, 0), expected,\n\t    \"Unexepected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.arenas.0.muzzy_nmadvise\",\n\t    (void *)&muzzy_nmadvise, &sz, NULL, 0), expected,\n\t    \"Unexepected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.arenas.0.muzzy_purged\",\n\t    (void *)&muzzy_purged, &sz, NULL, 0), expected,\n\t    \"Unexepected mallctl() result\");\n\n\tif (config_stats) {\n\t\tif (!background_thread_enabled()) {\n\t\t\tassert_u64_gt(dirty_npurge + muzzy_npurge, 0,\n\t\t\t    \"At least one purge should have occurred\");\n\t\t}\n\t\tassert_u64_le(dirty_nmadvise, dirty_purged,\n\t\t    \"dirty_nmadvise should be no greater than dirty_purged\");\n\t\tassert_u64_le(muzzy_nmadvise, muzzy_purged,\n\t\t    \"muzzy_nmadvise should be no greater than muzzy_purged\");\n\t}\n}\nTEST_END\n\nvoid *\nthd_start(void *arg) {\n\treturn NULL;\n}\n\nstatic void\nno_lazy_lock(void) {\n\tthd_t thd;\n\n\tthd_create(&thd, thd_start, NULL);\n\tthd_join(thd, NULL);\n}\n\nTEST_BEGIN(test_stats_arenas_small) {\n\tvoid *p;\n\tsize_t sz, allocated;\n\tuint64_t epoch, nmalloc, ndalloc, nrequests;\n\tint expected = config_stats ? 0 : ENOENT;\n\n\tno_lazy_lock(); /* Lazy locking would dodge tcache testing. */\n\n\tp = mallocx(SMALL_MAXCLASS, MALLOCX_ARENA(0));\n\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\n\tassert_d_eq(mallctl(\"thread.tcache.flush\", NULL, NULL, NULL, 0),\n\t    opt_tcache ? 0 : EFAULT, \"Unexpected mallctl() result\");\n\n\tassert_d_eq(mallctl(\"epoch\", NULL, NULL, (void *)&epoch, sizeof(epoch)),\n\t    0, \"Unexpected mallctl() failure\");\n\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctl(\"stats.arenas.0.small.allocated\",\n\t    (void *)&allocated, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\tsz = sizeof(uint64_t);\n\tassert_d_eq(mallctl(\"stats.arenas.0.small.nmalloc\", (void *)&nmalloc,\n\t    &sz, NULL, 0), expected, \"Unexpected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.arenas.0.small.ndalloc\", (void *)&ndalloc,\n\t    &sz, NULL, 0), expected, \"Unexpected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.arenas.0.small.nrequests\",\n\t    (void *)&nrequests, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\n\tif (config_stats) {\n\t\tassert_zu_gt(allocated, 0,\n\t\t    \"allocated should be greater than zero\");\n\t\tassert_u64_gt(nmalloc, 0,\n\t\t    \"nmalloc should be no greater than zero\");\n\t\tassert_u64_ge(nmalloc, ndalloc,\n\t\t    \"nmalloc should be at least as large as ndalloc\");\n\t\tassert_u64_gt(nrequests, 0,\n\t\t    \"nrequests should be greater than zero\");\n\t}\n\n\tdallocx(p, 0);\n}\nTEST_END\n\nTEST_BEGIN(test_stats_arenas_large) {\n\tvoid *p;\n\tsize_t sz, allocated;\n\tuint64_t epoch, nmalloc, ndalloc;\n\tint expected = config_stats ? 0 : ENOENT;\n\n\tp = mallocx((1U << LG_LARGE_MINCLASS), MALLOCX_ARENA(0));\n\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\n\tassert_d_eq(mallctl(\"epoch\", NULL, NULL, (void *)&epoch, sizeof(epoch)),\n\t    0, \"Unexpected mallctl() failure\");\n\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctl(\"stats.arenas.0.large.allocated\",\n\t    (void *)&allocated, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\tsz = sizeof(uint64_t);\n\tassert_d_eq(mallctl(\"stats.arenas.0.large.nmalloc\", (void *)&nmalloc,\n\t    &sz, NULL, 0), expected, \"Unexpected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.arenas.0.large.ndalloc\", (void *)&ndalloc,\n\t    &sz, NULL, 0), expected, \"Unexpected mallctl() result\");\n\n\tif (config_stats) {\n\t\tassert_zu_gt(allocated, 0,\n\t\t    \"allocated should be greater than zero\");\n\t\tassert_u64_gt(nmalloc, 0,\n\t\t    \"nmalloc should be greater than zero\");\n\t\tassert_u64_ge(nmalloc, ndalloc,\n\t\t    \"nmalloc should be at least as large as ndalloc\");\n\t}\n\n\tdallocx(p, 0);\n}\nTEST_END\n\nstatic void\ngen_mallctl_str(char *cmd, char *name, unsigned arena_ind) {\n\tsprintf(cmd, \"stats.arenas.%u.bins.0.%s\", arena_ind, name);\n}\n\nTEST_BEGIN(test_stats_arenas_bins) {\n\tvoid *p;\n\tsize_t sz, curslabs, curregs;\n\tuint64_t epoch, nmalloc, ndalloc, nrequests, nfills, nflushes;\n\tuint64_t nslabs, nreslabs;\n\tint expected = config_stats ? 0 : ENOENT;\n\n\t/* Make sure allocation below isn't satisfied by tcache. */\n\tassert_d_eq(mallctl(\"thread.tcache.flush\", NULL, NULL, NULL, 0),\n\t    opt_tcache ? 0 : EFAULT, \"Unexpected mallctl() result\");\n\n\tunsigned arena_ind, old_arena_ind;\n\tsz = sizeof(unsigned);\n\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena_ind, &sz, NULL, 0),\n\t    0, \"Arena creation failure\");\n\tsz = sizeof(arena_ind);\n\tassert_d_eq(mallctl(\"thread.arena\", (void *)&old_arena_ind, &sz,\n\t    (void *)&arena_ind, sizeof(arena_ind)), 0,\n\t    \"Unexpected mallctl() failure\");\n\n\tp = malloc(arena_bin_info[0].reg_size);\n\tassert_ptr_not_null(p, \"Unexpected malloc() failure\");\n\n\tassert_d_eq(mallctl(\"thread.tcache.flush\", NULL, NULL, NULL, 0),\n\t    opt_tcache ? 0 : EFAULT, \"Unexpected mallctl() result\");\n\n\tassert_d_eq(mallctl(\"epoch\", NULL, NULL, (void *)&epoch, sizeof(epoch)),\n\t    0, \"Unexpected mallctl() failure\");\n\n\tchar cmd[128];\n\tsz = sizeof(uint64_t);\n\tgen_mallctl_str(cmd, \"nmalloc\", arena_ind);\n\tassert_d_eq(mallctl(cmd, (void *)&nmalloc, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\tgen_mallctl_str(cmd, \"ndalloc\", arena_ind);\n\tassert_d_eq(mallctl(cmd, (void *)&ndalloc, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\tgen_mallctl_str(cmd, \"nrequests\", arena_ind);\n\tassert_d_eq(mallctl(cmd, (void *)&nrequests, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\tsz = sizeof(size_t);\n\tgen_mallctl_str(cmd, \"curregs\", arena_ind);\n\tassert_d_eq(mallctl(cmd, (void *)&curregs, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\n\tsz = sizeof(uint64_t);\n\tgen_mallctl_str(cmd, \"nfills\", arena_ind);\n\tassert_d_eq(mallctl(cmd, (void *)&nfills, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\tgen_mallctl_str(cmd, \"nflushes\", arena_ind);\n\tassert_d_eq(mallctl(cmd, (void *)&nflushes, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\n\tgen_mallctl_str(cmd, \"nslabs\", arena_ind);\n\tassert_d_eq(mallctl(cmd, (void *)&nslabs, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\tgen_mallctl_str(cmd, \"nreslabs\", arena_ind);\n\tassert_d_eq(mallctl(cmd, (void *)&nreslabs, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\tsz = sizeof(size_t);\n\tgen_mallctl_str(cmd, \"curslabs\", arena_ind);\n\tassert_d_eq(mallctl(cmd, (void *)&curslabs, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\n\tif (config_stats) {\n\t\tassert_u64_gt(nmalloc, 0,\n\t\t    \"nmalloc should be greater than zero\");\n\t\tassert_u64_ge(nmalloc, ndalloc,\n\t\t    \"nmalloc should be at least as large as ndalloc\");\n\t\tassert_u64_gt(nrequests, 0,\n\t\t    \"nrequests should be greater than zero\");\n\t\tassert_zu_gt(curregs, 0,\n\t\t    \"allocated should be greater than zero\");\n\t\tif (opt_tcache) {\n\t\t\tassert_u64_gt(nfills, 0,\n\t\t\t    \"At least one fill should have occurred\");\n\t\t\tassert_u64_gt(nflushes, 0,\n\t\t\t    \"At least one flush should have occurred\");\n\t\t}\n\t\tassert_u64_gt(nslabs, 0,\n\t\t    \"At least one slab should have been allocated\");\n\t\tassert_zu_gt(curslabs, 0,\n\t\t    \"At least one slab should be currently allocated\");\n\t}\n\n\tdallocx(p, 0);\n}\nTEST_END\n\nTEST_BEGIN(test_stats_arenas_lextents) {\n\tvoid *p;\n\tuint64_t epoch, nmalloc, ndalloc;\n\tsize_t curlextents, sz, hsize;\n\tint expected = config_stats ? 0 : ENOENT;\n\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctl(\"arenas.lextent.0.size\", (void *)&hsize, &sz, NULL,\n\t    0), 0, \"Unexpected mallctl() failure\");\n\n\tp = mallocx(hsize, MALLOCX_ARENA(0));\n\tassert_ptr_not_null(p, \"Unexpected mallocx() failure\");\n\n\tassert_d_eq(mallctl(\"epoch\", NULL, NULL, (void *)&epoch, sizeof(epoch)),\n\t    0, \"Unexpected mallctl() failure\");\n\n\tsz = sizeof(uint64_t);\n\tassert_d_eq(mallctl(\"stats.arenas.0.lextents.0.nmalloc\",\n\t    (void *)&nmalloc, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\tassert_d_eq(mallctl(\"stats.arenas.0.lextents.0.ndalloc\",\n\t    (void *)&ndalloc, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\tsz = sizeof(size_t);\n\tassert_d_eq(mallctl(\"stats.arenas.0.lextents.0.curlextents\",\n\t    (void *)&curlextents, &sz, NULL, 0), expected,\n\t    \"Unexpected mallctl() result\");\n\n\tif (config_stats) {\n\t\tassert_u64_gt(nmalloc, 0,\n\t\t    \"nmalloc should be greater than zero\");\n\t\tassert_u64_ge(nmalloc, ndalloc,\n\t\t    \"nmalloc should be at least as large as ndalloc\");\n\t\tassert_u64_gt(curlextents, 0,\n\t\t    \"At least one extent should be currently allocated\");\n\t}\n\n\tdallocx(p, 0);\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test_no_reentrancy(\n\t    test_stats_summary,\n\t    test_stats_large,\n\t    test_stats_arenas_summary,\n\t    test_stats_arenas_small,\n\t    test_stats_arenas_large,\n\t    test_stats_arenas_bins,\n\t    test_stats_arenas_lextents);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/stats_print.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/util.h\"\n\ntypedef enum {\n\tTOKEN_TYPE_NONE,\n\tTOKEN_TYPE_ERROR,\n\tTOKEN_TYPE_EOI,\n\tTOKEN_TYPE_NULL,\n\tTOKEN_TYPE_FALSE,\n\tTOKEN_TYPE_TRUE,\n\tTOKEN_TYPE_LBRACKET,\n\tTOKEN_TYPE_RBRACKET,\n\tTOKEN_TYPE_LBRACE,\n\tTOKEN_TYPE_RBRACE,\n\tTOKEN_TYPE_COLON,\n\tTOKEN_TYPE_COMMA,\n\tTOKEN_TYPE_STRING,\n\tTOKEN_TYPE_NUMBER\n} token_type_t;\n\ntypedef struct parser_s parser_t;\ntypedef struct {\n\tparser_t\t*parser;\n\ttoken_type_t\ttoken_type;\n\tsize_t\t\tpos;\n\tsize_t\t\tlen;\n\tsize_t\t\tline;\n\tsize_t\t\tcol;\n} token_t;\n\nstruct parser_s {\n\tbool verbose;\n\tchar\t*buf; /* '\\0'-terminated. */\n\tsize_t\tlen; /* Number of characters preceding '\\0' in buf. */\n\tsize_t\tpos;\n\tsize_t\tline;\n\tsize_t\tcol;\n\ttoken_t\ttoken;\n};\n\nstatic void\ntoken_init(token_t *token, parser_t *parser, token_type_t token_type,\n    size_t pos, size_t len, size_t line, size_t col) {\n\ttoken->parser = parser;\n\ttoken->token_type = token_type;\n\ttoken->pos = pos;\n\ttoken->len = len;\n\ttoken->line = line;\n\ttoken->col = col;\n}\n\nstatic void\ntoken_error(token_t *token) {\n\tif (!token->parser->verbose) {\n\t\treturn;\n\t}\n\tswitch (token->token_type) {\n\tcase TOKEN_TYPE_NONE:\n\t\tnot_reached();\n\tcase TOKEN_TYPE_ERROR:\n\t\tmalloc_printf(\"%zu:%zu: Unexpected character in token: \",\n\t\t    token->line, token->col);\n\t\tbreak;\n\tdefault:\n\t\tmalloc_printf(\"%zu:%zu: Unexpected token: \", token->line,\n\t\t    token->col);\n\t\tbreak;\n\t}\n\tUNUSED ssize_t err = write(STDERR_FILENO,\n\t    &token->parser->buf[token->pos], token->len);\n\tmalloc_printf(\"\\n\");\n}\n\nstatic void\nparser_init(parser_t *parser, bool verbose) {\n\tparser->verbose = verbose;\n\tparser->buf = NULL;\n\tparser->len = 0;\n\tparser->pos = 0;\n\tparser->line = 1;\n\tparser->col = 0;\n}\n\nstatic void\nparser_fini(parser_t *parser) {\n\tif (parser->buf != NULL) {\n\t\tdallocx(parser->buf, MALLOCX_TCACHE_NONE);\n\t}\n}\n\nstatic bool\nparser_append(parser_t *parser, const char *str) {\n\tsize_t len = strlen(str);\n\tchar *buf = (parser->buf == NULL) ? mallocx(len + 1,\n\t    MALLOCX_TCACHE_NONE) : rallocx(parser->buf, parser->len + len + 1,\n\t    MALLOCX_TCACHE_NONE);\n\tif (buf == NULL) {\n\t\treturn true;\n\t}\n\tmemcpy(&buf[parser->len], str, len + 1);\n\tparser->buf = buf;\n\tparser->len += len;\n\treturn false;\n}\n\nstatic bool\nparser_tokenize(parser_t *parser) {\n\tenum {\n\t\tSTATE_START,\n\t\tSTATE_EOI,\n\t\tSTATE_N, STATE_NU, STATE_NUL, STATE_NULL,\n\t\tSTATE_F, STATE_FA, STATE_FAL, STATE_FALS, STATE_FALSE,\n\t\tSTATE_T, STATE_TR, STATE_TRU, STATE_TRUE,\n\t\tSTATE_LBRACKET,\n\t\tSTATE_RBRACKET,\n\t\tSTATE_LBRACE,\n\t\tSTATE_RBRACE,\n\t\tSTATE_COLON,\n\t\tSTATE_COMMA,\n\t\tSTATE_CHARS,\n\t\tSTATE_CHAR_ESCAPE,\n\t\tSTATE_CHAR_U, STATE_CHAR_UD, STATE_CHAR_UDD, STATE_CHAR_UDDD,\n\t\tSTATE_STRING,\n\t\tSTATE_MINUS,\n\t\tSTATE_LEADING_ZERO,\n\t\tSTATE_DIGITS,\n\t\tSTATE_DECIMAL,\n\t\tSTATE_FRAC_DIGITS,\n\t\tSTATE_EXP,\n\t\tSTATE_EXP_SIGN,\n\t\tSTATE_EXP_DIGITS,\n\t\tSTATE_ACCEPT\n\t} state = STATE_START;\n\tsize_t token_pos JEMALLOC_CC_SILENCE_INIT(0);\n\tsize_t token_line JEMALLOC_CC_SILENCE_INIT(1);\n\tsize_t token_col JEMALLOC_CC_SILENCE_INIT(0);\n\n\tassert_zu_le(parser->pos, parser->len,\n\t    \"Position is past end of buffer\");\n\n\twhile (state != STATE_ACCEPT) {\n\t\tchar c = parser->buf[parser->pos];\n\n\t\tswitch (state) {\n\t\tcase STATE_START:\n\t\t\ttoken_pos = parser->pos;\n\t\t\ttoken_line = parser->line;\n\t\t\ttoken_col = parser->col;\n\t\t\tswitch (c) {\n\t\t\tcase ' ': case '\\b': case '\\n': case '\\r': case '\\t':\n\t\t\t\tbreak;\n\t\t\tcase '\\0':\n\t\t\t\tstate = STATE_EOI;\n\t\t\t\tbreak;\n\t\t\tcase 'n':\n\t\t\t\tstate = STATE_N;\n\t\t\t\tbreak;\n\t\t\tcase 'f':\n\t\t\t\tstate = STATE_F;\n\t\t\t\tbreak;\n\t\t\tcase 't':\n\t\t\t\tstate = STATE_T;\n\t\t\t\tbreak;\n\t\t\tcase '[':\n\t\t\t\tstate = STATE_LBRACKET;\n\t\t\t\tbreak;\n\t\t\tcase ']':\n\t\t\t\tstate = STATE_RBRACKET;\n\t\t\t\tbreak;\n\t\t\tcase '{':\n\t\t\t\tstate = STATE_LBRACE;\n\t\t\t\tbreak;\n\t\t\tcase '}':\n\t\t\t\tstate = STATE_RBRACE;\n\t\t\t\tbreak;\n\t\t\tcase ':':\n\t\t\t\tstate = STATE_COLON;\n\t\t\t\tbreak;\n\t\t\tcase ',':\n\t\t\t\tstate = STATE_COMMA;\n\t\t\t\tbreak;\n\t\t\tcase '\"':\n\t\t\t\tstate = STATE_CHARS;\n\t\t\t\tbreak;\n\t\t\tcase '-':\n\t\t\t\tstate = STATE_MINUS;\n\t\t\t\tbreak;\n\t\t\tcase '0':\n\t\t\t\tstate = STATE_LEADING_ZERO;\n\t\t\t\tbreak;\n\t\t\tcase '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9':\n\t\t\t\tstate = STATE_DIGITS;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_EOI:\n\t\t\ttoken_init(&parser->token, parser,\n\t\t\t    TOKEN_TYPE_EOI, token_pos, parser->pos -\n\t\t\t    token_pos, token_line, token_col);\n\t\t\tstate = STATE_ACCEPT;\n\t\t\tbreak;\n\t\tcase STATE_N:\n\t\t\tswitch (c) {\n\t\t\tcase 'u':\n\t\t\t\tstate = STATE_NU;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_NU:\n\t\t\tswitch (c) {\n\t\t\tcase 'l':\n\t\t\t\tstate = STATE_NUL;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_NUL:\n\t\t\tswitch (c) {\n\t\t\tcase 'l':\n\t\t\t\tstate = STATE_NULL;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_NULL:\n\t\t\tswitch (c) {\n\t\t\tcase ' ': case '\\b': case '\\n': case '\\r': case '\\t':\n\t\t\tcase '\\0':\n\t\t\tcase '[': case ']': case '{': case '}': case ':':\n\t\t\tcase ',':\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\ttoken_init(&parser->token, parser, TOKEN_TYPE_NULL,\n\t\t\t    token_pos, parser->pos - token_pos, token_line,\n\t\t\t    token_col);\n\t\t\tstate = STATE_ACCEPT;\n\t\t\tbreak;\n\t\tcase STATE_F:\n\t\t\tswitch (c) {\n\t\t\tcase 'a':\n\t\t\t\tstate = STATE_FA;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_FA:\n\t\t\tswitch (c) {\n\t\t\tcase 'l':\n\t\t\t\tstate = STATE_FAL;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_FAL:\n\t\t\tswitch (c) {\n\t\t\tcase 's':\n\t\t\t\tstate = STATE_FALS;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_FALS:\n\t\t\tswitch (c) {\n\t\t\tcase 'e':\n\t\t\t\tstate = STATE_FALSE;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_FALSE:\n\t\t\tswitch (c) {\n\t\t\tcase ' ': case '\\b': case '\\n': case '\\r': case '\\t':\n\t\t\tcase '\\0':\n\t\t\tcase '[': case ']': case '{': case '}': case ':':\n\t\t\tcase ',':\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\ttoken_init(&parser->token, parser,\n\t\t\t    TOKEN_TYPE_FALSE, token_pos, parser->pos -\n\t\t\t    token_pos, token_line, token_col);\n\t\t\tstate = STATE_ACCEPT;\n\t\t\tbreak;\n\t\tcase STATE_T:\n\t\t\tswitch (c) {\n\t\t\tcase 'r':\n\t\t\t\tstate = STATE_TR;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_TR:\n\t\t\tswitch (c) {\n\t\t\tcase 'u':\n\t\t\t\tstate = STATE_TRU;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_TRU:\n\t\t\tswitch (c) {\n\t\t\tcase 'e':\n\t\t\t\tstate = STATE_TRUE;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_TRUE:\n\t\t\tswitch (c) {\n\t\t\tcase ' ': case '\\b': case '\\n': case '\\r': case '\\t':\n\t\t\tcase '\\0':\n\t\t\tcase '[': case ']': case '{': case '}': case ':':\n\t\t\tcase ',':\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\ttoken_init(&parser->token, parser, TOKEN_TYPE_TRUE,\n\t\t\t    token_pos, parser->pos - token_pos, token_line,\n\t\t\t    token_col);\n\t\t\tstate = STATE_ACCEPT;\n\t\t\tbreak;\n\t\tcase STATE_LBRACKET:\n\t\t\ttoken_init(&parser->token, parser, TOKEN_TYPE_LBRACKET,\n\t\t\t    token_pos, parser->pos - token_pos, token_line,\n\t\t\t    token_col);\n\t\t\tstate = STATE_ACCEPT;\n\t\t\tbreak;\n\t\tcase STATE_RBRACKET:\n\t\t\ttoken_init(&parser->token, parser, TOKEN_TYPE_RBRACKET,\n\t\t\t    token_pos, parser->pos - token_pos, token_line,\n\t\t\t    token_col);\n\t\t\tstate = STATE_ACCEPT;\n\t\t\tbreak;\n\t\tcase STATE_LBRACE:\n\t\t\ttoken_init(&parser->token, parser, TOKEN_TYPE_LBRACE,\n\t\t\t    token_pos, parser->pos - token_pos, token_line,\n\t\t\t    token_col);\n\t\t\tstate = STATE_ACCEPT;\n\t\t\tbreak;\n\t\tcase STATE_RBRACE:\n\t\t\ttoken_init(&parser->token, parser, TOKEN_TYPE_RBRACE,\n\t\t\t    token_pos, parser->pos - token_pos, token_line,\n\t\t\t    token_col);\n\t\t\tstate = STATE_ACCEPT;\n\t\t\tbreak;\n\t\tcase STATE_COLON:\n\t\t\ttoken_init(&parser->token, parser, TOKEN_TYPE_COLON,\n\t\t\t    token_pos, parser->pos - token_pos, token_line,\n\t\t\t    token_col);\n\t\t\tstate = STATE_ACCEPT;\n\t\t\tbreak;\n\t\tcase STATE_COMMA:\n\t\t\ttoken_init(&parser->token, parser, TOKEN_TYPE_COMMA,\n\t\t\t    token_pos, parser->pos - token_pos, token_line,\n\t\t\t    token_col);\n\t\t\tstate = STATE_ACCEPT;\n\t\t\tbreak;\n\t\tcase STATE_CHARS:\n\t\t\tswitch (c) {\n\t\t\tcase '\\\\':\n\t\t\t\tstate = STATE_CHAR_ESCAPE;\n\t\t\t\tbreak;\n\t\t\tcase '\"':\n\t\t\t\tstate = STATE_STRING;\n\t\t\t\tbreak;\n\t\t\tcase 0x00: case 0x01: case 0x02: case 0x03: case 0x04:\n\t\t\tcase 0x05: case 0x06: case 0x07: case 0x08: case 0x09:\n\t\t\tcase 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e:\n\t\t\tcase 0x0f: case 0x10: case 0x11: case 0x12: case 0x13:\n\t\t\tcase 0x14: case 0x15: case 0x16: case 0x17: case 0x18:\n\t\t\tcase 0x19: case 0x1a: case 0x1b: case 0x1c: case 0x1d:\n\t\t\tcase 0x1e: case 0x1f:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\tdefault:\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_CHAR_ESCAPE:\n\t\t\tswitch (c) {\n\t\t\tcase '\"': case '\\\\': case '/': case 'b': case 'n':\n\t\t\tcase 'r': case 't':\n\t\t\t\tstate = STATE_CHARS;\n\t\t\t\tbreak;\n\t\t\tcase 'u':\n\t\t\t\tstate = STATE_CHAR_U;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_CHAR_U:\n\t\t\tswitch (c) {\n\t\t\tcase '0': case '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9':\n\t\t\tcase 'a': case 'b': case 'c': case 'd': case 'e':\n\t\t\tcase 'f':\n\t\t\tcase 'A': case 'B': case 'C': case 'D': case 'E':\n\t\t\tcase 'F':\n\t\t\t\tstate = STATE_CHAR_UD;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_CHAR_UD:\n\t\t\tswitch (c) {\n\t\t\tcase '0': case '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9':\n\t\t\tcase 'a': case 'b': case 'c': case 'd': case 'e':\n\t\t\tcase 'f':\n\t\t\tcase 'A': case 'B': case 'C': case 'D': case 'E':\n\t\t\tcase 'F':\n\t\t\t\tstate = STATE_CHAR_UDD;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_CHAR_UDD:\n\t\t\tswitch (c) {\n\t\t\tcase '0': case '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9':\n\t\t\tcase 'a': case 'b': case 'c': case 'd': case 'e':\n\t\t\tcase 'f':\n\t\t\tcase 'A': case 'B': case 'C': case 'D': case 'E':\n\t\t\tcase 'F':\n\t\t\t\tstate = STATE_CHAR_UDDD;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_CHAR_UDDD:\n\t\t\tswitch (c) {\n\t\t\tcase '0': case '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9':\n\t\t\tcase 'a': case 'b': case 'c': case 'd': case 'e':\n\t\t\tcase 'f':\n\t\t\tcase 'A': case 'B': case 'C': case 'D': case 'E':\n\t\t\tcase 'F':\n\t\t\t\tstate = STATE_CHARS;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_STRING:\n\t\t\ttoken_init(&parser->token, parser, TOKEN_TYPE_STRING,\n\t\t\t    token_pos, parser->pos - token_pos, token_line,\n\t\t\t    token_col);\n\t\t\tstate = STATE_ACCEPT;\n\t\t\tbreak;\n\t\tcase STATE_MINUS:\n\t\t\tswitch (c) {\n\t\t\tcase '0':\n\t\t\t\tstate = STATE_LEADING_ZERO;\n\t\t\t\tbreak;\n\t\t\tcase '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9':\n\t\t\t\tstate = STATE_DIGITS;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_LEADING_ZERO:\n\t\t\tswitch (c) {\n\t\t\tcase '.':\n\t\t\t\tstate = STATE_DECIMAL;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_NUMBER, token_pos, parser->pos -\n\t\t\t\t    token_pos, token_line, token_col);\n\t\t\t\tstate = STATE_ACCEPT;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_DIGITS:\n\t\t\tswitch (c) {\n\t\t\tcase '0': case '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9':\n\t\t\t\tbreak;\n\t\t\tcase '.':\n\t\t\t\tstate = STATE_DECIMAL;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_NUMBER, token_pos, parser->pos -\n\t\t\t\t    token_pos, token_line, token_col);\n\t\t\t\tstate = STATE_ACCEPT;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_DECIMAL:\n\t\t\tswitch (c) {\n\t\t\tcase '0': case '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9':\n\t\t\t\tstate = STATE_FRAC_DIGITS;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_FRAC_DIGITS:\n\t\t\tswitch (c) {\n\t\t\tcase '0': case '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9':\n\t\t\t\tbreak;\n\t\t\tcase 'e': case 'E':\n\t\t\t\tstate = STATE_EXP;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_NUMBER, token_pos, parser->pos -\n\t\t\t\t    token_pos, token_line, token_col);\n\t\t\t\tstate = STATE_ACCEPT;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_EXP:\n\t\t\tswitch (c) {\n\t\t\tcase '-': case '+':\n\t\t\t\tstate = STATE_EXP_SIGN;\n\t\t\t\tbreak;\n\t\t\tcase '0': case '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9':\n\t\t\t\tstate = STATE_EXP_DIGITS;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_EXP_SIGN:\n\t\t\tswitch (c) {\n\t\t\tcase '0': case '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9':\n\t\t\t\tstate = STATE_EXP_DIGITS;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_ERROR, token_pos, parser->pos + 1\n\t\t\t\t    - token_pos, token_line, token_col);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase STATE_EXP_DIGITS:\n\t\t\tswitch (c) {\n\t\t\tcase '0': case '1': case '2': case '3': case '4':\n\t\t\tcase '5': case '6': case '7': case '8': case '9':\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\ttoken_init(&parser->token, parser,\n\t\t\t\t    TOKEN_TYPE_NUMBER, token_pos, parser->pos -\n\t\t\t\t    token_pos, token_line, token_col);\n\t\t\t\tstate = STATE_ACCEPT;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tnot_reached();\n\t\t}\n\n\t\tif (state != STATE_ACCEPT) {\n\t\t\tif (c == '\\n') {\n\t\t\t\tparser->line++;\n\t\t\t\tparser->col = 0;\n\t\t\t} else {\n\t\t\t\tparser->col++;\n\t\t\t}\n\t\t\tparser->pos++;\n\t\t}\n\t}\n\treturn false;\n}\n\nstatic bool\tparser_parse_array(parser_t *parser);\nstatic bool\tparser_parse_object(parser_t *parser);\n\nstatic bool\nparser_parse_value(parser_t *parser) {\n\tswitch (parser->token.token_type) {\n\tcase TOKEN_TYPE_NULL:\n\tcase TOKEN_TYPE_FALSE:\n\tcase TOKEN_TYPE_TRUE:\n\tcase TOKEN_TYPE_STRING:\n\tcase TOKEN_TYPE_NUMBER:\n\t\treturn false;\n\tcase TOKEN_TYPE_LBRACE:\n\t\treturn parser_parse_object(parser);\n\tcase TOKEN_TYPE_LBRACKET:\n\t\treturn parser_parse_array(parser);\n\tdefault:\n\t\treturn true;\n\t}\n\tnot_reached();\n}\n\nstatic bool\nparser_parse_pair(parser_t *parser) {\n\tassert_d_eq(parser->token.token_type, TOKEN_TYPE_STRING,\n\t    \"Pair should start with string\");\n\tif (parser_tokenize(parser)) {\n\t\treturn true;\n\t}\n\tswitch (parser->token.token_type) {\n\tcase TOKEN_TYPE_COLON:\n\t\tif (parser_tokenize(parser)) {\n\t\t\treturn true;\n\t\t}\n\t\treturn parser_parse_value(parser);\n\tdefault:\n\t\treturn true;\n\t}\n}\n\nstatic bool\nparser_parse_values(parser_t *parser) {\n\tif (parser_parse_value(parser)) {\n\t\treturn true;\n\t}\n\n\twhile (true) {\n\t\tif (parser_tokenize(parser)) {\n\t\t\treturn true;\n\t\t}\n\t\tswitch (parser->token.token_type) {\n\t\tcase TOKEN_TYPE_COMMA:\n\t\t\tif (parser_tokenize(parser)) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tif (parser_parse_value(parser)) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase TOKEN_TYPE_RBRACKET:\n\t\t\treturn false;\n\t\tdefault:\n\t\t\treturn true;\n\t\t}\n\t}\n}\n\nstatic bool\nparser_parse_array(parser_t *parser) {\n\tassert_d_eq(parser->token.token_type, TOKEN_TYPE_LBRACKET,\n\t    \"Array should start with [\");\n\tif (parser_tokenize(parser)) {\n\t\treturn true;\n\t}\n\tswitch (parser->token.token_type) {\n\tcase TOKEN_TYPE_RBRACKET:\n\t\treturn false;\n\tdefault:\n\t\treturn parser_parse_values(parser);\n\t}\n\tnot_reached();\n}\n\nstatic bool\nparser_parse_pairs(parser_t *parser) {\n\tassert_d_eq(parser->token.token_type, TOKEN_TYPE_STRING,\n\t    \"Object should start with string\");\n\tif (parser_parse_pair(parser)) {\n\t\treturn true;\n\t}\n\n\twhile (true) {\n\t\tif (parser_tokenize(parser)) {\n\t\t\treturn true;\n\t\t}\n\t\tswitch (parser->token.token_type) {\n\t\tcase TOKEN_TYPE_COMMA:\n\t\t\tif (parser_tokenize(parser)) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tswitch (parser->token.token_type) {\n\t\t\tcase TOKEN_TYPE_STRING:\n\t\t\t\tif (parser_parse_pair(parser)) {\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase TOKEN_TYPE_RBRACE:\n\t\t\treturn false;\n\t\tdefault:\n\t\t\treturn true;\n\t\t}\n\t}\n}\n\nstatic bool\nparser_parse_object(parser_t *parser) {\n\tassert_d_eq(parser->token.token_type, TOKEN_TYPE_LBRACE,\n\t    \"Object should start with {\");\n\tif (parser_tokenize(parser)) {\n\t\treturn true;\n\t}\n\tswitch (parser->token.token_type) {\n\tcase TOKEN_TYPE_STRING:\n\t\treturn parser_parse_pairs(parser);\n\tcase TOKEN_TYPE_RBRACE:\n\t\treturn false;\n\tdefault:\n\t\treturn true;\n\t}\n\tnot_reached();\n}\n\nstatic bool\nparser_parse(parser_t *parser) {\n\tif (parser_tokenize(parser)) {\n\t\tgoto label_error;\n\t}\n\tif (parser_parse_value(parser)) {\n\t\tgoto label_error;\n\t}\n\n\tif (parser_tokenize(parser)) {\n\t\tgoto label_error;\n\t}\n\tswitch (parser->token.token_type) {\n\tcase TOKEN_TYPE_EOI:\n\t\treturn false;\n\tdefault:\n\t\tgoto label_error;\n\t}\n\tnot_reached();\n\nlabel_error:\n\ttoken_error(&parser->token);\n\treturn true;\n}\n\nTEST_BEGIN(test_json_parser) {\n\tsize_t i;\n\tconst char *invalid_inputs[] = {\n\t\t/* Tokenizer error case tests. */\n\t\t\"{ \\\"string\\\": X }\",\n\t\t\"{ \\\"string\\\": nXll }\",\n\t\t\"{ \\\"string\\\": nuXl }\",\n\t\t\"{ \\\"string\\\": nulX }\",\n\t\t\"{ \\\"string\\\": nullX }\",\n\t\t\"{ \\\"string\\\": fXlse }\",\n\t\t\"{ \\\"string\\\": faXse }\",\n\t\t\"{ \\\"string\\\": falXe }\",\n\t\t\"{ \\\"string\\\": falsX }\",\n\t\t\"{ \\\"string\\\": falseX }\",\n\t\t\"{ \\\"string\\\": tXue }\",\n\t\t\"{ \\\"string\\\": trXe }\",\n\t\t\"{ \\\"string\\\": truX }\",\n\t\t\"{ \\\"string\\\": trueX }\",\n\t\t\"{ \\\"string\\\": \\\"\\n\\\" }\",\n\t\t\"{ \\\"string\\\": \\\"\\\\z\\\" }\",\n\t\t\"{ \\\"string\\\": \\\"\\\\uX000\\\" }\",\n\t\t\"{ \\\"string\\\": \\\"\\\\u0X00\\\" }\",\n\t\t\"{ \\\"string\\\": \\\"\\\\u00X0\\\" }\",\n\t\t\"{ \\\"string\\\": \\\"\\\\u000X\\\" }\",\n\t\t\"{ \\\"string\\\": -X }\",\n\t\t\"{ \\\"string\\\": 0.X }\",\n\t\t\"{ \\\"string\\\": 0.0eX }\",\n\t\t\"{ \\\"string\\\": 0.0e+X }\",\n\n\t\t/* Parser error test cases. */\n\t\t\"{\\\"string\\\": }\",\n\t\t\"{\\\"string\\\" }\",\n\t\t\"{\\\"string\\\": [ 0 }\",\n\t\t\"{\\\"string\\\": {\\\"a\\\":0, 1 } }\",\n\t\t\"{\\\"string\\\": {\\\"a\\\":0: } }\",\n\t\t\"{\",\n\t\t\"{}{\",\n\t};\n\tconst char *valid_inputs[] = {\n\t\t/* Token tests. */\n\t\t\"null\",\n\t\t\"false\",\n\t\t\"true\",\n\t\t\"{}\",\n\t\t\"{\\\"a\\\": 0}\",\n\t\t\"[]\",\n\t\t\"[0, 1]\",\n\t\t\"0\",\n\t\t\"1\",\n\t\t\"10\",\n\t\t\"-10\",\n\t\t\"10.23\",\n\t\t\"10.23e4\",\n\t\t\"10.23e-4\",\n\t\t\"10.23e+4\",\n\t\t\"10.23E4\",\n\t\t\"10.23E-4\",\n\t\t\"10.23E+4\",\n\t\t\"-10.23\",\n\t\t\"-10.23e4\",\n\t\t\"-10.23e-4\",\n\t\t\"-10.23e+4\",\n\t\t\"-10.23E4\",\n\t\t\"-10.23E-4\",\n\t\t\"-10.23E+4\",\n\t\t\"\\\"value\\\"\",\n\t\t\"\\\" \\\\\\\" \\\\/ \\\\b \\\\n \\\\r \\\\t \\\\u0abc \\\\u1DEF \\\"\",\n\n\t\t/* Parser test with various nesting. */\n\t\t\"{\\\"a\\\":null, \\\"b\\\":[1,[{\\\"c\\\":2},3]], \\\"d\\\":{\\\"e\\\":true}}\",\n\t};\n\n\tfor (i = 0; i < sizeof(invalid_inputs)/sizeof(const char *); i++) {\n\t\tconst char *input = invalid_inputs[i];\n\t\tparser_t parser;\n\t\tparser_init(&parser, false);\n\t\tassert_false(parser_append(&parser, input),\n\t\t    \"Unexpected input appending failure\");\n\t\tassert_true(parser_parse(&parser),\n\t\t    \"Unexpected parse success for input: %s\", input);\n\t\tparser_fini(&parser);\n\t}\n\n\tfor (i = 0; i < sizeof(valid_inputs)/sizeof(const char *); i++) {\n\t\tconst char *input = valid_inputs[i];\n\t\tparser_t parser;\n\t\tparser_init(&parser, true);\n\t\tassert_false(parser_append(&parser, input),\n\t\t    \"Unexpected input appending failure\");\n\t\tassert_false(parser_parse(&parser),\n\t\t    \"Unexpected parse error for input: %s\", input);\n\t\tparser_fini(&parser);\n\t}\n}\nTEST_END\n\nvoid\nwrite_cb(void *opaque, const char *str) {\n\tparser_t *parser = (parser_t *)opaque;\n\tif (parser_append(parser, str)) {\n\t\ttest_fail(\"Unexpected input appending failure\");\n\t}\n}\n\nTEST_BEGIN(test_stats_print_json) {\n\tconst char *opts[] = {\n\t\t\"J\",\n\t\t\"Jg\",\n\t\t\"Jm\",\n\t\t\"Jd\",\n\t\t\"Jmd\",\n\t\t\"Jgd\",\n\t\t\"Jgm\",\n\t\t\"Jgmd\",\n\t\t\"Ja\",\n\t\t\"Jb\",\n\t\t\"Jl\",\n\t\t\"Jx\",\n\t\t\"Jbl\",\n\t\t\"Jal\",\n\t\t\"Jab\",\n\t\t\"Jabl\",\n\t\t\"Jax\",\n\t\t\"Jbx\",\n\t\t\"Jlx\",\n\t\t\"Jablx\",\n\t\t\"Jgmdablx\",\n\t};\n\tunsigned arena_ind, i;\n\n\tfor (i = 0; i < 3; i++) {\n\t\tunsigned j;\n\n\t\tswitch (i) {\n\t\tcase 0:\n\t\t\tbreak;\n\t\tcase 1: {\n\t\t\tsize_t sz = sizeof(arena_ind);\n\t\t\tassert_d_eq(mallctl(\"arenas.create\", (void *)&arena_ind,\n\t\t\t    &sz, NULL, 0), 0, \"Unexpected mallctl failure\");\n\t\t\tbreak;\n\t\t} case 2: {\n\t\t\tsize_t mib[3];\n\t\t\tsize_t miblen = sizeof(mib)/sizeof(size_t);\n\t\t\tassert_d_eq(mallctlnametomib(\"arena.0.destroy\",\n\t\t\t    mib, &miblen), 0,\n\t\t\t    \"Unexpected mallctlnametomib failure\");\n\t\t\tmib[1] = arena_ind;\n\t\t\tassert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL,\n\t\t\t    0), 0, \"Unexpected mallctlbymib failure\");\n\t\t\tbreak;\n\t\t} default:\n\t\t\tnot_reached();\n\t\t}\n\n\t\tfor (j = 0; j < sizeof(opts)/sizeof(const char *); j++) {\n\t\t\tparser_t parser;\n\n\t\t\tparser_init(&parser, true);\n\t\t\tmalloc_stats_print(write_cb, (void *)&parser, opts[j]);\n\t\t\tassert_false(parser_parse(&parser),\n\t\t\t    \"Unexpected parse error, opts=\\\"%s\\\"\", opts[j]);\n\t\t\tparser_fini(&parser);\n\t\t}\n\t}\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_json_parser,\n\t    test_stats_print_json);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/ticker.c",
    "content": "#include \"test/jemalloc_test.h\"\n\n#include \"jemalloc/internal/ticker.h\"\n\nTEST_BEGIN(test_ticker_tick) {\n#define NREPS 2\n#define NTICKS 3\n\tticker_t ticker;\n\tint32_t i, j;\n\n\tticker_init(&ticker, NTICKS);\n\tfor (i = 0; i < NREPS; i++) {\n\t\tfor (j = 0; j < NTICKS; j++) {\n\t\t\tassert_u_eq(ticker_read(&ticker), NTICKS - j,\n\t\t\t    \"Unexpected ticker value (i=%d, j=%d)\", i, j);\n\t\t\tassert_false(ticker_tick(&ticker),\n\t\t\t    \"Unexpected ticker fire (i=%d, j=%d)\", i, j);\n\t\t}\n\t\tassert_u32_eq(ticker_read(&ticker), 0,\n\t\t    \"Expected ticker depletion\");\n\t\tassert_true(ticker_tick(&ticker),\n\t\t    \"Expected ticker fire (i=%d)\", i);\n\t\tassert_u32_eq(ticker_read(&ticker), NTICKS,\n\t\t    \"Expected ticker reset\");\n\t}\n#undef NTICKS\n}\nTEST_END\n\nTEST_BEGIN(test_ticker_ticks) {\n#define NTICKS 3\n\tticker_t ticker;\n\n\tticker_init(&ticker, NTICKS);\n\n\tassert_u_eq(ticker_read(&ticker), NTICKS, \"Unexpected ticker value\");\n\tassert_false(ticker_ticks(&ticker, NTICKS), \"Unexpected ticker fire\");\n\tassert_u_eq(ticker_read(&ticker), 0, \"Unexpected ticker value\");\n\tassert_true(ticker_ticks(&ticker, NTICKS), \"Expected ticker fire\");\n\tassert_u_eq(ticker_read(&ticker), NTICKS, \"Unexpected ticker value\");\n\n\tassert_true(ticker_ticks(&ticker, NTICKS + 1), \"Expected ticker fire\");\n\tassert_u_eq(ticker_read(&ticker), NTICKS, \"Unexpected ticker value\");\n#undef NTICKS\n}\nTEST_END\n\nTEST_BEGIN(test_ticker_copy) {\n#define NTICKS 3\n\tticker_t ta, tb;\n\n\tticker_init(&ta, NTICKS);\n\tticker_copy(&tb, &ta);\n\tassert_u_eq(ticker_read(&tb), NTICKS, \"Unexpected ticker value\");\n\tassert_true(ticker_ticks(&tb, NTICKS + 1), \"Expected ticker fire\");\n\tassert_u_eq(ticker_read(&tb), NTICKS, \"Unexpected ticker value\");\n\n\tticker_tick(&ta);\n\tticker_copy(&tb, &ta);\n\tassert_u_eq(ticker_read(&tb), NTICKS - 1, \"Unexpected ticker value\");\n\tassert_true(ticker_ticks(&tb, NTICKS), \"Expected ticker fire\");\n\tassert_u_eq(ticker_read(&tb), NTICKS, \"Unexpected ticker value\");\n#undef NTICKS\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_ticker_tick,\n\t    test_ticker_ticks,\n\t    test_ticker_copy);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/tsd.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic int data_cleanup_count;\n\nvoid\ndata_cleanup(int *data) {\n\tif (data_cleanup_count == 0) {\n\t\tassert_x_eq(*data, MALLOC_TSD_TEST_DATA_INIT,\n\t\t    \"Argument passed into cleanup function should match tsd \"\n\t\t    \"value\");\n\t}\n\t++data_cleanup_count;\n\n\t/*\n\t * Allocate during cleanup for two rounds, in order to assure that\n\t * jemalloc's internal tsd reinitialization happens.\n\t */\n\tbool reincarnate = false;\n\tswitch (*data) {\n\tcase MALLOC_TSD_TEST_DATA_INIT:\n\t\t*data = 1;\n\t\treincarnate = true;\n\t\tbreak;\n\tcase 1:\n\t\t*data = 2;\n\t\treincarnate = true;\n\t\tbreak;\n\tcase 2:\n\t\treturn;\n\tdefault:\n\t\tnot_reached();\n\t}\n\n\tif (reincarnate) {\n\t\tvoid *p = mallocx(1, 0);\n\t\tassert_ptr_not_null(p, \"Unexpeced mallocx() failure\");\n\t\tdallocx(p, 0);\n\t}\n}\n\nstatic void *\nthd_start(void *arg) {\n\tint d = (int)(uintptr_t)arg;\n\tvoid *p;\n\n\ttsd_t *tsd = tsd_fetch();\n\tassert_x_eq(tsd_test_data_get(tsd), MALLOC_TSD_TEST_DATA_INIT,\n\t    \"Initial tsd get should return initialization value\");\n\n\tp = malloc(1);\n\tassert_ptr_not_null(p, \"Unexpected malloc() failure\");\n\n\ttsd_test_data_set(tsd, d);\n\tassert_x_eq(tsd_test_data_get(tsd), d,\n\t    \"After tsd set, tsd get should return value that was set\");\n\n\td = 0;\n\tassert_x_eq(tsd_test_data_get(tsd), (int)(uintptr_t)arg,\n\t    \"Resetting local data should have no effect on tsd\");\n\n\ttsd_test_callback_set(tsd, &data_cleanup);\n\n\tfree(p);\n\treturn NULL;\n}\n\nTEST_BEGIN(test_tsd_main_thread) {\n\tthd_start((void *)(uintptr_t)0xa5f3e329);\n}\nTEST_END\n\nTEST_BEGIN(test_tsd_sub_thread) {\n\tthd_t thd;\n\n\tdata_cleanup_count = 0;\n\tthd_create(&thd, thd_start, (void *)MALLOC_TSD_TEST_DATA_INIT);\n\tthd_join(thd, NULL);\n\t/*\n\t * We reincarnate twice in the data cleanup, so it should execute at\n\t * least 3 times.\n\t */\n\tassert_x_ge(data_cleanup_count, 3,\n\t    \"Cleanup function should have executed multiple times.\");\n}\nTEST_END\n\nstatic void *\nthd_start_reincarnated(void *arg) {\n\ttsd_t *tsd = tsd_fetch();\n\tassert(tsd);\n\n\tvoid *p = malloc(1);\n\tassert_ptr_not_null(p, \"Unexpected malloc() failure\");\n\n\t/* Manually trigger reincarnation. */\n\tassert_ptr_not_null(tsd_arena_get(tsd),\n\t    \"Should have tsd arena set.\");\n\ttsd_cleanup((void *)tsd);\n\tassert_ptr_null(*tsd_arenap_get_unsafe(tsd),\n\t    \"TSD arena should have been cleared.\");\n\tassert_u_eq(tsd->state, tsd_state_purgatory,\n\t    \"TSD state should be purgatory\\n\");\n\n\tfree(p);\n\tassert_u_eq(tsd->state, tsd_state_reincarnated,\n\t    \"TSD state should be reincarnated\\n\");\n\tp = mallocx(1, MALLOCX_TCACHE_NONE);\n\tassert_ptr_not_null(p, \"Unexpected malloc() failure\");\n\tassert_ptr_null(*tsd_arenap_get_unsafe(tsd),\n\t    \"Should not have tsd arena set after reincarnation.\");\n\n\tfree(p);\n\ttsd_cleanup((void *)tsd);\n\tassert_ptr_null(*tsd_arenap_get_unsafe(tsd),\n\t    \"TSD arena should have been cleared after 2nd cleanup.\");\n\n\treturn NULL;\n}\n\nTEST_BEGIN(test_tsd_reincarnation) {\n\tthd_t thd;\n\tthd_create(&thd, thd_start_reincarnated, NULL);\n\tthd_join(thd, NULL);\n}\nTEST_END\n\nint\nmain(void) {\n\t/* Ensure tsd bootstrapped. */\n\tif (nallocx(1, 0) == 0) {\n\t\tmalloc_printf(\"Initialization error\");\n\t\treturn test_status_fail;\n\t}\n\n\treturn test_no_reentrancy(\n\t    test_tsd_main_thread,\n\t    test_tsd_sub_thread,\n\t    test_tsd_reincarnation);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/witness.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic witness_lock_error_t *witness_lock_error_orig;\nstatic witness_owner_error_t *witness_owner_error_orig;\nstatic witness_not_owner_error_t *witness_not_owner_error_orig;\nstatic witness_depth_error_t *witness_depth_error_orig;\n\nstatic bool saw_lock_error;\nstatic bool saw_owner_error;\nstatic bool saw_not_owner_error;\nstatic bool saw_depth_error;\n\nstatic void\nwitness_lock_error_intercept(const witness_list_t *witnesses,\n    const witness_t *witness) {\n\tsaw_lock_error = true;\n}\n\nstatic void\nwitness_owner_error_intercept(const witness_t *witness) {\n\tsaw_owner_error = true;\n}\n\nstatic void\nwitness_not_owner_error_intercept(const witness_t *witness) {\n\tsaw_not_owner_error = true;\n}\n\nstatic void\nwitness_depth_error_intercept(const witness_list_t *witnesses,\n    witness_rank_t rank_inclusive, unsigned depth) {\n\tsaw_depth_error = true;\n}\n\nstatic int\nwitness_comp(const witness_t *a, void *oa, const witness_t *b, void *ob) {\n\tassert_u_eq(a->rank, b->rank, \"Witnesses should have equal rank\");\n\n\tassert(oa == (void *)a);\n\tassert(ob == (void *)b);\n\n\treturn strcmp(a->name, b->name);\n}\n\nstatic int\nwitness_comp_reverse(const witness_t *a, void *oa, const witness_t *b,\n    void *ob) {\n\tassert_u_eq(a->rank, b->rank, \"Witnesses should have equal rank\");\n\n\tassert(oa == (void *)a);\n\tassert(ob == (void *)b);\n\n\treturn -strcmp(a->name, b->name);\n}\n\nTEST_BEGIN(test_witness) {\n\twitness_t a, b;\n\twitness_tsdn_t witness_tsdn = { WITNESS_TSD_INITIALIZER };\n\n\ttest_skip_if(!config_debug);\n\n\twitness_assert_lockless(&witness_tsdn);\n\twitness_assert_depth(&witness_tsdn, 0);\n\twitness_assert_depth_to_rank(&witness_tsdn, (witness_rank_t)1U, 0);\n\n\twitness_init(&a, \"a\", 1, NULL, NULL);\n\twitness_assert_not_owner(&witness_tsdn, &a);\n\twitness_lock(&witness_tsdn, &a);\n\twitness_assert_owner(&witness_tsdn, &a);\n\twitness_assert_depth(&witness_tsdn, 1);\n\twitness_assert_depth_to_rank(&witness_tsdn, (witness_rank_t)1U, 1);\n\twitness_assert_depth_to_rank(&witness_tsdn, (witness_rank_t)2U, 0);\n\n\twitness_init(&b, \"b\", 2, NULL, NULL);\n\twitness_assert_not_owner(&witness_tsdn, &b);\n\twitness_lock(&witness_tsdn, &b);\n\twitness_assert_owner(&witness_tsdn, &b);\n\twitness_assert_depth(&witness_tsdn, 2);\n\twitness_assert_depth_to_rank(&witness_tsdn, (witness_rank_t)1U, 2);\n\twitness_assert_depth_to_rank(&witness_tsdn, (witness_rank_t)2U, 1);\n\twitness_assert_depth_to_rank(&witness_tsdn, (witness_rank_t)3U, 0);\n\n\twitness_unlock(&witness_tsdn, &a);\n\twitness_assert_depth(&witness_tsdn, 1);\n\twitness_assert_depth_to_rank(&witness_tsdn, (witness_rank_t)1U, 1);\n\twitness_assert_depth_to_rank(&witness_tsdn, (witness_rank_t)2U, 1);\n\twitness_assert_depth_to_rank(&witness_tsdn, (witness_rank_t)3U, 0);\n\twitness_unlock(&witness_tsdn, &b);\n\n\twitness_assert_lockless(&witness_tsdn);\n\twitness_assert_depth(&witness_tsdn, 0);\n\twitness_assert_depth_to_rank(&witness_tsdn, (witness_rank_t)1U, 0);\n}\nTEST_END\n\nTEST_BEGIN(test_witness_comp) {\n\twitness_t a, b, c, d;\n\twitness_tsdn_t witness_tsdn = { WITNESS_TSD_INITIALIZER };\n\n\ttest_skip_if(!config_debug);\n\n\twitness_assert_lockless(&witness_tsdn);\n\n\twitness_init(&a, \"a\", 1, witness_comp, &a);\n\twitness_assert_not_owner(&witness_tsdn, &a);\n\twitness_lock(&witness_tsdn, &a);\n\twitness_assert_owner(&witness_tsdn, &a);\n\twitness_assert_depth(&witness_tsdn, 1);\n\n\twitness_init(&b, \"b\", 1, witness_comp, &b);\n\twitness_assert_not_owner(&witness_tsdn, &b);\n\twitness_lock(&witness_tsdn, &b);\n\twitness_assert_owner(&witness_tsdn, &b);\n\twitness_assert_depth(&witness_tsdn, 2);\n\twitness_unlock(&witness_tsdn, &b);\n\twitness_assert_depth(&witness_tsdn, 1);\n\n\twitness_lock_error_orig = witness_lock_error;\n\twitness_lock_error = witness_lock_error_intercept;\n\tsaw_lock_error = false;\n\n\twitness_init(&c, \"c\", 1, witness_comp_reverse, &c);\n\twitness_assert_not_owner(&witness_tsdn, &c);\n\tassert_false(saw_lock_error, \"Unexpected witness lock error\");\n\twitness_lock(&witness_tsdn, &c);\n\tassert_true(saw_lock_error, \"Expected witness lock error\");\n\twitness_unlock(&witness_tsdn, &c);\n\twitness_assert_depth(&witness_tsdn, 1);\n\n\tsaw_lock_error = false;\n\n\twitness_init(&d, \"d\", 1, NULL, NULL);\n\twitness_assert_not_owner(&witness_tsdn, &d);\n\tassert_false(saw_lock_error, \"Unexpected witness lock error\");\n\twitness_lock(&witness_tsdn, &d);\n\tassert_true(saw_lock_error, \"Expected witness lock error\");\n\twitness_unlock(&witness_tsdn, &d);\n\twitness_assert_depth(&witness_tsdn, 1);\n\n\twitness_unlock(&witness_tsdn, &a);\n\n\twitness_assert_lockless(&witness_tsdn);\n\n\twitness_lock_error = witness_lock_error_orig;\n}\nTEST_END\n\nTEST_BEGIN(test_witness_reversal) {\n\twitness_t a, b;\n\twitness_tsdn_t witness_tsdn = { WITNESS_TSD_INITIALIZER };\n\n\ttest_skip_if(!config_debug);\n\n\twitness_lock_error_orig = witness_lock_error;\n\twitness_lock_error = witness_lock_error_intercept;\n\tsaw_lock_error = false;\n\n\twitness_assert_lockless(&witness_tsdn);\n\n\twitness_init(&a, \"a\", 1, NULL, NULL);\n\twitness_init(&b, \"b\", 2, NULL, NULL);\n\n\twitness_lock(&witness_tsdn, &b);\n\twitness_assert_depth(&witness_tsdn, 1);\n\tassert_false(saw_lock_error, \"Unexpected witness lock error\");\n\twitness_lock(&witness_tsdn, &a);\n\tassert_true(saw_lock_error, \"Expected witness lock error\");\n\n\twitness_unlock(&witness_tsdn, &a);\n\twitness_assert_depth(&witness_tsdn, 1);\n\twitness_unlock(&witness_tsdn, &b);\n\n\twitness_assert_lockless(&witness_tsdn);\n\n\twitness_lock_error = witness_lock_error_orig;\n}\nTEST_END\n\nTEST_BEGIN(test_witness_recursive) {\n\twitness_t a;\n\twitness_tsdn_t witness_tsdn = { WITNESS_TSD_INITIALIZER };\n\n\ttest_skip_if(!config_debug);\n\n\twitness_not_owner_error_orig = witness_not_owner_error;\n\twitness_not_owner_error = witness_not_owner_error_intercept;\n\tsaw_not_owner_error = false;\n\n\twitness_lock_error_orig = witness_lock_error;\n\twitness_lock_error = witness_lock_error_intercept;\n\tsaw_lock_error = false;\n\n\twitness_assert_lockless(&witness_tsdn);\n\n\twitness_init(&a, \"a\", 1, NULL, NULL);\n\n\twitness_lock(&witness_tsdn, &a);\n\tassert_false(saw_lock_error, \"Unexpected witness lock error\");\n\tassert_false(saw_not_owner_error, \"Unexpected witness not owner error\");\n\twitness_lock(&witness_tsdn, &a);\n\tassert_true(saw_lock_error, \"Expected witness lock error\");\n\tassert_true(saw_not_owner_error, \"Expected witness not owner error\");\n\n\twitness_unlock(&witness_tsdn, &a);\n\n\twitness_assert_lockless(&witness_tsdn);\n\n\twitness_owner_error = witness_owner_error_orig;\n\twitness_lock_error = witness_lock_error_orig;\n\n}\nTEST_END\n\nTEST_BEGIN(test_witness_unlock_not_owned) {\n\twitness_t a;\n\twitness_tsdn_t witness_tsdn = { WITNESS_TSD_INITIALIZER };\n\n\ttest_skip_if(!config_debug);\n\n\twitness_owner_error_orig = witness_owner_error;\n\twitness_owner_error = witness_owner_error_intercept;\n\tsaw_owner_error = false;\n\n\twitness_assert_lockless(&witness_tsdn);\n\n\twitness_init(&a, \"a\", 1, NULL, NULL);\n\n\tassert_false(saw_owner_error, \"Unexpected owner error\");\n\twitness_unlock(&witness_tsdn, &a);\n\tassert_true(saw_owner_error, \"Expected owner error\");\n\n\twitness_assert_lockless(&witness_tsdn);\n\n\twitness_owner_error = witness_owner_error_orig;\n}\nTEST_END\n\nTEST_BEGIN(test_witness_depth) {\n\twitness_t a;\n\twitness_tsdn_t witness_tsdn = { WITNESS_TSD_INITIALIZER };\n\n\ttest_skip_if(!config_debug);\n\n\twitness_depth_error_orig = witness_depth_error;\n\twitness_depth_error = witness_depth_error_intercept;\n\tsaw_depth_error = false;\n\n\twitness_assert_lockless(&witness_tsdn);\n\twitness_assert_depth(&witness_tsdn, 0);\n\n\twitness_init(&a, \"a\", 1, NULL, NULL);\n\n\tassert_false(saw_depth_error, \"Unexpected depth error\");\n\twitness_assert_lockless(&witness_tsdn);\n\twitness_assert_depth(&witness_tsdn, 0);\n\n\twitness_lock(&witness_tsdn, &a);\n\twitness_assert_lockless(&witness_tsdn);\n\twitness_assert_depth(&witness_tsdn, 0);\n\tassert_true(saw_depth_error, \"Expected depth error\");\n\n\twitness_unlock(&witness_tsdn, &a);\n\n\twitness_assert_lockless(&witness_tsdn);\n\twitness_assert_depth(&witness_tsdn, 0);\n\n\twitness_depth_error = witness_depth_error_orig;\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_witness,\n\t    test_witness_comp,\n\t    test_witness_reversal,\n\t    test_witness_recursive,\n\t    test_witness_unlock_not_owned,\n\t    test_witness_depth);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/zero.c",
    "content": "#include \"test/jemalloc_test.h\"\n\nstatic void\ntest_zero(size_t sz_min, size_t sz_max) {\n\tuint8_t *s;\n\tsize_t sz_prev, sz, i;\n#define MAGIC\t((uint8_t)0x61)\n\n\tsz_prev = 0;\n\ts = (uint8_t *)mallocx(sz_min, 0);\n\tassert_ptr_not_null((void *)s, \"Unexpected mallocx() failure\");\n\n\tfor (sz = sallocx(s, 0); sz <= sz_max;\n\t    sz_prev = sz, sz = sallocx(s, 0)) {\n\t\tif (sz_prev > 0) {\n\t\t\tassert_u_eq(s[0], MAGIC,\n\t\t\t    \"Previously allocated byte %zu/%zu is corrupted\",\n\t\t\t    ZU(0), sz_prev);\n\t\t\tassert_u_eq(s[sz_prev-1], MAGIC,\n\t\t\t    \"Previously allocated byte %zu/%zu is corrupted\",\n\t\t\t    sz_prev-1, sz_prev);\n\t\t}\n\n\t\tfor (i = sz_prev; i < sz; i++) {\n\t\t\tassert_u_eq(s[i], 0x0,\n\t\t\t    \"Newly allocated byte %zu/%zu isn't zero-filled\",\n\t\t\t    i, sz);\n\t\t\ts[i] = MAGIC;\n\t\t}\n\n\t\tif (xallocx(s, sz+1, 0, 0) == sz) {\n\t\t\ts = (uint8_t *)rallocx(s, sz+1, 0);\n\t\t\tassert_ptr_not_null((void *)s,\n\t\t\t    \"Unexpected rallocx() failure\");\n\t\t}\n\t}\n\n\tdallocx(s, 0);\n#undef MAGIC\n}\n\nTEST_BEGIN(test_zero_small) {\n\ttest_skip_if(!config_fill);\n\ttest_zero(1, SMALL_MAXCLASS-1);\n}\nTEST_END\n\nTEST_BEGIN(test_zero_large) {\n\ttest_skip_if(!config_fill);\n\ttest_zero(SMALL_MAXCLASS+1, (1U << (LG_LARGE_MINCLASS+1)));\n}\nTEST_END\n\nint\nmain(void) {\n\treturn test(\n\t    test_zero_small,\n\t    test_zero_large);\n}\n"
  },
  {
    "path": "deps/memkind/src/jemalloc/test/unit/zero.sh",
    "content": "#!/bin/sh\n\nif [ \"x${enable_fill}\" = \"x1\" ] ; then\n  export MALLOC_CONF=\"abort:false,junk:false,zero:true\"\nfi\n"
  },
  {
    "path": "deps/memkind/src/m4/ax_cxx_compile_stdcxx.m4",
    "content": "# ===========================================================================\n#   http://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx.html\n# ===========================================================================\n#\n# SYNOPSIS\n#\n#   AX_CXX_COMPILE_STDCXX(VERSION, [ext|noext], [mandatory|optional])\n#\n# DESCRIPTION\n#\n#   Check for baseline language coverage in the compiler for the specified\n#   version of the C++ standard.  If necessary, add switches to CXX and\n#   CXXCPP to enable support.  VERSION may be '11' (for the C++11 standard)\n#   or '14' (for the C++14 standard).\n#\n#   The second argument, if specified, indicates whether you insist on an\n#   extended mode (e.g. -std=gnu++11) or a strict conformance mode (e.g.\n#   -std=c++11).  If neither is specified, you get whatever works, with\n#   preference for an extended mode.\n#\n#   The third argument, if specified 'mandatory' or if left unspecified,\n#   indicates that baseline support for the specified C++ standard is\n#   required and that the macro should error out if no mode with that\n#   support is found.  If specified 'optional', then configuration proceeds\n#   regardless, after defining HAVE_CXX${VERSION} if and only if a\n#   supporting mode is found.\n#\n# LICENSE\n#\n#   Copyright (c) 2008 Benjamin Kosnik <bkoz@redhat.com>\n#   Copyright (c) 2012 Zack Weinberg <zackw@panix.com>\n#   Copyright (c) 2013 Roy Stogner <roystgnr@ices.utexas.edu>\n#   Copyright (c) 2014, 2015 Google Inc.; contributed by Alexey Sokolov <sokolov@google.com>\n#   Copyright (c) 2015 Paul Norman <penorman@mac.com>\n#   Copyright (c) 2015 Moritz Klammler <moritz@klammler.eu>\n#\n#   Copying and distribution of this file, with or without modification, are\n#   permitted in any medium without royalty provided the copyright notice\n#   and this notice are preserved.  This file is offered as-is, without any\n#   warranty.\n\n#serial 4\n\ndnl  This macro is based on the code from the AX_CXX_COMPILE_STDCXX_11 macro\ndnl  (serial version number 13).\n\nAC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl\n  m4_if([$1], [11], [],\n        [$1], [14], [],\n        [$1], [17], [m4_fatal([support for C++17 not yet implemented in AX_CXX_COMPILE_STDCXX])],\n        [m4_fatal([invalid first argument `$1' to AX_CXX_COMPILE_STDCXX])])dnl\n  m4_if([$2], [], [],\n        [$2], [ext], [],\n        [$2], [noext], [],\n        [m4_fatal([invalid second argument `$2' to AX_CXX_COMPILE_STDCXX])])dnl\n  m4_if([$3], [], [ax_cxx_compile_cxx$1_required=true],\n        [$3], [mandatory], [ax_cxx_compile_cxx$1_required=true],\n        [$3], [optional], [ax_cxx_compile_cxx$1_required=false],\n        [m4_fatal([invalid third argument `$3' to AX_CXX_COMPILE_STDCXX])])\n  AC_LANG_PUSH([C++])dnl\n  ac_success=no\n  AC_CACHE_CHECK(whether $CXX supports C++$1 features by default,\n  ax_cv_cxx_compile_cxx$1,\n  [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],\n    [ax_cv_cxx_compile_cxx$1=yes],\n    [ax_cv_cxx_compile_cxx$1=no])])\n  if test x$ax_cv_cxx_compile_cxx$1 = xyes; then\n    ac_success=yes\n  fi\n\n  m4_if([$2], [noext], [], [dnl\n  if test x$ac_success = xno; then\n    for switch in -std=gnu++$1 -std=gnu++0x; do\n      cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])\n      AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,\n                     $cachevar,\n        [ac_save_CXX=\"$CXX\"\n         CXX=\"$CXX $switch\"\n         AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],\n          [eval $cachevar=yes],\n          [eval $cachevar=no])\n         CXX=\"$ac_save_CXX\"])\n      if eval test x\\$$cachevar = xyes; then\n        CXX=\"$CXX $switch\"\n        if test -n \"$CXXCPP\" ; then\n          CXXCPP=\"$CXXCPP $switch\"\n        fi\n        ac_success=yes\n        break\n      fi\n    done\n  fi])\n\n  m4_if([$2], [ext], [], [dnl\n  if test x$ac_success = xno; then\n    dnl HP's aCC needs +std=c++11 according to:\n    dnl http://h21007.www2.hp.com/portal/download/files/unprot/aCxx/PDF_Release_Notes/769149-001.pdf\n    dnl Cray's crayCC needs \"-h std=c++11\"\n    for switch in -std=c++$1 -std=c++0x +std=c++$1 \"-h std=c++$1\"; do\n      cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])\n      AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,\n                     $cachevar,\n        [ac_save_CXX=\"$CXX\"\n         CXX=\"$CXX $switch\"\n         AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],\n          [eval $cachevar=yes],\n          [eval $cachevar=no])\n         CXX=\"$ac_save_CXX\"])\n      if eval test x\\$$cachevar = xyes; then\n        CXX=\"$CXX $switch\"\n        if test -n \"$CXXCPP\" ; then\n          CXXCPP=\"$CXXCPP $switch\"\n        fi\n        ac_success=yes\n        break\n      fi\n    done\n  fi])\n  AC_LANG_POP([C++])\n  if test x$ax_cxx_compile_cxx$1_required = xtrue; then\n    if test x$ac_success = xno; then\n      AC_MSG_ERROR([*** A compiler with support for C++$1 language features is required.])\n    fi\n  fi\n  if test x$ac_success = xno; then\n    HAVE_CXX$1=0\n    AC_MSG_NOTICE([No compiler with C++$1 support was found])\n  else\n    HAVE_CXX$1=1\n    AC_DEFINE(HAVE_CXX$1,1,\n              [define if the compiler supports basic C++$1 syntax])\n  fi\n  AC_SUBST(HAVE_CXX$1)\n])\n\n\ndnl  Test body for checking C++11 support\n\nm4_define([_AX_CXX_COMPILE_STDCXX_testbody_11],\n  _AX_CXX_COMPILE_STDCXX_testbody_new_in_11\n)\n\n\ndnl  Test body for checking C++14 support\n\nm4_define([_AX_CXX_COMPILE_STDCXX_testbody_14],\n  _AX_CXX_COMPILE_STDCXX_testbody_new_in_11\n  _AX_CXX_COMPILE_STDCXX_testbody_new_in_14\n)\n\n\ndnl  Tests for new features in C++11\n\nm4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_11], [[\n\n// If the compiler admits that it is not ready for C++11, why torture it?\n// Hopefully, this will speed up the test.\n\n#ifndef __cplusplus\n\n#error \"This is not a C++ compiler\"\n\n#elif __cplusplus < 201103L\n\n#error \"This is not a C++11 compiler\"\n\n#else\n\nnamespace cxx11\n{\n\n  namespace test_static_assert\n  {\n\n    template <typename T>\n    struct check\n    {\n      static_assert(sizeof(int) <= sizeof(T), \"not big enough\");\n    };\n\n  }\n\n  namespace test_final_override\n  {\n\n    struct Base\n    {\n      virtual void f() {}\n    };\n\n    struct Derived : public Base\n    {\n      virtual void f() override {}\n    };\n\n  }\n\n  namespace test_double_right_angle_brackets\n  {\n\n    template < typename T >\n    struct check {};\n\n    typedef check<void> single_type;\n    typedef check<check<void>> double_type;\n    typedef check<check<check<void>>> triple_type;\n    typedef check<check<check<check<void>>>> quadruple_type;\n\n  }\n\n  namespace test_decltype\n  {\n\n    int\n    f()\n    {\n      int a = 1;\n      decltype(a) b = 2;\n      return a + b;\n    }\n\n  }\n\n  namespace test_type_deduction\n  {\n\n    template < typename T1, typename T2 >\n    struct is_same\n    {\n      static const bool value = false;\n    };\n\n    template < typename T >\n    struct is_same<T, T>\n    {\n      static const bool value = true;\n    };\n\n    template < typename T1, typename T2 >\n    auto\n    add(T1 a1, T2 a2) -> decltype(a1 + a2)\n    {\n      return a1 + a2;\n    }\n\n    int\n    test(const int c, volatile int v)\n    {\n      static_assert(is_same<int, decltype(0)>::value == true, \"\");\n      static_assert(is_same<int, decltype(c)>::value == false, \"\");\n      static_assert(is_same<int, decltype(v)>::value == false, \"\");\n      auto ac = c;\n      auto av = v;\n      auto sumi = ac + av + 'x';\n      auto sumf = ac + av + 1.0;\n      static_assert(is_same<int, decltype(ac)>::value == true, \"\");\n      static_assert(is_same<int, decltype(av)>::value == true, \"\");\n      static_assert(is_same<int, decltype(sumi)>::value == true, \"\");\n      static_assert(is_same<int, decltype(sumf)>::value == false, \"\");\n      static_assert(is_same<int, decltype(add(c, v))>::value == true, \"\");\n      return (sumf > 0.0) ? sumi : add(c, v);\n    }\n\n  }\n\n  namespace test_noexcept\n  {\n\n    int f() { return 0; }\n    int g() noexcept { return 0; }\n\n    static_assert(noexcept(f()) == false, \"\");\n    static_assert(noexcept(g()) == true, \"\");\n\n  }\n\n  namespace test_constexpr\n  {\n\n    template < typename CharT >\n    unsigned long constexpr\n    strlen_c_r(const CharT *const s, const unsigned long acc) noexcept\n    {\n      return *s ? strlen_c_r(s + 1, acc + 1) : acc;\n    }\n\n    template < typename CharT >\n    unsigned long constexpr\n    strlen_c(const CharT *const s) noexcept\n    {\n      return strlen_c_r(s, 0UL);\n    }\n\n    static_assert(strlen_c(\"\") == 0UL, \"\");\n    static_assert(strlen_c(\"1\") == 1UL, \"\");\n    static_assert(strlen_c(\"example\") == 7UL, \"\");\n    static_assert(strlen_c(\"another\\0example\") == 7UL, \"\");\n\n  }\n\n  namespace test_rvalue_references\n  {\n\n    template < int N >\n    struct answer\n    {\n      static constexpr int value = N;\n    };\n\n    answer<1> f(int&)       { return answer<1>(); }\n    answer<2> f(const int&) { return answer<2>(); }\n    answer<3> f(int&&)      { return answer<3>(); }\n\n    void\n    test()\n    {\n      int i = 0;\n      const int c = 0;\n      static_assert(decltype(f(i))::value == 1, \"\");\n      static_assert(decltype(f(c))::value == 2, \"\");\n      static_assert(decltype(f(0))::value == 3, \"\");\n    }\n\n  }\n\n  namespace test_uniform_initialization\n  {\n\n    struct test\n    {\n      static const int zero {};\n      static const int one {1};\n    };\n\n    static_assert(test::zero == 0, \"\");\n    static_assert(test::one == 1, \"\");\n\n  }\n\n  namespace test_lambdas\n  {\n\n    void\n    test1()\n    {\n      auto lambda1 = [](){};\n      auto lambda2 = lambda1;\n      lambda1();\n      lambda2();\n    }\n\n    int\n    test2()\n    {\n      auto a = [](int i, int j){ return i + j; }(1, 2);\n      auto b = []() -> int { return '0'; }();\n      auto c = [=](){ return a + b; }();\n      auto d = [&](){ return c; }();\n      auto e = [a, &b](int x) mutable {\n        const auto identity = [](int y){ return y; };\n        for (auto i = 0; i < a; ++i)\n          a += b--;\n        return x + identity(a + b);\n      }(0);\n      return a + b + c + d + e;\n    }\n\n    int\n    test3()\n    {\n      const auto nullary = [](){ return 0; };\n      const auto unary = [](int x){ return x; };\n      using nullary_t = decltype(nullary);\n      using unary_t = decltype(unary);\n      const auto higher1st = [](nullary_t f){ return f(); };\n      const auto higher2nd = [unary](nullary_t f1){\n        return [unary, f1](unary_t f2){ return f2(unary(f1())); };\n      };\n      return higher1st(nullary) + higher2nd(nullary)(unary);\n    }\n\n  }\n\n  namespace test_variadic_templates\n  {\n\n    template <int...>\n    struct sum;\n\n    template <int N0, int... N1toN>\n    struct sum<N0, N1toN...>\n    {\n      static constexpr auto value = N0 + sum<N1toN...>::value;\n    };\n\n    template <>\n    struct sum<>\n    {\n      static constexpr auto value = 0;\n    };\n\n    static_assert(sum<>::value == 0, \"\");\n    static_assert(sum<1>::value == 1, \"\");\n    static_assert(sum<23>::value == 23, \"\");\n    static_assert(sum<1, 2>::value == 3, \"\");\n    static_assert(sum<5, 5, 11>::value == 21, \"\");\n    static_assert(sum<2, 3, 5, 7, 11, 13>::value == 41, \"\");\n\n  }\n\n  // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae\n  // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function\n  // because of this.\n  namespace test_template_alias_sfinae\n  {\n\n    struct foo {};\n\n    template<typename T>\n    using member = typename T::member_type;\n\n    template<typename T>\n    void func(...) {}\n\n    template<typename T>\n    void func(member<T>*) {}\n\n    void test();\n\n    void test() { func<foo>(0); }\n\n  }\n\n}  // namespace cxx11\n\n#endif  // __cplusplus >= 201103L\n\n]])\n\n\ndnl  Tests for new features in C++14\n\nm4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_14], [[\n\n// If the compiler admits that it is not ready for C++14, why torture it?\n// Hopefully, this will speed up the test.\n\n#ifndef __cplusplus\n\n#error \"This is not a C++ compiler\"\n\n#elif __cplusplus < 201402L\n\n#error \"This is not a C++14 compiler\"\n\n#else\n\nnamespace cxx14\n{\n\n  namespace test_polymorphic_lambdas\n  {\n\n    int\n    test()\n    {\n      const auto lambda = [](auto&&... args){\n        const auto istiny = [](auto x){\n          return (sizeof(x) == 1UL) ? 1 : 0;\n        };\n        const int aretiny[] = { istiny(args)... };\n        return aretiny[0];\n      };\n      return lambda(1, 1L, 1.0f, '1');\n    }\n\n  }\n\n  namespace test_binary_literals\n  {\n\n    constexpr auto ivii = 0b0000000000101010;\n    static_assert(ivii == 42, \"wrong value\");\n\n  }\n\n  namespace test_generalized_constexpr\n  {\n\n    template < typename CharT >\n    constexpr unsigned long\n    strlen_c(const CharT *const s) noexcept\n    {\n      auto length = 0UL;\n      for (auto p = s; *p; ++p)\n        ++length;\n      return length;\n    }\n\n    static_assert(strlen_c(\"\") == 0UL, \"\");\n    static_assert(strlen_c(\"x\") == 1UL, \"\");\n    static_assert(strlen_c(\"test\") == 4UL, \"\");\n    static_assert(strlen_c(\"another\\0test\") == 7UL, \"\");\n\n  }\n\n  namespace test_lambda_init_capture\n  {\n\n    int\n    test()\n    {\n      auto x = 0;\n      const auto lambda1 = [a = x](int b){ return a + b; };\n      const auto lambda2 = [a = lambda1(x)](){ return a; };\n      return lambda2();\n    }\n\n  }\n\n  namespace test_digit_seperators\n  {\n\n    constexpr auto ten_million = 100'000'000;\n    static_assert(ten_million == 100000000, \"\");\n\n  }\n\n  namespace test_return_type_deduction\n  {\n\n    auto f(int& x) { return x; }\n    decltype(auto) g(int& x) { return x; }\n\n    template < typename T1, typename T2 >\n    struct is_same\n    {\n      static constexpr auto value = false;\n    };\n\n    template < typename T >\n    struct is_same<T, T>\n    {\n      static constexpr auto value = true;\n    };\n\n    int\n    test()\n    {\n      auto x = 0;\n      static_assert(is_same<int, decltype(f(x))>::value, \"\");\n      static_assert(is_same<int&, decltype(g(x))>::value, \"\");\n      return x;\n    }\n\n  }\n\n}  // namespace cxx14\n\n#endif  // __cplusplus >= 201402L\n\n]])\n"
  },
  {
    "path": "deps/memkind/src/m4/ax_cxx_compile_stdcxx_11.m4",
    "content": "# ============================================================================\n#  http://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx_11.html\n# ============================================================================\n#\n# SYNOPSIS\n#\n#   AX_CXX_COMPILE_STDCXX_11([ext|noext], [mandatory|optional])\n#\n# DESCRIPTION\n#\n#   Check for baseline language coverage in the compiler for the C++11\n#   standard; if necessary, add switches to CXX and CXXCPP to enable\n#   support.\n#\n#   This macro is a convenience alias for calling the AX_CXX_COMPILE_STDCXX\n#   macro with the version set to C++11.  The two optional arguments are\n#   forwarded literally as the second and third argument respectively.\n#   Please see the documentation for the AX_CXX_COMPILE_STDCXX macro for\n#   more information.  If you want to use this macro, you also need to\n#   download the ax_cxx_compile_stdcxx.m4 file.\n#\n# LICENSE\n#\n#   Copyright (c) 2008 Benjamin Kosnik <bkoz@redhat.com>\n#   Copyright (c) 2012 Zack Weinberg <zackw@panix.com>\n#   Copyright (c) 2013 Roy Stogner <roystgnr@ices.utexas.edu>\n#   Copyright (c) 2014, 2015 Google Inc.; contributed by Alexey Sokolov <sokolov@google.com>\n#   Copyright (c) 2015 Paul Norman <penorman@mac.com>\n#   Copyright (c) 2015 Moritz Klammler <moritz@klammler.eu>\n#\n#   Copying and distribution of this file, with or without modification, are\n#   permitted in any medium without royalty provided the copyright notice\n#   and this notice are preserved. This file is offered as-is, without any\n#   warranty.\n\n#serial 17\n\nAX_REQUIRE_DEFINED([AX_CXX_COMPILE_STDCXX])\nAC_DEFUN([AX_CXX_COMPILE_STDCXX_11], [AX_CXX_COMPILE_STDCXX([11], [$1], [$2])])\n"
  },
  {
    "path": "deps/memkind/src/m4/ax_pthread.m4",
    "content": "# ===========================================================================\n#        https://www.gnu.org/software/autoconf-archive/ax_pthread.html\n# ===========================================================================\n#\n# SYNOPSIS\n#\n#   AX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])\n#\n# DESCRIPTION\n#\n#   This macro figures out how to build C programs using POSIX threads. It\n#   sets the PTHREAD_LIBS output variable to the threads library and linker\n#   flags, and the PTHREAD_CFLAGS output variable to any special C compiler\n#   flags that are needed. (The user can also force certain compiler\n#   flags/libs to be tested by setting these environment variables.)\n#\n#   Also sets PTHREAD_CC to any special C compiler that is needed for\n#   multi-threaded programs (defaults to the value of CC otherwise). (This\n#   is necessary on AIX to use the special cc_r compiler alias.)\n#\n#   NOTE: You are assumed to not only compile your program with these flags,\n#   but also to link with them as well. For example, you might link with\n#   $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS $LIBS\n#\n#   If you are only building threaded programs, you may wish to use these\n#   variables in your default LIBS, CFLAGS, and CC:\n#\n#     LIBS=\"$PTHREAD_LIBS $LIBS\"\n#     CFLAGS=\"$CFLAGS $PTHREAD_CFLAGS\"\n#     CC=\"$PTHREAD_CC\"\n#\n#   In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute constant\n#   has a nonstandard name, this macro defines PTHREAD_CREATE_JOINABLE to\n#   that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).\n#\n#   Also HAVE_PTHREAD_PRIO_INHERIT is defined if pthread is found and the\n#   PTHREAD_PRIO_INHERIT symbol is defined when compiling with\n#   PTHREAD_CFLAGS.\n#\n#   ACTION-IF-FOUND is a list of shell commands to run if a threads library\n#   is found, and ACTION-IF-NOT-FOUND is a list of commands to run it if it\n#   is not found. If ACTION-IF-FOUND is not specified, the default action\n#   will define HAVE_PTHREAD.\n#\n#   Please let the authors know if this macro fails on any platform, or if\n#   you have any other suggestions or comments. This macro was based on work\n#   by SGJ on autoconf scripts for FFTW (http://www.fftw.org/) (with help\n#   from M. Frigo), as well as ac_pthread and hb_pthread macros posted by\n#   Alejandro Forero Cuervo to the autoconf macro repository. We are also\n#   grateful for the helpful feedback of numerous users.\n#\n#   Updated for Autoconf 2.68 by Daniel Richard G.\n#\n# LICENSE\n#\n#   Copyright (c) 2008 Steven G. Johnson <stevenj@alum.mit.edu>\n#   Copyright (c) 2011 Daniel Richard G. <skunk@iSKUNK.ORG>\n#\n#   This program is free software: you can redistribute it and/or modify it\n#   under the terms of the GNU General Public License as published by the\n#   Free Software Foundation, either version 3 of the License, or (at your\n#   option) any later version.\n#\n#   This program is distributed in the hope that it will be useful, but\n#   WITHOUT ANY WARRANTY; without even the implied warranty of\n#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General\n#   Public License for more details.\n#\n#   You should have received a copy of the GNU General Public License along\n#   with this program. If not, see <https://www.gnu.org/licenses/>.\n#\n#   As a special exception, the respective Autoconf Macro's copyright owner\n#   gives unlimited permission to copy, distribute and modify the configure\n#   scripts that are the output of Autoconf when processing the Macro. You\n#   need not follow the terms of the GNU General Public License when using\n#   or distributing such scripts, even though portions of the text of the\n#   Macro appear in them. The GNU General Public License (GPL) does govern\n#   all other use of the material that constitutes the Autoconf Macro.\n#\n#   This special exception to the GPL applies to versions of the Autoconf\n#   Macro released by the Autoconf Archive. When you make and distribute a\n#   modified version of the Autoconf Macro, you may extend this special\n#   exception to the GPL to apply to your modified version as well.\n\n#serial 24\n\nAU_ALIAS([ACX_PTHREAD], [AX_PTHREAD])\nAC_DEFUN([AX_PTHREAD], [\nAC_REQUIRE([AC_CANONICAL_HOST])\nAC_REQUIRE([AC_PROG_CC])\nAC_REQUIRE([AC_PROG_SED])\nAC_LANG_PUSH([C])\nax_pthread_ok=no\n\n# We used to check for pthread.h first, but this fails if pthread.h\n# requires special compiler flags (e.g. on Tru64 or Sequent).\n# It gets checked for in the link test anyway.\n\n# First of all, check if the user has set any of the PTHREAD_LIBS,\n# etcetera environment variables, and if threads linking works using\n# them:\nif test \"x$PTHREAD_CFLAGS$PTHREAD_LIBS\" != \"x\"; then\n        ax_pthread_save_CC=\"$CC\"\n        ax_pthread_save_CFLAGS=\"$CFLAGS\"\n        ax_pthread_save_LIBS=\"$LIBS\"\n        AS_IF([test \"x$PTHREAD_CC\" != \"x\"], [CC=\"$PTHREAD_CC\"])\n        CFLAGS=\"$CFLAGS $PTHREAD_CFLAGS\"\n        LIBS=\"$PTHREAD_LIBS $LIBS\"\n        AC_MSG_CHECKING([for pthread_join using $CC $PTHREAD_CFLAGS $PTHREAD_LIBS])\n        AC_LINK_IFELSE([AC_LANG_CALL([], [pthread_join])], [ax_pthread_ok=yes])\n        AC_MSG_RESULT([$ax_pthread_ok])\n        if test \"x$ax_pthread_ok\" = \"xno\"; then\n                PTHREAD_LIBS=\"\"\n                PTHREAD_CFLAGS=\"\"\n        fi\n        CC=\"$ax_pthread_save_CC\"\n        CFLAGS=\"$ax_pthread_save_CFLAGS\"\n        LIBS=\"$ax_pthread_save_LIBS\"\nfi\n\n# We must check for the threads library under a number of different\n# names; the ordering is very important because some systems\n# (e.g. DEC) have both -lpthread and -lpthreads, where one of the\n# libraries is broken (non-POSIX).\n\n# Create a list of thread flags to try.  Items starting with a \"-\" are\n# C compiler flags, and other items are library names, except for \"none\"\n# which indicates that we try without any flags at all, and \"pthread-config\"\n# which is a program returning the flags for the Pth emulation library.\n\nax_pthread_flags=\"pthreads none -Kthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config\"\n\n# The ordering *is* (sometimes) important.  Some notes on the\n# individual items follow:\n\n# pthreads: AIX (must check this before -lpthread)\n# none: in case threads are in libc; should be tried before -Kthread and\n#       other compiler flags to prevent continual compiler warnings\n# -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)\n# -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads), Tru64\n#           (Note: HP C rejects this with \"bad form for `-t' option\")\n# -pthreads: Solaris/gcc (Note: HP C also rejects)\n# -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it\n#      doesn't hurt to check since this sometimes defines pthreads and\n#      -D_REENTRANT too), HP C (must be checked before -lpthread, which\n#      is present but should not be used directly; and before -mthreads,\n#      because the compiler interprets this as \"-mt\" + \"-hreads\")\n# -mthreads: Mingw32/gcc, Lynx/gcc\n# pthread: Linux, etcetera\n# --thread-safe: KAI C++\n# pthread-config: use pthread-config program (for GNU Pth library)\n\ncase $host_os in\n\n        freebsd*)\n\n        # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)\n        # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)\n\n        ax_pthread_flags=\"-kthread lthread $ax_pthread_flags\"\n        ;;\n\n        hpux*)\n\n        # From the cc(1) man page: \"[-mt] Sets various -D flags to enable\n        # multi-threading and also sets -lpthread.\"\n\n        ax_pthread_flags=\"-mt -pthread pthread $ax_pthread_flags\"\n        ;;\n\n        openedition*)\n\n        # IBM z/OS requires a feature-test macro to be defined in order to\n        # enable POSIX threads at all, so give the user a hint if this is\n        # not set. (We don't define these ourselves, as they can affect\n        # other portions of the system API in unpredictable ways.)\n\n        AC_EGREP_CPP([AX_PTHREAD_ZOS_MISSING],\n            [\n#            if !defined(_OPEN_THREADS) && !defined(_UNIX03_THREADS)\n             AX_PTHREAD_ZOS_MISSING\n#            endif\n            ],\n            [AC_MSG_WARN([IBM z/OS requires -D_OPEN_THREADS or -D_UNIX03_THREADS to enable pthreads support.])])\n        ;;\n\n        solaris*)\n\n        # On Solaris (at least, for some versions), libc contains stubbed\n        # (non-functional) versions of the pthreads routines, so link-based\n        # tests will erroneously succeed. (N.B.: The stubs are missing\n        # pthread_cleanup_push, or rather a function called by this macro,\n        # so we could check for that, but who knows whether they'll stub\n        # that too in a future libc.)  So we'll check first for the\n        # standard Solaris way of linking pthreads (-mt -lpthread).\n\n        ax_pthread_flags=\"-mt,pthread pthread $ax_pthread_flags\"\n        ;;\nesac\n\n# GCC generally uses -pthread, or -pthreads on some platforms (e.g. SPARC)\n\nAS_IF([test \"x$GCC\" = \"xyes\"],\n      [ax_pthread_flags=\"-pthread -pthreads $ax_pthread_flags\"])\n\n# The presence of a feature test macro requesting re-entrant function\n# definitions is, on some systems, a strong hint that pthreads support is\n# correctly enabled\n\ncase $host_os in\n        darwin* | hpux* | linux* | osf* | solaris*)\n        ax_pthread_check_macro=\"_REENTRANT\"\n        ;;\n\n        aix*)\n        ax_pthread_check_macro=\"_THREAD_SAFE\"\n        ;;\n\n        *)\n        ax_pthread_check_macro=\"--\"\n        ;;\nesac\nAS_IF([test \"x$ax_pthread_check_macro\" = \"x--\"],\n      [ax_pthread_check_cond=0],\n      [ax_pthread_check_cond=\"!defined($ax_pthread_check_macro)\"])\n\n# Are we compiling with Clang?\n\nAC_CACHE_CHECK([whether $CC is Clang],\n    [ax_cv_PTHREAD_CLANG],\n    [ax_cv_PTHREAD_CLANG=no\n     # Note that Autoconf sets GCC=yes for Clang as well as GCC\n     if test \"x$GCC\" = \"xyes\"; then\n        AC_EGREP_CPP([AX_PTHREAD_CC_IS_CLANG],\n            [/* Note: Clang 2.7 lacks __clang_[a-z]+__ */\n#            if defined(__clang__) && defined(__llvm__)\n             AX_PTHREAD_CC_IS_CLANG\n#            endif\n            ],\n            [ax_cv_PTHREAD_CLANG=yes])\n     fi\n    ])\nax_pthread_clang=\"$ax_cv_PTHREAD_CLANG\"\n\nax_pthread_clang_warning=no\n\n# Clang needs special handling, because older versions handle the -pthread\n# option in a rather... idiosyncratic way\n\nif test \"x$ax_pthread_clang\" = \"xyes\"; then\n\n        # Clang takes -pthread; it has never supported any other flag\n\n        # (Note 1: This will need to be revisited if a system that Clang\n        # supports has POSIX threads in a separate library.  This tends not\n        # to be the way of modern systems, but it's conceivable.)\n\n        # (Note 2: On some systems, notably Darwin, -pthread is not needed\n        # to get POSIX threads support; the API is always present and\n        # active.  We could reasonably leave PTHREAD_CFLAGS empty.  But\n        # -pthread does define _REENTRANT, and while the Darwin headers\n        # ignore this macro, third-party headers might not.)\n\n        PTHREAD_CFLAGS=\"-pthread\"\n        PTHREAD_LIBS=\n\n        ax_pthread_ok=yes\n\n        # However, older versions of Clang make a point of warning the user\n        # that, in an invocation where only linking and no compilation is\n        # taking place, the -pthread option has no effect (\"argument unused\n        # during compilation\").  They expect -pthread to be passed in only\n        # when source code is being compiled.\n        #\n        # Problem is, this is at odds with the way Automake and most other\n        # C build frameworks function, which is that the same flags used in\n        # compilation (CFLAGS) are also used in linking.  Many systems\n        # supported by AX_PTHREAD require exactly this for POSIX threads\n        # support, and in fact it is often not straightforward to specify a\n        # flag that is used only in the compilation phase and not in\n        # linking.  Such a scenario is extremely rare in practice.\n        #\n        # Even though use of the -pthread flag in linking would only print\n        # a warning, this can be a nuisance for well-run software projects\n        # that build with -Werror.  So if the active version of Clang has\n        # this misfeature, we search for an option to squash it.\n\n        AC_CACHE_CHECK([whether Clang needs flag to prevent \"argument unused\" warning when linking with -pthread],\n            [ax_cv_PTHREAD_CLANG_NO_WARN_FLAG],\n            [ax_cv_PTHREAD_CLANG_NO_WARN_FLAG=unknown\n             # Create an alternate version of $ac_link that compiles and\n             # links in two steps (.c -> .o, .o -> exe) instead of one\n             # (.c -> exe), because the warning occurs only in the second\n             # step\n             ax_pthread_save_ac_link=\"$ac_link\"\n             ax_pthread_sed='s/conftest\\.\\$ac_ext/conftest.$ac_objext/g'\n             ax_pthread_link_step=`$as_echo \"$ac_link\" | sed \"$ax_pthread_sed\"`\n             ax_pthread_2step_ac_link=\"($ac_compile) && (echo ==== >&5) && ($ax_pthread_link_step)\"\n             ax_pthread_save_CFLAGS=\"$CFLAGS\"\n             for ax_pthread_try in '' -Qunused-arguments -Wno-unused-command-line-argument unknown; do\n                AS_IF([test \"x$ax_pthread_try\" = \"xunknown\"], [break])\n                CFLAGS=\"-Werror -Wunknown-warning-option $ax_pthread_try -pthread $ax_pthread_save_CFLAGS\"\n                ac_link=\"$ax_pthread_save_ac_link\"\n                AC_LINK_IFELSE([AC_LANG_SOURCE([[int main(void){return 0;}]])],\n                    [ac_link=\"$ax_pthread_2step_ac_link\"\n                     AC_LINK_IFELSE([AC_LANG_SOURCE([[int main(void){return 0;}]])],\n                         [break])\n                    ])\n             done\n             ac_link=\"$ax_pthread_save_ac_link\"\n             CFLAGS=\"$ax_pthread_save_CFLAGS\"\n             AS_IF([test \"x$ax_pthread_try\" = \"x\"], [ax_pthread_try=no])\n             ax_cv_PTHREAD_CLANG_NO_WARN_FLAG=\"$ax_pthread_try\"\n            ])\n\n        case \"$ax_cv_PTHREAD_CLANG_NO_WARN_FLAG\" in\n                no | unknown) ;;\n                *) PTHREAD_CFLAGS=\"$ax_cv_PTHREAD_CLANG_NO_WARN_FLAG $PTHREAD_CFLAGS\" ;;\n        esac\n\nfi # $ax_pthread_clang = yes\n\nif test \"x$ax_pthread_ok\" = \"xno\"; then\nfor ax_pthread_try_flag in $ax_pthread_flags; do\n\n        case $ax_pthread_try_flag in\n                none)\n                AC_MSG_CHECKING([whether pthreads work without any flags])\n                ;;\n\n                -mt,pthread)\n                AC_MSG_CHECKING([whether pthreads work with -mt -lpthread])\n                PTHREAD_CFLAGS=\"-mt\"\n                PTHREAD_LIBS=\"-lpthread\"\n                ;;\n\n                -*)\n                AC_MSG_CHECKING([whether pthreads work with $ax_pthread_try_flag])\n                PTHREAD_CFLAGS=\"$ax_pthread_try_flag\"\n                ;;\n\n                pthread-config)\n                AC_CHECK_PROG([ax_pthread_config], [pthread-config], [yes], [no])\n                AS_IF([test \"x$ax_pthread_config\" = \"xno\"], [continue])\n                PTHREAD_CFLAGS=\"`pthread-config --cflags`\"\n                PTHREAD_LIBS=\"`pthread-config --ldflags` `pthread-config --libs`\"\n                ;;\n\n                *)\n                AC_MSG_CHECKING([for the pthreads library -l$ax_pthread_try_flag])\n                PTHREAD_LIBS=\"-l$ax_pthread_try_flag\"\n                ;;\n        esac\n\n        ax_pthread_save_CFLAGS=\"$CFLAGS\"\n        ax_pthread_save_LIBS=\"$LIBS\"\n        CFLAGS=\"$CFLAGS $PTHREAD_CFLAGS\"\n        LIBS=\"$PTHREAD_LIBS $LIBS\"\n\n        # Check for various functions.  We must include pthread.h,\n        # since some functions may be macros.  (On the Sequent, we\n        # need a special flag -Kthread to make this header compile.)\n        # We check for pthread_join because it is in -lpthread on IRIX\n        # while pthread_create is in libc.  We check for pthread_attr_init\n        # due to DEC craziness with -lpthreads.  We check for\n        # pthread_cleanup_push because it is one of the few pthread\n        # functions on Solaris that doesn't have a non-functional libc stub.\n        # We try pthread_create on general principles.\n\n        AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <pthread.h>\n#                       if $ax_pthread_check_cond\n#                        error \"$ax_pthread_check_macro must be defined\"\n#                       endif\n                        static void routine(void *a) { a = 0; }\n                        static void *start_routine(void *a) { return a; }],\n                       [pthread_t th; pthread_attr_t attr;\n                        pthread_create(&th, 0, start_routine, 0);\n                        pthread_join(th, 0);\n                        pthread_attr_init(&attr);\n                        pthread_cleanup_push(routine, 0);\n                        pthread_cleanup_pop(0) /* ; */])],\n            [ax_pthread_ok=yes],\n            [])\n\n        CFLAGS=\"$ax_pthread_save_CFLAGS\"\n        LIBS=\"$ax_pthread_save_LIBS\"\n\n        AC_MSG_RESULT([$ax_pthread_ok])\n        AS_IF([test \"x$ax_pthread_ok\" = \"xyes\"], [break])\n\n        PTHREAD_LIBS=\"\"\n        PTHREAD_CFLAGS=\"\"\ndone\nfi\n\n# Various other checks:\nif test \"x$ax_pthread_ok\" = \"xyes\"; then\n        ax_pthread_save_CFLAGS=\"$CFLAGS\"\n        ax_pthread_save_LIBS=\"$LIBS\"\n        CFLAGS=\"$CFLAGS $PTHREAD_CFLAGS\"\n        LIBS=\"$PTHREAD_LIBS $LIBS\"\n\n        # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.\n        AC_CACHE_CHECK([for joinable pthread attribute],\n            [ax_cv_PTHREAD_JOINABLE_ATTR],\n            [ax_cv_PTHREAD_JOINABLE_ATTR=unknown\n             for ax_pthread_attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do\n                 AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <pthread.h>],\n                                                 [int attr = $ax_pthread_attr; return attr /* ; */])],\n                                [ax_cv_PTHREAD_JOINABLE_ATTR=$ax_pthread_attr; break],\n                                [])\n             done\n            ])\n        AS_IF([test \"x$ax_cv_PTHREAD_JOINABLE_ATTR\" != \"xunknown\" && \\\n               test \"x$ax_cv_PTHREAD_JOINABLE_ATTR\" != \"xPTHREAD_CREATE_JOINABLE\" && \\\n               test \"x$ax_pthread_joinable_attr_defined\" != \"xyes\"],\n              [AC_DEFINE_UNQUOTED([PTHREAD_CREATE_JOINABLE],\n                                  [$ax_cv_PTHREAD_JOINABLE_ATTR],\n                                  [Define to necessary symbol if this constant\n                                   uses a non-standard name on your system.])\n               ax_pthread_joinable_attr_defined=yes\n              ])\n\n        AC_CACHE_CHECK([whether more special flags are required for pthreads],\n            [ax_cv_PTHREAD_SPECIAL_FLAGS],\n            [ax_cv_PTHREAD_SPECIAL_FLAGS=no\n             case $host_os in\n             solaris*)\n             ax_cv_PTHREAD_SPECIAL_FLAGS=\"-D_POSIX_PTHREAD_SEMANTICS\"\n             ;;\n             esac\n            ])\n        AS_IF([test \"x$ax_cv_PTHREAD_SPECIAL_FLAGS\" != \"xno\" && \\\n               test \"x$ax_pthread_special_flags_added\" != \"xyes\"],\n              [PTHREAD_CFLAGS=\"$ax_cv_PTHREAD_SPECIAL_FLAGS $PTHREAD_CFLAGS\"\n               ax_pthread_special_flags_added=yes])\n\n        AC_CACHE_CHECK([for PTHREAD_PRIO_INHERIT],\n            [ax_cv_PTHREAD_PRIO_INHERIT],\n            [AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <pthread.h>]],\n                                             [[int i = PTHREAD_PRIO_INHERIT;]])],\n                            [ax_cv_PTHREAD_PRIO_INHERIT=yes],\n                            [ax_cv_PTHREAD_PRIO_INHERIT=no])\n            ])\n        AS_IF([test \"x$ax_cv_PTHREAD_PRIO_INHERIT\" = \"xyes\" && \\\n               test \"x$ax_pthread_prio_inherit_defined\" != \"xyes\"],\n              [AC_DEFINE([HAVE_PTHREAD_PRIO_INHERIT], [1], [Have PTHREAD_PRIO_INHERIT.])\n               ax_pthread_prio_inherit_defined=yes\n              ])\n\n        CFLAGS=\"$ax_pthread_save_CFLAGS\"\n        LIBS=\"$ax_pthread_save_LIBS\"\n\n        # More AIX lossage: compile with *_r variant\n        if test \"x$GCC\" != \"xyes\"; then\n            case $host_os in\n                aix*)\n                AS_CASE([\"x/$CC\"],\n                    [x*/c89|x*/c89_128|x*/c99|x*/c99_128|x*/cc|x*/cc128|x*/xlc|x*/xlc_v6|x*/xlc128|x*/xlc128_v6],\n                    [#handle absolute path differently from PATH based program lookup\n                     AS_CASE([\"x$CC\"],\n                         [x/*],\n                         [AS_IF([AS_EXECUTABLE_P([${CC}_r])],[PTHREAD_CC=\"${CC}_r\"])],\n                         [AC_CHECK_PROGS([PTHREAD_CC],[${CC}_r],[$CC])])])\n                ;;\n            esac\n        fi\nfi\n\ntest -n \"$PTHREAD_CC\" || PTHREAD_CC=\"$CC\"\n\nAC_SUBST([PTHREAD_LIBS])\nAC_SUBST([PTHREAD_CFLAGS])\nAC_SUBST([PTHREAD_CC])\n\n# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:\nif test \"x$ax_pthread_ok\" = \"xyes\"; then\n        ifelse([$1],,[AC_DEFINE([HAVE_PTHREAD],[1],[Define if you have POSIX threads libraries and header files.])],[$1])\n        :\nelse\n        ax_pthread_ok=no\n        $2\nfi\nAC_LANG_POP\n])dnl AX_PTHREAD\n"
  },
  {
    "path": "deps/memkind/src/make_rpm.mk",
    "content": "#\n#  Copyright (C) 2014 - 2016 Intel Corporation.\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#  1. Redistributions of source code must retain the above copyright notice(s),\n#     this list of conditions and the following disclaimer.\n#  2. Redistributions in binary form must reproduce the above copyright notice(s),\n#     this list of conditions and the following disclaimer in the documentation\n#     and/or other materials provided with the distribution.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n#  EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n#  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n#\n\npackage_prefix ?=\nname = $(package_prefix)memkind\narch = $(shell uname -p)\nversion = 0.0.0\nrelease = 1\n\nsrc = $(shell cat MANIFEST)\n\ntopdir = $(HOME)/rpmbuild\nrpm = $(topdir)/RPMS/$(arch)/$(name)-devel-$(version)-$(release).$(arch).rpm\ntrpm = $(topdir)/TRPMS/$(arch)/$(name)-tests-$(version)-$(release).$(arch).rpm\nsrpm = $(topdir)/SRPMS/$(name)-$(version)-$(release).src.rpm\nspecfile = $(topdir)/SPECS/$(name)-$(version).spec\nsource_tar = $(topdir)/SOURCES/memkind-$(version).tar.gz\nsource_tmp_dir := $(topdir)/SOURCES/$(name)-tmp-$(shell date +%s)\nrpmbuild_flags = -E '%define _topdir $(topdir)'\nrpmclean_flags = $(rpmbuild_flags) --clean --rmsource --rmspec\nmemkind_test_dir = $(MPSS_TEST_BASEDIR)/memkind-dt\nexclude_source_files = test/memkind-afts.ts \\\ntest/memkind-afts-ext.ts \\\ntest/memkind-slts.ts \\\ntest/memkind-perf.ts \\\ntest/memkind-perf-ext.ts \\\ntest/memkind-pytests.ts \\\ntest/python_framework/cmd_helper.py \\\ntest/python_framework/huge_page_organizer.py \\\ntest/python_framework/__init__.py \\\ntest/hbw_detection_test.py \\\ntest/autohbw_test.py \\\ntest/trace_mechanism_test.py \\\ntest/draw_plots.py \\\nVERSION\n\nall: $(rpm)\n\n$(rpm): $(specfile) $(source_tar)\n\trpmbuild $(rpmbuild_flags) $(specfile) -ba\n\tmkdir -p $(topdir)/TRPMS/$(arch)\n\tmv $(topdir)/RPMS/$(arch)/$(name)-tests-$(version)-$(release).$(arch).rpm $(trpm)\n\n$(source_tar): $(topdir)/.setup $(src) MANIFEST\n\tmkdir -p $(source_tmp_dir)\n\tset -e ; \\\n\tfor f in $(exclude_source_files); do \\\n\t\techo $$f >> $(source_tmp_dir)/EXCLUDE ; \\\n\tdone\n\ttar cf $(source_tmp_dir)/tmp.tar -T MANIFEST --transform=\"s|^|$(name)-$(version)/|\" -X $(source_tmp_dir)/EXCLUDE\n\tcd $(source_tmp_dir) && tar xf tmp.tar\n\tset -e ; \\\n\tfor f in $(exclude_source_files); do \\\n\t\tmkdir -p `dirname $(source_tmp_dir)/$(name)-$(version)/$$f` ; \\\n\t\tcp $$f $(source_tmp_dir)/$(name)-$(version)/$$f ; \\\n\tdone\n\tcd $(source_tmp_dir)/$(name)-$(version) && ./autogen.sh && (cd ./jemalloc && ./autogen.sh) && ./configure && make dist; \\\n\t# tar.gz produced by \"make dist\" from above produces memkind-$(version).tar.gz\n\t# If $(package_prefix) is not empty, then need to repackage that tar.gz to $(name)-$(version)\n\t# thus below command. Otherwise, rpmbuild will fail.\n\tif [ -n \"$(package_prefix)\" ]; then \\\n\t    cd $(source_tmp_dir)/$(name)-$(version) && \\\n\t    tar xf memkind-$(version).tar.gz && \\\n\t    rm -rf memkind-$(version).tar.gz && \\\n\t    mv memkind-$(version) $(name)-$(version) && \\\n\t    tar cfz memkind-$(version).tar.gz $(name)-$(version); \\\n\tfi\n\tmv $(source_tmp_dir)/$(name)-$(version)/memkind-$(version).tar.gz $@\n\trm -rf $(source_tmp_dir)\n\n$(specfile): $(topdir)/.setup memkind.spec.mk\n\t@echo \"$$memkind_spec\" > $@\n\n$(topdir)/.setup:\n\tmkdir -p $(topdir)/{SOURCES,SPECS}\n\ttouch $@\n\nclean:\n\t-rpmbuild $(rpmclean_flags) $(specfile)\n\n.PHONY: all clean\n\ninclude memkind.spec.mk\n\n"
  },
  {
    "path": "deps/memkind/src/man/autohbw.7",
    "content": ".\\\"\n.\\\" Copyright (C) 2014-2016 Intel Corporation.\n.\\\" All rights reserved.\n.\\\"\n.\\\" Redistribution and use in source and binary forms, with or without\n.\\\" modification, are permitted provided that the following conditions are met:\n.\\\" 1. Redistributions of source code must retain the above copyright notice(s),\n.\\\"    this list of conditions and the following disclaimer.\n.\\\" 2. Redistributions in binary form must reproduce the above copyright notice(s),\n.\\\"    this list of conditions and the following disclaimer in the documentation\n.\\\"    and/or other materials provided with the distribution.\n.\\\"\n.\\\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n.\\\" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n.\\\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n.\\\" EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n.\\\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n.\\\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n.\\\" PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n.\\\" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n.\\\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n.\\\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n.\\\"\n.TH \"AUTOHBW\" 7 \"2016-07-28\" \"Intel Corporation\" \"AUTOHBW\" \\\" -*- nroff -*-\n.SH \"NAME\"\nlibautohbw.so \\- An interposer library for redirecting heap allocations\n.SH \"SYNOPSIS\"\n.BR LD_PRELOAD=libautohbw.so\ncommand {arguments ...}\n.SH \"DESCRIPTION\"\n.B AutoHBW\nlibrary\n.BR (libautohbw.so)\nis an interposer library for redirecting heap allocations\n.B (malloc, calloc, realloc, valloc, posix_memalign, memlign)\nto high-bandwidth\n.B (HBW)\nmemory. Consequently, AutoHBW library can be used to\nautomatically allocate high-bandwidth memory without any modification to\nsource code of an application.\n\n.br\nFor instance, the following command-line runs existing binary /bin/ls with\nAutoHBW library, automatically redirecting heap allocations (larger than a given\nthreshold) to high-bandwidth memory.\n.IP\n.B LD_PRELOAD=libautohbw.so\n/bin/ls\n\n.SH \"ENVIRONMENT\"\n\nThe behavior of AutoHBW library is controlled by the following environment\nvariables.\n\n.PP\n.B AUTO_HBW_SIZE=x:[y]\n.br\nIndicates that any allocation larger than\n.I x\nand smaller than\n.I y\nshould be\nallocated in HBW memory.\n.I x\nand\n.I y\ncan be followed by a K, M, or G to indicate\nthe size in Kilo/Memga/Giga bytes (e.g., 4K, 3M, 2G).\n.br\n\nExamples:\n.IP AUTO_HBW_SIZE=4K\n# allocations larger than 4K allocated in HBW\n.IP AUTO_HBW_SIZE=1M:5M\n# allocations between 1M and 5M allocated in HBW\n\n.PP\n.B AUTO_HBW_LOG=level\n.br\nSets the value of logging (printing)\n.I level.\nIf\n.I level\nis:\n.br\n.IP -1\nno messages are printed\n.br\n.IP 0\nno allocations messages are printed but INFO messages are printed\n.br\n.IP 1\na log message is printed for each allocation (Default)\n.br\n.IP 2\na log message is printed for each allocation with a backtrace.\nRedirect this output and use\n.B autohbw_get_src_lines.pl\nto find source lines for each allocation. Your application must\nbe compiled with\n.B -g\nto see source lines.\n.PP\nNotes:\n.IP\n1. Logging adds extra overhead. Therefore, for performance\ncritical runs, logging level should be 0\n.IP\n2. The amount of free memory printed with log messages is only\napproximate -- e.g. pages that are not touched yet are excluded\n.PP\nExamples:\n.IP AUTO_HBW_LOG=1\n\n.PP\n.B AUTO_HBW_MEM_TYPE=memory_type\n.br\nSets the type of memory type that should be automatically allocated. By\ndefault, this type is MEMKIND_HBW_PREFERRED, if MCDRAM is found in your\nsystem; otherwise, the default is MEMKIND_DEFAULT. The names of memory\ntypes are defined in\n.B memkind(3)\nman page.\n.B memory_type\nhas to be one of\n.B MEMKIND_DEFAULT, MEMKIND_HUGETLB, MEMKIND_INTERLEAVE, MEMKIND_HBW,\n.B MEMKIND_HBW_PREFERRED, MEMKIND_HBW_HUGETLB, MEMKIND_HBW_PREFERRED_HUGETLB,\n.B MEMKIND_HBW_GBTLB (DEPRECATED), MEMKIND_HBW_PREFERRED_GBTLB (DEPRECATED), MEMKIND_GBTLB (DEPRECATED),\n.B MEMKIND_HBW_INTERLEAVE\n\nIf you are requesting any huge\nTLB pages, please make sure that the requested type is currently enabled\nin your OS.\n\nExamples:\n.IP AUTO_HBW_MEM_TYPE=MEMKIND_HBW_PREFERRED\n# (default, if MCDRAM present)\n.IP AUTO_HBW_MEM_TYPE=MEMKIND_DEFAULT\n# (default, if MCDRAM absent)\n.IP AUTO_HBW_MEM_TYPE=MEMKIND_HBW_HUGETLB\n.IP AUTO_HBW_MEM_TYPE=MEMKIND_HUGETLB\n\n.PP\n.B AUTO_HBW_DEBUG=0|1|2\n.br\nSet the debug message printing level. Default is 0. This is mainly for\ndevelopment.\n\n.SH \"NOTES\"\nIt is possible to temporarily disable/enable automatic HBW allocations by\ncalling disableAutoHBW() and enableAutoHBW() in source code. To call\nthese routines, please include\n.B autohbw_api.h\nheader file and link with -lautohbw.\n\n\n.br\nIf high-bandwidth memory is not physically present in your system,\nthe environment variable\n.B MEMKIND_HBW_NODES\nmust be set to indicate the high-bandwidth node as indicated in\n.B memkind(3).\n\n\n.SH \"EXAMPLES\"\n.br\nThe following will run /bin/ls with AutoHBW library. Make sure that paths to\nboth libautohbw.so and libmemkind.so are included in\n.B LD_LIBRARY_PATH.\n.IP\n.B LD_PRELOAD=libautohbw.so\n/bin/ls -l\n.PP\nTo run with MPI, a shell script must be created, with the correct LD_PRELOAD\ncommand for each rank. For example, if we put\n.B LD_PRELOAD=libautohbw.so /bin/ls\nin a shell script named autohbw_test.sh, it can be executed with 2 MPI ranks as:\n.br\n.IP\n.B mpirun -n 2 ./autohbw_test.sh\n\n.SH \"COPYRIGHT\"\nCopyright (C) 2014, 2015, 2016 Intel Corporation. All rights reserved.\n\n.SH \"SEE ALSO\"\n.BR memkind(3)\n.BR malloc (3),\n.BR numactl (8),\n\n\n\n\n\n\n\n"
  },
  {
    "path": "deps/memkind/src/man/hbwallocator.3",
    "content": ".\\\"\n.\\\" Copyright (C) 2015 - 2018 Intel Corporation.\n.\\\" All rights reserved.\n.\\\"\n.\\\" Redistribution and use in source and binary forms, with or without\n.\\\" modification, are permitted provided that the following conditions are met:\n.\\\" 1. Redistributions of source code must retain the above copyright notice(s),\n.\\\"    this list of conditions and the following disclaimer.\n.\\\" 2. Redistributions in binary form must reproduce the above copyright notice(s),\n.\\\"    this list of conditions and the following disclaimer in the documentation\n.\\\"    and/or other materials provided with the distribution.\n.\\\"\n.\\\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n.\\\" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n.\\\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n.\\\" EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n.\\\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n.\\\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n.\\\" PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n.\\\" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n.\\\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n.\\\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n.\\\"\n.TH \"HBWALLOCATOR\" 3 \"2015-11-02\" \"Intel Corporation\" \"HBWALLOCATOR\" \\\" -*- nroff -*-\n.SH \"NAME\"\nhbw::allocator<T> \\- The C++ allocator compatible with the C++ standard library allocator concepts\n.br\nNote: This is EXPERIMENTAL API. The functionality and the header file itself can be changed (including non-backward compatible changes) or removed.\n.SH \"SYNOPSIS\"\n.nf\n.B #include <hbw_allocator.h>\n.sp\n.B Link with -lmemkind\n.sp\n.B hbw::allocator()\n.br\n.B template <class U>hbw::allocator<T>::allocator(const hbw::allocator<U>&)\n.br\n.B hbw::allocator<T>::~allocator()\n.br\n.B hbw::allocator<T>::pointer hbw::allocator<T>::address(hbw::allocator<T>::reference x)\n.br\n.B hbw::allocator<T>::const_pointer hbw::allocator<T>::address(hbw::allocator<T>::const_reference x)\n.br\n.B hbw::allocator<T>::pointer hbw::allocator<T>::allocate(hbw::allocator<T>::size_type n, const void * = 0)\n.br\n.B void hbw::allocator<T>::deallocate(hbw::allocator<T>::pointer p, hbw::allocator<T>::size_type n)\n.br\n.B hbw::allocator<T>::size_type  hbw::allocator<T>::max_size()\n.br\n.B void hbw::allocator<T>::construct(hbw::allocator<T>::pointer p, const hbw::allocator<T>::value_type& val)\n.br\n.B void hbw::allocator<T>::destroy(hbw::allocator<T>::pointer p)\n.fi\n.SH \"DESCRIPTION\"\nThe hbw::allocator<T> is intended to be used with STL containers to allocate high bandwidth memory. Memory management is based on hbwmalloc (memkind library), enabling users to gain performance in multithreaded applications. Refer hbwmalloc(3) and memkind(3) man page for more details.\n.PP\nAll public member types and functions corresponds to standard library allocator concepts and definitions. The current implementation supports C++03 standard.\n.PP\nTemplate arguments:\n.br\nT is an object type aliased by value_type.\n.br\nU is an object type.\n.PP\nNote:\n.br\nhbw::allocator<T>::pointer hbw::allocator<T>::allocate(hbw::allocator<T>::size_type n, const void * = 0)\nallocates high bandwidth memory using\n.IR \"hbw_malloc()\".\nThrow\n.I std::bad_alloc\nwhen:\n.br\n.IR\t\tn \" = 0,\"\n.br\n.IR\t\tn \" > \"max_size() \",\"\n.br\n\tor there is not enough memory to satisfy the request.\n\n.PP\nvoid hbw::allocator<T>::deallocate(hbw::allocator<T>::pointer p, hbw::allocator<T>::size_type n) deallocates memory associated with pointer returned by\n.I allocate()\nusing\n.IR \"hbw_free()\".\n.PP\nTo find out more about\n.I hbw_malloc()\nand\n.I hbw_free()\nread hbwmalloc(3) man page.\n\n\n.SH ERRORS\n.TP\nThe same as described in ERRORS section of hbwmalloc(3) man page.\n.SH \"NOTES\"\nThe\n.I hbw::allocator<T>\nbehavior depends on hbwmalloc heap management policy. To get and set the policy please use\n.I hbw_get_policy()\nand\n.I hbw_set_policy()\nrespectively.\n.SH \"COPYRIGHT\"\nCopyright (C) 2015 - 2018 Intel Corporation. All rights reserved.\n.SH \"SEE ALSO\"\n.BR hbwmalloc(3),\n.BR numa (3),\n.BR numactl (8),\n.BR memkind (3)\n"
  },
  {
    "path": "deps/memkind/src/man/hbwmalloc.3",
    "content": ".\\\"\n.\\\" Copyright (C) 2014 - 2018 Intel Corporation.\n.\\\" All rights reserved.\n.\\\"\n.\\\" Redistribution and use in source and binary forms, with or without\n.\\\" modification, are permitted provided that the following conditions are met:\n.\\\" 1. Redistributions of source code must retain the above copyright notice(s),\n.\\\"    this list of conditions and the following disclaimer.\n.\\\" 2. Redistributions in binary form must reproduce the above copyright notice(s),\n.\\\"    this list of conditions and the following disclaimer in the documentation\n.\\\"    and/or other materials provided with the distribution.\n.\\\"\n.\\\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n.\\\" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n.\\\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n.\\\" EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n.\\\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n.\\\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n.\\\" PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n.\\\" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n.\\\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n.\\\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n.\\\"\n.TH \"HBWMALLOC\" 3 \"2015-03-31\" \"Intel Corporation\" \"HBWMALLOC\" \\\" -*- nroff -*-\n.SH \"NAME\"\nhbwmalloc \\- The high bandwidth memory interface\n.br\nNote: hbwmalloc.h functionality is considered as stable API (STANDARD API).\n.SH \"SYNOPSIS\"\n.nf\n.B #include <hbwmalloc.h>\n.sp\n.B Link with -lmemkind\n.sp\n.B int hbw_check_available(void);\n.br\n.BI \"void* hbw_malloc(size_t \" \"size\" );\n.br\n.BI \"void* hbw_calloc(size_t \" \"nmemb\" \", size_t \" \"size\" );\n.br\n.BI \"void* hbw_realloc (void \" \"*ptr\" \", size_t \" \"size\" );\n.br\n.BI \"void hbw_free(void \" \"*ptr\" );\n.br\n.BI \"int hbw_posix_memalign(void \" \"**memptr\" \", size_t \" \"alignment\" \", size_t \" \"size\" );\n.br\n.BI \"int hbw_posix_memalign_psize(void \" \"**memptr\" \", size_t \" \"alignment\" \", size_t \" \"size\" \", hbw_pagesize_t \" \"pagesize\" );\n.br\n.B hbw_policy_t hbw_get_policy(void);\n.br\n.BI \"int hbw_set_policy(hbw_policy_t \" \"mode\" );\n.br\n.BI \"int hbw_verify_memory_region(void \" \"*addr\" \", size_t \" \"size\" \", int \" \"flags\" );\n.fi\n.SH \"DESCRIPTION\"\n.BR hbw_check_available ()\nreturns zero if high bandwidth memory is available or an error code\ndescribed in the\n.B ERRORS\nsection if not.\n.PP\n.BR hbw_malloc ()\nallocates\n.I size\nbytes of uninitialized high bandwidth memory. The allocated space is\nsuitably aligned (after possible pointer coercion) for storage of any\ntype of object. If\n.I size\nis zero then\n.BR hbw_malloc ()\nreturns NULL.\n.PP\n.BR hbw_calloc ()\nallocates space for\n.I nmemb\nobjects in high bandwidth memory, each\n.I size\nbytes in length. The result is identical to calling\n.BR hbw_malloc ()\nwith an argument of\n.IR nmemb * size\n, with the exception that the allocated memory is explicitly\ninitialized to zero bytes.  If\n.I nmemb\nor\n.I size\nis 0, then\n.BR hbw_calloc ()\nreturns NULL.\n.PP\n.BR hbw_realloc ()\nchanges the size of the previously allocated high bandwidth memory\nreferenced by\n.I ptr\nto\n.I size\nbytes. The contents of the memory are unchanged up to the lesser of\nthe new and old sizes. If the new size is larger, the contents of the\nnewly allocated portion of the memory are undefined. Upon success, the\nmemory referenced by\n.I ptr\nis freed and a pointer to the newly allocated high bandwidth memory is\nreturned.\n\n.B Note:\n.BR hbw_realloc ()\nmay move the memory allocation, resulting in a different return value\nthan\n.IR \"ptr\" .\n\nIf\n.I ptr\nis NULL, the\n.BR hbw_realloc ()\nfunction behaves identically to\n.BR hbw_malloc ()\nfor the specified size.\nThe address\n.IR \"ptr\" ,\nif not NULL, was returned by a previous call to\n.BR hbw_malloc (),\n.BR hbw_calloc (),\n.BR hbw_realloc (),\nor\n.BR hbw_posix_memalign ().\nOtherwise, or if\n.I hbw_free(ptr)\nwas called before, undefined behavior occurs.\n\n\n.B Note:\n.BR hbw_realloc ()\ncannot be used with a pointer returned by\n.BR hbw_posix_memalign_psize ().\n\n.PP\n.BR hbw_free ()\ncauses the allocated memory referenced by\n.I ptr\nto be made available for future allocations. If\n.I ptr\nis NULL, no action occurs.\nThe address\n.IR \"ptr\" ,\nif not NULL, must have been returned by a previous call to\n.BR hbw_malloc (),\n.BR hbw_calloc (),\n.BR hbw_realloc (),\n.BR hbw_posix_memalign (),\nor\n.BR hbw_posix_memalign_psize ().\nOtherwise, if\n.I hbw_free(ptr)\nwas called before, undefined behavior occurs.\n.PP\n.BR hbw_posix_memalign ()\nallocates\n.I size\nbytes of high bandwidth memory such that the allocation's base address\nis an even multiple of\n.IR \"alignment\" ,\nand returns the allocation in the value pointed to by\n.IR \"memptr\" .\nThe requested\n.I alignment\nmust be a power of 2 at least as large as\n.IR \"sizeof(void *)\" .\n.PP\n.BR hbw_posix_memalign_psize ()\nallocates\n.I size\nbytes of high bandwidth memory such that the allocation's base address\nis an even multiple of\n.IR \"alignment\" ,\nand returns the allocation in the value pointed to by\n.IR \"memptr\" .\nThe requested\n.I alignment\nmust be a power of 2 at least as large as\n.IR \"sizeof(void *)\" .\nThe memory will be allocated using pages determined by the\n.IR \"pagesize\"\nvariable which may be one of the following enumerated values:\n.TP\n.B HBW_PAGESIZE_4KB\nThe four kilobyte page size option. Note that with transparent huge\npages enabled these allocations may be promoted by the operating\nsystem to two megabyte pages.\n.TP\n.B HBW_PAGESIZE_2MB\nThe two megabyte page size option. Note: This page size requires\nhuge pages configuration described in SYSTEM CONFIGURATION section.\n.TP\n.B HBW_PAGESIZE_1GB (DEPRECATED)\nThis option allows the user to specify arbitrary sizes backed by\n1GB chunks of huge pages. Huge pages are allocated even if the\nsize is not a modulo of 1GB. Note: This page size requires\nhuge pages configuration described in SYSTEM CONFIGURATION section.\n.TP\n.B HBW_PAGESIZE_1GB_STRICT (DEPRECATED)\nThe total size of the allocation must be a multiple of 1GB with\nthis option, otherwise the allocation will fail. Note: This page\nsize requires huge pages configuration described in SYSTEM\nCONFIGURATION section.\n.TP\nHBW_PAGESIZE_2MB, HBW_PAGESIZE_1GB and HBW_PAGESIZE_1GB_STRICT options are not supported with HBW_POLICY_INTERLEAVE policy.\n.PP\n.BR hbw_get_policy ()\nreturns the current fallback policy when insufficient high bandwidth\nmemory is available.\n.PP\n.BR hbw_set_policy ()\nsets the current fallback policy. The policy can be modified only once in the lifetime  of  an  application and before calling hbw_*alloc() or hbw_posix_memalign*() function.\n.br\nNote: If the policy is not set, than HBW_POLICY_PREFERRED will be used by default.\n.TP\n.B HBW_POLICY_BIND\nIf insufficient high bandwidth memory from the nearest NUMA node is\navailable to satisfy a request, the allocated pointer is set to NULL\nand\n.I errno\nis set to ENOMEM.  If insufficient high bandwidth memory pages are\navailable at fault time the Out Of Memory (OOM) killer is triggered.\nNote that pages are faulted exclusively from the high bandwidth NUMA\nnode nearest at time of allocation, not at time of fault.\n.TP\n.B HBW_POLICY_BIND_ALL\nIf insufficient high bandwidth memory is available to satisfy a request,\nthe allocated pointer is set to NULL and\n.I errno\nis set to ENOMEM.  If insufficient high bandwidth memory pages are\navailable at fault time the Out Of Memory (OOM) killer is triggered.\nNote that pages are faulted from the high bandwidth NUMA nodes.\nNearest NUMA node is selected at time of page fault.\n.TP\n.B HBW_POLICY_PREFERRED\nIf insufficient memory is available from the high bandwidth NUMA node\nclosest at allocation time, fall back to standard memory (default)\nwith the smallest NUMA distance.\n.TP\n.B HBW_POLICY_INTERLEAVE\nInterleave faulted pages from across all high bandwidth NUMA nodes\nusing standard size pages (the Transparent Huge Page feature is\ndisabled).\n.PP\n.BR hbw_verify_memory_region ()\nverifies if memory region fully fall into high bandwidth memory. Returns\n0 if memory address range from\n.IR \"addr\"\nto\n.IR \"addr\"\n+\n.IR \"size\"\nis allocated in high bandwidth memory,\n-1 if any fragment of memory was not backed by high bandwidth memory [e.g. when memory is not initialized]\nor one of error codes described in ERRORS section.\n\nUsing this function in production code may result in serious performance penalty.\n\n.IR Flags\nargument may include optional flags that modifies function behaviour:\n.TP\n.B HBW_TOUCH_PAGES\nBefore checking pages, function will touch first byte of all pages in address range starting from\n.IR \"addr\"\nto\n.IR \"addr\"\n+\n.IR \"size\"\nby read and write (so the content will be overwritten by the same data as it was read).\nUsing this option may trigger Out Of Memory killer.\n.SH \"RETURN VALUE\"\n.BR hbw_get_policy ()\nreturns\n.B HBW_POLICY_BIND,\n.B HBW_POLICY_BIND_ALL,\n.B HBW_POLICY_PREFERRED\nor\n.B HBW_POLICY_INTERLEAVE\nwhich represents the current high bandwidth policy.\n.BR hbw_free ()\ndo not have return value.\n.BR hbw_malloc ()\n.BR hbw_calloc (),\nand\n.BR hbw_realloc ()\nreturn the pointer to the allocated memory, or NULL if the request\nfails.\n.BR hbw_posix_memalign (),\n.BR hbw_posix_memalign_psize ()\nand\n.BR hbw_set_policy ()\nreturn zero on success and return an error code\nas described in the\n.B ERRORS\nsection below on failure.\n.SH ERRORS\n.TP\nError codes described here are the POSIX standard error codes as defined in <errno.h>\n.TP\n.BR hbw_check_available ()\nreturns\n.BR ENODEV\nif high-bandwidth memory is unavailable.\n.TP\n.BR \"hbw_posix_memalign\" \"() and \" \"hbw_posix_memalign_psize\" \"()\"\nIf the\n.I alignment\nparameter is not a power of two, or was not a multiple of\n.IR \"sizoeof(void *)\" ,\nthen\n.B EINVAL\nis returned.\nIf the policy and\n.I pagesize\ncombination is unsupported then\n.B EINVAL\nis returned.\nIf there was insufficient memory to satisfy the request then\n.B ENOMEM\nis returned.\n.TP\n.BR hbw_set_policy ()\nreturns\n.B EPERM\nif hbw_set_policy () was called more than once, or\n.B EINVAL if\n.I mode\nargument was neither\n.B HBW_POLICY_PREFERRED,\n.B HBW_POLICY_BIND,\n.B HBW_POLICY_BIND_ALL\nnor\n.B HBW_POLICY_INTERLEAVE.\n.TP\n.BR hbw_verify_memory_region ()\nreturns\n.B EINVAL\nif\n.IR \"addr\"\nis NULL,\n.IR \"size\"\nequals 0 or\n.IR \"flags\"\ncontained unsupported bit set. If memory pointed by\n.IR \"addr\"\ncould not be verified then\n.B EFAULT\nis returned.\n.SH \"NOTES\"\nThe\n.I hbwmalloc.h\nfile defines the external functions and enumerations for the hbwmalloc\nlibrary. These interfaces define a heap manager that targets high\nbandwidth memory numa nodes.\n.SH \"FILES\"\n.TP\n.I /usr/bin/memkind-hbw-nodes\nPrints a comma separated list of high bandwidth nodes.\n.SH \"ENVIRONMENT\"\n.TP\n.B MEMKIND_HBW_NODES\nThis environment variable is a comma separated list of NUMA nodes that\nare treated as high bandwidth. Uses the\n.I libnuma\nroutine\n.BR numa_parse_nodestring ()\nfor parsing, so the syntax described in the\n.BR numa (3)\nman page for this routine applies for example: 1-3,5 is a valid setting.\n.TP\n.B MEMKIND_ARENA_NUM_PER_KIND\nThis environment variable allows leveraging internal mechanism of\nthe library for setting number of arenas per kind. Value should be\na positive integer (not greater than INT_MAX defined in limits.h).\nThe user should set the value based on the characteristics\nof application that is using the library. Higher value can\nprovide better performance in extremely multithreaded applications at\nthe cost of memory overhead. See section \"IMPLEMENTATION NOTES\" of\n.BR jemalloc (3)\nfor more details about arenas.\n.TP\n.B MEMKIND_HEAP_MANAGER\nControls heap management behavior in memkind library by switching to one of the available heap managers.\n.br\nValues:\n.br\n    JEMALLOC – sets the jemalloc heap manager\n.br\n    TBB – sets the Intel Threading Building Blocks heap manager. This option requires installed\n    Intel Threading Building Blocks library.\nIf the MEMKIND_HEAP_MANAGER is not set then the jemalloc heap manager will be used by default.\n.SH \"SYSTEM CONFIGURATION\"\nInterfaces for obtaining 2MB (HUGETLB) need allocated\nhuge pages in the kernel's huge page pool.\n.TP\n.B HUGETLB (huge pages)\nCurrent number of \"persistent\" huge pages can be read from /proc/sys/vm/nr_hugepages file.\nProposed way of setting hugepages is: \"sudo sysctl vm.nr_hugepages=<number_of_hugepages>\".\nMore information can be found here:\nhttps://www.kernel.org/doc/Documentation/vm/hugetlbpage.txt\n.SH \"KNOWN ISSUES\"\n.TP\n.B HUGETLB (huge pages)\nThere might be some overhead in huge pages consumption caused by heap management.\nIf your allocation fails because of OOM, please try to allocate extra huge pages (e.g. 8 huge pages).\n.SH \"COPYRIGHT\"\nCopyright (C) 2014 - 2018 Intel Corporation. All rights reserved.\n.SH \"SEE ALSO\"\n.BR malloc (3),\n.BR numa (3),\n.BR numactl (8),\n.BR mbind (2),\n.BR mmap (2),\n.BR move_pages (2)\n.BR jemalloc (3)\n.BR memkind (3)\n"
  },
  {
    "path": "deps/memkind/src/man/memkind-hbw-nodes.1",
    "content": ".\\\"\n.\\\" Copyright (C) 2018 Intel Corporation.\n.\\\" All rights reserved.\n.\\\"\n.\\\" Redistribution and use in source and binary forms, with or without\n.\\\" modification, are permitted provided that the following conditions are met:\n.\\\" 1. Redistributions of source code must retain the above copyright notice(s),\n.\\\"    this list of conditions and the following disclaimer.\n.\\\" 2. Redistributions in binary form must reproduce the above copyright notice(s),\n.\\\"    this list of conditions and the following disclaimer in the documentation\n.\\\"    and/or other materials provided with the distribution.\n.\\\"\n.\\\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n.\\\" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n.\\\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n.\\\" EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n.\\\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n.\\\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n.\\\" PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n.\\\" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n.\\\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n.\\\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n.\\\"\n.TH \"MEMKIND-HBW_NODES\" 1 \"2018-03-12\" \"Intel Corporation\" \"MEMKIND-HBW_NODES\" \\\" -*- nroff -*-\n.SH \"NAME\"\nmemkind-hbw-nodes - print comma separated list of high bandwidth nodes.\n.br\nNote: This is EXPERIMENTAL API. The functionality and the header file itself can be changed (including non-backward compatible changes), or removed.\n.SH \"SYNOPSIS\"\nmemkind-hbw-nodes [-h|--help]\n.SH \"DESCRIPTION\"\n.BR memkind-hbw-nodes\nprints a comma separated list of high bandwidth NUMA nodes that can be used with the numactl --membind option.\n.SH \"OPTIONS\"\n.BR \" -h, --help\"\n              Display help text and exit.\n.SH \"EXIT STATUS\"\n.BR memkind-hbw-nodes\nexits with status 0 on success, 1 on failure, 2 on invalid argument.\n.SH \"COPYRIGHT\"\nCopyright (C) 2016 - 2018 Intel Corporation. All rights reserved.\n.SH \"SEE ALSO\"\n.BR hbwmalloc (3),\n.BR memkind (3),\n.BR numactl (8)\n.SH \"AUTHOR\"\nKrzysztof Kulakowski <krzysztof.kulakowski@intel.com>\n"
  },
  {
    "path": "deps/memkind/src/man/memkind.3",
    "content": ".\\\"\n.\\\" Copyright (C) 2014 - 2018 Intel Corporation.\n.\\\" All rights reserved.\n.\\\"\n.\\\" Redistribution and use in source and binary forms, with or without\n.\\\" modification, are permitted provided that the following conditions are met:\n.\\\" 1. Redistributions of source code must retain the above copyright notice(s),\n.\\\"    this list of conditions and the following disclaimer.\n.\\\" 2. Redistributions in binary form must reproduce the above copyright notice(s),\n.\\\"    this list of conditions and the following disclaimer in the documentation\n.\\\"    and/or other materials provided with the distribution.\n.\\\"\n.\\\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n.\\\" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n.\\\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n.\\\" EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n.\\\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n.\\\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n.\\\" PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n.\\\" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n.\\\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n.\\\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n.\\\"\n.TH \"MEMKIND\" 3 \"2015-03-31\" \"Intel Corporation\" \"MEMKIND\" \\\" -*- nroff -*-\n.SH \"NAME\"\nmemkind \\- Heap manager that enables allocations to memory with different properties.\n.br\nThis header expose STANDARD and EXPERIMENTAL API. API Standards are described below in this man page.\n.SH \"SYNOPSIS\"\n.nf\n.B #include <memkind.h>\n.sp\n.B Link with -lmemkind\n.br\n.SS \"EXPERIMENTAL API:\"\n.sp\n.B \"HEAP MANAGEMENT:\"\n.br\n.BI \"int memkind_posix_memalign(memkind_t \" \"kind\" \", void \" \"**memptr\" \", size_t \" \"alignment\" \", size_t \" \"size\" );\n.sp\n.B \"KIND MANAGEMENT:\"\n.br\n.BI \"int memkind_create_kind(memkind_memtype_t \" \"memtype_flags\" \", memkind_policy_t \" \"policy\" \", memkind_bits_t \" \"flags\" \", memkind_t \" \"*kind\" );\n.sp\n.SS \"STANDARD API:\"\n.sp\n.B \"ERROR HANDLING:\"\n.br\n.BI \"void memkind_error_message(int \" \"err\" \", char \" \"*msg\" \", size_t \" \"size\" );\n.sp\n.B \"LIBRARY VERSION:\"\n.br\n.BI \"int memkind_get_version();\"\n.sp\n.B \"HEAP MANAGEMENT:\"\n.br\n.BI \"void *memkind_malloc(memkind_t \" \"kind\" \", size_t \" \"size\" );\n.br\n.BI \"void *memkind_calloc(memkind_t \" \"kind\" \", size_t \" \"num\" \", size_t \" \"size\" );\n.br\n.BI \"void *memkind_realloc(memkind_t \" \"kind\" \", void \" \"*ptr\" \", size_t \" \"size\" );\n.br\n.BI \"void memkind_free(memkind_t \" \"kind\" \", void \" \"*ptr\" );\n.br\n.BI \"size_t memkind_malloc_usable_size(memkind_t \" \"kind\" \", void \" \"*ptr\" );\n.sp\n.B \"KIND MANAGEMENT:\"\n.br\n.BI \"int memkind_create_pmem(const char \" \"*dir\" \", size_t \" \"max_size\" \", memkind_t \" \"*kind\" );\n.br\n.BI \"int memkind_destroy_kind(memkind_t \" \"kind\" );\n.br\n.BI \"int memkind_check_available(memkind_t \" \"kind\" );\n.sp\n.B \"DECORATORS:\"\n.br\n.BI \"void memkind_malloc_pre(memkind_t \" \"*kind\" \", size_t \" \"*size\" );\n.br\n.BI \"void memkind_malloc_post(memkind_t \" \"kind\" \", size_t \" \"size\" \", void \" \"**result\" );\n.br\n.BI \"void memkind_calloc_pre(memkind_t \" \"*kind\" \", size_t \" \"*nmemb\" \", size_t \" \"*size\" );\n.br\n.BI \"void memkind_calloc_post(memkind_t \" \"kind\" \", size_t \" \"nmemb\" \", size_t \" \"size\" \", void \" \"**result\" );\n.br\n.BI \"void memkind_posix_memalign_pre(memkind_t \" \"*kind\" \", void \" \"**memptr\" \", size_t \" \"*alignment\" \", size_t \" \"*size\" );\n.br\n.BI \"void memkind_posix_memalign_post(memkind_t \" \"kind\" \", void \" \"**memptr\" \", size_t \" \"alignment\" \", size_t \" \"size\" \", int \" \"*err\" );\n.br\n.BI \"void memkind_realloc_pre(memkind_t \" \"*kind\" \", void \" \"**ptr\" \", size_t \" \"*size\" );\n.br\n.BI \"void memkind_realloc_post(memkind_t \" \"*kind\" \", void \" \"*ptr\" \", size_t \" \"size\" \", void \" \"**result\" );\n.br\n.BI \"void memkind_free_pre(memkind_t \" \"*kind\" \", void \" \"**ptr\" );\n.br\n.BI \"void memkind_free_post(memkind_t \" \"kind\" \", void \" \"*ptr\" );\n.sp\n.sp\n.br\n.SH \"DESCRIPTION\"\n.PP\n.BR memkind_error_message ()\nconverts an error number\n.I err\nreturned by a member of the memkind\ninterface to an error message\n.I msg\nwhere the maximum size of the message is passed by the\n.I size\nparameter.\n\n.B \"HEAP MANAGEMENT:\"\n.br\nThe functions described in this section define a heap manager with an\ninterface modeled on the ISO C standard API's, except that the user\nmust specify the\n.I kind\nof memory with the first argument to each function. See the\n.B KINDS\nsection below for a full description of the implemented kinds.\nFor file-backed kind of memory see\n.BR memkind_create_pmem() .\n.PP\n.BR memkind_malloc ()\nallocates\n.I size\nbytes of uninitialized memory of the specified\n.IR \"kind\" .\nThe allocated space is suitably aligned (after possible pointer\ncoercion) for storage of any type of object. If\n.I size\nis 0, then\n.BR memkind_malloc ()\nreturns NULL.\n.PP\n.BR memkind_calloc ()\nallocates space for\n.I num\nobjects each\n.I size\nbytes in length in memory of the specified\n.IR \"kind\" .\nThe result is identical to calling\n.BR memkind_malloc ()\nwith an argument of\n.IR num * size ,\nwith the exception that the allocated memory is explicitly\ninitialized to zero bytes.\nIf\n.I num\nor\n.I size\nis 0, then\n.BR memkind_calloc ()\nreturns NULL.\n.PP\n.BR memkind_realloc ()\nchanges the size of the previously allocated memory referenced by\n.I ptr\nto\n.I size\nbytes of the specified\n.IR \"kind\" .\nThe contents of the memory are unchanged up to the lesser of\nthe new and old sizes. If the new size is larger, the contents of the\nnewly allocated portion of the memory are undefined. Upon success, the\nmemory referenced by\n.I ptr\nis freed and a pointer to the newly allocated high bandwidth memory is\nreturned.\n\n.BR Note:\n.BR memkind_realloc ()\nmay move the memory allocation, resulting in a different return value\nthan\n.IR \"ptr\" .\n\nIf\n.I ptr\nis NULL, the\n.BR memkind_realloc ()\nfunction behaves identically to\n.BR memkind_malloc ()\nfor the specified size.\nThe address\n.IR \"ptr\" ,\nif not NULL, must have been returned by a previous call to\n.BR memkind_malloc (),\n.BR memkind_calloc (),\n.BR memkind_realloc (),\nor\n.BR memkind_posix_memalign ()\nwith the same\n.I kind\nas specified to the call to\n.BR memkind_realloc ().\nOtherwise, if\n.I memkind_free(kind, ptr)\nwas called before, undefined behavior occurs.\n.PP\n.BR memkind_posix_memalign ()\nallocates\n.I size\nbytes of memory of a specified\n.I kind\nsuch that the allocation's base address\nis an even multiple of\n.IR \"alignment\" ,\nand returns the allocation in the value pointed to by\n.IR \"memptr\" .\nThe requested\n.I alignment\nmust be a power of 2 at least as large as\n.IR \"sizeof(void *)\" .\nIf\n.I size\nis 0, then\n.BR memkind_posix_memalign ()\nreturns NULL.\n.PP\n.BR memkind_malloc_usable_size ()\nfunction provides the same semantics as\n.BR malloc_usable_size(3),\nbut operates on specified\n.I kind.\n\n.BR Note:\n.BR memkind_malloc_usable_size ()\nis not supported by TBB heap manager described in\n.B ENVIRONMENT\nsection.\n\n.PP\n.BR memkind_free ()\ncauses the allocated memory referenced by\n.I ptr\nto be made available for future allocations. This pointer\nmust have been returned by a previous call to\n.BR memkind_malloc (),\n.BR memkind_calloc (),\n.BR memkind_realloc (),\nor\n.BR memkind_posix_memalign ().\nOtherwise, if\n.I memkind_free(kind, ptr)\nwas already called before, undefined behavior occurs.\nIf\n.I ptr\nis NULL, no operation is performed.\nThe value of\n.B MEMKIND_DEFAULT\ncan be given as the\n.I kind\nfor all buffers allocated by a kind that leverages the jemalloc\nallocator. In cases where the kind is unknown in the\ncontext of the call to\n.BR memkind_free ()\n.B 0\ncan be given as the\n.I kind\nspecified to\n.BR memkind_free ()\nbut this will require a look up that can be bypassed by specifying\na non-zero value.\n.sp\n.B \"KIND MANAGEMENT:\"\n.br\nThere are built-in kinds that are always available and these are enumerated in the\n.B KINDS\nsection. The user can also create their own kinds of memory. This\nsection describes the API's that enable the tracking of the different\nkinds of memory and determining their properties.\n.PP\n.BR memkind_create_pmem ()\nis a convenient function used to create a file-backed kind of memory.\nIt allocates a temporary file in the given directory\n.IR dir .\nThe file is created in a fashion similar to\n.BR tmpfile (3),\nso that the file name does not appear when the directory is listed and\nthe space is automatically freed when the program terminates.\nThe file is truncated to a size of\n.I max_size\nbytes and the resulting space is memory-mapped.\n.br\nNote that the actual file system space is not allocated immediately, but only\non a call to\n.BR memkind_pmem_mmap ()\n(see\n.BR memkind_pmem (3)).\nThis allows to create a pmem memkind of a pretty large size without the\nneed to reserve in advance the corresponding file system space for the entire\nheap. If the value of\n.I max_size\nequals 0, pmem memkind is only limited by the capacity of the file system mounted under\n.I dir\nargument.\n The minimum\n.I max_size\nvalue which allows to limit the size of kind by the library is defined as\n.BR MEMKIND_PMEM_MIN_SIZE .\nCalling\n.BR memkind_create_pmem ()\nwith a size smaller than that and different than 0 will return an error.\nThe maximum allowed size is not limited by\n.BR memkind ,\nbut by the file system specified by the\n.I dir\nargument.\nThe\n.I max_size\npassed in is the raw size of the memory pool and\n.B jemalloc\nwill use some of that space for its own metadata.\nReturns zero if the pmem memkind is created successfully or an error code from the\n.B ERRORS\nsection if not.\n.PP\n.BR memkind_create_kind ()\ncreates kind that allocates memory with specific memory type, memory binding policy and flags (see\n.B MEMORY FLAGS\nsection).\nThe\n.IR memtype_flags\n(see\n.B MEMORY TYPES\nsection) determine memory types to allocate,\n.IR policy\nargument is policy for specifying page binding to memory types selected by\n.IR memtype_flags .\nReturns zero if the specified kind is created successfully or an error code from the\n.B ERRORS\nsection if not.\n.PP\n.BR memkind_destroy_kind ()\ndestroys previously created kind object, which must have been returned by a previous call to\n.BR memkind_create_pmem ()\nor\n.BR memkind_create_kind () .\nOtherwise, or if\n.I memkind_destroy_kind(kind)\nwas already called before, undefined behavior occurs.\nNote that, when the kind was returned by\n.BR memkind_create_kind ()\nall allocated memory must be freed before kind is destroyed,\notherwise this will cause memory leak. When the kind was returned by\n.BR memkind_create_pmem ()\nall allocated memory will be freed after kind will be destroyed.\n.PP\n.BR memkind_check_available ()\nreturns zero if the specified\n.I kind\nis available or an error code from the\n.B ERRORS\nsection if it is not.\n.PP\n.BR MEMKIND_PMEM_MIN_SIZE\nThe minimum size which allows to limit the file-backed memory partition.\n.sp\n.B \"DECORATORS:\"\n.br\nThe memkind library enables the user to define decorator functions that\ncan be called before and after each memkind heap management API. The\ndecorators that are called at the beginning of the function end are named\nafter that function with\n.I _pre\nappended to the name and those that are called at the end of the\nfunction are named after that function with\n.I _post\nappended to the name. These are weak symbols and if they are not\npresent at link time they are not called. The memkind library does\nnot define these symbols which are reserved for user definition.\nThese decorators can be used to track calls to the heap management\ninterface or to modify parameters. The decorators that are called at\nthe beginning of the allocator pass all inputs by reference and the\ndecorators that are called at the end of the allocator pass the output\nby reference. This enables the modification of the input and output\nof each heap management function by the decorators.\n.sp\n.B \"LIBRARY VERSION:\"\n.br\nThe memkind library version scheme consist major, minor and patch numbers separated by dot. Combining those numbers, we got the following representation:\n.br\nmajor.minor.patch, where:\n.br\n\t-major number is incremented whenever API is changed (loss of backward compatibility),\n.br\n\t-minor number is incremented whenever additional extensions are introduced or behavior has been changed,\n.br\n\t-patch number is incremented whenever small bug fixes are added.\n.sp\nmemkind library provide numeric representation of the version by exposing the following API:\n.PP\n.BR memkind_get_version ()\nreturn version number represented by a single integer number, obtained from the formula:\n.br\nmajor * 1000000 + minor * 1000 + patch\n.sp\nNote: major < 1 means unstable API.\n.sp\nAPI standards:\n.br\n-STANDARD API, API is considered as stable\n.br\n-NON-STANDARD API, API is considered as stable, however this is not a standard way to use memkind\n.br\n-EXPERIMENTAL API, API is considered as unstable and the subject to change\n.br\n.sp\n.SH \"RETURN VALUE\"\n.BR memkind_calloc (),\n.BR memkind_malloc (),\nand\n.BR memkind_realloc (),\nreturns the pointer to the allocated memory or NULL if the request fails.\n.BR memkind_malloc_usable_size ()\nreturns the number of usable bytes in the block of allocated memory pointed to by\n.I ptr,\na pointer to a block of memory allocated by\n.BR memkind_malloc ()\nor a related function. If\n.I ptr\nis NULL, 0 is returned.\n.BR memkind_free ()\nand\n.BR memkind_error_message ()\ndo not have return values.\nAll other memkind API's return 0 upon\nsuccess and an error code defined in the\n.B ERRORS\nsection upon failure.\nThe memkind library avoids setting\n.I errno\ndirectly, but calls to underlying libraries and system calls may set\n.IR errno\n( e.g.\n.BR memkind_create_pmem ()\n).\n.SH \"KINDS\"\nThe available kinds of memory:\n.TP\n.B MEMKIND_DEFAULT\nDefault allocation using standard memory and default page size.\n.TP\n.B MEMKIND_HUGETLB\nAllocate from standard memory using huge pages. Note: This kind requires\nhuge pages configuration described in\n.B SYSTEM CONFIGURATION\nsection.\n.TP\n.B MEMKIND_GBTLB (DEPRECATED)\nAllocate from standard memory using 1GB chunks backed by huge pages.\nNote: This kind requires huge pages configuration described in\n.B SYSTEM CONFIGURATION\nsection.\n.TP\n.B MEMKIND_INTERLEAVE\nAllocate pages interleaved across all NUMA nodes with transparent huge\npages disabled.\n.TP\n.B MEMKIND_HBW\nAllocate from the closest high bandwidth memory NUMA node at time\nof allocation. If there is not enough high bandwidth memory to satisfy the request\n.I errno\nis set to ENOMEM and the allocated pointer is set to NULL.\n.TP\n.B MEMKIND_HBW_ALL\nSame as\n.B MEMKIND_HBW\nexcept decision regarding closest NUMA node is postponed until the time of first write.\n.TP\n.B MEMKIND_HBW_HUGETLB\nSame as\n.B MEMKIND_HBW\nexcept the allocation is backed by huge pages. Note: This kind requires\nhuge pages configuration described in\n.B SYSTEM CONFIGURATION\nsection.\n.TP\n.B MEMKIND_HBW_ALL_HUGETLB\nCombination of\n.B MEMKIND_HBW_ALL\nand\n.B MEMKIND_HBW_HUGETLB\nproperties. Note: This kind requires\nhuge pages configuration described in\n.B SYSTEM CONFIGURATION\nsection.\n.TP\n.B MEMKIND_HBW_PREFERRED\nSame as\n.B MEMKIND_HBW\nexcept that if there is not enough high bandwidth memory to satisfy\nthe request, the allocation will fall back on standard memory.\n.TP\n.B MEMKIND_HBW_PREFERRED_HUGETLB\nSame as\n.B MEMKIND_HBW_PREFERRED\nexcept the allocation is backed by huge pages. Note: This kind requires huge pages\nconfiguration described in\n.B SYSTEM CONFIGURATION\nsection.\n.TP\n.B MEMKIND_HBW_GBTLB (DEPRECATED)\nSame as\n.B MEMKIND_HBW\nexcept the allocation is backed by 1GB chunks of huge pages. Note that\n.I size\ncan take on any value, but full gigabyte pages will allocated for each\nrequest, so remainder of the last page will be wasted.\nThis kind requires huge pages configuration described in\n.B SYSTEM CONFIGURATION\nsection.\n.TP\n.B MEMKIND_HBW_PREFERRED_GBTLB (DEPRECATED)\nSame as\n.B MEMKIND_HBW_GBTLB\nexcept that if there is not enough high bandwidth memory to satisfy\nthe request, the allocation will fall back on standard memory. Note: This kind\nrequires huge pages configuration described in\n.B SYSTEM CONFIGURATION\nsection.\n.TP\n.B MEMKIND_HBW_INTERLEAVE\nSame as\n.B MEMKIND_HBW\nexcept that the pages that support the allocation are interleaved\nacross all high bandwidth nodes and transparent huge pages are\ndisabled.\n.TP\n.B MEMKIND_REGULAR\nAllocate from regular memory using the default page size. Regular means general purpose memory\nfrom the NUMA nodes containing CPUs.\n.SH \"MEMORY TYPES\"\nThe available types of memory:\n.TP\n.B MEMKIND_MEMTYPE_DEFAULT\nStandard memory, the same as process uses.\n.TP\n.B MEMKIND_MEMTYPE_HIGH_BANDWIDTH\nHigh bandwidth memory (HBM). There must be at least two memory types with different bandwidth to determine which is the HBM.\n.SH \"MEMORY BINDING POLICY\"\nThe available types of memory binding policy:\n.TP\n.B MEMKIND_POLICY_BIND_LOCAL\nAllocate local memory. If there is not enough memory to satisfy the request\n.I errno\nis set to\n.BR ENOMEM\nand the allocated pointer is set to\n.IR \"NULL\" .\n.TP\n.B MEMKIND_POLICY_BIND_ALL\nMemory locality is ignored. If there is not enough memory to satisfy the request\n.I errno\nis set to\n.B ENOMEM\nand the allocated pointer is set to\n.IR \"NULL\" .\n.TP\n.B MEMKIND_POLICY_PREFERRED_LOCAL\nAllocate preferred memory that is local.\nIf there is not enough preferred memory to satisfy the request or\npreferred memory is not available, the allocation will fall back on any other memory.\n.TP\n.B MEMKIND_POLICY_INTERLEAVE_LOCAL\nInterleave allocation across local memory.\nFor n memory types the allocation will be interleaved across all of them.\n.TP\n.B MEMKIND_POLICY_INTERLEAVE_ALL\nInterleave allocation. Locality is ignored.\nFor n memory types the allocation will be interleaved across all of them.\n.TP\n.B MEMKIND_POLICY_MAX_VALUE\nMax policy value.\n.SH \"MEMORY FLAGS\"\nThe available types of memory flags:\n.TP\n.B MEMKIND_MASK_PAGE_SIZE_2MB\nAllocation backed by 2MB page size.\n.SH \"ERRORS\"\n.TP\n.BR memkind_posix_memalign ()\nreturns the one of the POSIX standard error codes\n.B EINVAL\nor\n.B ENOMEM\nas defined in\n.I <errno.h>\nif an error occurs (these have positive values).\nIf the\n.I alignment\nparameter is not a power of two or is not a multiple of\n.IR \"sizeof(void *)\" ,\nthen\n.B EINVAL\nis returned. If there is insufficient memory to satisfy the request then\n.B ENOMEM\nis returned.\n.PP\nAll functions other than\n.BR memkind_posix_memalign ()\nwhich have an integer return type return one of the negative error\ncodes as defined in\n.I <memkind.h>\nand described below.\n.TP\n.B MEMKIND_ERROR_UNAVAILABLE\nRequested memory kind is not available\n.TP\n.B MEMKIND_ERROR_MBIND\nCall to\n.BR mbind (2)\nfailed\n.TP\n.B MEMKIND_ERROR_MMAP\nCall to\n.BR mmap (2)\nfailed\n.TP\n.B MEMKIND_ERROR_MALLOC\nCall to jemalloc's\n.BR malloc ()\nfailed\n.TP\n.B MEMKIND_ERROR_ENVIRON\nError parsing environment variable (MEMKIND_*)\n.TP\n.B MEMKIND_ERROR_INVALID\nInvalid input arguments to memkind routine\n.TP\n.B MEMKIND_ERROR_TOOMANY\nError trying to initialize more than maximum\n.B MEMKIND_MAX_KIND\nnumber of kinds\n.TP\n.B MEMKIND_ERROR_BADOPS\nError memkind operation structure is missing or invalid\n.TP\n.B MEMKIND_ERROR_HUGETLB\nUnable to allocate huge pages\n.TP\n.B MEMKIND_ERROR_MEMTYPE_NOT_AVAILABLE\nError requested memory type is not available\n.TP\n.B MEMKIND_ERROR_OPERATION_FAILED\nError memkind operation failed\n.TP\n.B MEMKIND_ERROR_ARENAS_CREATE\nCall to jemalloc's\n.BR arenas.create ()\nfailed\n.TP\n.B MEMKIND_ERROR_RUNTIME\nUnspecified run-time error\n.SH \"FILES\"\n.TP\n.I /usr/bin/memkind-hbw-nodes\nPrints a comma separated list of high bandwidth nodes.\n.SH \"MEMORY TYPES\"\n.TP\n.B MEMKIND_MEMTYPE_DEFAULT\nStandard memory, the same as process uses.\n.TP\n.B MEMKIND_MEMTYPE_HIGH_BANDWIDTH\nHigh bandwidth memory (HBM). There must be at least two memory types with different bandwidth to determine which is the HBM.\n.SH \"MEMORY BINDING POLICY\"\n.TP\n.B MEMKIND_POLICY_BIND_LOCAL\nAllocate local memory.\nIf there is not enough memory to satisfy the request \n.I errno\nis set to\n.BR ENOMEM\nand the allocated pointer is set to\n.I NULL\\fR.\n.TP\n.B MEMKIND_POLICY_BIND_ALL\nMemory locality is ignored.\nIf there is not enough memory to satisfy the request \n.I errno\nis set to\n.B ENOMEM\nand the allocated pointer is set to \n.IR NULL .\n.TP\n.B MEMKIND_POLICY_PREFERRED_LOCAL\nAllocate preferred memory that is local.\nIf there is not enough preferred memory to satisfy the request or\npreferred memory is not available, the allocation will fall back on any\nother memory.\n.TP\n.B MEMKIND_POLICY_INTERLEAVE_LOCAL\nInterleave allocation across local memory.\nFor n memory types the allocation will be interleaved across all of\nthem.\n.TP\n.B MEMKIND_POLICY_INTERLEAVE_ALL\nInterleave allocation. Locality is ignored.\nFor n memory types the allocation will be interleaved across all of\nthem.\n.TP\n.B MEMKIND_POLICY_MAX_VALUE\nMax policy value.\n.SH \"MEMORY FLAGS\"\n.TP\n.B MEMKIND_MASK_PAGE_SIZE_2MB\nAllocation backed by 2MB page size.\n.SH \"ENVIRONMENT\"\n.TP\n.B MEMKIND_HBW_NODES\nThis environment variable is a comma separated list of NUMA nodes that\nare treated as high bandwidth. Uses the\n.I libnuma\nroutine\n.BR numa_parse_nodestring ()\nfor parsing, so the syntax described in the\n.BR numa (3)\nman page for this routine applies: e.g. 1-3,5 is a valid setting.\n.TP\n.B MEMKIND_ARENA_NUM_PER_KIND\nThis environment variable allows leveraging internal mechanism of\nthe library for setting number of arenas per kind. Value should be\na positive integer (not greater than INT_MAX defined in limits.h).\nThe user should set the value based on the characteristics\nof application that is using the library. Higher value can\nprovide better performance in extremely multithreaded applications at\nthe cost of memory overhead. See section \"IMPLEMENTATION NOTES\" of\n.BR jemalloc (3)\nfor more details about arenas.\n.TP\n.B MEMKIND_HOG_MEMORY\nControls behavior of memkind with regards to returning memory to underlaying OS. Setting\n.B MEMKIND_HOG_MEMORY\nto \"1\" causes memkind to not release memory to OS in anticipation of memory reuse soon. This will\nimprove latency of 'free' operations but increase memory usage.\n.TP\n.B MEMKIND_DEBUG\nControls logging mechanism in memkind. Setting\n.B MEMKIND_DEBUG\nto \"1\" enables printing messages like errors and general information about environment to\n.IR stderr .\n.TP\n.B MEMKIND_HEAP_MANAGER\nControls heap management behavior in memkind library by switching to one of the available heap managers.\n.br\nValues:\n.br\n    JEMALLOC – sets the jemalloc heap manager\n.br\n    TBB – sets the Intel Threading Building Blocks heap manager. This option requires installed\n    Intel Threading Building Blocks library.\nIf the MEMKIND_HEAP_MANAGER is not set then the jemalloc heap manager will be used by default.\n.SH \"SYSTEM CONFIGURATION\"\nInterfaces for obtaining 2MB (HUGETLB) need allocated\nhuge pages in the kernel's huge page pool.\n.TP\n.B HUGETLB (huge pages)\nCurrent number of \"persistent\" huge pages can be read from /proc/sys/vm/nr_hugepages file.\nProposed way of setting hugepages is: \"sudo sysctl vm.nr_hugepages=<number_of_hugepages>\".\nMore information can be found here:\nhttps://www.kernel.org/doc/Documentation/vm/hugetlbpage.txt\n.SH \"STATIC LINKING\"\n.TP\nWhen linking statically against memkind, libmemkind.a should be used together with its dependencies libnuma and pthread. Pthread can be linked by adding /usr/lib64/libpthread.a as a dependency (exact path may vary). Typically libnuma will need to be compiled from sources to use it as a static dependency. libnuma can be reached on github: https://github.com/numactl/numactl\n.SH \"KNOWN ISSUES\"\n.TP\n.B HUGETLB (huge pages)\nThere might be some overhead in huge pages consumption caused by heap management.\nIf your allocation fails because of OOM, please try to allocate extra huge pages (e.g. 8 huge pages).\n.SH \"COPYRIGHT\"\nCopyright (C) 2014 - 2018 Intel Corporation. All rights reserved.\n.SH \"SEE ALSO\"\n.BR malloc (3),\n.BR malloc_usable_size (3),\n.BR numa (3),\n.BR numactl (8),\n.BR mbind (2),\n.BR mmap (2),\n.BR move_pages (2),\n.BR jemalloc (3),\n.BR memkind_default (3),\n.BR memkind_arena (3),\n.BR memkind_hbw (3),\n.BR memkind_hugetlb (3),\n.BR memkind_pmem (3)\n"
  },
  {
    "path": "deps/memkind/src/man/memkind_arena.3",
    "content": ".\\\"\n.\\\" Copyright (C) 2014 - 2018 Intel Corporation.\n.\\\" All rights reserved.\n.\\\"\n.\\\" Redistribution and use in source and binary forms, with or without\n.\\\" modification, are permitted provided that the following conditions are met:\n.\\\" 1. Redistributions of source code must retain the above copyright notice(s),\n.\\\"    this list of conditions and the following disclaimer.\n.\\\" 2. Redistributions in binary form must reproduce the above copyright notice(s),\n.\\\"    this list of conditions and the following disclaimer in the documentation\n.\\\"    and/or other materials provided with the distribution.\n.\\\"\n.\\\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n.\\\" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n.\\\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n.\\\" EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n.\\\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n.\\\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n.\\\" PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n.\\\" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n.\\\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n.\\\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n.\\\"\n.TH \"MEMKIND_ARENA\" 3 \"2015-04-21\" \"Intel Corporation\" \"MEMKIND_ARENA\" \\\" -*- nroff -*-\n.SH \"NAME\"\nmemkind_arena.h \\- jemalloc arena allocation memkind operations.\n.br\nNote: This is EXPERIMENTAL API. The functionality and the header file itself can be changed (including non-backward compatible changes) or removed.\n.SH \"SYNOPSIS\"\n.nf\n.B #include <memkind/internal/memkind_arena.h>\n.sp\n.B Link with -lmemkind\n.sp\n.BI \"int memkind_arena_create(struct memkind \" \"*kind\" \", struct memkind_ops \" \"*ops\" \", const char \" \"*name\" );\n.BI \"int memkind_arena_create_map(struct memkind \" \"*kind\" \", extent_hooks_t \" \"*hooks\" );\n.BI \"int memkind_arena_destroy(struct memkind \" \"*kind\" );\n.BI \"void *memkind_arena_malloc(struct memkind \" \"*kind\" \", size_t \" \"size\" );\n.BI \"void *memkind_arena_calloc(struct memkind \" \"*kind\" \", size_t \" \"num\" \", size_t \" \"size\" );\n.BI \"void *memkind_arena_pmem_calloc(struct memkind \" \"*kind\" \", size_t \" \"num\" \", size_t \" \"size\" );\n.BI \"int memkind_arena_posix_memalign(struct memkind \" \"*kind\" \", void \" \"**memptr\" \", size_t \" \"alignment\" \", size_t \" \"size\" );\n.BI \"void *memkind_arena_realloc(struct memkind \" \"*kind\" \", void \" \"*ptr\" \", size_t \" \"size\" );\n.BI \"int memkind_thread_get_arena(struct memkind \" \"*kind\" \", unsigned int \" \"*arena\" \", size_t \" \"size\" );\n.BI \"int memkind_bijective_get_arena(struct memkind \" \"*kind\" \", unsigned int \" \"*arena\" \", size_t \" \"size\" );\n.BI \"struct memkind *get_kind_by_arena(unsigned \" \"arena_ind\" );\n.BI \"int memkind_arena_finalize(struct memkind \" \"*kind\" );\n.BI \"void memkind_arena_init(struct memkind \" \"*kind\" );\n.BI \"void memkind_arena_free(struct memkind \" \"*kind\" \", void \" \"*ptr\" );\n.br\n.SH DESCRIPTION\nThis header file is a collection of functions can be used to populate\nthe memkind operations structure for memory kinds that use jemalloc.\n.PP\n.BR memkind_arena_create ()\nis an implementation of the memkind \"create\" operation for memory\nkinds that use jemalloc.  This calls\n.BR memkind_default_create ()\n(see\n.BR memkind_default.h (3))\nfollowed by\n.BR memkind_arena_create_map ()\ndescribed below.\n.PP\n.BR memkind_arena_create_map ()\ncreates the\n.I arena_map\narray for the memkind structure pointed to by\n.IR kind\nwhich can be indexed by the\n.BR ops.get_arena ()\nfunction from the kind's operations.  If get_arena points\n.BR memkind_thread_get_arena ()\nthen there will be four arenas created for each processor,\nand if get_arena points to\n.BR memkind_bijective_get_arena ()\nthen just one arena is created.\n.PP\n.BR memkind_arena_destroy ()\nis an implementation of the memkind \"destroy\" operation for memory\nkinds that use jemalloc.  This releases all of the resources\nallocated by\n.BR memkind_arena_create ().\n.PP\n.BR memkind_arena_malloc ()\nis an implementation of the memkind \"malloc\" operation for memory\nkinds that use jemalloc.  This allocates memory using the arenas\ncreated by\n.BR memkind_arena_create ()\nthrough the jemalloc's\n.BR mallocx ()\ninterface.  It uses the memkind \"get_arena\" operation to select the\narena.\n.PP\n.BR memkind_arena_calloc ()\nis an implementation of the memkind \"calloc\" operation for memory\nkinds that use jemalloc.  This allocates memory using the arenas\ncreated by\n.BR memkind_arena_create ()\nthrough the jemalloc's\n.BR mallocx ()\ninterface.  It uses the memkind \"get_arena\" operation to select the\narena.\n.PP\n.BR memkind_arena_pmem_calloc ()\nis an implementation of the memkind \"calloc\" operation for file-backed memory\nkinds that use jemalloc.  This allocates memory using the arenas\ncreated by\n.BR memkind_arena_create ()\nthrough the jemalloc's\n.BR mallocx ()\ninterface.  It uses the memkind \"get_arena\" operation to select the\narena.\n.PP\n.BR memkind_arena_posix_memalign ()\nis an implementation of the memkind \"posix_memalign\" operation for memory\nkinds that use jemalloc.  This allocates memory using the arenas\ncreated by\n.BR memkind_arena_create ()\nthrough the jemalloc's\n.BR mallocx ()\ninterface.  It uses the memkind \"get_arena\" operation to select the\narena.  The POSIX standard requires that\n.BR posix_memalign (3)\nmay not set\n.I errno\nhowever the jemalloc's\n.BR mallocx ()\nroutine may.  In an attempt to abide by the standard\n.I errno\nis recorded before calling jemalloc's\n.BR mallocx ()\nand then reset after the call.\n.PP\n.BR memkind_arena_realloc ()\nis an implementation of the memkind \"realloc\" operation for memory\nkinds that use jemalloc.  This allocates memory using the arenas\ncreated by\n.BR memkind_arena_create ()\nthrough the jemalloc's\n.BR mallocx ()\ninterface.  It uses the memkind \"get_arena\" operation to select the\narena.\n.PP\n.BR memkind_thread_get_arena ()\nretrieves the\n.I arena\nindex that is bound to to the calling thread based on a hash of its\nthread ID.  The\n.I arena\nindex can be used with the MALLOCX_ARENA macro to set flags for jemalloc's\n.BR mallocx ().\n.PP\n.BR memkind_bijective_arena_get_arena ()\nretrieves the\n.I arena\nindex to be used with the MALLOCX_ARENA macro to set flags for jemalloc's\n.BR mallocx ().\nUse of this operation implies that only one arena is used for the\n.IR kind .\n.PP\n.BR memkind_arena_free ()\nis an implementation of the memkind \"free\" operation for memory\nkinds that use jemalloc.  It causes the allocated memory referenced by\n.IR ptr ,\nwhich must have been returned by a previous call to\n.BR memkind_arena_malloc () ,\n.BR memkind_arena_calloc ()\nor\n.BR memkind_arena_realloc ()\nto be made available for future allocations.\nIt uses the memkind \"get_arena\" operation to select the arena.\n.PP\n.BR get_kind_by_arena ()\nreturns pointer to memory kind structure associated with given arena.\n.PP\n.BR memkind_arena_finalize ()\nis an implementation of the memkind \"finalize\" operation for memory kinds that\nuse jemalloc. This function releases all resources allocated by\n.BR memkind_arena_create ()\nand it's called when\n.BR main ()\nfinishes or after calling\n.BR exit ()\nfunction.\n.PP\n.BR memkind_arena_init ()\ncreates arena map with proper hooks per specified kind.\n.SH \"COPYRIGHT\"\nCopyright (C) 2014 - 2018 Intel Corporation. All rights reserved.\n.SH \"SEE ALSO\"\n.BR memkind (3),\n.BR memkind_default (3),\n.BR memkind_hbw (3),\n.BR memkind_hugetlb (3),\n.BR memkind_pmem (3),\n.BR jemalloc (3),\n.BR mbind (2),\n.BR mmap (2)\n"
  },
  {
    "path": "deps/memkind/src/man/memkind_default.3",
    "content": ".\\\"\n.\\\" Copyright (C) 2014 - 2018 Intel Corporation.\n.\\\" All rights reserved.\n.\\\"\n.\\\" Redistribution and use in source and binary forms, with or without\n.\\\" modification, are permitted provided that the following conditions are met:\n.\\\" 1. Redistributions of source code must retain the above copyright notice(s),\n.\\\"    this list of conditions and the following disclaimer.\n.\\\" 2. Redistributions in binary form must reproduce the above copyright notice(s),\n.\\\"    this list of conditions and the following disclaimer in the documentation\n.\\\"    and/or other materials provided with the distribution.\n.\\\"\n.\\\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n.\\\" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n.\\\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n.\\\" EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n.\\\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n.\\\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n.\\\" PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n.\\\" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n.\\\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n.\\\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n.\\\"\n.TH \"MEMKIND_DEFAULT\" 3 \"2015-04-21\" \"Intel Corporation\" \"MEMKIND_DEFAULT\" \\\" -*- nroff -*-\n.SH \"NAME\"\nmemkind_default.h \\- default implementations for memkind operations.\n.br\nNote: This is EXPERIMENTAL API. The functionality and the header file itself can be changed (including non-backward compatible changes), or removed.\n.SH \"SYNOPSIS\"\n.nf\n.B #include <memkind/internal/memkind_default.h>\n.sp\n.B Link with -lmemkind\n.sp\n.BI \"int memkind_default_create(struct memkind \" \"*kind\" \", struct memkind_ops \" \"*ops\" \", const char \" \"*name\" );\n.br\n.BI \"int memkind_default_destroy(struct memkind \" \"*kind\" );\n.br\n.BI \"void *memkind_default_malloc(struct memkind \" \"*kind\" \", size_t \" \"size\" );\n.br\n.BI \"void *memkind_default_calloc(struct memkind \" \"*kind\" \", size_t \" \"num\" \", size_t \" \"size\" );\n.br\n.BI \"int memkind_default_posix_memalign(struct memkind \" \"*kind\" \", void \" \"**memptr\" \", size_t \" \"alignment\" \", size_t \" \"size\" );\n.br\n.BI \"void *memkind_default_realloc(struct memkind \" \"*kind\" \", void \" \"*ptr\" \", size_t \" \"size\" );\n.br\n.BI \"void memkind_default_free(struct memkind \" \"*kind\" \", void \" \"*ptr\" );\n.br\n.BI \"void *memkind_default_mmap(struct memkind \" \"*kind\" \", void \" \"*addr\" \", size_t \" \"size\" );\n.br\n.BI \"int memkind_default_mbind(struct memkind \" \"*kind\" \", void \" \"*ptr\" \", size_t \" \"len\" );\n.br\n.BI \"int memkind_default_get_mmap_flags(struct memkind \" \"*kind\" \", int \" \"*flags\" );\n.br\n.BI \"int memkind_default_get_mbind_mode(struct memkind \" \"*kind\" \", int \" \"*mode\" );\n.br\n.BI \"size_t memkind_default_malloc_usable_size(struct memkind \" \"*kind\" \", void \" \"*ptr\" );\n.br\n.BI \"int memkind_preferred_get_mbind_mode(struct memkind \" \"*kind\" \", int \" \"*mode\" );\n.br\n.BI \"int memkind_interleave_get_mbind_mode(struct memkind \" \"*kind\" \", int \" \"*mode\" );\n.br\n.BI \"int memkind_nohugepage_madvise(struct memkind \" \"*kind\" \", void \" \"*addr\" \", size_t \" \"size\" );\n.br\n.BI \"int memkind_posix_check_alignment(struct memkind \" \"*kind\" \", size_t \" \"alignment\" );\n.br\n.BI \"int memkind_default_get_mbind_nodemask(struct memkind \" \"*kind\" \", unsigned long \" \"*nodemask\" \", unsigned long \" \"maxnode\" );\n.br\n.BI \"void memkind_default_init_once(void);\"\n.br\n.BI \"bool size_out_of_bounds(size_t \" \"size\" );\n.br\n.SH DESCRIPTION\n.PP\nDefault implementations for memkind operations which include a several\nuseful methods that are not part of the\n.B MEMKIND_DEFAULT\nkind which is a fall through to the jemalloc implementation.\n.PP\n.BR memkind_default_create ()\nimplements the required start up for every kind. If a kind does not\npoint to this function directly for its\n.BR ops.create ()\noperation, then the function that it points to must call\n.BR memkind_default_create ()\nat its start.\n.PP\n.BR memkind_default_destroy ()\nimplements the required shutdown for every kind. If a kind does not\npoint to this function directly for its\n.BR ops.destroy ()\noperation, then the function that it points to must call\n.BR memkind_default_destroy ()\nat its end.\n.PP\n.BR memkind_default_malloc ()\nis a direct call through the jemalloc's\n.BR malloc ().\n.PP\n.BR memkind_default_calloc ()\nis a direct call through the jemalloc's\n.BR calloc ().\n.PP\n.BR memkind_default_posix_memalign ()\nis a direct call through the jemalloc's\n.BR posix_memalign ().\n.PP\n.BR memkind_default_realloc ()\nis a direct call through the jemalloc's\n.BR realloc ().\n.PP\n.BR memkind_default_free ().\nis a direct call through the jemalloc's\n.BR free ().\nNote that this method can be called on any pointer returned by a\njemalloc allocation, and in particular, all of the arena\nallocations described in\n.BR memkind_arena (3)\ncan use this function for freeing.\n.PP\n.BR memkind_default_mmap ()\nThis calls the ops->get_mmap_flags()\noperations for the kind, or falls back on the default implementations\nif the function pointers are NULL.  The results of these calls are\npassed to the\n.BR mmap (2)\ncall to allocate pages from the operating system.  The\n.I addr\nis the hint passed through to\n.BR mmap (2)\nand\n.I size\nis the size of the buffer to be allocated.  The return value is the\nallocated buffer or\n.B MAP_FAILED\nin the case of an error.\n.PP\n.BR memkind_default_mbind ()\nmakes calls the kind's\n.BR ops.get_mbind_nodemask ()\nand\n.BR ops.get_mbind_mode ()\noperations to gather inputs and then calls the\n.BR mbind (2)\nsystem call using the results along with and user input\n.I ptr\nand\n.IR len .\n.PP\n.BR memkind_default_get_mmap_flags ()\nsets\n.I flags\nto\n.BR \"MAP_PRIVATE | MAP_ANONYMOUS\" .\nSee\n.BR mmap (2)\nfor more information about these flags.\n.PP\n.BR memkind_default_get_mbind_mode ()\nsets\n.I mode\nto\n.BR MPOL_BIND .\nSee\n.BR mbind (2)\nfor more information about this flag.\n.PP\n.BR memkind_default_malloc_usable_size ()\nis a direct call through the jemalloc's\n.BR malloc_usable_size ().\n.PP\n.BR memkind_preferred_get_mbind_mode ()\nsets\n.I mode\nto\n.BR MPOL_PREFERRED .\nSee\n.BR mbind (2)\nfor more information about this flag.\n.PP\n.BR memkind_interleave_get_mbind_mode ()\nsets\n.I mode\nto\n.BR MPOL_INTERLEAVE .\nSee\n.BR mbind (2)\nfor more information about this flag.\n.PP\n.BR memkind_nohugepage_madvise ()\ncalls\n.BR madvise (2)\nwith the\n.B MADV_NOHUGEPAGE\nadvice.\nSee\n.BR madvise (2)\nfor more information about this option.\n.PP\n.BR memkind_posix_check_alignment ()\ncan be used to check the alignment value for\n.BR memkind_posix_memalign ()\nto ensure that is abides by the POSIX requirements:\nalignment must be a power of 2 at least as large as\n.BR sizeof( \"void *\" ).\n.PP\n.BR memkind_default_get_mbind_nodemask ()\nwraps jemalloc's\n.BR copy_bitmask_to_bitmask.\nThis function copies body of the bitmask structure into passed pointer.\n.PP\n.BR memkind_default_init_once ()\ninitializes heap manager.\n.PP\n.BR size_out_of_bounds ()\nreturns true if given size is out of bounds, otherwise will return false.\n.SH \"COPYRIGHT\"\nCopyright (C) 2014 - 2018 Intel Corporation. All rights reserved.\n.SH \"SEE ALSO\"\n.BR memkind (3),\n.BR memkind_arena (3),\n.BR memkind_hbw (3),\n.BR memkind_hugetlb (3),\n.BR memkind_pmem (3),\n.BR jemalloc (3),\n.BR mbind (2),\n.BR mmap (2)\n"
  },
  {
    "path": "deps/memkind/src/man/memkind_hbw.3",
    "content": ".\\\"\n.\\\" Copyright (C) 2014 - 2018 Intel Corporation.\n.\\\" All rights reserved.\n.\\\"\n.\\\" Redistribution and use in source and binary forms, with or without\n.\\\" modification, are permitted provided that the following conditions are met:\n.\\\" 1. Redistributions of source code must retain the above copyright notice(s),\n.\\\"    this list of conditions and the following disclaimer.\n.\\\" 2. Redistributions in binary form must reproduce the above copyright notice(s),\n.\\\"    this list of conditions and the following disclaimer in the documentation\n.\\\"    and/or other materials provided with the distribution.\n.\\\"\n.\\\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n.\\\" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n.\\\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n.\\\" EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n.\\\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n.\\\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n.\\\" PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n.\\\" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n.\\\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n.\\\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n.\\\"\n.TH \"MEMKIND_HBW\" 3 \"2016-06-13\" \"Intel Corporation\" \"MEMKIND_HBW\" \\\" -*- nroff -*-\n.SH \"NAME\"\nmemkind_hbw.h \\- high bandwidth memory memkind operations.\n.br\nNote: This is EXPERIMENTAL API. The functionality and the header file itself can be changed (including non-backward compatible changes) or removed.\n.SH \"SYNOPSIS\"\n.nf\n.B #include <memkind/internal/memkind_hbw.h>\n.sp\n.B Link with -lmemkind\n.sp\n.BI \"int memkind_hbw_check_available(struct memkind \" \"*kind\" );\n.br\n.BI \"int memkind_hbw_hugetlb_check_available(struct memkind \" \"*kind\" );\n.br\n.BI \"int memkind_hbw_get_mbind_nodemask(struct memkind \" \"*kind\" \", unsigned long \" \"*nodemask\" \", unsigned long \" \"maxnode\" );\n.br\n.BI \"int memkind_hbw_all_get_mbind_nodemask(struct memkind \" \"*kind\" \", unsigned long \" \"*nodemask\" \", unsigned long \" \"maxnode\" );\n.br\n.BI \"void memkind_hbw_init_once(void);\"\n.br\n.BI \"void memkind_hbw_hugetlb_init_once(void);\"\n.br\n.BI \"void memkind_hbw_all_hugetlb_init_once(void);\"\n.br\n.BI \"void memkind_hbw_preferred_init_once(void);\"\n.br\n.BI \"void memkind_hbw_preferred_hugetlb_init_once(void);\"\n.br\n.BI \"void memkind_hbw_interleave_init_once(void);\"\n.br\n.SH DESCRIPTION\n.PP\nHigh bandwidth memory memkind operations.\n.PP\n.BR memkind_hbw_check_available ()\nreturns zero if library was able to detect heterogeneous NUMA node\nbandwidths.   Returns\n.B MEMKIND_UNAVAILABLE\nif the detection mechanism failed.\nDetection mechanism can be also overridden by the\nenvironment variable\n.B MEMKIND_HBW_NODES\nas described in the\n.BR memkind (3)\nman page.\n.PP\n.BR memkind_hbw_hugetlb_check_available ()\nIn addition to checking for high bandwidth memory as is done by\nmemkind_hbw_check_available (), this also checks for 2MB huge pages as\nis done by memkind_hugetlb_check_available_2mb().\n.PP\n.BR memkind_hbw_get_mbind_nodemask ()\nsets the\n.I nodemask\nbit to one that corresponds to the high bandwidth NUMA node that has\nthe closest NUMA distance to the CPU of the calling process.\nAll other bits up to\n.I maxnode\nare set to zero.\nThe\n.I nodemask\ncan be used in conjunction with the\n.BR mbind (2)\nsystem call.\n.PP\n.BR memkind_hbw_all_get_mbind_nodemask ()\nsets the\n.I nodemask\nbits to one that correspond to the all high bandwidth NUMA nodes in\nthe system. All other bits up to\n.I maxnode\nare set to zero.\nThe\n.I nodemask\ncan be used in conjunction with the\n.BR mbind (2)\nsystem call.\n.PP\n.BR memkind_hbw_init_once ()\nThis function initializes MEMKIND_HBW kind and it should not be called more than once.\nNote:\n.BR memkind_hbw_init_once ()\nmay reserve some extra memory.\n.PP\n.BR memkind_hbw_hugetlb_init_once ()\nThis function initializes MEMKIND_HBW_HUGETLB kind and it should not be called more than once.\nNote:\n.BR memkind_hbw_hugetlb_init_once ()\nmay reserve some extra memory.\n.PP\n.BR memkind_hbw_preferred_init_once ()\nThis function initializes MEMKIND_HBW_PREFERRED kind and it should not be called more than once.\nNote:\n.BR memkind_hbw_preferred_init_once ()\nmay reserve some extra memory.\n.PP\n.BR memkind_hbw_preferred_hugetlb_init_once ()\nThis function initializes MEMKIND_HBW_PREFERRED_HUGETLB kind and it should not be called more than once.\nNote:\n.BR memkind_hbw_preferred_hugetlb_init_once ()\nmay reserve some extra memory.\n.PP\n.BR memkind_hbw_all_hugetlb_init_once ()\nThis function initializes MEMKIND_HBW_ALL_HUGETLB kind and it should not be called more than once.\nNote:\n.BR memkind_hbw_all_hugetlb_init_once()\nmay reserve some extra memory.\n.PP\n.BR memkind_hbw_interleave_init_once ()\nThis function initializes MEMKIND_HBW_INTERLEAVE kind and it should not be called more than once.\nNote:\n.BR memkind_hbw_interleave_init_once ()\nmay reserve some extra memory.\n.SH \"COPYRIGHT\"\nCopyright (C) 2014 - 2018 Intel Corporation. All rights reserved.\n.SH \"SEE ALSO\"\n.BR memkind (3),\n.BR memkind_arena (3),\n.BR memkind_default (3),\n.BR memkind_hugetlb (3),\n.BR memkind_pmem (3),\n.BR jemalloc (3),\n.BR mbind (2),\n.BR mmap (2)\n"
  },
  {
    "path": "deps/memkind/src/man/memkind_hugetlb.3",
    "content": ".\\\"\n.\\\" Copyright (C) 2014 - 2016 Intel Corporation.\n.\\\" All rights reserved.\n.\\\"\n.\\\" Redistribution and use in source and binary forms, with or without\n.\\\" modification, are permitted provided that the following conditions are met:\n.\\\" 1. Redistributions of source code must retain the above copyright notice(s),\n.\\\"    this list of conditions and the following disclaimer.\n.\\\" 2. Redistributions in binary form must reproduce the above copyright notice(s),\n.\\\"    this list of conditions and the following disclaimer in the documentation\n.\\\"    and/or other materials provided with the distribution.\n.\\\"\n.\\\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n.\\\" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n.\\\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n.\\\" EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n.\\\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n.\\\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n.\\\" PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n.\\\" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n.\\\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n.\\\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n.\\\"\n.TH \"MEMKIND_HUGETLB\" 3 \"2015-04-21\" \"Intel Corporation\" \"MEMKIND_HUGETLB\" \\\" -*- nroff -*-\n.SH \"NAME\"\nmemkind_hugetlb.h \\- hugetlb memory memkind operations.\n.br\nNote: This is EXPERIMENTAL API. The functionality and the header file itself can be changed (including non-backward compatible changes) or removed.\n.SH \"SYNOPSIS\"\n.nf\n.B #include <memkind/internal/memkind_hugetlb.h>\n.sp\n.B Link with -lmemkind\n.sp\n.BI \"int memkind_hugetlb_check_available_2mb(struct memkind \" \"*kind\" );\n.br\n.BI \"int memkind_hugetlb_get_mmap_flags(struct memkind \" \"*kind\" \", int \" \"*flags\" );\n.br\n.BI \"void memkind_hugetlb_init_once(void);\"\n.br\n\n.SH DESCRIPTION\n.PP\nThe hugetlb memory memkind operations enable memory kinds which use\nthe Linux hugetlbfs file system.  For more information about the\nhugetlbfs see link below.\n.br\n.IR https://www.kernel.org/doc/Documentation/vm/hugetlbpage.txt\n.PP\n.BR memkind_hugetlb_check_available_2mb ()\nCheck if there are 2MB pages reserved in the default hugetlbfs.  If\nthe kind implements ops.get_mbind_nodemask(), then only the NUMA nodes\nset by the nodemask are checked, otherwise every NUMA node is checked.\n.PP\n.BR memkind_hugetlb_get_mmap_flags ()\nSets the flags for the\n.BR mmap ()\nsystem call such that the hugetlbfs is utilized for allocations.\n.PP\n.BR memkind_hugetlb_init_once ()\nThis function initializes MEMKIND_HUGETLB kind and it should not be called more than once.\n.BR Note:\n.BR memkind_hugetlb_init_once ()\nmay reserve some extra memory.\n.SH \"COPYRIGHT\"\nCopyright (C) 2014 - 2018 Intel Corporation. All rights reserved.\n.SH \"SEE ALSO\"\n.BR memkind (3),\n.BR memkind_arena (3),\n.BR memkind_default (3),\n.BR memkind_hbw (3),\n.BR memkind_pmem (3),\n.BR jemalloc (3),\n.BR mbind (2),\n.BR mmap (2)\n"
  },
  {
    "path": "deps/memkind/src/man/memkind_interleave.3",
    "content": ".\\\"\n.\\\" Copyright (C) 2016 - 2018 Intel Corporation.\n.\\\" All rights reserved.\n.\\\"\n.\\\" Redistribution and use in source and binary forms, with or without\n.\\\" modification, are permitted provided that the following conditions are met:\n.\\\" 1. Redistributions of source code must retain the above copyright notice(s),\n.\\\"    this list of conditions and the following disclaimer.\n.\\\" 2. Redistributions in binary form must reproduce the above copyright notice(s),\n.\\\"    this list of conditions and the following disclaimer in the documentation\n.\\\"    and/or other materials provided with the distribution.\n.\\\"\n.\\\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n.\\\" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n.\\\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n.\\\" EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n.\\\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n.\\\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n.\\\" PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n.\\\" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n.\\\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n.\\\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n.\\\"\n.TH \"MEMKIND_INTERLEAVE\" 3 \"2016-02-19\" \"Intel Corporation\" \"MEMKIND_INTERLEAVE\" \\\" -*- nroff -*-\n.SH \"NAME\"\nmemkind_interleave.h \\- interleave memory memkind operations.\n.br\nNote: This is EXPERIMENTAL API. The functionality and the header file itself can be changed (including non-backward compatible changes) or removed.\n.SH \"SYNOPSIS\"\n.nf\n.B #include <memkind/internal/memkind_interleave.h>\n.sp\n.B Link with -lmemkind\n.sp\n.BI \"void memkind_interleave_init_once(void);\"\n.br\n.SH DESCRIPTION\n.PP\n.BR memkind_interleave_init_once()\ninitializes MEMKIND_INTERLEAVE kind and it should not be called more than once.\n.BR Note:\n.BR memkind_interleave_init_once ()\nmay reserve some extra memory.\n.SH \"COPYRIGHT\"\nCopyright (C) 2016 - 2018 Intel Corporation. All rights reserved.\n.SH \"SEE ALSO\"\n.BR memkind (3),\n.BR memkind_arena (3),\n.BR memkind_default (3),\n.BR memkind_hbw (3),\n.BR memkind_pmem (3),\n.BR jemalloc (3),\n.BR mbind (2),\n.BR mmap (2)\n"
  },
  {
    "path": "deps/memkind/src/man/memkind_pmem.3",
    "content": ".\\\"\n.\\\" Copyright (C) 2014 - 2018 Intel Corporation.\n.\\\" All rights reserved.\n.\\\"\n.\\\" Redistribution and use in source and binary forms, with or without\n.\\\" modification, are permitted provided that the following conditions are met:\n.\\\" 1. Redistributions of source code must retain the above copyright notice(s),\n.\\\"    this list of conditions and the following disclaimer.\n.\\\" 2. Redistributions in binary form must reproduce the above copyright notice(s),\n.\\\"    this list of conditions and the following disclaimer in the documentation\n.\\\"    and/or other materials provided with the distribution.\n.\\\"\n.\\\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n.\\\" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n.\\\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n.\\\" EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n.\\\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n.\\\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n.\\\" PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n.\\\" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n.\\\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n.\\\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n.\\\"\n.TH \"MEMKIND_PMEM\" 3 \"2015-04-21\" \"Intel Corporation\" \"MEMKIND_PMEM\" \\\" -*- nroff -*-\n.SH \"NAME\"\nmemkind_pmem.h \\- file-backed memory memkind operations.\n.br\nNote: This is EXPERIMENTAL API. The functionality and the header file itself can be changed (including non-backward compatible changes) or removed.\n.SH \"SYNOPSIS\"\n.nf\n.B #include <memkind/internal/memkind_pmem.h>\n.sp\n.B Link with -lmemkind\n.sp\n.BI \"int memkind_pmem_create(struct memkind \" \"*kind\" \", struct memkind_ops \" \"*ops\" \", const char \" \"*name\" );\n.br\n.BI \"int memkind_pmem_destroy(struct memkind \" \"*kind\" );\n.br\n.BI \"void *memkind_pmem_mmap(struct memkind \" \"*kind\" \", void \" \"*addr\" \", size_t \" \"size\" );\n.br\n.BI \"int memkind_pmem_get_mmap_flags(struct memkind \" \"*kind\" \", int \" \"*flags\" );\n.br\n.SH DESCRIPTION\n.PP\nThe pmem memory memkind operations enable memory kinds built on memory-mapped\nfiles.  These support traditional\n.B volatile\nmemory allocation in a fashion similar to\n.BR libvmem (3)\nlibrary.  It uses the\n.BR mmap (2)\nsystem call to create a pool of volatile memory.  Such memory may have different\nattributes, depending on the file system containing the memory-mapped files.\n(See also\n.IR http://pmem.io/pmdk/libvmem ).\n.PP\nThe pmem memkinds are most useful when used with\n.I Direct Access\nstorage (DAX), which is memory-addressable persistent storage\nthat supports load/store access without being paged via the system page cache.\nA Persistent Memory-aware file system is typically used to provide this\ntype of access.\n.PP\nThe most convenient way to create pmem memkinds is to use\n.BR memkind_create_pmem ()\n(see\n.BR memkind (3)).\n.PP\n.BR memkind_pmem_create ()\nis an implementation of the memkind \"create\" operation for file-backed memory\nkinds.  This allocates a space for some pmem-specific metadata, then calls\n.BR memkind_arena_create ()\n(see\n.BR memkind_arena (3))\n.PP\n.BR memkind_pmem_destroy ()\nis an implementation of the memkind \"destroy\" operation for file-backed memory\nkinds.  This releases all of the resources\nallocated by\n.BR memkind_pmem_create ()\nand allows the file system space to be reclaimed.\n.PP\n.BR memkind_pmem_mmap ()\nallocates the file system space for a block of\n.I size\nbytes in the memory-mapped file associated with given kind.\nThe\n.I addr\nhint is ignored.  The return value is the address of mapped memory region or\n.B MAP_FAILED\nin the case of an error.\n.PP\n.BR memkind_pmem_get_mmap_flags ()\nsets\n.I flags\nto\n.BR \"MAP_SHARED\" .\nSee\n.BR mmap (2)\nfor more information about these flags.\n.TP\n.B MEMKIND_PMEM_CHUNK_SIZE\nThe size of the PMEM chunk size.\n.SH \"COPYRIGHT\"\nCopyright (C) 2015 - 2018 Intel Corporation. All rights reserved.\n.SH \"SEE ALSO\"\n.BR memkind (3),\n.BR memkind_arena (3),\n.BR memkind_default (3),\n.BR memkind_hbw (3),\n.BR memkind_hugetlb (3),\n.BR libvmem (3),\n.BR jemalloc (3),\n.BR mbind (2),\n.BR mmap (2)\n"
  },
  {
    "path": "deps/memkind/src/man/pmemallocator.3",
    "content": ".\\\"\n.\\\" Copyright (C) 2018 Intel Corporation.\n.\\\" All rights reserved.\n.\\\"\n.\\\" Redistribution and use in source and binary forms, with or without\n.\\\" modification, are permitted provided that the following conditions are met:\n.\\\" 1. Redistributions of source code must retain the above copyright notice(s),\n.\\\"    this list of conditions and the following disclaimer.\n.\\\" 2. Redistributions in binary form must reproduce the above copyright notice(s),\n.\\\"    this list of conditions and the following disclaimer in the documentation\n.\\\"    and/or other materials provided with the distribution.\n.\\\"\n.\\\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n.\\\" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n.\\\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n.\\\" EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n.\\\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n.\\\" LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n.\\\" PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n.\\\" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n.\\\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n.\\\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n.\\\"\n.TH \"PMEMALLOCATOR\" 3 \"2018-09-13\" \"Intel Corporation\" \"PMEMALLOCATOR\" \\\" -*- nroff -*-\n.SH \"NAME\"\npmem::allocator<T> \\- The C++ allocator compatible with the C++ standard library allocator concepts\n.br\nNote: This is EXPERIMENTAL API. The functionality and the header file itself can be changed (including non-backward compatible changes), or removed.\n.SH \"SYNOPSIS\"\n.nf\n.B #include <pmem_allocator.h>\n.sp\n.B Link with -lmemkind\n.sp\n.B pmem::allocator(const char *dir, size_t max_size)\n.br\n.B pmem::allocator(const std::string& dir, size_t max_size)\n.br\n.B template <typename U> pmem::allocator<T>::allocator(const pmem::allocator<U>&) noexcept\n.br\n.B template <typename U> pmem::allocator(const allocator<U>&& other) noexcept\n.br\n.B pmem::allocator<T>::~allocator()\n.br\n.B T* pmem::allocator<T>::allocate(std::size_t n) const\n.br\n.B void pmem::allocator<T>::deallocate(T* p, std::size_t n) const\n.br\n.B template <class U, class... Args> void pmem::allocator<T>::construct(U* p, Args... args) const\n.br\n.B void pmem::allocator<T>::destroy(T* p) const\n.fi\n.SH \"DESCRIPTION\"\nThe pmem::allocator<T> is intended to be used with STL containers to allocate persistent memory. Memory management is based on memkind_pmem (memkind library). Refer memkind_pmem(3) and memkind(3) man page for more details.\n.PP\nAll public member types and functions corresponds to standard library allocator concepts and definitions. The current implementation supports C++11 standard.\n.PP\nTemplate arguments:\n.br\nT is an object type aliased by value_type.\n.br\nU is an object type.\n.PP\nNote:\n.br\nT* pmem::allocator<T>::allocate(std::size_t n)\nallocates persistent memory memory using\n.IR \"memkind_malloc()\".\nThrow\n.I std::bad_alloc\nwhen:\n.br\n.IR\t\tn \" = 0,\"\n.br\n\tor there is not enough memory to satisfy the request.\n\n.PP\nvoid pmem::allocator<T>::deallocate(T* p, std::size_t n) deallocates memory associated with pointer returned by\n.I allocate()\nusing\n.IR \"memkind_free()\".\n.PP\nTo find out more about\n.I \"memkind_malloc()\nand\n.I \"memkind_free()\nread memkind(3) man page.\n\n\n.SH \"NOTES\"\nThe\n.I pmem::allocator<T>\nbehavior depends on the heap manager used by memkind. Refer memkind(3) man page for more details.\n.SH \"COPYRIGHT\"\nCopyright (C) 2018 Intel Corporation. All rights reserved.\n.SH \"SEE ALSO\"\n.BR memkind_pmem(3),\n.BR memkind (3)\n"
  },
  {
    "path": "deps/memkind/src/memkind.spec.mk",
    "content": "#\n#  Copyright (C) 2014 - 2018 Intel Corporation.\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#  1. Redistributions of source code must retain the above copyright notice(s),\n#     this list of conditions and the following disclaimer.\n#  2. Redistributions in binary form must reproduce the above copyright notice(s),\n#     this list of conditions and the following disclaimer in the documentation\n#     and/or other materials provided with the distribution.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n#  EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n#  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n#\n\n# targets for building rpm\nversion ?= 0.0.0\nrelease ?= 1\narch = $(shell uname -p)\nname ?= memkind\n\nrpm: $(name)-$(version).tar.gz\n\trpmbuild $(rpmbuild_flags) $^ -ta\n\nmemkind-$(version).spec:\n\t@echo \"$$memkind_spec\" > $@\n\tcat ChangeLog >> $@\n\n.PHONY: rpm\n\ndefine memkind_spec\nSummary: User Extensible Heap Manager\nName: $(name)\nVersion: $(version)\nRelease: $(release)\nLicense: BSD-2-Clause\nGroup: System Environment/Libraries\nURL: http://memkind.github.io/memkind\nBuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)\nBuildRequires: automake libtool gcc-c++ unzip\n%if %{defined suse_version}\nBuildRequires: libnuma-devel\n%else\nBuildRequires: numactl-devel\n%endif\n\nPrefix: %{_prefix}\nPrefix: %{_unitdir}\nObsoletes: memkind\nProvides: memkind libmemkind0\n\n%define namespace memkind\n\n%if %{defined suse_version}\n%define docdir %{_defaultdocdir}/%{namespace}\n%else\n%define docdir %{_defaultdocdir}/%{namespace}-%{version}\n%endif\n\n# x86_64 is the only arch memkind will build due to its\n# current dependency on SSE4.2 CRC32 instruction which\n# is used to compute thread local storage arena mappings\n# with polynomial accumulations via GCC's intrinsic _mm_crc32_u64\n# For further info check:\n# - /lib/gcc/<target>/<version>/include/smmintrin.h\n# - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=36095\n# - http://en.wikipedia.org/wiki/SSE4\nExclusiveArch: x86_64\n\n# default values if version is a tagged release on github\n%{!?commit: %define commit %{version}}\n%{!?buildsubdir: %define buildsubdir %{namespace}-%{commit}}\nSource0: https://github.com/%{namespace}/%{namespace}/archive/v%{commit}/%{buildsubdir}.tar.gz\n\n%description\nThe memkind library is an user extensible heap manager built on top of\njemalloc which enables control of memory characteristics and a\npartitioning of the heap between kinds of memory. The kinds of memory\nare defined by operating system memory policies that have been applied\nto virtual address ranges. Memory characteristics supported by\nmemkind without user extension include control of NUMA and page size\nfeatures. The jemalloc non-standard interface has been extended to\nenable specialized arenas to make requests for virtual memory from the\noperating system through the memkind partition interface. Through the\nother memkind interfaces the user can control and extend memory\npartition features and allocate memory while selecting enabled\nfeatures. This software is being made available for early evaluation.\nFeedback on design or implementation is greatly appreciated.\n\n%package devel\nSummary: Memkind User Extensible Heap Manager development lib and tools\nGroup: Development/Libraries\nRequires: %{name} = %{version}-%{release}\nObsoletes: memkind-devel\nProvides: memkind-devel\n\n%description devel\nInstall header files and development aids to link memkind library into\napplications.\n\n%package tests\nSummary: Extention to libnuma for kinds of memory - validation\nGroup: Validation/Libraries\nRequires: %{name} = %{version}-%{release}\n\n%description tests\nmemkind functional tests\n\n%prep\n%setup -q -a 0 -n $(name)-%{version}\n\n%build\n# It is required that we configure and build the jemalloc subdirectory\n# before we configure and start building the top level memkind directory.\n# To ensure the memkind build step is able to discover the output\n# of the jemalloc build we must create an 'obj' directory, and build\n# from within that directory.\n\ncd %{_builddir}/%{buildsubdir}\necho %{version} > %{_builddir}/%{buildsubdir}/VERSION\n./build.sh --prefix=%{_prefix} --includedir=%{_includedir} --libdir=%{_libdir} \\\n           --bindir=%{_bindir} --docdir=%{_docdir}/%{namespace} --mandir=%{_mandir} --sbindir=%{_sbindir}\n\n%install\ncd %{_builddir}/%{buildsubdir}\n%{__make} DESTDIR=%{buildroot} install\n%{__install} -d %{buildroot}$(memkind_test_dir)\n%{__install} -d %{buildroot}/%{_unitdir}\n%{__install} -d %{buildroot}/$(memkind_test_dir)/python_framework\n%{__install} test/.libs/* test/*.sh test/*.ts test/*.py %{buildroot}$(memkind_test_dir)\n%{__install} test/python_framework/*.py %{buildroot}/$(memkind_test_dir)/python_framework\nrm -f %{buildroot}$(memkind_test_dir)/libautohbw.*\nrm -f %{buildroot}/%{_libdir}/lib%{namespace}.{l,}a\nrm -f %{buildroot}/%{_libdir}/libautohbw.{l,}a\n\n%pre\n\n%post\n/sbin/ldconfig\n\n%preun\n\n%postun\n/sbin/ldconfig\n\n%files\n%defattr(-,root,root,-)\n%license %{_docdir}/%{namespace}/COPYING\n%doc %{_docdir}/%{namespace}/README\n%doc %{_docdir}/%{namespace}/VERSION\n%dir %{_docdir}/%{namespace}\n%{_libdir}/lib%{namespace}.so.*\n%{_libdir}/libautohbw.so.*\n%{_bindir}/%{namespace}-hbw-nodes\n\n%define internal_include memkind/internal\n\n%files devel\n%defattr(-,root,root,-)\n%{_includedir}\n%{_includedir}/hbwmalloc.h\n%{_includedir}/hbw_allocator.h\n%{_includedir}/pmem_allocator.h\n%{_libdir}/lib%{namespace}.so\n%{_libdir}/libautohbw.so\n%{_includedir}/%{namespace}.h\n%{_includedir}/%{internal_include}\n%{_includedir}/%{internal_include}/%{namespace}*.h\n%{_mandir}/man1/memkind-hbw-nodes.1.*\n%{_mandir}/man3/hbwmalloc.3.*\n%{_mandir}/man3/hbwallocator.3.*\n%{_mandir}/man3/pmemallocator.3.*\n%{_mandir}/man3/%{namespace}*.3.*\n%{_mandir}/man7/autohbw.7.*\n\n%exclude %{_includedir}/%{internal_include}/%{namespace}_log.h\n\n%files tests\n%defattr(-,root,root,-)\n$(memkind_test_dir)/all_tests\n$(memkind_test_dir)/environ_err_hbw_malloc_test\n$(memkind_test_dir)/decorator_test\n$(memkind_test_dir)/locality_test\n$(memkind_test_dir)/freeing_memory_segfault_test\n$(memkind_test_dir)/gb_page_tests_bind_policy\n$(memkind_test_dir)/filter_memkind\n$(memkind_test_dir)/hello_hbw\n$(memkind_test_dir)/hello_memkind\n$(memkind_test_dir)/hello_memkind_debug\n$(memkind_test_dir)/memkind_allocated\n$(memkind_test_dir)/autohbw_candidates\n${memkind_test_dir}/pmem_kinds\n${memkind_test_dir}/pmem_malloc\n${memkind_test_dir}/pmem_malloc_unlimited\n${memkind_test_dir}/pmem_usable_size\n${memkind_test_dir}/pmem_alignment\n${memkind_test_dir}/pmem_and_default_kind\n${memkind_test_dir}/pmem_multithreads\n${memkind_test_dir}/pmem_multithreads_onekind\n$(memkind_test_dir)/pmem_free_with_unknown_kind\n${memkind_test_dir}/pmem_cpp_allocator\n${memkind_test_dir}/allocator_perf_tool_tests\n${memkind_test_dir}/perf_tool\n${memkind_test_dir}/autohbw_test_helper\n${memkind_test_dir}/trace_mechanism_test_helper\n$(memkind_test_dir)/memkind-afts.ts\n$(memkind_test_dir)/memkind-afts-ext.ts\n$(memkind_test_dir)/memkind-slts.ts\n$(memkind_test_dir)/memkind-perf.ts\n$(memkind_test_dir)/memkind-perf-ext.ts\n$(memkind_test_dir)/memkind-pytests.ts\n$(memkind_test_dir)/test.sh\n$(memkind_test_dir)/hbw_detection_test.py\n$(memkind_test_dir)/autohbw_test.py\n$(memkind_test_dir)/trace_mechanism_test.py\n$(memkind_test_dir)/python_framework\n$(memkind_test_dir)/python_framework/cmd_helper.py\n$(memkind_test_dir)/python_framework/huge_page_organizer.py\n$(memkind_test_dir)/python_framework/__init__.py\n$(memkind_test_dir)/draw_plots.py\n$(memkind_test_dir)/run_alloc_benchmark.sh\n$(memkind_test_dir)/alloc_benchmark_hbw\n$(memkind_test_dir)/alloc_benchmark_glibc\n$(memkind_test_dir)/alloc_benchmark_tbb\n$(memkind_test_dir)/alloc_benchmark_pmem\n\n%exclude $(memkind_test_dir)/*.pyo\n%exclude $(memkind_test_dir)/*.pyc\n%exclude $(memkind_test_dir)/python_framework/*.pyo\n%exclude $(memkind_test_dir)/python_framework/*.pyc\n\n%changelog\nendef\n\nexport memkind_spec\n"
  },
  {
    "path": "deps/memkind/src/src/Makefile.mk",
    "content": "#\n#  Copyright (C) 2018 Intel Corporation.\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#  1. Redistributions of source code must retain the above copyright notice(s),\n#     this list of conditions and the following disclaimer.\n#  2. Redistributions in binary form must reproduce the above copyright notice(s),\n#     this list of conditions and the following disclaimer in the documentation\n#     and/or other materials provided with the distribution.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n#  EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n#  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n#\n\nclean-local: src-clean\n\nsrc-clean:\n\trm -f src/*.gcno\n"
  },
  {
    "path": "deps/memkind/src/src/hbwmalloc.c",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <hbwmalloc.h>\n#include <memkind.h>\n#include <memkind/internal/memkind_private.h>\n#include <memkind/internal/memkind_hbw.h>\n#include <memkind/internal/memkind_log.h>\n\n#include <stdlib.h>\n#include <stdio.h>\n#include <pthread.h>\n#include <errno.h>\n#include <numa.h>\n#include <numaif.h>\n#include <unistd.h>\n#include <stdint.h>\n\nstatic hbw_policy_t hbw_policy_g = HBW_POLICY_PREFERRED;\nstatic pthread_once_t hbw_policy_once_g = PTHREAD_ONCE_INIT;\n\nstatic void hbw_policy_bind_init(void)\n{\n    hbw_policy_g = HBW_POLICY_BIND;\n}\n\nstatic void hbw_policy_bind_all_init(void)\n{\n    hbw_policy_g = HBW_POLICY_BIND_ALL;\n}\n\nstatic void hbw_policy_preferred_init(void)\n{\n    hbw_policy_g = HBW_POLICY_PREFERRED;\n}\n\nstatic void hbw_policy_interleave_init(void)\n{\n    hbw_policy_g = HBW_POLICY_INTERLEAVE;\n}\n\n// This function is intended to be called once per pagesize\n// Getting kind should be done using hbw_get_kind() defined below\nstatic memkind_t hbw_choose_kind(hbw_pagesize_t pagesize)\n{\n    memkind_t result = NULL;\n\n    hbw_set_policy(hbw_policy_g);\n\n    int policy = hbw_get_policy();\n\n    // PREFERRED policy have separate handling cause it can fallback\n    // to non-HBW kinds in case of HBW absence\n    if (policy != HBW_POLICY_PREFERRED ) {\n        switch (pagesize) {\n            case HBW_PAGESIZE_2MB:\n                if(policy == HBW_POLICY_BIND_ALL) {\n                    result = MEMKIND_HBW_ALL_HUGETLB;\n                } else {\n                    result = MEMKIND_HBW_HUGETLB;\n                }\n                break;\n            case HBW_PAGESIZE_1GB:\n            case HBW_PAGESIZE_1GB_STRICT:\n                result = MEMKIND_HBW_GBTLB;\n                break;\n            default:\n                if (policy == HBW_POLICY_BIND) {\n                    result = MEMKIND_HBW;\n                } else if (policy == HBW_POLICY_BIND_ALL) {\n                    result = MEMKIND_HBW_ALL;\n                } else {\n                    result = MEMKIND_HBW_INTERLEAVE;\n                }\n                break;\n        }\n    } else if (memkind_check_available(MEMKIND_HBW) == 0) {\n        switch (pagesize) {\n            case HBW_PAGESIZE_2MB:\n                result = MEMKIND_HBW_PREFERRED_HUGETLB;\n                break;\n            case HBW_PAGESIZE_1GB:\n            case HBW_PAGESIZE_1GB_STRICT:\n                result = MEMKIND_HBW_PREFERRED_GBTLB;\n                break;\n            default:\n                result = MEMKIND_HBW_PREFERRED;\n                break;\n        }\n    } else {\n        switch (pagesize) {\n            case HBW_PAGESIZE_2MB:\n                result = MEMKIND_HUGETLB;\n                break;\n            case HBW_PAGESIZE_1GB:\n            case HBW_PAGESIZE_1GB_STRICT:\n                result = MEMKIND_GBTLB;\n                break;\n            default:\n                result = MEMKIND_DEFAULT;\n                break;\n        }\n    }\n    return result;\n}\n\nstatic memkind_t pagesize_kind[HBW_PAGESIZE_MAX_VALUE];\nstatic inline memkind_t hbw_get_kind(hbw_pagesize_t pagesize)\n{\n    if(pagesize_kind[pagesize] == NULL) {\n        pagesize_kind[pagesize] = hbw_choose_kind(pagesize);\n    }\n    return pagesize_kind[pagesize];\n}\n\n\nMEMKIND_EXPORT hbw_policy_t hbw_get_policy(void)\n{\n    return hbw_policy_g;\n}\n\nMEMKIND_EXPORT int hbw_set_policy(hbw_policy_t mode)\n{\n    switch(mode) {\n        case HBW_POLICY_PREFERRED:\n            pthread_once(&hbw_policy_once_g, hbw_policy_preferred_init);\n            break;\n        case HBW_POLICY_BIND:\n            pthread_once(&hbw_policy_once_g, hbw_policy_bind_init);\n            break;\n        case HBW_POLICY_BIND_ALL:\n            pthread_once(&hbw_policy_once_g, hbw_policy_bind_all_init);\n            break;\n        case HBW_POLICY_INTERLEAVE:\n            pthread_once(&hbw_policy_once_g, hbw_policy_interleave_init);\n            break;\n        default:\n            return EINVAL;\n    }\n\n    if (mode != hbw_policy_g) {\n        return EPERM;\n    }\n\n    return 0;\n}\n\nMEMKIND_EXPORT int hbw_check_available(void)\n{\n    return  (memkind_check_available(MEMKIND_HBW) == 0) ? 0 : ENODEV;\n}\n\nstatic inline void hbw_touch_page(void *addr)\n{\n    volatile char *temp_ptr = (volatile char *) addr;\n    char value = temp_ptr[0];\n    temp_ptr[0] = value;\n}\n\nMEMKIND_EXPORT int hbw_verify_memory_region(void *addr, size_t size, int flags)\n{\n    /*\n     * if size is invalid, flags have unsupported bit set or if addr is NULL.\n     */\n    if (addr == NULL || size == 0 || flags & ~HBW_TOUCH_PAGES) {\n        return EINVAL;\n    }\n\n    /*\n     * 4KB is the smallest pagesize. When pagesize is bigger, pages are verified more than once\n     */\n    const size_t page_size = sysconf(_SC_PAGESIZE);\n    const size_t page_mask = ~(page_size-1);\n\n    /*\n     * block size should be power of two to enable compiler optimizations\n     */\n    const unsigned block_size = 64;\n\n    char *end = addr + size;\n    char *aligned_beg = (char *)((uintptr_t)addr & page_mask);\n    nodemask_t nodemask;\n    struct bitmask expected_nodemask = {NUMA_NUM_NODES, nodemask.n};\n\n    memkind_hbw_all_get_mbind_nodemask(NULL, expected_nodemask.maskp,\n                                       expected_nodemask.size);\n\n    while(aligned_beg < end) {\n        int nodes[block_size];\n        void *pages[block_size];\n        int i = 0, page_count = 0;\n        char *iter_end = aligned_beg + block_size*page_size;\n\n        if (iter_end > end) {\n            iter_end = end;\n        }\n\n        while (aligned_beg < iter_end) {\n            if (flags & HBW_TOUCH_PAGES) {\n                hbw_touch_page(aligned_beg);\n            }\n            pages[page_count++] = aligned_beg;\n            aligned_beg += page_size;\n        }\n\n        if (move_pages(0, page_count, pages, NULL, nodes, MPOL_MF_MOVE)) {\n            return EFAULT;\n        }\n\n        for (i = 0; i < page_count; i++) {\n            /*\n             * negative value of nodes[i] indicates that move_pages could not establish\n             * page location, e.g. addr is not pointing to valid virtual mapping\n             */\n            if(nodes[i] < 0) {\n                return -1;\n            }\n            /*\n             * if nodes[i] is not present in expected_nodemask then\n             * physical memory backing page is not hbw\n             */\n            if (!numa_bitmask_isbitset(&expected_nodemask, nodes[i])) {\n                return -1;\n            }\n        }\n    }\n\n    return 0;\n}\n\nMEMKIND_EXPORT void *hbw_malloc(size_t size)\n{\n    return memkind_malloc(hbw_get_kind(HBW_PAGESIZE_4KB), size);\n}\n\nMEMKIND_EXPORT void *hbw_calloc(size_t num, size_t size)\n{\n    return memkind_calloc(hbw_get_kind(HBW_PAGESIZE_4KB), num, size);\n}\n\nMEMKIND_EXPORT int hbw_posix_memalign(void **memptr, size_t alignment,\n                                      size_t size)\n{\n    return memkind_posix_memalign(hbw_get_kind(HBW_PAGESIZE_4KB), memptr, alignment,\n                                  size);\n}\n\nMEMKIND_EXPORT int hbw_posix_memalign_psize(void **memptr, size_t alignment,\n                                            size_t size,\n                                            hbw_pagesize_t pagesize)\n{\n    if (pagesize == HBW_PAGESIZE_1GB_STRICT && size % (1 << 30)) {\n        return EINVAL;\n    }\n\n    if((pagesize == HBW_PAGESIZE_2MB ||\n        pagesize == HBW_PAGESIZE_1GB_STRICT ||\n        pagesize == HBW_PAGESIZE_1GB) &&\n       hbw_get_policy() == HBW_POLICY_INTERLEAVE) {\n\n        log_err(\"HBW_POLICY_INTERLEAVE is unsupported with used page size!\");\n        return EINVAL;\n    }\n\n    return memkind_posix_memalign(hbw_get_kind(pagesize), memptr, alignment, size);\n}\n\nMEMKIND_EXPORT void *hbw_realloc(void *ptr, size_t size)\n{\n    return memkind_realloc(hbw_get_kind(HBW_PAGESIZE_4KB), ptr, size);\n}\n\nMEMKIND_EXPORT void hbw_free(void *ptr)\n{\n    memkind_free(0, ptr);\n}\n"
  },
  {
    "path": "deps/memkind/src/src/heap_manager.c",
    "content": "/*\n * Copyright (C) 2017 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind/internal/heap_manager.h>\n#include <memkind/internal/tbb_wrapper.h>\n#include <memkind/internal/memkind_arena.h>\n\n#include <stdio.h>\n#include <pthread.h>\n#include <string.h>\n\nstatic struct heap_manager_ops *heap_manager_g;\n\npthread_once_t heap_manager_init_once_g = PTHREAD_ONCE_INIT;\n\nstruct heap_manager_ops {\n    void (*init)(struct memkind *kind);\n    void (*heap_manager_free)(struct memkind *kind, void *ptr);\n};\n\nstruct heap_manager_ops arena_heap_manager_g = {\n    .init = memkind_arena_init,\n    .heap_manager_free = memkind_arena_free\n};\n\nstruct heap_manager_ops tbb_heap_manager_g = {\n    .init = tbb_initialize,\n    .heap_manager_free = tbb_pool_free\n};\n\nstatic void set_heap_manager()\n{\n    heap_manager_g = &arena_heap_manager_g;\n    const char *env = getenv(\"MEMKIND_HEAP_MANAGER\");\n    if(env && strcmp(env, \"TBB\") == 0) {\n        heap_manager_g = &tbb_heap_manager_g;\n    }\n}\n\nstatic inline struct heap_manager_ops *get_heap_manager()\n{\n    pthread_once(&heap_manager_init_once_g, set_heap_manager);\n    return heap_manager_g;\n}\n\nvoid heap_manager_init(struct memkind *kind)\n{\n    get_heap_manager()->init(kind);\n}\n\nvoid heap_manager_free(struct memkind *kind, void *ptr)\n{\n    get_heap_manager()->heap_manager_free(kind, ptr);\n}\n"
  },
  {
    "path": "deps/memkind/src/src/memkind-hbw-nodes.c",
    "content": "/*\n * Copyright (C) 2016 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind/internal/memkind_hbw.h>\n\n#include <numa.h>\n#include <stdio.h>\n\n#define MAX_ARG_LEN 8\n\nconst char *help_message =\n    \"\\n\"\n    \"NAME\\n\"\n    \"    memkind-hbw-nodes - Print comma separated list of high bandwidth nodes.\\n\"\n    \"\\n\"\n    \"SYNOPSIS\\n\"\n    \"    memkind-hbw-nodes -h | --help\\n\"\n    \"        Print this help message.\\n\"\n    \"\\n\"\n    \"DESCRIPTION\\n\"\n    \"    Prints a comma separated list of high bandwidth NUMA nodes\\n\"\n    \"    that can be used with the numactl --membind option.\\n\"\n    \"\\n\"\n    \"EXIT STATUS\\n\"\n    \"    Return code is :\\n\"\n    \"        0 on success\\n\"\n    \"        1 on failure\\n\"\n    \"        2 on invalid argument\\n\"\n    \"\\n\"\n    \"COPYRIGHT\\n\"\n    \"    Copyright 2016 Intel Corporation All Rights Reserved.\\n\"\n    \"\\n\"\n    \"AUTHORS\\n\"\n    \"    Krzysztof Kulakowski\\n\"\n    \"\\n\"\n    \"SEE ALSO\\n\"\n    \"    hbwmalloc(3), memkind(3)\\n\"\n    \"\\n\";\n\nextern unsigned int numa_bitmask_weight(const struct bitmask *bmp );\n\nint print_hbw_nodes()\n{\n    int i, j = 0;\n\n    nodemask_t nodemask;\n    struct bitmask nodemask_bm = {NUMA_NUM_NODES, nodemask.n};\n    numa_bitmask_clearall(&nodemask_bm);\n\n    //WARNING: code below is usage of memkind experimental API which may be changed in future\n    if(memkind_hbw_all_get_mbind_nodemask(NULL, nodemask.n, NUMA_NUM_NODES) != 0) {\n        return 1;\n    }\n\n    for(i=0; i<NUMA_NUM_NODES; i++) {\n        if(numa_bitmask_isbitset(&nodemask_bm, i)) {\n            printf(\"%d%s\", i, (++j == numa_bitmask_weight(&nodemask_bm)) ? \"\" : \",\");\n        }\n    }\n    printf(\"\\n\");\n    return 0;\n}\n\nint main(int argc, char *argv[])\n{\n    if(argc == 1) {\n        return print_hbw_nodes();\n    } else if ((argc == 2) && (strncmp(argv[1], \"-h\", MAX_ARG_LEN) == 0 ||\n                               strncmp(argv[1], \"--help\", MAX_ARG_LEN) == 0)) {\n        printf(\"%s\", help_message);\n        return 2;\n    }\n\n    printf(\"ERROR: Unknown option %s. More info with \\\"%s --help\\\".\\n\", argv[1],\n           argv[0]);\n    return 2;\n}\n"
  },
  {
    "path": "deps/memkind/src/src/memkind.c",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#define MEMKIND_VERSION_MAJOR 1\n#define MEMKIND_VERSION_MINOR 8\n#define MEMKIND_VERSION_PATCH 0\n\n#include <memkind.h>\n#include <memkind/internal/memkind_default.h>\n#include <memkind/internal/memkind_hugetlb.h>\n#include <memkind/internal/memkind_arena.h>\n#include <memkind/internal/memkind_hbw.h>\n#include <memkind/internal/memkind_regular.h>\n#include <memkind/internal/memkind_gbtlb.h>\n#include <memkind/internal/memkind_pmem.h>\n#include <memkind/internal/memkind_interleave.h>\n#include <memkind/internal/memkind_private.h>\n#include <memkind/internal/memkind_log.h>\n#include <memkind/internal/tbb_wrapper.h>\n#include <memkind/internal/heap_manager.h>\n\n#include \"config.h\"\n\n#include <numa.h>\n#include <sys/param.h>\n#include <sys/mman.h>\n#include <stdint.h>\n#include <stdlib.h>\n#include <stdio.h>\n#include <string.h>\n#include <assert.h>\n#include <pthread.h>\n#include <errno.h>\n#include <signal.h>\n#include <fcntl.h>\n#include <unistd.h>\n#include <jemalloc/jemalloc.h>\n\n/* Clear bits in x, but only this specified in mask. */\n#define CLEAR_BIT(x, mask) ((x) &= (~(mask)))\n\nextern struct memkind_ops MEMKIND_HBW_GBTLB_OPS;\nextern struct memkind_ops MEMKIND_HBW_PREFERRED_GBTLB_OPS;\nextern struct memkind_ops MEMKIND_GBTLB_OPS;\n\nstatic struct memkind MEMKIND_DEFAULT_STATIC = {\n    .ops =  &MEMKIND_DEFAULT_OPS,\n    .partition = MEMKIND_PARTITION_DEFAULT,\n    .name = \"memkind_default\",\n    .init_once = PTHREAD_ONCE_INIT,\n};\n\nstatic struct memkind MEMKIND_HUGETLB_STATIC = {\n    .ops = &MEMKIND_HUGETLB_OPS,\n    .partition = MEMKIND_PARTITION_HUGETLB,\n    .name = \"memkind_hugetlb\",\n    .init_once = PTHREAD_ONCE_INIT,\n};\n\nstatic struct memkind MEMKIND_INTERLEAVE_STATIC = {\n    .ops = &MEMKIND_INTERLEAVE_OPS,\n    .partition = MEMKIND_PARTITION_INTERLEAVE,\n    .name = \"memkind_interleave\",\n    .init_once = PTHREAD_ONCE_INIT,\n};\n\nstatic struct memkind MEMKIND_HBW_STATIC = {\n    .ops = &MEMKIND_HBW_OPS,\n    .partition = MEMKIND_PARTITION_HBW,\n    .name = \"memkind_hbw\",\n    .init_once = PTHREAD_ONCE_INIT,\n};\n\nstatic struct memkind MEMKIND_HBW_ALL_STATIC = {\n    .ops = &MEMKIND_HBW_ALL_OPS,\n    .partition = MEMKIND_PARTITION_HBW_ALL,\n    .name = \"memkind_hbw_all\",\n    .init_once = PTHREAD_ONCE_INIT,\n};\n\nstatic struct memkind MEMKIND_HBW_PREFERRED_STATIC = {\n    .ops = &MEMKIND_HBW_PREFERRED_OPS,\n    .partition = MEMKIND_PARTITION_HBW_PREFERRED,\n    .name = \"memkind_hbw_preferred\",\n    .init_once = PTHREAD_ONCE_INIT,\n};\n\nstatic struct memkind MEMKIND_HBW_HUGETLB_STATIC = {\n    .ops = &MEMKIND_HBW_HUGETLB_OPS,\n    .partition = MEMKIND_PARTITION_HBW_HUGETLB,\n    .name = \"memkind_hbw_hugetlb\",\n    .init_once = PTHREAD_ONCE_INIT,\n};\n\nstatic struct memkind MEMKIND_HBW_ALL_HUGETLB_STATIC = {\n    .ops = &MEMKIND_HBW_ALL_HUGETLB_OPS,\n    .partition = MEMKIND_PARTITION_HBW_ALL_HUGETLB,\n    .name = \"memkind_hbw_all_hugetlb\",\n    .init_once = PTHREAD_ONCE_INIT,\n};\n\nstatic struct memkind MEMKIND_HBW_PREFERRED_HUGETLB_STATIC = {\n    .ops = &MEMKIND_HBW_PREFERRED_HUGETLB_OPS,\n    .partition = MEMKIND_PARTITION_HBW_PREFERRED_HUGETLB,\n    .name = \"memkind_hbw_preferred_hugetlb\",\n    .init_once = PTHREAD_ONCE_INIT,\n};\n\nstatic struct memkind MEMKIND_HBW_GBTLB_STATIC = {\n    .ops = &MEMKIND_HBW_GBTLB_OPS,\n    .partition = MEMKIND_PARTITION_HBW_GBTLB,\n    .name = \"memkind_hbw_gbtlb\",\n    .init_once = PTHREAD_ONCE_INIT,\n};\n\nstatic struct memkind MEMKIND_HBW_PREFERRED_GBTLB_STATIC = {\n    .ops = &MEMKIND_HBW_PREFERRED_GBTLB_OPS,\n    .partition = MEMKIND_PARTITION_HBW_PREFERRED_GBTLB,\n    .name = \"memkind_hbw_preferred_gbtlb\",\n    .init_once = PTHREAD_ONCE_INIT,\n};\n\nstatic struct memkind MEMKIND_GBTLB_STATIC = {\n    .ops = &MEMKIND_GBTLB_OPS,\n    .partition = MEMKIND_PARTITION_GBTLB,\n    .name = \"memkind_gbtlb\",\n    .init_once = PTHREAD_ONCE_INIT,\n};\n\nstatic struct memkind MEMKIND_HBW_INTERLEAVE_STATIC = {\n    .ops = &MEMKIND_HBW_INTERLEAVE_OPS,\n    .partition = MEMKIND_PARTITION_HBW_INTERLEAVE,\n    .name = \"memkind_hbw_interleave\",\n    .init_once = PTHREAD_ONCE_INIT,\n};\n\nstatic struct memkind MEMKIND_REGULAR_STATIC = {\n    .ops = &MEMKIND_REGULAR_OPS,\n    .partition = MEMKIND_PARTITION_REGULAR,\n    .name = \"memkind_regular\",\n    .init_once = PTHREAD_ONCE_INIT,\n};\n\nMEMKIND_EXPORT struct memkind *MEMKIND_DEFAULT = &MEMKIND_DEFAULT_STATIC;\nMEMKIND_EXPORT struct memkind *MEMKIND_HUGETLB = &MEMKIND_HUGETLB_STATIC;\nMEMKIND_EXPORT struct memkind *MEMKIND_INTERLEAVE = &MEMKIND_INTERLEAVE_STATIC;\nMEMKIND_EXPORT struct memkind *MEMKIND_HBW = &MEMKIND_HBW_STATIC;\nMEMKIND_EXPORT struct memkind *MEMKIND_HBW_ALL = &MEMKIND_HBW_ALL_STATIC;\nMEMKIND_EXPORT struct memkind *MEMKIND_HBW_PREFERRED =\n        &MEMKIND_HBW_PREFERRED_STATIC;\nMEMKIND_EXPORT struct memkind *MEMKIND_HBW_HUGETLB =\n        &MEMKIND_HBW_HUGETLB_STATIC;\nMEMKIND_EXPORT struct memkind *MEMKIND_HBW_ALL_HUGETLB =\n        &MEMKIND_HBW_ALL_HUGETLB_STATIC;\nMEMKIND_EXPORT struct memkind *MEMKIND_HBW_PREFERRED_HUGETLB =\n        &MEMKIND_HBW_PREFERRED_HUGETLB_STATIC;\nMEMKIND_EXPORT struct memkind *MEMKIND_HBW_GBTLB = &MEMKIND_HBW_GBTLB_STATIC;\nMEMKIND_EXPORT struct memkind *MEMKIND_HBW_PREFERRED_GBTLB =\n        &MEMKIND_HBW_PREFERRED_GBTLB_STATIC;\nMEMKIND_EXPORT struct memkind *MEMKIND_GBTLB = &MEMKIND_GBTLB_STATIC;\nMEMKIND_EXPORT struct memkind *MEMKIND_HBW_INTERLEAVE =\n        &MEMKIND_HBW_INTERLEAVE_STATIC;\nMEMKIND_EXPORT struct memkind *MEMKIND_REGULAR = &MEMKIND_REGULAR_STATIC;\n\nstruct memkind_registry {\n    struct memkind *partition_map[MEMKIND_MAX_KIND];\n    int num_kind;\n    pthread_mutex_t lock;\n};\n\nstatic struct memkind_registry memkind_registry_g = {\n    {\n        [MEMKIND_PARTITION_DEFAULT] = &MEMKIND_DEFAULT_STATIC,\n        [MEMKIND_PARTITION_HBW] = &MEMKIND_HBW_STATIC,\n        [MEMKIND_PARTITION_HBW_PREFERRED] = &MEMKIND_HBW_PREFERRED_STATIC,\n        [MEMKIND_PARTITION_HBW_HUGETLB] = &MEMKIND_HBW_HUGETLB_STATIC,\n        [MEMKIND_PARTITION_HBW_PREFERRED_HUGETLB] = &MEMKIND_HBW_PREFERRED_HUGETLB_STATIC,\n        [MEMKIND_PARTITION_HUGETLB] = &MEMKIND_HUGETLB_STATIC,\n        [MEMKIND_PARTITION_HBW_GBTLB] = &MEMKIND_HBW_GBTLB_STATIC,\n        [MEMKIND_PARTITION_HBW_PREFERRED_GBTLB] = &MEMKIND_HBW_PREFERRED_GBTLB_STATIC,\n        [MEMKIND_PARTITION_GBTLB] = &MEMKIND_GBTLB_STATIC,\n        [MEMKIND_PARTITION_HBW_INTERLEAVE] = &MEMKIND_HBW_INTERLEAVE_STATIC,\n        [MEMKIND_PARTITION_INTERLEAVE] = &MEMKIND_INTERLEAVE_STATIC,\n        [MEMKIND_PARTITION_REGULAR] = &MEMKIND_REGULAR_STATIC,\n        [MEMKIND_PARTITION_HBW_ALL] = &MEMKIND_HBW_ALL_STATIC,\n        [MEMKIND_PARTITION_HBW_ALL_HUGETLB] = &MEMKIND_HBW_ALL_HUGETLB_STATIC,\n    },\n    MEMKIND_NUM_BASE_KIND,\n    PTHREAD_MUTEX_INITIALIZER\n};\n\nvoid *kind_mmap(struct memkind *kind, void *addr, size_t size)\n{\n    if (MEMKIND_LIKELY(kind->ops->mmap == NULL)) {\n        return memkind_default_mmap(kind, addr, size);\n    } else {\n        return kind->ops->mmap(kind, addr, size);\n    }\n}\n\n\nstatic int validate_memtype_bits(memkind_memtype_t memtype)\n{\n    if(memtype == 0) return -1;\n\n    CLEAR_BIT(memtype, MEMKIND_MEMTYPE_DEFAULT);\n    CLEAR_BIT(memtype, MEMKIND_MEMTYPE_HIGH_BANDWIDTH);\n\n    if(memtype != 0) return -1;\n    return 0;\n}\n\nstatic int validate_flags_bits(memkind_bits_t flags)\n{\n    CLEAR_BIT(flags, MEMKIND_MASK_PAGE_SIZE_2MB);\n\n    if(flags != 0) return -1;\n    return 0;\n}\n\nstatic int validate_policy(memkind_policy_t policy)\n{\n    if((policy >= 0) && (policy < MEMKIND_POLICY_MAX_VALUE)) return 0;\n    return -1;\n}\n\nstruct create_args {\n    memkind_t kind;\n    memkind_policy_t policy;\n    memkind_bits_t flags;\n    memkind_memtype_t memtype_flags;\n};\n\nstatic struct create_args supported_args[] = {\n\n    {&MEMKIND_HBW_STATIC,                    MEMKIND_POLICY_BIND_LOCAL,      0,                          MEMKIND_MEMTYPE_HIGH_BANDWIDTH},\n    {&MEMKIND_HBW_HUGETLB_STATIC,            MEMKIND_POLICY_BIND_LOCAL,      MEMKIND_MASK_PAGE_SIZE_2MB, MEMKIND_MEMTYPE_HIGH_BANDWIDTH},\n    {&MEMKIND_HBW_ALL_STATIC,                MEMKIND_POLICY_BIND_ALL,        0,                          MEMKIND_MEMTYPE_HIGH_BANDWIDTH},\n    {&MEMKIND_HBW_ALL_HUGETLB_STATIC,        MEMKIND_POLICY_BIND_ALL,        MEMKIND_MASK_PAGE_SIZE_2MB, MEMKIND_MEMTYPE_HIGH_BANDWIDTH},\n    {&MEMKIND_HBW_PREFERRED_STATIC,          MEMKIND_POLICY_PREFERRED_LOCAL, 0,                          MEMKIND_MEMTYPE_HIGH_BANDWIDTH},\n    {&MEMKIND_HBW_PREFERRED_HUGETLB_STATIC,  MEMKIND_POLICY_PREFERRED_LOCAL, MEMKIND_MASK_PAGE_SIZE_2MB, MEMKIND_MEMTYPE_HIGH_BANDWIDTH},\n    {&MEMKIND_HBW_INTERLEAVE_STATIC,         MEMKIND_POLICY_INTERLEAVE_ALL,  0,                          MEMKIND_MEMTYPE_HIGH_BANDWIDTH},\n    {&MEMKIND_DEFAULT_STATIC,                MEMKIND_POLICY_PREFERRED_LOCAL, 0,                          MEMKIND_MEMTYPE_DEFAULT},\n    {&MEMKIND_HUGETLB_STATIC,                MEMKIND_POLICY_PREFERRED_LOCAL, MEMKIND_MASK_PAGE_SIZE_2MB, MEMKIND_MEMTYPE_DEFAULT},\n    {&MEMKIND_INTERLEAVE_STATIC,             MEMKIND_POLICY_INTERLEAVE_ALL,  0,                          MEMKIND_MEMTYPE_HIGH_BANDWIDTH | MEMKIND_MEMTYPE_DEFAULT},\n};\n\n/* Kind creation */\nMEMKIND_EXPORT int memkind_create_kind(memkind_memtype_t memtype_flags,\n                                       memkind_policy_t policy,\n                                       memkind_bits_t flags,\n                                       memkind_t *kind)\n{\n    if(validate_memtype_bits(memtype_flags) != 0) {\n        log_err(\"Cannot create kind: incorrect memtype_flags.\");\n        return MEMKIND_ERROR_INVALID;\n    }\n\n    if(validate_flags_bits(flags) != 0) {\n        log_err(\"Cannot create kind: incorrect flags.\");\n        return MEMKIND_ERROR_INVALID;\n    }\n\n    if(validate_policy(policy) != 0) {\n        log_err(\"Cannot create kind: incorrect policy.\");\n        return MEMKIND_ERROR_INVALID;\n    }\n\n    if(kind == NULL) {\n        log_err(\"Cannot create kind: 'kind' is NULL pointer.\");\n        return MEMKIND_ERROR_INVALID;\n    }\n\n    int i, num_supported_args = sizeof(supported_args) / sizeof(struct create_args);\n    for(i = 0; i < num_supported_args; i++) {\n        if((supported_args[i].memtype_flags == memtype_flags) &&\n           (supported_args[i].policy == policy) &&\n           (supported_args[i].flags == flags)) {\n\n            if(memkind_check_available(supported_args[i].kind) == 0) {\n                *kind = supported_args[i].kind;\n                return MEMKIND_SUCCESS;\n            } else if(policy == MEMKIND_POLICY_PREFERRED_LOCAL) {\n                *kind = MEMKIND_DEFAULT;\n                return MEMKIND_SUCCESS;\n            }\n            log_err(\"Cannot create kind: requested memory type is not available.\");\n            return MEMKIND_ERROR_MEMTYPE_NOT_AVAILABLE;\n        }\n    }\n\n    log_err(\"Cannot create kind: unsupported set of capabilities.\");\n    return MEMKIND_ERROR_INVALID;\n}\n\nstatic void memkind_destroy_dynamic_kind_from_register(unsigned int i,\n                                                       memkind_t kind)\n{\n    if (i >= MEMKIND_NUM_BASE_KIND) {\n        memkind_registry_g.partition_map[i] = NULL;\n        --memkind_registry_g.num_kind;\n        jemk_free(kind);\n    }\n}\n\n/* Kind destruction. */\nMEMKIND_EXPORT int memkind_destroy_kind(memkind_t kind)\n{\n    if (pthread_mutex_lock(&memkind_registry_g.lock) != 0)\n        assert(0 && \"failed to acquire mutex\");\n    unsigned int i;\n    int err = kind->ops->destroy(kind);\n    for (i = MEMKIND_NUM_BASE_KIND; i < MEMKIND_MAX_KIND; ++i) {\n        if (memkind_registry_g.partition_map[i] &&\n            strcmp(kind->name, memkind_registry_g.partition_map[i]->name) == 0) {\n            memkind_destroy_dynamic_kind_from_register(i, kind);\n            break;\n        }\n    }\n    if (pthread_mutex_unlock(&memkind_registry_g.lock) != 0)\n        assert(0 && \"failed to release mutex\");\n    return err;\n}\n\n/* Declare weak symbols for allocator decorators */\nextern void memkind_malloc_pre(struct memkind **,\n                               size_t *) __attribute__((weak));\nextern void memkind_malloc_post(struct memkind *, size_t,\n                                void **) __attribute__((weak));\nextern void memkind_calloc_pre(struct memkind **, size_t *,\n                               size_t *) __attribute__((weak));\nextern void memkind_calloc_post(struct memkind *, size_t, size_t,\n                                void **) __attribute__((weak));\nextern void memkind_posix_memalign_pre(struct memkind **, void **, size_t *,\n                                       size_t *) __attribute__((weak));\nextern void memkind_posix_memalign_post(struct memkind *, void **, size_t,\n                                        size_t, int *) __attribute__((weak));\nextern void memkind_realloc_pre(struct memkind **, void **,\n                                size_t *) __attribute__((weak));\nextern void memkind_realloc_post(struct memkind *, void *, size_t,\n                                 void **) __attribute__((weak));\nextern void memkind_free_pre(struct memkind **, void **) __attribute__((weak));\nextern void memkind_free_post(struct memkind *, void *) __attribute__((weak));\n\nMEMKIND_EXPORT int memkind_get_version()\n{\n    return MEMKIND_VERSION_MAJOR * 1000000 + MEMKIND_VERSION_MINOR * 1000 +\n           MEMKIND_VERSION_PATCH;\n}\n\nMEMKIND_EXPORT void memkind_error_message(int err, char *msg, size_t size)\n{\n    switch (err) {\n        case MEMKIND_ERROR_UNAVAILABLE:\n            strncpy(msg, \"<memkind> Requested memory kind is not available\", size);\n            break;\n        case MEMKIND_ERROR_MBIND:\n            strncpy(msg, \"<memkind> Call to mbind() failed\", size);\n            break;\n        case MEMKIND_ERROR_MMAP:\n            strncpy(msg, \"<memkind> Call to mmap() failed\", size);\n            break;\n        case MEMKIND_ERROR_MALLOC:\n            strncpy(msg, \"<memkind> Call to jemk_malloc() failed\", size);\n            break;\n        case MEMKIND_ERROR_ENVIRON:\n            strncpy(msg, \"<memkind> Error parsing environment variable (MEMKIND_*)\", size);\n            break;\n        case MEMKIND_ERROR_INVALID:\n            strncpy(msg, \"<memkind> Invalid input arguments to memkind routine\", size);\n            break;\n        case MEMKIND_ERROR_TOOMANY:\n            snprintf(msg, size,\n                     \"<memkind> Attempted to initialize more than maximum (%i) number of kinds\",\n                     MEMKIND_MAX_KIND);\n            break;\n        case MEMKIND_ERROR_RUNTIME:\n            strncpy(msg, \"<memkind> Unspecified run-time error\", size);\n            break;\n        case EINVAL:\n            strncpy(msg,\n                    \"<memkind> Alignment must be a power of two and larger than sizeof(void *)\",\n                    size);\n            break;\n        case ENOMEM:\n            strncpy(msg, \"<memkind> Call to jemk_mallocx() failed\", size);\n            break;\n        case MEMKIND_ERROR_HUGETLB:\n            strncpy(msg, \"<memkind> unable to allocate huge pages\", size);\n            break;\n        case MEMKIND_ERROR_BADOPS:\n            strncpy(msg,\n                    \"<memkind> memkind_ops structure is poorly formed (missing or incorrect functions)\",\n                    size);\n            break;\n        case MEMKIND_ERROR_MEMTYPE_NOT_AVAILABLE:\n            strncpy(msg, \"<memkind> Requested memory type is not available\", size);\n            break;\n        case MEMKIND_ERROR_OPERATION_FAILED:\n            strncpy(msg, \"<memkind> Operation failed\", size);\n            break;\n        case MEMKIND_ERROR_ARENAS_CREATE:\n            strncpy(msg, \"<memkind> Call to jemalloc's arenas.create () failed\", size);\n            break;\n        default:\n            snprintf(msg, size, \"<memkind> Undefined error number: %i\", err);\n            break;\n    }\n    if (size > 0) {\n        msg[size-1] = '\\0';\n    }\n}\n\nvoid memkind_init(memkind_t kind, bool check_numa)\n{\n    log_info(\"Initializing kind %s.\", kind->name);\n    heap_manager_init(kind);\n    if (check_numa) {\n        int err = numa_available();\n        if (err) {\n            log_fatal(\"[%s] NUMA not available (error code:%d).\", kind->name, err);\n            abort();\n        }\n    }\n}\n\nstatic void nop(void) {}\n\nstatic int memkind_create(struct memkind_ops *ops, const char *name,\n                          struct memkind **kind)\n{\n    int err;\n    unsigned int i;\n    unsigned int id_kind = 0;\n\n    *kind = NULL;\n    if (pthread_mutex_lock(&memkind_registry_g.lock) != 0)\n        assert(0 && \"failed to acquire mutex\");\n\n    if (memkind_registry_g.num_kind == MEMKIND_MAX_KIND) {\n        log_err(\"Attempted to initialize more than maximum (%i) number of kinds.\",\n                MEMKIND_MAX_KIND);\n        err = MEMKIND_ERROR_TOOMANY;\n        goto exit;\n    }\n    if (ops->create == NULL ||\n        ops->destroy == NULL ||\n        ops->malloc == NULL ||\n        ops->calloc == NULL ||\n        ops->realloc == NULL ||\n        ops->posix_memalign == NULL ||\n        ops->free == NULL ||\n        ops->init_once != NULL) {\n        err = MEMKIND_ERROR_BADOPS;\n        goto exit;\n    }\n    for (i = 0; i < MEMKIND_MAX_KIND; ++i) {\n        if (memkind_registry_g.partition_map[i] == NULL) {\n            id_kind = i;\n            break;\n        } else if (strcmp(name, memkind_registry_g.partition_map[i]->name) == 0) {\n            log_err(\"Kind with the name %s already exists\", name);\n            err = MEMKIND_ERROR_INVALID;\n            goto exit;\n        }\n    }\n    *kind = (struct memkind *)jemk_calloc(1, sizeof(struct memkind));\n    if (!*kind) {\n        err = MEMKIND_ERROR_MALLOC;\n        log_err(\"jemk_calloc() failed.\");\n        goto exit;\n    }\n\n    (*kind)->partition = memkind_registry_g.num_kind;\n    err = ops->create(*kind, ops, name);\n    if (err) {\n        jemk_free(*kind);\n        goto exit;\n    }\n    memkind_registry_g.partition_map[id_kind] = *kind;\n    ++memkind_registry_g.num_kind;\n\n    (*kind)->init_once = PTHREAD_ONCE_INIT;\n    pthread_once(&(*kind)->init_once,\n                 nop); //this is done to avoid init_once for dynamic kinds\nexit:\n    if (pthread_mutex_unlock(&memkind_registry_g.lock) != 0)\n        assert(0 && \"failed to release mutex\");\n\n    return err;\n}\n\n#ifdef __GNUC__\n__attribute__((destructor))\n#endif\nstatic int memkind_finalize(void)\n{\n    struct memkind *kind;\n    unsigned int i;\n    int err = 0;\n\n    if (pthread_mutex_lock(&memkind_registry_g.lock) != 0)\n        assert(0 && \"failed to acquire mutex\");\n\n    for (i = 0; i < MEMKIND_MAX_KIND; ++i) {\n        kind = memkind_registry_g.partition_map[i];\n        if (kind && kind->ops->finalize) {\n            err = kind->ops->finalize(kind);\n            if (err) {\n                goto exit;\n            }\n            memkind_destroy_dynamic_kind_from_register(i, kind);\n        }\n    }\n    assert(memkind_registry_g.num_kind == MEMKIND_NUM_BASE_KIND);\n\nexit:\n    if (pthread_mutex_unlock(&memkind_registry_g.lock) != 0)\n        assert(0 && \"failed to release mutex\");\n\n    return err;\n}\n\nMEMKIND_EXPORT int memkind_check_available(struct memkind *kind)\n{\n    int err = 0;\n\n    if (MEMKIND_LIKELY(kind->ops->check_available)) {\n        err = kind->ops->check_available(kind);\n    }\n    return err;\n}\n\nMEMKIND_EXPORT size_t memkind_malloc_usable_size(struct memkind *kind,\n                                                 void *ptr)\n{\n    size_t size = 0;\n\n    if (MEMKIND_LIKELY(kind->ops->malloc_usable_size)) {\n        size = kind->ops->malloc_usable_size(kind, ptr);\n    }\n    return size;\n}\n\nMEMKIND_EXPORT void *memkind_malloc(struct memkind *kind, size_t size)\n{\n    void *result;\n\n    pthread_once(&kind->init_once, kind->ops->init_once);\n\n#ifdef MEMKIND_DECORATION_ENABLED\n    if (memkind_malloc_pre) {\n        memkind_malloc_pre(&kind, &size);\n    }\n#endif\n\n    result = kind->ops->malloc(kind, size);\n\n#ifdef MEMKIND_DECORATION_ENABLED\n    if (memkind_malloc_post) {\n        memkind_malloc_post(kind, size, &result);\n    }\n#endif\n\n    return result;\n}\n\nMEMKIND_EXPORT void *memkind_calloc(struct memkind *kind, size_t num,\n                                    size_t size)\n{\n    void *result;\n\n    pthread_once(&kind->init_once, kind->ops->init_once);\n\n#ifdef MEMKIND_DECORATION_ENABLED\n    if (memkind_calloc_pre) {\n        memkind_calloc_pre(&kind, &num, &size);\n    }\n#endif\n\n    result = kind->ops->calloc(kind, num, size);\n\n#ifdef MEMKIND_DECORATION_ENABLED\n    if (memkind_calloc_post) {\n        memkind_calloc_post(kind, num, size, &result);\n    }\n#endif\n\n    return result;\n}\n\nMEMKIND_EXPORT int memkind_posix_memalign(struct memkind *kind, void **memptr,\n                                          size_t alignment,\n                                          size_t size)\n{\n    int err;\n\n    pthread_once(&kind->init_once, kind->ops->init_once);\n\n#ifdef MEMKIND_DECORATION_ENABLED\n    if (memkind_posix_memalign_pre) {\n        memkind_posix_memalign_pre(&kind, memptr, &alignment, &size);\n    }\n#endif\n\n    err = kind->ops->posix_memalign(kind, memptr, alignment, size);\n\n#ifdef MEMKIND_DECORATION_ENABLED\n    if (memkind_posix_memalign_post) {\n        memkind_posix_memalign_post(kind, memptr, alignment, size, &err);\n    }\n#endif\n\n    return err;\n}\n\nMEMKIND_EXPORT void *memkind_realloc(struct memkind *kind, void *ptr,\n                                     size_t size)\n{\n    void *result;\n\n    pthread_once(&kind->init_once, kind->ops->init_once);\n\n#ifdef MEMKIND_DECORATION_ENABLED\n    if (memkind_realloc_pre) {\n        memkind_realloc_pre(&kind, &ptr, &size);\n    }\n#endif\n\n    result = kind->ops->realloc(kind, ptr, size);\n\n#ifdef MEMKIND_DECORATION_ENABLED\n    if (memkind_realloc_post) {\n        memkind_realloc_post(kind, ptr, size, &result);\n    }\n#endif\n\n    return result;\n}\n\nMEMKIND_EXPORT void memkind_free(struct memkind *kind, void *ptr)\n{\n#ifdef MEMKIND_DECORATION_ENABLED\n    if (memkind_free_pre) {\n        memkind_free_pre(&kind, &ptr);\n    }\n#endif\n    if (!kind) {\n        heap_manager_free(kind, ptr);\n    } else {\n        pthread_once(&kind->init_once, kind->ops->init_once);\n        kind->ops->free(kind, ptr);\n    }\n\n#ifdef MEMKIND_DECORATION_ENABLED\n    if (memkind_free_post) {\n        memkind_free_post(kind, ptr);\n    }\n#endif\n}\n\nMEMKIND_EXPORT int memkind_tmpfile(const char *dir, int *fd)\n{\n    static char template[] = \"/memkind.XXXXXX\";\n    int err = 0;\n    int oerrno;\n    int dir_len = strlen(dir);\n\n    if (dir_len > PATH_MAX) {\n        log_err(\"Could not create temporary file: too long path.\");\n        return MEMKIND_ERROR_INVALID;\n    }\n\n    char fullname[dir_len + sizeof (template)];\n    (void) strcpy(fullname, dir);\n    (void) strcat(fullname, template);\n\n    sigset_t set, oldset;\n    sigfillset(&set);\n    (void) sigprocmask(SIG_BLOCK, &set, &oldset);\n\n    if ((*fd = mkstemp(fullname)) < 0) {\n        log_err(\"Could not create temporary file: errno=%d.\", errno);\n        err = MEMKIND_ERROR_INVALID;\n        goto exit;\n    }\n\n    (void) unlink(fullname);\n    (void) sigprocmask(SIG_SETMASK, &oldset, NULL);\n\n    return err;\n\nexit:\n    oerrno = errno;\n    (void) sigprocmask(SIG_SETMASK, &oldset, NULL);\n    if (*fd != -1) {\n        (void) close(*fd);\n    }\n    *fd = -1;\n    errno = oerrno;\n    return err;\n}\n\nMEMKIND_EXPORT int memkind_create_pmem(const char *dir, size_t max_size,\n                                       struct memkind **kind)\n{\n    int err = 0;\n    int oerrno;\n\n    if (max_size && max_size < MEMKIND_PMEM_MIN_SIZE) {\n        log_err(\"Cannot create pmem: invalid size.\");\n        return MEMKIND_ERROR_INVALID;\n    }\n\n    if (max_size) {\n        /* round up to a multiple of jemalloc chunk size */\n        max_size = roundup(max_size, MEMKIND_PMEM_CHUNK_SIZE);\n    }\n\n    int fd = -1;\n    char name[16];\n\n    err = memkind_tmpfile(dir, &fd);\n    if (err) {\n        goto exit;\n    }\n\n    snprintf(name, sizeof (name), \"pmem%08x\", fd);\n\n    err = memkind_create(&MEMKIND_PMEM_OPS, name, kind);\n    if (err) {\n        goto exit;\n    }\n\n    struct memkind_pmem *priv = (*kind)->priv;\n\n    priv->fd = fd;\n    priv->offset = 0;\n    priv->max_size = max_size;\n\n    return err;\n\nexit:\n    oerrno = errno;\n    if (fd != -1) {\n        (void) close(fd);\n    }\n    errno = oerrno;\n    return err;\n}\n\nstatic int memkind_get_kind_by_partition_internal(int partition,\n                                                  struct memkind **kind)\n{\n    int err = 0;\n\n    if (MEMKIND_LIKELY(partition >= 0 &&\n                       partition < MEMKIND_MAX_KIND &&\n                       memkind_registry_g.partition_map[partition] != NULL)) {\n        *kind = memkind_registry_g.partition_map[partition];\n    } else {\n        *kind = NULL;\n        err = MEMKIND_ERROR_UNAVAILABLE;\n    }\n    return err;\n}\n\nMEMKIND_EXPORT int memkind_get_kind_by_partition(int partition,\n                                                 struct memkind **kind)\n{\n    return memkind_get_kind_by_partition_internal(partition, kind);\n}\n\nint memkind_lookup_arena(void *ptr, unsigned int *arena);\nMEMKIND_EXPORT memkind_t memkind_get_kind(void *ptr)\n{\n    unsigned arena;\n    int err = memkind_lookup_arena(ptr, &arena);\n    memkind_t kind = NULL;\n    if (MEMKIND_UNLIKELY(err))\n        return NULL;\n    kind = get_kind_by_arena(arena);\n\n    if (!kind)\n        return MEMKIND_DEFAULT;\n\n    return kind;\n}\n"
  },
  {
    "path": "deps/memkind/src/src/memkind_arena.c",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind.h>\n#include <memkind/internal/memkind_default.h>\n#include <memkind/internal/memkind_arena.h>\n#include <memkind/internal/memkind_private.h>\n#include <memkind/internal/memkind_log.h>\n\n#include <stdlib.h>\n#include <stdio.h>\n#include <limits.h>\n#include <stdint.h>\n#include <unistd.h>\n#include <pthread.h>\n#include <errno.h>\n#include <numa.h>\n#include <numaif.h>\n#include <jemalloc/jemalloc.h>\n#include <utmpx.h>\n#include <sched.h>\n#include <smmintrin.h>\n#include <limits.h>\n#include <sys/mman.h>\n#include <assert.h>\n\n#include \"config.h\"\n\n#define HUGE_PAGE_SIZE (1ull << MEMKIND_MASK_PAGE_SIZE_2MB)\n\nstatic void *jemk_mallocx_check(size_t size, int flags);\nstatic void *jemk_rallocx_check(void *ptr, size_t size, int flags);\nstatic void tcache_finalize(void *args);\n\nstatic unsigned int integer_log2(unsigned int v)\n{\n    return (sizeof(unsigned) * 8) - (__builtin_clz(v) + 1);\n}\n\nstatic unsigned int round_pow2_up(unsigned int v)\n{\n    unsigned int v_log2 = integer_log2(v);\n\n    if (v != 1 << v_log2) {\n        v = 1 << (v_log2 + 1);\n    }\n    return v;\n}\n\nstatic int min_int(int a, int b)\n{\n    return a > b ? b : a;\n}\n\nMEMKIND_EXPORT int memkind_set_arena_map_len(struct memkind *kind)\n{\n    if (kind->ops->get_arena == memkind_bijective_get_arena) {\n        kind->arena_map_len = 1;\n    } else if (kind->ops->get_arena == memkind_thread_get_arena) {\n        char *arena_num_env = getenv(\"MEMKIND_ARENA_NUM_PER_KIND\");\n\n        if (arena_num_env) {\n            unsigned long int arena_num_value = strtoul(arena_num_env, NULL, 10);\n\n            if ((arena_num_value == 0) || (arena_num_value > INT_MAX)) {\n                log_err(\"Wrong MEMKIND_ARENA_NUM_PER_KIND environment value: %lu.\",\n                        arena_num_value);\n                return MEMKIND_ERROR_ENVIRON;\n            }\n\n            kind->arena_map_len = arena_num_value;\n        } else {\n            int calculated_arena_num = numa_num_configured_cpus() * 4;\n\n#if ARENA_LIMIT_PER_KIND != 0\n            calculated_arena_num = min_int(ARENA_LIMIT_PER_KIND, calculated_arena_num);\n#endif\n            kind->arena_map_len = calculated_arena_num;\n        }\n\n        kind->arena_map_len = round_pow2_up(kind->arena_map_len);\n    }\n\n    kind->arena_map_mask = kind->arena_map_len - 1;\n    return 0;\n}\n\nstatic pthread_once_t arena_config_once = PTHREAD_ONCE_INIT;\nstatic int arena_init_status;\n\nstatic pthread_key_t tcache_key;\nstatic bool memkind_hog_memory;\n\nstatic void arena_config_init()\n{\n    const char *str = getenv(\"MEMKIND_HOG_MEMORY\");\n    memkind_hog_memory = str && str[0] == '1';\n\n    arena_init_status = pthread_key_create(&tcache_key, tcache_finalize);\n}\n\n#define MALLOCX_ARENA_MAX 0xffe // copy-pasted from jemalloc/internal/jemalloc_internal.h\nstatic struct memkind *arena_registry_g[MALLOCX_ARENA_MAX];\nstatic pthread_mutex_t arena_registry_write_lock;\n\nstruct memkind *get_kind_by_arena(unsigned arena_ind)\n{\n    // there is no way to obtain MALLOCX_ARENA_MAX from jemalloc\n    // so this checks if arena_ind does not exceed assumed range\n    assert(arena_ind < MALLOCX_ARENA_MAX);\n\n    return arena_registry_g[arena_ind];\n}\n\n// Allocates size bytes aligned to alignment. Returns NULL if allocation fails.\nstatic void *alloc_aligned_slow(size_t size, size_t alignment,\n                                struct memkind *kind)\n{\n    size_t extended_size = size + alignment;\n    void *ptr;\n\n    ptr = kind_mmap(kind,  NULL, extended_size);\n\n    if(ptr == MAP_FAILED) {\n        return NULL;\n    }\n\n    uintptr_t addr = (uintptr_t)ptr;\n    uintptr_t aligned_addr = (addr + alignment) & ~(alignment - 1);\n\n    size_t head_len = aligned_addr - addr;\n    if (head_len > 0) {\n        munmap(ptr, head_len);\n    }\n\n    uintptr_t tail = aligned_addr + size;\n    size_t tail_len = (addr + extended_size) - (aligned_addr + size);\n    if (tail_len > 0) {\n        munmap((void *)tail, tail_len);\n    }\n\n    return (void *)aligned_addr;\n}\n\n\nvoid *arena_extent_alloc(extent_hooks_t *extent_hooks,\n                         void *new_addr,\n                         size_t size,\n                         size_t alignment,\n                         bool *zero,\n                         bool *commit,\n                         unsigned arena_ind)\n{\n    int err;\n    void *addr = NULL;\n\n    struct memkind *kind = get_kind_by_arena(arena_ind);\n\n    err = memkind_check_available(kind);\n    if (err) {\n        return NULL;\n    }\n\n    addr = kind_mmap(kind, new_addr, size);\n    if (addr == MAP_FAILED) {\n        return NULL;\n    }\n\n    if (new_addr != NULL && addr != new_addr) {\n        /* wrong place */\n        munmap(addr, size);\n        return NULL;\n    }\n\n    if ((uintptr_t)addr & (alignment-1)) {\n        munmap(addr, size);\n        addr = alloc_aligned_slow(size, alignment, kind);\n        if(addr == NULL) {\n            return NULL;\n        }\n    }\n\n    *zero = true;\n    *commit = true;\n\n    return addr;\n}\n\nvoid *arena_extent_alloc_hugetlb(extent_hooks_t *extent_hooks,\n                                 void *new_addr,\n                                 size_t size,\n                                 size_t alignment,\n                                 bool *zero,\n                                 bool *commit,\n                                 unsigned arena_ind)\n{\n    //round up to huge page size\n    size = (size + (HUGE_PAGE_SIZE - 1)) & ~(HUGE_PAGE_SIZE - 1);\n    return arena_extent_alloc(extent_hooks, new_addr, size, alignment, zero, commit,\n                              arena_ind);\n}\n\nbool arena_extent_dalloc(extent_hooks_t *extent_hooks,\n                         void *addr,\n                         size_t size,\n                         bool committed,\n                         unsigned arena_ind)\n{\n    return true;\n}\n\nbool arena_extent_commit(extent_hooks_t *extent_hooks,\n                         void *addr,\n                         size_t size,\n                         size_t offset,\n                         size_t length,\n                         unsigned arena_ind)\n{\n    return false;\n}\n\nbool arena_extent_decommit(extent_hooks_t *extent_hooks,\n                           void *addr,\n                           size_t size,\n                           size_t offset,\n                           size_t length,\n                           unsigned arena_ind)\n{\n    return true;\n}\n\nbool arena_extent_purge(extent_hooks_t *extent_hooks,\n                        void *addr,\n                        size_t size,\n                        size_t offset,\n                        size_t length,\n                        unsigned arena_ind)\n{\n    int err;\n\n    if (memkind_hog_memory) {\n        return true;\n    }\n\n    err = madvise(addr + offset, length, MADV_DONTNEED);\n    return (err != 0);\n}\n\nbool arena_extent_split(extent_hooks_t *extent_hooks,\n                        void *addr,\n                        size_t size,\n                        size_t size_a,\n                        size_t size_b,\n                        bool committed,\n                        unsigned arena_ind)\n{\n    return false;\n}\n\nbool arena_extent_merge(extent_hooks_t *extent_hooks,\n                        void *addr_a,\n                        size_t size_a,\n                        void *addr_b,\n                        size_t size_b,\n                        bool committed,\n                        unsigned arena_ind)\n{\n    return false;\n}\n\nextent_hooks_t arena_extent_hooks = {\n    .alloc = arena_extent_alloc,\n    .dalloc = arena_extent_dalloc,\n    .commit = arena_extent_commit,\n    .decommit = arena_extent_decommit,\n    .purge_lazy = arena_extent_purge,\n    .split = arena_extent_split,\n    .merge = arena_extent_merge\n};\n\nextent_hooks_t arena_extent_hooks_hugetlb = {\n    .alloc = arena_extent_alloc_hugetlb,\n    .dalloc = arena_extent_dalloc,\n    .commit = arena_extent_commit,\n    .decommit = arena_extent_decommit,\n    .purge_lazy = arena_extent_purge,\n    .split = arena_extent_split,\n    .merge = arena_extent_merge\n};\n\nextent_hooks_t *get_extent_hooks_by_kind(struct memkind *kind)\n{\n    if (kind == MEMKIND_HUGETLB\n        || kind == MEMKIND_HBW_HUGETLB\n        || kind == MEMKIND_HBW_ALL_HUGETLB\n        || kind == MEMKIND_HBW_PREFERRED_HUGETLB) {\n        return &arena_extent_hooks_hugetlb;\n    } else {\n        return &arena_extent_hooks;\n    }\n}\n\nMEMKIND_EXPORT int memkind_arena_create_map(struct memkind *kind,\n                                            extent_hooks_t *hooks)\n{\n    int err = 0;\n    size_t unsigned_size = sizeof(unsigned int);\n\n    pthread_once(&arena_config_once, arena_config_init);\n    if(arena_init_status) {\n        return arena_init_status;\n    }\n\n    err = memkind_set_arena_map_len(kind);\n    if(err) {\n        return err;\n    }\n#ifdef MEMKIND_TLS\n    if (kind->ops->get_arena == memkind_thread_get_arena) {\n        pthread_key_create(&(kind->arena_key), jemk_free);\n    }\n#endif\n\n    pthread_mutex_lock(&arena_registry_write_lock);\n    unsigned i = 0;\n    kind->arena_zero = UINT_MAX;\n    for(i = 0; i<kind->arena_map_len; i++) {\n        //create new arena with consecutive index\n        unsigned arena_index;\n        err = jemk_mallctl(\"arenas.create\", (void *)&arena_index, &unsigned_size, NULL,\n                           0);\n        if(err) {\n            log_err(\"Could not create arena.\");\n            err = MEMKIND_ERROR_ARENAS_CREATE;\n            goto exit;\n        }\n        //store arena with lowest index (arenas could be created in descending/ascending order)\n        if(kind->arena_zero > arena_index) {\n            kind->arena_zero = arena_index;\n        }\n        //setup extent_hooks for newly created arena\n        char cmd[64];\n        snprintf(cmd, sizeof(cmd), \"arena.%u.extent_hooks\", arena_index);\n        err = jemk_mallctl(cmd, NULL, NULL, (void *)&hooks, sizeof(extent_hooks_t *));\n        if(err) {\n            goto exit;\n        }\n        arena_registry_g[arena_index] = kind;\n    }\n\nexit:\n    pthread_mutex_unlock(&arena_registry_write_lock);\n    return err;\n}\n\nMEMKIND_EXPORT int memkind_arena_create(struct memkind *kind,\n                                        struct memkind_ops *ops, const char *name)\n{\n    int err = 0;\n\n    err = memkind_default_create(kind, ops, name);\n    if (!err) {\n        err = memkind_arena_create_map(kind, get_extent_hooks_by_kind(kind));\n    }\n    return err;\n}\n\nMEMKIND_EXPORT int memkind_arena_destroy(struct memkind *kind)\n{\n    char cmd[128];\n    unsigned int i;\n\n    if (kind->arena_map_len) {\n\n        pthread_mutex_lock(&arena_registry_write_lock);\n\n        for (i = 0; i < kind->arena_map_len; ++i) {\n            snprintf(cmd, 128, \"arena.%u.destroy\", kind->arena_zero + i);\n            jemk_mallctl(cmd, NULL, NULL, NULL, 0);\n            arena_registry_g[kind->arena_zero + i] = NULL;\n        }\n\n        pthread_mutex_unlock(&arena_registry_write_lock);\n\n#ifdef MEMKIND_TLS\n        if (kind->ops->get_arena == memkind_thread_get_arena) {\n            pthread_key_delete(kind->arena_key);\n        }\n#endif\n    }\n\n    memkind_default_destroy(kind);\n    return 0;\n}\n\nint memkind_arena_finalize(struct memkind *kind)\n{\n    return memkind_arena_destroy(kind);\n}\n\n// max allocation size to be cached by tcache mechanism\n// should be aligned with jemalloc opt.lg_tcache_max\n#define TCACHE_MAX (1<<12)\n\nstatic void tcache_finalize(void *args)\n{\n    int i;\n    unsigned *tcache_map = args;\n    for(i = 0; i<MEMKIND_NUM_BASE_KIND; i++) {\n        if(tcache_map[i] != 0) {\n            jemk_mallctl(\"tcache.destroy\", NULL, NULL, (void *)&tcache_map[i],\n                         sizeof(unsigned));\n        }\n    }\n}\n\nint memkind_lookup_arena(void *ptr, unsigned int *arena)\n{\n    size_t sz = sizeof(unsigned);\n    unsigned temp_arena;\n    int err = jemk_mallctl(\"arenas.lookup\", &temp_arena, &sz, &ptr, sizeof(ptr));\n\n    if (err) {\n        log_err(\"Could not found arena, err=%d\", err);\n        return 1;\n    }\n\n    *arena = temp_arena;\n    return 0;\n}\n\nstatic inline int get_tcache_flag(unsigned partition, size_t size)\n{\n\n    // do not cache allocation larger than tcache_max nor those comming from non-static kinds\n    if(size > TCACHE_MAX || partition >= MEMKIND_NUM_BASE_KIND) {\n        return MALLOCX_TCACHE_NONE;\n    }\n\n    unsigned *tcache_map = pthread_getspecific(tcache_key);\n    if(tcache_map == NULL) {\n        tcache_map = jemk_calloc(MEMKIND_NUM_BASE_KIND, sizeof(unsigned));\n        if(tcache_map == NULL) {\n            return MALLOCX_TCACHE_NONE;\n        }\n        pthread_setspecific(tcache_key, (void *)tcache_map);\n    }\n\n    if(MEMKIND_UNLIKELY(tcache_map[partition] == 0)) {\n        size_t unsigned_size = sizeof(unsigned);\n        int err = jemk_mallctl(\"tcache.create\", (void *)&tcache_map[partition],\n                               &unsigned_size, NULL, 0);\n        if(err) {\n            log_err(\"Could not acquire tcache, err=%d\", err);\n            return MALLOCX_TCACHE_NONE;\n        }\n    }\n    return MALLOCX_TCACHE(tcache_map[partition]);\n}\n\nMEMKIND_EXPORT void *memkind_arena_malloc(struct memkind *kind, size_t size)\n{\n    void *result = NULL;\n    int err = 0;\n    unsigned int arena;\n\n    err = kind->ops->get_arena(kind, &arena, size);\n    if (MEMKIND_LIKELY(!err)) {\n        result = jemk_mallocx_check(size,\n                                    MALLOCX_ARENA(arena) | get_tcache_flag(kind->partition, size));\n    }\n    return result;\n}\n\nMEMKIND_EXPORT void memkind_arena_free(struct memkind *kind, void *ptr)\n{\n    if (!kind && ptr != NULL) {\n        unsigned int arena;\n        int err = memkind_lookup_arena(ptr, &arena);\n        if (MEMKIND_LIKELY(!err)) {\n            kind = get_kind_by_arena(arena);\n        }\n    }\n\n    if (!kind) {\n        jemk_free(ptr);\n    } else if (ptr != NULL) {\n        jemk_dallocx(ptr, get_tcache_flag(kind->partition, 0));\n    }\n}\n\nMEMKIND_EXPORT void *memkind_arena_realloc(struct memkind *kind, void *ptr,\n                                           size_t size)\n{\n    int err = 0;\n    unsigned int arena;\n\n    if (size == 0 && ptr != NULL) {\n        memkind_free(kind, ptr);\n        ptr = NULL;\n    } else {\n        err = kind->ops->get_arena(kind, &arena, size);\n        if (MEMKIND_LIKELY(!err)) {\n            if (ptr == NULL) {\n                ptr = jemk_mallocx_check(size,\n                                         MALLOCX_ARENA(arena) | get_tcache_flag(kind->partition, size));\n            } else {\n                ptr = jemk_rallocx_check(ptr, size,\n                                         MALLOCX_ARENA(arena) | get_tcache_flag(kind->partition, size));\n            }\n        }\n    }\n    return ptr;\n}\n\n// TODO: function is workaround for PR#1302 in jemalloc upstream\n// and it should be removed/replaced with memkind_arena_calloc()\n// after PR will be merged\nMEMKIND_EXPORT void *memkind_arena_pmem_calloc(struct memkind *kind, size_t num,\n                                               size_t size)\n{\n    void *result = NULL;\n    int err = 0;\n    unsigned int arena;\n\n    err = kind->ops->get_arena(kind, &arena, size);\n    if (MEMKIND_LIKELY(!err)) {\n        result = jemk_mallocx_check(num * size,\n                                    MALLOCX_ARENA(arena) | get_tcache_flag(kind->partition, size));\n        if (MEMKIND_LIKELY(result)) {\n            memset(result, 0, size);\n        }\n    }\n    return result;\n}\n\nMEMKIND_EXPORT void *memkind_arena_calloc(struct memkind *kind, size_t num,\n                                          size_t size)\n{\n    void *result = NULL;\n    int err = 0;\n    unsigned int arena;\n\n    err = kind->ops->get_arena(kind, &arena, size);\n    if (MEMKIND_LIKELY(!err)) {\n        result = jemk_mallocx_check(num * size,\n                                    MALLOCX_ARENA(arena) | MALLOCX_ZERO | get_tcache_flag(kind->partition, size));\n    }\n    return result;\n}\n\nMEMKIND_EXPORT int memkind_arena_posix_memalign(struct memkind *kind,\n                                                void **memptr, size_t alignment,\n                                                size_t size)\n{\n    int err = 0;\n    unsigned int arena;\n    int errno_before;\n\n    *memptr = NULL;\n    err = kind->ops->get_arena(kind, &arena, size);\n    if (MEMKIND_LIKELY(!err)) {\n        err = memkind_posix_check_alignment(kind, alignment);\n    }\n    if (MEMKIND_LIKELY(!err)) {\n        /* posix_memalign should not change errno.\n           Set it to its previous value after calling jemalloc */\n        errno_before = errno;\n        *memptr = jemk_mallocx_check(size,\n                                     MALLOCX_ALIGN(alignment) | MALLOCX_ARENA(arena) | get_tcache_flag(\n                                         kind->partition, size));\n        errno = errno_before;\n        err = *memptr ? 0 : ENOMEM;\n    }\n    return err;\n}\n\nMEMKIND_EXPORT int memkind_bijective_get_arena(struct memkind *kind,\n                                               unsigned int *arena, size_t size)\n{\n    *arena = kind->arena_zero;\n    return 0;\n}\n\n#ifdef MEMKIND_TLS\nMEMKIND_EXPORT int memkind_thread_get_arena(struct memkind *kind,\n                                            unsigned int *arena, size_t size)\n{\n    int err = 0;\n    unsigned int *arena_tsd;\n    arena_tsd = pthread_getspecific(kind->arena_key);\n\n    if (MEMKIND_UNLIKELY(arena_tsd == NULL)) {\n        arena_tsd = jemk_malloc(sizeof(unsigned int));\n        if (arena_tsd == NULL) {\n            err = MEMKIND_ERROR_MALLOC;\n            log_err(\"jemk_malloc() failed.\");\n        }\n        if (!err) {\n            *arena_tsd = _mm_crc32_u64(0, (uint64_t)pthread_self()) %\n                         kind->arena_map_len;\n            err = pthread_setspecific(kind->arena_key, arena_tsd) ?\n                  MEMKIND_ERROR_RUNTIME : 0;\n        }\n    }\n    *arena = kind->arena_zero + *arena_tsd;\n    return err;\n}\n\n#else\n\n/*\n *\n * We use thread control block as unique thread identifier\n * For more read: https://www.akkadia.org/drepper/tls.pdf\n * We could consider using rdfsbase when it will arrive to linux kernel\n *\n */\nstatic uintptr_t get_fs_base()\n{\n    uintptr_t fs_base;\n    asm (\"movq %%fs:0, %0\" : \"=r\" (fs_base));\n    return fs_base;\n}\n\nMEMKIND_EXPORT int memkind_thread_get_arena(struct memkind *kind,\n                                            unsigned int *arena, size_t size)\n{\n    unsigned int arena_idx;\n    // it's likely that each thread control block lies on diffrent page\n    // so we extracting page number with >> 12 to improve hashing\n    arena_idx = (get_fs_base() >> 12) & kind->arena_map_mask;\n    *arena = kind->arena_zero + arena_idx;\n    return 0;\n}\n#endif //MEMKIND_TLS\n\nstatic void *jemk_mallocx_check(size_t size, int flags)\n{\n    /*\n     * Checking for out of range size due to unhandled error in\n     * jemk_mallocx().  Size invalid for the range\n     * LLONG_MAX <= size <= ULLONG_MAX\n     * which is the result of passing a negative signed number as size\n     */\n    void *result = NULL;\n\n    if (MEMKIND_UNLIKELY(size >= LLONG_MAX)) {\n        errno = ENOMEM;\n    } else if (size != 0) {\n        result = jemk_mallocx(size, flags);\n    }\n    return result;\n}\n\nstatic void *jemk_rallocx_check(void *ptr, size_t size, int flags)\n{\n    /*\n     * Checking for out of range size due to unhandled error in\n     * jemk_mallocx().  Size invalid for the range\n     * LLONG_MAX <= size <= ULLONG_MAX\n     * which is the result of passing a negative signed number as size\n     */\n    void *result = NULL;\n\n    if (MEMKIND_UNLIKELY(size >= LLONG_MAX)) {\n        errno = ENOMEM;\n    } else {\n        result = jemk_rallocx(ptr, size, flags);\n    }\n    return result;\n\n}\n\nvoid memkind_arena_init(struct memkind *kind)\n{\n    int err = 0;\n    if (kind != MEMKIND_DEFAULT) {\n        err = memkind_arena_create_map(kind, get_extent_hooks_by_kind(kind));\n        if (err) {\n            log_fatal(\"[%s] Failed to create arena map (error code:%d).\", kind->name, err);\n            abort();\n        }\n    }\n}\n"
  },
  {
    "path": "deps/memkind/src/src/memkind_default.c",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind/internal/memkind_default.h>\n#include <memkind/internal/memkind_private.h>\n#include <memkind/internal/memkind_log.h>\n#include <memkind/internal/tbb_wrapper.h>\n#include <memkind/internal/heap_manager.h>\n\n#include <numa.h>\n#include <numaif.h>\n#include <sys/mman.h>\n#include <errno.h>\n#include <jemalloc/jemalloc.h>\n#include <stdint.h>\n\n#ifndef MADV_NOHUGEPAGE\n#define MADV_NOHUGEPAGE 15\n#endif\n\nMEMKIND_EXPORT struct memkind_ops MEMKIND_DEFAULT_OPS = {\n    .create = memkind_default_create,\n    .destroy = memkind_default_destroy,\n    .malloc = memkind_default_malloc,\n    .calloc = memkind_default_calloc,\n    .posix_memalign = memkind_default_posix_memalign,\n    .realloc = memkind_default_realloc,\n    .free = memkind_default_free,\n    .init_once = memkind_default_init_once,\n    .malloc_usable_size = memkind_default_malloc_usable_size,\n    .finalize = memkind_default_destroy\n};\n\nMEMKIND_EXPORT int memkind_default_create(struct memkind *kind,\n                                          struct memkind_ops *ops, const char *name)\n{\n    int err = 0;\n\n    kind->ops = ops;\n    if (strlen(name) >= MEMKIND_NAME_LENGTH_PRIV) {\n        kind->name[0] = '\\0';\n        err = MEMKIND_ERROR_INVALID;\n    } else {\n        strcpy(kind->name, name);\n    }\n    return err;\n}\n\nMEMKIND_EXPORT int memkind_default_destroy(struct memkind *kind)\n{\n    return 0;\n}\n\nMEMKIND_EXPORT void *memkind_default_malloc(struct memkind *kind, size_t size)\n{\n    if(MEMKIND_UNLIKELY(size_out_of_bounds(size))) {\n        return NULL;\n    }\n    return jemk_malloc(size);\n}\n\nMEMKIND_EXPORT void *memkind_default_calloc(struct memkind *kind, size_t num,\n                                            size_t size)\n{\n    if(MEMKIND_UNLIKELY(size_out_of_bounds(num) || size_out_of_bounds(size))) {\n        return NULL;\n    }\n    return jemk_calloc(num, size);\n}\n\nMEMKIND_EXPORT int memkind_default_posix_memalign(struct memkind *kind,\n                                                  void **memptr, size_t alignment, size_t size)\n{\n    if(MEMKIND_UNLIKELY(size_out_of_bounds(size))) {\n        return EINVAL;\n    }\n    return jemk_posix_memalign(memptr, alignment, size);\n}\n\nMEMKIND_EXPORT void *memkind_default_realloc(struct memkind *kind, void *ptr,\n                                             size_t size)\n{\n    if(MEMKIND_UNLIKELY(size_out_of_bounds(size))) {\n        return NULL;\n    }\n    return jemk_realloc(ptr, size);\n}\n\nMEMKIND_EXPORT void memkind_default_free(struct memkind *kind, void *ptr)\n{\n    jemk_free(ptr);\n}\n\nMEMKIND_EXPORT size_t memkind_default_malloc_usable_size(struct memkind *kind,\n                                                         void *ptr)\n{\n    return jemk_malloc_usable_size(ptr);\n}\n\nMEMKIND_EXPORT void *memkind_default_mmap(struct memkind *kind, void *addr,\n                                          size_t size)\n{\n    void *result = MAP_FAILED;\n    int err = 0;\n    int flags;\n\n    if (kind->ops->get_mmap_flags) {\n        err = kind->ops->get_mmap_flags(kind, &flags);\n    } else {\n        err = memkind_default_get_mmap_flags(kind, &flags);\n    }\n    if (MEMKIND_LIKELY(!err)) {\n        result = mmap(addr, size, PROT_READ | PROT_WRITE, flags, -1, 0);\n        if (result == MAP_FAILED) {\n            log_err(\"syscall mmap() returned: %p\", result);\n            return result;\n        }\n    }\n    if (kind->ops->mbind) {\n        err = kind->ops->mbind(kind, result, size);\n        if (err) {\n            munmap(result, size);\n            result = MAP_FAILED;\n        }\n    }\n    if (kind->ops->madvise) {\n        err = kind->ops->madvise(kind, result, size);\n        if (err) {\n            munmap(result, size);\n            result = MAP_FAILED;\n        }\n    }\n    return result;\n}\n\nMEMKIND_EXPORT int memkind_nohugepage_madvise(struct memkind *kind, void *addr,\n                                              size_t size)\n{\n    int err = madvise(addr, size, MADV_NOHUGEPAGE);\n\n    //checking if EINVAL was returned due to lack of THP support in kernel\n    if ((err == EINVAL) && (((uintptr_t) addr & (uintptr_t) 0xfff) == 0) &&\n        (size > 0)) {\n        return 0;\n    }\n    if (MEMKIND_UNLIKELY(err)) {\n        log_err(\"syscall madvise() returned: %d\", err);\n    }\n    return err;\n}\n\nMEMKIND_EXPORT int memkind_default_mbind(struct memkind *kind, void *ptr,\n                                         size_t size)\n{\n    nodemask_t nodemask;\n    int err = 0;\n    int mode;\n\n    if (MEMKIND_UNLIKELY(kind->ops->get_mbind_nodemask == NULL ||\n                         kind->ops->get_mbind_mode == NULL)) {\n        log_err(\"memkind_ops->mbind_mode or memkind_ops->bind_nodemask is NULL.\");\n        return MEMKIND_ERROR_BADOPS;\n    }\n    err = kind->ops->get_mbind_nodemask(kind, nodemask.n, NUMA_NUM_NODES);\n    if (MEMKIND_UNLIKELY(err)) {\n        return err;\n    }\n    err = kind->ops->get_mbind_mode(kind, &mode);\n    if (MEMKIND_UNLIKELY(err)) {\n        return err;\n    }\n    err = mbind(ptr, size, mode, nodemask.n, NUMA_NUM_NODES, 0);\n    if (MEMKIND_UNLIKELY(err)) {\n        log_err(\"syscall mbind() returned: %d\", err);\n        return MEMKIND_ERROR_MBIND;\n    }\n    return err;\n}\n\nMEMKIND_EXPORT int memkind_default_get_mmap_flags(struct memkind *kind,\n                                                  int *flags)\n{\n    *flags = MAP_PRIVATE | MAP_ANONYMOUS;\n    return 0;\n}\n\nMEMKIND_EXPORT int memkind_default_get_mbind_nodemask(struct memkind *kind,\n                                                      unsigned long *nodemask,\n                                                      unsigned long maxnode)\n{\n    struct bitmask nodemask_bm = {maxnode, nodemask};\n    copy_bitmask_to_bitmask(numa_all_nodes_ptr, &nodemask_bm);\n    return 0;\n}\n\nMEMKIND_EXPORT int memkind_default_get_mbind_mode(struct memkind *kind,\n                                                  int *mode)\n{\n    *mode = MPOL_BIND;\n    return 0;\n}\n\nMEMKIND_EXPORT int memkind_preferred_get_mbind_mode(struct memkind *kind,\n                                                    int *mode)\n{\n    *mode = MPOL_PREFERRED;\n    return 0;\n}\n\nMEMKIND_EXPORT int memkind_interleave_get_mbind_mode(struct memkind *kind,\n                                                     int *mode)\n{\n    *mode = MPOL_INTERLEAVE;\n    return 0;\n}\n\nMEMKIND_EXPORT int memkind_posix_check_alignment(struct memkind *kind,\n                                                 size_t alignment)\n{\n    int err = 0;\n    if ((alignment < sizeof(void *)) ||\n        (((alignment - 1) & alignment) != 0)) {\n        err = EINVAL;\n    }\n    return err;\n}\n\nMEMKIND_EXPORT void memkind_default_init_once(void)\n{\n    heap_manager_init(MEMKIND_DEFAULT);\n}\n"
  },
  {
    "path": "deps/memkind/src/src/memkind_gbtlb.c",
    "content": "/*\n * Copyright (C) 2014 - 2017 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind/internal/memkind_gbtlb.h>\n#include <memkind/internal/memkind_hugetlb.h>\n#include <memkind/internal/memkind_default.h>\n#include <memkind/internal/memkind_hbw.h>\n#include <memkind/internal/memkind_private.h>\n#include <memkind/internal/memkind_log.h>\n#include <memkind/internal/memkind_arena.h>\n\nstatic void memkind_hbw_gbtlb_init_once(void);\nstatic void memkind_hbw_preferred_gbtlb_init_once(void);\nstatic void memkind_gbtlb_init_once(void);\n\nstatic void *gbtlb_mmap(struct memkind *kind, void *addr, size_t size);\n\nMEMKIND_EXPORT struct memkind_ops MEMKIND_HBW_GBTLB_OPS = {\n    .create = memkind_arena_create,\n    .destroy = memkind_default_destroy,\n    .malloc = memkind_arena_malloc,\n    .calloc = memkind_arena_calloc,\n    .posix_memalign = memkind_arena_posix_memalign,\n    .realloc = memkind_arena_realloc,\n    .free = memkind_default_free,\n    .mmap = gbtlb_mmap,\n    .check_available = memkind_hugetlb_check_available_2mb,\n    .mbind = memkind_default_mbind,\n    .get_mmap_flags = memkind_hugetlb_get_mmap_flags,\n    .get_mbind_mode = memkind_default_get_mbind_mode,\n    .get_mbind_nodemask = memkind_hbw_get_mbind_nodemask,\n    .get_arena = memkind_thread_get_arena,\n    .init_once = memkind_hbw_gbtlb_init_once,\n    .finalize = memkind_arena_finalize\n};\n\nMEMKIND_EXPORT struct memkind_ops MEMKIND_HBW_PREFERRED_GBTLB_OPS = {\n    .create = memkind_arena_create,\n    .destroy = memkind_default_destroy,\n    .malloc = memkind_arena_malloc,\n    .calloc = memkind_arena_calloc,\n    .posix_memalign = memkind_arena_posix_memalign,\n    .realloc = memkind_arena_realloc,\n    .free = memkind_default_free,\n    .mmap = gbtlb_mmap,\n    .check_available = memkind_hugetlb_check_available_2mb,\n    .mbind = memkind_default_mbind,\n    .get_mmap_flags = memkind_hugetlb_get_mmap_flags,\n    .get_mbind_mode = memkind_preferred_get_mbind_mode,\n    .get_mbind_nodemask = memkind_hbw_get_mbind_nodemask,\n    .get_arena = memkind_thread_get_arena,\n    .init_once = memkind_hbw_preferred_gbtlb_init_once,\n    .finalize = memkind_arena_finalize\n};\n\nMEMKIND_EXPORT struct memkind_ops MEMKIND_GBTLB_OPS = {\n    .create = memkind_arena_create,\n    .destroy = memkind_default_destroy,\n    .malloc = memkind_arena_malloc,\n    .calloc = memkind_arena_calloc,\n    .posix_memalign = memkind_arena_posix_memalign,\n    .realloc = memkind_arena_realloc,\n    .free = memkind_default_free,\n    .mmap = gbtlb_mmap,\n    .check_available = memkind_hugetlb_check_available_2mb,\n    .get_mmap_flags = memkind_hugetlb_get_mmap_flags,\n    .get_arena = memkind_thread_get_arena,\n    .init_once = memkind_gbtlb_init_once,\n    .finalize = memkind_arena_finalize\n};\n\n#define ONE_GB 1073741824ULL\n\nstatic void memkind_gbtlb_ceil_size(size_t *size)\n{\n    *size = *size % ONE_GB ? ((*size / ONE_GB) + 1) * ONE_GB : *size;\n}\n\nstatic void *gbtlb_mmap(struct memkind *kind, void *addr, size_t size)\n{\n    memkind_gbtlb_ceil_size(&size);\n    return memkind_default_mmap(kind, addr, size);\n}\n\nstatic void memkind_hbw_gbtlb_init_once(void)\n{\n    memkind_init(MEMKIND_HBW_GBTLB, false);\n}\n\nstatic void memkind_hbw_preferred_gbtlb_init_once(void)\n{\n    memkind_init(MEMKIND_HBW_PREFERRED_GBTLB, false);\n}\n\nstatic void memkind_gbtlb_init_once(void)\n{\n    memkind_init(MEMKIND_GBTLB, false);\n}\n"
  },
  {
    "path": "deps/memkind/src/src/memkind_hbw.c",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind/internal/memkind_hbw.h>\n#include <memkind/internal/memkind_default.h>\n#include <memkind/internal/memkind_hugetlb.h>\n#include <memkind/internal/memkind_arena.h>\n#include <memkind/internal/memkind_private.h>\n#include <memkind/internal/memkind_log.h>\n#include <memkind/internal/heap_manager.h>\n\n#include <stdlib.h>\n#include <stdio.h>\n#include <unistd.h>\n#include <limits.h>\n#include <pthread.h>\n#include <numa.h>\n#include <numaif.h>\n#include <errno.h>\n#include <sys/types.h>\n#include <sys/stat.h>\n#include <jemalloc/jemalloc.h>\n#include <utmpx.h>\n#include <sched.h>\n#include <stdint.h>\n#include <assert.h>\n\nMEMKIND_EXPORT struct memkind_ops MEMKIND_HBW_OPS = {\n    .create = memkind_arena_create,\n    .destroy = memkind_default_destroy,\n    .malloc = memkind_arena_malloc,\n    .calloc = memkind_arena_calloc,\n    .posix_memalign = memkind_arena_posix_memalign,\n    .realloc = memkind_arena_realloc,\n    .free = heap_manager_free,\n    .check_available = memkind_hbw_check_available,\n    .mbind = memkind_default_mbind,\n    .get_mmap_flags = memkind_default_get_mmap_flags,\n    .get_mbind_mode = memkind_default_get_mbind_mode,\n    .get_mbind_nodemask = memkind_hbw_get_mbind_nodemask,\n    .get_arena = memkind_thread_get_arena,\n    .init_once = memkind_hbw_init_once,\n    .finalize = memkind_arena_finalize\n};\n\nMEMKIND_EXPORT struct memkind_ops MEMKIND_HBW_ALL_OPS = {\n    .create = memkind_arena_create,\n    .destroy = memkind_default_destroy,\n    .malloc = memkind_arena_malloc,\n    .calloc = memkind_arena_calloc,\n    .posix_memalign = memkind_arena_posix_memalign,\n    .realloc = memkind_arena_realloc,\n    .free = heap_manager_free,\n    .check_available = memkind_hbw_check_available,\n    .mbind = memkind_default_mbind,\n    .get_mmap_flags = memkind_default_get_mmap_flags,\n    .get_mbind_mode = memkind_default_get_mbind_mode,\n    .get_mbind_nodemask = memkind_hbw_all_get_mbind_nodemask,\n    .get_arena = memkind_thread_get_arena,\n    .init_once = memkind_hbw_all_init_once,\n    .finalize = memkind_arena_finalize\n};\n\nMEMKIND_EXPORT struct memkind_ops MEMKIND_HBW_HUGETLB_OPS = {\n    .create = memkind_arena_create,\n    .destroy = memkind_default_destroy,\n    .malloc = memkind_arena_malloc,\n    .calloc = memkind_arena_calloc,\n    .posix_memalign = memkind_arena_posix_memalign,\n    .realloc = memkind_arena_realloc,\n    .free = heap_manager_free,\n    .check_available = memkind_hbw_hugetlb_check_available,\n    .mbind = memkind_default_mbind,\n    .get_mmap_flags = memkind_hugetlb_get_mmap_flags,\n    .get_mbind_mode = memkind_default_get_mbind_mode,\n    .get_mbind_nodemask = memkind_hbw_get_mbind_nodemask,\n    .get_arena = memkind_thread_get_arena,\n    .init_once = memkind_hbw_hugetlb_init_once,\n    .finalize = memkind_arena_finalize\n};\n\nMEMKIND_EXPORT struct memkind_ops MEMKIND_HBW_ALL_HUGETLB_OPS = {\n    .create = memkind_arena_create,\n    .destroy = memkind_default_destroy,\n    .malloc = memkind_arena_malloc,\n    .calloc = memkind_arena_calloc,\n    .posix_memalign = memkind_arena_posix_memalign,\n    .realloc = memkind_arena_realloc,\n    .free = heap_manager_free,\n    .check_available = memkind_hbw_hugetlb_check_available,\n    .mbind = memkind_default_mbind,\n    .get_mmap_flags = memkind_hugetlb_get_mmap_flags,\n    .get_mbind_mode = memkind_default_get_mbind_mode,\n    .get_mbind_nodemask = memkind_hbw_all_get_mbind_nodemask,\n    .get_arena = memkind_thread_get_arena,\n    .init_once = memkind_hbw_all_hugetlb_init_once,\n    .finalize = memkind_arena_finalize\n};\n\nMEMKIND_EXPORT struct memkind_ops MEMKIND_HBW_PREFERRED_OPS = {\n    .create = memkind_arena_create,\n    .destroy = memkind_default_destroy,\n    .malloc = memkind_arena_malloc,\n    .calloc = memkind_arena_calloc,\n    .posix_memalign = memkind_arena_posix_memalign,\n    .realloc = memkind_arena_realloc,\n    .free = heap_manager_free,\n    .check_available = memkind_hbw_check_available,\n    .mbind = memkind_default_mbind,\n    .get_mmap_flags = memkind_default_get_mmap_flags,\n    .get_mbind_mode = memkind_preferred_get_mbind_mode,\n    .get_mbind_nodemask = memkind_hbw_get_mbind_nodemask,\n    .get_arena = memkind_thread_get_arena,\n    .init_once = memkind_hbw_preferred_init_once,\n    .finalize = memkind_arena_finalize\n};\n\nMEMKIND_EXPORT struct memkind_ops MEMKIND_HBW_PREFERRED_HUGETLB_OPS = {\n    .create = memkind_arena_create,\n    .destroy = memkind_default_destroy,\n    .malloc = memkind_arena_malloc,\n    .calloc = memkind_arena_calloc,\n    .posix_memalign = memkind_arena_posix_memalign,\n    .realloc = memkind_arena_realloc,\n    .free = heap_manager_free,\n    .check_available = memkind_hbw_hugetlb_check_available,\n    .mbind = memkind_default_mbind,\n    .get_mmap_flags = memkind_hugetlb_get_mmap_flags,\n    .get_mbind_mode = memkind_preferred_get_mbind_mode,\n    .get_mbind_nodemask = memkind_hbw_get_mbind_nodemask,\n    .get_arena = memkind_thread_get_arena,\n    .init_once = memkind_hbw_preferred_hugetlb_init_once,\n    .finalize = memkind_arena_finalize\n};\n\nMEMKIND_EXPORT struct memkind_ops MEMKIND_HBW_INTERLEAVE_OPS = {\n    .create = memkind_arena_create,\n    .destroy = memkind_default_destroy,\n    .malloc = memkind_arena_malloc,\n    .calloc = memkind_arena_calloc,\n    .posix_memalign = memkind_arena_posix_memalign,\n    .realloc = memkind_arena_realloc,\n    .free = heap_manager_free,\n    .check_available = memkind_hbw_check_available,\n    .mbind = memkind_default_mbind,\n    .madvise = memkind_nohugepage_madvise,\n    .get_mmap_flags = memkind_default_get_mmap_flags,\n    .get_mbind_mode = memkind_interleave_get_mbind_mode,\n    .get_mbind_nodemask = memkind_hbw_all_get_mbind_nodemask,\n    .get_arena = memkind_thread_get_arena,\n    .init_once = memkind_hbw_interleave_init_once,\n    .finalize = memkind_arena_finalize\n};\n\nstruct numanode_bandwidth_t {\n    int numanode;\n    int bandwidth;\n};\n\nstruct bandwidth_nodes_t {\n    int bandwidth;\n    int num_numanodes;\n    int *numanodes;\n};\n\nstruct memkind_hbw_closest_numanode_t {\n    int init_err;\n    int num_cpu;\n    int *closest_numanode;\n};\n\nstatic struct memkind_hbw_closest_numanode_t memkind_hbw_closest_numanode_g;\nstatic pthread_once_t memkind_hbw_closest_numanode_once_g = PTHREAD_ONCE_INIT;\n\nstatic void memkind_hbw_closest_numanode_init(void);\n\nstatic int create_bandwidth_nodes(int num_bandwidth, const int *bandwidth,\n                                  int *num_unique, struct bandwidth_nodes_t **bandwidth_nodes);\n\nstatic int set_closest_numanode(int num_unique,\n                                const struct bandwidth_nodes_t *bandwidth_nodes,\n                                int target_bandwidth, int num_cpunode, int *closest_numanode);\n\nstatic int numanode_bandwidth_compare(const void *a, const void *b);\n\n// This declaration is necesarry, cause it's missing in headers from libnuma 2.0.8\nextern unsigned int numa_bitmask_weight(const struct bitmask *bmp );\n\nstatic void assign_arbitrary_bandwidth_values(int *bandwidth, int bandwidth_len,\n                                              struct bitmask *hbw_nodes);\n\nstatic int fill_bandwidth_values_heuristically (int *bandwidth,\n                                                int bandwidth_len);\n\nstatic int fill_bandwidth_values_from_enviroment(int *bandwidth,\n                                                 int bandwidth_len, char *hbw_nodes_env);\n\nstatic int fill_nodes_bandwidth(int *bandwidth, int bandwidth_len);\n\nMEMKIND_EXPORT int memkind_hbw_check_available(struct memkind *kind)\n{\n    return kind->ops->get_mbind_nodemask(kind, NULL, 0);\n}\n\nMEMKIND_EXPORT int memkind_hbw_hugetlb_check_available(struct memkind *kind)\n{\n    int err = memkind_hbw_check_available(kind);\n    if (!err) {\n        err = memkind_hugetlb_check_available_2mb(kind);\n    }\n    return err;\n}\n\nMEMKIND_EXPORT int memkind_hbw_get_mbind_nodemask(struct memkind *kind,\n                                                  unsigned long *nodemask,\n                                                  unsigned long maxnode)\n{\n    int cpu;\n    struct bitmask nodemask_bm = {maxnode, nodemask};\n    struct memkind_hbw_closest_numanode_t *g =\n            &memkind_hbw_closest_numanode_g;\n    pthread_once(&memkind_hbw_closest_numanode_once_g,\n                 memkind_hbw_closest_numanode_init);\n    if (MEMKIND_LIKELY(!g->init_err && nodemask)) {\n        numa_bitmask_clearall(&nodemask_bm);\n        cpu = sched_getcpu();\n        if (MEMKIND_LIKELY(cpu < g->num_cpu)) {\n            numa_bitmask_setbit(&nodemask_bm, g->closest_numanode[cpu]);\n        } else {\n            return MEMKIND_ERROR_RUNTIME;\n        }\n    }\n    return g->init_err;\n}\n\nMEMKIND_EXPORT int memkind_hbw_all_get_mbind_nodemask(struct memkind *kind,\n                                                      unsigned long *nodemask,\n                                                      unsigned long maxnode)\n{\n    int cpu;\n    struct bitmask nodemask_bm = {maxnode, nodemask};\n    struct memkind_hbw_closest_numanode_t *g =\n            &memkind_hbw_closest_numanode_g;\n    pthread_once(&memkind_hbw_closest_numanode_once_g,\n                 memkind_hbw_closest_numanode_init);\n\n    if (MEMKIND_LIKELY(!g->init_err && nodemask)) {\n        numa_bitmask_clearall(&nodemask_bm);\n        for (cpu = 0; cpu < g->num_cpu; ++cpu) {\n            numa_bitmask_setbit(&nodemask_bm, g->closest_numanode[cpu]);\n        }\n    }\n    return g->init_err;\n}\n\nstatic void assign_arbitrary_bandwidth_values(int *bandwidth, int bandwidth_len,\n                                              struct bitmask *hbw_nodes)\n{\n    int i, nodes_num = numa_num_configured_nodes();\n\n    // Assigning arbitrary bandwidth values for nodes:\n    // 2 - high BW node (if bit set in hbw_nodes nodemask),\n    // 1 - low  BW node,\n    // 0 - node not present\n    for (i=0; i<NUMA_NUM_NODES; i++) {\n        if (i >= nodes_num) {\n            bandwidth[i] = 0;\n        } else if (numa_bitmask_isbitset(hbw_nodes, i)) {\n            bandwidth[i] = 2;\n        } else {\n            bandwidth[i] = 1;\n        }\n    }\n\n}\n\ntypedef struct registers_t {\n    uint32_t eax;\n    uint32_t ebx;\n    uint32_t ecx;\n    uint32_t edx;\n} registers_t;\n\ninline static void cpuid_asm(int leaf, int subleaf, registers_t *registers)\n{\n    asm volatile(\"cpuid\":\"=a\"(registers->eax),\n                 \"=b\"(registers->ebx),\n                 \"=c\"(registers->ecx),\n                 \"=d\"(registers->edx):\"0\"(leaf), \"2\"(subleaf));\n}\n\n#define CPUID_MODEL_SHIFT       (4)\n#define CPUID_MODEL_MASK        (0xf)\n#define CPUID_EXT_MODEL_MASK    (0xf)\n#define CPUID_EXT_MODEL_SHIFT   (16)\n#define CPUID_FAMILY_MASK       (0xf)\n#define CPUID_FAMILY_SHIFT      (8)\n#define CPU_MODEL_KNL           (0x57)\n#define CPU_MODEL_KNM           (0x85)\n#define CPU_FAMILY_INTEL        (0x06)\n\ntypedef struct {\n    uint32_t model;\n    uint32_t family;\n} cpu_model_data_t;\n\nstatic cpu_model_data_t get_cpu_model_data()\n{\n    registers_t registers;\n    cpuid_asm(1, 0, &registers);\n    uint32_t model = (registers.eax >> CPUID_MODEL_SHIFT) & CPUID_MODEL_MASK;\n    uint32_t model_ext = (registers.eax >> CPUID_EXT_MODEL_SHIFT) &\n                         CPUID_EXT_MODEL_MASK;\n\n    cpu_model_data_t data;\n    data.model = model | (model_ext << 4);\n    data.family = (registers.eax >> CPUID_FAMILY_SHIFT) & CPUID_FAMILY_MASK;\n    return data;\n}\n\nstatic bool is_hbm_supported(cpu_model_data_t cpu)\n{\n    return cpu.family == CPU_FAMILY_INTEL &&\n           (cpu.model == CPU_MODEL_KNL || cpu.model == CPU_MODEL_KNM);\n}\n\nstatic int get_high_bandwidth_nodes(struct bitmask *hbw_node_mask)\n{\n    int nodes_num = numa_num_configured_nodes();\n    // Check if NUMA configuration is supported.\n    if(nodes_num == 2 || nodes_num == 4 || nodes_num == 8) {\n        struct bitmask *node_cpus = numa_allocate_cpumask();\n\n        assert(hbw_node_mask->size >= nodes_num);\n        assert(node_cpus->size >= nodes_num);\n        int i;\n        for(i=0; i<nodes_num; i++) {\n            numa_node_to_cpus(i, node_cpus);\n            if(numa_bitmask_weight(node_cpus) == 0) {\n                //NUMA nodes without CPU are HBW nodes.\n                numa_bitmask_setbit(hbw_node_mask, i);\n            }\n        }\n\n        numa_bitmask_free(node_cpus);\n\n        if(2*numa_bitmask_weight(hbw_node_mask) == nodes_num) {\n            return 0;\n        }\n    }\n\n    return MEMKIND_ERROR_UNAVAILABLE;\n}\n\nstatic int fill_bandwidth(int *bandwidth, int bandwidth_len)\n{\n    struct bitmask *hbw_node_mask = numa_allocate_nodemask();\n    int ret = get_high_bandwidth_nodes(hbw_node_mask);\n    if(ret == 0) {\n        assign_arbitrary_bandwidth_values(bandwidth, bandwidth_len, hbw_node_mask);\n    }\n    numa_bitmask_free(hbw_node_mask);\n\n    return ret;\n}\n\n///This function tries to fill bandwidth array based on knowledge about known CPU models\nstatic int fill_bandwidth_values_heuristically(int *bandwidth,\n                                               int bandwidth_len)\n{\n    cpu_model_data_t cpu = get_cpu_model_data();\n\n    if(!is_hbm_supported(cpu)) {\n        return MEMKIND_ERROR_UNAVAILABLE;\n    }\n\n    switch(cpu.model) {\n        case CPU_MODEL_KNL:\n        case CPU_MODEL_KNM: {\n            int ret = fill_bandwidth(bandwidth, bandwidth_len);\n            if(ret == 0) {\n                log_info(\"Detected High Bandwidth Memory.\");\n            }\n            return ret;\n        }\n        default:\n            return MEMKIND_ERROR_UNAVAILABLE;\n    }\n}\n\nstatic int fill_bandwidth_values_from_enviroment(int *bandwidth,\n                                                 int bandwidth_len, char *hbw_nodes_env)\n{\n    struct bitmask *hbw_nodes_bm = numa_parse_nodestring(hbw_nodes_env);\n\n    if (!hbw_nodes_bm) {\n        log_err(\"Wrong MEMKIND_HBW_NODES environment value.\");\n        return MEMKIND_ERROR_ENVIRON;\n    } else {\n        assign_arbitrary_bandwidth_values(bandwidth, bandwidth_len, hbw_nodes_bm);\n        numa_bitmask_free(hbw_nodes_bm);\n    }\n\n    return 0;\n}\n\nstatic int fill_nodes_bandwidth(int *bandwidth, int bandwidth_len)\n{\n    char *hbw_nodes_env;\n\n    hbw_nodes_env = getenv(\"MEMKIND_HBW_NODES\");\n    if (hbw_nodes_env) {\n        log_info(\"Environment variable MEMKIND_HBW_NODES detected: %s.\", hbw_nodes_env);\n        return fill_bandwidth_values_from_enviroment(bandwidth, bandwidth_len,\n                                                     hbw_nodes_env);\n    }\n\n    return fill_bandwidth_values_heuristically(bandwidth, bandwidth_len);\n}\n\nstatic void memkind_hbw_closest_numanode_init(void)\n{\n    struct memkind_hbw_closest_numanode_t *g =\n            &memkind_hbw_closest_numanode_g;\n    int *bandwidth = NULL;\n    int num_unique = 0;\n    int high_bandwidth = 0;\n    int i;\n\n    struct bandwidth_nodes_t *bandwidth_nodes = NULL;\n\n    g->num_cpu = numa_num_configured_cpus();\n    g->closest_numanode = (int *)jemk_malloc(sizeof(int) * g->num_cpu);\n    bandwidth = (int *)jemk_malloc(sizeof(int) * NUMA_NUM_NODES);\n\n    if (!(g->closest_numanode && bandwidth)) {\n        g->init_err = MEMKIND_ERROR_MALLOC;\n        log_err(\"jemk_malloc() failed.\");\n        goto exit;\n    }\n\n    g->init_err = fill_nodes_bandwidth(bandwidth, NUMA_NUM_NODES);\n    if (g->init_err)\n        goto exit;\n\n    g->init_err = create_bandwidth_nodes(NUMA_NUM_NODES, bandwidth,\n                                         &num_unique, &bandwidth_nodes);\n    if (g->init_err)\n        goto exit;\n\n    high_bandwidth = bandwidth_nodes[num_unique-1].bandwidth;\n    g->init_err = set_closest_numanode(num_unique, bandwidth_nodes,\n                                       high_bandwidth, g->num_cpu,\n                                       g->closest_numanode);\n\n    for(i=0; i<bandwidth_nodes[num_unique-1].num_numanodes; i++) {\n        log_info(\"NUMA node %d is high-bandwidth memory.\",\n                 bandwidth_nodes[num_unique-1].numanodes[i]);\n    }\n\nexit:\n\n    jemk_free(bandwidth_nodes);\n    jemk_free(bandwidth);\n\n    if (g->init_err) {\n        jemk_free(g->closest_numanode);\n        g->closest_numanode = NULL;\n    }\n}\n\n\nstatic int create_bandwidth_nodes(int num_bandwidth, const int *bandwidth,\n                                  int *num_unique, struct bandwidth_nodes_t **bandwidth_nodes)\n{\n    /***************************************************************************\n    *   num_bandwidth (IN):                                                    *\n    *       number of numa nodes and length of bandwidth vector.               *\n    *   bandwidth (IN):                                                        *\n    *       A vector of length num_bandwidth that gives bandwidth for          *\n    *       each numa node, zero if numa node has unknown bandwidth.           *\n    *   num_unique (OUT):                                                      *\n    *       number of unique non-zero bandwidth values in bandwidth            *\n    *       vector.                                                            *\n    *   bandwidth_nodes (OUT):                                                 *\n    *       A list of length num_unique sorted by bandwidth value where        *\n    *       each element gives a list of the numa nodes that have the          *\n    *       given bandwidth.                                                   *\n    *   RETURNS zero on success, error code on failure                         *\n    ***************************************************************************/\n    int err = 0;\n    int i, j, k, l, last_bandwidth;\n    struct numanode_bandwidth_t *numanode_bandwidth = NULL;\n    *bandwidth_nodes = NULL;\n    /* allocate space for sorting array */\n    numanode_bandwidth = jemk_malloc(sizeof(struct numanode_bandwidth_t) *\n                                     num_bandwidth);\n    if (!numanode_bandwidth) {\n        err = MEMKIND_ERROR_MALLOC;\n        log_err(\"jemk_malloc() failed.\");\n    }\n    if (!err) {\n        /* set sorting array */\n        j = 0;\n        for (i = 0; i < num_bandwidth; ++i) {\n            if (bandwidth[i] != 0) {\n                numanode_bandwidth[j].numanode = i;\n                numanode_bandwidth[j].bandwidth = bandwidth[i];\n                ++j;\n            }\n        }\n        /* ignore zero bandwidths */\n        num_bandwidth = j;\n        if (num_bandwidth == 0) {\n            err = MEMKIND_ERROR_UNAVAILABLE;\n        }\n    }\n    if (!err) {\n        qsort(numanode_bandwidth, num_bandwidth,\n              sizeof(struct numanode_bandwidth_t), numanode_bandwidth_compare);\n        /* calculate the number of unique bandwidths */\n        *num_unique = 1;\n        last_bandwidth = numanode_bandwidth[0].bandwidth;\n        for (i = 1; i < num_bandwidth; ++i) {\n            if (numanode_bandwidth[i].bandwidth != last_bandwidth) {\n                last_bandwidth = numanode_bandwidth[i].bandwidth;\n                ++*num_unique;\n            }\n        }\n        /* allocate output array */\n        *bandwidth_nodes = (struct bandwidth_nodes_t *)jemk_malloc(\n                               sizeof(struct bandwidth_nodes_t) * (*num_unique) +\n                               sizeof(int) * num_bandwidth);\n        if (!*bandwidth_nodes) {\n            err = MEMKIND_ERROR_MALLOC;\n            log_err(\"jemk_malloc() failed.\");\n        }\n    }\n    if (!err) {\n        /* populate output */\n        (*bandwidth_nodes)[0].numanodes = (int *)(*bandwidth_nodes + *num_unique);\n        last_bandwidth = numanode_bandwidth[0].bandwidth;\n        k = 0;\n        l = 0;\n        for (i = 0; i < num_bandwidth; ++i, ++l) {\n            (*bandwidth_nodes)[0].numanodes[i] = numanode_bandwidth[i].numanode;\n            if (numanode_bandwidth[i].bandwidth != last_bandwidth) {\n                (*bandwidth_nodes)[k].num_numanodes = l;\n                (*bandwidth_nodes)[k].bandwidth = last_bandwidth;\n                l = 0;\n                ++k;\n                (*bandwidth_nodes)[k].numanodes = (*bandwidth_nodes)[0].numanodes + i;\n                last_bandwidth = numanode_bandwidth[i].bandwidth;\n            }\n        }\n        (*bandwidth_nodes)[k].num_numanodes = l;\n        (*bandwidth_nodes)[k].bandwidth = last_bandwidth;\n    }\n    if (numanode_bandwidth) {\n        jemk_free(numanode_bandwidth);\n    }\n    if (err) {\n        if (*bandwidth_nodes) {\n            jemk_free(*bandwidth_nodes);\n        }\n    }\n    return err;\n}\n\nstatic int set_closest_numanode(int num_unique,\n                                const struct bandwidth_nodes_t *bandwidth_nodes,\n                                int target_bandwidth, int num_cpunode, int *closest_numanode)\n{\n    /***************************************************************************\n    *   num_unique (IN):                                                       *\n    *       Length of bandwidth_nodes vector.                                  *\n    *   bandwidth_nodes (IN):                                                  *\n    *       Output vector from create_bandwitdth_nodes().                      *\n    *   target_bandwidth (IN):                                                 *\n    *       The bandwidth to select for comparison.                            *\n    *   num_cpunode (IN):                                                      *\n    *       Number of cpu's and length of closest_numanode.                    *\n    *   closest_numanode (OUT):                                                *\n    *       Vector that maps cpu index to closest numa node of the specified   *\n    *       bandwidth.                                                         *\n    *   RETURNS zero on success, error code on failure                         *\n    ***************************************************************************/\n    int err = 0;\n    int min_distance, distance, i, j, old_errno, min_unique;\n    struct bandwidth_nodes_t match;\n    match.bandwidth = -1;\n    for (i = 0; i < num_cpunode; ++i) {\n        closest_numanode[i] = -1;\n    }\n    for (i = 0; i < num_unique; ++i) {\n        if (bandwidth_nodes[i].bandwidth == target_bandwidth) {\n            match = bandwidth_nodes[i];\n            break;\n        }\n    }\n    if (match.bandwidth == -1) {\n        err = MEMKIND_ERROR_UNAVAILABLE;\n    } else {\n        for (i = 0; i < num_cpunode; ++i) {\n            min_distance = INT_MAX;\n            min_unique = 1;\n            for (j = 0; j < match.num_numanodes; ++j) {\n                old_errno = errno;\n                distance = numa_distance(numa_node_of_cpu(i),\n                                         match.numanodes[j]);\n                errno = old_errno;\n                if (distance < min_distance) {\n                    min_distance = distance;\n                    closest_numanode[i] = match.numanodes[j];\n                    min_unique = 1;\n                } else if (distance == min_distance) {\n                    min_unique = 0;\n                }\n            }\n            if (!min_unique) {\n                err = MEMKIND_ERROR_RUNTIME;\n            }\n        }\n    }\n    return err;\n}\n\nstatic int numanode_bandwidth_compare(const void *a, const void *b)\n{\n    /***************************************************************************\n    *  qsort comparison function for numa_node_bandwidth structures.  Sorts in *\n    *  order of bandwidth and then numanode.                                   *\n    ***************************************************************************/\n    int result;\n    struct numanode_bandwidth_t *aa = (struct numanode_bandwidth_t *)(a);\n    struct numanode_bandwidth_t *bb = (struct numanode_bandwidth_t *)(b);\n    result = (aa->bandwidth > bb->bandwidth) - (aa->bandwidth < bb->bandwidth);\n    if (result == 0) {\n        result = (aa->numanode > bb->numanode) - (aa->numanode < bb->numanode);\n    }\n    return result;\n}\n\nMEMKIND_EXPORT void memkind_hbw_init_once(void)\n{\n    memkind_init(MEMKIND_HBW, true);\n}\n\nMEMKIND_EXPORT void memkind_hbw_all_init_once(void)\n{\n    memkind_init(MEMKIND_HBW_ALL, true);\n}\n\nMEMKIND_EXPORT void memkind_hbw_hugetlb_init_once(void)\n{\n    memkind_init(MEMKIND_HBW_HUGETLB, true);\n}\n\nMEMKIND_EXPORT void memkind_hbw_all_hugetlb_init_once(void)\n{\n    memkind_init(MEMKIND_HBW_ALL_HUGETLB, true);\n}\n\nMEMKIND_EXPORT void memkind_hbw_preferred_init_once(void)\n{\n    memkind_init(MEMKIND_HBW_PREFERRED, true);\n}\n\nMEMKIND_EXPORT void memkind_hbw_preferred_hugetlb_init_once(void)\n{\n    memkind_init(MEMKIND_HBW_PREFERRED_HUGETLB, true);\n}\n\nMEMKIND_EXPORT void memkind_hbw_interleave_init_once(void)\n{\n    memkind_init(MEMKIND_HBW_INTERLEAVE, true);\n}\n"
  },
  {
    "path": "deps/memkind/src/src/memkind_hugetlb.c",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind/internal/memkind_hugetlb.h>\n#include <memkind/internal/memkind_default.h>\n#include <memkind/internal/memkind_arena.h>\n#include <memkind/internal/memkind_private.h>\n#include <memkind/internal/memkind_log.h>\n\n#include <sys/mman.h>\n#ifndef MAP_HUGETLB\n#define MAP_HUGETLB 0x40000\n#endif\n#ifndef MAP_HUGE_2MB\n#define MAP_HUGE_2MB (21 << 26)\n#endif\n\n#include <stdio.h>\n#include <errno.h>\n#include <numa.h>\n#include <pthread.h>\n#include <dirent.h>\n#include <jemalloc/jemalloc.h>\n\nMEMKIND_EXPORT struct memkind_ops MEMKIND_HUGETLB_OPS = {\n    .create = memkind_arena_create,\n    .destroy = memkind_default_destroy,\n    .malloc = memkind_arena_malloc,\n    .calloc = memkind_arena_calloc,\n    .posix_memalign = memkind_arena_posix_memalign,\n    .realloc = memkind_arena_realloc,\n    .free = memkind_default_free,\n    .check_available = memkind_hugetlb_check_available_2mb,\n    .get_mmap_flags = memkind_hugetlb_get_mmap_flags,\n    .get_arena = memkind_thread_get_arena,\n    .init_once = memkind_hugetlb_init_once,\n    .finalize = memkind_arena_finalize\n};\n\nstatic int get_nr_overcommit_hugepages_cached(size_t pagesize, size_t *out);\nstatic int get_nr_hugepages_cached(size_t pagesize, struct bitmask *nodemask,\n                                   size_t *out);\n\nstatic int memkind_hugetlb_check_available(struct memkind *kind,\n                                           size_t huge_size);\n\nMEMKIND_EXPORT int memkind_hugetlb_get_mmap_flags(struct memkind *kind,\n                                                  int *flags)\n{\n    *flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_HUGE_2MB;\n    return 0;\n}\n\nMEMKIND_EXPORT void memkind_hugetlb_init_once(void)\n{\n    memkind_init(MEMKIND_HUGETLB, false);\n}\n\nMEMKIND_EXPORT int memkind_hugetlb_check_available_2mb(struct memkind *kind)\n{\n    return memkind_hugetlb_check_available(kind, 2097152);\n}\n\n/* huge_size: the huge page size in bytes */\nstatic int memkind_hugetlb_check_available(struct memkind *kind,\n                                           size_t huge_size)\n{\n    int err = 0;\n    nodemask_t nodemask;\n    struct bitmask nodemask_bm = {NUMA_NUM_NODES, nodemask.n};\n\n    /* on x86_64 default huge page size is 2MB */\n    if (huge_size == 0) {\n        huge_size = 2097152;\n    }\n\n    if (kind->ops->get_mbind_nodemask) {\n        err = kind->ops->get_mbind_nodemask(kind, nodemask.n, NUMA_NUM_NODES);\n    } else {\n        numa_bitmask_setall(&nodemask_bm);\n    }\n\n    size_t nr_persistent_hugepages, nr_overcommit_hugepages;\n\n    err = get_nr_hugepages_cached(huge_size, &nodemask_bm,\n                                  &nr_persistent_hugepages);\n    if(err) {\n        return err;\n    }\n\n    err = get_nr_overcommit_hugepages_cached(huge_size, &nr_overcommit_hugepages);\n    if(err) {\n        return err;\n    }\n\n    if (!nr_overcommit_hugepages && !nr_persistent_hugepages) {\n        log_err(\"Persistent hugepages and overcommit hugepages are not available.\");\n        return MEMKIND_ERROR_HUGETLB;\n    }\n\n    return err;\n}\n\nstruct hugepage_size_info {\n    size_t size;\n    size_t *nr_hugepages_per_node_array;\n    size_t  nr_overcommit;\n};\n\nstruct memkind_hugepages_config_t {\n    struct hugepage_size_info **hugepages_info_array;\n    int hugepages_info_array_len;\n    int err; // 0 if sysfs parsing successful, appropriate memkind_error otherwise\n} memkind_hugepages_config;\n\nstatic pthread_once_t memkind_hugepages_config_once_g = PTHREAD_ONCE_INIT;\n\nstatic struct hugepage_size_info *allocate_hugepage_size_info()\n{\n    struct hugepage_size_info *newInfo = jemk_malloc(sizeof(\n                                                         struct hugepage_size_info));\n    if(newInfo == NULL) {\n        log_err(\"jemk_malloc() failed.\");\n        return NULL;\n    }\n\n    newInfo->nr_hugepages_per_node_array = jemk_calloc(NUMA_NUM_NODES,\n                                                       sizeof(size_t));\n    if(newInfo->nr_hugepages_per_node_array == NULL) {\n        jemk_free(newInfo);\n        log_err(\"jemk_calloc() failed.\");\n        return NULL;\n    }\n\n    return newInfo;\n}\n\nstatic size_t get_sysfs_entry_value(const char *entry_path)\n{\n    int errno_before;\n    FILE *fid;\n    int num_read;\n    size_t value_read, ret = 0;\n\n    errno_before = errno;\n    fid = fopen(entry_path, \"r\");\n    if (fid) {\n        num_read = fscanf(fid, \"%zud\", &value_read);\n        if(num_read) {\n            ret  = value_read;\n        }\n        fclose(fid);\n    } else {\n        errno = errno_before;\n    }\n    return ret;\n}\n\n// construct hugepage_size_info object and fill it with data for provided pagesize\nstatic void init_hugepage_size_info(size_t pagesize,\n                                    struct hugepage_size_info *newInfo)\n{\n    char formatted_path[128];\n    const char *nr_path_fmt =\n        \"/sys/devices/system/node/node%u/hugepages/hugepages-%zukB/nr_hugepages\";\n    const char *nr_overcommit_path_fmt =\n        \"/sys/kernel/mm/hugepages/hugepages-%zukB/nr_overcommit_hugepages\";\n    int snprintf_ret = 0;\n    size_t node;\n\n    size_t pagesize_kb = pagesize >> 10;\n\n    newInfo->size = pagesize;\n\n    //read overcommit hugepages limit for this pagesize\n    snprintf_ret = snprintf(formatted_path, sizeof(formatted_path),\n                            nr_overcommit_path_fmt, pagesize_kb);\n    if (snprintf_ret > 0 && snprintf_ret < sizeof(formatted_path)) {\n        newInfo->nr_overcommit = get_sysfs_entry_value(formatted_path);\n        log_info(\"Overcommit limit for %zu kB hugepages is %zu.\", pagesize,\n                 newInfo->nr_overcommit);\n    }\n\n    //read every node nr_hugepages for this pagesize\n    for (node = 0; node < NUMA_NUM_NODES; ++node) {\n        snprintf_ret = snprintf(formatted_path, sizeof(formatted_path), nr_path_fmt,\n                                node, pagesize_kb);\n        if(snprintf_ret > 0 && snprintf_ret < sizeof(formatted_path)) {\n            newInfo->nr_hugepages_per_node_array[node] = get_sysfs_entry_value(\n                                                             formatted_path);\n            if(node < numa_num_configured_nodes()) {\n                log_info(\"Number of %zu kB hugepages on node %zu equals %zu.\", pagesize, node,\n                         newInfo->nr_hugepages_per_node_array[node]);\n            }\n        }\n    }\n}\n\n// get hugepage size in bytes out of sysfs dir name\nstatic int parse_pagesize_from_sysfs_entry(const char *entry, size_t *out)\n{\n    size_t pagesize;\n    int ret = sscanf(entry, \"hugepages-%zukB\", &pagesize);\n\n    if(ret == 1) {\n        *out = pagesize << 10; //we are using bytes but kernel is using kB\n        return 0;\n    }\n\n    return -1;\n}\n\n\nstatic void hugepages_config_init_once()\n{\n    unsigned j, i = 0;\n    size_t pagesize;\n    struct hugepage_size_info **hugepages_info_array = NULL;\n    struct dirent *dir;\n    DIR *hugepages_sysfs = opendir(\"/sys/kernel/mm/hugepages\");\n    if(hugepages_sysfs == NULL) {\n        memkind_hugepages_config.err = MEMKIND_ERROR_HUGETLB;\n        log_err(\"/sys/kernel/mm/hugepages directory is not available.\");\n        return;\n    }\n\n    unsigned hugepages_info_array_len = 2; //initial size of array\n    hugepages_info_array = jemk_malloc(hugepages_info_array_len * sizeof(\n                                           struct hugepage_size_info *));\n    if (hugepages_info_array == NULL) {\n        memkind_hugepages_config.err = MEMKIND_ERROR_MALLOC;\n        closedir(hugepages_sysfs);\n        log_err(\"jemk_malloc() failed.\");\n        return;\n    }\n\n    while ((dir = readdir(hugepages_sysfs)) != NULL) {\n        if(dir->d_type == DT_DIR &&\n           parse_pagesize_from_sysfs_entry(dir->d_name, &pagesize) == 0) {\n            struct hugepage_size_info *new_hugepage_info = allocate_hugepage_size_info();\n            if(new_hugepage_info == NULL) {\n                memkind_hugepages_config.err = MEMKIND_ERROR_MALLOC;\n                break;\n            }\n\n            init_hugepage_size_info(pagesize, new_hugepage_info);\n\n            //there is more hugepage sizes than expected, reallocation of array needed\n            if(i == hugepages_info_array_len) {\n                hugepages_info_array_len *= 2;\n                struct hugepage_size_info **swap_tmp = jemk_realloc(hugepages_info_array,\n                                                                    hugepages_info_array_len * sizeof(struct hugepage_size_info *));\n                if(swap_tmp == NULL) {\n                    jemk_free(new_hugepage_info);\n                    memkind_hugepages_config.err = MEMKIND_ERROR_MALLOC;\n                    log_err(\"jemk_realloc() failed.\");\n                    break;\n                }\n                hugepages_info_array = swap_tmp;\n\n            }\n            hugepages_info_array[i] = new_hugepage_info;\n            i++;\n        }\n    }\n\n    closedir(hugepages_sysfs);\n\n    if(memkind_hugepages_config.err == 0) {\n        memkind_hugepages_config.hugepages_info_array = hugepages_info_array;\n        memkind_hugepages_config.hugepages_info_array_len = i;\n    } else {\n        for(j=0; j<i; j++) {\n            jemk_free(hugepages_info_array[i]);\n        }\n        jemk_free(hugepages_info_array);\n    }\n\n    return;\n}\n\n#ifdef __GNUC__\n__attribute__((destructor))\n#endif\nstatic void destroy_hugepages_per_node()\n{\n    int i;\n    for(i=0; i<memkind_hugepages_config.hugepages_info_array_len; i++) {\n        jemk_free(memkind_hugepages_config.hugepages_info_array[i]);\n    }\n    jemk_free(memkind_hugepages_config.hugepages_info_array);\n}\n\n// helper function that find and return hugepage_size_info object for specified pagesize\nstatic struct hugepage_size_info *get_hugepage_info_for_pagesize(\n    size_t pagesize)\n{\n    int i;\n\n    for(i=0; i<memkind_hugepages_config.hugepages_info_array_len; i++) {\n        if(memkind_hugepages_config.hugepages_info_array[i]->size == pagesize) {\n            return memkind_hugepages_config.hugepages_info_array[i];\n        }\n    }\n    return NULL;\n}\n\n// returns sum of pre-allocated hugepage for specified pagesize and set of nodes\nstatic int get_nr_hugepages_cached(size_t pagesize, struct bitmask *nodemask,\n                                   size_t *out)\n{\n    int i;\n    size_t nr_hugepages = 0;\n    int num_node = numa_num_configured_nodes();\n    pthread_once(&memkind_hugepages_config_once_g,\n                 hugepages_config_init_once);\n\n\n    if(memkind_hugepages_config.err != 0) {\n        return memkind_hugepages_config.err;\n    }\n\n    struct hugepage_size_info *info = get_hugepage_info_for_pagesize(pagesize);\n    if(info == NULL) {\n        log_err(\"Unable to allocate hugepages, because info about pre-allocated hugepages is not available.\");\n        return MEMKIND_ERROR_HUGETLB;\n    }\n\n    for(i=0; i<num_node; i++) {\n        if(numa_bitmask_isbitset(nodemask, i)) {\n            nr_hugepages += info->nr_hugepages_per_node_array[i];\n        }\n    }\n\n    *out = nr_hugepages;\n    return 0;\n}\n\n// returns hugepages overcommit limit for specified pagesize\nstatic int get_nr_overcommit_hugepages_cached(size_t pagesize, size_t *out)\n{\n    pthread_once(&memkind_hugepages_config_once_g,\n                 hugepages_config_init_once);\n\n    if(memkind_hugepages_config.err != 0) {\n        return memkind_hugepages_config.err;\n    }\n\n    struct hugepage_size_info *info = get_hugepage_info_for_pagesize(pagesize);\n    if(info == NULL) {\n        log_err(\"Unable to allocate hugepages, because info about overcommit hugepages is not available.\");\n        return MEMKIND_ERROR_HUGETLB;\n    }\n\n    *out = info->nr_overcommit;\n    return 0;\n}\n\n"
  },
  {
    "path": "deps/memkind/src/src/memkind_interleave.c",
    "content": "/*\n * Copyright (C) 2015 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind/internal/memkind_interleave.h>\n#include <memkind/internal/memkind_default.h>\n#include <memkind/internal/memkind_arena.h>\n#include <memkind/internal/memkind_private.h>\n\nMEMKIND_EXPORT struct memkind_ops MEMKIND_INTERLEAVE_OPS = {\n    .create = memkind_arena_create,\n    .destroy = memkind_default_destroy,\n    .malloc = memkind_arena_malloc,\n    .calloc = memkind_arena_calloc,\n    .posix_memalign = memkind_arena_posix_memalign,\n    .realloc = memkind_arena_realloc,\n    .free = memkind_default_free,\n    .mbind = memkind_default_mbind,\n    .madvise = memkind_nohugepage_madvise,\n    .get_mmap_flags = memkind_default_get_mmap_flags,\n    .get_mbind_mode = memkind_interleave_get_mbind_mode,\n    .get_mbind_nodemask = memkind_default_get_mbind_nodemask,\n    .get_arena = memkind_thread_get_arena,\n    .init_once = memkind_interleave_init_once,\n    .finalize = memkind_arena_finalize\n};\n\nMEMKIND_EXPORT void memkind_interleave_init_once(void)\n{\n    memkind_init(MEMKIND_INTERLEAVE, true);\n}\n"
  },
  {
    "path": "deps/memkind/src/src/memkind_log.c",
    "content": "/*\n * Copyright (C) 2016 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind/internal/memkind_log.h>\n\n#include <stdio.h>\n#include <pthread.h>\n#include <stdarg.h>\n#include <stdlib.h>\n#include <string.h>\n#include <stdbool.h>\n\ntypedef enum {\n    MESSAGE_TYPE_INFO,\n    MESSAGE_TYPE_ERROR,\n    MESSAGE_TYPE_FATAL,\n    MESSAGE_TYPE_MAX_VALUE,\n} message_type_t;\n\nstatic char *message_prefixes[MESSAGE_TYPE_MAX_VALUE] = {\n    [MESSAGE_TYPE_INFO]     = \"MEMKIND_INFO\",\n    [MESSAGE_TYPE_ERROR]    = \"MEMKIND_ERROR\",\n    [MESSAGE_TYPE_FATAL]    = \"MEMKIND_FATAL\",\n};\n\nstatic bool log_enabled;\n\nstatic pthread_once_t init_once = PTHREAD_ONCE_INIT;\nstatic void log_init_once(void)\n{\n    char *memkind_debug_env= getenv(\"MEMKIND_DEBUG\");\n\n    if (memkind_debug_env) {\n        if(strcmp(memkind_debug_env, \"1\") == 0) {\n            log_enabled = true;\n        } else {\n            fprintf(stderr,\n                    \"MEMKIND_WARNING: debug option \\\"%s\\\" unknown; Try man memkind for available options.\\n\",\n                    memkind_debug_env);\n        }\n    }\n}\n\nstatic pthread_mutex_t log_lock = PTHREAD_MUTEX_INITIALIZER;\nstatic void log_generic(message_type_t type, const char *format, va_list args)\n{\n    pthread_once(&init_once, log_init_once);\n    if(log_enabled || (type == MESSAGE_TYPE_FATAL)) {\n        pthread_mutex_lock(&log_lock);\n        fprintf(stderr, \"%s: \", message_prefixes[type]);\n        vfprintf(stderr, format, args);\n        fprintf(stderr, \"\\n\");\n        pthread_mutex_unlock(&log_lock);\n    }\n}\n\nvoid log_info(const char *format, ...)\n{\n    va_list args;\n    va_start(args, format);\n    log_generic(MESSAGE_TYPE_INFO, format, args);\n    va_end(args);\n}\n\nvoid log_err(const char *format, ...)\n{\n    va_list args;\n    va_start(args, format);\n    log_generic(MESSAGE_TYPE_ERROR, format, args);\n    va_end(args);\n}\n\nvoid log_fatal(const char *format, ...)\n{\n    va_list args;\n    va_start(args, format);\n    log_generic(MESSAGE_TYPE_FATAL, format, args);\n    va_end(args);\n}\n\n"
  },
  {
    "path": "deps/memkind/src/src/memkind_pmem.c",
    "content": "/*\n * Copyright (C) 2015 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind/internal/memkind_arena.h>\n#include <memkind/internal/memkind_pmem.h>\n#include <memkind/internal/memkind_private.h>\n#include <memkind/internal/memkind_log.h>\n\n#include <sys/mman.h>\n#include <unistd.h>\n#include <fcntl.h>\n#include <errno.h>\n#include <jemalloc/jemalloc.h>\n#include <assert.h>\n\nMEMKIND_EXPORT struct memkind_ops MEMKIND_PMEM_OPS = {\n    .create = memkind_pmem_create,\n    .destroy = memkind_pmem_destroy,\n    .malloc = memkind_arena_malloc,\n    .calloc = memkind_arena_pmem_calloc,\n    .posix_memalign = memkind_arena_posix_memalign,\n    .realloc = memkind_arena_realloc,\n    .free = memkind_arena_free,\n    .mmap = memkind_pmem_mmap,\n    .get_mmap_flags = memkind_pmem_get_mmap_flags,\n    .get_arena = memkind_thread_get_arena,\n    .finalize = memkind_pmem_destroy,\n    .malloc_usable_size = memkind_default_malloc_usable_size\n};\n\nvoid clearextent(void *addr, struct memkind_pmem *priv)\n{\n\tif (priv == NULL)\n\t\treturn;\n\tfor (int iextent = 0; iextent < priv->cextents; ++iextent){\n\t\tif (priv->rgextents[iextent].addrBase == addr){\n\t\t\tfallocate(priv->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, priv->rgextents[iextent].offset, priv->rgextents[iextent].cb);\n\t\t\tpriv->rgextents[iextent].addrBase = NULL;\n\t\t\treturn;\n\t\t}\n\t}\n}\n\nMEMKIND_EXPORT void memkind_pmem_remapfd(struct memkind *kind, int fdNew)\n{\n\tstruct memkind_pmem *priv = kind->priv;\n\tfor (int iextent = 0; iextent < priv->cextents; ++iextent)\n\t{\n\t\tstruct memkind_pmem_extent *extent = priv->rgextents + iextent;\n\t\tmunmap(extent->addrBase, extent->cb);\n\t\tmmap(extent->addrBase, extent->cb, PROT_READ | PROT_WRITE, MAP_SHARED, fdNew, extent->offset);\n\t}\n\tpriv->fd = fdNew;\n}\n\nMEMKIND_EXPORT int memkind_pmem_iskind(struct memkind *kind, void *pv)\n{\n    struct memkind_pmem *priv = kind->priv;\n    char *pb = pv;\n\tfor (int iextent = 0; iextent < priv->cextents; ++iextent)\n    {\n        struct memkind_pmem_extent *extent = priv->rgextents + iextent;\n        if ((((char*)extent->addrBase) <= pb) && (((char*)extent->addrBase)+extent->cb) > pb)\n            return 1;\n    }\n    return 0;\n}\n\nvoid *pmem_extent_alloc(extent_hooks_t *extent_hooks,\n                        void *new_addr,\n                        size_t size,\n                        size_t alignment,\n                        bool *zero,\n                        bool *commit,\n                        unsigned arena_ind)\n{\n    int err;\n    void *addr = NULL;\n\n    if (new_addr != NULL) {\n        /* not supported */\n        goto exit;\n    }\n\n    struct memkind *kind;\n    kind = get_kind_by_arena(arena_ind);\n    if (kind == NULL) {\n        return NULL;\n    }\n\n    err = memkind_check_available(kind);\n    if (err) {\n        goto exit;\n    }\n\n    addr = memkind_pmem_mmap(kind, new_addr, size);\n\n    if (addr != MAP_FAILED) {\n        *zero = true;\n        *commit = true;\n\n        /* XXX - check alignment */\n    } else {\n        addr = NULL;\n    }\nexit:\n    return addr;\n}\n\nbool pmem_extent_dalloc(extent_hooks_t *extent_hooks,\n                        void *addr,\n                        size_t size,\n                        bool committed,\n                        unsigned arena_ind)\n{\n    // if madvise fail, it means that addr isn't mapped shared (doesn't come from pmem)\n    // and it should be unmapped to avoid space exhaustion when calling large number of\n    // operations like memkind_create_pmem and memkind_destroy_kind\n    // EOPNOTSUPP is returned in case of filesystem doesn't support FALLOC_FL_PUNCH_HOLE\n    errno = 0;\n    if (madvise(addr, size, MADV_REMOVE) != 0 && errno != EOPNOTSUPP) {\n        if (munmap(addr, size) == -1) {\n            log_err(\"munmap failed!\");\n        }\n\tstruct memkind *kind = get_kind_by_arena(arena_ind);\n\tif (kind != NULL)\n\t\tclearextent(addr, kind->priv);\n    }\n    return true;\n}\n\nbool pmem_extent_commit(extent_hooks_t *extent_hooks,\n                        void *addr,\n                        size_t size,\n                        size_t offset,\n                        size_t length,\n                        unsigned arena_ind)\n{\n    /* do nothing - report success */\n    return false;\n}\n\nbool pmem_extent_decommit(extent_hooks_t *extent_hooks,\n                          void *addr,\n                          size_t size,\n                          size_t offset,\n                          size_t length,\n                          unsigned arena_ind)\n{\n    /* do nothing - report failure (opt-out) */\n    return true;\n}\n\nbool pmem_extent_purge(extent_hooks_t *extent_hooks,\n                       void *addr,\n                       size_t size,\n                       size_t offset,\n                       size_t length,\n                       unsigned arena_ind)\n{\n    /* do nothing - report failure (opt-out) */\n    return true;\n}\n\nbool pmem_extent_split(extent_hooks_t *extent_hooks,\n                       void *addr,\n                       size_t size,\n                       size_t size_a,\n                       size_t size_b,\n                       bool committed,\n                       unsigned arena_ind)\n{\n    /* do nothing - report success */\n    return false;\n}\n\nbool pmem_extent_merge(extent_hooks_t *extent_hooks,\n                       void *addr_a,\n                       size_t size_a,\n                       void *addr_b,\n                       size_t size_b,\n                       bool committed,\n                       unsigned arena_ind)\n{\n    /* do nothing - report success */\n    return false;\n}\n\nvoid pmem_extent_destroy(extent_hooks_t *extent_hooks,\n                         void *addr,\n                         size_t size,\n                         bool committed,\n                         unsigned arena_ind)\n{\n    if (munmap(addr, size) == -1) {\n        log_err(\"munmap failed!\");\n    } \n    else{\n\tstruct memkind *kind = get_kind_by_arena(arena_ind);\n\tif (kind != NULL)\n\t\tclearextent(addr, kind->priv);\n    }\n}\n\nstatic extent_hooks_t pmem_extent_hooks = {\n    .alloc = pmem_extent_alloc,\n    .dalloc = pmem_extent_dalloc,\n    .commit = pmem_extent_commit,\n    .decommit = pmem_extent_decommit,\n    .purge_lazy = pmem_extent_purge,\n    .split = pmem_extent_split,\n    .merge = pmem_extent_merge,\n    .destroy = pmem_extent_destroy\n};\n\nMEMKIND_EXPORT int memkind_fd(struct memkind *kind)\n{\n\tstruct memkind_pmem *priv = kind->priv;\n\treturn priv->fd;\n}\n\nMEMKIND_EXPORT int memkind_pmem_create(struct memkind *kind,\n                                       struct memkind_ops *ops, const char *name)\n{\n    struct memkind_pmem *priv;\n    int err;\n\n    priv = (struct memkind_pmem *)jemk_calloc(sizeof(struct memkind_pmem), 1);\n    if (!priv) {\n        log_err(\"cemk_malloc() failed.\");\n        return MEMKIND_ERROR_MALLOC;\n    }\n\n    if (pthread_mutex_init(&priv->pmem_lock, NULL) != 0) {\n        err = MEMKIND_ERROR_RUNTIME;\n        goto exit;\n    }\n\n    err = memkind_default_create(kind, ops, name);\n    if (err) {\n        goto exit;\n    }\n\n    err = memkind_arena_create_map(kind, &pmem_extent_hooks);\n    if (err) {\n        goto exit;\n    }\n\n    kind->priv = priv;\n    return 0;\n\nexit:\n    /* err is set, please don't overwrite it with result of pthread_mutex_destroy */\n    pthread_mutex_destroy(&priv->pmem_lock);\n    jemk_free(priv);\n    return err;\n}\n\nMEMKIND_EXPORT int memkind_pmem_destroy(struct memkind *kind)\n{\n    struct memkind_pmem *priv = kind->priv;\n\n    memkind_arena_destroy(kind);\n\n    pthread_mutex_destroy(&priv->pmem_lock);\n\n    (void) close(priv->fd);\n    jemk_free(priv);\n\n    return 0;\n}\n\nMEMKIND_EXPORT void *memkind_pmem_mmap(struct memkind *kind, void *addr,\n                                       size_t size)\n{\n    struct memkind_pmem *priv = kind->priv;\n    void *result;\n\n    if (pthread_mutex_lock(&priv->pmem_lock) != 0)\n        assert(0 && \"failed to acquire mutex\");\n\n    if (priv->max_size != 0 && (size_t)priv->offset + size > priv->max_size) {\n        pthread_mutex_unlock(&priv->pmem_lock);\n        return MAP_FAILED;\n    }\n\n    if ((errno = posix_fallocate(priv->fd, priv->offset, (off_t)size)) != 0) {\n        pthread_mutex_unlock(&priv->pmem_lock);\n        return MAP_FAILED;\n    }\n\n    if ((result = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, priv->fd,\n                       priv->offset)) != MAP_FAILED) {\n        \n\tif (priv->cextentsAlloc <= priv->cextents){\n        \tif (priv->cextentsAlloc == 0)\n\t\t\tpriv->cextentsAlloc = 64;\n\t\telse\n\t\t\tpriv->cextentsAlloc *= 2;\n\t\tpriv->rgextents = jemk_realloc(priv->rgextents, priv->cextentsAlloc * sizeof(struct memkind_pmem_extent));\n\t}\n\tpriv->rgextents[priv->cextents].addrBase = result;\n\tpriv->rgextents[priv->cextents].cb = size;\n\tpriv->rgextents[priv->cextents].offset = priv->offset;\n\tpriv->cextents++;\n        priv->offset += size;\n    }\n\n    pthread_mutex_unlock(&priv->pmem_lock);\n\n    return result;\n}\n\nMEMKIND_EXPORT int memkind_pmem_get_mmap_flags(struct memkind *kind, int *flags)\n{\n    *flags = MAP_SHARED;\n    return 0;\n}\n"
  },
  {
    "path": "deps/memkind/src/src/memkind_regular.c",
    "content": "/*\n * Copyright (C) 2017 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind.h>\n#include <memkind/internal/memkind_arena.h>\n#include <memkind/internal/memkind_default.h>\n#include <memkind/internal/heap_manager.h>\n\n#include <numa.h>\n\nstatic struct bitmask *regular_nodes_mask = NULL;\n\nstatic void regular_nodes_init(void)\n{\n    int i, node = 0, nodes_num = numa_num_configured_nodes();\n    struct bitmask *node_cpus = numa_allocate_cpumask();\n\n    regular_nodes_mask = numa_allocate_nodemask();\n\n    for (i = 0; i < nodes_num; i++) {\n        numa_node_to_cpus(node, node_cpus);\n        if (numa_bitmask_weight(node_cpus))\n            numa_bitmask_setbit(regular_nodes_mask, i);\n    }\n    numa_bitmask_free(node_cpus);\n}\n\nstatic void memkind_regular_init_once(void)\n{\n    regular_nodes_init();\n    memkind_init(MEMKIND_REGULAR, true);\n}\n\nstatic int memkind_regular_check_available(struct memkind *kind)\n{\n    /* init_once method is called in memkind_malloc function\n     * when memkind malloc is not called this function will fail.\n     * Call pthread_once to be sure that situation mentioned\n     * above will never happen */\n    pthread_once(&kind->init_once, kind->ops->init_once);\n    return regular_nodes_mask != NULL ? MEMKIND_SUCCESS : MEMKIND_ERROR_UNAVAILABLE;\n}\n\nMEMKIND_EXPORT int memkind_regular_all_get_mbind_nodemask(struct memkind *kind,\n                                                          unsigned long *nodemask,\n                                                          unsigned long maxnode)\n{\n    struct bitmask nodemask_bm = {maxnode, nodemask};\n\n    if(!regular_nodes_mask) {\n        return MEMKIND_ERROR_UNAVAILABLE;\n    }\n\n    copy_bitmask_to_bitmask(regular_nodes_mask, &nodemask_bm);\n    return MEMKIND_SUCCESS;\n}\n\nstatic int memkind_regular_finalize(memkind_t kind)\n{\n    if(regular_nodes_mask)\n        numa_bitmask_free(regular_nodes_mask);\n\n    return memkind_arena_finalize(kind);\n}\n\nMEMKIND_EXPORT struct memkind_ops MEMKIND_REGULAR_OPS = {\n    .create = memkind_arena_create,\n    .destroy = memkind_default_destroy,\n    .malloc = memkind_arena_malloc,\n    .calloc = memkind_arena_calloc,\n    .posix_memalign = memkind_arena_posix_memalign,\n    .realloc = memkind_arena_realloc,\n    .free = heap_manager_free,\n    .check_available = memkind_regular_check_available,\n    .mbind = memkind_default_mbind,\n    .get_mmap_flags = memkind_default_get_mmap_flags,\n    .get_mbind_mode = memkind_default_get_mbind_mode,\n    .get_mbind_nodemask = memkind_regular_all_get_mbind_nodemask,\n    .get_arena = memkind_thread_get_arena,\n    .init_once = memkind_regular_init_once,\n    .finalize = memkind_regular_finalize\n};\n\n\n"
  },
  {
    "path": "deps/memkind/src/src/tbb_wrapper.c",
    "content": "/*\n * Copyright (C) 2017 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind/internal/memkind_default.h>\n#include <memkind/internal/memkind_log.h>\n#include <memkind/internal/memkind_private.h>\n#include <memkind/internal/tbb_wrapper.h>\n#include <memkind/internal/tbb_mem_pool_policy.h>\n#include <limits.h>\n\n#include <stdint.h>\n#include <errno.h>\n#include <stdio.h>\n#include <sys/mman.h>\n#include <stdlib.h>\n#include <dlfcn.h>\n#include <string.h>\n\nvoid *(*pool_malloc)(void *, size_t);\nvoid *(*pool_realloc)(void *, void *, size_t);\nvoid *(*pool_aligned_malloc)(void *, size_t, size_t);\nbool (*pool_free)(void *, void *);\nint (*pool_create_v1)(intptr_t, const struct MemPoolPolicy *, void **);\nbool (*pool_destroy)(void *);\nvoid *(*pool_identify)(void *object);\n\nstatic void *tbb_handle = NULL;\n\nstatic int load_tbb_symbols()\n{\n    const char so_name[]=\"libtbbmalloc.so.2\";\n    tbb_handle = dlopen(so_name, RTLD_LAZY);\n    if(!tbb_handle) {\n        log_err(\"%s not found.\", so_name);\n        return -1;\n    }\n\n    pool_malloc = dlsym(tbb_handle, \"_ZN3rml11pool_mallocEPNS_10MemoryPoolEm\");\n    pool_realloc = dlsym(tbb_handle, \"_ZN3rml12pool_reallocEPNS_10MemoryPoolEPvm\");\n    pool_aligned_malloc = dlsym(tbb_handle,\n                                \"_ZN3rml19pool_aligned_mallocEPNS_10MemoryPoolEmm\");\n    pool_free = dlsym(tbb_handle, \"_ZN3rml9pool_freeEPNS_10MemoryPoolEPv\");\n    pool_create_v1 = dlsym(tbb_handle,\n                           \"_ZN3rml14pool_create_v1ElPKNS_13MemPoolPolicyEPPNS_10MemoryPoolE\");\n    pool_destroy = dlsym(tbb_handle, \"_ZN3rml12pool_destroyEPNS_10MemoryPoolE\");\n    pool_identify = dlsym(tbb_handle, \"_ZN3rml13pool_identifyEPv\");\n\n    if(!pool_malloc ||\n       !pool_realloc ||\n       !pool_aligned_malloc ||\n       !pool_free ||\n       !pool_create_v1 ||\n       !pool_destroy ||\n       !pool_identify)\n\n    {\n        log_err(\"Could not find symbols in %s.\", so_name);\n        dlclose(tbb_handle);\n        return -1;\n    }\n\n    return 0;\n}\n\n//Granularity of raw_alloc allocations\n#define GRANULARITY 2*1024*1024\nstatic void *raw_alloc(intptr_t pool_id, size_t *bytes/*=n*GRANULARITY*/)\n{\n    void *ptr = kind_mmap((struct memkind *)pool_id, NULL, *bytes);\n    return (ptr==MAP_FAILED) ? NULL : ptr;\n}\n\nstatic int raw_free(intptr_t pool_id, void *raw_ptr, size_t raw_bytes)\n{\n    return munmap(raw_ptr, raw_bytes);\n}\n\nstatic void *tbb_pool_malloc(struct memkind *kind, size_t size)\n{\n    if(size_out_of_bounds(size)) return NULL;\n    void *result = pool_malloc(kind->priv, size);\n    if (!result)\n        errno = ENOMEM;\n    return result;\n}\n\nstatic void *tbb_pool_calloc(struct memkind *kind, size_t num, size_t size)\n{\n    if (size_out_of_bounds(num) || size_out_of_bounds(size)) return NULL;\n\n    const size_t array_size = num*size;\n    if (array_size/num != size) {\n        errno = ENOMEM;\n        return NULL;\n    }\n    void *result = pool_malloc(kind->priv, array_size);\n    if (result) {\n        memset(result, 0, array_size);\n    } else {\n        errno = ENOMEM;\n    }\n    return result;\n}\n\nstatic void *tbb_pool_realloc(struct memkind *kind, void *ptr, size_t size)\n{\n    if(size_out_of_bounds(size)) return NULL;\n    void *result = pool_realloc(kind->priv, ptr, size);\n    if (!result && size)\n        errno = ENOMEM;\n    return result;\n}\n\nstatic int tbb_pool_posix_memalign(struct memkind *kind, void **memptr,\n                                   size_t alignment, size_t size)\n{\n    //Check if alignment is \"at least as large as sizeof(void *)\".\n    if(!alignment && (0 != (alignment & (alignment-sizeof(void *))))) return EINVAL;\n    //Check if alignment is \"a power of 2\".\n    if(alignment & (alignment-1)) return EINVAL;\n    if(size_out_of_bounds(size)) return ENOMEM;\n    void *result = pool_aligned_malloc(kind->priv, size, alignment);\n    if (!result) {\n        return ENOMEM;\n    }\n    *memptr = result;\n    return 0;\n}\n\nvoid tbb_pool_free(struct memkind *kind, void *ptr)\n{\n    if(kind) {\n        pool_free(kind->priv, ptr);\n    } else {\n        pool_free(pool_identify(ptr), ptr);\n    }\n}\n\nstatic size_t tbb_pool_usable_size(struct memkind *kind, void *ptr)\n{\n    log_err(\"memkind_malloc_usable_size() is not supported by TBB.\");\n    return 0;\n}\n\nstatic int tbb_destroy(struct memkind *kind)\n{\n    bool pool_destroy_ret = pool_destroy(kind->priv);\n    dlclose(tbb_handle);\n\n    if(!pool_destroy_ret) {\n        log_err(\"TBB pool destroy failure.\");\n        return MEMKIND_ERROR_OPERATION_FAILED;\n    }\n    return MEMKIND_SUCCESS;\n}\n\nvoid tbb_initialize(struct memkind *kind)\n{\n    if(!kind || load_tbb_symbols()) {\n        log_fatal(\"Failed to initialize TBB.\");\n        abort();\n    }\n\n    struct MemPoolPolicy policy = {\n        .pAlloc = raw_alloc,\n        .pFree = raw_free,\n        .granularity = GRANULARITY,\n        .version = 1,\n        .fixedPool = false,\n        .keepAllMemory = false,\n        .reserved = 0\n    };\n\n    pool_create_v1((intptr_t)kind, &policy, &kind->priv);\n    if (!kind->priv) {\n        log_fatal(\"Unable to create TBB memory pool.\");\n        abort();\n    }\n\n    kind->ops->malloc = tbb_pool_malloc;\n    kind->ops->calloc = tbb_pool_calloc;\n    kind->ops->posix_memalign = tbb_pool_posix_memalign;\n    kind->ops->realloc = tbb_pool_realloc;\n    kind->ops->free = tbb_pool_free;\n    kind->ops->finalize = tbb_destroy;\n    kind->ops->malloc_usable_size = tbb_pool_usable_size;\n}\n"
  },
  {
    "path": "deps/memkind/src/test/Allocator.hpp",
    "content": "/*\n* Copyright (C) 2017 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n\n#include <stdio.h>\n#include <assert.h>\n#include <map>\n#include <iterator>\n\nclass Allocator\n{\npublic:\n\n    virtual void *malloc(size_t size) = 0;\n    virtual void *calloc(size_t num, size_t size) = 0;\n    virtual void *realloc(void *ptr, size_t size) = 0;\n    virtual int memalign(void **ptr, size_t alignment, size_t size) = 0;\n    virtual void free(void *ptr) = 0;\n\n    /* get_numa_policy() returns MPOL_INTERLEAVE, MPOL_BIND, MPOL_PREFERRED\n     or -1 when the allocator is not providing NUMA policy */\n    virtual int get_numa_policy() = 0;\n    virtual bool is_high_bandwidth() = 0;\n    virtual size_t get_page_size() = 0;\n\n    virtual ~Allocator(void) {}\n};\n\nclass MemkindAllocator : public Allocator\n{\npublic:\n\n    MemkindAllocator(memkind_memtype_t memtype, memkind_policy_t policy,\n                     memkind_bits_t flags) :\n        memtype(memtype),\n        policy(policy),\n        flags(flags)\n    {\n        int ret = memkind_create_kind(memtype, policy, flags, &kind);\n        assert(ret == MEMKIND_SUCCESS);\n        assert(kind != NULL);\n    }\n\n    MemkindAllocator(memkind_t kind) : kind(kind)\n    {\n        assert(kind != NULL);\n    }\n\n    virtual void *malloc(size_t size)\n    {\n        return memkind_malloc(kind, size);\n    }\n\n    virtual void *calloc(size_t num, size_t size)\n    {\n        return memkind_calloc(kind, num, size);\n    }\n\n    virtual void *realloc(void *ptr, size_t size)\n    {\n        return memkind_realloc(kind, ptr, size);\n    }\n\n    virtual int memalign(void **ptr, size_t alignment, size_t size)\n    {\n        return memkind_posix_memalign(kind, ptr, alignment, size);\n    }\n\n    virtual void free(void *ptr)\n    {\n        memkind_free(kind, ptr);\n    }\n\n    virtual int get_numa_policy()\n    {\n        static std::map<memkind_t, int> kind_policy = {\n            std::make_pair(MEMKIND_HBW_INTERLEAVE, MPOL_INTERLEAVE),\n            std::make_pair(MEMKIND_INTERLEAVE, MPOL_INTERLEAVE),\n            std::make_pair(MEMKIND_HBW_PREFERRED, MPOL_PREFERRED),\n            std::make_pair(MEMKIND_HBW_PREFERRED_HUGETLB, MPOL_PREFERRED),\n            std::make_pair(MEMKIND_HBW, MPOL_BIND),\n            std::make_pair(MEMKIND_HBW_HUGETLB, MPOL_BIND),\n            std::make_pair(MEMKIND_REGULAR, MPOL_BIND),\n            std::make_pair(MEMKIND_DEFAULT, MPOL_DEFAULT),\n            std::make_pair(MEMKIND_HUGETLB, MPOL_DEFAULT),\n            std::make_pair(MEMKIND_HBW_ALL_HUGETLB, MPOL_BIND),\n            std::make_pair(MEMKIND_HBW_ALL, MPOL_BIND)\n        };\n        auto it = kind_policy.find(kind);\n\n        if(it != std::end(kind_policy)) {\n            return it->second;\n        }\n        return -1;\n    }\n\n    virtual bool is_high_bandwidth()\n    {\n        return  memtype == MEMKIND_MEMTYPE_HIGH_BANDWIDTH ||\n                kind == MEMKIND_HBW ||\n                kind == MEMKIND_HBW_HUGETLB ||\n                kind == MEMKIND_HBW_PREFERRED ||\n                kind == MEMKIND_HBW_INTERLEAVE;\n    }\n\n    virtual size_t get_page_size()\n    {\n        if (kind == MEMKIND_HUGETLB ||\n            kind == MEMKIND_HBW_HUGETLB ||\n            kind == MEMKIND_HBW_PREFERRED_HUGETLB ||\n            (flags & MEMKIND_MASK_PAGE_SIZE_2MB)) {\n            return 2*MB;\n        }\n        return 4*KB;\n    }\n\n    virtual ~MemkindAllocator()\n    {\n        int ret = memkind_destroy_kind(kind);\n        assert(ret == MEMKIND_SUCCESS);\n        kind = NULL;\n    }\n\nprivate:\n    memkind_t kind = NULL;\n    memkind_memtype_t memtype = memkind_memtype_t();\n    memkind_policy_t policy = MEMKIND_POLICY_MAX_VALUE;\n    memkind_bits_t flags = memkind_bits_t();\n};\n\nclass HbwmallocAllocator : public Allocator\n{\npublic:\n\n    HbwmallocAllocator(hbw_policy_t hbw_policy)\n    {\n        hbw_set_policy(hbw_policy);\n    }\n\n    virtual void *malloc(size_t size)\n    {\n        return hbw_malloc(size);\n    }\n\n    virtual void *calloc(size_t num, size_t size)\n    {\n        return hbw_calloc(num, size);\n    }\n\n    virtual void *realloc(void *ptr, size_t size)\n    {\n        return hbw_realloc(ptr, size);\n    }\n\n    virtual void set_memalign_page_size(hbw_pagesize_t psize)\n    {\n        page_size = psize;\n    }\n\n    virtual int memalign(void **ptr, size_t alignment, size_t size)\n    {\n        if (page_size == HBW_PAGESIZE_4KB) {\n            return hbw_posix_memalign(ptr, alignment, size);\n        } else {\n            return hbw_posix_memalign_psize(ptr, alignment, size, page_size);\n        }\n    }\n\n    virtual void free(void *ptr)\n    {\n        hbw_free(ptr);\n    }\n\n    virtual int get_numa_policy()\n    {\n        if (hbw_get_policy() == HBW_POLICY_INTERLEAVE) {\n            return MPOL_INTERLEAVE;\n        } else if (hbw_get_policy() == HBW_POLICY_PREFERRED) {\n            return MPOL_PREFERRED;\n        } else if (hbw_get_policy() == HBW_POLICY_BIND ||\n                   hbw_get_policy() == HBW_POLICY_BIND_ALL) {\n            return MPOL_BIND;\n        }\n        return -1;\n    }\n\n    virtual bool is_high_bandwidth()\n    {\n        return  hbw_check_available() == 0;\n    }\n\n    virtual size_t get_page_size()\n    {\n        if (page_size == HBW_PAGESIZE_2MB) {\n            return 2*MB;\n        }\n        return 4*KB;\n    }\n\nprivate:\n    hbw_pagesize_t page_size = HBW_PAGESIZE_4KB;\n};\n\n"
  },
  {
    "path": "deps/memkind/src/test/Makefile.mk",
    "content": "#\n#  Copyright (C) 2014 - 2018 Intel Corporation.\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#  1. Redistributions of source code must retain the above copyright notice(s),\n#     this list of conditions and the following disclaimer.\n#  2. Redistributions in binary form must reproduce the above copyright notice(s),\n#     this list of conditions and the following disclaimer in the documentation\n#     and/or other materials provided with the distribution.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n#  EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n#  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n#\n\nAM_CPPFLAGS += -Itest/gtest_fused -DMEMKIND_DEPRECATED\\(x\\)=x\n\ncheck_PROGRAMS += test/all_tests \\\n                  test/environ_err_hbw_malloc_test \\\n                  test/decorator_test \\\n                  test/allocator_perf_tool_tests \\\n                  test/autohbw_test_helper \\\n                  test/trace_mechanism_test_helper \\\n                  test/gb_page_tests_bind_policy \\\n                  test/freeing_memory_segfault_test \\\n                  test/locality_test \\\n                  # end\n\nTESTS += test/test.sh\n\nEXTRA_DIST += test/memkind-afts.ts \\\n              test/memkind-afts-ext.ts \\\n              test/memkind-slts.ts \\\n              test/memkind-perf.ts \\\n              test/memkind-perf-ext.ts \\\n              test/memkind-pytests.ts \\\n              test/hbw_detection_test.py \\\n              test/autohbw_test.py \\\n              test/trace_mechanism_test.py \\\n              test/python_framework/cmd_helper.py \\\n              test/python_framework/huge_page_organizer.py \\\n              test/python_framework/__init__.py \\\n              test/draw_plots.py \\\n              test/run_alloc_benchmark.sh \\\n              test/gtest_fused/gtest/gtest-all.cc \\\n              test/gtest_fused/gtest/gtest.h \\\n              # end\n\n\n\ntest_all_tests_LDADD = libmemkind.la\ntest_environ_err_hbw_malloc_test_LDADD = libmemkind.la\ntest_decorator_test_LDADD = libmemkind.la\ntest_allocator_perf_tool_tests_LDADD = libmemkind.la\ntest_autohbw_test_helper_LDADD = libmemkind.la\ntest_gb_page_tests_bind_policy_LDADD = libmemkind.la\ntest_trace_mechanism_test_helper_LDADD = libmemkind.la\ntest_freeing_memory_segfault_test_LDADD = libmemkind.la\n\nfused_gtest = test/gtest_fused/gtest/gtest-all.cc \\\n              test/main.cpp \\\n              # end\n\ntest_all_tests_SOURCES = $(fused_gtest) \\\n                         test/Allocator.hpp \\\n                         test/TestPolicy.hpp \\\n                         test/common.h \\\n                         test/bat_tests.cpp \\\n                         test/trial_generator.cpp \\\n                         test/check.cpp \\\n                         test/multithreaded_tests.cpp \\\n                         test/trial_generator.h \\\n                         test/static_kinds_list.h \\\n                         test/check.h \\\n                         test/negative_tests.cpp \\\n                         test/error_message_tests.cpp \\\n                         test/get_arena_test.cpp \\\n                         test/memkind_pmem_tests.cpp \\\n                         test/memkind_pmem_long_time_tests.cpp \\\n                         test/performance/operations.hpp \\\n                         test/performance/perf_tests.hpp \\\n                         test/performance/perf_tests.cpp \\\n                         test/performance/framework.hpp \\\n                         test/performance/framework.cpp \\\n                         test/hbw_allocator_tests.cpp \\\n                         test/memkind_versioning_tests.cpp \\\n                         test/static_kinds_tests.cpp \\\n                         test/hbw_verify_function_test.cpp \\\n                         test/dlopen_test.cpp \\\n                         test/pmem_allocator_tests.cpp \\\n                         #end\n\ntest_locality_test_SOURCES = $(fused_gtest) test/allocator_perf_tool/Allocation_info.cpp test/locality_test.cpp\ntest_locality_test_LDADD = libmemkind.la\n\ntest_locality_test_CPPFLAGS = -fopenmp -O0 -Wno-error $(AM_CPPFLAGS)\ntest_locality_test_CXXFLAGS = -fopenmp -O0 -Wno-error $(AM_CPPFLAGS)\n\ntest_environ_err_hbw_malloc_test_SOURCES = test/environ_err_hbw_malloc_test.cpp\ntest_decorator_test_SOURCES = $(fused_gtest) test/decorator_test.cpp test/decorator_test.h\ntest_autohbw_test_helper_SOURCES = test/autohbw_test_helper.c\ntest_gb_page_tests_bind_policy_SOURCES = $(fused_gtest) test/gb_page_tests_bind_policy.cpp test/trial_generator.cpp test/check.cpp\ntest_trace_mechanism_test_helper_SOURCES = test/trace_mechanism_test_helper.c\ntest_freeing_memory_segfault_test_SOURCES = $(fused_gtest) test/freeing_memory_segfault_test.cpp\n\n#Tests based on Allocator Perf Tool\nallocator_perf_tool_library_sources = test/allocator_perf_tool/AllocationSizes.hpp \\\n                                      test/allocator_perf_tool/Allocation_info.hpp \\\n                                      test/allocator_perf_tool/Allocation_info.cpp \\\n                                      test/allocator_perf_tool/Allocator.hpp \\\n                                      test/allocator_perf_tool/AllocatorFactory.hpp \\\n                                      test/allocator_perf_tool/CSVLogger.hpp \\\n                                      test/allocator_perf_tool/CommandLine.hpp \\\n                                      test/allocator_perf_tool/Configuration.hpp \\\n                                      test/allocator_perf_tool/ConsoleLog.hpp \\\n                                      test/allocator_perf_tool/FunctionCalls.hpp \\\n                                      test/allocator_perf_tool/FunctionCallsPerformanceTask.cpp \\\n                                      test/allocator_perf_tool/FunctionCallsPerformanceTask.h \\\n                                      test/allocator_perf_tool/GTestAdapter.hpp \\\n                                      test/allocator_perf_tool/Iterator.hpp \\\n                                      test/allocator_perf_tool/JemallocAllocatorWithTimer.hpp \\\n                                      test/allocator_perf_tool/MemkindAllocatorWithTimer.hpp \\\n                                      test/allocator_perf_tool/Numastat.hpp \\\n                                      test/allocator_perf_tool/Runnable.hpp \\\n                                      test/allocator_perf_tool/PmemMockup.cpp \\\n                                      test/allocator_perf_tool/PmemMockup.hpp \\\n                                      test/allocator_perf_tool/ScenarioWorkload.cpp \\\n                                      test/allocator_perf_tool/ScenarioWorkload.h \\\n                                      test/allocator_perf_tool/StandardAllocatorWithTimer.hpp \\\n                                      test/allocator_perf_tool/Stats.hpp \\\n                                      test/allocator_perf_tool/StressIncreaseToMax.cpp \\\n                                      test/allocator_perf_tool/StressIncreaseToMax.h \\\n                                      test/allocator_perf_tool/Task.hpp \\\n                                      test/allocator_perf_tool/TaskFactory.hpp \\\n                                      test/allocator_perf_tool/Tests.hpp \\\n                                      test/allocator_perf_tool/Thread.hpp \\\n                                      test/allocator_perf_tool/TimerSysTime.hpp \\\n                                      test/allocator_perf_tool/VectorIterator.hpp \\\n                                      test/allocator_perf_tool/Workload.hpp \\\n                                      test/allocator_perf_tool/WrappersMacros.hpp \\\n                                      test/allocator_perf_tool/HugePageUnmap.hpp \\\n                                      test/allocator_perf_tool/HugePageOrganizer.hpp \\\n                                      test/allocator_perf_tool/HBWmallocAllocatorWithTimer.hpp \\\n                                      test/memory_manager.h \\\n                                      test/random_sizes_allocator.h \\\n                                      test/proc_stat.h \\\n                                      # end\n\n\ntest_allocator_perf_tool_tests_SOURCES = $(allocator_perf_tool_library_sources) \\\n                                         $(fused_gtest) \\\n                                         test/allocate_to_max_stress_test.cpp \\\n                                         test/heap_manager_init_perf_test.cpp \\\n                                         test/huge_page_test.cpp \\\n                                         test/alloc_performance_tests.cpp \\\n                                         test/memory_footprint_test.cpp \\\n                                         # end\n\n\ntest_allocator_perf_tool_tests_CPPFLAGS = -Itest/allocator_perf_tool/ -lpthread -lnuma -O0 -Wno-error $(AM_CPPFLAGS)\ntest_allocator_perf_tool_tests_CXXFLAGS = -Itest/allocator_perf_tool/ -lpthread -lnuma -O0 -Wno-error $(AM_CPPFLAGS)\n\nNUMAKIND_MAX = 2048\ntest_all_tests_CXXFLAGS = $(AM_CXXFLAGS) $(CXXFLAGS) $(OPENMP_CFLAGS) -DNUMAKIND_MAX=$(NUMAKIND_MAX) -ldl\n\n#Allocator Perf Tool stand alone app\ncheck_PROGRAMS += test/perf_tool\ntest_perf_tool_LDADD = libmemkind.la\ntest_perf_tool_SOURCES = $(allocator_perf_tool_library_sources) \\\n                         test/allocator_perf_tool/main.cpp \\\n                         # end\n\n\ntest_perf_tool_CPPFLAGS = -Itest/allocator_perf_tool/ -lpthread -lnuma -O0 -Wno-error $(AM_CPPFLAGS)\ntest_perf_tool_CXXFLAGS = -Itest/allocator_perf_tool/ -lpthread -lnuma -O0 -Wno-error $(AM_CPPFLAGS)\nif HAVE_CXX11\ntest_perf_tool_CPPFLAGS += -std=c++11\ntest_perf_tool_CXXFLAGS += -std=c++11\nendif\n\n#Alloc benchmark\ncheck_PROGRAMS += test/alloc_benchmark_hbw \\\n                  test/alloc_benchmark_glibc \\\n                  test/alloc_benchmark_tbb \\\n                  test/alloc_benchmark_pmem \\\n                  # end\n\ntest_alloc_benchmark_hbw_LDADD = libmemkind.la\ntest_alloc_benchmark_hbw_SOURCES = test/alloc_benchmark.c\ntest_alloc_benchmark_hbw_CFLAGS = -O0 -g -fopenmp -Wall -DHBWMALLOC -lmemkind\n\ntest_alloc_benchmark_glibc_LDADD = libmemkind.la\ntest_alloc_benchmark_glibc_SOURCES = test/alloc_benchmark.c\ntest_alloc_benchmark_glibc_CFLAGS = -O0 -g -fopenmp -Wall\n\ntest_alloc_benchmark_tbb_LDADD = libmemkind.la\ntest_alloc_benchmark_tbb_SOURCES = test/alloc_benchmark.c \\\n                                   test/load_tbbmalloc_symbols.c \\\n                                   test/tbbmalloc.h \\\n                                   # end\ntest_alloc_benchmark_tbb_CFLAGS = -O0 -g -fopenmp -Wall -DTBBMALLOC -ldl\n\ntest_alloc_benchmark_pmem_LDADD = libmemkind.la\ntest_alloc_benchmark_pmem_SOURCES = test/alloc_benchmark.c\ntest_alloc_benchmark_pmem_CFLAGS = -O0 -g -fopenmp -Wall -DPMEMMALLOC -ldl\n\n# Examples as tests\ncheck_PROGRAMS += test/hello_memkind \\\n                  test/autohbw_candidates \\\n                  test/hello_memkind_debug \\\n                  test/hello_hbw \\\n                  test/filter_memkind \\\n                  test/pmem_kinds \\\n                  test/pmem_malloc \\\n                  test/pmem_malloc_unlimited \\\n                  test/pmem_usable_size \\\n                  test/pmem_alignment \\\n                  test/pmem_and_default_kind \\\n                  test/pmem_multithreads \\\n                  test/pmem_multithreads_onekind \\\n                  test/pmem_free_with_unknown_kind \\\n                  # end\nif HAVE_CXX11\ncheck_PROGRAMS += test/memkind_allocated \\\n                  test/pmem_cpp_allocator\nendif\n\n\ntest_hello_memkind_LDADD = libmemkind.la\ntest_hello_memkind_debug_LDADD = libmemkind.la\ntest_hello_hbw_LDADD = libmemkind.la\ntest_filter_memkind_LDADD = libmemkind.la\ntest_pmem_kinds_LDADD = libmemkind.la\ntest_pmem_malloc_LDADD = libmemkind.la\ntest_pmem_malloc_unlimited_LDADD = libmemkind.la\ntest_pmem_usable_size_LDADD = libmemkind.la\ntest_pmem_alignment_LDADD = libmemkind.la\ntest_pmem_and_default_kind_LDADD = libmemkind.la\ntest_pmem_multithreads_LDADD = libmemkind.la\ntest_pmem_multithreads_onekind_LDADD = libmemkind.la\ntest_pmem_free_with_unknown_kind_LDADD = libmemkind.la\ntest_autohbw_candidates_LDADD = libmemkind.la \\\n                                # end\nif HAVE_CXX11\ntest_memkind_allocated_LDADD = libmemkind.la\ntest_pmem_cpp_allocator_LDADD = libmemkind.la\nendif\n\ntest_hello_memkind_SOURCES = examples/hello_memkind_example.c\ntest_hello_memkind_debug_SOURCES = examples/hello_memkind_example.c examples/memkind_decorator_debug.c\ntest_hello_hbw_SOURCES = examples/hello_hbw_example.c\ntest_filter_memkind_SOURCES = examples/filter_example.c\ntest_pmem_kinds_SOURCES = examples/pmem_kinds.c\ntest_pmem_malloc_SOURCES = examples/pmem_malloc.c\ntest_pmem_malloc_unlimited_SOURCES = examples/pmem_malloc_unlimited.c\ntest_pmem_usable_size_SOURCES = examples/pmem_usable_size.c\ntest_pmem_alignment_SOURCES = examples/pmem_alignment.c\ntest_pmem_and_default_kind_SOURCES = examples/pmem_and_default_kind.c\ntest_pmem_multithreads_SOURCES = examples/pmem_multithreads.c\ntest_pmem_multithreads_onekind_SOURCES = examples/pmem_multithreads_onekind.c\ntest_pmem_free_with_unknown_kind_SOURCES = examples/pmem_free_with_unknown_kind.c\ntest_autohbw_candidates_SOURCES = examples/autohbw_candidates.c\ntest_libautohbw_la_SOURCES = autohbw/autohbw.c\nnoinst_LTLIBRARIES += test/libautohbw.la\nif HAVE_CXX11\ntest_memkind_allocated_SOURCES = examples/memkind_allocated_example.cpp examples/memkind_allocated.hpp\ntest_pmem_cpp_allocator_SOURCES = examples/pmem_cpp_allocator.cpp\nendif\n\nclean-local: test-clean\n\ntest-clean:\n\tfind test \\( -name \"*.gcda\" -o -name \"*.gcno\" \\) -type f -delete\n"
  },
  {
    "path": "deps/memkind/src/test/README",
    "content": "MEMKIND TESTS\n=============\n\n\nDISCLAIMER\n==========\nSEE COPYING FILE FOR LICENSE INFORMATION.\n\nTHIS SOFTWARE IS PROVIDED AS A DEVELOPMENT SNAPSHOT TO AID\nCOLLABORATION AND WAS NOT ISSUED AS A RELEASED PRODUCT BY INTEL.\n\nLAST UPDATE\n===========\nAgata Wozniak <agata.wozniak@intel.com>\nWednesday May 11 2016\n\nTESTS SCENARIOS\n===============\n\nmemkind-afts\n------------\nThis is scenario for basic functional tests. Maximum number of allocations per numa node:\n- 512 x 4KB pages (summary: 2GB)\n- 16 x 2MB pages\n\nmemkind-afts-ext\n----------------\nThe same tests as in memkind-afts, however this tests are more ravenous and needs more memory\navailable. Special test cases prepared only for this scenario are described as ravenous\nand needs at least:\n- 512 x 4KB pages (summary: 2GB)\n- 16 x 2MB pages\nmemory available per numa node.\n\nmemkind-perf\n------------\nThis is scenario for performance tests, excluding tests that allocate more than:\n- 512 x 4KB pages (summary: 2GB)\n- 16 x 2MB pages\n\nmemkind-perf-ext\n----------------\nThis test scenario extend memkind-perf for tests that allocate more than:\n- 512 x 4KB pages (summary: 2GB)\n- 16 x 2MB pages\nAllocPerformanceTest is a group of performance tests for *alloc() execution. TC properties:\n- 6 kinds (MEMKIND_DEFAULT, MEMKIND_HBW, MEMKIND_INTERLEAVE, MEMKIND_HBW_INTERLEAVE, MEMKIND_HBW_PREFERRED, PMEM kind)\n- 3 calls (malloc, calloc, realloc)\n- 5 allocation sizes (100bytes, 4096bytes, 1000bytes, 1001bytes, 1.5MB - 1572864bytes)\n- single thread and multithreaded (10threads and 72threads)\nAll tests record three values:\n- total_time_spend_on_alloc (total time spend on *alloc() call during the test - less is better)\n- alloc_operations_per_thread (number of done allocations per thread - this value is constant)\n- ref_delta_time_percent (percent delta between standard allocator and current tested kind - less is better)\n\nmemkind-slts\n------------\nThis is scenario for all stress and longevity tests.\n\n\nMANUAL TESTING\n==============\nIn order to run tests on local machine, test.sh should be executed.\nWhen test.sh is run, all tests are executed as long as -d (skip MCDRAM nodes detection), -m (skip\ntests that require 2MB pages) parameters are not used.\n"
  },
  {
    "path": "deps/memkind/src/test/TestPolicy.hpp",
    "content": "/*\n * Copyright (C) 2017 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#include <memkind/internal/memkind_hbw.h>\n\n#include \"common.h\"\n#include \"allocator_perf_tool/GTestAdapter.hpp\"\n#include \"numa.h\"\n\n#include <memory>\n\nnamespace TestPolicy\n{\n    typedef std::unique_ptr<struct bitmask, decltype(&numa_free_nodemask)>\n    unique_bitmask_ptr;\n\n    unique_bitmask_ptr make_nodemask_ptr()\n    {\n        return std::move(unique_bitmask_ptr(numa_allocate_nodemask(),\n                                            numa_free_nodemask));\n    }\n\n    unique_bitmask_ptr make_cpumask_ptr()\n    {\n        return std::move(unique_bitmask_ptr(numa_allocate_cpumask(),\n                                            numa_free_nodemask));\n    }\n\n    int get_num_of_pages(const size_t size, const size_t page_size)\n    {\n        size_t pages_number = size / page_size;\n        pages_number += size % page_size ? 1 : 0;\n        return pages_number;\n    }\n\n    std::vector<void *> get_address_of_pages(const void *ptr,\n                                             const size_t pages_number, const size_t page_size)\n    {\n        std::vector<void *> address(pages_number);\n        const size_t page_mask = ~(page_size-1);\n        address[0] = (void *)((uintptr_t)ptr &\n                              page_mask); //aligned address of first page\n        for (size_t page_num = 1; page_num < pages_number; page_num++) {\n            address[page_num] = (char *)address[page_num-1] + page_size;\n        }\n        return address;\n    }\n\n    void record_page_association(const void *ptr, const size_t size,\n                                 const size_t page_size)\n    {\n        size_t pages_number = get_num_of_pages(size, page_size);\n        std::vector<void *> address = get_address_of_pages(ptr, pages_number,\n                                                           page_size);\n\n        int max_node_id = numa_max_node();\n        std::vector<int> nodes(pages_number);\n        std::vector<int> pages_on_node(max_node_id+1);\n\n        if (move_pages(0, pages_number, address.data(), NULL, nodes.data(),\n                       MPOL_MF_MOVE)) {\n            fprintf(stderr, \"Error: move_pages() returned %s\\n\", strerror(errno));\n            return;\n        }\n\n        for (size_t i = 0; i < pages_number; i++) {\n            if (nodes[i] < 0) {\n                fprintf(stderr,\"Error: status of page %p is %d\\n\", address[i], nodes[i]);\n                return;\n            } else {\n                pages_on_node[nodes[i]]++;\n            }\n        }\n\n        for (size_t i = 0; i < (size_t)max_node_id + 1; i++) {\n            if (pages_on_node[i] > 0) {\n                char buffer[1024];\n                snprintf(buffer, sizeof(buffer), \"Node%zd\", i);\n                GTestAdapter::RecordProperty(buffer, pages_on_node[i]);\n            }\n        }\n    }\n\n    void check_numa_nodes(unique_bitmask_ptr &expected_bitmask, int policy,\n                          void *ptr, size_t size)\n    {\n\n        const size_t page_size = sysconf(_SC_PAGESIZE);\n        size_t pages_number = get_num_of_pages(size, page_size);\n        std::vector<void *> address = get_address_of_pages(ptr, pages_number,\n                                                           page_size);\n        unique_bitmask_ptr returned_bitmask = make_nodemask_ptr();\n        int status = -1;\n\n        for (size_t page_num = 0; page_num < address.size(); page_num++) {\n            ASSERT_EQ(0, get_mempolicy(&status, returned_bitmask->maskp,\n                                       returned_bitmask->size, address[page_num], MPOL_F_ADDR));\n            ASSERT_EQ(policy, status);\n            switch(policy) {\n                case MPOL_INTERLEAVE:\n                    EXPECT_TRUE(numa_bitmask_equal(expected_bitmask.get(), returned_bitmask.get()));\n                    break;\n                case MPOL_DEFAULT:\n                    break;\n                case MPOL_BIND:\n                case MPOL_PREFERRED:\n                    for(int i=0; i < numa_num_possible_nodes(); i++) {\n                        if(numa_bitmask_isbitset(returned_bitmask.get(), i)) {\n                            EXPECT_TRUE(numa_bitmask_isbitset(expected_bitmask.get(), i));\n                        }\n                    }\n                    break;\n                default:\n                    assert(!\"Unknown policy\\n\");\n            }\n        }\n    }\n\n    void check_hbw_numa_nodes(int policy, void *ptr, size_t size)\n    {\n        unique_bitmask_ptr expected_bitmask = make_nodemask_ptr();\n\n        memkind_hbw_all_get_mbind_nodemask(NULL, expected_bitmask->maskp,\n                                           expected_bitmask->size);\n        check_numa_nodes(expected_bitmask, policy, ptr, size);\n    }\n\n    void check_all_numa_nodes(int policy, void *ptr, size_t size)\n    {\n        if (policy != MPOL_INTERLEAVE && policy != MPOL_DEFAULT) return;\n\n        unique_bitmask_ptr expected_bitmask = make_nodemask_ptr();\n\n        for(int i=0; i < numa_num_configured_nodes(); i++) {\n            numa_bitmask_setbit(expected_bitmask.get(), i);\n        }\n\n        check_numa_nodes(expected_bitmask, policy, ptr, size);\n    }\n}\n"
  },
  {
    "path": "deps/memkind/src/test/alloc_benchmark.c",
    "content": "/*\n * Copyright (C) 2016 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <stdio.h>\n#include <stdlib.h>\n#include <time.h>\n#include <sys/time.h>\n#include <unistd.h>\n#include <stdint.h>\n#include <limits.h>\n#ifdef _OPENMP\n#include <omp.h>\n#endif\n#if defined(HBWMALLOC)\n#include <hbwmalloc.h>\n#define MALLOC_FN hbw_malloc\n#define FREE_FN hbw_free\n#elif defined (TBBMALLOC)\n#include \"tbbmalloc.h\"\n#define MALLOC_FN scalable_malloc\n#define FREE_FN scalable_free\n#elif defined (PMEMMALLOC)\n#include <sys/stat.h>\n#include \"memkind.h\"\n#define MALLOC_FN(x) memkind_malloc(pmem_bench_kind, (x))\n#define FREE_FN(x) memkind_free(pmem_bench_kind, (x))\n\nstatic const size_t PMEM_PART_SIZE = 0;\nstatic const char *PMEM_DIR = \"/tmp/\";\nstatic memkind_t pmem_bench_kind;\n#else\n#define MALLOC_FN malloc\n#define FREE_FN free\n#endif\n\ndouble ctimer(void);\nvoid usage(char *name);\n\nint main(int argc, char *argv[])\n{\n#ifdef _OPENMP\n    int nthr = omp_get_max_threads();\n#else\n    int nthr = 1;\n#endif\n    long n, size;\n    size_t alloc_size;\n    unsigned long i;\n    double dt, t_start, t_end, t_malloc, t_free, t_first_malloc, t_first_free,\n           malloc_time = 0.0, free_time = 0.0, first_malloc_time, first_free_time;\n    void *ptr;\n#ifdef TBBMALLOC\n    int ret;\n\n    ret = load_tbbmalloc_symbols();\n    if (ret) {\n        printf(\"Error: TBB symbols not loaded (ret: %d)\\n\", ret);\n        return EXIT_FAILURE;\n    }\n#endif\n#ifdef PMEMMALLOC\n    struct stat st;\n\n    /* Handle command line arguments */\n    if (argc == 3 || argc == 4) {\n        n = atol(argv[1]);\n        size = atol(argv[2]);\n\n        if (argc == 4) {\n            if (stat(argv[3], &st) != 0 || !S_ISDIR(st.st_mode)) {\n                usage(argv[0]);\n                return EXIT_FAILURE;\n            } else {\n                PMEM_DIR = argv[3];\n            }\n        }\n    }\n    if ((argc != 3 && argc != 4) || n < 0 || size < 0 || size > (LONG_MAX >> 10)) {\n        usage(argv[0]);\n        return EXIT_FAILURE;\n    }\n\n    int err = memkind_create_pmem(PMEM_DIR, PMEM_PART_SIZE, &pmem_bench_kind);\n    if (err) {\n        printf(\"Error: memkind_create_pmem failed %d\\n\", err);\n        return EXIT_FAILURE;\n    }\n#else\n    /* Handle command line arguments */\n    if (argc == 3) {\n        n = atol(argv[1]);\n        size = atol(argv[2]);\n    }\n    if (argc != 3 || n < 0 || size < 0 || size > (LONG_MAX >> 10)) {\n        usage(argv[0]);\n        return EXIT_FAILURE;\n    }\n#endif\n    alloc_size = (size_t) size * 1024;\n\n    /* Get pagesize and compute page_mask */\n    const size_t page_size = sysconf(_SC_PAGESIZE);\n    const size_t page_mask = ~(page_size-1);\n\n    /* Warm up */\n    t_first_malloc = ctimer();\n    ptr = MALLOC_FN(alloc_size);\n    first_malloc_time = ctimer() - t_first_malloc;\n    if (ptr == NULL) {\n        printf(\"Error: first allocation failed\\n\");\n        return EXIT_FAILURE;\n    }\n    t_first_free = ctimer();\n    FREE_FN(ptr);\n    first_free_time = ctimer() - t_first_free;\n    ptr = NULL;\n\n    t_start = ctimer();\n    #pragma omp parallel private(i,t_malloc,t_free,ptr) reduction(max:malloc_time,free_time)\n    {\n        malloc_time = 0.0;\n        free_time = 0.0;\n        for (i=0; i<n; i++) {\n            t_malloc = ctimer();\n            ptr = (void *) MALLOC_FN(alloc_size);\n            malloc_time += ctimer() - t_malloc;\n            #pragma omp critical\n            {\n                if (ptr == NULL) {\n                    printf(\"Error: allocation failed\\n\");\n                    exit(EXIT_FAILURE);\n                }\n            }\n\n            /* Make sure to touch every page */\n            char *end = ptr + alloc_size;\n            char *aligned_beg = (char *)((uintptr_t)ptr & page_mask);\n            while(aligned_beg < end) {\n                char *temp_ptr = (char *) aligned_beg;\n                char value = temp_ptr[0];\n                temp_ptr[0] = value;\n                aligned_beg += page_size;\n            }\n\n            t_free = ctimer();\n            FREE_FN(ptr);\n            free_time += ctimer() - t_free;\n            ptr = NULL;\n        }\n    }\n    t_end = ctimer();\n    dt = t_end - t_start;\n\n    printf(\"%d %lu %8.6f %8.6f  %8.6f  %8.6f  %8.6f\\n\",\n           nthr, size, dt/n, malloc_time/n, free_time/n, first_malloc_time,\n           first_free_time);\n#ifdef PMEMMALLOC\n    err = memkind_destroy_kind(pmem_bench_kind);\n    if (err) {\n        printf(\"Error: memkind_destroy_kind failed %d\\n\", err);\n        return EXIT_FAILURE;\n    }\n#endif\n    return EXIT_SUCCESS;\n}\n\nvoid usage(char *name)\n{\n#ifdef PMEMMALLOC\n    printf(\"Usage: %s <N> <SIZE> [DIR], where \\n\"\n           \"N is an number of repetitions \\n\"\n           \"SIZE is an allocation size in kbytes\\n\"\n           \"DIR is a custom path for PMEM kind, (default: \\\"/tmp/\\\")\\n\",\n           name);\n#else\n    printf(\"Usage: %s <N> <SIZE>, where \\n\"\n           \"N is an number of repetitions \\n\"\n           \"SIZE is an allocation size in kbytes\\n\", name);\n#endif\n}\n\ninline double ctimer()\n{\n    struct timeval tmr;\n    gettimeofday(&tmr, NULL);\n    /* Return time in ms */\n    return (tmr.tv_sec + tmr.tv_usec/1000000.0)*1000;\n}\n"
  },
  {
    "path": "deps/memkind/src/test/alloc_performance_tests.cpp",
    "content": "/*\n* Copyright (C) 2016 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n\n#include \"common.h\"\n#include \"allocator_perf_tool/TaskFactory.hpp\"\n#include \"allocator_perf_tool/Stats.hpp\"\n#include \"allocator_perf_tool/Thread.hpp\"\n#include \"allocator_perf_tool/GTestAdapter.hpp\"\n#include \"allocator_perf_tool/PmemMockup.hpp\"\n\nstatic const size_t PMEM_PART_SIZE = 0;\nextern const char  *PMEM_DIR;\n\nclass AllocPerformanceTest: public :: testing::Test\n{\nprivate:\n    AllocatorFactory allocator_factory;\n\nprotected:\n    void SetUp()\n    {\n        allocator_factory.initialize_allocator(AllocatorTypes::STANDARD_ALLOCATOR);\n\n        int err = memkind_create_pmem(PMEM_DIR, PMEM_PART_SIZE, &MEMKIND_PMEM_MOCKUP);\n        ASSERT_EQ(0, err);\n        ASSERT_TRUE(nullptr != MEMKIND_PMEM_MOCKUP);\n    }\n\n    void TearDown()\n    {\n        int err = memkind_destroy_kind(MEMKIND_PMEM_MOCKUP);\n        ASSERT_EQ(0, err);\n    }\n\n    float run(unsigned kind, unsigned call, size_t threads_number,\n              size_t alloc_size, unsigned mem_operations_num)\n    {\n        TaskFactory task_factory;\n        std::vector<Thread *> threads;\n        std::vector<Task *> tasks;\n        TypesConf func_calls;\n        TypesConf allocator_types;\n\n        func_calls.enable_type(FunctionCalls::FREE);\n        func_calls.enable_type(call);\n        allocator_types.enable_type(kind);\n\n        TaskConf conf = {\n            mem_operations_num, //number of memory operations\n            {\n                mem_operations_num, //number of memory operations\n                alloc_size, //min. size of single allocation\n                alloc_size //max. size of single allocatioion\n            },\n            func_calls, //enable function calls\n            allocator_types, //enable allocators\n            11, //random seed\n            false, //does not log memory operations and statistics to csv file\n        };\n\n        for (int i=0; i<threads_number; i++) {\n            Task *task = task_factory.create(conf);\n            tasks.push_back(task);\n            threads.push_back(new Thread(task));\n            conf.seed += 1;\n        }\n\n        ThreadsManager threads_manager(threads);\n        threads_manager.start();\n        threads_manager.barrier();\n\n        TimeStats time_stats;\n        for (int i=0; i<tasks.size(); i++) {\n            time_stats += tasks[i]->get_results();\n        }\n\n        return time_stats.stats[kind][call].total_time;\n    }\n\n    void run_test(unsigned kind, unsigned call, size_t threads_number,\n                  size_t alloc_size, unsigned mem_operations_num)\n    {\n        allocator_factory.initialize_allocator(kind);\n        float ref_time = run(AllocatorTypes::STANDARD_ALLOCATOR, call, threads_number,\n                             alloc_size, mem_operations_num);\n        float perf_time = run(kind, call, threads_number, alloc_size,\n                              mem_operations_num);\n        float ref_delta_time_percent = allocator_factory.calc_ref_delta(ref_time,\n                                                                        perf_time);\n\n        GTestAdapter::RecordProperty(\"total_time_spend_on_alloc\", perf_time);\n        GTestAdapter::RecordProperty(\"alloc_operations_per_thread\", mem_operations_num);\n        GTestAdapter::RecordProperty(\"ref_delta_time_percent\", ref_delta_time_percent);\n    }\n\n};\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_malloc_1_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::MALLOC, 1, 100, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_malloc_1_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::MALLOC, 1, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_malloc_1_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::MALLOC, 1, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_malloc_1_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::MALLOC, 1, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_malloc_1_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::MALLOC, 1, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_malloc_10_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::MALLOC, 10, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_malloc_10_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::MALLOC, 10, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_malloc_10_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::MALLOC, 10, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_malloc_10_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::MALLOC, 10, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_malloc_10_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::MALLOC, 10, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_malloc_72_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::MALLOC, 72, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_malloc_72_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::MALLOC, 72, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_malloc_72_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::MALLOC, 72, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_malloc_72_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::MALLOC, 72, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_malloc_72_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::MALLOC, 72, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_calloc_1_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::CALLOC, 1, 100, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_calloc_1_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::CALLOC, 1, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_calloc_1_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::CALLOC, 1, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_calloc_1_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::CALLOC, 1, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_calloc_1_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::CALLOC, 1, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_calloc_10_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::CALLOC, 10, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_calloc_10_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::CALLOC, 10, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_calloc_10_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::CALLOC, 10, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_calloc_10_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::CALLOC, 10, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_calloc_10_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::CALLOC, 10, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_calloc_72_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::CALLOC, 72, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_calloc_72_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::CALLOC, 72, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_calloc_72_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::CALLOC, 72, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_calloc_72_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::CALLOC, 72, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_calloc_72_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::CALLOC, 72, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_realloc_1_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::REALLOC, 1, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_realloc_1_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::REALLOC, 1, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_realloc_1_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::REALLOC, 1, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_realloc_1_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::REALLOC, 1, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_realloc_1_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::REALLOC, 1, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_realloc_10_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::REALLOC, 10, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_realloc_10_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::REALLOC, 10, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_realloc_10_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::REALLOC, 10, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_realloc_10_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::REALLOC, 10, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_realloc_10_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::REALLOC, 10, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_realloc_72_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::REALLOC, 72, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_realloc_72_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::REALLOC, 72, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_realloc_72_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::REALLOC, 72, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_realloc_72_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::REALLOC, 72, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_DEFAULT_realloc_72_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_DEFAULT, FunctionCalls::REALLOC, 72, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_malloc_1_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::MALLOC, 1, 100, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_malloc_1_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::MALLOC, 1, 4096, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_malloc_1_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::MALLOC, 1, 1000, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_malloc_1_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::MALLOC, 1, 1001, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_malloc_1_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::MALLOC, 1, 1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_malloc_10_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::MALLOC, 10, 100, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_malloc_10_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::MALLOC, 10, 4096, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_malloc_10_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::MALLOC, 10, 1000, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_malloc_10_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::MALLOC, 10, 1001, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_malloc_10_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::MALLOC, 10, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_malloc_72_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::MALLOC, 72, 100, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_malloc_72_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::MALLOC, 72, 4096, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_malloc_72_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::MALLOC, 72, 1000, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_malloc_72_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::MALLOC, 72, 1001, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_malloc_72_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::MALLOC, 72, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_calloc_1_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::CALLOC, 1, 100, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_calloc_1_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::CALLOC, 1, 4096, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_calloc_1_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::CALLOC, 1, 1000, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_calloc_1_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::CALLOC, 1, 1001, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_calloc_1_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::CALLOC, 1, 1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_calloc_10_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::CALLOC, 10, 100, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_calloc_10_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::CALLOC, 10, 4096, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_calloc_10_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::CALLOC, 10, 1000, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_calloc_10_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::CALLOC, 10, 1001, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_calloc_10_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::CALLOC, 10, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_calloc_72_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::CALLOC, 72, 100, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_calloc_72_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::CALLOC, 72, 4096, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_calloc_72_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::CALLOC, 72, 1000, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_calloc_72_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::CALLOC, 72, 1001, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_ext_MEMKIND_HBW_calloc_72_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::CALLOC, 72, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_realloc_1_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::REALLOC, 1, 100, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_realloc_1_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::REALLOC, 1, 4096, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_realloc_1_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::REALLOC, 1, 1000, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_realloc_1_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::REALLOC, 1, 1001, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_realloc_1_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::REALLOC, 1, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_realloc_10_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::REALLOC, 10, 100, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_realloc_10_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::REALLOC, 10, 4096, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_realloc_10_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::REALLOC, 10, 1000, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_realloc_10_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::REALLOC, 10, 1001, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_realloc_10_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::REALLOC, 10, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_realloc_72_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::REALLOC, 72, 100, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_realloc_72_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::REALLOC, 72, 4096, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_realloc_72_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::REALLOC, 72, 1000, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_realloc_72_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::REALLOC, 72, 1001, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_realloc_72_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW, FunctionCalls::REALLOC, 72, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_malloc_1_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::MALLOC, 1, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_malloc_1_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::MALLOC, 1, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_malloc_1_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::MALLOC, 1, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_malloc_1_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::MALLOC, 1, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_malloc_1_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::MALLOC, 1, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_malloc_10_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::MALLOC, 10, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_malloc_10_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::MALLOC, 10, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_malloc_10_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::MALLOC, 10, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_malloc_10_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::MALLOC, 10, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_malloc_10_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::MALLOC, 10, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_malloc_72_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::MALLOC, 72, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_malloc_72_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::MALLOC, 72, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_malloc_72_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::MALLOC, 72, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_malloc_72_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::MALLOC, 72, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_malloc_72_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::MALLOC, 72, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_calloc_1_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::CALLOC, 1, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_calloc_1_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::CALLOC, 1, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_calloc_1_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::CALLOC, 1, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_calloc_1_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::CALLOC, 1, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_calloc_1_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::CALLOC, 1, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_calloc_10_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::CALLOC, 10, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_calloc_10_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::CALLOC, 10, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_calloc_10_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::CALLOC, 10, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_calloc_10_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::CALLOC, 10, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_calloc_10_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::CALLOC, 10, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_calloc_72_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::CALLOC, 72, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_calloc_72_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::CALLOC, 72, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_calloc_72_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::CALLOC, 72, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_calloc_72_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::CALLOC, 72, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_calloc_72_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::CALLOC, 72, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_realloc_1_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::REALLOC, 1, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_realloc_1_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::REALLOC, 1, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_realloc_1_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::REALLOC, 1, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_realloc_1_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::REALLOC, 1, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_realloc_1_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::REALLOC, 1, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_realloc_10_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::REALLOC, 10, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_realloc_10_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::REALLOC, 10, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_realloc_10_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::REALLOC, 10, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_realloc_10_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::REALLOC, 10, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_realloc_10_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::REALLOC, 10,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_realloc_72_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::REALLOC, 72, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_realloc_72_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::REALLOC, 72, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_realloc_72_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::REALLOC, 72, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_realloc_72_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::REALLOC, 72, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_INTERLEAVE_realloc_72_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_INTERLEAVE, FunctionCalls::REALLOC, 72,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_malloc_1_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::MALLOC, 1, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_malloc_1_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::MALLOC, 1, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_malloc_1_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::MALLOC, 1, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_malloc_1_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::MALLOC, 1, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_malloc_1_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::MALLOC, 1,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_malloc_10_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::MALLOC, 10, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_malloc_10_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::MALLOC, 10,\n             4096, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_malloc_10_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::MALLOC, 10,\n             1000, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_malloc_10_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::MALLOC, 10,\n             1001, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_malloc_10_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::MALLOC, 10,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_malloc_72_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::MALLOC, 72, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_malloc_72_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::MALLOC, 72,\n             4096, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_malloc_72_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::MALLOC, 72,\n             1000, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_malloc_72_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::MALLOC, 72,\n             1001, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_malloc_72_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::MALLOC, 72,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_calloc_1_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::CALLOC, 1, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_calloc_1_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::CALLOC, 1, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_calloc_1_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::CALLOC, 1, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_calloc_1_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::CALLOC, 1, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_calloc_1_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::CALLOC, 1,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_calloc_10_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::CALLOC, 10, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_calloc_10_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::CALLOC, 10,\n             4096, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_calloc_10_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::CALLOC, 10,\n             1000, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_calloc_10_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::CALLOC, 10,\n             1001, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_calloc_10_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::CALLOC, 10,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_calloc_72_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::CALLOC, 72, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_calloc_72_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::CALLOC, 72,\n             4096, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_calloc_72_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::CALLOC, 72,\n             1000, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_calloc_72_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::CALLOC, 72,\n             1001, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_calloc_72_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::CALLOC, 72,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_realloc_1_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::REALLOC, 1, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_realloc_1_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::REALLOC, 1,\n             4096, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_realloc_1_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::REALLOC, 1,\n             1000, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_realloc_1_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::REALLOC, 1,\n             1001, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_realloc_1_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::REALLOC, 1,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_realloc_10_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::REALLOC, 10,\n             100, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_realloc_10_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::REALLOC, 10,\n             4096, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_realloc_10_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::REALLOC, 10,\n             1000, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_realloc_10_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::REALLOC, 10,\n             1001, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_realloc_10_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::REALLOC, 10,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_realloc_72_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::REALLOC, 72,\n             100, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_realloc_72_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::REALLOC, 72,\n             4096, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_realloc_72_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::REALLOC, 72,\n             1000, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_realloc_72_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::REALLOC, 72,\n             1001, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_INTERLEAVE_realloc_72_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_INTERLEAVE, FunctionCalls::REALLOC, 72,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_malloc_1_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::MALLOC, 1, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_malloc_1_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::MALLOC, 1, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_malloc_1_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::MALLOC, 1, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_malloc_1_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::MALLOC, 1, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_malloc_1_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::MALLOC, 1,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_malloc_10_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::MALLOC, 10, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_malloc_10_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::MALLOC, 10, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_malloc_10_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::MALLOC, 10, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_malloc_10_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::MALLOC, 10, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_malloc_10_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::MALLOC, 10,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_malloc_72_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::MALLOC, 72, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_malloc_72_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::MALLOC, 72, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_malloc_72_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::MALLOC, 72, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_malloc_72_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::MALLOC, 72, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_malloc_72_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::MALLOC, 72,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_calloc_1_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::CALLOC, 1, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_calloc_1_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::CALLOC, 1, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_calloc_1_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::CALLOC, 1, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_calloc_1_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::CALLOC, 1, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_calloc_1_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::CALLOC, 1,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_calloc_10_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::CALLOC, 10, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_calloc_10_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::CALLOC, 10, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_calloc_10_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::CALLOC, 10, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_calloc_10_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::CALLOC, 10, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_calloc_10_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::CALLOC, 10,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_calloc_72_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::CALLOC, 72, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_calloc_72_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::CALLOC, 72, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_calloc_72_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::CALLOC, 72, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_calloc_72_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::CALLOC, 72, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_calloc_72_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::CALLOC, 72,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_realloc_1_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::REALLOC, 1, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_realloc_1_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::REALLOC, 1, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_realloc_1_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::REALLOC, 1, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_realloc_1_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::REALLOC, 1, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_realloc_1_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::REALLOC, 1,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_realloc_10_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::REALLOC, 10, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_realloc_10_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::REALLOC, 10,\n             4096, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_realloc_10_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::REALLOC, 10,\n             1000, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_realloc_10_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::REALLOC, 10,\n             1001, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_realloc_10_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::REALLOC, 10,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_realloc_72_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::REALLOC, 72, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_realloc_72_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::REALLOC, 72,\n             4096, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_realloc_72_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::REALLOC, 72,\n             1000, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_realloc_72_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::REALLOC, 72,\n             1001, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_HBW_PREFERRED_realloc_72_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_HBW_PREFERRED, FunctionCalls::REALLOC, 72,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_malloc_1_thread_100_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::MALLOC, 1, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_malloc_1_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::MALLOC, 1, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_malloc_1_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::MALLOC, 1, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_malloc_1_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::MALLOC, 1, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_malloc_1_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::MALLOC, 1, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_malloc_10_thread_100_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::MALLOC, 10, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_malloc_10_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::MALLOC, 10, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_malloc_10_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::MALLOC, 10, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_malloc_10_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::MALLOC, 10, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_malloc_10_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::MALLOC, 10,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_malloc_72_thread_100_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::MALLOC, 72, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_malloc_72_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::MALLOC, 72, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_malloc_72_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::MALLOC, 72, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_malloc_72_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::MALLOC, 72, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_malloc_72_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::MALLOC, 72,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_calloc_1_thread_100_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::CALLOC, 1, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_calloc_1_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::CALLOC, 1, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_calloc_1_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::CALLOC, 1, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_calloc_1_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::CALLOC, 1, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_calloc_1_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::CALLOC, 1, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_calloc_10_thread_100_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::CALLOC, 10, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_calloc_10_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::CALLOC, 10, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_calloc_10_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::CALLOC, 10, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_calloc_10_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::CALLOC, 10, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_calloc_10_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::CALLOC, 10,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_calloc_72_thread_100_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::CALLOC, 72, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_calloc_72_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::CALLOC, 72, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_calloc_72_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::CALLOC, 72, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_calloc_72_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::CALLOC, 72, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_calloc_72_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::CALLOC, 72,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_realloc_1_thread_100_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::REALLOC, 1, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_realloc_1_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::REALLOC, 1, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_realloc_1_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::REALLOC, 1, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_realloc_1_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::REALLOC, 1, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_realloc_1_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::REALLOC, 1,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_realloc_10_thread_100_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::REALLOC, 10, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_realloc_10_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::REALLOC, 10, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_realloc_10_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::REALLOC, 10, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_realloc_10_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::REALLOC, 10, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_realloc_10_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::REALLOC, 10,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_realloc_72_thread_100_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::REALLOC, 72, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_realloc_72_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::REALLOC, 72, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_realloc_72_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::REALLOC, 72, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_realloc_72_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::REALLOC, 72, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_HBWMALLOC_ALLOCATOR_realloc_72_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::REALLOC, 72,\n             1572864, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_malloc_1_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::MALLOC, 1, 100, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_malloc_1_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::MALLOC, 1, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_malloc_1_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::MALLOC, 1, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_malloc_1_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::MALLOC, 1, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_malloc_1_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::MALLOC, 1, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_malloc_10_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::MALLOC, 10, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_malloc_10_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::MALLOC, 10, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_malloc_10_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::MALLOC, 10, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_malloc_10_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::MALLOC, 10, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_malloc_10_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::MALLOC, 10, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_malloc_72_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::MALLOC, 72, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_malloc_72_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::MALLOC, 72, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_malloc_72_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::MALLOC, 72, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_malloc_72_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::MALLOC, 72, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_malloc_72_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::MALLOC, 72, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_calloc_1_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::CALLOC, 1, 100, 10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_calloc_1_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::CALLOC, 1, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_calloc_1_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::CALLOC, 1, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_calloc_1_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::CALLOC, 1, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_calloc_1_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::CALLOC, 1, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_calloc_10_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::CALLOC, 10, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_calloc_10_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::CALLOC, 10, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_calloc_10_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::CALLOC, 10, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_calloc_10_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::CALLOC, 10, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_calloc_10_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::CALLOC, 10, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_calloc_72_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::CALLOC, 72, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_calloc_72_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::CALLOC, 72, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_calloc_72_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::CALLOC, 72, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_calloc_72_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::CALLOC, 72, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_calloc_72_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::CALLOC, 72, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_realloc_1_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::REALLOC, 1, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_realloc_1_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::REALLOC, 1, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_realloc_1_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::REALLOC, 1, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_realloc_1_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::REALLOC, 1, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_realloc_1_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::REALLOC, 1, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_realloc_10_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::REALLOC, 10, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_realloc_10_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::REALLOC, 10, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_realloc_10_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::REALLOC, 10, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_realloc_10_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::REALLOC, 10, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_realloc_10_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::REALLOC, 10, 1572864,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_realloc_72_thread_100_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::REALLOC, 72, 100,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_realloc_72_thread_4096_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::REALLOC, 72, 4096,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_realloc_72_thread_1000_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::REALLOC, 72, 1000,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_realloc_72_thread_1001_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::REALLOC, 72, 1001,\n             10000);\n}\n\nTEST_F(AllocPerformanceTest,\n       test_TC_MEMKIND_MEMKIND_PMEM_realloc_72_thread_1572864_bytes)\n{\n    run_test(AllocatorTypes::MEMKIND_PMEM, FunctionCalls::REALLOC, 72, 1572864,\n             10000);\n}\n"
  },
  {
    "path": "deps/memkind/src/test/allocate_to_max_stress_test.cpp",
    "content": "/*\n * Copyright (C) 2015 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <chrono>\n#include \"common.h\"\n#include \"allocator_perf_tool/Configuration.hpp\"\n#include \"allocator_perf_tool/StressIncreaseToMax.h\"\n#include \"allocator_perf_tool/HugePageOrganizer.hpp\"\n\n//memkind stress and longevity tests using Allocatr Perf Tool.\nclass AllocateToMaxStressTests: public :: testing::Test\n{\n\nprotected:\n    void SetUp()\n    {}\n\n    void TearDown()\n    {}\n\n    //Allocates memory up to 'memory_request_limit'.\n    void run(TypesConf kinds, TypesConf func_calls, unsigned operations,\n             size_t size_from, size_t size_to, size_t memory_request_limit,\n             bool touch_memory)\n    {\n        RecordProperty(\"memory_operations\", operations);\n        RecordProperty(\"size_from\", size_from);\n        RecordProperty(\"size_to\", size_to);\n\n        TaskConf task_conf = {\n            .n = operations, //number of memory operations\n            .allocation_sizes_conf = {\n                operations, //number of memory operations\n                size_from, //no random sizes.\n                size_to\n            },\n            .func_calls = func_calls, //enable allocator function call\n            .allocators_types = kinds, //enable allocator\n            .seed = 11, //random seed\n            .touch_memory = touch_memory //enable or disable touching memory\n        };\n\n        std::chrono::time_point<std::chrono::system_clock> start, end;\n        start = std::chrono::system_clock::now();\n\n        //Execute test iterations.\n        std::vector<iteration_result> results =\n            StressIncreaseToMax::execute_test_iterations(task_conf, 120,\n                                                         memory_request_limit);\n\n        end = std::chrono::system_clock::now();\n\n        std::chrono::duration<double> elapsed_time = end - start;\n\n        RecordProperty(\"elapsed_time\", elapsed_time.count());\n\n        //Check finish status.\n        EXPECT_EQ(check_allocation_errors(results, task_conf), 0);\n    }\n\n    //Check true allocation errors over all iterations.\n    //Return iteration number (>0) when error occurs, or zero\n    int check_allocation_errors(std::vector<iteration_result> &results,\n                                const TaskConf &task_conf)\n    {\n        for (size_t i=0; i<results.size(); i++) {\n            //Check if test ends with allocation error.\n            if(results[i].is_allocation_error) {\n                return i+1;\n            }\n        }\n\n        return 0;\n    }\n};\n\nTEST_F(AllocateToMaxStressTests,\n       test_TC_MEMKIND_slts_ALLOCATE_TO_MAX_MEMKIND_HBW)\n{\n    run(TypesConf(AllocatorTypes::MEMKIND_HBW), TypesConf(FunctionCalls::MALLOC),\n        1024, MB, MB, GB, true);\n}\n\nTEST_F(AllocateToMaxStressTests,\n       test_TC_MEMKIND_slts_ALLOCATE_TO_MAX_MEMKIND_INTERLEAVE)\n{\n    run(TypesConf(AllocatorTypes::MEMKIND_INTERLEAVE),\n        TypesConf(FunctionCalls::MALLOC), 4096, MB, MB, 4*GB, true);\n}\n\nTEST_F(AllocateToMaxStressTests,\n       test_TC_MEMKIND_slts_ALLOCATE_TO_MAX_MEMKIND_HBW_PREFERRED)\n{\n    run(TypesConf(AllocatorTypes::MEMKIND_HBW_PREFERRED),\n        TypesConf(FunctionCalls::MALLOC), 17408, MB, MB, 17*GB, true);\n}\n\nTEST_F(AllocateToMaxStressTests,\n       test_TC_MEMKIND_2MBPages_slts_ext_ALLOCATE_TO_MAX_MEMKIND_HBW_HUGETLB)\n{\n    HugePageOrganizer huge_page_organizer(2250);\n    run(TypesConf(AllocatorTypes::MEMKIND_HBW_HUGETLB),\n        TypesConf(FunctionCalls::MALLOC), 1024, 4*MB, 4*MB, GB, true);\n}\n\nTEST_F(AllocateToMaxStressTests,\n       test_TC_MEMKIND_slts_ALLOCATE_TO_MAX_DIFFERENT_SIZES)\n{\n    run(TypesConf(AllocatorTypes::MEMKIND_HBW), TypesConf(FunctionCalls::MALLOC),\n        2500, 1, 8*MB, GB, true);\n}\n\nTEST_F(AllocateToMaxStressTests,\n       test_TC_MEMKIND_slts_ALLOCATE_TO_MAX_AND_FREE_MEMKIND_DEFAULT)\n{\n    TypesConf func_calls;\n    func_calls.enable_type(FunctionCalls::MALLOC);\n    func_calls.enable_type(FunctionCalls::FREE);\n    run(TypesConf(AllocatorTypes::MEMKIND_DEFAULT), func_calls, 2500, 500*MB, 8*GB,\n        16*GB, false);\n}\n\nTEST_F(AllocateToMaxStressTests,\n       test_TC_MEMKIND_slts_ALLOCATE_TO_MAX_AND_FREE_MEMKIND_REGULAR)\n{\n    TypesConf func_calls;\n    func_calls.enable_type(FunctionCalls::MALLOC);\n    func_calls.enable_type(FunctionCalls::FREE);\n    run(TypesConf(AllocatorTypes::MEMKIND_REGULAR), func_calls, 2500, 500*MB, 8*GB,\n        16*GB, false);\n}\n\nTEST_F(AllocateToMaxStressTests,\n       test_TC_MEMKIND_slts_ALLOCATE_TO_MAX_DIFFERENT_KINDS)\n{\n    TypesConf kinds;\n    kinds.enable_type(AllocatorTypes::MEMKIND_HBW);\n    kinds.enable_type(AllocatorTypes::MEMKIND_HBW_PREFERRED);\n    kinds.enable_type(AllocatorTypes::MEMKIND_DEFAULT);\n    kinds.enable_type(AllocatorTypes::MEMKIND_INTERLEAVE);\n    kinds.enable_type(AllocatorTypes::MEMKIND_HBW_INTERLEAVE);\n    kinds.enable_type(AllocatorTypes::MEMKIND_REGULAR);\n    run(kinds, TypesConf(FunctionCalls::MALLOC), 2048, MB, MB, 2*GB, true);\n}\n\nTEST_F(AllocateToMaxStressTests,\n       test_TC_MEMKIND_slts_ext_ALLOCATE_TO_MAX_DIFFERENT_KINDS_WITH_HUGETLB)\n{\n    HugePageOrganizer huge_page_organizer(2250);\n    TypesConf kinds;\n    kinds.enable_type(AllocatorTypes::MEMKIND_HBW);\n    kinds.enable_type(AllocatorTypes::MEMKIND_HBW_HUGETLB);\n    kinds.enable_type(AllocatorTypes::MEMKIND_HBW_PREFERRED_HUGETLB);\n    run(kinds, TypesConf(FunctionCalls::MALLOC), 2048, MB, MB, 2*GB, true);\n}\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/AllocationSizes.hpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n\n#pragma once\n\n#include <vector>\n#include <cstdlib>\n\n#include \"VectorIterator.hpp\"\n\nclass AllocationSizes\n{\npublic:\n\n    static VectorIterator<size_t> generate_random_sizes(int sizes_num, size_t from,\n                                                        size_t to, int seed)\n    {\n        srand(seed);\n        std::vector<size_t> sizes;\n        size_t range = to - from;\n\n        if(from == to) {\n            for (int i=0; i<sizes_num; i++)\n                sizes.push_back(from);\n        } else {\n            for (int i=0; i<sizes_num; i++)\n                sizes.push_back(rand() % (range-1)+from);\n        }\n\n        return VectorIterator<size_t>::create(sizes);\n    }\n\n    static VectorIterator<size_t> generate_random_sizes(AllocationSizesConf conf,\n                                                        int seed)\n    {\n        return generate_random_sizes(conf.n, conf.size_from, conf.size_to, seed);\n    }\n\n};\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/Allocation_info.cpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n\n#include \"Allocation_info.hpp\"\n#include <numaif.h>\n\ndouble convert_bytes_to_mb(uint64_t bytes)\n{\n    return bytes / (1024.0 * 1024.0);\n}\n\nint get_numa_node_id(void *ptr)\n{\n    int status = -1;\n\n    get_mempolicy(&status, NULL, 0, ptr, MPOL_F_NODE | MPOL_F_ADDR);\n    return status;\n}\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/Allocation_info.hpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n\n#include <stdint.h>\n#include <stddef.h>\n\n\n\n//This structure is responsible to store information about single memory operation.\nstruct memory_operation {\n    void *ptr;\n    double total_time;\n    size_t size_of_allocation;\n    unsigned allocator_type;\n    unsigned allocation_method;\n    bool is_allocated;\n    int error_code;\n};\n\ndouble convert_bytes_to_mb(uint64_t bytes);\n\nint get_numa_node_id(void *ptr);\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/Allocator.hpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n\n#include <stdio.h>\n#include \"Allocation_info.hpp\"\n\nclass Allocator\n{\npublic:\n\n    virtual memory_operation wrapped_malloc(size_t size) = 0;\n    virtual memory_operation wrapped_calloc(size_t num, size_t size) = 0;\n    virtual memory_operation wrapped_realloc(void *ptr, size_t size) = 0;\n    virtual void wrapped_free(void *ptr) = 0;\n    virtual unsigned type() = 0;\n\n    virtual ~Allocator(void) {}\n};\n\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/AllocatorFactory.hpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n\n#include \"memkind.h\"\n\n#include \"Allocator.hpp\"\n#include \"StandardAllocatorWithTimer.hpp\"\n#include \"VectorIterator.hpp\"\n#include \"Configuration.hpp\"\n#include \"JemallocAllocatorWithTimer.hpp\"\n#include \"MemkindAllocatorWithTimer.hpp\"\n#include \"HBWmallocAllocatorWithTimer.hpp\"\n#include \"Numastat.hpp\"\n#include \"PmemMockup.hpp\"\n\n#include <vector>\n#include <assert.h>\n#include <string.h>\n#include <map>\n\n\nclass AllocatorFactory\n{\npublic:\n    //Allocator initialization statistics\n    struct initialization_stat {\n        float total_time; //Total time of initialization.\n        float ref_delta_time; //Delta Time.\n        unsigned allocator_type;\n        std::vector<float> memory_overhead; //Memory overhead per numa node.\n    };\n\n    AllocatorFactory()\n    {\n        memkind_allocators[AllocatorTypes::MEMKIND_DEFAULT] = MemkindAllocatorWithTimer(\n                                                                  MEMKIND_DEFAULT, AllocatorTypes::MEMKIND_DEFAULT);\n        memkind_allocators[AllocatorTypes::MEMKIND_REGULAR] = MemkindAllocatorWithTimer(\n                                                                  MEMKIND_REGULAR, AllocatorTypes::MEMKIND_REGULAR);\n        memkind_allocators[AllocatorTypes::MEMKIND_HBW] = MemkindAllocatorWithTimer(\n                                                              MEMKIND_HBW, AllocatorTypes::MEMKIND_HBW);\n        memkind_allocators[AllocatorTypes::MEMKIND_INTERLEAVE] =\n            MemkindAllocatorWithTimer(MEMKIND_INTERLEAVE,\n                                      AllocatorTypes::MEMKIND_INTERLEAVE);\n        memkind_allocators[AllocatorTypes::MEMKIND_HBW_INTERLEAVE] =\n            MemkindAllocatorWithTimer(MEMKIND_HBW_INTERLEAVE,\n                                      AllocatorTypes::MEMKIND_HBW_INTERLEAVE);\n        memkind_allocators[AllocatorTypes::MEMKIND_HBW_PREFERRED] =\n            MemkindAllocatorWithTimer(MEMKIND_HBW_PREFERRED,\n                                      AllocatorTypes::MEMKIND_HBW_PREFERRED);\n        memkind_allocators[AllocatorTypes::MEMKIND_HUGETLB] = MemkindAllocatorWithTimer(\n                                                                  MEMKIND_HUGETLB, AllocatorTypes::MEMKIND_HUGETLB);\n        memkind_allocators[AllocatorTypes::MEMKIND_GBTLB] = MemkindAllocatorWithTimer(\n                                                                MEMKIND_GBTLB, AllocatorTypes::MEMKIND_GBTLB);\n        memkind_allocators[AllocatorTypes::MEMKIND_HBW_HUGETLB] =\n            MemkindAllocatorWithTimer(MEMKIND_HBW_HUGETLB,\n                                      AllocatorTypes::MEMKIND_HBW_HUGETLB);\n        memkind_allocators[AllocatorTypes::MEMKIND_HBW_PREFERRED_HUGETLB] =\n            MemkindAllocatorWithTimer(MEMKIND_HBW_PREFERRED_HUGETLB,\n                                      AllocatorTypes::MEMKIND_HBW_PREFERRED_HUGETLB);\n        memkind_allocators[AllocatorTypes::MEMKIND_HBW_GBTLB] =\n            MemkindAllocatorWithTimer(MEMKIND_HBW_GBTLB, AllocatorTypes::MEMKIND_HBW_GBTLB);\n        memkind_allocators[AllocatorTypes::MEMKIND_HBW_PREFERRED_GBTLB] =\n            MemkindAllocatorWithTimer(MEMKIND_HBW_PREFERRED_GBTLB,\n                                      AllocatorTypes::MEMKIND_HBW_PREFERRED_GBTLB);\n        memkind_allocators[AllocatorTypes::MEMKIND_PMEM] = MemkindAllocatorWithTimer(\n                                                               MEMKIND_PMEM_MOCKUP, AllocatorTypes::MEMKIND_PMEM);\n    }\n\n    //Get existing allocator without creating new.\n    //The owner of existing allocator is AllocatorFactory object.\n    Allocator *get_existing(unsigned type)\n    {\n        switch(type) {\n            case AllocatorTypes::STANDARD_ALLOCATOR:\n                return &standard_allocator;\n\n            case AllocatorTypes::JEMALLOC:\n                return &jemalloc;\n\n            case AllocatorTypes::HBWMALLOC_ALLOCATOR:\n                return &hbwmalloc_allocator;\n\n            default: {\n                if(memkind_allocators.count(type))\n                    return &memkind_allocators[type];\n\n                assert(!\"'type' out of range!\");\n            }\n        }\n    }\n\n    initialization_stat initialize_allocator(Allocator &allocator)\n    {\n        size_t initial_size = 512;\n        float before_node1 = Numastat::get_total_memory(0);\n        float before_node2 = Numastat::get_total_memory(1);\n        initialization_stat stat = {0};\n\n        //malloc\n        memory_operation malloc_data = allocator.wrapped_malloc(initial_size);\n        stat.total_time += malloc_data.total_time;\n\n        //realloc\n        memory_operation realloc_data = allocator.wrapped_realloc(malloc_data.ptr, 256);\n        allocator.wrapped_free(realloc_data.ptr);\n\n        stat.total_time += realloc_data.total_time;\n\n        //calloc\n        memory_operation calloc_data = allocator.wrapped_calloc(initial_size, 1);\n        allocator.wrapped_free(calloc_data.ptr);\n\n        stat.total_time += calloc_data.total_time;\n\n        stat.allocator_type = allocator.type();\n\n        //calc memory overhead\n        stat.memory_overhead.push_back(Numastat::get_total_memory(0) - before_node1);\n        stat.memory_overhead.push_back(Numastat::get_total_memory(1) - before_node2);\n\n        return stat;\n    }\n\n    initialization_stat initialize_allocator(unsigned type)\n    {\n        return initialize_allocator(*get_existing(type));\n    }\n\n    //Calc percent delta between reference value and current value.\n    float calc_ref_delta(float ref_value, float value)\n    {\n        return ((value / ref_value)  - 1.0) * 100.0;\n    }\n\n    //Test initialization performance over available allocators.\n    //Return statistics.\n    std::vector<initialization_stat> initialization_test()\n    {\n        std::vector<initialization_stat> stats;\n        initialization_stat stat;\n\n        stat = initialize_allocator(standard_allocator);\n        float ref_time = stat.total_time;\n        stats.push_back(stat);\n\n        //Loop over available allocators to call initializer and compute stats.\n        for (unsigned i=1; i<AllocatorTypes::NUM_OF_ALLOCATOR_TYPES; i++) {\n            stat = initialize_allocator(*get_existing(i));\n            stat.ref_delta_time = calc_ref_delta(ref_time, stat.total_time);\n            stats.push_back(stat);\n        }\n\n        return stats;\n    }\n\n    VectorIterator<Allocator *> generate_random_allocator_calls(int num, int seed,\n                                                                TypesConf allocator_calls)\n    {\n        srand(seed);\n        std::vector<Allocator *> allocators_calls;\n\n        for (int i=0; i<num; i++) {\n            int index;\n\n            do {\n                index = (rand() % (AllocatorTypes::NUM_OF_ALLOCATOR_TYPES));\n            } while(!allocator_calls.is_enabled(index));\n\n            allocators_calls.push_back(get_existing(index));\n        }\n\n        return VectorIterator<Allocator *>::create(allocators_calls);\n    }\n\n    //Return kind to the corresponding AllocatorTypes enum specified in argument.\n    memkind_t get_kind_by_type(unsigned type)\n    {\n        if(memkind_allocators.count(type))\n            return memkind_allocators[type].get_kind();\n\n        assert(!\"'type' out of range!\");\n    }\n\nprivate:\n    StandardAllocatorWithTimer standard_allocator;\n    JemallocAllocatorWithTimer jemalloc;\n    HBWmallocAllocatorWithTimer hbwmalloc_allocator;\n\n    std::map<unsigned, MemkindAllocatorWithTimer> memkind_allocators;\n};\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/CSVLogger.hpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n\n#include <iostream>\n#include <string>\n#include <sstream>\n#include <fstream>\n#include <ios>\n\nnamespace csv\n{\n\n    class Row\n    {\n    public:\n        Row()\n        {\n            row << std::fixed;\n            row.precision(6);\n        }\n\n        template<class T>\n        void append(const T &e)\n        {\n            row << \",\" << e;\n        }\n\n        std::string export_row() const\n        {\n            std::stringstream ss(row.str());\n            ss << std::endl;\n            return ss.str();\n        }\n\n    private:\n        std::stringstream row;\n    };\n\n}\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/CommandLine.hpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n\n#include <map>\n#include <vector>\n#include <string>\n\nclass CommandLine\n{\npublic:\n\n    //Parse and write to val when option exist and strtol(...) > 0, otherwise val is not changed.\n    //T should be an integer type.\n    template<class T>\n    void parse_with_strtol(const std::string &option, T &val)\n    {\n        if(args.count(option)) {\n            T tmp = static_cast<T>(strtol(args[option].c_str(), NULL, 10));\n            if(tmp > 0)\n                val = tmp;\n            else\n                printf(\"Warning! Option '%s' may not be set.\\n\", option.c_str());\n        } else {\n            //Do not modify val.\n            printf(\"Warning! Option '%s' is not present.\\n\", option.c_str());\n        }\n    }\n\n    bool is_option_present(const std::string &option) const\n    {\n        return args.count(option);\n    }\n\n    bool is_option_set(const std::string option, std::string val)\n    {\n        if(is_option_present(option))\n            return (args[option] == val);\n        return false;\n    }\n\n    const std::string &get_option_value(const std::string &option)\n    {\n        return args[option];\n    }\n\n    CommandLine(int argc, char *argv[])\n    {\n        for (int i=0; i<argc; i++) {\n            std::string arg(argv[i]);\n            size_t found = arg.find(\"=\");\n\n            if(found != std::string::npos) {\n                std::string option = arg.substr(0, found);\n                std::string val = arg.substr(found+1, arg.length());\n\n                args[option] = val;\n            }\n        }\n    }\n\nprivate:\n    std::map<std::string, std::string> args;\n};\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/Configuration.hpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n\n#include <string>\n#include <map>\n#include <assert.h>\n\n//AllocatorTypes class represent allocator types and names related to this types.\nclass AllocatorTypes\n{\npublic:\n    enum {\n        STANDARD_ALLOCATOR,\n        JEMALLOC,\n        MEMKIND_DEFAULT,\n        MEMKIND_REGULAR,\n        MEMKIND_HBW,\n        MEMKIND_INTERLEAVE,\n        MEMKIND_HBW_INTERLEAVE,\n        MEMKIND_HBW_PREFERRED,\n        MEMKIND_HUGETLB,\n        MEMKIND_GBTLB,\n        MEMKIND_HBW_HUGETLB,\n        MEMKIND_HBW_PREFERRED_HUGETLB,\n        MEMKIND_HBW_GBTLB,\n        MEMKIND_HBW_PREFERRED_GBTLB,\n        HBWMALLOC_ALLOCATOR,\n        MEMKIND_PMEM,\n        NUM_OF_ALLOCATOR_TYPES\n    };\n\n    static const std::string &allocator_name(unsigned type)\n    {\n        static const std::string names[] = {\n            \"STANDARD_ALLOCATOR\",\n            \"JEMALLOC\",\n            \"MEMKIND_DEFAULT\",\n            \"MEMKIND_REGULAR\",\n            \"MEMKIND_HBW\",\n            \"MEMKIND_INTERLEAVE\",\n            \"MEMKIND_HBW_INTERLEAVE\",\n            \"MEMKIND_HBW_PREFERRED\",\n            \"MEMKIND_HUGETLB\",\n            \"MEMKIND_GBTLB\",\n            \"MEMKIND_HBW_HUGETLB\",\n            \"MEMKIND_HBW_PREFERRED_HUGETLB\",\n            \"MEMKIND_HBW_GBTLB\",\n            \"MEMKIND_HBW_PREFERRED_GBTLB\",\n            \"HBWMALLOC_ALLOCATOR\",\n            \"MEMKIND_PMEM\"\n        };\n\n        if(type >= NUM_OF_ALLOCATOR_TYPES) assert(!\"Invalid input argument!\");\n\n        return names[type];\n    }\n\n    static unsigned allocator_type(const std::string &name)\n    {\n        for (unsigned i=0; i<NUM_OF_ALLOCATOR_TYPES; i++) {\n            if(allocator_name(i) == name)\n                return i;\n        }\n\n        assert(!\"Invalid input argument!\");\n    }\n\n    static bool is_valid_memkind(unsigned type)\n    {\n        return (type >= MEMKIND_DEFAULT) && (type < NUM_OF_ALLOCATOR_TYPES);\n    }\n};\n\n//Enable or disable enum values (types).\nclass TypesConf\n{\npublic:\n    TypesConf() {}\n\n    TypesConf(unsigned type)\n    {\n        enable_type(type);\n    }\n\n    void enable_type(unsigned type)\n    {\n        types[type] = true;\n    }\n\n    void disable_type(unsigned type)\n    {\n        if(types.count(type))\n            types[type] = false;\n    }\n\n    bool is_enabled(unsigned type) const\n    {\n        return (types.count(type) ? types.find(type)->second : false);\n    }\n\nprivate:\n    std::map<unsigned, bool> types;\n};\n\n//AllocationSizesConf class represents allocation sizes configuration.\n//This data is needed to generate \"n\" sizes in range from \"size_from\" to \"size_to\".\nclass AllocationSizesConf\n{\npublic:\n    unsigned n;\n    size_t size_from;\n    size_t size_to;\n};\n\n//TaskConf class contain configuration data for task,\n//where:\n// - \"n\" - number of iterations,\n// - \"allocation_sizes_conf\" - allocation sizes configuration,\n// - \"func_calls\" - enabled or disabled function calls,\n// - \"allocators_types\" - enable allocators,\n// - \"seed\" - random seed.\n// - \"touch_memory\" - enable or disable touching memory\nclass TaskConf\n{\npublic:\n    unsigned n;\n    AllocationSizesConf allocation_sizes_conf;\n    TypesConf func_calls;\n    TypesConf allocators_types;\n    unsigned seed;\n    bool touch_memory;\n    bool is_csv_log_enabled;\n};\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/ConsoleLog.hpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n#include \"Configuration.hpp\"\n#include \"Stats.hpp\"\n#include \"FunctionCalls.hpp\"\n\n#include <stdio.h>\n\nclass ConsoleLog\n{\npublic:\n    static void print_stats(TimeStats &stats, unsigned allocator_type,\n                            unsigned func_calls)\n    {\n        if(stats.stats.count(allocator_type)) {\n            if(stats.stats[allocator_type].count(func_calls)) {\n                MethodStats method_stats = stats.stats[allocator_type][func_calls];\n                printf(\" %20s (%u) | %7s | %10f.s | %10f.s  | %zu bytes/%f MB \\n\",\n                       AllocatorTypes::allocator_name(allocator_type).c_str(),\n                       allocator_type,\n                       FunctionCalls::function_name(func_calls).c_str(),\n                       method_stats.total_time,\n                       method_stats.average_time,\n                       method_stats.allocation_size,\n                       convert_bytes_to_mb(method_stats.allocation_size)\n                      );\n            }\n        }\n    }\n\n    static void print_table(TimeStats &stats)\n    {\n        printf(\"\\n====== Allocators function calls performance =================================================\\n\");\n        printf(\" %20s Id:   Method:    Total time:    Average time:  Allocated memory bytes/MB: \\n\",\n               \"Allocator:\");\n        for (unsigned i=0; i<=AllocatorTypes::MEMKIND_HBW_PREFERRED; i++) {\n            for (unsigned func_call=FunctionCalls::FREE+1;\n                 func_call<FunctionCalls::NUM_OF_FUNCTIONS; func_call++) {\n                print_stats(stats, i, func_call);\n            }\n        }\n        printf(\"==============================================================================================\\n\");\n    }\n\n    static void print_requested_memory(TimeStats &stats, std::string test_name)\n    {\n        printf(\"\\n====== Requested memory stats for %s =================\\n\",\n               test_name.c_str());\n        printf(\"Total requested allocations: %zu bytes/%f MB. \\n\",stats.get_allocated(),\n               convert_bytes_to_mb(stats.get_allocated()));\n        printf(\"Total requested deallocations: %zu bytes/%f MB. \\n\",\n               stats.get_deallocated(), convert_bytes_to_mb(stats.get_deallocated()));\n        printf(\"=====================================================================\\n\");\n    }\n};\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/FunctionCalls.hpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n\n#include \"VectorIterator.hpp\"\n#include \"Configuration.hpp\"\n#include <vector>\n#include <cstdlib>\n\nclass FunctionCalls\n{\npublic:\n    enum {FREE, MALLOC, CALLOC, REALLOC, NUM_OF_FUNCTIONS};\n\n    static const std::string function_name(unsigned type)\n    {\n        static std::string names[] = {\"free\", \"malloc\", \"calloc\", \"realloc\"};\n\n        if(type >= NUM_OF_FUNCTIONS) assert(!\"Invalidate input argument!\");\n\n        return names[type];\n    }\n\n    static unsigned function_type(const std::string &name)\n    {\n        for (unsigned i=0; i<NUM_OF_FUNCTIONS; i++) {\n            if(function_name(i) == name)\n                return i;\n        }\n\n        assert(!\"Invalid input argument!\");\n    }\n\n    static VectorIterator<int> generate_random_allocator_func_calls(int call_num,\n                                                                    int seed, TypesConf func_calls)\n    {\n        std::vector<unsigned> avail_types;\n\n        std::srand(seed);\n        std::vector<int> calls;\n\n        for (int i=0; i<call_num; i++) {\n            int index;\n            do {\n                index = rand() % (NUM_OF_FUNCTIONS);\n            } while(!func_calls.is_enabled(index));\n\n            calls.push_back(index);\n        }\n\n        return VectorIterator<int>::create(calls);\n    }\n\n};\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/FunctionCallsPerformanceTask.cpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#include \"FunctionCallsPerformanceTask.h\"\n\n\nvoid FunctionCallsPerformanceTask::run()\n{\n    VectorIterator<size_t> allocation_sizes =\n        AllocationSizes::generate_random_sizes(task_conf.allocation_sizes_conf,\n                                               task_conf.seed);\n\n    VectorIterator<int> func_calls =\n        FunctionCalls::generate_random_allocator_func_calls(task_conf.n, task_conf.seed,\n                                                            task_conf.func_calls);\n\n    AllocatorFactory allocator_types;\n    VectorIterator<Allocator *> allocators_calls =\n        allocator_types.generate_random_allocator_calls(task_conf.n, task_conf.seed,\n                                                        task_conf.allocators_types);\n\n    ScenarioWorkload scenario_workload = ScenarioWorkload(\n                                             &allocators_calls,\n                                             &allocation_sizes,\n                                             &func_calls\n                                         );\n\n    while (scenario_workload.run());\n\n    results = scenario_workload.get_allocations_info();\n}\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/FunctionCallsPerformanceTask.h",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n\n#include \"Task.hpp\"\n#include \"AllocatorFactory.hpp\"\n#include \"FunctionCalls.hpp\"\n#include \"AllocationSizes.hpp\"\n#include \"VectorIterator.hpp\"\n#include \"ScenarioWorkload.h\"\n#include \"Configuration.hpp\"\n\n\nclass FunctionCallsPerformanceTask :\n    public Task\n{\npublic:\n    FunctionCallsPerformanceTask(TaskConf conf)\n    {\n        task_conf = conf;\n    }\n\n    void run();\n\n    std::vector<memory_operation> get_results()\n    {\n        return results;\n    }\n\nprivate:\n    TaskConf task_conf;\n    std::vector<memory_operation> results;\n};\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/GTestAdapter.hpp",
    "content": "/*\n* Copyright (C) 2016 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n\n#include <iostream>\n#include <sstream>\n#include <gtest/gtest.h>\n\n/*\n * The GTestAdapter class is an adapter for GTest framework.\n * All methods from GTest framework that need to be adapted should be located here.\n */\n\nclass GTestAdapter\n{\npublic:\n    template<class T>\n    static void RecordProperty(const std::string &key, const T &value)\n    {\n        std::ostringstream tmp_value;\n        tmp_value << value;\n        testing::Test::RecordProperty(key, tmp_value.str());\n        std::ios::fmtflags flags(std::cout.flags());\n        std::cout << key << \": \" << std::fixed  <<  std::setprecision(\n                      5) << value << std::endl;\n        std::cout.flags(flags);\n    }\n};\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/HBWmallocAllocatorWithTimer.hpp",
    "content": "/*\n* Copyright (C) 2017 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n\n#pragma once\n\n#include <hbwmalloc.h>\n\n#include \"Allocator.hpp\"\n#include \"Allocation_info.hpp\"\n#include \"Configuration.hpp\"\n#include \"WrappersMacros.hpp\"\n#include \"FunctionCalls.hpp\"\n#include <cerrno>\n\n#include <stdlib.h>\n\n\nclass HBWmallocAllocatorWithTimer\n    : public Allocator\n{\npublic:\n    memory_operation wrapped_malloc(size_t size)\n    {\n        START_TEST(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::MALLOC)\n        data.ptr = hbw_malloc(size);\n        END_TEST\n    }\n\n    memory_operation wrapped_calloc(size_t num, size_t size)\n    {\n        START_TEST(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::CALLOC)\n        data.ptr = hbw_calloc(num, size);\n        END_TEST\n    }\n\n    memory_operation wrapped_realloc(void *ptr, size_t size)\n    {\n        START_TEST(AllocatorTypes::HBWMALLOC_ALLOCATOR, FunctionCalls::REALLOC)\n        data.ptr = hbw_realloc(ptr, size);\n        END_TEST\n    }\n\n    void wrapped_free(void *ptr)\n    {\n        hbw_free(ptr);\n    }\n\n    unsigned type()\n    {\n        return AllocatorTypes::HBWMALLOC_ALLOCATOR;\n    }\n\n};\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/HugePageOrganizer.hpp",
    "content": "\n/*\n* Copyright (C) 2016 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n\n#include <fstream>\n#include <string.h>\n#include <numa.h>\n#include <vector>\n#include <iostream>\n#include <stdexcept>\n\n/*\n * HugePageOrganizer sets hugepages per NUMA node and restore initial setup of hugepages.\n * It writes and reads from the same file, so using HugePageOrganizer with parallel execution may cause undefined behaviour.\n */\n\nclass HugePageOrganizer\n{\npublic:\n    HugePageOrganizer(int nr_hugepages_per_node)\n    {\n        for (int node_id = 0; node_id < numa_num_configured_nodes(); node_id++) {\n            initial_nr_hugepages_per_nodes.push_back(get_nr_hugepages(node_id));\n            if (set_nr_hugepages(nr_hugepages_per_node, node_id)) {\n                restore();\n                throw std::runtime_error(\"Error: Could not set the requested amount of huge pages.\");\n            }\n        }\n    }\n\n    ~HugePageOrganizer()\n    {\n        restore();\n    }\nprivate:\n    std::vector<int> initial_nr_hugepages_per_nodes;\n\n    int get_nr_hugepages(int node_number)\n    {\n        std::string line;\n        char path[128];\n        sprintf(path,\n                \"/sys/devices/system/node/node%d/hugepages/hugepages-2048kB/nr_hugepages\",\n                node_number);\n        std::ifstream file(path);\n        if (!file) {\n            return -1;\n        }\n        std::getline(file, line);\n        return strtol(line.c_str(), 0, 10);\n    }\n\n    int set_nr_hugepages(int nr_hugepages, int node_number)\n    {\n        char cmd[128];\n        sprintf(cmd, \"sudo sh -c \\\"echo %d > \\\n            /sys/devices/system/node/node%d/hugepages/hugepages-2048kB/nr_hugepages\\\"\",\n                nr_hugepages, node_number);\n        if (system(cmd) || (get_nr_hugepages(node_number) != nr_hugepages)) {\n            return -1;\n        }\n        return 0;\n    }\n\n    void restore()\n    {\n        for(size_t node_id = 0; node_id < initial_nr_hugepages_per_nodes.size();\n            node_id++) {\n            set_nr_hugepages(initial_nr_hugepages_per_nodes[node_id], node_id);\n        }\n    }\n};\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/HugePageUnmap.hpp",
    "content": "/*\n* Copyright (C) 2016 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n\n#include <vector>\n#include \"Task.hpp\"\n\nclass HugePageUnmap: public Task\n{\n\npublic:\n    HugePageUnmap(int operations, bool touch_memory, size_t alignment_size,\n                  size_t alloc_size, hbw_pagesize_t page_size) :\n        mem_operations_num(operations),\n        touch_memory(touch_memory),\n        alignment_size(alignment_size),\n        alloc_size(alloc_size),\n        page_size(page_size)\n    {}\n\n    ~HugePageUnmap()\n    {\n        for(int i=0; i<mem_operations_num; i++) {\n            hbw_free(results[i].ptr);\n        }\n    };\n\n    void run()\n    {\n        void *ptr = NULL;\n\n        for(int i=0; i<mem_operations_num; i++) {\n            int ret = hbw_posix_memalign_psize(&ptr, alignment_size, alloc_size, page_size);\n\n            ASSERT_EQ(ret, 0);\n            ASSERT_FALSE(ptr == NULL);\n\n            if(touch_memory) {\n                memset(ptr, alignment_size, alignment_size);\n            }\n\n            memory_operation data;\n            data.ptr = ptr;\n            results.push_back(data);\n        }\n    }\n\n    std::vector<memory_operation> get_results()\n    {\n        return results;\n    }\n\nprivate:\n    int mem_operations_num;\n    std::vector<memory_operation> results;\n\n    bool touch_memory;\n    size_t alignment_size;\n    size_t alloc_size;\n    hbw_pagesize_t page_size;\n};\n\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/Iterator.hpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n\ntemplate<class T>\nclass Iterator\n{\npublic:\n    virtual bool has_next() const = 0;\n    virtual T next() = 0;\n    virtual size_t size() const = 0;\n};\n\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/JemallocAllocatorWithTimer.hpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n\n#include \"Allocator.hpp\"\n#include \"Allocation_info.hpp\"\n#include \"Configuration.hpp\"\n#include \"WrappersMacros.hpp\"\n#include \"FunctionCalls.hpp\"\n\n#include <jemalloc/jemalloc.h>\n\nclass JemallocAllocatorWithTimer\n    : public Allocator\n{\npublic:\n\n    memory_operation wrapped_malloc(size_t size)\n    {\n        START_TEST(AllocatorTypes::JEMALLOC, FunctionCalls::MALLOC)\n        data.ptr = malloc(size);\n        END_TEST\n    }\n\n    memory_operation wrapped_calloc(size_t num, size_t size)\n    {\n        START_TEST(AllocatorTypes::JEMALLOC, FunctionCalls::CALLOC)\n        data.ptr = calloc(num, size);\n        END_TEST\n    }\n    memory_operation wrapped_realloc(void *ptr, size_t size)\n    {\n        START_TEST(AllocatorTypes::JEMALLOC, FunctionCalls::REALLOC)\n        data.ptr = realloc(ptr, size);\n        END_TEST\n    }\n\n    void wrapped_free(void *ptr)\n    {\n        free(ptr);\n    }\n\n    unsigned type()\n    {\n        return AllocatorTypes::JEMALLOC;\n    }\n};\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/Makefile",
    "content": "#\n#  Copyright (C) 2015 - 2018 Intel Corporation.\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#  1. Redistributions of source code must retain the above copyright notice(s),\n#     this list of conditions and the following disclaimer.\n#  2. Redistributions in binary form must reproduce the above copyright notice(s),\n#     this list of conditions and the following disclaimer in the documentation\n#     and/or other materials provided with the distribution.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n#  EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n#  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n#\n\nperf_tool: main.cpp ScenarioWorkload.cpp FunctionCallsPerformanceTask.cpp StressIncreaseToMax.cpp Allocation_info.cpp PmemMockup.cpp\n\tg++ -o perf_tool $^ -O0 -lmemkind -std=c++11 -lpthread -lnuma -g\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/MemkindAllocatorWithTimer.hpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n\n#pragma once\n\n#include <memkind.h>\n\n#include \"Allocator.hpp\"\n#include \"Allocation_info.hpp\"\n#include \"Configuration.hpp\"\n#include \"WrappersMacros.hpp\"\n#include \"FunctionCalls.hpp\"\n#include <cerrno>\n\n#include <stdlib.h>\n\n\nclass MemkindAllocatorWithTimer\n    : public Allocator\n{\npublic:\n    MemkindAllocatorWithTimer() : kind(&MEMKIND_DEFAULT) {}\n\n    MemkindAllocatorWithTimer(memkind_t &memory_kind, unsigned kind_type_id)\n    {\n        kind = &memory_kind;\n        type_id = kind_type_id;\n    }\n    ~MemkindAllocatorWithTimer(void) {}\n\n    memory_operation wrapped_malloc(size_t size)\n    {\n        START_TEST(type_id, FunctionCalls::MALLOC)\n        data.ptr = memkind_malloc(*kind, size);\n        END_TEST\n    }\n\n    memory_operation wrapped_calloc(size_t num, size_t size)\n    {\n        START_TEST(type_id, FunctionCalls::CALLOC)\n        data.ptr = memkind_calloc(*kind, num, size);\n        END_TEST\n    }\n\n    memory_operation wrapped_realloc(void *ptr, size_t size)\n    {\n        START_TEST(type_id, FunctionCalls::REALLOC)\n        data.ptr = memkind_realloc(*kind, ptr, size);\n        END_TEST\n    }\n\n    void wrapped_free(void *ptr)\n    {\n        memkind_free(*kind, ptr);\n    }\n\n    void change_kind(memkind_t &memory_kind, unsigned kind_type_id)\n    {\n        kind = &memory_kind;\n        type_id = kind_type_id;\n    }\n\n    unsigned type()\n    {\n        return type_id;\n    }\n    memkind_t get_kind()\n    {\n        return *kind;\n    }\n\nprivate:\n    memkind_t *kind;\n    unsigned type_id;\n\n};\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/Numastat.hpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n#include <assert.h>\n\n#include <sys/types.h>\n#include <unistd.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <iostream>\n#include <string>\n#include <sstream>\n\n\nclass Numastat\n{\npublic:\n    //Returns numastat memory usage per node.\n    static float get_total_memory(unsigned node)\n    {\n        static pid_t pid = getpid();\n\n        std::stringstream cmd;\n        cmd << \"numastat \" << pid;\n\n        FILE *file;\n        char buff[256];\n        float result = -1.0;\n\n        if((file = popen(cmd.str().c_str(), \"r\"))) {\n            while(fgets(buff, 256, file));\n            pclose(file);\n\n            //We got: \"Total                     1181.90            2.00         1183.90\".\n            //2.00 is our memory usage from Node 1.\n            std::string last_line(buff);\n\n            size_t dot_pos = 0;\n            //Now we search in: \"           2.00         1183.90\".\n            for(unsigned i=0; i<=node; i++) {\n                dot_pos = last_line.find(\".\", dot_pos+1);\n                assert(dot_pos != std::string::npos);\n            }\n\n            //We are at: \" 2.00         1183.90\".\n            size_t number_begin = last_line.rfind(\" \", dot_pos);\n            assert(number_begin != std::string::npos);\n\n            number_begin += 1;\n            buff[dot_pos+3] = '\\0';\n\n#if __cplusplus > 201100L\n            result = strtod(&buff[number_begin], NULL);\n#else\n            result = atof(&buff[number_begin]);\n#endif\n        }\n\n        assert(result > -0.01);\n\n        return result;\n    }\n\n};\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/PmemMockup.cpp",
    "content": "/*\n* Copyright (C) 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#include \"PmemMockup.hpp\"\n\nstruct memkind *MEMKIND_PMEM_MOCKUP;\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/PmemMockup.hpp",
    "content": "/*\n* Copyright (C) 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n#include \"memkind.h\"\n\n///\n/// \\brief Mockup structure for PMEM kind to use allocator_perf_tool engine\n///\nextern memkind_t MEMKIND_PMEM_MOCKUP;\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/Runnable.hpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n\nclass Runnable\n{\npublic:\n    virtual void run() = 0;\n    virtual ~Runnable() {};\n};\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/ScenarioWorkload.cpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#include \"ScenarioWorkload.h\"\n\nScenarioWorkload::ScenarioWorkload(VectorIterator<Allocator *> *a,\n                                   VectorIterator<size_t> *as,  VectorIterator<int> *fc)\n{\n    allocators = a;\n    func_calls = fc;\n    alloc_sizes = as;\n}\n\nbool ScenarioWorkload::run()\n{\n    if(func_calls->has_next() && allocators->has_next() &&\n       alloc_sizes->has_next()) {\n        switch(func_calls->next()) {\n            case FunctionCalls::MALLOC: {\n                memory_operation data = allocators->next()->wrapped_malloc(alloc_sizes->next());\n                post_allocation_check(data);\n                break;\n            }\n            case FunctionCalls::CALLOC: {\n                memory_operation data = allocators->next()->wrapped_calloc(1,\n                                                                           alloc_sizes->next());\n                post_allocation_check(data);\n                break;\n            }\n            case FunctionCalls::REALLOC: {\n                //Guarantee the memory for realloc.\n                Allocator *allocator = allocators->next();\n                memory_operation to_realloc = allocator->wrapped_malloc(512);\n\n                memory_operation data = allocator->wrapped_realloc(to_realloc.ptr,\n                                                                   alloc_sizes->next());\n                post_allocation_check(data);\n                break;\n            }\n            case FunctionCalls::FREE: {\n                memory_operation *data = get_allocated_memory();\n\n                if(!allocations.empty() && (data != NULL)) {\n                    allocator_factory.get_existing(data->allocator_type)->wrapped_free(data->ptr);\n                    data->is_allocated = false;\n\n                    memory_operation free_op = *data;\n                    free_op.allocation_method = FunctionCalls::FREE;\n                    allocations.push_back(free_op);\n                }\n\n                break;\n            }\n            default:\n                assert(!\"Function call identifier out of range.\");\n                break;\n        }\n\n        return true;\n    }\n\n    return false;\n}\n\nScenarioWorkload::~ScenarioWorkload(void)\n{\n    for (int i=0; i<allocations.size(); i++) {\n        memory_operation data = allocations[i];\n        if(data.is_allocated && (data.allocation_method != FunctionCalls::FREE))\n            allocator_factory.get_existing(data.allocator_type)->wrapped_free(data.ptr);\n    }\n}\n\nmemory_operation *ScenarioWorkload::get_allocated_memory()\n{\n    for (int i=allocations.size()-1; i>=0; i--) {\n        memory_operation *data = &allocations[i];\n        if(data->is_allocated)\n            return data;\n    }\n\n    return NULL;\n}\n\nvoid ScenarioWorkload::post_allocation_check(const memory_operation &data)\n{\n    allocations.push_back(data);\n    if(touch_memory_on_allocation && (data.ptr != NULL) &&\n       (data.error_code != ENOMEM)) {\n        //Write memory to ensure physical allocation.\n        memset(data.ptr, 1, data.size_of_allocation);\n    }\n}\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/ScenarioWorkload.h",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n#include \"Workload.hpp\"\n#include \"FunctionCalls.hpp\"\n#include \"Allocator.hpp\"\n#include \"AllocatorFactory.hpp\"\n\n#include <string.h>\n\nclass ScenarioWorkload :\n    public Workload\n{\npublic:\n    ScenarioWorkload(VectorIterator<Allocator *> *a, VectorIterator<size_t> *as,\n                     VectorIterator<int> *fc);\n    ~ScenarioWorkload(void);\n\n    double get_time_costs();\n\n    const std::vector<memory_operation> &get_allocations_info() const\n    {\n        return allocations;\n    }\n\n    bool run();\n\n    memory_operation *get_allocated_memory();\n\n    void enable_touch_memory_on_allocation(bool enable)\n    {\n        touch_memory_on_allocation = enable;\n    }\n\n    void post_allocation_check(const memory_operation &data);\n\nprivate:\n    AllocatorFactory allocator_factory;\n    std::vector<memory_operation> allocations;\n\n    bool touch_memory_on_allocation;\n\n    VectorIterator<int> *func_calls;\n    VectorIterator<size_t> *alloc_sizes;\n    VectorIterator<Allocator *> *allocators;\n};\n\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/StandardAllocatorWithTimer.hpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n\n#include \"Allocator.hpp\"\n#include \"Allocation_info.hpp\"\n#include \"Configuration.hpp\"\n#include \"WrappersMacros.hpp\"\n#include \"FunctionCalls.hpp\"\n\n#include <stdlib.h>\n\nclass StandardAllocatorWithTimer\n    : public Allocator\n{\npublic:\n\n    memory_operation wrapped_malloc(size_t size)\n    {\n        START_TEST(AllocatorTypes::STANDARD_ALLOCATOR, FunctionCalls::MALLOC)\n        data.ptr = malloc(size);\n        END_TEST\n    }\n\n    memory_operation wrapped_calloc(size_t num, size_t size)\n    {\n        START_TEST(AllocatorTypes::STANDARD_ALLOCATOR, FunctionCalls::CALLOC)\n        data.ptr = calloc(num, size);\n        END_TEST\n    }\n    memory_operation wrapped_realloc(void *ptr, size_t size)\n    {\n        START_TEST(AllocatorTypes::STANDARD_ALLOCATOR, FunctionCalls::REALLOC)\n        data.ptr = realloc(ptr, size);\n        END_TEST\n    }\n\n    void wrapped_free(void *ptr)\n    {\n        free(ptr);\n    }\n\n    unsigned type()\n    {\n        return AllocatorTypes::STANDARD_ALLOCATOR;\n    }\n};\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/Stats.hpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n\n#include <map>\n#include <vector>\n\n#include \"Allocation_info.hpp\"\n#include \"Configuration.hpp\"\n#include \"FunctionCalls.hpp\"\n\nclass MethodStats\n{\npublic:\n    MethodStats()\n    {\n        total_time = 0.0;\n        average_time = 0;\n        allocation_size = 0;\n        samples_num = 0;\n    }\n\n    double total_time;\n    double average_time;\n    unsigned samples_num;\n    size_t allocation_size;\n};\n\n\nclass TimeStats\n{\npublic:\n\n    TimeStats()\n    {\n        allocated = 0;\n        deallocated = 0;\n    }\n\n    std::map<unsigned, std::map<unsigned, MethodStats> > stats;\n\n    TimeStats &operator+=(const std::vector<memory_operation> &data)\n    {\n        for (size_t i=0; i<data.size(); i++) {\n            memory_operation tmp = data[i];\n            MethodStats &method_stats = stats[tmp.allocator_type][tmp.allocation_method];\n            method_stats.allocation_size += tmp.size_of_allocation;\n            method_stats.total_time += tmp.total_time;\n            method_stats.samples_num++;\n\n            //Update average.\n            double total_time = method_stats.total_time;\n            double samples_num = method_stats.samples_num;\n            double avg = total_time/samples_num;\n            method_stats.average_time = avg;\n\n            if(tmp.allocation_method != FunctionCalls::FREE) {\n                allocated += tmp.size_of_allocation;\n            } else {\n                deallocated += tmp.size_of_allocation;\n            }\n        }\n\n        return *this;\n    }\n\n    size_t get_allocated() const\n    {\n        return allocated;\n    }\n    size_t get_deallocated() const\n    {\n        return deallocated;\n    }\n\nprivate:\n    size_t allocated;\n    size_t deallocated;\n};\n\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/StressIncreaseToMax.cpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n\n#include \"StressIncreaseToMax.h\"\n\n\nvoid StressIncreaseToMax::run()\n{\n    //Generate constant allocation sizes.\n    VectorIterator<size_t> allocation_sizes =\n        AllocationSizes::generate_random_sizes(task_conf.allocation_sizes_conf,\n                                               task_conf.seed);\n    //Generate only mallocs.\n    VectorIterator<int> func_calls =\n        FunctionCalls::generate_random_allocator_func_calls(task_conf.n, task_conf.seed,\n                                                            task_conf.func_calls);\n\n    unsigned type;\n    for (type = 0; type < AllocatorTypes::NUM_OF_ALLOCATOR_TYPES; type++) {\n        if(task_conf.allocators_types.is_enabled(type))\n            break; //Assume that there is only one type.\n    }\n\n\n    AllocatorFactory allocator_factory;\n    VectorIterator<Allocator *> allocators_calls =\n        allocator_factory.generate_random_allocator_calls(task_conf.n, task_conf.seed,\n                                                          task_conf.allocators_types);\n\n    ScenarioWorkload scenario_workload(\n        &allocators_calls,\n        &allocation_sizes,\n        &func_calls\n    );\n\n    scenario_workload.enable_touch_memory_on_allocation(task_conf.touch_memory);\n\n    test_status.is_allocation_error = false;\n\n    size_t requested_memory = 0;\n    bool has_reach_memory_request_limit = false;\n\n    while (!has_reach_memory_request_limit &&\n           !test_status.is_allocation_error &&\n           (test_status.has_next_memory_operation = scenario_workload.run())) {\n        memory_operation data = scenario_workload.get_allocations_info().back();\n        test_status.is_allocation_error = (data.error_code == ENOMEM) ||\n                                          (data.ptr == NULL);\n\n        if(data.allocation_method != FunctionCalls::FREE) {\n            requested_memory += data.size_of_allocation;\n            has_reach_memory_request_limit = requested_memory >= req_mem_limit;\n        }\n    }\n\n    if(!scenario_workload.get_allocations_info().size() < task_conf.n &&\n       !has_reach_memory_request_limit)\n        printf(\"\\nWARNING: Too few memory operations to reach the limit.\\n\");\n    if(test_status.is_allocation_error) printf(\"\\nWARNING: Allocation error. \\n\");\n\n    results = scenario_workload.get_allocations_info();\n}\n\nstd::vector<iteration_result> StressIncreaseToMax::execute_test_iterations(\n    const TaskConf &task_conf,\n    unsigned time,\n    size_t requested_memory_limit)\n{\n    TimerSysTime timer;\n    unsigned itr = 0;\n    std::vector<iteration_result> results;\n\n    std::ofstream csv_file;\n\n    csv::Row row;\n    row.append(\"Iteration\");\n    row.append(\"Allocated memory (MB)\");\n    row.append(\"Elapsed time (seconds)\");\n    if(task_conf.is_csv_log_enabled) {\n        csv_file.open(\"stress_test_increase_to_max.csv\");\n        csv_file << row.export_row();\n    }\n    printf(\"%s\", row.export_row().c_str());\n\n    timer.start();\n\n    while (timer.getElapsedTime() < time) {\n        StressIncreaseToMax stress_test(task_conf, requested_memory_limit);\n        stress_test.run();\n        float elapsed_time = timer.getElapsedTime();\n\n        TimeStats stats;\n        stats += stress_test.get_results();\n\n        results.push_back(stress_test.get_test_status());\n\n        //Log every interation of StressIncreaseToMax test.\n        csv::Row row;\n        row.append(itr);\n        row.append(convert_bytes_to_mb(stats.get_allocated()));\n        row.append(elapsed_time);\n        if(task_conf.is_csv_log_enabled) {\n            csv_file << row.export_row();\n        }\n        printf(\"%s\", row.export_row().c_str());\n        fflush(stdout);\n\n        itr++;\n    }\n\n    printf(\"\\nStress test (StressIncreaseToMax) finish in time %f.\\n\",\n           timer.getElapsedTime());\n\n    csv_file.close();\n\n    return results;\n}\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/StressIncreaseToMax.h",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n\n#include \"Configuration.hpp\"\n#include \"ScenarioWorkload.h\"\n#include \"FunctionCalls.hpp\"\n#include \"AllocationSizes.hpp\"\n#include \"VectorIterator.hpp\"\n#include \"AllocatorFactory.hpp\"\n#include \"Task.hpp\"\n#include \"Numastat.hpp\"\n#include \"CSVLogger.hpp\"\n#include \"TimerSysTime.hpp\"\n#include \"Stats.hpp\"\n\n#include <vector>\n\nstruct iteration_result {\n    bool has_next_memory_operation;\n    bool is_allocation_error;\n};\n\nclass StressIncreaseToMax\n    : public Task\n{\npublic:\n    StressIncreaseToMax(const TaskConf &conf, size_t requested_memory_limit)\n        : task_conf(conf),\n          req_mem_limit(requested_memory_limit)\n    {}\n\n    void run();\n\n    //Return memory operations from the last run.\n    std::vector<memory_operation> get_results()\n    {\n        return results;\n    }\n    iteration_result get_test_status()\n    {\n        return test_status;\n    }\n\n    static std::vector<iteration_result> execute_test_iterations(\n        const TaskConf &task_conf, unsigned time, size_t requested_memory_limit);\n\nprivate:\n    size_t req_mem_limit;\n    ScenarioWorkload *scenario_workload;\n    std::vector<memory_operation> results;\n    const TaskConf &task_conf;\n\n    //Test status\n    iteration_result test_status;\n};\n\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/Task.hpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n\n#include <vector>\n\n#include \"Runnable.hpp\"\n#include \"Allocation_info.hpp\"\n\nclass Task\n    : public Runnable\n{\npublic:\n    virtual ~Task() {}\n\n    virtual std::vector<memory_operation> get_results() = 0;\n};\n\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/TaskFactory.hpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n\n#include <cstdlib>\n#include <vector>\n#include <assert.h>\n\n#include \"Task.hpp\"\n#include \"FunctionCallsPerformanceTask.h\"\n#include \"Configuration.hpp\"\n\n\nclass TaskFactory\n{\npublic:\n\n    Task *create(TaskConf conf)\n    {\n        Task *task = NULL;\n        task = new FunctionCallsPerformanceTask(conf);\n\n        tasks.push_back(task);\n\n        return task;\n    }\n\n    ~TaskFactory()\n    {\n        for (int i=0; i<tasks.size(); i++) {\n            delete tasks[i];\n        }\n    }\n\nprivate:\n    std::vector<Task *> tasks;\n};\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/Tests.hpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n\n/*\nThis file contain self-tests for Allocator Perf Tool.\n*/\n#pragma once\n\n#include <unistd.h>\n\n#include \"Configuration.hpp\"\n#include \"AllocatorFactory.hpp\"\n#include \"AllocationSizes.hpp\"\n#include \"VectorIterator.hpp\"\n#include \"StandardAllocatorWithTimer.hpp\"\n#include \"ScenarioWorkload.h\"\n#include \"TaskFactory.hpp\"\n#include \"Task.hpp\"\n\n//Basic test for allocators. Allocate 32 bytes by malloc, then deallocate it by free.\nvoid test_allocator(Allocator &allocator, const char *allocator_name)\n{\n    printf(\"\\n============= Allocator (%s) test ============= \\n\",allocator_name);\n\n    memory_operation data = allocator.wrapped_malloc(32);\n    printf(\"malloc: %p \\n\",data.ptr);\n\n    assert(data.ptr!=NULL);\n\n    allocator.wrapped_free(data.ptr);\n    printf(\"free: %p \\n\",data.ptr);\n\n    printf(\"\\n==================================================== \\n\");\n}\n\n//Test for the workload classes. This test count and validate the number or runs.\nvoid test_workload(Workload &workload, const int N, const char *workload_name)\n{\n    printf(\"\\n============= Work load (%s) test ============= \\n\",workload_name);\n\n    assert(workload.run()); // Do \"single\" memory operation (allocate or deallocate).\n\n    int i;\n    for (i=1; workload.run(); i++); // Do the rest of operations.\n    assert(i == N);\n\n    printf(\"\\n==================================================== \\n\");\n}\n\n//Simulate time consuming memory operation.\nmemory_operation time_consuming_memory_operation(unsigned int seconds)\n{\n    size_t size;\n\n    START_TEST(0,0)\n    sleep(seconds);\n    END_TEST\n}\n\n//This test check if timer has measured time.\nvoid test_timer()\n{\n    float total_time = time_consuming_memory_operation(2).total_time;\n\n    bool test_res = (total_time >=  2.0) && (total_time < 2.2);\n    if(!test_res)\n        printf(\"test_timer(): unexpected timing %f\", total_time);\n    assert(test_res);\n}\n\n//Test for iterators. Check the number of iterations, such as:\n//N == number of iterations.\ntemplate<class T>\nvoid test_iterator(Iterator<T> &it, const int N)\n{\n    printf(\"\\n================= Iteartor test ==================== \\n\");\n\n    assert(it.size() == N);\n    int i;\n    for (i=0; it.has_next(); i++) {\n        it.next();\n    }\n    printf(\"iterations=%d/%d\\n\",i,N);\n    assert(i == N);\n\n    printf(\"\\n==================================================== \\n\");\n}\n\n//This test validate range of values generated by random generators.\ntemplate<class T, class C>\nvoid test_iterator_values(VectorIterator<T> &it, const C from, const C to)\n{\n    for (int i=0; it.has_next(); i++) {\n        T val = it.next();\n\n        if(val < from) std::cout << \"ivalid value: actual=\" << val << \", expected= >=\"\n                                     << from << std::endl;\n\n        if(val > to) std::cout << \"ivalid value: actual=\" << val << \", expected= <=\" <<\n                                   to << std::endl;\n    }\n}\n\n//Test time counting instrumentation in StandardAllocatorWithTimer.\n//The instrumentation is made with START_TEST and END_TEST macros from WrapperMacros.h.\nvoid test_allocator_with_timer(int N, int seed)\n{\n    printf(\"\\n============= Allocator with timer test ============= \\n\");\n\n    StandardAllocatorWithTimer allocator;\n    VectorIterator<size_t> allocation_sizes =\n        AllocationSizes::generate_random_sizes(N, 32, 2048, seed);\n    memory_operation data;\n\n    double elaspsed_time = 0;\n    for (int i=0; i<N; i++) {\n        data = allocator.wrapped_malloc(allocation_sizes.next());\n        elaspsed_time += data.total_time;\n        allocator.wrapped_free(data.ptr);\n    }\n\n    printf(\"%d allocations and frees done in time: %f \\n\", N, elaspsed_time);\n\n    printf(\"\\n==================================================== \\n\");\n}\n\n//Test behavior of TypesConfiguration class, by enabling and disabling types.\nvoid test_types_conf()\n{\n    TypesConf types;\n\n    for (unsigned i=0; i<FunctionCalls::NUM_OF_FUNCTIONS; i++) {\n        types.enable_type(i);\n        assert(types.is_enabled(i));\n        types.disable_type(i);\n        assert(!types.is_enabled(i));\n    }\n}\n\nvoid execute_self_tests()\n{\n    const int N = 10000;\n    const size_t size_from = 32, size_to = 2048;\n    const unsigned seed = 11;\n\n    test_types_conf();\n\n    {\n        VectorIterator<size_t> it = AllocationSizes::generate_random_sizes(N, size_from,\n                                                                           size_to,seed);\n        test_iterator_values(it, size_from, size_to);\n    }\n\n    {\n        TypesConf enable_func_calls;\n        enable_func_calls.enable_type(FunctionCalls::MALLOC);\n\n        VectorIterator<int> it = FunctionCalls::generate_random_allocator_func_calls(N,\n                                                                                     seed, enable_func_calls);\n        test_iterator(it, N);\n    }\n\n//Timer implementation __cplusplus < 201100L is based on CPU clocks counting and it will fail on this test.\n#if __cplusplus > 201100L\n    test_timer();\n#endif\n\n    printf(\"Test completed! (press ENTER to continue)\\n\");\n}\n\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/Thread.hpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n#include <pthread.h>\n#include <assert.h>\n\n#include \"Runnable.hpp\"\n\nclass Thread\n{\npublic:\n    Thread(Runnable *runnable) : runnable_task(runnable) {}\n\n    void start()\n    {\n        int err = pthread_create(&thread_handle, NULL, execute_thread,\n                                 static_cast<void *>(runnable_task));\n        assert(!err);\n    };\n\n    void wait()\n    {\n        pthread_join(thread_handle, NULL);\n    };\n\n    Runnable *get_runnable_task()\n    {\n        return runnable_task;\n    }\n\nprivate:\n    static void *execute_thread(void *ptr)\n    {\n        Runnable *runnable = static_cast<Runnable *>(ptr);\n        assert(runnable);\n        runnable->run();\n        pthread_exit(NULL);\n    }\n\n    pthread_t thread_handle;\n    Runnable *runnable_task;\n};\n\n\nclass ThreadsManager\n{\npublic:\n    ThreadsManager(std::vector<Thread *> &threads_vec) : threads(threads_vec) {}\n    ~ThreadsManager()\n    {\n        release();\n    }\n\n    void start()\n    {\n        for (int i=0; i<threads.size(); i++) {\n            threads[i]->start();\n        }\n    }\n\n    void barrier()\n    {\n        for (int i=0; i<threads.size(); i++) {\n            threads[i]->wait();\n        }\n    }\n\n    void release()\n    {\n        for (int i=0; i<threads.size(); i++) {\n            delete threads[i];\n        }\n        threads.clear();\n    }\n\nprivate:\n    std::vector<Thread *> &threads;\n};\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/TimerSysTime.hpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n\n#include <sys/time.h>\n\nclass TimerSysTime\n{\npublic:\n    void start()\n    {\n        gettimeofday(&last, 0);\n    }\n\n    double getElapsedTime() const\n    {\n        struct timeval now;\n        gettimeofday(&now, 0);\n        double time_delta_sec = ((double)now.tv_sec - (double)last.tv_sec);\n        double time_delta_usec = ((double)now.tv_usec - (double)last.tv_usec) /\n                                 1000000.0;\n        return time_delta_sec + time_delta_usec;\n    }\n\nprivate:\n    struct timeval last;\n};\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/VectorIterator.hpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n\n#include \"Iterator.hpp\"\n\n#include <vector>\n\ntemplate<class T>\nclass VectorIterator\n    : public Iterator<T>\n{\npublic:\n\n    static VectorIterator create(int init_size)\n    {\n        return VectorIterator(init_size);\n    }\n    static VectorIterator create(std::vector<T> v)\n    {\n        return VectorIterator(v);\n    }\n\n    bool has_next() const\n    {\n        return !vec.empty();\n    }\n\n    T next()\n    {\n        T val = vec.back();\n        vec.pop_back();\n        return val;\n    }\n\n    size_t size() const\n    {\n        return vec.size();\n    }\n\nprivate:\n    VectorIterator(std::vector<T> v)\n    {\n        vec = v;\n    }\n\n    std::vector<T> vec;\n};\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/Workload.hpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n\n#include \"Allocator.hpp\"\n\nclass Workload\n{\npublic:\n    virtual bool run() = 0;\n    virtual ~Workload(void) {}\n\nprotected:\n    Allocator *allocator;\n};\n\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/WrappersMacros.hpp",
    "content": "/*\n* Copyright (C) 2015 - 2017 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n#pragma once\n\n#include <ctime>\n#include <sys/time.h>\n\n#include <chrono>\n#define START_TEST(ALLOCATOR, METHOD) \\\n\tmemory_operation data; \\\n\tdata.allocator_type = ALLOCATOR; \\\n\tdata.allocation_method = METHOD;\\\n\tdata.error_code = 0; \\\n\tstd::chrono::system_clock::time_point last = std::chrono::high_resolution_clock::now();\n#define END_TEST \\\n\tstd::chrono::system_clock::time_point now = std::chrono::high_resolution_clock::now(); \\\n\tstd::chrono::duration<double,std::milli> elapsedTime(now - last); \\\n\tdata.total_time = elapsedTime.count() / 1000.0; \\\n\tdata.size_of_allocation = size; \\\n\tdata.error_code = errno; \\\n\tdata.is_allocated = true; \\\n\treturn data;\n"
  },
  {
    "path": "deps/memkind/src/test/allocator_perf_tool/main.cpp",
    "content": "/*\n* Copyright (C) 2015 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n\n#include <stdio.h>\n#include <assert.h>\n#include <iostream>\n#include <vector>\n\n#include \"Configuration.hpp\"\n#include \"AllocatorFactory.hpp\"\n#include \"TaskFactory.hpp\"\n#include \"Task.hpp\"\n#include \"ConsoleLog.hpp\"\n#include \"Stats.hpp\"\n#include \"Thread.hpp\"\n#include \"Tests.hpp\"\n#include \"CommandLine.hpp\"\n#include \"FunctionCallsPerformanceTask.h\"\n#include \"StressIncreaseToMax.h\"\n\n/*\nCommand line description.\nSyntax:\n\tkey=value\nOptions:\n\t- 'test' - specify the test case. This option can be used with the following values: 'calls', 'all' or 'self',\n\twhere:\n\t\t'calls' - function calls performance test,\n\t\t'all' - execute both above ('footprint' and 'calls') tests,\n\t\t'self' - execute self tests\n\t\t's1' - stress tests\n\t\t(perform allocations until the maximum amount of allocated memory has been reached, than frees allocated memory.\n\t\tIf the time interval has not been exceed, than repeat the test),\n\t- 'operations' - the number of memory operations per thread\n\t- 'size_from' - lower bound for the random sizes of allocation\n\t- 'size_to' - upper bound for the random sizes of allocation\n\t- 'seed' - random seed\n\t- 'threads_num' - the number of threads per test case\n\t- 'time' - minimum execution time interval\n\t- 'kind' - the kind to test\n\t- 'csv_log' - if 'true' then log to csv file memory operations and statistics\n\t- 'call' specify the allocation function call. This option can be used with the following values: 'malloc' (default), 'calloc', 'realloc',\n\t- 'requested_memory_limit' test stops when the requested memory limit has been reached\n* - maximum of available memory in OS, or maximum memory based 'operations' parameter\nExample:\n1. Performance test:\n./perf_tool test=all operations=1000 size_from=32 size_to=20480 seed=11 threads_num=200\n2. Stress test\n./perf_tool test=s1 time=120 kind=MEMKIND_HBW size_from=1048576 csv_log=true requested_memory_limit=1048576\n*/\n\nint main(int argc, char *argv[])\n{\n    unsigned mem_operations_num = 1000;\n    size_t size_from = 32, size_to = 2048*1024;\n    unsigned seed = 11;\n    //should be at least one\n    size_t threads_number = 10;\n\n    CommandLine cmd_line(argc, argv);\n\n    if((argc >= 1) && cmd_line.is_option_set(\"test\", \"self\")) {\n        execute_self_tests();\n        getchar();\n    }\n\n    cmd_line.parse_with_strtol(\"operations\", mem_operations_num);\n    cmd_line.parse_with_strtol(\"size_from\", size_from);\n    cmd_line.parse_with_strtol(\"size_to\", size_to);\n    cmd_line.parse_with_strtol(\"seed\", seed);\n    cmd_line.parse_with_strtol(\"threads_num\", threads_number);\n\n    bool is_csv_log_enabled = cmd_line.is_option_set(\"csv_log\", \"true\");\n\n    //Heap Manager initialization\n    std::vector<AllocatorFactory::initialization_stat> stats =\n        AllocatorFactory().initialization_test();\n\n    if(!cmd_line.is_option_set(\"print_init_stats\", \"false\")) {\n        printf(\"\\nInitialization overhead:\\n\");\n        for (int i=0; i<stats.size(); i++) {\n            AllocatorFactory::initialization_stat stat = stats[i];\n            printf(\"%32s : time=%7.7f.s, ref_delta_time=%15f, node0=%10fMB, node1=%7.7fMB\\n\",\n                   AllocatorTypes::allocator_name(stat.allocator_type).c_str(),\n                   stat.total_time,\n                   stat.ref_delta_time,\n                   stat.memory_overhead[0],\n                   stat.memory_overhead[1]);\n        }\n    }\n\n    //Stress test by repeatedly increasing memory (to maximum), until given time interval has been exceed.\n    if(cmd_line.is_option_set(\"test\", \"s1\")) {\n        printf(\"Stress test (StressIncreaseToMax) start. \\n\");\n\n        if(!cmd_line.is_option_present(\"operations\"))\n            mem_operations_num = 1000000;\n\n        unsigned time = 120; //Default time interval.\n        cmd_line.parse_with_strtol(\"time\", time);\n\n        size_t requested_memory_limit = 1024*1024;\n        cmd_line.parse_with_strtol(\"requested_memory_limit\", requested_memory_limit);\n\n        unsigned allocator = AllocatorTypes::MEMKIND_HBW;\n        if(cmd_line.is_option_present(\"kind\")) {\n            //Enable memkind allocator and specify kind.\n            allocator = AllocatorTypes::allocator_type(cmd_line.get_option_value(\"kind\"));\n        }\n        TypesConf allocator_types;\n        allocator_types.enable_type(allocator);\n\n        TypesConf enable_func_calls;\n        enable_func_calls.enable_type(FunctionCalls::MALLOC);\n\n        TaskConf task_conf = {\n            mem_operations_num,\n            {\n                mem_operations_num,\n                size_from, //No random sizes.\n                size_from\n            },\n            enable_func_calls,\n            allocator_types,\n            11,\n            is_csv_log_enabled,\n        };\n\n        StressIncreaseToMax::execute_test_iterations(task_conf, time,\n                                                     requested_memory_limit);\n        return 0;\n    }\n\n    printf(\"\\nTest configuration: \\n\");\n    printf(\"\\t memory operations per thread = %u \\n\", mem_operations_num);\n    printf(\"\\t seed = %d\\n\", seed);\n    printf(\"\\t number of threads = %zu\\n\", threads_number);\n    printf(\"\\t size from-to = %zu-%zu\\n\\n\", size_from, size_to);\n\n    assert(size_from <= size_to);\n\n\n    TypesConf func_calls;\n    func_calls.enable_type(FunctionCalls::FREE);\n\n    if(cmd_line.is_option_present(\"call\")) {\n        //Enable heap manager function call.\n        func_calls.enable_type(FunctionCalls::function_type(\n                                   cmd_line.get_option_value(\"call\")));\n    } else {\n        func_calls.enable_type(FunctionCalls::MALLOC);\n    }\n\n    TypesConf allocator_types;\n    if(cmd_line.is_option_present(\"allocator\")) {\n        allocator_types.enable_type(AllocatorTypes::allocator_type(\n                                        cmd_line.get_option_value(\"allocator\")));\n    } else {\n        for(unsigned i = 0; i <= AllocatorTypes::MEMKIND_HBW_PREFERRED; i++) {\n            allocator_types.enable_type(i);\n        }\n    }\n\n    TaskConf conf = {\n        mem_operations_num, //number memory operations\n        {\n            mem_operations_num, //number of memory operations\n            size_from, //min. size of single allocation\n            size_to //max. size of single allocatioion\n        },\n        func_calls, //enable function calls\n        allocator_types, //enable allocators\n        seed, //random seed\n        is_csv_log_enabled,\n    };\n\n    //Function calls test\n    if(cmd_line.is_option_set(\"test\", \"calls\") ||\n       cmd_line.is_option_set(\"test\", \"all\")) {\n        TaskFactory task_factory;\n        std::vector<Thread *> threads;\n        std::vector<Task *> tasks;\n\n        for (int i=0; i<threads_number; i++) {\n            FunctionCallsPerformanceTask *task =\n                static_cast<FunctionCallsPerformanceTask *>(\n                    task_factory.create(conf)\n                );\n            tasks.push_back(task);\n            threads.push_back(new Thread(task));\n            conf.seed += 1;\n        }\n\n        ThreadsManager threads_manager(threads);\n        threads_manager.start();\n        threads_manager.barrier();\n\n        TimeStats stats;\n        for (int i=0; i<tasks.size(); i++) {\n            stats += tasks[i]->get_results();\n        }\n\n        ConsoleLog::print_table(stats);\n        ConsoleLog::print_requested_memory(stats, \"func. calls test\");\n\n        threads_manager.release();\n    }\n\n    return 0;\n}\n"
  },
  {
    "path": "deps/memkind/src/test/autohbw_test.py",
    "content": "#\n#  Copyright (C) 2016 Intel Corporation.\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#  1. Redistributions of source code must retain the above copyright notice(s),\n#     this list of conditions and the following disclaimer.\n#  2. Redistributions in binary form must reproduce the above copyright notice(s),\n#     this list of conditions and the following disclaimer in the documentation\n#     and/or other materials provided with the distribution.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n#  EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n#  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n#\n\nimport pytest\nimport os\nfrom python_framework import CMD_helper\n\ndef _get_lib_path():\n    for p in [\"/usr/lib64\", \"/usr/lib\"]:\n        if os.path.isdir(p):\n            return p\n    raise Exception(\"Cannot find library path in OS\")\n\nclass Test_autohbw(object):\n    binary = \"../autohbw_test_helper\"\n    fail_msg = \"Test failed with:\\n {0}\"\n    test_prefix = \"AUTO_HBW_LOG=2 LD_PRELOAD=%s/libautohbw.so.0 \" % _get_lib_path()\n    memkind_malloc_log = \"In my memkind malloc\"\n    memkind_calloc_log = \"In my memkind calloc\"\n    memkind_realloc_log = \"In my memkind realloc\"\n    memkind_posix_memalign_log = \"In my memkind align\"\n    memkind_free_log = \"In my memkind free\"\n    cmd_helper = CMD_helper()\n\n    def test_TC_MEMKIND_autohbw_malloc_and_free(self):\n        \"\"\" This test executes ./autohbw_test_helper with LD_PRELOAD that is overriding malloc() and free() to equivalent autohbw functions\"\"\"\n        command = self.test_prefix + self.cmd_helper.get_command_path(self.binary) + \" malloc\"\n        print \"Executing command: {0}\".format(command)\n        output, retcode = self.cmd_helper.execute_cmd(command, sudo=False)\n        assert retcode == 0, self.fail_msg.format(\"\\nError: autohbw_test_helper returned {0} \\noutput: {1}\".format(retcode,output))\n        assert self.memkind_malloc_log in output, self.fail_msg.format(\"\\nError: malloc was not overrided by autohbw equivalent \\noutput: {0}\").format(output)\n        assert self.memkind_free_log in output, self.fail_msg.format(\"\\nError: free was not overrided by autohbw equivalent \\noutput: {0}\").format(output)\n\n    def test_TC_MEMKIND_autohbw_calloc_and_free(self):\n        \"\"\" This test executes ./autohbw_test_helper with LD_PRELOAD that is overriding calloc() and free() to equivalent autohbw functions\"\"\"\n        command = self.test_prefix + self.cmd_helper.get_command_path(self.binary) + \" calloc\"\n        print \"Executing command: {0}\".format(command)\n        output, retcode = self.cmd_helper.execute_cmd(command, sudo=False)\n        assert retcode == 0, self.fail_msg.format(\"\\nError: autohbw_test_helper returned {0} \\noutput: {1}\".format(retcode,output))\n        assert self.memkind_calloc_log in output, self.fail_msg.format(\"\\nError: calloc was not overrided by autohbw equivalent \\noutput: {0}\").format(output)\n        assert self.memkind_free_log in output, self.fail_msg.format(\"Error: free was not overrided by autohbw equivalent \\noutput: {0}\").format(output)\n\n    def test_TC_MEMKIND_autohbw_realloc_and_free(self):\n        \"\"\" This test executes ./autohbw_test_helper with LD_PRELOAD that is overriding realloc() and free() to equivalent autohbw functions\"\"\"\n        command = self.test_prefix + self.cmd_helper.get_command_path(self.binary) + \" realloc\"\n        print \"Executing command: {0}\".format(command)\n        output, retcode = self.cmd_helper.execute_cmd(command, sudo=False)\n        assert retcode == 0, self.fail_msg.format(\"\\nError: autohbw_test_helper returned {0} \\noutput: {1}\".format(retcode,output))\n        assert self.memkind_realloc_log in output, self.fail_msg.format(\"\\nError: realloc was not overrided by autohbw equivalent \\noutput: {0}\").format(output)\n        assert self.memkind_free_log in output, self.fail_msg.format(\"\\nError: free was not overrided by autohbw equivalent \\noutput: {0}\").format(output)\n\n    def test_TC_MEMKIND_autohbw_posix_memalign_and_free(self):\n        \"\"\" This test executes ./autohbw_test_helper with LD_PRELOAD that is overriding posix_memalign() and free() to equivalent autohbw functions\"\"\"\n        command = self.test_prefix + self.cmd_helper.get_command_path(self.binary) + \" posix_memalign\"\n        print \"Executing command: {0}\".format(command)\n        output, retcode = self.cmd_helper.execute_cmd(command, sudo=False)\n        assert retcode == 0, self.fail_msg.format(\"\\nError: autohbw_test_helper returned {0} \\noutput: {1}\".format(retcode,output))\n        assert self.memkind_posix_memalign_log in output, self.fail_msg.format(\"\\nError: posix_memalign was not overrided by autohbw equivalent \\noutput: {0}\").format(output)\n        assert self.memkind_free_log in output, self.fail_msg.format(\"\\nError: free was not overrided by autohbw equivalent \\noutput: {0}\").format(output)\n"
  },
  {
    "path": "deps/memkind/src/test/autohbw_test_helper.c",
    "content": "/*\n * Copyright (C) 2016 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\nint main(int argc, char *argv[])\n{\n    int err = 0;\n    const size_t size = 1024 * 1024;\n    void *buf = NULL;\n\n    //It is expected that \"malloc\", \"calloc\", \"realloc\" or \"posix_memalign\" argument is passed\n    if (argc != 2) {\n        printf(\"Error: Wrong number of parameters\\n\");\n        err = -1;\n        return err;\n    }\n\n    if (strcmp(argv[1], \"malloc\") == 0) {\n        buf = malloc(size);\n        if (buf == NULL) {\n            printf(\"Error: malloc returned NULL\\n\");\n            err = -1;\n        }\n        free(buf);\n    } else if (strcmp(argv[1], \"calloc\") == 0) {\n        buf = calloc(size, 1);\n        if (buf == NULL) {\n            printf(\"Error: calloc returned NULL\\n\");\n            err = -1;\n        }\n        free(buf);\n    } else if (strcmp(argv[1], \"realloc\") == 0) {\n        buf = malloc(size);\n        if (buf == NULL) {\n            printf(\"Error: malloc before realloc returned NULL\\n\");\n            err = -1;\n        }\n        buf = realloc(buf, size * 2);\n        if (buf == NULL) {\n            printf(\"Error: realloc returned NULL\\n\");\n            err = -1;\n        }\n        free(buf);\n    } else if (strcmp(argv[1], \"posix_memalign\") == 0) {\n        err = posix_memalign(&buf, 64, size);\n        if (err != 0) {\n            printf(\"Error: posix_memalign returned %d\\n\", err);\n        }\n        free(buf);\n    } else {\n        printf(\"Error: unknown parameter\\n\");\n        err = -1;\n    }\n\n    return err;\n}\n"
  },
  {
    "path": "deps/memkind/src/test/bat_tests.cpp",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"memkind.h\"\n\n#include <fstream>\n#include <algorithm>\n#include <numaif.h>\n#include <memkind/internal/memkind_regular.h>\n\n#include \"common.h\"\n#include \"check.h\"\n#include \"omp.h\"\n#include \"trial_generator.h\"\n#include \"allocator_perf_tool/HugePageOrganizer.hpp\"\n#include \"Allocator.hpp\"\n#include \"TestPolicy.hpp\"\n\n#define TEST_PREFIX \"test_TC_MEMKIND_BAT_\"\n\ntypedef void(*test_function)(Allocator *, size_t);\n\ntypedef std::tuple<test_function, memkind_memtype_t, memkind_policy_t, memkind_bits_t, size_t>\nmemtype_policy_test_params;\n\ntest_function get_function(memtype_policy_test_params params)\n{\n    return std::get<0>(params);\n}\n\nmemkind_memtype_t get_memtype(memtype_policy_test_params params)\n{\n    return std::get<1>(params);\n}\n\nmemkind_policy_t get_policy(memtype_policy_test_params params)\n{\n    return std::get<2>(params);\n}\n\nmemkind_bits_t get_flags(memtype_policy_test_params params)\n{\n    return std::get<3>(params);\n}\n\nsize_t get_size(memtype_policy_test_params params)\n{\n    return std::get<4>(params);\n}\n\ntypedef std::tuple<test_function, hbw_policy_t, size_t> hbw_policy_test_params;\n\ntest_function get_function(hbw_policy_test_params params)\n{\n    return std::get<0>(params);\n}\n\nhbw_policy_t get_hbw_policy(hbw_policy_test_params params)\n{\n    return std::get<1>(params);\n}\n\nsize_t get_size(hbw_policy_test_params params)\n{\n    return std::get<2>(params);\n}\n\ntypedef std::tuple<hbw_policy_t, size_t> hbw_policy_huge_page_test_params;\n\nhbw_policy_t get_hbw_policy(hbw_policy_huge_page_test_params params)\n{\n    return std::get<0>(params);\n}\n\nsize_t get_size(hbw_policy_huge_page_test_params params)\n{\n    return std::get<1>(params);\n}\n\n\n\n/*\n * Set of basic acceptance tests.\n */\nclass BATest: public TGTest {};\nclass BasicAllocTest\n{\npublic:\n\n    BasicAllocTest(Allocator *allocator) : allocator(allocator) {}\n\n    void check_policy_and_numa_node(void *ptr, size_t size)\n    {\n        int policy = allocator->get_numa_policy();\n\n        EXPECT_NE(-1, policy);\n\n        if (allocator->is_high_bandwidth()) {\n            TestPolicy::check_hbw_numa_nodes(policy, ptr, size);\n        } else {\n            TestPolicy::check_all_numa_nodes(policy, ptr, size);\n        }\n    }\n\n    void record_page_association(void *ptr, size_t size)\n    {\n        TestPolicy::record_page_association(ptr, size, allocator->get_page_size());\n    }\n\n    void malloc(size_t size)\n    {\n        void *ptr = allocator->malloc(size);\n        ASSERT_TRUE(ptr != NULL) << \"malloc() returns NULL\";\n        void *memset_ret = memset(ptr, 3, size);\n        EXPECT_TRUE(memset_ret != NULL);\n        record_page_association(ptr, size);\n        check_policy_and_numa_node(ptr, size);\n        allocator->free(ptr);\n    }\n\n    void calloc(size_t num, size_t size)\n    {\n        void *ptr = allocator->calloc(num, size);\n        ASSERT_TRUE(ptr != NULL) << \"calloc() returns NULL\";\n        void *memset_ret = memset(ptr, 3, size);\n        ASSERT_TRUE(memset_ret != NULL);\n        record_page_association(ptr, size);\n        check_policy_and_numa_node(ptr, size);\n        allocator->free(ptr);\n    }\n\n    void realloc(size_t size)\n    {\n        void *ptr = allocator->malloc(size);\n        ASSERT_TRUE(ptr != NULL) << \"malloc() returns NULL\";\n        size_t realloc_size = size+128;\n        ptr = allocator->realloc(ptr, realloc_size);\n        ASSERT_TRUE(ptr != NULL) << \"realloc() returns NULL\";\n        void *memset_ret = memset(ptr, 3, realloc_size);\n        ASSERT_TRUE(memset_ret != NULL);\n        record_page_association(ptr, size);\n        check_policy_and_numa_node(ptr, size);\n        allocator->free(ptr);\n    }\n\n    void memalign(size_t alignment, size_t size)\n    {\n        void *ptr = NULL;\n        int ret = allocator->memalign(&ptr, alignment, size);\n        ASSERT_EQ(0, ret) << \"posix_memalign() != 0\";\n        ASSERT_TRUE(ptr != NULL) << \"posix_memalign() returns NULL pointer\";\n        void *memset_ret = memset(ptr, 3, size);\n        ASSERT_TRUE(memset_ret != NULL);\n        record_page_association(ptr, size);\n        check_policy_and_numa_node(ptr, size);\n        allocator->free(ptr);\n    }\n\n    void free(size_t size)\n    {\n        void *ptr = allocator->malloc(size);\n        ASSERT_TRUE(ptr != NULL) << \"malloc() returns NULL\";\n        allocator->free(ptr);\n    }\n\n    virtual ~BasicAllocTest() {}\nprivate:\n    Allocator *allocator;\n};\n\n\nstatic void test_malloc(Allocator *allocator, size_t size)\n{\n    BasicAllocTest(allocator).malloc(size);\n}\n\nstatic void test_calloc(Allocator *allocator, size_t size)\n{\n    BasicAllocTest(allocator).calloc(1, size);\n}\n\nstatic void test_realloc(Allocator *allocator, size_t size)\n{\n    BasicAllocTest(allocator).realloc(size);\n}\n\nstatic void test_memalign(Allocator *allocator, size_t size)\n{\n    BasicAllocTest(allocator).memalign(4096, size);\n}\n\nstatic void test_free(Allocator *allocator, size_t size)\n{\n    BasicAllocTest(allocator).free(size);\n}\n\n\ntemplate <typename T>\nstd::vector<T> GetKeys(std::map<T, std::string> dict)\n{\n    std::vector<T> keys;\n    for (auto const &item: dict) {\n        keys.push_back(item.first);\n    }\n    return keys;\n}\n\nstruct TestParameters {\n    static const std::map<test_function, std::string> functions;\n    static const std::map<memkind_memtype_t, std::string> memtypes;\n    static const std::map<memkind_policy_t, std::string> policies;\n    static const std::map<hbw_policy_t, std::string> hbw_policies;\n    static const std::vector<size_t> sizes;\n};\n\nconst map<test_function, std::string> TestParameters::functions = {\n    {&test_malloc, \"malloc\"},\n    {&test_calloc, \"calloc\"},\n    {&test_realloc, \"realloc\"},\n    {&test_memalign, \"memalign\"},\n    {&test_free, \"free\"}\n};\nconst std::map<memkind_memtype_t, std::string> TestParameters::memtypes = {{MEMKIND_MEMTYPE_DEFAULT, \"DEFAULT\"},\n    {MEMKIND_MEMTYPE_HIGH_BANDWIDTH, \"HIGH_BANDWIDTH\"}\n};\nconst std::map<memkind_policy_t, std::string> TestParameters::policies = {{MEMKIND_POLICY_PREFERRED_LOCAL, \"PREFERRED_LOCAL\"},\n    {MEMKIND_POLICY_BIND_LOCAL, \"BIND_LOCAL\"},\n    {MEMKIND_POLICY_BIND_ALL, \"BIND_ALL\"},\n    {MEMKIND_POLICY_INTERLEAVE_ALL, \"MEMKIND_POLICY_INTERLEAVE_ALL\"}\n};\nconst std::map<hbw_policy_t, std::string> TestParameters::hbw_policies = {{HBW_POLICY_PREFERRED, \"HBW_POLICY_PREFERRED\"},\n    {HBW_POLICY_BIND, \"HBW_POLICY_BIND\"},\n    {HBW_POLICY_BIND_ALL, \"HBW_POLICY_BIND_ALL\"},\n    {HBW_POLICY_INTERLEAVE, \"HBW_POLICY_INTERLEAVE\"}\n};\nconst std::vector<size_t> TestParameters::sizes = {4096, 4194305};\n\n\nclass MemtypePolicyTest: public TGTest,\n    public ::testing::WithParamInterface<memtype_policy_test_params>\n{\npublic:\n    static std::string get_test_name_suffix(\n        testing::TestParamInfo<memtype_policy_test_params> info)\n    {\n        return TEST_PREFIX +\n               TestParameters::functions.at(get_function(info.param)) + \"_\" +\n               TestParameters::memtypes.at(get_memtype(info.param)) + \"_\" +\n               TestParameters::policies.at(get_policy(info.param)) + \"_\" +\n               (get_flags(info.param) == MEMKIND_MASK_PAGE_SIZE_2MB ? \"PAGE_SIZE_2MB_\" : \"\") +\n               std::to_string(get_size(info.param)) + \"_bytes\";\n    }\n};\n\nTEST_P(MemtypePolicyTest, memkind_heap_mgmt)\n{\n    HugePageOrganizer huge_page_organizer(8);\n    auto params = GetParam();\n    auto flags = get_flags(params);\n    MemkindAllocator allocator(get_memtype(GetParam()), get_policy(GetParam()),\n                               flags);\n    get_function(GetParam())(&allocator, get_size(GetParam()));\n}\n\nINSTANTIATE_TEST_CASE_P(DEFAULT,\n                        MemtypePolicyTest,\n                        ::testing::Combine(::testing::ValuesIn(GetKeys(TestParameters::functions)),\n                                           ::testing::Values(MEMKIND_MEMTYPE_DEFAULT),\n                                           ::testing::Values(MEMKIND_POLICY_PREFERRED_LOCAL),\n                                           ::testing::Values(MEMKIND_MASK_PAGE_SIZE_2MB, memkind_bits_t()),\n                                           ::testing::ValuesIn(TestParameters::sizes)),\n                        MemtypePolicyTest::get_test_name_suffix\n                       );\n\nINSTANTIATE_TEST_CASE_P(HBW,\n                        MemtypePolicyTest,\n                        ::testing::Combine(::testing::ValuesIn(GetKeys(TestParameters::functions)),\n                                           ::testing::Values(MEMKIND_MEMTYPE_HIGH_BANDWIDTH),\n                                           ::testing::ValuesIn(GetKeys(TestParameters::policies)),\n                                           ::testing::Values(memkind_bits_t()),\n                                           ::testing::ValuesIn(TestParameters::sizes)),\n                        MemtypePolicyTest::get_test_name_suffix\n                       );\n\nINSTANTIATE_TEST_CASE_P(HBW_HUGE,\n                        MemtypePolicyTest,\n                        ::testing::Combine(::testing::ValuesIn(GetKeys(TestParameters::functions)),\n                                           ::testing::Values(MEMKIND_MEMTYPE_HIGH_BANDWIDTH),\n                                           ::testing::Values(MEMKIND_POLICY_PREFERRED_LOCAL,\n                                                             MEMKIND_POLICY_BIND_LOCAL,\n                                                             MEMKIND_POLICY_BIND_ALL),\n                                           ::testing::Values(MEMKIND_MASK_PAGE_SIZE_2MB),\n                                           ::testing::ValuesIn(TestParameters::sizes)),\n                        MemtypePolicyTest::get_test_name_suffix\n                       );\n\n\nclass HBWPolicyHugePageTest: public TGTest,\n    public ::testing::WithParamInterface<hbw_policy_huge_page_test_params>\n{\npublic:\n    static std::string get_test_name_suffix(\n        testing::TestParamInfo<hbw_policy_huge_page_test_params> info)\n    {\n        return TEST_PREFIX +\n               TestParameters::hbw_policies.at(get_hbw_policy(info.param)) + \"_\" +\n               std::to_string(get_size(info.param)) + \"_bytes\";\n    }\n};\n\nTEST_P(HBWPolicyHugePageTest, memalign)\n{\n    HugePageOrganizer huge_page_organizer(8);\n    HbwmallocAllocator hbwmalloc_allocator(get_hbw_policy(GetParam()));\n    hbwmalloc_allocator.set_memalign_page_size(HBW_PAGESIZE_2MB);\n    BasicAllocTest(&hbwmalloc_allocator).memalign(4096, get_size(GetParam()));\n}\n\nINSTANTIATE_TEST_CASE_P(HBW_memalign,\n                        HBWPolicyHugePageTest,\n                        ::testing::Combine(::testing::Values(HBW_POLICY_PREFERRED, HBW_POLICY_BIND,\n                                                             HBW_POLICY_BIND_ALL),\n                                           ::testing::ValuesIn(TestParameters::sizes)),\n                        HBWPolicyHugePageTest::get_test_name_suffix\n                       );\n\n\nclass HBWPolicyTest: public TGTest,\n    public ::testing::WithParamInterface<hbw_policy_test_params>\n{\npublic:\n    static std::string get_test_name_suffix(\n        testing::TestParamInfo<hbw_policy_test_params> info)\n    {\n        return TEST_PREFIX +\n               TestParameters::functions.at(get_function(info.param)) + \"_\" +\n               TestParameters::hbw_policies.at(get_hbw_policy(info.param)) + \"_\" +\n               std::to_string(get_size(info.param)) + \"_bytes\";\n    }\n};\n\nTEST_P(HBWPolicyTest, memkind_heap_mgmt)\n{\n    HbwmallocAllocator allocator(get_hbw_policy(GetParam()));\n    get_function(GetParam())(&allocator, get_size(GetParam()));\n}\n\nINSTANTIATE_TEST_CASE_P(HBWPolicy,\n                        HBWPolicyTest,\n                        ::testing::Combine(::testing::ValuesIn(GetKeys(TestParameters::functions)),\n                                           ::testing::ValuesIn(GetKeys(TestParameters::hbw_policies)),\n                                           ::testing::ValuesIn(TestParameters::sizes)),\n                        HBWPolicyTest::get_test_name_suffix\n                       );\n\n\nclass MemkindRegularTest: public TGTest,\n    public ::testing::WithParamInterface<tuple<test_function, size_t>>\n{\npublic:\n    static std::string get_test_name_suffix(\n        testing::TestParamInfo<std::tuple<test_function, size_t>> info)\n    {\n        return TEST_PREFIX +\n               TestParameters::functions.at(std::get<0>(info.param)) + \"_\" +\n               std::to_string(std::get<1>(info.param)) + \"_bytes\";\n    }\n};\n\n\nTEST_P(MemkindRegularTest, memkind_heap_mgmt)\n{\n    MemkindAllocator allocator(MEMKIND_REGULAR);\n    std::get<0>(GetParam())(&allocator, std::get<1>(GetParam()));\n}\n\nINSTANTIATE_TEST_CASE_P(Regular,\n                        MemkindRegularTest,\n                        ::testing::Combine(::testing::ValuesIn(GetKeys(TestParameters::functions)),\n                                           ::testing::ValuesIn(TestParameters::sizes)),\n                        MemkindRegularTest::get_test_name_suffix\n                       );\n\n\nTEST_F(BATest,\n       test_TC_MEMKIND_malloc_DEFAULT_HIGH_BANDWIDTH_INTERLEAVE_ALL_4194305_bytes)\n{\n    MemkindAllocator allocator((memkind_memtype_t)(MEMKIND_MEMTYPE_DEFAULT |\n                                                   MEMKIND_MEMTYPE_HIGH_BANDWIDTH), MEMKIND_POLICY_INTERLEAVE_ALL,\n                               memkind_bits_t());\n    test_malloc(&allocator, 4194305);\n}\n\nTEST_F(BATest,\n       test_TC_MEMKIND_free_MEMKIND_DEFAULT_free_with_NULL_kind_4096_bytes)\n{\n    void *ptr = memkind_malloc(MEMKIND_DEFAULT, 4096);\n    ASSERT_TRUE(ptr != NULL) << \"malloc() returns NULL\";\n    memkind_free(0, ptr);\n}\n\nTEST_F(BATest, test_TC_MEMKIND_free_ext_MEMKIND_GBTLB_4096_bytes)\n{\n    HugePageOrganizer huge_page_organizer(1000);\n    MemkindAllocator memkind_allocator(MEMKIND_GBTLB);\n    BasicAllocTest(&memkind_allocator).free(4096);\n}\n\nTEST_F(BATest, test_TC_MEMKIND_hbwmalloc_Pref_CheckAvailable)\n{\n    ASSERT_EQ(0, hbw_check_available());\n}\n\nTEST_F(BATest, test_TC_MEMKIND_hbwmalloc_Pref_Policy)\n{\n    hbw_set_policy(HBW_POLICY_PREFERRED);\n    EXPECT_EQ(HBW_POLICY_PREFERRED, hbw_get_policy());\n}\n\nTEST_F(BATest, test_TC_MEMKIND_REGULAR_nodemask)\n{\n    using namespace TestPolicy;\n    void *mem = memkind_malloc(MEMKIND_REGULAR, 1234567);\n    unique_bitmask_ptr kind_nodemask = make_nodemask_ptr();\n    ASSERT_EQ(0, memkind_regular_all_get_mbind_nodemask(MEMKIND_REGULAR,\n                                                        kind_nodemask->maskp,\n                                                        kind_nodemask->size));\n\n    check_numa_nodes(kind_nodemask, MPOL_BIND, mem, 1234567);\n    memkind_free(MEMKIND_REGULAR, mem);\n}\n\n"
  },
  {
    "path": "deps/memkind/src/test/check.cpp",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind/internal/memkind_hbw.h>\n\n#include <stdlib.h>\n#include <stdio.h>\n#include <sys/types.h>\n#include <sys/stat.h>\n#include <unistd.h>\n#include <fcntl.h>\n#include <numaif.h>\n#include <errno.h>\n#include <fstream>\n#include <sstream>\n#include <iostream>\n#include <fstream>\n#include <string>\n#include <cstring>\n#include <numa.h>\n#include <errno.h>\n\n#include \"check.h\"\n\n\nusing namespace std;\n\n\n/*Check each page between the start\nand the end address additionally also\ncheck the end address for pagesize*/\nCheck::Check(const void *p, const trial_t &trial): Check(p, trial.size,\n                                                             trial.page_size)\n{\n}\n\nCheck::Check(const void *p, const size_t size, const size_t page_size)\n{\n    const size_t min_page_size = 4096;\n    this->ptr = p;\n    this->size = size;\n    size_t psize = (page_size >= min_page_size ? page_size : min_page_size);\n    if (p && size && psize) {\n        num_address = size / psize;\n        num_address += size % psize ? 1 : 0;\n\n        address = new void *[num_address];\n        size_t i;\n        for (i = 0; i < num_address - 1; ++i) {\n            address[i] = (char *)ptr + i * psize;\n        }\n        address[i] = (char *)p + size - 1;\n    } else {\n        address = NULL;\n        num_address = 0;\n    }\n}\n\nCheck::~Check()\n{\n    delete[] address;\n}\n\nCheck::Check(const Check &other)\n{\n    num_address = other.num_address;\n\n    address = new void *[num_address];\n    for (size_t i = 0; i < num_address; ++i) {\n        address[i] = other.address[i];\n    }\n}\n\nint Check::check_zero(void)\n{\n    size_t i;\n    const char *cptr = (char *)ptr;\n    for (i = 0; i < size; ++i) {\n        if (cptr[i] != '\\0') {\n            return -1;\n        }\n    }\n    return 0;\n}\n\nint Check::check_align(size_t align)\n{\n    return (size_t)ptr % align;\n}\n\nstring Check::skip_to_next_entry (ifstream &ip)\n{\n    string temp, token;\n    size_t found = 0;\n    string empty =\"\";\n\n    while (!ip.eof()) {\n        getline (ip, temp);\n        found = temp.find(\"-\");\n        if (found != string::npos) {\n            istringstream iss(temp);\n            getline(iss, token, ' ');\n            return token;\n        }\n    }\n    return empty;\n}\n\nstring Check::skip_to_next_kpage(ifstream &ip)\n{\n    string temp, token;\n    size_t found = 0;\n    string empty =\"\";\n\n    while (!ip.eof()) {\n        getline (ip, temp);\n        found = temp.find(\"KernelPageSize:\");\n        if (found != string::npos) {\n            return temp;\n        }\n    }\n    return empty;\n}\n\n\nvoid Check::get_address_range(string &line,\n                              unsigned long long *start_addr,\n                              unsigned long long *end_addr)\n{\n    stringstream ss(line);\n    string token;\n\n    getline(ss, token, '-');\n    *start_addr = strtoul(token.c_str(),\n                          NULL,\n                          16);\n    getline(ss, token, '-');\n    *end_addr = strtoul(token.c_str(),\n                        NULL,\n                        16);\n}\n\nsize_t Check::get_kpagesize(string line)\n{\n    stringstream ss(line);\n    string token;\n    size_t pagesize;\n\n\n    ss  >> token;\n    ss  >> token;\n\n    pagesize = atol(token.c_str());\n\n    return (size_t)pagesize;\n}\n\nint Check::check_page_size(size_t page_size)\n{\n    int err = 0;\n    size_t i;\n\n    ip.open (\"/proc/self/smaps\");\n\n    populate_smaps_table();\n    if (check_page_size(page_size, address[0])) {\n        err = -1;\n    }\n    for (i = 1; i < num_address && !err; ++i) {\n        if (check_page_size(page_size, address[i])) {\n            err = i;\n        }\n    }\n    return err;\n}\n\nint Check::populate_smaps_table ()\n{\n    string read;\n    size_t lpagesize;\n    smaps_entry_t lentry;\n    unsigned long long start_addr;\n    unsigned long long end_addr;\n\n    ip >> read;\n    while (!ip.eof()) {\n\n        start_addr = end_addr = 0;\n        get_address_range (read,\n                           &start_addr,\n                           &end_addr);\n        read = skip_to_next_kpage(ip);\n        getline(ip, read);\n        lpagesize = get_kpagesize(read);\n        lpagesize *= 1024;\n        lentry.start_addr = start_addr;\n        lentry.end_addr = end_addr;\n        lentry.pagesize = lpagesize;\n        smaps_table.push_back(lentry);\n        read = skip_to_next_entry(ip);\n        if (read.empty()) {\n            break;\n        }\n    }\n\n    if (0 == smaps_table.size()) {\n        fprintf(stderr,\"Empty smaps table\\n\");\n        return -1;\n    } else {\n        return 0;\n    }\n\n}\n\nint Check::check_page_size(size_t page_size, void *vaddr)\n{\n    string read;\n    unsigned long long virt_addr;\n    size_t lpagesize;\n    list<smaps_entry_t>::iterator it;\n    unsigned long long start_addr;\n    unsigned long long end_addr;\n\n    virt_addr = (unsigned long long)(vaddr);\n\n    for (it = smaps_table.begin();\n         it != smaps_table.end();\n         it++) {\n\n        start_addr = it->start_addr;\n        end_addr = it->end_addr;\n\n        if ((virt_addr >= start_addr) &&\n            (virt_addr < end_addr)) {\n            lpagesize = it->pagesize;\n            if (lpagesize == page_size) {\n                return 0;\n            } else {\n                /*The pagesize of allocation and req don't match*/\n                fprintf(stderr,\"%zd does not match entry in SMAPS (%zd)\\n\",\n                        page_size, lpagesize);\n                return -1;\n            }\n        }\n    }\n    /*Never found a match!*/\n    return 1;\n}\n"
  },
  {
    "path": "deps/memkind/src/test/check.h",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef check_include_h\n#define check_include_h\n\n#include <list>\n#include \"trial_generator.h\"\n\n\ntypedef struct {\n    unsigned long start_addr;\n    unsigned long end_addr;\n    size_t pagesize;\n} smaps_entry_t;\n\n\n\nusing namespace std;\n\nclass Check\n{\npublic:\n    Check(const void *p, const trial_t &trial);\n    Check(const void *p, const size_t size, const size_t page_size);\n    Check(const Check &);\n    ~Check();\n    int check_page_size(size_t page_size);\n    int check_zero(void);\n    int check_align(size_t align);\nprivate:\n    const void *ptr;\n    size_t size;\n    void **address;\n    size_t num_address;\n    list<smaps_entry_t>smaps_table;\n    ifstream ip;\n    int smaps_fd;\n    string skip_to_next_entry(ifstream &);\n    string skip_to_next_kpage(ifstream &);\n    void get_address_range(string &line, unsigned long long *start_addr,\n                           unsigned long long *end_addr);\n    size_t get_kpagesize(string line);\n    int check_page_size(size_t page_size, void *vaddr);\n    int populate_smaps_table();\n};\n\n#endif\n"
  },
  {
    "path": "deps/memkind/src/test/common.h",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef COMMON_H\n#define COMMON_H\n\n#include <hbwmalloc.h>\n\n#include <iostream>\n#include <stdint.h>\n#include <stdlib.h>\n#include <gtest/gtest.h>\n\n#define MB 1048576ULL\n#define GB 1073741824ULL\n#define KB 1024ULL\n\n#define HBW_SUCCESS 0\n#define HBW_ERROR -1\n\n#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))\n\n#endif\n"
  },
  {
    "path": "deps/memkind/src/test/decorator_test.cpp",
    "content": "/*\n * Copyright (C) 2014 - 2016 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"memkind.h\"\n\n#include \"common.h\"\n#include \"decorator_test.h\"\n#include \"config.h\"\n\nsize_t size = 16;\nmemkind_t kind = MEMKIND_DEFAULT;\n\nclass DecoratorTest: public :: testing::Test\n{\n\nprotected:\n    void SetUp()\n    {\n        decorators_state = new decorators_flags();\n    }\n\n    void TearDown()\n    {\n        free(decorators_state);\n    }\n};\n\nTEST_F(DecoratorTest, test_TC_MEMKIND_DT_malloc)\n{\n#ifdef MEMKIND_DECORATION_ENABLED\n    void *buffer = memkind_malloc(kind, size);\n\n    ASSERT_TRUE(buffer != NULL);\n    EXPECT_EQ(1, decorators_state->malloc_pre);\n    EXPECT_EQ(1, decorators_state->malloc_post);\n\n    memkind_free(0, buffer);\n#endif\n}\n\nTEST_F(DecoratorTest, test_TC_MEMKIND_DT_calloc)\n{\n#ifdef MEMKIND_DECORATION_ENABLED\n    void *buffer = memkind_calloc(kind, 1, size);\n\n    ASSERT_TRUE(buffer != NULL);\n    EXPECT_EQ(1, decorators_state->calloc_pre);\n    EXPECT_EQ(1, decorators_state->calloc_post);\n\n    memkind_free(0, buffer);\n#endif\n}\n\nTEST_F(DecoratorTest, test_TC_MEMKIND_DT_posix_memalign)\n{\n#ifdef MEMKIND_DECORATION_ENABLED\n    void *buffer;\n\n    int res = memkind_posix_memalign(kind, &buffer, 8, size);\n\n    ASSERT_TRUE(buffer != NULL);\n    ASSERT_EQ(0, res);\n    EXPECT_EQ(1, decorators_state->posix_memalign_pre);\n    EXPECT_EQ(1, decorators_state->posix_memalign_post);\n\n    memkind_free(0, buffer);\n#endif\n}\n\nTEST_F(DecoratorTest, test_TC_MEMKIND_DT_realloc)\n{\n#ifdef MEMKIND_DECORATION_ENABLED\n    void *buffer = memkind_realloc(kind, NULL, size);\n\n    ASSERT_TRUE(buffer != NULL);\n    EXPECT_EQ(1, decorators_state->realloc_pre);\n    EXPECT_EQ(1, decorators_state->realloc_post);\n\n    memkind_free(0, buffer);\n#endif\n}\n\nTEST_F(DecoratorTest, test_TC_MEMKIND_DT_free)\n{\n#ifdef MEMKIND_DECORATION_ENABLED\n    void *buffer = memkind_malloc(kind, size);\n\n    ASSERT_TRUE(buffer != NULL);\n\n    memkind_free(0, buffer);\n\n    EXPECT_EQ(1, decorators_state->free_pre);\n    EXPECT_EQ(1, decorators_state->free_post);\n#endif\n}"
  },
  {
    "path": "deps/memkind/src/test/decorator_test.h",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <stdlib.h>\n#include <stdio.h>\n#include <memkind.h>\n\nstruct decorators_flags {\n    int malloc_pre;\n    int malloc_post;\n    int calloc_pre;\n    int calloc_post;\n    int posix_memalign_pre;\n    int posix_memalign_post;\n    int realloc_pre;\n    int realloc_post;\n    int free_pre;\n    int free_post;\n};\n\nstruct decorators_flags *decorators_state;\n\nextern \"C\" {\n\n    void memkind_malloc_pre(struct memkind *kind, size_t size)\n    {\n        decorators_state->malloc_pre++;\n    }\n\n    void memkind_malloc_post(struct memkind *kind, size_t size, void **result)\n    {\n        decorators_state->malloc_post++;\n    }\n\n    void memkind_calloc_pre(struct memkind *kind, size_t nmemb, size_t size)\n    {\n        decorators_state->calloc_pre++;\n    }\n\n    void memkind_calloc_post(struct memkind *kind, size_t nmemb, size_t size,\n                             void **result)\n    {\n        decorators_state->calloc_post++;\n    }\n\n    void memkind_posix_memalign_pre(struct memkind *kind, void **memptr,\n                                    size_t alignment, size_t size)\n    {\n        decorators_state->posix_memalign_pre++;\n    }\n\n    void memkind_posix_memalign_post(struct memkind *kind, void **memptr,\n                                     size_t alignment, size_t size, int *err)\n    {\n        decorators_state->posix_memalign_post++;\n    }\n\n    void memkind_realloc_pre(struct memkind *kind, void *ptr, size_t size)\n    {\n        decorators_state->realloc_pre++;\n    }\n\n    void memkind_realloc_post(struct memkind *kind, void *ptr, size_t size,\n                              void **result)\n    {\n        decorators_state->realloc_post++;\n    }\n\n    void memkind_free_pre(struct memkind **kind, void **ptr)\n    {\n        decorators_state->free_pre++;\n    }\n\n    void memkind_free_post(struct memkind **kind, void **ptr)\n    {\n        decorators_state->free_post++;\n    }\n\n}\n"
  },
  {
    "path": "deps/memkind/src/test/dlopen_test.cpp",
    "content": "/*\n * Copyright (C) 2017 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#include \"allocator_perf_tool/HugePageOrganizer.hpp\"\n\n#include <dlfcn.h>\n\n#include \"common.h\"\n\nclass DlopenTest: public :: testing::Test\n{\nprotected:\n    DlopenTest()\n    {\n        const char *path = \"/usr/lib64/libmemkind.so\";\n        if (!pathExists(path)) {\n            path = \"/usr/lib/libmemkind.so\";\n        }\n        dlerror();\n        handle = dlopen(path, RTLD_LAZY);\n        assert((handle != NULL && dlerror() == NULL) && \"Couldn't open libmemkind.so\");\n        memkind_malloc = (memkind_malloc_t)dlsym(handle, \"memkind_malloc\");\n        assert(dlerror() == NULL && \"Couldn't get memkind_malloc from memkind library\");\n        memkind_free = (memkind_free_t)dlsym(handle, \"memkind_free\");\n        assert(dlerror() == NULL && \"Couldn't get memkind_free from memkind library\");\n    }\n\n    ~DlopenTest()\n    {\n        dlclose(handle);\n    }\n\n    void test(const char *kind_name, size_t alloc_size)\n    {\n        void **kind_ptr = (void **)dlsym(handle, kind_name);\n        EXPECT_TRUE(dlerror() == NULL) << \"Couldn't get kind from memkind library\";\n        EXPECT_TRUE(kind_ptr != NULL) << \"Kind ptr to memkind library is NULL\";\n\n        void *allocation_ptr = memkind_malloc((*kind_ptr), alloc_size);\n        EXPECT_TRUE(allocation_ptr != NULL) << \"Allocation with memkind_malloc failed\";\n\n        memset(allocation_ptr, 0, alloc_size);\n\n        memkind_free((*kind_ptr), allocation_ptr);\n    }\n\n    bool pathExists(const char *p)\n    {\n        struct stat info;\n        if (0 != stat(p, &info)) {\n            return false;\n        }\n        return true;\n    }\n\nprivate:\n    void *handle;\n    typedef void *(*memkind_malloc_t)(void *, size_t);\n    typedef void (*memkind_free_t)(void *, void *);\n    memkind_malloc_t memkind_malloc;\n    memkind_free_t memkind_free;\n};\n\nTEST_F(DlopenTest, test_TC_MEMKIND_DEFAULT_4194305_bytes)\n{\n    test(\"MEMKIND_DEFAULT\", 4194305);\n}\n\nTEST_F(DlopenTest, test_TC_MEMKIND_HBW_4194305_bytes)\n{\n    test(\"MEMKIND_HBW\", 4194305);\n}\n\nTEST_F(DlopenTest, test_TC_MEMKIND_HBW_HUGETLB_4194305_bytes)\n{\n    HugePageOrganizer huge_page_organizer(8);\n    test(\"MEMKIND_HBW_HUGETLB\", 4194305);\n}\n\nTEST_F(DlopenTest, test_TC_MEMKIND_HBW_PREFERRED_4194305_bytes)\n{\n    test(\"MEMKIND_HBW_PREFERRED\", 4194305);\n}\n\nTEST_F(DlopenTest, test_TC_MEMKIND_HBW_INTERLEAVE_4194305_bytes)\n{\n    test(\"MEMKIND_HBW_INTERLEAVE\", 4194305);\n}\n"
  },
  {
    "path": "deps/memkind/src/test/draw_plots.py",
    "content": "#\n#  Copyright (C) 2016 - 2018 Intel Corporation.\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#  1. Redistributions of source code must retain the above copyright notice(s),\n#     this list of conditions and the following disclaimer.\n#  2. Redistributions in binary form must reproduce the above copyright notice(s),\n#     this list of conditions and the following disclaimer in the documentation\n#     and/or other materials provided with the distribution.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n#  EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n#  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n#\n\nfrom mpl_toolkits.mplot3d import Axes3D\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport numpy.ma as ma\nimport os\nfrom shutil import rmtree\n\nfiles = ('alloctest_hbw.txt', 'alloctest_glibc.txt', 'alloctest_tbb.txt', 'alloctest_pmem.txt')\nlegend = ('avg hbw', 'avg glibc', 'avg tbb', 'avg pmem', 'first operation')\ncolors = ('red', 'green', 'blue', 'black')\nfirst_operation_color = 'yellow'\n\nthreads_values = ('1', '2', '4', '8', '16', '18', '36')\n\nsmall_sizes_values = ('1', '4', '16')\nmedium_sizes_values = ('64', '256')\nbig_sizes_values = ('1024', '4096', '16384')\nsizes_values = (small_sizes_values, medium_sizes_values, big_sizes_values)\nsizes_display = ('small', 'medium', 'big')\n\noperations = ('Allocation', 'Free', 'Total')\n\niterations = 1000\noutput_directory = './plots/'\n\nthreads_index      = np.arange(len(threads_values))\nsmall_sizes_index  = np.arange(len(small_sizes_values))\nmedium_sizes_index = np.arange(len(medium_sizes_values))\nbig_sizes_index    = np.arange(len(big_sizes_values))\nsizes_index = (small_sizes_index, medium_sizes_index, big_sizes_index)\n\n# 3D and 2D plots needs different width, so that bars do not cover each other in 3D\nbar_width_2D = 0.2\nbar_width_3D = 0.1\n\n# return times (total, allocation, free, first allocation, first free) as columns 2 - 6\ndef return_times(entry):\n    return entry[:,2], entry[:,3], entry[:,4], entry[:,5], entry[:,6]\n\n# initialize axis with labels, ticks and values\ndef init_axis(fig, suptitle, subplot_projection, x_label, x_ticks, x_values, y_label, y_ticks, y_values, z_label):\n    assert fig is not None\n    fig.clf()\n    fig.suptitle(suptitle)\n    if subplot_projection:\n        ax = fig.add_subplot(111, projection=subplot_projection)\n    else:\n        ax = fig.add_subplot(111)\n    assert ax is not None\n    if x_label:\n        ax.set_xlabel(x_label)\n        if x_ticks is not None and x_values is not None:\n            ax.set_xticks(x_ticks)\n            ax.set_xticklabels(x_values)\n    if y_label:\n        ax.set_ylabel(y_label)\n        if y_ticks is not None and y_values is not None:\n            ax.set_yticks(y_ticks)\n            ax.set_yticklabels(y_values)\n    if z_label:\n        ax.set_zlabel(z_label)\n    return ax\n\ndef draw_bar(ax, show_first_operation, x, y, time, init_time, draw_color):\n    assert ax is not None\n    if y is None:\n        if show_first_operation is True:\n            mask_time = ma.where(time>=init_time)\n            mask_init_time = ma.where(init_time>=time)\n            ax.bar(x[mask_time], time[mask_time], width=bar_width, color=draw_color)\n            ax.bar(x, init_time, width=bar_width, color=first_operation_color)\n            ax.bar(x[mask_init_time], time[mask_init_time], width=bar_width, color=draw_color)\n        else:\n            ax.bar(x, time, width=bar_width, color=draw_color)\n    else:\n        if show_first_operation is True:\n            mask_time = ma.where(time>=init_time)\n            mask_init_time = ma.where(init_time>=time)\n            ax.bar(x[mask_time], (time - init_time)[mask_time], y[mask_time], bottom=init_time[mask_time], zdir='y', width=bar_width, color=draw_color)\n            ax.bar(x[mask_init_time], (init_time - time)[mask_init_time], y[mask_init_time], bottom=time[mask_init_time], zdir='y', width=bar_width, color=first_operation_color)\n            ax.bar(x[mask_init_time], time[mask_init_time], y[mask_init_time], zdir='y', width=bar_width, color=draw_color)\n            ax.bar(x[mask_time], init_time[mask_time], y[mask_time], zdir='y', width=bar_width, color=first_operation_color)\n        else:\n            ax.bar(x, time, y, zdir='y', width=bar_width, color=draw_color)\n\ndef save_plot(show_first_operation, subfolder, filename):\n    if show_first_operation:\n        path = os.path.join(output_directory, \"with_first_operation\")\n    else:\n        path = os.path.join(output_directory, \"without_first_operation\")\n    if subfolder:\n        path = os.path.join(path, subfolder)\n    if not os.path.exists(path):\n        os.mkdir(path)\n    plt.savefig(os.path.join(path, filename))\n    print \"Saved file %s\" % filename\n\ndef load_data_from_files():\n    data = []\n    # load all files into data\n    for f in files:\n        assert os.path.isfile(f) is True\n        data.append(np.loadtxt(f, comments='#'))\n    return data\n\ndef set_bar_width_and_offsets(requested_width):\n    bar_width = requested_width\n    offsets = (0, bar_width, 2*bar_width, 3*bar_width)\n    return bar_width, offsets\n\nif os.path.exists(output_directory):\n    rmtree(output_directory)\nos.mkdir(output_directory)\n\ndata = load_data_from_files()\n\nfig = plt.figure()\n\n########################################\n# Draw 3D plots (time, sizes, threads) #\n########################################\n\nbar_width, offsets = set_bar_width_and_offsets(bar_width_3D)\n\n# for each size range (small, medium, big)\nfor size_values, size_index, size_display in zip(sizes_values, sizes_index, sizes_display):\n    # show plots with and without first operation\n    for show_first_operation in (True, False):\n        # for each operation (allocation, free, total)\n        for operation in operations:\n            # add bar_width to each element of size_index\n            ax = init_axis(fig, \"%s time of %s sizes (%s iterations)\" % (operation, size_display, iterations), '3d',\n                           'size [kB]', size_index + (bar_width,) * len(size_index), size_values,\n                           'threads', threads_index, threads_values,\n                           'time [ms]')\n            legend_data = []\n            # for each allocator (hbw, glibc, tbb, pmem)\n            for entry, offset, draw_color in zip(data, offsets, colors):\n                # remove all rows where column 1 (size) is not in size_values (current size range)\n                entry = entry[np.in1d(entry[:,1], np.array(size_values).astype(np.int))]\n                # convert column 0 (threads values) to thread index\n                threads_col = np.array([threads_values.index(str(n)) for n in map(int, entry[:,0])])\n                # convert column 1 (sizes values) to size index\n                size_col = [size_values.index(str(n)) for n in map(int, entry[:,1])]\n                # add offset to size index so that bars display near each other\n                size_col = np.array(size_col) + offset\n                total_time_col, alloc_time_col, free_time_col, first_alloc_time_col, first_free_time_col = return_times(entry)\n                if operation == 'Allocation':\n                    draw_bar(ax, show_first_operation, size_col, threads_col, alloc_time_col, first_alloc_time_col, draw_color)\n                elif operation == 'Free':\n                    draw_bar(ax, show_first_operation, size_col, threads_col, free_time_col, first_free_time_col, draw_color)\n                elif operation == 'Total':\n                    draw_bar(ax, show_first_operation, size_col, threads_col, total_time_col, first_alloc_time_col+first_free_time_col, draw_color)\n                legend_data.append(plt.Rectangle((0, 0), 1, 1, fc=draw_color))\n            if show_first_operation is True:\n                legend_data.append(plt.Rectangle((0, 0), 1, 1, fc=first_operation_color))\n            ax.legend(legend_data,legend,loc='best')\n            plt.grid()\n            save_plot(show_first_operation, None, \"%s_time_of_%s_sizes_%s_iterations.png\" % (operation, size_display, iterations))\n\n################################################\n# Draw 2D plots (time, sizes, constant thread) #\n################################################\n\nbar_width, offsets = set_bar_width_and_offsets(bar_width_2D)\n\n# for each size range (small, medium, big)\nfor size_values, size_index, size_display in zip(sizes_values, sizes_index, sizes_display):\n    # show plots with and without first operation\n    for show_first_operation in (True, False):\n        for thread in threads_values:\n            # for each operation (allocation, free, total)\n            for operation in operations:\n                # add bar_width to each element of size_index\n                ax = init_axis(fig, \"%s time of %s sizes with %s threads (%s operations)\" % (operation, size_display, thread, iterations), None,\n                               'size [kB]', size_index + (bar_width,) * len(size_index), size_values,\n                               'time [ms]', None, None,\n                               None)\n                legend_data = []\n                # for each allocator (hbw, glibc, tbb, pmem)\n                for entry, offset, draw_color in zip(data, offsets, colors):\n                    # remove all rows where column 1 (size) is not in size_values (current size range)\n                    entry = entry[np.in1d(entry[:,1], np.array(size_values).astype(np.int))]\n                    # remove all rows where column 0 (threads) is not equal to currently analyzed thread value\n                    entry = entry[entry[:,0] == int(thread)]\n                    # convert column 0 (threads values) to thread index\n                    threads_col = np.array([threads_values.index(str(n)) for n in map(int, entry[:,0])])\n                    # convert column 1 (size values) to size index\n                    size_col = [size_values.index(str(n)) for n in map(int, entry[:,1])]\n                    # add offset to size index so that bars display near each other\n                    size_col = np.array(size_col) + offset\n                    total_time_col, alloc_time_col, free_time_col, first_alloc_time_col, first_free_time_col = return_times(entry)\n                    if operation == 'Allocation':\n                        draw_bar(ax, show_first_operation, size_col, None, alloc_time_col, first_alloc_time_col, draw_color)\n                    elif operation == 'Free':\n                        draw_bar(ax, show_first_operation, size_col, None, free_time_col, first_free_time_col, draw_color)\n                    elif operation == 'Total':\n                        draw_bar(ax, show_first_operation, size_col, None, total_time_col, first_alloc_time_col+first_free_time_col, draw_color)\n                    legend_data.append(plt.Rectangle((0, 0), 1, 1, fc=draw_color))\n                if show_first_operation is True:\n                    legend_data.append(plt.Rectangle((0, 0), 1, 1, fc=first_operation_color))\n                ax.legend(legend_data,legend,loc='best')\n                plt.grid()\n                save_plot(show_first_operation, \"constant_thread\", \"%s_time_of_%s_sizes_with_%s_threads_%s_iterations.png\" % (operation, size_display, thread, iterations))\n\n################################################\n# Draw 2D plots (time, threads, constant size) #\n################################################\n\n# for each size range (small, medium, big)\nfor size_values, size_index, size_display in zip(sizes_values, sizes_index, sizes_display):\n    # show plots with and without first operation\n    for show_first_operation in (True, False):\n        for size in size_values:\n            # for each operation (allocation, free, total)\n            for operation in operations:\n                # add bar_width to each element of threads_index\n                ax = init_axis(fig, \"%s time of %s kB (%s operations)\" % (operation, size, iterations), None,\n                               'threads', threads_index + (bar_width,) * len(threads_index), threads_values,\n                               'time [ms]', None, None,\n                               None)\n                legend_data = []\n                # for each allocator (hbw, glibc, tbb, pmem)\n                for entry, offset, draw_color in zip(data, offsets, colors):\n                    # remove all rows where column 1 (size) is not in size_values (current size range)\n                    entry = entry[np.in1d(entry[:,1], np.array(size_values).astype(np.int))]\n                    # remove all rows where column 1 (size) is not equal to currently analyzed size value\n                    entry = entry[entry[:,1] == int(size)]\n                    # convert column 0 (threads values) to thread index\n                    threads_col = np.array([threads_values.index(str(n)) for n in map(int, entry[:,0])])\n                    # add offset to thread index so that bars display near each other\n                    threads_col = np.array(threads_col) + offset\n                    # convert column 1 (size values) to size index\n                    size_col = [size_values.index(str(n)) for n in map(int, entry[:,1])]\n                    total_time_col, alloc_time_col, free_time_col, first_alloc_time_col, first_free_time_col = return_times(entry)\n                    if operation == 'Allocation':\n                        draw_bar(ax, show_first_operation, threads_col, None, alloc_time_col, first_alloc_time_col, draw_color)\n                    elif operation == 'Free':\n                        draw_bar(ax, show_first_operation, threads_col, None, free_time_col, first_free_time_col, draw_color)\n                    elif operation == 'Total':\n                        draw_bar(ax, show_first_operation, threads_col, None, total_time_col, first_alloc_time_col+first_free_time_col, draw_color)\n                    legend_data.append(plt.Rectangle((0, 0), 1, 1, fc=draw_color))\n                if show_first_operation is True:\n                    legend_data.append(plt.Rectangle((0, 0), 1, 1, fc=first_operation_color))\n                ax.legend(legend_data,legend,loc='best')\n                plt.grid()\n                save_plot(show_first_operation, \"constant_size\", \"%s_time_of_%s_kB_%s_operations.png\" % (operation, size, iterations))\n"
  },
  {
    "path": "deps/memkind/src/test/environ_err_hbw_malloc_test.cpp",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <hbwmalloc.h>\n#include <memkind.h>\n#include \"memkind/internal/memkind_hbw.h\"\n\n#include <numa.h>\n#include <numaif.h>\n\n#include \"common.h\"\n\n/* This test is run with overridden MEMKIND_HBW_NODES environment variable\n * and tries to perform allocation from DRAM using hbw_malloc() using\n * default HBW_POLICY_PREFERRED policy.\n */\nint main()\n{\n    struct bitmask *expected_nodemask = NULL;\n    struct bitmask *returned_nodemask = NULL;\n    void *ptr = NULL;\n    int ret = 0;\n    int status = 0;\n\n    ptr = hbw_malloc(KB);\n    if (ptr == NULL) {\n        printf(\"Error: allocation failed\\n\");\n        goto exit;\n    }\n\n    expected_nodemask = numa_allocate_nodemask();\n    status = memkind_hbw_all_get_mbind_nodemask(NULL, expected_nodemask->maskp,\n                                                expected_nodemask->size);\n    if (status != MEMKIND_ERROR_ENVIRON) {\n        printf(\"Error: wrong return value from memkind_hbw_all_get_mbind_nodemask()\\n\");\n        printf(\"Expected: %d\\n\", MEMKIND_ERROR_ENVIRON);\n        printf(\"Actual: %d\\n\", status);\n        goto exit;\n    }\n\n    returned_nodemask = numa_allocate_nodemask();\n    status = get_mempolicy(NULL, returned_nodemask->maskp, returned_nodemask->size,\n                           ptr, MPOL_F_ADDR);\n    if (status) {\n        printf(\"Error: get_mempolicy() returned %d\\n\", status);\n        goto exit;\n    }\n\n    ret = numa_bitmask_equal(returned_nodemask, expected_nodemask);\n    if (!ret) {\n        printf(\"Error: Memkind hbw and allocated pointer nodemasks are not equal\\n\");\n    }\n\nexit:\n    if (expected_nodemask) {\n        numa_free_nodemask(expected_nodemask);\n    }\n    if (returned_nodemask) {\n        numa_free_nodemask(returned_nodemask);\n    }\n    if (ptr) {\n        hbw_free(ptr);\n    }\n\n    return ret;\n}\n"
  },
  {
    "path": "deps/memkind/src/test/error_message_tests.cpp",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind.h>\n\n#include <errno.h>\n#include <gtest/gtest.h>\n\n/* Tests which calls APIS in wrong ways to generate Error Messages thrown by the\n * the memkind library\n */\n\nconst int all_error_code[] = {\n    MEMKIND_ERROR_UNAVAILABLE,\n    MEMKIND_ERROR_MBIND,\n    MEMKIND_ERROR_MMAP,\n    MEMKIND_ERROR_MALLOC,\n    MEMKIND_ERROR_RUNTIME,\n    MEMKIND_ERROR_ENVIRON,\n    MEMKIND_ERROR_INVALID,\n    MEMKIND_ERROR_TOOMANY,\n    MEMKIND_ERROR_BADOPS,\n    MEMKIND_ERROR_HUGETLB,\n    MEMKIND_ERROR_ARENAS_CREATE,\n    MEMKIND_ERROR_OPERATION_FAILED,\n    MEMKIND_ERROR_MEMTYPE_NOT_AVAILABLE,\n    EINVAL,\n    ENOMEM\n};\n\nclass ErrorMessage: public :: testing :: Test\n{\nprotected:\n\n    void SetUp()\n    {\n    }\n    void TearDown()\n    {\n    }\n};\n\nTEST_F(ErrorMessage, test_TC_MEMKIND_ErrorMsgLength)\n{\n    size_t i;\n    char error_message[MEMKIND_ERROR_MESSAGE_SIZE];\n    for (i = 0; i < sizeof(all_error_code)/sizeof(all_error_code[0]); ++i) {\n        memkind_error_message(all_error_code[i], error_message,\n                              MEMKIND_ERROR_MESSAGE_SIZE);\n        EXPECT_TRUE(strlen(error_message) < MEMKIND_ERROR_MESSAGE_SIZE - 1);\n    }\n    memkind_error_message(MEMKIND_ERROR_UNAVAILABLE, NULL, 0);\n}\n\nTEST_F(ErrorMessage, test_TC_MEMKIND_ErrorMsgFormat)\n{\n    size_t i;\n    char error_message[MEMKIND_ERROR_MESSAGE_SIZE];\n    for (i = 0; i < sizeof(all_error_code)/sizeof(all_error_code[0]); ++i) {\n        memkind_error_message(all_error_code[i], error_message,\n                              MEMKIND_ERROR_MESSAGE_SIZE);\n        EXPECT_TRUE(strncmp(error_message, \"<memkind>\", strlen(\"<memkind>\")) == 0);\n    }\n}\n\nTEST_F(ErrorMessage, test_TC_MEMKIND_ErrorMsgUndefMesg)\n{\n    char error_message[MEMKIND_ERROR_MESSAGE_SIZE];\n    memkind_error_message(-0xdeadbeef, error_message, MEMKIND_ERROR_MESSAGE_SIZE);\n    EXPECT_TRUE(strncmp(error_message, \"<memkind> Undefined error number:\",\n                        strlen(\"<memkind> Undefined error number:\")) == 0);\n}\n\n"
  },
  {
    "path": "deps/memkind/src/test/freeing_memory_segfault_test.cpp",
    "content": "/*\n* Copyright (C) 2017 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n\n#include \"memkind.h\"\n#include \"common.h\"\n#include <thread>\n\n//This test reproduce segfault when using TBB library.\nclass FreeingMemorySegfault: public :: testing::Test\n{\nprotected:\n    void SetUp() {}\n    void TearDown() {}\n};\n\nTEST_F(FreeingMemorySegfault,\n       test_TC_MEMKIND_freeing_memory_after_thread_finish)\n{\n    void *ptr = nullptr;\n\n    std::thread t([&] {\n        ptr = memkind_malloc(MEMKIND_DEFAULT, 32);\n        ASSERT_TRUE(ptr != NULL);\n    });\n    t.join();\n\n    memkind_free(0, ptr);\n    SUCCEED();\n}\n"
  },
  {
    "path": "deps/memkind/src/test/gb_page_tests_bind_policy.cpp",
    "content": "/*\n * Copyright (C) 2016 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"memkind.h\"\n#include \"hbwmalloc.h\"\n#include \"common.h\"\n#include \"trial_generator.h\"\n#include \"allocator_perf_tool/HugePageOrganizer.hpp\"\n\n/* This set of tests are intended to test HBW_PAGESIZE_1GB and\n * HBW_PAGESIZE_1GB_STRICT flags.\n */\nclass GBPagesTestBindPolicy : public TGTest\n{\nprotected:\n    void run(int iterations, size_t alignment, bool psize_strict)\n    {\n        std::vector<void *> addr_to_free;\n        hbw_set_policy(HBW_POLICY_BIND);\n        EXPECT_EQ(HBW_POLICY_BIND, hbw_get_policy());\n\n        for (int i = 0; i < iterations; i++) {\n            void *ptr = NULL;\n            int ret;\n            if (psize_strict) {\n                ret = hbw_posix_memalign_psize(&ptr, alignment, GB, HBW_PAGESIZE_1GB_STRICT);\n            } else {\n                ret = hbw_posix_memalign_psize(&ptr, alignment, GB, HBW_PAGESIZE_1GB);\n            }\n            ASSERT_EQ(0, ret);\n            ASSERT_FALSE(ptr == NULL);\n            ASSERT_EQ(0, hbw_verify_memory_region(ptr, GB, HBW_TOUCH_PAGES));\n            addr_to_free.push_back(ptr);\n        }\n\n        for (int i = 0; i < iterations; i++) {\n            hbw_free(addr_to_free[i]);\n        }\n    }\n\nprivate:\n    HugePageOrganizer huge_page_organizer = HugePageOrganizer(2250);\n};\n\nTEST_F(GBPagesTestBindPolicy,\n       test_TC_MEMKIND_GBPages_ext_HBW_Misalign_Preferred_Bind_Strict_1GB)\n{\n    run(1, 2*GB, true);\n}\n\nTEST_F(GBPagesTestBindPolicy,\n       test_TC_MEMKIND_GBPages_ext_HBW_Memalign_Psize_Bind_1GB)\n{\n    run(1, GB, true);\n}\n\n/*\n * Below tests allocate GB pages incrementally.\n*/\n\nTEST_F(GBPagesTestBindPolicy,\n       test_TC_MEMKIND_GBPages_ext_HBW_Memalign_Psize_Bind_2GB)\n{\n    run(2, GB, false);\n}\n\nTEST_F(GBPagesTestBindPolicy,\n       test_TC_MEMKIND_GBPages_ext_HBW_Memalign_Psize_Strict_Bind_3GB)\n{\n    run(3, GB, true);\n}\n"
  },
  {
    "path": "deps/memkind/src/test/get_arena_test.cpp",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind/internal/memkind_arena.h>\n\n#include <algorithm>\n#include <vector>\n#include <gtest/gtest.h>\n#include <omp.h>\n#include <pthread.h>\n\nclass GetArenaTest: public :: testing::Test\n{\n\nprotected:\n    void SetUp()\n    {}\n\n    void TearDown()\n    {}\n\n};\n\nbool uint_comp(unsigned int a, unsigned int b)\n{\n    return (a < b);\n}\n\nTEST_F(GetArenaTest, test_TC_MEMKIND_ThreadHash)\n{\n    int num_threads = omp_get_max_threads();\n    std::vector<unsigned int> arena_idx(num_threads);\n    unsigned int thread_idx, idx;\n    int err = 0;\n    size_t size = 0;\n    int i;\n    unsigned max_collisions, collisions;\n    const unsigned collisions_limit = 5;\n\n    //Initialize kind\n    memkind_malloc(MEMKIND_HBW, 0);\n\n    #pragma omp parallel shared(arena_idx) private(thread_idx)\n    {\n        thread_idx = omp_get_thread_num();\n        err = memkind_thread_get_arena(MEMKIND_HBW, &(arena_idx[thread_idx]), size);\n    }\n    ASSERT_TRUE(err == 0);\n    std::sort(arena_idx.begin(), arena_idx.end(), uint_comp);\n    idx = arena_idx[0];\n    collisions = 0;\n    max_collisions = 0;\n    for (i = 1; i < num_threads; ++i) {\n        if (arena_idx[i] == idx) {\n            collisions++;\n        } else {\n            if (collisions > max_collisions) {\n                max_collisions = collisions;\n            }\n            idx = arena_idx[i];\n            collisions = 0;\n        }\n    }\n    EXPECT_LE(max_collisions, collisions_limit);\n    RecordProperty(\"max_collisions\", max_collisions);\n}\n"
  },
  {
    "path": "deps/memkind/src/test/gtest_fused/gtest/gtest-all.cc",
    "content": "// Copyright 2008, Google Inc.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Author: mheule@google.com (Markus Heule)\n//\n// Google C++ Testing Framework (Google Test)\n//\n// Sometimes it's desirable to build Google Test by compiling a single file.\n// This file serves this purpose.\n\n// This line ensures that gtest.h can be compiled on its own, even\n// when it's fused.\n#include \"gtest/gtest.h\"\n\n// The following lines pull in the real gtest *.cc files.\n// Copyright 2005, Google Inc.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Author: wan@google.com (Zhanyong Wan)\n//\n// The Google C++ Testing Framework (Google Test)\n\n// Copyright 2007, Google Inc.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Author: wan@google.com (Zhanyong Wan)\n//\n// Utilities for testing Google Test itself and code that uses Google Test\n// (e.g. frameworks built on top of Google Test).\n\n#ifndef GTEST_INCLUDE_GTEST_GTEST_SPI_H_\n#define GTEST_INCLUDE_GTEST_GTEST_SPI_H_\n\n\nnamespace testing {\n\n// This helper class can be used to mock out Google Test failure reporting\n// so that we can test Google Test or code that builds on Google Test.\n//\n// An object of this class appends a TestPartResult object to the\n// TestPartResultArray object given in the constructor whenever a Google Test\n// failure is reported. It can either intercept only failures that are\n// generated in the same thread that created this object or it can intercept\n// all generated failures. The scope of this mock object can be controlled with\n// the second argument to the two arguments constructor.\nclass GTEST_API_ ScopedFakeTestPartResultReporter\n    : public TestPartResultReporterInterface {\n public:\n  // The two possible mocking modes of this object.\n  enum InterceptMode {\n    INTERCEPT_ONLY_CURRENT_THREAD,  // Intercepts only thread local failures.\n    INTERCEPT_ALL_THREADS           // Intercepts all failures.\n  };\n\n  // The c'tor sets this object as the test part result reporter used\n  // by Google Test.  The 'result' parameter specifies where to report the\n  // results. This reporter will only catch failures generated in the current\n  // thread. DEPRECATED\n  explicit ScopedFakeTestPartResultReporter(TestPartResultArray* result);\n\n  // Same as above, but you can choose the interception scope of this object.\n  ScopedFakeTestPartResultReporter(InterceptMode intercept_mode,\n                                   TestPartResultArray* result);\n\n  // The d'tor restores the previous test part result reporter.\n  virtual ~ScopedFakeTestPartResultReporter();\n\n  // Appends the TestPartResult object to the TestPartResultArray\n  // received in the constructor.\n  //\n  // This method is from the TestPartResultReporterInterface\n  // interface.\n  virtual void ReportTestPartResult(const TestPartResult& result);\n private:\n  void Init();\n\n  const InterceptMode intercept_mode_;\n  TestPartResultReporterInterface* old_reporter_;\n  TestPartResultArray* const result_;\n\n  GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedFakeTestPartResultReporter);\n};\n\nnamespace internal {\n\n// A helper class for implementing EXPECT_FATAL_FAILURE() and\n// EXPECT_NONFATAL_FAILURE().  Its destructor verifies that the given\n// TestPartResultArray contains exactly one failure that has the given\n// type and contains the given substring.  If that's not the case, a\n// non-fatal failure will be generated.\nclass GTEST_API_ SingleFailureChecker {\n public:\n  // The constructor remembers the arguments.\n  SingleFailureChecker(const TestPartResultArray* results,\n                       TestPartResult::Type type, const std::string& substr);\n  ~SingleFailureChecker();\n private:\n  const TestPartResultArray* const results_;\n  const TestPartResult::Type type_;\n  const std::string substr_;\n\n  GTEST_DISALLOW_COPY_AND_ASSIGN_(SingleFailureChecker);\n};\n\n}  // namespace internal\n\n}  // namespace testing\n\n// A set of macros for testing Google Test assertions or code that's expected\n// to generate Google Test fatal failures.  It verifies that the given\n// statement will cause exactly one fatal Google Test failure with 'substr'\n// being part of the failure message.\n//\n// There are two different versions of this macro. EXPECT_FATAL_FAILURE only\n// affects and considers failures generated in the current thread and\n// EXPECT_FATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.\n//\n// The verification of the assertion is done correctly even when the statement\n// throws an exception or aborts the current function.\n//\n// Known restrictions:\n//   - 'statement' cannot reference local non-static variables or\n//     non-static members of the current object.\n//   - 'statement' cannot return a value.\n//   - You cannot stream a failure message to this macro.\n//\n// Note that even though the implementations of the following two\n// macros are much alike, we cannot refactor them to use a common\n// helper macro, due to some peculiarity in how the preprocessor\n// works.  The AcceptsMacroThatExpandsToUnprotectedComma test in\n// gtest_unittest.cc will fail to compile if we do that.\n#define EXPECT_FATAL_FAILURE(statement, substr) \\\n  do { \\\n    class GTestExpectFatalFailureHelper {\\\n     public:\\\n      static void Execute() { statement; }\\\n    };\\\n    ::testing::TestPartResultArray gtest_failures;\\\n    ::testing::internal::SingleFailureChecker gtest_checker(\\\n        &gtest_failures, ::testing::TestPartResult::kFatalFailure, (substr));\\\n    {\\\n      ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\\\n          ::testing::ScopedFakeTestPartResultReporter:: \\\n          INTERCEPT_ONLY_CURRENT_THREAD, &gtest_failures);\\\n      GTestExpectFatalFailureHelper::Execute();\\\n    }\\\n  } while (::testing::internal::AlwaysFalse())\n\n#define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr) \\\n  do { \\\n    class GTestExpectFatalFailureHelper {\\\n     public:\\\n      static void Execute() { statement; }\\\n    };\\\n    ::testing::TestPartResultArray gtest_failures;\\\n    ::testing::internal::SingleFailureChecker gtest_checker(\\\n        &gtest_failures, ::testing::TestPartResult::kFatalFailure, (substr));\\\n    {\\\n      ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\\\n          ::testing::ScopedFakeTestPartResultReporter:: \\\n          INTERCEPT_ALL_THREADS, &gtest_failures);\\\n      GTestExpectFatalFailureHelper::Execute();\\\n    }\\\n  } while (::testing::internal::AlwaysFalse())\n\n// A macro for testing Google Test assertions or code that's expected to\n// generate Google Test non-fatal failures.  It asserts that the given\n// statement will cause exactly one non-fatal Google Test failure with 'substr'\n// being part of the failure message.\n//\n// There are two different versions of this macro. EXPECT_NONFATAL_FAILURE only\n// affects and considers failures generated in the current thread and\n// EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.\n//\n// 'statement' is allowed to reference local variables and members of\n// the current object.\n//\n// The verification of the assertion is done correctly even when the statement\n// throws an exception or aborts the current function.\n//\n// Known restrictions:\n//   - You cannot stream a failure message to this macro.\n//\n// Note that even though the implementations of the following two\n// macros are much alike, we cannot refactor them to use a common\n// helper macro, due to some peculiarity in how the preprocessor\n// works.  If we do that, the code won't compile when the user gives\n// EXPECT_NONFATAL_FAILURE() a statement that contains a macro that\n// expands to code containing an unprotected comma.  The\n// AcceptsMacroThatExpandsToUnprotectedComma test in gtest_unittest.cc\n// catches that.\n//\n// For the same reason, we have to write\n//   if (::testing::internal::AlwaysTrue()) { statement; }\n// instead of\n//   GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)\n// to avoid an MSVC warning on unreachable code.\n#define EXPECT_NONFATAL_FAILURE(statement, substr) \\\n  do {\\\n    ::testing::TestPartResultArray gtest_failures;\\\n    ::testing::internal::SingleFailureChecker gtest_checker(\\\n        &gtest_failures, ::testing::TestPartResult::kNonFatalFailure, \\\n        (substr));\\\n    {\\\n      ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\\\n          ::testing::ScopedFakeTestPartResultReporter:: \\\n          INTERCEPT_ONLY_CURRENT_THREAD, &gtest_failures);\\\n      if (::testing::internal::AlwaysTrue()) { statement; }\\\n    }\\\n  } while (::testing::internal::AlwaysFalse())\n\n#define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr) \\\n  do {\\\n    ::testing::TestPartResultArray gtest_failures;\\\n    ::testing::internal::SingleFailureChecker gtest_checker(\\\n        &gtest_failures, ::testing::TestPartResult::kNonFatalFailure, \\\n        (substr));\\\n    {\\\n      ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\\\n          ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \\\n          &gtest_failures);\\\n      if (::testing::internal::AlwaysTrue()) { statement; }\\\n    }\\\n  } while (::testing::internal::AlwaysFalse())\n\n#endif  // GTEST_INCLUDE_GTEST_GTEST_SPI_H_\n\n#include <ctype.h>\n#include <math.h>\n#include <stdarg.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <time.h>\n#include <wchar.h>\n#include <wctype.h>\n\n#include <algorithm>\n#include <iomanip>\n#include <limits>\n#include <list>\n#include <map>\n#include <ostream>  // NOLINT\n#include <sstream>\n#include <vector>\n\n#if GTEST_OS_LINUX\n\n// TODO(kenton@google.com): Use autoconf to detect availability of\n// gettimeofday().\n# define GTEST_HAS_GETTIMEOFDAY_ 1\n\n# include <fcntl.h>  // NOLINT\n# include <limits.h>  // NOLINT\n# include <sched.h>  // NOLINT\n// Declares vsnprintf().  This header is not available on Windows.\n# include <strings.h>  // NOLINT\n# include <sys/mman.h>  // NOLINT\n# include <sys/time.h>  // NOLINT\n# include <unistd.h>  // NOLINT\n# include <string>\n\n#elif GTEST_OS_SYMBIAN\n# define GTEST_HAS_GETTIMEOFDAY_ 1\n# include <sys/time.h>  // NOLINT\n\n#elif GTEST_OS_ZOS\n# define GTEST_HAS_GETTIMEOFDAY_ 1\n# include <sys/time.h>  // NOLINT\n\n// On z/OS we additionally need strings.h for strcasecmp.\n# include <strings.h>  // NOLINT\n\n#elif GTEST_OS_WINDOWS_MOBILE  // We are on Windows CE.\n\n# include <windows.h>  // NOLINT\n# undef min\n\n#elif GTEST_OS_WINDOWS  // We are on Windows proper.\n\n# include <io.h>  // NOLINT\n# include <sys/timeb.h>  // NOLINT\n# include <sys/types.h>  // NOLINT\n# include <sys/stat.h>  // NOLINT\n\n# if GTEST_OS_WINDOWS_MINGW\n// MinGW has gettimeofday() but not _ftime64().\n// TODO(kenton@google.com): Use autoconf to detect availability of\n//   gettimeofday().\n// TODO(kenton@google.com): There are other ways to get the time on\n//   Windows, like GetTickCount() or GetSystemTimeAsFileTime().  MinGW\n//   supports these.  consider using them instead.\n#  define GTEST_HAS_GETTIMEOFDAY_ 1\n#  include <sys/time.h>  // NOLINT\n# endif  // GTEST_OS_WINDOWS_MINGW\n\n// cpplint thinks that the header is already included, so we want to\n// silence it.\n# include <windows.h>  // NOLINT\n# undef min\n\n#else\n\n// Assume other platforms have gettimeofday().\n// TODO(kenton@google.com): Use autoconf to detect availability of\n//   gettimeofday().\n# define GTEST_HAS_GETTIMEOFDAY_ 1\n\n// cpplint thinks that the header is already included, so we want to\n// silence it.\n# include <sys/time.h>  // NOLINT\n# include <unistd.h>  // NOLINT\n\n#endif  // GTEST_OS_LINUX\n\n#if GTEST_HAS_EXCEPTIONS\n# include <stdexcept>\n#endif\n\n#if GTEST_CAN_STREAM_RESULTS_\n# include <arpa/inet.h>  // NOLINT\n# include <netdb.h>  // NOLINT\n# include <sys/socket.h>  // NOLINT\n# include <sys/types.h>  // NOLINT\n#endif\n\n// Indicates that this translation unit is part of Google Test's\n// implementation.  It must come before gtest-internal-inl.h is\n// included, or there will be a compiler error.  This trick is to\n// prevent a user from accidentally including gtest-internal-inl.h in\n// his code.\n#define GTEST_IMPLEMENTATION_ 1\n// Copyright 2005, Google Inc.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\n// Utility functions and classes used by the Google C++ testing framework.\n//\n// Author: wan@google.com (Zhanyong Wan)\n//\n// This file contains purely Google Test's internal implementation.  Please\n// DO NOT #INCLUDE IT IN A USER PROGRAM.\n\n#ifndef GTEST_SRC_GTEST_INTERNAL_INL_H_\n#define GTEST_SRC_GTEST_INTERNAL_INL_H_\n\n// GTEST_IMPLEMENTATION_ is defined to 1 iff the current translation unit is\n// part of Google Test's implementation; otherwise it's undefined.\n#if !GTEST_IMPLEMENTATION_\n// If this file is included from the user's code, just say no.\n# error \"gtest-internal-inl.h is part of Google Test's internal implementation.\"\n# error \"It must not be included except by Google Test itself.\"\n#endif  // GTEST_IMPLEMENTATION_\n\n#ifndef _WIN32_WCE\n# include <errno.h>\n#endif  // !_WIN32_WCE\n#include <stddef.h>\n#include <stdlib.h>  // For strtoll/_strtoul64/malloc/free.\n#include <string.h>  // For memmove.\n\n#include <algorithm>\n#include <string>\n#include <vector>\n\n\n#if GTEST_CAN_STREAM_RESULTS_\n# include <arpa/inet.h>  // NOLINT\n# include <netdb.h>  // NOLINT\n#endif\n\n#if GTEST_OS_WINDOWS\n# include <windows.h>  // NOLINT\n#endif  // GTEST_OS_WINDOWS\n\n\nnamespace testing {\n\n// Declares the flags.\n//\n// We don't want the users to modify this flag in the code, but want\n// Google Test's own unit tests to be able to access it. Therefore we\n// declare it here as opposed to in gtest.h.\nGTEST_DECLARE_bool_(death_test_use_fork);\n\nnamespace internal {\n\n// The value of GetTestTypeId() as seen from within the Google Test\n// library.  This is solely for testing GetTestTypeId().\nGTEST_API_ extern const TypeId kTestTypeIdInGoogleTest;\n\n// Names of the flags (needed for parsing Google Test flags).\nconst char kAlsoRunDisabledTestsFlag[] = \"also_run_disabled_tests\";\nconst char kBreakOnFailureFlag[] = \"break_on_failure\";\nconst char kCatchExceptionsFlag[] = \"catch_exceptions\";\nconst char kColorFlag[] = \"color\";\nconst char kFilterFlag[] = \"filter\";\nconst char kListTestsFlag[] = \"list_tests\";\nconst char kOutputFlag[] = \"output\";\nconst char kPrintTimeFlag[] = \"print_time\";\nconst char kRandomSeedFlag[] = \"random_seed\";\nconst char kRepeatFlag[] = \"repeat\";\nconst char kShuffleFlag[] = \"shuffle\";\nconst char kStackTraceDepthFlag[] = \"stack_trace_depth\";\nconst char kStreamResultToFlag[] = \"stream_result_to\";\nconst char kThrowOnFailureFlag[] = \"throw_on_failure\";\nconst char kFlagfileFlag[] = \"flagfile\";\n\n// A valid random seed must be in [1, kMaxRandomSeed].\nconst int kMaxRandomSeed = 99999;\n\n// g_help_flag is true iff the --help flag or an equivalent form is\n// specified on the command line.\nGTEST_API_ extern bool g_help_flag;\n\n// Returns the current time in milliseconds.\nGTEST_API_ TimeInMillis GetTimeInMillis();\n\n// Returns true iff Google Test should use colors in the output.\nGTEST_API_ bool ShouldUseColor(bool stdout_is_tty);\n\n// Formats the given time in milliseconds as seconds.\nGTEST_API_ std::string FormatTimeInMillisAsSeconds(TimeInMillis ms);\n\n// Converts the given time in milliseconds to a date string in the ISO 8601\n// format, without the timezone information.  N.B.: due to the use the\n// non-reentrant localtime() function, this function is not thread safe.  Do\n// not use it in any code that can be called from multiple threads.\nGTEST_API_ std::string FormatEpochTimeInMillisAsIso8601(TimeInMillis ms);\n\n// Parses a string for an Int32 flag, in the form of \"--flag=value\".\n//\n// On success, stores the value of the flag in *value, and returns\n// true.  On failure, returns false without changing *value.\nGTEST_API_ bool ParseInt32Flag(\n    const char* str, const char* flag, Int32* value);\n\n// Returns a random seed in range [1, kMaxRandomSeed] based on the\n// given --gtest_random_seed flag value.\ninline int GetRandomSeedFromFlag(Int32 random_seed_flag) {\n  const unsigned int raw_seed = (random_seed_flag == 0) ?\n      static_cast<unsigned int>(GetTimeInMillis()) :\n      static_cast<unsigned int>(random_seed_flag);\n\n  // Normalizes the actual seed to range [1, kMaxRandomSeed] such that\n  // it's easy to type.\n  const int normalized_seed =\n      static_cast<int>((raw_seed - 1U) %\n                       static_cast<unsigned int>(kMaxRandomSeed)) + 1;\n  return normalized_seed;\n}\n\n// Returns the first valid random seed after 'seed'.  The behavior is\n// undefined if 'seed' is invalid.  The seed after kMaxRandomSeed is\n// considered to be 1.\ninline int GetNextRandomSeed(int seed) {\n  GTEST_CHECK_(1 <= seed && seed <= kMaxRandomSeed)\n      << \"Invalid random seed \" << seed << \" - must be in [1, \"\n      << kMaxRandomSeed << \"].\";\n  const int next_seed = seed + 1;\n  return (next_seed > kMaxRandomSeed) ? 1 : next_seed;\n}\n\n// This class saves the values of all Google Test flags in its c'tor, and\n// restores them in its d'tor.\nclass GTestFlagSaver {\n public:\n  // The c'tor.\n  GTestFlagSaver() {\n    also_run_disabled_tests_ = GTEST_FLAG(also_run_disabled_tests);\n    break_on_failure_ = GTEST_FLAG(break_on_failure);\n    catch_exceptions_ = GTEST_FLAG(catch_exceptions);\n    color_ = GTEST_FLAG(color);\n    death_test_style_ = GTEST_FLAG(death_test_style);\n    death_test_use_fork_ = GTEST_FLAG(death_test_use_fork);\n    filter_ = GTEST_FLAG(filter);\n    internal_run_death_test_ = GTEST_FLAG(internal_run_death_test);\n    list_tests_ = GTEST_FLAG(list_tests);\n    output_ = GTEST_FLAG(output);\n    print_time_ = GTEST_FLAG(print_time);\n    random_seed_ = GTEST_FLAG(random_seed);\n    repeat_ = GTEST_FLAG(repeat);\n    shuffle_ = GTEST_FLAG(shuffle);\n    stack_trace_depth_ = GTEST_FLAG(stack_trace_depth);\n    stream_result_to_ = GTEST_FLAG(stream_result_to);\n    throw_on_failure_ = GTEST_FLAG(throw_on_failure);\n  }\n\n  // The d'tor is not virtual.  DO NOT INHERIT FROM THIS CLASS.\n  ~GTestFlagSaver() {\n    GTEST_FLAG(also_run_disabled_tests) = also_run_disabled_tests_;\n    GTEST_FLAG(break_on_failure) = break_on_failure_;\n    GTEST_FLAG(catch_exceptions) = catch_exceptions_;\n    GTEST_FLAG(color) = color_;\n    GTEST_FLAG(death_test_style) = death_test_style_;\n    GTEST_FLAG(death_test_use_fork) = death_test_use_fork_;\n    GTEST_FLAG(filter) = filter_;\n    GTEST_FLAG(internal_run_death_test) = internal_run_death_test_;\n    GTEST_FLAG(list_tests) = list_tests_;\n    GTEST_FLAG(output) = output_;\n    GTEST_FLAG(print_time) = print_time_;\n    GTEST_FLAG(random_seed) = random_seed_;\n    GTEST_FLAG(repeat) = repeat_;\n    GTEST_FLAG(shuffle) = shuffle_;\n    GTEST_FLAG(stack_trace_depth) = stack_trace_depth_;\n    GTEST_FLAG(stream_result_to) = stream_result_to_;\n    GTEST_FLAG(throw_on_failure) = throw_on_failure_;\n  }\n\n private:\n  // Fields for saving the original values of flags.\n  bool also_run_disabled_tests_;\n  bool break_on_failure_;\n  bool catch_exceptions_;\n  std::string color_;\n  std::string death_test_style_;\n  bool death_test_use_fork_;\n  std::string filter_;\n  std::string internal_run_death_test_;\n  bool list_tests_;\n  std::string output_;\n  bool print_time_;\n  internal::Int32 random_seed_;\n  internal::Int32 repeat_;\n  bool shuffle_;\n  internal::Int32 stack_trace_depth_;\n  std::string stream_result_to_;\n  bool throw_on_failure_;\n} GTEST_ATTRIBUTE_UNUSED_;\n\n// Converts a Unicode code point to a narrow string in UTF-8 encoding.\n// code_point parameter is of type UInt32 because wchar_t may not be\n// wide enough to contain a code point.\n// If the code_point is not a valid Unicode code point\n// (i.e. outside of Unicode range U+0 to U+10FFFF) it will be converted\n// to \"(Invalid Unicode 0xXXXXXXXX)\".\nGTEST_API_ std::string CodePointToUtf8(UInt32 code_point);\n\n// Converts a wide string to a narrow string in UTF-8 encoding.\n// The wide string is assumed to have the following encoding:\n//   UTF-16 if sizeof(wchar_t) == 2 (on Windows, Cygwin, Symbian OS)\n//   UTF-32 if sizeof(wchar_t) == 4 (on Linux)\n// Parameter str points to a null-terminated wide string.\n// Parameter num_chars may additionally limit the number\n// of wchar_t characters processed. -1 is used when the entire string\n// should be processed.\n// If the string contains code points that are not valid Unicode code points\n// (i.e. outside of Unicode range U+0 to U+10FFFF) they will be output\n// as '(Invalid Unicode 0xXXXXXXXX)'. If the string is in UTF16 encoding\n// and contains invalid UTF-16 surrogate pairs, values in those pairs\n// will be encoded as individual Unicode characters from Basic Normal Plane.\nGTEST_API_ std::string WideStringToUtf8(const wchar_t* str, int num_chars);\n\n// Reads the GTEST_SHARD_STATUS_FILE environment variable, and creates the file\n// if the variable is present. If a file already exists at this location, this\n// function will write over it. If the variable is present, but the file cannot\n// be created, prints an error and exits.\nvoid WriteToShardStatusFileIfNeeded();\n\n// Checks whether sharding is enabled by examining the relevant\n// environment variable values. If the variables are present,\n// but inconsistent (e.g., shard_index >= total_shards), prints\n// an error and exits. If in_subprocess_for_death_test, sharding is\n// disabled because it must only be applied to the original test\n// process. Otherwise, we could filter out death tests we intended to execute.\nGTEST_API_ bool ShouldShard(const char* total_shards_str,\n                            const char* shard_index_str,\n                            bool in_subprocess_for_death_test);\n\n// Parses the environment variable var as an Int32. If it is unset,\n// returns default_val. If it is not an Int32, prints an error and\n// and aborts.\nGTEST_API_ Int32 Int32FromEnvOrDie(const char* env_var, Int32 default_val);\n\n// Given the total number of shards, the shard index, and the test id,\n// returns true iff the test should be run on this shard. The test id is\n// some arbitrary but unique non-negative integer assigned to each test\n// method. Assumes that 0 <= shard_index < total_shards.\nGTEST_API_ bool ShouldRunTestOnShard(\n    int total_shards, int shard_index, int test_id);\n\n// STL container utilities.\n\n// Returns the number of elements in the given container that satisfy\n// the given predicate.\ntemplate <class Container, typename Predicate>\ninline int CountIf(const Container& c, Predicate predicate) {\n  // Implemented as an explicit loop since std::count_if() in libCstd on\n  // Solaris has a non-standard signature.\n  int count = 0;\n  for (typename Container::const_iterator it = c.begin(); it != c.end(); ++it) {\n    if (predicate(*it))\n      ++count;\n  }\n  return count;\n}\n\n// Applies a function/functor to each element in the container.\ntemplate <class Container, typename Functor>\nvoid ForEach(const Container& c, Functor functor) {\n  std::for_each(c.begin(), c.end(), functor);\n}\n\n// Returns the i-th element of the vector, or default_value if i is not\n// in range [0, v.size()).\ntemplate <typename E>\ninline E GetElementOr(const std::vector<E>& v, int i, E default_value) {\n  return (i < 0 || i >= static_cast<int>(v.size())) ? default_value : v[i];\n}\n\n// Performs an in-place shuffle of a range of the vector's elements.\n// 'begin' and 'end' are element indices as an STL-style range;\n// i.e. [begin, end) are shuffled, where 'end' == size() means to\n// shuffle to the end of the vector.\ntemplate <typename E>\nvoid ShuffleRange(internal::Random* random, int begin, int end,\n                  std::vector<E>* v) {\n  const int size = static_cast<int>(v->size());\n  GTEST_CHECK_(0 <= begin && begin <= size)\n      << \"Invalid shuffle range start \" << begin << \": must be in range [0, \"\n      << size << \"].\";\n  GTEST_CHECK_(begin <= end && end <= size)\n      << \"Invalid shuffle range finish \" << end << \": must be in range [\"\n      << begin << \", \" << size << \"].\";\n\n  // Fisher-Yates shuffle, from\n  // http://en.wikipedia.org/wiki/Fisher-Yates_shuffle\n  for (int range_width = end - begin; range_width >= 2; range_width--) {\n    const int last_in_range = begin + range_width - 1;\n    const int selected = begin + random->Generate(range_width);\n    std::swap((*v)[selected], (*v)[last_in_range]);\n  }\n}\n\n// Performs an in-place shuffle of the vector's elements.\ntemplate <typename E>\ninline void Shuffle(internal::Random* random, std::vector<E>* v) {\n  ShuffleRange(random, 0, static_cast<int>(v->size()), v);\n}\n\n// A function for deleting an object.  Handy for being used as a\n// functor.\ntemplate <typename T>\nstatic void Delete(T* x) {\n  delete x;\n}\n\n// A predicate that checks the key of a TestProperty against a known key.\n//\n// TestPropertyKeyIs is copyable.\nclass TestPropertyKeyIs {\n public:\n  // Constructor.\n  //\n  // TestPropertyKeyIs has NO default constructor.\n  explicit TestPropertyKeyIs(const std::string& key) : key_(key) {}\n\n  // Returns true iff the test name of test property matches on key_.\n  bool operator()(const TestProperty& test_property) const {\n    return test_property.key() == key_;\n  }\n\n private:\n  std::string key_;\n};\n\n// Class UnitTestOptions.\n//\n// This class contains functions for processing options the user\n// specifies when running the tests.  It has only static members.\n//\n// In most cases, the user can specify an option using either an\n// environment variable or a command line flag.  E.g. you can set the\n// test filter using either GTEST_FILTER or --gtest_filter.  If both\n// the variable and the flag are present, the latter overrides the\n// former.\nclass GTEST_API_ UnitTestOptions {\n public:\n  // Functions for processing the gtest_output flag.\n\n  // Returns the output format, or \"\" for normal printed output.\n  static std::string GetOutputFormat();\n\n  // Returns the absolute path of the requested output file, or the\n  // default (test_detail.xml in the original working directory) if\n  // none was explicitly specified.\n  static std::string GetAbsolutePathToOutputFile();\n\n  // Functions for processing the gtest_filter flag.\n\n  // Returns true iff the wildcard pattern matches the string.  The\n  // first ':' or '\\0' character in pattern marks the end of it.\n  //\n  // This recursive algorithm isn't very efficient, but is clear and\n  // works well enough for matching test names, which are short.\n  static bool PatternMatchesString(const char *pattern, const char *str);\n\n  // Returns true iff the user-specified filter matches the test case\n  // name and the test name.\n  static bool FilterMatchesTest(const std::string &test_case_name,\n                                const std::string &test_name);\n\n#if GTEST_OS_WINDOWS\n  // Function for supporting the gtest_catch_exception flag.\n\n  // Returns EXCEPTION_EXECUTE_HANDLER if Google Test should handle the\n  // given SEH exception, or EXCEPTION_CONTINUE_SEARCH otherwise.\n  // This function is useful as an __except condition.\n  static int GTestShouldProcessSEH(DWORD exception_code);\n#endif  // GTEST_OS_WINDOWS\n\n  // Returns true if \"name\" matches the ':' separated list of glob-style\n  // filters in \"filter\".\n  static bool MatchesFilter(const std::string& name, const char* filter);\n};\n\n// Returns the current application's name, removing directory path if that\n// is present.  Used by UnitTestOptions::GetOutputFile.\nGTEST_API_ FilePath GetCurrentExecutableName();\n\n// The role interface for getting the OS stack trace as a string.\nclass OsStackTraceGetterInterface {\n public:\n  OsStackTraceGetterInterface() {}\n  virtual ~OsStackTraceGetterInterface() {}\n\n  // Returns the current OS stack trace as an std::string.  Parameters:\n  //\n  //   max_depth  - the maximum number of stack frames to be included\n  //                in the trace.\n  //   skip_count - the number of top frames to be skipped; doesn't count\n  //                against max_depth.\n  virtual std::string CurrentStackTrace(int max_depth, int skip_count) = 0;\n\n  // UponLeavingGTest() should be called immediately before Google Test calls\n  // user code. It saves some information about the current stack that\n  // CurrentStackTrace() will use to find and hide Google Test stack frames.\n  virtual void UponLeavingGTest() = 0;\n\n  // This string is inserted in place of stack frames that are part of\n  // Google Test's implementation.\n  static const char* const kElidedFramesMarker;\n\n private:\n  GTEST_DISALLOW_COPY_AND_ASSIGN_(OsStackTraceGetterInterface);\n};\n\n// A working implementation of the OsStackTraceGetterInterface interface.\nclass OsStackTraceGetter : public OsStackTraceGetterInterface {\n public:\n  OsStackTraceGetter() {}\n\n  virtual std::string CurrentStackTrace(int max_depth, int skip_count);\n  virtual void UponLeavingGTest();\n\n private:\n  GTEST_DISALLOW_COPY_AND_ASSIGN_(OsStackTraceGetter);\n};\n\n// Information about a Google Test trace point.\nstruct TraceInfo {\n  const char* file;\n  int line;\n  std::string message;\n};\n\n// This is the default global test part result reporter used in UnitTestImpl.\n// This class should only be used by UnitTestImpl.\nclass DefaultGlobalTestPartResultReporter\n  : public TestPartResultReporterInterface {\n public:\n  explicit DefaultGlobalTestPartResultReporter(UnitTestImpl* unit_test);\n  // Implements the TestPartResultReporterInterface. Reports the test part\n  // result in the current test.\n  virtual void ReportTestPartResult(const TestPartResult& result);\n\n private:\n  UnitTestImpl* const unit_test_;\n\n  GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultGlobalTestPartResultReporter);\n};\n\n// This is the default per thread test part result reporter used in\n// UnitTestImpl. This class should only be used by UnitTestImpl.\nclass DefaultPerThreadTestPartResultReporter\n    : public TestPartResultReporterInterface {\n public:\n  explicit DefaultPerThreadTestPartResultReporter(UnitTestImpl* unit_test);\n  // Implements the TestPartResultReporterInterface. The implementation just\n  // delegates to the current global test part result reporter of *unit_test_.\n  virtual void ReportTestPartResult(const TestPartResult& result);\n\n private:\n  UnitTestImpl* const unit_test_;\n\n  GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultPerThreadTestPartResultReporter);\n};\n\n// The private implementation of the UnitTest class.  We don't protect\n// the methods under a mutex, as this class is not accessible by a\n// user and the UnitTest class that delegates work to this class does\n// proper locking.\nclass GTEST_API_ UnitTestImpl {\n public:\n  explicit UnitTestImpl(UnitTest* parent);\n  virtual ~UnitTestImpl();\n\n  // There are two different ways to register your own TestPartResultReporter.\n  // You can register your own repoter to listen either only for test results\n  // from the current thread or for results from all threads.\n  // By default, each per-thread test result repoter just passes a new\n  // TestPartResult to the global test result reporter, which registers the\n  // test part result for the currently running test.\n\n  // Returns the global test part result reporter.\n  TestPartResultReporterInterface* GetGlobalTestPartResultReporter();\n\n  // Sets the global test part result reporter.\n  void SetGlobalTestPartResultReporter(\n      TestPartResultReporterInterface* reporter);\n\n  // Returns the test part result reporter for the current thread.\n  TestPartResultReporterInterface* GetTestPartResultReporterForCurrentThread();\n\n  // Sets the test part result reporter for the current thread.\n  void SetTestPartResultReporterForCurrentThread(\n      TestPartResultReporterInterface* reporter);\n\n  // Gets the number of successful test cases.\n  int successful_test_case_count() const;\n\n  // Gets the number of failed test cases.\n  int failed_test_case_count() const;\n\n  // Gets the number of all test cases.\n  int total_test_case_count() const;\n\n  // Gets the number of all test cases that contain at least one test\n  // that should run.\n  int test_case_to_run_count() const;\n\n  // Gets the number of successful tests.\n  int successful_test_count() const;\n\n  // Gets the number of failed tests.\n  int failed_test_count() const;\n\n  // Gets the number of disabled tests that will be reported in the XML report.\n  int reportable_disabled_test_count() const;\n\n  // Gets the number of disabled tests.\n  int disabled_test_count() const;\n\n  // Gets the number of tests to be printed in the XML report.\n  int reportable_test_count() const;\n\n  // Gets the number of all tests.\n  int total_test_count() const;\n\n  // Gets the number of tests that should run.\n  int test_to_run_count() const;\n\n  // Gets the time of the test program start, in ms from the start of the\n  // UNIX epoch.\n  TimeInMillis start_timestamp() const { return start_timestamp_; }\n\n  // Gets the elapsed time, in milliseconds.\n  TimeInMillis elapsed_time() const { return elapsed_time_; }\n\n  // Returns true iff the unit test passed (i.e. all test cases passed).\n  bool Passed() const { return !Failed(); }\n\n  // Returns true iff the unit test failed (i.e. some test case failed\n  // or something outside of all tests failed).\n  bool Failed() const {\n    return failed_test_case_count() > 0 || ad_hoc_test_result()->Failed();\n  }\n\n  // Gets the i-th test case among all the test cases. i can range from 0 to\n  // total_test_case_count() - 1. If i is not in that range, returns NULL.\n  const TestCase* GetTestCase(int i) const {\n    const int index = GetElementOr(test_case_indices_, i, -1);\n    return index < 0 ? NULL : test_cases_[i];\n  }\n\n  // Gets the i-th test case among all the test cases. i can range from 0 to\n  // total_test_case_count() - 1. If i is not in that range, returns NULL.\n  TestCase* GetMutableTestCase(int i) {\n    const int index = GetElementOr(test_case_indices_, i, -1);\n    return index < 0 ? NULL : test_cases_[index];\n  }\n\n  // Provides access to the event listener list.\n  TestEventListeners* listeners() { return &listeners_; }\n\n  // Returns the TestResult for the test that's currently running, or\n  // the TestResult for the ad hoc test if no test is running.\n  TestResult* current_test_result();\n\n  // Returns the TestResult for the ad hoc test.\n  const TestResult* ad_hoc_test_result() const { return &ad_hoc_test_result_; }\n\n  // Sets the OS stack trace getter.\n  //\n  // Does nothing if the input and the current OS stack trace getter\n  // are the same; otherwise, deletes the old getter and makes the\n  // input the current getter.\n  void set_os_stack_trace_getter(OsStackTraceGetterInterface* getter);\n\n  // Returns the current OS stack trace getter if it is not NULL;\n  // otherwise, creates an OsStackTraceGetter, makes it the current\n  // getter, and returns it.\n  OsStackTraceGetterInterface* os_stack_trace_getter();\n\n  // Returns the current OS stack trace as an std::string.\n  //\n  // The maximum number of stack frames to be included is specified by\n  // the gtest_stack_trace_depth flag.  The skip_count parameter\n  // specifies the number of top frames to be skipped, which doesn't\n  // count against the number of frames to be included.\n  //\n  // For example, if Foo() calls Bar(), which in turn calls\n  // CurrentOsStackTraceExceptTop(1), Foo() will be included in the\n  // trace but Bar() and CurrentOsStackTraceExceptTop() won't.\n  std::string CurrentOsStackTraceExceptTop(int skip_count) GTEST_NO_INLINE_;\n\n  // Finds and returns a TestCase with the given name.  If one doesn't\n  // exist, creates one and returns it.\n  //\n  // Arguments:\n  //\n  //   test_case_name: name of the test case\n  //   type_param:     the name of the test's type parameter, or NULL if\n  //                   this is not a typed or a type-parameterized test.\n  //   set_up_tc:      pointer to the function that sets up the test case\n  //   tear_down_tc:   pointer to the function that tears down the test case\n  TestCase* GetTestCase(const char* test_case_name,\n                        const char* type_param,\n                        Test::SetUpTestCaseFunc set_up_tc,\n                        Test::TearDownTestCaseFunc tear_down_tc);\n\n  // Adds a TestInfo to the unit test.\n  //\n  // Arguments:\n  //\n  //   set_up_tc:    pointer to the function that sets up the test case\n  //   tear_down_tc: pointer to the function that tears down the test case\n  //   test_info:    the TestInfo object\n  void AddTestInfo(Test::SetUpTestCaseFunc set_up_tc,\n                   Test::TearDownTestCaseFunc tear_down_tc,\n                   TestInfo* test_info) {\n    // In order to support thread-safe death tests, we need to\n    // remember the original working directory when the test program\n    // was first invoked.  We cannot do this in RUN_ALL_TESTS(), as\n    // the user may have changed the current directory before calling\n    // RUN_ALL_TESTS().  Therefore we capture the current directory in\n    // AddTestInfo(), which is called to register a TEST or TEST_F\n    // before main() is reached.\n    if (original_working_dir_.IsEmpty()) {\n      original_working_dir_.Set(FilePath::GetCurrentDir());\n      GTEST_CHECK_(!original_working_dir_.IsEmpty())\n          << \"Failed to get the current working directory.\";\n    }\n\n    GetTestCase(test_info->test_case_name(),\n                test_info->type_param(),\n                set_up_tc,\n                tear_down_tc)->AddTestInfo(test_info);\n  }\n\n#if GTEST_HAS_PARAM_TEST\n  // Returns ParameterizedTestCaseRegistry object used to keep track of\n  // value-parameterized tests and instantiate and register them.\n  internal::ParameterizedTestCaseRegistry& parameterized_test_registry() {\n    return parameterized_test_registry_;\n  }\n#endif  // GTEST_HAS_PARAM_TEST\n\n  // Sets the TestCase object for the test that's currently running.\n  void set_current_test_case(TestCase* a_current_test_case) {\n    current_test_case_ = a_current_test_case;\n  }\n\n  // Sets the TestInfo object for the test that's currently running.  If\n  // current_test_info is NULL, the assertion results will be stored in\n  // ad_hoc_test_result_.\n  void set_current_test_info(TestInfo* a_current_test_info) {\n    current_test_info_ = a_current_test_info;\n  }\n\n  // Registers all parameterized tests defined using TEST_P and\n  // INSTANTIATE_TEST_CASE_P, creating regular tests for each test/parameter\n  // combination. This method can be called more then once; it has guards\n  // protecting from registering the tests more then once.  If\n  // value-parameterized tests are disabled, RegisterParameterizedTests is\n  // present but does nothing.\n  void RegisterParameterizedTests();\n\n  // Runs all tests in this UnitTest object, prints the result, and\n  // returns true if all tests are successful.  If any exception is\n  // thrown during a test, this test is considered to be failed, but\n  // the rest of the tests will still be run.\n  bool RunAllTests();\n\n  // Clears the results of all tests, except the ad hoc tests.\n  void ClearNonAdHocTestResult() {\n    ForEach(test_cases_, TestCase::ClearTestCaseResult);\n  }\n\n  // Clears the results of ad-hoc test assertions.\n  void ClearAdHocTestResult() {\n    ad_hoc_test_result_.Clear();\n  }\n\n  // Adds a TestProperty to the current TestResult object when invoked in a\n  // context of a test or a test case, or to the global property set. If the\n  // result already contains a property with the same key, the value will be\n  // updated.\n  void RecordProperty(const TestProperty& test_property);\n\n  enum ReactionToSharding {\n    HONOR_SHARDING_PROTOCOL,\n    IGNORE_SHARDING_PROTOCOL\n  };\n\n  // Matches the full name of each test against the user-specified\n  // filter to decide whether the test should run, then records the\n  // result in each TestCase and TestInfo object.\n  // If shard_tests == HONOR_SHARDING_PROTOCOL, further filters tests\n  // based on sharding variables in the environment.\n  // Returns the number of tests that should run.\n  int FilterTests(ReactionToSharding shard_tests);\n\n  // Prints the names of the tests matching the user-specified filter flag.\n  void ListTestsMatchingFilter();\n\n  const TestCase* current_test_case() const { return current_test_case_; }\n  TestInfo* current_test_info() { return current_test_info_; }\n  const TestInfo* current_test_info() const { return current_test_info_; }\n\n  // Returns the vector of environments that need to be set-up/torn-down\n  // before/after the tests are run.\n  std::vector<Environment*>& environments() { return environments_; }\n\n  // Getters for the per-thread Google Test trace stack.\n  std::vector<TraceInfo>& gtest_trace_stack() {\n    return *(gtest_trace_stack_.pointer());\n  }\n  const std::vector<TraceInfo>& gtest_trace_stack() const {\n    return gtest_trace_stack_.get();\n  }\n\n#if GTEST_HAS_DEATH_TEST\n  void InitDeathTestSubprocessControlInfo() {\n    internal_run_death_test_flag_.reset(ParseInternalRunDeathTestFlag());\n  }\n  // Returns a pointer to the parsed --gtest_internal_run_death_test\n  // flag, or NULL if that flag was not specified.\n  // This information is useful only in a death test child process.\n  // Must not be called before a call to InitGoogleTest.\n  const InternalRunDeathTestFlag* internal_run_death_test_flag() const {\n    return internal_run_death_test_flag_.get();\n  }\n\n  // Returns a pointer to the current death test factory.\n  internal::DeathTestFactory* death_test_factory() {\n    return death_test_factory_.get();\n  }\n\n  void SuppressTestEventsIfInSubprocess();\n\n  friend class ReplaceDeathTestFactory;\n#endif  // GTEST_HAS_DEATH_TEST\n\n  // Initializes the event listener performing XML output as specified by\n  // UnitTestOptions. Must not be called before InitGoogleTest.\n  void ConfigureXmlOutput();\n\n#if GTEST_CAN_STREAM_RESULTS_\n  // Initializes the event listener for streaming test results to a socket.\n  // Must not be called before InitGoogleTest.\n  void ConfigureStreamingOutput();\n#endif\n\n  // Performs initialization dependent upon flag values obtained in\n  // ParseGoogleTestFlagsOnly.  Is called from InitGoogleTest after the call to\n  // ParseGoogleTestFlagsOnly.  In case a user neglects to call InitGoogleTest\n  // this function is also called from RunAllTests.  Since this function can be\n  // called more than once, it has to be idempotent.\n  void PostFlagParsingInit();\n\n  // Gets the random seed used at the start of the current test iteration.\n  int random_seed() const { return random_seed_; }\n\n  // Gets the random number generator.\n  internal::Random* random() { return &random_; }\n\n  // Shuffles all test cases, and the tests within each test case,\n  // making sure that death tests are still run first.\n  void ShuffleTests();\n\n  // Restores the test cases and tests to their order before the first shuffle.\n  void UnshuffleTests();\n\n  // Returns the value of GTEST_FLAG(catch_exceptions) at the moment\n  // UnitTest::Run() starts.\n  bool catch_exceptions() const { return catch_exceptions_; }\n\n private:\n  friend class ::testing::UnitTest;\n\n  // Used by UnitTest::Run() to capture the state of\n  // GTEST_FLAG(catch_exceptions) at the moment it starts.\n  void set_catch_exceptions(bool value) { catch_exceptions_ = value; }\n\n  // The UnitTest object that owns this implementation object.\n  UnitTest* const parent_;\n\n  // The working directory when the first TEST() or TEST_F() was\n  // executed.\n  internal::FilePath original_working_dir_;\n\n  // The default test part result reporters.\n  DefaultGlobalTestPartResultReporter default_global_test_part_result_reporter_;\n  DefaultPerThreadTestPartResultReporter\n      default_per_thread_test_part_result_reporter_;\n\n  // Points to (but doesn't own) the global test part result reporter.\n  TestPartResultReporterInterface* global_test_part_result_repoter_;\n\n  // Protects read and write access to global_test_part_result_reporter_.\n  internal::Mutex global_test_part_result_reporter_mutex_;\n\n  // Points to (but doesn't own) the per-thread test part result reporter.\n  internal::ThreadLocal<TestPartResultReporterInterface*>\n      per_thread_test_part_result_reporter_;\n\n  // The vector of environments that need to be set-up/torn-down\n  // before/after the tests are run.\n  std::vector<Environment*> environments_;\n\n  // The vector of TestCases in their original order.  It owns the\n  // elements in the vector.\n  std::vector<TestCase*> test_cases_;\n\n  // Provides a level of indirection for the test case list to allow\n  // easy shuffling and restoring the test case order.  The i-th\n  // element of this vector is the index of the i-th test case in the\n  // shuffled order.\n  std::vector<int> test_case_indices_;\n\n#if GTEST_HAS_PARAM_TEST\n  // ParameterizedTestRegistry object used to register value-parameterized\n  // tests.\n  internal::ParameterizedTestCaseRegistry parameterized_test_registry_;\n\n  // Indicates whether RegisterParameterizedTests() has been called already.\n  bool parameterized_tests_registered_;\n#endif  // GTEST_HAS_PARAM_TEST\n\n  // Index of the last death test case registered.  Initially -1.\n  int last_death_test_case_;\n\n  // This points to the TestCase for the currently running test.  It\n  // changes as Google Test goes through one test case after another.\n  // When no test is running, this is set to NULL and Google Test\n  // stores assertion results in ad_hoc_test_result_.  Initially NULL.\n  TestCase* current_test_case_;\n\n  // This points to the TestInfo for the currently running test.  It\n  // changes as Google Test goes through one test after another.  When\n  // no test is running, this is set to NULL and Google Test stores\n  // assertion results in ad_hoc_test_result_.  Initially NULL.\n  TestInfo* current_test_info_;\n\n  // Normally, a user only writes assertions inside a TEST or TEST_F,\n  // or inside a function called by a TEST or TEST_F.  Since Google\n  // Test keeps track of which test is current running, it can\n  // associate such an assertion with the test it belongs to.\n  //\n  // If an assertion is encountered when no TEST or TEST_F is running,\n  // Google Test attributes the assertion result to an imaginary \"ad hoc\"\n  // test, and records the result in ad_hoc_test_result_.\n  TestResult ad_hoc_test_result_;\n\n  // The list of event listeners that can be used to track events inside\n  // Google Test.\n  TestEventListeners listeners_;\n\n  // The OS stack trace getter.  Will be deleted when the UnitTest\n  // object is destructed.  By default, an OsStackTraceGetter is used,\n  // but the user can set this field to use a custom getter if that is\n  // desired.\n  OsStackTraceGetterInterface* os_stack_trace_getter_;\n\n  // True iff PostFlagParsingInit() has been called.\n  bool post_flag_parse_init_performed_;\n\n  // The random number seed used at the beginning of the test run.\n  int random_seed_;\n\n  // Our random number generator.\n  internal::Random random_;\n\n  // The time of the test program start, in ms from the start of the\n  // UNIX epoch.\n  TimeInMillis start_timestamp_;\n\n  // How long the test took to run, in milliseconds.\n  TimeInMillis elapsed_time_;\n\n#if GTEST_HAS_DEATH_TEST\n  // The decomposed components of the gtest_internal_run_death_test flag,\n  // parsed when RUN_ALL_TESTS is called.\n  internal::scoped_ptr<InternalRunDeathTestFlag> internal_run_death_test_flag_;\n  internal::scoped_ptr<internal::DeathTestFactory> death_test_factory_;\n#endif  // GTEST_HAS_DEATH_TEST\n\n  // A per-thread stack of traces created by the SCOPED_TRACE() macro.\n  internal::ThreadLocal<std::vector<TraceInfo> > gtest_trace_stack_;\n\n  // The value of GTEST_FLAG(catch_exceptions) at the moment RunAllTests()\n  // starts.\n  bool catch_exceptions_;\n\n  GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTestImpl);\n};  // class UnitTestImpl\n\n// Convenience function for accessing the global UnitTest\n// implementation object.\ninline UnitTestImpl* GetUnitTestImpl() {\n  return UnitTest::GetInstance()->impl();\n}\n\n#if GTEST_USES_SIMPLE_RE\n\n// Internal helper functions for implementing the simple regular\n// expression matcher.\nGTEST_API_ bool IsInSet(char ch, const char* str);\nGTEST_API_ bool IsAsciiDigit(char ch);\nGTEST_API_ bool IsAsciiPunct(char ch);\nGTEST_API_ bool IsRepeat(char ch);\nGTEST_API_ bool IsAsciiWhiteSpace(char ch);\nGTEST_API_ bool IsAsciiWordChar(char ch);\nGTEST_API_ bool IsValidEscape(char ch);\nGTEST_API_ bool AtomMatchesChar(bool escaped, char pattern, char ch);\nGTEST_API_ bool ValidateRegex(const char* regex);\nGTEST_API_ bool MatchRegexAtHead(const char* regex, const char* str);\nGTEST_API_ bool MatchRepetitionAndRegexAtHead(\n    bool escaped, char ch, char repeat, const char* regex, const char* str);\nGTEST_API_ bool MatchRegexAnywhere(const char* regex, const char* str);\n\n#endif  // GTEST_USES_SIMPLE_RE\n\n// Parses the command line for Google Test flags, without initializing\n// other parts of Google Test.\nGTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, char** argv);\nGTEST_API_ void ParseGoogleTestFlagsOnly(int* argc, wchar_t** argv);\n\n#if GTEST_HAS_DEATH_TEST\n\n// Returns the message describing the last system error, regardless of the\n// platform.\nGTEST_API_ std::string GetLastErrnoDescription();\n\n// Attempts to parse a string into a positive integer pointed to by the\n// number parameter.  Returns true if that is possible.\n// GTEST_HAS_DEATH_TEST implies that we have ::std::string, so we can use\n// it here.\ntemplate <typename Integer>\nbool ParseNaturalNumber(const ::std::string& str, Integer* number) {\n  // Fail fast if the given string does not begin with a digit;\n  // this bypasses strtoXXX's \"optional leading whitespace and plus\n  // or minus sign\" semantics, which are undesirable here.\n  if (str.empty() || !IsDigit(str[0])) {\n    return false;\n  }\n  errno = 0;\n\n  char* end;\n  // BiggestConvertible is the largest integer type that system-provided\n  // string-to-number conversion routines can return.\n\n# if GTEST_OS_WINDOWS && !defined(__GNUC__)\n\n  // MSVC and C++ Builder define __int64 instead of the standard long long.\n  typedef unsigned __int64 BiggestConvertible;\n  const BiggestConvertible parsed = _strtoui64(str.c_str(), &end, 10);\n\n# else\n\n  typedef unsigned long long BiggestConvertible;  // NOLINT\n  const BiggestConvertible parsed = strtoull(str.c_str(), &end, 10);\n\n# endif  // GTEST_OS_WINDOWS && !defined(__GNUC__)\n\n  const bool parse_success = *end == '\\0' && errno == 0;\n\n  // TODO(vladl@google.com): Convert this to compile time assertion when it is\n  // available.\n  GTEST_CHECK_(sizeof(Integer) <= sizeof(parsed));\n\n  const Integer result = static_cast<Integer>(parsed);\n  if (parse_success && static_cast<BiggestConvertible>(result) == parsed) {\n    *number = result;\n    return true;\n  }\n  return false;\n}\n#endif  // GTEST_HAS_DEATH_TEST\n\n// TestResult contains some private methods that should be hidden from\n// Google Test user but are required for testing. This class allow our tests\n// to access them.\n//\n// This class is supplied only for the purpose of testing Google Test's own\n// constructs. Do not use it in user tests, either directly or indirectly.\nclass TestResultAccessor {\n public:\n  static void RecordProperty(TestResult* test_result,\n                             const std::string& xml_element,\n                             const TestProperty& property) {\n    test_result->RecordProperty(xml_element, property);\n  }\n\n  static void ClearTestPartResults(TestResult* test_result) {\n    test_result->ClearTestPartResults();\n  }\n\n  static const std::vector<testing::TestPartResult>& test_part_results(\n      const TestResult& test_result) {\n    return test_result.test_part_results();\n  }\n};\n\n#if GTEST_CAN_STREAM_RESULTS_\n\n// Streams test results to the given port on the given host machine.\nclass GTEST_API_ StreamingListener : public EmptyTestEventListener {\n public:\n  // Abstract base class for writing strings to a socket.\n  class AbstractSocketWriter {\n   public:\n    virtual ~AbstractSocketWriter() {}\n\n    // Sends a string to the socket.\n    virtual void Send(const std::string& message) = 0;\n\n    // Closes the socket.\n    virtual void CloseConnection() {}\n\n    // Sends a string and a newline to the socket.\n    void SendLn(const std::string& message) { Send(message + \"\\n\"); }\n  };\n\n  // Concrete class for actually writing strings to a socket.\n  class SocketWriter : public AbstractSocketWriter {\n   public:\n    SocketWriter(const std::string& host, const std::string& port)\n        : sockfd_(-1), host_name_(host), port_num_(port) {\n      MakeConnection();\n    }\n\n    virtual ~SocketWriter() {\n      if (sockfd_ != -1)\n        CloseConnection();\n    }\n\n    // Sends a string to the socket.\n    virtual void Send(const std::string& message) {\n      GTEST_CHECK_(sockfd_ != -1)\n          << \"Send() can be called only when there is a connection.\";\n\n      const int len = static_cast<int>(message.length());\n      if (write(sockfd_, message.c_str(), len) != len) {\n        GTEST_LOG_(WARNING)\n            << \"stream_result_to: failed to stream to \"\n            << host_name_ << \":\" << port_num_;\n      }\n    }\n\n   private:\n    // Creates a client socket and connects to the server.\n    void MakeConnection();\n\n    // Closes the socket.\n    void CloseConnection() {\n      GTEST_CHECK_(sockfd_ != -1)\n          << \"CloseConnection() can be called only when there is a connection.\";\n\n      close(sockfd_);\n      sockfd_ = -1;\n    }\n\n    int sockfd_;  // socket file descriptor\n    const std::string host_name_;\n    const std::string port_num_;\n\n    GTEST_DISALLOW_COPY_AND_ASSIGN_(SocketWriter);\n  };  // class SocketWriter\n\n  // Escapes '=', '&', '%', and '\\n' characters in str as \"%xx\".\n  static std::string UrlEncode(const char* str);\n\n  StreamingListener(const std::string& host, const std::string& port)\n      : socket_writer_(new SocketWriter(host, port)) {\n    Start();\n  }\n\n  explicit StreamingListener(AbstractSocketWriter* socket_writer)\n      : socket_writer_(socket_writer) { Start(); }\n\n  void OnTestProgramStart(const UnitTest& /* unit_test */) {\n    SendLn(\"event=TestProgramStart\");\n  }\n\n  void OnTestProgramEnd(const UnitTest& unit_test) {\n    // Note that Google Test current only report elapsed time for each\n    // test iteration, not for the entire test program.\n    SendLn(\"event=TestProgramEnd&passed=\" + FormatBool(unit_test.Passed()));\n\n    // Notify the streaming server to stop.\n    socket_writer_->CloseConnection();\n  }\n\n  void OnTestIterationStart(const UnitTest& /* unit_test */, int iteration) {\n    SendLn(\"event=TestIterationStart&iteration=\" +\n           StreamableToString(iteration));\n  }\n\n  void OnTestIterationEnd(const UnitTest& unit_test, int /* iteration */) {\n    SendLn(\"event=TestIterationEnd&passed=\" +\n           FormatBool(unit_test.Passed()) + \"&elapsed_time=\" +\n           StreamableToString(unit_test.elapsed_time()) + \"ms\");\n  }\n\n  void OnTestCaseStart(const TestCase& test_case) {\n    SendLn(std::string(\"event=TestCaseStart&name=\") + test_case.name());\n  }\n\n  void OnTestCaseEnd(const TestCase& test_case) {\n    SendLn(\"event=TestCaseEnd&passed=\" + FormatBool(test_case.Passed())\n           + \"&elapsed_time=\" + StreamableToString(test_case.elapsed_time())\n           + \"ms\");\n  }\n\n  void OnTestStart(const TestInfo& test_info) {\n    SendLn(std::string(\"event=TestStart&name=\") + test_info.name());\n  }\n\n  void OnTestEnd(const TestInfo& test_info) {\n    SendLn(\"event=TestEnd&passed=\" +\n           FormatBool((test_info.result())->Passed()) +\n           \"&elapsed_time=\" +\n           StreamableToString((test_info.result())->elapsed_time()) + \"ms\");\n  }\n\n  void OnTestPartResult(const TestPartResult& test_part_result) {\n    const char* file_name = test_part_result.file_name();\n    if (file_name == NULL)\n      file_name = \"\";\n    SendLn(\"event=TestPartResult&file=\" + UrlEncode(file_name) +\n           \"&line=\" + StreamableToString(test_part_result.line_number()) +\n           \"&message=\" + UrlEncode(test_part_result.message()));\n  }\n\n private:\n  // Sends the given message and a newline to the socket.\n  void SendLn(const std::string& message) { socket_writer_->SendLn(message); }\n\n  // Called at the start of streaming to notify the receiver what\n  // protocol we are using.\n  void Start() { SendLn(\"gtest_streaming_protocol_version=1.0\"); }\n\n  std::string FormatBool(bool value) { return value ? \"1\" : \"0\"; }\n\n  const scoped_ptr<AbstractSocketWriter> socket_writer_;\n\n  GTEST_DISALLOW_COPY_AND_ASSIGN_(StreamingListener);\n};  // class StreamingListener\n\n#endif  // GTEST_CAN_STREAM_RESULTS_\n\n}  // namespace internal\n\n// Returns the value of g_help_flag.\nbool GetGtestHelpFlag() {\n  return internal::g_help_flag;\n}\n}  // namespace testing\n\n#endif  // GTEST_SRC_GTEST_INTERNAL_INL_H_\n#undef GTEST_IMPLEMENTATION_\n\n#if GTEST_OS_WINDOWS\n# define vsnprintf _vsnprintf\n#endif  // GTEST_OS_WINDOWS\n\nnamespace testing {\n\nusing internal::CountIf;\nusing internal::ForEach;\nusing internal::GetElementOr;\nusing internal::Shuffle;\n\n// Constants.\n\n// A test whose test case name or test name matches this filter is\n// disabled and not run.\nstatic const char kDisableTestFilter[] = \"DISABLED_*:*/DISABLED_*\";\n\n// A test case whose name matches this filter is considered a death\n// test case and will be run before test cases whose name doesn't\n// match this filter.\nstatic const char kDeathTestCaseFilter[] = \"*DeathTest:*DeathTest/*\";\n\n// A test filter that matches everything.\nstatic const char kUniversalFilter[] = \"*\";\n\n// The default output file for XML output.\nstatic const char kDefaultOutputFile[] = \"test_detail.xml\";\n\n// The environment variable name for the test shard index.\nstatic const char kTestShardIndex[] = \"GTEST_SHARD_INDEX\";\n// The environment variable name for the total number of test shards.\nstatic const char kTestTotalShards[] = \"GTEST_TOTAL_SHARDS\";\n// The environment variable name for the test shard status file.\nstatic const char kTestShardStatusFile[] = \"GTEST_SHARD_STATUS_FILE\";\n\nnamespace internal {\n\n// The text used in failure messages to indicate the start of the\n// stack trace.\nconst char kStackTraceMarker[] = \"\\nStack trace:\\n\";\n\n// g_help_flag is true iff the --help flag or an equivalent form is\n// specified on the command line.\nbool g_help_flag = false;\n\n}  // namespace internal\n\nstatic const char* GetDefaultFilter() {\n#ifdef GTEST_TEST_FILTER_ENV_VAR_\n  const char* const testbridge_test_only = getenv(GTEST_TEST_FILTER_ENV_VAR_);\n  if (testbridge_test_only != NULL) {\n    return testbridge_test_only;\n  }\n#endif  // GTEST_TEST_FILTER_ENV_VAR_\n  return kUniversalFilter;\n}\n\nGTEST_DEFINE_bool_(\n    also_run_disabled_tests,\n    internal::BoolFromGTestEnv(\"also_run_disabled_tests\", false),\n    \"Run disabled tests too, in addition to the tests normally being run.\");\n\nGTEST_DEFINE_bool_(\n    break_on_failure,\n    internal::BoolFromGTestEnv(\"break_on_failure\", false),\n    \"True iff a failed assertion should be a debugger break-point.\");\n\nGTEST_DEFINE_bool_(\n    catch_exceptions,\n    internal::BoolFromGTestEnv(\"catch_exceptions\", true),\n    \"True iff \" GTEST_NAME_\n    \" should catch exceptions and treat them as test failures.\");\n\nGTEST_DEFINE_string_(\n    color,\n    internal::StringFromGTestEnv(\"color\", \"auto\"),\n    \"Whether to use colors in the output.  Valid values: yes, no, \"\n    \"and auto.  'auto' means to use colors if the output is \"\n    \"being sent to a terminal and the TERM environment variable \"\n    \"is set to a terminal type that supports colors.\");\n\nGTEST_DEFINE_string_(\n    filter,\n    internal::StringFromGTestEnv(\"filter\", GetDefaultFilter()),\n    \"A colon-separated list of glob (not regex) patterns \"\n    \"for filtering the tests to run, optionally followed by a \"\n    \"'-' and a : separated list of negative patterns (tests to \"\n    \"exclude).  A test is run if it matches one of the positive \"\n    \"patterns and does not match any of the negative patterns.\");\n\nGTEST_DEFINE_bool_(list_tests, false,\n                   \"List all tests without running them.\");\n\nGTEST_DEFINE_string_(\n    output,\n    internal::StringFromGTestEnv(\"output\", \"\"),\n    \"A format (currently must be \\\"xml\\\"), optionally followed \"\n    \"by a colon and an output file name or directory. A directory \"\n    \"is indicated by a trailing pathname separator. \"\n    \"Examples: \\\"xml:filename.xml\\\", \\\"xml::directoryname/\\\". \"\n    \"If a directory is specified, output files will be created \"\n    \"within that directory, with file-names based on the test \"\n    \"executable's name and, if necessary, made unique by adding \"\n    \"digits.\");\n\nGTEST_DEFINE_bool_(\n    print_time,\n    internal::BoolFromGTestEnv(\"print_time\", true),\n    \"True iff \" GTEST_NAME_\n    \" should display elapsed time in text output.\");\n\nGTEST_DEFINE_int32_(\n    random_seed,\n    internal::Int32FromGTestEnv(\"random_seed\", 0),\n    \"Random number seed to use when shuffling test orders.  Must be in range \"\n    \"[1, 99999], or 0 to use a seed based on the current time.\");\n\nGTEST_DEFINE_int32_(\n    repeat,\n    internal::Int32FromGTestEnv(\"repeat\", 1),\n    \"How many times to repeat each test.  Specify a negative number \"\n    \"for repeating forever.  Useful for shaking out flaky tests.\");\n\nGTEST_DEFINE_bool_(\n    show_internal_stack_frames, false,\n    \"True iff \" GTEST_NAME_ \" should include internal stack frames when \"\n    \"printing test failure stack traces.\");\n\nGTEST_DEFINE_bool_(\n    shuffle,\n    internal::BoolFromGTestEnv(\"shuffle\", false),\n    \"True iff \" GTEST_NAME_\n    \" should randomize tests' order on every run.\");\n\nGTEST_DEFINE_int32_(\n    stack_trace_depth,\n    internal::Int32FromGTestEnv(\"stack_trace_depth\", kMaxStackTraceDepth),\n    \"The maximum number of stack frames to print when an \"\n    \"assertion fails.  The valid range is 0 through 100, inclusive.\");\n\nGTEST_DEFINE_string_(\n    stream_result_to,\n    internal::StringFromGTestEnv(\"stream_result_to\", \"\"),\n    \"This flag specifies the host name and the port number on which to stream \"\n    \"test results. Example: \\\"localhost:555\\\". The flag is effective only on \"\n    \"Linux.\");\n\nGTEST_DEFINE_bool_(\n    throw_on_failure,\n    internal::BoolFromGTestEnv(\"throw_on_failure\", false),\n    \"When this flag is specified, a failed assertion will throw an exception \"\n    \"if exceptions are enabled or exit the program with a non-zero code \"\n    \"otherwise.\");\n\n#if GTEST_USE_OWN_FLAGFILE_FLAG_\nGTEST_DEFINE_string_(\n    flagfile,\n    internal::StringFromGTestEnv(\"flagfile\", \"\"),\n    \"This flag specifies the flagfile to read command-line flags from.\");\n#endif  // GTEST_USE_OWN_FLAGFILE_FLAG_\n\nnamespace internal {\n\n// Generates a random number from [0, range), using a Linear\n// Congruential Generator (LCG).  Crashes if 'range' is 0 or greater\n// than kMaxRange.\nUInt32 Random::Generate(UInt32 range) {\n  // These constants are the same as are used in glibc's rand(3).\n  state_ = (1103515245U*state_ + 12345U) % kMaxRange;\n\n  GTEST_CHECK_(range > 0)\n      << \"Cannot generate a number in the range [0, 0).\";\n  GTEST_CHECK_(range <= kMaxRange)\n      << \"Generation of a number in [0, \" << range << \") was requested, \"\n      << \"but this can only generate numbers in [0, \" << kMaxRange << \").\";\n\n  // Converting via modulus introduces a bit of downward bias, but\n  // it's simple, and a linear congruential generator isn't too good\n  // to begin with.\n  return state_ % range;\n}\n\n// GTestIsInitialized() returns true iff the user has initialized\n// Google Test.  Useful for catching the user mistake of not initializing\n// Google Test before calling RUN_ALL_TESTS().\nstatic bool GTestIsInitialized() { return GetArgvs().size() > 0; }\n\n// Iterates over a vector of TestCases, keeping a running sum of the\n// results of calling a given int-returning method on each.\n// Returns the sum.\nstatic int SumOverTestCaseList(const std::vector<TestCase*>& case_list,\n                               int (TestCase::*method)() const) {\n  int sum = 0;\n  for (size_t i = 0; i < case_list.size(); i++) {\n    sum += (case_list[i]->*method)();\n  }\n  return sum;\n}\n\n// Returns true iff the test case passed.\nstatic bool TestCasePassed(const TestCase* test_case) {\n  return test_case->should_run() && test_case->Passed();\n}\n\n// Returns true iff the test case failed.\nstatic bool TestCaseFailed(const TestCase* test_case) {\n  return test_case->should_run() && test_case->Failed();\n}\n\n// Returns true iff test_case contains at least one test that should\n// run.\nstatic bool ShouldRunTestCase(const TestCase* test_case) {\n  return test_case->should_run();\n}\n\n// AssertHelper constructor.\nAssertHelper::AssertHelper(TestPartResult::Type type,\n                           const char* file,\n                           int line,\n                           const char* message)\n    : data_(new AssertHelperData(type, file, line, message)) {\n}\n\nAssertHelper::~AssertHelper() {\n  delete data_;\n}\n\n// Message assignment, for assertion streaming support.\nvoid AssertHelper::operator=(const Message& message) const {\n  UnitTest::GetInstance()->\n    AddTestPartResult(data_->type, data_->file, data_->line,\n                      AppendUserMessage(data_->message, message),\n                      UnitTest::GetInstance()->impl()\n                      ->CurrentOsStackTraceExceptTop(1)\n                      // Skips the stack frame for this function itself.\n                      );  // NOLINT\n}\n\n// Mutex for linked pointers.\nGTEST_API_ GTEST_DEFINE_STATIC_MUTEX_(g_linked_ptr_mutex);\n\n// A copy of all command line arguments.  Set by InitGoogleTest().\n::std::vector<testing::internal::string> g_argvs;\n\nconst ::std::vector<testing::internal::string>& GetArgvs() {\n#if defined(GTEST_CUSTOM_GET_ARGVS_)\n  return GTEST_CUSTOM_GET_ARGVS_();\n#else  // defined(GTEST_CUSTOM_GET_ARGVS_)\n  return g_argvs;\n#endif  // defined(GTEST_CUSTOM_GET_ARGVS_)\n}\n\n// Returns the current application's name, removing directory path if that\n// is present.\nFilePath GetCurrentExecutableName() {\n  FilePath result;\n\n#if GTEST_OS_WINDOWS\n  result.Set(FilePath(GetArgvs()[0]).RemoveExtension(\"exe\"));\n#else\n  result.Set(FilePath(GetArgvs()[0]));\n#endif  // GTEST_OS_WINDOWS\n\n  return result.RemoveDirectoryName();\n}\n\n// Functions for processing the gtest_output flag.\n\n// Returns the output format, or \"\" for normal printed output.\nstd::string UnitTestOptions::GetOutputFormat() {\n  const char* const gtest_output_flag = GTEST_FLAG(output).c_str();\n  if (gtest_output_flag == NULL) return std::string(\"\");\n\n  const char* const colon = strchr(gtest_output_flag, ':');\n  return (colon == NULL) ?\n      std::string(gtest_output_flag) :\n      std::string(gtest_output_flag, colon - gtest_output_flag);\n}\n\n// Returns the name of the requested output file, or the default if none\n// was explicitly specified.\nstd::string UnitTestOptions::GetAbsolutePathToOutputFile() {\n  const char* const gtest_output_flag = GTEST_FLAG(output).c_str();\n  if (gtest_output_flag == NULL)\n    return \"\";\n\n  const char* const colon = strchr(gtest_output_flag, ':');\n  if (colon == NULL)\n    return internal::FilePath::ConcatPaths(\n        internal::FilePath(\n            UnitTest::GetInstance()->original_working_dir()),\n        internal::FilePath(kDefaultOutputFile)).string();\n\n  internal::FilePath output_name(colon + 1);\n  if (!output_name.IsAbsolutePath())\n    // TODO(wan@google.com): on Windows \\some\\path is not an absolute\n    // path (as its meaning depends on the current drive), yet the\n    // following logic for turning it into an absolute path is wrong.\n    // Fix it.\n    output_name = internal::FilePath::ConcatPaths(\n        internal::FilePath(UnitTest::GetInstance()->original_working_dir()),\n        internal::FilePath(colon + 1));\n\n  if (!output_name.IsDirectory())\n    return output_name.string();\n\n  internal::FilePath result(internal::FilePath::GenerateUniqueFileName(\n      output_name, internal::GetCurrentExecutableName(),\n      GetOutputFormat().c_str()));\n  return result.string();\n}\n\n// Returns true iff the wildcard pattern matches the string.  The\n// first ':' or '\\0' character in pattern marks the end of it.\n//\n// This recursive algorithm isn't very efficient, but is clear and\n// works well enough for matching test names, which are short.\nbool UnitTestOptions::PatternMatchesString(const char *pattern,\n                                           const char *str) {\n  switch (*pattern) {\n    case '\\0':\n    case ':':  // Either ':' or '\\0' marks the end of the pattern.\n      return *str == '\\0';\n    case '?':  // Matches any single character.\n      return *str != '\\0' && PatternMatchesString(pattern + 1, str + 1);\n    case '*':  // Matches any string (possibly empty) of characters.\n      return (*str != '\\0' && PatternMatchesString(pattern, str + 1)) ||\n          PatternMatchesString(pattern + 1, str);\n    default:  // Non-special character.  Matches itself.\n      return *pattern == *str &&\n          PatternMatchesString(pattern + 1, str + 1);\n  }\n}\n\nbool UnitTestOptions::MatchesFilter(\n    const std::string& name, const char* filter) {\n  const char *cur_pattern = filter;\n  for (;;) {\n    if (PatternMatchesString(cur_pattern, name.c_str())) {\n      return true;\n    }\n\n    // Finds the next pattern in the filter.\n    cur_pattern = strchr(cur_pattern, ':');\n\n    // Returns if no more pattern can be found.\n    if (cur_pattern == NULL) {\n      return false;\n    }\n\n    // Skips the pattern separater (the ':' character).\n    cur_pattern++;\n  }\n}\n\n// Returns true iff the user-specified filter matches the test case\n// name and the test name.\nbool UnitTestOptions::FilterMatchesTest(const std::string &test_case_name,\n                                        const std::string &test_name) {\n  const std::string& full_name = test_case_name + \".\" + test_name.c_str();\n\n  // Split --gtest_filter at '-', if there is one, to separate into\n  // positive filter and negative filter portions\n  const char* const p = GTEST_FLAG(filter).c_str();\n  const char* const dash = strchr(p, '-');\n  std::string positive;\n  std::string negative;\n  if (dash == NULL) {\n    positive = GTEST_FLAG(filter).c_str();  // Whole string is a positive filter\n    negative = \"\";\n  } else {\n    positive = std::string(p, dash);   // Everything up to the dash\n    negative = std::string(dash + 1);  // Everything after the dash\n    if (positive.empty()) {\n      // Treat '-test1' as the same as '*-test1'\n      positive = kUniversalFilter;\n    }\n  }\n\n  // A filter is a colon-separated list of patterns.  It matches a\n  // test if any pattern in it matches the test.\n  return (MatchesFilter(full_name, positive.c_str()) &&\n          !MatchesFilter(full_name, negative.c_str()));\n}\n\n#if GTEST_HAS_SEH\n// Returns EXCEPTION_EXECUTE_HANDLER if Google Test should handle the\n// given SEH exception, or EXCEPTION_CONTINUE_SEARCH otherwise.\n// This function is useful as an __except condition.\nint UnitTestOptions::GTestShouldProcessSEH(DWORD exception_code) {\n  // Google Test should handle a SEH exception if:\n  //   1. the user wants it to, AND\n  //   2. this is not a breakpoint exception, AND\n  //   3. this is not a C++ exception (VC++ implements them via SEH,\n  //      apparently).\n  //\n  // SEH exception code for C++ exceptions.\n  // (see http://support.microsoft.com/kb/185294 for more information).\n  const DWORD kCxxExceptionCode = 0xe06d7363;\n\n  bool should_handle = true;\n\n  if (!GTEST_FLAG(catch_exceptions))\n    should_handle = false;\n  else if (exception_code == EXCEPTION_BREAKPOINT)\n    should_handle = false;\n  else if (exception_code == kCxxExceptionCode)\n    should_handle = false;\n\n  return should_handle ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH;\n}\n#endif  // GTEST_HAS_SEH\n\n}  // namespace internal\n\n// The c'tor sets this object as the test part result reporter used by\n// Google Test.  The 'result' parameter specifies where to report the\n// results. Intercepts only failures from the current thread.\nScopedFakeTestPartResultReporter::ScopedFakeTestPartResultReporter(\n    TestPartResultArray* result)\n    : intercept_mode_(INTERCEPT_ONLY_CURRENT_THREAD),\n      result_(result) {\n  Init();\n}\n\n// The c'tor sets this object as the test part result reporter used by\n// Google Test.  The 'result' parameter specifies where to report the\n// results.\nScopedFakeTestPartResultReporter::ScopedFakeTestPartResultReporter(\n    InterceptMode intercept_mode, TestPartResultArray* result)\n    : intercept_mode_(intercept_mode),\n      result_(result) {\n  Init();\n}\n\nvoid ScopedFakeTestPartResultReporter::Init() {\n  internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();\n  if (intercept_mode_ == INTERCEPT_ALL_THREADS) {\n    old_reporter_ = impl->GetGlobalTestPartResultReporter();\n    impl->SetGlobalTestPartResultReporter(this);\n  } else {\n    old_reporter_ = impl->GetTestPartResultReporterForCurrentThread();\n    impl->SetTestPartResultReporterForCurrentThread(this);\n  }\n}\n\n// The d'tor restores the test part result reporter used by Google Test\n// before.\nScopedFakeTestPartResultReporter::~ScopedFakeTestPartResultReporter() {\n  internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();\n  if (intercept_mode_ == INTERCEPT_ALL_THREADS) {\n    impl->SetGlobalTestPartResultReporter(old_reporter_);\n  } else {\n    impl->SetTestPartResultReporterForCurrentThread(old_reporter_);\n  }\n}\n\n// Increments the test part result count and remembers the result.\n// This method is from the TestPartResultReporterInterface interface.\nvoid ScopedFakeTestPartResultReporter::ReportTestPartResult(\n    const TestPartResult& result) {\n  result_->Append(result);\n}\n\nnamespace internal {\n\n// Returns the type ID of ::testing::Test.  We should always call this\n// instead of GetTypeId< ::testing::Test>() to get the type ID of\n// testing::Test.  This is to work around a suspected linker bug when\n// using Google Test as a framework on Mac OS X.  The bug causes\n// GetTypeId< ::testing::Test>() to return different values depending\n// on whether the call is from the Google Test framework itself or\n// from user test code.  GetTestTypeId() is guaranteed to always\n// return the same value, as it always calls GetTypeId<>() from the\n// gtest.cc, which is within the Google Test framework.\nTypeId GetTestTypeId() {\n  return GetTypeId<Test>();\n}\n\n// The value of GetTestTypeId() as seen from within the Google Test\n// library.  This is solely for testing GetTestTypeId().\nextern const TypeId kTestTypeIdInGoogleTest = GetTestTypeId();\n\n// This predicate-formatter checks that 'results' contains a test part\n// failure of the given type and that the failure message contains the\n// given substring.\nAssertionResult HasOneFailure(const char* /* results_expr */,\n                              const char* /* type_expr */,\n                              const char* /* substr_expr */,\n                              const TestPartResultArray& results,\n                              TestPartResult::Type type,\n                              const std::string& substr) {\n  const std::string expected(type == TestPartResult::kFatalFailure ?\n                        \"1 fatal failure\" :\n                        \"1 non-fatal failure\");\n  Message msg;\n  if (results.size() != 1) {\n    msg << \"Expected: \" << expected << \"\\n\"\n        << \"  Actual: \" << results.size() << \" failures\";\n    for (int i = 0; i < results.size(); i++) {\n      msg << \"\\n\" << results.GetTestPartResult(i);\n    }\n    return AssertionFailure() << msg;\n  }\n\n  const TestPartResult& r = results.GetTestPartResult(0);\n  if (r.type() != type) {\n    return AssertionFailure() << \"Expected: \" << expected << \"\\n\"\n                              << \"  Actual:\\n\"\n                              << r;\n  }\n\n  if (strstr(r.message(), substr.c_str()) == NULL) {\n    return AssertionFailure() << \"Expected: \" << expected << \" containing \\\"\"\n                              << substr << \"\\\"\\n\"\n                              << \"  Actual:\\n\"\n                              << r;\n  }\n\n  return AssertionSuccess();\n}\n\n// The constructor of SingleFailureChecker remembers where to look up\n// test part results, what type of failure we expect, and what\n// substring the failure message should contain.\nSingleFailureChecker::SingleFailureChecker(const TestPartResultArray* results,\n                                           TestPartResult::Type type,\n                                           const std::string& substr)\n    : results_(results), type_(type), substr_(substr) {}\n\n// The destructor of SingleFailureChecker verifies that the given\n// TestPartResultArray contains exactly one failure that has the given\n// type and contains the given substring.  If that's not the case, a\n// non-fatal failure will be generated.\nSingleFailureChecker::~SingleFailureChecker() {\n  EXPECT_PRED_FORMAT3(HasOneFailure, *results_, type_, substr_);\n}\n\nDefaultGlobalTestPartResultReporter::DefaultGlobalTestPartResultReporter(\n    UnitTestImpl* unit_test) : unit_test_(unit_test) {}\n\nvoid DefaultGlobalTestPartResultReporter::ReportTestPartResult(\n    const TestPartResult& result) {\n  unit_test_->current_test_result()->AddTestPartResult(result);\n  unit_test_->listeners()->repeater()->OnTestPartResult(result);\n}\n\nDefaultPerThreadTestPartResultReporter::DefaultPerThreadTestPartResultReporter(\n    UnitTestImpl* unit_test) : unit_test_(unit_test) {}\n\nvoid DefaultPerThreadTestPartResultReporter::ReportTestPartResult(\n    const TestPartResult& result) {\n  unit_test_->GetGlobalTestPartResultReporter()->ReportTestPartResult(result);\n}\n\n// Returns the global test part result reporter.\nTestPartResultReporterInterface*\nUnitTestImpl::GetGlobalTestPartResultReporter() {\n  internal::MutexLock lock(&global_test_part_result_reporter_mutex_);\n  return global_test_part_result_repoter_;\n}\n\n// Sets the global test part result reporter.\nvoid UnitTestImpl::SetGlobalTestPartResultReporter(\n    TestPartResultReporterInterface* reporter) {\n  internal::MutexLock lock(&global_test_part_result_reporter_mutex_);\n  global_test_part_result_repoter_ = reporter;\n}\n\n// Returns the test part result reporter for the current thread.\nTestPartResultReporterInterface*\nUnitTestImpl::GetTestPartResultReporterForCurrentThread() {\n  return per_thread_test_part_result_reporter_.get();\n}\n\n// Sets the test part result reporter for the current thread.\nvoid UnitTestImpl::SetTestPartResultReporterForCurrentThread(\n    TestPartResultReporterInterface* reporter) {\n  per_thread_test_part_result_reporter_.set(reporter);\n}\n\n// Gets the number of successful test cases.\nint UnitTestImpl::successful_test_case_count() const {\n  return CountIf(test_cases_, TestCasePassed);\n}\n\n// Gets the number of failed test cases.\nint UnitTestImpl::failed_test_case_count() const {\n  return CountIf(test_cases_, TestCaseFailed);\n}\n\n// Gets the number of all test cases.\nint UnitTestImpl::total_test_case_count() const {\n  return static_cast<int>(test_cases_.size());\n}\n\n// Gets the number of all test cases that contain at least one test\n// that should run.\nint UnitTestImpl::test_case_to_run_count() const {\n  return CountIf(test_cases_, ShouldRunTestCase);\n}\n\n// Gets the number of successful tests.\nint UnitTestImpl::successful_test_count() const {\n  return SumOverTestCaseList(test_cases_, &TestCase::successful_test_count);\n}\n\n// Gets the number of failed tests.\nint UnitTestImpl::failed_test_count() const {\n  return SumOverTestCaseList(test_cases_, &TestCase::failed_test_count);\n}\n\n// Gets the number of disabled tests that will be reported in the XML report.\nint UnitTestImpl::reportable_disabled_test_count() const {\n  return SumOverTestCaseList(test_cases_,\n                             &TestCase::reportable_disabled_test_count);\n}\n\n// Gets the number of disabled tests.\nint UnitTestImpl::disabled_test_count() const {\n  return SumOverTestCaseList(test_cases_, &TestCase::disabled_test_count);\n}\n\n// Gets the number of tests to be printed in the XML report.\nint UnitTestImpl::reportable_test_count() const {\n  return SumOverTestCaseList(test_cases_, &TestCase::reportable_test_count);\n}\n\n// Gets the number of all tests.\nint UnitTestImpl::total_test_count() const {\n  return SumOverTestCaseList(test_cases_, &TestCase::total_test_count);\n}\n\n// Gets the number of tests that should run.\nint UnitTestImpl::test_to_run_count() const {\n  return SumOverTestCaseList(test_cases_, &TestCase::test_to_run_count);\n}\n\n// Returns the current OS stack trace as an std::string.\n//\n// The maximum number of stack frames to be included is specified by\n// the gtest_stack_trace_depth flag.  The skip_count parameter\n// specifies the number of top frames to be skipped, which doesn't\n// count against the number of frames to be included.\n//\n// For example, if Foo() calls Bar(), which in turn calls\n// CurrentOsStackTraceExceptTop(1), Foo() will be included in the\n// trace but Bar() and CurrentOsStackTraceExceptTop() won't.\nstd::string UnitTestImpl::CurrentOsStackTraceExceptTop(int skip_count) {\n  return os_stack_trace_getter()->CurrentStackTrace(\n      static_cast<int>(GTEST_FLAG(stack_trace_depth)),\n      skip_count + 1\n      // Skips the user-specified number of frames plus this function\n      // itself.\n      );  // NOLINT\n}\n\n// Returns the current time in milliseconds.\nTimeInMillis GetTimeInMillis() {\n#if GTEST_OS_WINDOWS_MOBILE || defined(__BORLANDC__)\n  // Difference between 1970-01-01 and 1601-01-01 in milliseconds.\n  // http://analogous.blogspot.com/2005/04/epoch.html\n  const TimeInMillis kJavaEpochToWinFileTimeDelta =\n    static_cast<TimeInMillis>(116444736UL) * 100000UL;\n  const DWORD kTenthMicrosInMilliSecond = 10000;\n\n  SYSTEMTIME now_systime;\n  FILETIME now_filetime;\n  ULARGE_INTEGER now_int64;\n  // TODO(kenton@google.com): Shouldn't this just use\n  //   GetSystemTimeAsFileTime()?\n  GetSystemTime(&now_systime);\n  if (SystemTimeToFileTime(&now_systime, &now_filetime)) {\n    now_int64.LowPart = now_filetime.dwLowDateTime;\n    now_int64.HighPart = now_filetime.dwHighDateTime;\n    now_int64.QuadPart = (now_int64.QuadPart / kTenthMicrosInMilliSecond) -\n      kJavaEpochToWinFileTimeDelta;\n    return now_int64.QuadPart;\n  }\n  return 0;\n#elif GTEST_OS_WINDOWS && !GTEST_HAS_GETTIMEOFDAY_\n  __timeb64 now;\n\n  // MSVC 8 deprecates _ftime64(), so we want to suppress warning 4996\n  // (deprecated function) there.\n  // TODO(kenton@google.com): Use GetTickCount()?  Or use\n  //   SystemTimeToFileTime()\n  GTEST_DISABLE_MSC_WARNINGS_PUSH_(4996)\n  _ftime64(&now);\n  GTEST_DISABLE_MSC_WARNINGS_POP_()\n\n  return static_cast<TimeInMillis>(now.time) * 1000 + now.millitm;\n#elif GTEST_HAS_GETTIMEOFDAY_\n  struct timeval now;\n  gettimeofday(&now, NULL);\n  return static_cast<TimeInMillis>(now.tv_sec) * 1000 + now.tv_usec / 1000;\n#else\n# error \"Don't know how to get the current time on your system.\"\n#endif\n}\n\n// Utilities\n\n// class String.\n\n#if GTEST_OS_WINDOWS_MOBILE\n// Creates a UTF-16 wide string from the given ANSI string, allocating\n// memory using new. The caller is responsible for deleting the return\n// value using delete[]. Returns the wide string, or NULL if the\n// input is NULL.\nLPCWSTR String::AnsiToUtf16(const char* ansi) {\n  if (!ansi) return NULL;\n  const int length = strlen(ansi);\n  const int unicode_length =\n      MultiByteToWideChar(CP_ACP, 0, ansi, length,\n                          NULL, 0);\n  WCHAR* unicode = new WCHAR[unicode_length + 1];\n  MultiByteToWideChar(CP_ACP, 0, ansi, length,\n                      unicode, unicode_length);\n  unicode[unicode_length] = 0;\n  return unicode;\n}\n\n// Creates an ANSI string from the given wide string, allocating\n// memory using new. The caller is responsible for deleting the return\n// value using delete[]. Returns the ANSI string, or NULL if the\n// input is NULL.\nconst char* String::Utf16ToAnsi(LPCWSTR utf16_str)  {\n  if (!utf16_str) return NULL;\n  const int ansi_length =\n      WideCharToMultiByte(CP_ACP, 0, utf16_str, -1,\n                          NULL, 0, NULL, NULL);\n  char* ansi = new char[ansi_length + 1];\n  WideCharToMultiByte(CP_ACP, 0, utf16_str, -1,\n                      ansi, ansi_length, NULL, NULL);\n  ansi[ansi_length] = 0;\n  return ansi;\n}\n\n#endif  // GTEST_OS_WINDOWS_MOBILE\n\n// Compares two C strings.  Returns true iff they have the same content.\n//\n// Unlike strcmp(), this function can handle NULL argument(s).  A NULL\n// C string is considered different to any non-NULL C string,\n// including the empty string.\nbool String::CStringEquals(const char * lhs, const char * rhs) {\n  if ( lhs == NULL ) return rhs == NULL;\n\n  if ( rhs == NULL ) return false;\n\n  return strcmp(lhs, rhs) == 0;\n}\n\n#if GTEST_HAS_STD_WSTRING || GTEST_HAS_GLOBAL_WSTRING\n\n// Converts an array of wide chars to a narrow string using the UTF-8\n// encoding, and streams the result to the given Message object.\nstatic void StreamWideCharsToMessage(const wchar_t* wstr, size_t length,\n                                     Message* msg) {\n  for (size_t i = 0; i != length; ) {  // NOLINT\n    if (wstr[i] != L'\\0') {\n      *msg << WideStringToUtf8(wstr + i, static_cast<int>(length - i));\n      while (i != length && wstr[i] != L'\\0')\n        i++;\n    } else {\n      *msg << '\\0';\n      i++;\n    }\n  }\n}\n\n#endif  // GTEST_HAS_STD_WSTRING || GTEST_HAS_GLOBAL_WSTRING\n\nvoid SplitString(const ::std::string& str, char delimiter,\n                 ::std::vector< ::std::string>* dest) {\n  ::std::vector< ::std::string> parsed;\n  ::std::string::size_type pos = 0;\n  while (::testing::internal::AlwaysTrue()) {\n    const ::std::string::size_type colon = str.find(delimiter, pos);\n    if (colon == ::std::string::npos) {\n      parsed.push_back(str.substr(pos));\n      break;\n    } else {\n      parsed.push_back(str.substr(pos, colon - pos));\n      pos = colon + 1;\n    }\n  }\n  dest->swap(parsed);\n}\n\n}  // namespace internal\n\n// Constructs an empty Message.\n// We allocate the stringstream separately because otherwise each use of\n// ASSERT/EXPECT in a procedure adds over 200 bytes to the procedure's\n// stack frame leading to huge stack frames in some cases; gcc does not reuse\n// the stack space.\nMessage::Message() : ss_(new ::std::stringstream) {\n  // By default, we want there to be enough precision when printing\n  // a double to a Message.\n  *ss_ << std::setprecision(std::numeric_limits<double>::digits10 + 2);\n}\n\n// These two overloads allow streaming a wide C string to a Message\n// using the UTF-8 encoding.\nMessage& Message::operator <<(const wchar_t* wide_c_str) {\n  return *this << internal::String::ShowWideCString(wide_c_str);\n}\nMessage& Message::operator <<(wchar_t* wide_c_str) {\n  return *this << internal::String::ShowWideCString(wide_c_str);\n}\n\n#if GTEST_HAS_STD_WSTRING\n// Converts the given wide string to a narrow string using the UTF-8\n// encoding, and streams the result to this Message object.\nMessage& Message::operator <<(const ::std::wstring& wstr) {\n  internal::StreamWideCharsToMessage(wstr.c_str(), wstr.length(), this);\n  return *this;\n}\n#endif  // GTEST_HAS_STD_WSTRING\n\n#if GTEST_HAS_GLOBAL_WSTRING\n// Converts the given wide string to a narrow string using the UTF-8\n// encoding, and streams the result to this Message object.\nMessage& Message::operator <<(const ::wstring& wstr) {\n  internal::StreamWideCharsToMessage(wstr.c_str(), wstr.length(), this);\n  return *this;\n}\n#endif  // GTEST_HAS_GLOBAL_WSTRING\n\n// Gets the text streamed to this object so far as an std::string.\n// Each '\\0' character in the buffer is replaced with \"\\\\0\".\nstd::string Message::GetString() const {\n  return internal::StringStreamToString(ss_.get());\n}\n\n// AssertionResult constructors.\n// Used in EXPECT_TRUE/FALSE(assertion_result).\nAssertionResult::AssertionResult(const AssertionResult& other)\n    : success_(other.success_),\n      message_(other.message_.get() != NULL ?\n               new ::std::string(*other.message_) :\n               static_cast< ::std::string*>(NULL)) {\n}\n\n// Swaps two AssertionResults.\nvoid AssertionResult::swap(AssertionResult& other) {\n  using std::swap;\n  swap(success_, other.success_);\n  swap(message_, other.message_);\n}\n\n// Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE.\nAssertionResult AssertionResult::operator!() const {\n  AssertionResult negation(!success_);\n  if (message_.get() != NULL)\n    negation << *message_;\n  return negation;\n}\n\n// Makes a successful assertion result.\nAssertionResult AssertionSuccess() {\n  return AssertionResult(true);\n}\n\n// Makes a failed assertion result.\nAssertionResult AssertionFailure() {\n  return AssertionResult(false);\n}\n\n// Makes a failed assertion result with the given failure message.\n// Deprecated; use AssertionFailure() << message.\nAssertionResult AssertionFailure(const Message& message) {\n  return AssertionFailure() << message;\n}\n\nnamespace internal {\n\nnamespace edit_distance {\nstd::vector<EditType> CalculateOptimalEdits(const std::vector<size_t>& left,\n                                            const std::vector<size_t>& right) {\n  std::vector<std::vector<double> > costs(\n      left.size() + 1, std::vector<double>(right.size() + 1));\n  std::vector<std::vector<EditType> > best_move(\n      left.size() + 1, std::vector<EditType>(right.size() + 1));\n\n  // Populate for empty right.\n  for (size_t l_i = 0; l_i < costs.size(); ++l_i) {\n    costs[l_i][0] = static_cast<double>(l_i);\n    best_move[l_i][0] = kRemove;\n  }\n  // Populate for empty left.\n  for (size_t r_i = 1; r_i < costs[0].size(); ++r_i) {\n    costs[0][r_i] = static_cast<double>(r_i);\n    best_move[0][r_i] = kAdd;\n  }\n\n  for (size_t l_i = 0; l_i < left.size(); ++l_i) {\n    for (size_t r_i = 0; r_i < right.size(); ++r_i) {\n      if (left[l_i] == right[r_i]) {\n        // Found a match. Consume it.\n        costs[l_i + 1][r_i + 1] = costs[l_i][r_i];\n        best_move[l_i + 1][r_i + 1] = kMatch;\n        continue;\n      }\n\n      const double add = costs[l_i + 1][r_i];\n      const double remove = costs[l_i][r_i + 1];\n      const double replace = costs[l_i][r_i];\n      if (add < remove && add < replace) {\n        costs[l_i + 1][r_i + 1] = add + 1;\n        best_move[l_i + 1][r_i + 1] = kAdd;\n      } else if (remove < add && remove < replace) {\n        costs[l_i + 1][r_i + 1] = remove + 1;\n        best_move[l_i + 1][r_i + 1] = kRemove;\n      } else {\n        // We make replace a little more expensive than add/remove to lower\n        // their priority.\n        costs[l_i + 1][r_i + 1] = replace + 1.00001;\n        best_move[l_i + 1][r_i + 1] = kReplace;\n      }\n    }\n  }\n\n  // Reconstruct the best path. We do it in reverse order.\n  std::vector<EditType> best_path;\n  for (size_t l_i = left.size(), r_i = right.size(); l_i > 0 || r_i > 0;) {\n    EditType move = best_move[l_i][r_i];\n    best_path.push_back(move);\n    l_i -= move != kAdd;\n    r_i -= move != kRemove;\n  }\n  std::reverse(best_path.begin(), best_path.end());\n  return best_path;\n}\n\nnamespace {\n\n// Helper class to convert string into ids with deduplication.\nclass InternalStrings {\n public:\n  size_t GetId(const std::string& str) {\n    IdMap::iterator it = ids_.find(str);\n    if (it != ids_.end()) return it->second;\n    size_t id = ids_.size();\n    return ids_[str] = id;\n  }\n\n private:\n  typedef std::map<std::string, size_t> IdMap;\n  IdMap ids_;\n};\n\n}  // namespace\n\nstd::vector<EditType> CalculateOptimalEdits(\n    const std::vector<std::string>& left,\n    const std::vector<std::string>& right) {\n  std::vector<size_t> left_ids, right_ids;\n  {\n    InternalStrings intern_table;\n    for (size_t i = 0; i < left.size(); ++i) {\n      left_ids.push_back(intern_table.GetId(left[i]));\n    }\n    for (size_t i = 0; i < right.size(); ++i) {\n      right_ids.push_back(intern_table.GetId(right[i]));\n    }\n  }\n  return CalculateOptimalEdits(left_ids, right_ids);\n}\n\nnamespace {\n\n// Helper class that holds the state for one hunk and prints it out to the\n// stream.\n// It reorders adds/removes when possible to group all removes before all\n// adds. It also adds the hunk header before printint into the stream.\nclass Hunk {\n public:\n  Hunk(size_t left_start, size_t right_start)\n      : left_start_(left_start),\n        right_start_(right_start),\n        adds_(),\n        removes_(),\n        common_() {}\n\n  void PushLine(char edit, const char* line) {\n    switch (edit) {\n      case ' ':\n        ++common_;\n        FlushEdits();\n        hunk_.push_back(std::make_pair(' ', line));\n        break;\n      case '-':\n        ++removes_;\n        hunk_removes_.push_back(std::make_pair('-', line));\n        break;\n      case '+':\n        ++adds_;\n        hunk_adds_.push_back(std::make_pair('+', line));\n        break;\n    }\n  }\n\n  void PrintTo(std::ostream* os) {\n    PrintHeader(os);\n    FlushEdits();\n    for (std::list<std::pair<char, const char*> >::const_iterator it =\n             hunk_.begin();\n         it != hunk_.end(); ++it) {\n      *os << it->first << it->second << \"\\n\";\n    }\n  }\n\n  bool has_edits() const { return adds_ || removes_; }\n\n private:\n  void FlushEdits() {\n    hunk_.splice(hunk_.end(), hunk_removes_);\n    hunk_.splice(hunk_.end(), hunk_adds_);\n  }\n\n  // Print a unified diff header for one hunk.\n  // The format is\n  //   \"@@ -<left_start>,<left_length> +<right_start>,<right_length> @@\"\n  // where the left/right parts are ommitted if unnecessary.\n  void PrintHeader(std::ostream* ss) const {\n    *ss << \"@@ \";\n    if (removes_) {\n      *ss << \"-\" << left_start_ << \",\" << (removes_ + common_);\n    }\n    if (removes_ && adds_) {\n      *ss << \" \";\n    }\n    if (adds_) {\n      *ss << \"+\" << right_start_ << \",\" << (adds_ + common_);\n    }\n    *ss << \" @@\\n\";\n  }\n\n  size_t left_start_, right_start_;\n  size_t adds_, removes_, common_;\n  std::list<std::pair<char, const char*> > hunk_, hunk_adds_, hunk_removes_;\n};\n\n}  // namespace\n\n// Create a list of diff hunks in Unified diff format.\n// Each hunk has a header generated by PrintHeader above plus a body with\n// lines prefixed with ' ' for no change, '-' for deletion and '+' for\n// addition.\n// 'context' represents the desired unchanged prefix/suffix around the diff.\n// If two hunks are close enough that their contexts overlap, then they are\n// joined into one hunk.\nstd::string CreateUnifiedDiff(const std::vector<std::string>& left,\n                              const std::vector<std::string>& right,\n                              size_t context) {\n  const std::vector<EditType> edits = CalculateOptimalEdits(left, right);\n\n  size_t l_i = 0, r_i = 0, edit_i = 0;\n  std::stringstream ss;\n  while (edit_i < edits.size()) {\n    // Find first edit.\n    while (edit_i < edits.size() && edits[edit_i] == kMatch) {\n      ++l_i;\n      ++r_i;\n      ++edit_i;\n    }\n\n    // Find the first line to include in the hunk.\n    const size_t prefix_context = std::min(l_i, context);\n    Hunk hunk(l_i - prefix_context + 1, r_i - prefix_context + 1);\n    for (size_t i = prefix_context; i > 0; --i) {\n      hunk.PushLine(' ', left[l_i - i].c_str());\n    }\n\n    // Iterate the edits until we found enough suffix for the hunk or the input\n    // is over.\n    size_t n_suffix = 0;\n    for (; edit_i < edits.size(); ++edit_i) {\n      if (n_suffix >= context) {\n        // Continue only if the next hunk is very close.\n        std::vector<EditType>::const_iterator it = edits.begin() + edit_i;\n        while (it != edits.end() && *it == kMatch) ++it;\n        if (it == edits.end() || (it - edits.begin()) - edit_i >= context) {\n          // There is no next edit or it is too far away.\n          break;\n        }\n      }\n\n      EditType edit = edits[edit_i];\n      // Reset count when a non match is found.\n      n_suffix = edit == kMatch ? n_suffix + 1 : 0;\n\n      if (edit == kMatch || edit == kRemove || edit == kReplace) {\n        hunk.PushLine(edit == kMatch ? ' ' : '-', left[l_i].c_str());\n      }\n      if (edit == kAdd || edit == kReplace) {\n        hunk.PushLine('+', right[r_i].c_str());\n      }\n\n      // Advance indices, depending on edit type.\n      l_i += edit != kAdd;\n      r_i += edit != kRemove;\n    }\n\n    if (!hunk.has_edits()) {\n      // We are done. We don't want this hunk.\n      break;\n    }\n\n    hunk.PrintTo(&ss);\n  }\n  return ss.str();\n}\n\n}  // namespace edit_distance\n\nnamespace {\n\n// The string representation of the values received in EqFailure() are already\n// escaped. Split them on escaped '\\n' boundaries. Leave all other escaped\n// characters the same.\nstd::vector<std::string> SplitEscapedString(const std::string& str) {\n  std::vector<std::string> lines;\n  size_t start = 0, end = str.size();\n  if (end > 2 && str[0] == '\"' && str[end - 1] == '\"') {\n    ++start;\n    --end;\n  }\n  bool escaped = false;\n  for (size_t i = start; i + 1 < end; ++i) {\n    if (escaped) {\n      escaped = false;\n      if (str[i] == 'n') {\n        lines.push_back(str.substr(start, i - start - 1));\n        start = i + 1;\n      }\n    } else {\n      escaped = str[i] == '\\\\';\n    }\n  }\n  lines.push_back(str.substr(start, end - start));\n  return lines;\n}\n\n}  // namespace\n\n// Constructs and returns the message for an equality assertion\n// (e.g. ASSERT_EQ, EXPECT_STREQ, etc) failure.\n//\n// The first four parameters are the expressions used in the assertion\n// and their values, as strings.  For example, for ASSERT_EQ(foo, bar)\n// where foo is 5 and bar is 6, we have:\n//\n//   lhs_expression: \"foo\"\n//   rhs_expression: \"bar\"\n//   lhs_value:      \"5\"\n//   rhs_value:      \"6\"\n//\n// The ignoring_case parameter is true iff the assertion is a\n// *_STRCASEEQ*.  When it's true, the string \"Ignoring case\" will\n// be inserted into the message.\nAssertionResult EqFailure(const char* lhs_expression,\n                          const char* rhs_expression,\n                          const std::string& lhs_value,\n                          const std::string& rhs_value,\n                          bool ignoring_case) {\n  Message msg;\n  msg << \"      Expected: \" << lhs_expression;\n  if (lhs_value != lhs_expression) {\n    msg << \"\\n      Which is: \" << lhs_value;\n  }\n  msg << \"\\nTo be equal to: \" << rhs_expression;\n  if (rhs_value != rhs_expression) {\n    msg << \"\\n      Which is: \" << rhs_value;\n  }\n\n  if (ignoring_case) {\n    msg << \"\\nIgnoring case\";\n  }\n\n  if (!lhs_value.empty() && !rhs_value.empty()) {\n    const std::vector<std::string> lhs_lines =\n        SplitEscapedString(lhs_value);\n    const std::vector<std::string> rhs_lines =\n        SplitEscapedString(rhs_value);\n    if (lhs_lines.size() > 1 || rhs_lines.size() > 1) {\n      msg << \"\\nWith diff:\\n\"\n          << edit_distance::CreateUnifiedDiff(lhs_lines, rhs_lines);\n    }\n  }\n\n  return AssertionFailure() << msg;\n}\n\n// Constructs a failure message for Boolean assertions such as EXPECT_TRUE.\nstd::string GetBoolAssertionFailureMessage(\n    const AssertionResult& assertion_result,\n    const char* expression_text,\n    const char* actual_predicate_value,\n    const char* expected_predicate_value) {\n  const char* actual_message = assertion_result.message();\n  Message msg;\n  msg << \"Value of: \" << expression_text\n      << \"\\n  Actual: \" << actual_predicate_value;\n  if (actual_message[0] != '\\0')\n    msg << \" (\" << actual_message << \")\";\n  msg << \"\\nExpected: \" << expected_predicate_value;\n  return msg.GetString();\n}\n\n// Helper function for implementing ASSERT_NEAR.\nAssertionResult DoubleNearPredFormat(const char* expr1,\n                                     const char* expr2,\n                                     const char* abs_error_expr,\n                                     double val1,\n                                     double val2,\n                                     double abs_error) {\n  const double diff = fabs(val1 - val2);\n  if (diff <= abs_error) return AssertionSuccess();\n\n  // TODO(wan): do not print the value of an expression if it's\n  // already a literal.\n  return AssertionFailure()\n      << \"The difference between \" << expr1 << \" and \" << expr2\n      << \" is \" << diff << \", which exceeds \" << abs_error_expr << \", where\\n\"\n      << expr1 << \" evaluates to \" << val1 << \",\\n\"\n      << expr2 << \" evaluates to \" << val2 << \", and\\n\"\n      << abs_error_expr << \" evaluates to \" << abs_error << \".\";\n}\n\n\n// Helper template for implementing FloatLE() and DoubleLE().\ntemplate <typename RawType>\nAssertionResult FloatingPointLE(const char* expr1,\n                                const char* expr2,\n                                RawType val1,\n                                RawType val2) {\n  // Returns success if val1 is less than val2,\n  if (val1 < val2) {\n    return AssertionSuccess();\n  }\n\n  // or if val1 is almost equal to val2.\n  const FloatingPoint<RawType> lhs(val1), rhs(val2);\n  if (lhs.AlmostEquals(rhs)) {\n    return AssertionSuccess();\n  }\n\n  // Note that the above two checks will both fail if either val1 or\n  // val2 is NaN, as the IEEE floating-point standard requires that\n  // any predicate involving a NaN must return false.\n\n  ::std::stringstream val1_ss;\n  val1_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)\n          << val1;\n\n  ::std::stringstream val2_ss;\n  val2_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)\n          << val2;\n\n  return AssertionFailure()\n      << \"Expected: (\" << expr1 << \") <= (\" << expr2 << \")\\n\"\n      << \"  Actual: \" << StringStreamToString(&val1_ss) << \" vs \"\n      << StringStreamToString(&val2_ss);\n}\n\n}  // namespace internal\n\n// Asserts that val1 is less than, or almost equal to, val2.  Fails\n// otherwise.  In particular, it fails if either val1 or val2 is NaN.\nAssertionResult FloatLE(const char* expr1, const char* expr2,\n                        float val1, float val2) {\n  return internal::FloatingPointLE<float>(expr1, expr2, val1, val2);\n}\n\n// Asserts that val1 is less than, or almost equal to, val2.  Fails\n// otherwise.  In particular, it fails if either val1 or val2 is NaN.\nAssertionResult DoubleLE(const char* expr1, const char* expr2,\n                         double val1, double val2) {\n  return internal::FloatingPointLE<double>(expr1, expr2, val1, val2);\n}\n\nnamespace internal {\n\n// The helper function for {ASSERT|EXPECT}_EQ with int or enum\n// arguments.\nAssertionResult CmpHelperEQ(const char* lhs_expression,\n                            const char* rhs_expression,\n                            BiggestInt lhs,\n                            BiggestInt rhs) {\n  if (lhs == rhs) {\n    return AssertionSuccess();\n  }\n\n  return EqFailure(lhs_expression,\n                   rhs_expression,\n                   FormatForComparisonFailureMessage(lhs, rhs),\n                   FormatForComparisonFailureMessage(rhs, lhs),\n                   false);\n}\n\n// A macro for implementing the helper functions needed to implement\n// ASSERT_?? and EXPECT_?? with integer or enum arguments.  It is here\n// just to avoid copy-and-paste of similar code.\n#define GTEST_IMPL_CMP_HELPER_(op_name, op)\\\nAssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \\\n                                   BiggestInt val1, BiggestInt val2) {\\\n  if (val1 op val2) {\\\n    return AssertionSuccess();\\\n  } else {\\\n    return AssertionFailure() \\\n        << \"Expected: (\" << expr1 << \") \" #op \" (\" << expr2\\\n        << \"), actual: \" << FormatForComparisonFailureMessage(val1, val2)\\\n        << \" vs \" << FormatForComparisonFailureMessage(val2, val1);\\\n  }\\\n}\n\n// Implements the helper function for {ASSERT|EXPECT}_NE with int or\n// enum arguments.\nGTEST_IMPL_CMP_HELPER_(NE, !=)\n// Implements the helper function for {ASSERT|EXPECT}_LE with int or\n// enum arguments.\nGTEST_IMPL_CMP_HELPER_(LE, <=)\n// Implements the helper function for {ASSERT|EXPECT}_LT with int or\n// enum arguments.\nGTEST_IMPL_CMP_HELPER_(LT, < )\n// Implements the helper function for {ASSERT|EXPECT}_GE with int or\n// enum arguments.\nGTEST_IMPL_CMP_HELPER_(GE, >=)\n// Implements the helper function for {ASSERT|EXPECT}_GT with int or\n// enum arguments.\nGTEST_IMPL_CMP_HELPER_(GT, > )\n\n#undef GTEST_IMPL_CMP_HELPER_\n\n// The helper function for {ASSERT|EXPECT}_STREQ.\nAssertionResult CmpHelperSTREQ(const char* lhs_expression,\n                               const char* rhs_expression,\n                               const char* lhs,\n                               const char* rhs) {\n  if (String::CStringEquals(lhs, rhs)) {\n    return AssertionSuccess();\n  }\n\n  return EqFailure(lhs_expression,\n                   rhs_expression,\n                   PrintToString(lhs),\n                   PrintToString(rhs),\n                   false);\n}\n\n// The helper function for {ASSERT|EXPECT}_STRCASEEQ.\nAssertionResult CmpHelperSTRCASEEQ(const char* lhs_expression,\n                                   const char* rhs_expression,\n                                   const char* lhs,\n                                   const char* rhs) {\n  if (String::CaseInsensitiveCStringEquals(lhs, rhs)) {\n    return AssertionSuccess();\n  }\n\n  return EqFailure(lhs_expression,\n                   rhs_expression,\n                   PrintToString(lhs),\n                   PrintToString(rhs),\n                   true);\n}\n\n// The helper function for {ASSERT|EXPECT}_STRNE.\nAssertionResult CmpHelperSTRNE(const char* s1_expression,\n                               const char* s2_expression,\n                               const char* s1,\n                               const char* s2) {\n  if (!String::CStringEquals(s1, s2)) {\n    return AssertionSuccess();\n  } else {\n    return AssertionFailure() << \"Expected: (\" << s1_expression << \") != (\"\n                              << s2_expression << \"), actual: \\\"\"\n                              << s1 << \"\\\" vs \\\"\" << s2 << \"\\\"\";\n  }\n}\n\n// The helper function for {ASSERT|EXPECT}_STRCASENE.\nAssertionResult CmpHelperSTRCASENE(const char* s1_expression,\n                                   const char* s2_expression,\n                                   const char* s1,\n                                   const char* s2) {\n  if (!String::CaseInsensitiveCStringEquals(s1, s2)) {\n    return AssertionSuccess();\n  } else {\n    return AssertionFailure()\n        << \"Expected: (\" << s1_expression << \") != (\"\n        << s2_expression << \") (ignoring case), actual: \\\"\"\n        << s1 << \"\\\" vs \\\"\" << s2 << \"\\\"\";\n  }\n}\n\n}  // namespace internal\n\nnamespace {\n\n// Helper functions for implementing IsSubString() and IsNotSubstring().\n\n// This group of overloaded functions return true iff needle is a\n// substring of haystack.  NULL is considered a substring of itself\n// only.\n\nbool IsSubstringPred(const char* needle, const char* haystack) {\n  if (needle == NULL || haystack == NULL)\n    return needle == haystack;\n\n  return strstr(haystack, needle) != NULL;\n}\n\nbool IsSubstringPred(const wchar_t* needle, const wchar_t* haystack) {\n  if (needle == NULL || haystack == NULL)\n    return needle == haystack;\n\n  return wcsstr(haystack, needle) != NULL;\n}\n\n// StringType here can be either ::std::string or ::std::wstring.\ntemplate <typename StringType>\nbool IsSubstringPred(const StringType& needle,\n                     const StringType& haystack) {\n  return haystack.find(needle) != StringType::npos;\n}\n\n// This function implements either IsSubstring() or IsNotSubstring(),\n// depending on the value of the expected_to_be_substring parameter.\n// StringType here can be const char*, const wchar_t*, ::std::string,\n// or ::std::wstring.\ntemplate <typename StringType>\nAssertionResult IsSubstringImpl(\n    bool expected_to_be_substring,\n    const char* needle_expr, const char* haystack_expr,\n    const StringType& needle, const StringType& haystack) {\n  if (IsSubstringPred(needle, haystack) == expected_to_be_substring)\n    return AssertionSuccess();\n\n  const bool is_wide_string = sizeof(needle[0]) > 1;\n  const char* const begin_string_quote = is_wide_string ? \"L\\\"\" : \"\\\"\";\n  return AssertionFailure()\n      << \"Value of: \" << needle_expr << \"\\n\"\n      << \"  Actual: \" << begin_string_quote << needle << \"\\\"\\n\"\n      << \"Expected: \" << (expected_to_be_substring ? \"\" : \"not \")\n      << \"a substring of \" << haystack_expr << \"\\n\"\n      << \"Which is: \" << begin_string_quote << haystack << \"\\\"\";\n}\n\n}  // namespace\n\n// IsSubstring() and IsNotSubstring() check whether needle is a\n// substring of haystack (NULL is considered a substring of itself\n// only), and return an appropriate error message when they fail.\n\nAssertionResult IsSubstring(\n    const char* needle_expr, const char* haystack_expr,\n    const char* needle, const char* haystack) {\n  return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack);\n}\n\nAssertionResult IsSubstring(\n    const char* needle_expr, const char* haystack_expr,\n    const wchar_t* needle, const wchar_t* haystack) {\n  return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack);\n}\n\nAssertionResult IsNotSubstring(\n    const char* needle_expr, const char* haystack_expr,\n    const char* needle, const char* haystack) {\n  return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack);\n}\n\nAssertionResult IsNotSubstring(\n    const char* needle_expr, const char* haystack_expr,\n    const wchar_t* needle, const wchar_t* haystack) {\n  return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack);\n}\n\nAssertionResult IsSubstring(\n    const char* needle_expr, const char* haystack_expr,\n    const ::std::string& needle, const ::std::string& haystack) {\n  return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack);\n}\n\nAssertionResult IsNotSubstring(\n    const char* needle_expr, const char* haystack_expr,\n    const ::std::string& needle, const ::std::string& haystack) {\n  return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack);\n}\n\n#if GTEST_HAS_STD_WSTRING\nAssertionResult IsSubstring(\n    const char* needle_expr, const char* haystack_expr,\n    const ::std::wstring& needle, const ::std::wstring& haystack) {\n  return IsSubstringImpl(true, needle_expr, haystack_expr, needle, haystack);\n}\n\nAssertionResult IsNotSubstring(\n    const char* needle_expr, const char* haystack_expr,\n    const ::std::wstring& needle, const ::std::wstring& haystack) {\n  return IsSubstringImpl(false, needle_expr, haystack_expr, needle, haystack);\n}\n#endif  // GTEST_HAS_STD_WSTRING\n\nnamespace internal {\n\n#if GTEST_OS_WINDOWS\n\nnamespace {\n\n// Helper function for IsHRESULT{SuccessFailure} predicates\nAssertionResult HRESULTFailureHelper(const char* expr,\n                                     const char* expected,\n                                     long hr) {  // NOLINT\n# if GTEST_OS_WINDOWS_MOBILE\n\n  // Windows CE doesn't support FormatMessage.\n  const char error_text[] = \"\";\n\n# else\n\n  // Looks up the human-readable system message for the HRESULT code\n  // and since we're not passing any params to FormatMessage, we don't\n  // want inserts expanded.\n  const DWORD kFlags = FORMAT_MESSAGE_FROM_SYSTEM |\n                       FORMAT_MESSAGE_IGNORE_INSERTS;\n  const DWORD kBufSize = 4096;\n  // Gets the system's human readable message string for this HRESULT.\n  char error_text[kBufSize] = { '\\0' };\n  DWORD message_length = ::FormatMessageA(kFlags,\n                                          0,  // no source, we're asking system\n                                          hr,  // the error\n                                          0,  // no line width restrictions\n                                          error_text,  // output buffer\n                                          kBufSize,  // buf size\n                                          NULL);  // no arguments for inserts\n  // Trims tailing white space (FormatMessage leaves a trailing CR-LF)\n  for (; message_length && IsSpace(error_text[message_length - 1]);\n          --message_length) {\n    error_text[message_length - 1] = '\\0';\n  }\n\n# endif  // GTEST_OS_WINDOWS_MOBILE\n\n  const std::string error_hex(\"0x\" + String::FormatHexInt(hr));\n  return ::testing::AssertionFailure()\n      << \"Expected: \" << expr << \" \" << expected << \".\\n\"\n      << \"  Actual: \" << error_hex << \" \" << error_text << \"\\n\";\n}\n\n}  // namespace\n\nAssertionResult IsHRESULTSuccess(const char* expr, long hr) {  // NOLINT\n  if (SUCCEEDED(hr)) {\n    return AssertionSuccess();\n  }\n  return HRESULTFailureHelper(expr, \"succeeds\", hr);\n}\n\nAssertionResult IsHRESULTFailure(const char* expr, long hr) {  // NOLINT\n  if (FAILED(hr)) {\n    return AssertionSuccess();\n  }\n  return HRESULTFailureHelper(expr, \"fails\", hr);\n}\n\n#endif  // GTEST_OS_WINDOWS\n\n// Utility functions for encoding Unicode text (wide strings) in\n// UTF-8.\n\n// A Unicode code-point can have upto 21 bits, and is encoded in UTF-8\n// like this:\n//\n// Code-point length   Encoding\n//   0 -  7 bits       0xxxxxxx\n//   8 - 11 bits       110xxxxx 10xxxxxx\n//  12 - 16 bits       1110xxxx 10xxxxxx 10xxxxxx\n//  17 - 21 bits       11110xxx 10xxxxxx 10xxxxxx 10xxxxxx\n\n// The maximum code-point a one-byte UTF-8 sequence can represent.\nconst UInt32 kMaxCodePoint1 = (static_cast<UInt32>(1) <<  7) - 1;\n\n// The maximum code-point a two-byte UTF-8 sequence can represent.\nconst UInt32 kMaxCodePoint2 = (static_cast<UInt32>(1) << (5 + 6)) - 1;\n\n// The maximum code-point a three-byte UTF-8 sequence can represent.\nconst UInt32 kMaxCodePoint3 = (static_cast<UInt32>(1) << (4 + 2*6)) - 1;\n\n// The maximum code-point a four-byte UTF-8 sequence can represent.\nconst UInt32 kMaxCodePoint4 = (static_cast<UInt32>(1) << (3 + 3*6)) - 1;\n\n// Chops off the n lowest bits from a bit pattern.  Returns the n\n// lowest bits.  As a side effect, the original bit pattern will be\n// shifted to the right by n bits.\ninline UInt32 ChopLowBits(UInt32* bits, int n) {\n  const UInt32 low_bits = *bits & ((static_cast<UInt32>(1) << n) - 1);\n  *bits >>= n;\n  return low_bits;\n}\n\n// Converts a Unicode code point to a narrow string in UTF-8 encoding.\n// code_point parameter is of type UInt32 because wchar_t may not be\n// wide enough to contain a code point.\n// If the code_point is not a valid Unicode code point\n// (i.e. outside of Unicode range U+0 to U+10FFFF) it will be converted\n// to \"(Invalid Unicode 0xXXXXXXXX)\".\nstd::string CodePointToUtf8(UInt32 code_point) {\n  if (code_point > kMaxCodePoint4) {\n    return \"(Invalid Unicode 0x\" + String::FormatHexInt(code_point) + \")\";\n  }\n\n  char str[5];  // Big enough for the largest valid code point.\n  if (code_point <= kMaxCodePoint1) {\n    str[1] = '\\0';\n    str[0] = static_cast<char>(code_point);                          // 0xxxxxxx\n  } else if (code_point <= kMaxCodePoint2) {\n    str[2] = '\\0';\n    str[1] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6));  // 10xxxxxx\n    str[0] = static_cast<char>(0xC0 | code_point);                   // 110xxxxx\n  } else if (code_point <= kMaxCodePoint3) {\n    str[3] = '\\0';\n    str[2] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6));  // 10xxxxxx\n    str[1] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6));  // 10xxxxxx\n    str[0] = static_cast<char>(0xE0 | code_point);                   // 1110xxxx\n  } else {  // code_point <= kMaxCodePoint4\n    str[4] = '\\0';\n    str[3] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6));  // 10xxxxxx\n    str[2] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6));  // 10xxxxxx\n    str[1] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6));  // 10xxxxxx\n    str[0] = static_cast<char>(0xF0 | code_point);                   // 11110xxx\n  }\n  return str;\n}\n\n// The following two functions only make sense if the the system\n// uses UTF-16 for wide string encoding. All supported systems\n// with 16 bit wchar_t (Windows, Cygwin, Symbian OS) do use UTF-16.\n\n// Determines if the arguments constitute UTF-16 surrogate pair\n// and thus should be combined into a single Unicode code point\n// using CreateCodePointFromUtf16SurrogatePair.\ninline bool IsUtf16SurrogatePair(wchar_t first, wchar_t second) {\n  return sizeof(wchar_t) == 2 &&\n      (first & 0xFC00) == 0xD800 && (second & 0xFC00) == 0xDC00;\n}\n\n// Creates a Unicode code point from UTF16 surrogate pair.\ninline UInt32 CreateCodePointFromUtf16SurrogatePair(wchar_t first,\n                                                    wchar_t second) {\n  const UInt32 mask = (1 << 10) - 1;\n  return (sizeof(wchar_t) == 2) ?\n      (((first & mask) << 10) | (second & mask)) + 0x10000 :\n      // This function should not be called when the condition is\n      // false, but we provide a sensible default in case it is.\n      static_cast<UInt32>(first);\n}\n\n// Converts a wide string to a narrow string in UTF-8 encoding.\n// The wide string is assumed to have the following encoding:\n//   UTF-16 if sizeof(wchar_t) == 2 (on Windows, Cygwin, Symbian OS)\n//   UTF-32 if sizeof(wchar_t) == 4 (on Linux)\n// Parameter str points to a null-terminated wide string.\n// Parameter num_chars may additionally limit the number\n// of wchar_t characters processed. -1 is used when the entire string\n// should be processed.\n// If the string contains code points that are not valid Unicode code points\n// (i.e. outside of Unicode range U+0 to U+10FFFF) they will be output\n// as '(Invalid Unicode 0xXXXXXXXX)'. If the string is in UTF16 encoding\n// and contains invalid UTF-16 surrogate pairs, values in those pairs\n// will be encoded as individual Unicode characters from Basic Normal Plane.\nstd::string WideStringToUtf8(const wchar_t* str, int num_chars) {\n  if (num_chars == -1)\n    num_chars = static_cast<int>(wcslen(str));\n\n  ::std::stringstream stream;\n  for (int i = 0; i < num_chars; ++i) {\n    UInt32 unicode_code_point;\n\n    if (str[i] == L'\\0') {\n      break;\n    } else if (i + 1 < num_chars && IsUtf16SurrogatePair(str[i], str[i + 1])) {\n      unicode_code_point = CreateCodePointFromUtf16SurrogatePair(str[i],\n                                                                 str[i + 1]);\n      i++;\n    } else {\n      unicode_code_point = static_cast<UInt32>(str[i]);\n    }\n\n    stream << CodePointToUtf8(unicode_code_point);\n  }\n  return StringStreamToString(&stream);\n}\n\n// Converts a wide C string to an std::string using the UTF-8 encoding.\n// NULL will be converted to \"(null)\".\nstd::string String::ShowWideCString(const wchar_t * wide_c_str) {\n  if (wide_c_str == NULL)  return \"(null)\";\n\n  return internal::WideStringToUtf8(wide_c_str, -1);\n}\n\n// Compares two wide C strings.  Returns true iff they have the same\n// content.\n//\n// Unlike wcscmp(), this function can handle NULL argument(s).  A NULL\n// C string is considered different to any non-NULL C string,\n// including the empty string.\nbool String::WideCStringEquals(const wchar_t * lhs, const wchar_t * rhs) {\n  if (lhs == NULL) return rhs == NULL;\n\n  if (rhs == NULL) return false;\n\n  return wcscmp(lhs, rhs) == 0;\n}\n\n// Helper function for *_STREQ on wide strings.\nAssertionResult CmpHelperSTREQ(const char* lhs_expression,\n                               const char* rhs_expression,\n                               const wchar_t* lhs,\n                               const wchar_t* rhs) {\n  if (String::WideCStringEquals(lhs, rhs)) {\n    return AssertionSuccess();\n  }\n\n  return EqFailure(lhs_expression,\n                   rhs_expression,\n                   PrintToString(lhs),\n                   PrintToString(rhs),\n                   false);\n}\n\n// Helper function for *_STRNE on wide strings.\nAssertionResult CmpHelperSTRNE(const char* s1_expression,\n                               const char* s2_expression,\n                               const wchar_t* s1,\n                               const wchar_t* s2) {\n  if (!String::WideCStringEquals(s1, s2)) {\n    return AssertionSuccess();\n  }\n\n  return AssertionFailure() << \"Expected: (\" << s1_expression << \") != (\"\n                            << s2_expression << \"), actual: \"\n                            << PrintToString(s1)\n                            << \" vs \" << PrintToString(s2);\n}\n\n// Compares two C strings, ignoring case.  Returns true iff they have\n// the same content.\n//\n// Unlike strcasecmp(), this function can handle NULL argument(s).  A\n// NULL C string is considered different to any non-NULL C string,\n// including the empty string.\nbool String::CaseInsensitiveCStringEquals(const char * lhs, const char * rhs) {\n  if (lhs == NULL)\n    return rhs == NULL;\n  if (rhs == NULL)\n    return false;\n  return posix::StrCaseCmp(lhs, rhs) == 0;\n}\n\n  // Compares two wide C strings, ignoring case.  Returns true iff they\n  // have the same content.\n  //\n  // Unlike wcscasecmp(), this function can handle NULL argument(s).\n  // A NULL C string is considered different to any non-NULL wide C string,\n  // including the empty string.\n  // NB: The implementations on different platforms slightly differ.\n  // On windows, this method uses _wcsicmp which compares according to LC_CTYPE\n  // environment variable. On GNU platform this method uses wcscasecmp\n  // which compares according to LC_CTYPE category of the current locale.\n  // On MacOS X, it uses towlower, which also uses LC_CTYPE category of the\n  // current locale.\nbool String::CaseInsensitiveWideCStringEquals(const wchar_t* lhs,\n                                              const wchar_t* rhs) {\n  if (lhs == NULL) return rhs == NULL;\n\n  if (rhs == NULL) return false;\n\n#if GTEST_OS_WINDOWS\n  return _wcsicmp(lhs, rhs) == 0;\n#elif GTEST_OS_LINUX && !GTEST_OS_LINUX_ANDROID\n  return wcscasecmp(lhs, rhs) == 0;\n#else\n  // Android, Mac OS X and Cygwin don't define wcscasecmp.\n  // Other unknown OSes may not define it either.\n  wint_t left, right;\n  do {\n    left = towlower(*lhs++);\n    right = towlower(*rhs++);\n  } while (left && left == right);\n  return left == right;\n#endif  // OS selector\n}\n\n// Returns true iff str ends with the given suffix, ignoring case.\n// Any string is considered to end with an empty suffix.\nbool String::EndsWithCaseInsensitive(\n    const std::string& str, const std::string& suffix) {\n  const size_t str_len = str.length();\n  const size_t suffix_len = suffix.length();\n  return (str_len >= suffix_len) &&\n         CaseInsensitiveCStringEquals(str.c_str() + str_len - suffix_len,\n                                      suffix.c_str());\n}\n\n// Formats an int value as \"%02d\".\nstd::string String::FormatIntWidth2(int value) {\n  std::stringstream ss;\n  ss << std::setfill('0') << std::setw(2) << value;\n  return ss.str();\n}\n\n// Formats an int value as \"%X\".\nstd::string String::FormatHexInt(int value) {\n  std::stringstream ss;\n  ss << std::hex << std::uppercase << value;\n  return ss.str();\n}\n\n// Formats a byte as \"%02X\".\nstd::string String::FormatByte(unsigned char value) {\n  std::stringstream ss;\n  ss << std::setfill('0') << std::setw(2) << std::hex << std::uppercase\n     << static_cast<unsigned int>(value);\n  return ss.str();\n}\n\n// Converts the buffer in a stringstream to an std::string, converting NUL\n// bytes to \"\\\\0\" along the way.\nstd::string StringStreamToString(::std::stringstream* ss) {\n  const ::std::string& str = ss->str();\n  const char* const start = str.c_str();\n  const char* const end = start + str.length();\n\n  std::string result;\n  result.reserve(2 * (end - start));\n  for (const char* ch = start; ch != end; ++ch) {\n    if (*ch == '\\0') {\n      result += \"\\\\0\";  // Replaces NUL with \"\\\\0\";\n    } else {\n      result += *ch;\n    }\n  }\n\n  return result;\n}\n\n// Appends the user-supplied message to the Google-Test-generated message.\nstd::string AppendUserMessage(const std::string& gtest_msg,\n                              const Message& user_msg) {\n  // Appends the user message if it's non-empty.\n  const std::string user_msg_string = user_msg.GetString();\n  if (user_msg_string.empty()) {\n    return gtest_msg;\n  }\n\n  return gtest_msg + \"\\n\" + user_msg_string;\n}\n\n}  // namespace internal\n\n// class TestResult\n\n// Creates an empty TestResult.\nTestResult::TestResult()\n    : death_test_count_(0),\n      elapsed_time_(0) {\n}\n\n// D'tor.\nTestResult::~TestResult() {\n}\n\n// Returns the i-th test part result among all the results. i can\n// range from 0 to total_part_count() - 1. If i is not in that range,\n// aborts the program.\nconst TestPartResult& TestResult::GetTestPartResult(int i) const {\n  if (i < 0 || i >= total_part_count())\n    internal::posix::Abort();\n  return test_part_results_.at(i);\n}\n\n// Returns the i-th test property. i can range from 0 to\n// test_property_count() - 1. If i is not in that range, aborts the\n// program.\nconst TestProperty& TestResult::GetTestProperty(int i) const {\n  if (i < 0 || i >= test_property_count())\n    internal::posix::Abort();\n  return test_properties_.at(i);\n}\n\n// Clears the test part results.\nvoid TestResult::ClearTestPartResults() {\n  test_part_results_.clear();\n}\n\n// Adds a test part result to the list.\nvoid TestResult::AddTestPartResult(const TestPartResult& test_part_result) {\n  test_part_results_.push_back(test_part_result);\n}\n\n// Adds a test property to the list. If a property with the same key as the\n// supplied property is already represented, the value of this test_property\n// replaces the old value for that key.\nvoid TestResult::RecordProperty(const std::string& xml_element,\n                                const TestProperty& test_property) {\n  if (!ValidateTestProperty(xml_element, test_property)) {\n    return;\n  }\n  internal::MutexLock lock(&test_properites_mutex_);\n  const std::vector<TestProperty>::iterator property_with_matching_key =\n      std::find_if(test_properties_.begin(), test_properties_.end(),\n                   internal::TestPropertyKeyIs(test_property.key()));\n  if (property_with_matching_key == test_properties_.end()) {\n    test_properties_.push_back(test_property);\n    return;\n  }\n  property_with_matching_key->SetValue(test_property.value());\n}\n\n// The list of reserved attributes used in the <testsuites> element of XML\n// output.\nstatic const char* const kReservedTestSuitesAttributes[] = {\n  \"disabled\",\n  \"errors\",\n  \"failures\",\n  \"name\",\n  \"random_seed\",\n  \"tests\",\n  \"time\",\n  \"timestamp\"\n};\n\n// The list of reserved attributes used in the <testsuite> element of XML\n// output.\nstatic const char* const kReservedTestSuiteAttributes[] = {\n  \"disabled\",\n  \"errors\",\n  \"failures\",\n  \"name\",\n  \"tests\",\n  \"time\"\n};\n\n// The list of reserved attributes used in the <testcase> element of XML output.\nstatic const char* const kReservedTestCaseAttributes[] = {\n  \"classname\",\n  \"name\",\n  \"status\",\n  \"time\",\n  \"type_param\",\n  \"value_param\"\n};\n\ntemplate <int kSize>\nstd::vector<std::string> ArrayAsVector(const char* const (&array)[kSize]) {\n  return std::vector<std::string>(array, array + kSize);\n}\n\nstatic std::vector<std::string> GetReservedAttributesForElement(\n    const std::string& xml_element) {\n  if (xml_element == \"testsuites\") {\n    return ArrayAsVector(kReservedTestSuitesAttributes);\n  } else if (xml_element == \"testsuite\") {\n    return ArrayAsVector(kReservedTestSuiteAttributes);\n  } else if (xml_element == \"testcase\") {\n    return ArrayAsVector(kReservedTestCaseAttributes);\n  } else {\n    GTEST_CHECK_(false) << \"Unrecognized xml_element provided: \" << xml_element;\n  }\n  // This code is unreachable but some compilers may not realizes that.\n  return std::vector<std::string>();\n}\n\nstatic std::string FormatWordList(const std::vector<std::string>& words) {\n  Message word_list;\n  for (size_t i = 0; i < words.size(); ++i) {\n    if (i > 0 && words.size() > 2) {\n      word_list << \", \";\n    }\n    if (i == words.size() - 1) {\n      word_list << \"and \";\n    }\n    word_list << \"'\" << words[i] << \"'\";\n  }\n  return word_list.GetString();\n}\n\nbool ValidateTestPropertyName(const std::string& property_name,\n                              const std::vector<std::string>& reserved_names) {\n  if (std::find(reserved_names.begin(), reserved_names.end(), property_name) !=\n          reserved_names.end()) {\n    ADD_FAILURE() << \"Reserved key used in RecordProperty(): \" << property_name\n                  << \" (\" << FormatWordList(reserved_names)\n                  << \" are reserved by \" << GTEST_NAME_ << \")\";\n    return false;\n  }\n  return true;\n}\n\n// Adds a failure if the key is a reserved attribute of the element named\n// xml_element.  Returns true if the property is valid.\nbool TestResult::ValidateTestProperty(const std::string& xml_element,\n                                      const TestProperty& test_property) {\n  return ValidateTestPropertyName(test_property.key(),\n                                  GetReservedAttributesForElement(xml_element));\n}\n\n// Clears the object.\nvoid TestResult::Clear() {\n  test_part_results_.clear();\n  test_properties_.clear();\n  death_test_count_ = 0;\n  elapsed_time_ = 0;\n}\n\n// Returns true iff the test failed.\nbool TestResult::Failed() const {\n  for (int i = 0; i < total_part_count(); ++i) {\n    if (GetTestPartResult(i).failed())\n      return true;\n  }\n  return false;\n}\n\n// Returns true iff the test part fatally failed.\nstatic bool TestPartFatallyFailed(const TestPartResult& result) {\n  return result.fatally_failed();\n}\n\n// Returns true iff the test fatally failed.\nbool TestResult::HasFatalFailure() const {\n  return CountIf(test_part_results_, TestPartFatallyFailed) > 0;\n}\n\n// Returns true iff the test part non-fatally failed.\nstatic bool TestPartNonfatallyFailed(const TestPartResult& result) {\n  return result.nonfatally_failed();\n}\n\n// Returns true iff the test has a non-fatal failure.\nbool TestResult::HasNonfatalFailure() const {\n  return CountIf(test_part_results_, TestPartNonfatallyFailed) > 0;\n}\n\n// Gets the number of all test parts.  This is the sum of the number\n// of successful test parts and the number of failed test parts.\nint TestResult::total_part_count() const {\n  return static_cast<int>(test_part_results_.size());\n}\n\n// Returns the number of the test properties.\nint TestResult::test_property_count() const {\n  return static_cast<int>(test_properties_.size());\n}\n\n// class Test\n\n// Creates a Test object.\n\n// The c'tor saves the states of all flags.\nTest::Test()\n    : gtest_flag_saver_(new GTEST_FLAG_SAVER_) {\n}\n\n// The d'tor restores the states of all flags.  The actual work is\n// done by the d'tor of the gtest_flag_saver_ field, and thus not\n// visible here.\nTest::~Test() {\n}\n\n// Sets up the test fixture.\n//\n// A sub-class may override this.\nvoid Test::SetUp() {\n}\n\n// Tears down the test fixture.\n//\n// A sub-class may override this.\nvoid Test::TearDown() {\n}\n\n// Allows user supplied key value pairs to be recorded for later output.\nvoid Test::RecordProperty(const std::string& key, const std::string& value) {\n  UnitTest::GetInstance()->RecordProperty(key, value);\n}\n\n// Allows user supplied key value pairs to be recorded for later output.\nvoid Test::RecordProperty(const std::string& key, int value) {\n  Message value_message;\n  value_message << value;\n  RecordProperty(key, value_message.GetString().c_str());\n}\n\nnamespace internal {\n\nvoid ReportFailureInUnknownLocation(TestPartResult::Type result_type,\n                                    const std::string& message) {\n  // This function is a friend of UnitTest and as such has access to\n  // AddTestPartResult.\n  UnitTest::GetInstance()->AddTestPartResult(\n      result_type,\n      NULL,  // No info about the source file where the exception occurred.\n      -1,    // We have no info on which line caused the exception.\n      message,\n      \"\");   // No stack trace, either.\n}\n\n}  // namespace internal\n\n// Google Test requires all tests in the same test case to use the same test\n// fixture class.  This function checks if the current test has the\n// same fixture class as the first test in the current test case.  If\n// yes, it returns true; otherwise it generates a Google Test failure and\n// returns false.\nbool Test::HasSameFixtureClass() {\n  internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();\n  const TestCase* const test_case = impl->current_test_case();\n\n  // Info about the first test in the current test case.\n  const TestInfo* const first_test_info = test_case->test_info_list()[0];\n  const internal::TypeId first_fixture_id = first_test_info->fixture_class_id_;\n  const char* const first_test_name = first_test_info->name();\n\n  // Info about the current test.\n  const TestInfo* const this_test_info = impl->current_test_info();\n  const internal::TypeId this_fixture_id = this_test_info->fixture_class_id_;\n  const char* const this_test_name = this_test_info->name();\n\n  if (this_fixture_id != first_fixture_id) {\n    // Is the first test defined using TEST?\n    const bool first_is_TEST = first_fixture_id == internal::GetTestTypeId();\n    // Is this test defined using TEST?\n    const bool this_is_TEST = this_fixture_id == internal::GetTestTypeId();\n\n    if (first_is_TEST || this_is_TEST) {\n      // Both TEST and TEST_F appear in same test case, which is incorrect.\n      // Tell the user how to fix this.\n\n      // Gets the name of the TEST and the name of the TEST_F.  Note\n      // that first_is_TEST and this_is_TEST cannot both be true, as\n      // the fixture IDs are different for the two tests.\n      const char* const TEST_name =\n          first_is_TEST ? first_test_name : this_test_name;\n      const char* const TEST_F_name =\n          first_is_TEST ? this_test_name : first_test_name;\n\n      ADD_FAILURE()\n          << \"All tests in the same test case must use the same test fixture\\n\"\n          << \"class, so mixing TEST_F and TEST in the same test case is\\n\"\n          << \"illegal.  In test case \" << this_test_info->test_case_name()\n          << \",\\n\"\n          << \"test \" << TEST_F_name << \" is defined using TEST_F but\\n\"\n          << \"test \" << TEST_name << \" is defined using TEST.  You probably\\n\"\n          << \"want to change the TEST to TEST_F or move it to another test\\n\"\n          << \"case.\";\n    } else {\n      // Two fixture classes with the same name appear in two different\n      // namespaces, which is not allowed. Tell the user how to fix this.\n      ADD_FAILURE()\n          << \"All tests in the same test case must use the same test fixture\\n\"\n          << \"class.  However, in test case \"\n          << this_test_info->test_case_name() << \",\\n\"\n          << \"you defined test \" << first_test_name\n          << \" and test \" << this_test_name << \"\\n\"\n          << \"using two different test fixture classes.  This can happen if\\n\"\n          << \"the two classes are from different namespaces or translation\\n\"\n          << \"units and have the same name.  You should probably rename one\\n\"\n          << \"of the classes to put the tests into different test cases.\";\n    }\n    return false;\n  }\n\n  return true;\n}\n\n#if GTEST_HAS_SEH\n\n// Adds an \"exception thrown\" fatal failure to the current test.  This\n// function returns its result via an output parameter pointer because VC++\n// prohibits creation of objects with destructors on stack in functions\n// using __try (see error C2712).\nstatic std::string* FormatSehExceptionMessage(DWORD exception_code,\n                                              const char* location) {\n  Message message;\n  message << \"SEH exception with code 0x\" << std::setbase(16) <<\n    exception_code << std::setbase(10) << \" thrown in \" << location << \".\";\n\n  return new std::string(message.GetString());\n}\n\n#endif  // GTEST_HAS_SEH\n\nnamespace internal {\n\n#if GTEST_HAS_EXCEPTIONS\n\n// Adds an \"exception thrown\" fatal failure to the current test.\nstatic std::string FormatCxxExceptionMessage(const char* description,\n                                             const char* location) {\n  Message message;\n  if (description != NULL) {\n    message << \"C++ exception with description \\\"\" << description << \"\\\"\";\n  } else {\n    message << \"Unknown C++ exception\";\n  }\n  message << \" thrown in \" << location << \".\";\n\n  return message.GetString();\n}\n\nstatic std::string PrintTestPartResultToString(\n    const TestPartResult& test_part_result);\n\nGoogleTestFailureException::GoogleTestFailureException(\n    const TestPartResult& failure)\n    : ::std::runtime_error(PrintTestPartResultToString(failure).c_str()) {}\n\n#endif  // GTEST_HAS_EXCEPTIONS\n\n// We put these helper functions in the internal namespace as IBM's xlC\n// compiler rejects the code if they were declared static.\n\n// Runs the given method and handles SEH exceptions it throws, when\n// SEH is supported; returns the 0-value for type Result in case of an\n// SEH exception.  (Microsoft compilers cannot handle SEH and C++\n// exceptions in the same function.  Therefore, we provide a separate\n// wrapper function for handling SEH exceptions.)\ntemplate <class T, typename Result>\nResult HandleSehExceptionsInMethodIfSupported(\n    T* object, Result (T::*method)(), const char* location) {\n#if GTEST_HAS_SEH\n  __try {\n    return (object->*method)();\n  } __except (internal::UnitTestOptions::GTestShouldProcessSEH(  // NOLINT\n      GetExceptionCode())) {\n    // We create the exception message on the heap because VC++ prohibits\n    // creation of objects with destructors on stack in functions using __try\n    // (see error C2712).\n    std::string* exception_message = FormatSehExceptionMessage(\n        GetExceptionCode(), location);\n    internal::ReportFailureInUnknownLocation(TestPartResult::kFatalFailure,\n                                             *exception_message);\n    delete exception_message;\n    return static_cast<Result>(0);\n  }\n#else\n  (void)location;\n  return (object->*method)();\n#endif  // GTEST_HAS_SEH\n}\n\n// Runs the given method and catches and reports C++ and/or SEH-style\n// exceptions, if they are supported; returns the 0-value for type\n// Result in case of an SEH exception.\ntemplate <class T, typename Result>\nResult HandleExceptionsInMethodIfSupported(\n    T* object, Result (T::*method)(), const char* location) {\n  // NOTE: The user code can affect the way in which Google Test handles\n  // exceptions by setting GTEST_FLAG(catch_exceptions), but only before\n  // RUN_ALL_TESTS() starts. It is technically possible to check the flag\n  // after the exception is caught and either report or re-throw the\n  // exception based on the flag's value:\n  //\n  // try {\n  //   // Perform the test method.\n  // } catch (...) {\n  //   if (GTEST_FLAG(catch_exceptions))\n  //     // Report the exception as failure.\n  //   else\n  //     throw;  // Re-throws the original exception.\n  // }\n  //\n  // However, the purpose of this flag is to allow the program to drop into\n  // the debugger when the exception is thrown. On most platforms, once the\n  // control enters the catch block, the exception origin information is\n  // lost and the debugger will stop the program at the point of the\n  // re-throw in this function -- instead of at the point of the original\n  // throw statement in the code under test.  For this reason, we perform\n  // the check early, sacrificing the ability to affect Google Test's\n  // exception handling in the method where the exception is thrown.\n  if (internal::GetUnitTestImpl()->catch_exceptions()) {\n#if GTEST_HAS_EXCEPTIONS\n    try {\n      return HandleSehExceptionsInMethodIfSupported(object, method, location);\n    } catch (const internal::GoogleTestFailureException&) {  // NOLINT\n      // This exception type can only be thrown by a failed Google\n      // Test assertion with the intention of letting another testing\n      // framework catch it.  Therefore we just re-throw it.\n      throw;\n    } catch (const std::exception& e) {  // NOLINT\n      internal::ReportFailureInUnknownLocation(\n          TestPartResult::kFatalFailure,\n          FormatCxxExceptionMessage(e.what(), location));\n    } catch (...) {  // NOLINT\n      internal::ReportFailureInUnknownLocation(\n          TestPartResult::kFatalFailure,\n          FormatCxxExceptionMessage(NULL, location));\n    }\n    return static_cast<Result>(0);\n#else\n    return HandleSehExceptionsInMethodIfSupported(object, method, location);\n#endif  // GTEST_HAS_EXCEPTIONS\n  } else {\n    return (object->*method)();\n  }\n}\n\n}  // namespace internal\n\n// Runs the test and updates the test result.\nvoid Test::Run() {\n  if (!HasSameFixtureClass()) return;\n\n  internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();\n  impl->os_stack_trace_getter()->UponLeavingGTest();\n  internal::HandleExceptionsInMethodIfSupported(this, &Test::SetUp, \"SetUp()\");\n  // We will run the test only if SetUp() was successful.\n  if (!HasFatalFailure()) {\n    impl->os_stack_trace_getter()->UponLeavingGTest();\n    internal::HandleExceptionsInMethodIfSupported(\n        this, &Test::TestBody, \"the test body\");\n  }\n\n  // However, we want to clean up as much as possible.  Hence we will\n  // always call TearDown(), even if SetUp() or the test body has\n  // failed.\n  impl->os_stack_trace_getter()->UponLeavingGTest();\n  internal::HandleExceptionsInMethodIfSupported(\n      this, &Test::TearDown, \"TearDown()\");\n}\n\n// Returns true iff the current test has a fatal failure.\nbool Test::HasFatalFailure() {\n  return internal::GetUnitTestImpl()->current_test_result()->HasFatalFailure();\n}\n\n// Returns true iff the current test has a non-fatal failure.\nbool Test::HasNonfatalFailure() {\n  return internal::GetUnitTestImpl()->current_test_result()->\n      HasNonfatalFailure();\n}\n\n// class TestInfo\n\n// Constructs a TestInfo object. It assumes ownership of the test factory\n// object.\nTestInfo::TestInfo(const std::string& a_test_case_name,\n                   const std::string& a_name,\n                   const char* a_type_param,\n                   const char* a_value_param,\n                   internal::CodeLocation a_code_location,\n                   internal::TypeId fixture_class_id,\n                   internal::TestFactoryBase* factory)\n    : test_case_name_(a_test_case_name),\n      name_(a_name),\n      type_param_(a_type_param ? new std::string(a_type_param) : NULL),\n      value_param_(a_value_param ? new std::string(a_value_param) : NULL),\n      location_(a_code_location),\n      fixture_class_id_(fixture_class_id),\n      should_run_(false),\n      is_disabled_(false),\n      matches_filter_(false),\n      factory_(factory),\n      result_() {}\n\n// Destructs a TestInfo object.\nTestInfo::~TestInfo() { delete factory_; }\n\nnamespace internal {\n\n// Creates a new TestInfo object and registers it with Google Test;\n// returns the created object.\n//\n// Arguments:\n//\n//   test_case_name:   name of the test case\n//   name:             name of the test\n//   type_param:       the name of the test's type parameter, or NULL if\n//                     this is not a typed or a type-parameterized test.\n//   value_param:      text representation of the test's value parameter,\n//                     or NULL if this is not a value-parameterized test.\n//   code_location:    code location where the test is defined\n//   fixture_class_id: ID of the test fixture class\n//   set_up_tc:        pointer to the function that sets up the test case\n//   tear_down_tc:     pointer to the function that tears down the test case\n//   factory:          pointer to the factory that creates a test object.\n//                     The newly created TestInfo instance will assume\n//                     ownership of the factory object.\nTestInfo* MakeAndRegisterTestInfo(\n    const char* test_case_name,\n    const char* name,\n    const char* type_param,\n    const char* value_param,\n    CodeLocation code_location,\n    TypeId fixture_class_id,\n    SetUpTestCaseFunc set_up_tc,\n    TearDownTestCaseFunc tear_down_tc,\n    TestFactoryBase* factory) {\n  TestInfo* const test_info =\n      new TestInfo(test_case_name, name, type_param, value_param,\n                   code_location, fixture_class_id, factory);\n  GetUnitTestImpl()->AddTestInfo(set_up_tc, tear_down_tc, test_info);\n  return test_info;\n}\n\n#if GTEST_HAS_PARAM_TEST\nvoid ReportInvalidTestCaseType(const char* test_case_name,\n                               CodeLocation code_location) {\n  Message errors;\n  errors\n      << \"Attempted redefinition of test case \" << test_case_name << \".\\n\"\n      << \"All tests in the same test case must use the same test fixture\\n\"\n      << \"class.  However, in test case \" << test_case_name << \", you tried\\n\"\n      << \"to define a test using a fixture class different from the one\\n\"\n      << \"used earlier. This can happen if the two fixture classes are\\n\"\n      << \"from different namespaces and have the same name. You should\\n\"\n      << \"probably rename one of the classes to put the tests into different\\n\"\n      << \"test cases.\";\n\n  fprintf(stderr, \"%s %s\",\n          FormatFileLocation(code_location.file.c_str(),\n                             code_location.line).c_str(),\n          errors.GetString().c_str());\n}\n#endif  // GTEST_HAS_PARAM_TEST\n\n}  // namespace internal\n\nnamespace {\n\n// A predicate that checks the test name of a TestInfo against a known\n// value.\n//\n// This is used for implementation of the TestCase class only.  We put\n// it in the anonymous namespace to prevent polluting the outer\n// namespace.\n//\n// TestNameIs is copyable.\nclass TestNameIs {\n public:\n  // Constructor.\n  //\n  // TestNameIs has NO default constructor.\n  explicit TestNameIs(const char* name)\n      : name_(name) {}\n\n  // Returns true iff the test name of test_info matches name_.\n  bool operator()(const TestInfo * test_info) const {\n    return test_info && test_info->name() == name_;\n  }\n\n private:\n  std::string name_;\n};\n\n}  // namespace\n\nnamespace internal {\n\n// This method expands all parameterized tests registered with macros TEST_P\n// and INSTANTIATE_TEST_CASE_P into regular tests and registers those.\n// This will be done just once during the program runtime.\nvoid UnitTestImpl::RegisterParameterizedTests() {\n#if GTEST_HAS_PARAM_TEST\n  if (!parameterized_tests_registered_) {\n    parameterized_test_registry_.RegisterTests();\n    parameterized_tests_registered_ = true;\n  }\n#endif\n}\n\n}  // namespace internal\n\n// Creates the test object, runs it, records its result, and then\n// deletes it.\nvoid TestInfo::Run() {\n  if (!should_run_) return;\n\n  // Tells UnitTest where to store test result.\n  internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();\n  impl->set_current_test_info(this);\n\n  TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater();\n\n  // Notifies the unit test event listeners that a test is about to start.\n  repeater->OnTestStart(*this);\n\n  const TimeInMillis start = internal::GetTimeInMillis();\n\n  impl->os_stack_trace_getter()->UponLeavingGTest();\n\n  // Creates the test object.\n  Test* const test = internal::HandleExceptionsInMethodIfSupported(\n      factory_, &internal::TestFactoryBase::CreateTest,\n      \"the test fixture's constructor\");\n\n  // Runs the test only if the test object was created and its\n  // constructor didn't generate a fatal failure.\n  if ((test != NULL) && !Test::HasFatalFailure()) {\n    // This doesn't throw as all user code that can throw are wrapped into\n    // exception handling code.\n    test->Run();\n  }\n\n  // Deletes the test object.\n  impl->os_stack_trace_getter()->UponLeavingGTest();\n  internal::HandleExceptionsInMethodIfSupported(\n      test, &Test::DeleteSelf_, \"the test fixture's destructor\");\n\n  result_.set_elapsed_time(internal::GetTimeInMillis() - start);\n\n  // Notifies the unit test event listener that a test has just finished.\n  repeater->OnTestEnd(*this);\n\n  // Tells UnitTest to stop associating assertion results to this\n  // test.\n  impl->set_current_test_info(NULL);\n}\n\n// class TestCase\n\n// Gets the number of successful tests in this test case.\nint TestCase::successful_test_count() const {\n  return CountIf(test_info_list_, TestPassed);\n}\n\n// Gets the number of failed tests in this test case.\nint TestCase::failed_test_count() const {\n  return CountIf(test_info_list_, TestFailed);\n}\n\n// Gets the number of disabled tests that will be reported in the XML report.\nint TestCase::reportable_disabled_test_count() const {\n  return CountIf(test_info_list_, TestReportableDisabled);\n}\n\n// Gets the number of disabled tests in this test case.\nint TestCase::disabled_test_count() const {\n  return CountIf(test_info_list_, TestDisabled);\n}\n\n// Gets the number of tests to be printed in the XML report.\nint TestCase::reportable_test_count() const {\n  return CountIf(test_info_list_, TestReportable);\n}\n\n// Get the number of tests in this test case that should run.\nint TestCase::test_to_run_count() const {\n  return CountIf(test_info_list_, ShouldRunTest);\n}\n\n// Gets the number of all tests.\nint TestCase::total_test_count() const {\n  return static_cast<int>(test_info_list_.size());\n}\n\n// Creates a TestCase with the given name.\n//\n// Arguments:\n//\n//   name:         name of the test case\n//   a_type_param: the name of the test case's type parameter, or NULL if\n//                 this is not a typed or a type-parameterized test case.\n//   set_up_tc:    pointer to the function that sets up the test case\n//   tear_down_tc: pointer to the function that tears down the test case\nTestCase::TestCase(const char* a_name, const char* a_type_param,\n                   Test::SetUpTestCaseFunc set_up_tc,\n                   Test::TearDownTestCaseFunc tear_down_tc)\n    : name_(a_name),\n      type_param_(a_type_param ? new std::string(a_type_param) : NULL),\n      set_up_tc_(set_up_tc),\n      tear_down_tc_(tear_down_tc),\n      should_run_(false),\n      elapsed_time_(0) {\n}\n\n// Destructor of TestCase.\nTestCase::~TestCase() {\n  // Deletes every Test in the collection.\n  ForEach(test_info_list_, internal::Delete<TestInfo>);\n}\n\n// Returns the i-th test among all the tests. i can range from 0 to\n// total_test_count() - 1. If i is not in that range, returns NULL.\nconst TestInfo* TestCase::GetTestInfo(int i) const {\n  const int index = GetElementOr(test_indices_, i, -1);\n  return index < 0 ? NULL : test_info_list_[index];\n}\n\n// Returns the i-th test among all the tests. i can range from 0 to\n// total_test_count() - 1. If i is not in that range, returns NULL.\nTestInfo* TestCase::GetMutableTestInfo(int i) {\n  const int index = GetElementOr(test_indices_, i, -1);\n  return index < 0 ? NULL : test_info_list_[index];\n}\n\n// Adds a test to this test case.  Will delete the test upon\n// destruction of the TestCase object.\nvoid TestCase::AddTestInfo(TestInfo * test_info) {\n  test_info_list_.push_back(test_info);\n  test_indices_.push_back(static_cast<int>(test_indices_.size()));\n}\n\n// Runs every test in this TestCase.\nvoid TestCase::Run() {\n  if (!should_run_) return;\n\n  internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();\n  impl->set_current_test_case(this);\n\n  TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater();\n\n  repeater->OnTestCaseStart(*this);\n  impl->os_stack_trace_getter()->UponLeavingGTest();\n  internal::HandleExceptionsInMethodIfSupported(\n      this, &TestCase::RunSetUpTestCase, \"SetUpTestCase()\");\n\n  const internal::TimeInMillis start = internal::GetTimeInMillis();\n  for (int i = 0; i < total_test_count(); i++) {\n    GetMutableTestInfo(i)->Run();\n  }\n  elapsed_time_ = internal::GetTimeInMillis() - start;\n\n  impl->os_stack_trace_getter()->UponLeavingGTest();\n  internal::HandleExceptionsInMethodIfSupported(\n      this, &TestCase::RunTearDownTestCase, \"TearDownTestCase()\");\n\n  repeater->OnTestCaseEnd(*this);\n  impl->set_current_test_case(NULL);\n}\n\n// Clears the results of all tests in this test case.\nvoid TestCase::ClearResult() {\n  ad_hoc_test_result_.Clear();\n  ForEach(test_info_list_, TestInfo::ClearTestResult);\n}\n\n// Shuffles the tests in this test case.\nvoid TestCase::ShuffleTests(internal::Random* random) {\n  Shuffle(random, &test_indices_);\n}\n\n// Restores the test order to before the first shuffle.\nvoid TestCase::UnshuffleTests() {\n  for (size_t i = 0; i < test_indices_.size(); i++) {\n    test_indices_[i] = static_cast<int>(i);\n  }\n}\n\n// Formats a countable noun.  Depending on its quantity, either the\n// singular form or the plural form is used. e.g.\n//\n// FormatCountableNoun(1, \"formula\", \"formuli\") returns \"1 formula\".\n// FormatCountableNoun(5, \"book\", \"books\") returns \"5 books\".\nstatic std::string FormatCountableNoun(int count,\n                                       const char * singular_form,\n                                       const char * plural_form) {\n  return internal::StreamableToString(count) + \" \" +\n      (count == 1 ? singular_form : plural_form);\n}\n\n// Formats the count of tests.\nstatic std::string FormatTestCount(int test_count) {\n  return FormatCountableNoun(test_count, \"test\", \"tests\");\n}\n\n// Formats the count of test cases.\nstatic std::string FormatTestCaseCount(int test_case_count) {\n  return FormatCountableNoun(test_case_count, \"test case\", \"test cases\");\n}\n\n// Converts a TestPartResult::Type enum to human-friendly string\n// representation.  Both kNonFatalFailure and kFatalFailure are translated\n// to \"Failure\", as the user usually doesn't care about the difference\n// between the two when viewing the test result.\nstatic const char * TestPartResultTypeToString(TestPartResult::Type type) {\n  switch (type) {\n    case TestPartResult::kSuccess:\n      return \"Success\";\n\n    case TestPartResult::kNonFatalFailure:\n    case TestPartResult::kFatalFailure:\n#ifdef _MSC_VER\n      return \"error: \";\n#else\n      return \"Failure\\n\";\n#endif\n    default:\n      return \"Unknown result type\";\n  }\n}\n\nnamespace internal {\n\n// Prints a TestPartResult to an std::string.\nstatic std::string PrintTestPartResultToString(\n    const TestPartResult& test_part_result) {\n  return (Message()\n          << internal::FormatFileLocation(test_part_result.file_name(),\n                                          test_part_result.line_number())\n          << \" \" << TestPartResultTypeToString(test_part_result.type())\n          << test_part_result.message()).GetString();\n}\n\n// Prints a TestPartResult.\nstatic void PrintTestPartResult(const TestPartResult& test_part_result) {\n  const std::string& result =\n      PrintTestPartResultToString(test_part_result);\n  printf(\"%s\\n\", result.c_str());\n  fflush(stdout);\n  // If the test program runs in Visual Studio or a debugger, the\n  // following statements add the test part result message to the Output\n  // window such that the user can double-click on it to jump to the\n  // corresponding source code location; otherwise they do nothing.\n#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE\n  // We don't call OutputDebugString*() on Windows Mobile, as printing\n  // to stdout is done by OutputDebugString() there already - we don't\n  // want the same message printed twice.\n  ::OutputDebugStringA(result.c_str());\n  ::OutputDebugStringA(\"\\n\");\n#endif\n}\n\n// class PrettyUnitTestResultPrinter\n\nenum GTestColor {\n  COLOR_DEFAULT,\n  COLOR_RED,\n  COLOR_GREEN,\n  COLOR_YELLOW\n};\n\n#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE && \\\n    !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT\n\n// Returns the character attribute for the given color.\nWORD GetColorAttribute(GTestColor color) {\n  switch (color) {\n    case COLOR_RED:    return FOREGROUND_RED;\n    case COLOR_GREEN:  return FOREGROUND_GREEN;\n    case COLOR_YELLOW: return FOREGROUND_RED | FOREGROUND_GREEN;\n    default:           return 0;\n  }\n}\n\n#else\n\n// Returns the ANSI color code for the given color.  COLOR_DEFAULT is\n// an invalid input.\nconst char* GetAnsiColorCode(GTestColor color) {\n  switch (color) {\n    case COLOR_RED:     return \"1\";\n    case COLOR_GREEN:   return \"2\";\n    case COLOR_YELLOW:  return \"3\";\n    default:            return NULL;\n  };\n}\n\n#endif  // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE\n\n// Returns true iff Google Test should use colors in the output.\nbool ShouldUseColor(bool stdout_is_tty) {\n  const char* const gtest_color = GTEST_FLAG(color).c_str();\n\n  if (String::CaseInsensitiveCStringEquals(gtest_color, \"auto\")) {\n#if GTEST_OS_WINDOWS\n    // On Windows the TERM variable is usually not set, but the\n    // console there does support colors.\n    return stdout_is_tty;\n#else\n    // On non-Windows platforms, we rely on the TERM variable.\n    const char* const term = posix::GetEnv(\"TERM\");\n    const bool term_supports_color =\n        String::CStringEquals(term, \"xterm\") ||\n        String::CStringEquals(term, \"xterm-color\") ||\n        String::CStringEquals(term, \"xterm-256color\") ||\n        String::CStringEquals(term, \"screen\") ||\n        String::CStringEquals(term, \"screen-256color\") ||\n        String::CStringEquals(term, \"tmux\") ||\n        String::CStringEquals(term, \"tmux-256color\") ||\n        String::CStringEquals(term, \"rxvt-unicode\") ||\n        String::CStringEquals(term, \"rxvt-unicode-256color\") ||\n        String::CStringEquals(term, \"linux\") ||\n        String::CStringEquals(term, \"cygwin\");\n    return stdout_is_tty && term_supports_color;\n#endif  // GTEST_OS_WINDOWS\n  }\n\n  return String::CaseInsensitiveCStringEquals(gtest_color, \"yes\") ||\n      String::CaseInsensitiveCStringEquals(gtest_color, \"true\") ||\n      String::CaseInsensitiveCStringEquals(gtest_color, \"t\") ||\n      String::CStringEquals(gtest_color, \"1\");\n  // We take \"yes\", \"true\", \"t\", and \"1\" as meaning \"yes\".  If the\n  // value is neither one of these nor \"auto\", we treat it as \"no\" to\n  // be conservative.\n}\n\n// Helpers for printing colored strings to stdout. Note that on Windows, we\n// cannot simply emit special characters and have the terminal change colors.\n// This routine must actually emit the characters rather than return a string\n// that would be colored when printed, as can be done on Linux.\nGTEST_ATTRIBUTE_PRINTF_(2, 3)\nvoid ColoredPrintf(GTestColor color, const char* fmt, ...) {\n  va_list args;\n  va_start(args, fmt);\n\n#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_SYMBIAN || GTEST_OS_ZOS || \\\n    GTEST_OS_IOS || GTEST_OS_WINDOWS_PHONE || GTEST_OS_WINDOWS_RT\n  const bool use_color = AlwaysFalse();\n#else\n  static const bool in_color_mode =\n      ShouldUseColor(posix::IsATTY(posix::FileNo(stdout)) != 0);\n  const bool use_color = in_color_mode && (color != COLOR_DEFAULT);\n#endif  // GTEST_OS_WINDOWS_MOBILE || GTEST_OS_SYMBIAN || GTEST_OS_ZOS\n  // The '!= 0' comparison is necessary to satisfy MSVC 7.1.\n\n  if (!use_color) {\n    vprintf(fmt, args);\n    va_end(args);\n    return;\n  }\n\n#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE && \\\n    !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT\n  const HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);\n\n  // Gets the current text color.\n  CONSOLE_SCREEN_BUFFER_INFO buffer_info;\n  GetConsoleScreenBufferInfo(stdout_handle, &buffer_info);\n  const WORD old_color_attrs = buffer_info.wAttributes;\n\n  // We need to flush the stream buffers into the console before each\n  // SetConsoleTextAttribute call lest it affect the text that is already\n  // printed but has not yet reached the console.\n  fflush(stdout);\n  SetConsoleTextAttribute(stdout_handle,\n                          GetColorAttribute(color) | FOREGROUND_INTENSITY);\n  vprintf(fmt, args);\n\n  fflush(stdout);\n  // Restores the text color.\n  SetConsoleTextAttribute(stdout_handle, old_color_attrs);\n#else\n  printf(\"\\033[0;3%sm\", GetAnsiColorCode(color));\n  vprintf(fmt, args);\n  printf(\"\\033[m\");  // Resets the terminal to default.\n#endif  // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MOBILE\n  va_end(args);\n}\n\n// Text printed in Google Test's text output and --gunit_list_tests\n// output to label the type parameter and value parameter for a test.\nstatic const char kTypeParamLabel[] = \"TypeParam\";\nstatic const char kValueParamLabel[] = \"GetParam()\";\n\nvoid PrintFullTestCommentIfPresent(const TestInfo& test_info) {\n  const char* const type_param = test_info.type_param();\n  const char* const value_param = test_info.value_param();\n\n  if (type_param != NULL || value_param != NULL) {\n    printf(\", where \");\n    if (type_param != NULL) {\n      printf(\"%s = %s\", kTypeParamLabel, type_param);\n      if (value_param != NULL)\n        printf(\" and \");\n    }\n    if (value_param != NULL) {\n      printf(\"%s = %s\", kValueParamLabel, value_param);\n    }\n  }\n}\n\n// This class implements the TestEventListener interface.\n//\n// Class PrettyUnitTestResultPrinter is copyable.\nclass PrettyUnitTestResultPrinter : public TestEventListener {\n public:\n  PrettyUnitTestResultPrinter() {}\n  static void PrintTestName(const char * test_case, const char * test) {\n    printf(\"%s.%s\", test_case, test);\n  }\n\n  // The following methods override what's in the TestEventListener class.\n  virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) {}\n  virtual void OnTestIterationStart(const UnitTest& unit_test, int iteration);\n  virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test);\n  virtual void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) {}\n  virtual void OnTestCaseStart(const TestCase& test_case);\n  virtual void OnTestStart(const TestInfo& test_info);\n  virtual void OnTestPartResult(const TestPartResult& result);\n  virtual void OnTestEnd(const TestInfo& test_info);\n  virtual void OnTestCaseEnd(const TestCase& test_case);\n  virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test);\n  virtual void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) {}\n  virtual void OnTestIterationEnd(const UnitTest& unit_test, int iteration);\n  virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) {}\n\n private:\n  static void PrintFailedTests(const UnitTest& unit_test);\n};\n\n  // Fired before each iteration of tests starts.\nvoid PrettyUnitTestResultPrinter::OnTestIterationStart(\n    const UnitTest& unit_test, int iteration) {\n  if (GTEST_FLAG(repeat) != 1)\n    printf(\"\\nRepeating all tests (iteration %d) . . .\\n\\n\", iteration + 1);\n\n  const char* const filter = GTEST_FLAG(filter).c_str();\n\n  // Prints the filter if it's not *.  This reminds the user that some\n  // tests may be skipped.\n  if (!String::CStringEquals(filter, kUniversalFilter)) {\n    ColoredPrintf(COLOR_YELLOW,\n                  \"Note: %s filter = %s\\n\", GTEST_NAME_, filter);\n  }\n\n  if (internal::ShouldShard(kTestTotalShards, kTestShardIndex, false)) {\n    const Int32 shard_index = Int32FromEnvOrDie(kTestShardIndex, -1);\n    ColoredPrintf(COLOR_YELLOW,\n                  \"Note: This is test shard %d of %s.\\n\",\n                  static_cast<int>(shard_index) + 1,\n                  internal::posix::GetEnv(kTestTotalShards));\n  }\n\n  if (GTEST_FLAG(shuffle)) {\n    ColoredPrintf(COLOR_YELLOW,\n                  \"Note: Randomizing tests' orders with a seed of %d .\\n\",\n                  unit_test.random_seed());\n  }\n\n  ColoredPrintf(COLOR_GREEN,  \"[==========] \");\n  printf(\"Running %s from %s.\\n\",\n         FormatTestCount(unit_test.test_to_run_count()).c_str(),\n         FormatTestCaseCount(unit_test.test_case_to_run_count()).c_str());\n  fflush(stdout);\n}\n\nvoid PrettyUnitTestResultPrinter::OnEnvironmentsSetUpStart(\n    const UnitTest& /*unit_test*/) {\n  ColoredPrintf(COLOR_GREEN,  \"[----------] \");\n  printf(\"Global test environment set-up.\\n\");\n  fflush(stdout);\n}\n\nvoid PrettyUnitTestResultPrinter::OnTestCaseStart(const TestCase& test_case) {\n  const std::string counts =\n      FormatCountableNoun(test_case.test_to_run_count(), \"test\", \"tests\");\n  ColoredPrintf(COLOR_GREEN, \"[----------] \");\n  printf(\"%s from %s\", counts.c_str(), test_case.name());\n  if (test_case.type_param() == NULL) {\n    printf(\"\\n\");\n  } else {\n    printf(\", where %s = %s\\n\", kTypeParamLabel, test_case.type_param());\n  }\n  fflush(stdout);\n}\n\nvoid PrettyUnitTestResultPrinter::OnTestStart(const TestInfo& test_info) {\n  ColoredPrintf(COLOR_GREEN,  \"[ RUN      ] \");\n  PrintTestName(test_info.test_case_name(), test_info.name());\n  printf(\"\\n\");\n  fflush(stdout);\n}\n\n// Called after an assertion failure.\nvoid PrettyUnitTestResultPrinter::OnTestPartResult(\n    const TestPartResult& result) {\n  // If the test part succeeded, we don't need to do anything.\n  if (result.type() == TestPartResult::kSuccess)\n    return;\n\n  // Print failure message from the assertion (e.g. expected this and got that).\n  PrintTestPartResult(result);\n  fflush(stdout);\n}\n\nvoid PrettyUnitTestResultPrinter::OnTestEnd(const TestInfo& test_info) {\n  if (test_info.result()->Passed()) {\n    ColoredPrintf(COLOR_GREEN, \"[       OK ] \");\n  } else {\n    ColoredPrintf(COLOR_RED, \"[  FAILED  ] \");\n  }\n  PrintTestName(test_info.test_case_name(), test_info.name());\n  if (test_info.result()->Failed())\n    PrintFullTestCommentIfPresent(test_info);\n\n  if (GTEST_FLAG(print_time)) {\n    printf(\" (%s ms)\\n\", internal::StreamableToString(\n           test_info.result()->elapsed_time()).c_str());\n  } else {\n    printf(\"\\n\");\n  }\n  fflush(stdout);\n}\n\nvoid PrettyUnitTestResultPrinter::OnTestCaseEnd(const TestCase& test_case) {\n  if (!GTEST_FLAG(print_time)) return;\n\n  const std::string counts =\n      FormatCountableNoun(test_case.test_to_run_count(), \"test\", \"tests\");\n  ColoredPrintf(COLOR_GREEN, \"[----------] \");\n  printf(\"%s from %s (%s ms total)\\n\\n\",\n         counts.c_str(), test_case.name(),\n         internal::StreamableToString(test_case.elapsed_time()).c_str());\n  fflush(stdout);\n}\n\nvoid PrettyUnitTestResultPrinter::OnEnvironmentsTearDownStart(\n    const UnitTest& /*unit_test*/) {\n  ColoredPrintf(COLOR_GREEN,  \"[----------] \");\n  printf(\"Global test environment tear-down\\n\");\n  fflush(stdout);\n}\n\n// Internal helper for printing the list of failed tests.\nvoid PrettyUnitTestResultPrinter::PrintFailedTests(const UnitTest& unit_test) {\n  const int failed_test_count = unit_test.failed_test_count();\n  if (failed_test_count == 0) {\n    return;\n  }\n\n  for (int i = 0; i < unit_test.total_test_case_count(); ++i) {\n    const TestCase& test_case = *unit_test.GetTestCase(i);\n    if (!test_case.should_run() || (test_case.failed_test_count() == 0)) {\n      continue;\n    }\n    for (int j = 0; j < test_case.total_test_count(); ++j) {\n      const TestInfo& test_info = *test_case.GetTestInfo(j);\n      if (!test_info.should_run() || test_info.result()->Passed()) {\n        continue;\n      }\n      ColoredPrintf(COLOR_RED, \"[  FAILED  ] \");\n      printf(\"%s.%s\", test_case.name(), test_info.name());\n      PrintFullTestCommentIfPresent(test_info);\n      printf(\"\\n\");\n    }\n  }\n}\n\nvoid PrettyUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test,\n                                                     int /*iteration*/) {\n  ColoredPrintf(COLOR_GREEN,  \"[==========] \");\n  printf(\"%s from %s ran.\",\n         FormatTestCount(unit_test.test_to_run_count()).c_str(),\n         FormatTestCaseCount(unit_test.test_case_to_run_count()).c_str());\n  if (GTEST_FLAG(print_time)) {\n    printf(\" (%s ms total)\",\n           internal::StreamableToString(unit_test.elapsed_time()).c_str());\n  }\n  printf(\"\\n\");\n  ColoredPrintf(COLOR_GREEN,  \"[  PASSED  ] \");\n  printf(\"%s.\\n\", FormatTestCount(unit_test.successful_test_count()).c_str());\n\n  int num_failures = unit_test.failed_test_count();\n  if (!unit_test.Passed()) {\n    const int failed_test_count = unit_test.failed_test_count();\n    ColoredPrintf(COLOR_RED,  \"[  FAILED  ] \");\n    printf(\"%s, listed below:\\n\", FormatTestCount(failed_test_count).c_str());\n    PrintFailedTests(unit_test);\n    printf(\"\\n%2d FAILED %s\\n\", num_failures,\n                        num_failures == 1 ? \"TEST\" : \"TESTS\");\n  }\n\n  int num_disabled = unit_test.reportable_disabled_test_count();\n  if (num_disabled && !GTEST_FLAG(also_run_disabled_tests)) {\n    if (!num_failures) {\n      printf(\"\\n\");  // Add a spacer if no FAILURE banner is displayed.\n    }\n    ColoredPrintf(COLOR_YELLOW,\n                  \"  YOU HAVE %d DISABLED %s\\n\\n\",\n                  num_disabled,\n                  num_disabled == 1 ? \"TEST\" : \"TESTS\");\n  }\n  // Ensure that Google Test output is printed before, e.g., heapchecker output.\n  fflush(stdout);\n}\n\n// End PrettyUnitTestResultPrinter\n\n// class TestEventRepeater\n//\n// This class forwards events to other event listeners.\nclass TestEventRepeater : public TestEventListener {\n public:\n  TestEventRepeater() : forwarding_enabled_(true) {}\n  virtual ~TestEventRepeater();\n  void Append(TestEventListener *listener);\n  TestEventListener* Release(TestEventListener* listener);\n\n  // Controls whether events will be forwarded to listeners_. Set to false\n  // in death test child processes.\n  bool forwarding_enabled() const { return forwarding_enabled_; }\n  void set_forwarding_enabled(bool enable) { forwarding_enabled_ = enable; }\n\n  virtual void OnTestProgramStart(const UnitTest& unit_test);\n  virtual void OnTestIterationStart(const UnitTest& unit_test, int iteration);\n  virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test);\n  virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test);\n  virtual void OnTestCaseStart(const TestCase& test_case);\n  virtual void OnTestStart(const TestInfo& test_info);\n  virtual void OnTestPartResult(const TestPartResult& result);\n  virtual void OnTestEnd(const TestInfo& test_info);\n  virtual void OnTestCaseEnd(const TestCase& test_case);\n  virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test);\n  virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test);\n  virtual void OnTestIterationEnd(const UnitTest& unit_test, int iteration);\n  virtual void OnTestProgramEnd(const UnitTest& unit_test);\n\n private:\n  // Controls whether events will be forwarded to listeners_. Set to false\n  // in death test child processes.\n  bool forwarding_enabled_;\n  // The list of listeners that receive events.\n  std::vector<TestEventListener*> listeners_;\n\n  GTEST_DISALLOW_COPY_AND_ASSIGN_(TestEventRepeater);\n};\n\nTestEventRepeater::~TestEventRepeater() {\n  ForEach(listeners_, Delete<TestEventListener>);\n}\n\nvoid TestEventRepeater::Append(TestEventListener *listener) {\n  listeners_.push_back(listener);\n}\n\n// TODO(vladl@google.com): Factor the search functionality into Vector::Find.\nTestEventListener* TestEventRepeater::Release(TestEventListener *listener) {\n  for (size_t i = 0; i < listeners_.size(); ++i) {\n    if (listeners_[i] == listener) {\n      listeners_.erase(listeners_.begin() + i);\n      return listener;\n    }\n  }\n\n  return NULL;\n}\n\n// Since most methods are very similar, use macros to reduce boilerplate.\n// This defines a member that forwards the call to all listeners.\n#define GTEST_REPEATER_METHOD_(Name, Type) \\\nvoid TestEventRepeater::Name(const Type& parameter) { \\\n  if (forwarding_enabled_) { \\\n    for (size_t i = 0; i < listeners_.size(); i++) { \\\n      listeners_[i]->Name(parameter); \\\n    } \\\n  } \\\n}\n// This defines a member that forwards the call to all listeners in reverse\n// order.\n#define GTEST_REVERSE_REPEATER_METHOD_(Name, Type) \\\nvoid TestEventRepeater::Name(const Type& parameter) { \\\n  if (forwarding_enabled_) { \\\n    for (int i = static_cast<int>(listeners_.size()) - 1; i >= 0; i--) { \\\n      listeners_[i]->Name(parameter); \\\n    } \\\n  } \\\n}\n\nGTEST_REPEATER_METHOD_(OnTestProgramStart, UnitTest)\nGTEST_REPEATER_METHOD_(OnEnvironmentsSetUpStart, UnitTest)\nGTEST_REPEATER_METHOD_(OnTestCaseStart, TestCase)\nGTEST_REPEATER_METHOD_(OnTestStart, TestInfo)\nGTEST_REPEATER_METHOD_(OnTestPartResult, TestPartResult)\nGTEST_REPEATER_METHOD_(OnEnvironmentsTearDownStart, UnitTest)\nGTEST_REVERSE_REPEATER_METHOD_(OnEnvironmentsSetUpEnd, UnitTest)\nGTEST_REVERSE_REPEATER_METHOD_(OnEnvironmentsTearDownEnd, UnitTest)\nGTEST_REVERSE_REPEATER_METHOD_(OnTestEnd, TestInfo)\nGTEST_REVERSE_REPEATER_METHOD_(OnTestCaseEnd, TestCase)\nGTEST_REVERSE_REPEATER_METHOD_(OnTestProgramEnd, UnitTest)\n\n#undef GTEST_REPEATER_METHOD_\n#undef GTEST_REVERSE_REPEATER_METHOD_\n\nvoid TestEventRepeater::OnTestIterationStart(const UnitTest& unit_test,\n                                             int iteration) {\n  if (forwarding_enabled_) {\n    for (size_t i = 0; i < listeners_.size(); i++) {\n      listeners_[i]->OnTestIterationStart(unit_test, iteration);\n    }\n  }\n}\n\nvoid TestEventRepeater::OnTestIterationEnd(const UnitTest& unit_test,\n                                           int iteration) {\n  if (forwarding_enabled_) {\n    for (int i = static_cast<int>(listeners_.size()) - 1; i >= 0; i--) {\n      listeners_[i]->OnTestIterationEnd(unit_test, iteration);\n    }\n  }\n}\n\n// End TestEventRepeater\n\n// This class generates an XML output file.\nclass XmlUnitTestResultPrinter : public EmptyTestEventListener {\n public:\n  explicit XmlUnitTestResultPrinter(const char* output_file);\n\n  virtual void OnTestIterationEnd(const UnitTest& unit_test, int iteration);\n\n private:\n  // Is c a whitespace character that is normalized to a space character\n  // when it appears in an XML attribute value?\n  static bool IsNormalizableWhitespace(char c) {\n    return c == 0x9 || c == 0xA || c == 0xD;\n  }\n\n  // May c appear in a well-formed XML document?\n  static bool IsValidXmlCharacter(char c) {\n    return IsNormalizableWhitespace(c) || c >= 0x20;\n  }\n\n  // Returns an XML-escaped copy of the input string str.  If\n  // is_attribute is true, the text is meant to appear as an attribute\n  // value, and normalizable whitespace is preserved by replacing it\n  // with character references.\n  static std::string EscapeXml(const std::string& str, bool is_attribute);\n\n  // Returns the given string with all characters invalid in XML removed.\n  static std::string RemoveInvalidXmlCharacters(const std::string& str);\n\n  // Convenience wrapper around EscapeXml when str is an attribute value.\n  static std::string EscapeXmlAttribute(const std::string& str) {\n    return EscapeXml(str, true);\n  }\n\n  // Convenience wrapper around EscapeXml when str is not an attribute value.\n  static std::string EscapeXmlText(const char* str) {\n    return EscapeXml(str, false);\n  }\n\n  // Verifies that the given attribute belongs to the given element and\n  // streams the attribute as XML.\n  static void OutputXmlAttribute(std::ostream* stream,\n                                 const std::string& element_name,\n                                 const std::string& name,\n                                 const std::string& value);\n\n  // Streams an XML CDATA section, escaping invalid CDATA sequences as needed.\n  static void OutputXmlCDataSection(::std::ostream* stream, const char* data);\n\n  // Streams an XML representation of a TestInfo object.\n  static void OutputXmlTestInfo(::std::ostream* stream,\n                                const char* test_case_name,\n                                const TestInfo& test_info);\n\n  // Prints an XML representation of a TestCase object\n  static void PrintXmlTestCase(::std::ostream* stream,\n                               const TestCase& test_case);\n\n  // Prints an XML summary of unit_test to output stream out.\n  static void PrintXmlUnitTest(::std::ostream* stream,\n                               const UnitTest& unit_test);\n\n  // Produces a string representing the test properties in a result as space\n  // delimited XML attributes based on the property key=\"value\" pairs.\n  // When the std::string is not empty, it includes a space at the beginning,\n  // to delimit this attribute from prior attributes.\n  static std::string TestPropertiesAsXmlAttributes(const TestResult& result);\n\n  // The output file.\n  const std::string output_file_;\n\n  GTEST_DISALLOW_COPY_AND_ASSIGN_(XmlUnitTestResultPrinter);\n};\n\n// Creates a new XmlUnitTestResultPrinter.\nXmlUnitTestResultPrinter::XmlUnitTestResultPrinter(const char* output_file)\n    : output_file_(output_file) {\n  if (output_file_.c_str() == NULL || output_file_.empty()) {\n    fprintf(stderr, \"XML output file may not be null\\n\");\n    fflush(stderr);\n    exit(EXIT_FAILURE);\n  }\n}\n\n// Called after the unit test ends.\nvoid XmlUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test,\n                                                  int /*iteration*/) {\n  FILE* xmlout = NULL;\n  FilePath output_file(output_file_);\n  FilePath output_dir(output_file.RemoveFileName());\n\n  if (output_dir.CreateDirectoriesRecursively()) {\n    xmlout = posix::FOpen(output_file_.c_str(), \"w\");\n  }\n  if (xmlout == NULL) {\n    // TODO(wan): report the reason of the failure.\n    //\n    // We don't do it for now as:\n    //\n    //   1. There is no urgent need for it.\n    //   2. It's a bit involved to make the errno variable thread-safe on\n    //      all three operating systems (Linux, Windows, and Mac OS).\n    //   3. To interpret the meaning of errno in a thread-safe way,\n    //      we need the strerror_r() function, which is not available on\n    //      Windows.\n    fprintf(stderr,\n            \"Unable to open file \\\"%s\\\"\\n\",\n            output_file_.c_str());\n    fflush(stderr);\n    exit(EXIT_FAILURE);\n  }\n  std::stringstream stream;\n  PrintXmlUnitTest(&stream, unit_test);\n  fprintf(xmlout, \"%s\", StringStreamToString(&stream).c_str());\n  fclose(xmlout);\n}\n\n// Returns an XML-escaped copy of the input string str.  If is_attribute\n// is true, the text is meant to appear as an attribute value, and\n// normalizable whitespace is preserved by replacing it with character\n// references.\n//\n// Invalid XML characters in str, if any, are stripped from the output.\n// It is expected that most, if not all, of the text processed by this\n// module will consist of ordinary English text.\n// If this module is ever modified to produce version 1.1 XML output,\n// most invalid characters can be retained using character references.\n// TODO(wan): It might be nice to have a minimally invasive, human-readable\n// escaping scheme for invalid characters, rather than dropping them.\nstd::string XmlUnitTestResultPrinter::EscapeXml(\n    const std::string& str, bool is_attribute) {\n  Message m;\n\n  for (size_t i = 0; i < str.size(); ++i) {\n    const char ch = str[i];\n    switch (ch) {\n      case '<':\n        m << \"&lt;\";\n        break;\n      case '>':\n        m << \"&gt;\";\n        break;\n      case '&':\n        m << \"&amp;\";\n        break;\n      case '\\'':\n        if (is_attribute)\n          m << \"&apos;\";\n        else\n          m << '\\'';\n        break;\n      case '\"':\n        if (is_attribute)\n          m << \"&quot;\";\n        else\n          m << '\"';\n        break;\n      default:\n        if (IsValidXmlCharacter(ch)) {\n          if (is_attribute && IsNormalizableWhitespace(ch))\n            m << \"&#x\" << String::FormatByte(static_cast<unsigned char>(ch))\n              << \";\";\n          else\n            m << ch;\n        }\n        break;\n    }\n  }\n\n  return m.GetString();\n}\n\n// Returns the given string with all characters invalid in XML removed.\n// Currently invalid characters are dropped from the string. An\n// alternative is to replace them with certain characters such as . or ?.\nstd::string XmlUnitTestResultPrinter::RemoveInvalidXmlCharacters(\n    const std::string& str) {\n  std::string output;\n  output.reserve(str.size());\n  for (std::string::const_iterator it = str.begin(); it != str.end(); ++it)\n    if (IsValidXmlCharacter(*it))\n      output.push_back(*it);\n\n  return output;\n}\n\n// The following routines generate an XML representation of a UnitTest\n// object.\n//\n// This is how Google Test concepts map to the DTD:\n//\n// <testsuites name=\"AllTests\">        <-- corresponds to a UnitTest object\n//   <testsuite name=\"testcase-name\">  <-- corresponds to a TestCase object\n//     <testcase name=\"test-name\">     <-- corresponds to a TestInfo object\n//       <failure message=\"...\">...</failure>\n//       <failure message=\"...\">...</failure>\n//       <failure message=\"...\">...</failure>\n//                                     <-- individual assertion failures\n//     </testcase>\n//   </testsuite>\n// </testsuites>\n\n// Formats the given time in milliseconds as seconds.\nstd::string FormatTimeInMillisAsSeconds(TimeInMillis ms) {\n  ::std::stringstream ss;\n  ss << (static_cast<double>(ms) * 1e-3);\n  return ss.str();\n}\n\nstatic bool PortableLocaltime(time_t seconds, struct tm* out) {\n#if defined(_MSC_VER)\n  return localtime_s(out, &seconds) == 0;\n#elif defined(__MINGW32__) || defined(__MINGW64__)\n  // MINGW <time.h> provides neither localtime_r nor localtime_s, but uses\n  // Windows' localtime(), which has a thread-local tm buffer.\n  struct tm* tm_ptr = localtime(&seconds);  // NOLINT\n  if (tm_ptr == NULL)\n    return false;\n  *out = *tm_ptr;\n  return true;\n#else\n  return localtime_r(&seconds, out) != NULL;\n#endif\n}\n\n// Converts the given epoch time in milliseconds to a date string in the ISO\n// 8601 format, without the timezone information.\nstd::string FormatEpochTimeInMillisAsIso8601(TimeInMillis ms) {\n  struct tm time_struct;\n  if (!PortableLocaltime(static_cast<time_t>(ms / 1000), &time_struct))\n    return \"\";\n  // YYYY-MM-DDThh:mm:ss\n  return StreamableToString(time_struct.tm_year + 1900) + \"-\" +\n      String::FormatIntWidth2(time_struct.tm_mon + 1) + \"-\" +\n      String::FormatIntWidth2(time_struct.tm_mday) + \"T\" +\n      String::FormatIntWidth2(time_struct.tm_hour) + \":\" +\n      String::FormatIntWidth2(time_struct.tm_min) + \":\" +\n      String::FormatIntWidth2(time_struct.tm_sec);\n}\n\n// Streams an XML CDATA section, escaping invalid CDATA sequences as needed.\nvoid XmlUnitTestResultPrinter::OutputXmlCDataSection(::std::ostream* stream,\n                                                     const char* data) {\n  const char* segment = data;\n  *stream << \"<![CDATA[\";\n  for (;;) {\n    const char* const next_segment = strstr(segment, \"]]>\");\n    if (next_segment != NULL) {\n      stream->write(\n          segment, static_cast<std::streamsize>(next_segment - segment));\n      *stream << \"]]>]]&gt;<![CDATA[\";\n      segment = next_segment + strlen(\"]]>\");\n    } else {\n      *stream << segment;\n      break;\n    }\n  }\n  *stream << \"]]>\";\n}\n\nvoid XmlUnitTestResultPrinter::OutputXmlAttribute(\n    std::ostream* stream,\n    const std::string& element_name,\n    const std::string& name,\n    const std::string& value) {\n  const std::vector<std::string>& allowed_names =\n      GetReservedAttributesForElement(element_name);\n\n  GTEST_CHECK_(std::find(allowed_names.begin(), allowed_names.end(), name) !=\n                   allowed_names.end())\n      << \"Attribute \" << name << \" is not allowed for element <\" << element_name\n      << \">.\";\n\n  *stream << \" \" << name << \"=\\\"\" << EscapeXmlAttribute(value) << \"\\\"\";\n}\n\n// Prints an XML representation of a TestInfo object.\n// TODO(wan): There is also value in printing properties with the plain printer.\nvoid XmlUnitTestResultPrinter::OutputXmlTestInfo(::std::ostream* stream,\n                                                 const char* test_case_name,\n                                                 const TestInfo& test_info) {\n  const TestResult& result = *test_info.result();\n  const std::string kTestcase = \"testcase\";\n\n  *stream << \"    <testcase\";\n  OutputXmlAttribute(stream, kTestcase, \"name\", test_info.name());\n\n  if (test_info.value_param() != NULL) {\n    OutputXmlAttribute(stream, kTestcase, \"value_param\",\n                       test_info.value_param());\n  }\n  if (test_info.type_param() != NULL) {\n    OutputXmlAttribute(stream, kTestcase, \"type_param\", test_info.type_param());\n  }\n\n  OutputXmlAttribute(stream, kTestcase, \"status\",\n                     test_info.should_run() ? \"run\" : \"notrun\");\n  OutputXmlAttribute(stream, kTestcase, \"time\",\n                     FormatTimeInMillisAsSeconds(result.elapsed_time()));\n  OutputXmlAttribute(stream, kTestcase, \"classname\", test_case_name);\n  *stream << TestPropertiesAsXmlAttributes(result);\n\n  int failures = 0;\n  for (int i = 0; i < result.total_part_count(); ++i) {\n    const TestPartResult& part = result.GetTestPartResult(i);\n    if (part.failed()) {\n      if (++failures == 1) {\n        *stream << \">\\n\";\n      }\n      const std::string location =\n          internal::FormatCompilerIndependentFileLocation(part.file_name(),\n                                                          part.line_number());\n      const std::string summary = location + \"\\n\" + part.summary();\n      *stream << \"      <failure message=\\\"\"\n              << EscapeXmlAttribute(summary.c_str())\n              << \"\\\" type=\\\"\\\">\";\n      const std::string detail = location + \"\\n\" + part.message();\n      OutputXmlCDataSection(stream, RemoveInvalidXmlCharacters(detail).c_str());\n      *stream << \"</failure>\\n\";\n    }\n  }\n\n  if (failures == 0)\n    *stream << \" />\\n\";\n  else\n    *stream << \"    </testcase>\\n\";\n}\n\n// Prints an XML representation of a TestCase object\nvoid XmlUnitTestResultPrinter::PrintXmlTestCase(std::ostream* stream,\n                                                const TestCase& test_case) {\n  const std::string kTestsuite = \"testsuite\";\n  *stream << \"  <\" << kTestsuite;\n  OutputXmlAttribute(stream, kTestsuite, \"name\", test_case.name());\n  OutputXmlAttribute(stream, kTestsuite, \"tests\",\n                     StreamableToString(test_case.reportable_test_count()));\n  OutputXmlAttribute(stream, kTestsuite, \"failures\",\n                     StreamableToString(test_case.failed_test_count()));\n  OutputXmlAttribute(\n      stream, kTestsuite, \"disabled\",\n      StreamableToString(test_case.reportable_disabled_test_count()));\n  OutputXmlAttribute(stream, kTestsuite, \"errors\", \"0\");\n  OutputXmlAttribute(stream, kTestsuite, \"time\",\n                     FormatTimeInMillisAsSeconds(test_case.elapsed_time()));\n  *stream << TestPropertiesAsXmlAttributes(test_case.ad_hoc_test_result())\n          << \">\\n\";\n\n  for (int i = 0; i < test_case.total_test_count(); ++i) {\n    if (test_case.GetTestInfo(i)->is_reportable())\n      OutputXmlTestInfo(stream, test_case.name(), *test_case.GetTestInfo(i));\n  }\n  *stream << \"  </\" << kTestsuite << \">\\n\";\n}\n\n// Prints an XML summary of unit_test to output stream out.\nvoid XmlUnitTestResultPrinter::PrintXmlUnitTest(std::ostream* stream,\n                                                const UnitTest& unit_test) {\n  const std::string kTestsuites = \"testsuites\";\n\n  *stream << \"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?>\\n\";\n  *stream << \"<\" << kTestsuites;\n\n  OutputXmlAttribute(stream, kTestsuites, \"tests\",\n                     StreamableToString(unit_test.reportable_test_count()));\n  OutputXmlAttribute(stream, kTestsuites, \"failures\",\n                     StreamableToString(unit_test.failed_test_count()));\n  OutputXmlAttribute(\n      stream, kTestsuites, \"disabled\",\n      StreamableToString(unit_test.reportable_disabled_test_count()));\n  OutputXmlAttribute(stream, kTestsuites, \"errors\", \"0\");\n  OutputXmlAttribute(\n      stream, kTestsuites, \"timestamp\",\n      FormatEpochTimeInMillisAsIso8601(unit_test.start_timestamp()));\n  OutputXmlAttribute(stream, kTestsuites, \"time\",\n                     FormatTimeInMillisAsSeconds(unit_test.elapsed_time()));\n\n  if (GTEST_FLAG(shuffle)) {\n    OutputXmlAttribute(stream, kTestsuites, \"random_seed\",\n                       StreamableToString(unit_test.random_seed()));\n  }\n\n  *stream << TestPropertiesAsXmlAttributes(unit_test.ad_hoc_test_result());\n\n  OutputXmlAttribute(stream, kTestsuites, \"name\", \"AllTests\");\n  *stream << \">\\n\";\n\n  for (int i = 0; i < unit_test.total_test_case_count(); ++i) {\n    if (unit_test.GetTestCase(i)->reportable_test_count() > 0)\n      PrintXmlTestCase(stream, *unit_test.GetTestCase(i));\n  }\n  *stream << \"</\" << kTestsuites << \">\\n\";\n}\n\n// Produces a string representing the test properties in a result as space\n// delimited XML attributes based on the property key=\"value\" pairs.\nstd::string XmlUnitTestResultPrinter::TestPropertiesAsXmlAttributes(\n    const TestResult& result) {\n  Message attributes;\n  for (int i = 0; i < result.test_property_count(); ++i) {\n    const TestProperty& property = result.GetTestProperty(i);\n    attributes << \" \" << property.key() << \"=\"\n        << \"\\\"\" << EscapeXmlAttribute(property.value()) << \"\\\"\";\n  }\n  return attributes.GetString();\n}\n\n// End XmlUnitTestResultPrinter\n\n#if GTEST_CAN_STREAM_RESULTS_\n\n// Checks if str contains '=', '&', '%' or '\\n' characters. If yes,\n// replaces them by \"%xx\" where xx is their hexadecimal value. For\n// example, replaces \"=\" with \"%3D\".  This algorithm is O(strlen(str))\n// in both time and space -- important as the input str may contain an\n// arbitrarily long test failure message and stack trace.\nstd::string StreamingListener::UrlEncode(const char* str) {\n  std::string result;\n  result.reserve(strlen(str) + 1);\n  for (char ch = *str; ch != '\\0'; ch = *++str) {\n    switch (ch) {\n      case '%':\n      case '=':\n      case '&':\n      case '\\n':\n        result.append(\"%\" + String::FormatByte(static_cast<unsigned char>(ch)));\n        break;\n      default:\n        result.push_back(ch);\n        break;\n    }\n  }\n  return result;\n}\n\nvoid StreamingListener::SocketWriter::MakeConnection() {\n  GTEST_CHECK_(sockfd_ == -1)\n      << \"MakeConnection() can't be called when there is already a connection.\";\n\n  addrinfo hints;\n  memset(&hints, 0, sizeof(hints));\n  hints.ai_family = AF_UNSPEC;    // To allow both IPv4 and IPv6 addresses.\n  hints.ai_socktype = SOCK_STREAM;\n  addrinfo* servinfo = NULL;\n\n  // Use the getaddrinfo() to get a linked list of IP addresses for\n  // the given host name.\n  const int error_num = getaddrinfo(\n      host_name_.c_str(), port_num_.c_str(), &hints, &servinfo);\n  if (error_num != 0) {\n    GTEST_LOG_(WARNING) << \"stream_result_to: getaddrinfo() failed: \"\n                        << gai_strerror(error_num);\n  }\n\n  // Loop through all the results and connect to the first we can.\n  for (addrinfo* cur_addr = servinfo; sockfd_ == -1 && cur_addr != NULL;\n       cur_addr = cur_addr->ai_next) {\n    sockfd_ = socket(\n        cur_addr->ai_family, cur_addr->ai_socktype, cur_addr->ai_protocol);\n    if (sockfd_ != -1) {\n      // Connect the client socket to the server socket.\n      if (connect(sockfd_, cur_addr->ai_addr, cur_addr->ai_addrlen) == -1) {\n        close(sockfd_);\n        sockfd_ = -1;\n      }\n    }\n  }\n\n  freeaddrinfo(servinfo);  // all done with this structure\n\n  if (sockfd_ == -1) {\n    GTEST_LOG_(WARNING) << \"stream_result_to: failed to connect to \"\n                        << host_name_ << \":\" << port_num_;\n  }\n}\n\n// End of class Streaming Listener\n#endif  // GTEST_CAN_STREAM_RESULTS__\n\n// Class ScopedTrace\n\n// Pushes the given source file location and message onto a per-thread\n// trace stack maintained by Google Test.\nScopedTrace::ScopedTrace(const char* file, int line, const Message& message)\n    GTEST_LOCK_EXCLUDED_(&UnitTest::mutex_) {\n  TraceInfo trace;\n  trace.file = file;\n  trace.line = line;\n  trace.message = message.GetString();\n\n  UnitTest::GetInstance()->PushGTestTrace(trace);\n}\n\n// Pops the info pushed by the c'tor.\nScopedTrace::~ScopedTrace()\n    GTEST_LOCK_EXCLUDED_(&UnitTest::mutex_) {\n  UnitTest::GetInstance()->PopGTestTrace();\n}\n\n\n// class OsStackTraceGetter\n\nconst char* const OsStackTraceGetterInterface::kElidedFramesMarker =\n    \"... \" GTEST_NAME_ \" internal frames ...\";\n\nstd::string OsStackTraceGetter::CurrentStackTrace(int /*max_depth*/,\n                                                  int /*skip_count*/) {\n  return \"\";\n}\n\nvoid OsStackTraceGetter::UponLeavingGTest() {}\n\n// A helper class that creates the premature-exit file in its\n// constructor and deletes the file in its destructor.\nclass ScopedPrematureExitFile {\n public:\n  explicit ScopedPrematureExitFile(const char* premature_exit_filepath)\n      : premature_exit_filepath_(premature_exit_filepath) {\n    // If a path to the premature-exit file is specified...\n    if (premature_exit_filepath != NULL && *premature_exit_filepath != '\\0') {\n      // create the file with a single \"0\" character in it.  I/O\n      // errors are ignored as there's nothing better we can do and we\n      // don't want to fail the test because of this.\n      FILE* pfile = posix::FOpen(premature_exit_filepath, \"w\");\n      fwrite(\"0\", 1, 1, pfile);\n      fclose(pfile);\n    }\n  }\n\n  ~ScopedPrematureExitFile() {\n    if (premature_exit_filepath_ != NULL && *premature_exit_filepath_ != '\\0') {\n      remove(premature_exit_filepath_);\n    }\n  }\n\n private:\n  const char* const premature_exit_filepath_;\n\n  GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedPrematureExitFile);\n};\n\n}  // namespace internal\n\n// class TestEventListeners\n\nTestEventListeners::TestEventListeners()\n    : repeater_(new internal::TestEventRepeater()),\n      default_result_printer_(NULL),\n      default_xml_generator_(NULL) {\n}\n\nTestEventListeners::~TestEventListeners() { delete repeater_; }\n\n// Returns the standard listener responsible for the default console\n// output.  Can be removed from the listeners list to shut down default\n// console output.  Note that removing this object from the listener list\n// with Release transfers its ownership to the user.\nvoid TestEventListeners::Append(TestEventListener* listener) {\n  repeater_->Append(listener);\n}\n\n// Removes the given event listener from the list and returns it.  It then\n// becomes the caller's responsibility to delete the listener. Returns\n// NULL if the listener is not found in the list.\nTestEventListener* TestEventListeners::Release(TestEventListener* listener) {\n  if (listener == default_result_printer_)\n    default_result_printer_ = NULL;\n  else if (listener == default_xml_generator_)\n    default_xml_generator_ = NULL;\n  return repeater_->Release(listener);\n}\n\n// Returns repeater that broadcasts the TestEventListener events to all\n// subscribers.\nTestEventListener* TestEventListeners::repeater() { return repeater_; }\n\n// Sets the default_result_printer attribute to the provided listener.\n// The listener is also added to the listener list and previous\n// default_result_printer is removed from it and deleted. The listener can\n// also be NULL in which case it will not be added to the list. Does\n// nothing if the previous and the current listener objects are the same.\nvoid TestEventListeners::SetDefaultResultPrinter(TestEventListener* listener) {\n  if (default_result_printer_ != listener) {\n    // It is an error to pass this method a listener that is already in the\n    // list.\n    delete Release(default_result_printer_);\n    default_result_printer_ = listener;\n    if (listener != NULL)\n      Append(listener);\n  }\n}\n\n// Sets the default_xml_generator attribute to the provided listener.  The\n// listener is also added to the listener list and previous\n// default_xml_generator is removed from it and deleted. The listener can\n// also be NULL in which case it will not be added to the list. Does\n// nothing if the previous and the current listener objects are the same.\nvoid TestEventListeners::SetDefaultXmlGenerator(TestEventListener* listener) {\n  if (default_xml_generator_ != listener) {\n    // It is an error to pass this method a listener that is already in the\n    // list.\n    delete Release(default_xml_generator_);\n    default_xml_generator_ = listener;\n    if (listener != NULL)\n      Append(listener);\n  }\n}\n\n// Controls whether events will be forwarded by the repeater to the\n// listeners in the list.\nbool TestEventListeners::EventForwardingEnabled() const {\n  return repeater_->forwarding_enabled();\n}\n\nvoid TestEventListeners::SuppressEventForwarding() {\n  repeater_->set_forwarding_enabled(false);\n}\n\n// class UnitTest\n\n// Gets the singleton UnitTest object.  The first time this method is\n// called, a UnitTest object is constructed and returned.  Consecutive\n// calls will return the same object.\n//\n// We don't protect this under mutex_ as a user is not supposed to\n// call this before main() starts, from which point on the return\n// value will never change.\nUnitTest* UnitTest::GetInstance() {\n  // When compiled with MSVC 7.1 in optimized mode, destroying the\n  // UnitTest object upon exiting the program messes up the exit code,\n  // causing successful tests to appear failed.  We have to use a\n  // different implementation in this case to bypass the compiler bug.\n  // This implementation makes the compiler happy, at the cost of\n  // leaking the UnitTest object.\n\n  // CodeGear C++Builder insists on a public destructor for the\n  // default implementation.  Use this implementation to keep good OO\n  // design with private destructor.\n\n#if (_MSC_VER == 1310 && !defined(_DEBUG)) || defined(__BORLANDC__)\n  static UnitTest* const instance = new UnitTest;\n  return instance;\n#else\n  static UnitTest instance;\n  return &instance;\n#endif  // (_MSC_VER == 1310 && !defined(_DEBUG)) || defined(__BORLANDC__)\n}\n\n// Gets the number of successful test cases.\nint UnitTest::successful_test_case_count() const {\n  return impl()->successful_test_case_count();\n}\n\n// Gets the number of failed test cases.\nint UnitTest::failed_test_case_count() const {\n  return impl()->failed_test_case_count();\n}\n\n// Gets the number of all test cases.\nint UnitTest::total_test_case_count() const {\n  return impl()->total_test_case_count();\n}\n\n// Gets the number of all test cases that contain at least one test\n// that should run.\nint UnitTest::test_case_to_run_count() const {\n  return impl()->test_case_to_run_count();\n}\n\n// Gets the number of successful tests.\nint UnitTest::successful_test_count() const {\n  return impl()->successful_test_count();\n}\n\n// Gets the number of failed tests.\nint UnitTest::failed_test_count() const { return impl()->failed_test_count(); }\n\n// Gets the number of disabled tests that will be reported in the XML report.\nint UnitTest::reportable_disabled_test_count() const {\n  return impl()->reportable_disabled_test_count();\n}\n\n// Gets the number of disabled tests.\nint UnitTest::disabled_test_count() const {\n  return impl()->disabled_test_count();\n}\n\n// Gets the number of tests to be printed in the XML report.\nint UnitTest::reportable_test_count() const {\n  return impl()->reportable_test_count();\n}\n\n// Gets the number of all tests.\nint UnitTest::total_test_count() const { return impl()->total_test_count(); }\n\n// Gets the number of tests that should run.\nint UnitTest::test_to_run_count() const { return impl()->test_to_run_count(); }\n\n// Gets the time of the test program start, in ms from the start of the\n// UNIX epoch.\ninternal::TimeInMillis UnitTest::start_timestamp() const {\n    return impl()->start_timestamp();\n}\n\n// Gets the elapsed time, in milliseconds.\ninternal::TimeInMillis UnitTest::elapsed_time() const {\n  return impl()->elapsed_time();\n}\n\n// Returns true iff the unit test passed (i.e. all test cases passed).\nbool UnitTest::Passed() const { return impl()->Passed(); }\n\n// Returns true iff the unit test failed (i.e. some test case failed\n// or something outside of all tests failed).\nbool UnitTest::Failed() const { return impl()->Failed(); }\n\n// Gets the i-th test case among all the test cases. i can range from 0 to\n// total_test_case_count() - 1. If i is not in that range, returns NULL.\nconst TestCase* UnitTest::GetTestCase(int i) const {\n  return impl()->GetTestCase(i);\n}\n\n// Returns the TestResult containing information on test failures and\n// properties logged outside of individual test cases.\nconst TestResult& UnitTest::ad_hoc_test_result() const {\n  return *impl()->ad_hoc_test_result();\n}\n\n// Gets the i-th test case among all the test cases. i can range from 0 to\n// total_test_case_count() - 1. If i is not in that range, returns NULL.\nTestCase* UnitTest::GetMutableTestCase(int i) {\n  return impl()->GetMutableTestCase(i);\n}\n\n// Returns the list of event listeners that can be used to track events\n// inside Google Test.\nTestEventListeners& UnitTest::listeners() {\n  return *impl()->listeners();\n}\n\n// Registers and returns a global test environment.  When a test\n// program is run, all global test environments will be set-up in the\n// order they were registered.  After all tests in the program have\n// finished, all global test environments will be torn-down in the\n// *reverse* order they were registered.\n//\n// The UnitTest object takes ownership of the given environment.\n//\n// We don't protect this under mutex_, as we only support calling it\n// from the main thread.\nEnvironment* UnitTest::AddEnvironment(Environment* env) {\n  if (env == NULL) {\n    return NULL;\n  }\n\n  impl_->environments().push_back(env);\n  return env;\n}\n\n// Adds a TestPartResult to the current TestResult object.  All Google Test\n// assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc) eventually call\n// this to report their results.  The user code should use the\n// assertion macros instead of calling this directly.\nvoid UnitTest::AddTestPartResult(\n    TestPartResult::Type result_type,\n    const char* file_name,\n    int line_number,\n    const std::string& message,\n    const std::string& os_stack_trace) GTEST_LOCK_EXCLUDED_(mutex_) {\n  Message msg;\n  msg << message;\n\n  internal::MutexLock lock(&mutex_);\n  if (impl_->gtest_trace_stack().size() > 0) {\n    msg << \"\\n\" << GTEST_NAME_ << \" trace:\";\n\n    for (int i = static_cast<int>(impl_->gtest_trace_stack().size());\n         i > 0; --i) {\n      const internal::TraceInfo& trace = impl_->gtest_trace_stack()[i - 1];\n      msg << \"\\n\" << internal::FormatFileLocation(trace.file, trace.line)\n          << \" \" << trace.message;\n    }\n  }\n\n  if (os_stack_trace.c_str() != NULL && !os_stack_trace.empty()) {\n    msg << internal::kStackTraceMarker << os_stack_trace;\n  }\n\n  const TestPartResult result =\n    TestPartResult(result_type, file_name, line_number,\n                   msg.GetString().c_str());\n  impl_->GetTestPartResultReporterForCurrentThread()->\n      ReportTestPartResult(result);\n\n  if (result_type != TestPartResult::kSuccess) {\n    // gtest_break_on_failure takes precedence over\n    // gtest_throw_on_failure.  This allows a user to set the latter\n    // in the code (perhaps in order to use Google Test assertions\n    // with another testing framework) and specify the former on the\n    // command line for debugging.\n    if (GTEST_FLAG(break_on_failure)) {\n#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT\n      // Using DebugBreak on Windows allows gtest to still break into a debugger\n      // when a failure happens and both the --gtest_break_on_failure and\n      // the --gtest_catch_exceptions flags are specified.\n      DebugBreak();\n#else\n      // Dereference NULL through a volatile pointer to prevent the compiler\n      // from removing. We use this rather than abort() or __builtin_trap() for\n      // portability: Symbian doesn't implement abort() well, and some debuggers\n      // don't correctly trap abort().\n      *static_cast<volatile int*>(NULL) = 1;\n#endif  // GTEST_OS_WINDOWS\n    } else if (GTEST_FLAG(throw_on_failure)) {\n#if GTEST_HAS_EXCEPTIONS\n      throw internal::GoogleTestFailureException(result);\n#else\n      // We cannot call abort() as it generates a pop-up in debug mode\n      // that cannot be suppressed in VC 7.1 or below.\n      exit(1);\n#endif\n    }\n  }\n}\n\n// Adds a TestProperty to the current TestResult object when invoked from\n// inside a test, to current TestCase's ad_hoc_test_result_ when invoked\n// from SetUpTestCase or TearDownTestCase, or to the global property set\n// when invoked elsewhere.  If the result already contains a property with\n// the same key, the value will be updated.\nvoid UnitTest::RecordProperty(const std::string& key,\n                              const std::string& value) {\n  impl_->RecordProperty(TestProperty(key, value));\n}\n\n// Runs all tests in this UnitTest object and prints the result.\n// Returns 0 if successful, or 1 otherwise.\n//\n// We don't protect this under mutex_, as we only support calling it\n// from the main thread.\nint UnitTest::Run() {\n  const bool in_death_test_child_process =\n      internal::GTEST_FLAG(internal_run_death_test).length() > 0;\n\n  // Google Test implements this protocol for catching that a test\n  // program exits before returning control to Google Test:\n  //\n  //   1. Upon start, Google Test creates a file whose absolute path\n  //      is specified by the environment variable\n  //      TEST_PREMATURE_EXIT_FILE.\n  //   2. When Google Test has finished its work, it deletes the file.\n  //\n  // This allows a test runner to set TEST_PREMATURE_EXIT_FILE before\n  // running a Google-Test-based test program and check the existence\n  // of the file at the end of the test execution to see if it has\n  // exited prematurely.\n\n  // If we are in the child process of a death test, don't\n  // create/delete the premature exit file, as doing so is unnecessary\n  // and will confuse the parent process.  Otherwise, create/delete\n  // the file upon entering/leaving this function.  If the program\n  // somehow exits before this function has a chance to return, the\n  // premature-exit file will be left undeleted, causing a test runner\n  // that understands the premature-exit-file protocol to report the\n  // test as having failed.\n  const internal::ScopedPrematureExitFile premature_exit_file(\n      in_death_test_child_process ?\n      NULL : internal::posix::GetEnv(\"TEST_PREMATURE_EXIT_FILE\"));\n\n  // Captures the value of GTEST_FLAG(catch_exceptions).  This value will be\n  // used for the duration of the program.\n  impl()->set_catch_exceptions(GTEST_FLAG(catch_exceptions));\n\n#if GTEST_HAS_SEH\n  // Either the user wants Google Test to catch exceptions thrown by the\n  // tests or this is executing in the context of death test child\n  // process. In either case the user does not want to see pop-up dialogs\n  // about crashes - they are expected.\n  if (impl()->catch_exceptions() || in_death_test_child_process) {\n# if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT\n    // SetErrorMode doesn't exist on CE.\n    SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOALIGNMENTFAULTEXCEPT |\n                 SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX);\n# endif  // !GTEST_OS_WINDOWS_MOBILE\n\n# if (defined(_MSC_VER) || GTEST_OS_WINDOWS_MINGW) && !GTEST_OS_WINDOWS_MOBILE\n    // Death test children can be terminated with _abort().  On Windows,\n    // _abort() can show a dialog with a warning message.  This forces the\n    // abort message to go to stderr instead.\n    _set_error_mode(_OUT_TO_STDERR);\n# endif\n\n# if _MSC_VER >= 1400 && !GTEST_OS_WINDOWS_MOBILE\n    // In the debug version, Visual Studio pops up a separate dialog\n    // offering a choice to debug the aborted program. We need to suppress\n    // this dialog or it will pop up for every EXPECT/ASSERT_DEATH statement\n    // executed. Google Test will notify the user of any unexpected\n    // failure via stderr.\n    //\n    // VC++ doesn't define _set_abort_behavior() prior to the version 8.0.\n    // Users of prior VC versions shall suffer the agony and pain of\n    // clicking through the countless debug dialogs.\n    // TODO(vladl@google.com): find a way to suppress the abort dialog() in the\n    // debug mode when compiled with VC 7.1 or lower.\n    if (!GTEST_FLAG(break_on_failure))\n      _set_abort_behavior(\n          0x0,                                    // Clear the following flags:\n          _WRITE_ABORT_MSG | _CALL_REPORTFAULT);  // pop-up window, core dump.\n# endif\n  }\n#endif  // GTEST_HAS_SEH\n\n  return internal::HandleExceptionsInMethodIfSupported(\n      impl(),\n      &internal::UnitTestImpl::RunAllTests,\n      \"auxiliary test code (environments or event listeners)\") ? 0 : 1;\n}\n\n// Returns the working directory when the first TEST() or TEST_F() was\n// executed.\nconst char* UnitTest::original_working_dir() const {\n  return impl_->original_working_dir_.c_str();\n}\n\n// Returns the TestCase object for the test that's currently running,\n// or NULL if no test is running.\nconst TestCase* UnitTest::current_test_case() const\n    GTEST_LOCK_EXCLUDED_(mutex_) {\n  internal::MutexLock lock(&mutex_);\n  return impl_->current_test_case();\n}\n\n// Returns the TestInfo object for the test that's currently running,\n// or NULL if no test is running.\nconst TestInfo* UnitTest::current_test_info() const\n    GTEST_LOCK_EXCLUDED_(mutex_) {\n  internal::MutexLock lock(&mutex_);\n  return impl_->current_test_info();\n}\n\n// Returns the random seed used at the start of the current test run.\nint UnitTest::random_seed() const { return impl_->random_seed(); }\n\n#if GTEST_HAS_PARAM_TEST\n// Returns ParameterizedTestCaseRegistry object used to keep track of\n// value-parameterized tests and instantiate and register them.\ninternal::ParameterizedTestCaseRegistry&\n    UnitTest::parameterized_test_registry()\n        GTEST_LOCK_EXCLUDED_(mutex_) {\n  return impl_->parameterized_test_registry();\n}\n#endif  // GTEST_HAS_PARAM_TEST\n\n// Creates an empty UnitTest.\nUnitTest::UnitTest() {\n  impl_ = new internal::UnitTestImpl(this);\n}\n\n// Destructor of UnitTest.\nUnitTest::~UnitTest() {\n  delete impl_;\n}\n\n// Pushes a trace defined by SCOPED_TRACE() on to the per-thread\n// Google Test trace stack.\nvoid UnitTest::PushGTestTrace(const internal::TraceInfo& trace)\n    GTEST_LOCK_EXCLUDED_(mutex_) {\n  internal::MutexLock lock(&mutex_);\n  impl_->gtest_trace_stack().push_back(trace);\n}\n\n// Pops a trace from the per-thread Google Test trace stack.\nvoid UnitTest::PopGTestTrace()\n    GTEST_LOCK_EXCLUDED_(mutex_) {\n  internal::MutexLock lock(&mutex_);\n  impl_->gtest_trace_stack().pop_back();\n}\n\nnamespace internal {\n\nUnitTestImpl::UnitTestImpl(UnitTest* parent)\n    : parent_(parent),\n      GTEST_DISABLE_MSC_WARNINGS_PUSH_(4355 /* using this in initializer */)\n      default_global_test_part_result_reporter_(this),\n      default_per_thread_test_part_result_reporter_(this),\n      GTEST_DISABLE_MSC_WARNINGS_POP_()\n      global_test_part_result_repoter_(\n          &default_global_test_part_result_reporter_),\n      per_thread_test_part_result_reporter_(\n          &default_per_thread_test_part_result_reporter_),\n#if GTEST_HAS_PARAM_TEST\n      parameterized_test_registry_(),\n      parameterized_tests_registered_(false),\n#endif  // GTEST_HAS_PARAM_TEST\n      last_death_test_case_(-1),\n      current_test_case_(NULL),\n      current_test_info_(NULL),\n      ad_hoc_test_result_(),\n      os_stack_trace_getter_(NULL),\n      post_flag_parse_init_performed_(false),\n      random_seed_(0),  // Will be overridden by the flag before first use.\n      random_(0),  // Will be reseeded before first use.\n      start_timestamp_(0),\n      elapsed_time_(0),\n#if GTEST_HAS_DEATH_TEST\n      death_test_factory_(new DefaultDeathTestFactory),\n#endif\n      // Will be overridden by the flag before first use.\n      catch_exceptions_(false) {\n  listeners()->SetDefaultResultPrinter(new PrettyUnitTestResultPrinter);\n}\n\nUnitTestImpl::~UnitTestImpl() {\n  // Deletes every TestCase.\n  ForEach(test_cases_, internal::Delete<TestCase>);\n\n  // Deletes every Environment.\n  ForEach(environments_, internal::Delete<Environment>);\n\n  delete os_stack_trace_getter_;\n}\n\n// Adds a TestProperty to the current TestResult object when invoked in a\n// context of a test, to current test case's ad_hoc_test_result when invoke\n// from SetUpTestCase/TearDownTestCase, or to the global property set\n// otherwise.  If the result already contains a property with the same key,\n// the value will be updated.\nvoid UnitTestImpl::RecordProperty(const TestProperty& test_property) {\n  std::string xml_element;\n  TestResult* test_result;  // TestResult appropriate for property recording.\n\n  if (current_test_info_ != NULL) {\n    xml_element = \"testcase\";\n    test_result = &(current_test_info_->result_);\n  } else if (current_test_case_ != NULL) {\n    xml_element = \"testsuite\";\n    test_result = &(current_test_case_->ad_hoc_test_result_);\n  } else {\n    xml_element = \"testsuites\";\n    test_result = &ad_hoc_test_result_;\n  }\n  test_result->RecordProperty(xml_element, test_property);\n}\n\n#if GTEST_HAS_DEATH_TEST\n// Disables event forwarding if the control is currently in a death test\n// subprocess. Must not be called before InitGoogleTest.\nvoid UnitTestImpl::SuppressTestEventsIfInSubprocess() {\n  if (internal_run_death_test_flag_.get() != NULL)\n    listeners()->SuppressEventForwarding();\n}\n#endif  // GTEST_HAS_DEATH_TEST\n\n// Initializes event listeners performing XML output as specified by\n// UnitTestOptions. Must not be called before InitGoogleTest.\nvoid UnitTestImpl::ConfigureXmlOutput() {\n  const std::string& output_format = UnitTestOptions::GetOutputFormat();\n  if (output_format == \"xml\") {\n    listeners()->SetDefaultXmlGenerator(new XmlUnitTestResultPrinter(\n        UnitTestOptions::GetAbsolutePathToOutputFile().c_str()));\n  } else if (output_format != \"\") {\n    printf(\"WARNING: unrecognized output format \\\"%s\\\" ignored.\\n\",\n           output_format.c_str());\n    fflush(stdout);\n  }\n}\n\n#if GTEST_CAN_STREAM_RESULTS_\n// Initializes event listeners for streaming test results in string form.\n// Must not be called before InitGoogleTest.\nvoid UnitTestImpl::ConfigureStreamingOutput() {\n  const std::string& target = GTEST_FLAG(stream_result_to);\n  if (!target.empty()) {\n    const size_t pos = target.find(':');\n    if (pos != std::string::npos) {\n      listeners()->Append(new StreamingListener(target.substr(0, pos),\n                                                target.substr(pos+1)));\n    } else {\n      printf(\"WARNING: unrecognized streaming target \\\"%s\\\" ignored.\\n\",\n             target.c_str());\n      fflush(stdout);\n    }\n  }\n}\n#endif  // GTEST_CAN_STREAM_RESULTS_\n\n// Performs initialization dependent upon flag values obtained in\n// ParseGoogleTestFlagsOnly.  Is called from InitGoogleTest after the call to\n// ParseGoogleTestFlagsOnly.  In case a user neglects to call InitGoogleTest\n// this function is also called from RunAllTests.  Since this function can be\n// called more than once, it has to be idempotent.\nvoid UnitTestImpl::PostFlagParsingInit() {\n  // Ensures that this function does not execute more than once.\n  if (!post_flag_parse_init_performed_) {\n    post_flag_parse_init_performed_ = true;\n\n#if defined(GTEST_CUSTOM_TEST_EVENT_LISTENER_)\n    // Register to send notifications about key process state changes.\n    listeners()->Append(new GTEST_CUSTOM_TEST_EVENT_LISTENER_());\n#endif  // defined(GTEST_CUSTOM_TEST_EVENT_LISTENER_)\n\n#if GTEST_HAS_DEATH_TEST\n    InitDeathTestSubprocessControlInfo();\n    SuppressTestEventsIfInSubprocess();\n#endif  // GTEST_HAS_DEATH_TEST\n\n    // Registers parameterized tests. This makes parameterized tests\n    // available to the UnitTest reflection API without running\n    // RUN_ALL_TESTS.\n    RegisterParameterizedTests();\n\n    // Configures listeners for XML output. This makes it possible for users\n    // to shut down the default XML output before invoking RUN_ALL_TESTS.\n    ConfigureXmlOutput();\n\n#if GTEST_CAN_STREAM_RESULTS_\n    // Configures listeners for streaming test results to the specified server.\n    ConfigureStreamingOutput();\n#endif  // GTEST_CAN_STREAM_RESULTS_\n  }\n}\n\n// A predicate that checks the name of a TestCase against a known\n// value.\n//\n// This is used for implementation of the UnitTest class only.  We put\n// it in the anonymous namespace to prevent polluting the outer\n// namespace.\n//\n// TestCaseNameIs is copyable.\nclass TestCaseNameIs {\n public:\n  // Constructor.\n  explicit TestCaseNameIs(const std::string& name)\n      : name_(name) {}\n\n  // Returns true iff the name of test_case matches name_.\n  bool operator()(const TestCase* test_case) const {\n    return test_case != NULL && strcmp(test_case->name(), name_.c_str()) == 0;\n  }\n\n private:\n  std::string name_;\n};\n\n// Finds and returns a TestCase with the given name.  If one doesn't\n// exist, creates one and returns it.  It's the CALLER'S\n// RESPONSIBILITY to ensure that this function is only called WHEN THE\n// TESTS ARE NOT SHUFFLED.\n//\n// Arguments:\n//\n//   test_case_name: name of the test case\n//   type_param:     the name of the test case's type parameter, or NULL if\n//                   this is not a typed or a type-parameterized test case.\n//   set_up_tc:      pointer to the function that sets up the test case\n//   tear_down_tc:   pointer to the function that tears down the test case\nTestCase* UnitTestImpl::GetTestCase(const char* test_case_name,\n                                    const char* type_param,\n                                    Test::SetUpTestCaseFunc set_up_tc,\n                                    Test::TearDownTestCaseFunc tear_down_tc) {\n  // Can we find a TestCase with the given name?\n  const std::vector<TestCase*>::const_iterator test_case =\n      std::find_if(test_cases_.begin(), test_cases_.end(),\n                   TestCaseNameIs(test_case_name));\n\n  if (test_case != test_cases_.end())\n    return *test_case;\n\n  // No.  Let's create one.\n  TestCase* const new_test_case =\n      new TestCase(test_case_name, type_param, set_up_tc, tear_down_tc);\n\n  // Is this a death test case?\n  if (internal::UnitTestOptions::MatchesFilter(test_case_name,\n                                               kDeathTestCaseFilter)) {\n    // Yes.  Inserts the test case after the last death test case\n    // defined so far.  This only works when the test cases haven't\n    // been shuffled.  Otherwise we may end up running a death test\n    // after a non-death test.\n    ++last_death_test_case_;\n    test_cases_.insert(test_cases_.begin() + last_death_test_case_,\n                       new_test_case);\n  } else {\n    // No.  Appends to the end of the list.\n    test_cases_.push_back(new_test_case);\n  }\n\n  test_case_indices_.push_back(static_cast<int>(test_case_indices_.size()));\n  return new_test_case;\n}\n\n// Helpers for setting up / tearing down the given environment.  They\n// are for use in the ForEach() function.\nstatic void SetUpEnvironment(Environment* env) { env->SetUp(); }\nstatic void TearDownEnvironment(Environment* env) { env->TearDown(); }\n\n// Runs all tests in this UnitTest object, prints the result, and\n// returns true if all tests are successful.  If any exception is\n// thrown during a test, the test is considered to be failed, but the\n// rest of the tests will still be run.\n//\n// When parameterized tests are enabled, it expands and registers\n// parameterized tests first in RegisterParameterizedTests().\n// All other functions called from RunAllTests() may safely assume that\n// parameterized tests are ready to be counted and run.\nbool UnitTestImpl::RunAllTests() {\n  // Makes sure InitGoogleTest() was called.\n  if (!GTestIsInitialized()) {\n    printf(\"%s\",\n           \"\\nThis test program did NOT call ::testing::InitGoogleTest \"\n           \"before calling RUN_ALL_TESTS().  Please fix it.\\n\");\n    return false;\n  }\n\n  // Do not run any test if the --help flag was specified.\n  if (g_help_flag)\n    return true;\n\n  // Repeats the call to the post-flag parsing initialization in case the\n  // user didn't call InitGoogleTest.\n  PostFlagParsingInit();\n\n  // Even if sharding is not on, test runners may want to use the\n  // GTEST_SHARD_STATUS_FILE to query whether the test supports the sharding\n  // protocol.\n  internal::WriteToShardStatusFileIfNeeded();\n\n  // True iff we are in a subprocess for running a thread-safe-style\n  // death test.\n  bool in_subprocess_for_death_test = false;\n\n#if GTEST_HAS_DEATH_TEST\n  in_subprocess_for_death_test = (internal_run_death_test_flag_.get() != NULL);\n# if defined(GTEST_EXTRA_DEATH_TEST_CHILD_SETUP_)\n  if (in_subprocess_for_death_test) {\n    GTEST_EXTRA_DEATH_TEST_CHILD_SETUP_();\n  }\n# endif  // defined(GTEST_EXTRA_DEATH_TEST_CHILD_SETUP_)\n#endif  // GTEST_HAS_DEATH_TEST\n\n  const bool should_shard = ShouldShard(kTestTotalShards, kTestShardIndex,\n                                        in_subprocess_for_death_test);\n\n  // Compares the full test names with the filter to decide which\n  // tests to run.\n  const bool has_tests_to_run = FilterTests(should_shard\n                                              ? HONOR_SHARDING_PROTOCOL\n                                              : IGNORE_SHARDING_PROTOCOL) > 0;\n\n  // Lists the tests and exits if the --gtest_list_tests flag was specified.\n  if (GTEST_FLAG(list_tests)) {\n    // This must be called *after* FilterTests() has been called.\n    ListTestsMatchingFilter();\n    return true;\n  }\n\n  random_seed_ = GTEST_FLAG(shuffle) ?\n      GetRandomSeedFromFlag(GTEST_FLAG(random_seed)) : 0;\n\n  // True iff at least one test has failed.\n  bool failed = false;\n\n  TestEventListener* repeater = listeners()->repeater();\n\n  start_timestamp_ = GetTimeInMillis();\n  repeater->OnTestProgramStart(*parent_);\n\n  // How many times to repeat the tests?  We don't want to repeat them\n  // when we are inside the subprocess of a death test.\n  const int repeat = in_subprocess_for_death_test ? 1 : GTEST_FLAG(repeat);\n  // Repeats forever if the repeat count is negative.\n  const bool forever = repeat < 0;\n  for (int i = 0; forever || i != repeat; i++) {\n    // We want to preserve failures generated by ad-hoc test\n    // assertions executed before RUN_ALL_TESTS().\n    ClearNonAdHocTestResult();\n\n    const TimeInMillis start = GetTimeInMillis();\n\n    // Shuffles test cases and tests if requested.\n    if (has_tests_to_run && GTEST_FLAG(shuffle)) {\n      random()->Reseed(random_seed_);\n      // This should be done before calling OnTestIterationStart(),\n      // such that a test event listener can see the actual test order\n      // in the event.\n      ShuffleTests();\n    }\n\n    // Tells the unit test event listeners that the tests are about to start.\n    repeater->OnTestIterationStart(*parent_, i);\n\n    // Runs each test case if there is at least one test to run.\n    if (has_tests_to_run) {\n      // Sets up all environments beforehand.\n      repeater->OnEnvironmentsSetUpStart(*parent_);\n      ForEach(environments_, SetUpEnvironment);\n      repeater->OnEnvironmentsSetUpEnd(*parent_);\n\n      // Runs the tests only if there was no fatal failure during global\n      // set-up.\n      if (!Test::HasFatalFailure()) {\n        for (int test_index = 0; test_index < total_test_case_count();\n             test_index++) {\n          GetMutableTestCase(test_index)->Run();\n        }\n      }\n\n      // Tears down all environments in reverse order afterwards.\n      repeater->OnEnvironmentsTearDownStart(*parent_);\n      std::for_each(environments_.rbegin(), environments_.rend(),\n                    TearDownEnvironment);\n      repeater->OnEnvironmentsTearDownEnd(*parent_);\n    }\n\n    elapsed_time_ = GetTimeInMillis() - start;\n\n    // Tells the unit test event listener that the tests have just finished.\n    repeater->OnTestIterationEnd(*parent_, i);\n\n    // Gets the result and clears it.\n    if (!Passed()) {\n      failed = true;\n    }\n\n    // Restores the original test order after the iteration.  This\n    // allows the user to quickly repro a failure that happens in the\n    // N-th iteration without repeating the first (N - 1) iterations.\n    // This is not enclosed in \"if (GTEST_FLAG(shuffle)) { ... }\", in\n    // case the user somehow changes the value of the flag somewhere\n    // (it's always safe to unshuffle the tests).\n    UnshuffleTests();\n\n    if (GTEST_FLAG(shuffle)) {\n      // Picks a new random seed for each iteration.\n      random_seed_ = GetNextRandomSeed(random_seed_);\n    }\n  }\n\n  repeater->OnTestProgramEnd(*parent_);\n\n  return !failed;\n}\n\n// Reads the GTEST_SHARD_STATUS_FILE environment variable, and creates the file\n// if the variable is present. If a file already exists at this location, this\n// function will write over it. If the variable is present, but the file cannot\n// be created, prints an error and exits.\nvoid WriteToShardStatusFileIfNeeded() {\n  const char* const test_shard_file = posix::GetEnv(kTestShardStatusFile);\n  if (test_shard_file != NULL) {\n    FILE* const file = posix::FOpen(test_shard_file, \"w\");\n    if (file == NULL) {\n      ColoredPrintf(COLOR_RED,\n                    \"Could not write to the test shard status file \\\"%s\\\" \"\n                    \"specified by the %s environment variable.\\n\",\n                    test_shard_file, kTestShardStatusFile);\n      fflush(stdout);\n      exit(EXIT_FAILURE);\n    }\n    fclose(file);\n  }\n}\n\n// Checks whether sharding is enabled by examining the relevant\n// environment variable values. If the variables are present,\n// but inconsistent (i.e., shard_index >= total_shards), prints\n// an error and exits. If in_subprocess_for_death_test, sharding is\n// disabled because it must only be applied to the original test\n// process. Otherwise, we could filter out death tests we intended to execute.\nbool ShouldShard(const char* total_shards_env,\n                 const char* shard_index_env,\n                 bool in_subprocess_for_death_test) {\n  if (in_subprocess_for_death_test) {\n    return false;\n  }\n\n  const Int32 total_shards = Int32FromEnvOrDie(total_shards_env, -1);\n  const Int32 shard_index = Int32FromEnvOrDie(shard_index_env, -1);\n\n  if (total_shards == -1 && shard_index == -1) {\n    return false;\n  } else if (total_shards == -1 && shard_index != -1) {\n    const Message msg = Message()\n      << \"Invalid environment variables: you have \"\n      << kTestShardIndex << \" = \" << shard_index\n      << \", but have left \" << kTestTotalShards << \" unset.\\n\";\n    ColoredPrintf(COLOR_RED, \"%s\", msg.GetString().c_str());\n    fflush(stdout);\n    exit(EXIT_FAILURE);\n  } else if (total_shards != -1 && shard_index == -1) {\n    const Message msg = Message()\n      << \"Invalid environment variables: you have \"\n      << kTestTotalShards << \" = \" << total_shards\n      << \", but have left \" << kTestShardIndex << \" unset.\\n\";\n    ColoredPrintf(COLOR_RED, \"%s\", msg.GetString().c_str());\n    fflush(stdout);\n    exit(EXIT_FAILURE);\n  } else if (shard_index < 0 || shard_index >= total_shards) {\n    const Message msg = Message()\n      << \"Invalid environment variables: we require 0 <= \"\n      << kTestShardIndex << \" < \" << kTestTotalShards\n      << \", but you have \" << kTestShardIndex << \"=\" << shard_index\n      << \", \" << kTestTotalShards << \"=\" << total_shards << \".\\n\";\n    ColoredPrintf(COLOR_RED, \"%s\", msg.GetString().c_str());\n    fflush(stdout);\n    exit(EXIT_FAILURE);\n  }\n\n  return total_shards > 1;\n}\n\n// Parses the environment variable var as an Int32. If it is unset,\n// returns default_val. If it is not an Int32, prints an error\n// and aborts.\nInt32 Int32FromEnvOrDie(const char* var, Int32 default_val) {\n  const char* str_val = posix::GetEnv(var);\n  if (str_val == NULL) {\n    return default_val;\n  }\n\n  Int32 result;\n  if (!ParseInt32(Message() << \"The value of environment variable \" << var,\n                  str_val, &result)) {\n    exit(EXIT_FAILURE);\n  }\n  return result;\n}\n\n// Given the total number of shards, the shard index, and the test id,\n// returns true iff the test should be run on this shard. The test id is\n// some arbitrary but unique non-negative integer assigned to each test\n// method. Assumes that 0 <= shard_index < total_shards.\nbool ShouldRunTestOnShard(int total_shards, int shard_index, int test_id) {\n  return (test_id % total_shards) == shard_index;\n}\n\n// Compares the name of each test with the user-specified filter to\n// decide whether the test should be run, then records the result in\n// each TestCase and TestInfo object.\n// If shard_tests == true, further filters tests based on sharding\n// variables in the environment - see\n// http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide.\n// Returns the number of tests that should run.\nint UnitTestImpl::FilterTests(ReactionToSharding shard_tests) {\n  const Int32 total_shards = shard_tests == HONOR_SHARDING_PROTOCOL ?\n      Int32FromEnvOrDie(kTestTotalShards, -1) : -1;\n  const Int32 shard_index = shard_tests == HONOR_SHARDING_PROTOCOL ?\n      Int32FromEnvOrDie(kTestShardIndex, -1) : -1;\n\n  // num_runnable_tests are the number of tests that will\n  // run across all shards (i.e., match filter and are not disabled).\n  // num_selected_tests are the number of tests to be run on\n  // this shard.\n  int num_runnable_tests = 0;\n  int num_selected_tests = 0;\n  for (size_t i = 0; i < test_cases_.size(); i++) {\n    TestCase* const test_case = test_cases_[i];\n    const std::string &test_case_name = test_case->name();\n    test_case->set_should_run(false);\n\n    for (size_t j = 0; j < test_case->test_info_list().size(); j++) {\n      TestInfo* const test_info = test_case->test_info_list()[j];\n      const std::string test_name(test_info->name());\n      // A test is disabled if test case name or test name matches\n      // kDisableTestFilter.\n      const bool is_disabled =\n          internal::UnitTestOptions::MatchesFilter(test_case_name,\n                                                   kDisableTestFilter) ||\n          internal::UnitTestOptions::MatchesFilter(test_name,\n                                                   kDisableTestFilter);\n      test_info->is_disabled_ = is_disabled;\n\n      const bool matches_filter =\n          internal::UnitTestOptions::FilterMatchesTest(test_case_name,\n                                                       test_name);\n      test_info->matches_filter_ = matches_filter;\n\n      const bool is_runnable =\n          (GTEST_FLAG(also_run_disabled_tests) || !is_disabled) &&\n          matches_filter;\n\n      const bool is_selected = is_runnable &&\n          (shard_tests == IGNORE_SHARDING_PROTOCOL ||\n           ShouldRunTestOnShard(total_shards, shard_index,\n                                num_runnable_tests));\n\n      num_runnable_tests += is_runnable;\n      num_selected_tests += is_selected;\n\n      test_info->should_run_ = is_selected;\n      test_case->set_should_run(test_case->should_run() || is_selected);\n    }\n  }\n  return num_selected_tests;\n}\n\n// Prints the given C-string on a single line by replacing all '\\n'\n// characters with string \"\\\\n\".  If the output takes more than\n// max_length characters, only prints the first max_length characters\n// and \"...\".\nstatic void PrintOnOneLine(const char* str, int max_length) {\n  if (str != NULL) {\n    for (int i = 0; *str != '\\0'; ++str) {\n      if (i >= max_length) {\n        printf(\"...\");\n        break;\n      }\n      if (*str == '\\n') {\n        printf(\"\\\\n\");\n        i += 2;\n      } else {\n        printf(\"%c\", *str);\n        ++i;\n      }\n    }\n  }\n}\n\n// Prints the names of the tests matching the user-specified filter flag.\nvoid UnitTestImpl::ListTestsMatchingFilter() {\n  // Print at most this many characters for each type/value parameter.\n  const int kMaxParamLength = 250;\n\n  for (size_t i = 0; i < test_cases_.size(); i++) {\n    const TestCase* const test_case = test_cases_[i];\n    bool printed_test_case_name = false;\n\n    for (size_t j = 0; j < test_case->test_info_list().size(); j++) {\n      const TestInfo* const test_info =\n          test_case->test_info_list()[j];\n      if (test_info->matches_filter_) {\n        if (!printed_test_case_name) {\n          printed_test_case_name = true;\n          printf(\"%s.\", test_case->name());\n          if (test_case->type_param() != NULL) {\n            printf(\"  # %s = \", kTypeParamLabel);\n            // We print the type parameter on a single line to make\n            // the output easy to parse by a program.\n            PrintOnOneLine(test_case->type_param(), kMaxParamLength);\n          }\n          printf(\"\\n\");\n        }\n        printf(\"  %s\", test_info->name());\n        printf(\"\\n\");\n      }\n    }\n  }\n  fflush(stdout);\n}\n\n// Sets the OS stack trace getter.\n//\n// Does nothing if the input and the current OS stack trace getter are\n// the same; otherwise, deletes the old getter and makes the input the\n// current getter.\nvoid UnitTestImpl::set_os_stack_trace_getter(\n    OsStackTraceGetterInterface* getter) {\n  if (os_stack_trace_getter_ != getter) {\n    delete os_stack_trace_getter_;\n    os_stack_trace_getter_ = getter;\n  }\n}\n\n// Returns the current OS stack trace getter if it is not NULL;\n// otherwise, creates an OsStackTraceGetter, makes it the current\n// getter, and returns it.\nOsStackTraceGetterInterface* UnitTestImpl::os_stack_trace_getter() {\n  if (os_stack_trace_getter_ == NULL) {\n#ifdef GTEST_OS_STACK_TRACE_GETTER_\n    os_stack_trace_getter_ = new GTEST_OS_STACK_TRACE_GETTER_;\n#else\n    os_stack_trace_getter_ = new OsStackTraceGetter;\n#endif  // GTEST_OS_STACK_TRACE_GETTER_\n  }\n\n  return os_stack_trace_getter_;\n}\n\n// Returns the TestResult for the test that's currently running, or\n// the TestResult for the ad hoc test if no test is running.\nTestResult* UnitTestImpl::current_test_result() {\n  return current_test_info_ ?\n      &(current_test_info_->result_) : &ad_hoc_test_result_;\n}\n\n// Shuffles all test cases, and the tests within each test case,\n// making sure that death tests are still run first.\nvoid UnitTestImpl::ShuffleTests() {\n  // Shuffles the death test cases.\n  ShuffleRange(random(), 0, last_death_test_case_ + 1, &test_case_indices_);\n\n  // Shuffles the non-death test cases.\n  ShuffleRange(random(), last_death_test_case_ + 1,\n               static_cast<int>(test_cases_.size()), &test_case_indices_);\n\n  // Shuffles the tests inside each test case.\n  for (size_t i = 0; i < test_cases_.size(); i++) {\n    test_cases_[i]->ShuffleTests(random());\n  }\n}\n\n// Restores the test cases and tests to their order before the first shuffle.\nvoid UnitTestImpl::UnshuffleTests() {\n  for (size_t i = 0; i < test_cases_.size(); i++) {\n    // Unshuffles the tests in each test case.\n    test_cases_[i]->UnshuffleTests();\n    // Resets the index of each test case.\n    test_case_indices_[i] = static_cast<int>(i);\n  }\n}\n\n// Returns the current OS stack trace as an std::string.\n//\n// The maximum number of stack frames to be included is specified by\n// the gtest_stack_trace_depth flag.  The skip_count parameter\n// specifies the number of top frames to be skipped, which doesn't\n// count against the number of frames to be included.\n//\n// For example, if Foo() calls Bar(), which in turn calls\n// GetCurrentOsStackTraceExceptTop(..., 1), Foo() will be included in\n// the trace but Bar() and GetCurrentOsStackTraceExceptTop() won't.\nstd::string GetCurrentOsStackTraceExceptTop(UnitTest* /*unit_test*/,\n                                            int skip_count) {\n  // We pass skip_count + 1 to skip this wrapper function in addition\n  // to what the user really wants to skip.\n  return GetUnitTestImpl()->CurrentOsStackTraceExceptTop(skip_count + 1);\n}\n\n// Used by the GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_ macro to\n// suppress unreachable code warnings.\nnamespace {\nclass ClassUniqueToAlwaysTrue {};\n}\n\nbool IsTrue(bool condition) { return condition; }\n\nbool AlwaysTrue() {\n#if GTEST_HAS_EXCEPTIONS\n  // This condition is always false so AlwaysTrue() never actually throws,\n  // but it makes the compiler think that it may throw.\n  if (IsTrue(false))\n    throw ClassUniqueToAlwaysTrue();\n#endif  // GTEST_HAS_EXCEPTIONS\n  return true;\n}\n\n// If *pstr starts with the given prefix, modifies *pstr to be right\n// past the prefix and returns true; otherwise leaves *pstr unchanged\n// and returns false.  None of pstr, *pstr, and prefix can be NULL.\nbool SkipPrefix(const char* prefix, const char** pstr) {\n  const size_t prefix_len = strlen(prefix);\n  if (strncmp(*pstr, prefix, prefix_len) == 0) {\n    *pstr += prefix_len;\n    return true;\n  }\n  return false;\n}\n\n// Parses a string as a command line flag.  The string should have\n// the format \"--flag=value\".  When def_optional is true, the \"=value\"\n// part can be omitted.\n//\n// Returns the value of the flag, or NULL if the parsing failed.\nconst char* ParseFlagValue(const char* str,\n                           const char* flag,\n                           bool def_optional) {\n  // str and flag must not be NULL.\n  if (str == NULL || flag == NULL) return NULL;\n\n  // The flag must start with \"--\" followed by GTEST_FLAG_PREFIX_.\n  const std::string flag_str = std::string(\"--\") + GTEST_FLAG_PREFIX_ + flag;\n  const size_t flag_len = flag_str.length();\n  if (strncmp(str, flag_str.c_str(), flag_len) != 0) return NULL;\n\n  // Skips the flag name.\n  const char* flag_end = str + flag_len;\n\n  // When def_optional is true, it's OK to not have a \"=value\" part.\n  if (def_optional && (flag_end[0] == '\\0')) {\n    return flag_end;\n  }\n\n  // If def_optional is true and there are more characters after the\n  // flag name, or if def_optional is false, there must be a '=' after\n  // the flag name.\n  if (flag_end[0] != '=') return NULL;\n\n  // Returns the string after \"=\".\n  return flag_end + 1;\n}\n\n// Parses a string for a bool flag, in the form of either\n// \"--flag=value\" or \"--flag\".\n//\n// In the former case, the value is taken as true as long as it does\n// not start with '0', 'f', or 'F'.\n//\n// In the latter case, the value is taken as true.\n//\n// On success, stores the value of the flag in *value, and returns\n// true.  On failure, returns false without changing *value.\nbool ParseBoolFlag(const char* str, const char* flag, bool* value) {\n  // Gets the value of the flag as a string.\n  const char* const value_str = ParseFlagValue(str, flag, true);\n\n  // Aborts if the parsing failed.\n  if (value_str == NULL) return false;\n\n  // Converts the string value to a bool.\n  *value = !(*value_str == '0' || *value_str == 'f' || *value_str == 'F');\n  return true;\n}\n\n// Parses a string for an Int32 flag, in the form of\n// \"--flag=value\".\n//\n// On success, stores the value of the flag in *value, and returns\n// true.  On failure, returns false without changing *value.\nbool ParseInt32Flag(const char* str, const char* flag, Int32* value) {\n  // Gets the value of the flag as a string.\n  const char* const value_str = ParseFlagValue(str, flag, false);\n\n  // Aborts if the parsing failed.\n  if (value_str == NULL) return false;\n\n  // Sets *value to the value of the flag.\n  return ParseInt32(Message() << \"The value of flag --\" << flag,\n                    value_str, value);\n}\n\n// Parses a string for a string flag, in the form of\n// \"--flag=value\".\n//\n// On success, stores the value of the flag in *value, and returns\n// true.  On failure, returns false without changing *value.\nbool ParseStringFlag(const char* str, const char* flag, std::string* value) {\n  // Gets the value of the flag as a string.\n  const char* const value_str = ParseFlagValue(str, flag, false);\n\n  // Aborts if the parsing failed.\n  if (value_str == NULL) return false;\n\n  // Sets *value to the value of the flag.\n  *value = value_str;\n  return true;\n}\n\n// Determines whether a string has a prefix that Google Test uses for its\n// flags, i.e., starts with GTEST_FLAG_PREFIX_ or GTEST_FLAG_PREFIX_DASH_.\n// If Google Test detects that a command line flag has its prefix but is not\n// recognized, it will print its help message. Flags starting with\n// GTEST_INTERNAL_PREFIX_ followed by \"internal_\" are considered Google Test\n// internal flags and do not trigger the help message.\nstatic bool HasGoogleTestFlagPrefix(const char* str) {\n  return (SkipPrefix(\"--\", &str) ||\n          SkipPrefix(\"-\", &str) ||\n          SkipPrefix(\"/\", &str)) &&\n         !SkipPrefix(GTEST_FLAG_PREFIX_ \"internal_\", &str) &&\n         (SkipPrefix(GTEST_FLAG_PREFIX_, &str) ||\n          SkipPrefix(GTEST_FLAG_PREFIX_DASH_, &str));\n}\n\n// Prints a string containing code-encoded text.  The following escape\n// sequences can be used in the string to control the text color:\n//\n//   @@    prints a single '@' character.\n//   @R    changes the color to red.\n//   @G    changes the color to green.\n//   @Y    changes the color to yellow.\n//   @D    changes to the default terminal text color.\n//\n// TODO(wan@google.com): Write tests for this once we add stdout\n// capturing to Google Test.\nstatic void PrintColorEncoded(const char* str) {\n  GTestColor color = COLOR_DEFAULT;  // The current color.\n\n  // Conceptually, we split the string into segments divided by escape\n  // sequences.  Then we print one segment at a time.  At the end of\n  // each iteration, the str pointer advances to the beginning of the\n  // next segment.\n  for (;;) {\n    const char* p = strchr(str, '@');\n    if (p == NULL) {\n      ColoredPrintf(color, \"%s\", str);\n      return;\n    }\n\n    ColoredPrintf(color, \"%s\", std::string(str, p).c_str());\n\n    const char ch = p[1];\n    str = p + 2;\n    if (ch == '@') {\n      ColoredPrintf(color, \"@\");\n    } else if (ch == 'D') {\n      color = COLOR_DEFAULT;\n    } else if (ch == 'R') {\n      color = COLOR_RED;\n    } else if (ch == 'G') {\n      color = COLOR_GREEN;\n    } else if (ch == 'Y') {\n      color = COLOR_YELLOW;\n    } else {\n      --str;\n    }\n  }\n}\n\nstatic const char kColorEncodedHelpMessage[] =\n\"This program contains tests written using \" GTEST_NAME_ \". You can use the\\n\"\n\"following command line flags to control its behavior:\\n\"\n\"\\n\"\n\"Test Selection:\\n\"\n\"  @G--\" GTEST_FLAG_PREFIX_ \"list_tests@D\\n\"\n\"      List the names of all tests instead of running them. The name of\\n\"\n\"      TEST(Foo, Bar) is \\\"Foo.Bar\\\".\\n\"\n\"  @G--\" GTEST_FLAG_PREFIX_ \"filter=@YPOSTIVE_PATTERNS\"\n    \"[@G-@YNEGATIVE_PATTERNS]@D\\n\"\n\"      Run only the tests whose name matches one of the positive patterns but\\n\"\n\"      none of the negative patterns. '?' matches any single character; '*'\\n\"\n\"      matches any substring; ':' separates two patterns.\\n\"\n\"  @G--\" GTEST_FLAG_PREFIX_ \"also_run_disabled_tests@D\\n\"\n\"      Run all disabled tests too.\\n\"\n\"\\n\"\n\"Test Execution:\\n\"\n\"  @G--\" GTEST_FLAG_PREFIX_ \"repeat=@Y[COUNT]@D\\n\"\n\"      Run the tests repeatedly; use a negative count to repeat forever.\\n\"\n\"  @G--\" GTEST_FLAG_PREFIX_ \"shuffle@D\\n\"\n\"      Randomize tests' orders on every iteration.\\n\"\n\"  @G--\" GTEST_FLAG_PREFIX_ \"random_seed=@Y[NUMBER]@D\\n\"\n\"      Random number seed to use for shuffling test orders (between 1 and\\n\"\n\"      99999, or 0 to use a seed based on the current time).\\n\"\n\"\\n\"\n\"Test Output:\\n\"\n\"  @G--\" GTEST_FLAG_PREFIX_ \"color=@Y(@Gyes@Y|@Gno@Y|@Gauto@Y)@D\\n\"\n\"      Enable/disable colored output. The default is @Gauto@D.\\n\"\n\"  @G--\" GTEST_FLAG_PREFIX_ \"print_time=0@D\\n\"\n\"      Don't print the elapsed time of each test.\\n\"\n\"  @G--\" GTEST_FLAG_PREFIX_ \"output=xml@Y[@G:@YDIRECTORY_PATH@G\"\n    GTEST_PATH_SEP_ \"@Y|@G:@YFILE_PATH]@D\\n\"\n\"      Generate an XML report in the given directory or with the given file\\n\"\n\"      name. @YFILE_PATH@D defaults to @Gtest_details.xml@D.\\n\"\n#if GTEST_CAN_STREAM_RESULTS_\n\"  @G--\" GTEST_FLAG_PREFIX_ \"stream_result_to=@YHOST@G:@YPORT@D\\n\"\n\"      Stream test results to the given server.\\n\"\n#endif  // GTEST_CAN_STREAM_RESULTS_\n\"\\n\"\n\"Assertion Behavior:\\n\"\n#if GTEST_HAS_DEATH_TEST && !GTEST_OS_WINDOWS\n\"  @G--\" GTEST_FLAG_PREFIX_ \"death_test_style=@Y(@Gfast@Y|@Gthreadsafe@Y)@D\\n\"\n\"      Set the default death test style.\\n\"\n#endif  // GTEST_HAS_DEATH_TEST && !GTEST_OS_WINDOWS\n\"  @G--\" GTEST_FLAG_PREFIX_ \"break_on_failure@D\\n\"\n\"      Turn assertion failures into debugger break-points.\\n\"\n\"  @G--\" GTEST_FLAG_PREFIX_ \"throw_on_failure@D\\n\"\n\"      Turn assertion failures into C++ exceptions.\\n\"\n\"  @G--\" GTEST_FLAG_PREFIX_ \"catch_exceptions=0@D\\n\"\n\"      Do not report exceptions as test failures. Instead, allow them\\n\"\n\"      to crash the program or throw a pop-up (on Windows).\\n\"\n\"\\n\"\n\"Except for @G--\" GTEST_FLAG_PREFIX_ \"list_tests@D, you can alternatively set \"\n    \"the corresponding\\n\"\n\"environment variable of a flag (all letters in upper-case). For example, to\\n\"\n\"disable colored text output, you can either specify @G--\" GTEST_FLAG_PREFIX_\n    \"color=no@D or set\\n\"\n\"the @G\" GTEST_FLAG_PREFIX_UPPER_ \"COLOR@D environment variable to @Gno@D.\\n\"\n\"\\n\"\n\"For more information, please read the \" GTEST_NAME_ \" documentation at\\n\"\n\"@G\" GTEST_PROJECT_URL_ \"@D. If you find a bug in \" GTEST_NAME_ \"\\n\"\n\"(not one in your own code or tests), please report it to\\n\"\n\"@G<\" GTEST_DEV_EMAIL_ \">@D.\\n\";\n\nbool ParseGoogleTestFlag(const char* const arg) {\n  return ParseBoolFlag(arg, kAlsoRunDisabledTestsFlag,\n                       &GTEST_FLAG(also_run_disabled_tests)) ||\n      ParseBoolFlag(arg, kBreakOnFailureFlag,\n                    &GTEST_FLAG(break_on_failure)) ||\n      ParseBoolFlag(arg, kCatchExceptionsFlag,\n                    &GTEST_FLAG(catch_exceptions)) ||\n      ParseStringFlag(arg, kColorFlag, &GTEST_FLAG(color)) ||\n      ParseStringFlag(arg, kDeathTestStyleFlag,\n                      &GTEST_FLAG(death_test_style)) ||\n      ParseBoolFlag(arg, kDeathTestUseFork,\n                    &GTEST_FLAG(death_test_use_fork)) ||\n      ParseStringFlag(arg, kFilterFlag, &GTEST_FLAG(filter)) ||\n      ParseStringFlag(arg, kInternalRunDeathTestFlag,\n                      &GTEST_FLAG(internal_run_death_test)) ||\n      ParseBoolFlag(arg, kListTestsFlag, &GTEST_FLAG(list_tests)) ||\n      ParseStringFlag(arg, kOutputFlag, &GTEST_FLAG(output)) ||\n      ParseBoolFlag(arg, kPrintTimeFlag, &GTEST_FLAG(print_time)) ||\n      ParseInt32Flag(arg, kRandomSeedFlag, &GTEST_FLAG(random_seed)) ||\n      ParseInt32Flag(arg, kRepeatFlag, &GTEST_FLAG(repeat)) ||\n      ParseBoolFlag(arg, kShuffleFlag, &GTEST_FLAG(shuffle)) ||\n      ParseInt32Flag(arg, kStackTraceDepthFlag,\n                     &GTEST_FLAG(stack_trace_depth)) ||\n      ParseStringFlag(arg, kStreamResultToFlag,\n                      &GTEST_FLAG(stream_result_to)) ||\n      ParseBoolFlag(arg, kThrowOnFailureFlag,\n                    &GTEST_FLAG(throw_on_failure));\n}\n\n#if GTEST_USE_OWN_FLAGFILE_FLAG_\nvoid LoadFlagsFromFile(const std::string& path) {\n  FILE* flagfile = posix::FOpen(path.c_str(), \"r\");\n  if (!flagfile) {\n    fprintf(stderr,\n            \"Unable to open file \\\"%s\\\"\\n\",\n            GTEST_FLAG(flagfile).c_str());\n    fflush(stderr);\n    exit(EXIT_FAILURE);\n  }\n  std::string contents(ReadEntireFile(flagfile));\n  posix::FClose(flagfile);\n  std::vector<std::string> lines;\n  SplitString(contents, '\\n', &lines);\n  for (size_t i = 0; i < lines.size(); ++i) {\n    if (lines[i].empty())\n      continue;\n    if (!ParseGoogleTestFlag(lines[i].c_str()))\n      g_help_flag = true;\n  }\n}\n#endif  // GTEST_USE_OWN_FLAGFILE_FLAG_\n\n// Parses the command line for Google Test flags, without initializing\n// other parts of Google Test.  The type parameter CharType can be\n// instantiated to either char or wchar_t.\ntemplate <typename CharType>\nvoid ParseGoogleTestFlagsOnlyImpl(int* argc, CharType** argv) {\n  for (int i = 1; i < *argc; i++) {\n    const std::string arg_string = StreamableToString(argv[i]);\n    const char* const arg = arg_string.c_str();\n\n    using internal::ParseBoolFlag;\n    using internal::ParseInt32Flag;\n    using internal::ParseStringFlag;\n\n    bool remove_flag = false;\n    if (ParseGoogleTestFlag(arg)) {\n      remove_flag = true;\n#if GTEST_USE_OWN_FLAGFILE_FLAG_\n    } else if (ParseStringFlag(arg, kFlagfileFlag, &GTEST_FLAG(flagfile))) {\n      LoadFlagsFromFile(GTEST_FLAG(flagfile));\n      remove_flag = true;\n#endif  // GTEST_USE_OWN_FLAGFILE_FLAG_\n    } else if (arg_string == \"--help\" || arg_string == \"-h\" ||\n               arg_string == \"-?\" || arg_string == \"/?\" ||\n               HasGoogleTestFlagPrefix(arg)) {\n      // Both help flag and unrecognized Google Test flags (excluding\n      // internal ones) trigger help display.\n      g_help_flag = true;\n    }\n\n    if (remove_flag) {\n      // Shift the remainder of the argv list left by one.  Note\n      // that argv has (*argc + 1) elements, the last one always being\n      // NULL.  The following loop moves the trailing NULL element as\n      // well.\n      for (int j = i; j != *argc; j++) {\n        argv[j] = argv[j + 1];\n      }\n\n      // Decrements the argument count.\n      (*argc)--;\n\n      // We also need to decrement the iterator as we just removed\n      // an element.\n      i--;\n    }\n  }\n\n  if (g_help_flag) {\n    // We print the help here instead of in RUN_ALL_TESTS(), as the\n    // latter may not be called at all if the user is using Google\n    // Test with another testing framework.\n    PrintColorEncoded(kColorEncodedHelpMessage);\n  }\n}\n\n// Parses the command line for Google Test flags, without initializing\n// other parts of Google Test.\nvoid ParseGoogleTestFlagsOnly(int* argc, char** argv) {\n  ParseGoogleTestFlagsOnlyImpl(argc, argv);\n}\nvoid ParseGoogleTestFlagsOnly(int* argc, wchar_t** argv) {\n  ParseGoogleTestFlagsOnlyImpl(argc, argv);\n}\n\n// The internal implementation of InitGoogleTest().\n//\n// The type parameter CharType can be instantiated to either char or\n// wchar_t.\ntemplate <typename CharType>\nvoid InitGoogleTestImpl(int* argc, CharType** argv) {\n  // We don't want to run the initialization code twice.\n  if (GTestIsInitialized()) return;\n\n  if (*argc <= 0) return;\n\n  g_argvs.clear();\n  for (int i = 0; i != *argc; i++) {\n    g_argvs.push_back(StreamableToString(argv[i]));\n  }\n\n  ParseGoogleTestFlagsOnly(argc, argv);\n  GetUnitTestImpl()->PostFlagParsingInit();\n}\n\n}  // namespace internal\n\n// Initializes Google Test.  This must be called before calling\n// RUN_ALL_TESTS().  In particular, it parses a command line for the\n// flags that Google Test recognizes.  Whenever a Google Test flag is\n// seen, it is removed from argv, and *argc is decremented.\n//\n// No value is returned.  Instead, the Google Test flag variables are\n// updated.\n//\n// Calling the function for the second time has no user-visible effect.\nvoid InitGoogleTest(int* argc, char** argv) {\n#if defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)\n  GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_(argc, argv);\n#else  // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)\n  internal::InitGoogleTestImpl(argc, argv);\n#endif  // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)\n}\n\n// This overloaded version can be used in Windows programs compiled in\n// UNICODE mode.\nvoid InitGoogleTest(int* argc, wchar_t** argv) {\n#if defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)\n  GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_(argc, argv);\n#else  // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)\n  internal::InitGoogleTestImpl(argc, argv);\n#endif  // defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)\n}\n\nstd::string TempDir() {\n#if defined(GTEST_CUSTOM_TEMPDIR_FUNCTION_)\n    return GTEST_CUSTOM_TEMPDIR_FUNCTION_();\n#endif\n#if GTEST_OS_WINDOWS_MOBILE\n  return \"\\\\temp\\\\\";\n#elif GTEST_OS_WINDOWS\n  const char* temp_dir = internal::posix::GetEnv(\"TEMP\");\n  if (temp_dir == NULL || temp_dir[0] == '\\0')\n    return \"\\\\temp\\\\\";\n  else if (temp_dir[strlen(temp_dir) - 1] == '\\\\')\n    return temp_dir;\n  else\n    return std::string(temp_dir) + \"\\\\\";\n#elif GTEST_OS_LINUX_ANDROID\n  return \"/sdcard/\";\n#else\n  return \"/tmp/\";\n#endif  // GTEST_OS_WINDOWS_MOBILE\n}\n\n}  // namespace testing\n// Copyright 2005, Google Inc.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Author: wan@google.com (Zhanyong Wan), vladl@google.com (Vlad Losev)\n//\n// This file implements death tests.\n\n\n#if GTEST_HAS_DEATH_TEST\n\n# if GTEST_OS_MAC\n#  include <crt_externs.h>\n# endif  // GTEST_OS_MAC\n\n# include <errno.h>\n# include <fcntl.h>\n# include <limits.h>\n\n# if GTEST_OS_LINUX\n#  include <signal.h>\n# endif  // GTEST_OS_LINUX\n\n# include <stdarg.h>\n\n# if GTEST_OS_WINDOWS\n#  include <windows.h>\n# else\n#  include <sys/mman.h>\n#  include <sys/wait.h>\n# endif  // GTEST_OS_WINDOWS\n\n# if GTEST_OS_QNX\n#  include <spawn.h>\n# endif  // GTEST_OS_QNX\n\n#endif  // GTEST_HAS_DEATH_TEST\n\n\n// Indicates that this translation unit is part of Google Test's\n// implementation.  It must come before gtest-internal-inl.h is\n// included, or there will be a compiler error.  This trick exists to\n// prevent the accidental inclusion of gtest-internal-inl.h in the\n// user's code.\n#define GTEST_IMPLEMENTATION_ 1\n#undef GTEST_IMPLEMENTATION_\n\nnamespace testing {\n\n// Constants.\n\n// The default death test style.\nstatic const char kDefaultDeathTestStyle[] = \"fast\";\n\nGTEST_DEFINE_string_(\n    death_test_style,\n    internal::StringFromGTestEnv(\"death_test_style\", kDefaultDeathTestStyle),\n    \"Indicates how to run a death test in a forked child process: \"\n    \"\\\"threadsafe\\\" (child process re-executes the test binary \"\n    \"from the beginning, running only the specific death test) or \"\n    \"\\\"fast\\\" (child process runs the death test immediately \"\n    \"after forking).\");\n\nGTEST_DEFINE_bool_(\n    death_test_use_fork,\n    internal::BoolFromGTestEnv(\"death_test_use_fork\", false),\n    \"Instructs to use fork()/_exit() instead of clone() in death tests. \"\n    \"Ignored and always uses fork() on POSIX systems where clone() is not \"\n    \"implemented. Useful when running under valgrind or similar tools if \"\n    \"those do not support clone(). Valgrind 3.3.1 will just fail if \"\n    \"it sees an unsupported combination of clone() flags. \"\n    \"It is not recommended to use this flag w/o valgrind though it will \"\n    \"work in 99% of the cases. Once valgrind is fixed, this flag will \"\n    \"most likely be removed.\");\n\nnamespace internal {\nGTEST_DEFINE_string_(\n    internal_run_death_test, \"\",\n    \"Indicates the file, line number, temporal index of \"\n    \"the single death test to run, and a file descriptor to \"\n    \"which a success code may be sent, all separated by \"\n    \"the '|' characters.  This flag is specified if and only if the current \"\n    \"process is a sub-process launched for running a thread-safe \"\n    \"death test.  FOR INTERNAL USE ONLY.\");\n}  // namespace internal\n\n#if GTEST_HAS_DEATH_TEST\n\nnamespace internal {\n\n// Valid only for fast death tests. Indicates the code is running in the\n// child process of a fast style death test.\n# if !GTEST_OS_WINDOWS\nstatic bool g_in_fast_death_test_child = false;\n# endif\n\n// Returns a Boolean value indicating whether the caller is currently\n// executing in the context of the death test child process.  Tools such as\n// Valgrind heap checkers may need this to modify their behavior in death\n// tests.  IMPORTANT: This is an internal utility.  Using it may break the\n// implementation of death tests.  User code MUST NOT use it.\nbool InDeathTestChild() {\n# if GTEST_OS_WINDOWS\n\n  // On Windows, death tests are thread-safe regardless of the value of the\n  // death_test_style flag.\n  return !GTEST_FLAG(internal_run_death_test).empty();\n\n# else\n\n  if (GTEST_FLAG(death_test_style) == \"threadsafe\")\n    return !GTEST_FLAG(internal_run_death_test).empty();\n  else\n    return g_in_fast_death_test_child;\n#endif\n}\n\n}  // namespace internal\n\n// ExitedWithCode constructor.\nExitedWithCode::ExitedWithCode(int exit_code) : exit_code_(exit_code) {\n}\n\n// ExitedWithCode function-call operator.\nbool ExitedWithCode::operator()(int exit_status) const {\n# if GTEST_OS_WINDOWS\n\n  return exit_status == exit_code_;\n\n# else\n\n  return WIFEXITED(exit_status) && WEXITSTATUS(exit_status) == exit_code_;\n\n# endif  // GTEST_OS_WINDOWS\n}\n\n# if !GTEST_OS_WINDOWS\n// KilledBySignal constructor.\nKilledBySignal::KilledBySignal(int signum) : signum_(signum) {\n}\n\n// KilledBySignal function-call operator.\nbool KilledBySignal::operator()(int exit_status) const {\n#  if defined(GTEST_KILLED_BY_SIGNAL_OVERRIDE_)\n  {\n    bool result;\n    if (GTEST_KILLED_BY_SIGNAL_OVERRIDE_(signum_, exit_status, &result)) {\n      return result;\n    }\n  }\n#  endif  // defined(GTEST_KILLED_BY_SIGNAL_OVERRIDE_)\n  return WIFSIGNALED(exit_status) && WTERMSIG(exit_status) == signum_;\n}\n# endif  // !GTEST_OS_WINDOWS\n\nnamespace internal {\n\n// Utilities needed for death tests.\n\n// Generates a textual description of a given exit code, in the format\n// specified by wait(2).\nstatic std::string ExitSummary(int exit_code) {\n  Message m;\n\n# if GTEST_OS_WINDOWS\n\n  m << \"Exited with exit status \" << exit_code;\n\n# else\n\n  if (WIFEXITED(exit_code)) {\n    m << \"Exited with exit status \" << WEXITSTATUS(exit_code);\n  } else if (WIFSIGNALED(exit_code)) {\n    m << \"Terminated by signal \" << WTERMSIG(exit_code);\n  }\n#  ifdef WCOREDUMP\n  if (WCOREDUMP(exit_code)) {\n    m << \" (core dumped)\";\n  }\n#  endif\n# endif  // GTEST_OS_WINDOWS\n\n  return m.GetString();\n}\n\n// Returns true if exit_status describes a process that was terminated\n// by a signal, or exited normally with a nonzero exit code.\nbool ExitedUnsuccessfully(int exit_status) {\n  return !ExitedWithCode(0)(exit_status);\n}\n\n# if !GTEST_OS_WINDOWS\n// Generates a textual failure message when a death test finds more than\n// one thread running, or cannot determine the number of threads, prior\n// to executing the given statement.  It is the responsibility of the\n// caller not to pass a thread_count of 1.\nstatic std::string DeathTestThreadWarning(size_t thread_count) {\n  Message msg;\n  msg << \"Death tests use fork(), which is unsafe particularly\"\n      << \" in a threaded context. For this test, \" << GTEST_NAME_ << \" \";\n  if (thread_count == 0)\n    msg << \"couldn't detect the number of threads.\";\n  else\n    msg << \"detected \" << thread_count << \" threads.\";\n  return msg.GetString();\n}\n# endif  // !GTEST_OS_WINDOWS\n\n// Flag characters for reporting a death test that did not die.\nstatic const char kDeathTestLived = 'L';\nstatic const char kDeathTestReturned = 'R';\nstatic const char kDeathTestThrew = 'T';\nstatic const char kDeathTestInternalError = 'I';\n\n// An enumeration describing all of the possible ways that a death test can\n// conclude.  DIED means that the process died while executing the test\n// code; LIVED means that process lived beyond the end of the test code;\n// RETURNED means that the test statement attempted to execute a return\n// statement, which is not allowed; THREW means that the test statement\n// returned control by throwing an exception.  IN_PROGRESS means the test\n// has not yet concluded.\n// TODO(vladl@google.com): Unify names and possibly values for\n// AbortReason, DeathTestOutcome, and flag characters above.\nenum DeathTestOutcome { IN_PROGRESS, DIED, LIVED, RETURNED, THREW };\n\n// Routine for aborting the program which is safe to call from an\n// exec-style death test child process, in which case the error\n// message is propagated back to the parent process.  Otherwise, the\n// message is simply printed to stderr.  In either case, the program\n// then exits with status 1.\nvoid DeathTestAbort(const std::string& message) {\n  // On a POSIX system, this function may be called from a threadsafe-style\n  // death test child process, which operates on a very small stack.  Use\n  // the heap for any additional non-minuscule memory requirements.\n  const InternalRunDeathTestFlag* const flag =\n      GetUnitTestImpl()->internal_run_death_test_flag();\n  if (flag != NULL) {\n    FILE* parent = posix::FDOpen(flag->write_fd(), \"w\");\n    fputc(kDeathTestInternalError, parent);\n    fprintf(parent, \"%s\", message.c_str());\n    fflush(parent);\n    _exit(1);\n  } else {\n    fprintf(stderr, \"%s\", message.c_str());\n    fflush(stderr);\n    posix::Abort();\n  }\n}\n\n// A replacement for CHECK that calls DeathTestAbort if the assertion\n// fails.\n# define GTEST_DEATH_TEST_CHECK_(expression) \\\n  do { \\\n    if (!::testing::internal::IsTrue(expression)) { \\\n      DeathTestAbort( \\\n          ::std::string(\"CHECK failed: File \") + __FILE__ +  \", line \" \\\n          + ::testing::internal::StreamableToString(__LINE__) + \": \" \\\n          + #expression); \\\n    } \\\n  } while (::testing::internal::AlwaysFalse())\n\n// This macro is similar to GTEST_DEATH_TEST_CHECK_, but it is meant for\n// evaluating any system call that fulfills two conditions: it must return\n// -1 on failure, and set errno to EINTR when it is interrupted and\n// should be tried again.  The macro expands to a loop that repeatedly\n// evaluates the expression as long as it evaluates to -1 and sets\n// errno to EINTR.  If the expression evaluates to -1 but errno is\n// something other than EINTR, DeathTestAbort is called.\n# define GTEST_DEATH_TEST_CHECK_SYSCALL_(expression) \\\n  do { \\\n    int gtest_retval; \\\n    do { \\\n      gtest_retval = (expression); \\\n    } while (gtest_retval == -1 && errno == EINTR); \\\n    if (gtest_retval == -1) { \\\n      DeathTestAbort( \\\n          ::std::string(\"CHECK failed: File \") + __FILE__ + \", line \" \\\n          + ::testing::internal::StreamableToString(__LINE__) + \": \" \\\n          + #expression + \" != -1\"); \\\n    } \\\n  } while (::testing::internal::AlwaysFalse())\n\n// Returns the message describing the last system error in errno.\nstd::string GetLastErrnoDescription() {\n    return errno == 0 ? \"\" : posix::StrError(errno);\n}\n\n// This is called from a death test parent process to read a failure\n// message from the death test child process and log it with the FATAL\n// severity. On Windows, the message is read from a pipe handle. On other\n// platforms, it is read from a file descriptor.\nstatic void FailFromInternalError(int fd) {\n  Message error;\n  char buffer[256];\n  int num_read;\n\n  do {\n    while ((num_read = posix::Read(fd, buffer, 255)) > 0) {\n      buffer[num_read] = '\\0';\n      error << buffer;\n    }\n  } while (num_read == -1 && errno == EINTR);\n\n  if (num_read == 0) {\n    GTEST_LOG_(FATAL) << error.GetString();\n  } else {\n    const int last_error = errno;\n    GTEST_LOG_(FATAL) << \"Error while reading death test internal: \"\n                      << GetLastErrnoDescription() << \" [\" << last_error << \"]\";\n  }\n}\n\n// Death test constructor.  Increments the running death test count\n// for the current test.\nDeathTest::DeathTest() {\n  TestInfo* const info = GetUnitTestImpl()->current_test_info();\n  if (info == NULL) {\n    DeathTestAbort(\"Cannot run a death test outside of a TEST or \"\n                   \"TEST_F construct\");\n  }\n}\n\n// Creates and returns a death test by dispatching to the current\n// death test factory.\nbool DeathTest::Create(const char* statement, const RE* regex,\n                       const char* file, int line, DeathTest** test) {\n  return GetUnitTestImpl()->death_test_factory()->Create(\n      statement, regex, file, line, test);\n}\n\nconst char* DeathTest::LastMessage() {\n  return last_death_test_message_.c_str();\n}\n\nvoid DeathTest::set_last_death_test_message(const std::string& message) {\n  last_death_test_message_ = message;\n}\n\nstd::string DeathTest::last_death_test_message_;\n\n// Provides cross platform implementation for some death functionality.\nclass DeathTestImpl : public DeathTest {\n protected:\n  DeathTestImpl(const char* a_statement, const RE* a_regex)\n      : statement_(a_statement),\n        regex_(a_regex),\n        spawned_(false),\n        status_(-1),\n        outcome_(IN_PROGRESS),\n        read_fd_(-1),\n        write_fd_(-1) {}\n\n  // read_fd_ is expected to be closed and cleared by a derived class.\n  ~DeathTestImpl() { GTEST_DEATH_TEST_CHECK_(read_fd_ == -1); }\n\n  void Abort(AbortReason reason);\n  virtual bool Passed(bool status_ok);\n\n  const char* statement() const { return statement_; }\n  const RE* regex() const { return regex_; }\n  bool spawned() const { return spawned_; }\n  void set_spawned(bool is_spawned) { spawned_ = is_spawned; }\n  int status() const { return status_; }\n  void set_status(int a_status) { status_ = a_status; }\n  DeathTestOutcome outcome() const { return outcome_; }\n  void set_outcome(DeathTestOutcome an_outcome) { outcome_ = an_outcome; }\n  int read_fd() const { return read_fd_; }\n  void set_read_fd(int fd) { read_fd_ = fd; }\n  int write_fd() const { return write_fd_; }\n  void set_write_fd(int fd) { write_fd_ = fd; }\n\n  // Called in the parent process only. Reads the result code of the death\n  // test child process via a pipe, interprets it to set the outcome_\n  // member, and closes read_fd_.  Outputs diagnostics and terminates in\n  // case of unexpected codes.\n  void ReadAndInterpretStatusByte();\n\n private:\n  // The textual content of the code this object is testing.  This class\n  // doesn't own this string and should not attempt to delete it.\n  const char* const statement_;\n  // The regular expression which test output must match.  DeathTestImpl\n  // doesn't own this object and should not attempt to delete it.\n  const RE* const regex_;\n  // True if the death test child process has been successfully spawned.\n  bool spawned_;\n  // The exit status of the child process.\n  int status_;\n  // How the death test concluded.\n  DeathTestOutcome outcome_;\n  // Descriptor to the read end of the pipe to the child process.  It is\n  // always -1 in the child process.  The child keeps its write end of the\n  // pipe in write_fd_.\n  int read_fd_;\n  // Descriptor to the child's write end of the pipe to the parent process.\n  // It is always -1 in the parent process.  The parent keeps its end of the\n  // pipe in read_fd_.\n  int write_fd_;\n};\n\n// Called in the parent process only. Reads the result code of the death\n// test child process via a pipe, interprets it to set the outcome_\n// member, and closes read_fd_.  Outputs diagnostics and terminates in\n// case of unexpected codes.\nvoid DeathTestImpl::ReadAndInterpretStatusByte() {\n  char flag;\n  int bytes_read;\n\n  // The read() here blocks until data is available (signifying the\n  // failure of the death test) or until the pipe is closed (signifying\n  // its success), so it's okay to call this in the parent before\n  // the child process has exited.\n  do {\n    bytes_read = posix::Read(read_fd(), &flag, 1);\n  } while (bytes_read == -1 && errno == EINTR);\n\n  if (bytes_read == 0) {\n    set_outcome(DIED);\n  } else if (bytes_read == 1) {\n    switch (flag) {\n      case kDeathTestReturned:\n        set_outcome(RETURNED);\n        break;\n      case kDeathTestThrew:\n        set_outcome(THREW);\n        break;\n      case kDeathTestLived:\n        set_outcome(LIVED);\n        break;\n      case kDeathTestInternalError:\n        FailFromInternalError(read_fd());  // Does not return.\n        break;\n      default:\n        GTEST_LOG_(FATAL) << \"Death test child process reported \"\n                          << \"unexpected status byte (\"\n                          << static_cast<unsigned int>(flag) << \")\";\n    }\n  } else {\n    GTEST_LOG_(FATAL) << \"Read from death test child process failed: \"\n                      << GetLastErrnoDescription();\n  }\n  GTEST_DEATH_TEST_CHECK_SYSCALL_(posix::Close(read_fd()));\n  set_read_fd(-1);\n}\n\n// Signals that the death test code which should have exited, didn't.\n// Should be called only in a death test child process.\n// Writes a status byte to the child's status file descriptor, then\n// calls _exit(1).\nvoid DeathTestImpl::Abort(AbortReason reason) {\n  // The parent process considers the death test to be a failure if\n  // it finds any data in our pipe.  So, here we write a single flag byte\n  // to the pipe, then exit.\n  const char status_ch =\n      reason == TEST_DID_NOT_DIE ? kDeathTestLived :\n      reason == TEST_THREW_EXCEPTION ? kDeathTestThrew : kDeathTestReturned;\n\n  GTEST_DEATH_TEST_CHECK_SYSCALL_(posix::Write(write_fd(), &status_ch, 1));\n  // We are leaking the descriptor here because on some platforms (i.e.,\n  // when built as Windows DLL), destructors of global objects will still\n  // run after calling _exit(). On such systems, write_fd_ will be\n  // indirectly closed from the destructor of UnitTestImpl, causing double\n  // close if it is also closed here. On debug configurations, double close\n  // may assert. As there are no in-process buffers to flush here, we are\n  // relying on the OS to close the descriptor after the process terminates\n  // when the destructors are not run.\n  _exit(1);  // Exits w/o any normal exit hooks (we were supposed to crash)\n}\n\n// Returns an indented copy of stderr output for a death test.\n// This makes distinguishing death test output lines from regular log lines\n// much easier.\nstatic ::std::string FormatDeathTestOutput(const ::std::string& output) {\n  ::std::string ret;\n  for (size_t at = 0; ; ) {\n    const size_t line_end = output.find('\\n', at);\n    ret += \"[  DEATH   ] \";\n    if (line_end == ::std::string::npos) {\n      ret += output.substr(at);\n      break;\n    }\n    ret += output.substr(at, line_end + 1 - at);\n    at = line_end + 1;\n  }\n  return ret;\n}\n\n// Assesses the success or failure of a death test, using both private\n// members which have previously been set, and one argument:\n//\n// Private data members:\n//   outcome:  An enumeration describing how the death test\n//             concluded: DIED, LIVED, THREW, or RETURNED.  The death test\n//             fails in the latter three cases.\n//   status:   The exit status of the child process. On *nix, it is in the\n//             in the format specified by wait(2). On Windows, this is the\n//             value supplied to the ExitProcess() API or a numeric code\n//             of the exception that terminated the program.\n//   regex:    A regular expression object to be applied to\n//             the test's captured standard error output; the death test\n//             fails if it does not match.\n//\n// Argument:\n//   status_ok: true if exit_status is acceptable in the context of\n//              this particular death test, which fails if it is false\n//\n// Returns true iff all of the above conditions are met.  Otherwise, the\n// first failing condition, in the order given above, is the one that is\n// reported. Also sets the last death test message string.\nbool DeathTestImpl::Passed(bool status_ok) {\n  if (!spawned())\n    return false;\n\n  const std::string error_message = GetCapturedStderr();\n\n  bool success = false;\n  Message buffer;\n\n  buffer << \"Death test: \" << statement() << \"\\n\";\n  switch (outcome()) {\n    case LIVED:\n      buffer << \"    Result: failed to die.\\n\"\n             << \" Error msg:\\n\" << FormatDeathTestOutput(error_message);\n      break;\n    case THREW:\n      buffer << \"    Result: threw an exception.\\n\"\n             << \" Error msg:\\n\" << FormatDeathTestOutput(error_message);\n      break;\n    case RETURNED:\n      buffer << \"    Result: illegal return in test statement.\\n\"\n             << \" Error msg:\\n\" << FormatDeathTestOutput(error_message);\n      break;\n    case DIED:\n      if (status_ok) {\n        const bool matched = RE::PartialMatch(error_message.c_str(), *regex());\n        if (matched) {\n          success = true;\n        } else {\n          buffer << \"    Result: died but not with expected error.\\n\"\n                 << \"  Expected: \" << regex()->pattern() << \"\\n\"\n                 << \"Actual msg:\\n\" << FormatDeathTestOutput(error_message);\n        }\n      } else {\n        buffer << \"    Result: died but not with expected exit code:\\n\"\n               << \"            \" << ExitSummary(status()) << \"\\n\"\n               << \"Actual msg:\\n\" << FormatDeathTestOutput(error_message);\n      }\n      break;\n    case IN_PROGRESS:\n    default:\n      GTEST_LOG_(FATAL)\n          << \"DeathTest::Passed somehow called before conclusion of test\";\n  }\n\n  DeathTest::set_last_death_test_message(buffer.GetString());\n  return success;\n}\n\n# if GTEST_OS_WINDOWS\n// WindowsDeathTest implements death tests on Windows. Due to the\n// specifics of starting new processes on Windows, death tests there are\n// always threadsafe, and Google Test considers the\n// --gtest_death_test_style=fast setting to be equivalent to\n// --gtest_death_test_style=threadsafe there.\n//\n// A few implementation notes:  Like the Linux version, the Windows\n// implementation uses pipes for child-to-parent communication. But due to\n// the specifics of pipes on Windows, some extra steps are required:\n//\n// 1. The parent creates a communication pipe and stores handles to both\n//    ends of it.\n// 2. The parent starts the child and provides it with the information\n//    necessary to acquire the handle to the write end of the pipe.\n// 3. The child acquires the write end of the pipe and signals the parent\n//    using a Windows event.\n// 4. Now the parent can release the write end of the pipe on its side. If\n//    this is done before step 3, the object's reference count goes down to\n//    0 and it is destroyed, preventing the child from acquiring it. The\n//    parent now has to release it, or read operations on the read end of\n//    the pipe will not return when the child terminates.\n// 5. The parent reads child's output through the pipe (outcome code and\n//    any possible error messages) from the pipe, and its stderr and then\n//    determines whether to fail the test.\n//\n// Note: to distinguish Win32 API calls from the local method and function\n// calls, the former are explicitly resolved in the global namespace.\n//\nclass WindowsDeathTest : public DeathTestImpl {\n public:\n  WindowsDeathTest(const char* a_statement,\n                   const RE* a_regex,\n                   const char* file,\n                   int line)\n      : DeathTestImpl(a_statement, a_regex), file_(file), line_(line) {}\n\n  // All of these virtual functions are inherited from DeathTest.\n  virtual int Wait();\n  virtual TestRole AssumeRole();\n\n private:\n  // The name of the file in which the death test is located.\n  const char* const file_;\n  // The line number on which the death test is located.\n  const int line_;\n  // Handle to the write end of the pipe to the child process.\n  AutoHandle write_handle_;\n  // Child process handle.\n  AutoHandle child_handle_;\n  // Event the child process uses to signal the parent that it has\n  // acquired the handle to the write end of the pipe. After seeing this\n  // event the parent can release its own handles to make sure its\n  // ReadFile() calls return when the child terminates.\n  AutoHandle event_handle_;\n};\n\n// Waits for the child in a death test to exit, returning its exit\n// status, or 0 if no child process exists.  As a side effect, sets the\n// outcome data member.\nint WindowsDeathTest::Wait() {\n  if (!spawned())\n    return 0;\n\n  // Wait until the child either signals that it has acquired the write end\n  // of the pipe or it dies.\n  const HANDLE wait_handles[2] = { child_handle_.Get(), event_handle_.Get() };\n  switch (::WaitForMultipleObjects(2,\n                                   wait_handles,\n                                   FALSE,  // Waits for any of the handles.\n                                   INFINITE)) {\n    case WAIT_OBJECT_0:\n    case WAIT_OBJECT_0 + 1:\n      break;\n    default:\n      GTEST_DEATH_TEST_CHECK_(false);  // Should not get here.\n  }\n\n  // The child has acquired the write end of the pipe or exited.\n  // We release the handle on our side and continue.\n  write_handle_.Reset();\n  event_handle_.Reset();\n\n  ReadAndInterpretStatusByte();\n\n  // Waits for the child process to exit if it haven't already. This\n  // returns immediately if the child has already exited, regardless of\n  // whether previous calls to WaitForMultipleObjects synchronized on this\n  // handle or not.\n  GTEST_DEATH_TEST_CHECK_(\n      WAIT_OBJECT_0 == ::WaitForSingleObject(child_handle_.Get(),\n                                             INFINITE));\n  DWORD status_code;\n  GTEST_DEATH_TEST_CHECK_(\n      ::GetExitCodeProcess(child_handle_.Get(), &status_code) != FALSE);\n  child_handle_.Reset();\n  set_status(static_cast<int>(status_code));\n  return status();\n}\n\n// The AssumeRole process for a Windows death test.  It creates a child\n// process with the same executable as the current process to run the\n// death test.  The child process is given the --gtest_filter and\n// --gtest_internal_run_death_test flags such that it knows to run the\n// current death test only.\nDeathTest::TestRole WindowsDeathTest::AssumeRole() {\n  const UnitTestImpl* const impl = GetUnitTestImpl();\n  const InternalRunDeathTestFlag* const flag =\n      impl->internal_run_death_test_flag();\n  const TestInfo* const info = impl->current_test_info();\n  const int death_test_index = info->result()->death_test_count();\n\n  if (flag != NULL) {\n    // ParseInternalRunDeathTestFlag() has performed all the necessary\n    // processing.\n    set_write_fd(flag->write_fd());\n    return EXECUTE_TEST;\n  }\n\n  // WindowsDeathTest uses an anonymous pipe to communicate results of\n  // a death test.\n  SECURITY_ATTRIBUTES handles_are_inheritable = {\n    sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };\n  HANDLE read_handle, write_handle;\n  GTEST_DEATH_TEST_CHECK_(\n      ::CreatePipe(&read_handle, &write_handle, &handles_are_inheritable,\n                   0)  // Default buffer size.\n      != FALSE);\n  set_read_fd(::_open_osfhandle(reinterpret_cast<intptr_t>(read_handle),\n                                O_RDONLY));\n  write_handle_.Reset(write_handle);\n  event_handle_.Reset(::CreateEvent(\n      &handles_are_inheritable,\n      TRUE,    // The event will automatically reset to non-signaled state.\n      FALSE,   // The initial state is non-signalled.\n      NULL));  // The even is unnamed.\n  GTEST_DEATH_TEST_CHECK_(event_handle_.Get() != NULL);\n  const std::string filter_flag =\n      std::string(\"--\") + GTEST_FLAG_PREFIX_ + kFilterFlag + \"=\" +\n      info->test_case_name() + \".\" + info->name();\n  const std::string internal_flag =\n      std::string(\"--\") + GTEST_FLAG_PREFIX_ + kInternalRunDeathTestFlag +\n      \"=\" + file_ + \"|\" + StreamableToString(line_) + \"|\" +\n      StreamableToString(death_test_index) + \"|\" +\n      StreamableToString(static_cast<unsigned int>(::GetCurrentProcessId())) +\n      // size_t has the same width as pointers on both 32-bit and 64-bit\n      // Windows platforms.\n      // See http://msdn.microsoft.com/en-us/library/tcxf1dw6.aspx.\n      \"|\" + StreamableToString(reinterpret_cast<size_t>(write_handle)) +\n      \"|\" + StreamableToString(reinterpret_cast<size_t>(event_handle_.Get()));\n\n  char executable_path[_MAX_PATH + 1];  // NOLINT\n  GTEST_DEATH_TEST_CHECK_(\n      _MAX_PATH + 1 != ::GetModuleFileNameA(NULL,\n                                            executable_path,\n                                            _MAX_PATH));\n\n  std::string command_line =\n      std::string(::GetCommandLineA()) + \" \" + filter_flag + \" \\\"\" +\n      internal_flag + \"\\\"\";\n\n  DeathTest::set_last_death_test_message(\"\");\n\n  CaptureStderr();\n  // Flush the log buffers since the log streams are shared with the child.\n  FlushInfoLog();\n\n  // The child process will share the standard handles with the parent.\n  STARTUPINFOA startup_info;\n  memset(&startup_info, 0, sizeof(STARTUPINFO));\n  startup_info.dwFlags = STARTF_USESTDHANDLES;\n  startup_info.hStdInput = ::GetStdHandle(STD_INPUT_HANDLE);\n  startup_info.hStdOutput = ::GetStdHandle(STD_OUTPUT_HANDLE);\n  startup_info.hStdError = ::GetStdHandle(STD_ERROR_HANDLE);\n\n  PROCESS_INFORMATION process_info;\n  GTEST_DEATH_TEST_CHECK_(::CreateProcessA(\n      executable_path,\n      const_cast<char*>(command_line.c_str()),\n      NULL,   // Retuned process handle is not inheritable.\n      NULL,   // Retuned thread handle is not inheritable.\n      TRUE,   // Child inherits all inheritable handles (for write_handle_).\n      0x0,    // Default creation flags.\n      NULL,   // Inherit the parent's environment.\n      UnitTest::GetInstance()->original_working_dir(),\n      &startup_info,\n      &process_info) != FALSE);\n  child_handle_.Reset(process_info.hProcess);\n  ::CloseHandle(process_info.hThread);\n  set_spawned(true);\n  return OVERSEE_TEST;\n}\n# else  // We are not on Windows.\n\n// ForkingDeathTest provides implementations for most of the abstract\n// methods of the DeathTest interface.  Only the AssumeRole method is\n// left undefined.\nclass ForkingDeathTest : public DeathTestImpl {\n public:\n  ForkingDeathTest(const char* statement, const RE* regex);\n\n  // All of these virtual functions are inherited from DeathTest.\n  virtual int Wait();\n\n protected:\n  void set_child_pid(pid_t child_pid) { child_pid_ = child_pid; }\n\n private:\n  // PID of child process during death test; 0 in the child process itself.\n  pid_t child_pid_;\n};\n\n// Constructs a ForkingDeathTest.\nForkingDeathTest::ForkingDeathTest(const char* a_statement, const RE* a_regex)\n    : DeathTestImpl(a_statement, a_regex),\n      child_pid_(-1) {}\n\n// Waits for the child in a death test to exit, returning its exit\n// status, or 0 if no child process exists.  As a side effect, sets the\n// outcome data member.\nint ForkingDeathTest::Wait() {\n  if (!spawned())\n    return 0;\n\n  ReadAndInterpretStatusByte();\n\n  int status_value;\n  GTEST_DEATH_TEST_CHECK_SYSCALL_(waitpid(child_pid_, &status_value, 0));\n  set_status(status_value);\n  return status_value;\n}\n\n// A concrete death test class that forks, then immediately runs the test\n// in the child process.\nclass NoExecDeathTest : public ForkingDeathTest {\n public:\n  NoExecDeathTest(const char* a_statement, const RE* a_regex) :\n      ForkingDeathTest(a_statement, a_regex) { }\n  virtual TestRole AssumeRole();\n};\n\n// The AssumeRole process for a fork-and-run death test.  It implements a\n// straightforward fork, with a simple pipe to transmit the status byte.\nDeathTest::TestRole NoExecDeathTest::AssumeRole() {\n  const size_t thread_count = GetThreadCount();\n  if (thread_count != 1) {\n    GTEST_LOG_(WARNING) << DeathTestThreadWarning(thread_count);\n  }\n\n  int pipe_fd[2];\n  GTEST_DEATH_TEST_CHECK_(pipe(pipe_fd) != -1);\n\n  DeathTest::set_last_death_test_message(\"\");\n  CaptureStderr();\n  // When we fork the process below, the log file buffers are copied, but the\n  // file descriptors are shared.  We flush all log files here so that closing\n  // the file descriptors in the child process doesn't throw off the\n  // synchronization between descriptors and buffers in the parent process.\n  // This is as close to the fork as possible to avoid a race condition in case\n  // there are multiple threads running before the death test, and another\n  // thread writes to the log file.\n  FlushInfoLog();\n\n  const pid_t child_pid = fork();\n  GTEST_DEATH_TEST_CHECK_(child_pid != -1);\n  set_child_pid(child_pid);\n  if (child_pid == 0) {\n    GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[0]));\n    set_write_fd(pipe_fd[1]);\n    // Redirects all logging to stderr in the child process to prevent\n    // concurrent writes to the log files.  We capture stderr in the parent\n    // process and append the child process' output to a log.\n    LogToStderr();\n    // Event forwarding to the listeners of event listener API mush be shut\n    // down in death test subprocesses.\n    GetUnitTestImpl()->listeners()->SuppressEventForwarding();\n    g_in_fast_death_test_child = true;\n    return EXECUTE_TEST;\n  } else {\n    GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[1]));\n    set_read_fd(pipe_fd[0]);\n    set_spawned(true);\n    return OVERSEE_TEST;\n  }\n}\n\n// A concrete death test class that forks and re-executes the main\n// program from the beginning, with command-line flags set that cause\n// only this specific death test to be run.\nclass ExecDeathTest : public ForkingDeathTest {\n public:\n  ExecDeathTest(const char* a_statement, const RE* a_regex,\n                const char* file, int line) :\n      ForkingDeathTest(a_statement, a_regex), file_(file), line_(line) { }\n  virtual TestRole AssumeRole();\n private:\n  static ::std::vector<std::string> GetArgvsForDeathTestChildProcess() {\n    ::std::vector<std::string> args = GetInjectableArgvs();\n#  if defined(GTEST_EXTRA_DEATH_TEST_COMMAND_LINE_ARGS_)\n    ::std::vector<std::string> extra_args =\n        GTEST_EXTRA_DEATH_TEST_COMMAND_LINE_ARGS_();\n    args.insert(args.end(), extra_args.begin(), extra_args.end());\n#  endif  // defined(GTEST_EXTRA_DEATH_TEST_COMMAND_LINE_ARGS_)\n    return args;\n  }\n  // The name of the file in which the death test is located.\n  const char* const file_;\n  // The line number on which the death test is located.\n  const int line_;\n};\n\n// Utility class for accumulating command-line arguments.\nclass Arguments {\n public:\n  Arguments() {\n    args_.push_back(NULL);\n  }\n\n  ~Arguments() {\n    for (std::vector<char*>::iterator i = args_.begin(); i != args_.end();\n         ++i) {\n      free(*i);\n    }\n  }\n  void AddArgument(const char* argument) {\n    args_.insert(args_.end() - 1, posix::StrDup(argument));\n  }\n\n  template <typename Str>\n  void AddArguments(const ::std::vector<Str>& arguments) {\n    for (typename ::std::vector<Str>::const_iterator i = arguments.begin();\n         i != arguments.end();\n         ++i) {\n      args_.insert(args_.end() - 1, posix::StrDup(i->c_str()));\n    }\n  }\n  char* const* Argv() {\n    return &args_[0];\n  }\n\n private:\n  std::vector<char*> args_;\n};\n\n// A struct that encompasses the arguments to the child process of a\n// threadsafe-style death test process.\nstruct ExecDeathTestArgs {\n  char* const* argv;  // Command-line arguments for the child's call to exec\n  int close_fd;       // File descriptor to close; the read end of a pipe\n};\n\n#  if GTEST_OS_MAC\ninline char** GetEnviron() {\n  // When Google Test is built as a framework on MacOS X, the environ variable\n  // is unavailable. Apple's documentation (man environ) recommends using\n  // _NSGetEnviron() instead.\n  return *_NSGetEnviron();\n}\n#  else\n// Some POSIX platforms expect you to declare environ. extern \"C\" makes\n// it reside in the global namespace.\nextern \"C\" char** environ;\ninline char** GetEnviron() { return environ; }\n#  endif  // GTEST_OS_MAC\n\n#  if !GTEST_OS_QNX\n// The main function for a threadsafe-style death test child process.\n// This function is called in a clone()-ed process and thus must avoid\n// any potentially unsafe operations like malloc or libc functions.\nstatic int ExecDeathTestChildMain(void* child_arg) {\n  ExecDeathTestArgs* const args = static_cast<ExecDeathTestArgs*>(child_arg);\n  GTEST_DEATH_TEST_CHECK_SYSCALL_(close(args->close_fd));\n\n  // We need to execute the test program in the same environment where\n  // it was originally invoked.  Therefore we change to the original\n  // working directory first.\n  const char* const original_dir =\n      UnitTest::GetInstance()->original_working_dir();\n  // We can safely call chdir() as it's a direct system call.\n  if (chdir(original_dir) != 0) {\n    DeathTestAbort(std::string(\"chdir(\\\"\") + original_dir + \"\\\") failed: \" +\n                   GetLastErrnoDescription());\n    return EXIT_FAILURE;\n  }\n\n  // We can safely call execve() as it's a direct system call.  We\n  // cannot use execvp() as it's a libc function and thus potentially\n  // unsafe.  Since execve() doesn't search the PATH, the user must\n  // invoke the test program via a valid path that contains at least\n  // one path separator.\n  execve(args->argv[0], args->argv, GetEnviron());\n  DeathTestAbort(std::string(\"execve(\") + args->argv[0] + \", ...) in \" +\n                 original_dir + \" failed: \" +\n                 GetLastErrnoDescription());\n  return EXIT_FAILURE;\n}\n#  endif  // !GTEST_OS_QNX\n\n// Two utility routines that together determine the direction the stack\n// grows.\n// This could be accomplished more elegantly by a single recursive\n// function, but we want to guard against the unlikely possibility of\n// a smart compiler optimizing the recursion away.\n//\n// GTEST_NO_INLINE_ is required to prevent GCC 4.6 from inlining\n// StackLowerThanAddress into StackGrowsDown, which then doesn't give\n// correct answer.\nvoid StackLowerThanAddress(const void* ptr, bool* result) GTEST_NO_INLINE_;\nvoid StackLowerThanAddress(const void* ptr, bool* result) {\n  int dummy;\n  *result = (&dummy < ptr);\n}\n\n// Make sure AddressSanitizer does not tamper with the stack here.\nGTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_\nbool StackGrowsDown() {\n  int dummy;\n  bool result;\n  StackLowerThanAddress(&dummy, &result);\n  return result;\n}\n\n// Spawns a child process with the same executable as the current process in\n// a thread-safe manner and instructs it to run the death test.  The\n// implementation uses fork(2) + exec.  On systems where clone(2) is\n// available, it is used instead, being slightly more thread-safe.  On QNX,\n// fork supports only single-threaded environments, so this function uses\n// spawn(2) there instead.  The function dies with an error message if\n// anything goes wrong.\nstatic pid_t ExecDeathTestSpawnChild(char* const* argv, int close_fd) {\n  ExecDeathTestArgs args = { argv, close_fd };\n  pid_t child_pid = -1;\n\n#  if GTEST_OS_QNX\n  // Obtains the current directory and sets it to be closed in the child\n  // process.\n  const int cwd_fd = open(\".\", O_RDONLY);\n  GTEST_DEATH_TEST_CHECK_(cwd_fd != -1);\n  GTEST_DEATH_TEST_CHECK_SYSCALL_(fcntl(cwd_fd, F_SETFD, FD_CLOEXEC));\n  // We need to execute the test program in the same environment where\n  // it was originally invoked.  Therefore we change to the original\n  // working directory first.\n  const char* const original_dir =\n      UnitTest::GetInstance()->original_working_dir();\n  // We can safely call chdir() as it's a direct system call.\n  if (chdir(original_dir) != 0) {\n    DeathTestAbort(std::string(\"chdir(\\\"\") + original_dir + \"\\\") failed: \" +\n                   GetLastErrnoDescription());\n    return EXIT_FAILURE;\n  }\n\n  int fd_flags;\n  // Set close_fd to be closed after spawn.\n  GTEST_DEATH_TEST_CHECK_SYSCALL_(fd_flags = fcntl(close_fd, F_GETFD));\n  GTEST_DEATH_TEST_CHECK_SYSCALL_(fcntl(close_fd, F_SETFD,\n                                        fd_flags | FD_CLOEXEC));\n  struct inheritance inherit = {0};\n  // spawn is a system call.\n  child_pid = spawn(args.argv[0], 0, NULL, &inherit, args.argv, GetEnviron());\n  // Restores the current working directory.\n  GTEST_DEATH_TEST_CHECK_(fchdir(cwd_fd) != -1);\n  GTEST_DEATH_TEST_CHECK_SYSCALL_(close(cwd_fd));\n\n#  else   // GTEST_OS_QNX\n#   if GTEST_OS_LINUX\n  // When a SIGPROF signal is received while fork() or clone() are executing,\n  // the process may hang. To avoid this, we ignore SIGPROF here and re-enable\n  // it after the call to fork()/clone() is complete.\n  struct sigaction saved_sigprof_action;\n  struct sigaction ignore_sigprof_action;\n  memset(&ignore_sigprof_action, 0, sizeof(ignore_sigprof_action));\n  sigemptyset(&ignore_sigprof_action.sa_mask);\n  ignore_sigprof_action.sa_handler = SIG_IGN;\n  GTEST_DEATH_TEST_CHECK_SYSCALL_(sigaction(\n      SIGPROF, &ignore_sigprof_action, &saved_sigprof_action));\n#   endif  // GTEST_OS_LINUX\n\n#   if GTEST_HAS_CLONE\n  const bool use_fork = GTEST_FLAG(death_test_use_fork);\n\n  if (!use_fork) {\n    static const bool stack_grows_down = StackGrowsDown();\n    const size_t stack_size = getpagesize();\n    // MMAP_ANONYMOUS is not defined on Mac, so we use MAP_ANON instead.\n    void* const stack = mmap(NULL, stack_size, PROT_READ | PROT_WRITE,\n                             MAP_ANON | MAP_PRIVATE, -1, 0);\n    GTEST_DEATH_TEST_CHECK_(stack != MAP_FAILED);\n\n    // Maximum stack alignment in bytes:  For a downward-growing stack, this\n    // amount is subtracted from size of the stack space to get an address\n    // that is within the stack space and is aligned on all systems we care\n    // about.  As far as I know there is no ABI with stack alignment greater\n    // than 64.  We assume stack and stack_size already have alignment of\n    // kMaxStackAlignment.\n    const size_t kMaxStackAlignment = 64;\n    void* const stack_top =\n        static_cast<char*>(stack) +\n            (stack_grows_down ? stack_size - kMaxStackAlignment : 0);\n    GTEST_DEATH_TEST_CHECK_(stack_size > kMaxStackAlignment &&\n        reinterpret_cast<intptr_t>(stack_top) % kMaxStackAlignment == 0);\n\n    child_pid = clone(&ExecDeathTestChildMain, stack_top, SIGCHLD, &args);\n\n    GTEST_DEATH_TEST_CHECK_(munmap(stack, stack_size) != -1);\n  }\n#   else\n  const bool use_fork = true;\n#   endif  // GTEST_HAS_CLONE\n\n  if (use_fork && (child_pid = fork()) == 0) {\n      ExecDeathTestChildMain(&args);\n      _exit(0);\n  }\n#  endif  // GTEST_OS_QNX\n#  if GTEST_OS_LINUX\n  GTEST_DEATH_TEST_CHECK_SYSCALL_(\n      sigaction(SIGPROF, &saved_sigprof_action, NULL));\n#  endif  // GTEST_OS_LINUX\n\n  GTEST_DEATH_TEST_CHECK_(child_pid != -1);\n  return child_pid;\n}\n\n// The AssumeRole process for a fork-and-exec death test.  It re-executes the\n// main program from the beginning, setting the --gtest_filter\n// and --gtest_internal_run_death_test flags to cause only the current\n// death test to be re-run.\nDeathTest::TestRole ExecDeathTest::AssumeRole() {\n  const UnitTestImpl* const impl = GetUnitTestImpl();\n  const InternalRunDeathTestFlag* const flag =\n      impl->internal_run_death_test_flag();\n  const TestInfo* const info = impl->current_test_info();\n  const int death_test_index = info->result()->death_test_count();\n\n  if (flag != NULL) {\n    set_write_fd(flag->write_fd());\n    return EXECUTE_TEST;\n  }\n\n  int pipe_fd[2];\n  GTEST_DEATH_TEST_CHECK_(pipe(pipe_fd) != -1);\n  // Clear the close-on-exec flag on the write end of the pipe, lest\n  // it be closed when the child process does an exec:\n  GTEST_DEATH_TEST_CHECK_(fcntl(pipe_fd[1], F_SETFD, 0) != -1);\n\n  const std::string filter_flag =\n      std::string(\"--\") + GTEST_FLAG_PREFIX_ + kFilterFlag + \"=\"\n      + info->test_case_name() + \".\" + info->name();\n  const std::string internal_flag =\n      std::string(\"--\") + GTEST_FLAG_PREFIX_ + kInternalRunDeathTestFlag + \"=\"\n      + file_ + \"|\" + StreamableToString(line_) + \"|\"\n      + StreamableToString(death_test_index) + \"|\"\n      + StreamableToString(pipe_fd[1]);\n  Arguments args;\n  args.AddArguments(GetArgvsForDeathTestChildProcess());\n  args.AddArgument(filter_flag.c_str());\n  args.AddArgument(internal_flag.c_str());\n\n  DeathTest::set_last_death_test_message(\"\");\n\n  CaptureStderr();\n  // See the comment in NoExecDeathTest::AssumeRole for why the next line\n  // is necessary.\n  FlushInfoLog();\n\n  const pid_t child_pid = ExecDeathTestSpawnChild(args.Argv(), pipe_fd[0]);\n  GTEST_DEATH_TEST_CHECK_SYSCALL_(close(pipe_fd[1]));\n  set_child_pid(child_pid);\n  set_read_fd(pipe_fd[0]);\n  set_spawned(true);\n  return OVERSEE_TEST;\n}\n\n# endif  // !GTEST_OS_WINDOWS\n\n// Creates a concrete DeathTest-derived class that depends on the\n// --gtest_death_test_style flag, and sets the pointer pointed to\n// by the \"test\" argument to its address.  If the test should be\n// skipped, sets that pointer to NULL.  Returns true, unless the\n// flag is set to an invalid value.\nbool DefaultDeathTestFactory::Create(const char* statement, const RE* regex,\n                                     const char* file, int line,\n                                     DeathTest** test) {\n  UnitTestImpl* const impl = GetUnitTestImpl();\n  const InternalRunDeathTestFlag* const flag =\n      impl->internal_run_death_test_flag();\n  const int death_test_index = impl->current_test_info()\n      ->increment_death_test_count();\n\n  if (flag != NULL) {\n    if (death_test_index > flag->index()) {\n      DeathTest::set_last_death_test_message(\n          \"Death test count (\" + StreamableToString(death_test_index)\n          + \") somehow exceeded expected maximum (\"\n          + StreamableToString(flag->index()) + \")\");\n      return false;\n    }\n\n    if (!(flag->file() == file && flag->line() == line &&\n          flag->index() == death_test_index)) {\n      *test = NULL;\n      return true;\n    }\n  }\n\n# if GTEST_OS_WINDOWS\n\n  if (GTEST_FLAG(death_test_style) == \"threadsafe\" ||\n      GTEST_FLAG(death_test_style) == \"fast\") {\n    *test = new WindowsDeathTest(statement, regex, file, line);\n  }\n\n# else\n\n  if (GTEST_FLAG(death_test_style) == \"threadsafe\") {\n    *test = new ExecDeathTest(statement, regex, file, line);\n  } else if (GTEST_FLAG(death_test_style) == \"fast\") {\n    *test = new NoExecDeathTest(statement, regex);\n  }\n\n# endif  // GTEST_OS_WINDOWS\n\n  else {  // NOLINT - this is more readable than unbalanced brackets inside #if.\n    DeathTest::set_last_death_test_message(\n        \"Unknown death test style \\\"\" + GTEST_FLAG(death_test_style)\n        + \"\\\" encountered\");\n    return false;\n  }\n\n  return true;\n}\n\n# if GTEST_OS_WINDOWS\n// Recreates the pipe and event handles from the provided parameters,\n// signals the event, and returns a file descriptor wrapped around the pipe\n// handle. This function is called in the child process only.\nint GetStatusFileDescriptor(unsigned int parent_process_id,\n                            size_t write_handle_as_size_t,\n                            size_t event_handle_as_size_t) {\n  AutoHandle parent_process_handle(::OpenProcess(PROCESS_DUP_HANDLE,\n                                                   FALSE,  // Non-inheritable.\n                                                   parent_process_id));\n  if (parent_process_handle.Get() == INVALID_HANDLE_VALUE) {\n    DeathTestAbort(\"Unable to open parent process \" +\n                   StreamableToString(parent_process_id));\n  }\n\n  // TODO(vladl@google.com): Replace the following check with a\n  // compile-time assertion when available.\n  GTEST_CHECK_(sizeof(HANDLE) <= sizeof(size_t));\n\n  const HANDLE write_handle =\n      reinterpret_cast<HANDLE>(write_handle_as_size_t);\n  HANDLE dup_write_handle;\n\n  // The newly initialized handle is accessible only in in the parent\n  // process. To obtain one accessible within the child, we need to use\n  // DuplicateHandle.\n  if (!::DuplicateHandle(parent_process_handle.Get(), write_handle,\n                         ::GetCurrentProcess(), &dup_write_handle,\n                         0x0,    // Requested privileges ignored since\n                                 // DUPLICATE_SAME_ACCESS is used.\n                         FALSE,  // Request non-inheritable handler.\n                         DUPLICATE_SAME_ACCESS)) {\n    DeathTestAbort(\"Unable to duplicate the pipe handle \" +\n                   StreamableToString(write_handle_as_size_t) +\n                   \" from the parent process \" +\n                   StreamableToString(parent_process_id));\n  }\n\n  const HANDLE event_handle = reinterpret_cast<HANDLE>(event_handle_as_size_t);\n  HANDLE dup_event_handle;\n\n  if (!::DuplicateHandle(parent_process_handle.Get(), event_handle,\n                         ::GetCurrentProcess(), &dup_event_handle,\n                         0x0,\n                         FALSE,\n                         DUPLICATE_SAME_ACCESS)) {\n    DeathTestAbort(\"Unable to duplicate the event handle \" +\n                   StreamableToString(event_handle_as_size_t) +\n                   \" from the parent process \" +\n                   StreamableToString(parent_process_id));\n  }\n\n  const int write_fd =\n      ::_open_osfhandle(reinterpret_cast<intptr_t>(dup_write_handle), O_APPEND);\n  if (write_fd == -1) {\n    DeathTestAbort(\"Unable to convert pipe handle \" +\n                   StreamableToString(write_handle_as_size_t) +\n                   \" to a file descriptor\");\n  }\n\n  // Signals the parent that the write end of the pipe has been acquired\n  // so the parent can release its own write end.\n  ::SetEvent(dup_event_handle);\n\n  return write_fd;\n}\n# endif  // GTEST_OS_WINDOWS\n\n// Returns a newly created InternalRunDeathTestFlag object with fields\n// initialized from the GTEST_FLAG(internal_run_death_test) flag if\n// the flag is specified; otherwise returns NULL.\nInternalRunDeathTestFlag* ParseInternalRunDeathTestFlag() {\n  if (GTEST_FLAG(internal_run_death_test) == \"\") return NULL;\n\n  // GTEST_HAS_DEATH_TEST implies that we have ::std::string, so we\n  // can use it here.\n  int line = -1;\n  int index = -1;\n  ::std::vector< ::std::string> fields;\n  SplitString(GTEST_FLAG(internal_run_death_test).c_str(), '|', &fields);\n  int write_fd = -1;\n\n# if GTEST_OS_WINDOWS\n\n  unsigned int parent_process_id = 0;\n  size_t write_handle_as_size_t = 0;\n  size_t event_handle_as_size_t = 0;\n\n  if (fields.size() != 6\n      || !ParseNaturalNumber(fields[1], &line)\n      || !ParseNaturalNumber(fields[2], &index)\n      || !ParseNaturalNumber(fields[3], &parent_process_id)\n      || !ParseNaturalNumber(fields[4], &write_handle_as_size_t)\n      || !ParseNaturalNumber(fields[5], &event_handle_as_size_t)) {\n    DeathTestAbort(\"Bad --gtest_internal_run_death_test flag: \" +\n                   GTEST_FLAG(internal_run_death_test));\n  }\n  write_fd = GetStatusFileDescriptor(parent_process_id,\n                                     write_handle_as_size_t,\n                                     event_handle_as_size_t);\n# else\n\n  if (fields.size() != 4\n      || !ParseNaturalNumber(fields[1], &line)\n      || !ParseNaturalNumber(fields[2], &index)\n      || !ParseNaturalNumber(fields[3], &write_fd)) {\n    DeathTestAbort(\"Bad --gtest_internal_run_death_test flag: \"\n        + GTEST_FLAG(internal_run_death_test));\n  }\n\n# endif  // GTEST_OS_WINDOWS\n\n  return new InternalRunDeathTestFlag(fields[0], line, index, write_fd);\n}\n\n}  // namespace internal\n\n#endif  // GTEST_HAS_DEATH_TEST\n\n}  // namespace testing\n// Copyright 2008, Google Inc.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Authors: keith.ray@gmail.com (Keith Ray)\n\n\n#include <stdlib.h>\n\n#if GTEST_OS_WINDOWS_MOBILE\n# include <windows.h>\n#elif GTEST_OS_WINDOWS\n# include <direct.h>\n# include <io.h>\n#elif GTEST_OS_SYMBIAN\n// Symbian OpenC has PATH_MAX in sys/syslimits.h\n# include <sys/syslimits.h>\n#else\n# include <limits.h>\n# include <climits>  // Some Linux distributions define PATH_MAX here.\n#endif  // GTEST_OS_WINDOWS_MOBILE\n\n#if GTEST_OS_WINDOWS\n# define GTEST_PATH_MAX_ _MAX_PATH\n#elif defined(PATH_MAX)\n# define GTEST_PATH_MAX_ PATH_MAX\n#elif defined(_XOPEN_PATH_MAX)\n# define GTEST_PATH_MAX_ _XOPEN_PATH_MAX\n#else\n# define GTEST_PATH_MAX_ _POSIX_PATH_MAX\n#endif  // GTEST_OS_WINDOWS\n\n\nnamespace testing {\nnamespace internal {\n\n#if GTEST_OS_WINDOWS\n// On Windows, '\\\\' is the standard path separator, but many tools and the\n// Windows API also accept '/' as an alternate path separator. Unless otherwise\n// noted, a file path can contain either kind of path separators, or a mixture\n// of them.\nconst char kPathSeparator = '\\\\';\nconst char kAlternatePathSeparator = '/';\nconst char kAlternatePathSeparatorString[] = \"/\";\n# if GTEST_OS_WINDOWS_MOBILE\n// Windows CE doesn't have a current directory. You should not use\n// the current directory in tests on Windows CE, but this at least\n// provides a reasonable fallback.\nconst char kCurrentDirectoryString[] = \"\\\\\";\n// Windows CE doesn't define INVALID_FILE_ATTRIBUTES\nconst DWORD kInvalidFileAttributes = 0xffffffff;\n# else\nconst char kCurrentDirectoryString[] = \".\\\\\";\n# endif  // GTEST_OS_WINDOWS_MOBILE\n#else\nconst char kPathSeparator = '/';\nconst char kCurrentDirectoryString[] = \"./\";\n#endif  // GTEST_OS_WINDOWS\n\n// Returns whether the given character is a valid path separator.\nstatic bool IsPathSeparator(char c) {\n#if GTEST_HAS_ALT_PATH_SEP_\n  return (c == kPathSeparator) || (c == kAlternatePathSeparator);\n#else\n  return c == kPathSeparator;\n#endif\n}\n\n// Returns the current working directory, or \"\" if unsuccessful.\nFilePath FilePath::GetCurrentDir() {\n#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE || GTEST_OS_WINDOWS_RT\n  // Windows CE doesn't have a current directory, so we just return\n  // something reasonable.\n  return FilePath(kCurrentDirectoryString);\n#elif GTEST_OS_WINDOWS\n  char cwd[GTEST_PATH_MAX_ + 1] = { '\\0' };\n  return FilePath(_getcwd(cwd, sizeof(cwd)) == NULL ? \"\" : cwd);\n#else\n  char cwd[GTEST_PATH_MAX_ + 1] = { '\\0' };\n  char* result = getcwd(cwd, sizeof(cwd));\n# if GTEST_OS_NACL\n  // getcwd will likely fail in NaCl due to the sandbox, so return something\n  // reasonable. The user may have provided a shim implementation for getcwd,\n  // however, so fallback only when failure is detected.\n  return FilePath(result == NULL ? kCurrentDirectoryString : cwd);\n# endif  // GTEST_OS_NACL\n  return FilePath(result == NULL ? \"\" : cwd);\n#endif  // GTEST_OS_WINDOWS_MOBILE\n}\n\n// Returns a copy of the FilePath with the case-insensitive extension removed.\n// Example: FilePath(\"dir/file.exe\").RemoveExtension(\"EXE\") returns\n// FilePath(\"dir/file\"). If a case-insensitive extension is not\n// found, returns a copy of the original FilePath.\nFilePath FilePath::RemoveExtension(const char* extension) const {\n  const std::string dot_extension = std::string(\".\") + extension;\n  if (String::EndsWithCaseInsensitive(pathname_, dot_extension)) {\n    return FilePath(pathname_.substr(\n        0, pathname_.length() - dot_extension.length()));\n  }\n  return *this;\n}\n\n// Returns a pointer to the last occurence of a valid path separator in\n// the FilePath. On Windows, for example, both '/' and '\\' are valid path\n// separators. Returns NULL if no path separator was found.\nconst char* FilePath::FindLastPathSeparator() const {\n  const char* const last_sep = strrchr(c_str(), kPathSeparator);\n#if GTEST_HAS_ALT_PATH_SEP_\n  const char* const last_alt_sep = strrchr(c_str(), kAlternatePathSeparator);\n  // Comparing two pointers of which only one is NULL is undefined.\n  if (last_alt_sep != NULL &&\n      (last_sep == NULL || last_alt_sep > last_sep)) {\n    return last_alt_sep;\n  }\n#endif\n  return last_sep;\n}\n\n// Returns a copy of the FilePath with the directory part removed.\n// Example: FilePath(\"path/to/file\").RemoveDirectoryName() returns\n// FilePath(\"file\"). If there is no directory part (\"just_a_file\"), it returns\n// the FilePath unmodified. If there is no file part (\"just_a_dir/\") it\n// returns an empty FilePath (\"\").\n// On Windows platform, '\\' is the path separator, otherwise it is '/'.\nFilePath FilePath::RemoveDirectoryName() const {\n  const char* const last_sep = FindLastPathSeparator();\n  return last_sep ? FilePath(last_sep + 1) : *this;\n}\n\n// RemoveFileName returns the directory path with the filename removed.\n// Example: FilePath(\"path/to/file\").RemoveFileName() returns \"path/to/\".\n// If the FilePath is \"a_file\" or \"/a_file\", RemoveFileName returns\n// FilePath(\"./\") or, on Windows, FilePath(\".\\\\\"). If the filepath does\n// not have a file, like \"just/a/dir/\", it returns the FilePath unmodified.\n// On Windows platform, '\\' is the path separator, otherwise it is '/'.\nFilePath FilePath::RemoveFileName() const {\n  const char* const last_sep = FindLastPathSeparator();\n  std::string dir;\n  if (last_sep) {\n    dir = std::string(c_str(), last_sep + 1 - c_str());\n  } else {\n    dir = kCurrentDirectoryString;\n  }\n  return FilePath(dir);\n}\n\n// Helper functions for naming files in a directory for xml output.\n\n// Given directory = \"dir\", base_name = \"test\", number = 0,\n// extension = \"xml\", returns \"dir/test.xml\". If number is greater\n// than zero (e.g., 12), returns \"dir/test_12.xml\".\n// On Windows platform, uses \\ as the separator rather than /.\nFilePath FilePath::MakeFileName(const FilePath& directory,\n                                const FilePath& base_name,\n                                int number,\n                                const char* extension) {\n  std::string file;\n  if (number == 0) {\n    file = base_name.string() + \".\" + extension;\n  } else {\n    file = base_name.string() + \"_\" + StreamableToString(number)\n        + \".\" + extension;\n  }\n  return ConcatPaths(directory, FilePath(file));\n}\n\n// Given directory = \"dir\", relative_path = \"test.xml\", returns \"dir/test.xml\".\n// On Windows, uses \\ as the separator rather than /.\nFilePath FilePath::ConcatPaths(const FilePath& directory,\n                               const FilePath& relative_path) {\n  if (directory.IsEmpty())\n    return relative_path;\n  const FilePath dir(directory.RemoveTrailingPathSeparator());\n  return FilePath(dir.string() + kPathSeparator + relative_path.string());\n}\n\n// Returns true if pathname describes something findable in the file-system,\n// either a file, directory, or whatever.\nbool FilePath::FileOrDirectoryExists() const {\n#if GTEST_OS_WINDOWS_MOBILE\n  LPCWSTR unicode = String::AnsiToUtf16(pathname_.c_str());\n  const DWORD attributes = GetFileAttributes(unicode);\n  delete [] unicode;\n  return attributes != kInvalidFileAttributes;\n#else\n  posix::StatStruct file_stat;\n  return posix::Stat(pathname_.c_str(), &file_stat) == 0;\n#endif  // GTEST_OS_WINDOWS_MOBILE\n}\n\n// Returns true if pathname describes a directory in the file-system\n// that exists.\nbool FilePath::DirectoryExists() const {\n  bool result = false;\n#if GTEST_OS_WINDOWS\n  // Don't strip off trailing separator if path is a root directory on\n  // Windows (like \"C:\\\\\").\n  const FilePath& path(IsRootDirectory() ? *this :\n                                           RemoveTrailingPathSeparator());\n#else\n  const FilePath& path(*this);\n#endif\n\n#if GTEST_OS_WINDOWS_MOBILE\n  LPCWSTR unicode = String::AnsiToUtf16(path.c_str());\n  const DWORD attributes = GetFileAttributes(unicode);\n  delete [] unicode;\n  if ((attributes != kInvalidFileAttributes) &&\n      (attributes & FILE_ATTRIBUTE_DIRECTORY)) {\n    result = true;\n  }\n#else\n  posix::StatStruct file_stat;\n  result = posix::Stat(path.c_str(), &file_stat) == 0 &&\n      posix::IsDir(file_stat);\n#endif  // GTEST_OS_WINDOWS_MOBILE\n\n  return result;\n}\n\n// Returns true if pathname describes a root directory. (Windows has one\n// root directory per disk drive.)\nbool FilePath::IsRootDirectory() const {\n#if GTEST_OS_WINDOWS\n  // TODO(wan@google.com): on Windows a network share like\n  // \\\\server\\share can be a root directory, although it cannot be the\n  // current directory.  Handle this properly.\n  return pathname_.length() == 3 && IsAbsolutePath();\n#else\n  return pathname_.length() == 1 && IsPathSeparator(pathname_.c_str()[0]);\n#endif\n}\n\n// Returns true if pathname describes an absolute path.\nbool FilePath::IsAbsolutePath() const {\n  const char* const name = pathname_.c_str();\n#if GTEST_OS_WINDOWS\n  return pathname_.length() >= 3 &&\n     ((name[0] >= 'a' && name[0] <= 'z') ||\n      (name[0] >= 'A' && name[0] <= 'Z')) &&\n     name[1] == ':' &&\n     IsPathSeparator(name[2]);\n#else\n  return IsPathSeparator(name[0]);\n#endif\n}\n\n// Returns a pathname for a file that does not currently exist. The pathname\n// will be directory/base_name.extension or\n// directory/base_name_<number>.extension if directory/base_name.extension\n// already exists. The number will be incremented until a pathname is found\n// that does not already exist.\n// Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'.\n// There could be a race condition if two or more processes are calling this\n// function at the same time -- they could both pick the same filename.\nFilePath FilePath::GenerateUniqueFileName(const FilePath& directory,\n                                          const FilePath& base_name,\n                                          const char* extension) {\n  FilePath full_pathname;\n  int number = 0;\n  do {\n    full_pathname.Set(MakeFileName(directory, base_name, number++, extension));\n  } while (full_pathname.FileOrDirectoryExists());\n  return full_pathname;\n}\n\n// Returns true if FilePath ends with a path separator, which indicates that\n// it is intended to represent a directory. Returns false otherwise.\n// This does NOT check that a directory (or file) actually exists.\nbool FilePath::IsDirectory() const {\n  return !pathname_.empty() &&\n         IsPathSeparator(pathname_.c_str()[pathname_.length() - 1]);\n}\n\n// Create directories so that path exists. Returns true if successful or if\n// the directories already exist; returns false if unable to create directories\n// for any reason.\nbool FilePath::CreateDirectoriesRecursively() const {\n  if (!this->IsDirectory()) {\n    return false;\n  }\n\n  if (pathname_.length() == 0 || this->DirectoryExists()) {\n    return true;\n  }\n\n  const FilePath parent(this->RemoveTrailingPathSeparator().RemoveFileName());\n  return parent.CreateDirectoriesRecursively() && this->CreateFolder();\n}\n\n// Create the directory so that path exists. Returns true if successful or\n// if the directory already exists; returns false if unable to create the\n// directory for any reason, including if the parent directory does not\n// exist. Not named \"CreateDirectory\" because that's a macro on Windows.\nbool FilePath::CreateFolder() const {\n#if GTEST_OS_WINDOWS_MOBILE\n  FilePath removed_sep(this->RemoveTrailingPathSeparator());\n  LPCWSTR unicode = String::AnsiToUtf16(removed_sep.c_str());\n  int result = CreateDirectory(unicode, NULL) ? 0 : -1;\n  delete [] unicode;\n#elif GTEST_OS_WINDOWS\n  int result = _mkdir(pathname_.c_str());\n#else\n  int result = mkdir(pathname_.c_str(), 0777);\n#endif  // GTEST_OS_WINDOWS_MOBILE\n\n  if (result == -1) {\n    return this->DirectoryExists();  // An error is OK if the directory exists.\n  }\n  return true;  // No error.\n}\n\n// If input name has a trailing separator character, remove it and return the\n// name, otherwise return the name string unmodified.\n// On Windows platform, uses \\ as the separator, other platforms use /.\nFilePath FilePath::RemoveTrailingPathSeparator() const {\n  return IsDirectory()\n      ? FilePath(pathname_.substr(0, pathname_.length() - 1))\n      : *this;\n}\n\n// Removes any redundant separators that might be in the pathname.\n// For example, \"bar///foo\" becomes \"bar/foo\". Does not eliminate other\n// redundancies that might be in a pathname involving \".\" or \"..\".\n// TODO(wan@google.com): handle Windows network shares (e.g. \\\\server\\share).\nvoid FilePath::Normalize() {\n  if (pathname_.c_str() == NULL) {\n    pathname_ = \"\";\n    return;\n  }\n  const char* src = pathname_.c_str();\n  char* const dest = new char[pathname_.length() + 1];\n  char* dest_ptr = dest;\n  memset(dest_ptr, 0, pathname_.length() + 1);\n\n  while (*src != '\\0') {\n    *dest_ptr = *src;\n    if (!IsPathSeparator(*src)) {\n      src++;\n    } else {\n#if GTEST_HAS_ALT_PATH_SEP_\n      if (*dest_ptr == kAlternatePathSeparator) {\n        *dest_ptr = kPathSeparator;\n      }\n#endif\n      while (IsPathSeparator(*src))\n        src++;\n    }\n    dest_ptr++;\n  }\n  *dest_ptr = '\\0';\n  pathname_ = dest;\n  delete[] dest;\n}\n\n}  // namespace internal\n}  // namespace testing\n// Copyright 2008, Google Inc.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Author: wan@google.com (Zhanyong Wan)\n\n\n#include <limits.h>\n#include <stdlib.h>\n#include <stdio.h>\n#include <string.h>\n#include <fstream>\n\n#if GTEST_OS_WINDOWS\n# include <windows.h>\n# include <io.h>\n# include <sys/stat.h>\n# include <map>  // Used in ThreadLocal.\n#else\n# include <unistd.h>\n#endif  // GTEST_OS_WINDOWS\n\n#if GTEST_OS_MAC\n# include <mach/mach_init.h>\n# include <mach/task.h>\n# include <mach/vm_map.h>\n#endif  // GTEST_OS_MAC\n\n#if GTEST_OS_QNX\n# include <devctl.h>\n# include <fcntl.h>\n# include <sys/procfs.h>\n#endif  // GTEST_OS_QNX\n\n#if GTEST_OS_AIX\n# include <procinfo.h>\n# include <sys/types.h>\n#endif  // GTEST_OS_AIX\n\n\n// Indicates that this translation unit is part of Google Test's\n// implementation.  It must come before gtest-internal-inl.h is\n// included, or there will be a compiler error.  This trick exists to\n// prevent the accidental inclusion of gtest-internal-inl.h in the\n// user's code.\n#define GTEST_IMPLEMENTATION_ 1\n#undef GTEST_IMPLEMENTATION_\n\nnamespace testing {\nnamespace internal {\n\n#if defined(_MSC_VER) || defined(__BORLANDC__)\n// MSVC and C++Builder do not provide a definition of STDERR_FILENO.\nconst int kStdOutFileno = 1;\nconst int kStdErrFileno = 2;\n#else\nconst int kStdOutFileno = STDOUT_FILENO;\nconst int kStdErrFileno = STDERR_FILENO;\n#endif  // _MSC_VER\n\n#if GTEST_OS_LINUX\n\nnamespace {\ntemplate <typename T>\nT ReadProcFileField(const std::string& filename, int field) {\n  std::string dummy;\n  std::ifstream file(filename.c_str());\n  while (field-- > 0) {\n    file >> dummy;\n  }\n  T output = 0;\n  file >> output;\n  return output;\n}\n}  // namespace\n\n// Returns the number of active threads, or 0 when there is an error.\nsize_t GetThreadCount() {\n  const std::string filename =\n      (Message() << \"/proc/\" << getpid() << \"/stat\").GetString();\n  return ReadProcFileField<int>(filename, 19);\n}\n\n#elif GTEST_OS_MAC\n\nsize_t GetThreadCount() {\n  const task_t task = mach_task_self();\n  mach_msg_type_number_t thread_count;\n  thread_act_array_t thread_list;\n  const kern_return_t status = task_threads(task, &thread_list, &thread_count);\n  if (status == KERN_SUCCESS) {\n    // task_threads allocates resources in thread_list and we need to free them\n    // to avoid leaks.\n    vm_deallocate(task,\n                  reinterpret_cast<vm_address_t>(thread_list),\n                  sizeof(thread_t) * thread_count);\n    return static_cast<size_t>(thread_count);\n  } else {\n    return 0;\n  }\n}\n\n#elif GTEST_OS_QNX\n\n// Returns the number of threads running in the process, or 0 to indicate that\n// we cannot detect it.\nsize_t GetThreadCount() {\n  const int fd = open(\"/proc/self/as\", O_RDONLY);\n  if (fd < 0) {\n    return 0;\n  }\n  procfs_info process_info;\n  const int status =\n      devctl(fd, DCMD_PROC_INFO, &process_info, sizeof(process_info), NULL);\n  close(fd);\n  if (status == EOK) {\n    return static_cast<size_t>(process_info.num_threads);\n  } else {\n    return 0;\n  }\n}\n\n#elif GTEST_OS_AIX\n\nsize_t GetThreadCount() {\n  struct procentry64 entry;\n  pid_t pid = getpid();\n  int status = getprocs64(&entry, sizeof(entry), NULL, 0, &pid, 1);\n  if (status == 1) {\n    return entry.pi_thcount;\n  } else {\n    return 0;\n  }\n}\n\n#else\n\nsize_t GetThreadCount() {\n  // There's no portable way to detect the number of threads, so we just\n  // return 0 to indicate that we cannot detect it.\n  return 0;\n}\n\n#endif  // GTEST_OS_LINUX\n\n#if GTEST_IS_THREADSAFE && GTEST_OS_WINDOWS\n\nvoid SleepMilliseconds(int n) {\n  ::Sleep(n);\n}\n\nAutoHandle::AutoHandle()\n    : handle_(INVALID_HANDLE_VALUE) {}\n\nAutoHandle::AutoHandle(Handle handle)\n    : handle_(handle) {}\n\nAutoHandle::~AutoHandle() {\n  Reset();\n}\n\nAutoHandle::Handle AutoHandle::Get() const {\n  return handle_;\n}\n\nvoid AutoHandle::Reset() {\n  Reset(INVALID_HANDLE_VALUE);\n}\n\nvoid AutoHandle::Reset(HANDLE handle) {\n  // Resetting with the same handle we already own is invalid.\n  if (handle_ != handle) {\n    if (IsCloseable()) {\n      ::CloseHandle(handle_);\n    }\n    handle_ = handle;\n  } else {\n    GTEST_CHECK_(!IsCloseable())\n        << \"Resetting a valid handle to itself is likely a programmer error \"\n            \"and thus not allowed.\";\n  }\n}\n\nbool AutoHandle::IsCloseable() const {\n  // Different Windows APIs may use either of these values to represent an\n  // invalid handle.\n  return handle_ != NULL && handle_ != INVALID_HANDLE_VALUE;\n}\n\nNotification::Notification()\n    : event_(::CreateEvent(NULL,   // Default security attributes.\n                           TRUE,   // Do not reset automatically.\n                           FALSE,  // Initially unset.\n                           NULL)) {  // Anonymous event.\n  GTEST_CHECK_(event_.Get() != NULL);\n}\n\nvoid Notification::Notify() {\n  GTEST_CHECK_(::SetEvent(event_.Get()) != FALSE);\n}\n\nvoid Notification::WaitForNotification() {\n  GTEST_CHECK_(\n      ::WaitForSingleObject(event_.Get(), INFINITE) == WAIT_OBJECT_0);\n}\n\nMutex::Mutex()\n    : owner_thread_id_(0),\n      type_(kDynamic),\n      critical_section_init_phase_(0),\n      critical_section_(new CRITICAL_SECTION) {\n  ::InitializeCriticalSection(critical_section_);\n}\n\nMutex::~Mutex() {\n  // Static mutexes are leaked intentionally. It is not thread-safe to try\n  // to clean them up.\n  // TODO(yukawa): Switch to Slim Reader/Writer (SRW) Locks, which requires\n  // nothing to clean it up but is available only on Vista and later.\n  // http://msdn.microsoft.com/en-us/library/windows/desktop/aa904937.aspx\n  if (type_ == kDynamic) {\n    ::DeleteCriticalSection(critical_section_);\n    delete critical_section_;\n    critical_section_ = NULL;\n  }\n}\n\nvoid Mutex::Lock() {\n  ThreadSafeLazyInit();\n  ::EnterCriticalSection(critical_section_);\n  owner_thread_id_ = ::GetCurrentThreadId();\n}\n\nvoid Mutex::Unlock() {\n  ThreadSafeLazyInit();\n  // We don't protect writing to owner_thread_id_ here, as it's the\n  // caller's responsibility to ensure that the current thread holds the\n  // mutex when this is called.\n  owner_thread_id_ = 0;\n  ::LeaveCriticalSection(critical_section_);\n}\n\n// Does nothing if the current thread holds the mutex. Otherwise, crashes\n// with high probability.\nvoid Mutex::AssertHeld() {\n  ThreadSafeLazyInit();\n  GTEST_CHECK_(owner_thread_id_ == ::GetCurrentThreadId())\n      << \"The current thread is not holding the mutex @\" << this;\n}\n\n// Initializes owner_thread_id_ and critical_section_ in static mutexes.\nvoid Mutex::ThreadSafeLazyInit() {\n  // Dynamic mutexes are initialized in the constructor.\n  if (type_ == kStatic) {\n    switch (\n        ::InterlockedCompareExchange(&critical_section_init_phase_, 1L, 0L)) {\n      case 0:\n        // If critical_section_init_phase_ was 0 before the exchange, we\n        // are the first to test it and need to perform the initialization.\n        owner_thread_id_ = 0;\n        critical_section_ = new CRITICAL_SECTION;\n        ::InitializeCriticalSection(critical_section_);\n        // Updates the critical_section_init_phase_ to 2 to signal\n        // initialization complete.\n        GTEST_CHECK_(::InterlockedCompareExchange(\n                          &critical_section_init_phase_, 2L, 1L) ==\n                      1L);\n        break;\n      case 1:\n        // Somebody else is already initializing the mutex; spin until they\n        // are done.\n        while (::InterlockedCompareExchange(&critical_section_init_phase_,\n                                            2L,\n                                            2L) != 2L) {\n          // Possibly yields the rest of the thread's time slice to other\n          // threads.\n          ::Sleep(0);\n        }\n        break;\n\n      case 2:\n        break;  // The mutex is already initialized and ready for use.\n\n      default:\n        GTEST_CHECK_(false)\n            << \"Unexpected value of critical_section_init_phase_ \"\n            << \"while initializing a static mutex.\";\n    }\n  }\n}\n\nnamespace {\n\nclass ThreadWithParamSupport : public ThreadWithParamBase {\n public:\n  static HANDLE CreateThread(Runnable* runnable,\n                             Notification* thread_can_start) {\n    ThreadMainParam* param = new ThreadMainParam(runnable, thread_can_start);\n    DWORD thread_id;\n    // TODO(yukawa): Consider to use _beginthreadex instead.\n    HANDLE thread_handle = ::CreateThread(\n        NULL,    // Default security.\n        0,       // Default stack size.\n        &ThreadWithParamSupport::ThreadMain,\n        param,   // Parameter to ThreadMainStatic\n        0x0,     // Default creation flags.\n        &thread_id);  // Need a valid pointer for the call to work under Win98.\n    GTEST_CHECK_(thread_handle != NULL) << \"CreateThread failed with error \"\n                                        << ::GetLastError() << \".\";\n    if (thread_handle == NULL) {\n      delete param;\n    }\n    return thread_handle;\n  }\n\n private:\n  struct ThreadMainParam {\n    ThreadMainParam(Runnable* runnable, Notification* thread_can_start)\n        : runnable_(runnable),\n          thread_can_start_(thread_can_start) {\n    }\n    scoped_ptr<Runnable> runnable_;\n    // Does not own.\n    Notification* thread_can_start_;\n  };\n\n  static DWORD WINAPI ThreadMain(void* ptr) {\n    // Transfers ownership.\n    scoped_ptr<ThreadMainParam> param(static_cast<ThreadMainParam*>(ptr));\n    if (param->thread_can_start_ != NULL)\n      param->thread_can_start_->WaitForNotification();\n    param->runnable_->Run();\n    return 0;\n  }\n\n  // Prohibit instantiation.\n  ThreadWithParamSupport();\n\n  GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParamSupport);\n};\n\n}  // namespace\n\nThreadWithParamBase::ThreadWithParamBase(Runnable *runnable,\n                                         Notification* thread_can_start)\n      : thread_(ThreadWithParamSupport::CreateThread(runnable,\n                                                     thread_can_start)) {\n}\n\nThreadWithParamBase::~ThreadWithParamBase() {\n  Join();\n}\n\nvoid ThreadWithParamBase::Join() {\n  GTEST_CHECK_(::WaitForSingleObject(thread_.Get(), INFINITE) == WAIT_OBJECT_0)\n      << \"Failed to join the thread with error \" << ::GetLastError() << \".\";\n}\n\n// Maps a thread to a set of ThreadIdToThreadLocals that have values\n// instantiated on that thread and notifies them when the thread exits.  A\n// ThreadLocal instance is expected to persist until all threads it has\n// values on have terminated.\nclass ThreadLocalRegistryImpl {\n public:\n  // Registers thread_local_instance as having value on the current thread.\n  // Returns a value that can be used to identify the thread from other threads.\n  static ThreadLocalValueHolderBase* GetValueOnCurrentThread(\n      const ThreadLocalBase* thread_local_instance) {\n    DWORD current_thread = ::GetCurrentThreadId();\n    MutexLock lock(&mutex_);\n    ThreadIdToThreadLocals* const thread_to_thread_locals =\n        GetThreadLocalsMapLocked();\n    ThreadIdToThreadLocals::iterator thread_local_pos =\n        thread_to_thread_locals->find(current_thread);\n    if (thread_local_pos == thread_to_thread_locals->end()) {\n      thread_local_pos = thread_to_thread_locals->insert(\n          std::make_pair(current_thread, ThreadLocalValues())).first;\n      StartWatcherThreadFor(current_thread);\n    }\n    ThreadLocalValues& thread_local_values = thread_local_pos->second;\n    ThreadLocalValues::iterator value_pos =\n        thread_local_values.find(thread_local_instance);\n    if (value_pos == thread_local_values.end()) {\n      value_pos =\n          thread_local_values\n              .insert(std::make_pair(\n                  thread_local_instance,\n                  linked_ptr<ThreadLocalValueHolderBase>(\n                      thread_local_instance->NewValueForCurrentThread())))\n              .first;\n    }\n    return value_pos->second.get();\n  }\n\n  static void OnThreadLocalDestroyed(\n      const ThreadLocalBase* thread_local_instance) {\n    std::vector<linked_ptr<ThreadLocalValueHolderBase> > value_holders;\n    // Clean up the ThreadLocalValues data structure while holding the lock, but\n    // defer the destruction of the ThreadLocalValueHolderBases.\n    {\n      MutexLock lock(&mutex_);\n      ThreadIdToThreadLocals* const thread_to_thread_locals =\n          GetThreadLocalsMapLocked();\n      for (ThreadIdToThreadLocals::iterator it =\n          thread_to_thread_locals->begin();\n          it != thread_to_thread_locals->end();\n          ++it) {\n        ThreadLocalValues& thread_local_values = it->second;\n        ThreadLocalValues::iterator value_pos =\n            thread_local_values.find(thread_local_instance);\n        if (value_pos != thread_local_values.end()) {\n          value_holders.push_back(value_pos->second);\n          thread_local_values.erase(value_pos);\n          // This 'if' can only be successful at most once, so theoretically we\n          // could break out of the loop here, but we don't bother doing so.\n        }\n      }\n    }\n    // Outside the lock, let the destructor for 'value_holders' deallocate the\n    // ThreadLocalValueHolderBases.\n  }\n\n  static void OnThreadExit(DWORD thread_id) {\n    GTEST_CHECK_(thread_id != 0) << ::GetLastError();\n    std::vector<linked_ptr<ThreadLocalValueHolderBase> > value_holders;\n    // Clean up the ThreadIdToThreadLocals data structure while holding the\n    // lock, but defer the destruction of the ThreadLocalValueHolderBases.\n    {\n      MutexLock lock(&mutex_);\n      ThreadIdToThreadLocals* const thread_to_thread_locals =\n          GetThreadLocalsMapLocked();\n      ThreadIdToThreadLocals::iterator thread_local_pos =\n          thread_to_thread_locals->find(thread_id);\n      if (thread_local_pos != thread_to_thread_locals->end()) {\n        ThreadLocalValues& thread_local_values = thread_local_pos->second;\n        for (ThreadLocalValues::iterator value_pos =\n            thread_local_values.begin();\n            value_pos != thread_local_values.end();\n            ++value_pos) {\n          value_holders.push_back(value_pos->second);\n        }\n        thread_to_thread_locals->erase(thread_local_pos);\n      }\n    }\n    // Outside the lock, let the destructor for 'value_holders' deallocate the\n    // ThreadLocalValueHolderBases.\n  }\n\n private:\n  // In a particular thread, maps a ThreadLocal object to its value.\n  typedef std::map<const ThreadLocalBase*,\n                   linked_ptr<ThreadLocalValueHolderBase> > ThreadLocalValues;\n  // Stores all ThreadIdToThreadLocals having values in a thread, indexed by\n  // thread's ID.\n  typedef std::map<DWORD, ThreadLocalValues> ThreadIdToThreadLocals;\n\n  // Holds the thread id and thread handle that we pass from\n  // StartWatcherThreadFor to WatcherThreadFunc.\n  typedef std::pair<DWORD, HANDLE> ThreadIdAndHandle;\n\n  static void StartWatcherThreadFor(DWORD thread_id) {\n    // The returned handle will be kept in thread_map and closed by\n    // watcher_thread in WatcherThreadFunc.\n    HANDLE thread = ::OpenThread(SYNCHRONIZE | THREAD_QUERY_INFORMATION,\n                                 FALSE,\n                                 thread_id);\n    GTEST_CHECK_(thread != NULL);\n    // We need to to pass a valid thread ID pointer into CreateThread for it\n    // to work correctly under Win98.\n    DWORD watcher_thread_id;\n    HANDLE watcher_thread = ::CreateThread(\n        NULL,   // Default security.\n        0,      // Default stack size\n        &ThreadLocalRegistryImpl::WatcherThreadFunc,\n        reinterpret_cast<LPVOID>(new ThreadIdAndHandle(thread_id, thread)),\n        CREATE_SUSPENDED,\n        &watcher_thread_id);\n    GTEST_CHECK_(watcher_thread != NULL);\n    // Give the watcher thread the same priority as ours to avoid being\n    // blocked by it.\n    ::SetThreadPriority(watcher_thread,\n                        ::GetThreadPriority(::GetCurrentThread()));\n    ::ResumeThread(watcher_thread);\n    ::CloseHandle(watcher_thread);\n  }\n\n  // Monitors exit from a given thread and notifies those\n  // ThreadIdToThreadLocals about thread termination.\n  static DWORD WINAPI WatcherThreadFunc(LPVOID param) {\n    const ThreadIdAndHandle* tah =\n        reinterpret_cast<const ThreadIdAndHandle*>(param);\n    GTEST_CHECK_(\n        ::WaitForSingleObject(tah->second, INFINITE) == WAIT_OBJECT_0);\n    OnThreadExit(tah->first);\n    ::CloseHandle(tah->second);\n    delete tah;\n    return 0;\n  }\n\n  // Returns map of thread local instances.\n  static ThreadIdToThreadLocals* GetThreadLocalsMapLocked() {\n    mutex_.AssertHeld();\n    static ThreadIdToThreadLocals* map = new ThreadIdToThreadLocals;\n    return map;\n  }\n\n  // Protects access to GetThreadLocalsMapLocked() and its return value.\n  static Mutex mutex_;\n  // Protects access to GetThreadMapLocked() and its return value.\n  static Mutex thread_map_mutex_;\n};\n\nMutex ThreadLocalRegistryImpl::mutex_(Mutex::kStaticMutex);\nMutex ThreadLocalRegistryImpl::thread_map_mutex_(Mutex::kStaticMutex);\n\nThreadLocalValueHolderBase* ThreadLocalRegistry::GetValueOnCurrentThread(\n      const ThreadLocalBase* thread_local_instance) {\n  return ThreadLocalRegistryImpl::GetValueOnCurrentThread(\n      thread_local_instance);\n}\n\nvoid ThreadLocalRegistry::OnThreadLocalDestroyed(\n      const ThreadLocalBase* thread_local_instance) {\n  ThreadLocalRegistryImpl::OnThreadLocalDestroyed(thread_local_instance);\n}\n\n#endif  // GTEST_IS_THREADSAFE && GTEST_OS_WINDOWS\n\n#if GTEST_USES_POSIX_RE\n\n// Implements RE.  Currently only needed for death tests.\n\nRE::~RE() {\n  if (is_valid_) {\n    // regfree'ing an invalid regex might crash because the content\n    // of the regex is undefined. Since the regex's are essentially\n    // the same, one cannot be valid (or invalid) without the other\n    // being so too.\n    regfree(&partial_regex_);\n    regfree(&full_regex_);\n  }\n  free(const_cast<char*>(pattern_));\n}\n\n// Returns true iff regular expression re matches the entire str.\nbool RE::FullMatch(const char* str, const RE& re) {\n  if (!re.is_valid_) return false;\n\n  regmatch_t match;\n  return regexec(&re.full_regex_, str, 1, &match, 0) == 0;\n}\n\n// Returns true iff regular expression re matches a substring of str\n// (including str itself).\nbool RE::PartialMatch(const char* str, const RE& re) {\n  if (!re.is_valid_) return false;\n\n  regmatch_t match;\n  return regexec(&re.partial_regex_, str, 1, &match, 0) == 0;\n}\n\n// Initializes an RE from its string representation.\nvoid RE::Init(const char* regex) {\n  pattern_ = posix::StrDup(regex);\n\n  // Reserves enough bytes to hold the regular expression used for a\n  // full match.\n  const size_t full_regex_len = strlen(regex) + 10;\n  char* const full_pattern = new char[full_regex_len];\n\n  snprintf(full_pattern, full_regex_len, \"^(%s)$\", regex);\n  is_valid_ = regcomp(&full_regex_, full_pattern, REG_EXTENDED) == 0;\n  // We want to call regcomp(&partial_regex_, ...) even if the\n  // previous expression returns false.  Otherwise partial_regex_ may\n  // not be properly initialized can may cause trouble when it's\n  // freed.\n  //\n  // Some implementation of POSIX regex (e.g. on at least some\n  // versions of Cygwin) doesn't accept the empty string as a valid\n  // regex.  We change it to an equivalent form \"()\" to be safe.\n  if (is_valid_) {\n    const char* const partial_regex = (*regex == '\\0') ? \"()\" : regex;\n    is_valid_ = regcomp(&partial_regex_, partial_regex, REG_EXTENDED) == 0;\n  }\n  EXPECT_TRUE(is_valid_)\n      << \"Regular expression \\\"\" << regex\n      << \"\\\" is not a valid POSIX Extended regular expression.\";\n\n  delete[] full_pattern;\n}\n\n#elif GTEST_USES_SIMPLE_RE\n\n// Returns true iff ch appears anywhere in str (excluding the\n// terminating '\\0' character).\nbool IsInSet(char ch, const char* str) {\n  return ch != '\\0' && strchr(str, ch) != NULL;\n}\n\n// Returns true iff ch belongs to the given classification.  Unlike\n// similar functions in <ctype.h>, these aren't affected by the\n// current locale.\nbool IsAsciiDigit(char ch) { return '0' <= ch && ch <= '9'; }\nbool IsAsciiPunct(char ch) {\n  return IsInSet(ch, \"^-!\\\"#$%&'()*+,./:;<=>?@[\\\\]_`{|}~\");\n}\nbool IsRepeat(char ch) { return IsInSet(ch, \"?*+\"); }\nbool IsAsciiWhiteSpace(char ch) { return IsInSet(ch, \" \\f\\n\\r\\t\\v\"); }\nbool IsAsciiWordChar(char ch) {\n  return ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') ||\n      ('0' <= ch && ch <= '9') || ch == '_';\n}\n\n// Returns true iff \"\\\\c\" is a supported escape sequence.\nbool IsValidEscape(char c) {\n  return (IsAsciiPunct(c) || IsInSet(c, \"dDfnrsStvwW\"));\n}\n\n// Returns true iff the given atom (specified by escaped and pattern)\n// matches ch.  The result is undefined if the atom is invalid.\nbool AtomMatchesChar(bool escaped, char pattern_char, char ch) {\n  if (escaped) {  // \"\\\\p\" where p is pattern_char.\n    switch (pattern_char) {\n      case 'd': return IsAsciiDigit(ch);\n      case 'D': return !IsAsciiDigit(ch);\n      case 'f': return ch == '\\f';\n      case 'n': return ch == '\\n';\n      case 'r': return ch == '\\r';\n      case 's': return IsAsciiWhiteSpace(ch);\n      case 'S': return !IsAsciiWhiteSpace(ch);\n      case 't': return ch == '\\t';\n      case 'v': return ch == '\\v';\n      case 'w': return IsAsciiWordChar(ch);\n      case 'W': return !IsAsciiWordChar(ch);\n    }\n    return IsAsciiPunct(pattern_char) && pattern_char == ch;\n  }\n\n  return (pattern_char == '.' && ch != '\\n') || pattern_char == ch;\n}\n\n// Helper function used by ValidateRegex() to format error messages.\nstd::string FormatRegexSyntaxError(const char* regex, int index) {\n  return (Message() << \"Syntax error at index \" << index\n          << \" in simple regular expression \\\"\" << regex << \"\\\": \").GetString();\n}\n\n// Generates non-fatal failures and returns false if regex is invalid;\n// otherwise returns true.\nbool ValidateRegex(const char* regex) {\n  if (regex == NULL) {\n    // TODO(wan@google.com): fix the source file location in the\n    // assertion failures to match where the regex is used in user\n    // code.\n    ADD_FAILURE() << \"NULL is not a valid simple regular expression.\";\n    return false;\n  }\n\n  bool is_valid = true;\n\n  // True iff ?, *, or + can follow the previous atom.\n  bool prev_repeatable = false;\n  for (int i = 0; regex[i]; i++) {\n    if (regex[i] == '\\\\') {  // An escape sequence\n      i++;\n      if (regex[i] == '\\0') {\n        ADD_FAILURE() << FormatRegexSyntaxError(regex, i - 1)\n                      << \"'\\\\' cannot appear at the end.\";\n        return false;\n      }\n\n      if (!IsValidEscape(regex[i])) {\n        ADD_FAILURE() << FormatRegexSyntaxError(regex, i - 1)\n                      << \"invalid escape sequence \\\"\\\\\" << regex[i] << \"\\\".\";\n        is_valid = false;\n      }\n      prev_repeatable = true;\n    } else {  // Not an escape sequence.\n      const char ch = regex[i];\n\n      if (ch == '^' && i > 0) {\n        ADD_FAILURE() << FormatRegexSyntaxError(regex, i)\n                      << \"'^' can only appear at the beginning.\";\n        is_valid = false;\n      } else if (ch == '$' && regex[i + 1] != '\\0') {\n        ADD_FAILURE() << FormatRegexSyntaxError(regex, i)\n                      << \"'$' can only appear at the end.\";\n        is_valid = false;\n      } else if (IsInSet(ch, \"()[]{}|\")) {\n        ADD_FAILURE() << FormatRegexSyntaxError(regex, i)\n                      << \"'\" << ch << \"' is unsupported.\";\n        is_valid = false;\n      } else if (IsRepeat(ch) && !prev_repeatable) {\n        ADD_FAILURE() << FormatRegexSyntaxError(regex, i)\n                      << \"'\" << ch << \"' can only follow a repeatable token.\";\n        is_valid = false;\n      }\n\n      prev_repeatable = !IsInSet(ch, \"^$?*+\");\n    }\n  }\n\n  return is_valid;\n}\n\n// Matches a repeated regex atom followed by a valid simple regular\n// expression.  The regex atom is defined as c if escaped is false,\n// or \\c otherwise.  repeat is the repetition meta character (?, *,\n// or +).  The behavior is undefined if str contains too many\n// characters to be indexable by size_t, in which case the test will\n// probably time out anyway.  We are fine with this limitation as\n// std::string has it too.\nbool MatchRepetitionAndRegexAtHead(\n    bool escaped, char c, char repeat, const char* regex,\n    const char* str) {\n  const size_t min_count = (repeat == '+') ? 1 : 0;\n  const size_t max_count = (repeat == '?') ? 1 :\n      static_cast<size_t>(-1) - 1;\n  // We cannot call numeric_limits::max() as it conflicts with the\n  // max() macro on Windows.\n\n  for (size_t i = 0; i <= max_count; ++i) {\n    // We know that the atom matches each of the first i characters in str.\n    if (i >= min_count && MatchRegexAtHead(regex, str + i)) {\n      // We have enough matches at the head, and the tail matches too.\n      // Since we only care about *whether* the pattern matches str\n      // (as opposed to *how* it matches), there is no need to find a\n      // greedy match.\n      return true;\n    }\n    if (str[i] == '\\0' || !AtomMatchesChar(escaped, c, str[i]))\n      return false;\n  }\n  return false;\n}\n\n// Returns true iff regex matches a prefix of str.  regex must be a\n// valid simple regular expression and not start with \"^\", or the\n// result is undefined.\nbool MatchRegexAtHead(const char* regex, const char* str) {\n  if (*regex == '\\0')  // An empty regex matches a prefix of anything.\n    return true;\n\n  // \"$\" only matches the end of a string.  Note that regex being\n  // valid guarantees that there's nothing after \"$\" in it.\n  if (*regex == '$')\n    return *str == '\\0';\n\n  // Is the first thing in regex an escape sequence?\n  const bool escaped = *regex == '\\\\';\n  if (escaped)\n    ++regex;\n  if (IsRepeat(regex[1])) {\n    // MatchRepetitionAndRegexAtHead() calls MatchRegexAtHead(), so\n    // here's an indirect recursion.  It terminates as the regex gets\n    // shorter in each recursion.\n    return MatchRepetitionAndRegexAtHead(\n        escaped, regex[0], regex[1], regex + 2, str);\n  } else {\n    // regex isn't empty, isn't \"$\", and doesn't start with a\n    // repetition.  We match the first atom of regex with the first\n    // character of str and recurse.\n    return (*str != '\\0') && AtomMatchesChar(escaped, *regex, *str) &&\n        MatchRegexAtHead(regex + 1, str + 1);\n  }\n}\n\n// Returns true iff regex matches any substring of str.  regex must be\n// a valid simple regular expression, or the result is undefined.\n//\n// The algorithm is recursive, but the recursion depth doesn't exceed\n// the regex length, so we won't need to worry about running out of\n// stack space normally.  In rare cases the time complexity can be\n// exponential with respect to the regex length + the string length,\n// but usually it's must faster (often close to linear).\nbool MatchRegexAnywhere(const char* regex, const char* str) {\n  if (regex == NULL || str == NULL)\n    return false;\n\n  if (*regex == '^')\n    return MatchRegexAtHead(regex + 1, str);\n\n  // A successful match can be anywhere in str.\n  do {\n    if (MatchRegexAtHead(regex, str))\n      return true;\n  } while (*str++ != '\\0');\n  return false;\n}\n\n// Implements the RE class.\n\nRE::~RE() {\n  free(const_cast<char*>(pattern_));\n  free(const_cast<char*>(full_pattern_));\n}\n\n// Returns true iff regular expression re matches the entire str.\nbool RE::FullMatch(const char* str, const RE& re) {\n  return re.is_valid_ && MatchRegexAnywhere(re.full_pattern_, str);\n}\n\n// Returns true iff regular expression re matches a substring of str\n// (including str itself).\nbool RE::PartialMatch(const char* str, const RE& re) {\n  return re.is_valid_ && MatchRegexAnywhere(re.pattern_, str);\n}\n\n// Initializes an RE from its string representation.\nvoid RE::Init(const char* regex) {\n  pattern_ = full_pattern_ = NULL;\n  if (regex != NULL) {\n    pattern_ = posix::StrDup(regex);\n  }\n\n  is_valid_ = ValidateRegex(regex);\n  if (!is_valid_) {\n    // No need to calculate the full pattern when the regex is invalid.\n    return;\n  }\n\n  const size_t len = strlen(regex);\n  // Reserves enough bytes to hold the regular expression used for a\n  // full match: we need space to prepend a '^', append a '$', and\n  // terminate the string with '\\0'.\n  char* buffer = static_cast<char*>(malloc(len + 3));\n  full_pattern_ = buffer;\n\n  if (*regex != '^')\n    *buffer++ = '^';  // Makes sure full_pattern_ starts with '^'.\n\n  // We don't use snprintf or strncpy, as they trigger a warning when\n  // compiled with VC++ 8.0.\n  memcpy(buffer, regex, len);\n  buffer += len;\n\n  if (len == 0 || regex[len - 1] != '$')\n    *buffer++ = '$';  // Makes sure full_pattern_ ends with '$'.\n\n  *buffer = '\\0';\n}\n\n#endif  // GTEST_USES_POSIX_RE\n\nconst char kUnknownFile[] = \"unknown file\";\n\n// Formats a source file path and a line number as they would appear\n// in an error message from the compiler used to compile this code.\nGTEST_API_ ::std::string FormatFileLocation(const char* file, int line) {\n  const std::string file_name(file == NULL ? kUnknownFile : file);\n\n  if (line < 0) {\n    return file_name + \":\";\n  }\n#ifdef _MSC_VER\n  return file_name + \"(\" + StreamableToString(line) + \"):\";\n#else\n  return file_name + \":\" + StreamableToString(line) + \":\";\n#endif  // _MSC_VER\n}\n\n// Formats a file location for compiler-independent XML output.\n// Although this function is not platform dependent, we put it next to\n// FormatFileLocation in order to contrast the two functions.\n// Note that FormatCompilerIndependentFileLocation() does NOT append colon\n// to the file location it produces, unlike FormatFileLocation().\nGTEST_API_ ::std::string FormatCompilerIndependentFileLocation(\n    const char* file, int line) {\n  const std::string file_name(file == NULL ? kUnknownFile : file);\n\n  if (line < 0)\n    return file_name;\n  else\n    return file_name + \":\" + StreamableToString(line);\n}\n\nGTestLog::GTestLog(GTestLogSeverity severity, const char* file, int line)\n    : severity_(severity) {\n  const char* const marker =\n      severity == GTEST_INFO ?    \"[  INFO ]\" :\n      severity == GTEST_WARNING ? \"[WARNING]\" :\n      severity == GTEST_ERROR ?   \"[ ERROR ]\" : \"[ FATAL ]\";\n  GetStream() << ::std::endl << marker << \" \"\n              << FormatFileLocation(file, line).c_str() << \": \";\n}\n\n// Flushes the buffers and, if severity is GTEST_FATAL, aborts the program.\nGTestLog::~GTestLog() {\n  GetStream() << ::std::endl;\n  if (severity_ == GTEST_FATAL) {\n    fflush(stderr);\n    posix::Abort();\n  }\n}\n// Disable Microsoft deprecation warnings for POSIX functions called from\n// this class (creat, dup, dup2, and close)\nGTEST_DISABLE_MSC_WARNINGS_PUSH_(4996)\n\n#if GTEST_HAS_STREAM_REDIRECTION\n\n// Object that captures an output stream (stdout/stderr).\nclass CapturedStream {\n public:\n  // The ctor redirects the stream to a temporary file.\n  explicit CapturedStream(int fd) : fd_(fd), uncaptured_fd_(dup(fd)) {\n# if GTEST_OS_WINDOWS\n    char temp_dir_path[MAX_PATH + 1] = { '\\0' };  // NOLINT\n    char temp_file_path[MAX_PATH + 1] = { '\\0' };  // NOLINT\n\n    ::GetTempPathA(sizeof(temp_dir_path), temp_dir_path);\n    const UINT success = ::GetTempFileNameA(temp_dir_path,\n                                            \"gtest_redir\",\n                                            0,  // Generate unique file name.\n                                            temp_file_path);\n    GTEST_CHECK_(success != 0)\n        << \"Unable to create a temporary file in \" << temp_dir_path;\n    const int captured_fd = creat(temp_file_path, _S_IREAD | _S_IWRITE);\n    GTEST_CHECK_(captured_fd != -1) << \"Unable to open temporary file \"\n                                    << temp_file_path;\n    filename_ = temp_file_path;\n# else\n    // There's no guarantee that a test has write access to the current\n    // directory, so we create the temporary file in the /tmp directory\n    // instead. We use /tmp on most systems, and /sdcard on Android.\n    // That's because Android doesn't have /tmp.\n#  if GTEST_OS_LINUX_ANDROID\n    // Note: Android applications are expected to call the framework's\n    // Context.getExternalStorageDirectory() method through JNI to get\n    // the location of the world-writable SD Card directory. However,\n    // this requires a Context handle, which cannot be retrieved\n    // globally from native code. Doing so also precludes running the\n    // code as part of a regular standalone executable, which doesn't\n    // run in a Dalvik process (e.g. when running it through 'adb shell').\n    //\n    // The location /sdcard is directly accessible from native code\n    // and is the only location (unofficially) supported by the Android\n    // team. It's generally a symlink to the real SD Card mount point\n    // which can be /mnt/sdcard, /mnt/sdcard0, /system/media/sdcard, or\n    // other OEM-customized locations. Never rely on these, and always\n    // use /sdcard.\n    char name_template[] = \"/sdcard/gtest_captured_stream.XXXXXX\";\n#  else\n    char name_template[] = \"/tmp/captured_stream.XXXXXX\";\n#  endif  // GTEST_OS_LINUX_ANDROID\n    const int captured_fd = mkstemp(name_template);\n    filename_ = name_template;\n# endif  // GTEST_OS_WINDOWS\n    fflush(NULL);\n    dup2(captured_fd, fd_);\n    close(captured_fd);\n  }\n\n  ~CapturedStream() {\n    remove(filename_.c_str());\n  }\n\n  std::string GetCapturedString() {\n    if (uncaptured_fd_ != -1) {\n      // Restores the original stream.\n      fflush(NULL);\n      dup2(uncaptured_fd_, fd_);\n      close(uncaptured_fd_);\n      uncaptured_fd_ = -1;\n    }\n\n    FILE* const file = posix::FOpen(filename_.c_str(), \"r\");\n    const std::string content = ReadEntireFile(file);\n    posix::FClose(file);\n    return content;\n  }\n\n private:\n  const int fd_;  // A stream to capture.\n  int uncaptured_fd_;\n  // Name of the temporary file holding the stderr output.\n  ::std::string filename_;\n\n  GTEST_DISALLOW_COPY_AND_ASSIGN_(CapturedStream);\n};\n\nGTEST_DISABLE_MSC_WARNINGS_POP_()\n\nstatic CapturedStream* g_captured_stderr = NULL;\nstatic CapturedStream* g_captured_stdout = NULL;\n\n// Starts capturing an output stream (stdout/stderr).\nvoid CaptureStream(int fd, const char* stream_name, CapturedStream** stream) {\n  if (*stream != NULL) {\n    GTEST_LOG_(FATAL) << \"Only one \" << stream_name\n                      << \" capturer can exist at a time.\";\n  }\n  *stream = new CapturedStream(fd);\n}\n\n// Stops capturing the output stream and returns the captured string.\nstd::string GetCapturedStream(CapturedStream** captured_stream) {\n  const std::string content = (*captured_stream)->GetCapturedString();\n\n  delete *captured_stream;\n  *captured_stream = NULL;\n\n  return content;\n}\n\n// Starts capturing stdout.\nvoid CaptureStdout() {\n  CaptureStream(kStdOutFileno, \"stdout\", &g_captured_stdout);\n}\n\n// Starts capturing stderr.\nvoid CaptureStderr() {\n  CaptureStream(kStdErrFileno, \"stderr\", &g_captured_stderr);\n}\n\n// Stops capturing stdout and returns the captured string.\nstd::string GetCapturedStdout() {\n  return GetCapturedStream(&g_captured_stdout);\n}\n\n// Stops capturing stderr and returns the captured string.\nstd::string GetCapturedStderr() {\n  return GetCapturedStream(&g_captured_stderr);\n}\n\n#endif  // GTEST_HAS_STREAM_REDIRECTION\n\nsize_t GetFileSize(FILE* file) {\n  fseek(file, 0, SEEK_END);\n  return static_cast<size_t>(ftell(file));\n}\n\nstd::string ReadEntireFile(FILE* file) {\n  const size_t file_size = GetFileSize(file);\n  char* const buffer = new char[file_size];\n\n  size_t bytes_last_read = 0;  // # of bytes read in the last fread()\n  size_t bytes_read = 0;       // # of bytes read so far\n\n  fseek(file, 0, SEEK_SET);\n\n  // Keeps reading the file until we cannot read further or the\n  // pre-determined file size is reached.\n  do {\n    bytes_last_read = fread(buffer+bytes_read, 1, file_size-bytes_read, file);\n    bytes_read += bytes_last_read;\n  } while (bytes_last_read > 0 && bytes_read < file_size);\n\n  const std::string content(buffer, bytes_read);\n  delete[] buffer;\n\n  return content;\n}\n\n#if GTEST_HAS_DEATH_TEST\n\nstatic const ::std::vector<testing::internal::string>* g_injected_test_argvs =\n                                        NULL;  // Owned.\n\nvoid SetInjectableArgvs(const ::std::vector<testing::internal::string>* argvs) {\n  if (g_injected_test_argvs != argvs)\n    delete g_injected_test_argvs;\n  g_injected_test_argvs = argvs;\n}\n\nconst ::std::vector<testing::internal::string>& GetInjectableArgvs() {\n  if (g_injected_test_argvs != NULL) {\n    return *g_injected_test_argvs;\n  }\n  return GetArgvs();\n}\n#endif  // GTEST_HAS_DEATH_TEST\n\n#if GTEST_OS_WINDOWS_MOBILE\nnamespace posix {\nvoid Abort() {\n  DebugBreak();\n  TerminateProcess(GetCurrentProcess(), 1);\n}\n}  // namespace posix\n#endif  // GTEST_OS_WINDOWS_MOBILE\n\n// Returns the name of the environment variable corresponding to the\n// given flag.  For example, FlagToEnvVar(\"foo\") will return\n// \"GTEST_FOO\" in the open-source version.\nstatic std::string FlagToEnvVar(const char* flag) {\n  const std::string full_flag =\n      (Message() << GTEST_FLAG_PREFIX_ << flag).GetString();\n\n  Message env_var;\n  for (size_t i = 0; i != full_flag.length(); i++) {\n    env_var << ToUpper(full_flag.c_str()[i]);\n  }\n\n  return env_var.GetString();\n}\n\n// Parses 'str' for a 32-bit signed integer.  If successful, writes\n// the result to *value and returns true; otherwise leaves *value\n// unchanged and returns false.\nbool ParseInt32(const Message& src_text, const char* str, Int32* value) {\n  // Parses the environment variable as a decimal integer.\n  char* end = NULL;\n  const long long_value = strtol(str, &end, 10);  // NOLINT\n\n  // Has strtol() consumed all characters in the string?\n  if (*end != '\\0') {\n    // No - an invalid character was encountered.\n    Message msg;\n    msg << \"WARNING: \" << src_text\n        << \" is expected to be a 32-bit integer, but actually\"\n        << \" has value \\\"\" << str << \"\\\".\\n\";\n    printf(\"%s\", msg.GetString().c_str());\n    fflush(stdout);\n    return false;\n  }\n\n  // Is the parsed value in the range of an Int32?\n  const Int32 result = static_cast<Int32>(long_value);\n  if (long_value == LONG_MAX || long_value == LONG_MIN ||\n      // The parsed value overflows as a long.  (strtol() returns\n      // LONG_MAX or LONG_MIN when the input overflows.)\n      result != long_value\n      // The parsed value overflows as an Int32.\n      ) {\n    Message msg;\n    msg << \"WARNING: \" << src_text\n        << \" is expected to be a 32-bit integer, but actually\"\n        << \" has value \" << str << \", which overflows.\\n\";\n    printf(\"%s\", msg.GetString().c_str());\n    fflush(stdout);\n    return false;\n  }\n\n  *value = result;\n  return true;\n}\n\n// Reads and returns the Boolean environment variable corresponding to\n// the given flag; if it's not set, returns default_value.\n//\n// The value is considered true iff it's not \"0\".\nbool BoolFromGTestEnv(const char* flag, bool default_value) {\n#if defined(GTEST_GET_BOOL_FROM_ENV_)\n  return GTEST_GET_BOOL_FROM_ENV_(flag, default_value);\n#endif  // defined(GTEST_GET_BOOL_FROM_ENV_)\n  const std::string env_var = FlagToEnvVar(flag);\n  const char* const string_value = posix::GetEnv(env_var.c_str());\n  return string_value == NULL ?\n      default_value : strcmp(string_value, \"0\") != 0;\n}\n\n// Reads and returns a 32-bit integer stored in the environment\n// variable corresponding to the given flag; if it isn't set or\n// doesn't represent a valid 32-bit integer, returns default_value.\nInt32 Int32FromGTestEnv(const char* flag, Int32 default_value) {\n#if defined(GTEST_GET_INT32_FROM_ENV_)\n  return GTEST_GET_INT32_FROM_ENV_(flag, default_value);\n#endif  // defined(GTEST_GET_INT32_FROM_ENV_)\n  const std::string env_var = FlagToEnvVar(flag);\n  const char* const string_value = posix::GetEnv(env_var.c_str());\n  if (string_value == NULL) {\n    // The environment variable is not set.\n    return default_value;\n  }\n\n  Int32 result = default_value;\n  if (!ParseInt32(Message() << \"Environment variable \" << env_var,\n                  string_value, &result)) {\n    printf(\"The default value %s is used.\\n\",\n           (Message() << default_value).GetString().c_str());\n    fflush(stdout);\n    return default_value;\n  }\n\n  return result;\n}\n\n// Reads and returns the string environment variable corresponding to\n// the given flag; if it's not set, returns default_value.\nstd::string StringFromGTestEnv(const char* flag, const char* default_value) {\n#if defined(GTEST_GET_STRING_FROM_ENV_)\n  return GTEST_GET_STRING_FROM_ENV_(flag, default_value);\n#endif  // defined(GTEST_GET_STRING_FROM_ENV_)\n  const std::string env_var = FlagToEnvVar(flag);\n  const char* value = posix::GetEnv(env_var.c_str());\n  if (value != NULL) {\n    return value;\n  }\n\n  // As a special case for the 'output' flag, if GTEST_OUTPUT is not\n  // set, we look for XML_OUTPUT_FILE, which is set by the Bazel build\n  // system.  The value of XML_OUTPUT_FILE is a filename without the\n  // \"xml:\" prefix of GTEST_OUTPUT.\n  //\n  // The net priority order after flag processing is thus:\n  //   --gtest_output command line flag\n  //   GTEST_OUTPUT environment variable\n  //   XML_OUTPUT_FILE environment variable\n  //   'default_value'\n  if (strcmp(flag, \"output\") == 0) {\n    value = posix::GetEnv(\"XML_OUTPUT_FILE\");\n    if (value != NULL) {\n      return std::string(\"xml:\") + value;\n    }\n  }\n  return default_value;\n}\n\n}  // namespace internal\n}  // namespace testing\n// Copyright 2007, Google Inc.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Author: wan@google.com (Zhanyong Wan)\n\n// Google Test - The Google C++ Testing Framework\n//\n// This file implements a universal value printer that can print a\n// value of any type T:\n//\n//   void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr);\n//\n// It uses the << operator when possible, and prints the bytes in the\n// object otherwise.  A user can override its behavior for a class\n// type Foo by defining either operator<<(::std::ostream&, const Foo&)\n// or void PrintTo(const Foo&, ::std::ostream*) in the namespace that\n// defines Foo.\n\n#include <ctype.h>\n#include <stdio.h>\n#include <cwchar>\n#include <ostream>  // NOLINT\n#include <string>\n\nnamespace testing {\n\nnamespace {\n\nusing ::std::ostream;\n\n// Prints a segment of bytes in the given object.\nGTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_\nGTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_\nGTEST_ATTRIBUTE_NO_SANITIZE_THREAD_\nvoid PrintByteSegmentInObjectTo(const unsigned char* obj_bytes, size_t start,\n                                size_t count, ostream* os) {\n  char text[5] = \"\";\n  for (size_t i = 0; i != count; i++) {\n    const size_t j = start + i;\n    if (i != 0) {\n      // Organizes the bytes into groups of 2 for easy parsing by\n      // human.\n      if ((j % 2) == 0)\n        *os << ' ';\n      else\n        *os << '-';\n    }\n    GTEST_SNPRINTF_(text, sizeof(text), \"%02X\", obj_bytes[j]);\n    *os << text;\n  }\n}\n\n// Prints the bytes in the given value to the given ostream.\nvoid PrintBytesInObjectToImpl(const unsigned char* obj_bytes, size_t count,\n                              ostream* os) {\n  // Tells the user how big the object is.\n  *os << count << \"-byte object <\";\n\n  const size_t kThreshold = 132;\n  const size_t kChunkSize = 64;\n  // If the object size is bigger than kThreshold, we'll have to omit\n  // some details by printing only the first and the last kChunkSize\n  // bytes.\n  // TODO(wan): let the user control the threshold using a flag.\n  if (count < kThreshold) {\n    PrintByteSegmentInObjectTo(obj_bytes, 0, count, os);\n  } else {\n    PrintByteSegmentInObjectTo(obj_bytes, 0, kChunkSize, os);\n    *os << \" ... \";\n    // Rounds up to 2-byte boundary.\n    const size_t resume_pos = (count - kChunkSize + 1)/2*2;\n    PrintByteSegmentInObjectTo(obj_bytes, resume_pos, count - resume_pos, os);\n  }\n  *os << \">\";\n}\n\n}  // namespace\n\nnamespace internal2 {\n\n// Delegates to PrintBytesInObjectToImpl() to print the bytes in the\n// given object.  The delegation simplifies the implementation, which\n// uses the << operator and thus is easier done outside of the\n// ::testing::internal namespace, which contains a << operator that\n// sometimes conflicts with the one in STL.\nvoid PrintBytesInObjectTo(const unsigned char* obj_bytes, size_t count,\n                          ostream* os) {\n  PrintBytesInObjectToImpl(obj_bytes, count, os);\n}\n\n}  // namespace internal2\n\nnamespace internal {\n\n// Depending on the value of a char (or wchar_t), we print it in one\n// of three formats:\n//   - as is if it's a printable ASCII (e.g. 'a', '2', ' '),\n//   - as a hexidecimal escape sequence (e.g. '\\x7F'), or\n//   - as a special escape sequence (e.g. '\\r', '\\n').\nenum CharFormat {\n  kAsIs,\n  kHexEscape,\n  kSpecialEscape\n};\n\n// Returns true if c is a printable ASCII character.  We test the\n// value of c directly instead of calling isprint(), which is buggy on\n// Windows Mobile.\ninline bool IsPrintableAscii(wchar_t c) {\n  return 0x20 <= c && c <= 0x7E;\n}\n\n// Prints a wide or narrow char c as a character literal without the\n// quotes, escaping it when necessary; returns how c was formatted.\n// The template argument UnsignedChar is the unsigned version of Char,\n// which is the type of c.\ntemplate <typename UnsignedChar, typename Char>\nstatic CharFormat PrintAsCharLiteralTo(Char c, ostream* os) {\n  switch (static_cast<wchar_t>(c)) {\n    case L'\\0':\n      *os << \"\\\\0\";\n      break;\n    case L'\\'':\n      *os << \"\\\\'\";\n      break;\n    case L'\\\\':\n      *os << \"\\\\\\\\\";\n      break;\n    case L'\\a':\n      *os << \"\\\\a\";\n      break;\n    case L'\\b':\n      *os << \"\\\\b\";\n      break;\n    case L'\\f':\n      *os << \"\\\\f\";\n      break;\n    case L'\\n':\n      *os << \"\\\\n\";\n      break;\n    case L'\\r':\n      *os << \"\\\\r\";\n      break;\n    case L'\\t':\n      *os << \"\\\\t\";\n      break;\n    case L'\\v':\n      *os << \"\\\\v\";\n      break;\n    default:\n      if (IsPrintableAscii(c)) {\n        *os << static_cast<char>(c);\n        return kAsIs;\n      } else {\n        *os << \"\\\\x\" + String::FormatHexInt(static_cast<UnsignedChar>(c));\n        return kHexEscape;\n      }\n  }\n  return kSpecialEscape;\n}\n\n// Prints a wchar_t c as if it's part of a string literal, escaping it when\n// necessary; returns how c was formatted.\nstatic CharFormat PrintAsStringLiteralTo(wchar_t c, ostream* os) {\n  switch (c) {\n    case L'\\'':\n      *os << \"'\";\n      return kAsIs;\n    case L'\"':\n      *os << \"\\\\\\\"\";\n      return kSpecialEscape;\n    default:\n      return PrintAsCharLiteralTo<wchar_t>(c, os);\n  }\n}\n\n// Prints a char c as if it's part of a string literal, escaping it when\n// necessary; returns how c was formatted.\nstatic CharFormat PrintAsStringLiteralTo(char c, ostream* os) {\n  return PrintAsStringLiteralTo(\n      static_cast<wchar_t>(static_cast<unsigned char>(c)), os);\n}\n\n// Prints a wide or narrow character c and its code.  '\\0' is printed\n// as \"'\\\\0'\", other unprintable characters are also properly escaped\n// using the standard C++ escape sequence.  The template argument\n// UnsignedChar is the unsigned version of Char, which is the type of c.\ntemplate <typename UnsignedChar, typename Char>\nvoid PrintCharAndCodeTo(Char c, ostream* os) {\n  // First, print c as a literal in the most readable form we can find.\n  *os << ((sizeof(c) > 1) ? \"L'\" : \"'\");\n  const CharFormat format = PrintAsCharLiteralTo<UnsignedChar>(c, os);\n  *os << \"'\";\n\n  // To aid user debugging, we also print c's code in decimal, unless\n  // it's 0 (in which case c was printed as '\\\\0', making the code\n  // obvious).\n  if (c == 0)\n    return;\n  *os << \" (\" << static_cast<int>(c);\n\n  // For more convenience, we print c's code again in hexidecimal,\n  // unless c was already printed in the form '\\x##' or the code is in\n  // [1, 9].\n  if (format == kHexEscape || (1 <= c && c <= 9)) {\n    // Do nothing.\n  } else {\n    *os << \", 0x\" << String::FormatHexInt(static_cast<UnsignedChar>(c));\n  }\n  *os << \")\";\n}\n\nvoid PrintTo(unsigned char c, ::std::ostream* os) {\n  PrintCharAndCodeTo<unsigned char>(c, os);\n}\nvoid PrintTo(signed char c, ::std::ostream* os) {\n  PrintCharAndCodeTo<unsigned char>(c, os);\n}\n\n// Prints a wchar_t as a symbol if it is printable or as its internal\n// code otherwise and also as its code.  L'\\0' is printed as \"L'\\\\0'\".\nvoid PrintTo(wchar_t wc, ostream* os) {\n  PrintCharAndCodeTo<wchar_t>(wc, os);\n}\n\n// Prints the given array of characters to the ostream.  CharType must be either\n// char or wchar_t.\n// The array starts at begin, the length is len, it may include '\\0' characters\n// and may not be NUL-terminated.\ntemplate <typename CharType>\nGTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_\nGTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_\nGTEST_ATTRIBUTE_NO_SANITIZE_THREAD_\nstatic void PrintCharsAsStringTo(\n    const CharType* begin, size_t len, ostream* os) {\n  const char* const kQuoteBegin = sizeof(CharType) == 1 ? \"\\\"\" : \"L\\\"\";\n  *os << kQuoteBegin;\n  bool is_previous_hex = false;\n  for (size_t index = 0; index < len; ++index) {\n    const CharType cur = begin[index];\n    if (is_previous_hex && IsXDigit(cur)) {\n      // Previous character is of '\\x..' form and this character can be\n      // interpreted as another hexadecimal digit in its number. Break string to\n      // disambiguate.\n      *os << \"\\\" \" << kQuoteBegin;\n    }\n    is_previous_hex = PrintAsStringLiteralTo(cur, os) == kHexEscape;\n  }\n  *os << \"\\\"\";\n}\n\n// Prints a (const) char/wchar_t array of 'len' elements, starting at address\n// 'begin'.  CharType must be either char or wchar_t.\ntemplate <typename CharType>\nGTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_\nGTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_\nGTEST_ATTRIBUTE_NO_SANITIZE_THREAD_\nstatic void UniversalPrintCharArray(\n    const CharType* begin, size_t len, ostream* os) {\n  // The code\n  //   const char kFoo[] = \"foo\";\n  // generates an array of 4, not 3, elements, with the last one being '\\0'.\n  //\n  // Therefore when printing a char array, we don't print the last element if\n  // it's '\\0', such that the output matches the string literal as it's\n  // written in the source code.\n  if (len > 0 && begin[len - 1] == '\\0') {\n    PrintCharsAsStringTo(begin, len - 1, os);\n    return;\n  }\n\n  // If, however, the last element in the array is not '\\0', e.g.\n  //    const char kFoo[] = { 'f', 'o', 'o' };\n  // we must print the entire array.  We also print a message to indicate\n  // that the array is not NUL-terminated.\n  PrintCharsAsStringTo(begin, len, os);\n  *os << \" (no terminating NUL)\";\n}\n\n// Prints a (const) char array of 'len' elements, starting at address 'begin'.\nvoid UniversalPrintArray(const char* begin, size_t len, ostream* os) {\n  UniversalPrintCharArray(begin, len, os);\n}\n\n// Prints a (const) wchar_t array of 'len' elements, starting at address\n// 'begin'.\nvoid UniversalPrintArray(const wchar_t* begin, size_t len, ostream* os) {\n  UniversalPrintCharArray(begin, len, os);\n}\n\n// Prints the given C string to the ostream.\nvoid PrintTo(const char* s, ostream* os) {\n  if (s == NULL) {\n    *os << \"NULL\";\n  } else {\n    *os << ImplicitCast_<const void*>(s) << \" pointing to \";\n    PrintCharsAsStringTo(s, strlen(s), os);\n  }\n}\n\n// MSVC compiler can be configured to define whar_t as a typedef\n// of unsigned short. Defining an overload for const wchar_t* in that case\n// would cause pointers to unsigned shorts be printed as wide strings,\n// possibly accessing more memory than intended and causing invalid\n// memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when\n// wchar_t is implemented as a native type.\n#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)\n// Prints the given wide C string to the ostream.\nvoid PrintTo(const wchar_t* s, ostream* os) {\n  if (s == NULL) {\n    *os << \"NULL\";\n  } else {\n    *os << ImplicitCast_<const void*>(s) << \" pointing to \";\n    PrintCharsAsStringTo(s, std::wcslen(s), os);\n  }\n}\n#endif  // wchar_t is native\n\n// Prints a ::string object.\n#if GTEST_HAS_GLOBAL_STRING\nvoid PrintStringTo(const ::string& s, ostream* os) {\n  PrintCharsAsStringTo(s.data(), s.size(), os);\n}\n#endif  // GTEST_HAS_GLOBAL_STRING\n\nvoid PrintStringTo(const ::std::string& s, ostream* os) {\n  PrintCharsAsStringTo(s.data(), s.size(), os);\n}\n\n// Prints a ::wstring object.\n#if GTEST_HAS_GLOBAL_WSTRING\nvoid PrintWideStringTo(const ::wstring& s, ostream* os) {\n  PrintCharsAsStringTo(s.data(), s.size(), os);\n}\n#endif  // GTEST_HAS_GLOBAL_WSTRING\n\n#if GTEST_HAS_STD_WSTRING\nvoid PrintWideStringTo(const ::std::wstring& s, ostream* os) {\n  PrintCharsAsStringTo(s.data(), s.size(), os);\n}\n#endif  // GTEST_HAS_STD_WSTRING\n\n}  // namespace internal\n\n}  // namespace testing\n// Copyright 2008, Google Inc.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Author: mheule@google.com (Markus Heule)\n//\n// The Google C++ Testing Framework (Google Test)\n\n\n// Indicates that this translation unit is part of Google Test's\n// implementation.  It must come before gtest-internal-inl.h is\n// included, or there will be a compiler error.  This trick exists to\n// prevent the accidental inclusion of gtest-internal-inl.h in the\n// user's code.\n#define GTEST_IMPLEMENTATION_ 1\n#undef GTEST_IMPLEMENTATION_\n\nnamespace testing {\n\nusing internal::GetUnitTestImpl;\n\n// Gets the summary of the failure message by omitting the stack trace\n// in it.\nstd::string TestPartResult::ExtractSummary(const char* message) {\n  const char* const stack_trace = strstr(message, internal::kStackTraceMarker);\n  return stack_trace == NULL ? message :\n      std::string(message, stack_trace);\n}\n\n// Prints a TestPartResult object.\nstd::ostream& operator<<(std::ostream& os, const TestPartResult& result) {\n  return os\n      << result.file_name() << \":\" << result.line_number() << \": \"\n      << (result.type() == TestPartResult::kSuccess ? \"Success\" :\n          result.type() == TestPartResult::kFatalFailure ? \"Fatal failure\" :\n          \"Non-fatal failure\") << \":\\n\"\n      << result.message() << std::endl;\n}\n\n// Appends a TestPartResult to the array.\nvoid TestPartResultArray::Append(const TestPartResult& result) {\n  array_.push_back(result);\n}\n\n// Returns the TestPartResult at the given index (0-based).\nconst TestPartResult& TestPartResultArray::GetTestPartResult(int index) const {\n  if (index < 0 || index >= size()) {\n    printf(\"\\nInvalid index (%d) into TestPartResultArray.\\n\", index);\n    internal::posix::Abort();\n  }\n\n  return array_[index];\n}\n\n// Returns the number of TestPartResult objects in the array.\nint TestPartResultArray::size() const {\n  return static_cast<int>(array_.size());\n}\n\nnamespace internal {\n\nHasNewFatalFailureHelper::HasNewFatalFailureHelper()\n    : has_new_fatal_failure_(false),\n      original_reporter_(GetUnitTestImpl()->\n                         GetTestPartResultReporterForCurrentThread()) {\n  GetUnitTestImpl()->SetTestPartResultReporterForCurrentThread(this);\n}\n\nHasNewFatalFailureHelper::~HasNewFatalFailureHelper() {\n  GetUnitTestImpl()->SetTestPartResultReporterForCurrentThread(\n      original_reporter_);\n}\n\nvoid HasNewFatalFailureHelper::ReportTestPartResult(\n    const TestPartResult& result) {\n  if (result.fatally_failed())\n    has_new_fatal_failure_ = true;\n  original_reporter_->ReportTestPartResult(result);\n}\n\n}  // namespace internal\n\n}  // namespace testing\n// Copyright 2008 Google Inc.\n// All Rights Reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Author: wan@google.com (Zhanyong Wan)\n\n\nnamespace testing {\nnamespace internal {\n\n#if GTEST_HAS_TYPED_TEST_P\n\n// Skips to the first non-space char in str. Returns an empty string if str\n// contains only whitespace characters.\nstatic const char* SkipSpaces(const char* str) {\n  while (IsSpace(*str))\n    str++;\n  return str;\n}\n\nstatic std::vector<std::string> SplitIntoTestNames(const char* src) {\n  std::vector<std::string> name_vec;\n  src = SkipSpaces(src);\n  for (; src != NULL; src = SkipComma(src)) {\n    name_vec.push_back(StripTrailingSpaces(GetPrefixUntilComma(src)));\n  }\n  return name_vec;\n}\n\n// Verifies that registered_tests match the test names in\n// registered_tests_; returns registered_tests if successful, or\n// aborts the program otherwise.\nconst char* TypedTestCasePState::VerifyRegisteredTestNames(\n    const char* file, int line, const char* registered_tests) {\n  typedef RegisteredTestsMap::const_iterator RegisteredTestIter;\n  registered_ = true;\n\n  std::vector<std::string> name_vec = SplitIntoTestNames(registered_tests);\n\n  Message errors;\n\n  std::set<std::string> tests;\n  for (std::vector<std::string>::const_iterator name_it = name_vec.begin();\n       name_it != name_vec.end(); ++name_it) {\n    const std::string& name = *name_it;\n    if (tests.count(name) != 0) {\n      errors << \"Test \" << name << \" is listed more than once.\\n\";\n      continue;\n    }\n\n    bool found = false;\n    for (RegisteredTestIter it = registered_tests_.begin();\n         it != registered_tests_.end();\n         ++it) {\n      if (name == it->first) {\n        found = true;\n        break;\n      }\n    }\n\n    if (found) {\n      tests.insert(name);\n    } else {\n      errors << \"No test named \" << name\n             << \" can be found in this test case.\\n\";\n    }\n  }\n\n  for (RegisteredTestIter it = registered_tests_.begin();\n       it != registered_tests_.end();\n       ++it) {\n    if (tests.count(it->first) == 0) {\n      errors << \"You forgot to list test \" << it->first << \".\\n\";\n    }\n  }\n\n  const std::string& errors_str = errors.GetString();\n  if (errors_str != \"\") {\n    fprintf(stderr, \"%s %s\", FormatFileLocation(file, line).c_str(),\n            errors_str.c_str());\n    fflush(stderr);\n    posix::Abort();\n  }\n\n  return registered_tests;\n}\n\n#endif  // GTEST_HAS_TYPED_TEST_P\n\n}  // namespace internal\n}  // namespace testing\n"
  },
  {
    "path": "deps/memkind/src/test/gtest_fused/gtest/gtest.h",
    "content": "// Copyright 2005, Google Inc.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Author: wan@google.com (Zhanyong Wan)\n//\n// The Google C++ Testing Framework (Google Test)\n//\n// This header file defines the public API for Google Test.  It should be\n// included by any test program that uses Google Test.\n//\n// IMPORTANT NOTE: Due to limitation of the C++ language, we have to\n// leave some internal implementation details in this header file.\n// They are clearly marked by comments like this:\n//\n//   // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.\n//\n// Such code is NOT meant to be used by a user directly, and is subject\n// to CHANGE WITHOUT NOTICE.  Therefore DO NOT DEPEND ON IT in a user\n// program!\n//\n// Acknowledgment: Google Test borrowed the idea of automatic test\n// registration from Barthelemy Dagenais' (barthelemy@prologique.com)\n// easyUnit framework.\n\n#ifndef GTEST_INCLUDE_GTEST_GTEST_H_\n#define GTEST_INCLUDE_GTEST_GTEST_H_\n\n#include <limits>\n#include <ostream>\n#include <vector>\n\n// Copyright 2005, Google Inc.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee)\n//\n// The Google C++ Testing Framework (Google Test)\n//\n// This header file declares functions and macros used internally by\n// Google Test.  They are subject to change without notice.\n\n#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_\n#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_\n\n// Copyright 2005, Google Inc.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Authors: wan@google.com (Zhanyong Wan)\n//\n// Low-level types and utilities for porting Google Test to various\n// platforms.  All macros ending with _ and symbols defined in an\n// internal namespace are subject to change without notice.  Code\n// outside Google Test MUST NOT USE THEM DIRECTLY.  Macros that don't\n// end with _ are part of Google Test's public API and can be used by\n// code outside Google Test.\n//\n// This file is fundamental to Google Test.  All other Google Test source\n// files are expected to #include this.  Therefore, it cannot #include\n// any other Google Test header.\n\n#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_\n#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_\n\n// Environment-describing macros\n// -----------------------------\n//\n// Google Test can be used in many different environments.  Macros in\n// this section tell Google Test what kind of environment it is being\n// used in, such that Google Test can provide environment-specific\n// features and implementations.\n//\n// Google Test tries to automatically detect the properties of its\n// environment, so users usually don't need to worry about these\n// macros.  However, the automatic detection is not perfect.\n// Sometimes it's necessary for a user to define some of the following\n// macros in the build script to override Google Test's decisions.\n//\n// If the user doesn't define a macro in the list, Google Test will\n// provide a default definition.  After this header is #included, all\n// macros in this list will be defined to either 1 or 0.\n//\n// Notes to maintainers:\n//   - Each macro here is a user-tweakable knob; do not grow the list\n//     lightly.\n//   - Use #if to key off these macros.  Don't use #ifdef or \"#if\n//     defined(...)\", which will not work as these macros are ALWAYS\n//     defined.\n//\n//   GTEST_HAS_CLONE          - Define it to 1/0 to indicate that clone(2)\n//                              is/isn't available.\n//   GTEST_HAS_EXCEPTIONS     - Define it to 1/0 to indicate that exceptions\n//                              are enabled.\n//   GTEST_HAS_GLOBAL_STRING  - Define it to 1/0 to indicate that ::string\n//                              is/isn't available (some systems define\n//                              ::string, which is different to std::string).\n//   GTEST_HAS_GLOBAL_WSTRING - Define it to 1/0 to indicate that ::string\n//                              is/isn't available (some systems define\n//                              ::wstring, which is different to std::wstring).\n//   GTEST_HAS_POSIX_RE       - Define it to 1/0 to indicate that POSIX regular\n//                              expressions are/aren't available.\n//   GTEST_HAS_PTHREAD        - Define it to 1/0 to indicate that <pthread.h>\n//                              is/isn't available.\n//   GTEST_HAS_RTTI           - Define it to 1/0 to indicate that RTTI is/isn't\n//                              enabled.\n//   GTEST_HAS_STD_WSTRING    - Define it to 1/0 to indicate that\n//                              std::wstring does/doesn't work (Google Test can\n//                              be used where std::wstring is unavailable).\n//   GTEST_HAS_TR1_TUPLE      - Define it to 1/0 to indicate tr1::tuple\n//                              is/isn't available.\n//   GTEST_HAS_SEH            - Define it to 1/0 to indicate whether the\n//                              compiler supports Microsoft's \"Structured\n//                              Exception Handling\".\n//   GTEST_HAS_STREAM_REDIRECTION\n//                            - Define it to 1/0 to indicate whether the\n//                              platform supports I/O stream redirection using\n//                              dup() and dup2().\n//   GTEST_USE_OWN_TR1_TUPLE  - Define it to 1/0 to indicate whether Google\n//                              Test's own tr1 tuple implementation should be\n//                              used.  Unused when the user sets\n//                              GTEST_HAS_TR1_TUPLE to 0.\n//   GTEST_LANG_CXX11         - Define it to 1/0 to indicate that Google Test\n//                              is building in C++11/C++98 mode.\n//   GTEST_LINKED_AS_SHARED_LIBRARY\n//                            - Define to 1 when compiling tests that use\n//                              Google Test as a shared library (known as\n//                              DLL on Windows).\n//   GTEST_CREATE_SHARED_LIBRARY\n//                            - Define to 1 when compiling Google Test itself\n//                              as a shared library.\n\n// Platform-indicating macros\n// --------------------------\n//\n// Macros indicating the platform on which Google Test is being used\n// (a macro is defined to 1 if compiled on the given platform;\n// otherwise UNDEFINED -- it's never defined to 0.).  Google Test\n// defines these macros automatically.  Code outside Google Test MUST\n// NOT define them.\n//\n//   GTEST_OS_AIX      - IBM AIX\n//   GTEST_OS_CYGWIN   - Cygwin\n//   GTEST_OS_FREEBSD  - FreeBSD\n//   GTEST_OS_HPUX     - HP-UX\n//   GTEST_OS_LINUX    - Linux\n//     GTEST_OS_LINUX_ANDROID - Google Android\n//   GTEST_OS_MAC      - Mac OS X\n//     GTEST_OS_IOS    - iOS\n//   GTEST_OS_NACL     - Google Native Client (NaCl)\n//   GTEST_OS_NETBSD   - NetBSD\n//   GTEST_OS_OPENBSD  - OpenBSD\n//   GTEST_OS_QNX      - QNX\n//   GTEST_OS_SOLARIS  - Sun Solaris\n//   GTEST_OS_SYMBIAN  - Symbian\n//   GTEST_OS_WINDOWS  - Windows (Desktop, MinGW, or Mobile)\n//     GTEST_OS_WINDOWS_DESKTOP  - Windows Desktop\n//     GTEST_OS_WINDOWS_MINGW    - MinGW\n//     GTEST_OS_WINDOWS_MOBILE   - Windows Mobile\n//     GTEST_OS_WINDOWS_PHONE    - Windows Phone\n//     GTEST_OS_WINDOWS_RT       - Windows Store App/WinRT\n//   GTEST_OS_ZOS      - z/OS\n//\n// Among the platforms, Cygwin, Linux, Max OS X, and Windows have the\n// most stable support.  Since core members of the Google Test project\n// don't have access to other platforms, support for them may be less\n// stable.  If you notice any problems on your platform, please notify\n// googletestframework@googlegroups.com (patches for fixing them are\n// even more welcome!).\n//\n// It is possible that none of the GTEST_OS_* macros are defined.\n\n// Feature-indicating macros\n// -------------------------\n//\n// Macros indicating which Google Test features are available (a macro\n// is defined to 1 if the corresponding feature is supported;\n// otherwise UNDEFINED -- it's never defined to 0.).  Google Test\n// defines these macros automatically.  Code outside Google Test MUST\n// NOT define them.\n//\n// These macros are public so that portable tests can be written.\n// Such tests typically surround code using a feature with an #if\n// which controls that code.  For example:\n//\n// #if GTEST_HAS_DEATH_TEST\n//   EXPECT_DEATH(DoSomethingDeadly());\n// #endif\n//\n//   GTEST_HAS_COMBINE      - the Combine() function (for value-parameterized\n//                            tests)\n//   GTEST_HAS_DEATH_TEST   - death tests\n//   GTEST_HAS_PARAM_TEST   - value-parameterized tests\n//   GTEST_HAS_TYPED_TEST   - typed tests\n//   GTEST_HAS_TYPED_TEST_P - type-parameterized tests\n//   GTEST_IS_THREADSAFE    - Google Test is thread-safe.\n//   GTEST_USES_POSIX_RE    - enhanced POSIX regex is used. Do not confuse with\n//                            GTEST_HAS_POSIX_RE (see above) which users can\n//                            define themselves.\n//   GTEST_USES_SIMPLE_RE   - our own simple regex is used;\n//                            the above two are mutually exclusive.\n//   GTEST_CAN_COMPARE_NULL - accepts untyped NULL in EXPECT_EQ().\n\n// Misc public macros\n// ------------------\n//\n//   GTEST_FLAG(flag_name)  - references the variable corresponding to\n//                            the given Google Test flag.\n\n// Internal utilities\n// ------------------\n//\n// The following macros and utilities are for Google Test's INTERNAL\n// use only.  Code outside Google Test MUST NOT USE THEM DIRECTLY.\n//\n// Macros for basic C++ coding:\n//   GTEST_AMBIGUOUS_ELSE_BLOCKER_ - for disabling a gcc warning.\n//   GTEST_ATTRIBUTE_UNUSED_  - declares that a class' instances or a\n//                              variable don't have to be used.\n//   GTEST_DISALLOW_ASSIGN_   - disables operator=.\n//   GTEST_DISALLOW_COPY_AND_ASSIGN_ - disables copy ctor and operator=.\n//   GTEST_MUST_USE_RESULT_   - declares that a function's result must be used.\n//   GTEST_INTENTIONAL_CONST_COND_PUSH_ - start code section where MSVC C4127 is\n//                                        suppressed (constant conditional).\n//   GTEST_INTENTIONAL_CONST_COND_POP_  - finish code section where MSVC C4127\n//                                        is suppressed.\n//\n// C++11 feature wrappers:\n//\n//   testing::internal::move  - portability wrapper for std::move.\n//\n// Synchronization:\n//   Mutex, MutexLock, ThreadLocal, GetThreadCount()\n//                            - synchronization primitives.\n//\n// Template meta programming:\n//   is_pointer     - as in TR1; needed on Symbian and IBM XL C/C++ only.\n//   IteratorTraits - partial implementation of std::iterator_traits, which\n//                    is not available in libCstd when compiled with Sun C++.\n//\n// Smart pointers:\n//   scoped_ptr     - as in TR2.\n//\n// Regular expressions:\n//   RE             - a simple regular expression class using the POSIX\n//                    Extended Regular Expression syntax on UNIX-like\n//                    platforms, or a reduced regular exception syntax on\n//                    other platforms, including Windows.\n//\n// Logging:\n//   GTEST_LOG_()   - logs messages at the specified severity level.\n//   LogToStderr()  - directs all log messages to stderr.\n//   FlushInfoLog() - flushes informational log messages.\n//\n// Stdout and stderr capturing:\n//   CaptureStdout()     - starts capturing stdout.\n//   GetCapturedStdout() - stops capturing stdout and returns the captured\n//                         string.\n//   CaptureStderr()     - starts capturing stderr.\n//   GetCapturedStderr() - stops capturing stderr and returns the captured\n//                         string.\n//\n// Integer types:\n//   TypeWithSize   - maps an integer to a int type.\n//   Int32, UInt32, Int64, UInt64, TimeInMillis\n//                  - integers of known sizes.\n//   BiggestInt     - the biggest signed integer type.\n//\n// Command-line utilities:\n//   GTEST_DECLARE_*()  - declares a flag.\n//   GTEST_DEFINE_*()   - defines a flag.\n//   GetInjectableArgvs() - returns the command line as a vector of strings.\n//\n// Environment variable utilities:\n//   GetEnv()             - gets the value of an environment variable.\n//   BoolFromGTestEnv()   - parses a bool environment variable.\n//   Int32FromGTestEnv()  - parses an Int32 environment variable.\n//   StringFromGTestEnv() - parses a string environment variable.\n\n#include <ctype.h>   // for isspace, etc\n#include <stddef.h>  // for ptrdiff_t\n#include <stdlib.h>\n#include <stdio.h>\n#include <string.h>\n#ifndef _WIN32_WCE\n# include <sys/types.h>\n# include <sys/stat.h>\n#endif  // !_WIN32_WCE\n\n#if defined __APPLE__\n# include <AvailabilityMacros.h>\n# include <TargetConditionals.h>\n#endif\n\n#include <algorithm>  // NOLINT\n#include <iostream>  // NOLINT\n#include <sstream>  // NOLINT\n#include <string>  // NOLINT\n#include <utility>\n#include <vector>  // NOLINT\n\n// Copyright 2015, Google Inc.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// The Google C++ Testing Framework (Google Test)\n//\n// This header file defines the GTEST_OS_* macro.\n// It is separate from gtest-port.h so that custom/gtest-port.h can include it.\n\n#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_ARCH_H_\n#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_ARCH_H_\n\n// Determines the platform on which Google Test is compiled.\n#ifdef __CYGWIN__\n# define GTEST_OS_CYGWIN 1\n#elif defined __SYMBIAN32__\n# define GTEST_OS_SYMBIAN 1\n#elif defined _WIN32\n# define GTEST_OS_WINDOWS 1\n# ifdef _WIN32_WCE\n#  define GTEST_OS_WINDOWS_MOBILE 1\n# elif defined(__MINGW__) || defined(__MINGW32__)\n#  define GTEST_OS_WINDOWS_MINGW 1\n# elif defined(WINAPI_FAMILY)\n#  include <winapifamily.h>\n#  if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)\n#   define GTEST_OS_WINDOWS_DESKTOP 1\n#  elif WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_PHONE_APP)\n#   define GTEST_OS_WINDOWS_PHONE 1\n#  elif WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)\n#   define GTEST_OS_WINDOWS_RT 1\n#  else\n// WINAPI_FAMILY defined but no known partition matched.\n// Default to desktop.\n#   define GTEST_OS_WINDOWS_DESKTOP 1\n#  endif\n# else\n#  define GTEST_OS_WINDOWS_DESKTOP 1\n# endif  // _WIN32_WCE\n#elif defined __APPLE__\n# define GTEST_OS_MAC 1\n# if TARGET_OS_IPHONE\n#  define GTEST_OS_IOS 1\n# endif\n#elif defined __FreeBSD__\n# define GTEST_OS_FREEBSD 1\n#elif defined __linux__\n# define GTEST_OS_LINUX 1\n# if defined __ANDROID__\n#  define GTEST_OS_LINUX_ANDROID 1\n# endif\n#elif defined __MVS__\n# define GTEST_OS_ZOS 1\n#elif defined(__sun) && defined(__SVR4)\n# define GTEST_OS_SOLARIS 1\n#elif defined(_AIX)\n# define GTEST_OS_AIX 1\n#elif defined(__hpux)\n# define GTEST_OS_HPUX 1\n#elif defined __native_client__\n# define GTEST_OS_NACL 1\n#elif defined __NetBSD__\n# define GTEST_OS_NETBSD 1\n#elif defined __OpenBSD__\n# define GTEST_OS_OPENBSD 1\n#elif defined __QNX__\n# define GTEST_OS_QNX 1\n#endif  // __CYGWIN__\n\n#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_ARCH_H_\n// Copyright 2015, Google Inc.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Injection point for custom user configurations.\n// The following macros can be defined:\n//\n//   Flag related macros:\n//     GTEST_FLAG(flag_name)\n//     GTEST_USE_OWN_FLAGFILE_FLAG_  - Define to 0 when the system provides its\n//                                     own flagfile flag parsing.\n//     GTEST_DECLARE_bool_(name)\n//     GTEST_DECLARE_int32_(name)\n//     GTEST_DECLARE_string_(name)\n//     GTEST_DEFINE_bool_(name, default_val, doc)\n//     GTEST_DEFINE_int32_(name, default_val, doc)\n//     GTEST_DEFINE_string_(name, default_val, doc)\n//\n//   Test filtering:\n//     GTEST_TEST_FILTER_ENV_VAR_ - The name of an environment variable that\n//                                  will be used if --GTEST_FLAG(test_filter)\n//                                  is not provided.\n//\n//   Logging:\n//     GTEST_LOG_(severity)\n//     GTEST_CHECK_(condition)\n//     Functions LogToStderr() and FlushInfoLog() have to be provided too.\n//\n//   Threading:\n//     GTEST_HAS_NOTIFICATION_ - Enabled if Notification is already provided.\n//     GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ - Enabled if Mutex and ThreadLocal are\n//                                         already provided.\n//     Must also provide GTEST_DECLARE_STATIC_MUTEX_(mutex) and\n//     GTEST_DEFINE_STATIC_MUTEX_(mutex)\n//\n//     GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks)\n//     GTEST_LOCK_EXCLUDED_(locks)\n//\n//   Exporting API symbols:\n//     GTEST_API_ - Specifier for exported symbols.\n//\n// ** Custom implementation starts here **\n\n#ifndef GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PORT_H_\n#define GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PORT_H_\n\n#endif  // GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PORT_H_\n\n#if !defined(GTEST_DEV_EMAIL_)\n# define GTEST_DEV_EMAIL_ \"googletestframework@@googlegroups.com\"\n# define GTEST_FLAG_PREFIX_ \"gtest_\"\n# define GTEST_FLAG_PREFIX_DASH_ \"gtest-\"\n# define GTEST_FLAG_PREFIX_UPPER_ \"GTEST_\"\n# define GTEST_NAME_ \"Google Test\"\n# define GTEST_PROJECT_URL_ \"https://github.com/google/googletest/\"\n#endif  // !defined(GTEST_DEV_EMAIL_)\n\n#if !defined(GTEST_INIT_GOOGLE_TEST_NAME_)\n# define GTEST_INIT_GOOGLE_TEST_NAME_ \"testing::InitGoogleTest\"\n#endif  // !defined(GTEST_INIT_GOOGLE_TEST_NAME_)\n\n// Determines the version of gcc that is used to compile this.\n#ifdef __GNUC__\n// 40302 means version 4.3.2.\n# define GTEST_GCC_VER_ \\\n    (__GNUC__*10000 + __GNUC_MINOR__*100 + __GNUC_PATCHLEVEL__)\n#endif  // __GNUC__\n\n// Macros for disabling Microsoft Visual C++ warnings.\n//\n//   GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800 4385)\n//   /* code that triggers warnings C4800 and C4385 */\n//   GTEST_DISABLE_MSC_WARNINGS_POP_()\n#if _MSC_VER >= 1500\n# define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings) \\\n    __pragma(warning(push))                        \\\n    __pragma(warning(disable: warnings))\n# define GTEST_DISABLE_MSC_WARNINGS_POP_()          \\\n    __pragma(warning(pop))\n#else\n// Older versions of MSVC don't have __pragma.\n# define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)\n# define GTEST_DISABLE_MSC_WARNINGS_POP_()\n#endif\n\n#ifndef GTEST_LANG_CXX11\n// gcc and clang define __GXX_EXPERIMENTAL_CXX0X__ when\n// -std={c,gnu}++{0x,11} is passed.  The C++11 standard specifies a\n// value for __cplusplus, and recent versions of clang, gcc, and\n// probably other compilers set that too in C++11 mode.\n# if __GXX_EXPERIMENTAL_CXX0X__ || __cplusplus >= 201103L\n// Compiling in at least C++11 mode.\n#  define GTEST_LANG_CXX11 1\n# else\n#  define GTEST_LANG_CXX11 0\n# endif\n#endif\n\n// Distinct from C++11 language support, some environments don't provide\n// proper C++11 library support. Notably, it's possible to build in\n// C++11 mode when targeting Mac OS X 10.6, which has an old libstdc++\n// with no C++11 support.\n//\n// libstdc++ has sufficient C++11 support as of GCC 4.6.0, __GLIBCXX__\n// 20110325, but maintenance releases in the 4.4 and 4.5 series followed\n// this date, so check for those versions by their date stamps.\n// https://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html#abi.versioning\n#if GTEST_LANG_CXX11 && \\\n    (!defined(__GLIBCXX__) || ( \\\n        __GLIBCXX__ >= 20110325ul &&  /* GCC >= 4.6.0 */ \\\n        /* Blacklist of patch releases of older branches: */ \\\n        __GLIBCXX__ != 20110416ul &&  /* GCC 4.4.6 */ \\\n        __GLIBCXX__ != 20120313ul &&  /* GCC 4.4.7 */ \\\n        __GLIBCXX__ != 20110428ul &&  /* GCC 4.5.3 */ \\\n        __GLIBCXX__ != 20120702ul))   /* GCC 4.5.4 */\n# define GTEST_STDLIB_CXX11 1\n#endif\n\n// Only use C++11 library features if the library provides them.\n#if GTEST_STDLIB_CXX11\n# define GTEST_HAS_STD_BEGIN_AND_END_ 1\n# define GTEST_HAS_STD_FORWARD_LIST_ 1\n# define GTEST_HAS_STD_FUNCTION_ 1\n# define GTEST_HAS_STD_INITIALIZER_LIST_ 1\n# define GTEST_HAS_STD_MOVE_ 1\n# define GTEST_HAS_STD_SHARED_PTR_ 1\n# define GTEST_HAS_STD_TYPE_TRAITS_ 1\n# define GTEST_HAS_STD_UNIQUE_PTR_ 1\n#endif\n\n// C++11 specifies that <tuple> provides std::tuple.\n// Some platforms still might not have it, however.\n#if GTEST_LANG_CXX11\n# define GTEST_HAS_STD_TUPLE_ 1\n# if defined(__clang__)\n// Inspired by http://clang.llvm.org/docs/LanguageExtensions.html#__has_include\n#  if defined(__has_include) && !__has_include(<tuple>)\n#   undef GTEST_HAS_STD_TUPLE_\n#  endif\n# elif defined(_MSC_VER)\n// Inspired by boost/config/stdlib/dinkumware.hpp\n#  if defined(_CPPLIB_VER) && _CPPLIB_VER < 520\n#   undef GTEST_HAS_STD_TUPLE_\n#  endif\n# elif defined(__GLIBCXX__)\n// Inspired by boost/config/stdlib/libstdcpp3.hpp,\n// http://gcc.gnu.org/gcc-4.2/changes.html and\n// http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt01ch01.html#manual.intro.status.standard.200x\n#  if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 2)\n#   undef GTEST_HAS_STD_TUPLE_\n#  endif\n# endif\n#endif\n\n// Brings in definitions for functions used in the testing::internal::posix\n// namespace (read, write, close, chdir, isatty, stat). We do not currently\n// use them on Windows Mobile.\n#if GTEST_OS_WINDOWS\n# if !GTEST_OS_WINDOWS_MOBILE\n#  include <direct.h>\n#  include <io.h>\n# endif\n// In order to avoid having to include <windows.h>, use forward declaration\n#if GTEST_OS_WINDOWS_MINGW && !defined(__MINGW64_VERSION_MAJOR)\n// MinGW defined _CRITICAL_SECTION and _RTL_CRITICAL_SECTION as two\n// separate (equivalent) structs, instead of using typedef\ntypedef struct _CRITICAL_SECTION GTEST_CRITICAL_SECTION;\n#else\n// Assume CRITICAL_SECTION is a typedef of _RTL_CRITICAL_SECTION.\n// This assumption is verified by\n// WindowsTypesTest.CRITICAL_SECTIONIs_RTL_CRITICAL_SECTION.\ntypedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;\n#endif\n#else\n// This assumes that non-Windows OSes provide unistd.h. For OSes where this\n// is not the case, we need to include headers that provide the functions\n// mentioned above.\n# include <unistd.h>\n# include <strings.h>\n#endif  // GTEST_OS_WINDOWS\n\n#if GTEST_OS_LINUX_ANDROID\n// Used to define __ANDROID_API__ matching the target NDK API level.\n#  include <android/api-level.h>  // NOLINT\n#endif\n\n// Defines this to true iff Google Test can use POSIX regular expressions.\n#ifndef GTEST_HAS_POSIX_RE\n# if GTEST_OS_LINUX_ANDROID\n// On Android, <regex.h> is only available starting with Gingerbread.\n#  define GTEST_HAS_POSIX_RE (__ANDROID_API__ >= 9)\n# else\n#  define GTEST_HAS_POSIX_RE (!GTEST_OS_WINDOWS)\n# endif\n#endif\n\n#if GTEST_USES_PCRE\n// The appropriate headers have already been included.\n\n#elif GTEST_HAS_POSIX_RE\n\n// On some platforms, <regex.h> needs someone to define size_t, and\n// won't compile otherwise.  We can #include it here as we already\n// included <stdlib.h>, which is guaranteed to define size_t through\n// <stddef.h>.\n# include <regex.h>  // NOLINT\n\n# define GTEST_USES_POSIX_RE 1\n\n#elif GTEST_OS_WINDOWS\n\n// <regex.h> is not available on Windows.  Use our own simple regex\n// implementation instead.\n# define GTEST_USES_SIMPLE_RE 1\n\n#else\n\n// <regex.h> may not be available on this platform.  Use our own\n// simple regex implementation instead.\n# define GTEST_USES_SIMPLE_RE 1\n\n#endif  // GTEST_USES_PCRE\n\n#ifndef GTEST_HAS_EXCEPTIONS\n// The user didn't tell us whether exceptions are enabled, so we need\n// to figure it out.\n# if defined(_MSC_VER) || defined(__BORLANDC__)\n// MSVC's and C++Builder's implementations of the STL use the _HAS_EXCEPTIONS\n// macro to enable exceptions, so we'll do the same.\n// Assumes that exceptions are enabled by default.\n#  ifndef _HAS_EXCEPTIONS\n#   define _HAS_EXCEPTIONS 1\n#  endif  // _HAS_EXCEPTIONS\n#  define GTEST_HAS_EXCEPTIONS _HAS_EXCEPTIONS\n# elif defined(__clang__)\n// clang defines __EXCEPTIONS iff exceptions are enabled before clang 220714,\n// but iff cleanups are enabled after that. In Obj-C++ files, there can be\n// cleanups for ObjC exceptions which also need cleanups, even if C++ exceptions\n// are disabled. clang has __has_feature(cxx_exceptions) which checks for C++\n// exceptions starting at clang r206352, but which checked for cleanups prior to\n// that. To reliably check for C++ exception availability with clang, check for\n// __EXCEPTIONS && __has_feature(cxx_exceptions).\n#  define GTEST_HAS_EXCEPTIONS (__EXCEPTIONS && __has_feature(cxx_exceptions))\n# elif defined(__GNUC__) && __EXCEPTIONS\n// gcc defines __EXCEPTIONS to 1 iff exceptions are enabled.\n#  define GTEST_HAS_EXCEPTIONS 1\n# elif defined(__SUNPRO_CC)\n// Sun Pro CC supports exceptions.  However, there is no compile-time way of\n// detecting whether they are enabled or not.  Therefore, we assume that\n// they are enabled unless the user tells us otherwise.\n#  define GTEST_HAS_EXCEPTIONS 1\n# elif defined(__IBMCPP__) && __EXCEPTIONS\n// xlC defines __EXCEPTIONS to 1 iff exceptions are enabled.\n#  define GTEST_HAS_EXCEPTIONS 1\n# elif defined(__HP_aCC)\n// Exception handling is in effect by default in HP aCC compiler. It has to\n// be turned of by +noeh compiler option if desired.\n#  define GTEST_HAS_EXCEPTIONS 1\n# else\n// For other compilers, we assume exceptions are disabled to be\n// conservative.\n#  define GTEST_HAS_EXCEPTIONS 0\n# endif  // defined(_MSC_VER) || defined(__BORLANDC__)\n#endif  // GTEST_HAS_EXCEPTIONS\n\n#if !defined(GTEST_HAS_STD_STRING)\n// Even though we don't use this macro any longer, we keep it in case\n// some clients still depend on it.\n# define GTEST_HAS_STD_STRING 1\n#elif !GTEST_HAS_STD_STRING\n// The user told us that ::std::string isn't available.\n# error \"Google Test cannot be used where ::std::string isn't available.\"\n#endif  // !defined(GTEST_HAS_STD_STRING)\n\n#ifndef GTEST_HAS_GLOBAL_STRING\n// The user didn't tell us whether ::string is available, so we need\n// to figure it out.\n\n# define GTEST_HAS_GLOBAL_STRING 0\n\n#endif  // GTEST_HAS_GLOBAL_STRING\n\n#ifndef GTEST_HAS_STD_WSTRING\n// The user didn't tell us whether ::std::wstring is available, so we need\n// to figure it out.\n// TODO(wan@google.com): uses autoconf to detect whether ::std::wstring\n//   is available.\n\n// Cygwin 1.7 and below doesn't support ::std::wstring.\n// Solaris' libc++ doesn't support it either.  Android has\n// no support for it at least as recent as Froyo (2.2).\n# define GTEST_HAS_STD_WSTRING \\\n    (!(GTEST_OS_LINUX_ANDROID || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS))\n\n#endif  // GTEST_HAS_STD_WSTRING\n\n#ifndef GTEST_HAS_GLOBAL_WSTRING\n// The user didn't tell us whether ::wstring is available, so we need\n// to figure it out.\n# define GTEST_HAS_GLOBAL_WSTRING \\\n    (GTEST_HAS_STD_WSTRING && GTEST_HAS_GLOBAL_STRING)\n#endif  // GTEST_HAS_GLOBAL_WSTRING\n\n// Determines whether RTTI is available.\n#ifndef GTEST_HAS_RTTI\n// The user didn't tell us whether RTTI is enabled, so we need to\n// figure it out.\n\n# ifdef _MSC_VER\n\n#  ifdef _CPPRTTI  // MSVC defines this macro iff RTTI is enabled.\n#   define GTEST_HAS_RTTI 1\n#  else\n#   define GTEST_HAS_RTTI 0\n#  endif\n\n// Starting with version 4.3.2, gcc defines __GXX_RTTI iff RTTI is enabled.\n# elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40302)\n\n#  ifdef __GXX_RTTI\n// When building against STLport with the Android NDK and with\n// -frtti -fno-exceptions, the build fails at link time with undefined\n// references to __cxa_bad_typeid. Note sure if STL or toolchain bug,\n// so disable RTTI when detected.\n#   if GTEST_OS_LINUX_ANDROID && defined(_STLPORT_MAJOR) && \\\n       !defined(__EXCEPTIONS)\n#    define GTEST_HAS_RTTI 0\n#   else\n#    define GTEST_HAS_RTTI 1\n#   endif  // GTEST_OS_LINUX_ANDROID && __STLPORT_MAJOR && !__EXCEPTIONS\n#  else\n#   define GTEST_HAS_RTTI 0\n#  endif  // __GXX_RTTI\n\n// Clang defines __GXX_RTTI starting with version 3.0, but its manual recommends\n// using has_feature instead. has_feature(cxx_rtti) is supported since 2.7, the\n// first version with C++ support.\n# elif defined(__clang__)\n\n#  define GTEST_HAS_RTTI __has_feature(cxx_rtti)\n\n// Starting with version 9.0 IBM Visual Age defines __RTTI_ALL__ to 1 if\n// both the typeid and dynamic_cast features are present.\n# elif defined(__IBMCPP__) && (__IBMCPP__ >= 900)\n\n#  ifdef __RTTI_ALL__\n#   define GTEST_HAS_RTTI 1\n#  else\n#   define GTEST_HAS_RTTI 0\n#  endif\n\n# else\n\n// For all other compilers, we assume RTTI is enabled.\n#  define GTEST_HAS_RTTI 1\n\n# endif  // _MSC_VER\n\n#endif  // GTEST_HAS_RTTI\n\n// It's this header's responsibility to #include <typeinfo> when RTTI\n// is enabled.\n#if GTEST_HAS_RTTI\n# include <typeinfo>\n#endif\n\n// Determines whether Google Test can use the pthreads library.\n#ifndef GTEST_HAS_PTHREAD\n// The user didn't tell us explicitly, so we make reasonable assumptions about\n// which platforms have pthreads support.\n//\n// To disable threading support in Google Test, add -DGTEST_HAS_PTHREAD=0\n// to your compiler flags.\n# define GTEST_HAS_PTHREAD (GTEST_OS_LINUX || GTEST_OS_MAC || GTEST_OS_HPUX \\\n    || GTEST_OS_QNX || GTEST_OS_FREEBSD || GTEST_OS_NACL || GTEST_OS_NETBSD)\n#endif  // GTEST_HAS_PTHREAD\n\n#if GTEST_HAS_PTHREAD\n// gtest-port.h guarantees to #include <pthread.h> when GTEST_HAS_PTHREAD is\n// true.\n# include <pthread.h>  // NOLINT\n\n// For timespec and nanosleep, used below.\n# include <time.h>  // NOLINT\n#endif\n\n// Determines if hash_map/hash_set are available.\n// Only used for testing against those containers.\n#if !defined(GTEST_HAS_HASH_MAP_)\n# if defined(_MSC_VER) && (_MSC_VER < 1900)\n#  define GTEST_HAS_HASH_MAP_ 1  // Indicates that hash_map is available.\n#  define GTEST_HAS_HASH_SET_ 1  // Indicates that hash_set is available.\n# endif  // _MSC_VER\n#endif  // !defined(GTEST_HAS_HASH_MAP_)\n\n// Determines whether Google Test can use tr1/tuple.  You can define\n// this macro to 0 to prevent Google Test from using tuple (any\n// feature depending on tuple with be disabled in this mode).\n#ifndef GTEST_HAS_TR1_TUPLE\n# if GTEST_OS_LINUX_ANDROID && defined(_STLPORT_MAJOR)\n// STLport, provided with the Android NDK, has neither <tr1/tuple> or <tuple>.\n#  define GTEST_HAS_TR1_TUPLE 0\n# else\n// The user didn't tell us not to do it, so we assume it's OK.\n#  define GTEST_HAS_TR1_TUPLE 1\n# endif\n#endif  // GTEST_HAS_TR1_TUPLE\n\n// Determines whether Google Test's own tr1 tuple implementation\n// should be used.\n#ifndef GTEST_USE_OWN_TR1_TUPLE\n// The user didn't tell us, so we need to figure it out.\n\n// We use our own TR1 tuple if we aren't sure the user has an\n// implementation of it already.  At this time, libstdc++ 4.0.0+ and\n// MSVC 2010 are the only mainstream standard libraries that come\n// with a TR1 tuple implementation.  NVIDIA's CUDA NVCC compiler\n// pretends to be GCC by defining __GNUC__ and friends, but cannot\n// compile GCC's tuple implementation.  MSVC 2008 (9.0) provides TR1\n// tuple in a 323 MB Feature Pack download, which we cannot assume the\n// user has.  QNX's QCC compiler is a modified GCC but it doesn't\n// support TR1 tuple.  libc++ only provides std::tuple, in C++11 mode,\n// and it can be used with some compilers that define __GNUC__.\n# if (defined(__GNUC__) && !defined(__CUDACC__) && (GTEST_GCC_VER_ >= 40000) \\\n      && !GTEST_OS_QNX && !defined(_LIBCPP_VERSION)) || _MSC_VER >= 1600\n#  define GTEST_ENV_HAS_TR1_TUPLE_ 1\n# endif\n\n// C++11 specifies that <tuple> provides std::tuple. Use that if gtest is used\n// in C++11 mode and libstdc++ isn't very old (binaries targeting OS X 10.6\n// can build with clang but need to use gcc4.2's libstdc++).\n# if GTEST_LANG_CXX11 && (!defined(__GLIBCXX__) || __GLIBCXX__ > 20110325)\n#  define GTEST_ENV_HAS_STD_TUPLE_ 1\n# endif\n\n# if GTEST_ENV_HAS_TR1_TUPLE_ || GTEST_ENV_HAS_STD_TUPLE_\n#  define GTEST_USE_OWN_TR1_TUPLE 0\n# else\n#  define GTEST_USE_OWN_TR1_TUPLE 1\n# endif\n\n#endif  // GTEST_USE_OWN_TR1_TUPLE\n\n// To avoid conditional compilation everywhere, we make it\n// gtest-port.h's responsibility to #include the header implementing\n// tuple.\n#if GTEST_HAS_STD_TUPLE_\n# include <tuple>  // IWYU pragma: export\n# define GTEST_TUPLE_NAMESPACE_ ::std\n#endif  // GTEST_HAS_STD_TUPLE_\n\n// We include tr1::tuple even if std::tuple is available to define printers for\n// them.\n#if GTEST_HAS_TR1_TUPLE\n# ifndef GTEST_TUPLE_NAMESPACE_\n#  define GTEST_TUPLE_NAMESPACE_ ::std::tr1\n# endif  // GTEST_TUPLE_NAMESPACE_\n\n# if GTEST_USE_OWN_TR1_TUPLE\n// This file was GENERATED by command:\n//     pump.py gtest-tuple.h.pump\n// DO NOT EDIT BY HAND!!!\n\n// Copyright 2009 Google Inc.\n// All Rights Reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Author: wan@google.com (Zhanyong Wan)\n\n// Implements a subset of TR1 tuple needed by Google Test and Google Mock.\n\n#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TUPLE_H_\n#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TUPLE_H_\n\n#include <utility>  // For ::std::pair.\n\n// The compiler used in Symbian has a bug that prevents us from declaring the\n// tuple template as a friend (it complains that tuple is redefined).  This\n// hack bypasses the bug by declaring the members that should otherwise be\n// private as public.\n// Sun Studio versions < 12 also have the above bug.\n#if defined(__SYMBIAN32__) || (defined(__SUNPRO_CC) && __SUNPRO_CC < 0x590)\n# define GTEST_DECLARE_TUPLE_AS_FRIEND_ public:\n#else\n# define GTEST_DECLARE_TUPLE_AS_FRIEND_ \\\n    template <GTEST_10_TYPENAMES_(U)> friend class tuple; \\\n   private:\n#endif\n\n// Visual Studio 2010, 2012, and 2013 define symbols in std::tr1 that conflict\n// with our own definitions. Therefore using our own tuple does not work on\n// those compilers.\n#if defined(_MSC_VER) && _MSC_VER >= 1600  /* 1600 is Visual Studio 2010 */\n# error \"gtest's tuple doesn't compile on Visual Studio 2010 or later. \\\nGTEST_USE_OWN_TR1_TUPLE must be set to 0 on those compilers.\"\n#endif\n\n// GTEST_n_TUPLE_(T) is the type of an n-tuple.\n#define GTEST_0_TUPLE_(T) tuple<>\n#define GTEST_1_TUPLE_(T) tuple<T##0, void, void, void, void, void, void, \\\n    void, void, void>\n#define GTEST_2_TUPLE_(T) tuple<T##0, T##1, void, void, void, void, void, \\\n    void, void, void>\n#define GTEST_3_TUPLE_(T) tuple<T##0, T##1, T##2, void, void, void, void, \\\n    void, void, void>\n#define GTEST_4_TUPLE_(T) tuple<T##0, T##1, T##2, T##3, void, void, void, \\\n    void, void, void>\n#define GTEST_5_TUPLE_(T) tuple<T##0, T##1, T##2, T##3, T##4, void, void, \\\n    void, void, void>\n#define GTEST_6_TUPLE_(T) tuple<T##0, T##1, T##2, T##3, T##4, T##5, void, \\\n    void, void, void>\n#define GTEST_7_TUPLE_(T) tuple<T##0, T##1, T##2, T##3, T##4, T##5, T##6, \\\n    void, void, void>\n#define GTEST_8_TUPLE_(T) tuple<T##0, T##1, T##2, T##3, T##4, T##5, T##6, \\\n    T##7, void, void>\n#define GTEST_9_TUPLE_(T) tuple<T##0, T##1, T##2, T##3, T##4, T##5, T##6, \\\n    T##7, T##8, void>\n#define GTEST_10_TUPLE_(T) tuple<T##0, T##1, T##2, T##3, T##4, T##5, T##6, \\\n    T##7, T##8, T##9>\n\n// GTEST_n_TYPENAMES_(T) declares a list of n typenames.\n#define GTEST_0_TYPENAMES_(T)\n#define GTEST_1_TYPENAMES_(T) typename T##0\n#define GTEST_2_TYPENAMES_(T) typename T##0, typename T##1\n#define GTEST_3_TYPENAMES_(T) typename T##0, typename T##1, typename T##2\n#define GTEST_4_TYPENAMES_(T) typename T##0, typename T##1, typename T##2, \\\n    typename T##3\n#define GTEST_5_TYPENAMES_(T) typename T##0, typename T##1, typename T##2, \\\n    typename T##3, typename T##4\n#define GTEST_6_TYPENAMES_(T) typename T##0, typename T##1, typename T##2, \\\n    typename T##3, typename T##4, typename T##5\n#define GTEST_7_TYPENAMES_(T) typename T##0, typename T##1, typename T##2, \\\n    typename T##3, typename T##4, typename T##5, typename T##6\n#define GTEST_8_TYPENAMES_(T) typename T##0, typename T##1, typename T##2, \\\n    typename T##3, typename T##4, typename T##5, typename T##6, typename T##7\n#define GTEST_9_TYPENAMES_(T) typename T##0, typename T##1, typename T##2, \\\n    typename T##3, typename T##4, typename T##5, typename T##6, \\\n    typename T##7, typename T##8\n#define GTEST_10_TYPENAMES_(T) typename T##0, typename T##1, typename T##2, \\\n    typename T##3, typename T##4, typename T##5, typename T##6, \\\n    typename T##7, typename T##8, typename T##9\n\n// In theory, defining stuff in the ::std namespace is undefined\n// behavior.  We can do this as we are playing the role of a standard\n// library vendor.\nnamespace std\n{\n    namespace tr1\n    {\n\n        template <typename T0 = void, typename T1 = void, typename T2 = void,\n                  typename T3 = void, typename T4 = void, typename T5 = void,\n                  typename T6 = void, typename T7 = void, typename T8 = void,\n                  typename T9 = void>\n        class tuple;\n\n// Anything in namespace gtest_internal is Google Test's INTERNAL\n// IMPLEMENTATION DETAIL and MUST NOT BE USED DIRECTLY in user code.\n        namespace gtest_internal\n        {\n\n// ByRef<T>::type is T if T is a reference; otherwise it's const T&.\n            template <typename T>\n            struct ByRef {\n                typedef const T &type;\n            };  // NOLINT\n            template <typename T>\n            struct ByRef<T &> {\n                typedef T &type;\n            };  // NOLINT\n\n// A handy wrapper for ByRef.\n#define GTEST_BY_REF_(T) typename ::std::tr1::gtest_internal::ByRef<T>::type\n\n// AddRef<T>::type is T if T is a reference; otherwise it's T&.  This\n// is the same as tr1::add_reference<T>::type.\n            template <typename T>\n            struct AddRef {\n                typedef T &type;\n            };  // NOLINT\n            template <typename T>\n            struct AddRef<T &> {\n                typedef T &type;\n            };  // NOLINT\n\n// A handy wrapper for AddRef.\n#define GTEST_ADD_REF_(T) typename ::std::tr1::gtest_internal::AddRef<T>::type\n\n// A helper for implementing get<k>().\n            template <int k> class Get;\n\n// A helper for implementing tuple_element<k, T>.  kIndexValid is true\n// iff k < the number of fields in tuple type T.\n            template <bool kIndexValid, int kIndex, class Tuple>\n            struct TupleElement;\n\n            template <GTEST_10_TYPENAMES_(T)>\n            struct TupleElement<true, 0, GTEST_10_TUPLE_(T) > {\n                typedef T0 type;\n            };\n\n            template <GTEST_10_TYPENAMES_(T)>\n            struct TupleElement<true, 1, GTEST_10_TUPLE_(T) > {\n                typedef T1 type;\n            };\n\n            template <GTEST_10_TYPENAMES_(T)>\n            struct TupleElement<true, 2, GTEST_10_TUPLE_(T) > {\n                typedef T2 type;\n            };\n\n            template <GTEST_10_TYPENAMES_(T)>\n            struct TupleElement<true, 3, GTEST_10_TUPLE_(T) > {\n                typedef T3 type;\n            };\n\n            template <GTEST_10_TYPENAMES_(T)>\n            struct TupleElement<true, 4, GTEST_10_TUPLE_(T) > {\n                typedef T4 type;\n            };\n\n            template <GTEST_10_TYPENAMES_(T)>\n            struct TupleElement<true, 5, GTEST_10_TUPLE_(T) > {\n                typedef T5 type;\n            };\n\n            template <GTEST_10_TYPENAMES_(T)>\n            struct TupleElement<true, 6, GTEST_10_TUPLE_(T) > {\n                typedef T6 type;\n            };\n\n            template <GTEST_10_TYPENAMES_(T)>\n            struct TupleElement<true, 7, GTEST_10_TUPLE_(T) > {\n                typedef T7 type;\n            };\n\n            template <GTEST_10_TYPENAMES_(T)>\n            struct TupleElement<true, 8, GTEST_10_TUPLE_(T) > {\n                typedef T8 type;\n            };\n\n            template <GTEST_10_TYPENAMES_(T)>\n            struct TupleElement<true, 9, GTEST_10_TUPLE_(T) > {\n                typedef T9 type;\n            };\n\n        }  // namespace gtest_internal\n\n        template <>\n        class tuple<>\n        {\n        public:\n            tuple() {}\n            tuple(const tuple & /* t */)  {}\n            tuple &operator=(const tuple & /* t */)\n            {\n                return *this;\n            }\n        };\n\n        template <GTEST_1_TYPENAMES_(T)>\n        class GTEST_1_TUPLE_(T)\n        {\n        public:\n            template <int k> friend class gtest_internal::Get;\n\n            tuple() : f0_() {}\n\n            explicit tuple(GTEST_BY_REF_(T0) f0) : f0_(f0) {}\n\n            tuple(const tuple& t) : f0_(t.f0_) {}\n\n            template <GTEST_1_TYPENAMES_(U)>\n            tuple(const GTEST_1_TUPLE_(U)& t) : f0_(t.f0_) {}\n\n            tuple &operator=(const tuple& t) {\n                return CopyFrom(t);\n            }\n\n            template <GTEST_1_TYPENAMES_(U)>\n            tuple &operator=(const GTEST_1_TUPLE_(U)& t) {\n                return CopyFrom(t);\n            }\n\n            GTEST_DECLARE_TUPLE_AS_FRIEND_\n\n            template <GTEST_1_TYPENAMES_(U)>\n            tuple &CopyFrom(const GTEST_1_TUPLE_(U)& t) {\n                f0_ = t.f0_;\n                return *this;\n            }\n\n            T0 f0_;\n        };\n\n        template <GTEST_2_TYPENAMES_(T)>\n        class GTEST_2_TUPLE_(T)\n        {\n        public:\n            template <int k> friend class gtest_internal::Get;\n\n            tuple() : f0_(), f1_() {}\n\n            explicit tuple(GTEST_BY_REF_(T0) f0, GTEST_BY_REF_(T1) f1) : f0_(f0),\n                f1_(f1) {}\n\n            tuple(const tuple& t) : f0_(t.f0_), f1_(t.f1_) {}\n\n            template <GTEST_2_TYPENAMES_(U)>\n            tuple(const GTEST_2_TUPLE_(U)& t) : f0_(t.f0_), f1_(t.f1_) {}\n            template <typename U0, typename U1>\n            tuple(const ::std::pair<U0, U1> &p) : f0_(p.first), f1_(p.second) {}\n\n            tuple &operator=(const tuple& t) {\n                return CopyFrom(t);\n            }\n\n            template <GTEST_2_TYPENAMES_(U)>\n            tuple &operator=(const GTEST_2_TUPLE_(U)& t) {\n                return CopyFrom(t);\n            }\n            template <typename U0, typename U1>\n            tuple &operator=(const ::std::pair<U0, U1> &p) {\n                f0_ = p.first;\n                f1_ = p.second;\n                return *this;\n            }\n\n            GTEST_DECLARE_TUPLE_AS_FRIEND_\n\n            template <GTEST_2_TYPENAMES_(U)>\n            tuple &CopyFrom(const GTEST_2_TUPLE_(U)& t) {\n                f0_ = t.f0_;\n                f1_ = t.f1_;\n                return *this;\n            }\n\n            T0 f0_;\n            T1 f1_;\n        };\n\n        template <GTEST_3_TYPENAMES_(T)>\n        class GTEST_3_TUPLE_(T)\n        {\n        public:\n            template <int k> friend class gtest_internal::Get;\n\n            tuple() : f0_(), f1_(), f2_() {}\n\n            explicit tuple(GTEST_BY_REF_(T0) f0, GTEST_BY_REF_(T1) f1,\n                           GTEST_BY_REF_(T2) f2) : f0_(f0), f1_(f1), f2_(f2) {}\n\n            tuple(const tuple& t) : f0_(t.f0_), f1_(t.f1_), f2_(t.f2_) {}\n\n            template <GTEST_3_TYPENAMES_(U)>\n            tuple(const GTEST_3_TUPLE_(U)& t) : f0_(t.f0_), f1_(t.f1_), f2_(t.f2_) {}\n\n            tuple &operator=(const tuple& t) {\n                return CopyFrom(t);\n            }\n\n            template <GTEST_3_TYPENAMES_(U)>\n            tuple &operator=(const GTEST_3_TUPLE_(U)& t) {\n                return CopyFrom(t);\n            }\n\n            GTEST_DECLARE_TUPLE_AS_FRIEND_\n\n            template <GTEST_3_TYPENAMES_(U)>\n            tuple &CopyFrom(const GTEST_3_TUPLE_(U)& t) {\n                f0_ = t.f0_;\n                f1_ = t.f1_;\n                f2_ = t.f2_;\n                return *this;\n            }\n\n            T0 f0_;\n            T1 f1_;\n            T2 f2_;\n        };\n\n        template <GTEST_4_TYPENAMES_(T)>\n        class GTEST_4_TUPLE_(T)\n        {\n        public:\n            template <int k> friend class gtest_internal::Get;\n\n            tuple() : f0_(), f1_(), f2_(), f3_() {}\n\n            explicit tuple(GTEST_BY_REF_(T0) f0, GTEST_BY_REF_(T1) f1,\n                           GTEST_BY_REF_(T2) f2, GTEST_BY_REF_(T3) f3) : f0_(f0), f1_(f1), f2_(f2),\n                f3_(f3) {}\n\n            tuple(const tuple& t) : f0_(t.f0_), f1_(t.f1_), f2_(t.f2_), f3_(t.f3_) {}\n\n            template <GTEST_4_TYPENAMES_(U)>\n            tuple(const GTEST_4_TUPLE_(U)& t) : f0_(t.f0_), f1_(t.f1_), f2_(t.f2_),\n                f3_(t.f3_) {}\n\n            tuple &operator=(const tuple& t) {\n                return CopyFrom(t);\n            }\n\n            template <GTEST_4_TYPENAMES_(U)>\n            tuple &operator=(const GTEST_4_TUPLE_(U)& t) {\n                return CopyFrom(t);\n            }\n\n            GTEST_DECLARE_TUPLE_AS_FRIEND_\n\n            template <GTEST_4_TYPENAMES_(U)>\n            tuple &CopyFrom(const GTEST_4_TUPLE_(U)& t) {\n                f0_ = t.f0_;\n                f1_ = t.f1_;\n                f2_ = t.f2_;\n                f3_ = t.f3_;\n                return *this;\n            }\n\n            T0 f0_;\n            T1 f1_;\n            T2 f2_;\n            T3 f3_;\n        };\n\n        template <GTEST_5_TYPENAMES_(T)>\n        class GTEST_5_TUPLE_(T)\n        {\n        public:\n            template <int k> friend class gtest_internal::Get;\n\n            tuple() : f0_(), f1_(), f2_(), f3_(), f4_() {}\n\n            explicit tuple(GTEST_BY_REF_(T0) f0, GTEST_BY_REF_(T1) f1,\n                           GTEST_BY_REF_(T2) f2, GTEST_BY_REF_(T3) f3,\n                           GTEST_BY_REF_(T4) f4) : f0_(f0), f1_(f1), f2_(f2), f3_(f3), f4_(f4) {}\n\n            tuple(const tuple& t) : f0_(t.f0_), f1_(t.f1_), f2_(t.f2_), f3_(t.f3_),\n                f4_(t.f4_) {}\n\n            template <GTEST_5_TYPENAMES_(U)>\n            tuple(const GTEST_5_TUPLE_(U)& t) : f0_(t.f0_), f1_(t.f1_), f2_(t.f2_),\n                f3_(t.f3_), f4_(t.f4_) {}\n\n            tuple &operator=(const tuple& t) {\n                return CopyFrom(t);\n            }\n\n            template <GTEST_5_TYPENAMES_(U)>\n            tuple &operator=(const GTEST_5_TUPLE_(U)& t) {\n                return CopyFrom(t);\n            }\n\n            GTEST_DECLARE_TUPLE_AS_FRIEND_\n\n            template <GTEST_5_TYPENAMES_(U)>\n            tuple &CopyFrom(const GTEST_5_TUPLE_(U)& t) {\n                f0_ = t.f0_;\n                f1_ = t.f1_;\n                f2_ = t.f2_;\n                f3_ = t.f3_;\n                f4_ = t.f4_;\n                return *this;\n            }\n\n            T0 f0_;\n            T1 f1_;\n            T2 f2_;\n            T3 f3_;\n            T4 f4_;\n        };\n\n        template <GTEST_6_TYPENAMES_(T)>\n        class GTEST_6_TUPLE_(T)\n        {\n        public:\n            template <int k> friend class gtest_internal::Get;\n\n            tuple() : f0_(), f1_(), f2_(), f3_(), f4_(), f5_() {}\n\n            explicit tuple(GTEST_BY_REF_(T0) f0, GTEST_BY_REF_(T1) f1,\n                           GTEST_BY_REF_(T2) f2, GTEST_BY_REF_(T3) f3, GTEST_BY_REF_(T4) f4,\n                           GTEST_BY_REF_(T5) f5) : f0_(f0), f1_(f1), f2_(f2), f3_(f3), f4_(f4),\n                f5_(f5) {}\n\n            tuple(const tuple& t) : f0_(t.f0_), f1_(t.f1_), f2_(t.f2_), f3_(t.f3_),\n                f4_(t.f4_), f5_(t.f5_) {}\n\n            template <GTEST_6_TYPENAMES_(U)>\n            tuple(const GTEST_6_TUPLE_(U)& t) : f0_(t.f0_), f1_(t.f1_), f2_(t.f2_),\n                f3_(t.f3_), f4_(t.f4_), f5_(t.f5_) {}\n\n            tuple &operator=(const tuple& t) {\n                return CopyFrom(t);\n            }\n\n            template <GTEST_6_TYPENAMES_(U)>\n            tuple &operator=(const GTEST_6_TUPLE_(U)& t) {\n                return CopyFrom(t);\n            }\n\n            GTEST_DECLARE_TUPLE_AS_FRIEND_\n\n            template <GTEST_6_TYPENAMES_(U)>\n            tuple &CopyFrom(const GTEST_6_TUPLE_(U)& t) {\n                f0_ = t.f0_;\n                f1_ = t.f1_;\n                f2_ = t.f2_;\n                f3_ = t.f3_;\n                f4_ = t.f4_;\n                f5_ = t.f5_;\n                return *this;\n            }\n\n            T0 f0_;\n            T1 f1_;\n            T2 f2_;\n            T3 f3_;\n            T4 f4_;\n            T5 f5_;\n        };\n\n        template <GTEST_7_TYPENAMES_(T)>\n        class GTEST_7_TUPLE_(T)\n        {\n        public:\n            template <int k> friend class gtest_internal::Get;\n\n            tuple() : f0_(), f1_(), f2_(), f3_(), f4_(), f5_(), f6_() {}\n\n            explicit tuple(GTEST_BY_REF_(T0) f0, GTEST_BY_REF_(T1) f1,\n                           GTEST_BY_REF_(T2) f2, GTEST_BY_REF_(T3) f3, GTEST_BY_REF_(T4) f4,\n                           GTEST_BY_REF_(T5) f5, GTEST_BY_REF_(T6) f6) : f0_(f0), f1_(f1), f2_(f2),\n                f3_(f3), f4_(f4), f5_(f5), f6_(f6) {}\n\n            tuple(const tuple& t) : f0_(t.f0_), f1_(t.f1_), f2_(t.f2_), f3_(t.f3_),\n                f4_(t.f4_), f5_(t.f5_), f6_(t.f6_) {}\n\n            template <GTEST_7_TYPENAMES_(U)>\n            tuple(const GTEST_7_TUPLE_(U)& t) : f0_(t.f0_), f1_(t.f1_), f2_(t.f2_),\n                f3_(t.f3_), f4_(t.f4_), f5_(t.f5_), f6_(t.f6_) {}\n\n            tuple &operator=(const tuple& t) {\n                return CopyFrom(t);\n            }\n\n            template <GTEST_7_TYPENAMES_(U)>\n            tuple &operator=(const GTEST_7_TUPLE_(U)& t) {\n                return CopyFrom(t);\n            }\n\n            GTEST_DECLARE_TUPLE_AS_FRIEND_\n\n            template <GTEST_7_TYPENAMES_(U)>\n            tuple &CopyFrom(const GTEST_7_TUPLE_(U)& t) {\n                f0_ = t.f0_;\n                f1_ = t.f1_;\n                f2_ = t.f2_;\n                f3_ = t.f3_;\n                f4_ = t.f4_;\n                f5_ = t.f5_;\n                f6_ = t.f6_;\n                return *this;\n            }\n\n            T0 f0_;\n            T1 f1_;\n            T2 f2_;\n            T3 f3_;\n            T4 f4_;\n            T5 f5_;\n            T6 f6_;\n        };\n\n        template <GTEST_8_TYPENAMES_(T)>\n        class GTEST_8_TUPLE_(T)\n        {\n        public:\n            template <int k> friend class gtest_internal::Get;\n\n            tuple() : f0_(), f1_(), f2_(), f3_(), f4_(), f5_(), f6_(), f7_() {}\n\n            explicit tuple(GTEST_BY_REF_(T0) f0, GTEST_BY_REF_(T1) f1,\n                           GTEST_BY_REF_(T2) f2, GTEST_BY_REF_(T3) f3, GTEST_BY_REF_(T4) f4,\n                           GTEST_BY_REF_(T5) f5, GTEST_BY_REF_(T6) f6,\n                           GTEST_BY_REF_(T7) f7) : f0_(f0), f1_(f1), f2_(f2), f3_(f3), f4_(f4),\n                f5_(f5), f6_(f6), f7_(f7) {}\n\n            tuple(const tuple& t) : f0_(t.f0_), f1_(t.f1_), f2_(t.f2_), f3_(t.f3_),\n                f4_(t.f4_), f5_(t.f5_), f6_(t.f6_), f7_(t.f7_) {}\n\n            template <GTEST_8_TYPENAMES_(U)>\n            tuple(const GTEST_8_TUPLE_(U)& t) : f0_(t.f0_), f1_(t.f1_), f2_(t.f2_),\n                f3_(t.f3_), f4_(t.f4_), f5_(t.f5_), f6_(t.f6_), f7_(t.f7_) {}\n\n            tuple &operator=(const tuple& t) {\n                return CopyFrom(t);\n            }\n\n            template <GTEST_8_TYPENAMES_(U)>\n            tuple &operator=(const GTEST_8_TUPLE_(U)& t) {\n                return CopyFrom(t);\n            }\n\n            GTEST_DECLARE_TUPLE_AS_FRIEND_\n\n            template <GTEST_8_TYPENAMES_(U)>\n            tuple &CopyFrom(const GTEST_8_TUPLE_(U)& t) {\n                f0_ = t.f0_;\n                f1_ = t.f1_;\n                f2_ = t.f2_;\n                f3_ = t.f3_;\n                f4_ = t.f4_;\n                f5_ = t.f5_;\n                f6_ = t.f6_;\n                f7_ = t.f7_;\n                return *this;\n            }\n\n            T0 f0_;\n            T1 f1_;\n            T2 f2_;\n            T3 f3_;\n            T4 f4_;\n            T5 f5_;\n            T6 f6_;\n            T7 f7_;\n        };\n\n        template <GTEST_9_TYPENAMES_(T)>\n        class GTEST_9_TUPLE_(T)\n        {\n        public:\n            template <int k> friend class gtest_internal::Get;\n\n            tuple() : f0_(), f1_(), f2_(), f3_(), f4_(), f5_(), f6_(), f7_(), f8_() {}\n\n            explicit tuple(GTEST_BY_REF_(T0) f0, GTEST_BY_REF_(T1) f1,\n                           GTEST_BY_REF_(T2) f2, GTEST_BY_REF_(T3) f3, GTEST_BY_REF_(T4) f4,\n                           GTEST_BY_REF_(T5) f5, GTEST_BY_REF_(T6) f6, GTEST_BY_REF_(T7) f7,\n                           GTEST_BY_REF_(T8) f8) : f0_(f0), f1_(f1), f2_(f2), f3_(f3), f4_(f4),\n                f5_(f5), f6_(f6), f7_(f7), f8_(f8) {}\n\n            tuple(const tuple& t) : f0_(t.f0_), f1_(t.f1_), f2_(t.f2_), f3_(t.f3_),\n                f4_(t.f4_), f5_(t.f5_), f6_(t.f6_), f7_(t.f7_), f8_(t.f8_) {}\n\n            template <GTEST_9_TYPENAMES_(U)>\n            tuple(const GTEST_9_TUPLE_(U)& t) : f0_(t.f0_), f1_(t.f1_), f2_(t.f2_),\n                f3_(t.f3_), f4_(t.f4_), f5_(t.f5_), f6_(t.f6_), f7_(t.f7_), f8_(t.f8_) {}\n\n            tuple &operator=(const tuple& t) {\n                return CopyFrom(t);\n            }\n\n            template <GTEST_9_TYPENAMES_(U)>\n            tuple &operator=(const GTEST_9_TUPLE_(U)& t) {\n                return CopyFrom(t);\n            }\n\n            GTEST_DECLARE_TUPLE_AS_FRIEND_\n\n            template <GTEST_9_TYPENAMES_(U)>\n            tuple &CopyFrom(const GTEST_9_TUPLE_(U)& t) {\n                f0_ = t.f0_;\n                f1_ = t.f1_;\n                f2_ = t.f2_;\n                f3_ = t.f3_;\n                f4_ = t.f4_;\n                f5_ = t.f5_;\n                f6_ = t.f6_;\n                f7_ = t.f7_;\n                f8_ = t.f8_;\n                return *this;\n            }\n\n            T0 f0_;\n            T1 f1_;\n            T2 f2_;\n            T3 f3_;\n            T4 f4_;\n            T5 f5_;\n            T6 f6_;\n            T7 f7_;\n            T8 f8_;\n        };\n\n        template <GTEST_10_TYPENAMES_(T)>\n        class tuple\n        {\n        public:\n            template <int k> friend class gtest_internal::Get;\n\n            tuple() : f0_(), f1_(), f2_(), f3_(), f4_(), f5_(), f6_(), f7_(), f8_(),\n                f9_() {}\n\n            explicit tuple(GTEST_BY_REF_(T0) f0, GTEST_BY_REF_(T1) f1,\n                           GTEST_BY_REF_(T2) f2, GTEST_BY_REF_(T3) f3, GTEST_BY_REF_(T4) f4,\n                           GTEST_BY_REF_(T5) f5, GTEST_BY_REF_(T6) f6, GTEST_BY_REF_(T7) f7,\n                           GTEST_BY_REF_(T8) f8, GTEST_BY_REF_(T9) f9) : f0_(f0), f1_(f1), f2_(f2),\n                f3_(f3), f4_(f4), f5_(f5), f6_(f6), f7_(f7), f8_(f8), f9_(f9) {}\n\n            tuple(const tuple &t) : f0_(t.f0_), f1_(t.f1_), f2_(t.f2_), f3_(t.f3_),\n                f4_(t.f4_), f5_(t.f5_), f6_(t.f6_), f7_(t.f7_), f8_(t.f8_), f9_(t.f9_) {}\n\n            template <GTEST_10_TYPENAMES_(U)>\n            tuple(const GTEST_10_TUPLE_(U)& t) : f0_(t.f0_), f1_(t.f1_), f2_(t.f2_),\n                f3_(t.f3_), f4_(t.f4_), f5_(t.f5_), f6_(t.f6_), f7_(t.f7_), f8_(t.f8_),\n                f9_(t.f9_) {}\n\n            tuple &operator=(const tuple &t)\n            {\n                return CopyFrom(t);\n            }\n\n            template <GTEST_10_TYPENAMES_(U)>\n            tuple &operator=(const GTEST_10_TUPLE_(U)& t)\n            {\n                return CopyFrom(t);\n            }\n\n            GTEST_DECLARE_TUPLE_AS_FRIEND_\n\n            template <GTEST_10_TYPENAMES_(U)>\n            tuple &CopyFrom(const GTEST_10_TUPLE_(U)& t)\n            {\n                f0_ = t.f0_;\n                f1_ = t.f1_;\n                f2_ = t.f2_;\n                f3_ = t.f3_;\n                f4_ = t.f4_;\n                f5_ = t.f5_;\n                f6_ = t.f6_;\n                f7_ = t.f7_;\n                f8_ = t.f8_;\n                f9_ = t.f9_;\n                return *this;\n            }\n\n            T0 f0_;\n            T1 f1_;\n            T2 f2_;\n            T3 f3_;\n            T4 f4_;\n            T5 f5_;\n            T6 f6_;\n            T7 f7_;\n            T8 f8_;\n            T9 f9_;\n        };\n\n// 6.1.3.2 Tuple creation functions.\n\n// Known limitations: we don't support passing an\n// std::tr1::reference_wrapper<T> to make_tuple().  And we don't\n// implement tie().\n\n        inline tuple<> make_tuple()\n        {\n            return tuple<>();\n        }\n\n        template <GTEST_1_TYPENAMES_(T)>\n        inline GTEST_1_TUPLE_(T) make_tuple(const T0 &f0)\n        {\n            return GTEST_1_TUPLE_(T)(f0);\n        }\n\n        template <GTEST_2_TYPENAMES_(T)>\n        inline GTEST_2_TUPLE_(T) make_tuple(const T0 &f0, const T1 &f1)\n        {\n            return GTEST_2_TUPLE_(T)(f0, f1);\n        }\n\n        template <GTEST_3_TYPENAMES_(T)>\n        inline GTEST_3_TUPLE_(T) make_tuple(const T0 &f0, const T1 &f1, const T2 &f2)\n        {\n            return GTEST_3_TUPLE_(T)(f0, f1, f2);\n        }\n\n        template <GTEST_4_TYPENAMES_(T)>\n        inline GTEST_4_TUPLE_(T) make_tuple(const T0 &f0, const T1 &f1, const T2 &f2,\n                                            const T3 &f3)\n        {\n            return GTEST_4_TUPLE_(T)(f0, f1, f2, f3);\n        }\n\n        template <GTEST_5_TYPENAMES_(T)>\n        inline GTEST_5_TUPLE_(T) make_tuple(const T0 &f0, const T1 &f1, const T2 &f2,\n                                            const T3 &f3, const T4 &f4)\n        {\n            return GTEST_5_TUPLE_(T)(f0, f1, f2, f3, f4);\n        }\n\n        template <GTEST_6_TYPENAMES_(T)>\n        inline GTEST_6_TUPLE_(T) make_tuple(const T0 &f0, const T1 &f1, const T2 &f2,\n                                            const T3 &f3, const T4 &f4, const T5 &f5)\n        {\n            return GTEST_6_TUPLE_(T)(f0, f1, f2, f3, f4, f5);\n        }\n\n        template <GTEST_7_TYPENAMES_(T)>\n        inline GTEST_7_TUPLE_(T) make_tuple(const T0 &f0, const T1 &f1, const T2 &f2,\n                                            const T3 &f3, const T4 &f4, const T5 &f5, const T6 &f6)\n        {\n            return GTEST_7_TUPLE_(T)(f0, f1, f2, f3, f4, f5, f6);\n        }\n\n        template <GTEST_8_TYPENAMES_(T)>\n        inline GTEST_8_TUPLE_(T) make_tuple(const T0 &f0, const T1 &f1, const T2 &f2,\n                                            const T3 &f3, const T4 &f4, const T5 &f5, const T6 &f6, const T7 &f7)\n        {\n            return GTEST_8_TUPLE_(T)(f0, f1, f2, f3, f4, f5, f6, f7);\n        }\n\n        template <GTEST_9_TYPENAMES_(T)>\n        inline GTEST_9_TUPLE_(T) make_tuple(const T0 &f0, const T1 &f1, const T2 &f2,\n                                            const T3 &f3, const T4 &f4, const T5 &f5, const T6 &f6, const T7 &f7,\n                                            const T8 &f8)\n        {\n            return GTEST_9_TUPLE_(T)(f0, f1, f2, f3, f4, f5, f6, f7, f8);\n        }\n\n        template <GTEST_10_TYPENAMES_(T)>\n        inline GTEST_10_TUPLE_(T) make_tuple(const T0 &f0, const T1 &f1, const T2 &f2,\n                                             const T3 &f3, const T4 &f4, const T5 &f5, const T6 &f6, const T7 &f7,\n                                             const T8 &f8, const T9 &f9)\n        {\n            return GTEST_10_TUPLE_(T)(f0, f1, f2, f3, f4, f5, f6, f7, f8, f9);\n        }\n\n// 6.1.3.3 Tuple helper classes.\n\n        template <typename Tuple> struct tuple_size;\n\n        template <GTEST_0_TYPENAMES_(T)>\n        struct tuple_size<GTEST_0_TUPLE_(T) > {\n            static const int value = 0;\n        };\n\n        template <GTEST_1_TYPENAMES_(T)>\n        struct tuple_size<GTEST_1_TUPLE_(T) > {\n            static const int value = 1;\n        };\n\n        template <GTEST_2_TYPENAMES_(T)>\n        struct tuple_size<GTEST_2_TUPLE_(T) > {\n            static const int value = 2;\n        };\n\n        template <GTEST_3_TYPENAMES_(T)>\n        struct tuple_size<GTEST_3_TUPLE_(T) > {\n            static const int value = 3;\n        };\n\n        template <GTEST_4_TYPENAMES_(T)>\n        struct tuple_size<GTEST_4_TUPLE_(T) > {\n            static const int value = 4;\n        };\n\n        template <GTEST_5_TYPENAMES_(T)>\n        struct tuple_size<GTEST_5_TUPLE_(T) > {\n            static const int value = 5;\n        };\n\n        template <GTEST_6_TYPENAMES_(T)>\n        struct tuple_size<GTEST_6_TUPLE_(T) > {\n            static const int value = 6;\n        };\n\n        template <GTEST_7_TYPENAMES_(T)>\n        struct tuple_size<GTEST_7_TUPLE_(T) > {\n            static const int value = 7;\n        };\n\n        template <GTEST_8_TYPENAMES_(T)>\n        struct tuple_size<GTEST_8_TUPLE_(T) > {\n            static const int value = 8;\n        };\n\n        template <GTEST_9_TYPENAMES_(T)>\n        struct tuple_size<GTEST_9_TUPLE_(T) > {\n            static const int value = 9;\n        };\n\n        template <GTEST_10_TYPENAMES_(T)>\n        struct tuple_size<GTEST_10_TUPLE_(T) > {\n            static const int value = 10;\n        };\n\n        template <int k, class Tuple>\n        struct tuple_element {\n            typedef typename gtest_internal::TupleElement<\n            k < (tuple_size<Tuple>::value), k, Tuple>::type type;\n        };\n\n#define GTEST_TUPLE_ELEMENT_(k, Tuple) typename tuple_element<k, Tuple >::type\n\n// 6.1.3.4 Element access.\n\n        namespace gtest_internal\n        {\n\n            template <>\n            class Get<0>\n            {\n            public:\n                template <class Tuple>\n                static GTEST_ADD_REF_(GTEST_TUPLE_ELEMENT_(0, Tuple))\n                Field(Tuple &t)\n                {\n                    return t.f0_;    // NOLINT\n                }\n\n                template <class Tuple>\n                static GTEST_BY_REF_(GTEST_TUPLE_ELEMENT_(0, Tuple))\n                ConstField(const Tuple &t)\n                {\n                    return t.f0_;\n                }\n            };\n\n            template <>\n            class Get<1>\n            {\n            public:\n                template <class Tuple>\n                static GTEST_ADD_REF_(GTEST_TUPLE_ELEMENT_(1, Tuple))\n                Field(Tuple &t)\n                {\n                    return t.f1_;    // NOLINT\n                }\n\n                template <class Tuple>\n                static GTEST_BY_REF_(GTEST_TUPLE_ELEMENT_(1, Tuple))\n                ConstField(const Tuple &t)\n                {\n                    return t.f1_;\n                }\n            };\n\n            template <>\n            class Get<2>\n            {\n            public:\n                template <class Tuple>\n                static GTEST_ADD_REF_(GTEST_TUPLE_ELEMENT_(2, Tuple))\n                Field(Tuple &t)\n                {\n                    return t.f2_;    // NOLINT\n                }\n\n                template <class Tuple>\n                static GTEST_BY_REF_(GTEST_TUPLE_ELEMENT_(2, Tuple))\n                ConstField(const Tuple &t)\n                {\n                    return t.f2_;\n                }\n            };\n\n            template <>\n            class Get<3>\n            {\n            public:\n                template <class Tuple>\n                static GTEST_ADD_REF_(GTEST_TUPLE_ELEMENT_(3, Tuple))\n                Field(Tuple &t)\n                {\n                    return t.f3_;    // NOLINT\n                }\n\n                template <class Tuple>\n                static GTEST_BY_REF_(GTEST_TUPLE_ELEMENT_(3, Tuple))\n                ConstField(const Tuple &t)\n                {\n                    return t.f3_;\n                }\n            };\n\n            template <>\n            class Get<4>\n            {\n            public:\n                template <class Tuple>\n                static GTEST_ADD_REF_(GTEST_TUPLE_ELEMENT_(4, Tuple))\n                Field(Tuple &t)\n                {\n                    return t.f4_;    // NOLINT\n                }\n\n                template <class Tuple>\n                static GTEST_BY_REF_(GTEST_TUPLE_ELEMENT_(4, Tuple))\n                ConstField(const Tuple &t)\n                {\n                    return t.f4_;\n                }\n            };\n\n            template <>\n            class Get<5>\n            {\n            public:\n                template <class Tuple>\n                static GTEST_ADD_REF_(GTEST_TUPLE_ELEMENT_(5, Tuple))\n                Field(Tuple &t)\n                {\n                    return t.f5_;    // NOLINT\n                }\n\n                template <class Tuple>\n                static GTEST_BY_REF_(GTEST_TUPLE_ELEMENT_(5, Tuple))\n                ConstField(const Tuple &t)\n                {\n                    return t.f5_;\n                }\n            };\n\n            template <>\n            class Get<6>\n            {\n            public:\n                template <class Tuple>\n                static GTEST_ADD_REF_(GTEST_TUPLE_ELEMENT_(6, Tuple))\n                Field(Tuple &t)\n                {\n                    return t.f6_;    // NOLINT\n                }\n\n                template <class Tuple>\n                static GTEST_BY_REF_(GTEST_TUPLE_ELEMENT_(6, Tuple))\n                ConstField(const Tuple &t)\n                {\n                    return t.f6_;\n                }\n            };\n\n            template <>\n            class Get<7>\n            {\n            public:\n                template <class Tuple>\n                static GTEST_ADD_REF_(GTEST_TUPLE_ELEMENT_(7, Tuple))\n                Field(Tuple &t)\n                {\n                    return t.f7_;    // NOLINT\n                }\n\n                template <class Tuple>\n                static GTEST_BY_REF_(GTEST_TUPLE_ELEMENT_(7, Tuple))\n                ConstField(const Tuple &t)\n                {\n                    return t.f7_;\n                }\n            };\n\n            template <>\n            class Get<8>\n            {\n            public:\n                template <class Tuple>\n                static GTEST_ADD_REF_(GTEST_TUPLE_ELEMENT_(8, Tuple))\n                Field(Tuple &t)\n                {\n                    return t.f8_;    // NOLINT\n                }\n\n                template <class Tuple>\n                static GTEST_BY_REF_(GTEST_TUPLE_ELEMENT_(8, Tuple))\n                ConstField(const Tuple &t)\n                {\n                    return t.f8_;\n                }\n            };\n\n            template <>\n            class Get<9>\n            {\n            public:\n                template <class Tuple>\n                static GTEST_ADD_REF_(GTEST_TUPLE_ELEMENT_(9, Tuple))\n                Field(Tuple &t)\n                {\n                    return t.f9_;    // NOLINT\n                }\n\n                template <class Tuple>\n                static GTEST_BY_REF_(GTEST_TUPLE_ELEMENT_(9, Tuple))\n                ConstField(const Tuple &t)\n                {\n                    return t.f9_;\n                }\n            };\n\n        }  // namespace gtest_internal\n\n        template <int k, GTEST_10_TYPENAMES_(T)>\n        GTEST_ADD_REF_(GTEST_TUPLE_ELEMENT_(k, GTEST_10_TUPLE_(T)))\n        get(GTEST_10_TUPLE_(T)& t)\n        {\n            return gtest_internal::Get<k>::Field(t);\n        }\n\n        template <int k, GTEST_10_TYPENAMES_(T)>\n        GTEST_BY_REF_(GTEST_TUPLE_ELEMENT_(k,  GTEST_10_TUPLE_(T)))\n        get(const GTEST_10_TUPLE_(T)& t)\n        {\n            return gtest_internal::Get<k>::ConstField(t);\n        }\n\n// 6.1.3.5 Relational operators\n\n// We only implement == and !=, as we don't have a need for the rest yet.\n\n        namespace gtest_internal\n        {\n\n// SameSizeTuplePrefixComparator<k, k>::Eq(t1, t2) returns true if the\n// first k fields of t1 equals the first k fields of t2.\n// SameSizeTuplePrefixComparator(k1, k2) would be a compiler error if\n// k1 != k2.\n            template <int kSize1, int kSize2>\n            struct SameSizeTuplePrefixComparator;\n\n            template <>\n            struct SameSizeTuplePrefixComparator<0, 0> {\n                template <class Tuple1, class Tuple2>\n                static bool Eq(const Tuple1 & /* t1 */, const Tuple2 & /* t2 */)\n                {\n                    return true;\n                }\n            };\n\n            template <int k>\n            struct SameSizeTuplePrefixComparator<k, k> {\n                template <class Tuple1, class Tuple2>\n                static bool Eq(const Tuple1 &t1, const Tuple2 &t2)\n                {\n                    return SameSizeTuplePrefixComparator<k - 1, k - 1>::Eq(t1, t2) &&\n                           ::std::tr1::get<k - 1>(t1) == ::std::tr1::get<k - 1>(t2);\n                }\n            };\n\n        }  // namespace gtest_internal\n\n        template <GTEST_10_TYPENAMES_(T), GTEST_10_TYPENAMES_(U)>\n        inline bool operator==(const GTEST_10_TUPLE_(T)& t,\n                               const GTEST_10_TUPLE_(U)& u)\n        {\n            return gtest_internal::SameSizeTuplePrefixComparator<\n                   tuple_size<GTEST_10_TUPLE_(T) >::value,\n                   tuple_size<GTEST_10_TUPLE_(U) >::value>::Eq(t, u);\n        }\n\n        template <GTEST_10_TYPENAMES_(T), GTEST_10_TYPENAMES_(U)>\n        inline bool operator!=(const GTEST_10_TUPLE_(T)& t,\n                               const GTEST_10_TUPLE_(U)& u)\n        {\n            return !(t == u);\n        }\n\n// 6.1.4 Pairs.\n// Unimplemented.\n\n    }  // namespace tr1\n}  // namespace std\n\n#undef GTEST_0_TUPLE_\n#undef GTEST_1_TUPLE_\n#undef GTEST_2_TUPLE_\n#undef GTEST_3_TUPLE_\n#undef GTEST_4_TUPLE_\n#undef GTEST_5_TUPLE_\n#undef GTEST_6_TUPLE_\n#undef GTEST_7_TUPLE_\n#undef GTEST_8_TUPLE_\n#undef GTEST_9_TUPLE_\n#undef GTEST_10_TUPLE_\n\n#undef GTEST_0_TYPENAMES_\n#undef GTEST_1_TYPENAMES_\n#undef GTEST_2_TYPENAMES_\n#undef GTEST_3_TYPENAMES_\n#undef GTEST_4_TYPENAMES_\n#undef GTEST_5_TYPENAMES_\n#undef GTEST_6_TYPENAMES_\n#undef GTEST_7_TYPENAMES_\n#undef GTEST_8_TYPENAMES_\n#undef GTEST_9_TYPENAMES_\n#undef GTEST_10_TYPENAMES_\n\n#undef GTEST_DECLARE_TUPLE_AS_FRIEND_\n#undef GTEST_BY_REF_\n#undef GTEST_ADD_REF_\n#undef GTEST_TUPLE_ELEMENT_\n\n#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TUPLE_H_\n# elif GTEST_ENV_HAS_STD_TUPLE_\n#  include <tuple>\n// C++11 puts its tuple into the ::std namespace rather than\n// ::std::tr1.  gtest expects tuple to live in ::std::tr1, so put it there.\n// This causes undefined behavior, but supported compilers react in\n// the way we intend.\nnamespace std\n{\n    namespace tr1\n    {\n        using ::std::get;\n        using ::std::make_tuple;\n        using ::std::tuple;\n        using ::std::tuple_element;\n        using ::std::tuple_size;\n    }\n}\n\n# elif GTEST_OS_SYMBIAN\n\n// On Symbian, BOOST_HAS_TR1_TUPLE causes Boost's TR1 tuple library to\n// use STLport's tuple implementation, which unfortunately doesn't\n// work as the copy of STLport distributed with Symbian is incomplete.\n// By making sure BOOST_HAS_TR1_TUPLE is undefined, we force Boost to\n// use its own tuple implementation.\n#  ifdef BOOST_HAS_TR1_TUPLE\n#   undef BOOST_HAS_TR1_TUPLE\n#  endif  // BOOST_HAS_TR1_TUPLE\n\n// This prevents <boost/tr1/detail/config.hpp>, which defines\n// BOOST_HAS_TR1_TUPLE, from being #included by Boost's <tuple>.\n#  define BOOST_TR1_DETAIL_CONFIG_HPP_INCLUDED\n#  include <tuple>  // IWYU pragma: export  // NOLINT\n\n# elif defined(__GNUC__) && (GTEST_GCC_VER_ >= 40000)\n// GCC 4.0+ implements tr1/tuple in the <tr1/tuple> header.  This does\n// not conform to the TR1 spec, which requires the header to be <tuple>.\n\n#  if !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302\n// Until version 4.3.2, gcc has a bug that causes <tr1/functional>,\n// which is #included by <tr1/tuple>, to not compile when RTTI is\n// disabled.  _TR1_FUNCTIONAL is the header guard for\n// <tr1/functional>.  Hence the following #define is a hack to prevent\n// <tr1/functional> from being included.\n#   define _TR1_FUNCTIONAL 1\n#   include <tr1/tuple>\n#   undef _TR1_FUNCTIONAL  // Allows the user to #include\n// <tr1/functional> if he chooses to.\n#  else\n#   include <tr1/tuple>  // NOLINT\n#  endif  // !GTEST_HAS_RTTI && GTEST_GCC_VER_ < 40302\n\n# else\n// If the compiler is not GCC 4.0+, we assume the user is using a\n// spec-conforming TR1 implementation.\n#  include <tuple>  // IWYU pragma: export  // NOLINT\n# endif  // GTEST_USE_OWN_TR1_TUPLE\n\n#endif  // GTEST_HAS_TR1_TUPLE\n\n// Determines whether clone(2) is supported.\n// Usually it will only be available on Linux, excluding\n// Linux on the Itanium architecture.\n// Also see http://linux.die.net/man/2/clone.\n#ifndef GTEST_HAS_CLONE\n// The user didn't tell us, so we need to figure it out.\n\n# if GTEST_OS_LINUX && !defined(__ia64__)\n#  if GTEST_OS_LINUX_ANDROID\n// On Android, clone() became available at different API levels for each 32-bit\n// architecture.\n#    if defined(__LP64__) || \\\n        (defined(__arm__) && __ANDROID_API__ >= 9) || \\\n        (defined(__mips__) && __ANDROID_API__ >= 12) || \\\n        (defined(__i386__) && __ANDROID_API__ >= 17)\n#     define GTEST_HAS_CLONE 1\n#    else\n#     define GTEST_HAS_CLONE 0\n#    endif\n#  else\n#   define GTEST_HAS_CLONE 1\n#  endif\n# else\n#  define GTEST_HAS_CLONE 0\n# endif  // GTEST_OS_LINUX && !defined(__ia64__)\n\n#endif  // GTEST_HAS_CLONE\n\n// Determines whether to support stream redirection. This is used to test\n// output correctness and to implement death tests.\n#ifndef GTEST_HAS_STREAM_REDIRECTION\n// By default, we assume that stream redirection is supported on all\n// platforms except known mobile ones.\n# if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_SYMBIAN || \\\n    GTEST_OS_WINDOWS_PHONE || GTEST_OS_WINDOWS_RT\n#  define GTEST_HAS_STREAM_REDIRECTION 0\n# else\n#  define GTEST_HAS_STREAM_REDIRECTION 1\n# endif  // !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_SYMBIAN\n#endif  // GTEST_HAS_STREAM_REDIRECTION\n\n// Determines whether to support death tests.\n// Google Test does not support death tests for VC 7.1 and earlier as\n// abort() in a VC 7.1 application compiled as GUI in debug config\n// pops up a dialog window that cannot be suppressed programmatically.\n#if (GTEST_OS_LINUX || GTEST_OS_CYGWIN || GTEST_OS_SOLARIS || \\\n     (GTEST_OS_MAC && !GTEST_OS_IOS) || \\\n     (GTEST_OS_WINDOWS_DESKTOP && _MSC_VER >= 1400) || \\\n     GTEST_OS_WINDOWS_MINGW || GTEST_OS_AIX || GTEST_OS_HPUX || \\\n     GTEST_OS_OPENBSD || GTEST_OS_QNX || GTEST_OS_FREEBSD || GTEST_OS_NETBSD)\n# define GTEST_HAS_DEATH_TEST 1\n#endif\n\n// We don't support MSVC 7.1 with exceptions disabled now.  Therefore\n// all the compilers we care about are adequate for supporting\n// value-parameterized tests.\n#define GTEST_HAS_PARAM_TEST 1\n\n// Determines whether to support type-driven tests.\n\n// Typed tests need <typeinfo> and variadic macros, which GCC, VC++ 8.0,\n// Sun Pro CC, IBM Visual Age, and HP aCC support.\n#if defined(__GNUC__) || (_MSC_VER >= 1400) || defined(__SUNPRO_CC) || \\\n    defined(__IBMCPP__) || defined(__HP_aCC)\n# define GTEST_HAS_TYPED_TEST 1\n# define GTEST_HAS_TYPED_TEST_P 1\n#endif\n\n// Determines whether to support Combine(). This only makes sense when\n// value-parameterized tests are enabled.  The implementation doesn't\n// work on Sun Studio since it doesn't understand templated conversion\n// operators.\n#if GTEST_HAS_PARAM_TEST && GTEST_HAS_TR1_TUPLE && !defined(__SUNPRO_CC)\n# define GTEST_HAS_COMBINE 1\n#endif\n\n// Determines whether the system compiler uses UTF-16 for encoding wide strings.\n#define GTEST_WIDE_STRING_USES_UTF16_ \\\n    (GTEST_OS_WINDOWS || GTEST_OS_CYGWIN || GTEST_OS_SYMBIAN || GTEST_OS_AIX)\n\n// Determines whether test results can be streamed to a socket.\n#if GTEST_OS_LINUX\n# define GTEST_CAN_STREAM_RESULTS_ 1\n#endif\n\n// Defines some utility macros.\n\n// The GNU compiler emits a warning if nested \"if\" statements are followed by\n// an \"else\" statement and braces are not used to explicitly disambiguate the\n// \"else\" binding.  This leads to problems with code like:\n//\n//   if (gate)\n//     ASSERT_*(condition) << \"Some message\";\n//\n// The \"switch (0) case 0:\" idiom is used to suppress this.\n#ifdef __INTEL_COMPILER\n# define GTEST_AMBIGUOUS_ELSE_BLOCKER_\n#else\n# define GTEST_AMBIGUOUS_ELSE_BLOCKER_ switch (0) case 0: default:  // NOLINT\n#endif\n\n// Use this annotation at the end of a struct/class definition to\n// prevent the compiler from optimizing away instances that are never\n// used.  This is useful when all interesting logic happens inside the\n// c'tor and / or d'tor.  Example:\n//\n//   struct Foo {\n//     Foo() { ... }\n//   } GTEST_ATTRIBUTE_UNUSED_;\n//\n// Also use it after a variable or parameter declaration to tell the\n// compiler the variable/parameter does not have to be used.\n#if defined(__GNUC__) && !defined(COMPILER_ICC)\n# define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused))\n#elif defined(__clang__)\n# if __has_attribute(unused)\n#  define GTEST_ATTRIBUTE_UNUSED_ __attribute__ ((unused))\n# endif\n#endif\n#ifndef GTEST_ATTRIBUTE_UNUSED_\n# define GTEST_ATTRIBUTE_UNUSED_\n#endif\n\n// Use this annotation before a function that takes a printf format string.\n#if defined(__GNUC__) && !defined(COMPILER_ICC)\n# if defined(__MINGW_PRINTF_FORMAT)\n// MinGW has two different printf implementations. Ensure the format macro\n// matches the selected implementation. See\n// https://sourceforge.net/p/mingw-w64/wiki2/gnu%20printf/.\n#  define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) \\\n       __attribute__((__format__(__MINGW_PRINTF_FORMAT, string_index, \\\n                                 first_to_check)))\n# else\n#  define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check) \\\n       __attribute__((__format__(__printf__, string_index, first_to_check)))\n# endif\n#else\n# define GTEST_ATTRIBUTE_PRINTF_(string_index, first_to_check)\n#endif\n\n// A macro to disallow operator=\n// This should be used in the private: declarations for a class.\n#define GTEST_DISALLOW_ASSIGN_(type)\\\n  void operator=(type const &)\n\n// A macro to disallow copy constructor and operator=\n// This should be used in the private: declarations for a class.\n#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)\\\n  type(type const &);\\\n  GTEST_DISALLOW_ASSIGN_(type)\n\n// Tell the compiler to warn about unused return values for functions declared\n// with this macro.  The macro should be used on function declarations\n// following the argument list:\n//\n//   Sprocket* AllocateSprocket() GTEST_MUST_USE_RESULT_;\n#if defined(__GNUC__) && (GTEST_GCC_VER_ >= 30400) && !defined(COMPILER_ICC)\n# define GTEST_MUST_USE_RESULT_ __attribute__ ((warn_unused_result))\n#else\n# define GTEST_MUST_USE_RESULT_\n#endif  // __GNUC__ && (GTEST_GCC_VER_ >= 30400) && !COMPILER_ICC\n\n// MS C++ compiler emits warning when a conditional expression is compile time\n// constant. In some contexts this warning is false positive and needs to be\n// suppressed. Use the following two macros in such cases:\n//\n// GTEST_INTENTIONAL_CONST_COND_PUSH_()\n// while (true) {\n// GTEST_INTENTIONAL_CONST_COND_POP_()\n// }\n# define GTEST_INTENTIONAL_CONST_COND_PUSH_() \\\n    GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127)\n# define GTEST_INTENTIONAL_CONST_COND_POP_() \\\n    GTEST_DISABLE_MSC_WARNINGS_POP_()\n\n// Determine whether the compiler supports Microsoft's Structured Exception\n// Handling.  This is supported by several Windows compilers but generally\n// does not exist on any other system.\n#ifndef GTEST_HAS_SEH\n// The user didn't tell us, so we need to figure it out.\n\n# if defined(_MSC_VER) || defined(__BORLANDC__)\n// These two compilers are known to support SEH.\n#  define GTEST_HAS_SEH 1\n# else\n// Assume no SEH.\n#  define GTEST_HAS_SEH 0\n# endif\n\n#define GTEST_IS_THREADSAFE \\\n    (GTEST_HAS_MUTEX_AND_THREAD_LOCAL_ \\\n     || (GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT) \\\n     || GTEST_HAS_PTHREAD)\n\n#endif  // GTEST_HAS_SEH\n\n// GTEST_API_ qualifies all symbols that must be exported. The definitions below\n// are guarded by #ifndef to give embedders a chance to define GTEST_API_ in\n// gtest/internal/custom/gtest-port.h\n#ifndef GTEST_API_\n\n#ifdef _MSC_VER\n# if GTEST_LINKED_AS_SHARED_LIBRARY\n#  define GTEST_API_ __declspec(dllimport)\n# elif GTEST_CREATE_SHARED_LIBRARY\n#  define GTEST_API_ __declspec(dllexport)\n# endif\n#elif __GNUC__ >= 4 || defined(__clang__)\n# define GTEST_API_ __attribute__((visibility (\"default\")))\n#endif // _MSC_VER\n\n#endif // GTEST_API_\n\n#ifndef GTEST_API_\n# define GTEST_API_\n#endif // GTEST_API_\n\n#ifdef __GNUC__\n// Ask the compiler to never inline a given function.\n# define GTEST_NO_INLINE_ __attribute__((noinline))\n#else\n# define GTEST_NO_INLINE_\n#endif\n\n// _LIBCPP_VERSION is defined by the libc++ library from the LLVM project.\n#if defined(__GLIBCXX__) || defined(_LIBCPP_VERSION)\n# define GTEST_HAS_CXXABI_H_ 1\n#else\n# define GTEST_HAS_CXXABI_H_ 0\n#endif\n\n// A function level attribute to disable checking for use of uninitialized\n// memory when built with MemorySanitizer.\n#if defined(__clang__)\n# if __has_feature(memory_sanitizer)\n#  define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ \\\n       __attribute__((no_sanitize_memory))\n# else\n#  define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_\n# endif  // __has_feature(memory_sanitizer)\n#else\n# define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_\n#endif  // __clang__\n\n// A function level attribute to disable AddressSanitizer instrumentation.\n#if defined(__clang__)\n# if __has_feature(address_sanitizer)\n#  define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ \\\n       __attribute__((no_sanitize_address))\n# else\n#  define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_\n# endif  // __has_feature(address_sanitizer)\n#else\n# define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_\n#endif  // __clang__\n\n// A function level attribute to disable ThreadSanitizer instrumentation.\n#if defined(__clang__)\n# if __has_feature(thread_sanitizer)\n#  define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ \\\n       __attribute__((no_sanitize_thread))\n# else\n#  define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_\n# endif  // __has_feature(thread_sanitizer)\n#else\n# define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_\n#endif  // __clang__\n\nnamespace testing\n{\n\n    class Message;\n\n#if defined(GTEST_TUPLE_NAMESPACE_)\n// Import tuple and friends into the ::testing namespace.\n// It is part of our interface, having them in ::testing allows us to change\n// their types as needed.\n    using GTEST_TUPLE_NAMESPACE_::get;\n    using GTEST_TUPLE_NAMESPACE_::make_tuple;\n    using GTEST_TUPLE_NAMESPACE_::tuple;\n    using GTEST_TUPLE_NAMESPACE_::tuple_size;\n    using GTEST_TUPLE_NAMESPACE_::tuple_element;\n#endif  // defined(GTEST_TUPLE_NAMESPACE_)\n\n    namespace internal\n    {\n\n// A secret type that Google Test users don't know about.  It has no\n// definition on purpose.  Therefore it's impossible to create a\n// Secret object, which is what we want.\n        class Secret;\n\n// The GTEST_COMPILE_ASSERT_ macro can be used to verify that a compile time\n// expression is true. For example, you could use it to verify the\n// size of a static array:\n//\n//   GTEST_COMPILE_ASSERT_(GTEST_ARRAY_SIZE_(names) == NUM_NAMES,\n//                         names_incorrect_size);\n//\n// or to make sure a struct is smaller than a certain size:\n//\n//   GTEST_COMPILE_ASSERT_(sizeof(foo) < 128, foo_too_large);\n//\n// The second argument to the macro is the name of the variable. If\n// the expression is false, most compilers will issue a warning/error\n// containing the name of the variable.\n\n#if GTEST_LANG_CXX11\n# define GTEST_COMPILE_ASSERT_(expr, msg) static_assert(expr, #msg)\n#else  // !GTEST_LANG_CXX11\n        template <bool>\n        struct CompileAssert {\n        };\n\n# define GTEST_COMPILE_ASSERT_(expr, msg) \\\n  typedef ::testing::internal::CompileAssert<(static_cast<bool>(expr))> \\\n      msg[static_cast<bool>(expr) ? 1 : -1] GTEST_ATTRIBUTE_UNUSED_\n#endif  // !GTEST_LANG_CXX11\n\n// Implementation details of GTEST_COMPILE_ASSERT_:\n//\n// (In C++11, we simply use static_assert instead of the following)\n//\n// - GTEST_COMPILE_ASSERT_ works by defining an array type that has -1\n//   elements (and thus is invalid) when the expression is false.\n//\n// - The simpler definition\n//\n//    #define GTEST_COMPILE_ASSERT_(expr, msg) typedef char msg[(expr) ? 1 : -1]\n//\n//   does not work, as gcc supports variable-length arrays whose sizes\n//   are determined at run-time (this is gcc's extension and not part\n//   of the C++ standard).  As a result, gcc fails to reject the\n//   following code with the simple definition:\n//\n//     int foo;\n//     GTEST_COMPILE_ASSERT_(foo, msg); // not supposed to compile as foo is\n//                                      // not a compile-time constant.\n//\n// - By using the type CompileAssert<(bool(expr))>, we ensures that\n//   expr is a compile-time constant.  (Template arguments must be\n//   determined at compile-time.)\n//\n// - The outter parentheses in CompileAssert<(bool(expr))> are necessary\n//   to work around a bug in gcc 3.4.4 and 4.0.1.  If we had written\n//\n//     CompileAssert<bool(expr)>\n//\n//   instead, these compilers will refuse to compile\n//\n//     GTEST_COMPILE_ASSERT_(5 > 0, some_message);\n//\n//   (They seem to think the \">\" in \"5 > 0\" marks the end of the\n//   template argument list.)\n//\n// - The array size is (bool(expr) ? 1 : -1), instead of simply\n//\n//     ((expr) ? 1 : -1).\n//\n//   This is to avoid running into a bug in MS VC 7.1, which\n//   causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.\n\n// StaticAssertTypeEqHelper is used by StaticAssertTypeEq defined in gtest.h.\n//\n// This template is declared, but intentionally undefined.\n        template <typename T1, typename T2>\n        struct StaticAssertTypeEqHelper;\n\n        template <typename T>\n        struct StaticAssertTypeEqHelper<T, T> {\n            enum { value = true };\n        };\n\n// Evaluates to the number of elements in 'array'.\n#define GTEST_ARRAY_SIZE_(array) (sizeof(array) / sizeof(array[0]))\n\n#if GTEST_HAS_GLOBAL_STRING\n        typedef ::string string;\n#else\n        typedef ::std::string string;\n#endif  // GTEST_HAS_GLOBAL_STRING\n\n#if GTEST_HAS_GLOBAL_WSTRING\n        typedef ::wstring wstring;\n#elif GTEST_HAS_STD_WSTRING\n        typedef ::std::wstring wstring;\n#endif  // GTEST_HAS_GLOBAL_WSTRING\n\n// A helper for suppressing warnings on constant condition.  It just\n// returns 'condition'.\n        GTEST_API_ bool IsTrue(bool condition);\n\n// Defines scoped_ptr.\n\n// This implementation of scoped_ptr is PARTIAL - it only contains\n// enough stuff to satisfy Google Test's need.\n        template <typename T>\n        class scoped_ptr\n        {\n        public:\n            typedef T element_type;\n\n            explicit scoped_ptr(T *p = NULL) : ptr_(p) {}\n            ~scoped_ptr()\n            {\n                reset();\n            }\n\n            T &operator*() const\n            {\n                return *ptr_;\n            }\n            T *operator->() const\n            {\n                return ptr_;\n            }\n            T *get() const\n            {\n                return ptr_;\n            }\n\n            T *release()\n            {\n                T *const ptr = ptr_;\n                ptr_ = NULL;\n                return ptr;\n            }\n\n            void reset(T *p = NULL)\n            {\n                if (p != ptr_) {\n                    if (IsTrue(sizeof(T) > 0)) {  // Makes sure T is a complete type.\n                        delete ptr_;\n                    }\n                    ptr_ = p;\n                }\n            }\n\n            friend void swap(scoped_ptr &a, scoped_ptr &b)\n            {\n                using std::swap;\n                swap(a.ptr_, b.ptr_);\n            }\n\n        private:\n            T *ptr_;\n\n            GTEST_DISALLOW_COPY_AND_ASSIGN_(scoped_ptr);\n        };\n\n// Defines RE.\n\n// A simple C++ wrapper for <regex.h>.  It uses the POSIX Extended\n// Regular Expression syntax.\n        class GTEST_API_ RE\n        {\n        public:\n            // A copy constructor is required by the Standard to initialize object\n            // references from r-values.\n            RE(const RE &other)\n            {\n                Init(other.pattern());\n            }\n\n            // Constructs an RE from a string.\n            RE(const ::std::string &regex)\n            {\n                Init(regex.c_str());    // NOLINT\n            }\n\n#if GTEST_HAS_GLOBAL_STRING\n\n            RE(const ::string &regex)\n            {\n                Init(regex.c_str());    // NOLINT\n            }\n\n#endif  // GTEST_HAS_GLOBAL_STRING\n\n            RE(const char *regex)\n            {\n                Init(regex);    // NOLINT\n            }\n            ~RE();\n\n            // Returns the string representation of the regex.\n            const char *pattern() const\n            {\n                return pattern_;\n            }\n\n            // FullMatch(str, re) returns true iff regular expression re matches\n            // the entire str.\n            // PartialMatch(str, re) returns true iff regular expression re\n            // matches a substring of str (including str itself).\n            //\n            // TODO(wan@google.com): make FullMatch() and PartialMatch() work\n            // when str contains NUL characters.\n            static bool FullMatch(const ::std::string &str, const RE &re)\n            {\n                return FullMatch(str.c_str(), re);\n            }\n            static bool PartialMatch(const ::std::string &str, const RE &re)\n            {\n                return PartialMatch(str.c_str(), re);\n            }\n\n#if GTEST_HAS_GLOBAL_STRING\n\n            static bool FullMatch(const ::string &str, const RE &re)\n            {\n                return FullMatch(str.c_str(), re);\n            }\n            static bool PartialMatch(const ::string &str, const RE &re)\n            {\n                return PartialMatch(str.c_str(), re);\n            }\n\n#endif  // GTEST_HAS_GLOBAL_STRING\n\n            static bool FullMatch(const char *str, const RE &re);\n            static bool PartialMatch(const char *str, const RE &re);\n\n        private:\n            void Init(const char *regex);\n\n            // We use a const char* instead of an std::string, as Google Test used to be\n            // used where std::string is not available.  TODO(wan@google.com): change to\n            // std::string.\n            const char *pattern_;\n            bool is_valid_;\n\n#if GTEST_USES_POSIX_RE\n\n            regex_t full_regex_;     // For FullMatch().\n            regex_t partial_regex_;  // For PartialMatch().\n\n#else  // GTEST_USES_SIMPLE_RE\n\n            const char *full_pattern_;  // For FullMatch();\n\n#endif\n\n            GTEST_DISALLOW_ASSIGN_(RE);\n        };\n\n// Formats a source file path and a line number as they would appear\n// in an error message from the compiler used to compile this code.\n        GTEST_API_ ::std::string FormatFileLocation(const char *file, int line);\n\n// Formats a file location for compiler-independent XML output.\n// Although this function is not platform dependent, we put it next to\n// FormatFileLocation in order to contrast the two functions.\n        GTEST_API_ ::std::string FormatCompilerIndependentFileLocation(const char *file,\n                                                                       int line);\n\n// Defines logging utilities:\n//   GTEST_LOG_(severity) - logs messages at the specified severity level. The\n//                          message itself is streamed into the macro.\n//   LogToStderr()  - directs all log messages to stderr.\n//   FlushInfoLog() - flushes informational log messages.\n\n        enum GTestLogSeverity {\n            GTEST_INFO,\n            GTEST_WARNING,\n            GTEST_ERROR,\n            GTEST_FATAL\n        };\n\n// Formats log entry severity, provides a stream object for streaming the\n// log message, and terminates the message with a newline when going out of\n// scope.\n        class GTEST_API_ GTestLog\n        {\n        public:\n            GTestLog(GTestLogSeverity severity, const char *file, int line);\n\n            // Flushes the buffers and, if severity is GTEST_FATAL, aborts the program.\n            ~GTestLog();\n\n            ::std::ostream &GetStream()\n            {\n                return ::std::cerr;\n            }\n\n        private:\n            const GTestLogSeverity severity_;\n\n            GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestLog);\n        };\n\n#if !defined(GTEST_LOG_)\n\n# define GTEST_LOG_(severity) \\\n    ::testing::internal::GTestLog(::testing::internal::GTEST_##severity, \\\n                                  __FILE__, __LINE__).GetStream()\n\n        inline void LogToStderr() {}\n        inline void FlushInfoLog()\n        {\n            fflush(NULL);\n        }\n\n#endif  // !defined(GTEST_LOG_)\n\n#if !defined(GTEST_CHECK_)\n// INTERNAL IMPLEMENTATION - DO NOT USE.\n//\n// GTEST_CHECK_ is an all-mode assert. It aborts the program if the condition\n// is not satisfied.\n//  Synopsys:\n//    GTEST_CHECK_(boolean_condition);\n//     or\n//    GTEST_CHECK_(boolean_condition) << \"Additional message\";\n//\n//    This checks the condition and if the condition is not satisfied\n//    it prints message about the condition violation, including the\n//    condition itself, plus additional message streamed into it, if any,\n//    and then it aborts the program. It aborts the program irrespective of\n//    whether it is built in the debug mode or not.\n# define GTEST_CHECK_(condition) \\\n    GTEST_AMBIGUOUS_ELSE_BLOCKER_ \\\n    if (::testing::internal::IsTrue(condition)) \\\n      ; \\\n    else \\\n      GTEST_LOG_(FATAL) << \"Condition \" #condition \" failed. \"\n#endif  // !defined(GTEST_CHECK_)\n\n// An all-mode assert to verify that the given POSIX-style function\n// call returns 0 (indicating success).  Known limitation: this\n// doesn't expand to a balanced 'if' statement, so enclose the macro\n// in {} if you need to use it as the only statement in an 'if'\n// branch.\n#define GTEST_CHECK_POSIX_SUCCESS_(posix_call) \\\n  if (const int gtest_error = (posix_call)) \\\n    GTEST_LOG_(FATAL) << #posix_call << \"failed with error \" \\\n                      << gtest_error\n\n#if GTEST_HAS_STD_MOVE_\n        using std::move;\n#else  // GTEST_HAS_STD_MOVE_\n        template <typename T>\n        const T &move(const T &t)\n        {\n            return t;\n        }\n#endif  // GTEST_HAS_STD_MOVE_\n\n// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.\n//\n// Use ImplicitCast_ as a safe version of static_cast for upcasting in\n// the type hierarchy (e.g. casting a Foo* to a SuperclassOfFoo* or a\n// const Foo*).  When you use ImplicitCast_, the compiler checks that\n// the cast is safe.  Such explicit ImplicitCast_s are necessary in\n// surprisingly many situations where C++ demands an exact type match\n// instead of an argument type convertable to a target type.\n//\n// The syntax for using ImplicitCast_ is the same as for static_cast:\n//\n//   ImplicitCast_<ToType>(expr)\n//\n// ImplicitCast_ would have been part of the C++ standard library,\n// but the proposal was submitted too late.  It will probably make\n// its way into the language in the future.\n//\n// This relatively ugly name is intentional. It prevents clashes with\n// similar functions users may have (e.g., implicit_cast). The internal\n// namespace alone is not enough because the function can be found by ADL.\n        template<typename To>\n        inline To ImplicitCast_(To x)\n        {\n            return x;\n        }\n\n// When you upcast (that is, cast a pointer from type Foo to type\n// SuperclassOfFoo), it's fine to use ImplicitCast_<>, since upcasts\n// always succeed.  When you downcast (that is, cast a pointer from\n// type Foo to type SubclassOfFoo), static_cast<> isn't safe, because\n// how do you know the pointer is really of type SubclassOfFoo?  It\n// could be a bare Foo, or of type DifferentSubclassOfFoo.  Thus,\n// when you downcast, you should use this macro.  In debug mode, we\n// use dynamic_cast<> to double-check the downcast is legal (we die\n// if it's not).  In normal mode, we do the efficient static_cast<>\n// instead.  Thus, it's important to test in debug mode to make sure\n// the cast is legal!\n//    This is the only place in the code we should use dynamic_cast<>.\n// In particular, you SHOULDN'T be using dynamic_cast<> in order to\n// do RTTI (eg code like this:\n//    if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);\n//    if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);\n// You should design the code some other way not to need this.\n//\n// This relatively ugly name is intentional. It prevents clashes with\n// similar functions users may have (e.g., down_cast). The internal\n// namespace alone is not enough because the function can be found by ADL.\n        template<typename To, typename From>  // use like this: DownCast_<T*>(foo);\n        inline To DownCast_(From *f)    // so we only accept pointers\n        {\n            // Ensures that To is a sub-type of From *.  This test is here only\n            // for compile-time type checking, and has no overhead in an\n            // optimized build at run-time, as it will be optimized away\n            // completely.\n            GTEST_INTENTIONAL_CONST_COND_PUSH_()\n            if (false) {\n                GTEST_INTENTIONAL_CONST_COND_POP_()\n                const To to = NULL;\n                ::testing::internal::ImplicitCast_<From *>(to);\n            }\n\n#if GTEST_HAS_RTTI\n            // RTTI: debug mode only!\n            GTEST_CHECK_(f == NULL || dynamic_cast<To>(f) != NULL);\n#endif\n            return static_cast<To>(f);\n        }\n\n// Downcasts the pointer of type Base to Derived.\n// Derived must be a subclass of Base. The parameter MUST\n// point to a class of type Derived, not any subclass of it.\n// When RTTI is available, the function performs a runtime\n// check to enforce this.\n        template <class Derived, class Base>\n        Derived *CheckedDowncastToActualType(Base *base)\n        {\n#if GTEST_HAS_RTTI\n            GTEST_CHECK_(typeid(*base) == typeid(Derived));\n#endif\n\n#if GTEST_HAS_DOWNCAST_\n            return ::down_cast<Derived *>(base);\n#elif GTEST_HAS_RTTI\n            return dynamic_cast<Derived *>(base); // NOLINT\n#else\n            return static_cast<Derived *>(base); // Poor man's downcast.\n#endif\n        }\n\n#if GTEST_HAS_STREAM_REDIRECTION\n\n// Defines the stderr capturer:\n//   CaptureStdout     - starts capturing stdout.\n//   GetCapturedStdout - stops capturing stdout and returns the captured string.\n//   CaptureStderr     - starts capturing stderr.\n//   GetCapturedStderr - stops capturing stderr and returns the captured string.\n//\n        GTEST_API_ void CaptureStdout();\n        GTEST_API_ std::string GetCapturedStdout();\n        GTEST_API_ void CaptureStderr();\n        GTEST_API_ std::string GetCapturedStderr();\n\n#endif  // GTEST_HAS_STREAM_REDIRECTION\n\n// Returns the size (in bytes) of a file.\n        GTEST_API_ size_t GetFileSize(FILE *file);\n\n// Reads the entire content of a file as a string.\n        GTEST_API_ std::string ReadEntireFile(FILE *file);\n\n// All command line arguments.\n        GTEST_API_ const ::std::vector<testing::internal::string> &GetArgvs();\n\n#if GTEST_HAS_DEATH_TEST\n\n        const ::std::vector<testing::internal::string> &GetInjectableArgvs();\n        void SetInjectableArgvs(const ::std::vector<testing::internal::string> *\n                                new_argvs);\n\n\n#endif  // GTEST_HAS_DEATH_TEST\n\n// Defines synchronization primitives.\n#if GTEST_IS_THREADSAFE\n# if GTEST_HAS_PTHREAD\n// Sleeps for (roughly) n milliseconds.  This function is only for testing\n// Google Test's own constructs.  Don't use it in user tests, either\n// directly or indirectly.\n        inline void SleepMilliseconds(int n)\n        {\n            const timespec time = {\n                0,                  // 0 seconds.\n                n * 1000L * 1000L,  // And n ms.\n            };\n            nanosleep(&time, NULL);\n        }\n# endif  // GTEST_HAS_PTHREAD\n\n# if GTEST_HAS_NOTIFICATION_\n// Notification has already been imported into the namespace.\n// Nothing to do here.\n\n# elif GTEST_HAS_PTHREAD\n// Allows a controller thread to pause execution of newly created\n// threads until notified.  Instances of this class must be created\n// and destroyed in the controller thread.\n//\n// This class is only for testing Google Test's own constructs. Do not\n// use it in user tests, either directly or indirectly.\n        class Notification\n        {\n        public:\n            Notification() : notified_(false)\n            {\n                GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, NULL));\n            }\n            ~Notification()\n            {\n                pthread_mutex_destroy(&mutex_);\n            }\n\n            // Notifies all threads created with this notification to start. Must\n            // be called from the controller thread.\n            void Notify()\n            {\n                pthread_mutex_lock(&mutex_);\n                notified_ = true;\n                pthread_mutex_unlock(&mutex_);\n            }\n\n            // Blocks until the controller thread notifies. Must be called from a test\n            // thread.\n            void WaitForNotification()\n            {\n                for (;;) {\n                    pthread_mutex_lock(&mutex_);\n                    const bool notified = notified_;\n                    pthread_mutex_unlock(&mutex_);\n                    if (notified)\n                        break;\n                    SleepMilliseconds(10);\n                }\n            }\n\n        private:\n            pthread_mutex_t mutex_;\n            bool notified_;\n\n            GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification);\n        };\n\n# elif GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT\n\n        GTEST_API_ void SleepMilliseconds(int n);\n\n// Provides leak-safe Windows kernel handle ownership.\n// Used in death tests and in threading support.\n        class GTEST_API_ AutoHandle\n        {\n        public:\n            // Assume that Win32 HANDLE type is equivalent to void*. Doing so allows us to\n            // avoid including <windows.h> in this header file. Including <windows.h> is\n            // undesirable because it defines a lot of symbols and macros that tend to\n            // conflict with client code. This assumption is verified by\n            // WindowsTypesTest.HANDLEIsVoidStar.\n            typedef void *Handle;\n            AutoHandle();\n            explicit AutoHandle(Handle handle);\n\n            ~AutoHandle();\n\n            Handle Get() const;\n            void Reset();\n            void Reset(Handle handle);\n\n        private:\n            // Returns true iff the handle is a valid handle object that can be closed.\n            bool IsCloseable() const;\n\n            Handle handle_;\n\n            GTEST_DISALLOW_COPY_AND_ASSIGN_(AutoHandle);\n        };\n\n// Allows a controller thread to pause execution of newly created\n// threads until notified.  Instances of this class must be created\n// and destroyed in the controller thread.\n//\n// This class is only for testing Google Test's own constructs. Do not\n// use it in user tests, either directly or indirectly.\n        class GTEST_API_ Notification\n        {\n        public:\n            Notification();\n            void Notify();\n            void WaitForNotification();\n\n        private:\n            AutoHandle event_;\n\n            GTEST_DISALLOW_COPY_AND_ASSIGN_(Notification);\n        };\n# endif  // GTEST_HAS_NOTIFICATION_\n\n// On MinGW, we can have both GTEST_OS_WINDOWS and GTEST_HAS_PTHREAD\n// defined, but we don't want to use MinGW's pthreads implementation, which\n// has conformance problems with some versions of the POSIX standard.\n# if GTEST_HAS_PTHREAD && !GTEST_OS_WINDOWS_MINGW\n\n// As a C-function, ThreadFuncWithCLinkage cannot be templated itself.\n// Consequently, it cannot select a correct instantiation of ThreadWithParam\n// in order to call its Run(). Introducing ThreadWithParamBase as a\n// non-templated base class for ThreadWithParam allows us to bypass this\n// problem.\n        class ThreadWithParamBase\n        {\n        public:\n            virtual ~ThreadWithParamBase() {}\n            virtual void Run() = 0;\n        };\n\n// pthread_create() accepts a pointer to a function type with the C linkage.\n// According to the Standard (7.5/1), function types with different linkages\n// are different even if they are otherwise identical.  Some compilers (for\n// example, SunStudio) treat them as different types.  Since class methods\n// cannot be defined with C-linkage we need to define a free C-function to\n// pass into pthread_create().\n        extern \"C\" inline void *ThreadFuncWithCLinkage(void *thread)\n        {\n            static_cast<ThreadWithParamBase *>(thread)->Run();\n            return NULL;\n        }\n\n// Helper class for testing Google Test's multi-threading constructs.\n// To use it, write:\n//\n//   void ThreadFunc(int param) { /* Do things with param */ }\n//   Notification thread_can_start;\n//   ...\n//   // The thread_can_start parameter is optional; you can supply NULL.\n//   ThreadWithParam<int> thread(&ThreadFunc, 5, &thread_can_start);\n//   thread_can_start.Notify();\n//\n// These classes are only for testing Google Test's own constructs. Do\n// not use them in user tests, either directly or indirectly.\n        template <typename T>\n        class ThreadWithParam : public ThreadWithParamBase\n        {\n        public:\n            typedef void UserThreadFunc(T);\n\n            ThreadWithParam(UserThreadFunc *func, T param, Notification *thread_can_start)\n                : func_(func),\n                  param_(param),\n                  thread_can_start_(thread_can_start),\n                  finished_(false)\n            {\n                ThreadWithParamBase *const base = this;\n                // The thread can be created only after all fields except thread_\n                // have been initialized.\n                GTEST_CHECK_POSIX_SUCCESS_(\n                    pthread_create(&thread_, 0, &ThreadFuncWithCLinkage, base));\n            }\n            ~ThreadWithParam()\n            {\n                Join();\n            }\n\n            void Join()\n            {\n                if (!finished_) {\n                    GTEST_CHECK_POSIX_SUCCESS_(pthread_join(thread_, 0));\n                    finished_ = true;\n                }\n            }\n\n            virtual void Run()\n            {\n                if (thread_can_start_ != NULL)\n                    thread_can_start_->WaitForNotification();\n                func_(param_);\n            }\n\n        private:\n            UserThreadFunc *const func_;  // User-supplied thread function.\n            const T param_;  // User-supplied parameter to the thread function.\n            // When non-NULL, used to block execution until the controller thread\n            // notifies.\n            Notification *const thread_can_start_;\n            bool finished_;  // true iff we know that the thread function has finished.\n            pthread_t thread_;  // The native thread object.\n\n            GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam);\n        };\n# endif  // !GTEST_OS_WINDOWS && GTEST_HAS_PTHREAD ||\n        // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_\n\n# if GTEST_HAS_MUTEX_AND_THREAD_LOCAL_\n// Mutex and ThreadLocal have already been imported into the namespace.\n// Nothing to do here.\n\n# elif GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT\n\n// Mutex implements mutex on Windows platforms.  It is used in conjunction\n// with class MutexLock:\n//\n//   Mutex mutex;\n//   ...\n//   MutexLock lock(&mutex);  // Acquires the mutex and releases it at the\n//                            // end of the current scope.\n//\n// A static Mutex *must* be defined or declared using one of the following\n// macros:\n//   GTEST_DEFINE_STATIC_MUTEX_(g_some_mutex);\n//   GTEST_DECLARE_STATIC_MUTEX_(g_some_mutex);\n//\n// (A non-static Mutex is defined/declared in the usual way).\n        class GTEST_API_ Mutex\n        {\n        public:\n            enum MutexType { kStatic = 0, kDynamic = 1 };\n            // We rely on kStaticMutex being 0 as it is to what the linker initializes\n            // type_ in static mutexes.  critical_section_ will be initialized lazily\n            // in ThreadSafeLazyInit().\n            enum StaticConstructorSelector { kStaticMutex = 0 };\n\n            // This constructor intentionally does nothing.  It relies on type_ being\n            // statically initialized to 0 (effectively setting it to kStatic) and on\n            // ThreadSafeLazyInit() to lazily initialize the rest of the members.\n            explicit Mutex(StaticConstructorSelector /*dummy*/) {}\n\n            Mutex();\n            ~Mutex();\n\n            void Lock();\n\n            void Unlock();\n\n            // Does nothing if the current thread holds the mutex. Otherwise, crashes\n            // with high probability.\n            void AssertHeld();\n\n        private:\n            // Initializes owner_thread_id_ and critical_section_ in static mutexes.\n            void ThreadSafeLazyInit();\n\n            // Per http://blogs.msdn.com/b/oldnewthing/archive/2004/02/23/78395.aspx,\n            // we assume that 0 is an invalid value for thread IDs.\n            unsigned int owner_thread_id_;\n\n            // For static mutexes, we rely on these members being initialized to zeros\n            // by the linker.\n            MutexType type_;\n            long critical_section_init_phase_;  // NOLINT\n            GTEST_CRITICAL_SECTION *critical_section_;\n\n            GTEST_DISALLOW_COPY_AND_ASSIGN_(Mutex);\n        };\n\n# define GTEST_DECLARE_STATIC_MUTEX_(mutex) \\\n    extern ::testing::internal::Mutex mutex\n\n# define GTEST_DEFINE_STATIC_MUTEX_(mutex) \\\n    ::testing::internal::Mutex mutex(::testing::internal::Mutex::kStaticMutex)\n\n// We cannot name this class MutexLock because the ctor declaration would\n// conflict with a macro named MutexLock, which is defined on some\n// platforms. That macro is used as a defensive measure to prevent against\n// inadvertent misuses of MutexLock like \"MutexLock(&mu)\" rather than\n// \"MutexLock l(&mu)\".  Hence the typedef trick below.\n        class GTestMutexLock\n        {\n        public:\n            explicit GTestMutexLock(Mutex *mutex)\n                : mutex_(mutex)\n            {\n                mutex_->Lock();\n            }\n\n            ~GTestMutexLock()\n            {\n                mutex_->Unlock();\n            }\n\n        private:\n            Mutex *const mutex_;\n\n            GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestMutexLock);\n        };\n\n        typedef GTestMutexLock MutexLock;\n\n// Base class for ValueHolder<T>.  Allows a caller to hold and delete a value\n// without knowing its type.\n        class ThreadLocalValueHolderBase\n        {\n        public:\n            virtual ~ThreadLocalValueHolderBase() {}\n        };\n\n// Provides a way for a thread to send notifications to a ThreadLocal\n// regardless of its parameter type.\n        class ThreadLocalBase\n        {\n        public:\n            // Creates a new ValueHolder<T> object holding a default value passed to\n            // this ThreadLocal<T>'s constructor and returns it.  It is the caller's\n            // responsibility not to call this when the ThreadLocal<T> instance already\n            // has a value on the current thread.\n            virtual ThreadLocalValueHolderBase *NewValueForCurrentThread() const = 0;\n\n        protected:\n            ThreadLocalBase() {}\n            virtual ~ThreadLocalBase() {}\n\n        private:\n            GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocalBase);\n        };\n\n// Maps a thread to a set of ThreadLocals that have values instantiated on that\n// thread and notifies them when the thread exits.  A ThreadLocal instance is\n// expected to persist until all threads it has values on have terminated.\n        class GTEST_API_ ThreadLocalRegistry\n        {\n        public:\n            // Registers thread_local_instance as having value on the current thread.\n            // Returns a value that can be used to identify the thread from other threads.\n            static ThreadLocalValueHolderBase *GetValueOnCurrentThread(\n                const ThreadLocalBase *thread_local_instance);\n\n            // Invoked when a ThreadLocal instance is destroyed.\n            static void OnThreadLocalDestroyed(\n                const ThreadLocalBase *thread_local_instance);\n        };\n\n        class GTEST_API_ ThreadWithParamBase\n        {\n        public:\n            void Join();\n\n        protected:\n            class Runnable\n            {\n            public:\n                virtual ~Runnable() {}\n                virtual void Run() = 0;\n            };\n\n            ThreadWithParamBase(Runnable *runnable, Notification *thread_can_start);\n            virtual ~ThreadWithParamBase();\n\n        private:\n            AutoHandle thread_;\n        };\n\n// Helper class for testing Google Test's multi-threading constructs.\n        template <typename T>\n        class ThreadWithParam : public ThreadWithParamBase\n        {\n        public:\n            typedef void UserThreadFunc(T);\n\n            ThreadWithParam(UserThreadFunc *func, T param, Notification *thread_can_start)\n                : ThreadWithParamBase(new RunnableImpl(func, param), thread_can_start)\n            {\n            }\n            virtual ~ThreadWithParam() {}\n\n        private:\n            class RunnableImpl : public Runnable\n            {\n            public:\n                RunnableImpl(UserThreadFunc *func, T param)\n                    : func_(func),\n                      param_(param)\n                {\n                }\n                virtual ~RunnableImpl() {}\n                virtual void Run()\n                {\n                    func_(param_);\n                }\n\n            private:\n                UserThreadFunc *const func_;\n                const T param_;\n\n                GTEST_DISALLOW_COPY_AND_ASSIGN_(RunnableImpl);\n            };\n\n            GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadWithParam);\n        };\n\n// Implements thread-local storage on Windows systems.\n//\n//   // Thread 1\n//   ThreadLocal<int> tl(100);  // 100 is the default value for each thread.\n//\n//   // Thread 2\n//   tl.set(150);  // Changes the value for thread 2 only.\n//   EXPECT_EQ(150, tl.get());\n//\n//   // Thread 1\n//   EXPECT_EQ(100, tl.get());  // In thread 1, tl has the original value.\n//   tl.set(200);\n//   EXPECT_EQ(200, tl.get());\n//\n// The template type argument T must have a public copy constructor.\n// In addition, the default ThreadLocal constructor requires T to have\n// a public default constructor.\n//\n// The users of a TheadLocal instance have to make sure that all but one\n// threads (including the main one) using that instance have exited before\n// destroying it. Otherwise, the per-thread objects managed for them by the\n// ThreadLocal instance are not guaranteed to be destroyed on all platforms.\n//\n// Google Test only uses global ThreadLocal objects.  That means they\n// will die after main() has returned.  Therefore, no per-thread\n// object managed by Google Test will be leaked as long as all threads\n// using Google Test have exited when main() returns.\n        template <typename T>\n        class ThreadLocal : public ThreadLocalBase\n        {\n        public:\n            ThreadLocal() : default_factory_(new DefaultValueHolderFactory()) {}\n            explicit ThreadLocal(const T &value)\n                : default_factory_(new InstanceValueHolderFactory(value)) {}\n\n            ~ThreadLocal()\n            {\n                ThreadLocalRegistry::OnThreadLocalDestroyed(this);\n            }\n\n            T *pointer()\n            {\n                return GetOrCreateValue();\n            }\n            const T *pointer() const\n            {\n                return GetOrCreateValue();\n            }\n            const T &get() const\n            {\n                return *pointer();\n            }\n            void set(const T &value)\n            {\n                *pointer() = value;\n            }\n\n        private:\n            // Holds a value of T.  Can be deleted via its base class without the caller\n            // knowing the type of T.\n            class ValueHolder : public ThreadLocalValueHolderBase\n            {\n            public:\n                ValueHolder() : value_() {}\n                explicit ValueHolder(const T &value) : value_(value) {}\n\n                T *pointer()\n                {\n                    return &value_;\n                }\n\n            private:\n                T value_;\n                GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolder);\n            };\n\n\n            T *GetOrCreateValue() const\n            {\n                return static_cast<ValueHolder *>(\n                           ThreadLocalRegistry::GetValueOnCurrentThread(this))->pointer();\n            }\n\n            virtual ThreadLocalValueHolderBase *NewValueForCurrentThread() const\n            {\n                return default_factory_->MakeNewHolder();\n            }\n\n            class ValueHolderFactory\n            {\n            public:\n                ValueHolderFactory() {}\n                virtual ~ValueHolderFactory() {}\n                virtual ValueHolder *MakeNewHolder() const = 0;\n\n            private:\n                GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolderFactory);\n            };\n\n            class DefaultValueHolderFactory : public ValueHolderFactory\n            {\n            public:\n                DefaultValueHolderFactory() {}\n                virtual ValueHolder *MakeNewHolder() const\n                {\n                    return new ValueHolder();\n                }\n\n            private:\n                GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultValueHolderFactory);\n            };\n\n            class InstanceValueHolderFactory : public ValueHolderFactory\n            {\n            public:\n                explicit InstanceValueHolderFactory(const T &value) : value_(value) {}\n                virtual ValueHolder *MakeNewHolder() const\n                {\n                    return new ValueHolder(value_);\n                }\n\n            private:\n                const T value_;  // The value for each thread.\n\n                GTEST_DISALLOW_COPY_AND_ASSIGN_(InstanceValueHolderFactory);\n            };\n\n            scoped_ptr<ValueHolderFactory> default_factory_;\n\n            GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocal);\n        };\n\n# elif GTEST_HAS_PTHREAD\n\n// MutexBase and Mutex implement mutex on pthreads-based platforms.\n        class MutexBase\n        {\n        public:\n            // Acquires this mutex.\n            void Lock()\n            {\n                GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_lock(&mutex_));\n                owner_ = pthread_self();\n                has_owner_ = true;\n            }\n\n            // Releases this mutex.\n            void Unlock()\n            {\n                // Since the lock is being released the owner_ field should no longer be\n                // considered valid. We don't protect writing to has_owner_ here, as it's\n                // the caller's responsibility to ensure that the current thread holds the\n                // mutex when this is called.\n                has_owner_ = false;\n                GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_unlock(&mutex_));\n            }\n\n            // Does nothing if the current thread holds the mutex. Otherwise, crashes\n            // with high probability.\n            void AssertHeld() const\n            {\n                GTEST_CHECK_(has_owner_ && pthread_equal(owner_, pthread_self()))\n                        << \"The current thread is not holding the mutex @\" << this;\n            }\n\n            // A static mutex may be used before main() is entered.  It may even\n            // be used before the dynamic initialization stage.  Therefore we\n            // must be able to initialize a static mutex object at link time.\n            // This means MutexBase has to be a POD and its member variables\n            // have to be public.\n        public:\n            pthread_mutex_t mutex_;  // The underlying pthread mutex.\n            // has_owner_ indicates whether the owner_ field below contains a valid thread\n            // ID and is therefore safe to inspect (e.g., to use in pthread_equal()). All\n            // accesses to the owner_ field should be protected by a check of this field.\n            // An alternative might be to memset() owner_ to all zeros, but there's no\n            // guarantee that a zero'd pthread_t is necessarily invalid or even different\n            // from pthread_self().\n            bool has_owner_;\n            pthread_t owner_;  // The thread holding the mutex.\n        };\n\n// Forward-declares a static mutex.\n#  define GTEST_DECLARE_STATIC_MUTEX_(mutex) \\\n     extern ::testing::internal::MutexBase mutex\n\n// Defines and statically (i.e. at link time) initializes a static mutex.\n#  define GTEST_DEFINE_STATIC_MUTEX_(mutex) \\\n     ::testing::internal::MutexBase mutex = { PTHREAD_MUTEX_INITIALIZER, false, pthread_t() }\n\n// The Mutex class can only be used for mutexes created at runtime. It\n// shares its API with MutexBase otherwise.\n        class Mutex : public MutexBase\n        {\n        public:\n            Mutex()\n            {\n                GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_init(&mutex_, NULL));\n                has_owner_ = false;\n            }\n            ~Mutex()\n            {\n                GTEST_CHECK_POSIX_SUCCESS_(pthread_mutex_destroy(&mutex_));\n            }\n\n        private:\n            GTEST_DISALLOW_COPY_AND_ASSIGN_(Mutex);\n        };\n\n// We cannot name this class MutexLock because the ctor declaration would\n// conflict with a macro named MutexLock, which is defined on some\n// platforms. That macro is used as a defensive measure to prevent against\n// inadvertent misuses of MutexLock like \"MutexLock(&mu)\" rather than\n// \"MutexLock l(&mu)\".  Hence the typedef trick below.\n        class GTestMutexLock\n        {\n        public:\n            explicit GTestMutexLock(MutexBase *mutex)\n                : mutex_(mutex)\n            {\n                mutex_->Lock();\n            }\n\n            ~GTestMutexLock()\n            {\n                mutex_->Unlock();\n            }\n\n        private:\n            MutexBase *const mutex_;\n\n            GTEST_DISALLOW_COPY_AND_ASSIGN_(GTestMutexLock);\n        };\n\n        typedef GTestMutexLock MutexLock;\n\n// Helpers for ThreadLocal.\n\n// pthread_key_create() requires DeleteThreadLocalValue() to have\n// C-linkage.  Therefore it cannot be templatized to access\n// ThreadLocal<T>.  Hence the need for class\n// ThreadLocalValueHolderBase.\n        class ThreadLocalValueHolderBase\n        {\n        public:\n            virtual ~ThreadLocalValueHolderBase() {}\n        };\n\n// Called by pthread to delete thread-local data stored by\n// pthread_setspecific().\n        extern \"C\" inline void DeleteThreadLocalValue(void *value_holder)\n        {\n            delete static_cast<ThreadLocalValueHolderBase *>(value_holder);\n        }\n\n// Implements thread-local storage on pthreads-based systems.\n        template <typename T>\n        class ThreadLocal\n        {\n        public:\n            ThreadLocal()\n                : key_(CreateKey()), default_factory_(new DefaultValueHolderFactory()) {}\n            explicit ThreadLocal(const T &value)\n                : key_(CreateKey()),\n                  default_factory_(new InstanceValueHolderFactory(value)) {}\n\n            ~ThreadLocal()\n            {\n                // Destroys the managed object for the current thread, if any.\n                DeleteThreadLocalValue(pthread_getspecific(key_));\n\n                // Releases resources associated with the key.  This will *not*\n                // delete managed objects for other threads.\n                GTEST_CHECK_POSIX_SUCCESS_(pthread_key_delete(key_));\n            }\n\n            T *pointer()\n            {\n                return GetOrCreateValue();\n            }\n            const T *pointer() const\n            {\n                return GetOrCreateValue();\n            }\n            const T &get() const\n            {\n                return *pointer();\n            }\n            void set(const T &value)\n            {\n                *pointer() = value;\n            }\n\n        private:\n            // Holds a value of type T.\n            class ValueHolder : public ThreadLocalValueHolderBase\n            {\n            public:\n                ValueHolder() : value_() {}\n                explicit ValueHolder(const T &value) : value_(value) {}\n\n                T *pointer()\n                {\n                    return &value_;\n                }\n\n            private:\n                T value_;\n                GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolder);\n            };\n\n            static pthread_key_t CreateKey()\n            {\n                pthread_key_t key;\n                // When a thread exits, DeleteThreadLocalValue() will be called on\n                // the object managed for that thread.\n                GTEST_CHECK_POSIX_SUCCESS_(\n                    pthread_key_create(&key, &DeleteThreadLocalValue));\n                return key;\n            }\n\n            T *GetOrCreateValue() const\n            {\n                ThreadLocalValueHolderBase *const holder =\n                    static_cast<ThreadLocalValueHolderBase *>(pthread_getspecific(key_));\n                if (holder != NULL) {\n                    return CheckedDowncastToActualType<ValueHolder>(holder)->pointer();\n                }\n\n                ValueHolder *const new_holder = default_factory_->MakeNewHolder();\n                ThreadLocalValueHolderBase *const holder_base = new_holder;\n                GTEST_CHECK_POSIX_SUCCESS_(pthread_setspecific(key_, holder_base));\n                return new_holder->pointer();\n            }\n\n            class ValueHolderFactory\n            {\n            public:\n                ValueHolderFactory() {}\n                virtual ~ValueHolderFactory() {}\n                virtual ValueHolder *MakeNewHolder() const = 0;\n\n            private:\n                GTEST_DISALLOW_COPY_AND_ASSIGN_(ValueHolderFactory);\n            };\n\n            class DefaultValueHolderFactory : public ValueHolderFactory\n            {\n            public:\n                DefaultValueHolderFactory() {}\n                virtual ValueHolder *MakeNewHolder() const\n                {\n                    return new ValueHolder();\n                }\n\n            private:\n                GTEST_DISALLOW_COPY_AND_ASSIGN_(DefaultValueHolderFactory);\n            };\n\n            class InstanceValueHolderFactory : public ValueHolderFactory\n            {\n            public:\n                explicit InstanceValueHolderFactory(const T &value) : value_(value) {}\n                virtual ValueHolder *MakeNewHolder() const\n                {\n                    return new ValueHolder(value_);\n                }\n\n            private:\n                const T value_;  // The value for each thread.\n\n                GTEST_DISALLOW_COPY_AND_ASSIGN_(InstanceValueHolderFactory);\n            };\n\n            // A key pthreads uses for looking up per-thread values.\n            const pthread_key_t key_;\n            scoped_ptr<ValueHolderFactory> default_factory_;\n\n            GTEST_DISALLOW_COPY_AND_ASSIGN_(ThreadLocal);\n        };\n\n# endif  // GTEST_HAS_MUTEX_AND_THREAD_LOCAL_\n\n#else  // GTEST_IS_THREADSAFE\n\n// A dummy implementation of synchronization primitives (mutex, lock,\n// and thread-local variable).  Necessary for compiling Google Test where\n// mutex is not supported - using Google Test in multiple threads is not\n// supported on such platforms.\n\n        class Mutex\n        {\n        public:\n            Mutex() {}\n            void Lock() {}\n            void Unlock() {}\n            void AssertHeld() const {}\n        };\n\n# define GTEST_DECLARE_STATIC_MUTEX_(mutex) \\\n  extern ::testing::internal::Mutex mutex\n\n# define GTEST_DEFINE_STATIC_MUTEX_(mutex) ::testing::internal::Mutex mutex\n\n// We cannot name this class MutexLock because the ctor declaration would\n// conflict with a macro named MutexLock, which is defined on some\n// platforms. That macro is used as a defensive measure to prevent against\n// inadvertent misuses of MutexLock like \"MutexLock(&mu)\" rather than\n// \"MutexLock l(&mu)\".  Hence the typedef trick below.\n        class GTestMutexLock\n        {\n        public:\n            explicit GTestMutexLock(Mutex *) {} // NOLINT\n        };\n\n        typedef GTestMutexLock MutexLock;\n\n        template <typename T>\n        class ThreadLocal\n        {\n        public:\n            ThreadLocal() : value_() {}\n            explicit ThreadLocal(const T &value) : value_(value) {}\n            T *pointer()\n            {\n                return &value_;\n            }\n            const T *pointer() const\n            {\n                return &value_;\n            }\n            const T &get() const\n            {\n                return value_;\n            }\n            void set(const T &value)\n            {\n                value_ = value;\n            }\n        private:\n            T value_;\n        };\n\n#endif  // GTEST_IS_THREADSAFE\n\n// Returns the number of threads running in the process, or 0 to indicate that\n// we cannot detect it.\n        GTEST_API_ size_t GetThreadCount();\n\n// Passing non-POD classes through ellipsis (...) crashes the ARM\n// compiler and generates a warning in Sun Studio.  The Nokia Symbian\n// and the IBM XL C/C++ compiler try to instantiate a copy constructor\n// for objects passed through ellipsis (...), failing for uncopyable\n// objects.  We define this to ensure that only POD is passed through\n// ellipsis on these systems.\n#if defined(__SYMBIAN32__) || defined(__IBMCPP__) || defined(__SUNPRO_CC)\n// We lose support for NULL detection where the compiler doesn't like\n// passing non-POD classes through ellipsis (...).\n# define GTEST_ELLIPSIS_NEEDS_POD_ 1\n#else\n# define GTEST_CAN_COMPARE_NULL 1\n#endif\n\n// The Nokia Symbian and IBM XL C/C++ compilers cannot decide between\n// const T& and const T* in a function template.  These compilers\n// _can_ decide between class template specializations for T and T*,\n// so a tr1::type_traits-like is_pointer works.\n#if defined(__SYMBIAN32__) || defined(__IBMCPP__)\n# define GTEST_NEEDS_IS_POINTER_ 1\n#endif\n\n        template <bool bool_value>\n        struct bool_constant {\n            typedef bool_constant<bool_value> type;\n            static const bool value = bool_value;\n        };\n        template <bool bool_value> const bool bool_constant<bool_value>::value;\n\n        typedef bool_constant<false> false_type;\n        typedef bool_constant<true> true_type;\n\n        template <typename T>\n        struct is_pointer : public false_type {};\n\n        template <typename T>\n        struct is_pointer<T *> : public true_type {};\n\n        template <typename Iterator>\n        struct IteratorTraits {\n            typedef typename Iterator::value_type value_type;\n        };\n\n        template <typename T>\n        struct IteratorTraits<T *> {\n            typedef T value_type;\n        };\n\n        template <typename T>\n        struct IteratorTraits<const T *> {\n            typedef T value_type;\n        };\n\n#if GTEST_OS_WINDOWS\n# define GTEST_PATH_SEP_ \"\\\\\"\n# define GTEST_HAS_ALT_PATH_SEP_ 1\n// The biggest signed integer type the compiler supports.\n        typedef __int64 BiggestInt;\n#else\n# define GTEST_PATH_SEP_ \"/\"\n# define GTEST_HAS_ALT_PATH_SEP_ 0\n        typedef long long BiggestInt;  // NOLINT\n#endif  // GTEST_OS_WINDOWS\n\n// Utilities for char.\n\n// isspace(int ch) and friends accept an unsigned char or EOF.  char\n// may be signed, depending on the compiler (or compiler flags).\n// Therefore we need to cast a char to unsigned char before calling\n// isspace(), etc.\n\n        inline bool IsAlpha(char ch)\n        {\n            return isalpha(static_cast<unsigned char>(ch)) != 0;\n        }\n        inline bool IsAlNum(char ch)\n        {\n            return isalnum(static_cast<unsigned char>(ch)) != 0;\n        }\n        inline bool IsDigit(char ch)\n        {\n            return isdigit(static_cast<unsigned char>(ch)) != 0;\n        }\n        inline bool IsLower(char ch)\n        {\n            return islower(static_cast<unsigned char>(ch)) != 0;\n        }\n        inline bool IsSpace(char ch)\n        {\n            return isspace(static_cast<unsigned char>(ch)) != 0;\n        }\n        inline bool IsUpper(char ch)\n        {\n            return isupper(static_cast<unsigned char>(ch)) != 0;\n        }\n        inline bool IsXDigit(char ch)\n        {\n            return isxdigit(static_cast<unsigned char>(ch)) != 0;\n        }\n        inline bool IsXDigit(wchar_t ch)\n        {\n            const unsigned char low_byte = static_cast<unsigned char>(ch);\n            return ch == low_byte && isxdigit(low_byte) != 0;\n        }\n\n        inline char ToLower(char ch)\n        {\n            return static_cast<char>(tolower(static_cast<unsigned char>(ch)));\n        }\n        inline char ToUpper(char ch)\n        {\n            return static_cast<char>(toupper(static_cast<unsigned char>(ch)));\n        }\n\n        inline std::string StripTrailingSpaces(std::string str)\n        {\n            std::string::iterator it = str.end();\n            while (it != str.begin() && IsSpace(*--it))\n                it = str.erase(it);\n            return str;\n        }\n\n// The testing::internal::posix namespace holds wrappers for common\n// POSIX functions.  These wrappers hide the differences between\n// Windows/MSVC and POSIX systems.  Since some compilers define these\n// standard functions as macros, the wrapper cannot have the same name\n// as the wrapped function.\n\n        namespace posix\n        {\n\n// Functions with a different name on Windows.\n\n#if GTEST_OS_WINDOWS\n\n            typedef struct _stat StatStruct;\n\n# ifdef __BORLANDC__\n            inline int IsATTY(int fd)\n            {\n                return isatty(fd);\n            }\n            inline int StrCaseCmp(const char *s1, const char *s2)\n            {\n                return stricmp(s1, s2);\n            }\n            inline char *StrDup(const char *src)\n            {\n                return strdup(src);\n            }\n# else  // !__BORLANDC__\n#  if GTEST_OS_WINDOWS_MOBILE\n            inline int IsATTY(int /* fd */)\n            {\n                return 0;\n            }\n#  else\n            inline int IsATTY(int fd)\n            {\n                return _isatty(fd);\n            }\n#  endif  // GTEST_OS_WINDOWS_MOBILE\n            inline int StrCaseCmp(const char *s1, const char *s2)\n            {\n                return _stricmp(s1, s2);\n            }\n            inline char *StrDup(const char *src)\n            {\n                return _strdup(src);\n            }\n# endif  // __BORLANDC__\n\n# if GTEST_OS_WINDOWS_MOBILE\n            inline int FileNo(FILE *file)\n            {\n                return reinterpret_cast<int>(_fileno(file));\n            }\n// Stat(), RmDir(), and IsDir() are not needed on Windows CE at this\n// time and thus not defined there.\n# else\n            inline int FileNo(FILE *file)\n            {\n                return _fileno(file);\n            }\n            inline int Stat(const char *path, StatStruct *buf)\n            {\n                return _stat(path, buf);\n            }\n            inline int RmDir(const char *dir)\n            {\n                return _rmdir(dir);\n            }\n            inline bool IsDir(const StatStruct &st)\n            {\n                return (_S_IFDIR & st.st_mode) != 0;\n            }\n# endif  // GTEST_OS_WINDOWS_MOBILE\n\n#else\n\n            typedef struct stat StatStruct;\n\n            inline int FileNo(FILE *file)\n            {\n                return fileno(file);\n            }\n            inline int IsATTY(int fd)\n            {\n                return isatty(fd);\n            }\n            inline int Stat(const char *path, StatStruct *buf)\n            {\n                return stat(path, buf);\n            }\n            inline int StrCaseCmp(const char *s1, const char *s2)\n            {\n                return strcasecmp(s1, s2);\n            }\n            inline char *StrDup(const char *src)\n            {\n                return strdup(src);\n            }\n            inline int RmDir(const char *dir)\n            {\n                return rmdir(dir);\n            }\n            inline bool IsDir(const StatStruct &st)\n            {\n                return S_ISDIR(st.st_mode);\n            }\n\n#endif  // GTEST_OS_WINDOWS\n\n// Functions deprecated by MSVC 8.0.\n\n            GTEST_DISABLE_MSC_WARNINGS_PUSH_(4996 /* deprecated function */)\n\n            inline const char *StrNCpy(char *dest, const char *src, size_t n)\n            {\n                return strncpy(dest, src, n);\n            }\n\n// ChDir(), FReopen(), FDOpen(), Read(), Write(), Close(), and\n// StrError() aren't needed on Windows CE at this time and thus not\n// defined there.\n\n#if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RT\n            inline int ChDir(const char *dir)\n            {\n                return chdir(dir);\n            }\n#endif\n            inline FILE *FOpen(const char *path, const char *mode)\n            {\n                return fopen(path, mode);\n            }\n#if !GTEST_OS_WINDOWS_MOBILE\n            inline FILE *FReopen(const char *path, const char *mode, FILE *stream)\n            {\n                return freopen(path, mode, stream);\n            }\n            inline FILE *FDOpen(int fd, const char *mode)\n            {\n                return fdopen(fd, mode);\n            }\n#endif\n            inline int FClose(FILE *fp)\n            {\n                return fclose(fp);\n            }\n#if !GTEST_OS_WINDOWS_MOBILE\n            inline int Read(int fd, void *buf, unsigned int count)\n            {\n                return static_cast<int>(read(fd, buf, count));\n            }\n            inline int Write(int fd, const void *buf, unsigned int count)\n            {\n                return static_cast<int>(write(fd, buf, count));\n            }\n            inline int Close(int fd)\n            {\n                return close(fd);\n            }\n            inline const char *StrError(int errnum)\n            {\n                return strerror(errnum);\n            }\n#endif\n            inline const char *GetEnv(const char *name)\n            {\n#if GTEST_OS_WINDOWS_MOBILE || GTEST_OS_WINDOWS_PHONE | GTEST_OS_WINDOWS_RT\n                // We are on Windows CE, which has no environment variables.\n                static_cast<void>(name);  // To prevent 'unused argument' warning.\n                return NULL;\n#elif defined(__BORLANDC__) || defined(__SunOS_5_8) || defined(__SunOS_5_9)\n                // Environment variables which we programmatically clear will be set to the\n                // empty string rather than unset (NULL).  Handle that case.\n                const char *const env = getenv(name);\n                return (env != NULL && env[0] != '\\0') ? env : NULL;\n#else\n                return getenv(name);\n#endif\n            }\n\n            GTEST_DISABLE_MSC_WARNINGS_POP_()\n\n#if GTEST_OS_WINDOWS_MOBILE\n// Windows CE has no C library. The abort() function is used in\n// several places in Google Test. This implementation provides a reasonable\n// imitation of standard behaviour.\n            void Abort();\n#else\n            inline void Abort()\n            {\n                abort();\n            }\n#endif  // GTEST_OS_WINDOWS_MOBILE\n\n        }  // namespace posix\n\n// MSVC \"deprecates\" snprintf and issues warnings wherever it is used.  In\n// order to avoid these warnings, we need to use _snprintf or _snprintf_s on\n// MSVC-based platforms.  We map the GTEST_SNPRINTF_ macro to the appropriate\n// function in order to achieve that.  We use macro definition here because\n// snprintf is a variadic function.\n#if _MSC_VER >= 1400 && !GTEST_OS_WINDOWS_MOBILE\n// MSVC 2005 and above support variadic macros.\n# define GTEST_SNPRINTF_(buffer, size, format, ...) \\\n     _snprintf_s(buffer, size, size, format, __VA_ARGS__)\n#elif defined(_MSC_VER)\n// Windows CE does not define _snprintf_s and MSVC prior to 2005 doesn't\n// complain about _snprintf.\n# define GTEST_SNPRINTF_ _snprintf\n#else\n# define GTEST_SNPRINTF_ snprintf\n#endif\n\n// The maximum number a BiggestInt can represent.  This definition\n// works no matter BiggestInt is represented in one's complement or\n// two's complement.\n//\n// We cannot rely on numeric_limits in STL, as __int64 and long long\n// are not part of standard C++ and numeric_limits doesn't need to be\n// defined for them.\n        const BiggestInt kMaxBiggestInt =\n            ~(static_cast<BiggestInt>(1) << (8*sizeof(BiggestInt) - 1));\n\n// This template class serves as a compile-time function from size to\n// type.  It maps a size in bytes to a primitive type with that\n// size. e.g.\n//\n//   TypeWithSize<4>::UInt\n//\n// is typedef-ed to be unsigned int (unsigned integer made up of 4\n// bytes).\n//\n// Such functionality should belong to STL, but I cannot find it\n// there.\n//\n// Google Test uses this class in the implementation of floating-point\n// comparison.\n//\n// For now it only handles UInt (unsigned int) as that's all Google Test\n// needs.  Other types can be easily added in the future if need\n// arises.\n        template <size_t size>\n        class TypeWithSize\n        {\n        public:\n            // This prevents the user from using TypeWithSize<N> with incorrect\n            // values of N.\n            typedef void UInt;\n        };\n\n// The specialization for size 4.\n        template <>\n        class TypeWithSize<4>\n        {\n        public:\n            // unsigned int has size 4 in both gcc and MSVC.\n            //\n            // As base/basictypes.h doesn't compile on Windows, we cannot use\n            // uint32, uint64, and etc here.\n            typedef int Int;\n            typedef unsigned int UInt;\n        };\n\n// The specialization for size 8.\n        template <>\n        class TypeWithSize<8>\n        {\n        public:\n#if GTEST_OS_WINDOWS\n            typedef __int64 Int;\n            typedef unsigned __int64 UInt;\n#else\n            typedef long long Int;  // NOLINT\n            typedef unsigned long long UInt;  // NOLINT\n#endif  // GTEST_OS_WINDOWS\n        };\n\n// Integer types of known sizes.\n        typedef TypeWithSize<4>::Int Int32;\n        typedef TypeWithSize<4>::UInt UInt32;\n        typedef TypeWithSize<8>::Int Int64;\n        typedef TypeWithSize<8>::UInt UInt64;\n        typedef TypeWithSize<8>::Int TimeInMillis;  // Represents time in milliseconds.\n\n// Utilities for command line flags and environment variables.\n\n// Macro for referencing flags.\n#if !defined(GTEST_FLAG)\n# define GTEST_FLAG(name) FLAGS_gtest_##name\n#endif  // !defined(GTEST_FLAG)\n\n#if !defined(GTEST_USE_OWN_FLAGFILE_FLAG_)\n# define GTEST_USE_OWN_FLAGFILE_FLAG_ 1\n#endif  // !defined(GTEST_USE_OWN_FLAGFILE_FLAG_)\n\n#if !defined(GTEST_DECLARE_bool_)\n# define GTEST_FLAG_SAVER_ ::testing::internal::GTestFlagSaver\n\n// Macros for declaring flags.\n# define GTEST_DECLARE_bool_(name) GTEST_API_ extern bool GTEST_FLAG(name)\n# define GTEST_DECLARE_int32_(name) \\\n    GTEST_API_ extern ::testing::internal::Int32 GTEST_FLAG(name)\n#define GTEST_DECLARE_string_(name) \\\n    GTEST_API_ extern ::std::string GTEST_FLAG(name)\n\n// Macros for defining flags.\n#define GTEST_DEFINE_bool_(name, default_val, doc) \\\n    GTEST_API_ bool GTEST_FLAG(name) = (default_val)\n#define GTEST_DEFINE_int32_(name, default_val, doc) \\\n    GTEST_API_ ::testing::internal::Int32 GTEST_FLAG(name) = (default_val)\n#define GTEST_DEFINE_string_(name, default_val, doc) \\\n    GTEST_API_ ::std::string GTEST_FLAG(name) = (default_val)\n\n#endif  // !defined(GTEST_DECLARE_bool_)\n\n// Thread annotations\n#if !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_)\n# define GTEST_EXCLUSIVE_LOCK_REQUIRED_(locks)\n# define GTEST_LOCK_EXCLUDED_(locks)\n#endif  // !defined(GTEST_EXCLUSIVE_LOCK_REQUIRED_)\n\n// Parses 'str' for a 32-bit signed integer.  If successful, writes the result\n// to *value and returns true; otherwise leaves *value unchanged and returns\n// false.\n// TODO(chandlerc): Find a better way to refactor flag and environment parsing\n// out of both gtest-port.cc and gtest.cc to avoid exporting this utility\n// function.\n        bool ParseInt32(const Message &src_text, const char *str, Int32 *value);\n\n// Parses a bool/Int32/string from the environment variable\n// corresponding to the given Google Test flag.\n        bool BoolFromGTestEnv(const char *flag, bool default_val);\n        GTEST_API_ Int32 Int32FromGTestEnv(const char *flag, Int32 default_val);\n        std::string StringFromGTestEnv(const char *flag, const char *default_val);\n\n    }  // namespace internal\n\n// Returns a path to temporary directory.\n// Tries to determine an appropriate directory for the platform.\n    GTEST_API_ std::string TempDir();\n\n}  // namespace testing\n\n#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_\n\n#if GTEST_OS_LINUX\n# include <stdlib.h>\n# include <sys/types.h>\n# include <sys/wait.h>\n# include <unistd.h>\n#endif  // GTEST_OS_LINUX\n\n#if GTEST_HAS_EXCEPTIONS\n# include <stdexcept>\n#endif\n\n#include <ctype.h>\n#include <float.h>\n#include <string.h>\n#include <iomanip>\n#include <limits>\n#include <map>\n#include <set>\n#include <string>\n#include <vector>\n\n// Copyright 2005, Google Inc.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Author: wan@google.com (Zhanyong Wan)\n//\n// The Google C++ Testing Framework (Google Test)\n//\n// This header file defines the Message class.\n//\n// IMPORTANT NOTE: Due to limitation of the C++ language, we have to\n// leave some internal implementation details in this header file.\n// They are clearly marked by comments like this:\n//\n//   // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.\n//\n// Such code is NOT meant to be used by a user directly, and is subject\n// to CHANGE WITHOUT NOTICE.  Therefore DO NOT DEPEND ON IT in a user\n// program!\n\n#ifndef GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_\n#define GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_\n\n#include <limits>\n\n\n// Ensures that there is at least one operator<< in the global namespace.\n// See Message& operator<<(...) below for why.\nvoid operator<<(const testing::internal::Secret &, int);\n\nnamespace testing\n{\n\n// The Message class works like an ostream repeater.\n//\n// Typical usage:\n//\n//   1. You stream a bunch of values to a Message object.\n//      It will remember the text in a stringstream.\n//   2. Then you stream the Message object to an ostream.\n//      This causes the text in the Message to be streamed\n//      to the ostream.\n//\n// For example;\n//\n//   testing::Message foo;\n//   foo << 1 << \" != \" << 2;\n//   std::cout << foo;\n//\n// will print \"1 != 2\".\n//\n// Message is not intended to be inherited from.  In particular, its\n// destructor is not virtual.\n//\n// Note that stringstream behaves differently in gcc and in MSVC.  You\n// can stream a NULL char pointer to it in the former, but not in the\n// latter (it causes an access violation if you do).  The Message\n// class hides this difference by treating a NULL char pointer as\n// \"(null)\".\n    class GTEST_API_ Message\n    {\n    private:\n        // The type of basic IO manipulators (endl, ends, and flush) for\n        // narrow streams.\n        typedef std::ostream &(*BasicNarrowIoManip)(std::ostream &);\n\n    public:\n        // Constructs an empty Message.\n        Message();\n\n        // Copy constructor.\n        Message(const Message &msg) : ss_(new ::std::stringstream)    // NOLINT\n        {\n            *ss_ << msg.GetString();\n        }\n\n        // Constructs a Message from a C-string.\n        explicit Message(const char *str) : ss_(new ::std::stringstream)\n        {\n            *ss_ << str;\n        }\n\n#if GTEST_OS_SYMBIAN\n        // Streams a value (either a pointer or not) to this object.\n        template <typename T>\n        inline Message &operator <<(const T &value)\n        {\n            StreamHelper(typename internal::is_pointer<T>::type(), value);\n            return *this;\n        }\n#else\n        // Streams a non-pointer value to this object.\n        template <typename T>\n        inline Message &operator <<(const T &val)\n        {\n            // Some libraries overload << for STL containers.  These\n            // overloads are defined in the global namespace instead of ::std.\n            //\n            // C++'s symbol lookup rule (i.e. Koenig lookup) says that these\n            // overloads are visible in either the std namespace or the global\n            // namespace, but not other namespaces, including the testing\n            // namespace which Google Test's Message class is in.\n            //\n            // To allow STL containers (and other types that has a << operator\n            // defined in the global namespace) to be used in Google Test\n            // assertions, testing::Message must access the custom << operator\n            // from the global namespace.  With this using declaration,\n            // overloads of << defined in the global namespace and those\n            // visible via Koenig lookup are both exposed in this function.\n            using ::operator <<;\n            *ss_ << val;\n            return *this;\n        }\n\n        // Streams a pointer value to this object.\n        //\n        // This function is an overload of the previous one.  When you\n        // stream a pointer to a Message, this definition will be used as it\n        // is more specialized.  (The C++ Standard, section\n        // [temp.func.order].)  If you stream a non-pointer, then the\n        // previous definition will be used.\n        //\n        // The reason for this overload is that streaming a NULL pointer to\n        // ostream is undefined behavior.  Depending on the compiler, you\n        // may get \"0\", \"(nil)\", \"(null)\", or an access violation.  To\n        // ensure consistent result across compilers, we always treat NULL\n        // as \"(null)\".\n        template <typename T>\n        inline Message &operator <<(T *const &pointer)    // NOLINT\n        {\n            if (pointer == NULL) {\n                *ss_ << \"(null)\";\n            } else {\n                *ss_ << pointer;\n            }\n            return *this;\n        }\n#endif  // GTEST_OS_SYMBIAN\n\n        // Since the basic IO manipulators are overloaded for both narrow\n        // and wide streams, we have to provide this specialized definition\n        // of operator <<, even though its body is the same as the\n        // templatized version above.  Without this definition, streaming\n        // endl or other basic IO manipulators to Message will confuse the\n        // compiler.\n        Message &operator <<(BasicNarrowIoManip val)\n        {\n            *ss_ << val;\n            return *this;\n        }\n\n        // Instead of 1/0, we want to see true/false for bool values.\n        Message &operator <<(bool b)\n        {\n            return *this << (b ? \"true\" : \"false\");\n        }\n\n        // These two overloads allow streaming a wide C string to a Message\n        // using the UTF-8 encoding.\n        Message &operator <<(const wchar_t *wide_c_str);\n        Message &operator <<(wchar_t *wide_c_str);\n\n#if GTEST_HAS_STD_WSTRING\n        // Converts the given wide string to a narrow string using the UTF-8\n        // encoding, and streams the result to this Message object.\n        Message &operator <<(const ::std::wstring &wstr);\n#endif  // GTEST_HAS_STD_WSTRING\n\n#if GTEST_HAS_GLOBAL_WSTRING\n        // Converts the given wide string to a narrow string using the UTF-8\n        // encoding, and streams the result to this Message object.\n        Message &operator <<(const ::wstring &wstr);\n#endif  // GTEST_HAS_GLOBAL_WSTRING\n\n        // Gets the text streamed to this object so far as an std::string.\n        // Each '\\0' character in the buffer is replaced with \"\\\\0\".\n        //\n        // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.\n        std::string GetString() const;\n\n    private:\n\n#if GTEST_OS_SYMBIAN\n        // These are needed as the Nokia Symbian Compiler cannot decide between\n        // const T& and const T* in a function template. The Nokia compiler _can_\n        // decide between class template specializations for T and T*, so a\n        // tr1::type_traits-like is_pointer works, and we can overload on that.\n        template <typename T>\n        inline void StreamHelper(internal::true_type /*is_pointer*/, T *pointer)\n        {\n            if (pointer == NULL) {\n                *ss_ << \"(null)\";\n            } else {\n                *ss_ << pointer;\n            }\n        }\n        template <typename T>\n        inline void StreamHelper(internal::false_type /*is_pointer*/,\n                                 const T &value)\n        {\n            // See the comments in Message& operator <<(const T&) above for why\n            // we need this using statement.\n            using ::operator <<;\n            *ss_ << value;\n        }\n#endif  // GTEST_OS_SYMBIAN\n\n        // We'll hold the text streamed to this object here.\n        const internal::scoped_ptr< ::std::stringstream> ss_;\n\n        // We declare (but don't implement) this to prevent the compiler\n        // from implementing the assignment operator.\n        void operator=(const Message &);\n    };\n\n// Streams a Message to an ostream.\n    inline std::ostream &operator <<(std::ostream &os, const Message &sb)\n    {\n        return os << sb.GetString();\n    }\n\n    namespace internal\n    {\n\n// Converts a streamable value to an std::string.  A NULL pointer is\n// converted to \"(null)\".  When the input value is a ::string,\n// ::std::string, ::wstring, or ::std::wstring object, each NUL\n// character in it is replaced with \"\\\\0\".\n        template <typename T>\n        std::string StreamableToString(const T &streamable)\n        {\n            return (Message() << streamable).GetString();\n        }\n\n    }  // namespace internal\n}  // namespace testing\n\n#endif  // GTEST_INCLUDE_GTEST_GTEST_MESSAGE_H_\n// Copyright 2005, Google Inc.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee)\n//\n// The Google C++ Testing Framework (Google Test)\n//\n// This header file declares the String class and functions used internally by\n// Google Test.  They are subject to change without notice. They should not used\n// by code external to Google Test.\n//\n// This header file is #included by <gtest/internal/gtest-internal.h>.\n// It should not be #included by other files.\n\n#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_\n#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_\n\n#ifdef __BORLANDC__\n// string.h is not guaranteed to provide strcpy on C++ Builder.\n# include <mem.h>\n#endif\n\n#include <string.h>\n#include <string>\n\n\nnamespace testing\n{\n    namespace internal\n    {\n\n// String - an abstract class holding static string utilities.\n        class GTEST_API_ String\n        {\n        public:\n            // Static utility methods\n\n            // Clones a 0-terminated C string, allocating memory using new.  The\n            // caller is responsible for deleting the return value using\n            // delete[].  Returns the cloned string, or NULL if the input is\n            // NULL.\n            //\n            // This is different from strdup() in string.h, which allocates\n            // memory using malloc().\n            static const char *CloneCString(const char *c_str);\n\n#if GTEST_OS_WINDOWS_MOBILE\n            // Windows CE does not have the 'ANSI' versions of Win32 APIs. To be\n            // able to pass strings to Win32 APIs on CE we need to convert them\n            // to 'Unicode', UTF-16.\n\n            // Creates a UTF-16 wide string from the given ANSI string, allocating\n            // memory using new. The caller is responsible for deleting the return\n            // value using delete[]. Returns the wide string, or NULL if the\n            // input is NULL.\n            //\n            // The wide string is created using the ANSI codepage (CP_ACP) to\n            // match the behaviour of the ANSI versions of Win32 calls and the\n            // C runtime.\n            static LPCWSTR AnsiToUtf16(const char *c_str);\n\n            // Creates an ANSI string from the given wide string, allocating\n            // memory using new. The caller is responsible for deleting the return\n            // value using delete[]. Returns the ANSI string, or NULL if the\n            // input is NULL.\n            //\n            // The returned string is created using the ANSI codepage (CP_ACP) to\n            // match the behaviour of the ANSI versions of Win32 calls and the\n            // C runtime.\n            static const char *Utf16ToAnsi(LPCWSTR utf16_str);\n#endif\n\n            // Compares two C strings.  Returns true iff they have the same content.\n            //\n            // Unlike strcmp(), this function can handle NULL argument(s).  A\n            // NULL C string is considered different to any non-NULL C string,\n            // including the empty string.\n            static bool CStringEquals(const char *lhs, const char *rhs);\n\n            // Converts a wide C string to a String using the UTF-8 encoding.\n            // NULL will be converted to \"(null)\".  If an error occurred during\n            // the conversion, \"(failed to convert from wide string)\" is\n            // returned.\n            static std::string ShowWideCString(const wchar_t *wide_c_str);\n\n            // Compares two wide C strings.  Returns true iff they have the same\n            // content.\n            //\n            // Unlike wcscmp(), this function can handle NULL argument(s).  A\n            // NULL C string is considered different to any non-NULL C string,\n            // including the empty string.\n            static bool WideCStringEquals(const wchar_t *lhs, const wchar_t *rhs);\n\n            // Compares two C strings, ignoring case.  Returns true iff they\n            // have the same content.\n            //\n            // Unlike strcasecmp(), this function can handle NULL argument(s).\n            // A NULL C string is considered different to any non-NULL C string,\n            // including the empty string.\n            static bool CaseInsensitiveCStringEquals(const char *lhs,\n                                                     const char *rhs);\n\n            // Compares two wide C strings, ignoring case.  Returns true iff they\n            // have the same content.\n            //\n            // Unlike wcscasecmp(), this function can handle NULL argument(s).\n            // A NULL C string is considered different to any non-NULL wide C string,\n            // including the empty string.\n            // NB: The implementations on different platforms slightly differ.\n            // On windows, this method uses _wcsicmp which compares according to LC_CTYPE\n            // environment variable. On GNU platform this method uses wcscasecmp\n            // which compares according to LC_CTYPE category of the current locale.\n            // On MacOS X, it uses towlower, which also uses LC_CTYPE category of the\n            // current locale.\n            static bool CaseInsensitiveWideCStringEquals(const wchar_t *lhs,\n                                                         const wchar_t *rhs);\n\n            // Returns true iff the given string ends with the given suffix, ignoring\n            // case. Any string is considered to end with an empty suffix.\n            static bool EndsWithCaseInsensitive(\n                const std::string &str, const std::string &suffix);\n\n            // Formats an int value as \"%02d\".\n            static std::string FormatIntWidth2(int value);  // \"%02d\" for width == 2\n\n            // Formats an int value as \"%X\".\n            static std::string FormatHexInt(int value);\n\n            // Formats a byte as \"%02X\".\n            static std::string FormatByte(unsigned char value);\n\n        private:\n            String();  // Not meant to be instantiated.\n        };  // class String\n\n// Gets the content of the stringstream's buffer as an std::string.  Each '\\0'\n// character in the buffer is replaced with \"\\\\0\".\n        GTEST_API_ std::string StringStreamToString(::std::stringstream *stream);\n\n    }  // namespace internal\n}  // namespace testing\n\n#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_STRING_H_\n// Copyright 2008, Google Inc.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Author: keith.ray@gmail.com (Keith Ray)\n//\n// Google Test filepath utilities\n//\n// This header file declares classes and functions used internally by\n// Google Test.  They are subject to change without notice.\n//\n// This file is #included in <gtest/internal/gtest-internal.h>.\n// Do not include this header file separately!\n\n#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_\n#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_\n\n\nnamespace testing\n{\n    namespace internal\n    {\n\n// FilePath - a class for file and directory pathname manipulation which\n// handles platform-specific conventions (like the pathname separator).\n// Used for helper functions for naming files in a directory for xml output.\n// Except for Set methods, all methods are const or static, which provides an\n// \"immutable value object\" -- useful for peace of mind.\n// A FilePath with a value ending in a path separator (\"like/this/\") represents\n// a directory, otherwise it is assumed to represent a file. In either case,\n// it may or may not represent an actual file or directory in the file system.\n// Names are NOT checked for syntax correctness -- no checking for illegal\n// characters, malformed paths, etc.\n\n        class GTEST_API_ FilePath\n        {\n        public:\n            FilePath() : pathname_(\"\") { }\n            FilePath(const FilePath &rhs) : pathname_(rhs.pathname_) { }\n\n            explicit FilePath(const std::string &pathname) : pathname_(pathname)\n            {\n                Normalize();\n            }\n\n            FilePath &operator=(const FilePath &rhs)\n            {\n                Set(rhs);\n                return *this;\n            }\n\n            void Set(const FilePath &rhs)\n            {\n                pathname_ = rhs.pathname_;\n            }\n\n            const std::string &string() const\n            {\n                return pathname_;\n            }\n            const char *c_str() const\n            {\n                return pathname_.c_str();\n            }\n\n            // Returns the current working directory, or \"\" if unsuccessful.\n            static FilePath GetCurrentDir();\n\n            // Given directory = \"dir\", base_name = \"test\", number = 0,\n            // extension = \"xml\", returns \"dir/test.xml\". If number is greater\n            // than zero (e.g., 12), returns \"dir/test_12.xml\".\n            // On Windows platform, uses \\ as the separator rather than /.\n            static FilePath MakeFileName(const FilePath &directory,\n                                         const FilePath &base_name,\n                                         int number,\n                                         const char *extension);\n\n            // Given directory = \"dir\", relative_path = \"test.xml\",\n            // returns \"dir/test.xml\".\n            // On Windows, uses \\ as the separator rather than /.\n            static FilePath ConcatPaths(const FilePath &directory,\n                                        const FilePath &relative_path);\n\n            // Returns a pathname for a file that does not currently exist. The pathname\n            // will be directory/base_name.extension or\n            // directory/base_name_<number>.extension if directory/base_name.extension\n            // already exists. The number will be incremented until a pathname is found\n            // that does not already exist.\n            // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'.\n            // There could be a race condition if two or more processes are calling this\n            // function at the same time -- they could both pick the same filename.\n            static FilePath GenerateUniqueFileName(const FilePath &directory,\n                                                   const FilePath &base_name,\n                                                   const char *extension);\n\n            // Returns true iff the path is \"\".\n            bool IsEmpty() const\n            {\n                return pathname_.empty();\n            }\n\n            // If input name has a trailing separator character, removes it and returns\n            // the name, otherwise return the name string unmodified.\n            // On Windows platform, uses \\ as the separator, other platforms use /.\n            FilePath RemoveTrailingPathSeparator() const;\n\n            // Returns a copy of the FilePath with the directory part removed.\n            // Example: FilePath(\"path/to/file\").RemoveDirectoryName() returns\n            // FilePath(\"file\"). If there is no directory part (\"just_a_file\"), it returns\n            // the FilePath unmodified. If there is no file part (\"just_a_dir/\") it\n            // returns an empty FilePath (\"\").\n            // On Windows platform, '\\' is the path separator, otherwise it is '/'.\n            FilePath RemoveDirectoryName() const;\n\n            // RemoveFileName returns the directory path with the filename removed.\n            // Example: FilePath(\"path/to/file\").RemoveFileName() returns \"path/to/\".\n            // If the FilePath is \"a_file\" or \"/a_file\", RemoveFileName returns\n            // FilePath(\"./\") or, on Windows, FilePath(\".\\\\\"). If the filepath does\n            // not have a file, like \"just/a/dir/\", it returns the FilePath unmodified.\n            // On Windows platform, '\\' is the path separator, otherwise it is '/'.\n            FilePath RemoveFileName() const;\n\n            // Returns a copy of the FilePath with the case-insensitive extension removed.\n            // Example: FilePath(\"dir/file.exe\").RemoveExtension(\"EXE\") returns\n            // FilePath(\"dir/file\"). If a case-insensitive extension is not\n            // found, returns a copy of the original FilePath.\n            FilePath RemoveExtension(const char *extension) const;\n\n            // Creates directories so that path exists. Returns true if successful or if\n            // the directories already exist; returns false if unable to create\n            // directories for any reason. Will also return false if the FilePath does\n            // not represent a directory (that is, it doesn't end with a path separator).\n            bool CreateDirectoriesRecursively() const;\n\n            // Create the directory so that path exists. Returns true if successful or\n            // if the directory already exists; returns false if unable to create the\n            // directory for any reason, including if the parent directory does not\n            // exist. Not named \"CreateDirectory\" because that's a macro on Windows.\n            bool CreateFolder() const;\n\n            // Returns true if FilePath describes something in the file-system,\n            // either a file, directory, or whatever, and that something exists.\n            bool FileOrDirectoryExists() const;\n\n            // Returns true if pathname describes a directory in the file-system\n            // that exists.\n            bool DirectoryExists() const;\n\n            // Returns true if FilePath ends with a path separator, which indicates that\n            // it is intended to represent a directory. Returns false otherwise.\n            // This does NOT check that a directory (or file) actually exists.\n            bool IsDirectory() const;\n\n            // Returns true if pathname describes a root directory. (Windows has one\n            // root directory per disk drive.)\n            bool IsRootDirectory() const;\n\n            // Returns true if pathname describes an absolute path.\n            bool IsAbsolutePath() const;\n\n        private:\n            // Replaces multiple consecutive separators with a single separator.\n            // For example, \"bar///foo\" becomes \"bar/foo\". Does not eliminate other\n            // redundancies that might be in a pathname involving \".\" or \"..\".\n            //\n            // A pathname with multiple consecutive separators may occur either through\n            // user error or as a result of some scripts or APIs that generate a pathname\n            // with a trailing separator. On other platforms the same API or script\n            // may NOT generate a pathname with a trailing \"/\". Then elsewhere that\n            // pathname may have another \"/\" and pathname components added to it,\n            // without checking for the separator already being there.\n            // The script language and operating system may allow paths like \"foo//bar\"\n            // but some of the functions in FilePath will not handle that correctly. In\n            // particular, RemoveTrailingPathSeparator() only removes one separator, and\n            // it is called in CreateDirectoriesRecursively() assuming that it will change\n            // a pathname from directory syntax (trailing separator) to filename syntax.\n            //\n            // On Windows this method also replaces the alternate path separator '/' with\n            // the primary path separator '\\\\', so that for example \"bar\\\\/\\\\foo\" becomes\n            // \"bar\\\\foo\".\n\n            void Normalize();\n\n            // Returns a pointer to the last occurence of a valid path separator in\n            // the FilePath. On Windows, for example, both '/' and '\\' are valid path\n            // separators. Returns NULL if no path separator was found.\n            const char *FindLastPathSeparator() const;\n\n            std::string pathname_;\n        };  // class FilePath\n\n    }  // namespace internal\n}  // namespace testing\n\n#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_\n// This file was GENERATED by command:\n//     pump.py gtest-type-util.h.pump\n// DO NOT EDIT BY HAND!!!\n\n// Copyright 2008 Google Inc.\n// All Rights Reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Author: wan@google.com (Zhanyong Wan)\n\n// Type utilities needed for implementing typed and type-parameterized\n// tests.  This file is generated by a SCRIPT.  DO NOT EDIT BY HAND!\n//\n// Currently we support at most 50 types in a list, and at most 50\n// type-parameterized tests in one type-parameterized test case.\n// Please contact googletestframework@googlegroups.com if you need\n// more.\n\n#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_\n#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_\n\n\n// #ifdef __GNUC__ is too general here.  It is possible to use gcc without using\n// libstdc++ (which is where cxxabi.h comes from).\n# if GTEST_HAS_CXXABI_H_\n#  include <cxxabi.h>\n# elif defined(__HP_aCC)\n#  include <acxx_demangle.h>\n# endif  // GTEST_HASH_CXXABI_H_\n\nnamespace testing\n{\n    namespace internal\n    {\n\n// GetTypeName<T>() returns a human-readable name of type T.\n// NB: This function is also used in Google Mock, so don't move it inside of\n// the typed-test-only section below.\n        template <typename T>\n        std::string GetTypeName()\n        {\n# if GTEST_HAS_RTTI\n\n            const char *const name = typeid(T).name();\n#  if GTEST_HAS_CXXABI_H_ || defined(__HP_aCC)\n            int status = 0;\n            // gcc's implementation of typeid(T).name() mangles the type name,\n            // so we have to demangle it.\n#   if GTEST_HAS_CXXABI_H_\n            using abi::__cxa_demangle;\n#   endif  // GTEST_HAS_CXXABI_H_\n            char *const readable_name = __cxa_demangle(name, 0, 0, &status);\n            const std::string name_str(status == 0 ? readable_name : name);\n            free(readable_name);\n            return name_str;\n#  else\n            return name;\n#  endif  // GTEST_HAS_CXXABI_H_ || __HP_aCC\n\n# else\n\n            return \"<type>\";\n\n# endif  // GTEST_HAS_RTTI\n        }\n\n#if GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P\n\n// AssertyTypeEq<T1, T2>::type is defined iff T1 and T2 are the same\n// type.  This can be used as a compile-time assertion to ensure that\n// two types are equal.\n\n        template <typename T1, typename T2>\n        struct AssertTypeEq;\n\n        template <typename T>\n        struct AssertTypeEq<T, T> {\n            typedef bool type;\n        };\n\n// A unique type used as the default value for the arguments of class\n// template Types.  This allows us to simulate variadic templates\n// (e.g. Types<int>, Type<int, double>, and etc), which C++ doesn't\n// support directly.\n        struct None {};\n\n// The following family of struct and struct templates are used to\n// represent type lists.  In particular, TypesN<T1, T2, ..., TN>\n// represents a type list with N types (T1, T2, ..., and TN) in it.\n// Except for Types0, every struct in the family has two member types:\n// Head for the first type in the list, and Tail for the rest of the\n// list.\n\n// The empty type list.\n        struct Types0 {};\n\n// Type lists of length 1, 2, 3, and so on.\n\n        template <typename T1>\n        struct Types1 {\n            typedef T1 Head;\n            typedef Types0 Tail;\n        };\n        template <typename T1, typename T2>\n        struct Types2 {\n            typedef T1 Head;\n            typedef Types1<T2> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3>\n        struct Types3 {\n            typedef T1 Head;\n            typedef Types2<T2, T3> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4>\n        struct Types4 {\n            typedef T1 Head;\n            typedef Types3<T2, T3, T4> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5>\n        struct Types5 {\n            typedef T1 Head;\n            typedef Types4<T2, T3, T4, T5> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6>\n        struct Types6 {\n            typedef T1 Head;\n            typedef Types5<T2, T3, T4, T5, T6> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7>\n        struct Types7 {\n            typedef T1 Head;\n            typedef Types6<T2, T3, T4, T5, T6, T7> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8>\n        struct Types8 {\n            typedef T1 Head;\n            typedef Types7<T2, T3, T4, T5, T6, T7, T8> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9>\n        struct Types9 {\n            typedef T1 Head;\n            typedef Types8<T2, T3, T4, T5, T6, T7, T8, T9> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10>\n        struct Types10 {\n            typedef T1 Head;\n            typedef Types9<T2, T3, T4, T5, T6, T7, T8, T9, T10> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11>\n        struct Types11 {\n            typedef T1 Head;\n            typedef Types10<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12>\n        struct Types12 {\n            typedef T1 Head;\n            typedef Types11<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13>\n        struct Types13 {\n            typedef T1 Head;\n            typedef Types12<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14>\n        struct Types14 {\n            typedef T1 Head;\n            typedef Types13<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15>\n        struct Types15 {\n            typedef T1 Head;\n            typedef Types14<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16>\n        struct Types16 {\n            typedef T1 Head;\n            typedef Types15<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17>\n        struct Types17 {\n            typedef T1 Head;\n            typedef Types16<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18>\n        struct Types18 {\n            typedef T1 Head;\n            typedef Types17<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19>\n        struct Types19 {\n            typedef T1 Head;\n            typedef Types18<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20>\n        struct Types20 {\n            typedef T1 Head;\n            typedef Types19<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21>\n        struct Types21 {\n            typedef T1 Head;\n            typedef Types20<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22>\n        struct Types22 {\n            typedef T1 Head;\n            typedef Types21<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23>\n        struct Types23 {\n            typedef T1 Head;\n            typedef Types22<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24>\n        struct Types24 {\n            typedef T1 Head;\n            typedef Types23<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25>\n        struct Types25 {\n            typedef T1 Head;\n            typedef Types24<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24, T25> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26>\n        struct Types26 {\n            typedef T1 Head;\n            typedef Types25<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27>\n        struct Types27 {\n            typedef T1 Head;\n            typedef Types26<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28>\n        struct Types28 {\n            typedef T1 Head;\n            typedef Types27<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29>\n        struct Types29 {\n            typedef T1 Head;\n            typedef Types28<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n                    T29> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30>\n        struct Types30 {\n            typedef T1 Head;\n            typedef Types29<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                    T30> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31>\n        struct Types31 {\n            typedef T1 Head;\n            typedef Types30<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                    T30, T31> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32>\n        struct Types32 {\n            typedef T1 Head;\n            typedef Types31<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                    T30, T31, T32> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33>\n        struct Types33 {\n            typedef T1 Head;\n            typedef Types32<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                    T30, T31, T32, T33> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34>\n        struct Types34 {\n            typedef T1 Head;\n            typedef Types33<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                    T30, T31, T32, T33, T34> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35>\n        struct Types35 {\n            typedef T1 Head;\n            typedef Types34<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                    T30, T31, T32, T33, T34, T35> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36>\n        struct Types36 {\n            typedef T1 Head;\n            typedef Types35<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                    T30, T31, T32, T33, T34, T35, T36> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37>\n        struct Types37 {\n            typedef T1 Head;\n            typedef Types36<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                    T30, T31, T32, T33, T34, T35, T36, T37> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38>\n        struct Types38 {\n            typedef T1 Head;\n            typedef Types37<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                    T30, T31, T32, T33, T34, T35, T36, T37, T38> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38, typename T39>\n        struct Types39 {\n            typedef T1 Head;\n            typedef Types38<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                    T30, T31, T32, T33, T34, T35, T36, T37, T38, T39> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38, typename T39, typename T40>\n        struct Types40 {\n            typedef T1 Head;\n            typedef Types39<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                    T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38, typename T39, typename T40,\n                  typename T41>\n        struct Types41 {\n            typedef T1 Head;\n            typedef Types40<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                    T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38, typename T39, typename T40,\n                  typename T41, typename T42>\n        struct Types42 {\n            typedef T1 Head;\n            typedef Types41<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                    T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38, typename T39, typename T40,\n                  typename T41, typename T42, typename T43>\n        struct Types43 {\n            typedef T1 Head;\n            typedef Types42<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                    T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42,\n                    T43> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38, typename T39, typename T40,\n                  typename T41, typename T42, typename T43, typename T44>\n        struct Types44 {\n            typedef T1 Head;\n            typedef Types43<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                    T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43,\n                    T44> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38, typename T39, typename T40,\n                  typename T41, typename T42, typename T43, typename T44, typename T45>\n        struct Types45 {\n            typedef T1 Head;\n            typedef Types44<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                    T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43,\n                    T44, T45> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38, typename T39, typename T40,\n                  typename T41, typename T42, typename T43, typename T44, typename T45,\n                  typename T46>\n        struct Types46 {\n            typedef T1 Head;\n            typedef Types45<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                    T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43,\n                    T44, T45, T46> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38, typename T39, typename T40,\n                  typename T41, typename T42, typename T43, typename T44, typename T45,\n                  typename T46, typename T47>\n        struct Types47 {\n            typedef T1 Head;\n            typedef Types46<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                    T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43,\n                    T44, T45, T46, T47> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38, typename T39, typename T40,\n                  typename T41, typename T42, typename T43, typename T44, typename T45,\n                  typename T46, typename T47, typename T48>\n        struct Types48 {\n            typedef T1 Head;\n            typedef Types47<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                    T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43,\n                    T44, T45, T46, T47, T48> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38, typename T39, typename T40,\n                  typename T41, typename T42, typename T43, typename T44, typename T45,\n                  typename T46, typename T47, typename T48, typename T49>\n        struct Types49 {\n            typedef T1 Head;\n            typedef Types48<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                    T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43,\n                    T44, T45, T46, T47, T48, T49> Tail;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38, typename T39, typename T40,\n                  typename T41, typename T42, typename T43, typename T44, typename T45,\n                  typename T46, typename T47, typename T48, typename T49, typename T50>\n        struct Types50 {\n            typedef T1 Head;\n            typedef Types49<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n                    T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                    T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43,\n                    T44, T45, T46, T47, T48, T49, T50> Tail;\n        };\n\n\n    }  // namespace internal\n\n// We don't want to require the users to write TypesN<...> directly,\n// as that would require them to count the length.  Types<...> is much\n// easier to write, but generates horrible messages when there is a\n// compiler error, as gcc insists on printing out each template\n// argument, even if it has the default value (this means Types<int>\n// will appear as Types<int, None, None, ..., None> in the compiler\n// errors).\n//\n// Our solution is to combine the best part of the two approaches: a\n// user would write Types<T1, ..., TN>, and Google Test will translate\n// that to TypesN<T1, ..., TN> internally to make error messages\n// readable.  The translation is done by the 'type' member of the\n// Types template.\n    template <typename T1 = internal::None, typename T2 = internal::None,\n              typename T3 = internal::None, typename T4 = internal::None,\n              typename T5 = internal::None, typename T6 = internal::None,\n              typename T7 = internal::None, typename T8 = internal::None,\n              typename T9 = internal::None, typename T10 = internal::None,\n              typename T11 = internal::None, typename T12 = internal::None,\n              typename T13 = internal::None, typename T14 = internal::None,\n              typename T15 = internal::None, typename T16 = internal::None,\n              typename T17 = internal::None, typename T18 = internal::None,\n              typename T19 = internal::None, typename T20 = internal::None,\n              typename T21 = internal::None, typename T22 = internal::None,\n              typename T23 = internal::None, typename T24 = internal::None,\n              typename T25 = internal::None, typename T26 = internal::None,\n              typename T27 = internal::None, typename T28 = internal::None,\n              typename T29 = internal::None, typename T30 = internal::None,\n              typename T31 = internal::None, typename T32 = internal::None,\n              typename T33 = internal::None, typename T34 = internal::None,\n              typename T35 = internal::None, typename T36 = internal::None,\n              typename T37 = internal::None, typename T38 = internal::None,\n              typename T39 = internal::None, typename T40 = internal::None,\n              typename T41 = internal::None, typename T42 = internal::None,\n              typename T43 = internal::None, typename T44 = internal::None,\n              typename T45 = internal::None, typename T46 = internal::None,\n              typename T47 = internal::None, typename T48 = internal::None,\n              typename T49 = internal::None, typename T50 = internal::None>\n    struct Types {\n        typedef internal::Types50<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n                T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40,\n                T41, T42, T43, T44, T45, T46, T47, T48, T49, T50> type;\n    };\n\n    template <>\n    struct Types<internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None> {\n        typedef internal::Types0 type;\n    };\n    template <typename T1>\n    struct Types<T1, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None> {\n        typedef internal::Types1<T1> type;\n    };\n    template <typename T1, typename T2>\n    struct Types<T1, T2, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None> {\n        typedef internal::Types2<T1, T2> type;\n    };\n    template <typename T1, typename T2, typename T3>\n    struct Types<T1, T2, T3, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None> {\n        typedef internal::Types3<T1, T2, T3> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4>\n    struct Types<T1, T2, T3, T4, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None> {\n        typedef internal::Types4<T1, T2, T3, T4> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5>\n    struct Types<T1, T2, T3, T4, T5, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None> {\n        typedef internal::Types5<T1, T2, T3, T4, T5> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6>\n    struct Types<T1, T2, T3, T4, T5, T6, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None> {\n        typedef internal::Types6<T1, T2, T3, T4, T5, T6> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None> {\n        typedef internal::Types7<T1, T2, T3, T4, T5, T6, T7> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None> {\n        typedef internal::Types8<T1, T2, T3, T4, T5, T6, T7, T8> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None> {\n        typedef internal::Types9<T1, T2, T3, T4, T5, T6, T7, T8, T9> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None> {\n        typedef internal::Types10<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None> {\n        typedef internal::Types11<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None> {\n        typedef internal::Types12<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n                T12> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None> {\n        typedef internal::Types13<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None> {\n        typedef internal::Types14<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None> {\n        typedef internal::Types15<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None> {\n        typedef internal::Types16<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None> {\n        typedef internal::Types17<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None> {\n        typedef internal::Types18<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None> {\n        typedef internal::Types19<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None> {\n        typedef internal::Types20<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None> {\n        typedef internal::Types21<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None> {\n        typedef internal::Types22<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None> {\n        typedef internal::Types23<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, T24, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None> {\n        typedef internal::Types24<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None> {\n        typedef internal::Types25<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None> {\n        typedef internal::Types26<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n                T26> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None> {\n        typedef internal::Types27<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n                T27> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None> {\n        typedef internal::Types28<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n                T27, T28> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None> {\n        typedef internal::Types29<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n                T27, T28, T29> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None> {\n        typedef internal::Types30<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n                T27, T28, T29, T30> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30,\n               T31, internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None> {\n        typedef internal::Types31<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n                T27, T28, T29, T30, T31> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30,\n               T31, T32, internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None> {\n        typedef internal::Types32<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n                T27, T28, T29, T30, T31, T32> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30,\n               T31, T32, T33, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None> {\n        typedef internal::Types33<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n                T27, T28, T29, T30, T31, T32, T33> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30,\n               T31, T32, T33, T34, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None> {\n        typedef internal::Types34<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n                T27, T28, T29, T30, T31, T32, T33, T34> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30,\n               T31, T32, T33, T34, T35, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None> {\n        typedef internal::Types35<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n                T27, T28, T29, T30, T31, T32, T33, T34, T35> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30,\n               T31, T32, T33, T34, T35, T36, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None> {\n        typedef internal::Types36<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n                T27, T28, T29, T30, T31, T32, T33, T34, T35, T36> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30,\n               T31, T32, T33, T34, T35, T36, T37, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None> {\n        typedef internal::Types37<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n                T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37, typename T38>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30,\n               T31, T32, T33, T34, T35, T36, T37, T38, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None> {\n        typedef internal::Types38<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n                T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37, typename T38, typename T39>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30,\n               T31, T32, T33, T34, T35, T36, T37, T38, T39, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None> {\n        typedef internal::Types39<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n                T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37, typename T38, typename T39, typename T40>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30,\n               T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None> {\n        typedef internal::Types40<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n                T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39,\n                T40> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37, typename T38, typename T39, typename T40,\n              typename T41>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30,\n               T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None, internal::None> {\n        typedef internal::Types41<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n                T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40,\n                T41> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37, typename T38, typename T39, typename T40,\n              typename T41, typename T42>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30,\n               T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, internal::None,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None> {\n        typedef internal::Types42<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n                T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40,\n                T41, T42> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37, typename T38, typename T39, typename T40,\n              typename T41, typename T42, typename T43>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30,\n               T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None, internal::None> {\n        typedef internal::Types43<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n                T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40,\n                T41, T42, T43> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37, typename T38, typename T39, typename T40,\n              typename T41, typename T42, typename T43, typename T44>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30,\n               T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43, T44,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None, internal::None> {\n        typedef internal::Types44<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n                T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40,\n                T41, T42, T43, T44> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37, typename T38, typename T39, typename T40,\n              typename T41, typename T42, typename T43, typename T44, typename T45>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30,\n               T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43, T44, T45,\n               internal::None, internal::None, internal::None, internal::None,\n               internal::None> {\n        typedef internal::Types45<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n                T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40,\n                T41, T42, T43, T44, T45> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37, typename T38, typename T39, typename T40,\n              typename T41, typename T42, typename T43, typename T44, typename T45,\n              typename T46>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30,\n               T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43, T44, T45,\n               T46, internal::None, internal::None, internal::None, internal::None> {\n        typedef internal::Types46<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n                T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40,\n                T41, T42, T43, T44, T45, T46> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37, typename T38, typename T39, typename T40,\n              typename T41, typename T42, typename T43, typename T44, typename T45,\n              typename T46, typename T47>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30,\n               T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43, T44, T45,\n               T46, T47, internal::None, internal::None, internal::None> {\n        typedef internal::Types47<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n                T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40,\n                T41, T42, T43, T44, T45, T46, T47> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37, typename T38, typename T39, typename T40,\n              typename T41, typename T42, typename T43, typename T44, typename T45,\n              typename T46, typename T47, typename T48>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30,\n               T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43, T44, T45,\n               T46, T47, T48, internal::None, internal::None> {\n        typedef internal::Types48<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n                T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40,\n                T41, T42, T43, T44, T45, T46, T47, T48> type;\n    };\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37, typename T38, typename T39, typename T40,\n              typename T41, typename T42, typename T43, typename T44, typename T45,\n              typename T46, typename T47, typename T48, typename T49>\n    struct Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15,\n               T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29, T30,\n               T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43, T44, T45,\n               T46, T47, T48, T49, internal::None> {\n        typedef internal::Types49<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n                T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40,\n                T41, T42, T43, T44, T45, T46, T47, T48, T49> type;\n    };\n\n    namespace internal\n    {\n\n# define GTEST_TEMPLATE_ template <typename T> class\n\n// The template \"selector\" struct TemplateSel<Tmpl> is used to\n// represent Tmpl, which must be a class template with one type\n// parameter, as a type.  TemplateSel<Tmpl>::Bind<T>::type is defined\n// as the type Tmpl<T>.  This allows us to actually instantiate the\n// template \"selected\" by TemplateSel<Tmpl>.\n//\n// This trick is necessary for simulating typedef for class templates,\n// which C++ doesn't support directly.\n        template <GTEST_TEMPLATE_ Tmpl>\n        struct TemplateSel {\n            template <typename T>\n            struct Bind {\n                typedef Tmpl<T> type;\n            };\n        };\n\n# define GTEST_BIND_(TmplSel, T) \\\n  TmplSel::template Bind<T>::type\n\n// A unique struct template used as the default value for the\n// arguments of class template Templates.  This allows us to simulate\n// variadic templates (e.g. Templates<int>, Templates<int, double>,\n// and etc), which C++ doesn't support directly.\n        template <typename T>\n        struct NoneT {};\n\n// The following family of struct and struct templates are used to\n// represent template lists.  In particular, TemplatesN<T1, T2, ...,\n// TN> represents a list of N templates (T1, T2, ..., and TN).  Except\n// for Templates0, every struct in the family has two member types:\n// Head for the selector of the first template in the list, and Tail\n// for the rest of the list.\n\n// The empty template list.\n        struct Templates0 {};\n\n// Template lists of length 1, 2, 3, and so on.\n\n        template <GTEST_TEMPLATE_ T1>\n        struct Templates1 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates0 Tail;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2>\n        struct Templates2 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates1<T2> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3>\n        struct Templates3 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates2<T2, T3> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4>\n        struct Templates4 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates3<T2, T3, T4> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5>\n        struct Templates5 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates4<T2, T3, T4, T5> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6>\n        struct Templates6 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates5<T2, T3, T4, T5, T6> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7>\n        struct Templates7 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates6<T2, T3, T4, T5, T6, T7> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8>\n        struct Templates8 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates7<T2, T3, T4, T5, T6, T7, T8> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9>\n        struct Templates9 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates8<T2, T3, T4, T5, T6, T7, T8, T9> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10>\n        struct Templates10 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates9<T2, T3, T4, T5, T6, T7, T8, T9, T10> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11>\n        struct Templates11 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates10<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12>\n        struct Templates12 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates11<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13>\n        struct Templates13 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates12<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14>\n        struct Templates14 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates13<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15>\n        struct Templates15 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates14<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16>\n        struct Templates16 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates15<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17>\n        struct Templates17 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates16<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18>\n        struct Templates18 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates17<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19>\n        struct Templates19 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates18<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20>\n        struct Templates20 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates19<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21>\n        struct Templates21 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates20<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22>\n        struct Templates22 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates21<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23>\n        struct Templates23 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates22<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24>\n        struct Templates24 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates23<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25>\n        struct Templates25 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates24<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26>\n        struct Templates26 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates25<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27>\n        struct Templates27 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates26<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28>\n        struct Templates28 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates27<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,\n                    T28> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29>\n        struct Templates29 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates28<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n                    T29> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30>\n        struct Templates30 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates29<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n                    T29, T30> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31>\n        struct Templates31 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates30<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n                    T29, T30, T31> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32>\n        struct Templates32 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates31<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n                    T29, T30, T31, T32> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33>\n        struct Templates33 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates32<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n                    T29, T30, T31, T32, T33> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34>\n        struct Templates34 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates33<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n                    T29, T30, T31, T32, T33, T34> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35>\n        struct Templates35 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates34<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n                    T29, T30, T31, T32, T33, T34, T35> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36>\n        struct Templates36 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates35<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n                    T29, T30, T31, T32, T33, T34, T35, T36> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37>\n        struct Templates37 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates36<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n                    T29, T30, T31, T32, T33, T34, T35, T36, T37> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37, GTEST_TEMPLATE_ T38>\n        struct Templates38 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates37<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n                    T29, T30, T31, T32, T33, T34, T35, T36, T37, T38> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37, GTEST_TEMPLATE_ T38, GTEST_TEMPLATE_ T39>\n        struct Templates39 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates38<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n                    T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37, GTEST_TEMPLATE_ T38, GTEST_TEMPLATE_ T39,\n                  GTEST_TEMPLATE_ T40>\n        struct Templates40 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates39<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n                    T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37, GTEST_TEMPLATE_ T38, GTEST_TEMPLATE_ T39,\n                  GTEST_TEMPLATE_ T40, GTEST_TEMPLATE_ T41>\n        struct Templates41 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates40<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n                    T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37, GTEST_TEMPLATE_ T38, GTEST_TEMPLATE_ T39,\n                  GTEST_TEMPLATE_ T40, GTEST_TEMPLATE_ T41, GTEST_TEMPLATE_ T42>\n        struct Templates42 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates41<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n                    T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41,\n                    T42> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37, GTEST_TEMPLATE_ T38, GTEST_TEMPLATE_ T39,\n                  GTEST_TEMPLATE_ T40, GTEST_TEMPLATE_ T41, GTEST_TEMPLATE_ T42,\n                  GTEST_TEMPLATE_ T43>\n        struct Templates43 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates42<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n                    T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42,\n                    T43> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37, GTEST_TEMPLATE_ T38, GTEST_TEMPLATE_ T39,\n                  GTEST_TEMPLATE_ T40, GTEST_TEMPLATE_ T41, GTEST_TEMPLATE_ T42,\n                  GTEST_TEMPLATE_ T43, GTEST_TEMPLATE_ T44>\n        struct Templates44 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates43<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n                    T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42,\n                    T43, T44> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37, GTEST_TEMPLATE_ T38, GTEST_TEMPLATE_ T39,\n                  GTEST_TEMPLATE_ T40, GTEST_TEMPLATE_ T41, GTEST_TEMPLATE_ T42,\n                  GTEST_TEMPLATE_ T43, GTEST_TEMPLATE_ T44, GTEST_TEMPLATE_ T45>\n        struct Templates45 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates44<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n                    T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42,\n                    T43, T44, T45> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37, GTEST_TEMPLATE_ T38, GTEST_TEMPLATE_ T39,\n                  GTEST_TEMPLATE_ T40, GTEST_TEMPLATE_ T41, GTEST_TEMPLATE_ T42,\n                  GTEST_TEMPLATE_ T43, GTEST_TEMPLATE_ T44, GTEST_TEMPLATE_ T45,\n                  GTEST_TEMPLATE_ T46>\n        struct Templates46 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates45<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n                    T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42,\n                    T43, T44, T45, T46> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37, GTEST_TEMPLATE_ T38, GTEST_TEMPLATE_ T39,\n                  GTEST_TEMPLATE_ T40, GTEST_TEMPLATE_ T41, GTEST_TEMPLATE_ T42,\n                  GTEST_TEMPLATE_ T43, GTEST_TEMPLATE_ T44, GTEST_TEMPLATE_ T45,\n                  GTEST_TEMPLATE_ T46, GTEST_TEMPLATE_ T47>\n        struct Templates47 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates46<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n                    T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42,\n                    T43, T44, T45, T46, T47> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37, GTEST_TEMPLATE_ T38, GTEST_TEMPLATE_ T39,\n                  GTEST_TEMPLATE_ T40, GTEST_TEMPLATE_ T41, GTEST_TEMPLATE_ T42,\n                  GTEST_TEMPLATE_ T43, GTEST_TEMPLATE_ T44, GTEST_TEMPLATE_ T45,\n                  GTEST_TEMPLATE_ T46, GTEST_TEMPLATE_ T47, GTEST_TEMPLATE_ T48>\n        struct Templates48 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates47<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n                    T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42,\n                    T43, T44, T45, T46, T47, T48> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37, GTEST_TEMPLATE_ T38, GTEST_TEMPLATE_ T39,\n                  GTEST_TEMPLATE_ T40, GTEST_TEMPLATE_ T41, GTEST_TEMPLATE_ T42,\n                  GTEST_TEMPLATE_ T43, GTEST_TEMPLATE_ T44, GTEST_TEMPLATE_ T45,\n                  GTEST_TEMPLATE_ T46, GTEST_TEMPLATE_ T47, GTEST_TEMPLATE_ T48,\n                  GTEST_TEMPLATE_ T49>\n        struct Templates49 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates48<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n                    T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42,\n                    T43, T44, T45, T46, T47, T48, T49> Tail;\n        };\n\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37, GTEST_TEMPLATE_ T38, GTEST_TEMPLATE_ T39,\n                  GTEST_TEMPLATE_ T40, GTEST_TEMPLATE_ T41, GTEST_TEMPLATE_ T42,\n                  GTEST_TEMPLATE_ T43, GTEST_TEMPLATE_ T44, GTEST_TEMPLATE_ T45,\n                  GTEST_TEMPLATE_ T46, GTEST_TEMPLATE_ T47, GTEST_TEMPLATE_ T48,\n                  GTEST_TEMPLATE_ T49, GTEST_TEMPLATE_ T50>\n        struct Templates50 {\n            typedef TemplateSel<T1> Head;\n            typedef Templates49<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                    T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n                    T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42,\n                    T43, T44, T45, T46, T47, T48, T49, T50> Tail;\n        };\n\n\n// We don't want to require the users to write TemplatesN<...> directly,\n// as that would require them to count the length.  Templates<...> is much\n// easier to write, but generates horrible messages when there is a\n// compiler error, as gcc insists on printing out each template\n// argument, even if it has the default value (this means Templates<list>\n// will appear as Templates<list, NoneT, NoneT, ..., NoneT> in the compiler\n// errors).\n//\n// Our solution is to combine the best part of the two approaches: a\n// user would write Templates<T1, ..., TN>, and Google Test will translate\n// that to TemplatesN<T1, ..., TN> internally to make error messages\n// readable.  The translation is done by the 'type' member of the\n// Templates template.\n        template <GTEST_TEMPLATE_ T1 = NoneT, GTEST_TEMPLATE_ T2 = NoneT,\n                  GTEST_TEMPLATE_ T3 = NoneT, GTEST_TEMPLATE_ T4 = NoneT,\n                  GTEST_TEMPLATE_ T5 = NoneT, GTEST_TEMPLATE_ T6 = NoneT,\n                  GTEST_TEMPLATE_ T7 = NoneT, GTEST_TEMPLATE_ T8 = NoneT,\n                  GTEST_TEMPLATE_ T9 = NoneT, GTEST_TEMPLATE_ T10 = NoneT,\n                  GTEST_TEMPLATE_ T11 = NoneT, GTEST_TEMPLATE_ T12 = NoneT,\n                  GTEST_TEMPLATE_ T13 = NoneT, GTEST_TEMPLATE_ T14 = NoneT,\n                  GTEST_TEMPLATE_ T15 = NoneT, GTEST_TEMPLATE_ T16 = NoneT,\n                  GTEST_TEMPLATE_ T17 = NoneT, GTEST_TEMPLATE_ T18 = NoneT,\n                  GTEST_TEMPLATE_ T19 = NoneT, GTEST_TEMPLATE_ T20 = NoneT,\n                  GTEST_TEMPLATE_ T21 = NoneT, GTEST_TEMPLATE_ T22 = NoneT,\n                  GTEST_TEMPLATE_ T23 = NoneT, GTEST_TEMPLATE_ T24 = NoneT,\n                  GTEST_TEMPLATE_ T25 = NoneT, GTEST_TEMPLATE_ T26 = NoneT,\n                  GTEST_TEMPLATE_ T27 = NoneT, GTEST_TEMPLATE_ T28 = NoneT,\n                  GTEST_TEMPLATE_ T29 = NoneT, GTEST_TEMPLATE_ T30 = NoneT,\n                  GTEST_TEMPLATE_ T31 = NoneT, GTEST_TEMPLATE_ T32 = NoneT,\n                  GTEST_TEMPLATE_ T33 = NoneT, GTEST_TEMPLATE_ T34 = NoneT,\n                  GTEST_TEMPLATE_ T35 = NoneT, GTEST_TEMPLATE_ T36 = NoneT,\n                  GTEST_TEMPLATE_ T37 = NoneT, GTEST_TEMPLATE_ T38 = NoneT,\n                  GTEST_TEMPLATE_ T39 = NoneT, GTEST_TEMPLATE_ T40 = NoneT,\n                  GTEST_TEMPLATE_ T41 = NoneT, GTEST_TEMPLATE_ T42 = NoneT,\n                  GTEST_TEMPLATE_ T43 = NoneT, GTEST_TEMPLATE_ T44 = NoneT,\n                  GTEST_TEMPLATE_ T45 = NoneT, GTEST_TEMPLATE_ T46 = NoneT,\n                  GTEST_TEMPLATE_ T47 = NoneT, GTEST_TEMPLATE_ T48 = NoneT,\n                  GTEST_TEMPLATE_ T49 = NoneT, GTEST_TEMPLATE_ T50 = NoneT>\n        struct Templates {\n            typedef Templates50<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,\n                    T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41,\n                    T42, T43, T44, T45, T46, T47, T48, T49, T50> type;\n        };\n\n        template <>\n        struct Templates<NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT> {\n            typedef Templates0 type;\n        };\n        template <GTEST_TEMPLATE_ T1>\n        struct Templates<T1, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT> {\n            typedef Templates1<T1> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2>\n        struct Templates<T1, T2, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT> {\n            typedef Templates2<T1, T2> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3>\n        struct Templates<T1, T2, T3, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates3<T1, T2, T3> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4>\n        struct Templates<T1, T2, T3, T4, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates4<T1, T2, T3, T4> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5>\n        struct Templates<T1, T2, T3, T4, T5, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates5<T1, T2, T3, T4, T5> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6>\n        struct Templates<T1, T2, T3, T4, T5, T6, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates6<T1, T2, T3, T4, T5, T6> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates7<T1, T2, T3, T4, T5, T6, T7> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates8<T1, T2, T3, T4, T5, T6, T7, T8> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates9<T1, T2, T3, T4, T5, T6, T7, T8, T9> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates10<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates11<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates12<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates13<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                    T13> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates14<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates15<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates16<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates17<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates18<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates19<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates20<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates21<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT> {\n            typedef Templates22<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT> {\n            typedef Templates23<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT> {\n            typedef Templates24<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT> {\n            typedef Templates25<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT> {\n            typedef Templates26<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT> {\n            typedef Templates27<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n                    T27> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT> {\n            typedef Templates28<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,\n                    T28> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT> {\n            typedef Templates29<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,\n                    T28, T29> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                   T30, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates30<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,\n                    T28, T29, T30> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                   T30, T31, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates31<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,\n                    T28, T29, T30, T31> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                   T30, T31, T32, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates32<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,\n                    T28, T29, T30, T31, T32> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                   T30, T31, T32, T33, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates33<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,\n                    T28, T29, T30, T31, T32, T33> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                   T30, T31, T32, T33, T34, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates34<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,\n                    T28, T29, T30, T31, T32, T33, T34> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                   T30, T31, T32, T33, T34, T35, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates35<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,\n                    T28, T29, T30, T31, T32, T33, T34, T35> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                   T30, T31, T32, T33, T34, T35, T36, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates36<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,\n                    T28, T29, T30, T31, T32, T33, T34, T35, T36> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                   T30, T31, T32, T33, T34, T35, T36, T37, NoneT, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates37<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,\n                    T28, T29, T30, T31, T32, T33, T34, T35, T36, T37> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37, GTEST_TEMPLATE_ T38>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                   T30, T31, T32, T33, T34, T35, T36, T37, T38, NoneT, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates38<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,\n                    T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37, GTEST_TEMPLATE_ T38, GTEST_TEMPLATE_ T39>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                   T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates39<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,\n                    T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37, GTEST_TEMPLATE_ T38, GTEST_TEMPLATE_ T39,\n                  GTEST_TEMPLATE_ T40>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                   T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, NoneT, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates40<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,\n                    T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37, GTEST_TEMPLATE_ T38, GTEST_TEMPLATE_ T39,\n                  GTEST_TEMPLATE_ T40, GTEST_TEMPLATE_ T41>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                   T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, NoneT, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates41<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,\n                    T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40,\n                    T41> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37, GTEST_TEMPLATE_ T38, GTEST_TEMPLATE_ T39,\n                  GTEST_TEMPLATE_ T40, GTEST_TEMPLATE_ T41, GTEST_TEMPLATE_ T42>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                   T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, NoneT,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates42<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,\n                    T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41,\n                    T42> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37, GTEST_TEMPLATE_ T38, GTEST_TEMPLATE_ T39,\n                  GTEST_TEMPLATE_ T40, GTEST_TEMPLATE_ T41, GTEST_TEMPLATE_ T42,\n                  GTEST_TEMPLATE_ T43>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                   T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates43<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,\n                    T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41,\n                    T42, T43> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37, GTEST_TEMPLATE_ T38, GTEST_TEMPLATE_ T39,\n                  GTEST_TEMPLATE_ T40, GTEST_TEMPLATE_ T41, GTEST_TEMPLATE_ T42,\n                  GTEST_TEMPLATE_ T43, GTEST_TEMPLATE_ T44>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                   T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43, T44,\n                   NoneT, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates44<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,\n                    T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41,\n                    T42, T43, T44> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37, GTEST_TEMPLATE_ T38, GTEST_TEMPLATE_ T39,\n                  GTEST_TEMPLATE_ T40, GTEST_TEMPLATE_ T41, GTEST_TEMPLATE_ T42,\n                  GTEST_TEMPLATE_ T43, GTEST_TEMPLATE_ T44, GTEST_TEMPLATE_ T45>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                   T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43, T44,\n                   T45, NoneT, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates45<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,\n                    T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41,\n                    T42, T43, T44, T45> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37, GTEST_TEMPLATE_ T38, GTEST_TEMPLATE_ T39,\n                  GTEST_TEMPLATE_ T40, GTEST_TEMPLATE_ T41, GTEST_TEMPLATE_ T42,\n                  GTEST_TEMPLATE_ T43, GTEST_TEMPLATE_ T44, GTEST_TEMPLATE_ T45,\n                  GTEST_TEMPLATE_ T46>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                   T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43, T44,\n                   T45, T46, NoneT, NoneT, NoneT, NoneT> {\n            typedef Templates46<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,\n                    T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41,\n                    T42, T43, T44, T45, T46> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37, GTEST_TEMPLATE_ T38, GTEST_TEMPLATE_ T39,\n                  GTEST_TEMPLATE_ T40, GTEST_TEMPLATE_ T41, GTEST_TEMPLATE_ T42,\n                  GTEST_TEMPLATE_ T43, GTEST_TEMPLATE_ T44, GTEST_TEMPLATE_ T45,\n                  GTEST_TEMPLATE_ T46, GTEST_TEMPLATE_ T47>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                   T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43, T44,\n                   T45, T46, T47, NoneT, NoneT, NoneT> {\n            typedef Templates47<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,\n                    T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41,\n                    T42, T43, T44, T45, T46, T47> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37, GTEST_TEMPLATE_ T38, GTEST_TEMPLATE_ T39,\n                  GTEST_TEMPLATE_ T40, GTEST_TEMPLATE_ T41, GTEST_TEMPLATE_ T42,\n                  GTEST_TEMPLATE_ T43, GTEST_TEMPLATE_ T44, GTEST_TEMPLATE_ T45,\n                  GTEST_TEMPLATE_ T46, GTEST_TEMPLATE_ T47, GTEST_TEMPLATE_ T48>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                   T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43, T44,\n                   T45, T46, T47, T48, NoneT, NoneT> {\n            typedef Templates48<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,\n                    T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41,\n                    T42, T43, T44, T45, T46, T47, T48> type;\n        };\n        template <GTEST_TEMPLATE_ T1, GTEST_TEMPLATE_ T2, GTEST_TEMPLATE_ T3,\n                  GTEST_TEMPLATE_ T4, GTEST_TEMPLATE_ T5, GTEST_TEMPLATE_ T6,\n                  GTEST_TEMPLATE_ T7, GTEST_TEMPLATE_ T8, GTEST_TEMPLATE_ T9,\n                  GTEST_TEMPLATE_ T10, GTEST_TEMPLATE_ T11, GTEST_TEMPLATE_ T12,\n                  GTEST_TEMPLATE_ T13, GTEST_TEMPLATE_ T14, GTEST_TEMPLATE_ T15,\n                  GTEST_TEMPLATE_ T16, GTEST_TEMPLATE_ T17, GTEST_TEMPLATE_ T18,\n                  GTEST_TEMPLATE_ T19, GTEST_TEMPLATE_ T20, GTEST_TEMPLATE_ T21,\n                  GTEST_TEMPLATE_ T22, GTEST_TEMPLATE_ T23, GTEST_TEMPLATE_ T24,\n                  GTEST_TEMPLATE_ T25, GTEST_TEMPLATE_ T26, GTEST_TEMPLATE_ T27,\n                  GTEST_TEMPLATE_ T28, GTEST_TEMPLATE_ T29, GTEST_TEMPLATE_ T30,\n                  GTEST_TEMPLATE_ T31, GTEST_TEMPLATE_ T32, GTEST_TEMPLATE_ T33,\n                  GTEST_TEMPLATE_ T34, GTEST_TEMPLATE_ T35, GTEST_TEMPLATE_ T36,\n                  GTEST_TEMPLATE_ T37, GTEST_TEMPLATE_ T38, GTEST_TEMPLATE_ T39,\n                  GTEST_TEMPLATE_ T40, GTEST_TEMPLATE_ T41, GTEST_TEMPLATE_ T42,\n                  GTEST_TEMPLATE_ T43, GTEST_TEMPLATE_ T44, GTEST_TEMPLATE_ T45,\n                  GTEST_TEMPLATE_ T46, GTEST_TEMPLATE_ T47, GTEST_TEMPLATE_ T48,\n                  GTEST_TEMPLATE_ T49>\n        struct Templates<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14,\n                   T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28, T29,\n                   T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43, T44,\n                   T45, T46, T47, T48, T49, NoneT> {\n            typedef Templates49<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                    T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,\n                    T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41,\n                    T42, T43, T44, T45, T46, T47, T48, T49> type;\n        };\n\n// The TypeList template makes it possible to use either a single type\n// or a Types<...> list in TYPED_TEST_CASE() and\n// INSTANTIATE_TYPED_TEST_CASE_P().\n\n        template <typename T>\n        struct TypeList {\n            typedef Types1<T> type;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38, typename T39, typename T40,\n                  typename T41, typename T42, typename T43, typename T44, typename T45,\n                  typename T46, typename T47, typename T48, typename T49, typename T50>\n        struct TypeList<Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n                   T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n                   T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43,\n                   T44, T45, T46, T47, T48, T49, T50> > {\n            typedef typename Types<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n                    T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n                    T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40,\n                    T41, T42, T43, T44, T45, T46, T47, T48, T49, T50>::type type;\n        };\n\n#endif  // GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P\n\n    }  // namespace internal\n}  // namespace testing\n\n#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_\n\n// Due to C++ preprocessor weirdness, we need double indirection to\n// concatenate two tokens when one of them is __LINE__.  Writing\n//\n//   foo ## __LINE__\n//\n// will result in the token foo__LINE__, instead of foo followed by\n// the current line number.  For more details, see\n// http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.6\n#define GTEST_CONCAT_TOKEN_(foo, bar) GTEST_CONCAT_TOKEN_IMPL_(foo, bar)\n#define GTEST_CONCAT_TOKEN_IMPL_(foo, bar) foo ## bar\n\nclass ProtocolMessage;\nnamespace proto2\n{\n    class Message;\n}\n\nnamespace testing\n{\n\n// Forward declarations.\n\n    class AssertionResult;                 // Result of an assertion.\n    class Message;                         // Represents a failure message.\n    class Test;                            // Represents a test.\n    class TestInfo;                        // Information about a test.\n    class TestPartResult;                  // Result of a test part.\n    class UnitTest;                        // A collection of test cases.\n\n    template <typename T>\n    ::std::string PrintToString(const T &value);\n\n    namespace internal\n    {\n\n        struct TraceInfo;                      // Information about a trace point.\n        class ScopedTrace;                     // Implements scoped trace.\n        class TestInfoImpl;                    // Opaque implementation of TestInfo\n        class UnitTestImpl;                    // Opaque implementation of UnitTest\n\n// The text used in failure messages to indicate the start of the\n// stack trace.\n        GTEST_API_ extern const char kStackTraceMarker[];\n\n// Two overloaded helpers for checking at compile time whether an\n// expression is a null pointer literal (i.e. NULL or any 0-valued\n// compile-time integral constant).  Their return values have\n// different sizes, so we can use sizeof() to test which version is\n// picked by the compiler.  These helpers have no implementations, as\n// we only need their signatures.\n//\n// Given IsNullLiteralHelper(x), the compiler will pick the first\n// version if x can be implicitly converted to Secret*, and pick the\n// second version otherwise.  Since Secret is a secret and incomplete\n// type, the only expression a user can write that has type Secret* is\n// a null pointer literal.  Therefore, we know that x is a null\n// pointer literal if and only if the first version is picked by the\n// compiler.\n        char IsNullLiteralHelper(Secret *p);\n        char (&IsNullLiteralHelper(...))[2];  // NOLINT\n\n// A compile-time bool constant that is true if and only if x is a\n// null pointer literal (i.e. NULL or any 0-valued compile-time\n// integral constant).\n#ifdef GTEST_ELLIPSIS_NEEDS_POD_\n// We lose support for NULL detection where the compiler doesn't like\n// passing non-POD classes through ellipsis (...).\n# define GTEST_IS_NULL_LITERAL_(x) false\n#else\n# define GTEST_IS_NULL_LITERAL_(x) \\\n    (sizeof(::testing::internal::IsNullLiteralHelper(x)) == 1)\n#endif  // GTEST_ELLIPSIS_NEEDS_POD_\n\n// Appends the user-supplied message to the Google-Test-generated message.\n        GTEST_API_ std::string AppendUserMessage(\n            const std::string &gtest_msg, const Message &user_msg);\n\n#if GTEST_HAS_EXCEPTIONS\n\n// This exception is thrown by (and only by) a failed Google Test\n// assertion when GTEST_FLAG(throw_on_failure) is true (if exceptions\n// are enabled).  We derive it from std::runtime_error, which is for\n// errors presumably detectable only at run time.  Since\n// std::runtime_error inherits from std::exception, many testing\n// frameworks know how to extract and print the message inside it.\n        class GTEST_API_ GoogleTestFailureException : public ::std::runtime_error\n        {\n        public:\n            explicit GoogleTestFailureException(const TestPartResult &failure);\n        };\n\n#endif  // GTEST_HAS_EXCEPTIONS\n\n// A helper class for creating scoped traces in user programs.\n        class GTEST_API_ ScopedTrace\n        {\n        public:\n            // The c'tor pushes the given source file location and message onto\n            // a trace stack maintained by Google Test.\n            ScopedTrace(const char *file, int line, const Message &message);\n\n            // The d'tor pops the info pushed by the c'tor.\n            //\n            // Note that the d'tor is not virtual in order to be efficient.\n            // Don't inherit from ScopedTrace!\n            ~ScopedTrace();\n\n        private:\n            GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedTrace);\n        } GTEST_ATTRIBUTE_UNUSED_;  // A ScopedTrace object does its job in its\n        // c'tor and d'tor.  Therefore it doesn't\n        // need to be used otherwise.\n\n        namespace edit_distance\n        {\n// Returns the optimal edits to go from 'left' to 'right'.\n// All edits cost the same, with replace having lower priority than\n// add/remove.\n// Simple implementation of the Wagner-Fischer algorithm.\n// See http://en.wikipedia.org/wiki/Wagner-Fischer_algorithm\n            enum EditType { kMatch, kAdd, kRemove, kReplace };\n            GTEST_API_ std::vector<EditType> CalculateOptimalEdits(\n                const std::vector<size_t> &left, const std::vector<size_t> &right);\n\n// Same as above, but the input is represented as strings.\n            GTEST_API_ std::vector<EditType> CalculateOptimalEdits(\n                const std::vector<std::string> &left,\n                const std::vector<std::string> &right);\n\n// Create a diff of the input strings in Unified diff format.\n            GTEST_API_ std::string CreateUnifiedDiff(const std::vector<std::string> &left,\n                                                     const std::vector<std::string> &right,\n                                                     size_t context = 2);\n\n        }  // namespace edit_distance\n\n// Calculate the diff between 'left' and 'right' and return it in unified diff\n// format.\n// If not null, stores in 'total_line_count' the total number of lines found\n// in left + right.\n        GTEST_API_ std::string DiffStrings(const std::string &left,\n                                           const std::string &right,\n                                           size_t *total_line_count);\n\n// Constructs and returns the message for an equality assertion\n// (e.g. ASSERT_EQ, EXPECT_STREQ, etc) failure.\n//\n// The first four parameters are the expressions used in the assertion\n// and their values, as strings.  For example, for ASSERT_EQ(foo, bar)\n// where foo is 5 and bar is 6, we have:\n//\n//   expected_expression: \"foo\"\n//   actual_expression:   \"bar\"\n//   expected_value:      \"5\"\n//   actual_value:        \"6\"\n//\n// The ignoring_case parameter is true iff the assertion is a\n// *_STRCASEEQ*.  When it's true, the string \" (ignoring case)\" will\n// be inserted into the message.\n        GTEST_API_ AssertionResult EqFailure(const char *expected_expression,\n                                             const char *actual_expression,\n                                             const std::string &expected_value,\n                                             const std::string &actual_value,\n                                             bool ignoring_case);\n\n// Constructs a failure message for Boolean assertions such as EXPECT_TRUE.\n        GTEST_API_ std::string GetBoolAssertionFailureMessage(\n            const AssertionResult &assertion_result,\n            const char *expression_text,\n            const char *actual_predicate_value,\n            const char *expected_predicate_value);\n\n// This template class represents an IEEE floating-point number\n// (either single-precision or double-precision, depending on the\n// template parameters).\n//\n// The purpose of this class is to do more sophisticated number\n// comparison.  (Due to round-off error, etc, it's very unlikely that\n// two floating-points will be equal exactly.  Hence a naive\n// comparison by the == operation often doesn't work.)\n//\n// Format of IEEE floating-point:\n//\n//   The most-significant bit being the leftmost, an IEEE\n//   floating-point looks like\n//\n//     sign_bit exponent_bits fraction_bits\n//\n//   Here, sign_bit is a single bit that designates the sign of the\n//   number.\n//\n//   For float, there are 8 exponent bits and 23 fraction bits.\n//\n//   For double, there are 11 exponent bits and 52 fraction bits.\n//\n//   More details can be found at\n//   http://en.wikipedia.org/wiki/IEEE_floating-point_standard.\n//\n// Template parameter:\n//\n//   RawType: the raw floating-point type (either float or double)\n        template <typename RawType>\n        class FloatingPoint\n        {\n        public:\n            // Defines the unsigned integer type that has the same size as the\n            // floating point number.\n            typedef typename TypeWithSize<sizeof(RawType)>::UInt Bits;\n\n            // Constants.\n\n            // # of bits in a number.\n            static const size_t kBitCount = 8*sizeof(RawType);\n\n            // # of fraction bits in a number.\n            static const size_t kFractionBitCount =\n                std::numeric_limits<RawType>::digits - 1;\n\n            // # of exponent bits in a number.\n            static const size_t kExponentBitCount = kBitCount - 1 - kFractionBitCount;\n\n            // The mask for the sign bit.\n            static const Bits kSignBitMask = static_cast<Bits>(1) << (kBitCount - 1);\n\n            // The mask for the fraction bits.\n            static const Bits kFractionBitMask =\n                ~static_cast<Bits>(0) >> (kExponentBitCount + 1);\n\n            // The mask for the exponent bits.\n            static const Bits kExponentBitMask = ~(kSignBitMask | kFractionBitMask);\n\n            // How many ULP's (Units in the Last Place) we want to tolerate when\n            // comparing two numbers.  The larger the value, the more error we\n            // allow.  A 0 value means that two numbers must be exactly the same\n            // to be considered equal.\n            //\n            // The maximum error of a single floating-point operation is 0.5\n            // units in the last place.  On Intel CPU's, all floating-point\n            // calculations are done with 80-bit precision, while double has 64\n            // bits.  Therefore, 4 should be enough for ordinary use.\n            //\n            // See the following article for more details on ULP:\n            // http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/\n            static const size_t kMaxUlps = 4;\n\n            // Constructs a FloatingPoint from a raw floating-point number.\n            //\n            // On an Intel CPU, passing a non-normalized NAN (Not a Number)\n            // around may change its bits, although the new value is guaranteed\n            // to be also a NAN.  Therefore, don't expect this constructor to\n            // preserve the bits in x when x is a NAN.\n            explicit FloatingPoint(const RawType &x)\n            {\n                u_.value_ = x;\n            }\n\n            // Static methods\n\n            // Reinterprets a bit pattern as a floating-point number.\n            //\n            // This function is needed to test the AlmostEquals() method.\n            static RawType ReinterpretBits(const Bits bits)\n            {\n                FloatingPoint fp(0);\n                fp.u_.bits_ = bits;\n                return fp.u_.value_;\n            }\n\n            // Returns the floating-point number that represent positive infinity.\n            static RawType Infinity()\n            {\n                return ReinterpretBits(kExponentBitMask);\n            }\n\n            // Returns the maximum representable finite floating-point number.\n            static RawType Max();\n\n            // Non-static methods\n\n            // Returns the bits that represents this number.\n            const Bits &bits() const\n            {\n                return u_.bits_;\n            }\n\n            // Returns the exponent bits of this number.\n            Bits exponent_bits() const\n            {\n                return kExponentBitMask & u_.bits_;\n            }\n\n            // Returns the fraction bits of this number.\n            Bits fraction_bits() const\n            {\n                return kFractionBitMask & u_.bits_;\n            }\n\n            // Returns the sign bit of this number.\n            Bits sign_bit() const\n            {\n                return kSignBitMask & u_.bits_;\n            }\n\n            // Returns true iff this is NAN (not a number).\n            bool is_nan() const\n            {\n                // It's a NAN if the exponent bits are all ones and the fraction\n                // bits are not entirely zeros.\n                return (exponent_bits() == kExponentBitMask) && (fraction_bits() != 0);\n            }\n\n            // Returns true iff this number is at most kMaxUlps ULP's away from\n            // rhs.  In particular, this function:\n            //\n            //   - returns false if either number is (or both are) NAN.\n            //   - treats really large numbers as almost equal to infinity.\n            //   - thinks +0.0 and -0.0 are 0 DLP's apart.\n            bool AlmostEquals(const FloatingPoint &rhs) const\n            {\n                // The IEEE standard says that any comparison operation involving\n                // a NAN must return false.\n                if (is_nan() || rhs.is_nan()) return false;\n\n                return DistanceBetweenSignAndMagnitudeNumbers(u_.bits_, rhs.u_.bits_)\n                       <= kMaxUlps;\n            }\n\n        private:\n            // The data type used to store the actual floating-point number.\n            union FloatingPointUnion {\n                RawType value_;  // The raw floating-point number.\n                Bits bits_;      // The bits that represent the number.\n            };\n\n            // Converts an integer from the sign-and-magnitude representation to\n            // the biased representation.  More precisely, let N be 2 to the\n            // power of (kBitCount - 1), an integer x is represented by the\n            // unsigned number x + N.\n            //\n            // For instance,\n            //\n            //   -N + 1 (the most negative number representable using\n            //          sign-and-magnitude) is represented by 1;\n            //   0      is represented by N; and\n            //   N - 1  (the biggest number representable using\n            //          sign-and-magnitude) is represented by 2N - 1.\n            //\n            // Read http://en.wikipedia.org/wiki/Signed_number_representations\n            // for more details on signed number representations.\n            static Bits SignAndMagnitudeToBiased(const Bits &sam)\n            {\n                if (kSignBitMask & sam) {\n                    // sam represents a negative number.\n                    return ~sam + 1;\n                } else {\n                    // sam represents a positive number.\n                    return kSignBitMask | sam;\n                }\n            }\n\n            // Given two numbers in the sign-and-magnitude representation,\n            // returns the distance between them as an unsigned number.\n            static Bits DistanceBetweenSignAndMagnitudeNumbers(const Bits &sam1,\n                                                               const Bits &sam2)\n            {\n                const Bits biased1 = SignAndMagnitudeToBiased(sam1);\n                const Bits biased2 = SignAndMagnitudeToBiased(sam2);\n                return (biased1 >= biased2) ? (biased1 - biased2) : (biased2 - biased1);\n            }\n\n            FloatingPointUnion u_;\n        };\n\n// We cannot use std::numeric_limits<T>::max() as it clashes with the max()\n// macro defined by <windows.h>.\n        template <>\n        inline float FloatingPoint<float>::Max()\n        {\n            return FLT_MAX;\n        }\n        template <>\n        inline double FloatingPoint<double>::Max()\n        {\n            return DBL_MAX;\n        }\n\n// Typedefs the instances of the FloatingPoint template class that we\n// care to use.\n        typedef FloatingPoint<float> Float;\n        typedef FloatingPoint<double> Double;\n\n// In order to catch the mistake of putting tests that use different\n// test fixture classes in the same test case, we need to assign\n// unique IDs to fixture classes and compare them.  The TypeId type is\n// used to hold such IDs.  The user should treat TypeId as an opaque\n// type: the only operation allowed on TypeId values is to compare\n// them for equality using the == operator.\n        typedef const void *TypeId;\n\n        template <typename T>\n        class TypeIdHelper\n        {\n        public:\n            // dummy_ must not have a const type.  Otherwise an overly eager\n            // compiler (e.g. MSVC 7.1 & 8.0) may try to merge\n            // TypeIdHelper<T>::dummy_ for different Ts as an \"optimization\".\n            static bool dummy_;\n        };\n\n        template <typename T>\n        bool TypeIdHelper<T>::dummy_ = false;\n\n// GetTypeId<T>() returns the ID of type T.  Different values will be\n// returned for different types.  Calling the function twice with the\n// same type argument is guaranteed to return the same ID.\n        template <typename T>\n        TypeId GetTypeId()\n        {\n            // The compiler is required to allocate a different\n            // TypeIdHelper<T>::dummy_ variable for each T used to instantiate\n            // the template.  Therefore, the address of dummy_ is guaranteed to\n            // be unique.\n            return &(TypeIdHelper<T>::dummy_);\n        }\n\n// Returns the type ID of ::testing::Test.  Always call this instead\n// of GetTypeId< ::testing::Test>() to get the type ID of\n// ::testing::Test, as the latter may give the wrong result due to a\n// suspected linker bug when compiling Google Test as a Mac OS X\n// framework.\n        GTEST_API_ TypeId GetTestTypeId();\n\n// Defines the abstract factory interface that creates instances\n// of a Test object.\n        class TestFactoryBase\n        {\n        public:\n            virtual ~TestFactoryBase() {}\n\n            // Creates a test instance to run. The instance is both created and destroyed\n            // within TestInfoImpl::Run()\n            virtual Test *CreateTest() = 0;\n\n        protected:\n            TestFactoryBase() {}\n\n        private:\n            GTEST_DISALLOW_COPY_AND_ASSIGN_(TestFactoryBase);\n        };\n\n// This class provides implementation of TeastFactoryBase interface.\n// It is used in TEST and TEST_F macros.\n        template <class TestClass>\n        class TestFactoryImpl : public TestFactoryBase\n        {\n        public:\n            virtual Test *CreateTest()\n            {\n                return new TestClass;\n            }\n        };\n\n#if GTEST_OS_WINDOWS\n\n// Predicate-formatters for implementing the HRESULT checking macros\n// {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED}\n// We pass a long instead of HRESULT to avoid causing an\n// include dependency for the HRESULT type.\n        GTEST_API_ AssertionResult IsHRESULTSuccess(const char *expr,\n                                                    long hr);  // NOLINT\n        GTEST_API_ AssertionResult IsHRESULTFailure(const char *expr,\n                                                    long hr);  // NOLINT\n\n#endif  // GTEST_OS_WINDOWS\n\n// Types of SetUpTestCase() and TearDownTestCase() functions.\n        typedef void (*SetUpTestCaseFunc)();\n        typedef void (*TearDownTestCaseFunc)();\n\n        struct CodeLocation {\n            CodeLocation(const std::string &a_file, int a_line)\n                : file(a_file), line(a_line) {}\n\n            std::string file;\n            int line;\n        };\n\n// Creates a new TestInfo object and registers it with Google Test;\n// returns the created object.\n//\n// Arguments:\n//\n//   test_case_name:   name of the test case\n//   name:             name of the test\n//   type_param        the name of the test's type parameter, or NULL if\n//                     this is not a typed or a type-parameterized test.\n//   value_param       text representation of the test's value parameter,\n//                     or NULL if this is not a type-parameterized test.\n//   code_location:    code location where the test is defined\n//   fixture_class_id: ID of the test fixture class\n//   set_up_tc:        pointer to the function that sets up the test case\n//   tear_down_tc:     pointer to the function that tears down the test case\n//   factory:          pointer to the factory that creates a test object.\n//                     The newly created TestInfo instance will assume\n//                     ownership of the factory object.\n        GTEST_API_ TestInfo *MakeAndRegisterTestInfo(\n            const char *test_case_name,\n            const char *name,\n            const char *type_param,\n            const char *value_param,\n            CodeLocation code_location,\n            TypeId fixture_class_id,\n            SetUpTestCaseFunc set_up_tc,\n            TearDownTestCaseFunc tear_down_tc,\n            TestFactoryBase *factory);\n\n// If *pstr starts with the given prefix, modifies *pstr to be right\n// past the prefix and returns true; otherwise leaves *pstr unchanged\n// and returns false.  None of pstr, *pstr, and prefix can be NULL.\n        GTEST_API_ bool SkipPrefix(const char *prefix, const char **pstr);\n\n#if GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P\n\n// State of the definition of a type-parameterized test case.\n        class GTEST_API_ TypedTestCasePState\n        {\n        public:\n            TypedTestCasePState() : registered_(false) {}\n\n            // Adds the given test name to defined_test_names_ and return true\n            // if the test case hasn't been registered; otherwise aborts the\n            // program.\n            bool AddTestName(const char *file, int line, const char *case_name,\n                             const char *test_name)\n            {\n                if (registered_) {\n                    fprintf(stderr, \"%s Test %s must be defined before \"\n                            \"REGISTER_TYPED_TEST_CASE_P(%s, ...).\\n\",\n                            FormatFileLocation(file, line).c_str(), test_name, case_name);\n                    fflush(stderr);\n                    posix::Abort();\n                }\n                registered_tests_.insert(\n                    ::std::make_pair(test_name, CodeLocation(file, line)));\n                return true;\n            }\n\n            bool TestExists(const std::string &test_name) const\n            {\n                return registered_tests_.count(test_name) > 0;\n            }\n\n            const CodeLocation &GetCodeLocation(const std::string &test_name) const\n            {\n                RegisteredTestsMap::const_iterator it = registered_tests_.find(test_name);\n                GTEST_CHECK_(it != registered_tests_.end());\n                return it->second;\n            }\n\n            // Verifies that registered_tests match the test names in\n            // defined_test_names_; returns registered_tests if successful, or\n            // aborts the program otherwise.\n            const char *VerifyRegisteredTestNames(\n                const char *file, int line, const char *registered_tests);\n\n        private:\n            typedef ::std::map<std::string, CodeLocation> RegisteredTestsMap;\n\n            bool registered_;\n            RegisteredTestsMap registered_tests_;\n        };\n\n// Skips to the first non-space char after the first comma in 'str';\n// returns NULL if no comma is found in 'str'.\n        inline const char *SkipComma(const char *str)\n        {\n            const char *comma = strchr(str, ',');\n            if (comma == NULL) {\n                return NULL;\n            }\n            while (IsSpace(*(++comma))) {}\n            return comma;\n        }\n\n// Returns the prefix of 'str' before the first comma in it; returns\n// the entire string if it contains no comma.\n        inline std::string GetPrefixUntilComma(const char *str)\n        {\n            const char *comma = strchr(str, ',');\n            return comma == NULL ? str : std::string(str, comma);\n        }\n\n// Splits a given string on a given delimiter, populating a given\n// vector with the fields.\n        void SplitString(const ::std::string &str, char delimiter,\n                         ::std::vector< ::std::string> *dest);\n\n// TypeParameterizedTest<Fixture, TestSel, Types>::Register()\n// registers a list of type-parameterized tests with Google Test.  The\n// return value is insignificant - we just need to return something\n// such that we can call this function in a namespace scope.\n//\n// Implementation note: The GTEST_TEMPLATE_ macro declares a template\n// template parameter.  It's defined in gtest-type-util.h.\n        template <GTEST_TEMPLATE_ Fixture, class TestSel, typename Types>\n        class TypeParameterizedTest\n        {\n        public:\n            // 'index' is the index of the test in the type list 'Types'\n            // specified in INSTANTIATE_TYPED_TEST_CASE_P(Prefix, TestCase,\n            // Types).  Valid values for 'index' are [0, N - 1] where N is the\n            // length of Types.\n            static bool Register(const char *prefix,\n                                 CodeLocation code_location,\n                                 const char *case_name, const char *test_names,\n                                 int index)\n            {\n                typedef typename Types::Head Type;\n                typedef Fixture<Type> FixtureClass;\n                typedef typename GTEST_BIND_(TestSel, Type) TestClass;\n\n                // First, registers the first type-parameterized test in the type\n                // list.\n                MakeAndRegisterTestInfo(\n                    (std::string(prefix) + (prefix[0] == '\\0' ? \"\" : \"/\") + case_name + \"/\"\n                     + StreamableToString(index)).c_str(),\n                    StripTrailingSpaces(GetPrefixUntilComma(test_names)).c_str(),\n                    GetTypeName<Type>().c_str(),\n                    NULL,  // No value parameter.\n                    code_location,\n                    GetTypeId<FixtureClass>(),\n                    TestClass::SetUpTestCase,\n                    TestClass::TearDownTestCase,\n                    new TestFactoryImpl<TestClass>);\n\n                // Next, recurses (at compile time) with the tail of the type list.\n                return TypeParameterizedTest<Fixture, TestSel, typename Types::Tail>\n                       ::Register(prefix, code_location, case_name, test_names, index + 1);\n            }\n        };\n\n// The base case for the compile time recursion.\n        template <GTEST_TEMPLATE_ Fixture, class TestSel>\n        class TypeParameterizedTest<Fixture, TestSel, Types0>\n        {\n        public:\n            static bool Register(const char * /*prefix*/, CodeLocation,\n                                 const char * /*case_name*/, const char * /*test_names*/,\n                                 int /*index*/)\n            {\n                return true;\n            }\n        };\n\n// TypeParameterizedTestCase<Fixture, Tests, Types>::Register()\n// registers *all combinations* of 'Tests' and 'Types' with Google\n// Test.  The return value is insignificant - we just need to return\n// something such that we can call this function in a namespace scope.\n        template <GTEST_TEMPLATE_ Fixture, typename Tests, typename Types>\n        class TypeParameterizedTestCase\n        {\n        public:\n            static bool Register(const char *prefix, CodeLocation code_location,\n                                 const TypedTestCasePState *state,\n                                 const char *case_name, const char *test_names)\n            {\n                std::string test_name = StripTrailingSpaces(\n                                            GetPrefixUntilComma(test_names));\n                if (!state->TestExists(test_name)) {\n                    fprintf(stderr, \"Failed to get code location for test %s.%s at %s.\",\n                            case_name, test_name.c_str(),\n                            FormatFileLocation(code_location.file.c_str(),\n                                               code_location.line).c_str());\n                    fflush(stderr);\n                    posix::Abort();\n                }\n                const CodeLocation &test_location = state->GetCodeLocation(test_name);\n\n                typedef typename Tests::Head Head;\n\n                // First, register the first test in 'Test' for each type in 'Types'.\n                TypeParameterizedTest<Fixture, Head, Types>::Register(\n                    prefix, test_location, case_name, test_names, 0);\n\n                // Next, recurses (at compile time) with the tail of the test list.\n                return TypeParameterizedTestCase<Fixture, typename Tests::Tail, Types>\n                       ::Register(prefix, code_location, state,\n                                  case_name, SkipComma(test_names));\n            }\n        };\n\n// The base case for the compile time recursion.\n        template <GTEST_TEMPLATE_ Fixture, typename Types>\n        class TypeParameterizedTestCase<Fixture, Templates0, Types>\n        {\n        public:\n            static bool Register(const char * /*prefix*/, CodeLocation,\n                                 const TypedTestCasePState * /*state*/,\n                                 const char * /*case_name*/, const char * /*test_names*/)\n            {\n                return true;\n            }\n        };\n\n#endif  // GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P\n\n// Returns the current OS stack trace as an std::string.\n//\n// The maximum number of stack frames to be included is specified by\n// the gtest_stack_trace_depth flag.  The skip_count parameter\n// specifies the number of top frames to be skipped, which doesn't\n// count against the number of frames to be included.\n//\n// For example, if Foo() calls Bar(), which in turn calls\n// GetCurrentOsStackTraceExceptTop(..., 1), Foo() will be included in\n// the trace but Bar() and GetCurrentOsStackTraceExceptTop() won't.\n        GTEST_API_ std::string GetCurrentOsStackTraceExceptTop(\n            UnitTest *unit_test, int skip_count);\n\n// Helpers for suppressing warnings on unreachable code or constant\n// condition.\n\n// Always returns true.\n        GTEST_API_ bool AlwaysTrue();\n\n// Always returns false.\n        inline bool AlwaysFalse()\n        {\n            return !AlwaysTrue();\n        }\n\n// Helper for suppressing false warning from Clang on a const char*\n// variable declared in a conditional expression always being NULL in\n// the else branch.\n        struct GTEST_API_ ConstCharPtr {\n            ConstCharPtr(const char *str) : value(str) {}\n            operator bool() const\n            {\n                return true;\n            }\n            const char *value;\n        };\n\n// A simple Linear Congruential Generator for generating random\n// numbers with a uniform distribution.  Unlike rand() and srand(), it\n// doesn't use global state (and therefore can't interfere with user\n// code).  Unlike rand_r(), it's portable.  An LCG isn't very random,\n// but it's good enough for our purposes.\n        class GTEST_API_ Random\n        {\n        public:\n            static const UInt32 kMaxRange = 1u << 31;\n\n            explicit Random(UInt32 seed) : state_(seed) {}\n\n            void Reseed(UInt32 seed)\n            {\n                state_ = seed;\n            }\n\n            // Generates a random number from [0, range).  Crashes if 'range' is\n            // 0 or greater than kMaxRange.\n            UInt32 Generate(UInt32 range);\n\n        private:\n            UInt32 state_;\n            GTEST_DISALLOW_COPY_AND_ASSIGN_(Random);\n        };\n\n// Defining a variable of type CompileAssertTypesEqual<T1, T2> will cause a\n// compiler error iff T1 and T2 are different types.\n        template <typename T1, typename T2>\n        struct CompileAssertTypesEqual;\n\n        template <typename T>\n        struct CompileAssertTypesEqual<T, T> {\n        };\n\n// Removes the reference from a type if it is a reference type,\n// otherwise leaves it unchanged.  This is the same as\n// tr1::remove_reference, which is not widely available yet.\n        template <typename T>\n        struct RemoveReference {\n            typedef T type;\n        };  // NOLINT\n        template <typename T>\n        struct RemoveReference<T &> {\n            typedef T type;\n        };  // NOLINT\n\n// A handy wrapper around RemoveReference that works when the argument\n// T depends on template parameters.\n#define GTEST_REMOVE_REFERENCE_(T) \\\n    typename ::testing::internal::RemoveReference<T>::type\n\n// Removes const from a type if it is a const type, otherwise leaves\n// it unchanged.  This is the same as tr1::remove_const, which is not\n// widely available yet.\n        template <typename T>\n        struct RemoveConst {\n            typedef T type;\n        };  // NOLINT\n        template <typename T>\n        struct RemoveConst<const T> {\n            typedef T type;\n        };  // NOLINT\n\n// MSVC 8.0, Sun C++, and IBM XL C++ have a bug which causes the above\n// definition to fail to remove the const in 'const int[3]' and 'const\n// char[3][4]'.  The following specialization works around the bug.\n        template <typename T, size_t N>\n        struct RemoveConst<const T[N]> {\n            typedef typename RemoveConst<T>::type type[N];\n        };\n\n#if defined(_MSC_VER) && _MSC_VER < 1400\n// This is the only specialization that allows VC++ 7.1 to remove const in\n// 'const int[3] and 'const int[3][4]'.  However, it causes trouble with GCC\n// and thus needs to be conditionally compiled.\n        template <typename T, size_t N>\n        struct RemoveConst<T[N]> {\n            typedef typename RemoveConst<T>::type type[N];\n        };\n#endif\n\n// A handy wrapper around RemoveConst that works when the argument\n// T depends on template parameters.\n#define GTEST_REMOVE_CONST_(T) \\\n    typename ::testing::internal::RemoveConst<T>::type\n\n// Turns const U&, U&, const U, and U all into U.\n#define GTEST_REMOVE_REFERENCE_AND_CONST_(T) \\\n    GTEST_REMOVE_CONST_(GTEST_REMOVE_REFERENCE_(T))\n\n// Adds reference to a type if it is not a reference type,\n// otherwise leaves it unchanged.  This is the same as\n// tr1::add_reference, which is not widely available yet.\n        template <typename T>\n        struct AddReference {\n            typedef T &type;\n        };  // NOLINT\n        template <typename T>\n        struct AddReference<T &> {\n            typedef T &type;\n        };  // NOLINT\n\n// A handy wrapper around AddReference that works when the argument T\n// depends on template parameters.\n#define GTEST_ADD_REFERENCE_(T) \\\n    typename ::testing::internal::AddReference<T>::type\n\n// Adds a reference to const on top of T as necessary.  For example,\n// it transforms\n//\n//   char         ==> const char&\n//   const char   ==> const char&\n//   char&        ==> const char&\n//   const char&  ==> const char&\n//\n// The argument T must depend on some template parameters.\n#define GTEST_REFERENCE_TO_CONST_(T) \\\n    GTEST_ADD_REFERENCE_(const GTEST_REMOVE_REFERENCE_(T))\n\n// ImplicitlyConvertible<From, To>::value is a compile-time bool\n// constant that's true iff type From can be implicitly converted to\n// type To.\n        template <typename From, typename To>\n        class ImplicitlyConvertible\n        {\n        private:\n            // We need the following helper functions only for their types.\n            // They have no implementations.\n\n            // MakeFrom() is an expression whose type is From.  We cannot simply\n            // use From(), as the type From may not have a public default\n            // constructor.\n            static typename AddReference<From>::type MakeFrom();\n\n            // These two functions are overloaded.  Given an expression\n            // Helper(x), the compiler will pick the first version if x can be\n            // implicitly converted to type To; otherwise it will pick the\n            // second version.\n            //\n            // The first version returns a value of size 1, and the second\n            // version returns a value of size 2.  Therefore, by checking the\n            // size of Helper(x), which can be done at compile time, we can tell\n            // which version of Helper() is used, and hence whether x can be\n            // implicitly converted to type To.\n            static char Helper(To);\n            static char (&Helper(...))[2];  // NOLINT\n\n            // We have to put the 'public' section after the 'private' section,\n            // or MSVC refuses to compile the code.\n        public:\n#if defined(__BORLANDC__)\n            // C++Builder cannot use member overload resolution during template\n            // instantiation.  The simplest workaround is to use its C++0x type traits\n            // functions (C++Builder 2009 and above only).\n            static const bool value = __is_convertible(From, To);\n#else\n            // MSVC warns about implicitly converting from double to int for\n            // possible loss of data, so we need to temporarily disable the\n            // warning.\n            GTEST_DISABLE_MSC_WARNINGS_PUSH_(4244)\n            static const bool value =\n                sizeof(Helper(ImplicitlyConvertible::MakeFrom())) == 1;\n            GTEST_DISABLE_MSC_WARNINGS_POP_()\n#endif  // __BORLANDC__\n        };\n        template <typename From, typename To>\n        const bool ImplicitlyConvertible<From, To>::value;\n\n// IsAProtocolMessage<T>::value is a compile-time bool constant that's\n// true iff T is type ProtocolMessage, proto2::Message, or a subclass\n// of those.\n        template <typename T>\n        struct IsAProtocolMessage\n            : public bool_constant<\n              ImplicitlyConvertible<const T *, const ::ProtocolMessage *>::value ||\n              ImplicitlyConvertible<const T *, const ::proto2::Message *>::value> {\n        };\n\n// When the compiler sees expression IsContainerTest<C>(0), if C is an\n// STL-style container class, the first overload of IsContainerTest\n// will be viable (since both C::iterator* and C::const_iterator* are\n// valid types and NULL can be implicitly converted to them).  It will\n// be picked over the second overload as 'int' is a perfect match for\n// the type of argument 0.  If C::iterator or C::const_iterator is not\n// a valid type, the first overload is not viable, and the second\n// overload will be picked.  Therefore, we can determine whether C is\n// a container class by checking the type of IsContainerTest<C>(0).\n// The value of the expression is insignificant.\n//\n// Note that we look for both C::iterator and C::const_iterator.  The\n// reason is that C++ injects the name of a class as a member of the\n// class itself (e.g. you can refer to class iterator as either\n// 'iterator' or 'iterator::iterator').  If we look for C::iterator\n// only, for example, we would mistakenly think that a class named\n// iterator is an STL container.\n//\n// Also note that the simpler approach of overloading\n// IsContainerTest(typename C::const_iterator*) and\n// IsContainerTest(...) doesn't work with Visual Age C++ and Sun C++.\n        typedef int IsContainer;\n        template <class C>\n        IsContainer IsContainerTest(int /* dummy */,\n                                    typename C::iterator * /* it */ = NULL,\n                                    typename C::const_iterator * /* const_it */ = NULL)\n        {\n            return 0;\n        }\n\n        typedef char IsNotContainer;\n        template <class C>\n        IsNotContainer IsContainerTest(long /* dummy */)\n        {\n            return '\\0';\n        }\n\n// EnableIf<condition>::type is void when 'Cond' is true, and\n// undefined when 'Cond' is false.  To use SFINAE to make a function\n// overload only apply when a particular expression is true, add\n// \"typename EnableIf<expression>::type* = 0\" as the last parameter.\n        template<bool> struct EnableIf;\n        template<> struct EnableIf<true> {\n            typedef void type;\n        };  // NOLINT\n\n// Utilities for native arrays.\n\n// ArrayEq() compares two k-dimensional native arrays using the\n// elements' operator==, where k can be any integer >= 0.  When k is\n// 0, ArrayEq() degenerates into comparing a single pair of values.\n\n        template <typename T, typename U>\n        bool ArrayEq(const T *lhs, size_t size, const U *rhs);\n\n// This generic version is used when k is 0.\n        template <typename T, typename U>\n        inline bool ArrayEq(const T &lhs, const U &rhs)\n        {\n            return lhs == rhs;\n        }\n\n// This overload is used when k >= 1.\n        template <typename T, typename U, size_t N>\n        inline bool ArrayEq(const T(&lhs)[N], const U(&rhs)[N])\n        {\n            return internal::ArrayEq(lhs, N, rhs);\n        }\n\n// This helper reduces code bloat.  If we instead put its logic inside\n// the previous ArrayEq() function, arrays with different sizes would\n// lead to different copies of the template code.\n        template <typename T, typename U>\n        bool ArrayEq(const T *lhs, size_t size, const U *rhs)\n        {\n            for (size_t i = 0; i != size; i++) {\n                if (!internal::ArrayEq(lhs[i], rhs[i]))\n                    return false;\n            }\n            return true;\n        }\n\n// Finds the first element in the iterator range [begin, end) that\n// equals elem.  Element may be a native array type itself.\n        template <typename Iter, typename Element>\n        Iter ArrayAwareFind(Iter begin, Iter end, const Element &elem)\n        {\n            for (Iter it = begin; it != end; ++it) {\n                if (internal::ArrayEq(*it, elem))\n                    return it;\n            }\n            return end;\n        }\n\n// CopyArray() copies a k-dimensional native array using the elements'\n// operator=, where k can be any integer >= 0.  When k is 0,\n// CopyArray() degenerates into copying a single value.\n\n        template <typename T, typename U>\n        void CopyArray(const T *from, size_t size, U *to);\n\n// This generic version is used when k is 0.\n        template <typename T, typename U>\n        inline void CopyArray(const T &from, U *to)\n        {\n            *to = from;\n        }\n\n// This overload is used when k >= 1.\n        template <typename T, typename U, size_t N>\n        inline void CopyArray(const T(&from)[N], U(*to)[N])\n        {\n            internal::CopyArray(from, N, *to);\n        }\n\n// This helper reduces code bloat.  If we instead put its logic inside\n// the previous CopyArray() function, arrays with different sizes\n// would lead to different copies of the template code.\n        template <typename T, typename U>\n        void CopyArray(const T *from, size_t size, U *to)\n        {\n            for (size_t i = 0; i != size; i++) {\n                internal::CopyArray(from[i], to + i);\n            }\n        }\n\n// The relation between an NativeArray object (see below) and the\n// native array it represents.\n// We use 2 different structs to allow non-copyable types to be used, as long\n// as RelationToSourceReference() is passed.\n        struct RelationToSourceReference {};\n        struct RelationToSourceCopy {};\n\n// Adapts a native array to a read-only STL-style container.  Instead\n// of the complete STL container concept, this adaptor only implements\n// members useful for Google Mock's container matchers.  New members\n// should be added as needed.  To simplify the implementation, we only\n// support Element being a raw type (i.e. having no top-level const or\n// reference modifier).  It's the client's responsibility to satisfy\n// this requirement.  Element can be an array type itself (hence\n// multi-dimensional arrays are supported).\n        template <typename Element>\n        class NativeArray\n        {\n        public:\n            // STL-style container typedefs.\n            typedef Element value_type;\n            typedef Element *iterator;\n            typedef const Element *const_iterator;\n\n            // Constructs from a native array. References the source.\n            NativeArray(const Element *array, size_t count, RelationToSourceReference)\n            {\n                InitRef(array, count);\n            }\n\n            // Constructs from a native array. Copies the source.\n            NativeArray(const Element *array, size_t count, RelationToSourceCopy)\n            {\n                InitCopy(array, count);\n            }\n\n            // Copy constructor.\n            NativeArray(const NativeArray &rhs)\n            {\n                (this->*rhs.clone_)(rhs.array_, rhs.size_);\n            }\n\n            ~NativeArray()\n            {\n                if (clone_ != &NativeArray::InitRef)\n                    delete[] array_;\n            }\n\n            // STL-style container methods.\n            size_t size() const\n            {\n                return size_;\n            }\n            const_iterator begin() const\n            {\n                return array_;\n            }\n            const_iterator end() const\n            {\n                return array_ + size_;\n            }\n            bool operator==(const NativeArray &rhs) const\n            {\n                return size() == rhs.size() &&\n                       ArrayEq(begin(), size(), rhs.begin());\n            }\n\n        private:\n            enum {\n                kCheckTypeIsNotConstOrAReference = StaticAssertTypeEqHelper<\n                                                   Element, GTEST_REMOVE_REFERENCE_AND_CONST_(Element)>::value,\n            };\n\n            // Initializes this object with a copy of the input.\n            void InitCopy(const Element *array, size_t a_size)\n            {\n                Element *const copy = new Element[a_size];\n                CopyArray(array, a_size, copy);\n                array_ = copy;\n                size_ = a_size;\n                clone_ = &NativeArray::InitCopy;\n            }\n\n            // Initializes this object with a reference of the input.\n            void InitRef(const Element *array, size_t a_size)\n            {\n                array_ = array;\n                size_ = a_size;\n                clone_ = &NativeArray::InitRef;\n            }\n\n            const Element *array_;\n            size_t size_;\n            void (NativeArray::*clone_)(const Element *, size_t);\n\n            GTEST_DISALLOW_ASSIGN_(NativeArray);\n        };\n\n    }  // namespace internal\n}  // namespace testing\n\n#define GTEST_MESSAGE_AT_(file, line, message, result_type) \\\n  ::testing::internal::AssertHelper(result_type, file, line, message) \\\n    = ::testing::Message()\n\n#define GTEST_MESSAGE_(message, result_type) \\\n  GTEST_MESSAGE_AT_(__FILE__, __LINE__, message, result_type)\n\n#define GTEST_FATAL_FAILURE_(message) \\\n  return GTEST_MESSAGE_(message, ::testing::TestPartResult::kFatalFailure)\n\n#define GTEST_NONFATAL_FAILURE_(message) \\\n  GTEST_MESSAGE_(message, ::testing::TestPartResult::kNonFatalFailure)\n\n#define GTEST_SUCCESS_(message) \\\n  GTEST_MESSAGE_(message, ::testing::TestPartResult::kSuccess)\n\n// Suppresses MSVC warnings 4072 (unreachable code) for the code following\n// statement if it returns or throws (or doesn't return or throw in some\n// situations).\n#define GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement) \\\n  if (::testing::internal::AlwaysTrue()) { statement; }\n\n#define GTEST_TEST_THROW_(statement, expected_exception, fail) \\\n  GTEST_AMBIGUOUS_ELSE_BLOCKER_ \\\n  if (::testing::internal::ConstCharPtr gtest_msg = \"\") { \\\n    bool gtest_caught_expected = false; \\\n    try { \\\n      GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \\\n    } \\\n    catch (expected_exception const&) { \\\n      gtest_caught_expected = true; \\\n    } \\\n    catch (...) { \\\n      gtest_msg.value = \\\n          \"Expected: \" #statement \" throws an exception of type \" \\\n          #expected_exception \".\\n  Actual: it throws a different type.\"; \\\n      goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \\\n    } \\\n    if (!gtest_caught_expected) { \\\n      gtest_msg.value = \\\n          \"Expected: \" #statement \" throws an exception of type \" \\\n          #expected_exception \".\\n  Actual: it throws nothing.\"; \\\n      goto GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__); \\\n    } \\\n  } else \\\n    GTEST_CONCAT_TOKEN_(gtest_label_testthrow_, __LINE__): \\\n      fail(gtest_msg.value)\n\n#define GTEST_TEST_NO_THROW_(statement, fail) \\\n  GTEST_AMBIGUOUS_ELSE_BLOCKER_ \\\n  if (::testing::internal::AlwaysTrue()) { \\\n    try { \\\n      GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \\\n    } \\\n    catch (...) { \\\n      goto GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__); \\\n    } \\\n  } else \\\n    GTEST_CONCAT_TOKEN_(gtest_label_testnothrow_, __LINE__): \\\n      fail(\"Expected: \" #statement \" doesn't throw an exception.\\n\" \\\n           \"  Actual: it throws.\")\n\n#define GTEST_TEST_ANY_THROW_(statement, fail) \\\n  GTEST_AMBIGUOUS_ELSE_BLOCKER_ \\\n  if (::testing::internal::AlwaysTrue()) { \\\n    bool gtest_caught_any = false; \\\n    try { \\\n      GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \\\n    } \\\n    catch (...) { \\\n      gtest_caught_any = true; \\\n    } \\\n    if (!gtest_caught_any) { \\\n      goto GTEST_CONCAT_TOKEN_(gtest_label_testanythrow_, __LINE__); \\\n    } \\\n  } else \\\n    GTEST_CONCAT_TOKEN_(gtest_label_testanythrow_, __LINE__): \\\n      fail(\"Expected: \" #statement \" throws an exception.\\n\" \\\n           \"  Actual: it doesn't.\")\n\n\n// Implements Boolean test assertions such as EXPECT_TRUE. expression can be\n// either a boolean expression or an AssertionResult. text is a textual\n// represenation of expression as it was passed into the EXPECT_TRUE.\n#define GTEST_TEST_BOOLEAN_(expression, text, actual, expected, fail) \\\n  GTEST_AMBIGUOUS_ELSE_BLOCKER_ \\\n  if (const ::testing::AssertionResult gtest_ar_ = \\\n      ::testing::AssertionResult(expression)) \\\n    ; \\\n  else \\\n    fail(::testing::internal::GetBoolAssertionFailureMessage(\\\n        gtest_ar_, text, #actual, #expected).c_str())\n\n#define GTEST_TEST_NO_FATAL_FAILURE_(statement, fail) \\\n  GTEST_AMBIGUOUS_ELSE_BLOCKER_ \\\n  if (::testing::internal::AlwaysTrue()) { \\\n    ::testing::internal::HasNewFatalFailureHelper gtest_fatal_failure_checker; \\\n    GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \\\n    if (gtest_fatal_failure_checker.has_new_fatal_failure()) { \\\n      goto GTEST_CONCAT_TOKEN_(gtest_label_testnofatal_, __LINE__); \\\n    } \\\n  } else \\\n    GTEST_CONCAT_TOKEN_(gtest_label_testnofatal_, __LINE__): \\\n      fail(\"Expected: \" #statement \" doesn't generate new fatal \" \\\n           \"failures in the current thread.\\n\" \\\n           \"  Actual: it does.\")\n\n// Expands to the name of the class that implements the given test.\n#define GTEST_TEST_CLASS_NAME_(test_case_name, test_name) \\\n  test_case_name##_##test_name##_Test\n\n// Helper macro for defining tests.\n#define GTEST_TEST_(test_case_name, test_name, parent_class, parent_id)\\\nclass GTEST_TEST_CLASS_NAME_(test_case_name, test_name) : public parent_class {\\\n public:\\\n  GTEST_TEST_CLASS_NAME_(test_case_name, test_name)() {}\\\n private:\\\n  virtual void TestBody();\\\n  static ::testing::TestInfo* const test_info_ GTEST_ATTRIBUTE_UNUSED_;\\\n  GTEST_DISALLOW_COPY_AND_ASSIGN_(\\\n      GTEST_TEST_CLASS_NAME_(test_case_name, test_name));\\\n};\\\n\\\n::testing::TestInfo* const GTEST_TEST_CLASS_NAME_(test_case_name, test_name)\\\n  ::test_info_ =\\\n    ::testing::internal::MakeAndRegisterTestInfo(\\\n        #test_case_name, #test_name, NULL, NULL, \\\n        ::testing::internal::CodeLocation(__FILE__, __LINE__), \\\n        (parent_id), \\\n        parent_class::SetUpTestCase, \\\n        parent_class::TearDownTestCase, \\\n        new ::testing::internal::TestFactoryImpl<\\\n            GTEST_TEST_CLASS_NAME_(test_case_name, test_name)>);\\\nvoid GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody()\n\n#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_INTERNAL_H_\n\n// Copyright 2005, Google Inc.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Author: wan@google.com (Zhanyong Wan)\n//\n// The Google C++ Testing Framework (Google Test)\n//\n// This header file defines the public API for death tests.  It is\n// #included by gtest.h so a user doesn't need to include this\n// directly.\n\n#ifndef GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_\n#define GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_\n\n// Copyright 2005, Google Inc.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Authors: wan@google.com (Zhanyong Wan), eefacm@gmail.com (Sean Mcafee)\n//\n// The Google C++ Testing Framework (Google Test)\n//\n// This header file defines internal utilities needed for implementing\n// death tests.  They are subject to change without notice.\n\n#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_\n#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_\n\n\n#include <stdio.h>\n\nnamespace testing\n{\n    namespace internal\n    {\n\n        GTEST_DECLARE_string_(internal_run_death_test);\n\n// Names of the flags (needed for parsing Google Test flags).\n        const char kDeathTestStyleFlag[] = \"death_test_style\";\n        const char kDeathTestUseFork[] = \"death_test_use_fork\";\n        const char kInternalRunDeathTestFlag[] = \"internal_run_death_test\";\n\n#if GTEST_HAS_DEATH_TEST\n\n// DeathTest is a class that hides much of the complexity of the\n// GTEST_DEATH_TEST_ macro.  It is abstract; its static Create method\n// returns a concrete class that depends on the prevailing death test\n// style, as defined by the --gtest_death_test_style and/or\n// --gtest_internal_run_death_test flags.\n\n// In describing the results of death tests, these terms are used with\n// the corresponding definitions:\n//\n// exit status:  The integer exit information in the format specified\n//               by wait(2)\n// exit code:    The integer code passed to exit(3), _exit(2), or\n//               returned from main()\n        class GTEST_API_ DeathTest\n        {\n        public:\n            // Create returns false if there was an error determining the\n            // appropriate action to take for the current death test; for example,\n            // if the gtest_death_test_style flag is set to an invalid value.\n            // The LastMessage method will return a more detailed message in that\n            // case.  Otherwise, the DeathTest pointer pointed to by the \"test\"\n            // argument is set.  If the death test should be skipped, the pointer\n            // is set to NULL; otherwise, it is set to the address of a new concrete\n            // DeathTest object that controls the execution of the current test.\n            static bool Create(const char *statement, const RE *regex,\n                               const char *file, int line, DeathTest **test);\n            DeathTest();\n            virtual ~DeathTest() { }\n\n            // A helper class that aborts a death test when it's deleted.\n            class ReturnSentinel\n            {\n            public:\n                explicit ReturnSentinel(DeathTest *test) : test_(test) { }\n                ~ReturnSentinel()\n                {\n                    test_->Abort(TEST_ENCOUNTERED_RETURN_STATEMENT);\n                }\n            private:\n                DeathTest *const test_;\n                GTEST_DISALLOW_COPY_AND_ASSIGN_(ReturnSentinel);\n            } GTEST_ATTRIBUTE_UNUSED_;\n\n            // An enumeration of possible roles that may be taken when a death\n            // test is encountered.  EXECUTE means that the death test logic should\n            // be executed immediately.  OVERSEE means that the program should prepare\n            // the appropriate environment for a child process to execute the death\n            // test, then wait for it to complete.\n            enum TestRole { OVERSEE_TEST, EXECUTE_TEST };\n\n            // An enumeration of the three reasons that a test might be aborted.\n            enum AbortReason {\n                TEST_ENCOUNTERED_RETURN_STATEMENT,\n                TEST_THREW_EXCEPTION,\n                TEST_DID_NOT_DIE\n            };\n\n            // Assumes one of the above roles.\n            virtual TestRole AssumeRole() = 0;\n\n            // Waits for the death test to finish and returns its status.\n            virtual int Wait() = 0;\n\n            // Returns true if the death test passed; that is, the test process\n            // exited during the test, its exit status matches a user-supplied\n            // predicate, and its stderr output matches a user-supplied regular\n            // expression.\n            // The user-supplied predicate may be a macro expression rather\n            // than a function pointer or functor, or else Wait and Passed could\n            // be combined.\n            virtual bool Passed(bool exit_status_ok) = 0;\n\n            // Signals that the death test did not die as expected.\n            virtual void Abort(AbortReason reason) = 0;\n\n            // Returns a human-readable outcome message regarding the outcome of\n            // the last death test.\n            static const char *LastMessage();\n\n            static void set_last_death_test_message(const std::string &message);\n\n        private:\n            // A string containing a description of the outcome of the last death test.\n            static std::string last_death_test_message_;\n\n            GTEST_DISALLOW_COPY_AND_ASSIGN_(DeathTest);\n        };\n\n// Factory interface for death tests.  May be mocked out for testing.\n        class DeathTestFactory\n        {\n        public:\n            virtual ~DeathTestFactory() { }\n            virtual bool Create(const char *statement, const RE *regex,\n                                const char *file, int line, DeathTest **test) = 0;\n        };\n\n// A concrete DeathTestFactory implementation for normal use.\n        class DefaultDeathTestFactory : public DeathTestFactory\n        {\n        public:\n            virtual bool Create(const char *statement, const RE *regex,\n                                const char *file, int line, DeathTest **test);\n        };\n\n// Returns true if exit_status describes a process that was terminated\n// by a signal, or exited normally with a nonzero exit code.\n        GTEST_API_ bool ExitedUnsuccessfully(int exit_status);\n\n// Traps C++ exceptions escaping statement and reports them as test\n// failures. Note that trapping SEH exceptions is not implemented here.\n# if GTEST_HAS_EXCEPTIONS\n#  define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test) \\\n  try { \\\n    GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \\\n  } catch (const ::std::exception& gtest_exception) { \\\n    fprintf(\\\n        stderr, \\\n        \"\\n%s: Caught std::exception-derived exception escaping the \" \\\n        \"death test statement. Exception message: %s\\n\", \\\n        ::testing::internal::FormatFileLocation(__FILE__, __LINE__).c_str(), \\\n        gtest_exception.what()); \\\n    fflush(stderr); \\\n    death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \\\n  } catch (...) { \\\n    death_test->Abort(::testing::internal::DeathTest::TEST_THREW_EXCEPTION); \\\n  }\n\n# else\n#  define GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, death_test) \\\n  GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)\n\n# endif\n\n// This macro is for implementing ASSERT_DEATH*, EXPECT_DEATH*,\n// ASSERT_EXIT*, and EXPECT_EXIT*.\n# define GTEST_DEATH_TEST_(statement, predicate, regex, fail) \\\n  GTEST_AMBIGUOUS_ELSE_BLOCKER_ \\\n  if (::testing::internal::AlwaysTrue()) { \\\n    const ::testing::internal::RE& gtest_regex = (regex); \\\n    ::testing::internal::DeathTest* gtest_dt; \\\n    if (!::testing::internal::DeathTest::Create(#statement, &gtest_regex, \\\n        __FILE__, __LINE__, &gtest_dt)) { \\\n      goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \\\n    } \\\n    if (gtest_dt != NULL) { \\\n      ::testing::internal::scoped_ptr< ::testing::internal::DeathTest> \\\n          gtest_dt_ptr(gtest_dt); \\\n      switch (gtest_dt->AssumeRole()) { \\\n        case ::testing::internal::DeathTest::OVERSEE_TEST: \\\n          if (!gtest_dt->Passed(predicate(gtest_dt->Wait()))) { \\\n            goto GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__); \\\n          } \\\n          break; \\\n        case ::testing::internal::DeathTest::EXECUTE_TEST: { \\\n          ::testing::internal::DeathTest::ReturnSentinel \\\n              gtest_sentinel(gtest_dt); \\\n          GTEST_EXECUTE_DEATH_TEST_STATEMENT_(statement, gtest_dt); \\\n          gtest_dt->Abort(::testing::internal::DeathTest::TEST_DID_NOT_DIE); \\\n          break; \\\n        } \\\n        default: \\\n          break; \\\n      } \\\n    } \\\n  } else \\\n    GTEST_CONCAT_TOKEN_(gtest_label_, __LINE__): \\\n      fail(::testing::internal::DeathTest::LastMessage())\n// The symbol \"fail\" here expands to something into which a message\n// can be streamed.\n\n// This macro is for implementing ASSERT/EXPECT_DEBUG_DEATH when compiled in\n// NDEBUG mode. In this case we need the statements to be executed, the regex is\n// ignored, and the macro must accept a streamed message even though the message\n// is never printed.\n# define GTEST_EXECUTE_STATEMENT_(statement, regex) \\\n  GTEST_AMBIGUOUS_ELSE_BLOCKER_ \\\n  if (::testing::internal::AlwaysTrue()) { \\\n     GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \\\n  } else \\\n    ::testing::Message()\n\n// A class representing the parsed contents of the\n// --gtest_internal_run_death_test flag, as it existed when\n// RUN_ALL_TESTS was called.\n        class InternalRunDeathTestFlag\n        {\n        public:\n            InternalRunDeathTestFlag(const std::string &a_file,\n                                     int a_line,\n                                     int an_index,\n                                     int a_write_fd)\n                : file_(a_file), line_(a_line), index_(an_index),\n                  write_fd_(a_write_fd) {}\n\n            ~InternalRunDeathTestFlag()\n            {\n                if (write_fd_ >= 0)\n                    posix::Close(write_fd_);\n            }\n\n            const std::string &file() const\n            {\n                return file_;\n            }\n            int line() const\n            {\n                return line_;\n            }\n            int index() const\n            {\n                return index_;\n            }\n            int write_fd() const\n            {\n                return write_fd_;\n            }\n\n        private:\n            std::string file_;\n            int line_;\n            int index_;\n            int write_fd_;\n\n            GTEST_DISALLOW_COPY_AND_ASSIGN_(InternalRunDeathTestFlag);\n        };\n\n// Returns a newly created InternalRunDeathTestFlag object with fields\n// initialized from the GTEST_FLAG(internal_run_death_test) flag if\n// the flag is specified; otherwise returns NULL.\n        InternalRunDeathTestFlag *ParseInternalRunDeathTestFlag();\n\n#else  // GTEST_HAS_DEATH_TEST\n\n// This macro is used for implementing macros such as\n// EXPECT_DEATH_IF_SUPPORTED and ASSERT_DEATH_IF_SUPPORTED on systems where\n// death tests are not supported. Those macros must compile on such systems\n// iff EXPECT_DEATH and ASSERT_DEATH compile with the same parameters on\n// systems that support death tests. This allows one to write such a macro\n// on a system that does not support death tests and be sure that it will\n// compile on a death-test supporting system.\n//\n// Parameters:\n//   statement -  A statement that a macro such as EXPECT_DEATH would test\n//                for program termination. This macro has to make sure this\n//                statement is compiled but not executed, to ensure that\n//                EXPECT_DEATH_IF_SUPPORTED compiles with a certain\n//                parameter iff EXPECT_DEATH compiles with it.\n//   regex     -  A regex that a macro such as EXPECT_DEATH would use to test\n//                the output of statement.  This parameter has to be\n//                compiled but not evaluated by this macro, to ensure that\n//                this macro only accepts expressions that a macro such as\n//                EXPECT_DEATH would accept.\n//   terminator - Must be an empty statement for EXPECT_DEATH_IF_SUPPORTED\n//                and a return statement for ASSERT_DEATH_IF_SUPPORTED.\n//                This ensures that ASSERT_DEATH_IF_SUPPORTED will not\n//                compile inside functions where ASSERT_DEATH doesn't\n//                compile.\n//\n//  The branch that has an always false condition is used to ensure that\n//  statement and regex are compiled (and thus syntactically correct) but\n//  never executed. The unreachable code macro protects the terminator\n//  statement from generating an 'unreachable code' warning in case\n//  statement unconditionally returns or throws. The Message constructor at\n//  the end allows the syntax of streaming additional messages into the\n//  macro, for compilational compatibility with EXPECT_DEATH/ASSERT_DEATH.\n# define GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, terminator) \\\n    GTEST_AMBIGUOUS_ELSE_BLOCKER_ \\\n    if (::testing::internal::AlwaysTrue()) { \\\n      GTEST_LOG_(WARNING) \\\n          << \"Death tests are not supported on this platform.\\n\" \\\n          << \"Statement '\" #statement \"' cannot be verified.\"; \\\n    } else if (::testing::internal::AlwaysFalse()) { \\\n      ::testing::internal::RE::PartialMatch(\".*\", (regex)); \\\n      GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement); \\\n      terminator; \\\n    } else \\\n      ::testing::Message()\n\n#endif  // GTEST_HAS_DEATH_TEST\n\n    }  // namespace internal\n}  // namespace testing\n\n#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_DEATH_TEST_INTERNAL_H_\n\nnamespace testing\n{\n\n// This flag controls the style of death tests.  Valid values are \"threadsafe\",\n// meaning that the death test child process will re-execute the test binary\n// from the start, running only a single death test, or \"fast\",\n// meaning that the child process will execute the test logic immediately\n// after forking.\n    GTEST_DECLARE_string_(death_test_style);\n\n#if GTEST_HAS_DEATH_TEST\n\n    namespace internal\n    {\n\n// Returns a Boolean value indicating whether the caller is currently\n// executing in the context of the death test child process.  Tools such as\n// Valgrind heap checkers may need this to modify their behavior in death\n// tests.  IMPORTANT: This is an internal utility.  Using it may break the\n// implementation of death tests.  User code MUST NOT use it.\n        GTEST_API_ bool InDeathTestChild();\n\n    }  // namespace internal\n\n// The following macros are useful for writing death tests.\n\n// Here's what happens when an ASSERT_DEATH* or EXPECT_DEATH* is\n// executed:\n//\n//   1. It generates a warning if there is more than one active\n//   thread.  This is because it's safe to fork() or clone() only\n//   when there is a single thread.\n//\n//   2. The parent process clone()s a sub-process and runs the death\n//   test in it; the sub-process exits with code 0 at the end of the\n//   death test, if it hasn't exited already.\n//\n//   3. The parent process waits for the sub-process to terminate.\n//\n//   4. The parent process checks the exit code and error message of\n//   the sub-process.\n//\n// Examples:\n//\n//   ASSERT_DEATH(server.SendMessage(56, \"Hello\"), \"Invalid port number\");\n//   for (int i = 0; i < 5; i++) {\n//     EXPECT_DEATH(server.ProcessRequest(i),\n//                  \"Invalid request .* in ProcessRequest()\")\n//                  << \"Failed to die on request \" << i;\n//   }\n//\n//   ASSERT_EXIT(server.ExitNow(), ::testing::ExitedWithCode(0), \"Exiting\");\n//\n//   bool KilledBySIGHUP(int exit_code) {\n//     return WIFSIGNALED(exit_code) && WTERMSIG(exit_code) == SIGHUP;\n//   }\n//\n//   ASSERT_EXIT(client.HangUpServer(), KilledBySIGHUP, \"Hanging up!\");\n//\n// On the regular expressions used in death tests:\n//\n//   On POSIX-compliant systems (*nix), we use the <regex.h> library,\n//   which uses the POSIX extended regex syntax.\n//\n//   On other platforms (e.g. Windows), we only support a simple regex\n//   syntax implemented as part of Google Test.  This limited\n//   implementation should be enough most of the time when writing\n//   death tests; though it lacks many features you can find in PCRE\n//   or POSIX extended regex syntax.  For example, we don't support\n//   union (\"x|y\"), grouping (\"(xy)\"), brackets (\"[xy]\"), and\n//   repetition count (\"x{5,7}\"), among others.\n//\n//   Below is the syntax that we do support.  We chose it to be a\n//   subset of both PCRE and POSIX extended regex, so it's easy to\n//   learn wherever you come from.  In the following: 'A' denotes a\n//   literal character, period (.), or a single \\\\ escape sequence;\n//   'x' and 'y' denote regular expressions; 'm' and 'n' are for\n//   natural numbers.\n//\n//     c     matches any literal character c\n//     \\\\d   matches any decimal digit\n//     \\\\D   matches any character that's not a decimal digit\n//     \\\\f   matches \\f\n//     \\\\n   matches \\n\n//     \\\\r   matches \\r\n//     \\\\s   matches any ASCII whitespace, including \\n\n//     \\\\S   matches any character that's not a whitespace\n//     \\\\t   matches \\t\n//     \\\\v   matches \\v\n//     \\\\w   matches any letter, _, or decimal digit\n//     \\\\W   matches any character that \\\\w doesn't match\n//     \\\\c   matches any literal character c, which must be a punctuation\n//     .     matches any single character except \\n\n//     A?    matches 0 or 1 occurrences of A\n//     A*    matches 0 or many occurrences of A\n//     A+    matches 1 or many occurrences of A\n//     ^     matches the beginning of a string (not that of each line)\n//     $     matches the end of a string (not that of each line)\n//     xy    matches x followed by y\n//\n//   If you accidentally use PCRE or POSIX extended regex features\n//   not implemented by us, you will get a run-time failure.  In that\n//   case, please try to rewrite your regular expression within the\n//   above syntax.\n//\n//   This implementation is *not* meant to be as highly tuned or robust\n//   as a compiled regex library, but should perform well enough for a\n//   death test, which already incurs significant overhead by launching\n//   a child process.\n//\n// Known caveats:\n//\n//   A \"threadsafe\" style death test obtains the path to the test\n//   program from argv[0] and re-executes it in the sub-process.  For\n//   simplicity, the current implementation doesn't search the PATH\n//   when launching the sub-process.  This means that the user must\n//   invoke the test program via a path that contains at least one\n//   path separator (e.g. path/to/foo_test and\n//   /absolute/path/to/bar_test are fine, but foo_test is not).  This\n//   is rarely a problem as people usually don't put the test binary\n//   directory in PATH.\n//\n// TODO(wan@google.com): make thread-safe death tests search the PATH.\n\n// Asserts that a given statement causes the program to exit, with an\n// integer exit status that satisfies predicate, and emitting error output\n// that matches regex.\n# define ASSERT_EXIT(statement, predicate, regex) \\\n    GTEST_DEATH_TEST_(statement, predicate, regex, GTEST_FATAL_FAILURE_)\n\n// Like ASSERT_EXIT, but continues on to successive tests in the\n// test case, if any:\n# define EXPECT_EXIT(statement, predicate, regex) \\\n    GTEST_DEATH_TEST_(statement, predicate, regex, GTEST_NONFATAL_FAILURE_)\n\n// Asserts that a given statement causes the program to exit, either by\n// explicitly exiting with a nonzero exit code or being killed by a\n// signal, and emitting error output that matches regex.\n# define ASSERT_DEATH(statement, regex) \\\n    ASSERT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, regex)\n\n// Like ASSERT_DEATH, but continues on to successive tests in the\n// test case, if any:\n# define EXPECT_DEATH(statement, regex) \\\n    EXPECT_EXIT(statement, ::testing::internal::ExitedUnsuccessfully, regex)\n\n// Two predicate classes that can be used in {ASSERT,EXPECT}_EXIT*:\n\n// Tests that an exit code describes a normal exit with a given exit code.\n    class GTEST_API_ ExitedWithCode\n    {\n    public:\n        explicit ExitedWithCode(int exit_code);\n        bool operator()(int exit_status) const;\n    private:\n        // No implementation - assignment is unsupported.\n        void operator=(const ExitedWithCode &other);\n\n        const int exit_code_;\n    };\n\n# if !GTEST_OS_WINDOWS\n// Tests that an exit code describes an exit due to termination by a\n// given signal.\n    class GTEST_API_ KilledBySignal\n    {\n    public:\n        explicit KilledBySignal(int signum);\n        bool operator()(int exit_status) const;\n    private:\n        const int signum_;\n    };\n# endif  // !GTEST_OS_WINDOWS\n\n// EXPECT_DEBUG_DEATH asserts that the given statements die in debug mode.\n// The death testing framework causes this to have interesting semantics,\n// since the sideeffects of the call are only visible in opt mode, and not\n// in debug mode.\n//\n// In practice, this can be used to test functions that utilize the\n// LOG(DFATAL) macro using the following style:\n//\n// int DieInDebugOr12(int* sideeffect) {\n//   if (sideeffect) {\n//     *sideeffect = 12;\n//   }\n//   LOG(DFATAL) << \"death\";\n//   return 12;\n// }\n//\n// TEST(TestCase, TestDieOr12WorksInDgbAndOpt) {\n//   int sideeffect = 0;\n//   // Only asserts in dbg.\n//   EXPECT_DEBUG_DEATH(DieInDebugOr12(&sideeffect), \"death\");\n//\n// #ifdef NDEBUG\n//   // opt-mode has sideeffect visible.\n//   EXPECT_EQ(12, sideeffect);\n// #else\n//   // dbg-mode no visible sideeffect.\n//   EXPECT_EQ(0, sideeffect);\n// #endif\n// }\n//\n// This will assert that DieInDebugReturn12InOpt() crashes in debug\n// mode, usually due to a DCHECK or LOG(DFATAL), but returns the\n// appropriate fallback value (12 in this case) in opt mode. If you\n// need to test that a function has appropriate side-effects in opt\n// mode, include assertions against the side-effects.  A general\n// pattern for this is:\n//\n// EXPECT_DEBUG_DEATH({\n//   // Side-effects here will have an effect after this statement in\n//   // opt mode, but none in debug mode.\n//   EXPECT_EQ(12, DieInDebugOr12(&sideeffect));\n// }, \"death\");\n//\n# ifdef NDEBUG\n\n#  define EXPECT_DEBUG_DEATH(statement, regex) \\\n  GTEST_EXECUTE_STATEMENT_(statement, regex)\n\n#  define ASSERT_DEBUG_DEATH(statement, regex) \\\n  GTEST_EXECUTE_STATEMENT_(statement, regex)\n\n# else\n\n#  define EXPECT_DEBUG_DEATH(statement, regex) \\\n  EXPECT_DEATH(statement, regex)\n\n#  define ASSERT_DEBUG_DEATH(statement, regex) \\\n  ASSERT_DEATH(statement, regex)\n\n# endif  // NDEBUG for EXPECT_DEBUG_DEATH\n#endif  // GTEST_HAS_DEATH_TEST\n\n// EXPECT_DEATH_IF_SUPPORTED(statement, regex) and\n// ASSERT_DEATH_IF_SUPPORTED(statement, regex) expand to real death tests if\n// death tests are supported; otherwise they just issue a warning.  This is\n// useful when you are combining death test assertions with normal test\n// assertions in one test.\n#if GTEST_HAS_DEATH_TEST\n# define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \\\n    EXPECT_DEATH(statement, regex)\n# define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \\\n    ASSERT_DEATH(statement, regex)\n#else\n# define EXPECT_DEATH_IF_SUPPORTED(statement, regex) \\\n    GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, )\n# define ASSERT_DEATH_IF_SUPPORTED(statement, regex) \\\n    GTEST_UNSUPPORTED_DEATH_TEST_(statement, regex, return)\n#endif\n\n}  // namespace testing\n\n#endif  // GTEST_INCLUDE_GTEST_GTEST_DEATH_TEST_H_\n// This file was GENERATED by command:\n//     pump.py gtest-param-test.h.pump\n// DO NOT EDIT BY HAND!!!\n\n// Copyright 2008, Google Inc.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Authors: vladl@google.com (Vlad Losev)\n//\n// Macros and functions for implementing parameterized tests\n// in Google C++ Testing Framework (Google Test)\n//\n// This file is generated by a SCRIPT.  DO NOT EDIT BY HAND!\n//\n#ifndef GTEST_INCLUDE_GTEST_GTEST_PARAM_TEST_H_\n#define GTEST_INCLUDE_GTEST_GTEST_PARAM_TEST_H_\n\n\n// Value-parameterized tests allow you to test your code with different\n// parameters without writing multiple copies of the same test.\n//\n// Here is how you use value-parameterized tests:\n\n#if 0\n\n// To write value-parameterized tests, first you should define a fixture\n// class. It is usually derived from testing::TestWithParam<T> (see below for\n// another inheritance scheme that's sometimes useful in more complicated\n// class hierarchies), where the type of your parameter values.\n// TestWithParam<T> is itself derived from testing::Test. T can be any\n// copyable type. If it's a raw pointer, you are responsible for managing the\n// lifespan of the pointed values.\n\nclass FooTest : public ::testing::TestWithParam<const char *>\n{\n    // You can implement all the usual class fixture members here.\n};\n\n// Then, use the TEST_P macro to define as many parameterized tests\n// for this fixture as you want. The _P suffix is for \"parameterized\"\n// or \"pattern\", whichever you prefer to think.\n\nTEST_P(FooTest, DoesBlah)\n{\n    // Inside a test, access the test parameter with the GetParam() method\n    // of the TestWithParam<T> class:\n    EXPECT_TRUE(foo.Blah(GetParam()));\n    ...\n}\n\nTEST_P(FooTest, HasBlahBlah)\n{\n    ...\n}\n\n// Finally, you can use INSTANTIATE_TEST_CASE_P to instantiate the test\n// case with any set of parameters you want. Google Test defines a number\n// of functions for generating test parameters. They return what we call\n// (surprise!) parameter generators. Here is a  summary of them, which\n// are all in the testing namespace:\n//\n//\n//  Range(begin, end [, step]) - Yields values {begin, begin+step,\n//                               begin+step+step, ...}. The values do not\n//                               include end. step defaults to 1.\n//  Values(v1, v2, ..., vN)    - Yields values {v1, v2, ..., vN}.\n//  ValuesIn(container)        - Yields values from a C-style array, an STL\n//  ValuesIn(begin,end)          container, or an iterator range [begin, end).\n//  Bool()                     - Yields sequence {false, true}.\n//  Combine(g1, g2, ..., gN)   - Yields all combinations (the Cartesian product\n//                               for the math savvy) of the values generated\n//                               by the N generators.\n//\n// For more details, see comments at the definitions of these functions below\n// in this file.\n//\n// The following statement will instantiate tests from the FooTest test case\n// each with parameter values \"meeny\", \"miny\", and \"moe\".\n\nINSTANTIATE_TEST_CASE_P(InstantiationName,\n                        FooTest,\n                        Values(\"meeny\", \"miny\", \"moe\"));\n\n// To distinguish different instances of the pattern, (yes, you\n// can instantiate it more then once) the first argument to the\n// INSTANTIATE_TEST_CASE_P macro is a prefix that will be added to the\n// actual test case name. Remember to pick unique prefixes for different\n// instantiations. The tests from the instantiation above will have\n// these names:\n//\n//    * InstantiationName/FooTest.DoesBlah/0 for \"meeny\"\n//    * InstantiationName/FooTest.DoesBlah/1 for \"miny\"\n//    * InstantiationName/FooTest.DoesBlah/2 for \"moe\"\n//    * InstantiationName/FooTest.HasBlahBlah/0 for \"meeny\"\n//    * InstantiationName/FooTest.HasBlahBlah/1 for \"miny\"\n//    * InstantiationName/FooTest.HasBlahBlah/2 for \"moe\"\n//\n// You can use these names in --gtest_filter.\n//\n// This statement will instantiate all tests from FooTest again, each\n// with parameter values \"cat\" and \"dog\":\n\nconst char *pets[] = {\"cat\", \"dog\"};\nINSTANTIATE_TEST_CASE_P(AnotherInstantiationName, FooTest, ValuesIn(pets));\n\n// The tests from the instantiation above will have these names:\n//\n//    * AnotherInstantiationName/FooTest.DoesBlah/0 for \"cat\"\n//    * AnotherInstantiationName/FooTest.DoesBlah/1 for \"dog\"\n//    * AnotherInstantiationName/FooTest.HasBlahBlah/0 for \"cat\"\n//    * AnotherInstantiationName/FooTest.HasBlahBlah/1 for \"dog\"\n//\n// Please note that INSTANTIATE_TEST_CASE_P will instantiate all tests\n// in the given test case, whether their definitions come before or\n// AFTER the INSTANTIATE_TEST_CASE_P statement.\n//\n// Please also note that generator expressions (including parameters to the\n// generators) are evaluated in InitGoogleTest(), after main() has started.\n// This allows the user on one hand, to adjust generator parameters in order\n// to dynamically determine a set of tests to run and on the other hand,\n// give the user a chance to inspect the generated tests with Google Test\n// reflection API before RUN_ALL_TESTS() is executed.\n//\n// You can see samples/sample7_unittest.cc and samples/sample8_unittest.cc\n// for more examples.\n//\n// In the future, we plan to publish the API for defining new parameter\n// generators. But for now this interface remains part of the internal\n// implementation and is subject to change.\n//\n//\n// A parameterized test fixture must be derived from testing::Test and from\n// testing::WithParamInterface<T>, where T is the type of the parameter\n// values. Inheriting from TestWithParam<T> satisfies that requirement because\n// TestWithParam<T> inherits from both Test and WithParamInterface. In more\n// complicated hierarchies, however, it is occasionally useful to inherit\n// separately from Test and WithParamInterface. For example:\n\nclass BaseTest : public ::testing::Test\n{\n    // You can inherit all the usual members for a non-parameterized test\n    // fixture here.\n};\n\nclass DerivedTest : public BaseTest, public ::testing::WithParamInterface<int>\n{\n    // The usual test fixture members go here too.\n};\n\nTEST_F(BaseTest, HasFoo)\n{\n    // This is an ordinary non-parameterized test.\n}\n\nTEST_P(DerivedTest, DoesBlah)\n{\n    // GetParam works just the same here as if you inherit from TestWithParam.\n    EXPECT_TRUE(foo.Blah(GetParam()));\n}\n\n#endif  // 0\n\n\n#if !GTEST_OS_SYMBIAN\n# include <utility>\n#endif\n\n// scripts/fuse_gtest.py depends on gtest's own header being #included\n// *unconditionally*.  Therefore these #includes cannot be moved\n// inside #if GTEST_HAS_PARAM_TEST.\n// Copyright 2008 Google Inc.\n// All Rights Reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Author: vladl@google.com (Vlad Losev)\n\n// Type and function utilities for implementing parameterized tests.\n\n#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_\n#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_\n\n#include <ctype.h>\n\n#include <iterator>\n#include <set>\n#include <utility>\n#include <vector>\n\n// scripts/fuse_gtest.py depends on gtest's own header being #included\n// *unconditionally*.  Therefore these #includes cannot be moved\n// inside #if GTEST_HAS_PARAM_TEST.\n// Copyright 2003 Google Inc.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Authors: Dan Egnor (egnor@google.com)\n//\n// A \"smart\" pointer type with reference tracking.  Every pointer to a\n// particular object is kept on a circular linked list.  When the last pointer\n// to an object is destroyed or reassigned, the object is deleted.\n//\n// Used properly, this deletes the object when the last reference goes away.\n// There are several caveats:\n// - Like all reference counting schemes, cycles lead to leaks.\n// - Each smart pointer is actually two pointers (8 bytes instead of 4).\n// - Every time a pointer is assigned, the entire list of pointers to that\n//   object is traversed.  This class is therefore NOT SUITABLE when there\n//   will often be more than two or three pointers to a particular object.\n// - References are only tracked as long as linked_ptr<> objects are copied.\n//   If a linked_ptr<> is converted to a raw pointer and back, BAD THINGS\n//   will happen (double deletion).\n//\n// A good use of this class is storing object references in STL containers.\n// You can safely put linked_ptr<> in a vector<>.\n// Other uses may not be as good.\n//\n// Note: If you use an incomplete type with linked_ptr<>, the class\n// *containing* linked_ptr<> must have a constructor and destructor (even\n// if they do nothing!).\n//\n// Bill Gibbons suggested we use something like this.\n//\n// Thread Safety:\n//   Unlike other linked_ptr implementations, in this implementation\n//   a linked_ptr object is thread-safe in the sense that:\n//     - it's safe to copy linked_ptr objects concurrently,\n//     - it's safe to copy *from* a linked_ptr and read its underlying\n//       raw pointer (e.g. via get()) concurrently, and\n//     - it's safe to write to two linked_ptrs that point to the same\n//       shared object concurrently.\n// TODO(wan@google.com): rename this to safe_linked_ptr to avoid\n// confusion with normal linked_ptr.\n\n#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_\n#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_\n\n#include <stdlib.h>\n#include <assert.h>\n\n\nnamespace testing\n{\n    namespace internal\n    {\n\n// Protects copying of all linked_ptr objects.\n        GTEST_API_ GTEST_DECLARE_STATIC_MUTEX_(g_linked_ptr_mutex);\n\n// This is used internally by all instances of linked_ptr<>.  It needs to be\n// a non-template class because different types of linked_ptr<> can refer to\n// the same object (linked_ptr<Superclass>(obj) vs linked_ptr<Subclass>(obj)).\n// So, it needs to be possible for different types of linked_ptr to participate\n// in the same circular linked list, so we need a single class type here.\n//\n// DO NOT USE THIS CLASS DIRECTLY YOURSELF.  Use linked_ptr<T>.\n        class linked_ptr_internal\n        {\n        public:\n            // Create a new circle that includes only this instance.\n            void join_new()\n            {\n                next_ = this;\n            }\n\n            // Many linked_ptr operations may change p.link_ for some linked_ptr\n            // variable p in the same circle as this object.  Therefore we need\n            // to prevent two such operations from occurring concurrently.\n            //\n            // Note that different types of linked_ptr objects can coexist in a\n            // circle (e.g. linked_ptr<Base>, linked_ptr<Derived1>, and\n            // linked_ptr<Derived2>).  Therefore we must use a single mutex to\n            // protect all linked_ptr objects.  This can create serious\n            // contention in production code, but is acceptable in a testing\n            // framework.\n\n            // Join an existing circle.\n            void join(linked_ptr_internal const *ptr)\n            GTEST_LOCK_EXCLUDED_(g_linked_ptr_mutex)\n            {\n                MutexLock lock(&g_linked_ptr_mutex);\n\n                linked_ptr_internal const *p = ptr;\n                while (p->next_ != ptr) {\n                    assert(p->next_ != this &&\n                           \"Trying to join() a linked ring we are already in. \"\n                           \"Is GMock thread safety enabled?\");\n                    p = p->next_;\n                }\n                p->next_ = this;\n                next_ = ptr;\n            }\n\n            // Leave whatever circle we're part of.  Returns true if we were the\n            // last member of the circle.  Once this is done, you can join() another.\n            bool depart()\n            GTEST_LOCK_EXCLUDED_(g_linked_ptr_mutex)\n            {\n                MutexLock lock(&g_linked_ptr_mutex);\n\n                if (next_ == this) return true;\n                linked_ptr_internal const *p = next_;\n                while (p->next_ != this) {\n                    assert(p->next_ != next_ &&\n                           \"Trying to depart() a linked ring we are not in. \"\n                           \"Is GMock thread safety enabled?\");\n                    p = p->next_;\n                }\n                p->next_ = next_;\n                return false;\n            }\n\n        private:\n            mutable linked_ptr_internal const *next_;\n        };\n\n        template <typename T>\n        class linked_ptr\n        {\n        public:\n            typedef T element_type;\n\n            // Take over ownership of a raw pointer.  This should happen as soon as\n            // possible after the object is created.\n            explicit linked_ptr(T *ptr = NULL)\n            {\n                capture(ptr);\n            }\n            ~linked_ptr()\n            {\n                depart();\n            }\n\n            // Copy an existing linked_ptr<>, adding ourselves to the list of references.\n            template <typename U> linked_ptr(linked_ptr<U> const &ptr)\n            {\n                copy(&ptr);\n            }\n            linked_ptr(linked_ptr const &ptr)    // NOLINT\n            {\n                assert(&ptr != this);\n                copy(&ptr);\n            }\n\n            // Assignment releases the old value and acquires the new.\n            template <typename U> linked_ptr &operator=(linked_ptr<U> const &ptr)\n            {\n                depart();\n                copy(&ptr);\n                return *this;\n            }\n\n            linked_ptr &operator=(linked_ptr const &ptr)\n            {\n                if (&ptr != this) {\n                    depart();\n                    copy(&ptr);\n                }\n                return *this;\n            }\n\n            // Smart pointer members.\n            void reset(T *ptr = NULL)\n            {\n                depart();\n                capture(ptr);\n            }\n            T *get() const\n            {\n                return value_;\n            }\n            T *operator->() const\n            {\n                return value_;\n            }\n            T &operator*() const\n            {\n                return *value_;\n            }\n\n            bool operator==(T *p) const\n            {\n                return value_ == p;\n            }\n            bool operator!=(T *p) const\n            {\n                return value_ != p;\n            }\n            template <typename U>\n            bool operator==(linked_ptr<U> const &ptr) const\n            {\n                return value_ == ptr.get();\n            }\n            template <typename U>\n            bool operator!=(linked_ptr<U> const &ptr) const\n            {\n                return value_ != ptr.get();\n            }\n\n        private:\n            template <typename U>\n            friend class linked_ptr;\n\n            T *value_;\n            linked_ptr_internal link_;\n\n            void depart()\n            {\n                if (link_.depart()) delete value_;\n            }\n\n            void capture(T *ptr)\n            {\n                value_ = ptr;\n                link_.join_new();\n            }\n\n            template <typename U> void copy(linked_ptr<U> const *ptr)\n            {\n                value_ = ptr->get();\n                if (value_)\n                    link_.join(&ptr->link_);\n                else\n                    link_.join_new();\n            }\n        };\n\n        template<typename T> inline\n        bool operator==(T *ptr, const linked_ptr<T> &x)\n        {\n            return ptr == x.get();\n        }\n\n        template<typename T> inline\n        bool operator!=(T *ptr, const linked_ptr<T> &x)\n        {\n            return ptr != x.get();\n        }\n\n// A function to convert T* into linked_ptr<T>\n// Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation\n// for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))\n        template <typename T>\n        linked_ptr<T> make_linked_ptr(T *ptr)\n        {\n            return linked_ptr<T>(ptr);\n        }\n\n    }  // namespace internal\n}  // namespace testing\n\n#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_LINKED_PTR_H_\n// Copyright 2007, Google Inc.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Author: wan@google.com (Zhanyong Wan)\n\n// Google Test - The Google C++ Testing Framework\n//\n// This file implements a universal value printer that can print a\n// value of any type T:\n//\n//   void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr);\n//\n// A user can teach this function how to print a class type T by\n// defining either operator<<() or PrintTo() in the namespace that\n// defines T.  More specifically, the FIRST defined function in the\n// following list will be used (assuming T is defined in namespace\n// foo):\n//\n//   1. foo::PrintTo(const T&, ostream*)\n//   2. operator<<(ostream&, const T&) defined in either foo or the\n//      global namespace.\n//\n// If none of the above is defined, it will print the debug string of\n// the value if it is a protocol buffer, or print the raw bytes in the\n// value otherwise.\n//\n// To aid debugging: when T is a reference type, the address of the\n// value is also printed; when T is a (const) char pointer, both the\n// pointer value and the NUL-terminated string it points to are\n// printed.\n//\n// We also provide some convenient wrappers:\n//\n//   // Prints a value to a string.  For a (const or not) char\n//   // pointer, the NUL-terminated string (but not the pointer) is\n//   // printed.\n//   std::string ::testing::PrintToString(const T& value);\n//\n//   // Prints a value tersely: for a reference type, the referenced\n//   // value (but not the address) is printed; for a (const or not) char\n//   // pointer, the NUL-terminated string (but not the pointer) is\n//   // printed.\n//   void ::testing::internal::UniversalTersePrint(const T& value, ostream*);\n//\n//   // Prints value using the type inferred by the compiler.  The difference\n//   // from UniversalTersePrint() is that this function prints both the\n//   // pointer and the NUL-terminated string for a (const or not) char pointer.\n//   void ::testing::internal::UniversalPrint(const T& value, ostream*);\n//\n//   // Prints the fields of a tuple tersely to a string vector, one\n//   // element for each field. Tuple support must be enabled in\n//   // gtest-port.h.\n//   std::vector<string> UniversalTersePrintTupleFieldsToStrings(\n//       const Tuple& value);\n//\n// Known limitation:\n//\n// The print primitives print the elements of an STL-style container\n// using the compiler-inferred type of *iter where iter is a\n// const_iterator of the container.  When const_iterator is an input\n// iterator but not a forward iterator, this inferred type may not\n// match value_type, and the print output may be incorrect.  In\n// practice, this is rarely a problem as for most containers\n// const_iterator is a forward iterator.  We'll fix this if there's an\n// actual need for it.  Note that this fix cannot rely on value_type\n// being defined as many user-defined container types don't have\n// value_type.\n\n#ifndef GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_\n#define GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_\n\n#include <ostream>  // NOLINT\n#include <sstream>\n#include <string>\n#include <utility>\n#include <vector>\n\n#if GTEST_HAS_STD_TUPLE_\n# include <tuple>\n#endif\n\nnamespace testing\n{\n\n// Definitions in the 'internal' and 'internal2' name spaces are\n// subject to change without notice.  DO NOT USE THEM IN USER CODE!\n    namespace internal2\n    {\n\n// Prints the given number of bytes in the given object to the given\n// ostream.\n        GTEST_API_ void PrintBytesInObjectTo(const unsigned char *obj_bytes,\n                                             size_t count,\n                                             ::std::ostream *os);\n\n// For selecting which printer to use when a given type has neither <<\n// nor PrintTo().\n        enum TypeKind {\n            kProtobuf,              // a protobuf type\n            kConvertibleToInteger,  // a type implicitly convertible to BiggestInt\n            // (e.g. a named or unnamed enum type)\n            kOtherType              // anything else\n        };\n\n// TypeWithoutFormatter<T, kTypeKind>::PrintValue(value, os) is called\n// by the universal printer to print a value of type T when neither\n// operator<< nor PrintTo() is defined for T, where kTypeKind is the\n// \"kind\" of T as defined by enum TypeKind.\n        template <typename T, TypeKind kTypeKind>\n        class TypeWithoutFormatter\n        {\n        public:\n            // This default version is called when kTypeKind is kOtherType.\n            static void PrintValue(const T &value, ::std::ostream *os)\n            {\n                PrintBytesInObjectTo(reinterpret_cast<const unsigned char *>(&value),\n                                     sizeof(value), os);\n            }\n        };\n\n// We print a protobuf using its ShortDebugString() when the string\n// doesn't exceed this many characters; otherwise we print it using\n// DebugString() for better readability.\n        const size_t kProtobufOneLinerMaxLength = 50;\n\n        template <typename T>\n        class TypeWithoutFormatter<T, kProtobuf>\n        {\n        public:\n            static void PrintValue(const T &value, ::std::ostream *os)\n            {\n                std::string pretty_str = value.ShortDebugString();\n                if (pretty_str.length() > kProtobufOneLinerMaxLength) {\n                    pretty_str = \"\\n\" + value.DebugString();\n                }\n                *os << (\"<\" + pretty_str + \">\");\n            }\n        };\n\n        template <typename T>\n        class TypeWithoutFormatter<T, kConvertibleToInteger>\n        {\n        public:\n            // Since T has no << operator or PrintTo() but can be implicitly\n            // converted to BiggestInt, we print it as a BiggestInt.\n            //\n            // Most likely T is an enum type (either named or unnamed), in which\n            // case printing it as an integer is the desired behavior.  In case\n            // T is not an enum, printing it as an integer is the best we can do\n            // given that it has no user-defined printer.\n            static void PrintValue(const T &value, ::std::ostream *os)\n            {\n                const internal::BiggestInt kBigInt = value;\n                *os << kBigInt;\n            }\n        };\n\n// Prints the given value to the given ostream.  If the value is a\n// protocol message, its debug string is printed; if it's an enum or\n// of a type implicitly convertible to BiggestInt, it's printed as an\n// integer; otherwise the bytes in the value are printed.  This is\n// what UniversalPrinter<T>::Print() does when it knows nothing about\n// type T and T has neither << operator nor PrintTo().\n//\n// A user can override this behavior for a class type Foo by defining\n// a << operator in the namespace where Foo is defined.\n//\n// We put this operator in namespace 'internal2' instead of 'internal'\n// to simplify the implementation, as much code in 'internal' needs to\n// use << in STL, which would conflict with our own << were it defined\n// in 'internal'.\n//\n// Note that this operator<< takes a generic std::basic_ostream<Char,\n// CharTraits> type instead of the more restricted std::ostream.  If\n// we define it to take an std::ostream instead, we'll get an\n// \"ambiguous overloads\" compiler error when trying to print a type\n// Foo that supports streaming to std::basic_ostream<Char,\n// CharTraits>, as the compiler cannot tell whether\n// operator<<(std::ostream&, const T&) or\n// operator<<(std::basic_stream<Char, CharTraits>, const Foo&) is more\n// specific.\n        template <typename Char, typename CharTraits, typename T>\n        ::std::basic_ostream<Char, CharTraits> &operator<<(\n            ::std::basic_ostream<Char, CharTraits> &os, const T &x)\n        {\n            TypeWithoutFormatter<T,\n                                 (internal::IsAProtocolMessage<T>::value ? kProtobuf :\n                                  internal::ImplicitlyConvertible<const T &, internal::BiggestInt>::value ?\n                                  kConvertibleToInteger : kOtherType)>::PrintValue(x, &os);\n            return os;\n        }\n\n    }  // namespace internal2\n}  // namespace testing\n\n// This namespace MUST NOT BE NESTED IN ::testing, or the name look-up\n// magic needed for implementing UniversalPrinter won't work.\nnamespace testing_internal\n{\n\n// Used to print a value that is not an STL-style container when the\n// user doesn't define PrintTo() for it.\n    template <typename T>\n    void DefaultPrintNonContainerTo(const T &value, ::std::ostream *os)\n    {\n        // With the following statement, during unqualified name lookup,\n        // testing::internal2::operator<< appears as if it was declared in\n        // the nearest enclosing namespace that contains both\n        // ::testing_internal and ::testing::internal2, i.e. the global\n        // namespace.  For more details, refer to the C++ Standard section\n        // 7.3.4-1 [namespace.udir].  This allows us to fall back onto\n        // testing::internal2::operator<< in case T doesn't come with a <<\n        // operator.\n        //\n        // We cannot write 'using ::testing::internal2::operator<<;', which\n        // gcc 3.3 fails to compile due to a compiler bug.\n        using namespace ::testing::internal2;  // NOLINT\n\n        // Assuming T is defined in namespace foo, in the next statement,\n        // the compiler will consider all of:\n        //\n        //   1. foo::operator<< (thanks to Koenig look-up),\n        //   2. ::operator<< (as the current namespace is enclosed in ::),\n        //   3. testing::internal2::operator<< (thanks to the using statement above).\n        //\n        // The operator<< whose type matches T best will be picked.\n        //\n        // We deliberately allow #2 to be a candidate, as sometimes it's\n        // impossible to define #1 (e.g. when foo is ::std, defining\n        // anything in it is undefined behavior unless you are a compiler\n        // vendor.).\n        *os << value;\n    }\n\n}  // namespace testing_internal\n\nnamespace testing\n{\n    namespace internal\n    {\n\n// FormatForComparison<ToPrint, OtherOperand>::Format(value) formats a\n// value of type ToPrint that is an operand of a comparison assertion\n// (e.g. ASSERT_EQ).  OtherOperand is the type of the other operand in\n// the comparison, and is used to help determine the best way to\n// format the value.  In particular, when the value is a C string\n// (char pointer) and the other operand is an STL string object, we\n// want to format the C string as a string, since we know it is\n// compared by value with the string object.  If the value is a char\n// pointer but the other operand is not an STL string object, we don't\n// know whether the pointer is supposed to point to a NUL-terminated\n// string, and thus want to print it as a pointer to be safe.\n//\n// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.\n\n// The default case.\n        template <typename ToPrint, typename OtherOperand>\n        class FormatForComparison\n        {\n        public:\n            static ::std::string Format(const ToPrint &value)\n            {\n                return ::testing::PrintToString(value);\n            }\n        };\n\n// Array.\n        template <typename ToPrint, size_t N, typename OtherOperand>\n        class FormatForComparison<ToPrint[N], OtherOperand>\n        {\n        public:\n            static ::std::string Format(const ToPrint *value)\n            {\n                return FormatForComparison<const ToPrint *, OtherOperand>::Format(value);\n            }\n        };\n\n// By default, print C string as pointers to be safe, as we don't know\n// whether they actually point to a NUL-terminated string.\n\n#define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType)                \\\n  template <typename OtherOperand>                                      \\\n  class FormatForComparison<CharType*, OtherOperand> {                  \\\n   public:                                                              \\\n    static ::std::string Format(CharType* value) {                      \\\n      return ::testing::PrintToString(static_cast<const void*>(value)); \\\n    }                                                                   \\\n  }\n\n        GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char);\n        GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char);\n        GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(wchar_t);\n        GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const wchar_t);\n\n#undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_\n\n// If a C string is compared with an STL string object, we know it's meant\n// to point to a NUL-terminated string, and thus can print it as a string.\n\n#define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \\\n  template <>                                                           \\\n  class FormatForComparison<CharType*, OtherStringType> {               \\\n   public:                                                              \\\n    static ::std::string Format(CharType* value) {                      \\\n      return ::testing::PrintToString(value);                           \\\n    }                                                                   \\\n  }\n\n        GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::std::string);\n        GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::std::string);\n\n#if GTEST_HAS_GLOBAL_STRING\n        GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::string);\n        GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::string);\n#endif\n\n#if GTEST_HAS_GLOBAL_WSTRING\n        GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::wstring);\n        GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::wstring);\n#endif\n\n#if GTEST_HAS_STD_WSTRING\n        GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::std::wstring);\n        GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::std::wstring);\n#endif\n\n#undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_\n\n// Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc)\n// operand to be used in a failure message.  The type (but not value)\n// of the other operand may affect the format.  This allows us to\n// print a char* as a raw pointer when it is compared against another\n// char* or void*, and print it as a C string when it is compared\n// against an std::string object, for example.\n//\n// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.\n        template <typename T1, typename T2>\n        std::string FormatForComparisonFailureMessage(\n            const T1 &value, const T2 & /* other_operand */)\n        {\n            return FormatForComparison<T1, T2>::Format(value);\n        }\n\n// UniversalPrinter<T>::Print(value, ostream_ptr) prints the given\n// value to the given ostream.  The caller must ensure that\n// 'ostream_ptr' is not NULL, or the behavior is undefined.\n//\n// We define UniversalPrinter as a class template (as opposed to a\n// function template), as we need to partially specialize it for\n// reference types, which cannot be done with function templates.\n        template <typename T>\n        class UniversalPrinter;\n\n        template <typename T>\n        void UniversalPrint(const T &value, ::std::ostream *os);\n\n        enum DefaultPrinterType {\n            kPrintContainer,\n            kPrintPointer,\n            kPrintFunctionPointer,\n            kPrintOther,\n        };\n        template <DefaultPrinterType type> struct WrapPrinterType {};\n\n// Used to print an STL-style container when the user doesn't define\n// a PrintTo() for it.\n        template <typename C>\n        void DefaultPrintTo(WrapPrinterType<kPrintContainer> /* dummy */,\n                            const C &container, ::std::ostream *os)\n        {\n            const size_t kMaxCount = 32;  // The maximum number of elements to print.\n            *os << '{';\n            size_t count = 0;\n            for (typename C::const_iterator it = container.begin();\n                 it != container.end(); ++it, ++count) {\n                if (count > 0) {\n                    *os << ',';\n                    if (count == kMaxCount) {  // Enough has been printed.\n                        *os << \" ...\";\n                        break;\n                    }\n                }\n                *os << ' ';\n                // We cannot call PrintTo(*it, os) here as PrintTo() doesn't\n                // handle *it being a native array.\n                internal::UniversalPrint(*it, os);\n            }\n\n            if (count > 0) {\n                *os << ' ';\n            }\n            *os << '}';\n        }\n\n// Used to print a pointer that is neither a char pointer nor a member\n// pointer, when the user doesn't define PrintTo() for it.  (A member\n// variable pointer or member function pointer doesn't really point to\n// a location in the address space.  Their representation is\n// implementation-defined.  Therefore they will be printed as raw\n// bytes.)\n        template <typename T>\n        void DefaultPrintTo(WrapPrinterType<kPrintPointer> /* dummy */,\n                            T *p, ::std::ostream *os)\n        {\n            if (p == NULL) {\n                *os << \"NULL\";\n            } else {\n                // T is not a function type.  We just call << to print p,\n                // relying on ADL to pick up user-defined << for their pointer\n                // types, if any.\n                *os << p;\n            }\n        }\n        template <typename T>\n        void DefaultPrintTo(WrapPrinterType<kPrintFunctionPointer> /* dummy */,\n                            T *p, ::std::ostream *os)\n        {\n            if (p == NULL) {\n                *os << \"NULL\";\n            } else {\n                // T is a function type, so '*os << p' doesn't do what we want\n                // (it just prints p as bool).  We want to print p as a const\n                // void*.  However, we cannot cast it to const void* directly,\n                // even using reinterpret_cast, as earlier versions of gcc\n                // (e.g. 3.4.5) cannot compile the cast when p is a function\n                // pointer.  Casting to UInt64 first solves the problem.\n                *os << reinterpret_cast<const void *>(\n                        reinterpret_cast<internal::UInt64>(p));\n            }\n        }\n\n// Used to print a non-container, non-pointer value when the user\n// doesn't define PrintTo() for it.\n        template <typename T>\n        void DefaultPrintTo(WrapPrinterType<kPrintOther> /* dummy */,\n                            const T &value, ::std::ostream *os)\n        {\n            ::testing_internal::DefaultPrintNonContainerTo(value, os);\n        }\n\n// Prints the given value using the << operator if it has one;\n// otherwise prints the bytes in it.  This is what\n// UniversalPrinter<T>::Print() does when PrintTo() is not specialized\n// or overloaded for type T.\n//\n// A user can override this behavior for a class type Foo by defining\n// an overload of PrintTo() in the namespace where Foo is defined.  We\n// give the user this option as sometimes defining a << operator for\n// Foo is not desirable (e.g. the coding style may prevent doing it,\n// or there is already a << operator but it doesn't do what the user\n// wants).\n        template <typename T>\n        void PrintTo(const T &value, ::std::ostream *os)\n        {\n            // DefaultPrintTo() is overloaded.  The type of its first argument\n            // determines which version will be picked.\n            //\n            // Note that we check for container types here, prior to we check\n            // for protocol message types in our operator<<.  The rationale is:\n            //\n            // For protocol messages, we want to give people a chance to\n            // override Google Mock's format by defining a PrintTo() or\n            // operator<<.  For STL containers, other formats can be\n            // incompatible with Google Mock's format for the container\n            // elements; therefore we check for container types here to ensure\n            // that our format is used.\n            //\n            // Note that MSVC and clang-cl do allow an implicit conversion from\n            // pointer-to-function to pointer-to-object, but clang-cl warns on it.\n            // So don't use ImplicitlyConvertible if it can be helped since it will\n            // cause this warning, and use a separate overload of DefaultPrintTo for\n            // function pointers so that the `*os << p` in the object pointer overload\n            // doesn't cause that warning either.\n            DefaultPrintTo(\n                WrapPrinterType<sizeof(IsContainerTest<T>(0)) == sizeof(IsContainer)\n                ? kPrintContainer : !is_pointer<T>::value\n                ? kPrintOther\n#if GTEST_LANG_CXX11\n                : std::is_function<typename std::remove_pointer<T>::type>::value\n#else\n                : !internal::ImplicitlyConvertible<T, const void *>::value\n#endif\n                ? kPrintFunctionPointer\n                : kPrintPointer>(),\n                value, os);\n        }\n\n// The following list of PrintTo() overloads tells\n// UniversalPrinter<T>::Print() how to print standard types (built-in\n// types, strings, plain arrays, and pointers).\n\n// Overloads for various char types.\n        GTEST_API_ void PrintTo(unsigned char c, ::std::ostream *os);\n        GTEST_API_ void PrintTo(signed char c, ::std::ostream *os);\n        inline void PrintTo(char c, ::std::ostream *os)\n        {\n            // When printing a plain char, we always treat it as unsigned.  This\n            // way, the output won't be affected by whether the compiler thinks\n            // char is signed or not.\n            PrintTo(static_cast<unsigned char>(c), os);\n        }\n\n// Overloads for other simple built-in types.\n        inline void PrintTo(bool x, ::std::ostream *os)\n        {\n            *os << (x ? \"true\" : \"false\");\n        }\n\n// Overload for wchar_t type.\n// Prints a wchar_t as a symbol if it is printable or as its internal\n// code otherwise and also as its decimal code (except for L'\\0').\n// The L'\\0' char is printed as \"L'\\\\0'\". The decimal code is printed\n// as signed integer when wchar_t is implemented by the compiler\n// as a signed type and is printed as an unsigned integer when wchar_t\n// is implemented as an unsigned type.\n        GTEST_API_ void PrintTo(wchar_t wc, ::std::ostream *os);\n\n// Overloads for C strings.\n        GTEST_API_ void PrintTo(const char *s, ::std::ostream *os);\n        inline void PrintTo(char *s, ::std::ostream *os)\n        {\n            PrintTo(ImplicitCast_<const char *>(s), os);\n        }\n\n// signed/unsigned char is often used for representing binary data, so\n// we print pointers to it as void* to be safe.\n        inline void PrintTo(const signed char *s, ::std::ostream *os)\n        {\n            PrintTo(ImplicitCast_<const void *>(s), os);\n        }\n        inline void PrintTo(signed char *s, ::std::ostream *os)\n        {\n            PrintTo(ImplicitCast_<const void *>(s), os);\n        }\n        inline void PrintTo(const unsigned char *s, ::std::ostream *os)\n        {\n            PrintTo(ImplicitCast_<const void *>(s), os);\n        }\n        inline void PrintTo(unsigned char *s, ::std::ostream *os)\n        {\n            PrintTo(ImplicitCast_<const void *>(s), os);\n        }\n\n// MSVC can be configured to define wchar_t as a typedef of unsigned\n// short.  It defines _NATIVE_WCHAR_T_DEFINED when wchar_t is a native\n// type.  When wchar_t is a typedef, defining an overload for const\n// wchar_t* would cause unsigned short* be printed as a wide string,\n// possibly causing invalid memory accesses.\n#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)\n// Overloads for wide C strings\n        GTEST_API_ void PrintTo(const wchar_t *s, ::std::ostream *os);\n        inline void PrintTo(wchar_t *s, ::std::ostream *os)\n        {\n            PrintTo(ImplicitCast_<const wchar_t *>(s), os);\n        }\n#endif\n\n// Overload for C arrays.  Multi-dimensional arrays are printed\n// properly.\n\n// Prints the given number of elements in an array, without printing\n// the curly braces.\n        template <typename T>\n        void PrintRawArrayTo(const T a[], size_t count, ::std::ostream *os)\n        {\n            UniversalPrint(a[0], os);\n            for (size_t i = 1; i != count; i++) {\n                *os << \", \";\n                UniversalPrint(a[i], os);\n            }\n        }\n\n// Overloads for ::string and ::std::string.\n#if GTEST_HAS_GLOBAL_STRING\n        GTEST_API_ void PrintStringTo(const ::string &s, ::std::ostream *os);\n        inline void PrintTo(const ::string &s, ::std::ostream *os)\n        {\n            PrintStringTo(s, os);\n        }\n#endif  // GTEST_HAS_GLOBAL_STRING\n\n        GTEST_API_ void PrintStringTo(const ::std::string &s, ::std::ostream *os);\n        inline void PrintTo(const ::std::string &s, ::std::ostream *os)\n        {\n            PrintStringTo(s, os);\n        }\n\n// Overloads for ::wstring and ::std::wstring.\n#if GTEST_HAS_GLOBAL_WSTRING\n        GTEST_API_ void PrintWideStringTo(const ::wstring &s, ::std::ostream *os);\n        inline void PrintTo(const ::wstring &s, ::std::ostream *os)\n        {\n            PrintWideStringTo(s, os);\n        }\n#endif  // GTEST_HAS_GLOBAL_WSTRING\n\n#if GTEST_HAS_STD_WSTRING\n        GTEST_API_ void PrintWideStringTo(const ::std::wstring &s, ::std::ostream *os);\n        inline void PrintTo(const ::std::wstring &s, ::std::ostream *os)\n        {\n            PrintWideStringTo(s, os);\n        }\n#endif  // GTEST_HAS_STD_WSTRING\n\n#if GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_\n// Helper function for printing a tuple.  T must be instantiated with\n// a tuple type.\n        template <typename T>\n        void PrintTupleTo(const T &t, ::std::ostream *os);\n#endif  // GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_\n\n#if GTEST_HAS_TR1_TUPLE\n// Overload for ::std::tr1::tuple.  Needed for printing function arguments,\n// which are packed as tuples.\n\n// Overloaded PrintTo() for tuples of various arities.  We support\n// tuples of up-to 10 fields.  The following implementation works\n// regardless of whether tr1::tuple is implemented using the\n// non-standard variadic template feature or not.\n\n        inline void PrintTo(const ::std::tr1::tuple<> &t, ::std::ostream *os)\n        {\n            PrintTupleTo(t, os);\n        }\n\n        template <typename T1>\n        void PrintTo(const ::std::tr1::tuple<T1> &t, ::std::ostream *os)\n        {\n            PrintTupleTo(t, os);\n        }\n\n        template <typename T1, typename T2>\n        void PrintTo(const ::std::tr1::tuple<T1, T2> &t, ::std::ostream *os)\n        {\n            PrintTupleTo(t, os);\n        }\n\n        template <typename T1, typename T2, typename T3>\n        void PrintTo(const ::std::tr1::tuple<T1, T2, T3> &t, ::std::ostream *os)\n        {\n            PrintTupleTo(t, os);\n        }\n\n        template <typename T1, typename T2, typename T3, typename T4>\n        void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4> &t, ::std::ostream *os)\n        {\n            PrintTupleTo(t, os);\n        }\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5>\n        void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5> &t,\n                     ::std::ostream *os)\n        {\n            PrintTupleTo(t, os);\n        }\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6>\n        void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6> &t,\n                     ::std::ostream *os)\n        {\n            PrintTupleTo(t, os);\n        }\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7>\n        void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7> &t,\n                     ::std::ostream *os)\n        {\n            PrintTupleTo(t, os);\n        }\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8>\n        void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8> &t,\n                     ::std::ostream *os)\n        {\n            PrintTupleTo(t, os);\n        }\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9>\n        void PrintTo(const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9> &t,\n                     ::std::ostream *os)\n        {\n            PrintTupleTo(t, os);\n        }\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10>\n        void PrintTo(\n            const ::std::tr1::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> &t,\n            ::std::ostream *os)\n        {\n            PrintTupleTo(t, os);\n        }\n#endif  // GTEST_HAS_TR1_TUPLE\n\n#if GTEST_HAS_STD_TUPLE_\n        template <typename... Types>\n        void PrintTo(const ::std::tuple<Types...> &t, ::std::ostream *os)\n        {\n            PrintTupleTo(t, os);\n        }\n#endif  // GTEST_HAS_STD_TUPLE_\n\n// Overload for std::pair.\n        template <typename T1, typename T2>\n        void PrintTo(const ::std::pair<T1, T2> &value, ::std::ostream *os)\n        {\n            *os << '(';\n            // We cannot use UniversalPrint(value.first, os) here, as T1 may be\n            // a reference type.  The same for printing value.second.\n            UniversalPrinter<T1>::Print(value.first, os);\n            *os << \", \";\n            UniversalPrinter<T2>::Print(value.second, os);\n            *os << ')';\n        }\n\n// Implements printing a non-reference type T by letting the compiler\n// pick the right overload of PrintTo() for T.\n        template <typename T>\n        class UniversalPrinter\n        {\n        public:\n            // MSVC warns about adding const to a function type, so we want to\n            // disable the warning.\n            GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)\n\n            // Note: we deliberately don't call this PrintTo(), as that name\n            // conflicts with ::testing::internal::PrintTo in the body of the\n            // function.\n            static void Print(const T &value, ::std::ostream *os)\n            {\n                // By default, ::testing::internal::PrintTo() is used for printing\n                // the value.\n                //\n                // Thanks to Koenig look-up, if T is a class and has its own\n                // PrintTo() function defined in its namespace, that function will\n                // be visible here.  Since it is more specific than the generic ones\n                // in ::testing::internal, it will be picked by the compiler in the\n                // following statement - exactly what we want.\n                PrintTo(value, os);\n            }\n\n            GTEST_DISABLE_MSC_WARNINGS_POP_()\n        };\n\n// UniversalPrintArray(begin, len, os) prints an array of 'len'\n// elements, starting at address 'begin'.\n        template <typename T>\n        void UniversalPrintArray(const T *begin, size_t len, ::std::ostream *os)\n        {\n            if (len == 0) {\n                *os << \"{}\";\n            } else {\n                *os << \"{ \";\n                const size_t kThreshold = 18;\n                const size_t kChunkSize = 8;\n                // If the array has more than kThreshold elements, we'll have to\n                // omit some details by printing only the first and the last\n                // kChunkSize elements.\n                // TODO(wan@google.com): let the user control the threshold using a flag.\n                if (len <= kThreshold) {\n                    PrintRawArrayTo(begin, len, os);\n                } else {\n                    PrintRawArrayTo(begin, kChunkSize, os);\n                    *os << \", ..., \";\n                    PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os);\n                }\n                *os << \" }\";\n            }\n        }\n// This overload prints a (const) char array compactly.\n        GTEST_API_ void UniversalPrintArray(\n            const char *begin, size_t len, ::std::ostream *os);\n\n// This overload prints a (const) wchar_t array compactly.\n        GTEST_API_ void UniversalPrintArray(\n            const wchar_t *begin, size_t len, ::std::ostream *os);\n\n// Implements printing an array type T[N].\n        template <typename T, size_t N>\n        class UniversalPrinter<T[N]>\n        {\n        public:\n            // Prints the given array, omitting some elements when there are too\n            // many.\n            static void Print(const T (&a)[N], ::std::ostream *os)\n            {\n                UniversalPrintArray(a, N, os);\n            }\n        };\n\n// Implements printing a reference type T&.\n        template <typename T>\n        class UniversalPrinter<T &>\n        {\n        public:\n            // MSVC warns about adding const to a function type, so we want to\n            // disable the warning.\n            GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)\n\n            static void Print(const T &value, ::std::ostream *os)\n            {\n                // Prints the address of the value.  We use reinterpret_cast here\n                // as static_cast doesn't compile when T is a function type.\n                *os << \"@\" << reinterpret_cast<const void *>(&value) << \" \";\n\n                // Then prints the value itself.\n                UniversalPrint(value, os);\n            }\n\n            GTEST_DISABLE_MSC_WARNINGS_POP_()\n        };\n\n// Prints a value tersely: for a reference type, the referenced value\n// (but not the address) is printed; for a (const) char pointer, the\n// NUL-terminated string (but not the pointer) is printed.\n\n        template <typename T>\n        class UniversalTersePrinter\n        {\n        public:\n            static void Print(const T &value, ::std::ostream *os)\n            {\n                UniversalPrint(value, os);\n            }\n        };\n        template <typename T>\n        class UniversalTersePrinter<T &>\n        {\n        public:\n            static void Print(const T &value, ::std::ostream *os)\n            {\n                UniversalPrint(value, os);\n            }\n        };\n        template <typename T, size_t N>\n        class UniversalTersePrinter<T[N]>\n        {\n        public:\n            static void Print(const T (&value)[N], ::std::ostream *os)\n            {\n                UniversalPrinter<T[N]>::Print(value, os);\n            }\n        };\n        template <>\n        class UniversalTersePrinter<const char *>\n        {\n        public:\n            static void Print(const char *str, ::std::ostream *os)\n            {\n                if (str == NULL) {\n                    *os << \"NULL\";\n                } else {\n                    UniversalPrint(std::string(str), os);\n                }\n            }\n        };\n        template <>\n        class UniversalTersePrinter<char *>\n        {\n        public:\n            static void Print(char *str, ::std::ostream *os)\n            {\n                UniversalTersePrinter<const char *>::Print(str, os);\n            }\n        };\n\n#if GTEST_HAS_STD_WSTRING\n        template <>\n        class UniversalTersePrinter<const wchar_t *>\n        {\n        public:\n            static void Print(const wchar_t *str, ::std::ostream *os)\n            {\n                if (str == NULL) {\n                    *os << \"NULL\";\n                } else {\n                    UniversalPrint(::std::wstring(str), os);\n                }\n            }\n        };\n#endif\n\n        template <>\n        class UniversalTersePrinter<wchar_t *>\n        {\n        public:\n            static void Print(wchar_t *str, ::std::ostream *os)\n            {\n                UniversalTersePrinter<const wchar_t *>::Print(str, os);\n            }\n        };\n\n        template <typename T>\n        void UniversalTersePrint(const T &value, ::std::ostream *os)\n        {\n            UniversalTersePrinter<T>::Print(value, os);\n        }\n\n// Prints a value using the type inferred by the compiler.  The\n// difference between this and UniversalTersePrint() is that for a\n// (const) char pointer, this prints both the pointer and the\n// NUL-terminated string.\n        template <typename T>\n        void UniversalPrint(const T &value, ::std::ostream *os)\n        {\n            // A workarond for the bug in VC++ 7.1 that prevents us from instantiating\n            // UniversalPrinter with T directly.\n            typedef T T1;\n            UniversalPrinter<T1>::Print(value, os);\n        }\n\n        typedef ::std::vector<string> Strings;\n\n// TuplePolicy<TupleT> must provide:\n// - tuple_size\n//     size of tuple TupleT.\n// - get<size_t I>(const TupleT& t)\n//     static function extracting element I of tuple TupleT.\n// - tuple_element<size_t I>::type\n//     type of element I of tuple TupleT.\n        template <typename TupleT>\n        struct TuplePolicy;\n\n#if GTEST_HAS_TR1_TUPLE\n        template <typename TupleT>\n        struct TuplePolicy {\n            typedef TupleT Tuple;\n            static const size_t tuple_size = ::std::tr1::tuple_size<Tuple>::value;\n\n            template <size_t I>\n            struct tuple_element : ::std::tr1::tuple_element<I, Tuple> {};\n\n            template <size_t I>\n            static typename AddReference<\n            const typename ::std::tr1::tuple_element<I, Tuple>::type>::type get(\n                const Tuple &tuple)\n            {\n                return ::std::tr1::get<I>(tuple);\n            }\n        };\n        template <typename TupleT>\n        const size_t TuplePolicy<TupleT>::tuple_size;\n#endif  // GTEST_HAS_TR1_TUPLE\n\n#if GTEST_HAS_STD_TUPLE_\n        template <typename... Types>\n        struct TuplePolicy< ::std::tuple<Types...> > {\n            typedef ::std::tuple<Types...> Tuple;\n            static const size_t tuple_size = ::std::tuple_size<Tuple>::value;\n\n            template <size_t I>\n            struct tuple_element : ::std::tuple_element<I, Tuple> {};\n\n            template <size_t I>\n            static const typename ::std::tuple_element<I, Tuple>::type &get(\n                const Tuple &tuple)\n            {\n                return ::std::get<I>(tuple);\n            }\n        };\n        template <typename... Types>\n        const size_t TuplePolicy< ::std::tuple<Types...> >::tuple_size;\n#endif  // GTEST_HAS_STD_TUPLE_\n\n#if GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_\n// This helper template allows PrintTo() for tuples and\n// UniversalTersePrintTupleFieldsToStrings() to be defined by\n// induction on the number of tuple fields.  The idea is that\n// TuplePrefixPrinter<N>::PrintPrefixTo(t, os) prints the first N\n// fields in tuple t, and can be defined in terms of\n// TuplePrefixPrinter<N - 1>.\n//\n// The inductive case.\n        template <size_t N>\n        struct TuplePrefixPrinter {\n            // Prints the first N fields of a tuple.\n            template <typename Tuple>\n            static void PrintPrefixTo(const Tuple &t, ::std::ostream *os)\n            {\n                TuplePrefixPrinter<N - 1>::PrintPrefixTo(t, os);\n                GTEST_INTENTIONAL_CONST_COND_PUSH_()\n                if (N > 1) {\n                    GTEST_INTENTIONAL_CONST_COND_POP_()\n                    *os << \", \";\n                }\n                UniversalPrinter<\n                typename TuplePolicy<Tuple>::template tuple_element<N - 1>::type>\n                ::Print(TuplePolicy<Tuple>::template get<N - 1>(t), os);\n            }\n\n            // Tersely prints the first N fields of a tuple to a string vector,\n            // one element for each field.\n            template <typename Tuple>\n            static void TersePrintPrefixToStrings(const Tuple &t, Strings *strings)\n            {\n                TuplePrefixPrinter<N - 1>::TersePrintPrefixToStrings(t, strings);\n                ::std::stringstream ss;\n                UniversalTersePrint(TuplePolicy<Tuple>::template get<N - 1>(t), &ss);\n                strings->push_back(ss.str());\n            }\n        };\n\n// Base case.\n        template <>\n        struct TuplePrefixPrinter<0> {\n            template <typename Tuple>\n            static void PrintPrefixTo(const Tuple &, ::std::ostream *) {}\n\n            template <typename Tuple>\n            static void TersePrintPrefixToStrings(const Tuple &, Strings *) {}\n        };\n\n// Helper function for printing a tuple.\n// Tuple must be either std::tr1::tuple or std::tuple type.\n        template <typename Tuple>\n        void PrintTupleTo(const Tuple &t, ::std::ostream *os)\n        {\n            *os << \"(\";\n            TuplePrefixPrinter<TuplePolicy<Tuple>::tuple_size>::PrintPrefixTo(t, os);\n            *os << \")\";\n        }\n\n// Prints the fields of a tuple tersely to a string vector, one\n// element for each field.  See the comment before\n// UniversalTersePrint() for how we define \"tersely\".\n        template <typename Tuple>\n        Strings UniversalTersePrintTupleFieldsToStrings(const Tuple &value)\n        {\n            Strings result;\n            TuplePrefixPrinter<TuplePolicy<Tuple>::tuple_size>::\n            TersePrintPrefixToStrings(value, &result);\n            return result;\n        }\n#endif  // GTEST_HAS_TR1_TUPLE || GTEST_HAS_STD_TUPLE_\n\n    }  // namespace internal\n\n    template <typename T>\n    ::std::string PrintToString(const T &value)\n    {\n        ::std::stringstream ss;\n        internal::UniversalTersePrinter<T>::Print(value, &ss);\n        return ss.str();\n    }\n\n}  // namespace testing\n\n// Include any custom printer added by the local installation.\n// We must include this header at the end to make sure it can use the\n// declarations from this file.\n// Copyright 2015, Google Inc.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// This file provides an injection point for custom printers in a local\n// installation of gTest.\n// It will be included from gtest-printers.h and the overrides in this file\n// will be visible to everyone.\n// See documentation at gtest/gtest-printers.h for details on how to define a\n// custom printer.\n//\n// ** Custom implementation starts here **\n\n#ifndef GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PRINTERS_H_\n#define GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PRINTERS_H_\n\n#endif  // GTEST_INCLUDE_GTEST_INTERNAL_CUSTOM_GTEST_PRINTERS_H_\n\n#endif  // GTEST_INCLUDE_GTEST_GTEST_PRINTERS_H_\n\n#if GTEST_HAS_PARAM_TEST\n\nnamespace testing\n{\n\n// Input to a parameterized test name generator, describing a test parameter.\n// Consists of the parameter value and the integer parameter index.\n    template <class ParamType>\n    struct TestParamInfo {\n        TestParamInfo(const ParamType &a_param, size_t an_index) :\n            param(a_param),\n            index(an_index) {}\n        ParamType param;\n        size_t index;\n    };\n\n// A builtin parameterized test name generator which returns the result of\n// testing::PrintToString.\n    struct PrintToStringParamName {\n        template <class ParamType>\n        std::string operator()(const TestParamInfo<ParamType> &info) const\n        {\n            return PrintToString(info.param);\n        }\n    };\n\n    namespace internal\n    {\n\n// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.\n//\n// Outputs a message explaining invalid registration of different\n// fixture class for the same test case. This may happen when\n// TEST_P macro is used to define two tests with the same name\n// but in different namespaces.\n        GTEST_API_ void ReportInvalidTestCaseType(const char *test_case_name,\n                                                  CodeLocation code_location);\n\n        template <typename> class ParamGeneratorInterface;\n        template <typename> class ParamGenerator;\n\n// Interface for iterating over elements provided by an implementation\n// of ParamGeneratorInterface<T>.\n        template <typename T>\n        class ParamIteratorInterface\n        {\n        public:\n            virtual ~ParamIteratorInterface() {}\n            // A pointer to the base generator instance.\n            // Used only for the purposes of iterator comparison\n            // to make sure that two iterators belong to the same generator.\n            virtual const ParamGeneratorInterface<T> *BaseGenerator() const = 0;\n            // Advances iterator to point to the next element\n            // provided by the generator. The caller is responsible\n            // for not calling Advance() on an iterator equal to\n            // BaseGenerator()->End().\n            virtual void Advance() = 0;\n            // Clones the iterator object. Used for implementing copy semantics\n            // of ParamIterator<T>.\n            virtual ParamIteratorInterface *Clone() const = 0;\n            // Dereferences the current iterator and provides (read-only) access\n            // to the pointed value. It is the caller's responsibility not to call\n            // Current() on an iterator equal to BaseGenerator()->End().\n            // Used for implementing ParamGenerator<T>::operator*().\n            virtual const T *Current() const = 0;\n            // Determines whether the given iterator and other point to the same\n            // element in the sequence generated by the generator.\n            // Used for implementing ParamGenerator<T>::operator==().\n            virtual bool Equals(const ParamIteratorInterface &other) const = 0;\n        };\n\n// Class iterating over elements provided by an implementation of\n// ParamGeneratorInterface<T>. It wraps ParamIteratorInterface<T>\n// and implements the const forward iterator concept.\n        template <typename T>\n        class ParamIterator\n        {\n        public:\n            typedef T value_type;\n            typedef const T &reference;\n            typedef ptrdiff_t difference_type;\n\n            // ParamIterator assumes ownership of the impl_ pointer.\n            ParamIterator(const ParamIterator &other) : impl_(other.impl_->Clone()) {}\n            ParamIterator &operator=(const ParamIterator &other)\n            {\n                if (this != &other)\n                    impl_.reset(other.impl_->Clone());\n                return *this;\n            }\n\n            const T &operator*() const\n            {\n                return *impl_->Current();\n            }\n            const T *operator->() const\n            {\n                return impl_->Current();\n            }\n            // Prefix version of operator++.\n            ParamIterator &operator++()\n            {\n                impl_->Advance();\n                return *this;\n            }\n            // Postfix version of operator++.\n            ParamIterator operator++(int /*unused*/)\n            {\n                ParamIteratorInterface<T> *clone = impl_->Clone();\n                impl_->Advance();\n                return ParamIterator(clone);\n            }\n            bool operator==(const ParamIterator &other) const\n            {\n                return impl_.get() == other.impl_.get() || impl_->Equals(*other.impl_);\n            }\n            bool operator!=(const ParamIterator &other) const\n            {\n                return !(*this == other);\n            }\n\n        private:\n            friend class ParamGenerator<T>;\n            explicit ParamIterator(ParamIteratorInterface<T> *impl) : impl_(impl) {}\n            scoped_ptr<ParamIteratorInterface<T> > impl_;\n        };\n\n// ParamGeneratorInterface<T> is the binary interface to access generators\n// defined in other translation units.\n        template <typename T>\n        class ParamGeneratorInterface\n        {\n        public:\n            typedef T ParamType;\n\n            virtual ~ParamGeneratorInterface() {}\n\n            // Generator interface definition\n            virtual ParamIteratorInterface<T> *Begin() const = 0;\n            virtual ParamIteratorInterface<T> *End() const = 0;\n        };\n\n// Wraps ParamGeneratorInterface<T> and provides general generator syntax\n// compatible with the STL Container concept.\n// This class implements copy initialization semantics and the contained\n// ParamGeneratorInterface<T> instance is shared among all copies\n// of the original object. This is possible because that instance is immutable.\n        template<typename T>\n        class ParamGenerator\n        {\n        public:\n            typedef ParamIterator<T> iterator;\n\n            explicit ParamGenerator(ParamGeneratorInterface<T> *impl) : impl_(impl) {}\n            ParamGenerator(const ParamGenerator &other) : impl_(other.impl_) {}\n\n            ParamGenerator &operator=(const ParamGenerator &other)\n            {\n                impl_ = other.impl_;\n                return *this;\n            }\n\n            iterator begin() const\n            {\n                return iterator(impl_->Begin());\n            }\n            iterator end() const\n            {\n                return iterator(impl_->End());\n            }\n\n        private:\n            linked_ptr<const ParamGeneratorInterface<T> > impl_;\n        };\n\n// Generates values from a range of two comparable values. Can be used to\n// generate sequences of user-defined types that implement operator+() and\n// operator<().\n// This class is used in the Range() function.\n        template <typename T, typename IncrementT>\n        class RangeGenerator : public ParamGeneratorInterface<T>\n        {\n        public:\n            RangeGenerator(T begin, T end, IncrementT step)\n                : begin_(begin), end_(end),\n                  step_(step), end_index_(CalculateEndIndex(begin, end, step)) {}\n            virtual ~RangeGenerator() {}\n\n            virtual ParamIteratorInterface<T> *Begin() const\n            {\n                return new Iterator(this, begin_, 0, step_);\n            }\n            virtual ParamIteratorInterface<T> *End() const\n            {\n                return new Iterator(this, end_, end_index_, step_);\n            }\n\n        private:\n            class Iterator : public ParamIteratorInterface<T>\n            {\n            public:\n                Iterator(const ParamGeneratorInterface<T> *base, T value, int index,\n                         IncrementT step)\n                    : base_(base), value_(value), index_(index), step_(step) {}\n                virtual ~Iterator() {}\n\n                virtual const ParamGeneratorInterface<T> *BaseGenerator() const\n                {\n                    return base_;\n                }\n                virtual void Advance()\n                {\n                    value_ = static_cast<T>(value_ + step_);\n                    index_++;\n                }\n                virtual ParamIteratorInterface<T> *Clone() const\n                {\n                    return new Iterator(*this);\n                }\n                virtual const T *Current() const\n                {\n                    return &value_;\n                }\n                virtual bool Equals(const ParamIteratorInterface<T> &other) const\n                {\n                    // Having the same base generator guarantees that the other\n                    // iterator is of the same type and we can downcast.\n                    GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())\n                            << \"The program attempted to compare iterators \"\n                            << \"from different generators.\" << std::endl;\n                    const int other_index =\n                        CheckedDowncastToActualType<const Iterator>(&other)->index_;\n                    return index_ == other_index;\n                }\n\n            private:\n                Iterator(const Iterator &other)\n                    : ParamIteratorInterface<T>(),\n                      base_(other.base_), value_(other.value_), index_(other.index_),\n                      step_(other.step_) {}\n\n                // No implementation - assignment is unsupported.\n                void operator=(const Iterator &other);\n\n                const ParamGeneratorInterface<T> *const base_;\n                T value_;\n                int index_;\n                const IncrementT step_;\n            };  // class RangeGenerator::Iterator\n\n            static int CalculateEndIndex(const T &begin,\n                                         const T &end,\n                                         const IncrementT &step)\n            {\n                int end_index = 0;\n                for (T i = begin; i < end; i = static_cast<T>(i + step))\n                    end_index++;\n                return end_index;\n            }\n\n            // No implementation - assignment is unsupported.\n            void operator=(const RangeGenerator &other);\n\n            const T begin_;\n            const T end_;\n            const IncrementT step_;\n            // The index for the end() iterator. All the elements in the generated\n            // sequence are indexed (0-based) to aid iterator comparison.\n            const int end_index_;\n        };  // class RangeGenerator\n\n\n// Generates values from a pair of STL-style iterators. Used in the\n// ValuesIn() function. The elements are copied from the source range\n// since the source can be located on the stack, and the generator\n// is likely to persist beyond that stack frame.\n        template <typename T>\n        class ValuesInIteratorRangeGenerator : public ParamGeneratorInterface<T>\n        {\n        public:\n            template <typename ForwardIterator>\n            ValuesInIteratorRangeGenerator(ForwardIterator begin, ForwardIterator end)\n                : container_(begin, end) {}\n            virtual ~ValuesInIteratorRangeGenerator() {}\n\n            virtual ParamIteratorInterface<T> *Begin() const\n            {\n                return new Iterator(this, container_.begin());\n            }\n            virtual ParamIteratorInterface<T> *End() const\n            {\n                return new Iterator(this, container_.end());\n            }\n\n        private:\n            typedef typename ::std::vector<T> ContainerType;\n\n            class Iterator : public ParamIteratorInterface<T>\n            {\n            public:\n                Iterator(const ParamGeneratorInterface<T> *base,\n                         typename ContainerType::const_iterator iterator)\n                    : base_(base), iterator_(iterator) {}\n                virtual ~Iterator() {}\n\n                virtual const ParamGeneratorInterface<T> *BaseGenerator() const\n                {\n                    return base_;\n                }\n                virtual void Advance()\n                {\n                    ++iterator_;\n                    value_.reset();\n                }\n                virtual ParamIteratorInterface<T> *Clone() const\n                {\n                    return new Iterator(*this);\n                }\n                // We need to use cached value referenced by iterator_ because *iterator_\n                // can return a temporary object (and of type other then T), so just\n                // having \"return &*iterator_;\" doesn't work.\n                // value_ is updated here and not in Advance() because Advance()\n                // can advance iterator_ beyond the end of the range, and we cannot\n                // detect that fact. The client code, on the other hand, is\n                // responsible for not calling Current() on an out-of-range iterator.\n                virtual const T *Current() const\n                {\n                    if (value_.get() == NULL)\n                        value_.reset(new T(*iterator_));\n                    return value_.get();\n                }\n                virtual bool Equals(const ParamIteratorInterface<T> &other) const\n                {\n                    // Having the same base generator guarantees that the other\n                    // iterator is of the same type and we can downcast.\n                    GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())\n                            << \"The program attempted to compare iterators \"\n                            << \"from different generators.\" << std::endl;\n                    return iterator_ ==\n                           CheckedDowncastToActualType<const Iterator>(&other)->iterator_;\n                }\n\n            private:\n                Iterator(const Iterator &other)\n                // The explicit constructor call suppresses a false warning\n                // emitted by gcc when supplied with the -Wextra option.\n                    : ParamIteratorInterface<T>(),\n                      base_(other.base_),\n                      iterator_(other.iterator_) {}\n\n                const ParamGeneratorInterface<T> *const base_;\n                typename ContainerType::const_iterator iterator_;\n                // A cached value of *iterator_. We keep it here to allow access by\n                // pointer in the wrapping iterator's operator->().\n                // value_ needs to be mutable to be accessed in Current().\n                // Use of scoped_ptr helps manage cached value's lifetime,\n                // which is bound by the lifespan of the iterator itself.\n                mutable scoped_ptr<const T> value_;\n            };  // class ValuesInIteratorRangeGenerator::Iterator\n\n            // No implementation - assignment is unsupported.\n            void operator=(const ValuesInIteratorRangeGenerator &other);\n\n            const ContainerType container_;\n        };  // class ValuesInIteratorRangeGenerator\n\n// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.\n//\n// Default parameterized test name generator, returns a string containing the\n// integer test parameter index.\n        template <class ParamType>\n        std::string DefaultParamName(const TestParamInfo<ParamType> &info)\n        {\n            Message name_stream;\n            name_stream << info.index;\n            return name_stream.GetString();\n        }\n\n// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.\n//\n// Parameterized test name overload helpers, which help the\n// INSTANTIATE_TEST_CASE_P macro choose between the default parameterized\n// test name generator and user param name generator.\n        template <class ParamType, class ParamNameGenFunctor>\n        ParamNameGenFunctor GetParamNameGen(ParamNameGenFunctor func)\n        {\n            return func;\n        }\n\n        template <class ParamType>\n        struct ParamNameGenFunc {\n            typedef std::string Type(const TestParamInfo<ParamType> &);\n        };\n\n        template <class ParamType>\n        typename ParamNameGenFunc<ParamType>::Type *GetParamNameGen()\n        {\n            return DefaultParamName;\n        }\n\n// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.\n//\n// Stores a parameter value and later creates tests parameterized with that\n// value.\n        template <class TestClass>\n        class ParameterizedTestFactory : public TestFactoryBase\n        {\n        public:\n            typedef typename TestClass::ParamType ParamType;\n            explicit ParameterizedTestFactory(ParamType parameter) :\n                parameter_(parameter) {}\n            virtual Test *CreateTest()\n            {\n                TestClass::SetParam(&parameter_);\n                return new TestClass();\n            }\n\n        private:\n            const ParamType parameter_;\n\n            GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestFactory);\n        };\n\n// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.\n//\n// TestMetaFactoryBase is a base class for meta-factories that create\n// test factories for passing into MakeAndRegisterTestInfo function.\n        template <class ParamType>\n        class TestMetaFactoryBase\n        {\n        public:\n            virtual ~TestMetaFactoryBase() {}\n\n            virtual TestFactoryBase *CreateTestFactory(ParamType parameter) = 0;\n        };\n\n// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.\n//\n// TestMetaFactory creates test factories for passing into\n// MakeAndRegisterTestInfo function. Since MakeAndRegisterTestInfo receives\n// ownership of test factory pointer, same factory object cannot be passed\n// into that method twice. But ParameterizedTestCaseInfo is going to call\n// it for each Test/Parameter value combination. Thus it needs meta factory\n// creator class.\n        template <class TestCase>\n        class TestMetaFactory\n            : public TestMetaFactoryBase<typename TestCase::ParamType>\n        {\n        public:\n            typedef typename TestCase::ParamType ParamType;\n\n            TestMetaFactory() {}\n\n            virtual TestFactoryBase *CreateTestFactory(ParamType parameter)\n            {\n                return new ParameterizedTestFactory<TestCase>(parameter);\n            }\n\n        private:\n            GTEST_DISALLOW_COPY_AND_ASSIGN_(TestMetaFactory);\n        };\n\n// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.\n//\n// ParameterizedTestCaseInfoBase is a generic interface\n// to ParameterizedTestCaseInfo classes. ParameterizedTestCaseInfoBase\n// accumulates test information provided by TEST_P macro invocations\n// and generators provided by INSTANTIATE_TEST_CASE_P macro invocations\n// and uses that information to register all resulting test instances\n// in RegisterTests method. The ParameterizeTestCaseRegistry class holds\n// a collection of pointers to the ParameterizedTestCaseInfo objects\n// and calls RegisterTests() on each of them when asked.\n        class ParameterizedTestCaseInfoBase\n        {\n        public:\n            virtual ~ParameterizedTestCaseInfoBase() {}\n\n            // Base part of test case name for display purposes.\n            virtual const std::string &GetTestCaseName() const = 0;\n            // Test case id to verify identity.\n            virtual TypeId GetTestCaseTypeId() const = 0;\n            // UnitTest class invokes this method to register tests in this\n            // test case right before running them in RUN_ALL_TESTS macro.\n            // This method should not be called more then once on any single\n            // instance of a ParameterizedTestCaseInfoBase derived class.\n            virtual void RegisterTests() = 0;\n\n        protected:\n            ParameterizedTestCaseInfoBase() {}\n\n        private:\n            GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestCaseInfoBase);\n        };\n\n// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.\n//\n// ParameterizedTestCaseInfo accumulates tests obtained from TEST_P\n// macro invocations for a particular test case and generators\n// obtained from INSTANTIATE_TEST_CASE_P macro invocations for that\n// test case. It registers tests with all values generated by all\n// generators when asked.\n        template <class TestCase>\n        class ParameterizedTestCaseInfo : public ParameterizedTestCaseInfoBase\n        {\n        public:\n            // ParamType and GeneratorCreationFunc are private types but are required\n            // for declarations of public methods AddTestPattern() and\n            // AddTestCaseInstantiation().\n            typedef typename TestCase::ParamType ParamType;\n            // A function that returns an instance of appropriate generator type.\n            typedef ParamGenerator<ParamType>(GeneratorCreationFunc)();\n            typedef typename ParamNameGenFunc<ParamType>::Type ParamNameGeneratorFunc;\n\n            explicit ParameterizedTestCaseInfo(\n                const char *name, CodeLocation code_location)\n                : test_case_name_(name), code_location_(code_location) {}\n\n            // Test case base name for display purposes.\n            virtual const std::string &GetTestCaseName() const\n            {\n                return test_case_name_;\n            }\n            // Test case id to verify identity.\n            virtual TypeId GetTestCaseTypeId() const\n            {\n                return GetTypeId<TestCase>();\n            }\n            // TEST_P macro uses AddTestPattern() to record information\n            // about a single test in a LocalTestInfo structure.\n            // test_case_name is the base name of the test case (without invocation\n            // prefix). test_base_name is the name of an individual test without\n            // parameter index. For the test SequenceA/FooTest.DoBar/1 FooTest is\n            // test case base name and DoBar is test base name.\n            void AddTestPattern(const char *test_case_name,\n                                const char *test_base_name,\n                                TestMetaFactoryBase<ParamType> *meta_factory)\n            {\n                tests_.push_back(linked_ptr<TestInfo>(new TestInfo(test_case_name,\n                                                                   test_base_name,\n                                                                   meta_factory)));\n            }\n            // INSTANTIATE_TEST_CASE_P macro uses AddGenerator() to record information\n            // about a generator.\n            int AddTestCaseInstantiation(const std::string &instantiation_name,\n                                         GeneratorCreationFunc *func,\n                                         ParamNameGeneratorFunc *name_func,\n                                         const char *file, int line)\n            {\n                instantiations_.push_back(\n                    InstantiationInfo(instantiation_name, func, name_func, file, line));\n                return 0;  // Return value used only to run this method in namespace scope.\n            }\n            // UnitTest class invokes this method to register tests in this test case\n            // test cases right before running tests in RUN_ALL_TESTS macro.\n            // This method should not be called more then once on any single\n            // instance of a ParameterizedTestCaseInfoBase derived class.\n            // UnitTest has a guard to prevent from calling this method more then once.\n            virtual void RegisterTests()\n            {\n                for (typename TestInfoContainer::iterator test_it = tests_.begin();\n                     test_it != tests_.end(); ++test_it) {\n                    linked_ptr<TestInfo> test_info = *test_it;\n                    for (typename InstantiationContainer::iterator gen_it =\n                             instantiations_.begin(); gen_it != instantiations_.end();\n                         ++gen_it) {\n                        const std::string &instantiation_name = gen_it->name;\n                        ParamGenerator<ParamType> generator((*gen_it->generator)());\n                        ParamNameGeneratorFunc *name_func = gen_it->name_func;\n                        const char *file = gen_it->file;\n                        int line = gen_it->line;\n\n                        std::string test_case_name;\n                        if ( !instantiation_name.empty() )\n                            test_case_name = instantiation_name + \"/\";\n                        test_case_name += test_info->test_case_base_name;\n\n                        size_t i = 0;\n                        std::set<std::string> test_param_names;\n                        for (typename ParamGenerator<ParamType>::iterator param_it =\n                                 generator.begin();\n                             param_it != generator.end(); ++param_it, ++i) {\n                            Message test_name_stream;\n\n                            std::string param_name = name_func(\n                                                         TestParamInfo<ParamType>(*param_it, i));\n\n                            GTEST_CHECK_(IsValidParamName(param_name))\n                                    << \"Parameterized test name '\" << param_name\n                                    << \"' is invalid, in \" << file\n                                    << \" line \" << line << std::endl;\n\n                            GTEST_CHECK_(test_param_names.count(param_name) == 0)\n                                    << \"Duplicate parameterized test name '\" << param_name\n                                    << \"', in \" << file << \" line \" << line << std::endl;\n\n                            test_param_names.insert(param_name);\n\n                            test_name_stream << test_info->test_base_name << \"/\" << param_name;\n                            MakeAndRegisterTestInfo(\n                                test_case_name.c_str(),\n                                test_name_stream.GetString().c_str(),\n                                NULL,  // No type parameter.\n                                PrintToString(*param_it).c_str(),\n                                code_location_,\n                                GetTestCaseTypeId(),\n                                TestCase::SetUpTestCase,\n                                TestCase::TearDownTestCase,\n                                test_info->test_meta_factory->CreateTestFactory(*param_it));\n                        }  // for param_it\n                    }  // for gen_it\n                }  // for test_it\n            }  // RegisterTests\n\n        private:\n            // LocalTestInfo structure keeps information about a single test registered\n            // with TEST_P macro.\n            struct TestInfo {\n                TestInfo(const char *a_test_case_base_name,\n                         const char *a_test_base_name,\n                         TestMetaFactoryBase<ParamType> *a_test_meta_factory) :\n                    test_case_base_name(a_test_case_base_name),\n                    test_base_name(a_test_base_name),\n                    test_meta_factory(a_test_meta_factory) {}\n\n                const std::string test_case_base_name;\n                const std::string test_base_name;\n                const scoped_ptr<TestMetaFactoryBase<ParamType> > test_meta_factory;\n            };\n            typedef ::std::vector<linked_ptr<TestInfo> > TestInfoContainer;\n            // Records data received from INSTANTIATE_TEST_CASE_P macros:\n            //  <Instantiation name, Sequence generator creation function,\n            //     Name generator function, Source file, Source line>\n            struct InstantiationInfo {\n                InstantiationInfo(const std::string &name_in,\n                                  GeneratorCreationFunc *generator_in,\n                                  ParamNameGeneratorFunc *name_func_in,\n                                  const char *file_in,\n                                  int line_in)\n                    : name(name_in),\n                      generator(generator_in),\n                      name_func(name_func_in),\n                      file(file_in),\n                      line(line_in) {}\n\n                std::string name;\n                GeneratorCreationFunc *generator;\n                ParamNameGeneratorFunc *name_func;\n                const char *file;\n                int line;\n            };\n            typedef ::std::vector<InstantiationInfo> InstantiationContainer;\n\n            static bool IsValidParamName(const std::string &name)\n            {\n                // Check for empty string\n                if (name.empty())\n                    return false;\n\n                // Check for invalid characters\n                for (std::string::size_type index = 0; index < name.size(); ++index) {\n                    if (!isalnum(name[index]) && name[index] != '_')\n                        return false;\n                }\n\n                return true;\n            }\n\n            const std::string test_case_name_;\n            CodeLocation code_location_;\n            TestInfoContainer tests_;\n            InstantiationContainer instantiations_;\n\n            GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestCaseInfo);\n        };  // class ParameterizedTestCaseInfo\n\n// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.\n//\n// ParameterizedTestCaseRegistry contains a map of ParameterizedTestCaseInfoBase\n// classes accessed by test case names. TEST_P and INSTANTIATE_TEST_CASE_P\n// macros use it to locate their corresponding ParameterizedTestCaseInfo\n// descriptors.\n        class ParameterizedTestCaseRegistry\n        {\n        public:\n            ParameterizedTestCaseRegistry() {}\n            ~ParameterizedTestCaseRegistry()\n            {\n                for (TestCaseInfoContainer::iterator it = test_case_infos_.begin();\n                     it != test_case_infos_.end(); ++it) {\n                    delete *it;\n                }\n            }\n\n            // Looks up or creates and returns a structure containing information about\n            // tests and instantiations of a particular test case.\n            template <class TestCase>\n            ParameterizedTestCaseInfo<TestCase> *GetTestCasePatternHolder(\n                const char *test_case_name,\n                CodeLocation code_location)\n            {\n                ParameterizedTestCaseInfo<TestCase> *typed_test_info = NULL;\n                for (TestCaseInfoContainer::iterator it = test_case_infos_.begin();\n                     it != test_case_infos_.end(); ++it) {\n                    if ((*it)->GetTestCaseName() == test_case_name) {\n                        if ((*it)->GetTestCaseTypeId() != GetTypeId<TestCase>()) {\n                            // Complain about incorrect usage of Google Test facilities\n                            // and terminate the program since we cannot guaranty correct\n                            // test case setup and tear-down in this case.\n                            ReportInvalidTestCaseType(test_case_name, code_location);\n                            posix::Abort();\n                        } else {\n                            // At this point we are sure that the object we found is of the same\n                            // type we are looking for, so we downcast it to that type\n                            // without further checks.\n                            typed_test_info = CheckedDowncastToActualType<\n                                              ParameterizedTestCaseInfo<TestCase> >(*it);\n                        }\n                        break;\n                    }\n                }\n                if (typed_test_info == NULL) {\n                    typed_test_info = new ParameterizedTestCaseInfo<TestCase>(\n                        test_case_name, code_location);\n                    test_case_infos_.push_back(typed_test_info);\n                }\n                return typed_test_info;\n            }\n            void RegisterTests()\n            {\n                for (TestCaseInfoContainer::iterator it = test_case_infos_.begin();\n                     it != test_case_infos_.end(); ++it) {\n                    (*it)->RegisterTests();\n                }\n            }\n\n        private:\n            typedef ::std::vector<ParameterizedTestCaseInfoBase *> TestCaseInfoContainer;\n\n            TestCaseInfoContainer test_case_infos_;\n\n            GTEST_DISALLOW_COPY_AND_ASSIGN_(ParameterizedTestCaseRegistry);\n        };\n\n    }  // namespace internal\n}  // namespace testing\n\n#endif  //  GTEST_HAS_PARAM_TEST\n\n#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_H_\n// This file was GENERATED by command:\n//     pump.py gtest-param-util-generated.h.pump\n// DO NOT EDIT BY HAND!!!\n\n// Copyright 2008 Google Inc.\n// All Rights Reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Author: vladl@google.com (Vlad Losev)\n\n// Type and function utilities for implementing parameterized tests.\n// This file is generated by a SCRIPT.  DO NOT EDIT BY HAND!\n//\n// Currently Google Test supports at most 50 arguments in Values,\n// and at most 10 arguments in Combine. Please contact\n// googletestframework@googlegroups.com if you need more.\n// Please note that the number of arguments to Combine is limited\n// by the maximum arity of the implementation of tuple which is\n// currently set at 10.\n\n#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_GENERATED_H_\n#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_GENERATED_H_\n\n// scripts/fuse_gtest.py depends on gtest's own header being #included\n// *unconditionally*.  Therefore these #includes cannot be moved\n// inside #if GTEST_HAS_PARAM_TEST.\n\n#if GTEST_HAS_PARAM_TEST\n\nnamespace testing\n{\n\n// Forward declarations of ValuesIn(), which is implemented in\n// include/gtest/gtest-param-test.h.\n    template <typename ForwardIterator>\n    internal::ParamGenerator<\n    typename ::testing::internal::IteratorTraits<ForwardIterator>::value_type>\n    ValuesIn(ForwardIterator begin, ForwardIterator end);\n\n    template <typename T, size_t N>\n    internal::ParamGenerator<T> ValuesIn(const T (&array)[N]);\n\n    template <class Container>\n    internal::ParamGenerator<typename Container::value_type> ValuesIn(\n        const Container &container);\n\n    namespace internal\n    {\n\n// Used in the Values() function to provide polymorphic capabilities.\n        template <typename T1>\n        class ValueArray1\n        {\n        public:\n            explicit ValueArray1(T1 v1) : v1_(v1) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_)};\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray1 &other);\n\n            const T1 v1_;\n        };\n\n        template <typename T1, typename T2>\n        class ValueArray2\n        {\n        public:\n            ValueArray2(T1 v1, T2 v2) : v1_(v1), v2_(v2) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_)};\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray2 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n        };\n\n        template <typename T1, typename T2, typename T3>\n        class ValueArray3\n        {\n        public:\n            ValueArray3(T1 v1, T2 v2, T3 v3) : v1_(v1), v2_(v2), v3_(v3) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray3 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4>\n        class ValueArray4\n        {\n        public:\n            ValueArray4(T1 v1, T2 v2, T3 v3, T4 v4) : v1_(v1), v2_(v2), v3_(v3),\n                v4_(v4) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray4 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5>\n        class ValueArray5\n        {\n        public:\n            ValueArray5(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5) : v1_(v1), v2_(v2), v3_(v3),\n                v4_(v4), v5_(v5) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray5 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6>\n        class ValueArray6\n        {\n        public:\n            ValueArray6(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6) : v1_(v1), v2_(v2),\n                v3_(v3), v4_(v4), v5_(v5), v6_(v6) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray6 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7>\n        class ValueArray7\n        {\n        public:\n            ValueArray7(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7) : v1_(v1),\n                v2_(v2), v3_(v3), v4_(v4), v5_(v5), v6_(v6), v7_(v7) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray7 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8>\n        class ValueArray8\n        {\n        public:\n            ValueArray8(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7,\n                        T8 v8) : v1_(v1), v2_(v2), v3_(v3), v4_(v4), v5_(v5), v6_(v6), v7_(v7),\n                v8_(v8) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray8 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9>\n        class ValueArray9\n        {\n        public:\n            ValueArray9(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8,\n                        T9 v9) : v1_(v1), v2_(v2), v3_(v3), v4_(v4), v5_(v5), v6_(v6), v7_(v7),\n                v8_(v8), v9_(v9) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray9 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10>\n        class ValueArray10\n        {\n        public:\n            ValueArray10(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10) : v1_(v1), v2_(v2), v3_(v3), v4_(v4), v5_(v5), v6_(v6), v7_(v7),\n                v8_(v8), v9_(v9), v10_(v10) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray10 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11>\n        class ValueArray11\n        {\n        public:\n            ValueArray11(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11) : v1_(v1), v2_(v2), v3_(v3), v4_(v4), v5_(v5), v6_(v6),\n                v7_(v7), v8_(v8), v9_(v9), v10_(v10), v11_(v11) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray11 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12>\n        class ValueArray12\n        {\n        public:\n            ValueArray12(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12) : v1_(v1), v2_(v2), v3_(v3), v4_(v4), v5_(v5),\n                v6_(v6), v7_(v7), v8_(v8), v9_(v9), v10_(v10), v11_(v11), v12_(v12) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray12 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13>\n        class ValueArray13\n        {\n        public:\n            ValueArray13(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13) : v1_(v1), v2_(v2), v3_(v3), v4_(v4),\n                v5_(v5), v6_(v6), v7_(v7), v8_(v8), v9_(v9), v10_(v10), v11_(v11),\n                v12_(v12), v13_(v13) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray13 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14>\n        class ValueArray14\n        {\n        public:\n            ValueArray14(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14) : v1_(v1), v2_(v2), v3_(v3),\n                v4_(v4), v5_(v5), v6_(v6), v7_(v7), v8_(v8), v9_(v9), v10_(v10),\n                v11_(v11), v12_(v12), v13_(v13), v14_(v14) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray14 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15>\n        class ValueArray15\n        {\n        public:\n            ValueArray15(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15) : v1_(v1), v2_(v2),\n                v3_(v3), v4_(v4), v5_(v5), v6_(v6), v7_(v7), v8_(v8), v9_(v9), v10_(v10),\n                v11_(v11), v12_(v12), v13_(v13), v14_(v14), v15_(v15) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray15 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16>\n        class ValueArray16\n        {\n        public:\n            ValueArray16(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16) : v1_(v1),\n                v2_(v2), v3_(v3), v4_(v4), v5_(v5), v6_(v6), v7_(v7), v8_(v8), v9_(v9),\n                v10_(v10), v11_(v11), v12_(v12), v13_(v13), v14_(v14), v15_(v15),\n                v16_(v16) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray16 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17>\n        class ValueArray17\n        {\n        public:\n            ValueArray17(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16,\n                         T17 v17) : v1_(v1), v2_(v2), v3_(v3), v4_(v4), v5_(v5), v6_(v6), v7_(v7),\n                v8_(v8), v9_(v9), v10_(v10), v11_(v11), v12_(v12), v13_(v13), v14_(v14),\n                v15_(v15), v16_(v16), v17_(v17) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray17 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18>\n        class ValueArray18\n        {\n        public:\n            ValueArray18(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18) : v1_(v1), v2_(v2), v3_(v3), v4_(v4), v5_(v5), v6_(v6), v7_(v7),\n                v8_(v8), v9_(v9), v10_(v10), v11_(v11), v12_(v12), v13_(v13), v14_(v14),\n                v15_(v15), v16_(v16), v17_(v17), v18_(v18) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray18 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19>\n        class ValueArray19\n        {\n        public:\n            ValueArray19(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19) : v1_(v1), v2_(v2), v3_(v3), v4_(v4), v5_(v5), v6_(v6),\n                v7_(v7), v8_(v8), v9_(v9), v10_(v10), v11_(v11), v12_(v12), v13_(v13),\n                v14_(v14), v15_(v15), v16_(v16), v17_(v17), v18_(v18), v19_(v19) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray19 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20>\n        class ValueArray20\n        {\n        public:\n            ValueArray20(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20) : v1_(v1), v2_(v2), v3_(v3), v4_(v4), v5_(v5),\n                v6_(v6), v7_(v7), v8_(v8), v9_(v9), v10_(v10), v11_(v11), v12_(v12),\n                v13_(v13), v14_(v14), v15_(v15), v16_(v16), v17_(v17), v18_(v18),\n                v19_(v19), v20_(v20) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray20 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21>\n        class ValueArray21\n        {\n        public:\n            ValueArray21(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21) : v1_(v1), v2_(v2), v3_(v3), v4_(v4),\n                v5_(v5), v6_(v6), v7_(v7), v8_(v8), v9_(v9), v10_(v10), v11_(v11),\n                v12_(v12), v13_(v13), v14_(v14), v15_(v15), v16_(v16), v17_(v17),\n                v18_(v18), v19_(v19), v20_(v20), v21_(v21) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray21 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22>\n        class ValueArray22\n        {\n        public:\n            ValueArray22(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22) : v1_(v1), v2_(v2), v3_(v3),\n                v4_(v4), v5_(v5), v6_(v6), v7_(v7), v8_(v8), v9_(v9), v10_(v10),\n                v11_(v11), v12_(v12), v13_(v13), v14_(v14), v15_(v15), v16_(v16),\n                v17_(v17), v18_(v18), v19_(v19), v20_(v20), v21_(v21), v22_(v22) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray22 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23>\n        class ValueArray23\n        {\n        public:\n            ValueArray23(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23) : v1_(v1), v2_(v2),\n                v3_(v3), v4_(v4), v5_(v5), v6_(v6), v7_(v7), v8_(v8), v9_(v9), v10_(v10),\n                v11_(v11), v12_(v12), v13_(v13), v14_(v14), v15_(v15), v16_(v16),\n                v17_(v17), v18_(v18), v19_(v19), v20_(v20), v21_(v21), v22_(v22),\n                v23_(v23) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray23 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24>\n        class ValueArray24\n        {\n        public:\n            ValueArray24(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24) : v1_(v1),\n                v2_(v2), v3_(v3), v4_(v4), v5_(v5), v6_(v6), v7_(v7), v8_(v8), v9_(v9),\n                v10_(v10), v11_(v11), v12_(v12), v13_(v13), v14_(v14), v15_(v15),\n                v16_(v16), v17_(v17), v18_(v18), v19_(v19), v20_(v20), v21_(v21),\n                v22_(v22), v23_(v23), v24_(v24) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray24 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25>\n        class ValueArray25\n        {\n        public:\n            ValueArray25(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24,\n                         T25 v25) : v1_(v1), v2_(v2), v3_(v3), v4_(v4), v5_(v5), v6_(v6), v7_(v7),\n                v8_(v8), v9_(v9), v10_(v10), v11_(v11), v12_(v12), v13_(v13), v14_(v14),\n                v15_(v15), v16_(v16), v17_(v17), v18_(v18), v19_(v19), v20_(v20),\n                v21_(v21), v22_(v22), v23_(v23), v24_(v24), v25_(v25) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_), static_cast<T>(v25_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray25 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n            const T25 v25_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26>\n        class ValueArray26\n        {\n        public:\n            ValueArray26(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26) : v1_(v1), v2_(v2), v3_(v3), v4_(v4), v5_(v5), v6_(v6), v7_(v7),\n                v8_(v8), v9_(v9), v10_(v10), v11_(v11), v12_(v12), v13_(v13), v14_(v14),\n                v15_(v15), v16_(v16), v17_(v17), v18_(v18), v19_(v19), v20_(v20),\n                v21_(v21), v22_(v22), v23_(v23), v24_(v24), v25_(v25), v26_(v26) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_), static_cast<T>(v25_), static_cast<T>(v26_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray26 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n            const T25 v25_;\n            const T26 v26_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27>\n        class ValueArray27\n        {\n        public:\n            ValueArray27(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27) : v1_(v1), v2_(v2), v3_(v3), v4_(v4), v5_(v5), v6_(v6),\n                v7_(v7), v8_(v8), v9_(v9), v10_(v10), v11_(v11), v12_(v12), v13_(v13),\n                v14_(v14), v15_(v15), v16_(v16), v17_(v17), v18_(v18), v19_(v19),\n                v20_(v20), v21_(v21), v22_(v22), v23_(v23), v24_(v24), v25_(v25),\n                v26_(v26), v27_(v27) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_), static_cast<T>(v25_), static_cast<T>(v26_),\n                                   static_cast<T>(v27_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray27 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n            const T25 v25_;\n            const T26 v26_;\n            const T27 v27_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28>\n        class ValueArray28\n        {\n        public:\n            ValueArray28(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28) : v1_(v1), v2_(v2), v3_(v3), v4_(v4), v5_(v5),\n                v6_(v6), v7_(v7), v8_(v8), v9_(v9), v10_(v10), v11_(v11), v12_(v12),\n                v13_(v13), v14_(v14), v15_(v15), v16_(v16), v17_(v17), v18_(v18),\n                v19_(v19), v20_(v20), v21_(v21), v22_(v22), v23_(v23), v24_(v24),\n                v25_(v25), v26_(v26), v27_(v27), v28_(v28) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_), static_cast<T>(v25_), static_cast<T>(v26_),\n                                   static_cast<T>(v27_), static_cast<T>(v28_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray28 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n            const T25 v25_;\n            const T26 v26_;\n            const T27 v27_;\n            const T28 v28_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29>\n        class ValueArray29\n        {\n        public:\n            ValueArray29(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29) : v1_(v1), v2_(v2), v3_(v3), v4_(v4),\n                v5_(v5), v6_(v6), v7_(v7), v8_(v8), v9_(v9), v10_(v10), v11_(v11),\n                v12_(v12), v13_(v13), v14_(v14), v15_(v15), v16_(v16), v17_(v17),\n                v18_(v18), v19_(v19), v20_(v20), v21_(v21), v22_(v22), v23_(v23),\n                v24_(v24), v25_(v25), v26_(v26), v27_(v27), v28_(v28), v29_(v29) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_), static_cast<T>(v25_), static_cast<T>(v26_),\n                                   static_cast<T>(v27_), static_cast<T>(v28_), static_cast<T>(v29_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray29 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n            const T25 v25_;\n            const T26 v26_;\n            const T27 v27_;\n            const T28 v28_;\n            const T29 v29_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30>\n        class ValueArray30\n        {\n        public:\n            ValueArray30(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29, T30 v30) : v1_(v1), v2_(v2), v3_(v3),\n                v4_(v4), v5_(v5), v6_(v6), v7_(v7), v8_(v8), v9_(v9), v10_(v10),\n                v11_(v11), v12_(v12), v13_(v13), v14_(v14), v15_(v15), v16_(v16),\n                v17_(v17), v18_(v18), v19_(v19), v20_(v20), v21_(v21), v22_(v22),\n                v23_(v23), v24_(v24), v25_(v25), v26_(v26), v27_(v27), v28_(v28),\n                v29_(v29), v30_(v30) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_), static_cast<T>(v25_), static_cast<T>(v26_),\n                                   static_cast<T>(v27_), static_cast<T>(v28_), static_cast<T>(v29_),\n                                   static_cast<T>(v30_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray30 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n            const T25 v25_;\n            const T26 v26_;\n            const T27 v27_;\n            const T28 v28_;\n            const T29 v29_;\n            const T30 v30_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31>\n        class ValueArray31\n        {\n        public:\n            ValueArray31(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31) : v1_(v1), v2_(v2),\n                v3_(v3), v4_(v4), v5_(v5), v6_(v6), v7_(v7), v8_(v8), v9_(v9), v10_(v10),\n                v11_(v11), v12_(v12), v13_(v13), v14_(v14), v15_(v15), v16_(v16),\n                v17_(v17), v18_(v18), v19_(v19), v20_(v20), v21_(v21), v22_(v22),\n                v23_(v23), v24_(v24), v25_(v25), v26_(v26), v27_(v27), v28_(v28),\n                v29_(v29), v30_(v30), v31_(v31) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_), static_cast<T>(v25_), static_cast<T>(v26_),\n                                   static_cast<T>(v27_), static_cast<T>(v28_), static_cast<T>(v29_),\n                                   static_cast<T>(v30_), static_cast<T>(v31_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray31 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n            const T25 v25_;\n            const T26 v26_;\n            const T27 v27_;\n            const T28 v28_;\n            const T29 v29_;\n            const T30 v30_;\n            const T31 v31_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32>\n        class ValueArray32\n        {\n        public:\n            ValueArray32(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31, T32 v32) : v1_(v1),\n                v2_(v2), v3_(v3), v4_(v4), v5_(v5), v6_(v6), v7_(v7), v8_(v8), v9_(v9),\n                v10_(v10), v11_(v11), v12_(v12), v13_(v13), v14_(v14), v15_(v15),\n                v16_(v16), v17_(v17), v18_(v18), v19_(v19), v20_(v20), v21_(v21),\n                v22_(v22), v23_(v23), v24_(v24), v25_(v25), v26_(v26), v27_(v27),\n                v28_(v28), v29_(v29), v30_(v30), v31_(v31), v32_(v32) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_), static_cast<T>(v25_), static_cast<T>(v26_),\n                                   static_cast<T>(v27_), static_cast<T>(v28_), static_cast<T>(v29_),\n                                   static_cast<T>(v30_), static_cast<T>(v31_), static_cast<T>(v32_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray32 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n            const T25 v25_;\n            const T26 v26_;\n            const T27 v27_;\n            const T28 v28_;\n            const T29 v29_;\n            const T30 v30_;\n            const T31 v31_;\n            const T32 v32_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33>\n        class ValueArray33\n        {\n        public:\n            ValueArray33(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31, T32 v32,\n                         T33 v33) : v1_(v1), v2_(v2), v3_(v3), v4_(v4), v5_(v5), v6_(v6), v7_(v7),\n                v8_(v8), v9_(v9), v10_(v10), v11_(v11), v12_(v12), v13_(v13), v14_(v14),\n                v15_(v15), v16_(v16), v17_(v17), v18_(v18), v19_(v19), v20_(v20),\n                v21_(v21), v22_(v22), v23_(v23), v24_(v24), v25_(v25), v26_(v26),\n                v27_(v27), v28_(v28), v29_(v29), v30_(v30), v31_(v31), v32_(v32),\n                v33_(v33) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_), static_cast<T>(v25_), static_cast<T>(v26_),\n                                   static_cast<T>(v27_), static_cast<T>(v28_), static_cast<T>(v29_),\n                                   static_cast<T>(v30_), static_cast<T>(v31_), static_cast<T>(v32_),\n                                   static_cast<T>(v33_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray33 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n            const T25 v25_;\n            const T26 v26_;\n            const T27 v27_;\n            const T28 v28_;\n            const T29 v29_;\n            const T30 v30_;\n            const T31 v31_;\n            const T32 v32_;\n            const T33 v33_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34>\n        class ValueArray34\n        {\n        public:\n            ValueArray34(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31, T32 v32, T33 v33,\n                         T34 v34) : v1_(v1), v2_(v2), v3_(v3), v4_(v4), v5_(v5), v6_(v6), v7_(v7),\n                v8_(v8), v9_(v9), v10_(v10), v11_(v11), v12_(v12), v13_(v13), v14_(v14),\n                v15_(v15), v16_(v16), v17_(v17), v18_(v18), v19_(v19), v20_(v20),\n                v21_(v21), v22_(v22), v23_(v23), v24_(v24), v25_(v25), v26_(v26),\n                v27_(v27), v28_(v28), v29_(v29), v30_(v30), v31_(v31), v32_(v32),\n                v33_(v33), v34_(v34) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_), static_cast<T>(v25_), static_cast<T>(v26_),\n                                   static_cast<T>(v27_), static_cast<T>(v28_), static_cast<T>(v29_),\n                                   static_cast<T>(v30_), static_cast<T>(v31_), static_cast<T>(v32_),\n                                   static_cast<T>(v33_), static_cast<T>(v34_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray34 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n            const T25 v25_;\n            const T26 v26_;\n            const T27 v27_;\n            const T28 v28_;\n            const T29 v29_;\n            const T30 v30_;\n            const T31 v31_;\n            const T32 v32_;\n            const T33 v33_;\n            const T34 v34_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35>\n        class ValueArray35\n        {\n        public:\n            ValueArray35(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31, T32 v32, T33 v33,\n                         T34 v34, T35 v35) : v1_(v1), v2_(v2), v3_(v3), v4_(v4), v5_(v5), v6_(v6),\n                v7_(v7), v8_(v8), v9_(v9), v10_(v10), v11_(v11), v12_(v12), v13_(v13),\n                v14_(v14), v15_(v15), v16_(v16), v17_(v17), v18_(v18), v19_(v19),\n                v20_(v20), v21_(v21), v22_(v22), v23_(v23), v24_(v24), v25_(v25),\n                v26_(v26), v27_(v27), v28_(v28), v29_(v29), v30_(v30), v31_(v31),\n                v32_(v32), v33_(v33), v34_(v34), v35_(v35) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_), static_cast<T>(v25_), static_cast<T>(v26_),\n                                   static_cast<T>(v27_), static_cast<T>(v28_), static_cast<T>(v29_),\n                                   static_cast<T>(v30_), static_cast<T>(v31_), static_cast<T>(v32_),\n                                   static_cast<T>(v33_), static_cast<T>(v34_), static_cast<T>(v35_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray35 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n            const T25 v25_;\n            const T26 v26_;\n            const T27 v27_;\n            const T28 v28_;\n            const T29 v29_;\n            const T30 v30_;\n            const T31 v31_;\n            const T32 v32_;\n            const T33 v33_;\n            const T34 v34_;\n            const T35 v35_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36>\n        class ValueArray36\n        {\n        public:\n            ValueArray36(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31, T32 v32, T33 v33,\n                         T34 v34, T35 v35, T36 v36) : v1_(v1), v2_(v2), v3_(v3), v4_(v4), v5_(v5),\n                v6_(v6), v7_(v7), v8_(v8), v9_(v9), v10_(v10), v11_(v11), v12_(v12),\n                v13_(v13), v14_(v14), v15_(v15), v16_(v16), v17_(v17), v18_(v18),\n                v19_(v19), v20_(v20), v21_(v21), v22_(v22), v23_(v23), v24_(v24),\n                v25_(v25), v26_(v26), v27_(v27), v28_(v28), v29_(v29), v30_(v30),\n                v31_(v31), v32_(v32), v33_(v33), v34_(v34), v35_(v35), v36_(v36) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_), static_cast<T>(v25_), static_cast<T>(v26_),\n                                   static_cast<T>(v27_), static_cast<T>(v28_), static_cast<T>(v29_),\n                                   static_cast<T>(v30_), static_cast<T>(v31_), static_cast<T>(v32_),\n                                   static_cast<T>(v33_), static_cast<T>(v34_), static_cast<T>(v35_),\n                                   static_cast<T>(v36_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray36 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n            const T25 v25_;\n            const T26 v26_;\n            const T27 v27_;\n            const T28 v28_;\n            const T29 v29_;\n            const T30 v30_;\n            const T31 v31_;\n            const T32 v32_;\n            const T33 v33_;\n            const T34 v34_;\n            const T35 v35_;\n            const T36 v36_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37>\n        class ValueArray37\n        {\n        public:\n            ValueArray37(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31, T32 v32, T33 v33,\n                         T34 v34, T35 v35, T36 v36, T37 v37) : v1_(v1), v2_(v2), v3_(v3), v4_(v4),\n                v5_(v5), v6_(v6), v7_(v7), v8_(v8), v9_(v9), v10_(v10), v11_(v11),\n                v12_(v12), v13_(v13), v14_(v14), v15_(v15), v16_(v16), v17_(v17),\n                v18_(v18), v19_(v19), v20_(v20), v21_(v21), v22_(v22), v23_(v23),\n                v24_(v24), v25_(v25), v26_(v26), v27_(v27), v28_(v28), v29_(v29),\n                v30_(v30), v31_(v31), v32_(v32), v33_(v33), v34_(v34), v35_(v35),\n                v36_(v36), v37_(v37) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_), static_cast<T>(v25_), static_cast<T>(v26_),\n                                   static_cast<T>(v27_), static_cast<T>(v28_), static_cast<T>(v29_),\n                                   static_cast<T>(v30_), static_cast<T>(v31_), static_cast<T>(v32_),\n                                   static_cast<T>(v33_), static_cast<T>(v34_), static_cast<T>(v35_),\n                                   static_cast<T>(v36_), static_cast<T>(v37_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray37 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n            const T25 v25_;\n            const T26 v26_;\n            const T27 v27_;\n            const T28 v28_;\n            const T29 v29_;\n            const T30 v30_;\n            const T31 v31_;\n            const T32 v32_;\n            const T33 v33_;\n            const T34 v34_;\n            const T35 v35_;\n            const T36 v36_;\n            const T37 v37_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38>\n        class ValueArray38\n        {\n        public:\n            ValueArray38(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31, T32 v32, T33 v33,\n                         T34 v34, T35 v35, T36 v36, T37 v37, T38 v38) : v1_(v1), v2_(v2), v3_(v3),\n                v4_(v4), v5_(v5), v6_(v6), v7_(v7), v8_(v8), v9_(v9), v10_(v10),\n                v11_(v11), v12_(v12), v13_(v13), v14_(v14), v15_(v15), v16_(v16),\n                v17_(v17), v18_(v18), v19_(v19), v20_(v20), v21_(v21), v22_(v22),\n                v23_(v23), v24_(v24), v25_(v25), v26_(v26), v27_(v27), v28_(v28),\n                v29_(v29), v30_(v30), v31_(v31), v32_(v32), v33_(v33), v34_(v34),\n                v35_(v35), v36_(v36), v37_(v37), v38_(v38) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_), static_cast<T>(v25_), static_cast<T>(v26_),\n                                   static_cast<T>(v27_), static_cast<T>(v28_), static_cast<T>(v29_),\n                                   static_cast<T>(v30_), static_cast<T>(v31_), static_cast<T>(v32_),\n                                   static_cast<T>(v33_), static_cast<T>(v34_), static_cast<T>(v35_),\n                                   static_cast<T>(v36_), static_cast<T>(v37_), static_cast<T>(v38_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray38 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n            const T25 v25_;\n            const T26 v26_;\n            const T27 v27_;\n            const T28 v28_;\n            const T29 v29_;\n            const T30 v30_;\n            const T31 v31_;\n            const T32 v32_;\n            const T33 v33_;\n            const T34 v34_;\n            const T35 v35_;\n            const T36 v36_;\n            const T37 v37_;\n            const T38 v38_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38, typename T39>\n        class ValueArray39\n        {\n        public:\n            ValueArray39(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31, T32 v32, T33 v33,\n                         T34 v34, T35 v35, T36 v36, T37 v37, T38 v38, T39 v39) : v1_(v1), v2_(v2),\n                v3_(v3), v4_(v4), v5_(v5), v6_(v6), v7_(v7), v8_(v8), v9_(v9), v10_(v10),\n                v11_(v11), v12_(v12), v13_(v13), v14_(v14), v15_(v15), v16_(v16),\n                v17_(v17), v18_(v18), v19_(v19), v20_(v20), v21_(v21), v22_(v22),\n                v23_(v23), v24_(v24), v25_(v25), v26_(v26), v27_(v27), v28_(v28),\n                v29_(v29), v30_(v30), v31_(v31), v32_(v32), v33_(v33), v34_(v34),\n                v35_(v35), v36_(v36), v37_(v37), v38_(v38), v39_(v39) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_), static_cast<T>(v25_), static_cast<T>(v26_),\n                                   static_cast<T>(v27_), static_cast<T>(v28_), static_cast<T>(v29_),\n                                   static_cast<T>(v30_), static_cast<T>(v31_), static_cast<T>(v32_),\n                                   static_cast<T>(v33_), static_cast<T>(v34_), static_cast<T>(v35_),\n                                   static_cast<T>(v36_), static_cast<T>(v37_), static_cast<T>(v38_),\n                                   static_cast<T>(v39_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray39 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n            const T25 v25_;\n            const T26 v26_;\n            const T27 v27_;\n            const T28 v28_;\n            const T29 v29_;\n            const T30 v30_;\n            const T31 v31_;\n            const T32 v32_;\n            const T33 v33_;\n            const T34 v34_;\n            const T35 v35_;\n            const T36 v36_;\n            const T37 v37_;\n            const T38 v38_;\n            const T39 v39_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38, typename T39, typename T40>\n        class ValueArray40\n        {\n        public:\n            ValueArray40(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31, T32 v32, T33 v33,\n                         T34 v34, T35 v35, T36 v36, T37 v37, T38 v38, T39 v39, T40 v40) : v1_(v1),\n                v2_(v2), v3_(v3), v4_(v4), v5_(v5), v6_(v6), v7_(v7), v8_(v8), v9_(v9),\n                v10_(v10), v11_(v11), v12_(v12), v13_(v13), v14_(v14), v15_(v15),\n                v16_(v16), v17_(v17), v18_(v18), v19_(v19), v20_(v20), v21_(v21),\n                v22_(v22), v23_(v23), v24_(v24), v25_(v25), v26_(v26), v27_(v27),\n                v28_(v28), v29_(v29), v30_(v30), v31_(v31), v32_(v32), v33_(v33),\n                v34_(v34), v35_(v35), v36_(v36), v37_(v37), v38_(v38), v39_(v39),\n                v40_(v40) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_), static_cast<T>(v25_), static_cast<T>(v26_),\n                                   static_cast<T>(v27_), static_cast<T>(v28_), static_cast<T>(v29_),\n                                   static_cast<T>(v30_), static_cast<T>(v31_), static_cast<T>(v32_),\n                                   static_cast<T>(v33_), static_cast<T>(v34_), static_cast<T>(v35_),\n                                   static_cast<T>(v36_), static_cast<T>(v37_), static_cast<T>(v38_),\n                                   static_cast<T>(v39_), static_cast<T>(v40_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray40 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n            const T25 v25_;\n            const T26 v26_;\n            const T27 v27_;\n            const T28 v28_;\n            const T29 v29_;\n            const T30 v30_;\n            const T31 v31_;\n            const T32 v32_;\n            const T33 v33_;\n            const T34 v34_;\n            const T35 v35_;\n            const T36 v36_;\n            const T37 v37_;\n            const T38 v38_;\n            const T39 v39_;\n            const T40 v40_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38, typename T39, typename T40,\n                  typename T41>\n        class ValueArray41\n        {\n        public:\n            ValueArray41(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31, T32 v32, T33 v33,\n                         T34 v34, T35 v35, T36 v36, T37 v37, T38 v38, T39 v39, T40 v40,\n                         T41 v41) : v1_(v1), v2_(v2), v3_(v3), v4_(v4), v5_(v5), v6_(v6), v7_(v7),\n                v8_(v8), v9_(v9), v10_(v10), v11_(v11), v12_(v12), v13_(v13), v14_(v14),\n                v15_(v15), v16_(v16), v17_(v17), v18_(v18), v19_(v19), v20_(v20),\n                v21_(v21), v22_(v22), v23_(v23), v24_(v24), v25_(v25), v26_(v26),\n                v27_(v27), v28_(v28), v29_(v29), v30_(v30), v31_(v31), v32_(v32),\n                v33_(v33), v34_(v34), v35_(v35), v36_(v36), v37_(v37), v38_(v38),\n                v39_(v39), v40_(v40), v41_(v41) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_), static_cast<T>(v25_), static_cast<T>(v26_),\n                                   static_cast<T>(v27_), static_cast<T>(v28_), static_cast<T>(v29_),\n                                   static_cast<T>(v30_), static_cast<T>(v31_), static_cast<T>(v32_),\n                                   static_cast<T>(v33_), static_cast<T>(v34_), static_cast<T>(v35_),\n                                   static_cast<T>(v36_), static_cast<T>(v37_), static_cast<T>(v38_),\n                                   static_cast<T>(v39_), static_cast<T>(v40_), static_cast<T>(v41_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray41 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n            const T25 v25_;\n            const T26 v26_;\n            const T27 v27_;\n            const T28 v28_;\n            const T29 v29_;\n            const T30 v30_;\n            const T31 v31_;\n            const T32 v32_;\n            const T33 v33_;\n            const T34 v34_;\n            const T35 v35_;\n            const T36 v36_;\n            const T37 v37_;\n            const T38 v38_;\n            const T39 v39_;\n            const T40 v40_;\n            const T41 v41_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38, typename T39, typename T40,\n                  typename T41, typename T42>\n        class ValueArray42\n        {\n        public:\n            ValueArray42(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31, T32 v32, T33 v33,\n                         T34 v34, T35 v35, T36 v36, T37 v37, T38 v38, T39 v39, T40 v40, T41 v41,\n                         T42 v42) : v1_(v1), v2_(v2), v3_(v3), v4_(v4), v5_(v5), v6_(v6), v7_(v7),\n                v8_(v8), v9_(v9), v10_(v10), v11_(v11), v12_(v12), v13_(v13), v14_(v14),\n                v15_(v15), v16_(v16), v17_(v17), v18_(v18), v19_(v19), v20_(v20),\n                v21_(v21), v22_(v22), v23_(v23), v24_(v24), v25_(v25), v26_(v26),\n                v27_(v27), v28_(v28), v29_(v29), v30_(v30), v31_(v31), v32_(v32),\n                v33_(v33), v34_(v34), v35_(v35), v36_(v36), v37_(v37), v38_(v38),\n                v39_(v39), v40_(v40), v41_(v41), v42_(v42) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_), static_cast<T>(v25_), static_cast<T>(v26_),\n                                   static_cast<T>(v27_), static_cast<T>(v28_), static_cast<T>(v29_),\n                                   static_cast<T>(v30_), static_cast<T>(v31_), static_cast<T>(v32_),\n                                   static_cast<T>(v33_), static_cast<T>(v34_), static_cast<T>(v35_),\n                                   static_cast<T>(v36_), static_cast<T>(v37_), static_cast<T>(v38_),\n                                   static_cast<T>(v39_), static_cast<T>(v40_), static_cast<T>(v41_),\n                                   static_cast<T>(v42_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray42 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n            const T25 v25_;\n            const T26 v26_;\n            const T27 v27_;\n            const T28 v28_;\n            const T29 v29_;\n            const T30 v30_;\n            const T31 v31_;\n            const T32 v32_;\n            const T33 v33_;\n            const T34 v34_;\n            const T35 v35_;\n            const T36 v36_;\n            const T37 v37_;\n            const T38 v38_;\n            const T39 v39_;\n            const T40 v40_;\n            const T41 v41_;\n            const T42 v42_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38, typename T39, typename T40,\n                  typename T41, typename T42, typename T43>\n        class ValueArray43\n        {\n        public:\n            ValueArray43(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31, T32 v32, T33 v33,\n                         T34 v34, T35 v35, T36 v36, T37 v37, T38 v38, T39 v39, T40 v40, T41 v41,\n                         T42 v42, T43 v43) : v1_(v1), v2_(v2), v3_(v3), v4_(v4), v5_(v5), v6_(v6),\n                v7_(v7), v8_(v8), v9_(v9), v10_(v10), v11_(v11), v12_(v12), v13_(v13),\n                v14_(v14), v15_(v15), v16_(v16), v17_(v17), v18_(v18), v19_(v19),\n                v20_(v20), v21_(v21), v22_(v22), v23_(v23), v24_(v24), v25_(v25),\n                v26_(v26), v27_(v27), v28_(v28), v29_(v29), v30_(v30), v31_(v31),\n                v32_(v32), v33_(v33), v34_(v34), v35_(v35), v36_(v36), v37_(v37),\n                v38_(v38), v39_(v39), v40_(v40), v41_(v41), v42_(v42), v43_(v43) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_), static_cast<T>(v25_), static_cast<T>(v26_),\n                                   static_cast<T>(v27_), static_cast<T>(v28_), static_cast<T>(v29_),\n                                   static_cast<T>(v30_), static_cast<T>(v31_), static_cast<T>(v32_),\n                                   static_cast<T>(v33_), static_cast<T>(v34_), static_cast<T>(v35_),\n                                   static_cast<T>(v36_), static_cast<T>(v37_), static_cast<T>(v38_),\n                                   static_cast<T>(v39_), static_cast<T>(v40_), static_cast<T>(v41_),\n                                   static_cast<T>(v42_), static_cast<T>(v43_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray43 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n            const T25 v25_;\n            const T26 v26_;\n            const T27 v27_;\n            const T28 v28_;\n            const T29 v29_;\n            const T30 v30_;\n            const T31 v31_;\n            const T32 v32_;\n            const T33 v33_;\n            const T34 v34_;\n            const T35 v35_;\n            const T36 v36_;\n            const T37 v37_;\n            const T38 v38_;\n            const T39 v39_;\n            const T40 v40_;\n            const T41 v41_;\n            const T42 v42_;\n            const T43 v43_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38, typename T39, typename T40,\n                  typename T41, typename T42, typename T43, typename T44>\n        class ValueArray44\n        {\n        public:\n            ValueArray44(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31, T32 v32, T33 v33,\n                         T34 v34, T35 v35, T36 v36, T37 v37, T38 v38, T39 v39, T40 v40, T41 v41,\n                         T42 v42, T43 v43, T44 v44) : v1_(v1), v2_(v2), v3_(v3), v4_(v4), v5_(v5),\n                v6_(v6), v7_(v7), v8_(v8), v9_(v9), v10_(v10), v11_(v11), v12_(v12),\n                v13_(v13), v14_(v14), v15_(v15), v16_(v16), v17_(v17), v18_(v18),\n                v19_(v19), v20_(v20), v21_(v21), v22_(v22), v23_(v23), v24_(v24),\n                v25_(v25), v26_(v26), v27_(v27), v28_(v28), v29_(v29), v30_(v30),\n                v31_(v31), v32_(v32), v33_(v33), v34_(v34), v35_(v35), v36_(v36),\n                v37_(v37), v38_(v38), v39_(v39), v40_(v40), v41_(v41), v42_(v42),\n                v43_(v43), v44_(v44) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_), static_cast<T>(v25_), static_cast<T>(v26_),\n                                   static_cast<T>(v27_), static_cast<T>(v28_), static_cast<T>(v29_),\n                                   static_cast<T>(v30_), static_cast<T>(v31_), static_cast<T>(v32_),\n                                   static_cast<T>(v33_), static_cast<T>(v34_), static_cast<T>(v35_),\n                                   static_cast<T>(v36_), static_cast<T>(v37_), static_cast<T>(v38_),\n                                   static_cast<T>(v39_), static_cast<T>(v40_), static_cast<T>(v41_),\n                                   static_cast<T>(v42_), static_cast<T>(v43_), static_cast<T>(v44_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray44 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n            const T25 v25_;\n            const T26 v26_;\n            const T27 v27_;\n            const T28 v28_;\n            const T29 v29_;\n            const T30 v30_;\n            const T31 v31_;\n            const T32 v32_;\n            const T33 v33_;\n            const T34 v34_;\n            const T35 v35_;\n            const T36 v36_;\n            const T37 v37_;\n            const T38 v38_;\n            const T39 v39_;\n            const T40 v40_;\n            const T41 v41_;\n            const T42 v42_;\n            const T43 v43_;\n            const T44 v44_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38, typename T39, typename T40,\n                  typename T41, typename T42, typename T43, typename T44, typename T45>\n        class ValueArray45\n        {\n        public:\n            ValueArray45(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31, T32 v32, T33 v33,\n                         T34 v34, T35 v35, T36 v36, T37 v37, T38 v38, T39 v39, T40 v40, T41 v41,\n                         T42 v42, T43 v43, T44 v44, T45 v45) : v1_(v1), v2_(v2), v3_(v3), v4_(v4),\n                v5_(v5), v6_(v6), v7_(v7), v8_(v8), v9_(v9), v10_(v10), v11_(v11),\n                v12_(v12), v13_(v13), v14_(v14), v15_(v15), v16_(v16), v17_(v17),\n                v18_(v18), v19_(v19), v20_(v20), v21_(v21), v22_(v22), v23_(v23),\n                v24_(v24), v25_(v25), v26_(v26), v27_(v27), v28_(v28), v29_(v29),\n                v30_(v30), v31_(v31), v32_(v32), v33_(v33), v34_(v34), v35_(v35),\n                v36_(v36), v37_(v37), v38_(v38), v39_(v39), v40_(v40), v41_(v41),\n                v42_(v42), v43_(v43), v44_(v44), v45_(v45) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_), static_cast<T>(v25_), static_cast<T>(v26_),\n                                   static_cast<T>(v27_), static_cast<T>(v28_), static_cast<T>(v29_),\n                                   static_cast<T>(v30_), static_cast<T>(v31_), static_cast<T>(v32_),\n                                   static_cast<T>(v33_), static_cast<T>(v34_), static_cast<T>(v35_),\n                                   static_cast<T>(v36_), static_cast<T>(v37_), static_cast<T>(v38_),\n                                   static_cast<T>(v39_), static_cast<T>(v40_), static_cast<T>(v41_),\n                                   static_cast<T>(v42_), static_cast<T>(v43_), static_cast<T>(v44_),\n                                   static_cast<T>(v45_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray45 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n            const T25 v25_;\n            const T26 v26_;\n            const T27 v27_;\n            const T28 v28_;\n            const T29 v29_;\n            const T30 v30_;\n            const T31 v31_;\n            const T32 v32_;\n            const T33 v33_;\n            const T34 v34_;\n            const T35 v35_;\n            const T36 v36_;\n            const T37 v37_;\n            const T38 v38_;\n            const T39 v39_;\n            const T40 v40_;\n            const T41 v41_;\n            const T42 v42_;\n            const T43 v43_;\n            const T44 v44_;\n            const T45 v45_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38, typename T39, typename T40,\n                  typename T41, typename T42, typename T43, typename T44, typename T45,\n                  typename T46>\n        class ValueArray46\n        {\n        public:\n            ValueArray46(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31, T32 v32, T33 v33,\n                         T34 v34, T35 v35, T36 v36, T37 v37, T38 v38, T39 v39, T40 v40, T41 v41,\n                         T42 v42, T43 v43, T44 v44, T45 v45, T46 v46) : v1_(v1), v2_(v2), v3_(v3),\n                v4_(v4), v5_(v5), v6_(v6), v7_(v7), v8_(v8), v9_(v9), v10_(v10),\n                v11_(v11), v12_(v12), v13_(v13), v14_(v14), v15_(v15), v16_(v16),\n                v17_(v17), v18_(v18), v19_(v19), v20_(v20), v21_(v21), v22_(v22),\n                v23_(v23), v24_(v24), v25_(v25), v26_(v26), v27_(v27), v28_(v28),\n                v29_(v29), v30_(v30), v31_(v31), v32_(v32), v33_(v33), v34_(v34),\n                v35_(v35), v36_(v36), v37_(v37), v38_(v38), v39_(v39), v40_(v40),\n                v41_(v41), v42_(v42), v43_(v43), v44_(v44), v45_(v45), v46_(v46) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_), static_cast<T>(v25_), static_cast<T>(v26_),\n                                   static_cast<T>(v27_), static_cast<T>(v28_), static_cast<T>(v29_),\n                                   static_cast<T>(v30_), static_cast<T>(v31_), static_cast<T>(v32_),\n                                   static_cast<T>(v33_), static_cast<T>(v34_), static_cast<T>(v35_),\n                                   static_cast<T>(v36_), static_cast<T>(v37_), static_cast<T>(v38_),\n                                   static_cast<T>(v39_), static_cast<T>(v40_), static_cast<T>(v41_),\n                                   static_cast<T>(v42_), static_cast<T>(v43_), static_cast<T>(v44_),\n                                   static_cast<T>(v45_), static_cast<T>(v46_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray46 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n            const T25 v25_;\n            const T26 v26_;\n            const T27 v27_;\n            const T28 v28_;\n            const T29 v29_;\n            const T30 v30_;\n            const T31 v31_;\n            const T32 v32_;\n            const T33 v33_;\n            const T34 v34_;\n            const T35 v35_;\n            const T36 v36_;\n            const T37 v37_;\n            const T38 v38_;\n            const T39 v39_;\n            const T40 v40_;\n            const T41 v41_;\n            const T42 v42_;\n            const T43 v43_;\n            const T44 v44_;\n            const T45 v45_;\n            const T46 v46_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38, typename T39, typename T40,\n                  typename T41, typename T42, typename T43, typename T44, typename T45,\n                  typename T46, typename T47>\n        class ValueArray47\n        {\n        public:\n            ValueArray47(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31, T32 v32, T33 v33,\n                         T34 v34, T35 v35, T36 v36, T37 v37, T38 v38, T39 v39, T40 v40, T41 v41,\n                         T42 v42, T43 v43, T44 v44, T45 v45, T46 v46, T47 v47) : v1_(v1), v2_(v2),\n                v3_(v3), v4_(v4), v5_(v5), v6_(v6), v7_(v7), v8_(v8), v9_(v9), v10_(v10),\n                v11_(v11), v12_(v12), v13_(v13), v14_(v14), v15_(v15), v16_(v16),\n                v17_(v17), v18_(v18), v19_(v19), v20_(v20), v21_(v21), v22_(v22),\n                v23_(v23), v24_(v24), v25_(v25), v26_(v26), v27_(v27), v28_(v28),\n                v29_(v29), v30_(v30), v31_(v31), v32_(v32), v33_(v33), v34_(v34),\n                v35_(v35), v36_(v36), v37_(v37), v38_(v38), v39_(v39), v40_(v40),\n                v41_(v41), v42_(v42), v43_(v43), v44_(v44), v45_(v45), v46_(v46),\n                v47_(v47) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_), static_cast<T>(v25_), static_cast<T>(v26_),\n                                   static_cast<T>(v27_), static_cast<T>(v28_), static_cast<T>(v29_),\n                                   static_cast<T>(v30_), static_cast<T>(v31_), static_cast<T>(v32_),\n                                   static_cast<T>(v33_), static_cast<T>(v34_), static_cast<T>(v35_),\n                                   static_cast<T>(v36_), static_cast<T>(v37_), static_cast<T>(v38_),\n                                   static_cast<T>(v39_), static_cast<T>(v40_), static_cast<T>(v41_),\n                                   static_cast<T>(v42_), static_cast<T>(v43_), static_cast<T>(v44_),\n                                   static_cast<T>(v45_), static_cast<T>(v46_), static_cast<T>(v47_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray47 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n            const T25 v25_;\n            const T26 v26_;\n            const T27 v27_;\n            const T28 v28_;\n            const T29 v29_;\n            const T30 v30_;\n            const T31 v31_;\n            const T32 v32_;\n            const T33 v33_;\n            const T34 v34_;\n            const T35 v35_;\n            const T36 v36_;\n            const T37 v37_;\n            const T38 v38_;\n            const T39 v39_;\n            const T40 v40_;\n            const T41 v41_;\n            const T42 v42_;\n            const T43 v43_;\n            const T44 v44_;\n            const T45 v45_;\n            const T46 v46_;\n            const T47 v47_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38, typename T39, typename T40,\n                  typename T41, typename T42, typename T43, typename T44, typename T45,\n                  typename T46, typename T47, typename T48>\n        class ValueArray48\n        {\n        public:\n            ValueArray48(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31, T32 v32, T33 v33,\n                         T34 v34, T35 v35, T36 v36, T37 v37, T38 v38, T39 v39, T40 v40, T41 v41,\n                         T42 v42, T43 v43, T44 v44, T45 v45, T46 v46, T47 v47, T48 v48) : v1_(v1),\n                v2_(v2), v3_(v3), v4_(v4), v5_(v5), v6_(v6), v7_(v7), v8_(v8), v9_(v9),\n                v10_(v10), v11_(v11), v12_(v12), v13_(v13), v14_(v14), v15_(v15),\n                v16_(v16), v17_(v17), v18_(v18), v19_(v19), v20_(v20), v21_(v21),\n                v22_(v22), v23_(v23), v24_(v24), v25_(v25), v26_(v26), v27_(v27),\n                v28_(v28), v29_(v29), v30_(v30), v31_(v31), v32_(v32), v33_(v33),\n                v34_(v34), v35_(v35), v36_(v36), v37_(v37), v38_(v38), v39_(v39),\n                v40_(v40), v41_(v41), v42_(v42), v43_(v43), v44_(v44), v45_(v45),\n                v46_(v46), v47_(v47), v48_(v48) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_), static_cast<T>(v25_), static_cast<T>(v26_),\n                                   static_cast<T>(v27_), static_cast<T>(v28_), static_cast<T>(v29_),\n                                   static_cast<T>(v30_), static_cast<T>(v31_), static_cast<T>(v32_),\n                                   static_cast<T>(v33_), static_cast<T>(v34_), static_cast<T>(v35_),\n                                   static_cast<T>(v36_), static_cast<T>(v37_), static_cast<T>(v38_),\n                                   static_cast<T>(v39_), static_cast<T>(v40_), static_cast<T>(v41_),\n                                   static_cast<T>(v42_), static_cast<T>(v43_), static_cast<T>(v44_),\n                                   static_cast<T>(v45_), static_cast<T>(v46_), static_cast<T>(v47_),\n                                   static_cast<T>(v48_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray48 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n            const T25 v25_;\n            const T26 v26_;\n            const T27 v27_;\n            const T28 v28_;\n            const T29 v29_;\n            const T30 v30_;\n            const T31 v31_;\n            const T32 v32_;\n            const T33 v33_;\n            const T34 v34_;\n            const T35 v35_;\n            const T36 v36_;\n            const T37 v37_;\n            const T38 v38_;\n            const T39 v39_;\n            const T40 v40_;\n            const T41 v41_;\n            const T42 v42_;\n            const T43 v43_;\n            const T44 v44_;\n            const T45 v45_;\n            const T46 v46_;\n            const T47 v47_;\n            const T48 v48_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38, typename T39, typename T40,\n                  typename T41, typename T42, typename T43, typename T44, typename T45,\n                  typename T46, typename T47, typename T48, typename T49>\n        class ValueArray49\n        {\n        public:\n            ValueArray49(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31, T32 v32, T33 v33,\n                         T34 v34, T35 v35, T36 v36, T37 v37, T38 v38, T39 v39, T40 v40, T41 v41,\n                         T42 v42, T43 v43, T44 v44, T45 v45, T46 v46, T47 v47, T48 v48,\n                         T49 v49) : v1_(v1), v2_(v2), v3_(v3), v4_(v4), v5_(v5), v6_(v6), v7_(v7),\n                v8_(v8), v9_(v9), v10_(v10), v11_(v11), v12_(v12), v13_(v13), v14_(v14),\n                v15_(v15), v16_(v16), v17_(v17), v18_(v18), v19_(v19), v20_(v20),\n                v21_(v21), v22_(v22), v23_(v23), v24_(v24), v25_(v25), v26_(v26),\n                v27_(v27), v28_(v28), v29_(v29), v30_(v30), v31_(v31), v32_(v32),\n                v33_(v33), v34_(v34), v35_(v35), v36_(v36), v37_(v37), v38_(v38),\n                v39_(v39), v40_(v40), v41_(v41), v42_(v42), v43_(v43), v44_(v44),\n                v45_(v45), v46_(v46), v47_(v47), v48_(v48), v49_(v49) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_), static_cast<T>(v25_), static_cast<T>(v26_),\n                                   static_cast<T>(v27_), static_cast<T>(v28_), static_cast<T>(v29_),\n                                   static_cast<T>(v30_), static_cast<T>(v31_), static_cast<T>(v32_),\n                                   static_cast<T>(v33_), static_cast<T>(v34_), static_cast<T>(v35_),\n                                   static_cast<T>(v36_), static_cast<T>(v37_), static_cast<T>(v38_),\n                                   static_cast<T>(v39_), static_cast<T>(v40_), static_cast<T>(v41_),\n                                   static_cast<T>(v42_), static_cast<T>(v43_), static_cast<T>(v44_),\n                                   static_cast<T>(v45_), static_cast<T>(v46_), static_cast<T>(v47_),\n                                   static_cast<T>(v48_), static_cast<T>(v49_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray49 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n            const T25 v25_;\n            const T26 v26_;\n            const T27 v27_;\n            const T28 v28_;\n            const T29 v29_;\n            const T30 v30_;\n            const T31 v31_;\n            const T32 v32_;\n            const T33 v33_;\n            const T34 v34_;\n            const T35 v35_;\n            const T36 v36_;\n            const T37 v37_;\n            const T38 v38_;\n            const T39 v39_;\n            const T40 v40_;\n            const T41 v41_;\n            const T42 v42_;\n            const T43 v43_;\n            const T44 v44_;\n            const T45 v45_;\n            const T46 v46_;\n            const T47 v47_;\n            const T48 v48_;\n            const T49 v49_;\n        };\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10,\n                  typename T11, typename T12, typename T13, typename T14, typename T15,\n                  typename T16, typename T17, typename T18, typename T19, typename T20,\n                  typename T21, typename T22, typename T23, typename T24, typename T25,\n                  typename T26, typename T27, typename T28, typename T29, typename T30,\n                  typename T31, typename T32, typename T33, typename T34, typename T35,\n                  typename T36, typename T37, typename T38, typename T39, typename T40,\n                  typename T41, typename T42, typename T43, typename T44, typename T45,\n                  typename T46, typename T47, typename T48, typename T49, typename T50>\n        class ValueArray50\n        {\n        public:\n            ValueArray50(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31, T32 v32, T33 v33,\n                         T34 v34, T35 v35, T36 v36, T37 v37, T38 v38, T39 v39, T40 v40, T41 v41,\n                         T42 v42, T43 v43, T44 v44, T45 v45, T46 v46, T47 v47, T48 v48, T49 v49,\n                         T50 v50) : v1_(v1), v2_(v2), v3_(v3), v4_(v4), v5_(v5), v6_(v6), v7_(v7),\n                v8_(v8), v9_(v9), v10_(v10), v11_(v11), v12_(v12), v13_(v13), v14_(v14),\n                v15_(v15), v16_(v16), v17_(v17), v18_(v18), v19_(v19), v20_(v20),\n                v21_(v21), v22_(v22), v23_(v23), v24_(v24), v25_(v25), v26_(v26),\n                v27_(v27), v28_(v28), v29_(v29), v30_(v30), v31_(v31), v32_(v32),\n                v33_(v33), v34_(v34), v35_(v35), v36_(v36), v37_(v37), v38_(v38),\n                v39_(v39), v40_(v40), v41_(v41), v42_(v42), v43_(v43), v44_(v44),\n                v45_(v45), v46_(v46), v47_(v47), v48_(v48), v49_(v49), v50_(v50) {}\n\n            template <typename T>\n            operator ParamGenerator<T>() const\n            {\n                const T array[] = {static_cast<T>(v1_), static_cast<T>(v2_),\n                                   static_cast<T>(v3_), static_cast<T>(v4_), static_cast<T>(v5_),\n                                   static_cast<T>(v6_), static_cast<T>(v7_), static_cast<T>(v8_),\n                                   static_cast<T>(v9_), static_cast<T>(v10_), static_cast<T>(v11_),\n                                   static_cast<T>(v12_), static_cast<T>(v13_), static_cast<T>(v14_),\n                                   static_cast<T>(v15_), static_cast<T>(v16_), static_cast<T>(v17_),\n                                   static_cast<T>(v18_), static_cast<T>(v19_), static_cast<T>(v20_),\n                                   static_cast<T>(v21_), static_cast<T>(v22_), static_cast<T>(v23_),\n                                   static_cast<T>(v24_), static_cast<T>(v25_), static_cast<T>(v26_),\n                                   static_cast<T>(v27_), static_cast<T>(v28_), static_cast<T>(v29_),\n                                   static_cast<T>(v30_), static_cast<T>(v31_), static_cast<T>(v32_),\n                                   static_cast<T>(v33_), static_cast<T>(v34_), static_cast<T>(v35_),\n                                   static_cast<T>(v36_), static_cast<T>(v37_), static_cast<T>(v38_),\n                                   static_cast<T>(v39_), static_cast<T>(v40_), static_cast<T>(v41_),\n                                   static_cast<T>(v42_), static_cast<T>(v43_), static_cast<T>(v44_),\n                                   static_cast<T>(v45_), static_cast<T>(v46_), static_cast<T>(v47_),\n                                   static_cast<T>(v48_), static_cast<T>(v49_), static_cast<T>(v50_)\n                                  };\n                return ValuesIn(array);\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const ValueArray50 &other);\n\n            const T1 v1_;\n            const T2 v2_;\n            const T3 v3_;\n            const T4 v4_;\n            const T5 v5_;\n            const T6 v6_;\n            const T7 v7_;\n            const T8 v8_;\n            const T9 v9_;\n            const T10 v10_;\n            const T11 v11_;\n            const T12 v12_;\n            const T13 v13_;\n            const T14 v14_;\n            const T15 v15_;\n            const T16 v16_;\n            const T17 v17_;\n            const T18 v18_;\n            const T19 v19_;\n            const T20 v20_;\n            const T21 v21_;\n            const T22 v22_;\n            const T23 v23_;\n            const T24 v24_;\n            const T25 v25_;\n            const T26 v26_;\n            const T27 v27_;\n            const T28 v28_;\n            const T29 v29_;\n            const T30 v30_;\n            const T31 v31_;\n            const T32 v32_;\n            const T33 v33_;\n            const T34 v34_;\n            const T35 v35_;\n            const T36 v36_;\n            const T37 v37_;\n            const T38 v38_;\n            const T39 v39_;\n            const T40 v40_;\n            const T41 v41_;\n            const T42 v42_;\n            const T43 v43_;\n            const T44 v44_;\n            const T45 v45_;\n            const T46 v46_;\n            const T47 v47_;\n            const T48 v48_;\n            const T49 v49_;\n            const T50 v50_;\n        };\n\n# if GTEST_HAS_COMBINE\n// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.\n//\n// Generates values from the Cartesian product of values produced\n// by the argument generators.\n//\n        template <typename T1, typename T2>\n        class CartesianProductGenerator2\n            : public ParamGeneratorInterface< ::testing::tuple<T1, T2> >\n        {\n        public:\n            typedef ::testing::tuple<T1, T2> ParamType;\n\n            CartesianProductGenerator2(const ParamGenerator<T1> &g1,\n                                       const ParamGenerator<T2> &g2)\n                : g1_(g1), g2_(g2) {}\n            virtual ~CartesianProductGenerator2() {}\n\n            virtual ParamIteratorInterface<ParamType> *Begin() const\n            {\n                return new Iterator(this, g1_, g1_.begin(), g2_, g2_.begin());\n            }\n            virtual ParamIteratorInterface<ParamType> *End() const\n            {\n                return new Iterator(this, g1_, g1_.end(), g2_, g2_.end());\n            }\n\n        private:\n            class Iterator : public ParamIteratorInterface<ParamType>\n            {\n            public:\n                Iterator(const ParamGeneratorInterface<ParamType> *base,\n                         const ParamGenerator<T1> &g1,\n                         const typename ParamGenerator<T1>::iterator &current1,\n                         const ParamGenerator<T2> &g2,\n                         const typename ParamGenerator<T2>::iterator &current2)\n                    : base_(base),\n                      begin1_(g1.begin()), end1_(g1.end()), current1_(current1),\n                      begin2_(g2.begin()), end2_(g2.end()), current2_(current2)\n                {\n                    ComputeCurrentValue();\n                }\n                virtual ~Iterator() {}\n\n                virtual const ParamGeneratorInterface<ParamType> *BaseGenerator() const\n                {\n                    return base_;\n                }\n                // Advance should not be called on beyond-of-range iterators\n                // so no component iterators must be beyond end of range, either.\n                virtual void Advance()\n                {\n                    assert(!AtEnd());\n                    ++current2_;\n                    if (current2_ == end2_) {\n                        current2_ = begin2_;\n                        ++current1_;\n                    }\n                    ComputeCurrentValue();\n                }\n                virtual ParamIteratorInterface<ParamType> *Clone() const\n                {\n                    return new Iterator(*this);\n                }\n                virtual const ParamType *Current() const\n                {\n                    return &current_value_;\n                }\n                virtual bool Equals(const ParamIteratorInterface<ParamType> &other) const\n                {\n                    // Having the same base generator guarantees that the other\n                    // iterator is of the same type and we can downcast.\n                    GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())\n                            << \"The program attempted to compare iterators \"\n                            << \"from different generators.\" << std::endl;\n                    const Iterator *typed_other =\n                        CheckedDowncastToActualType<const Iterator>(&other);\n                    // We must report iterators equal if they both point beyond their\n                    // respective ranges. That can happen in a variety of fashions,\n                    // so we have to consult AtEnd().\n                    return (AtEnd() && typed_other->AtEnd()) ||\n                           (\n                               current1_ == typed_other->current1_ &&\n                               current2_ == typed_other->current2_);\n                }\n\n            private:\n                Iterator(const Iterator &other)\n                    : base_(other.base_),\n                      begin1_(other.begin1_),\n                      end1_(other.end1_),\n                      current1_(other.current1_),\n                      begin2_(other.begin2_),\n                      end2_(other.end2_),\n                      current2_(other.current2_)\n                {\n                    ComputeCurrentValue();\n                }\n\n                void ComputeCurrentValue()\n                {\n                    if (!AtEnd())\n                        current_value_ = ParamType(*current1_, *current2_);\n                }\n                bool AtEnd() const\n                {\n                    // We must report iterator past the end of the range when either of the\n                    // component iterators has reached the end of its range.\n                    return\n                        current1_ == end1_ ||\n                        current2_ == end2_;\n                }\n\n                // No implementation - assignment is unsupported.\n                void operator=(const Iterator &other);\n\n                const ParamGeneratorInterface<ParamType> *const base_;\n                // begin[i]_ and end[i]_ define the i-th range that Iterator traverses.\n                // current[i]_ is the actual traversing iterator.\n                const typename ParamGenerator<T1>::iterator begin1_;\n                const typename ParamGenerator<T1>::iterator end1_;\n                typename ParamGenerator<T1>::iterator current1_;\n                const typename ParamGenerator<T2>::iterator begin2_;\n                const typename ParamGenerator<T2>::iterator end2_;\n                typename ParamGenerator<T2>::iterator current2_;\n                ParamType current_value_;\n            };  // class CartesianProductGenerator2::Iterator\n\n            // No implementation - assignment is unsupported.\n            void operator=(const CartesianProductGenerator2 &other);\n\n            const ParamGenerator<T1> g1_;\n            const ParamGenerator<T2> g2_;\n        };  // class CartesianProductGenerator2\n\n\n        template <typename T1, typename T2, typename T3>\n        class CartesianProductGenerator3\n            : public ParamGeneratorInterface< ::testing::tuple<T1, T2, T3> >\n        {\n        public:\n            typedef ::testing::tuple<T1, T2, T3> ParamType;\n\n            CartesianProductGenerator3(const ParamGenerator<T1> &g1,\n                                       const ParamGenerator<T2> &g2, const ParamGenerator<T3> &g3)\n                : g1_(g1), g2_(g2), g3_(g3) {}\n            virtual ~CartesianProductGenerator3() {}\n\n            virtual ParamIteratorInterface<ParamType> *Begin() const\n            {\n                return new Iterator(this, g1_, g1_.begin(), g2_, g2_.begin(), g3_,\n                                    g3_.begin());\n            }\n            virtual ParamIteratorInterface<ParamType> *End() const\n            {\n                return new Iterator(this, g1_, g1_.end(), g2_, g2_.end(), g3_, g3_.end());\n            }\n\n        private:\n            class Iterator : public ParamIteratorInterface<ParamType>\n            {\n            public:\n                Iterator(const ParamGeneratorInterface<ParamType> *base,\n                         const ParamGenerator<T1> &g1,\n                         const typename ParamGenerator<T1>::iterator &current1,\n                         const ParamGenerator<T2> &g2,\n                         const typename ParamGenerator<T2>::iterator &current2,\n                         const ParamGenerator<T3> &g3,\n                         const typename ParamGenerator<T3>::iterator &current3)\n                    : base_(base),\n                      begin1_(g1.begin()), end1_(g1.end()), current1_(current1),\n                      begin2_(g2.begin()), end2_(g2.end()), current2_(current2),\n                      begin3_(g3.begin()), end3_(g3.end()), current3_(current3)\n                {\n                    ComputeCurrentValue();\n                }\n                virtual ~Iterator() {}\n\n                virtual const ParamGeneratorInterface<ParamType> *BaseGenerator() const\n                {\n                    return base_;\n                }\n                // Advance should not be called on beyond-of-range iterators\n                // so no component iterators must be beyond end of range, either.\n                virtual void Advance()\n                {\n                    assert(!AtEnd());\n                    ++current3_;\n                    if (current3_ == end3_) {\n                        current3_ = begin3_;\n                        ++current2_;\n                    }\n                    if (current2_ == end2_) {\n                        current2_ = begin2_;\n                        ++current1_;\n                    }\n                    ComputeCurrentValue();\n                }\n                virtual ParamIteratorInterface<ParamType> *Clone() const\n                {\n                    return new Iterator(*this);\n                }\n                virtual const ParamType *Current() const\n                {\n                    return &current_value_;\n                }\n                virtual bool Equals(const ParamIteratorInterface<ParamType> &other) const\n                {\n                    // Having the same base generator guarantees that the other\n                    // iterator is of the same type and we can downcast.\n                    GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())\n                            << \"The program attempted to compare iterators \"\n                            << \"from different generators.\" << std::endl;\n                    const Iterator *typed_other =\n                        CheckedDowncastToActualType<const Iterator>(&other);\n                    // We must report iterators equal if they both point beyond their\n                    // respective ranges. That can happen in a variety of fashions,\n                    // so we have to consult AtEnd().\n                    return (AtEnd() && typed_other->AtEnd()) ||\n                           (\n                               current1_ == typed_other->current1_ &&\n                               current2_ == typed_other->current2_ &&\n                               current3_ == typed_other->current3_);\n                }\n\n            private:\n                Iterator(const Iterator &other)\n                    : base_(other.base_),\n                      begin1_(other.begin1_),\n                      end1_(other.end1_),\n                      current1_(other.current1_),\n                      begin2_(other.begin2_),\n                      end2_(other.end2_),\n                      current2_(other.current2_),\n                      begin3_(other.begin3_),\n                      end3_(other.end3_),\n                      current3_(other.current3_)\n                {\n                    ComputeCurrentValue();\n                }\n\n                void ComputeCurrentValue()\n                {\n                    if (!AtEnd())\n                        current_value_ = ParamType(*current1_, *current2_, *current3_);\n                }\n                bool AtEnd() const\n                {\n                    // We must report iterator past the end of the range when either of the\n                    // component iterators has reached the end of its range.\n                    return\n                        current1_ == end1_ ||\n                        current2_ == end2_ ||\n                        current3_ == end3_;\n                }\n\n                // No implementation - assignment is unsupported.\n                void operator=(const Iterator &other);\n\n                const ParamGeneratorInterface<ParamType> *const base_;\n                // begin[i]_ and end[i]_ define the i-th range that Iterator traverses.\n                // current[i]_ is the actual traversing iterator.\n                const typename ParamGenerator<T1>::iterator begin1_;\n                const typename ParamGenerator<T1>::iterator end1_;\n                typename ParamGenerator<T1>::iterator current1_;\n                const typename ParamGenerator<T2>::iterator begin2_;\n                const typename ParamGenerator<T2>::iterator end2_;\n                typename ParamGenerator<T2>::iterator current2_;\n                const typename ParamGenerator<T3>::iterator begin3_;\n                const typename ParamGenerator<T3>::iterator end3_;\n                typename ParamGenerator<T3>::iterator current3_;\n                ParamType current_value_;\n            };  // class CartesianProductGenerator3::Iterator\n\n            // No implementation - assignment is unsupported.\n            void operator=(const CartesianProductGenerator3 &other);\n\n            const ParamGenerator<T1> g1_;\n            const ParamGenerator<T2> g2_;\n            const ParamGenerator<T3> g3_;\n        };  // class CartesianProductGenerator3\n\n\n        template <typename T1, typename T2, typename T3, typename T4>\n        class CartesianProductGenerator4\n            : public ParamGeneratorInterface< ::testing::tuple<T1, T2, T3, T4> >\n        {\n        public:\n            typedef ::testing::tuple<T1, T2, T3, T4> ParamType;\n\n            CartesianProductGenerator4(const ParamGenerator<T1> &g1,\n                                       const ParamGenerator<T2> &g2, const ParamGenerator<T3> &g3,\n                                       const ParamGenerator<T4> &g4)\n                : g1_(g1), g2_(g2), g3_(g3), g4_(g4) {}\n            virtual ~CartesianProductGenerator4() {}\n\n            virtual ParamIteratorInterface<ParamType> *Begin() const\n            {\n                return new Iterator(this, g1_, g1_.begin(), g2_, g2_.begin(), g3_,\n                                    g3_.begin(), g4_, g4_.begin());\n            }\n            virtual ParamIteratorInterface<ParamType> *End() const\n            {\n                return new Iterator(this, g1_, g1_.end(), g2_, g2_.end(), g3_, g3_.end(),\n                                    g4_, g4_.end());\n            }\n\n        private:\n            class Iterator : public ParamIteratorInterface<ParamType>\n            {\n            public:\n                Iterator(const ParamGeneratorInterface<ParamType> *base,\n                         const ParamGenerator<T1> &g1,\n                         const typename ParamGenerator<T1>::iterator &current1,\n                         const ParamGenerator<T2> &g2,\n                         const typename ParamGenerator<T2>::iterator &current2,\n                         const ParamGenerator<T3> &g3,\n                         const typename ParamGenerator<T3>::iterator &current3,\n                         const ParamGenerator<T4> &g4,\n                         const typename ParamGenerator<T4>::iterator &current4)\n                    : base_(base),\n                      begin1_(g1.begin()), end1_(g1.end()), current1_(current1),\n                      begin2_(g2.begin()), end2_(g2.end()), current2_(current2),\n                      begin3_(g3.begin()), end3_(g3.end()), current3_(current3),\n                      begin4_(g4.begin()), end4_(g4.end()), current4_(current4)\n                {\n                    ComputeCurrentValue();\n                }\n                virtual ~Iterator() {}\n\n                virtual const ParamGeneratorInterface<ParamType> *BaseGenerator() const\n                {\n                    return base_;\n                }\n                // Advance should not be called on beyond-of-range iterators\n                // so no component iterators must be beyond end of range, either.\n                virtual void Advance()\n                {\n                    assert(!AtEnd());\n                    ++current4_;\n                    if (current4_ == end4_) {\n                        current4_ = begin4_;\n                        ++current3_;\n                    }\n                    if (current3_ == end3_) {\n                        current3_ = begin3_;\n                        ++current2_;\n                    }\n                    if (current2_ == end2_) {\n                        current2_ = begin2_;\n                        ++current1_;\n                    }\n                    ComputeCurrentValue();\n                }\n                virtual ParamIteratorInterface<ParamType> *Clone() const\n                {\n                    return new Iterator(*this);\n                }\n                virtual const ParamType *Current() const\n                {\n                    return &current_value_;\n                }\n                virtual bool Equals(const ParamIteratorInterface<ParamType> &other) const\n                {\n                    // Having the same base generator guarantees that the other\n                    // iterator is of the same type and we can downcast.\n                    GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())\n                            << \"The program attempted to compare iterators \"\n                            << \"from different generators.\" << std::endl;\n                    const Iterator *typed_other =\n                        CheckedDowncastToActualType<const Iterator>(&other);\n                    // We must report iterators equal if they both point beyond their\n                    // respective ranges. That can happen in a variety of fashions,\n                    // so we have to consult AtEnd().\n                    return (AtEnd() && typed_other->AtEnd()) ||\n                           (\n                               current1_ == typed_other->current1_ &&\n                               current2_ == typed_other->current2_ &&\n                               current3_ == typed_other->current3_ &&\n                               current4_ == typed_other->current4_);\n                }\n\n            private:\n                Iterator(const Iterator &other)\n                    : base_(other.base_),\n                      begin1_(other.begin1_),\n                      end1_(other.end1_),\n                      current1_(other.current1_),\n                      begin2_(other.begin2_),\n                      end2_(other.end2_),\n                      current2_(other.current2_),\n                      begin3_(other.begin3_),\n                      end3_(other.end3_),\n                      current3_(other.current3_),\n                      begin4_(other.begin4_),\n                      end4_(other.end4_),\n                      current4_(other.current4_)\n                {\n                    ComputeCurrentValue();\n                }\n\n                void ComputeCurrentValue()\n                {\n                    if (!AtEnd())\n                        current_value_ = ParamType(*current1_, *current2_, *current3_,\n                                                   *current4_);\n                }\n                bool AtEnd() const\n                {\n                    // We must report iterator past the end of the range when either of the\n                    // component iterators has reached the end of its range.\n                    return\n                        current1_ == end1_ ||\n                        current2_ == end2_ ||\n                        current3_ == end3_ ||\n                        current4_ == end4_;\n                }\n\n                // No implementation - assignment is unsupported.\n                void operator=(const Iterator &other);\n\n                const ParamGeneratorInterface<ParamType> *const base_;\n                // begin[i]_ and end[i]_ define the i-th range that Iterator traverses.\n                // current[i]_ is the actual traversing iterator.\n                const typename ParamGenerator<T1>::iterator begin1_;\n                const typename ParamGenerator<T1>::iterator end1_;\n                typename ParamGenerator<T1>::iterator current1_;\n                const typename ParamGenerator<T2>::iterator begin2_;\n                const typename ParamGenerator<T2>::iterator end2_;\n                typename ParamGenerator<T2>::iterator current2_;\n                const typename ParamGenerator<T3>::iterator begin3_;\n                const typename ParamGenerator<T3>::iterator end3_;\n                typename ParamGenerator<T3>::iterator current3_;\n                const typename ParamGenerator<T4>::iterator begin4_;\n                const typename ParamGenerator<T4>::iterator end4_;\n                typename ParamGenerator<T4>::iterator current4_;\n                ParamType current_value_;\n            };  // class CartesianProductGenerator4::Iterator\n\n            // No implementation - assignment is unsupported.\n            void operator=(const CartesianProductGenerator4 &other);\n\n            const ParamGenerator<T1> g1_;\n            const ParamGenerator<T2> g2_;\n            const ParamGenerator<T3> g3_;\n            const ParamGenerator<T4> g4_;\n        };  // class CartesianProductGenerator4\n\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5>\n        class CartesianProductGenerator5\n            : public ParamGeneratorInterface< ::testing::tuple<T1, T2, T3, T4, T5> >\n        {\n        public:\n            typedef ::testing::tuple<T1, T2, T3, T4, T5> ParamType;\n\n            CartesianProductGenerator5(const ParamGenerator<T1> &g1,\n                                       const ParamGenerator<T2> &g2, const ParamGenerator<T3> &g3,\n                                       const ParamGenerator<T4> &g4, const ParamGenerator<T5> &g5)\n                : g1_(g1), g2_(g2), g3_(g3), g4_(g4), g5_(g5) {}\n            virtual ~CartesianProductGenerator5() {}\n\n            virtual ParamIteratorInterface<ParamType> *Begin() const\n            {\n                return new Iterator(this, g1_, g1_.begin(), g2_, g2_.begin(), g3_,\n                                    g3_.begin(), g4_, g4_.begin(), g5_, g5_.begin());\n            }\n            virtual ParamIteratorInterface<ParamType> *End() const\n            {\n                return new Iterator(this, g1_, g1_.end(), g2_, g2_.end(), g3_, g3_.end(),\n                                    g4_, g4_.end(), g5_, g5_.end());\n            }\n\n        private:\n            class Iterator : public ParamIteratorInterface<ParamType>\n            {\n            public:\n                Iterator(const ParamGeneratorInterface<ParamType> *base,\n                         const ParamGenerator<T1> &g1,\n                         const typename ParamGenerator<T1>::iterator &current1,\n                         const ParamGenerator<T2> &g2,\n                         const typename ParamGenerator<T2>::iterator &current2,\n                         const ParamGenerator<T3> &g3,\n                         const typename ParamGenerator<T3>::iterator &current3,\n                         const ParamGenerator<T4> &g4,\n                         const typename ParamGenerator<T4>::iterator &current4,\n                         const ParamGenerator<T5> &g5,\n                         const typename ParamGenerator<T5>::iterator &current5)\n                    : base_(base),\n                      begin1_(g1.begin()), end1_(g1.end()), current1_(current1),\n                      begin2_(g2.begin()), end2_(g2.end()), current2_(current2),\n                      begin3_(g3.begin()), end3_(g3.end()), current3_(current3),\n                      begin4_(g4.begin()), end4_(g4.end()), current4_(current4),\n                      begin5_(g5.begin()), end5_(g5.end()), current5_(current5)\n                {\n                    ComputeCurrentValue();\n                }\n                virtual ~Iterator() {}\n\n                virtual const ParamGeneratorInterface<ParamType> *BaseGenerator() const\n                {\n                    return base_;\n                }\n                // Advance should not be called on beyond-of-range iterators\n                // so no component iterators must be beyond end of range, either.\n                virtual void Advance()\n                {\n                    assert(!AtEnd());\n                    ++current5_;\n                    if (current5_ == end5_) {\n                        current5_ = begin5_;\n                        ++current4_;\n                    }\n                    if (current4_ == end4_) {\n                        current4_ = begin4_;\n                        ++current3_;\n                    }\n                    if (current3_ == end3_) {\n                        current3_ = begin3_;\n                        ++current2_;\n                    }\n                    if (current2_ == end2_) {\n                        current2_ = begin2_;\n                        ++current1_;\n                    }\n                    ComputeCurrentValue();\n                }\n                virtual ParamIteratorInterface<ParamType> *Clone() const\n                {\n                    return new Iterator(*this);\n                }\n                virtual const ParamType *Current() const\n                {\n                    return &current_value_;\n                }\n                virtual bool Equals(const ParamIteratorInterface<ParamType> &other) const\n                {\n                    // Having the same base generator guarantees that the other\n                    // iterator is of the same type and we can downcast.\n                    GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())\n                            << \"The program attempted to compare iterators \"\n                            << \"from different generators.\" << std::endl;\n                    const Iterator *typed_other =\n                        CheckedDowncastToActualType<const Iterator>(&other);\n                    // We must report iterators equal if they both point beyond their\n                    // respective ranges. That can happen in a variety of fashions,\n                    // so we have to consult AtEnd().\n                    return (AtEnd() && typed_other->AtEnd()) ||\n                           (\n                               current1_ == typed_other->current1_ &&\n                               current2_ == typed_other->current2_ &&\n                               current3_ == typed_other->current3_ &&\n                               current4_ == typed_other->current4_ &&\n                               current5_ == typed_other->current5_);\n                }\n\n            private:\n                Iterator(const Iterator &other)\n                    : base_(other.base_),\n                      begin1_(other.begin1_),\n                      end1_(other.end1_),\n                      current1_(other.current1_),\n                      begin2_(other.begin2_),\n                      end2_(other.end2_),\n                      current2_(other.current2_),\n                      begin3_(other.begin3_),\n                      end3_(other.end3_),\n                      current3_(other.current3_),\n                      begin4_(other.begin4_),\n                      end4_(other.end4_),\n                      current4_(other.current4_),\n                      begin5_(other.begin5_),\n                      end5_(other.end5_),\n                      current5_(other.current5_)\n                {\n                    ComputeCurrentValue();\n                }\n\n                void ComputeCurrentValue()\n                {\n                    if (!AtEnd())\n                        current_value_ = ParamType(*current1_, *current2_, *current3_,\n                                                   *current4_, *current5_);\n                }\n                bool AtEnd() const\n                {\n                    // We must report iterator past the end of the range when either of the\n                    // component iterators has reached the end of its range.\n                    return\n                        current1_ == end1_ ||\n                        current2_ == end2_ ||\n                        current3_ == end3_ ||\n                        current4_ == end4_ ||\n                        current5_ == end5_;\n                }\n\n                // No implementation - assignment is unsupported.\n                void operator=(const Iterator &other);\n\n                const ParamGeneratorInterface<ParamType> *const base_;\n                // begin[i]_ and end[i]_ define the i-th range that Iterator traverses.\n                // current[i]_ is the actual traversing iterator.\n                const typename ParamGenerator<T1>::iterator begin1_;\n                const typename ParamGenerator<T1>::iterator end1_;\n                typename ParamGenerator<T1>::iterator current1_;\n                const typename ParamGenerator<T2>::iterator begin2_;\n                const typename ParamGenerator<T2>::iterator end2_;\n                typename ParamGenerator<T2>::iterator current2_;\n                const typename ParamGenerator<T3>::iterator begin3_;\n                const typename ParamGenerator<T3>::iterator end3_;\n                typename ParamGenerator<T3>::iterator current3_;\n                const typename ParamGenerator<T4>::iterator begin4_;\n                const typename ParamGenerator<T4>::iterator end4_;\n                typename ParamGenerator<T4>::iterator current4_;\n                const typename ParamGenerator<T5>::iterator begin5_;\n                const typename ParamGenerator<T5>::iterator end5_;\n                typename ParamGenerator<T5>::iterator current5_;\n                ParamType current_value_;\n            };  // class CartesianProductGenerator5::Iterator\n\n            // No implementation - assignment is unsupported.\n            void operator=(const CartesianProductGenerator5 &other);\n\n            const ParamGenerator<T1> g1_;\n            const ParamGenerator<T2> g2_;\n            const ParamGenerator<T3> g3_;\n            const ParamGenerator<T4> g4_;\n            const ParamGenerator<T5> g5_;\n        };  // class CartesianProductGenerator5\n\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6>\n        class CartesianProductGenerator6\n            : public ParamGeneratorInterface< ::testing::tuple<T1, T2, T3, T4, T5,\n              T6> >\n        {\n        public:\n            typedef ::testing::tuple<T1, T2, T3, T4, T5, T6> ParamType;\n\n            CartesianProductGenerator6(const ParamGenerator<T1> &g1,\n                                       const ParamGenerator<T2> &g2, const ParamGenerator<T3> &g3,\n                                       const ParamGenerator<T4> &g4, const ParamGenerator<T5> &g5,\n                                       const ParamGenerator<T6> &g6)\n                : g1_(g1), g2_(g2), g3_(g3), g4_(g4), g5_(g5), g6_(g6) {}\n            virtual ~CartesianProductGenerator6() {}\n\n            virtual ParamIteratorInterface<ParamType> *Begin() const\n            {\n                return new Iterator(this, g1_, g1_.begin(), g2_, g2_.begin(), g3_,\n                                    g3_.begin(), g4_, g4_.begin(), g5_, g5_.begin(), g6_, g6_.begin());\n            }\n            virtual ParamIteratorInterface<ParamType> *End() const\n            {\n                return new Iterator(this, g1_, g1_.end(), g2_, g2_.end(), g3_, g3_.end(),\n                                    g4_, g4_.end(), g5_, g5_.end(), g6_, g6_.end());\n            }\n\n        private:\n            class Iterator : public ParamIteratorInterface<ParamType>\n            {\n            public:\n                Iterator(const ParamGeneratorInterface<ParamType> *base,\n                         const ParamGenerator<T1> &g1,\n                         const typename ParamGenerator<T1>::iterator &current1,\n                         const ParamGenerator<T2> &g2,\n                         const typename ParamGenerator<T2>::iterator &current2,\n                         const ParamGenerator<T3> &g3,\n                         const typename ParamGenerator<T3>::iterator &current3,\n                         const ParamGenerator<T4> &g4,\n                         const typename ParamGenerator<T4>::iterator &current4,\n                         const ParamGenerator<T5> &g5,\n                         const typename ParamGenerator<T5>::iterator &current5,\n                         const ParamGenerator<T6> &g6,\n                         const typename ParamGenerator<T6>::iterator &current6)\n                    : base_(base),\n                      begin1_(g1.begin()), end1_(g1.end()), current1_(current1),\n                      begin2_(g2.begin()), end2_(g2.end()), current2_(current2),\n                      begin3_(g3.begin()), end3_(g3.end()), current3_(current3),\n                      begin4_(g4.begin()), end4_(g4.end()), current4_(current4),\n                      begin5_(g5.begin()), end5_(g5.end()), current5_(current5),\n                      begin6_(g6.begin()), end6_(g6.end()), current6_(current6)\n                {\n                    ComputeCurrentValue();\n                }\n                virtual ~Iterator() {}\n\n                virtual const ParamGeneratorInterface<ParamType> *BaseGenerator() const\n                {\n                    return base_;\n                }\n                // Advance should not be called on beyond-of-range iterators\n                // so no component iterators must be beyond end of range, either.\n                virtual void Advance()\n                {\n                    assert(!AtEnd());\n                    ++current6_;\n                    if (current6_ == end6_) {\n                        current6_ = begin6_;\n                        ++current5_;\n                    }\n                    if (current5_ == end5_) {\n                        current5_ = begin5_;\n                        ++current4_;\n                    }\n                    if (current4_ == end4_) {\n                        current4_ = begin4_;\n                        ++current3_;\n                    }\n                    if (current3_ == end3_) {\n                        current3_ = begin3_;\n                        ++current2_;\n                    }\n                    if (current2_ == end2_) {\n                        current2_ = begin2_;\n                        ++current1_;\n                    }\n                    ComputeCurrentValue();\n                }\n                virtual ParamIteratorInterface<ParamType> *Clone() const\n                {\n                    return new Iterator(*this);\n                }\n                virtual const ParamType *Current() const\n                {\n                    return &current_value_;\n                }\n                virtual bool Equals(const ParamIteratorInterface<ParamType> &other) const\n                {\n                    // Having the same base generator guarantees that the other\n                    // iterator is of the same type and we can downcast.\n                    GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())\n                            << \"The program attempted to compare iterators \"\n                            << \"from different generators.\" << std::endl;\n                    const Iterator *typed_other =\n                        CheckedDowncastToActualType<const Iterator>(&other);\n                    // We must report iterators equal if they both point beyond their\n                    // respective ranges. That can happen in a variety of fashions,\n                    // so we have to consult AtEnd().\n                    return (AtEnd() && typed_other->AtEnd()) ||\n                           (\n                               current1_ == typed_other->current1_ &&\n                               current2_ == typed_other->current2_ &&\n                               current3_ == typed_other->current3_ &&\n                               current4_ == typed_other->current4_ &&\n                               current5_ == typed_other->current5_ &&\n                               current6_ == typed_other->current6_);\n                }\n\n            private:\n                Iterator(const Iterator &other)\n                    : base_(other.base_),\n                      begin1_(other.begin1_),\n                      end1_(other.end1_),\n                      current1_(other.current1_),\n                      begin2_(other.begin2_),\n                      end2_(other.end2_),\n                      current2_(other.current2_),\n                      begin3_(other.begin3_),\n                      end3_(other.end3_),\n                      current3_(other.current3_),\n                      begin4_(other.begin4_),\n                      end4_(other.end4_),\n                      current4_(other.current4_),\n                      begin5_(other.begin5_),\n                      end5_(other.end5_),\n                      current5_(other.current5_),\n                      begin6_(other.begin6_),\n                      end6_(other.end6_),\n                      current6_(other.current6_)\n                {\n                    ComputeCurrentValue();\n                }\n\n                void ComputeCurrentValue()\n                {\n                    if (!AtEnd())\n                        current_value_ = ParamType(*current1_, *current2_, *current3_,\n                                                   *current4_, *current5_, *current6_);\n                }\n                bool AtEnd() const\n                {\n                    // We must report iterator past the end of the range when either of the\n                    // component iterators has reached the end of its range.\n                    return\n                        current1_ == end1_ ||\n                        current2_ == end2_ ||\n                        current3_ == end3_ ||\n                        current4_ == end4_ ||\n                        current5_ == end5_ ||\n                        current6_ == end6_;\n                }\n\n                // No implementation - assignment is unsupported.\n                void operator=(const Iterator &other);\n\n                const ParamGeneratorInterface<ParamType> *const base_;\n                // begin[i]_ and end[i]_ define the i-th range that Iterator traverses.\n                // current[i]_ is the actual traversing iterator.\n                const typename ParamGenerator<T1>::iterator begin1_;\n                const typename ParamGenerator<T1>::iterator end1_;\n                typename ParamGenerator<T1>::iterator current1_;\n                const typename ParamGenerator<T2>::iterator begin2_;\n                const typename ParamGenerator<T2>::iterator end2_;\n                typename ParamGenerator<T2>::iterator current2_;\n                const typename ParamGenerator<T3>::iterator begin3_;\n                const typename ParamGenerator<T3>::iterator end3_;\n                typename ParamGenerator<T3>::iterator current3_;\n                const typename ParamGenerator<T4>::iterator begin4_;\n                const typename ParamGenerator<T4>::iterator end4_;\n                typename ParamGenerator<T4>::iterator current4_;\n                const typename ParamGenerator<T5>::iterator begin5_;\n                const typename ParamGenerator<T5>::iterator end5_;\n                typename ParamGenerator<T5>::iterator current5_;\n                const typename ParamGenerator<T6>::iterator begin6_;\n                const typename ParamGenerator<T6>::iterator end6_;\n                typename ParamGenerator<T6>::iterator current6_;\n                ParamType current_value_;\n            };  // class CartesianProductGenerator6::Iterator\n\n            // No implementation - assignment is unsupported.\n            void operator=(const CartesianProductGenerator6 &other);\n\n            const ParamGenerator<T1> g1_;\n            const ParamGenerator<T2> g2_;\n            const ParamGenerator<T3> g3_;\n            const ParamGenerator<T4> g4_;\n            const ParamGenerator<T5> g5_;\n            const ParamGenerator<T6> g6_;\n        };  // class CartesianProductGenerator6\n\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7>\n        class CartesianProductGenerator7\n            : public ParamGeneratorInterface< ::testing::tuple<T1, T2, T3, T4, T5, T6,\n              T7> >\n        {\n        public:\n            typedef ::testing::tuple<T1, T2, T3, T4, T5, T6, T7> ParamType;\n\n            CartesianProductGenerator7(const ParamGenerator<T1> &g1,\n                                       const ParamGenerator<T2> &g2, const ParamGenerator<T3> &g3,\n                                       const ParamGenerator<T4> &g4, const ParamGenerator<T5> &g5,\n                                       const ParamGenerator<T6> &g6, const ParamGenerator<T7> &g7)\n                : g1_(g1), g2_(g2), g3_(g3), g4_(g4), g5_(g5), g6_(g6), g7_(g7) {}\n            virtual ~CartesianProductGenerator7() {}\n\n            virtual ParamIteratorInterface<ParamType> *Begin() const\n            {\n                return new Iterator(this, g1_, g1_.begin(), g2_, g2_.begin(), g3_,\n                                    g3_.begin(), g4_, g4_.begin(), g5_, g5_.begin(), g6_, g6_.begin(), g7_,\n                                    g7_.begin());\n            }\n            virtual ParamIteratorInterface<ParamType> *End() const\n            {\n                return new Iterator(this, g1_, g1_.end(), g2_, g2_.end(), g3_, g3_.end(),\n                                    g4_, g4_.end(), g5_, g5_.end(), g6_, g6_.end(), g7_, g7_.end());\n            }\n\n        private:\n            class Iterator : public ParamIteratorInterface<ParamType>\n            {\n            public:\n                Iterator(const ParamGeneratorInterface<ParamType> *base,\n                         const ParamGenerator<T1> &g1,\n                         const typename ParamGenerator<T1>::iterator &current1,\n                         const ParamGenerator<T2> &g2,\n                         const typename ParamGenerator<T2>::iterator &current2,\n                         const ParamGenerator<T3> &g3,\n                         const typename ParamGenerator<T3>::iterator &current3,\n                         const ParamGenerator<T4> &g4,\n                         const typename ParamGenerator<T4>::iterator &current4,\n                         const ParamGenerator<T5> &g5,\n                         const typename ParamGenerator<T5>::iterator &current5,\n                         const ParamGenerator<T6> &g6,\n                         const typename ParamGenerator<T6>::iterator &current6,\n                         const ParamGenerator<T7> &g7,\n                         const typename ParamGenerator<T7>::iterator &current7)\n                    : base_(base),\n                      begin1_(g1.begin()), end1_(g1.end()), current1_(current1),\n                      begin2_(g2.begin()), end2_(g2.end()), current2_(current2),\n                      begin3_(g3.begin()), end3_(g3.end()), current3_(current3),\n                      begin4_(g4.begin()), end4_(g4.end()), current4_(current4),\n                      begin5_(g5.begin()), end5_(g5.end()), current5_(current5),\n                      begin6_(g6.begin()), end6_(g6.end()), current6_(current6),\n                      begin7_(g7.begin()), end7_(g7.end()), current7_(current7)\n                {\n                    ComputeCurrentValue();\n                }\n                virtual ~Iterator() {}\n\n                virtual const ParamGeneratorInterface<ParamType> *BaseGenerator() const\n                {\n                    return base_;\n                }\n                // Advance should not be called on beyond-of-range iterators\n                // so no component iterators must be beyond end of range, either.\n                virtual void Advance()\n                {\n                    assert(!AtEnd());\n                    ++current7_;\n                    if (current7_ == end7_) {\n                        current7_ = begin7_;\n                        ++current6_;\n                    }\n                    if (current6_ == end6_) {\n                        current6_ = begin6_;\n                        ++current5_;\n                    }\n                    if (current5_ == end5_) {\n                        current5_ = begin5_;\n                        ++current4_;\n                    }\n                    if (current4_ == end4_) {\n                        current4_ = begin4_;\n                        ++current3_;\n                    }\n                    if (current3_ == end3_) {\n                        current3_ = begin3_;\n                        ++current2_;\n                    }\n                    if (current2_ == end2_) {\n                        current2_ = begin2_;\n                        ++current1_;\n                    }\n                    ComputeCurrentValue();\n                }\n                virtual ParamIteratorInterface<ParamType> *Clone() const\n                {\n                    return new Iterator(*this);\n                }\n                virtual const ParamType *Current() const\n                {\n                    return &current_value_;\n                }\n                virtual bool Equals(const ParamIteratorInterface<ParamType> &other) const\n                {\n                    // Having the same base generator guarantees that the other\n                    // iterator is of the same type and we can downcast.\n                    GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())\n                            << \"The program attempted to compare iterators \"\n                            << \"from different generators.\" << std::endl;\n                    const Iterator *typed_other =\n                        CheckedDowncastToActualType<const Iterator>(&other);\n                    // We must report iterators equal if they both point beyond their\n                    // respective ranges. That can happen in a variety of fashions,\n                    // so we have to consult AtEnd().\n                    return (AtEnd() && typed_other->AtEnd()) ||\n                           (\n                               current1_ == typed_other->current1_ &&\n                               current2_ == typed_other->current2_ &&\n                               current3_ == typed_other->current3_ &&\n                               current4_ == typed_other->current4_ &&\n                               current5_ == typed_other->current5_ &&\n                               current6_ == typed_other->current6_ &&\n                               current7_ == typed_other->current7_);\n                }\n\n            private:\n                Iterator(const Iterator &other)\n                    : base_(other.base_),\n                      begin1_(other.begin1_),\n                      end1_(other.end1_),\n                      current1_(other.current1_),\n                      begin2_(other.begin2_),\n                      end2_(other.end2_),\n                      current2_(other.current2_),\n                      begin3_(other.begin3_),\n                      end3_(other.end3_),\n                      current3_(other.current3_),\n                      begin4_(other.begin4_),\n                      end4_(other.end4_),\n                      current4_(other.current4_),\n                      begin5_(other.begin5_),\n                      end5_(other.end5_),\n                      current5_(other.current5_),\n                      begin6_(other.begin6_),\n                      end6_(other.end6_),\n                      current6_(other.current6_),\n                      begin7_(other.begin7_),\n                      end7_(other.end7_),\n                      current7_(other.current7_)\n                {\n                    ComputeCurrentValue();\n                }\n\n                void ComputeCurrentValue()\n                {\n                    if (!AtEnd())\n                        current_value_ = ParamType(*current1_, *current2_, *current3_,\n                                                   *current4_, *current5_, *current6_, *current7_);\n                }\n                bool AtEnd() const\n                {\n                    // We must report iterator past the end of the range when either of the\n                    // component iterators has reached the end of its range.\n                    return\n                        current1_ == end1_ ||\n                        current2_ == end2_ ||\n                        current3_ == end3_ ||\n                        current4_ == end4_ ||\n                        current5_ == end5_ ||\n                        current6_ == end6_ ||\n                        current7_ == end7_;\n                }\n\n                // No implementation - assignment is unsupported.\n                void operator=(const Iterator &other);\n\n                const ParamGeneratorInterface<ParamType> *const base_;\n                // begin[i]_ and end[i]_ define the i-th range that Iterator traverses.\n                // current[i]_ is the actual traversing iterator.\n                const typename ParamGenerator<T1>::iterator begin1_;\n                const typename ParamGenerator<T1>::iterator end1_;\n                typename ParamGenerator<T1>::iterator current1_;\n                const typename ParamGenerator<T2>::iterator begin2_;\n                const typename ParamGenerator<T2>::iterator end2_;\n                typename ParamGenerator<T2>::iterator current2_;\n                const typename ParamGenerator<T3>::iterator begin3_;\n                const typename ParamGenerator<T3>::iterator end3_;\n                typename ParamGenerator<T3>::iterator current3_;\n                const typename ParamGenerator<T4>::iterator begin4_;\n                const typename ParamGenerator<T4>::iterator end4_;\n                typename ParamGenerator<T4>::iterator current4_;\n                const typename ParamGenerator<T5>::iterator begin5_;\n                const typename ParamGenerator<T5>::iterator end5_;\n                typename ParamGenerator<T5>::iterator current5_;\n                const typename ParamGenerator<T6>::iterator begin6_;\n                const typename ParamGenerator<T6>::iterator end6_;\n                typename ParamGenerator<T6>::iterator current6_;\n                const typename ParamGenerator<T7>::iterator begin7_;\n                const typename ParamGenerator<T7>::iterator end7_;\n                typename ParamGenerator<T7>::iterator current7_;\n                ParamType current_value_;\n            };  // class CartesianProductGenerator7::Iterator\n\n            // No implementation - assignment is unsupported.\n            void operator=(const CartesianProductGenerator7 &other);\n\n            const ParamGenerator<T1> g1_;\n            const ParamGenerator<T2> g2_;\n            const ParamGenerator<T3> g3_;\n            const ParamGenerator<T4> g4_;\n            const ParamGenerator<T5> g5_;\n            const ParamGenerator<T6> g6_;\n            const ParamGenerator<T7> g7_;\n        };  // class CartesianProductGenerator7\n\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8>\n        class CartesianProductGenerator8\n            : public ParamGeneratorInterface< ::testing::tuple<T1, T2, T3, T4, T5, T6,\n              T7, T8> >\n        {\n        public:\n            typedef ::testing::tuple<T1, T2, T3, T4, T5, T6, T7, T8> ParamType;\n\n            CartesianProductGenerator8(const ParamGenerator<T1> &g1,\n                                       const ParamGenerator<T2> &g2, const ParamGenerator<T3> &g3,\n                                       const ParamGenerator<T4> &g4, const ParamGenerator<T5> &g5,\n                                       const ParamGenerator<T6> &g6, const ParamGenerator<T7> &g7,\n                                       const ParamGenerator<T8> &g8)\n                : g1_(g1), g2_(g2), g3_(g3), g4_(g4), g5_(g5), g6_(g6), g7_(g7),\n                  g8_(g8) {}\n            virtual ~CartesianProductGenerator8() {}\n\n            virtual ParamIteratorInterface<ParamType> *Begin() const\n            {\n                return new Iterator(this, g1_, g1_.begin(), g2_, g2_.begin(), g3_,\n                                    g3_.begin(), g4_, g4_.begin(), g5_, g5_.begin(), g6_, g6_.begin(), g7_,\n                                    g7_.begin(), g8_, g8_.begin());\n            }\n            virtual ParamIteratorInterface<ParamType> *End() const\n            {\n                return new Iterator(this, g1_, g1_.end(), g2_, g2_.end(), g3_, g3_.end(),\n                                    g4_, g4_.end(), g5_, g5_.end(), g6_, g6_.end(), g7_, g7_.end(), g8_,\n                                    g8_.end());\n            }\n\n        private:\n            class Iterator : public ParamIteratorInterface<ParamType>\n            {\n            public:\n                Iterator(const ParamGeneratorInterface<ParamType> *base,\n                         const ParamGenerator<T1> &g1,\n                         const typename ParamGenerator<T1>::iterator &current1,\n                         const ParamGenerator<T2> &g2,\n                         const typename ParamGenerator<T2>::iterator &current2,\n                         const ParamGenerator<T3> &g3,\n                         const typename ParamGenerator<T3>::iterator &current3,\n                         const ParamGenerator<T4> &g4,\n                         const typename ParamGenerator<T4>::iterator &current4,\n                         const ParamGenerator<T5> &g5,\n                         const typename ParamGenerator<T5>::iterator &current5,\n                         const ParamGenerator<T6> &g6,\n                         const typename ParamGenerator<T6>::iterator &current6,\n                         const ParamGenerator<T7> &g7,\n                         const typename ParamGenerator<T7>::iterator &current7,\n                         const ParamGenerator<T8> &g8,\n                         const typename ParamGenerator<T8>::iterator &current8)\n                    : base_(base),\n                      begin1_(g1.begin()), end1_(g1.end()), current1_(current1),\n                      begin2_(g2.begin()), end2_(g2.end()), current2_(current2),\n                      begin3_(g3.begin()), end3_(g3.end()), current3_(current3),\n                      begin4_(g4.begin()), end4_(g4.end()), current4_(current4),\n                      begin5_(g5.begin()), end5_(g5.end()), current5_(current5),\n                      begin6_(g6.begin()), end6_(g6.end()), current6_(current6),\n                      begin7_(g7.begin()), end7_(g7.end()), current7_(current7),\n                      begin8_(g8.begin()), end8_(g8.end()), current8_(current8)\n                {\n                    ComputeCurrentValue();\n                }\n                virtual ~Iterator() {}\n\n                virtual const ParamGeneratorInterface<ParamType> *BaseGenerator() const\n                {\n                    return base_;\n                }\n                // Advance should not be called on beyond-of-range iterators\n                // so no component iterators must be beyond end of range, either.\n                virtual void Advance()\n                {\n                    assert(!AtEnd());\n                    ++current8_;\n                    if (current8_ == end8_) {\n                        current8_ = begin8_;\n                        ++current7_;\n                    }\n                    if (current7_ == end7_) {\n                        current7_ = begin7_;\n                        ++current6_;\n                    }\n                    if (current6_ == end6_) {\n                        current6_ = begin6_;\n                        ++current5_;\n                    }\n                    if (current5_ == end5_) {\n                        current5_ = begin5_;\n                        ++current4_;\n                    }\n                    if (current4_ == end4_) {\n                        current4_ = begin4_;\n                        ++current3_;\n                    }\n                    if (current3_ == end3_) {\n                        current3_ = begin3_;\n                        ++current2_;\n                    }\n                    if (current2_ == end2_) {\n                        current2_ = begin2_;\n                        ++current1_;\n                    }\n                    ComputeCurrentValue();\n                }\n                virtual ParamIteratorInterface<ParamType> *Clone() const\n                {\n                    return new Iterator(*this);\n                }\n                virtual const ParamType *Current() const\n                {\n                    return &current_value_;\n                }\n                virtual bool Equals(const ParamIteratorInterface<ParamType> &other) const\n                {\n                    // Having the same base generator guarantees that the other\n                    // iterator is of the same type and we can downcast.\n                    GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())\n                            << \"The program attempted to compare iterators \"\n                            << \"from different generators.\" << std::endl;\n                    const Iterator *typed_other =\n                        CheckedDowncastToActualType<const Iterator>(&other);\n                    // We must report iterators equal if they both point beyond their\n                    // respective ranges. That can happen in a variety of fashions,\n                    // so we have to consult AtEnd().\n                    return (AtEnd() && typed_other->AtEnd()) ||\n                           (\n                               current1_ == typed_other->current1_ &&\n                               current2_ == typed_other->current2_ &&\n                               current3_ == typed_other->current3_ &&\n                               current4_ == typed_other->current4_ &&\n                               current5_ == typed_other->current5_ &&\n                               current6_ == typed_other->current6_ &&\n                               current7_ == typed_other->current7_ &&\n                               current8_ == typed_other->current8_);\n                }\n\n            private:\n                Iterator(const Iterator &other)\n                    : base_(other.base_),\n                      begin1_(other.begin1_),\n                      end1_(other.end1_),\n                      current1_(other.current1_),\n                      begin2_(other.begin2_),\n                      end2_(other.end2_),\n                      current2_(other.current2_),\n                      begin3_(other.begin3_),\n                      end3_(other.end3_),\n                      current3_(other.current3_),\n                      begin4_(other.begin4_),\n                      end4_(other.end4_),\n                      current4_(other.current4_),\n                      begin5_(other.begin5_),\n                      end5_(other.end5_),\n                      current5_(other.current5_),\n                      begin6_(other.begin6_),\n                      end6_(other.end6_),\n                      current6_(other.current6_),\n                      begin7_(other.begin7_),\n                      end7_(other.end7_),\n                      current7_(other.current7_),\n                      begin8_(other.begin8_),\n                      end8_(other.end8_),\n                      current8_(other.current8_)\n                {\n                    ComputeCurrentValue();\n                }\n\n                void ComputeCurrentValue()\n                {\n                    if (!AtEnd())\n                        current_value_ = ParamType(*current1_, *current2_, *current3_,\n                                                   *current4_, *current5_, *current6_, *current7_, *current8_);\n                }\n                bool AtEnd() const\n                {\n                    // We must report iterator past the end of the range when either of the\n                    // component iterators has reached the end of its range.\n                    return\n                        current1_ == end1_ ||\n                        current2_ == end2_ ||\n                        current3_ == end3_ ||\n                        current4_ == end4_ ||\n                        current5_ == end5_ ||\n                        current6_ == end6_ ||\n                        current7_ == end7_ ||\n                        current8_ == end8_;\n                }\n\n                // No implementation - assignment is unsupported.\n                void operator=(const Iterator &other);\n\n                const ParamGeneratorInterface<ParamType> *const base_;\n                // begin[i]_ and end[i]_ define the i-th range that Iterator traverses.\n                // current[i]_ is the actual traversing iterator.\n                const typename ParamGenerator<T1>::iterator begin1_;\n                const typename ParamGenerator<T1>::iterator end1_;\n                typename ParamGenerator<T1>::iterator current1_;\n                const typename ParamGenerator<T2>::iterator begin2_;\n                const typename ParamGenerator<T2>::iterator end2_;\n                typename ParamGenerator<T2>::iterator current2_;\n                const typename ParamGenerator<T3>::iterator begin3_;\n                const typename ParamGenerator<T3>::iterator end3_;\n                typename ParamGenerator<T3>::iterator current3_;\n                const typename ParamGenerator<T4>::iterator begin4_;\n                const typename ParamGenerator<T4>::iterator end4_;\n                typename ParamGenerator<T4>::iterator current4_;\n                const typename ParamGenerator<T5>::iterator begin5_;\n                const typename ParamGenerator<T5>::iterator end5_;\n                typename ParamGenerator<T5>::iterator current5_;\n                const typename ParamGenerator<T6>::iterator begin6_;\n                const typename ParamGenerator<T6>::iterator end6_;\n                typename ParamGenerator<T6>::iterator current6_;\n                const typename ParamGenerator<T7>::iterator begin7_;\n                const typename ParamGenerator<T7>::iterator end7_;\n                typename ParamGenerator<T7>::iterator current7_;\n                const typename ParamGenerator<T8>::iterator begin8_;\n                const typename ParamGenerator<T8>::iterator end8_;\n                typename ParamGenerator<T8>::iterator current8_;\n                ParamType current_value_;\n            };  // class CartesianProductGenerator8::Iterator\n\n            // No implementation - assignment is unsupported.\n            void operator=(const CartesianProductGenerator8 &other);\n\n            const ParamGenerator<T1> g1_;\n            const ParamGenerator<T2> g2_;\n            const ParamGenerator<T3> g3_;\n            const ParamGenerator<T4> g4_;\n            const ParamGenerator<T5> g5_;\n            const ParamGenerator<T6> g6_;\n            const ParamGenerator<T7> g7_;\n            const ParamGenerator<T8> g8_;\n        };  // class CartesianProductGenerator8\n\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9>\n        class CartesianProductGenerator9\n            : public ParamGeneratorInterface< ::testing::tuple<T1, T2, T3, T4, T5, T6,\n              T7, T8, T9> >\n        {\n        public:\n            typedef ::testing::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9> ParamType;\n\n            CartesianProductGenerator9(const ParamGenerator<T1> &g1,\n                                       const ParamGenerator<T2> &g2, const ParamGenerator<T3> &g3,\n                                       const ParamGenerator<T4> &g4, const ParamGenerator<T5> &g5,\n                                       const ParamGenerator<T6> &g6, const ParamGenerator<T7> &g7,\n                                       const ParamGenerator<T8> &g8, const ParamGenerator<T9> &g9)\n                : g1_(g1), g2_(g2), g3_(g3), g4_(g4), g5_(g5), g6_(g6), g7_(g7), g8_(g8),\n                  g9_(g9) {}\n            virtual ~CartesianProductGenerator9() {}\n\n            virtual ParamIteratorInterface<ParamType> *Begin() const\n            {\n                return new Iterator(this, g1_, g1_.begin(), g2_, g2_.begin(), g3_,\n                                    g3_.begin(), g4_, g4_.begin(), g5_, g5_.begin(), g6_, g6_.begin(), g7_,\n                                    g7_.begin(), g8_, g8_.begin(), g9_, g9_.begin());\n            }\n            virtual ParamIteratorInterface<ParamType> *End() const\n            {\n                return new Iterator(this, g1_, g1_.end(), g2_, g2_.end(), g3_, g3_.end(),\n                                    g4_, g4_.end(), g5_, g5_.end(), g6_, g6_.end(), g7_, g7_.end(), g8_,\n                                    g8_.end(), g9_, g9_.end());\n            }\n\n        private:\n            class Iterator : public ParamIteratorInterface<ParamType>\n            {\n            public:\n                Iterator(const ParamGeneratorInterface<ParamType> *base,\n                         const ParamGenerator<T1> &g1,\n                         const typename ParamGenerator<T1>::iterator &current1,\n                         const ParamGenerator<T2> &g2,\n                         const typename ParamGenerator<T2>::iterator &current2,\n                         const ParamGenerator<T3> &g3,\n                         const typename ParamGenerator<T3>::iterator &current3,\n                         const ParamGenerator<T4> &g4,\n                         const typename ParamGenerator<T4>::iterator &current4,\n                         const ParamGenerator<T5> &g5,\n                         const typename ParamGenerator<T5>::iterator &current5,\n                         const ParamGenerator<T6> &g6,\n                         const typename ParamGenerator<T6>::iterator &current6,\n                         const ParamGenerator<T7> &g7,\n                         const typename ParamGenerator<T7>::iterator &current7,\n                         const ParamGenerator<T8> &g8,\n                         const typename ParamGenerator<T8>::iterator &current8,\n                         const ParamGenerator<T9> &g9,\n                         const typename ParamGenerator<T9>::iterator &current9)\n                    : base_(base),\n                      begin1_(g1.begin()), end1_(g1.end()), current1_(current1),\n                      begin2_(g2.begin()), end2_(g2.end()), current2_(current2),\n                      begin3_(g3.begin()), end3_(g3.end()), current3_(current3),\n                      begin4_(g4.begin()), end4_(g4.end()), current4_(current4),\n                      begin5_(g5.begin()), end5_(g5.end()), current5_(current5),\n                      begin6_(g6.begin()), end6_(g6.end()), current6_(current6),\n                      begin7_(g7.begin()), end7_(g7.end()), current7_(current7),\n                      begin8_(g8.begin()), end8_(g8.end()), current8_(current8),\n                      begin9_(g9.begin()), end9_(g9.end()), current9_(current9)\n                {\n                    ComputeCurrentValue();\n                }\n                virtual ~Iterator() {}\n\n                virtual const ParamGeneratorInterface<ParamType> *BaseGenerator() const\n                {\n                    return base_;\n                }\n                // Advance should not be called on beyond-of-range iterators\n                // so no component iterators must be beyond end of range, either.\n                virtual void Advance()\n                {\n                    assert(!AtEnd());\n                    ++current9_;\n                    if (current9_ == end9_) {\n                        current9_ = begin9_;\n                        ++current8_;\n                    }\n                    if (current8_ == end8_) {\n                        current8_ = begin8_;\n                        ++current7_;\n                    }\n                    if (current7_ == end7_) {\n                        current7_ = begin7_;\n                        ++current6_;\n                    }\n                    if (current6_ == end6_) {\n                        current6_ = begin6_;\n                        ++current5_;\n                    }\n                    if (current5_ == end5_) {\n                        current5_ = begin5_;\n                        ++current4_;\n                    }\n                    if (current4_ == end4_) {\n                        current4_ = begin4_;\n                        ++current3_;\n                    }\n                    if (current3_ == end3_) {\n                        current3_ = begin3_;\n                        ++current2_;\n                    }\n                    if (current2_ == end2_) {\n                        current2_ = begin2_;\n                        ++current1_;\n                    }\n                    ComputeCurrentValue();\n                }\n                virtual ParamIteratorInterface<ParamType> *Clone() const\n                {\n                    return new Iterator(*this);\n                }\n                virtual const ParamType *Current() const\n                {\n                    return &current_value_;\n                }\n                virtual bool Equals(const ParamIteratorInterface<ParamType> &other) const\n                {\n                    // Having the same base generator guarantees that the other\n                    // iterator is of the same type and we can downcast.\n                    GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())\n                            << \"The program attempted to compare iterators \"\n                            << \"from different generators.\" << std::endl;\n                    const Iterator *typed_other =\n                        CheckedDowncastToActualType<const Iterator>(&other);\n                    // We must report iterators equal if they both point beyond their\n                    // respective ranges. That can happen in a variety of fashions,\n                    // so we have to consult AtEnd().\n                    return (AtEnd() && typed_other->AtEnd()) ||\n                           (\n                               current1_ == typed_other->current1_ &&\n                               current2_ == typed_other->current2_ &&\n                               current3_ == typed_other->current3_ &&\n                               current4_ == typed_other->current4_ &&\n                               current5_ == typed_other->current5_ &&\n                               current6_ == typed_other->current6_ &&\n                               current7_ == typed_other->current7_ &&\n                               current8_ == typed_other->current8_ &&\n                               current9_ == typed_other->current9_);\n                }\n\n            private:\n                Iterator(const Iterator &other)\n                    : base_(other.base_),\n                      begin1_(other.begin1_),\n                      end1_(other.end1_),\n                      current1_(other.current1_),\n                      begin2_(other.begin2_),\n                      end2_(other.end2_),\n                      current2_(other.current2_),\n                      begin3_(other.begin3_),\n                      end3_(other.end3_),\n                      current3_(other.current3_),\n                      begin4_(other.begin4_),\n                      end4_(other.end4_),\n                      current4_(other.current4_),\n                      begin5_(other.begin5_),\n                      end5_(other.end5_),\n                      current5_(other.current5_),\n                      begin6_(other.begin6_),\n                      end6_(other.end6_),\n                      current6_(other.current6_),\n                      begin7_(other.begin7_),\n                      end7_(other.end7_),\n                      current7_(other.current7_),\n                      begin8_(other.begin8_),\n                      end8_(other.end8_),\n                      current8_(other.current8_),\n                      begin9_(other.begin9_),\n                      end9_(other.end9_),\n                      current9_(other.current9_)\n                {\n                    ComputeCurrentValue();\n                }\n\n                void ComputeCurrentValue()\n                {\n                    if (!AtEnd())\n                        current_value_ = ParamType(*current1_, *current2_, *current3_,\n                                                   *current4_, *current5_, *current6_, *current7_, *current8_,\n                                                   *current9_);\n                }\n                bool AtEnd() const\n                {\n                    // We must report iterator past the end of the range when either of the\n                    // component iterators has reached the end of its range.\n                    return\n                        current1_ == end1_ ||\n                        current2_ == end2_ ||\n                        current3_ == end3_ ||\n                        current4_ == end4_ ||\n                        current5_ == end5_ ||\n                        current6_ == end6_ ||\n                        current7_ == end7_ ||\n                        current8_ == end8_ ||\n                        current9_ == end9_;\n                }\n\n                // No implementation - assignment is unsupported.\n                void operator=(const Iterator &other);\n\n                const ParamGeneratorInterface<ParamType> *const base_;\n                // begin[i]_ and end[i]_ define the i-th range that Iterator traverses.\n                // current[i]_ is the actual traversing iterator.\n                const typename ParamGenerator<T1>::iterator begin1_;\n                const typename ParamGenerator<T1>::iterator end1_;\n                typename ParamGenerator<T1>::iterator current1_;\n                const typename ParamGenerator<T2>::iterator begin2_;\n                const typename ParamGenerator<T2>::iterator end2_;\n                typename ParamGenerator<T2>::iterator current2_;\n                const typename ParamGenerator<T3>::iterator begin3_;\n                const typename ParamGenerator<T3>::iterator end3_;\n                typename ParamGenerator<T3>::iterator current3_;\n                const typename ParamGenerator<T4>::iterator begin4_;\n                const typename ParamGenerator<T4>::iterator end4_;\n                typename ParamGenerator<T4>::iterator current4_;\n                const typename ParamGenerator<T5>::iterator begin5_;\n                const typename ParamGenerator<T5>::iterator end5_;\n                typename ParamGenerator<T5>::iterator current5_;\n                const typename ParamGenerator<T6>::iterator begin6_;\n                const typename ParamGenerator<T6>::iterator end6_;\n                typename ParamGenerator<T6>::iterator current6_;\n                const typename ParamGenerator<T7>::iterator begin7_;\n                const typename ParamGenerator<T7>::iterator end7_;\n                typename ParamGenerator<T7>::iterator current7_;\n                const typename ParamGenerator<T8>::iterator begin8_;\n                const typename ParamGenerator<T8>::iterator end8_;\n                typename ParamGenerator<T8>::iterator current8_;\n                const typename ParamGenerator<T9>::iterator begin9_;\n                const typename ParamGenerator<T9>::iterator end9_;\n                typename ParamGenerator<T9>::iterator current9_;\n                ParamType current_value_;\n            };  // class CartesianProductGenerator9::Iterator\n\n            // No implementation - assignment is unsupported.\n            void operator=(const CartesianProductGenerator9 &other);\n\n            const ParamGenerator<T1> g1_;\n            const ParamGenerator<T2> g2_;\n            const ParamGenerator<T3> g3_;\n            const ParamGenerator<T4> g4_;\n            const ParamGenerator<T5> g5_;\n            const ParamGenerator<T6> g6_;\n            const ParamGenerator<T7> g7_;\n            const ParamGenerator<T8> g8_;\n            const ParamGenerator<T9> g9_;\n        };  // class CartesianProductGenerator9\n\n\n        template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                  typename T6, typename T7, typename T8, typename T9, typename T10>\n        class CartesianProductGenerator10\n            : public ParamGeneratorInterface< ::testing::tuple<T1, T2, T3, T4, T5, T6,\n              T7, T8, T9, T10> >\n        {\n        public:\n            typedef ::testing::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> ParamType;\n\n            CartesianProductGenerator10(const ParamGenerator<T1> &g1,\n                                        const ParamGenerator<T2> &g2, const ParamGenerator<T3> &g3,\n                                        const ParamGenerator<T4> &g4, const ParamGenerator<T5> &g5,\n                                        const ParamGenerator<T6> &g6, const ParamGenerator<T7> &g7,\n                                        const ParamGenerator<T8> &g8, const ParamGenerator<T9> &g9,\n                                        const ParamGenerator<T10> &g10)\n                : g1_(g1), g2_(g2), g3_(g3), g4_(g4), g5_(g5), g6_(g6), g7_(g7), g8_(g8),\n                  g9_(g9), g10_(g10) {}\n            virtual ~CartesianProductGenerator10() {}\n\n            virtual ParamIteratorInterface<ParamType> *Begin() const\n            {\n                return new Iterator(this, g1_, g1_.begin(), g2_, g2_.begin(), g3_,\n                                    g3_.begin(), g4_, g4_.begin(), g5_, g5_.begin(), g6_, g6_.begin(), g7_,\n                                    g7_.begin(), g8_, g8_.begin(), g9_, g9_.begin(), g10_, g10_.begin());\n            }\n            virtual ParamIteratorInterface<ParamType> *End() const\n            {\n                return new Iterator(this, g1_, g1_.end(), g2_, g2_.end(), g3_, g3_.end(),\n                                    g4_, g4_.end(), g5_, g5_.end(), g6_, g6_.end(), g7_, g7_.end(), g8_,\n                                    g8_.end(), g9_, g9_.end(), g10_, g10_.end());\n            }\n\n        private:\n            class Iterator : public ParamIteratorInterface<ParamType>\n            {\n            public:\n                Iterator(const ParamGeneratorInterface<ParamType> *base,\n                         const ParamGenerator<T1> &g1,\n                         const typename ParamGenerator<T1>::iterator &current1,\n                         const ParamGenerator<T2> &g2,\n                         const typename ParamGenerator<T2>::iterator &current2,\n                         const ParamGenerator<T3> &g3,\n                         const typename ParamGenerator<T3>::iterator &current3,\n                         const ParamGenerator<T4> &g4,\n                         const typename ParamGenerator<T4>::iterator &current4,\n                         const ParamGenerator<T5> &g5,\n                         const typename ParamGenerator<T5>::iterator &current5,\n                         const ParamGenerator<T6> &g6,\n                         const typename ParamGenerator<T6>::iterator &current6,\n                         const ParamGenerator<T7> &g7,\n                         const typename ParamGenerator<T7>::iterator &current7,\n                         const ParamGenerator<T8> &g8,\n                         const typename ParamGenerator<T8>::iterator &current8,\n                         const ParamGenerator<T9> &g9,\n                         const typename ParamGenerator<T9>::iterator &current9,\n                         const ParamGenerator<T10> &g10,\n                         const typename ParamGenerator<T10>::iterator &current10)\n                    : base_(base),\n                      begin1_(g1.begin()), end1_(g1.end()), current1_(current1),\n                      begin2_(g2.begin()), end2_(g2.end()), current2_(current2),\n                      begin3_(g3.begin()), end3_(g3.end()), current3_(current3),\n                      begin4_(g4.begin()), end4_(g4.end()), current4_(current4),\n                      begin5_(g5.begin()), end5_(g5.end()), current5_(current5),\n                      begin6_(g6.begin()), end6_(g6.end()), current6_(current6),\n                      begin7_(g7.begin()), end7_(g7.end()), current7_(current7),\n                      begin8_(g8.begin()), end8_(g8.end()), current8_(current8),\n                      begin9_(g9.begin()), end9_(g9.end()), current9_(current9),\n                      begin10_(g10.begin()), end10_(g10.end()), current10_(current10)\n                {\n                    ComputeCurrentValue();\n                }\n                virtual ~Iterator() {}\n\n                virtual const ParamGeneratorInterface<ParamType> *BaseGenerator() const\n                {\n                    return base_;\n                }\n                // Advance should not be called on beyond-of-range iterators\n                // so no component iterators must be beyond end of range, either.\n                virtual void Advance()\n                {\n                    assert(!AtEnd());\n                    ++current10_;\n                    if (current10_ == end10_) {\n                        current10_ = begin10_;\n                        ++current9_;\n                    }\n                    if (current9_ == end9_) {\n                        current9_ = begin9_;\n                        ++current8_;\n                    }\n                    if (current8_ == end8_) {\n                        current8_ = begin8_;\n                        ++current7_;\n                    }\n                    if (current7_ == end7_) {\n                        current7_ = begin7_;\n                        ++current6_;\n                    }\n                    if (current6_ == end6_) {\n                        current6_ = begin6_;\n                        ++current5_;\n                    }\n                    if (current5_ == end5_) {\n                        current5_ = begin5_;\n                        ++current4_;\n                    }\n                    if (current4_ == end4_) {\n                        current4_ = begin4_;\n                        ++current3_;\n                    }\n                    if (current3_ == end3_) {\n                        current3_ = begin3_;\n                        ++current2_;\n                    }\n                    if (current2_ == end2_) {\n                        current2_ = begin2_;\n                        ++current1_;\n                    }\n                    ComputeCurrentValue();\n                }\n                virtual ParamIteratorInterface<ParamType> *Clone() const\n                {\n                    return new Iterator(*this);\n                }\n                virtual const ParamType *Current() const\n                {\n                    return &current_value_;\n                }\n                virtual bool Equals(const ParamIteratorInterface<ParamType> &other) const\n                {\n                    // Having the same base generator guarantees that the other\n                    // iterator is of the same type and we can downcast.\n                    GTEST_CHECK_(BaseGenerator() == other.BaseGenerator())\n                            << \"The program attempted to compare iterators \"\n                            << \"from different generators.\" << std::endl;\n                    const Iterator *typed_other =\n                        CheckedDowncastToActualType<const Iterator>(&other);\n                    // We must report iterators equal if they both point beyond their\n                    // respective ranges. That can happen in a variety of fashions,\n                    // so we have to consult AtEnd().\n                    return (AtEnd() && typed_other->AtEnd()) ||\n                           (\n                               current1_ == typed_other->current1_ &&\n                               current2_ == typed_other->current2_ &&\n                               current3_ == typed_other->current3_ &&\n                               current4_ == typed_other->current4_ &&\n                               current5_ == typed_other->current5_ &&\n                               current6_ == typed_other->current6_ &&\n                               current7_ == typed_other->current7_ &&\n                               current8_ == typed_other->current8_ &&\n                               current9_ == typed_other->current9_ &&\n                               current10_ == typed_other->current10_);\n                }\n\n            private:\n                Iterator(const Iterator &other)\n                    : base_(other.base_),\n                      begin1_(other.begin1_),\n                      end1_(other.end1_),\n                      current1_(other.current1_),\n                      begin2_(other.begin2_),\n                      end2_(other.end2_),\n                      current2_(other.current2_),\n                      begin3_(other.begin3_),\n                      end3_(other.end3_),\n                      current3_(other.current3_),\n                      begin4_(other.begin4_),\n                      end4_(other.end4_),\n                      current4_(other.current4_),\n                      begin5_(other.begin5_),\n                      end5_(other.end5_),\n                      current5_(other.current5_),\n                      begin6_(other.begin6_),\n                      end6_(other.end6_),\n                      current6_(other.current6_),\n                      begin7_(other.begin7_),\n                      end7_(other.end7_),\n                      current7_(other.current7_),\n                      begin8_(other.begin8_),\n                      end8_(other.end8_),\n                      current8_(other.current8_),\n                      begin9_(other.begin9_),\n                      end9_(other.end9_),\n                      current9_(other.current9_),\n                      begin10_(other.begin10_),\n                      end10_(other.end10_),\n                      current10_(other.current10_)\n                {\n                    ComputeCurrentValue();\n                }\n\n                void ComputeCurrentValue()\n                {\n                    if (!AtEnd())\n                        current_value_ = ParamType(*current1_, *current2_, *current3_,\n                                                   *current4_, *current5_, *current6_, *current7_, *current8_,\n                                                   *current9_, *current10_);\n                }\n                bool AtEnd() const\n                {\n                    // We must report iterator past the end of the range when either of the\n                    // component iterators has reached the end of its range.\n                    return\n                        current1_ == end1_ ||\n                        current2_ == end2_ ||\n                        current3_ == end3_ ||\n                        current4_ == end4_ ||\n                        current5_ == end5_ ||\n                        current6_ == end6_ ||\n                        current7_ == end7_ ||\n                        current8_ == end8_ ||\n                        current9_ == end9_ ||\n                        current10_ == end10_;\n                }\n\n                // No implementation - assignment is unsupported.\n                void operator=(const Iterator &other);\n\n                const ParamGeneratorInterface<ParamType> *const base_;\n                // begin[i]_ and end[i]_ define the i-th range that Iterator traverses.\n                // current[i]_ is the actual traversing iterator.\n                const typename ParamGenerator<T1>::iterator begin1_;\n                const typename ParamGenerator<T1>::iterator end1_;\n                typename ParamGenerator<T1>::iterator current1_;\n                const typename ParamGenerator<T2>::iterator begin2_;\n                const typename ParamGenerator<T2>::iterator end2_;\n                typename ParamGenerator<T2>::iterator current2_;\n                const typename ParamGenerator<T3>::iterator begin3_;\n                const typename ParamGenerator<T3>::iterator end3_;\n                typename ParamGenerator<T3>::iterator current3_;\n                const typename ParamGenerator<T4>::iterator begin4_;\n                const typename ParamGenerator<T4>::iterator end4_;\n                typename ParamGenerator<T4>::iterator current4_;\n                const typename ParamGenerator<T5>::iterator begin5_;\n                const typename ParamGenerator<T5>::iterator end5_;\n                typename ParamGenerator<T5>::iterator current5_;\n                const typename ParamGenerator<T6>::iterator begin6_;\n                const typename ParamGenerator<T6>::iterator end6_;\n                typename ParamGenerator<T6>::iterator current6_;\n                const typename ParamGenerator<T7>::iterator begin7_;\n                const typename ParamGenerator<T7>::iterator end7_;\n                typename ParamGenerator<T7>::iterator current7_;\n                const typename ParamGenerator<T8>::iterator begin8_;\n                const typename ParamGenerator<T8>::iterator end8_;\n                typename ParamGenerator<T8>::iterator current8_;\n                const typename ParamGenerator<T9>::iterator begin9_;\n                const typename ParamGenerator<T9>::iterator end9_;\n                typename ParamGenerator<T9>::iterator current9_;\n                const typename ParamGenerator<T10>::iterator begin10_;\n                const typename ParamGenerator<T10>::iterator end10_;\n                typename ParamGenerator<T10>::iterator current10_;\n                ParamType current_value_;\n            };  // class CartesianProductGenerator10::Iterator\n\n            // No implementation - assignment is unsupported.\n            void operator=(const CartesianProductGenerator10 &other);\n\n            const ParamGenerator<T1> g1_;\n            const ParamGenerator<T2> g2_;\n            const ParamGenerator<T3> g3_;\n            const ParamGenerator<T4> g4_;\n            const ParamGenerator<T5> g5_;\n            const ParamGenerator<T6> g6_;\n            const ParamGenerator<T7> g7_;\n            const ParamGenerator<T8> g8_;\n            const ParamGenerator<T9> g9_;\n            const ParamGenerator<T10> g10_;\n        };  // class CartesianProductGenerator10\n\n\n// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.\n//\n// Helper classes providing Combine() with polymorphic features. They allow\n// casting CartesianProductGeneratorN<T> to ParamGenerator<U> if T is\n// convertible to U.\n//\n        template <class Generator1, class Generator2>\n        class CartesianProductHolder2\n        {\n        public:\n            CartesianProductHolder2(const Generator1 &g1, const Generator2 &g2)\n                : g1_(g1), g2_(g2) {}\n            template <typename T1, typename T2>\n            operator ParamGenerator< ::testing::tuple<T1, T2> >() const\n            {\n                return ParamGenerator< ::testing::tuple<T1, T2> >(\n                           new CartesianProductGenerator2<T1, T2>(\n                               static_cast<ParamGenerator<T1> >(g1_),\n                               static_cast<ParamGenerator<T2> >(g2_)));\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const CartesianProductHolder2 &other);\n\n            const Generator1 g1_;\n            const Generator2 g2_;\n        };  // class CartesianProductHolder2\n\n        template <class Generator1, class Generator2, class Generator3>\n        class CartesianProductHolder3\n        {\n        public:\n            CartesianProductHolder3(const Generator1 &g1, const Generator2 &g2,\n                                    const Generator3 &g3)\n                : g1_(g1), g2_(g2), g3_(g3) {}\n            template <typename T1, typename T2, typename T3>\n            operator ParamGenerator< ::testing::tuple<T1, T2, T3> >() const\n            {\n                return ParamGenerator< ::testing::tuple<T1, T2, T3> >(\n                           new CartesianProductGenerator3<T1, T2, T3>(\n                               static_cast<ParamGenerator<T1> >(g1_),\n                               static_cast<ParamGenerator<T2> >(g2_),\n                               static_cast<ParamGenerator<T3> >(g3_)));\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const CartesianProductHolder3 &other);\n\n            const Generator1 g1_;\n            const Generator2 g2_;\n            const Generator3 g3_;\n        };  // class CartesianProductHolder3\n\n        template <class Generator1, class Generator2, class Generator3,\n                  class Generator4>\n        class CartesianProductHolder4\n        {\n        public:\n            CartesianProductHolder4(const Generator1 &g1, const Generator2 &g2,\n                                    const Generator3 &g3, const Generator4 &g4)\n                : g1_(g1), g2_(g2), g3_(g3), g4_(g4) {}\n            template <typename T1, typename T2, typename T3, typename T4>\n            operator ParamGenerator< ::testing::tuple<T1, T2, T3, T4> >() const\n            {\n                return ParamGenerator< ::testing::tuple<T1, T2, T3, T4> >(\n                           new CartesianProductGenerator4<T1, T2, T3, T4>(\n                               static_cast<ParamGenerator<T1> >(g1_),\n                               static_cast<ParamGenerator<T2> >(g2_),\n                               static_cast<ParamGenerator<T3> >(g3_),\n                               static_cast<ParamGenerator<T4> >(g4_)));\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const CartesianProductHolder4 &other);\n\n            const Generator1 g1_;\n            const Generator2 g2_;\n            const Generator3 g3_;\n            const Generator4 g4_;\n        };  // class CartesianProductHolder4\n\n        template <class Generator1, class Generator2, class Generator3,\n                  class Generator4, class Generator5>\n        class CartesianProductHolder5\n        {\n        public:\n            CartesianProductHolder5(const Generator1 &g1, const Generator2 &g2,\n                                    const Generator3 &g3, const Generator4 &g4, const Generator5 &g5)\n                : g1_(g1), g2_(g2), g3_(g3), g4_(g4), g5_(g5) {}\n            template <typename T1, typename T2, typename T3, typename T4, typename T5>\n            operator ParamGenerator< ::testing::tuple<T1, T2, T3, T4, T5> >() const\n            {\n                return ParamGenerator< ::testing::tuple<T1, T2, T3, T4, T5> >(\n                           new CartesianProductGenerator5<T1, T2, T3, T4, T5>(\n                               static_cast<ParamGenerator<T1> >(g1_),\n                               static_cast<ParamGenerator<T2> >(g2_),\n                               static_cast<ParamGenerator<T3> >(g3_),\n                               static_cast<ParamGenerator<T4> >(g4_),\n                               static_cast<ParamGenerator<T5> >(g5_)));\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const CartesianProductHolder5 &other);\n\n            const Generator1 g1_;\n            const Generator2 g2_;\n            const Generator3 g3_;\n            const Generator4 g4_;\n            const Generator5 g5_;\n        };  // class CartesianProductHolder5\n\n        template <class Generator1, class Generator2, class Generator3,\n                  class Generator4, class Generator5, class Generator6>\n        class CartesianProductHolder6\n        {\n        public:\n            CartesianProductHolder6(const Generator1 &g1, const Generator2 &g2,\n                                    const Generator3 &g3, const Generator4 &g4, const Generator5 &g5,\n                                    const Generator6 &g6)\n                : g1_(g1), g2_(g2), g3_(g3), g4_(g4), g5_(g5), g6_(g6) {}\n            template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                      typename T6>\n            operator ParamGenerator< ::testing::tuple<T1, T2, T3, T4, T5, T6> >() const\n            {\n                return ParamGenerator< ::testing::tuple<T1, T2, T3, T4, T5, T6> >(\n                           new CartesianProductGenerator6<T1, T2, T3, T4, T5, T6>(\n                               static_cast<ParamGenerator<T1> >(g1_),\n                               static_cast<ParamGenerator<T2> >(g2_),\n                               static_cast<ParamGenerator<T3> >(g3_),\n                               static_cast<ParamGenerator<T4> >(g4_),\n                               static_cast<ParamGenerator<T5> >(g5_),\n                               static_cast<ParamGenerator<T6> >(g6_)));\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const CartesianProductHolder6 &other);\n\n            const Generator1 g1_;\n            const Generator2 g2_;\n            const Generator3 g3_;\n            const Generator4 g4_;\n            const Generator5 g5_;\n            const Generator6 g6_;\n        };  // class CartesianProductHolder6\n\n        template <class Generator1, class Generator2, class Generator3,\n                  class Generator4, class Generator5, class Generator6, class Generator7>\n        class CartesianProductHolder7\n        {\n        public:\n            CartesianProductHolder7(const Generator1 &g1, const Generator2 &g2,\n                                    const Generator3 &g3, const Generator4 &g4, const Generator5 &g5,\n                                    const Generator6 &g6, const Generator7 &g7)\n                : g1_(g1), g2_(g2), g3_(g3), g4_(g4), g5_(g5), g6_(g6), g7_(g7) {}\n            template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                      typename T6, typename T7>\n            operator ParamGenerator< ::testing::tuple<T1, T2, T3, T4, T5, T6,\n                     T7> >() const\n            {\n                return ParamGenerator< ::testing::tuple<T1, T2, T3, T4, T5, T6, T7> >(\n                           new CartesianProductGenerator7<T1, T2, T3, T4, T5, T6, T7>(\n                               static_cast<ParamGenerator<T1> >(g1_),\n                               static_cast<ParamGenerator<T2> >(g2_),\n                               static_cast<ParamGenerator<T3> >(g3_),\n                               static_cast<ParamGenerator<T4> >(g4_),\n                               static_cast<ParamGenerator<T5> >(g5_),\n                               static_cast<ParamGenerator<T6> >(g6_),\n                               static_cast<ParamGenerator<T7> >(g7_)));\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const CartesianProductHolder7 &other);\n\n            const Generator1 g1_;\n            const Generator2 g2_;\n            const Generator3 g3_;\n            const Generator4 g4_;\n            const Generator5 g5_;\n            const Generator6 g6_;\n            const Generator7 g7_;\n        };  // class CartesianProductHolder7\n\n        template <class Generator1, class Generator2, class Generator3,\n                  class Generator4, class Generator5, class Generator6, class Generator7,\n                  class Generator8>\n        class CartesianProductHolder8\n        {\n        public:\n            CartesianProductHolder8(const Generator1 &g1, const Generator2 &g2,\n                                    const Generator3 &g3, const Generator4 &g4, const Generator5 &g5,\n                                    const Generator6 &g6, const Generator7 &g7, const Generator8 &g8)\n                : g1_(g1), g2_(g2), g3_(g3), g4_(g4), g5_(g5), g6_(g6), g7_(g7),\n                  g8_(g8) {}\n            template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                      typename T6, typename T7, typename T8>\n            operator ParamGenerator< ::testing::tuple<T1, T2, T3, T4, T5, T6, T7,\n                     T8> >() const\n            {\n                return ParamGenerator< ::testing::tuple<T1, T2, T3, T4, T5, T6, T7, T8> >(\n                           new CartesianProductGenerator8<T1, T2, T3, T4, T5, T6, T7, T8>(\n                               static_cast<ParamGenerator<T1> >(g1_),\n                               static_cast<ParamGenerator<T2> >(g2_),\n                               static_cast<ParamGenerator<T3> >(g3_),\n                               static_cast<ParamGenerator<T4> >(g4_),\n                               static_cast<ParamGenerator<T5> >(g5_),\n                               static_cast<ParamGenerator<T6> >(g6_),\n                               static_cast<ParamGenerator<T7> >(g7_),\n                               static_cast<ParamGenerator<T8> >(g8_)));\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const CartesianProductHolder8 &other);\n\n            const Generator1 g1_;\n            const Generator2 g2_;\n            const Generator3 g3_;\n            const Generator4 g4_;\n            const Generator5 g5_;\n            const Generator6 g6_;\n            const Generator7 g7_;\n            const Generator8 g8_;\n        };  // class CartesianProductHolder8\n\n        template <class Generator1, class Generator2, class Generator3,\n                  class Generator4, class Generator5, class Generator6, class Generator7,\n                  class Generator8, class Generator9>\n        class CartesianProductHolder9\n        {\n        public:\n            CartesianProductHolder9(const Generator1 &g1, const Generator2 &g2,\n                                    const Generator3 &g3, const Generator4 &g4, const Generator5 &g5,\n                                    const Generator6 &g6, const Generator7 &g7, const Generator8 &g8,\n                                    const Generator9 &g9)\n                : g1_(g1), g2_(g2), g3_(g3), g4_(g4), g5_(g5), g6_(g6), g7_(g7), g8_(g8),\n                  g9_(g9) {}\n            template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                      typename T6, typename T7, typename T8, typename T9>\n            operator ParamGenerator< ::testing::tuple<T1, T2, T3, T4, T5, T6, T7, T8,\n                     T9> >() const\n            {\n                return ParamGenerator< ::testing::tuple<T1, T2, T3, T4, T5, T6, T7, T8,\n                       T9> >(\n                           new CartesianProductGenerator9<T1, T2, T3, T4, T5, T6, T7, T8, T9>(\n                               static_cast<ParamGenerator<T1> >(g1_),\n                               static_cast<ParamGenerator<T2> >(g2_),\n                               static_cast<ParamGenerator<T3> >(g3_),\n                               static_cast<ParamGenerator<T4> >(g4_),\n                               static_cast<ParamGenerator<T5> >(g5_),\n                               static_cast<ParamGenerator<T6> >(g6_),\n                               static_cast<ParamGenerator<T7> >(g7_),\n                               static_cast<ParamGenerator<T8> >(g8_),\n                               static_cast<ParamGenerator<T9> >(g9_)));\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const CartesianProductHolder9 &other);\n\n            const Generator1 g1_;\n            const Generator2 g2_;\n            const Generator3 g3_;\n            const Generator4 g4_;\n            const Generator5 g5_;\n            const Generator6 g6_;\n            const Generator7 g7_;\n            const Generator8 g8_;\n            const Generator9 g9_;\n        };  // class CartesianProductHolder9\n\n        template <class Generator1, class Generator2, class Generator3,\n                  class Generator4, class Generator5, class Generator6, class Generator7,\n                  class Generator8, class Generator9, class Generator10>\n        class CartesianProductHolder10\n        {\n        public:\n            CartesianProductHolder10(const Generator1 &g1, const Generator2 &g2,\n                                     const Generator3 &g3, const Generator4 &g4, const Generator5 &g5,\n                                     const Generator6 &g6, const Generator7 &g7, const Generator8 &g8,\n                                     const Generator9 &g9, const Generator10 &g10)\n                : g1_(g1), g2_(g2), g3_(g3), g4_(g4), g5_(g5), g6_(g6), g7_(g7), g8_(g8),\n                  g9_(g9), g10_(g10) {}\n            template <typename T1, typename T2, typename T3, typename T4, typename T5,\n                      typename T6, typename T7, typename T8, typename T9, typename T10>\n            operator ParamGenerator< ::testing::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9,\n                     T10> >() const\n            {\n                return ParamGenerator< ::testing::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9,\n                       T10> >(\n                           new CartesianProductGenerator10<T1, T2, T3, T4, T5, T6, T7, T8, T9,\n                           T10>(\n                               static_cast<ParamGenerator<T1> >(g1_),\n                               static_cast<ParamGenerator<T2> >(g2_),\n                               static_cast<ParamGenerator<T3> >(g3_),\n                               static_cast<ParamGenerator<T4> >(g4_),\n                               static_cast<ParamGenerator<T5> >(g5_),\n                               static_cast<ParamGenerator<T6> >(g6_),\n                               static_cast<ParamGenerator<T7> >(g7_),\n                               static_cast<ParamGenerator<T8> >(g8_),\n                               static_cast<ParamGenerator<T9> >(g9_),\n                               static_cast<ParamGenerator<T10> >(g10_)));\n            }\n\n        private:\n            // No implementation - assignment is unsupported.\n            void operator=(const CartesianProductHolder10 &other);\n\n            const Generator1 g1_;\n            const Generator2 g2_;\n            const Generator3 g3_;\n            const Generator4 g4_;\n            const Generator5 g5_;\n            const Generator6 g6_;\n            const Generator7 g7_;\n            const Generator8 g8_;\n            const Generator9 g9_;\n            const Generator10 g10_;\n        };  // class CartesianProductHolder10\n\n# endif  // GTEST_HAS_COMBINE\n\n    }  // namespace internal\n}  // namespace testing\n\n#endif  //  GTEST_HAS_PARAM_TEST\n\n#endif  // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PARAM_UTIL_GENERATED_H_\n\n#if GTEST_HAS_PARAM_TEST\n\nnamespace testing\n{\n\n// Functions producing parameter generators.\n//\n// Google Test uses these generators to produce parameters for value-\n// parameterized tests. When a parameterized test case is instantiated\n// with a particular generator, Google Test creates and runs tests\n// for each element in the sequence produced by the generator.\n//\n// In the following sample, tests from test case FooTest are instantiated\n// each three times with parameter values 3, 5, and 8:\n//\n// class FooTest : public TestWithParam<int> { ... };\n//\n// TEST_P(FooTest, TestThis) {\n// }\n// TEST_P(FooTest, TestThat) {\n// }\n// INSTANTIATE_TEST_CASE_P(TestSequence, FooTest, Values(3, 5, 8));\n//\n\n// Range() returns generators providing sequences of values in a range.\n//\n// Synopsis:\n// Range(start, end)\n//   - returns a generator producing a sequence of values {start, start+1,\n//     start+2, ..., }.\n// Range(start, end, step)\n//   - returns a generator producing a sequence of values {start, start+step,\n//     start+step+step, ..., }.\n// Notes:\n//   * The generated sequences never include end. For example, Range(1, 5)\n//     returns a generator producing a sequence {1, 2, 3, 4}. Range(1, 9, 2)\n//     returns a generator producing {1, 3, 5, 7}.\n//   * start and end must have the same type. That type may be any integral or\n//     floating-point type or a user defined type satisfying these conditions:\n//     * It must be assignable (have operator=() defined).\n//     * It must have operator+() (operator+(int-compatible type) for\n//       two-operand version).\n//     * It must have operator<() defined.\n//     Elements in the resulting sequences will also have that type.\n//   * Condition start < end must be satisfied in order for resulting sequences\n//     to contain any elements.\n//\n    template <typename T, typename IncrementT>\n    internal::ParamGenerator<T> Range(T start, T end, IncrementT step)\n    {\n        return internal::ParamGenerator<T>(\n                   new internal::RangeGenerator<T, IncrementT>(start, end, step));\n    }\n\n    template <typename T>\n    internal::ParamGenerator<T> Range(T start, T end)\n    {\n        return Range(start, end, 1);\n    }\n\n// ValuesIn() function allows generation of tests with parameters coming from\n// a container.\n//\n// Synopsis:\n// ValuesIn(const T (&array)[N])\n//   - returns a generator producing sequences with elements from\n//     a C-style array.\n// ValuesIn(const Container& container)\n//   - returns a generator producing sequences with elements from\n//     an STL-style container.\n// ValuesIn(Iterator begin, Iterator end)\n//   - returns a generator producing sequences with elements from\n//     a range [begin, end) defined by a pair of STL-style iterators. These\n//     iterators can also be plain C pointers.\n//\n// Please note that ValuesIn copies the values from the containers\n// passed in and keeps them to generate tests in RUN_ALL_TESTS().\n//\n// Examples:\n//\n// This instantiates tests from test case StringTest\n// each with C-string values of \"foo\", \"bar\", and \"baz\":\n//\n// const char* strings[] = {\"foo\", \"bar\", \"baz\"};\n// INSTANTIATE_TEST_CASE_P(StringSequence, SrtingTest, ValuesIn(strings));\n//\n// This instantiates tests from test case StlStringTest\n// each with STL strings with values \"a\" and \"b\":\n//\n// ::std::vector< ::std::string> GetParameterStrings() {\n//   ::std::vector< ::std::string> v;\n//   v.push_back(\"a\");\n//   v.push_back(\"b\");\n//   return v;\n// }\n//\n// INSTANTIATE_TEST_CASE_P(CharSequence,\n//                         StlStringTest,\n//                         ValuesIn(GetParameterStrings()));\n//\n//\n// This will also instantiate tests from CharTest\n// each with parameter values 'a' and 'b':\n//\n// ::std::list<char> GetParameterChars() {\n//   ::std::list<char> list;\n//   list.push_back('a');\n//   list.push_back('b');\n//   return list;\n// }\n// ::std::list<char> l = GetParameterChars();\n// INSTANTIATE_TEST_CASE_P(CharSequence2,\n//                         CharTest,\n//                         ValuesIn(l.begin(), l.end()));\n//\n    template <typename ForwardIterator>\n    internal::ParamGenerator<\n    typename ::testing::internal::IteratorTraits<ForwardIterator>::value_type>\n    ValuesIn(ForwardIterator begin, ForwardIterator end)\n    {\n        typedef typename ::testing::internal::IteratorTraits<ForwardIterator>\n        ::value_type ParamType;\n        return internal::ParamGenerator<ParamType>(\n                   new internal::ValuesInIteratorRangeGenerator<ParamType>(begin, end));\n    }\n\n    template <typename T, size_t N>\n    internal::ParamGenerator<T> ValuesIn(const T (&array)[N])\n    {\n        return ValuesIn(array, array + N);\n    }\n\n    template <class Container>\n    internal::ParamGenerator<typename Container::value_type> ValuesIn(\n        const Container &container)\n    {\n        return ValuesIn(container.begin(), container.end());\n    }\n\n// Values() allows generating tests from explicitly specified list of\n// parameters.\n//\n// Synopsis:\n// Values(T v1, T v2, ..., T vN)\n//   - returns a generator producing sequences with elements v1, v2, ..., vN.\n//\n// For example, this instantiates tests from test case BarTest each\n// with values \"one\", \"two\", and \"three\":\n//\n// INSTANTIATE_TEST_CASE_P(NumSequence, BarTest, Values(\"one\", \"two\", \"three\"));\n//\n// This instantiates tests from test case BazTest each with values 1, 2, 3.5.\n// The exact type of values will depend on the type of parameter in BazTest.\n//\n// INSTANTIATE_TEST_CASE_P(FloatingNumbers, BazTest, Values(1, 2, 3.5));\n//\n// Currently, Values() supports from 1 to 50 parameters.\n//\n    template <typename T1>\n    internal::ValueArray1<T1> Values(T1 v1)\n    {\n        return internal::ValueArray1<T1>(v1);\n    }\n\n    template <typename T1, typename T2>\n    internal::ValueArray2<T1, T2> Values(T1 v1, T2 v2)\n    {\n        return internal::ValueArray2<T1, T2>(v1, v2);\n    }\n\n    template <typename T1, typename T2, typename T3>\n    internal::ValueArray3<T1, T2, T3> Values(T1 v1, T2 v2, T3 v3)\n    {\n        return internal::ValueArray3<T1, T2, T3>(v1, v2, v3);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4>\n    internal::ValueArray4<T1, T2, T3, T4> Values(T1 v1, T2 v2, T3 v3, T4 v4)\n    {\n        return internal::ValueArray4<T1, T2, T3, T4>(v1, v2, v3, v4);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5>\n    internal::ValueArray5<T1, T2, T3, T4, T5> Values(T1 v1, T2 v2, T3 v3, T4 v4,\n                                                     T5 v5)\n    {\n        return internal::ValueArray5<T1, T2, T3, T4, T5>(v1, v2, v3, v4, v5);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6>\n    internal::ValueArray6<T1, T2, T3, T4, T5, T6> Values(T1 v1, T2 v2, T3 v3,\n                                                         T4 v4, T5 v5, T6 v6)\n    {\n        return internal::ValueArray6<T1, T2, T3, T4, T5, T6>(v1, v2, v3, v4, v5, v6);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7>\n    internal::ValueArray7<T1, T2, T3, T4, T5, T6, T7> Values(T1 v1, T2 v2, T3 v3,\n                                                             T4 v4, T5 v5, T6 v6, T7 v7)\n    {\n        return internal::ValueArray7<T1, T2, T3, T4, T5, T6, T7>(v1, v2, v3, v4, v5,\n                                                                 v6, v7);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8>\n    internal::ValueArray8<T1, T2, T3, T4, T5, T6, T7, T8> Values(T1 v1, T2 v2,\n                                                                 T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8)\n    {\n        return internal::ValueArray8<T1, T2, T3, T4, T5, T6, T7, T8>(v1, v2, v3, v4,\n                                                                     v5, v6, v7, v8);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9>\n    internal::ValueArray9<T1, T2, T3, T4, T5, T6, T7, T8, T9> Values(T1 v1, T2 v2,\n                                                                     T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9)\n    {\n        return internal::ValueArray9<T1, T2, T3, T4, T5, T6, T7, T8, T9>(v1, v2, v3,\n                                                                         v4, v5, v6, v7, v8, v9);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10>\n    internal::ValueArray10<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> Values(T1 v1,\n                                                                           T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9, T10 v10)\n    {\n        return internal::ValueArray10<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(v1,\n                                                                               v2, v3, v4, v5, v6, v7, v8, v9, v10);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11>\n    internal::ValueArray11<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10,\n             T11> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11)\n    {\n        return internal::ValueArray11<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10,\n               T11>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12>\n    internal::ValueArray12<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n             T12> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12)\n    {\n        return internal::ValueArray12<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13>\n    internal::ValueArray13<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,\n             T13> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13)\n    {\n        return internal::ValueArray13<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14>\n    internal::ValueArray14<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14)\n    {\n        return internal::ValueArray14<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13,\n                              v14);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15>\n    internal::ValueArray15<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8,\n                              T9 v9, T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15)\n    {\n        return internal::ValueArray15<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12,\n                                   v13, v14, v15);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16>\n    internal::ValueArray16<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7,\n                                   T8 v8, T9 v9, T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15,\n                                   T16 v16)\n    {\n        return internal::ValueArray16<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11,\n                                        v12, v13, v14, v15, v16);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17>\n    internal::ValueArray17<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7,\n                                        T8 v8, T9 v9, T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15,\n                                        T16 v16, T17 v17)\n    {\n        return internal::ValueArray17<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10,\n                                             v11, v12, v13, v14, v15, v16, v17);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18>\n    internal::ValueArray18<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6,\n                                             T7 v7, T8 v8, T9 v9, T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15,\n                                             T16 v16, T17 v17, T18 v18)\n    {\n        return internal::ValueArray18<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18>(v1, v2, v3, v4, v5, v6, v7, v8, v9,\n                                                  v10, v11, v12, v13, v14, v15, v16, v17, v18);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19>\n    internal::ValueArray19<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5,\n                                                  T6 v6, T7 v7, T8 v8, T9 v9, T10 v10, T11 v11, T12 v12, T13 v13, T14 v14,\n                                                  T15 v15, T16 v16, T17 v17, T18 v18, T19 v19)\n    {\n        return internal::ValueArray19<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19>(v1, v2, v3, v4, v5, v6, v7, v8,\n                                                       v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20>\n    internal::ValueArray20<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20> Values(T1 v1, T2 v2, T3 v3, T4 v4,\n                                                       T5 v5, T6 v6, T7 v7, T8 v8, T9 v9, T10 v10, T11 v11, T12 v12, T13 v13,\n                                                       T14 v14, T15 v15, T16 v16, T17 v17, T18 v18, T19 v19, T20 v20)\n    {\n        return internal::ValueArray20<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20>(v1, v2, v3, v4, v5, v6, v7,\n                                                            v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21>\n    internal::ValueArray21<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21> Values(T1 v1, T2 v2, T3 v3, T4 v4,\n                                                            T5 v5, T6 v6, T7 v7, T8 v8, T9 v9, T10 v10, T11 v11, T12 v12, T13 v13,\n                                                            T14 v14, T15 v15, T16 v16, T17 v17, T18 v18, T19 v19, T20 v20, T21 v21)\n    {\n        return internal::ValueArray21<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21>(v1, v2, v3, v4, v5, v6,\n                                                                 v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22>\n    internal::ValueArray22<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22> Values(T1 v1, T2 v2, T3 v3,\n                                                                 T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9, T10 v10, T11 v11, T12 v12,\n                                                                 T13 v13, T14 v14, T15 v15, T16 v16, T17 v17, T18 v18, T19 v19, T20 v20,\n                                                                 T21 v21, T22 v22)\n    {\n        return internal::ValueArray22<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22>(v1, v2, v3, v4,\n                                                                      v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19,\n                                                                      v20, v21, v22);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23>\n    internal::ValueArray23<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23> Values(T1 v1, T2 v2,\n                                                                      T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9, T10 v10, T11 v11, T12 v12,\n                                                                      T13 v13, T14 v14, T15 v15, T16 v16, T17 v17, T18 v18, T19 v19, T20 v20,\n                                                                      T21 v21, T22 v22, T23 v23)\n    {\n        return internal::ValueArray23<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23>(v1, v2, v3,\n                                                                           v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19,\n                                                                           v20, v21, v22, v23);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24>\n    internal::ValueArray24<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24> Values(T1 v1, T2 v2,\n                                                                           T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9, T10 v10, T11 v11, T12 v12,\n                                                                           T13 v13, T14 v14, T15 v15, T16 v16, T17 v17, T18 v18, T19 v19, T20 v20,\n                                                                           T21 v21, T22 v22, T23 v23, T24 v24)\n    {\n        return internal::ValueArray24<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24>(v1, v2,\n                                                                                v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18,\n                                                                                v19, v20, v21, v22, v23, v24);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25>\n    internal::ValueArray25<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25> Values(T1 v1,\n                                                                                T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9, T10 v10, T11 v11,\n                                                                                T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17, T18 v18, T19 v19,\n                                                                                T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25)\n    {\n        return internal::ValueArray25<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25>(v1,\n                                                                                     v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17,\n                                                                                     v18, v19, v20, v21, v22, v23, v24, v25);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26>\n    internal::ValueArray26<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n             T26> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26)\n    {\n        return internal::ValueArray26<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n               T26>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15,\n                    v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27>\n    internal::ValueArray27<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26,\n             T27> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27)\n    {\n        return internal::ValueArray27<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n               T26, T27>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14,\n                         v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28>\n    internal::ValueArray28<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27,\n             T28> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28)\n    {\n        return internal::ValueArray28<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n               T26, T27, T28>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13,\n                              v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27,\n                              v28);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29>\n    internal::ValueArray29<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n             T29> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29)\n    {\n        return internal::ValueArray29<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n               T26, T27, T28, T29>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12,\n                                   v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26,\n                                   v27, v28, v29);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30>\n    internal::ValueArray30<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n             T29, T30> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8,\n                              T9 v9, T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16,\n                              T17 v17, T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24,\n                              T25 v25, T26 v26, T27 v27, T28 v28, T29 v29, T30 v30)\n    {\n        return internal::ValueArray30<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n               T26, T27, T28, T29, T30>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11,\n                                        v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25,\n                                        v26, v27, v28, v29, v30);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31>\n    internal::ValueArray31<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n             T29, T30, T31> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7,\n                                   T8 v8, T9 v9, T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15,\n                                   T16 v16, T17 v17, T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23,\n                                   T24 v24, T25 v25, T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31)\n    {\n        return internal::ValueArray31<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n               T26, T27, T28, T29, T30, T31>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10,\n                                             v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24,\n                                             v25, v26, v27, v28, v29, v30, v31);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32>\n    internal::ValueArray32<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n             T29, T30, T31, T32> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7,\n                                        T8 v8, T9 v9, T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15,\n                                        T16 v16, T17 v17, T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23,\n                                        T24 v24, T25 v25, T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31,\n                                        T32 v32)\n    {\n        return internal::ValueArray32<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n               T26, T27, T28, T29, T30, T31, T32>(v1, v2, v3, v4, v5, v6, v7, v8, v9,\n                                                  v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23,\n                                                  v24, v25, v26, v27, v28, v29, v30, v31, v32);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33>\n    internal::ValueArray33<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n             T29, T30, T31, T32, T33> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6,\n                                             T7 v7, T8 v8, T9 v9, T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15,\n                                             T16 v16, T17 v17, T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23,\n                                             T24 v24, T25 v25, T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31,\n                                             T32 v32, T33 v33)\n    {\n        return internal::ValueArray33<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n               T26, T27, T28, T29, T30, T31, T32, T33>(v1, v2, v3, v4, v5, v6, v7, v8,\n                                                       v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23,\n                                                       v24, v25, v26, v27, v28, v29, v30, v31, v32, v33);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34>\n    internal::ValueArray34<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n             T29, T30, T31, T32, T33, T34> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5,\n                                                  T6 v6, T7 v7, T8 v8, T9 v9, T10 v10, T11 v11, T12 v12, T13 v13, T14 v14,\n                                                  T15 v15, T16 v16, T17 v17, T18 v18, T19 v19, T20 v20, T21 v21, T22 v22,\n                                                  T23 v23, T24 v24, T25 v25, T26 v26, T27 v27, T28 v28, T29 v29, T30 v30,\n                                                  T31 v31, T32 v32, T33 v33, T34 v34)\n    {\n        return internal::ValueArray34<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n               T26, T27, T28, T29, T30, T31, T32, T33, T34>(v1, v2, v3, v4, v5, v6, v7,\n                                                            v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22,\n                                                            v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35>\n    internal::ValueArray35<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n             T29, T30, T31, T32, T33, T34, T35> Values(T1 v1, T2 v2, T3 v3, T4 v4,\n                                                       T5 v5, T6 v6, T7 v7, T8 v8, T9 v9, T10 v10, T11 v11, T12 v12, T13 v13,\n                                                       T14 v14, T15 v15, T16 v16, T17 v17, T18 v18, T19 v19, T20 v20, T21 v21,\n                                                       T22 v22, T23 v23, T24 v24, T25 v25, T26 v26, T27 v27, T28 v28, T29 v29,\n                                                       T30 v30, T31 v31, T32 v32, T33 v33, T34 v34, T35 v35)\n    {\n        return internal::ValueArray35<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n               T26, T27, T28, T29, T30, T31, T32, T33, T34, T35>(v1, v2, v3, v4, v5, v6,\n                                                                 v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21,\n                                                                 v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36>\n    internal::ValueArray36<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n             T29, T30, T31, T32, T33, T34, T35, T36> Values(T1 v1, T2 v2, T3 v3, T4 v4,\n                                                            T5 v5, T6 v6, T7 v7, T8 v8, T9 v9, T10 v10, T11 v11, T12 v12, T13 v13,\n                                                            T14 v14, T15 v15, T16 v16, T17 v17, T18 v18, T19 v19, T20 v20, T21 v21,\n                                                            T22 v22, T23 v23, T24 v24, T25 v25, T26 v26, T27 v27, T28 v28, T29 v29,\n                                                            T30 v30, T31 v31, T32 v32, T33 v33, T34 v34, T35 v35, T36 v36)\n    {\n        return internal::ValueArray36<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n               T26, T27, T28, T29, T30, T31, T32, T33, T34, T35, T36>(v1, v2, v3, v4,\n                                                                      v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19,\n                                                                      v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33,\n                                                                      v34, v35, v36);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37>\n    internal::ValueArray37<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n             T29, T30, T31, T32, T33, T34, T35, T36, T37> Values(T1 v1, T2 v2, T3 v3,\n                                                                 T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9, T10 v10, T11 v11, T12 v12,\n                                                                 T13 v13, T14 v14, T15 v15, T16 v16, T17 v17, T18 v18, T19 v19, T20 v20,\n                                                                 T21 v21, T22 v22, T23 v23, T24 v24, T25 v25, T26 v26, T27 v27, T28 v28,\n                                                                 T29 v29, T30 v30, T31 v31, T32 v32, T33 v33, T34 v34, T35 v35, T36 v36,\n                                                                 T37 v37)\n    {\n        return internal::ValueArray37<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n               T26, T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37>(v1, v2, v3,\n                                                                           v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19,\n                                                                           v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33,\n                                                                           v34, v35, v36, v37);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37, typename T38>\n    internal::ValueArray38<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n             T29, T30, T31, T32, T33, T34, T35, T36, T37, T38> Values(T1 v1, T2 v2,\n                                                                      T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9, T10 v10, T11 v11, T12 v12,\n                                                                      T13 v13, T14 v14, T15 v15, T16 v16, T17 v17, T18 v18, T19 v19, T20 v20,\n                                                                      T21 v21, T22 v22, T23 v23, T24 v24, T25 v25, T26 v26, T27 v27, T28 v28,\n                                                                      T29 v29, T30 v30, T31 v31, T32 v32, T33 v33, T34 v34, T35 v35, T36 v36,\n                                                                      T37 v37, T38 v38)\n    {\n        return internal::ValueArray38<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n               T26, T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38>(v1, v2,\n                                                                                v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18,\n                                                                                v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32,\n                                                                                v33, v34, v35, v36, v37, v38);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37, typename T38, typename T39>\n    internal::ValueArray39<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n             T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39> Values(T1 v1, T2 v2,\n                                                                           T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9, T10 v10, T11 v11, T12 v12,\n                                                                           T13 v13, T14 v14, T15 v15, T16 v16, T17 v17, T18 v18, T19 v19, T20 v20,\n                                                                           T21 v21, T22 v22, T23 v23, T24 v24, T25 v25, T26 v26, T27 v27, T28 v28,\n                                                                           T29 v29, T30 v30, T31 v31, T32 v32, T33 v33, T34 v34, T35 v35, T36 v36,\n                                                                           T37 v37, T38 v38, T39 v39)\n    {\n        return internal::ValueArray39<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n               T26, T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39>(v1,\n                                                                                     v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17,\n                                                                                     v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31,\n                                                                                     v32, v33, v34, v35, v36, v37, v38, v39);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37, typename T38, typename T39, typename T40>\n    internal::ValueArray40<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n             T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40> Values(T1 v1,\n                                                                                T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9, T10 v10, T11 v11,\n                                                                                T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17, T18 v18, T19 v19,\n                                                                                T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25, T26 v26, T27 v27,\n                                                                                T28 v28, T29 v29, T30 v30, T31 v31, T32 v32, T33 v33, T34 v34, T35 v35,\n                                                                                T36 v36, T37 v37, T38 v38, T39 v39, T40 v40)\n    {\n        return internal::ValueArray40<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n               T26, T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39,\n               T40>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15,\n                    v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29,\n                    v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37, typename T38, typename T39, typename T40,\n              typename T41>\n    internal::ValueArray41<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n             T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40,\n             T41> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31, T32 v32, T33 v33,\n                         T34 v34, T35 v35, T36 v36, T37 v37, T38 v38, T39 v39, T40 v40, T41 v41)\n    {\n        return internal::ValueArray41<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n               T26, T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39,\n               T40, T41>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14,\n                         v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28,\n                         v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37, typename T38, typename T39, typename T40,\n              typename T41, typename T42>\n    internal::ValueArray42<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n             T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41,\n             T42> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31, T32 v32, T33 v33,\n                         T34 v34, T35 v35, T36 v36, T37 v37, T38 v38, T39 v39, T40 v40, T41 v41,\n                         T42 v42)\n    {\n        return internal::ValueArray42<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n               T26, T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39,\n               T40, T41, T42>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13,\n                              v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27,\n                              v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41,\n                              v42);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37, typename T38, typename T39, typename T40,\n              typename T41, typename T42, typename T43>\n    internal::ValueArray43<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n             T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42,\n             T43> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31, T32 v32, T33 v33,\n                         T34 v34, T35 v35, T36 v36, T37 v37, T38 v38, T39 v39, T40 v40, T41 v41,\n                         T42 v42, T43 v43)\n    {\n        return internal::ValueArray43<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n               T26, T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39,\n               T40, T41, T42, T43>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12,\n                                   v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26,\n                                   v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40,\n                                   v41, v42, v43);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37, typename T38, typename T39, typename T40,\n              typename T41, typename T42, typename T43, typename T44>\n    internal::ValueArray44<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n             T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43,\n             T44> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8, T9 v9,\n                         T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16, T17 v17,\n                         T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24, T25 v25,\n                         T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31, T32 v32, T33 v33,\n                         T34 v34, T35 v35, T36 v36, T37 v37, T38 v38, T39 v39, T40 v40, T41 v41,\n                         T42 v42, T43 v43, T44 v44)\n    {\n        return internal::ValueArray44<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n               T26, T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39,\n               T40, T41, T42, T43, T44>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11,\n                                        v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25,\n                                        v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39,\n                                        v40, v41, v42, v43, v44);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37, typename T38, typename T39, typename T40,\n              typename T41, typename T42, typename T43, typename T44, typename T45>\n    internal::ValueArray45<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n             T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43,\n             T44, T45> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7, T8 v8,\n                              T9 v9, T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15, T16 v16,\n                              T17 v17, T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23, T24 v24,\n                              T25 v25, T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31, T32 v32,\n                              T33 v33, T34 v34, T35 v35, T36 v36, T37 v37, T38 v38, T39 v39, T40 v40,\n                              T41 v41, T42 v42, T43 v43, T44 v44, T45 v45)\n    {\n        return internal::ValueArray45<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n               T26, T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39,\n               T40, T41, T42, T43, T44, T45>(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10,\n                                             v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24,\n                                             v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38,\n                                             v39, v40, v41, v42, v43, v44, v45);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37, typename T38, typename T39, typename T40,\n              typename T41, typename T42, typename T43, typename T44, typename T45,\n              typename T46>\n    internal::ValueArray46<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n             T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43,\n             T44, T45, T46> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7,\n                                   T8 v8, T9 v9, T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15,\n                                   T16 v16, T17 v17, T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23,\n                                   T24 v24, T25 v25, T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31,\n                                   T32 v32, T33 v33, T34 v34, T35 v35, T36 v36, T37 v37, T38 v38, T39 v39,\n                                   T40 v40, T41 v41, T42 v42, T43 v43, T44 v44, T45 v45, T46 v46)\n    {\n        return internal::ValueArray46<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n               T26, T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39,\n               T40, T41, T42, T43, T44, T45, T46>(v1, v2, v3, v4, v5, v6, v7, v8, v9,\n                                                  v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23,\n                                                  v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37,\n                                                  v38, v39, v40, v41, v42, v43, v44, v45, v46);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37, typename T38, typename T39, typename T40,\n              typename T41, typename T42, typename T43, typename T44, typename T45,\n              typename T46, typename T47>\n    internal::ValueArray47<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n             T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43,\n             T44, T45, T46, T47> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7,\n                                        T8 v8, T9 v9, T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15,\n                                        T16 v16, T17 v17, T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23,\n                                        T24 v24, T25 v25, T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31,\n                                        T32 v32, T33 v33, T34 v34, T35 v35, T36 v36, T37 v37, T38 v38, T39 v39,\n                                        T40 v40, T41 v41, T42 v42, T43 v43, T44 v44, T45 v45, T46 v46, T47 v47)\n    {\n        return internal::ValueArray47<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n               T26, T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39,\n               T40, T41, T42, T43, T44, T45, T46, T47>(v1, v2, v3, v4, v5, v6, v7, v8,\n                                                       v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23,\n                                                       v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37,\n                                                       v38, v39, v40, v41, v42, v43, v44, v45, v46, v47);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37, typename T38, typename T39, typename T40,\n              typename T41, typename T42, typename T43, typename T44, typename T45,\n              typename T46, typename T47, typename T48>\n    internal::ValueArray48<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n             T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43,\n             T44, T45, T46, T47, T48> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6,\n                                             T7 v7, T8 v8, T9 v9, T10 v10, T11 v11, T12 v12, T13 v13, T14 v14, T15 v15,\n                                             T16 v16, T17 v17, T18 v18, T19 v19, T20 v20, T21 v21, T22 v22, T23 v23,\n                                             T24 v24, T25 v25, T26 v26, T27 v27, T28 v28, T29 v29, T30 v30, T31 v31,\n                                             T32 v32, T33 v33, T34 v34, T35 v35, T36 v36, T37 v37, T38 v38, T39 v39,\n                                             T40 v40, T41 v41, T42 v42, T43 v43, T44 v44, T45 v45, T46 v46, T47 v47,\n                                             T48 v48)\n    {\n        return internal::ValueArray48<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n               T26, T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39,\n               T40, T41, T42, T43, T44, T45, T46, T47, T48>(v1, v2, v3, v4, v5, v6, v7,\n                                                            v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22,\n                                                            v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36,\n                                                            v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37, typename T38, typename T39, typename T40,\n              typename T41, typename T42, typename T43, typename T44, typename T45,\n              typename T46, typename T47, typename T48, typename T49>\n    internal::ValueArray49<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n             T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43,\n             T44, T45, T46, T47, T48, T49> Values(T1 v1, T2 v2, T3 v3, T4 v4, T5 v5,\n                                                  T6 v6, T7 v7, T8 v8, T9 v9, T10 v10, T11 v11, T12 v12, T13 v13, T14 v14,\n                                                  T15 v15, T16 v16, T17 v17, T18 v18, T19 v19, T20 v20, T21 v21, T22 v22,\n                                                  T23 v23, T24 v24, T25 v25, T26 v26, T27 v27, T28 v28, T29 v29, T30 v30,\n                                                  T31 v31, T32 v32, T33 v33, T34 v34, T35 v35, T36 v36, T37 v37, T38 v38,\n                                                  T39 v39, T40 v40, T41 v41, T42 v42, T43 v43, T44 v44, T45 v45, T46 v46,\n                                                  T47 v47, T48 v48, T49 v49)\n    {\n        return internal::ValueArray49<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n               T26, T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39,\n               T40, T41, T42, T43, T44, T45, T46, T47, T48, T49>(v1, v2, v3, v4, v5, v6,\n                                                                 v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21,\n                                                                 v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35,\n                                                                 v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49);\n    }\n\n    template <typename T1, typename T2, typename T3, typename T4, typename T5,\n              typename T6, typename T7, typename T8, typename T9, typename T10,\n              typename T11, typename T12, typename T13, typename T14, typename T15,\n              typename T16, typename T17, typename T18, typename T19, typename T20,\n              typename T21, typename T22, typename T23, typename T24, typename T25,\n              typename T26, typename T27, typename T28, typename T29, typename T30,\n              typename T31, typename T32, typename T33, typename T34, typename T35,\n              typename T36, typename T37, typename T38, typename T39, typename T40,\n              typename T41, typename T42, typename T43, typename T44, typename T45,\n              typename T46, typename T47, typename T48, typename T49, typename T50>\n    internal::ValueArray50<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,\n             T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25, T26, T27, T28,\n             T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39, T40, T41, T42, T43,\n             T44, T45, T46, T47, T48, T49, T50> Values(T1 v1, T2 v2, T3 v3, T4 v4,\n                                                       T5 v5, T6 v6, T7 v7, T8 v8, T9 v9, T10 v10, T11 v11, T12 v12, T13 v13,\n                                                       T14 v14, T15 v15, T16 v16, T17 v17, T18 v18, T19 v19, T20 v20, T21 v21,\n                                                       T22 v22, T23 v23, T24 v24, T25 v25, T26 v26, T27 v27, T28 v28, T29 v29,\n                                                       T30 v30, T31 v31, T32 v32, T33 v33, T34 v34, T35 v35, T36 v36, T37 v37,\n                                                       T38 v38, T39 v39, T40 v40, T41 v41, T42 v42, T43 v43, T44 v44, T45 v45,\n                                                       T46 v46, T47 v47, T48 v48, T49 v49, T50 v50)\n    {\n        return internal::ValueArray50<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,\n               T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,\n               T26, T27, T28, T29, T30, T31, T32, T33, T34, T35, T36, T37, T38, T39,\n               T40, T41, T42, T43, T44, T45, T46, T47, T48, T49, T50>(v1, v2, v3, v4,\n                                                                      v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19,\n                                                                      v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33,\n                                                                      v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47,\n                                                                      v48, v49, v50);\n    }\n\n// Bool() allows generating tests with parameters in a set of (false, true).\n//\n// Synopsis:\n// Bool()\n//   - returns a generator producing sequences with elements {false, true}.\n//\n// It is useful when testing code that depends on Boolean flags. Combinations\n// of multiple flags can be tested when several Bool()'s are combined using\n// Combine() function.\n//\n// In the following example all tests in the test case FlagDependentTest\n// will be instantiated twice with parameters false and true.\n//\n// class FlagDependentTest : public testing::TestWithParam<bool> {\n//   virtual void SetUp() {\n//     external_flag = GetParam();\n//   }\n// }\n// INSTANTIATE_TEST_CASE_P(BoolSequence, FlagDependentTest, Bool());\n//\n    inline internal::ParamGenerator<bool> Bool()\n    {\n        return Values(false, true);\n    }\n\n# if GTEST_HAS_COMBINE\n// Combine() allows the user to combine two or more sequences to produce\n// values of a Cartesian product of those sequences' elements.\n//\n// Synopsis:\n// Combine(gen1, gen2, ..., genN)\n//   - returns a generator producing sequences with elements coming from\n//     the Cartesian product of elements from the sequences generated by\n//     gen1, gen2, ..., genN. The sequence elements will have a type of\n//     tuple<T1, T2, ..., TN> where T1, T2, ..., TN are the types\n//     of elements from sequences produces by gen1, gen2, ..., genN.\n//\n// Combine can have up to 10 arguments. This number is currently limited\n// by the maximum number of elements in the tuple implementation used by Google\n// Test.\n//\n// Example:\n//\n// This will instantiate tests in test case AnimalTest each one with\n// the parameter values tuple(\"cat\", BLACK), tuple(\"cat\", WHITE),\n// tuple(\"dog\", BLACK), and tuple(\"dog\", WHITE):\n//\n// enum Color { BLACK, GRAY, WHITE };\n// class AnimalTest\n//     : public testing::TestWithParam<tuple<const char*, Color> > {...};\n//\n// TEST_P(AnimalTest, AnimalLooksNice) {...}\n//\n// INSTANTIATE_TEST_CASE_P(AnimalVariations, AnimalTest,\n//                         Combine(Values(\"cat\", \"dog\"),\n//                                 Values(BLACK, WHITE)));\n//\n// This will instantiate tests in FlagDependentTest with all variations of two\n// Boolean flags:\n//\n// class FlagDependentTest\n//     : public testing::TestWithParam<tuple<bool, bool> > {\n//   virtual void SetUp() {\n//     // Assigns external_flag_1 and external_flag_2 values from the tuple.\n//     tie(external_flag_1, external_flag_2) = GetParam();\n//   }\n// };\n//\n// TEST_P(FlagDependentTest, TestFeature1) {\n//   // Test your code using external_flag_1 and external_flag_2 here.\n// }\n// INSTANTIATE_TEST_CASE_P(TwoBoolSequence, FlagDependentTest,\n//                         Combine(Bool(), Bool()));\n//\n    template <typename Generator1, typename Generator2>\n    internal::CartesianProductHolder2<Generator1, Generator2> Combine(\n        const Generator1 &g1, const Generator2 &g2)\n    {\n        return internal::CartesianProductHolder2<Generator1, Generator2>(\n                   g1, g2);\n    }\n\n    template <typename Generator1, typename Generator2, typename Generator3>\n    internal::CartesianProductHolder3<Generator1, Generator2, Generator3> Combine(\n        const Generator1 &g1, const Generator2 &g2, const Generator3 &g3)\n    {\n        return internal::CartesianProductHolder3<Generator1, Generator2, Generator3>(\n                   g1, g2, g3);\n    }\n\n    template <typename Generator1, typename Generator2, typename Generator3,\n              typename Generator4>\n    internal::CartesianProductHolder4<Generator1, Generator2, Generator3,\n             Generator4> Combine(\n                 const Generator1 &g1, const Generator2 &g2, const Generator3 &g3,\n                 const Generator4 &g4)\n    {\n        return internal::CartesianProductHolder4<Generator1, Generator2, Generator3,\n               Generator4>(\n                   g1, g2, g3, g4);\n    }\n\n    template <typename Generator1, typename Generator2, typename Generator3,\n              typename Generator4, typename Generator5>\n    internal::CartesianProductHolder5<Generator1, Generator2, Generator3,\n             Generator4, Generator5> Combine(\n                 const Generator1 &g1, const Generator2 &g2, const Generator3 &g3,\n                 const Generator4 &g4, const Generator5 &g5)\n    {\n        return internal::CartesianProductHolder5<Generator1, Generator2, Generator3,\n               Generator4, Generator5>(\n                   g1, g2, g3, g4, g5);\n    }\n\n    template <typename Generator1, typename Generator2, typename Generator3,\n              typename Generator4, typename Generator5, typename Generator6>\n    internal::CartesianProductHolder6<Generator1, Generator2, Generator3,\n             Generator4, Generator5, Generator6> Combine(\n                 const Generator1 &g1, const Generator2 &g2, const Generator3 &g3,\n                 const Generator4 &g4, const Generator5 &g5, const Generator6 &g6)\n    {\n        return internal::CartesianProductHolder6<Generator1, Generator2, Generator3,\n               Generator4, Generator5, Generator6>(\n                   g1, g2, g3, g4, g5, g6);\n    }\n\n    template <typename Generator1, typename Generator2, typename Generator3,\n              typename Generator4, typename Generator5, typename Generator6,\n              typename Generator7>\n    internal::CartesianProductHolder7<Generator1, Generator2, Generator3,\n             Generator4, Generator5, Generator6, Generator7> Combine(\n                 const Generator1 &g1, const Generator2 &g2, const Generator3 &g3,\n                 const Generator4 &g4, const Generator5 &g5, const Generator6 &g6,\n                 const Generator7 &g7)\n    {\n        return internal::CartesianProductHolder7<Generator1, Generator2, Generator3,\n               Generator4, Generator5, Generator6, Generator7>(\n                   g1, g2, g3, g4, g5, g6, g7);\n    }\n\n    template <typename Generator1, typename Generator2, typename Generator3,\n              typename Generator4, typename Generator5, typename Generator6,\n              typename Generator7, typename Generator8>\n    internal::CartesianProductHolder8<Generator1, Generator2, Generator3,\n             Generator4, Generator5, Generator6, Generator7, Generator8> Combine(\n                 const Generator1 &g1, const Generator2 &g2, const Generator3 &g3,\n                 const Generator4 &g4, const Generator5 &g5, const Generator6 &g6,\n                 const Generator7 &g7, const Generator8 &g8)\n    {\n        return internal::CartesianProductHolder8<Generator1, Generator2, Generator3,\n               Generator4, Generator5, Generator6, Generator7, Generator8>(\n                   g1, g2, g3, g4, g5, g6, g7, g8);\n    }\n\n    template <typename Generator1, typename Generator2, typename Generator3,\n              typename Generator4, typename Generator5, typename Generator6,\n              typename Generator7, typename Generator8, typename Generator9>\n    internal::CartesianProductHolder9<Generator1, Generator2, Generator3,\n             Generator4, Generator5, Generator6, Generator7, Generator8,\n             Generator9> Combine(\n                 const Generator1 &g1, const Generator2 &g2, const Generator3 &g3,\n                 const Generator4 &g4, const Generator5 &g5, const Generator6 &g6,\n                 const Generator7 &g7, const Generator8 &g8, const Generator9 &g9)\n    {\n        return internal::CartesianProductHolder9<Generator1, Generator2, Generator3,\n               Generator4, Generator5, Generator6, Generator7, Generator8, Generator9>(\n                   g1, g2, g3, g4, g5, g6, g7, g8, g9);\n    }\n\n    template <typename Generator1, typename Generator2, typename Generator3,\n              typename Generator4, typename Generator5, typename Generator6,\n              typename Generator7, typename Generator8, typename Generator9,\n              typename Generator10>\n    internal::CartesianProductHolder10<Generator1, Generator2, Generator3,\n             Generator4, Generator5, Generator6, Generator7, Generator8, Generator9,\n             Generator10> Combine(\n                 const Generator1 &g1, const Generator2 &g2, const Generator3 &g3,\n                 const Generator4 &g4, const Generator5 &g5, const Generator6 &g6,\n                 const Generator7 &g7, const Generator8 &g8, const Generator9 &g9,\n                 const Generator10 &g10)\n    {\n        return internal::CartesianProductHolder10<Generator1, Generator2, Generator3,\n               Generator4, Generator5, Generator6, Generator7, Generator8, Generator9,\n               Generator10>(\n                   g1, g2, g3, g4, g5, g6, g7, g8, g9, g10);\n    }\n# endif  // GTEST_HAS_COMBINE\n\n\n\n# define TEST_P(test_case_name, test_name) \\\n  class GTEST_TEST_CLASS_NAME_(test_case_name, test_name) \\\n      : public test_case_name { \\\n   public: \\\n    GTEST_TEST_CLASS_NAME_(test_case_name, test_name)() {} \\\n    virtual void TestBody(); \\\n   private: \\\n    static int AddToRegistry() { \\\n      ::testing::UnitTest::GetInstance()->parameterized_test_registry(). \\\n          GetTestCasePatternHolder<test_case_name>(\\\n              #test_case_name, \\\n              ::testing::internal::CodeLocation(\\\n                  __FILE__, __LINE__))->AddTestPattern(\\\n                      #test_case_name, \\\n                      #test_name, \\\n                      new ::testing::internal::TestMetaFactory< \\\n                          GTEST_TEST_CLASS_NAME_(\\\n                              test_case_name, test_name)>()); \\\n      return 0; \\\n    } \\\n    static int gtest_registering_dummy_ GTEST_ATTRIBUTE_UNUSED_; \\\n    GTEST_DISALLOW_COPY_AND_ASSIGN_(\\\n        GTEST_TEST_CLASS_NAME_(test_case_name, test_name)); \\\n  }; \\\n  int GTEST_TEST_CLASS_NAME_(test_case_name, \\\n                             test_name)::gtest_registering_dummy_ = \\\n      GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::AddToRegistry(); \\\n  void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody()\n\n// The optional last argument to INSTANTIATE_TEST_CASE_P allows the user\n// to specify a function or functor that generates custom test name suffixes\n// based on the test parameters. The function should accept one argument of\n// type testing::TestParamInfo<class ParamType>, and return std::string.\n//\n// testing::PrintToStringParamName is a builtin test suffix generator that\n// returns the value of testing::PrintToString(GetParam()). It does not work\n// for std::string or C strings.\n//\n// Note: test names must be non-empty, unique, and may only contain ASCII\n// alphanumeric characters or underscore.\n\n# define INSTANTIATE_TEST_CASE_P(prefix, test_case_name, generator, ...) \\\n  ::testing::internal::ParamGenerator<test_case_name::ParamType> \\\n      gtest_##prefix##test_case_name##_EvalGenerator_() { return generator; } \\\n  ::std::string gtest_##prefix##test_case_name##_EvalGenerateName_( \\\n      const ::testing::TestParamInfo<test_case_name::ParamType>& info) { \\\n    return ::testing::internal::GetParamNameGen<test_case_name::ParamType> \\\n        (__VA_ARGS__)(info); \\\n  } \\\n  int gtest_##prefix##test_case_name##_dummy_ GTEST_ATTRIBUTE_UNUSED_ = \\\n      ::testing::UnitTest::GetInstance()->parameterized_test_registry(). \\\n          GetTestCasePatternHolder<test_case_name>(\\\n              #test_case_name, \\\n              ::testing::internal::CodeLocation(\\\n                  __FILE__, __LINE__))->AddTestCaseInstantiation(\\\n                      #prefix, \\\n                      &gtest_##prefix##test_case_name##_EvalGenerator_, \\\n                      &gtest_##prefix##test_case_name##_EvalGenerateName_, \\\n                      __FILE__, __LINE__)\n\n}  // namespace testing\n\n#endif  // GTEST_HAS_PARAM_TEST\n\n#endif  // GTEST_INCLUDE_GTEST_GTEST_PARAM_TEST_H_\n// Copyright 2006, Google Inc.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Author: wan@google.com (Zhanyong Wan)\n//\n// Google C++ Testing Framework definitions useful in production code.\n\n#ifndef GTEST_INCLUDE_GTEST_GTEST_PROD_H_\n#define GTEST_INCLUDE_GTEST_GTEST_PROD_H_\n\n// When you need to test the private or protected members of a class,\n// use the FRIEND_TEST macro to declare your tests as friends of the\n// class.  For example:\n//\n// class MyClass {\n//  private:\n//   void MyMethod();\n//   FRIEND_TEST(MyClassTest, MyMethod);\n// };\n//\n// class MyClassTest : public testing::Test {\n//   // ...\n// };\n//\n// TEST_F(MyClassTest, MyMethod) {\n//   // Can call MyClass::MyMethod() here.\n// }\n\n#define FRIEND_TEST(test_case_name, test_name)\\\nfriend class test_case_name##_##test_name##_Test\n\n#endif  // GTEST_INCLUDE_GTEST_GTEST_PROD_H_\n// Copyright 2008, Google Inc.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Author: mheule@google.com (Markus Heule)\n//\n\n#ifndef GTEST_INCLUDE_GTEST_GTEST_TEST_PART_H_\n#define GTEST_INCLUDE_GTEST_GTEST_TEST_PART_H_\n\n#include <iosfwd>\n#include <vector>\n\nnamespace testing\n{\n\n// A copyable object representing the result of a test part (i.e. an\n// assertion or an explicit FAIL(), ADD_FAILURE(), or SUCCESS()).\n//\n// Don't inherit from TestPartResult as its destructor is not virtual.\n    class GTEST_API_ TestPartResult\n    {\n    public:\n        // The possible outcomes of a test part (i.e. an assertion or an\n        // explicit SUCCEED(), FAIL(), or ADD_FAILURE()).\n        enum Type {\n            kSuccess,          // Succeeded.\n            kNonFatalFailure,  // Failed but the test can continue.\n            kFatalFailure      // Failed and the test should be terminated.\n        };\n\n        // C'tor.  TestPartResult does NOT have a default constructor.\n        // Always use this constructor (with parameters) to create a\n        // TestPartResult object.\n        TestPartResult(Type a_type,\n                       const char *a_file_name,\n                       int a_line_number,\n                       const char *a_message)\n            : type_(a_type),\n              file_name_(a_file_name == NULL ? \"\" : a_file_name),\n              line_number_(a_line_number),\n              summary_(ExtractSummary(a_message)),\n              message_(a_message)\n        {\n        }\n\n        // Gets the outcome of the test part.\n        Type type() const\n        {\n            return type_;\n        }\n\n        // Gets the name of the source file where the test part took place, or\n        // NULL if it's unknown.\n        const char *file_name() const\n        {\n            return file_name_.empty() ? NULL : file_name_.c_str();\n        }\n\n        // Gets the line in the source file where the test part took place,\n        // or -1 if it's unknown.\n        int line_number() const\n        {\n            return line_number_;\n        }\n\n        // Gets the summary of the failure message.\n        const char *summary() const\n        {\n            return summary_.c_str();\n        }\n\n        // Gets the message associated with the test part.\n        const char *message() const\n        {\n            return message_.c_str();\n        }\n\n        // Returns true iff the test part passed.\n        bool passed() const\n        {\n            return type_ == kSuccess;\n        }\n\n        // Returns true iff the test part failed.\n        bool failed() const\n        {\n            return type_ != kSuccess;\n        }\n\n        // Returns true iff the test part non-fatally failed.\n        bool nonfatally_failed() const\n        {\n            return type_ == kNonFatalFailure;\n        }\n\n        // Returns true iff the test part fatally failed.\n        bool fatally_failed() const\n        {\n            return type_ == kFatalFailure;\n        }\n\n    private:\n        Type type_;\n\n        // Gets the summary of the failure message by omitting the stack\n        // trace in it.\n        static std::string ExtractSummary(const char *message);\n\n        // The name of the source file where the test part took place, or\n        // \"\" if the source file is unknown.\n        std::string file_name_;\n        // The line in the source file where the test part took place, or -1\n        // if the line number is unknown.\n        int line_number_;\n        std::string summary_;  // The test failure summary.\n        std::string message_;  // The test failure message.\n    };\n\n// Prints a TestPartResult object.\n    std::ostream &operator<<(std::ostream &os, const TestPartResult &result);\n\n// An array of TestPartResult objects.\n//\n// Don't inherit from TestPartResultArray as its destructor is not\n// virtual.\n    class GTEST_API_ TestPartResultArray\n    {\n    public:\n        TestPartResultArray() {}\n\n        // Appends the given TestPartResult to the array.\n        void Append(const TestPartResult &result);\n\n        // Returns the TestPartResult at the given index (0-based).\n        const TestPartResult &GetTestPartResult(int index) const;\n\n        // Returns the number of TestPartResult objects in the array.\n        int size() const;\n\n    private:\n        std::vector<TestPartResult> array_;\n\n        GTEST_DISALLOW_COPY_AND_ASSIGN_(TestPartResultArray);\n    };\n\n// This interface knows how to report a test part result.\n    class TestPartResultReporterInterface\n    {\n    public:\n        virtual ~TestPartResultReporterInterface() {}\n\n        virtual void ReportTestPartResult(const TestPartResult &result) = 0;\n    };\n\n    namespace internal\n    {\n\n// This helper class is used by {ASSERT|EXPECT}_NO_FATAL_FAILURE to check if a\n// statement generates new fatal failures. To do so it registers itself as the\n// current test part result reporter. Besides checking if fatal failures were\n// reported, it only delegates the reporting to the former result reporter.\n// The original result reporter is restored in the destructor.\n// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.\n        class GTEST_API_ HasNewFatalFailureHelper\n            : public TestPartResultReporterInterface\n        {\n        public:\n            HasNewFatalFailureHelper();\n            virtual ~HasNewFatalFailureHelper();\n            virtual void ReportTestPartResult(const TestPartResult &result);\n            bool has_new_fatal_failure() const\n            {\n                return has_new_fatal_failure_;\n            }\n        private:\n            bool has_new_fatal_failure_;\n            TestPartResultReporterInterface *original_reporter_;\n\n            GTEST_DISALLOW_COPY_AND_ASSIGN_(HasNewFatalFailureHelper);\n        };\n\n    }  // namespace internal\n\n}  // namespace testing\n\n#endif  // GTEST_INCLUDE_GTEST_GTEST_TEST_PART_H_\n// Copyright 2008 Google Inc.\n// All Rights Reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n//\n// Author: wan@google.com (Zhanyong Wan)\n\n#ifndef GTEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_\n#define GTEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_\n\n// This header implements typed tests and type-parameterized tests.\n\n// Typed (aka type-driven) tests repeat the same test for types in a\n// list.  You must know which types you want to test with when writing\n// typed tests. Here's how you do it:\n\n#if 0\n\n// First, define a fixture class template.  It should be parameterized\n// by a type.  Remember to derive it from testing::Test.\ntemplate <typename T>\nclass FooTest : public testing::Test\n{\npublic:\n    ...\n    typedef std::list<T> List;\n    static T shared_;\n    T value_;\n};\n\n// Next, associate a list of types with the test case, which will be\n// repeated for each type in the list.  The typedef is necessary for\n// the macro to parse correctly.\ntypedef testing::Types<char, int, unsigned int> MyTypes;\nTYPED_TEST_CASE(FooTest, MyTypes);\n\n// If the type list contains only one type, you can write that type\n// directly without Types<...>:\n//   TYPED_TEST_CASE(FooTest, int);\n\n// Then, use TYPED_TEST() instead of TEST_F() to define as many typed\n// tests for this test case as you want.\nTYPED_TEST(FooTest, DoesBlah)\n{\n    // Inside a test, refer to TypeParam to get the type parameter.\n    // Since we are inside a derived class template, C++ requires use to\n    // visit the members of FooTest via 'this'.\n    TypeParam n = this->value_;\n\n    // To visit static members of the fixture, add the TestFixture::\n    // prefix.\n    n += TestFixture::shared_;\n\n    // To refer to typedefs in the fixture, add the \"typename\n    // TestFixture::\" prefix.\n    typename TestFixture::List values;\n    values.push_back(n);\n    ...\n}\n\nTYPED_TEST(FooTest, HasPropertyA)\n{\n    ...\n}\n\n#endif  // 0\n\n// Type-parameterized tests are abstract test patterns parameterized\n// by a type.  Compared with typed tests, type-parameterized tests\n// allow you to define the test pattern without knowing what the type\n// parameters are.  The defined pattern can be instantiated with\n// different types any number of times, in any number of translation\n// units.\n//\n// If you are designing an interface or concept, you can define a\n// suite of type-parameterized tests to verify properties that any\n// valid implementation of the interface/concept should have.  Then,\n// each implementation can easily instantiate the test suite to verify\n// that it conforms to the requirements, without having to write\n// similar tests repeatedly.  Here's an example:\n\n#if 0\n\n// First, define a fixture class template.  It should be parameterized\n// by a type.  Remember to derive it from testing::Test.\ntemplate <typename T>\nclass FooTest : public testing::Test\n{\n    ...\n};\n\n// Next, declare that you will define a type-parameterized test case\n// (the _P suffix is for \"parameterized\" or \"pattern\", whichever you\n// prefer):\nTYPED_TEST_CASE_P(FooTest);\n\n// Then, use TYPED_TEST_P() to define as many type-parameterized tests\n// for this type-parameterized test case as you want.\nTYPED_TEST_P(FooTest, DoesBlah)\n{\n    // Inside a test, refer to TypeParam to get the type parameter.\n    TypeParam n = 0;\n    ...\n}\n\nTYPED_TEST_P(FooTest, HasPropertyA)\n{\n    ...\n}\n\n// Now the tricky part: you need to register all test patterns before\n// you can instantiate them.  The first argument of the macro is the\n// test case name; the rest are the names of the tests in this test\n// case.\nREGISTER_TYPED_TEST_CASE_P(FooTest,\n                           DoesBlah, HasPropertyA);\n\n// Finally, you are free to instantiate the pattern with the types you\n// want.  If you put the above code in a header file, you can #include\n// it in multiple C++ source files and instantiate it multiple times.\n//\n// To distinguish different instances of the pattern, the first\n// argument to the INSTANTIATE_* macro is a prefix that will be added\n// to the actual test case name.  Remember to pick unique prefixes for\n// different instances.\ntypedef testing::Types<char, int, unsigned int> MyTypes;\nINSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, MyTypes);\n\n// If the type list contains only one type, you can write that type\n// directly without Types<...>:\n//   INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, int);\n\n#endif  // 0\n\n\n// Implements typed tests.\n\n#if GTEST_HAS_TYPED_TEST\n\n// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.\n//\n// Expands to the name of the typedef for the type parameters of the\n// given test case.\n# define GTEST_TYPE_PARAMS_(TestCaseName) gtest_type_params_##TestCaseName##_\n\n// The 'Types' template argument below must have spaces around it\n// since some compilers may choke on '>>' when passing a template\n// instance (e.g. Types<int>)\n# define TYPED_TEST_CASE(CaseName, Types) \\\n  typedef ::testing::internal::TypeList< Types >::type \\\n      GTEST_TYPE_PARAMS_(CaseName)\n\n# define TYPED_TEST(CaseName, TestName) \\\n  template <typename gtest_TypeParam_> \\\n  class GTEST_TEST_CLASS_NAME_(CaseName, TestName) \\\n      : public CaseName<gtest_TypeParam_> { \\\n   private: \\\n    typedef CaseName<gtest_TypeParam_> TestFixture; \\\n    typedef gtest_TypeParam_ TypeParam; \\\n    virtual void TestBody(); \\\n  }; \\\n  bool gtest_##CaseName##_##TestName##_registered_ GTEST_ATTRIBUTE_UNUSED_ = \\\n      ::testing::internal::TypeParameterizedTest< \\\n          CaseName, \\\n          ::testing::internal::TemplateSel< \\\n              GTEST_TEST_CLASS_NAME_(CaseName, TestName)>, \\\n          GTEST_TYPE_PARAMS_(CaseName)>::Register(\\\n              \"\", ::testing::internal::CodeLocation(__FILE__, __LINE__), \\\n              #CaseName, #TestName, 0); \\\n  template <typename gtest_TypeParam_> \\\n  void GTEST_TEST_CLASS_NAME_(CaseName, TestName)<gtest_TypeParam_>::TestBody()\n\n#endif  // GTEST_HAS_TYPED_TEST\n\n// Implements type-parameterized tests.\n\n#if GTEST_HAS_TYPED_TEST_P\n\n// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.\n//\n// Expands to the namespace name that the type-parameterized tests for\n// the given type-parameterized test case are defined in.  The exact\n// name of the namespace is subject to change without notice.\n# define GTEST_CASE_NAMESPACE_(TestCaseName) \\\n  gtest_case_##TestCaseName##_\n\n// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.\n//\n// Expands to the name of the variable used to remember the names of\n// the defined tests in the given test case.\n# define GTEST_TYPED_TEST_CASE_P_STATE_(TestCaseName) \\\n  gtest_typed_test_case_p_state_##TestCaseName##_\n\n// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE DIRECTLY.\n//\n// Expands to the name of the variable used to remember the names of\n// the registered tests in the given test case.\n# define GTEST_REGISTERED_TEST_NAMES_(TestCaseName) \\\n  gtest_registered_test_names_##TestCaseName##_\n\n// The variables defined in the type-parameterized test macros are\n// static as typically these macros are used in a .h file that can be\n// #included in multiple translation units linked together.\n# define TYPED_TEST_CASE_P(CaseName) \\\n  static ::testing::internal::TypedTestCasePState \\\n      GTEST_TYPED_TEST_CASE_P_STATE_(CaseName)\n\n# define TYPED_TEST_P(CaseName, TestName) \\\n  namespace GTEST_CASE_NAMESPACE_(CaseName) { \\\n  template <typename gtest_TypeParam_> \\\n  class TestName : public CaseName<gtest_TypeParam_> { \\\n   private: \\\n    typedef CaseName<gtest_TypeParam_> TestFixture; \\\n    typedef gtest_TypeParam_ TypeParam; \\\n    virtual void TestBody(); \\\n  }; \\\n  static bool gtest_##TestName##_defined_ GTEST_ATTRIBUTE_UNUSED_ = \\\n      GTEST_TYPED_TEST_CASE_P_STATE_(CaseName).AddTestName(\\\n          __FILE__, __LINE__, #CaseName, #TestName); \\\n  } \\\n  template <typename gtest_TypeParam_> \\\n  void GTEST_CASE_NAMESPACE_(CaseName)::TestName<gtest_TypeParam_>::TestBody()\n\n# define REGISTER_TYPED_TEST_CASE_P(CaseName, ...) \\\n  namespace GTEST_CASE_NAMESPACE_(CaseName) { \\\n  typedef ::testing::internal::Templates<__VA_ARGS__>::type gtest_AllTests_; \\\n  } \\\n  static const char* const GTEST_REGISTERED_TEST_NAMES_(CaseName) \\\n      GTEST_ATTRIBUTE_UNUSED_ = \\\n          GTEST_TYPED_TEST_CASE_P_STATE_(CaseName).VerifyRegisteredTestNames(\\\n              __FILE__, __LINE__, #__VA_ARGS__)\n\n// The 'Types' template argument below must have spaces around it\n// since some compilers may choke on '>>' when passing a template\n// instance (e.g. Types<int>)\n# define INSTANTIATE_TYPED_TEST_CASE_P(Prefix, CaseName, Types) \\\n  bool gtest_##Prefix##_##CaseName GTEST_ATTRIBUTE_UNUSED_ = \\\n      ::testing::internal::TypeParameterizedTestCase<CaseName, \\\n          GTEST_CASE_NAMESPACE_(CaseName)::gtest_AllTests_, \\\n          ::testing::internal::TypeList< Types >::type>::Register(\\\n              #Prefix, \\\n              ::testing::internal::CodeLocation(__FILE__, __LINE__), \\\n              &GTEST_TYPED_TEST_CASE_P_STATE_(CaseName), \\\n              #CaseName, GTEST_REGISTERED_TEST_NAMES_(CaseName))\n\n#endif  // GTEST_HAS_TYPED_TEST_P\n\n#endif  // GTEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_\n\n// Depending on the platform, different string classes are available.\n// On Linux, in addition to ::std::string, Google also makes use of\n// class ::string, which has the same interface as ::std::string, but\n// has a different implementation.\n//\n// You can define GTEST_HAS_GLOBAL_STRING to 1 to indicate that\n// ::string is available AND is a distinct type to ::std::string, or\n// define it to 0 to indicate otherwise.\n//\n// If ::std::string and ::string are the same class on your platform\n// due to aliasing, you should define GTEST_HAS_GLOBAL_STRING to 0.\n//\n// If you do not define GTEST_HAS_GLOBAL_STRING, it is defined\n// heuristically.\n\nnamespace testing\n{\n\n// Declares the flags.\n\n// This flag temporary enables the disabled tests.\n    GTEST_DECLARE_bool_(also_run_disabled_tests);\n\n// This flag brings the debugger on an assertion failure.\n    GTEST_DECLARE_bool_(break_on_failure);\n\n// This flag controls whether Google Test catches all test-thrown exceptions\n// and logs them as failures.\n    GTEST_DECLARE_bool_(catch_exceptions);\n\n// This flag enables using colors in terminal output. Available values are\n// \"yes\" to enable colors, \"no\" (disable colors), or \"auto\" (the default)\n// to let Google Test decide.\n    GTEST_DECLARE_string_(color);\n\n// This flag sets up the filter to select by name using a glob pattern\n// the tests to run. If the filter is not given all tests are executed.\n    GTEST_DECLARE_string_(filter);\n\n// This flag causes the Google Test to list tests. None of the tests listed\n// are actually run if the flag is provided.\n    GTEST_DECLARE_bool_(list_tests);\n\n// This flag controls whether Google Test emits a detailed XML report to a file\n// in addition to its normal textual output.\n    GTEST_DECLARE_string_(output);\n\n// This flags control whether Google Test prints the elapsed time for each\n// test.\n    GTEST_DECLARE_bool_(print_time);\n\n// This flag specifies the random number seed.\n    GTEST_DECLARE_int32_(random_seed);\n\n// This flag sets how many times the tests are repeated. The default value\n// is 1. If the value is -1 the tests are repeating forever.\n    GTEST_DECLARE_int32_(repeat);\n\n// This flag controls whether Google Test includes Google Test internal\n// stack frames in failure stack traces.\n    GTEST_DECLARE_bool_(show_internal_stack_frames);\n\n// When this flag is specified, tests' order is randomized on every iteration.\n    GTEST_DECLARE_bool_(shuffle);\n\n// This flag specifies the maximum number of stack frames to be\n// printed in a failure message.\n    GTEST_DECLARE_int32_(stack_trace_depth);\n\n// When this flag is specified, a failed assertion will throw an\n// exception if exceptions are enabled, or exit the program with a\n// non-zero code otherwise.\n    GTEST_DECLARE_bool_(throw_on_failure);\n\n// When this flag is set with a \"host:port\" string, on supported\n// platforms test results are streamed to the specified port on\n// the specified host machine.\n    GTEST_DECLARE_string_(stream_result_to);\n\n// The upper limit for valid stack trace depths.\n    const int kMaxStackTraceDepth = 100;\n\n    namespace internal\n    {\n\n        class AssertHelper;\n        class DefaultGlobalTestPartResultReporter;\n        class ExecDeathTest;\n        class NoExecDeathTest;\n        class FinalSuccessChecker;\n        class GTestFlagSaver;\n        class StreamingListenerTest;\n        class TestResultAccessor;\n        class TestEventListenersAccessor;\n        class TestEventRepeater;\n        class UnitTestRecordPropertyTestHelper;\n        class WindowsDeathTest;\n        class UnitTestImpl *GetUnitTestImpl();\n        void ReportFailureInUnknownLocation(TestPartResult::Type result_type,\n                                            const std::string &message);\n\n    }  // namespace internal\n\n// The friend relationship of some of these classes is cyclic.\n// If we don't forward declare them the compiler might confuse the classes\n// in friendship clauses with same named classes on the scope.\n    class Test;\n    class TestCase;\n    class TestInfo;\n    class UnitTest;\n\n// A class for indicating whether an assertion was successful.  When\n// the assertion wasn't successful, the AssertionResult object\n// remembers a non-empty message that describes how it failed.\n//\n// To create an instance of this class, use one of the factory functions\n// (AssertionSuccess() and AssertionFailure()).\n//\n// This class is useful for two purposes:\n//   1. Defining predicate functions to be used with Boolean test assertions\n//      EXPECT_TRUE/EXPECT_FALSE and their ASSERT_ counterparts\n//   2. Defining predicate-format functions to be\n//      used with predicate assertions (ASSERT_PRED_FORMAT*, etc).\n//\n// For example, if you define IsEven predicate:\n//\n//   testing::AssertionResult IsEven(int n) {\n//     if ((n % 2) == 0)\n//       return testing::AssertionSuccess();\n//     else\n//       return testing::AssertionFailure() << n << \" is odd\";\n//   }\n//\n// Then the failed expectation EXPECT_TRUE(IsEven(Fib(5)))\n// will print the message\n//\n//   Value of: IsEven(Fib(5))\n//     Actual: false (5 is odd)\n//   Expected: true\n//\n// instead of a more opaque\n//\n//   Value of: IsEven(Fib(5))\n//     Actual: false\n//   Expected: true\n//\n// in case IsEven is a simple Boolean predicate.\n//\n// If you expect your predicate to be reused and want to support informative\n// messages in EXPECT_FALSE and ASSERT_FALSE (negative assertions show up\n// about half as often as positive ones in our tests), supply messages for\n// both success and failure cases:\n//\n//   testing::AssertionResult IsEven(int n) {\n//     if ((n % 2) == 0)\n//       return testing::AssertionSuccess() << n << \" is even\";\n//     else\n//       return testing::AssertionFailure() << n << \" is odd\";\n//   }\n//\n// Then a statement EXPECT_FALSE(IsEven(Fib(6))) will print\n//\n//   Value of: IsEven(Fib(6))\n//     Actual: true (8 is even)\n//   Expected: false\n//\n// NB: Predicates that support negative Boolean assertions have reduced\n// performance in positive ones so be careful not to use them in tests\n// that have lots (tens of thousands) of positive Boolean assertions.\n//\n// To use this class with EXPECT_PRED_FORMAT assertions such as:\n//\n//   // Verifies that Foo() returns an even number.\n//   EXPECT_PRED_FORMAT1(IsEven, Foo());\n//\n// you need to define:\n//\n//   testing::AssertionResult IsEven(const char* expr, int n) {\n//     if ((n % 2) == 0)\n//       return testing::AssertionSuccess();\n//     else\n//       return testing::AssertionFailure()\n//         << \"Expected: \" << expr << \" is even\\n  Actual: it's \" << n;\n//   }\n//\n// If Foo() returns 5, you will see the following message:\n//\n//   Expected: Foo() is even\n//     Actual: it's 5\n//\n    class GTEST_API_ AssertionResult\n    {\n    public:\n        // Copy constructor.\n        // Used in EXPECT_TRUE/FALSE(assertion_result).\n        AssertionResult(const AssertionResult &other);\n\n        GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800 /* forcing value to bool */)\n\n        // Used in the EXPECT_TRUE/FALSE(bool_expression).\n        //\n        // T must be contextually convertible to bool.\n        //\n        // The second parameter prevents this overload from being considered if\n        // the argument is implicitly convertible to AssertionResult. In that case\n        // we want AssertionResult's copy constructor to be used.\n        template <typename T>\n        explicit AssertionResult(\n            const T &success,\n            typename internal::EnableIf<\n            !internal::ImplicitlyConvertible<T, AssertionResult>::value>::type *\n            /*enabler*/ = NULL)\n            : success_(success) {}\n\n        GTEST_DISABLE_MSC_WARNINGS_POP_()\n\n        // Assignment operator.\n        AssertionResult &operator=(AssertionResult other)\n        {\n            swap(other);\n            return *this;\n        }\n\n        // Returns true iff the assertion succeeded.\n        operator bool() const\n        {\n            return success_;    // NOLINT\n        }\n\n        // Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE.\n        AssertionResult operator!() const;\n\n        // Returns the text streamed into this AssertionResult. Test assertions\n        // use it when they fail (i.e., the predicate's outcome doesn't match the\n        // assertion's expectation). When nothing has been streamed into the\n        // object, returns an empty string.\n        const char *message() const\n        {\n            return message_.get() != NULL ?  message_->c_str() : \"\";\n        }\n        // TODO(vladl@google.com): Remove this after making sure no clients use it.\n        // Deprecated; please use message() instead.\n        const char *failure_message() const\n        {\n            return message();\n        }\n\n        // Streams a custom failure message into this object.\n        template <typename T> AssertionResult &operator<<(const T &value)\n        {\n            AppendMessage(Message() << value);\n            return *this;\n        }\n\n        // Allows streaming basic output manipulators such as endl or flush into\n        // this object.\n        AssertionResult &operator<<(\n            ::std::ostream& (*basic_manipulator)(::std::ostream &stream))\n        {\n            AppendMessage(Message() << basic_manipulator);\n            return *this;\n        }\n\n    private:\n        // Appends the contents of message to message_.\n        void AppendMessage(const Message &a_message)\n        {\n            if (message_.get() == NULL)\n                message_.reset(new ::std::string);\n            message_->append(a_message.GetString().c_str());\n        }\n\n        // Swap the contents of this AssertionResult with other.\n        void swap(AssertionResult &other);\n\n        // Stores result of the assertion predicate.\n        bool success_;\n        // Stores the message describing the condition in case the expectation\n        // construct is not satisfied with the predicate's outcome.\n        // Referenced via a pointer to avoid taking too much stack frame space\n        // with test assertions.\n        internal::scoped_ptr< ::std::string> message_;\n    };\n\n// Makes a successful assertion result.\n    GTEST_API_ AssertionResult AssertionSuccess();\n\n// Makes a failed assertion result.\n    GTEST_API_ AssertionResult AssertionFailure();\n\n// Makes a failed assertion result with the given failure message.\n// Deprecated; use AssertionFailure() << msg.\n    GTEST_API_ AssertionResult AssertionFailure(const Message &msg);\n\n// The abstract class that all tests inherit from.\n//\n// In Google Test, a unit test program contains one or many TestCases, and\n// each TestCase contains one or many Tests.\n//\n// When you define a test using the TEST macro, you don't need to\n// explicitly derive from Test - the TEST macro automatically does\n// this for you.\n//\n// The only time you derive from Test is when defining a test fixture\n// to be used a TEST_F.  For example:\n//\n//   class FooTest : public testing::Test {\n//    protected:\n//     void SetUp() override { ... }\n//     void TearDown() override { ... }\n//     ...\n//   };\n//\n//   TEST_F(FooTest, Bar) { ... }\n//   TEST_F(FooTest, Baz) { ... }\n//\n// Test is not copyable.\n    class GTEST_API_ Test\n    {\n    public:\n        friend class TestInfo;\n\n        // Defines types for pointers to functions that set up and tear down\n        // a test case.\n        typedef internal::SetUpTestCaseFunc SetUpTestCaseFunc;\n        typedef internal::TearDownTestCaseFunc TearDownTestCaseFunc;\n\n        // The d'tor is virtual as we intend to inherit from Test.\n        virtual ~Test();\n\n        // Sets up the stuff shared by all tests in this test case.\n        //\n        // Google Test will call Foo::SetUpTestCase() before running the first\n        // test in test case Foo.  Hence a sub-class can define its own\n        // SetUpTestCase() method to shadow the one defined in the super\n        // class.\n        static void SetUpTestCase() {}\n\n        // Tears down the stuff shared by all tests in this test case.\n        //\n        // Google Test will call Foo::TearDownTestCase() after running the last\n        // test in test case Foo.  Hence a sub-class can define its own\n        // TearDownTestCase() method to shadow the one defined in the super\n        // class.\n        static void TearDownTestCase() {}\n\n        // Returns true iff the current test has a fatal failure.\n        static bool HasFatalFailure();\n\n        // Returns true iff the current test has a non-fatal failure.\n        static bool HasNonfatalFailure();\n\n        // Returns true iff the current test has a (either fatal or\n        // non-fatal) failure.\n        static bool HasFailure()\n        {\n            return HasFatalFailure() || HasNonfatalFailure();\n        }\n\n        // Logs a property for the current test, test case, or for the entire\n        // invocation of the test program when used outside of the context of a\n        // test case.  Only the last value for a given key is remembered.  These\n        // are public static so they can be called from utility functions that are\n        // not members of the test fixture.  Calls to RecordProperty made during\n        // lifespan of the test (from the moment its constructor starts to the\n        // moment its destructor finishes) will be output in XML as attributes of\n        // the <testcase> element.  Properties recorded from fixture's\n        // SetUpTestCase or TearDownTestCase are logged as attributes of the\n        // corresponding <testsuite> element.  Calls to RecordProperty made in the\n        // global context (before or after invocation of RUN_ALL_TESTS and from\n        // SetUp/TearDown method of Environment objects registered with Google\n        // Test) will be output as attributes of the <testsuites> element.\n        static void RecordProperty(const std::string &key, const std::string &value);\n        static void RecordProperty(const std::string &key, int value);\n\n    protected:\n        // Creates a Test object.\n        Test();\n\n        // Sets up the test fixture.\n        virtual void SetUp();\n\n        // Tears down the test fixture.\n        virtual void TearDown();\n\n    private:\n        // Returns true iff the current test has the same fixture class as\n        // the first test in the current test case.\n        static bool HasSameFixtureClass();\n\n        // Runs the test after the test fixture has been set up.\n        //\n        // A sub-class must implement this to define the test logic.\n        //\n        // DO NOT OVERRIDE THIS FUNCTION DIRECTLY IN A USER PROGRAM.\n        // Instead, use the TEST or TEST_F macro.\n        virtual void TestBody() = 0;\n\n        // Sets up, executes, and tears down the test.\n        void Run();\n\n        // Deletes self.  We deliberately pick an unusual name for this\n        // internal method to avoid clashing with names used in user TESTs.\n        void DeleteSelf_()\n        {\n            delete this;\n        }\n\n        const internal::scoped_ptr< GTEST_FLAG_SAVER_ > gtest_flag_saver_;\n\n        // Often a user misspells SetUp() as Setup() and spends a long time\n        // wondering why it is never called by Google Test.  The declaration of\n        // the following method is solely for catching such an error at\n        // compile time:\n        //\n        //   - The return type is deliberately chosen to be not void, so it\n        //   will be a conflict if void Setup() is declared in the user's\n        //   test fixture.\n        //\n        //   - This method is private, so it will be another compiler error\n        //   if the method is called from the user's test fixture.\n        //\n        // DO NOT OVERRIDE THIS FUNCTION.\n        //\n        // If you see an error about overriding the following function or\n        // about it being private, you have mis-spelled SetUp() as Setup().\n        struct Setup_should_be_spelled_SetUp {};\n        virtual Setup_should_be_spelled_SetUp *Setup()\n        {\n            return NULL;\n        }\n\n        // We disallow copying Tests.\n        GTEST_DISALLOW_COPY_AND_ASSIGN_(Test);\n    };\n\n    typedef internal::TimeInMillis TimeInMillis;\n\n// A copyable object representing a user specified test property which can be\n// output as a key/value string pair.\n//\n// Don't inherit from TestProperty as its destructor is not virtual.\n    class TestProperty\n    {\n    public:\n        // C'tor.  TestProperty does NOT have a default constructor.\n        // Always use this constructor (with parameters) to create a\n        // TestProperty object.\n        TestProperty(const std::string &a_key, const std::string &a_value) :\n            key_(a_key), value_(a_value)\n        {\n        }\n\n        // Gets the user supplied key.\n        const char *key() const\n        {\n            return key_.c_str();\n        }\n\n        // Gets the user supplied value.\n        const char *value() const\n        {\n            return value_.c_str();\n        }\n\n        // Sets a new value, overriding the one supplied in the constructor.\n        void SetValue(const std::string &new_value)\n        {\n            value_ = new_value;\n        }\n\n    private:\n        // The key supplied by the user.\n        std::string key_;\n        // The value supplied by the user.\n        std::string value_;\n    };\n\n// The result of a single Test.  This includes a list of\n// TestPartResults, a list of TestProperties, a count of how many\n// death tests there are in the Test, and how much time it took to run\n// the Test.\n//\n// TestResult is not copyable.\n    class GTEST_API_ TestResult\n    {\n    public:\n        // Creates an empty TestResult.\n        TestResult();\n\n        // D'tor.  Do not inherit from TestResult.\n        ~TestResult();\n\n        // Gets the number of all test parts.  This is the sum of the number\n        // of successful test parts and the number of failed test parts.\n        int total_part_count() const;\n\n        // Returns the number of the test properties.\n        int test_property_count() const;\n\n        // Returns true iff the test passed (i.e. no test part failed).\n        bool Passed() const\n        {\n            return !Failed();\n        }\n\n        // Returns true iff the test failed.\n        bool Failed() const;\n\n        // Returns true iff the test fatally failed.\n        bool HasFatalFailure() const;\n\n        // Returns true iff the test has a non-fatal failure.\n        bool HasNonfatalFailure() const;\n\n        // Returns the elapsed time, in milliseconds.\n        TimeInMillis elapsed_time() const\n        {\n            return elapsed_time_;\n        }\n\n        // Returns the i-th test part result among all the results. i can range\n        // from 0 to test_property_count() - 1. If i is not in that range, aborts\n        // the program.\n        const TestPartResult &GetTestPartResult(int i) const;\n\n        // Returns the i-th test property. i can range from 0 to\n        // test_property_count() - 1. If i is not in that range, aborts the\n        // program.\n        const TestProperty &GetTestProperty(int i) const;\n\n    private:\n        friend class TestInfo;\n        friend class TestCase;\n        friend class UnitTest;\n        friend class internal::DefaultGlobalTestPartResultReporter;\n        friend class internal::ExecDeathTest;\n        friend class internal::TestResultAccessor;\n        friend class internal::UnitTestImpl;\n        friend class internal::WindowsDeathTest;\n\n        // Gets the vector of TestPartResults.\n        const std::vector<TestPartResult> &test_part_results() const\n        {\n            return test_part_results_;\n        }\n\n        // Gets the vector of TestProperties.\n        const std::vector<TestProperty> &test_properties() const\n        {\n            return test_properties_;\n        }\n\n        // Sets the elapsed time.\n        void set_elapsed_time(TimeInMillis elapsed)\n        {\n            elapsed_time_ = elapsed;\n        }\n\n        // Adds a test property to the list. The property is validated and may add\n        // a non-fatal failure if invalid (e.g., if it conflicts with reserved\n        // key names). If a property is already recorded for the same key, the\n        // value will be updated, rather than storing multiple values for the same\n        // key.  xml_element specifies the element for which the property is being\n        // recorded and is used for validation.\n        void RecordProperty(const std::string &xml_element,\n                            const TestProperty &test_property);\n\n        // Adds a failure if the key is a reserved attribute of Google Test\n        // testcase tags.  Returns true if the property is valid.\n        // TODO(russr): Validate attribute names are legal and human readable.\n        static bool ValidateTestProperty(const std::string &xml_element,\n                                         const TestProperty &test_property);\n\n        // Adds a test part result to the list.\n        void AddTestPartResult(const TestPartResult &test_part_result);\n\n        // Returns the death test count.\n        int death_test_count() const\n        {\n            return death_test_count_;\n        }\n\n        // Increments the death test count, returning the new count.\n        int increment_death_test_count()\n        {\n            return ++death_test_count_;\n        }\n\n        // Clears the test part results.\n        void ClearTestPartResults();\n\n        // Clears the object.\n        void Clear();\n\n        // Protects mutable state of the property vector and of owned\n        // properties, whose values may be updated.\n        internal::Mutex test_properites_mutex_;\n\n        // The vector of TestPartResults\n        std::vector<TestPartResult> test_part_results_;\n        // The vector of TestProperties\n        std::vector<TestProperty> test_properties_;\n        // Running count of death tests.\n        int death_test_count_;\n        // The elapsed time, in milliseconds.\n        TimeInMillis elapsed_time_;\n\n        // We disallow copying TestResult.\n        GTEST_DISALLOW_COPY_AND_ASSIGN_(TestResult);\n    };  // class TestResult\n\n// A TestInfo object stores the following information about a test:\n//\n//   Test case name\n//   Test name\n//   Whether the test should be run\n//   A function pointer that creates the test object when invoked\n//   Test result\n//\n// The constructor of TestInfo registers itself with the UnitTest\n// singleton such that the RUN_ALL_TESTS() macro knows which tests to\n// run.\n    class GTEST_API_ TestInfo\n    {\n    public:\n        // Destructs a TestInfo object.  This function is not virtual, so\n        // don't inherit from TestInfo.\n        ~TestInfo();\n\n        // Returns the test case name.\n        const char *test_case_name() const\n        {\n            return test_case_name_.c_str();\n        }\n\n        // Returns the test name.\n        const char *name() const\n        {\n            return name_.c_str();\n        }\n\n        // Returns the name of the parameter type, or NULL if this is not a typed\n        // or a type-parameterized test.\n        const char *type_param() const\n        {\n            if (type_param_.get() != NULL)\n                return type_param_->c_str();\n            return NULL;\n        }\n\n        // Returns the text representation of the value parameter, or NULL if this\n        // is not a value-parameterized test.\n        const char *value_param() const\n        {\n            if (value_param_.get() != NULL)\n                return value_param_->c_str();\n            return NULL;\n        }\n\n        // Returns the file name where this test is defined.\n        const char *file() const\n        {\n            return location_.file.c_str();\n        }\n\n        // Returns the line where this test is defined.\n        int line() const\n        {\n            return location_.line;\n        }\n\n        // Returns true if this test should run, that is if the test is not\n        // disabled (or it is disabled but the also_run_disabled_tests flag has\n        // been specified) and its full name matches the user-specified filter.\n        //\n        // Google Test allows the user to filter the tests by their full names.\n        // The full name of a test Bar in test case Foo is defined as\n        // \"Foo.Bar\".  Only the tests that match the filter will run.\n        //\n        // A filter is a colon-separated list of glob (not regex) patterns,\n        // optionally followed by a '-' and a colon-separated list of\n        // negative patterns (tests to exclude).  A test is run if it\n        // matches one of the positive patterns and does not match any of\n        // the negative patterns.\n        //\n        // For example, *A*:Foo.* is a filter that matches any string that\n        // contains the character 'A' or starts with \"Foo.\".\n        bool should_run() const\n        {\n            return should_run_;\n        }\n\n        // Returns true iff this test will appear in the XML report.\n        bool is_reportable() const\n        {\n            // For now, the XML report includes all tests matching the filter.\n            // In the future, we may trim tests that are excluded because of\n            // sharding.\n            return matches_filter_;\n        }\n\n        // Returns the result of the test.\n        const TestResult *result() const\n        {\n            return &result_;\n        }\n\n    private:\n#if GTEST_HAS_DEATH_TEST\n        friend class internal::DefaultDeathTestFactory;\n#endif  // GTEST_HAS_DEATH_TEST\n        friend class Test;\n        friend class TestCase;\n        friend class internal::UnitTestImpl;\n        friend class internal::StreamingListenerTest;\n        friend TestInfo *internal::MakeAndRegisterTestInfo(\n            const char *test_case_name,\n            const char *name,\n            const char *type_param,\n            const char *value_param,\n            internal::CodeLocation code_location,\n            internal::TypeId fixture_class_id,\n            Test::SetUpTestCaseFunc set_up_tc,\n            Test::TearDownTestCaseFunc tear_down_tc,\n            internal::TestFactoryBase *factory);\n\n        // Constructs a TestInfo object. The newly constructed instance assumes\n        // ownership of the factory object.\n        TestInfo(const std::string &test_case_name,\n                 const std::string &name,\n                 const char *a_type_param,   // NULL if not a type-parameterized test\n                 const char *a_value_param,  // NULL if not a value-parameterized test\n                 internal::CodeLocation a_code_location,\n                 internal::TypeId fixture_class_id,\n                 internal::TestFactoryBase *factory);\n\n        // Increments the number of death tests encountered in this test so\n        // far.\n        int increment_death_test_count()\n        {\n            return result_.increment_death_test_count();\n        }\n\n        // Creates the test object, runs it, records its result, and then\n        // deletes it.\n        void Run();\n\n        static void ClearTestResult(TestInfo *test_info)\n        {\n            test_info->result_.Clear();\n        }\n\n        // These fields are immutable properties of the test.\n        const std::string test_case_name_;     // Test case name\n        const std::string name_;               // Test name\n        // Name of the parameter type, or NULL if this is not a typed or a\n        // type-parameterized test.\n        const internal::scoped_ptr<const ::std::string> type_param_;\n        // Text representation of the value parameter, or NULL if this is not a\n        // value-parameterized test.\n        const internal::scoped_ptr<const ::std::string> value_param_;\n        internal::CodeLocation location_;\n        const internal::TypeId fixture_class_id_;   // ID of the test fixture class\n        bool should_run_;                 // True iff this test should run\n        bool is_disabled_;                // True iff this test is disabled\n        bool matches_filter_;             // True if this test matches the\n        // user-specified filter.\n        internal::TestFactoryBase *const factory_;  // The factory that creates\n        // the test object\n\n        // This field is mutable and needs to be reset before running the\n        // test for the second time.\n        TestResult result_;\n\n        GTEST_DISALLOW_COPY_AND_ASSIGN_(TestInfo);\n    };\n\n// A test case, which consists of a vector of TestInfos.\n//\n// TestCase is not copyable.\n    class GTEST_API_ TestCase\n    {\n    public:\n        // Creates a TestCase with the given name.\n        //\n        // TestCase does NOT have a default constructor.  Always use this\n        // constructor to create a TestCase object.\n        //\n        // Arguments:\n        //\n        //   name:         name of the test case\n        //   a_type_param: the name of the test's type parameter, or NULL if\n        //                 this is not a type-parameterized test.\n        //   set_up_tc:    pointer to the function that sets up the test case\n        //   tear_down_tc: pointer to the function that tears down the test case\n        TestCase(const char *name, const char *a_type_param,\n                 Test::SetUpTestCaseFunc set_up_tc,\n                 Test::TearDownTestCaseFunc tear_down_tc);\n\n        // Destructor of TestCase.\n        virtual ~TestCase();\n\n        // Gets the name of the TestCase.\n        const char *name() const\n        {\n            return name_.c_str();\n        }\n\n        // Returns the name of the parameter type, or NULL if this is not a\n        // type-parameterized test case.\n        const char *type_param() const\n        {\n            if (type_param_.get() != NULL)\n                return type_param_->c_str();\n            return NULL;\n        }\n\n        // Returns true if any test in this test case should run.\n        bool should_run() const\n        {\n            return should_run_;\n        }\n\n        // Gets the number of successful tests in this test case.\n        int successful_test_count() const;\n\n        // Gets the number of failed tests in this test case.\n        int failed_test_count() const;\n\n        // Gets the number of disabled tests that will be reported in the XML report.\n        int reportable_disabled_test_count() const;\n\n        // Gets the number of disabled tests in this test case.\n        int disabled_test_count() const;\n\n        // Gets the number of tests to be printed in the XML report.\n        int reportable_test_count() const;\n\n        // Get the number of tests in this test case that should run.\n        int test_to_run_count() const;\n\n        // Gets the number of all tests in this test case.\n        int total_test_count() const;\n\n        // Returns true iff the test case passed.\n        bool Passed() const\n        {\n            return !Failed();\n        }\n\n        // Returns true iff the test case failed.\n        bool Failed() const\n        {\n            return failed_test_count() > 0;\n        }\n\n        // Returns the elapsed time, in milliseconds.\n        TimeInMillis elapsed_time() const\n        {\n            return elapsed_time_;\n        }\n\n        // Returns the i-th test among all the tests. i can range from 0 to\n        // total_test_count() - 1. If i is not in that range, returns NULL.\n        const TestInfo *GetTestInfo(int i) const;\n\n        // Returns the TestResult that holds test properties recorded during\n        // execution of SetUpTestCase and TearDownTestCase.\n        const TestResult &ad_hoc_test_result() const\n        {\n            return ad_hoc_test_result_;\n        }\n\n    private:\n        friend class Test;\n        friend class internal::UnitTestImpl;\n\n        // Gets the (mutable) vector of TestInfos in this TestCase.\n        std::vector<TestInfo *> &test_info_list()\n        {\n            return test_info_list_;\n        }\n\n        // Gets the (immutable) vector of TestInfos in this TestCase.\n        const std::vector<TestInfo *> &test_info_list() const\n        {\n            return test_info_list_;\n        }\n\n        // Returns the i-th test among all the tests. i can range from 0 to\n        // total_test_count() - 1. If i is not in that range, returns NULL.\n        TestInfo *GetMutableTestInfo(int i);\n\n        // Sets the should_run member.\n        void set_should_run(bool should)\n        {\n            should_run_ = should;\n        }\n\n        // Adds a TestInfo to this test case.  Will delete the TestInfo upon\n        // destruction of the TestCase object.\n        void AddTestInfo(TestInfo *test_info);\n\n        // Clears the results of all tests in this test case.\n        void ClearResult();\n\n        // Clears the results of all tests in the given test case.\n        static void ClearTestCaseResult(TestCase *test_case)\n        {\n            test_case->ClearResult();\n        }\n\n        // Runs every test in this TestCase.\n        void Run();\n\n        // Runs SetUpTestCase() for this TestCase.  This wrapper is needed\n        // for catching exceptions thrown from SetUpTestCase().\n        void RunSetUpTestCase()\n        {\n            (*set_up_tc_)();\n        }\n\n        // Runs TearDownTestCase() for this TestCase.  This wrapper is\n        // needed for catching exceptions thrown from TearDownTestCase().\n        void RunTearDownTestCase()\n        {\n            (*tear_down_tc_)();\n        }\n\n        // Returns true iff test passed.\n        static bool TestPassed(const TestInfo *test_info)\n        {\n            return test_info->should_run() && test_info->result()->Passed();\n        }\n\n        // Returns true iff test failed.\n        static bool TestFailed(const TestInfo *test_info)\n        {\n            return test_info->should_run() && test_info->result()->Failed();\n        }\n\n        // Returns true iff the test is disabled and will be reported in the XML\n        // report.\n        static bool TestReportableDisabled(const TestInfo *test_info)\n        {\n            return test_info->is_reportable() && test_info->is_disabled_;\n        }\n\n        // Returns true iff test is disabled.\n        static bool TestDisabled(const TestInfo *test_info)\n        {\n            return test_info->is_disabled_;\n        }\n\n        // Returns true iff this test will appear in the XML report.\n        static bool TestReportable(const TestInfo *test_info)\n        {\n            return test_info->is_reportable();\n        }\n\n        // Returns true if the given test should run.\n        static bool ShouldRunTest(const TestInfo *test_info)\n        {\n            return test_info->should_run();\n        }\n\n        // Shuffles the tests in this test case.\n        void ShuffleTests(internal::Random *random);\n\n        // Restores the test order to before the first shuffle.\n        void UnshuffleTests();\n\n        // Name of the test case.\n        std::string name_;\n        // Name of the parameter type, or NULL if this is not a typed or a\n        // type-parameterized test.\n        const internal::scoped_ptr<const ::std::string> type_param_;\n        // The vector of TestInfos in their original order.  It owns the\n        // elements in the vector.\n        std::vector<TestInfo *> test_info_list_;\n        // Provides a level of indirection for the test list to allow easy\n        // shuffling and restoring the test order.  The i-th element in this\n        // vector is the index of the i-th test in the shuffled test list.\n        std::vector<int> test_indices_;\n        // Pointer to the function that sets up the test case.\n        Test::SetUpTestCaseFunc set_up_tc_;\n        // Pointer to the function that tears down the test case.\n        Test::TearDownTestCaseFunc tear_down_tc_;\n        // True iff any test in this test case should run.\n        bool should_run_;\n        // Elapsed time, in milliseconds.\n        TimeInMillis elapsed_time_;\n        // Holds test properties recorded during execution of SetUpTestCase and\n        // TearDownTestCase.\n        TestResult ad_hoc_test_result_;\n\n        // We disallow copying TestCases.\n        GTEST_DISALLOW_COPY_AND_ASSIGN_(TestCase);\n    };\n\n// An Environment object is capable of setting up and tearing down an\n// environment.  You should subclass this to define your own\n// environment(s).\n//\n// An Environment object does the set-up and tear-down in virtual\n// methods SetUp() and TearDown() instead of the constructor and the\n// destructor, as:\n//\n//   1. You cannot safely throw from a destructor.  This is a problem\n//      as in some cases Google Test is used where exceptions are enabled, and\n//      we may want to implement ASSERT_* using exceptions where they are\n//      available.\n//   2. You cannot use ASSERT_* directly in a constructor or\n//      destructor.\n    class Environment\n    {\n    public:\n        // The d'tor is virtual as we need to subclass Environment.\n        virtual ~Environment() {}\n\n        // Override this to define how to set up the environment.\n        virtual void SetUp() {}\n\n        // Override this to define how to tear down the environment.\n        virtual void TearDown() {}\n    private:\n        // If you see an error about overriding the following function or\n        // about it being private, you have mis-spelled SetUp() as Setup().\n        struct Setup_should_be_spelled_SetUp {};\n        virtual Setup_should_be_spelled_SetUp *Setup()\n        {\n            return NULL;\n        }\n    };\n\n// The interface for tracing execution of tests. The methods are organized in\n// the order the corresponding events are fired.\n    class TestEventListener\n    {\n    public:\n        virtual ~TestEventListener() {}\n\n        // Fired before any test activity starts.\n        virtual void OnTestProgramStart(const UnitTest &unit_test) = 0;\n\n        // Fired before each iteration of tests starts.  There may be more than\n        // one iteration if GTEST_FLAG(repeat) is set. iteration is the iteration\n        // index, starting from 0.\n        virtual void OnTestIterationStart(const UnitTest &unit_test,\n                                          int iteration) = 0;\n\n        // Fired before environment set-up for each iteration of tests starts.\n        virtual void OnEnvironmentsSetUpStart(const UnitTest &unit_test) = 0;\n\n        // Fired after environment set-up for each iteration of tests ends.\n        virtual void OnEnvironmentsSetUpEnd(const UnitTest &unit_test) = 0;\n\n        // Fired before the test case starts.\n        virtual void OnTestCaseStart(const TestCase &test_case) = 0;\n\n        // Fired before the test starts.\n        virtual void OnTestStart(const TestInfo &test_info) = 0;\n\n        // Fired after a failed assertion or a SUCCEED() invocation.\n        virtual void OnTestPartResult(const TestPartResult &test_part_result) = 0;\n\n        // Fired after the test ends.\n        virtual void OnTestEnd(const TestInfo &test_info) = 0;\n\n        // Fired after the test case ends.\n        virtual void OnTestCaseEnd(const TestCase &test_case) = 0;\n\n        // Fired before environment tear-down for each iteration of tests starts.\n        virtual void OnEnvironmentsTearDownStart(const UnitTest &unit_test) = 0;\n\n        // Fired after environment tear-down for each iteration of tests ends.\n        virtual void OnEnvironmentsTearDownEnd(const UnitTest &unit_test) = 0;\n\n        // Fired after each iteration of tests finishes.\n        virtual void OnTestIterationEnd(const UnitTest &unit_test,\n                                        int iteration) = 0;\n\n        // Fired after all test activities have ended.\n        virtual void OnTestProgramEnd(const UnitTest &unit_test) = 0;\n    };\n\n// The convenience class for users who need to override just one or two\n// methods and are not concerned that a possible change to a signature of\n// the methods they override will not be caught during the build.  For\n// comments about each method please see the definition of TestEventListener\n// above.\n    class EmptyTestEventListener : public TestEventListener\n    {\n    public:\n        virtual void OnTestProgramStart(const UnitTest & /*unit_test*/) {}\n        virtual void OnTestIterationStart(const UnitTest & /*unit_test*/,\n                                          int /*iteration*/) {}\n        virtual void OnEnvironmentsSetUpStart(const UnitTest & /*unit_test*/) {}\n        virtual void OnEnvironmentsSetUpEnd(const UnitTest & /*unit_test*/) {}\n        virtual void OnTestCaseStart(const TestCase & /*test_case*/) {}\n        virtual void OnTestStart(const TestInfo & /*test_info*/) {}\n        virtual void OnTestPartResult(const TestPartResult & /*test_part_result*/) {}\n        virtual void OnTestEnd(const TestInfo & /*test_info*/) {}\n        virtual void OnTestCaseEnd(const TestCase & /*test_case*/) {}\n        virtual void OnEnvironmentsTearDownStart(const UnitTest & /*unit_test*/) {}\n        virtual void OnEnvironmentsTearDownEnd(const UnitTest & /*unit_test*/) {}\n        virtual void OnTestIterationEnd(const UnitTest & /*unit_test*/,\n                                        int /*iteration*/) {}\n        virtual void OnTestProgramEnd(const UnitTest & /*unit_test*/) {}\n    };\n\n// TestEventListeners lets users add listeners to track events in Google Test.\n    class GTEST_API_ TestEventListeners\n    {\n    public:\n        TestEventListeners();\n        ~TestEventListeners();\n\n        // Appends an event listener to the end of the list. Google Test assumes\n        // the ownership of the listener (i.e. it will delete the listener when\n        // the test program finishes).\n        void Append(TestEventListener *listener);\n\n        // Removes the given event listener from the list and returns it.  It then\n        // becomes the caller's responsibility to delete the listener. Returns\n        // NULL if the listener is not found in the list.\n        TestEventListener *Release(TestEventListener *listener);\n\n        // Returns the standard listener responsible for the default console\n        // output.  Can be removed from the listeners list to shut down default\n        // console output.  Note that removing this object from the listener list\n        // with Release transfers its ownership to the caller and makes this\n        // function return NULL the next time.\n        TestEventListener *default_result_printer() const\n        {\n            return default_result_printer_;\n        }\n\n        // Returns the standard listener responsible for the default XML output\n        // controlled by the --gtest_output=xml flag.  Can be removed from the\n        // listeners list by users who want to shut down the default XML output\n        // controlled by this flag and substitute it with custom one.  Note that\n        // removing this object from the listener list with Release transfers its\n        // ownership to the caller and makes this function return NULL the next\n        // time.\n        TestEventListener *default_xml_generator() const\n        {\n            return default_xml_generator_;\n        }\n\n    private:\n        friend class TestCase;\n        friend class TestInfo;\n        friend class internal::DefaultGlobalTestPartResultReporter;\n        friend class internal::NoExecDeathTest;\n        friend class internal::TestEventListenersAccessor;\n        friend class internal::UnitTestImpl;\n\n        // Returns repeater that broadcasts the TestEventListener events to all\n        // subscribers.\n        TestEventListener *repeater();\n\n        // Sets the default_result_printer attribute to the provided listener.\n        // The listener is also added to the listener list and previous\n        // default_result_printer is removed from it and deleted. The listener can\n        // also be NULL in which case it will not be added to the list. Does\n        // nothing if the previous and the current listener objects are the same.\n        void SetDefaultResultPrinter(TestEventListener *listener);\n\n        // Sets the default_xml_generator attribute to the provided listener.  The\n        // listener is also added to the listener list and previous\n        // default_xml_generator is removed from it and deleted. The listener can\n        // also be NULL in which case it will not be added to the list. Does\n        // nothing if the previous and the current listener objects are the same.\n        void SetDefaultXmlGenerator(TestEventListener *listener);\n\n        // Controls whether events will be forwarded by the repeater to the\n        // listeners in the list.\n        bool EventForwardingEnabled() const;\n        void SuppressEventForwarding();\n\n        // The actual list of listeners.\n        internal::TestEventRepeater *repeater_;\n        // Listener responsible for the standard result output.\n        TestEventListener *default_result_printer_;\n        // Listener responsible for the creation of the XML output file.\n        TestEventListener *default_xml_generator_;\n\n        // We disallow copying TestEventListeners.\n        GTEST_DISALLOW_COPY_AND_ASSIGN_(TestEventListeners);\n    };\n\n// A UnitTest consists of a vector of TestCases.\n//\n// This is a singleton class.  The only instance of UnitTest is\n// created when UnitTest::GetInstance() is first called.  This\n// instance is never deleted.\n//\n// UnitTest is not copyable.\n//\n// This class is thread-safe as long as the methods are called\n// according to their specification.\n    class GTEST_API_ UnitTest\n    {\n    public:\n        // Gets the singleton UnitTest object.  The first time this method\n        // is called, a UnitTest object is constructed and returned.\n        // Consecutive calls will return the same object.\n        static UnitTest *GetInstance();\n\n        // Runs all tests in this UnitTest object and prints the result.\n        // Returns 0 if successful, or 1 otherwise.\n        //\n        // This method can only be called from the main thread.\n        //\n        // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.\n        int Run() GTEST_MUST_USE_RESULT_;\n\n        // Returns the working directory when the first TEST() or TEST_F()\n        // was executed.  The UnitTest object owns the string.\n        const char *original_working_dir() const;\n\n        // Returns the TestCase object for the test that's currently running,\n        // or NULL if no test is running.\n        const TestCase *current_test_case() const\n        GTEST_LOCK_EXCLUDED_(mutex_);\n\n        // Returns the TestInfo object for the test that's currently running,\n        // or NULL if no test is running.\n        const TestInfo *current_test_info() const\n        GTEST_LOCK_EXCLUDED_(mutex_);\n\n        // Returns the random seed used at the start of the current test run.\n        int random_seed() const;\n\n#if GTEST_HAS_PARAM_TEST\n        // Returns the ParameterizedTestCaseRegistry object used to keep track of\n        // value-parameterized tests and instantiate and register them.\n        //\n        // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.\n        internal::ParameterizedTestCaseRegistry &parameterized_test_registry()\n        GTEST_LOCK_EXCLUDED_(mutex_);\n#endif  // GTEST_HAS_PARAM_TEST\n\n        // Gets the number of successful test cases.\n        int successful_test_case_count() const;\n\n        // Gets the number of failed test cases.\n        int failed_test_case_count() const;\n\n        // Gets the number of all test cases.\n        int total_test_case_count() const;\n\n        // Gets the number of all test cases that contain at least one test\n        // that should run.\n        int test_case_to_run_count() const;\n\n        // Gets the number of successful tests.\n        int successful_test_count() const;\n\n        // Gets the number of failed tests.\n        int failed_test_count() const;\n\n        // Gets the number of disabled tests that will be reported in the XML report.\n        int reportable_disabled_test_count() const;\n\n        // Gets the number of disabled tests.\n        int disabled_test_count() const;\n\n        // Gets the number of tests to be printed in the XML report.\n        int reportable_test_count() const;\n\n        // Gets the number of all tests.\n        int total_test_count() const;\n\n        // Gets the number of tests that should run.\n        int test_to_run_count() const;\n\n        // Gets the time of the test program start, in ms from the start of the\n        // UNIX epoch.\n        TimeInMillis start_timestamp() const;\n\n        // Gets the elapsed time, in milliseconds.\n        TimeInMillis elapsed_time() const;\n\n        // Returns true iff the unit test passed (i.e. all test cases passed).\n        bool Passed() const;\n\n        // Returns true iff the unit test failed (i.e. some test case failed\n        // or something outside of all tests failed).\n        bool Failed() const;\n\n        // Gets the i-th test case among all the test cases. i can range from 0 to\n        // total_test_case_count() - 1. If i is not in that range, returns NULL.\n        const TestCase *GetTestCase(int i) const;\n\n        // Returns the TestResult containing information on test failures and\n        // properties logged outside of individual test cases.\n        const TestResult &ad_hoc_test_result() const;\n\n        // Returns the list of event listeners that can be used to track events\n        // inside Google Test.\n        TestEventListeners &listeners();\n\n    private:\n        // Registers and returns a global test environment.  When a test\n        // program is run, all global test environments will be set-up in\n        // the order they were registered.  After all tests in the program\n        // have finished, all global test environments will be torn-down in\n        // the *reverse* order they were registered.\n        //\n        // The UnitTest object takes ownership of the given environment.\n        //\n        // This method can only be called from the main thread.\n        Environment *AddEnvironment(Environment *env);\n\n        // Adds a TestPartResult to the current TestResult object.  All\n        // Google Test assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc)\n        // eventually call this to report their results.  The user code\n        // should use the assertion macros instead of calling this directly.\n        void AddTestPartResult(TestPartResult::Type result_type,\n                               const char *file_name,\n                               int line_number,\n                               const std::string &message,\n                               const std::string &os_stack_trace)\n        GTEST_LOCK_EXCLUDED_(mutex_);\n\n        // Adds a TestProperty to the current TestResult object when invoked from\n        // inside a test, to current TestCase's ad_hoc_test_result_ when invoked\n        // from SetUpTestCase or TearDownTestCase, or to the global property set\n        // when invoked elsewhere.  If the result already contains a property with\n        // the same key, the value will be updated.\n        void RecordProperty(const std::string &key, const std::string &value);\n\n        // Gets the i-th test case among all the test cases. i can range from 0 to\n        // total_test_case_count() - 1. If i is not in that range, returns NULL.\n        TestCase *GetMutableTestCase(int i);\n\n        // Accessors for the implementation object.\n        internal::UnitTestImpl *impl()\n        {\n            return impl_;\n        }\n        const internal::UnitTestImpl *impl() const\n        {\n            return impl_;\n        }\n\n        // These classes and functions are friends as they need to access private\n        // members of UnitTest.\n        friend class Test;\n        friend class internal::AssertHelper;\n        friend class internal::ScopedTrace;\n        friend class internal::StreamingListenerTest;\n        friend class internal::UnitTestRecordPropertyTestHelper;\n        friend Environment *AddGlobalTestEnvironment(Environment *env);\n        friend internal::UnitTestImpl *internal::GetUnitTestImpl();\n        friend void internal::ReportFailureInUnknownLocation(\n            TestPartResult::Type result_type,\n            const std::string &message);\n\n        // Creates an empty UnitTest.\n        UnitTest();\n\n        // D'tor\n        virtual ~UnitTest();\n\n        // Pushes a trace defined by SCOPED_TRACE() on to the per-thread\n        // Google Test trace stack.\n        void PushGTestTrace(const internal::TraceInfo &trace)\n        GTEST_LOCK_EXCLUDED_(mutex_);\n\n        // Pops a trace from the per-thread Google Test trace stack.\n        void PopGTestTrace()\n        GTEST_LOCK_EXCLUDED_(mutex_);\n\n        // Protects mutable state in *impl_.  This is mutable as some const\n        // methods need to lock it too.\n        mutable internal::Mutex mutex_;\n\n        // Opaque implementation object.  This field is never changed once\n        // the object is constructed.  We don't mark it as const here, as\n        // doing so will cause a warning in the constructor of UnitTest.\n        // Mutable state in *impl_ is protected by mutex_.\n        internal::UnitTestImpl *impl_;\n\n        // We disallow copying UnitTest.\n        GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTest);\n    };\n\n// A convenient wrapper for adding an environment for the test\n// program.\n//\n// You should call this before RUN_ALL_TESTS() is called, probably in\n// main().  If you use gtest_main, you need to call this before main()\n// starts for it to take effect.  For example, you can define a global\n// variable like this:\n//\n//   testing::Environment* const foo_env =\n//       testing::AddGlobalTestEnvironment(new FooEnvironment);\n//\n// However, we strongly recommend you to write your own main() and\n// call AddGlobalTestEnvironment() there, as relying on initialization\n// of global variables makes the code harder to read and may cause\n// problems when you register multiple environments from different\n// translation units and the environments have dependencies among them\n// (remember that the compiler doesn't guarantee the order in which\n// global variables from different translation units are initialized).\n    inline Environment *AddGlobalTestEnvironment(Environment *env)\n    {\n        return UnitTest::GetInstance()->AddEnvironment(env);\n    }\n\n// Initializes Google Test.  This must be called before calling\n// RUN_ALL_TESTS().  In particular, it parses a command line for the\n// flags that Google Test recognizes.  Whenever a Google Test flag is\n// seen, it is removed from argv, and *argc is decremented.\n//\n// No value is returned.  Instead, the Google Test flag variables are\n// updated.\n//\n// Calling the function for the second time has no user-visible effect.\n    GTEST_API_ void InitGoogleTest(int *argc, char **argv);\n\n// This overloaded version can be used in Windows programs compiled in\n// UNICODE mode.\n    GTEST_API_ void InitGoogleTest(int *argc, wchar_t **argv);\n    GTEST_API_ bool GetGtestHelpFlag();\n\n    namespace internal\n    {\n\n// Separate the error generating code from the code path to reduce the stack\n// frame size of CmpHelperEQ. This helps reduce the overhead of some sanitizers\n// when calling EXPECT_* in a tight loop.\n        template <typename T1, typename T2>\n        AssertionResult CmpHelperEQFailure(const char *lhs_expression,\n                                           const char *rhs_expression,\n                                           const T1 &lhs, const T2 &rhs)\n        {\n            return EqFailure(lhs_expression,\n                             rhs_expression,\n                             FormatForComparisonFailureMessage(lhs, rhs),\n                             FormatForComparisonFailureMessage(rhs, lhs),\n                             false);\n        }\n\n// The helper function for {ASSERT|EXPECT}_EQ.\n        template <typename T1, typename T2>\n        AssertionResult CmpHelperEQ(const char *lhs_expression,\n                                    const char *rhs_expression,\n                                    const T1 &lhs,\n                                    const T2 &rhs)\n        {\n            GTEST_DISABLE_MSC_WARNINGS_PUSH_(4389 /* signed/unsigned mismatch */)\n            if (lhs == rhs) {\n                return AssertionSuccess();\n            }\n            GTEST_DISABLE_MSC_WARNINGS_POP_()\n\n            return CmpHelperEQFailure(lhs_expression, rhs_expression, lhs, rhs);\n        }\n\n// With this overloaded version, we allow anonymous enums to be used\n// in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous enums\n// can be implicitly cast to BiggestInt.\n        GTEST_API_ AssertionResult CmpHelperEQ(const char *lhs_expression,\n                                               const char *rhs_expression,\n                                               BiggestInt lhs,\n                                               BiggestInt rhs);\n\n// The helper class for {ASSERT|EXPECT}_EQ.  The template argument\n// lhs_is_null_literal is true iff the first argument to ASSERT_EQ()\n// is a null pointer literal.  The following default implementation is\n// for lhs_is_null_literal being false.\n        template <bool lhs_is_null_literal>\n        class EqHelper\n        {\n        public:\n            // This templatized version is for the general case.\n            template <typename T1, typename T2>\n            static AssertionResult Compare(const char *lhs_expression,\n                                           const char *rhs_expression,\n                                           const T1 &lhs,\n                                           const T2 &rhs)\n            {\n                return CmpHelperEQ(lhs_expression, rhs_expression, lhs, rhs);\n            }\n\n            // With this overloaded version, we allow anonymous enums to be used\n            // in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous\n            // enums can be implicitly cast to BiggestInt.\n            //\n            // Even though its body looks the same as the above version, we\n            // cannot merge the two, as it will make anonymous enums unhappy.\n            static AssertionResult Compare(const char *lhs_expression,\n                                           const char *rhs_expression,\n                                           BiggestInt lhs,\n                                           BiggestInt rhs)\n            {\n                return CmpHelperEQ(lhs_expression, rhs_expression, lhs, rhs);\n            }\n        };\n\n// This specialization is used when the first argument to ASSERT_EQ()\n// is a null pointer literal, like NULL, false, or 0.\n        template <>\n        class EqHelper<true>\n        {\n        public:\n            // We define two overloaded versions of Compare().  The first\n            // version will be picked when the second argument to ASSERT_EQ() is\n            // NOT a pointer, e.g. ASSERT_EQ(0, AnIntFunction()) or\n            // EXPECT_EQ(false, a_bool).\n            template <typename T1, typename T2>\n            static AssertionResult Compare(\n                const char *lhs_expression,\n                const char *rhs_expression,\n                const T1 &lhs,\n                const T2 &rhs,\n                // The following line prevents this overload from being considered if T2\n                // is not a pointer type.  We need this because ASSERT_EQ(NULL, my_ptr)\n                // expands to Compare(\"\", \"\", NULL, my_ptr), which requires a conversion\n                // to match the Secret* in the other overload, which would otherwise make\n                // this template match better.\n                typename EnableIf<!is_pointer<T2>::value>::type * = 0)\n            {\n                return CmpHelperEQ(lhs_expression, rhs_expression, lhs, rhs);\n            }\n\n            // This version will be picked when the second argument to ASSERT_EQ() is a\n            // pointer, e.g. ASSERT_EQ(NULL, a_pointer).\n            template <typename T>\n            static AssertionResult Compare(\n                const char *lhs_expression,\n                const char *rhs_expression,\n                // We used to have a second template parameter instead of Secret*.  That\n                // template parameter would deduce to 'long', making this a better match\n                // than the first overload even without the first overload's EnableIf.\n                // Unfortunately, gcc with -Wconversion-null warns when \"passing NULL to\n                // non-pointer argument\" (even a deduced integral argument), so the old\n                // implementation caused warnings in user code.\n                Secret * /* lhs (NULL) */,\n                T *rhs)\n            {\n                // We already know that 'lhs' is a null pointer.\n                return CmpHelperEQ(lhs_expression, rhs_expression,\n                                   static_cast<T *>(NULL), rhs);\n            }\n        };\n\n// Separate the error generating code from the code path to reduce the stack\n// frame size of CmpHelperOP. This helps reduce the overhead of some sanitizers\n// when calling EXPECT_OP in a tight loop.\n        template <typename T1, typename T2>\n        AssertionResult CmpHelperOpFailure(const char *expr1, const char *expr2,\n                                           const T1 &val1, const T2 &val2,\n                                           const char *op)\n        {\n            return AssertionFailure()\n                   << \"Expected: (\" << expr1 << \") \" << op << \" (\" << expr2\n                   << \"), actual: \" << FormatForComparisonFailureMessage(val1, val2)\n                   << \" vs \" << FormatForComparisonFailureMessage(val2, val1);\n        }\n\n// A macro for implementing the helper functions needed to implement\n// ASSERT_?? and EXPECT_??.  It is here just to avoid copy-and-paste\n// of similar code.\n//\n// For each templatized helper function, we also define an overloaded\n// version for BiggestInt in order to reduce code bloat and allow\n// anonymous enums to be used with {ASSERT|EXPECT}_?? when compiled\n// with gcc 4.\n//\n// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.\n\n#define GTEST_IMPL_CMP_HELPER_(op_name, op)\\\ntemplate <typename T1, typename T2>\\\nAssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \\\n                                   const T1& val1, const T2& val2) {\\\n  if (val1 op val2) {\\\n    return AssertionSuccess();\\\n  } else {\\\n    return CmpHelperOpFailure(expr1, expr2, val1, val2, #op);\\\n  }\\\n}\\\nGTEST_API_ AssertionResult CmpHelper##op_name(\\\n    const char* expr1, const char* expr2, BiggestInt val1, BiggestInt val2)\n\n// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.\n\n// Implements the helper function for {ASSERT|EXPECT}_NE\n        GTEST_IMPL_CMP_HELPER_(NE, !=);\n// Implements the helper function for {ASSERT|EXPECT}_LE\n        GTEST_IMPL_CMP_HELPER_(LE, <=);\n// Implements the helper function for {ASSERT|EXPECT}_LT\n        GTEST_IMPL_CMP_HELPER_(LT, <);\n// Implements the helper function for {ASSERT|EXPECT}_GE\n        GTEST_IMPL_CMP_HELPER_(GE, >=);\n// Implements the helper function for {ASSERT|EXPECT}_GT\n        GTEST_IMPL_CMP_HELPER_(GT, >);\n\n#undef GTEST_IMPL_CMP_HELPER_\n\n// The helper function for {ASSERT|EXPECT}_STREQ.\n//\n// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.\n        GTEST_API_ AssertionResult CmpHelperSTREQ(const char *s1_expression,\n                                                  const char *s2_expression,\n                                                  const char *s1,\n                                                  const char *s2);\n\n// The helper function for {ASSERT|EXPECT}_STRCASEEQ.\n//\n// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.\n        GTEST_API_ AssertionResult CmpHelperSTRCASEEQ(const char *s1_expression,\n                                                      const char *s2_expression,\n                                                      const char *s1,\n                                                      const char *s2);\n\n// The helper function for {ASSERT|EXPECT}_STRNE.\n//\n// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.\n        GTEST_API_ AssertionResult CmpHelperSTRNE(const char *s1_expression,\n                                                  const char *s2_expression,\n                                                  const char *s1,\n                                                  const char *s2);\n\n// The helper function for {ASSERT|EXPECT}_STRCASENE.\n//\n// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.\n        GTEST_API_ AssertionResult CmpHelperSTRCASENE(const char *s1_expression,\n                                                      const char *s2_expression,\n                                                      const char *s1,\n                                                      const char *s2);\n\n\n// Helper function for *_STREQ on wide strings.\n//\n// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.\n        GTEST_API_ AssertionResult CmpHelperSTREQ(const char *s1_expression,\n                                                  const char *s2_expression,\n                                                  const wchar_t *s1,\n                                                  const wchar_t *s2);\n\n// Helper function for *_STRNE on wide strings.\n//\n// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.\n        GTEST_API_ AssertionResult CmpHelperSTRNE(const char *s1_expression,\n                                                  const char *s2_expression,\n                                                  const wchar_t *s1,\n                                                  const wchar_t *s2);\n\n    }  // namespace internal\n\n// IsSubstring() and IsNotSubstring() are intended to be used as the\n// first argument to {EXPECT,ASSERT}_PRED_FORMAT2(), not by\n// themselves.  They check whether needle is a substring of haystack\n// (NULL is considered a substring of itself only), and return an\n// appropriate error message when they fail.\n//\n// The {needle,haystack}_expr arguments are the stringified\n// expressions that generated the two real arguments.\n    GTEST_API_ AssertionResult IsSubstring(\n        const char *needle_expr, const char *haystack_expr,\n        const char *needle, const char *haystack);\n    GTEST_API_ AssertionResult IsSubstring(\n        const char *needle_expr, const char *haystack_expr,\n        const wchar_t *needle, const wchar_t *haystack);\n    GTEST_API_ AssertionResult IsNotSubstring(\n        const char *needle_expr, const char *haystack_expr,\n        const char *needle, const char *haystack);\n    GTEST_API_ AssertionResult IsNotSubstring(\n        const char *needle_expr, const char *haystack_expr,\n        const wchar_t *needle, const wchar_t *haystack);\n    GTEST_API_ AssertionResult IsSubstring(\n        const char *needle_expr, const char *haystack_expr,\n        const ::std::string &needle, const ::std::string &haystack);\n    GTEST_API_ AssertionResult IsNotSubstring(\n        const char *needle_expr, const char *haystack_expr,\n        const ::std::string &needle, const ::std::string &haystack);\n\n#if GTEST_HAS_STD_WSTRING\n    GTEST_API_ AssertionResult IsSubstring(\n        const char *needle_expr, const char *haystack_expr,\n        const ::std::wstring &needle, const ::std::wstring &haystack);\n    GTEST_API_ AssertionResult IsNotSubstring(\n        const char *needle_expr, const char *haystack_expr,\n        const ::std::wstring &needle, const ::std::wstring &haystack);\n#endif  // GTEST_HAS_STD_WSTRING\n\n    namespace internal\n    {\n\n// Helper template function for comparing floating-points.\n//\n// Template parameter:\n//\n//   RawType: the raw floating-point type (either float or double)\n//\n// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.\n        template <typename RawType>\n        AssertionResult CmpHelperFloatingPointEQ(const char *lhs_expression,\n                                                 const char *rhs_expression,\n                                                 RawType lhs_value,\n                                                 RawType rhs_value)\n        {\n            const FloatingPoint<RawType> lhs(lhs_value), rhs(rhs_value);\n\n            if (lhs.AlmostEquals(rhs)) {\n                return AssertionSuccess();\n            }\n\n            ::std::stringstream lhs_ss;\n            lhs_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)\n                   << lhs_value;\n\n            ::std::stringstream rhs_ss;\n            rhs_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)\n                   << rhs_value;\n\n            return EqFailure(lhs_expression,\n                             rhs_expression,\n                             StringStreamToString(&lhs_ss),\n                             StringStreamToString(&rhs_ss),\n                             false);\n        }\n\n// Helper function for implementing ASSERT_NEAR.\n//\n// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.\n        GTEST_API_ AssertionResult DoubleNearPredFormat(const char *expr1,\n                                                        const char *expr2,\n                                                        const char *abs_error_expr,\n                                                        double val1,\n                                                        double val2,\n                                                        double abs_error);\n\n// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.\n// A class that enables one to stream messages to assertion macros\n        class GTEST_API_ AssertHelper\n        {\n        public:\n            // Constructor.\n            AssertHelper(TestPartResult::Type type,\n                         const char *file,\n                         int line,\n                         const char *message);\n            ~AssertHelper();\n\n            // Message assignment is a semantic trick to enable assertion\n            // streaming; see the GTEST_MESSAGE_ macro below.\n            void operator=(const Message &message) const;\n\n        private:\n            // We put our data in a struct so that the size of the AssertHelper class can\n            // be as small as possible.  This is important because gcc is incapable of\n            // re-using stack space even for temporary variables, so every EXPECT_EQ\n            // reserves stack space for another AssertHelper.\n            struct AssertHelperData {\n                AssertHelperData(TestPartResult::Type t,\n                                 const char *srcfile,\n                                 int line_num,\n                                 const char *msg)\n                    : type(t), file(srcfile), line(line_num), message(msg) { }\n\n                TestPartResult::Type const type;\n                const char *const file;\n                int const line;\n                std::string const message;\n\n            private:\n                GTEST_DISALLOW_COPY_AND_ASSIGN_(AssertHelperData);\n            };\n\n            AssertHelperData *const data_;\n\n            GTEST_DISALLOW_COPY_AND_ASSIGN_(AssertHelper);\n        };\n\n    }  // namespace internal\n\n#if GTEST_HAS_PARAM_TEST\n// The pure interface class that all value-parameterized tests inherit from.\n// A value-parameterized class must inherit from both ::testing::Test and\n// ::testing::WithParamInterface. In most cases that just means inheriting\n// from ::testing::TestWithParam, but more complicated test hierarchies\n// may need to inherit from Test and WithParamInterface at different levels.\n//\n// This interface has support for accessing the test parameter value via\n// the GetParam() method.\n//\n// Use it with one of the parameter generator defining functions, like Range(),\n// Values(), ValuesIn(), Bool(), and Combine().\n//\n// class FooTest : public ::testing::TestWithParam<int> {\n//  protected:\n//   FooTest() {\n//     // Can use GetParam() here.\n//   }\n//   virtual ~FooTest() {\n//     // Can use GetParam() here.\n//   }\n//   virtual void SetUp() {\n//     // Can use GetParam() here.\n//   }\n//   virtual void TearDown {\n//     // Can use GetParam() here.\n//   }\n// };\n// TEST_P(FooTest, DoesBar) {\n//   // Can use GetParam() method here.\n//   Foo foo;\n//   ASSERT_TRUE(foo.DoesBar(GetParam()));\n// }\n// INSTANTIATE_TEST_CASE_P(OneToTenRange, FooTest, ::testing::Range(1, 10));\n\n    template <typename T>\n    class WithParamInterface\n    {\n    public:\n        typedef T ParamType;\n        virtual ~WithParamInterface() {}\n\n        // The current parameter value. Is also available in the test fixture's\n        // constructor. This member function is non-static, even though it only\n        // references static data, to reduce the opportunity for incorrect uses\n        // like writing 'WithParamInterface<bool>::GetParam()' for a test that\n        // uses a fixture whose parameter type is int.\n        const ParamType &GetParam() const\n        {\n            GTEST_CHECK_(parameter_ != NULL)\n                    << \"GetParam() can only be called inside a value-parameterized test \"\n                    << \"-- did you intend to write TEST_P instead of TEST_F?\";\n            return *parameter_;\n        }\n\n    private:\n        // Sets parameter value. The caller is responsible for making sure the value\n        // remains alive and unchanged throughout the current test.\n        static void SetParam(const ParamType *parameter)\n        {\n            parameter_ = parameter;\n        }\n\n        // Static value used for accessing parameter during a test lifetime.\n        static const ParamType *parameter_;\n\n        // TestClass must be a subclass of WithParamInterface<T> and Test.\n        template <class TestClass> friend class internal::ParameterizedTestFactory;\n    };\n\n    template <typename T>\n    const T *WithParamInterface<T>::parameter_ = NULL;\n\n// Most value-parameterized classes can ignore the existence of\n// WithParamInterface, and can just inherit from ::testing::TestWithParam.\n\n    template <typename T>\n    class TestWithParam : public Test, public WithParamInterface<T>\n    {\n    };\n\n#endif  // GTEST_HAS_PARAM_TEST\n\n// Macros for indicating success/failure in test code.\n\n// ADD_FAILURE unconditionally adds a failure to the current test.\n// SUCCEED generates a success - it doesn't automatically make the\n// current test successful, as a test is only successful when it has\n// no failure.\n//\n// EXPECT_* verifies that a certain condition is satisfied.  If not,\n// it behaves like ADD_FAILURE.  In particular:\n//\n//   EXPECT_TRUE  verifies that a Boolean condition is true.\n//   EXPECT_FALSE verifies that a Boolean condition is false.\n//\n// FAIL and ASSERT_* are similar to ADD_FAILURE and EXPECT_*, except\n// that they will also abort the current function on failure.  People\n// usually want the fail-fast behavior of FAIL and ASSERT_*, but those\n// writing data-driven tests often find themselves using ADD_FAILURE\n// and EXPECT_* more.\n\n// Generates a nonfatal failure with a generic message.\n#define ADD_FAILURE() GTEST_NONFATAL_FAILURE_(\"Failed\")\n\n// Generates a nonfatal failure at the given source file location with\n// a generic message.\n#define ADD_FAILURE_AT(file, line) \\\n  GTEST_MESSAGE_AT_(file, line, \"Failed\", \\\n                    ::testing::TestPartResult::kNonFatalFailure)\n\n// Generates a fatal failure with a generic message.\n#define GTEST_FAIL() GTEST_FATAL_FAILURE_(\"Failed\")\n\n// Define this macro to 1 to omit the definition of FAIL(), which is a\n// generic name and clashes with some other libraries.\n#if !GTEST_DONT_DEFINE_FAIL\n# define FAIL() GTEST_FAIL()\n#endif\n\n// Generates a success with a generic message.\n#define GTEST_SUCCEED() GTEST_SUCCESS_(\"Succeeded\")\n\n// Define this macro to 1 to omit the definition of SUCCEED(), which\n// is a generic name and clashes with some other libraries.\n#if !GTEST_DONT_DEFINE_SUCCEED\n# define SUCCEED() GTEST_SUCCEED()\n#endif\n\n// Macros for testing exceptions.\n//\n//    * {ASSERT|EXPECT}_THROW(statement, expected_exception):\n//         Tests that the statement throws the expected exception.\n//    * {ASSERT|EXPECT}_NO_THROW(statement):\n//         Tests that the statement doesn't throw any exception.\n//    * {ASSERT|EXPECT}_ANY_THROW(statement):\n//         Tests that the statement throws an exception.\n\n#define EXPECT_THROW(statement, expected_exception) \\\n  GTEST_TEST_THROW_(statement, expected_exception, GTEST_NONFATAL_FAILURE_)\n#define EXPECT_NO_THROW(statement) \\\n  GTEST_TEST_NO_THROW_(statement, GTEST_NONFATAL_FAILURE_)\n#define EXPECT_ANY_THROW(statement) \\\n  GTEST_TEST_ANY_THROW_(statement, GTEST_NONFATAL_FAILURE_)\n#define ASSERT_THROW(statement, expected_exception) \\\n  GTEST_TEST_THROW_(statement, expected_exception, GTEST_FATAL_FAILURE_)\n#define ASSERT_NO_THROW(statement) \\\n  GTEST_TEST_NO_THROW_(statement, GTEST_FATAL_FAILURE_)\n#define ASSERT_ANY_THROW(statement) \\\n  GTEST_TEST_ANY_THROW_(statement, GTEST_FATAL_FAILURE_)\n\n// Boolean assertions. Condition can be either a Boolean expression or an\n// AssertionResult. For more information on how to use AssertionResult with\n// these macros see comments on that class.\n#define EXPECT_TRUE(condition) \\\n  GTEST_TEST_BOOLEAN_((condition), #condition, false, true, \\\n                      GTEST_NONFATAL_FAILURE_)\n#define EXPECT_FALSE(condition) \\\n  GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \\\n                      GTEST_NONFATAL_FAILURE_)\n#define ASSERT_TRUE(condition) \\\n  GTEST_TEST_BOOLEAN_((condition), #condition, false, true, \\\n                      GTEST_FATAL_FAILURE_)\n#define ASSERT_FALSE(condition) \\\n  GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \\\n                      GTEST_FATAL_FAILURE_)\n\n// Includes the auto-generated header that implements a family of\n// generic predicate assertion macros.\n// Copyright 2006, Google Inc.\n// All rights reserved.\n//\n// Redistribution and use in source and binary forms, with or without\n// modification, are permitted provided that the following conditions are\n// met:\n//\n//     * Redistributions of source code must retain the above copyright\n// notice, this list of conditions and the following disclaimer.\n//     * Redistributions in binary form must reproduce the above\n// copyright notice, this list of conditions and the following disclaimer\n// in the documentation and/or other materials provided with the\n// distribution.\n//     * Neither the name of Google Inc. nor the names of its\n// contributors may be used to endorse or promote products derived from\n// this software without specific prior written permission.\n//\n// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n// \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\n// This file is AUTOMATICALLY GENERATED on 10/31/2011 by command\n// 'gen_gtest_pred_impl.py 5'.  DO NOT EDIT BY HAND!\n//\n// Implements a family of generic predicate assertion macros.\n\n#ifndef GTEST_INCLUDE_GTEST_GTEST_PRED_IMPL_H_\n#define GTEST_INCLUDE_GTEST_GTEST_PRED_IMPL_H_\n\n// Makes sure this header is not included before gtest.h.\n#ifndef GTEST_INCLUDE_GTEST_GTEST_H_\n# error Do not include gtest_pred_impl.h directly.  Include gtest.h instead.\n#endif  // GTEST_INCLUDE_GTEST_GTEST_H_\n\n// This header implements a family of generic predicate assertion\n// macros:\n//\n//   ASSERT_PRED_FORMAT1(pred_format, v1)\n//   ASSERT_PRED_FORMAT2(pred_format, v1, v2)\n//   ...\n//\n// where pred_format is a function or functor that takes n (in the\n// case of ASSERT_PRED_FORMATn) values and their source expression\n// text, and returns a testing::AssertionResult.  See the definition\n// of ASSERT_EQ in gtest.h for an example.\n//\n// If you don't care about formatting, you can use the more\n// restrictive version:\n//\n//   ASSERT_PRED1(pred, v1)\n//   ASSERT_PRED2(pred, v1, v2)\n//   ...\n//\n// where pred is an n-ary function or functor that returns bool,\n// and the values v1, v2, ..., must support the << operator for\n// streaming to std::ostream.\n//\n// We also define the EXPECT_* variations.\n//\n// For now we only support predicates whose arity is at most 5.\n// Please email googletestframework@googlegroups.com if you need\n// support for higher arities.\n\n// GTEST_ASSERT_ is the basic statement to which all of the assertions\n// in this file reduce.  Don't use this in your code.\n\n#define GTEST_ASSERT_(expression, on_failure) \\\n  GTEST_AMBIGUOUS_ELSE_BLOCKER_ \\\n  if (const ::testing::AssertionResult gtest_ar = (expression)) \\\n    ; \\\n  else \\\n    on_failure(gtest_ar.failure_message())\n\n\n// Helper function for implementing {EXPECT|ASSERT}_PRED1.  Don't use\n// this in your code.\n    template <typename Pred,\n              typename T1>\n    AssertionResult AssertPred1Helper(const char *pred_text,\n                                      const char *e1,\n                                      Pred pred,\n                                      const T1 &v1)\n    {\n        if (pred(v1)) return AssertionSuccess();\n\n        return AssertionFailure() << pred_text << \"(\"\n               << e1 << \") evaluates to false, where\"\n               << \"\\n\" << e1 << \" evaluates to \" << v1;\n    }\n\n// Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT1.\n// Don't use this in your code.\n#define GTEST_PRED_FORMAT1_(pred_format, v1, on_failure)\\\n  GTEST_ASSERT_(pred_format(#v1, v1), \\\n                on_failure)\n\n// Internal macro for implementing {EXPECT|ASSERT}_PRED1.  Don't use\n// this in your code.\n#define GTEST_PRED1_(pred, v1, on_failure)\\\n  GTEST_ASSERT_(::testing::AssertPred1Helper(#pred, \\\n                                             #v1, \\\n                                             pred, \\\n                                             v1), on_failure)\n\n// Unary predicate assertion macros.\n#define EXPECT_PRED_FORMAT1(pred_format, v1) \\\n  GTEST_PRED_FORMAT1_(pred_format, v1, GTEST_NONFATAL_FAILURE_)\n#define EXPECT_PRED1(pred, v1) \\\n  GTEST_PRED1_(pred, v1, GTEST_NONFATAL_FAILURE_)\n#define ASSERT_PRED_FORMAT1(pred_format, v1) \\\n  GTEST_PRED_FORMAT1_(pred_format, v1, GTEST_FATAL_FAILURE_)\n#define ASSERT_PRED1(pred, v1) \\\n  GTEST_PRED1_(pred, v1, GTEST_FATAL_FAILURE_)\n\n\n\n// Helper function for implementing {EXPECT|ASSERT}_PRED2.  Don't use\n// this in your code.\n    template <typename Pred,\n              typename T1,\n              typename T2>\n    AssertionResult AssertPred2Helper(const char *pred_text,\n                                      const char *e1,\n                                      const char *e2,\n                                      Pred pred,\n                                      const T1 &v1,\n                                      const T2 &v2)\n    {\n        if (pred(v1, v2)) return AssertionSuccess();\n\n        return AssertionFailure() << pred_text << \"(\"\n               << e1 << \", \"\n               << e2 << \") evaluates to false, where\"\n               << \"\\n\" << e1 << \" evaluates to \" << v1\n               << \"\\n\" << e2 << \" evaluates to \" << v2;\n    }\n\n// Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT2.\n// Don't use this in your code.\n#define GTEST_PRED_FORMAT2_(pred_format, v1, v2, on_failure)\\\n  GTEST_ASSERT_(pred_format(#v1, #v2, v1, v2), \\\n                on_failure)\n\n// Internal macro for implementing {EXPECT|ASSERT}_PRED2.  Don't use\n// this in your code.\n#define GTEST_PRED2_(pred, v1, v2, on_failure)\\\n  GTEST_ASSERT_(::testing::AssertPred2Helper(#pred, \\\n                                             #v1, \\\n                                             #v2, \\\n                                             pred, \\\n                                             v1, \\\n                                             v2), on_failure)\n\n// Binary predicate assertion macros.\n#define EXPECT_PRED_FORMAT2(pred_format, v1, v2) \\\n  GTEST_PRED_FORMAT2_(pred_format, v1, v2, GTEST_NONFATAL_FAILURE_)\n#define EXPECT_PRED2(pred, v1, v2) \\\n  GTEST_PRED2_(pred, v1, v2, GTEST_NONFATAL_FAILURE_)\n#define ASSERT_PRED_FORMAT2(pred_format, v1, v2) \\\n  GTEST_PRED_FORMAT2_(pred_format, v1, v2, GTEST_FATAL_FAILURE_)\n#define ASSERT_PRED2(pred, v1, v2) \\\n  GTEST_PRED2_(pred, v1, v2, GTEST_FATAL_FAILURE_)\n\n\n\n// Helper function for implementing {EXPECT|ASSERT}_PRED3.  Don't use\n// this in your code.\n    template <typename Pred,\n              typename T1,\n              typename T2,\n              typename T3>\n    AssertionResult AssertPred3Helper(const char *pred_text,\n                                      const char *e1,\n                                      const char *e2,\n                                      const char *e3,\n                                      Pred pred,\n                                      const T1 &v1,\n                                      const T2 &v2,\n                                      const T3 &v3)\n    {\n        if (pred(v1, v2, v3)) return AssertionSuccess();\n\n        return AssertionFailure() << pred_text << \"(\"\n               << e1 << \", \"\n               << e2 << \", \"\n               << e3 << \") evaluates to false, where\"\n               << \"\\n\" << e1 << \" evaluates to \" << v1\n               << \"\\n\" << e2 << \" evaluates to \" << v2\n               << \"\\n\" << e3 << \" evaluates to \" << v3;\n    }\n\n// Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT3.\n// Don't use this in your code.\n#define GTEST_PRED_FORMAT3_(pred_format, v1, v2, v3, on_failure)\\\n  GTEST_ASSERT_(pred_format(#v1, #v2, #v3, v1, v2, v3), \\\n                on_failure)\n\n// Internal macro for implementing {EXPECT|ASSERT}_PRED3.  Don't use\n// this in your code.\n#define GTEST_PRED3_(pred, v1, v2, v3, on_failure)\\\n  GTEST_ASSERT_(::testing::AssertPred3Helper(#pred, \\\n                                             #v1, \\\n                                             #v2, \\\n                                             #v3, \\\n                                             pred, \\\n                                             v1, \\\n                                             v2, \\\n                                             v3), on_failure)\n\n// Ternary predicate assertion macros.\n#define EXPECT_PRED_FORMAT3(pred_format, v1, v2, v3) \\\n  GTEST_PRED_FORMAT3_(pred_format, v1, v2, v3, GTEST_NONFATAL_FAILURE_)\n#define EXPECT_PRED3(pred, v1, v2, v3) \\\n  GTEST_PRED3_(pred, v1, v2, v3, GTEST_NONFATAL_FAILURE_)\n#define ASSERT_PRED_FORMAT3(pred_format, v1, v2, v3) \\\n  GTEST_PRED_FORMAT3_(pred_format, v1, v2, v3, GTEST_FATAL_FAILURE_)\n#define ASSERT_PRED3(pred, v1, v2, v3) \\\n  GTEST_PRED3_(pred, v1, v2, v3, GTEST_FATAL_FAILURE_)\n\n\n\n// Helper function for implementing {EXPECT|ASSERT}_PRED4.  Don't use\n// this in your code.\n    template <typename Pred,\n              typename T1,\n              typename T2,\n              typename T3,\n              typename T4>\n    AssertionResult AssertPred4Helper(const char *pred_text,\n                                      const char *e1,\n                                      const char *e2,\n                                      const char *e3,\n                                      const char *e4,\n                                      Pred pred,\n                                      const T1 &v1,\n                                      const T2 &v2,\n                                      const T3 &v3,\n                                      const T4 &v4)\n    {\n        if (pred(v1, v2, v3, v4)) return AssertionSuccess();\n\n        return AssertionFailure() << pred_text << \"(\"\n               << e1 << \", \"\n               << e2 << \", \"\n               << e3 << \", \"\n               << e4 << \") evaluates to false, where\"\n               << \"\\n\" << e1 << \" evaluates to \" << v1\n               << \"\\n\" << e2 << \" evaluates to \" << v2\n               << \"\\n\" << e3 << \" evaluates to \" << v3\n               << \"\\n\" << e4 << \" evaluates to \" << v4;\n    }\n\n// Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT4.\n// Don't use this in your code.\n#define GTEST_PRED_FORMAT4_(pred_format, v1, v2, v3, v4, on_failure)\\\n  GTEST_ASSERT_(pred_format(#v1, #v2, #v3, #v4, v1, v2, v3, v4), \\\n                on_failure)\n\n// Internal macro for implementing {EXPECT|ASSERT}_PRED4.  Don't use\n// this in your code.\n#define GTEST_PRED4_(pred, v1, v2, v3, v4, on_failure)\\\n  GTEST_ASSERT_(::testing::AssertPred4Helper(#pred, \\\n                                             #v1, \\\n                                             #v2, \\\n                                             #v3, \\\n                                             #v4, \\\n                                             pred, \\\n                                             v1, \\\n                                             v2, \\\n                                             v3, \\\n                                             v4), on_failure)\n\n// 4-ary predicate assertion macros.\n#define EXPECT_PRED_FORMAT4(pred_format, v1, v2, v3, v4) \\\n  GTEST_PRED_FORMAT4_(pred_format, v1, v2, v3, v4, GTEST_NONFATAL_FAILURE_)\n#define EXPECT_PRED4(pred, v1, v2, v3, v4) \\\n  GTEST_PRED4_(pred, v1, v2, v3, v4, GTEST_NONFATAL_FAILURE_)\n#define ASSERT_PRED_FORMAT4(pred_format, v1, v2, v3, v4) \\\n  GTEST_PRED_FORMAT4_(pred_format, v1, v2, v3, v4, GTEST_FATAL_FAILURE_)\n#define ASSERT_PRED4(pred, v1, v2, v3, v4) \\\n  GTEST_PRED4_(pred, v1, v2, v3, v4, GTEST_FATAL_FAILURE_)\n\n\n\n// Helper function for implementing {EXPECT|ASSERT}_PRED5.  Don't use\n// this in your code.\n    template <typename Pred,\n              typename T1,\n              typename T2,\n              typename T3,\n              typename T4,\n              typename T5>\n    AssertionResult AssertPred5Helper(const char *pred_text,\n                                      const char *e1,\n                                      const char *e2,\n                                      const char *e3,\n                                      const char *e4,\n                                      const char *e5,\n                                      Pred pred,\n                                      const T1 &v1,\n                                      const T2 &v2,\n                                      const T3 &v3,\n                                      const T4 &v4,\n                                      const T5 &v5)\n    {\n        if (pred(v1, v2, v3, v4, v5)) return AssertionSuccess();\n\n        return AssertionFailure() << pred_text << \"(\"\n               << e1 << \", \"\n               << e2 << \", \"\n               << e3 << \", \"\n               << e4 << \", \"\n               << e5 << \") evaluates to false, where\"\n               << \"\\n\" << e1 << \" evaluates to \" << v1\n               << \"\\n\" << e2 << \" evaluates to \" << v2\n               << \"\\n\" << e3 << \" evaluates to \" << v3\n               << \"\\n\" << e4 << \" evaluates to \" << v4\n               << \"\\n\" << e5 << \" evaluates to \" << v5;\n    }\n\n// Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT5.\n// Don't use this in your code.\n#define GTEST_PRED_FORMAT5_(pred_format, v1, v2, v3, v4, v5, on_failure)\\\n  GTEST_ASSERT_(pred_format(#v1, #v2, #v3, #v4, #v5, v1, v2, v3, v4, v5), \\\n                on_failure)\n\n// Internal macro for implementing {EXPECT|ASSERT}_PRED5.  Don't use\n// this in your code.\n#define GTEST_PRED5_(pred, v1, v2, v3, v4, v5, on_failure)\\\n  GTEST_ASSERT_(::testing::AssertPred5Helper(#pred, \\\n                                             #v1, \\\n                                             #v2, \\\n                                             #v3, \\\n                                             #v4, \\\n                                             #v5, \\\n                                             pred, \\\n                                             v1, \\\n                                             v2, \\\n                                             v3, \\\n                                             v4, \\\n                                             v5), on_failure)\n\n// 5-ary predicate assertion macros.\n#define EXPECT_PRED_FORMAT5(pred_format, v1, v2, v3, v4, v5) \\\n  GTEST_PRED_FORMAT5_(pred_format, v1, v2, v3, v4, v5, GTEST_NONFATAL_FAILURE_)\n#define EXPECT_PRED5(pred, v1, v2, v3, v4, v5) \\\n  GTEST_PRED5_(pred, v1, v2, v3, v4, v5, GTEST_NONFATAL_FAILURE_)\n#define ASSERT_PRED_FORMAT5(pred_format, v1, v2, v3, v4, v5) \\\n  GTEST_PRED_FORMAT5_(pred_format, v1, v2, v3, v4, v5, GTEST_FATAL_FAILURE_)\n#define ASSERT_PRED5(pred, v1, v2, v3, v4, v5) \\\n  GTEST_PRED5_(pred, v1, v2, v3, v4, v5, GTEST_FATAL_FAILURE_)\n\n\n\n#endif  // GTEST_INCLUDE_GTEST_GTEST_PRED_IMPL_H_\n\n// Macros for testing equalities and inequalities.\n//\n//    * {ASSERT|EXPECT}_EQ(v1, v2): Tests that v1 == v2\n//    * {ASSERT|EXPECT}_NE(v1, v2): Tests that v1 != v2\n//    * {ASSERT|EXPECT}_LT(v1, v2): Tests that v1 < v2\n//    * {ASSERT|EXPECT}_LE(v1, v2): Tests that v1 <= v2\n//    * {ASSERT|EXPECT}_GT(v1, v2): Tests that v1 > v2\n//    * {ASSERT|EXPECT}_GE(v1, v2): Tests that v1 >= v2\n//\n// When they are not, Google Test prints both the tested expressions and\n// their actual values.  The values must be compatible built-in types,\n// or you will get a compiler error.  By \"compatible\" we mean that the\n// values can be compared by the respective operator.\n//\n// Note:\n//\n//   1. It is possible to make a user-defined type work with\n//   {ASSERT|EXPECT}_??(), but that requires overloading the\n//   comparison operators and is thus discouraged by the Google C++\n//   Usage Guide.  Therefore, you are advised to use the\n//   {ASSERT|EXPECT}_TRUE() macro to assert that two objects are\n//   equal.\n//\n//   2. The {ASSERT|EXPECT}_??() macros do pointer comparisons on\n//   pointers (in particular, C strings).  Therefore, if you use it\n//   with two C strings, you are testing how their locations in memory\n//   are related, not how their content is related.  To compare two C\n//   strings by content, use {ASSERT|EXPECT}_STR*().\n//\n//   3. {ASSERT|EXPECT}_EQ(v1, v2) is preferred to\n//   {ASSERT|EXPECT}_TRUE(v1 == v2), as the former tells you\n//   what the actual value is when it fails, and similarly for the\n//   other comparisons.\n//\n//   4. Do not depend on the order in which {ASSERT|EXPECT}_??()\n//   evaluate their arguments, which is undefined.\n//\n//   5. These macros evaluate their arguments exactly once.\n//\n// Examples:\n//\n//   EXPECT_NE(5, Foo());\n//   EXPECT_EQ(NULL, a_pointer);\n//   ASSERT_LT(i, array_size);\n//   ASSERT_GT(records.size(), 0) << \"There is no record left.\";\n\n#define EXPECT_EQ(val1, val2) \\\n  EXPECT_PRED_FORMAT2(::testing::internal:: \\\n                      EqHelper<GTEST_IS_NULL_LITERAL_(val1)>::Compare, \\\n                      val1, val2)\n#define EXPECT_NE(val1, val2) \\\n  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2)\n#define EXPECT_LE(val1, val2) \\\n  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2)\n#define EXPECT_LT(val1, val2) \\\n  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2)\n#define EXPECT_GE(val1, val2) \\\n  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2)\n#define EXPECT_GT(val1, val2) \\\n  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2)\n\n#define GTEST_ASSERT_EQ(val1, val2) \\\n  ASSERT_PRED_FORMAT2(::testing::internal:: \\\n                      EqHelper<GTEST_IS_NULL_LITERAL_(val1)>::Compare, \\\n                      val1, val2)\n#define GTEST_ASSERT_NE(val1, val2) \\\n  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2)\n#define GTEST_ASSERT_LE(val1, val2) \\\n  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2)\n#define GTEST_ASSERT_LT(val1, val2) \\\n  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2)\n#define GTEST_ASSERT_GE(val1, val2) \\\n  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2)\n#define GTEST_ASSERT_GT(val1, val2) \\\n  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2)\n\n// Define macro GTEST_DONT_DEFINE_ASSERT_XY to 1 to omit the definition of\n// ASSERT_XY(), which clashes with some users' own code.\n\n#if !GTEST_DONT_DEFINE_ASSERT_EQ\n# define ASSERT_EQ(val1, val2) GTEST_ASSERT_EQ(val1, val2)\n#endif\n\n#if !GTEST_DONT_DEFINE_ASSERT_NE\n# define ASSERT_NE(val1, val2) GTEST_ASSERT_NE(val1, val2)\n#endif\n\n#if !GTEST_DONT_DEFINE_ASSERT_LE\n# define ASSERT_LE(val1, val2) GTEST_ASSERT_LE(val1, val2)\n#endif\n\n#if !GTEST_DONT_DEFINE_ASSERT_LT\n# define ASSERT_LT(val1, val2) GTEST_ASSERT_LT(val1, val2)\n#endif\n\n#if !GTEST_DONT_DEFINE_ASSERT_GE\n# define ASSERT_GE(val1, val2) GTEST_ASSERT_GE(val1, val2)\n#endif\n\n#if !GTEST_DONT_DEFINE_ASSERT_GT\n# define ASSERT_GT(val1, val2) GTEST_ASSERT_GT(val1, val2)\n#endif\n\n// C-string Comparisons.  All tests treat NULL and any non-NULL string\n// as different.  Two NULLs are equal.\n//\n//    * {ASSERT|EXPECT}_STREQ(s1, s2):     Tests that s1 == s2\n//    * {ASSERT|EXPECT}_STRNE(s1, s2):     Tests that s1 != s2\n//    * {ASSERT|EXPECT}_STRCASEEQ(s1, s2): Tests that s1 == s2, ignoring case\n//    * {ASSERT|EXPECT}_STRCASENE(s1, s2): Tests that s1 != s2, ignoring case\n//\n// For wide or narrow string objects, you can use the\n// {ASSERT|EXPECT}_??() macros.\n//\n// Don't depend on the order in which the arguments are evaluated,\n// which is undefined.\n//\n// These macros evaluate their arguments exactly once.\n\n#define EXPECT_STREQ(s1, s2) \\\n  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, s1, s2)\n#define EXPECT_STRNE(s1, s2) \\\n  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2)\n#define EXPECT_STRCASEEQ(s1, s2) \\\n  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, s1, s2)\n#define EXPECT_STRCASENE(s1, s2)\\\n  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2)\n\n#define ASSERT_STREQ(s1, s2) \\\n  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, s1, s2)\n#define ASSERT_STRNE(s1, s2) \\\n  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2)\n#define ASSERT_STRCASEEQ(s1, s2) \\\n  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, s1, s2)\n#define ASSERT_STRCASENE(s1, s2)\\\n  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2)\n\n// Macros for comparing floating-point numbers.\n//\n//    * {ASSERT|EXPECT}_FLOAT_EQ(val1, val2):\n//         Tests that two float values are almost equal.\n//    * {ASSERT|EXPECT}_DOUBLE_EQ(val1, val2):\n//         Tests that two double values are almost equal.\n//    * {ASSERT|EXPECT}_NEAR(v1, v2, abs_error):\n//         Tests that v1 and v2 are within the given distance to each other.\n//\n// Google Test uses ULP-based comparison to automatically pick a default\n// error bound that is appropriate for the operands.  See the\n// FloatingPoint template class in gtest-internal.h if you are\n// interested in the implementation details.\n\n#define EXPECT_FLOAT_EQ(val1, val2)\\\n  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \\\n                      val1, val2)\n\n#define EXPECT_DOUBLE_EQ(val1, val2)\\\n  EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \\\n                      val1, val2)\n\n#define ASSERT_FLOAT_EQ(val1, val2)\\\n  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \\\n                      val1, val2)\n\n#define ASSERT_DOUBLE_EQ(val1, val2)\\\n  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \\\n                      val1, val2)\n\n#define EXPECT_NEAR(val1, val2, abs_error)\\\n  EXPECT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \\\n                      val1, val2, abs_error)\n\n#define ASSERT_NEAR(val1, val2, abs_error)\\\n  ASSERT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \\\n                      val1, val2, abs_error)\n\n// These predicate format functions work on floating-point values, and\n// can be used in {ASSERT|EXPECT}_PRED_FORMAT2*(), e.g.\n//\n//   EXPECT_PRED_FORMAT2(testing::DoubleLE, Foo(), 5.0);\n\n// Asserts that val1 is less than, or almost equal to, val2.  Fails\n// otherwise.  In particular, it fails if either val1 or val2 is NaN.\n    GTEST_API_ AssertionResult FloatLE(const char *expr1, const char *expr2,\n                                       float val1, float val2);\n    GTEST_API_ AssertionResult DoubleLE(const char *expr1, const char *expr2,\n                                        double val1, double val2);\n\n\n#if GTEST_OS_WINDOWS\n\n// Macros that test for HRESULT failure and success, these are only useful\n// on Windows, and rely on Windows SDK macros and APIs to compile.\n//\n//    * {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED}(expr)\n//\n// When expr unexpectedly fails or succeeds, Google Test prints the\n// expected result and the actual result with both a human-readable\n// string representation of the error, if available, as well as the\n// hex result code.\n# define EXPECT_HRESULT_SUCCEEDED(expr) \\\n    EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr))\n\n# define ASSERT_HRESULT_SUCCEEDED(expr) \\\n    ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr))\n\n# define EXPECT_HRESULT_FAILED(expr) \\\n    EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr))\n\n# define ASSERT_HRESULT_FAILED(expr) \\\n    ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr))\n\n#endif  // GTEST_OS_WINDOWS\n\n// Macros that execute statement and check that it doesn't generate new fatal\n// failures in the current thread.\n//\n//   * {ASSERT|EXPECT}_NO_FATAL_FAILURE(statement);\n//\n// Examples:\n//\n//   EXPECT_NO_FATAL_FAILURE(Process());\n//   ASSERT_NO_FATAL_FAILURE(Process()) << \"Process() failed\";\n//\n#define ASSERT_NO_FATAL_FAILURE(statement) \\\n    GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_FATAL_FAILURE_)\n#define EXPECT_NO_FATAL_FAILURE(statement) \\\n    GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_NONFATAL_FAILURE_)\n\n// Causes a trace (including the source file path, the current line\n// number, and the given message) to be included in every test failure\n// message generated by code in the current scope.  The effect is\n// undone when the control leaves the current scope.\n//\n// The message argument can be anything streamable to std::ostream.\n//\n// In the implementation, we include the current line number as part\n// of the dummy variable name, thus allowing multiple SCOPED_TRACE()s\n// to appear in the same block - as long as they are on different\n// lines.\n#define SCOPED_TRACE(message) \\\n  ::testing::internal::ScopedTrace GTEST_CONCAT_TOKEN_(gtest_trace_, __LINE__)(\\\n    __FILE__, __LINE__, ::testing::Message() << (message))\n\n// Compile-time assertion for type equality.\n// StaticAssertTypeEq<type1, type2>() compiles iff type1 and type2 are\n// the same type.  The value it returns is not interesting.\n//\n// Instead of making StaticAssertTypeEq a class template, we make it a\n// function template that invokes a helper class template.  This\n// prevents a user from misusing StaticAssertTypeEq<T1, T2> by\n// defining objects of that type.\n//\n// CAVEAT:\n//\n// When used inside a method of a class template,\n// StaticAssertTypeEq<T1, T2>() is effective ONLY IF the method is\n// instantiated.  For example, given:\n//\n//   template <typename T> class Foo {\n//    public:\n//     void Bar() { testing::StaticAssertTypeEq<int, T>(); }\n//   };\n//\n// the code:\n//\n//   void Test1() { Foo<bool> foo; }\n//\n// will NOT generate a compiler error, as Foo<bool>::Bar() is never\n// actually instantiated.  Instead, you need:\n//\n//   void Test2() { Foo<bool> foo; foo.Bar(); }\n//\n// to cause a compiler error.\n    template <typename T1, typename T2>\n    bool StaticAssertTypeEq()\n    {\n        (void)internal::StaticAssertTypeEqHelper<T1, T2>();\n        return true;\n    }\n\n// Defines a test.\n//\n// The first parameter is the name of the test case, and the second\n// parameter is the name of the test within the test case.\n//\n// The convention is to end the test case name with \"Test\".  For\n// example, a test case for the Foo class can be named FooTest.\n//\n// Test code should appear between braces after an invocation of\n// this macro.  Example:\n//\n//   TEST(FooTest, InitializesCorrectly) {\n//     Foo foo;\n//     EXPECT_TRUE(foo.StatusIsOK());\n//   }\n\n// Note that we call GetTestTypeId() instead of GetTypeId<\n// ::testing::Test>() here to get the type ID of testing::Test.  This\n// is to work around a suspected linker bug when using Google Test as\n// a framework on Mac OS X.  The bug causes GetTypeId<\n// ::testing::Test>() to return different values depending on whether\n// the call is from the Google Test framework itself or from user test\n// code.  GetTestTypeId() is guaranteed to always return the same\n// value, as it always calls GetTypeId<>() from the Google Test\n// framework.\n#define GTEST_TEST(test_case_name, test_name)\\\n  GTEST_TEST_(test_case_name, test_name, \\\n              ::testing::Test, ::testing::internal::GetTestTypeId())\n\n// Define this macro to 1 to omit the definition of TEST(), which\n// is a generic name and clashes with some other libraries.\n#if !GTEST_DONT_DEFINE_TEST\n# define TEST(test_case_name, test_name) GTEST_TEST(test_case_name, test_name)\n#endif\n\n// Defines a test that uses a test fixture.\n//\n// The first parameter is the name of the test fixture class, which\n// also doubles as the test case name.  The second parameter is the\n// name of the test within the test case.\n//\n// A test fixture class must be declared earlier.  The user should put\n// the test code between braces after using this macro.  Example:\n//\n//   class FooTest : public testing::Test {\n//    protected:\n//     virtual void SetUp() { b_.AddElement(3); }\n//\n//     Foo a_;\n//     Foo b_;\n//   };\n//\n//   TEST_F(FooTest, InitializesCorrectly) {\n//     EXPECT_TRUE(a_.StatusIsOK());\n//   }\n//\n//   TEST_F(FooTest, ReturnsElementCountCorrectly) {\n//     EXPECT_EQ(0, a_.size());\n//     EXPECT_EQ(1, b_.size());\n//   }\n\n#define TEST_F(test_fixture, test_name)\\\n  GTEST_TEST_(test_fixture, test_name, test_fixture, \\\n              ::testing::internal::GetTypeId<test_fixture>())\n\n// Returns a path to temporary directory.\n// Tries to determine an appropriate directory for the platform.\n    GTEST_API_ std::string TempDir();\n\n}  // namespace testing\n\n// Use this function in main() to run all tests.  It returns 0 if all\n// tests are successful, or 1 otherwise.\n//\n// RUN_ALL_TESTS() should be invoked after the command line has been\n// parsed by InitGoogleTest().\n//\n// This function was formerly a macro; thus, it is in the global\n// namespace and has an all-caps name.\nint RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_;\n\ninline int RUN_ALL_TESTS()\n{\n    return ::testing::UnitTest::GetInstance()->Run();\n}\n\n#endif  // GTEST_INCLUDE_GTEST_GTEST_H_\n"
  },
  {
    "path": "deps/memkind/src/test/hbw_allocator_tests.cpp",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <hbw_allocator.h>\n\n#include \"common.h\"\n#include <vector>\n\n// Tests for hbw::allocator class.\nclass HbwAllocatorTests: public :: testing::Test\n{\n\nprotected:\n    void SetUp()\n    {}\n\n    void TearDown()\n    {}\n\n};\n\n//Test standard memory allocation and deallocation.\nTEST_F(HbwAllocatorTests, test_TC_MEMKIND_DetaultAllocatorTest)\n{\n    const size_t size = 512;\n    hbw::allocator<size_t> allocator;\n\n    hbw::allocator<size_t>::pointer ptr = allocator.allocate(size);\n\n    ASSERT_TRUE(NULL != ptr);\n\n    //Do the actually memory writing\n    for (size_t i=0; i<size; i++) {\n        ptr[i] = i;\n    }\n\n    allocator.deallocate(ptr, size);\n}\n\n//Test address convertion functionality.\nTEST_F(HbwAllocatorTests, test_TC_MEMKIND_AddressConvertion)\n{\n    const size_t size = 512;\n    hbw::allocator<int> allocator;\n\n    int *ptr = allocator.allocate(size);\n\n    ASSERT_TRUE(NULL != ptr);\n\n    const int excpected_val = 4;\n    ptr[0] = excpected_val;\n\n    hbw::allocator<int>::reference reference = *ptr;\n    hbw::allocator<int>::const_reference const_reference = *ptr;\n\n    hbw::allocator<int>::pointer test_ptr = allocator.address(reference);\n    hbw::allocator<int>::const_pointer test_const_ptr = allocator.address(\n                                                            const_reference);\n\n    ASSERT_TRUE(NULL != test_ptr);\n    ASSERT_TRUE(NULL != test_const_ptr);\n\n    EXPECT_EQ(excpected_val, test_ptr[0]);\n    EXPECT_EQ(excpected_val, test_const_ptr[0]);\n\n    allocator.deallocate(ptr, size);\n}\n\n//Test for boundaries of allocation sizes.\n//We expect to catch allocation exceptions caused by out of bound sizes.\nTEST_F(HbwAllocatorTests, test_TC_MEMKIND_AllocationSizeOutOfBounds)\n{\n    hbw::allocator<size_t> allocator;\n\n    const size_t over_size = allocator.max_size() + 1;\n\n    ASSERT_THROW(allocator.allocate(over_size), std::bad_alloc);\n\n    const size_t max_size = -1; //This will give maximum value of size_t.\n\n    ASSERT_THROW(allocator.allocate(max_size), std::bad_alloc);\n\n    ASSERT_THROW(allocator.allocate(0), std::bad_alloc);\n}\n\n//Test if variable will be constructed.\nTEST_F(HbwAllocatorTests, test_TC_MEMKIND_AllocatorConstruct)\n{\n    hbw::allocator<int> allocator;\n\n    int x = 0;\n    int expect_val = 4;\n    allocator.construct(&x, expect_val);\n\n    EXPECT_EQ(expect_val, x);\n}\n\n//Test the integration with std::vector.\nTEST_F(HbwAllocatorTests, test_TC_MEMKIND_StandardVector)\n{\n    std::vector<int, hbw::allocator<int> > vec;\n    const size_t size = 10000;\n\n    for (size_t i=0; i<size; i++) {\n        vec.push_back(i);\n    }\n\n    EXPECT_EQ(size, vec.size());\n    EXPECT_EQ(1, vec[1]);\n\n    vec.clear();\n\n    EXPECT_EQ((size_t)0, vec.size());\n}\n"
  },
  {
    "path": "deps/memkind/src/test/hbw_detection_test.py",
    "content": "#\n#  Copyright (C) 2017 - 2018 Intel Corporation.\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#  1. Redistributions of source code must retain the above copyright notice(s),\n#     this list of conditions and the following disclaimer.\n#  2. Redistributions in binary form must reproduce the above copyright notice(s),\n#     this list of conditions and the following disclaimer in the documentation\n#     and/or other materials provided with the distribution.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n#  EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n#  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n#\nfrom distutils.spawn import find_executable\nfrom python_framework import CMD_helper\nimport os\n\nclass Test_hbw_detection(object):\n    os.environ[\"PATH\"] += os.pathsep + os.path.dirname(os.path.dirname(__file__))\n    binary_path = find_executable(\"memkind-hbw-nodes\")\n    environ_err_test = \"../environ_err_hbw_malloc_test\"\n    expected_libnuma_warning = \"libnuma: Warning: node argument -1 is out of range\\n\\n\"\n    fail_msg = \"Test failed with:\\n {0}\"\n    cmd_helper = CMD_helper()\n\n    def get_hbw_nodes(self, nodemask=None):\n        \"\"\" This function executes memkind function 'get_mbind_nodemask' and returns its output - comma separated HBW nodes \"\"\"\n        command = self.binary_path\n        if (nodemask):\n            command = \"MEMKIND_HBW_NODES={}\".format(nodemask) + command\n        output, retcode = self.cmd_helper.execute_cmd(command, sudo=False)\n        assert retcode == 0, self.fail_msg.format(\"\\nError: Execution of \\'{0}\\' returns {1}, \\noutput: {2}\".format(command, retcode, output))\n        print(\"\\nExecution of {} returns output {}\".format(command, output))\n        return output\n\n    def test_TC_MEMKIND_hbw_detection_compare_nodemask_default_and_env_variable(self):\n        \"\"\" This test checks whether hbw_nodemask_default and hbw_nodemask_env_variable has the same value \"\"\"\n        hbw_nodemask_default = self.get_hbw_nodes()\n        hbw_nodemask_env_variable = self.get_hbw_nodes(hbw_nodemask_default)\n        assert hbw_nodemask_default == hbw_nodemask_env_variable, self.fail_msg.format(\"Error: Nodemask hbw_nodemask_default ({0}) \" \\\n               \"is not the same as nodemask hbw_nodemask_env_variable ({1})\".format(hbw_nodemask_default, hbw_nodemask_env_variable))\n\n    def test_TC_MEMKIND_hbw_detection_negative_hbw_malloc(self):\n        \"\"\" This test sets usupported value of MEMKIND_HBW_NODES, then try to perform a successfull allocation from DRAM using hbw_malloc()\n        thanks to default HBW_POLICY_PREFERRED policy \"\"\"\n        command = \"MEMKIND_HBW_NODES=-1 \" + self.cmd_helper.get_command_path(self.environ_err_test)\n        output, retcode = self.cmd_helper.execute_cmd(command, sudo=False)\n        assert retcode != 0, self.fail_msg.format(\"\\nError: Execution of: \\'{0}\\' returns: {1} \\noutput: {2}\".format(command, retcode, output))\n        assert self.expected_libnuma_warning == output, self.fail_msg.format(\"Error: expected libnuma warning ({0}) \" \\\n               \"was not found (output: {1})\").format(self.expected_libnuma_warning, output)\n"
  },
  {
    "path": "deps/memkind/src/test/hbw_verify_function_test.cpp",
    "content": "/*\n * Copyright (C) 2016 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#include \"memkind.h\"\n#include \"memkind/internal/memkind_hbw.h\"\n#include \"allocator_perf_tool/HugePageOrganizer.hpp\"\n#include \"hbwmalloc.h\"\n#include \"common.h\"\n\n#include <sys/mman.h>\n#include <numaif.h>\n#include <numa.h>\n#include <stdint.h>\n\n#define SHIFT_BYTES(ptr, bytes) ((char*)(ptr)+(bytes))\n\n/*\n * Set of tests for hbw_verify_memory_region() function,\n * which intend to check if allocated memory fully fall into high bandwidth memory.\n * Note: in this tests we are using internal function memkind_hbw_all_get_mbind_nodemask().\n * In future we intend to rewrite this function when we will have replacement in new API.\n */\nclass HbwVerifyFunctionTest: public :: testing::Test\n{\nprotected:\n    const size_t BLOCK_SIZE = 64;\n    const size_t page_size = sysconf(_SC_PAGESIZE);\n    const int flags = MAP_ANONYMOUS | MAP_PRIVATE;\n};\n\n/*\n * Group of basic tests for different sizes of allocations\n */\nTEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_HBW_page_size_not_round)\n{\n    size_t size = page_size * 1024 + 5;\n    char *ptr = (char *) hbw_malloc(size);\n    ASSERT_FALSE(ptr == NULL);\n    EXPECT_EQ(hbw_verify_memory_region(ptr, size, HBW_TOUCH_PAGES), 0);\n    hbw_free(ptr);\n}\n\nTEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_HBW_page_size_round)\n{\n    size_t size = page_size * 1024;\n    char *ptr = (char *) hbw_malloc(size);\n    ASSERT_FALSE(ptr == NULL);\n    EXPECT_EQ(hbw_verify_memory_region(ptr, size, HBW_TOUCH_PAGES), 0);\n    hbw_free(ptr);\n}\n\nTEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_HBW_iterate_1_byte_to_8194_bytes)\n{\n    for (size_t size = 1; size <= (page_size * 2 + 2);\n         size++) { //iterate through 2 pages and 2 bytes\n        char *ptr = (char *) hbw_malloc(size);\n        ASSERT_FALSE(ptr == NULL);\n        EXPECT_EQ(hbw_verify_memory_region(ptr, size, HBW_TOUCH_PAGES), 0);\n        hbw_free(ptr);\n    }\n}\n\nTEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_HBW_ext_5GB)\n{\n    size_t size = 5ull*(1<<30); //5GB - big allocation\n    char *ptr = (char *) hbw_malloc(size);\n    ASSERT_FALSE(ptr == NULL);\n    EXPECT_EQ(hbw_verify_memory_region(ptr, size, HBW_TOUCH_PAGES), 0);\n    hbw_free(ptr);\n}\n\n/*\n * Test setting memory without HBW_TOUCH_PAGES flag\n */\nTEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_HBW_setting_memory_without_flag)\n{\n    size_t size = page_size;\n    char *ptr = (char *) hbw_malloc(size);\n    ASSERT_FALSE(ptr == NULL);\n    memset(ptr, '.', size);\n    EXPECT_EQ(hbw_verify_memory_region(ptr, size, 0), 0);\n    for (size_t i = 0; i < size; i++) { //check that content is unchanged\n        EXPECT_TRUE(ptr[i] == '.');\n    }\n    hbw_free(ptr);\n}\n\n/*\n * Test HBW_TOUCH_PAGES flag\n */\nTEST_F(HbwVerifyFunctionTest,\n       test_TC_MEMKIND_HBW_TOUCH_PAGES_check_overwritten_content)\n{\n    size_t size = 5 * page_size;\n    char *ptr = (char *) hbw_malloc(size);\n    ASSERT_FALSE(ptr == NULL);\n    memset(ptr, '.', size);\n    EXPECT_EQ(hbw_verify_memory_region(ptr, size, HBW_TOUCH_PAGES), 0);\n    for (size_t i = 0; i < size;\n         i++) { //check that content of all pages doesn't change\n        EXPECT_TRUE(ptr[i] == '.');\n    }\n    hbw_free(ptr);\n}\n\n/*\n * Tests check if number of 64-page blocks are working correctly\n */\nTEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_HBW_many_blocks_round)\n{\n    size_t size = 16 * (BLOCK_SIZE * page_size); //exactly 16 * 64-page blocks\n    char *ptr = (char *) hbw_malloc(size);\n    ASSERT_FALSE(ptr == NULL);\n    EXPECT_EQ(hbw_verify_memory_region(ptr, size, HBW_TOUCH_PAGES), 0);\n    hbw_free(ptr);\n}\n\nTEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_HBW_many_blocks_not_round)\n{\n    size_t size = (16 * BLOCK_SIZE * page_size) + (8 *\n                                                   page_size); //16 * 64-page blocks and 1 * 8-paged block\n    char *ptr = (char *) hbw_malloc(size);\n    ASSERT_FALSE(ptr == NULL);\n    EXPECT_EQ(hbw_verify_memory_region(ptr, size, HBW_TOUCH_PAGES), 0);\n    hbw_free(ptr);\n}\n\n/*\n * Check if 2 blocks and parts of the third block are working correctly.\n * With this test we can be sure that different sizes of allocation (not only whole blocks) are ok.\n */\nTEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_HBW_iterate_not_round)\n{\n    for (size_t i = 1; i < BLOCK_SIZE; i++) {\n        size_t size = (2 * BLOCK_SIZE * page_size) + (i *\n                                                      page_size); // 2 * 64-paged blocks and iterate through whole 3rd 64-paged block\n        char *ptr = (char *) hbw_malloc(size);\n        ASSERT_FALSE(ptr == NULL);\n        EXPECT_EQ(hbw_verify_memory_region(ptr, size, HBW_TOUCH_PAGES), 0);\n        hbw_free(ptr);\n    }\n}\n\n/*\n * Tests for other kinds and malloc\n */\nTEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_2MBPages_HBW_HUGETLB)\n{\n    HugePageOrganizer huge_page_organizer(16);\n    size_t size = 2 * 1024 * 1024 * 10; //10 * 2MB pages\n    char *ptr = (char *) memkind_malloc(MEMKIND_HBW_HUGETLB, size);\n    ASSERT_FALSE(ptr == NULL);\n    EXPECT_EQ(hbw_verify_memory_region(ptr, size, HBW_TOUCH_PAGES), 0);\n    memkind_free(MEMKIND_HBW_HUGETLB, ptr);\n}\n\nTEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_DEFAULT)\n{\n    size_t size = page_size * 1024;\n    char *ptr = (char *) memkind_malloc(MEMKIND_DEFAULT, size);\n    ASSERT_FALSE(ptr == NULL);\n    EXPECT_EQ(hbw_verify_memory_region(ptr, size, HBW_TOUCH_PAGES), -1);\n    memkind_free(MEMKIND_DEFAULT, ptr);\n}\n\nTEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_malloc)\n{\n    size_t size = page_size * 1024;\n    char *ptr = (char *) malloc(size);\n    ASSERT_FALSE(ptr == NULL);\n    EXPECT_EQ(hbw_verify_memory_region(ptr, size, HBW_TOUCH_PAGES), -1);\n    free(ptr);\n}\n\n/*\n * Group of negative tests\n */\nTEST_F(HbwVerifyFunctionTest,\n       test_TC_MEMKIND_Negative_size_0_and_SET_MEMORY_flag)\n{\n    void *ptr = hbw_malloc(page_size);\n    ASSERT_FALSE(ptr == NULL);\n    EXPECT_EQ(hbw_verify_memory_region(ptr, 0, HBW_TOUCH_PAGES), EINVAL);\n    hbw_free(ptr);\n}\n\nTEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_Negative_size_0_without_flag)\n{\n    void *ptr = hbw_malloc(page_size);\n    ASSERT_FALSE(ptr == NULL);\n    EXPECT_EQ(hbw_verify_memory_region(ptr, 0, 0), EINVAL);\n    hbw_free(ptr);\n}\n\nTEST_F(HbwVerifyFunctionTest,\n       test_TC_MEMKIND_Negative_Uninitialized_Memory_without_flag)\n{\n    void *ptr = NULL;\n    EXPECT_EQ(hbw_verify_memory_region(ptr, page_size * 1024, 0), EINVAL);\n}\n\nTEST_F(HbwVerifyFunctionTest,\n       test_TC_MEMKIND_Negative_Uninitialized_Memory_and_SET_MEMORY_flag)\n{\n    void *ptr = NULL;\n    EXPECT_EQ(hbw_verify_memory_region(ptr, page_size * 1024, HBW_TOUCH_PAGES),\n              EINVAL);\n}\n\nTEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_Negative_Without_Memset)\n{\n    size_t size = page_size * 1024;\n    void *ptr = hbw_malloc(size);\n    ASSERT_FALSE(ptr == NULL);\n    EXPECT_EQ(hbw_verify_memory_region(ptr, size, 0), -1);\n    hbw_free(ptr);\n}\n\n/*\n * Corner cases: tests for half of pages\n * + HBM memory\n * # HBM memory and verified\n * - not HBM memory, but allocated\n * = not HBM memory and verified\n */\n\n/* 3 pages:\n * |++##|####|##++|\n */\nTEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_Half_Pages)\n{\n    size_t size = 3 * page_size;\n    nodemask_t nodemask;\n    struct bitmask hbw_nodemask = {NUMA_NUM_NODES, nodemask.n};\n    //function memkind_hbw_all_get_mbind_nodemask() has to be rewritten, when we will have replacement in new API.\n    memkind_hbw_all_get_mbind_nodemask(NULL, hbw_nodemask.maskp, hbw_nodemask.size);\n    char *ptr = (char *) mmap(NULL, size, PROT_READ | PROT_WRITE, flags, -1, 0);\n    ASSERT_FALSE(ptr == NULL);\n\n    //all pages should fall on HBM\n    mbind(ptr, size, MPOL_BIND, hbw_nodemask.maskp, NUMA_NUM_NODES, 0);\n\n    //verified are: half of the first page, second page and half of the third page\n    EXPECT_EQ(hbw_verify_memory_region(SHIFT_BYTES(ptr, page_size/2),\n                                       size - page_size, HBW_TOUCH_PAGES), 0);\n    EXPECT_EQ(munmap(ptr, size), 0);\n}\n\n/* 3 pages:\n * |####|####|----|\n * |++##|####|==--|\n */\nTEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_Half_Pages_1_and_2_page)\n{\n    size_t size = 3 * page_size;\n    nodemask_t nodemask;\n    struct bitmask hbw_nodemask = {NUMA_NUM_NODES, nodemask.n};\n    //function memkind_hbw_all_get_mbind_nodemask() has to be rewritten, when we will have replacement in new API.\n    memkind_hbw_all_get_mbind_nodemask(NULL, hbw_nodemask.maskp, hbw_nodemask.size);\n    char *ptr = (char *) mmap(NULL, size, PROT_READ | PROT_WRITE, flags, -1, 0);\n    ASSERT_FALSE(ptr == NULL);\n\n    //first and second page should fall on HBM\n    mbind(ptr, size - page_size, MPOL_BIND, hbw_nodemask.maskp, NUMA_NUM_NODES, 0);\n\n    //check if mbind is successful |####|####|----|\n    EXPECT_EQ(hbw_verify_memory_region(ptr, size - page_size, HBW_TOUCH_PAGES), 0);\n\n    //verified are: half of the first page, second page and half of the third page |++##|####|==--|\n    EXPECT_EQ(hbw_verify_memory_region(SHIFT_BYTES(ptr, page_size/2),\n                                       size - page_size, HBW_TOUCH_PAGES), -1);\n    EXPECT_EQ(munmap(ptr, size), 0);\n}\n\n/* 3 pages:\n * |----|####|####|\n * |--==|####|##++|\n */\nTEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_Half_Pages_2_and_3_page)\n{\n    size_t size = 3 * page_size;\n    nodemask_t nodemask;\n    struct bitmask hbw_nodemask = {NUMA_NUM_NODES, nodemask.n};\n    //function memkind_hbw_all_get_mbind_nodemask() has to be rewritten, when we will have replacement in new API.\n    memkind_hbw_all_get_mbind_nodemask(NULL, hbw_nodemask.maskp, hbw_nodemask.size);\n    char *ptr = (char *) mmap(NULL, size, PROT_READ | PROT_WRITE, flags, -1, 0);\n    ASSERT_FALSE(ptr == NULL);\n\n    //second and third page should fall on HBM\n    mbind(SHIFT_BYTES(ptr, page_size), size - page_size, MPOL_BIND,\n          hbw_nodemask.maskp, NUMA_NUM_NODES, 0);\n\n    //check if mbind is successful |----|####|####|\n    EXPECT_EQ(hbw_verify_memory_region(SHIFT_BYTES(ptr, page_size),\n                                       size - page_size, HBW_TOUCH_PAGES), 0);\n\n    //verified are: half of the first page, second page and half of the third page\n    EXPECT_EQ(hbw_verify_memory_region(SHIFT_BYTES(ptr, page_size/2),\n                                       size - page_size, HBW_TOUCH_PAGES), -1);\n    EXPECT_EQ(munmap(ptr, size), 0);\n}\n\n/* 3 pages:\n * |####|----|++++| and  |++++|----|####|\n * |++##|====|##++|\n */\nTEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_Half_Pages_1_and_3_page)\n{\n    size_t size = 3 * page_size;\n    nodemask_t nodemask;\n    struct bitmask hbw_nodemask = {NUMA_NUM_NODES, nodemask.n};\n    //function memkind_hbw_all_get_mbind_nodemask() has to be rewritten, when we will have replacement in new API.\n    memkind_hbw_all_get_mbind_nodemask(NULL, hbw_nodemask.maskp, hbw_nodemask.size);\n    char *ptr = (char *) mmap(NULL, size, PROT_READ | PROT_WRITE, flags, -1, 0);\n    ASSERT_FALSE(ptr == NULL);\n\n    //first and third page should fall on HBM\n    mbind(ptr, page_size, MPOL_BIND, hbw_nodemask.maskp, NUMA_NUM_NODES, 0);\n    mbind(SHIFT_BYTES(ptr, 2 * page_size), page_size, MPOL_BIND, hbw_nodemask.maskp,\n          NUMA_NUM_NODES, 0);\n\n    //check if mbind is successful |####|----|++++| and  |++++|----|####|\n    EXPECT_EQ(hbw_verify_memory_region(ptr, size - 2*page_size, HBW_TOUCH_PAGES),\n              0);\n    EXPECT_EQ(hbw_verify_memory_region(SHIFT_BYTES(ptr, 2*page_size),\n                                       size - 2*page_size, HBW_TOUCH_PAGES), 0);\n\n    //verified are: half of the first page, second page and half of the third page\n    EXPECT_EQ(hbw_verify_memory_region(SHIFT_BYTES(ptr, page_size/2),\n                                       size - page_size, HBW_TOUCH_PAGES), -1);\n    EXPECT_EQ(munmap(ptr, size), 0);\n}\n\n/* 5 pages:\n * |----|+###|####|###+|----|\n */\nTEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_Boundaries_CornerCase)\n{\n    size_t size = 5 * page_size;\n    nodemask_t nodemask;\n    struct bitmask hbw_nodemask = {NUMA_NUM_NODES, nodemask.n};\n    //function memkind_hbw_all_get_mbind_nodemask() has to be rewritten, when we will have replacement in new API.\n    memkind_hbw_all_get_mbind_nodemask(NULL, hbw_nodemask.maskp, hbw_nodemask.size);\n    char *ptr = (char *) mmap(NULL, size, PROT_READ | PROT_WRITE, flags, -1, 0);\n    ASSERT_FALSE(ptr == NULL);\n\n    size_t tested_pages_size = size - 2 * page_size;\n\n    //second, third and fourth page should fall on HBM\n    mbind(SHIFT_BYTES(ptr, page_size), tested_pages_size, MPOL_BIND,\n          hbw_nodemask.maskp, NUMA_NUM_NODES, 0);\n\n    //verified are: second byte of the second page to last byte -1 of fourth page\n    EXPECT_EQ(hbw_verify_memory_region(SHIFT_BYTES(ptr, page_size + 1),\n                                       tested_pages_size - 2, HBW_TOUCH_PAGES), 0);\n    EXPECT_EQ(munmap(ptr, size), 0);\n}\n\n/* 5 pages:\n * |-###|####|####|####|###-|\n */\nTEST_F(HbwVerifyFunctionTest, test_TC_MEMKIND_HBW_partial_verification)\n{\n    size_t size = 5 * page_size;\n\n    //all pages should fall on HBM\n    void *ptr = hbw_malloc(size);\n    ASSERT_FALSE(ptr == NULL);\n\n    //but only second byte of the first page to last byte -1 of the last page are touched\n    memset(SHIFT_BYTES(ptr, 1), 0, size - 2);\n\n    //verified are: second byte of the first page to last byte -1 of last page\n    EXPECT_EQ(hbw_verify_memory_region(SHIFT_BYTES(ptr, 1), size - 2, 0), 0);\n    hbw_free(ptr);\n}\n"
  },
  {
    "path": "deps/memkind/src/test/heap_manager_init_perf_test.cpp",
    "content": "/*\n * Copyright (C) 2016 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#include <sstream>\n#include <vector>\n\n#include \"common.h\"\n#include \"allocator_perf_tool/Configuration.hpp\"\n#include \"allocator_perf_tool/AllocatorFactory.hpp\"\n#include \"allocator_perf_tool/HugePageOrganizer.hpp\"\n\n//Test heap managers initialization performance.\nclass HeapManagerInitPerfTest: public :: testing::Test\n{\n\nprotected:\n    void SetUp()\n    {\n        //Calculate reference statistics.\n        ref_time = allocator_factory.initialize_allocator(\n                       AllocatorTypes::STANDARD_ALLOCATOR).total_time;\n    }\n\n    void TearDown()\n    {}\n\n    void run_test(unsigned allocator_type)\n    {\n        AllocatorFactory::initialization_stat stat =\n            allocator_factory.initialize_allocator(AllocatorTypes::MEMKIND_DEFAULT);\n\n        post_test(stat);\n    }\n\n    void post_test(AllocatorFactory::initialization_stat &stat)\n    {\n        //Calculate (%) distance to the reference time for function calls.\n        stat.ref_delta_time = allocator_factory.calc_ref_delta(ref_time,\n                                                               stat.total_time);\n\n        std::stringstream elapsed_time;\n        elapsed_time << stat.total_time;\n\n        std::stringstream ref_delta_time;\n        ref_delta_time << std::fixed << stat.ref_delta_time << std::endl;\n\n        RecordProperty(\"elapsed_time\", elapsed_time.str());\n        RecordProperty(\"ref_delta_time_percent_rate\", ref_delta_time.str());\n\n        for (int i=0; i<stat.memory_overhead.size(); i++) {\n            std::stringstream node;\n            node << \"memory_overhad_node_\" << i;\n            std::stringstream memory_overhead;\n            memory_overhead << stat.memory_overhead[i];\n            RecordProperty(node.str(), memory_overhead.str());\n        }\n    }\n\n    AllocatorFactory allocator_factory;\n    float ref_time;\n};\n\n\nTEST_F(HeapManagerInitPerfTest, test_TC_MEMKIND_perf_libinit_DEFAULT)\n{\n    AllocatorFactory::initialization_stat stat =\n        allocator_factory.initialize_allocator(AllocatorTypes::MEMKIND_DEFAULT);\n    post_test(stat);\n}\n\nTEST_F(HeapManagerInitPerfTest, test_TC_MEMKIND_perf_libinit_HBW)\n{\n    AllocatorFactory::initialization_stat stat =\n        allocator_factory.initialize_allocator(AllocatorTypes::MEMKIND_HBW);\n    post_test(stat);\n}\n\nTEST_F(HeapManagerInitPerfTest, test_TC_MEMKIND_perf_libinit_INTERLEAVE)\n{\n    AllocatorFactory::initialization_stat stat =\n        allocator_factory.initialize_allocator(AllocatorTypes::MEMKIND_INTERLEAVE);\n    post_test(stat);\n}\n\nTEST_F(HeapManagerInitPerfTest, test_TC_MEMKIND_perf_libinit_HBW_INTERLEAVE)\n{\n    AllocatorFactory::initialization_stat stat =\n        allocator_factory.initialize_allocator(AllocatorTypes::MEMKIND_HBW_INTERLEAVE);\n    post_test(stat);\n}\n\nTEST_F(HeapManagerInitPerfTest, test_TC_MEMKIND_perf_libinit_HBW_PREFERRED)\n{\n    AllocatorFactory::initialization_stat stat =\n        allocator_factory.initialize_allocator(AllocatorTypes::MEMKIND_HBW_PREFERRED);\n    post_test(stat);\n}\n\nTEST_F(HeapManagerInitPerfTest, test_TC_MEMKIND_perf_libinit_HUGETLB)\n{\n    HugePageOrganizer huge_page_organizer(16);\n    AllocatorFactory::initialization_stat stat =\n        allocator_factory.initialize_allocator(AllocatorTypes::MEMKIND_HUGETLB);\n    post_test(stat);\n}\n\nTEST_F(HeapManagerInitPerfTest, test_TC_MEMKIND_perf_libinit_GBTLB)\n{\n    AllocatorFactory::initialization_stat stat =\n        allocator_factory.initialize_allocator(AllocatorTypes::MEMKIND_GBTLB);\n    post_test(stat);\n}\n\nTEST_F(HeapManagerInitPerfTest, test_TC_MEMKIND_perf_libinit_HBW_HUGETLB)\n{\n    HugePageOrganizer huge_page_organizer(16);\n    AllocatorFactory::initialization_stat stat =\n        allocator_factory.initialize_allocator(AllocatorTypes::MEMKIND_HBW_HUGETLB);\n    post_test(stat);\n}\n\nTEST_F(HeapManagerInitPerfTest,\n       test_TC_MEMKIND_perf_libinit_HBW_PREFERRED_HUGETLB)\n{\n    HugePageOrganizer huge_page_organizer(16);\n    AllocatorFactory::initialization_stat stat =\n        allocator_factory.initialize_allocator(\n            AllocatorTypes::MEMKIND_HBW_PREFERRED_HUGETLB);\n    post_test(stat);\n}\n\nTEST_F(HeapManagerInitPerfTest, test_TC_MEMKIND_perf_ext_libinit_HBW_GBTLB)\n{\n    AllocatorFactory::initialization_stat stat =\n        allocator_factory.initialize_allocator(AllocatorTypes::MEMKIND_HBW_GBTLB);\n    post_test(stat);\n}\n\nTEST_F(HeapManagerInitPerfTest,\n       test_TC_MEMKIND_perf_libinit_HBW_PREFERRED_GBTLB)\n{\n    AllocatorFactory::initialization_stat stat =\n        allocator_factory.initialize_allocator(\n            AllocatorTypes::MEMKIND_HBW_PREFERRED_GBTLB);\n    post_test(stat);\n}\n\n"
  },
  {
    "path": "deps/memkind/src/test/huge_page_test.cpp",
    "content": "/*\n* Copyright (C) 2016 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n\n#include \"common.h\"\n#include \"allocator_perf_tool/HugePageUnmap.hpp\"\n#include \"allocator_perf_tool/HugePageOrganizer.hpp\"\n#include \"TimerSysTime.hpp\"\n#include \"Thread.hpp\"\n\n/*\n* This test was created because of the munmap() fail in jemalloc.\n* There are two root causes of the error:\n* - kernel bug (munmap() fails when the size is not aligned)\n* - heap Manager doesn’t provide size aligned to 2MB pages for munmap()\n* Test allocates 2000MB using Huge Pages (50threads*10operations*4MBalloc_size),\n* but it needs extra Huge Pages due to overhead caused by heap management.\n*/\nclass  HugePageTest: public :: testing::Test\n{\nprotected:\n    void run()\n    {\n        unsigned mem_operations_num = 10;\n        size_t threads_number = 50;\n        bool touch_memory = true;\n        size_t size_1MB = 1024*1024;\n        size_t alignment = 2*size_1MB;\n        size_t alloc_size = 4*size_1MB;\n\n        std::vector<Thread *> threads;\n        std::vector<Task *> tasks;\n\n        TimerSysTime timer;\n        timer.start();\n\n        // This bug occurs more frequently under stress of multithreaded allocations.\n        for (int i=0; i<threads_number; i++) {\n            Task *task = new HugePageUnmap(mem_operations_num, touch_memory, alignment,\n                                           alloc_size, HBW_PAGESIZE_2MB);\n            tasks.push_back(task);\n            threads.push_back(new Thread(task));\n        }\n\n        float elapsed_time = timer.getElapsedTime();\n\n        ThreadsManager threads_manager(threads);\n        threads_manager.start();\n        threads_manager.barrier();\n        threads_manager.release();\n\n        //task release\n        for (int i=0; i<tasks.size(); i++) {\n            delete tasks[i];\n        }\n\n        RecordProperty(\"threads_number\", threads_number);\n        RecordProperty(\"memory_operations_per_thread\", mem_operations_num);\n        RecordProperty(\"elapsed_time\", elapsed_time);\n    }\n};\n\n\n\n// Test passes when there is no crash.\nTEST_F(HugePageTest, test_TC_MEMKIND_ext_UNMAP_HUGE_PAGE)\n{\n    HugePageOrganizer huge_page_organizer(1024);\n    run();\n}\n"
  },
  {
    "path": "deps/memkind/src/test/load_tbbmalloc_symbols.c",
    "content": "/*\n * Copyright (C) 2016 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"tbbmalloc.h\"\n\nint load_tbbmalloc_symbols()\n{\n    const char so_name[]=\"libtbbmalloc.so.2\";\n    void *tbb_handle = dlopen(so_name, RTLD_LAZY);\n    if(!tbb_handle) {\n        printf(\"Cannot load %s\\n\", so_name);\n        return -1;\n    }\n\n    scalable_malloc = dlsym(tbb_handle, \"scalable_malloc\");\n    if(!scalable_malloc) {\n        printf(\"Cannot load scalable_malloc symbol from %s\\n\", so_name);\n        return -1;\n    }\n\n    scalable_realloc = dlsym(tbb_handle, \"scalable_realloc\");\n    if(!scalable_realloc) {\n        printf(\"Cannot load scalable_realloc symbol from %s\\n\", so_name);\n        return -1;\n    }\n\n    scalable_calloc = dlsym(tbb_handle, \"scalable_calloc\");\n    if(!scalable_calloc) {\n        printf(\"Cannot load scalable_calloc symbol from %s\\n\", so_name);\n        return -1;\n    }\n\n    scalable_free = dlsym(tbb_handle, \"scalable_free\");\n    if(!scalable_free) {\n        printf(\"Cannot load scalable_free symbol from %s\\n\", so_name);\n        return -1;\n    }\n\n    return 0;\n}\n"
  },
  {
    "path": "deps/memkind/src/test/locality_test.cpp",
    "content": "/*\n* Copyright (C) 2017 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n\n#include <stdio.h>\n#include <numa.h>\n#include <hbwmalloc.h>\n#include <memkind.h>\n#include <vector>\n#include <memory>\n#include <gtest/gtest.h>\n#include \"allocator_perf_tool/Allocation_info.hpp\"\n#include \"allocator_perf_tool/GTestAdapter.hpp\"\n\ntypedef std::unique_ptr<void, void(*)(void *)> hbw_mem_ptr;\n\n\nclass HBWPreferredLocalityTest: public ::testing::Test\n{\nprivate:\n    int find_closest_node(int node, const std::vector<int> &nodes)\n    {\n        int min_distance = 0;\n        int closest_node = -1;\n        for (int i = 0; i < nodes.size(); i++) {\n            int distance = numa_distance(node, nodes[i]);\n            if (distance && (distance < min_distance || min_distance == 0)) {\n                min_distance = distance;\n                closest_node = nodes[i];\n            }\n        }\n        return closest_node;\n    }\n\n    bool pin_to_cpu(int cpu_id)\n    {\n        cpu_set_t cpu_set;\n        CPU_ZERO(&cpu_set);\n        CPU_SET(cpu_id, &cpu_set);\n        return sched_setaffinity(0, sizeof(cpu_set_t), &cpu_set) != -1;\n    }\n\n    void check_ptr_numa(void *ptr, int expected_numa_id, int cpu_id, size_t size)\n    {\n        memset(ptr, 1, size);\n\n        int numa_id = get_numa_node_id(ptr);\n        EXPECT_EQ(numa_id, expected_numa_id);\n\n        char property_name[50];\n        snprintf(property_name, 50, \"actual_numa_for_cpu_%d_expected_numa_%d\", cpu_id,\n                 expected_numa_id);\n        GTestAdapter::RecordProperty(property_name, numa_id);\n    }\n\npublic:\n    void pin_memory_in_requesting_mem_thread(size_t size,\n                                             const std::vector<int> &cpu_ids,\n                                             const std::vector<int> &mcdram_nodes)\n    {\n        int threads_num = cpu_ids.size();\n        int ret = hbw_set_policy(HBW_POLICY_PREFERRED);\n        ASSERT_EQ(ret, 0);\n\n        #pragma omp parallel for num_threads(threads_num)\n        for (int i = 0; i < threads_num; i++) {\n            if (!pin_to_cpu(cpu_ids[i])) {\n                ADD_FAILURE();\n                continue;\n            }\n\n            void *internal_ptr = hbw_malloc(size);\n            if (!internal_ptr) {\n                ADD_FAILURE();\n                continue;\n            }\n            hbw_mem_ptr ptr(internal_ptr, hbw_free);\n            int expected_numa_id = find_closest_node(numa_node_of_cpu(cpu_ids[i]),\n                                                     mcdram_nodes);\n            check_ptr_numa(ptr.get(), expected_numa_id, cpu_ids[i], size);\n        }\n    }\n\n    void pin_memory_in_other_thread_than_requesting_mem(size_t size,\n                                                        const std::vector<int> &cpu_ids,\n                                                        const std::vector<int> &mcdram_nodes)\n    {\n        int threads_num = cpu_ids.size();\n        int ret = hbw_set_policy(HBW_POLICY_PREFERRED);\n        ASSERT_EQ(ret, 0);\n\n        int main_thread_cpu_id = 0;\n        int expected_numa_id = find_closest_node(main_thread_cpu_id, mcdram_nodes);\n        ASSERT_TRUE(pin_to_cpu(main_thread_cpu_id));\n\n        std::vector<hbw_mem_ptr> ptrs;\n        for (int i = 0; i < threads_num; i++) {\n            void *internal_ptr = hbw_malloc(size);\n            ASSERT_TRUE(internal_ptr);\n            ptrs.emplace_back(internal_ptr, hbw_free);\n        }\n\n        #pragma omp parallel for num_threads(threads_num)\n        for (int i = 0; i < threads_num; i++) {\n            if (!pin_to_cpu(cpu_ids[i])) {\n                ADD_FAILURE();\n                continue;\n            }\n            check_ptr_numa(ptrs[i].get(), expected_numa_id, cpu_ids[i], size);\n        }\n    }\n};\n\nTEST_F(HBWPreferredLocalityTest,\n       test_TC_MEMKIND_KNL_SNC4_pin_memory_in_requesting_mem_thread_4_threads_100_bytes)\n{\n    pin_memory_in_requesting_mem_thread(100u, std::vector<int> {0, 18, 36, 54},\n                                        std::vector<int> {4, 5, 6, 7});\n}\n\nTEST_F(HBWPreferredLocalityTest,\n       test_TC_MEMKIND_KNL_SNC4_pin_memory_in_other_thread_than_requesting_mem_4_threads_100_bytes)\n{\n    pin_memory_in_other_thread_than_requesting_mem(100u, std::vector<int> {0, 18, 36, 54},\n                                                   std::vector<int> {4, 5, 6, 7});\n}\n\n"
  },
  {
    "path": "deps/memkind/src/test/main.cpp",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"common.h\"\n\nchar *PMEM_DIR = const_cast<char *>(\"/tmp/\");\n\nint main(int argc, char **argv)\n{\n    int opt = 0;\n    struct stat st;\n\n    testing::InitGoogleTest(&argc, argv);\n\n    if (testing::GetGtestHelpFlag()) {\n        printf(\"\\nMemkind options:\\n\"\n               \"-d <directory_path>   change directory on which PMEM kinds\\n\"\n               \"                      are created (default /tmp/)\\n\");\n        return 0;\n    }\n\n    while ((opt = getopt(argc, argv, \"d:\")) != -1) {\n        switch (opt) {\n            case 'd':\n                PMEM_DIR = optarg;\n\n                if (stat(PMEM_DIR, &st) != 0 || !S_ISDIR(st.st_mode)) {\n                    printf(\"%s : Error in getting path status or\"\n                           \" invalid or non-existent directory\\n\", PMEM_DIR);\n                    return -1;\n                }\n\n                break;\n            default:\n                return -1;\n        }\n    }\n\n    return RUN_ALL_TESTS();\n}\n"
  },
  {
    "path": "deps/memkind/src/test/memkind-afts-ext.ts",
    "content": "/usr/share/mpss/test/memkind-dt/all_tests -a --gtest_filter=-PerformanceTest.*\n/usr/share/mpss/test/memkind-dt/decorator_test -a\n/usr/share/mpss/test/memkind-dt/gb_page_tests_bind_policy -a\n"
  },
  {
    "path": "deps/memkind/src/test/memkind-afts.ts",
    "content": "/usr/share/mpss/test/memkind-dt/all_tests -a --gtest_filter=-PerformanceTest.*:*.*ext*\n/usr/share/mpss/test/memkind-dt/decorator_test -a\n/usr/share/mpss/test/memkind-dt/gb_page_tests_bind_policy -a --gtest_filter=-*.*ext*\n/usr/share/mpss/test/memkind-dt/gb_page_tests_bind_policy -a --gtest_filter=-*.*ext*\n"
  },
  {
    "path": "deps/memkind/src/test/memkind-perf-ext.ts",
    "content": "/usr/share/mpss/test/memkind-dt/all_tests -a --gtest_filter=PerformanceTest.*\n/usr/share/mpss/test/memkind-dt/allocator_perf_tool_tests -a --gtest_filter=HeapManagerInitPerfTest*:AllocPerformanceTest*\n"
  },
  {
    "path": "deps/memkind/src/test/memkind-perf.ts",
    "content": "/usr/share/mpss/test/memkind-dt/all_tests -a --gtest_filter=PerformanceTest.*\n/usr/share/mpss/test/memkind-dt/allocator_perf_tool_tests -a --gtest_filter=HeapManagerInitPerfTest*:AllocPerformanceTest*:-*.*ext*\n"
  },
  {
    "path": "deps/memkind/src/test/memkind-pytests.ts",
    "content": "-k \"test_TC_MEMKIND_\"\n"
  },
  {
    "path": "deps/memkind/src/test/memkind-slts.ts",
    "content": "/usr/share/mpss/test/memkind-dt/allocator_perf_tool_tests -a --gtest_filter=AllocateToMaxStressTests*:-HugePageTest*\n/usr/share/mpss/test/memkind-dt/allocator_perf_tool_tests -a --gtest_filter=HugePageTest* --gtest_repeat=10 --gtest_break_on_failure\n"
  },
  {
    "path": "deps/memkind/src/test/memkind_pmem_long_time_tests.cpp",
    "content": "/*\n * Copyright (C) 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind.h>\n#include \"allocator_perf_tool/TimerSysTime.hpp\"\n#include \"common.h\"\n\n#define STRESS_TIME (3*24*60*60)\n\nextern const char  *PMEM_DIR;\n\nstatic const size_t small_size[] = {8, 16, 32, 48, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,\n                                    448, 512, 640, 768, 896, 1 * KB, 1280, 1536, 1792, 2 * KB, 2560, 3 * KB,\n                                    3584, 4 * KB, 5 * KB, 6 * KB, 7 * KB, 8 * KB, 10 * KB, 12 * KB, 14 * KB\n                                   };\n\nstatic const size_t large_size[] = {16 * KB, 32 * KB, 20 * KB, 24 * KB, 28 * KB, 32 * KB, 40 * KB, 48 * KB,\n                                    56 * KB, 64 * KB, 80 * KB, 96 * KB, 112 * KB, 128 * KB, 160 * KB, 192 * KB,\n                                    224 * KB, 256 * KB, 320 * KB, 384 * KB, 448 * KB, 512 * KB, 640 * KB,\n                                    768 * KB, 896 * KB, 1 * MB, 1280 * KB, 1536 * KB, 1792 * KB, 2 * MB,\n                                    2560 * KB, 3 * MB, 3584 * KB, 4 * MB, 5 * MB, 6 * MB, 7 * MB, 8 * MB\n                                   };\n\nclass MemkindPmemLongTimeStress: public :: testing::Test\n{\n\nprotected:\n    memkind_t pmem_kind;\n    void SetUp()\n    {\n        int err = memkind_create_pmem(PMEM_DIR, 0, &pmem_kind);\n        ASSERT_EQ(0, err);\n        ASSERT_TRUE(nullptr != pmem_kind);\n    }\n\n    void TearDown()\n    {\n        int err = memkind_destroy_kind(pmem_kind);\n        ASSERT_EQ(0, err);\n    }\n};\n\nTEST_F(MemkindPmemLongTimeStress, DISABLED_test_TC_MEMKIND_PmemStressSmallSize)\n{\n    void *test = nullptr;\n    TimerSysTime timer;\n    timer.start();\n\n    do {\n        for (size_t i = 0; i < ARRAY_SIZE(small_size); i++) {\n            test = memkind_malloc(pmem_kind, small_size[i]);\n            ASSERT_TRUE(test != nullptr);\n            memkind_free(pmem_kind, test);\n        }\n    } while (timer.getElapsedTime() < STRESS_TIME);\n}\n\nTEST_F(MemkindPmemLongTimeStress, DISABLED_test_TC_MEMKIND_PmemStressLargeSize)\n{\n    void *test = nullptr;\n    TimerSysTime timer;\n    timer.start();\n\n    do {\n        for (size_t i = 0; i < ARRAY_SIZE(large_size); i++) {\n            test = memkind_malloc(pmem_kind, large_size[i]);\n            ASSERT_TRUE(test != nullptr);\n            memkind_free(pmem_kind, test);\n        }\n    } while (timer.getElapsedTime() < STRESS_TIME);\n}\n\nTEST_F(MemkindPmemLongTimeStress,\n       DISABLED_test_TC_MEMKIND_PmemStressSmallAndLargeSize)\n{\n    void *test = nullptr;\n    size_t i = 0, j = 0;\n    TimerSysTime timer;\n    timer.start();\n\n    do {\n        if (i < ARRAY_SIZE(small_size)) {\n            test = memkind_malloc(pmem_kind, small_size[i]);\n            ASSERT_TRUE(test != nullptr);\n            memkind_free(pmem_kind, test);\n            i++;\n        } else\n            i = 0;\n\n        if (j < ARRAY_SIZE(large_size)) {\n            test = memkind_malloc(pmem_kind, large_size[j]);\n            ASSERT_TRUE(test != nullptr);\n            memkind_free(pmem_kind, test);\n            j++;\n        } else\n            j = 0;\n\n    } while (timer.getElapsedTime() < STRESS_TIME);\n}\n"
  },
  {
    "path": "deps/memkind/src/test/memkind_pmem_tests.cpp",
    "content": "/*\n * Copyright (C) 2015 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind/internal/memkind_pmem.h>\n#include <memkind/internal/memkind_private.h>\n#include \"allocator_perf_tool/TimerSysTime.hpp\"\n\n#include <sys/param.h>\n#include <sys/mman.h>\n#include <sys/statfs.h>\n#include <stdio.h>\n#include <pthread.h>\n#include \"common.h\"\n\nstatic const size_t PMEM_PART_SIZE = MEMKIND_PMEM_MIN_SIZE + 4 * KB;\nstatic const size_t PMEM_NO_LIMIT = 0;\nextern const char  *PMEM_DIR;\nstatic pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;\nstatic pthread_cond_t cond = PTHREAD_COND_INITIALIZER;\n\nclass MemkindPmemTests: public :: testing::Test\n{\n\nprotected:\n    memkind_t pmem_kind;\n    void SetUp()\n    {\n        // create PMEM partition\n        int err = memkind_create_pmem(PMEM_DIR, PMEM_PART_SIZE, &pmem_kind);\n        ASSERT_EQ(0, err);\n        ASSERT_TRUE(nullptr != pmem_kind);\n    }\n\n    void TearDown()\n    {\n        int err = memkind_destroy_kind(pmem_kind);\n        ASSERT_EQ(0, err);\n    }\n};\n\nclass MemkindPmemTestsCalloc : public MemkindPmemTests,\n    public ::testing::WithParamInterface<std::tuple<int, int>>\n{\n};\n\nclass MemkindPmemTestsMalloc : public MemkindPmemTests,\n    public ::testing::WithParamInterface<size_t>\n{\n};\n\nstatic void pmem_get_size(struct memkind *kind, size_t &total, size_t &free)\n{\n    struct memkind_pmem *priv = reinterpret_cast<struct memkind_pmem *>(kind->priv);\n\n    total = priv->max_size;\n    free = priv->max_size - priv->offset; /* rough estimation */\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemPriv)\n{\n    size_t total_mem = 0;\n    size_t free_mem = 0;\n\n    pmem_get_size(pmem_kind, total_mem, free_mem);\n\n    ASSERT_TRUE(total_mem != 0);\n    ASSERT_TRUE(free_mem != 0);\n\n    ASSERT_EQ(total_mem, roundup(PMEM_PART_SIZE, MEMKIND_PMEM_CHUNK_SIZE));\n\n    size_t offset = total_mem - free_mem;\n    ASSERT_LT(offset, MEMKIND_PMEM_CHUNK_SIZE);\n    ASSERT_LT(offset, total_mem);\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemCreatePmemFailNonExistingDirectory)\n{\n    const char *non_existing_directory = \"/temp/non_exisitng_directory\";\n    struct memkind *pmem_temp = nullptr;\n    errno = 0;\n    int err = memkind_create_pmem(non_existing_directory, MEMKIND_PMEM_MIN_SIZE,\n                                  &pmem_temp);\n    ASSERT_EQ(MEMKIND_ERROR_INVALID, err);\n    ASSERT_TRUE(nullptr == pmem_temp);\n    ASSERT_EQ(ENOENT, errno);\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemCreatePmemFailWritePermissionIssue)\n{\n    struct stat path_stat;\n    struct memkind *pmem_temp = nullptr;\n    char temp_dir[] = \"/tmp/tmpdir.XXXXXX\";\n\n    char *dir_name = mkdtemp(temp_dir);\n    ASSERT_TRUE(nullptr != dir_name);\n\n    int err = stat(dir_name, &path_stat);\n    ASSERT_EQ(0, err);\n\n    err = chmod(dir_name, path_stat.st_mode & ~S_IWUSR);\n    ASSERT_EQ(0, err);\n\n    errno = 0;\n    err = memkind_create_pmem(dir_name, MEMKIND_PMEM_MIN_SIZE, &pmem_temp);\n\n    ASSERT_EQ(MEMKIND_ERROR_INVALID, err);\n    ASSERT_TRUE(nullptr == pmem_temp);\n    ASSERT_EQ(EACCES, errno);\n\n    err = rmdir(temp_dir);\n    ASSERT_EQ(0, err);\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemMalloc)\n{\n    const size_t size = 1 * KB;\n    char *default_str = nullptr;\n\n    default_str = (char *)memkind_malloc(pmem_kind, size);\n    ASSERT_TRUE(nullptr != default_str);\n\n    sprintf(default_str, \"memkind_malloc MEMKIND_PMEM\\n\");\n    printf(\"%s\", default_str);\n\n    memkind_free(pmem_kind, default_str);\n\n    // Out of memory\n    default_str = (char *)memkind_malloc(pmem_kind, 2 * PMEM_PART_SIZE);\n    ASSERT_EQ(nullptr, default_str);\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemMallocZero)\n{\n    void *test1 = nullptr;\n\n    test1 = memkind_malloc(pmem_kind, 0);\n    ASSERT_TRUE(test1 == nullptr);\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemMallocSizeMax)\n{\n    void *test1 = nullptr;\n\n    errno = 0;\n    test1 = memkind_malloc(pmem_kind, SIZE_MAX);\n    ASSERT_TRUE(test1 == nullptr);\n    ASSERT_TRUE(errno == ENOMEM);\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemCallocSmallClassMultipleTimes)\n{\n    const size_t size = 1 * KB;\n    const size_t num = 1;\n    const size_t iteration = 100;\n    char *default_str = nullptr;\n\n    for (size_t i = 0; i < iteration; ++i) {\n        default_str = (char *)memkind_calloc(pmem_kind, num, size);\n        ASSERT_TRUE(nullptr != default_str);\n        ASSERT_EQ(*default_str, 0);\n        ASSERT_EQ(memcmp(default_str, default_str + 1, size - 1), 0);\n\n        sprintf(default_str, \"memkind_calloc MEMKIND_PMEM\\n\");\n\n        memkind_free(pmem_kind, default_str);\n    }\n}\n\n/*\n * Test will check if it is not possible to allocate memory\n * with calloc arguments size or num equal to zero\n */\nTEST_P(MemkindPmemTestsCalloc, test_TC_MEMKIND_PmemCallocZero)\n{\n    void *test = nullptr;\n    size_t size = std::get<0>(GetParam());\n    size_t num = std::get<1>(GetParam());\n\n    test = memkind_calloc(pmem_kind, size, num);\n    ASSERT_TRUE(test == nullptr);\n}\n\nINSTANTIATE_TEST_CASE_P(\n    CallocParam, MemkindPmemTestsCalloc,\n    ::testing::Values(std::make_tuple(10, 0),\n                      std::make_tuple(0, 0),\n                      std::make_tuple(0, 10)));\n\nTEST_P(MemkindPmemTestsCalloc, test_TC_MEMKIND_PmemCallocSizeMax)\n{\n    void *test = nullptr;\n    size_t size = SIZE_MAX;\n    size_t num = 1;\n    errno = 0;\n\n    test = memkind_calloc(pmem_kind, size, num);\n    ASSERT_TRUE(test == nullptr);\n    ASSERT_TRUE(errno == ENOMEM);\n}\n\nTEST_P(MemkindPmemTestsCalloc, test_TC_MEMKIND_PmemCallocNumMax)\n{\n    void *test = nullptr;\n    size_t size = 10;\n    size_t num = SIZE_MAX;\n    errno = 0;\n\n    test = memkind_calloc(pmem_kind, size, num);\n    ASSERT_TRUE(test == nullptr);\n    ASSERT_TRUE(errno == ENOMEM);\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemCallocHugeClassMultipleTimes)\n{\n    const size_t size = MEMKIND_PMEM_CHUNK_SIZE;\n    const size_t num = 1;\n    const size_t iteration = 100;\n    char *default_str = nullptr;\n\n    for (size_t i = 0; i < iteration; ++i) {\n        default_str = (char *)memkind_calloc(pmem_kind, num, size);\n        ASSERT_TRUE(nullptr != default_str);\n        ASSERT_EQ(*default_str, 0);\n        ASSERT_EQ(memcmp(default_str, default_str + 1, size - 1), 0);\n\n        sprintf(default_str, \"memkind_calloc MEMKIND_PMEM\\n\");\n\n        memkind_free(pmem_kind, default_str);\n    }\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemFreeMemoryAfterDestroyLargeClass)\n{\n    memkind_t pmem_kind_test;\n    struct statfs st;\n    double blocksAvailable;\n\n    int err = memkind_create_pmem(PMEM_DIR, PMEM_PART_SIZE, &pmem_kind_test);\n    ASSERT_EQ(0, err);\n    ASSERT_TRUE(nullptr != pmem_kind_test);\n\n    ASSERT_EQ(0, statfs(PMEM_DIR, &st));\n    blocksAvailable = st.f_bfree;\n\n    while(1) {\n        if (memkind_malloc(pmem_kind_test, 16 * KB) == nullptr)\n            break;\n    }\n\n    ASSERT_EQ(0, statfs(PMEM_DIR, &st));\n    ASSERT_GT(blocksAvailable, st.f_bfree);\n\n    err = memkind_destroy_kind(pmem_kind_test);\n    ASSERT_EQ(0, err);\n\n    ASSERT_EQ(0, statfs(PMEM_DIR, &st));\n    ASSERT_EQ(blocksAvailable, st.f_bfree);\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemFreeMemoryAfterDestroySmallClass)\n{\n    memkind_t pmem_kind_test;\n    struct statfs st;\n    double blocksAvailable;\n\n    int err = memkind_create_pmem(PMEM_DIR, PMEM_PART_SIZE, &pmem_kind_test);\n    ASSERT_EQ(0, err);\n    ASSERT_TRUE(nullptr != pmem_kind_test);\n\n    ASSERT_EQ(0, statfs(PMEM_DIR, &st));\n    blocksAvailable = st.f_bfree;\n\n    for(int i = 0; i < 100; ++i) {\n        ASSERT_TRUE(memkind_malloc(pmem_kind_test, 32) != nullptr);\n    }\n\n    ASSERT_EQ(0, statfs(PMEM_DIR, &st));\n    ASSERT_GT(blocksAvailable, st.f_bfree);\n\n    err = memkind_destroy_kind(pmem_kind_test);\n    ASSERT_EQ(0, err);\n\n    ASSERT_EQ(0, statfs(PMEM_DIR, &st));\n    ASSERT_EQ(blocksAvailable, st.f_bfree);\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemRealloc)\n{\n    const size_t size1 = 512;\n    const size_t size2 = 1 * KB;\n    char *default_str = nullptr;\n\n    default_str = (char *)memkind_realloc(pmem_kind, default_str, size1);\n    ASSERT_TRUE(nullptr != default_str);\n\n    sprintf(default_str, \"memkind_realloc MEMKIND_PMEM with size %zu\\n\", size1);\n    printf(\"%s\", default_str);\n\n    default_str = (char *)memkind_realloc(pmem_kind, default_str, size2);\n    ASSERT_TRUE(nullptr != default_str);\n\n    sprintf(default_str, \"memkind_realloc MEMKIND_PMEM with size %zu\\n\", size2);\n    printf(\"%s\", default_str);\n\n    memkind_free(pmem_kind, default_str);\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemMallocUsableSize)\n{\n    const struct {\n        size_t size;\n        size_t spacing;\n    } check_sizes[] = {\n        {.size = 10, .spacing = 8},\n        {.size = 100, .spacing = 16},\n        {.size = 200, .spacing = 32},\n        {.size = 500, .spacing = 64},\n        {.size = 1000, .spacing = 128},\n        {.size = 2000, .spacing = 256},\n        {.size = 3000, .spacing = 512},\n        {.size = 1 * MB, .spacing = 4 * MB},\n        {.size = 2 * MB, .spacing = 4 * MB},\n        {.size = 3 * MB, .spacing = 4 * MB},\n        {.size = 4 * MB, .spacing = 4 * MB}\n    };\n    struct memkind *pmem_temp = nullptr;\n\n    int err = memkind_create_pmem(PMEM_DIR, MEMKIND_PMEM_MIN_SIZE, &pmem_temp);\n    ASSERT_EQ(0, err);\n    ASSERT_TRUE(nullptr != pmem_temp);\n    size_t usable_size = memkind_malloc_usable_size(pmem_temp, nullptr);\n    ASSERT_EQ(0u, usable_size);\n    for (unsigned int i = 0; i < ARRAY_SIZE(check_sizes); ++i) {\n        size_t size = check_sizes[i].size;\n        void *alloc = memkind_malloc(pmem_temp, size);\n        ASSERT_TRUE(nullptr != alloc);\n\n        usable_size = memkind_malloc_usable_size(pmem_temp, alloc);\n        size_t diff = usable_size - size;\n        ASSERT_GE(usable_size, size);\n        ASSERT_LE(diff, check_sizes[i].spacing);\n\n        memkind_free(pmem_temp, alloc);\n    }\n    err = memkind_destroy_kind(pmem_temp);\n    ASSERT_EQ(0, err);\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemResize)\n{\n    const size_t size = MEMKIND_PMEM_CHUNK_SIZE;\n    char *pmem_str10 = nullptr;\n    char *pmem_strX = nullptr;\n    memkind_t pmem_kind_no_limit = nullptr;\n    memkind_t pmem_kind_not_possible = nullptr;\n    int err = 0;\n\n    pmem_str10 = (char *)memkind_malloc(pmem_kind, MEMKIND_PMEM_MIN_SIZE);\n    ASSERT_TRUE(nullptr != pmem_str10);\n\n    // Out of memory\n    pmem_strX = (char *)memkind_malloc(pmem_kind, size);\n    ASSERT_TRUE(nullptr == pmem_strX);\n\n    memkind_free(pmem_kind, pmem_str10);\n    memkind_free(pmem_kind, pmem_strX);\n\n    err = memkind_create_pmem(PMEM_DIR, PMEM_NO_LIMIT, &pmem_kind_no_limit);\n    ASSERT_EQ(0, err);\n    ASSERT_TRUE(nullptr != pmem_kind_no_limit);\n\n    pmem_str10 = (char *)memkind_malloc(pmem_kind_no_limit, MEMKIND_PMEM_MIN_SIZE);\n    ASSERT_TRUE(nullptr != pmem_str10);\n\n    pmem_strX = (char *)memkind_malloc(pmem_kind_no_limit, size);\n    ASSERT_TRUE(nullptr != pmem_strX);\n\n    memkind_free(pmem_kind_no_limit, pmem_str10);\n    memkind_free(pmem_kind_no_limit, pmem_strX);\n\n    err = memkind_destroy_kind(pmem_kind_no_limit);\n    ASSERT_EQ(0, err);\n\n    err = memkind_create_pmem(PMEM_DIR, MEMKIND_PMEM_MIN_SIZE-1,\n                              &pmem_kind_not_possible);\n    ASSERT_EQ(MEMKIND_ERROR_INVALID, err);\n    ASSERT_TRUE(nullptr == pmem_kind_not_possible);\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemReallocZero)\n{\n    size_t size = 1 * KB;\n    void *test = nullptr;\n    void *new_test = nullptr;\n\n    test = memkind_malloc(pmem_kind, size);\n    ASSERT_TRUE(test != nullptr);\n\n    new_test = memkind_realloc(pmem_kind, test, 0);\n    ASSERT_TRUE(new_test == nullptr);\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemReallocSizeMax)\n{\n    size_t size = 1 * KB;\n    void *test = nullptr;\n    void *new_test = nullptr;\n\n    test = memkind_malloc(pmem_kind, size);\n    ASSERT_TRUE(test != nullptr);\n    errno = 0;\n    new_test = memkind_realloc(pmem_kind, test, SIZE_MAX);\n    ASSERT_TRUE(new_test == nullptr);\n    ASSERT_TRUE(errno == ENOMEM);\n\n    memkind_free(pmem_kind, test);\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemReallocPtrCheck)\n{\n    size_t size = 1 * KB;\n    void *ptr_malloc = nullptr;\n    void *ptr_malloc_copy = nullptr;\n    void *ptr_realloc = nullptr;\n\n    ptr_malloc = memkind_malloc(pmem_kind, size);\n    ASSERT_TRUE(ptr_malloc != nullptr);\n\n    ptr_malloc_copy = ptr_malloc;\n\n    ptr_realloc = memkind_realloc(pmem_kind, ptr_malloc, PMEM_PART_SIZE);\n    ASSERT_TRUE(ptr_realloc == nullptr);\n    ASSERT_TRUE(ptr_malloc == ptr_malloc_copy);\n\n    memkind_free(pmem_kind, ptr_malloc);\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemReallocNullptr)\n{\n    size_t size = 1 * KB;\n    void *test = nullptr;\n\n    test = memkind_realloc(pmem_kind, test, size);\n    ASSERT_TRUE(test != nullptr);\n\n    memkind_free(pmem_kind, test);\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemReallocNullptrSizeMax)\n{\n    void *test = nullptr;\n\n    test = memkind_realloc(pmem_kind, test, SIZE_MAX);\n    ASSERT_TRUE(test == nullptr);\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemReallocNullptrZero)\n{\n    void *test = nullptr;\n\n    test = memkind_realloc(pmem_kind, test, 0);\n    ASSERT_TRUE(test == nullptr);\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemReallocIncreaseSize)\n{\n    size_t size = 1 * KB;\n    char *test1 = nullptr;\n    char *test2 = nullptr;\n    const char val[] = \"test_TC_MEMKIND_PmemReallocIncreaseSize\";\n    int status;\n\n    test1 = (char *)memkind_malloc(pmem_kind, size);\n    ASSERT_TRUE(test1 != nullptr);\n\n    sprintf(test1, \"%s\", val);\n\n    size *= 2;\n    test2 = (char *)memkind_realloc(pmem_kind, test1, size);\n    ASSERT_TRUE(test2 != nullptr);\n    status = memcmp(val, test2, sizeof(val));\n    ASSERT_TRUE(status == 0);\n\n    memkind_free(pmem_kind, test2);\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemReallocDecreaseSize)\n{\n    size_t size = 1 * KB;\n    char *test1 = nullptr;\n    char *test2 = nullptr;\n    const char val[] = \"test_TC_MEMKIND_PmemReallocDecreaseSize\";\n    int status;\n\n    test1 = (char *)memkind_malloc(pmem_kind, size);\n    ASSERT_TRUE(test1 != nullptr);\n\n    sprintf(test1, \"%s\", val);\n\n    size = 4;\n    test2 = (char *)memkind_realloc(pmem_kind, test1, size);\n    ASSERT_TRUE(test2 != nullptr);\n    status = memcmp(val, test2, size);\n    ASSERT_TRUE(status == 0);\n\n    memkind_free(pmem_kind, test2);\n}\n\n/*\n * This test shows realloc \"in-place\" mechanism.\n * In some cases like allocation shrinking within the same size class\n * realloc will shrink allocation \"in-place\",\n * but in others not (like changing size class).\n */\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemReallocInPlace)\n{\n    void *test1 = memkind_malloc(pmem_kind, 10 * MB);\n    ASSERT_TRUE(test1 != nullptr);\n\n    /* Several reallocations within the same jemalloc size class*/\n    void *test1r = memkind_realloc(pmem_kind, test1, 6 * MB);\n    ASSERT_EQ(test1r, test1);\n\n    test1r = memkind_realloc(pmem_kind, test1, 10 * MB);\n    ASSERT_EQ(test1r, test1);\n\n    test1r = memkind_realloc(pmem_kind, test1, 8 * MB);\n    ASSERT_EQ(test1r, test1);\n\n    void *test2 = memkind_malloc(pmem_kind, 4 * MB);\n    ASSERT_TRUE(test2 != nullptr);\n\n    /* 4MB => 16B (changing size class) */\n    void *test2r = memkind_realloc(pmem_kind, test2, 16);\n    ASSERT_TRUE(test2r != nullptr);\n\n    /* 8MB => 16B */\n    test1r = memkind_realloc(pmem_kind, test1, 16);\n\n    /*\n     * If the old size of the allocation is larger than\n     * the chunk size (4MB), we can reallocate it to 4MB first (in place),\n     * releasing some space, which makes it possible to do the actual\n     * shrinking...\n     */\n    ASSERT_TRUE(test1r != nullptr);\n    ASSERT_NE(test1r, test1);\n\n    /* ... and leaves some memory for new allocations. */\n    void *test3 = memkind_malloc(pmem_kind, 5 * MB);\n    ASSERT_TRUE(test3 != nullptr);\n\n    memkind_free(pmem_kind, test1r);\n    memkind_free(pmem_kind, test2r);\n    memkind_free(pmem_kind, test3);\n}\n\n/*\n * This test shows that we can make a single highest possible allocation\n * and there still will be a place for another allocations.\n */\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemMaxFill)\n{\n    const int possible_alloc_max = 4;\n    void *test[possible_alloc_max+1] = {nullptr};\n    size_t total_mem = 0;\n    size_t free_mem = 0;\n    int i, j;\n\n    pmem_get_size(pmem_kind, total_mem, free_mem);\n\n    for (i = 0; i < possible_alloc_max; i++) {\n        for (j = total_mem; j > 0; --j) {\n            test[i] = memkind_malloc(pmem_kind, j);\n            if(test[i] != nullptr)\n                break;\n        }\n        ASSERT_NE(j, 0);\n    }\n\n    for (j = total_mem; j > 0; --j) {\n        test[possible_alloc_max] = memkind_malloc(pmem_kind, j);\n        if(test[possible_alloc_max] != nullptr)\n            break;\n    }\n    //Ensure there is no more space available on kind\n    ASSERT_EQ(j, 0);\n\n    for (i = 0; i < possible_alloc_max; i++) {\n        memkind_free(pmem_kind, test[i]);\n    }\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemFreeNullptr)\n{\n    const double test_time = 5;\n\n    TimerSysTime timer;\n    timer.start();\n    do {\n        memkind_free(pmem_kind, nullptr);\n    } while(timer.getElapsedTime() < test_time);\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemFreeNullptrNullKind)\n{\n    const double test_time = 5;\n\n    TimerSysTime timer;\n    timer.start();\n    do {\n        memkind_free(nullptr, nullptr);\n    } while(timer.getElapsedTime() < test_time);\n}\n\n/*\n * This test will stress pmem kind with malloc-free loop\n * with various sizes for malloc\n */\nTEST_P(MemkindPmemTestsMalloc, test_TC_MEMKIND_PmemMallocSize)\n{\n    const int malloc_limit = 1000000;\n    const int loop_limit = 10;\n    int first_limit_of_allocations = 0;\n    int temp_limit_of_allocations = 0;\n    void *test[malloc_limit] = {nullptr};\n    int i = 0, j = 0;\n\n    //check maximum number of allocations right after create the kind\n    for (i = 0; i < malloc_limit; i++) {\n        test[i] = memkind_malloc(pmem_kind, GetParam());\n        if(test[i] == nullptr)\n            break;\n    }\n\n    ASSERT_TRUE(malloc_limit != i);\n    first_limit_of_allocations = i;\n\n    for (i = 0; i < first_limit_of_allocations; i++) {\n        memkind_free(pmem_kind, test[i]);\n        test[i] = nullptr;\n    }\n\n    //check number of allocations in consecutive iterations of malloc-free loop\n    for (i = 0; i < loop_limit; i++) {\n        for (j = 0; j < malloc_limit; j++) {\n            test[j] = memkind_malloc(pmem_kind, GetParam());\n            if(test[j] == nullptr)\n                break;\n        }\n\n        ASSERT_TRUE(malloc_limit != j);\n\n        temp_limit_of_allocations = j;\n\n        for (j = 0; j < temp_limit_of_allocations; j++) {\n            memkind_free(pmem_kind, test[j]);\n            test[j] = nullptr;\n        }\n\n        ASSERT_TRUE(temp_limit_of_allocations > 0.98 * first_limit_of_allocations);\n    }\n}\n\nINSTANTIATE_TEST_CASE_P(\n    MallocParam, MemkindPmemTestsMalloc,\n    ::testing::Values(32, 60, 80, 100, 128, 150, 160, 250, 256, 300, 320,\n                      500, 512, 800, 896, 3000, 4 * KB, 6000, 10000, 60000,\n                      96 * KB, 112 * KB, 128 * KB, 160 * KB, 192 * KB, 500000,\n                      2 * MB, 5 * MB));\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemMallocSmallSizeFill)\n{\n    const size_t small_size[] = {8, 16, 32, 48, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,\n                                 448, 512, 640, 768, 896, 1 * KB, 1280, 1536, 1792, 2 * KB, 2560,\n                                 3 * KB, 3584, 4 * KB, 5 * KB, 6 * KB, 7 * KB, 8 * KB, 10 * KB,\n                                 12 * KB, 14 * KB\n                                };\n    const int malloc_limit = 10000;\n    const int loop_limit = 100;\n    int first_limit_of_allocations = 0;\n    int temp_limit_of_allocations = 0;\n    void *test[malloc_limit][ARRAY_SIZE(small_size)] = {{nullptr}};\n    int i = 0, j = 0, k = 0;\n\n    //check maximum number of allocations right after create the kind\n    [&] {\n        for (i = 0; i < malloc_limit; i++)\n        {\n            for (j = 0; j < (int)ARRAY_SIZE(small_size); j++) {\n                test[i][j] = memkind_malloc(pmem_kind, small_size[j]);\n                if (test[i][j] == nullptr)\n                    return;\n            }\n        }\n    }();\n\n    ASSERT_TRUE(malloc_limit != i);\n    first_limit_of_allocations = i;\n\n    for(; i >= 0; i--) {\n        for(j--; j >= 0; j--) {\n            memkind_free(pmem_kind, test[i][j]);\n            test[i][j] = nullptr;\n        }\n        j = ARRAY_SIZE(small_size);\n    }\n\n    //check number of allocations in consecutive iterations of malloc-free loop\n    for (i = 0; i < loop_limit; i++) {\n        [&] {\n            for (j = 0; j < malloc_limit; j++)\n            {\n                for (k = 0; k < (int)ARRAY_SIZE(small_size); k++) {\n                    test[j][k] = memkind_malloc(pmem_kind, small_size[k]);\n                    if(test[j][k] == nullptr)\n                        return;\n                }\n            }\n        }();\n\n        ASSERT_TRUE(malloc_limit != j);\n        temp_limit_of_allocations = j;\n\n        for(; j >= 0; j--) {\n            for(k--; k >= 0; k--) {\n                memkind_free(pmem_kind, test[j][k]);\n                test[j][k] = nullptr;\n            }\n            k = ARRAY_SIZE(small_size);\n        }\n\n        ASSERT_TRUE(temp_limit_of_allocations > 0.98 * first_limit_of_allocations);\n    }\n}\n\nTEST_F(MemkindPmemTests,\n       test_TC_MEMKIND_PmemPosixMemalignWrongAlignmentLessThanVoidAndNotPowerOfTwo)\n{\n    void *test = nullptr;\n    size_t size = 32;\n    size_t wrong_alignment = 3;\n    int ret;\n\n    ret = memkind_posix_memalign(pmem_kind, &test, wrong_alignment, size);\n    ASSERT_TRUE(ret == EINVAL);\n    ASSERT_TRUE(test == nullptr);\n}\n\nTEST_F(MemkindPmemTests,\n       test_TC_MEMKIND_PmemPosixMemalignWrongAlignmentLessThanVoidAndPowerOfTwo)\n{\n    void *test = nullptr;\n    size_t size = 32;\n    size_t wrong_alignment = sizeof(void *)/2;\n    int ret;\n\n    ret = memkind_posix_memalign(pmem_kind, &test, wrong_alignment, size);\n    ASSERT_TRUE(ret == EINVAL);\n    ASSERT_TRUE(test == nullptr);\n}\n\nTEST_F(MemkindPmemTests,\n       test_TC_MEMKIND_PmemPosixMemalignWrongAlignmentNotPowerOfTwo)\n{\n    void *test = nullptr;\n    size_t size = 32;\n    size_t wrong_alignment = sizeof(void *)+1;\n    int ret;\n\n    ret = memkind_posix_memalign(pmem_kind, &test, wrong_alignment, size);\n    ASSERT_TRUE(ret == EINVAL);\n    ASSERT_TRUE(test == nullptr);\n}\n\nTEST_F(MemkindPmemTests,\n       test_TC_MEMKIND_PmemPosixMemalignLowestCorrectAlignment)\n{\n    void *test = nullptr;\n    size_t size = 32;\n    size_t alignment = sizeof(void *);\n    int ret;\n\n    ret = memkind_posix_memalign(pmem_kind, &test, alignment, size);\n    ASSERT_TRUE(ret == 0);\n    ASSERT_TRUE(test != nullptr);\n\n    memkind_free(pmem_kind, test);\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemPosixMemalignSizeZero)\n{\n    void *test = nullptr;\n    size_t alignment = sizeof(void *);\n    int ret;\n\n    ret = memkind_posix_memalign(pmem_kind, &test, alignment, 0);\n    ASSERT_TRUE(ret != 0);\n    ASSERT_TRUE(test == nullptr);\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemPosixMemalignSizeMax)\n{\n    void *test = nullptr;\n    size_t alignment = 64;\n    int ret;\n\n    ret = memkind_posix_memalign(pmem_kind, &test, alignment, SIZE_MAX);\n    ASSERT_TRUE(ret == ENOMEM);\n    ASSERT_TRUE(test == nullptr);\n}\n\n/*\n * This is a basic alignment test which will make alignment allocations,\n * check pointers, write and read values from allocated memory\n */\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemPosixMemalign)\n{\n    const int max_allocs = 1000000;\n    const int test_value = 123456;\n    const int test_loop = 10;\n    uintptr_t alignment;\n    unsigned malloc_counter = 0;\n    unsigned i = 0, j = 0;\n    int *ptrs[max_allocs] = {nullptr};\n    void *test = nullptr;\n    int ret;\n\n    for(alignment =  1 * KB; alignment <= 128 * KB; alignment *= 2) {\n        for (i = 0; i < test_loop; i++) {\n            for (j = 0; j < max_allocs; ++j) {\n                errno = 0;\n                ret = memkind_posix_memalign(pmem_kind, &test, alignment, sizeof(int *));\n                if(ret != 0) {\n                    //at least one allocation must succeed\n                    //ASSERT_TRUE(j > 0); TODO: this is issue with posix_mem_align and test should\n                    //be updated after resolving this, check PR#86\n                    malloc_counter = j;\n                    break;\n                }\n\n                ASSERT_EQ(ret, 0);\n                ASSERT_EQ(errno, 0);\n\n                ptrs[j] = (int *)test;\n\n                //test pointer should be usable\n                *(int *)test = test_value;\n                ASSERT_EQ(*(int *)test, test_value);\n\n                //check for correct address alignment\n                ASSERT_EQ((uintptr_t)(test) & (alignment - 1), (uintptr_t)0);\n            }\n\n            for (j = 0; j < malloc_counter; ++j) {\n                memkind_free(pmem_kind, ptrs[j]);\n                ptrs[j] = nullptr;\n            }\n        }\n    }\n}\n\nstatic memkind_t *pools;\nstatic int npools = 3;\nstatic void *thread_func(void *arg)\n{\n    int start_idx = *(int *)arg;\n    int err = 0;\n    for (int idx = 0; idx < npools; ++idx) {\n        int pool_id = start_idx + idx;\n\n        if (pools[pool_id] == nullptr) {\n            err = memkind_create_pmem(PMEM_DIR, PMEM_PART_SIZE, &pools[pool_id]);\n            EXPECT_EQ(0, err);\n        }\n\n        if (err == 0) {\n            void *test = memkind_malloc(pools[pool_id], sizeof(void *));\n            EXPECT_TRUE(test != nullptr);\n            memkind_free(pools[pool_id], test);\n            memkind_destroy_kind(pools[pool_id]);\n        }\n    }\n\n    return nullptr;\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemMultithreads)\n{\n    int nthreads = 10, status = 0;\n    pthread_t *threads = (pthread_t *)calloc(nthreads, sizeof(pthread_t));\n    ASSERT_TRUE(threads != nullptr);\n    int *pool_idx = (int *)calloc(nthreads, sizeof(int));\n    ASSERT_TRUE(pool_idx != nullptr);\n    pools = (memkind_t *)calloc(npools * nthreads, sizeof(memkind_t));\n    ASSERT_TRUE(pools != nullptr);\n\n    for (int t = 0; t < nthreads; t++) {\n        pool_idx[t] = npools * t;\n        status = pthread_create(&threads[t], nullptr, thread_func, &pool_idx[t]);\n        ASSERT_EQ(0, status);\n    }\n\n    for (int t = 0; t < nthreads; t++) {\n        status = pthread_join(threads[t], nullptr);\n        ASSERT_EQ(0, status);\n    }\n\n    free(pools);\n    free(threads);\n    free(pool_idx);\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemDestroyKind)\n{\n    const size_t pmem_array_size = 10;\n    struct memkind *pmem_kind_array[pmem_array_size] = {nullptr};\n\n    int err = memkind_create_pmem(PMEM_DIR, MEMKIND_PMEM_MIN_SIZE,\n                                  &pmem_kind_array[0]);\n    ASSERT_EQ(err, 0);\n\n    err = memkind_destroy_kind(pmem_kind_array[0]);\n    ASSERT_EQ(err, 0);\n\n    for (unsigned int i = 0; i < pmem_array_size; ++i) {\n        err = memkind_create_pmem(PMEM_DIR, MEMKIND_PMEM_MIN_SIZE, &pmem_kind_array[i]);\n        ASSERT_EQ(err, 0);\n    }\n\n    char *pmem_middle_name = pmem_kind_array[5]->name;\n    err = memkind_destroy_kind(pmem_kind_array[5]);\n    ASSERT_EQ(err, 0);\n\n    err = memkind_destroy_kind(pmem_kind_array[6]);\n    ASSERT_EQ(err, 0);\n\n    err = memkind_create_pmem(PMEM_DIR, MEMKIND_PMEM_MIN_SIZE, &pmem_kind_array[5]);\n    ASSERT_EQ(err, 0);\n\n    char *pmem_new_middle_name = pmem_kind_array[5]->name;\n\n    ASSERT_STREQ(pmem_middle_name, pmem_new_middle_name);\n\n    for (unsigned int i = 0; i < pmem_array_size; ++i) {\n        if (i != 6) {\n            err = memkind_destroy_kind(pmem_kind_array[i]);\n            ASSERT_EQ(err, 0);\n        }\n    }\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemDestroyKindArenaZero)\n{\n    struct memkind *pmem_temp_1 = nullptr;\n    struct memkind *pmem_temp_2 = nullptr;\n    unsigned int arena_zero = 0;\n    int err = memkind_create_pmem(PMEM_DIR, MEMKIND_PMEM_MIN_SIZE, &pmem_temp_1);\n    ASSERT_EQ(err, 0);\n\n    arena_zero = pmem_temp_1->arena_zero;\n    err = memkind_destroy_kind(pmem_temp_1);\n    ASSERT_EQ(err, 0);\n    err = memkind_create_pmem(PMEM_DIR, MEMKIND_PMEM_MIN_SIZE, &pmem_temp_2);\n    ASSERT_EQ(err, 0);\n\n    ASSERT_EQ(arena_zero,pmem_temp_2->arena_zero);\n\n    err = memkind_destroy_kind(pmem_temp_2);\n    ASSERT_EQ(err, 0);\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemCreateDestroyKindEmptyLoop)\n{\n    struct memkind *pmem_temp = nullptr;\n\n    for (unsigned int i = 0; i < MEMKIND_MAX_KIND; ++i) {\n        int err = memkind_create_pmem(PMEM_DIR, MEMKIND_PMEM_MIN_SIZE, &pmem_temp);\n        ASSERT_EQ(err, 0);\n        err = memkind_destroy_kind(pmem_temp);\n        ASSERT_EQ(err, 0);\n    }\n}\n\nTEST_F(MemkindPmemTests,\n       test_TC_MEMKIND_PmemCreateDestroyKindLoopMallocSmallSizeFreeDefinedPmemKind)\n{\n    struct memkind *pmem_temp = nullptr;\n    const size_t size = 1 * KB;\n\n    for (unsigned int i = 0; i < MEMKIND_MAX_KIND; ++i) {\n        int err = memkind_create_pmem(PMEM_DIR, MEMKIND_PMEM_MIN_SIZE, &pmem_temp);\n        ASSERT_EQ(err, 0);\n        void *ptr = memkind_malloc(pmem_temp, size);\n        ASSERT_TRUE(nullptr != ptr);\n        memkind_free(pmem_temp, ptr);\n        err = memkind_destroy_kind(pmem_temp);\n        ASSERT_EQ(err, 0);\n    }\n}\n\nTEST_F(MemkindPmemTests,\n       test_TC_MEMKIND_PmemCreateDestroyKindLoopMallocDifferentSizesDifferentKindsDefinedFreeForAllKinds)\n{\n    struct memkind *pmem_temp = nullptr;\n    const size_t size_1 = 1 * KB;\n    const size_t size_2 = MEMKIND_PMEM_CHUNK_SIZE;\n    void *ptr = nullptr;\n    void *ptr_default = nullptr;\n    void *ptr_regular = nullptr;\n    for (unsigned int i = 0; i < MEMKIND_MAX_KIND; ++i) {\n        int err = memkind_create_pmem(PMEM_DIR, MEMKIND_PMEM_MIN_SIZE, &pmem_temp);\n        ASSERT_EQ(err, 0);\n\n        ptr = memkind_malloc(pmem_temp, size_1);\n        ASSERT_TRUE(nullptr != ptr);\n        memkind_free(pmem_temp, ptr);\n        ptr = memkind_malloc(pmem_temp, size_2);\n        ASSERT_TRUE(nullptr != ptr);\n        memkind_free(pmem_temp, ptr);\n        ptr_default = memkind_malloc(MEMKIND_DEFAULT, size_2);\n        ASSERT_TRUE(nullptr != ptr_default);\n        memkind_free(MEMKIND_DEFAULT, ptr_default);\n        ptr_default = memkind_malloc(MEMKIND_DEFAULT, size_1);\n        ASSERT_TRUE(nullptr != ptr_default);\n        memkind_free(MEMKIND_DEFAULT, ptr_default);\n        ptr_regular = memkind_malloc(MEMKIND_REGULAR, size_2);\n        ASSERT_TRUE(nullptr != ptr_regular);\n        memkind_free(MEMKIND_REGULAR, ptr_regular);\n        ptr_regular = memkind_malloc(MEMKIND_REGULAR, size_1);\n        ASSERT_TRUE(nullptr != ptr_regular);\n        memkind_free(MEMKIND_REGULAR, ptr_regular);\n\n        err = memkind_destroy_kind(pmem_temp);\n        ASSERT_EQ(err, 0);\n    }\n}\n\nTEST_F(MemkindPmemTests,\n       test_TC_MEMKIND_PmemCreateDestroyKindLoopMallocDifferentSizesDifferentKindsDefinedFreeForNotPmemKinds)\n{\n    struct memkind *pmem_temp = nullptr;\n    const size_t size_1 = 1 * KB;\n    const size_t size_2 = MEMKIND_PMEM_CHUNK_SIZE;\n    void *ptr = nullptr;\n    void *ptr_default = nullptr;\n    void *ptr_regular = nullptr;\n    for (unsigned int i = 0; i < MEMKIND_MAX_KIND; ++i) {\n        int err = memkind_create_pmem(PMEM_DIR, MEMKIND_PMEM_MIN_SIZE, &pmem_temp);\n        ASSERT_EQ(err, 0);\n\n        ptr = memkind_malloc(pmem_temp, size_1);\n        ASSERT_TRUE(nullptr != ptr);\n        memkind_free(pmem_temp, ptr);\n        ptr = memkind_malloc(pmem_temp, size_2);\n        ASSERT_TRUE(nullptr != ptr);\n        memkind_free(nullptr, ptr);\n        ptr_default = memkind_malloc(MEMKIND_DEFAULT, size_2);\n        ASSERT_TRUE(nullptr != ptr_default);\n        memkind_free(MEMKIND_DEFAULT, ptr_default);\n        ptr_default = memkind_malloc(MEMKIND_DEFAULT, size_1);\n        ASSERT_TRUE(nullptr != ptr_default);\n        memkind_free(MEMKIND_DEFAULT, ptr_default);\n        ptr_regular = memkind_malloc(MEMKIND_REGULAR, size_2);\n        ASSERT_TRUE(nullptr != ptr_regular);\n        memkind_free(MEMKIND_REGULAR, ptr_regular);\n        ptr_regular = memkind_malloc(MEMKIND_REGULAR, size_1);\n        ASSERT_TRUE(nullptr != ptr_regular);\n        memkind_free(MEMKIND_REGULAR, ptr_regular);\n\n        err = memkind_destroy_kind(pmem_temp);\n        ASSERT_EQ(err, 0);\n    }\n}\n\nTEST_F(MemkindPmemTests,\n       test_TC_MEMKIND_PmemCreateDestroyKindLoopMallocDifferentSizesDifferentKindsNullKindFree)\n{\n    struct memkind *pmem_temp = nullptr;\n    const size_t size_1 = 1 * KB;\n    const size_t size_2 = MEMKIND_PMEM_CHUNK_SIZE;\n    void *ptr = nullptr;\n    void *ptr_default = nullptr;\n    void *ptr_regular = nullptr;\n    for (unsigned int i = 0; i < MEMKIND_MAX_KIND; ++i) {\n        int err = memkind_create_pmem(PMEM_DIR, MEMKIND_PMEM_MIN_SIZE, &pmem_temp);\n        ASSERT_EQ(err, 0);\n\n        ptr = memkind_malloc(pmem_temp, size_1);\n        ASSERT_TRUE(nullptr != ptr);\n        memkind_free(pmem_temp, ptr);\n        ptr = memkind_malloc(pmem_temp, size_2);\n        ASSERT_TRUE(nullptr != ptr);\n        memkind_free(nullptr, ptr);\n        ptr_default = memkind_malloc(MEMKIND_DEFAULT, size_2);\n        ASSERT_TRUE(nullptr != ptr_default);\n        memkind_free(nullptr, ptr_default);\n        ptr_default = memkind_malloc(MEMKIND_DEFAULT, size_1);\n        ASSERT_TRUE(nullptr != ptr_default);\n        memkind_free(nullptr, ptr_default);\n        ptr_regular = memkind_malloc(MEMKIND_REGULAR, size_2);\n        ASSERT_TRUE(nullptr != ptr_regular);\n        memkind_free(nullptr, ptr_regular);\n        ptr_regular = memkind_malloc(MEMKIND_REGULAR, size_1);\n        ASSERT_TRUE(nullptr != ptr_regular);\n        memkind_free(nullptr, ptr_regular);\n\n        err = memkind_destroy_kind(pmem_temp);\n        ASSERT_EQ(err, 0);\n    }\n}\n\nTEST_F(MemkindPmemTests,\n       test_TC_MEMKIND_PmemCreateDestroyKindLoopWithRealloc)\n{\n    struct memkind *pmem_temp = nullptr;\n    const size_t size_1 = 512;\n    const size_t size_2 = 1 * KB;\n\n    for (unsigned int i = 0; i < MEMKIND_MAX_KIND; ++i) {\n        int err = memkind_create_pmem(PMEM_DIR, MEMKIND_PMEM_MIN_SIZE, &pmem_temp);\n        ASSERT_EQ(err, 0);\n        void *ptr = memkind_malloc(pmem_temp, size_1);\n        ASSERT_TRUE(nullptr != ptr);\n        void *ptr_2 = memkind_realloc(pmem_temp, ptr, size_2);\n        ASSERT_TRUE(nullptr != ptr_2);\n        memkind_free(pmem_temp, ptr_2);\n        err = memkind_destroy_kind(pmem_temp);\n        ASSERT_EQ(err, 0);\n    }\n}\n\nTEST_F(MemkindPmemTests,\n       test_TC_MEMKIND_PmemCreateCheckErrorCodeArenaCreate)\n{\n    struct memkind *pmem_temp[MEMKIND_MAX_KIND] = { nullptr };\n    unsigned i = 0, j = 0;\n    int err = 0;\n\n    for (i = 0; i < MEMKIND_MAX_KIND; ++i) {\n        err = memkind_create_pmem(PMEM_DIR, MEMKIND_PMEM_MIN_SIZE, &pmem_temp[i]);\n        if (err) {\n            ASSERT_EQ(err, MEMKIND_ERROR_ARENAS_CREATE);\n            break;\n        }\n    }\n    for (j = 0; j < i; ++j) {\n        err = memkind_destroy_kind(pmem_temp[j]);\n        ASSERT_EQ(err, 0);\n    }\n}\n\nstatic void *thread_func_kinds(void *arg)\n{\n    memkind_t pmem_thread_kind;\n    int err = 0;\n\n    pthread_mutex_lock(&mutex);\n    pthread_cond_wait(&cond, &mutex);\n    pthread_mutex_unlock(&mutex);\n\n    err = memkind_create_pmem(PMEM_DIR, PMEM_PART_SIZE, &pmem_thread_kind);\n\n    if(err == 0) {\n        void *test = memkind_malloc(pmem_thread_kind, 32);\n        EXPECT_TRUE(test != nullptr);\n\n        memkind_free(pmem_thread_kind, test);\n        err = memkind_destroy_kind(pmem_thread_kind);\n        EXPECT_EQ(0, err);\n    }\n\n    return nullptr;\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemMultithreadsStressKindsCreate)\n{\n    const int nthreads = 50;\n    int i, t, err;\n    int max_possible_kind = 0;\n    memkind_t pmem_kinds[MEMKIND_MAX_KIND] = {nullptr};\n    pthread_t *threads = (pthread_t *)calloc(nthreads, sizeof(pthread_t));\n    ASSERT_TRUE(threads != nullptr);\n\n    // This loop will create as many kinds as possible\n    // to obtain a real kind limit\n    for (i = 0; i < MEMKIND_MAX_KIND; i++) {\n        err = memkind_create_pmem(PMEM_DIR, PMEM_PART_SIZE, &pmem_kinds[i]);\n        if(err != 0) {\n            ASSERT_TRUE(i > 0);\n            max_possible_kind = i;\n            --i;\n            break;\n        }\n        ASSERT_TRUE(nullptr != pmem_kinds[i]);\n    }\n\n    // destroy last kind so it will be possible\n    // to create only one pmem kind in threads\n    err = memkind_destroy_kind(pmem_kinds[i]);\n    ASSERT_EQ(0, err);\n\n    for (t = 0; t < nthreads; t++) {\n        err = pthread_create(&threads[t], nullptr, thread_func_kinds, nullptr);\n        ASSERT_EQ(0, err);\n    }\n\n    sleep(1);\n    pthread_cond_broadcast(&cond);\n\n    for (t = 0; t < nthreads; t++) {\n        err = pthread_join(threads[t], nullptr);\n        ASSERT_EQ(0, err);\n    }\n\n    for (i = 0; i < max_possible_kind - 1; i++) {\n        err = memkind_destroy_kind(pmem_kinds[i]);\n        ASSERT_EQ(0, err);\n    }\n\n    free(threads);\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemKindFreeBenchmarkOneThread)\n{\n    const size_t pmem_array_size = 10;\n    struct memkind *pmem_kind_array[pmem_array_size] = { nullptr };\n    const int malloc_limit = 100000;\n    void *ptr[pmem_array_size][malloc_limit] = { { nullptr } };\n    TimerSysTime timer;\n    double test1Time, test2Time;\n\n    for (size_t i = 0; i < pmem_array_size; ++i) {\n        int err = memkind_create_pmem(PMEM_DIR, PMEM_PART_SIZE, &pmem_kind_array[i]);\n        ASSERT_EQ(0, err);\n    }\n\n    for (size_t i = 0; i < pmem_array_size; ++i) {\n        size_t j = 0;\n        for (j = 0; j < malloc_limit; ++j) {\n            ptr[i][j] = memkind_malloc(pmem_kind_array[i], 512);\n\n            if (ptr[i][j] == nullptr) {\n                break;\n            }\n        }\n        ASSERT_TRUE(j != malloc_limit);\n    }\n\n    timer.start();\n    for (size_t i = 0; i < pmem_array_size; ++i) {\n        for (size_t j = 0; j < malloc_limit; ++j) {\n            if (ptr[i][j] == nullptr) {\n                break;\n            }\n            memkind_free(pmem_kind_array[i], ptr[i][j]);\n        }\n    }\n    test1Time = timer.getElapsedTime();\n    printf(\"Free time with explicitly kind: %f\\n\", test1Time);\n\n    for (size_t i = 0; i < pmem_array_size; ++i) {\n        size_t j = 0;\n        for (j = 0; j < malloc_limit; ++j) {\n            ptr[i][j] = memkind_malloc(pmem_kind_array[i], 512);\n            if (ptr[i][j] == nullptr) {\n                break;\n            }\n        }\n        ASSERT_TRUE(j != malloc_limit);\n    }\n\n    timer.start();\n    for (size_t i = 0; i < pmem_array_size; ++i) {\n        for (size_t j = 0; j < malloc_limit; ++j) {\n            if (ptr[i][j] == nullptr) {\n                break;\n            }\n            memkind_free(nullptr, ptr[i][j]);\n        }\n    }\n    test2Time = timer.getElapsedTime();\n    printf(\"Free time with implicitly kind: %f\\n\", test2Time);\n\n    ASSERT_TRUE(test1Time < test2Time);\n\n    for (size_t i = 0; i < pmem_array_size; ++i) {\n        int err = memkind_destroy_kind(pmem_kind_array[i]);\n        ASSERT_EQ(0, err);\n    }\n}\n\nstatic const int threadsNum = 10;\nstatic memkind *testKind[threadsNum] = { nullptr };\nstatic const int mallocCount = 100000;\nstatic void *ptr[threadsNum][mallocCount] = { { nullptr } };\n\nstatic void *thread_func_FreeWithNullptr(void *arg)\n{\n    int kindIndex = *(int *)arg;\n    pthread_mutex_lock(&mutex);\n    pthread_cond_wait(&cond, &mutex);\n    pthread_mutex_unlock(&mutex);\n\n    for (int j = 0; j < mallocCount; ++j) {\n        if (ptr[kindIndex][j] == nullptr) {\n            break;\n        }\n        memkind_free(nullptr, ptr[kindIndex][j]);\n        ptr[kindIndex][j] = nullptr;\n    }\n\n    return nullptr;\n}\n\nstatic void *thread_func_FreeWithKind(void *arg)\n{\n    int kindIndex = *(int *)arg;\n    pthread_mutex_lock(&mutex);\n    pthread_cond_wait(&cond, &mutex);\n    pthread_mutex_unlock(&mutex);\n\n    for (int j = 0; j < mallocCount; ++j) {\n        if (ptr[kindIndex][j] == nullptr) {\n            break;\n        }\n        memkind_free(testKind[kindIndex], ptr[kindIndex][j]);\n    }\n\n    return nullptr;\n}\n\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemKindFreeBenchmarkWithThreads)\n{\n    const size_t allocSize = 512;\n    TimerSysTime timer;\n    double duration;\n    pthread_t *threads = (pthread_t *)calloc(threadsNum, sizeof(pthread_t));\n    ASSERT_TRUE(threads != nullptr);\n\n    for (int i = 0; i < threadsNum; ++i) {\n        int err = memkind_create_pmem(PMEM_DIR, PMEM_PART_SIZE, &testKind[i]);\n        ASSERT_EQ(err, 0);\n        ASSERT_TRUE(nullptr != testKind[i]);\n        int j = 0;\n        for (j = 0; j < mallocCount; ++j) {\n            ptr[i][j] = memkind_malloc(testKind[i], allocSize);\n            if (ptr[i][j] == nullptr) {\n                break;\n            }\n        }\n        ASSERT_TRUE(j != mallocCount);\n    }\n\n    int threadIndex[threadsNum];\n    for (int i = 0; i < threadsNum; ++i) {\n        threadIndex[i] = i;\n    }\n\n    for (int t = 0; t < threadsNum; t++) {\n        int err = pthread_create(&threads[t], nullptr, thread_func_FreeWithKind,\n                                 &threadIndex[t]);\n        ASSERT_EQ(0, err);\n    }\n\n    // sleep is here to ensure that all threads start at one the same time\n    sleep(1);\n    timer.start();\n    pthread_cond_broadcast(&cond);\n    for (int t = 0; t < threadsNum; ++t) {\n        int err = pthread_join(threads[t], nullptr);\n        ASSERT_EQ(0, err);\n    }\n    duration = timer.getElapsedTime();\n    printf(\"Free time with explicitly kind: %f\\n\", duration);\n\n    for (int i = 0; i < threadsNum; i++) {\n        int j = 0;\n        for (j = 0; j < mallocCount; ++j) {\n            ptr[i][j] = memkind_malloc(testKind[i], allocSize);\n            if (ptr[i][j] == nullptr) {\n                break;\n            }\n        }\n        ASSERT_TRUE(j != mallocCount);\n    }\n\n    for (int t = 0; t < threadsNum; ++t) {\n        int err = pthread_create(&threads[t], nullptr, thread_func_FreeWithNullptr,\n                                 &threadIndex[t]);\n        ASSERT_EQ(0, err);\n    }\n\n    // sleep is here to ensure that all threads start at one the same time\n    sleep(1);\n    timer.start();\n    pthread_cond_broadcast(&cond);\n    for (int t = 0; t < threadsNum; t++) {\n        int err = pthread_join(threads[t], nullptr);\n        ASSERT_EQ(0, err);\n    }\n\n    duration = timer.getElapsedTime();\n    printf(\"Free time with implicitly kind: %f\\n\", duration);\n\n    for (int i = 0; i < threadsNum; ++i) {\n        int err = memkind_destroy_kind(testKind[i]);\n        ASSERT_EQ(0, err);\n    }\n    free(threads);\n}\n\n/*\n * Test will create array of kinds, fill them and then attempt to free one ptr\n * passing nullptr instead of kind. Then it will try to malloc again and if it will be\n * successful the test passes (memkind_free(nullptr,...) was successful)\n */\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemFreeUsingNullptrInsteadOfKind)\n{\n    const size_t pmem_array_size = 10;\n    struct memkind *pmem_kind_array[pmem_array_size] = { nullptr };\n    const int malloc_limit = 100000;\n    void *ptr[pmem_array_size][malloc_limit] = { nullptr };\n    void *testPtr = nullptr;\n\n    for (size_t i = 0; i < pmem_array_size; ++i) {\n        int err = memkind_create_pmem(PMEM_DIR, PMEM_PART_SIZE, &pmem_kind_array[i]);\n        ASSERT_EQ(0, err);\n    }\n\n    for (size_t i = 0; i < pmem_array_size; ++i) {\n        int index = 0;\n        for (index = 0; index < malloc_limit; ++index) {\n            ptr[i][index] = memkind_malloc(pmem_kind_array[i], 1 * KB);\n\n            if (ptr[i][index] == nullptr) {\n                break;\n            }\n        }\n        ASSERT_TRUE(malloc_limit != index);\n    }\n\n    memkind_free(nullptr, ptr[5][5]);\n\n    for (size_t i = 0; i < pmem_array_size; ++i) {\n        // attempt to alloc memory to the kinds\n        testPtr = memkind_malloc(pmem_kind_array[i], 1 * KB);\n        if (i == 5) {\n            // allocation should be successful - confirmation that memkind_free(nullptr,...) works fine\n            ASSERT_TRUE(testPtr != nullptr);\n            ptr[5][5] = testPtr;\n        } else {\n            // There is no more free space in other kinds\n            ASSERT_TRUE(testPtr == nullptr);\n        }\n    }\n    // free the rest of the space and destroy kinds.\n    for (size_t i = 0; i < pmem_array_size; ++i) {\n        for (int j = 0; j < malloc_limit; ++j) {\n            if (ptr[i][j] == nullptr)\n                break;\n\n            memkind_free(pmem_kind_array[i], ptr[i][j]);\n        }\n        int err = memkind_destroy_kind(pmem_kind_array[i]);\n        ASSERT_EQ(0, err);\n    }\n}\n\n/*\n * This is a test which confirms that extent deallocation function ( pmem_extent_dalloc )\n * was called correctly for pmem allocation.\n */\nTEST_F(MemkindPmemTests, test_TC_MEMKIND_PmemCheckExtentDalloc)\n{\n    struct memkind *kind = nullptr;\n    const int mallocLimit = 10000;\n    void *ptr[mallocLimit] = { nullptr };\n    struct stat st;\n    double initialBlocks;\n\n    int err = memkind_create_pmem(PMEM_DIR, PMEM_PART_SIZE, &kind);\n    ASSERT_EQ(err, 0);\n\n    struct memkind_pmem *priv = (memkind_pmem *)kind->priv;\n\n    for (int x = 0; x < 10; ++x) {\n        // sleep is here to help trigger dalloc extent\n        sleep(2);\n        int allocCount = 0;\n        for (int i = 0; i < mallocLimit; ++i) {\n            ptr[i] = memkind_malloc(kind, 32);\n            if (ptr[i] == nullptr)\n                break;\n\n            allocCount = i;\n        }\n\n        // store initial amount of allocated blocks\n        if (x == 0) {\n            ASSERT_EQ(0, fstat(priv->fd, &st));\n            initialBlocks = st.st_blocks;\n        }\n\n        for (int i = 0; i < allocCount; ++i)\n            memkind_free(kind, ptr[i]);\n\n        ASSERT_EQ(0, fstat(priv->fd, &st));\n        // if amount of blocks is less than initial, extent was called.\n        if (initialBlocks > st.st_blocks)\n            break;\n    }\n    ASSERT_GT(initialBlocks, st.st_blocks);\n\n    err = memkind_destroy_kind(kind);\n    ASSERT_EQ(0, err);\n}\n"
  },
  {
    "path": "deps/memkind/src/test/memkind_versioning_tests.cpp",
    "content": "/*\n * Copyright (C) 2015 - 2016 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n\n#include \"memkind.h\"\n\n#include \"common.h\"\n\n/*\n * memkind versioning tests.\n */\nclass MemkindVersioningTests: public :: testing::Test\n{\n\nprotected:\n    void SetUp()\n    {}\n\n    void TearDown()\n    {}\n\n    const int max_version_value = 999;\n\n};\n\n//Test memkind_get_version().\nTEST_F(MemkindVersioningTests, test_TC_MEMKIND_GetVersionFunc)\n{\n    int max_return_val =\n        1000000 * max_version_value\n        + 1000 * max_version_value\n        + max_version_value;\n\n    //version number > 0\n    EXPECT_GT(memkind_get_version(), 0);\n\n    //version number <= max_return_val\n    EXPECT_LE(memkind_get_version(), max_return_val);\n}\n"
  },
  {
    "path": "deps/memkind/src/test/memory_footprint_test.cpp",
    "content": "/*\n * Copyright (C) 2017 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"common.h\"\n#include \"random_sizes_allocator.h\"\n#include \"proc_stat.h\"\n#include \"allocator_perf_tool/GTestAdapter.hpp\"\n#include \"allocator_perf_tool/Allocation_info.hpp\"\n\n#include <memkind.h>\n\n#include <condition_variable>\n#include <functional>\n#include <mutex>\n#include <random>\n#include <thread>\n\nclass Worker\n{\npublic:\n    Worker(RandomSizesAllocator &&allocator, double malloc_probability)\n        : allocator(std::move(allocator)), malloc_probability(malloc_probability)  {}\n\n    void work()\n    {\n        if (allocator.empty() || get_random_bool(malloc_probability)) {\n            requested_memory_sum += allocator.malloc_random_memory();\n        } else {\n            requested_memory_sum -= allocator.free_random_memory();\n        }\n    }\n\n    size_t get_requested_memory_sum_bytes() const\n    {\n        return requested_memory_sum;\n    }\n\nprivate:\n    bool get_random_bool(double probability)\n    {\n        std::bernoulli_distribution distribution(probability);\n        return distribution(generator);\n    }\n\n    std::default_random_engine generator;\n    RandomSizesAllocator allocator;\n    size_t requested_memory_sum = 0;\n    double malloc_probability;\n};\n\n\nclass MemoryFootprintStats\n{\npublic:\n    void reset()\n    {\n        std::lock_guard<std::mutex> lk(sample_guard);\n        initial_virtual_memory = proc_stat.get_virtual_memory_size_bytes();\n        initial_physical_memory = proc_stat.get_physical_memory_size_bytes();\n        vm_overhead_sum = 0;\n        current_vm_overhead = 0;\n        max_vm_overhead = 0;\n\n        current_phys_overhead = 0;\n        phys_overhead_sum = 0;\n        max_phys_overhead = 0;\n\n        requested_memory = 0;\n        sample_count = 0;\n    }\n\n    void sample(long long requested_memory_bytes)\n    {\n        std::lock_guard<std::mutex> lk(sample_guard);\n        current_vm = proc_stat.get_virtual_memory_size_bytes();\n        current_phys = proc_stat.get_physical_memory_size_bytes();\n\n        sample_count++;\n\n        requested_memory = requested_memory_bytes;\n\n        current_vm_overhead = current_vm - initial_virtual_memory - requested_memory;\n        vm_overhead_sum += current_vm_overhead;\n        max_vm_overhead = std::max(max_vm_overhead, current_vm_overhead);\n\n        current_phys_overhead = current_phys - initial_physical_memory;\n        phys_overhead_sum += current_phys_overhead;\n        max_phys_overhead = std::max(max_phys_overhead, current_phys_overhead);\n    }\n\n    void log_data() const\n    {\n        std::lock_guard<std::mutex> lk(sample_guard);\n        GTestAdapter::RecordProperty(\"avg_vm_overhead_per_operation_mb\",\n                                     convert_bytes_to_mb(vm_overhead_sum) / sample_count);\n        GTestAdapter::RecordProperty(\"avg_vm_overhead_growth_per_operation_mb\",\n                                     convert_bytes_to_mb(current_vm_overhead) / sample_count);\n        GTestAdapter::RecordProperty(\"max_vm_overhead_mb\",\n                                     convert_bytes_to_mb(max_vm_overhead));\n        GTestAdapter::RecordProperty(\"avg_phys_overhead_per_operation_mb\",\n                                     convert_bytes_to_mb(phys_overhead_sum) / sample_count);\n        GTestAdapter::RecordProperty(\"max_phys_overhead_mb\",\n                                     convert_bytes_to_mb(max_phys_overhead));\n        GTestAdapter::RecordProperty(\"overhead_to_requested_memory_ratio_percent\",\n                                     100.f * current_vm_overhead / requested_memory);\n        GTestAdapter::RecordProperty(\"requested_memory_mb\",\n                                     convert_bytes_to_mb(requested_memory));\n    }\nprivate:\n    long long initial_virtual_memory;\n    long long vm_overhead_sum = 0;\n    long long current_vm_overhead = 0;\n    long long max_vm_overhead = 0;\n\n    long long initial_physical_memory;\n    long long current_phys_overhead = 0;\n    long long phys_overhead_sum = 0;\n    long long max_phys_overhead = 0;\n\n    long long requested_memory = 0;\n    long long sample_count = 0;\n\n    long long current_vm;\n    long long current_phys;\n\n    ProcStat proc_stat;\n    mutable std::mutex sample_guard;\n};\n\n/* Execute func calling it n_calls times in n_threads threads.\n * The execution is multithreaded but the func calls order is sequential.\n * func takes thread id, and operation id as an argument,\n * and must return thread id of the next thread, where thread ids are in range <0, n_threads-1>.\n * init_thread_id specify the initial thread id.\n */\nvoid run_multithreaded_seq_exec(unsigned n_threads, unsigned init_thread_id,\n                                unsigned n_calls, std::function<unsigned(unsigned, unsigned)> func)\n{\n    std::vector<std::thread> threads;\n    std::mutex mutex;\n    std::condition_variable turns_holder;\n    unsigned current_call = 0;\n    unsigned current_tid = init_thread_id;\n\n    threads.reserve(n_threads);\n\n    mutex.lock();\n\n    for(int tid=0; tid<n_threads; ++tid) {\n        threads.emplace_back([ &, tid]() {\n            while(current_call < n_calls) {\n                std::unique_lock<std::mutex> lk(mutex);\n                turns_holder.wait(lk, [ &,tid] {return current_tid == tid || current_call == n_calls;});\n                if(current_call == n_calls) {\n                    return;\n                }\n                current_tid = func(tid, current_call);\n                ASSERT_LT(current_tid, n_threads) << \"Incorrect thread id!\";\n                current_call++;\n                lk.unlock();\n                turns_holder.notify_all();\n            }\n        });\n    }\n\n    mutex.unlock();\n\n    for(int i=0; i<threads.size(); i++) {\n        threads[i].join();\n    }\n}\n\n/*\n * Create threads and measure the cost of maintaining allocations from threads.\n * Allocations order is sequential (otherwise the results might be very nondeterministic).\n */\nvoid run_test(memkind_t kind, size_t min_size, size_t max_size,\n              unsigned n_threads, double malloc_probability=1.0, unsigned n_calls=1000)\n{\n    Worker worker(RandomSizesAllocator(kind, min_size, max_size, n_calls),\n                  malloc_probability);\n\n    MemoryFootprintStats mem_footprint_stats;\n\n    auto func = [&](unsigned tid, unsigned id) -> unsigned {\n        if(id == 0)\n        {\n            mem_footprint_stats.reset();\n        }\n\n        worker.work();\n        mem_footprint_stats.sample(worker.get_requested_memory_sum_bytes());\n\n        return (tid + 1) % n_threads; //next thread id\n    };\n    run_multithreaded_seq_exec(n_threads, 0, n_calls, func);\n    mem_footprint_stats.log_data();\n}\n\nclass MemoryFootprintTest: public :: testing::Test\n{};\n\nTEST_F(MemoryFootprintTest,\n       test_TC_MEMKIND_DEFAULT_only_malloc_small_allocations_1_thread)\n{\n    run_test(MEMKIND_DEFAULT, 128, 15 * KB, 1);\n}\n\nTEST_F(MemoryFootprintTest,\n       test_TC_MEMKIND_DEFAULT_only_malloc_small_allocations_10_thread)\n{\n    run_test(MEMKIND_DEFAULT, 128, 15 * KB, 10);\n}\n\nTEST_F(MemoryFootprintTest,\n       test_TC_MEMKIND_DEFAULT_only_malloc_medium_allocations_1_thread)\n{\n    run_test(MEMKIND_DEFAULT, 16 * KB, 1 * MB, 1);\n}\n\nTEST_F(MemoryFootprintTest,\n       test_TC_MEMKIND_DEFAULT_only_malloc_medium_allocations_10_thread)\n{\n    run_test(MEMKIND_DEFAULT, 16 * KB, 1 * MB, 10);\n}\n\nTEST_F(MemoryFootprintTest,\n       test_TC_MEMKIND_DEFAULT_only_malloc_large_allocations_1_thread)\n{\n    run_test(MEMKIND_DEFAULT, 2 * MB, 10 * MB, 1, 1.0, 100);\n}\n\nTEST_F(MemoryFootprintTest,\n       test_TC_MEMKIND_DEFAULT_only_malloc_large_allocations_10_thread)\n{\n    run_test(MEMKIND_DEFAULT, 2 * MB, 10 * MB, 10, 1.0, 100);\n}\n\nTEST_F(MemoryFootprintTest,\n       test_TC_MEMKIND_DEFAULT_random_malloc80_free20_random_small_allocations_1_thread)\n{\n    run_test(MEMKIND_DEFAULT, 128, 15 * KB, 1, 0.8);\n}\n\nTEST_F(MemoryFootprintTest,\n       test_TC_MEMKIND_DEFAULT_random_malloc80_free20_random_small_allocations_10_thread)\n{\n    run_test(MEMKIND_DEFAULT, 128, 15 * KB, 10, 0.8);\n}\n\nTEST_F(MemoryFootprintTest,\n       test_TC_MEMKIND_DEFAULT_random_malloc80_free20_random_medium_allocations_1_thread)\n{\n    run_test(MEMKIND_DEFAULT, 16 * KB, 1 * MB, 1, 0.8);\n}\n\nTEST_F(MemoryFootprintTest,\n       test_TC_MEMKIND_DEFAULT_random_malloc80_free20_random_large_allocations_10_thread)\n{\n    run_test(MEMKIND_DEFAULT, 2 * MB, 10 * MB,  10, 0.8, 100);\n}\n\nTEST_F(MemoryFootprintTest,\n       test_TC_MEMKIND_HBW_only_malloc_small_allocations_1_thread)\n{\n    run_test(MEMKIND_HBW, 128, 15 * KB, 1);\n}\n\nTEST_F(MemoryFootprintTest,\n       test_TC_MEMKIND_HBW_only_malloc_small_allocations_10_thread)\n{\n    run_test(MEMKIND_HBW, 128, 15 * KB, 10);\n}\n\nTEST_F(MemoryFootprintTest,\n       test_TC_MEMKIND_HBW_only_malloc_medium_allocations_1_thread)\n{\n    run_test(MEMKIND_HBW, 16 * KB, 1 * MB, 1);\n}\n\nTEST_F(MemoryFootprintTest,\n       test_TC_MEMKIND_HBW_only_malloc_medium_allocations_10_thread)\n{\n    run_test(MEMKIND_HBW, 16 * KB, 1 * MB, 10);\n}\n\nTEST_F(MemoryFootprintTest,\n       test_TC_MEMKIND_HBW_only_malloc_large_allocations_1_thread)\n{\n    run_test(MEMKIND_HBW, 2 * MB, 10 * MB, 1, 100);\n}\n\nTEST_F(MemoryFootprintTest,\n       test_TC_MEMKIND_HBW_only_malloc_large_allocations_10_thread)\n{\n    run_test(MEMKIND_HBW, 2 * MB, 10 * MB, 10, 100);\n}\n\nTEST_F(MemoryFootprintTest,\n       test_TC_MEMKIND_HBW_random_malloc80_free20_random_small_allocations_1_thread)\n{\n    run_test(MEMKIND_HBW, 128, 15 * KB, 1, 0.8);\n}\n\nTEST_F(MemoryFootprintTest,\n       test_TC_MEMKIND_HBW_random_malloc80_free20_random_small_allocations_10_thread)\n{\n    run_test(MEMKIND_HBW, 128, 15 * KB, 10, 0.8);\n}\n\nTEST_F(MemoryFootprintTest,\n       test_TC_MEMKIND_HBW_random_malloc80_free20_random_medium_allocations_1_thread)\n{\n    run_test(MEMKIND_HBW, 16 * KB, 1 * MB, 1, 0.8);\n}\n\nTEST_F(MemoryFootprintTest,\n       test_TC_MEMKIND_HBW_random_malloc80_free20_random_medium_allocations_10_thread)\n{\n    run_test(MEMKIND_HBW, 16 * KB, 1 * MB, 10, 0.8);\n}\n\nTEST_F(MemoryFootprintTest,\n       test_TC_MEMKIND_HBW_random_malloc80_free20_random_large_allocations_1_thread)\n{\n    run_test(MEMKIND_HBW, 2 * MB, 10 * MB, 1, 0.8, 100);\n}\n\nTEST_F(MemoryFootprintTest,\n       test_TC_MEMKIND_HBW_random_malloc80_free20_random_large_allocations_10_thread)\n{\n    run_test(MEMKIND_HBW, 2 * MB, 10 * MB, 10, 0.8, 100);\n}\n"
  },
  {
    "path": "deps/memkind/src/test/memory_manager.h",
    "content": "/*\n * Copyright (C) 2017 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#pragma once\n#include <memkind.h>\n#include <new>\n\nclass MemoryManager\n{\nprivate:\n    memkind_t kind;\n    size_t memory_size;\n    void *memory_pointer;\n\n    void move(MemoryManager &&other)\n    {\n        kind = other.kind;\n        memory_size = other.memory_size;\n        if (memory_pointer)\n            memkind_free(kind, memory_pointer);\n        memory_pointer = std::move(other.memory_pointer);\n        other.memory_pointer = nullptr;\n    }\n\npublic:\n    MemoryManager(memkind_t kind, size_t size) :\n        kind(kind),\n        memory_size(size),\n        memory_pointer(memkind_malloc(kind, size))\n    {\n        if(!memory_pointer) {\n            throw std::bad_alloc();\n        }\n    }\n\n    size_t size()\n    {\n        return memory_size;\n    }\n\n    MemoryManager(const MemoryManager &) = delete;\n\n    MemoryManager(MemoryManager &&other)\n    {\n        move(std::move(other));\n    }\n\n    MemoryManager &operator=(MemoryManager &&other)\n    {\n        move(std::move(other));\n        return *this;\n    }\n\n    ~MemoryManager()\n    {\n        if (memory_pointer)\n            memkind_free(kind, memory_pointer);\n    }\n};\n\n"
  },
  {
    "path": "deps/memkind/src/test/multithreaded_tests.cpp",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"memkind.h\"\n\n#include <fstream>\n#include <algorithm>\n\n#include \"common.h\"\n#include \"check.h\"\n#include \"omp.h\"\n#include \"trial_generator.h\"\n\n#define NTHREADS 2\n\n/* Multithread test content which will also use the trial_benerator to call\n * malloc, calloc, memalign, realloc, with different memory sizes but this time\n * it will use Open MP threads with NTHREADS set to 2\n */\nclass MultithreadedTest : public TGTest\n{\n};\n\nTEST_F(MultithreadedTest,\n       test_TC_MEMKIND_Multithread_HBW_Malloc_2bytes_2KB_2MB_sizes)\n{\n    tgen->generate_size_2bytes_2KB_2MB(HBW_MALLOC);\n    #pragma omp parallel num_threads(NTHREADS)\n    {\n        tgen->run(num_bandwidth,\n                  bandwidth);\n    }\n}\n\nTEST_F(MultithreadedTest,\n       test_TC_MEMKIND_Multithread_HBW_Calloc_2bytes_2KB_2MB_sizes)\n{\n    tgen->generate_size_2bytes_2KB_2MB(HBW_CALLOC);\n    #pragma omp parallel num_threads(NTHREADS)\n    {\n        tgen->run(num_bandwidth,\n                  bandwidth);\n    }\n}\n\nTEST_F(MultithreadedTest,\n       test_TC_MEMKIND_Multithread_HBW_Memalign_2bytes_2KB_2MB_sizes)\n{\n    tgen->generate_size_2bytes_2KB_2MB(HBW_MEMALIGN);\n    #pragma omp parallel num_threads(NTHREADS)\n    {\n        tgen->run(num_bandwidth,\n                  bandwidth);\n    }\n}\n\nTEST_F(MultithreadedTest,\n       test_TC_MEMKIND_Multithread_HBW_MemalignPsize_2bytes_2KB_2MB_sizes)\n{\n    tgen->generate_size_2bytes_2KB_2MB(HBW_MEMALIGN_PSIZE);\n    #pragma omp parallel num_threads(NTHREADS)\n    {\n        tgen->run(num_bandwidth,\n                  bandwidth);\n    }\n}\n"
  },
  {
    "path": "deps/memkind/src/test/negative_tests.cpp",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind.h>\n\n#include <fstream>\n#include <algorithm>\n#include <numa.h>\n#include <errno.h>\n#include <limits.h>\n#include <sys/sysinfo.h>\n\n#include \"common.h\"\n#include \"check.h\"\n#include \"omp.h\"\n#include \"trial_generator.h\"\n#include \"allocator_perf_tool/HugePageOrganizer.hpp\"\n\n/* Set of negative test cases for memkind, its main goal are to verify that the\n * library behaves accordingly to documentation when calling an API with\n * invalid inputs, incorrect usage, NULL pointers.\n */\nclass NegativeTest: public ::testing::Test\n{};\n\nclass NegativeTestHuge: public ::testing::Test\n{\nprivate:\n    //Enable huge pages to avoid false positive test result.\n    HugePageOrganizer huge_page_organizer = HugePageOrganizer(8);\n};\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_create_kind_zero_memtype)\n{\n    memkind_t kind;\n    int ret = memkind_create_kind(\n                  memkind_memtype_t(), //Set incorrect value.\n                  MEMKIND_POLICY_PREFERRED_LOCAL,\n                  memkind_bits_t(),\n                  &kind);\n    ASSERT_EQ(ret, MEMKIND_ERROR_INVALID);\n}\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_create_kind_incorrect_memtype)\n{\n    memkind_memtype_t memtype_flags;\n    //Set incorrect value.\n    memset(&memtype_flags, -1, sizeof(memtype_flags));\n\n    memkind_t kind;\n    int ret = memkind_create_kind(\n                  memtype_flags,\n                  MEMKIND_POLICY_PREFERRED_LOCAL,\n                  memkind_bits_t(),\n                  &kind);\n    ASSERT_EQ(ret, MEMKIND_ERROR_INVALID);\n}\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_create_kind_incorrect_policy)\n{\n    memkind_policy_t policy;\n    //Set incorrect value.\n    memset(&policy, -1, sizeof(policy));\n\n    memkind_t kind;\n    int ret = memkind_create_kind(\n                  MEMKIND_MEMTYPE_DEFAULT,\n                  policy,\n                  memkind_bits_t(),\n                  &kind);\n    ASSERT_EQ(ret, MEMKIND_ERROR_INVALID);\n}\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_create_kind_incorrect_mask)\n{\n    memkind_bits_t flags;\n\n    //Set incorrect value.\n    memset(&flags, 255, sizeof(flags));\n\n    memkind_t kind;\n    int ret = memkind_create_kind(\n                  MEMKIND_MEMTYPE_DEFAULT,\n                  MEMKIND_POLICY_PREFERRED_LOCAL,\n                  flags,\n                  &kind);\n    ASSERT_EQ(ret, MEMKIND_ERROR_INVALID);\n}\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_create_kind_DEFAULT_BIND_LOCAL)\n{\n    memkind_t kind;\n    int ret = memkind_create_kind(\n                  MEMKIND_MEMTYPE_DEFAULT,\n                  MEMKIND_POLICY_BIND_LOCAL,\n                  memkind_bits_t(),\n                  &kind);\n    ASSERT_EQ(ret, MEMKIND_ERROR_INVALID);\n}\n\nTEST_F(NegativeTest,\n       test_TC_MEMKIND_Negative_create_kind_DEFAULT_BIND_LOCAL_PAGE_SIZE_2MB)\n{\n    memkind_t kind;\n    int ret = memkind_create_kind(\n                  MEMKIND_MEMTYPE_DEFAULT,\n                  MEMKIND_POLICY_BIND_LOCAL,\n                  MEMKIND_MASK_PAGE_SIZE_2MB,\n                  &kind);\n    ASSERT_EQ(ret, MEMKIND_ERROR_INVALID);\n}\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_create_kind_DEFAULT_BIND_ALL)\n{\n    memkind_t kind;\n    int ret = memkind_create_kind(\n                  MEMKIND_MEMTYPE_DEFAULT,\n                  MEMKIND_POLICY_BIND_ALL,\n                  memkind_bits_t(),\n                  &kind);\n    ASSERT_EQ(ret, MEMKIND_ERROR_INVALID);\n}\n\nTEST_F(NegativeTest,\n       test_TC_MEMKIND_Negative_create_kind_DEFAULT_BIND_ALL_PAGE_SIZE_2MB)\n{\n    memkind_t kind;\n    int ret = memkind_create_kind(\n                  MEMKIND_MEMTYPE_DEFAULT,\n                  MEMKIND_POLICY_BIND_ALL,\n                  MEMKIND_MASK_PAGE_SIZE_2MB,\n                  &kind);\n    ASSERT_EQ(ret, MEMKIND_ERROR_INVALID);\n}\n\nTEST_F(NegativeTest,\n       test_TC_MEMKIND_Negative_create_kind_DEFAULT_INTERLEAVE_LOCAL)\n{\n    memkind_t kind;\n    int ret = memkind_create_kind(\n                  MEMKIND_MEMTYPE_DEFAULT,\n                  MEMKIND_POLICY_INTERLEAVE_LOCAL,\n                  memkind_bits_t(),\n                  &kind);\n    ASSERT_EQ(ret, MEMKIND_ERROR_INVALID);\n}\n\nTEST_F(NegativeTest,\n       test_TC_MEMKIND_Negative_create_kind_DEFAULT_INTERLEAVE_LOCAL_PAGE_SIZE_2MB)\n{\n    memkind_t kind;\n    int ret = memkind_create_kind(\n                  MEMKIND_MEMTYPE_DEFAULT,\n                  MEMKIND_POLICY_INTERLEAVE_LOCAL,\n                  MEMKIND_MASK_PAGE_SIZE_2MB,\n                  &kind);\n    ASSERT_EQ(ret, MEMKIND_ERROR_INVALID);\n}\n\nTEST_F(NegativeTest,\n       test_TC_MEMKIND_Negative_create_kind_DEFAULT_INTERLEAVE_ALL)\n{\n    memkind_t kind;\n    int ret = memkind_create_kind(\n                  MEMKIND_MEMTYPE_DEFAULT,\n                  MEMKIND_POLICY_INTERLEAVE_ALL,\n                  memkind_bits_t(),\n                  &kind);\n    ASSERT_EQ(ret, MEMKIND_ERROR_INVALID);\n}\n\nTEST_F(NegativeTest,\n       test_TC_MEMKIND_Negative_create_kind_DEFAULT_INTERLEAVE_ALL_PAGE_SIZE_2MB)\n{\n    memkind_t kind;\n    int ret = memkind_create_kind(\n                  MEMKIND_MEMTYPE_DEFAULT,\n                  MEMKIND_POLICY_INTERLEAVE_ALL,\n                  MEMKIND_MASK_PAGE_SIZE_2MB,\n                  &kind);\n    ASSERT_EQ(ret, MEMKIND_ERROR_INVALID);\n}\n\nTEST_F(NegativeTest,\n       test_TC_MEMKIND_Negative_create_kind_HIGH_BANDWIDTH_INTERLEAVE_LOCAL)\n{\n    memkind_t kind;\n    int ret = memkind_create_kind(\n                  MEMKIND_MEMTYPE_HIGH_BANDWIDTH,\n                  MEMKIND_POLICY_INTERLEAVE_LOCAL,\n                  memkind_bits_t(),\n                  &kind);\n    ASSERT_EQ(ret, MEMKIND_ERROR_INVALID);\n}\n\nTEST_F(NegativeTest,\n       test_TC_MEMKIND_Negative_create_kind_HIGH_BANDWIDTH_INTERLEAVE_LOCAL_PAGE_SIZE_2MB)\n{\n    memkind_t kind;\n    int ret = memkind_create_kind(\n                  MEMKIND_MEMTYPE_HIGH_BANDWIDTH,\n                  MEMKIND_POLICY_INTERLEAVE_LOCAL,\n                  MEMKIND_MASK_PAGE_SIZE_2MB,\n                  &kind);\n    ASSERT_EQ(ret, MEMKIND_ERROR_INVALID);\n}\n\nTEST_F(NegativeTest,\n       test_TC_MEMKIND_Negative_create_kind_HIGH_BANDWIDTH_INTERLEAVE_ALL_PAGE_SIZE_2MB)\n{\n    memkind_t kind;\n    int ret = memkind_create_kind(\n                  MEMKIND_MEMTYPE_HIGH_BANDWIDTH,\n                  MEMKIND_POLICY_INTERLEAVE_ALL,\n                  MEMKIND_MASK_PAGE_SIZE_2MB,\n                  &kind);\n    ASSERT_EQ(ret, MEMKIND_ERROR_INVALID);\n}\n\nTEST_F(NegativeTest,\n       test_TC_MEMKIND_Negative_create_kind_DEFAULT_HIGH_BANDWIDTH_BIND_ALL)\n{\n    memkind_t kind;\n    int flags_tmp = MEMKIND_MEMTYPE_DEFAULT | MEMKIND_MEMTYPE_HIGH_BANDWIDTH;\n    memkind_memtype_t memtype_flags;\n    memcpy(&memtype_flags, &flags_tmp, sizeof(memtype_flags));\n\n    int ret = memkind_create_kind(\n                  memtype_flags,\n                  MEMKIND_POLICY_BIND_ALL,\n                  memkind_bits_t(),\n                  &kind);\n    ASSERT_EQ(ret, MEMKIND_ERROR_INVALID);\n}\n\nTEST_F(NegativeTest,\n       test_TC_MEMKIND_Negative_create_kind_DEFAULT_HIGH_BANDWIDTH_INTERLEAVE_ALL_PAGE_SIZE_2MB)\n{\n    memkind_t kind;\n    int flags_tmp = MEMKIND_MEMTYPE_DEFAULT | MEMKIND_MEMTYPE_HIGH_BANDWIDTH;\n    memkind_memtype_t memtype_flags;\n    memcpy(&memtype_flags, &flags_tmp, sizeof(memtype_flags));\n\n    int ret = memkind_create_kind(\n                  memtype_flags,\n                  MEMKIND_POLICY_INTERLEAVE_ALL,\n                  MEMKIND_MASK_PAGE_SIZE_2MB,\n                  &kind);\n    ASSERT_EQ(ret, MEMKIND_ERROR_INVALID);\n}\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_ErrorMemAlign)\n{\n    int ret = 0;\n    void *ptr = NULL;\n    int err = EINVAL;\n\n    errno = 0;\n    ret = memkind_posix_memalign(MEMKIND_DEFAULT,\n                                 &ptr,\n                                 5,\n                                 100);\n    EXPECT_EQ(err, ret);\n    EXPECT_EQ(errno, 0);\n}\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_ErrorAlignment)\n{\n    int ret = 0;\n    void *ptr = NULL;\n    int err = EINVAL;\n\n    errno = 0;\n    ret = memkind_posix_memalign(MEMKIND_HBW,\n                                 &ptr,\n                                 5,\n                                 100);\n    EXPECT_EQ(err, ret);\n    EXPECT_EQ(errno, 0);\n}\n\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_ErrorAllocM)\n{\n    int ret = 0;\n    void *ptr = NULL;\n    int err = ENOMEM;\n    struct sysinfo info;\n    unsigned long long MemTotal = 0;\n\n    ret = sysinfo(&info);\n    EXPECT_EQ(ret, 0);\n\n    //Determine total memory size as totalram (total usable main memory size)\n    //multipled by mem_unit (memory unit size in bytes). This value is equal\n    //to MemTotal field in /proc/meminfo.\n    MemTotal = info.totalram * info.mem_unit;\n\n    RecordProperty(\"MemTotal_kB\", MemTotal/KB);\n\n    errno = 0;\n    ret = memkind_posix_memalign(MEMKIND_HBW,\n                                 &ptr,\n                                 16,\n                                 2*MemTotal);\n    EXPECT_EQ(err, ret);\n}\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_hbw_malloc_over_size)\n{\n    void *ptr = hbw_malloc(SIZE_MAX);\n    ASSERT_TRUE(ptr == NULL);\n}\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_hbw_malloc_size_zero)\n{\n    void *ptr = hbw_malloc(0);\n    ASSERT_TRUE(ptr == NULL);\n}\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_memkind_malloc_over_size)\n{\n    void *ptr = memkind_malloc(MEMKIND_HBW, SIZE_MAX);\n    ASSERT_TRUE(ptr == NULL);\n}\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_memkind_malloc_size_zero)\n{\n    void *ptr = memkind_malloc(MEMKIND_HBW, 0);\n    ASSERT_TRUE(ptr == NULL);\n}\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_hbw_calloc_over_size)\n{\n    void *ptr = hbw_calloc(1, SIZE_MAX);\n    ASSERT_TRUE(ptr == NULL);\n}\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_hbw_calloc_size_zero)\n{\n    void *ptr = hbw_calloc(1, 0);\n    ASSERT_TRUE(ptr == NULL);\n}\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_memkind_calloc_over_size)\n{\n    void *ptr = memkind_calloc(MEMKIND_HBW, 1, SIZE_MAX);\n    ASSERT_TRUE(ptr == NULL);\n}\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_memkind_calloc_size_zero)\n{\n    void *ptr = memkind_calloc(MEMKIND_HBW, 1, 0);\n    ASSERT_TRUE(ptr == NULL);\n}\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_hbw_realloc_over_size)\n{\n    void *ptr = hbw_realloc(NULL, SIZE_MAX);\n    ASSERT_TRUE(ptr == NULL);\n}\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_memkind_realloc_over_size)\n{\n    void *ptr = memkind_realloc(MEMKIND_HBW, NULL, SIZE_MAX);\n    ASSERT_TRUE(ptr == NULL);\n}\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_hbw_posix_memalign_over_size)\n{\n    void *ptr = NULL;\n    int ret = hbw_posix_memalign(&ptr, 4096, SIZE_MAX);\n    EXPECT_TRUE(ptr == NULL);\n    EXPECT_EQ(ENOMEM, ret);\n}\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_memkind_posix_memalign_over_size)\n{\n    void *ptr = NULL;\n    int ret = memkind_posix_memalign(MEMKIND_HBW, &ptr, 4096, SIZE_MAX);\n    EXPECT_TRUE(ptr == NULL);\n    EXPECT_EQ(ENOMEM, ret);\n}\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_GBFailureMemalign)\n{\n    int ret = 0;\n    void *ptr = NULL;\n    int err = EINVAL;\n\n    ret = hbw_posix_memalign_psize(&ptr,\n                                   1073741824,\n                                   1073741826,\n                                   HBW_PAGESIZE_1GB_STRICT);\n    EXPECT_EQ(ret, err);\n    EXPECT_TRUE(ptr == NULL);\n}\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_RegularReallocWithMemAllign)\n{\n    int ret = 0;\n    void *ptr = NULL;\n\n    ret = hbw_posix_memalign_psize(&ptr,\n                                   4096,\n                                   4096,\n                                   HBW_PAGESIZE_4KB);\n    EXPECT_EQ(ret, 0);\n    ASSERT_TRUE(ptr != NULL);\n    memset(ptr, 0, 4096);\n    ptr = hbw_realloc(ptr, 8192);\n    memset(ptr, 0, 8192);\n    hbw_free(ptr);\n}\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_SetPolicy)\n{\n    // First call should be successfull, consequent should generate a warning\n    // and be ignored\n    EXPECT_EQ(hbw_set_policy(HBW_POLICY_PREFERRED), 0);\n    EXPECT_EQ(hbw_set_policy(HBW_POLICY_BIND), EPERM);\n    EXPECT_EQ(hbw_set_policy(HBW_POLICY_INTERLEAVE), EPERM);\n    EXPECT_EQ(hbw_get_policy(), HBW_POLICY_PREFERRED);\n    EXPECT_EQ(hbw_set_policy((hbw_policy_t)0xFF), EINVAL);\n}\n\n//Check if hbw_set_policy() will be ignored after malloc.\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_SetPolicyAfterMalloc)\n{\n    void *ptr = hbw_malloc(512);\n    EXPECT_TRUE(ptr != NULL);\n\n    EXPECT_EQ(hbw_set_policy(HBW_POLICY_BIND), EPERM);\n    EXPECT_NE(hbw_get_policy(), HBW_POLICY_BIND);\n    EXPECT_EQ(hbw_get_policy(), HBW_POLICY_PREFERRED);\n\n    hbw_free(ptr);\n}\n\n//Check if hbw_set_policy() will be ignored after calloc.\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_SetPolicyAfterCalloc)\n{\n    void *ptr = hbw_calloc(512, 1);\n    EXPECT_TRUE(ptr != NULL);\n\n    EXPECT_EQ(hbw_set_policy(HBW_POLICY_BIND), EPERM);\n    EXPECT_NE(hbw_get_policy(), HBW_POLICY_BIND);\n    EXPECT_EQ(hbw_get_policy(), HBW_POLICY_PREFERRED);\n\n    hbw_free(ptr);\n}\n\n//Check if hbw_set_policy() will be ignored after realloc.\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_SetPolicyAfterRealloc)\n{\n    void *ptr = hbw_malloc(512);\n    EXPECT_TRUE(ptr != NULL);\n\n    hbw_realloc(ptr, 512);\n\n    EXPECT_EQ(hbw_set_policy(HBW_POLICY_BIND), EPERM);\n    EXPECT_NE(hbw_get_policy(), HBW_POLICY_BIND);\n    EXPECT_EQ(hbw_get_policy(), HBW_POLICY_PREFERRED);\n\n    hbw_free(ptr);\n}\n\n//Check if hbw_set_policy() will be ignored after hbw_posix_memalign.\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_SetPolicyAfterHbwPosixMemalign)\n{\n    void *ptr = NULL;\n\n    hbw_posix_memalign(&ptr, 2048, 2048);\n    EXPECT_TRUE(ptr != NULL);\n\n    EXPECT_EQ(hbw_set_policy(HBW_POLICY_BIND), EPERM);\n    EXPECT_NE(hbw_get_policy(), HBW_POLICY_BIND);\n    EXPECT_EQ(hbw_get_policy(), HBW_POLICY_PREFERRED);\n\n    hbw_free(ptr);\n}\n\n//Check if hbw_set_policy() will be ignored after hbw_posix_memalign_psize.\nTEST_F(NegativeTest,\n       test_TC_MEMKIND_Negative_SetPolicyAfterHbwPosixMemalignPsize)\n{\n    void *ptr = NULL;\n\n    hbw_posix_memalign_psize(&ptr, 2048, 2048, HBW_PAGESIZE_4KB);\n    EXPECT_TRUE(ptr != NULL);\n\n    EXPECT_EQ(hbw_set_policy(HBW_POLICY_BIND), EPERM);\n    EXPECT_NE(hbw_get_policy(), HBW_POLICY_BIND);\n    EXPECT_EQ(hbw_get_policy(), HBW_POLICY_PREFERRED);\n\n    hbw_free(ptr);\n}\n\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_GBMemalignPsizeAllign)\n{\n    void *ptr = NULL;\n    int ret = 0;\n    int err = EINVAL;\n\n    ret = hbw_posix_memalign_psize(&ptr, -1, 1024, HBW_PAGESIZE_1GB);\n    EXPECT_EQ(err, ret);\n}\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_GBNullRealloc)\n{\n    void *ptr = NULL;\n    ptr = memkind_realloc(MEMKIND_HBW_GBTLB, NULL, -1);\n    EXPECT_TRUE(ptr == NULL);\n}\n\nTEST_F(NegativeTest, test_TC_MEMKIND_Negative_GBNullFree)\n{\n    memkind_free(MEMKIND_GBTLB, NULL);\n}\n\nTEST_F(NegativeTestHuge,\n       test_TC_MEMKIND_hbwmalloc_memalign_psize_Interleave_Policy_PAGE_SIZE_2MB)\n{\n    void *ptr = NULL;\n    hbw_set_policy(HBW_POLICY_INTERLEAVE);\n    int ret = hbw_posix_memalign_psize(&ptr, 4096, 4096, HBW_PAGESIZE_2MB);\n    ASSERT_EQ(EINVAL, ret);\n}\n"
  },
  {
    "path": "deps/memkind/src/test/performance/framework.cpp",
    "content": "/*\n* Copyright (C) 2014 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n\n#include <algorithm> // sort\n#include <math.h> // log2\n#include <cassert>\n#include <iostream>\n#include \"framework.hpp\"\n\nnamespace performance_tests\n{\n    namespace ch = std::chrono;\n    using std::cout;\n    using std::endl;\n    using std::unique_lock;\n    using std::mutex;\n\n\n#ifdef __DEBUG\n    mutex g_coutMutex;\n    int g_msgLevel = 1;\n#endif\n\n    void Barrier::wait()\n    {\n        unique_lock<mutex> lock(m_barrierMutex);\n        // Decrement number of threads awaited at the barrier\n        m_waiting--;\n        if (m_waiting == 0) {\n            // Called by the last expected thread - notify all waiting threads and exit\n            m_cVar.notify_all();\n            // Store the time when barrier was released\n            if (m_releasedAt.tv_sec == 0 && m_releasedAt.tv_nsec == 0) {\n                clock_gettime(CLOCK_MONOTONIC, &m_releasedAt);\n            }\n            return;\n        }\n        // Wait unitl the last expected thread calls wait() on Barrier instance, or timeout occurs\n        m_cVar.wait_until(lock, ch::system_clock::now() + ch::seconds(10), []() {\n            return GetInstance().m_waiting == 0;\n        });\n    }\n\n    // Worker class\n    Worker::Worker(\n        uint32_t actionsCount,\n        const vector<size_t> &allocationSizes,\n        Operation *freeOperation,\n        memkind_t kind)\n        : m_actionsCount(actionsCount)\n        , m_allocationSizes(allocationSizes)\n        , m_actions(vector<Action*>(actionsCount, nullptr))\n        , m_kind(kind)\n    {\n        assert(freeOperation->getName() == OperationName::Free);\n    }\n\n    Worker::~Worker()\n    {\n        for (Action *action : m_actions) { //each action\n            delete action;\n        }\n\n    }\n\n    void Worker::init(const vector<Operation *> &testOperations,\n                      Operation *&freeOperation)\n    {\n        for(uint32_t i = 0 ; i < m_actionsCount ; i++) {\n            int bucketSize = rand() % Operation::MaxBucketSize;\n\n            for (Operation *operation : testOperations) { //each operation\n                if (operation->checkCondition(bucketSize)) {\n                    size_t size = m_allocationSizes[m_allocationSizes.size() > 1 ? rand() %\n                                                                             m_allocationSizes.size() : 0];\n                    m_actions[i] = new Action(\n                        operation,\n                        freeOperation,\n                        m_kind,\n                        size,\n                        log2(rand() % size),\n                        sizeof(void *) * (1 << ((rand() % Operation::MemalignMaxMultiplier))));\n                    break;\n                }\n            }\n        }\n    }\n\n    void Worker::run()\n    {\n        m_thread = new thread(&Worker::work, this);\n    }\n\n#ifdef __DEBUG\n    uint16_t Worker::getId()\n    {\n        return m_threadId;\n    }\n    void Worker::setId(uint16_t threadId)\n    {\n        m_threadId = threadId;\n    }\n#endif\n\n    void Worker::finish()\n    {\n        if (m_thread != nullptr) {\n            m_thread->join();\n            delete m_thread;\n        }\n    }\n\n    void Worker::work()\n    {\n        EMIT(1, \"Entering barrier \" << m_threadId)\n        Barrier::GetInstance().wait();\n        EMIT(1, \"Starting thread \" << m_threadId)\n        for (Action *action : m_actions) {\n            action->alloc();\n        }\n    }\n\n    void Worker::clean()\n    {\n        EMIT(2, \"Cleaning thread \" << m_threadId)\n        for (Action *action : m_actions) {\n            action->free();\n        }\n        EMIT(1, \"Thread \" << m_threadId << \" finished\")\n    }\n\n    // PerformanceTest class\n    PerformanceTest::PerformanceTest(\n        size_t repeatsCount,\n        size_t threadsCount,\n        size_t operationsCount)\n        : m_repeatsCount(repeatsCount)\n        , m_discardCount(repeatsCount * (distardPercent / 100.0))\n        , m_threadsCount(threadsCount)\n        , m_operationsCount(operationsCount)\n        , m_executionMode(ExecutionMode::SingleInteration)\n    {\n    }\n\n    void PerformanceTest::setAllocationSizes(const vector<size_t> &allocationSizes)\n    {\n        m_allocationSizes = allocationSizes;\n    }\n\n    void PerformanceTest::setOperations(const vector<vector<Operation *>>\n                                        &testOperations, Operation *freeOperation)\n    {\n        m_testOperations = testOperations;\n        m_freeOperation = freeOperation;\n    }\n\n    void PerformanceTest::setExecutionMode(ExecutionMode executionMode)\n    {\n        m_executionMode = executionMode;\n    }\n\n    void PerformanceTest::setKind(const vector<memkind_t> &kinds)\n    {\n        m_kinds = kinds;\n    }\n\n    inline void PerformanceTest::runIteration()\n    {\n        timespec iterationStop, iterationStart;\n\n        Barrier::GetInstance().reset(m_threadsCount);\n        for (Worker *worker : m_workers) {\n            worker->run();\n        }\n        for (Worker *worker : m_workers) {\n            worker->finish();\n        }\n        EMIT(1, \"Alloc completed\");\n        clock_gettime(CLOCK_MONOTONIC, &iterationStop);\n        iterationStart = Barrier::GetInstance().releasedAt();\n        m_durations.push_back(\n            (iterationStop.tv_sec  * NanoSecInSec + iterationStop.tv_nsec) -\n            (iterationStart.tv_sec * NanoSecInSec + iterationStart.tv_nsec)\n        );\n        for (Worker *worker : m_workers) {\n            worker->clean();\n        }\n    }\n\n    void PerformanceTest::prepareWorkers()\n    {\n        for (size_t threadId = 0; threadId < m_threadsCount; threadId++) {\n            m_workers.push_back(\n                new Worker(\n                    m_operationsCount,\n                    m_allocationSizes,\n                    m_freeOperation,\n                    m_kinds.size() > 0 ? m_kinds[threadId % m_kinds.size()] : nullptr)\n            );\n#ifdef __DEBUG\n            m_workers.back()->setId(threadId);\n#endif\n            if (m_executionMode == ExecutionMode::SingleInteration) {\n                // In ManyIterations mode, operations will be set for each thread at the beginning of each iteration\n                m_workers.back()->init(m_testOperations[threadId % m_testOperations.size()],\n                                       m_freeOperation);\n            }\n        }\n    }\n\n    Metrics PerformanceTest::getMetrics()\n    {\n        uint64_t totalDuration = 0;\n\n        std::sort(m_durations.begin(), m_durations.end());\n\n        m_durations.erase(m_durations.end() - m_discardCount, m_durations.end());\n        for (uint64_t &duration : m_durations) {\n            totalDuration += duration;\n        }\n\n        Metrics metrics;\n\n        metrics.executedOperations   = m_durations.size() * m_threadsCount *\n                                       m_operationsCount;\n        metrics.totalDuration        = totalDuration;\n        metrics.repeatDuration       = (double) totalDuration /\n                                       ((uint64_t)m_durations.size() * NanoSecInSec);\n        metrics.iterationDuration    = metrics.repeatDuration;\n        if (m_executionMode == ExecutionMode::ManyIterations) {\n            metrics.executedOperations *= m_testOperations.size();\n            metrics.iterationDuration  /= m_testOperations.size();\n        }\n        metrics.operationsPerSecond  = (double) metrics.executedOperations *\n                                       NanoSecInSec / totalDuration;\n        metrics.avgOperationDuration = (double) totalDuration /\n                                       metrics.executedOperations;\n        assert(metrics.iterationDuration != 0.0);\n        return metrics;\n    }\n\n    void PerformanceTest::writeMetrics(const string &suiteName,\n                                       const string &caseName, const string &fileName)\n    {\n        Metrics metrics = getMetrics();\n\n        // For thousands separation\n        setlocale(LC_ALL, \"\");\n        if (!fileName.empty()) {\n            FILE *f;\n            if((f = fopen(fileName.c_str(), \"a+\"))) {\n                fprintf(f,\n                        \"%s;%s;%zu;%zu;%lu;%f;%f;%f;%f\\n\",\n                        suiteName.c_str(),\n                        caseName.c_str(),\n                        m_repeatsCount,\n                        m_threadsCount,\n                        metrics.executedOperations,\n                        metrics.operationsPerSecond,\n                        metrics.avgOperationDuration,\n                        metrics.iterationDuration,\n                        metrics.repeatDuration);\n                fclose(f);\n            }\n\n        }\n        printf(\"Operations/sec:\\t\\t\\t%'f\\n\"\n               \"Avg. operation duration:\\t%f nsec\\n\"\n               \"Iteration duration:\\t\\t%f sec\\n\"\n               \"Repeat duration:\\t\\t%f sec\\n\",\n               metrics.operationsPerSecond,\n               metrics.avgOperationDuration,\n               metrics.iterationDuration,\n               metrics.repeatDuration);\n    }\n\n    int PerformanceTest::run()\n    {\n        if (m_testOperations.empty() ||\n            m_allocationSizes.empty() ||\n            m_freeOperation == nullptr) {\n            cout << \"ERROR: Test not initialized\" << endl;\n            return 1;\n        }\n        // Create threads\n        prepareWorkers();\n        //warmup kinds\n        void *alloc = nullptr;\n\n        for (const memkind_t &kind : m_kinds) {\n            m_testOperations[0][0]->perform(kind, alloc, 1e6);\n            m_freeOperation->perform(kind, alloc);\n        }\n        for (size_t repeat = 0; repeat < m_repeatsCount; repeat++) {\n            EMIT(1, \"Test run #\" << repeat)\n            if (m_executionMode == ExecutionMode::SingleInteration) {\n                runIteration();\n            } else {\n                // Perform each operations list in separate iteration, for each thread\n                for (vector<Operation *> &ops : m_testOperations) {\n                    for (Worker *worker : m_workers) {\n                        worker->init(ops, m_freeOperation);\n                    }\n                    runIteration();\n                }\n            }\n        }\n        return 0;\n    }\n\n    void PerformanceTest::showInfo()\n    {\n        printf(\"Test parameters: %lu repeats, %lu threads, %d operations per thread\\n\",\n               m_repeatsCount,\n               m_threadsCount,\n               m_operationsCount);\n        printf(\"Thread memory allocation operations:\\n\");\n        for (unsigned long i = 0; i < m_testOperations.size(); i++) {\n            if (m_executionMode == ExecutionMode::SingleInteration) {\n                printf(\"\\tThread %lu,%lu,...\\n\", i, i + (m_testOperations.size()));\n            } else {\n                printf(\"\\tIteration %lu\\n\", i);\n            }\n            for (const Operation *op : m_testOperations[i]) {\n                printf(\"\\t\\t %s (bucket size: %d)\\n\", op->getNameStr().c_str(),\n                       op->getBucketSize());\n            }\n        }\n        printf(\"Memory free operation:\\n\\t\\t%s\\n\",\n               m_freeOperation->getNameStr().c_str());\n        printf(\"Allocation sizes:\\n\");\n        for (size_t size : m_allocationSizes) {\n            printf(\"\\t\\t%lu bytes\\n\", size);\n        }\n    }\n}\n"
  },
  {
    "path": "deps/memkind/src/test/performance/framework.hpp",
    "content": "/*\n* Copyright (C) 2014 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n\n#pragma once\n\n#include <memkind.h>\n\n#include <thread>\n#include <vector>\n#include <mutex>\n#include <condition_variable>\n// Malloc, jemalloc, memkind jemalloc and memkind memory operations definitions\n#include \"operations.hpp\"\n\n/* Framework for testing memory allocators pefromance */\nnamespace performance_tests\n{\n// Nanoseconds in second\n    const uint32_t NanoSecInSec = 1e9;\n\n    // Simple barrier implementation\n    class Barrier\n    {\n        // Barrier mutex\n        std::mutex m_barrierMutex;\n        // Contitional variable\n        std::condition_variable m_cVar;\n        // Number of threads expected to enter the barrier\n        size_t m_waiting;\n        timespec m_releasedAt;\n\n    public:\n        // Called by each thread entering the barrier; returns control to caller\n        // only after been called from the last thread expected at the barrier\n        void wait();\n\n        // (Re)Initializes the barrier\n        void reset(unsigned waiting)\n        {\n            m_releasedAt.tv_sec = m_releasedAt.tv_nsec = 0;\n            m_waiting = waiting;\n        }\n\n        // Get time when barrier was released\n        timespec &releasedAt()\n        {\n            return m_releasedAt;\n        }\n\n        // Singleton\n        static Barrier &GetInstance()\n        {\n            // Automatically created and deleted one and only instance\n            static Barrier instance;\n            return instance;\n        }\n    private:\n        Barrier()\n        {\n            reset(0);\n        }\n        // Cannot be used with singleton, so prevent compiler from creating them automatically\n        Barrier(Barrier const &)        = delete;\n        void operator=(Barrier const &) = delete;\n    };\n\n    // Data of a single test action, that is, memory operation (malloc, calloc, etc.)\n    // to perform and its parameters (size, alignment etc.)\n    class Action\n    {\n    protected:\n        Operation *m_operation;\n        Operation *m_freeOperation;\n        const memkind_t m_kind;\n        void *m_allocation;\n        const size_t m_size;\n        const size_t m_offset;\n        const size_t m_alignment;\n\n    public:\n        Action(\n            Operation *operation,\n            Operation *freeOperation,\n            const memkind_t kind,\n            const size_t size,\n            const size_t offset,\n            const size_t alignment)\n            : m_operation(operation)\n            , m_freeOperation(freeOperation)\n            , m_kind(kind)\n            , m_allocation(nullptr)\n            , m_size(size)\n            , m_offset(offset)\n            , m_alignment(alignment)\n        {}\n\n        Action(\n            Operation *operation,\n            Operation *freeOperation,\n            const memkind_t kind)\n            : Action(operation, freeOperation, kind, 0, 0, 0)\n        {\n        }\n\n        void alloc()\n        {\n            m_operation->perform(m_kind, m_allocation, m_size, m_offset, m_alignment);\n        }\n\n        void free()\n        {\n            m_freeOperation->perform(m_kind, m_allocation);\n        }\n\n    };\n\n    // Performs and tracks requested memory operations in a separate thread\n    class Worker\n    {\n    protected:\n#ifdef __DEBUG\n        uint16_t m_threadId;\n#endif\n        // Requested number of test actions\n        const uint32_t m_actionsCount;\n        // List of memory block sizes - for each memory allocation operation actual value is chosen randomly\n        const vector<size_t> &m_allocationSizes;\n        // List of test actions\n        vector<Action *> m_actions;\n        // Memory free action\n        Action *m_freeAction;\n        // Operation kind (useful for memkind only)\n        memkind_t m_kind;\n        // Working thread\n        thread *m_thread;\n\n    public:\n        Worker(\n            uint32_t actionsCount,\n            const vector<size_t> &allocationSizes,\n            Operation *freeOperation,\n            memkind_t kind);\n\n        ~Worker();\n\n        // Set operations list for the worker\n        void init(const vector<Operation *> &testOperations, Operation *&freeOperation);\n\n        // Create & start thread\n        void run();\n\n#ifdef __DEBUG\n        // Get thread id\n        uint16_t getId();\n\n        // Set thread id\n        void setId(uint16_t threadId);\n#endif\n\n        // Finish thread and free all allocations made\n        void finish();\n\n        // Free allocated memory\n        virtual void clean();\n\n    private:\n        // Actual thread function (allow inheritance)\n        virtual void work();\n    };\n\n    enum ExecutionMode {\n        SingleInteration, // Single iteration, operations listS will be distributed among threads sequentially\n        ManyIterations    // Each operations list will be run in separate iteration by each thread\n    };\n\n    struct Metrics {\n        uint64_t executedOperations;\n        uint64_t totalDuration;\n        double operationsPerSecond;\n        double avgOperationDuration;\n        double iterationDuration;\n        double repeatDuration;\n    };\n\n    // Performance test parameters class\n    class PerformanceTest\n    {\n    protected:\n        // empirically determined % of worst results needed to be discarded\n        // to eliminate malloc() performance results skewness\n        static constexpr double distardPercent = 20.0;\n    protected:\n        // Number of test repeats\n        size_t                      m_repeatsCount;\n        // Number of test repeats with worst results to be discarded\n        size_t                      m_discardCount;\n        // Number of threads\n        size_t                      m_threadsCount;\n        // Number of memory operations in each thread\n        uint32_t                    m_operationsCount;\n        // List of allocation sizes\n        vector<size_t>              m_allocationSizes;\n        // List of list of allocation operations, utlization depends on execution mode\n        vector<vector<Operation *>>  m_testOperations;\n        // Free operation\n        Operation                  *m_freeOperation;\n        // List of memory kinds (for memkind allocation only)\n        // distributed among threads sequentially\n        vector<memkind_t>           m_kinds;\n        // List of thread workers\n        vector<Worker *>             m_workers;\n        // Time measurment\n        vector<uint64_t>            m_durations;\n        // Execution mode\n        ExecutionMode               m_executionMode;\n\n    public:\n        // Create test\n        PerformanceTest(\n            size_t repeatsCount,\n            size_t threadsCount,\n            size_t operationsCount);\n\n        virtual ~PerformanceTest() {}\n\n        // Set list of block sizes\n        void setAllocationSizes(const vector<size_t> &allocationSizes);\n\n        // Set list of operations per thread/per iteration (depending on execution mode)\n        void setOperations(const vector<vector<Operation *>> &testOperations,\n                           Operation *freeOperation);\n\n        // Set per-thread list of memory kinds\n        void setKind(const vector<memkind_t> &kinds);\n\n        // Set execution mode (different operations per each thread/same operations for each thread, but many iterations)\n        void setExecutionMode(ExecutionMode operationMode);\n\n        // Execute test\n        int run();\n\n        // Print test parameters\n        virtual void showInfo();\n\n        // Write test metrics\n        void writeMetrics(const string &suiteName, const string &caseName,\n                          const string &fileName = \"\");\n\n        Metrics getMetrics();\n    private:\n\n        // Run single iteration\n        void runIteration();\n\n        // Setup thread workers\n        void prepareWorkers();\n\n    };\n}\n"
  },
  {
    "path": "deps/memkind/src/test/performance/operations.hpp",
    "content": "/*\n* Copyright (C) 2014 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n\n#pragma once\n\n#include <memkind.h>\n\n#include <malloc.h>\n#include <cassert>\n#include \"jemalloc/jemalloc.h\"\n\n// Malloc, jemalloc, memkind jemalloc and memkind memory operations definitions\nnamespace performance_tests\n{\n    using std::vector;\n    using std::string;\n    using std::thread;\n\n#ifdef __DEBUG\n#include <mutex>\n    // Write entire text at once, avoiding switching to another thread\n    extern int g_msgLevel;\n    extern std::mutex g_coutMutex;\n#define EMIT(LEVEL, TEXT) \\\n    if (g_msgLevel >= LEVEL) \\\n    { \\\n        g_coutMutex.lock(); std::cout << TEXT << std::endl; g_coutMutex.unlock(); \\\n    }\n#else\n#define EMIT(LEVEL, TEXT)\n#endif\n\n    // Use jemalloc, compiled with unique prefix (--with-jemalloc-prefix= configure option)\n#ifdef SYSTEM_JEMALLOC_PREFIX\n#define TOKENPASTE(x, y) x ## y\n#define JE(x, y) TOKENPASTE(x, y)\n#define jexx_malloc JE(SYSTEM_JEMALLOC_PREFIX, malloc)\n#define jexx_calloc JE(SYSTEM_JEMALLOC_PREFIX, calloc)\n#define jexx_memalign JE(SYSTEM_JEMALLOC_PREFIX, memalign)\n#define jexx_realloc JE(SYSTEM_JEMALLOC_PREFIX, realloc)\n#define jexx_free JE(SYSTEM_JEMALLOC_PREFIX, free)\n    extern \"C\" {\n        // jemalloc function prototypes\n        // full header cannot be include due to conflict with memkind jemalloc\n        extern void *jexx_malloc(size_t size);\n        extern void *jexx_calloc(size_t num, size_t size);\n        extern void *jexx_memalign(size_t alignment, size_t size);\n        extern void *jexx_realloc(void *ptr, size_t size);\n        extern void  jexx_free(void *ptr);\n    }\n#endif\n\n    // Available memory operations\n    enum OperationName {\n        Malloc,\n        Calloc,\n        Realloc,\n        Align,\n        Free,\n        Invalid\n    };\n\n    // Reprents a memory operation\n    class Operation\n    {\n    public:\n        // Each operation is assigned a bucket size from range (0, MaxBucketSize)\n        static const unsigned MaxBucketSize = 100;\n        // For memalign operation, alignment parameter will be a random value\n        // from range (sizeof(void*), sizeof(void*) * MemalignMaxMultiplier)\n        static const unsigned MemalignMaxMultiplier = 4;\n\n    protected:\n        OperationName m_name;\n        // If random number from range (0, MaxBucketSize) is lower than m_bucketSize, an operation will be performed\n        unsigned m_bucketSize;\n\n    public:\n        Operation()\n        {\n        }\n\n        // Creates an operation with given name and bucket size\n        // If no bucket size is given, operation will be always performed\n        Operation(\n            OperationName name,\n            unsigned bucketSize = MaxBucketSize)\n            : m_name(name)\n            , m_bucketSize(bucketSize)\n        {\n            assert(bucketSize <= MaxBucketSize);\n        }\n\n        virtual ~Operation() {}\n        ;\n\n        // Check if operation should be performed (currently drawn random number lower than bucket size)\n        bool checkCondition(unsigned ballSize) const\n        {\n            return (ballSize < m_bucketSize);\n        }\n\n        OperationName getName() const\n        {\n            return m_name;\n        }\n\n        string getNameStr() const\n        {\n            switch (m_name) {\n                case OperationName::Malloc:\n                    return \"malloc\";\n\n                case OperationName::Calloc:\n                    return \"calloc\";\n\n                case OperationName::Realloc:\n                    return \"realloc\";\n\n                case OperationName::Align:\n                    return \"align\";\n\n                case OperationName::Free:\n                    return \"free\";\n\n                default:\n                    return \"<unknown>\";\n            }\n        }\n\n        // Get operation bucket size\n        unsigned getBucketSize() const\n        {\n            return m_bucketSize;\n        }\n\n        // perform memory operation\n        virtual void perform(const memkind_t &kind,\n                             void *&mem,\n                             size_t size = 0,\n                             size_t offset=0,\n                             size_t alignment=0)\n        const = 0;\n\n    };\n\n    // Malloc memory operations\n    class MallocOperation : public Operation\n    {\n    public:\n        MallocOperation(\n            OperationName name)\n            : Operation(name)\n        {\n        }\n\n        MallocOperation(\n            OperationName name,\n            unsigned bucketSize)\n            : Operation(name, bucketSize)\n        {\n        }\n\n        virtual void perform(const memkind_t &kind,\n                             void *&mem,\n                             size_t size,\n                             size_t offset,\n                             size_t alignment) const override\n        {\n            EMIT(2, \"Entering Operation::\" << getNameStr()\n                 <<  \", size=\" << size\n                 << \", offset=\" << offset\n                 << \", alignment=\" << alignment\n                 << \", mem=\" << mem)\n            switch(m_name) {\n                case Malloc: {\n                    if (mem != nullptr) {\n                        free(mem);\n                    }\n                    mem = malloc(size);\n                    break;\n                }\n                case Calloc: {\n                    if (mem != nullptr) {\n                        free(mem);\n                    }\n                    // split allocation size randomly\n                    // between number of elements and element size\n                    mem = calloc((1 << offset), (size >> offset));\n                    break;\n                }\n                case Realloc: {\n                    mem = realloc(mem, size);\n                    break;\n                }\n                case Align: {\n                    if (mem != nullptr) {\n                        free(mem);\n                    }\n                    // randomly choose alignment from (8, 8 * MemalignMaxMultiplie)\n                    mem = memalign(alignment, size);\n                    break;\n                }\n                case Free: {\n                    free(mem);\n                    mem = nullptr;\n                    break;\n                }\n\n                default:\n                    throw \"Not implemented\";\n                    break;\n            }\n            EMIT(2, \"Exiting Operation::\" << getNameStr()\n                 <<  \", size=\" << size\n                 << \", offset=\" << offset\n                 << \", alignment=\" << alignment\n                 << \", mem=\" << mem)\n        }\n    };\n\n#ifdef SYSTEM_JEMALLOC_PREFIX\n    // Jemalloc memory operations\n    class JemallocOperation : public Operation\n    {\n    public:\n        JemallocOperation(\n            OperationName name)\n            : Operation(name)\n        {\n        }\n\n        JemallocOperation(\n            OperationName name,\n            unsigned bucketSize)\n            : Operation(name, bucketSize)\n        {\n        }\n\n        virtual void perform(const memkind_t &kind,\n                             void *&mem,\n                             size_t size,\n                             size_t offset,\n                             size_t alignment) const override\n        {\n            EMIT(2, \"Entering Operation::\" << getNameStr()\n                 <<  \", size=\" << size\n                 << \", offset=\" << offset\n                 << \", alignment=\" << alignment\n                 << \", mem=\" << mem)\n            switch(m_name) {\n                case Malloc: {\n                    if (mem != nullptr) {\n                        jexx_free(mem);\n                    }\n                    mem = jexx_malloc(size);\n                    break;\n                }\n                case Calloc: {\n                    if (mem != nullptr) {\n                        jexx_free(mem);\n                    }\n                    // split allocation size randomly\n                    // between number of elements and element size\n                    mem = jexx_calloc((1 <<, offset), (size >>, offset));\n                    break;\n                }\n                case Realloc: {\n                    mem = jexx_realloc(mem, size);\n                    break;\n                }\n                case Align: {\n                    if (mem != nullptr) {\n                        jexx_free(mem);\n                    }\n                    // randomly choose alignment from (8, 8 * MemalignMaxMultiplie)\n                    mem = jexx_memalign(alignment, size);\n                    break;\n                }\n                case Free: {\n                    jexx_free(mem);\n                    mem = nullptr;\n                    break;\n                }\n\n                default:\n                    throw \"Not implemented\";\n                    break;\n            }\n            EMIT(2, \"Exiting Operation::\" << getNameStr()\n                 <<  \", size=\" << size\n                 << \", offset=\" << offset\n                 << \", alignment=\" << alignment\n                 << \", mem=\" << mem)\n        }\n    };\n#endif\n    // Jemkmalloc memory operations\n    class JemkmallocOperation : public Operation\n    {\n    public:\n        JemkmallocOperation(\n            OperationName name)\n            : Operation(name)\n        {\n        }\n\n        JemkmallocOperation(\n            OperationName name,\n            unsigned bucketSize)\n            : Operation(name, bucketSize)\n        {\n        }\n\n        virtual void perform(const memkind_t &kind,\n                             void *&mem,\n                             size_t size,\n                             size_t offset,\n                             size_t alignment) const override\n        {\n#ifdef JEMK\n            EMIT(2, \"Entering Operation::\" << getNameStr()\n                 <<  \", size=\" << size\n                 << \", offset=\" << offset\n                 << \", alignment=\" << alignment\n                 << \", mem=\" << mem)\n            switch(m_name) {\n                case Malloc: {\n                    if (mem != nullptr) {\n                        jemk_free(mem);\n                    }\n                    mem = jemk_malloc(size);\n                    break;\n                }\n                case Calloc: {\n                    if (mem != nullptr) {\n                        jemk_free(mem);\n                    }\n                    // split allocation size randomly\n                    // between number of elements and element size\n                    mem = jemk_calloc((1 << offset), (size >> offset));\n                    break;\n                }\n                case Realloc: {\n                    mem = jemk_realloc(mem, size);\n                    break;\n                }\n                case Align: {\n                    if (mem != nullptr) {\n                        jemk_free(mem);\n                    }\n                    // randomly choose alignment from (8, 8 * MemalignMaxMultiplie)\n                    mem = jemk_memalign(alignment, size);\n                    break;\n                }\n                case Free: {\n                    jemk_free(mem);\n                    mem = nullptr;\n                    break;\n                }\n\n                default:\n                    throw \"Not implemented\";\n                    break;\n            }\n            EMIT(2, \"Exiting Operation::\" << getNameStr()\n                 <<  \", size=\" << size\n                 << \", offset=\" << offset\n                 << \", alignment=\" << alignment\n                 << \", mem=\" << mem)\n#endif // JE_MK\n        }\n    };\n\n    // Memkind memory operations\n    class MemkindOperation : public Operation\n    {\n    public:\n        MemkindOperation()\n        {}\n\n        MemkindOperation(\n            OperationName name)\n            : Operation(name)\n        {\n        }\n\n        MemkindOperation(\n            OperationName name,\n            size_t bucketSize)\n            : Operation(name, bucketSize)\n        {\n        }\n\n        virtual void perform(const memkind_t &kind,\n                             void *&mem,\n                             size_t size,\n                             size_t offset,\n                             size_t alignment) const override\n        {\n            EMIT(2, \"Entering Operation::\" << getNameStr()\n                 <<  \", size=\" << size\n                 << \", offset=\" << offset\n                 << \", alignment=\" << alignment\n                 << \", mem=\" << mem)\n            switch (m_name) {\n                case Malloc: {\n                    if (mem != nullptr) {\n                        memkind_free(kind, mem);\n                    }\n                    mem = memkind_malloc(kind, size);\n                    break;\n                }\n                case Calloc: {\n                    if (mem != nullptr) {\n                        memkind_free(kind, mem);\n                    }\n                    // split allocation size randomly\n                    // between number of elements and element size\n                    mem = memkind_calloc(kind, 1 << offset, size >> offset);\n                    break;\n                }\n                case Realloc: {\n                    mem = memkind_realloc(kind, mem, size);\n                    break;\n                }\n\n                case Align: {\n                    if (mem != nullptr) {\n                        memkind_free(kind, mem);\n                        mem = nullptr;\n                    }\n                    // randomly choose alignment from (sizeof(void*), sizeof(void*) * MemalignMaxMultiplie)\n                    if (memkind_posix_memalign(kind, &mem, alignment, size) != 0) {\n                        // failure\n                        mem = nullptr;\n                    }\n                    break;\n                }\n                case Free: {\n                    memkind_free(kind, mem);\n                    mem = nullptr;\n                    break;\n                }\n\n                default:\n                    break;\n            }\n            EMIT(2, \"Exiting Operation::\" << getNameStr()\n                 <<  \", size=\" << size\n                 << \", offset=\" << offset\n                 << \", alignment=\" << alignment\n                 << \", mem=\" << mem)\n        }\n    };\n}\n"
  },
  {
    "path": "deps/memkind/src/test/performance/perf_tests.cpp",
    "content": "/*\n* Copyright (C) 2014 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n\n#include \"perf_tests.hpp\"\n#include <iostream>\n#include <cmath>\n#include <gtest/gtest.h>\n\n// Memkind performance tests\nusing std::cout;\nusing std::endl;\nusing std::abs;\nusing std::ostringstream;\n\n// Memkind tests\nclass PerformanceTest : public testing::Test\n{\nprotected:\n    const double Tolerance = 0.15;\n    const double Confidence = 0.10;\n\n    Metrics referenceMetrics;\n    Metrics performanceMetrics;\n    PerfTestCase<performance_tests::MallocOperation> referenceTest;\n    PerfTestCase<performance_tests::MemkindOperation> performanceTest;\n\n    template<class T>\n    static void RecordProperty(const string &key, const T value)\n    {\n        ostringstream values;\n        values << value;\n        testing::Test::RecordProperty(key, values.str());\n    }\n\n    void SetUp()\n    {\n    }\n\n    void TearDown()\n    {\n    }\n\n    bool checkDelta(double value, double reference, const string &info,\n                    double delta, bool higherIsBetter = false)\n    {\n        // If higherIsBetter is true, current value should be >= (reference - delta); otherwise, it should be <= (reference + delta)\n        double treshold = higherIsBetter ? reference * (1 - delta) : reference *\n                          (1 + delta);\n        cout <<\n             \"Metric: \" << info << \". Reference value: \" << reference << \". \"\n             \"Expected: \" << (higherIsBetter ? \">= \" : \"<= \") << treshold << \" (delta = \" <<\n             delta << \").\"\n             \"Actual: \" << value << \" (delta = \" <<\n             ((value > reference ? (value / reference) : (reference / value)) - 1.0) /\n             ((higherIsBetter != (value > reference)) ? 1.0 : -1.0)\n             << \").\"\n             << endl ;\n        if (higherIsBetter ? (value <= treshold) : (value >= treshold)) {\n            cout << \"WARNING : Value of '\" << info << \"' outside expected bounds!\" << endl;\n            return false;\n        }\n        return true;\n    }\n\n    bool compareMetrics(Metrics metrics, Metrics reference, double delta)\n    {\n        bool result = true;\n        result &= checkDelta(metrics.operationsPerSecond, reference.operationsPerSecond,\n                             \"operationsPerSecond\", delta, true);\n        result &= checkDelta(metrics.avgOperationDuration,\n                             reference.avgOperationDuration, \"avgOperationDuration\", delta);\n        return result;\n    }\n\n    void writeMetrics(Metrics metrics, Metrics reference)\n    {\n        RecordProperty(\"ops_per_sec\", metrics.operationsPerSecond);\n        RecordProperty(\"ops_per_sec_vs_ref\",\n                       (reference.operationsPerSecond - metrics.operationsPerSecond) * 100.0f\n                       / reference.operationsPerSecond);\n        RecordProperty(\"avg_op_time_nsec\", metrics.avgOperationDuration);\n        RecordProperty(\"avg_op_time_nsec_vs_ref\",\n                       (metrics.avgOperationDuration - reference.avgOperationDuration) * 100.0f\n                       / reference.avgOperationDuration);\n    }\n\n    void run()\n    {\n        cout << \"Running reference std::malloc test\" << endl;\n        referenceMetrics = referenceTest.runTest();\n\n        cout << \"Running memkind test\" << endl;\n        performanceMetrics = performanceTest.runTest();\n\n        writeMetrics(performanceMetrics, referenceMetrics);\n        EXPECT_TRUE(compareMetrics(performanceMetrics, referenceMetrics,\n                                   Tolerance + Confidence));\n    }\n};\n\nTEST_F(PerformanceTest, test_TC_MEMKIND_perf_single_op_single_iter)\n{\n    referenceTest.setupTest_singleOpSingleIter();\n    performanceTest.setupTest_singleOpSingleIter();\n    run();\n}\n\nTEST_F(PerformanceTest, test_TC_MEMKIND_perf_many_ops_single_iter)\n{\n    referenceTest.setupTest_manyOpsSingleIter();\n    performanceTest.setupTest_manyOpsSingleIter();\n    run();\n}\n\nTEST_F(PerformanceTest, test_TC_MEMKIND_perf_many_ops_single_iter_huge_alloc)\n{\n    referenceTest.setupTest_manyOpsSingleIterHugeAlloc();\n    performanceTest.setupTest_manyOpsSingleIterHugeAlloc();\n    run();\n}\n\nTEST_F(PerformanceTest, test_TC_MEMKIND_perf_single_op_many_iters)\n{\n    referenceTest.setupTest_singleOpManyIters();\n    performanceTest.setupTest_singleOpManyIters();\n    run();\n}\n\nTEST_F(PerformanceTest, test_TC_MEMKIND_perf_many_ops_many_iters)\n{\n    referenceTest.setupTest_manyOpsManyIters();\n    performanceTest.setupTest_manyOpsManyIters();\n    run();\n}\n\nTEST_F(PerformanceTest, test_TC_MEMKIND_perf_many_ops_many_iters_many_kinds)\n{\n    referenceTest.setupTest_manyOpsManyIters();\n    performanceTest.setupTest_manyOpsManyIters();\n    run();\n}\n"
  },
  {
    "path": "deps/memkind/src/test/performance/perf_tests.hpp",
    "content": "/*\n* Copyright (C) 2014 - 2018 Intel Corporation.\n* All rights reserved.\n*\n* Redistribution and use in source and binary forms, with or without\n* modification, are permitted provided that the following conditions are met:\n* 1. Redistributions of source code must retain the above copyright notice(s),\n*    this list of conditions and the following disclaimer.\n* 2. Redistributions in binary form must reproduce the above copyright notice(s),\n*    this list of conditions and the following disclaimer in the documentation\n*    and/or other materials provided with the distribution.\n*\n* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n* EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n*/\n\n#pragma once\n\n#include \"framework.hpp\"\n#include \"operations.hpp\"\n\nusing std::vector;\nusing std::string;\n\nusing performance_tests::Operation;\nusing performance_tests::OperationName;\nusing performance_tests::Metrics;\nusing performance_tests::ExecutionMode;\n\ntemplate <class T>\nclass PerfTestCase\n{\n\nprivate:\n    vector<vector<Operation *>> m_testOperations;\n    Operation *m_freeOperation;\n    performance_tests::PerformanceTest *m_test;\n    const unsigned m_seed = 1297654;\n    const unsigned m_repeats = 50;\n    const unsigned m_threads = 64;\n    const unsigned m_iterations = 100;\n\npublic:\n    PerfTestCase()\n    {\n        m_freeOperation = new T(OperationName::Free);\n        srand(m_seed);\n        m_test = nullptr;\n    }\n\n    ~PerfTestCase()\n    {\n        delete m_test;\n        for (vector<Operation *> ops : m_testOperations) {\n            for (Operation *op : ops) {\n                delete op;\n            }\n        }\n    }\n\n    // Perform common actions for all test cases\n    Metrics runTest(vector<memkind_t> kinds = { MEMKIND_DEFAULT })\n    {\n        m_test->setKind(kinds);\n        m_test->showInfo();\n        m_test->run();\n        return m_test->getMetrics();\n    }\n\n    // malloc only\n    // 128 bytes\n\n    void setupTest_singleOpSingleIter()\n    {\n\n        m_testOperations = { { new T(OperationName::Malloc) } };\n\n        m_test = new performance_tests::PerformanceTest(m_repeats, m_threads,\n                                                        m_iterations * 10);\n        m_test->setOperations(m_testOperations, m_freeOperation);\n        m_test->setAllocationSizes({ 128 });\n    }\n\n    // malloc, calloc, realloc and align (equal probability)\n    // 120, 521, 1200 and 4099 bytes\n    void setupTest_manyOpsSingleIter()\n    {\n        m_testOperations = { {\n                new T(OperationName::Malloc, 25),\n                new T(OperationName::Calloc, 50),\n                new T(OperationName::Realloc, 75),\n                new T(OperationName::Align, 100)\n            }\n        };\n\n        m_test = new performance_tests::PerformanceTest(m_repeats, m_threads,\n                                                        m_iterations * 10);\n        m_test->setOperations(m_testOperations, m_freeOperation);\n        m_test->setAllocationSizes({ 120, 521, 1200, 4099 });\n    }\n\n    // malloc, calloc, realloc and align (equal probability)\n    // 500000, 1000000, 2000000 and 4000000 bytes\n    void setupTest_manyOpsSingleIterHugeAlloc()\n    {\n        m_testOperations = { {\n                new T(OperationName::Malloc, 25),\n                new T(OperationName::Calloc, 50),\n                new T(OperationName::Realloc, 75),\n                new T(OperationName::Align, 100)\n            }\n        };\n\n        m_test = new performance_tests::PerformanceTest(m_repeats, m_threads,\n                                                        m_iterations);\n        m_test->setOperations(m_testOperations, m_freeOperation);\n        m_test->setAllocationSizes({ 500000, 1000000, 2000000, 4000000 });\n    }\n\n    // 4 iterations of each thread (first malloc, then calloc, realloc and align)\n    // 120, 521, 1200 and 4099 bytes\n    void setupTest_singleOpManyIters()\n    {\n        m_testOperations = {\n            { new T(OperationName::Malloc, 100) },\n            { new T(OperationName::Calloc, 100) },\n            { new T(OperationName::Realloc, 100) },\n            { new T(OperationName::Align, 100) }\n        };\n\n        m_test = new performance_tests::PerformanceTest(m_repeats, m_threads,\n                                                        m_iterations * 10);\n        m_test->setOperations(m_testOperations, m_freeOperation);\n        m_test->setAllocationSizes({ 120, 521, 1200, 4099 });\n        m_test->setExecutionMode(ExecutionMode::ManyIterations);\n    }\n\n    // 4 iterations of each thread, all with same set of operations (malloc, then calloc, realloc and align),\n    // but different operation probability\n    // 120, 521, 1200 and 4099 bytes\n    void setupTest_manyOpsManyIters()\n    {\n        m_testOperations = {\n            {\n                new T(OperationName::Malloc, 25),\n                new T(OperationName::Calloc, 50),\n                new T(OperationName::Realloc, 75),\n                new T(OperationName::Align, 100)\n            },\n            {\n                new T(OperationName::Malloc, 50),\n                new T(OperationName::Calloc, 70),\n                new T(OperationName::Realloc, 80),\n                new T(OperationName::Align, 100)\n            },\n            {\n                new T(OperationName::Calloc, 50),\n                new T(OperationName::Malloc, 60),\n                new T(OperationName::Realloc, 75),\n                new T(OperationName::Align, 100)\n            },\n            {\n                new T(OperationName::Realloc, 60),\n                new T(OperationName::Malloc, 80),\n                new T(OperationName::Calloc, 90),\n                new T(OperationName::Align, 100)\n            },\n            {\n                new T(OperationName::Realloc, 40),\n                new T(OperationName::Malloc, 55),\n                new T(OperationName::Calloc, 70),\n                new T(OperationName::Align, 100)\n            }\n        };\n\n        m_test = new performance_tests::PerformanceTest(m_repeats, m_threads,\n                                                        m_iterations * 10);\n        m_test->setOperations(m_testOperations, m_freeOperation);\n        m_test->setAllocationSizes({ 120, 521, 1200, 4099 });\n        m_test->setExecutionMode(ExecutionMode::ManyIterations);\n    }\n};\n"
  },
  {
    "path": "deps/memkind/src/test/pmem_allocator_tests.cpp",
    "content": "/*\n * Copyright (C) 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"pmem_allocator.h\"\n#include \"common.h\"\n#include <vector>\n#include <memory>\n#include <array>\n#include <list>\n#include <map>\n#include <utility>\n#include <string>\n#include <algorithm>\n#include <scoped_allocator>\n#include <thread>\n\nextern const char *PMEM_DIR;\n\n// Tests for pmem::allocator class.\nclass PmemAllocatorTests: public ::testing::Test\n{\npublic:\n    const size_t pmem_max_size = 1024*1024*1024;\n\n    pmem::allocator<int> alloc_source { std::string(PMEM_DIR), pmem_max_size };\n\n    pmem::allocator<int> alloc_source_f1 { std::string(PMEM_DIR), pmem_max_size };\n    pmem::allocator<int> alloc_source_f2 { std::string(PMEM_DIR), pmem_max_size };\n\nprotected:\n    void SetUp()\n    {}\n\n    void TearDown()\n    {}\n};\n\n\n//Compare two same kind different type allocators\nTEST_F(PmemAllocatorTests,\n       test_TC_MEMKIND_AllocatorCompare_SameKindDifferentType_Test)\n{\n    pmem::allocator<int> alc1 { alloc_source_f1 };\n    pmem::allocator<char> alc2 { alloc_source_f1 };\n    ASSERT_TRUE(alc1 == alc2);\n    ASSERT_FALSE(alc1 != alc2);\n}\n\n//Compare two same kind same type\nTEST_F(PmemAllocatorTests,\n       test_TC_MEMKIND_AllocatorCompare_SameKindSameType_Test)\n{\n    pmem::allocator<int> alc1 { alloc_source_f1 };\n    pmem::allocator<int> alc2 { alloc_source_f1 };\n    ASSERT_TRUE(alc1 == alc2);\n    ASSERT_FALSE(alc1 != alc2);\n}\n\n//Compare two different kind different type\nTEST_F(PmemAllocatorTests,\n       test_TC_MEMKIND_AllocatorCompare_DifferentKindDifferentType_Test)\n{\n    pmem::allocator<int> alc1 { alloc_source_f1 };\n    pmem::allocator<char> alc2 { alloc_source_f2 };\n    ASSERT_FALSE(alc1 == alc2);\n    ASSERT_TRUE(alc1 != alc2);\n}\n\n//Compare two different kind same type allocators\nTEST_F(PmemAllocatorTests,\n       test_TC_MEMKIND_AllocatorCompare_DifferentKindSameType_Test)\n{\n    pmem::allocator<int> alc1 { alloc_source_f1 };\n    pmem::allocator<int> alc2 { alloc_source_f2 };\n    ASSERT_FALSE(alc1 == alc2);\n    ASSERT_TRUE(alc1 != alc2);\n}\n\n//Copy assignment test\nTEST_F(PmemAllocatorTests, test_TC_MEMKIND_Allocator_CopyAssignment_Test)\n{\n    pmem::allocator<int> alc1 { alloc_source_f1 };\n    pmem::allocator<int> alc2 { alloc_source_f2 };\n    ASSERT_TRUE(alc1 != alc2);\n    alc1 = alc2;\n    ASSERT_TRUE(alc1 == alc2);\n}\n\n//Move constructor test\nTEST_F(PmemAllocatorTests, test_TC_MEMKIND_Allocator_MoveConstructor_Test)\n{\n    pmem::allocator<int> alc1 { alloc_source_f2 };\n    pmem::allocator<int> alc2 { alloc_source_f2 };\n    ASSERT_TRUE(alc2 == alc1);\n    pmem::allocator<int> alc3 = std::move(alc1);\n    ASSERT_TRUE(alc2 == alc3);\n}\n\n//Move assignment test\nTEST_F(PmemAllocatorTests, test_TC_MEMKIND_Allocator_MoveAssignment_Test)\n{\n    pmem::allocator<int> alc1 { alloc_source_f1 };\n    pmem::allocator<int> alc2 { alloc_source_f2 };\n    ASSERT_TRUE(alc1 != alc2);\n\n    {\n        pmem::allocator<int> alc3 { alc2 };\n        alc1 = std::move(alc3);\n    }\n    ASSERT_TRUE(alc1 == alc2);\n}\n\n//Single allocation-deallocation test\nTEST_F(PmemAllocatorTests,\n       test_TC_MEMKIND_Allocator_SingleAllocationDeallocation_Test)\n{\n    pmem::allocator<int> alc1 { alloc_source_f1 };\n    int *created_object = alc1.allocate(1);\n    alc1.deallocate(created_object, 1);\n}\n\n//Shared cross-allocation-deallocation test\nTEST_F(PmemAllocatorTests,\n       test_TC_MEMKIND_Allocator_SharedAllocationDeallocation_Test)\n{\n    pmem::allocator<int> alc1 { alloc_source_f1 };\n    pmem::allocator<int> alc2 { alloc_source_f1 };\n    int *created_object = nullptr;\n    int *created_object_2 = nullptr;\n    created_object = alc1.allocate(1);\n    ASSERT_TRUE(alc1 == alc2);\n    alc2.deallocate(created_object, 1);\n    created_object = alc2.allocate(1);\n    alc1.deallocate(created_object, 1);\n\n    created_object = alc1.allocate(1);\n    created_object_2 = alc2.allocate(1);\n    alc2.deallocate(created_object, 1);\n    alc1.deallocate(created_object_2, 1);\n}\n\n//Construct-destroy test\nTEST_F(PmemAllocatorTests, test_TC_MEMKIND_Allocator_ConstructDestroy_Test)\n{\n    pmem::allocator<int> alc1 { alloc_source_f1 };\n    int *created_object = alc1.allocate(1);\n    alc1.construct(created_object);\n    alc1.destroy(created_object);\n}\n\n//Testing thread-safety support\nTEST_F(PmemAllocatorTests, test_TC_MEMKIND_Allocator_MultithreadingSupport_Test)\n{\n    const size_t num_threads = 1000;\n    const size_t iteration_count = 5000;\n\n    std::vector<std::thread> thds;\n    for (size_t i = 0; i < num_threads; ++i) {\n\n        thds.push_back(std::thread( [&]() {\n            std::vector<pmem::allocator<int>> allocators_local;\n            for (size_t j = 0; j < iteration_count; j++) {\n                allocators_local.push_back(pmem::allocator<int> ( alloc_source ));\n            }\n            for (size_t j = 0; j < iteration_count; j++) {\n                allocators_local.pop_back();\n            }\n        }));\n    }\n\n    for (size_t i = 0; i < num_threads; ++i) {\n        thds[i].join();\n    }\n}\n\n//Test multiple memkind allocator usage\nTEST_F(PmemAllocatorTests, test_TC_MEMKIND_MultipleAllocatorUsage_Test)\n{\n    {\n        pmem::allocator<int> alloc { PMEM_DIR, pmem_max_size } ;\n    }\n\n    {\n        pmem::allocator<int> alloc { PMEM_DIR, pmem_max_size } ;\n    }\n}\n\n//Test vector\nTEST_F(PmemAllocatorTests, test_TC_MEMKIND_AllocatorUsage_Vector_Test)\n{\n    pmem::allocator<int> alc{ alloc_source  };\n    std::vector<int, pmem::allocator<int>> vector{ alc };\n\n    const int num = 20;\n\n    for (int i = 0; i < num; ++i) {\n        vector.push_back(0xDEAD + i);\n    }\n\n    for (int i = 0; i < num; ++i) {\n        ASSERT_TRUE(vector[i] == 0xDEAD + i);\n    }\n}\n\n//Test list\nTEST_F(PmemAllocatorTests, test_TC_MEMKIND_AllocatorUsage_List_Test)\n{\n    pmem::allocator<int> alc{ alloc_source };\n\n    std::list<int, pmem::allocator<int>> list{ alc };\n\n    const int num = 4;\n\n    for (int i = 0; i < num; ++i) {\n        list.emplace_back(0xBEAC011 + i);\n        ASSERT_TRUE(list.back() == 0xBEAC011 + i);\n    }\n\n    for (int i = 0; i < num; ++i) {\n        list.pop_back();\n    }\n}\n\n//Test map\nTEST_F(PmemAllocatorTests, test_TC_MEMKIND_AllocatorUsage_Map_Test)\n{\n    pmem::allocator<std::pair<const std::string, std::string>> alc{ alloc_source };\n\n    std::map<std::string, std::string, std::less<std::string>, pmem::allocator<std::pair<const std::string, std::string>>>\n    map{ std::less<std::string>(), alc };\n\n    const int num = 10;\n\n    for (int i = 0; i < num; ++i) {\n        map[std::to_string(i)] = std::to_string(0x0CEA11 + i);\n        ASSERT_TRUE(map[std::to_string(i)] == std::to_string(0x0CEA11 + i));\n        map[std::to_string((i * 997 + 83) % 113)] = std::to_string(0x0CEA11 + i);\n        ASSERT_TRUE(map[std::to_string((i * 997 + 83) % 113)] == std::to_string(\n                        0x0CEA11 + i));\n    }\n}\n\n#if _GLIBCXX_USE_CXX11_ABI\n//Test vector of strings\nTEST_F(PmemAllocatorTests, test_TC_MEMKIND_AllocatorUsage_VectorOfString_Test)\n{\n    typedef std::basic_string<char, std::char_traits<char>, pmem::allocator<char>>\n                                                                                pmem_string;\n    typedef pmem::allocator<pmem_string> pmem_alloc;\n\n    pmem_alloc alc{ alloc_source };\n    pmem::allocator<char> st_alc{alc};\n\n    std::vector<pmem_string,std::scoped_allocator_adaptor<pmem_alloc>> vec{ alc };\n    pmem_string arg{ \"Very very loooong striiiing\", st_alc };\n\n    vec.push_back(arg);\n    ASSERT_TRUE(vec.back() == arg);\n}\n\n//Test map of int strings\nTEST_F(PmemAllocatorTests,\n       test_TC_MEMKIND_AllocatorScopedUsage_MapOfIntString_Test)\n{\n    typedef std::basic_string<char, std::char_traits<char>, pmem::allocator<char>>\n                                                                                pmem_string;\n    typedef int key_t;\n    typedef pmem_string value_t;\n    typedef std::pair<key_t, value_t> target_pair;\n    typedef pmem::allocator<target_pair> pmem_alloc;\n    typedef pmem::allocator<char> str_allocator_t;\n    typedef std::map<key_t, value_t, std::less<key_t>, std::scoped_allocator_adaptor<pmem_alloc>>\n            map_t;\n\n    pmem_alloc map_allocator( alloc_source );\n\n    str_allocator_t str_allocator( map_allocator );\n\n    value_t source_str1(\"Lorem ipsum dolor \", str_allocator);\n    value_t source_str2(\"sit amet consectetuer adipiscing elit\", str_allocator );\n\n    map_t target_map{ std::scoped_allocator_adaptor<pmem_alloc>(map_allocator) };\n\n    target_map[key_t(165)] = source_str1;\n    ASSERT_TRUE(target_map[key_t(165)] == source_str1);\n    target_map[key_t(165)] = source_str2;\n    ASSERT_TRUE(target_map[key_t(165)] == source_str2);\n}\n#endif // _GLIBCXX_USE_CXX11_ABI\n"
  },
  {
    "path": "deps/memkind/src/test/proc_stat.h",
    "content": "/*\n * Copyright (C) 2017 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#pragma once\n#include <cstring>\n#include <fstream>\n\nclass ProcStat\n{\npublic:\n    size_t get_virtual_memory_size_bytes()\n    {\n        get_stat(\"VmSize\", str_value);\n        return strtol(str_value, NULL, 10) * 1024;\n    }\n\n    size_t get_physical_memory_size_bytes()\n    {\n        get_stat(\"VmRSS\", str_value);\n        return strtol(str_value, NULL, 10) * 1024;\n    }\nprivate:\n    /* We are avoiding to allocate local buffers,\n     * since it can produce noise in memory footprint tests.\n     */\n    char line[1024];\n    char current_entry_name[1024];\n    char str_value[1024];\n\n    // Note: this function is not thread-safe.\n    void get_stat(const char *field_name, char *value)\n    {\n        char *pos = nullptr;\n        std::ifstream file(\"/proc/self/status\", std::ifstream::in);\n        if (file.is_open()) {\n            while (file.getline(line, sizeof(line))) {\n                pos = strstr(line, field_name);\n                if (pos) {\n                    sscanf(pos, \"%64[a-zA-Z_0-9()]: %s\", current_entry_name, value);\n                    break;\n                }\n            }\n            file.close();\n        }\n    }\n};\n\n"
  },
  {
    "path": "deps/memkind/src/test/python_framework/__init__.py",
    "content": "#\n#  Copyright (C) 2016 Intel Corporation.\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#  1. Redistributions of source code must retain the above copyright notice(s),\n#     this list of conditions and the following disclaimer.\n#  2. Redistributions in binary form must reproduce the above copyright notice(s),\n#     this list of conditions and the following disclaimer in the documentation\n#     and/or other materials provided with the distribution.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n#  EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n#  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n#\n\nfrom python_framework.cmd_helper import CMD_helper\nfrom python_framework.huge_page_organizer import Huge_page_organizer\n"
  },
  {
    "path": "deps/memkind/src/test/python_framework/cmd_helper.py",
    "content": "#\n#  Copyright (C) 2017 Intel Corporation.\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#  1. Redistributions of source code must retain the above copyright notice(s),\n#     this list of conditions and the following disclaimer.\n#  2. Redistributions in binary form must reproduce the above copyright notice(s),\n#     this list of conditions and the following disclaimer in the documentation\n#     and/or other materials provided with the distribution.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n#  EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n#  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n#\n\nimport pytest\nimport os\nimport tempfile\nimport subprocess\n\nclass CMD_helper(object):\n\n    def execute_cmd(self, command, sudo=False):\n        if sudo:\n            command = \"sudo {0}\".format(command)\n        #Initialize temp file for stdout. Will be removed when closed.\n        outfile = tempfile.SpooledTemporaryFile()\n        try:\n            #Invoke process\n            p = subprocess.Popen(command, stdout=outfile, stderr=subprocess.STDOUT, shell=True)\n            p.communicate()\n            #Read stdout from file\n            outfile.seek(0)\n            stdout = outfile.read()\n        except:\n            raise\n        finally:\n            #Make sure the file is closed\n            outfile.close()\n        retcode = p.returncode\n        return stdout, retcode\n\n    def get_command_path(self, binary):\n        \"\"\"Get the path to the binary.\"\"\"\n        path = os.path.dirname(os.path.abspath(__file__))\n        return os.path.join(path, binary)\n"
  },
  {
    "path": "deps/memkind/src/test/python_framework/huge_page_organizer.py",
    "content": "#\n#  Copyright (C) 2017 Intel Corporation.\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#  1. Redistributions of source code must retain the above copyright notice(s),\n#     this list of conditions and the following disclaimer.\n#  2. Redistributions in binary form must reproduce the above copyright notice(s),\n#     this list of conditions and the following disclaimer in the documentation\n#     and/or other materials provided with the distribution.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n#  EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n#  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n#\n\nfrom cmd_helper import CMD_helper\nimport os\nimport itertools\n\n\"\"\"\nHuge_page_organizer sets hugepages per NUMA node and restores initial setup of hugepages.\nIt writes and reads from the same file, so using Huge_page_organizer with parallel\nexecution may cause undefined behaviour.\n\"\"\"\nclass Huge_page_organizer(object):\n\n    cmd_helper = CMD_helper()\n    path = \"/sys/devices/system/node/node{0}/hugepages/hugepages-2048kB/nr_hugepages\"\n    restore_values = []\n\n    def __restore(self):\n        \"\"\"Restore initial setup of hugepages.\"\"\"\n        for node_id, restore_value in enumerate(self.restore_values):\n            self.__set_nr_hugepages(node_id, restore_value)\n\n    def __get_nr_hugepages(self, node_id):\n        \"\"\"Return number of hugepages on given node or None if given node is not configured.\"\"\"\n        if not os.path.isfile(self.path.format(node_id)):\n            return None\n        with open(self.path.format(node_id), \"r\") as f:\n            return int(f.read())\n\n    def __set_nr_hugepages(self, node_id, nr_hugepages):\n        \"\"\"Set hugepages on given node to given number. Return True on success, False otherwise.\"\"\"\n        command = 'sh -c \"echo {0} >> {1}\"'.format(nr_hugepages, self.path.format(node_id))\n        output, retcode = self.cmd_helper.execute_cmd(command, sudo=True)\n        if retcode:\n            return False\n        return self.__get_nr_hugepages(node_id) is nr_hugepages\n\n    def __init__(self, nr_hugepages_per_node):\n        \"\"\"Save current hugepages setup and set hugepages per NUMA node.\"\"\"\n        for node_id in itertools.count():\n            nr_hugepages = self.__get_nr_hugepages(node_id)\n            if nr_hugepages is None:\n                break\n            self.restore_values.append(nr_hugepages)\n            retcode = self.__set_nr_hugepages(node_id, nr_hugepages_per_node)\n            if not retcode:\n               self.__restore()\n            assert retcode, \"Error: Could not set the requested amount of hugepages.\"\n\n    def __del__(self):\n        \"\"\"Call __restore() function.\"\"\"\n        self.__restore()\n"
  },
  {
    "path": "deps/memkind/src/test/random_sizes_allocator.h",
    "content": "/*\n * Copyright (C) 2017 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#pragma once\n#include \"memory_manager.h\"\n#include <vector>\n#include <random>\n\nclass RandomSizesAllocator\n{\nprivate:\n    std::vector<MemoryManager> allocated_memory;\n    memkind_t kind;\n    std::default_random_engine generator;\n    std::uniform_int_distribution<int> memory_distribution;\n\n    size_t get_random_size()\n    {\n        return memory_distribution(generator);\n    }\n\npublic:\n    RandomSizesAllocator(memkind_t kind, size_t min_size, size_t max_size,\n                         int max_allocations_number) :\n        kind(kind),\n        memory_distribution(min_size, max_size)\n    {\n        allocated_memory.reserve(max_allocations_number);\n    }\n\n    size_t malloc_random_memory()\n    {\n        size_t size = get_random_size();\n        allocated_memory.emplace_back(kind, size);\n        return size;\n    }\n\n    size_t free_random_memory()\n    {\n        if (empty())\n            return 0;\n        std::uniform_int_distribution<int> distribution(0, allocated_memory.size() - 1);\n        int random_index = distribution(generator);\n        auto it = std::begin(allocated_memory) + random_index;\n        size_t size = it->size();\n        allocated_memory.erase(it);\n        return size;\n    }\n\n    bool empty()\n    {\n        return allocated_memory.empty();\n    }\n};\n\n"
  },
  {
    "path": "deps/memkind/src/test/run_alloc_benchmark.sh",
    "content": "#!/bin/bash\n#\n#  Copyright (C) 2014 - 2018 Intel Corporation.\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#  1. Redistributions of source code must retain the above copyright notice(s),\n#     this list of conditions and the following disclaimer.\n#  2. Redistributions in binary form must reproduce the above copyright notice(s),\n#     this list of conditions and the following disclaimer in the documentation\n#     and/or other materials provided with the distribution.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n#  EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n#  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n#\n\n# Allocator\nALLOCATOR=\"glibc tbb hbw pmem\"\n# Thread configuration\nTHREADS=(1 2 4 8 16 18 36)\n# Memory configuration (in kB) / iterations\nMEMORY=(1 4 16 64 256 1024 4096 16384)\n# Iterations\nITERS=1000\n\nexport KMP_AFFINITY=scatter,granularity=fine\n\n# For each algorithm\nfor alloc in $ALLOCATOR\ndo\n    rm -f alloctest_$alloc.txt\n    echo \"# Number of threads, allocation size [kB], average malloc and free time [ms], average allocation time [ms], \\\naverage free time [ms], first allocation time [ms], first free time [ms]\" >> alloctest_$alloc.txt\n    # For each number of threads\n    for nthr in ${THREADS[*]}\n    do\n        # For each amount of memory\n        for mem in ${MEMORY[*]}\n        do\n            echo \"OMP_NUM_THREADS=$nthr ./alloc_benchmark_$alloc $ITERS $mem >> alloctest_$alloc.txt\"\n            OMP_NUM_THREADS=$nthr ./alloc_benchmark_$alloc $ITERS $mem >> alloctest_$alloc.txt\n            ret=$?\n            if [ $ret -ne 0 ]; then\n                echo \"Error: alloc_benchmark_$alloc returned $ret\"\n                exit\n            fi\n        done\n    done\ndone\n\necho \"Data collected. You can draw performance plots using python draw_plots.py.\"\n"
  },
  {
    "path": "deps/memkind/src/test/static_kinds_list.h",
    "content": "/*\n * Copyright (C) 2016 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#pragma once\n\n#include <memkind.h>\n\n\nstatic memkind_t static_kinds_list[] = {\n    MEMKIND_DEFAULT,\n    MEMKIND_HBW,\n    MEMKIND_HBW_HUGETLB,\n    MEMKIND_HBW_PREFERRED,\n    MEMKIND_HBW_PREFERRED_HUGETLB,\n    MEMKIND_HUGETLB,\n    MEMKIND_HBW_GBTLB,\n    MEMKIND_HBW_PREFERRED_GBTLB,\n    MEMKIND_GBTLB,\n    MEMKIND_HBW_INTERLEAVE,\n    MEMKIND_INTERLEAVE\n};\n\n\n"
  },
  {
    "path": "deps/memkind/src/test/static_kinds_tests.cpp",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"memkind.h\"\n#include \"memkind/internal/memkind_private.h\"\n\n#include \"common.h\"\n#include \"static_kinds_list.h\"\n\n/*\n * Set of tests for checking if static kinds meets non-trivial assumptions\n */\n\nclass StaticKindsTest: public :: testing::Test\n{\npublic:\n\nprotected:\n    void SetUp()\n    {\n    }\n};\n\n\n/*\n * Assumption: all static kinds should implement init_once operation\n * Reason:  init_once should perform memkind_register (and other initialization if needed)\n *          we are also using that fact to optimize initialization on first use (in memkind_malloc etc.)\n */\nTEST_F(StaticKindsTest, test_TC_MEMKIND_STATIC_KINDS_INIT_ONCE)\n{\n    for(size_t i=0; i<(sizeof(static_kinds_list)/sizeof(static_kinds_list[0]));\n        i++) {\n        ASSERT_TRUE(static_kinds_list[i]->ops->init_once != NULL) <<\n                                                                  static_kinds_list[i]->name << \" does not implement init_once operation!\";\n    }\n}\n\n"
  },
  {
    "path": "deps/memkind/src/test/tbbmalloc.h",
    "content": "/*\n * Copyright (C) 2016 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <stdio.h>\n#include <dlfcn.h>\n\nvoid *(*scalable_malloc)(size_t);\nvoid *(*scalable_realloc)(void *, size_t);\nvoid *(*scalable_calloc)(size_t, size_t);\nvoid  (*scalable_free)(void *);\n\nint load_tbbmalloc_symbols();\n"
  },
  {
    "path": "deps/memkind/src/test/test.sh",
    "content": "#!/bin/bash\n#\n#  Copyright (C) 2014 - 2018 Intel Corporation.\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#  1. Redistributions of source code must retain the above copyright notice(s),\n#     this list of conditions and the following disclaimer.\n#  2. Redistributions in binary form must reproduce the above copyright notice(s),\n#     this list of conditions and the following disclaimer in the documentation\n#     and/or other materials provided with the distribution.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n#  EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n#  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n#\n\nbasedir=\"$( cd \"$( dirname \"${BASH_SOURCE[0]}\" )\" && pwd )\"\nPROGNAME=`basename $0`\n\n# Default path (to installation dir of memkind-tests RPM)\nTEST_PATH=\"$basedir/\"\n\n# Gtest binaries executed by Berta\nGTEST_BINARIES=(all_tests decorator_test allocator_perf_tool_tests gb_page_tests_bind_policy)\n\n# Pytest files executed by Berta\nPYTEST_FILES=(hbw_detection_test.py autohbw_test.py trace_mechanism_test.py)\n\nred=`tput setaf 1`\ngreen=`tput setaf 2`\nyellow=`tput setaf 3`\ndefault=`tput sgr0`\n\nerr=0\n\nfunction usage () {\n   cat <<EOF\n\nUsage: $PROGNAME [-c csv_file] [-l log_file] [-f test_filter] [-T tests_dir] [-d] [-m] [-p] [-x] [-s] [-h]\n\nOPTIONS\n    -c,\n        path to CSV file\n    -l,\n        path to LOG file\n    -f,\n        test filter\n    -T,\n        path to tests directory\n    -d,\n        skip high bandwidth memory nodes detection tests\n    -m,\n        skip tests that require 2MB pages configured on the machine\n    -p,\n        skip python tests\n    -x,\n        skip tests that are passed as value\n    -s,\n        run disabled test (e.g. pmem long time stress test)\n    -h,\n        parameter added to display script usage\nEOF\n}\n\nfunction emit() {\n    if [ \"$LOG_FILE\" != \"\" ]; then\n        echo \"$@\" 2>&1 | tee -a $LOG_FILE;\n    else\n        echo \"$@\"\n    fi\n}\n\nfunction normalize_path {\n    local PATH=$1\n    if [ ! -d $PATH ];\n    then\n        echo \"Not a directory: '$PATH'\"\n        usage\n        exit 1\n    fi\n    if [[ $PATH != /* ]]; then\n        PATH=`pwd`/$PATH\n    fi\n    echo $PATH\n}\n\nfunction show_skipped_tests()\n{\n    SKIP_PATTERN=$1\n    DEFAULT_IFS=$IFS\n\n    # Search for gtests that match given pattern\n    for i in ${!GTEST_BINARIES[*]}; do\n        GTEST_BINARY_PATH=$TEST_PATH${GTEST_BINARIES[$i]}\n        for LINE in $($GTEST_BINARY_PATH --gtest_list_tests); do\n            if [[ $LINE == *. ]]; then\n                TEST_SUITE=$LINE;\n            else\n                if [[ \"$TEST_SUITE$LINE\" == *\"$SKIP_PATTERN\"* ]]; then\n                    emit \"$TEST_SUITE$LINE,${yellow}SKIPPED${default}\"\n                fi\n            fi\n        done\n    done\n\n    # Search for pytests that match given pattern\n    for i in ${!PYTEST_FILES[*]}; do\n        PTEST_BINARY_PATH=$TEST_PATH${PYTEST_FILES[$i]}\n        IFS=$'\\n'\n        for LINE in $(py.test $PTEST_BINARY_PATH --collect-only); do\n            if [[ $LINE == *\"<Class \"* ]]; then\n                TEST_SUITE=$(sed \"s/^.*'\\(.*\\)'.*$/\\1/\" <<< $LINE)\n            elif [[ $LINE == *\"<Function \"* ]]; then\n                LINE=$(sed \"s/^.*'\\(.*\\)'.*$/\\1/\" <<< $LINE)\n                if [[ \"$TEST_SUITE.$LINE\" == *\"$SKIP_PATTERN\"* ]]; then\n                    emit \"$TEST_SUITE.$LINE,${yellow}SKIPPED${default}\"\n                fi\n            fi\n        done\n    done\n\n    IFS=$DEFAULT_IFS\n    emit \"\"\n}\n\nfunction execute_gtest()\n{\n    ret_val=1\n    TESTCMD=$1\n    TEST=$2\n    # Apply filter (if provided)\n    if [ \"$TEST_FILTER\" != \"\" ]; then\n        if [[ $TEST != $TEST_FILTER ]]; then\n            return\n        fi\n    fi\n    # Concatenate test command\n    TESTCMD=$(printf \"$TESTCMD\" \"$TEST\"\"$SKIPPED_GTESTS\"\"$RUN_DISABLED_GTEST\")\n    # And test prefix if applicable\n    if [ \"$TEST_PREFIX\" != \"\" ]; then\n        TESTCMD=$(printf \"$TEST_PREFIX\" \"$TESTCMD\")\n    fi\n    OUTPUT=`eval $TESTCMD`\n    PATOK='.*OK ].*'\n    PATFAILED='.*FAILED  ].*'\n        PATSKIPPED='.*PASSED  ] 0.*'\n    if [[ $OUTPUT =~ $PATOK ]]; then\n        RESULT=\"$TEST,${green}PASSED${default}\"\n        ret_val=0\n    elif [[ $OUTPUT =~ $PATFAILED ]]; then\n        RESULT=\"$TEST,${red}FAILED${default}\"\n    elif [[ $OUTPUT =~ $PATSKIPPED ]]; then\n        return 0\n    else\n        RESULT=\"$TEST,${red}CRASH${default}\"\n    fi\n    if [ \"$CSV\" != \"\" ]; then\n        emit \"$OUTPUT\"\n        echo $RESULT >> $CSV\n    else\n        echo $RESULT\n    fi\n    return $ret_val\n}\n\nfunction execute_pytest()\n{\n    ret=1\n    TESTCMD=$1\n    TEST_SUITE=$2\n    TEST=$3\n    # Apply filter (if provided)\n    if [ \"$TEST_FILTER\" != \"\" ]; then\n        if [[ $TEST_SUITE.$TEST != $TEST_FILTER ]]; then\n            return\n        fi\n    fi\n    # Concatenate test command\n    TESTCMD=$(printf \"$TESTCMD\" \"$TEST$SKIPPED_PYTESTS\")\n    # And test prefix if applicable\n    if [ \"$TEST_PREFIX\" != \"\" ]; then\n        TESTCMD=$(printf \"$TEST_PREFIX\" \"$TESTCMD\")\n    fi\n    OUTPUT=`eval $TESTCMD`\n    PATOK='.*1 passed.*'\n    PATFAILED='.*1 failed.*'\n    PATSKIPPED='.*deselected.*'\n    if [[ $OUTPUT =~ $PATOK ]]; then\n        RESULT=\"$TEST_SUITE.$TEST,${green}PASSED${default}\"\n        ret=0\n    elif [[ $OUTPUT =~ $PATFAILED ]]; then\n        RESULT=\"$TEST_SUITE.$TEST,${red}FAILED${default}\"\n    elif [[ $OUTPUT =~ $PATSKIPPED ]]; then\n        return 0\n    else\n        RESULT=\"$TEST_SUITE.$TEST,${red}CRASH${default}\"\n    fi\n    if [ \"$CSV\" != \"\" ]; then\n        emit \"$OUTPUT\"\n        echo $RESULT >> $CSV\n    else\n        echo $RESULT\n    fi\n    return $ret\n}\n\nnumactl --hardware | grep \"^node 1\" > /dev/null\nif [ $? -ne 0 ]; then\n    echo \"ERROR: $0 requires a NUMA enabled system with more than one node.\"\n    exit 1\nfi\n\nif [ ! -f /usr/bin/memkind-hbw-nodes ]; then\n        if [ -x ./memkind-hbw-nodes ]; then\n                export PATH=$PATH:$PWD\n        else\n                echo \"Cannot find 'memkind-hbw-nodes' in $PWD. Did you run 'make'?\"\n                exit 1\n        fi\nfi\nret=$(memkind-hbw-nodes)\nif [[ $ret == \"\" ]]; then\n    export MEMKIND_HBW_NODES=1\n    TEST_PREFIX=\"numactl --membind=0 --cpunodebind=$MEMKIND_HBW_NODES %s\"\nfi\n\nOPTIND=1\n\nwhile getopts \"T:c:f:l:hdmsx:p:\" opt; do\n    case \"$opt\" in\n        T)\n            TEST_PATH=$OPTARG;\n            ;;\n        c)\n            CSV=$OPTARG;\n            ;;\n        f)\n            TEST_FILTER=$OPTARG;\n            ;;\n        l)\n            LOG_FILE=$OPTARG;\n            ;;\n        m)\n            echo \"Skipping tests that require 2MB pages due to unsatisfactory system conditions\"\n            if [[ \"$SKIPPED_GTESTS\" == \"\" ]]; then\n                SKIPPED_GTESTS=\":-*test_TC_MEMKIND_2MBPages_*\"\n            else\n                SKIPPED_GTESTS=$SKIPPED_GTESTS\":*test_TC_MEMKIND_2MBPages_*\"\n            fi\n            if [[ \"$SKIPPED_PYTESTS\" == \"\" ]]; then\n                SKIPPED_PYTESTS=\" and not test_TC_MEMKIND_2MBPages_\"\n            else\n                SKIPPED_PYTESTS=$SKIPPED_PYTESTS\" and not test_TC_MEMKIND_2MBPages_\"\n            fi\n            show_skipped_tests \"test_TC_MEMKIND_2MBPages_\"\n            ;;\n        d)\n            echo \"Skipping tests that detect high bandwidth memory nodes due to unsatisfactory system conditions\"\n            if [[ $SKIPPED_PYTESTS == \"\" ]]; then\n                SKIPPED_PYTESTS=\" and not hbw_detection\"\n            else\n                SKIPPED_PYTESTS=$SKIPPED_PYTESTS\" and not hbw_detection\"\n            fi\n            show_skipped_tests \"test_TC_MEMKIND_hbw_detection\"\n            ;;\n        p)\n            SKIPPED_PYTESTS=$SKIPPED_PYTESTS$OPTARG\n            show_skipped_tests \"$OPTARG\"\n            break;\n            ;;\n        x)\n            echo \"Skipping some tests on demand '$OPTARG'\"\n            if [[ $SKIPPED_GTESTS == \"\" ]]; then\n                SKIPPED_GTESTS=\":-\"$OPTARG\n            else\n                SKIPPED_GTESTS=$SKIPPED_GTESTS\":\"$OPTARG\n            fi\n            show_skipped_tests \"$OPTARG\"\n            ;;\n        s)\n            echo \"Run also disabled tests\"\n            RUN_DISABLED_GTEST=\" --gtest_also_run_disabled_tests\"\n            ;;\n        h)\n            usage;\n            ;;\n    esac\ndone\n\nTEST_PATH=`normalize_path \"$TEST_PATH\"`\n\n# Clear any remnants of previous execution(s)\nrm -rf $CSV\nrm -rf $LOG_FILE\n\n# Run tests written in gtest\nfor i in ${!GTEST_BINARIES[*]}; do\n    GTEST_BINARY_PATH=$TEST_PATH${GTEST_BINARIES[$i]}\n    emit\n    emit \"### Processing gtest binary '$GTEST_BINARY_PATH' ###\"\n    for LINE in $($GTEST_BINARY_PATH --gtest_list_tests); do\n        if [[ $LINE == *. ]]; then\n            TEST_SUITE=$LINE;\n        else\n            TEST_CMD=\"$GTEST_BINARY_PATH --gtest_filter=%s 2>&1\"\n            execute_gtest \"$TEST_CMD\" \"$TEST_SUITE$LINE\"\n            ret=$?\n            if [ $err -eq 0 ]; then err=$ret; fi\n        fi\n    done\ndone\n\n# Run tests written in pytest\nfor i in ${!PYTEST_FILES[*]}; do\n    PTEST_BINARY_PATH=$TEST_PATH${PYTEST_FILES[$i]}\n    emit\n    emit \"### Processing pytest file '$PTEST_BINARY_PATH' ###\"\n    IFS=$'\\n'\n    for LINE in $(py.test $PTEST_BINARY_PATH --collect-only); do\n        if [[ $LINE == *\"<Class \"* ]]; then\n            TEST_SUITE=$(sed \"s/^.*'\\(.*\\)'.*$/\\1/\" <<< $LINE)\n        elif [[ $LINE == *\"<Function \"* ]]; then\n            LINE=$(sed \"s/^.*'\\(.*\\)'.*$/\\1/\" <<< $LINE)\n            TEST_CMD=\"py.test $PTEST_BINARY_PATH -k='%s' 2>&1\"\n            execute_pytest \"$TEST_CMD\" \"$TEST_SUITE\" \"$LINE\"\n            ret=$?\n            if [ $err -eq 0 ]; then err=$ret; fi\n        fi\n    done\ndone\n\nexit $err\n"
  },
  {
    "path": "deps/memkind/src/test/trace_mechanism_test.py",
    "content": "#\n#  Copyright (C) 2016 Intel Corporation.\n#  All rights reserved.\n#\n#  Redistribution and use in source and binary forms, with or without\n#  modification, are permitted provided that the following conditions are met:\n#  1. Redistributions of source code must retain the above copyright notice(s),\n#     this list of conditions and the following disclaimer.\n#  2. Redistributions in binary form must reproduce the above copyright notice(s),\n#     this list of conditions and the following disclaimer in the documentation\n#     and/or other materials provided with the distribution.\n#\n#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n#  EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n#  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n#  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n#  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n#\n\nimport pytest\nimport os\nimport tempfile\nimport subprocess\nfrom python_framework import CMD_helper\nfrom python_framework import Huge_page_organizer\n\nclass Test_trace_mechanism(object):\n    binary = \"../trace_mechanism_test_helper\"\n    fail_msg = \"Test failed with:\\n {0}\"\n    debug_env = \"MEMKIND_DEBUG=1 \"\n    cmd_helper = CMD_helper()\n\n    def test_TC_MEMKIND_logging_MEMKIND_HBW(self):\n        #This test executes trace_mechanism_test_helper and test if MEMKIND_INFO message occurs while calling MEMKIND_HBW\n        command = self.debug_env + self.cmd_helper.get_command_path(self.binary) + \" MEMKIND_HBW\"\n        print \"Executing command: {0}\".format(command)\n        output, retcode = self.cmd_helper.execute_cmd(command, sudo=False)\n        assert retcode == 0, self.fail_msg.format(\"\\nError: trace_mechanism_test_helper returned {0} \\noutput: {1}\".format(retcode,output))\n        assert \"MEMKIND_INFO: NUMA node\" in output, self.fail_msg.format(\"\\nError: trace mechanism in memkind doesn't show MEMKIND_INFO message \\noutput: {0}\").format(output)\n\n    def test_TC_MEMKIND_2MBPages_logging_MEMKIND_HUGETLB(self):\n        huge_page_organizer = Huge_page_organizer(8)\n        #This test executes trace_mechanism_test_helper and test if MEMKIND_INFO message occurs while calling MEMKIND_HUGETLB\n        command = self.debug_env + self.cmd_helper.get_command_path(self.binary) + \" MEMKIND_HUGETLB\"\n        print \"Executing command: {0}\".format(command)\n        output, retcode= self.cmd_helper.execute_cmd(command, sudo=False)\n        assert retcode == 0, self.fail_msg.format(\"\\nError: trace_mechanism_test_helper returned {0} \\noutput: {1}\".format(retcode,output))\n        assert \"MEMKIND_INFO: Number of\" in output, self.fail_msg.format(\"\\nError: trace mechanism in memkind doesn't show MEMKIND_INFO message \\noutput: {0}\").format(output)\n        assert \"MEMKIND_INFO: Overcommit limit for\" in output, self.fail_msg.format(\"\\nError: trace mechanism in memkind doesn't show MEMKIND_INFO message \\n output: {0}\").format(output)\n\n    def test_TC_MEMKIND_logging_negative_MEMKIND_DEBUG_env(self):\n        #This test executes trace_mechanism_test_helper and test if setting MEMKIND_DEBUG to wrong value causes MEMKIND_WARNING message\n        command = \"MEMKIND_DEBUG=-1 \" + self.cmd_helper.get_command_path(self.binary) + \" MEMKIND_HBW\"\n        print \"Executing command: {0}\".format(command)\n        output, retcode = self.cmd_helper.execute_cmd(command, sudo=False)\n        assert retcode == 0, self.fail_msg.format(\"\\nError: trace_mechanism_test_helper returned {0} \\noutput: {1}\".format(retcode,output))\n        assert \"MEMKIND_WARNING: debug option\" in output, self.fail_msg.format(\"\\nError: setting wrong MEMKIND_DEBUG environment variable doesn't show MEMKIND_WARNING \\noutput: {0})\").format(output)\n"
  },
  {
    "path": "deps/memkind/src/test/trace_mechanism_test_helper.c",
    "content": "/*\n * Copyright (C) 2016 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n#include \"memkind.h\"\n\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\nint main(int argc, char *argv[])\n{\n    int err = 0;\n    const size_t size = 1024 * 1024;\n    void *buf = NULL;\n\n    //It is expected that \"MEMKIND_HBW\" or \"MEMKIND_HUGETLB\" argument is passed\n    if (argc != 2) {\n        printf(\"Error: Wrong number of parameters\\n\");\n        err = -1;\n        return err;\n    }\n\n    if (strcmp(argv[1], \"MEMKIND_HBW\") == 0) {\n        buf = memkind_malloc(MEMKIND_HBW, size);\n        if (buf == NULL) {\n            printf(\"Allocation of MEMKIND_HBW failed\\n\");\n            err = -1;\n        }\n        memkind_free(MEMKIND_HBW, buf);\n    } else if (strcmp(argv[1], \"MEMKIND_HUGETLB\") == 0) {\n        buf = memkind_malloc(MEMKIND_HUGETLB, size);\n        if (buf == NULL) {\n            printf(\"Allocation of MEMKIND_HUGETLB failed\\n\");\n            err = -1;\n        }\n        memkind_free(MEMKIND_HUGETLB, buf);\n    } else {\n        printf(\"Error: unknown parameter\\n\");\n        err = -1;\n    }\n\n    return err;\n}\n"
  },
  {
    "path": "deps/memkind/src/test/trial_generator.cpp",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <memkind/internal/memkind_hbw.h>\n#include \"allocator_perf_tool/HugePageOrganizer.hpp\"\n\n#include \"trial_generator.h\"\n#include \"check.h\"\n#include <vector>\n#include <numa.h>\n#include <numaif.h>\n\ntrial_t TrialGenerator :: create_trial_tuple(alloc_api_t api,\n                                             size_t size,\n                                             size_t alignment,\n                                             int page_size,\n                                             memkind_t memkind,\n                                             int free_index)\n{\n    trial_t ltrial;\n    ltrial.api = api;\n    ltrial.size = size;\n    ltrial.alignment = alignment;\n    ltrial.page_size = page_size;\n    ltrial.memkind = memkind;\n    ltrial.free_index = free_index;\n    return ltrial;\n}\n\n\nvoid TrialGenerator :: generate_gb (alloc_api_t api, int number_of_gb_pages,\n                                    memkind_t memkind, alloc_api_t api_free, bool psize_strict, size_t align)\n{\n    std::vector<size_t> sizes_to_alloc;\n    //When API = HBW_MEMALIGN_PSIZE: psize is set to HBW_PAGESIZE_1GB_STRICT when allocation is a multiple of 1GB. Otherwise it is set to HBW_PAGESIZE_1GB.\n    for (int i=1; i <= number_of_gb_pages; i++) {\n        if (psize_strict || api!=HBW_MEMALIGN_PSIZE)\n            sizes_to_alloc.push_back(i*GB);\n        else\n            sizes_to_alloc.push_back(i*GB+1);\n    }\n    int k = 0;\n    trial_vec.clear();\n\n    for (int i = 0; i< (int)sizes_to_alloc.size(); i++) {\n        trial_vec.push_back(create_trial_tuple(api, sizes_to_alloc[i],\n                                               align, 2*MB,\n                                               memkind,\n                                               -1));\n        if (i > 0)\n            k++;\n        trial_vec.push_back(create_trial_tuple(api_free,0,0,0,\n                                               memkind,\n                                               k++));\n    }\n}\n\nint n_random(int i)\n{\n    return random() % i;\n}\n\nvoid TrialGenerator :: generate_size_2bytes_2KB_2MB(alloc_api_t api)\n{\n    size_t size[] = {2, 2*KB, 2*MB};\n\n    int k = 0;\n    trial_vec.clear();\n    for (unsigned int i = 0; i < (int)(sizeof(size)/sizeof(size[0]));\n         i++) {\n        trial_vec.push_back(\n            create_trial_tuple(\n                api,size[i],\n                32,\n                4096,\n                MEMKIND_HBW,\n                -1\n            )\n        );\n\n        if (i > 0) k++;\n        trial_vec.push_back(create_trial_tuple(HBW_FREE, 0, 0, 0,\n                                               MEMKIND_HBW, k));\n        k++;\n    }\n}\n\nvoid TrialGenerator :: print()\n{\n\n    std::vector<trial_t>:: iterator it;\n\n    std::cout <<\"*********** Size: \"<< trial_vec.size()\n              <<\"********\\n\";\n    std::cout << \"SIZE PSIZE ALIGN FREE KIND\"<<std::endl;\n\n    for (it = trial_vec.begin();\n         it != trial_vec.end();\n         it++) {\n        std::cout << it->size <<\" \"\n                  << it->page_size <<\" \"\n                  << it->alignment <<\" \"\n                  << it->free_index <<\" \"\n                  << it->memkind <<\" \"\n                  <<std::endl;\n    }\n\n}\n\n\nvoid TrialGenerator :: run(int num_bandwidth, std::vector<int> &bandwidth)\n{\n\n    int num_trial = trial_vec.size();\n    int i, ret = 0;\n    void **ptr_vec = NULL;\n\n    ptr_vec = (void **) malloc (num_trial *\n                                sizeof (void *));\n    if (NULL == ptr_vec) {\n        fprintf (stderr, \"Error in allocating ptr array\\n\");\n        exit(-1);\n    }\n\n    for (i = 0; i < num_trial; ++i) {\n        ptr_vec[i] = NULL;\n    }\n    for (i = 0; i < num_trial; ++i) {\n        switch(trial_vec[i].api) {\n            case HBW_FREE:\n                if (i == num_trial - 1 || trial_vec[i + 1].api != HBW_REALLOC) {\n                    hbw_free(ptr_vec[trial_vec[i].free_index]);\n                    ptr_vec[trial_vec[i].free_index] = NULL;\n                    ptr_vec[i] = NULL;\n                } else {\n                    ptr_vec[i + 1] = hbw_realloc(ptr_vec[trial_vec[i].free_index],\n                                                 trial_vec[i + 1].size);\n                    ptr_vec[trial_vec[i].free_index] = NULL;\n                }\n                break;\n            case MEMKIND_FREE:\n                if (i == num_trial - 1 || trial_vec[i + 1].api != MEMKIND_REALLOC) {\n                    memkind_free(trial_vec[i].memkind,\n                                 ptr_vec[trial_vec[i].free_index]);\n                    ptr_vec[trial_vec[i].free_index] = NULL;\n                    ptr_vec[i] = NULL;\n                } else {\n                    ptr_vec[i + 1] = memkind_realloc(trial_vec[i].memkind,\n                                                     ptr_vec[trial_vec[i].free_index],\n                                                     trial_vec[i + 1].size);\n                    ptr_vec[trial_vec[i].free_index] = NULL;\n                }\n                break;\n            case HBW_MALLOC:\n                fprintf (stdout,\"Allocating %zd bytes using hbw_malloc\\n\",\n                         trial_vec[i].size);\n                ptr_vec[i] = hbw_malloc(trial_vec[i].size);\n                break;\n            case HBW_CALLOC:\n                fprintf (stdout,\"Allocating %zd bytes using hbw_calloc\\n\",\n                         trial_vec[i].size);\n                ptr_vec[i] = hbw_calloc(trial_vec[i].size, 1);\n                break;\n            case HBW_REALLOC:\n                fprintf (stdout,\"Allocating %zd bytes using hbw_realloc\\n\",\n                         trial_vec[i].size);\n                fflush(stdout);\n                if (NULL == ptr_vec[i]) {\n                    ptr_vec[i] = hbw_realloc(NULL, trial_vec[i].size);\n                }\n                break;\n            case HBW_MEMALIGN:\n                fprintf (stdout,\"Allocating %zd bytes using hbw_memalign\\n\",\n                         trial_vec[i].size);\n                ret =  hbw_posix_memalign(&ptr_vec[i],\n                                          trial_vec[i].alignment,\n                                          trial_vec[i].size);\n                break;\n            case HBW_MEMALIGN_PSIZE:\n                fprintf (stdout,\"Allocating %zd bytes using hbw_memalign_psize\\n\",\n                         trial_vec[i].size);\n                hbw_pagesize_t psize;\n                if (trial_vec[i].page_size == 4096)\n                    psize = HBW_PAGESIZE_4KB;\n                else if (trial_vec[i].page_size == 2097152)\n                    psize = HBW_PAGESIZE_2MB;\n                else if (trial_vec[i].size %\n                         trial_vec[i].page_size > 0)\n                    psize = HBW_PAGESIZE_1GB;\n                else\n                    psize = HBW_PAGESIZE_1GB_STRICT;\n\n                ret = hbw_posix_memalign_psize(&ptr_vec[i],\n                                               trial_vec[i].alignment,\n                                               trial_vec[i].size,\n                                               psize);\n\n                break;\n            case MEMKIND_MALLOC:\n                fprintf (stdout,\"Allocating %zd bytes using memkind_malloc\\n\",\n                         trial_vec[i].size);\n                ptr_vec[i] = memkind_malloc(trial_vec[i].memkind,\n                                            trial_vec[i].size);\n                break;\n            case MEMKIND_CALLOC:\n                fprintf (stdout,\"Allocating %zd bytes using memkind_calloc\\n\",\n                         trial_vec[i].size);\n                ptr_vec[i] = memkind_calloc(trial_vec[i].memkind,\n                                            trial_vec[i].size, 1);\n                break;\n            case MEMKIND_REALLOC:\n                fprintf (stdout,\"Allocating %zd bytes using memkind_realloc\\n\",\n                         trial_vec[i].size);\n                if (NULL == ptr_vec[i]) {\n                    ptr_vec[i] = memkind_realloc(trial_vec[i].memkind,\n                                                 ptr_vec[i],\n                                                 trial_vec[i].size);\n                }\n                break;\n            case MEMKIND_POSIX_MEMALIGN:\n                fprintf (stdout,\n                         \"Allocating %zd bytes using memkind_posix_memalign\\n\",\n                         trial_vec[i].size);\n\n                ret = memkind_posix_memalign(trial_vec[i].memkind,\n                                             &ptr_vec[i],\n                                             trial_vec[i].alignment,\n                                             trial_vec[i].size);\n                break;\n        }\n        if (trial_vec[i].api != HBW_FREE &&\n            trial_vec[i].api != MEMKIND_FREE &&\n            trial_vec[i].memkind != MEMKIND_DEFAULT) {\n            ASSERT_TRUE(ptr_vec[i] != NULL);\n            memset(ptr_vec[i], 0, trial_vec[i].size);\n            Check check(ptr_vec[i], trial_vec[i]);\n            if (trial_vec[i].api == HBW_CALLOC) {\n                EXPECT_EQ(0, check.check_zero());\n            }\n            if (trial_vec[i].api == HBW_MEMALIGN ||\n                trial_vec[i].api == HBW_MEMALIGN_PSIZE ||\n                trial_vec[i].api == MEMKIND_POSIX_MEMALIGN) {\n                EXPECT_EQ(0, check.check_align(trial_vec[i].alignment));\n                EXPECT_EQ(0, ret);\n            }\n            if (trial_vec[i].api == HBW_MEMALIGN_PSIZE ||\n                (trial_vec[i].api == MEMKIND_MALLOC &&\n                 (trial_vec[i].memkind == MEMKIND_HBW_HUGETLB ||\n                  trial_vec[i].memkind == MEMKIND_HBW_PREFERRED_HUGETLB))) {\n                EXPECT_EQ(0, check.check_page_size(trial_vec[i].page_size));\n            }\n        }\n    }\n    for (i = 0; i < num_trial; ++i) {\n        if (ptr_vec[i]) {\n            hbw_free(ptr_vec[i]);\n        }\n    }\n}\n\nvoid TGTest :: SetUp()\n{\n    size_t node;\n    char *hbw_nodes_env, *endptr;\n    tgen = std::move(std::unique_ptr<TrialGenerator>(new TrialGenerator()));\n\n    hbw_nodes_env = getenv(\"MEMKIND_HBW_NODES\");\n    if (hbw_nodes_env) {\n        num_bandwidth = 128;\n        for (node = 0; node < num_bandwidth; node++) {\n            bandwidth.push_back(1);\n        }\n        node = strtol(hbw_nodes_env, &endptr, 10);\n        bandwidth.push_back(2);\n        while (*endptr == ':') {\n            hbw_nodes_env = endptr + 1;\n            node = strtol(hbw_nodes_env, &endptr, 10);\n            if (endptr != hbw_nodes_env && node >= 0 && node < num_bandwidth) {\n                bandwidth.push_back(2);\n            }\n        }\n    } else {\n        num_bandwidth = NUMA_NUM_NODES;\n        nodemask_t nodemask;\n        struct bitmask nodemask_bm = {NUMA_NUM_NODES, nodemask.n};\n        numa_bitmask_clearall(&nodemask_bm);\n\n        memkind_hbw_all_get_mbind_nodemask(NULL, nodemask.n, NUMA_NUM_NODES);\n\n        int i, nodes_num = numa_num_configured_nodes();\n        for (i=0; i<NUMA_NUM_NODES; i++) {\n            if (i >= nodes_num) {\n                bandwidth.push_back(0);\n            } else if (numa_bitmask_isbitset(&nodemask_bm, i)) {\n                bandwidth.push_back(2);\n            } else {\n                bandwidth.push_back(1);\n            }\n        }\n    }\n}\n\nvoid TGTest :: TearDown()\n{}\n"
  },
  {
    "path": "deps/memkind/src/test/trial_generator.h",
    "content": "/*\n * Copyright (C) 2014 - 2018 Intel Corporation.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n * 1. Redistributions of source code must retain the above copyright notice(s),\n *    this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright notice(s),\n *    this list of conditions and the following disclaimer in the documentation\n *    and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\n * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\n * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\n * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef execute_trials_include_h\n#define execute_trials_include_h\n\n#include \"hbwmalloc.h\"\n#include \"memkind.h\"\n\n#include <vector>\n#include <stdlib.h>\n#include <vector>\n#include <memory>\n#include <algorithm>\n#include <fstream>\n#include <iostream>\n\n#include \"common.h\"\n\n\ntypedef enum {\n    HBW_MALLOC,\n    HBW_CALLOC,\n    HBW_REALLOC,\n    HBW_MEMALIGN,\n    HBW_MEMALIGN_PSIZE,\n    HBW_FREE,\n    MEMKIND_MALLOC,\n    MEMKIND_CALLOC,\n    MEMKIND_REALLOC,\n    MEMKIND_POSIX_MEMALIGN,\n    MEMKIND_FREE\n} alloc_api_t;\n\ntypedef struct {\n    alloc_api_t api;\n    size_t size;\n    size_t alignment;\n    size_t page_size;\n    memkind_t memkind;\n    int free_index;\n} trial_t;\n\nclass TrialGenerator\n{\npublic:\n    TrialGenerator() {}\n    void generate_gb(alloc_api_t api, int number_of_gb_pages, memkind_t memkind,\n                     alloc_api_t api_free, bool psize_strict=false, size_t align = GB);\n    void run(int num_bandwidth, std::vector<int> &bandwidths);\n    void generate_size_2bytes_2KB_2MB(alloc_api_t api);\n    /*For debugging purposes*/\n    void print();\nprivate:\n    std::vector<trial_t> trial_vec;\n    trial_t create_trial_tuple(alloc_api_t api,\n                               size_t size,\n                               size_t alignment,\n                               int page_size,\n                               memkind_t memkind,\n                               int free_index);\n\n\n};\n\nclass TGTest : public::testing::Test\n{\nprotected:\n    size_t num_bandwidth;\n    std::vector<int> bandwidth;\n    std::unique_ptr<TrialGenerator> tgen;\n    void SetUp();\n    void TearDown();\n};\n\n#endif\n"
  },
  {
    "path": "deps/update-jemalloc.sh",
    "content": "#!/bin/bash\nVER=$1\nURL=\"http://www.canonware.com/download/jemalloc/jemalloc-${VER}.tar.bz2\"\necho \"Downloading $URL\"\ncurl $URL > /tmp/jemalloc.tar.bz2\ntar xvjf /tmp/jemalloc.tar.bz2\nrm -rf jemalloc\nmv jemalloc-${VER} jemalloc\necho \"Use git status, add all files and commit changes.\"\n"
  },
  {
    "path": "fuzz/rdb/dict.txt",
    "content": "=\"repl-stream-db\"\n=\"repl-id\"\n=\"repl-offset\"\n=\"lua\"\n=\"redis-ver\"\n=\"ctime\"\n=\"used-mem\"\n=\"aof-preamble\"\n=\"redis-bits\"\n=\"mvcc-tstamp\"\n=\"keydb-subexpire-key\"\n=\"keydb-subexpire-when\"\n\n"
  },
  {
    "path": "keydb.conf",
    "content": "# KeyDB configuration file example.\n#\n# Note that in order to read the configuration file, KeyDB must be\n# started with the file path as first argument:\n#\n# ./keydb-server /path/to/keydb.conf\n\n# Note on units: when memory size is needed, it is possible to specify\n# it in the usual form of 1k 5GB 4M and so forth:\n#\n# 1k => 1000 bytes\n# 1kb => 1024 bytes\n# 1m => 1000000 bytes\n# 1mb => 1024*1024 bytes\n# 1g => 1000000000 bytes\n# 1gb => 1024*1024*1024 bytes\n#\n# units are case insensitive so 1GB 1Gb 1gB are all the same.\n\n################################## INCLUDES ###################################\n\n# Include one or more other config files here.  This is useful if you\n# have a standard template that goes to all KeyDB servers but also need\n# to customize a few per-server settings.  Include files can include\n# other files, so use this wisely.\n#\n# Note that option \"include\" won't be rewritten by command \"CONFIG REWRITE\"\n# from admin or KeyDB Sentinel. Since KeyDB always uses the last processed\n# line as value of a configuration directive, you'd better put includes\n# at the beginning of this file to avoid overwriting config change at runtime.\n#\n# If instead you are interested in using includes to override configuration\n# options, it is better to use include as the last line.\n#\n# Included paths may contain wildcards. All files matching the wildcards will\n# be included in alphabetical order.\n# Note that if an include path contains a wildcards but no files match it when\n# the server is started, the include statement will be ignored and no error will\n# be emitted.  It is safe, therefore, to include wildcard files from empty\n# directories.\n#\n# include /path/to/local.conf\n# include /path/to/other.conf\n# include /path/to/fragments/*.conf\n#\n\n################################## MODULES #####################################\n\n# Load modules at startup. If the server is not able to load modules\n# it will abort. It is possible to use multiple loadmodule directives.\n#\n# loadmodule /path/to/my_module.so\n# loadmodule /path/to/other_module.so\n\n################################## NETWORK #####################################\n\n# By default, if no \"bind\" configuration directive is specified, KeyDB listens\n# for connections from all available network interfaces on the host machine.\n# It is possible to listen to just one or multiple selected interfaces using\n# the \"bind\" configuration directive, followed by one or more IP addresses.\n# Each address can be prefixed by \"-\", which means that redis will not fail to\n# start if the address is not available. Being not available only refers to\n# addresses that does not correspond to any network interfece. Addresses that\n# are already in use will always fail, and unsupported protocols will always BE\n# silently skipped.\n#\n# Examples:\n#\n# bind 192.168.1.100 10.0.0.1     # listens on two specific IPv4 addresses\n# bind 127.0.0.1 ::1              # listens on loopback IPv4 and IPv6\n# bind * -::*                     # like the default, all available interfaces\n#\n# ~~~ WARNING ~~~ If the computer running KeyDB is directly exposed to the\n# internet, binding to all the interfaces is dangerous and will expose the\n# instance to everybody on the internet. So by default we uncomment the\n# following bind directive, that will force KeyDB to listen only on the\n# IPv4 and IPv6 (if available) loopback interface addresses (this means KeyDB will only be able to\n# accept client connections from the same host that it is running on).\n#\n# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES\n# JUST COMMENT OUT THE FOLLOWING LINE.\n#\n# You will also need to set a password unless you explicitly disable protected\n# mode.\n# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nbind 127.0.0.1 -::1\n\n# Protected mode is a layer of security protection, in order to avoid that\n# KeyDB instances left open on the internet are accessed and exploited.\n#\n# When protected mode is on and if:\n#\n# 1) The server is not binding explicitly to a set of addresses using the\n#    \"bind\" directive.\n# 2) No password is configured.\n#\n# The server only accepts connections from clients connecting from the\n# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain\n# sockets.\n#\n# By default protected mode is enabled. You should disable it only if\n# you are sure you want clients from other hosts to connect to KeyDB\n# even if no authentication is configured, nor a specific set of interfaces\n# are explicitly listed using the \"bind\" directive.\nprotected-mode yes\n\n# Accept connections on the specified port, default is 6379 (IANA #815344).\n# If port 0 is specified KeyDB will not listen on a TCP socket.\nport 6379\n\n# TCP listen() backlog.\n#\n# In high requests-per-second environments you need a high backlog in order\n# to avoid slow clients connection issues. Note that the Linux kernel\n# will silently truncate it to the value of /proc/sys/net/core/somaxconn so\n# make sure to raise both the value of somaxconn and tcp_max_syn_backlog\n# in order to get the desired effect.\ntcp-backlog 511\n\n# Unix socket.\n#\n# Specify the path for the Unix socket that will be used to listen for\n# incoming connections. There is no default, so KeyDB will not listen\n# on a unix socket when not specified.\n#\n# unixsocket /tmp/keydb.sock\n# unixsocketperm 700\n\n# Close the connection after a client is idle for N seconds (0 to disable)\ntimeout 0\n\n# TCP keepalive.\n#\n# If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence\n# of communication. This is useful for two reasons:\n#\n# 1) Detect dead peers.\n# 2) Force network equipment in the middle to consider the connection to be\n#    alive.\n#\n# On Linux, the specified value (in seconds) is the period used to send ACKs.\n# Note that to close the connection the double of the time is needed.\n# On other kernels the period depends on the kernel configuration.\n#\n# A reasonable value for this option is 300 seconds, which is the new\n# KeyDB default starting with KeyDB 3.2.1.\ntcp-keepalive 300\n\n################################# TLS/SSL #####################################\n\n# By default, TLS/SSL is disabled. To enable it, the \"tls-port\" configuration\n# directive can be used to define TLS-listening ports. To enable TLS on the\n# default port, use:\n#\n# port 0\n# tls-port 6379\n\n# Configure a X.509 certificate and private key to use for authenticating the\n# server to connected clients, masters or cluster peers.  These files should be\n# PEM formatted.\n#\n# tls-cert-file keydb.crt \n# tls-key-file keydb.key\n#\n# If the key file is encrypted using a passphrase, it can be included here\n# as well.\n#\n# tls-key-file-pass secret\n\n# Normally KeyDB uses the same certificate for both server functions (accepting\n# connections) and client functions (replicating from a master, establishing\n# cluster bus connections, etc.).\n#\n# Sometimes certificates are issued with attributes that designate them as\n# client-only or server-only certificates. In that case it may be desired to use\n# different certificates for incoming (server) and outgoing (client)\n# connections. To do that, use the following directives:\n#\n# tls-client-cert-file client.crt\n# tls-client-key-file client.key\n#\n# If the key file is encrypted using a passphrase, it can be included here\n# as well.\n#\n# tls-client-key-file-pass secret\n\n# Configure a DH parameters file to enable Diffie-Hellman (DH) key exchange,\n# required by older versions of OpenSSL (<3.0). Newer versions do not require\n# this configuration and recommend against it.\n#\n# tls-dh-params-file keydb.dh\n\n# Configure a CA certificate(s) bundle or directory to authenticate TLS/SSL\n# clients and peers. KeyDB requires an explicit configuration of at least one\n# of these, and will not implicitly use the system wide configuration.\n#\n# tls-ca-cert-file ca.crt\n# tls-ca-cert-dir /etc/ssl/certs\n\n# By default, clients (including replica servers) on a TLS port are required\n# to authenticate using valid client side certificates.\n#\n# If \"no\" is specified, client certificates are not required and not accepted.\n# If \"optional\" is specified, client certificates are accepted and must be\n# valid if provided, but are not required.\n#\n# tls-auth-clients no\n# tls-auth-clients optional\n\n# By default, a KeyDB replica does not attempt to establish a TLS connection\n# with its master.\n#\n# Use the following directive to enable TLS on replication links.\n#\n# tls-replication yes\n\n# By default, the KeyDB Cluster bus uses a plain TCP connection. To enable\n# TLS for the bus protocol, use the following directive:\n#\n# tls-cluster yes\n\n# Explicitly specify TLS versions to support. Allowed values are case insensitive\n# and include \"TLSv1\", \"TLSv1.1\", \"TLSv1.2\", \"TLSv1.3\" (OpenSSL >= 1.1.1) or\n# any combination. To enable only TLSv1.2 and TLSv1.3, use:\n#\n# tls-protocols \"TLSv1.2 TLSv1.3\"\n\n# Configure allowed ciphers.  See the ciphers(1ssl) manpage for more information\n# about the syntax of this string.\n#\n# Note: this configuration applies only to <= TLSv1.2.\n#\n# tls-ciphers DEFAULT:!MEDIUM\n\n# Configure allowed TLSv1.3 ciphersuites.  See the ciphers(1ssl) manpage for more\n# information about the syntax of this string, and specifically for TLSv1.3\n# ciphersuites.\n#\n# tls-ciphersuites TLS_CHACHA20_POLY1305_SHA256\n\n# When choosing a cipher, use the server's preference instead of the client\n# preference. By default, the server follows the client's preference.\n#\n# tls-prefer-server-ciphers yes\n\n# By default, TLS session caching is enabled to allow faster and less expensive\n# reconnections by clients that support it. Use the following directive to disable\n# caching.\n#\n# tls-session-caching no\n\n# Change the default number of TLS sessions cached. A zero value sets the cache\n# to unlimited size. The default size is 20480.\n#\n# tls-session-cache-size 5000\n\n# Change the default timeout of cached TLS sessions. The default timeout is 300\n# seconds.\n#\n# tls-session-cache-timeout 60\n\n# Allow the server to monitor the filesystem and rotate out TLS certificates if \n# they change on disk, defaults to no.\n# \n# tls-rotation no\n\n# Setup a allowlist of allowed Common Names (CNs)/Subject Alternative Names (SANs)\n# that are allowed to connect to this server. This includes both normal clients as\n# well as other servers connected for replication/clustering purposes. If nothing is\n# specified, then no allowlist is used and all certificates are accepted. \n# Supports IPv4, DNS, RFC822, and URI SAN types.\n# You can put multiple names on one line as follows:\n#\n# tls-allowlist <dns1> <dns2> <dns3> ...\n# \n#\n# This configuration also allows for wildcard characters with glob style formatting\n# i.e. \"*.com\" would allow all clients to connect with a CN/SAN that ends with \".com\"\n\n################################# GENERAL #####################################\n\n# By default KeyDB does not run as a daemon. Use 'yes' if you need it.\n# Note that KeyDB will write a pid file in /var/run/keydb.pid when daemonized.\ndaemonize no\n\n# If you run KeyDB from upstart or systemd, KeyDB can interact with your\n# supervision tree. Options:\n#   supervised no      - no supervision interaction\n#   supervised upstart - signal upstart by putting KeyDB into SIGSTOP mode\n#                        requires \"expect stop\" in your upstart job config\n#   supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET\n#   supervised auto    - detect upstart or systemd method based on\n#                        UPSTART_JOB or NOTIFY_SOCKET environment variables\n# Note: these supervision methods only signal \"process is ready.\"\n#       They do not enable continuous pings back to your supervisor.\nsupervised no\n\n# If a pid file is specified, KeyDB writes it where specified at startup\n# and removes it at exit.\n#\n# When the server runs non daemonized, no pid file is created if none is\n# specified in the configuration. When the server is daemonized, the pid file\n# is used even if not specified, defaulting to \"/var/run/keydb.pid\".\n#\n# Creating a pid file is best effort: if KeyDB is not able to create it\n# nothing bad happens, the server will start and run normally.\npidfile /var/run/keydb_6379.pid\n\n# Specify the server verbosity level.\n# This can be one of:\n# debug (a lot of information, useful for development/testing)\n# verbose (many rarely useful info, but not a mess like the debug level)\n# notice (moderately verbose, what you want in production probably)\n# warning (only very important / critical messages are logged)\nloglevel notice\n\n# Specify the log file name. Also the empty string can be used to force\n# KeyDB to log on the standard output. Note that if you use standard\n# output for logging but daemonize, logs will be sent to /dev/null\nlogfile \"\"\n\n# To enable logging to the system logger, just set 'syslog-enabled' to yes,\n# and optionally update the other syslog parameters to suit your needs.\n# syslog-enabled no\n\n# Specify the syslog identity.\n# syslog-ident keydb\n\n# Specify the syslog facility. Must be USER or between LOCAL0-LOCAL7.\n# syslog-facility local0\n\n# To disable the built in crash log, which will possibly produce cleaner core\n# dumps when they are needed, uncomment the following:\n#\n# crash-log-enabled no\n\n# To disable the fast memory check that's run as part of the crash log, which\n# will possibly let keydb terminate sooner, uncomment the following:\n#\n# crash-memcheck-enabled no\n\n# Set the number of databases. The default database is DB 0, you can select\n# a different one on a per-connection basis using SELECT <dbid> where\n# dbid is a number between 0 and 'databases'-1\ndatabases 16\n\n# By default KeyDB shows an ASCII art logo only when started to log to the\n# standard output and if the standard output is a TTY. Basically this means\n# that normally a logo is displayed only in interactive sessions.\n#\n# However it is possible to force the pre-4.0 behavior and always show a\n# ASCII art logo in startup logs by setting the following option to yes.\nalways-show-logo yes\n\n# By default, KeyDB modifies the process title (as seen in 'top' and 'ps') to\n# provide some runtime information. It is possible to disable this and leave\n# the process name as executed by setting the following to no.\nset-proc-title yes\n\n# Retrieving \"message of today\" using CURL requests.\n#enable-motd yes\n\n# When changing the process title, KeyDB uses the following template to construct\n# the modified title.\n#\n# Template variables are specified in curly brackets. The following variables are\n# supported:\n#\n# {title}           Name of process as executed if parent, or type of child process.\n# {listen-addr}     Bind address or '*' followed by TCP or TLS port listening on, or\n#                   Unix socket if only that's available.\n# {server-mode}     Special mode, i.e. \"[sentinel]\" or \"[cluster]\".\n# {port}            TCP port listening on, or 0.\n# {tls-port}        TLS port listening on, or 0.\n# {unixsocket}      Unix domain socket listening on, or \"\".\n# {config-file}     Name of configuration file used.\n#\nproc-title-template \"{title} {listen-addr} {server-mode}\"\n\n################################ SNAPSHOTTING  ################################\n#\n# Save the DB on disk:\n#\n#   save <seconds> <changes>\n#\n#   Will save the DB if both the given number of seconds and the given\n#   number of write operations against the DB occurred.\n#\n#   In the example below the behavior will be to save:\n#   after 900 sec (15 min) if at least 1 key changed\n#   after 300 sec (5 min) if at least 10 keys changed\n#   after 60 sec if at least 10000 keys changed\n#\n#   It is also possible to remove all the previously configured save\n#   points by adding a save directive with a single empty string argument\n#   like in the following example:\n#\n#   save \"\"\n\nsave 900 1\nsave 300 10\nsave 60 10000\n\n# By default KeyDB will stop accepting writes if RDB snapshots are enabled\n# (at least one save point) and the latest background save failed.\n# This will make the user aware (in a hard way) that data is not persisting\n# on disk properly, otherwise chances are that no one will notice and some\n# disaster will happen.\n#\n# If the background saving process will start working again KeyDB will\n# automatically allow writes again.\n#\n# However if you have setup your proper monitoring of the KeyDB server\n# and persistence, you may want to disable this feature so that KeyDB will\n# continue to work as usual even if there are problems with disk,\n# permissions, and so forth.\nstop-writes-on-bgsave-error yes\n\n# Compress string objects using LZF when dump .rdb databases?\n# By default compression is enabled as it's almost always a win.\n# If you want to save some CPU in the saving child set it to 'no' but\n# the dataset will likely be bigger if you have compressible values or keys.\nrdbcompression yes\n\n# Since version 5 of RDB a CRC64 checksum is placed at the end of the file.\n# This makes the format more resistant to corruption but there is a performance\n# hit to pay (around 10%) when saving and loading RDB files, so you can disable it\n# for maximum performances.\n#\n# RDB files created with checksum disabled have a checksum of zero that will\n# tell the loading code to skip the check.\nrdbchecksum yes\n\n# Enables or disables full sanitation checks for ziplist and listpack etc when\n# loading an RDB or RESTORE payload. This reduces the chances of a assertion or\n# crash later on while processing commands.\n# Options:\n#   no         - Never perform full sanitation\n#   yes        - Always perform full sanitation\n#   clients    - Perform full sanitation only for user connections.\n#                Excludes: RDB files, RESTORE commands received from the master\n#                connection, and client connections which have the\n#                skip-sanitize-payload ACL flag.\n# The default should be 'clients' but since it currently affects cluster\n# resharding via MIGRATE, it is temporarily set to 'no' by default.\n#\n# sanitize-dump-payload no\n\n# The filename where to dump the DB\ndbfilename dump.rdb\n\n# Remove RDB files used by replication in instances without persistence\n# enabled. By default this option is disabled, however there are environments\n# where for regulations or other security concerns, RDB files persisted on\n# disk by masters in order to feed replicas, or stored on disk by replicas\n# in order to load them for the initial synchronization, should be deleted\n# ASAP. Note that this option ONLY WORKS in instances that have both AOF\n# and RDB persistence disabled, otherwise is completely ignored.\n#\n# An alternative (and sometimes better) way to obtain the same effect is\n# to use diskless replication on both master and replicas instances. However\n# in the case of replicas, diskless is not always an option.\nrdb-del-sync-files no\n\n# The working directory.\n#\n# The DB will be written inside this directory, with the filename specified\n# above using the 'dbfilename' configuration directive.\n#\n# The Append Only File will also be created inside this directory.\n#\n# Note that you must specify a directory here, not a file name.\ndir ./\n\n################################# REPLICATION #################################\n\n# Master-Replica replication. Use replicaof to make a KeyDB instance a copy of\n# another KeyDB server. A few things to understand ASAP about KeyDB replication.\n#\n#   +------------------+      +---------------+\n#   |      Master      | ---> |    Replica    |\n#   | (receive writes) |      |  (exact copy) |\n#   +------------------+      +---------------+\n#\n# 1) KeyDB replication is asynchronous, but you can configure a master to\n#    stop accepting writes if it appears to be not connected with at least\n#    a given number of replicas.\n# 2) KeyDB replicas are able to perform a partial resynchronization with the\n#    master if the replication link is lost for a relatively small amount of\n#    time. You may want to configure the replication backlog size (see the next\n#    sections of this file) with a sensible value depending on your needs.\n# 3) Replication is automatic and does not need user intervention. After a\n#    network partition replicas automatically try to reconnect to masters\n#    and resynchronize with them.\n#\n# replicaof <masterip> <masterport>\n\n# If the master is password protected (using the \"requirepass\" configuration\n# directive below) it is possible to tell the replica to authenticate before\n# starting the replication synchronization process, otherwise the master will\n# refuse the replica request.\n#\n# masterauth <master-password>\n#\n# However this is not enough if you are using KeyDB ACLs (for KeyDB version\n# 6 or greater), and the default user is not capable of running the PSYNC\n# command and/or other commands needed for replication (gathered in the\n# @replication group). In this case it's better to configure a special user to\n# use with replication, and specify the masteruser configuration as such:\n#\n# masteruser <username>\n#\n# When masteruser is specified, the replica will authenticate against its\n# master using the new AUTH form: AUTH <username> <password>.\n\n# When a replica loses its connection with the master, or when the replication\n# is still in progress, the replica can act in two different ways:\n#\n# 1) if replica-serve-stale-data is set to 'yes' (the default) the replica will\n#    still reply to client requests, possibly with out of date data, or the\n#    data set may just be empty if this is the first synchronization.\n#\n# 2) If replica-serve-stale-data is set to 'no' the replica will reply with\n#    an error \"SYNC with master in progress\" to all commands except:\n#    INFO, REPLICAOF, AUTH, PING, SHUTDOWN, REPLCONF, ROLE, CONFIG, SUBSCRIBE,\n#    UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, PUBLISH, PUBSUB, COMMAND, POST,\n#    HOST and LATENCY.\n#\nreplica-serve-stale-data yes\n\n# Active Replicas will allow read only data access while loading remote RDBs\n# provided they are permitted to serve stale data.  As an option you may also\n# permit them to accept write commands.  This is an EXPERIMENTAL feature and\n# may result in commands not being fully synchronized\n#\n# allow-write-during-load no\n\n# You can modify the number of masters necessary to form a replica quorum when\n# multi-master is enabled and replica-serve-stale-data is \"no\".  By default \n# this is set to -1 which implies the number of known masters (e.g. those\n# you added with replicaof)\n#\n# replica-quorum -1\n\n# You can configure a replica instance to accept writes or not. Writing against\n# a replica instance may be useful to store some ephemeral data (because data\n# written on a replica will be easily deleted after resync with the master) but\n# may also cause problems if clients are writing to it because of a\n# misconfiguration.\n#\n# Since KeyDB 2.6 by default replicas are read-only.\n#\n# Note: read only replicas are not designed to be exposed to untrusted clients\n# on the internet. It's just a protection layer against misuse of the instance.\n# Still a read only replica exports by default all the administrative commands\n# such as CONFIG, DEBUG, and so forth. To a limited extent you can improve\n# security of read only replicas using 'rename-command' to shadow all the\n# administrative / dangerous commands.\nreplica-read-only yes\n\n# Replication SYNC strategy: disk or socket.\n#\n# New replicas and reconnecting replicas that are not able to continue the\n# replication process just receiving differences, need to do what is called a\n# \"full synchronization\". An RDB file is transmitted from the master to the\n# replicas.\n#\n# The transmission can happen in two different ways:\n#\n# 1) Disk-backed: The KeyDB master creates a new process that writes the RDB\n#                 file on disk. Later the file is transferred by the parent\n#                 process to the replicas incrementally.\n# 2) Diskless: The KeyDB master creates a new process that directly writes the\n#              RDB file to replica sockets, without touching the disk at all.\n#\n# With disk-backed replication, while the RDB file is generated, more replicas\n# can be queued and served with the RDB file as soon as the current child\n# producing the RDB file finishes its work. With diskless replication instead\n# once the transfer starts, new replicas arriving will be queued and a new\n# transfer will start when the current one terminates.\n#\n# When diskless replication is used, the master waits a configurable amount of\n# time (in seconds) before starting the transfer in the hope that multiple\n# replicas will arrive and the transfer can be parallelized.\n#\n# With slow disks and fast (large bandwidth) networks, diskless replication\n# works better.\nrepl-diskless-sync no\n\n# When diskless replication is enabled, it is possible to configure the delay\n# the server waits in order to spawn the child that transfers the RDB via socket\n# to the replicas.\n#\n# This is important since once the transfer starts, it is not possible to serve\n# new replicas arriving, that will be queued for the next RDB transfer, so the\n# server waits a delay in order to let more replicas arrive.\n#\n# The delay is specified in seconds, and by default is 5 seconds. To disable\n# it entirely just set it to 0 seconds and the transfer will start ASAP.\nrepl-diskless-sync-delay 5\n\n# -----------------------------------------------------------------------------\n# WARNING: RDB diskless load is experimental. Since in this setup the replica\n# does not immediately store an RDB on disk, it may cause data loss during\n# failovers. RDB diskless load + KeyDB modules not handling I/O reads may also\n# cause KeyDB to abort in case of I/O errors during the initial synchronization\n# stage with the master. Use only if your do what you are doing.\n# -----------------------------------------------------------------------------\n#\n# Replica can load the RDB it reads from the replication link directly from the\n# socket, or store the RDB to a file and read that file after it was completely\n# received from the master.\n#\n# In many cases the disk is slower than the network, and storing and loading\n# the RDB file may increase replication time (and even increase the master's\n# Copy on Write memory and salve buffers).\n# However, parsing the RDB file directly from the socket may mean that we have\n# to flush the contents of the current database before the full rdb was\n# received. For this reason we have the following options:\n#\n# \"disabled\"    - Don't use diskless load (store the rdb file to the disk first)\n# \"on-empty-db\" - Use diskless load only when it is completely safe.\n# \"swapdb\"      - Keep a copy of the current db contents in RAM while parsing\n#                 the data directly from the socket. note that this requires\n#                 sufficient memory, if you don't have it, you risk an OOM kill.\nrepl-diskless-load disabled\n\n# Replicas send PINGs to server in a predefined interval. It's possible to\n# change this interval with the repl_ping_replica_period option. The default\n# value is 10 seconds.\n#\n# repl-ping-replica-period 10\n\n# The following option sets the replication timeout for:\n#\n# 1) Bulk transfer I/O during SYNC, from the point of view of replica.\n# 2) Master timeout from the point of view of replicas (data, pings).\n# 3) Replica timeout from the point of view of masters (REPLCONF ACK pings).\n#\n# It is important to make sure that this value is greater than the value\n# specified for repl-ping-replica-period otherwise a timeout will be detected\n# every time there is low traffic between the master and the replica. The default\n# value is 60 seconds.\n#\n# repl-timeout 60\n\n# Disable TCP_NODELAY on the replica socket after SYNC?\n#\n# If you select \"yes\" KeyDB will use a smaller number of TCP packets and\n# less bandwidth to send data to replicas. But this can add a delay for\n# the data to appear on the replica side, up to 40 milliseconds with\n# Linux kernels using a default configuration.\n#\n# If you select \"no\" the delay for data to appear on the replica side will\n# be reduced but more bandwidth will be used for replication.\n#\n# By default we optimize for low latency, but in very high traffic conditions\n# or when the master and replicas are many hops away, turning this to \"yes\" may\n# be a good idea.\nrepl-disable-tcp-nodelay no\n\n# Set the replication backlog size. The backlog is a buffer that accumulates\n# replica data when replicas are disconnected for some time, so that when a\n# replica wants to reconnect again, often a full resync is not needed, but a\n# partial resync is enough, just passing the portion of data the replica\n# missed while disconnected.\n#\n# The bigger the replication backlog, the longer the replica can endure the\n# disconnect and later be able to perform a partial resynchronization.\n#\n# The backlog is only allocated if there is at least one replica connected.\n#\n# repl-backlog-size 1mb\n\n# After a master has no connected replicas for some time, the backlog will be\n# freed. The following option configures the amount of seconds that need to\n# elapse, starting from the time the last replica disconnected, for the backlog\n# buffer to be freed.\n#\n# Note that replicas never free the backlog for timeout, since they may be\n# promoted to masters later, and should be able to correctly \"partially\n# resynchronize\" with other replicas: hence they should always accumulate backlog.\n#\n# A value of 0 means to never release the backlog.\n#\n# repl-backlog-ttl 3600\n\n# The replica priority is an integer number published by KeyDB in the INFO\n# output. It is used by KeyDB Sentinel in order to select a replica to promote\n# into a master if the master is no longer working correctly.\n#\n# A replica with a low priority number is considered better for promotion, so\n# for instance if there are three replicas with priority 10, 100, 25 Sentinel\n# will pick the one with priority 10, that is the lowest.\n#\n# However a special priority of 0 marks the replica as not able to perform the\n# role of master, so a replica with priority of 0 will never be selected by\n# KeyDB Sentinel for promotion.\n#\n# By default the priority is 100.\nreplica-priority 100\n\n# -----------------------------------------------------------------------------\n# By default, KeyDB Sentinel includes all replicas in its reports. A replica\n# can be excluded from KeyDB Sentinel's announcements. An unannounced replica\n# will be ignored by the 'sentinel replicas <master>' command and won't be\n# exposed to KeyDB Sentinel's clients.\n#\n# This option does not change the behavior of replica-priority. Even with\n# replica-announced set to 'no', the replica can be promoted to master. To\n# prevent this behavior, set replica-priority to 0.\n#\n# replica-announced yes\n\n# It is possible for a master to stop accepting writes if there are less than\n# N replicas connected, having a lag less or equal than M seconds.\n#\n# The N replicas need to be in \"online\" state.\n#\n# The lag in seconds, that must be <= the specified value, is calculated from\n# the last ping received from the replica, that is usually sent every second.\n#\n# This option does not GUARANTEE that N replicas will accept the write, but\n# will limit the window of exposure for lost writes in case not enough replicas\n# are available, to the specified number of seconds.\n#\n# For example to require at least 3 replicas with a lag <= 10 seconds use:\n#\n# min-replicas-to-write 3\n# min-replicas-max-lag 10\n#\n# Setting one or the other to 0 disables the feature.\n#\n# By default min-replicas-to-write is set to 0 (feature disabled) and\n# min-replicas-max-lag is set to 10.\n\n# A KeyDB master is able to list the address and port of the attached\n# replicas in different ways. For example the \"INFO replication\" section\n# offers this information, which is used, among other tools, by\n# KeyDB Sentinel in order to discover replica instances.\n# Another place where this info is available is in the output of the\n# \"ROLE\" command of a master.\n#\n# The listed IP address and port normally reported by a replica is\n# obtained in the following way:\n#\n#   IP: The address is auto detected by checking the peer address\n#   of the socket used by the replica to connect with the master.\n#\n#   Port: The port is communicated by the replica during the replication\n#   handshake, and is normally the port that the replica is using to\n#   listen for connections.\n#\n# However when port forwarding or Network Address Translation (NAT) is\n# used, the replica may actually be reachable via different IP and port\n# pairs. The following two options can be used by a replica in order to\n# report to its master a specific set of IP and port, so that both INFO\n# and ROLE will report those values.\n#\n# There is no need to use both the options if you need to override just\n# the port or the IP address.\n#\n# replica-announce-ip 5.5.5.5\n# replica-announce-port 1234\n\n############################### KEYS TRACKING #################################\n\n# KeyDB implements server assisted support for client side caching of values.\n# This is implemented using an invalidation table that remembers, using\n# 16 millions of slots, what clients may have certain subsets of keys. In turn\n# this is used in order to send invalidation messages to clients. Please\n# check this page to understand more about the feature:\n#\n#   https://redis.io/topics/client-side-caching\n#\n# When tracking is enabled for a client, all the read only queries are assumed\n# to be cached: this will force KeyDB to store information in the invalidation\n# table. When keys are modified, such information is flushed away, and\n# invalidation messages are sent to the clients. However if the workload is\n# heavily dominated by reads, KeyDB could use more and more memory in order\n# to track the keys fetched by many clients.\n#\n# For this reason it is possible to configure a maximum fill value for the\n# invalidation table. By default it is set to 1M of keys, and once this limit\n# is reached, KeyDB will start to evict keys in the invalidation table\n# even if they were not modified, just to reclaim memory: this will in turn\n# force the clients to invalidate the cached values. Basically the table\n# maximum size is a trade off between the memory you want to spend server\n# side to track information about who cached what, and the ability of clients\n# to retain cached objects in memory.\n#\n# If you set the value to 0, it means there are no limits, and KeyDB will\n# retain as many keys as needed in the invalidation table.\n# In the \"stats\" INFO section, you can find information about the number of\n# keys in the invalidation table at every given moment.\n#\n# Note: when key tracking is used in broadcasting mode, no memory is used\n# in the server side so this setting is useless.\n#\n# tracking-table-max-keys 1000000\n\n################################## SECURITY ###################################\n\n# Warning: since KeyDB is pretty fast, an outside user can try up to\n# 1 million passwords per second against a modern box. This means that you\n# should use very strong passwords, otherwise they will be very easy to break.\n# Note that because the password is really a shared secret between the client\n# and the server, and should not be memorized by any human, the password\n# can be easily a long string from /dev/urandom or whatever, so by using a\n# long and unguessable password no brute force attack will be possible.\n\n# KeyDB ACL users are defined in the following format:\n#\n#   user <username> ... acl rules ...\n#\n# For example:\n#\n#   user worker +@list +@connection ~jobs:* on >ffa9203c493aa99\n#\n# The special username \"default\" is used for new connections. If this user\n# has the \"nopass\" rule, then new connections will be immediately authenticated\n# as the \"default\" user without the need of any password provided via the\n# AUTH command. Otherwise if the \"default\" user is not flagged with \"nopass\"\n# the connections will start in not authenticated state, and will require\n# AUTH (or the HELLO command AUTH option) in order to be authenticated and\n# start to work.\n#\n# The ACL rules that describe what a user can do are the following:\n#\n#  on           Enable the user: it is possible to authenticate as this user.\n#  off          Disable the user: it's no longer possible to authenticate\n#               with this user, however the already authenticated connections\n#               will still work.\n#  skip-sanitize-payload    RESTORE dump-payload sanitation is skipped.\n#  sanitize-payload         RESTORE dump-payload is sanitized (default).\n#  +<command>   Allow the execution of that command\n#  -<command>   Disallow the execution of that command\n#  +@<category> Allow the execution of all the commands in such category\n#               with valid categories are like @admin, @set, @sortedset, ...\n#               and so forth, see the full list in the server.cpp file where\n#               the KeyDB command table is described and defined.\n#               The special category @all means all the commands, but currently\n#               present in the server, and that will be loaded in the future\n#               via modules.\n#  +<command>|subcommand    Allow a specific subcommand of an otherwise\n#                           disabled command. Note that this form is not\n#                           allowed as negative like -DEBUG|SEGFAULT, but\n#                           only additive starting with \"+\".\n#  allcommands  Alias for +@all. Note that it implies the ability to execute\n#               all the future commands loaded via the modules system.\n#  nocommands   Alias for -@all.\n#  ~<pattern>   Add a pattern of keys that can be mentioned as part of\n#               commands. For instance ~* allows all the keys. The pattern\n#               is a glob-style pattern like the one of KEYS.\n#               It is possible to specify multiple patterns.\n#  allkeys      Alias for ~*\n#  resetkeys    Flush the list of allowed keys patterns.\n#  &<pattern>   Add a glob-style pattern of Pub/Sub channels that can be\n#               accessed by the user. It is possible to specify multiple channel\n#               patterns.\n#  allchannels  Alias for &*\n#  resetchannels            Flush the list of allowed channel patterns.\n#  ><password>  Add this password to the list of valid password for the user.\n#               For example >mypass will add \"mypass\" to the list.\n#               This directive clears the \"nopass\" flag (see later).\n#  <<password>  Remove this password from the list of valid passwords.\n#  nopass       All the set passwords of the user are removed, and the user\n#               is flagged as requiring no password: it means that every\n#               password will work against this user. If this directive is\n#               used for the default user, every new connection will be\n#               immediately authenticated with the default user without\n#               any explicit AUTH command required. Note that the \"resetpass\"\n#               directive will clear this condition.\n#  resetpass    Flush the list of allowed passwords. Moreover removes the\n#               \"nopass\" status. After \"resetpass\" the user has no associated\n#               passwords and there is no way to authenticate without adding\n#               some password (or setting it as \"nopass\" later).\n#  reset        Performs the following actions: resetpass, resetkeys, off,\n#               -@all. The user returns to the same state it has immediately\n#               after its creation.\n#\n# ACL rules can be specified in any order: for instance you can start with\n# passwords, then flags, or key patterns. However note that the additive\n# and subtractive rules will CHANGE MEANING depending on the ordering.\n# For instance see the following example:\n#\n#   user alice on +@all -DEBUG ~* >somepassword\n#\n# This will allow \"alice\" to use all the commands with the exception of the\n# DEBUG command, since +@all added all the commands to the set of the commands\n# alice can use, and later DEBUG was removed. However if we invert the order\n# of two ACL rules the result will be different:\n#\n#   user alice on -DEBUG +@all ~* >somepassword\n#\n# Now DEBUG was removed when alice had yet no commands in the set of allowed\n# commands, later all the commands are added, so the user will be able to\n# execute everything.\n#\n# Basically ACL rules are processed left-to-right.\n#\n# The following is a list of command categories and their meanings:\n# * keyspace - Writing or reading from keys, databases, or their metadata \n#     in a type agnostic way. Includes DEL, RESTORE, DUMP, RENAME, EXISTS, DBSIZE,\n#     KEYS, EXPIRE, TTL, FLUSHALL, etc. Commands that may modify the keyspace,\n#     key or metadata will also have `write` category. Commands that only read\n#     the keyspace, key or metadata will have the `read` category.\n# * read - Reading from keys (values or metadata). Note that commands that don't\n#     interact with keys, will not have either `read` or `write`.\n# * write - Writing to keys (values or metadata)\n# * admin - Administrative commands. Normal applications will never need to use\n#     these. Includes REPLICAOF, CONFIG, DEBUG, SAVE, MONITOR, ACL, SHUTDOWN, etc.\n# * dangerous - Potentially dangerous (each should be considered with care for\n#     various reasons). This includes FLUSHALL, MIGRATE, RESTORE, SORT, KEYS,\n#     CLIENT, DEBUG, INFO, CONFIG, SAVE, REPLICAOF, etc.\n# * connection - Commands affecting the connection or other connections.\n#     This includes AUTH, SELECT, COMMAND, CLIENT, ECHO, PING, etc.\n# * blocking - Potentially blocking the connection until released by another\n#     command.\n# * fast - Fast O(1) commands. May loop on the number of arguments, but not the\n#     number of elements in the key.\n# * slow - All commands that are not Fast.\n# * pubsub - PUBLISH / SUBSCRIBE related\n# * transaction - WATCH / MULTI / EXEC related commands.\n# * scripting - Scripting related.\n# * set - Data type: sets related.\n# * sortedset - Data type: zsets related.\n# * list - Data type: lists related.\n# * hash - Data type: hashes related.\n# * string - Data type: strings related.\n# * bitmap - Data type: bitmaps related.\n# * hyperloglog - Data type: hyperloglog related.\n# * geo - Data type: geo related.\n# * stream - Data type: streams related.\n#\n# For more information about ACL configuration please refer to\n# the Redis web site at https://redis.io/topics/acl\n\n# ACL LOG\n#\n# The ACL Log tracks failed commands and authentication events associated\n# with ACLs. The ACL Log is useful to troubleshoot failed commands blocked \n# by ACLs. The ACL Log is stored in memory. You can reclaim memory with \n# ACL LOG RESET. Define the maximum entry length of the ACL Log below.\nacllog-max-len 128\n\n# Using an external ACL file\n#\n# Instead of configuring users here in this file, it is possible to use\n# a stand-alone file just listing users. The two methods cannot be mixed:\n# if you configure users here and at the same time you activate the external\n# ACL file, the server will refuse to start.\n#\n# The format of the external ACL user file is exactly the same as the\n# format that is used inside keydb.conf to describe users.\n#\n# aclfile /etc/keydb/users.acl\n\n# IMPORTANT NOTE: starting with KeyDB 6 \"requirepass\" is just a compatibility\n# layer on top of the new ACL system. The option effect will be just setting\n# the password for the default user. Clients will still authenticate using\n# AUTH <password> as usually, or more explicitly with AUTH default <password>\n# if they follow the new protocol: both will work.\n#\n# The requirepass is not compatible with aclfile option and the ACL LOAD\n# command, these will cause requirepass to be ignored.\n#\n# requirepass foobared\n\n# New users are initialized with restrictive permissions by default, via the\n# equivalent of this ACL rule 'off resetkeys -@all'. Starting with KeyDB 6.2, it\n# is possible to manage access to Pub/Sub channels with ACL rules as well. The\n# default Pub/Sub channels permission if new users is controlled by the\n# acl-pubsub-default configuration directive, which accepts one of these values:\n#\n# allchannels: grants access to all Pub/Sub channels\n# resetchannels: revokes access to all Pub/Sub channels\n#\n# To ensure backward compatibility while upgrading KeyDB 6.0, acl-pubsub-default\n# defaults to the 'allchannels' permission.\n#\n# Future compatibility note: it is very likely that in a future version of KeyDB\n# the directive's default of 'allchannels' will be changed to 'resetchannels' in\n# order to provide better out-of-the-box Pub/Sub security. Therefore, it is\n# recommended that you explicitly define Pub/Sub permissions for all users\n# rather then rely on implicit default values. Once you've set explicit\n# Pub/Sub for all existing users, you should uncomment the following line.\n#\n# acl-pubsub-default resetchannels\n\n# Command renaming (DEPRECATED).\n#\n# ------------------------------------------------------------------------\n# WARNING: avoid using this option if possible. Instead use ACLs to remove\n# commands from the default user, and put them only in some admin user you\n# create for administrative purposes.\n# ------------------------------------------------------------------------\n#\n# It is possible to change the name of dangerous commands in a shared\n# environment. For instance the CONFIG command may be renamed into something\n# hard to guess so that it will still be available for internal-use tools\n# but not available for general clients.\n#\n# Example:\n#\n# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52\n#\n# It is also possible to completely kill a command by renaming it into\n# an empty string:\n#\n# rename-command CONFIG \"\"\n#\n# Please note that changing the name of commands that are logged into the\n# AOF file or transmitted to replicas may cause problems.\n\n################################### CLIENTS ####################################\n\n# Set the max number of connected clients at the same time. By default\n# this limit is set to 10000 clients, however if the KeyDB server is not\n# able to configure the process file limit to allow for the specified limit\n# the max number of allowed clients is set to the current file limit\n# minus 32 (as KeyDB reserves a few file descriptors for internal uses).\n#\n# Once the limit is reached KeyDB will close all the new connections sending\n# an error 'max number of clients reached'.\n#\n# IMPORTANT: When KeyDB Cluster is used, the max number of connections is also\n# shared with the cluster bus: every node in the cluster will use two\n# connections, one incoming and another outgoing. It is important to size the\n# limit accordingly in case of very large clusters.\n#\n# maxclients 10000\n\n############################## MEMORY MANAGEMENT ################################\n\n# Set a memory usage limit to the specified amount of bytes.\n# When the memory limit is reached KeyDB will try to remove keys\n# according to the eviction policy selected (see maxmemory-policy).\n#\n# If KeyDB can't remove keys according to the policy, or if the policy is\n# set to 'noeviction', KeyDB will start to reply with errors to commands\n# that would use more memory, like SET, LPUSH, and so on, and will continue\n# to reply to read-only commands like GET.\n#\n# This option is usually useful when using KeyDB as an LRU or LFU cache, or to\n# set a hard memory limit for an instance (using the 'noeviction' policy).\n#\n# WARNING: If you have replicas attached to an instance with maxmemory on,\n# the size of the output buffers needed to feed the replicas are subtracted\n# from the used memory count, so that network problems / resyncs will\n# not trigger a loop where keys are evicted, and in turn the output\n# buffer of replicas is full with DELs of keys evicted triggering the deletion\n# of more keys, and so forth until the database is completely emptied.\n#\n# In short... if you have replicas attached it is suggested that you set a lower\n# limit for maxmemory so that there is some free RAM on the system for replica\n# output buffers (but this is not needed if the policy is 'noeviction').\n#\n# maxmemory <bytes>\n\n# MAXMEMORY POLICY: how KeyDB will select what to remove when maxmemory\n# is reached. You can select one from the following behaviors:\n#\n# volatile-lru -> Evict using approximated LRU, only keys with an expire set.\n# allkeys-lru -> Evict any key using approximated LRU.\n# volatile-lfu -> Evict using approximated LFU, only keys with an expire set.\n# allkeys-lfu -> Evict any key using approximated LFU.\n# volatile-random -> Remove a random key having an expire set.\n# allkeys-random -> Remove a random key, any key.\n# volatile-ttl -> Remove the key with the nearest expire time (minor TTL)\n# noeviction -> Don't evict anything, just return an error on write operations.\n#\n# LRU means Least Recently Used\n# LFU means Least Frequently Used\n#\n# Both LRU, LFU and volatile-ttl are implemented using approximated\n# randomized algorithms.\n#\n# Note: with any of the above policies, KeyDB will return an error on write\n#       operations, when there are no suitable keys for eviction.\n#\n#       At the date of writing these commands are: set setnx setex append\n#       incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd\n#       sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby\n#       zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby\n#       getset mset msetnx exec sort\n#\n# The default is:\n#\n# maxmemory-policy noeviction\n\n# LRU, LFU and minimal TTL algorithms are not precise algorithms but approximated\n# algorithms (in order to save memory), so you can tune it for speed or\n# accuracy. By default KeyDB will check five keys and pick the one that was\n# used least recently, you can change the sample size using the following\n# configuration directive.\n#\n# The default of 5 produces good enough results. 10 Approximates very closely\n# true LRU but costs more CPU. 3 is faster but not very accurate.\n#\n# maxmemory-samples 5\n\n# Eviction processing is designed to function well with the default setting.\n# If there is an unusually large amount of write traffic, this value may need to\n# be increased.  Decreasing this value may reduce latency at the risk of\n# eviction processing effectiveness\n#   0 = minimum latency, 10 = default, 100 = process without regard to latency\n#\n# maxmemory-eviction-tenacity 10\n\n# Starting from KeyDB 5, by default a replica will ignore its maxmemory setting\n# (unless it is promoted to master after a failover or manually). It means\n# that the eviction of keys will be just handled by the master, sending the\n# DEL commands to the replica as keys evict in the master side.\n#\n# This behavior ensures that masters and replicas stay consistent, and is usually\n# what you want, however if your replica is writable, or you want the replica\n# to have a different memory setting, and you are sure all the writes performed\n# to the replica are idempotent, then you may change this default (but be sure\n# to understand what you are doing).\n#\n# Note that since the replica by default does not evict, it may end using more\n# memory than the one set via maxmemory (there are certain buffers that may\n# be larger on the replica, or data structures may sometimes take more memory\n# and so forth). So make sure you monitor your replicas and make sure they\n# have enough memory to never hit a real out-of-memory condition before the\n# master hits the configured maxmemory setting.\n#\n# replica-ignore-maxmemory yes\n\n# KeyDB reclaims expired keys in two ways: upon access when those keys are\n# found to be expired, and also in background, in what is called the\n# \"active expire key\". The key space is slowly and interactively scanned\n# looking for expired keys to reclaim, so that it is possible to free memory\n# of keys that are expired and will never be accessed again in a short time.\n#\n# The default effort of the expire cycle will try to avoid having more than\n# ten percent of expired keys still in memory, and will try to avoid consuming\n# more than 25% of total memory and to add latency to the system. However\n# it is possible to increase the expire \"effort\" that is normally set to\n# \"1\", to a greater value, up to the value \"10\". At its maximum value the\n# system will use more CPU, longer cycles (and technically may introduce\n# more latency), and will tolerate less already expired keys still present\n# in the system. It's a tradeoff between memory, CPU and latency.\n#\n# active-expire-effort 1\n\n# Force evictions when used system memory reaches X% of total system memory.\n# This is useful as a safeguard to prevent OOM kills (0 to disable).\n#\n# force-eviction-percent 0\n\n############################# LAZY FREEING ####################################\n\n# KeyDB has two primitives to delete keys. One is called DEL and is a blocking\n# deletion of the object. It means that the server stops processing new commands\n# in order to reclaim all the memory associated with an object in a synchronous\n# way. If the key deleted is associated with a small object, the time needed\n# in order to execute the DEL command is very small and comparable to most other\n# O(1) or O(log_N) commands in KeyDB. However if the key is associated with an\n# aggregated value containing millions of elements, the server can block for\n# a long time (even seconds) in order to complete the operation.\n#\n# For the above reasons KeyDB also offers non blocking deletion primitives\n# such as UNLINK (non blocking DEL) and the ASYNC option of FLUSHALL and\n# FLUSHDB commands, in order to reclaim memory in background. Those commands\n# are executed in constant time. Another thread will incrementally free the\n# object in the background as fast as possible.\n#\n# DEL, UNLINK and ASYNC option of FLUSHALL and FLUSHDB are user-controlled.\n# It's up to the design of the application to understand when it is a good\n# idea to use one or the other. However the KeyDB server sometimes has to\n# delete keys or flush the whole database as a side effect of other operations.\n# Specifically KeyDB deletes objects independently of a user call in the\n# following scenarios:\n#\n# 1) On eviction, because of the maxmemory and maxmemory policy configurations,\n#    in order to make room for new data, without going over the specified\n#    memory limit.\n# 2) Because of expire: when a key with an associated time to live (see the\n#    EXPIRE command) must be deleted from memory.\n# 3) Because of a side effect of a command that stores data on a key that may\n#    already exist. For example the RENAME command may delete the old key\n#    content when it is replaced with another one. Similarly SUNIONSTORE\n#    or SORT with STORE option may delete existing keys. The SET command\n#    itself removes any old content of the specified key in order to replace\n#    it with the specified string.\n# 4) During replication, when a replica performs a full resynchronization with\n#    its master, the content of the whole database is removed in order to\n#    load the RDB file just transferred.\n#\n# In all the above cases the default is to delete objects in a blocking way,\n# like if DEL was called. However you can configure each case specifically\n# in order to instead release memory in a non-blocking way like if UNLINK\n# was called, using the following configuration directives.\n\nlazyfree-lazy-eviction no\nlazyfree-lazy-expire no\nlazyfree-lazy-server-del no\nreplica-lazy-flush no\n\n# It is also possible, for the case when to replace the user code DEL calls\n# with UNLINK calls is not easy, to modify the default behavior of the DEL\n# command to act exactly like UNLINK, using the following configuration\n# directive:\n\nlazyfree-lazy-user-del no\n\n# FLUSHDB, FLUSHALL, and SCRIPT FLUSH support both asynchronous and synchronous\n# deletion, which can be controlled by passing the [SYNC|ASYNC] flags into the\n# commands. When neither flag is passed, this directive will be used to determine\n# if the data should be deleted asynchronously.\n\nlazyfree-lazy-user-flush no\n\n############################ KERNEL OOM CONTROL ##############################\n\n# On Linux, it is possible to hint the kernel OOM killer on what processes\n# should be killed first when out of memory.\n#\n# Enabling this feature makes KeyDB actively control the oom_score_adj value\n# for all its processes, depending on their role. The default scores will\n# attempt to have background child processes killed before all others, and\n# replicas killed before masters.\n#\n# KeyDB supports three options:\n#\n# no:       Don't make changes to oom-score-adj (default).\n# yes:      Alias to \"relative\" see below.\n# absolute: Values in oom-score-adj-values are written as is to the kernel.\n# relative: Values are used relative to the initial value of oom_score_adj when\n#           the server starts and are then clamped to a range of -1000 to 1000.\n#           Because typically the initial value is 0, they will often match the\n#           absolute values.\noom-score-adj no\n\n# When oom-score-adj is used, this directive controls the specific values used\n# for master, replica and background child processes. Values range -2000 to\n# 2000 (higher means more likely to be killed).\n#\n# Unprivileged processes (not root, and without CAP_SYS_RESOURCE capabilities)\n# can freely increase their value, but not decrease it below its initial\n# settings. This means that setting oom-score-adj to \"relative\" and setting the\n# oom-score-adj-values to positive values will always succeed.\noom-score-adj-values 0 200 800\n\n\n#################### KERNEL transparent hugepage CONTROL ######################\n\n# Usually the kernel Transparent Huge Pages control is set to \"madvise\" or\n# or \"never\" by default (/sys/kernel/mm/transparent_hugepage/enabled), in which\n# case this config has no effect. On systems in which it is set to \"always\",\n# KeyDB will attempt to disable it specifically for the KeyDB process in order\n# to avoid latency problems specifically with fork(2) and CoW.\n# If for some reason you prefer to keep it enabled, you can set this config to\n# \"no\" and the kernel global to \"always\".\n\ndisable-thp yes\n\n############################## APPEND ONLY MODE ###############################\n\n# By default KeyDB asynchronously dumps the dataset on disk. This mode is\n# good enough in many applications, but an issue with the KeyDB process or\n# a power outage may result into a few minutes of writes lost (depending on\n# the configured save points).\n#\n# The Append Only File is an alternative persistence mode that provides\n# much better durability. For instance using the default data fsync policy\n# (see later in the config file) KeyDB can lose just one second of writes in a\n# dramatic event like a server power outage, or a single write if something\n# wrong with the KeyDB process itself happens, but the operating system is\n# still running correctly.\n#\n# AOF and RDB persistence can be enabled at the same time without problems.\n# If the AOF is enabled on startup KeyDB will load the AOF, that is the file\n# with the better durability guarantees.\n#\n# Please check http://redis.io/topics/persistence for more information.\n\nappendonly no\n\n# The name of the append only file (default: \"appendonly.aof\")\n\nappendfilename \"appendonly.aof\"\n\n# The fsync() call tells the Operating System to actually write data on disk\n# instead of waiting for more data in the output buffer. Some OS will really flush\n# data on disk, some other OS will just try to do it ASAP.\n#\n# KeyDB supports three different modes:\n#\n# no: don't fsync, just let the OS flush the data when it wants. Faster.\n# always: fsync after every write to the append only log. Slow, Safest.\n# everysec: fsync only one time every second. Compromise.\n#\n# The default is \"everysec\", as that's usually the right compromise between\n# speed and data safety. It's up to you to understand if you can relax this to\n# \"no\" that will let the operating system flush the output buffer when\n# it wants, for better performances (but if you can live with the idea of\n# some data loss consider the default persistence mode that's snapshotting),\n# or on the contrary, use \"always\" that's very slow but a bit safer than\n# everysec.\n#\n# More details please check the following article:\n# http://antirez.com/post/redis-persistence-demystified.html\n#\n# If unsure, use \"everysec\".\n\n# appendfsync always\nappendfsync everysec\n# appendfsync no\n\n# When the AOF fsync policy is set to always or everysec, and a background\n# saving process (a background save or AOF log background rewriting) is\n# performing a lot of I/O against the disk, in some Linux configurations\n# KeyDB may block too long on the fsync() call. Note that there is no fix for\n# this currently, as even performing fsync in a different thread will block\n# our synchronous write(2) call.\n#\n# In order to mitigate this problem it's possible to use the following option\n# that will prevent fsync() from being called in the main process while a\n# BGSAVE or BGREWRITEAOF is in progress.\n#\n# This means that while another child is saving, the durability of KeyDB is\n# the same as \"appendfsync none\". In practical terms, this means that it is\n# possible to lose up to 30 seconds of log in the worst scenario (with the\n# default Linux settings).\n#\n# If you have latency problems turn this to \"yes\". Otherwise leave it as\n# \"no\" that is the safest pick from the point of view of durability.\n\nno-appendfsync-on-rewrite no\n\n# Automatic rewrite of the append only file.\n# KeyDB is able to automatically rewrite the log file implicitly calling\n# BGREWRITEAOF when the AOF log size grows by the specified percentage.\n#\n# This is how it works: KeyDB remembers the size of the AOF file after the\n# latest rewrite (if no rewrite has happened since the restart, the size of\n# the AOF at startup is used).\n#\n# This base size is compared to the current size. If the current size is\n# bigger than the specified percentage, the rewrite is triggered. Also\n# you need to specify a minimal size for the AOF file to be rewritten, this\n# is useful to avoid rewriting the AOF file even if the percentage increase\n# is reached but it is still pretty small.\n#\n# Specify a percentage of zero in order to disable the automatic AOF\n# rewrite feature.\n\nauto-aof-rewrite-percentage 100\nauto-aof-rewrite-min-size 64mb\n\n# An AOF file may be found to be truncated at the end during the KeyDB\n# startup process, when the AOF data gets loaded back into memory.\n# This may happen when the system where KeyDB is running\n# crashes, especially when an ext4 filesystem is mounted without the\n# data=ordered option (however this can't happen when KeyDB itself\n# crashes or aborts but the operating system still works correctly).\n#\n# KeyDB can either exit with an error when this happens, or load as much\n# data as possible (the default now) and start if the AOF file is found\n# to be truncated at the end. The following option controls this behavior.\n#\n# If aof-load-truncated is set to yes, a truncated AOF file is loaded and\n# the KeyDB server starts emitting a log to inform the user of the event.\n# Otherwise if the option is set to no, the server aborts with an error\n# and refuses to start. When the option is set to no, the user requires\n# to fix the AOF file using the \"keydb-check-aof\" utility before to restart\n# the server.\n#\n# Note that if the AOF file will be found to be corrupted in the middle\n# the server will still exit with an error. This option only applies when\n# KeyDB will try to read more data from the AOF file but not enough bytes\n# will be found.\naof-load-truncated yes\n\n# When rewriting the AOF file, KeyDB is able to use an RDB preamble in the\n# AOF file for faster rewrites and recoveries. When this option is turned\n# on the rewritten AOF file is composed of two different stanzas:\n#\n#   [RDB file][AOF tail]\n#\n# When loading, KeyDB recognizes that the AOF file starts with the \"REDIS\"\n# string and loads the prefixed RDB file, then continues loading the AOF\n# tail.\naof-use-rdb-preamble yes\n\n################################ LUA SCRIPTING  ###############################\n\n# Max execution time of a Lua script in milliseconds.\n#\n# If the maximum execution time is reached KeyDB will log that a script is\n# still in execution after the maximum allowed time and will start to\n# reply to queries with an error.\n#\n# When a long running script exceeds the maximum execution time only the\n# SCRIPT KILL and SHUTDOWN NOSAVE commands are available. The first can be\n# used to stop a script that did not yet call any write commands. The second\n# is the only way to shut down the server in the case a write command was\n# already issued by the script but the user doesn't want to wait for the natural\n# termination of the script.\n#\n# Set it to 0 or a negative value for unlimited execution without warnings.\nlua-time-limit 5000\n\n################################ KEYDB CLUSTER  ###############################\n\n# Normal KeyDB instances can't be part of a KeyDB Cluster; only nodes that are\n# started as cluster nodes can. In order to start a KeyDB instance as a\n# cluster node enable the cluster support uncommenting the following:\n#\n# cluster-enabled yes\n\n# Every cluster node has a cluster configuration file. This file is not\n# intended to be edited by hand. It is created and updated by KeyDB nodes.\n# Every KeyDB Cluster node requires a different cluster configuration file.\n# Make sure that instances running in the same system do not have\n# overlapping cluster configuration file names.\n#\n# cluster-config-file nodes-6379.conf\n\n# Cluster node timeout is the amount of milliseconds a node must be unreachable\n# for it to be considered in failure state.\n# Most other internal time limits are a multiple of the node timeout.\n#\n# cluster-node-timeout 15000\n\n# A replica of a failing master will avoid to start a failover if its data\n# looks too old.\n#\n# There is no simple way for a replica to actually have an exact measure of\n# its \"data age\", so the following two checks are performed:\n#\n# 1) If there are multiple replicas able to failover, they exchange messages\n#    in order to try to give an advantage to the replica with the best\n#    replication offset (more data from the master processed).\n#    Replicas will try to get their rank by offset, and apply to the start\n#    of the failover a delay proportional to their rank.\n#\n# 2) Every single replica computes the time of the last interaction with\n#    its master. This can be the last ping or command received (if the master\n#    is still in the \"connected\" state), or the time that elapsed since the\n#    disconnection with the master (if the replication link is currently down).\n#    If the last interaction is too old, the replica will not try to failover\n#    at all.\n#\n# The point \"2\" can be tuned by user. Specifically a replica will not perform\n# the failover if, since the last interaction with the master, the time\n# elapsed is greater than:\n#\n#   (node-timeout * cluster-replica-validity-factor) + repl-ping-replica-period\n#\n# So for example if node-timeout is 30 seconds, and the cluster-replica-validity-factor\n# is 10, and assuming a default repl-ping-replica-period of 10 seconds, the\n# replica will not try to failover if it was not able to talk with the master\n# for longer than 310 seconds.\n#\n# A large cluster-replica-validity-factor may allow replicas with too old data to failover\n# a master, while a too small value may prevent the cluster from being able to\n# elect a replica at all.\n#\n# For maximum availability, it is possible to set the cluster-replica-validity-factor\n# to a value of 0, which means, that replicas will always try to failover the\n# master regardless of the last time they interacted with the master.\n# (However they'll always try to apply a delay proportional to their\n# offset rank).\n#\n# Zero is the only value able to guarantee that when all the partitions heal\n# the cluster will always be able to continue.\n#\n# cluster-replica-validity-factor 10\n\n# Cluster replicas are able to migrate to orphaned masters, that are masters\n# that are left without working replicas. This improves the cluster ability\n# to resist to failures as otherwise an orphaned master can't be failed over\n# in case of failure if it has no working replicas.\n#\n# Replicas migrate to orphaned masters only if there are still at least a\n# given number of other working replicas for their old master. This number\n# is the \"migration barrier\". A migration barrier of 1 means that a replica\n# will migrate only if there is at least 1 other working replica for its master\n# and so forth. It usually reflects the number of replicas you want for every\n# master in your cluster.\n#\n# Default is 1 (replicas migrate only if their masters remain with at least\n# one replica). To disable migration just set it to a very large value or\n# set cluster-allow-replica-migration to 'no'.\n# A value of 0 can be set but is useful only for debugging and dangerous\n# in production.\n#\n# cluster-migration-barrier 1\n\n# Turning off this option allows to use less automatic cluster configuration.\n# It both disables migration to orphaned masters and migration from masters\n# that became empty.\n#\n# Default is 'yes' (allow automatic migrations).\n#\n# cluster-allow-replica-migration yes\n\n# By default KeyDB Cluster nodes stop accepting queries if they detect there\n# is at least a hash slot uncovered (no available node is serving it).\n# This way if the cluster is partially down (for example a range of hash slots\n# are no longer covered) all the cluster becomes, eventually, unavailable.\n# It automatically returns available as soon as all the slots are covered again.\n#\n# However sometimes you want the subset of the cluster which is working,\n# to continue to accept queries for the part of the key space that is still\n# covered. In order to do so, just set the cluster-require-full-coverage\n# option to no.\n#\n# cluster-require-full-coverage yes\n\n# This option, when set to yes, prevents replicas from trying to failover its\n# master during master failures. However the master can still perform a\n# manual failover, if forced to do so.\n#\n# This is useful in different scenarios, especially in the case of multiple\n# data center operations, where we want one side to never be promoted if not\n# in the case of a total DC failure.\n#\n# cluster-replica-no-failover no\n\n# This option, when set to yes, allows nodes to serve read traffic while the\n# the cluster is in a down state, as long as it believes it owns the slots. \n#\n# This is useful for two cases.  The first case is for when an application \n# doesn't require consistency of data during node failures or network partitions.\n# One example of this is a cache, where as long as the node has the data it\n# should be able to serve it. \n#\n# The second use case is for configurations that don't meet the recommended  \n# three shards but want to enable cluster mode and scale later. A \n# master outage in a 1 or 2 shard configuration causes a read/write outage to the\n# entire cluster without this option set, with it set there is only a write outage.\n# Without a quorum of masters, slot ownership will not change automatically. \n#\n# cluster-allow-reads-when-down no\n\n# In order to setup your cluster make sure to read the documentation\n# available at http://redis.io web site.\n\n########################## CLUSTER DOCKER/NAT support  ########################\n\n# In certain deployments, KeyDB Cluster nodes address discovery fails, because\n# addresses are NAT-ted or because ports are forwarded (the typical case is\n# Docker and other containers).\n#\n# In order to make KeyDB Cluster working in such environments, a static\n# configuration where each node knows its public address is needed. The\n# following four options are used for this scope, and are:\n#\n# * cluster-announce-ip\n# * cluster-announce-port\n# * cluster-announce-tls-port\n# * cluster-announce-bus-port\n#\n# Each instructs the node about its address, client ports (for connections\n# without and with TLS), and cluster message\n# bus port. The information is then published in the header of the bus packets\n# so that other nodes will be able to correctly map the address of the node\n# publishing the information.\n#\n# If cluster-tls is set to yes and cluster-announce-tls-port is omitted or set\n# to zero, then cluster-announce-port refers to the TLS port. Note also that\n# cluster-announce-tls-port has no effect if cluster-tls is set to no.\n#\n# If the above options are not used, the normal KeyDB Cluster auto-detection\n# will be used instead.\n#\n# Note that when remapped, the bus port may not be at the fixed offset of\n# clients port + 10000, so you can specify any port and bus-port depending\n# on how they get remapped. If the bus-port is not set, a fixed offset of\n# 10000 will be used as usual.\n#\n# Example:\n#\n# cluster-announce-ip 10.1.1.5\n# cluster-announce-tls-port 6379\n# cluster-announce-port 0\n# cluster-announce-bus-port 6380\n\n################################## SLOW LOG ###################################\n\n# The KeyDB Slow Log is a system to log queries that exceeded a specified\n# execution time. The execution time does not include the I/O operations\n# like talking with the client, sending the reply and so forth,\n# but just the time needed to actually execute the command (this is the only\n# stage of command execution where the thread is blocked and can not serve\n# other requests in the meantime).\n#\n# You can configure the slow log with two parameters: one tells KeyDB\n# what is the execution time, in microseconds, to exceed in order for the\n# command to get logged, and the other parameter is the length of the\n# slow log. When a new command is logged the oldest one is removed from the\n# queue of logged commands.\n\n# The following time is expressed in microseconds, so 1000000 is equivalent\n# to one second. Note that a negative number disables the slow log, while\n# a value of zero forces the logging of every command.\nslowlog-log-slower-than 10000\n\n# There is no limit to this length. Just be aware that it will consume memory.\n# You can reclaim memory used by the slow log with SLOWLOG RESET.\nslowlog-max-len 128\n\n################################ LATENCY MONITOR ##############################\n\n# The KeyDB latency monitoring subsystem samples different operations\n# at runtime in order to collect data related to possible sources of\n# latency of a KeyDB instance.\n#\n# Via the LATENCY command this information is available to the user that can\n# print graphs and obtain reports.\n#\n# The system only logs operations that were performed in a time equal or\n# greater than the amount of milliseconds specified via the\n# latency-monitor-threshold configuration directive. When its value is set\n# to zero, the latency monitor is turned off.\n#\n# By default latency monitoring is disabled since it is mostly not needed\n# if you don't have latency issues, and collecting data has a performance\n# impact, that while very small, can be measured under big load. Latency\n# monitoring can easily be enabled at runtime using the command\n# \"CONFIG SET latency-monitor-threshold <milliseconds>\" if needed.\nlatency-monitor-threshold 0\n\n############################# EVENT NOTIFICATION ##############################\n\n# KeyDB can notify Pub/Sub clients about events happening in the key space.\n# This feature is documented at http://redis.io/topics/notifications\n#\n# For instance if keyspace events notification is enabled, and a client\n# performs a DEL operation on key \"foo\" stored in the Database 0, two\n# messages will be published via Pub/Sub:\n#\n# PUBLISH __keyspace@0__:foo del\n# PUBLISH __keyevent@0__:del foo\n#\n# It is possible to select the events that KeyDB will notify among a set\n# of classes. Every class is identified by a single character:\n#\n#  K     Keyspace events, published with __keyspace@<db>__ prefix.\n#  E     Keyevent events, published with __keyevent@<db>__ prefix.\n#  g     Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ...\n#  $     String commands\n#  l     List commands\n#  s     Set commands\n#  h     Hash commands\n#  z     Sorted set commands\n#  x     Expired events (events generated every time a key expires)\n#  e     Evicted events (events generated when a key is evicted for maxmemory)\n#  t     Stream commands\n#  d     Module key type events\n#  m     Key-miss events (Note: It is not included in the 'A' class)\n#  A     Alias for g$lshzxetd, so that the \"AKE\" string means all the events\n#        (Except key-miss events which are excluded from 'A' due to their\n#         unique nature).\n#\n#  The \"notify-keyspace-events\" takes as argument a string that is composed\n#  of zero or multiple characters. The empty string means that notifications\n#  are disabled.\n#\n#  Example: to enable list and generic events, from the point of view of the\n#           event name, use:\n#\n#  notify-keyspace-events Elg\n#\n#  Example 2: to get the stream of the expired keys subscribing to channel\n#             name __keyevent@0__:expired use:\n#\n#  notify-keyspace-events Ex\n#\n#  By default all notifications are disabled because most users don't need\n#  this feature and the feature has some overhead. Note that if you don't\n#  specify at least one of K or E, no events will be delivered.\nnotify-keyspace-events \"\"\n\n############################### ADVANCED CONFIG ###############################\n\n# Hashes are encoded using a memory efficient data structure when they have a\n# small number of entries, and the biggest entry does not exceed a given\n# threshold. These thresholds can be configured using the following directives.\nhash-max-ziplist-entries 512\nhash-max-ziplist-value 64\n\n# Lists are also encoded in a special way to save a lot of space.\n# The number of entries allowed per internal list node can be specified\n# as a fixed maximum size or a maximum number of elements.\n# For a fixed maximum size, use -5 through -1, meaning:\n# -5: max size: 64 Kb  <-- not recommended for normal workloads\n# -4: max size: 32 Kb  <-- not recommended\n# -3: max size: 16 Kb  <-- probably not recommended\n# -2: max size: 8 Kb   <-- good\n# -1: max size: 4 Kb   <-- good\n# Positive numbers mean store up to _exactly_ that number of elements\n# per list node.\n# The highest performing option is usually -2 (8 Kb size) or -1 (4 Kb size),\n# but if your use case is unique, adjust the settings as necessary.\nlist-max-ziplist-size -2\n\n# Lists may also be compressed.\n# Compress depth is the number of quicklist ziplist nodes from *each* side of\n# the list to *exclude* from compression.  The head and tail of the list\n# are always uncompressed for fast push/pop operations.  Settings are:\n# 0: disable all list compression\n# 1: depth 1 means \"don't start compressing until after 1 node into the list,\n#    going from either the head or tail\"\n#    So: [head]->node->node->...->node->[tail]\n#    [head], [tail] will always be uncompressed; inner nodes will compress.\n# 2: [head]->[next]->node->node->...->node->[prev]->[tail]\n#    2 here means: don't compress head or head->next or tail->prev or tail,\n#    but compress all nodes between them.\n# 3: [head]->[next]->[next]->node->node->...->node->[prev]->[prev]->[tail]\n# etc.\nlist-compress-depth 0\n\n# Sets have a special encoding in just one case: when a set is composed\n# of just strings that happen to be integers in radix 10 in the range\n# of 64 bit signed integers.\n# The following configuration setting sets the limit in the size of the\n# set in order to use this special memory saving encoding.\nset-max-intset-entries 512\n\n# Similarly to hashes and lists, sorted sets are also specially encoded in\n# order to save a lot of space. This encoding is only used when the length and\n# elements of a sorted set are below the following limits:\nzset-max-ziplist-entries 128\nzset-max-ziplist-value 64\n\n# HyperLogLog sparse representation bytes limit. The limit includes the\n# 16 bytes header. When an HyperLogLog using the sparse representation crosses\n# this limit, it is converted into the dense representation.\n#\n# A value greater than 16000 is totally useless, since at that point the\n# dense representation is more memory efficient.\n#\n# The suggested value is ~ 3000 in order to have the benefits of\n# the space efficient encoding without slowing down too much PFADD,\n# which is O(N) with the sparse encoding. The value can be raised to\n# ~ 10000 when CPU is not a concern, but space is, and the data set is\n# composed of many HyperLogLogs with cardinality in the 0 - 15000 range.\nhll-sparse-max-bytes 3000\n\n# Streams macro node max size / items. The stream data structure is a radix\n# tree of big nodes that encode multiple items inside. Using this configuration\n# it is possible to configure how big a single node can be in bytes, and the\n# maximum number of items it may contain before switching to a new node when\n# appending new stream entries. If any of the following settings are set to\n# zero, the limit is ignored, so for instance it is possible to set just a\n# max entires limit by setting max-bytes to 0 and max-entries to the desired\n# value.\nstream-node-max-bytes 4096\nstream-node-max-entries 100\n\n# Active rehashing uses 1 millisecond every 100 milliseconds of CPU time in\n# order to help rehashing the main KeyDB hash table (the one mapping top-level\n# keys to values). The hash table implementation KeyDB uses (see dict.c)\n# performs a lazy rehashing: the more operation you run into a hash table\n# that is rehashing, the more rehashing \"steps\" are performed, so if the\n# server is idle the rehashing is never complete and some more memory is used\n# by the hash table.\n#\n# The default is to use this millisecond 10 times every second in order to\n# actively rehash the main dictionaries, freeing memory when possible.\n#\n# If unsure:\n# use \"activerehashing no\" if you have hard latency requirements and it is\n# not a good thing in your environment that KeyDB can reply from time to time\n# to queries with 2 milliseconds delay.\n#\n# use \"activerehashing yes\" if you don't have such hard requirements but\n# want to free memory asap when possible.\nactiverehashing yes\n\n# The client output buffer limits can be used to force disconnection of clients\n# that are not reading data from the server fast enough for some reason (a\n# common reason is that a Pub/Sub client can't consume messages as fast as the\n# publisher can produce them).\n#\n# The limit can be set differently for the three different classes of clients:\n#\n# normal -> normal clients including MONITOR clients\n# replica  -> replica clients\n# pubsub -> clients subscribed to at least one pubsub channel or pattern\n#\n# The syntax of every client-output-buffer-limit directive is the following:\n#\n# client-output-buffer-limit <class> <hard limit> <soft limit> <soft seconds>\n#\n# A client is immediately disconnected once the hard limit is reached, or if\n# the soft limit is reached and remains reached for the specified number of\n# seconds (continuously).\n# So for instance if the hard limit is 32 megabytes and the soft limit is\n# 16 megabytes / 10 seconds, the client will get disconnected immediately\n# if the size of the output buffers reach 32 megabytes, but will also get\n# disconnected if the client reaches 16 megabytes and continuously overcomes\n# the limit for 10 seconds.\n#\n# By default normal clients are not limited because they don't receive data\n# without asking (in a push way), but just after a request, so only\n# asynchronous clients may create a scenario where data is requested faster\n# than it can read.\n#\n# Instead there is a default limit for pubsub and replica clients, since\n# subscribers and replicas receive data in a push fashion.\n#\n# Both the hard or the soft limit can be disabled by setting them to zero.\nclient-output-buffer-limit normal 0 0 0\nclient-output-buffer-limit replica 256mb 64mb 60\nclient-output-buffer-limit pubsub 32mb 8mb 60\n\n# Client query buffers accumulate new commands. They are limited to a fixed\n# amount by default in order to avoid that a protocol desynchronization (for\n# instance due to a bug in the client) will lead to unbound memory usage in\n# the query buffer. However you can configure it here if you have very special\n# needs, such us huge multi/exec requests or alike.\n#\n# client-query-buffer-limit 1gb\n\n# In the KeyDB protocol, bulk requests, that are, elements representing single\n# strings, are normally limited to 512 mb. However you can change this limit\n# here, but must be 1mb or greater\n#\n# proto-max-bulk-len 512mb\n\n# KeyDB calls an internal function to perform many background tasks, like\n# closing connections of clients in timeout, purging expired keys that are\n# never requested, and so forth.\n#\n# Not all tasks are performed with the same frequency, but KeyDB checks for\n# tasks to perform according to the specified \"hz\" value.\n#\n# By default \"hz\" is set to 10. Raising the value will use more CPU when\n# KeyDB is idle, but at the same time will make KeyDB more responsive when\n# there are many keys expiring at the same time, and timeouts may be\n# handled with more precision.\n#\n# The range is between 1 and 500, however a value over 100 is usually not\n# a good idea. Most users should use the default of 10 and raise this up to\n# 100 only in environments where very low latency is required.\nhz 10\n\n# Normally it is useful to have an HZ value which is proportional to the\n# number of clients connected. This is useful in order, for instance, to\n# avoid too many clients are processed for each background task invocation\n# in order to avoid latency spikes.\n#\n# Since the default HZ value by default is conservatively set to 10, KeyDB\n# offers, and enables by default, the ability to use an adaptive HZ value\n# which will temporarily raise when there are many connected clients.\n#\n# When dynamic HZ is enabled, the actual configured HZ will be used\n# as a baseline, but multiples of the configured HZ value will be actually\n# used as needed once more clients are connected. In this way an idle\n# instance will use very little CPU time while a busy instance will be\n# more responsive.\ndynamic-hz yes\n\n# When a child rewrites the AOF file, if the following option is enabled\n# the file will be fsync-ed every 32 MB of data generated. This is useful\n# in order to commit the file to the disk more incrementally and avoid\n# big latency spikes.\naof-rewrite-incremental-fsync yes\n\n# When KeyDB saves RDB file, if the following option is enabled\n# the file will be fsync-ed every 32 MB of data generated. This is useful\n# in order to commit the file to the disk more incrementally and avoid\n# big latency spikes.\nrdb-save-incremental-fsync yes\n\n# KeyDB LFU eviction (see maxmemory setting) can be tuned. However it is a good\n# idea to start with the default settings and only change them after investigating\n# how to improve the performances and how the keys LFU change over time, which\n# is possible to inspect via the OBJECT FREQ command.\n#\n# There are two tunable parameters in the KeyDB LFU implementation: the\n# counter logarithm factor and the counter decay time. It is important to\n# understand what the two parameters mean before changing them.\n#\n# The LFU counter is just 8 bits per key, it's maximum value is 255, so KeyDB\n# uses a probabilistic increment with logarithmic behavior. Given the value\n# of the old counter, when a key is accessed, the counter is incremented in\n# this way:\n#\n# 1. A random number R between 0 and 1 is extracted.\n# 2. A probability P is calculated as 1/(old_value*lfu_log_factor+1).\n# 3. The counter is incremented only if R < P.\n#\n# The default lfu-log-factor is 10. This is a table of how the frequency\n# counter changes with a different number of accesses with different\n# logarithmic factors:\n#\n# +--------+------------+------------+------------+------------+------------+\n# | factor | 100 hits   | 1000 hits  | 100K hits  | 1M hits    | 10M hits   |\n# +--------+------------+------------+------------+------------+------------+\n# | 0      | 104        | 255        | 255        | 255        | 255        |\n# +--------+------------+------------+------------+------------+------------+\n# | 1      | 18         | 49         | 255        | 255        | 255        |\n# +--------+------------+------------+------------+------------+------------+\n# | 10     | 10         | 18         | 142        | 255        | 255        |\n# +--------+------------+------------+------------+------------+------------+\n# | 100    | 8          | 11         | 49         | 143        | 255        |\n# +--------+------------+------------+------------+------------+------------+\n#\n# NOTE: The above table was obtained by running the following commands:\n#\n#   keydb-benchmark -n 1000000 incr foo\n#   keydb-cli object freq foo\n#\n# NOTE 2: The counter initial value is 5 in order to give new objects a chance\n# to accumulate hits.\n#\n# The counter decay time is the time, in minutes, that must elapse in order\n# for the key counter to be divided by two (or decremented if it has a value\n# less <= 10).\n#\n# The default value for the lfu-decay-time is 1. A special value of 0 means to\n# decay the counter every time it happens to be scanned.\n#\n# lfu-log-factor 10\n# lfu-decay-time 1\n\n########################### ACTIVE DEFRAGMENTATION #######################\n#\n# What is active defragmentation?\n# -------------------------------\n#\n# Active (online) defragmentation allows a KeyDB server to compact the\n# spaces left between small allocations and deallocations of data in memory,\n# thus allowing to reclaim back memory.\n#\n# Fragmentation is a natural process that happens with every allocator (but\n# less so with Jemalloc, fortunately) and certain workloads. Normally a server\n# restart is needed in order to lower the fragmentation, or at least to flush\n# away all the data and create it again. However thanks to this feature\n# implemented by Oran Agra for Redis 4.0 this process can happen at runtime\n# in a \"hot\" way, while the server is running.\n#\n# Basically when the fragmentation is over a certain level (see the\n# configuration options below) KeyDB will start to create new copies of the\n# values in contiguous memory regions by exploiting certain specific Jemalloc\n# features (in order to understand if an allocation is causing fragmentation\n# and to allocate it in a better place), and at the same time, will release the\n# old copies of the data. This process, repeated incrementally for all the keys\n# will cause the fragmentation to drop back to normal values.\n#\n# Important things to understand:\n#\n# 1. This feature is disabled by default, and only works if you compiled KeyDB\n#    to use the copy of Jemalloc we ship with the source code of KeyDB.\n#    This is the default with Linux builds.\n#\n# 2. You never need to enable this feature if you don't have fragmentation\n#    issues.\n#\n# 3. Once you experience fragmentation, you can enable this feature when\n#    needed with the command \"CONFIG SET activedefrag yes\".\n#\n# The configuration parameters are able to fine tune the behavior of the\n# defragmentation process. If you are not sure about what they mean it is\n# a good idea to leave the defaults untouched.\n\n# Enabled active defragmentation\n# activedefrag no\n\n# Minimum amount of fragmentation waste to start active defrag\n# active-defrag-ignore-bytes 100mb\n\n# Minimum percentage of fragmentation to start active defrag\n# active-defrag-threshold-lower 10\n\n# Maximum percentage of fragmentation at which we use maximum effort\n# active-defrag-threshold-upper 100\n\n# Minimal effort for defrag in CPU percentage, to be used when the lower\n# threshold is reached\n# active-defrag-cycle-min 1\n\n# Maximal effort for defrag in CPU percentage, to be used when the upper\n# threshold is reached\n# active-defrag-cycle-max 25\n\n# Maximum number of set/hash/zset/list fields that will be processed from\n# the main dictionary scan\n# active-defrag-max-scan-fields 1000\n\n# Jemalloc background thread for purging will be enabled by default\njemalloc-bg-thread yes\n\n# It is possible to pin different threads and processes of KeyDB to specific\n# CPUs in your system, in order to maximize the performances of the server.\n# This is useful both in order to pin different KeyDB threads in different\n# CPUs, but also in order to make sure that multiple KeyDB instances running\n# in the same host will be pinned to different CPUs.\n#\n# Normally you can do this using the \"taskset\" command, however it is also\n# possible to this via KeyDB configuration directly, both in Linux and FreeBSD.\n#\n# You can pin the server/IO threads, bio threads, aof rewrite child process, and\n# the bgsave child process. The syntax to specify the cpu list is the same as\n# the taskset command:\n#\n# Set redis server/io threads to cpu affinity 0,2,4,6:\n# server_cpulist 0-7:2\n#\n# Set bio threads to cpu affinity 1,3:\n# bio_cpulist 1,3\n#\n# Set aof rewrite child process to cpu affinity 8,9,10,11:\n# aof_rewrite_cpulist 8-11\n#\n# Set bgsave child process to cpu affinity 1,10,11\n# bgsave_cpulist 1,10-11\n\n# In some cases KeyDB will emit warnings and even refuse to start if it detects\n# that the system is in bad state, it is possible to suppress these warnings\n# by setting the following config which takes a space delimited list of warnings\n# to suppress\n#\n# ignore-warnings ARM64-COW-BUG\n\n# The minimum number of clients on a thread before KeyDB assigns new connections to a different thread\n#  Tuning this parameter is a tradeoff between locking overhead and distributing the workload over multiple cores\n# min-clients-per-thread 50\n\n# How often to run RDB load progress callback?\n# The callback runs during key load to ping other servers and prevent timeouts.\n# It also updates load time estimates.\n# Change these values to run it more or less often. It will run when either condition is true.\n# Either when x bytes have been processed, or when x keys have been loaded.\n# loading-process-events-interval-bytes 2097152\n# loading-process-events-interval-keys 8192\n\n# Avoid forwarding RREPLAY messages to other masters?\n#   WARNING: This setting is dangerous! You must be certain all masters are connected to each\n#   other in a true mesh topology or data loss will occur!\n#   This command can be used to reduce multimaster bus traffic\n# multi-master-no-forward no\n\n# Path to directory for file backed scratchpad.  The file backed scratchpad\n# reduces memory requirements by storing rarely accessed data on disk \n# instead of RAM.  A temporary file will be created in this directory.\n# scratch-file-path /tmp/\n\n# Number of worker threads serving requests.  This number should be related to the performance\n# of your network hardware, not the number of cores on your machine.  We don't recommend going\n# above 4 at this time.  By default this is set 1.\n#\n# Note: KeyDB does not use io-threads, but io-threads is a config alias for server-threads\nserver-threads 2\n\n# Should KeyDB pin threads to CPUs? By default this is disabled, and KeyDB will not bind threads.\n# When enabled threads are bount to cores sequentially starting at core 0.\n# server-thread-affinity true\n\n# Uncomment the option below to enable Active Active support.  Note that\n# replicas will still sync in the normal way and incorrect ordering when\n# bringing up replicas can result in data loss (the first master will win).\n# active-replica yes\n\n# KeyDB will attempt to balance clients across threads evenly; However, replica clients\n# are usually much more expensive than a normal client, and so KeyDB will try to assign\n# fewer clients to threads with a replica.  The weighting factor below is intented to help tune\n# this behavior.  A replica weighting factor of 2 means we treat a replica as the equivalent\n# of two normal clients.  Adjusting this value may improve performance when replication is\n# used.  The best weighting is workload specific - e.g. read heavy workloads should set\n# this to 1.  Very write heavy workloads may benefit from higher numbers.\n#\n# By default KeyDB sets this to 2.\nreplica-weighting-factor 2\n\n# Should KeyDB make active attempts at balancing clients across threads?  This can impact\n# performance accepting new clients.  By default this is enabled.  If disabled there is still\n# a best effort from the kernel to distribute across threads with SO_REUSEPORT but it will not\n# be as fair.\n#\n# By default this is enabled\n#\nactive-client-balancing yes\n\n# Enable FLASH support (Experimental Feature)\n# storage-provider flash /path/to/flash/db\n\n# Blob support is a way to store very large objects (>200MB) on disk\n# The files are automatically cleaned up when KeyDB exits and are only\n# for temporary use.  This helps reduce memory pressure for very large\n# data items at the cost of some performance.\n#\n# By default this config is disable.  When enabled the disk associated\n# with KeyDB's working directory will be used.  If there is insufficient\n# disk space or any other I/O error KeyDB will instead use memory.\n#\n# blob-support false\n\n# Begin load shedding if we use more than X% CPU relative to the number of server threads\n# E.g. if overload-protect-percent is set to 80 and there are 8 server-threads, then the \n# actual CPU protection will be 8 * 100 * 0.80 = 640% CPU usage.\n#\n# Set to 0 to disable\n# overload-protect-percent 0\n\n# Inform KeyDB of the availability zone if running in a cloud environment.  Currently\n# this is only exposed via the info command for clients to use, but in the future we\n# we may also use this when making decisions for replication.\n#\n# availability-zone \"us-east-1a\""
  },
  {
    "path": "machamp_scripts/Dockerfile",
    "content": "FROM ubuntu:20.04\nSHELL [\"/bin/bash\",\"-c\"]\nRUN groupadd -r keydb && useradd -r -g keydb keydb\n# use gosu for easy step-down from root: https://github.com/tianon/gosu/releases\nENV GOSU_VERSION 1.14\nRUN set -eux; \\\n        savedAptMark=\"$(apt-mark showmanual)\"; \\\n        apt-get update; \\\n        apt-get -o Dpkg::Options::=\"--force-confnew\" install -y --no-install-recommends ca-certificates dirmngr gnupg wget; \\\n        rm -rf /var/lib/apt/lists/*; \\\n        dpkgArch=\"$(dpkg --print-architecture | awk -F- '{ print $NF }')\"; \\\n        wget -O /usr/local/bin/gosu \"https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch\"; \\\n        wget -O /usr/local/bin/gosu.asc \"https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch.asc\"; \\\n        export GNUPGHOME=\"$(mktemp -d)\"; \\\n        gpg --batch --keyserver hkps://keys.openpgp.org --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4; \\\n        gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu; \\\n        gpgconf --kill all; \\\n        rm -rf \"$GNUPGHOME\" /usr/local/bin/gosu.asc; \\\n        apt-mark auto '.*' > /dev/null; \\\n        [ -z \"$savedAptMark\" ] || apt-mark manual $savedAptMark > /dev/null; \\\n        apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \\\n        chmod +x /usr/local/bin/gosu; \\\n        gosu --version; \\\n        gosu nobody true\n# build KeyDB\nARG MAKE_JOBS=\"\"\nARG ENABLE_FLASH=\"\"\nCOPY . /tmp/keydb-internal\nRUN set -eux; \\\n        cd /tmp/keydb-internal; \\\n        savedAptMark=\"$(apt-mark showmanual)\"; \\\n        apt-get update; \\\n        DEBIAN_FRONTEND=noninteractive apt-get -o Dpkg::Options::=\"--force-confnew\" install -qqy --no-install-recommends \\\n                dpkg-dev \\\n                pkg-config \\\n                ca-certificates \\\n                build-essential \\\n                nasm \\\n                autotools-dev \\\n                autoconf \\\n                libjemalloc-dev \\\n                tcl \\\n                tcl-dev \\\n                uuid-dev \\\n                libcurl4-openssl-dev \\\n                libbz2-dev \\\n                libzstd-dev \\\n                liblz4-dev \\\n                libsnappy-dev \\\n                libssl-dev \\\n                git; \\\n        # disable protected mode as it relates to docker\n        grep -E '^ *createBoolConfig[(]\"protected-mode\",.*, *1 *,.*[)],$' ./src/config.cpp; \\\n        sed -ri 's!^( *createBoolConfig[(]\"protected-mode\",.*, *)1( *,.*[)],)$!\\10\\2!' ./src/config.cpp; \\\n        grep -E '^ *createBoolConfig[(]\"protected-mode\",.*, *0 *,.*[)],$' ./src/config.cpp; \\\n        make distclean; \\\n        make -j$([ -z \"$MAKE_JOBS\" ] &&  nproc || echo \"$MAKE_JOBS\") BUILD_TLS=yes NO_LICENSE_CHECK=yes $([ -z \"$ENABLE_FLASH\" ] &&  echo \"\" || echo \"ENABLE_FLASH=$ENABLE_FLASH\"); \\\n        cd src; \\\n        mv modules/keydb_modstatsd/modstatsd.so /usr/local/lib/; \\\n        strip keydb-cli keydb-benchmark keydb-check-rdb keydb-check-aof keydb-diagnostic-tool keydb-sentinel; \\\n        mv keydb-server keydb-cli keydb-benchmark keydb-check-rdb keydb-check-aof keydb-diagnostic-tool keydb-sentinel /usr/local/bin/; \\\n        # clean up unused dependencies\n        echo $savedAptMark; \\\n        apt-mark auto '.*' > /dev/null; \\\n        [ -z \"$savedAptMark\" ] || apt-mark manual $savedAptMark > /dev/null; \\\n        find /usr/local -type f -executable -exec ldd '{}' ';' \\\n               | awk '/=>/ { print $(NF-1) }' \\\n               | sed 's:.*/::' \\\n               | sort -u \\\n               | xargs -r dpkg-query --search \\\n               | cut -d: -f1 \\\n               | sort -u \\\n               | xargs -r apt-mark manual \\\n        ; \\\n        apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \\\n        rm -rf /var/lib/apt/lists/*; \\\n# create working directories and organize files\nRUN \\\n        mkdir /data && chown keydb:keydb /data; \\\n        mkdir /flash && chown keydb:keydb /flash; \\\n        mkdir -p /etc/keydb; \\\n        cp /tmp/keydb-internal/keydb.conf /etc/keydb/; \\\n        sed -i 's/^\\(daemonize .*\\)$/# \\1/' /etc/keydb/keydb.conf; \\\n        sed -i 's/^\\(dir .*\\)$/# \\1\\ndir \\/data/' /etc/keydb/keydb.conf; \\\n        sed -i 's/^\\(logfile .*\\)$/# \\1/' /etc/keydb/keydb.conf; \\\n        sed -i 's/protected-mode yes/protected-mode no/g' /etc/keydb/keydb.conf; \\\n        sed -i 's/^\\(bind .*\\)$/# \\1/' /etc/keydb/keydb.conf; \\\n        echo -e \"\\nloadmodule /usr/local/lib/modstatsd.so\" >> /etc/keydb/keydb.conf; \\\n\tln -s keydb-cli redis-cli; \\\n        cd /etc/keydb; \\\n        ln -s keydb.conf redis.conf; \\\n        rm -rf /tmp/*\n# generate entrypoint script\nRUN set -eux; \\\n        echo '#!/bin/sh' > /usr/local/bin/docker-entrypoint.sh; \\\n        echo 'set -e' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo \"# perpend 'keydb-server' if not provided as first argument\" >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo 'if [ \"${1}\" != \"keydb-server\" ]; then' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo '        set -- keydb-server \"$@\"' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo 'fi' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo \"# allow the container to be started with `--user`\" >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo 'if [ \"$1\" = \"keydb-server\" -a \"$(id -u)\" = \"0\" ]; then' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo \"        find . \\! -user keydb -exec chown keydb '{}' +\" >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo '        exec gosu keydb \"$0\" \"$@\"' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo 'fi' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo 'exec \"$@\"' >> /usr/local/bin/docker-entrypoint.sh; \\\n        chmod +x /usr/local/bin/docker-entrypoint.sh\n# set remaining image properties\nVOLUME /data\nWORKDIR /data\nENV KEYDB_PRO_DIRECTORY=/usr/local/bin/\nENTRYPOINT [\"docker-entrypoint.sh\"]\nEXPOSE 6379\nCMD [\"keydb-server\",\"/etc/keydb/keydb.conf\"]\n"
  },
  {
    "path": "machamp_scripts/build.sh",
    "content": "#!/bin/bash\n\n# make the build\ngit submodule init && git submodule update\nmake BUILD_TLS=yes ENABLE_FLASH=yes -j$(nproc) KEYDB_CFLAGS='-Werror' KEYDB_CXXFLAGS='-Werror'\n\n# gen-cert\n./utils/gen-test-certs.sh"
  },
  {
    "path": "monkey/monkey.py",
    "content": "import random\nimport time\nimport socket\nimport asyncore\nimport threading\nimport argparse\nfrom pprint import pprint\n\n# Globals\nops = {}\nnumclients = 0\nnumkeys = 0\nruntime = 0\nclients = []\n\ndef _buildResp(*args):\n    result = \"*\" + str(len(args)) + \"\\r\\n\"\n    for v in args:\n        result = result + \"$\" + str(len(v)) + \"\\r\\n\"\n        result = result + v + \"\\r\\n\"\n    return result.encode('utf-8')\n\nclass Client(asyncore.dispatcher):\n    def __init__(self, host, port):\n        asyncore.dispatcher.__init__(self)\n        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)\n        self.connect((host, port))\n        self.buf = b''\n        self.inbuf = b''\n        self.callbacks = list()\n        self.client_id = 0\n        self.get_client_id()\n\n    def handle_connect(self):\n        pass\n\n    def handle_read(self):\n        self.inbuf += self.recv(8192)\n        self.parse_response()\n\n    def handle_write(self):\n        sent = self.send(self.buf)\n        self.buf = self.buf[sent:]\n\n    def handle_close(self):\n        self.close()\n\n    def writable(self):\n        return len(self.buf) > 0\n\n    def parse_array(self, startpos):\n        assert(self.inbuf[startpos] == ord('*'))\n        endrange = self.inbuf[startpos+1:].find(ord('\\r')) + 1 + startpos\n        assert(endrange > 0)\n        numargs = int(self.inbuf[startpos+1:endrange])\n        if numargs == -1: # Nil array, used in some returns\n            startpos = endrange + 2\n            return startpos, []\n        assert(numargs > 0)\n        args = list()\n        startpos = endrange + 2 # plus 1 gets us to the '\\n' and the next gets us to the start char\n\n        while len(args) < numargs:\n            # We're parsing entries of the form \"$N\\r\\nnnnnnn\\r\\n\"\n            if startpos >= len(self.inbuf):\n                return # Not the full response\n            if self.inbuf[startpos] == ord('*'):\n                startpos, arr = self.parse_array(startpos)\n                args.append(arr)\n            else:\n                assert(self.inbuf[startpos] == ord('$'))\n                startpos = startpos + 1\n                endrange = self.inbuf[startpos:].find(b'\\r')\n                if endrange < 0:\n                    return\n                endrange += startpos\n                assert(endrange <= len(self.inbuf))\n                length = int(self.inbuf[startpos:endrange])\n                if length < 0:\n                    return\n                startpos = endrange + 2\n                assert((startpos + length) <= len(self.inbuf))\n                assert(self.inbuf[startpos+length] == ord('\\r'))\n                assert(self.inbuf[startpos+length+1] == ord('\\n'))\n                args.append(self.inbuf[startpos:(startpos+length)])\n                startpos += length + 2\n        assert(len(args) == numargs)\n        return startpos, args\n\n    def parse_response(self):\n        if len(self.inbuf) == 0:\n            return\n        \n        while len(self.inbuf) > 0:\n            if self.inbuf[0] == ord('+') or self.inbuf[0] == ord('-') or self.inbuf[0] == ord(':'):\n                # This is a single line response\n                endpos = self.inbuf.find(b'\\n')\n                if endpos < 0:\n                    return  # incomplete response\n                self.callbacks[0](self, self.inbuf[0:endpos-1])\n                self.callbacks.pop(0)\n                self.inbuf = self.inbuf[endpos+1:]\n\n            elif self.inbuf[0] == ord('*'):\n                #RESP response\n                try:\n                    startpos, args = self.parse_array(0)\n                except:\n                    return # Not all data here yet\n                self.callbacks[0](self, args)\n                self.callbacks.pop(0)\n                self.inbuf = self.inbuf[startpos:]\n            else:\n                print(\"ERROR: Unknown response:\")\n                pprint(self.inbuf)\n                assert(False)\n\n\n    def default_result_handler(self, result):\n        pprint(result)\n\n    # Public Methods\n    def set(self, key, val, callback = default_result_handler):\n        self.buf += _buildResp(\"set\", key, val)\n        self.callbacks.append(callback)\n\n    def lpush(self, key, val, callback = default_result_handler):\n        self.buf += _buildResp(\"lpush\", key, val)\n        self.callbacks.append(callback)\n\n    def blpop(self, *keys, timeout=0, callback=default_result_handler):\n        self.buf += _buildResp(\"blpop\", *keys, str(timeout))\n        self.callbacks.append(callback)\n\n    def delete(self, key, callback = default_result_handler):\n        self.buf += _buildResp(\"del\", key)\n        self.callbacks.append(callback)\n\n    def unblock(self, client_id, callback=default_result_handler):\n        self.buf += _buildResp(\"client\", \"unblock\", str(client_id))\n        self.callbacks.append(callback)\n\n    def scan(self, iter, match=None, count=None, callback = default_result_handler):\n        args = [\"scan\", str(iter)]\n        if match != None:\n            args.append(\"MATCH\")\n            args.append(match)\n        if count != None:\n            args.append(\"COUNT\")\n            args.append(str(count))\n        self.buf += _buildResp(*args)\n        self.callbacks.append(callback)\n\n    def get_client_id(self):\n        self.buf += _buildResp(\"client\", \"id\")\n        self.callbacks.append(self.store_client_id)\n\n    def store_client_id(self, c, resp):\n        assert(resp[0] == ord(':'))\n        self.client_id = int(resp[1:])\n        assert(self.client_id == c.client_id)\n\n    def get(self, key, callback = None):\n       return \n\ndef getrandomkey():\n    return str(random.randrange(0, numkeys))\n\ndef handle_lpush_response(c, resp=None):\n    global ops\n    if resp != None:\n        ops['lpush'] += 1\n        assert(resp[0] == ord(':'))\n    c.lpush(\"list_\" + getrandomkey(), 'bardsklfjkldsjfdlsjflksdfjklsdjflksd kldsjflksd jlkdsjf lksdjklds jrfklsdjfklsdjfkl', handle_lpush_response)\n\ndef handle_blpop_response(c, resp=None):\n    global ops\n    if resp != None:\n        ops['blpop'] += 1\n    c.blpop(\"list_\" + getrandomkey(), callback=handle_blpop_response)\n\ndef handle_set_response(c, resp=None):\n    global ops\n    if resp != None:\n        ops['set'] += 1\n        assert(resp[0] == ord('+'))\n    c.set(\"str_\" + getrandomkey(), 'bardsklfjkldsjfdlsjflksdfjklsdjflksd kldsjflksd jlkdsjf lksdjklds jrfklsdjfklsdjfkl', handle_set_response)\n\ndef handle_del_response(c, resp=None):\n    global ops\n    if resp != None:\n        ops['del'] += 1\n    c.delete(\"list_\" + getrandomkey(), handle_del_response)\n\ndef scan_callback(c, resp=None):\n    global ops\n    nextstart = int(resp[0])\n    c.scan(nextstart, count=500, callback=scan_callback)\n    ops['scan'] += 1\n\ndef clear_ops():\n    global ops\n    ops = {'lpush': 0, 'blpop': 0, 'del': 0, 'scan': 0, 'set': 0, 'get': 0}\n\ndef stats_thread():\n    global ops\n    global runtime\n    i = 0\n    while i < runtime or not runtime:\n        time.sleep(1)\n        print(\"Ops per second: \" + str({k:v for (k,v) in ops.items() if v}))\n        clear_ops()\n        i += 1\n    asyncore.close_all()\n\ndef flush_db_sync():\n    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n    server.connect(('127.0.0.1', 6379))\n    server.send(_buildResp(\"flushdb\"))\n    resp = server.recv(8192)\n    assert(resp[:3] == \"+OK\".encode('utf-8'))\n\ndef init_blocking():\n    global clients\n    if numkeys > 100 * numclients:\n        print(\"WARNING: High ratio of keys to clients. Most lpushes will not be popped and unblocking will take a long time!\")\n    for i in range(numclients):\n        clients.append(Client('127.0.0.1', 6379))\n        if i % 2:\n            handle_blpop_response(clients[-1])\n        else:\n            handle_lpush_response(clients[-1])\n\ndef init_lpush():\n    global clients\n    for i in range(numclients):\n        clients.append(Client('127.0.0.1', 6379))\n        for i in range (10):\n            handle_lpush_response(clients[-1])\n        #handle_set_response(clients[-1], None)\n\n    scan_client = Client('127.0.0.1', 6379)\n    scan_client.scan(0, count=500, callback=scan_callback)\n\n    del_client = Client('127.0.0.1', 6379)\n    handle_del_response(del_client)\n\ndef main(test, flush):\n    clear_ops()\n\n    if flush:\n        flush_db_sync()\n\n    try:\n        globals()[f\"init_{test}\"]()\n    except KeyError:\n        print(f\"Test \\\"{test}\\\" not found. Exiting...\")\n        exit()\n    except ConnectionRefusedError:\n        print(\"Could not connect to server. Is it running?\")\n        print(\"Exiting...\")\n        exit()\n\n    threading.Thread(target=stats_thread).start()\n    asyncore.loop()\n    print(\"Done.\")\n\nparser = argparse.ArgumentParser(description=\"Test use cases for KeyDB.\")\nparser.add_argument('test', choices=[x[5:] for x in filter(lambda name: name.startswith(\"init_\"), globals().keys())], help=\"which test to run\")\nparser.add_argument('-c', '--clients', type=int, default=50, help=\"number of running clients to use\")\nparser.add_argument('-k', '--keys', type=int, default=100000, help=\"number of keys to choose from for random tests\")\nparser.add_argument('-t', '--runtime', type=int, default=0, help=\"how long to run the test for (default: 0 for infinite)\")\nparser.add_argument('-f', '--flush', action=\"store_true\", help=\"flush the db before running the test\")\n\nif __name__ == \"__main__\":\n    try:\n        args = parser.parse_args()\n    except:\n        exit()\n    numclients = args.clients\n    numkeys = args.keys\n    runtime = args.runtime\n    main(args.test, args.flush)\n"
  },
  {
    "path": "pkg/README.md",
    "content": "### KeyDB DEB Package Source Builds\n\nThis directory contains scripts and components needed to generate debian packages on different distributions/architectures from source\n\nThe 'debian' directory contains debian source code for bionic, buster and later distributions as it uses functions only available with debhelper11+. 'debian_dh9' is used for xenial, stretch and earlier distributions using debhelper9.\n\nYou will need to install pbuilder `sudo apt install pbuilder` along with other distribution specific dependencies\n\nGenerate deb packages with the following script command run from this directory:\n\n```\n$ ./deb-buildsource.sh\n```\n\nThis generates a directory structure, .dsc file, original.tar.gz, and new changelog for the distribution and architecture installed.\n\nWhen complete the produced debian packages will be located in deb_files_generated directory.\n\nArguments for the deb-buildsource.sh script that can be passed are either 'None' or '\"your custom comments\"'. 'None' assumes a permanent changelog entry has been made listing the correct build for the version. If you quote and enter your own comment string, it will be appended to the changelog for that deb package. If no argument is passed a default comment is generated saying that this deb package was generated by this script.\n\nThis script has been tested on xenial/bionic/focal/stretch/buster/bullseye\n"
  },
  {
    "path": "pkg/deb/README.md",
    "content": "### KeyDB DEB Package Source Builds\n\nThis directory contains scripts and components needed to generate debian packages on different distributions/architectures using source\n\nYou will need to install pbuilder `sudo apt install pbuilder`\n\nGenerate deb packages with the following script command run from this directory:\n```\n$ ./deb-buildsource.sh\n```\nThis generates a directory structure, .dsc file, original.tar.gz, .changes files and new changelog for the distribution and architecture installed.\n\nWhen complete the produced debian packages will be located in deb_files_generated directory.\n\nArguments for the deb-buildsource.sh script that can be passed are either 'None' or '\"your custom comments\"'. 'None' assumes a permanent changelog entry has been made listing the correct build for the version. If you quote and enter your own comment string, it will be appended to the changelog for that deb package. If no argument is passed a default comment is generated saying that this deb package was generated by this script.\n\nThis script has been tested on xenial/bionic/focal/stretch/buster/bullseye\n"
  },
  {
    "path": "pkg/deb/conf/keydb.conf",
    "content": "# KeyDB configuration file example.\n#\n# Note that in order to read the configuration file, KeyDB must be\n# started with the file path as first argument:\n#\n# ./keydb-server /path/to/keydb.conf\n\n# Note on units: when memory size is needed, it is possible to specify\n# it in the usual form of 1k 5GB 4M and so forth:\n#\n# 1k => 1000 bytes\n# 1kb => 1024 bytes\n# 1m => 1000000 bytes\n# 1mb => 1024*1024 bytes\n# 1g => 1000000000 bytes\n# 1gb => 1024*1024*1024 bytes\n#\n# units are case insensitive so 1GB 1Gb 1gB are all the same.\n\n################################## INCLUDES ###################################\n\n# Include one or more other config files here.  This is useful if you\n# have a standard template that goes to all KeyDB servers but also need\n# to customize a few per-server settings.  Include files can include\n# other files, so use this wisely.\n#\n# Note that option \"include\" won't be rewritten by command \"CONFIG REWRITE\"\n# from admin or KeyDB Sentinel. Since KeyDB always uses the last processed\n# line as value of a configuration directive, you'd better put includes\n# at the beginning of this file to avoid overwriting config change at runtime.\n#\n# If instead you are interested in using includes to override configuration\n# options, it is better to use include as the last line.\n#\n# Included paths may contain wildcards. All files matching the wildcards will\n# be included in alphabetical order.\n# Note that if an include path contains a wildcards but no files match it when\n# the server is started, the include statement will be ignored and no error will\n# be emitted.  It is safe, therefore, to include wildcard files from empty\n# directories.\n#\n# include /path/to/local.conf\n# include /path/to/other.conf\n# include /path/to/fragments/*.conf\n#\n\n################################## MODULES #####################################\n\n# Load modules at startup. If the server is not able to load modules\n# it will abort. It is possible to use multiple loadmodule directives.\n#\n# loadmodule /path/to/my_module.so\n# loadmodule /path/to/other_module.so\n\n################################## NETWORK #####################################\n\n# By default, if no \"bind\" configuration directive is specified, KeyDB listens\n# for connections from all available network interfaces on the host machine.\n# It is possible to listen to just one or multiple selected interfaces using\n# the \"bind\" configuration directive, followed by one or more IP addresses.\n# Each address can be prefixed by \"-\", which means that redis will not fail to\n# start if the address is not available. Being not available only refers to\n# addresses that does not correspond to any network interfece. Addresses that\n# are already in use will always fail, and unsupported protocols will always BE\n# silently skipped.\n#\n# Examples:\n#\n# bind 192.168.1.100 10.0.0.1     # listens on two specific IPv4 addresses\n# bind 127.0.0.1 ::1              # listens on loopback IPv4 and IPv6\n# bind * -::*                     # like the default, all available interfaces\n#\n# ~~~ WARNING ~~~ If the computer running KeyDB is directly exposed to the\n# internet, binding to all the interfaces is dangerous and will expose the\n# instance to everybody on the internet. So by default we uncomment the\n# following bind directive, that will force KeyDB to listen only on the\n# IPv4 and IPv6 (if available) loopback interface addresses (this means KeyDB will only be able to\n# accept client connections from the same host that it is running on).\n#\n# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES\n# JUST COMMENT OUT THE FOLLOWING LINE.\n#\n# You will also need to set a password unless you explicitly disable protected\n# mode.\n# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nbind 127.0.0.1 -::1\n\n# Protected mode is a layer of security protection, in order to avoid that\n# KeyDB instances left open on the internet are accessed and exploited.\n#\n# When protected mode is on and if:\n#\n# 1) The server is not binding explicitly to a set of addresses using the\n#    \"bind\" directive.\n# 2) No password is configured.\n#\n# The server only accepts connections from clients connecting from the\n# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain\n# sockets.\n#\n# By default protected mode is enabled. You should disable it only if\n# you are sure you want clients from other hosts to connect to KeyDB\n# even if no authentication is configured, nor a specific set of interfaces\n# are explicitly listed using the \"bind\" directive.\nprotected-mode yes\n\n# Accept connections on the specified port, default is 6379 (IANA #815344).\n# If port 0 is specified KeyDB will not listen on a TCP socket.\nport 6379\n\n# TCP listen() backlog.\n#\n# In high requests-per-second environments you need a high backlog in order\n# to avoid slow clients connection issues. Note that the Linux kernel\n# will silently truncate it to the value of /proc/sys/net/core/somaxconn so\n# make sure to raise both the value of somaxconn and tcp_max_syn_backlog\n# in order to get the desired effect.\ntcp-backlog 511\n\n# Unix socket.\n#\n# Specify the path for the Unix socket that will be used to listen for\n# incoming connections. There is no default, so KeyDB will not listen\n# on a unix socket when not specified.\n#\n# unixsocket /tmp/keydb.sock\n# unixsocketperm 700\n\n# Close the connection after a client is idle for N seconds (0 to disable)\ntimeout 0\n\n# TCP keepalive.\n#\n# If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence\n# of communication. This is useful for two reasons:\n#\n# 1) Detect dead peers.\n# 2) Force network equipment in the middle to consider the connection to be\n#    alive.\n#\n# On Linux, the specified value (in seconds) is the period used to send ACKs.\n# Note that to close the connection the double of the time is needed.\n# On other kernels the period depends on the kernel configuration.\n#\n# A reasonable value for this option is 300 seconds, which is the new\n# KeyDB default starting with KeyDB 3.2.1.\ntcp-keepalive 300\n\n################################# TLS/SSL #####################################\n\n# By default, TLS/SSL is disabled. To enable it, the \"tls-port\" configuration\n# directive can be used to define TLS-listening ports. To enable TLS on the\n# default port, use:\n#\n# port 0\n# tls-port 6379\n\n# Configure a X.509 certificate and private key to use for authenticating the\n# server to connected clients, masters or cluster peers.  These files should be\n# PEM formatted.\n#\n# tls-cert-file keydb.crt \n# tls-key-file keydb.key\n#\n# If the key file is encrypted using a passphrase, it can be included here\n# as well.\n#\n# tls-key-file-pass secret\n\n# Normally KeyDB uses the same certificate for both server functions (accepting\n# connections) and client functions (replicating from a master, establishing\n# cluster bus connections, etc.).\n#\n# Sometimes certificates are issued with attributes that designate them as\n# client-only or server-only certificates. In that case it may be desired to use\n# different certificates for incoming (server) and outgoing (client)\n# connections. To do that, use the following directives:\n#\n# tls-client-cert-file client.crt\n# tls-client-key-file client.key\n#\n# If the key file is encrypted using a passphrase, it can be included here\n# as well.\n#\n# tls-client-key-file-pass secret\n\n# Configure a DH parameters file to enable Diffie-Hellman (DH) key exchange:\n#\n# tls-dh-params-file keydb.dh\n\n# Configure a CA certificate(s) bundle or directory to authenticate TLS/SSL\n# clients and peers. KeyDB requires an explicit configuration of at least one\n# of these, and will not implicitly use the system wide configuration.\n#\n# tls-ca-cert-file ca.crt\n# tls-ca-cert-dir /etc/ssl/certs\n\n# By default, clients (including replica servers) on a TLS port are required\n# to authenticate using valid client side certificates.\n#\n# If \"no\" is specified, client certificates are not required and not accepted.\n# If \"optional\" is specified, client certificates are accepted and must be\n# valid if provided, but are not required.\n#\n# tls-auth-clients no\n# tls-auth-clients optional\n\n# By default, a KeyDB replica does not attempt to establish a TLS connection\n# with its master.\n#\n# Use the following directive to enable TLS on replication links.\n#\n# tls-replication yes\n\n# By default, the KeyDB Cluster bus uses a plain TCP connection. To enable\n# TLS for the bus protocol, use the following directive:\n#\n# tls-cluster yes\n\n# Explicitly specify TLS versions to support. Allowed values are case insensitive\n# and include \"TLSv1\", \"TLSv1.1\", \"TLSv1.2\", \"TLSv1.3\" (OpenSSL >= 1.1.1) or\n# any combination. To enable only TLSv1.2 and TLSv1.3, use:\n#\n# tls-protocols \"TLSv1.2 TLSv1.3\"\n\n# Configure allowed ciphers.  See the ciphers(1ssl) manpage for more information\n# about the syntax of this string.\n#\n# Note: this configuration applies only to <= TLSv1.2.\n#\n# tls-ciphers DEFAULT:!MEDIUM\n\n# Configure allowed TLSv1.3 ciphersuites.  See the ciphers(1ssl) manpage for more\n# information about the syntax of this string, and specifically for TLSv1.3\n# ciphersuites.\n#\n# tls-ciphersuites TLS_CHACHA20_POLY1305_SHA256\n\n# When choosing a cipher, use the server's preference instead of the client\n# preference. By default, the server follows the client's preference.\n#\n# tls-prefer-server-ciphers yes\n\n# By default, TLS session caching is enabled to allow faster and less expensive\n# reconnections by clients that support it. Use the following directive to disable\n# caching.\n#\n# tls-session-caching no\n\n# Change the default number of TLS sessions cached. A zero value sets the cache\n# to unlimited size. The default size is 20480.\n#\n# tls-session-cache-size 5000\n\n# Change the default timeout of cached TLS sessions. The default timeout is 300\n# seconds.\n#\n# tls-session-cache-timeout 60\n\n# Allow the server to monitor the filesystem and rotate out TLS certificates if \n# they change on disk, defaults to no.\n# \n# tls-rotation no\n\n# Setup a allowlist of allowed Common Names (CNs)/Subject Alternative Names (SANs)\n# that are allowed to connect to this server. This includes both normal clients as\n# well as other servers connected for replication/clustering purposes. If nothing is\n# specified, then no allowlist is used and all certificates are accepted. \n# Supports IPv4, DNS, RFC822, and URI SAN types.\n# You can put multiple names on one line as follows:\n#\n# tls-allowlist <dns1> <dns2> <dns3> ...\n# \n#\n# This configuration also allows for wildcard characters with glob style formatting\n# i.e. \"*.com\" would allow all clients to connect with a CN/SAN that ends with \".com\"\n\n################################# GENERAL #####################################\n\n# By default KeyDB does not run as a daemon. Use 'yes' if you need it.\n# Note that KeyDB will write a pid file in /var/run/keydb.pid when daemonized.\ndaemonize yes\n\n# If you run KeyDB from upstart or systemd, KeyDB can interact with your\n# supervision tree. Options:\n#   supervised no      - no supervision interaction\n#   supervised upstart - signal upstart by putting KeyDB into SIGSTOP mode\n#                        requires \"expect stop\" in your upstart job config\n#   supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET\n#   supervised auto    - detect upstart or systemd method based on\n#                        UPSTART_JOB or NOTIFY_SOCKET environment variables\n# Note: these supervision methods only signal \"process is ready.\"\n#       They do not enable continuous pings back to your supervisor.\n# supervised no\n\n# If a pid file is specified, KeyDB writes it where specified at startup\n# and removes it at exit.\n#\n# When the server runs non daemonized, no pid file is created if none is\n# specified in the configuration. When the server is daemonized, the pid file\n# is used even if not specified, defaulting to \"/var/run/keydb.pid\".\n#\n# Creating a pid file is best effort: if KeyDB is not able to create it\n# nothing bad happens, the server will start and run normally.\npidfile /var/run/keydb/keydb-server.pid\n\n# Specify the server verbosity level.\n# This can be one of:\n# debug (a lot of information, useful for development/testing)\n# verbose (many rarely useful info, but not a mess like the debug level)\n# notice (moderately verbose, what you want in production probably)\n# warning (only very important / critical messages are logged)\nloglevel notice\n\n# Specify the log file name. Also the empty string can be used to force\n# KeyDB to log on the standard output. Note that if you use standard\n# output for logging but daemonize, logs will be sent to /dev/null\nlogfile /var/log/keydb/keydb-server.log\n\n# To enable logging to the system logger, just set 'syslog-enabled' to yes,\n# and optionally update the other syslog parameters to suit your needs.\n# syslog-enabled no\n\n# Specify the syslog identity.\n# syslog-ident keydb\n\n# Specify the syslog facility. Must be USER or between LOCAL0-LOCAL7.\n# syslog-facility local0\n\n# To disable the built in crash log, which will possibly produce cleaner core\n# dumps when they are needed, uncomment the following:\n#\n# crash-log-enabled no\n\n# To disable the fast memory check that's run as part of the crash log, which\n# will possibly let keydb terminate sooner, uncomment the following:\n#\n# crash-memcheck-enabled no\n\n# Set the number of databases. The default database is DB 0, you can select\n# a different one on a per-connection basis using SELECT <dbid> where\n# dbid is a number between 0 and 'databases'-1\ndatabases 16\n\n# By default KeyDB shows an ASCII art logo only when started to log to the\n# standard output and if the standard output is a TTY. Basically this means\n# that normally a logo is displayed only in interactive sessions.\n#\n# However it is possible to force the pre-4.0 behavior and always show a\n# ASCII art logo in startup logs by setting the following option to yes.\nalways-show-logo yes\n\n# By default, KeyDB modifies the process title (as seen in 'top' and 'ps') to\n# provide some runtime information. It is possible to disable this and leave\n# the process name as executed by setting the following to no.\nset-proc-title yes\n\n# Retrieving \"message of today\" using CURL requests.\n#enable-motd yes\n\n# When changing the process title, KeyDB uses the following template to construct\n# the modified title.\n#\n# Template variables are specified in curly brackets. The following variables are\n# supported:\n#\n# {title}           Name of process as executed if parent, or type of child process.\n# {listen-addr}     Bind address or '*' followed by TCP or TLS port listening on, or\n#                   Unix socket if only that's available.\n# {server-mode}     Special mode, i.e. \"[sentinel]\" or \"[cluster]\".\n# {port}            TCP port listening on, or 0.\n# {tls-port}        TLS port listening on, or 0.\n# {unixsocket}      Unix domain socket listening on, or \"\".\n# {config-file}     Name of configuration file used.\n#\nproc-title-template \"{title} {listen-addr} {server-mode}\"\n\n################################ SNAPSHOTTING  ################################\n#\n# Save the DB on disk:\n#\n#   save <seconds> <changes>\n#\n#   Will save the DB if both the given number of seconds and the given\n#   number of write operations against the DB occurred.\n#\n#   In the example below the behavior will be to save:\n#   after 900 sec (15 min) if at least 1 key changed\n#   after 300 sec (5 min) if at least 10 keys changed\n#   after 60 sec if at least 10000 keys changed\n#\n#   It is also possible to remove all the previously configured save\n#   points by adding a save directive with a single empty string argument\n#   like in the following example:\n#\n#   save \"\"\n\nsave 900 1\nsave 300 10\nsave 60 10000\n\n# By default KeyDB will stop accepting writes if RDB snapshots are enabled\n# (at least one save point) and the latest background save failed.\n# This will make the user aware (in a hard way) that data is not persisting\n# on disk properly, otherwise chances are that no one will notice and some\n# disaster will happen.\n#\n# If the background saving process will start working again KeyDB will\n# automatically allow writes again.\n#\n# However if you have setup your proper monitoring of the KeyDB server\n# and persistence, you may want to disable this feature so that KeyDB will\n# continue to work as usual even if there are problems with disk,\n# permissions, and so forth.\nstop-writes-on-bgsave-error yes\n\n# Compress string objects using LZF when dump .rdb databases?\n# By default compression is enabled as it's almost always a win.\n# If you want to save some CPU in the saving child set it to 'no' but\n# the dataset will likely be bigger if you have compressible values or keys.\nrdbcompression yes\n\n# Since version 5 of RDB a CRC64 checksum is placed at the end of the file.\n# This makes the format more resistant to corruption but there is a performance\n# hit to pay (around 10%) when saving and loading RDB files, so you can disable it\n# for maximum performances.\n#\n# RDB files created with checksum disabled have a checksum of zero that will\n# tell the loading code to skip the check.\nrdbchecksum yes\n\n# Enables or disables full sanitation checks for ziplist and listpack etc when\n# loading an RDB or RESTORE payload. This reduces the chances of a assertion or\n# crash later on while processing commands.\n# Options:\n#   no         - Never perform full sanitation\n#   yes        - Always perform full sanitation\n#   clients    - Perform full sanitation only for user connections.\n#                Excludes: RDB files, RESTORE commands received from the master\n#                connection, and client connections which have the\n#                skip-sanitize-payload ACL flag.\n# The default should be 'clients' but since it currently affects cluster\n# resharding via MIGRATE, it is temporarily set to 'no' by default.\n#\n# sanitize-dump-payload no\n\n# The filename where to dump the DB\ndbfilename dump.rdb\n\n# Remove RDB files used by replication in instances without persistence\n# enabled. By default this option is disabled, however there are environments\n# where for regulations or other security concerns, RDB files persisted on\n# disk by masters in order to feed replicas, or stored on disk by replicas\n# in order to load them for the initial synchronization, should be deleted\n# ASAP. Note that this option ONLY WORKS in instances that have both AOF\n# and RDB persistence disabled, otherwise is completely ignored.\n#\n# An alternative (and sometimes better) way to obtain the same effect is\n# to use diskless replication on both master and replicas instances. However\n# in the case of replicas, diskless is not always an option.\nrdb-del-sync-files no\n\n# The working directory.\n#\n# The DB will be written inside this directory, with the filename specified\n# above using the 'dbfilename' configuration directive.\n#\n# The Append Only File will also be created inside this directory.\n#\n# Note that you must specify a directory here, not a file name.\ndir /var/lib/keydb\n\n################################# REPLICATION #################################\n\n# Master-Replica replication. Use replicaof to make a KeyDB instance a copy of\n# another KeyDB server. A few things to understand ASAP about KeyDB replication.\n#\n#   +------------------+      +---------------+\n#   |      Master      | ---> |    Replica    |\n#   | (receive writes) |      |  (exact copy) |\n#   +------------------+      +---------------+\n#\n# 1) KeyDB replication is asynchronous, but you can configure a master to\n#    stop accepting writes if it appears to be not connected with at least\n#    a given number of replicas.\n# 2) KeyDB replicas are able to perform a partial resynchronization with the\n#    master if the replication link is lost for a relatively small amount of\n#    time. You may want to configure the replication backlog size (see the next\n#    sections of this file) with a sensible value depending on your needs.\n# 3) Replication is automatic and does not need user intervention. After a\n#    network partition replicas automatically try to reconnect to masters\n#    and resynchronize with them.\n#\n# replicaof <masterip> <masterport>\n\n# If the master is password protected (using the \"requirepass\" configuration\n# directive below) it is possible to tell the replica to authenticate before\n# starting the replication synchronization process, otherwise the master will\n# refuse the replica request.\n#\n# masterauth <master-password>\n#\n# However this is not enough if you are using KeyDB ACLs (for KeyDB version\n# 6 or greater), and the default user is not capable of running the PSYNC\n# command and/or other commands needed for replication (gathered in the\n# @replication group). In this case it's better to configure a special user to\n# use with replication, and specify the masteruser configuration as such:\n#\n# masteruser <username>\n#\n# When masteruser is specified, the replica will authenticate against its\n# master using the new AUTH form: AUTH <username> <password>.\n\n# When a replica loses its connection with the master, or when the replication\n# is still in progress, the replica can act in two different ways:\n#\n# 1) if replica-serve-stale-data is set to 'yes' (the default) the replica will\n#    still reply to client requests, possibly with out of date data, or the\n#    data set may just be empty if this is the first synchronization.\n#\n# 2) If replica-serve-stale-data is set to 'no' the replica will reply with\n#    an error \"SYNC with master in progress\" to all commands except:\n#    INFO, REPLICAOF, AUTH, PING, SHUTDOWN, REPLCONF, ROLE, CONFIG, SUBSCRIBE,\n#    UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, PUBLISH, PUBSUB, COMMAND, POST,\n#    HOST and LATENCY.\n#\nreplica-serve-stale-data yes\n\n# Active Replicas will allow read only data access while loading remote RDBs\n# provided they are permitted to serve stale data.  As an option you may also\n# permit them to accept write commands.  This is an EXPERIMENTAL feature and\n# may result in commands not being fully synchronized\n#\n# allow-write-during-load no\n\n# You can modify the number of masters necessary to form a replica quorum when\n# multi-master is enabled and replica-serve-stale-data is \"no\".  By default \n# this is set to -1 which implies the number of known masters (e.g. those\n# you added with replicaof)\n#\n# replica-quorum -1\n\n# You can configure a replica instance to accept writes or not. Writing against\n# a replica instance may be useful to store some ephemeral data (because data\n# written on a replica will be easily deleted after resync with the master) but\n# may also cause problems if clients are writing to it because of a\n# misconfiguration.\n#\n# Since KeyDB 2.6 by default replicas are read-only.\n#\n# Note: read only replicas are not designed to be exposed to untrusted clients\n# on the internet. It's just a protection layer against misuse of the instance.\n# Still a read only replica exports by default all the administrative commands\n# such as CONFIG, DEBUG, and so forth. To a limited extent you can improve\n# security of read only replicas using 'rename-command' to shadow all the\n# administrative / dangerous commands.\nreplica-read-only yes\n\n# Replication SYNC strategy: disk or socket.\n#\n# New replicas and reconnecting replicas that are not able to continue the\n# replication process just receiving differences, need to do what is called a\n# \"full synchronization\". An RDB file is transmitted from the master to the\n# replicas.\n#\n# The transmission can happen in two different ways:\n#\n# 1) Disk-backed: The KeyDB master creates a new process that writes the RDB\n#                 file on disk. Later the file is transferred by the parent\n#                 process to the replicas incrementally.\n# 2) Diskless: The KeyDB master creates a new process that directly writes the\n#              RDB file to replica sockets, without touching the disk at all.\n#\n# With disk-backed replication, while the RDB file is generated, more replicas\n# can be queued and served with the RDB file as soon as the current child\n# producing the RDB file finishes its work. With diskless replication instead\n# once the transfer starts, new replicas arriving will be queued and a new\n# transfer will start when the current one terminates.\n#\n# When diskless replication is used, the master waits a configurable amount of\n# time (in seconds) before starting the transfer in the hope that multiple\n# replicas will arrive and the transfer can be parallelized.\n#\n# With slow disks and fast (large bandwidth) networks, diskless replication\n# works better.\nrepl-diskless-sync no\n\n# When diskless replication is enabled, it is possible to configure the delay\n# the server waits in order to spawn the child that transfers the RDB via socket\n# to the replicas.\n#\n# This is important since once the transfer starts, it is not possible to serve\n# new replicas arriving, that will be queued for the next RDB transfer, so the\n# server waits a delay in order to let more replicas arrive.\n#\n# The delay is specified in seconds, and by default is 5 seconds. To disable\n# it entirely just set it to 0 seconds and the transfer will start ASAP.\nrepl-diskless-sync-delay 5\n\n# -----------------------------------------------------------------------------\n# WARNING: RDB diskless load is experimental. Since in this setup the replica\n# does not immediately store an RDB on disk, it may cause data loss during\n# failovers. RDB diskless load + KeyDB modules not handling I/O reads may also\n# cause KeyDB to abort in case of I/O errors during the initial synchronization\n# stage with the master. Use only if your do what you are doing.\n# -----------------------------------------------------------------------------\n#\n# Replica can load the RDB it reads from the replication link directly from the\n# socket, or store the RDB to a file and read that file after it was completely\n# received from the master.\n#\n# In many cases the disk is slower than the network, and storing and loading\n# the RDB file may increase replication time (and even increase the master's\n# Copy on Write memory and salve buffers).\n# However, parsing the RDB file directly from the socket may mean that we have\n# to flush the contents of the current database before the full rdb was\n# received. For this reason we have the following options:\n#\n# \"disabled\"    - Don't use diskless load (store the rdb file to the disk first)\n# \"on-empty-db\" - Use diskless load only when it is completely safe.\n# \"swapdb\"      - Keep a copy of the current db contents in RAM while parsing\n#                 the data directly from the socket. note that this requires\n#                 sufficient memory, if you don't have it, you risk an OOM kill.\nrepl-diskless-load disabled\n\n# Replicas send PINGs to server in a predefined interval. It's possible to\n# change this interval with the repl_ping_replica_period option. The default\n# value is 10 seconds.\n#\n# repl-ping-replica-period 10\n\n# The following option sets the replication timeout for:\n#\n# 1) Bulk transfer I/O during SYNC, from the point of view of replica.\n# 2) Master timeout from the point of view of replicas (data, pings).\n# 3) Replica timeout from the point of view of masters (REPLCONF ACK pings).\n#\n# It is important to make sure that this value is greater than the value\n# specified for repl-ping-replica-period otherwise a timeout will be detected\n# every time there is low traffic between the master and the replica. The default\n# value is 60 seconds.\n#\n# repl-timeout 60\n\n# Disable TCP_NODELAY on the replica socket after SYNC?\n#\n# If you select \"yes\" KeyDB will use a smaller number of TCP packets and\n# less bandwidth to send data to replicas. But this can add a delay for\n# the data to appear on the replica side, up to 40 milliseconds with\n# Linux kernels using a default configuration.\n#\n# If you select \"no\" the delay for data to appear on the replica side will\n# be reduced but more bandwidth will be used for replication.\n#\n# By default we optimize for low latency, but in very high traffic conditions\n# or when the master and replicas are many hops away, turning this to \"yes\" may\n# be a good idea.\nrepl-disable-tcp-nodelay no\n\n# Set the replication backlog size. The backlog is a buffer that accumulates\n# replica data when replicas are disconnected for some time, so that when a\n# replica wants to reconnect again, often a full resync is not needed, but a\n# partial resync is enough, just passing the portion of data the replica\n# missed while disconnected.\n#\n# The bigger the replication backlog, the longer the replica can endure the\n# disconnect and later be able to perform a partial resynchronization.\n#\n# The backlog is only allocated if there is at least one replica connected.\n#\n# repl-backlog-size 1mb\n\n# After a master has no connected replicas for some time, the backlog will be\n# freed. The following option configures the amount of seconds that need to\n# elapse, starting from the time the last replica disconnected, for the backlog\n# buffer to be freed.\n#\n# Note that replicas never free the backlog for timeout, since they may be\n# promoted to masters later, and should be able to correctly \"partially\n# resynchronize\" with other replicas: hence they should always accumulate backlog.\n#\n# A value of 0 means to never release the backlog.\n#\n# repl-backlog-ttl 3600\n\n# The replica priority is an integer number published by KeyDB in the INFO\n# output. It is used by KeyDB Sentinel in order to select a replica to promote\n# into a master if the master is no longer working correctly.\n#\n# A replica with a low priority number is considered better for promotion, so\n# for instance if there are three replicas with priority 10, 100, 25 Sentinel\n# will pick the one with priority 10, that is the lowest.\n#\n# However a special priority of 0 marks the replica as not able to perform the\n# role of master, so a replica with priority of 0 will never be selected by\n# KeyDB Sentinel for promotion.\n#\n# By default the priority is 100.\nreplica-priority 100\n\n# -----------------------------------------------------------------------------\n# By default, KeyDB Sentinel includes all replicas in its reports. A replica\n# can be excluded from KeyDB Sentinel's announcements. An unannounced replica\n# will be ignored by the 'sentinel replicas <master>' command and won't be\n# exposed to KeyDB Sentinel's clients.\n#\n# This option does not change the behavior of replica-priority. Even with\n# replica-announced set to 'no', the replica can be promoted to master. To\n# prevent this behavior, set replica-priority to 0.\n#\n# replica-announced yes\n\n# It is possible for a master to stop accepting writes if there are less than\n# N replicas connected, having a lag less or equal than M seconds.\n#\n# The N replicas need to be in \"online\" state.\n#\n# The lag in seconds, that must be <= the specified value, is calculated from\n# the last ping received from the replica, that is usually sent every second.\n#\n# This option does not GUARANTEE that N replicas will accept the write, but\n# will limit the window of exposure for lost writes in case not enough replicas\n# are available, to the specified number of seconds.\n#\n# For example to require at least 3 replicas with a lag <= 10 seconds use:\n#\n# min-replicas-to-write 3\n# min-replicas-max-lag 10\n#\n# Setting one or the other to 0 disables the feature.\n#\n# By default min-replicas-to-write is set to 0 (feature disabled) and\n# min-replicas-max-lag is set to 10.\n\n# A KeyDB master is able to list the address and port of the attached\n# replicas in different ways. For example the \"INFO replication\" section\n# offers this information, which is used, among other tools, by\n# KeyDB Sentinel in order to discover replica instances.\n# Another place where this info is available is in the output of the\n# \"ROLE\" command of a master.\n#\n# The listed IP address and port normally reported by a replica is\n# obtained in the following way:\n#\n#   IP: The address is auto detected by checking the peer address\n#   of the socket used by the replica to connect with the master.\n#\n#   Port: The port is communicated by the replica during the replication\n#   handshake, and is normally the port that the replica is using to\n#   listen for connections.\n#\n# However when port forwarding or Network Address Translation (NAT) is\n# used, the replica may actually be reachable via different IP and port\n# pairs. The following two options can be used by a replica in order to\n# report to its master a specific set of IP and port, so that both INFO\n# and ROLE will report those values.\n#\n# There is no need to use both the options if you need to override just\n# the port or the IP address.\n#\n# replica-announce-ip 5.5.5.5\n# replica-announce-port 1234\n\n############################### KEYS TRACKING #################################\n\n# KeyDB implements server assisted support for client side caching of values.\n# This is implemented using an invalidation table that remembers, using\n# 16 millions of slots, what clients may have certain subsets of keys. In turn\n# this is used in order to send invalidation messages to clients. Please\n# check this page to understand more about the feature:\n#\n#   https://redis.io/topics/client-side-caching\n#\n# When tracking is enabled for a client, all the read only queries are assumed\n# to be cached: this will force KeyDB to store information in the invalidation\n# table. When keys are modified, such information is flushed away, and\n# invalidation messages are sent to the clients. However if the workload is\n# heavily dominated by reads, KeyDB could use more and more memory in order\n# to track the keys fetched by many clients.\n#\n# For this reason it is possible to configure a maximum fill value for the\n# invalidation table. By default it is set to 1M of keys, and once this limit\n# is reached, KeyDB will start to evict keys in the invalidation table\n# even if they were not modified, just to reclaim memory: this will in turn\n# force the clients to invalidate the cached values. Basically the table\n# maximum size is a trade off between the memory you want to spend server\n# side to track information about who cached what, and the ability of clients\n# to retain cached objects in memory.\n#\n# If you set the value to 0, it means there are no limits, and KeyDB will\n# retain as many keys as needed in the invalidation table.\n# In the \"stats\" INFO section, you can find information about the number of\n# keys in the invalidation table at every given moment.\n#\n# Note: when key tracking is used in broadcasting mode, no memory is used\n# in the server side so this setting is useless.\n#\n# tracking-table-max-keys 1000000\n\n################################## SECURITY ###################################\n\n# Warning: since KeyDB is pretty fast, an outside user can try up to\n# 1 million passwords per second against a modern box. This means that you\n# should use very strong passwords, otherwise they will be very easy to break.\n# Note that because the password is really a shared secret between the client\n# and the server, and should not be memorized by any human, the password\n# can be easily a long string from /dev/urandom or whatever, so by using a\n# long and unguessable password no brute force attack will be possible.\n\n# KeyDB ACL users are defined in the following format:\n#\n#   user <username> ... acl rules ...\n#\n# For example:\n#\n#   user worker +@list +@connection ~jobs:* on >ffa9203c493aa99\n#\n# The special username \"default\" is used for new connections. If this user\n# has the \"nopass\" rule, then new connections will be immediately authenticated\n# as the \"default\" user without the need of any password provided via the\n# AUTH command. Otherwise if the \"default\" user is not flagged with \"nopass\"\n# the connections will start in not authenticated state, and will require\n# AUTH (or the HELLO command AUTH option) in order to be authenticated and\n# start to work.\n#\n# The ACL rules that describe what a user can do are the following:\n#\n#  on           Enable the user: it is possible to authenticate as this user.\n#  off          Disable the user: it's no longer possible to authenticate\n#               with this user, however the already authenticated connections\n#               will still work.\n#  skip-sanitize-payload    RESTORE dump-payload sanitation is skipped.\n#  sanitize-payload         RESTORE dump-payload is sanitized (default).\n#  +<command>   Allow the execution of that command\n#  -<command>   Disallow the execution of that command\n#  +@<category> Allow the execution of all the commands in such category\n#               with valid categories are like @admin, @set, @sortedset, ...\n#               and so forth, see the full list in the server.cpp file where\n#               the KeyDB command table is described and defined.\n#               The special category @all means all the commands, but currently\n#               present in the server, and that will be loaded in the future\n#               via modules.\n#  +<command>|subcommand    Allow a specific subcommand of an otherwise\n#                           disabled command. Note that this form is not\n#                           allowed as negative like -DEBUG|SEGFAULT, but\n#                           only additive starting with \"+\".\n#  allcommands  Alias for +@all. Note that it implies the ability to execute\n#               all the future commands loaded via the modules system.\n#  nocommands   Alias for -@all.\n#  ~<pattern>   Add a pattern of keys that can be mentioned as part of\n#               commands. For instance ~* allows all the keys. The pattern\n#               is a glob-style pattern like the one of KEYS.\n#               It is possible to specify multiple patterns.\n#  allkeys      Alias for ~*\n#  resetkeys    Flush the list of allowed keys patterns.\n#  &<pattern>   Add a glob-style pattern of Pub/Sub channels that can be\n#               accessed by the user. It is possible to specify multiple channel\n#               patterns.\n#  allchannels  Alias for &*\n#  resetchannels            Flush the list of allowed channel patterns.\n#  ><password>  Add this password to the list of valid password for the user.\n#               For example >mypass will add \"mypass\" to the list.\n#               This directive clears the \"nopass\" flag (see later).\n#  <<password>  Remove this password from the list of valid passwords.\n#  nopass       All the set passwords of the user are removed, and the user\n#               is flagged as requiring no password: it means that every\n#               password will work against this user. If this directive is\n#               used for the default user, every new connection will be\n#               immediately authenticated with the default user without\n#               any explicit AUTH command required. Note that the \"resetpass\"\n#               directive will clear this condition.\n#  resetpass    Flush the list of allowed passwords. Moreover removes the\n#               \"nopass\" status. After \"resetpass\" the user has no associated\n#               passwords and there is no way to authenticate without adding\n#               some password (or setting it as \"nopass\" later).\n#  reset        Performs the following actions: resetpass, resetkeys, off,\n#               -@all. The user returns to the same state it has immediately\n#               after its creation.\n#\n# ACL rules can be specified in any order: for instance you can start with\n# passwords, then flags, or key patterns. However note that the additive\n# and subtractive rules will CHANGE MEANING depending on the ordering.\n# For instance see the following example:\n#\n#   user alice on +@all -DEBUG ~* >somepassword\n#\n# This will allow \"alice\" to use all the commands with the exception of the\n# DEBUG command, since +@all added all the commands to the set of the commands\n# alice can use, and later DEBUG was removed. However if we invert the order\n# of two ACL rules the result will be different:\n#\n#   user alice on -DEBUG +@all ~* >somepassword\n#\n# Now DEBUG was removed when alice had yet no commands in the set of allowed\n# commands, later all the commands are added, so the user will be able to\n# execute everything.\n#\n# Basically ACL rules are processed left-to-right.\n#\n# The following is a list of command categories and their meanings:\n# * keyspace - Writing or reading from keys, databases, or their metadata \n#     in a type agnostic way. Includes DEL, RESTORE, DUMP, RENAME, EXISTS, DBSIZE,\n#     KEYS, EXPIRE, TTL, FLUSHALL, etc. Commands that may modify the keyspace,\n#     key or metadata will also have `write` category. Commands that only read\n#     the keyspace, key or metadata will have the `read` category.\n# * read - Reading from keys (values or metadata). Note that commands that don't\n#     interact with keys, will not have either `read` or `write`.\n# * write - Writing to keys (values or metadata)\n# * admin - Administrative commands. Normal applications will never need to use\n#     these. Includes REPLICAOF, CONFIG, DEBUG, SAVE, MONITOR, ACL, SHUTDOWN, etc.\n# * dangerous - Potentially dangerous (each should be considered with care for\n#     various reasons). This includes FLUSHALL, MIGRATE, RESTORE, SORT, KEYS,\n#     CLIENT, DEBUG, INFO, CONFIG, SAVE, REPLICAOF, etc.\n# * connection - Commands affecting the connection or other connections.\n#     This includes AUTH, SELECT, COMMAND, CLIENT, ECHO, PING, etc.\n# * blocking - Potentially blocking the connection until released by another\n#     command.\n# * fast - Fast O(1) commands. May loop on the number of arguments, but not the\n#     number of elements in the key.\n# * slow - All commands that are not Fast.\n# * pubsub - PUBLISH / SUBSCRIBE related\n# * transaction - WATCH / MULTI / EXEC related commands.\n# * scripting - Scripting related.\n# * set - Data type: sets related.\n# * sortedset - Data type: zsets related.\n# * list - Data type: lists related.\n# * hash - Data type: hashes related.\n# * string - Data type: strings related.\n# * bitmap - Data type: bitmaps related.\n# * hyperloglog - Data type: hyperloglog related.\n# * geo - Data type: geo related.\n# * stream - Data type: streams related.\n#\n# For more information about ACL configuration please refer to\n# the Redis web site at https://redis.io/topics/acl\n\n# ACL LOG\n#\n# The ACL Log tracks failed commands and authentication events associated\n# with ACLs. The ACL Log is useful to troubleshoot failed commands blocked \n# by ACLs. The ACL Log is stored in memory. You can reclaim memory with \n# ACL LOG RESET. Define the maximum entry length of the ACL Log below.\nacllog-max-len 128\n\n# Using an external ACL file\n#\n# Instead of configuring users here in this file, it is possible to use\n# a stand-alone file just listing users. The two methods cannot be mixed:\n# if you configure users here and at the same time you activate the external\n# ACL file, the server will refuse to start.\n#\n# The format of the external ACL user file is exactly the same as the\n# format that is used inside keydb.conf to describe users.\n#\n# aclfile /etc/keydb/users.acl\n\n# IMPORTANT NOTE: starting with KeyDB 6 \"requirepass\" is just a compatibility\n# layer on top of the new ACL system. The option effect will be just setting\n# the password for the default user. Clients will still authenticate using\n# AUTH <password> as usually, or more explicitly with AUTH default <password>\n# if they follow the new protocol: both will work.\n#\n# The requirepass is not compatible with aclfile option and the ACL LOAD\n# command, these will cause requirepass to be ignored.\n#\n# requirepass foobared\n\n# New users are initialized with restrictive permissions by default, via the\n# equivalent of this ACL rule 'off resetkeys -@all'. Starting with KeyDB 6.2, it\n# is possible to manage access to Pub/Sub channels with ACL rules as well. The\n# default Pub/Sub channels permission if new users is controlled by the\n# acl-pubsub-default configuration directive, which accepts one of these values:\n#\n# allchannels: grants access to all Pub/Sub channels\n# resetchannels: revokes access to all Pub/Sub channels\n#\n# To ensure backward compatibility while upgrading KeyDB 6.0, acl-pubsub-default\n# defaults to the 'allchannels' permission.\n#\n# Future compatibility note: it is very likely that in a future version of KeyDB\n# the directive's default of 'allchannels' will be changed to 'resetchannels' in\n# order to provide better out-of-the-box Pub/Sub security. Therefore, it is\n# recommended that you explicitly define Pub/Sub permissions for all users\n# rather then rely on implicit default values. Once you've set explicit\n# Pub/Sub for all existing users, you should uncomment the following line.\n#\n# acl-pubsub-default resetchannels\n\n# Command renaming (DEPRECATED).\n#\n# ------------------------------------------------------------------------\n# WARNING: avoid using this option if possible. Instead use ACLs to remove\n# commands from the default user, and put them only in some admin user you\n# create for administrative purposes.\n# ------------------------------------------------------------------------\n#\n# It is possible to change the name of dangerous commands in a shared\n# environment. For instance the CONFIG command may be renamed into something\n# hard to guess so that it will still be available for internal-use tools\n# but not available for general clients.\n#\n# Example:\n#\n# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52\n#\n# It is also possible to completely kill a command by renaming it into\n# an empty string:\n#\n# rename-command CONFIG \"\"\n#\n# Please note that changing the name of commands that are logged into the\n# AOF file or transmitted to replicas may cause problems.\n\n################################### CLIENTS ####################################\n\n# Set the max number of connected clients at the same time. By default\n# this limit is set to 10000 clients, however if the KeyDB server is not\n# able to configure the process file limit to allow for the specified limit\n# the max number of allowed clients is set to the current file limit\n# minus 32 (as KeyDB reserves a few file descriptors for internal uses).\n#\n# Once the limit is reached KeyDB will close all the new connections sending\n# an error 'max number of clients reached'.\n#\n# IMPORTANT: When KeyDB Cluster is used, the max number of connections is also\n# shared with the cluster bus: every node in the cluster will use two\n# connections, one incoming and another outgoing. It is important to size the\n# limit accordingly in case of very large clusters.\n#\n# maxclients 10000\n\n############################## MEMORY MANAGEMENT ################################\n\n# Set a memory usage limit to the specified amount of bytes.\n# When the memory limit is reached KeyDB will try to remove keys\n# according to the eviction policy selected (see maxmemory-policy).\n#\n# If KeyDB can't remove keys according to the policy, or if the policy is\n# set to 'noeviction', KeyDB will start to reply with errors to commands\n# that would use more memory, like SET, LPUSH, and so on, and will continue\n# to reply to read-only commands like GET.\n#\n# This option is usually useful when using KeyDB as an LRU or LFU cache, or to\n# set a hard memory limit for an instance (using the 'noeviction' policy).\n#\n# WARNING: If you have replicas attached to an instance with maxmemory on,\n# the size of the output buffers needed to feed the replicas are subtracted\n# from the used memory count, so that network problems / resyncs will\n# not trigger a loop where keys are evicted, and in turn the output\n# buffer of replicas is full with DELs of keys evicted triggering the deletion\n# of more keys, and so forth until the database is completely emptied.\n#\n# In short... if you have replicas attached it is suggested that you set a lower\n# limit for maxmemory so that there is some free RAM on the system for replica\n# output buffers (but this is not needed if the policy is 'noeviction').\n#\n# maxmemory <bytes>\n\n# MAXMEMORY POLICY: how KeyDB will select what to remove when maxmemory\n# is reached. You can select one from the following behaviors:\n#\n# volatile-lru -> Evict using approximated LRU, only keys with an expire set.\n# allkeys-lru -> Evict any key using approximated LRU.\n# volatile-lfu -> Evict using approximated LFU, only keys with an expire set.\n# allkeys-lfu -> Evict any key using approximated LFU.\n# volatile-random -> Remove a random key having an expire set.\n# allkeys-random -> Remove a random key, any key.\n# volatile-ttl -> Remove the key with the nearest expire time (minor TTL)\n# noeviction -> Don't evict anything, just return an error on write operations.\n#\n# LRU means Least Recently Used\n# LFU means Least Frequently Used\n#\n# Both LRU, LFU and volatile-ttl are implemented using approximated\n# randomized algorithms.\n#\n# Note: with any of the above policies, KeyDB will return an error on write\n#       operations, when there are no suitable keys for eviction.\n#\n#       At the date of writing these commands are: set setnx setex append\n#       incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd\n#       sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby\n#       zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby\n#       getset mset msetnx exec sort\n#\n# The default is:\n#\n# maxmemory-policy noeviction\n\n# LRU, LFU and minimal TTL algorithms are not precise algorithms but approximated\n# algorithms (in order to save memory), so you can tune it for speed or\n# accuracy. By default KeyDB will check five keys and pick the one that was\n# used least recently, you can change the sample size using the following\n# configuration directive.\n#\n# The default of 5 produces good enough results. 10 Approximates very closely\n# true LRU but costs more CPU. 3 is faster but not very accurate.\n#\n# maxmemory-samples 5\n\n# Eviction processing is designed to function well with the default setting.\n# If there is an unusually large amount of write traffic, this value may need to\n# be increased.  Decreasing this value may reduce latency at the risk of\n# eviction processing effectiveness\n#   0 = minimum latency, 10 = default, 100 = process without regard to latency\n#\n# maxmemory-eviction-tenacity 10\n\n# Starting from KeyDB 5, by default a replica will ignore its maxmemory setting\n# (unless it is promoted to master after a failover or manually). It means\n# that the eviction of keys will be just handled by the master, sending the\n# DEL commands to the replica as keys evict in the master side.\n#\n# This behavior ensures that masters and replicas stay consistent, and is usually\n# what you want, however if your replica is writable, or you want the replica\n# to have a different memory setting, and you are sure all the writes performed\n# to the replica are idempotent, then you may change this default (but be sure\n# to understand what you are doing).\n#\n# Note that since the replica by default does not evict, it may end using more\n# memory than the one set via maxmemory (there are certain buffers that may\n# be larger on the replica, or data structures may sometimes take more memory\n# and so forth). So make sure you monitor your replicas and make sure they\n# have enough memory to never hit a real out-of-memory condition before the\n# master hits the configured maxmemory setting.\n#\n# replica-ignore-maxmemory yes\n\n# KeyDB reclaims expired keys in two ways: upon access when those keys are\n# found to be expired, and also in background, in what is called the\n# \"active expire key\". The key space is slowly and interactively scanned\n# looking for expired keys to reclaim, so that it is possible to free memory\n# of keys that are expired and will never be accessed again in a short time.\n#\n# The default effort of the expire cycle will try to avoid having more than\n# ten percent of expired keys still in memory, and will try to avoid consuming\n# more than 25% of total memory and to add latency to the system. However\n# it is possible to increase the expire \"effort\" that is normally set to\n# \"1\", to a greater value, up to the value \"10\". At its maximum value the\n# system will use more CPU, longer cycles (and technically may introduce\n# more latency), and will tolerate less already expired keys still present\n# in the system. It's a tradeoff between memory, CPU and latency.\n#\n# active-expire-effort 1\n\n############################# LAZY FREEING ####################################\n\n# KeyDB has two primitives to delete keys. One is called DEL and is a blocking\n# deletion of the object. It means that the server stops processing new commands\n# in order to reclaim all the memory associated with an object in a synchronous\n# way. If the key deleted is associated with a small object, the time needed\n# in order to execute the DEL command is very small and comparable to most other\n# O(1) or O(log_N) commands in KeyDB. However if the key is associated with an\n# aggregated value containing millions of elements, the server can block for\n# a long time (even seconds) in order to complete the operation.\n#\n# For the above reasons KeyDB also offers non blocking deletion primitives\n# such as UNLINK (non blocking DEL) and the ASYNC option of FLUSHALL and\n# FLUSHDB commands, in order to reclaim memory in background. Those commands\n# are executed in constant time. Another thread will incrementally free the\n# object in the background as fast as possible.\n#\n# DEL, UNLINK and ASYNC option of FLUSHALL and FLUSHDB are user-controlled.\n# It's up to the design of the application to understand when it is a good\n# idea to use one or the other. However the KeyDB server sometimes has to\n# delete keys or flush the whole database as a side effect of other operations.\n# Specifically KeyDB deletes objects independently of a user call in the\n# following scenarios:\n#\n# 1) On eviction, because of the maxmemory and maxmemory policy configurations,\n#    in order to make room for new data, without going over the specified\n#    memory limit.\n# 2) Because of expire: when a key with an associated time to live (see the\n#    EXPIRE command) must be deleted from memory.\n# 3) Because of a side effect of a command that stores data on a key that may\n#    already exist. For example the RENAME command may delete the old key\n#    content when it is replaced with another one. Similarly SUNIONSTORE\n#    or SORT with STORE option may delete existing keys. The SET command\n#    itself removes any old content of the specified key in order to replace\n#    it with the specified string.\n# 4) During replication, when a replica performs a full resynchronization with\n#    its master, the content of the whole database is removed in order to\n#    load the RDB file just transferred.\n#\n# In all the above cases the default is to delete objects in a blocking way,\n# like if DEL was called. However you can configure each case specifically\n# in order to instead release memory in a non-blocking way like if UNLINK\n# was called, using the following configuration directives.\n\nlazyfree-lazy-eviction no\nlazyfree-lazy-expire no\nlazyfree-lazy-server-del no\nreplica-lazy-flush no\n\n# It is also possible, for the case when to replace the user code DEL calls\n# with UNLINK calls is not easy, to modify the default behavior of the DEL\n# command to act exactly like UNLINK, using the following configuration\n# directive:\n\nlazyfree-lazy-user-del no\n\n# FLUSHDB, FLUSHALL, and SCRIPT FLUSH support both asynchronous and synchronous\n# deletion, which can be controlled by passing the [SYNC|ASYNC] flags into the\n# commands. When neither flag is passed, this directive will be used to determine\n# if the data should be deleted asynchronously.\n\nlazyfree-lazy-user-flush no\n\n############################ KERNEL OOM CONTROL ##############################\n\n# On Linux, it is possible to hint the kernel OOM killer on what processes\n# should be killed first when out of memory.\n#\n# Enabling this feature makes KeyDB actively control the oom_score_adj value\n# for all its processes, depending on their role. The default scores will\n# attempt to have background child processes killed before all others, and\n# replicas killed before masters.\n#\n# KeyDB supports three options:\n#\n# no:       Don't make changes to oom-score-adj (default).\n# yes:      Alias to \"relative\" see below.\n# absolute: Values in oom-score-adj-values are written as is to the kernel.\n# relative: Values are used relative to the initial value of oom_score_adj when\n#           the server starts and are then clamped to a range of -1000 to 1000.\n#           Because typically the initial value is 0, they will often match the\n#           absolute values.\noom-score-adj no\n\n# When oom-score-adj is used, this directive controls the specific values used\n# for master, replica and background child processes. Values range -2000 to\n# 2000 (higher means more likely to be killed).\n#\n# Unprivileged processes (not root, and without CAP_SYS_RESOURCE capabilities)\n# can freely increase their value, but not decrease it below its initial\n# settings. This means that setting oom-score-adj to \"relative\" and setting the\n# oom-score-adj-values to positive values will always succeed.\noom-score-adj-values 0 200 800\n\n\n#################### KERNEL transparent hugepage CONTROL ######################\n\n# Usually the kernel Transparent Huge Pages control is set to \"madvise\" or\n# or \"never\" by default (/sys/kernel/mm/transparent_hugepage/enabled), in which\n# case this config has no effect. On systems in which it is set to \"always\",\n# KeyDB will attempt to disable it specifically for the KeyDB process in order\n# to avoid latency problems specifically with fork(2) and CoW.\n# If for some reason you prefer to keep it enabled, you can set this config to\n# \"no\" and the kernel global to \"always\".\n\ndisable-thp yes\n\n############################## APPEND ONLY MODE ###############################\n\n# By default KeyDB asynchronously dumps the dataset on disk. This mode is\n# good enough in many applications, but an issue with the KeyDB process or\n# a power outage may result into a few minutes of writes lost (depending on\n# the configured save points).\n#\n# The Append Only File is an alternative persistence mode that provides\n# much better durability. For instance using the default data fsync policy\n# (see later in the config file) KeyDB can lose just one second of writes in a\n# dramatic event like a server power outage, or a single write if something\n# wrong with the KeyDB process itself happens, but the operating system is\n# still running correctly.\n#\n# AOF and RDB persistence can be enabled at the same time without problems.\n# If the AOF is enabled on startup KeyDB will load the AOF, that is the file\n# with the better durability guarantees.\n#\n# Please check http://redis.io/topics/persistence for more information.\n\nappendonly no\n\n# The name of the append only file (default: \"appendonly.aof\")\n\nappendfilename \"appendonly.aof\"\n\n# The fsync() call tells the Operating System to actually write data on disk\n# instead of waiting for more data in the output buffer. Some OS will really flush\n# data on disk, some other OS will just try to do it ASAP.\n#\n# KeyDB supports three different modes:\n#\n# no: don't fsync, just let the OS flush the data when it wants. Faster.\n# always: fsync after every write to the append only log. Slow, Safest.\n# everysec: fsync only one time every second. Compromise.\n#\n# The default is \"everysec\", as that's usually the right compromise between\n# speed and data safety. It's up to you to understand if you can relax this to\n# \"no\" that will let the operating system flush the output buffer when\n# it wants, for better performances (but if you can live with the idea of\n# some data loss consider the default persistence mode that's snapshotting),\n# or on the contrary, use \"always\" that's very slow but a bit safer than\n# everysec.\n#\n# More details please check the following article:\n# http://antirez.com/post/redis-persistence-demystified.html\n#\n# If unsure, use \"everysec\".\n\n# appendfsync always\nappendfsync everysec\n# appendfsync no\n\n# When the AOF fsync policy is set to always or everysec, and a background\n# saving process (a background save or AOF log background rewriting) is\n# performing a lot of I/O against the disk, in some Linux configurations\n# KeyDB may block too long on the fsync() call. Note that there is no fix for\n# this currently, as even performing fsync in a different thread will block\n# our synchronous write(2) call.\n#\n# In order to mitigate this problem it's possible to use the following option\n# that will prevent fsync() from being called in the main process while a\n# BGSAVE or BGREWRITEAOF is in progress.\n#\n# This means that while another child is saving, the durability of KeyDB is\n# the same as \"appendfsync none\". In practical terms, this means that it is\n# possible to lose up to 30 seconds of log in the worst scenario (with the\n# default Linux settings).\n#\n# If you have latency problems turn this to \"yes\". Otherwise leave it as\n# \"no\" that is the safest pick from the point of view of durability.\n\nno-appendfsync-on-rewrite no\n\n# Automatic rewrite of the append only file.\n# KeyDB is able to automatically rewrite the log file implicitly calling\n# BGREWRITEAOF when the AOF log size grows by the specified percentage.\n#\n# This is how it works: KeyDB remembers the size of the AOF file after the\n# latest rewrite (if no rewrite has happened since the restart, the size of\n# the AOF at startup is used).\n#\n# This base size is compared to the current size. If the current size is\n# bigger than the specified percentage, the rewrite is triggered. Also\n# you need to specify a minimal size for the AOF file to be rewritten, this\n# is useful to avoid rewriting the AOF file even if the percentage increase\n# is reached but it is still pretty small.\n#\n# Specify a percentage of zero in order to disable the automatic AOF\n# rewrite feature.\n\nauto-aof-rewrite-percentage 100\nauto-aof-rewrite-min-size 64mb\n\n# An AOF file may be found to be truncated at the end during the KeyDB\n# startup process, when the AOF data gets loaded back into memory.\n# This may happen when the system where KeyDB is running\n# crashes, especially when an ext4 filesystem is mounted without the\n# data=ordered option (however this can't happen when KeyDB itself\n# crashes or aborts but the operating system still works correctly).\n#\n# KeyDB can either exit with an error when this happens, or load as much\n# data as possible (the default now) and start if the AOF file is found\n# to be truncated at the end. The following option controls this behavior.\n#\n# If aof-load-truncated is set to yes, a truncated AOF file is loaded and\n# the KeyDB server starts emitting a log to inform the user of the event.\n# Otherwise if the option is set to no, the server aborts with an error\n# and refuses to start. When the option is set to no, the user requires\n# to fix the AOF file using the \"keydb-check-aof\" utility before to restart\n# the server.\n#\n# Note that if the AOF file will be found to be corrupted in the middle\n# the server will still exit with an error. This option only applies when\n# KeyDB will try to read more data from the AOF file but not enough bytes\n# will be found.\naof-load-truncated yes\n\n# When rewriting the AOF file, KeyDB is able to use an RDB preamble in the\n# AOF file for faster rewrites and recoveries. When this option is turned\n# on the rewritten AOF file is composed of two different stanzas:\n#\n#   [RDB file][AOF tail]\n#\n# When loading, KeyDB recognizes that the AOF file starts with the \"REDIS\"\n# string and loads the prefixed RDB file, then continues loading the AOF\n# tail.\naof-use-rdb-preamble yes\n\n################################ LUA SCRIPTING  ###############################\n\n# Max execution time of a Lua script in milliseconds.\n#\n# If the maximum execution time is reached KeyDB will log that a script is\n# still in execution after the maximum allowed time and will start to\n# reply to queries with an error.\n#\n# When a long running script exceeds the maximum execution time only the\n# SCRIPT KILL and SHUTDOWN NOSAVE commands are available. The first can be\n# used to stop a script that did not yet call any write commands. The second\n# is the only way to shut down the server in the case a write command was\n# already issued by the script but the user doesn't want to wait for the natural\n# termination of the script.\n#\n# Set it to 0 or a negative value for unlimited execution without warnings.\nlua-time-limit 5000\n\n################################ KEYDB CLUSTER  ###############################\n\n# Normal KeyDB instances can't be part of a KeyDB Cluster; only nodes that are\n# started as cluster nodes can. In order to start a KeyDB instance as a\n# cluster node enable the cluster support uncommenting the following:\n#\n# cluster-enabled yes\n\n# Every cluster node has a cluster configuration file. This file is not\n# intended to be edited by hand. It is created and updated by KeyDB nodes.\n# Every KeyDB Cluster node requires a different cluster configuration file.\n# Make sure that instances running in the same system do not have\n# overlapping cluster configuration file names.\n#\n# cluster-config-file nodes-6379.conf\n\n# Cluster node timeout is the amount of milliseconds a node must be unreachable\n# for it to be considered in failure state.\n# Most other internal time limits are a multiple of the node timeout.\n#\n# cluster-node-timeout 15000\n\n# A replica of a failing master will avoid to start a failover if its data\n# looks too old.\n#\n# There is no simple way for a replica to actually have an exact measure of\n# its \"data age\", so the following two checks are performed:\n#\n# 1) If there are multiple replicas able to failover, they exchange messages\n#    in order to try to give an advantage to the replica with the best\n#    replication offset (more data from the master processed).\n#    Replicas will try to get their rank by offset, and apply to the start\n#    of the failover a delay proportional to their rank.\n#\n# 2) Every single replica computes the time of the last interaction with\n#    its master. This can be the last ping or command received (if the master\n#    is still in the \"connected\" state), or the time that elapsed since the\n#    disconnection with the master (if the replication link is currently down).\n#    If the last interaction is too old, the replica will not try to failover\n#    at all.\n#\n# The point \"2\" can be tuned by user. Specifically a replica will not perform\n# the failover if, since the last interaction with the master, the time\n# elapsed is greater than:\n#\n#   (node-timeout * cluster-replica-validity-factor) + repl-ping-replica-period\n#\n# So for example if node-timeout is 30 seconds, and the cluster-replica-validity-factor\n# is 10, and assuming a default repl-ping-replica-period of 10 seconds, the\n# replica will not try to failover if it was not able to talk with the master\n# for longer than 310 seconds.\n#\n# A large cluster-replica-validity-factor may allow replicas with too old data to failover\n# a master, while a too small value may prevent the cluster from being able to\n# elect a replica at all.\n#\n# For maximum availability, it is possible to set the cluster-replica-validity-factor\n# to a value of 0, which means, that replicas will always try to failover the\n# master regardless of the last time they interacted with the master.\n# (However they'll always try to apply a delay proportional to their\n# offset rank).\n#\n# Zero is the only value able to guarantee that when all the partitions heal\n# the cluster will always be able to continue.\n#\n# cluster-replica-validity-factor 10\n\n# Cluster replicas are able to migrate to orphaned masters, that are masters\n# that are left without working replicas. This improves the cluster ability\n# to resist to failures as otherwise an orphaned master can't be failed over\n# in case of failure if it has no working replicas.\n#\n# Replicas migrate to orphaned masters only if there are still at least a\n# given number of other working replicas for their old master. This number\n# is the \"migration barrier\". A migration barrier of 1 means that a replica\n# will migrate only if there is at least 1 other working replica for its master\n# and so forth. It usually reflects the number of replicas you want for every\n# master in your cluster.\n#\n# Default is 1 (replicas migrate only if their masters remain with at least\n# one replica). To disable migration just set it to a very large value or\n# set cluster-allow-replica-migration to 'no'.\n# A value of 0 can be set but is useful only for debugging and dangerous\n# in production.\n#\n# cluster-migration-barrier 1\n\n# Turning off this option allows to use less automatic cluster configuration.\n# It both disables migration to orphaned masters and migration from masters\n# that became empty.\n#\n# Default is 'yes' (allow automatic migrations).\n#\n# cluster-allow-replica-migration yes\n\n# By default KeyDB Cluster nodes stop accepting queries if they detect there\n# is at least a hash slot uncovered (no available node is serving it).\n# This way if the cluster is partially down (for example a range of hash slots\n# are no longer covered) all the cluster becomes, eventually, unavailable.\n# It automatically returns available as soon as all the slots are covered again.\n#\n# However sometimes you want the subset of the cluster which is working,\n# to continue to accept queries for the part of the key space that is still\n# covered. In order to do so, just set the cluster-require-full-coverage\n# option to no.\n#\n# cluster-require-full-coverage yes\n\n# This option, when set to yes, prevents replicas from trying to failover its\n# master during master failures. However the master can still perform a\n# manual failover, if forced to do so.\n#\n# This is useful in different scenarios, especially in the case of multiple\n# data center operations, where we want one side to never be promoted if not\n# in the case of a total DC failure.\n#\n# cluster-replica-no-failover no\n\n# This option, when set to yes, allows nodes to serve read traffic while the\n# the cluster is in a down state, as long as it believes it owns the slots. \n#\n# This is useful for two cases.  The first case is for when an application \n# doesn't require consistency of data during node failures or network partitions.\n# One example of this is a cache, where as long as the node has the data it\n# should be able to serve it. \n#\n# The second use case is for configurations that don't meet the recommended  \n# three shards but want to enable cluster mode and scale later. A \n# master outage in a 1 or 2 shard configuration causes a read/write outage to the\n# entire cluster without this option set, with it set there is only a write outage.\n# Without a quorum of masters, slot ownership will not change automatically. \n#\n# cluster-allow-reads-when-down no\n\n# In order to setup your cluster make sure to read the documentation\n# available at http://redis.io web site.\n\n########################## CLUSTER DOCKER/NAT support  ########################\n\n# In certain deployments, KeyDB Cluster nodes address discovery fails, because\n# addresses are NAT-ted or because ports are forwarded (the typical case is\n# Docker and other containers).\n#\n# In order to make KeyDB Cluster working in such environments, a static\n# configuration where each node knows its public address is needed. The\n# following four options are used for this scope, and are:\n#\n# * cluster-announce-ip\n# * cluster-announce-port\n# * cluster-announce-tls-port\n# * cluster-announce-bus-port\n#\n# Each instructs the node about its address, client ports (for connections\n# without and with TLS), and cluster message\n# bus port. The information is then published in the header of the bus packets\n# so that other nodes will be able to correctly map the address of the node\n# publishing the information.\n#\n# If cluster-tls is set to yes and cluster-announce-tls-port is omitted or set\n# to zero, then cluster-announce-port refers to the TLS port. Note also that\n# cluster-announce-tls-port has no effect if cluster-tls is set to no.\n#\n# If the above options are not used, the normal KeyDB Cluster auto-detection\n# will be used instead.\n#\n# Note that when remapped, the bus port may not be at the fixed offset of\n# clients port + 10000, so you can specify any port and bus-port depending\n# on how they get remapped. If the bus-port is not set, a fixed offset of\n# 10000 will be used as usual.\n#\n# Example:\n#\n# cluster-announce-ip 10.1.1.5\n# cluster-announce-tls-port 6379\n# cluster-announce-port 0\n# cluster-announce-bus-port 6380\n\n################################## SLOW LOG ###################################\n\n# The KeyDB Slow Log is a system to log queries that exceeded a specified\n# execution time. The execution time does not include the I/O operations\n# like talking with the client, sending the reply and so forth,\n# but just the time needed to actually execute the command (this is the only\n# stage of command execution where the thread is blocked and can not serve\n# other requests in the meantime).\n#\n# You can configure the slow log with two parameters: one tells KeyDB\n# what is the execution time, in microseconds, to exceed in order for the\n# command to get logged, and the other parameter is the length of the\n# slow log. When a new command is logged the oldest one is removed from the\n# queue of logged commands.\n\n# The following time is expressed in microseconds, so 1000000 is equivalent\n# to one second. Note that a negative number disables the slow log, while\n# a value of zero forces the logging of every command.\nslowlog-log-slower-than 10000\n\n# There is no limit to this length. Just be aware that it will consume memory.\n# You can reclaim memory used by the slow log with SLOWLOG RESET.\nslowlog-max-len 128\n\n################################ LATENCY MONITOR ##############################\n\n# The KeyDB latency monitoring subsystem samples different operations\n# at runtime in order to collect data related to possible sources of\n# latency of a KeyDB instance.\n#\n# Via the LATENCY command this information is available to the user that can\n# print graphs and obtain reports.\n#\n# The system only logs operations that were performed in a time equal or\n# greater than the amount of milliseconds specified via the\n# latency-monitor-threshold configuration directive. When its value is set\n# to zero, the latency monitor is turned off.\n#\n# By default latency monitoring is disabled since it is mostly not needed\n# if you don't have latency issues, and collecting data has a performance\n# impact, that while very small, can be measured under big load. Latency\n# monitoring can easily be enabled at runtime using the command\n# \"CONFIG SET latency-monitor-threshold <milliseconds>\" if needed.\nlatency-monitor-threshold 0\n\n############################# EVENT NOTIFICATION ##############################\n\n# KeyDB can notify Pub/Sub clients about events happening in the key space.\n# This feature is documented at http://redis.io/topics/notifications\n#\n# For instance if keyspace events notification is enabled, and a client\n# performs a DEL operation on key \"foo\" stored in the Database 0, two\n# messages will be published via Pub/Sub:\n#\n# PUBLISH __keyspace@0__:foo del\n# PUBLISH __keyevent@0__:del foo\n#\n# It is possible to select the events that KeyDB will notify among a set\n# of classes. Every class is identified by a single character:\n#\n#  K     Keyspace events, published with __keyspace@<db>__ prefix.\n#  E     Keyevent events, published with __keyevent@<db>__ prefix.\n#  g     Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ...\n#  $     String commands\n#  l     List commands\n#  s     Set commands\n#  h     Hash commands\n#  z     Sorted set commands\n#  x     Expired events (events generated every time a key expires)\n#  e     Evicted events (events generated when a key is evicted for maxmemory)\n#  t     Stream commands\n#  d     Module key type events\n#  m     Key-miss events (Note: It is not included in the 'A' class)\n#  A     Alias for g$lshzxetd, so that the \"AKE\" string means all the events\n#        (Except key-miss events which are excluded from 'A' due to their\n#         unique nature).\n#\n#  The \"notify-keyspace-events\" takes as argument a string that is composed\n#  of zero or multiple characters. The empty string means that notifications\n#  are disabled.\n#\n#  Example: to enable list and generic events, from the point of view of the\n#           event name, use:\n#\n#  notify-keyspace-events Elg\n#\n#  Example 2: to get the stream of the expired keys subscribing to channel\n#             name __keyevent@0__:expired use:\n#\n#  notify-keyspace-events Ex\n#\n#  By default all notifications are disabled because most users don't need\n#  this feature and the feature has some overhead. Note that if you don't\n#  specify at least one of K or E, no events will be delivered.\nnotify-keyspace-events \"\"\n\n############################### ADVANCED CONFIG ###############################\n\n# Hashes are encoded using a memory efficient data structure when they have a\n# small number of entries, and the biggest entry does not exceed a given\n# threshold. These thresholds can be configured using the following directives.\nhash-max-ziplist-entries 512\nhash-max-ziplist-value 64\n\n# Lists are also encoded in a special way to save a lot of space.\n# The number of entries allowed per internal list node can be specified\n# as a fixed maximum size or a maximum number of elements.\n# For a fixed maximum size, use -5 through -1, meaning:\n# -5: max size: 64 Kb  <-- not recommended for normal workloads\n# -4: max size: 32 Kb  <-- not recommended\n# -3: max size: 16 Kb  <-- probably not recommended\n# -2: max size: 8 Kb   <-- good\n# -1: max size: 4 Kb   <-- good\n# Positive numbers mean store up to _exactly_ that number of elements\n# per list node.\n# The highest performing option is usually -2 (8 Kb size) or -1 (4 Kb size),\n# but if your use case is unique, adjust the settings as necessary.\nlist-max-ziplist-size -2\n\n# Lists may also be compressed.\n# Compress depth is the number of quicklist ziplist nodes from *each* side of\n# the list to *exclude* from compression.  The head and tail of the list\n# are always uncompressed for fast push/pop operations.  Settings are:\n# 0: disable all list compression\n# 1: depth 1 means \"don't start compressing until after 1 node into the list,\n#    going from either the head or tail\"\n#    So: [head]->node->node->...->node->[tail]\n#    [head], [tail] will always be uncompressed; inner nodes will compress.\n# 2: [head]->[next]->node->node->...->node->[prev]->[tail]\n#    2 here means: don't compress head or head->next or tail->prev or tail,\n#    but compress all nodes between them.\n# 3: [head]->[next]->[next]->node->node->...->node->[prev]->[prev]->[tail]\n# etc.\nlist-compress-depth 0\n\n# Sets have a special encoding in just one case: when a set is composed\n# of just strings that happen to be integers in radix 10 in the range\n# of 64 bit signed integers.\n# The following configuration setting sets the limit in the size of the\n# set in order to use this special memory saving encoding.\nset-max-intset-entries 512\n\n# Similarly to hashes and lists, sorted sets are also specially encoded in\n# order to save a lot of space. This encoding is only used when the length and\n# elements of a sorted set are below the following limits:\nzset-max-ziplist-entries 128\nzset-max-ziplist-value 64\n\n# HyperLogLog sparse representation bytes limit. The limit includes the\n# 16 bytes header. When an HyperLogLog using the sparse representation crosses\n# this limit, it is converted into the dense representation.\n#\n# A value greater than 16000 is totally useless, since at that point the\n# dense representation is more memory efficient.\n#\n# The suggested value is ~ 3000 in order to have the benefits of\n# the space efficient encoding without slowing down too much PFADD,\n# which is O(N) with the sparse encoding. The value can be raised to\n# ~ 10000 when CPU is not a concern, but space is, and the data set is\n# composed of many HyperLogLogs with cardinality in the 0 - 15000 range.\nhll-sparse-max-bytes 3000\n\n# Streams macro node max size / items. The stream data structure is a radix\n# tree of big nodes that encode multiple items inside. Using this configuration\n# it is possible to configure how big a single node can be in bytes, and the\n# maximum number of items it may contain before switching to a new node when\n# appending new stream entries. If any of the following settings are set to\n# zero, the limit is ignored, so for instance it is possible to set just a\n# max entires limit by setting max-bytes to 0 and max-entries to the desired\n# value.\nstream-node-max-bytes 4096\nstream-node-max-entries 100\n\n# Active rehashing uses 1 millisecond every 100 milliseconds of CPU time in\n# order to help rehashing the main KeyDB hash table (the one mapping top-level\n# keys to values). The hash table implementation KeyDB uses (see dict.c)\n# performs a lazy rehashing: the more operation you run into a hash table\n# that is rehashing, the more rehashing \"steps\" are performed, so if the\n# server is idle the rehashing is never complete and some more memory is used\n# by the hash table.\n#\n# The default is to use this millisecond 10 times every second in order to\n# actively rehash the main dictionaries, freeing memory when possible.\n#\n# If unsure:\n# use \"activerehashing no\" if you have hard latency requirements and it is\n# not a good thing in your environment that KeyDB can reply from time to time\n# to queries with 2 milliseconds delay.\n#\n# use \"activerehashing yes\" if you don't have such hard requirements but\n# want to free memory asap when possible.\nactiverehashing yes\n\n# The client output buffer limits can be used to force disconnection of clients\n# that are not reading data from the server fast enough for some reason (a\n# common reason is that a Pub/Sub client can't consume messages as fast as the\n# publisher can produce them).\n#\n# The limit can be set differently for the three different classes of clients:\n#\n# normal -> normal clients including MONITOR clients\n# replica  -> replica clients\n# pubsub -> clients subscribed to at least one pubsub channel or pattern\n#\n# The syntax of every client-output-buffer-limit directive is the following:\n#\n# client-output-buffer-limit <class> <hard limit> <soft limit> <soft seconds>\n#\n# A client is immediately disconnected once the hard limit is reached, or if\n# the soft limit is reached and remains reached for the specified number of\n# seconds (continuously).\n# So for instance if the hard limit is 32 megabytes and the soft limit is\n# 16 megabytes / 10 seconds, the client will get disconnected immediately\n# if the size of the output buffers reach 32 megabytes, but will also get\n# disconnected if the client reaches 16 megabytes and continuously overcomes\n# the limit for 10 seconds.\n#\n# By default normal clients are not limited because they don't receive data\n# without asking (in a push way), but just after a request, so only\n# asynchronous clients may create a scenario where data is requested faster\n# than it can read.\n#\n# Instead there is a default limit for pubsub and replica clients, since\n# subscribers and replicas receive data in a push fashion.\n#\n# Both the hard or the soft limit can be disabled by setting them to zero.\nclient-output-buffer-limit normal 0 0 0\nclient-output-buffer-limit replica 256mb 64mb 60\nclient-output-buffer-limit pubsub 32mb 8mb 60\n\n# Client query buffers accumulate new commands. They are limited to a fixed\n# amount by default in order to avoid that a protocol desynchronization (for\n# instance due to a bug in the client) will lead to unbound memory usage in\n# the query buffer. However you can configure it here if you have very special\n# needs, such us huge multi/exec requests or alike.\n#\n# client-query-buffer-limit 1gb\n\n# In the KeyDB protocol, bulk requests, that are, elements representing single\n# strings, are normally limited to 512 mb. However you can change this limit\n# here, but must be 1mb or greater\n#\n# proto-max-bulk-len 512mb\n\n# KeyDB calls an internal function to perform many background tasks, like\n# closing connections of clients in timeout, purging expired keys that are\n# never requested, and so forth.\n#\n# Not all tasks are performed with the same frequency, but KeyDB checks for\n# tasks to perform according to the specified \"hz\" value.\n#\n# By default \"hz\" is set to 10. Raising the value will use more CPU when\n# KeyDB is idle, but at the same time will make KeyDB more responsive when\n# there are many keys expiring at the same time, and timeouts may be\n# handled with more precision.\n#\n# The range is between 1 and 500, however a value over 100 is usually not\n# a good idea. Most users should use the default of 10 and raise this up to\n# 100 only in environments where very low latency is required.\nhz 10\n\n# Normally it is useful to have an HZ value which is proportional to the\n# number of clients connected. This is useful in order, for instance, to\n# avoid too many clients are processed for each background task invocation\n# in order to avoid latency spikes.\n#\n# Since the default HZ value by default is conservatively set to 10, KeyDB\n# offers, and enables by default, the ability to use an adaptive HZ value\n# which will temporarily raise when there are many connected clients.\n#\n# When dynamic HZ is enabled, the actual configured HZ will be used\n# as a baseline, but multiples of the configured HZ value will be actually\n# used as needed once more clients are connected. In this way an idle\n# instance will use very little CPU time while a busy instance will be\n# more responsive.\ndynamic-hz yes\n\n# When a child rewrites the AOF file, if the following option is enabled\n# the file will be fsync-ed every 32 MB of data generated. This is useful\n# in order to commit the file to the disk more incrementally and avoid\n# big latency spikes.\naof-rewrite-incremental-fsync yes\n\n# When KeyDB saves RDB file, if the following option is enabled\n# the file will be fsync-ed every 32 MB of data generated. This is useful\n# in order to commit the file to the disk more incrementally and avoid\n# big latency spikes.\nrdb-save-incremental-fsync yes\n\n# KeyDB LFU eviction (see maxmemory setting) can be tuned. However it is a good\n# idea to start with the default settings and only change them after investigating\n# how to improve the performances and how the keys LFU change over time, which\n# is possible to inspect via the OBJECT FREQ command.\n#\n# There are two tunable parameters in the KeyDB LFU implementation: the\n# counter logarithm factor and the counter decay time. It is important to\n# understand what the two parameters mean before changing them.\n#\n# The LFU counter is just 8 bits per key, it's maximum value is 255, so KeyDB\n# uses a probabilistic increment with logarithmic behavior. Given the value\n# of the old counter, when a key is accessed, the counter is incremented in\n# this way:\n#\n# 1. A random number R between 0 and 1 is extracted.\n# 2. A probability P is calculated as 1/(old_value*lfu_log_factor+1).\n# 3. The counter is incremented only if R < P.\n#\n# The default lfu-log-factor is 10. This is a table of how the frequency\n# counter changes with a different number of accesses with different\n# logarithmic factors:\n#\n# +--------+------------+------------+------------+------------+------------+\n# | factor | 100 hits   | 1000 hits  | 100K hits  | 1M hits    | 10M hits   |\n# +--------+------------+------------+------------+------------+------------+\n# | 0      | 104        | 255        | 255        | 255        | 255        |\n# +--------+------------+------------+------------+------------+------------+\n# | 1      | 18         | 49         | 255        | 255        | 255        |\n# +--------+------------+------------+------------+------------+------------+\n# | 10     | 10         | 18         | 142        | 255        | 255        |\n# +--------+------------+------------+------------+------------+------------+\n# | 100    | 8          | 11         | 49         | 143        | 255        |\n# +--------+------------+------------+------------+------------+------------+\n#\n# NOTE: The above table was obtained by running the following commands:\n#\n#   keydb-benchmark -n 1000000 incr foo\n#   keydb-cli object freq foo\n#\n# NOTE 2: The counter initial value is 5 in order to give new objects a chance\n# to accumulate hits.\n#\n# The counter decay time is the time, in minutes, that must elapse in order\n# for the key counter to be divided by two (or decremented if it has a value\n# less <= 10).\n#\n# The default value for the lfu-decay-time is 1. A special value of 0 means to\n# decay the counter every time it happens to be scanned.\n#\n# lfu-log-factor 10\n# lfu-decay-time 1\n\n########################### ACTIVE DEFRAGMENTATION #######################\n#\n# What is active defragmentation?\n# -------------------------------\n#\n# Active (online) defragmentation allows a KeyDB server to compact the\n# spaces left between small allocations and deallocations of data in memory,\n# thus allowing to reclaim back memory.\n#\n# Fragmentation is a natural process that happens with every allocator (but\n# less so with Jemalloc, fortunately) and certain workloads. Normally a server\n# restart is needed in order to lower the fragmentation, or at least to flush\n# away all the data and create it again. However thanks to this feature\n# implemented by Oran Agra for Redis 4.0 this process can happen at runtime\n# in a \"hot\" way, while the server is running.\n#\n# Basically when the fragmentation is over a certain level (see the\n# configuration options below) KeyDB will start to create new copies of the\n# values in contiguous memory regions by exploiting certain specific Jemalloc\n# features (in order to understand if an allocation is causing fragmentation\n# and to allocate it in a better place), and at the same time, will release the\n# old copies of the data. This process, repeated incrementally for all the keys\n# will cause the fragmentation to drop back to normal values.\n#\n# Important things to understand:\n#\n# 1. This feature is disabled by default, and only works if you compiled KeyDB\n#    to use the copy of Jemalloc we ship with the source code of KeyDB.\n#    This is the default with Linux builds.\n#\n# 2. You never need to enable this feature if you don't have fragmentation\n#    issues.\n#\n# 3. Once you experience fragmentation, you can enable this feature when\n#    needed with the command \"CONFIG SET activedefrag yes\".\n#\n# The configuration parameters are able to fine tune the behavior of the\n# defragmentation process. If you are not sure about what they mean it is\n# a good idea to leave the defaults untouched.\n\n# Enabled active defragmentation\n# activedefrag no\n\n# Minimum amount of fragmentation waste to start active defrag\n# active-defrag-ignore-bytes 100mb\n\n# Minimum percentage of fragmentation to start active defrag\n# active-defrag-threshold-lower 10\n\n# Maximum percentage of fragmentation at which we use maximum effort\n# active-defrag-threshold-upper 100\n\n# Minimal effort for defrag in CPU percentage, to be used when the lower\n# threshold is reached\n# active-defrag-cycle-min 1\n\n# Maximal effort for defrag in CPU percentage, to be used when the upper\n# threshold is reached\n# active-defrag-cycle-max 25\n\n# Maximum number of set/hash/zset/list fields that will be processed from\n# the main dictionary scan\n# active-defrag-max-scan-fields 1000\n\n# Jemalloc background thread for purging will be enabled by default\njemalloc-bg-thread yes\n\n# It is possible to pin different threads and processes of KeyDB to specific\n# CPUs in your system, in order to maximize the performances of the server.\n# This is useful both in order to pin different KeyDB threads in different\n# CPUs, but also in order to make sure that multiple KeyDB instances running\n# in the same host will be pinned to different CPUs.\n#\n# Normally you can do this using the \"taskset\" command, however it is also\n# possible to this via KeyDB configuration directly, both in Linux and FreeBSD.\n#\n# You can pin the server/IO threads, bio threads, aof rewrite child process, and\n# the bgsave child process. The syntax to specify the cpu list is the same as\n# the taskset command:\n#\n# Set redis server/io threads to cpu affinity 0,2,4,6:\n# server_cpulist 0-7:2\n#\n# Set bio threads to cpu affinity 1,3:\n# bio_cpulist 1,3\n#\n# Set aof rewrite child process to cpu affinity 8,9,10,11:\n# aof_rewrite_cpulist 8-11\n#\n# Set bgsave child process to cpu affinity 1,10,11\n# bgsave_cpulist 1,10-11\n\n# In some cases KeyDB will emit warnings and even refuse to start if it detects\n# that the system is in bad state, it is possible to suppress these warnings\n# by setting the following config which takes a space delimited list of warnings\n# to suppress\n#\n# ignore-warnings ARM64-COW-BUG\n\n# The minimum number of clients on a thread before KeyDB assigns new connections to a different thread\n#  Tuning this parameter is a tradeoff between locking overhead and distributing the workload over multiple cores\n# min-clients-per-thread 50\n\n# How often to run RDB load progress callback?\n# The callback runs during key load to ping other servers and prevent timeouts.\n# It also updates load time estimates.\n# Change these values to run it more or less often. It will run when either condition is true.\n# Either when x bytes have been processed, or when x keys have been loaded.\n# loading-process-events-interval-bytes 2097152\n# loading-process-events-interval-keys 8192\n\n# Avoid forwarding RREPLAY messages to other masters?\n#   WARNING: This setting is dangerous! You must be certain all masters are connected to each\n#   other in a true mesh topology or data loss will occur!\n#   This command can be used to reduce multimaster bus traffic\n# multi-master-no-forward no\n\n# Path to directory for file backed scratchpad.  The file backed scratchpad\n# reduces memory requirements by storing rarely accessed data on disk \n# instead of RAM.  A temporary file will be created in this directory.\n# scratch-file-path /tmp/\n\n# Number of worker threads serving requests.  This number should be related to the performance\n# of your network hardware, not the number of cores on your machine.  We don't recommend going\n# above 4 at this time.  By default this is set 1.\n#\n# Note: KeyDB does not use io-threads, but io-threads is a config alias for server-threads\nserver-threads 2\n\n# Should KeyDB pin threads to CPUs? By default this is disabled, and KeyDB will not bind threads.\n# When enabled threads are bount to cores sequentially starting at core 0.\n# server-thread-affinity true\n\n# Uncomment the option below to enable Active Active support.  Note that\n# replicas will still sync in the normal way and incorrect ordering when\n# bringing up replicas can result in data loss (the first master will win).\n# active-replica yes\n\n# KeyDB will attempt to balance clients across threads evenly; However, replica clients\n# are usually much more expensive than a normal client, and so KeyDB will try to assign\n# fewer clients to threads with a replica.  The weighting factor below is intented to help tune\n# this behavior.  A replica weighting factor of 2 means we treat a replica as the equivalent\n# of two normal clients.  Adjusting this value may improve performance when replication is\n# used.  The best weighting is workload specific - e.g. read heavy workloads should set\n# this to 1.  Very write heavy workloads may benefit from higher numbers.\n#\n# By default KeyDB sets this to 2.\nreplica-weighting-factor 2\n\n# Enable FLASH support (Experimental Feature)\n# storage-provider flash /path/to/flash/db\n\n# Blob support is a way to store very large objects (>200MB) on disk\n# The files are automatically cleaned up when KeyDB exits and are only\n# for temporary use.  This helps reduce memory pressure for very large\n# data items at the cost of some performance.\n# \n# By default this config is disable.  When enabled the disk associated\n# with KeyDB's working directory will be used.  If there is insufficient\n# disk space or any other I/O error KeyDB will instead use memory.\n# \n# blob-support false\n"
  },
  {
    "path": "pkg/deb/conf/sentinel.conf",
    "content": "# Example sentinel.conf\n\n# *** IMPORTANT ***\n#\n# By default Sentinel will not be reachable from interfaces different than\n# localhost, either use the 'bind' directive to bind to a list of network\n# interfaces, or disable protected mode with \"protected-mode no\" by\n# adding it to this configuration file.\n#\n# Before doing that MAKE SURE the instance is protected from the outside\n# world via firewalling or other means.\n#\n# For example you may use one of the following:\n#\n# bind 127.0.0.1 192.168.1.1\n#\n# protected-mode no\n\n# port <sentinel-port>\n# The port that this sentinel instance will run on\nport 26379\n\n# By default KeyDB Sentinel does not run as a daemon. Use 'yes' if you need it.\n# Note that KeyDB will write a pid file in /var/run/keydb-sentinel.pid when\n# daemonized.\ndaemonize yes\n\n# When running daemonized, KeyDB Sentinel writes a pid file in\n# /var/run/keydb-sentinel.pid by default. You can specify a custom pid file\n# location here.\npidfile /var/run/sentinel/keydb-sentinel.pid\n\n# Specify the log file name. Also the empty string can be used to force\n# Sentinel to log on the standard output. Note that if you use standard\n# output for logging but daemonize, logs will be sent to /dev/null\nlogfile /var/log/keydb/keydb-sentinel.log\n\n# sentinel announce-ip <ip>\n# sentinel announce-port <port>\n#\n# The above two configuration directives are useful in environments where,\n# because of NAT, Sentinel is reachable from outside via a non-local address.\n#\n# When announce-ip is provided, the Sentinel will claim the specified IP address\n# in HELLO messages used to gossip its presence, instead of auto-detecting the\n# local address as it usually does.\n#\n# Similarly when announce-port is provided and is valid and non-zero, Sentinel\n# will announce the specified TCP port.\n#\n# The two options don't need to be used together, if only announce-ip is\n# provided, the Sentinel will announce the specified IP and the server port\n# as specified by the \"port\" option. If only announce-port is provided, the\n# Sentinel will announce the auto-detected local IP and the specified port.\n#\n# Example:\n#\n# sentinel announce-ip 1.2.3.4\n\n# dir <working-directory>\n# Every long running process should have a well-defined working directory.\n# For KeyDB Sentinel to chdir to /tmp at startup is the simplest thing\n# for the process to don't interfere with administrative tasks such as\n# unmounting filesystems.\ndir /var/lib/keydb\n\n# sentinel monitor <master-name> <ip> <keydb-port> <quorum>\n#\n# Tells Sentinel to monitor this master, and to consider it in O_DOWN\n# (Objectively Down) state only if at least <quorum> sentinels agree.\n#\n# Note that whatever is the ODOWN quorum, a Sentinel will require to\n# be elected by the majority of the known Sentinels in order to\n# start a failover, so no failover can be performed in minority.\n#\n# Replicas are auto-discovered, so you don't need to specify replicas in\n# any way. Sentinel itself will rewrite this configuration file adding\n# the replicas using additional configuration options.\n# Also note that the configuration file is rewritten when a\n# replica is promoted to master.\n#\n# Note: master name should not include special characters or spaces.\n# The valid charset is A-z 0-9 and the three characters \".-_\".\nsentinel monitor mymaster 127.0.0.1 6379 2\n\n# sentinel auth-pass <master-name> <password>\n#\n# Set the password to use to authenticate with the master and replicas.\n# Useful if there is a password set in the KeyDB instances to monitor.\n#\n# Note that the master password is also used for replicas, so it is not\n# possible to set a different password in masters and replicas instances\n# if you want to be able to monitor these instances with Sentinel.\n#\n# However you can have KeyDB instances without the authentication enabled\n# mixed with KeyDB instances requiring the authentication (as long as the\n# password set is the same for all the instances requiring the password) as\n# the AUTH command will have no effect in KeyDB instances with authentication\n# switched off.\n#\n# Example:\n#\n# sentinel auth-pass mymaster MySUPER--secret-0123passw0rd\n\n# sentinel auth-user <master-name> <username>\n#\n# This is useful in order to authenticate to instances having ACL capabilities,\n# that is, running KeyDB 6.0 or greater. When just auth-pass is provided the\n# Sentinel instance will authenticate to KeyDB using the old \"AUTH <pass>\"\n# method. When also an username is provided, it will use \"AUTH <user> <pass>\".\n# In the KeyDB servers side, the ACL to provide just minimal access to\n# Sentinel instances, should be configured along the following lines:\n#\n#     user sentinel-user >somepassword +client +subscribe +publish \\\n#                        +ping +info +multi +slaveof +config +client +exec on\n\n# sentinel down-after-milliseconds <master-name> <milliseconds>\n#\n# Number of milliseconds the master (or any attached replica or sentinel) should\n# be unreachable (as in, not acceptable reply to PING, continuously, for the\n# specified period) in order to consider it in S_DOWN state (Subjectively\n# Down).\n#\n# Default is 30 seconds.\nsentinel down-after-milliseconds mymaster 30000\n\n# IMPORTANT NOTE: starting with KeyDB 6.2 ACL capability is supported for\n# Sentinel mode, please refer to the Redis website https://redis.io/topics/acl\n# for more details.\n\n# Sentinel's ACL users are defined in the following format:\n#\n#   user <username> ... acl rules ...\n#\n# For example:\n#\n#   user worker +@admin +@connection ~* on >ffa9203c493aa99\n#\n# For more information about ACL configuration please refer to the Redis\n# website at https://redis.io/topics/acl and KeyDB server configuration \n# template keydb.conf.\n\n# ACL LOG\n#\n# The ACL Log tracks failed commands and authentication events associated\n# with ACLs. The ACL Log is useful to troubleshoot failed commands blocked \n# by ACLs. The ACL Log is stored in memory. You can reclaim memory with \n# ACL LOG RESET. Define the maximum entry length of the ACL Log below.\nacllog-max-len 128\n\n# Using an external ACL file\n#\n# Instead of configuring users here in this file, it is possible to use\n# a stand-alone file just listing users. The two methods cannot be mixed:\n# if you configure users here and at the same time you activate the external\n# ACL file, the server will refuse to start.\n#\n# The format of the external ACL user file is exactly the same as the\n# format that is used inside keydb.conf to describe users.\n#\n# aclfile /etc/keydb/sentinel-users.acl\n\n# requirepass <password>\n#\n# You can configure Sentinel itself to require a password, however when doing\n# so Sentinel will try to authenticate with the same password to all the\n# other Sentinels. So you need to configure all your Sentinels in a given\n# group with the same \"requirepass\" password. Check the following documentation\n# for more info: https://redis.io/topics/sentinel\n#\n# IMPORTANT NOTE: starting with KeyDB 6.2 \"requirepass\" is a compatibility\n# layer on top of the ACL system. The option effect will be just setting\n# the password for the default user. Clients will still authenticate using\n# AUTH <password> as usually, or more explicitly with AUTH default <password>\n# if they follow the new protocol: both will work.\n#\n# New config files are advised to use separate authentication control for\n# incoming connections (via ACL), and for outgoing connections (via\n# sentinel-user and sentinel-pass) \n#\n# The requirepass is not compatable with aclfile option and the ACL LOAD\n# command, these will cause requirepass to be ignored.\n\n# sentinel sentinel-user <username>\n#\n# You can configure Sentinel to authenticate with other Sentinels with specific\n# user name. \n\n# sentinel sentinel-pass <password>\n#\n# The password for Sentinel to authenticate with other Sentinels. If sentinel-user\n# is not configured, Sentinel will use 'default' user with sentinel-pass to authenticate.\n\n# sentinel parallel-syncs <master-name> <numreplicas>\n#\n# How many replicas we can reconfigure to point to the new replica simultaneously\n# during the failover. Use a low number if you use the replicas to serve query\n# to avoid that all the replicas will be unreachable at about the same\n# time while performing the synchronization with the master.\nsentinel parallel-syncs mymaster 1\n\n# sentinel failover-timeout <master-name> <milliseconds>\n#\n# Specifies the failover timeout in milliseconds. It is used in many ways:\n#\n# - The time needed to re-start a failover after a previous failover was\n#   already tried against the same master by a given Sentinel, is two\n#   times the failover timeout.\n#\n# - The time needed for a replica replicating to a wrong master according\n#   to a Sentinel current configuration, to be forced to replicate\n#   with the right master, is exactly the failover timeout (counting since\n#   the moment a Sentinel detected the misconfiguration).\n#\n# - The time needed to cancel a failover that is already in progress but\n#   did not produced any configuration change (SLAVEOF NO ONE yet not\n#   acknowledged by the promoted replica).\n#\n# - The maximum time a failover in progress waits for all the replicas to be\n#   reconfigured as replicas of the new master. However even after this time\n#   the replicas will be reconfigured by the Sentinels anyway, but not with\n#   the exact parallel-syncs progression as specified.\n#\n# Default is 3 minutes.\nsentinel failover-timeout mymaster 180000\n\n# SCRIPTS EXECUTION\n#\n# sentinel notification-script and sentinel reconfig-script are used in order\n# to configure scripts that are called to notify the system administrator\n# or to reconfigure clients after a failover. The scripts are executed\n# with the following rules for error handling:\n#\n# If script exits with \"1\" the execution is retried later (up to a maximum\n# number of times currently set to 10).\n#\n# If script exits with \"2\" (or an higher value) the script execution is\n# not retried.\n#\n# If script terminates because it receives a signal the behavior is the same\n# as exit code 1.\n#\n# A script has a maximum running time of 60 seconds. After this limit is\n# reached the script is terminated with a SIGKILL and the execution retried.\n\n# NOTIFICATION SCRIPT\n#\n# sentinel notification-script <master-name> <script-path>\n# \n# Call the specified notification script for any sentinel event that is\n# generated in the WARNING level (for instance -sdown, -odown, and so forth).\n# This script should notify the system administrator via email, SMS, or any\n# other messaging system, that there is something wrong with the monitored\n# KeyDB systems.\n#\n# The script is called with just two arguments: the first is the event type\n# and the second the event description.\n#\n# The script must exist and be executable in order for sentinel to start if\n# this option is provided.\n#\n# Example:\n#\n# sentinel notification-script mymaster /var/keydb/notify.sh\n\n# CLIENTS RECONFIGURATION SCRIPT\n#\n# sentinel client-reconfig-script <master-name> <script-path>\n#\n# When the master changed because of a failover a script can be called in\n# order to perform application-specific tasks to notify the clients that the\n# configuration has changed and the master is at a different address.\n# \n# The following arguments are passed to the script:\n#\n# <master-name> <role> <state> <from-ip> <from-port> <to-ip> <to-port>\n#\n# <state> is currently always \"failover\"\n# <role> is either \"leader\" or \"observer\"\n# \n# The arguments from-ip, from-port, to-ip, to-port are used to communicate\n# the old address of the master and the new address of the elected replica\n# (now a master).\n#\n# This script should be resistant to multiple invocations.\n#\n# Example:\n#\n# sentinel client-reconfig-script mymaster /var/keydb/reconfig.sh\n\n# SECURITY\n#\n# By default SENTINEL SET will not be able to change the notification-script\n# and client-reconfig-script at runtime. This avoids a trivial security issue\n# where clients can set the script to anything and trigger a failover in order\n# to get the program executed.\n\nsentinel deny-scripts-reconfig yes\n\n# KEYDB COMMANDS RENAMING\n#\n# Sometimes the KeyDB server has certain commands, that are needed for Sentinel\n# to work correctly, renamed to unguessable strings. This is often the case\n# of CONFIG and SLAVEOF in the context of providers that provide KeyDB as\n# a service, and don't want the customers to reconfigure the instances outside\n# of the administration console.\n#\n# In such case it is possible to tell Sentinel to use different command names\n# instead of the normal ones. For example if the master \"mymaster\", and the\n# associated replicas, have \"CONFIG\" all renamed to \"GUESSME\", I could use:\n#\n# SENTINEL rename-command mymaster CONFIG GUESSME\n#\n# After such configuration is set, every time Sentinel would use CONFIG it will\n# use GUESSME instead. Note that there is no actual need to respect the command\n# case, so writing \"config guessme\" is the same in the example above.\n#\n# SENTINEL SET can also be used in order to perform this configuration at runtime.\n#\n# In order to set a command back to its original name (undo the renaming), it\n# is possible to just rename a command to itself:\n#\n# SENTINEL rename-command mymaster CONFIG CONFIG\n\n# HOSTNAMES SUPPORT\n#\n# Normally Sentinel uses only IP addresses and requires SENTINEL MONITOR\n# to specify an IP address. Also, it requires the KeyDB replica-announce-ip\n# keyword to specify only IP addresses.\n#\n# You may enable hostnames support by enabling resolve-hostnames. Note\n# that you must make sure your DNS is configured properly and that DNS\n# resolution does not introduce very long delays.\n#\nSENTINEL resolve-hostnames no\n\n# When resolve-hostnames is enabled, Sentinel still uses IP addresses\n# when exposing instances to users, configuration files, etc. If you want\n# to retain the hostnames when announced, enable announce-hostnames below.\n#\nSENTINEL announce-hostnames no\n"
  },
  {
    "path": "pkg/deb/deb-buildsource.sh",
    "content": "#! /bin/bash\n\n# define parameters used in this deb package build\n# enter comments as an argument string quoted. If you do not want comments to be added (ie. you have updated in git repo) type None\nif [ $# -eq 0 ]; then\n        changelog_comments=\"This is a new source build generated from the deb_build_source.sh script\"\nelse\n        changelog_comments=$1\nfi\necho $changelog_comments\nbuild=1 #change if updated build number required. Default is 1 as convention for keydb is to update minor version\nversion=$(grep KEYDB_REAL_VERSION ../../src/version.h | awk '{ printf $3 }' | tr -d \\\")\nmajorv=\"${version:0:1}\"\ndistributor=$(lsb_release --id --short)\nif [ \"$distributor\" == \"Debian\" ]; then\n        distname=+deb$(lsb_release --release --short | cut -d. -f1)u1\nelif [ \"$distributor\" == \"Ubuntu\" ]; then\n        distname=~$(lsb_release --codename --short)1\nfi\ncodename=$(lsb_release --codename --short)\ndate=$(date +%a,\" \"%d\" \"%b\" \"%Y\" \"%T)\n\n# overwrite debian bookworm version until updated\nif [ $codename == \"bookworm\" ]; then\n    distname=+deb12u1\nfi\npkg_name=keydb-$majorv:$version$distname\n\n# create build tree\ncd ../../../\ntar -czvf keydb_$version.orig.tar.gz --force-local KeyDB\ncd KeyDB/pkg/deb/\nmkdir -p $pkg_name/tmp\nif [[ \"$codename\" == \"xenial\" ]] || [[ \"$codename\" == \"stretch\" ]]; then\n\tcp -r debian_dh9 $pkg_name/tmp/debian\nelse\n\tcp -r debian $pkg_name/tmp\nfi\ncp master_changelog $pkg_name/tmp/debian/changelog\nmv ../../../keydb_$version.orig.tar.gz ./$pkg_name\ncd $pkg_name/tmp\nchangelog_str=\"keydb ($majorv:$version-$build$distname) $codename; urgency=medium\\n\\n  * $version $changelog_comments \\n\\n -- Ben Schermel <ben@eqalpha.com>  $date +0000\\n\\n\"\nif [ $# -eq 0 ]; then\n        sed -i \"1s/^/$changelog_str\\n/\" debian/changelog\nelif [ $# -eq 1 ] && [ \"$1\" != \"None\" ]; then\n        sed -i \"1s/^/$changelog_str\\n/\" debian/changelog\nfi\nsed -i \"s/distribution_placeholder/$distname/g\" debian/changelog\nsed -i \"s/codename_placeholder/$codename/g\" debian/changelog\n\n# generate required files and structure for pbuilder including .dsc file\ndebuild -S -sa\ncd ../\n\n# create pbuilder chrooted environment and build the deb package\nif [ \"$codename\" == \"xenial\" ]; then\n        sudo pbuilder create --distribution $codename --othermirror \"deb http://archive.ubuntu.com/ubuntu $codename universe multiverse\"\nelse\n        sudo pbuilder create --distribution $codename\nfi\n\nsudo pbuilder --update\nsudo pbuilder --build *.dsc  --logfile /mnt/pbuilderlog.log\n\n# move new packages to deb_files_generated and clean up\ncp /var/cache/pbuilder/result/*$version*.deb ../deb_files_generated\nsudo pbuilder clean\ncd ../\nrm -rf $pkg_name\n"
  },
  {
    "path": "pkg/deb/deb_files_generated/.gitkeep",
    "content": ""
  },
  {
    "path": "pkg/deb/debian/bash_completion.d/keydb-cli",
    "content": "# -*- sh -*-\n#\n# Bash completion function for the 'keydb-cli' command.\n#\n# Steve\n# --\n# http://www.steve.org.uk\n#\n\n_keydb-cli()\n{\n    COMPREPLY=()\n    cur=${COMP_WORDS[COMP_CWORD]}\n    prev=${COMP_WORDS[COMP_CWORD-1]}\n\n    #\n    #  All known commands accepted.  Sorted.\n    #\n    opts='bgrewriteaof bgsave dbsize debug decr decrby del echo exists expire expireat flushall flushdb get getset incr incrby info keys lastsave lindex llen lpop lpush lrange lrem lset ltrim mget move mset msetnx ping randomkey rename renamenx rewriteaof rpop rpoplpush rpush sadd save scard sdiff sdiffstore select set setnx shutdown sinter sinterstore sismember slaveof smembers smove sort spop srandmember srem sunion sunionstore ttl type zadd zcard zincrby zrange zrangebyscore zrem zremrangebyscore zrevrange zscore'\n\n    #\n    #  Only complete on the first term.\n    #\n    if [ $COMP_CWORD -eq 1 ]; then\n        COMPREPLY=( $(compgen -W \"${opts}\" -- ${cur}) )\n        return 0\n    fi\n\n}\ncomplete -F _keydb-cli keydb-cli\n"
  },
  {
    "path": "pkg/deb/debian/bin/generate-systemd-service-files",
    "content": "#!/bin/sh\n\nSYSTEMD_VER=$(systemctl --version | head -1 | cut -d' ' -f2)\n\nif [ ${SYSTEMD_VER} -lt 237 ]\nthen\n    SYSTEMD_EXTRA=\"\"\nelse\n    SYSTEMD_EXTRA=$(cat <<EOF\n\nRestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX\n#MemoryDenyWriteExecute=true\nProtectKernelModules=true\nProtectKernelTunables=true\nProtectControlGroups=true\nRestrictRealtime=true\nRestrictNamespaces=true\nEOF\n                 )\nfi\n\nfor BINARY in keydb-server keydb-sentinel\ndo\n\tfor MODE in default templated\n\tdo\n\t\tcase \"${BINARY}\" in\n\t\tkeydb-server)\n\t\t\tNAME=\"keydb\"\n\t\t\t;;\n\t\tkeydb-sentinel)\n\t\t\tNAME=\"sentinel\"\n\t\t\t;;\n\t\tesac\n\n\t\tcase \"${MODE}\" in\n\t\tdefault)\n\t\t\tEXTRA=\"Alias=${NAME}.service\"\n\t\t\tTARGET=\"debian/${BINARY}.service\"\n\t\t\tNAMESPACED=\"${NAME}\"\n\t\t\tDESCRIPTION=\"Advanced key-value store\"\n\t\t\t;;\n\t\ttemplated)\n\t\t\tEXTRA=\"\"\n\t\t\tTARGET=\"debian/${BINARY}@.service\"\n\t\t\tNAMESPACED=\"${NAME}-%i\"\n\t\t\tDESCRIPTION=\"Advanced key-value store (%I)\"\n\t\t\t;;\n\t\tesac\n\n\t\t: >${TARGET}\n\n\t\tif [ \"${MODE}\" = \"templated\" ]\n\t\tthen\n\t\t\tcat >> ${TARGET} <<EOF\n# Templated service file for ${BINARY}(1)\n#\n# Each instance of ${BINARY} requires its own configuration file:\n#\n#   $ cp /etc/keydb/${NAME}.conf /etc/keydb/${NAME}-myname.conf\n#   $ chown keydb:keydb /etc/keydb/${NAME}-myname.conf\n#\n# Ensure each instance is using their own database:\n#\n#   $ sed -i -e 's@^dbfilename .*@dbfilename dump-myname.rdb@' /etc/keydb/${NAME}-myname.conf\n#\n# We then listen exlusively on UNIX sockets to avoid TCP port collisions:\n#\n#   $ sed -i -e 's@^port .*@port 0@' /etc/keydb/${NAME}-myname.conf\n#   $ sed -i -e 's@^\\\\(# \\\\)\\\\{0,1\\\\}unixsocket .*@unixsocket /var/run/${NAME}-myname/${BINARY}.sock@' /etc/keydb/${NAME}-myname.conf\n#\n# ... and ensure we are logging, etc. in a unique location:\n#\n#   $ sed -i -e 's@^logfile .*@logfile /var/log/keydb/${BINARY}-myname.log@' /etc/keydb/${NAME}-myname.conf\n#   $ sed -i -e 's@^pidfile .*@pidfile /var/run/keydb-myname/${BINARY}.pid@' /etc/keydb/${NAME}-myname.conf\n#\n# We can then start the service as follows, validating we are using our own\n# configuration:\n#\n#   $ systemctl start ${BINARY}@myname.service\n#   $ keydb-cli -s /var/run/${NAME}-myname/${BINARY}.sock info | grep config_file\nEOF\n\t\tfi\n\n\t\tcat >> ${TARGET} <<EOF\n[Unit]\nDescription=${DESCRIPTION}\nAfter=network.target\nDocumentation=https://docs.keydb.dev, man:${BINARY}(1)\n\n[Service]\nType=forking\nExecStart=/usr/bin/${BINARY} /etc/keydb/${NAMESPACED}.conf\nExecStop=/bin/kill -s TERM \\$MAINPID\nPIDFile=/var/run/${NAMESPACED}/${BINARY}.pid\nTimeoutStopSec=0\nRestart=always\nUser=keydb\nGroup=keydb\nRuntimeDirectory=${NAMESPACED}\nRuntimeDirectoryMode=2755\n\nUMask=007\nPrivateTmp=yes\nLimitNOFILE=65535\nPrivateDevices=yes\nProtectHome=yes\nReadOnlyDirectories=/\nReadWriteDirectories=-/var/lib/keydb\nReadWriteDirectories=-/var/log/keydb\nReadWriteDirectories=-/var/run/${NAMESPACED}\n\nNoNewPrivileges=true\nCapabilityBoundingSet=CAP_SETGID CAP_SETUID CAP_SYS_RESOURCE${SYSTEMD_EXTRA}\n\n# ${BINARY} can write to its own config file when in cluster mode so we\n# permit writing there by default. If you are not using this feature, it is\n# recommended that you replace the following lines with \"ProtectSystem=full\".\nProtectSystem=true\nReadWriteDirectories=-/etc/keydb\n\n[Install]\nWantedBy=multi-user.target\nEOF\n\t\tif [ \"${EXTRA}\" != \"\" ]\n\t\tthen\n\t\t\techo \"${EXTRA}\" >> \"${TARGET}\"\n\t\tfi\n\tdone\ndone\n"
  },
  {
    "path": "pkg/deb/debian/changelog",
    "content": "keydb (5:5.3.3-1~bionic1) bionic; urgency=medium\n\n  * 5.3.3 Updating deb source package and naming convention.\n\n -- Ben Schermel <ben@eqalpha.com>  Fri, 25 Oct 2019 8:00:37 +0000\n\nkeydb (5:5.1.12-1chl1~bionic1) bionic; urgency=medium\n\n  * 5.1.1 update. This update fixes several rare deadlock scenarios. Deadlock detection is also added.  \n\n -- Ben Schermel <ben@eqalpha.com>  Fri, 25 Oct 2019 8:00:37 +0000\n\n\nkeydb (5:5.1.11-1chl1~bionic1) bionic; urgency=medium\n\n  * 5.1 release. This release includes subkey expires (EXPIREMEMBER/EXPIREMEMBERAT), with updates to PTTL/TTL accordingly. New OBJECT LASTMODIFIED, BITIOP LSHIFT & BITOP RSHIFT commands. See https://docs.keydb.dev/blog/2019/10/20/blog-post/ for detailed review of release.\n\n -- Ben Schermel <ben@eqalpha.com>  Mon, 21 Oct 2019 8:00:37 +0000\n\n\nkeydb (5:5.0.1-1chl1~bionic1) bionic; urgency=medium\n\n  * Arm build now included for bionic package\n\n -- Ben Schermel <ben@eqalpha.com>  Wed, 21 Aug 2019 22:58:37 +0000\n\n\nkeydb (5:5.0.0-1chl1~bionic1) bionic; urgency=medium\n\n  * Initial release of KeyDB PPA. This PPA was originally derived from https://launchpad.net/~chris-lea/+archive/ubuntu/redis-server\n\n -- Ben Schermel <ben@eqalpha.com>  Wed, 21 Aug 2019 2:58:37 +0000\n"
  },
  {
    "path": "pkg/deb/debian/compat",
    "content": "9\n"
  },
  {
    "path": "pkg/deb/debian/control",
    "content": "Source: keydb\nSection: database\nPriority: optional\nMaintainer: Ben Schermel <ben@eqalpha.com>\nBuild-Depends:\n debhelper (>= 9~),\n dpkg-dev (>= 1.17.5),\n systemd,\n libsystemd-dev <!nocheck>,\n procps <!nocheck>,\n pkg-config <!nocheck>,\n build-essential <!nocheck>,\n tcl <!nocheck>,\n tcl-dev <!nocheck>,\n uuid-dev <!nocheck>,\n libcurl4-openssl-dev <!nocheck>,\n nasm <!nocheck>,\n autotools-dev <!nocheck>,\n autoconf <!nocheck>,\n libjemalloc-dev <!nocheck>,\n libssl-dev <!nocheck>,\n libsnappy-dev <!nocheck>,\n zlib1g-dev <!nocheck>,\n libbz2-dev <!nocheck>,\n liblz4-dev <!nocheck>,\n libzstd-dev <!nocheck>\nStandards-Version: 4.2.1\nHomepage: https://docs.keydb.dev/\nVcs-Git: https://github.com/Snapchat/KeyDB.git\nVcs-Browser: https://github.com/Snapchat/KeyDB\n\nPackage: keydb\nArchitecture: all\nDepends:\n keydb-server (<< ${binary:Version}.1~),\n keydb-server (>= ${binary:Version}),\n ${misc:Depends},\nDescription: Persistent key-value database with network interface (metapackage)\n keydb is a key-value database in a similar vein to memcache but the dataset\n is non-volatile. keydb additionally provides native support for atomically\n manipulating and querying data structures such as lists and sets.\n .\n The dataset is stored entirely in memory and periodically flushed to disk.\n .\n This package depends on the keydb-server package.\n\nPackage: keydb-sentinel\nArchitecture: any\nDepends:\n lsb-base (>= 3.2-14),\n keydb-tools (= ${binary:Version}),\n ${misc:Depends},\nDescription: Persistent key-value database with network interface (monitoring)\n keydb is a key-value database in a similar vein to memcache but the dataset\n is non-volatile. keydb additionally provides native support for atomically\n manipulating and querying data structures such as lists and sets.\n .\n This package contains the keydb Sentinel monitoring software.\n\nPackage: keydb-server\nArchitecture: any\nDepends:\n lsb-base (>= 3.2-14),\n keydb-tools (= ${binary:Version}),\n ${misc:Depends},\nDescription: Persistent key-value database with network interface\n keydb is a key-value database in a similar vein to memcache but the dataset\n is non-volatile. keydb additionally provides native support for atomically\n manipulating and querying data structures such as lists and sets.\n .\n The dataset is stored entirely in memory and periodically flushed to disk.\n\nPackage: keydb-tools\nArchitecture: any\nDepends:\n adduser,\n ${misc:Depends},\n ${shlibs:Depends},\nDescription: Persistent key-value database with network interface (client)\n keydb is a key-value database in a similar vein to memcache but the dataset\n is non-volatile. keydb additionally provides native support for atomically\n manipulating and querying data structures such as lists and sets.\n .\n This package contains the command line client and other tools.\n"
  },
  {
    "path": "pkg/deb/debian/copyright",
    "content": "Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/\nUpstream-Contact: John Sully <john@eqalpha.com>\nUpstream-Name: KeyDB\nSource: https://github.com/EQ-Alpha/KeyDB\n\nFiles: *\nCopyright: \n © 2006-2014 Salvatore Sanfilippo <antirez@gmail.com>\n © 2019 John Sully\n © 2019-2021 EQ Alpha Technology Ltd.\n © 2021 Snap Inc.\n\nLicense: BSD-3-clause\n\nFiles:\n src/rio.*\n src/t_zset.c\n src/ziplist.h\n src/intset.*\n src/redis-check-aof.c\n deps/hiredis/*\n deps/linenoise/*\nCopyright:\n © 2009-2012 Pieter Noordhuis <pcnoordhuis@gmail.com>\n © 2009-2012 Salvatore Sanfilippo <antirez@gmail.com>\nLicense: BSD-3-clause\n\nFiles:\n src/lzf.h\n src/lzfP.h\n src/lzf_d.c\n src/lzf_c.c\nCopyright:\n © 2000-2007 Marc Alexander Lehmann <schmorp@schmorp.de>\n © 2009-2012 Salvatore Sanfilippo <antirez@gmail.com>\nLicense: BSD-2-clause\n\nFiles: src/setproctitle.c\nCopyright:\n © 2010 William Ahern\n © 2013 Salvatore Sanfilippo\n © 2013 Stam He\nLicense: BSD-3-clause\n\nFiles: src/ae_evport.c\nCopyright: © 2012 Joyent, Inc.\nLicense: BSD-3-clause\n\nFiles: src/ae_kqueue.c\nCopyright: © 2009 Harish Mallipeddi <harish.mallipeddi@gmail.com>\nLicense: BSD-3-clause\n\nFiles: utils/install_server.sh\nCopyright: © 2011 Dvir Volk <dvirsk@gmail.com>\nLicense: BSD-3-clause\n\nFiles: deps/jemalloc/*\nCopyright:\n © 2002-2012 Jason Evans <jasone@canonware.com>\n © 2007-2012 Mozilla Foundation\n © 2009-2012 Facebook, Inc.\nLicense: BSD-3-clause\n\nFiles: src/pqsort.*\nCopyright: © 1992-1993 The Regents of the University of California\nLicense: BSD-3-clause\n\nFiles: deps/lua/*\nCopyright: © 1994-2012 Lua.org, PUC-Ri\nLicense: MIT\n Permission is hereby granted, free of charge, to any person obtaining a copy\n of this software and associated documentation files (the \"Software\"), to deal\n in the Software without restriction, including without limitation the rights\n to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n copies of the Software, and to permit persons to whom the Software is\n furnished to do so, subject to the following conditions:\n .\n The above copyright notice and this permission notice shall be included in\n all copies or substantial portions of the Software.\n .\n THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n THE SOFTWARE.\n\nFiles: pkg/deb/debian/*\nCopyright:\n © 2009 Chris Lamb <lamby@debian.org>\n © 2020-2021 EQ Alpha Technology Ltd. <support@eqalpha.com>\n © 2021 Snap Inc.\nLicense: BSD-3-clause\n\nLicense: BSD-2-clause\n Redistribution and use in source and binary forms, with or without\n modification, are permitted provided that the following conditions are met:\n .\n 1. Redistributions of source code must retain the above copyright notice, this\n list of conditions and the following disclaimer.\n .\n 2. Redistributions in binary form must reproduce the above copyright notice,\n this list of conditions and the following disclaimer in the documentation\n and/or other materials provided with the distribution.\n .\n THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\n FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\n CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\n OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\nLicense: BSD-3-clause\n Redistribution and use in source and binary forms, with or without\n modification, are permitted provided that the following conditions\n are met:\n .\n    * Redistributions of source code must retain the above copyright\n      notice, this list of conditions and the following disclaimer.\n    * Redistributions in binary form must reproduce the above copyright\n      notice, this list of conditions and the following disclaimer in the\n      documentation and/or other materials provided with the distribution.\n    * Neither the name of Redis nor the names of its contributors may be\n      used to endorse or promote products derived from this software\n      without specific prior written permission.\n .\n THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE\n FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\n CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\n OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
  },
  {
    "path": "pkg/deb/debian/keydb-benchmark.1",
    "content": ".TH KEYDB-BENCHMARK 1 \"August 17, 2019\"\n.SH NAME\nkeydb-benchmark \\- Benchmark a KeyDB instance\n.SH SYNOPSIS\n.B redis-benchmark\n[\\-h <host>] [\\-p <port>] [\\-c <clients>] [\\-n <requests]> [\\-k <boolean>]\n.SH DESCRIPTION\nKeyDB is a key-value database. It is similar to memcached but the dataset is\nnot volatile and other datatypes (such as lists and sets) are natively\nsupported.\n.SH OPTIONS\n.TP\n\\-h hostname\nServer hostname (default 127.0.0.1)\n.TP\n\\-p port\nServer port (default 6379)\n.TP\n\\-s socket\nServer socket (overrides host and port)\n.TP\n\\-a password\nPassword for KeyDB Auth\n.TP \n\\-c clients\nNumber of parallel connections (default 50)\n.TP\n\\-n requests\nTotal number of requests (default 100000)\n.TP\n\\-d size\nData size of SET/GET value in bytes (default 2)\n.TP\n\\-dbnum db\nSELECT the specified db number (default 0)\n.TP\n\\-k boolean\n1=keep alive 0=reconnect (default 1)\n.TP\n\\-r keyspacelen\nUse random keys for SET/GET/INCR, random values for SADD Using this option the\nbenchmark will get/set keys in the form mykey_rand000000012456 instead of\nconstant keys, the <keyspacelen> argument determines the max number of values\nfor the random number. For instance if set to 10 only rand000000000000 -\nrand000000000009 range will be allowed.\n.TP\n\\-P numreq\nPipeline <numreq> requests. Default 1 (no pipeline).\n.TP\n\\-q\nQuiet. Just show query/sec values\n.TP\n\\-\\-csv\nOurput in CSV format\n.TP\n\\-l\nLoop. Run the tests forever\n.TP\n\\-I\nIdle mode. Just open N idle connections and wait.\n.TP\n\\-D\nDebug mode. more verbose.\n.SH AUTHOR\n\\fBkeydb-benchmark\\fP was written by John Sully, originating as a fork of Redis. Redis was written by Salvatore Sanfilippo.\n.PP\nThis manual page was written by Chris Lamb <lamby@debian.org> for the Debian\nproject (but may be used by others). Modified by Ben Schermel <ben@eqalpha.com>\n"
  },
  {
    "path": "pkg/deb/debian/keydb-check-aof.1",
    "content": ".TH KEYDB-CHECK-AOF 1 \"August 17, 2019\"\n.SH NAME\nkeydb-check-aof \\- Check integrity of a KeyDB .AOF file\n.SH SYNOPSIS\n.B keydb-check-aof\nfilename\n.SH DESCRIPTION\nKeyDB is a key-value database. It is similar to memcached but the dataset is\nnot volatile and other datatypes (such as lists and sets) are natively\nsupported.\n.PP\nThis utility checks the integrity of a dumped .AOF file.\n.SH AUTHOR\n\\fBkeydb-check-aof\\fP was written by John Sully, originating as a fork of Redis. Redis was written by Salvatore Sanfilippo.\n.PP\nThis manual page was written by Chris Lamb <lamby@debian.org> for the Debian\nproject (but may be used by others). Modified by Ben Schermel <ben@eqalpha.com>\n"
  },
  {
    "path": "pkg/deb/debian/keydb-check-rdb.1",
    "content": ".TH KEYDB-CHECK-RDB 1 \"August 17, 2019\"\n.SH NAME\nkeydb-check-rdb \\- Check integrity of KeyDB dumped database file\n.SH SYNOPSIS\n.B keydb-check-rdb\nfilename\n.SH DESCRIPTION\nKeyDB is a key-value database. It is similar to memcached but the dataset is\nnot volatile and other datatypes (such as lists and sets) are natively\nsupported.\n.PP\nThis utility checks the integrity of a dumped database file.\n.SH AUTHOR\n\\fBredis-check-rdb\\fP was written by John Sully, originating as a fork of Redis. Redis was written by Salvatore Sanfilippo.\n.PP\nThis manual page was written by Chris Lamb <lamby@debian.org> for the Debian\nproject (but may be used by others). Modified by Ben Schermel <ben@eqalpha.com>\n"
  },
  {
    "path": "pkg/deb/debian/keydb-cli.1",
    "content": ".TH KEYDB-CLI 1 \"August 17, 2019\"\n.SH NAME\nkeydb-cli \\- Command-line client to keydb-server\n.SH SYNOPSIS\n.B keydb-cli\n.RI [options]\n.SH DESCRIPTION\nKeyDB is a key-value database. It is similar to memcached but the dataset is\nnot volatile and other datatypes (such as lists and sets) are natively\nsupported.\n.PP\n\\fBkeydb-cli\\fP provides a simple command-line interface to a KeyDB server.\n.SH OPTIONS\nSee \\fBkeydb-doc\\fP for more information on the commands KeyDB accepts.\n.SH AUTHOR\n\\fBkeydb-cli\\fP was written by John Sully, originating as a fork of Redis. Redis was written by Salvatore Sanfilippo.\n.PP\nThis manual page was written by Chris Lamb <lamby@debian.org> for the Debian\nproject (but may be used by others). Modified by Ben Schermel <ben@eqalpha.com>\n"
  },
  {
    "path": "pkg/deb/debian/keydb-sentinel.1",
    "content": ".TH KEYDB-SENTINEL 1 \"August 17, 2019\"\n.SH NAME\nkeydb-sentinel \\- Persistent key-value database (cluster mode)\n.SH SYNOPSIS\n.B keydb-sentinel\n.RI configfile\n.SH DESCRIPTION\nKeyDB is a key-value database. It is similar to memcached but the dataset is\nnot volatile and other datatypes (such as lists and sets) are natively\nsupported.\n.PP\n.SH OPTIONS\n.IP \"configfile\"\nRead options from specified configuration file.\n.SH NOTES\nOn Debian GNU/Linux systems, \\fBkeydb-sentinel\\fP is typically started via the\n\\fB/etc/init.d/keydb-sentinel\\fP initscript, not manually. This defaults to using\n\\fB/etc/keydb/sentinel.conf\\fP as a configuration file.\n.SH AUTHOR\n\\fBkeydb-sentinel\\fP was written by John Sully, originating as a fork of Redis. Redis was written by Salvatore Sanfilippo.\n.PP\nThis manual page was written by Chris Lamb <lamby@debian.org> for the Debian\nproject (but may be used by others). Modified by Ben Schermel <ben@eqalpha.com>\n"
  },
  {
    "path": "pkg/deb/debian/keydb-sentinel.install",
    "content": "debian/keydb-sentinel.service /lib/systemd/system/\npkg/deb/conf/sentinel.conf\t/etc/keydb\n"
  },
  {
    "path": "pkg/deb/debian/keydb-sentinel.logrotate",
    "content": "/var/log/keydb/keydb-sentinel*.log {\n        weekly\n        missingok\n        rotate 12\n        compress\n        notifempty\n}\n"
  },
  {
    "path": "pkg/deb/debian/keydb-sentinel.manpages",
    "content": "debian/keydb-sentinel.1\n"
  },
  {
    "path": "pkg/deb/debian/keydb-sentinel.postinst",
    "content": "#!/bin/sh\n\nset -eu\n\nUSER=\"keydb\"\nGROUP=\"$USER\"\nCONFFILE=\"/etc/keydb/sentinel.conf\"\n\nif [ \"$1\" = \"configure\" ]\nthen\n\tif ! dpkg-statoverride --list ${CONFFILE} >/dev/null 2>&1\n\tthen\n\t\tdpkg-statoverride --update --add ${USER} ${GROUP} 640 ${CONFFILE}\n\tfi\nfi\n\n#DEBHELPER#\n\nif [ \"$1\" = \"configure\" ]\nthen\n\tfind /etc/keydb -maxdepth 1 -type d -name 'keydb-sentinel.*.d' -empty -delete\nfi\n\nsystemctl daemon-reload\n\nexit 0\n"
  },
  {
    "path": "pkg/deb/debian/keydb-sentinel.postrm",
    "content": "#!/bin/sh\n\nset -eu\n\nCONFFILE=\"/etc/keydb/sentinel.conf\"\n\nif [ \"$1\" = \"purge\" ]\nthen\n\tdpkg-statoverride --remove ${CONFFILE} || test $? -eq 2\nfi\n\n#DEBHELPER#\n\nsystemctl daemon-reload\n\nexit 0\n"
  },
  {
    "path": "pkg/deb/debian/keydb-server.1",
    "content": ".TH KEYDB-SERVER 1 \"August 17, 2019\"\n.SH NAME\nkeydb-server \\- Persistent key-value database\n.SH SYNOPSIS\n.B keydb-server\n.RI configfile\n.SH DESCRIPTION\nKeyDB is a key-value database. It is similar to memcached but the dataset is\nnot volatile and other datatypes (such as lists and sets) are natively\nsupported.\n.PP\n.SH OPTIONS\n.IP \"configfile\"\nRead options from specified configuration file.\n.SH NOTES\nOn Debian GNU/Linux systems, \\fBkeydb-server\\fP is typically started via the\n\\fB/etc/init.d/keydb-server\\fP initscript, not manually. This defaults to using\n\\fB/etc/keydb/keydb.conf\\fP as a configuration file.\n.SH AUTHOR\n\\fBkeydb-server\\fP was written by John Sully, originating as a fork of Redis. Redis was written by Salvatore Sanfilippo.\n.PP\nThis manual page was written by Chris Lamb <lamby@debian.org> for the Debian\nproject (but may be used by others). Modified by Ben Schermel <ben@eqalpha.com>\n"
  },
  {
    "path": "pkg/deb/debian/keydb-server.docs",
    "content": "README.md\n"
  },
  {
    "path": "pkg/deb/debian/keydb-server.install",
    "content": "debian/keydb-server.service /lib/systemd/system/\npkg/deb/conf/keydb.conf\t/etc/keydb\n"
  },
  {
    "path": "pkg/deb/debian/keydb-server.logrotate",
    "content": "/var/log/keydb/keydb-server*.log {\n        weekly\n        missingok\n        rotate 12\n        compress\n        notifempty\n}\n"
  },
  {
    "path": "pkg/deb/debian/keydb-server.manpages",
    "content": "debian/keydb-server.1\n"
  },
  {
    "path": "pkg/deb/debian/keydb-server.postinst",
    "content": "#!/bin/sh\n\nset -eu\n\nUSER=\"keydb\"\nGROUP=\"$USER\"\nCONFFILE=\"/etc/keydb/keydb.conf\"\n\nif [ \"$1\" = \"configure\" ]\nthen\n\tif ! dpkg-statoverride --list ${CONFFILE} >/dev/null 2>&1\n\tthen\n\t\tdpkg-statoverride --update --add ${USER} ${GROUP} 640 ${CONFFILE}\n\tfi\nfi\n\n#DEBHELPER#\n\nif [ \"$1\" = \"configure\" ]\nthen\n\tfind /etc/keydb -maxdepth 1 -type d -name 'keydb-server.*.d' -empty -delete\nfi\n\nsystemctl daemon-reload\n\nexit 0\n"
  },
  {
    "path": "pkg/deb/debian/keydb-server.postrm",
    "content": "#!/bin/sh\n\nset -eu\n\nCONFFILE=\"/etc/keydb/keydb.conf\"\n\nif [ \"${1}\" = \"purge\" ]\nthen\n\tdpkg-statoverride --remove ${CONFFILE} || test $? -eq 2\nfi\n\n#DEBHELPER#\n\nsystemctl daemon-reload\n\nexit 0\n"
  },
  {
    "path": "pkg/deb/debian/keydb-tools.examples",
    "content": "src/redis-trib.rb\nutils/lru\n"
  },
  {
    "path": "pkg/deb/debian/keydb-tools.install",
    "content": "debian/bash_completion.d/*\t/usr/share/bash-completion/completions\nsrc/keydb-benchmark\t/usr/bin\nsrc/keydb-check-aof\t/usr/bin\nsrc/keydb-check-rdb\t/usr/bin\nsrc/keydb-cli\t\t/usr/bin\nsrc/keydb-server\t/usr/bin\nsrc/keydb-sentinel\t/usr/bin\nsrc/keydb-diagnostic-tool\t/usr/bin\n"
  },
  {
    "path": "pkg/deb/debian/keydb-tools.manpages",
    "content": "debian/keydb-benchmark.1\ndebian/keydb-check-aof.1\ndebian/keydb-check-rdb.1\ndebian/keydb-cli.1\n"
  },
  {
    "path": "pkg/deb/debian/keydb-tools.postinst",
    "content": "#!/bin/sh\n\nset -eu\n\nUSER=\"keydb\"\n\nSetup_dir () {\n\tDIR=\"${1}\"\n\tMODE=\"${2}\"\n\tGROUP=\"${3}\"\n\n\tmkdir -p ${DIR}\n\n\tcase \"${DIR}\" in\n\t/var/log/keydb)\n\t\tMODE=\"02750\"\n\t\tGROUP=\"adm\"\n\t\t;;\n\t*)\n\t\tMODE=\"750\"\n\t\tGROUP=\"${USER}\"\n\t\t;;\n\tesac\n\n\tif ! dpkg-statoverride --list ${DIR} >/dev/null 2>&1\n\tthen\n\t\tchown ${USER}:${GROUP} ${DIR}\n\t\tchmod ${MODE} ${DIR}\n\tfi\n}\n\nif [ \"$1\" = \"configure\" ]\nthen\n\tadduser \\\n\t\t--system \\\n\t\t--home /var/lib/keydb \\\n\t\t--quiet \\\n\t\t--group \\\n\t\t${USER} || true\n\n\tSetup_dir /var/log/keydb ${USER}:adm 2750\n\tSetup_dir /var/lib/keydb ${USER}:${USER} 750\nfi\n\n#DEBHELPER#\n\nexit 0\n"
  },
  {
    "path": "pkg/deb/debian/keydb-tools.postrm",
    "content": "#!/bin/sh\n\nset -eu\n\nif [ \"${1}\" = \"purge\" ]\nthen\n\tuserdel keydb || true\n\trm -rf /var/lib/keydb /var/log/keydb\nfi\n\n#DEBHELPER#\n\nexit 0\n"
  },
  {
    "path": "pkg/deb/debian/rules",
    "content": "#!/usr/bin/make -f\n\ninclude /usr/share/dpkg/buildflags.mk\n\nexport BUILD_TLS=yes\nexport USE_SYSTEMD=yes\nexport ENABLE_FLASH=yes\nexport CFLAGS CPPFLAGS LDFLAGS\nexport DEB_BUILD_MAINT_OPTIONS = hardening=+all\nexport DEB_LDFLAGS_MAINT_APPEND = -ldl -latomic $(LUA_LDFLAGS)\n\nifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))\n\tNUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))\n\tMAKEFLAGS += -j$(NUMJOBS)\n\texport MAKEFLAGS\nendif\n\n%:\n\tdh $@\n\noverride_dh_auto_install:\n\tdebian/bin/generate-systemd-service-files\n\tdh_installsystemd --restart-after-upgrade\n\noverride_dh_auto_test:\nifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))\n\t# Avoid race conditions in upstream testsuite.\n\t./runtest || true\n\t./runtest-cluster || true\n\t./runtest-sentinel || true\nendif\n\noverride_dh_auto_clean:\n\tdh_auto_clean\n\trm -f src/release.h debian/*.service\n\noverride_dh_compress:\n\tdh_compress -Xredis-trib.rb\n\noverride_dh_installchangelogs:\n\tdh_installchangelogs --keep 00-RELEASENOTES\n"
  },
  {
    "path": "pkg/deb/debian/source/format",
    "content": "3.0 (quilt)\n"
  },
  {
    "path": "pkg/deb/debian/source/lintian-overrides",
    "content": "# Upstream do not provide signed tarballs.\nkeydb-pro source: debian-watch-does-not-check-gpg-signature\n"
  },
  {
    "path": "pkg/deb/debian/tests/0001-keydb-cli",
    "content": "#!/bin/sh\n#\n# Show the INFO from \"keydb-cli\"\n\nset -eu\n\nkeydb-cli INFO\nkeydb-cli LOLWUT\n"
  },
  {
    "path": "pkg/deb/debian/tests/0002-benchmark",
    "content": "#!/bin/sh\n#\n# Run the benchmarking\n\nset -eu\n\nkeydb-benchmark -P 10\n"
  },
  {
    "path": "pkg/deb/debian/tests/0003-keydb-check-aof",
    "content": "#!/bin/sh\n#\n# Smoke test keydb-check-aof\n\nkeydb-check-aof 2>&1 | grep -qsi usage:\n"
  },
  {
    "path": "pkg/deb/debian/tests/0004-keydb-check-rdb",
    "content": "#!/bin/sh\n#\n# Test keydb-check-rdb\n\nset -eu\n\n# Perform a synchronous save to ensure .rdb file eixsts\nkeydb-cli SAVE\n\nkeydb-check-rdb /var/lib/keydb/dump.rdb\n"
  },
  {
    "path": "pkg/deb/debian/tests/control",
    "content": "Tests: 0001-keydb-cli\n\nTests: 0002-benchmark\n\nTests: 0003-keydb-check-aof\n\nTests: 0004-keydb-check-rdb\nRestrictions: needs-root\n"
  },
  {
    "path": "pkg/deb/debian/watch",
    "content": "version=6\nopts=uversionmangle=s/-?(alpha|beta|rc)/~$1/ \\\n\thttps://github.com/EQ-Alpha/KeyDB/releases .*/archive/(.*).tar.gz\n"
  },
  {
    "path": "pkg/deb/debian/zsh-completion/_keydb-cli",
    "content": "#compdef keydb-cli\nlocal -a options\noptions=(\n  '-h[Server hostname (default: 127.0.0.1).]: :_hosts'\n  '-p[Server port (default: 6379).]'\n  '-s[Server socket (overrides hostname and port).]'\n  '-a[Password to use when connecting to the server. You can also use the REDISCLI_AUTH environment variable to pass this password more safely (if both are used, this argument takes precedence).]'\n  '--user[Used to send ACL style \"AUTH username pass\". Needs -a.]'\n  '--pass[Alias of -a for consistency with the new --user option.]'\n  '--askpass[Force user to input password with mask from STDIN. If this argument is used, \"-a\" and REDISCLI_AUTH environment variable will be ignored.]'\n  '-u[Server URI.]'\n  '-r[Execute specified command N times.]'\n  '-i[When -r is used, waits <interval> seconds per command. It is possible to specify sub-second times like -i 0.1.]'\n  '-n[Database number.]'\n  '-3[Start session in RESP3 protocol mode.]'\n  '-x[Read last argument from STDIN.]'\n  '-d[Delimiter between response bulks for raw formatting (default: \\n).]'\n  '-D[D <delimiter>     Delimiter between responses for raw formatting (default: \\n).]'\n  '-c[Enable cluster mode (follow -ASK and -MOVED redirections).]'\n  '-e[Return exit error code when command execution fails.]'\n  '--raw[Use raw formatting for replies (default when STDOUT is not a tty).]'\n  '--no-raw[Force formatted output even when STDOUT is not a tty.]'\n  '--quoted-input[Force input to be handled as quoted strings.]'\n  '--csv[Output in CSV format.]'\n  '--show-pushes[Whether to print RESP3 PUSH messages.  Enabled by default when STDOUT is a tty but can be overriden with --show-pushes no.]'\n  '--stat[Print rolling stats about server: mem, clients, ...]'\n  '--latency[Enter a special mode continuously sampling latency. If you use this mode in an interactive session it runs forever displaying real-time stats. Otherwise if --raw or --csv is specified, or if you redirect the output to a non TTY, it samples the latency for 1 second (you can use -i to change the interval), then produces a single output and exits.]'\n  '--latency-history[Like --latency but tracking latency changes over time. Default time interval is 15 sec. Change it using -i.]'\n  '--latency-dist[Shows latency as a spectrum, requires xterm 256 colors. Default time interval is 1 sec. Change it using -i.]'\n  '--lru-test[Simulate a cache workload with an 80-20 distribution.]'\n  '--replica[Simulate a replica showing commands received from the master.]'\n  '--rdb[Transfer an RDB dump from remote server to local file.]'\n  '--pipe[Transfer raw KeyDB protocol from stdin to server.]'\n  '--pipe-timeout[In --pipe mode, abort with error if after sending all data. no reply is received within <n> seconds. Default timeout: 30. Use 0 to wait forever.]'\n  '--bigkeys[Sample KeyDB keys looking for keys with many elements (complexity).]'\n  '--memkeys[Sample KeyDB keys looking for keys consuming a lot of memory.]'\n  '--memkeys-samples[Sample KeyDB keys looking for keys consuming a lot of memory. And define number of key elements to sample]'\n  '--hotkeys[Sample KeyDB keys looking for hot keys. only works when maxmemory-policy is *lfu.]'\n  '--scan[List all keys using the SCAN command.]'\n  '--pattern[Keys pattern when using the --scan, --bigkeys or --hotkeys options (default: *).]'\n  '--quoted-pattern[Same as --pattern, but the specified string can be quoted, in order to pass an otherwise non binary-safe string.]'\n  '--intrinsic-latency[Run a test to measure intrinsic system latency. The test will run for the specified amount of seconds.]'\n  '--eval[Send an EVAL command using the Lua script at <file>.]'\n  '--ldb[Used with --eval enable the Redis Lua debugger.]'\n  '--ldb-sync-mode[Like --ldb but uses the synchronous Lua debugger, in this mode the server is blocked and script changes are not rolled back from the server memory.]'\n  '--cluster[<command> args... opts... Cluster Manager command and arguments (see below).]'\n  '--verbose[Verbose mode.]'\n  '--no-auth-warning[Dont show warning message when using password on command line interface.]'\n  '--help[Output this help and exit.]'\n   '--version[Output version and exit.]'\n)\n\n_arguments -s $options\n"
  },
  {
    "path": "pkg/deb/debian_dh9/NEWS",
    "content": "keydb (4:4.0.2-3) unstable; urgency=medium\n\n  This version drops the Debian-specific support for the\n  /etc/keydb/keydb-{server,sentinel}.{pre,post}-{up,down}.d directories in\n  favour of using systemd's ExecStartPre, ExecStartPost, ExecStopPre,\n  ExecStopPost commands.\n\n -- Chris Lamb <lamby@debian.org>  Wed, 11 Oct 2017 22:55:00 -0400\n"
  },
  {
    "path": "pkg/deb/debian_dh9/bash_completion.d/keydb-cli",
    "content": "# -*- sh -*-\n#\n# Bash completion function for the 'keydb-cli' command.\n#\n# Steve\n# --\n# http://www.steve.org.uk\n#\n\n_keydb-cli()\n{\n    COMPREPLY=()\n    cur=${COMP_WORDS[COMP_CWORD]}\n    prev=${COMP_WORDS[COMP_CWORD-1]}\n\n    #\n    #  All known commands accepted.  Sorted.\n    #\n    opts='bgrewriteaof bgsave dbsize debug decr decrby del echo exists expire expireat flushall flushdb get getset incr incrby info keys lastsave lindex llen lpop lpush lrange lrem lset ltrim mget move mset msetnx ping randomkey rename renamenx rewriteaof rpop rpoplpush rpush sadd save scard sdiff sdiffstore select set setnx shutdown sinter sinterstore sismember slaveof smembers smove sort spop srandmember srem sunion sunionstore ttl type zadd zcard zincrby zrange zrangebyscore zrem zremrangebyscore zrevrange zscore'\n\n    #\n    #  Only complete on the first term.\n    #\n    if [ $COMP_CWORD -eq 1 ]; then\n        COMPREPLY=( $(compgen -W \"${opts}\" -- ${cur}) )\n        return 0\n    fi\n\n}\ncomplete -F _keydb-cli keydb-cli\n"
  },
  {
    "path": "pkg/deb/debian_dh9/bin/generate-systemd-service-files",
    "content": "#!/bin/sh\n\nSYSTEMD_VER=$(systemctl --version | head -1 | cut -d' ' -f2)\n\nif [ ${SYSTEMD_VER} -lt 237 ]\nthen\n    SYSTEMD_EXTRA=\"\"\nelse\n    SYSTEMD_EXTRA=$(cat <<EOF\n\nRestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX\n#MemoryDenyWriteExecute=true\nProtectKernelModules=true\nProtectKernelTunables=true\nProtectControlGroups=true\nRestrictRealtime=true\nRestrictNamespaces=true\nEOF\n                 )\nfi\n\nfor BINARY in keydb-server keydb-sentinel\ndo\n\tfor MODE in default templated\n\tdo\n\t\tcase \"${BINARY}\" in\n\t\tkeydb-server)\n\t\t\tNAME=\"keydb\"\n\t\t\t;;\n\t\tkeydb-sentinel)\n\t\t\tNAME=\"sentinel\"\n\t\t\t;;\n\t\tesac\n\n\t\tcase \"${MODE}\" in\n\t\tdefault)\n\t\t\tEXTRA=\"Alias=${NAME}.service\"\n\t\t\tTARGET=\"debian/${BINARY}.service\"\n\t\t\tNAMESPACED=\"${NAME}\"\n\t\t\tDESCRIPTION=\"Advanced key-value store\"\n\t\t\t;;\n\t\ttemplated)\n\t\t\tEXTRA=\"\"\n\t\t\tTARGET=\"debian/${BINARY}@.service\"\n\t\t\tNAMESPACED=\"${NAME}-%i\"\n\t\t\tDESCRIPTION=\"Advanced key-value store (%I)\"\n\t\t\t;;\n\t\tesac\n\n\t\t: >${TARGET}\n\n\t\tif [ \"${MODE}\" = \"templated\" ]\n\t\tthen\n\t\t\tcat >> ${TARGET} <<EOF\n# Templated service file for ${BINARY}(1)\n#\n# Each instance of ${BINARY} requires its own configuration file:\n#\n#   $ cp /etc/keydb/${NAME}.conf /etc/keydb/${NAME}-myname.conf\n#   $ chown keydb:keydb /etc/keydb/${NAME}-myname.conf\n#\n# Ensure each instance is using their own database:\n#\n#   $ sed -i -e 's@^dbfilename .*@dbfilename dump-myname.rdb@' /etc/keydb/${NAME}-myname.conf\n#\n# We then listen exlusively on UNIX sockets to avoid TCP port collisions:\n#\n#   $ sed -i -e 's@^port .*@port 0@' /etc/keydb/${NAME}-myname.conf\n#   $ sed -i -e 's@^\\\\(# \\\\)\\\\{0,1\\\\}unixsocket .*@unixsocket /var/run/${NAME}-myname/${BINARY}.sock@' /etc/keydb/${NAME}-myname.conf\n#\n# ... and ensure we are logging, etc. in a unique location:\n#\n#   $ sed -i -e 's@^logfile .*@logfile /var/log/keydb/${BINARY}-myname.log@' /etc/keydb/${NAME}-myname.conf\n#   $ sed -i -e 's@^pidfile .*@pidfile /var/run/keydb-myname/${BINARY}.pid@' /etc/keydb/${NAME}-myname.conf\n#\n# We can then start the service as follows, validating we are using our own\n# configuration:\n#\n#   $ systemctl start ${BINARY}@myname.service\n#   $ keydb-cli -s /var/run/${NAME}-myname/${BINARY}.sock info | grep config_file\n#\n#  -- Chris Lamb <lamby@debian.org>  Mon, 09 Oct 2017 22:17:24 +0100\nEOF\n\t\tfi\n\n\t\tcat >> ${TARGET} <<EOF\n[Unit]\nDescription=${DESCRIPTION}\nAfter=network.target\nDocumentation=https://docs.keydb.dev, man:${BINARY}(1)\n\n[Service]\nType=forking\nExecStart=/usr/bin/${BINARY} /etc/keydb/${NAMESPACED}.conf\nExecStop=/bin/kill -s TERM \\$MAINPID\nPIDFile=/var/run/${NAMESPACED}/${BINARY}.pid\nTimeoutStopSec=0\nRestart=always\nUser=keydb\nGroup=keydb\nRuntimeDirectory=${NAMESPACED}\nRuntimeDirectoryMode=2755\n\nUMask=007\nPrivateTmp=yes\nLimitNOFILE=65535\nPrivateDevices=yes\nProtectHome=yes\nReadOnlyDirectories=/\nReadWriteDirectories=-/var/lib/keydb\nReadWriteDirectories=-/var/log/keydb\nReadWriteDirectories=-/var/run/${NAMESPACED}\n\nNoNewPrivileges=true\nCapabilityBoundingSet=CAP_SETGID CAP_SETUID CAP_SYS_RESOURCE${SYSTEMD_EXTRA}\n\n# ${BINARY} can write to its own config file when in cluster mode so we\n# permit writing there by default. If you are not using this feature, it is\n# recommended that you replace the following lines with \"ProtectSystem=full\".\nProtectSystem=true\nReadWriteDirectories=-/etc/keydb\n\n[Install]\nWantedBy=multi-user.target\nEOF\n\t\tif [ \"${EXTRA}\" != \"\" ]\n\t\tthen\n\t\t\techo \"${EXTRA}\" >> \"${TARGET}\"\n\t\tfi\n\tdone\ndone\n"
  },
  {
    "path": "pkg/deb/debian_dh9/changelog",
    "content": "keydb (5:5.3.3-1~bionic1) bionic; urgency=medium\n\n  * 5.3.3 Updating deb source package and naming convention.\n\n -- Ben Schermel <ben@eqalpha.com>  Fri, 25 Oct 2019 8:00:37 +0000\n\nkeydb (5:5.1.12-1chl1~bionic1) bionic; urgency=medium\n\n  * 5.1.1 update. This update fixes several rare deadlock scenarios. Deadlock detection is also added.  \n\n -- Ben Schermel <ben@eqalpha.com>  Fri, 25 Oct 2019 8:00:37 +0000\n\n\nkeydb (5:5.1.11-1chl1~bionic1) bionic; urgency=medium\n\n  * 5.1 release. This release includes subkey expires (EXPIREMEMBER/EXPIREMEMBERAT), with updates to PTTL/TTL accordingly. New OBJECT LASTMODIFIED, BITIOP LSHIFT & BITOP RSHIFT commands. See https://docs.keydb.dev/blog/2019/10/20/blog-post/ for detailed review of release.\n\n -- Ben Schermel <ben@eqalpha.com>  Mon, 21 Oct 2019 8:00:37 +0000\n\n\nkeydb (5:5.0.1-1chl1~bionic1) bionic; urgency=medium\n\n  * Arm build now included for bionic package\n\n -- Ben Schermel <ben@eqalpha.com>  Wed, 21 Aug 2019 22:58:37 +0000\n\n\nkeydb (5:5.0.0-1chl1~bionic1) bionic; urgency=medium\n\n  * Initial release of KeyDB PPA. This PPA was originally derived from https://launchpad.net/~chris-lea/+archive/ubuntu/redis-server\n\n -- Ben Schermel <ben@eqalpha.com>  Wed, 21 Aug 2019 2:58:37 +0000\n"
  },
  {
    "path": "pkg/deb/debian_dh9/compat",
    "content": "9\n"
  },
  {
    "path": "pkg/deb/debian_dh9/control",
    "content": "Source: keydb\nSection: database\nPriority: optional\nMaintainer: Ben Schermel <ben@eqalpha.com>\nBuild-Depends:\n debhelper (>= 9~),\n dpkg-dev (>= 1.17.5),\n systemd,\n libsystemd-dev <!nocheck>,\n procps <!nocheck>,\n build-essential <!nocheck>,\n tcl <!nocheck>,\n tcl-dev <!nocheck>,\n uuid-dev <!nocheck>,\n libcurl4-openssl-dev <!nocheck>,\n nasm <!nocheck>,\n autotools-dev <!nocheck>,\n autoconf <!nocheck>,\n libjemalloc-dev <!nocheck>,\n libssl-dev <!nocheck>,\n libsnappy-dev <!nocheck>,\n zlib1g-dev <!nocheck>,\n libbz2-dev <!nocheck>,\n liblz4-dev <!nocheck>,\n libzstd-dev <!nocheck>\nStandards-Version: 4.2.1\nHomepage: https://docs.keydb.dev/\nVcs-Git: https://github.com/Snapchat/KeyDB.git\nVcs-Browser: https://github.com/Snapchat/KeyDB\n\nPackage: keydb\nArchitecture: all\nDepends:\n keydb-server (<< ${binary:Version}.1~),\n keydb-server (>= ${binary:Version}),\n ${misc:Depends},\nDescription: Persistent key-value database with network interface (metapackage)\n keydb is a key-value database in a similar vein to memcache but the dataset\n is non-volatile. keydb additionally provides native support for atomically\n manipulating and querying data structures such as lists and sets.\n .\n The dataset is stored entirely in memory and periodically flushed to disk.\n .\n This package depends on the keydb-server package.\n\nPackage: keydb-sentinel\nArchitecture: any\nDepends:\n lsb-base (>= 3.2-14),\n keydb-tools (= ${binary:Version}),\n ${misc:Depends},\nDescription: Persistent key-value database with network interface (monitoring)\n keydb is a key-value database in a similar vein to memcache but the dataset\n is non-volatile. keydb additionally provides native support for atomically\n manipulating and querying data structures such as lists and sets.\n .\n This package contains the keydb Sentinel monitoring software.\n\nPackage: keydb-server\nArchitecture: any\nDepends:\n lsb-base (>= 3.2-14),\n keydb-tools (= ${binary:Version}),\n ${misc:Depends},\nDescription: Persistent key-value database with network interface\n keydb is a key-value database in a similar vein to memcache but the dataset\n is non-volatile. keydb additionally provides native support for atomically\n manipulating and querying data structures such as lists and sets.\n .\n The dataset is stored entirely in memory and periodically flushed to disk.\n\nPackage: keydb-tools\nArchitecture: any\nDepends:\n adduser,\n ${misc:Depends},\n ${shlibs:Depends},\nDescription: Persistent key-value database with network interface (client)\n keydb is a key-value database in a similar vein to memcache but the dataset\n is non-volatile. keydb additionally provides native support for atomically\n manipulating and querying data structures such as lists and sets.\n .\n This package contains the command line client and other tools.\n"
  },
  {
    "path": "pkg/deb/debian_dh9/copyright",
    "content": "Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/\nUpstream-Contact: John Sully <john@eqalpha.com>\nUpstream-Name: KeyDB\nSource: https://github.com/EQ-Alpha/KeyDB\n\nFiles: *\nCopyright: \n © 2006-2014 Salvatore Sanfilippo <antirez@gmail.com>\n © 2019 John Sully\n © 2019-2021 EQ Alpha Technology Ltd.\n\nLicense: BSD-3-clause\n\nFiles:\n src/rio.*\n src/t_zset.c\n src/ziplist.h\n src/intset.*\n src/redis-check-aof.c\n deps/hiredis/*\n deps/linenoise/*\nCopyright:\n © 2009-2012 Pieter Noordhuis <pcnoordhuis@gmail.com>\n © 2009-2012 Salvatore Sanfilippo <antirez@gmail.com>\nLicense: BSD-3-clause\n\nFiles:\n src/lzf.h\n src/lzfP.h\n src/lzf_d.c\n src/lzf_c.c\nCopyright:\n © 2000-2007 Marc Alexander Lehmann <schmorp@schmorp.de>\n © 2009-2012 Salvatore Sanfilippo <antirez@gmail.com>\nLicense: BSD-2-clause\n\nFiles: src/setproctitle.c\nCopyright:\n © 2010 William Ahern\n © 2013 Salvatore Sanfilippo\n © 2013 Stam He\nLicense: BSD-3-clause\n\nFiles: src/ae_evport.c\nCopyright: © 2012 Joyent, Inc.\nLicense: BSD-3-clause\n\nFiles: src/ae_kqueue.c\nCopyright: © 2009 Harish Mallipeddi <harish.mallipeddi@gmail.com>\nLicense: BSD-3-clause\n\nFiles: utils/install_server.sh\nCopyright: © 2011 Dvir Volk <dvirsk@gmail.com>\nLicense: BSD-3-clause\n\nFiles: deps/jemalloc/*\nCopyright:\n © 2002-2012 Jason Evans <jasone@canonware.com>\n © 2007-2012 Mozilla Foundation\n © 2009-2012 Facebook, Inc.\nLicense: BSD-3-clause\n\nFiles: src/pqsort.*\nCopyright: © 1992-1993 The Regents of the University of California\nLicense: BSD-3-clause\n\nFiles: deps/lua/*\nCopyright: © 1994-2012 Lua.org, PUC-Ri\nLicense: MIT\n Permission is hereby granted, free of charge, to any person obtaining a copy\n of this software and associated documentation files (the \"Software\"), to deal\n in the Software without restriction, including without limitation the rights\n to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n copies of the Software, and to permit persons to whom the Software is\n furnished to do so, subject to the following conditions:\n .\n The above copyright notice and this permission notice shall be included in\n all copies or substantial portions of the Software.\n .\n THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n THE SOFTWARE.\n\nFiles: pkg/deb/debian_dh9/*\nCopyright: \n © 2009 Chris Lamb <lamby@debian.org>\n © 2020-2021 EQ Alpha Technology Ltd. <support@eqalpha.com>\nLicense: BSD-3-clause\n\nLicense: BSD-2-clause\n Redistribution and use in source and binary forms, with or without\n modification, are permitted provided that the following conditions are met:\n .\n 1. Redistributions of source code must retain the above copyright notice, this\n list of conditions and the following disclaimer.\n .\n 2. Redistributions in binary form must reproduce the above copyright notice,\n this list of conditions and the following disclaimer in the documentation\n and/or other materials provided with the distribution.\n .\n THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\n FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\n CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\n OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\nLicense: BSD-3-clause\n Redistribution and use in source and binary forms, with or without\n modification, are permitted provided that the following conditions\n are met:\n .\n    * Redistributions of source code must retain the above copyright\n      notice, this list of conditions and the following disclaimer.\n    * Redistributions in binary form must reproduce the above copyright\n      notice, this list of conditions and the following disclaimer in the\n      documentation and/or other materials provided with the distribution.\n    * Neither the name of Redis nor the names of its contributors may be\n      used to endorse or promote products derived from this software\n      without specific prior written permission.\n .\n THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE\n FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\n CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\n OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
  },
  {
    "path": "pkg/deb/debian_dh9/files",
    "content": "keydb_5.3.3-1~bionic1_source.buildinfo database optional\n"
  },
  {
    "path": "pkg/deb/debian_dh9/gbp.conf",
    "content": "[DEFAULT]\ndebian-branch=debian/sid\nupstream-branch=upstream/sid\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-benchmark.1",
    "content": ".TH KEYDB-BENCHMARK 1 \"August 17, 2019\"\n.SH NAME\nkeydb-benchmark \\- Benechmark a KeyDB instance\n.SH SYNOPSIS\n.B redis-benchmark\n[\\-h <host>] [\\-p <port>] [\\-c <clients>] [\\-n <requests]> [\\-k <boolean>]\n.SH DESCRIPTION\nKeyDB is a key-value database. It is similar to memcached but the dataset is\nnot volatile and other datatypes (such as lists and sets) are natively\nsupported.\n.SH OPTIONS\n.TP\n\\-h hostname\nServer hostname (default 127.0.0.1)\n.TP\n\\-p port\nServer port (default 6379)\n.TP\n\\-s socket\nServer socket (overrides host and port)\n.TP\n\\-a password\nPassword for KeyDB Auth\n.TP \n\\-c clients\nNumber of parallel connections (default 50)\n.TP\n\\-n requests\nTotal number of requests (default 100000)\n.TP\n\\-d size\nData size of SET/GET value in bytes (default 2)\n.TP\n\\-dbnum db\nSELECT the specified db number (default 0)\n.TP\n\\-k boolean\n1=keep alive 0=reconnect (default 1)\n.TP\n\\-r keyspacelen\nUse random keys for SET/GET/INCR, random values for SADD Using this option the\nbenchmark will get/set keys in the form mykey_rand000000012456 instead of\nconstant keys, the <keyspacelen> argument determines the max number of values\nfor the random number. For instance if set to 10 only rand000000000000 -\nrand000000000009 range will be allowed.\n.TP\n\\-P numreq\nPipeline <numreq> requests. Default 1 (no pipeline).\n.TP\n\\-q\nQuiet. Just show query/sec values\n.TP\n\\-\\-csv\nOurput in CSV format\n.TP\n\\-l\nLoop. Run the tests forever\n.TP\n\\-I\nIdle mode. Just open N idle connections and wait.\n.TP\n\\-D\nDebug mode. more verbose.\n.SH AUTHOR\n\\fBkeydb-benchmark\\fP was written by John Sully, originating as a fork of Redis. Redis was written by Salvatore Sanfilippo.\n.PP\nThis manual page was written by Chris Lamb <lamby@debian.org> for the Debian\nproject (but may be used by others). Modified by Ben Schermel <ben@eqalpha.com>\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-check-aof.1",
    "content": ".TH KEYDB-CHECK-AOF 1 \"August 17, 2019\"\n.SH NAME\nkeydb-check-aof \\- Check integrity of a KeyDB .AOF file\n.SH SYNOPSIS\n.B keydb-check-aof\nfilename\n.SH DESCRIPTION\nKeyDB is a key-value database. It is similar to memcached but the dataset is\nnot volatile and other datatypes (such as lists and sets) are natively\nsupported.\n.PP\nThis utility checks the integrity of a dumped .AOF file.\n.SH AUTHOR\n\\fBkeydb-check-aof\\fP was written by John Sully, originating as a fork of Redis. Redis was written by Salvatore Sanfilippo.\n.PP\nThis manual page was written by Chris Lamb <lamby@debian.org> for the Debian\nproject (but may be used by others). Modified by Ben Schermel <ben@eqalpha.com>\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-check-rdb.1",
    "content": ".TH KEYDB-CHECK-RDB 1 \"August 17, 2019\"\n.SH NAME\nkeydb-check-rdb \\- Check integrity of KeyDB dumped database file\n.SH SYNOPSIS\n.B keydb-check-rdb\nfilename\n.SH DESCRIPTION\nKeyDB is a key-value database. It is similar to memcached but the dataset is\nnot volatile and other datatypes (such as lists and sets) are natively\nsupported.\n.PP\nThis utility checks the integrity of a dumped database file.\n.SH AUTHOR\n\\fBredis-check-rdb\\fP was written by John Sully, originating as a fork of Redis. Redis was written by Salvatore Sanfilippo.\n.PP\nThis manual page was written by Chris Lamb <lamby@debian.org> for the Debian\nproject (but may be used by others). Modified by Ben Schermel <ben@eqalpha.com>\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-cli.1",
    "content": ".TH KEYDB-CLI 1 \"August 17, 2019\"\n.SH NAME\nkeydb-cli \\- Command-line client to keydb-server\n.SH SYNOPSIS\n.B keydb-cli\n.RI [options]\n.SH DESCRIPTION\nKeyDB is a key-value database. It is similar to memcached but the dataset is\nnot volatile and other datatypes (such as lists and sets) are natively\nsupported.\n.PP\n\\fBkeydb-cli\\fP provides a simple command-line interface to a KeyDB server.\n.SH OPTIONS\nSee \\fBkeydb-doc\\fP for more information on the commands KeyDB accepts.\n.SH AUTHOR\n\\fBkeydb-cli\\fP was written by John Sully, originating as a fork of Redis. Redis was written by Salvatore Sanfilippo.\n.PP\nThis manual page was written by Chris Lamb <lamby@debian.org> for the Debian\nproject (but may be used by others). Modified by Ben Schermel <ben@eqalpha.com>\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-sentinel.1",
    "content": ".TH KEYDB-SENTINEL 1 \"August 17, 2019\"\n.SH NAME\nkeydb-sentinel \\- Persistent key-value database (cluster mode)\n.SH SYNOPSIS\n.B keydb-sentinel\n.RI configfile\n.SH DESCRIPTION\nKeyDB is a key-value database. It is similar to memcached but the dataset is\nnot volatile and other datatypes (such as lists and sets) are natively\nsupported.\n.PP\n.SH OPTIONS\n.IP \"configfile\"\nRead options from specified configuration file.\n.SH NOTES\nOn Debian GNU/Linux systems, \\fBkeydb-sentinel\\fP is typically started via the\n\\fB/etc/init.d/keydb-sentinel\\fP initscript, not manually. This defaults to using\n\\fB/etc/keydb/sentinel.conf\\fP as a configuration file.\n.SH AUTHOR\n\\fBkeydb-sentinel\\fP was written by John Sully, originating as a fork of Redis. Redis was written by Salvatore Sanfilippo.\n.PP\nThis manual page was written by Chris Lamb <lamby@debian.org> for the Debian\nproject (but may be used by others). Modified by Ben Schermel <ben@eqalpha.com>\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-sentinel.default",
    "content": "# keydb-sentinel configure options\n\n# ULIMIT: Call ulimit -n with this argument prior to invoking KeyDB Sentinel\n# itself.  This may be required for high-concurrency environments. KeyDB\n# Sentinel itself cannot alter its limits as it is not being run as root.\n# (default: 65536)\n#\nULIMIT=65536\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-sentinel.init",
    "content": "#! /bin/sh\n### BEGIN INIT INFO\n# Provides:\t\tkeydb-sentinel\n# Required-Start:\t$syslog $remote_fs\n# Required-Stop:\t$syslog $remote_fs\n# Should-Start:\t\t$local_fs\n# Should-Stop:\t\t$local_fs\n# Default-Start:\t2 3 4 5\n# Default-Stop:\t\t0 1 6\n# Short-Description:\tkeydb-sentinel - Persistent key-value db monitor\n# Description:\t\tkeydb-sentinel - Persistent key-value db monitor\n### END INIT INFO\n\n\nPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\nDAEMON=/usr/bin/keydb-sentinel\nDAEMON_ARGS=/etc/keydb/sentinel.conf\nNAME=keydb-sentinel\nDESC=keydb-sentinel\n\nRUNDIR=/var/run/sentinel\nPIDFILE=$RUNDIR/keydb-sentinel.pid\n\ntest -x $DAEMON || exit 0\n\nif [ -r /etc/default/$NAME ]\nthen\n\t. /etc/default/$NAME\nfi\n\n. /lib/lsb/init-functions\n\nset -e\n\nif [ \"$(id -u)\" != \"0\" ]\nthen\n\tlog_failure_msg \"Must be run as root.\"\n\texit 1\nfi\n\ncase \"$1\" in\n  start)\n\techo -n \"Starting $DESC: \"\n\tmkdir -p $RUNDIR\n\ttouch $PIDFILE\n\tchown keydb:keydb $RUNDIR $PIDFILE\n\tchmod 755 $RUNDIR\n\n\tif [ -n \"$ULIMIT\" ]\n\tthen\n\t\tulimit -n $ULIMIT || true\n\tfi\n\n\tif start-stop-daemon --start --quiet --oknodo --umask 007 --pidfile $PIDFILE --chuid keydb:keydb --exec $DAEMON -- $DAEMON_ARGS\n\tthen\n\t\techo \"$NAME.\"\n\telse\n\t\techo \"failed\"\n\tfi\n\t;;\n  stop)\n\techo -n \"Stopping $DESC: \"\n\n\tif start-stop-daemon --stop --retry forever/TERM/1 --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON\n\tthen\n\t\techo \"$NAME.\"\n\telse\n\t\techo \"failed\"\n\tfi\n\trm -f $PIDFILE\n\tsleep 1\n\t;;\n\n  restart|force-reload)\n\t${0} stop\n\t${0} start\n\t;;\n\n  status)\n\tstatus_of_proc -p ${PIDFILE} ${DAEMON} ${NAME}\n\t;;\n\n  *)\n\techo \"Usage: /etc/init.d/$NAME {start|stop|restart|force-reload|status}\" >&2\n\texit 1\n\t;;\nesac\n\nexit 0\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-sentinel.install",
    "content": "debian/keydb-sentinel.service /lib/systemd/system\npkg/deb/conf/sentinel.conf\t/etc/keydb\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-sentinel.logrotate",
    "content": "/var/log/keydb/keydb-sentinel*.log {\n        weekly\n        missingok\n        rotate 12\n        compress\n        notifempty\n}\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-sentinel.maintscript",
    "content": "rm_conffile /etc/keydb/keydb-sentinel.post-down.d/00_example 4:4.0.2-3~\nrm_conffile /etc/keydb/keydb-sentinel.post-up.d/00_example 4:4.0.2-3~\nrm_conffile /etc/keydb/keydb-sentinel.pre-down.d/00_example 4:4.0.2-3~\nrm_conffile /etc/keydb/keydb-sentinel.pre-up.d/00_example 4:4.0.2-3~\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-sentinel.manpages",
    "content": "debian/keydb-sentinel.1\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-sentinel.postinst",
    "content": "#!/bin/sh\n\nset -eu\n\nUSER=\"keydb\"\nGROUP=\"$USER\"\nCONFFILE=\"/etc/keydb/sentinel.conf\"\n\nif [ \"$1\" = \"configure\" ]\nthen\n\tif ! dpkg-statoverride --list ${CONFFILE} >/dev/null 2>&1\n\tthen\n\t\tdpkg-statoverride --update --add ${USER} ${GROUP} 640 ${CONFFILE}\n\tfi\nfi\n\n#DEBHELPER#\n\nif [ \"$1\" = \"configure\" ]\nthen\n\tfind /etc/keydb -maxdepth 1 -type d -name 'keydb-sentinel.*.d' -empty -delete\nfi\n\nexit 0\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-sentinel.postrm",
    "content": "#!/bin/sh\n\nset -eu\n\nCONFFILE=\"/etc/keydb/sentinel.conf\"\n\nif [ \"$1\" = \"purge\" ]\nthen\n\tdpkg-statoverride --remove ${CONFFILE} || test $? -eq 2\nfi\n\n#DEBHELPER#\n\nexit 0\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-server.1",
    "content": ".TH KEYDB-SERVER 1 \"August 17, 2019\"\n.SH NAME\nkeydb-server \\- Persistent key-value database\n.SH SYNOPSIS\n.B keydb-server\n.RI configfile\n.SH DESCRIPTION\nKeyDB is a key-value database. It is similar to memcached but the dataset is\nnot volatile and other datatypes (such as lists and sets) are natively\nsupported.\n.PP\n.SH OPTIONS\n.IP \"configfile\"\nRead options from specified configuration file.\n.SH NOTES\nOn Debian GNU/Linux systems, \\fBkeydb-server\\fP is typically started via the\n\\fB/etc/init.d/keydb-server\\fP initscript, not manually. This defaults to using\n\\fB/etc/keydb/keydb.conf\\fP as a configuration file.\n.SH AUTHOR\n\\fBkeydb-server\\fP was written by John Sully, originating as a fork of Redis. Redis was written by Salvatore Sanfilippo.\n.PP\nThis manual page was written by Chris Lamb <lamby@debian.org> for the Debian\nproject (but may be used by others). Modified by Ben Schermel <ben@eqalpha.com>\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-server.default",
    "content": "# keydb-server configure options\n\n# ULIMIT: Call ulimit -n with this argument prior to invoking Redis itself.\n# This may be required for high-concurrency environments. KeyDB itself cannot\n# alter its limits as it is not being run as root. (default: 65536)\n#\nULIMIT=65536\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-server.docs",
    "content": "README.md\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-server.init",
    "content": "#! /bin/sh\n### BEGIN INIT INFO\n# Provides:\t\tkeydb-server\n# Required-Start:\t$syslog $remote_fs\n# Required-Stop:\t$syslog $remote_fs\n# Should-Start:\t\t$local_fs\n# Should-Stop:\t\t$local_fs\n# Default-Start:\t2 3 4 5\n# Default-Stop:\t\t0 1 6\n# Short-Description:\tkeydb-server - Persistent key-value db\n# Description:\t\tkeydb-server - Persistent key-value db\n### END INIT INFO\n\n\nPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\nDAEMON=/usr/bin/keydb-server\nDAEMON_ARGS=/etc/keydb/keydb.conf\nNAME=keydb-server\nDESC=keydb-server\n\nRUNDIR=/var/run/keydb\nPIDFILE=$RUNDIR/keydb-server.pid\n\ntest -x $DAEMON || exit 0\n\nif [ -r /etc/default/$NAME ]\nthen\n\t. /etc/default/$NAME\nfi\n\n. /lib/lsb/init-functions\n\nset -e\n\nif [ \"$(id -u)\" != \"0\" ]\nthen\n\tlog_failure_msg \"Must be run as root.\"\n\texit 1\nfi\n\ncase \"$1\" in\n  start)\n\techo -n \"Starting $DESC: \"\n\tmkdir -p $RUNDIR\n\ttouch $PIDFILE\n\tchown keydb:keydb $RUNDIR $PIDFILE\n\tchmod 755 $RUNDIR\n\n\tif [ -n \"$ULIMIT\" ]\n\tthen\n\t\tulimit -n $ULIMIT || true\n\tfi\n\n\tif start-stop-daemon --start --quiet --oknodo --umask 007 --pidfile $PIDFILE --chuid keydb:keydb --exec $DAEMON -- $DAEMON_ARGS\n\tthen\n\t\techo \"$NAME.\"\n\telse\n\t\techo \"failed\"\n\tfi\n\t;;\n  stop)\n\techo -n \"Stopping $DESC: \"\n\n\tif start-stop-daemon --stop --retry forever/TERM/1 --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON\n\tthen\n\t\techo \"$NAME.\"\n\telse\n\t\techo \"failed\"\n\tfi\n\trm -f $PIDFILE\n\tsleep 1\n\t;;\n\n  restart|force-reload)\n\t${0} stop\n\t${0} start\n\t;;\n\n  status)\n\tstatus_of_proc -p ${PIDFILE} ${DAEMON} ${NAME}\n\t;;\n\n  *)\n\techo \"Usage: /etc/init.d/$NAME {start|stop|restart|force-reload|status}\" >&2\n\texit 1\n\t;;\nesac\n\nexit 0\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-server.install",
    "content": "debian/keydb-server.service /lib/systemd/system\npkg/deb/conf/keydb.conf\t/etc/keydb\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-server.logrotate",
    "content": "/var/log/keydb/keydb-server*.log {\n        weekly\n        missingok\n        rotate 12\n        compress\n        notifempty\n}\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-server.maintscript",
    "content": "rm_conffile /etc/keydb/keydb-server.post-down.d/00_example 4:4.0.2-3~\nrm_conffile /etc/keydb/keydb-server.post-up.d/00_example 4:4.0.2-3~\nrm_conffile /etc/keydb/keydb-server.pre-down.d/00_example 4:4.0.2-3~\nrm_conffile /etc/keydb/keydb-server.pre-up.d/00_example 4:4.0.2-3~\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-server.manpages",
    "content": "debian/keydb-server.1\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-server.postinst",
    "content": "#!/bin/sh\n\nset -eu\n\nUSER=\"keydb\"\nGROUP=\"$USER\"\nCONFFILE=\"/etc/keydb/keydb.conf\"\n\nif [ \"$1\" = \"configure\" ]\nthen\n\tif ! dpkg-statoverride --list ${CONFFILE} >/dev/null 2>&1\n\tthen\n\t\tdpkg-statoverride --update --add ${USER} ${GROUP} 640 ${CONFFILE}\n\tfi\nfi\n\n#DEBHELPER#\n\nif [ \"$1\" = \"configure\" ]\nthen\n\tfind /etc/keydb -maxdepth 1 -type d -name 'keydb-server.*.d' -empty -delete\nfi\n\nexit 0\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-server.postrm",
    "content": "#!/bin/sh\n\nset -eu\n\nCONFFILE=\"/etc/keydb/keydb.conf\"\n\nif [ \"${1}\" = \"purge\" ]\nthen\n\tdpkg-statoverride --remove ${CONFFILE} || test $? -eq 2\nfi\n\n#DEBHELPER#\n\nexit 0\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-tools.examples",
    "content": "src/redis-trib.rb\nutils/lru\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-tools.install",
    "content": "debian/bash_completion.d/*\t/usr/share/bash-completion/completions\nsrc/keydb-server\t/usr/bin\nsrc/keydb-benchmark\t/usr/bin\nsrc/keydb-check-aof\t/usr/bin\nsrc/keydb-check-rdb\t/usr/bin\nsrc/keydb-cli\t\t/usr/bin\nsrc/keydb-sentinel\t/usr/bin\nsrc/keydb-diagnostic-tool\t/usr/bin\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-tools.manpages",
    "content": "debian/keydb-benchmark.1\ndebian/keydb-check-aof.1\ndebian/keydb-check-rdb.1\ndebian/keydb-cli.1\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-tools.postinst",
    "content": "#!/bin/sh\n\nset -eu\n\nUSER=\"keydb\"\n\nSetup_dir () {\n\tDIR=\"${1}\"\n\tMODE=\"${2}\"\n\tGROUP=\"${3}\"\n\n\tmkdir -p ${DIR}\n\n\tcase \"${DIR}\" in\n\t/var/log/keydb)\n\t\tMODE=\"02750\"\n\t\tGROUP=\"adm\"\n\t\t;;\n\t*)\n\t\tMODE=\"750\"\n\t\tGROUP=\"${USER}\"\n\t\t;;\n\tesac\n\n\tif ! dpkg-statoverride --list ${DIR} >/dev/null 2>&1\n\tthen\n\t\tchown ${USER}:${GROUP} ${DIR}\n\t\tchmod ${MODE} ${DIR}\n\tfi\n}\n\nif [ \"$1\" = \"configure\" ]\nthen\n\tadduser \\\n\t\t--system \\\n\t\t--home /var/lib/keydb \\\n\t\t--quiet \\\n\t\t--group \\\n\t\t${USER} || true\n\n\tSetup_dir /var/log/keydb ${USER}:adm 2750\n\tSetup_dir /var/lib/keydb ${USER}:${USER} 750\nfi\n\n#DEBHELPER#\n\nexit 0\n"
  },
  {
    "path": "pkg/deb/debian_dh9/keydb-tools.postrm",
    "content": "#!/bin/sh\n\nset -eu\n\nif [ \"${1}\" = \"purge\" ]\nthen\n\tuserdel keydb || true\n\trm -rf /var/lib/keydb /var/log/keydb\nfi\n\n#DEBHELPER#\n\nexit 0\n"
  },
  {
    "path": "pkg/deb/debian_dh9/patches/0001-fix-ftbfs-on-kfreebsd.patch",
    "content": "From: Chris Lamb <lamby@debian.org>\nDate: Fri, 30 Oct 2015 10:53:42 +0000\nSubject: fix-ftbfs-on-kfreebsd\n\n---\n src/fmacros.h | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/src/fmacros.h b/src/fmacros.h\nindex 6e56c75..d490aec 100644\n--- a/src/fmacros.h\n+++ b/src/fmacros.h\n@@ -41,7 +41,7 @@\n #define _ALL_SOURCE\n #endif\n \n-#if defined(__linux__) || defined(__OpenBSD__)\n+#if defined(__linux__) || defined(__OpenBSD__) || defined(__GLIBC__)\n #define _XOPEN_SOURCE 700\n /*\n  * On NetBSD, _XOPEN_SOURCE undefines _NETBSD_SOURCE and\n"
  },
  {
    "path": "pkg/deb/debian_dh9/patches/0010-Use-get_current_dir_name-over-PATHMAX-etc.patch",
    "content": "From: Chris Lamb <lamby@debian.org>\nDate: Wed, 24 Jan 2018 22:06:35 +1100\nSubject: Use get_current_dir_name over PATHMAX, etc.\n\n---\n src/aof.c | 3 ++-\n src/rdb.c | 7 ++++---\n 2 files changed, 6 insertions(+), 4 deletions(-)\n\ndiff --git a/src/aof.c b/src/aof.c\nindex 9723fc3..cc89847 100644\n--- a/src/aof.c\n+++ b/src/aof.c\n@@ -246,7 +246,7 @@ int startAppendOnly(void) {\n     newfd = open(server.aof_filename,O_WRONLY|O_APPEND|O_CREAT,0644);\n     serverAssert(server.aof_state == AOF_OFF);\n     if (newfd == -1) {\n-        char *cwdp = getcwd(cwd,MAXPATHLEN);\n+        char *cwdp = get_current_dir_name();\n \n         serverLog(LL_WARNING,\n             \"Redis needs to enable the AOF but can't open the \"\n@@ -254,6 +254,7 @@ int startAppendOnly(void) {\n             server.aof_filename,\n             cwdp ? cwdp : \"unknown\",\n             strerror(errno));\n+        zfree(cwdp);\n         return C_ERR;\n     }\n     if (server.rdb_child_pid != -1) {\ndiff --git a/src/rdb.c b/src/rdb.c\nindex 3e43cb4..6058160 100644\n--- a/src/rdb.c\n+++ b/src/rdb.c\n@@ -1218,7 +1218,6 @@ werr: /* Write error. */\n /* Save the DB on disk. Return C_ERR on error, C_OK on success. */\n int rdbSave(char *filename, rdbSaveInfo *rsi) {\n     char tmpfile[256];\n-    char cwd[MAXPATHLEN]; /* Current working dir path for error messages. */\n     FILE *fp;\n     rio rdb;\n     int error = 0;\n@@ -1226,13 +1225,14 @@ int rdbSave(char *filename, rdbSaveInfo *rsi) {\n     snprintf(tmpfile,256,\"temp-%d.rdb\", (int) getpid());\n     fp = fopen(tmpfile,\"w\");\n     if (!fp) {\n-        char *cwdp = getcwd(cwd,MAXPATHLEN);\n+        char *cwdp = get_current_dir_name();\n         serverLog(LL_WARNING,\n             \"Failed opening the RDB file %s (in server root dir %s) \"\n             \"for saving: %s\",\n             filename,\n             cwdp ? cwdp : \"unknown\",\n             strerror(errno));\n+        zfree(cwdp);\n         return C_ERR;\n     }\n \n@@ -1254,7 +1254,7 @@ int rdbSave(char *filename, rdbSaveInfo *rsi) {\n     /* Use RENAME to make sure the DB file is changed atomically only\n      * if the generate DB file is ok. */\n     if (rename(tmpfile,filename) == -1) {\n-        char *cwdp = getcwd(cwd,MAXPATHLEN);\n+        char *cwdp = get_current_dir_name();\n         serverLog(LL_WARNING,\n             \"Error moving temp DB file %s on the final \"\n             \"destination %s (in server root dir %s): %s\",\n@@ -1262,6 +1262,7 @@ int rdbSave(char *filename, rdbSaveInfo *rsi) {\n             filename,\n             cwdp ? cwdp : \"unknown\",\n             strerror(errno));\n+        zfree(cwdp);\n         unlink(tmpfile);\n         return C_ERR;\n     }\n"
  },
  {
    "path": "pkg/deb/debian_dh9/patches/0011-Add-support-for-a-USE_SYSTEM_LUA-flag.patch",
    "content": "From: Chris Lamb <lamby@debian.org>\nDate: Sun, 26 Aug 2018 12:57:32 +0200\nSubject: Add support for a USE_SYSTEM_LUA flag.\n\nhttps://github.com/antirez/redis/pull/5280\n---\n deps/Makefile |  2 ++\n src/Makefile  | 15 ++++++++++++---\n 2 files changed, 14 insertions(+), 3 deletions(-)\n\ndiff --git a/deps/Makefile b/deps/Makefile\nindex 1342fac..2ed7736 100644\n--- a/deps/Makefile\n+++ b/deps/Makefile\n@@ -35,7 +35,9 @@ endif\n distclean:\n \t-(cd hiredis && $(MAKE) clean) > /dev/null || true\n \t-(cd linenoise && $(MAKE) clean) > /dev/null || true\n+ifneq ($(USE_SYSTEM_LUA),yes)\n \t-(cd lua && $(MAKE) clean) > /dev/null || true\n+endif\n ifneq ($(USE_SYSTEM_JEMALLOC),yes)\n \t-(cd jemalloc && [ -f Makefile ] && $(MAKE) distclean) > /dev/null || true\n endif\ndiff --git a/src/Makefile b/src/Makefile\nindex 51363fe..49085f2 100644\n--- a/src/Makefile\n+++ b/src/Makefile\n@@ -16,7 +16,7 @@ release_hdr := $(shell sh -c './mkreleasehdr.sh')\n uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')\n uname_M := $(shell sh -c 'uname -m 2>/dev/null || echo not')\n OPTIMIZATION?=-O2\n-DEPENDENCY_TARGETS=hiredis linenoise lua\n+DEPENDENCY_TARGETS=hiredis linenoise\n NODEPS:=clean distclean\n \n # Default settings\n@@ -107,7 +107,7 @@ endif\n endif\n endif\n # Include paths to dependencies\n-FINAL_CFLAGS+= -I../deps/hiredis -I../deps/linenoise -I../deps/lua/src\n+FINAL_CFLAGS+= -I../deps/hiredis -I../deps/linenoise\n \n ifeq ($(MALLOC),tcmalloc)\n \tFINAL_CFLAGS+= -DUSE_TCMALLOC\n@@ -130,6 +130,15 @@ else\n endif\n endif\n \n+ifeq ($(USE_SYSTEM_LUA),yes)\n+\tFINAL_CFLAGS+= -I/usr/include/lua5.1\n+\tFINAL_LIBS := -llua5.1 $(FINAL_LIBS)\n+else\n+\tFINAL_CFLAGS+= -I../deps/lua/src\n+\tDEPENDENCY_TARGETS+= lua\n+\tFINAL_LIBS := ../deps/lua/src/liblua.a $(FINAL_LIBS)\n+endif\n+\n REDIS_CC=$(QUIET_CC)$(CC) $(FINAL_CFLAGS) $(CPPFLAGS)\n REDIS_LD=$(QUIET_LINK)$(CC) $(FINAL_LDFLAGS)\n REDIS_INSTALL=$(QUIET_INSTALL)$(INSTALL)\n@@ -201,7 +210,7 @@ endif\n \n # redis-server\n $(REDIS_SERVER_NAME): $(REDIS_SERVER_OBJ)\n-\t$(REDIS_LD) -o $@ $^ ../deps/hiredis/libhiredis.a ../deps/lua/src/liblua.a $(FINAL_LIBS)\n+\t$(REDIS_LD) -o $@ $^ ../deps/hiredis/libhiredis.a $(FINAL_LIBS)\n \n # redis-sentinel\n $(REDIS_SENTINEL_NAME): $(REDIS_SERVER_NAME)\n"
  },
  {
    "path": "pkg/deb/debian_dh9/patches/debian-packaging/0003-dpkg-buildflags.patch",
    "content": "From: Chris Lamb <lamby@debian.org>\nDate: Fri, 30 Oct 2015 10:53:42 +0000\nSubject: Add CPPFLAGS in upstream makefiles\n\n---\n deps/hiredis/Makefile   | 2 +-\n deps/linenoise/Makefile | 2 +-\n src/Makefile            | 2 +-\n 3 files changed, 3 insertions(+), 3 deletions(-)\n\ndiff --git a/deps/hiredis/Makefile b/deps/hiredis/Makefile\nindex 9a4de83..4c8a8e4 100644\n--- a/deps/hiredis/Makefile\n+++ b/deps/hiredis/Makefile\n@@ -41,7 +41,7 @@ CXX:=$(shell sh -c 'type $(CXX) >/dev/null 2>/dev/null && echo $(CXX) || echo g+\n OPTIMIZATION?=-O3\n WARNINGS=-Wall -W -Wstrict-prototypes -Wwrite-strings\n DEBUG_FLAGS?= -g -ggdb\n-REAL_CFLAGS=$(OPTIMIZATION) -fPIC $(CFLAGS) $(WARNINGS) $(DEBUG_FLAGS) $(ARCH)\n+REAL_CFLAGS=$(OPTIMIZATION) -fPIC $(CFLAGS) $(WARNINGS) $(DEBUG_FLAGS) $(ARCH) $(CPPFLAGS)\n REAL_LDFLAGS=$(LDFLAGS) $(ARCH)\n \n DYLIBSUFFIX=so\ndiff --git a/deps/linenoise/Makefile b/deps/linenoise/Makefile\nindex 1dd894b..12ada21 100644\n--- a/deps/linenoise/Makefile\n+++ b/deps/linenoise/Makefile\n@@ -6,7 +6,7 @@ R_CFLAGS= $(STD) $(WARN) $(OPT) $(DEBUG) $(CFLAGS)\n R_LDFLAGS= $(LDFLAGS)\n DEBUG= -g\n \n-R_CC=$(CC) $(R_CFLAGS)\n+R_CC=$(CC) $(R_CFLAGS) $(CPPFLAGS)\n R_LD=$(CC) $(R_LDFLAGS)\n \n linenoise.o: linenoise.h linenoise.c\ndiff --git a/src/Makefile b/src/Makefile\nindex 773d3b2..0ff6e8b 100644\n--- a/src/Makefile\n+++ b/src/Makefile\n@@ -125,7 +125,7 @@ ifeq ($(MALLOC),jemalloc)\n \tFINAL_LIBS := ../deps/jemalloc/lib/libjemalloc.a $(FINAL_LIBS)\n endif\n \n-REDIS_CC=$(QUIET_CC)$(CC) $(FINAL_CFLAGS)\n+REDIS_CC=$(QUIET_CC)$(CC) $(FINAL_CFLAGS) $(CPPFLAGS)\n REDIS_LD=$(QUIET_LINK)$(CC) $(FINAL_LDFLAGS)\n REDIS_INSTALL=$(QUIET_INSTALL)$(INSTALL)\n \n"
  },
  {
    "path": "pkg/deb/debian_dh9/patches/debian-packaging/0007-Set-Debian-configuration-defaults.patch",
    "content": "From: Chris Lamb <lamby@debian.org>\nDate: Tue, 10 Oct 2017 09:56:42 +0100\nSubject: Set Debian configuration defaults.\n\n---\n keydb.conf    | 12 ++++++------\n sentinel.conf |  9 +++++----\n 2 files changed, 11 insertions(+), 10 deletions(-)\n\ndiff --git a/keydb.conf b/keydb.conf\nindex 93ab9a4..24e6c79 100644\n--- a/keydb.conf\n+++ b/keydb.conf\n@@ -66,7 +66,7 @@\n # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES\n # JUST COMMENT THE FOLLOWING LINE.\n # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n-bind 127.0.0.1\n+bind 127.0.0.1 ::1\n \n # Protected mode is a layer of security protection, in order to avoid that\n # keydb instances left open on the internet are accessed and exploited.\n@@ -106,7 +106,7 @@ tcp-backlog 511\n # incoming connections. There is no default, so keydb will not listen\n # on a unix socket when not specified.\n #\n-# unixsocket /tmp/keydb.sock\n+# unixsocket /var/run/keydb/keydb-server.sock\n # unixsocketperm 700\n \n # Close the connection after a client is idle for N seconds (0 to disable)\n@@ -133,7 +133,7 @@ tcp-keepalive 300\n \n # By default keydb does not run as a daemon. Use 'yes' if you need it.\n # Note that keydb will write a pid file in /var/run/keydb.pid when daemonized.\n-daemonize no\n+daemonize yes\n \n # If you run keydb from upstart or systemd, keydb can interact with your\n # supervision tree. Options:\n@@ -155,7 +155,7 @@ supervised no\n #\n # Creating a pid file is best effort: if keydb is not able to create it\n # nothing bad happens, the server will start and run normally.\n-pidfile /var/run/keydb_6379.pid\n+pidfile /var/run/keydb/keydb-server.pid\n \n # Specify the server verbosity level.\n # This can be one of:\n@@ -168,7 +168,7 @@ loglevel notice\n # Specify the log file name. Also the empty string can be used to force\n # keydb to log on the standard output. Note that if you use standard\n # output for logging but daemonize, logs will be sent to /dev/null\n-logfile \"\"\n+logfile /var/log/keydb/keydb-server.log\n \n # To enable logging to the system logger, just set 'syslog-enabled' to yes,\n # and optionally update the other syslog parameters to suit your needs.\n@@ -260,7 +260,7 @@ dbfilename dump.rdb\n # The Append Only File will also be created inside this directory.\n #\n # Note that you must specify a directory here, not a file name.\n-dir ./\n+dir /var/lib/keydb\n \n ################################# REPLICATION #################################\n \ndiff --git a/sentinel.conf b/sentinel.conf\nindex bc9a705..58a4c84 100644\n--- a/sentinel.conf\n+++ b/sentinel.conf\n@@ -13,6 +13,7 @@\n # For example you may use one of the following:\n #\n # bind 127.0.0.1 192.168.1.1\n+bind 127.0.0.1 ::1\n #\n # protected-mode no\n \n@@ -23,17 +24,17 @@ port 26379\n # By default keydb Sentinel does not run as a daemon. Use 'yes' if you need it.\n # Note that keydb will write a pid file in /var/run/keydb-sentinel.pid when\n # daemonized.\n-daemonize no\n+daemonize yes\n \n # When running daemonized, keydb Sentinel writes a pid file in\n # /var/run/keydb-sentinel.pid by default. You can specify a custom pid file\n # location here.\n-pidfile /var/run/keydb-sentinel.pid\n+pidfile /var/run/sentinel/keydb-sentinel.pid\n \n # Specify the log file name. Also the empty string can be used to force\n # Sentinel to log on the standard output. Note that if you use standard\n # output for logging but daemonize, logs will be sent to /dev/null\n-logfile \"\"\n+logfile /var/log/keydb/keydb-sentinel.log\n \n # sentinel announce-ip <ip>\n # sentinel announce-port <port>\n@@ -62,7 +63,7 @@ logfile \"\"\n # For keydb Sentinel to chdir to /tmp at startup is the simplest thing\n # for the process to don't interfere with administrative tasks such as\n # unmounting filesystems.\n-dir /tmp\n+dir /var/lib/keydb\n \n # sentinel monitor <master-name> <ip> <keydb-port> <quorum>\n #\n"
  },
  {
    "path": "pkg/deb/debian_dh9/patches/series",
    "content": "0001-fix-ftbfs-on-kfreebsd.patch\n#debian-packaging/0003-dpkg-buildflags.patch\n#debian-packaging/0007-Set-Debian-configuration-defaults.patch\n#0010-Use-get_current_dir_name-over-PATHMAX-etc.patch\n#0011-Add-support-for-a-USE_SYSTEM_LUA-flag.patch\n#test\n"
  },
  {
    "path": "pkg/deb/debian_dh9/patches/test",
    "content": "Description: <short summary of the patch>\n TODO: Put a short summary on the line above and replace this paragraph\n with a longer explanation of this change. Complete the meta-information\n with other relevant fields (see below for details). To make it easier, the\n information below has been extracted from the changelog. Adjust it or drop\n it.\n .\n redis (5:5.0.5-1chl1~bionic1) bionic; urgency=medium\n .\n   * Redis 5.0.5 fixes an important issue with AOF and adds multiple very useful\n     modules APIs. Moreover smaller bugs in other parts of Redis are fixed in\n     this release.\n   * Streams: a bug in the iterator could prevent certain items to be returned in\n     range queries under specific conditions.\n   * Memleak in bitfieldCommand fixed.\n   * Modules API: Preserve client*>id for blocked clients.\n   * Fix memory leak when rewriting config file in case of write errors.\n   * New modules API: RedisModule_GetKeyNameFromIO().\n   * Fix non critical bugs in diskless replication.\n   * New mdouels API: command filtering. See RedisModule_RegisterCommandFilter();\n   * Tests improved to be more deterministic.\n   * Fix a Redis Cluster bug, manual failover may abort because of the master\n     sending PINGs to the replicas.\nAuthor: Chris Lea <chris.lea@gmail.com>\n\n---\nThe information above should follow the Patch Tagging Guidelines, please\ncheckout http://dep.debian.net/deps/dep3/ to learn about the format. Here\nare templates for supplementary fields that you might want to add:\n\nOrigin: <vendor|upstream|other>, <url of original patch>\nBug: <url in upstream bugtracker>\nBug-Debian: https://bugs.debian.org/<bugnumber>\nBug-Ubuntu: https://launchpad.net/bugs/<bugnumber>\nForwarded: <no|not-needed|url proving that it has been forwarded>\nReviewed-By: <name and email of someone who approved the patch>\nLast-Update: 2019-08-14\n\n--- /dev/null\n+++ redis-5.0.5/redis_5.0.5-1chl1~bionic1.dsc\n@@ -0,0 +1,42 @@\n+-----BEGIN PGP SIGNED MESSAGE-----\n+Hash: SHA512\n+\n+Format: 3.0 (quilt)\n+Source: redis\n+Binary: redis, redis-sentinel, redis-server, redis-tools\n+Architecture: any all\n+Version: 5:5.0.5-1chl1~bionic1\n+Maintainer: Chris Lamb <lamby@debian.org>\n+Homepage: https://redis.io/\n+Standards-Version: 4.2.1\n+Vcs-Browser: https://salsa.debian.org/lamby/pkg-redis\n+Vcs-Git: https://salsa.debian.org/lamby/pkg-redis.git\n+Testsuite: autopkgtest\n+Build-Depends: debhelper (>= 9~), dpkg-dev (>= 1.17.5), systemd, procps <!nocheck>, tcl <!nocheck>\n+Package-List:\n+ redis deb database optional arch=all\n+ redis-sentinel deb database optional arch=any\n+ redis-server deb database optional arch=any\n+ redis-tools deb database optional arch=any\n+Checksums-Sha1:\n+ 71e38ae09ac70012b5bc326522b976bcb8e269d6 1975750 redis_5.0.5.orig.tar.gz\n+ 4698bbe4c190f31601d3864d9bcd5ed8380c8723 25956 redis_5.0.5-1chl1~bionic1.debian.tar.xz\n+Checksums-Sha256:\n+ 2139009799d21d8ff94fc40b7f36ac46699b9e1254086299f8d3b223ca54a375 1975750 redis_5.0.5.orig.tar.gz\n+ 639fdb1be66542dc6b84e8c0bc30357e661bde809118cd1754e4cbbb83773a62 25956 redis_5.0.5-1chl1~bionic1.debian.tar.xz\n+Files:\n+ 2d2c8142baf72e6543174fc7beccaaa1 1975750 redis_5.0.5.orig.tar.gz\n+ 1dd668600931bcca0e25f86c5c2cc0aa 25956 redis_5.0.5-1chl1~bionic1.debian.tar.xz\n+\n+-----BEGIN PGP SIGNATURE-----\n+\n+iQEzBAEBCgAdFiEEtTSUDPRzjdEehKuC2ZMjAYFA0IoFAl1Tk1QACgkQ2ZMjAYFA\n+0Iqqzgf5AbOFgzqszqv4YxXg9hf+Iq5CCuw4J5U2Aid5fijLQVFPZaI0AgE3Br7C\n+0nxqvxYEIjKF+e2nX6zNjVk3JXYz3/Am/quyrSBI4KW2lIbpfCnhC8zCLOVcDtlX\n+jhdlg2rziqCDVaS1jGKR76vdz2FGSv/nAcGrjjOn0Lux7VhoaJgBoMgGCNigL8gB\n+Wo36UBhZQ6h3zXJjiJUee40ne55xYENhBoWpMvChOlA6/7cW4Xwf6PKBiqwKaRry\n+TYMTeLYgZ2S51ThK5zitfOPpBiP2xVuisL9NxgPiRTL1BnclJnOQzupX3xntNPtn\n+Ym34YnxbLTGwew232JvgK3ywMmwBeA==\n+=CJU0\n+-----END PGP SIGNATURE-----\n+\n"
  },
  {
    "path": "pkg/deb/debian_dh9/rules",
    "content": "#!/usr/bin/make -f\n\ninclude /usr/share/dpkg/buildflags.mk\n\n#LUA_LIBS_DEBIAN = cjson bitop\n#LUA_LIBS_BUNDLED = struct cmsgpack\n#\n#LUA_OBJECTS = $(addprefix lua_,$(addsuffix .o,$(LUA_LIBS_BUNDLED)))\n#LUA_LDFLAGS = $(addprefix -llua5.1-,$(LUA_LIBS_DEBIAN)) $(addprefix ../deps/lua/src/,$(LUA_OBJECTS))\n\nexport BUILD_TLS=yes\nexport USE_SYSTEMD=yes\nexport ENABLE_FLASH=yes\nexport CFLAGS CPPFLAGS LDFLAGS\nexport DEB_BUILD_MAINT_OPTIONS = hardening=+all\nexport DEB_LDFLAGS_MAINT_APPEND = -ldl -latomic $(LUA_LDFLAGS)\n\nifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))\n\tNUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))\n\tMAKEFLAGS += -j$(NUMJOBS)\n\texport MAKEFLAGS\nendif\n\n%:\n\tdh $@\n\noverride_dh_auto_install:\n\tdebian/bin/generate-systemd-service-files\n\n#override_dh_auto_build:\n#\tmake -C deps/lua/src $(LUA_OBJECTS)\n#\tdh_auto_build --parallel -- V=1 USE_SYSTEM_JEMALLOC=yes USE_SYSTEM_LUA=yes USE_SYSTEM_HIREDIS=yes\n\noverride_dh_auto_test:\nifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))\n\t# Avoid race conditions in upstream testsuite.\n#\t./runtest --clients 1 || true\n#\t./runtest-cluster || true\n#\t./runtest-sentinel || true\nendif\n\noverride_dh_auto_clean:\n\tdh_auto_clean\n\trm -f src/release.h debian/*.service\n\noverride_dh_compress:\n\tdh_compress -Xredis-trib.rb\n\noverride_dh_installchangelogs:\n\tdh_installchangelogs --keep 00-RELEASENOTES\n"
  },
  {
    "path": "pkg/deb/debian_dh9/source/format",
    "content": "3.0 (quilt)\n"
  },
  {
    "path": "pkg/deb/debian_dh9/source/include-binaries",
    "content": "keydb_5.0.6.orig.tar.gz\nkeydb_5.0.6-1chl1~bionic1.debian.tar.xz\n"
  },
  {
    "path": "pkg/deb/debian_dh9/source/lintian-overrides",
    "content": "# Upstream do not provide signed tarballs.\nkeydb source: debian-watch-does-not-check-gpg-signature\n"
  },
  {
    "path": "pkg/deb/debian_dh9/source/options",
    "content": "extend-diff-ignore = \"^\\.travis\\.yml$\"\n"
  },
  {
    "path": "pkg/deb/debian_dh9/tests/0001-keydb-cli",
    "content": "#!/bin/sh\n#\n# Show the INFO from \"keydb-cli\"\n\nset -eu\n\nkeydb-cli INFO\nkeydb-cli LOLWUT\n"
  },
  {
    "path": "pkg/deb/debian_dh9/tests/0002-benchmark",
    "content": "#!/bin/sh\n#\n# Run the benchmarking\n\nset -eu\n\nkeydb-benchmark -P 10\n"
  },
  {
    "path": "pkg/deb/debian_dh9/tests/0003-keydb-check-aof",
    "content": "#!/bin/sh\n#\n# Smoke test keydb-check-aof\n\nkeydb-check-aof 2>&1 | grep -qsi usage:\n"
  },
  {
    "path": "pkg/deb/debian_dh9/tests/0004-keydb-check-rdb",
    "content": "#!/bin/sh\n#\n# Test keydb-check-rdb\n\nset -eu\n\n# Perform a synchronous save to ensure .rdb file eixsts\nkeydb-cli SAVE\n\nkeydb-check-rdb /var/lib/keydb/dump.rdb\n"
  },
  {
    "path": "pkg/deb/debian_dh9/tests/control",
    "content": "Tests: 0001-keydb-cli\n\nTests: 0002-benchmark\n\nTests: 0003-keydb-check-aof\n\nTests: 0004-keydb-check-rdb\nRestrictions: needs-root\n"
  },
  {
    "path": "pkg/deb/debian_dh9/watch",
    "content": "version=4\nopts=uversionmangle=s/-?(alpha|beta|rc)/~$1/ \\\n\thttps://github.com/EQ-Alpha/KeyDB/releases .*/archive/(.*).tar.gz\n"
  },
  {
    "path": "pkg/deb/master_changelog",
    "content": "keydb (6:6.3.4-1distribution_placeholder) codename_placeholder; urgency=medium\n\n  * Add keydb_modstatsd, a module for providing keydb stats to a local statsd server. Can be found under src/modules/keydb_modstatsd\n  * Fixed FreeBSD compile(Thanks to @bra-fsn)\n  * Fixed a bug in s3 bucket config that blocked startup(Thanks to Alexandre Chichmanian)\n  * Fixed a bug causing crash if keys command is called after a blocking command (Fixes issues #659, #605, #532)\n  * Added proper error checking of replica configs, now KeyDB will throw an error if replica-of config is passed before active-replica or multimaster configs\n  * Fixed double free bug in lazy free\n  * Added \"overload-protect-percent\" config, when enabled this will load shed clients whenever CPU usage exceeds configured value\n  * Added \"availability-zone\" config, this can be passed any string which will then be reported by info command\n  * Updated eviction logic to account for total system memory availability using sysinfo\n  * Fixed a bug where repl-backlog-size config was modified in keydb.conf with the runtime value during config rewrite\n  * Fixed a possible deadlock when running CLIENT KILL with large number of clients\n  * Fixed a bug where KeyDB would overcommit memory during fork BG save\n  * Fixed a bug with disk repl_backlog causing double free\n  * Fixed integer overflow issue in the temp rdb file naming (Thanks to @karthyuom)\n  * Fixed compile issue with GCC 13.1.1 (Thanks to @michieldwitte)\n  * Removed expireset and restored redis expire behaviour\n  * Fixed a bug causing forked processes to hang, specifically affecting RDB and AOF(Fixes issues #675, #619)\n  * Added \"CLUSTER REPLICATE NO ONE\" to turn a replica into an empty primary\n  * Added RDB-less full sync, can be enabled with config 'enable-keydb-fastsync'\n  * Added \"flash-disable-key-cache\" config to disable key cache which stores every key(but not value) in memory\n  * Moved cluster slot to key map to being stored by storage provider, rather than in memory\n  * Moved expires to be stored by storage provider, rather than in memory\n  * Enabled expiry/eviction from storage provider, previously only data stored in memory could be expired/evicted(Fixes #645 along with the 2 previous updates)\n  * Fixed a bug where swapdb result was not recovered after keydb restarts in FLASH mode (Thanks to @karthyuom)\n  * Fixed double free bug when fast sync was canceled early\n  * Fixed a bug where a temp rdb file with zero bytes is generated in flash mode (Thanks to @karthyuom)\n  * Fixed a bug where flash CF options are being reset to default after flushall (Thanks to @karthyuom)(Fixes issue #717)\n  * Updated debug reload command to work with flash\n  * Added support for KEYDB_PASSWORD env variable(Thanks to @einar-pexip)\n  * Compile with flash in Dockerfile(Thanks to @der-eismann)\n  * Fix broken redis-cli symlink (Thanks to @pimvandenbroek)\n  * Use tini for alpine docker(Thanks to @rofafor)\n\n-- Malavan Sotheeswaran <msotheeswaran@snapchat.com>  Sun, 29 Oct 2023 8:39:21 +0000\n\nkeydb (6:6.3.3-1distribution_placeholder) codename_placeholder; urgency=medium\n\n  * To help accelerate our development efforts for KeyDB, this will be the last release containing support for Centos 7, Ubuntu 16.04, Ubuntu 18.04, Debian 9 and 32-bit builds\n  * This release contains fixes for 11 issues along with improvements to the KeyDB FLASH feature:\n  * Fixed race condition with expireset access (Issue #597)\n  * Fixed keys command with lua, and added keydb as an alternative to redis as lua variable to access db (Issue #562 and #594)\n  * Fixed hang in aof child (Issue #554)\n  * Fixed leaking fds from RDB save (Issue #453 and #584)\n  * Added config for S3 RDB load/save (Issue #584)\n  * Enabled active defrag during forkless background save to improve average memory efficiency (Issue #460)\n  * Fixed crash with fork background save during replication (Issue #567)\n  * RocksDB has been updated to v7.9.2\n  * Fixed missing slot_to_key map in FLASH cluster mode (Issue #574)\n  * Added keyspace notifications for keys loaded from FLASH at startup\n  * Fixed race condition in prefetchKeysAsync with FLASH enabled (Issue #571)\n  * Fixed integer overflow in rand family of commands(Issue #631, #632, #633)\n  * Fixed bad value in hincrbyfloat(Issue #634)\n  * Fixed OOM hang in rand family of commands(Issue #635)\n  * Added config to limit count of return values in rand family of commands(Issue #636)\n\n-- Ben Schermel <ben@eqalpha.com>  Tue, 18 Apr 2023 10:00:37 +0000\n\nkeydb (6:6.3.2-1distribution_placeholder) codename_placeholder; urgency=medium\n\n  * This release contains Beta level support for KeyDB FLASH, new ASYNC commands, latency improvements and a number of bug fixes. \n  * KeyDB FLASH is included as a Beta feature. FLASH allows KeyDB to persist data to the storage medium it is written to, avoiding the need for AOF/RDB files. KeyDB uses rocksdb as the persistent storage provider and can be enabled with config \"storage-provider flash /path/to/rocksdb/output\". Read more at https://docs.keydb.dev/docs/flash/\n  * In addition to GET/MGET, ASYNC support has been added for the following commands: HGET, HMGET, HKEYS, HVALS, HGETALL, HSCAN and can be enable with config \"enable-async-commands yes”\n  * Packaging support for Ubuntu 22.04 (Jammy) and Debian 12 (Bookworm) has been included with this release\n  * Added new soft shutdown feature, can be enabled with config \"soft-shutdown yes\".\n  * If soft shutdown is enabled, instead of shutting down right away, the server will wait until all clients have disconnected, and will reject all new connection attempts.\n  * Fixed memory leak with tls certificates when tls allowlist is enabled\n  * Fixed bug in rdb load with flash enabled to ensure all dbs are safe to load (previously only checked db[0])\n  * Fixed race conditions in rdb load and replication\n  * Fixed memory access of rdb file after it should have been deleted\n  * Fixed integer overflow bug in flash(Issue #486)\n  * Improve TLS latency by queueing new commands before executing instead of after\n  * Removed O(n) count of memory usage from info command(replaced by O(1) estimate)\n  * Improved latency of clearing large number of flash DBs(Thanks to Paul Chen for this fix)(Issue #516)\n  * replaced sprintf with snprintf to avoid potential security bugs\n  * Fixed bug where a failed move due to key already existing in move target would result in the key being removed from move source(Thanks to Paul Chen for this fix)(Issue #497)\n  * Fixed usage of deprecated OpenSSL api in OpenSSL v>3.0.1(Issue #392)\n  * Imported security fixes from Redis (CVE-2023-22458 CVE-2022-35977)\n  * Other fixed issues: #480 #477 #454 #452 #303 #425 #492 #541\n\n-- Ben Schermel <ben@eqalpha.com>  Fri, 20 Jan 2023 20:00:37 +0000\n\nkeydb (6:6.3.1-1distribution_placeholder) codename_placeholder; urgency=medium\n\n  * This point release contains fixes to bugs related to expires, active-rep, and rdb saving\n  * Issues fixed: #419, #422, #428\n  * PRs: #426, #429, #431, #433\n\n-- Ben Schermel <ben@eqalpha.com>  Mon, 23 May 2022 20:00:37 +0000\n\nkeydb (6:6.3.0-1distribution_placeholder) codename_placeholder; urgency=medium\n\n  * This release open sources KeyDB Enterprise features into the open source project along with PSYNC for active replication\n  * Partial synchronization for active replication is introduced\n  * MVCC introduced into codebase from KeyDB Enterprise\n  * Async commands added: GET, MGET. These will see perf improvements\n  * KEYS and SCAN commands will no longer be blocking calls\n  * Async Rehash implemented for additional stability to perf\n  * IStorage interface added\n  * In-process background saving (forkless) to comply with maxmemory setting\n  * See v6.3.0 tagged release notes on github for a detailed explanation of these changes\n\n-- Ben Schermel <ben@eqalpha.com>  Wed, 11 May 2022 20:00:37 +0000\n\nkeydb (6:6.2.2-1distribution_placeholder) codename_placeholder; urgency=medium\n\n  * Acquire lock in module.cpp to fix module test break\n  * Fix usage of nanosleep() on Apple hardware\n  * Fix networking deadlock (#183)\n  * Remove redundant zfree() in keydb-benchmark\n  * Issues resolved: #378, #384\n\n-- Ben Schermel <ben@eqalpha.com>  Sat, 15 Jan 2022 20:00:37 +0000\n\nkeydb (6:6.2.1-1distribution_placeholder) codename_placeholder; urgency=medium\n\n  * This release of KeyDB is at parity with Redis 6.2.6. In addition, the following changes were included:\n  * Systemd support for release packages \n  * Fixes to buffer overflows in the `BITOP SHIFT` and `CRON` commands \n  * Server times are now computed on a seperate thread to improve performance \n  * Now enforces syslog identity and facility as soon as possible \n  * Removed erroneous use of LOG_... flags when using syslog \n  * Fixed erroneous `#endif` leading to build errors on some platforms \n  * Fixed the incorrect counting of client connections \n  * Issues resolved: #355, #370\n\n -- Ben Schermel <ben@eqalpha.com>  Wed, 17 Nov 2021 20:00:37 +0000\n\nkeydb (6:6.2.0-1distribution_placeholder) codename_placeholder; urgency=medium\n\n  * Removed unused command line options(-a, -d, -P, -r, -q, --csv, -l, -I, -e, --precision, --cluster, --enable-tracking)\n  * --ms option changed to --time, added --clients, --host, --port, and --threads options\n  * Added keydb-diagnostic-tool\n  * Improved test reliability\n  * Fixed memory leak in keydb-benchmark\n  * Fixed a number of race conditions\n  * Fixed OSX build issues\n  * Feature parity with Redis 6.2.3\n  * Fixed Issues: #323 #325 #276\n\n -- Ben Schermel <ben@eqalpha.com>  Thu, 12 Aug 2021 20:00:37 +0000\n\nkeydb (6:6.0.18-1distribution_placeholder) codename_placeholder; urgency=medium\n\n  * 6.0.18 - This release contains a number of improvements to stability and performance. \n  * Additional tests were added with improvements to existing test reliability\n  * Merged Redis 6.0.10 into KeyDB\n  * Issues resolved in this release: #214, #222, #238, #257, #273, #276, #285\n\n -- Ben Schermel <ben@eqalpha.com>  Fri, 26 Mar 2021 16:00:00 +0000\n\nkeydb (6:6.0.16-1distribution_placeholder) codename_placeholder; urgency=medium\n\n  * 6.0.16 - This release contains significant performance improvements as well as fixes to the following issues: #234, #236, #233, #207, #229, #231\n  * Reduced memory consumption when Active Replication is not used\n  * Reduced CPU consumption\n  * Improved TLS performance\n  * replica-quorum config for multi-master.Intended to be used with \"replica-serve-stale-data no\", if at least N replicas are connected we will consider our master link up and serve data.\n  * Subkey expires performance improvement for big sets\n\n -- Ben Schermel <ben@eqalpha.com>  Mon, 28 Sep 2020 16:00:00 +0000\n\nkeydb (6:6.0.13-1distribution_placeholder) codename_placeholder; urgency=medium\n\n  * 6.0.13 New features added: KEYDB.MEXISTS, REPLICAOF REMOVE, multi-master-no-forward configuration option\n  * KEYDB.MEXISTS - return exactly which keys exist in the database not just the count\n  * REPLICAOF REMOVE - Remove specific masters in a multi master setup\n  * multi-master-no-forward configuration option - Reduce network traffic in mesh topology multimaster setups\n  * Redis 6.0.5 feature parity\n  * Fixed Issues: 194 Bad directive or wrong number of arguments error on 'lazyfree-lazy-user-del no'; 209 Timeout using modules; 210 Databases are not merged on multi-master sync; 211 rpm packages now signed; 212 rpm file permissions updated\n\n -- Ben Schermel <ben@eqalpha.com>  Mon, 13 Jul 2020 8:00:00 +0000\n\nkeydb (6:6.0.9-1distribution_placeholder) codename_placeholder; urgency=medium\n\n  * 6.0.9 Addressing issues 187 - cpu lockup with subkey expire, 190 - missing sentinel binary in keydb-tools. \n\n -- Ben Schermel <ben@eqalpha.com>  Sun, 07 Jun 2020 18:00:37 +0000\n\nkeydb (6:6.0.8-1distribution_placeholder) codename_placeholder; urgency=medium\n\n  * This is our first release to fully support all Redis 6.0.4 features, including but not limited to: TLS Support (fully supports multithreading!), Client side caching, RESP 3 Support\n  * KeyDB Has also added the following new features: Improved memory efficiency for short strings, Fastlock autotuning, KeyDB.HRENAME (rename a member of a hash)In addition we've spent a lot of time focussing on stability.\n  * The following bug fixes are resolved: 150 - Deadlock in ReplicationFeedMonitors, 170 - KeyDB dying via SIGABORT, 180 - crash after setting maxclients via cmd line, 169 - Write performance of a master dropped sharply when a replica is added\n  * In addition untracked issues were resolved: Potential deadlock when entering futex sleep, Pub/Sub Async messages may not be sent in a timely manner when load is low, Excessive logging during failed RREPLAY, KeyDB unresponsive handling clients on different threads during RDB load\n  * Updated deb package builds to build from source and phasing out init.d. dh_installsystemd from deb helper 11 used where applicable\n  * Naming conventions are now updated [ keydbpackage_version-build~distribution_architecture] and master changelog included and maintained\n  * keydb-pro-server binary is no longer inluded in the open source package\n\n -- Ben Schermel <ben@eqalpha.com>  Mon, 1 Jun 2020 8:00:37 +0000\n\nkeydb (5:5.3.3-1distribution_placeholder) codename_placeholder; urgency=medium\n\n  * 5.3.3 Updating deb source package, naming conventions, and build scripts.\n\n -- Ben Schermel <ben@eqalpha.com>  Wed, 06 May 2020 8:00:37 +0000\n\nkeydb (5:5.1.12-1chl1distribution_placeholder) codename_placeholder; urgency=medium\n\n  * 5.1.1 update. This update fixes several rare deadlock scenarios. Deadlock detection is also added.  \n\n -- Ben Schermel <ben@eqalpha.com>  Fri, 25 Oct 2019 8:00:37 +0000\n\n\nkeydb (5:5.1.11-1chl1distribution_placeholder) codename_placeholder; urgency=medium\n\n  * 5.1 release. This release includes subkey expires (EXPIREMEMBER/EXPIREMEMBERAT), with updates to PTTL/TTL accordingly. New OBJECT LASTMODIFIED, BITIOP LSHIFT & BITOP RSHIFT commands. See https://docs.keydb.dev/blog/2019/10/20/blog-post/ for detailed review of release.\n\n -- Ben Schermel <ben@eqalpha.com>  Mon, 21 Oct 2019 8:00:37 +0000\n\n\nkeydb (5:5.0.1-1chl1distribution_placeholder) codename_placeholder; urgency=medium\n\n  * Arm build now included for bionic package\n\n -- Ben Schermel <ben@eqalpha.com>  Wed, 21 Aug 2019 22:58:37 +0000\n\n\nkeydb (5:5.0.0-1chl1distribution_placeholder) codename_placeholder; urgency=medium\n\n  * Initial release of KeyDB PPA. This PPA was originally derived from https://launchpad.net/~chris-lea/+archive/ubuntu/redis-server\n\n -- Ben Schermel <ben@eqalpha.com>  Wed, 21 Aug 2019 2:58:37 +0000\n"
  },
  {
    "path": "pkg/docker/Dockerfile",
    "content": "FROM ubuntu:20.04\nSHELL [\"/bin/bash\",\"-c\"]\nRUN groupadd -r keydb && useradd -r -g keydb keydb\n# use gosu for easy step-down from root: https://github.com/tianon/gosu/releases\nENV GOSU_VERSION 1.14\nRUN set -eux; \\\n        savedAptMark=\"$(apt-mark showmanual)\"; \\\n        apt-get update; \\\n        apt-get install -y --no-install-recommends ca-certificates dirmngr gnupg wget; \\\n        rm -rf /var/lib/apt/lists/*; \\\n        dpkgArch=\"$(dpkg --print-architecture | awk -F- '{ print $NF }')\"; \\\n        wget -O /usr/local/bin/gosu \"https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch\"; \\\n        wget -O /usr/local/bin/gosu.asc \"https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch.asc\"; \\\n        export GNUPGHOME=\"$(mktemp -d)\"; \\\n        gpg --batch --keyserver hkps://keys.openpgp.org --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4; \\\n        gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu; \\\n        gpgconf --kill all; \\\n        rm -rf \"$GNUPGHOME\" /usr/local/bin/gosu.asc; \\\n        apt-mark auto '.*' > /dev/null; \\\n        [ -z \"$savedAptMark\" ] || apt-mark manual $savedAptMark > /dev/null; \\\n        apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \\\n        chmod +x /usr/local/bin/gosu; \\\n        gosu --version; \\\n        gosu nobody true\n# build KeyDB\nARG BRANCH\nRUN set -eux; \\\n        \\\n        savedAptMark=\"$(apt-mark showmanual)\"; \\\n        apt-get update; \\\n        DEBIAN_FRONTEND=noninteractive apt-get install -qqy --no-install-recommends \\\n                dpkg-dev \\\n                pkg-config \\\n                ca-certificates \\\n                build-essential \\\n                nasm \\\n                autotools-dev \\\n                autoconf \\\n                libjemalloc-dev \\\n                tcl \\\n                tcl-dev \\\n                uuid-dev \\\n                libcurl4-openssl-dev \\\n                libbz2-dev \\\n                libzstd-dev \\\n                liblz4-dev \\\n                libsnappy-dev \\\n                libssl-dev \\\n                git; \\\n        cd /tmp && git clone --branch $BRANCH https://github.com/Snapchat/KeyDB.git --recursive; \\\n        cd /tmp/KeyDB; \\\n        # disable protected mode as it relates to docker\n        grep -E '^ *createBoolConfig[(]\"protected-mode\",.*, *1 *,.*[)],$' ./src/config.cpp; \\\n        sed -ri 's!^( *createBoolConfig[(]\"protected-mode\",.*, *)1( *,.*[)],)$!\\10\\2!' ./src/config.cpp; \\\n        grep -E '^ *createBoolConfig[(]\"protected-mode\",.*, *0 *,.*[)],$' ./src/config.cpp; \\\n        make -j$(nproc) BUILD_TLS=yes ENABLE_FLASH=yes; \\\n        cd src; \\\n        strip keydb-cli keydb-benchmark keydb-check-rdb keydb-check-aof keydb-diagnostic-tool keydb-sentinel keydb-server; \\\n        mv keydb-server keydb-cli keydb-benchmark keydb-check-rdb keydb-check-aof keydb-diagnostic-tool keydb-sentinel /usr/local/bin/; \\\n        # clean up unused dependencies\n        echo $savedAptMark; \\\n        apt-mark auto '.*' > /dev/null; \\\n        [ -z \"$savedAptMark\" ] || apt-mark manual $savedAptMark > /dev/null; \\\n        find /usr/local -type f -executable -exec ldd '{}' ';' \\\n               | awk '/=>/ { print $(NF-1) }' \\\n               | sed 's:.*/::' \\\n               | sort -u \\\n               | xargs -r dpkg-query --search \\\n               | cut -d: -f1 \\\n               | sort -u \\\n               | xargs -r apt-mark manual \\\n        ; \\\n        apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \\\n        rm -rf /var/lib/apt/lists/*; \\\n# create working directories and organize files\nRUN \\\n        mkdir /data && chown keydb:keydb /data; \\\n        mkdir /flash && chown keydb:keydb /flash; \\\n        mkdir -p /etc/keydb; \\\n        cp /tmp/KeyDB/keydb.conf /etc/keydb/; \\\n        sed -i 's/^\\(daemonize .*\\)$/# \\1/' /etc/keydb/keydb.conf; \\\n        sed -i 's/^\\(dir .*\\)$/# \\1\\ndir \\/data/' /etc/keydb/keydb.conf; \\\n        sed -i 's/^\\(logfile .*\\)$/# \\1/' /etc/keydb/keydb.conf; \\\n        sed -i 's/protected-mode yes/protected-mode no/g' /etc/keydb/keydb.conf; \\\n        sed -i 's/^\\(bind .*\\)$/# \\1/' /etc/keydb/keydb.conf; \\\n        cd /usr/local/bin; \\\n        ln -s keydb-cli redis-cli; \\\n        cd /etc/keydb; \\\n        ln -s keydb.conf redis.conf; \\\n        rm -rf /tmp/*\n# generate entrypoint script\nRUN set -eux; \\\n        echo '#!/bin/sh' > /usr/local/bin/docker-entrypoint.sh; \\\n        echo 'set -e' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo \"# first arg is '-f' or '--some-option'\" >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo \"# or first arg is `something.conf`\" >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo 'if [ \"${1#-}\" != \"$1\" ] || [ \"${1%.conf}\" != \"$1\" ]; then' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo '        set -- keydb-server \"$@\"' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo 'fi' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo '# if KEYDB_PASSWORD is set, add it to the arguments' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo 'if [ -n \"$KEYDB_PASSWORD\" ]; then' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo '        set -- \"$@\" --requirepass \"${KEYDB_PASSWORD}\"' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo 'fi' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo \"# allow the container to be started with `--user`\" >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo 'if [ \"$1\" = \"keydb-server\" -a \"$(id -u)\" = \"0\" ]; then' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo \"        find . \\! -user keydb -exec chown keydb '{}' +\" >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo '        exec gosu keydb \"$0\" \"$@\"' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo 'fi' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo 'exec \"$@\"' >> /usr/local/bin/docker-entrypoint.sh; \\\n        chmod +x /usr/local/bin/docker-entrypoint.sh\n# set remaining image properties\nVOLUME /data\nWORKDIR /data\nENV KEYDB_PRO_DIRECTORY=/usr/local/bin/\nENTRYPOINT [\"docker-entrypoint.sh\"]\nEXPOSE 6379\nCMD [\"keydb-server\",\"/etc/keydb/keydb.conf\"]\n"
  },
  {
    "path": "pkg/docker/Dockerfile_Alpine",
    "content": "FROM alpine:3.18\n# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added\nRUN addgroup -S -g 1000 keydb && adduser -S -G keydb -u 999 keydb\nRUN mkdir -p /etc/keydb\nARG BRANCH\nRUN set -eux; \\\n        \\\n        apk add --no-cache su-exec tini; \\\n        apk add --no-cache --virtual .build-deps \\\n                coreutils \\\n                gcc \\\n                linux-headers \\\n                make \\\n                musl-dev \\\n                openssl-dev \\\n                git \\\n                util-linux-dev \\\n                curl-dev \\\n                g++ \\\n                libunwind-dev \\\n                bash \\\n                perl \\\n                git \\\n                bzip2-dev \\\n                zstd-dev \\\n                lz4-dev \\\n                snappy-dev \\\n        ; \\\n        cd /tmp && git clone --branch $BRANCH https://github.com/Snapchat/KeyDB.git --recursive; \\\n        cd /tmp/KeyDB; \\\n        # disable protected mode as it relates to docker\n        grep -E '^ *createBoolConfig[(]\"protected-mode\",.*, *1 *,.*[)],$' ./src/config.cpp; \\\n        sed -ri 's!^( *createBoolConfig[(]\"protected-mode\",.*, *)1( *,.*[)],)$!\\10\\2!' ./src/config.cpp; \\\n        grep -E '^ *createBoolConfig[(]\"protected-mode\",.*, *0 *,.*[)],$' ./src/config.cpp; \\\n        make -j$(nproc) BUILD_TLS=yes ENABLE_FLASH=yes; \\\n        cd src; \\\n        strip keydb-cli keydb-benchmark keydb-check-rdb keydb-check-aof keydb-diagnostic-tool keydb-sentinel keydb-server; \\\n        mv keydb-server keydb-cli keydb-benchmark keydb-check-rdb keydb-check-aof keydb-diagnostic-tool keydb-sentinel /usr/local/bin/; \\\n        runDeps=\"$( \\\n                scanelf --needed --nobanner --format '%n#p' --recursive /usr/local \\\n                        | tr ',' '\\n' \\\n                        | sort -u \\\n                        | awk 'system(\"[ -e /usr/local/lib/\" $1 \" ]\") == 0 { next } { print \"so:\" $1 }' \\\n        )\"; \\\n        apk add --no-network --virtual .keydb-rundeps $runDeps; \\\n        apk del --no-network .build-deps; \\\n        # create working directories and organize files\n        mkdir /data && chown keydb:keydb /data; \\\n        mkdir /flash && chown keydb:keydb /flash; \\\n        mkdir -p /etc/keydb; \\\n        cp /tmp/KeyDB/keydb.conf /etc/keydb/; \\\n        sed -i 's/^\\(daemonize .*\\)$/# \\1/' /etc/keydb/keydb.conf; \\\n        sed -i 's/^\\(dir .*\\)$/# \\1\\ndir \\/data/' /etc/keydb/keydb.conf; \\\n        sed -i 's/^\\(logfile .*\\)$/# \\1/' /etc/keydb/keydb.conf; \\\n        sed -i 's/protected-mode yes/protected-mode no/g' /etc/keydb/keydb.conf; \\\n        sed -i 's/^\\(bind .*\\)$/# \\1/' /etc/keydb/keydb.conf; \\\n        cd /usr/local/bin; \\\n        ln -s keydb-cli redis-cli; \\\n        cd /etc/keydb; \\\n        ln -s keydb.conf redis.conf; \\\n        rm -rf /tmp/*\n# generate entrypoint script\nRUN set -eux; \\\n        echo '#!/bin/sh' > /usr/local/bin/docker-entrypoint.sh; \\\n        echo 'set -e' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo \"# first arg is '-f' or '--some-option'\" >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo \"# or first arg is `something.conf`\" >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo 'if [ \"${1#-}\" != \"$1\" ] || [ \"${1%.conf}\" != \"$1\" ]; then' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo '        set -- keydb-server \"$@\"' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo 'fi' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo '# if KEYDB_PASSWORD is set, add it to the arguments' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo 'if [ -n \"$KEYDB_PASSWORD\" ]; then' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo '        set -- \"$@\" --requirepass \"${KEYDB_PASSWORD}\"' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo 'fi' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo \"# allow the container to be started with `--user`\" >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo 'if [ \"$1\" = \"keydb-server\" -a \"$(id -u)\" = \"0\" ]; then' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo \"        find . \\! -user keydb -exec chown keydb '{}' +\" >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo '        exec su-exec keydb \"$0\" \"$@\"' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo 'fi' >> /usr/local/bin/docker-entrypoint.sh; \\\n        echo 'exec \"$@\"' >> /usr/local/bin/docker-entrypoint.sh; \\\n        chmod +x /usr/local/bin/docker-entrypoint.sh\nVOLUME /data\nWORKDIR /data\nENTRYPOINT [\"tini\", \"--\", \"docker-entrypoint.sh\"]\nEXPOSE 6379\nCMD [\"keydb-server\", \"/etc/keydb/keydb.conf\"]\n"
  },
  {
    "path": "pkg/docker/README.md",
    "content": "This Dockerfile will clone the KeyDB repo, build, and generate a Docker image you can use\n\nTo build, use experimental mode to enable use of build args. Tag the build and specify branch name. The command below will generate your docker image:\n\n```\nDOCKER_CLI_EXPERIMENTAL=enabled docker build --build-arg BRANCH=<keydbBranch> -t <yourImageName>\n```\n"
  },
  {
    "path": "pkg/rpm/README.md",
    "content": "### Generate RPM files for the generated binaries\n\nAfter making the binaries you can run the following script\n\nUsage: \n```\n$ cd KeyDB/pkg/rpm\n$ sudo ./generate-rpms.sh\n```\n\nThis rpm script is currently tested on centos 7 and centos 8 builds\n\nDependencies:\n```\nyum install -y scl-utils centos-release-scl rpm-build\n```\n"
  },
  {
    "path": "pkg/rpm/generate_rpms.sh",
    "content": "#! /bin/bash\n\n### usage sudo ./generate_rpms\nDIR=\"$( cd \"$( dirname \"${BASH_SOURCE[0]}\" )\" >/dev/null 2>&1 && pwd )\"\nversion=$(grep KEYDB_REAL_VERSION $DIR/../../src/version.h | awk '{ printf $3 }' | tr -d \\\")\nrelease=1 # by default this will always be 1 for keydb version structure. If build release version needs to be update you can modify here\narch=$(uname -m)\ndist=$(rpm --eval '%{?dist}')\n\nif [[ \"$arch\" != \"aarch64\" ]] && [[ \"$arch\" != \"x86_64\" ]]; then\n\techo \"This script is only valid and tested for aarch64 and x86_64 architectures. You are trying to use: $arch\"\nfi\n\n# remove any old rpm packages\nrm $DIR/rpm_files_generated/keydb*\n\n# generate empty directories that github would otherwise delete (avoids .gitkeep in directory)\nmkdir -p $DIR/keydb_build/keydb_rpm/usr/bin\nmkdir -p $DIR/keydb_build/keydb_rpm/usr/lib64/redis/modules\nmkdir -p $DIR/keydb_build/keydb_rpm/var/lib/keydb\nmkdir -p $DIR/keydb_build/keydb_rpm/var/log/keydb\n\n# move binaries to bin\nrm $DIR/keydb_build/keydb_rpm/usr/bin/*\ncp $DIR/../../src/keydb-server $DIR/keydb_build/keydb_rpm/usr/bin/\ncp $DIR/../../src/keydb-sentinel $DIR/keydb_build/keydb_rpm/usr/bin/\ncp $DIR/../../src/keydb-cli $DIR/keydb_build/keydb_rpm/usr/bin/\ncp $DIR/../../src/keydb-benchmark $DIR/keydb_build/keydb_rpm/usr/bin/\ncp $DIR/../../src/keydb-check-aof $DIR/keydb_build/keydb_rpm/usr/bin/\ncp $DIR/../../src/keydb-check-rdb $DIR/keydb_build/keydb_rpm/usr/bin/\ncp $DIR/../../src/keydb-diagnostic-tool $DIR/keydb_build/keydb_rpm/usr/bin/\n\n# update spec file with build info\nsed -i '2d' $DIR/keydb_build/keydb.spec\nsed -i -E \"1a\\Version     : $version\" $DIR/keydb_build/keydb.spec\nsed -i '3d' $DIR/keydb_build/keydb.spec\nsed -i -E \"2a\\Release     : $release%{?dist}\" $DIR/keydb_build/keydb.spec\n\nmkdir -p /root/rpmbuild/BUILDROOT/keydb-$version-$release$dist.$arch\ncp -r $DIR/keydb_build/keydb_rpm/* /root/rpmbuild/BUILDROOT/keydb-$version-$release$dist.$arch/\nrpmbuild -bb $DIR/keydb_build/keydb.spec\nmv /root/rpmbuild/RPMS/$arch/* $DIR/rpm_files_generated\n\nexit\n"
  },
  {
    "path": "pkg/rpm/keydb_build/keydb.spec",
    "content": "Name        : keydb\nVersion     : 6.0.5\nRelease     : 1%{?dist}\nGroup       : Unspecified\nLicense     : BSD\nPackager    : Snap Inc.\n\nURL         : https://keydb.dev\nSummary     : A persistent key-value database\n\nRequires:          /bin/awk\nRequires:          logrotate\nRequires(pre):     shadow-utils\nRequires(post):    systemd\nRequires(preun):   systemd\nRequires(postun):  systemd\nRequires(post):    chkconfig\nRequires(preun):   chkconfig\nRequires(preun):   initscripts\nRequires(postun):  initscripts\n\n# scripts\n\n#preinstall scriptlet (using /bin/sh):\n%pre\ngetent group keydb &> /dev/null || \\\ngroupadd -r keydb &> /dev/null\ngetent passwd keydb &> /dev/null || \\\nuseradd -r -g keydb -d /var/lib/keydb -s /sbin/nologin \\\n-c 'KeyDB Database Server' keydb &> /dev/null\nexit 0\n\n#postinstall scriptlet (using /bin/sh):\n%post\nif [ $1 -eq 1 ] ; then \n        # Initial installation \n        systemctl preset keydb.service >/dev/null 2>&1 || : \nfi \n\n\nif [ $1 -eq 1 ] ; then \n        # Initial installation \n        systemctl preset keydb-sentinel.service >/dev/null 2>&1 || : \nfi\n\nchown -R keydb:keydb /etc/keydb\nchown -R keydb:keydb /var/log/keydb\nchown -R keydb:keydb /var/lib/keydb\nchown -R keydb:keydb /usr/libexec/keydb-shutdown\n\n#preuninstall scriptlet (using /bin/sh):\n%preun\nif [ $1 -eq 0 ] ; then \n        # Package removal, not upgrade \n        systemctl --no-reload disable keydb.service > /dev/null 2>&1 || : \n        systemctl stop keydb.service > /dev/null 2>&1 || : \nfi \n\n\nif [ $1 -eq 0 ] ; then \n        # Package removal, not upgrade \n        systemctl --no-reload disable keydb-sentinel.service > /dev/null 2>&1 || : \n        systemctl stop keydb-sentinel.service > /dev/null 2>&1 || : \nfi\n\n#postuninstall scriptlet (using /bin/sh):\n%postun\nsystemctl daemon-reload >/dev/null 2>&1 || : \nif [ $1 -ge 1 ] ; then \n        # Package upgrade, not uninstall \n        systemctl try-restart keydb.service >/dev/null 2>&1 || : \nfi \n\n\nsystemctl daemon-reload >/dev/null 2>&1 || : \nif [ $1 -ge 1 ] ; then \n        # Package upgrade, not uninstall \n        systemctl try-restart keydb-sentinel.service >/dev/null 2>&1 || : \nfi\n\n\n%Description\nKeyDB is an advanced key-value store. It is often referred to as a data\nstructure server since keys can contain strings, hashes, lists, sets and\nsorted sets.\n\nYou can run atomic operations on these types, like appending to a string;\nincrementing the value in a hash; pushing to a list; computing set\nintersection, union and difference; or getting the member with highest\nranking in a sorted set.\n\nIn order to achieve its outstanding performance, KeyDB works with an\nin-memory dataset. Depending on your use case, you can persist it either\nby dumping the dataset to disk every once in a while, or by appending\neach command to a log.\n\nKeyDB also supports typical master-replica setups, active replica setups\nand multi master setups.\n\nOther features include Transactions, Pub/Sub, Lua scripting, Keys with a\nlimited time-to-live, and configuration settings to make KeyDB behave like\na cache.\n\nYou can use KeyDB from most programming languages also.\n\n%files\n/etc/logrotate.d/keydb\n/etc/systemd/system/keydb.service.d/limit.conf\n/etc/systemd/system/keydb-sentinel.service.d/limit.conf\n/usr/bin/*\n/usr/lib/systemd/system/*\n/usr/libexec/*\n/usr/share/licenses/*\n/usr/share/man/man1/*\n/usr/share/man/man5/*\n/var/lib/*\n/var/log/*\n/usr/lib64/redis/modules\n%config(noreplace) /etc/keydb/*\n"
  },
  {
    "path": "pkg/rpm/keydb_build/keydb_rpm/etc/keydb/keydb.conf",
    "content": "# KeyDB configuration file example.\n#\n# Note that in order to read the configuration file, KeyDB must be\n# started with the file path as first argument:\n#\n# ./keydb-server /path/to/keydb.conf\n\n# Note on units: when memory size is needed, it is possible to specify\n# it in the usual form of 1k 5GB 4M and so forth:\n#\n# 1k => 1000 bytes\n# 1kb => 1024 bytes\n# 1m => 1000000 bytes\n# 1mb => 1024*1024 bytes\n# 1g => 1000000000 bytes\n# 1gb => 1024*1024*1024 bytes\n#\n# units are case insensitive so 1GB 1Gb 1gB are all the same.\n\n################################## INCLUDES ###################################\n\n# Include one or more other config files here.  This is useful if you\n# have a standard template that goes to all KeyDB servers but also need\n# to customize a few per-server settings.  Include files can include\n# other files, so use this wisely.\n#\n# Note that option \"include\" won't be rewritten by command \"CONFIG REWRITE\"\n# from admin or KeyDB Sentinel. Since KeyDB always uses the last processed\n# line as value of a configuration directive, you'd better put includes\n# at the beginning of this file to avoid overwriting config change at runtime.\n#\n# If instead you are interested in using includes to override configuration\n# options, it is better to use include as the last line.\n#\n# Included paths may contain wildcards. All files matching the wildcards will\n# be included in alphabetical order.\n# Note that if an include path contains a wildcards but no files match it when\n# the server is started, the include statement will be ignored and no error will\n# be emitted.  It is safe, therefore, to include wildcard files from empty\n# directories.\n#\n# include /path/to/local.conf\n# include /path/to/other.conf\n# include /path/to/fragments/*.conf\n#\n\n################################## MODULES #####################################\n\n# Load modules at startup. If the server is not able to load modules\n# it will abort. It is possible to use multiple loadmodule directives.\n#\n# loadmodule /path/to/my_module.so\n# loadmodule /path/to/other_module.so\n\n################################## NETWORK #####################################\n\n# By default, if no \"bind\" configuration directive is specified, KeyDB listens\n# for connections from all available network interfaces on the host machine.\n# It is possible to listen to just one or multiple selected interfaces using\n# the \"bind\" configuration directive, followed by one or more IP addresses.\n# Each address can be prefixed by \"-\", which means that redis will not fail to\n# start if the address is not available. Being not available only refers to\n# addresses that does not correspond to any network interfece. Addresses that\n# are already in use will always fail, and unsupported protocols will always BE\n# silently skipped.\n#\n# Examples:\n#\n# bind 192.168.1.100 10.0.0.1     # listens on two specific IPv4 addresses\n# bind 127.0.0.1 ::1              # listens on loopback IPv4 and IPv6\n# bind * -::*                     # like the default, all available interfaces\n#\n# ~~~ WARNING ~~~ If the computer running KeyDB is directly exposed to the\n# internet, binding to all the interfaces is dangerous and will expose the\n# instance to everybody on the internet. So by default we uncomment the\n# following bind directive, that will force KeyDB to listen only on the\n# IPv4 and IPv6 (if available) loopback interface addresses (this means KeyDB will only be able to\n# accept client connections from the same host that it is running on).\n#\n# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES\n# JUST COMMENT OUT THE FOLLOWING LINE.\n#\n# You will also need to set a password unless you explicitly disable protected\n# mode.\n# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nbind 127.0.0.1 -::1\n\n# Protected mode is a layer of security protection, in order to avoid that\n# KeyDB instances left open on the internet are accessed and exploited.\n#\n# When protected mode is on and if:\n#\n# 1) The server is not binding explicitly to a set of addresses using the\n#    \"bind\" directive.\n# 2) No password is configured.\n#\n# The server only accepts connections from clients connecting from the\n# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain\n# sockets.\n#\n# By default protected mode is enabled. You should disable it only if\n# you are sure you want clients from other hosts to connect to KeyDB\n# even if no authentication is configured, nor a specific set of interfaces\n# are explicitly listed using the \"bind\" directive.\nprotected-mode yes\n\n# Accept connections on the specified port, default is 6379 (IANA #815344).\n# If port 0 is specified KeyDB will not listen on a TCP socket.\nport 6379\n\n# TCP listen() backlog.\n#\n# In high requests-per-second environments you need a high backlog in order\n# to avoid slow clients connection issues. Note that the Linux kernel\n# will silently truncate it to the value of /proc/sys/net/core/somaxconn so\n# make sure to raise both the value of somaxconn and tcp_max_syn_backlog\n# in order to get the desired effect.\ntcp-backlog 511\n\n# Unix socket.\n#\n# Specify the path for the Unix socket that will be used to listen for\n# incoming connections. There is no default, so KeyDB will not listen\n# on a unix socket when not specified.\n#\n# unixsocket /tmp/keydb.sock\n# unixsocketperm 700\n\n# Close the connection after a client is idle for N seconds (0 to disable)\ntimeout 0\n\n# TCP keepalive.\n#\n# If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence\n# of communication. This is useful for two reasons:\n#\n# 1) Detect dead peers.\n# 2) Force network equipment in the middle to consider the connection to be\n#    alive.\n#\n# On Linux, the specified value (in seconds) is the period used to send ACKs.\n# Note that to close the connection the double of the time is needed.\n# On other kernels the period depends on the kernel configuration.\n#\n# A reasonable value for this option is 300 seconds, which is the new\n# KeyDB default starting with KeyDB 3.2.1.\ntcp-keepalive 300\n\n################################# TLS/SSL #####################################\n\n# By default, TLS/SSL is disabled. To enable it, the \"tls-port\" configuration\n# directive can be used to define TLS-listening ports. To enable TLS on the\n# default port, use:\n#\n# port 0\n# tls-port 6379\n\n# Configure a X.509 certificate and private key to use for authenticating the\n# server to connected clients, masters or cluster peers.  These files should be\n# PEM formatted.\n#\n# tls-cert-file keydb.crt \n# tls-key-file keydb.key\n#\n# If the key file is encrypted using a passphrase, it can be included here\n# as well.\n#\n# tls-key-file-pass secret\n\n# Normally KeyDB uses the same certificate for both server functions (accepting\n# connections) and client functions (replicating from a master, establishing\n# cluster bus connections, etc.).\n#\n# Sometimes certificates are issued with attributes that designate them as\n# client-only or server-only certificates. In that case it may be desired to use\n# different certificates for incoming (server) and outgoing (client)\n# connections. To do that, use the following directives:\n#\n# tls-client-cert-file client.crt\n# tls-client-key-file client.key\n#\n# If the key file is encrypted using a passphrase, it can be included here\n# as well.\n#\n# tls-client-key-file-pass secret\n\n# Configure a DH parameters file to enable Diffie-Hellman (DH) key exchange:\n#\n# tls-dh-params-file keydb.dh\n\n# Configure a CA certificate(s) bundle or directory to authenticate TLS/SSL\n# clients and peers. KeyDB requires an explicit configuration of at least one\n# of these, and will not implicitly use the system wide configuration.\n#\n# tls-ca-cert-file ca.crt\n# tls-ca-cert-dir /etc/ssl/certs\n\n# By default, clients (including replica servers) on a TLS port are required\n# to authenticate using valid client side certificates.\n#\n# If \"no\" is specified, client certificates are not required and not accepted.\n# If \"optional\" is specified, client certificates are accepted and must be\n# valid if provided, but are not required.\n#\n# tls-auth-clients no\n# tls-auth-clients optional\n\n# By default, a KeyDB replica does not attempt to establish a TLS connection\n# with its master.\n#\n# Use the following directive to enable TLS on replication links.\n#\n# tls-replication yes\n\n# By default, the KeyDB Cluster bus uses a plain TCP connection. To enable\n# TLS for the bus protocol, use the following directive:\n#\n# tls-cluster yes\n\n# Explicitly specify TLS versions to support. Allowed values are case insensitive\n# and include \"TLSv1\", \"TLSv1.1\", \"TLSv1.2\", \"TLSv1.3\" (OpenSSL >= 1.1.1) or\n# any combination. To enable only TLSv1.2 and TLSv1.3, use:\n#\n# tls-protocols \"TLSv1.2 TLSv1.3\"\n\n# Configure allowed ciphers.  See the ciphers(1ssl) manpage for more information\n# about the syntax of this string.\n#\n# Note: this configuration applies only to <= TLSv1.2.\n#\n# tls-ciphers DEFAULT:!MEDIUM\n\n# Configure allowed TLSv1.3 ciphersuites.  See the ciphers(1ssl) manpage for more\n# information about the syntax of this string, and specifically for TLSv1.3\n# ciphersuites.\n#\n# tls-ciphersuites TLS_CHACHA20_POLY1305_SHA256\n\n# When choosing a cipher, use the server's preference instead of the client\n# preference. By default, the server follows the client's preference.\n#\n# tls-prefer-server-ciphers yes\n\n# By default, TLS session caching is enabled to allow faster and less expensive\n# reconnections by clients that support it. Use the following directive to disable\n# caching.\n#\n# tls-session-caching no\n\n# Change the default number of TLS sessions cached. A zero value sets the cache\n# to unlimited size. The default size is 20480.\n#\n# tls-session-cache-size 5000\n\n# Change the default timeout of cached TLS sessions. The default timeout is 300\n# seconds.\n#\n# tls-session-cache-timeout 60\n\n# Allow the server to monitor the filesystem and rotate out TLS certificates if \n# they change on disk, defaults to no.\n# \n# tls-rotation no\n\n# Setup a allowlist of allowed Common Names (CNs)/Subject Alternative Names (SANs)\n# that are allowed to connect to this server. This includes both normal clients as\n# well as other servers connected for replication/clustering purposes. If nothing is\n# specified, then no allowlist is used and all certificates are accepted. \n# Supports IPv4, DNS, RFC822, and URI SAN types.\n# You can put multiple names on one line as follows:\n#\n# tls-allowlist <dns1> <dns2> <dns3> ...\n# \n#\n# This configuration also allows for wildcard characters with glob style formatting\n# i.e. \"*.com\" would allow all clients to connect with a CN/SAN that ends with \".com\"\n\n################################# GENERAL #####################################\n\n# By default KeyDB does not run as a daemon. Use 'yes' if you need it.\n# Note that KeyDB will write a pid file in /var/run/keydb.pid when daemonized.\ndaemonize yes\n\n# If you run KeyDB from upstart or systemd, KeyDB can interact with your\n# supervision tree. Options:\n#   supervised no      - no supervision interaction\n#   supervised upstart - signal upstart by putting KeyDB into SIGSTOP mode\n#                        requires \"expect stop\" in your upstart job config\n#   supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET\n#   supervised auto    - detect upstart or systemd method based on\n#                        UPSTART_JOB or NOTIFY_SOCKET environment variables\n# Note: these supervision methods only signal \"process is ready.\"\n#       They do not enable continuous pings back to your supervisor.\nsupervised no\n\n# If a pid file is specified, KeyDB writes it where specified at startup\n# and removes it at exit.\n#\n# When the server runs non daemonized, no pid file is created if none is\n# specified in the configuration. When the server is daemonized, the pid file\n# is used even if not specified, defaulting to \"/var/run/keydb.pid\".\n#\n# Creating a pid file is best effort: if KeyDB is not able to create it\n# nothing bad happens, the server will start and run normally.\npidfile /var/run/keydb/keydb-server.pid\n\n# Specify the server verbosity level.\n# This can be one of:\n# debug (a lot of information, useful for development/testing)\n# verbose (many rarely useful info, but not a mess like the debug level)\n# notice (moderately verbose, what you want in production probably)\n# warning (only very important / critical messages are logged)\nloglevel notice\n\n# Specify the log file name. Also the empty string can be used to force\n# KeyDB to log on the standard output. Note that if you use standard\n# output for logging but daemonize, logs will be sent to /dev/null\nlogfile /var/log/keydb/keydb-server.log\n\n# To enable logging to the system logger, just set 'syslog-enabled' to yes,\n# and optionally update the other syslog parameters to suit your needs.\n# syslog-enabled no\n\n# Specify the syslog identity.\n# syslog-ident keydb\n\n# Specify the syslog facility. Must be USER or between LOCAL0-LOCAL7.\n# syslog-facility local0\n\n# To disable the built in crash log, which will possibly produce cleaner core\n# dumps when they are needed, uncomment the following:\n#\n# crash-log-enabled no\n\n# To disable the fast memory check that's run as part of the crash log, which\n# will possibly let keydb terminate sooner, uncomment the following:\n#\n# crash-memcheck-enabled no\n\n# Set the number of databases. The default database is DB 0, you can select\n# a different one on a per-connection basis using SELECT <dbid> where\n# dbid is a number between 0 and 'databases'-1\ndatabases 16\n\n# By default KeyDB shows an ASCII art logo only when started to log to the\n# standard output and if the standard output is a TTY. Basically this means\n# that normally a logo is displayed only in interactive sessions.\n#\n# However it is possible to force the pre-4.0 behavior and always show a\n# ASCII art logo in startup logs by setting the following option to yes.\nalways-show-logo yes\n\n# By default, KeyDB modifies the process title (as seen in 'top' and 'ps') to\n# provide some runtime information. It is possible to disable this and leave\n# the process name as executed by setting the following to no.\nset-proc-title yes\n\n# Retrieving \"message of today\" using CURL requests.\n#enable-motd yes\n\n# When changing the process title, KeyDB uses the following template to construct\n# the modified title.\n#\n# Template variables are specified in curly brackets. The following variables are\n# supported:\n#\n# {title}           Name of process as executed if parent, or type of child process.\n# {listen-addr}     Bind address or '*' followed by TCP or TLS port listening on, or\n#                   Unix socket if only that's available.\n# {server-mode}     Special mode, i.e. \"[sentinel]\" or \"[cluster]\".\n# {port}            TCP port listening on, or 0.\n# {tls-port}        TLS port listening on, or 0.\n# {unixsocket}      Unix domain socket listening on, or \"\".\n# {config-file}     Name of configuration file used.\n#\nproc-title-template \"{title} {listen-addr} {server-mode}\"\n\n################################ SNAPSHOTTING  ################################\n#\n# Save the DB on disk:\n#\n#   save <seconds> <changes>\n#\n#   Will save the DB if both the given number of seconds and the given\n#   number of write operations against the DB occurred.\n#\n#   In the example below the behavior will be to save:\n#   after 900 sec (15 min) if at least 1 key changed\n#   after 300 sec (5 min) if at least 10 keys changed\n#   after 60 sec if at least 10000 keys changed\n#\n#   It is also possible to remove all the previously configured save\n#   points by adding a save directive with a single empty string argument\n#   like in the following example:\n#\n#   save \"\"\n\nsave 900 1\nsave 300 10\nsave 60 10000\n\n# By default KeyDB will stop accepting writes if RDB snapshots are enabled\n# (at least one save point) and the latest background save failed.\n# This will make the user aware (in a hard way) that data is not persisting\n# on disk properly, otherwise chances are that no one will notice and some\n# disaster will happen.\n#\n# If the background saving process will start working again KeyDB will\n# automatically allow writes again.\n#\n# However if you have setup your proper monitoring of the KeyDB server\n# and persistence, you may want to disable this feature so that KeyDB will\n# continue to work as usual even if there are problems with disk,\n# permissions, and so forth.\nstop-writes-on-bgsave-error yes\n\n# Compress string objects using LZF when dump .rdb databases?\n# By default compression is enabled as it's almost always a win.\n# If you want to save some CPU in the saving child set it to 'no' but\n# the dataset will likely be bigger if you have compressible values or keys.\nrdbcompression yes\n\n# Since version 5 of RDB a CRC64 checksum is placed at the end of the file.\n# This makes the format more resistant to corruption but there is a performance\n# hit to pay (around 10%) when saving and loading RDB files, so you can disable it\n# for maximum performances.\n#\n# RDB files created with checksum disabled have a checksum of zero that will\n# tell the loading code to skip the check.\nrdbchecksum yes\n\n# Enables or disables full sanitation checks for ziplist and listpack etc when\n# loading an RDB or RESTORE payload. This reduces the chances of a assertion or\n# crash later on while processing commands.\n# Options:\n#   no         - Never perform full sanitation\n#   yes        - Always perform full sanitation\n#   clients    - Perform full sanitation only for user connections.\n#                Excludes: RDB files, RESTORE commands received from the master\n#                connection, and client connections which have the\n#                skip-sanitize-payload ACL flag.\n# The default should be 'clients' but since it currently affects cluster\n# resharding via MIGRATE, it is temporarily set to 'no' by default.\n#\n# sanitize-dump-payload no\n\n# The filename where to dump the DB\ndbfilename dump.rdb\n\n# Remove RDB files used by replication in instances without persistence\n# enabled. By default this option is disabled, however there are environments\n# where for regulations or other security concerns, RDB files persisted on\n# disk by masters in order to feed replicas, or stored on disk by replicas\n# in order to load them for the initial synchronization, should be deleted\n# ASAP. Note that this option ONLY WORKS in instances that have both AOF\n# and RDB persistence disabled, otherwise is completely ignored.\n#\n# An alternative (and sometimes better) way to obtain the same effect is\n# to use diskless replication on both master and replicas instances. However\n# in the case of replicas, diskless is not always an option.\nrdb-del-sync-files no\n\n# The working directory.\n#\n# The DB will be written inside this directory, with the filename specified\n# above using the 'dbfilename' configuration directive.\n#\n# The Append Only File will also be created inside this directory.\n#\n# Note that you must specify a directory here, not a file name.\ndir /var/lib/keydb\n\n################################# REPLICATION #################################\n\n# Master-Replica replication. Use replicaof to make a KeyDB instance a copy of\n# another KeyDB server. A few things to understand ASAP about KeyDB replication.\n#\n#   +------------------+      +---------------+\n#   |      Master      | ---> |    Replica    |\n#   | (receive writes) |      |  (exact copy) |\n#   +------------------+      +---------------+\n#\n# 1) KeyDB replication is asynchronous, but you can configure a master to\n#    stop accepting writes if it appears to be not connected with at least\n#    a given number of replicas.\n# 2) KeyDB replicas are able to perform a partial resynchronization with the\n#    master if the replication link is lost for a relatively small amount of\n#    time. You may want to configure the replication backlog size (see the next\n#    sections of this file) with a sensible value depending on your needs.\n# 3) Replication is automatic and does not need user intervention. After a\n#    network partition replicas automatically try to reconnect to masters\n#    and resynchronize with them.\n#\n# replicaof <masterip> <masterport>\n\n# If the master is password protected (using the \"requirepass\" configuration\n# directive below) it is possible to tell the replica to authenticate before\n# starting the replication synchronization process, otherwise the master will\n# refuse the replica request.\n#\n# masterauth <master-password>\n#\n# However this is not enough if you are using KeyDB ACLs (for KeyDB version\n# 6 or greater), and the default user is not capable of running the PSYNC\n# command and/or other commands needed for replication (gathered in the\n# @replication group). In this case it's better to configure a special user to\n# use with replication, and specify the masteruser configuration as such:\n#\n# masteruser <username>\n#\n# When masteruser is specified, the replica will authenticate against its\n# master using the new AUTH form: AUTH <username> <password>.\n\n# When a replica loses its connection with the master, or when the replication\n# is still in progress, the replica can act in two different ways:\n#\n# 1) if replica-serve-stale-data is set to 'yes' (the default) the replica will\n#    still reply to client requests, possibly with out of date data, or the\n#    data set may just be empty if this is the first synchronization.\n#\n# 2) If replica-serve-stale-data is set to 'no' the replica will reply with\n#    an error \"SYNC with master in progress\" to all commands except:\n#    INFO, REPLICAOF, AUTH, PING, SHUTDOWN, REPLCONF, ROLE, CONFIG, SUBSCRIBE,\n#    UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, PUBLISH, PUBSUB, COMMAND, POST,\n#    HOST and LATENCY.\n#\nreplica-serve-stale-data yes\n\n# Active Replicas will allow read only data access while loading remote RDBs\n# provided they are permitted to serve stale data.  As an option you may also\n# permit them to accept write commands.  This is an EXPERIMENTAL feature and\n# may result in commands not being fully synchronized\n#\n# allow-write-during-load no\n\n# You can modify the number of masters necessary to form a replica quorum when\n# multi-master is enabled and replica-serve-stale-data is \"no\".  By default \n# this is set to -1 which implies the number of known masters (e.g. those\n# you added with replicaof)\n#\n# replica-quorum -1\n\n# You can configure a replica instance to accept writes or not. Writing against\n# a replica instance may be useful to store some ephemeral data (because data\n# written on a replica will be easily deleted after resync with the master) but\n# may also cause problems if clients are writing to it because of a\n# misconfiguration.\n#\n# Since KeyDB 2.6 by default replicas are read-only.\n#\n# Note: read only replicas are not designed to be exposed to untrusted clients\n# on the internet. It's just a protection layer against misuse of the instance.\n# Still a read only replica exports by default all the administrative commands\n# such as CONFIG, DEBUG, and so forth. To a limited extent you can improve\n# security of read only replicas using 'rename-command' to shadow all the\n# administrative / dangerous commands.\nreplica-read-only yes\n\n# Replication SYNC strategy: disk or socket.\n#\n# New replicas and reconnecting replicas that are not able to continue the\n# replication process just receiving differences, need to do what is called a\n# \"full synchronization\". An RDB file is transmitted from the master to the\n# replicas.\n#\n# The transmission can happen in two different ways:\n#\n# 1) Disk-backed: The KeyDB master creates a new process that writes the RDB\n#                 file on disk. Later the file is transferred by the parent\n#                 process to the replicas incrementally.\n# 2) Diskless: The KeyDB master creates a new process that directly writes the\n#              RDB file to replica sockets, without touching the disk at all.\n#\n# With disk-backed replication, while the RDB file is generated, more replicas\n# can be queued and served with the RDB file as soon as the current child\n# producing the RDB file finishes its work. With diskless replication instead\n# once the transfer starts, new replicas arriving will be queued and a new\n# transfer will start when the current one terminates.\n#\n# When diskless replication is used, the master waits a configurable amount of\n# time (in seconds) before starting the transfer in the hope that multiple\n# replicas will arrive and the transfer can be parallelized.\n#\n# With slow disks and fast (large bandwidth) networks, diskless replication\n# works better.\nrepl-diskless-sync no\n\n# When diskless replication is enabled, it is possible to configure the delay\n# the server waits in order to spawn the child that transfers the RDB via socket\n# to the replicas.\n#\n# This is important since once the transfer starts, it is not possible to serve\n# new replicas arriving, that will be queued for the next RDB transfer, so the\n# server waits a delay in order to let more replicas arrive.\n#\n# The delay is specified in seconds, and by default is 5 seconds. To disable\n# it entirely just set it to 0 seconds and the transfer will start ASAP.\nrepl-diskless-sync-delay 5\n\n# -----------------------------------------------------------------------------\n# WARNING: RDB diskless load is experimental. Since in this setup the replica\n# does not immediately store an RDB on disk, it may cause data loss during\n# failovers. RDB diskless load + KeyDB modules not handling I/O reads may also\n# cause KeyDB to abort in case of I/O errors during the initial synchronization\n# stage with the master. Use only if your do what you are doing.\n# -----------------------------------------------------------------------------\n#\n# Replica can load the RDB it reads from the replication link directly from the\n# socket, or store the RDB to a file and read that file after it was completely\n# received from the master.\n#\n# In many cases the disk is slower than the network, and storing and loading\n# the RDB file may increase replication time (and even increase the master's\n# Copy on Write memory and salve buffers).\n# However, parsing the RDB file directly from the socket may mean that we have\n# to flush the contents of the current database before the full rdb was\n# received. For this reason we have the following options:\n#\n# \"disabled\"    - Don't use diskless load (store the rdb file to the disk first)\n# \"on-empty-db\" - Use diskless load only when it is completely safe.\n# \"swapdb\"      - Keep a copy of the current db contents in RAM while parsing\n#                 the data directly from the socket. note that this requires\n#                 sufficient memory, if you don't have it, you risk an OOM kill.\nrepl-diskless-load disabled\n\n# Replicas send PINGs to server in a predefined interval. It's possible to\n# change this interval with the repl_ping_replica_period option. The default\n# value is 10 seconds.\n#\n# repl-ping-replica-period 10\n\n# The following option sets the replication timeout for:\n#\n# 1) Bulk transfer I/O during SYNC, from the point of view of replica.\n# 2) Master timeout from the point of view of replicas (data, pings).\n# 3) Replica timeout from the point of view of masters (REPLCONF ACK pings).\n#\n# It is important to make sure that this value is greater than the value\n# specified for repl-ping-replica-period otherwise a timeout will be detected\n# every time there is low traffic between the master and the replica. The default\n# value is 60 seconds.\n#\n# repl-timeout 60\n\n# Disable TCP_NODELAY on the replica socket after SYNC?\n#\n# If you select \"yes\" KeyDB will use a smaller number of TCP packets and\n# less bandwidth to send data to replicas. But this can add a delay for\n# the data to appear on the replica side, up to 40 milliseconds with\n# Linux kernels using a default configuration.\n#\n# If you select \"no\" the delay for data to appear on the replica side will\n# be reduced but more bandwidth will be used for replication.\n#\n# By default we optimize for low latency, but in very high traffic conditions\n# or when the master and replicas are many hops away, turning this to \"yes\" may\n# be a good idea.\nrepl-disable-tcp-nodelay no\n\n# Set the replication backlog size. The backlog is a buffer that accumulates\n# replica data when replicas are disconnected for some time, so that when a\n# replica wants to reconnect again, often a full resync is not needed, but a\n# partial resync is enough, just passing the portion of data the replica\n# missed while disconnected.\n#\n# The bigger the replication backlog, the longer the replica can endure the\n# disconnect and later be able to perform a partial resynchronization.\n#\n# The backlog is only allocated if there is at least one replica connected.\n#\n# repl-backlog-size 1mb\n\n# After a master has no connected replicas for some time, the backlog will be\n# freed. The following option configures the amount of seconds that need to\n# elapse, starting from the time the last replica disconnected, for the backlog\n# buffer to be freed.\n#\n# Note that replicas never free the backlog for timeout, since they may be\n# promoted to masters later, and should be able to correctly \"partially\n# resynchronize\" with other replicas: hence they should always accumulate backlog.\n#\n# A value of 0 means to never release the backlog.\n#\n# repl-backlog-ttl 3600\n\n# The replica priority is an integer number published by KeyDB in the INFO\n# output. It is used by KeyDB Sentinel in order to select a replica to promote\n# into a master if the master is no longer working correctly.\n#\n# A replica with a low priority number is considered better for promotion, so\n# for instance if there are three replicas with priority 10, 100, 25 Sentinel\n# will pick the one with priority 10, that is the lowest.\n#\n# However a special priority of 0 marks the replica as not able to perform the\n# role of master, so a replica with priority of 0 will never be selected by\n# KeyDB Sentinel for promotion.\n#\n# By default the priority is 100.\nreplica-priority 100\n\n# -----------------------------------------------------------------------------\n# By default, KeyDB Sentinel includes all replicas in its reports. A replica\n# can be excluded from KeyDB Sentinel's announcements. An unannounced replica\n# will be ignored by the 'sentinel replicas <master>' command and won't be\n# exposed to KeyDB Sentinel's clients.\n#\n# This option does not change the behavior of replica-priority. Even with\n# replica-announced set to 'no', the replica can be promoted to master. To\n# prevent this behavior, set replica-priority to 0.\n#\n# replica-announced yes\n\n# It is possible for a master to stop accepting writes if there are less than\n# N replicas connected, having a lag less or equal than M seconds.\n#\n# The N replicas need to be in \"online\" state.\n#\n# The lag in seconds, that must be <= the specified value, is calculated from\n# the last ping received from the replica, that is usually sent every second.\n#\n# This option does not GUARANTEE that N replicas will accept the write, but\n# will limit the window of exposure for lost writes in case not enough replicas\n# are available, to the specified number of seconds.\n#\n# For example to require at least 3 replicas with a lag <= 10 seconds use:\n#\n# min-replicas-to-write 3\n# min-replicas-max-lag 10\n#\n# Setting one or the other to 0 disables the feature.\n#\n# By default min-replicas-to-write is set to 0 (feature disabled) and\n# min-replicas-max-lag is set to 10.\n\n# A KeyDB master is able to list the address and port of the attached\n# replicas in different ways. For example the \"INFO replication\" section\n# offers this information, which is used, among other tools, by\n# KeyDB Sentinel in order to discover replica instances.\n# Another place where this info is available is in the output of the\n# \"ROLE\" command of a master.\n#\n# The listed IP address and port normally reported by a replica is\n# obtained in the following way:\n#\n#   IP: The address is auto detected by checking the peer address\n#   of the socket used by the replica to connect with the master.\n#\n#   Port: The port is communicated by the replica during the replication\n#   handshake, and is normally the port that the replica is using to\n#   listen for connections.\n#\n# However when port forwarding or Network Address Translation (NAT) is\n# used, the replica may actually be reachable via different IP and port\n# pairs. The following two options can be used by a replica in order to\n# report to its master a specific set of IP and port, so that both INFO\n# and ROLE will report those values.\n#\n# There is no need to use both the options if you need to override just\n# the port or the IP address.\n#\n# replica-announce-ip 5.5.5.5\n# replica-announce-port 1234\n\n############################### KEYS TRACKING #################################\n\n# KeyDB implements server assisted support for client side caching of values.\n# This is implemented using an invalidation table that remembers, using\n# 16 millions of slots, what clients may have certain subsets of keys. In turn\n# this is used in order to send invalidation messages to clients. Please\n# check this page to understand more about the feature:\n#\n#   https://redis.io/topics/client-side-caching\n#\n# When tracking is enabled for a client, all the read only queries are assumed\n# to be cached: this will force KeyDB to store information in the invalidation\n# table. When keys are modified, such information is flushed away, and\n# invalidation messages are sent to the clients. However if the workload is\n# heavily dominated by reads, KeyDB could use more and more memory in order\n# to track the keys fetched by many clients.\n#\n# For this reason it is possible to configure a maximum fill value for the\n# invalidation table. By default it is set to 1M of keys, and once this limit\n# is reached, KeyDB will start to evict keys in the invalidation table\n# even if they were not modified, just to reclaim memory: this will in turn\n# force the clients to invalidate the cached values. Basically the table\n# maximum size is a trade off between the memory you want to spend server\n# side to track information about who cached what, and the ability of clients\n# to retain cached objects in memory.\n#\n# If you set the value to 0, it means there are no limits, and KeyDB will\n# retain as many keys as needed in the invalidation table.\n# In the \"stats\" INFO section, you can find information about the number of\n# keys in the invalidation table at every given moment.\n#\n# Note: when key tracking is used in broadcasting mode, no memory is used\n# in the server side so this setting is useless.\n#\n# tracking-table-max-keys 1000000\n\n################################## SECURITY ###################################\n\n# Warning: since KeyDB is pretty fast, an outside user can try up to\n# 1 million passwords per second against a modern box. This means that you\n# should use very strong passwords, otherwise they will be very easy to break.\n# Note that because the password is really a shared secret between the client\n# and the server, and should not be memorized by any human, the password\n# can be easily a long string from /dev/urandom or whatever, so by using a\n# long and unguessable password no brute force attack will be possible.\n\n# KeyDB ACL users are defined in the following format:\n#\n#   user <username> ... acl rules ...\n#\n# For example:\n#\n#   user worker +@list +@connection ~jobs:* on >ffa9203c493aa99\n#\n# The special username \"default\" is used for new connections. If this user\n# has the \"nopass\" rule, then new connections will be immediately authenticated\n# as the \"default\" user without the need of any password provided via the\n# AUTH command. Otherwise if the \"default\" user is not flagged with \"nopass\"\n# the connections will start in not authenticated state, and will require\n# AUTH (or the HELLO command AUTH option) in order to be authenticated and\n# start to work.\n#\n# The ACL rules that describe what a user can do are the following:\n#\n#  on           Enable the user: it is possible to authenticate as this user.\n#  off          Disable the user: it's no longer possible to authenticate\n#               with this user, however the already authenticated connections\n#               will still work.\n#  skip-sanitize-payload    RESTORE dump-payload sanitation is skipped.\n#  sanitize-payload         RESTORE dump-payload is sanitized (default).\n#  +<command>   Allow the execution of that command\n#  -<command>   Disallow the execution of that command\n#  +@<category> Allow the execution of all the commands in such category\n#               with valid categories are like @admin, @set, @sortedset, ...\n#               and so forth, see the full list in the server.cpp file where\n#               the KeyDB command table is described and defined.\n#               The special category @all means all the commands, but currently\n#               present in the server, and that will be loaded in the future\n#               via modules.\n#  +<command>|subcommand    Allow a specific subcommand of an otherwise\n#                           disabled command. Note that this form is not\n#                           allowed as negative like -DEBUG|SEGFAULT, but\n#                           only additive starting with \"+\".\n#  allcommands  Alias for +@all. Note that it implies the ability to execute\n#               all the future commands loaded via the modules system.\n#  nocommands   Alias for -@all.\n#  ~<pattern>   Add a pattern of keys that can be mentioned as part of\n#               commands. For instance ~* allows all the keys. The pattern\n#               is a glob-style pattern like the one of KEYS.\n#               It is possible to specify multiple patterns.\n#  allkeys      Alias for ~*\n#  resetkeys    Flush the list of allowed keys patterns.\n#  &<pattern>   Add a glob-style pattern of Pub/Sub channels that can be\n#               accessed by the user. It is possible to specify multiple channel\n#               patterns.\n#  allchannels  Alias for &*\n#  resetchannels            Flush the list of allowed channel patterns.\n#  ><password>  Add this password to the list of valid password for the user.\n#               For example >mypass will add \"mypass\" to the list.\n#               This directive clears the \"nopass\" flag (see later).\n#  <<password>  Remove this password from the list of valid passwords.\n#  nopass       All the set passwords of the user are removed, and the user\n#               is flagged as requiring no password: it means that every\n#               password will work against this user. If this directive is\n#               used for the default user, every new connection will be\n#               immediately authenticated with the default user without\n#               any explicit AUTH command required. Note that the \"resetpass\"\n#               directive will clear this condition.\n#  resetpass    Flush the list of allowed passwords. Moreover removes the\n#               \"nopass\" status. After \"resetpass\" the user has no associated\n#               passwords and there is no way to authenticate without adding\n#               some password (or setting it as \"nopass\" later).\n#  reset        Performs the following actions: resetpass, resetkeys, off,\n#               -@all. The user returns to the same state it has immediately\n#               after its creation.\n#\n# ACL rules can be specified in any order: for instance you can start with\n# passwords, then flags, or key patterns. However note that the additive\n# and subtractive rules will CHANGE MEANING depending on the ordering.\n# For instance see the following example:\n#\n#   user alice on +@all -DEBUG ~* >somepassword\n#\n# This will allow \"alice\" to use all the commands with the exception of the\n# DEBUG command, since +@all added all the commands to the set of the commands\n# alice can use, and later DEBUG was removed. However if we invert the order\n# of two ACL rules the result will be different:\n#\n#   user alice on -DEBUG +@all ~* >somepassword\n#\n# Now DEBUG was removed when alice had yet no commands in the set of allowed\n# commands, later all the commands are added, so the user will be able to\n# execute everything.\n#\n# Basically ACL rules are processed left-to-right.\n#\n# The following is a list of command categories and their meanings:\n# * keyspace - Writing or reading from keys, databases, or their metadata \n#     in a type agnostic way. Includes DEL, RESTORE, DUMP, RENAME, EXISTS, DBSIZE,\n#     KEYS, EXPIRE, TTL, FLUSHALL, etc. Commands that may modify the keyspace,\n#     key or metadata will also have `write` category. Commands that only read\n#     the keyspace, key or metadata will have the `read` category.\n# * read - Reading from keys (values or metadata). Note that commands that don't\n#     interact with keys, will not have either `read` or `write`.\n# * write - Writing to keys (values or metadata)\n# * admin - Administrative commands. Normal applications will never need to use\n#     these. Includes REPLICAOF, CONFIG, DEBUG, SAVE, MONITOR, ACL, SHUTDOWN, etc.\n# * dangerous - Potentially dangerous (each should be considered with care for\n#     various reasons). This includes FLUSHALL, MIGRATE, RESTORE, SORT, KEYS,\n#     CLIENT, DEBUG, INFO, CONFIG, SAVE, REPLICAOF, etc.\n# * connection - Commands affecting the connection or other connections.\n#     This includes AUTH, SELECT, COMMAND, CLIENT, ECHO, PING, etc.\n# * blocking - Potentially blocking the connection until released by another\n#     command.\n# * fast - Fast O(1) commands. May loop on the number of arguments, but not the\n#     number of elements in the key.\n# * slow - All commands that are not Fast.\n# * pubsub - PUBLISH / SUBSCRIBE related\n# * transaction - WATCH / MULTI / EXEC related commands.\n# * scripting - Scripting related.\n# * set - Data type: sets related.\n# * sortedset - Data type: zsets related.\n# * list - Data type: lists related.\n# * hash - Data type: hashes related.\n# * string - Data type: strings related.\n# * bitmap - Data type: bitmaps related.\n# * hyperloglog - Data type: hyperloglog related.\n# * geo - Data type: geo related.\n# * stream - Data type: streams related.\n#\n# For more information about ACL configuration please refer to\n# the Redis web site at https://redis.io/topics/acl\n\n# ACL LOG\n#\n# The ACL Log tracks failed commands and authentication events associated\n# with ACLs. The ACL Log is useful to troubleshoot failed commands blocked \n# by ACLs. The ACL Log is stored in memory. You can reclaim memory with \n# ACL LOG RESET. Define the maximum entry length of the ACL Log below.\nacllog-max-len 128\n\n# Using an external ACL file\n#\n# Instead of configuring users here in this file, it is possible to use\n# a stand-alone file just listing users. The two methods cannot be mixed:\n# if you configure users here and at the same time you activate the external\n# ACL file, the server will refuse to start.\n#\n# The format of the external ACL user file is exactly the same as the\n# format that is used inside keydb.conf to describe users.\n#\n# aclfile /etc/keydb/users.acl\n\n# IMPORTANT NOTE: starting with KeyDB 6 \"requirepass\" is just a compatibility\n# layer on top of the new ACL system. The option effect will be just setting\n# the password for the default user. Clients will still authenticate using\n# AUTH <password> as usually, or more explicitly with AUTH default <password>\n# if they follow the new protocol: both will work.\n#\n# The requirepass is not compatible with aclfile option and the ACL LOAD\n# command, these will cause requirepass to be ignored.\n#\n# requirepass foobared\n\n# New users are initialized with restrictive permissions by default, via the\n# equivalent of this ACL rule 'off resetkeys -@all'. Starting with KeyDB 6.2, it\n# is possible to manage access to Pub/Sub channels with ACL rules as well. The\n# default Pub/Sub channels permission if new users is controlled by the\n# acl-pubsub-default configuration directive, which accepts one of these values:\n#\n# allchannels: grants access to all Pub/Sub channels\n# resetchannels: revokes access to all Pub/Sub channels\n#\n# To ensure backward compatibility while upgrading KeyDB 6.0, acl-pubsub-default\n# defaults to the 'allchannels' permission.\n#\n# Future compatibility note: it is very likely that in a future version of KeyDB\n# the directive's default of 'allchannels' will be changed to 'resetchannels' in\n# order to provide better out-of-the-box Pub/Sub security. Therefore, it is\n# recommended that you explicitly define Pub/Sub permissions for all users\n# rather then rely on implicit default values. Once you've set explicit\n# Pub/Sub for all existing users, you should uncomment the following line.\n#\n# acl-pubsub-default resetchannels\n\n# Command renaming (DEPRECATED).\n#\n# ------------------------------------------------------------------------\n# WARNING: avoid using this option if possible. Instead use ACLs to remove\n# commands from the default user, and put them only in some admin user you\n# create for administrative purposes.\n# ------------------------------------------------------------------------\n#\n# It is possible to change the name of dangerous commands in a shared\n# environment. For instance the CONFIG command may be renamed into something\n# hard to guess so that it will still be available for internal-use tools\n# but not available for general clients.\n#\n# Example:\n#\n# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52\n#\n# It is also possible to completely kill a command by renaming it into\n# an empty string:\n#\n# rename-command CONFIG \"\"\n#\n# Please note that changing the name of commands that are logged into the\n# AOF file or transmitted to replicas may cause problems.\n\n################################### CLIENTS ####################################\n\n# Set the max number of connected clients at the same time. By default\n# this limit is set to 10000 clients, however if the KeyDB server is not\n# able to configure the process file limit to allow for the specified limit\n# the max number of allowed clients is set to the current file limit\n# minus 32 (as KeyDB reserves a few file descriptors for internal uses).\n#\n# Once the limit is reached KeyDB will close all the new connections sending\n# an error 'max number of clients reached'.\n#\n# IMPORTANT: When KeyDB Cluster is used, the max number of connections is also\n# shared with the cluster bus: every node in the cluster will use two\n# connections, one incoming and another outgoing. It is important to size the\n# limit accordingly in case of very large clusters.\n#\n# maxclients 10000\n\n############################## MEMORY MANAGEMENT ################################\n\n# Set a memory usage limit to the specified amount of bytes.\n# When the memory limit is reached KeyDB will try to remove keys\n# according to the eviction policy selected (see maxmemory-policy).\n#\n# If KeyDB can't remove keys according to the policy, or if the policy is\n# set to 'noeviction', KeyDB will start to reply with errors to commands\n# that would use more memory, like SET, LPUSH, and so on, and will continue\n# to reply to read-only commands like GET.\n#\n# This option is usually useful when using KeyDB as an LRU or LFU cache, or to\n# set a hard memory limit for an instance (using the 'noeviction' policy).\n#\n# WARNING: If you have replicas attached to an instance with maxmemory on,\n# the size of the output buffers needed to feed the replicas are subtracted\n# from the used memory count, so that network problems / resyncs will\n# not trigger a loop where keys are evicted, and in turn the output\n# buffer of replicas is full with DELs of keys evicted triggering the deletion\n# of more keys, and so forth until the database is completely emptied.\n#\n# In short... if you have replicas attached it is suggested that you set a lower\n# limit for maxmemory so that there is some free RAM on the system for replica\n# output buffers (but this is not needed if the policy is 'noeviction').\n#\n# maxmemory <bytes>\n\n# MAXMEMORY POLICY: how KeyDB will select what to remove when maxmemory\n# is reached. You can select one from the following behaviors:\n#\n# volatile-lru -> Evict using approximated LRU, only keys with an expire set.\n# allkeys-lru -> Evict any key using approximated LRU.\n# volatile-lfu -> Evict using approximated LFU, only keys with an expire set.\n# allkeys-lfu -> Evict any key using approximated LFU.\n# volatile-random -> Remove a random key having an expire set.\n# allkeys-random -> Remove a random key, any key.\n# volatile-ttl -> Remove the key with the nearest expire time (minor TTL)\n# noeviction -> Don't evict anything, just return an error on write operations.\n#\n# LRU means Least Recently Used\n# LFU means Least Frequently Used\n#\n# Both LRU, LFU and volatile-ttl are implemented using approximated\n# randomized algorithms.\n#\n# Note: with any of the above policies, KeyDB will return an error on write\n#       operations, when there are no suitable keys for eviction.\n#\n#       At the date of writing these commands are: set setnx setex append\n#       incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd\n#       sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby\n#       zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby\n#       getset mset msetnx exec sort\n#\n# The default is:\n#\n# maxmemory-policy noeviction\n\n# LRU, LFU and minimal TTL algorithms are not precise algorithms but approximated\n# algorithms (in order to save memory), so you can tune it for speed or\n# accuracy. By default KeyDB will check five keys and pick the one that was\n# used least recently, you can change the sample size using the following\n# configuration directive.\n#\n# The default of 5 produces good enough results. 10 Approximates very closely\n# true LRU but costs more CPU. 3 is faster but not very accurate.\n#\n# maxmemory-samples 5\n\n# Eviction processing is designed to function well with the default setting.\n# If there is an unusually large amount of write traffic, this value may need to\n# be increased.  Decreasing this value may reduce latency at the risk of\n# eviction processing effectiveness\n#   0 = minimum latency, 10 = default, 100 = process without regard to latency\n#\n# maxmemory-eviction-tenacity 10\n\n# Starting from KeyDB 5, by default a replica will ignore its maxmemory setting\n# (unless it is promoted to master after a failover or manually). It means\n# that the eviction of keys will be just handled by the master, sending the\n# DEL commands to the replica as keys evict in the master side.\n#\n# This behavior ensures that masters and replicas stay consistent, and is usually\n# what you want, however if your replica is writable, or you want the replica\n# to have a different memory setting, and you are sure all the writes performed\n# to the replica are idempotent, then you may change this default (but be sure\n# to understand what you are doing).\n#\n# Note that since the replica by default does not evict, it may end using more\n# memory than the one set via maxmemory (there are certain buffers that may\n# be larger on the replica, or data structures may sometimes take more memory\n# and so forth). So make sure you monitor your replicas and make sure they\n# have enough memory to never hit a real out-of-memory condition before the\n# master hits the configured maxmemory setting.\n#\n# replica-ignore-maxmemory yes\n\n# KeyDB reclaims expired keys in two ways: upon access when those keys are\n# found to be expired, and also in background, in what is called the\n# \"active expire key\". The key space is slowly and interactively scanned\n# looking for expired keys to reclaim, so that it is possible to free memory\n# of keys that are expired and will never be accessed again in a short time.\n#\n# The default effort of the expire cycle will try to avoid having more than\n# ten percent of expired keys still in memory, and will try to avoid consuming\n# more than 25% of total memory and to add latency to the system. However\n# it is possible to increase the expire \"effort\" that is normally set to\n# \"1\", to a greater value, up to the value \"10\". At its maximum value the\n# system will use more CPU, longer cycles (and technically may introduce\n# more latency), and will tolerate less already expired keys still present\n# in the system. It's a tradeoff between memory, CPU and latency.\n#\n# active-expire-effort 1\n\n############################# LAZY FREEING ####################################\n\n# KeyDB has two primitives to delete keys. One is called DEL and is a blocking\n# deletion of the object. It means that the server stops processing new commands\n# in order to reclaim all the memory associated with an object in a synchronous\n# way. If the key deleted is associated with a small object, the time needed\n# in order to execute the DEL command is very small and comparable to most other\n# O(1) or O(log_N) commands in KeyDB. However if the key is associated with an\n# aggregated value containing millions of elements, the server can block for\n# a long time (even seconds) in order to complete the operation.\n#\n# For the above reasons KeyDB also offers non blocking deletion primitives\n# such as UNLINK (non blocking DEL) and the ASYNC option of FLUSHALL and\n# FLUSHDB commands, in order to reclaim memory in background. Those commands\n# are executed in constant time. Another thread will incrementally free the\n# object in the background as fast as possible.\n#\n# DEL, UNLINK and ASYNC option of FLUSHALL and FLUSHDB are user-controlled.\n# It's up to the design of the application to understand when it is a good\n# idea to use one or the other. However the KeyDB server sometimes has to\n# delete keys or flush the whole database as a side effect of other operations.\n# Specifically KeyDB deletes objects independently of a user call in the\n# following scenarios:\n#\n# 1) On eviction, because of the maxmemory and maxmemory policy configurations,\n#    in order to make room for new data, without going over the specified\n#    memory limit.\n# 2) Because of expire: when a key with an associated time to live (see the\n#    EXPIRE command) must be deleted from memory.\n# 3) Because of a side effect of a command that stores data on a key that may\n#    already exist. For example the RENAME command may delete the old key\n#    content when it is replaced with another one. Similarly SUNIONSTORE\n#    or SORT with STORE option may delete existing keys. The SET command\n#    itself removes any old content of the specified key in order to replace\n#    it with the specified string.\n# 4) During replication, when a replica performs a full resynchronization with\n#    its master, the content of the whole database is removed in order to\n#    load the RDB file just transferred.\n#\n# In all the above cases the default is to delete objects in a blocking way,\n# like if DEL was called. However you can configure each case specifically\n# in order to instead release memory in a non-blocking way like if UNLINK\n# was called, using the following configuration directives.\n\nlazyfree-lazy-eviction no\nlazyfree-lazy-expire no\nlazyfree-lazy-server-del no\nreplica-lazy-flush no\n\n# It is also possible, for the case when to replace the user code DEL calls\n# with UNLINK calls is not easy, to modify the default behavior of the DEL\n# command to act exactly like UNLINK, using the following configuration\n# directive:\n\nlazyfree-lazy-user-del no\n\n# FLUSHDB, FLUSHALL, and SCRIPT FLUSH support both asynchronous and synchronous\n# deletion, which can be controlled by passing the [SYNC|ASYNC] flags into the\n# commands. When neither flag is passed, this directive will be used to determine\n# if the data should be deleted asynchronously.\n\nlazyfree-lazy-user-flush no\n\n############################ KERNEL OOM CONTROL ##############################\n\n# On Linux, it is possible to hint the kernel OOM killer on what processes\n# should be killed first when out of memory.\n#\n# Enabling this feature makes KeyDB actively control the oom_score_adj value\n# for all its processes, depending on their role. The default scores will\n# attempt to have background child processes killed before all others, and\n# replicas killed before masters.\n#\n# KeyDB supports three options:\n#\n# no:       Don't make changes to oom-score-adj (default).\n# yes:      Alias to \"relative\" see below.\n# absolute: Values in oom-score-adj-values are written as is to the kernel.\n# relative: Values are used relative to the initial value of oom_score_adj when\n#           the server starts and are then clamped to a range of -1000 to 1000.\n#           Because typically the initial value is 0, they will often match the\n#           absolute values.\noom-score-adj no\n\n# When oom-score-adj is used, this directive controls the specific values used\n# for master, replica and background child processes. Values range -2000 to\n# 2000 (higher means more likely to be killed).\n#\n# Unprivileged processes (not root, and without CAP_SYS_RESOURCE capabilities)\n# can freely increase their value, but not decrease it below its initial\n# settings. This means that setting oom-score-adj to \"relative\" and setting the\n# oom-score-adj-values to positive values will always succeed.\noom-score-adj-values 0 200 800\n\n\n#################### KERNEL transparent hugepage CONTROL ######################\n\n# Usually the kernel Transparent Huge Pages control is set to \"madvise\" or\n# or \"never\" by default (/sys/kernel/mm/transparent_hugepage/enabled), in which\n# case this config has no effect. On systems in which it is set to \"always\",\n# KeyDB will attempt to disable it specifically for the KeyDB process in order\n# to avoid latency problems specifically with fork(2) and CoW.\n# If for some reason you prefer to keep it enabled, you can set this config to\n# \"no\" and the kernel global to \"always\".\n\ndisable-thp yes\n\n############################## APPEND ONLY MODE ###############################\n\n# By default KeyDB asynchronously dumps the dataset on disk. This mode is\n# good enough in many applications, but an issue with the KeyDB process or\n# a power outage may result into a few minutes of writes lost (depending on\n# the configured save points).\n#\n# The Append Only File is an alternative persistence mode that provides\n# much better durability. For instance using the default data fsync policy\n# (see later in the config file) KeyDB can lose just one second of writes in a\n# dramatic event like a server power outage, or a single write if something\n# wrong with the KeyDB process itself happens, but the operating system is\n# still running correctly.\n#\n# AOF and RDB persistence can be enabled at the same time without problems.\n# If the AOF is enabled on startup KeyDB will load the AOF, that is the file\n# with the better durability guarantees.\n#\n# Please check http://redis.io/topics/persistence for more information.\n\nappendonly no\n\n# The name of the append only file (default: \"appendonly.aof\")\n\nappendfilename \"appendonly.aof\"\n\n# The fsync() call tells the Operating System to actually write data on disk\n# instead of waiting for more data in the output buffer. Some OS will really flush\n# data on disk, some other OS will just try to do it ASAP.\n#\n# KeyDB supports three different modes:\n#\n# no: don't fsync, just let the OS flush the data when it wants. Faster.\n# always: fsync after every write to the append only log. Slow, Safest.\n# everysec: fsync only one time every second. Compromise.\n#\n# The default is \"everysec\", as that's usually the right compromise between\n# speed and data safety. It's up to you to understand if you can relax this to\n# \"no\" that will let the operating system flush the output buffer when\n# it wants, for better performances (but if you can live with the idea of\n# some data loss consider the default persistence mode that's snapshotting),\n# or on the contrary, use \"always\" that's very slow but a bit safer than\n# everysec.\n#\n# More details please check the following article:\n# http://antirez.com/post/redis-persistence-demystified.html\n#\n# If unsure, use \"everysec\".\n\n# appendfsync always\nappendfsync everysec\n# appendfsync no\n\n# When the AOF fsync policy is set to always or everysec, and a background\n# saving process (a background save or AOF log background rewriting) is\n# performing a lot of I/O against the disk, in some Linux configurations\n# KeyDB may block too long on the fsync() call. Note that there is no fix for\n# this currently, as even performing fsync in a different thread will block\n# our synchronous write(2) call.\n#\n# In order to mitigate this problem it's possible to use the following option\n# that will prevent fsync() from being called in the main process while a\n# BGSAVE or BGREWRITEAOF is in progress.\n#\n# This means that while another child is saving, the durability of KeyDB is\n# the same as \"appendfsync none\". In practical terms, this means that it is\n# possible to lose up to 30 seconds of log in the worst scenario (with the\n# default Linux settings).\n#\n# If you have latency problems turn this to \"yes\". Otherwise leave it as\n# \"no\" that is the safest pick from the point of view of durability.\n\nno-appendfsync-on-rewrite no\n\n# Automatic rewrite of the append only file.\n# KeyDB is able to automatically rewrite the log file implicitly calling\n# BGREWRITEAOF when the AOF log size grows by the specified percentage.\n#\n# This is how it works: KeyDB remembers the size of the AOF file after the\n# latest rewrite (if no rewrite has happened since the restart, the size of\n# the AOF at startup is used).\n#\n# This base size is compared to the current size. If the current size is\n# bigger than the specified percentage, the rewrite is triggered. Also\n# you need to specify a minimal size for the AOF file to be rewritten, this\n# is useful to avoid rewriting the AOF file even if the percentage increase\n# is reached but it is still pretty small.\n#\n# Specify a percentage of zero in order to disable the automatic AOF\n# rewrite feature.\n\nauto-aof-rewrite-percentage 100\nauto-aof-rewrite-min-size 64mb\n\n# An AOF file may be found to be truncated at the end during the KeyDB\n# startup process, when the AOF data gets loaded back into memory.\n# This may happen when the system where KeyDB is running\n# crashes, especially when an ext4 filesystem is mounted without the\n# data=ordered option (however this can't happen when KeyDB itself\n# crashes or aborts but the operating system still works correctly).\n#\n# KeyDB can either exit with an error when this happens, or load as much\n# data as possible (the default now) and start if the AOF file is found\n# to be truncated at the end. The following option controls this behavior.\n#\n# If aof-load-truncated is set to yes, a truncated AOF file is loaded and\n# the KeyDB server starts emitting a log to inform the user of the event.\n# Otherwise if the option is set to no, the server aborts with an error\n# and refuses to start. When the option is set to no, the user requires\n# to fix the AOF file using the \"keydb-check-aof\" utility before to restart\n# the server.\n#\n# Note that if the AOF file will be found to be corrupted in the middle\n# the server will still exit with an error. This option only applies when\n# KeyDB will try to read more data from the AOF file but not enough bytes\n# will be found.\naof-load-truncated yes\n\n# When rewriting the AOF file, KeyDB is able to use an RDB preamble in the\n# AOF file for faster rewrites and recoveries. When this option is turned\n# on the rewritten AOF file is composed of two different stanzas:\n#\n#   [RDB file][AOF tail]\n#\n# When loading, KeyDB recognizes that the AOF file starts with the \"REDIS\"\n# string and loads the prefixed RDB file, then continues loading the AOF\n# tail.\naof-use-rdb-preamble yes\n\n################################ LUA SCRIPTING  ###############################\n\n# Max execution time of a Lua script in milliseconds.\n#\n# If the maximum execution time is reached KeyDB will log that a script is\n# still in execution after the maximum allowed time and will start to\n# reply to queries with an error.\n#\n# When a long running script exceeds the maximum execution time only the\n# SCRIPT KILL and SHUTDOWN NOSAVE commands are available. The first can be\n# used to stop a script that did not yet call any write commands. The second\n# is the only way to shut down the server in the case a write command was\n# already issued by the script but the user doesn't want to wait for the natural\n# termination of the script.\n#\n# Set it to 0 or a negative value for unlimited execution without warnings.\nlua-time-limit 5000\n\n################################ KEYDB CLUSTER  ###############################\n\n# Normal KeyDB instances can't be part of a KeyDB Cluster; only nodes that are\n# started as cluster nodes can. In order to start a KeyDB instance as a\n# cluster node enable the cluster support uncommenting the following:\n#\n# cluster-enabled yes\n\n# Every cluster node has a cluster configuration file. This file is not\n# intended to be edited by hand. It is created and updated by KeyDB nodes.\n# Every KeyDB Cluster node requires a different cluster configuration file.\n# Make sure that instances running in the same system do not have\n# overlapping cluster configuration file names.\n#\n# cluster-config-file nodes-6379.conf\n\n# Cluster node timeout is the amount of milliseconds a node must be unreachable\n# for it to be considered in failure state.\n# Most other internal time limits are a multiple of the node timeout.\n#\n# cluster-node-timeout 15000\n\n# A replica of a failing master will avoid to start a failover if its data\n# looks too old.\n#\n# There is no simple way for a replica to actually have an exact measure of\n# its \"data age\", so the following two checks are performed:\n#\n# 1) If there are multiple replicas able to failover, they exchange messages\n#    in order to try to give an advantage to the replica with the best\n#    replication offset (more data from the master processed).\n#    Replicas will try to get their rank by offset, and apply to the start\n#    of the failover a delay proportional to their rank.\n#\n# 2) Every single replica computes the time of the last interaction with\n#    its master. This can be the last ping or command received (if the master\n#    is still in the \"connected\" state), or the time that elapsed since the\n#    disconnection with the master (if the replication link is currently down).\n#    If the last interaction is too old, the replica will not try to failover\n#    at all.\n#\n# The point \"2\" can be tuned by user. Specifically a replica will not perform\n# the failover if, since the last interaction with the master, the time\n# elapsed is greater than:\n#\n#   (node-timeout * cluster-replica-validity-factor) + repl-ping-replica-period\n#\n# So for example if node-timeout is 30 seconds, and the cluster-replica-validity-factor\n# is 10, and assuming a default repl-ping-replica-period of 10 seconds, the\n# replica will not try to failover if it was not able to talk with the master\n# for longer than 310 seconds.\n#\n# A large cluster-replica-validity-factor may allow replicas with too old data to failover\n# a master, while a too small value may prevent the cluster from being able to\n# elect a replica at all.\n#\n# For maximum availability, it is possible to set the cluster-replica-validity-factor\n# to a value of 0, which means, that replicas will always try to failover the\n# master regardless of the last time they interacted with the master.\n# (However they'll always try to apply a delay proportional to their\n# offset rank).\n#\n# Zero is the only value able to guarantee that when all the partitions heal\n# the cluster will always be able to continue.\n#\n# cluster-replica-validity-factor 10\n\n# Cluster replicas are able to migrate to orphaned masters, that are masters\n# that are left without working replicas. This improves the cluster ability\n# to resist to failures as otherwise an orphaned master can't be failed over\n# in case of failure if it has no working replicas.\n#\n# Replicas migrate to orphaned masters only if there are still at least a\n# given number of other working replicas for their old master. This number\n# is the \"migration barrier\". A migration barrier of 1 means that a replica\n# will migrate only if there is at least 1 other working replica for its master\n# and so forth. It usually reflects the number of replicas you want for every\n# master in your cluster.\n#\n# Default is 1 (replicas migrate only if their masters remain with at least\n# one replica). To disable migration just set it to a very large value or\n# set cluster-allow-replica-migration to 'no'.\n# A value of 0 can be set but is useful only for debugging and dangerous\n# in production.\n#\n# cluster-migration-barrier 1\n\n# Turning off this option allows to use less automatic cluster configuration.\n# It both disables migration to orphaned masters and migration from masters\n# that became empty.\n#\n# Default is 'yes' (allow automatic migrations).\n#\n# cluster-allow-replica-migration yes\n\n# By default KeyDB Cluster nodes stop accepting queries if they detect there\n# is at least a hash slot uncovered (no available node is serving it).\n# This way if the cluster is partially down (for example a range of hash slots\n# are no longer covered) all the cluster becomes, eventually, unavailable.\n# It automatically returns available as soon as all the slots are covered again.\n#\n# However sometimes you want the subset of the cluster which is working,\n# to continue to accept queries for the part of the key space that is still\n# covered. In order to do so, just set the cluster-require-full-coverage\n# option to no.\n#\n# cluster-require-full-coverage yes\n\n# This option, when set to yes, prevents replicas from trying to failover its\n# master during master failures. However the master can still perform a\n# manual failover, if forced to do so.\n#\n# This is useful in different scenarios, especially in the case of multiple\n# data center operations, where we want one side to never be promoted if not\n# in the case of a total DC failure.\n#\n# cluster-replica-no-failover no\n\n# This option, when set to yes, allows nodes to serve read traffic while the\n# the cluster is in a down state, as long as it believes it owns the slots. \n#\n# This is useful for two cases.  The first case is for when an application \n# doesn't require consistency of data during node failures or network partitions.\n# One example of this is a cache, where as long as the node has the data it\n# should be able to serve it. \n#\n# The second use case is for configurations that don't meet the recommended  \n# three shards but want to enable cluster mode and scale later. A \n# master outage in a 1 or 2 shard configuration causes a read/write outage to the\n# entire cluster without this option set, with it set there is only a write outage.\n# Without a quorum of masters, slot ownership will not change automatically. \n#\n# cluster-allow-reads-when-down no\n\n# In order to setup your cluster make sure to read the documentation\n# available at http://redis.io web site.\n\n########################## CLUSTER DOCKER/NAT support  ########################\n\n# In certain deployments, KeyDB Cluster nodes address discovery fails, because\n# addresses are NAT-ted or because ports are forwarded (the typical case is\n# Docker and other containers).\n#\n# In order to make KeyDB Cluster working in such environments, a static\n# configuration where each node knows its public address is needed. The\n# following four options are used for this scope, and are:\n#\n# * cluster-announce-ip\n# * cluster-announce-port\n# * cluster-announce-tls-port\n# * cluster-announce-bus-port\n#\n# Each instructs the node about its address, client ports (for connections\n# without and with TLS), and cluster message\n# bus port. The information is then published in the header of the bus packets\n# so that other nodes will be able to correctly map the address of the node\n# publishing the information.\n#\n# If cluster-tls is set to yes and cluster-announce-tls-port is omitted or set\n# to zero, then cluster-announce-port refers to the TLS port. Note also that\n# cluster-announce-tls-port has no effect if cluster-tls is set to no.\n#\n# If the above options are not used, the normal KeyDB Cluster auto-detection\n# will be used instead.\n#\n# Note that when remapped, the bus port may not be at the fixed offset of\n# clients port + 10000, so you can specify any port and bus-port depending\n# on how they get remapped. If the bus-port is not set, a fixed offset of\n# 10000 will be used as usual.\n#\n# Example:\n#\n# cluster-announce-ip 10.1.1.5\n# cluster-announce-tls-port 6379\n# cluster-announce-port 0\n# cluster-announce-bus-port 6380\n\n################################## SLOW LOG ###################################\n\n# The KeyDB Slow Log is a system to log queries that exceeded a specified\n# execution time. The execution time does not include the I/O operations\n# like talking with the client, sending the reply and so forth,\n# but just the time needed to actually execute the command (this is the only\n# stage of command execution where the thread is blocked and can not serve\n# other requests in the meantime).\n#\n# You can configure the slow log with two parameters: one tells KeyDB\n# what is the execution time, in microseconds, to exceed in order for the\n# command to get logged, and the other parameter is the length of the\n# slow log. When a new command is logged the oldest one is removed from the\n# queue of logged commands.\n\n# The following time is expressed in microseconds, so 1000000 is equivalent\n# to one second. Note that a negative number disables the slow log, while\n# a value of zero forces the logging of every command.\nslowlog-log-slower-than 10000\n\n# There is no limit to this length. Just be aware that it will consume memory.\n# You can reclaim memory used by the slow log with SLOWLOG RESET.\nslowlog-max-len 128\n\n################################ LATENCY MONITOR ##############################\n\n# The KeyDB latency monitoring subsystem samples different operations\n# at runtime in order to collect data related to possible sources of\n# latency of a KeyDB instance.\n#\n# Via the LATENCY command this information is available to the user that can\n# print graphs and obtain reports.\n#\n# The system only logs operations that were performed in a time equal or\n# greater than the amount of milliseconds specified via the\n# latency-monitor-threshold configuration directive. When its value is set\n# to zero, the latency monitor is turned off.\n#\n# By default latency monitoring is disabled since it is mostly not needed\n# if you don't have latency issues, and collecting data has a performance\n# impact, that while very small, can be measured under big load. Latency\n# monitoring can easily be enabled at runtime using the command\n# \"CONFIG SET latency-monitor-threshold <milliseconds>\" if needed.\nlatency-monitor-threshold 0\n\n############################# EVENT NOTIFICATION ##############################\n\n# KeyDB can notify Pub/Sub clients about events happening in the key space.\n# This feature is documented at http://redis.io/topics/notifications\n#\n# For instance if keyspace events notification is enabled, and a client\n# performs a DEL operation on key \"foo\" stored in the Database 0, two\n# messages will be published via Pub/Sub:\n#\n# PUBLISH __keyspace@0__:foo del\n# PUBLISH __keyevent@0__:del foo\n#\n# It is possible to select the events that KeyDB will notify among a set\n# of classes. Every class is identified by a single character:\n#\n#  K     Keyspace events, published with __keyspace@<db>__ prefix.\n#  E     Keyevent events, published with __keyevent@<db>__ prefix.\n#  g     Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ...\n#  $     String commands\n#  l     List commands\n#  s     Set commands\n#  h     Hash commands\n#  z     Sorted set commands\n#  x     Expired events (events generated every time a key expires)\n#  e     Evicted events (events generated when a key is evicted for maxmemory)\n#  t     Stream commands\n#  d     Module key type events\n#  m     Key-miss events (Note: It is not included in the 'A' class)\n#  A     Alias for g$lshzxetd, so that the \"AKE\" string means all the events\n#        (Except key-miss events which are excluded from 'A' due to their\n#         unique nature).\n#\n#  The \"notify-keyspace-events\" takes as argument a string that is composed\n#  of zero or multiple characters. The empty string means that notifications\n#  are disabled.\n#\n#  Example: to enable list and generic events, from the point of view of the\n#           event name, use:\n#\n#  notify-keyspace-events Elg\n#\n#  Example 2: to get the stream of the expired keys subscribing to channel\n#             name __keyevent@0__:expired use:\n#\n#  notify-keyspace-events Ex\n#\n#  By default all notifications are disabled because most users don't need\n#  this feature and the feature has some overhead. Note that if you don't\n#  specify at least one of K or E, no events will be delivered.\nnotify-keyspace-events \"\"\n\n############################### ADVANCED CONFIG ###############################\n\n# Hashes are encoded using a memory efficient data structure when they have a\n# small number of entries, and the biggest entry does not exceed a given\n# threshold. These thresholds can be configured using the following directives.\nhash-max-ziplist-entries 512\nhash-max-ziplist-value 64\n\n# Lists are also encoded in a special way to save a lot of space.\n# The number of entries allowed per internal list node can be specified\n# as a fixed maximum size or a maximum number of elements.\n# For a fixed maximum size, use -5 through -1, meaning:\n# -5: max size: 64 Kb  <-- not recommended for normal workloads\n# -4: max size: 32 Kb  <-- not recommended\n# -3: max size: 16 Kb  <-- probably not recommended\n# -2: max size: 8 Kb   <-- good\n# -1: max size: 4 Kb   <-- good\n# Positive numbers mean store up to _exactly_ that number of elements\n# per list node.\n# The highest performing option is usually -2 (8 Kb size) or -1 (4 Kb size),\n# but if your use case is unique, adjust the settings as necessary.\nlist-max-ziplist-size -2\n\n# Lists may also be compressed.\n# Compress depth is the number of quicklist ziplist nodes from *each* side of\n# the list to *exclude* from compression.  The head and tail of the list\n# are always uncompressed for fast push/pop operations.  Settings are:\n# 0: disable all list compression\n# 1: depth 1 means \"don't start compressing until after 1 node into the list,\n#    going from either the head or tail\"\n#    So: [head]->node->node->...->node->[tail]\n#    [head], [tail] will always be uncompressed; inner nodes will compress.\n# 2: [head]->[next]->node->node->...->node->[prev]->[tail]\n#    2 here means: don't compress head or head->next or tail->prev or tail,\n#    but compress all nodes between them.\n# 3: [head]->[next]->[next]->node->node->...->node->[prev]->[prev]->[tail]\n# etc.\nlist-compress-depth 0\n\n# Sets have a special encoding in just one case: when a set is composed\n# of just strings that happen to be integers in radix 10 in the range\n# of 64 bit signed integers.\n# The following configuration setting sets the limit in the size of the\n# set in order to use this special memory saving encoding.\nset-max-intset-entries 512\n\n# Similarly to hashes and lists, sorted sets are also specially encoded in\n# order to save a lot of space. This encoding is only used when the length and\n# elements of a sorted set are below the following limits:\nzset-max-ziplist-entries 128\nzset-max-ziplist-value 64\n\n# HyperLogLog sparse representation bytes limit. The limit includes the\n# 16 bytes header. When an HyperLogLog using the sparse representation crosses\n# this limit, it is converted into the dense representation.\n#\n# A value greater than 16000 is totally useless, since at that point the\n# dense representation is more memory efficient.\n#\n# The suggested value is ~ 3000 in order to have the benefits of\n# the space efficient encoding without slowing down too much PFADD,\n# which is O(N) with the sparse encoding. The value can be raised to\n# ~ 10000 when CPU is not a concern, but space is, and the data set is\n# composed of many HyperLogLogs with cardinality in the 0 - 15000 range.\nhll-sparse-max-bytes 3000\n\n# Streams macro node max size / items. The stream data structure is a radix\n# tree of big nodes that encode multiple items inside. Using this configuration\n# it is possible to configure how big a single node can be in bytes, and the\n# maximum number of items it may contain before switching to a new node when\n# appending new stream entries. If any of the following settings are set to\n# zero, the limit is ignored, so for instance it is possible to set just a\n# max entires limit by setting max-bytes to 0 and max-entries to the desired\n# value.\nstream-node-max-bytes 4096\nstream-node-max-entries 100\n\n# Active rehashing uses 1 millisecond every 100 milliseconds of CPU time in\n# order to help rehashing the main KeyDB hash table (the one mapping top-level\n# keys to values). The hash table implementation KeyDB uses (see dict.c)\n# performs a lazy rehashing: the more operation you run into a hash table\n# that is rehashing, the more rehashing \"steps\" are performed, so if the\n# server is idle the rehashing is never complete and some more memory is used\n# by the hash table.\n#\n# The default is to use this millisecond 10 times every second in order to\n# actively rehash the main dictionaries, freeing memory when possible.\n#\n# If unsure:\n# use \"activerehashing no\" if you have hard latency requirements and it is\n# not a good thing in your environment that KeyDB can reply from time to time\n# to queries with 2 milliseconds delay.\n#\n# use \"activerehashing yes\" if you don't have such hard requirements but\n# want to free memory asap when possible.\nactiverehashing yes\n\n# The client output buffer limits can be used to force disconnection of clients\n# that are not reading data from the server fast enough for some reason (a\n# common reason is that a Pub/Sub client can't consume messages as fast as the\n# publisher can produce them).\n#\n# The limit can be set differently for the three different classes of clients:\n#\n# normal -> normal clients including MONITOR clients\n# replica  -> replica clients\n# pubsub -> clients subscribed to at least one pubsub channel or pattern\n#\n# The syntax of every client-output-buffer-limit directive is the following:\n#\n# client-output-buffer-limit <class> <hard limit> <soft limit> <soft seconds>\n#\n# A client is immediately disconnected once the hard limit is reached, or if\n# the soft limit is reached and remains reached for the specified number of\n# seconds (continuously).\n# So for instance if the hard limit is 32 megabytes and the soft limit is\n# 16 megabytes / 10 seconds, the client will get disconnected immediately\n# if the size of the output buffers reach 32 megabytes, but will also get\n# disconnected if the client reaches 16 megabytes and continuously overcomes\n# the limit for 10 seconds.\n#\n# By default normal clients are not limited because they don't receive data\n# without asking (in a push way), but just after a request, so only\n# asynchronous clients may create a scenario where data is requested faster\n# than it can read.\n#\n# Instead there is a default limit for pubsub and replica clients, since\n# subscribers and replicas receive data in a push fashion.\n#\n# Both the hard or the soft limit can be disabled by setting them to zero.\nclient-output-buffer-limit normal 0 0 0\nclient-output-buffer-limit replica 256mb 64mb 60\nclient-output-buffer-limit pubsub 32mb 8mb 60\n\n# Client query buffers accumulate new commands. They are limited to a fixed\n# amount by default in order to avoid that a protocol desynchronization (for\n# instance due to a bug in the client) will lead to unbound memory usage in\n# the query buffer. However you can configure it here if you have very special\n# needs, such us huge multi/exec requests or alike.\n#\n# client-query-buffer-limit 1gb\n\n# In the KeyDB protocol, bulk requests, that are, elements representing single\n# strings, are normally limited to 512 mb. However you can change this limit\n# here, but must be 1mb or greater\n#\n# proto-max-bulk-len 512mb\n\n# KeyDB calls an internal function to perform many background tasks, like\n# closing connections of clients in timeout, purging expired keys that are\n# never requested, and so forth.\n#\n# Not all tasks are performed with the same frequency, but KeyDB checks for\n# tasks to perform according to the specified \"hz\" value.\n#\n# By default \"hz\" is set to 10. Raising the value will use more CPU when\n# KeyDB is idle, but at the same time will make KeyDB more responsive when\n# there are many keys expiring at the same time, and timeouts may be\n# handled with more precision.\n#\n# The range is between 1 and 500, however a value over 100 is usually not\n# a good idea. Most users should use the default of 10 and raise this up to\n# 100 only in environments where very low latency is required.\nhz 10\n\n# Normally it is useful to have an HZ value which is proportional to the\n# number of clients connected. This is useful in order, for instance, to\n# avoid too many clients are processed for each background task invocation\n# in order to avoid latency spikes.\n#\n# Since the default HZ value by default is conservatively set to 10, KeyDB\n# offers, and enables by default, the ability to use an adaptive HZ value\n# which will temporarily raise when there are many connected clients.\n#\n# When dynamic HZ is enabled, the actual configured HZ will be used\n# as a baseline, but multiples of the configured HZ value will be actually\n# used as needed once more clients are connected. In this way an idle\n# instance will use very little CPU time while a busy instance will be\n# more responsive.\ndynamic-hz yes\n\n# When a child rewrites the AOF file, if the following option is enabled\n# the file will be fsync-ed every 32 MB of data generated. This is useful\n# in order to commit the file to the disk more incrementally and avoid\n# big latency spikes.\naof-rewrite-incremental-fsync yes\n\n# When KeyDB saves RDB file, if the following option is enabled\n# the file will be fsync-ed every 32 MB of data generated. This is useful\n# in order to commit the file to the disk more incrementally and avoid\n# big latency spikes.\nrdb-save-incremental-fsync yes\n\n# KeyDB LFU eviction (see maxmemory setting) can be tuned. However it is a good\n# idea to start with the default settings and only change them after investigating\n# how to improve the performances and how the keys LFU change over time, which\n# is possible to inspect via the OBJECT FREQ command.\n#\n# There are two tunable parameters in the KeyDB LFU implementation: the\n# counter logarithm factor and the counter decay time. It is important to\n# understand what the two parameters mean before changing them.\n#\n# The LFU counter is just 8 bits per key, it's maximum value is 255, so KeyDB\n# uses a probabilistic increment with logarithmic behavior. Given the value\n# of the old counter, when a key is accessed, the counter is incremented in\n# this way:\n#\n# 1. A random number R between 0 and 1 is extracted.\n# 2. A probability P is calculated as 1/(old_value*lfu_log_factor+1).\n# 3. The counter is incremented only if R < P.\n#\n# The default lfu-log-factor is 10. This is a table of how the frequency\n# counter changes with a different number of accesses with different\n# logarithmic factors:\n#\n# +--------+------------+------------+------------+------------+------------+\n# | factor | 100 hits   | 1000 hits  | 100K hits  | 1M hits    | 10M hits   |\n# +--------+------------+------------+------------+------------+------------+\n# | 0      | 104        | 255        | 255        | 255        | 255        |\n# +--------+------------+------------+------------+------------+------------+\n# | 1      | 18         | 49         | 255        | 255        | 255        |\n# +--------+------------+------------+------------+------------+------------+\n# | 10     | 10         | 18         | 142        | 255        | 255        |\n# +--------+------------+------------+------------+------------+------------+\n# | 100    | 8          | 11         | 49         | 143        | 255        |\n# +--------+------------+------------+------------+------------+------------+\n#\n# NOTE: The above table was obtained by running the following commands:\n#\n#   keydb-benchmark -n 1000000 incr foo\n#   keydb-cli object freq foo\n#\n# NOTE 2: The counter initial value is 5 in order to give new objects a chance\n# to accumulate hits.\n#\n# The counter decay time is the time, in minutes, that must elapse in order\n# for the key counter to be divided by two (or decremented if it has a value\n# less <= 10).\n#\n# The default value for the lfu-decay-time is 1. A special value of 0 means to\n# decay the counter every time it happens to be scanned.\n#\n# lfu-log-factor 10\n# lfu-decay-time 1\n\n########################### ACTIVE DEFRAGMENTATION #######################\n#\n# What is active defragmentation?\n# -------------------------------\n#\n# Active (online) defragmentation allows a KeyDB server to compact the\n# spaces left between small allocations and deallocations of data in memory,\n# thus allowing to reclaim back memory.\n#\n# Fragmentation is a natural process that happens with every allocator (but\n# less so with Jemalloc, fortunately) and certain workloads. Normally a server\n# restart is needed in order to lower the fragmentation, or at least to flush\n# away all the data and create it again. However thanks to this feature\n# implemented by Oran Agra for Redis 4.0 this process can happen at runtime\n# in a \"hot\" way, while the server is running.\n#\n# Basically when the fragmentation is over a certain level (see the\n# configuration options below) KeyDB will start to create new copies of the\n# values in contiguous memory regions by exploiting certain specific Jemalloc\n# features (in order to understand if an allocation is causing fragmentation\n# and to allocate it in a better place), and at the same time, will release the\n# old copies of the data. This process, repeated incrementally for all the keys\n# will cause the fragmentation to drop back to normal values.\n#\n# Important things to understand:\n#\n# 1. This feature is disabled by default, and only works if you compiled KeyDB\n#    to use the copy of Jemalloc we ship with the source code of KeyDB.\n#    This is the default with Linux builds.\n#\n# 2. You never need to enable this feature if you don't have fragmentation\n#    issues.\n#\n# 3. Once you experience fragmentation, you can enable this feature when\n#    needed with the command \"CONFIG SET activedefrag yes\".\n#\n# The configuration parameters are able to fine tune the behavior of the\n# defragmentation process. If you are not sure about what they mean it is\n# a good idea to leave the defaults untouched.\n\n# Enabled active defragmentation\n# activedefrag no\n\n# Minimum amount of fragmentation waste to start active defrag\n# active-defrag-ignore-bytes 100mb\n\n# Minimum percentage of fragmentation to start active defrag\n# active-defrag-threshold-lower 10\n\n# Maximum percentage of fragmentation at which we use maximum effort\n# active-defrag-threshold-upper 100\n\n# Minimal effort for defrag in CPU percentage, to be used when the lower\n# threshold is reached\n# active-defrag-cycle-min 1\n\n# Maximal effort for defrag in CPU percentage, to be used when the upper\n# threshold is reached\n# active-defrag-cycle-max 25\n\n# Maximum number of set/hash/zset/list fields that will be processed from\n# the main dictionary scan\n# active-defrag-max-scan-fields 1000\n\n# Jemalloc background thread for purging will be enabled by default\njemalloc-bg-thread yes\n\n# It is possible to pin different threads and processes of KeyDB to specific\n# CPUs in your system, in order to maximize the performances of the server.\n# This is useful both in order to pin different KeyDB threads in different\n# CPUs, but also in order to make sure that multiple KeyDB instances running\n# in the same host will be pinned to different CPUs.\n#\n# Normally you can do this using the \"taskset\" command, however it is also\n# possible to this via KeyDB configuration directly, both in Linux and FreeBSD.\n#\n# You can pin the server/IO threads, bio threads, aof rewrite child process, and\n# the bgsave child process. The syntax to specify the cpu list is the same as\n# the taskset command:\n#\n# Set redis server/io threads to cpu affinity 0,2,4,6:\n# server_cpulist 0-7:2\n#\n# Set bio threads to cpu affinity 1,3:\n# bio_cpulist 1,3\n#\n# Set aof rewrite child process to cpu affinity 8,9,10,11:\n# aof_rewrite_cpulist 8-11\n#\n# Set bgsave child process to cpu affinity 1,10,11\n# bgsave_cpulist 1,10-11\n\n# In some cases KeyDB will emit warnings and even refuse to start if it detects\n# that the system is in bad state, it is possible to suppress these warnings\n# by setting the following config which takes a space delimited list of warnings\n# to suppress\n#\n# ignore-warnings ARM64-COW-BUG\n\n# The minimum number of clients on a thread before KeyDB assigns new connections to a different thread\n#  Tuning this parameter is a tradeoff between locking overhead and distributing the workload over multiple cores\n# min-clients-per-thread 50\n\n# How often to run RDB load progress callback?\n# The callback runs during key load to ping other servers and prevent timeouts.\n# It also updates load time estimates.\n# Change these values to run it more or less often. It will run when either condition is true.\n# Either when x bytes have been processed, or when x keys have been loaded.\n# loading-process-events-interval-bytes 2097152\n# loading-process-events-interval-keys 8192\n\n# Avoid forwarding RREPLAY messages to other masters?\n#   WARNING: This setting is dangerous! You must be certain all masters are connected to each\n#   other in a true mesh topology or data loss will occur!\n#   This command can be used to reduce multimaster bus traffic\n# multi-master-no-forward no\n\n# Path to directory for file backed scratchpad.  The file backed scratchpad\n# reduces memory requirements by storing rarely accessed data on disk \n# instead of RAM.  A temporary file will be created in this directory.\n# scratch-file-path /tmp/\n\n# Number of worker threads serving requests.  This number should be related to the performance\n# of your network hardware, not the number of cores on your machine.  We don't recommend going\n# above 4 at this time.  By default this is set 1.\n#\n# Note: KeyDB does not use io-threads, but io-threads is a config alias for server-threads\nserver-threads 2\n\n# Should KeyDB pin threads to CPUs? By default this is disabled, and KeyDB will not bind threads.\n# When enabled threads are bount to cores sequentially starting at core 0.\n# server-thread-affinity true\n\n# Uncomment the option below to enable Active Active support.  Note that\n# replicas will still sync in the normal way and incorrect ordering when\n# bringing up replicas can result in data loss (the first master will win).\n# active-replica yes\n\n# KeyDB will attempt to balance clients across threads evenly; However, replica clients\n# are usually much more expensive than a normal client, and so KeyDB will try to assign\n# fewer clients to threads with a replica.  The weighting factor below is intented to help tune\n# this behavior.  A replica weighting factor of 2 means we treat a replica as the equivalent\n# of two normal clients.  Adjusting this value may improve performance when replication is\n# used.  The best weighting is workload specific - e.g. read heavy workloads should set\n# this to 1.  Very write heavy workloads may benefit from higher numbers.\n#\n# By default KeyDB sets this to 2.\nreplica-weighting-factor 2\n\n# Enable FLASH support (Experimental Feature)\n# storage-provider flash /path/to/flash/db\n\n# Blob support is a way to store very large objects (>200MB) on disk\n# The files are automatically cleaned up when KeyDB exits and are only\n# for temporary use.  This helps reduce memory pressure for very large\n# data items at the cost of some performance.\n# \n# By default this config is disable.  When enabled the disk associated\n# with KeyDB's working directory will be used.  If there is insufficient\n# disk space or any other I/O error KeyDB will instead use memory.\n# \n# blob-support false\n"
  },
  {
    "path": "pkg/rpm/keydb_build/keydb_rpm/etc/keydb/sentinel.conf",
    "content": "# Example sentinel.conf\n\n# *** IMPORTANT ***\n#\n# By default Sentinel will not be reachable from interfaces different than\n# localhost, either use the 'bind' directive to bind to a list of network\n# interfaces, or disable protected mode with \"protected-mode no\" by\n# adding it to this configuration file.\n#\n# Before doing that MAKE SURE the instance is protected from the outside\n# world via firewalling or other means.\n#\n# For example you may use one of the following:\n#\n# bind 127.0.0.1 192.168.1.1\n#\n# protected-mode no\n\n# port <sentinel-port>\n# The port that this sentinel instance will run on\nport 26379\n\n# By default KeyDB Sentinel does not run as a daemon. Use 'yes' if you need it.\n# Note that KeyDB will write a pid file in /var/run/keydb-sentinel.pid when\n# daemonized.\ndaemonize no\n\n# When running daemonized, KeyDB Sentinel writes a pid file in\n# /var/run/keydb-sentinel.pid by default. You can specify a custom pid file\n# location here.\npidfile /var/run/sentinel/keydb-sentinel.pid\n\n# Specify the log file name. Also the empty string can be used to force\n# Sentinel to log on the standard output. Note that if you use standard\n# output for logging but daemonize, logs will be sent to /dev/null\nlogfile /var/log/keydb/keydb-sentinel.log\n\n# sentinel announce-ip <ip>\n# sentinel announce-port <port>\n#\n# The above two configuration directives are useful in environments where,\n# because of NAT, Sentinel is reachable from outside via a non-local address.\n#\n# When announce-ip is provided, the Sentinel will claim the specified IP address\n# in HELLO messages used to gossip its presence, instead of auto-detecting the\n# local address as it usually does.\n#\n# Similarly when announce-port is provided and is valid and non-zero, Sentinel\n# will announce the specified TCP port.\n#\n# The two options don't need to be used together, if only announce-ip is\n# provided, the Sentinel will announce the specified IP and the server port\n# as specified by the \"port\" option. If only announce-port is provided, the\n# Sentinel will announce the auto-detected local IP and the specified port.\n#\n# Example:\n#\n# sentinel announce-ip 1.2.3.4\n\n# dir <working-directory>\n# Every long running process should have a well-defined working directory.\n# For KeyDB Sentinel to chdir to /tmp at startup is the simplest thing\n# for the process to don't interfere with administrative tasks such as\n# unmounting filesystems.\ndir /tmp\n\n# sentinel monitor <master-name> <ip> <keydb-port> <quorum>\n#\n# Tells Sentinel to monitor this master, and to consider it in O_DOWN\n# (Objectively Down) state only if at least <quorum> sentinels agree.\n#\n# Note that whatever is the ODOWN quorum, a Sentinel will require to\n# be elected by the majority of the known Sentinels in order to\n# start a failover, so no failover can be performed in minority.\n#\n# Replicas are auto-discovered, so you don't need to specify replicas in\n# any way. Sentinel itself will rewrite this configuration file adding\n# the replicas using additional configuration options.\n# Also note that the configuration file is rewritten when a\n# replica is promoted to master.\n#\n# Note: master name should not include special characters or spaces.\n# The valid charset is A-z 0-9 and the three characters \".-_\".\nsentinel monitor mymaster 127.0.0.1 6379 2\n\n# sentinel auth-pass <master-name> <password>\n#\n# Set the password to use to authenticate with the master and replicas.\n# Useful if there is a password set in the KeyDB instances to monitor.\n#\n# Note that the master password is also used for replicas, so it is not\n# possible to set a different password in masters and replicas instances\n# if you want to be able to monitor these instances with Sentinel.\n#\n# However you can have KeyDB instances without the authentication enabled\n# mixed with KeyDB instances requiring the authentication (as long as the\n# password set is the same for all the instances requiring the password) as\n# the AUTH command will have no effect in KeyDB instances with authentication\n# switched off.\n#\n# Example:\n#\n# sentinel auth-pass mymaster MySUPER--secret-0123passw0rd\n\n# sentinel auth-user <master-name> <username>\n#\n# This is useful in order to authenticate to instances having ACL capabilities,\n# that is, running KeyDB 6.0 or greater. When just auth-pass is provided the\n# Sentinel instance will authenticate to KeyDB using the old \"AUTH <pass>\"\n# method. When also an username is provided, it will use \"AUTH <user> <pass>\".\n# In the KeyDB servers side, the ACL to provide just minimal access to\n# Sentinel instances, should be configured along the following lines:\n#\n#     user sentinel-user >somepassword +client +subscribe +publish \\\n#                        +ping +info +multi +slaveof +config +client +exec on\n\n# sentinel down-after-milliseconds <master-name> <milliseconds>\n#\n# Number of milliseconds the master (or any attached replica or sentinel) should\n# be unreachable (as in, not acceptable reply to PING, continuously, for the\n# specified period) in order to consider it in S_DOWN state (Subjectively\n# Down).\n#\n# Default is 30 seconds.\nsentinel down-after-milliseconds mymaster 30000\n\n# IMPORTANT NOTE: starting with KeyDB 6.2 ACL capability is supported for\n# Sentinel mode, please refer to the Redis website https://redis.io/topics/acl\n# for more details.\n\n# Sentinel's ACL users are defined in the following format:\n#\n#   user <username> ... acl rules ...\n#\n# For example:\n#\n#   user worker +@admin +@connection ~* on >ffa9203c493aa99\n#\n# For more information about ACL configuration please refer to the Redis\n# website at https://redis.io/topics/acl and KeyDB server configuration \n# template keydb.conf.\n\n# ACL LOG\n#\n# The ACL Log tracks failed commands and authentication events associated\n# with ACLs. The ACL Log is useful to troubleshoot failed commands blocked \n# by ACLs. The ACL Log is stored in memory. You can reclaim memory with \n# ACL LOG RESET. Define the maximum entry length of the ACL Log below.\nacllog-max-len 128\n\n# Using an external ACL file\n#\n# Instead of configuring users here in this file, it is possible to use\n# a stand-alone file just listing users. The two methods cannot be mixed:\n# if you configure users here and at the same time you activate the external\n# ACL file, the server will refuse to start.\n#\n# The format of the external ACL user file is exactly the same as the\n# format that is used inside keydb.conf to describe users.\n#\n# aclfile /etc/keydb/sentinel-users.acl\n\n# requirepass <password>\n#\n# You can configure Sentinel itself to require a password, however when doing\n# so Sentinel will try to authenticate with the same password to all the\n# other Sentinels. So you need to configure all your Sentinels in a given\n# group with the same \"requirepass\" password. Check the following documentation\n# for more info: https://redis.io/topics/sentinel\n#\n# IMPORTANT NOTE: starting with KeyDB 6.2 \"requirepass\" is a compatibility\n# layer on top of the ACL system. The option effect will be just setting\n# the password for the default user. Clients will still authenticate using\n# AUTH <password> as usually, or more explicitly with AUTH default <password>\n# if they follow the new protocol: both will work.\n#\n# New config files are advised to use separate authentication control for\n# incoming connections (via ACL), and for outgoing connections (via\n# sentinel-user and sentinel-pass) \n#\n# The requirepass is not compatable with aclfile option and the ACL LOAD\n# command, these will cause requirepass to be ignored.\n\n# sentinel sentinel-user <username>\n#\n# You can configure Sentinel to authenticate with other Sentinels with specific\n# user name. \n\n# sentinel sentinel-pass <password>\n#\n# The password for Sentinel to authenticate with other Sentinels. If sentinel-user\n# is not configured, Sentinel will use 'default' user with sentinel-pass to authenticate.\n\n# sentinel parallel-syncs <master-name> <numreplicas>\n#\n# How many replicas we can reconfigure to point to the new replica simultaneously\n# during the failover. Use a low number if you use the replicas to serve query\n# to avoid that all the replicas will be unreachable at about the same\n# time while performing the synchronization with the master.\nsentinel parallel-syncs mymaster 1\n\n# sentinel failover-timeout <master-name> <milliseconds>\n#\n# Specifies the failover timeout in milliseconds. It is used in many ways:\n#\n# - The time needed to re-start a failover after a previous failover was\n#   already tried against the same master by a given Sentinel, is two\n#   times the failover timeout.\n#\n# - The time needed for a replica replicating to a wrong master according\n#   to a Sentinel current configuration, to be forced to replicate\n#   with the right master, is exactly the failover timeout (counting since\n#   the moment a Sentinel detected the misconfiguration).\n#\n# - The time needed to cancel a failover that is already in progress but\n#   did not produced any configuration change (SLAVEOF NO ONE yet not\n#   acknowledged by the promoted replica).\n#\n# - The maximum time a failover in progress waits for all the replicas to be\n#   reconfigured as replicas of the new master. However even after this time\n#   the replicas will be reconfigured by the Sentinels anyway, but not with\n#   the exact parallel-syncs progression as specified.\n#\n# Default is 3 minutes.\nsentinel failover-timeout mymaster 180000\n\n# SCRIPTS EXECUTION\n#\n# sentinel notification-script and sentinel reconfig-script are used in order\n# to configure scripts that are called to notify the system administrator\n# or to reconfigure clients after a failover. The scripts are executed\n# with the following rules for error handling:\n#\n# If script exits with \"1\" the execution is retried later (up to a maximum\n# number of times currently set to 10).\n#\n# If script exits with \"2\" (or an higher value) the script execution is\n# not retried.\n#\n# If script terminates because it receives a signal the behavior is the same\n# as exit code 1.\n#\n# A script has a maximum running time of 60 seconds. After this limit is\n# reached the script is terminated with a SIGKILL and the execution retried.\n\n# NOTIFICATION SCRIPT\n#\n# sentinel notification-script <master-name> <script-path>\n# \n# Call the specified notification script for any sentinel event that is\n# generated in the WARNING level (for instance -sdown, -odown, and so forth).\n# This script should notify the system administrator via email, SMS, or any\n# other messaging system, that there is something wrong with the monitored\n# KeyDB systems.\n#\n# The script is called with just two arguments: the first is the event type\n# and the second the event description.\n#\n# The script must exist and be executable in order for sentinel to start if\n# this option is provided.\n#\n# Example:\n#\n# sentinel notification-script mymaster /var/keydb/notify.sh\n\n# CLIENTS RECONFIGURATION SCRIPT\n#\n# sentinel client-reconfig-script <master-name> <script-path>\n#\n# When the master changed because of a failover a script can be called in\n# order to perform application-specific tasks to notify the clients that the\n# configuration has changed and the master is at a different address.\n# \n# The following arguments are passed to the script:\n#\n# <master-name> <role> <state> <from-ip> <from-port> <to-ip> <to-port>\n#\n# <state> is currently always \"failover\"\n# <role> is either \"leader\" or \"observer\"\n# \n# The arguments from-ip, from-port, to-ip, to-port are used to communicate\n# the old address of the master and the new address of the elected replica\n# (now a master).\n#\n# This script should be resistant to multiple invocations.\n#\n# Example:\n#\n# sentinel client-reconfig-script mymaster /var/keydb/reconfig.sh\n\n# SECURITY\n#\n# By default SENTINEL SET will not be able to change the notification-script\n# and client-reconfig-script at runtime. This avoids a trivial security issue\n# where clients can set the script to anything and trigger a failover in order\n# to get the program executed.\n\nsentinel deny-scripts-reconfig yes\n\n# KEYDB COMMANDS RENAMING\n#\n# Sometimes the KeyDB server has certain commands, that are needed for Sentinel\n# to work correctly, renamed to unguessable strings. This is often the case\n# of CONFIG and SLAVEOF in the context of providers that provide KeyDB as\n# a service, and don't want the customers to reconfigure the instances outside\n# of the administration console.\n#\n# In such case it is possible to tell Sentinel to use different command names\n# instead of the normal ones. For example if the master \"mymaster\", and the\n# associated replicas, have \"CONFIG\" all renamed to \"GUESSME\", I could use:\n#\n# SENTINEL rename-command mymaster CONFIG GUESSME\n#\n# After such configuration is set, every time Sentinel would use CONFIG it will\n# use GUESSME instead. Note that there is no actual need to respect the command\n# case, so writing \"config guessme\" is the same in the example above.\n#\n# SENTINEL SET can also be used in order to perform this configuration at runtime.\n#\n# In order to set a command back to its original name (undo the renaming), it\n# is possible to just rename a command to itself:\n#\n# SENTINEL rename-command mymaster CONFIG CONFIG\n\n# HOSTNAMES SUPPORT\n#\n# Normally Sentinel uses only IP addresses and requires SENTINEL MONITOR\n# to specify an IP address. Also, it requires the KeyDB replica-announce-ip\n# keyword to specify only IP addresses.\n#\n# You may enable hostnames support by enabling resolve-hostnames. Note\n# that you must make sure your DNS is configured properly and that DNS\n# resolution does not introduce very long delays.\n#\nSENTINEL resolve-hostnames no\n\n# When resolve-hostnames is enabled, Sentinel still uses IP addresses\n# when exposing instances to users, configuration files, etc. If you want\n# to retain the hostnames when announced, enable announce-hostnames below.\n#\nSENTINEL announce-hostnames no\n"
  },
  {
    "path": "pkg/rpm/keydb_build/keydb_rpm/etc/logrotate.d/keydb",
    "content": "/var/log/keydb/*.log {\n    weekly\n    rotate 10\n    copytruncate\n    delaycompress\n    compress\n    notifempty\n    missingok\n}\n"
  },
  {
    "path": "pkg/rpm/keydb_build/keydb_rpm/etc/systemd/system/keydb-sentinel.service.d/limit.conf",
    "content": "# If you need to change max open file limit\n# for example, when you change maxclient in configuration\n# you can change the LimitNOFILE value below\n# see \"man systemd.exec\" for information\n\n[Service]\nLimitNOFILE=10240\n"
  },
  {
    "path": "pkg/rpm/keydb_build/keydb_rpm/etc/systemd/system/keydb.service.d/limit.conf",
    "content": "# If you need to change max open file limit\n# for example, when you change maxclient in configuration\n# you can change the LimitNOFILE value below\n# see \"man systemd.exec\" for information\n\n[Service]\nLimitNOFILE=10240\n"
  },
  {
    "path": "pkg/rpm/keydb_build/keydb_rpm/usr/lib/systemd/system/keydb-sentinel.service",
    "content": "[Unit]\nDescription=Advanced key-value store\nAfter=network.target\nDocumentation=http://keydb.io/documentation, man:keydb-sentinel(1)\n\n[Service]\nType=forking\nExecStart=/usr/bin/keydb-sentinel /etc/keydb/sentinel.conf\nExecStop=/bin/kill -s TERM $MAINPID\nPIDFile=/var/run/sentinel/keydb-sentinel.pid\nTimeoutStopSec=0\nRestart=always\nUser=keydb\nGroup=keydb\nRuntimeDirectory=sentinel\nRuntimeDirectoryMode=2755\n\nUMask=007\nPrivateTmp=yes\nLimitNOFILE=65535\nPrivateDevices=yes\nProtectHome=yes\nReadOnlyDirectories=/\nReadWriteDirectories=-/var/lib/keydb\nReadWriteDirectories=-/var/log/keydb\nReadWriteDirectories=-/var/run/sentinel\n\nNoNewPrivileges=true\nCapabilityBoundingSet=CAP_SETGID CAP_SETUID CAP_SYS_RESOURCE\nRestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX\n\n# keydb-sentinel can write to its own config file when in cluster mode so we\n# permit writing there by default. If you are not using this feature, it is\n# recommended that you replace the following lines with \"ProtectSystem=full\".\nProtectSystem=true\nReadWriteDirectories=-/etc/keydb\n\n[Install]\nWantedBy=multi-user.target\nAlias=sentinel.service\n"
  },
  {
    "path": "pkg/rpm/keydb_build/keydb_rpm/usr/lib/systemd/system/keydb.service",
    "content": "[Unit]\nDescription=Advanced key-value store\nAfter=network.target\nDocumentation=https://docs.keydb.dev, man:keydb-server(1)\n\n[Service]\nType=forking\nExecStart=/usr/bin/keydb-server /etc/keydb/keydb.conf\nExecStop=/bin/kill -s TERM $MAINPID\nPIDFile=/var/run/keydb/keydb-server.pid\nTimeoutStopSec=0\nRestart=always\nUser=keydb\nGroup=keydb\nRuntimeDirectory=keydb\nRuntimeDirectoryMode=2755\n\nUMask=007\nPrivateTmp=yes\nLimitNOFILE=65535\nPrivateDevices=yes\nProtectHome=yes\nReadOnlyDirectories=/\nReadWriteDirectories=-/var/lib/keydb\nReadWriteDirectories=-/var/log/keydb\nReadWriteDirectories=-/var/run/keydb\n\nNoNewPrivileges=true\nCapabilityBoundingSet=CAP_SETGID CAP_SETUID CAP_SYS_RESOURCE\nRestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX\n\n# keydb-server can write to its own config file when in cluster mode so we\n# permit writing there by default. If you are not using this feature, it is\n# recommended that you replace the following lines with \"ProtectSystem=full\".\nProtectSystem=true\nReadWriteDirectories=-/etc/keydb\n\n[Install]\nWantedBy=multi-user.target\nAlias=keydb.service\n"
  },
  {
    "path": "pkg/rpm/keydb_build/keydb_rpm/usr/libexec/keydb-shutdown",
    "content": "#!/bin/bash\n#\n# Wrapper to close properly keydb and sentinel\ntest x\"$KEYDB_DEBUG\" != x && set -x\n\nKEYDB_CLI=/usr/bin/keydb-cli\n\n# Retrieve service name\nSERVICE_NAME=\"$1\"\nif [ -z \"$SERVICE_NAME\" ]; then\n   SERVICE_NAME=keydb\nfi\n\n# Get the proper config file based on service name\nCONFIG_FILE=\"/etc/keydb/$SERVICE_NAME.conf\"\n\n# Use awk to retrieve host, port from config file\nHOST=`awk '/^[[:blank:]]*bind/ { print $2 }' $CONFIG_FILE | tail -n1`\nPORT=`awk '/^[[:blank:]]*port/ { print $2 }' $CONFIG_FILE | tail -n1`\nPASS=`awk '/^[[:blank:]]*requirepass/ { print $2 }' $CONFIG_FILE | tail -n1`\nSOCK=`awk '/^[[:blank:]]*unixsocket\\s/ { print $2 }' $CONFIG_FILE | tail -n1`\n\n# Just in case, use default host, port\nHOST=${HOST:-127.0.0.1}\nif [ \"$SERVICE_NAME\" = keydb ]; then\n    PORT=${PORT:-6379}\nelse\n    PORT=${PORT:-26739}\nfi\n\n# Setup additional parameters\n# e.g password-protected keydb instances\n[ -z \"$PASS\"  ] || ADDITIONAL_PARAMS=\"-a $PASS\"\n\n# shutdown the service properly\nif [ -e \"$SOCK\" ] ; then\n\t$KEYDB_CLI -s $SOCK $ADDITIONAL_PARAMS shutdown\nelse\n\t$KEYDB_CLI -h $HOST -p $PORT $ADDITIONAL_PARAMS shutdown\nfi\n"
  },
  {
    "path": "pkg/rpm/keydb_build/keydb_rpm/usr/share/licenses/keydb/COPYING",
    "content": "Copyright (c) 2006-2020, Salvatore Sanfilippo\nCopyright (C) 2019, John Sully\nCopyright (C) 2020-2021, EQ Alpha Technology Ltd.\nCopyright (C) 2021, Snap Inc.\n\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:\n\n    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.\n    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.\n    * Neither the name of Redis nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
  },
  {
    "path": "pkg/rpm/keydb_build/keydb_rpm/usr/share/licenses/keydb/COPYING-hiredis",
    "content": "Copyright (c) 2009-2011, Salvatore Sanfilippo <antirez at gmail dot com>\nCopyright (c) 2010-2011, Pieter Noordhuis <pcnoordhuis at gmail dot com>\n\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are met:\n\n* Redistributions of source code must retain the above copyright notice,\n  this list of conditions and the following disclaimer.\n\n* Redistributions in binary form must reproduce the above copyright notice,\n  this list of conditions and the following disclaimer in the documentation\n  and/or other materials provided with the distribution.\n\n* Neither the name of Redis nor the names of its contributors may be used\n  to endorse or promote products derived from this software without specific\n  prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND\nANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\nWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\nDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR\nANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\nLOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\nANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\nSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
  },
  {
    "path": "pkg/rpm/keydb_build/keydb_rpm/usr/share/licenses/keydb/COPYING-jemalloc",
    "content": "Unless otherwise specified, files in the jemalloc source distribution are\nsubject to the following license:\n--------------------------------------------------------------------------------\nCopyright (C) 2002-2018 Jason Evans <jasone@canonware.com>.\nAll rights reserved.\nCopyright (C) 2007-2012 Mozilla Foundation.  All rights reserved.\nCopyright (C) 2009-2018 Facebook, Inc.  All rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are met:\n1. Redistributions of source code must retain the above copyright notice(s),\n   this list of conditions and the following disclaimer.\n2. Redistributions in binary form must reproduce the above copyright notice(s),\n   this list of conditions and the following disclaimer in the documentation\n   and/or other materials provided with the distribution.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS\nOR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\nMERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\nEVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,\nINCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\nLIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\nPROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\nLIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE\nOR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\nADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n--------------------------------------------------------------------------------\n"
  },
  {
    "path": "pkg/rpm/keydb_build/keydb_rpm/usr/share/licenses/keydb/COPYRIGHT-lua",
    "content": "Lua License\n-----------\n\nLua is licensed under the terms of the MIT license reproduced below.\nThis means that Lua is free software and can be used for both academic\nand commercial purposes at absolutely no cost.\n\nFor details and rationale, see http://www.lua.org/license.html .\n\n===============================================================================\n\nCopyright (C) 1994-2012 Lua.org, PUC-Rio.\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n\n===============================================================================\n\n(end of COPYRIGHT)\n"
  },
  {
    "path": "pkg/rpm/rpm_files_generated/.gitkeep",
    "content": "# keep this directory\n"
  },
  {
    "path": "runtest",
    "content": "#!/bin/sh\nTCL_VERSIONS=\"8.5 8.6\"\nTCLSH=\"\"\n\nexport ASAN_OPTIONS=allocator_may_return_null=1  $ASAN_OPTIONS\n\nfor VERSION in $TCL_VERSIONS; do\n\tTCL=`which tclsh$VERSION 2>/dev/null` && TCLSH=$TCL\ndone\n\nif [ -z $TCLSH ]\nthen\n    echo \"You need tcl 8.5 or newer in order to run the KeyDB test\"\n    exit 1\nfi\n$TCLSH tests/test_helper.tcl \"${@}\"\n"
  },
  {
    "path": "runtest-cluster",
    "content": "#!/bin/sh\nTCL_VERSIONS=\"8.5 8.6\"\nTCLSH=\"\"\n\nfor VERSION in $TCL_VERSIONS; do\n\tTCL=`which tclsh$VERSION 2>/dev/null` && TCLSH=$TCL\ndone\n\nif [ -z $TCLSH ]\nthen\n    echo \"You need tcl 8.5 or newer in order to run the KeyDB Cluster test\"\n    exit 1\nfi\n$TCLSH tests/cluster/run.tcl $*\n"
  },
  {
    "path": "runtest-moduleapi",
    "content": "#!/bin/sh\nTCL_VERSIONS=\"8.5 8.6\"\nTCLSH=\"\"\n[ -z \"$MAKE\" ] && MAKE=make\n\nfor VERSION in $TCL_VERSIONS; do\n\tTCL=`which tclsh$VERSION 2>/dev/null` && TCLSH=$TCL\ndone\n\nif [ -z $TCLSH ]\nthen\n    echo \"You need tcl 8.5 or newer in order to run the KeyDB ModuleApi test\"\n    exit 1\nfi\n\n$MAKE -C tests/modules && \\\n$TCLSH tests/test_helper.tcl \\\n--single unit/moduleapi/commandfilter \\\n--single unit/moduleapi/basics \\\n--single unit/moduleapi/fork \\\n--single unit/moduleapi/testrdb \\\n--single unit/moduleapi/infotest \\\n--single unit/moduleapi/propagate \\\n--single unit/moduleapi/hooks \\\n--single unit/moduleapi/misc \\\n--single unit/moduleapi/blockonkeys \\\n--single unit/moduleapi/blockonbackground \\\n--single unit/moduleapi/scan \\\n--single unit/moduleapi/datatype \\\n--single unit/moduleapi/auth \\\n--single unit/moduleapi/keyspace_events \\\n--single unit/moduleapi/blockedclient \\\n--single unit/moduleapi/moduleloadsave \\\n--single unit/moduleapi/getkeys \\\n--single unit/moduleapi/test_lazyfree \\\n--single unit/moduleapi/defrag \\\n--single unit/moduleapi/hash \\\n--single unit/moduleapi/zset \\\n--single unit/moduleapi/stream \\\n--single unit/moduleapi/load \\\n--config server-threads 3 \\\n\"${@}\"\n"
  },
  {
    "path": "runtest-rotation",
    "content": "#!/bin/sh\nTCL_VERSIONS=\"8.5 8.6\"\nTCLSH=\"\"\n\nexport ASAN_OPTIONS=allocator_may_return_null=1  $ASAN_OPTIONS\n\nfor VERSION in $TCL_VERSIONS; do\n\tTCL=`which tclsh$VERSION 2>/dev/null` && TCLSH=$TCL\ndone\n\nif [ -z $TCLSH ]\nthen\n    echo \"You need tcl 8.5 or newer in order to run the KeyDB test\"\n    exit 1\nfi\nif [ ! -r tests/tls ] || [ ! -r tests/tls_1 ] || [ ! -r tests/tls_2 ];\nthen\n    echo \"Generating neccessary certificates for TLS rotation testing.\"\n    rm -rf tests/tls tests/tls_1 tests/tls_2\n\n    utils/gen-test-certs.sh\n    mv tests/tls tests/tls_1\n    utils/gen-test-certs.sh\n    mv tests/tls tests/tls_2\n    utils/gen-test-certs.sh\nfi\n$TCLSH tests/test_helper.tcl \\\n--single unit/tls-rotation \\\n--tls \\\n--config server-threads 3 \\\n\"${@}\"\n"
  },
  {
    "path": "runtest-sentinel",
    "content": "#!/bin/sh\nTCL_VERSIONS=\"8.5 8.6\"\nTCLSH=\"\"\n\nfor VERSION in $TCL_VERSIONS; do\n\tTCL=`which tclsh$VERSION 2>/dev/null` && TCLSH=$TCL\ndone\n\nif [ -z $TCLSH ]\nthen\n    echo \"You need tcl 8.5 or newer in order to run the KeyDB Sentinel test\"\n    exit 1\nfi\n$TCLSH tests/sentinel/run.tcl $*\n"
  },
  {
    "path": "sentinel.conf",
    "content": "# Example sentinel.conf\n\n# *** IMPORTANT ***\n#\n# By default Sentinel will not be reachable from interfaces different than\n# localhost, either use the 'bind' directive to bind to a list of network\n# interfaces, or disable protected mode with \"protected-mode no\" by\n# adding it to this configuration file.\n#\n# Before doing that MAKE SURE the instance is protected from the outside\n# world via firewalling or other means.\n#\n# For example you may use one of the following:\n#\n# bind 127.0.0.1 192.168.1.1\n#\n# protected-mode no\n\n# port <sentinel-port>\n# The port that this sentinel instance will run on\nport 26379\n\n# By default KeyDB Sentinel does not run as a daemon. Use 'yes' if you need it.\n# Note that KeyDB will write a pid file in /var/run/keydb-sentinel.pid when\n# daemonized.\ndaemonize no\n\n# When running daemonized, KeyDB Sentinel writes a pid file in\n# /var/run/keydb-sentinel.pid by default. You can specify a custom pid file\n# location here.\npidfile /var/run/keydb-sentinel.pid\n\n# Specify the log file name. Also the empty string can be used to force\n# Sentinel to log on the standard output. Note that if you use standard\n# output for logging but daemonize, logs will be sent to /dev/null\nlogfile \"\"\n\n# sentinel announce-ip <ip>\n# sentinel announce-port <port>\n#\n# The above two configuration directives are useful in environments where,\n# because of NAT, Sentinel is reachable from outside via a non-local address.\n#\n# When announce-ip is provided, the Sentinel will claim the specified IP address\n# in HELLO messages used to gossip its presence, instead of auto-detecting the\n# local address as it usually does.\n#\n# Similarly when announce-port is provided and is valid and non-zero, Sentinel\n# will announce the specified TCP port.\n#\n# The two options don't need to be used together, if only announce-ip is\n# provided, the Sentinel will announce the specified IP and the server port\n# as specified by the \"port\" option. If only announce-port is provided, the\n# Sentinel will announce the auto-detected local IP and the specified port.\n#\n# Example:\n#\n# sentinel announce-ip 1.2.3.4\n\n# dir <working-directory>\n# Every long running process should have a well-defined working directory.\n# For KeyDB Sentinel to chdir to /tmp at startup is the simplest thing\n# for the process to don't interfere with administrative tasks such as\n# unmounting filesystems.\ndir /tmp\n\n# sentinel monitor <master-name> <ip> <keydb-port> <quorum>\n#\n# Tells Sentinel to monitor this master, and to consider it in O_DOWN\n# (Objectively Down) state only if at least <quorum> sentinels agree.\n#\n# Note that whatever is the ODOWN quorum, a Sentinel will require to\n# be elected by the majority of the known Sentinels in order to\n# start a failover, so no failover can be performed in minority.\n#\n# Replicas are auto-discovered, so you don't need to specify replicas in\n# any way. Sentinel itself will rewrite this configuration file adding\n# the replicas using additional configuration options.\n# Also note that the configuration file is rewritten when a\n# replica is promoted to master.\n#\n# Note: master name should not include special characters or spaces.\n# The valid charset is A-z 0-9 and the three characters \".-_\".\nsentinel monitor mymaster 127.0.0.1 6379 2\n\n# sentinel auth-pass <master-name> <password>\n#\n# Set the password to use to authenticate with the master and replicas.\n# Useful if there is a password set in the KeyDB instances to monitor.\n#\n# Note that the master password is also used for replicas, so it is not\n# possible to set a different password in masters and replicas instances\n# if you want to be able to monitor these instances with Sentinel.\n#\n# However you can have KeyDB instances without the authentication enabled\n# mixed with KeyDB instances requiring the authentication (as long as the\n# password set is the same for all the instances requiring the password) as\n# the AUTH command will have no effect in KeyDB instances with authentication\n# switched off.\n#\n# Example:\n#\n# sentinel auth-pass mymaster MySUPER--secret-0123passw0rd\n\n# sentinel auth-user <master-name> <username>\n#\n# This is useful in order to authenticate to instances having ACL capabilities,\n# that is, running KeyDB 6.0 or greater. When just auth-pass is provided the\n# Sentinel instance will authenticate to KeyDB using the old \"AUTH <pass>\"\n# method. When also an username is provided, it will use \"AUTH <user> <pass>\".\n# In the KeyDB servers side, the ACL to provide just minimal access to\n# Sentinel instances, should be configured along the following lines:\n#\n#     user sentinel-user >somepassword +client +subscribe +publish \\\n#                        +ping +info +multi +slaveof +config +client +exec on\n\n# sentinel down-after-milliseconds <master-name> <milliseconds>\n#\n# Number of milliseconds the master (or any attached replica or sentinel) should\n# be unreachable (as in, not acceptable reply to PING, continuously, for the\n# specified period) in order to consider it in S_DOWN state (Subjectively\n# Down).\n#\n# Default is 30 seconds.\nsentinel down-after-milliseconds mymaster 30000\n\n# IMPORTANT NOTE: starting with KeyDB 6.2 ACL capability is supported for\n# Sentinel mode, please refer to the Redis website https://redis.io/topics/acl\n# for more details.\n\n# Sentinel's ACL users are defined in the following format:\n#\n#   user <username> ... acl rules ...\n#\n# For example:\n#\n#   user worker +@admin +@connection ~* on >ffa9203c493aa99\n#\n# For more information about ACL configuration please refer to the Redis\n# website at https://redis.io/topics/acl and KeyDB server configuration \n# template keydb.conf.\n\n# ACL LOG\n#\n# The ACL Log tracks failed commands and authentication events associated\n# with ACLs. The ACL Log is useful to troubleshoot failed commands blocked \n# by ACLs. The ACL Log is stored in memory. You can reclaim memory with \n# ACL LOG RESET. Define the maximum entry length of the ACL Log below.\nacllog-max-len 128\n\n# Using an external ACL file\n#\n# Instead of configuring users here in this file, it is possible to use\n# a stand-alone file just listing users. The two methods cannot be mixed:\n# if you configure users here and at the same time you activate the external\n# ACL file, the server will refuse to start.\n#\n# The format of the external ACL user file is exactly the same as the\n# format that is used inside keydb.conf to describe users.\n#\n# aclfile /etc/keydb/sentinel-users.acl\n\n# requirepass <password>\n#\n# You can configure Sentinel itself to require a password, however when doing\n# so Sentinel will try to authenticate with the same password to all the\n# other Sentinels. So you need to configure all your Sentinels in a given\n# group with the same \"requirepass\" password. Check the following documentation\n# for more info: https://redis.io/topics/sentinel\n#\n# IMPORTANT NOTE: starting with KeyDB 6.2 \"requirepass\" is a compatibility\n# layer on top of the ACL system. The option effect will be just setting\n# the password for the default user. Clients will still authenticate using\n# AUTH <password> as usually, or more explicitly with AUTH default <password>\n# if they follow the new protocol: both will work.\n#\n# New config files are advised to use separate authentication control for\n# incoming connections (via ACL), and for outgoing connections (via\n# sentinel-user and sentinel-pass) \n#\n# The requirepass is not compatable with aclfile option and the ACL LOAD\n# command, these will cause requirepass to be ignored.\n\n# sentinel sentinel-user <username>\n#\n# You can configure Sentinel to authenticate with other Sentinels with specific\n# user name. \n\n# sentinel sentinel-pass <password>\n#\n# The password for Sentinel to authenticate with other Sentinels. If sentinel-user\n# is not configured, Sentinel will use 'default' user with sentinel-pass to authenticate.\n\n# sentinel parallel-syncs <master-name> <numreplicas>\n#\n# How many replicas we can reconfigure to point to the new replica simultaneously\n# during the failover. Use a low number if you use the replicas to serve query\n# to avoid that all the replicas will be unreachable at about the same\n# time while performing the synchronization with the master.\nsentinel parallel-syncs mymaster 1\n\n# sentinel failover-timeout <master-name> <milliseconds>\n#\n# Specifies the failover timeout in milliseconds. It is used in many ways:\n#\n# - The time needed to re-start a failover after a previous failover was\n#   already tried against the same master by a given Sentinel, is two\n#   times the failover timeout.\n#\n# - The time needed for a replica replicating to a wrong master according\n#   to a Sentinel current configuration, to be forced to replicate\n#   with the right master, is exactly the failover timeout (counting since\n#   the moment a Sentinel detected the misconfiguration).\n#\n# - The time needed to cancel a failover that is already in progress but\n#   did not produced any configuration change (SLAVEOF NO ONE yet not\n#   acknowledged by the promoted replica).\n#\n# - The maximum time a failover in progress waits for all the replicas to be\n#   reconfigured as replicas of the new master. However even after this time\n#   the replicas will be reconfigured by the Sentinels anyway, but not with\n#   the exact parallel-syncs progression as specified.\n#\n# Default is 3 minutes.\nsentinel failover-timeout mymaster 180000\n\n# SCRIPTS EXECUTION\n#\n# sentinel notification-script and sentinel reconfig-script are used in order\n# to configure scripts that are called to notify the system administrator\n# or to reconfigure clients after a failover. The scripts are executed\n# with the following rules for error handling:\n#\n# If script exits with \"1\" the execution is retried later (up to a maximum\n# number of times currently set to 10).\n#\n# If script exits with \"2\" (or an higher value) the script execution is\n# not retried.\n#\n# If script terminates because it receives a signal the behavior is the same\n# as exit code 1.\n#\n# A script has a maximum running time of 60 seconds. After this limit is\n# reached the script is terminated with a SIGKILL and the execution retried.\n\n# NOTIFICATION SCRIPT\n#\n# sentinel notification-script <master-name> <script-path>\n# \n# Call the specified notification script for any sentinel event that is\n# generated in the WARNING level (for instance -sdown, -odown, and so forth).\n# This script should notify the system administrator via email, SMS, or any\n# other messaging system, that there is something wrong with the monitored\n# KeyDB systems.\n#\n# The script is called with just two arguments: the first is the event type\n# and the second the event description.\n#\n# The script must exist and be executable in order for sentinel to start if\n# this option is provided.\n#\n# Example:\n#\n# sentinel notification-script mymaster /var/keydb/notify.sh\n\n# CLIENTS RECONFIGURATION SCRIPT\n#\n# sentinel client-reconfig-script <master-name> <script-path>\n#\n# When the master changed because of a failover a script can be called in\n# order to perform application-specific tasks to notify the clients that the\n# configuration has changed and the master is at a different address.\n# \n# The following arguments are passed to the script:\n#\n# <master-name> <role> <state> <from-ip> <from-port> <to-ip> <to-port>\n#\n# <state> is currently always \"failover\"\n# <role> is either \"leader\" or \"observer\"\n# \n# The arguments from-ip, from-port, to-ip, to-port are used to communicate\n# the old address of the master and the new address of the elected replica\n# (now a master).\n#\n# This script should be resistant to multiple invocations.\n#\n# Example:\n#\n# sentinel client-reconfig-script mymaster /var/keydb/reconfig.sh\n\n# SECURITY\n#\n# By default SENTINEL SET will not be able to change the notification-script\n# and client-reconfig-script at runtime. This avoids a trivial security issue\n# where clients can set the script to anything and trigger a failover in order\n# to get the program executed.\n\nsentinel deny-scripts-reconfig yes\n\n# KEYDB COMMANDS RENAMING\n#\n# Sometimes the KeyDB server has certain commands, that are needed for Sentinel\n# to work correctly, renamed to unguessable strings. This is often the case\n# of CONFIG and SLAVEOF in the context of providers that provide KeyDB as\n# a service, and don't want the customers to reconfigure the instances outside\n# of the administration console.\n#\n# In such case it is possible to tell Sentinel to use different command names\n# instead of the normal ones. For example if the master \"mymaster\", and the\n# associated replicas, have \"CONFIG\" all renamed to \"GUESSME\", I could use:\n#\n# SENTINEL rename-command mymaster CONFIG GUESSME\n#\n# After such configuration is set, every time Sentinel would use CONFIG it will\n# use GUESSME instead. Note that there is no actual need to respect the command\n# case, so writing \"config guessme\" is the same in the example above.\n#\n# SENTINEL SET can also be used in order to perform this configuration at runtime.\n#\n# In order to set a command back to its original name (undo the renaming), it\n# is possible to just rename a command to itself:\n#\n# SENTINEL rename-command mymaster CONFIG CONFIG\n\n# HOSTNAMES SUPPORT\n#\n# Normally Sentinel uses only IP addresses and requires SENTINEL MONITOR\n# to specify an IP address. Also, it requires the KeyDB replica-announce-ip\n# keyword to specify only IP addresses.\n#\n# You may enable hostnames support by enabling resolve-hostnames. Note\n# that you must make sure your DNS is configured properly and that DNS\n# resolution does not introduce very long delays.\n#\nSENTINEL resolve-hostnames no\n\n# When resolve-hostnames is enabled, Sentinel still uses IP addresses\n# when exposing instances to users, configuration files, etc. If you want\n# to retain the hostnames when announced, enable announce-hostnames below.\n#\nSENTINEL announce-hostnames no\n"
  },
  {
    "path": "src/.gitignore",
    "content": "*.gcda\n*.gcno\n*.gcov\nredis.info\nKeyDB.info\nlcov-html\n"
  },
  {
    "path": "src/AsyncWorkQueue.cpp",
    "content": "#include \"AsyncWorkQueue.h\"\n#include \"server.h\"\n\nAsyncWorkQueue::AsyncWorkQueue(int nthreads)\n{\n    for (int i = 0; i < nthreads; ++i)\n    {\n        m_vecthreads.emplace_back([&]{\n            WorkerThreadMain();\n        });\n    }\n}\n\nvoid AsyncWorkQueue::WorkerThreadMain()\n{\n    redisServerThreadVars vars;\n    serverTL = &vars;\n\n    vars.clients_pending_asyncwrite = listCreate();\n\n    m_mutex.lock();\n    m_vecpthreadVars.push_back(&vars);\n    m_mutex.unlock();\n\n    while (!m_fQuitting)\n    {\n        std::unique_lock<std::mutex> lock(m_mutex);\n        if (m_workqueue.empty())\n            m_cvWakeup.wait(lock);\n\n        aeThreadOnline();\n        while (!m_workqueue.empty())\n        {\n            WorkItem task = std::move(m_workqueue.front());\n            m_workqueue.pop_front();\n\n            lock.unlock();\n            serverTL->gcEpoch = g_pserver->garbageCollector.startEpoch();\n            task.fnAsync();\n            g_pserver->garbageCollector.endEpoch(serverTL->gcEpoch);\n            lock.lock();\n        }\n\n        lock.unlock();\n        serverTL->gcEpoch = g_pserver->garbageCollector.startEpoch();\n        if (listLength(serverTL->clients_pending_asyncwrite)) {\n            aeAcquireLock();\n            ProcessPendingAsyncWrites();\n            aeReleaseLock();\n        }\n        g_pserver->garbageCollector.endEpoch(serverTL->gcEpoch);\n        serverTL->gcEpoch.reset();\n        aeThreadOffline();\n    }\n\n    listRelease(vars.clients_pending_asyncwrite);\n\n    std::unique_lock<fastlock> lockf(serverTL->lockPendingWrite);\n    serverTL->vecclientsProcess.clear();\n    serverTL->clients_pending_write.clear();\n    std::atomic_thread_fence(std::memory_order_seq_cst);\n}\n\nbool AsyncWorkQueue::removeClientAsyncWrites(client *c)\n{\n    bool fFound = false;\n    aeAcquireLock();\n    m_mutex.lock();\n    for (auto pvars : m_vecpthreadVars)\n    {\n        listIter li;\n        listNode *ln;\n        listRewind(pvars->clients_pending_asyncwrite, &li);\n        while ((ln = listNext(&li)) != nullptr)\n        {\n            if (c == listNodeValue(ln))\n            {\n                listDelNode(pvars->clients_pending_asyncwrite, ln);\n                fFound = true;\n            }\n        }\n    }\n    m_mutex.unlock();\n    aeReleaseLock();\n    return fFound;\n}\n\nvoid AsyncWorkQueue::shutdown()\n{\n    std::unique_lock<std::mutex> lock(m_mutex);\n    serverAssert(!GlobalLocksAcquired());\n    m_fQuitting = true;\n    m_cvWakeup.notify_all();\n    lock.unlock();\n\n    for (auto &thread : m_vecthreads)\n        thread.join();\n}\n\nvoid AsyncWorkQueue::abandonThreads()\n{\n    std::unique_lock<std::mutex> lock(m_mutex);\n    m_fQuitting = true;\n    m_cvWakeup.notify_all();\n    for (auto &thread : m_vecthreads)\n    {\n        thread.detach();\n    }\n    m_vecthreads.clear();\n}\n\nAsyncWorkQueue::~AsyncWorkQueue()\n{\n    serverAssert(!GlobalLocksAcquired() || m_vecthreads.empty());\n    std::unique_lock<std::mutex> lock(m_mutex);\n    m_fQuitting = true;\n    m_cvWakeup.notify_all();\n    lock.unlock();\n    \n    abandonThreads();\n}\n\nvoid AsyncWorkQueue::AddWorkFunction(std::function<void()> &&fnAsync, bool fHiPri)\n{\n    std::unique_lock<std::mutex> lock(m_mutex);\n    if (fHiPri)\n        m_workqueue.emplace_front(std::move(fnAsync));\n    else\n        m_workqueue.emplace_back(std::move(fnAsync));\n    m_cvWakeup.notify_one();\n}"
  },
  {
    "path": "src/AsyncWorkQueue.h",
    "content": "#pragma once\n#include \"fastlock.h\"\n#include <vector>\n#include <deque>\n#include <mutex>\n#include <thread>\n#include <condition_variable>\n#include <atomic>\n#include <functional>\n\nclass AsyncWorkQueue\n{\n    struct WorkItem\n    {\n        WorkItem(std::function<void()> &&fnAsync)\n            : fnAsync(std::move(fnAsync))\n            {}\n\n        WorkItem(WorkItem&&) = default;\n        std::function<void()> fnAsync;\n    };\n    std::vector<std::thread> m_vecthreads;\n    std::vector<struct redisServerThreadVars*> m_vecpthreadVars;\n    std::deque<WorkItem> m_workqueue;\n    std::mutex m_mutex;\n    std::condition_variable m_cvWakeup;\n    std::atomic<bool> m_fQuitting { false };\n\n    void WorkerThreadMain();\npublic:\n    AsyncWorkQueue(int nthreads);\n    ~AsyncWorkQueue();\n\n    void AddWorkFunction(std::function<void()> &&fnAsync, bool fHiPri = false);\n    bool removeClientAsyncWrites(struct client *c);\n\n    void shutdown();\n\n    void abandonThreads();\n};"
  },
  {
    "path": "src/IStorage.h",
    "content": "#pragma once\n#include <functional>\n#include \"sds.h\"\n#include <string>\n\n#define METADATA_DB_IDENTIFIER \"c299fde0-6d42-4ec4-b939-34f680ffe39f\"\n\nclass IStorageFactory\n{\npublic:\n    typedef void (*key_load_iterator)(const char *rgchKey, size_t cchKey, void *privdata);\n\n    virtual ~IStorageFactory() {}\n    virtual class IStorage *create(int db, key_load_iterator itr, void *privdata) = 0;\n    virtual class IStorage *createMetadataDb() = 0;\n    virtual const char *name() const = 0;\n    virtual size_t totalDiskspaceUsed() const = 0;\n    virtual sdsstring getInfo() const = 0;\n    virtual bool FSlow() const = 0;\n    virtual size_t filedsRequired() const { return 0; }\n};\n\nclass IStorage\n{\npublic:\n    typedef std::function<bool(const char *, size_t, const void *, size_t)> callback;\n    typedef std::function<void(const char *, size_t, const void *, size_t)> callbackSingle;\n\n    virtual ~IStorage();\n\n    virtual void insert(const char *key, size_t cchKey, void *data, size_t cb, bool fOverwire) = 0;\n    virtual bool erase(const char *key, size_t cchKey) = 0;\n    virtual void retrieve(const char *key, size_t cchKey, callbackSingle fn) const = 0;\n    virtual size_t clear() = 0;\n    virtual bool enumerate(callback fn) const = 0;\n    virtual bool enumerate_hashslot(callback fn, unsigned int hashslot) const = 0;\n    virtual size_t count() const = 0;\n\n    virtual void bulkInsert(char **rgkeys, size_t *rgcbkeys, char **rgvals, size_t *rgcbvals, size_t celem) {\n        beginWriteBatch();\n        for (size_t ielem = 0; ielem < celem; ++ielem) {\n            insert(rgkeys[ielem], rgcbkeys[ielem], rgvals[ielem], rgcbvals[ielem], false);\n        }\n        endWriteBatch();\n    }\n\n    virtual std::vector<std::string> getExpirationCandidates(unsigned int count) = 0;\n    virtual std::vector<std::string> getEvictionCandidates(unsigned int count) = 0;\n    virtual void setExpire(const char *key, size_t cchKey, long long expire) = 0;\n    virtual void removeExpire(const char *key, size_t cchKey, long long expire) = 0;\n\n    virtual void beginWriteBatch() {} // NOP\n    virtual void endWriteBatch() {} // NOP\n\n    virtual void batch_lock() {} // NOP\n    virtual void batch_unlock() {} // NOP\n\n    virtual void flush() = 0;\n\n    /* This is permitted to be a shallow clone */\n    virtual const IStorage *clone() const = 0;\n};\n"
  },
  {
    "path": "src/Makefile",
    "content": "# Redis Makefile\n# Copyright (C) 2009 Salvatore Sanfilippo <antirez at gmail dot com>\n# This file is released under the BSD license, see the COPYING file\n#\n# The Makefile composes the final FINAL_CFLAGS and FINAL_LDFLAGS using\n# what is needed for KeyDB plus the standard CFLAGS and LDFLAGS passed.\n# However when building the dependencies (Jemalloc, Lua, Hiredis, ...)\n# CFLAGS and LDFLAGS are propagated to the dependencies, so to pass\n# flags only to be used when compiling / linking KeyDB itself KEYDB_CFLAGS\n# and KEYDB_LDFLAGS are used instead (this is the case of 'make gcov').\n#\n# Dependencies are stored in the Makefile.dep file. To rebuild this file\n# Just use 'make dep', but this is only needed by developers.\n\n# we ship KeyDB with TLS by default\n# export it here as both this file and deps/Makefile uses it\nexport BUILD_TLS ?= yes\n\nrelease_hdr := $(shell sh -c './mkreleasehdr.sh')\nuname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')\nuname_M := $(shell sh -c 'uname -m 2>/dev/null || echo not')\nOPTIMIZATION?=-O2 -flto\nDEPENDENCY_TARGETS=linenoise lua hdr_histogram\nNODEPS:=clean distclean\n\n# Default settings\nSTD=-pedantic -DREDIS_STATIC=''\nCXX_STD=-std=c++17 -pedantic -fno-rtti -D__STDC_FORMAT_MACROS  \nifneq (,$(findstring clang,$(CC)))\n  STD+=-Wno-c11-extensions\nelse\nifneq (,$(findstring FreeBSD,$(uname_S)))\n  STD+=-Wno-c11-extensions\nendif\nendif\nWARN=-Wall -W -Wno-missing-field-initializers -Wno-address-of-packed-member -Wno-atomic-alignment\nOPT=$(OPTIMIZATION)\n\n# Detect if the compiler supports C11 _Atomic\nC11_ATOMIC := $(shell sh -c 'echo \"\\#include <stdatomic.h>\" > foo.c; \\\n\t$(CC) -std=c11 -c foo.c -o foo.o > /dev/null 2>&1; \\\n\tif [ -f foo.o ]; then echo \"yes\"; rm foo.o; fi; rm foo.c')\nifeq ($(C11_ATOMIC),yes)\n\tSTD+=-std=c11\nelse\n\tSTD+=-std=c99\nendif\n\nPREFIX?=/usr/local\nINSTALL_BIN=$(PREFIX)/bin\nINSTALL=install\nPKG_CONFIG?=pkg-config\n\n# Default allocator defaults to Jemalloc if it's not an ARM\nMALLOC=libc\nifneq ($(uname_M),armv6l)\nifneq ($(uname_M),armv7l)\nifeq ($(uname_S),Linux)\n\tMALLOC=jemalloc\nendif\nendif\nendif\n\nUSEASM?=true\nENABLE_FLASH?=no\n\nifneq ($(strip $(SANITIZE)),)\n\tCFLAGS+= -fsanitize=$(SANITIZE) -DSANITIZE  -fno-omit-frame-pointer\n\tCXXFLAGS+= -fsanitize=$(SANITIZE) -DSANITIZE  -fno-omit-frame-pointer\n\tLDFLAGS+= -fsanitize=$(SANITIZE)\n\tMALLOC=libc\n\tUSEASM=false\nendif\n\nifeq ($(ENABLE_FLASH),yes)\n\tSTORAGE_OBJ+= storage/rocksdb.o storage/rocksdbfactory.o\nifeq ($(USE_SYSTEM_ROCKSDB),yes)\n\tFINAL_LIBS+= $(shell pkg-config --libs rocksdb)\n\tFINAL_CXXFLAGS+= $(shell pkg-config --cflags rocksdb) -DENABLE_ROCKSDB\n\nelse\n\tFINAL_LIBS+= -lz -lcrypto -lbz2 -lzstd -llz4 -lsnappy\n\tCXXFLAGS+= -I../deps/rocksdb/include/ -DENABLE_ROCKSDB\n\tFINAL_CXXFLAGS+= -I../deps/rocksdb/include/\n\tFINAL_LIBS+= ../deps/rocksdb/librocksdb.a \n\tDEPENDENCY_TARGETS+= rocksdb\nendif\nendif\n\n\nifeq ($(CHECKED),true)\n\tCXXFLAGS+= -DCHECKED_BUILD\nendif\n\n# Do we use our assembly spinlock? X64 only\nifeq ($(uname_S),Linux)\nifeq ($(uname_M),x86_64)\nifneq ($(TARGET32), true)\nifeq ($(USEASM),true)\n\tASM_OBJ+= fastlock_x64.o\n\tCFLAGS+= -DASM_SPINLOCK\n\tCXXFLAGS+= -DASM_SPINLOCK\nendif\nendif\nendif\nendif\n\nifeq ($(COMPILER_NAME),clang)\n\tCXXFLAGS+= -stdlib=libc++ \nendif\n\n# To get ARM stack traces if KeyDB crashes we need a special C flag.\nifneq (,$(filter aarch64 armv,$(uname_M)))\n        CFLAGS+=-funwind-tables\n\tCXXFLAGS+=-funwind-tables\nelse\nifneq (,$(findstring armv,$(uname_M)))\n        CFLAGS+=-funwind-tables\n\tCXXFLAGS+=-funwind-tables\nendif\nendif\n\n# Backwards compatibility for selecting an allocator\nifeq ($(USE_TCMALLOC),yes)\n\tMALLOC=tcmalloc\nendif\n\nifeq ($(USE_TCMALLOC_MINIMAL),yes)\n\tMALLOC=tcmalloc_minimal\nendif\n\nifeq ($(USE_JEMALLOC),yes)\n\tMALLOC=jemalloc\nendif\n\nifeq ($(USE_JEMALLOC),no)\n\tMALLOC=libc\nendif\n\n\nifeq ($(NO_LICENSE_CHECK),yes)\n\tCXXFLAGS+=-DNO_LICENSE_CHECK=1\nendif\t\n\n# Override default settings if possible\n-include .make-settings\n\nDEBUG=-g -ggdb\nFINAL_CFLAGS=$(STD) $(WARN) $(OPT) $(DEBUG) $(CFLAGS) $(KEYDB_CFLAGS) $(REDIS_CFLAGS)\nFINAL_CXXFLAGS=$(CXX_STD) $(WARN) $(OPT) $(DEBUG) $(CXXFLAGS) $(KEYDB_CFLAGS) $(REDIS_CFLAGS)\nFINAL_LDFLAGS=$(LDFLAGS) $(KEYDB_LDFLAGS) $(DEBUG)\nFINAL_LIBS+=-lm -lz -lcrypto\n\nifneq ($(uname_S),Darwin)\n    ifneq ($(uname_S),FreeBSD)\n\tFINAL_LIBS+=-latomic\n    endif\nendif\n# Linux ARM32 needs -latomic at linking time\nifneq (,$(findstring armv,$(uname_M)))\n\tFINAL_LIBS+=-latomic\nendif\n\n\nifeq ($(uname_S),SunOS)\n\t# SunOS\n\tifeq ($(findstring -m32,$(FINAL_CFLAGS)),)\n\t\tCFLAGS+=-m64\n\t\tCXXFLAGS+= -m64\n\tendif\n\tifeq ($(findstring -m32,$(FINAL_LDFLAGS)),)\n\t\tLDFLAGS+=-m64\n\tendif\n\tDEBUG=-g\n\tDEBUG_FLAGS=-g\n\texport CFLAGS CXXFLAGS LDFLAGS DEBUG DEBUG_FLAGS\n\tINSTALL=cp -pf\n\tFINAL_CFLAGS+= -D__EXTENSIONS__ -D_XPG6\n\tFINAL_CXXFLAGS+= -D__EXTENSIONS__ -D_XPG6\n\tFINAL_LIBS+= -ldl -lnsl -lsocket -lresolv -lpthread -lrt\nelse\nifeq ($(uname_S),Darwin)\n\t# Darwin\n\tFINAL_LIBS+= -ldl\n\t# Homebrew's OpenSSL is not linked to /usr/local to avoid\n\t# conflicts with the system's LibreSSL installation so it\n\t# must be referenced explicitly during build.\nifeq ($(uname_M),arm64)\n\t# Homebrew arm64 uses /opt/homebrew as HOMEBREW_PREFIX\n\tOPENSSL_PREFIX?=/opt/homebrew/opt/openssl\nelse\n\t# Homebrew x86/ppc uses /usr/local as HOMEBREW_PREFIX\n\tOPENSSL_PREFIX?=/usr/local/opt/openssl\nendif\nelse\nifeq ($(uname_S),AIX)\n        # AIX\n        FINAL_LDFLAGS+= -Wl,-bexpall\n        FINAL_LIBS+=-ldl -pthread -lcrypt -lbsd\nelse\nifeq ($(uname_S),OpenBSD)\n\t# OpenBSD\n\tFINAL_LIBS+= -lpthread\n\tifeq ($(USE_BACKTRACE),yes)\n\t    FINAL_CFLAGS+= -DUSE_BACKTRACE -I/usr/local/include\n\t    FINAL_CXXFLAGS+= -DUSE_BACKTRACE -I/usr/local/include\n\t    FINAL_LDFLAGS+= -L/usr/local/lib\n\t    FINAL_LIBS+= -lexecinfo\n\tendif\n\nelse\nifeq ($(uname_S),NetBSD)\n\t# NetBSD\n\tFINAL_LIBS+= -lpthread\n\tifeq ($(USE_BACKTRACE),yes)\n\t    FINAL_CFLAGS+= -DUSE_BACKTRACE -I/usr/pkg/include\n\t    FINAL_LDFLAGS+= -L/usr/pkg/lib\n\t    FINAL_LIBS+= -lexecinfo\n\tendif\nelse\nifeq ($(uname_S),FreeBSD)\n\t# FreeBSD\n\tFINAL_LIBS+= -lpthread -luuid -lexecinfo\n\tFINAL_CFLAGS+= -I/usr/local/include\n\tFINAL_CXXFLAGS+= -I/usr/local/include\n\tFINAL_LDFLAGS+= -L/usr/local/lib\n\tifeq ($(USE_BACKTRACE),yes)\n\t    FINAL_CFLAGS+= -DUSE_BACKTRACE\n\tendif\nelse\nifeq ($(uname_S),DragonFly)\n\t# DragonFly\n\tFINAL_LIBS+= -lpthread -lexecinfo\nelse\nifeq ($(uname_S),OpenBSD)\n\t# OpenBSD\n\tFINAL_LIBS+= -lpthread -lexecinfo\nelse\nifeq ($(uname_S),NetBSD)\n\t# NetBSD\n\tFINAL_LIBS+= -lpthread -lexecinfo\nelse\nifeq ($(uname_S),Haiku)\n\t# Haiku\n\tFINAL_CFLAGS+= -DBSD_SOURCE\n\tFINAL_LDFLAGS+= -lbsd -lnetwork\n\tFINAL_LIBS+= -lpthread\nelse\n\t# All the other OSes (notably Linux)\n\tFINAL_LDFLAGS+= -rdynamic\n\tFINAL_LIBS+=-ldl -pthread -lrt -luuid\nifneq ($(NO_MOTD),yes)\n\tFINAL_CFLAGS += -DMOTD\n\tFINAL_CXXFLAGS += -DMOTD\n\tFINAL_LIBS+=-lcurl\nendif\nendif\nendif\nendif\nendif\nendif\nendif\nendif\nendif\nendif\nendif\n\nifdef OPENSSL_PREFIX\n\tOPENSSL_CFLAGS=-I$(OPENSSL_PREFIX)/include\n\tOPENSSL_CXXFLAGS=-I$(OPENSSL_PREFIX)/include\n\tOPENSSL_LDFLAGS=-L$(OPENSSL_PREFIX)/lib\n\t# Also export OPENSSL_PREFIX so it ends up in deps sub-Makefiles\n\texport OPENSSL_PREFIX\nendif\n\n# Include paths to dependencies\nFINAL_CFLAGS+= -I../deps/linenoise -I../deps/lua/src -I../deps/hdr_histogram\nFINAL_CXXFLAGS+= -I../deps/linenoise -I../deps/lua/src -I../deps/hdr_histogram\n\nifeq ($(USE_SYSTEM_CONCURRENTQUEUE),yes)\n\tFINAL_CXXFLAGS+= -I/usr/include/concurrentqueue/moodycamel\nelse\n\tFINAL_CXXFLAGS+= -I../deps/concurrentqueue\nendif\n\n# Determine systemd support and/or build preference (defaulting to auto-detection)\nBUILD_WITH_SYSTEMD=no\nLIBSYSTEMD_LIBS=-lsystemd\n\n# If 'USE_SYSTEMD' in the environment is neither \"no\" nor \"yes\", try to\n# auto-detect libsystemd's presence and link accordingly.\nifneq ($(USE_SYSTEMD),no)\n\tLIBSYSTEMD_PKGCONFIG := $(shell $(PKG_CONFIG) --exists libsystemd && echo $$?)\n# If libsystemd cannot be detected, continue building without support for it\n# (unless a later check tells us otherwise)\nifeq ($(LIBSYSTEMD_PKGCONFIG),0)\n\tBUILD_WITH_SYSTEMD=yes\n\tLIBSYSTEMD_LIBS=$(shell $(PKG_CONFIG) --libs libsystemd)\nendif\nendif\n\n# If 'USE_SYSTEMD' is set to \"yes\" use pkg-config if available or fall back to\n# default -lsystemd.\nifeq ($(USE_SYSTEMD),yes)\n\tBUILD_WITH_SYSTEMD=yes\nendif\n\nifeq ($(BUILD_WITH_SYSTEMD),yes)\n\tFINAL_LIBS+=$(LIBSYSTEMD_LIBS)\n\tFINAL_CFLAGS+= -DHAVE_LIBSYSTEMD\n\tFINAL_CXXFLAGS+= -DHAVE_LIBSYSTEMD\nendif\n\nifeq ($(MALLOC),tcmalloc)\n\tFINAL_CFLAGS+= -DUSE_TCMALLOC\n\tFINAL_CXXFLAGS+= -DUSE_TCMALLOC\n\tFINAL_LIBS+= -ltcmalloc\nendif\n\nifeq ($(MALLOC),tcmalloc_minimal)\n\tFINAL_CFLAGS+= -DUSE_TCMALLOC\n\tFINAL_CXXFLAGS+= -DUSE_TCMALLOC\n\tFINAL_LIBS+= -ltcmalloc_minimal\nendif\n\nifeq ($(MALLOC),jemalloc)\nifeq ($(USE_SYSTEM_JEMALLOC),yes)\n\tFINAL_CFLAGS+= -DUSE_JEMALLOC $(shell $(PKG_CONFIG) --cflags jemalloc)\n\tFINAL_CXXFLAGS+= -DUSE_JEMALLOC $(shell $(PKG_CONFIG) --cflags jemalloc)\n\tFINAL_LIBS := $(shell $(PKG_CONFIG) --libs jemalloc) $(FINAL_LIBS)\nelse\n\tDEPENDENCY_TARGETS+= jemalloc\n\tFINAL_CFLAGS+= -DUSE_JEMALLOC -I../deps/jemalloc/include\n\tFINAL_CXXFLAGS+= -DUSE_JEMALLOC -I../deps/jemalloc/include\n\tFINAL_LIBS := ../deps/jemalloc/lib/libjemalloc.a $(FINAL_LIBS)\nendif\nendif\n\nifeq ($(MALLOC),memkind)\n\tDEPENDENCY_TARGETS+= memkind\n\tFINAL_CFLAGS+= -DUSE_MEMKIND -I../deps/memkind/src/include\n\tFINAL_CXXFLAGS+= -DUSE_MEMKIND -I../deps/memkind/src/include\n\tFINAL_LIBS := ../deps/memkind/src/.libs/libmemkind.a -lnuma $(FINAL_LIBS)\nendif\n\nifeq ($(USE_SYSTEM_HIREDIS),yes)\n\tHIREDIS_CFLAGS := $(shell $(PKG_CONFIG) --cflags hiredis) -DUSE_SYSTEM_HIREDIS=1\n\tFINAL_CFLAGS+= $(HIREDIS_CFLAGS)\n\tFINAL_CXXFLAGS+= $(HIREDIS_CFLAGS)\n\tFINAL_LIBS+= $(shell $(PKG_CONFIG) --libs hiredis)\nifeq ($(BUILD_TLS),yes)\n\tHIREDIS_TLS_CFLAGS := $(shell $(PKG_CONFIG) --cflags hiredis_ssl)\n\tFINAL_CFLAGS+= $(HIREDIS_TLS_CFLAGS)\n\tFINAL_CXXFLAGS+= $(HIREDIS_TLS_CFLAGS)\n\tFINAL_LIBS+= $(shell $(PKG_CONFIG) --libs hiredis_ssl)\nendif\nelse\n\tDEPENDENCY_TARGETS+= hiredis\n\tFINAL_CFLAGS+= -I../deps/hiredis\n\tFINAL_CXXFLAGS+= -I../deps/hiredis\n\tFINAL_LIBS+=../deps/hiredis/libhiredis.a\nifeq ($(BUILD_TLS),yes)\n\tFINAL_CFLAGS+=-DUSE_OPENSSL $(OPENSSL_CFLAGS)\n\tFINAL_CXXFLAGS+=-DUSE_OPENSSL $(OPENSSL_CXXFLAGS)\n\tFINAL_LDFLAGS+=$(OPENSSL_LDFLAGS)\n\tLIBSSL_PKGCONFIG := $(shell $(PKG_CONFIG) --exists libssl && echo $$?)\nifeq ($(LIBSSL_PKGCONFIG),0)\n\tLIBSSL_LIBS=$(shell $(PKG_CONFIG) --libs libssl)\nelse\n\tLIBSSL_LIBS=-lssl\nendif\n\tLIBCRYPTO_PKGCONFIG := $(shell $(PKG_CONFIG) --exists libcrypto && echo $$?)\nifeq ($(LIBCRYPTO_PKGCONFIG),0)\n\tLIBCRYPTO_LIBS=$(shell $(PKG_CONFIG) --libs libcrypto)\nelse\n\tLIBCRYPTO_LIBS=-lcrypto\nendif\n\tFINAL_LIBS += ../deps/hiredis/libhiredis_ssl.a $(LIBSSL_LIBS) $(LIBCRYPTO_LIBS)\nendif\nendif\n\nifndef V\n    define MAKE_INSTALL\n        @printf '    %b %b\\n' $(LINKCOLOR)INSTALL$(ENDCOLOR) $(BINCOLOR)$(1)$(ENDCOLOR) 1>&2\n        @$(INSTALL) $(1) $(2)\n    endef\nelse\n    define MAKE_INSTALL\n        $(INSTALL) $(1) $(2)\n    endef\nendif\n\n# Alpine OS doesn't have support for the execinfo backtrace library we use for debug, so we provide an alternate implementation using libwunwind.\nOS := $(shell cat /etc/os-release | grep ID= | head -n 1 | cut -d'=' -f2)\nifeq ($(OS),alpine)\n    FINAL_CXXFLAGS+=-DUNW_LOCAL_ONLY\n\tFINAL_CXXFLAGS+=-DALPINE\n    FINAL_LIBS += -lunwind\nendif\n\n\nREDIS_CC=$(QUIET_CC)$(CC) $(FINAL_CFLAGS)\nREDIS_CXX=$(QUIET_CC)$(CXX) $(FINAL_CXXFLAGS)\nKEYDB_AS=$(QUIET_CC) as --64 -g\nREDIS_LD=$(QUIET_LINK)$(CXX) $(FINAL_LDFLAGS)\nREDIS_INSTALL=$(QUIET_INSTALL)$(INSTALL)\n\nCCCOLOR=\"\\033[34m\"\nLINKCOLOR=\"\\033[34;1m\"\nSRCCOLOR=\"\\033[33m\"\nBINCOLOR=\"\\033[37;1m\"\nMAKECOLOR=\"\\033[32;1m\"\nENDCOLOR=\"\\033[0m\"\n\nifndef V\nQUIET_CC = @printf '    %b %b\\n' $(CCCOLOR)CC$(ENDCOLOR) $(SRCCOLOR)$@$(ENDCOLOR);\nQUIET_CP = @printf '    %b %b\\n' $(CCCOLOR)COPY$(ENDCOLOR) $(SRCCOLOR)$@$(ENDCOLOR);\nQUIET_LINK = @printf '    %b %b\\n' $(LINKCOLOR)LINK$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR);\nQUIET_INSTALL = @printf '    %b %b\\n' $(LINKCOLOR)INSTALL$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR);\nendif\n\nREDIS_SERVER_NAME=keydb-server$(PROG_SUFFIX)\nREDIS_SENTINEL_NAME=keydb-sentinel$(PROG_SUFFIX)\nREDIS_SERVER_OBJ=adlist.o quicklist.o ae.o anet.o dict.o server.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o t_nhash.o config.o aof.o pubsub.o multi.o debug.o sort.o intset.o syncio.o cluster.o crc16.o endianconv.o slowlog.o scripting.o bio.o rio.o rand.o memtest.o crcspeed.o crc64.o bitops.o sentinel.o notify.o setproctitle.o blocked.o hyperloglog.o latency.o sparkline.o redis-check-rdb.o redis-check-aof.o geo.o lazyfree.o module.o evict.o expire.o geohash.o geohash_helper.o childinfo.o defrag.o siphash.o rax.o t_stream.o listpack.o localtime.o acl.o storage.o rdb-s3.o fastlock.o new.o tracking.o cron.o connection.o tls.o sha256.o motd_server.o timeout.o setcpuaffinity.o AsyncWorkQueue.o snapshot.o storage/teststorageprovider.o keydbutils.o StorageCache.o monotonic.o cli_common.o mt19937-64.o meminfo.o $(ASM_OBJ) $(STORAGE_OBJ)\nKEYDB_SERVER_OBJ=SnapshotPayloadParseState.o\nREDIS_CLI_NAME=keydb-cli$(PROG_SUFFIX)\nREDIS_CLI_OBJ=anet.o adlist.o dict.o redis-cli.o redis-cli-cpphelper.o zmalloc.o release.o anet.o ae.o crcspeed.o crc64.o siphash.o crc16.o storage-lite.o fastlock.o motd_client.o monotonic.o cli_common.o mt19937-64.o $(ASM_OBJ)\nREDIS_BENCHMARK_NAME=keydb-benchmark$(PROG_SUFFIX)\nREDIS_BENCHMARK_OBJ=ae.o anet.o redis-benchmark.o adlist.o dict.o zmalloc.o release.o crcspeed.o crc64.o siphash.o redis-benchmark.o storage-lite.o fastlock.o new.o monotonic.o cli_common.o mt19937-64.o $(ASM_OBJ)\nREDIS_CHECK_RDB_NAME=keydb-check-rdb$(PROG_SUFFIX)\nREDIS_CHECK_AOF_NAME=keydb-check-aof$(PROG_SUFFIX)\nKEYDB_DIAGNOSTIC_NAME=keydb-diagnostic-tool$(PROG_SUFFIX)\nKEYDB_DIAGNOSTIC_OBJ=ae.o anet.o keydb-diagnostic-tool.o adlist.o dict.o zmalloc.o release.o crcspeed.o crc64.o siphash.o keydb-diagnostic-tool.o storage-lite.o fastlock.o new.o monotonic.o cli_common.o mt19937-64.o $(ASM_OBJ)\n\nall: $(REDIS_SERVER_NAME) $(REDIS_SENTINEL_NAME) $(REDIS_CLI_NAME) $(REDIS_BENCHMARK_NAME) $(REDIS_CHECK_RDB_NAME) $(REDIS_CHECK_AOF_NAME) $(KEYDB_DIAGNOSTIC_NAME)\n\t@echo \"\"\n\t@echo \"Hint: It's a good idea to run 'make test' ;)\"\n\t@echo \"\"\n\nMakefile.dep:\n\t-$(REDIS_CC) -MM *.c > Makefile.dep 2> /dev/null || true\n\nifeq (0, $(words $(findstring $(MAKECMDGOALS), $(NODEPS))))\n-include Makefile.dep\nendif\n\n.PHONY: all\n\npersist-settings: distclean\n\techo STD=$(STD) >> .make-settings\n\techo WARN=$(WARN) >> .make-settings\n\techo OPT=$(OPT) >> .make-settings\n\techo USE_SYSTEM_CONCURRENTQUEUE=$(USE_SYSTEM_CONCURRENTQUEUE) >> .make-settings\n\techo MALLOC=$(MALLOC) >> .make-settings\n\techo USE_SYSTEM_JEMALLOC=$(USE_SYSTEM_JEMALLOC) >> .make-settings\n\techo BUILD_TLS=$(BUILD_TLS) >> .make-settings\n\techo USE_SYSTEMD=$(USE_SYSTEMD) >> .make-settings\n\techo USE_SYSTEM_HIREDIS=$(USE_SYSTEM_HIREDIS) >> .make-settings\n\techo CFLAGS=$(CFLAGS) >> .make-settings\n\techo CXXFLAGS=$(CXXFLAGS) >> .make-settings\n\techo LDFLAGS=$(LDFLAGS) >> .make-settings\n\techo KEYDB_CFLAGS=$(KEYDB_CFLAGS) >> .make-settings\n\techo KEYDB_CXXFLAGS=$(KEYDB_CXXFLAGS) >> .make-settings\n\techo KEYDB_LDFLAGS=$(KEYDB_LDFLAGS) >> .make-settings\n\techo PREV_FINAL_CFLAGS=$(FINAL_CFLAGS) >> .make-settings\n\techo PREV_FINAL_CXXFLAGS=$(FINAL_CXXFLAGS) >> .make-settings\n\techo PREV_FINAL_LDFLAGS=$(FINAL_LDFLAGS) >> .make-settings\n\t-(cd modules && $(MAKE))\n\t-(cd ../deps && $(MAKE) $(DEPENDENCY_TARGETS))\n\n.PHONY: persist-settings\n\n# Prerequisites target\n# Clean everything, persist settings and build dependencies if anything changed\nifneq ($(strip $(PREV_FINAL_CFLAGS)), $(strip $(FINAL_CFLAGS)))\n.make-prerequisites: persist-settings\nelse ifneq ($(strip $(PREV_FINAL_CXXFLAGS)), $(strip $(FINAL_CXXFLAGS)))\n.make-prerequisites: persist-settings\nelse ifneq ($(strip $(PREV_FINAL_LDFLAGS)), $(strip $(FINAL_LDFLAGS)))\n.make-prerequisites: persist-settings\nelse\n.make-prerequisites:\nendif\n\t@touch $@\n\n# keydb-server\n$(REDIS_SERVER_NAME): $(REDIS_SERVER_OBJ) $(KEYDB_SERVER_OBJ)\n\t$(REDIS_LD) -o $@ $^ ../deps/lua/src/liblua.a $(FINAL_LIBS)\n\n# keydb-sentinel\n$(REDIS_SENTINEL_NAME): $(REDIS_SERVER_NAME)\n\t$(REDIS_INSTALL) $(REDIS_SERVER_NAME) $(REDIS_SENTINEL_NAME)\n\n# keydb-check-rdb\n$(REDIS_CHECK_RDB_NAME): $(REDIS_SERVER_NAME)\n\t$(REDIS_INSTALL) $(REDIS_SERVER_NAME) $(REDIS_CHECK_RDB_NAME)\n\n# keydb-check-aof\n$(REDIS_CHECK_AOF_NAME): $(REDIS_SERVER_NAME)\n\t$(REDIS_INSTALL) $(REDIS_SERVER_NAME) $(REDIS_CHECK_AOF_NAME)\n\n# keydb-cli\n$(REDIS_CLI_NAME): $(REDIS_CLI_OBJ)\n\t$(REDIS_LD) -o $@ $^ ../deps/linenoise/linenoise.o $(FINAL_LIBS)\n\n# keydb-benchmark\n$(REDIS_BENCHMARK_NAME): $(REDIS_BENCHMARK_OBJ)\n\t$(REDIS_LD) -o $@ $^ ../deps/hdr_histogram/hdr_histogram.o $(FINAL_LIBS)\n\n# keydb-diagnostic-tool\n$(KEYDB_DIAGNOSTIC_NAME): $(KEYDB_DIAGNOSTIC_OBJ)\n\t$(REDIS_LD) -o $@ $^ $(FINAL_LIBS)\n\nDEP = $(REDIS_SERVER_OBJ:%.o=%.d) $(KEYDB_SERVER_OBJ:%.o=%.d) $(REDIS_CLI_OBJ:%.o=%.d) $(REDIS_BENCHMARK_OBJ:%.o=%.d)\n-include $(DEP)\n\n# Because the jemalloc.h header is generated as a part of the jemalloc build,\n# building it should complete before building any other object. Instead of\n# depending on a single artifact, build all dependencies first.\nmotd_client.o: motd.cpp .make-prerequisites\n\t$(REDIS_CXX) -MMD -o motd_client.o -c $< -DCLIENT -fno-lto\n\nmotd_server.o: motd.cpp .make-prerequisites\n\t$(REDIS_CXX) -MMD -o motd_server.o -c $< -DSERVER\n\n%.o: %.c .make-prerequisites\n\t$(REDIS_CC) -MMD -o $@ -c $<\n\n%.o: %.cpp .make-prerequisites\n\t$(REDIS_CXX) -MMD -o $@ -c $<\n\n%.o: %.asm .make-prerequisites\n\t$(KEYDB_AS) $< -o $@\n\nclean:\n\trm -rf $(REDIS_SERVER_NAME) $(REDIS_SENTINEL_NAME) $(REDIS_CLI_NAME) $(REDIS_BENCHMARK_NAME) $(REDIS_CHECK_RDB_NAME) $(REDIS_CHECK_AOF_NAME) $(KEYDB_DIAGNOSTIC_NAME) *.o *.gcda *.gcno *.gcov KeyDB.info lcov-html Makefile.dep\n\trm -rf storage/*.o\n\trm -rf keydb-server\n\trm -f $(DEP)\n\n.PHONY: clean\n\ndistclean: clean\n\t-(cd ../deps && $(MAKE) distclean)\n\t-(cd modules && $(MAKE) clean)\n\t-(cd ../tests/modules && $(MAKE) clean)\n\t-(rm -f .make-*)\n\n.PHONY: distclean\n\ntest: $(REDIS_SERVER_NAME) $(REDIS_CHECK_AOF_NAME) $(REDIS_CLI_NAME) $(REDIS_BENCHMARK_NAME)\n\t@(cd ..; ./runtest)\n\ntest-modules: $(REDIS_SERVER_NAME)\n\t@(cd ..; ./runtest-moduleapi)\n\ntest-sentinel: $(REDIS_SENTINEL_NAME) $(REDIS_CLI_NAME)\n\t@(cd ..; ./runtest-sentinel)\n\ncheck: test\n\nlcov:\n\t$(MAKE) gcov\n\t@(set -e; cd ..; ./runtest --config server-threads 3; ./runtest-sentinel; ./runtest-cluster; ./runtest-moduleapi)\n\t@geninfo -o KeyDB.info --no-external .\n\t@genhtml --legend -o lcov-html KeyDB.info\n\t@genhtml --legend -o lcov-html KeyDB.info | grep lines | awk '{print $$2;}' | sed 's/%//g'\n\n.PHONY: lcov\n\nbench: $(REDIS_BENCHMARK_NAME)\n\t./$(REDIS_BENCHMARK_NAME)\n\n32bit:\n\t@echo \"\"\n\t@echo \"WARNING: if it fails under Linux you probably need to install libc6-dev-i386\"\n\t@echo \"\"\n\t$(MAKE) CXXFLAGS=\"-m32\" CFLAGS=\"-m32\" LDFLAGS=\"-m32\" TARGET32=true\n\ngcov:\n\t$(MAKE) KEYDB_CXXFLAGS=\"-fprofile-arcs -ftest-coverage -DCOVERAGE_TEST\" KEYDB_CFLAGS=\"-fprofile-arcs -ftest-coverage -DCOVERAGE_TEST\" KEYDB_LDFLAGS=\"-fprofile-arcs -ftest-coverage\"\n\nnoopt:\n\t$(MAKE) OPTIMIZATION=\"-O0\"\n\nvalgrind:\n\t$(MAKE) OPTIMIZATION=\"-O0\" USEASM=\"false\" MALLOC=\"libc\" CFLAGS=\"-DSANITIZE\" CXXFLAGS=\"-DSANITIZE\"\n\nhelgrind:\n\t$(MAKE) OPTIMIZATION=\"-O0\" MALLOC=\"libc\" CFLAGS=\"-D__ATOMIC_VAR_FORCE_SYNC_MACROS\" KEYDB_CFLAGS=\"-I/usr/local/include\" KEYDB_LDFLAGS=\"-L/usr/local/lib\"\n\nsrc/help.h:\n\t@../utils/generate-command-help.rb > help.h\n\ninstall: all\n\t@mkdir -p $(INSTALL_BIN)\n\t$(call MAKE_INSTALL,$(REDIS_SERVER_NAME),$(INSTALL_BIN))\n\t$(call MAKE_INSTALL,$(REDIS_BENCHMARK_NAME),$(INSTALL_BIN))\n\t$(call MAKE_INSTALL,$(REDIS_CLI_NAME),$(INSTALL_BIN))\n\t@ln -sf $(REDIS_SERVER_NAME) $(INSTALL_BIN)/$(REDIS_CHECK_RDB_NAME)\n\t@ln -sf $(REDIS_SERVER_NAME) $(INSTALL_BIN)/$(REDIS_CHECK_AOF_NAME)\n\t@ln -sf $(REDIS_SERVER_NAME) $(INSTALL_BIN)/$(REDIS_SENTINEL_NAME)\n\nuninstall:\n\trm -f $(INSTALL_BIN)/{$(REDIS_SERVER_NAME),$(REDIS_BENCHMARK_NAME),$(REDIS_CLI_NAME),$(REDIS_CHECK_RDB_NAME),$(REDIS_CHECK_AOF_NAME),$(REDIS_SENTINEL_NAME),$(KEYDB_DIAGNOSTIC_NAME)}\n"
  },
  {
    "path": "src/SnapshotPayloadParseState.cpp",
    "content": "#include \"server.h\"\n#include \"SnapshotPayloadParseState.h\"\n#include <sys/mman.h>\n\nstatic const size_t SLAB_SIZE = 2*1024*1024;   // Note 64 MB because we try to give 64MB to a block\nclass SlabAllocator\n{\n    class Slab {\n        char *m_pv = nullptr;\n        size_t m_cb = 0;\n        size_t m_cbAllocated = 0;\n\n    public:\n        Slab(size_t cbAllocate) {\n            m_pv = (char*)zmalloc(cbAllocate);\n            m_cb = cbAllocate;\n        }\n\n        ~Slab() {\n            zfree(m_pv);\n        }\n\n        Slab(Slab &&other) {\n            m_pv = other.m_pv;\n            m_cb = other.m_cb;\n            m_cbAllocated = other.m_cbAllocated;\n            other.m_pv = nullptr;\n            other.m_cb = 0;\n            other.m_cbAllocated = 0;\n        }\n\n        bool canAllocate(size_t cb) const {\n            return (m_cbAllocated + cb) <= m_cb;\n        }\n\n        void *allocate(size_t cb) {\n            if ((m_cbAllocated + cb) > m_cb)\n                return nullptr;\n            void *pvret = m_pv + m_cbAllocated;\n            m_cbAllocated += cb;\n            return pvret;\n        }\n    };\n    std::vector<Slab> m_vecslabs;\npublic:\n\n    void *allocate(size_t cb) {\n        if (m_vecslabs.empty() || !m_vecslabs.back().canAllocate(cb)) {\n            m_vecslabs.emplace_back(std::max(cb, SLAB_SIZE));\n        }\n        return m_vecslabs.back().allocate(cb);\n    }\n};\n\nstatic uint64_t dictCStringHash(const void *key) {\n    return dictGenHashFunction((unsigned char*)key, strlen((char*)key));\n}\n\n\nstatic void dictKeyDestructor(void *privdata, void *key)\n{\n    DICT_NOTUSED(privdata);\n    sdsfree((sds)key);\n}\n\nstatic int dictCStringCompare(void *, const void *key1, const void *key2)\n{\n    int l1,l2;\n\n    l1 = strlen((sds)key1);\n    l2 = strlen((sds)key2);\n    if (l1 != l2) return 0;\n    return memcmp(key1, key2, l1) == 0;\n}\n\ndictType metadataLongLongDictType = {\n    dictCStringHash,            /* hash function */\n    NULL,                       /* key dup */\n    NULL,                       /* val dup */\n    dictCStringCompare,         /* key compare */\n    dictKeyDestructor,          /* key destructor */\n    nullptr,                    /* val destructor */\n    nullptr,                    /* allow to expand */\n    nullptr                     /* async free destructor */\n};\n\ndictType metadataDictType = {\n    dictSdsHash,            /* hash function */\n    NULL,                       /* key dup */\n    NULL,                       /* val dup */\n    dictSdsKeyCompare,         /* key compare */\n    dictSdsDestructor,          /* key destructor */\n    dictSdsDestructor,          /* val destructor */\n    nullptr,                    /* allow to expand */\n    nullptr                     /* async free destructor */\n};\n\n\nSnapshotPayloadParseState::ParseStageName SnapshotPayloadParseState::getNextStage() {\n    if (stackParse.empty())\n        return ParseStageName::Global;\n\n    switch (stackParse.back().name)\n    {\n        case ParseStageName::None:\n            return ParseStageName::Global;\n\n        case ParseStageName::Global:\n            if (stackParse.back().arraycur == 0)\n                return ParseStageName::MetaData;\n            else if (stackParse.back().arraycur == 1)\n                return ParseStageName::Databases;\n            break;\n\n        case ParseStageName::MetaData:\n            return ParseStageName::KeyValuePair;\n\n        case ParseStageName::Databases:\n            return ParseStageName::Dataset;\n\n        case ParseStageName::Dataset:\n            return ParseStageName::KeyValuePair;\n\n        default:\n            break;\n    }\n    throw \"Bad protocol: corrupt state\";\n}\n\nvoid SnapshotPayloadParseState::flushQueuedKeys() {\n    if (vecqueuedKeys.empty())\n        return;\n    serverAssert(current_database >= 0);\n\n    // TODO: We can't finish parse until all the work functions finish\n    int idb = current_database;\n    serverAssert(vecqueuedKeys.size() == vecqueuedVals.size());\n    auto sizePrev = vecqueuedKeys.size();\n    if (current_database < cserver.dbnum) {\n        if (g_pserver->m_pstorageFactory) {\n            (*insertsInFlight)++;\n            std::weak_ptr<std::atomic<int>> insertsInFlightTmp = insertsInFlight; // C++ GRRRRRRRRRRRRRRRR, we don't want to capute \"this\" because that's dangerous\n            g_pserver->asyncworkqueue->AddWorkFunction([idb, vecqueuedKeys = std::move(this->vecqueuedKeys), vecqueuedKeysCb = std::move(this->vecqueuedKeysCb), vecqueuedVals = std::move(this->vecqueuedVals), vecqueuedValsCb = std::move(this->vecqueuedValsCb), insertsInFlightTmp, pallocator = m_spallocator.release()]() mutable {\n                g_pserver->db[idb]->bulkDirectStorageInsert(vecqueuedKeys.data(), vecqueuedKeysCb.data(), vecqueuedVals.data(), vecqueuedValsCb.data(), vecqueuedKeys.size());\n                (*(insertsInFlightTmp.lock()))--;\n                delete pallocator;\n            });\n        } else {\n            for (size_t ival = 0; ival < vecqueuedKeys.size(); ++ival) {\n                size_t offset = 0;\n                auto spexpire = deserializeExpire(vecqueuedVals[ival], vecqueuedValsCb[ival], &offset);    \n                auto o = deserializeStoredObject(vecqueuedVals[ival] + offset, vecqueuedValsCb[ival] - offset);\n                sds sdsKey = sdsnewlen(vecqueuedKeys[ival], -static_cast<ssize_t>(vecqueuedKeysCb[ival]));\n                if (dbMerge(g_pserver->db[idb], sdsKey, o, false)) {\n                    if (spexpire != nullptr)\n                        g_pserver->db[idb]->setExpire(sdsKey, std::move(*spexpire));\n                } else {\n                    sdsfree(sdsKey);\n                }\n            }\n            vecqueuedKeys.clear();\n            vecqueuedKeysCb.clear();\n            vecqueuedVals.clear();\n            vecqueuedValsCb.clear();\n        }\n    } else {\n        // else drop the data\n        vecqueuedKeys.clear();\n        vecqueuedKeysCb.clear();\n        vecqueuedVals.clear();\n        vecqueuedValsCb.clear();\n        // Note: m_spallocator will get free'd when overwritten below\n    }\n    m_spallocator = std::make_unique<SlabAllocator>();\n    cbQueued = 0;\n    vecqueuedKeys.reserve(sizePrev);\n    vecqueuedKeysCb.reserve(sizePrev);\n    vecqueuedVals.reserve(sizePrev);\n    vecqueuedValsCb.reserve(sizePrev);\n    serverAssert(vecqueuedKeys.empty());\n    serverAssert(vecqueuedVals.empty());\n}\n\n\nSnapshotPayloadParseState::SnapshotPayloadParseState() {\n    // This is to represent the first array the client is intended to send us\n    ParseStage stage;\n    stage.name = ParseStageName::None;\n    stage.arraylen = 1;\n    stackParse.push_back(stage);\n\n    dictLongLongMetaData = dictCreate(&metadataLongLongDictType, nullptr);\n    dictMetaData = dictCreate(&metadataDictType, nullptr);\n    insertsInFlight = std::make_shared<std::atomic<int>>();\n    m_spallocator = std::make_unique<SlabAllocator>();\n}\n\nSnapshotPayloadParseState::~SnapshotPayloadParseState() {\n    dictRelease(dictLongLongMetaData);\n    dictRelease(dictMetaData);\n}\n\nconst char *SnapshotPayloadParseState::getStateDebugName(ParseStage stage) {\n    switch (stage.name) {\n        case ParseStageName::None:\n            return \"None\";\n\n        case ParseStageName::Global:\n            return \"Global\";\n\n        case ParseStageName::MetaData:\n            return \"MetaData\";\n\n        case ParseStageName::Databases:\n            return \"Databases\";\n\n        case ParseStageName::KeyValuePair:\n            return \"KeyValuePair\";\n\n        case ParseStageName::Dataset:\n            return \"Dataset\";\n\n        default:\n            return \"Unknown\";\n    }\n}\n\nsize_t SnapshotPayloadParseState::depth() const { return stackParse.size(); }\n\nvoid SnapshotPayloadParseState::trimState() {\n    while (!stackParse.empty() && (stackParse.back().arraycur == stackParse.back().arraylen))\n        stackParse.pop_back();\n    \n    if (stackParse.empty()) {\n        flushQueuedKeys();\n        while (*insertsInFlight > 0) {\n            // TODO: ProcessEventsWhileBlocked\n            aeReleaseLock();\n            aeAcquireLock();\n        }\n    }\n}\n\nvoid SnapshotPayloadParseState::pushArray(long long size) {\n    if (stackParse.empty())\n        throw \"Bad Protocol: unexpected trailing data\";\n\n    if (size == 0) {\n        stackParse.back().arraycur++;\n        return;\n    }\n    \n    if (stackParse.back().name == ParseStageName::Databases) {\n        flushQueuedKeys();\n        current_database = stackParse.back().arraycur;\n    }\n\n    ParseStage stage;\n    stage.name = getNextStage();\n    stage.arraylen = size;\n\n    if (stage.name == ParseStageName::Dataset) {\n        g_pserver->db[current_database]->expand(stage.arraylen);\n    }\n\n    // Note: This affects getNextStage so ensure its after\n    stackParse.back().arraycur++;\n    stackParse.push_back(stage);\n}\n\nvoid SnapshotPayloadParseState::pushValue(const char *rgch, long long cch) {\n    if (stackParse.empty())\n        throw \"Bad Protocol: unexpected trailing bulk string\";\n\n    if (stackParse.back().arraycur >= static_cast<int>(stackParse.back().arrvalues.size()))\n        throw \"Bad protocol: Unexpected value\";\n\n    auto &stage = stackParse.back();\n    stage.arrvalues[stackParse.back().arraycur].first = (char*)m_spallocator->allocate(cch);\n    stage.arrvalues[stackParse.back().arraycur].second = cch;\n    memcpy(stage.arrvalues[stackParse.back().arraycur].first, rgch, cch);\n    stage.arraycur++;\n    switch (stage.name) {\n        case ParseStageName::KeyValuePair:\n            if (stackParse.size() < 2)\n                throw \"Bad Protocol: unexpected bulk string\";\n            if (stackParse[stackParse.size()-2].name == ParseStageName::MetaData) {\n                if (stage.arraycur == 2) {\n                    // We loaded both pairs\n                    if (stage.arrvalues[0].first == nullptr || stage.arrvalues[1].first == nullptr)\n                        throw \"Bad Protocol: Got array when expecing a string\"; // A baddy could make us derefence the vector when its too small\n\n                    if (!strcasecmp(stage.arrvalues[0].first, \"lua\")) {\n                        /* Load the script back in memory. */\n                        robj *auxval = createStringObject(stage.arrvalues[1].first, stage.arrvalues[1].second);\n                        if (luaCreateFunction(NULL,g_pserver->lua,auxval) == NULL) {\n                            throw \"Can't load Lua script\";\n                        }\n                    } else {\n                        dictAdd(dictMetaData, sdsnewlen(stage.arrvalues[0].first, stage.arrvalues[0].second), sdsnewlen(stage.arrvalues[1].first, stage.arrvalues[1].second));\n                    }\n                }\n            } else if (stackParse[stackParse.size()-2].name == ParseStageName::Dataset) {\n                if (stage.arraycur == 2) {\n                    // We loaded both pairs\n                    if (stage.arrvalues[0].first == nullptr || stage.arrvalues[1].first == nullptr)\n                        throw \"Bad Protocol: Got array when expecing a string\"; // A baddy could make us derefence the vector when its too small\n                    vecqueuedKeys.push_back(stage.arrvalues[0].first);\n                    vecqueuedKeysCb.push_back(stage.arrvalues[0].second);\n                    vecqueuedVals.push_back(stage.arrvalues[1].first);\n                    vecqueuedValsCb.push_back(stage.arrvalues[1].second);\n                    stage.arrvalues[0].first = nullptr;\n                    stage.arrvalues[1].first = nullptr;\n                    cbQueued += vecqueuedKeysCb.back();\n                    cbQueued += vecqueuedValsCb.back();\n                    if (cbQueued >= queuedBatchLimit)\n                        flushQueuedKeys();\n                }\n            } else {\n                throw \"Bad Protocol: unexpected bulk string\";\n            }\n            break;\n\n        default:\n            throw \"Bad Protocol: unexpected bulk string out of KV pair\";\n    }\n}\n\nvoid SnapshotPayloadParseState::pushValue(long long value) {\n    if (stackParse.empty())\n        throw \"Bad Protocol: unexpected integer value\";\n    \n    stackParse.back().arraycur++;\n\n    if (stackParse.back().arraycur != 2 || stackParse.back().arrvalues[0].first == nullptr)\n        throw \"Bad Protocol: unexpected integer value\";\n\n    dictEntry *de = dictAddRaw(dictLongLongMetaData, sdsnewlen(stackParse.back().arrvalues[0].first, stackParse.back().arrvalues[0].second), nullptr);\n    if (de == nullptr)\n        throw \"Bad Protocol: metadata field sent twice\";\n    de->v.s64 = value;\n}\n\nlong long SnapshotPayloadParseState::getMetaDataLongLong(const char *szField) const {\n    dictEntry *de = dictFind(dictLongLongMetaData, szField);\n\n    if (de == nullptr) {\n        serverLog(LL_WARNING, \"Master did not send field: %s\", szField);\n        throw false;\n    }\n    return de->v.s64;\n}\n\nsds SnapshotPayloadParseState::getMetaDataStr(const char *szField) const {\n    sdsstring str(szField, strlen(szField));\n\n    dictEntry *de = dictFind(dictMetaData, str.get());\n    if (de == nullptr) {\n        serverLog(LL_WARNING, \"Master did not send field: %s\", szField);\n        throw false;\n    }\n    return (sds)de->v.val;\n}"
  },
  {
    "path": "src/SnapshotPayloadParseState.h",
    "content": "#pragma once\n#include <stack>\n#include <vector>\n\nclass SlabAllocator;\n\nclass SnapshotPayloadParseState {\n    enum class ParseStageName {\n        None,\n        Global,\n        MetaData,\n        Databases,\n        Dataset,\n        KeyValuePair,\n    };\n    struct ParseStage {\n        ParseStageName name;\n        long long arraylen = 0;\n        long long arraycur = 0;\n        std::array<std::pair<char*, size_t>, 2> arrvalues;\n\n        ParseStage() {\n            for (auto &pair : arrvalues) {\n                pair.first = nullptr;\n                pair.second = 0;\n            }\n        }\n    };\n\n    std::vector<ParseStage> stackParse;\n    \n    \n    std::vector<char*> vecqueuedKeys;\n    std::vector<size_t> vecqueuedKeysCb;\n    std::vector<char*> vecqueuedVals;\n    std::vector<size_t> vecqueuedValsCb;\n    \n    \n    std::shared_ptr<std::atomic<int>> insertsInFlight;\n    std::unique_ptr<SlabAllocator> m_spallocator;\n    dict *dictLongLongMetaData = nullptr;\n    dict *dictMetaData = nullptr;\n    size_t cbQueued = 0;\n    static const size_t queuedBatchLimit = 64*1024*1024;    // 64 MB\n    int current_database = -1;\n\n    ParseStageName getNextStage();\n    void flushQueuedKeys();\n\npublic:\n    SnapshotPayloadParseState();\n    ~SnapshotPayloadParseState();\n\n    long long getMetaDataLongLong(const char *field) const;\n    sds getMetaDataStr(const char *szField) const;\n\n    static const char *getStateDebugName(ParseStage stage);\n\n    size_t depth() const;\n\n    void trimState();\n    void pushArray(long long size);\n    void pushValue(const char *rgch, long long cch);\n    void pushValue(long long value);\n    bool shouldThrottle() const { return *insertsInFlight > (cserver.cthreads*4); }\n};"
  },
  {
    "path": "src/StorageCache.cpp",
    "content": "#include \"server.h\"\n\nuint64_t hashPassthrough(const void *hash) {\n    return static_cast<uint64_t>(reinterpret_cast<uintptr_t>(hash));\n}\n\nint hashCompare(void *, const void *key1, const void *key2) {\n    auto diff = (reinterpret_cast<uintptr_t>(key1) - reinterpret_cast<uintptr_t>(key2));\n    return !diff;\n}\n\ndictType dbStorageCacheType = {\n    hashPassthrough,            /* hash function */\n    NULL,                       /* key dup */\n    NULL,                       /* val dup */\n    hashCompare,                /* key compare */\n    NULL,                       /* key destructor */\n    NULL                        /* val destructor */\n};\n\nStorageCache::StorageCache(IStorage *storage, bool fCache)\n        : m_spstorage(storage)\n{\n    if (!g_pserver->flash_disable_key_cache && fCache)\n        m_pdict = dictCreate(&dbStorageCacheType, nullptr);\n}\n\nStorageCache::~StorageCache()\n{\n    if (m_pdict != nullptr)\n        dictRelease(m_pdict);\n}\n\nvoid StorageCache::clear(void(callback)(void*))\n{\n    std::unique_lock<fastlock> ul(m_lock);\n    if (m_pdict != nullptr)\n        dictEmpty(m_pdict, callback);\n    m_spstorage->clear();\n    m_collisionCount = 0;\n}\n\nvoid StorageCache::clearAsync()\n{\n    std::unique_lock<fastlock> ul(m_lock);\n    if (count() == 0)\n        return;\n    if (m_pdict != nullptr) {\n        dict *dSav = m_pdict;\n        m_pdict = dictCreate(&dbStorageCacheType, nullptr);\n        g_pserver->asyncworkqueue->AddWorkFunction([dSav]{\n            dictEmpty(dSav, nullptr);\n        });\n    }\n    m_spstorage->clear();\n    m_collisionCount = 0;\n}\n\nvoid StorageCache::cacheKey(sds key)\n{\n    if (m_pdict == nullptr)\n        return;\n    uintptr_t hash = dictSdsHash(key);\n    if (dictAdd(m_pdict, reinterpret_cast<void*>(hash), (void*)1) != DICT_OK) {\n        dictEntry *de = dictFind(m_pdict, reinterpret_cast<void*>(hash));\n        serverAssert(de != nullptr);\n        de->v.s64++;\n        m_collisionCount++;\n    }\n}\n\nvoid StorageCache::cacheKey(const char *rgch, size_t cch)\n{\n    if (m_pdict == nullptr)\n        return;\n    uintptr_t hash = dictGenHashFunction(rgch, (int)cch);\n    if (dictAdd(m_pdict, reinterpret_cast<void*>(hash), (void*)1) != DICT_OK) {\n        dictEntry *de = dictFind(m_pdict, reinterpret_cast<void*>(hash));\n        serverAssert(de != nullptr);\n        de->v.s64++;\n        m_collisionCount++;\n    }\n}\n\nbool StorageCache::erase(sds key)\n{\n    unsigned long long when = 0;\n    m_spstorage->retrieve(key, sdslen(key), [&when](const char *, size_t, const void * data, size_t cbdata) {\n        auto e = deserializeExpire((const char *)data, cbdata, nullptr);\n        if (e != nullptr)\n            when = e->when();\n    });\n    bool result = m_spstorage->erase(key, sdslen(key));\n    std::unique_lock<fastlock> ul(m_lock);\n    if (result)\n    {\n        if (m_pdict != nullptr)\n        {\n            uint64_t hash = dictSdsHash(key);\n            dictEntry *de = dictFind(m_pdict, reinterpret_cast<void*>(hash));\n            serverAssert(de != nullptr);\n            de->v.s64--;\n            serverAssert(de->v.s64 >= 0);\n            if (de->v.s64 == 0) {\n                dictDelete(m_pdict, reinterpret_cast<void*>(hash));\n            } else {\n                m_collisionCount--;\n            }\n        }\n        if (when != 0) {\n            m_spstorage->removeExpire(key, sdslen(key), when);\n        }\n    }\n    return result;\n}\n\nvoid StorageCache::insert(sds key, const void *data, size_t cbdata, bool fOverwrite)\n{\n    std::unique_lock<fastlock> ul(m_lock);\n    if (!fOverwrite && m_pdict != nullptr)\n    {\n        cacheKey(key);\n    }\n    ul.unlock();\n    m_spstorage->insert(key, sdslen(key), (void*)data, cbdata, fOverwrite);\n    auto e = deserializeExpire((const char *)data, cbdata, nullptr);\n    if (e != nullptr)\n        m_spstorage->setExpire(key, sdslen(key), e->when());\n}\n\nlong _dictKeyIndex(dict *d, const void *key, uint64_t hash, dictEntry **existing);\nvoid StorageCache::bulkInsert(char **rgkeys, size_t *rgcbkeys, char **rgvals, size_t *rgcbvals, size_t celem)\n{\n    std::vector<dictEntry*> vechashes;\n    if (m_pdict != nullptr) {\n        vechashes.reserve(celem);\n    }\n\n    for (size_t ielem = 0; ielem < celem; ++ielem) {\n        if (m_pdict != nullptr) {\n            dictEntry *de = (dictEntry*)zmalloc(sizeof(dictEntry));\n            de->key = (void*)dictGenHashFunction(rgkeys[ielem], (int)rgcbkeys[ielem]);\n            de->v.u64 = 1;\n            vechashes.push_back(de);\n        }\n        auto e = deserializeExpire(rgvals[ielem], rgcbvals[ielem], nullptr);\n        if (e != nullptr)\n            m_spstorage->setExpire(rgkeys[ielem], rgcbkeys[ielem], e->when());\n    }\n\n    std::unique_lock<fastlock> ul(m_lock);\n    bulkInsertsInProgress++;\n    if (m_pdict != nullptr) {\n        for (dictEntry *de : vechashes) {\n            if (dictIsRehashing(m_pdict)) dictRehash(m_pdict,1);\n            /* Get the index of the new element, or -1 if\n                * the element already exists. */\n            long index;\n            if ((index = _dictKeyIndex(m_pdict, de->key, (uint64_t)de->key, nullptr)) == -1) {\n                dictEntry *deLocal = dictFind(m_pdict, de->key);\n                serverAssert(deLocal != nullptr);\n                deLocal->v.s64++;\n                m_collisionCount++;\n                zfree(de);\n            } else {\n                int iht = dictIsRehashing(m_pdict) ? 1 : 0;\n                de->next = m_pdict->ht[iht].table[index];\n                m_pdict->ht[iht].table[index] = de;\n                m_pdict->ht[iht].used++;\n            }\n        }\n    }\n    ul.unlock();\n\n    m_spstorage->bulkInsert(rgkeys, rgcbkeys, rgvals, rgcbvals, celem);\n\n    bulkInsertsInProgress--;\n}\n\nconst StorageCache *StorageCache::clone()\n{\n    std::unique_lock<fastlock> ul(m_lock);\n    // Clones never clone the cache\n    StorageCache *cacheNew = new StorageCache(const_cast<IStorage*>(m_spstorage->clone()), false /*fCache*/);\n    return cacheNew;\n}\n\nvoid StorageCache::expand(uint64_t slots)\n{\n    std::unique_lock<fastlock> ul(m_lock);\n    if (m_pdict) {\n        dictExpand(m_pdict, slots);\n    }\n}\n\nvoid StorageCache::retrieve(sds key, IStorage::callbackSingle fn) const\n{\n    std::unique_lock<fastlock> ul(m_lock);\n    if (m_pdict != nullptr)\n    {\n        uint64_t hash = dictSdsHash(key);\n        dictEntry *de = dictFind(m_pdict, reinterpret_cast<void*>(hash));\n        \n        if (de == nullptr)\n            return; // Not found\n    }\n    ul.unlock();\n    m_spstorage->retrieve(key, sdslen(key), fn);\n}\n\nsize_t StorageCache::count() const\n{\n    std::unique_lock<fastlock> ul(m_lock, std::defer_lock);\n    bool fLocked = ul.try_lock();\n    size_t count = m_spstorage->count();\n    if (m_pdict != nullptr && fLocked) {\n        serverAssert(bulkInsertsInProgress.load(std::memory_order_seq_cst) || count == (dictSize(m_pdict) + m_collisionCount));\n    }\n    return count;\n}\n\nvoid StorageCache::beginWriteBatch() { \n    serverAssert(GlobalLocksAcquired());    // Otherwise we deadlock\n    m_spstorage->beginWriteBatch(); \n}\n\nvoid StorageCache::emergencyFreeCache() {\n    std::unique_lock<fastlock> ul(m_lock);\n    dict *d = m_pdict;\n    m_pdict = nullptr;\n    if (d != nullptr) {\n        g_pserver->asyncworkqueue->AddWorkFunction([d]{\n            dictRelease(d);\n        });\n    }\n}"
  },
  {
    "path": "src/StorageCache.h",
    "content": "#pragma once\n#include \"sds.h\"\n\nclass StorageCache\n{\n    std::shared_ptr<IStorage> m_spstorage;\n    dict *m_pdict = nullptr;\n    int m_collisionCount = 0;\n    mutable fastlock m_lock {\"StorageCache\"};\n    std::atomic<int> bulkInsertsInProgress;\n\n    StorageCache(IStorage *storage, bool fNoCache);\n\n    void cacheKey(sds key);\n    void cacheKey(const char *rgchKey, size_t cchKey);\n\n    struct load_iter_data\n    {\n        StorageCache *cache;\n        IStorageFactory::key_load_iterator itrParent;\n        void *privdataParent;\n    };\n    static void key_load_itr(const char *rgchKey, size_t cchKey, void *privdata)\n    {\n        load_iter_data *data = (load_iter_data*)privdata;\n        data->cache->cacheKey(rgchKey, cchKey);\n        if (data->itrParent)\n            data->itrParent(rgchKey, cchKey, data->privdataParent);\n    }\n\npublic:\n    ~StorageCache();\n\n    static StorageCache *create(IStorageFactory *pfactory, int db, IStorageFactory::key_load_iterator fn, void *privdata) {\n        StorageCache *cache = new StorageCache(nullptr, pfactory->FSlow() /*fCache*/);\n        load_iter_data data = {cache, fn, privdata};\n        cache->m_spstorage = std::shared_ptr<IStorage>(pfactory->create(db, key_load_itr, (void*)&data));\n        return cache;\n    }\n\n    void clear(void(callback)(void*));\n    void clearAsync();\n    void insert(sds key, const void *data, size_t cbdata, bool fOverwrite);\n    void bulkInsert(char **rgkeys, size_t *rgcbkeys, char **rgvals, size_t *rgcbvals, size_t celem);\n    void retrieve(sds key, IStorage::callbackSingle fn) const;\n    bool erase(sds key);\n    void emergencyFreeCache();\n    bool keycacheIsEnabled() const { return m_pdict != nullptr; }\n    void expand(uint64_t slots);\n\n    bool enumerate(IStorage::callback fn) const { return m_spstorage->enumerate(fn); }\n    bool enumerate_hashslot(IStorage::callback fn, unsigned int hashslot) const { return m_spstorage->enumerate_hashslot(fn, hashslot); }\n\n    std::vector<std::string> getExpirationCandidates(unsigned int count) { return m_spstorage->getExpirationCandidates(count); }\n    std::vector<std::string> getEvictionCandidates(unsigned int count) { return m_spstorage->getEvictionCandidates(count); }\n    void setExpire(const char *key, size_t cchKey, long long expire) { m_spstorage->setExpire(key, cchKey, expire); }\n    void removeExpire(const char *key, size_t cchKey, long long expire) { m_spstorage->removeExpire(key, cchKey, expire); }\n\n    void beginWriteBatch();\n    void endWriteBatch() { m_spstorage->endWriteBatch(); }\n    void batch_lock() { return m_spstorage->batch_lock(); }\n    void batch_unlock() { return m_spstorage->batch_unlock(); }\n\n    void flush() { m_spstorage->flush(); }\n\n    size_t count() const;\n\n    const StorageCache *clone();\n};\n"
  },
  {
    "path": "src/acl.cpp",
    "content": "/*\n * Copyright (c) 2018, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\nextern \"C\" {\n#include \"sha256.h\"\n}\n#include <fcntl.h>\n#include <ctype.h>\n\n/* =============================================================================\n * Global state for ACLs\n * ==========================================================================*/\n\nrax *Users; /* Table mapping usernames to user structures. */\n\nuser *DefaultUser;  /* Global reference to the default user.\n                       Every new connection is associated to it, if no\n                       AUTH or HELLO is used to authenticate with a\n                       different user. */\n\nlist *UsersToLoad;  /* This is a list of users found in the configuration file\n                       that we'll need to load in the final stage of Redis\n                       initialization, after all the modules are already\n                       loaded. Every list element is a NULL terminated\n                       array of SDS pointers: the first is the user name,\n                       all the remaining pointers are ACL rules in the same\n                       format as ACLSetUser(). */\nlist *ACLLog;       /* Our security log, the user is able to inspect that\n                       using the ACL LOG command .*/\n\nstatic rax *commandId = NULL; /* Command name to id mapping */\n\nstatic unsigned long nextid = 0; /* Next command id that has not been assigned */\n\nstruct ACLCategoryItem {\n    const char *name;\n    uint64_t flag;\n} ACLCommandCategories[] = {\n    {\"keyspace\", CMD_CATEGORY_KEYSPACE},\n    {\"read\", CMD_CATEGORY_READ},\n    {\"write\", CMD_CATEGORY_WRITE},\n    {\"set\", CMD_CATEGORY_SET},\n    {\"sortedset\", CMD_CATEGORY_SORTEDSET},\n    {\"list\", CMD_CATEGORY_LIST},\n    {\"hash\", CMD_CATEGORY_HASH},\n    {\"string\", CMD_CATEGORY_STRING},\n    {\"bitmap\", CMD_CATEGORY_BITMAP},\n    {\"hyperloglog\", CMD_CATEGORY_HYPERLOGLOG},\n    {\"geo\", CMD_CATEGORY_GEO},\n    {\"stream\", CMD_CATEGORY_STREAM},\n    {\"pubsub\", CMD_CATEGORY_PUBSUB},\n    {\"admin\", CMD_CATEGORY_ADMIN},\n    {\"fast\", CMD_CATEGORY_FAST},\n    {\"slow\", CMD_CATEGORY_SLOW},\n    {\"blocking\", CMD_CATEGORY_BLOCKING},\n    {\"dangerous\", CMD_CATEGORY_DANGEROUS},\n    {\"connection\", CMD_CATEGORY_CONNECTION},\n    {\"transaction\", CMD_CATEGORY_TRANSACTION},\n    {\"scripting\", CMD_CATEGORY_SCRIPTING},\n    {\"replication\", CMD_CATEGORY_REPLICATION},\n    {NULL,0} /* Terminator. */\n};\n\nstruct ACLUserFlag {\n    const char *name;\n    uint64_t flag;\n} ACLUserFlags[] = {\n    /* Note: the order here dictates the emitted order at ACLDescribeUser */\n    {\"on\", USER_FLAG_ENABLED},\n    {\"off\", USER_FLAG_DISABLED},\n    {\"allkeys\", USER_FLAG_ALLKEYS},\n    {\"allchannels\", USER_FLAG_ALLCHANNELS},\n    {\"allcommands\", USER_FLAG_ALLCOMMANDS},\n    {\"nopass\", USER_FLAG_NOPASS},\n    {\"skip-sanitize-payload\", USER_FLAG_SANITIZE_PAYLOAD_SKIP},\n    {\"sanitize-payload\", USER_FLAG_SANITIZE_PAYLOAD},\n    {NULL,0} /* Terminator. */\n};\n\nvoid ACLResetSubcommandsForCommand(user *u, unsigned long id);\nvoid ACLResetSubcommands(user *u);\nvoid ACLAddAllowedSubcommand(user *u, unsigned long id, const char *sub);\nvoid ACLFreeLogEntry(const void *le);\n\n/* The length of the string representation of a hashed password. */\n#define HASH_PASSWORD_LEN SHA256_BLOCK_SIZE*2\n\n/* =============================================================================\n * Helper functions for the rest of the ACL implementation\n * ==========================================================================*/\n\n/* Return zero if strings are the same, non-zero if they are not.\n * The comparison is performed in a way that prevents an attacker to obtain\n * information about the nature of the strings just monitoring the execution\n * time of the function.\n *\n * Note that limiting the comparison length to strings up to 512 bytes we\n * can avoid leaking any information about the password length and any\n * possible branch misprediction related leak.\n */\nint time_independent_strcmp(char *a, char *b) {\n    char bufa[CONFIG_AUTHPASS_MAX_LEN], bufb[CONFIG_AUTHPASS_MAX_LEN];\n    /* The above two strlen perform len(a) + len(b) operations where either\n     * a or b are fixed (our password) length, and the difference is only\n     * relative to the length of the user provided string, so no information\n     * leak is possible in the following two lines of code. */\n    unsigned int alen = strlen(a);\n    unsigned int blen = strlen(b);\n    unsigned int j;\n    int diff = 0;\n\n    /* We can't compare strings longer than our static buffers.\n     * Note that this will never pass the first test in practical circumstances\n     * so there is no info leak. */\n    if (alen > sizeof(bufa) || blen > sizeof(bufb)) return 1;\n\n    memset(bufa,0,sizeof(bufa));        /* Constant time. */\n    memset(bufb,0,sizeof(bufb));        /* Constant time. */\n    /* Again the time of the following two copies is proportional to\n     * len(a) + len(b) so no info is leaked. */\n    memcpy(bufa,a,alen);\n    memcpy(bufb,b,blen);\n\n    /* Always compare all the chars in the two buffers without\n     * conditional expressions. */\n    for (j = 0; j < sizeof(bufa); j++) {\n        diff |= (bufa[j] ^ bufb[j]);\n    }\n    /* Length must be equal as well. */\n    diff |= alen ^ blen;\n    return diff; /* If zero strings are the same. */\n}\n\n/* Given an SDS string, returns the SHA256 hex representation as a\n * new SDS string. */\nsds ACLHashPassword(unsigned char *cleartext, size_t len) {\n    SHA256_CTX ctx;\n    unsigned char hash[SHA256_BLOCK_SIZE];\n    char hex[HASH_PASSWORD_LEN];\n    const char *cset = \"0123456789abcdef\";\n\n    sha256_init(&ctx);\n    sha256_update(&ctx,(unsigned char*)cleartext,len);\n    sha256_final(&ctx,hash);\n\n    for (int j = 0; j < SHA256_BLOCK_SIZE; j++) {\n        hex[j*2] = cset[((hash[j]&0xF0)>>4)];\n        hex[j*2+1] = cset[(hash[j]&0xF)];\n    }\n    return sdsnewlen(hex,HASH_PASSWORD_LEN);\n}\n\n/* Given a hash and the hash length, returns C_OK if it is a valid password\n * hash, or C_ERR otherwise. */\nint ACLCheckPasswordHash(unsigned char *hash, int hashlen) {\n    if (hashlen != HASH_PASSWORD_LEN) {\n        return C_ERR;\n    }\n\n    /* Password hashes can only be characters that represent\n     * hexadecimal values, which are numbers and lowercase\n     * characters 'a' through 'f'. */\n    for(int i = 0; i < HASH_PASSWORD_LEN; i++) {\n        char c = hash[i];\n        if ((c < 'a' || c > 'f') && (c < '0' || c > '9')) {\n            return C_ERR;\n        }\n    }\n    return C_OK;\n}\n\n/* =============================================================================\n * Low level ACL API\n * ==========================================================================*/\n\n/* Return 1 if the specified string contains spaces or null characters.\n * We do this for usernames and key patterns for simpler rewriting of\n * ACL rules, presentation on ACL list, and to avoid subtle security bugs\n * that may arise from parsing the rules in presence of escapes.\n * The function returns 0 if the string has no spaces. */\nint ACLStringHasSpaces(const char *s, size_t len) {\n    for (size_t i = 0; i < len; i++) {\n        if (isspace(s[i]) || s[i] == 0) return 1;\n    }\n    return 0;\n}\n\n/* Given the category name the command returns the corresponding flag, or\n * zero if there is no match. */\nuint64_t ACLGetCommandCategoryFlagByName(const char *name) {\n    for (int j = 0; ACLCommandCategories[j].flag != 0; j++) {\n        if (!strcasecmp(name,ACLCommandCategories[j].name)) {\n            return ACLCommandCategories[j].flag;\n        }\n    }\n    return 0; /* No match. */\n}\n\n/* Method for pattern comparison used for the user->patterns list\n * so that we can search for items with listSearchKey(). */\nint ACLListMatchSds(void *a, void *b) {\n    return sdscmp((sds)a,(sds)b) == 0;\n}\n\n/* Method for password comparison used for the user->passwords list\n * Like the above, but uses a time independant compare for security reasons */\nint ACLListMatchSdsSecure(void *a, void* b) {\n    return time_independent_strcmp((sds)a,(sds)b) == 0;\n}\n\n/* Method to free list elements from ACL users password/patterns lists. */\nvoid ACLListFreeSds(const void *item) {\n    sdsfree((sds)item);\n}\n\n/* Method to duplicate list elements from ACL users password/patterns lists. */\nvoid *ACLListDupSds(void *item) {\n    return sdsdup((sds)item);\n}\n\n/* Create a new user with the specified name, store it in the list\n * of users (the Users global radix tree), and returns a reference to\n * the structure representing the user.\n *\n * If the user with such name already exists NULL is returned. */\nuser *ACLCreateUser(const char *name, size_t namelen) {\n    if (raxFind(Users,(unsigned char*)name,namelen) != raxNotFound) return NULL;\n    user *u = (user*)zmalloc(sizeof(*u), MALLOC_LOCAL);\n    u->name = sdsnewlen(name,namelen);\n    u->flags = USER_FLAG_DISABLED | g_pserver->acl_pubsub_default;\n    u->allowed_subcommands = NULL;\n    u->passwords = listCreate();\n    u->patterns = listCreate();\n    u->channels = listCreate();\n    listSetMatchMethod(u->passwords,ACLListMatchSdsSecure);\n    listSetFreeMethod(u->passwords,ACLListFreeSds);\n    listSetDupMethod(u->passwords,ACLListDupSds);\n    listSetMatchMethod(u->patterns,ACLListMatchSds);\n    listSetFreeMethod(u->patterns,ACLListFreeSds);\n    listSetDupMethod(u->patterns,ACLListDupSds);\n    listSetMatchMethod(u->channels,ACLListMatchSds);\n    listSetFreeMethod(u->channels,ACLListFreeSds);\n    listSetDupMethod(u->channels,ACLListDupSds);\n    memset(u->allowed_commands,0,sizeof(u->allowed_commands));\n    raxInsert(Users,(unsigned char*)name,namelen,u,NULL);\n    return u;\n}\n\n/* This function should be called when we need an unlinked \"fake\" user\n * we can use in order to validate ACL rules or for other similar reasons.\n * The user will not get linked to the Users radix tree. The returned\n * user should be released with ACLFreeUser() as usually. */\nuser *ACLCreateUnlinkedUser(void) {\n    char username[64];\n    for (int j = 0; ; j++) {\n        snprintf(username,sizeof(username),\"__fakeuser:%d__\",j);\n        user *fakeuser = ACLCreateUser(username,strlen(username));\n        if (fakeuser == NULL) continue;\n        int retval = raxRemove(Users,(unsigned char*) username,\n                               strlen(username),NULL);\n        serverAssert(retval != 0);\n        return fakeuser;\n    }\n}\n\n/* Release the memory used by the user structure. Note that this function\n * will not remove the user from the Users global radix tree. */\nvoid ACLFreeUser(user *u) {\n    sdsfree(u->name);\n    listRelease(u->passwords);\n    listRelease(u->patterns);\n    listRelease(u->channels);\n    ACLResetSubcommands(u);\n    zfree(u);\n}\n\n/* When a user is deleted we need to cycle the active\n * connections in order to kill all the pending ones that\n * are authenticated with such user. */\nvoid ACLFreeUserAndKillClients(user *u) {\n    listIter li;\n    listNode *ln;\n    listRewind(g_pserver->clients,&li);\n    while ((ln = listNext(&li)) != NULL) {\n        client *c = (client*)listNodeValue(ln);\n        if (c->user == u) {\n            /* We'll free the connection asynchronously, so\n             * in theory to set a different user is not needed.\n             * However if there are bugs in Redis, soon or later\n             * this may result in some security hole: it's much\n             * more defensive to set the default user and put\n             * it in non authenticated mode. */\n            c->user = DefaultUser;\n            c->authenticated = 0;\n            /* We will write replies to this client later, so we can't\n             * close it directly even if async. */\n            if (c == serverTL->current_client) {\n                c->flags |= CLIENT_CLOSE_AFTER_COMMAND;\n            } else {\n                freeClientAsync(c);\n            }\n        }\n    }\n    ACLFreeUser(u);\n}\n\n/* Copy the user ACL rules from the source user 'src' to the destination\n * user 'dst' so that at the end of the process they'll have exactly the\n * same rules (but the names will continue to be the original ones). */\nvoid ACLCopyUser(user *dst, user *src) {\n    listRelease(dst->passwords);\n    listRelease(dst->patterns);\n    listRelease(dst->channels);\n    dst->passwords = listDup(src->passwords);\n    dst->patterns = listDup(src->patterns);\n    dst->channels = listDup(src->channels);\n    memcpy(dst->allowed_commands,src->allowed_commands,\n           sizeof(dst->allowed_commands));\n    dst->flags = src->flags;\n    ACLResetSubcommands(dst);\n    /* Copy the allowed subcommands array of array of SDS strings. */\n    if (src->allowed_subcommands) {\n        for (int j = 0; j < USER_COMMAND_BITS_COUNT; j++) {\n            if (src->allowed_subcommands[j]) {\n                for (int i = 0; src->allowed_subcommands[j][i]; i++)\n                {\n                    ACLAddAllowedSubcommand(dst, j,\n                        src->allowed_subcommands[j][i]);\n                }\n            }\n        }\n    }\n}\n\n/* Free all the users registered in the radix tree 'users' and free the\n * radix tree itself. */\nvoid ACLFreeUsersSet(rax *users) {\n    raxFreeWithCallback(users,(void(*)(void*))ACLFreeUserAndKillClients);\n}\n\n/* Given a command ID, this function set by reference 'word' and 'bit'\n * so that user->allowed_commands[word] will address the right word\n * where the corresponding bit for the provided ID is stored, and\n * so that user->allowed_commands[word]&bit will identify that specific\n * bit. The function returns C_ERR in case the specified ID overflows\n * the bitmap in the user representation. */\nint ACLGetCommandBitCoordinates(uint64_t id, uint64_t *word, uint64_t *bit) {\n    if (id >= USER_COMMAND_BITS_COUNT) return C_ERR;\n    *word = id / sizeof(uint64_t) / 8;\n    *bit = 1ULL << (id % (sizeof(uint64_t) * 8));\n    return C_OK;\n}\n\n/* Check if the specified command bit is set for the specified user.\n * The function returns 1 is the bit is set or 0 if it is not.\n * Note that this function does not check the ALLCOMMANDS flag of the user\n * but just the lowlevel bitmask.\n *\n * If the bit overflows the user internal representation, zero is returned\n * in order to disallow the execution of the command in such edge case. */\nint ACLGetUserCommandBit(user *u, unsigned long id) {\n    uint64_t word, bit;\n    if (ACLGetCommandBitCoordinates(id,&word,&bit) == C_ERR) return 0;\n    return (u->allowed_commands[word] & bit) != 0;\n}\n\n/* When +@all or allcommands is given, we set a reserved bit as well that we\n * can later test, to see if the user has the right to execute \"future commands\",\n * that is, commands loaded later via modules. */\nint ACLUserCanExecuteFutureCommands(user *u) {\n    return ACLGetUserCommandBit(u,USER_COMMAND_BITS_COUNT-1);\n}\n\n/* Set the specified command bit for the specified user to 'value' (0 or 1).\n * If the bit overflows the user internal representation, no operation\n * is performed. As a side effect of calling this function with a value of\n * zero, the user flag ALLCOMMANDS is cleared since it is no longer possible\n * to skip the command bit explicit test. */\nvoid ACLSetUserCommandBit(user *u, unsigned long id, int value) {\n    uint64_t word=0, bit=0;\n    if (ACLGetCommandBitCoordinates(id,&word,&bit) == C_ERR) return;\n    if (value) {\n        u->allowed_commands[word] |= bit;\n    } else {\n        u->allowed_commands[word] &= ~bit;\n        u->flags &= ~USER_FLAG_ALLCOMMANDS;\n    }\n}\n\n/* This is like ACLSetUserCommandBit(), but instead of setting the specified\n * ID, it will check all the commands in the category specified as argument,\n * and will set all the bits corresponding to such commands to the specified\n * value. Since the category passed by the user may be non existing, the\n * function returns C_ERR if the category was not found, or C_OK if it was\n * found and the operation was performed. */\nint ACLSetUserCommandBitsForCategory(user *u, const char *category, int value) {\n    uint64_t cflag = ACLGetCommandCategoryFlagByName(category);\n    if (!cflag) return C_ERR;\n    dictIterator *di = dictGetIterator(g_pserver->orig_commands);\n    dictEntry *de;\n    while ((de = dictNext(di)) != NULL) {\n        struct redisCommand *cmd = (redisCommand*)dictGetVal(de);\n        if (cmd->flags & CMD_MODULE) continue; /* Ignore modules commands. */\n        if (cmd->flags & cflag) {\n            ACLSetUserCommandBit(u,cmd->id,value);\n            ACLResetSubcommandsForCommand(u,cmd->id);\n        }\n    }\n    dictReleaseIterator(di);\n    return C_OK;\n}\n\n/* Return the number of commands allowed (on) and denied (off) for the user 'u'\n * in the subset of commands flagged with the specified category name.\n * If the category name is not valid, C_ERR is returned, otherwise C_OK is\n * returned and on and off are populated by reference. */\nint ACLCountCategoryBitsForUser(user *u, unsigned long *on, unsigned long *off,\n                                const char *category)\n{\n    uint64_t cflag = ACLGetCommandCategoryFlagByName(category);\n    if (!cflag) return C_ERR;\n\n    *on = *off = 0;\n    dictIterator *di = dictGetIterator(g_pserver->orig_commands);\n    dictEntry *de;\n    while ((de = dictNext(di)) != NULL) {\n        struct redisCommand *cmd = (redisCommand*)dictGetVal(de);\n        if (cmd->flags & cflag) {\n            if (ACLGetUserCommandBit(u,cmd->id))\n                (*on)++;\n            else\n                (*off)++;\n        }\n    }\n    dictReleaseIterator(di);\n    return C_OK;\n}\n\n/* This function returns an SDS string representing the specified user ACL\n * rules related to command execution, in the same format you could set them\n * back using ACL SETUSER. The function will return just the set of rules needed\n * to recreate the user commands bitmap, without including other user flags such\n * as on/off, passwords and so forth. The returned string always starts with\n * the +@all or -@all rule, depending on the user bitmap, and is followed, if\n * needed, by the other rules needed to narrow or extend what the user can do. */\nsds ACLDescribeUserCommandRules(user *u) {\n    sds rules = sdsempty();\n    int additive;   /* If true we start from -@all and add, otherwise if\n                       false we start from +@all and remove. */\n\n    /* This code is based on a trick: as we generate the rules, we apply\n     * them to a fake user, so that as we go we still know what are the\n     * bit differences we should try to address by emitting more rules. */\n    user fu = {0};\n    user *fakeuser = &fu;\n\n    /* Here we want to understand if we should start with +@all and remove\n     * the commands corresponding to the bits that are not set in the user\n     * commands bitmap, or the contrary. Note that semantically the two are\n     * different. For instance starting with +@all and subtracting, the user\n     * will be able to execute future commands, while -@all and adding will just\n     * allow the user the run the selected commands and/or categories.\n     * How do we test for that? We use the trick of a reserved command ID bit\n     * that is set only by +@all (and its alias \"allcommands\"). */\n    if (ACLUserCanExecuteFutureCommands(u)) {\n        additive = 0;\n        rules = sdscat(rules,\"+@all \");\n        ACLSetUser(fakeuser,\"+@all\",-1);\n    } else {\n        additive = 1;\n        rules = sdscat(rules,\"-@all \");\n        ACLSetUser(fakeuser,\"-@all\",-1);\n    }\n\n    /* Attempt to find a good approximation for categories and commands\n     * based on the current bits used, by looping over the category list\n     * and applying the best fit each time. Often a set of categories will not \n     * perfectly match the set of commands into it, so at the end we do a \n     * final pass adding/removing the single commands needed to make the bitmap\n     * exactly match. A temp user is maintained to keep track of categories \n     * already applied. */\n    user tu = {0};\n    user *tempuser = &tu;\n    \n    /* Keep track of the categories that have been applied, to prevent\n     * applying them twice.  */\n    char applied[sizeof(ACLCommandCategories)/sizeof(ACLCommandCategories[0])];\n    memset(applied, 0, sizeof(applied));\n\n    memcpy(tempuser->allowed_commands,\n        u->allowed_commands, \n        sizeof(u->allowed_commands));\n    while (1) {\n        int best = -1;\n        unsigned long mindiff = INT_MAX, maxsame = 0;\n        for (int j = 0; ACLCommandCategories[j].flag != 0; j++) {\n            if (applied[j]) continue;\n\n            unsigned long on, off, diff, same;\n            ACLCountCategoryBitsForUser(tempuser,&on,&off,ACLCommandCategories[j].name);\n            /* Check if the current category is the best this loop:\n             * * It has more commands in common with the user than commands\n             *   that are different.\n             * AND EITHER\n             * * It has the fewest number of differences\n             *    than the best match we have found so far. \n             * * OR it matches the fewest number of differences\n             *   that we've seen but it has more in common. */\n            diff = additive ? off : on;\n            same = additive ? on : off;\n            if (same > diff && \n                ((diff < mindiff) || (diff == mindiff && same > maxsame)))\n            {\n                best = j;\n                mindiff = diff;\n                maxsame = same;\n            }\n        }\n\n        /* We didn't find a match */\n        if (best == -1) break;\n\n        sds op = sdsnewlen(additive ? \"+@\" : \"-@\", 2);\n        op = sdscat(op,ACLCommandCategories[best].name);\n        ACLSetUser(fakeuser,op,-1);\n\n        sds invop = sdsnewlen(additive ? \"-@\" : \"+@\", 2);\n        invop = sdscat(invop,ACLCommandCategories[best].name);\n        ACLSetUser(tempuser,invop,-1);\n\n        rules = sdscatsds(rules,op);\n        rules = sdscatlen(rules,\" \",1);\n        sdsfree(op);\n        sdsfree(invop);\n\n        applied[best] = 1;\n    }\n\n    /* Fix the final ACLs with single commands differences. */\n    dictIterator *di = dictGetIterator(g_pserver->orig_commands);\n    dictEntry *de;\n    while ((de = dictNext(di)) != NULL) {\n        struct redisCommand *cmd = (redisCommand*)dictGetVal(de);\n        int userbit = ACLGetUserCommandBit(u,cmd->id);\n        int fakebit = ACLGetUserCommandBit(fakeuser,cmd->id);\n        if (userbit != fakebit) {\n            rules = sdscatlen(rules, userbit ? \"+\" : \"-\", 1);\n            rules = sdscat(rules,cmd->name);\n            rules = sdscatlen(rules,\" \",1);\n            ACLSetUserCommandBit(fakeuser,cmd->id,userbit);\n        }\n\n        /* Emit the subcommands if there are any. */\n        if (userbit == 0 && u->allowed_subcommands &&\n            u->allowed_subcommands[cmd->id])\n        {\n            for (int j = 0; u->allowed_subcommands[cmd->id][j]; j++) {\n                rules = sdscatlen(rules,\"+\",1);\n                rules = sdscat(rules,cmd->name);\n                rules = sdscatlen(rules,\"|\",1);\n                rules = sdscatsds(rules,u->allowed_subcommands[cmd->id][j]);\n                rules = sdscatlen(rules,\" \",1);\n            }\n        }\n    }\n    dictReleaseIterator(di);\n\n    /* Trim the final useless space. */\n    sdsrange(rules,0,-2);\n\n    /* This is technically not needed, but we want to verify that now the\n     * predicted bitmap is exactly the same as the user bitmap, and abort\n     * otherwise, because aborting is better than a security risk in this\n     * code path. */\n    if (memcmp(fakeuser->allowed_commands,\n                        u->allowed_commands,\n                        sizeof(u->allowed_commands)) != 0)\n    {\n        serverLog(LL_WARNING,\n            \"CRITICAL ERROR: User ACLs don't match final bitmap: '%s'\",\n            rules);\n        serverPanic(\"No bitmap match in ACLDescribeUserCommandRules()\");\n    }\n    return rules;\n}\n\n/* This is similar to ACLDescribeUserCommandRules(), however instead of\n * describing just the user command rules, everything is described: user\n * flags, keys, passwords and finally the command rules obtained via\n * the ACLDescribeUserCommandRules() function. This is the function we call\n * when we want to rewrite the configuration files describing ACLs and\n * in order to show users with ACL LIST. */\nsds ACLDescribeUser(user *u) {\n    sds res = sdsempty();\n\n    /* Flags. */\n    for (int j = 0; ACLUserFlags[j].flag; j++) {\n        /* Skip the allcommands, allkeys and allchannels flags because they'll\n         * be emitted later as +@all, ~* and &*. */\n        if (ACLUserFlags[j].flag == USER_FLAG_ALLKEYS ||\n            ACLUserFlags[j].flag == USER_FLAG_ALLCHANNELS ||\n            ACLUserFlags[j].flag == USER_FLAG_ALLCOMMANDS) continue;\n        if (u->flags & ACLUserFlags[j].flag) {\n            res = sdscat(res,ACLUserFlags[j].name);\n            res = sdscatlen(res,\" \",1);\n        }\n    }\n\n    /* Passwords. */\n    listIter li;\n    listNode *ln;\n    listRewind(u->passwords,&li);\n    while((ln = listNext(&li))) {\n        sds thispass = (sds)listNodeValue(ln);\n        res = sdscatlen(res,\"#\",1);\n        res = sdscatsds(res,thispass);\n        res = sdscatlen(res,\" \",1);\n    }\n\n    /* Key patterns. */\n    if (u->flags & USER_FLAG_ALLKEYS) {\n        res = sdscatlen(res,\"~* \",3);\n    } else {\n        listRewind(u->patterns,&li);\n        while((ln = listNext(&li))) {\n            sds thispat = (sds)listNodeValue(ln);\n            res = sdscatlen(res,\"~\",1);\n            res = sdscatsds(res,thispat);\n            res = sdscatlen(res,\" \",1);\n        }\n    }\n\n    /* Pub/sub channel patterns. */\n    if (u->flags & USER_FLAG_ALLCHANNELS) {\n        res = sdscatlen(res,\"&* \",3);\n    } else {\n        res = sdscatlen(res,\"resetchannels \",14);\n        listRewind(u->channels,&li);\n        while((ln = listNext(&li))) {\n            sds thispat = (sds)listNodeValue(ln);\n            res = sdscatlen(res,\"&\",1);\n            res = sdscatsds(res,thispat);\n            res = sdscatlen(res,\" \",1);\n        }\n    }\n\n    /* Command rules. */\n    sds rules = ACLDescribeUserCommandRules(u);\n    res = sdscatsds(res,rules);\n    sdsfree(rules);\n    return res;\n}\n\n/* Get a command from the original command table, that is not affected\n * by the command renaming operations: we base all the ACL work from that\n * table, so that ACLs are valid regardless of command renaming. */\nstruct redisCommand *ACLLookupCommand(const char *name) {\n    struct redisCommand *cmd;\n    sds sdsname = sdsnew(name);\n    cmd = (redisCommand*)dictFetchValue(g_pserver->orig_commands, sdsname);\n    sdsfree(sdsname);\n    return cmd;\n}\n\n/* Flush the array of allowed subcommands for the specified user\n * and command ID. */\nvoid ACLResetSubcommandsForCommand(user *u, unsigned long id) {\n    if (u->allowed_subcommands && u->allowed_subcommands[id]) {\n        for (int i = 0; u->allowed_subcommands[id][i]; i++)\n            sdsfree(u->allowed_subcommands[id][i]);\n        zfree(u->allowed_subcommands[id]);\n        u->allowed_subcommands[id] = NULL;\n    }\n}\n\n/* Flush the entire table of subcommands. This is useful on +@all, -@all\n * or similar to return back to the minimal memory usage (and checks to do)\n * for the user. */\nvoid ACLResetSubcommands(user *u) {\n    if (u->allowed_subcommands == NULL) return;\n    for (int j = 0; j < USER_COMMAND_BITS_COUNT; j++) {\n        if (u->allowed_subcommands[j]) {\n            for (int i = 0; u->allowed_subcommands[j][i]; i++)\n                sdsfree(u->allowed_subcommands[j][i]);\n            zfree(u->allowed_subcommands[j]);\n        }\n    }\n    zfree(u->allowed_subcommands);\n    u->allowed_subcommands = NULL;\n}\n\n/* Add a subcommand to the list of subcommands for the user 'u' and\n * the command id specified. */\nvoid ACLAddAllowedSubcommand(user *u, unsigned long id, const char *sub) {\n    /* If this is the first subcommand to be configured for\n     * this user, we have to allocate the subcommands array. */\n    if (u->allowed_subcommands == NULL) {\n        u->allowed_subcommands = (sds**)zcalloc(USER_COMMAND_BITS_COUNT *\n                                 sizeof(sds*), MALLOC_LOCAL);\n    }\n\n    /* We also need to enlarge the allocation pointing to the\n     * null terminated SDS array, to make space for this one.\n     * To start check the current size, and while we are here\n     * make sure the subcommand is not already specified inside. */\n    long items = 0;\n    if (u->allowed_subcommands[id]) {\n        while(u->allowed_subcommands[id][items]) {\n            /* If it's already here do not add it again. */\n            if (!strcasecmp(u->allowed_subcommands[id][items],sub)) return;\n            items++;\n        }\n    }\n\n    /* Now we can make space for the new item (and the null term). */\n    items += 2;\n    u->allowed_subcommands[id] = (sds*)zrealloc(u->allowed_subcommands[id],\n                                 sizeof(sds)*items, MALLOC_LOCAL);\n    u->allowed_subcommands[id][items-2] = sdsnew(sub);\n    u->allowed_subcommands[id][items-1] = NULL;\n}\n\n/* Set user properties according to the string \"op\". The following\n * is a description of what different strings will do:\n *\n * on           Enable the user: it is possible to authenticate as this user.\n * off          Disable the user: it's no longer possible to authenticate\n *              with this user, however the already authenticated connections\n *              will still work.\n * +<command>   Allow the execution of that command\n * -<command>   Disallow the execution of that command\n * +@<category> Allow the execution of all the commands in such category\n *              with valid categories are like @admin, @set, @sortedset, ...\n *              and so forth, see the full list in the server.cpp file where\n *              the KeyDB command table is described and defined.\n *              The special category @all means all the commands, but currently\n *              present in the server, and that will be loaded in the future\n *              via modules.\n * +<command>|subcommand    Allow a specific subcommand of an otherwise\n *                          disabled command. Note that this form is not\n *                          allowed as negative like -DEBUG|SEGFAULT, but\n *                          only additive starting with \"+\".\n * allcommands  Alias for +@all. Note that it implies the ability to execute\n *              all the future commands loaded via the modules system.\n * nocommands   Alias for -@all.\n * ~<pattern>   Add a pattern of keys that can be mentioned as part of\n *              commands. For instance ~* allows all the keys. The pattern\n *              is a glob-style pattern like the one of KEYS.\n *              It is possible to specify multiple patterns.\n * allkeys      Alias for ~*\n * resetkeys    Flush the list of allowed keys patterns.\n * &<pattern>   Add a pattern of channels that can be mentioned as part of\n *              Pub/Sub commands. For instance &* allows all the channels. The\n *              pattern is a glob-style pattern like the one of PSUBSCRIBE.\n *              It is possible to specify multiple patterns.\n * allchannels              Alias for &*\n * resetchannels            Flush the list of allowed keys patterns.\n * ><password>  Add this password to the list of valid password for the user.\n *              For example >mypass will add \"mypass\" to the list.\n *              This directive clears the \"nopass\" flag (see later).\n * #<hash>      Add this password hash to the list of valid hashes for\n *              the user. This is useful if you have previously computed\n *              the hash, and don't want to store it in plaintext.\n *              This directive clears the \"nopass\" flag (see later).\n * <<password>  Remove this password from the list of valid passwords.\n * !<hash>      Remove this hashed password from the list of valid passwords.\n *              This is useful when you want to remove a password just by\n *              hash without knowing its plaintext version at all.\n * nopass       All the set passwords of the user are removed, and the user\n *              is flagged as requiring no password: it means that every\n *              password will work against this user. If this directive is\n *              used for the default user, every new connection will be\n *              immediately authenticated with the default user without\n *              any explicit AUTH command required. Note that the \"resetpass\"\n *              directive will clear this condition.\n * resetpass    Flush the list of allowed passwords. Moreover removes the\n *              \"nopass\" status. After \"resetpass\" the user has no associated\n *              passwords and there is no way to authenticate without adding\n *              some password (or setting it as \"nopass\" later).\n * reset        Performs the following actions: resetpass, resetkeys, off,\n *              -@all. The user returns to the same state it has immediately\n *              after its creation.\n *\n * The 'op' string must be null terminated. The 'oplen' argument should\n * specify the length of the 'op' string in case the caller requires to pass\n * binary data (for instance the >password form may use a binary password).\n * Otherwise the field can be set to -1 and the function will use strlen()\n * to determine the length.\n *\n * The function returns C_OK if the action to perform was understood because\n * the 'op' string made sense. Otherwise C_ERR is returned if the operation\n * is unknown or has some syntax error.\n *\n * When an error is returned, errno is set to the following values:\n *\n * EINVAL: The specified opcode is not understood or the key/channel pattern is\n *         invalid (contains non allowed characters).\n * ENOENT: The command name or command category provided with + or - is not\n *         known.\n * EEXIST: You are adding a key pattern after \"*\" was already added. This is\n *         almost surely an error on the user side.\n * EISDIR: You are adding a channel pattern after \"*\" was already added. This is\n *         almost surely an error on the user side.\n * ENODEV: The password you are trying to remove from the user does not exist.\n * EBADMSG: The hash you are trying to add is not a valid hash.\n */\nint ACLSetUser(user *u, const char *op, ssize_t oplen) {\n    if (oplen == -1) oplen = strlen(op);\n    if (oplen == 0) return C_OK; /* Empty string is a no-operation. */\n    if (!strcasecmp(op,\"on\")) {\n        u->flags |= USER_FLAG_ENABLED;\n        u->flags &= ~USER_FLAG_DISABLED;\n    } else if (!strcasecmp(op,\"off\")) {\n        u->flags |= USER_FLAG_DISABLED;\n        u->flags &= ~USER_FLAG_ENABLED;\n    } else if (!strcasecmp(op,\"skip-sanitize-payload\")) {\n        u->flags |= USER_FLAG_SANITIZE_PAYLOAD_SKIP;\n        u->flags &= ~USER_FLAG_SANITIZE_PAYLOAD;\n    } else if (!strcasecmp(op,\"sanitize-payload\")) {\n        u->flags &= ~USER_FLAG_SANITIZE_PAYLOAD_SKIP;\n        u->flags |= USER_FLAG_SANITIZE_PAYLOAD;\n    } else if (!strcasecmp(op,\"allkeys\") ||\n               !strcasecmp(op,\"~*\"))\n    {\n        u->flags |= USER_FLAG_ALLKEYS;\n        listEmpty(u->patterns);\n    } else if (!strcasecmp(op,\"resetkeys\")) {\n        u->flags &= ~USER_FLAG_ALLKEYS;\n        listEmpty(u->patterns);\n    } else if (!strcasecmp(op,\"allchannels\") ||\n               !strcasecmp(op,\"&*\"))\n    {\n        u->flags |= USER_FLAG_ALLCHANNELS;\n        listEmpty(u->channels);\n    } else if (!strcasecmp(op,\"resetchannels\")) {\n        u->flags &= ~USER_FLAG_ALLCHANNELS;\n        listEmpty(u->channels);\n    } else if (!strcasecmp(op,\"allcommands\") ||\n               !strcasecmp(op,\"+@all\"))\n    {\n        memset(u->allowed_commands,255,sizeof(u->allowed_commands));\n        u->flags |= USER_FLAG_ALLCOMMANDS;\n        ACLResetSubcommands(u);\n    } else if (!strcasecmp(op,\"nocommands\") ||\n               !strcasecmp(op,\"-@all\"))\n    {\n        memset(u->allowed_commands,0,sizeof(u->allowed_commands));\n        u->flags &= ~USER_FLAG_ALLCOMMANDS;\n        ACLResetSubcommands(u);\n    } else if (!strcasecmp(op,\"nopass\")) {\n        u->flags |= USER_FLAG_NOPASS;\n        listEmpty(u->passwords);\n    } else if (!strcasecmp(op,\"resetpass\")) {\n        u->flags &= ~USER_FLAG_NOPASS;\n        listEmpty(u->passwords);\n    } else if (op[0] == '>' || op[0] == '#') {\n        sds newpass;\n        if (op[0] == '>') {\n            newpass = ACLHashPassword((unsigned char*)op+1,oplen-1);\n        } else {\n            if (ACLCheckPasswordHash((unsigned char*)op+1,oplen-1) == C_ERR) {\n                errno = EBADMSG;\n                return C_ERR;\n            }\n            newpass = sdsnewlen(op+1,oplen-1);\n        }\n\n        listNode *ln = listSearchKey(u->passwords,newpass);\n        /* Avoid re-adding the same password multiple times. */\n        if (ln == NULL)\n            listAddNodeTail(u->passwords,newpass);\n        else\n            sdsfree(newpass);\n        u->flags &= ~USER_FLAG_NOPASS;\n    } else if (op[0] == '<' || op[0] == '!') {\n        sds delpass;\n        if (op[0] == '<') {\n            delpass = ACLHashPassword((unsigned char*)op+1,oplen-1);\n        } else {\n            if (ACLCheckPasswordHash((unsigned char*)op+1,oplen-1) == C_ERR) {\n                errno = EBADMSG;\n                return C_ERR;\n            }\n            delpass = sdsnewlen(op+1,oplen-1);\n        }\n        listNode *ln = listSearchKey(u->passwords,delpass);\n        sdsfree(delpass);\n        if (ln) {\n            listDelNode(u->passwords,ln);\n        } else {\n            errno = ENODEV;\n            return C_ERR;\n        }\n    } else if (op[0] == '~') {\n        if (u->flags & USER_FLAG_ALLKEYS) {\n            errno = EEXIST;\n            return C_ERR;\n        }\n        if (ACLStringHasSpaces(op+1,oplen-1)) {\n            errno = EINVAL;\n            return C_ERR;\n        }\n        sds newpat = sdsnewlen(op+1,oplen-1);\n        listNode *ln = listSearchKey(u->patterns,newpat);\n        /* Avoid re-adding the same key pattern multiple times. */\n        if (ln == NULL)\n            listAddNodeTail(u->patterns,newpat);\n        else\n            sdsfree(newpat);\n        u->flags &= ~USER_FLAG_ALLKEYS;\n    } else if (op[0] == '&') {\n        if (u->flags & USER_FLAG_ALLCHANNELS) {\n            errno = EISDIR;\n            return C_ERR;\n        }\n        if (ACLStringHasSpaces(op+1,oplen-1)) {\n            errno = EINVAL;\n            return C_ERR;\n        }\n        sds newpat = sdsnewlen(op+1,oplen-1);\n        listNode *ln = listSearchKey(u->channels,newpat);\n        /* Avoid re-adding the same channel pattern multiple times. */\n        if (ln == NULL)\n            listAddNodeTail(u->channels,newpat);\n        else\n            sdsfree(newpat);\n        u->flags &= ~USER_FLAG_ALLCHANNELS;\n    } else if (op[0] == '+' && op[1] != '@') {\n        if (strchr(op,'|') == NULL) {\n            if (ACLLookupCommand(op+1) == NULL) {\n                errno = ENOENT;\n                return C_ERR;\n            }\n            unsigned long id = ACLGetCommandID(op+1);\n            ACLSetUserCommandBit(u,id,1);\n            ACLResetSubcommandsForCommand(u,id);\n        } else {\n            /* Split the command and subcommand parts. */\n            char *copy = zstrdup(op+1);\n            char *sub = strchr(copy,'|');\n            sub[0] = '\\0';\n            sub++;\n\n            /* Check if the command exists. We can't check the\n             * subcommand to see if it is valid. */\n            if (ACLLookupCommand(copy) == NULL) {\n                zfree(copy);\n                errno = ENOENT;\n                return C_ERR;\n            }\n\n            /* The subcommand cannot be empty, so things like DEBUG|\n             * are syntax errors of course. */\n            if (strlen(sub) == 0) {\n                zfree(copy);\n                errno = EINVAL;\n                return C_ERR;\n            }\n\n            unsigned long id = ACLGetCommandID(copy);\n            /* Add the subcommand to the list of valid ones, if the command is not set. */\n            if (ACLGetUserCommandBit(u,id) == 0) {\n                ACLAddAllowedSubcommand(u,id,sub);\n            }\n\n            zfree(copy);\n        }\n    } else if (op[0] == '-' && op[1] != '@') {\n        if (ACLLookupCommand(op+1) == NULL) {\n            errno = ENOENT;\n            return C_ERR;\n        }\n        unsigned long id = ACLGetCommandID(op+1);\n        ACLSetUserCommandBit(u,id,0);\n        ACLResetSubcommandsForCommand(u,id);\n    } else if ((op[0] == '+' || op[0] == '-') && op[1] == '@') {\n        int bitval = op[0] == '+' ? 1 : 0;\n        if (ACLSetUserCommandBitsForCategory(u,op+2,bitval) == C_ERR) {\n            errno = ENOENT;\n            return C_ERR;\n        }\n    } else if (!strcasecmp(op,\"reset\")) {\n        serverAssert(ACLSetUser(u,\"resetpass\",-1) == C_OK);\n        serverAssert(ACLSetUser(u,\"resetkeys\",-1) == C_OK);\n        serverAssert(ACLSetUser(u,\"resetchannels\",-1) == C_OK);\n        if (g_pserver->acl_pubsub_default & USER_FLAG_ALLCHANNELS)\n            serverAssert(ACLSetUser(u,\"allchannels\",-1) == C_OK);\n        serverAssert(ACLSetUser(u,\"off\",-1) == C_OK);\n        serverAssert(ACLSetUser(u,\"sanitize-payload\",-1) == C_OK);\n        serverAssert(ACLSetUser(u,\"-@all\",-1) == C_OK);\n    } else {\n        errno = EINVAL;\n        return C_ERR;\n    }\n    return C_OK;\n}\n\n/* Return a description of the error that occurred in ACLSetUser() according to\n * the errno value set by the function on error. */\nconst char *ACLSetUserStringError(void) {\n    const char *errmsg = \"Wrong format\";\n    if (errno == ENOENT)\n        errmsg = \"Unknown command or category name in ACL\";\n    else if (errno == EINVAL)\n        errmsg = \"Syntax error\";\n    else if (errno == EEXIST)\n        errmsg = \"Adding a pattern after the * pattern (or the \"\n                 \"'allkeys' flag) is not valid and does not have any \"\n                 \"effect. Try 'resetkeys' to start with an empty \"\n                 \"list of patterns\";\n    else if (errno == EISDIR)\n        errmsg = \"Adding a pattern after the * pattern (or the \"\n                 \"'allchannels' flag) is not valid and does not have any \"\n                 \"effect. Try 'resetchannels' to start with an empty \"\n                 \"list of channels\";\n    else if (errno == ENODEV)\n        errmsg = \"The password you are trying to remove from the user does \"\n                 \"not exist\";\n    else if (errno == EBADMSG)\n        errmsg = \"The password hash must be exactly 64 characters and contain \"\n                 \"only lowercase hexadecimal characters\";\n    return errmsg;\n}\n\n/* Initialize the default user, that will always exist for all the process\n * lifetime. */\nvoid ACLInitDefaultUser(void) {\n    DefaultUser = ACLCreateUser(\"default\",7);\n    ACLSetUser(DefaultUser,\"+@all\",-1);\n    ACLSetUser(DefaultUser,\"~*\",-1);\n    ACLSetUser(DefaultUser,\"&*\",-1);\n    ACLSetUser(DefaultUser,\"on\",-1);\n    ACLSetUser(DefaultUser,\"nopass\",-1);\n}\n\n/* Initialization of the ACL subsystem. */\nvoid ACLInit(void) {\n    Users = raxNew();\n    UsersToLoad = listCreate();\n    ACLLog = listCreate();\n    ACLInitDefaultUser();\n}\n\n/* Check the username and password pair and return C_OK if they are valid,\n * otherwise C_ERR is returned and errno is set to:\n *\n *  EINVAL: if the username-password do not match.\n *  ENONENT: if the specified user does not exist at all.\n */\nint ACLCheckUserCredentials(robj *username, robj *password) {\n    user *u = ACLGetUserByName(szFromObj(username),sdslen(szFromObj(username)));\n    if (u == NULL) {\n        errno = ENOENT;\n        return C_ERR;\n    }\n\n    /* Disabled users can't login. */\n    if (u->flags & USER_FLAG_DISABLED) {\n        errno = EINVAL;\n        return C_ERR;\n    }\n\n    /* If the user is configured to don't require any password, we\n     * are already fine here. */\n    if (u->flags & USER_FLAG_NOPASS) return C_OK;\n\n    /* Check all the user passwords for at least one to match. */\n    listIter li;\n    listNode *ln;\n    listRewind(u->passwords,&li);\n    sds hashed = ACLHashPassword((unsigned char*)szFromObj(password),sdslen(szFromObj(password)));\n    while((ln = listNext(&li))) {\n        sds thispass = (sds)listNodeValue(ln);\n        if (!time_independent_strcmp(hashed, thispass)) {\n            sdsfree(hashed);\n            return C_OK;\n        }\n    }\n    sdsfree(hashed);\n\n    /* If we reached this point, no password matched. */\n    errno = EINVAL;\n    return C_ERR;\n}\n\n/* This is like ACLCheckUserCredentials(), however if the user/pass\n * are correct, the connection is put in authenticated state and the\n * connection user reference is populated.\n *\n * The return value is C_OK or C_ERR with the same meaning as\n * ACLCheckUserCredentials(). */\nint ACLAuthenticateUser(client *c, robj *username, robj *password) {\n    if (ACLCheckUserCredentials(username,password) == C_OK) {\n        c->authenticated = 1;\n        c->user = ACLGetUserByName((sds)ptrFromObj(username),sdslen((sds)ptrFromObj(username)));\n        moduleNotifyUserChanged(c);\n        return C_OK;\n    } else {\n        addACLLogEntry(c,ACL_DENIED_AUTH,0,szFromObj(username));\n        return C_ERR;\n    }\n}\n\n/* For ACL purposes, every user has a bitmap with the commands that such\n * user is allowed to execute. In order to populate the bitmap, every command\n * should have an assigned ID (that is used to index the bitmap). This function\n * creates such an ID: it uses sequential IDs, reusing the same ID for the same\n * command name, so that a command retains the same ID in case of modules that\n * are unloaded and later reloaded. */\nunsigned long ACLGetCommandID(const char *cmdname) {\n\n    sds lowername = sdsnew(cmdname);\n    sdstolower(lowername);\n    if (commandId == NULL) commandId = raxNew();\n    void *id = raxFind(commandId,(unsigned char*)lowername,sdslen(lowername));\n    if (id != raxNotFound) {\n        sdsfree(lowername);\n        return (unsigned long)id;\n    }\n    raxInsert(commandId,(unsigned char*)lowername,strlen(lowername),\n              (void*)nextid,NULL);\n    sdsfree(lowername);\n    unsigned long thisid = nextid;\n    nextid++;\n\n    /* We never assign the last bit in the user commands bitmap structure,\n     * this way we can later check if this bit is set, understanding if the\n     * current ACL for the user was created starting with a +@all to add all\n     * the possible commands and just subtracting other single commands or\n     * categories, or if, instead, the ACL was created just adding commands\n     * and command categories from scratch, not allowing future commands by\n     * default (loaded via modules). This is useful when rewriting the ACLs\n     * with ACL SAVE. */\n    if (nextid == USER_COMMAND_BITS_COUNT-1) nextid++;\n    return thisid;\n}\n\n/* Clear command id table and reset nextid to 0. */\nvoid ACLClearCommandID(void) {\n    if (commandId) raxFree(commandId);\n    commandId = NULL;\n    nextid = 0;\n}\n\n/* Return an username by its name, or NULL if the user does not exist. */\nuser *ACLGetUserByName(const char *name, size_t namelen) {\n    void *myuser = raxFind(Users,(unsigned char*)name,namelen);\n    if (myuser == raxNotFound) return NULL;\n    return (user*)myuser;\n}\n\n/* Check if the command is ready to be executed in the client 'c', already\n * referenced by c->cmd, and can be executed by this client according to the\n * ACLs associated to the client user c->user.\n *\n * If the user can execute the command ACL_OK is returned, otherwise\n * ACL_DENIED_CMD or ACL_DENIED_KEY is returned: the first in case the\n * command cannot be executed because the user is not allowed to run such\n * command, the second if the command is denied because the user is trying\n * to access keys that are not among the specified patterns. */\nint ACLCheckCommandPerm(client *c, int *keyidxptr) {\n    user *u = c->user;\n    uint64_t id = c->cmd->id;\n\n    /* If there is no associated user, the connection can run anything. */\n    if (u == NULL) return ACL_OK;\n\n    /* Check if the user can execute this command or if the command\n     * doesn't need to be authenticated (hello, auth). */\n    if (!(u->flags & USER_FLAG_ALLCOMMANDS) && !(c->cmd->flags & CMD_NO_AUTH))\n    {\n        /* If the bit is not set we have to check further, in case the\n         * command is allowed just with that specific subcommand. */\n        if (ACLGetUserCommandBit(u,id) == 0) {\n            /* Check if the subcommand matches. */\n            if (c->argc < 2 ||\n                u->allowed_subcommands == NULL ||\n                u->allowed_subcommands[id] == NULL)\n            {\n                return ACL_DENIED_CMD;\n            }\n\n            long subid = 0;\n            while (1) {\n                if (u->allowed_subcommands[id][subid] == NULL)\n                    return ACL_DENIED_CMD;\n                if (!strcasecmp(szFromObj(c->argv[1]),\n                                u->allowed_subcommands[id][subid]))\n                    break; /* Subcommand match found. Stop here. */\n                subid++;\n            }\n        }\n    }\n\n    /* Check if the user can execute commands explicitly touching the keys\n     * mentioned in the command arguments. */\n    if (!(c->user->flags & USER_FLAG_ALLKEYS) &&\n        (c->cmd->getkeys_proc || c->cmd->firstkey))\n    {\n        getKeysResult result = GETKEYS_RESULT_INIT;\n        int numkeys = getKeysFromCommand(c->cmd,c->argv,c->argc,&result);\n        int *keyidx = result.keys;\n        for (int j = 0; j < numkeys; j++) {\n            listIter li;\n            listNode *ln;\n            listRewind(u->patterns,&li);\n\n            /* Test this key against every pattern. */\n            int match = 0;\n            while((ln = listNext(&li))) {\n                sds pattern = (sds)listNodeValue(ln);\n                size_t plen = sdslen(pattern);\n                int idx = keyidx[j];\n                if (stringmatchlen(pattern,plen,szFromObj(c->argv[idx]),\n                                   sdslen(szFromObj(c->argv[idx])),0))\n                {\n                    match = 1;\n                    break;\n                }\n            }\n            if (!match) {\n                if (keyidxptr) *keyidxptr = keyidx[j];\n                getKeysFreeResult(&result);\n                return ACL_DENIED_KEY;\n            }\n        }\n        getKeysFreeResult(&result);\n    }\n\n    /* If we survived all the above checks, the user can execute the\n     * command. */\n    return ACL_OK;\n}\n\n/* Check if the provided channel is whitelisted by the given allowed channels\n * list. Glob-style pattern matching is employed, unless the literal flag is\n * set. Returns ACL_OK if access is granted or ACL_DENIED_CHANNEL otherwise. */\nint ACLCheckPubsubChannelPerm(sds channel, list *allowed, int literal) {\n    listIter li;\n    listNode *ln;\n    size_t clen = sdslen(channel);\n    int match = 0;\n\n    listRewind(allowed,&li);\n    while((ln = listNext(&li))) {\n        sds pattern = (sds)listNodeValue(ln);\n        size_t plen = sdslen(pattern);\n\n        if ((literal && !sdscmp(pattern,channel)) || \n            (!literal && stringmatchlen(pattern,plen,channel,clen,0)))\n        {\n            match = 1;\n            break;\n        }\n    }\n    if (!match) {\n        return ACL_DENIED_CHANNEL;\n    }\n    return ACL_OK;\n}\n\n/* Check if the user's existing pub/sub clients violate the ACL pub/sub\n * permissions specified via the upcoming argument, and kill them if so. */\nvoid ACLKillPubsubClientsIfNeeded(user *u, list *upcoming) {\n    listIter li, lpi;\n    listNode *ln, *lpn;\n    robj *o;\n    int kill = 0;\n    \n    /* Nothing to kill when the upcoming are a literal super set of the original\n     * permissions. */\n    listRewind(u->channels,&li);\n    while (!kill && ((ln = listNext(&li)) != NULL)) {\n        sds pattern = (sds)listNodeValue(ln);\n        kill = (ACLCheckPubsubChannelPerm(pattern,upcoming,1) ==\n                ACL_DENIED_CHANNEL);\n    }\n    if (!kill) return;\n\n    /* Scan all connected clients to find the user's pub/subs. */\n    listRewind(g_pserver->clients,&li);\n    while ((ln = listNext(&li)) != NULL) {\n        client *c = (client*)listNodeValue(ln);\n        kill = 0;\n\n        if (c->user == u && getClientType(c) == CLIENT_TYPE_PUBSUB) {\n            /* Check for pattern violations. */\n            listRewind(c->pubsub_patterns,&lpi);\n            while (!kill && ((lpn = listNext(&lpi)) != NULL)) {\n                o = (robj*)lpn->value;\n                kill = (ACLCheckPubsubChannelPerm(szFromObj(o),upcoming,1) == \n                        ACL_DENIED_CHANNEL);\n            }\n            /* Check for channel violations. */\n            if (!kill) {\n                dictIterator *di = dictGetIterator(c->pubsub_channels);\n                dictEntry *de;                \n                while (!kill && ((de = dictNext(di)) != NULL)) {\n                    o = (robj*)dictGetKey(de);\n                    kill = (ACLCheckPubsubChannelPerm(szFromObj(o),upcoming,0) ==\n                            ACL_DENIED_CHANNEL);\n                }\n                dictReleaseIterator(di);\n            }\n\n            /* Kill it. */\n            if (kill) {\n                freeClientAsync(c);\n            }\n        }\n    }\n}\n\n/* Check if the pub/sub channels of the command, that's ready to be executed in\n * the client 'c', can be executed by this client according to the ACLs channels\n * associated to the client user c->user.\n * \n * idx and count are the index and count of channel arguments from the\n * command. The literal argument controls whether the user's ACL channels are \n * evaluated as literal values or matched as glob-like patterns.\n *\n * If the user can execute the command ACL_OK is returned, otherwise \n * ACL_DENIED_CHANNEL. */\nint ACLCheckPubsubPerm(client *c, int idx, int count, int literal, int *idxptr) {\n    user *u = c->user;\n\n    /* If there is no associated user, the connection can run anything. */\n    if (u == NULL) return ACL_OK;\n\n    /* Check if the user can access the channels mentioned in the command's\n     * arguments. */\n    if (!(c->user->flags & USER_FLAG_ALLCHANNELS)) {\n        for (int j = idx; j < idx+count; j++) {\n            if (ACLCheckPubsubChannelPerm(szFromObj(c->argv[j]),u->channels,literal)\n                != ACL_OK) {\n                if (idxptr) *idxptr = j;\n                return ACL_DENIED_CHANNEL;\n            }\n        }\n    }\n\n    /* If we survived all the above checks, the user can execute the\n     * command. */\n    return ACL_OK;\n\n}\n\n/* Check whether the command is ready to be exceuted by ACLCheckCommandPerm.\n * If check passes, then check whether pub/sub channels of the command is\n * ready to be executed by ACLCheckPubsubPerm */\nint ACLCheckAllPerm(client *c, int *idxptr) {\n    int acl_retval = ACLCheckCommandPerm(c,idxptr);\n    if (acl_retval != ACL_OK)\n        return acl_retval;\n    if (c->cmd->proc == publishCommand)\n        acl_retval = ACLCheckPubsubPerm(c,1,1,0,idxptr);\n    else if (c->cmd->proc == subscribeCommand)\n        acl_retval = ACLCheckPubsubPerm(c,1,c->argc-1,0,idxptr);\n    else if (c->cmd->proc == psubscribeCommand)\n        acl_retval = ACLCheckPubsubPerm(c,1,c->argc-1,1,idxptr);\n    return acl_retval;\n}\n\n/* =============================================================================\n * ACL loading / saving functions\n * ==========================================================================*/\n\n/* Given an argument vector describing a user in the form:\n *\n *      user <username> ... ACL rules and flags ...\n *\n * this function validates, and if the syntax is valid, appends\n * the user definition to a list for later loading.\n *\n * The rules are tested for validity and if there obvious syntax errors\n * the function returns C_ERR and does nothing, otherwise C_OK is returned\n * and the user is appended to the list.\n *\n * Note that this function cannot stop in case of commands that are not found\n * and, in that case, the error will be emitted later, because certain\n * commands may be defined later once modules are loaded.\n *\n * When an error is detected and C_ERR is returned, the function populates\n * by reference (if not set to NULL) the argc_err argument with the index\n * of the argv vector that caused the error. */\nint ACLAppendUserForLoading(sds *argv, int argc, int *argc_err) {\n    if (argc < 2 || strcasecmp(argv[0],\"user\")) {\n        if (argc_err) *argc_err = 0;\n        return C_ERR;\n    }\n\n    /* Try to apply the user rules in a fake user to see if they\n     * are actually valid. */\n    user *fakeuser = ACLCreateUnlinkedUser();\n\n    for (int j = 2; j < argc; j++) {\n        if (ACLSetUser(fakeuser,argv[j],sdslen(argv[j])) == C_ERR) {\n            if (errno != ENOENT) {\n                ACLFreeUser(fakeuser);\n                if (argc_err) *argc_err = j;\n                return C_ERR;\n            }\n        }\n    }\n\n    /* Rules look valid, let's append the user to the list. */\n    sds *copy = (sds*)zmalloc(sizeof(sds)*argc, MALLOC_LOCAL);\n    for (int j = 1; j < argc; j++) copy[j-1] = sdsdup(argv[j]);\n    copy[argc-1] = NULL;\n    listAddNodeTail(UsersToLoad,copy);\n    ACLFreeUser(fakeuser);\n    return C_OK;\n}\n\n/* This function will load the configured users appended to the server\n * configuration via ACLAppendUserForLoading(). On loading errors it will\n * log an error and return C_ERR, otherwise C_OK will be returned. */\nint ACLLoadConfiguredUsers(void) {\n    listIter li;\n    listNode *ln;\n    listRewind(UsersToLoad,&li);\n    while ((ln = listNext(&li)) != NULL) {\n        sds *aclrules = (sds*)listNodeValue(ln);\n        sds username = aclrules[0];\n\n        if (ACLStringHasSpaces(aclrules[0],sdslen(aclrules[0]))) {\n            serverLog(LL_WARNING,\"Spaces not allowed in ACL usernames\");\n            return C_ERR;\n        }\n\n        user *u = ACLCreateUser(username,sdslen(username));\n        if (!u) {\n            u = ACLGetUserByName(username,sdslen(username));\n            serverAssert(u != NULL);\n            ACLSetUser(u,\"reset\",-1);\n        }\n\n        /* Load every rule defined for this user. */\n        for (int j = 1; aclrules[j]; j++) {\n            if (ACLSetUser(u,aclrules[j],sdslen(aclrules[j])) != C_OK) {\n                const char *errmsg = ACLSetUserStringError();\n                serverLog(LL_WARNING,\"Error loading ACL rule '%s' for \"\n                                     \"the user named '%s': %s\",\n                          aclrules[j],aclrules[0],errmsg);\n                return C_ERR;\n            }\n        }\n\n        /* Having a disabled user in the configuration may be an error,\n         * warn about it without returning any error to the caller. */\n        if (u->flags & USER_FLAG_DISABLED) {\n            serverLog(LL_NOTICE, \"The user '%s' is disabled (there is no \"\n                                 \"'on' modifier in the user description). Make \"\n                                 \"sure this is not a configuration error.\",\n                      aclrules[0]);\n        }\n    }\n    return C_OK;\n}\n\n/* This function loads the ACL from the specified filename: every line\n * is validated and should be either empty or in the format used to specify\n * users in the keydb.conf configuration or in the ACL file, that is:\n *\n *  user <username> ... rules ...\n *\n * Note that this function considers comments starting with '#' as errors\n * because the ACL file is meant to be rewritten, and comments would be\n * lost after the rewrite. Yet empty lines are allowed to avoid being too\n * strict.\n *\n * One important part of implementing ACL LOAD, that uses this function, is\n * to avoid ending with broken rules if the ACL file is invalid for some\n * reason, so the function will attempt to validate the rules before loading\n * each user. For every line that will be found broken the function will\n * collect an error message.\n *\n * IMPORTANT: If there is at least a single error, nothing will be loaded\n * and the rules will remain exactly as they were.\n *\n * At the end of the process, if no errors were found in the whole file then\n * NULL is returned. Otherwise an SDS string describing in a single line\n * a description of all the issues found is returned. */\nsds ACLLoadFromFile(const char *filename) {\n    FILE *fp;\n    char buf[1024];\n\n    /* Open the ACL file. */\n    if ((fp = fopen(filename,\"r\")) == NULL) {\n        sds errors = sdscatprintf(sdsempty(),\n            \"Error loading ACLs, opening file '%s': %s\",\n            filename, strerror(errno));\n        return errors;\n    }\n\n    /* Load the whole file as a single string in memory. */\n    sds acls = sdsempty();\n    while(fgets(buf,sizeof(buf),fp) != NULL)\n        acls = sdscat(acls,buf);\n    fclose(fp);\n\n    /* Split the file into lines and attempt to load each line. */\n    int totlines;\n    sds *lines, errors = sdsempty();\n    lines = sdssplitlen(acls,strlen(acls),\"\\n\",1,&totlines);\n    sdsfree(acls);\n\n    /* We need a fake user to validate the rules before making changes\n     * to the real user mentioned in the ACL line. */\n    user *fakeuser = ACLCreateUnlinkedUser();\n\n    /* We do all the loading in a fresh instance of the Users radix tree,\n     * so if there are errors loading the ACL file we can rollback to the\n     * old version. */\n    rax *old_users = Users;\n    user *old_default_user = DefaultUser;\n    Users = raxNew();\n    ACLInitDefaultUser();\n\n    /* Load each line of the file. */\n    for (int i = 0; i < totlines; i++) {\n        sds *argv;\n        int argc;\n        int linenum = i+1;\n\n        lines[i] = sdstrim(lines[i],\" \\t\\r\\n\");\n\n        /* Skip blank lines */\n        if (lines[i][0] == '\\0') continue;\n\n        /* Split into arguments */\n        argv = sdssplitlen(lines[i],sdslen(lines[i]),\" \",1,&argc);\n        if (argv == NULL) {\n            errors = sdscatprintf(errors,\n                     \"%s:%d: unbalanced quotes in acl line. \",\n                     g_pserver->acl_filename, linenum);\n            continue;\n        }\n\n        /* Skip this line if the resulting command vector is empty. */\n        if (argc == 0) {\n            sdsfreesplitres(argv,argc);\n            continue;\n        }\n\n        /* The line should start with the \"user\" keyword. */\n        if (strcmp(argv[0],\"user\") || argc < 2) {\n            errors = sdscatprintf(errors,\n                     \"%s:%d should start with user keyword followed \"\n                     \"by the username. \", g_pserver->acl_filename,\n                     linenum);\n            sdsfreesplitres(argv,argc);\n            continue;\n        }\n\n        /* Spaces are not allowed in usernames. */\n        if (ACLStringHasSpaces(argv[1],sdslen(argv[1]))) {\n            errors = sdscatprintf(errors,\n                     \"'%s:%d: username '%s' contains invalid characters. \",\n                     g_pserver->acl_filename, linenum, argv[1]);\n            sdsfreesplitres(argv,argc);\n            continue;\n        }\n\n        /* Try to process the line using the fake user to validate if\n         * the rules are able to apply cleanly. At this stage we also\n         * trim trailing spaces, so that we don't have to handle that\n         * in ACLSetUser(). */\n        ACLSetUser(fakeuser,\"reset\",-1);\n        int j;\n        for (j = 2; j < argc; j++) {\n            argv[j] = sdstrim(argv[j],\"\\t\\r\\n\");\n            if (ACLSetUser(fakeuser,argv[j],sdslen(argv[j])) != C_OK) {\n                const char *errmsg = ACLSetUserStringError();\n                errors = sdscatprintf(errors,\n                         \"%s:%d: %s. \",\n                         g_pserver->acl_filename, linenum, errmsg);\n                continue;\n            }\n        }\n\n        /* Apply the rule to the new users set only if so far there\n         * are no errors, otherwise it's useless since we are going\n         * to discard the new users set anyway. */\n        if (sdslen(errors) != 0) {\n            sdsfreesplitres(argv,argc);\n            continue;\n        }\n\n        /* We can finally lookup the user and apply the rule. If the\n         * user already exists we always reset it to start. */\n        user *u = ACLCreateUser(argv[1],sdslen(argv[1]));\n        if (!u) {\n            u = ACLGetUserByName(argv[1],sdslen(argv[1]));\n            serverAssert(u != NULL);\n            ACLSetUser(u,\"reset\",-1);\n        }\n\n        /* Note that the same rules already applied to the fake user, so\n         * we just assert that everything goes well: it should. */\n        for (j = 2; j < argc; j++)\n            serverAssert(ACLSetUser(u,argv[j],sdslen(argv[j])) == C_OK);\n\n        sdsfreesplitres(argv,argc);\n    }\n\n    ACLFreeUser(fakeuser);\n    sdsfreesplitres(lines,totlines);\n    DefaultUser = old_default_user; /* This pointer must never change. */\n\n    /* Check if we found errors and react accordingly. */\n    if (sdslen(errors) == 0) {\n        /* The default user pointer is referenced in different places: instead\n         * of replacing such occurrences it is much simpler to copy the new\n         * default user configuration in the old one. */\n        user *newuser = ACLGetUserByName(\"default\",7);\n        serverAssert(newuser != NULL);\n        ACLCopyUser(DefaultUser,newuser);\n        ACLFreeUser(newuser);\n        raxInsert(Users,(unsigned char*)\"default\",7,DefaultUser,NULL);\n        raxRemove(old_users,(unsigned char*)\"default\",7,NULL);\n        ACLFreeUsersSet(old_users);\n        sdsfree(errors);\n        return NULL;\n    } else {\n        ACLFreeUsersSet(Users);\n        Users = old_users;\n        errors = sdscat(errors,\"WARNING: ACL errors detected, no change to the previously active ACL rules was performed\");\n        return errors;\n    }\n}\n\n/* Generate a copy of the ACLs currently in memory in the specified filename.\n * Returns C_OK on success or C_ERR if there was an error during the I/O.\n * When C_ERR is returned a log is produced with hints about the issue. */\nint ACLSaveToFile(const char *filename) {\n    sds acl = sdsempty();\n    int fd = -1;\n    sds tmpfilename = NULL;\n    int retval = C_ERR;\n\n    /* Let's generate an SDS string containing the new version of the\n     * ACL file. */\n    raxIterator ri;\n    raxStart(&ri,Users);\n    raxSeek(&ri,\"^\",NULL,0);\n    while(raxNext(&ri)) {\n        user *u = (user*)ri.data;\n        /* Return information in the configuration file format. */\n        sds user = sdsnew(\"user \");\n        user = sdscatsds(user,u->name);\n        user = sdscatlen(user,\" \",1);\n        sds descr = ACLDescribeUser(u);\n        user = sdscatsds(user,descr);\n        sdsfree(descr);\n        acl = sdscatsds(acl,user);\n        acl = sdscatlen(acl,\"\\n\",1);\n        sdsfree(user);\n    }\n    raxStop(&ri);\n\n    /* Create a temp file with the new content. */\n    tmpfilename = sdsnew(filename);\n    tmpfilename = sdscatfmt(tmpfilename,\".tmp-%i-%I\",\n        (int)getpid(),(int)mstime());\n    if ((fd = open(tmpfilename,O_WRONLY|O_CREAT,0644)) == -1) {\n        serverLog(LL_WARNING,\"Opening temp ACL file for ACL SAVE: %s\",\n            strerror(errno));\n        goto cleanup;\n    }\n\n    /* Write it. */\n    if (write(fd,acl,sdslen(acl)) != (ssize_t)sdslen(acl)) {\n        serverLog(LL_WARNING,\"Writing ACL file for ACL SAVE: %s\",\n            strerror(errno));\n        goto cleanup;\n    }\n    close(fd); fd = -1;\n\n    /* Let's replace the new file with the old one. */\n    if (rename(tmpfilename,filename) == -1) {\n        serverLog(LL_WARNING,\"Renaming ACL file for ACL SAVE: %s\",\n            strerror(errno));\n        goto cleanup;\n    }\n    sdsfree(tmpfilename); tmpfilename = NULL;\n    retval = C_OK; /* If we reached this point, everything is fine. */\n\ncleanup:\n    if (fd != -1) close(fd);\n    if (tmpfilename) unlink(tmpfilename);\n    sdsfree(tmpfilename);\n    sdsfree(acl);\n    return retval;\n}\n\n/* This function is called once the server is already running, modules are\n * loaded, and we are ready to start, in order to load the ACLs either from\n * the pending list of users defined in keydb.conf, or from the ACL file.\n * The function will just exit with an error if the user is trying to mix\n * both the loading methods. */\nvoid ACLLoadUsersAtStartup(void) {\n    if (g_pserver->acl_filename[0] != '\\0' && listLength(UsersToLoad) != 0) {\n        serverLog(LL_WARNING,\n            \"Configuring KeyDB with users defined in keydb.conf and at \"\n            \"the same setting an ACL file path is invalid. This setup \"\n            \"is very likely to lead to configuration errors and security \"\n            \"holes, please define either an ACL file or declare users \"\n            \"directly in your keydb.conf, but not both.\");\n        exit(1);\n    }\n\n    if (ACLLoadConfiguredUsers() == C_ERR) {\n        serverLog(LL_WARNING,\n            \"Critical error while loading ACLs. Exiting.\");\n        exit(1);\n    }\n\n    if (g_pserver->acl_filename[0] != '\\0') {\n        sds errors = ACLLoadFromFile(g_pserver->acl_filename);\n        if (errors) {\n            serverLog(LL_WARNING,\n                \"Aborting KeyDB startup because of ACL errors: %s\", errors);\n            sdsfree(errors);\n            exit(1);\n        }\n    }\n}\n\n/* =============================================================================\n * ACL log\n * ==========================================================================*/\n\n#define ACL_LOG_CTX_TOPLEVEL 0\n#define ACL_LOG_CTX_LUA 1\n#define ACL_LOG_CTX_MULTI 2\n#define ACL_LOG_GROUPING_MAX_TIME_DELTA 60000\n\n/* This structure defines an entry inside the ACL log. */\ntypedef struct ACLLogEntry {\n    uint64_t count;     /* Number of times this happened recently. */\n    int reason;         /* Reason for denying the command. ACL_DENIED_*. */\n    int context;        /* Toplevel, Lua or MULTI/EXEC? ACL_LOG_CTX_*. */\n    sds object;         /* The key name or command name. */\n    sds username;       /* User the client is authenticated with. */\n    mstime_t ctime;     /* Milliseconds time of last update to this entry. */\n    sds cinfo;          /* Client info (last client if updated). */\n} ACLLogEntry;\n\n/* This function will check if ACL entries 'a' and 'b' are similar enough\n * that we should actually update the existing entry in our ACL log instead\n * of creating a new one. */\nint ACLLogMatchEntry(ACLLogEntry *a, ACLLogEntry *b) {\n    if (a->reason != b->reason) return 0;\n    if (a->context != b->context) return 0;\n    mstime_t delta = a->ctime - b->ctime;\n    if (delta < 0) delta = -delta;\n    if (delta > ACL_LOG_GROUPING_MAX_TIME_DELTA) return 0;\n    if (sdscmp(a->object,b->object) != 0) return 0;\n    if (sdscmp(a->username,b->username) != 0) return 0;\n    return 1;\n}\n\n/* Release an ACL log entry. */\nvoid ACLFreeLogEntry(const void *leptr) {\n    ACLLogEntry *le = (ACLLogEntry*)leptr;\n    sdsfree(le->object);\n    sdsfree(le->username);\n    sdsfree(le->cinfo);\n    zfree(le);\n}\n\n/* Adds a new entry in the ACL log, making sure to delete the old entry\n * if we reach the maximum length allowed for the log. This function attempts\n * to find similar entries in the current log in order to bump the counter of\n * the log entry instead of creating many entries for very similar ACL\n * rules issues.\n *\n * The argpos argument is used when the reason is ACL_DENIED_KEY or \n * ACL_DENIED_CHANNEL, since it allows the function to log the key or channel\n * name that caused the problem. Similarly the username is only passed when we\n * failed to authenticate the user with AUTH or HELLO, for the ACL_DENIED_AUTH\n * reason. Otherwise it will just be NULL.\n */\nvoid addACLLogEntry(client *c, int reason, int argpos, sds username) {\n    /* Create a new entry. */\n    struct ACLLogEntry *le = (ACLLogEntry*)zmalloc(sizeof(*le));\n    le->count = 1;\n    le->reason = reason;\n    le->username = sdsdup(reason == ACL_DENIED_AUTH ? username : c->user->name);\n    le->ctime = mstime();\n\n    switch(reason) {\n    case ACL_DENIED_CMD: le->object = sdsnew(c->cmd->name); break;\n    case ACL_DENIED_KEY: le->object = sdsdup(szFromObj(c->argv[argpos])); break;\n    case ACL_DENIED_CHANNEL: le->object = sdsdup(szFromObj(c->argv[argpos])); break;\n    case ACL_DENIED_AUTH: le->object = sdsdup(szFromObj(c->argv[0])); break;\n    default: le->object = sdsempty();\n    }\n\n    client *realclient = c;\n    if (realclient->flags & CLIENT_LUA) realclient = g_pserver->lua_caller;\n\n    le->cinfo = catClientInfoString(sdsempty(),realclient);\n    if (c->flags & CLIENT_MULTI) {\n        le->context = ACL_LOG_CTX_MULTI;\n    } else if (c->flags & CLIENT_LUA) {\n        le->context = ACL_LOG_CTX_LUA;\n    } else {\n        le->context = ACL_LOG_CTX_TOPLEVEL;\n    }\n\n    /* Try to match this entry with past ones, to see if we can just\n     * update an existing entry instead of creating a new one. */\n    long toscan = 10; /* Do a limited work trying to find duplicated. */\n    listIter li;\n    listNode *ln;\n    listRewind(ACLLog,&li);\n    ACLLogEntry *match = NULL;\n    while (toscan-- && (ln = listNext(&li)) != NULL) {\n        ACLLogEntry *current = (ACLLogEntry*)listNodeValue(ln);\n        if (ACLLogMatchEntry(current,le)) {\n            match = current;\n            listDelNode(ACLLog,ln);\n            listAddNodeHead(ACLLog,current);\n            break;\n        }\n    }\n\n    /* If there is a match update the entry, otherwise add it as a\n     * new one. */\n    if (match) {\n        /* We update a few fields of the existing entry and bump the\n         * counter of events for this entry. */\n        sdsfree(match->cinfo);\n        match->cinfo = le->cinfo;\n        match->ctime = le->ctime;\n        match->count++;\n\n        /* Release the old entry. */\n        le->cinfo = NULL;\n        ACLFreeLogEntry(le);\n    } else {\n        /* Add it to our list of entries. We'll have to trim the list\n         * to its maximum size. */\n        listAddNodeHead(ACLLog, le);\n        while(listLength(ACLLog) > g_pserver->acllog_max_len) {\n            listNode *ln = listLast(ACLLog);\n            ACLLogEntry *le = (ACLLogEntry*)listNodeValue(ln);\n            ACLFreeLogEntry(le);\n            listDelNode(ACLLog,ln);\n        }\n    }\n}\n\n/* =============================================================================\n * ACL related commands\n * ==========================================================================*/\n\n/* ACL -- show and modify the configuration of ACL users.\n * ACL HELP\n * ACL LOAD\n * ACL SAVE\n * ACL LIST\n * ACL USERS\n * ACL CAT [<category>]\n * ACL SETUSER <username> ... acl rules ...\n * ACL DELUSER <username> [...]\n * ACL GETUSER <username>\n * ACL GENPASS [<bits>]\n * ACL WHOAMI\n * ACL LOG [<count> | RESET]\n */\nvoid aclCommand(client *c) {\n    char *sub = szFromObj(c->argv[1]);\n    if (!strcasecmp(sub,\"setuser\") && c->argc >= 3) {\n        sds username = szFromObj(c->argv[2]);\n        /* Check username validity. */\n        if (ACLStringHasSpaces(username,sdslen(username))) {\n            addReplyErrorFormat(c,\n                \"Usernames can't contain spaces or null characters\");\n            return;\n        }\n\n        /* Create a temporary user to validate and stage all changes against\n         * before applying to an existing user or creating a new user. If all\n         * arguments are valid the user parameters will all be applied together.\n         * If there are any errors then none of the changes will be applied. */\n        user *tempu = ACLCreateUnlinkedUser();\n        user *u = ACLGetUserByName(username,sdslen(username));\n        if (u) ACLCopyUser(tempu, u);\n\n        /* Initially redact all of the arguments to not leak any information\n         * about the user. */\n        for (int j = 2; j < c->argc; j++) {\n            redactClientCommandArgument(c, j);\n        }\n\n        for (int j = 3; j < c->argc; j++) {\n            if (ACLSetUser(tempu,szFromObj(c->argv[j]),sdslen(szFromObj(c->argv[j]))) != C_OK) {\n                const char *errmsg = ACLSetUserStringError();\n                addReplyErrorFormat(c,\n                    \"Error in ACL SETUSER modifier '%s': %s\",\n                    (char*)ptrFromObj(c->argv[j]), errmsg);\n\n                ACLFreeUser(tempu);\n                return;\n            }\n        }\n\n        /* Existing pub/sub clients authenticated with the user may need to be\n         * disconnected if (some of) their channel permissions were revoked. */\n        if (u && !(tempu->flags & USER_FLAG_ALLCHANNELS))\n            ACLKillPubsubClientsIfNeeded(u,tempu->channels);\n\n        /* Overwrite the user with the temporary user we modified above. */\n        if (!u) u = ACLCreateUser(username,sdslen(username));\n        serverAssert(u != NULL);\n        ACLCopyUser(u, tempu);\n        ACLFreeUser(tempu);\n        addReply(c,shared.ok);\n    } else if (!strcasecmp(sub,\"deluser\") && c->argc >= 3) {\n        int deleted = 0;\n        for (int j = 2; j < c->argc; j++) {\n            sds username = szFromObj(c->argv[j]);\n            if (!strcmp(username,\"default\")) {\n                addReplyError(c,\"The 'default' user cannot be removed\");\n                return;\n            }\n        }\n\n        for (int j = 2; j < c->argc; j++) {\n            sds username = szFromObj(c->argv[j]);\n            user *u;\n            if (raxRemove(Users,(unsigned char*)username,\n                          sdslen(username),\n                          (void**)&u))\n            {\n                ACLFreeUserAndKillClients(u);\n                deleted++;\n            }\n        }\n        addReplyLongLong(c,deleted);\n    } else if (!strcasecmp(sub,\"getuser\") && c->argc == 3) {\n        user *u = ACLGetUserByName(szFromObj(c->argv[2]),sdslen(szFromObj(c->argv[2])));\n        if (u == NULL) {\n            addReplyNull(c);\n            return;\n        }\n\n        addReplyMapLen(c,5);\n\n        /* Flags */\n        addReplyBulkCString(c,\"flags\");\n        void *deflen = addReplyDeferredLen(c);\n        int numflags = 0;\n        for (int j = 0; ACLUserFlags[j].flag; j++) {\n            if (u->flags & ACLUserFlags[j].flag) {\n                addReplyBulkCString(c,ACLUserFlags[j].name);\n                numflags++;\n            }\n        }\n        setDeferredSetLen(c,deflen,numflags);\n\n        /* Passwords */\n        addReplyBulkCString(c,\"passwords\");\n        addReplyArrayLen(c,listLength(u->passwords));\n        listIter li;\n        listNode *ln;\n        listRewind(u->passwords,&li);\n        while((ln = listNext(&li))) {\n            sds thispass = (sds)listNodeValue(ln);\n            addReplyBulkCBuffer(c,thispass,sdslen(thispass));\n        }\n\n        /* Commands */\n        addReplyBulkCString(c,\"commands\");\n        sds cmddescr = ACLDescribeUserCommandRules(u);\n        addReplyBulkSds(c,cmddescr);\n\n        /* Key patterns */\n        addReplyBulkCString(c,\"keys\");\n        if (u->flags & USER_FLAG_ALLKEYS) {\n            addReplyArrayLen(c,1);\n            addReplyBulkCBuffer(c,\"*\",1);\n        } else {\n            addReplyArrayLen(c,listLength(u->patterns));\n            listIter li;\n            listNode *ln;\n            listRewind(u->patterns,&li);\n            while((ln = listNext(&li))) {\n                sds thispat = (sds)listNodeValue(ln);\n                addReplyBulkCBuffer(c,thispat,sdslen(thispat));\n            }\n        }\n\n        /* Pub/sub patterns */\n        addReplyBulkCString(c,\"channels\");\n        if (u->flags & USER_FLAG_ALLCHANNELS) {\n            addReplyArrayLen(c,1);\n            addReplyBulkCBuffer(c,\"*\",1);\n        } else {\n            addReplyArrayLen(c,listLength(u->channels));\n            listIter li;\n            listNode *ln;\n            listRewind(u->channels,&li);\n            while((ln = listNext(&li))) {\n                sds thispat = (sds)listNodeValue(ln);\n                addReplyBulkCBuffer(c,thispat,sdslen(thispat));\n            }\n        }\n    } else if ((!strcasecmp(sub,\"list\") || !strcasecmp(sub,\"users\")) &&\n               c->argc == 2)\n    {\n        int justnames = !strcasecmp(sub,\"users\");\n        addReplyArrayLen(c,raxSize(Users));\n        raxIterator ri;\n        raxStart(&ri,Users);\n        raxSeek(&ri,\"^\",NULL,0);\n        while(raxNext(&ri)) {\n            user *u = (user*)ri.data;\n            if (justnames) {\n                addReplyBulkCBuffer(c,u->name,sdslen(u->name));\n            } else {\n                /* Return information in the configuration file format. */\n                sds config = sdsnew(\"user \");\n                config = sdscatsds(config,u->name);\n                config = sdscatlen(config,\" \",1);\n                sds descr = ACLDescribeUser(u);\n                config = sdscatsds(config,descr);\n                sdsfree(descr);\n                addReplyBulkSds(c,config);\n            }\n        }\n        raxStop(&ri);\n    } else if (!strcasecmp(sub,\"whoami\") && c->argc == 2) {\n        if (c->user != NULL) {\n            addReplyBulkCBuffer(c,c->user->name,sdslen(c->user->name));\n        } else {\n            addReplyNull(c);\n        }\n    } else if (g_pserver->acl_filename[0] == '\\0' &&\n               (!strcasecmp(sub,\"load\") || !strcasecmp(sub,\"save\")))\n    {\n        addReplyError(c,\"This KeyDB instance is not configured to use an ACL file. You may want to specify users via the ACL SETUSER command and then issue a CONFIG REWRITE (assuming you have a KeyDB configuration file set) in order to store users in the KeyDB configuration.\");\n        return;\n    } else if (!strcasecmp(sub,\"load\") && c->argc == 2) {\n        sds errors = ACLLoadFromFile(g_pserver->acl_filename);\n        if (errors == NULL) {\n            addReply(c,shared.ok);\n        } else {\n            addReplyError(c,errors);\n            sdsfree(errors);\n        }\n    } else if (!strcasecmp(sub,\"save\") && c->argc == 2) {\n        if (ACLSaveToFile(g_pserver->acl_filename) == C_OK) {\n            addReply(c,shared.ok);\n        } else {\n            addReplyError(c,\"There was an error trying to save the ACLs. \"\n                            \"Please check the server logs for more \"\n                            \"information\");\n        }\n    } else if (!strcasecmp(sub,\"cat\") && c->argc == 2) {\n        void *dl = addReplyDeferredLen(c);\n        int j;\n        for (j = 0; ACLCommandCategories[j].flag != 0; j++)\n            addReplyBulkCString(c,ACLCommandCategories[j].name);\n        setDeferredArrayLen(c,dl,j);\n    } else if (!strcasecmp(sub,\"cat\") && c->argc == 3) {\n        uint64_t cflag = ACLGetCommandCategoryFlagByName(szFromObj(c->argv[2]));\n        if (cflag == 0) {\n            addReplyErrorFormat(c, \"Unknown category '%s'\", (char*)ptrFromObj(c->argv[2]));\n            return;\n        }\n        int arraylen = 0;\n        void *dl = addReplyDeferredLen(c);\n        dictIterator *di = dictGetIterator(g_pserver->orig_commands);\n        dictEntry *de;\n        while ((de = dictNext(di)) != NULL) {\n            struct redisCommand *cmd = (redisCommand*)dictGetVal(de);\n            if (cmd->flags & CMD_MODULE) continue;\n            if (cmd->flags & cflag) {\n                addReplyBulkCString(c,cmd->name);\n                arraylen++;\n            }\n        }\n        dictReleaseIterator(di);\n        setDeferredArrayLen(c,dl,arraylen);\n    } else if (!strcasecmp(sub,\"genpass\") && (c->argc == 2 || c->argc == 3)) {\n        #define GENPASS_MAX_BITS 4096\n        char pass[GENPASS_MAX_BITS/8*2]; /* Hex representation. */\n        long bits = 256; /* By default generate 256 bits passwords. */\n\n        if (c->argc == 3 && getLongFromObjectOrReply(c,c->argv[2],&bits,NULL)\n            != C_OK) return;\n\n        if (bits <= 0 || bits > GENPASS_MAX_BITS) {\n            addReplyErrorFormat(c,\n                \"ACL GENPASS argument must be the number of \"\n                \"bits for the output password, a positive number \"\n                \"up to %d\",GENPASS_MAX_BITS);\n            return;\n        }\n\n        long chars = (bits+3)/4; /* Round to number of characters to emit. */\n        getRandomHexChars(pass,chars);\n        addReplyBulkCBuffer(c,pass,chars);\n    } else if (!strcasecmp(sub,\"log\") && (c->argc == 2 || c->argc ==3)) {\n        long count = 10; /* Number of entries to emit by default. */\n\n        /* Parse the only argument that LOG may have: it could be either\n         * the number of entries the user wants to display, or alternatively\n         * the \"RESET\" command in order to flush the old entries. */\n        if (c->argc == 3) {\n            if (!strcasecmp(szFromObj(c->argv[2]),\"reset\")) {\n                listSetFreeMethod(ACLLog,ACLFreeLogEntry);\n                listEmpty(ACLLog);\n                listSetFreeMethod(ACLLog,NULL);\n                addReply(c,shared.ok);\n                return;\n            } else if (getLongFromObjectOrReply(c,c->argv[2],&count,NULL)\n                       != C_OK)\n            {\n                return;\n            }\n            if (count < 0) count = 0;\n        }\n\n        /* Fix the count according to the number of entries we got. */\n        if ((size_t)count > listLength(ACLLog))\n            count = listLength(ACLLog);\n\n        addReplyArrayLen(c,count);\n        listIter li;\n        listNode *ln;\n        listRewind(ACLLog,&li);\n        mstime_t now = mstime();\n        while (count-- && (ln = listNext(&li)) != NULL) {\n            ACLLogEntry *le = (ACLLogEntry*)listNodeValue(ln);\n            addReplyMapLen(c,7);\n            addReplyBulkCString(c,\"count\");\n            addReplyLongLong(c,le->count);\n\n            addReplyBulkCString(c,\"reason\");\n            const char *reasonstr;\n            switch(le->reason) {\n            case ACL_DENIED_CMD: reasonstr=\"command\"; break;\n            case ACL_DENIED_KEY: reasonstr=\"key\"; break;\n            case ACL_DENIED_CHANNEL: reasonstr=\"channel\"; break;\n            case ACL_DENIED_AUTH: reasonstr=\"auth\"; break;\n            default: reasonstr=\"unknown\";\n            }\n            addReplyBulkCString(c,reasonstr);\n\n            addReplyBulkCString(c,\"context\");\n            const char *ctxstr;\n            switch(le->context) {\n            case ACL_LOG_CTX_TOPLEVEL: ctxstr=\"toplevel\"; break;\n            case ACL_LOG_CTX_MULTI: ctxstr=\"multi\"; break;\n            case ACL_LOG_CTX_LUA: ctxstr=\"lua\"; break;\n            default: ctxstr=\"unknown\";\n            }\n            addReplyBulkCString(c,ctxstr);\n\n            addReplyBulkCString(c,\"object\");\n            addReplyBulkCBuffer(c,le->object,sdslen(le->object));\n            addReplyBulkCString(c,\"username\");\n            addReplyBulkCBuffer(c,le->username,sdslen(le->username));\n            addReplyBulkCString(c,\"age-seconds\");\n            double age = (double)(now - le->ctime)/1000;\n            addReplyDouble(c,age);\n            addReplyBulkCString(c,\"client-info\");\n            addReplyBulkCBuffer(c,le->cinfo,sdslen(le->cinfo));\n        }\n    } else if (c->argc == 2 && !strcasecmp(sub,\"help\")) {\n        const char *help[] = {\n\"CAT [<category>]\",\n\"    List all commands that belong to <category>, or all command categories\",\n\"    when no category is specified.\",\n\"DELUSER <username> [<username> ...]\",\n\"    Delete a list of users.\",\n\"GETUSER <username>\",\n\"    Get the user's details.\",\n\"GENPASS [<bits>]\",\n\"    Generate a secure 256-bit user password. The optional `bits` argument can\",\n\"    be used to specify a different size.\",\n\"LIST\",\n\"    Show users details in config file format.\",\n\"LOAD\",\n\"    Reload users from the ACL file.\",\n\"LOG [<count> | RESET]\",\n\"    Show the ACL log entries.\",\n\"SAVE\",\n\"    Save the current config to the ACL file.\",\n\"SETUSER <username> <attribute> [<attribute> ...]\",\n\"    Create or modify a user with the specified attributes.\",\n\"USERS\",\n\"    List all the registered usernames.\",\n\"WHOAMI\",\n\"    Return the current connection username.\",\nNULL\n        };\n        addReplyHelp(c,help);\n    } else {\n        addReplySubcommandSyntaxError(c);\n    }\n}\n\nvoid addReplyCommandCategories(client *c, struct redisCommand *cmd) {\n    int flagcount = 0;\n    void *flaglen = addReplyDeferredLen(c);\n    for (int j = 0; ACLCommandCategories[j].flag != 0; j++) {\n        if (cmd->flags & ACLCommandCategories[j].flag) {\n            addReplyStatusFormat(c, \"@%s\", ACLCommandCategories[j].name);\n            flagcount++;\n        }\n    }\n    setDeferredSetLen(c, flaglen, flagcount);\n}\n\n/* AUTH <password>\n * AUTH <username> <password> (Redis >= 6.0 form)\n *\n * When the user is omitted it means that we are trying to authenticate\n * against the default user. */\nvoid authCommand(client *c) {\n    /* Only two or three argument forms are allowed. */\n    if (c->argc > 3) {\n        addReplyErrorObject(c,shared.syntaxerr);\n        return;\n    }\n    /* Always redact the second argument */\n    redactClientCommandArgument(c, 1);\n\n    /* Handle the two different forms here. The form with two arguments\n     * will just use \"default\" as username. */\n    robj *username, *password;\n    if (c->argc == 2) {\n        /* Mimic the old behavior of giving an error for the two commands\n         * from if no password is configured. */\n        if (DefaultUser->flags & USER_FLAG_NOPASS) {\n            addReplyError(c,\"AUTH <password> called without any password \"\n                            \"configured for the default user. Are you sure \"\n                            \"your configuration is correct?\");\n            return;\n        }\n\n        username = shared.default_username; \n        password = c->argv[1];\n    } else {\n        username = c->argv[1];\n        password = c->argv[2];\n        redactClientCommandArgument(c, 2);\n    }\n\n    if (ACLAuthenticateUser(c,username,password) == C_OK) {\n        addReply(c,shared.ok);\n    } else {\n        addReplyError(c,\"-WRONGPASS invalid username-password pair or user is disabled.\");\n    }\n}\n\n/* Set the password for the \"default\" ACL user. This implements supports for\n * requirepass config, so passing in NULL will set the user to be nopass. */\nvoid ACLUpdateDefaultUserPassword(sds password) {\n    ACLSetUser(DefaultUser,\"resetpass\",-1);\n    if (password) {\n        sds aclop = sdscatlen(sdsnew(\">\"), password, sdslen(password));\n        ACLSetUser(DefaultUser,aclop,sdslen(aclop));\n        sdsfree(aclop);\n    } else {\n        ACLSetUser(DefaultUser,\"nopass\",-1);\n    }\n}\n"
  },
  {
    "path": "src/adlist.c",
    "content": "/* adlist.c - A generic doubly linked list implementation\n *\n * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n\n#include <stdlib.h>\n#include \"adlist.h\"\n#include \"zmalloc.h\"\n\n/* Create a new list. The created list can be freed with\n * listRelease(), but private value of every node need to be freed\n * by the user before to call listRelease(), or by setting a free method using\n * listSetFreeMethod.\n *\n * On error, NULL is returned. Otherwise the pointer to the new list. */\nlist *listCreate(void)\n{\n    struct list *list;\n\n    if ((list = zmalloc(sizeof(*list), MALLOC_SHARED)) == NULL)\n        return NULL;\n    list->head = list->tail = NULL;\n    list->len = 0;\n    list->dup = NULL;\n    list->free = NULL;\n    list->match = NULL;\n    return list;\n}\n\n/* Remove all the elements from the list without destroying the list itself. */\nvoid listEmpty(list *list)\n{\n    unsigned long len;\n    listNode *current, *next;\n\n    current = list->head;\n    len = list->len;\n    while(len--) {\n        next = current->next;\n        if (list->free) list->free(current->value);\n        zfree(current);\n        current = next;\n    }\n    list->head = list->tail = NULL;\n    list->len = 0;\n}\n\n/* Free the whole list.\n *\n * This function can't fail. */\nvoid listRelease(list *list)\n{\n    listEmpty(list);\n    zfree(list);\n}\n\n/* Add a new node to the list, to head, containing the specified 'value'\n * pointer as value.\n *\n * On error, NULL is returned and no operation is performed (i.e. the\n * list remains unaltered).\n * On success the 'list' pointer you pass to the function is returned. */\nlist *listAddNodeHead(list *list, void *value)\n{\n    listNode *node;\n\n    if ((node = zmalloc(sizeof(*node), MALLOC_SHARED)) == NULL)\n        return NULL;\n    node->value = value;\n    if (list->len == 0) {\n        list->head = list->tail = node;\n        node->prev = node->next = NULL;\n    } else {\n        node->prev = NULL;\n        node->next = list->head;\n        list->head->prev = node;\n        list->head = node;\n    }\n    list->len++;\n    return list;\n}\n\n/* Add a new node to the list, to tail, containing the specified 'value'\n * pointer as value.\n *\n * On error, NULL is returned and no operation is performed (i.e. the\n * list remains unaltered).\n * On success the 'list' pointer you pass to the function is returned. */\nlist *listAddNodeTail(list *list, void *value)\n{\n    listNode *node;\n\n    if ((node = zmalloc(sizeof(*node), MALLOC_SHARED)) == NULL)\n        return NULL;\n    node->value = value;\n    if (list->len == 0) {\n        list->head = list->tail = node;\n        node->prev = node->next = NULL;\n    } else {\n        node->prev = list->tail;\n        node->next = NULL;\n        list->tail->next = node;\n        list->tail = node;\n    }\n    list->len++;\n    return list;\n}\n\nlist *listInsertNode(list *list, listNode *old_node, void *value, int after) {\n    listNode *node;\n\n    if ((node = zmalloc(sizeof(*node), MALLOC_SHARED)) == NULL)\n        return NULL;\n    node->value = value;\n    if (after) {\n        node->prev = old_node;\n        node->next = old_node->next;\n        if (list->tail == old_node) {\n            list->tail = node;\n        }\n    } else {\n        node->next = old_node;\n        node->prev = old_node->prev;\n        if (list->head == old_node) {\n            list->head = node;\n        }\n    }\n    if (node->prev != NULL) {\n        node->prev->next = node;\n    }\n    if (node->next != NULL) {\n        node->next->prev = node;\n    }\n    list->len++;\n    return list;\n}\n\n/* Remove the specified node from the specified list.\n * It's up to the caller to free the private value of the node.\n *\n * This function can't fail. */\nvoid listDelNode(list *list, listNode *node)\n{\n    if (node->prev)\n        node->prev->next = node->next;\n    else\n        list->head = node->next;\n    if (node->next)\n        node->next->prev = node->prev;\n    else\n        list->tail = node->prev;\n    if (list->free) list->free(node->value);\n    zfree(node);\n    list->len--;\n}\n\n/* Returns a list iterator 'iter'. After the initialization every\n * call to listNext() will return the next element of the list.\n *\n * This function can't fail. */\nlistIter *listGetIterator(list *list, int direction)\n{\n    listIter *iter;\n\n    if ((iter = zmalloc(sizeof(*iter), MALLOC_SHARED)) == NULL) return NULL;\n    if (direction == AL_START_HEAD)\n        iter->next = list->head;\n    else\n        iter->next = list->tail;\n    iter->direction = direction;\n    return iter;\n}\n\n/* Release the iterator memory */\nvoid listReleaseIterator(listIter *iter) {\n    zfree(iter);\n}\n\n/* Create an iterator in the list private iterator structure */\nvoid listRewind(list *list, listIter *li) {\n    li->next = list->head;\n    li->direction = AL_START_HEAD;\n}\n\nvoid listRewindTail(list *list, listIter *li) {\n    li->next = list->tail;\n    li->direction = AL_START_TAIL;\n}\n\n/* Return the next element of an iterator.\n * It's valid to remove the currently returned element using\n * listDelNode(), but not to remove other elements.\n *\n * The function returns a pointer to the next element of the list,\n * or NULL if there are no more elements, so the classical usage\n * pattern is:\n *\n * iter = listGetIterator(list,<direction>);\n * while ((node = listNext(iter)) != NULL) {\n *     doSomethingWith(listNodeValue(node));\n * }\n *\n * */\nlistNode *listNext(listIter *iter)\n{\n    listNode *current = iter->next;\n\n    if (current != NULL) {\n        if (iter->direction == AL_START_HEAD)\n            iter->next = current->next;\n        else\n            iter->next = current->prev;\n    }\n    return current;\n}\n\n/* Duplicate the whole list. On out of memory NULL is returned.\n * On success a copy of the original list is returned.\n *\n * The 'Dup' method set with listSetDupMethod() function is used\n * to copy the node value. Otherwise the same pointer value of\n * the original node is used as value of the copied node.\n *\n * The original list both on success or error is never modified. */\nlist *listDup(list *orig)\n{\n    list *copy;\n    listIter iter;\n    listNode *node;\n\n    if ((copy = listCreate()) == NULL)\n        return NULL;\n    copy->dup = orig->dup;\n    copy->free = orig->free;\n    copy->match = orig->match;\n    listRewind(orig, &iter);\n    while((node = listNext(&iter)) != NULL) {\n        void *value;\n\n        if (copy->dup) {\n            value = copy->dup(node->value);\n            if (value == NULL) {\n                listRelease(copy);\n                return NULL;\n            }\n        } else\n            value = node->value;\n        if (listAddNodeTail(copy, value) == NULL) {\n            listRelease(copy);\n            return NULL;\n        }\n    }\n    return copy;\n}\n\n/* Search the list for a node matching a given key.\n * The match is performed using the 'match' method\n * set with listSetMatchMethod(). If no 'match' method\n * is set, the 'value' pointer of every node is directly\n * compared with the 'key' pointer.\n *\n * On success the first matching node pointer is returned\n * (search starts from head). If no matching node exists\n * NULL is returned. */\nlistNode *listSearchKey(list *list, void *key)\n{\n    listIter iter;\n    listNode *node;\n\n    listRewind(list, &iter);\n    while((node = listNext(&iter)) != NULL) {\n        if (list->match) {\n            if (list->match(node->value, key)) {\n                return node;\n            }\n        } else {\n            if (key == node->value) {\n                return node;\n            }\n        }\n    }\n    return NULL;\n}\n\n/* Return the element at the specified zero-based index\n * where 0 is the head, 1 is the element next to head\n * and so on. Negative integers are used in order to count\n * from the tail, -1 is the last element, -2 the penultimate\n * and so on. If the index is out of range NULL is returned. */\nlistNode *listIndex(list *list, long index) {\n    listNode *n;\n\n    if (index < 0) {\n        index = (-index)-1;\n        n = list->tail;\n        while(index-- && n) n = n->prev;\n    } else {\n        n = list->head;\n        while(index-- && n) n = n->next;\n    }\n    return n;\n}\n\n/* Rotate the list removing the tail node and inserting it to the head. */\nvoid listRotateTailToHead(list *list) {\n    if (listLength(list) <= 1) return;\n\n    /* Detach current tail */\n    listNode *tail = list->tail;\n    list->tail = tail->prev;\n    list->tail->next = NULL;\n    /* Move it as head */\n    list->head->prev = tail;\n    tail->prev = NULL;\n    tail->next = list->head;\n    list->head = tail;\n}\n\n/* Rotate the list removing the head node and inserting it to the tail. */\nvoid listRotateHeadToTail(list *list) {\n    if (listLength(list) <= 1) return;\n\n    listNode *head = list->head;\n    /* Detach current head */\n    list->head = head->next;\n    list->head->prev = NULL;\n    /* Move it as tail */\n    list->tail->next = head;\n    head->next = NULL;\n    head->prev = list->tail;\n    list->tail = head;\n}\n\n/* Add all the elements of the list 'o' at the end of the\n * list 'l'. The list 'other' remains empty but otherwise valid. */\nvoid listJoin(list *l, list *o) {\n    if (o->len == 0) return;\n\n    o->head->prev = l->tail;\n\n    if (l->tail)\n        l->tail->next = o->head;\n    else\n        l->head = o->head;\n\n    l->tail = o->tail;\n    l->len += o->len;\n\n    /* Setup other as an empty list. */\n    o->head = o->tail = NULL;\n    o->len = 0;\n}\n"
  },
  {
    "path": "src/adlist.h",
    "content": "/* adlist.h - A generic doubly linked list implementation\n *\n * Copyright (c) 2006-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __ADLIST_H__\n#define __ADLIST_H__\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/* Node, List, and Iterator are the only data structures used currently. */\n\ntypedef struct listNode {\n    struct listNode *prev;\n    struct listNode *next;\n    void *value;\n} listNode;\n\ntypedef struct listIter {\n    listNode *next;\n    int direction;\n} listIter;\n\ntypedef struct list {\n    listNode *head;\n    listNode *tail;\n    void *(*dup)(void *ptr);\n    void (*free)(const void *ptr);\n    int (*match)(void *ptr, void *key);\n    unsigned long len;\n} list;\n\n/* Functions implemented as macros */\n#define listLength(l) ((l)->len)\n#define listFirst(l) ((l)->head)\n#define listLast(l) ((l)->tail)\n#define listPrevNode(n) ((n)->prev)\n#define listNextNode(n) ((n)->next)\n#define listNodeValue(n) ((n)->value)\n\n#define listSetDupMethod(l,m) ((l)->dup = (m))\n#define listSetFreeMethod(l,m) ((l)->free = (m))\n#define listSetMatchMethod(l,m) ((l)->match = (m))\n\n#define listGetDupMethod(l) ((l)->dup)\n#define listGetFreeMethod(l) ((l)->free)\n#define listGetMatchMethod(l) ((l)->match)\n\n/* Prototypes */\nlist *listCreate(void);\nvoid listRelease(list *list);\nvoid listEmpty(list *list);\nlist *listAddNodeHead(list *list, void *value);\nlist *listAddNodeTail(list *list, void *value);\nlist *listInsertNode(list *list, listNode *old_node, void *value, int after);\nvoid listDelNode(list *list, listNode *node);\nlistIter *listGetIterator(list *list, int direction);\nlistNode *listNext(listIter *iter);\nvoid listReleaseIterator(listIter *iter);\nlist *listDup(list *orig);\nlistNode *listSearchKey(list *list, void *key);\nlistNode *listIndex(list *list, long index);\nvoid listRewind(list *list, listIter *li);\nvoid listRewindTail(list *list, listIter *li);\nvoid listRotateTailToHead(list *list);\nvoid listRotateHeadToTail(list *list);\nvoid listJoin(list *l, list *o);\n\n/* Directions for iterators */\n#define AL_START_HEAD 0\n#define AL_START_TAIL 1\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif /* __ADLIST_H__ */\n"
  },
  {
    "path": "src/ae.cpp",
    "content": "/* A simple event-driven programming library. Originally I wrote this code\n * for the Jim's event-loop (Jim is a Tcl interpreter) but later translated\n * it in form of a library for easy reuse.\n *\n * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"ae.h\"\n#include \"anet.h\"\n#include \"fastlock.h\"\n\n#include <condition_variable>\n#include <atomic>\n#include <mutex>\n#include <stdio.h>\n#include <fcntl.h>\n#include <sys/time.h>\n#include <sys/types.h>\n#include <unistd.h>\n#include <stdlib.h>\n#include <poll.h>\n#include <string.h>\n#include <time.h>\n#include <errno.h>\n\n#include \"ae.h\"\n#include \"fastlock.h\"\n#include \"zmalloc.h\"\n#include \"config.h\"\n#include \"serverassert.h\"\n#include \"readwritelock.h\"\n\n#ifdef USE_MUTEX\nthread_local int cOwnLock = 0;\nclass mutex_wrapper\n{\n    std::recursive_mutex m_mutex;\npublic:\n    void lock() {\n        m_mutex.lock();\n        cOwnLock++;\n    }\n\n    void unlock() {\n        cOwnLock--;\n        m_mutex.unlock();\n    }\n\n    bool try_lock() {\n        if (m_mutex.try_lock()) {\n            cOwnLock++;\n            return true;\n        }\n        return false;\n    }\n\n    bool fOwnLock() {\n        return cOwnLock > 0;\n    }\n};\nmutex_wrapper g_lock;\n\n#else\nfastlock g_lock(\"AE (global)\");\n#endif\nreadWriteLock g_forkLock(\"Fork (global)\");\nthread_local aeEventLoop *g_eventLoopThisThread = NULL;\n\n/* Include the best multiplexing layer supported by this system.\n * The following should be ordered by performances, descending. */\n#ifdef HAVE_EVPORT\n#include \"ae_evport.c\"\n#else\n    #ifdef HAVE_EPOLL\n    #include \"ae_epoll.cpp\"\n    #else\n        #ifdef HAVE_KQUEUE\n        #include \"ae_kqueue.c\"\n        #else\n        #include \"ae_select.c\"\n        #endif\n    #endif\n#endif\n\nenum class AE_ASYNC_OP\n{\n    PostFunction,\n    PostCppFunction,\n    DeleteFileEvent,\n    CreateFileEvent,\n    PostAsynDBFunction,\n\n};\n\nstruct aeCommand\n{\n    AE_ASYNC_OP op;\n    int fd; \n    int mask;\n    bool fLock = true;\n    union {\n        aePostFunctionProc *proc;\n        aeFileProc *fproc;\n        std::function<void()> *pfn;\n        aePostFunctionTokenProc* tproc;\n\n    };\n    void *clientData;\n};\n#ifdef PIPE_BUF\nstatic_assert(sizeof(aeCommand) <= PIPE_BUF, \"aeCommand must be small enough to send atomically through a pipe\");\n#endif\n\nvoid aeProcessCmd(aeEventLoop *eventLoop, int fd, void *, int )\n{\n    std::unique_lock<decltype(g_lock)> ulock(g_lock, std::defer_lock);\n    aeCommand cmd;\n    for (;;)\n    {\n        auto cb = read(fd, &cmd, sizeof(aeCommand));\n        if (cb != sizeof(cmd))\n        {\n            serverAssert(errno == EAGAIN);\n            break;\n        }\n        switch (cmd.op)\n        {\n        case AE_ASYNC_OP::DeleteFileEvent:\n            aeDeleteFileEvent(eventLoop, cmd.fd, cmd.mask);\n            break;\n\n        case AE_ASYNC_OP::CreateFileEvent:\n            aeCreateFileEvent(eventLoop, cmd.fd, cmd.mask, cmd.fproc, cmd.clientData);\n            break;\n\n        case AE_ASYNC_OP::PostFunction:\n            {\n            if (cmd.fLock && !ulock.owns_lock()) {\n                g_forkLock.releaseRead();\n                ulock.lock();\n                g_forkLock.acquireRead();\n            }\n            ((aePostFunctionProc*)cmd.proc)(cmd.clientData);\n            break;\n            }\n\n        case AE_ASYNC_OP::PostCppFunction:\n        {\n            if (cmd.fLock && !ulock.owns_lock()) {\n                g_forkLock.releaseRead();\n                ulock.lock();\n                g_forkLock.acquireRead();\n            }\n            (*cmd.pfn)();\n\n            delete cmd.pfn;\n            break;\n        }\n        case AE_ASYNC_OP::PostAsynDBFunction:\n        {   //added to support async api IStorage\n           if (cmd.fLock && !ulock.owns_lock()) {\n                g_forkLock.releaseRead();\n                ulock.lock();\n                g_forkLock.acquireRead();\n            }  \n            ((aePostFunctionTokenProc*)cmd.tproc)(eventLoop,(StorageToken*)cmd.clientData);\n            break; \n        }\n            break;\n        }\n    }\n}\n\n// Unlike write() this is an all or nothing thing.  We will block if a partial write is hit\nssize_t safe_write(int fd, const void *pv, size_t cb)\n{\n    const char *pcb = (const char*)pv;\n    ssize_t written = 0;\n    do\n    {\n        ssize_t rval = write(fd, pcb, cb);\n        if (rval > 0)\n        {\n            pcb += rval;\n            cb -= rval;\n            written += rval;\n        }\n        else if (errno == EAGAIN)\n        {\n            if (written == 0)\n                break;\n            // if we've already written something then we're committed so keep trying\n        }\n        else\n        {\n            if (rval == 0)\n                return written;\n            return rval;\n        }\n    } while (cb);\n    return written;\n}\n\nint aeCreateRemoteFileEvent(aeEventLoop *eventLoop, int fd, int mask,\n        aeFileProc *proc, void *clientData)\n{\n    if (eventLoop == g_eventLoopThisThread)\n        return aeCreateFileEvent(eventLoop, fd, mask, proc, clientData);\n\n    int ret = AE_OK;\n    \n    aeCommand cmd;\n    cmd.op = AE_ASYNC_OP::CreateFileEvent;\n    cmd.fd = fd;\n    cmd.mask = mask;\n    cmd.fproc = proc;\n    cmd.clientData = clientData;\n    cmd.fLock = true;\n\n    auto size = safe_write(eventLoop->fdCmdWrite, &cmd, sizeof(cmd));\n    if (size != sizeof(cmd))\n    {\n        serverAssert(size == sizeof(cmd) || size <= 0);\n        serverAssert(errno == EAGAIN);\n        ret = AE_ERR;\n    }\n\n    return ret;\n}\n\nint aePostFunction(aeEventLoop *eventLoop, aePostFunctionProc *proc, void *arg)\n{\n    if (eventLoop == g_eventLoopThisThread)\n    {\n        proc(arg);\n        return AE_OK;\n    }\n    aeCommand cmd = {};\n    cmd.op = AE_ASYNC_OP::PostFunction;\n    cmd.proc = proc;\n    cmd.clientData = arg;\n    cmd.fLock = true;\n    auto size = write(eventLoop->fdCmdWrite, &cmd, sizeof(cmd));\n    if (size != sizeof(cmd))\n        return AE_ERR;\n    return AE_OK;\n}\n\nint aePostFunction(aeEventLoop *eventLoop, aePostFunctionTokenProc *proc, StorageToken *token)\n{\n  //added to support async api IStorage\n    aeCommand cmd = {};\n    cmd.op = AE_ASYNC_OP::PostAsynDBFunction; \n    cmd.tproc = proc;\n    cmd.clientData = (void*)token; \n    cmd.fLock = true;\n    auto size = write(eventLoop->fdCmdWrite, &cmd, sizeof(cmd));\n    if (size != sizeof(cmd))\n        return AE_ERR;\n    return AE_OK;\n}\n\nint aePostFunction(aeEventLoop *eventLoop, std::function<void()> fn, bool fLock, bool fForceQueue)\n{\n    if (eventLoop == g_eventLoopThisThread && !fForceQueue)\n    {\n        fn();\n        return AE_OK;\n    }\n\n    aeCommand cmd = {};\n    cmd.op = AE_ASYNC_OP::PostCppFunction;\n    cmd.pfn = new std::function<void()>(fn);\n    cmd.fLock = fLock;\n\n    auto size = write(eventLoop->fdCmdWrite, &cmd, sizeof(cmd));\n    if (!(!size || size == sizeof(cmd))) {\n        printf(\"Last error: %d\\n\", errno);\n    }\n    serverAssert(!size || size == sizeof(cmd));\n\n    if (size == 0)\n        return AE_ERR;\n\n    return AE_OK;\n}\n\naeEventLoop *aeCreateEventLoop(int setsize) {\n    aeEventLoop *eventLoop;\n    int i;\n\n    monotonicInit();    /* just in case the calling app didn't initialize */\n    \n    if ((eventLoop = (aeEventLoop*)zmalloc(sizeof(*eventLoop), MALLOC_LOCAL)) == NULL) goto err;\n    eventLoop->events = (aeFileEvent*)zmalloc(sizeof(aeFileEvent)*setsize, MALLOC_LOCAL);\n    eventLoop->fired = (aeFiredEvent*)zmalloc(sizeof(aeFiredEvent)*setsize, MALLOC_LOCAL);\n    if (eventLoop->events == NULL || eventLoop->fired == NULL) goto err;\n    eventLoop->setsize = setsize;\n    eventLoop->timeEventHead = NULL;\n    eventLoop->timeEventNextId = 0;\n    eventLoop->stop = 0;\n    eventLoop->maxfd = -1;\n    eventLoop->beforesleep = NULL;\n    eventLoop->aftersleep = NULL;\n    eventLoop->flags = 0;\n    if (aeApiCreate(eventLoop) == -1) goto err;\n    /* Events with mask == AE_NONE are not set. So let's initialize the\n     * vector with it. */\n    for (i = 0; i < setsize; i++)\n        eventLoop->events[i].mask = AE_NONE;\n\n    fastlock_init(&eventLoop->flock, \"event loop\");\n    int rgfd[2];\n    if (pipe(rgfd) < 0)\n        goto err;\n    eventLoop->fdCmdRead = rgfd[0];\n    eventLoop->fdCmdWrite = rgfd[1];\n    //fcntl(eventLoop->fdCmdWrite, F_SETFL, O_NONBLOCK);\n    fcntl(eventLoop->fdCmdRead, F_SETFL, O_NONBLOCK);\n    eventLoop->cevents = 0;\n    aeCreateFileEvent(eventLoop, eventLoop->fdCmdRead, AE_READABLE|AE_READ_THREADSAFE, aeProcessCmd, NULL);\n\n    return eventLoop;\n\nerr:\n    if (eventLoop) {\n        zfree(eventLoop->events);\n        zfree(eventLoop->fired);\n        zfree(eventLoop);\n    }\n    return NULL;\n}\n\n/* Return the current set size. */\nint aeGetSetSize(aeEventLoop *eventLoop) {\n    return eventLoop->setsize;\n}\n\n/* Return the current EventLoop. */\naeEventLoop *aeGetCurrentEventLoop(){\n    return g_eventLoopThisThread;\n}\n\n/* Tells the next iteration/s of the event processing to set timeout of 0. */\nvoid aeSetDontWait(aeEventLoop *eventLoop, int noWait) {\n    if (noWait)\n        eventLoop->flags |= AE_DONT_WAIT;\n    else\n        eventLoop->flags &= ~AE_DONT_WAIT;\n}\n\n/* Resize the maximum set size of the event loop.\n * If the requested set size is smaller than the current set size, but\n * there is already a file descriptor in use that is >= the requested\n * set size minus one, AE_ERR is returned and the operation is not\n * performed at all.\n *\n * Otherwise AE_OK is returned and the operation is successful. */\nint aeResizeSetSize(aeEventLoop *eventLoop, int setsize) {\n    serverAssert(g_eventLoopThisThread == NULL || g_eventLoopThisThread == eventLoop);\n    int i;\n\n    if (setsize == eventLoop->setsize) return AE_OK;\n    if (eventLoop->maxfd >= setsize) return AE_ERR;\n    if (aeApiResize(eventLoop,setsize) == -1) return AE_ERR;\n\n    eventLoop->events = (aeFileEvent*)zrealloc(eventLoop->events,sizeof(aeFileEvent)*setsize, MALLOC_LOCAL);\n    eventLoop->fired = (aeFiredEvent*)zrealloc(eventLoop->fired,sizeof(aeFiredEvent)*setsize, MALLOC_LOCAL);\n    eventLoop->setsize = setsize;\n\n    /* Make sure that if we created new slots, they are initialized with\n     * an AE_NONE mask. */\n    for (i = eventLoop->maxfd+1; i < setsize; i++)\n        eventLoop->events[i].mask = AE_NONE;\n    return AE_OK;\n}\n\nextern \"C\" void aeDeleteEventLoop(aeEventLoop *eventLoop) {\n    aeApiFree(eventLoop);\n    zfree(eventLoop->events);\n    zfree(eventLoop->fired);\n    fastlock_free(&eventLoop->flock);\n    close(eventLoop->fdCmdRead);\n    close(eventLoop->fdCmdWrite);\n\n    /* Free the time events list. */\n    auto *te = eventLoop->timeEventHead;\n    while (te)\n    {\n        auto *teNext = te->next;\n        zfree(te);\n        te = teNext;\n    }\n    zfree(eventLoop);\n}\n\nextern \"C\" void aeStop(aeEventLoop *eventLoop) {\n    serverAssert(g_eventLoopThisThread == NULL || g_eventLoopThisThread == eventLoop);\n    eventLoop->stop = 1;\n}\n\nextern \"C\" int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask,\n        aeFileProc *proc, void *clientData)\n{\n    serverAssert(g_eventLoopThisThread == NULL || g_eventLoopThisThread == eventLoop);\n    if (fd >= eventLoop->setsize) {\n        errno = ERANGE;\n        return AE_ERR;\n    }\n\n    aeFileEvent *fe = &eventLoop->events[fd];\n\n    if (aeApiAddEvent(eventLoop, fd, mask) == -1)\n        return AE_ERR;\n    fe->mask |= mask;\n    if (mask & AE_READABLE) fe->rfileProc = proc;\n    if (mask & AE_WRITABLE) fe->wfileProc = proc;\n    fe->clientData = clientData;\n    if (fd > eventLoop->maxfd)\n        eventLoop->maxfd = fd;\n    return AE_OK;\n}\n\nvoid aeDeleteFileEventAsync(aeEventLoop *eventLoop, int fd, int mask)\n{\n    if (eventLoop == g_eventLoopThisThread)\n        return aeDeleteFileEvent(eventLoop, fd, mask);\n    aeCommand cmd = {};\n    cmd.op = AE_ASYNC_OP::DeleteFileEvent;\n    cmd.fd = fd;\n    cmd.mask = mask;\n    cmd.fLock = true;\n    auto cb = write(eventLoop->fdCmdWrite, &cmd, sizeof(cmd));\n    serverAssert(cb == sizeof(cmd));\n}\n\nextern \"C\" void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask)\n{\n    serverAssert(g_eventLoopThisThread == NULL || g_eventLoopThisThread == eventLoop);\n    if (fd >= eventLoop->setsize) return;\n    aeFileEvent *fe = &eventLoop->events[fd];\n    if (fe->mask == AE_NONE) return;\n\n    /* We want to always remove AE_BARRIER if set when AE_WRITABLE\n     * is removed. */\n    if (mask & AE_WRITABLE) mask |= AE_BARRIER;\n\n    if (mask & AE_WRITABLE) mask |= AE_WRITE_THREADSAFE;\n    if (mask & AE_READABLE) mask |= AE_READ_THREADSAFE;\n\n    aeApiDelEvent(eventLoop, fd, mask);\n    fe->mask = fe->mask & (~mask);\n    if (fd == eventLoop->maxfd && fe->mask == AE_NONE) {\n        /* Update the max fd */\n        int j;\n\n        for (j = eventLoop->maxfd-1; j >= 0; j--)\n            if (eventLoop->events[j].mask != AE_NONE) break;\n        eventLoop->maxfd = j;\n    }\n}\n\nextern \"C\" int aeGetFileEvents(aeEventLoop *eventLoop, int fd) {\n    if (fd >= eventLoop->setsize) return 0;\n    aeFileEvent *fe = &eventLoop->events[fd];\n\n    return fe->mask;\n}\n\nextern \"C\" long long aeCreateTimeEvent(aeEventLoop *eventLoop, long long milliseconds,\n        aeTimeProc *proc, void *clientData,\n        aeEventFinalizerProc *finalizerProc)\n{\n    serverAssert(g_eventLoopThisThread == NULL || g_eventLoopThisThread == eventLoop);\n    long long id = eventLoop->timeEventNextId++;\n    aeTimeEvent *te;\n\n    te = (aeTimeEvent*)zmalloc(sizeof(*te), MALLOC_LOCAL);\n    if (te == NULL) return AE_ERR;\n    te->id = id;\n    te->when = getMonotonicUs() + milliseconds * 1000;\n    te->timeProc = proc;\n    te->finalizerProc = finalizerProc;\n    te->clientData = clientData;\n    te->prev = NULL;\n    te->next = eventLoop->timeEventHead;\n    te->refcount = 0;\n    if (te->next)\n        te->next->prev = te;\n    eventLoop->timeEventHead = te;\n    return id;\n}\n\nextern \"C\" int aeDeleteTimeEvent(aeEventLoop *eventLoop, long long id)\n{\n    serverAssert(g_eventLoopThisThread == NULL || g_eventLoopThisThread == eventLoop);\n    aeTimeEvent *te = eventLoop->timeEventHead;\n    while(te) {\n        if (te->id == id) {\n            te->id = AE_DELETED_EVENT_ID;\n            return AE_OK;\n        }\n        te = te->next;\n    }\n    return AE_ERR; /* NO event with the specified ID found */\n}\n\n/* How many microseconds until the first timer should fire.\n * If there are no timers, -1 is returned.\n *\n * Note that's O(N) since time events are unsorted.\n * Possible optimizations (not needed by Redis so far, but...):\n * 1) Insert the event in order, so that the nearest is just the head.\n *    Much better but still insertion or deletion of timers is O(N).\n * 2) Use a skiplist to have this operation as O(1) and insertion as O(log(N)).\n */\nstatic int64_t usUntilEarliestTimer(aeEventLoop *eventLoop) {\n    aeTimeEvent *te = eventLoop->timeEventHead;\n    if (te == NULL) return -1;\n\n    aeTimeEvent *earliest = NULL;\n    while (te) {\n        if (!earliest || te->when < earliest->when)\n            earliest = te;\n        te = te->next;\n    }\n\n    monotime now = getMonotonicUs();\n    return (now >= earliest->when) ? 0 : earliest->when - now;\n}\n\n/* Process time events */\nstatic int processTimeEvents(aeEventLoop *eventLoop) {\n    std::unique_lock<decltype(g_lock)> ulock(g_lock, std::defer_lock);\n    int processed = 0;\n    aeTimeEvent *te;\n    long long maxId;\n\n    te = eventLoop->timeEventHead;\n    maxId = eventLoop->timeEventNextId-1;\n    monotime now = getMonotonicUs();\n    while(te) {\n        long long id;\n\n        /* Remove events scheduled for deletion. */\n        if (te->id == AE_DELETED_EVENT_ID) {\n            aeTimeEvent *next = te->next;\n            /* If a reference exists for this timer event,\n             * don't free it. This is currently incremented\n             * for recursive timerProc calls */\n            if (te->refcount) {\n                te = next;\n                continue;\n            }\n            if (te->prev)\n                te->prev->next = te->next;\n            else\n                eventLoop->timeEventHead = te->next;\n            if (te->next)\n                te->next->prev = te->prev;\n            if (te->finalizerProc) {\n                if (!ulock.owns_lock()) {\n                    g_forkLock.releaseRead();\n                    ulock.lock();\n                    g_forkLock.acquireRead();\n                }\n                te->finalizerProc(eventLoop, te->clientData);\n                now = getMonotonicUs();\n            }\n            zfree(te);\n            te = next;\n            continue;\n        }\n\n        /* Make sure we don't process time events created by time events in\n         * this iteration. Note that this check is currently useless: we always\n         * add new timers on the head, however if we change the implementation\n         * detail, this check may be useful again: we keep it here for future\n         * defense. */\n        if (te->id > maxId) {\n            te = te->next;\n            continue;\n        }\n\n        if (te->when <= now) {\n            if (!ulock.owns_lock()) {\n                g_forkLock.releaseRead();\n                ulock.lock();\n                g_forkLock.acquireRead();\n            }\n            int retval;\n\n            id = te->id;\n            te->refcount++;\n            retval = te->timeProc(eventLoop, id, te->clientData);\n            te->refcount--;\n            processed++;\n            now = getMonotonicUs();\n            if (retval != AE_NOMORE) {\n                te->when = now + retval * 1000;\n            } else {\n                te->id = AE_DELETED_EVENT_ID;\n            }\n        }\n        te = te->next;\n    }\n    return processed;\n}\n\nextern \"C\" void ProcessEventCore(aeEventLoop *eventLoop, aeFileEvent *fe, int mask, int fd)\n{\n#define LOCK_IF_NECESSARY(fe, tsmask) \\\n    std::unique_lock<decltype(g_lock)> ulock(g_lock, std::defer_lock); \\\n    if (!(fe->mask & tsmask)) { \\\n        g_forkLock.releaseRead(); \\\n        ulock.lock(); \\\n        g_forkLock.acquireRead(); \\\n    }\n\n    int fired = 0; /* Number of events fired for current fd. */\n\n    /* Normally we execute the readable event first, and the writable\n    * event laster. This is useful as sometimes we may be able\n    * to serve the reply of a query immediately after processing the\n    * query.\n    *\n    * However if AE_BARRIER is set in the mask, our application is\n    * asking us to do the reverse: never fire the writable event\n    * after the readable. In such a case, we invert the calls.\n    * This is useful when, for instance, we want to do things\n    * in the beforeSleep() hook, like fsynching a file to disk,\n    * before replying to a client. */\n    int invert = fe->mask & AE_BARRIER;\n\n    /* Note the \"fe->mask & mask & ...\" code: maybe an already\n        * processed event removed an element that fired and we still\n        * didn't processed, so we check if the event is still valid.\n        *\n        * Fire the readable event if the call sequence is not\n        * inverted. */\n    if (!invert && fe->mask & mask & AE_READABLE) {\n        LOCK_IF_NECESSARY(fe, AE_READ_THREADSAFE);\n        fe->rfileProc(eventLoop,fd,fe->clientData,mask | (fe->mask & AE_READ_THREADSAFE));\n        fired++;\n        fe = &eventLoop->events[fd]; /* Refresh in case of resize. */\n    }\n\n    /* Fire the writable event. */\n    if (fe->mask & mask & AE_WRITABLE) {\n        if (!fired || fe->wfileProc != fe->rfileProc) {\n            LOCK_IF_NECESSARY(fe, AE_WRITE_THREADSAFE);\n            fe->wfileProc(eventLoop,fd,fe->clientData,mask | (fe->mask & AE_WRITE_THREADSAFE));\n            fired++;\n        }\n    }\n\n    /* If we have to invert the call, fire the readable event now\n        * after the writable one. */\n    if (invert && fe->mask & mask & AE_READABLE) {\n        if (!fired || fe->wfileProc != fe->rfileProc) {\n            LOCK_IF_NECESSARY(fe, AE_READ_THREADSAFE);\n            fe->rfileProc(eventLoop,fd,fe->clientData,mask | (fe->mask & AE_READ_THREADSAFE));\n            fired++;\n        }\n    }\n\n#undef LOCK_IF_NECESSARY\n}\n\n/* Process every pending time event, then every pending file event\n * (that may be registered by time event callbacks just processed).\n * Without special flags the function sleeps until some file event\n * fires, or when the next time event occurs (if any).\n *\n * If flags is 0, the function does nothing and returns.\n * if flags has AE_ALL_EVENTS set, all the kind of events are processed.\n * if flags has AE_FILE_EVENTS set, file events are processed.\n * if flags has AE_TIME_EVENTS set, time events are processed.\n * if flags has AE_DONT_WAIT set the function returns ASAP until all\n * the events that's possible to process without to wait are processed.\n * if flags has AE_CALL_AFTER_SLEEP set, the aftersleep callback is called.\n * if flags has AE_CALL_BEFORE_SLEEP set, the beforesleep callback is called.\n *\n * The function returns the number of events processed. */\nint aeProcessEvents(aeEventLoop *eventLoop, int flags)\n{\n    serverAssert(g_eventLoopThisThread == NULL || g_eventLoopThisThread == eventLoop);\n    int processed = 0, numevents;\n\n    /* Nothing to do? return ASAP */\n    if (!(flags & AE_TIME_EVENTS) && !(flags & AE_FILE_EVENTS)) return 0;\n\n    /* Note that we want to call select() even if there are no\n     * file events to process as long as we want to process time\n     * events, in order to sleep until the next time event is ready\n     * to fire. */\n    if (eventLoop->maxfd != -1 ||\n        ((flags & AE_TIME_EVENTS) && !(flags & AE_DONT_WAIT))) {\n        int j;\n        struct timeval tv, *tvp;\n        int64_t usUntilTimer = -1;\n\n        if (flags & AE_TIME_EVENTS && !(flags & AE_DONT_WAIT))\n            usUntilTimer = usUntilEarliestTimer(eventLoop);\n\n        if (usUntilTimer >= 0) {\n            tv.tv_sec = usUntilTimer / 1000000;\n            tv.tv_usec = usUntilTimer % 1000000;\n            tvp = &tv;\n        } else {\n            /* If we have to check for events but need to return\n             * ASAP because of AE_DONT_WAIT we need to set the timeout\n             * to zero */\n            if (flags & AE_DONT_WAIT) {\n                tv.tv_sec = tv.tv_usec = 0;\n                tvp = &tv;\n            } else {\n                /* Otherwise we can block */\n                tvp = NULL; /* wait forever */\n            }\n        }\n\n        if (eventLoop->beforesleep != NULL && flags & AE_CALL_BEFORE_SLEEP) {\n            std::unique_lock<decltype(g_lock)> ulock(g_lock, std::defer_lock);\n            if (!(eventLoop->beforesleepFlags & AE_SLEEP_THREADSAFE)) {\n                g_forkLock.releaseRead();\n                ulock.lock();\n                g_forkLock.acquireRead();\n            }\n            eventLoop->beforesleep(eventLoop);\n        }\n\n        if (eventLoop->flags & AE_DONT_WAIT) {\n            tv.tv_sec = tv.tv_usec = 0;\n            tvp = &tv;\n        }\n\n        /* Call the multiplexing API, will return only on timeout or when\n         * some event fires. */\n        numevents = aeApiPoll(eventLoop, tvp);\n\n        /* After sleep callback. */\n        if (eventLoop->aftersleep != NULL && flags & AE_CALL_AFTER_SLEEP) {\n            std::unique_lock<decltype(g_lock)> ulock(g_lock, std::defer_lock);\n            if (!(eventLoop->aftersleepFlags & AE_SLEEP_THREADSAFE)) {\n                g_forkLock.releaseRead();\n                ulock.lock();\n                g_forkLock.acquireRead();\n            }\n            eventLoop->aftersleep(eventLoop);\n        }\n\n        for (j = 0; j < numevents; j++) {\n            aeFileEvent *fe = &eventLoop->events[eventLoop->fired[j].fd];\n            int mask = eventLoop->fired[j].mask;\n            int fd = eventLoop->fired[j].fd;\n\n            ProcessEventCore(eventLoop, fe, mask, fd);\n\n            processed++;\n        }\n    }\n    /* Check time events */\n    if (flags & AE_TIME_EVENTS)\n        processed += processTimeEvents(eventLoop);\n\n    eventLoop->cevents += processed;\n    return processed; /* return the number of processed file/time events */\n}\n\n/* Wait for milliseconds until the given file descriptor becomes\n * writable/readable/exception */\nint aeWait(int fd, int mask, long long milliseconds) {\n    struct pollfd pfd;\n    int retmask = 0, retval;\n\n    memset(&pfd, 0, sizeof(pfd));\n    pfd.fd = fd;\n    if (mask & AE_READABLE) pfd.events |= POLLIN;\n    if (mask & AE_WRITABLE) pfd.events |= POLLOUT;\n\n    if ((retval = poll(&pfd, 1, milliseconds))== 1) {\n        if (pfd.revents & POLLIN) retmask |= AE_READABLE;\n        if (pfd.revents & POLLOUT) retmask |= AE_WRITABLE;\n        if (pfd.revents & POLLERR) retmask |= AE_WRITABLE;\n        if (pfd.revents & POLLHUP) retmask |= AE_WRITABLE;\n        return retmask;\n    } else {\n        return retval;\n    }\n}\n\nvoid aeMain(aeEventLoop *eventLoop) {\n    eventLoop->stop = 0;\n    g_eventLoopThisThread = eventLoop;\n    while (!eventLoop->stop) {\n        serverAssert(!aeThreadOwnsLock()); // we should have relinquished it after processing\n        aeProcessEvents(eventLoop, AE_ALL_EVENTS|AE_CALL_BEFORE_SLEEP|AE_CALL_AFTER_SLEEP);\n        serverAssert(!aeThreadOwnsLock()); // we should have relinquished it after processing\n    }\n}\n\nconst char *aeGetApiName(void) {\n    return aeApiName();\n}\n\nvoid aeSetBeforeSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *beforesleep, int flags) {\n    eventLoop->beforesleep = beforesleep;\n    eventLoop->beforesleepFlags = flags;\n}\n\nvoid aeSetAfterSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *aftersleep, int flags) {\n    eventLoop->aftersleep = aftersleep;\n    eventLoop->aftersleepFlags = flags;\n}\n\nthread_local spin_worker tl_worker = nullptr;\nthread_local bool fOwnLockOverride = false;\nvoid setAeLockSetThreadSpinWorker(spin_worker worker)\n{\n    tl_worker = worker;\n}\n\nvoid aeThreadOnline()\n{\n    g_forkLock.acquireRead();\n}\n\nvoid aeAcquireLock()\n{\n    g_forkLock.releaseRead();\n    g_lock.lock(tl_worker);\n    g_forkLock.acquireRead();\n}\n\nvoid aeAcquireForkLock()\n{\n    g_forkLock.upgradeWrite();\n}\n\nint aeTryAcquireLock(int fWeak)\n{\n    return g_lock.try_lock(!!fWeak);\n}\n\nvoid aeThreadOffline()\n{\n    g_forkLock.releaseRead();\n}\n\nvoid aeReleaseLock()\n{\n    g_lock.unlock();\n}\n\nvoid aeSetThreadOwnsLockOverride(int fOverride)\n{\n    fOwnLockOverride = fOverride;\n}\n\nvoid aeReleaseForkLock()\n{\n    g_forkLock.downgradeWrite();\n}\n\nvoid aeForkLockInChild()\n{\n    g_forkLock.setMulti(false);\n}\n\nint aeThreadOwnsLock()\n{\n    return fOwnLockOverride || g_lock.fOwnLock();\n}\n\nint aeLockContested(int threshold)\n{\n    ticket ticketT;\n    __atomic_load(&g_lock.m_ticket.u, &ticketT.u, __ATOMIC_RELAXED);\n    return ticketT.m_active < static_cast<uint16_t>(ticketT.m_avail - threshold);\n}\n\nint aeLockContention()\n{\n    ticket ticketT;\n    __atomic_load(&g_lock.m_ticket.u, &ticketT.u, __ATOMIC_RELAXED);\n    int32_t avail = ticketT.m_avail;\n    int32_t active = ticketT.m_active;\n    if (avail < active)\n        avail += 0x10000;\n    return avail - active;\n}\n\nvoid aeClosePipesForForkChild(aeEventLoop *el)\n{\n    close(el->fdCmdRead);\n    el->fdCmdRead = -1;\n    close(el->fdCmdWrite);\n    el->fdCmdWrite = -1;\n}\n"
  },
  {
    "path": "src/ae.h",
    "content": "/* A simple event-driven programming library. Originally I wrote this code\n * for the Jim's event-loop (Jim is a Tcl interpreter) but later translated\n * it in form of a library for easy reuse.\n *\n * Copyright (c) 2006-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __AE_H__\n#define __AE_H__\n\n#ifdef __cplusplus\n#include <functional>\n#endif\n#include \"monotonic.h\"\n#include \"fastlock.h\"\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#define AE_OK 0\n#define AE_ERR -1\n\n#define AE_NONE 0       /* No events registered. */\n#define AE_READABLE 1   /* Fire when descriptor is readable. */\n#define AE_WRITABLE 2   /* Fire when descriptor is writable. */\n#define AE_BARRIER 4    /* With WRITABLE, never fire the event if the\n                           READABLE event already fired in the same event\n                           loop iteration. Useful when you want to persist\n                           things to disk before sending replies, and want\n                           to do that in a group fashion. */\n#define AE_READ_THREADSAFE 8\n#define AE_WRITE_THREADSAFE 16\n#define AE_SLEEP_THREADSAFE 32\n\n#define AE_FILE_EVENTS (1<<0)\n#define AE_TIME_EVENTS (1<<1)\n#define AE_ALL_EVENTS (AE_FILE_EVENTS|AE_TIME_EVENTS)\n#define AE_DONT_WAIT (1<<2)\n#define AE_CALL_BEFORE_SLEEP (1<<3)\n#define AE_CALL_AFTER_SLEEP (1<<4)\n\n#define AE_NOMORE -1\n#define AE_DELETED_EVENT_ID -1\n\n/* Macros */\n#define AE_NOTUSED(V) ((void) V)\n\nstruct aeEventLoop;\n/* Types and data structures */\ntypedef void aeFileProc(struct aeEventLoop *eventLoop, int fd, void *clientData, int mask);\ntypedef int aeTimeProc(struct aeEventLoop *eventLoop, long long id, void *clientData);\ntypedef void aeEventFinalizerProc(struct aeEventLoop *eventLoop, void *clientData);\ntypedef void aeBeforeSleepProc(struct aeEventLoop *eventLoop);\ntypedef void aePostFunctionProc(void *pvArgs);\n//added to support async api IStorage\nstruct StorageToken;\ntypedef void aePostFunctionTokenProc(struct aeEventLoop *el, struct StorageToken *token);\n\n/* File event structure */\ntypedef struct aeFileEvent {\n    int mask; /* one of AE_(READABLE|WRITABLE|BARRIER) */\n    aeFileProc *rfileProc;\n    aeFileProc *wfileProc;\n    void *clientData;\n} aeFileEvent;\n\n/* Time event structure */\ntypedef struct aeTimeEvent {\n    long long id; /* time event identifier. */\n    monotime when;\n    aeTimeProc *timeProc;\n    aeEventFinalizerProc *finalizerProc;\n    void *clientData;\n    struct aeTimeEvent *prev;\n    struct aeTimeEvent *next;\n    int refcount; /* refcount to prevent timer events from being\n  \t\t   * freed in recursive time event calls. */\n} aeTimeEvent;\n\n/* A fired event */\ntypedef struct aeFiredEvent {\n    int fd;\n    int mask;\n} aeFiredEvent;\n\n/* State of an event based program */\ntypedef struct aeEventLoop {\n    int maxfd;   /* highest file descriptor currently registered */\n    int setsize; /* max number of file descriptors tracked */\n    long long timeEventNextId;\n    aeFileEvent *events; /* Registered events */\n    aeFiredEvent *fired; /* Fired events */\n    aeTimeEvent *timeEventHead;\n    int stop;\n    void *apidata; /* This is used for polling API specific data */\n    aeBeforeSleepProc *beforesleep;\n    int beforesleepFlags;\n    aeBeforeSleepProc *aftersleep;\n    int aftersleepFlags;\n    struct fastlock flock;\n    int fdCmdWrite;\n    int fdCmdRead;\n    int cevents;\n    int flags;\n} aeEventLoop;\n\n/* Prototypes */\naeEventLoop *aeCreateEventLoop(int setsize);\nint aePostFunction(aeEventLoop *eventLoop, aePostFunctionProc *proc, void *arg);\n#ifdef __cplusplus\n}   // EXTERN C\nint aePostFunction(aeEventLoop *eventLoop, std::function<void()> fn, bool fLock = true, bool fForceQueue = false);\n//added to support async api IStorage\nint aePostFunction(aeEventLoop *eventLoop, aePostFunctionTokenProc *proc, StorageToken *token);\nextern \"C\" {\n#endif\nvoid aeDeleteEventLoop(aeEventLoop *eventLoop);\nvoid aeStop(aeEventLoop *eventLoop);\nint aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask,\n        aeFileProc *proc, void *clientData);\n\nint aeCreateRemoteFileEvent(aeEventLoop *eventLoop, int fd, int mask,\n        aeFileProc *proc, void *clientData);\n\nvoid aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask);\nvoid aeDeleteFileEventAsync(aeEventLoop *eventLoop, int fd, int mask);\nint aeGetFileEvents(aeEventLoop *eventLoop, int fd);\nlong long aeCreateTimeEvent(aeEventLoop *eventLoop, long long milliseconds,\n        aeTimeProc *proc, void *clientData,\n        aeEventFinalizerProc *finalizerProc);\nint aeDeleteTimeEvent(aeEventLoop *eventLoop, long long id);\nint aeProcessEvents(aeEventLoop *eventLoop, int flags);\nint aeWait(int fd, int mask, long long milliseconds);\nvoid aeMain(aeEventLoop *eventLoop);\nconst char *aeGetApiName(void);\nvoid aeSetBeforeSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *beforesleep, int flags);\nvoid aeSetAfterSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *aftersleep, int flags);\nint aeGetSetSize(aeEventLoop *eventLoop);\naeEventLoop *aeGetCurrentEventLoop();\nint aeResizeSetSize(aeEventLoop *eventLoop, int setsize);\nvoid aeSetDontWait(aeEventLoop *eventLoop, int noWait);\nvoid aeClosePipesForForkChild(aeEventLoop *eventLoop);\n\nvoid setAeLockSetThreadSpinWorker(spin_worker worker);\nvoid aeThreadOnline();\nvoid aeAcquireLock();\nvoid aeAcquireForkLock();\nint aeTryAcquireLock(int fWeak);\nvoid aeThreadOffline();\nvoid aeReleaseLock();\nvoid aeReleaseForkLock();\nvoid aeForkLockInChild();\nint aeThreadOwnsLock();\nvoid aeSetThreadOwnsLockOverride(int fOverride);\nint aeLockContested(int threshold);\nint aeLockContention(); // returns the number of instantaneous threads waiting on the lock\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n"
  },
  {
    "path": "src/ae_epoll.cpp",
    "content": "/* Linux epoll(2) based ae.c module\n *\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n\n#include <sys/epoll.h>\n\ntypedef struct aeApiState {\n    int epfd;\n    struct epoll_event *events;\n} aeApiState;\n\nstatic int aeApiCreate(aeEventLoop *eventLoop) {\n    aeApiState *state = (aeApiState*)zmalloc(sizeof(aeApiState), MALLOC_LOCAL);\n\n    if (!state) return -1;\n    state->events = (epoll_event*)zmalloc(sizeof(struct epoll_event)*eventLoop->setsize, MALLOC_LOCAL);\n    if (!state->events) {\n        zfree(state);\n        return -1;\n    }\n    state->epfd = epoll_create(1024); /* 1024 is just a hint for the kernel */\n    if (state->epfd == -1) {\n        zfree(state->events);\n        zfree(state);\n        return -1;\n    }\n    anetCloexec(state->epfd);\n    eventLoop->apidata = state;\n    return 0;\n}\n\nstatic int aeApiResize(aeEventLoop *eventLoop, int setsize) {\n    aeApiState *state = (aeApiState*)eventLoop->apidata;\n\n    state->events = (epoll_event*)zrealloc(state->events, sizeof(struct epoll_event)*setsize, MALLOC_LOCAL);\n    return 0;\n}\n\nstatic void aeApiFree(aeEventLoop *eventLoop) {\n    aeApiState *state = (aeApiState*)eventLoop->apidata;\n\n    close(state->epfd);\n    zfree(state->events);\n    zfree(state);\n}\n\nstatic int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {\n    aeApiState *state = (aeApiState*)eventLoop->apidata;\n    struct epoll_event ee = {0}; /* avoid valgrind warning */\n    /* If the fd was already monitored for some event, we need a MOD\n     * operation. Otherwise we need an ADD operation. */\n    int op = eventLoop->events[fd].mask == AE_NONE ?\n            EPOLL_CTL_ADD : EPOLL_CTL_MOD;\n\n    ee.events = 0;\n    mask |= eventLoop->events[fd].mask; /* Merge old events */\n    if (mask & AE_READABLE) ee.events |= EPOLLIN;\n    if (mask & AE_WRITABLE) ee.events |= EPOLLOUT;\n    ee.data.fd = fd;\n    if (epoll_ctl(state->epfd,op,fd,&ee) == -1)\n    {\n        perror(\"epoll_ctl failed\");\n        return -1;\n    }\n    return 0;\n}\n\nstatic void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int delmask) {\n    aeApiState *state = (aeApiState*)eventLoop->apidata;\n    struct epoll_event ee = {0}; /* avoid valgrind warning */\n    int mask = eventLoop->events[fd].mask & (~delmask);\n\n    ee.events = 0;\n    if (mask & AE_READABLE) ee.events |= EPOLLIN;\n    if (mask & AE_WRITABLE) ee.events |= EPOLLOUT;\n    ee.data.fd = fd;\n    if (mask != AE_NONE) {\n        epoll_ctl(state->epfd,EPOLL_CTL_MOD,fd,&ee);\n    } else {\n        /* Note, Kernel < 2.6.9 requires a non null event pointer even for\n         * EPOLL_CTL_DEL. */\n        epoll_ctl(state->epfd,EPOLL_CTL_DEL,fd,&ee);\n    }\n}\n\nstatic int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {\n    aeApiState *state = (aeApiState*)eventLoop->apidata;\n    int retval, numevents = 0;\n\n    retval = epoll_wait(state->epfd,state->events,eventLoop->setsize,\n            tvp ? (tvp->tv_sec*1000 + (tvp->tv_usec + 999)/1000) : -1);\n    if (retval > 0) {\n        int j;\n\n        numevents = retval;\n        for (j = 0; j < numevents; j++) {\n            int mask = 0;\n            struct epoll_event *e = state->events+j;\n\n            if (e->events & EPOLLIN) mask |= AE_READABLE;\n            if (e->events & EPOLLOUT) mask |= AE_WRITABLE;\n            if (e->events & EPOLLERR) mask |= AE_WRITABLE|AE_READABLE;\n            if (e->events & EPOLLHUP) mask |= AE_WRITABLE|AE_READABLE;\n            eventLoop->fired[j].fd = e->data.fd;\n            eventLoop->fired[j].mask = mask;\n        }\n    }\n    return numevents;\n}\n\nstatic const char *aeApiName(void) {\n    return \"epoll\";\n}\n"
  },
  {
    "path": "src/ae_evport.c",
    "content": "/* ae.c module for illumos event ports.\n *\n * Copyright (c) 2012, Joyent, Inc. All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n\n#include <assert.h>\n#include <errno.h>\n#include <port.h>\n#include <poll.h>\n\n#include <sys/types.h>\n#include <sys/time.h>\n\n#include <stdio.h>\n\nstatic int evport_debug = 0;\n\n/*\n * This file implements the ae API using event ports, present on Solaris-based\n * systems since Solaris 10.  Using the event port interface, we associate file\n * descriptors with the port.  Each association also includes the set of poll(2)\n * events that the consumer is interested in (e.g., POLLIN and POLLOUT).\n *\n * There's one tricky piece to this implementation: when we return events via\n * aeApiPoll, the corresponding file descriptors become dissociated from the\n * port.  This is necessary because poll events are level-triggered, so if the\n * fd didn't become dissociated, it would immediately fire another event since\n * the underlying state hasn't changed yet.  We must re-associate the file\n * descriptor, but only after we know that our caller has actually read from it.\n * The ae API does not tell us exactly when that happens, but we do know that\n * it must happen by the time aeApiPoll is called again.  Our solution is to\n * keep track of the last fds returned by aeApiPoll and re-associate them next\n * time aeApiPoll is invoked.\n *\n * To summarize, in this module, each fd association is EITHER (a) represented\n * only via the in-kernel association OR (b) represented by pending_fds and\n * pending_masks.  (b) is only true for the last fds we returned from aeApiPoll,\n * and only until we enter aeApiPoll again (at which point we restore the\n * in-kernel association).\n */\n#define MAX_EVENT_BATCHSZ 512\n\ntypedef struct aeApiState {\n    int     portfd;                             /* event port */\n    uint_t  npending;                           /* # of pending fds */\n    int     pending_fds[MAX_EVENT_BATCHSZ];     /* pending fds */\n    int     pending_masks[MAX_EVENT_BATCHSZ];   /* pending fds' masks */\n} aeApiState;\n\nstatic int aeApiCreate(aeEventLoop *eventLoop) {\n    int i;\n    aeApiState *state = zmalloc(sizeof(aeApiState), MALLOC_LOCAL);\n    if (!state) return -1;\n\n    state->portfd = port_create();\n    if (state->portfd == -1) {\n        zfree(state);\n        return -1;\n    }\n    anetCloexec(state->portfd);\n\n    state->npending = 0;\n\n    for (i = 0; i < MAX_EVENT_BATCHSZ; i++) {\n        state->pending_fds[i] = -1;\n        state->pending_masks[i] = AE_NONE;\n    }\n\n    eventLoop->apidata = state;\n    return 0;\n}\n\nstatic int aeApiResize(aeEventLoop *eventLoop, int setsize) {\n    (void) eventLoop;\n    (void) setsize;\n    /* Nothing to resize here. */\n    return 0;\n}\n\nstatic void aeApiFree(aeEventLoop *eventLoop) {\n    aeApiState *state = eventLoop->apidata;\n\n    close(state->portfd);\n    zfree(state);\n}\n\nstatic int aeApiLookupPending(aeApiState *state, int fd) {\n    uint_t i;\n\n    for (i = 0; i < state->npending; i++) {\n        if (state->pending_fds[i] == fd)\n            return (i);\n    }\n\n    return (-1);\n}\n\n/*\n * Helper function to invoke port_associate for the given fd and mask.\n */\nstatic int aeApiAssociate(const char *where, int portfd, int fd, int mask) {\n    int events = 0;\n    int rv, err;\n\n    if (mask & AE_READABLE)\n        events |= POLLIN;\n    if (mask & AE_WRITABLE)\n        events |= POLLOUT;\n\n    if (evport_debug)\n        fprintf(stderr, \"%s: port_associate(%d, 0x%x) = \", where, fd, events);\n\n    rv = port_associate(portfd, PORT_SOURCE_FD, fd, events,\n        (void *)(uintptr_t)mask);\n    err = errno;\n\n    if (evport_debug)\n        fprintf(stderr, \"%d (%s)\\n\", rv, rv == 0 ? \"no error\" : strerror(err));\n\n    if (rv == -1) {\n        fprintf(stderr, \"%s: port_associate: %s\\n\", where, strerror(err));\n\n        if (err == EAGAIN)\n            fprintf(stderr, \"aeApiAssociate: event port limit exceeded.\");\n    }\n\n    return rv;\n}\n\nstatic int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {\n    aeApiState *state = eventLoop->apidata;\n    int fullmask, pfd;\n\n    if (evport_debug)\n        fprintf(stderr, \"aeApiAddEvent: fd %d mask 0x%x\\n\", fd, mask);\n\n    /*\n     * Since port_associate's \"events\" argument replaces any existing events, we\n     * must be sure to include whatever events are already associated when\n     * we call port_associate() again.\n     */\n    fullmask = mask | eventLoop->events[fd].mask;\n    pfd = aeApiLookupPending(state, fd);\n\n    if (pfd != -1) {\n        /*\n         * This fd was recently returned from aeApiPoll.  It should be safe to\n         * assume that the consumer has processed that poll event, but we play\n         * it safer by simply updating pending_mask.  The fd will be\n         * re-associated as usual when aeApiPoll is called again.\n         */\n        if (evport_debug)\n            fprintf(stderr, \"aeApiAddEvent: adding to pending fd %d\\n\", fd);\n        state->pending_masks[pfd] |= fullmask;\n        return 0;\n    }\n\n    return (aeApiAssociate(\"aeApiAddEvent\", state->portfd, fd, fullmask));\n}\n\nstatic void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int mask) {\n    aeApiState *state = eventLoop->apidata;\n    int fullmask, pfd;\n\n    if (evport_debug)\n        fprintf(stderr, \"del fd %d mask 0x%x\\n\", fd, mask);\n\n    pfd = aeApiLookupPending(state, fd);\n\n    if (pfd != -1) {\n        if (evport_debug)\n            fprintf(stderr, \"deleting event from pending fd %d\\n\", fd);\n\n        /*\n         * This fd was just returned from aeApiPoll, so it's not currently\n         * associated with the port.  All we need to do is update\n         * pending_mask appropriately.\n         */\n        state->pending_masks[pfd] &= ~mask;\n\n        if (state->pending_masks[pfd] == AE_NONE)\n            state->pending_fds[pfd] = -1;\n\n        return;\n    }\n\n    /*\n     * The fd is currently associated with the port.  Like with the add case\n     * above, we must look at the full mask for the file descriptor before\n     * updating that association.  We don't have a good way of knowing what the\n     * events are without looking into the eventLoop state directly.  We rely on\n     * the fact that our caller has already updated the mask in the eventLoop.\n     */\n\n    fullmask = eventLoop->events[fd].mask;\n    if (fullmask == AE_NONE) {\n        /*\n         * We're removing *all* events, so use port_dissociate to remove the\n         * association completely.  Failure here indicates a bug.\n         */\n        if (evport_debug)\n            fprintf(stderr, \"aeApiDelEvent: port_dissociate(%d)\\n\", fd);\n\n        if (port_dissociate(state->portfd, PORT_SOURCE_FD, fd) != 0) {\n            perror(\"aeApiDelEvent: port_dissociate\");\n            abort(); /* will not return */\n        }\n    } else if (aeApiAssociate(\"aeApiDelEvent\", state->portfd, fd,\n        fullmask) != 0) {\n        /*\n         * ENOMEM is a potentially transient condition, but the kernel won't\n         * generally return it unless things are really bad.  EAGAIN indicates\n         * we've reached a resource limit, for which it doesn't make sense to\n         * retry (counter-intuitively).  All other errors indicate a bug.  In any\n         * of these cases, the best we can do is to abort.\n         */\n        abort(); /* will not return */\n    }\n}\n\nstatic int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {\n    aeApiState *state = eventLoop->apidata;\n    struct timespec timeout, *tsp;\n    uint_t mask, i;\n    uint_t nevents;\n    port_event_t event[MAX_EVENT_BATCHSZ];\n\n    /*\n     * If we've returned fd events before, we must re-associate them with the\n     * port now, before calling port_get().  See the block comment at the top of\n     * this file for an explanation of why.\n     */\n    for (i = 0; i < state->npending; i++) {\n        if (state->pending_fds[i] == -1)\n            /* This fd has since been deleted. */\n            continue;\n\n        if (aeApiAssociate(\"aeApiPoll\", state->portfd,\n            state->pending_fds[i], state->pending_masks[i]) != 0) {\n            /* See aeApiDelEvent for why this case is fatal. */\n            abort();\n        }\n\n        state->pending_masks[i] = AE_NONE;\n        state->pending_fds[i] = -1;\n    }\n\n    state->npending = 0;\n\n    if (tvp != NULL) {\n        timeout.tv_sec = tvp->tv_sec;\n        timeout.tv_nsec = tvp->tv_usec * 1000;\n        tsp = &timeout;\n    } else {\n        tsp = NULL;\n    }\n\n    /*\n     * port_getn can return with errno == ETIME having returned some events (!).\n     * So if we get ETIME, we check nevents, too.\n     */\n    nevents = 1;\n    if (port_getn(state->portfd, event, MAX_EVENT_BATCHSZ, &nevents,\n        tsp) == -1 && (errno != ETIME || nevents == 0)) {\n        if (errno == ETIME || errno == EINTR)\n            return 0;\n\n        /* Any other error indicates a bug. */\n        perror(\"aeApiPoll: port_get\");\n        abort();\n    }\n\n    state->npending = nevents;\n\n    for (i = 0; i < nevents; i++) {\n            mask = 0;\n            if (event[i].portev_events & POLLIN)\n                mask |= AE_READABLE;\n            if (event[i].portev_events & POLLOUT)\n                mask |= AE_WRITABLE;\n\n            eventLoop->fired[i].fd = event[i].portev_object;\n            eventLoop->fired[i].mask = mask;\n\n            if (evport_debug)\n                fprintf(stderr, \"aeApiPoll: fd %d mask 0x%x\\n\",\n                    (int)event[i].portev_object, mask);\n\n            state->pending_fds[i] = event[i].portev_object;\n            state->pending_masks[i] = (uintptr_t)event[i].portev_user;\n    }\n\n    return nevents;\n}\n\nstatic char *aeApiName(void) {\n    return \"evport\";\n}\n"
  },
  {
    "path": "src/ae_kqueue.c",
    "content": "/* Kqueue(2)-based ae.c module\n *\n * Copyright (C) 2009 Harish Mallipeddi - harish.mallipeddi@gmail.com\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n\n#include <sys/types.h>\n#include <sys/event.h>\n#include <sys/time.h>\n\ntypedef struct aeApiState {\n    int kqfd;\n    struct kevent *events;\n\n    /* Events mask for merge read and write event.\n     * To reduce memory consumption, we use 2 bits to store the mask\n     * of an event, so that 1 byte will store the mask of 4 events. */\n    char *eventsMask; \n} aeApiState;\n\n#define EVENT_MASK_MALLOC_SIZE(sz) (((sz) + 3) / 4)\n#define EVENT_MASK_OFFSET(fd) ((fd) % 4 * 2)\n#define EVENT_MASK_ENCODE(fd, mask) (((mask) & 0x3) << EVENT_MASK_OFFSET(fd))\n\nstatic inline int getEventMask(const char *eventsMask, int fd) {\n    return (eventsMask[fd/4] >> EVENT_MASK_OFFSET(fd)) & 0x3;\n}\n\nstatic inline void addEventMask(char *eventsMask, int fd, int mask) {\n    eventsMask[fd/4] |= EVENT_MASK_ENCODE(fd, mask);\n}\n\nstatic inline void resetEventMask(char *eventsMask, int fd) {\n    eventsMask[fd/4] &= ~EVENT_MASK_ENCODE(fd, 0x3);\n}\n\nstatic int aeApiCreate(aeEventLoop *eventLoop) {\n    aeApiState *state = (aeApiState*)zmalloc(sizeof(aeApiState), MALLOC_LOCAL);\n\n    if (!state) return -1;\n    state->events = (struct kevent*)zmalloc(sizeof(struct kevent)*eventLoop->setsize, MALLOC_LOCAL);\n    if (!state->events) {\n        zfree(state);\n        return -1;\n    }\n    state->kqfd = kqueue();\n    if (state->kqfd == -1) {\n        zfree(state->events);\n        zfree(state);\n        return -1;\n    }\n    anetCloexec(state->kqfd);\n    state->eventsMask = (char*)zmalloc(EVENT_MASK_MALLOC_SIZE(eventLoop->setsize), MALLOC_LOCAL);\n    memset(state->eventsMask, 0, EVENT_MASK_MALLOC_SIZE(eventLoop->setsize));\n    eventLoop->apidata = state;\n    return 0;\n}\n\nstatic int aeApiResize(aeEventLoop *eventLoop, int setsize) {\n    aeApiState *state = (aeApiState*)eventLoop->apidata;\n\n    state->events = (struct kevent*)zrealloc(state->events, sizeof(struct kevent)*setsize, MALLOC_LOCAL);    \n    state->eventsMask = (char*)zrealloc(state->eventsMask, EVENT_MASK_MALLOC_SIZE(setsize), MALLOC_LOCAL);\n    memset(state->eventsMask, 0, EVENT_MASK_MALLOC_SIZE(setsize));\n    return 0;\n}\n\nstatic void aeApiFree(aeEventLoop *eventLoop) {\n    aeApiState *state = (aeApiState*)eventLoop->apidata;\n\n    close(state->kqfd);\n    zfree(state->events);\n    zfree(state->eventsMask);\n    zfree(state);\n}\n\nstatic int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {\n    aeApiState *state = (aeApiState*)eventLoop->apidata;\n    struct kevent ke;\n\n    if (mask & AE_READABLE) {\n        EV_SET(&ke, fd, EVFILT_READ, EV_ADD, 0, 0, NULL);\n        if (kevent(state->kqfd, &ke, 1, NULL, 0, NULL) == -1) return -1;\n    }\n    if (mask & AE_WRITABLE) {\n        EV_SET(&ke, fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);\n        if (kevent(state->kqfd, &ke, 1, NULL, 0, NULL) == -1) return -1;\n    }\n    return 0;\n}\n\nstatic void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int mask) {\n    aeApiState *state = (aeApiState*)eventLoop->apidata;\n    struct kevent ke;\n\n    if (mask & AE_READABLE) {\n        EV_SET(&ke, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);\n        kevent(state->kqfd, &ke, 1, NULL, 0, NULL);\n    }\n    if (mask & AE_WRITABLE) {\n        EV_SET(&ke, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);\n        kevent(state->kqfd, &ke, 1, NULL, 0, NULL);\n    }\n}\n\nstatic int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {\n    aeApiState *state = (aeApiState*)eventLoop->apidata;\n    int retval, numevents = 0;\n\n    if (tvp != NULL) {\n        struct timespec timeout;\n        timeout.tv_sec = tvp->tv_sec;\n        timeout.tv_nsec = tvp->tv_usec * 1000;\n        retval = kevent(state->kqfd, NULL, 0, state->events, eventLoop->setsize,\n                        &timeout);\n    } else {\n        retval = kevent(state->kqfd, NULL, 0, state->events, eventLoop->setsize,\n                        NULL);\n    }\n\n    if (retval > 0) {\n        int j;\n\n        /* Normally we execute the read event first and then the write event.\n         * When the barrier is set, we will do it reverse.\n         * \n         * However, under kqueue, read and write events would be separate\n         * events, which would make it impossible to control the order of\n         * reads and writes. So we store the event's mask we've got and merge\n         * the same fd events later. */\n        for (j = 0; j < retval; j++) {\n            struct kevent *e = state->events+j;\n            int fd = e->ident;\n            int mask = 0; \n\n            if (e->filter == EVFILT_READ) mask = AE_READABLE;\n            else if (e->filter == EVFILT_WRITE) mask = AE_WRITABLE;\n            addEventMask(state->eventsMask, fd, mask);\n        }\n\n        /* Re-traversal to merge read and write events, and set the fd's mask to\n         * 0 so that events are not added again when the fd is encountered again. */\n        numevents = 0;\n        for (j = 0; j < retval; j++) {\n            struct kevent *e = state->events+j;\n            int fd = e->ident;\n            int mask = getEventMask(state->eventsMask, fd);\n\n            if (mask) {\n                eventLoop->fired[numevents].fd = fd;\n                eventLoop->fired[numevents].mask = mask;\n                resetEventMask(state->eventsMask, fd);\n                numevents++;\n            }\n        }\n    }\n    return numevents;\n}\n\nstatic const char *aeApiName(void) {\n    return \"kqueue\";\n}\n"
  },
  {
    "path": "src/ae_select.c",
    "content": "/* Select()-based ae.c module.\n *\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n\n#include <sys/select.h>\n#include <string.h>\n\ntypedef struct aeApiState {\n    fd_set rfds, wfds;\n    /* We need to have a copy of the fd sets as it's not safe to reuse\n     * FD sets after select(). */\n    fd_set _rfds, _wfds;\n} aeApiState;\n\nstatic int aeApiCreate(aeEventLoop *eventLoop) {\n    aeApiState *state = zmalloc(sizeof(aeApiState), MALLOC_LOCAL);\n\n    if (!state) return -1;\n    FD_ZERO(&state->rfds);\n    FD_ZERO(&state->wfds);\n    eventLoop->apidata = state;\n    return 0;\n}\n\nstatic int aeApiResize(aeEventLoop *eventLoop, int setsize) {\n    /* Just ensure we have enough room in the fd_set type. */\n    if (setsize >= FD_SETSIZE) return -1;\n    return 0;\n}\n\nstatic void aeApiFree(aeEventLoop *eventLoop) {\n    zfree(eventLoop->apidata);\n}\n\nstatic int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {\n    aeApiState *state = eventLoop->apidata;\n\n    if (mask & AE_READABLE) FD_SET(fd,&state->rfds);\n    if (mask & AE_WRITABLE) FD_SET(fd,&state->wfds);\n    return 0;\n}\n\nstatic void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int mask) {\n    aeApiState *state = eventLoop->apidata;\n\n    if (mask & AE_READABLE) FD_CLR(fd,&state->rfds);\n    if (mask & AE_WRITABLE) FD_CLR(fd,&state->wfds);\n}\n\nstatic int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {\n    aeApiState *state = eventLoop->apidata;\n    int retval, j, numevents = 0;\n\n    memcpy(&state->_rfds,&state->rfds,sizeof(fd_set));\n    memcpy(&state->_wfds,&state->wfds,sizeof(fd_set));\n\n    retval = select(eventLoop->maxfd+1,\n                &state->_rfds,&state->_wfds,NULL,tvp);\n    if (retval > 0) {\n        for (j = 0; j <= eventLoop->maxfd; j++) {\n            int mask = 0;\n            aeFileEvent *fe = &eventLoop->events[j];\n\n            if (fe->mask == AE_NONE) continue;\n            if (fe->mask & AE_READABLE && FD_ISSET(j,&state->_rfds))\n                mask |= AE_READABLE;\n            if (fe->mask & AE_WRITABLE && FD_ISSET(j,&state->_wfds))\n                mask |= AE_WRITABLE;\n            eventLoop->fired[numevents].fd = j;\n            eventLoop->fired[numevents].mask = mask;\n            numevents++;\n        }\n    }\n    return numevents;\n}\n\nstatic char *aeApiName(void) {\n    return \"select\";\n}\n"
  },
  {
    "path": "src/aelocker.h",
    "content": "#pragma once\n\nclass AeLocker\n{\n    bool m_fArmed = false;\n\npublic:\n    AeLocker()\n    {\n    }\n\n    void arm(client *c = nullptr, bool fIfNeeded = false) // if a client is passed, then the client is already locked\n    {\n        if (m_fArmed)\n            return;\n        \n        if (fIfNeeded && aeThreadOwnsLock())\n            return;\n\n        serverAssertDebug(!GlobalLocksAcquired());\n\n        if (c != nullptr)\n        {\n            serverAssert(c->lock.fOwnLock());\n\n            if (!aeTryAcquireLock(true /*fWeak*/))    // avoid locking the client if we can\n            {\n                bool fOwnClientLock = true;\n                int clientNesting = 1;\n                for (;;)\n                {\n                    if (fOwnClientLock)\n                    {\n                        clientNesting = c->lock.unlock_recursive();\n                        fOwnClientLock = false;\n                    }\n                    aeAcquireLock();\n                    if (!c->lock.try_lock(false))   // ensure a strong try because aeAcquireLock is expensive\n                    {\n                        aeReleaseLock();\n                    }\n                    else\n                    {\n                        break;\n                    }\n                }\n                c->lock.lock_recursive(clientNesting);\n            }\n            \n            m_fArmed = true;\n        }\n        else if (!m_fArmed)\n        {\n            m_fArmed = true;\n            aeAcquireLock();\n        }\n    }\n\n    void disarm()\n    {\n        serverAssert(m_fArmed);\n        m_fArmed = false;\n        aeReleaseLock();\n    }\n\n    bool isArmed() const\n    {\n        return m_fArmed;\n    }\n\n    void release()\n    {\n        m_fArmed = false;\n    }\n\n    ~AeLocker()\n    {\n        if (m_fArmed)\n            aeReleaseLock();\n    }\n};"
  },
  {
    "path": "src/anet.c",
    "content": "/* anet.c -- Basic TCP socket stuff made a bit less boring\n *\n * Copyright (c) 2006-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"fmacros.h\"\n\n#include <sys/types.h>\n#include <sys/socket.h>\n#include <sys/stat.h>\n#include <sys/un.h>\n#include <sys/time.h>\n#include <netinet/in.h>\n#include <netinet/tcp.h>\n#include <arpa/inet.h>\n#include <unistd.h>\n#include <fcntl.h>\n#include <string.h>\n#include <netdb.h>\n#include <errno.h>\n#include <stdarg.h>\n#include <stdio.h>\n\n#include \"anet.h\"\n\nstatic void anetSetError(char *err, const char *fmt, ...)\n{\n    va_list ap;\n\n    if (!err) return;\n    va_start(ap, fmt);\n    vsnprintf(err, ANET_ERR_LEN, fmt, ap);\n    va_end(ap);\n}\n\nint anetSetBlock(char *err, int fd, int non_block) {\n    int flags;\n\n    /* Set the socket blocking (if non_block is zero) or non-blocking.\n     * Note that fcntl(2) for F_GETFL and F_SETFL can't be\n     * interrupted by a signal. */\n    if ((flags = fcntl(fd, F_GETFL)) == -1) {\n        anetSetError(err, \"fcntl(F_GETFL): %s\", strerror(errno));\n        return ANET_ERR;\n    }\n\n    /* Check if this flag has been set or unset, if so, \n     * then there is no need to call fcntl to set/unset it again. */\n    if (!!(flags & O_NONBLOCK) == !!non_block)\n        return ANET_OK;\n\n    if (non_block)\n        flags |= O_NONBLOCK;\n    else\n        flags &= ~O_NONBLOCK;\n\n    if (fcntl(fd, F_SETFL, flags) == -1) {\n        anetSetError(err, \"fcntl(F_SETFL,O_NONBLOCK): %s\", strerror(errno));\n        return ANET_ERR;\n    }\n    return ANET_OK;\n}\n\nint anetNonBlock(char *err, int fd) {\n    return anetSetBlock(err,fd,1);\n}\n\nint anetBlock(char *err, int fd) {\n    return anetSetBlock(err,fd,0);\n}\n\n/* Enable the FD_CLOEXEC on the given fd to avoid fd leaks. \n * This function should be invoked for fd's on specific places \n * where fork + execve system calls are called. */\nint anetCloexec(int fd) {\n    int r;\n    int flags;\n\n    do {\n        r = fcntl(fd, F_GETFD);\n    } while (r == -1 && errno == EINTR);\n\n    if (r == -1 || (r & FD_CLOEXEC))\n        return r;\n\n    flags = r | FD_CLOEXEC;\n\n    do {\n        r = fcntl(fd, F_SETFD, flags);\n    } while (r == -1 && errno == EINTR);\n\n    return r;\n}\n\n/* Set TCP keep alive option to detect dead peers. The interval option\n * is only used for Linux as we are using Linux-specific APIs to set\n * the probe send time, interval, and count. */\nint anetKeepAlive(char *err, int fd, int interval)\n{\n    int val = 1;\n\n    if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) == -1)\n    {\n        anetSetError(err, \"setsockopt SO_KEEPALIVE: %s\", strerror(errno));\n        return ANET_ERR;\n    }\n\n#ifdef __linux__\n    /* Default settings are more or less garbage, with the keepalive time\n     * set to 7200 by default on Linux. Modify settings to make the feature\n     * actually useful. */\n\n    /* Send first probe after interval. */\n    val = interval;\n    if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof(val)) < 0) {\n        anetSetError(err, \"setsockopt TCP_KEEPIDLE: %s\\n\", strerror(errno));\n        return ANET_ERR;\n    }\n\n    /* Send next probes after the specified interval. Note that we set the\n     * delay as interval / 3, as we send three probes before detecting\n     * an error (see the next setsockopt call). */\n    val = interval/3;\n    if (val == 0) val = 1;\n    if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof(val)) < 0) {\n        anetSetError(err, \"setsockopt TCP_KEEPINTVL: %s\\n\", strerror(errno));\n        return ANET_ERR;\n    }\n\n    /* Consider the socket in error state after three we send three ACK\n     * probes without getting a reply. */\n    val = 3;\n    if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof(val)) < 0) {\n        anetSetError(err, \"setsockopt TCP_KEEPCNT: %s\\n\", strerror(errno));\n        return ANET_ERR;\n    }\n#else\n    ((void) interval); /* Avoid unused var warning for non Linux systems. */\n#endif\n\n    return ANET_OK;\n}\n\nstatic int anetSetTcpNoDelay(char *err, int fd, int val)\n{\n    if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)) == -1)\n    {\n        anetSetError(err, \"setsockopt TCP_NODELAY: %s\", strerror(errno));\n        return ANET_ERR;\n    }\n    return ANET_OK;\n}\n\nint anetEnableTcpNoDelay(char *err, int fd)\n{\n    return anetSetTcpNoDelay(err, fd, 1);\n}\n\nint anetDisableTcpNoDelay(char *err, int fd)\n{\n    return anetSetTcpNoDelay(err, fd, 0);\n}\n\n/* Set the socket send timeout (SO_SNDTIMEO socket option) to the specified\n * number of milliseconds, or disable it if the 'ms' argument is zero. */\nint anetSendTimeout(char *err, int fd, long long ms) {\n    struct timeval tv;\n\n    tv.tv_sec = ms/1000;\n    tv.tv_usec = (ms%1000)*1000;\n    if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {\n        anetSetError(err, \"setsockopt SO_SNDTIMEO: %s\", strerror(errno));\n        return ANET_ERR;\n    }\n    return ANET_OK;\n}\n\n/* Set the socket receive timeout (SO_RCVTIMEO socket option) to the specified\n * number of milliseconds, or disable it if the 'ms' argument is zero. */\nint anetRecvTimeout(char *err, int fd, long long ms) {\n    struct timeval tv;\n\n    tv.tv_sec = ms/1000;\n    tv.tv_usec = (ms%1000)*1000;\n    if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {\n        anetSetError(err, \"setsockopt SO_RCVTIMEO: %s\", strerror(errno));\n        return ANET_ERR;\n    }\n    return ANET_OK;\n}\n\n/* Resolve the hostname \"host\" and set the string representation of the\n * IP address into the buffer pointed by \"ipbuf\".\n *\n * If flags is set to ANET_IP_ONLY the function only resolves hostnames\n * that are actually already IPv4 or IPv6 addresses. This turns the function\n * into a validating / normalizing function. */\nint anetResolve(char *err, char *host, char *ipbuf, size_t ipbuf_len,\n                       int flags)\n{\n    struct addrinfo hints, *info;\n    int rv;\n\n    memset(&hints,0,sizeof(hints));\n    if (flags & ANET_IP_ONLY) hints.ai_flags = AI_NUMERICHOST;\n    hints.ai_family = AF_UNSPEC;\n    hints.ai_socktype = SOCK_STREAM;  /* specify socktype to avoid dups */\n\n    if ((rv = getaddrinfo(host, NULL, &hints, &info)) != 0) {\n        anetSetError(err, \"%s\", gai_strerror(rv));\n        return ANET_ERR;\n    }\n    if (info->ai_family == AF_INET) {\n        struct sockaddr_in *sa = (struct sockaddr_in *)info->ai_addr;\n        inet_ntop(AF_INET, &(sa->sin_addr), ipbuf, ipbuf_len);\n    } else {\n        struct sockaddr_in6 *sa = (struct sockaddr_in6 *)info->ai_addr;\n        inet_ntop(AF_INET6, &(sa->sin6_addr), ipbuf, ipbuf_len);\n    }\n\n    freeaddrinfo(info);\n    return ANET_OK;\n}\n\nstatic int anetSetReuseAddr(char *err, int fd) {\n    int yes = 1;\n    /* Make sure connection-intensive things like the redis benchmark\n     * will be able to close/open sockets a zillion of times */\n    if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) {\n        anetSetError(err, \"setsockopt SO_REUSEADDR: %s\", strerror(errno));\n        return ANET_ERR;\n    }\n    return ANET_OK;\n}\n\nstatic int anetSetReusePort(char *err, int fd) {\n    int yes = 1;\n    /* Let us load balance listen()s from multiple threads */\n    if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes)) == -1) {\n        anetSetError(err, \"setsockopt SO_REUSEPORT: %s\", strerror(errno));\n        return ANET_ERR;\n    }\n    return ANET_OK;\n}\n\nstatic int anetCreateSocket(char *err, int domain) {\n    int s;\n    if ((s = socket(domain, SOCK_STREAM, 0)) == -1) {\n        anetSetError(err, \"creating socket: %s\", strerror(errno));\n        return ANET_ERR;\n    }\n\n    /* Make sure connection-intensive things like the redis benchmark\n     * will be able to close/open sockets a zillion of times */\n    if (anetSetReuseAddr(err,s) == ANET_ERR) {\n        close(s);\n        return ANET_ERR;\n    }\n    return s;\n}\n\n#define ANET_CONNECT_NONE 0\n#define ANET_CONNECT_NONBLOCK 1\n#define ANET_CONNECT_BE_BINDING 2 /* Best effort binding. */\n#define ANET_CONNECT_REUSEPORT 4\nstatic int anetTcpGenericConnect(char *err, const char *addr, int port,\n                                 const char *source_addr, int flags)\n{\n    int s = ANET_ERR, rv;\n    char portstr[6];  /* strlen(\"65535\") + 1; */\n    struct addrinfo hints, *servinfo, *bservinfo, *p, *b;\n\n    snprintf(portstr,sizeof(portstr),\"%d\",port);\n    memset(&hints,0,sizeof(hints));\n    hints.ai_family = AF_UNSPEC;\n    hints.ai_socktype = SOCK_STREAM;\n\n    if ((rv = getaddrinfo(addr,portstr,&hints,&servinfo)) != 0) {\n        anetSetError(err, \"%s\", gai_strerror(rv));\n        return ANET_ERR;\n    }\n    for (p = servinfo; p != NULL; p = p->ai_next) {\n        /* Try to create the socket and to connect it.\n         * If we fail in the socket() call, or on connect(), we retry with\n         * the next entry in servinfo. */\n        if ((s = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == -1)\n            continue;\n        if (anetSetReuseAddr(err,s) == ANET_ERR) \n            goto error;\n        if (flags & ANET_CONNECT_REUSEPORT && anetSetReusePort(err, s) != ANET_OK)\n            goto error;\n        if (flags & ANET_CONNECT_NONBLOCK && anetNonBlock(err,s) != ANET_OK)\n            goto error;\n        if (source_addr) {\n            int bound = 0;\n            /* Using getaddrinfo saves us from self-determining IPv4 vs IPv6 */\n            if ((rv = getaddrinfo(source_addr, NULL, &hints, &bservinfo)) != 0)\n            {\n                anetSetError(err, \"%s\", gai_strerror(rv));\n                goto error;\n            }\n            for (b = bservinfo; b != NULL; b = b->ai_next) {\n                if (bind(s,b->ai_addr,b->ai_addrlen) != -1) {\n                    bound = 1;\n                    break;\n                }\n            }\n            freeaddrinfo(bservinfo);\n            if (!bound) {\n                anetSetError(err, \"bind: %s\", strerror(errno));\n                goto error;\n            }\n        }\n        if (connect(s,p->ai_addr,p->ai_addrlen) == -1) {\n            /* If the socket is non-blocking, it is ok for connect() to\n             * return an EINPROGRESS error here. */\n            if (errno == EINPROGRESS && flags & ANET_CONNECT_NONBLOCK)\n                goto end;\n            close(s);\n            s = ANET_ERR;\n            continue;\n        }\n\n        /* If we ended an iteration of the for loop without errors, we\n         * have a connected socket. Let's return to the caller. */\n        goto end;\n    }\n    if (p == NULL)\n        anetSetError(err, \"creating socket: %s\", strerror(errno));\n\nerror:\n    if (s != ANET_ERR) {\n        close(s);\n        s = ANET_ERR;\n    }\n\nend:\n    freeaddrinfo(servinfo);\n\n    /* Handle best effort binding: if a binding address was used, but it is\n     * not possible to create a socket, try again without a binding address. */\n    if (s == ANET_ERR && source_addr && (flags & ANET_CONNECT_BE_BINDING)) {\n        return anetTcpGenericConnect(err,addr,port,NULL,flags);\n    } else {\n        return s;\n    }\n}\n\nint anetTcpNonBlockConnect(char *err, const char *addr, int port)\n{\n    return anetTcpGenericConnect(err,addr,port,NULL,ANET_CONNECT_NONBLOCK);\n}\n\nint anetTcpNonBlockBestEffortBindConnect(char *err, const char *addr, int port,\n                                         const char *source_addr)\n{\n    return anetTcpGenericConnect(err,addr,port,source_addr,\n            ANET_CONNECT_NONBLOCK|ANET_CONNECT_BE_BINDING);\n}\n\nint anetUnixGenericConnect(char *err, const char *path, int flags)\n{\n    int s;\n    struct sockaddr_un sa;\n\n    if ((s = anetCreateSocket(err,AF_LOCAL)) == ANET_ERR)\n        return ANET_ERR;\n\n    sa.sun_family = AF_LOCAL;\n    strncpy(sa.sun_path,path,sizeof(sa.sun_path)-1);\n    if (flags & ANET_CONNECT_NONBLOCK) {\n        if (anetNonBlock(err,s) != ANET_OK) {\n            close(s);\n            return ANET_ERR;\n        }\n    }\n    if (connect(s,(struct sockaddr*)&sa,sizeof(sa)) == -1) {\n        if (errno == EINPROGRESS &&\n            flags & ANET_CONNECT_NONBLOCK)\n            return s;\n\n        anetSetError(err, \"connect: %s\", strerror(errno));\n        close(s);\n        return ANET_ERR;\n    }\n    return s;\n}\n\nstatic int anetListen(char *err, int s, struct sockaddr *sa, socklen_t len, int backlog) {\n    if (bind(s,sa,len) == -1) {\n        anetSetError(err, \"bind: %s\", strerror(errno));\n        close(s);\n        return ANET_ERR;\n    }\n\n    if (listen(s, backlog) == -1) {\n        anetSetError(err, \"listen: %s\", strerror(errno));\n        close(s);\n        return ANET_ERR;\n    }\n    return ANET_OK;\n}\n\nstatic int anetV6Only(char *err, int s) {\n    int yes = 1;\n    if (setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,&yes,sizeof(yes)) == -1) {\n        anetSetError(err, \"setsockopt: %s\", strerror(errno));\n        return ANET_ERR;\n    }\n    return ANET_OK;\n}\n\nstatic int _anetTcpServer(char *err, int port, const char *bindaddr, int af, int backlog, int fReusePort, int fFirstListen)\n{\n    int s = -1, rv;\n    char _port[6];  /* strlen(\"65535\") */\n    struct addrinfo hints, *servinfo, *p;\n\n    snprintf(_port,sizeof(_port),\"%d\",port);\n    memset(&hints,0,sizeof(hints));\n    hints.ai_family = af;\n    hints.ai_socktype = SOCK_STREAM;\n    hints.ai_flags = AI_PASSIVE;    /* No effect if bindaddr != NULL */\n    if (bindaddr && !strcmp(\"*\", bindaddr))\n        bindaddr = NULL;\n    if (af == AF_INET6 && bindaddr && !strcmp(\"::*\", bindaddr))\n        bindaddr = NULL;\n\n    if ((rv = getaddrinfo(bindaddr,_port,&hints,&servinfo)) != 0) {\n        anetSetError(err, \"%s\", gai_strerror(rv));\n        return ANET_ERR;\n    }\n    for (p = servinfo; p != NULL; p = p->ai_next) {\n        if ((s = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == -1)\n            continue;\n\n        if (af == AF_INET6 && anetV6Only(err,s) == ANET_ERR) goto error;\n        if (anetSetReuseAddr(err,s) == ANET_ERR) goto error;\n        if (fReusePort && !fFirstListen && anetSetReusePort(err,s) == ANET_ERR) goto error;\n        if (anetListen(err,s,p->ai_addr,p->ai_addrlen,backlog) == ANET_ERR) {\n            s = ANET_ERR;\n            goto end;\n        }\n        if (fReusePort && fFirstListen && anetSetReusePort(err,s) == ANET_ERR) goto error;\n        goto end;\n    }\n    if (p == NULL) {\n        anetSetError(err, \"unable to bind socket, errno: %d\", errno);\n        goto error;\n    }\n\nerror:\n    if (s != -1) close(s);\n    s = ANET_ERR;\nend:\n    freeaddrinfo(servinfo);\n    return s;\n}\n\nint anetTcpServer(char *err, int port, const char *bindaddr, int backlog, int fReusePort, int fFirstListen)\n{\n    return _anetTcpServer(err, port, bindaddr, AF_INET, backlog, fReusePort, fFirstListen);\n}\n\nint anetTcp6Server(char *err, int port, const char *bindaddr, int backlog, int fReusePort, int fFirstListen)\n{\n    return _anetTcpServer(err, port, bindaddr, AF_INET6, backlog, fReusePort, fFirstListen);\n}\n\nint anetUnixServer(char *err, char *path, mode_t perm, int backlog)\n{\n    int s;\n    struct sockaddr_un sa;\n\n    if ((s = anetCreateSocket(err,AF_LOCAL)) == ANET_ERR)\n        return ANET_ERR;\n\n    memset(&sa,0,sizeof(sa));\n    sa.sun_family = AF_LOCAL;\n    strncpy(sa.sun_path,path,sizeof(sa.sun_path)-1);\n    if (anetListen(err,s,(struct sockaddr*)&sa,sizeof(sa),backlog) == ANET_ERR)\n        return ANET_ERR;\n    if (perm)\n        chmod(sa.sun_path, perm);\n    return s;\n}\n\nstatic int anetGenericAccept(char *err, int s, struct sockaddr *sa, socklen_t *len) {\n    int fd;\n    while(1) {\n        fd = accept(s,sa,len);\n        if (fd == -1) {\n            if (errno == EINTR)\n                continue;\n            else {\n                anetSetError(err, \"accept: %s\", strerror(errno));\n                return ANET_ERR;\n            }\n        }\n        break;\n    }\n    return fd;\n}\n\nint anetTcpAccept(char *err, int s, char *ip, size_t ip_len, int *port) {\n    int fd;\n    struct sockaddr_storage sa;\n    socklen_t salen = sizeof(sa);\n    if ((fd = anetGenericAccept(err,s,(struct sockaddr*)&sa,&salen)) == -1)\n        return ANET_ERR;\n\n    if (sa.ss_family == AF_INET) {\n        struct sockaddr_in *s = (struct sockaddr_in *)&sa;\n        if (ip) inet_ntop(AF_INET,(void*)&(s->sin_addr),ip,ip_len);\n        if (port) *port = ntohs(s->sin_port);\n    } else {\n        struct sockaddr_in6 *s = (struct sockaddr_in6 *)&sa;\n        if (ip) inet_ntop(AF_INET6,(void*)&(s->sin6_addr),ip,ip_len);\n        if (port) *port = ntohs(s->sin6_port);\n    }\n    return fd;\n}\n\nint anetUnixAccept(char *err, int s) {\n    int fd;\n    struct sockaddr_un sa;\n    socklen_t salen = sizeof(sa);\n    if ((fd = anetGenericAccept(err,s,(struct sockaddr*)&sa,&salen)) == -1)\n        return ANET_ERR;\n\n    return fd;\n}\n\nint anetFdToString(int fd, char *ip, size_t ip_len, int *port, int fd_to_str_type) {\n    struct sockaddr_storage sa;\n    socklen_t salen = sizeof(sa);\n\n    if (fd_to_str_type == FD_TO_PEER_NAME) {\n        if (getpeername(fd, (struct sockaddr *)&sa, &salen) == -1) goto error;\n    } else {\n        if (getsockname(fd, (struct sockaddr *)&sa, &salen) == -1) goto error;\n    }\n    if (ip_len == 0) goto error;\n\n    if (sa.ss_family == AF_INET) {\n        struct sockaddr_in *s = (struct sockaddr_in *)&sa;\n        if (ip) inet_ntop(AF_INET,(void*)&(s->sin_addr),ip,ip_len);\n        if (port) *port = ntohs(s->sin_port);\n    } else if (sa.ss_family == AF_INET6) {\n        struct sockaddr_in6 *s = (struct sockaddr_in6 *)&sa;\n        if (ip) inet_ntop(AF_INET6,(void*)&(s->sin6_addr),ip,ip_len);\n        if (port) *port = ntohs(s->sin6_port);\n    } else if (sa.ss_family == AF_UNIX) {\n        if (ip) snprintf(ip, ip_len, \"/unixsocket\");\n        if (port) *port = 0;\n    } else {\n        goto error;\n    }\n    return 0;\n\nerror:\n    if (ip) {\n        if (ip_len >= 2) {\n            ip[0] = '?';\n            ip[1] = '\\0';\n        } else if (ip_len == 1) {\n            ip[0] = '\\0';\n        }\n    }\n    if (port) *port = 0;\n    return -1;\n}\n\n/* Format an IP,port pair into something easy to parse. If IP is IPv6\n * (matches for \":\"), the ip is surrounded by []. IP and port are just\n * separated by colons. This the standard to display addresses within Redis. */\nint anetFormatAddr(char *buf, size_t buf_len, char *ip, int port) {\n    return snprintf(buf,buf_len, strchr(ip,':') ?\n           \"[%s]:%d\" : \"%s:%d\", ip, port);\n}\n\n/* Like anetFormatAddr() but extract ip and port from the socket's peer/sockname. */\nint anetFormatFdAddr(int fd, char *buf, size_t buf_len, int fd_to_str_type) {\n    char ip[INET6_ADDRSTRLEN];\n    int port;\n\n    anetFdToString(fd,ip,sizeof(ip),&port,fd_to_str_type);\n    return anetFormatAddr(buf, buf_len, ip, port);\n}\n"
  },
  {
    "path": "src/anet.h",
    "content": "/* anet.c -- Basic TCP socket stuff made a bit less boring\n *\n * Copyright (c) 2006-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef ANET_H\n#define ANET_H\n\n#include <sys/types.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#define ANET_OK 0\n#define ANET_ERR -1\n#define ANET_ERR_LEN 256\n\n/* Flags used with certain functions. */\n#define ANET_NONE 0\n#define ANET_IP_ONLY (1<<0)\n\n#if defined(__sun) || defined(_AIX)\n#define AF_LOCAL AF_UNIX\n#endif\n\n#ifdef _AIX\n#undef ip_len\n#endif\n\n/* FD to address string conversion types */\n#define FD_TO_PEER_NAME 0\n#define FD_TO_SOCK_NAME 1\n\nint anetTcpNonBlockConnect(char *err, const char *addr, int port);\nint anetTcpNonBlockBestEffortBindConnect(char *err, const char *addr, int port, const char *source_addr);\nint anetResolve(char *err, char *host, char *ipbuf, size_t ipbuf_len, int flags);\nint anetTcpServer(char *err, int port, const char *bindaddr, int backlog, int fReusePort, int fFirstListen);\nint anetTcp6Server(char *err, int port, const char *bindaddr, int backlog, int fReusePort, int fFirstListen);\nint anetUnixServer(char *err, char *path, mode_t perm, int backlog);\nint anetTcpAccept(char *err, int serversock, char *ip, size_t ip_len, int *port);\nint anetUnixAccept(char *err, int serversock);\nint anetNonBlock(char *err, int fd);\nint anetBlock(char *err, int fd);\nint anetCloexec(int fd);\nint anetEnableTcpNoDelay(char *err, int fd);\nint anetDisableTcpNoDelay(char *err, int fd);\nint anetSendTimeout(char *err, int fd, long long ms);\nint anetRecvTimeout(char *err, int fd, long long ms);\nint anetFdToString(int fd, char *ip, size_t ip_len, int *port, int fd_to_str_type);\nint anetKeepAlive(char *err, int fd, int interval);\nint anetFormatAddr(char *fmt, size_t fmt_len, char *ip, int port);\nint anetFormatFdAddr(int fd, char *buf, size_t buf_len, int fd_to_str_type);\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n"
  },
  {
    "path": "src/aof.cpp",
    "content": "/*\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n#include \"bio.h\"\n#include \"rio.h\"\n\n#include <signal.h>\n#include <fcntl.h>\n#include <sys/stat.h>\n#include <sys/types.h>\n#include <sys/time.h>\n#include <sys/resource.h>\n#include <sys/wait.h>\n#include <sys/param.h>\n\nvoid aofUpdateCurrentSize(void);\nvoid aofClosePipes(void);\n\n/* ----------------------------------------------------------------------------\n * AOF rewrite buffer implementation.\n *\n * The following code implement a simple buffer used in order to accumulate\n * changes while the background process is rewriting the AOF file.\n *\n * We only need to append, but can't just use realloc with a large block\n * because 'huge' reallocs are not always handled as one could expect\n * (via remapping of pages at OS level) but may involve copying data.\n *\n * For this reason we use a list of blocks, every block is\n * AOF_RW_BUF_BLOCK_SIZE bytes.\n * ------------------------------------------------------------------------- */\n\n#define AOF_RW_BUF_BLOCK_SIZE (1024*1024*10)    /* 10 MB per block */\n\ntypedef struct aofrwblock {\n    unsigned long used, free;\n    char buf[AOF_RW_BUF_BLOCK_SIZE];\n} aofrwblock;\n\n/* This function free the old AOF rewrite buffer if needed, and initialize\n * a fresh new one. It tests for g_pserver->aof_rewrite_buf_blocks equal to NULL\n * so can be used for the first initialization as well. */\nvoid aofRewriteBufferReset(void) {\n    if (g_pserver->aof_rewrite_buf_blocks)\n        listRelease(g_pserver->aof_rewrite_buf_blocks);\n\n    g_pserver->aof_rewrite_buf_blocks = listCreate();\n    listSetFreeMethod(g_pserver->aof_rewrite_buf_blocks,zfree);\n}\n\n/* Return the current size of the AOF rewrite buffer. */\nunsigned long aofRewriteBufferSize(void) {\n    listNode *ln;\n    listIter li;\n    unsigned long size = 0;\n\n    listRewind(g_pserver->aof_rewrite_buf_blocks,&li);\n    while((ln = listNext(&li))) {\n        aofrwblock *block = (aofrwblock*)listNodeValue(ln);\n        size += block->used;\n    }\n    return size;\n}\n\n/* Event handler used to send data to the child process doing the AOF\n * rewrite. We send pieces of our AOF differences buffer so that the final\n * write when the child finishes the rewrite will be small. */\nvoid aofChildWriteDiffData(aeEventLoop *el, int fd, void *privdata, int mask) {\n    listNode *ln;\n    aofrwblock *block;\n    ssize_t nwritten;\n    serverAssert(GlobalLocksAcquired());\n    serverAssert(el == g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].el); // MUST run on main thread\n\n    UNUSED(el);\n    UNUSED(fd);\n    UNUSED(privdata);\n    UNUSED(mask);\n\n    while(1) {\n        ln = listFirst(g_pserver->aof_rewrite_buf_blocks);\n        block = (aofrwblock*)(ln ? ln->value : NULL);\n        if (g_pserver->aof_stop_sending_diff || !block) {\n            aeDeleteFileEvent(el,g_pserver->aof_pipe_write_data_to_child,\n                              AE_WRITABLE);\n            return;\n        }\n        if (block->used > 0) {\n            nwritten = write(g_pserver->aof_pipe_write_data_to_child,\n                             block->buf,block->used);\n            if (nwritten <= 0) return;\n            memmove(block->buf,block->buf+nwritten,block->used-nwritten);\n            block->used -= nwritten;\n            block->free += nwritten;\n        }\n        if (block->used == 0) listDelNode(g_pserver->aof_rewrite_buf_blocks,ln);\n    }\n}\n\nvoid installAofRewriteEvent()\n{\n    serverTL->fRetrySetAofEvent = false;\n    if (!g_pserver->aof_rewrite_pending) {\n        g_pserver->aof_rewrite_pending = true;\n        int res = aePostFunction(g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].el, [] {\n            g_pserver->aof_rewrite_pending = false;\n            if (!g_pserver->aof_stop_sending_diff && g_pserver->aof_pipe_write_data_to_child >= 0)\n                aeCreateFileEvent(g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].el, g_pserver->aof_pipe_write_data_to_child, AE_WRITABLE, aofChildWriteDiffData, NULL);\n        });\n        if (res != AE_OK)\n            serverTL->fRetrySetAofEvent = true;\n    }\n}\n\n/* Append data to the AOF rewrite buffer, allocating new blocks if needed. */\nvoid aofRewriteBufferAppend(unsigned char *s, unsigned long len) {\n    listNode *ln = listLast(g_pserver->aof_rewrite_buf_blocks);\n    aofrwblock *block = (aofrwblock*)(ln ? ln->value : NULL);\n\n    while(len) {\n        /* If we already got at least an allocated block, try appending\n         * at least some piece into it. */\n        if (block) {\n            unsigned long thislen = (block->free < len) ? block->free : len;\n            if (thislen) {  /* The current block is not already full. */\n                memcpy(block->buf+block->used, s, thislen);\n                block->used += thislen;\n                block->free -= thislen;\n                s += thislen;\n                len -= thislen;\n            }\n        }\n\n        if (len) { /* First block to allocate, or need another block. */\n            int numblocks;\n\n            block = (aofrwblock*)zmalloc(sizeof(*block), MALLOC_LOCAL);\n            block->free = AOF_RW_BUF_BLOCK_SIZE;\n            block->used = 0;\n            listAddNodeTail(g_pserver->aof_rewrite_buf_blocks,block);\n\n            /* Log every time we cross more 10 or 100 blocks, respectively\n             * as a notice or warning. */\n            numblocks = listLength(g_pserver->aof_rewrite_buf_blocks);\n            if (((numblocks+1) % 10) == 0) {\n                int level = ((numblocks+1) % 100) == 0 ? LL_WARNING :\n                                                         LL_NOTICE;\n                serverLog(level,\"Background AOF buffer size: %lu MB\",\n                    aofRewriteBufferSize()/(1024*1024));\n            }\n        }\n    }\n\n    /* Install a file event to send data to the rewrite child if there is\n     * not one already. */\n    installAofRewriteEvent();\n}\n\n/* Write the buffer (possibly composed of multiple blocks) into the specified\n * fd. If a short write or any other error happens -1 is returned,\n * otherwise the number of bytes written is returned. */\nssize_t aofRewriteBufferWrite(int fd) {\n    listNode *ln;\n    listIter li;\n    ssize_t count = 0;\n\n    listRewind(g_pserver->aof_rewrite_buf_blocks,&li);\n    while((ln = listNext(&li))) {\n        aofrwblock *block = (aofrwblock*)listNodeValue(ln);\n        ssize_t nwritten;\n\n        if (block->used) {\n            nwritten = write(fd,block->buf,block->used);\n            if (nwritten != (ssize_t)block->used) {\n                if (nwritten == 0) errno = EIO;\n                return -1;\n            }\n            count += nwritten;\n        }\n    }\n    return count;\n}\n\n/* ----------------------------------------------------------------------------\n * AOF file implementation\n * ------------------------------------------------------------------------- */\n\n/* Return true if an AOf fsync is currently already in progress in a\n * BIO thread. */\nint aofFsyncInProgress(void) {\n    return bioPendingJobsOfType(BIO_AOF_FSYNC) != 0;\n}\n\n/* Starts a background task that performs fsync() against the specified\n * file descriptor (the one of the AOF file) in another thread. */\nvoid aof_background_fsync(int fd) {\n    bioCreateFsyncJob(fd);\n}\n\n/* Kills an AOFRW child process if exists */\nvoid killAppendOnlyChild(void) {\n    int statloc;\n    /* No AOFRW child? return. */\n    if (g_pserver->child_type != CHILD_TYPE_AOF) return;\n    /* Kill AOFRW child, wait for child exit. */\n    serverLog(LL_NOTICE,\"Killing running AOF rewrite child: %ld\",\n        (long) g_pserver->child_pid);\n    if (kill(g_pserver->child_pid,SIGUSR1) != -1) {\n        while(waitpid(-1, &statloc, 0) != g_pserver->child_pid);\n    }\n    /* Reset the buffer accumulating changes while the child saves. */\n    aofRewriteBufferReset();\n    aofRemoveTempFile(g_pserver->child_pid);\n    resetChildState();\n    g_pserver->aof_rewrite_time_start = -1;\n    /* Close pipes used for IPC between the two processes. */\n    aofClosePipes();\n}\n\n/* Called when the user switches from \"appendonly yes\" to \"appendonly no\"\n * at runtime using the CONFIG command. */\nvoid stopAppendOnly(void) {\n    serverAssert(g_pserver->aof_state != AOF_OFF);\n    flushAppendOnlyFile(1);\n    if (redis_fsync(g_pserver->aof_fd) == -1) {\n        serverLog(LL_WARNING,\"Fail to fsync the AOF file: %s\",strerror(errno));\n    } else {\n        g_pserver->aof_fsync_offset = g_pserver->aof_current_size;\n        g_pserver->aof_last_fsync = g_pserver->unixtime;\n    }\n    close(g_pserver->aof_fd);\n\n    g_pserver->aof_fd = -1;\n    g_pserver->aof_selected_db = -1;\n    g_pserver->aof_state = AOF_OFF;\n    g_pserver->aof_rewrite_scheduled = 0;\n    killAppendOnlyChild();\n    sdsfree(g_pserver->aof_buf);\n    g_pserver->aof_buf = sdsempty();\n}\n\n/* Called when the user switches from \"appendonly no\" to \"appendonly yes\"\n * at runtime using the CONFIG command. */\nint startAppendOnly(void) {\n    char cwd[MAXPATHLEN]; /* Current working dir path for error messages. */\n    int newfd;\n\n    newfd = open(g_pserver->aof_filename,O_WRONLY|O_APPEND|O_CREAT,0644);\n    serverAssert(g_pserver->aof_state == AOF_OFF);\n    if (newfd == -1) {\n        char *cwdp = getcwd(cwd,MAXPATHLEN);\n\n        serverLog(LL_WARNING,\n            \"KeyDB needs to enable the AOF but can't open the \"\n            \"append only file %s (in server root dir %s): %s\",\n            g_pserver->aof_filename,\n            cwdp ? cwdp : \"unknown\",\n            strerror(errno));\n        return C_ERR;\n    }\n    if (hasActiveChildProcessOrBGSave() && g_pserver->child_type != CHILD_TYPE_AOF) {\n        g_pserver->aof_rewrite_scheduled = 1;\n        serverLog(LL_WARNING,\"AOF was enabled but there is already another background operation. An AOF background was scheduled to start when possible.\");\n    } else {\n        /* If there is a pending AOF rewrite, we need to switch it off and\n         * start a new one: the old one cannot be reused because it is not\n         * accumulating the AOF buffer. */\n        if (g_pserver->child_type == CHILD_TYPE_AOF) {\n            serverLog(LL_WARNING,\"AOF was enabled but there is already an AOF rewriting in background. Stopping background AOF and starting a rewrite now.\");\n            killAppendOnlyChild();\n        }\n        if (rewriteAppendOnlyFileBackground() == C_ERR) {\n            close(newfd);\n            serverLog(LL_WARNING,\"KeyDB needs to enable the AOF but can't trigger a background AOF rewrite operation. Check the above logs for more info about the error.\");\n            return C_ERR;\n        }\n    }\n    /* We correctly switched on AOF, now wait for the rewrite to be complete\n     * in order to append data on disk. */\n    g_pserver->aof_state = AOF_WAIT_REWRITE;\n    g_pserver->aof_last_fsync = g_pserver->unixtime;\n    g_pserver->aof_fd = newfd;\n\n    /* If AOF fsync error in bio job, we just ignore it and log the event. */\n    int aof_bio_fsync_status;\n    atomicGet(g_pserver->aof_bio_fsync_status, aof_bio_fsync_status);\n    if (aof_bio_fsync_status == C_ERR) {\n        serverLog(LL_WARNING,\n            \"AOF reopen, just ignore the AOF fsync error in bio job\");\n        atomicSet(g_pserver->aof_bio_fsync_status,C_OK);\n    }\n\n    /* If AOF was in error state, we just ignore it and log the event. */\n    if (g_pserver->aof_last_write_status == C_ERR) {\n        serverLog(LL_WARNING,\"AOF reopen, just ignore the last error.\");\n        g_pserver->aof_last_write_status = C_OK;\n    }\n    return C_OK;\n}\n\n/* This is a wrapper to the write syscall in order to retry on short writes\n * or if the syscall gets interrupted. It could look strange that we retry\n * on short writes given that we are writing to a block device: normally if\n * the first call is short, there is a end-of-space condition, so the next\n * is likely to fail. However apparently in modern systems this is no longer\n * true, and in general it looks just more resilient to retry the write. If\n * there is an actual error condition we'll get it at the next try. */\nssize_t aofWrite(int fd, const char *buf, size_t len) {\n    ssize_t nwritten = 0, totwritten = 0;\n\n    while(len) {\n        nwritten = write(fd, buf, len);\n\n        if (nwritten < 0) {\n            if (errno == EINTR) continue;\n            return totwritten ? totwritten : -1;\n        }\n\n        len -= nwritten;\n        buf += nwritten;\n        totwritten += nwritten;\n    }\n\n    return totwritten;\n}\n\n/* Write the append only file buffer on disk.\n *\n * Since we are required to write the AOF before replying to the client,\n * and the only way the client socket can get a write is entering when the\n * the event loop, we accumulate all the AOF writes in a memory\n * buffer and write it on disk using this function just before entering\n * the event loop again.\n *\n * About the 'force' argument:\n *\n * When the fsync policy is set to 'everysec' we may delay the flush if there\n * is still an fsync() going on in the background thread, since for instance\n * on Linux write(2) will be blocked by the background fsync anyway.\n * When this happens we remember that there is some aof buffer to be\n * flushed ASAP, and will try to do that in the serverCron() function.\n *\n * However if force is set to 1 we'll write regardless of the background\n * fsync. */\n#define AOF_WRITE_LOG_ERROR_RATE 30 /* Seconds between errors logging. */\nvoid flushAppendOnlyFile(int force) {\n    ssize_t nwritten;\n    int sync_in_progress = 0;\n    mstime_t latency;\n\n    if (serverTL->fRetrySetAofEvent)\n        installAofRewriteEvent();\n\n    if (sdslen(g_pserver->aof_buf) == 0) {\n        /* Check if we need to do fsync even the aof buffer is empty,\n         * because previously in AOF_FSYNC_EVERYSEC mode, fsync is\n         * called only when aof buffer is not empty, so if users\n         * stop write commands before fsync called in one second,\n         * the data in page cache cannot be flushed in time. */\n        if (g_pserver->aof_fsync == AOF_FSYNC_EVERYSEC &&\n            g_pserver->aof_fsync_offset != g_pserver->aof_current_size &&\n            g_pserver->unixtime > g_pserver->aof_last_fsync &&\n            !(sync_in_progress = aofFsyncInProgress())) {\n            goto try_fsync;\n        } else {\n            return;\n        }\n    }\n\n    if (g_pserver->aof_fsync == AOF_FSYNC_EVERYSEC)\n        sync_in_progress = aofFsyncInProgress();\n\n    if (g_pserver->aof_fsync == AOF_FSYNC_EVERYSEC && !force) {\n        /* With this append fsync policy we do background fsyncing.\n         * If the fsync is still in progress we can try to delay\n         * the write for a couple of seconds. */\n        if (sync_in_progress) {\n            if (g_pserver->aof_flush_postponed_start == 0) {\n                /* No previous write postponing, remember that we are\n                 * postponing the flush and return. */\n                g_pserver->aof_flush_postponed_start = g_pserver->unixtime;\n                return;\n            } else if (g_pserver->unixtime - g_pserver->aof_flush_postponed_start < 2) {\n                /* We were already waiting for fsync to finish, but for less\n                 * than two seconds this is still ok. Postpone again. */\n                return;\n            }\n            /* Otherwise fall trough, and go write since we can't wait\n             * over two seconds. */\n            g_pserver->aof_delayed_fsync++;\n            serverLog(LL_NOTICE,\"Asynchronous AOF fsync is taking too long (disk is busy?). Writing the AOF buffer without waiting for fsync to complete, this may slow down KeyDB.\");\n        }\n    }\n    /* We want to perform a single write. This should be guaranteed atomic\n     * at least if the filesystem we are writing is a real physical one.\n     * While this will save us against the server being killed I don't think\n     * there is much to do about the whole server stopping for power problems\n     * or alike */\n\n    if (g_pserver->aof_flush_sleep && sdslen(g_pserver->aof_buf)) {\n        usleep(g_pserver->aof_flush_sleep);\n    }\n\n    latencyStartMonitor(latency);\n    nwritten = aofWrite(g_pserver->aof_fd,g_pserver->aof_buf,sdslen(g_pserver->aof_buf));\n    latencyEndMonitor(latency);\n    /* We want to capture different events for delayed writes:\n     * when the delay happens with a pending fsync, or with a saving child\n     * active, and when the above two conditions are missing.\n     * We also use an additional event name to save all samples which is\n     * useful for graphing / monitoring purposes. */\n    if (sync_in_progress) {\n        latencyAddSampleIfNeeded(\"aof-write-pending-fsync\",latency);\n    } else if (hasActiveChildProcessOrBGSave()) {\n        latencyAddSampleIfNeeded(\"aof-write-active-child\",latency);\n    } else {\n        latencyAddSampleIfNeeded(\"aof-write-alone\",latency);\n    }\n    latencyAddSampleIfNeeded(\"aof-write\",latency);\n\n    /* We performed the write so reset the postponed flush sentinel to zero. */\n    g_pserver->aof_flush_postponed_start = 0;\n\n    if (nwritten != (ssize_t)sdslen(g_pserver->aof_buf)) {\n        static time_t last_write_error_log = 0;\n        int can_log = 0;\n\n        /* Limit logging rate to 1 line per AOF_WRITE_LOG_ERROR_RATE seconds. */\n        if ((g_pserver->unixtime - last_write_error_log) > AOF_WRITE_LOG_ERROR_RATE) {\n            can_log = 1;\n            last_write_error_log = g_pserver->unixtime;\n        }\n\n        /* Log the AOF write error and record the error code. */\n        if (nwritten == -1) {\n            if (can_log) {\n                serverLog(LL_WARNING,\"Error writing to the AOF file: %s\",\n                    strerror(errno));\n                g_pserver->aof_last_write_errno = errno;\n            }\n        } else {\n            if (can_log) {\n                serverLog(LL_WARNING,\"Short write while writing to \"\n                                       \"the AOF file: (nwritten=%lld, \"\n                                       \"expected=%lld)\",\n                                       (long long)nwritten,\n                                       (long long)sdslen(g_pserver->aof_buf));\n            }\n\n            if (ftruncate(g_pserver->aof_fd, g_pserver->aof_current_size) == -1) {\n                if (can_log) {\n                    serverLog(LL_WARNING, \"Could not remove short write \"\n                             \"from the append-only file.  KeyDB may refuse \"\n                             \"to load the AOF the next time it starts.  \"\n                             \"ftruncate: %s\", strerror(errno));\n                }\n            } else {\n                /* If the ftruncate() succeeded we can set nwritten to\n                 * -1 since there is no longer partial data into the AOF. */\n                nwritten = -1;\n            }\n            g_pserver->aof_last_write_errno = ENOSPC;\n        }\n\n        /* Handle the AOF write error. */\n        if (g_pserver->aof_fsync == AOF_FSYNC_ALWAYS) {\n            /* We can't recover when the fsync policy is ALWAYS since the reply\n             * for the client is already in the output buffers (both writes and\n             * reads), and the changes to the db can't be rolled back. Since we\n             * have a contract with the user that on acknowledged or observed\n             * writes are is synced on disk, we must exit. */\n            serverLog(LL_WARNING,\"Can't recover from AOF write error when the AOF fsync policy is 'always'. Exiting...\");\n            exit(1);\n        } else {\n            /* Recover from failed write leaving data into the buffer. However\n             * set an error to stop accepting writes as long as the error\n             * condition is not cleared. */\n            g_pserver->aof_last_write_status = C_ERR;\n\n            /* Trim the sds buffer if there was a partial write, and there\n             * was no way to undo it with ftruncate(2). */\n            if (nwritten > 0) {\n                g_pserver->aof_current_size += nwritten;\n                sdsrange(g_pserver->aof_buf,nwritten,-1);\n            }\n            return; /* We'll try again on the next call... */\n        }\n    } else {\n        /* Successful write(2). If AOF was in error state, restore the\n         * OK state and log the event. */\n        if (g_pserver->aof_last_write_status == C_ERR) {\n            serverLog(LL_WARNING,\n                \"AOF write error looks solved, KeyDB can write again.\");\n            g_pserver->aof_last_write_status = C_OK;\n        }\n    }\n    g_pserver->aof_current_size += nwritten;\n\n    /* Re-use AOF buffer when it is small enough. The maximum comes from the\n     * arena size of 4k minus some overhead (but is otherwise arbitrary). */\n    if ((sdslen(g_pserver->aof_buf)+sdsavail(g_pserver->aof_buf)) < 4000) {\n        sdsclear(g_pserver->aof_buf);\n    } else {\n        sdsfree(g_pserver->aof_buf);\n        g_pserver->aof_buf = sdsempty();\n    }\n\ntry_fsync:\n    /* Don't fsync if no-appendfsync-on-rewrite is set to yes and there are\n     * children doing I/O in the background. */\n    if (g_pserver->aof_no_fsync_on_rewrite && hasActiveChildProcessOrBGSave())\n        return;\n\n    /* Perform the fsync if needed. */\n    if (g_pserver->aof_fsync == AOF_FSYNC_ALWAYS) {\n        /* redis_fsync is defined as fdatasync() for Linux in order to avoid\n         * flushing metadata. */\n        latencyStartMonitor(latency);\n        /* Let's try to get this data on the disk. To guarantee data safe when\n         * the AOF fsync policy is 'always', we should exit if failed to fsync\n         * AOF (see comment next to the exit(1) after write error above). */\n        if (redis_fsync(g_pserver->aof_fd) == -1) {\n            serverLog(LL_WARNING,\"Can't persist AOF for fsync error when the \"\n              \"AOF fsync policy is 'always': %s. Exiting...\", strerror(errno));\n            exit(1);\n        }\n        latencyEndMonitor(latency);\n        latencyAddSampleIfNeeded(\"aof-fsync-always\",latency);\n        g_pserver->aof_fsync_offset = g_pserver->aof_current_size;\n        g_pserver->aof_last_fsync = g_pserver->unixtime;\n    } else if ((g_pserver->aof_fsync == AOF_FSYNC_EVERYSEC &&\n                g_pserver->unixtime > g_pserver->aof_last_fsync)) {\n        if (!sync_in_progress) {\n            aof_background_fsync(g_pserver->aof_fd);\n            g_pserver->aof_fsync_offset = g_pserver->aof_current_size;\n        }\n        g_pserver->aof_last_fsync = g_pserver->unixtime;\n    }\n}\n\nsds catAppendOnlyGenericCommand(sds dst, int argc, robj **argv) {\n    char buf[32];\n    int len, j;\n    robj *o;\n\n    buf[0] = '*';\n    len = 1+ll2string(buf+1,sizeof(buf)-1,argc);\n    buf[len++] = '\\r';\n    buf[len++] = '\\n';\n    dst = sdscatlen(dst,buf,len);\n\n    for (j = 0; j < argc; j++) {\n        if (sdsEncodedObject(argv[j]))\n            o = argv[j];\n        else\n            o = getDecodedObject(argv[j]);\n        buf[0] = '$';\n        len = 1+ll2string(buf+1,sizeof(buf)-1,sdslen(szFromObj(o)));\n        buf[len++] = '\\r';\n        buf[len++] = '\\n';\n        dst = sdscatlen(dst,buf,len);\n        dst = sdscatlen(dst,ptrFromObj(o),sdslen(szFromObj(o)));\n        dst = sdscatlen(dst,\"\\r\\n\",2);\n        if (o != argv[j])\n            decrRefCount(o);\n    }\n    return dst;\n}\n\n/* Create the sds representation of a PEXPIREAT command, using\n * 'seconds' as time to live and 'cmd' to understand what command\n * we are translating into a PEXPIREAT.\n *\n * This command is used in order to translate EXPIRE and PEXPIRE commands\n * into PEXPIREAT command so that we retain precision in the append only\n * file, and the time is always absolute and not relative. */\nsds catAppendOnlyExpireAtCommand(sds buf, struct redisCommand *cmd, robj *key, robj *seconds) {\n    long long when;\n    robj *argv[3];\n\n    /* Make sure we can use strtoll */\n    seconds = getDecodedObject(seconds);\n    when = strtoll(szFromObj(seconds),NULL,10);\n    /* Convert argument into milliseconds for EXPIRE, SETEX, EXPIREAT */\n    if (cmd->proc == expireCommand || cmd->proc == setexCommand ||\n        cmd->proc == expireatCommand)\n    {\n        when *= 1000;\n    }\n    /* Convert into absolute time for EXPIRE, PEXPIRE, SETEX, PSETEX */\n    if (cmd->proc == expireCommand || cmd->proc == pexpireCommand ||\n        cmd->proc == setexCommand || cmd->proc == psetexCommand)\n    {\n        when += mstime();\n    }\n    decrRefCount(seconds);\n\n    argv[0] = shared.pexpireat;\n    argv[1] = key;\n    argv[2] = createStringObjectFromLongLong(when);\n    buf = catAppendOnlyGenericCommand(buf, 3, argv);\n    decrRefCount(argv[2]);\n    return buf;\n}\n\nsds catAppendOnlyExpireMemberAtCommand(sds buf, struct redisCommand *cmd, robj **argv, const size_t argc) {\n    long long when = 0;\n    int unit = UNIT_SECONDS;\n    bool fAbsolute = false;\n    \n    if (cmd->proc == expireMemberCommand) {\n        if (getLongLongFromObject(argv[3], &when) != C_OK)\n            serverPanic(\"propogating invalid EXPIREMEMBER command\");\n        \n        if (argc == 5) {\n            unit = parseUnitString(szFromObj(argv[4]));\n        }\n    } else if (cmd->proc == expireMemberAtCommand) {\n        if (getLongLongFromObject(argv[3], &when) != C_OK)\n            serverPanic(\"propogating invalid EXPIREMEMBERAT command\");\n        fAbsolute = true;\n    } else if (cmd->proc == pexpireMemberAtCommand) {\n        if (getLongLongFromObject(argv[3], &when) != C_OK)\n            serverPanic(\"propogating invalid PEXPIREMEMBERAT command\");\n        fAbsolute = true;\n        unit = UNIT_MILLISECONDS;\n    } else {\n        serverPanic(\"Unknown expiremember command\");\n    }\n\n    switch (unit)\n    {\n    case UNIT_SECONDS:\n        when *= 1000;\n        break;\n\n    case UNIT_MILLISECONDS:\n        break;\n    }\n\n    if (!fAbsolute)\n        when += mstime();\n    \n    robj *argvNew[4];\n    argvNew[0] = shared.pexpirememberat;\n    argvNew[1] = argv[1];\n    argvNew[2] = argv[2];\n    argvNew[3] = createStringObjectFromLongLong(when);\n    buf = catAppendOnlyGenericCommand(buf, 4, argvNew);\n    decrRefCount(argvNew[0]);\n    decrRefCount(argvNew[3]);\n    return buf;\n}\n\nsds catCommandForAofAndActiveReplication(sds buf, struct redisCommand *cmd, robj **argv, int argc)\n{   \n    if (cmd->proc == expireCommand || cmd->proc == pexpireCommand ||\n        cmd->proc == expireatCommand) {\n        /* Translate EXPIRE/PEXPIRE/EXPIREAT into PEXPIREAT */\n        buf = catAppendOnlyExpireAtCommand(buf,cmd,argv[1],argv[2]);\n    } else if (cmd->proc == setCommand && argc > 3) {\n        robj *pxarg = NULL;\n        /* When SET is used with EX/PX argument setGenericCommand propagates them with PX millisecond argument.\n         * So since the command arguments are re-written there, we can rely here on the index of PX being 3. */\n        if (!strcasecmp(szFromObj(argv[3]), \"px\")) {\n            pxarg = argv[4];\n        }\n        /* For AOF we convert SET key value relative time in milliseconds to SET key value absolute time in\n         * millisecond. Whenever the condition is true it implies that original SET has been transformed\n         * to SET PX with millisecond time argument so we do not need to worry about unit here.*/\n        if (pxarg) {\n            robj *millisecond = getDecodedObject(pxarg);\n            long long when = strtoll(szFromObj(millisecond),NULL,10);\n            when += mstime();\n\n            decrRefCount(millisecond);\n\n            robj *newargs[5];\n            newargs[0] = argv[0];\n            newargs[1] = argv[1];\n            newargs[2] = argv[2];\n            newargs[3] = shared.pxat;\n            newargs[4] = createStringObjectFromLongLong(when);\n            buf = catAppendOnlyGenericCommand(buf,5,newargs);\n            decrRefCount(newargs[4]);\n        } else {\n            buf = catAppendOnlyGenericCommand(buf,argc,argv);\n        }\n    } else if (cmd->proc == expireMemberCommand || cmd->proc == expireMemberAtCommand || cmd->proc == pexpireMemberAtCommand) {\n        /* Translate subkey expire commands to PEXPIREMEMBERAT */\n        buf = catAppendOnlyExpireMemberAtCommand(buf, cmd, argv, argc);\n    } else {\n        /* All the other commands don't need translation or need the\n         * same translation already operated in the command vector\n         * for the replication itself. */\n        buf = catAppendOnlyGenericCommand(buf,argc,argv);\n    }\n\n    return buf;\n}\n\nvoid feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv, int argc) {\n    sds buf = sdsempty();\n\n    /* The DB this command was targeting is not the same as the last command\n     * we appended. To issue a SELECT command is needed. */\n    if (dictid != g_pserver->aof_selected_db) {\n        char seldb[64];\n\n        snprintf(seldb,sizeof(seldb),\"%d\",dictid);\n        buf = sdscatprintf(buf,\"*2\\r\\n$6\\r\\nSELECT\\r\\n$%lu\\r\\n%s\\r\\n\",\n            (unsigned long)strlen(seldb),seldb);\n        g_pserver->aof_selected_db = dictid;\n    }\n\n    buf = catCommandForAofAndActiveReplication(buf, cmd, argv, argc);\n\n    /* Append to the AOF buffer. This will be flushed on disk just before\n     * of re-entering the event loop, so before the client will get a\n     * positive reply about the operation performed. */\n    if (g_pserver->aof_state == AOF_ON)\n        g_pserver->aof_buf = sdscatlen(g_pserver->aof_buf,buf,sdslen(buf));\n\n    /* If a background append only file rewriting is in progress we want to\n     * accumulate the differences between the child DB and the current one\n     * in a buffer, so that when the child process will do its work we\n     * can append the differences to the new append only file. */\n    if (hasActiveChildProcess() && g_pserver->child_type == CHILD_TYPE_AOF)\n        aofRewriteBufferAppend((unsigned char*)buf,sdslen(buf));\n\n    sdsfree(buf);\n}\n\n/* ----------------------------------------------------------------------------\n * AOF loading\n * ------------------------------------------------------------------------- */\n\n/* In Redis commands are always executed in the context of a client, so in\n * order to load the append only file we need to create a fake client. */\nstruct client *createAOFClient(void) {\n    struct client *c = new client();\n\n    selectDb(c,0);\n    c->id = CLIENT_ID_AOF; /* So modules can identify it's the AOF client. */\n    c->conn = NULL;\n    c->iel = IDX_EVENT_LOOP_MAIN;\n    c->name = NULL;\n    c->querybuf = sdsempty();\n    c->querybuf_peak = 0;\n    c->argc = 0;\n    c->argv = NULL;\n    c->original_argc = 0;\n    c->original_argv = NULL;\n    c->bufpos = 0;\n    c->fPendingAsyncWrite = FALSE;\n    c->fPendingAsyncWriteHandler = FALSE;\n\n    /*\n     * The AOF client should never be blocked (unlike master\n     * replication connection).\n     * This is because blocking the AOF client might cause\n     * deadlock (because potentially no one will unblock it).\n     * Also, if the AOF client will be blocked just for\n     * background processing there is a chance that the\n     * command execution order will be violated.\n     */\n    c->flags = CLIENT_DENY_BLOCKING;\n\n    c->btype = BLOCKED_NONE;\n    /* We set the fake client as a replica waiting for the synchronization\n     * so that Redis will not try to send replies to this client. */\n    c->replstate = SLAVE_STATE_WAIT_BGSAVE_START;\n    c->reply = listCreate();\n    c->reply_bytes = 0;\n    c->obuf_soft_limit_reached_time = 0;\n    c->watched_keys = listCreate();\n    c->peerid = NULL;\n    c->sockname = NULL;\n    c->resp = 2;\n    c->user = NULL;\n    c->mvccCheckpoint = 0;\n    listSetFreeMethod(c->reply,freeClientReplyValue);\n    listSetDupMethod(c->reply,dupClientReplyValue);\n    fastlock_init(&c->lock, \"fake client\");\n    fastlock_lock(&c->lock);\n    initClientMultiState(c);\n    return c;\n}\n\nvoid freeFakeClientArgv(struct client *c) {\n    int j;\n\n    for (j = 0; j < c->argc; j++)\n        decrRefCount(c->argv[j]);\n    zfree(c->argv);\n    c->argv_len_sumActive = 0;\n}\n\nvoid freeFakeClient(struct client *c) {\n    sdsfree(c->querybuf);\n    listRelease(c->reply);\n    listRelease(c->watched_keys);\n    freeClientMultiState(c);\n    freeClientOriginalArgv(c);\n    fastlock_unlock(&c->lock);\n    fastlock_free(&c->lock);\n    delete c;\n}\n\n/* Replay the append log file. On success C_OK is returned. On non fatal\n * error (the append only file is zero-length) C_ERR is returned. On\n * fatal error an error message is logged and the program exists. */\nint loadAppendOnlyFile(char *filename) {\n    struct client *fakeClient;\n    FILE *fp = fopen(filename,\"r\");\n    struct redis_stat sb;\n    int old_aof_state = g_pserver->aof_state;\n    long loops = 0;\n    off_t valid_up_to = 0; /* Offset of latest well-formed command loaded. */\n    off_t valid_before_multi = 0; /* Offset before MULTI command loaded. */\n    serverAssert(serverTL != NULL); // This happens early in boot, ensure serverTL was setup\n\n    if (fp == NULL) {\n        serverLog(LL_WARNING,\"Fatal error: can't open the append log file for reading: %s\",strerror(errno));\n        exit(1);\n    }\n\n    /* Handle a zero-length AOF file as a special case. An empty AOF file\n     * is a valid AOF because an empty server with AOF enabled will create\n     * a zero length file at startup, that will remain like that if no write\n     * operation is received. */\n    if (fp && redis_fstat(fileno(fp),&sb) != -1 && sb.st_size == 0) {\n        g_pserver->aof_current_size = 0;\n        g_pserver->aof_fsync_offset = g_pserver->aof_current_size;\n        fclose(fp);\n        return C_ERR;\n    }\n\n    /* Temporarily disable AOF, to prevent EXEC from feeding a MULTI\n     * to the same file we're about to read. */\n    g_pserver->aof_state = AOF_OFF;\n\n    fakeClient = createAOFClient();\n    startLoadingFile(fp, filename, RDBFLAGS_AOF_PREAMBLE);\n\n    for (int idb = 0; idb < cserver.dbnum; ++idb)\n    {\n        g_pserver->db[idb]->trackChanges(true);\n    }\n\n    /* Check if this AOF file has an RDB preamble. In that case we need to\n     * load the RDB file and later continue loading the AOF tail. */\n    char sig[5]; /* \"REDIS\" */\n    if (fread(sig,1,5,fp) != 5 || memcmp(sig,\"REDIS\",5) != 0) {\n        /* No RDB preamble, seek back at 0 offset. */\n        if (fseek(fp,0,SEEK_SET) == -1) goto readerr;\n    } else {\n        /* RDB preamble. Pass loading the RDB functions. */\n        rio rdb;\n        rdbSaveInfo rsi;\n\n        serverLog(LL_NOTICE,\"Reading RDB preamble from AOF file...\");\n        if (fseek(fp,0,SEEK_SET) == -1) goto readerr;\n        rioInitWithFile(&rdb,fp);\n        if (rdbLoadRio(&rdb,RDBFLAGS_AOF_PREAMBLE,&rsi) != C_OK) {\n            serverLog(LL_WARNING,\"Error reading the RDB preamble of the AOF file, AOF loading aborted\");\n            goto readerr;\n        } else {\n            serverLog(LL_NOTICE,\"Reading the remaining AOF tail...\");\n        }\n    }\n\n    /* Read the actual AOF file, in REPL format, command by command. */\n    while(1) {\n        int argc, j;\n        unsigned long len;\n        robj **argv;\n        char buf[128];\n        sds argsds;\n        struct redisCommand *cmd;\n\n        /* Serve the clients from time to time */\n        if (!(loops++ % 1000)) {\n            loadingProgress(ftello(fp));\n            processEventsWhileBlocked(serverTL - g_pserver->rgthreadvar);\n            processModuleLoadingProgressEvent(1);\n        }\n\n        if (fgets(buf,sizeof(buf),fp) == NULL) {\n            if (feof(fp))\n                break;\n            else\n                goto readerr;\n        }\n        if (buf[0] != '*') goto fmterr;\n        if (buf[1] == '\\0') goto readerr;\n        argc = atoi(buf+1);\n        if (argc < 1) goto fmterr;\n\n        /* Load the next command in the AOF as our fake client\n         * argv. */\n        argv = (robj**)zmalloc(sizeof(robj*)*argc, MALLOC_LOCAL);\n        fakeClient->argc = argc;\n        fakeClient->argv = argv;\n\n        for (j = 0; j < argc; j++) {\n            /* Parse the argument len. */\n            char *readres = fgets(buf,sizeof(buf),fp);\n            if (readres == NULL || buf[0] != '$') {\n                fakeClient->argc = j; /* Free up to j-1. */\n                freeFakeClientArgv(fakeClient);\n                if (readres == NULL)\n                    goto readerr;\n                else\n                    goto fmterr;\n            }\n            len = strtol(buf+1,NULL,10);\n\n            /* Read it into a string object. */\n            argsds = sdsnewlen(SDS_NOINIT,len);\n            if (len && fread(argsds,len,1,fp) == 0) {\n                sdsfree(argsds);\n                fakeClient->argc = j; /* Free up to j-1. */\n                freeFakeClientArgv(fakeClient);\n                goto readerr;\n            }\n            argv[j] = createObject(OBJ_STRING,argsds);\n\n            /* Discard CRLF. */\n            if (fread(buf,2,1,fp) == 0) {\n                fakeClient->argc = j+1; /* Free up to j. */\n                freeFakeClientArgv(fakeClient);\n                goto readerr;\n            }\n        }\n\n        /* Command lookup */\n        cmd = lookupCommand(szFromObj(argv[0]));\n        if (!cmd) {\n            serverLog(LL_WARNING,\n                \"Unknown command '%s' reading the append only file\",\n                (char*)ptrFromObj(argv[0]));\n            exit(1);\n        }\n\n        if (cmd == cserver.multiCommand) valid_before_multi = valid_up_to;\n\n        /* Run the command in the context of a fake client */\n        fakeClient->cmd = fakeClient->lastcmd = cmd;\n        if (fakeClient->flags & CLIENT_MULTI &&\n            fakeClient->cmd->proc != execCommand)\n        {\n            queueMultiCommand(fakeClient);\n        } else {\n            cmd->proc(fakeClient);\n        }\n\n        /* The fake client should not have a reply */\n        serverAssert(fakeClient->bufpos == 0 &&\n                     listLength(fakeClient->reply) == 0);\n\n        /* The fake client should never get blocked */\n        serverAssert((fakeClient->flags & CLIENT_BLOCKED) == 0);\n\n        /* Clean up. Command code may have changed argv/argc so we use the\n         * argv/argc of the client instead of the local variables. */\n        freeFakeClientArgv(fakeClient);\n        fakeClient->cmd = NULL;\n        if (g_pserver->aof_load_truncated) valid_up_to = ftello(fp);\n        if (g_pserver->key_load_delay)\n            debugDelay(g_pserver->key_load_delay);\n    }\n\n    /* This point can only be reached when EOF is reached without errors.\n     * If the client is in the middle of a MULTI/EXEC, handle it as it was\n     * a short read, even if technically the protocol is correct: we want\n     * to remove the unprocessed tail and continue. */\n    if (fakeClient->flags & CLIENT_MULTI) {\n        serverLog(LL_WARNING,\n            \"Revert incomplete MULTI/EXEC transaction in AOF file\");\n        valid_up_to = valid_before_multi;\n        goto uxeof;\n    }\n\nloaded_ok: /* DB loaded, cleanup and return C_OK to the caller. */\n    for (int idb = 0; idb < cserver.dbnum; ++idb)\n    {\n        if (g_pserver->db[idb]->processChanges(false))\n            g_pserver->db[idb]->commitChanges();\n    }\n    fclose(fp);\n    freeFakeClient(fakeClient);\n    g_pserver->aof_state = old_aof_state;\n    stopLoading(1);\n    aofUpdateCurrentSize();\n    g_pserver->aof_rewrite_base_size = g_pserver->aof_current_size;\n    g_pserver->aof_fsync_offset = g_pserver->aof_current_size;\n    return C_OK;\n\nreaderr: /* Read error. If feof(fp) is true, fall through to unexpected EOF. */\n    if (!feof(fp)) {\n        if (fakeClient) freeFakeClient(fakeClient); /* avoid valgrind warning */\n        fclose(fp);\n        serverLog(LL_WARNING,\"Unrecoverable error reading the append only file: %s\", strerror(errno));\n        exit(1);\n    }\n\nuxeof: /* Unexpected AOF end of file. */\n    if (g_pserver->aof_load_truncated) {\n        serverLog(LL_WARNING,\"!!! Warning: short read while loading the AOF file !!!\");\n        serverLog(LL_WARNING,\"!!! Truncating the AOF at offset %llu !!!\",\n            (unsigned long long) valid_up_to);\n        if (valid_up_to == -1 || truncate(filename,valid_up_to) == -1) {\n            if (valid_up_to == -1) {\n                serverLog(LL_WARNING,\"Last valid command offset is invalid\");\n            } else {\n                serverLog(LL_WARNING,\"Error truncating the AOF file: %s\",\n                    strerror(errno));\n            }\n        } else {\n            /* Make sure the AOF file descriptor points to the end of the\n             * file after the truncate call. */\n            if (g_pserver->aof_fd != -1 && lseek(g_pserver->aof_fd,0,SEEK_END) == -1) {\n                serverLog(LL_WARNING,\"Can't seek the end of the AOF file: %s\",\n                    strerror(errno));\n            } else {\n                serverLog(LL_WARNING,\n                    \"AOF loaded anyway because aof-load-truncated is enabled\");\n                goto loaded_ok;\n            }\n        }\n    }\n    if (fakeClient) freeFakeClient(fakeClient); /* avoid valgrind warning */\n    fclose(fp);\n    serverLog(LL_WARNING,\"Unexpected end of file reading the append only file. You can: 1) Make a backup of your AOF file, then use ./keydb-check-aof --fix <filename>. 2) Alternatively you can set the 'aof-load-truncated' configuration option to yes and restart the server.\");\n    exit(1);\n\nfmterr: /* Format error. */\n    if (fakeClient) freeFakeClient(fakeClient); /* avoid valgrind warning */\n    fclose(fp);\n    serverLog(LL_WARNING,\"Bad file format reading the append only file: make a backup of your AOF file, then use ./keydb-check-aof --fix <filename>\");\n    exit(1);\n}\n\n/* ----------------------------------------------------------------------------\n * AOF rewrite\n * ------------------------------------------------------------------------- */\n\n/* Delegate writing an object to writing a bulk string or bulk long long.\n * This is not placed in rio.c since that adds the g_pserver->h dependency. */\nint rioWriteBulkObject(rio *r, robj *obj) {\n    /* Avoid using getDecodedObject to help copy-on-write (we are often\n     * in a child process when this function is called). */\n    if (obj->encoding == OBJ_ENCODING_INT) {\n        return rioWriteBulkLongLong(r,(long)obj->m_ptr);\n    } else if (sdsEncodedObject(obj)) {\n        return rioWriteBulkString(r,szFromObj(obj),sdslen(szFromObj(obj)));\n    } else {\n        serverPanic(\"Unknown string encoding\");\n    }\n}\n\n/* Emit the commands needed to rebuild a list object.\n * The function returns 0 on error, 1 on success. */\nint rewriteListObject(rio *r, robj *key, robj *o) {\n    long long count = 0, items = listTypeLength(o);\n\n    if (o->encoding == OBJ_ENCODING_QUICKLIST) {\n        quicklist *list = (quicklist*)ptrFromObj(o);\n        quicklistIter *li = quicklistGetIterator(list, AL_START_HEAD);\n        quicklistEntry entry;\n\n        while (quicklistNext(li,&entry)) {\n            if (count == 0) {\n                int cmd_items = (items > AOF_REWRITE_ITEMS_PER_CMD) ?\n                    AOF_REWRITE_ITEMS_PER_CMD : items;\n                if (!rioWriteBulkCount(r,'*',2+cmd_items) ||\n                    !rioWriteBulkString(r,\"RPUSH\",5) ||\n                    !rioWriteBulkObject(r,key)) \n                {\n                    quicklistReleaseIterator(li);\n                    return 0;\n                }\n            }\n\n            if (entry.value) {\n                if (!rioWriteBulkString(r,(char*)entry.value,entry.sz)) {\n                    quicklistReleaseIterator(li);\n                    return 0;\n                }\n            } else {\n                if (!rioWriteBulkLongLong(r,entry.longval)) {\n                    quicklistReleaseIterator(li);\n                    return 0;\n                }\n            }\n            if (++count == AOF_REWRITE_ITEMS_PER_CMD) count = 0;\n            items--;\n        }\n        quicklistReleaseIterator(li);\n    } else {\n        serverPanic(\"Unknown list encoding\");\n    }\n    return 1;\n}\n\n/* Emit the commands needed to rebuild a set object.\n * The function returns 0 on error, 1 on success. */\nint rewriteSetObject(rio *r, robj *key, robj *o) {\n    long long count = 0, items = setTypeSize(o);\n\n    if (o->encoding == OBJ_ENCODING_INTSET) {\n        int ii = 0;\n        int64_t llval;\n\n        while(intsetGet((intset*)ptrFromObj(o),ii++,&llval)) {\n            if (count == 0) {\n                int cmd_items = (items > AOF_REWRITE_ITEMS_PER_CMD) ?\n                    AOF_REWRITE_ITEMS_PER_CMD : items;\n\n                if (!rioWriteBulkCount(r,'*',2+cmd_items) ||\n                    !rioWriteBulkString(r,\"SADD\",4) ||\n                    !rioWriteBulkObject(r,key)) \n                {\n                    return 0;\n                }\n            }\n            if (!rioWriteBulkLongLong(r,llval)) return 0;\n            if (++count == AOF_REWRITE_ITEMS_PER_CMD) count = 0;\n            items--;\n        }\n    } else if (o->encoding == OBJ_ENCODING_HT) {\n        dictIterator *di = dictGetIterator((dict*)ptrFromObj(o));\n        dictEntry *de;\n\n        while((de = dictNext(di)) != NULL) {\n            sds ele = (sds)dictGetKey(de);\n            if (count == 0) {\n                int cmd_items = (items > AOF_REWRITE_ITEMS_PER_CMD) ?\n                    AOF_REWRITE_ITEMS_PER_CMD : items;\n\n                if (!rioWriteBulkCount(r,'*',2+cmd_items) ||\n                    !rioWriteBulkString(r,\"SADD\",4) ||\n                    !rioWriteBulkObject(r,key)) \n                {\n                    dictReleaseIterator(di);\n                    return 0;\n                }\n            }\n            if (!rioWriteBulkString(r,ele,sdslen(ele))) {\n                dictReleaseIterator(di);\n                return 0;          \n            }\n            if (++count == AOF_REWRITE_ITEMS_PER_CMD) count = 0;\n            items--;\n        }\n        dictReleaseIterator(di);\n    } else {\n        serverPanic(\"Unknown set encoding\");\n    }\n    return 1;\n}\n\n/* Emit the commands needed to rebuild a sorted set object.\n * The function returns 0 on error, 1 on success. */\nint rewriteSortedSetObject(rio *r, robj *key, robj *o) {\n    long long count = 0, items = zsetLength(o);\n\n    if (o->encoding == OBJ_ENCODING_ZIPLIST) {\n        unsigned char *zl = (unsigned char*)ptrFromObj(o);\n        unsigned char *eptr, *sptr;\n        unsigned char *vstr;\n        unsigned int vlen;\n        long long vll;\n        double score;\n\n        eptr = ziplistIndex(zl,0);\n        serverAssert(eptr != NULL);\n        sptr = ziplistNext(zl,eptr);\n        serverAssert(sptr != NULL);\n\n        while (eptr != NULL) {\n            serverAssert(ziplistGet(eptr,&vstr,&vlen,&vll));\n            score = zzlGetScore(sptr);\n\n            if (count == 0) {\n                int cmd_items = (items > AOF_REWRITE_ITEMS_PER_CMD) ?\n                    AOF_REWRITE_ITEMS_PER_CMD : items;\n\n                if (!rioWriteBulkCount(r,'*',2+cmd_items*2) ||\n                    !rioWriteBulkString(r,\"ZADD\",4) ||\n                    !rioWriteBulkObject(r,key)) \n                {\n                    return 0;\n                }\n            }\n            if (!rioWriteBulkDouble(r,score)) return 0;\n            if (vstr != NULL) {\n                if (!rioWriteBulkString(r,(char*)vstr,vlen)) return 0;\n            } else {\n                if (!rioWriteBulkLongLong(r,vll)) return 0;\n            }\n            zzlNext(zl,&eptr,&sptr);\n            if (++count == AOF_REWRITE_ITEMS_PER_CMD) count = 0;\n            items--;\n        }\n    } else if (o->encoding == OBJ_ENCODING_SKIPLIST) {\n        zset *zs = (zset*)ptrFromObj(o);\n        dictIterator *di = dictGetIterator(zs->dict);\n        dictEntry *de;\n\n        while((de = dictNext(di)) != NULL) {\n            sds ele = (sds)dictGetKey(de);\n            double *score = (double*)dictGetVal(de);\n\n            if (count == 0) {\n                int cmd_items = (items > AOF_REWRITE_ITEMS_PER_CMD) ?\n                    AOF_REWRITE_ITEMS_PER_CMD : items;\n\n                if (!rioWriteBulkCount(r,'*',2+cmd_items*2) ||\n                    !rioWriteBulkString(r,\"ZADD\",4) ||\n                    !rioWriteBulkObject(r,key)) \n                {\n                    dictReleaseIterator(di);\n                    return 0;\n                }\n            }\n            if (!rioWriteBulkDouble(r,*score) ||\n                !rioWriteBulkString(r,ele,sdslen(ele)))\n            {\n                dictReleaseIterator(di);\n                return 0;\n            }\n            if (++count == AOF_REWRITE_ITEMS_PER_CMD) count = 0;\n            items--;\n        }\n        dictReleaseIterator(di);\n    } else {\n        serverPanic(\"Unknown sorted zset encoding\");\n    }\n    return 1;\n}\n\n/* Write either the key or the value of the currently selected item of a hash.\n * The 'hi' argument passes a valid Redis hash iterator.\n * The 'what' filed specifies if to write a key or a value and can be\n * either OBJ_HASH_KEY or OBJ_HASH_VALUE.\n *\n * The function returns 0 on error, non-zero on success. */\nstatic int rioWriteHashIteratorCursor(rio *r, hashTypeIterator *hi, int what) {\n    if (hi->encoding == OBJ_ENCODING_ZIPLIST) {\n        unsigned char *vstr = NULL;\n        unsigned int vlen = UINT_MAX;\n        long long vll = LLONG_MAX;\n\n        hashTypeCurrentFromZiplist(hi, what, &vstr, &vlen, &vll);\n        if (vstr)\n            return rioWriteBulkString(r, (char*)vstr, vlen);\n        else\n            return rioWriteBulkLongLong(r, vll);\n    } else if (hi->encoding == OBJ_ENCODING_HT) {\n        sds value = hashTypeCurrentFromHashTable(hi, what);\n        return rioWriteBulkString(r, value, sdslen(value));\n    }\n\n    serverPanic(\"Unknown hash encoding\");\n    return 0;\n}\n\n/* Emit the commands needed to rebuild a hash object.\n * The function returns 0 on error, 1 on success. */\nint rewriteHashObject(rio *r, robj *key, robj *o) {\n    hashTypeIterator *hi;\n    long long count = 0, items = hashTypeLength(o);\n\n    hi = hashTypeInitIterator(o);\n    while (hashTypeNext(hi) != C_ERR) {\n        if (count == 0) {\n            int cmd_items = (items > AOF_REWRITE_ITEMS_PER_CMD) ?\n                AOF_REWRITE_ITEMS_PER_CMD : items;\n\n            if (!rioWriteBulkCount(r,'*',2+cmd_items*2) ||\n                !rioWriteBulkString(r,\"HMSET\",5) ||\n                !rioWriteBulkObject(r,key)) \n            {\n                hashTypeReleaseIterator(hi);\n                return 0;\n            }\n        }\n\n        if (!rioWriteHashIteratorCursor(r, hi, OBJ_HASH_KEY) ||\n            !rioWriteHashIteratorCursor(r, hi, OBJ_HASH_VALUE))\n        {\n            hashTypeReleaseIterator(hi);\n            return 0;           \n        }\n        if (++count == AOF_REWRITE_ITEMS_PER_CMD) count = 0;\n        items--;\n    }\n\n    hashTypeReleaseIterator(hi);\n\n    return 1;\n}\n\n/* Helper for rewriteStreamObject() that generates a bulk string into the\n * AOF representing the ID 'id'. */\nint rioWriteBulkStreamID(rio *r,streamID *id) {\n    int retval;\n\n    sds replyid = sdscatfmt(sdsempty(),\"%U-%U\",id->ms,id->seq);\n    retval = rioWriteBulkString(r,replyid,sdslen(replyid));\n    sdsfree(replyid);\n    return retval;\n}\n\n/* Helper for rewriteStreamObject(): emit the XCLAIM needed in order to\n * add the message described by 'nack' having the id 'rawid', into the pending\n * list of the specified consumer. All this in the context of the specified\n * key and group. */\nint rioWriteStreamPendingEntry(rio *r, robj *key, const char *groupname, size_t groupname_len, streamConsumer *consumer, unsigned char *rawid, streamNACK *nack) {\n     /* XCLAIM <key> <group> <consumer> 0 <id> TIME <milliseconds-unix-time>\n               RETRYCOUNT <count> JUSTID FORCE. */\n    streamID id;\n    streamDecodeID(rawid,&id);\n    if (rioWriteBulkCount(r,'*',12) == 0) return 0;\n    if (rioWriteBulkString(r,\"XCLAIM\",6) == 0) return 0;\n    if (rioWriteBulkObject(r,key) == 0) return 0;\n    if (rioWriteBulkString(r,groupname,groupname_len) == 0) return 0;\n    if (rioWriteBulkString(r,consumer->name,sdslen(consumer->name)) == 0) return 0;\n    if (rioWriteBulkString(r,\"0\",1) == 0) return 0;\n    if (rioWriteBulkStreamID(r,&id) == 0) return 0;\n    if (rioWriteBulkString(r,\"TIME\",4) == 0) return 0;\n    if (rioWriteBulkLongLong(r,nack->delivery_time) == 0) return 0;\n    if (rioWriteBulkString(r,\"RETRYCOUNT\",10) == 0) return 0;\n    if (rioWriteBulkLongLong(r,nack->delivery_count) == 0) return 0;\n    if (rioWriteBulkString(r,\"JUSTID\",6) == 0) return 0;\n    if (rioWriteBulkString(r,\"FORCE\",5) == 0) return 0;\n    return 1;\n}\n\n/* Helper for rewriteStreamObject(): emit the XGROUP CREATECONSUMER is\n * needed in order to create consumers that do not have any pending entries.\n * All this in the context of the specified key and group. */\nint rioWriteStreamEmptyConsumer(rio *r, robj *key, const char *groupname, size_t groupname_len, streamConsumer *consumer) {\n    /* XGROUP CREATECONSUMER <key> <group> <consumer> */\n    if (rioWriteBulkCount(r,'*',5) == 0) return 0;\n    if (rioWriteBulkString(r,\"XGROUP\",6) == 0) return 0;\n    if (rioWriteBulkString(r,\"CREATECONSUMER\",14) == 0) return 0;\n    if (rioWriteBulkObject(r,key) == 0) return 0;\n    if (rioWriteBulkString(r,groupname,groupname_len) == 0) return 0;\n    if (rioWriteBulkString(r,consumer->name,sdslen(consumer->name)) == 0) return 0;\n    return 1;\n}\n\n/* Emit the commands needed to rebuild a stream object.\n * The function returns 0 on error, 1 on success. */\nint rewriteStreamObject(rio *r, robj *key, robj *o) {\n    stream *s = (stream*)ptrFromObj(o);\n    streamIterator si;\n    streamIteratorStart(&si,s,NULL,NULL,0);\n    streamID id;\n    int64_t numfields;\n\n    if (s->length) {\n        /* Reconstruct the stream data using XADD commands. */\n        while(streamIteratorGetID(&si,&id,&numfields)) {\n            /* Emit a two elements array for each item. The first is\n             * the ID, the second is an array of field-value pairs. */\n\n            /* Emit the XADD <key> <id> ...fields... command. */\n            if (!rioWriteBulkCount(r,'*',3+numfields*2) || \n                !rioWriteBulkString(r,\"XADD\",4) ||\n                !rioWriteBulkObject(r,key) ||\n                !rioWriteBulkStreamID(r,&id)) \n            {\n                streamIteratorStop(&si);\n                return 0;\n            }\n            while(numfields--) {\n                unsigned char *field, *value;\n                int64_t field_len, value_len;\n                streamIteratorGetField(&si,&field,&value,&field_len,&value_len);\n                if (!rioWriteBulkString(r,(char*)field,field_len) ||\n                    !rioWriteBulkString(r,(char*)value,value_len)) \n                {\n                    streamIteratorStop(&si);\n                    return 0;                  \n                }\n            }\n        }\n    } else {\n        /* Use the XADD MAXLEN 0 trick to generate an empty stream if\n         * the key we are serializing is an empty string, which is possible\n         * for the Stream type. */\n        id.ms = 0; id.seq = 1; \n        if (!rioWriteBulkCount(r,'*',7) ||\n            !rioWriteBulkString(r,\"XADD\",4) ||\n            !rioWriteBulkObject(r,key) ||\n            !rioWriteBulkString(r,\"MAXLEN\",6) ||\n            !rioWriteBulkString(r,\"0\",1) ||\n            !rioWriteBulkStreamID(r,&id) ||\n            !rioWriteBulkString(r,\"x\",1) ||\n            !rioWriteBulkString(r,\"y\",1))\n        {\n            streamIteratorStop(&si);\n            return 0;     \n        }\n    }\n\n    /* Append XSETID after XADD, make sure lastid is correct,\n     * in case of XDEL lastid. */\n    if (!rioWriteBulkCount(r,'*',3) ||\n        !rioWriteBulkString(r,\"XSETID\",6) ||\n        !rioWriteBulkObject(r,key) ||\n        !rioWriteBulkStreamID(r,&s->last_id)) \n    {\n        streamIteratorStop(&si);\n        return 0; \n    }\n\n\n    /* Create all the stream consumer groups. */\n    if (s->cgroups) {\n        raxIterator ri;\n        raxStart(&ri,s->cgroups);\n        raxSeek(&ri,\"^\",NULL,0);\n        while(raxNext(&ri)) {\n            streamCG *group = (streamCG*)ri.data;\n            /* Emit the XGROUP CREATE in order to create the group. */\n            if (!rioWriteBulkCount(r,'*',5) ||\n                !rioWriteBulkString(r,\"XGROUP\",6) ||\n                !rioWriteBulkString(r,\"CREATE\",6) ||\n                !rioWriteBulkObject(r,key) ||\n                !rioWriteBulkString(r,(char*)ri.key,ri.key_len) ||\n                !rioWriteBulkStreamID(r,&group->last_id)) \n            {\n                raxStop(&ri);\n                streamIteratorStop(&si);\n                return 0;\n            }\n\n            /* Generate XCLAIMs for each consumer that happens to\n             * have pending entries. Empty consumers would be generated with\n             * XGROUP CREATECONSUMER. */\n            raxIterator ri_cons;\n            raxStart(&ri_cons,group->consumers);\n            raxSeek(&ri_cons,\"^\",NULL,0);\n            while(raxNext(&ri_cons)) {\n                streamConsumer *consumer = (streamConsumer*)ri_cons.data;\n                /* If there are no pending entries, just emit XGROUP CREATECONSUMER */\n                if (raxSize(consumer->pel) == 0) {\n                    if (rioWriteStreamEmptyConsumer(r,key,(char*)ri.key,\n                                                    ri.key_len,consumer) == 0)\n                    {\n                        raxStop(&ri_cons);\n                        raxStop(&ri);\n                        streamIteratorStop(&si);\n                        return 0;\n                    }\n                    continue;\n                }\n                /* For the current consumer, iterate all the PEL entries\n                 * to emit the XCLAIM protocol. */\n                raxIterator ri_pel;\n                raxStart(&ri_pel,consumer->pel);\n                raxSeek(&ri_pel,\"^\",NULL,0);\n                while(raxNext(&ri_pel)) {\n                    streamNACK *nack = (streamNACK*)ri_pel.data;\n                    if (rioWriteStreamPendingEntry(r,key,(char*)ri.key,\n                                                   ri.key_len,consumer,\n                                                   ri_pel.key,nack) == 0)\n                    {\n                        raxStop(&ri_pel);\n                        raxStop(&ri_cons);\n                        raxStop(&ri);\n                        streamIteratorStop(&si);\n                        return 0;\n                    }\n                }\n                raxStop(&ri_pel);\n            }\n            raxStop(&ri_cons);\n        }\n        raxStop(&ri);\n    }\n\n    streamIteratorStop(&si);\n    return 1;\n}\n\n/* Call the module type callback in order to rewrite a data type\n * that is exported by a module and is not handled by Redis itself.\n * The function returns 0 on error, 1 on success. */\nint rewriteModuleObject(rio *r, robj *key, robj *o) {\n    RedisModuleIO io;\n    moduleValue *mv = (moduleValue*)ptrFromObj(o);\n    moduleType *mt = mv->type;\n    moduleInitIOContext(io,mt,r,key);\n    mt->aof_rewrite(&io,key,mv->value);\n    if (io.ctx) {\n        moduleFreeContext(io.ctx);\n        zfree(io.ctx);\n    }\n    return io.error ? 0 : 1;\n}\n\n/* This function is called by the child rewriting the AOF file to read\n * the difference accumulated from the parent into a buffer, that is\n * concatenated at the end of the rewrite. */\nssize_t aofReadDiffFromParent(void) {\n    char buf[65536]; /* Default pipe buffer size on most Linux systems. */\n    ssize_t nread, total = 0;\n\n    while ((nread =\n            read(g_pserver->aof_pipe_read_data_from_parent,buf,sizeof(buf))) > 0) {\n        g_pserver->aof_child_diff = sdscatlen(g_pserver->aof_child_diff,buf,nread);\n        total += nread;\n    }\n    return total;\n}\n\nint rewriteAppendOnlyFileRio(rio *aof) {\n    size_t processed = 0;\n    int j;\n    long key_count = 0;\n    long long updated_time = 0;\n\n    for (j = 0; j < cserver.dbnum; j++) {\n        char selectcmd[] = \"*2\\r\\n$6\\r\\nSELECT\\r\\n\";\n        redisDb *db = g_pserver->db[j];\n        if (db->size() == 0) continue;\n\n        /* SELECT the new DB */\n        if (rioWrite(aof,selectcmd,sizeof(selectcmd)-1) == 0) goto werr;\n        if (rioWriteBulkLongLong(aof,j) == 0) goto werr;\n\n        /* Iterate this DB writing every entry */\n        bool fComplete = db->iterate([&](const char *keystr, robj *o)->bool{\n            redisObjectStack key;\n            initStaticStringObject(key,(sds)keystr);\n\n            /* Save the key and associated value */\n            if (o->type == OBJ_STRING) {\n                /* Emit a SET command */\n                char cmd[]=\"*3\\r\\n$3\\r\\nSET\\r\\n\";\n                if (rioWrite(aof,cmd,sizeof(cmd)-1) == 0) return false;\n                /* Key and value */\n                if (rioWriteBulkObject(aof,&key) == 0) return false;\n                if (rioWriteBulkObject(aof,o) == 0) return false;\n            } else if (o->type == OBJ_LIST) {\n                if (rewriteListObject(aof,&key,o) == 0) return false;\n            } else if (o->type == OBJ_SET) {\n                if (rewriteSetObject(aof,&key,o) == 0) return false;\n            } else if (o->type == OBJ_ZSET) {\n                if (rewriteSortedSetObject(aof,&key,o) == 0) return false;\n            } else if (o->type == OBJ_HASH) {\n                if (rewriteHashObject(aof,&key,o) == 0) return false;\n            } else if (o->type == OBJ_STREAM) {\n                if (rewriteStreamObject(aof,&key,o) == 0) return false;\n            } else if (o->type == OBJ_MODULE) {\n                if (rewriteModuleObject(aof,&key,o) == 0) return false;\n            } else {\n                serverPanic(\"Unknown object type\");\n            }\n            /* Save the expire time */\n            if (o->FExpires()) {\n                expireEntry *pexpire = &o->expire;\n                for (auto &subExpire : *pexpire) {\n                    if (subExpire.subkey() == nullptr)\n                    {\n                        char cmd[]=\"*3\\r\\n$9\\r\\nPEXPIREAT\\r\\n\";\n                        if (rioWrite(aof,cmd,sizeof(cmd)-1) == 0) return false;\n                        if (rioWriteBulkObject(aof,&key) == 0) return false;\n                    }\n                    else\n                    {\n                        char cmd[]=\"*4\\r\\n$12\\r\\nPEXPIREMEMBERAT\\r\\n\";\n                        if (rioWrite(aof,cmd,sizeof(cmd)-1) == 0) return false;\n                        if (rioWriteBulkObject(aof,&key) == 0) return false;\n                        if (rioWrite(aof,subExpire.subkey(),sdslen(subExpire.subkey())) == 0) return false;\n                    }\n                    if (rioWriteBulkLongLong(aof,subExpire.when()) == 0) return false; // common\n                }\n            }\n            \n            /* Read some diff from the parent process from time to time. */\n            if (aof->processed_bytes > processed+AOF_READ_DIFF_INTERVAL_BYTES) {\n                processed = aof->processed_bytes;\n                aofReadDiffFromParent();\n            }\n\n            /* Update info every 1 second (approximately).\n             * in order to avoid calling mstime() on each iteration, we will\n             * check the diff every 1024 keys */\n            if ((key_count++ & 1023) == 0) {\n                long long now = mstime();\n                if (now - updated_time >= 1000) {\n                    sendChildInfo(CHILD_INFO_TYPE_CURRENT_INFO, key_count, \"AOF rewrite\");\n                    updated_time = now;\n                }\n            }\n\n            return true;\n        });\n        if (!fComplete)\n            goto werr;\n    }\n    return C_OK;\n\nwerr:\n    return C_ERR;\n}\n\n/* Write a sequence of commands able to fully rebuild the dataset into\n * \"filename\". Used both by REWRITEAOF and BGREWRITEAOF.\n *\n * In order to minimize the number of commands needed in the rewritten\n * log Redis uses variadic commands when possible, such as RPUSH, SADD\n * and ZADD. However at max AOF_REWRITE_ITEMS_PER_CMD items per time\n * are inserted using a single command. */\nint rewriteAppendOnlyFile(char *filename) {\n    rio aof;\n    FILE *fp = NULL;\n    char tmpfile[256];\n    char byte;\n    int nodata = 0;\n    mstime_t start = 0;\n\n{ // BEGIN GOTO SCOPED VARIABLES\n    /* Note that we have to use a different temp name here compared to the\n     * one used by rewriteAppendOnlyFileBackground() function. */\n    snprintf(tmpfile,sizeof(tmpfile),\"temp-rewriteaof-%d.aof\", (int) getpid());\n    fp = fopen(tmpfile,\"w\");\n    if (!fp) {\n        serverLog(LL_WARNING, \"Opening the temp file for AOF rewrite in rewriteAppendOnlyFile(): %s\", strerror(errno));\n        return C_ERR;\n    }\n\n    g_pserver->aof_child_diff = sdsempty();\n    rioInitWithFile(&aof,fp);\n\n    if (g_pserver->aof_rewrite_incremental_fsync)\n        rioSetAutoSync(&aof,REDIS_AUTOSYNC_BYTES);\n\n    startSaving(RDBFLAGS_AOF_PREAMBLE);\n\n    if (g_pserver->aof_use_rdb_preamble) {\n        int error;\n        std::vector<const redisDbPersistentDataSnapshot*> vecpdb;\n        for (int idb = 0; idb < cserver.dbnum; ++idb)\n        {\n            vecpdb.push_back(g_pserver->db[idb]);\n        }\n        if (rdbSaveRio(&aof,vecpdb.data(),&error,RDBFLAGS_AOF_PREAMBLE,NULL) == C_ERR) {\n            errno = error;\n            goto werr;\n        }\n    } else {\n        if (rewriteAppendOnlyFileRio(&aof) == C_ERR) goto werr;\n    }\n\n    /* Do an initial slow fsync here while the parent is still sending\n     * data, in order to make the next final fsync faster. */\n    if (fflush(fp) == EOF) goto werr;\n    if (fsync(fileno(fp)) == -1) goto werr;\n\n    /* Read again a few times to get more data from the parent.\n     * We can't read forever (the server may receive data from clients\n     * faster than it is able to send data to the child), so we try to read\n     * some more data in a loop as soon as there is a good chance more data\n     * will come. If it looks like we are wasting time, we abort (this\n     * happens after 20 ms without new data). */\n    start = mstime();\n    while(mstime()-start < 1000 && nodata < 20) {\n        if (aeWait(g_pserver->aof_pipe_read_data_from_parent, AE_READABLE, 1) <= 0)\n        {\n            nodata++;\n            continue;\n        }\n        nodata = 0; /* Start counting from zero, we stop on N *contiguous*\n                       timeouts. */\n        aofReadDiffFromParent();\n    }\n\n    /* Ask the master to stop sending diffs. */\n    if (write(g_pserver->aof_pipe_write_ack_to_parent,\"!\",1) != 1) goto werr;\n    if (anetNonBlock(NULL,g_pserver->aof_pipe_read_ack_from_parent) != ANET_OK)\n        goto werr;\n    /* We read the ACK from the server using a 5 seconds timeout. Normally\n     * it should reply ASAP, but just in case we lose its reply, we are sure\n     * the child will eventually get terminated. */\n    if (syncRead(g_pserver->aof_pipe_read_ack_from_parent,&byte,1,5000) != 1 ||\n        byte != '!') goto werr;\n    serverLog(LL_NOTICE,\"Parent agreed to stop sending diffs. Finalizing AOF...\");\n\n    /* Read the final diff if any. */\n    aofReadDiffFromParent();\n\n    /* Write the received diff to the file. */\n    serverLog(LL_NOTICE,\n        \"Concatenating %.2f MB of AOF diff received from parent.\",\n        (double) sdslen(g_pserver->aof_child_diff) / (1024*1024));\n\n    /* Now we write the entire AOF buffer we received from the parent\n     * via the pipe during the life of this fork child.\n     * once a second, we'll take a break and send updated COW info to the parent */\n    size_t bytes_to_write = sdslen(g_pserver->aof_child_diff);\n    const char *buf = g_pserver->aof_child_diff;\n    long long cow_updated_time = mstime();\n    long long key_count = dbTotalServerKeyCount();\n    while (bytes_to_write) {\n        /* We write the AOF buffer in chunk of 8MB so that we can check the time in between them */\n        size_t chunk_size = bytes_to_write < (8<<20) ? bytes_to_write : (8<<20);\n\n        if (rioWrite(&aof,buf,chunk_size) == 0)\n            goto werr;\n\n        bytes_to_write -= chunk_size;\n        buf += chunk_size;\n\n        /* Update COW info */\n        long long now = mstime();\n        if (now - cow_updated_time >= 1000) {\n            sendChildInfo(CHILD_INFO_TYPE_CURRENT_INFO, key_count, \"AOF rewrite\");\n            cow_updated_time = now;\n        }\n    }\n\n    /* Make sure data will not remain on the OS's output buffers */\n    if (fflush(fp)) goto werr;\n    if (fsync(fileno(fp))) goto werr;\n    if (fclose(fp)) { fp = NULL; goto werr; }\n    fp = NULL;\n\n    /* Use RENAME to make sure the DB file is changed atomically only\n     * if the generate DB file is ok. */\n    if (rename(tmpfile,filename) == -1) {\n        serverLog(LL_WARNING,\"Error moving temp append only file on the final destination: %s\", strerror(errno));\n        unlink(tmpfile);\n        stopSaving(0);\n        return C_ERR;\n    }\n    serverLog(LL_NOTICE,\"SYNC append only file rewrite performed\");\n    stopSaving(1);\n    return C_OK;\n} // END GOTO SCOPED VARIABLES\n\nwerr:\n    serverLog(LL_WARNING,\"Write error writing append only file on disk: %s\", strerror(errno));\n    if (fp) fclose(fp);\n    unlink(tmpfile);\n    stopSaving(0);\n    return C_ERR;\n}\n\n/* ----------------------------------------------------------------------------\n * AOF rewrite pipes for IPC\n * -------------------------------------------------------------------------- */\n\n/* This event handler is called when the AOF rewriting child sends us a\n * single '!' char to signal we should stop sending buffer diffs. The\n * parent sends a '!' as well to acknowledge. */\nvoid aofChildPipeReadable(aeEventLoop *el, int fd, void *privdata, int mask) {\n    char byte;\n    UNUSED(el);\n    UNUSED(privdata);\n    UNUSED(mask);\n\n    if (read(fd,&byte,1) == 1 && byte == '!') {\n        serverLog(LL_NOTICE,\"AOF rewrite child asks to stop sending diffs.\");\n        g_pserver->aof_stop_sending_diff = 1;\n        if (write(g_pserver->aof_pipe_write_ack_to_child,\"!\",1) != 1) {\n            /* If we can't send the ack, inform the user, but don't try again\n             * since in the other side the children will use a timeout if the\n             * kernel can't buffer our write, or, the children was\n             * terminated. */\n            serverLog(LL_WARNING,\"Can't send ACK to AOF child: %s\",\n                strerror(errno));\n        }\n    }\n    /* Remove the handler since this can be called only one time during a\n     * rewrite. */\n    aeDeleteFileEvent(el,g_pserver->aof_pipe_read_ack_from_child,AE_READABLE);\n}\n\n/* Create the pipes used for parent - child process IPC during rewrite.\n * We have a data pipe used to send AOF incremental diffs to the child,\n * and two other pipes used by the children to signal it finished with\n * the rewrite so no more data should be written, and another for the\n * parent to acknowledge it understood this new condition. */\nint aofCreatePipes(void) {\n    int fds[6] = {-1, -1, -1, -1, -1, -1};\n    int j;\n\n    if (pipe(fds) == -1) goto error; /* parent -> children data. */\n    if (pipe(fds+2) == -1) goto error; /* children -> parent ack. */\n    if (pipe(fds+4) == -1) goto error; /* parent -> children ack. */\n    /* Parent -> children data is non blocking. */\n    if (anetNonBlock(NULL,fds[0]) != ANET_OK) goto error;\n    if (anetNonBlock(NULL,fds[1]) != ANET_OK) goto error;\n    if (aeCreateFileEvent(serverTL->el, fds[2], AE_READABLE, aofChildPipeReadable, NULL) == AE_ERR) goto error;\n\n    g_pserver->aof_pipe_write_data_to_child = fds[1];\n    g_pserver->aof_pipe_read_data_from_parent = fds[0];\n    g_pserver->aof_pipe_write_ack_to_parent = fds[3];\n    g_pserver->aof_pipe_read_ack_from_child = fds[2];\n    g_pserver->el_alf_pip_read_ack_from_child = serverTL->el;\n    g_pserver->aof_pipe_write_ack_to_child = fds[5];\n    g_pserver->aof_pipe_read_ack_from_parent = fds[4];\n    g_pserver->aof_stop_sending_diff = 0;\n    return C_OK;\n\nerror:\n    serverLog(LL_WARNING,\"Error opening /setting AOF rewrite IPC pipes: %s\",\n        strerror(errno));\n    for (j = 0; j < 6; j++) if(fds[j] != -1) close(fds[j]);\n    return C_ERR;\n}\n\nvoid aofClosePipes(void) {\n    int fdAofAckPipe = g_pserver->aof_pipe_read_ack_from_child;\n    int res = aePostFunction(g_pserver->el_alf_pip_read_ack_from_child, [fdAofAckPipe]{\n        aeDeleteFileEventAsync(serverTL->el,fdAofAckPipe,AE_READABLE);\n        close (fdAofAckPipe);\n    });\n    serverAssert(res == AE_OK);\n\n    int fdAofWritePipe = g_pserver->aof_pipe_write_data_to_child;\n    res = aePostFunction(g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].el, [fdAofWritePipe]{\n        aeDeleteFileEventAsync(serverTL->el,fdAofWritePipe,AE_WRITABLE);\n        close(fdAofWritePipe);\n    });\n    serverAssert(res == AE_OK);\n    g_pserver->aof_pipe_write_data_to_child = -1;\n    \n    close(g_pserver->aof_pipe_read_data_from_parent);\n    close(g_pserver->aof_pipe_write_ack_to_parent);\n    close(g_pserver->aof_pipe_write_ack_to_child);\n    close(g_pserver->aof_pipe_read_ack_from_parent);\n}\n\n/* ----------------------------------------------------------------------------\n * AOF background rewrite\n * ------------------------------------------------------------------------- */\n\n/* This is how rewriting of the append only file in background works:\n *\n * 1) The user calls BGREWRITEAOF\n * 2) Redis calls this function, that forks():\n *    2a) the child rewrite the append only file in a temp file.\n *    2b) the parent accumulates differences in g_pserver->aof_rewrite_buf.\n * 3) When the child finished '2a' exists.\n * 4) The parent will trap the exit code, if it's OK, will append the\n *    data accumulated into g_pserver->aof_rewrite_buf into the temp file, and\n *    finally will rename(2) the temp file in the actual file name.\n *    The the new file is reopened as the new append only file. Profit!\n */\nint rewriteAppendOnlyFileBackground(void) {\n    pid_t childpid;\n\n    if (hasActiveChildProcessOrBGSave()) return C_ERR;\n    if (aofCreatePipes() != C_OK) return C_ERR;\n    if ((childpid = redisFork(CHILD_TYPE_AOF)) == 0) {\n        char tmpfile[256];\n\n        /* Child */\n        redisSetProcTitle(\"keydb-aof-rewrite\");\n        redisSetCpuAffinity(g_pserver->aof_rewrite_cpulist);\n        snprintf(tmpfile,sizeof(tmpfile),\"temp-rewriteaof-bg-%d.aof\", (int) getpid());\n        if (rewriteAppendOnlyFile(tmpfile) == C_OK) {\n            sendChildCowInfo(CHILD_INFO_TYPE_AOF_COW_SIZE, \"AOF rewrite\");\n            exitFromChild(0);\n        } else {\n            exitFromChild(1);\n        }\n    } else {\n        /* Parent */\n        if (childpid == -1) {\n            serverLog(LL_WARNING,\n                \"Can't rewrite append only file in background: fork: %s\",\n                strerror(errno));\n            aofClosePipes();\n            return C_ERR;\n        }\n        serverLog(LL_NOTICE,\n            \"Background append only file rewriting started by pid %ld\",(long)childpid);\n        g_pserver->aof_rewrite_scheduled = 0;\n        g_pserver->aof_rewrite_time_start = time(NULL);\n        updateDictResizePolicy();\n        /* We set appendseldb to -1 in order to force the next call to the\n         * feedAppendOnlyFile() to issue a SELECT command, so the differences\n         * accumulated by the parent into g_pserver->aof_rewrite_buf will start\n         * with a SELECT statement and it will be safe to merge. */\n        g_pserver->aof_selected_db = -1;\n        replicationScriptCacheFlush();\n        return C_OK;\n    }\n    return C_OK; /* unreached */\n}\n\nvoid bgrewriteaofCommand(client *c) {\n    if (g_pserver->child_type == CHILD_TYPE_AOF) {\n        addReplyError(c,\"Background append only file rewriting already in progress\");\n    } else if (hasActiveChildProcessOrBGSave()) {\n        g_pserver->aof_rewrite_scheduled = 1;\n        addReplyStatus(c,\"Background append only file rewriting scheduled\");\n    } else if (rewriteAppendOnlyFileBackground() == C_OK) {\n        addReplyStatus(c,\"Background append only file rewriting started\");\n    } else {\n        addReplyError(c,\"Can't execute an AOF background rewriting. \"\n                        \"Please check the server logs for more information.\");\n    }\n}\n\nvoid aofRemoveTempFile(pid_t childpid) {\n    char tmpfile[256];\n\n    snprintf(tmpfile,sizeof(tmpfile),\"temp-rewriteaof-bg-%d.aof\", (int) childpid);\n    bg_unlink(tmpfile);\n\n    snprintf(tmpfile,sizeof(tmpfile),\"temp-rewriteaof-%d.aof\", (int) childpid);\n    bg_unlink(tmpfile);\n}\n\n/* Update the g_pserver->aof_current_size field explicitly using stat(2)\n * to check the size of the file. This is useful after a rewrite or after\n * a restart, normally the size is updated just adding the write length\n * to the current length, that is much faster. */\nvoid aofUpdateCurrentSize(void) {\n    struct redis_stat sb;\n    mstime_t latency;\n\n    latencyStartMonitor(latency);\n    if (redis_fstat(g_pserver->aof_fd,&sb) == -1) {\n        serverLog(LL_WARNING,\"Unable to obtain the AOF file length. stat: %s\",\n            strerror(errno));\n    } else {\n        g_pserver->aof_current_size = sb.st_size;\n    }\n    latencyEndMonitor(latency);\n    latencyAddSampleIfNeeded(\"aof-fstat\",latency);\n}\n\n/* A background append only file rewriting (BGREWRITEAOF) terminated its work.\n * Handle this. */\nvoid backgroundRewriteDoneHandler(int exitcode, int bysignal) {\n    if (!bysignal && exitcode == 0) {\n        int newfd, oldfd;\n        char tmpfile[256];\n        long long now = ustime();\n        mstime_t latency;\n\n        serverLog(LL_NOTICE,\n            \"Background AOF rewrite terminated with success\");\n\n        /* Flush the differences accumulated by the parent to the\n         * rewritten AOF. */\n        latencyStartMonitor(latency);\n        snprintf(tmpfile,sizeof(tmpfile),\"temp-rewriteaof-bg-%d.aof\",\n            (int)g_pserver->child_pid);\n        newfd = open(tmpfile,O_WRONLY|O_APPEND);\n        if (newfd == -1) {\n            serverLog(LL_WARNING,\n                \"Unable to open the temporary AOF produced by the child: %s\", strerror(errno));\n            goto cleanup;\n        }\n\n        if (aofRewriteBufferWrite(newfd) == -1) {\n            serverLog(LL_WARNING,\n                \"Error trying to flush the parent diff to the rewritten AOF: %s\", strerror(errno));\n            close(newfd);\n            goto cleanup;\n        }\n        latencyEndMonitor(latency);\n        latencyAddSampleIfNeeded(\"aof-rewrite-diff-write\",latency);\n  \n        if (g_pserver->aof_fsync == AOF_FSYNC_EVERYSEC) {\n            aof_background_fsync(newfd);\n        } else if (g_pserver->aof_fsync == AOF_FSYNC_ALWAYS) {\n            latencyStartMonitor(latency);\n            if (redis_fsync(newfd) == -1) {\n                serverLog(LL_WARNING,\n                    \"Error trying to fsync the parent diff to the rewritten AOF: %s\", strerror(errno));\n                close(newfd);\n                goto cleanup;\n            }\n            latencyEndMonitor(latency);\n            latencyAddSampleIfNeeded(\"aof-rewrite-done-fsync\",latency);\n        }\n\n        serverLog(LL_NOTICE,\n            \"Residual parent diff successfully flushed to the rewritten AOF (%.2f MB)\", (double) aofRewriteBufferSize() / (1024*1024));\n\n        /* The only remaining thing to do is to rename the temporary file to\n         * the configured file and switch the file descriptor used to do AOF\n         * writes. We don't want close(2) or rename(2) calls to block the\n         * server on old file deletion.\n         *\n         * There are two possible scenarios:\n         *\n         * 1) AOF is DISABLED and this was a one time rewrite. The temporary\n         * file will be renamed to the configured file. When this file already\n         * exists, it will be unlinked, which may block the g_pserver->\n         *\n         * 2) AOF is ENABLED and the rewritten AOF will immediately start\n         * receiving writes. After the temporary file is renamed to the\n         * configured file, the original AOF file descriptor will be closed.\n         * Since this will be the last reference to that file, closing it\n         * causes the underlying file to be unlinked, which may block the\n         * g_pserver->\n         *\n         * To mitigate the blocking effect of the unlink operation (either\n         * caused by rename(2) in scenario 1, or by close(2) in scenario 2), we\n         * use a background thread to take care of this. First, we\n         * make scenario 1 identical to scenario 2 by opening the target file\n         * when it exists. The unlink operation after the rename(2) will then\n         * be executed upon calling close(2) for its descriptor. Everything to\n         * guarantee atomicity for this switch has already happened by then, so\n         * we don't care what the outcome or duration of that close operation\n         * is, as long as the file descriptor is released again. */\n        if (g_pserver->aof_fd == -1) {\n            /* AOF disabled */\n\n            /* Don't care if this fails: oldfd will be -1 and we handle that.\n             * One notable case of -1 return is if the old file does\n             * not exist. */\n            oldfd = open(g_pserver->aof_filename,O_RDONLY|O_NONBLOCK);\n        } else {\n            /* AOF enabled */\n            oldfd = -1; /* We'll set this to the current AOF filedes later. */\n        }\n\n        /* Rename the temporary file. This will not unlink the target file if\n         * it exists, because we reference it with \"oldfd\". */\n        latencyStartMonitor(latency);\n        if (rename(tmpfile,g_pserver->aof_filename) == -1) {\n            serverLog(LL_WARNING,\n                \"Error trying to rename the temporary AOF file %s into %s: %s\",\n                tmpfile,\n                g_pserver->aof_filename,\n                strerror(errno));\n            close(newfd);\n            if (oldfd != -1) close(oldfd);\n            goto cleanup;\n        }\n        latencyEndMonitor(latency);\n        latencyAddSampleIfNeeded(\"aof-rename\",latency);\n\n        if (g_pserver->aof_fd == -1) {\n            /* AOF disabled, we don't need to set the AOF file descriptor\n             * to this new file, so we can close it. */\n            close(newfd);\n        } else {\n            /* AOF enabled, replace the old fd with the new one. */\n            oldfd = g_pserver->aof_fd;\n            g_pserver->aof_fd = newfd;\n            g_pserver->aof_selected_db = -1; /* Make sure SELECT is re-issued */\n            aofUpdateCurrentSize();\n            g_pserver->aof_rewrite_base_size = g_pserver->aof_current_size;\n            g_pserver->aof_fsync_offset = g_pserver->aof_current_size;\n            g_pserver->aof_last_fsync = g_pserver->unixtime;\n\n            /* Clear regular AOF buffer since its contents was just written to\n             * the new AOF from the background rewrite buffer. */\n            sdsfree(g_pserver->aof_buf);\n            g_pserver->aof_buf = sdsempty();\n        }\n\n        g_pserver->aof_lastbgrewrite_status = C_OK;\n\n        serverLog(LL_NOTICE, \"Background AOF rewrite finished successfully\");\n        /* Change state from WAIT_REWRITE to ON if needed */\n        if (g_pserver->aof_state == AOF_WAIT_REWRITE)\n            g_pserver->aof_state = AOF_ON;\n\n        /* Asynchronously close the overwritten AOF. */\n        if (oldfd != -1) bioCreateCloseJob(oldfd);\n\n        serverLog(LL_VERBOSE,\n            \"Background AOF rewrite signal handler took %lldus\", ustime()-now);\n    } else if (!bysignal && exitcode != 0) {\n        g_pserver->aof_lastbgrewrite_status = C_ERR;\n\n        serverLog(LL_WARNING,\n            \"Background AOF rewrite terminated with error\");\n    } else {\n        /* SIGUSR1 is whitelisted, so we have a way to kill a child without\n         * triggering an error condition. */\n        if (bysignal != SIGUSR1)\n            g_pserver->aof_lastbgrewrite_status = C_ERR;\n\n        serverLog(LL_WARNING,\n            \"Background AOF rewrite terminated by signal %d\", bysignal);\n    }\n\ncleanup:\n    aofClosePipes();\n    aofRewriteBufferReset();\n    aofRemoveTempFile(g_pserver->child_pid);\n    g_pserver->aof_rewrite_time_last = time(NULL)-g_pserver->aof_rewrite_time_start;\n    g_pserver->aof_rewrite_time_start = -1;\n    /* Schedule a new rewrite if we are waiting for it to switch the AOF ON. */\n    if (g_pserver->aof_state == AOF_WAIT_REWRITE)\n        g_pserver->aof_rewrite_scheduled = 1;\n}\n"
  },
  {
    "path": "src/asciilogo.h",
    "content": "/*\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\nconst char *ascii_logo =\n\"                                                                      \\n\"\n\"                  _                                                   \\n\"\n\"               _-(+)-_                                                \\n\"\n\"            _-- /   \\\\ --_                                            \\n\"\n\"         _--   /     \\\\   --_            KeyDB  %s (%s/%d) %s bit     \\n\"\n\"     __--     /       \\\\     --__                                     \\n\"\n\"    (+) _    /         \\\\    _ (+)       Running in %s mode\\n\"\n\"     |   -- /           \\\\ --   |        Port: %d\\n\"\n\"     |     /--_   _   _--\\\\     |        PID: %ld\\n\"\n\"     |    /     -(+)-     \\\\    |                                     \\n\"\n\"     |   /        |        \\\\   |        https://docs.keydb.dev       \\n\"\n\"     |  /         |         \\\\  |                                     \\n\"\n\"     | /          |          \\\\ |                                     \\n\"\n\"    (+)_ -- -- -- | -- -- -- _(+)                                     \\n\"\n\"        --_       |       _--                                         \\n\"\n\"            --_   |   _--                                             \\n\"\n\"                -(+)-        %s\\n\"\n\"                                                                     \\n\";\n"
  },
  {
    "path": "src/atomicvar.h",
    "content": "/* This file implements atomic counters using c11 _Atomic, __atomic or __sync\n * macros if available, otherwise we will throw an error when compile.\n *\n * The exported interface is composed of three macros:\n *\n * atomicIncr(var,count) -- Increment the atomic counter\n * atomicGetIncr(var,oldvalue_var,count) -- Get and increment the atomic counter\n * atomicDecr(var,count) -- Decrement the atomic counter\n * atomicGet(var,dstvar) -- Fetch the atomic counter value\n * atomicSet(var,value)  -- Set the atomic counter value\n * atomicGetWithSync(var,value)  -- 'atomicGet' with inter-thread synchronization\n * atomicSetWithSync(var,value)  -- 'atomicSet' with inter-thread synchronization\n *\n * Never use return value from the macros, instead use the AtomicGetIncr()\n * if you need to get the current value and increment it atomically, like\n * in the following example:\n *\n *  long oldvalue;\n *  atomicGetIncr(myvar,oldvalue,1);\n *  doSomethingWith(oldvalue);\n *\n * ----------------------------------------------------------------------------\n *\n * Copyright (c) 2015, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <pthread.h>\n#include \"config.h\"\n\n#ifndef __ATOMIC_VAR_H\n#define __ATOMIC_VAR_H\n\n/* Define redisAtomic for atomic variable. */\n#define redisAtomic\n\n/* To test Redis with Helgrind (a Valgrind tool) it is useful to define\n * the following macro, so that __sync macros are used: those can be detected\n * by Helgrind (even if they are less efficient) so that no false positive\n * is reported. */\n// #define __ATOMIC_VAR_FORCE_SYNC_MACROS\n\n/* There will be many false positives if we test Redis with Helgrind, since\n * Helgrind can't understand we have imposed ordering on the program, so\n * we use macros in helgrind.h to tell Helgrind inter-thread happens-before\n * relationship explicitly for avoiding false positives.\n *\n * For more details, please see: valgrind/helgrind.h and\n * https://www.valgrind.org/docs/manual/hg-manual.html#hg-manual.effective-use\n *\n * These macros take effect only when 'make helgrind', and you must first\n * install Valgrind in the default path configuration. */\n#ifdef __ATOMIC_VAR_FORCE_SYNC_MACROS\n#include <valgrind/helgrind.h>\n#else\n#define ANNOTATE_HAPPENS_BEFORE(v) ((void) v)\n#define ANNOTATE_HAPPENS_AFTER(v)  ((void) v)\n#endif\n\n#if !defined(__ATOMIC_VAR_FORCE_SYNC_MACROS) && defined(__STDC_VERSION__) && \\\n    (__STDC_VERSION__ >= 201112L) && !defined(__STDC_NO_ATOMICS__)\n/* Use '_Atomic' keyword if the compiler supports. */\n#undef  redisAtomic\n#define redisAtomic _Atomic\n/* Implementation using _Atomic in C11. */\n\n#include <stdatomic.h>\n#define atomicIncr(var,count) atomic_fetch_add_explicit(&var,(count),memory_order_relaxed)\n#define atomicGetIncr(var,oldvalue_var,count) do { \\\n    oldvalue_var = atomic_fetch_add_explicit(&var,(count),memory_order_relaxed); \\\n} while(0)\n#define atomicDecr(var,count) atomic_fetch_sub_explicit(&var,(count),memory_order_relaxed)\n#define atomicGet(var,dstvar) do { \\\n    dstvar = atomic_load_explicit(&var,memory_order_relaxed); \\\n} while(0)\n#define atomicSet(var,value) atomic_store_explicit(&var,value,memory_order_relaxed)\n#define atomicGetWithSync(var,dstvar) do { \\\n    dstvar = atomic_load_explicit(&var,memory_order_seq_cst); \\\n} while(0)\n#define atomicSetWithSync(var,value) \\\n    atomic_store_explicit(&var,value,memory_order_seq_cst)\n#define REDIS_ATOMIC_API \"c11-builtin\"\n\n#elif !defined(__ATOMIC_VAR_FORCE_SYNC_MACROS) && \\\n    (!defined(__clang__) || !defined(__APPLE__) || __apple_build_version__ > 4210057) && \\\n    defined(__ATOMIC_RELAXED) && defined(__ATOMIC_SEQ_CST)\n/* Implementation using __atomic macros. */\n\n#define atomicIncr(var,count) __atomic_add_fetch(&var,(count),__ATOMIC_RELAXED)\n#define atomicGetIncr(var,oldvalue_var,count) do { \\\n    oldvalue_var = __atomic_fetch_add(&var,(count),__ATOMIC_RELAXED); \\\n} while(0)\n#define atomicDecr(var,count) __atomic_sub_fetch(&var,(count),__ATOMIC_RELAXED)\n#define atomicGet(var,dstvar) do { \\\n    dstvar = __atomic_load_n(&var,__ATOMIC_RELAXED); \\\n} while(0)\n#define atomicSet(var,value) __atomic_store_n(&var,value,__ATOMIC_RELAXED)\n#define atomicGetWithSync(var,dstvar) do { \\\n    dstvar = __atomic_load_n(&var,__ATOMIC_SEQ_CST); \\\n} while(0)\n#define atomicSetWithSync(var,value) \\\n    __atomic_store_n(&var,value,__ATOMIC_SEQ_CST)\n#define REDIS_ATOMIC_API \"atomic-builtin\"\n\n#elif defined(HAVE_ATOMIC)\n/* Implementation using __sync macros. */\n\n#define atomicIncr(var,count) __sync_add_and_fetch(&var,(count))\n#define atomicGetIncr(var,oldvalue_var,count) do { \\\n    oldvalue_var = __sync_fetch_and_add(&var,(count)); \\\n} while(0)\n#define atomicDecr(var,count) __sync_sub_and_fetch(&var,(count))\n#define atomicGet(var,dstvar) do { \\\n    dstvar = __sync_sub_and_fetch(&var,0); \\\n} while(0)\n#define atomicSet(var,value) do { \\\n    while(!__sync_bool_compare_and_swap(&var,var,value)); \\\n} while(0)\n/* Actually the builtin issues a full memory barrier by default. */\n#define atomicGetWithSync(var,dstvar) { \\\n    dstvar = __sync_sub_and_fetch(&var,0,__sync_synchronize); \\\n    ANNOTATE_HAPPENS_AFTER(&var); \\\n} while(0)\n#define atomicSetWithSync(var,value) do { \\\n    ANNOTATE_HAPPENS_BEFORE(&var);  \\\n    while(!__sync_bool_compare_and_swap(&var,var,value,__sync_synchronize)); \\\n} while(0)\n#define REDIS_ATOMIC_API \"sync-builtin\"\n\n#else\n#error \"Unable to determine atomic operations for your platform\"\n\n#endif\n#endif /* __ATOMIC_VAR_H */\n"
  },
  {
    "path": "src/bio.cpp",
    "content": "/* Background I/O service for Redis.\n *\n * This file implements operations that we need to perform in the background.\n * Currently there is only a single operation, that is a background close(2)\n * system call. This is needed as when the process is the last owner of a\n * reference to a file closing it means unlinking it, and the deletion of the\n * file is slow, blocking the g_pserver->\n *\n * In the future we'll either continue implementing new things we need or\n * we'll switch to libeio. However there are probably long term uses for this\n * file as we may want to put here Redis specific background tasks (for instance\n * it is not impossible that we'll need a non blocking FLUSHDB/FLUSHALL\n * implementation).\n *\n * DESIGN\n * ------\n *\n * The design is trivial, we have a structure representing a job to perform\n * and a different thread and job queue for every job type.\n * Every thread waits for new jobs in its queue, and process every job\n * sequentially.\n *\n * Jobs of the same type are guaranteed to be processed from the least\n * recently inserted to the most recently inserted (older jobs processed\n * first).\n *\n * Currently there is no way for the creator of the job to be notified about\n * the completion of the operation, this will only be added when/if needed.\n *\n * ----------------------------------------------------------------------------\n *\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n\n#include \"server.h\"\n#include \"bio.h\"\n\nstatic pthread_t bio_threads[BIO_NUM_OPS];\nstatic pthread_mutex_t bio_mutex[BIO_NUM_OPS];\nstatic pthread_cond_t bio_newjob_cond[BIO_NUM_OPS];\nstatic pthread_cond_t bio_step_cond[BIO_NUM_OPS];\nstatic list *bio_jobs[BIO_NUM_OPS];\n/* The following array is used to hold the number of pending jobs for every\n * OP type. This allows us to export the bioPendingJobsOfType() API that is\n * useful when the main thread wants to perform some operation that may involve\n * objects shared with the background thread. The main thread will just wait\n * that there are no longer jobs of this type to be executed before performing\n * the sensible operation. This data is also useful for reporting. */\nstatic unsigned long long bio_pending[BIO_NUM_OPS];\n\n/* This structure represents a background Job. It is only used locally to this\n * file as the API does not expose the internals at all. */\nstruct bio_job {\n    time_t time; /* Time at which the job was created. */\n    /* Job specific arguments.*/\n    int fd; /* Fd for file based background jobs */\n    lazy_free_fn *free_fn; /* Function that will free the provided arguments */\n    void ** free_args() { return reinterpret_cast<void**>(this+1); } /* List of arguments to be passed to the free function */\n    void set_free_arg(int i, void *arg) { reinterpret_cast<void**>(this+1)[i] = arg; }\n};\n\nvoid *bioProcessBackgroundJobs(void *arg);\n\n/* Make sure we have enough stack to perform all the things we do in the\n * main thread. */\n#define REDIS_THREAD_STACK_SIZE (1024*1024*4)\n\n/* Initialize the background system, spawning the thread. */\nvoid bioInit(void) {\n    pthread_attr_t attr;\n    pthread_t thread;\n    size_t stacksize;\n    int j;\n\n    /* Initialization of state vars and objects */\n    for (j = 0; j < BIO_NUM_OPS; j++) {\n        serverAssert(pthread_mutex_init(&bio_mutex[j],NULL) == 0);\n        serverAssert(pthread_cond_init(&bio_newjob_cond[j],NULL) == 0);\n        serverAssert(pthread_cond_init(&bio_step_cond[j],NULL) == 0);\n        bio_jobs[j] = listCreate();\n        bio_pending[j] = 0;\n    }\n\n    /* Set the stack size as by default it may be small in some system */\n    pthread_attr_init(&attr);\n    pthread_attr_getstacksize(&attr,&stacksize);\n    if (!stacksize) stacksize = 1; /* The world is full of Solaris Fixes */\n    while (stacksize < REDIS_THREAD_STACK_SIZE) stacksize *= 2;\n    pthread_attr_setstacksize(&attr, stacksize);\n\n    /* Ready to spawn our threads. We use the single argument the thread\n     * function accepts in order to pass the job ID the thread is\n     * responsible of. */\n    for (j = 0; j < BIO_NUM_OPS; j++) {\n        void *arg = (void*)(unsigned long) j;\n        if (pthread_create(&thread,&attr,bioProcessBackgroundJobs,arg) != 0) {\n            serverLog(LL_WARNING,\"Fatal: Can't initialize Background Jobs.\");\n            exit(1);\n        }\n        bio_threads[j] = thread;\n    }\n}\n\nvoid bioSubmitJob(int type, struct bio_job *job) {\n    job->time = time(NULL);\n    pthread_mutex_lock(&bio_mutex[type]);\n    listAddNodeTail(bio_jobs[type],job);\n    bio_pending[type]++;\n    pthread_cond_signal(&bio_newjob_cond[type]);\n    pthread_mutex_unlock(&bio_mutex[type]);\n}\n\nvoid bioCreateLazyFreeJob(lazy_free_fn free_fn, int arg_count, ...) {\n    va_list valist;\n    /* Allocate memory for the job structure and all required\n     * arguments */\n    struct bio_job *job = (bio_job*)zmalloc(sizeof(*job) + sizeof(void *) * (arg_count));\n    job->free_fn = free_fn;\n\n    va_start(valist, arg_count);\n    for (int i = 0; i < arg_count; i++) {\n        job->set_free_arg(i, va_arg(valist, void *));\n    }\n    va_end(valist);\n    bioSubmitJob(BIO_LAZY_FREE, job);\n}\n\nvoid bioCreateCloseJob(int fd) {\n    struct bio_job *job = (bio_job*)zmalloc(sizeof(*job));\n    job->fd = fd;\n\n    bioSubmitJob(BIO_CLOSE_FILE, job);\n}\n\nvoid bioCreateFsyncJob(int fd) {\n    struct bio_job *job = (bio_job*)zmalloc(sizeof(*job));\n    job->fd = fd;\n\n    bioSubmitJob(BIO_AOF_FSYNC, job);\n}\n\nvoid *bioProcessBackgroundJobs(void *arg) {\n    struct bio_job *job;\n    unsigned long type = (unsigned long) arg;\n    sigset_t sigset;\n\n    /* Check that the type is within the right interval. */\n    if (type >= BIO_NUM_OPS) {\n        serverLog(LL_WARNING,\n            \"Warning: bio thread started with wrong type %lu\",type);\n        return NULL;\n    }\n\n    switch (type) {\n    case BIO_CLOSE_FILE:\n        redis_set_thread_title(\"bio_close_file\");\n        break;\n    case BIO_AOF_FSYNC:\n        redis_set_thread_title(\"bio_aof_fsync\");\n        break;\n    case BIO_LAZY_FREE:\n        redis_set_thread_title(\"bio_lazy_free\");\n        break;\n    }\n\n    redisSetCpuAffinity(g_pserver->bio_cpulist);\n\n    makeThreadKillable();\n\n    pthread_mutex_lock(&bio_mutex[type]);\n    /* Block SIGALRM so we are sure that only the main thread will\n     * receive the watchdog signal. */\n    sigemptyset(&sigset);\n    sigaddset(&sigset, SIGALRM);\n    if (pthread_sigmask(SIG_BLOCK, &sigset, NULL))\n        serverLog(LL_WARNING,\n            \"Warning: can't mask SIGALRM in bio.c thread: %s\", strerror(errno));\n\n    while(1) {\n        listNode *ln;\n\n        /* The loop always starts with the lock hold. */\n        if (listLength(bio_jobs[type]) == 0) {\n            pthread_cond_wait(&bio_newjob_cond[type],&bio_mutex[type]);\n            continue;\n        }\n        /* Pop the job from the queue. */\n        ln = listFirst(bio_jobs[type]);\n        job = (bio_job*)ln->value;\n        /* It is now possible to unlock the background system as we know have\n         * a stand alone job structure to process.*/\n        pthread_mutex_unlock(&bio_mutex[type]);\n\n        /* Process the job accordingly to its type. */\n        if (type == BIO_CLOSE_FILE) {\n            close(job->fd);\n        } else if (type == BIO_AOF_FSYNC) {\n            /* The fd may be closed by main thread and reused for another\n             * socket, pipe, or file. We just ignore these errno because\n             * aof fsync did not really fail. */\n            if (redis_fsync(job->fd) == -1 &&\n                errno != EBADF && errno != EINVAL)\n            {\n                int last_status;\n                atomicGet(g_pserver->aof_bio_fsync_status,last_status);\n                atomicSet(g_pserver->aof_bio_fsync_status,C_ERR);\n                atomicSet(g_pserver->aof_bio_fsync_errno,errno);\n                if (last_status == C_OK) {\n                    serverLog(LL_WARNING,\n                        \"Fail to fsync the AOF file: %s\",strerror(errno));\n                }\n            } else {\n                atomicSet(g_pserver->aof_bio_fsync_status,C_OK);\n            }\n        } else if (type == BIO_LAZY_FREE) {\n            job->free_fn(job->free_args());\n        } else {\n            serverPanic(\"Wrong job type in bioProcessBackgroundJobs().\");\n        }\n        zfree(job);\n\n        /* Lock again before reiterating the loop, if there are no longer\n         * jobs to process we'll block again in pthread_cond_wait(). */\n        pthread_mutex_lock(&bio_mutex[type]);\n        listDelNode(bio_jobs[type],ln);\n        bio_pending[type]--;\n\n        /* Unblock threads blocked on bioWaitStepOfType() if any. */\n        pthread_cond_broadcast(&bio_step_cond[type]);\n    }\n}\n\n/* Return the number of pending jobs of the specified type. */\nunsigned long long bioPendingJobsOfType(int type) {\n    unsigned long long val;\n    pthread_mutex_lock(&bio_mutex[type]);\n    val = bio_pending[type];\n    pthread_mutex_unlock(&bio_mutex[type]);\n    return val;\n}\n\n/* If there are pending jobs for the specified type, the function blocks\n * and waits that the next job was processed. Otherwise the function\n * does not block and returns ASAP.\n *\n * The function returns the number of jobs still to process of the\n * requested type.\n *\n * This function is useful when from another thread, we want to wait\n * a bio.c thread to do more work in a blocking way.\n */\nunsigned long long bioWaitStepOfType(int type) {\n    unsigned long long val;\n    pthread_mutex_lock(&bio_mutex[type]);\n    val = bio_pending[type];\n    if (val != 0) {\n        pthread_cond_wait(&bio_step_cond[type],&bio_mutex[type]);\n        val = bio_pending[type];\n    }\n    pthread_mutex_unlock(&bio_mutex[type]);\n    return val;\n}\n\n/* Kill the running bio threads in an unclean way. This function should be\n * used only when it's critical to stop the threads for some reason.\n * Currently Redis does this only on crash (for instance on SIGSEGV) in order\n * to perform a fast memory check without other threads messing with memory. */\nvoid bioKillThreads(void) {\n    int err, j;\n\n    for (j = 0; j < BIO_NUM_OPS; j++) {\n        if (bio_threads[j] == pthread_self()) continue;\n        if (bio_threads[j] && pthread_cancel(bio_threads[j]) == 0) {\n            if ((err = pthread_join(bio_threads[j],NULL)) != 0) {\n                serverLog(LL_WARNING,\n                    \"Bio thread for job type #%d can not be joined: %s\",\n                        j, strerror(err));\n            } else {\n                serverLog(LL_WARNING,\n                    \"Bio thread for job type #%d terminated\",j);\n            }\n        }\n    }\n}\n"
  },
  {
    "path": "src/bio.h",
    "content": "#pragma once\n/*\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\ntypedef void lazy_free_fn(void *args[]);\n\n/* Exported API */\nvoid bioInit(void);\nunsigned long long bioPendingJobsOfType(int type);\nunsigned long long bioWaitStepOfType(int type);\ntime_t bioOlderJobOfType(int type);\nvoid bioKillThreads(void);\nvoid bioCreateCloseJob(int fd);\nvoid bioCreateFsyncJob(int fd);\nvoid bioCreateLazyFreeJob(lazy_free_fn free_fn, int arg_count, ...);\n\n/* Background job opcodes */\n#define BIO_CLOSE_FILE    0 /* Deferred close(2) syscall. */\n#define BIO_AOF_FSYNC     1 /* Deferred AOF fsync. */\n#define BIO_LAZY_FREE     2 /* Deferred objects freeing. */\n#define BIO_NUM_OPS       3\n\n#ifdef __cplusplus\n}\n#endif\n"
  },
  {
    "path": "src/bitops.cpp",
    "content": "/* Bit operations.\n *\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n\n/* -----------------------------------------------------------------------------\n * Helpers and low level bit functions.\n * -------------------------------------------------------------------------- */\n\n/* Count number of bits set in the binary array pointed by 's' and long\n * 'count' bytes. The implementation of this function is required to\n * work with an input string length up to 512 MB or more (server.proto_max_bulk_len) */\nlong long redisPopcount(const void *s, long count) {\n    long long bits = 0;\n    unsigned char *p = (unsigned char*)s;\n    uint32_t *p4;\n    static const unsigned char bitsinbyte[256] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8};\n\n    /* Count initial bytes not aligned to 32 bit. */\n    while((unsigned long)p & 3 && count) {\n        bits += bitsinbyte[*p++];\n        count--;\n    }\n\n    /* Count bits 28 bytes at a time */\n    p4 = (uint32_t*)p;\n    while(count>=28) {\n        uint32_t aux1, aux2, aux3, aux4, aux5, aux6, aux7;\n\n        aux1 = *p4++;\n        aux2 = *p4++;\n        aux3 = *p4++;\n        aux4 = *p4++;\n        aux5 = *p4++;\n        aux6 = *p4++;\n        aux7 = *p4++;\n        count -= 28;\n\n        aux1 = aux1 - ((aux1 >> 1) & 0x55555555);\n        aux1 = (aux1 & 0x33333333) + ((aux1 >> 2) & 0x33333333);\n        aux2 = aux2 - ((aux2 >> 1) & 0x55555555);\n        aux2 = (aux2 & 0x33333333) + ((aux2 >> 2) & 0x33333333);\n        aux3 = aux3 - ((aux3 >> 1) & 0x55555555);\n        aux3 = (aux3 & 0x33333333) + ((aux3 >> 2) & 0x33333333);\n        aux4 = aux4 - ((aux4 >> 1) & 0x55555555);\n        aux4 = (aux4 & 0x33333333) + ((aux4 >> 2) & 0x33333333);\n        aux5 = aux5 - ((aux5 >> 1) & 0x55555555);\n        aux5 = (aux5 & 0x33333333) + ((aux5 >> 2) & 0x33333333);\n        aux6 = aux6 - ((aux6 >> 1) & 0x55555555);\n        aux6 = (aux6 & 0x33333333) + ((aux6 >> 2) & 0x33333333);\n        aux7 = aux7 - ((aux7 >> 1) & 0x55555555);\n        aux7 = (aux7 & 0x33333333) + ((aux7 >> 2) & 0x33333333);\n        bits += ((((aux1 + (aux1 >> 4)) & 0x0F0F0F0F) +\n                    ((aux2 + (aux2 >> 4)) & 0x0F0F0F0F) +\n                    ((aux3 + (aux3 >> 4)) & 0x0F0F0F0F) +\n                    ((aux4 + (aux4 >> 4)) & 0x0F0F0F0F) +\n                    ((aux5 + (aux5 >> 4)) & 0x0F0F0F0F) +\n                    ((aux6 + (aux6 >> 4)) & 0x0F0F0F0F) +\n                    ((aux7 + (aux7 >> 4)) & 0x0F0F0F0F))* 0x01010101) >> 24;\n    }\n    /* Count the remaining bytes. */\n    p = (unsigned char*)p4;\n    while(count--) bits += bitsinbyte[*p++];\n    return bits;\n}\n\n/* Return the position of the first bit set to one (if 'bit' is 1) or\n * zero (if 'bit' is 0) in the bitmap starting at 's' and long 'count' bytes.\n *\n * The function is guaranteed to return a value >= 0 if 'bit' is 0 since if\n * no zero bit is found, it returns count*8 assuming the string is zero\n * padded on the right. However if 'bit' is 1 it is possible that there is\n * not a single set bit in the bitmap. In this special case -1 is returned. */\nlong long redisBitpos(const void *s, unsigned long count, int bit) {\n    unsigned long *l;\n    unsigned char *c;\n    unsigned long skipval, word = 0, one;\n    long long pos = 0; /* Position of bit, to return to the caller. */\n    unsigned long j;\n    int found;\n\n    /* Process whole words first, seeking for first word that is not\n     * all ones or all zeros respectively if we are looking for zeros\n     * or ones. This is much faster with large strings having contiguous\n     * blocks of 1 or 0 bits compared to the vanilla bit per bit processing.\n     *\n     * Note that if we start from an address that is not aligned\n     * to sizeof(unsigned long) we consume it byte by byte until it is\n     * aligned. */\n\n    /* Skip initial bits not aligned to sizeof(unsigned long) byte by byte. */\n    skipval = bit ? 0 : UCHAR_MAX;\n    c = (unsigned char*) s;\n    found = 0;\n    while((unsigned long)c & (sizeof(*l)-1) && count) {\n        if (*c != skipval) {\n            found = 1;\n            break;\n        }\n        c++;\n        count--;\n        pos += 8;\n    }\n\n    /* Skip bits with full word step. */\n    l = (unsigned long*) c;\n    if (!found) {\n        skipval = bit ? 0 : ULONG_MAX;\n        while (count >= sizeof(*l)) {\n            if (*l != skipval) break;\n            l++;\n            count -= sizeof(*l);\n            pos += sizeof(*l)*8;\n        }\n    }\n\n    /* Load bytes into \"word\" considering the first byte as the most significant\n     * (we basically consider it as written in big endian, since we consider the\n     * string as a set of bits from left to right, with the first bit at position\n     * zero.\n     *\n     * Note that the loading is designed to work even when the bytes left\n     * (count) are less than a full word. We pad it with zero on the right. */\n    c = (unsigned char*)l;\n    for (j = 0; j < sizeof(*l); j++) {\n        word <<= 8;\n        if (count) {\n            word |= *c;\n            c++;\n            count--;\n        }\n    }\n\n    /* Special case:\n     * If bits in the string are all zero and we are looking for one,\n     * return -1 to signal that there is not a single \"1\" in the whole\n     * string. This can't happen when we are looking for \"0\" as we assume\n     * that the right of the string is zero padded. */\n    if (bit == 1 && word == 0) return -1;\n\n    /* Last word left, scan bit by bit. The first thing we need is to\n     * have a single \"1\" set in the most significant position in an\n     * unsigned long. We don't know the size of the long so we use a\n     * simple trick. */\n    one = ULONG_MAX; /* All bits set to 1.*/\n    one >>= 1;       /* All bits set to 1 but the MSB. */\n    one = ~one;      /* All bits set to 0 but the MSB. */\n\n    while(one) {\n        if (((one & word) != 0) == bit) return pos;\n        pos++;\n        one >>= 1;\n    }\n\n    /* If we reached this point, there is a bug in the algorithm, since\n     * the case of no match is handled as a special case before. */\n    serverPanic(\"End of redisBitpos() reached.\");\n    return 0; /* Just to avoid warnings. */\n}\n\n/* The following set.*Bitfield and get.*Bitfield functions implement setting\n * and getting arbitrary size (up to 64 bits) signed and unsigned integers\n * at arbitrary positions into a bitmap.\n *\n * The representation considers the bitmap as having the bit number 0 to be\n * the most significant bit of the first byte, and so forth, so for example\n * setting a 5 bits unsigned integer to value 23 at offset 7 into a bitmap\n * previously set to all zeroes, will produce the following representation:\n *\n * +--------+--------+\n * |00000001|01110000|\n * +--------+--------+\n *\n * When offsets and integer sizes are aligned to bytes boundaries, this is the\n * same as big endian, however when such alignment does not exist, its important\n * to also understand how the bits inside a byte are ordered.\n *\n * Note that this format follows the same convention as SETBIT and related\n * commands.\n */\n\nvoid setUnsignedBitfield(unsigned char *p, uint64_t offset, uint64_t bits, uint64_t value) {\n    uint64_t byte, bit, byteval, bitval, j;\n\n    for (j = 0; j < bits; j++) {\n        bitval = (value & ((uint64_t)1<<(bits-1-j))) != 0;\n        byte = offset >> 3;\n        bit = 7 - (offset & 0x7);\n        byteval = p[byte];\n        byteval &= ~(1 << bit);\n        byteval |= bitval << bit;\n        p[byte] = byteval & 0xff;\n        offset++;\n    }\n}\n\nvoid setSignedBitfield(unsigned char *p, uint64_t offset, uint64_t bits, int64_t value) {\n    uint64_t uv = value; /* Casting will add UINT64_MAX + 1 if v is negative. */\n    setUnsignedBitfield(p,offset,bits,uv);\n}\n\nuint64_t getUnsignedBitfield(unsigned char *p, uint64_t offset, uint64_t bits) {\n    uint64_t byte, bit, byteval, bitval, j, value = 0;\n\n    for (j = 0; j < bits; j++) {\n        byte = offset >> 3;\n        bit = 7 - (offset & 0x7);\n        byteval = p[byte];\n        bitval = (byteval >> bit) & 1;\n        value = (value<<1) | bitval;\n        offset++;\n    }\n    return value;\n}\n\nint64_t getSignedBitfield(unsigned char *p, uint64_t offset, uint64_t bits) {\n    int64_t value;\n    union {uint64_t u; int64_t i;} conv;\n\n    /* Converting from unsigned to signed is undefined when the value does\n     * not fit, however here we assume two's complement and the original value\n     * was obtained from signed -> unsigned conversion, so we'll find the\n     * most significant bit set if the original value was negative.\n     *\n     * Note that two's complement is mandatory for exact-width types\n     * according to the C99 standard. */\n    conv.u = getUnsignedBitfield(p,offset,bits);\n    value = conv.i;\n\n    /* If the top significant bit is 1, propagate it to all the\n     * higher bits for two's complement representation of signed\n     * integers. */\n    if (bits < 64 && (value & ((uint64_t)1 << (bits-1))))\n        value |= ((uint64_t)-1) << bits;\n    return value;\n}\n\n/* The following two functions detect overflow of a value in the context\n * of storing it as an unsigned or signed integer with the specified\n * number of bits. The functions both take the value and a possible increment.\n * If no overflow could happen and the value+increment fit inside the limits,\n * then zero is returned, otherwise in case of overflow, 1 is returned,\n * otherwise in case of underflow, -1 is returned.\n *\n * When non-zero is returned (overflow or underflow), if not NULL, *limit is\n * set to the value the operation should result when an overflow happens,\n * depending on the specified overflow semantics:\n *\n * For BFOVERFLOW_SAT if 1 is returned, *limit it is set maximum value that\n * you can store in that integer. when -1 is returned, *limit is set to the\n * minimum value that an integer of that size can represent.\n *\n * For BFOVERFLOW_WRAP *limit is set by performing the operation in order to\n * \"wrap\" around towards zero for unsigned integers, or towards the most\n * negative number that is possible to represent for signed integers. */\n\n#define BFOVERFLOW_WRAP 0\n#define BFOVERFLOW_SAT 1\n#define BFOVERFLOW_FAIL 2 /* Used by the BITFIELD command implementation. */\n\nint checkUnsignedBitfieldOverflow(uint64_t value, int64_t incr, uint64_t bits, int owtype, uint64_t *limit) {\n    uint64_t max = (bits == 64) ? UINT64_MAX : (((uint64_t)1<<bits)-1);\n    int64_t maxincr = max-value;\n    int64_t minincr = -value;\n\n    if (value > max || (incr > 0 && incr > maxincr)) {\n        if (limit) {\n            if (owtype == BFOVERFLOW_WRAP) {\n                goto handle_wrap;\n            } else if (owtype == BFOVERFLOW_SAT) {\n                *limit = max;\n            }\n        }\n        return 1;\n    } else if (incr < 0 && incr < minincr) {\n        if (limit) {\n            if (owtype == BFOVERFLOW_WRAP) {\n                goto handle_wrap;\n            } else if (owtype == BFOVERFLOW_SAT) {\n                *limit = 0;\n            }\n        }\n        return -1;\n    }\n    return 0;\n\nhandle_wrap:\n    {\n        uint64_t mask = ((uint64_t)-1) << bits;\n        uint64_t res = value+incr;\n\n        res &= ~mask;\n        *limit = res;\n    }\n    return 1;\n}\n\nint checkSignedBitfieldOverflow(int64_t value, int64_t incr, int bits, int owtype, int64_t *limit) {\n    int64_t max = (bits == 64) ? INT64_MAX : (((int64_t)1<<(bits-1))-1);\n    int64_t min = (-max)-1;\n\n    /* Note that maxincr and minincr could overflow, but we use the values\n     * only after checking 'value' range, so when we use it no overflow\n     * happens. */\n    int64_t maxincr = max-value;\n    int64_t minincr = min-value;\n\n    if (value > max || (bits != 64 && incr > maxincr) || (value >= 0 && incr > 0 && incr > maxincr))\n    {\n        if (limit) {\n            if (owtype == BFOVERFLOW_WRAP) {\n                goto handle_wrap;\n            } else if (owtype == BFOVERFLOW_SAT) {\n                *limit = max;\n            }\n        }\n        return 1;\n    } else if (value < min || (bits != 64 && incr < minincr) || (value < 0 && incr < 0 && incr < minincr)) {\n        if (limit) {\n            if (owtype == BFOVERFLOW_WRAP) {\n                goto handle_wrap;\n            } else if (owtype == BFOVERFLOW_SAT) {\n                *limit = min;\n            }\n        }\n        return -1;\n    }\n    return 0;\n\nhandle_wrap:\n    {\n        uint64_t msb = (uint64_t)1 << (bits-1);\n        uint64_t a = value, b = incr, c;\n        c = a+b; /* Perform addition as unsigned so that's defined. */\n\n        /* If the sign bit is set, propagate to all the higher order\n         * bits, to cap the negative value. If it's clear, mask to\n         * the positive integer limit. */\n        if (bits < 64) {\n            uint64_t mask = ((uint64_t)-1) << bits;\n            if (c & msb) {\n                c |= mask;\n            } else {\n                c &= ~mask;\n            }\n        }\n        *limit = c;\n    }\n    return 1;\n}\n\n/* Debugging function. Just show bits in the specified bitmap. Not used\n * but here for not having to rewrite it when debugging is needed. */\nvoid printBits(unsigned char *p, unsigned long count) {\n    unsigned long j, i, byte;\n\n    for (j = 0; j < count; j++) {\n        byte = p[j];\n        for (i = 0x80; i > 0; i /= 2)\n            printf(\"%c\", (byte & i) ? '1' : '0');\n        printf(\"|\");\n    }\n    printf(\"\\n\");\n}\n\n/* -----------------------------------------------------------------------------\n * Bits related string commands: GETBIT, SETBIT, BITCOUNT, BITOP.\n * -------------------------------------------------------------------------- */\n\n#define BITOP_AND   0\n#define BITOP_OR    1\n#define BITOP_XOR   2\n#define BITOP_NOT   3\n#define BITOP_LSHIFT 4\n#define BITOP_RSHIFT 5\n\n#define BITFIELDOP_GET 0\n#define BITFIELDOP_SET 1\n#define BITFIELDOP_INCRBY 2\n\n/* Make sure we don't bit shift too many bits at a time.\n * Even this amount is probably way too large. */\n#define BITOP_SHIFT_MAX (1<<30)\n\n/* This helper function used by GETBIT / SETBIT parses the bit offset argument\n * making sure an error is returned if it is negative or if it overflows\n * Redis 512 MB limit for the string value or more (server.proto_max_bulk_len).\n *\n * If the 'hash' argument is true, and 'bits is positive, then the command\n * will also parse bit offsets prefixed by \"#\". In such a case the offset\n * is multiplied by 'bits'. This is useful for the BITFIELD command. */\nint getBitOffsetFromArgument(client *c, robj *o, uint64_t *offset, int hash, int bits) {\n    long long loffset;\n    const char *err = \"bit offset is not an integer or out of range\";\n    char *p = szFromObj(o);\n    size_t plen = sdslen(p);\n    int usehash = 0;\n\n    /* Handle #<offset> form. */\n    if (p[0] == '#' && hash && bits > 0) usehash = 1;\n\n    if (string2ll(p+usehash,plen-usehash,&loffset) == 0) {\n        addReplyError(c,err);\n        return C_ERR;\n    }\n\n    /* Adjust the offset by 'bits' for #<offset> form. */\n    if (usehash) loffset *= bits;\n\n    /* Limit offset to server.proto_max_bulk_len (512MB in bytes by default) */\n    if ((loffset < 0) || (loffset >> 3) >= g_pserver->proto_max_bulk_len)\n    {\n        addReplyError(c,err);\n        return C_ERR;\n    }\n\n    *offset = loffset;\n    return C_OK;\n}\n\n/* This helper function for BITFIELD parses a bitfield type in the form\n * <sign><bits> where sign is 'u' or 'i' for unsigned and signed, and\n * the bits is a value between 1 and 64. However 64 bits unsigned integers\n * are reported as an error because of current limitations of Redis protocol\n * to return unsigned integer values greater than INT64_MAX.\n *\n * On error C_ERR is returned and an error is sent to the client. */\nint getBitfieldTypeFromArgument(client *c, robj *o, int *sign, int *bits) {\n    char *p = szFromObj(o);\n    const char *err = \"Invalid bitfield type. Use something like i16 u8. Note that u64 is not supported but i64 is.\";\n    long long llbits;\n\n    if (p[0] == 'i') {\n        *sign = 1;\n    } else if (p[0] == 'u') {\n        *sign = 0;\n    } else {\n        addReplyError(c,err);\n        return C_ERR;\n    }\n\n    if ((string2ll(p+1,strlen(p+1),&llbits)) == 0 ||\n        llbits < 1 ||\n        (*sign == 1 && llbits > 64) ||\n        (*sign == 0 && llbits > 63))\n    {\n        addReplyError(c,err);\n        return C_ERR;\n    }\n    *bits = llbits;\n    return C_OK;\n}\n\n/* This is an helper function for commands implementations that need to write\n * bits to a string object. The command creates or pad with zeroes the string\n * so that the 'maxbit' bit can be addressed. The object is finally\n * returned. Otherwise if the key holds a wrong type NULL is returned and\n * an error is sent to the client. */\nrobj *lookupStringForBitCommand(client *c, uint64_t maxbit) {\n    size_t byte = maxbit >> 3;\n    robj *o = lookupKeyWrite(c->db,c->argv[1]);\n    if (checkType(c,o,OBJ_STRING)) return NULL;\n\n    if (o == NULL) {\n        o = createObject(OBJ_STRING,sdsnewlen(NULL, byte+1));\n        dbAdd(c->db,c->argv[1],o);\n    } else {\n        o = dbUnshareStringValue(c->db,c->argv[1],o);\n        o->m_ptr = sdsgrowzero(szFromObj(o),byte+1);\n    }\n    return o;\n}\n\n/* Return a pointer to the string object content, and stores its length\n * in 'len'. The user is required to pass (likely stack allocated) buffer\n * 'llbuf' of at least LONG_STR_SIZE bytes. Such a buffer is used in the case\n * the object is integer encoded in order to provide the representation\n * without using heap allocation.\n *\n * The function returns the pointer to the object array of bytes representing\n * the string it contains, that may be a pointer to 'llbuf' or to the\n * internal object representation. As a side effect 'len' is filled with\n * the length of such buffer.\n *\n * If the source object is NULL the function is guaranteed to return NULL\n * and set 'len' to 0. */\nconst unsigned char *getObjectReadOnlyString(robj_roptr o, long *len, char *llbuf) {\n    serverAssert(o->type == OBJ_STRING);\n    const unsigned char *p = NULL;\n\n    /* Set the 'p' pointer to the string, that can be just a stack allocated\n     * array if our string was integer encoded. */\n    if (o && o->encoding == OBJ_ENCODING_INT) {\n        p = (const unsigned char*) llbuf;\n        if (len) *len = ll2string(llbuf,LONG_STR_SIZE,(long)ptrFromObj(o));\n    } else if (o) {\n        p = (const unsigned char*) ptrFromObj(o);\n        if (len) *len = sdslen(szFromObj(o));\n    } else {\n        if (len) *len = 0;\n    }\n    return p;\n}\n\n/* SETBIT key offset bitvalue */\nvoid setbitCommand(client *c) {\n    robj *o;\n    const char *err = \"bit is not an integer or out of range\";\n    uint64_t bitoffset;\n    ssize_t byte, bit;\n    int byteval, bitval;\n    long on;\n\n    if (getBitOffsetFromArgument(c,c->argv[2],&bitoffset,0,0) != C_OK)\n        return;\n\n    if (getLongFromObjectOrReply(c,c->argv[3],&on,err) != C_OK)\n        return;\n\n    /* Bits can only be set or cleared... */\n    if (on & ~1) {\n        addReplyError(c,err);\n        return;\n    }\n\n    if ((o = lookupStringForBitCommand(c,bitoffset)) == NULL) return;\n\n    /* Get current values */\n    byte = bitoffset >> 3;\n    byteval = ((uint8_t*)ptrFromObj(o))[byte];\n    bit = 7 - (bitoffset & 0x7);\n    bitval = byteval & (1 << bit);\n\n    /* Update byte with new bit value and return original value */\n    byteval &= ~(1 << bit);\n    byteval |= ((on & 0x1) << bit);\n    ((uint8_t*)ptrFromObj(o))[byte] = byteval;\n    signalModifiedKey(c,c->db,c->argv[1]);\n    notifyKeyspaceEvent(NOTIFY_STRING,\"setbit\",c->argv[1],c->db->id);\n    g_pserver->dirty++;\n    addReply(c, bitval ? shared.cone : shared.czero);\n}\n\n/* GETBIT key offset */\nvoid getbitCommand(client *c) {\n    robj_roptr o;\n    char llbuf[32];\n    uint64_t bitoffset;\n    size_t byte, bit;\n    size_t bitval = 0;\n\n    if (getBitOffsetFromArgument(c,c->argv[2],&bitoffset,0,0) != C_OK)\n        return;\n\n    if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == nullptr ||\n        checkType(c,o,OBJ_STRING)) return;\n\n    byte = bitoffset >> 3;\n    bit = 7 - (bitoffset & 0x7);\n    if (sdsEncodedObject(o)) {\n        if (byte < sdslen(szFromObj(o)))\n            bitval = ((uint8_t*)ptrFromObj(o))[byte] & (1 << bit);\n    } else {\n        if (byte < (size_t)ll2string(llbuf,sizeof(llbuf),(long)ptrFromObj(o)))\n            bitval = llbuf[byte] & (1 << bit);\n    }\n\n    addReply(c, bitval ? shared.cone : shared.czero);\n}\n\n/* BITOP op_name target_key src_key1 src_key2 src_key3 ... src_keyN */\nvoid bitopCommand(client *c) {\n    char *opname = szFromObj(c->argv[1]);\n    robj *targetkey = c->argv[2];\n    robj_roptr o;\n    int op;\n    unsigned long j, numkeys;\n    robj_roptr *objects;      /* Array of source objects. */\n    unsigned char **src; /* Array of source strings pointers. */\n    unsigned long *len, maxlen = 0; /* Array of length of src strings,\n                                       and max len. */\n    unsigned long minlen = 0;    /* Min len among the input keys. */\n    unsigned char *res = NULL; /* Resulting string. */\n\n    /* Parse the operation name. */\n    if ((opname[0] == 'a' || opname[0] == 'A') && !strcasecmp(opname,\"and\"))\n        op = BITOP_AND;\n    else if((opname[0] == 'o' || opname[0] == 'O') && !strcasecmp(opname,\"or\"))\n        op = BITOP_OR;\n    else if((opname[0] == 'x' || opname[0] == 'X') && !strcasecmp(opname,\"xor\"))\n        op = BITOP_XOR;\n    else if((opname[0] == 'n' || opname[0] == 'N') && !strcasecmp(opname,\"not\"))\n        op = BITOP_NOT;\n    else if (!strcasecmp(opname, \"lshift\"))\n        op = BITOP_LSHIFT;\n    else if (!strcasecmp(opname, \"rshift\"))\n        op = BITOP_RSHIFT;\n    else {\n        addReplyErrorObject(c,shared.syntaxerr);\n        return;\n    }\n\n    /* Sanity check: NOT accepts only a single key argument. */\n    if (op == BITOP_NOT && c->argc != 4) {\n        addReplyError(c,\"BITOP NOT must be called with a single source key.\");\n        return;\n    }\n\n    bool fShiftOp = (op == BITOP_LSHIFT) || (op == BITOP_RSHIFT);\n    long long shift = 0;\n    \n    /* Sanity check: SHIFTS only accept a single arg and an integer */\n    if (fShiftOp) {\n        if (c->argc != 5) {\n            addReplyError(c,\"BITOP SHIFT must be called with a single source key and an integer shift.\");\n            return;\n        }\n        if (getLongLongFromObject(c->argv[4], &shift) != C_OK) {\n            addReplyError(c, \"BITOP SHIFT's last parameter must be an integer\");\n            return;\n        }\n        if (shift > BITOP_SHIFT_MAX) {\n            addReplyError(c, \"BITOP SHIFT value is too large.\");\n            return;\n        }\n\n        if (op == BITOP_RSHIFT)\n            shift = -shift;\n    }\n\n    /* Lookup keys, and store pointers to the string objects into an array. */\n    numkeys = c->argc - (fShiftOp ? 4 : 3);\n    src = (unsigned char**)zmalloc(sizeof(unsigned char*) * numkeys, MALLOC_LOCAL);\n    len = (unsigned long*)zmalloc(sizeof(long) * numkeys, MALLOC_LOCAL);\n    objects = (robj_roptr*)zmalloc(sizeof(robj_roptr) * numkeys, MALLOC_LOCAL);\n    for (j = 0; j < numkeys; j++) {\n        o = lookupKeyRead(c->db,c->argv[j+3]);\n        /* Handle non-existing keys as empty strings. */\n        if (o == nullptr) {\n            objects[j] = nullptr;\n            src[j] = NULL;\n            len[j] = 0;\n            minlen = 0;\n            continue;\n        }\n        /* Return an error if one of the keys is not a string. */\n        if (checkType(c,o,OBJ_STRING)) {\n            unsigned long i;\n            for (i = 0; i < j; i++) {\n                if (objects[i])\n                    decrRefCount(objects[i]);\n            }\n            zfree(src);\n            zfree(len);\n            zfree(objects);\n            return;\n        }\n        objects[j] = getDecodedObject(o);\n        src[j] = (unsigned char*)ptrFromObj(objects[j]);\n        len[j] = sdslen(szFromObj(objects[j]));\n        if (len[j] > maxlen) maxlen = len[j];\n        if (j == 0 || len[j] < minlen) minlen = len[j];\n    }\n\n    if (fShiftOp)\n    {\n        long long newlen = (long long)maxlen + shift/CHAR_BIT;\n        if (shift > 0 && (shift % CHAR_BIT) != 0)\n            newlen++;\n\n        if (newlen < 0)\n            newlen = 0;\n        \n        if (newlen)\n        {\n            res = (unsigned char*) sdsnewlen(NULL,newlen);\n            if (shift >= 0)\n            {   // left shift\n                long long byteoffset = shift/CHAR_BIT;\n                memset(res, 0, byteoffset);\n                long long srcLen = newlen - byteoffset - ((shift % CHAR_BIT) ? 1 : 0);\n\n                // now the bitshift+copy\n                unsigned bitshift = shift % CHAR_BIT;\n                unsigned char carry = 0;\n                for (long long iSrc = 0; iSrc < srcLen; ++iSrc)\n                {\n                    res[byteoffset+iSrc] = (src[0][iSrc] << bitshift) | carry;\n                    carry = src[0][iSrc] >> (CHAR_BIT - bitshift);\n                }\n                if (bitshift)\n                    res[newlen-1] = carry;\n            } \n            else \n            {   // right shift\n                long long byteoffset = -shift/CHAR_BIT;\n                unsigned bitshift = -shift % CHAR_BIT;\n                if (bitshift)\n                    ++byteoffset;\n                res[0] = (src[0][byteoffset] << (CHAR_BIT-bitshift));\n                if (byteoffset > 0)\n                    res[0] |= (src[0][byteoffset-1] >> bitshift);\n                for (long long idx = 1; idx < newlen; ++idx)\n                {\n                    res[idx] = (src[0][byteoffset+idx] << (CHAR_BIT-bitshift)) | (src[0][byteoffset+idx-1] >> bitshift);\n                }\n            }\n        }\n        maxlen = newlen;    // this is to ensure we DEL below if newlen was 0\n    }\n    else\n    {\n        /* Compute the bit operation, if at least one string is not empty. */\n        if (maxlen) {\n            res = (unsigned char*) sdsnewlen(NULL,maxlen);\n            unsigned long i;\n\n            /* Fast path: as far as we have data for all the input bitmaps we\n            * can take a fast path that performs much better than the\n            * vanilla algorithm. On ARM we skip the fast path since it will\n            * result in GCC compiling the code using multiple-words load/store\n            * operations that are not supported even in ARM >= v6. */\n            j = 0;\n            #ifndef USE_ALIGNED_ACCESS\n            if (minlen >= sizeof(unsigned long)*4 && numkeys <= 16) {\n                unsigned long *lp[16];\n                unsigned long *lres = (unsigned long*) res;\n\n                /* Note: sds pointer is always aligned to 8 byte boundary. */\n                memcpy(lp,src,sizeof(unsigned long*)*numkeys);\n                memcpy(res,src[0],minlen);\n\n                /* Different branches per different operations for speed (sorry). */\n                if (op == BITOP_AND) {\n                    while(minlen >= sizeof(unsigned long)*4) {\n                        for (i = 1; i < numkeys; i++) {\n                            lres[0] &= lp[i][0];\n                            lres[1] &= lp[i][1];\n                            lres[2] &= lp[i][2];\n                            lres[3] &= lp[i][3];\n                            lp[i]+=4;\n                        }\n                        lres+=4;\n                        j += sizeof(unsigned long)*4;\n                        minlen -= sizeof(unsigned long)*4;\n                    }\n                } else if (op == BITOP_OR) {\n                    while(minlen >= sizeof(unsigned long)*4) {\n                        for (i = 1; i < numkeys; i++) {\n                            lres[0] |= lp[i][0];\n                            lres[1] |= lp[i][1];\n                            lres[2] |= lp[i][2];\n                            lres[3] |= lp[i][3];\n                            lp[i]+=4;\n                        }\n                        lres+=4;\n                        j += sizeof(unsigned long)*4;\n                        minlen -= sizeof(unsigned long)*4;\n                    }\n                } else if (op == BITOP_XOR) {\n                    while(minlen >= sizeof(unsigned long)*4) {\n                        for (i = 1; i < numkeys; i++) {\n                            lres[0] ^= lp[i][0];\n                            lres[1] ^= lp[i][1];\n                            lres[2] ^= lp[i][2];\n                            lres[3] ^= lp[i][3];\n                            lp[i]+=4;\n                        }\n                        lres+=4;\n                        j += sizeof(unsigned long)*4;\n                        minlen -= sizeof(unsigned long)*4;\n                    }\n                } else if (op == BITOP_NOT) {\n                    while(minlen >= sizeof(unsigned long)*4) {\n                        lres[0] = ~lres[0];\n                        lres[1] = ~lres[1];\n                        lres[2] = ~lres[2];\n                        lres[3] = ~lres[3];\n                        lres+=4;\n                        j += sizeof(unsigned long)*4;\n                        minlen -= sizeof(unsigned long)*4;\n                    }\n                }\n            }\n            #endif\n        }\n\n        /* j is set to the next byte to process by the previous loop. */\n        for (; j < maxlen; j++) {\n            auto output = (len[0] <= j) ? 0 : src[0][j];\n            if (op == BITOP_NOT) output = ~output;\n            for (unsigned long i = 1; i < numkeys; i++) {\n                int skip = 0;\n                auto byte = (len[i] <= j) ? 0 : src[i][j];\n                switch(op) {\n                case BITOP_AND:\n                    output &= byte;\n                    skip = (output == 0);\n                    break;\n                case BITOP_OR:\n                    output |= byte;\n                    skip = (output == 0xff);\n                    break;\n                case BITOP_XOR: output ^= byte; break;\n                }\n\n                if (skip) {\n                    break;\n                }\n            }\n            res[j] = output;\n        }\n    }\n    for (j = 0; j < numkeys; j++) {\n        if (objects[j])\n            decrRefCount(objects[j]);\n    }\n    zfree(src);\n    zfree(len);\n    zfree(objects);\n\n    /* Store the computed value into the target key */\n    if (maxlen) {\n        robj *o = createObject(OBJ_STRING,res);\n        setKey(c,c->db,targetkey,o);\n        notifyKeyspaceEvent(NOTIFY_STRING,\"set\",targetkey,c->db->id);\n        decrRefCount(o);\n        g_pserver->dirty++;\n    } else if (dbDelete(c->db,targetkey)) {\n        signalModifiedKey(c,c->db,targetkey);\n        notifyKeyspaceEvent(NOTIFY_GENERIC,\"del\",targetkey,c->db->id);\n        g_pserver->dirty++;\n    }\n    addReplyLongLong(c,maxlen); /* Return the output string length in bytes. */\n}\n\n/* BITCOUNT key [start end] */\nvoid bitcountCommand(client *c) {\n    robj_roptr o;\n    long start, end, strlen;\n    const unsigned char *p;\n    char llbuf[LONG_STR_SIZE];\n\n    /* Lookup, check for type, and return 0 for non existing keys. */\n    if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == nullptr ||\n        checkType(c,o,OBJ_STRING)) return;\n    p = getObjectReadOnlyString(o,&strlen,llbuf);\n\n    /* Parse start/end range if any. */\n    if (c->argc == 4) {\n        if (getLongFromObjectOrReply(c,c->argv[2],&start,NULL) != C_OK)\n            return;\n        if (getLongFromObjectOrReply(c,c->argv[3],&end,NULL) != C_OK)\n            return;\n        /* Convert negative indexes */\n        if (start < 0 && end < 0 && start > end) {\n            addReply(c,shared.czero);\n            return;\n        }\n        if (start < 0) start = strlen+start;\n        if (end < 0) end = strlen+end;\n        if (start < 0) start = 0;\n        if (end < 0) end = 0;\n        if (end >= strlen) end = strlen-1;\n    } else if (c->argc == 2) {\n        /* The whole string. */\n        start = 0;\n        end = strlen-1;\n    } else {\n        /* Syntax error. */\n        addReplyErrorObject(c,shared.syntaxerr);\n        return;\n    }\n\n    /* Precondition: end >= 0 && end < strlen, so the only condition where\n     * zero can be returned is: start > end. */\n    if (start > end) {\n        addReply(c,shared.czero);\n    } else {\n        long bytes = end-start+1;\n\n        addReplyLongLong(c,redisPopcount(p+start,bytes));\n    }\n}\n\n/* BITPOS key bit [start [end]] */\nvoid bitposCommand(client *c) {\n    robj_roptr o;\n    long bit, start, end, strlen;\n    const unsigned char *p;\n    char llbuf[LONG_STR_SIZE];\n    int end_given = 0;\n\n    /* Parse the bit argument to understand what we are looking for, set\n     * or clear bits. */\n    if (getLongFromObjectOrReply(c,c->argv[2],&bit,NULL) != C_OK)\n        return;\n    if (bit != 0 && bit != 1) {\n        addReplyError(c, \"The bit argument must be 1 or 0.\");\n        return;\n    }\n\n    /* If the key does not exist, from our point of view it is an infinite\n     * array of 0 bits. If the user is looking for the fist clear bit return 0,\n     * If the user is looking for the first set bit, return -1. */\n    if ((o = lookupKeyRead(c->db,c->argv[1])) == nullptr) {\n        addReplyLongLong(c, bit ? -1 : 0);\n        return;\n    }\n    if (checkType(c,o,OBJ_STRING)) return;\n    p = getObjectReadOnlyString(o,&strlen,llbuf);\n\n    /* Parse start/end range if any. */\n    if (c->argc == 4 || c->argc == 5) {\n        if (getLongFromObjectOrReply(c,c->argv[3],&start,NULL) != C_OK)\n            return;\n        if (c->argc == 5) {\n            if (getLongFromObjectOrReply(c,c->argv[4],&end,NULL) != C_OK)\n                return;\n            end_given = 1;\n        } else {\n            end = strlen-1;\n        }\n        /* Convert negative indexes */\n        if (start < 0) start = strlen+start;\n        if (end < 0) end = strlen+end;\n        if (start < 0) start = 0;\n        if (end < 0) end = 0;\n        if (end >= strlen) end = strlen-1;\n    } else if (c->argc == 3) {\n        /* The whole string. */\n        start = 0;\n        end = strlen-1;\n    } else {\n        /* Syntax error. */\n        addReplyErrorObject(c,shared.syntaxerr);\n        return;\n    }\n\n    /* For empty ranges (start > end) we return -1 as an empty range does\n     * not contain a 0 nor a 1. */\n    if (start > end) {\n        addReplyLongLong(c, -1);\n    } else {\n        long bytes = end-start+1;\n        long long pos = redisBitpos(p+start,bytes,bit);\n\n        /* If we are looking for clear bits, and the user specified an exact\n         * range with start-end, we can't consider the right of the range as\n         * zero padded (as we do when no explicit end is given).\n         *\n         * So if redisBitpos() returns the first bit outside the range,\n         * we return -1 to the caller, to mean, in the specified range there\n         * is not a single \"0\" bit. */\n        if (end_given && bit == 0 && pos == (long long)bytes<<3) {\n            addReplyLongLong(c,-1);\n            return;\n        }\n        if (pos != -1) pos += (long long)start<<3; /* Adjust for the bytes we skipped. */\n        addReplyLongLong(c,pos);\n    }\n}\n\n/* BITFIELD key subcommmand-1 arg ... subcommand-2 arg ... subcommand-N ...\n *\n * Supported subcommands:\n *\n * GET <type> <offset>\n * SET <type> <offset> <value>\n * INCRBY <type> <offset> <increment>\n * OVERFLOW [WRAP|SAT|FAIL]\n */\n\n#define BITFIELD_FLAG_NONE      0\n#define BITFIELD_FLAG_READONLY  (1<<0)\n\nstruct bitfieldOp {\n    uint64_t offset;    /* Bitfield offset. */\n    int64_t i64;        /* Increment amount (INCRBY) or SET value */\n    int opcode;         /* Operation id. */\n    int owtype;         /* Overflow type to use. */\n    int bits;           /* Integer bitfield bits width. */\n    int sign;           /* True if signed, otherwise unsigned op. */\n};\n\n/* This implements both the BITFIELD command and the BITFIELD_RO command\n * when flags is set to BITFIELD_FLAG_READONLY: in this case only the\n * GET subcommand is allowed, other subcommands will return an error. */\nvoid bitfieldGeneric(client *c, int flags) {\n    robj_roptr o;\n    uint64_t bitoffset;\n    int j, numops = 0, changes = 0;\n    struct bitfieldOp *ops = NULL; /* Array of ops to execute at end. */\n    int owtype = BFOVERFLOW_WRAP; /* Overflow type. */\n    int readonly = 1;\n    uint64_t highest_write_offset = 0;\n\n    for (j = 2; j < c->argc; j++) {\n        int remargs = c->argc-j-1; /* Remaining args other than current. */\n        char *subcmd = szFromObj(c->argv[j]); /* Current command name. */\n        int opcode; /* Current operation code. */\n        long long i64 = 0;  /* Signed SET value. */\n        int sign = 0; /* Signed or unsigned type? */\n        int bits = 0; /* Bitfield width in bits. */\n\n        if (!strcasecmp(subcmd,\"get\") && remargs >= 2)\n            opcode = BITFIELDOP_GET;\n        else if (!strcasecmp(subcmd,\"set\") && remargs >= 3)\n            opcode = BITFIELDOP_SET;\n        else if (!strcasecmp(subcmd,\"incrby\") && remargs >= 3)\n            opcode = BITFIELDOP_INCRBY;\n        else if (!strcasecmp(subcmd,\"overflow\") && remargs >= 1) {\n            char *owtypename = szFromObj(c->argv[j+1]);\n            j++;\n            if (!strcasecmp(owtypename,\"wrap\"))\n                owtype = BFOVERFLOW_WRAP;\n            else if (!strcasecmp(owtypename,\"sat\"))\n                owtype = BFOVERFLOW_SAT;\n            else if (!strcasecmp(owtypename,\"fail\"))\n                owtype = BFOVERFLOW_FAIL;\n            else {\n                addReplyError(c,\"Invalid OVERFLOW type specified\");\n                zfree(ops);\n                return;\n            }\n            continue;\n        } else {\n            addReplyErrorObject(c,shared.syntaxerr);\n            zfree(ops);\n            return;\n        }\n\n        /* Get the type and offset arguments, common to all the ops. */\n        if (getBitfieldTypeFromArgument(c,c->argv[j+1],&sign,&bits) != C_OK) {\n            zfree(ops);\n            return;\n        }\n\n        if (getBitOffsetFromArgument(c,c->argv[j+2],&bitoffset,1,bits) != C_OK){\n            zfree(ops);\n            return;\n        }\n\n        if (opcode != BITFIELDOP_GET) {\n            readonly = 0;\n            if (highest_write_offset < bitoffset + bits - 1)\n                highest_write_offset = bitoffset + bits - 1;\n            /* INCRBY and SET require another argument. */\n            if (getLongLongFromObjectOrReply(c,c->argv[j+3],&i64,NULL) != C_OK){\n                zfree(ops);\n                return;\n            }\n        }\n\n        /* Populate the array of operations we'll process. */\n        ops = (bitfieldOp*)zrealloc(ops,sizeof(*ops)*(numops+1), MALLOC_SHARED);\n        ops[numops].offset = bitoffset;\n        ops[numops].i64 = i64;\n        ops[numops].opcode = opcode;\n        ops[numops].owtype = owtype;\n        ops[numops].bits = bits;\n        ops[numops].sign = sign;\n        numops++;\n\n        j += 3 - (opcode == BITFIELDOP_GET);\n    }\n\n    if (readonly) {\n        /* Lookup for read is ok if key doesn't exit, but errors\n         * if it's not a string. */\n        o = lookupKeyRead(c->db,c->argv[1]);\n        if (o != nullptr && checkType(c,o,OBJ_STRING)) {\n            zfree(ops);\n            return;\n        }\n    } else {\n        if (flags & BITFIELD_FLAG_READONLY) {\n            zfree(ops);\n            addReplyError(c, \"BITFIELD_RO only supports the GET subcommand\");\n            return;\n        }\n\n        /* Lookup by making room up to the farest bit reached by\n         * this operation. */\n        if ((o = lookupStringForBitCommand(c,\n            highest_write_offset)) == nullptr) {\n            zfree(ops);\n            return;\n        }\n    }\n\n    addReplyArrayLen(c,numops);\n\n    /* Actually process the operations. */\n    for (j = 0; j < numops; j++) {\n        struct bitfieldOp *thisop = ops+j;\n\n        /* Execute the operation. */\n        if (thisop->opcode == BITFIELDOP_SET ||\n            thisop->opcode == BITFIELDOP_INCRBY)\n        {\n            /* SET and INCRBY: We handle both with the same code path\n             * for simplicity. SET return value is the previous value so\n             * we need fetch & store as well. */\n\n            /* We need two different but very similar code paths for signed\n             * and unsigned operations, since the set of functions to get/set\n             * the integers and the used variables types are different. */\n            if (thisop->sign) {\n                int64_t oldval, newval, wrapped, retval;\n                int overflow;\n\n                oldval = getSignedBitfield((unsigned char*)ptrFromObj(o),thisop->offset,\n                        thisop->bits);\n\n                if (thisop->opcode == BITFIELDOP_INCRBY) {\n                    newval = oldval + thisop->i64;\n                    overflow = checkSignedBitfieldOverflow(oldval,\n                            thisop->i64,thisop->bits,thisop->owtype,&wrapped);\n                    if (overflow) newval = wrapped;\n                    retval = newval;\n                } else {\n                    newval = thisop->i64;\n                    overflow = checkSignedBitfieldOverflow(newval,\n                            0,thisop->bits,thisop->owtype,&wrapped);\n                    if (overflow) newval = wrapped;\n                    retval = oldval;\n                }\n\n                /* On overflow of type is \"FAIL\", don't write and return\n                 * NULL to signal the condition. */\n                if (!(overflow && thisop->owtype == BFOVERFLOW_FAIL)) {\n                    addReplyLongLong(c,retval);\n                    setSignedBitfield((unsigned char*)ptrFromObj(o),thisop->offset,\n                                      thisop->bits,newval);\n                } else {\n                    addReplyNull(c);\n                }\n            } else {\n                uint64_t oldval, newval, wrapped, retval;\n                int overflow;\n\n                oldval = getUnsignedBitfield((unsigned char*)ptrFromObj(o),thisop->offset,\n                        thisop->bits);\n\n                if (thisop->opcode == BITFIELDOP_INCRBY) {\n                    newval = oldval + thisop->i64;\n                    overflow = checkUnsignedBitfieldOverflow(oldval,\n                            thisop->i64,thisop->bits,thisop->owtype,&wrapped);\n                    if (overflow) newval = wrapped;\n                    retval = newval;\n                } else {\n                    newval = thisop->i64;\n                    overflow = checkUnsignedBitfieldOverflow(newval,\n                            0,thisop->bits,thisop->owtype,&wrapped);\n                    if (overflow) newval = wrapped;\n                    retval = oldval;\n                }\n                /* On overflow of type is \"FAIL\", don't write and return\n                 * NULL to signal the condition. */\n                if (!(overflow && thisop->owtype == BFOVERFLOW_FAIL)) {\n                    addReplyLongLong(c,retval);\n                    setUnsignedBitfield((unsigned char*)ptrFromObj(o),thisop->offset,\n                                        thisop->bits,newval);\n                } else {\n                    addReplyNull(c);\n                }\n            }\n            changes++;\n        } else {\n            /* GET */\n            unsigned char buf[9];\n            long strlen = 0;\n            const unsigned char *src = NULL;\n            char llbuf[LONG_STR_SIZE];\n\n            if (o != nullptr)\n                src = getObjectReadOnlyString(o,&strlen,llbuf);\n\n            /* For GET we use a trick: before executing the operation\n             * copy up to 9 bytes to a local buffer, so that we can easily\n             * execute up to 64 bit operations that are at actual string\n             * object boundaries. */\n            memset(buf,0,9);\n            int i;\n            uint64_t byte = thisop->offset >> 3;\n            for (i = 0; i < 9; i++) {\n                if (src == NULL || i+byte >= (uint64_t)strlen) break;\n                buf[i] = src[i+byte];\n            }\n\n            /* Now operate on the copied buffer which is guaranteed\n             * to be zero-padded. */\n            if (thisop->sign) {\n                int64_t val = getSignedBitfield(buf,thisop->offset-(byte*8),\n                                            thisop->bits);\n                addReplyLongLong(c,val);\n            } else {\n                uint64_t val = getUnsignedBitfield(buf,thisop->offset-(byte*8),\n                                            thisop->bits);\n                addReplyLongLong(c,val);\n            }\n        }\n    }\n\n    if (changes) {\n        signalModifiedKey(c,c->db,c->argv[1]);\n        notifyKeyspaceEvent(NOTIFY_STRING,\"setbit\",c->argv[1],c->db->id);\n        g_pserver->dirty += changes;\n    }\n    zfree(ops);\n}\n\nvoid bitfieldCommand(client *c) {\n    bitfieldGeneric(c, BITFIELD_FLAG_NONE);\n}\n\nvoid bitfieldroCommand(client *c) {\n    bitfieldGeneric(c, BITFIELD_FLAG_READONLY);\n}\n"
  },
  {
    "path": "src/blocked.cpp",
    "content": "/* blocked.c - generic support for blocking operations like BLPOP & WAIT.\n *\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n *\n * ---------------------------------------------------------------------------\n *\n * API:\n *\n * blockClient() set the CLIENT_BLOCKED flag in the client, and set the\n * specified block type 'btype' filed to one of BLOCKED_* macros.\n *\n * unblockClient() unblocks the client doing the following:\n * 1) It calls the btype-specific function to cleanup the state.\n * 2) It unblocks the client by unsetting the CLIENT_BLOCKED flag.\n * 3) It puts the client into a list of just unblocked clients that are\n *    processed ASAP in the beforeSleep() event loop callback, so that\n *    if there is some query buffer to process, we do it. This is also\n *    required because otherwise there is no 'readable' event fired, we\n *    already read the pending commands. We also set the CLIENT_UNBLOCKED\n *    flag to remember the client is in the unblocked_clients list.\n *\n * processUnblockedClients() is called inside the beforeSleep() function\n * to process the query buffer from unblocked clients and remove the clients\n * from the blocked_clients queue.\n *\n * replyToBlockedClientTimedOut() is called by the cron function when\n * a client blocked reaches the specified timeout (if the timeout is set\n * to 0, no timeout is processed).\n * It usually just needs to send a reply to the client.\n *\n * When implementing a new type of blocking operation, the implementation\n * should modify unblockClient() and replyToBlockedClientTimedOut() in order\n * to handle the btype-specific behavior of this two functions.\n * If the blocking operation waits for certain keys to change state, the\n * clusterRedirectBlockedClientIfNeeded() function should also be updated.\n */\n\n#include \"server.h\"\n#include \"slowlog.h\"\n#include \"latency.h\"\n#include \"monotonic.h\"\n#include <mutex>\n\nint serveClientBlockedOnList(client *receiver, robj *key, robj *dstkey, redisDb *db, robj *value, int wherefrom, int whereto);\nint getListPositionFromObjectOrReply(client *c, robj *arg, int *position);\n\n/* This structure represents the blocked key information that we store\n * in the client structure. Each client blocked on keys, has a\n * client->bpop.keys hash table. The keys of the hash table are Redis\n * keys pointers to 'robj' structures. The value is this structure.\n * The structure has two goals: firstly we store the list node that this\n * client uses to be listed in the database \"blocked clients for this key\"\n * list, so we can later unblock in O(1) without a list scan.\n * Secondly for certain blocking types, we have additional info. Right now\n * the only use for additional info we have is when clients are blocked\n * on streams, as we have to remember the ID it blocked for. */\ntypedef struct bkinfo {\n    listNode *listnode;     /* List node for db->blocking_keys[key] list. */\n    streamID stream_id;     /* Stream ID if we blocked in a stream. */\n} bkinfo;\n\n/* Block a client for the specific operation type. Once the CLIENT_BLOCKED\n * flag is set client query buffer is not longer processed, but accumulated,\n * and will be processed when the client is unblocked. */\nvoid blockClient(client *c, int btype) {\n    serverAssert(GlobalLocksAcquired());\n    /* Master client should never be blocked unless pause or module */\n    serverAssert(!(c->flags & CLIENT_MASTER &&\n                   btype != BLOCKED_MODULE &&\n                   btype != BLOCKED_PAUSE));\n\n    c->flags |= CLIENT_BLOCKED;\n    c->btype = btype;\n    if (btype == BLOCKED_ASYNC)\n        c->casyncOpsPending++;\n    g_pserver->blocked_clients++;\n    g_pserver->blocked_clients_by_type[btype]++;\n    addClientToTimeoutTable(c);\n    if (btype == BLOCKED_PAUSE) {\n        listAddNodeTail(g_pserver->paused_clients, c);\n        c->paused_list_node = listLast(g_pserver->paused_clients);\n        /* Mark this client to execute its command */\n        c->flags |= CLIENT_PENDING_COMMAND;\n    }\n}\n\n/* This function is called after a client has finished a blocking operation\n * in order to update the total command duration, log the command into\n * the Slow log if needed, and log the reply duration event if needed. */\nvoid updateStatsOnUnblock(client *c, long blocked_us, long reply_us){\n    const ustime_t total_cmd_duration = c->duration + blocked_us + reply_us;\n    c->lastcmd->microseconds += total_cmd_duration;\n\n    /* Log the command into the Slow log if needed. */\n    slowlogPushCurrentCommand(c, c->lastcmd, total_cmd_duration);\n    /* Log the reply duration event. */\n    latencyAddSampleIfNeeded(\"command-unblocking\",reply_us/1000);\n}\n\n/* This function is called in the beforeSleep() function of the event loop\n * in order to process the pending input buffer of clients that were\n * unblocked after a blocking operation. */\nvoid processUnblockedClients(int iel) {\n    serverAssert(GlobalLocksAcquired());\n\n    listNode *ln;\n    client *c;\n    list *unblocked_clients = g_pserver->rgthreadvar[iel].unblocked_clients;\n    serverAssert(iel == (serverTL - g_pserver->rgthreadvar));\n\n    while (listLength(unblocked_clients)) {\n        ln = listFirst(unblocked_clients);\n        serverAssert(ln != NULL);\n        c = (client*)ln->value;\n        listDelNode(unblocked_clients,ln);\n        AssertCorrectThread(c);\n        \n        std::unique_lock<fastlock> ul(c->lock);\n        c->flags &= ~CLIENT_UNBLOCKED;\n\n        /* Process remaining data in the input buffer, unless the client\n         * is blocked again. Actually processInputBuffer() checks that the\n         * client is not blocked before to proceed, but things may change and\n         * the code is conceptually more correct this way. */\n        if (!(c->flags & CLIENT_BLOCKED)) {\n            /* If we have a queued command, execute it now. */\n            if (processPendingCommandsAndResetClient(c, CMD_CALL_FULL) == C_ERR) {\n                continue;\n            }\n            /* Then process client if it has more data in it's buffer. */\n            if (c->querybuf && sdslen(c->querybuf) > 0) {\n                processInputBuffer(c, true /*fParse*/, CMD_CALL_FULL);\n            }\n        }\n    }\n}\n\n/* This function will schedule the client for reprocessing at a safe time.\n *\n * This is useful when a client was blocked for some reason (blocking operation,\n * CLIENT PAUSE, or whatever), because it may end with some accumulated query\n * buffer that needs to be processed ASAP:\n *\n * 1. When a client is blocked, its readable handler is still active.\n * 2. However in this case it only gets data into the query buffer, but the\n *    query is not parsed or executed once there is enough to proceed as\n *    usually (because the client is blocked... so we can't execute commands).\n * 3. When the client is unblocked, without this function, the client would\n *    have to write some query in order for the readable handler to finally\n *    call processQueryBuffer*() on it.\n * 4. With this function instead we can put the client in a queue that will\n *    process it for queries ready to be executed at a safe time.\n */\nvoid queueClientForReprocessing(client *c) {\n    /* The client may already be into the unblocked list because of a previous\n     * blocking operation, don't add back it into the list multiple times. */\n    serverAssert(GlobalLocksAcquired());\n    std::unique_lock<fastlock> ul(c->lock);\n    if (!(c->flags & CLIENT_UNBLOCKED)) {\n        c->flags |= CLIENT_UNBLOCKED;\n        listAddNodeTail(g_pserver->rgthreadvar[c->iel].unblocked_clients,c);\n    }\n}\n\n/* Unblock a client calling the right function depending on the kind\n * of operation the client is blocking for. */\nvoid unblockClient(client *c) {\n    serverAssert(GlobalLocksAcquired());\n    serverAssert(c->lock.fOwnLock());\n    if (c->btype == BLOCKED_LIST ||\n        c->btype == BLOCKED_ZSET ||\n        c->btype == BLOCKED_STREAM) {\n        unblockClientWaitingData(c);\n    } else if (c->btype == BLOCKED_WAIT) {\n        unblockClientWaitingReplicas(c);\n    } else if (c->btype == BLOCKED_MODULE) {\n        if (moduleClientIsBlockedOnKeys(c)) unblockClientWaitingData(c);\n        unblockClientFromModule(c);\n    } else if (c->btype == BLOCKED_ASYNC) {\n        serverAssert(c->casyncOpsPending > 0);\n        c->casyncOpsPending--;\n    } else if (c->btype == BLOCKED_PAUSE) {\n        listDelNode(g_pserver->paused_clients,c->paused_list_node);\n        c->paused_list_node = NULL;\n    } else {\n        serverPanic(\"Unknown btype %d in unblockClient() for client %llu.\",\n            c->btype, (unsigned long long) c->id);\n    }\n\n    /* Reset the client for a new query since, for blocking commands\n     * we do not do it immediately after the command returns (when the\n     * client got blocked) in order to be still able to access the argument\n     * vector from module callbacks and updateStatsOnUnblock. */\n    if (c->btype != BLOCKED_PAUSE) {\n        freeClientOriginalArgv(c);\n        resetClient(c);\n    }\n\n    /* Clear the flags, and put the client in the unblocked list so that\n     * we'll process new commands in its query buffer ASAP. */\n    g_pserver->blocked_clients--;\n    g_pserver->blocked_clients_by_type[c->btype]--;\n    c->flags &= ~CLIENT_BLOCKED;\n    c->btype = BLOCKED_NONE;\n    removeClientFromTimeoutTable(c);\n    queueClientForReprocessing(c);\n}\n\n/* This function gets called when a blocked client timed out in order to\n * send it a reply of some kind. After this function is called,\n * unblockClient() will be called with the same client as argument. */\nvoid replyToBlockedClientTimedOut(client *c) {\n    if (c->btype == BLOCKED_LIST ||\n        c->btype == BLOCKED_ZSET ||\n        c->btype == BLOCKED_STREAM) {\n        addReplyNullArray(c);\n    } else if (c->btype == BLOCKED_WAIT) {\n        addReplyLongLong(c,replicationCountAcksByOffset(c->bpop.reploffset));\n    } else if (c->btype == BLOCKED_MODULE) {\n        moduleBlockedClientTimedOut(c);\n    } else {\n        serverPanic(\"Unknown btype %d in replyToBlockedClientTimedOut() for client %llu.\",\n            c->btype, (unsigned long long) c->id);\n    }\n}\n\n/* Mass-unblock clients because something changed in the instance that makes\n * blocking no longer safe. For example clients blocked in list operations\n * in an instance which turns from master to replica is unsafe, so this function\n * is called when a master turns into a replica.\n *\n * The semantics is to send an -UNBLOCKED error to the client, disconnecting\n * it at the same time. */\nvoid disconnectAllBlockedClients(void) {\n    serverAssert(GlobalLocksAcquired());\n    listNode *ln;\n    listIter li;\n\n    listRewind(g_pserver->clients,&li);\n    while((ln = listNext(&li))) {\n        client *c = (client*)listNodeValue(ln);\n        \n        std::unique_lock<fastlock> ul(c->lock);\n        if (c->flags & CLIENT_BLOCKED) {\n            /* PAUSED clients are an exception, when they'll be unblocked, the\n             * command processing will start from scratch, and the command will\n             * be either executed or rejected. (unlike LIST blocked clients for\n             * which the command is already in progress in a way. */\n            if (c->btype == BLOCKED_PAUSE)\n                continue;\n\n            addReplyError(c,\n                \"-UNBLOCKED force unblock from blocking operation, \"\n                \"instance state changed (master -> replica?)\");\n            unblockClient(c);\n            c->flags |= CLIENT_CLOSE_AFTER_REPLY;\n        }\n    }\n}\n\n/* Helper function for handleClientsBlockedOnKeys(). This function is called\n * when there may be clients blocked on a list key, and there may be new\n * data to fetch (the key is ready). */\nvoid serveClientsBlockedOnListKey(robj *o, readyList *rl) {\n    /* We serve clients in the same order they blocked for\n     * this key, from the first blocked to the last. */\n    dictEntry *de = dictFind(rl->db->blocking_keys,rl->key);\n    if (de) {\n        list *clients = (list*)dictGetVal(de);\n        int numclients = listLength(clients);\n\n        while(numclients--) {\n            listNode *clientnode = listFirst(clients);\n            client *receiver = (client*)clientnode->value;\n            std::unique_lock<decltype(receiver->lock)> lock(receiver->lock);\n\n            if (receiver->btype != BLOCKED_LIST) {\n                /* Put at the tail, so that at the next call\n                 * we'll not run into it again. */\n                listRotateHeadToTail(clients);\n                continue;\n            }\n\n            robj *dstkey = receiver->bpop.target;\n            int wherefrom = receiver->bpop.listpos.wherefrom;\n            int whereto = receiver->bpop.listpos.whereto;\n            robj *value = listTypePop(o, wherefrom);\n\n            if (value) {\n                /* Protect receiver->bpop.target, that will be\n                 * freed by the next unblockClient()\n                 * call. */\n                if (dstkey) incrRefCount(dstkey);\n\n                monotime replyTimer;\n                elapsedStart(&replyTimer);\n                if (serveClientBlockedOnList(receiver,\n                    rl->key,dstkey,rl->db,value,\n                    wherefrom, whereto) == C_ERR)\n                {\n                    /* If we failed serving the client we need\n                     * to also undo the POP operation. */\n                    listTypePush(o,value,wherefrom);\n                }\n                updateStatsOnUnblock(receiver, 0, elapsedUs(replyTimer));\n                unblockClient(receiver);\n\n                if (dstkey) decrRefCount(dstkey);\n                decrRefCount(value);\n            } else {\n                break;\n            }\n        }\n    }\n\n    if (listTypeLength(o) == 0) {\n        dbDelete(rl->db,rl->key);\n        notifyKeyspaceEvent(NOTIFY_GENERIC,\"del\",rl->key,rl->db->id);\n    }\n    /* We don't call signalModifiedKey() as it was already called\n     * when an element was pushed on the list. */\n}\n\n/* Helper function for handleClientsBlockedOnKeys(). This function is called\n * when there may be clients blocked on a sorted set key, and there may be new\n * data to fetch (the key is ready). */\nvoid serveClientsBlockedOnSortedSetKey(robj *o, readyList *rl) {\n    /* We serve clients in the same order they blocked for\n     * this key, from the first blocked to the last. */\n    dictEntry *de = dictFind(rl->db->blocking_keys,rl->key);\n    if (de) {\n        list *clients = (list*)dictGetVal(de);\n        int numclients = listLength(clients);\n        unsigned long zcard = zsetLength(o);\n\n        while(numclients-- && zcard) {\n            listNode *clientnode = listFirst(clients);\n            client *receiver = (client*)clientnode->value;\n            std::unique_lock<decltype(receiver->lock)> lock(receiver->lock);\n\n            if (receiver->btype != BLOCKED_ZSET) {\n                /* Put at the tail, so that at the next call\n                 * we'll not run into it again. */\n                listRotateHeadToTail(clients);\n                continue;\n            }\n\n            int where = (receiver->lastcmd &&\n                         receiver->lastcmd->proc == bzpopminCommand)\n                         ? ZSET_MIN : ZSET_MAX;\n            monotime replyTimer;\n            elapsedStart(&replyTimer);\n            genericZpopCommand(receiver,&rl->key,1,where,1,NULL);\n            updateStatsOnUnblock(receiver, 0, elapsedUs(replyTimer));\n            unblockClient(receiver);\n            zcard--;\n\n            /* Replicate the command. */\n            robj *argv[2];\n            struct redisCommand *cmd = where == ZSET_MIN ?\n                                       cserver.zpopminCommand :\n                                       cserver.zpopmaxCommand;\n            argv[0] = createStringObject(cmd->name,strlen(cmd->name));\n            argv[1] = rl->key;\n            incrRefCount(rl->key);\n            propagate(cmd,receiver->db->id,\n                      argv,2,PROPAGATE_AOF|PROPAGATE_REPL);\n            decrRefCount(argv[0]);\n            decrRefCount(argv[1]);\n        }\n    }\n}\n\n/* Helper function for handleClientsBlockedOnKeys(). This function is called\n * when there may be clients blocked on a stream key, and there may be new\n * data to fetch (the key is ready). */\nvoid serveClientsBlockedOnStreamKey(robj *o, readyList *rl) {\n    dictEntry *de = dictFind(rl->db->blocking_keys,rl->key);\n    stream *s = (stream*)ptrFromObj(o);\n\n    /* We need to provide the new data arrived on the stream\n     * to all the clients that are waiting for an offset smaller\n     * than the current top item. */\n    if (de) {\n        list *clients = (list*)dictGetVal(de);\n        listNode *ln;\n        listIter li;\n        listRewind(clients,&li);\n\n        while((ln = listNext(&li))) {\n            client *receiver = (client*)listNodeValue(ln);\n            if (receiver->btype != BLOCKED_STREAM) continue;\n            std::unique_lock<decltype(receiver->lock)> lock(receiver->lock);\n            bkinfo *bki = (bkinfo*)dictFetchValue(receiver->bpop.keys,rl->key);\n            streamID *gt = &bki->stream_id;\n\n            /* If we blocked in the context of a consumer\n             * group, we need to resolve the group and update the\n             * last ID the client is blocked for: this is needed\n             * because serving other clients in the same consumer\n             * group will alter the \"last ID\" of the consumer\n             * group, and clients blocked in a consumer group are\n             * always blocked for the \">\" ID: we need to deliver\n             * only new messages and avoid unblocking the client\n             * otherwise. */\n            streamCG *group = NULL;\n            if (receiver->bpop.xread_group) {\n                group = streamLookupCG(s,\n                        szFromObj(receiver->bpop.xread_group));\n                /* If the group was not found, send an error\n                 * to the consumer. */\n                if (!group) {\n                    addReplyError(receiver,\n                        \"-NOGROUP the consumer group this client \"\n                        \"was blocked on no longer exists\");\n                    unblockClient(receiver);\n                    continue;\n                } else {\n                    *gt = group->last_id;\n                }\n            }\n\n            if (streamCompareID(&s->last_id, gt) > 0) {\n                streamID start = *gt;\n                streamIncrID(&start);\n\n                /* Lookup the consumer for the group, if any. */\n                streamConsumer *consumer = NULL;\n                int noack = 0;\n\n                if (group) {\n                    int created = 0;\n                    consumer =\n                        streamLookupConsumer(group,\n                                             szFromObj(receiver->bpop.xread_consumer),\n                                             SLC_NONE,\n                                             &created);\n                    noack = receiver->bpop.xread_group_noack;\n                    if (created && noack) {\n                        streamPropagateConsumerCreation(receiver,rl->key,\n                                                        receiver->bpop.xread_group,\n                                                        consumer->name);\n                    }\n                }\n\n                monotime replyTimer;\n                elapsedStart(&replyTimer);\n                /* Emit the two elements sub-array consisting of\n                 * the name of the stream and the data we\n                 * extracted from it. Wrapped in a single-item\n                 * array, since we have just one key. */\n                if (receiver->resp == 2) {\n                    addReplyArrayLen(receiver,1);\n                    addReplyArrayLen(receiver,2);\n                } else {\n                    addReplyMapLen(receiver,1);\n                }\n                addReplyBulk(receiver,rl->key);\n\n                streamPropInfo pi = {\n                    rl->key,\n                    receiver->bpop.xread_group\n                };\n                streamReplyWithRange(receiver,s,&start,NULL,\n                                     receiver->bpop.xread_count,\n                                     0, group, consumer, noack, &pi);\n                updateStatsOnUnblock(receiver, 0, elapsedUs(replyTimer));\n\n                /* Note that after we unblock the client, 'gt'\n                 * and other receiver->bpop stuff are no longer\n                 * valid, so we must do the setup above before\n                 * this call. */\n                unblockClient(receiver);\n            }\n        }\n    }\n}\n\n/* Helper function for handleClientsBlockedOnKeys(). This function is called\n * in order to check if we can serve clients blocked by modules using\n * RM_BlockClientOnKeys(), when the corresponding key was signaled as ready:\n * our goal here is to call the RedisModuleBlockedClient reply() callback to\n * see if the key is really able to serve the client, and in that case,\n * unblock it. */\nvoid serveClientsBlockedOnKeyByModule(readyList *rl) {\n    dictEntry *de;\n\n    /* Optimization: If no clients are in type BLOCKED_MODULE,\n     * we can skip this loop. */\n    if (!g_pserver->blocked_clients_by_type[BLOCKED_MODULE]) return;\n\n    /* We serve clients in the same order they blocked for\n     * this key, from the first blocked to the last. */\n    de = dictFind(rl->db->blocking_keys,rl->key);\n    if (de) {\n        list *clients = (list*)dictGetVal(de);\n        int numclients = listLength(clients);\n\n        while(numclients--) {\n            listNode *clientnode = listFirst(clients);\n            client *receiver = (client*)clientnode->value;\n\n            /* Put at the tail, so that at the next call\n             * we'll not run into it again: clients here may not be\n             * ready to be served, so they'll remain in the list\n             * sometimes. We want also be able to skip clients that are\n             * not blocked for the MODULE type safely. */\n            listRotateHeadToTail(clients);\n\n            if (receiver->btype != BLOCKED_MODULE) continue;\n\n            /* Note that if *this* client cannot be served by this key,\n             * it does not mean that another client that is next into the\n             * list cannot be served as well: they may be blocked by\n             * different modules with different triggers to consider if a key\n             * is ready or not. This means we can't exit the loop but need\n             * to continue after the first failure. */\n            monotime replyTimer;\n            elapsedStart(&replyTimer);\n            if (!moduleTryServeClientBlockedOnKey(receiver, rl->key)) continue;\n            updateStatsOnUnblock(receiver, 0, elapsedUs(replyTimer));\n\n            moduleUnblockClient(receiver);\n        }\n    }\n}\n\n/* This function should be called by Redis every time a single command,\n * a MULTI/EXEC block, or a Lua script, terminated its execution after\n * being called by a client. It handles serving clients blocked in\n * lists, streams, and sorted sets, via a blocking commands.\n *\n * All the keys with at least one client blocked that received at least\n * one new element via some write operation are accumulated into\n * the g_pserver->ready_keys list. This function will run the list and will\n * serve clients accordingly. Note that the function will iterate again and\n * again as a result of serving BLMOVE we can have new blocking clients\n * to serve because of the PUSH side of BLMOVE.\n *\n * This function is normally \"fair\", that is, it will server clients\n * using a FIFO behavior. However this fairness is violated in certain\n * edge cases, that is, when we have clients blocked at the same time\n * in a sorted set and in a list, for the same key (a very odd thing to\n * do client side, indeed!). Because mismatching clients (blocking for\n * a different type compared to the current key type) are moved in the\n * other side of the linked list. However as long as the key starts to\n * be used only for a single type, like virtually any Redis application will\n * do, the function is already fair. */\nvoid handleClientsBlockedOnKeys(void) {\n    serverAssert(GlobalLocksAcquired());\n    while(listLength(g_pserver->ready_keys) != 0) {\n        list *l;\n\n        /* Point g_pserver->ready_keys to a fresh list and save the current one\n         * locally. This way as we run the old list we are free to call\n         * signalKeyAsReady() that may push new elements in g_pserver->ready_keys\n         * when handling clients blocked into BRPOPLPUSH. */\n        l = g_pserver->ready_keys;\n        g_pserver->ready_keys = listCreate();\n\n        while(listLength(l) != 0) {\n            listNode *ln = listFirst(l);\n            readyList *rl = (readyList*)ln->value;\n\n            /* First of all remove this key from db->ready_keys so that\n             * we can safely call signalKeyAsReady() against this key. */\n            dictDelete(rl->db->ready_keys,rl->key);\n\n            /* Even if we are not inside call(), increment the call depth\n             * in order to make sure that keys are expired against a fixed\n             * reference time, and not against the wallclock time. This\n             * way we can lookup an object multiple times (BLMOVE does\n             * that) without the risk of it being freed in the second\n             * lookup, invalidating the first one.\n             * See https://github.com/redis/redis/pull/6554. */\n            serverTL->fixed_time_expire++;\n\n            /* Serve clients blocked on the key. */\n            robj *o = lookupKeyWrite(rl->db,rl->key);\n\n            if (o != NULL) {\n                if (o->type == OBJ_LIST)\n                    serveClientsBlockedOnListKey(o,rl);\n                else if (o->type == OBJ_ZSET)\n                    serveClientsBlockedOnSortedSetKey(o,rl);\n                else if (o->type == OBJ_STREAM)\n                    serveClientsBlockedOnStreamKey(o,rl);\n                /* We want to serve clients blocked on module keys\n                 * regardless of the object type: we don't know what the\n                 * module is trying to accomplish right now. */\n                serveClientsBlockedOnKeyByModule(rl);\n            }\n            serverTL->fixed_time_expire--;\n\n            /* Free this item. */\n            decrRefCount(rl->key);\n            zfree(rl);\n            listDelNode(l,ln);\n        }\n        listRelease(l); /* We have the new list on place at this point. */\n    }\n}\n\n/* This is how the current blocking lists/sorted sets/streams work, we use\n * BLPOP as example, but the concept is the same for other list ops, sorted\n * sets and XREAD.\n * - If the user calls BLPOP and the key exists and contains a non empty list\n *   then LPOP is called instead. So BLPOP is semantically the same as LPOP\n *   if blocking is not required.\n * - If instead BLPOP is called and the key does not exists or the list is\n *   empty we need to block. In order to do so we remove the notification for\n *   new data to read in the client socket (so that we'll not serve new\n *   requests if the blocking request is not served). Also we put the client\n *   in a dictionary (db->blocking_keys) mapping keys to a list of clients\n *   blocking for this keys.\n * - If a PUSH operation against a key with blocked clients waiting is\n *   performed, we mark this key as \"ready\", and after the current command,\n *   MULTI/EXEC block, or script, is executed, we serve all the clients waiting\n *   for this list, from the one that blocked first, to the last, accordingly\n *   to the number of elements we have in the ready list.\n */\n\n/* Set a client in blocking mode for the specified key (list, zset or stream),\n * with the specified timeout. The 'type' argument is BLOCKED_LIST,\n * BLOCKED_ZSET or BLOCKED_STREAM depending on the kind of operation we are\n * waiting for an empty key in order to awake the client. The client is blocked\n * for all the 'numkeys' keys as in the 'keys' argument. When we block for\n * stream keys, we also provide an array of streamID structures: clients will\n * be unblocked only when items with an ID greater or equal to the specified\n * one is appended to the stream. */\nvoid blockForKeys(client *c, int btype, robj **keys, int numkeys, mstime_t timeout, robj *target, struct listPos *listpos, streamID *ids) {\n    dictEntry *de;\n    list *l;\n    int j;\n\n    c->bpop.timeout = timeout;\n    c->bpop.target = target;\n\n    if (listpos != NULL) c->bpop.listpos = *listpos;\n\n    if (target != NULL) incrRefCount(target);\n\n    for (j = 0; j < numkeys; j++) {\n        /* Allocate our bkinfo structure, associated to each key the client\n         * is blocked for. */\n        bkinfo *bki = (bkinfo*)zmalloc(sizeof(*bki));\n        if (btype == BLOCKED_STREAM)\n            bki->stream_id = ids[j];\n\n        /* If the key already exists in the dictionary ignore it. */\n        if (dictAdd(c->bpop.keys,keys[j],bki) != DICT_OK) {\n            zfree(bki);\n            continue;\n        }\n        incrRefCount(keys[j]);\n\n        /* And in the other \"side\", to map keys -> clients */\n        de = dictFind(c->db->blocking_keys,keys[j]);\n        if (de == NULL) {\n            int retval;\n\n            /* For every key we take a list of clients blocked for it */\n            l = listCreate();\n            retval = dictAdd(c->db->blocking_keys,keys[j],l);\n            incrRefCount(keys[j]);\n            serverAssertWithInfo(c,keys[j],retval == DICT_OK);\n        } else {\n            l = (list*)dictGetVal(de);\n        }\n        listAddNodeTail(l,c);\n        bki->listnode = listLast(l);\n    }\n    blockClient(c,btype);\n}\n\n/* Unblock a client that's waiting in a blocking operation such as BLPOP.\n * You should never call this function directly, but unblockClient() instead. */\nvoid unblockClientWaitingData(client *c) {\n    dictEntry *de;\n    dictIterator *di;\n    list *l;\n\n    serverAssertWithInfo(c,NULL,dictSize(c->bpop.keys) != 0);\n    di = dictGetIterator(c->bpop.keys);\n    /* The client may wait for multiple keys, so unblock it for every key. */\n    while((de = dictNext(di)) != NULL) {\n        robj *key = (robj*)dictGetKey(de);\n        bkinfo *bki = (bkinfo*)dictGetVal(de);\n\n        /* Remove this client from the list of clients waiting for this key. */\n        l = (list*)dictFetchValue(c->db->blocking_keys,key);\n        serverAssertWithInfo(c,key,l != NULL);\n        listDelNode(l,bki->listnode);\n        /* If the list is empty we need to remove it to avoid wasting memory */\n        if (listLength(l) == 0)\n            dictDelete(c->db->blocking_keys,key);\n    }\n    dictReleaseIterator(di);\n\n    /* Cleanup the client structure */\n    dictEmpty(c->bpop.keys,NULL);\n    if (c->bpop.target) {\n        decrRefCount(c->bpop.target);\n        c->bpop.target = NULL;\n    }\n    if (c->bpop.xread_group) {\n        decrRefCount(c->bpop.xread_group);\n        decrRefCount(c->bpop.xread_consumer);\n        c->bpop.xread_group = NULL;\n        c->bpop.xread_consumer = NULL;\n    }\n    c->bpop.timeout = 0;\n}\n\nstatic int getBlockedTypeByType(int type) {\n    switch (type) {\n        case OBJ_LIST: return BLOCKED_LIST;\n        case OBJ_ZSET: return BLOCKED_ZSET;\n        case OBJ_MODULE: return BLOCKED_MODULE;\n        case OBJ_STREAM: return BLOCKED_STREAM;\n        default: return BLOCKED_NONE;\n    }\n}\n\n/* If the specified key has clients blocked waiting for list pushes, this\n * function will put the key reference into the g_pserver->ready_keys list.\n * Note that db->ready_keys is a hash table that allows us to avoid putting\n * the same key again and again in the list in case of multiple pushes\n * made by a script or in the context of MULTI/EXEC.\n *\n * The list will be finally processed by handleClientsBlockedOnKeys() */\nvoid signalKeyAsReady(redisDb *db, robj *key, int type) {\n    readyList *rl;\n\n    /* Quick returns. */\n    int btype = getBlockedTypeByType(type);\n    if (btype == BLOCKED_NONE) {\n        /* The type can never block. */\n        return;\n    }\n    if (!g_pserver->blocked_clients_by_type[btype] &&\n        !g_pserver->blocked_clients_by_type[BLOCKED_MODULE]) {\n        /* No clients block on this type. Note: Blocked modules are represented\n         * by BLOCKED_MODULE, even if the intention is to wake up by normal\n         * types (list, zset, stream), so we need to check that there are no\n         * blocked modules before we do a quick return here. */\n        return;\n    }\n\n    /* No clients blocking for this key? No need to queue it. */\n    if (dictFind(db->blocking_keys,key) == NULL) return;\n\n    /* Key was already signaled? No need to queue it again. */\n    if (dictFind(db->ready_keys,key) != NULL) return;\n\n    if (key->getrefcount() == OBJ_STATIC_REFCOUNT) {\n        // Sometimes a key may be stack allocated, we'll need to dupe it\n        robj *newKey = createStringObject(szFromObj(key), sdslen(szFromObj(key)));\n        newKey->setrefcount(0); // Start with 0 but don't free\n        key = newKey;\n    }\n\n    /* Ok, we need to queue this key into g_pserver->ready_keys. */\n    rl = (readyList*)zmalloc(sizeof(*rl), MALLOC_SHARED);\n    rl->key = key;\n    rl->db = db;\n    incrRefCount(key);\n    listAddNodeTail(g_pserver->ready_keys,rl);\n\n    /* We also add the key in the db->ready_keys dictionary in order\n     * to avoid adding it multiple times into a list with a simple O(1)\n     * check. */\n    incrRefCount(key);\n    serverAssert(dictAdd(db->ready_keys,key,NULL) == DICT_OK);\n}\n\nvoid signalKeyAsReady(redisDb *db, sds key, int type) {\n    redisObjectStack o;\n    initStaticStringObject(o, key);\n    signalKeyAsReady(db, &o, type);\n}\n"
  },
  {
    "path": "src/childinfo.cpp",
    "content": "/*\n * Copyright (c) 2016, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n#include <unistd.h>\n\ntypedef struct {\n    size_t keys;\n    size_t cow;\n    monotime cow_updated;\n    double progress;\n    childInfoType information_type; /* Type of information */\n} child_info_data;\n\n/* Open a child-parent channel used in order to move information about the\n * RDB / AOF saving process from the child to the parent (for instance\n * the amount of copy on write memory used) */\nvoid openChildInfoPipe(void) {\n    serverAssert(g_pserver->child_info_pipe[0] == -1);\n    if (pipe(g_pserver->child_info_pipe) == -1) {\n        /* On error our two file descriptors should be still set to -1,\n         * but we call anyway closeChildInfoPipe() since can't hurt. */\n        closeChildInfoPipe();\n    } else if (anetNonBlock(NULL,g_pserver->child_info_pipe[0]) != ANET_OK) {\n        closeChildInfoPipe();\n    } else {\n        g_pserver->child_info_nread = 0;\n    }\n}\n\n/* Close the pipes opened with openChildInfoPipe(). */\nvoid closeChildInfoPipe(void) {\n    if (g_pserver->child_info_pipe[0] != -1 ||\n        g_pserver->child_info_pipe[1] != -1)\n    {\n        close(g_pserver->child_info_pipe[0]);\n        close(g_pserver->child_info_pipe[1]);\n        g_pserver->child_info_pipe[0] = -1;\n        g_pserver->child_info_pipe[1] = -1;\n        g_pserver->child_info_nread = 0;\n    }\n}\n\n/* Send save data to parent. */\nvoid sendChildInfoGeneric(childInfoType info_type, size_t keys, double progress, const char *pname) {\n    if (g_pserver->child_info_pipe[1] == -1) return;\n    if (g_pserver->rdbThreadVars.fRdbThreadActive && g_pserver->rdbThreadVars.fRdbThreadCancel) return;\n\n    static monotime cow_updated = 0;\n    static uint64_t cow_update_cost = 0;\n    static size_t cow = 0;\n\n    child_info_data data = {0}; /* zero everything, including padding to satisfy valgrind */\n\n    /* When called to report current info, we need to throttle down CoW updates as they\n     * can be very expensive. To do that, we measure the time it takes to get a reading\n     * and schedule the next reading to happen not before time*CHILD_COW_COST_FACTOR\n     * passes. */\n\n    monotime now = getMonotonicUs();\n    if (info_type != CHILD_INFO_TYPE_CURRENT_INFO ||\n        !cow_updated ||\n        now - cow_updated > cow_update_cost * CHILD_COW_DUTY_CYCLE)\n    {\n        cow = zmalloc_get_private_dirty(-1);\n        cow_updated = getMonotonicUs();\n        cow_update_cost = cow_updated - now;\n\n        if (cow) {\n            serverLog((info_type == CHILD_INFO_TYPE_CURRENT_INFO) ? LL_VERBOSE : LL_NOTICE,\n                      \"%s: %zu MB of memory used by copy-on-write\",\n                      pname, cow / (1024 * 1024));\n        }\n    }\n\n    data.information_type = info_type;\n    data.keys = keys;\n    data.cow = cow;\n    data.cow_updated = cow_updated;\n    data.progress = progress;\n\n    ssize_t wlen = sizeof(data);\n\n    if (write(g_pserver->child_info_pipe[1], &data, wlen) != wlen) {\n        /* Nothing to do on error, this will be detected by the other side. */\n    }\n}\n\n/* Update Child info. */\nvoid updateChildInfo(childInfoType information_type, size_t cow, monotime cow_updated, size_t keys, double progress) {\n    if (information_type == CHILD_INFO_TYPE_CURRENT_INFO) {\n        g_pserver->stat_current_cow_bytes = cow;\n        g_pserver->stat_current_cow_updated = cow_updated;\n        g_pserver->stat_current_save_keys_processed = keys;\n        if (progress != -1) g_pserver->stat_module_progress = progress;\n    } else if (information_type == CHILD_INFO_TYPE_AOF_COW_SIZE) {\n        g_pserver->stat_aof_cow_bytes = cow;\n    } else if (information_type == CHILD_INFO_TYPE_RDB_COW_SIZE) {\n        g_pserver->stat_rdb_cow_bytes = cow;\n    } else if (information_type == CHILD_INFO_TYPE_MODULE_COW_SIZE) {\n        g_pserver->stat_module_cow_bytes = cow;\n    }\n}\n\n/* Read child info data from the pipe.\n * if complete data read into the buffer, \n * data is stored into *buffer, and returns 1.\n * otherwise, the partial data is left in the buffer, waiting for the next read, and returns 0. */\nint readChildInfo(childInfoType *information_type, size_t *cow, monotime *cow_updated, size_t *keys, double* progress) {\n    /* We are using here a static buffer in combination with the server.child_info_nread to handle short reads */\n    static child_info_data buffer;\n    ssize_t wlen = sizeof(buffer);\n\n    /* Do not overlap */\n    if (g_pserver->child_info_nread == wlen) g_pserver->child_info_nread = 0;\n\n    int nread = read(g_pserver->child_info_pipe[0], (char *)&buffer + g_pserver->child_info_nread, wlen - g_pserver->child_info_nread);\n    if (nread > 0) {\n        g_pserver->child_info_nread += nread;\n    }\n\n    /* We have complete child info */\n    if (g_pserver->child_info_nread == wlen) {\n        *information_type = buffer.information_type;\n        *cow = buffer.cow;\n        *cow_updated = buffer.cow_updated;\n        *keys = buffer.keys;\n        *progress = buffer.progress;\n        return 1;\n    } else {\n        return 0;\n    }\n}\n\n/* Receive info data from child. */\nvoid receiveChildInfo(void) {\n    if (g_pserver->child_info_pipe[0] == -1) return;\n\n    size_t cow;\n    monotime cow_updated;\n    size_t keys;\n    double progress;\n    childInfoType information_type;\n\n    /* Drain the pipe and update child info so that we get the final message. */\n    while (readChildInfo(&information_type, &cow, &cow_updated, &keys, &progress)) {\n        updateChildInfo(information_type, cow, cow_updated, keys, progress);\n    }\n}\n"
  },
  {
    "path": "src/cli_common.c",
    "content": "/* CLI (command line interface) common methods\n * \n * Copyright (c) 2020, Redis Labs\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"cli_common.h\"\n#include <errno.h>\n#include <hiredis.h>\n#include \"sdscompat.h\" /* Use hiredis' sds compat header that maps sds calls to their hi_ variants */\n#include <sds.h> /* use sds.h from hiredis, so that only one set of sds functions will be present in the binary */\n#ifdef USE_OPENSSL\n#include <openssl/ssl.h>\n#include <openssl/err.h>\n#include <hiredis_ssl.h>\n#endif\n\n\n/* Wrapper around redisSecureConnection to avoid hiredis_ssl dependencies if\n * not building with TLS support.\n */\nint cliSecureConnection(redisContext *c, cliSSLconfig config, const char **err) {\n#ifdef USE_OPENSSL\n    static SSL_CTX *ssl_ctx = NULL;\n\n    if (!ssl_ctx) {\n        ssl_ctx = SSL_CTX_new(SSLv23_client_method());\n        if (!ssl_ctx) {\n            *err = \"Failed to create SSL_CTX\";\n            goto error;\n        }\n        SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);\n        SSL_CTX_set_verify(ssl_ctx, config.skip_cert_verify ? SSL_VERIFY_NONE : SSL_VERIFY_PEER, NULL);\n\n        if (config.cacert || config.cacertdir) {\n            if (!SSL_CTX_load_verify_locations(ssl_ctx, config.cacert, config.cacertdir)) {\n                *err = \"Invalid CA Certificate File/Directory\";\n                goto error;\n            }\n        } else {\n            if (!SSL_CTX_set_default_verify_paths(ssl_ctx)) {\n                *err = \"Failed to use default CA paths\";\n                goto error;\n            }\n        }\n\n        if (config.cert && !SSL_CTX_use_certificate_chain_file(ssl_ctx, config.cert)) {\n            *err = \"Invalid client certificate\";\n            goto error;\n        }\n\n        if (config.key && !SSL_CTX_use_PrivateKey_file(ssl_ctx, config.key, SSL_FILETYPE_PEM)) {\n            *err = \"Invalid private key\";\n            goto error;\n        }\n        if (config.ciphers && !SSL_CTX_set_cipher_list(ssl_ctx, config.ciphers)) {\n            *err = \"Error while configuring ciphers\";\n            goto error;\n        }\n#ifdef TLS1_3_VERSION\n        if (config.ciphersuites && !SSL_CTX_set_ciphersuites(ssl_ctx, config.ciphersuites)) {\n            *err = \"Error while setting cypher suites\";\n            goto error;\n        }\n#endif\n    }\n\n    SSL *ssl = SSL_new(ssl_ctx);\n    if (!ssl) {\n        *err = \"Failed to create SSL object\";\n        return REDIS_ERR;\n    }\n\n    if (config.sni && !SSL_set_tlsext_host_name(ssl, config.sni)) {\n        *err = \"Failed to configure SNI\";\n        SSL_free(ssl);\n        return REDIS_ERR;\n    }\n\n    return redisInitiateSSL(c, ssl);\n\nerror:\n    SSL_CTX_free(ssl_ctx);\n    ssl_ctx = NULL;\n    return REDIS_ERR;\n#else\n    (void) config;\n    (void) c;\n    (void) err;\n    return REDIS_OK;\n#endif\n}\n\n/* Wrapper around hiredis to allow arbitrary reads and writes.\n *\n * We piggybacks on top of hiredis to achieve transparent TLS support,\n * and use its internal buffers so it can co-exist with commands\n * previously/later issued on the connection.\n *\n * Interface is close to enough to read()/write() so things should mostly\n * work transparently.\n */\n\n/* Write a raw buffer through a redisContext. If we already have something\n * in the buffer (leftovers from hiredis operations) it will be written\n * as well.\n */\nssize_t cliWriteConn(redisContext *c, const char *buf, size_t buf_len)\n{\n    int done = 0;\n\n    /* Append data to buffer which is *usually* expected to be empty\n     * but we don't assume that, and write.\n     */\n    c->obuf = sdscatlen(c->obuf, buf, buf_len);\n    if (redisBufferWrite(c, &done) == REDIS_ERR) {\n        if (!(c->flags & REDIS_BLOCK))\n            errno = EAGAIN;\n\n        /* On error, we assume nothing was written and we roll back the\n         * buffer to its original state.\n         */\n        if (sdslen(c->obuf) > buf_len)\n            sdsrange(c->obuf, 0, -(buf_len+1));\n        else\n            sdsclear(c->obuf);\n\n        return -1;\n    }\n\n    /* If we're done, free up everything. We may have written more than\n     * buf_len (if c->obuf was not initially empty) but we don't have to\n     * tell.\n     */\n    if (done) {\n        sdsclear(c->obuf);\n        return buf_len;\n    }\n\n    /* Write was successful but we have some leftovers which we should\n     * remove from the buffer.\n     *\n     * Do we still have data that was there prior to our buf? If so,\n     * restore buffer to it's original state and report no new data was\n     * writen.\n     */\n    if (sdslen(c->obuf) > buf_len) {\n        sdsrange(c->obuf, 0, -(buf_len+1));\n        return 0;\n    }\n\n    /* At this point we're sure no prior data is left. We flush the buffer\n     * and report how much we've written.\n     */\n    size_t left = sdslen(c->obuf);\n    sdsclear(c->obuf);\n    return buf_len - left;\n}\n\n/* Wrapper around OpenSSL (libssl and libcrypto) initialisation\n */\nint cliSecureInit()\n{\n#ifdef USE_OPENSSL\n    ERR_load_crypto_strings();\n    SSL_load_error_strings();\n    SSL_library_init();\n#endif\n    return REDIS_OK;\n}\n"
  },
  {
    "path": "src/cli_common.h",
    "content": "#ifndef __CLICOMMON_H\n#define __CLICOMMON_H\n\n#include <hiredis.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\ntypedef struct cliSSLconfig {\n    /* Requested SNI, or NULL */\n    char *sni;\n    /* CA Certificate file, or NULL */\n    char *cacert;\n    /* Directory where trusted CA certificates are stored, or NULL */\n    char *cacertdir;\n    /* Skip server certificate verification. */\n    int skip_cert_verify;\n    /* Client certificate to authenticate with, or NULL */\n    char *cert;\n    /* Private key file to authenticate with, or NULL */\n    char *key;\n    /* Prefered cipher list, or NULL (applies only to <= TLSv1.2) */\n    char* ciphers;\n    /* Prefered ciphersuites list, or NULL (applies only to TLSv1.3) */\n    char* ciphersuites;\n} cliSSLconfig;\n\n/* Wrapper around redisSecureConnection to avoid hiredis_ssl dependencies if\n * not building with TLS support.\n */\nint cliSecureConnection(redisContext *c, cliSSLconfig config, const char **err);\n\n/* Wrapper around hiredis to allow arbitrary reads and writes.\n *\n * We piggybacks on top of hiredis to achieve transparent TLS support,\n * and use its internal buffers so it can co-exist with commands\n * previously/later issued on the connection.\n *\n * Interface is close to enough to read()/write() so things should mostly\n * work transparently.\n */\n\n/* Write a raw buffer through a redisContext. If we already have something\n * in the buffer (leftovers from hiredis operations) it will be written\n * as well.\n */\nssize_t cliWriteConn(redisContext *c, const char *buf, size_t buf_len);\n\n/* Wrapper around OpenSSL (libssl and libcrypto) initialisation.\n */\nint cliSecureInit();\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif /* __CLICOMMON_H */\n"
  },
  {
    "path": "src/cluster.cpp",
    "content": "/* Redis Cluster implementation.\n *\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n#include \"cluster.h\"\n#include \"endianconv.h\"\n\n#include <sys/types.h>\n#include <sys/socket.h>\n#include <arpa/inet.h>\n#include <fcntl.h>\n#include <unistd.h>\n#include <sys/stat.h>\n#include <sys/file.h>\n#include <math.h>\n\n/* A global reference to myself is handy to make code more clear.\n * Myself always points to g_pserver->cluster->myself, that is, the clusterNode\n * that represents this node. */\nclusterNode *myself = NULL;\n\nclusterNode *createClusterNode(char *nodename, int flags);\nvoid clusterAddNode(clusterNode *node);\nvoid clusterAcceptHandler(aeEventLoop *el, int fd, void *privdata, int mask);\nvoid clusterReadHandler(connection *conn);\nvoid clusterSendPing(clusterLink *link, int type);\nvoid clusterSendFail(char *nodename);\nvoid clusterSendFailoverAuthIfNeeded(clusterNode *node, clusterMsg *request);\nvoid clusterUpdateState(void);\nint clusterNodeGetSlotBit(clusterNode *n, int slot);\nsds clusterGenNodesDescription(int filter, int use_pport);\nclusterNode *clusterLookupNode(const char *name);\nint clusterNodeAddSlave(clusterNode *master, clusterNode *slave);\nint clusterAddSlot(clusterNode *n, int slot);\nint clusterDelSlot(int slot);\nint clusterDelNodeSlots(clusterNode *node);\nint clusterNodeSetSlotBit(clusterNode *n, int slot);\nvoid clusterSetMaster(clusterNode *n);\nvoid clusterHandleSlaveFailover(void);\nvoid clusterHandleSlaveMigration(int max_slaves);\nint bitmapTestBit(unsigned char *bitmap, int pos);\nvoid clusterDoBeforeSleep(int flags);\nvoid clusterSendUpdate(clusterLink *link, clusterNode *node);\nvoid resetManualFailover(void);\nvoid clusterCloseAllSlots(void);\nvoid clusterSetNodeAsMaster(clusterNode *n);\nvoid clusterDelNode(clusterNode *delnode);\nsds representClusterNodeFlags(sds ci, uint16_t flags);\nuint64_t clusterGetMaxEpoch(void);\nint clusterBumpConfigEpochWithoutConsensus(void);\nvoid moduleCallClusterReceivers(const char *sender_id, uint64_t module_id, uint8_t type, const unsigned char *payload, uint32_t len);\n\n#define RCVBUF_INIT_LEN 1024\n#define RCVBUF_MAX_PREALLOC (1<<20) /* 1MB */\n\nstruct redisMaster *getFirstMaster()\n{\n    serverAssert(listLength(g_pserver->masters) <= 1);\n    if (!listLength(g_pserver->masters))\n        return NULL;\n    return (redisMaster*)listFirst(g_pserver->masters)->value;\n}\n\n/* -----------------------------------------------------------------------------\n * Initialization\n * -------------------------------------------------------------------------- */\n\n/* Load the cluster config from 'filename'.\n *\n * If the file does not exist or is zero-length (this may happen because\n * when we lock the nodes.conf file, we create a zero-length one for the\n * sake of locking if it does not already exist), C_ERR is returned.\n * If the configuration was loaded from the file, C_OK is returned. */\nint clusterLoadConfig(char *filename) {\n    FILE *fp = fopen(filename,\"r\");\n    struct stat sb;\n    char *line;\n    int maxline, j;\n\n    if (fp == NULL) {\n        if (errno == ENOENT) {\n            return C_ERR;\n        } else {\n            serverLog(LL_WARNING,\n                \"Loading the cluster node config from %s: %s\",\n                filename, strerror(errno));\n            exit(1);\n        }\n    }\n\n    /* Check if the file is zero-length: if so return C_ERR to signal\n     * we have to write the config. */\n    if (fstat(fileno(fp),&sb) != -1 && sb.st_size == 0) {\n        fclose(fp);\n        return C_ERR;\n    }\n\n    /* Parse the file. Note that single lines of the cluster config file can\n     * be really long as they include all the hash slots of the node.\n     * This means in the worst possible case, half of the Redis slots will be\n     * present in a single line, possibly in importing or migrating state, so\n     * together with the node ID of the sender/receiver.\n     *\n     * To simplify we allocate 1024+CLUSTER_SLOTS*128 bytes per line. */\n    maxline = 1024+CLUSTER_SLOTS*128;\n    line = (char*)zmalloc(maxline, MALLOC_LOCAL);\n    while(fgets(line,maxline,fp) != NULL) {\n        int argc;\n        sds *argv;\n        clusterNode *n, *master;\n        char *p, *s;\n\n        /* Skip blank lines, they can be created either by users manually\n         * editing nodes.conf or by the config writing process if stopped\n         * before the truncate() call. */\n        if (line[0] == '\\n' || line[0] == '\\0') continue;\n\n        /* Split the line into arguments for processing. */\n        argv = sdssplitargs(line,&argc);\n        if (argv == NULL) goto fmterr;\n\n        /* Handle the special \"vars\" line. Don't pretend it is the last\n         * line even if it actually is when generated by Redis. */\n        if (strcasecmp(argv[0],\"vars\") == 0) {\n            if (!(argc % 2)) goto fmterr;\n            for (j = 1; j < argc; j += 2) {\n                if (strcasecmp(argv[j],\"currentEpoch\") == 0) {\n                    g_pserver->cluster->currentEpoch =\n                            strtoull(argv[j+1],NULL,10);\n                } else if (strcasecmp(argv[j],\"lastVoteEpoch\") == 0) {\n                    g_pserver->cluster->lastVoteEpoch =\n                            strtoull(argv[j+1],NULL,10);\n                } else {\n                    serverLog(LL_WARNING,\n                        \"Skipping unknown cluster config variable '%s'\",\n                        argv[j]);\n                }\n            }\n            sdsfreesplitres(argv,argc);\n            continue;\n        }\n\n        /* Regular config lines have at least eight fields */\n        if (argc < 8) {\n            sdsfreesplitres(argv,argc);\n            goto fmterr;\n        }\n\n        /* Create this node if it does not exist */\n        n = clusterLookupNode(argv[0]);\n        if (!n) {\n            n = createClusterNode(argv[0],0);\n            clusterAddNode(n);\n        }\n        /* Address and port */\n        if ((p = strrchr(argv[1],':')) == NULL) {\n            sdsfreesplitres(argv,argc);\n            goto fmterr;\n        }\n        *p = '\\0';\n        memcpy(n->ip,argv[1],strlen(argv[1])+1);\n        char *port = p+1;\n        char *busp = strchr(port,'@');\n        if (busp) {\n            *busp = '\\0';\n            busp++;\n        }\n        n->port = atoi(port);\n        /* In older versions of nodes.conf the \"@busport\" part is missing.\n         * In this case we set it to the default offset of 10000 from the\n         * base port. */\n        n->cport = busp ? atoi(busp) : n->port + CLUSTER_PORT_INCR;\n\n        /* The plaintext port for client in a TLS cluster (n->pport) is not\n         * stored in nodes.conf. It is received later over the bus protocol. */\n\n        /* Parse flags */\n        p = s = argv[2];\n        while(p) {\n            p = strchr(s,',');\n            if (p) *p = '\\0';\n            if (!strcasecmp(s,\"myself\")) {\n                serverAssert(g_pserver->cluster->myself == NULL);\n                myself = g_pserver->cluster->myself = n;\n                n->flags |= CLUSTER_NODE_MYSELF;\n            } else if (!strcasecmp(s,\"master\")) {\n                n->flags |= CLUSTER_NODE_MASTER;\n            } else if (!strcasecmp(s,\"slave\")) {\n                n->flags |= CLUSTER_NODE_SLAVE;\n            } else if (!strcasecmp(s,\"fail?\")) {\n                n->flags |= CLUSTER_NODE_PFAIL;\n            } else if (!strcasecmp(s,\"fail\")) {\n                n->flags |= CLUSTER_NODE_FAIL;\n                n->fail_time = mstime();\n            } else if (!strcasecmp(s,\"handshake\")) {\n                n->flags |= CLUSTER_NODE_HANDSHAKE;\n            } else if (!strcasecmp(s,\"noaddr\")) {\n                n->flags |= CLUSTER_NODE_NOADDR;\n            } else if (!strcasecmp(s,\"nofailover\")) {\n                n->flags |= CLUSTER_NODE_NOFAILOVER;\n            } else if (!strcasecmp(s,\"noflags\")) {\n                /* nothing to do */\n            } else {\n                serverPanic(\"Unknown flag in KeyDB cluster config file\");\n            }\n            if (p) s = p+1;\n        }\n\n        /* Get master if any. Set the master and populate master's\n         * slave list. */\n        if (argv[3][0] != '-') {\n            master = clusterLookupNode(argv[3]);\n            if (!master) {\n                master = createClusterNode(argv[3],0);\n                clusterAddNode(master);\n            }\n            n->slaveof = master;\n            clusterNodeAddSlave(master,n);\n        }\n\n        /* Set ping sent / pong received timestamps */\n        if (atoi(argv[4])) n->ping_sent = mstime();\n        if (atoi(argv[5])) n->pong_received = mstime();\n\n        /* Set configEpoch for this node. */\n        n->configEpoch = strtoull(argv[6],NULL,10);\n\n        /* Populate hash slots served by this instance. */\n        for (j = 8; j < argc; j++) {\n            int start, stop;\n\n            if (argv[j][0] == '[') {\n                /* Here we handle migrating / importing slots */\n                int slot;\n                char direction;\n                clusterNode *cn;\n\n                p = strchr(argv[j],'-');\n                serverAssert(p != NULL);\n                *p = '\\0';\n                direction = p[1]; /* Either '>' or '<' */\n                slot = atoi(argv[j]+1);\n                if (slot < 0 || slot >= CLUSTER_SLOTS) {\n                    sdsfreesplitres(argv,argc);\n                    goto fmterr;\n                }\n                p += 3;\n                cn = clusterLookupNode(p);\n                if (!cn) {\n                    cn = createClusterNode(p,0);\n                    clusterAddNode(cn);\n                }\n                if (direction == '>') {\n                    g_pserver->cluster->migrating_slots_to[slot] = cn;\n                } else {\n                    g_pserver->cluster->importing_slots_from[slot] = cn;\n                }\n                continue;\n            } else if ((p = strchr(argv[j],'-')) != NULL) {\n                *p = '\\0';\n                start = atoi(argv[j]);\n                stop = atoi(p+1);\n            } else {\n                start = stop = atoi(argv[j]);\n            }\n            if (start < 0 || start >= CLUSTER_SLOTS ||\n                stop < 0 || stop >= CLUSTER_SLOTS)\n            {\n                sdsfreesplitres(argv,argc);\n                goto fmterr;\n            }\n            while(start <= stop) clusterAddSlot(n, start++);\n        }\n\n        sdsfreesplitres(argv,argc);\n    }\n    /* Config sanity check */\n    if (g_pserver->cluster->myself == NULL) goto fmterr;\n\n    zfree(line);\n    fclose(fp);\n\n    serverLog(LL_NOTICE,\"Node configuration loaded, I'm %.40s\", myself->name);\n\n    /* Something that should never happen: currentEpoch smaller than\n     * the max epoch found in the nodes configuration. However we handle this\n     * as some form of protection against manual editing of critical files. */\n    if (clusterGetMaxEpoch() > g_pserver->cluster->currentEpoch) {\n        g_pserver->cluster->currentEpoch = clusterGetMaxEpoch();\n    }\n\n    if (dictSize(g_pserver->cluster->nodes) > 1 && cserver.thread_min_client_threshold < 100)\n    {\n        // Because we expect the individual load of a client to be much less in a cluster (it will spread over multiple server)\n        //  we can increase the grouping of clients on a single thread within reason\n        cserver.thread_min_client_threshold *= dictSize(g_pserver->cluster->nodes);\n        cserver.thread_min_client_threshold = std::min(cserver.thread_min_client_threshold, 200);\n        serverLog(LL_NOTICE, \"Expanding min-clients-per-thread to %d due to cluster\", cserver.thread_min_client_threshold);\n    }\n    return C_OK;\n\nfmterr:\n    serverLog(LL_WARNING,\n        \"Unrecoverable error: corrupted cluster config file.\");\n    zfree(line);\n    if (fp) fclose(fp);\n    exit(1);\n}\n\n/* Cluster node configuration is exactly the same as CLUSTER NODES output.\n *\n * This function writes the node config and returns 0, on error -1\n * is returned.\n *\n * Note: we need to write the file in an atomic way from the point of view\n * of the POSIX filesystem semantics, so that if the server is stopped\n * or crashes during the write, we'll end with either the old file or the\n * new one. Since we have the full payload to write available we can use\n * a single write to write the whole file. If the pre-existing file was\n * bigger we pad our payload with newlines that are anyway ignored and truncate\n * the file afterward. */\nint clusterSaveConfig(int do_fsync) {\n    sds ci;\n    size_t content_size;\n    struct stat sb;\n    int fd;\n\n    g_pserver->cluster->todo_before_sleep &= ~CLUSTER_TODO_SAVE_CONFIG;\n\n    /* Get the nodes description and concatenate our \"vars\" directive to\n     * save currentEpoch and lastVoteEpoch. */\n    ci = clusterGenNodesDescription(CLUSTER_NODE_HANDSHAKE, 0);\n    ci = sdscatprintf(ci,\"vars currentEpoch %llu lastVoteEpoch %llu\\n\",\n        (unsigned long long) g_pserver->cluster->currentEpoch,\n        (unsigned long long) g_pserver->cluster->lastVoteEpoch);\n    content_size = sdslen(ci);\n\n    if ((fd = open(g_pserver->cluster_configfile,O_WRONLY|O_CREAT,0644))\n        == -1) goto err;\n\n    /* Pad the new payload if the existing file length is greater. */\n    if (fstat(fd,&sb) != -1) {\n        if (sb.st_size > (off_t)content_size) {\n            ci = sdsgrowzero(ci,sb.st_size);\n            memset(ci+content_size,'\\n',sb.st_size-content_size);\n        }\n    }\n    if (write(fd,ci,sdslen(ci)) != (ssize_t)sdslen(ci)) goto err;\n    if (do_fsync) {\n        g_pserver->cluster->todo_before_sleep &= ~CLUSTER_TODO_FSYNC_CONFIG;\n        if (fsync(fd) == -1) goto err;\n    }\n\n    /* Truncate the file if needed to remove the final \\n padding that\n     * is just garbage. */\n    if (content_size != sdslen(ci) && ftruncate(fd,content_size) == -1) {\n        /* ftruncate() failing is not a critical error. */\n    }\n    close(fd);\n    sdsfree(ci);\n    return 0;\n\nerr:\n    if (fd != -1) close(fd);\n    sdsfree(ci);\n    return -1;\n}\n\nvoid clusterSaveConfigOrDie(int do_fsync) {\n    if (clusterSaveConfig(do_fsync) == -1) {\n        serverLog(LL_WARNING,\"Fatal: can't update cluster config file.\");\n        exit(1);\n    }\n}\n\n/* Lock the cluster config using flock(), and leaks the file descriptor used to\n * acquire the lock so that the file will be locked forever.\n *\n * This works because we always update nodes.conf with a new version\n * in-place, reopening the file, and writing to it in place (later adjusting\n * the length with ftruncate()).\n *\n * On success C_OK is returned, otherwise an error is logged and\n * the function returns C_ERR to signal a lock was not acquired. */\nint clusterLockConfig(char *filename) {\n/* flock() does not exist on Solaris\n * and a fcntl-based solution won't help, as we constantly re-open that file,\n * which will release _all_ locks anyway\n */\n#if !defined(__sun)\n    /* To lock it, we need to open the file in a way it is created if\n     * it does not exist, otherwise there is a race condition with other\n     * processes. */\n    int fd = open(filename,O_WRONLY|O_CREAT|O_CLOEXEC,0644);\n    if (fd == -1) {\n        serverLog(LL_WARNING,\n            \"Can't open %s in order to acquire a lock: %s\",\n            filename, strerror(errno));\n        return C_ERR;\n    }\n\n    if (flock(fd,LOCK_EX|LOCK_NB) == -1) {\n        if (errno == EWOULDBLOCK) {\n            serverLog(LL_WARNING,\n                 \"Sorry, the cluster configuration file %s is already used \"\n                 \"by a different KeyDB Cluster node. Please make sure that \"\n                 \"different nodes use different cluster configuration \"\n                 \"files.\", filename);\n        } else {\n            serverLog(LL_WARNING,\n                \"Impossible to lock %s: %s\", filename, strerror(errno));\n        }\n        close(fd);\n        return C_ERR;\n    }\n    /* Lock acquired: leak the 'fd' by not closing it, so that we'll retain the\n     * lock to the file as long as the process exists.\n     *\n     * After fork, the child process will get the fd opened by the parent process,\n     * we need save `fd` to `cluster_config_file_lock_fd`, so that in redisFork(),\n     * it will be closed in the child process.\n     * If it is not closed, when the main process is killed -9, but the child process\n     * (redis-aof-rewrite) is still alive, the fd(lock) will still be held by the\n     * child process, and the main process will fail to get lock, means fail to start. */\n    g_pserver->cluster_config_file_lock_fd = fd;\n#endif /* __sun */\n\n    return C_OK;\n}\n\n/* Derives our ports to be announced in the cluster bus. */\nvoid deriveAnnouncedPorts(int *announced_port, int *announced_pport,\n                          int *announced_cport) {\n    int port = g_pserver->tls_cluster ? g_pserver->tls_port : g_pserver->port;\n    /* Default announced ports. */\n    *announced_port = port;\n    *announced_pport = g_pserver->tls_cluster ? g_pserver->port : 0;\n    *announced_cport = port + CLUSTER_PORT_INCR;\n    /* Config overriding announced ports. */\n    if (g_pserver->tls_cluster && g_pserver->cluster_announce_tls_port) {\n        *announced_port = g_pserver->cluster_announce_tls_port;\n        *announced_pport = g_pserver->cluster_announce_port;\n    } else if (g_pserver->cluster_announce_port) {\n        *announced_port = g_pserver->cluster_announce_port;\n    }\n    if (g_pserver->cluster_announce_bus_port) {\n        *announced_cport = g_pserver->cluster_announce_bus_port;\n    }\n}\n\n/* Some flags (currently just the NOFAILOVER flag) may need to be updated\n * in the \"myself\" node based on the current configuration of the node,\n * that may change at runtime via CONFIG SET. This function changes the\n * set of flags in myself->flags accordingly. */\nvoid clusterUpdateMyselfFlags(void) {\n    int oldflags = myself->flags;\n    int nofailover = g_pserver->cluster_slave_no_failover ?\n                     CLUSTER_NODE_NOFAILOVER : 0;\n    myself->flags &= ~CLUSTER_NODE_NOFAILOVER;\n    myself->flags |= nofailover;\n    if (myself->flags != oldflags) {\n        clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG|\n                             CLUSTER_TODO_UPDATE_STATE);\n    }\n}\n\nvoid clusterInit(void) {\n    int saveconf = 0;\n    if (g_pserver->enable_multimaster)\n    {\n        serverLog(LL_WARNING, \"Clusters are not compatible with multi-master\");\n        exit(EXIT_FAILURE);\n    }\n\n    g_pserver->cluster = (clusterState*)zmalloc(sizeof(clusterState), MALLOC_LOCAL);\n    g_pserver->cluster->myself = NULL;\n    g_pserver->cluster->currentEpoch = 0;\n    g_pserver->cluster->state = CLUSTER_FAIL;\n    g_pserver->cluster->size = 1;\n    g_pserver->cluster->todo_before_sleep = 0;\n    g_pserver->cluster->nodes = dictCreate(&clusterNodesDictType,NULL);\n    g_pserver->cluster->nodes_black_list =\n        dictCreate(&clusterNodesBlackListDictType,NULL);\n    g_pserver->cluster->failover_auth_time = 0;\n    g_pserver->cluster->failover_auth_count = 0;\n    g_pserver->cluster->failover_auth_rank = 0;\n    g_pserver->cluster->failover_auth_epoch = 0;\n    g_pserver->cluster->cant_failover_reason = CLUSTER_CANT_FAILOVER_NONE;\n    g_pserver->cluster->lastVoteEpoch = 0;\n    for (int i = 0; i < CLUSTERMSG_TYPE_COUNT; i++) {\n        g_pserver->cluster->stats_bus_messages_sent[i] = 0;\n        g_pserver->cluster->stats_bus_messages_received[i] = 0;\n    }\n    g_pserver->cluster->stats_pfail_nodes = 0;\n    memset(g_pserver->cluster->slots,0, sizeof(g_pserver->cluster->slots));\n    clusterCloseAllSlots();\n\n    /* Lock the cluster config file to make sure every node uses\n     * its own nodes.conf. */\n    g_pserver->cluster_config_file_lock_fd = -1;\n    if (clusterLockConfig(g_pserver->cluster_configfile) == C_ERR)\n        exit(1);\n\n    /* Load or create a new nodes configuration. */\n    if (clusterLoadConfig(g_pserver->cluster_configfile) == C_ERR) {\n        /* No configuration found. We will just use the random name provided\n         * by the createClusterNode() function. */\n        myself = g_pserver->cluster->myself =\n            createClusterNode(NULL,CLUSTER_NODE_MYSELF|CLUSTER_NODE_MASTER);\n        serverLog(LL_NOTICE,\"No cluster configuration found, I'm %.40s\",\n            myself->name);\n        clusterAddNode(myself);\n        saveconf = 1;\n    }\n    if (saveconf) clusterSaveConfigOrDie(1);\n\n    /* We need a listening TCP port for our cluster messaging needs. */\n    g_pserver->cfd.count = 0;\n\n    /* Port sanity check II\n     * The other handshake port check is triggered too late to stop\n     * us from trying to use a too-high cluster port number. */\n    int port = g_pserver->tls_cluster ? g_pserver->tls_port : g_pserver->port;\n    if (port > (65535-CLUSTER_PORT_INCR)) {\n        serverLog(LL_WARNING, \"KeyDB port number too high. \"\n                   \"Cluster communication port is 10,000 port \"\n                   \"numbers higher than your KeyDB port. \"\n                   \"Your KeyDB port number must be 55535 or less.\");\n        exit(1);\n    }\n    if (listenToPort(port+CLUSTER_PORT_INCR, &g_pserver->cfd,  0 /*fReusePort*/, 0 /*fFirstListen*/) == C_ERR) {\n        exit(1);\n    }\n    \n    serverAssert(serverTL == &g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN]);\n    if (createSocketAcceptHandler(&g_pserver->cfd, clusterAcceptHandler) != C_OK) {\n        serverPanic(\"Unrecoverable error creating KeyDB Cluster socket accept handler.\");\n    }\n\n    /* The slots -> keys map is a radix tree. Initialize it here. */\n    g_pserver->cluster->slots_to_keys = raxNew();\n    memset(g_pserver->cluster->slots_keys_count,0,\n           sizeof(g_pserver->cluster->slots_keys_count));\n\n    /* Set myself->port/cport/pport to my listening ports, we'll just need to\n     * discover the IP address via MEET messages. */\n    deriveAnnouncedPorts(&myself->port, &myself->pport, &myself->cport);\n\n    g_pserver->cluster->mf_end = 0;\n    resetManualFailover();\n    clusterUpdateMyselfFlags();\n}\n\n/* Reset a node performing a soft or hard reset:\n *\n * 1) All other nodes are forgotten.\n * 2) All the assigned / open slots are released.\n * 3) If the node is a slave, it turns into a master.\n * 4) Only for hard reset: a new Node ID is generated.\n * 5) Only for hard reset: currentEpoch and configEpoch are set to 0.\n * 6) The new configuration is saved and the cluster state updated.\n * 7) If the node was a slave, the whole data set is flushed away. */\nvoid clusterReset(int hard) {\n    dictIterator *di;\n    dictEntry *de;\n    int j;\n\n    /* Turn into master. */\n    if (nodeIsSlave(myself)) {\n        clusterSetNodeAsMaster(myself);\n        if (listLength(g_pserver->masters) > 0)\n        {\n            serverAssert(listLength(g_pserver->masters) == 1);\n            replicationUnsetMaster((redisMaster*)listFirst(g_pserver->masters)->value);\n        }\n        emptyDb(-1,EMPTYDB_NO_FLAGS,NULL);\n    }\n\n    /* Close slots, reset manual failover state. */\n    clusterCloseAllSlots();\n    resetManualFailover();\n\n    /* Unassign all the slots. */\n    for (j = 0; j < CLUSTER_SLOTS; j++) clusterDelSlot(j);\n\n    /* Forget all the nodes, but myself. */\n    di = dictGetSafeIterator(g_pserver->cluster->nodes);\n    while((de = dictNext(di)) != NULL) {\n        clusterNode *node = (clusterNode*)dictGetVal(de);\n\n        if (node == myself) continue;\n        clusterDelNode(node);\n    }\n    dictReleaseIterator(di);\n\n    /* Hard reset only: set epochs to 0, change node ID. */\n    if (hard) {\n        sds oldname;\n\n        g_pserver->cluster->currentEpoch = 0;\n        g_pserver->cluster->lastVoteEpoch = 0;\n        myself->configEpoch = 0;\n        serverLog(LL_WARNING, \"configEpoch set to 0 via CLUSTER RESET HARD\");\n\n        /* To change the Node ID we need to remove the old name from the\n         * nodes table, change the ID, and re-add back with new name. */\n        oldname = sdsnewlen(myself->name, CLUSTER_NAMELEN);\n        dictDelete(g_pserver->cluster->nodes,oldname);\n        sdsfree(oldname);\n        getRandomHexChars(myself->name, CLUSTER_NAMELEN);\n        clusterAddNode(myself);\n        serverLog(LL_NOTICE,\"Node hard reset, now I'm %.40s\", myself->name);\n    }\n\n    /* Make sure to persist the new config and update the state. */\n    clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG|\n                         CLUSTER_TODO_UPDATE_STATE|\n                         CLUSTER_TODO_FSYNC_CONFIG);\n}\n\n/* -----------------------------------------------------------------------------\n * CLUSTER communication link\n * -------------------------------------------------------------------------- */\n\nclusterLink *createClusterLink(clusterNode *node) {\n    clusterLink *link = (clusterLink*)zmalloc(sizeof(*link), MALLOC_LOCAL);\n    link->ctime = mstime();\n    link->sndbuf = sdsempty();\n    link->rcvbuf = (char*)zmalloc(link->rcvbuf_alloc = RCVBUF_INIT_LEN);\n    link->rcvbuf_len = 0;\n    link->node = node;\n    link->conn = NULL;\n    return link;\n}\n\n/* Free a cluster link, but does not free the associated node of course.\n * This function will just make sure that the original node associated\n * with this link will have the 'link' field set to NULL. */\nvoid freeClusterLink(clusterLink *link) {\n    if (ielFromEventLoop(serverTL->el) != IDX_EVENT_LOOP_MAIN)\n    {\n        // we can't perform this operation on this thread, queue it on the main thread\n        if (link->node)\n            link->node->link = NULL;\n        link->node = nullptr;\n        int res = aePostFunction(g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].el, [link]{\n            freeClusterLink(link);\n        });\n        serverAssert(res == AE_OK);\n        return;\n    }\n    if (link->conn) {\n        connClose(link->conn);\n        link->conn = NULL;\n    }\n    sdsfree(link->sndbuf);\n    zfree(link->rcvbuf);\n    if (link->node)\n        link->node->link = NULL;\n    zfree(link);\n}\n\nstatic void clusterConnAcceptHandler(connection *conn) {\n    serverAssert(ielFromEventLoop(serverTL->el) == IDX_EVENT_LOOP_MAIN);\n    clusterLink *link;\n\n    if (connGetState(conn) != CONN_STATE_CONNECTED) {\n        serverLog(LL_VERBOSE,\n                \"Error accepting cluster node connection: %s\", connGetLastError(conn));\n        connClose(conn);\n        return;\n    }\n\n    /* Create a link object we use to handle the connection.\n     * It gets passed to the readable handler when data is available.\n     * Initially the link->node pointer is set to NULL as we don't know\n     * which node is, but the right node is references once we know the\n     * node identity. */\n    link = createClusterLink(NULL);\n    link->conn = conn;\n    connSetPrivateData(conn, link);\n\n    /* Register read handler */\n    connSetReadHandler(conn, clusterReadHandler);\n}\n\n#define MAX_CLUSTER_ACCEPTS_PER_CALL 1000\nvoid clusterAcceptHandler(aeEventLoop *el, int fd, void *privdata, int mask) {\n    int cport, cfd;\n    int max = MAX_CLUSTER_ACCEPTS_PER_CALL;\n    char cip[NET_IP_STR_LEN];\n    UNUSED(el);\n    UNUSED(mask);\n    UNUSED(privdata);\n\n    /* If the server is starting up, don't accept cluster connections:\n     * UPDATE messages may interact with the database content. */\n    if (listLength(g_pserver->masters) == 0 && g_pserver->loading) return;\n\n    while(max--) {\n        cfd = anetTcpAccept(serverTL->neterr, fd, cip, sizeof(cip), &cport);\n        if (cfd == ANET_ERR) {\n            if (errno != EWOULDBLOCK)\n                serverLog(LL_VERBOSE,\n                    \"Error accepting cluster node: %s\", serverTL->neterr);\n            return;\n        }\n\n        connection *conn = g_pserver->tls_cluster ?\n            connCreateAcceptedTLS(cfd, TLS_CLIENT_AUTH_YES) : connCreateAcceptedSocket(cfd);\n\n        /* Make sure connection is not in an error state */\n        if (connGetState(conn) != CONN_STATE_ACCEPTING) {\n            serverLog(LL_VERBOSE,\n                \"Error creating an accepting connection for cluster node: %s\",\n                    connGetLastError(conn));\n            connClose(conn);\n            return;\n        }\n        connNonBlock(conn);\n        connEnableTcpNoDelay(conn);\n        connKeepAlive(conn,g_pserver->cluster_node_timeout * 2);\n\n        /* Use non-blocking I/O for cluster messages. */\n        serverLog(LL_VERBOSE,\"Accepting cluster node connection from %s:%d\", cip, cport);\n\n        /* Accept the connection now.  connAccept() may call our handler directly\n         * or schedule it for later depending on connection implementation.\n         */\n        if (connAccept(conn, clusterConnAcceptHandler) == C_ERR) {\n            if (connGetState(conn) == CONN_STATE_ERROR)\n                serverLog(LL_VERBOSE,\n                        \"Error accepting cluster node connection: %s\",\n                        connGetLastError(conn));\n            connClose(conn);\n            return;\n        }\n    }\n}\n\n/* Return the approximated number of sockets we are using in order to\n * take the cluster bus connections. */\nunsigned long getClusterConnectionsCount(void) {\n    /* We decrement the number of nodes by one, since there is the\n     * \"myself\" node too in the list. Each node uses two file descriptors,\n     * one incoming and one outgoing, thus the multiplication by 2. */\n    return g_pserver->cluster_enabled && g_pserver->cluster != nullptr ?\n           ((dictSize(g_pserver->cluster->nodes)-1)*2) : 0;\n}\n\n/* -----------------------------------------------------------------------------\n * Key space handling\n * -------------------------------------------------------------------------- */\n\n/* We have 16384 hash slots. The hash slot of a given key is obtained\n * as the least significant 14 bits of the crc16 of the key.\n *\n * However if the key contains the {...} pattern, only the part between\n * { and } is hashed. This may be useful in the future to force certain\n * keys to be in the same node (assuming no resharding is in progress). */\nunsigned int keyHashSlot(const char *key, int keylen) {\n    int s, e; /* start-end indexes of { and } */\n\n    for (s = 0; s < keylen; s++)\n        if (key[s] == '{') break;\n\n    /* No '{' ? Hash the whole key. This is the base case. */\n    if (s == keylen) return crc16(key,keylen) & 0x3FFF;\n\n    /* '{' found? Check if we have the corresponding '}'. */\n    for (e = s+1; e < keylen; e++)\n        if (key[e] == '}') break;\n\n    /* No '}' or nothing between {} ? Hash the whole key. */\n    if (e == keylen || e == s+1) return crc16(key,keylen) & 0x3FFF;\n\n    /* If we are here there is both a { and a } on its right. Hash\n     * what is in the middle between { and }. */\n    return crc16(key+s+1,e-s-1) & 0x3FFF;\n}\n\n/* -----------------------------------------------------------------------------\n * CLUSTER node API\n * -------------------------------------------------------------------------- */\n\n/* Create a new cluster node, with the specified flags.\n * If \"nodename\" is NULL this is considered a first handshake and a random\n * node name is assigned to this node (it will be fixed later when we'll\n * receive the first pong).\n *\n * The node is created and returned to the user, but it is not automatically\n * added to the nodes hash table. */\nclusterNode *createClusterNode(char *nodename, int flags) {\n    clusterNode *node = (clusterNode*)zmalloc(sizeof(*node), MALLOC_LOCAL);\n\n    if (nodename)\n        memcpy(node->name, nodename, CLUSTER_NAMELEN);\n    else\n        getRandomHexChars(node->name, CLUSTER_NAMELEN);\n    node->ctime = mstime();\n    node->configEpoch = 0;\n    node->flags = flags;\n    memset(node->slots,0,sizeof(node->slots));\n    node->slots_info = NULL;\n    node->numslots = 0;\n    node->numslaves = 0;\n    node->slaves = NULL;\n    node->slaveof = NULL;\n    node->ping_sent = node->pong_received = 0;\n    node->data_received = 0;\n    node->fail_time = 0;\n    node->link = NULL;\n    memset(node->ip,0,sizeof(node->ip));\n    node->port = 0;\n    node->cport = 0;\n    node->pport = 0;\n    node->fail_reports = listCreate();\n    node->voted_time = 0;\n    node->orphaned_time = 0;\n    node->repl_offset_time = 0;\n    node->repl_offset = 0;\n    listSetFreeMethod(node->fail_reports,zfree);\n    return node;\n}\n\n/* This function is called every time we get a failure report from a node.\n * The side effect is to populate the fail_reports list (or to update\n * the timestamp of an existing report).\n *\n * 'failing' is the node that is in failure state according to the\n * 'sender' node.\n *\n * The function returns 0 if it just updates a timestamp of an existing\n * failure report from the same sender. 1 is returned if a new failure\n * report is created. */\nint clusterNodeAddFailureReport(clusterNode *failing, clusterNode *sender) {\n    list *l = failing->fail_reports;\n    listNode *ln;\n    listIter li;\n    clusterNodeFailReport *fr;\n\n    /* If a failure report from the same sender already exists, just update\n     * the timestamp. */\n    listRewind(l,&li);\n    while ((ln = listNext(&li)) != NULL) {\n        fr = (clusterNodeFailReport*)ln->value;\n        if (fr->node == sender) {\n            fr->time = mstime();\n            return 0;\n        }\n    }\n\n    /* Otherwise create a new report. */\n    fr = (clusterNodeFailReport*)zmalloc(sizeof(*fr), MALLOC_LOCAL);\n    fr->node = sender;\n    fr->time = mstime();\n    listAddNodeTail(l,fr);\n    return 1;\n}\n\n/* Remove failure reports that are too old, where too old means reasonably\n * older than the global node timeout. Note that anyway for a node to be\n * flagged as FAIL we need to have a local PFAIL state that is at least\n * older than the global node timeout, so we don't just trust the number\n * of failure reports from other nodes. */\nvoid clusterNodeCleanupFailureReports(clusterNode *node) {\n    list *l = node->fail_reports;\n    listNode *ln;\n    listIter li;\n    clusterNodeFailReport *fr;\n    mstime_t maxtime = g_pserver->cluster_node_timeout *\n                     CLUSTER_FAIL_REPORT_VALIDITY_MULT;\n    mstime_t now = mstime();\n\n    listRewind(l,&li);\n    while ((ln = listNext(&li)) != NULL) {\n        fr = (clusterNodeFailReport*)ln->value;\n        if (now - fr->time > maxtime) listDelNode(l,ln);\n    }\n}\n\n/* Remove the failing report for 'node' if it was previously considered\n * failing by 'sender'. This function is called when a node informs us via\n * gossip that a node is OK from its point of view (no FAIL or PFAIL flags).\n *\n * Note that this function is called relatively often as it gets called even\n * when there are no nodes failing, and is O(N), however when the cluster is\n * fine the failure reports list is empty so the function runs in constant\n * time.\n *\n * The function returns 1 if the failure report was found and removed.\n * Otherwise 0 is returned. */\nint clusterNodeDelFailureReport(clusterNode *node, clusterNode *sender) {\n    list *l = node->fail_reports;\n    listNode *ln;\n    listIter li;\n    clusterNodeFailReport *fr;\n\n    /* Search for a failure report from this sender. */\n    listRewind(l,&li);\n    while ((ln = listNext(&li)) != NULL) {\n        fr = (clusterNodeFailReport*)ln->value;\n        if (fr->node == sender) break;\n    }\n    if (!ln) return 0; /* No failure report from this sender. */\n\n    /* Remove the failure report. */\n    listDelNode(l,ln);\n    clusterNodeCleanupFailureReports(node);\n    return 1;\n}\n\n/* Return the number of external nodes that believe 'node' is failing,\n * not including this node, that may have a PFAIL or FAIL state for this\n * node as well. */\nint clusterNodeFailureReportsCount(clusterNode *node) {\n    clusterNodeCleanupFailureReports(node);\n    return listLength(node->fail_reports);\n}\n\nint clusterNodeRemoveSlave(clusterNode *master, clusterNode *slave) {\n    int j;\n\n    for (j = 0; j < master->numslaves; j++) {\n        if (master->slaves[j] == slave) {\n            if ((j+1) < master->numslaves) {\n                int remaining_slaves = (master->numslaves - j) - 1;\n                memmove(master->slaves+j,master->slaves+(j+1),\n                        (sizeof(*master->slaves) * remaining_slaves));\n            }\n            master->numslaves--;\n            if (master->numslaves == 0)\n                master->flags &= ~CLUSTER_NODE_MIGRATE_TO;\n            return C_OK;\n        }\n    }\n    return C_ERR;\n}\n\nint clusterNodeAddSlave(clusterNode *master, clusterNode *slave) {\n    int j;\n\n    /* If it's already a slave, don't add it again. */\n    for (j = 0; j < master->numslaves; j++)\n        if (master->slaves[j] == slave) return C_ERR;\n    master->slaves = (clusterNode**)zrealloc(master->slaves,\n        sizeof(clusterNode*)*(master->numslaves+1), MALLOC_LOCAL);\n    master->slaves[master->numslaves] = slave;\n    master->numslaves++;\n    master->flags |= CLUSTER_NODE_MIGRATE_TO;\n    return C_OK;\n}\n\nint clusterCountNonFailingSlaves(clusterNode *n) {\n    int j, okslaves = 0;\n\n    for (j = 0; j < n->numslaves; j++)\n        if (!nodeFailed(n->slaves[j])) okslaves++;\n    return okslaves;\n}\n\n/* Low level cleanup of the node structure. Only called by clusterDelNode(). */\nvoid freeClusterNode(clusterNode *n) {\n    sds nodename;\n    int j;\n\n    /* If the node has associated slaves, we have to set\n     * all the slaves->slaveof fields to NULL (unknown). */\n    for (j = 0; j < n->numslaves; j++)\n        n->slaves[j]->slaveof = NULL;\n\n    /* Remove this node from the list of slaves of its master. */\n    if (nodeIsSlave(n) && n->slaveof) clusterNodeRemoveSlave(n->slaveof,n);\n\n    /* Unlink from the set of nodes. */\n    nodename = sdsnewlen(n->name, CLUSTER_NAMELEN);\n    serverAssert(dictDelete(g_pserver->cluster->nodes,nodename) == DICT_OK);\n    sdsfree(nodename);\n\n    /* Release link and associated data structures. */\n    if (n->link) freeClusterLink(n->link);\n    listRelease(n->fail_reports);\n    zfree(n->slaves);\n    zfree(n);\n}\n\n/* Add a node to the nodes hash table */\nvoid clusterAddNode(clusterNode *node) {\n    int retval;\n\n    retval = dictAdd(g_pserver->cluster->nodes,\n            sdsnewlen(node->name,CLUSTER_NAMELEN), node);\n    serverAssert(retval == DICT_OK);\n}\n\n/* Remove a node from the cluster. The function performs the high level\n * cleanup, calling freeClusterNode() for the low level cleanup.\n * Here we do the following:\n *\n * 1) Mark all the slots handled by it as unassigned.\n * 2) Remove all the failure reports sent by this node and referenced by\n *    other nodes.\n * 3) Free the node with freeClusterNode() that will in turn remove it\n *    from the hash table and from the list of slaves of its master, if\n *    it is a slave node.\n */\nvoid clusterDelNode(clusterNode *delnode) {\n    int j;\n    dictIterator *di;\n    dictEntry *de;\n\n    /* 1) Mark slots as unassigned. */\n    for (j = 0; j < CLUSTER_SLOTS; j++) {\n        if (g_pserver->cluster->importing_slots_from[j] == delnode)\n            g_pserver->cluster->importing_slots_from[j] = NULL;\n        if (g_pserver->cluster->migrating_slots_to[j] == delnode)\n            g_pserver->cluster->migrating_slots_to[j] = NULL;\n        if (g_pserver->cluster->slots[j] == delnode)\n            clusterDelSlot(j);\n    }\n\n    /* 2) Remove failure reports. */\n    di = dictGetSafeIterator(g_pserver->cluster->nodes);\n    while((de = dictNext(di)) != NULL) {\n        clusterNode *node = (clusterNode*)dictGetVal(de);\n\n        if (node == delnode) continue;\n        clusterNodeDelFailureReport(node,delnode);\n    }\n    dictReleaseIterator(di);\n\n    /* 3) Free the node, unlinking it from the cluster. */\n    freeClusterNode(delnode);\n}\n\n/* Node lookup by name */\nclusterNode *clusterLookupNode(const char *name) {\n    sds s = sdsnewlen(name, CLUSTER_NAMELEN);\n    dictEntry *de;\n\n    de = dictFind(g_pserver->cluster->nodes,s);\n    sdsfree(s);\n    if (de == NULL) return NULL;\n    return (clusterNode*)dictGetVal(de);\n}\n\n/* This is only used after the handshake. When we connect a given IP/PORT\n * as a result of CLUSTER MEET we don't have the node name yet, so we\n * pick a random one, and will fix it when we receive the PONG request using\n * this function. */\nvoid clusterRenameNode(clusterNode *node, char *newname) {\n    int retval;\n    sds s = sdsnewlen(node->name, CLUSTER_NAMELEN);\n\n    serverLog(LL_DEBUG,\"Renaming node %.40s into %.40s\",\n        node->name, newname);\n    retval = dictDelete(g_pserver->cluster->nodes, s);\n    sdsfree(s);\n    serverAssert(retval == DICT_OK);\n    memcpy(node->name, newname, CLUSTER_NAMELEN);\n    clusterAddNode(node);\n}\n\n/* -----------------------------------------------------------------------------\n * CLUSTER config epoch handling\n * -------------------------------------------------------------------------- */\n\n/* Return the greatest configEpoch found in the cluster, or the current\n * epoch if greater than any node configEpoch. */\nuint64_t clusterGetMaxEpoch(void) {\n    uint64_t max = 0;\n    dictIterator *di;\n    dictEntry *de;\n\n    di = dictGetSafeIterator(g_pserver->cluster->nodes);\n    while((de = dictNext(di)) != NULL) {\n        clusterNode *node = (clusterNode*)dictGetVal(de);\n        if (node->configEpoch > max) max = node->configEpoch;\n    }\n    dictReleaseIterator(di);\n    if (max < g_pserver->cluster->currentEpoch) max = g_pserver->cluster->currentEpoch;\n    return max;\n}\n\n/* If this node epoch is zero or is not already the greatest across the\n * cluster (from the POV of the local configuration), this function will:\n *\n * 1) Generate a new config epoch, incrementing the current epoch.\n * 2) Assign the new epoch to this node, WITHOUT any consensus.\n * 3) Persist the configuration on disk before sending packets with the\n *    new configuration.\n *\n * If the new config epoch is generated and assigned, C_OK is returned,\n * otherwise C_ERR is returned (since the node has already the greatest\n * configuration around) and no operation is performed.\n *\n * Important note: this function violates the principle that config epochs\n * should be generated with consensus and should be unique across the cluster.\n * However Redis Cluster uses this auto-generated new config epochs in two\n * cases:\n *\n * 1) When slots are closed after importing. Otherwise resharding would be\n *    too expensive.\n * 2) When CLUSTER FAILOVER is called with options that force a slave to\n *    failover its master even if there is not master majority able to\n *    create a new configuration epoch.\n *\n * Redis Cluster will not explode using this function, even in the case of\n * a collision between this node and another node, generating the same\n * configuration epoch unilaterally, because the config epoch conflict\n * resolution algorithm will eventually move colliding nodes to different\n * config epochs. However using this function may violate the \"last failover\n * wins\" rule, so should only be used with care. */\nint clusterBumpConfigEpochWithoutConsensus(void) {\n    uint64_t maxEpoch = clusterGetMaxEpoch();\n\n    if (myself->configEpoch == 0 ||\n        myself->configEpoch != maxEpoch)\n    {\n        g_pserver->cluster->currentEpoch++;\n        myself->configEpoch = g_pserver->cluster->currentEpoch;\n        clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG|\n                             CLUSTER_TODO_FSYNC_CONFIG);\n        serverLog(LL_WARNING,\n            \"New configEpoch set to %llu\",\n            (unsigned long long) myself->configEpoch);\n        return C_OK;\n    } else {\n        return C_ERR;\n    }\n}\n\n/* This function is called when this node is a master, and we receive from\n * another master a configuration epoch that is equal to our configuration\n * epoch.\n *\n * BACKGROUND\n *\n * It is not possible that different slaves get the same config\n * epoch during a failover election, because the slaves need to get voted\n * by a majority. However when we perform a manual resharding of the cluster\n * the node will assign a configuration epoch to itself without to ask\n * for agreement. Usually resharding happens when the cluster is working well\n * and is supervised by the sysadmin, however it is possible for a failover\n * to happen exactly while the node we are resharding a slot to assigns itself\n * a new configuration epoch, but before it is able to propagate it.\n *\n * So technically it is possible in this condition that two nodes end with\n * the same configuration epoch.\n *\n * Another possibility is that there are bugs in the implementation causing\n * this to happen.\n *\n * Moreover when a new cluster is created, all the nodes start with the same\n * configEpoch. This collision resolution code allows nodes to automatically\n * end with a different configEpoch at startup automatically.\n *\n * In all the cases, we want a mechanism that resolves this issue automatically\n * as a safeguard. The same configuration epoch for masters serving different\n * set of slots is not harmful, but it is if the nodes end serving the same\n * slots for some reason (manual errors or software bugs) without a proper\n * failover procedure.\n *\n * In general we want a system that eventually always ends with different\n * masters having different configuration epochs whatever happened, since\n * nothing is worse than a split-brain condition in a distributed system.\n *\n * BEHAVIOR\n *\n * When this function gets called, what happens is that if this node\n * has the lexicographically smaller Node ID compared to the other node\n * with the conflicting epoch (the 'sender' node), it will assign itself\n * the greatest configuration epoch currently detected among nodes plus 1.\n *\n * This means that even if there are multiple nodes colliding, the node\n * with the greatest Node ID never moves forward, so eventually all the nodes\n * end with a different configuration epoch.\n */\nvoid clusterHandleConfigEpochCollision(clusterNode *sender) {\n    /* Prerequisites: nodes have the same configEpoch and are both masters. */\n    if (sender->configEpoch != myself->configEpoch ||\n        !nodeIsMaster(sender) || !nodeIsMaster(myself)) return;\n    /* Don't act if the colliding node has a smaller Node ID. */\n    if (memcmp(sender->name,myself->name,CLUSTER_NAMELEN) <= 0) return;\n    /* Get the next ID available at the best of this node knowledge. */\n    g_pserver->cluster->currentEpoch++;\n    myself->configEpoch = g_pserver->cluster->currentEpoch;\n    clusterSaveConfigOrDie(1);\n    serverLog(LL_VERBOSE,\n        \"WARNING: configEpoch collision with node %.40s.\"\n        \" configEpoch set to %llu\",\n        sender->name,\n        (unsigned long long) myself->configEpoch);\n}\n\n/* -----------------------------------------------------------------------------\n * CLUSTER nodes blacklist\n *\n * The nodes blacklist is just a way to ensure that a given node with a given\n * Node ID is not readded before some time elapsed (this time is specified\n * in seconds in CLUSTER_BLACKLIST_TTL).\n *\n * This is useful when we want to remove a node from the cluster completely:\n * when CLUSTER FORGET is called, it also puts the node into the blacklist so\n * that even if we receive gossip messages from other nodes that still remember\n * about the node we want to remove, we don't re-add it before some time.\n *\n * Currently the CLUSTER_BLACKLIST_TTL is set to 1 minute, this means\n * that keydb-trib has 60 seconds to send CLUSTER FORGET messages to nodes\n * in the cluster without dealing with the problem of other nodes re-adding\n * back the node to nodes we already sent the FORGET command to.\n *\n * The data structure used is a hash table with an sds string representing\n * the node ID as key, and the time when it is ok to re-add the node as\n * value.\n * -------------------------------------------------------------------------- */\n\n#define CLUSTER_BLACKLIST_TTL 60      /* 1 minute. */\n\n\n/* Before of the addNode() or Exists() operations we always remove expired\n * entries from the black list. This is an O(N) operation but it is not a\n * problem since add / exists operations are called very infrequently and\n * the hash table is supposed to contain very little elements at max.\n * However without the cleanup during long uptime and with some automated\n * node add/removal procedures, entries could accumulate. */\nvoid clusterBlacklistCleanup(void) {\n    dictIterator *di;\n    dictEntry *de;\n\n    di = dictGetSafeIterator(g_pserver->cluster->nodes_black_list);\n    while((de = dictNext(di)) != NULL) {\n        int64_t expire = dictGetUnsignedIntegerVal(de);\n\n        if (expire < g_pserver->unixtime)\n            dictDelete(g_pserver->cluster->nodes_black_list,dictGetKey(de));\n    }\n    dictReleaseIterator(di);\n}\n\n/* Cleanup the blacklist and add a new node ID to the black list. */\nvoid clusterBlacklistAddNode(clusterNode *node) {\n    dictEntry *de;\n    sds id = sdsnewlen(node->name,CLUSTER_NAMELEN);\n\n    clusterBlacklistCleanup();\n    if (dictAdd(g_pserver->cluster->nodes_black_list,id,NULL) == DICT_OK) {\n        /* If the key was added, duplicate the sds string representation of\n         * the key for the next lookup. We'll free it at the end. */\n        id = sdsdup(id);\n    }\n    de = dictFind(g_pserver->cluster->nodes_black_list,id);\n    dictSetUnsignedIntegerVal(de,time(NULL)+CLUSTER_BLACKLIST_TTL);\n    sdsfree(id);\n}\n\n/* Return non-zero if the specified node ID exists in the blacklist.\n * You don't need to pass an sds string here, any pointer to 40 bytes\n * will work. */\nint clusterBlacklistExists(char *nodeid) {\n    sds id = sdsnewlen(nodeid,CLUSTER_NAMELEN);\n    int retval;\n\n    clusterBlacklistCleanup();\n    retval = dictFind(g_pserver->cluster->nodes_black_list,id) != NULL;\n    sdsfree(id);\n    return retval;\n}\n\n/* -----------------------------------------------------------------------------\n * CLUSTER messages exchange - PING/PONG and gossip\n * -------------------------------------------------------------------------- */\n\n/* This function checks if a given node should be marked as FAIL.\n * It happens if the following conditions are met:\n *\n * 1) We received enough failure reports from other master nodes via gossip.\n *    Enough means that the majority of the masters signaled the node is\n *    down recently.\n * 2) We believe this node is in PFAIL state.\n *\n * If a failure is detected we also inform the whole cluster about this\n * event trying to force every other node to set the FAIL flag for the node.\n *\n * Note that the form of agreement used here is weak, as we collect the majority\n * of masters state during some time, and even if we force agreement by\n * propagating the FAIL message, because of partitions we may not reach every\n * node. However:\n *\n * 1) Either we reach the majority and eventually the FAIL state will propagate\n *    to all the cluster.\n * 2) Or there is no majority so no slave promotion will be authorized and the\n *    FAIL flag will be cleared after some time.\n */\nvoid markNodeAsFailingIfNeeded(clusterNode *node) {\n    int failures;\n    int needed_quorum = (g_pserver->cluster->size / 2) + 1;\n\n    if (!nodeTimedOut(node)) return; /* We can reach it. */\n    if (nodeFailed(node)) return; /* Already FAILing. */\n\n    failures = clusterNodeFailureReportsCount(node);\n    /* Also count myself as a voter if I'm a master. */\n    if (nodeIsMaster(myself)) failures++;\n    if (failures < needed_quorum) return; /* No weak agreement from masters. */\n\n    serverLog(LL_NOTICE,\n        \"Marking node %.40s as failing (quorum reached).\", node->name);\n\n    /* Mark the node as failing. */\n    node->flags &= ~CLUSTER_NODE_PFAIL;\n    node->flags |= CLUSTER_NODE_FAIL;\n    node->fail_time = mstime();\n\n    /* Broadcast the failing node name to everybody, forcing all the other\n     * reachable nodes to flag the node as FAIL.\n     * We do that even if this node is a replica and not a master: anyway\n     * the failing state is triggered collecting failure reports from masters,\n     * so here the replica is only helping propagating this status. */\n    clusterSendFail(node->name);\n    clusterDoBeforeSleep(CLUSTER_TODO_UPDATE_STATE|CLUSTER_TODO_SAVE_CONFIG);\n}\n\n/* This function is called only if a node is marked as FAIL, but we are able\n * to reach it again. It checks if there are the conditions to undo the FAIL\n * state. */\nvoid clearNodeFailureIfNeeded(clusterNode *node) {\n    mstime_t now = mstime();\n\n    serverAssert(nodeFailed(node));\n\n    /* For slaves we always clear the FAIL flag if we can contact the\n     * node again. */\n    if (nodeIsSlave(node) || node->numslots == 0) {\n        serverLog(LL_NOTICE,\n            \"Clear FAIL state for node %.40s: %s is reachable again.\",\n                node->name,\n                nodeIsSlave(node) ? \"replica\" : \"master without slots\");\n        node->flags &= ~CLUSTER_NODE_FAIL;\n        clusterDoBeforeSleep(CLUSTER_TODO_UPDATE_STATE|CLUSTER_TODO_SAVE_CONFIG);\n    }\n\n    /* If it is a master and...\n     * 1) The FAIL state is old enough.\n     * 2) It is yet serving slots from our point of view (not failed over).\n     * Apparently no one is going to fix these slots, clear the FAIL flag. */\n    if (nodeIsMaster(node) && node->numslots > 0 &&\n        (now - node->fail_time) >\n        (g_pserver->cluster_node_timeout * CLUSTER_FAIL_UNDO_TIME_MULT))\n    {\n        serverLog(LL_NOTICE,\n            \"Clear FAIL state for node %.40s: is reachable again and nobody is serving its slots after some time.\",\n                node->name);\n        node->flags &= ~CLUSTER_NODE_FAIL;\n        clusterDoBeforeSleep(CLUSTER_TODO_UPDATE_STATE|CLUSTER_TODO_SAVE_CONFIG);\n    }\n}\n\n/* Return true if we already have a node in HANDSHAKE state matching the\n * specified ip address and port number. This function is used in order to\n * avoid adding a new handshake node for the same address multiple times. */\nint clusterHandshakeInProgress(char *ip, int port, int cport) {\n    dictIterator *di;\n    dictEntry *de;\n\n    di = dictGetSafeIterator(g_pserver->cluster->nodes);\n    while((de = dictNext(di)) != NULL) {\n        clusterNode *node = (clusterNode*)dictGetVal(de);\n\n        if (!nodeInHandshake(node)) continue;\n        if (!strcasecmp(node->ip,ip) &&\n            node->port == port &&\n            node->cport == cport) break;\n    }\n    dictReleaseIterator(di);\n    return de != NULL;\n}\n\n/* Start a handshake with the specified address if there is not one\n * already in progress. Returns non-zero if the handshake was actually\n * started. On error zero is returned and errno is set to one of the\n * following values:\n *\n * EAGAIN - There is already a handshake in progress for this address.\n * EINVAL - IP or port are not valid. */\nint clusterStartHandshake(char *ip, int port, int cport) {\n    clusterNode *n;\n    char norm_ip[NET_IP_STR_LEN];\n    struct sockaddr_storage sa;\n\n    /* IP sanity check */\n    if (inet_pton(AF_INET,ip,\n            &(((struct sockaddr_in *)&sa)->sin_addr)))\n    {\n        sa.ss_family = AF_INET;\n    } else if (inet_pton(AF_INET6,ip,\n            &(((struct sockaddr_in6 *)&sa)->sin6_addr)))\n    {\n        sa.ss_family = AF_INET6;\n    } else {\n        errno = EINVAL;\n        return 0;\n    }\n\n    /* Port sanity check */\n    if (port <= 0 || port > 65535 || cport <= 0 || cport > 65535) {\n        errno = EINVAL;\n        return 0;\n    }\n\n    /* Set norm_ip as the normalized string representation of the node\n     * IP address. */\n    memset(norm_ip,0,NET_IP_STR_LEN);\n    if (sa.ss_family == AF_INET)\n        inet_ntop(AF_INET,\n            (void*)&(((struct sockaddr_in *)&sa)->sin_addr),\n            norm_ip,NET_IP_STR_LEN);\n    else\n        inet_ntop(AF_INET6,\n            (void*)&(((struct sockaddr_in6 *)&sa)->sin6_addr),\n            norm_ip,NET_IP_STR_LEN);\n\n    if (clusterHandshakeInProgress(norm_ip,port,cport)) {\n        errno = EAGAIN;\n        return 0;\n    }\n\n    /* Add the node with a random address (NULL as first argument to\n     * createClusterNode()). Everything will be fixed during the\n     * handshake. */\n    n = createClusterNode(NULL,CLUSTER_NODE_HANDSHAKE|CLUSTER_NODE_MEET);\n    memcpy(n->ip,norm_ip,sizeof(n->ip));\n    n->port = port;\n    n->cport = cport;\n    clusterAddNode(n);\n    return 1;\n}\n\n/* Process the gossip section of PING or PONG packets.\n * Note that this function assumes that the packet is already sanity-checked\n * by the caller, not in the content of the gossip section, but in the\n * length. */\nvoid clusterProcessGossipSection(clusterMsg *hdr, clusterLink *link) {\n    uint16_t count = ntohs(hdr->count);\n    clusterMsgDataGossip *g = (clusterMsgDataGossip*) hdr->data.ping.gossip;\n    clusterNode *sender = link->node ? link->node : clusterLookupNode(hdr->sender);\n\n    while(count--) {\n        uint16_t flags = ntohs(g->flags);\n        clusterNode *node;\n        sds ci;\n\n        if (cserver.verbosity == LL_DEBUG) {\n            ci = representClusterNodeFlags(sdsempty(), flags);\n            serverLog(LL_DEBUG,\"GOSSIP %.40s %s:%d@%d %s\",\n                g->nodename,\n                g->ip,\n                ntohs(g->port),\n                ntohs(g->cport),\n                ci);\n            sdsfree(ci);\n        }\n\n        /* Update our state accordingly to the gossip sections */\n        node = clusterLookupNode(g->nodename);\n        if (node) {\n            /* We already know this node.\n               Handle failure reports, only when the sender is a master. */\n            if (sender && nodeIsMaster(sender) && node != myself) {\n                if (flags & (CLUSTER_NODE_FAIL|CLUSTER_NODE_PFAIL)) {\n                    if (clusterNodeAddFailureReport(node,sender)) {\n                        serverLog(LL_VERBOSE,\n                            \"Node %.40s reported node %.40s as not reachable.\",\n                            sender->name, node->name);\n                    }\n                    markNodeAsFailingIfNeeded(node);\n                } else {\n                    if (clusterNodeDelFailureReport(node,sender)) {\n                        serverLog(LL_VERBOSE,\n                            \"Node %.40s reported node %.40s is back online.\",\n                            sender->name, node->name);\n                    }\n                }\n            }\n\n            /* If from our POV the node is up (no failure flags are set),\n             * we have no pending ping for the node, nor we have failure\n             * reports for this node, update the last pong time with the\n             * one we see from the other nodes. */\n            if (!(flags & (CLUSTER_NODE_FAIL|CLUSTER_NODE_PFAIL)) &&\n                node->ping_sent == 0 &&\n                clusterNodeFailureReportsCount(node) == 0)\n            {\n                mstime_t pongtime = ntohl(g->pong_received);\n                pongtime *= 1000; /* Convert back to milliseconds. */\n\n                /* Replace the pong time with the received one only if\n                 * it's greater than our view but is not in the future\n                 * (with 500 milliseconds tolerance) from the POV of our\n                 * clock. */\n                mstime_t mstime;\n                __atomic_load(&g_pserver->mstime, &mstime, __ATOMIC_RELAXED);\n                if (pongtime <= (mstime+500) &&\n                    pongtime > node->pong_received)\n                {\n                    node->pong_received = pongtime;\n                }\n            }\n\n            /* If we already know this node, but it is not reachable, and\n             * we see a different address in the gossip section of a node that\n             * can talk with this other node, update the address, disconnect\n             * the old link if any, so that we'll attempt to connect with the\n             * new address. */\n            if (node->flags & (CLUSTER_NODE_FAIL|CLUSTER_NODE_PFAIL) &&\n                !(flags & CLUSTER_NODE_NOADDR) &&\n                !(flags & (CLUSTER_NODE_FAIL|CLUSTER_NODE_PFAIL)) &&\n                (strcasecmp(node->ip,g->ip) ||\n                 node->port != ntohs(g->port) ||\n                 node->cport != ntohs(g->cport)))\n            {\n                if (node->link) freeClusterLink(node->link);\n                memcpy(node->ip,g->ip,NET_IP_STR_LEN);\n                node->port = ntohs(g->port);\n                node->pport = ntohs(g->pport);\n                node->cport = ntohs(g->cport);\n                node->flags &= ~CLUSTER_NODE_NOADDR;\n            }\n        } else {\n            /* If it's not in NOADDR state and we don't have it, we\n             * add it to our trusted dict with exact nodeid and flag.\n             * Note that we cannot simply start a handshake against\n             * this IP/PORT pairs, since IP/PORT can be reused already,\n             * otherwise we risk joining another cluster.\n             *\n             * Note that we require that the sender of this gossip message\n             * is a well known node in our cluster, otherwise we risk\n             * joining another cluster. */\n            if (sender &&\n                !(flags & CLUSTER_NODE_NOADDR) &&\n                !clusterBlacklistExists(g->nodename))\n            {\n                clusterNode *node;\n                node = createClusterNode(g->nodename, flags);\n                memcpy(node->ip,g->ip,NET_IP_STR_LEN);\n                node->port = ntohs(g->port);\n                node->pport = ntohs(g->pport);\n                node->cport = ntohs(g->cport);\n                clusterAddNode(node);\n            }\n        }\n\n        /* Next node */\n        g++;\n    }\n}\n\n/* IP -> string conversion. 'buf' is supposed to at least be 46 bytes.\n * If 'announced_ip' length is non-zero, it is used instead of extracting\n * the IP from the socket peer address. */\nvoid nodeIp2String(char *buf, clusterLink *link, char *announced_ip) {\n    if (announced_ip[0] != '\\0') {\n        memcpy(buf,announced_ip,NET_IP_STR_LEN);\n        buf[NET_IP_STR_LEN-1] = '\\0'; /* We are not sure the input is sane. */\n    } else {\n        connPeerToString(link->conn, buf, NET_IP_STR_LEN, NULL);\n    }\n}\n\n/* Update the node address to the IP address that can be extracted\n * from link->fd, or if hdr->myip is non empty, to the address the node\n * is announcing us. The port is taken from the packet header as well.\n *\n * If the address or port changed, disconnect the node link so that we'll\n * connect again to the new address.\n *\n * If the ip/port pair are already correct no operation is performed at\n * all.\n *\n * The function returns 0 if the node address is still the same,\n * otherwise 1 is returned. */\nint nodeUpdateAddressIfNeeded(clusterNode *node, clusterLink *link,\n                              clusterMsg *hdr)\n{\n    char ip[NET_IP_STR_LEN] = {0};\n    int port = ntohs(hdr->port);\n    int pport = ntohs(hdr->pport);\n    int cport = ntohs(hdr->cport);\n\n    /* We don't proceed if the link is the same as the sender link, as this\n     * function is designed to see if the node link is consistent with the\n     * symmetric link that is used to receive PINGs from the node.\n     *\n     * As a side effect this function never frees the passed 'link', so\n     * it is safe to call during packet processing. */\n    if (link == node->link) return 0;\n\n    nodeIp2String(ip,link,hdr->myip);\n    if (node->port == port && node->cport == cport && node->pport == pport &&\n        strcmp(ip,node->ip) == 0) return 0;\n\n    /* IP / port is different, update it. */\n    memcpy(node->ip,ip,sizeof(ip));\n    node->port = port;\n    node->pport = pport;\n    node->cport = cport;\n    if (node->link) freeClusterLink(node->link);\n    node->flags &= ~CLUSTER_NODE_NOADDR;\n    serverLog(LL_WARNING,\"Address updated for node %.40s, now %s:%d\",\n        node->name, node->ip, node->port);\n\n    /* Check if this is our master and we have to change the\n     * replication target as well. */\n    if (nodeIsSlave(myself) && myself->slaveof == node)\n    {\n        serverAssert(listLength(g_pserver->masters) == 1);\n        replicationAddMaster(node->ip, node->port);\n    }\n        \n    return 1;\n}\n\n/* Reconfigure the specified node 'n' as a master. This function is called when\n * a node that we believed to be a slave is now acting as master in order to\n * update the state of the node. */\nvoid clusterSetNodeAsMaster(clusterNode *n) {\n    if (nodeIsMaster(n)) return;\n\n    if (n->slaveof) {\n        clusterNodeRemoveSlave(n->slaveof,n);\n        if (n != myself) n->flags |= CLUSTER_NODE_MIGRATE_TO;\n    }\n    n->flags &= ~CLUSTER_NODE_SLAVE;\n    n->flags |= CLUSTER_NODE_MASTER;\n    n->slaveof = NULL;\n\n    /* Update config and state. */\n    clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG|\n                         CLUSTER_TODO_UPDATE_STATE);\n}\n\n/* This function is called when we receive a master configuration via a\n * PING, PONG or UPDATE packet. What we receive is a node, a configEpoch of the\n * node, and the set of slots claimed under this configEpoch.\n *\n * What we do is to rebind the slots with newer configuration compared to our\n * local configuration, and if needed, we turn ourself into a replica of the\n * node (see the function comments for more info).\n *\n * The 'sender' is the node for which we received a configuration update.\n * Sometimes it is not actually the \"Sender\" of the information, like in the\n * case we receive the info via an UPDATE packet. */\nvoid clusterUpdateSlotsConfigWith(clusterNode *sender, uint64_t senderConfigEpoch, unsigned char *slots) {\n    int j;\n    clusterNode *curmaster = NULL, *newmaster = NULL;\n    /* The dirty slots list is a list of slots for which we lose the ownership\n     * while having still keys inside. This usually happens after a failover\n     * or after a manual cluster reconfiguration operated by the admin.\n     *\n     * If the update message is not able to demote a master to slave (in this\n     * case we'll resync with the master updating the whole key space), we\n     * need to delete all the keys in the slots we lost ownership. */\n    uint16_t dirty_slots[CLUSTER_SLOTS];\n    int dirty_slots_count = 0;\n\n    /* We should detect if sender is new master of our shard.\n     * We will know it if all our slots were migrated to sender, and sender\n     * has no slots except ours */\n    int sender_slots = 0;\n    int migrated_our_slots = 0;\n\n    /* Here we set curmaster to this node or the node this node\n     * replicates to if it's a slave. In the for loop we are\n     * interested to check if slots are taken away from curmaster. */\n    curmaster = nodeIsMaster(myself) ? myself : myself->slaveof;\n\n    if (sender == myself) {\n        serverLog(LL_WARNING,\"Discarding UPDATE message about myself.\");\n        return;\n    }\n\n    for (j = 0; j < CLUSTER_SLOTS; j++) {\n        if (bitmapTestBit(slots,j)) {\n            sender_slots++;\n\n            /* The slot is already bound to the sender of this message. */\n            if (g_pserver->cluster->slots[j] == sender) continue;\n\n            /* The slot is in importing state, it should be modified only\n             * manually via keydb-trib (example: a resharding is in progress\n             * and the migrating side slot was already closed and is advertising\n             * a new config. We still want the slot to be closed manually). */\n            if (g_pserver->cluster->importing_slots_from[j]) continue;\n\n            /* We rebind the slot to the new node claiming it if:\n             * 1) The slot was unassigned or the new node claims it with a\n             *    greater configEpoch.\n             * 2) We are not currently importing the slot. */\n            if (g_pserver->cluster->slots[j] == NULL ||\n                g_pserver->cluster->slots[j]->configEpoch < senderConfigEpoch)\n            {\n                /* Was this slot mine, and still contains keys? Mark it as\n                 * a dirty slot. */\n                if (g_pserver->cluster->slots[j] == myself &&\n                    countKeysInSlot(j) &&\n                    sender != myself)\n                {\n                    dirty_slots[dirty_slots_count] = j;\n                    dirty_slots_count++;\n                }\n\n                if (g_pserver->cluster->slots[j] == curmaster) {\n                    newmaster = sender;\n                    migrated_our_slots++;\n                }\n                clusterDelSlot(j);\n                clusterAddSlot(sender,j);\n                clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG|\n                                     CLUSTER_TODO_UPDATE_STATE|\n                                     CLUSTER_TODO_FSYNC_CONFIG);\n            }\n        }\n    }\n\n    /* After updating the slots configuration, don't do any actual change\n     * in the state of the server if a module disabled Redis Cluster\n     * keys redirections. */\n    if (g_pserver->cluster_module_flags & CLUSTER_MODULE_FLAG_NO_REDIRECTION)\n        return;\n\n    /* If at least one slot was reassigned from a node to another node\n     * with a greater configEpoch, it is possible that:\n     * 1) We are a master left without slots. This means that we were\n     *    failed over and we should turn into a replica of the new\n     *    master.\n     * 2) We are a slave and our master is left without slots. We need\n     *    to replicate to the new slots owner. */\n    if (newmaster && curmaster->numslots == 0 &&\n            (g_pserver->cluster_allow_replica_migration ||\n             sender_slots == migrated_our_slots)) {\n        serverLog(LL_WARNING,\n            \"Configuration change detected. Reconfiguring myself \"\n            \"as a replica of %.40s\", sender->name);\n        clusterSetMaster(sender);\n        clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG|\n                             CLUSTER_TODO_UPDATE_STATE|\n                             CLUSTER_TODO_FSYNC_CONFIG);\n    } else if (dirty_slots_count) {\n        /* If we are here, we received an update message which removed\n         * ownership for certain slots we still have keys about, but still\n         * we are serving some slots, so this master node was not demoted to\n         * a slave.\n         *\n         * In order to maintain a consistent state between keys and slots\n         * we need to remove all the keys from the slots we lost. */\n        for (j = 0; j < dirty_slots_count; j++)\n            delKeysInSlot(dirty_slots[j]);\n    }\n}\n\n/* When this function is called, there is a packet to process starting\n * at node->rcvbuf. Releasing the buffer is up to the caller, so this\n * function should just handle the higher level stuff of processing the\n * packet, modifying the cluster state if needed.\n *\n * The function returns 1 if the link is still valid after the packet\n * was processed, otherwise 0 if the link was freed since the packet\n * processing lead to some inconsistency error (for instance a PONG\n * received from the wrong sender ID). */\nint clusterProcessPacket(clusterLink *link) {\n    serverAssert(ielFromEventLoop(serverTL->el) == IDX_EVENT_LOOP_MAIN);\n\n    clusterMsg *hdr = (clusterMsg*) link->rcvbuf;\n    uint32_t totlen = ntohl(hdr->totlen);\n    uint16_t type = ntohs(hdr->type);\n    mstime_t now = mstime();\n\n    if (type < CLUSTERMSG_TYPE_COUNT)\n        g_pserver->cluster->stats_bus_messages_received[type]++;\n    serverLog(LL_DEBUG,\"--- Processing packet of type %d, %lu bytes\",\n        type, (unsigned long) totlen);\n\n    /* Perform sanity checks */\n    if (totlen < 16) return 1; /* At least signature, version, totlen, count. */\n    if (totlen > link->rcvbuf_len) return 1;\n\n    if (ntohs(hdr->ver) != CLUSTER_PROTO_VER) {\n        /* Can't handle messages of different versions. */\n        return 1;\n    }\n\n    uint16_t flags = ntohs(hdr->flags);\n    uint64_t senderCurrentEpoch = 0, senderConfigEpoch = 0;\n    clusterNode *sender;\n\n    if (type == CLUSTERMSG_TYPE_PING || type == CLUSTERMSG_TYPE_PONG ||\n        type == CLUSTERMSG_TYPE_MEET)\n    {\n        uint16_t count = ntohs(hdr->count);\n        uint32_t explen; /* expected length of this packet */\n\n        explen = sizeof(clusterMsg)-sizeof(union clusterMsgData);\n        explen += (sizeof(clusterMsgDataGossip)*count);\n        if (totlen != explen) return 1;\n    } else if (type == CLUSTERMSG_TYPE_FAIL) {\n        uint32_t explen = sizeof(clusterMsg)-sizeof(union clusterMsgData);\n\n        explen += sizeof(clusterMsgDataFail);\n        if (totlen != explen) return 1;\n    } else if (type == CLUSTERMSG_TYPE_PUBLISH) {\n        uint32_t explen = sizeof(clusterMsg)-sizeof(union clusterMsgData);\n\n        explen += sizeof(clusterMsgDataPublish) -\n                8 +\n                ntohl(hdr->data.publish.msg.channel_len) +\n                ntohl(hdr->data.publish.msg.message_len);\n        if (totlen != explen) return 1;\n    } else if (type == CLUSTERMSG_TYPE_FAILOVER_AUTH_REQUEST ||\n               type == CLUSTERMSG_TYPE_FAILOVER_AUTH_ACK ||\n               type == CLUSTERMSG_TYPE_MFSTART)\n    {\n        uint32_t explen = sizeof(clusterMsg)-sizeof(union clusterMsgData);\n\n        if (totlen != explen) return 1;\n    } else if (type == CLUSTERMSG_TYPE_UPDATE) {\n        uint32_t explen = sizeof(clusterMsg)-sizeof(union clusterMsgData);\n\n        explen += sizeof(clusterMsgDataUpdate);\n        if (totlen != explen) return 1;\n    } else if (type == CLUSTERMSG_TYPE_MODULE) {\n        uint32_t explen = sizeof(clusterMsg)-sizeof(union clusterMsgData);\n\n        explen += sizeof(clusterMsgModule) -\n                3 + ntohl(hdr->data.module.msg.len);\n        if (totlen != explen) return 1;\n    }\n\n    /* Check if the sender is a known node. Note that for incoming connections\n     * we don't store link->node information, but resolve the node by the\n     * ID in the header each time in the current implementation. */\n    sender = clusterLookupNode(hdr->sender);\n\n    /* Update the last time we saw any data from this node. We\n     * use this in order to avoid detecting a timeout from a node that\n     * is just sending a lot of data in the cluster bus, for instance\n     * because of Pub/Sub. */\n    if (sender) sender->data_received = now;\n\n    if (sender && !nodeInHandshake(sender)) {\n        /* Update our currentEpoch if we see a newer epoch in the cluster. */\n        senderCurrentEpoch = ntohu64(hdr->currentEpoch);\n        senderConfigEpoch = ntohu64(hdr->configEpoch);\n        if (senderCurrentEpoch > g_pserver->cluster->currentEpoch)\n            g_pserver->cluster->currentEpoch = senderCurrentEpoch;\n        /* Update the sender configEpoch if it is publishing a newer one. */\n        if (senderConfigEpoch > sender->configEpoch) {\n            sender->configEpoch = senderConfigEpoch;\n            clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG|\n                                 CLUSTER_TODO_FSYNC_CONFIG);\n        }\n        /* Update the replication offset info for this node. */\n        sender->repl_offset = ntohu64(hdr->offset);\n        sender->repl_offset_time = now;\n        /* If we are a slave performing a manual failover and our master\n         * sent its offset while already paused, populate the MF state. */\n        if (g_pserver->cluster->mf_end &&\n            nodeIsSlave(myself) &&\n            myself->slaveof == sender &&\n            hdr->mflags[0] & CLUSTERMSG_FLAG0_PAUSED &&\n            g_pserver->cluster->mf_master_offset == -1)\n        {\n            g_pserver->cluster->mf_master_offset = sender->repl_offset;\n            clusterDoBeforeSleep(CLUSTER_TODO_HANDLE_MANUALFAILOVER);\n            serverLog(LL_WARNING,\n                \"Received replication offset for paused \"\n                \"master manual failover: %lld\",\n                g_pserver->cluster->mf_master_offset);\n        }\n    }\n\n    /* Initial processing of PING and MEET requests replying with a PONG. */\n    if (type == CLUSTERMSG_TYPE_PING || type == CLUSTERMSG_TYPE_MEET) {\n        serverLog(LL_DEBUG,\"Ping packet received: %p\", (void*)link->node);\n\n        /* We use incoming MEET messages in order to set the address\n         * for 'myself', since only other cluster nodes will send us\n         * MEET messages on handshakes, when the cluster joins, or\n         * later if we changed address, and those nodes will use our\n         * official address to connect to us. So by obtaining this address\n         * from the socket is a simple way to discover / update our own\n         * address in the cluster without it being hardcoded in the config.\n         *\n         * However if we don't have an address at all, we update the address\n         * even with a normal PING packet. If it's wrong it will be fixed\n         * by MEET later. */\n        if ((type == CLUSTERMSG_TYPE_MEET || myself->ip[0] == '\\0') &&\n            g_pserver->cluster_announce_ip == NULL)\n        {\n            char ip[NET_IP_STR_LEN];\n\n            if (connSockName(link->conn,ip,sizeof(ip),NULL) != -1 &&\n                strcmp(ip,myself->ip))\n            {\n                memcpy(myself->ip,ip,NET_IP_STR_LEN);\n                serverLog(LL_WARNING,\"IP address for this node updated to %s\",\n                    myself->ip);\n                clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG);\n            }\n        }\n\n        /* Add this node if it is new for us and the msg type is MEET.\n         * In this stage we don't try to add the node with the right\n         * flags, slaveof pointer, and so forth, as this details will be\n         * resolved when we'll receive PONGs from the node. */\n        if (!sender && type == CLUSTERMSG_TYPE_MEET) {\n            clusterNode *node;\n\n            node = createClusterNode(NULL,CLUSTER_NODE_HANDSHAKE);\n            nodeIp2String(node->ip,link,hdr->myip);\n            node->port = ntohs(hdr->port);\n            node->pport = ntohs(hdr->pport);\n            node->cport = ntohs(hdr->cport);\n            clusterAddNode(node);\n            clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG);\n        }\n\n        /* If this is a MEET packet from an unknown node, we still process\n         * the gossip section here since we have to trust the sender because\n         * of the message type. */\n        if (!sender && type == CLUSTERMSG_TYPE_MEET)\n            clusterProcessGossipSection(hdr,link);\n\n        /* Anyway reply with a PONG */\n        clusterSendPing(link,CLUSTERMSG_TYPE_PONG);\n    }\n\n    /* PING, PONG, MEET: process config information. */\n    if (type == CLUSTERMSG_TYPE_PING || type == CLUSTERMSG_TYPE_PONG ||\n        type == CLUSTERMSG_TYPE_MEET)\n    {\n        serverLog(LL_DEBUG,\"%s packet received: %p\",\n            type == CLUSTERMSG_TYPE_PING ? \"ping\" : \"pong\",\n            (void*)link->node);\n        if (link->node) {\n            if (nodeInHandshake(link->node)) {\n                /* If we already have this node, try to change the\n                 * IP/port of the node with the new one. */\n                if (sender) {\n                    serverLog(LL_VERBOSE,\n                        \"Handshake: we already know node %.40s, \"\n                        \"updating the address if needed.\", sender->name);\n                    if (nodeUpdateAddressIfNeeded(sender,link,hdr))\n                    {\n                        clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG|\n                                             CLUSTER_TODO_UPDATE_STATE);\n                    }\n                    /* Free this node as we already have it. This will\n                     * cause the link to be freed as well. */\n                    clusterDelNode(link->node);\n                    return 0;\n                }\n\n                /* First thing to do is replacing the random name with the\n                 * right node name if this was a handshake stage. */\n                clusterRenameNode(link->node, hdr->sender);\n                serverLog(LL_DEBUG,\"Handshake with node %.40s completed.\",\n                    link->node->name);\n                link->node->flags &= ~CLUSTER_NODE_HANDSHAKE;\n                link->node->flags |= flags&(CLUSTER_NODE_MASTER|CLUSTER_NODE_SLAVE);\n                clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG);\n            } else if (memcmp(link->node->name,hdr->sender,\n                        CLUSTER_NAMELEN) != 0)\n            {\n                /* If the reply has a non matching node ID we\n                 * disconnect this node and set it as not having an associated\n                 * address. */\n                serverLog(LL_DEBUG,\"PONG contains mismatching sender ID. About node %.40s added %d ms ago, having flags %d\",\n                    link->node->name,\n                    (int)(now-(link->node->ctime)),\n                    link->node->flags);\n                link->node->flags |= CLUSTER_NODE_NOADDR;\n                link->node->ip[0] = '\\0';\n                link->node->port = 0;\n                link->node->pport = 0;\n                link->node->cport = 0;\n                freeClusterLink(link);\n                clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG);\n                return 0;\n            }\n        }\n\n        /* Copy the CLUSTER_NODE_NOFAILOVER flag from what the sender\n         * announced. This is a dynamic flag that we receive from the\n         * sender, and the latest status must be trusted. We need it to\n         * be propagated because the slave ranking used to understand the\n         * delay of each slave in the voting process, needs to know\n         * what are the instances really competing. */\n        if (sender) {\n            int nofailover = flags & CLUSTER_NODE_NOFAILOVER;\n            sender->flags &= ~CLUSTER_NODE_NOFAILOVER;\n            sender->flags |= nofailover;\n        }\n\n        /* Update the node address if it changed. */\n        if (sender && type == CLUSTERMSG_TYPE_PING &&\n            !nodeInHandshake(sender) &&\n            nodeUpdateAddressIfNeeded(sender,link,hdr))\n        {\n            clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG|\n                                 CLUSTER_TODO_UPDATE_STATE);\n        }\n\n        /* Update our info about the node */\n        if (link->node && type == CLUSTERMSG_TYPE_PONG) {\n            link->node->pong_received = now;\n            link->node->ping_sent = 0;\n\n            /* The PFAIL condition can be reversed without external\n             * help if it is momentary (that is, if it does not\n             * turn into a FAIL state).\n             *\n             * The FAIL condition is also reversible under specific\n             * conditions detected by clearNodeFailureIfNeeded(). */\n            if (nodeTimedOut(link->node)) {\n                link->node->flags &= ~CLUSTER_NODE_PFAIL;\n                clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG|\n                                     CLUSTER_TODO_UPDATE_STATE);\n            } else if (nodeFailed(link->node)) {\n                clearNodeFailureIfNeeded(link->node);\n            }\n        }\n\n        /* Check for role switch: slave -> master or master -> slave. */\n        if (sender) {\n            if (!memcmp(hdr->slaveof,CLUSTER_NODE_NULL_NAME,\n                sizeof(hdr->slaveof)))\n            {\n                /* Node is a master. */\n                clusterSetNodeAsMaster(sender);\n            } else {\n                /* Node is a slave. */\n                clusterNode *master = clusterLookupNode(hdr->slaveof);\n\n                if (nodeIsMaster(sender)) {\n                    /* Master turned into a slave! Reconfigure the node. */\n                    clusterDelNodeSlots(sender);\n                    sender->flags &= ~(CLUSTER_NODE_MASTER|\n                                       CLUSTER_NODE_MIGRATE_TO);\n                    sender->flags |= CLUSTER_NODE_SLAVE;\n\n                    /* Update config and state. */\n                    clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG|\n                                         CLUSTER_TODO_UPDATE_STATE);\n                }\n\n                /* Master node changed for this slave? */\n                if (master && sender->slaveof != master) {\n                    if (sender->slaveof)\n                        clusterNodeRemoveSlave(sender->slaveof,sender);\n                    clusterNodeAddSlave(master,sender);\n                    sender->slaveof = master;\n\n                    /* Update config. */\n                    clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG);\n                }\n            }\n        }\n\n        /* Update our info about served slots.\n         *\n         * Note: this MUST happen after we update the master/slave state\n         * so that CLUSTER_NODE_MASTER flag will be set. */\n\n        /* Many checks are only needed if the set of served slots this\n         * instance claims is different compared to the set of slots we have\n         * for it. Check this ASAP to avoid other computational expansive\n         * checks later. */\n        clusterNode *sender_master = NULL; /* Sender or its master if slave. */\n        int dirty_slots = 0; /* Sender claimed slots don't match my view? */\n\n        if (sender) {\n            sender_master = nodeIsMaster(sender) ? sender : sender->slaveof;\n            if (sender_master) {\n                dirty_slots = memcmp(sender_master->slots,\n                        hdr->myslots,sizeof(hdr->myslots)) != 0;\n            }\n        }\n\n        /* 1) If the sender of the message is a master, and we detected that\n         *    the set of slots it claims changed, scan the slots to see if we\n         *    need to update our configuration. */\n        if (sender && nodeIsMaster(sender) && dirty_slots)\n            clusterUpdateSlotsConfigWith(sender,senderConfigEpoch,hdr->myslots);\n\n        /* 2) We also check for the reverse condition, that is, the sender\n         *    claims to serve slots we know are served by a master with a\n         *    greater configEpoch. If this happens we inform the sender.\n         *\n         * This is useful because sometimes after a partition heals, a\n         * reappearing master may be the last one to claim a given set of\n         * hash slots, but with a configuration that other instances know to\n         * be deprecated. Example:\n         *\n         * A and B are master and slave for slots 1,2,3.\n         * A is partitioned away, B gets promoted.\n         * B is partitioned away, and A returns available.\n         *\n         * Usually B would PING A publishing its set of served slots and its\n         * configEpoch, but because of the partition B can't inform A of the\n         * new configuration, so other nodes that have an updated table must\n         * do it. In this way A will stop to act as a master (or can try to\n         * failover if there are the conditions to win the election). */\n        if (sender && dirty_slots) {\n            int j;\n\n            for (j = 0; j < CLUSTER_SLOTS; j++) {\n                if (bitmapTestBit(hdr->myslots,j)) {\n                    if (g_pserver->cluster->slots[j] == sender ||\n                        g_pserver->cluster->slots[j] == NULL) continue;\n                    if (g_pserver->cluster->slots[j]->configEpoch >\n                        senderConfigEpoch)\n                    {\n                        serverLog(LL_VERBOSE,\n                            \"Node %.40s has old slots configuration, sending \"\n                            \"an UPDATE message about %.40s\",\n                                sender->name, g_pserver->cluster->slots[j]->name);\n                        clusterSendUpdate(sender->link,\n                            g_pserver->cluster->slots[j]);\n\n                        /* TODO: instead of exiting the loop send every other\n                         * UPDATE packet for other nodes that are the new owner\n                         * of sender's slots. */\n                        break;\n                    }\n                }\n            }\n        }\n\n        /* If our config epoch collides with the sender's try to fix\n         * the problem. */\n        if (sender &&\n            nodeIsMaster(myself) && nodeIsMaster(sender) &&\n            senderConfigEpoch == myself->configEpoch)\n        {\n            clusterHandleConfigEpochCollision(sender);\n        }\n\n        /* Get info from the gossip section */\n        if (sender) clusterProcessGossipSection(hdr,link);\n    } else if (type == CLUSTERMSG_TYPE_FAIL) {\n        clusterNode *failing;\n\n        if (sender) {\n            failing = clusterLookupNode(hdr->data.fail.about.nodename);\n            if (failing &&\n                !(failing->flags & (CLUSTER_NODE_FAIL|CLUSTER_NODE_MYSELF)))\n            {\n                serverLog(LL_NOTICE,\n                    \"FAIL message received from %.40s about %.40s\",\n                    hdr->sender, hdr->data.fail.about.nodename);\n                failing->flags |= CLUSTER_NODE_FAIL;\n                failing->fail_time = now;\n                failing->flags &= ~CLUSTER_NODE_PFAIL;\n                clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG|\n                                     CLUSTER_TODO_UPDATE_STATE);\n            }\n        } else {\n            serverLog(LL_NOTICE,\n                \"Ignoring FAIL message from unknown node %.40s about %.40s\",\n                hdr->sender, hdr->data.fail.about.nodename);\n        }\n    } else if (type == CLUSTERMSG_TYPE_PUBLISH) {\n        robj *channel, *message;\n        uint32_t channel_len, message_len;\n\n        /* Don't bother creating useless objects if there are no\n         * Pub/Sub subscribers. */\n        if (dictSize(g_pserver->pubsub_channels) ||\n           dictSize(g_pserver->pubsub_patterns))\n        {\n            channel_len = ntohl(hdr->data.publish.msg.channel_len);\n            message_len = ntohl(hdr->data.publish.msg.message_len);\n            channel = createStringObject(\n                        (char*)hdr->data.publish.msg.bulk_data,channel_len);\n            message = createStringObject(\n                        (char*)hdr->data.publish.msg.bulk_data+channel_len,\n                        message_len);\n            pubsubPublishMessage(channel,message);\n            decrRefCount(channel);\n            decrRefCount(message);\n        }\n    } else if (type == CLUSTERMSG_TYPE_FAILOVER_AUTH_REQUEST) {\n        if (!sender) return 1;  /* We don't know that node. */\n        clusterSendFailoverAuthIfNeeded(sender,hdr);\n    } else if (type == CLUSTERMSG_TYPE_FAILOVER_AUTH_ACK) {\n        if (!sender) return 1;  /* We don't know that node. */\n        /* We consider this vote only if the sender is a master serving\n         * a non zero number of slots, and its currentEpoch is greater or\n         * equal to epoch where this node started the election. */\n        if (nodeIsMaster(sender) && sender->numslots > 0 &&\n            senderCurrentEpoch >= g_pserver->cluster->failover_auth_epoch)\n        {\n            g_pserver->cluster->failover_auth_count++;\n            /* Maybe we reached a quorum here, set a flag to make sure\n             * we check ASAP. */\n            clusterDoBeforeSleep(CLUSTER_TODO_HANDLE_FAILOVER);\n        }\n    } else if (type == CLUSTERMSG_TYPE_MFSTART) {\n        /* This message is acceptable only if I'm a master and the sender\n         * is one of my slaves. */\n        if (!sender || sender->slaveof != myself) return 1;\n        /* Manual failover requested from slaves. Initialize the state\n         * accordingly. */\n        resetManualFailover();\n        g_pserver->cluster->mf_end = now + CLUSTER_MF_TIMEOUT;\n        g_pserver->cluster->mf_slave = sender;\n        pauseClients(now+(CLUSTER_MF_TIMEOUT*CLUSTER_MF_PAUSE_MULT),CLIENT_PAUSE_WRITE);\n        serverLog(LL_WARNING,\"Manual failover requested by replica %.40s.\",\n            sender->name);\n        /* We need to send a ping message to the replica, as it would carry\n         * `server.cluster->mf_master_offset`, which means the master paused clients\n         * at offset `server.cluster->mf_master_offset`, so that the replica would\n         * know that it is safe to set its `server.cluster->mf_can_start` to 1 so as\n         * to complete failover as quickly as possible. */\n        clusterSendPing(link, CLUSTERMSG_TYPE_PING);\n    } else if (type == CLUSTERMSG_TYPE_UPDATE) {\n        clusterNode *n; /* The node the update is about. */\n        uint64_t reportedConfigEpoch =\n                    ntohu64(hdr->data.update.nodecfg.configEpoch);\n\n        if (!sender) return 1;  /* We don't know the sender. */\n        n = clusterLookupNode(hdr->data.update.nodecfg.nodename);\n        if (!n) return 1;   /* We don't know the reported node. */\n        if (n->configEpoch >= reportedConfigEpoch) return 1; /* Nothing new. */\n\n        /* If in our current config the node is a slave, set it as a master. */\n        if (nodeIsSlave(n)) clusterSetNodeAsMaster(n);\n\n        /* Update the node's configEpoch. */\n        n->configEpoch = reportedConfigEpoch;\n        clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG|\n                             CLUSTER_TODO_FSYNC_CONFIG);\n\n        /* Check the bitmap of served slots and update our\n         * config accordingly. */\n        clusterUpdateSlotsConfigWith(n,reportedConfigEpoch,\n            hdr->data.update.nodecfg.slots);\n    } else if (type == CLUSTERMSG_TYPE_MODULE) {\n        if (!sender) return 1;  /* Protect the module from unknown nodes. */\n        /* We need to route this message back to the right module subscribed\n         * for the right message type. */\n        uint64_t module_id = hdr->data.module.msg.module_id; /* Endian-safe ID */\n        uint32_t len = ntohl(hdr->data.module.msg.len);\n        uint8_t type = hdr->data.module.msg.type;\n        unsigned char *payload = hdr->data.module.msg.bulk_data;\n        moduleCallClusterReceivers(sender->name,module_id,type,payload,len);\n    } else {\n        serverLog(LL_WARNING,\"Received unknown packet type: %d\", type);\n    }\n    return 1;\n}\n\n/* This function is called when we detect the link with this node is lost.\n   We set the node as no longer connected. The Cluster Cron will detect\n   this connection and will try to get it connected again.\n\n   Instead if the node is a temporary node used to accept a query, we\n   completely free the node on error. */\nvoid handleLinkIOError(clusterLink *link) {\n    freeClusterLink(link);\n}\n\n/* Send data. This is handled using a trivial send buffer that gets\n * consumed by write(). We don't try to optimize this for speed too much\n * as this is a very low traffic channel. */\nvoid clusterWriteHandler(connection *conn) {\n    serverAssert(ielFromEventLoop(serverTL->el) == IDX_EVENT_LOOP_MAIN);\n    clusterLink *link = (clusterLink*)connGetPrivateData(conn);\n    ssize_t nwritten;\n\n    nwritten = connWrite(conn, link->sndbuf, sdslen(link->sndbuf));\n    if (nwritten <= 0) {\n        serverLog(LL_DEBUG,\"I/O error writing to node link: %s\",\n            (nwritten == -1) ? connGetLastError(conn) : \"short write\");\n        handleLinkIOError(link);\n        return;\n    }\n    sdsrange(link->sndbuf,nwritten,-1);\n    if (sdslen(link->sndbuf) == 0)\n        connSetWriteHandler(link->conn, NULL);\n}\n\n/* A connect handler that gets called when a connection to another node\n * gets established.\n */\nvoid clusterLinkConnectHandler(connection *conn) {\n    clusterLink *link = (clusterLink*)connGetPrivateData(conn);\n    clusterNode *node = link->node;\n    if (node == nullptr)\n        return; // we're about to be freed\n\n    /* Check if connection succeeded */\n    if (connGetState(conn) != CONN_STATE_CONNECTED) {\n        serverLog(LL_VERBOSE, \"Connection with Node %.40s at %s:%d failed: %s\",\n                node->name, node->ip, node->cport,\n                connGetLastError(conn));\n        freeClusterLink(link);\n        return;\n    }\n\n    /* Register a read handler from now on */\n    connSetReadHandler(conn, clusterReadHandler);\n\n    /* Queue a PING in the new connection ASAP: this is crucial\n     * to avoid false positives in failure detection.\n     *\n     * If the node is flagged as MEET, we send a MEET message instead\n     * of a PING one, to force the receiver to add us in its node\n     * table. */\n    mstime_t old_ping_sent = node->ping_sent;\n    clusterSendPing(link, node->flags & CLUSTER_NODE_MEET ?\n            CLUSTERMSG_TYPE_MEET : CLUSTERMSG_TYPE_PING);\n    if (old_ping_sent) {\n        /* If there was an active ping before the link was\n         * disconnected, we want to restore the ping time, otherwise\n         * replaced by the clusterSendPing() call. */\n        node->ping_sent = old_ping_sent;\n    }\n    /* We can clear the flag after the first packet is sent.\n     * If we'll never receive a PONG, we'll never send new packets\n     * to this node. Instead after the PONG is received and we\n     * are no longer in meet/handshake status, we want to send\n     * normal PING packets. */\n    node->flags &= ~CLUSTER_NODE_MEET;\n\n    serverLog(LL_DEBUG,\"Connecting with Node %.40s at %s:%d\",\n            node->name, node->ip, node->cport);\n}\n\n/* Read data. Try to read the first field of the header first to check the\n * full length of the packet. When a whole packet is in memory this function\n * will call the function to process the packet. And so forth. */\nvoid clusterReadHandler(connection *conn) {\n    clusterMsg buf[1];\n    ssize_t nread;\n    clusterMsg *hdr;\n    clusterLink *link = (clusterLink*)connGetPrivateData(conn);\n    unsigned int readlen, rcvbuflen;\n\n    while(1) { /* Read as long as there is data to read. */\n        rcvbuflen = link->rcvbuf_len;\n        if (rcvbuflen < 8) {\n            /* First, obtain the first 8 bytes to get the full message\n             * length. */\n            readlen = 8 - rcvbuflen;\n        } else {\n            /* Finally read the full message. */\n            hdr = (clusterMsg*) link->rcvbuf;\n            if (rcvbuflen == 8) {\n                /* Perform some sanity check on the message signature\n                 * and length. */\n                if (memcmp(hdr->sig,\"RCmb\",4) != 0 ||\n                    ntohl(hdr->totlen) < CLUSTERMSG_MIN_LEN)\n                {\n                    serverLog(LL_WARNING,\n                        \"Bad message length or signature received \"\n                        \"from Cluster bus.\");\n                    handleLinkIOError(link);\n                    return;\n                }\n            }\n            readlen = ntohl(hdr->totlen) - rcvbuflen;\n            if (readlen > sizeof(buf)) readlen = sizeof(buf);\n        }\n\n        nread = connRead(conn,buf,readlen);\n        if (nread == -1 && (connGetState(conn) == CONN_STATE_CONNECTED)) return; /* No more data ready. */\n\n        if (nread <= 0) {\n            /* I/O error... */\n            serverLog(LL_DEBUG,\"I/O error reading from node link: %s\",\n                (nread == 0) ? \"connection closed\" : connGetLastError(conn));\n            handleLinkIOError(link);\n            return;\n        } else {\n            /* Read data and recast the pointer to the new buffer. */\n            size_t unused = link->rcvbuf_alloc - link->rcvbuf_len;\n            if ((size_t)nread > unused) {\n                size_t required = link->rcvbuf_len + nread;\n                /* If less than 1mb, grow to twice the needed size, if larger grow by 1mb. */\n                link->rcvbuf_alloc = required < RCVBUF_MAX_PREALLOC ? required * 2: required + RCVBUF_MAX_PREALLOC;\n                link->rcvbuf = (char*)zrealloc(link->rcvbuf, link->rcvbuf_alloc);\n            }\n            memcpy(link->rcvbuf + link->rcvbuf_len, buf, nread);\n            link->rcvbuf_len += nread;\n            hdr = (clusterMsg*) link->rcvbuf;\n            rcvbuflen += nread;\n        }\n\n        /* Total length obtained? Process this packet. */\n        if (rcvbuflen >= 8 && rcvbuflen == ntohl(hdr->totlen)) {\n            if (clusterProcessPacket(link)) {\n                if (link->rcvbuf_alloc > RCVBUF_INIT_LEN) {\n                    zfree(link->rcvbuf);\n                    link->rcvbuf = (char*)zmalloc(link->rcvbuf_alloc = RCVBUF_INIT_LEN);\n                }\n                link->rcvbuf_len = 0;\n            } else {\n                return; /* Link no longer valid. */\n            }\n        }\n    }\n}\n\n/* Put stuff into the send buffer.\n *\n * It is guaranteed that this function will never have as a side effect\n * the link to be invalidated, so it is safe to call this function\n * from event handlers that will do stuff with the same link later. */\nvoid clusterSendMessage(clusterLink *link, unsigned char *msg, size_t msglen) {\n    serverAssert(GlobalLocksAcquired());\n    if (sdslen(link->sndbuf) == 0 && msglen != 0)\n    {\n        aePostFunction(g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].el, [link] {\n            /* The connection could be timed out before this posted function executes (thanks to TCP keepalive).\n             * So check that the connection is still there before setting the write handler, otherwise you segfault */\n            if (link->conn != nullptr)\n                connSetWriteHandlerWithBarrier(link->conn, clusterWriteHandler, 1);\n        });\n    }\n\n    link->sndbuf = sdscatlen(link->sndbuf, msg, msglen);\n\n    /* Populate sent messages stats. */\n    clusterMsg *hdr = (clusterMsg*) msg;\n    uint16_t type = ntohs(hdr->type);\n    if (type < CLUSTERMSG_TYPE_COUNT)\n        g_pserver->cluster->stats_bus_messages_sent[type]++;\n}\n\n/* Send a message to all the nodes that are part of the cluster having\n * a connected link.\n *\n * It is guaranteed that this function will never have as a side effect\n * some node->link to be invalidated, so it is safe to call this function\n * from event handlers that will do stuff with node links later. */\nvoid clusterBroadcastMessage(void *buf, size_t len) {\n    dictIterator *di;\n    dictEntry *de;\n\n    di = dictGetSafeIterator(g_pserver->cluster->nodes);\n    while((de = dictNext(di)) != NULL) {\n        clusterNode *node = (clusterNode*)dictGetVal(de);\n\n        if (!node->link) continue;\n        if (node->flags & (CLUSTER_NODE_MYSELF|CLUSTER_NODE_HANDSHAKE))\n            continue;\n        clusterSendMessage(node->link,(unsigned char*)buf,len);\n    }\n    dictReleaseIterator(di);\n}\n\n/* Build the message header. hdr must point to a buffer at least\n * sizeof(clusterMsg) in bytes. */\nvoid clusterBuildMessageHdr(clusterMsg *hdr, int type) {\n    int totlen = 0;\n    uint64_t offset;\n    clusterNode *master;\n\n    /* If this node is a master, we send its slots bitmap and configEpoch.\n     * If this node is a slave we send the master's information instead (the\n     * node is flagged as slave so the receiver knows that it is NOT really\n     * in charge for this slots. */\n    master = (nodeIsSlave(myself) && myself->slaveof) ?\n              myself->slaveof : myself;\n\n    memset(hdr,0,sizeof(*hdr));\n    hdr->ver = htons(CLUSTER_PROTO_VER);\n    hdr->sig[0] = 'R';\n    hdr->sig[1] = 'C';\n    hdr->sig[2] = 'm';\n    hdr->sig[3] = 'b';\n    hdr->type = htons(type);\n    memcpy(hdr->sender,myself->name,CLUSTER_NAMELEN);\n\n    /* If cluster-announce-ip option is enabled, force the receivers of our\n     * packets to use the specified address for this node. Otherwise if the\n     * first byte is zero, they'll do auto discovery. */\n    memset(hdr->myip,0,NET_IP_STR_LEN);\n    if (g_pserver->cluster_announce_ip) {\n        strncpy(hdr->myip,g_pserver->cluster_announce_ip,NET_IP_STR_LEN-1);\n        hdr->myip[NET_IP_STR_LEN-1] = '\\0';\n    }\n\n    /* Handle cluster-announce-[tls-|bus-]port. */\n    int announced_port, announced_pport, announced_cport;\n    deriveAnnouncedPorts(&announced_port, &announced_pport, &announced_cport);\n\n    memcpy(hdr->myslots,master->slots,sizeof(hdr->myslots));\n    memset(hdr->slaveof,0,CLUSTER_NAMELEN);\n    if (myself->slaveof != NULL)\n        memcpy(hdr->slaveof,myself->slaveof->name, CLUSTER_NAMELEN);\n    hdr->port = htons(announced_port);\n    hdr->pport = htons(announced_pport);\n    hdr->cport = htons(announced_cport);\n    hdr->flags = htons(myself->flags);\n    hdr->state = g_pserver->cluster->state;\n\n    /* Set the currentEpoch and configEpochs. */\n    hdr->currentEpoch = htonu64(g_pserver->cluster->currentEpoch);\n    hdr->configEpoch = htonu64(master->configEpoch);\n\n    /* Set the replication offset. */\n    if (nodeIsSlave(myself))\n        offset = replicationGetSlaveOffset(getFirstMaster());\n    else\n        offset = g_pserver->master_repl_offset;\n    hdr->offset = htonu64(offset);\n\n    /* Set the message flags. */\n    if (nodeIsMaster(myself) && g_pserver->cluster->mf_end)\n        hdr->mflags[0] |= CLUSTERMSG_FLAG0_PAUSED;\n\n    /* Compute the message length for certain messages. For other messages\n     * this is up to the caller. */\n    if (type == CLUSTERMSG_TYPE_FAIL) {\n        totlen = sizeof(clusterMsg)-sizeof(union clusterMsgData);\n        totlen += sizeof(clusterMsgDataFail);\n    } else if (type == CLUSTERMSG_TYPE_UPDATE) {\n        totlen = sizeof(clusterMsg)-sizeof(union clusterMsgData);\n        totlen += sizeof(clusterMsgDataUpdate);\n    }\n    hdr->totlen = htonl(totlen);\n    /* For PING, PONG, and MEET, fixing the totlen field is up to the caller. */\n}\n\n/* Return non zero if the node is already present in the gossip section of the\n * message pointed by 'hdr' and having 'count' gossip entries. Otherwise\n * zero is returned. Helper for clusterSendPing(). */\nint clusterNodeIsInGossipSection(clusterMsg *hdr, int count, clusterNode *n) {\n    int j;\n    for (j = 0; j < count; j++) {\n        if (memcmp(hdr->data.ping.gossip[j].nodename,n->name,\n                CLUSTER_NAMELEN) == 0) break;\n    }\n    return j != count;\n}\n\n/* Set the i-th entry of the gossip section in the message pointed by 'hdr'\n * to the info of the specified node 'n'. */\nvoid clusterSetGossipEntry(clusterMsg *hdr, int i, clusterNode *n) {\n    clusterMsgDataGossip *gossip;\n    gossip = &(hdr->data.ping.gossip[i]);\n    memcpy(gossip->nodename,n->name,CLUSTER_NAMELEN);\n    gossip->ping_sent = htonl(n->ping_sent/1000);\n    gossip->pong_received = htonl(n->pong_received/1000);\n    memcpy(gossip->ip,n->ip,sizeof(n->ip));\n    gossip->port = htons(n->port);\n    gossip->cport = htons(n->cport);\n    gossip->flags = htons(n->flags);\n    gossip->pport = htons(n->pport);\n    gossip->notused1 = 0;\n}\n\n/* Send a PING or PONG packet to the specified node, making sure to add enough\n * gossip information. */\nvoid clusterSendPing(clusterLink *link, int type) {\n    unsigned char *buf;\n    clusterMsg *hdr;\n    int gossipcount = 0; /* Number of gossip sections added so far. */\n    int wanted; /* Number of gossip sections we want to append if possible. */\n    int totlen; /* Total packet length. */\n    /* freshnodes is the max number of nodes we can hope to append at all:\n     * nodes available minus two (ourself and the node we are sending the\n     * message to). However practically there may be less valid nodes since\n     * nodes in handshake state, disconnected, are not considered. */\n    int freshnodes = dictSize(g_pserver->cluster->nodes)-2;\n\n    /* How many gossip sections we want to add? 1/10 of the number of nodes\n     * and anyway at least 3. Why 1/10?\n     *\n     * If we have N masters, with N/10 entries, and we consider that in\n     * node_timeout we exchange with each other node at least 4 packets\n     * (we ping in the worst case in node_timeout/2 time, and we also\n     * receive two pings from the host), we have a total of 8 packets\n     * in the node_timeout*2 failure reports validity time. So we have\n     * that, for a single PFAIL node, we can expect to receive the following\n     * number of failure reports (in the specified window of time):\n     *\n     * PROB * GOSSIP_ENTRIES_PER_PACKET * TOTAL_PACKETS:\n     *\n     * PROB = probability of being featured in a single gossip entry,\n     *        which is 1 / NUM_OF_NODES.\n     * ENTRIES = 10.\n     * TOTAL_PACKETS = 2 * 4 * NUM_OF_MASTERS.\n     *\n     * If we assume we have just masters (so num of nodes and num of masters\n     * is the same), with 1/10 we always get over the majority, and specifically\n     * 80% of the number of nodes, to account for many masters failing at the\n     * same time.\n     *\n     * Since we have non-voting slaves that lower the probability of an entry\n     * to feature our node, we set the number of entries per packet as\n     * 10% of the total nodes we have. */\n    wanted = floor(dictSize(g_pserver->cluster->nodes)/10);\n    if (wanted < 3) wanted = 3;\n    if (wanted > freshnodes) wanted = freshnodes;\n\n    /* Include all the nodes in PFAIL state, so that failure reports are\n     * faster to propagate to go from PFAIL to FAIL state. */\n    int pfail_wanted = g_pserver->cluster->stats_pfail_nodes;\n\n    /* Compute the maximum totlen to allocate our buffer. We'll fix the totlen\n     * later according to the number of gossip sections we really were able\n     * to put inside the packet. */\n    totlen = sizeof(clusterMsg)-sizeof(union clusterMsgData);\n    totlen += (sizeof(clusterMsgDataGossip)*(wanted+pfail_wanted));\n    /* Note: clusterBuildMessageHdr() expects the buffer to be always at least\n     * sizeof(clusterMsg) or more. */\n    if (totlen < (int)sizeof(clusterMsg)) totlen = sizeof(clusterMsg);\n    buf = (unsigned char*)zcalloc(totlen, MALLOC_LOCAL);\n    hdr = (clusterMsg*) buf;\n\n    /* Populate the header. */\n    if (link->node && type == CLUSTERMSG_TYPE_PING)\n        link->node->ping_sent = mstime();\n    clusterBuildMessageHdr(hdr,type);\n\n    /* Populate the gossip fields */\n    int maxiterations = wanted*3;\n    while(freshnodes > 0 && gossipcount < wanted && maxiterations--) {\n        dictEntry *de = dictGetRandomKey(g_pserver->cluster->nodes);\n        clusterNode *thisNode = (clusterNode*)dictGetVal(de);\n\n        /* Don't include this node: the whole packet header is about us\n         * already, so we just gossip about other nodes. */\n        if (thisNode == myself) continue;\n\n        /* PFAIL nodes will be added later. */\n        if (thisNode->flags & CLUSTER_NODE_PFAIL) continue;\n\n        /* In the gossip section don't include:\n         * 1) Nodes in HANDSHAKE state.\n         * 3) Nodes with the NOADDR flag set.\n         * 4) Disconnected nodes if they don't have configured slots.\n         */\n        if (thisNode->flags & (CLUSTER_NODE_HANDSHAKE|CLUSTER_NODE_NOADDR) ||\n            (thisNode->link == NULL && thisNode->numslots == 0))\n        {\n            freshnodes--; /* Technically not correct, but saves CPU. */\n            continue;\n        }\n\n        /* Do not add a node we already have. */\n        if (clusterNodeIsInGossipSection(hdr,gossipcount,thisNode)) continue;\n\n        /* Add it */\n        clusterSetGossipEntry(hdr,gossipcount,thisNode);\n        freshnodes--;\n        gossipcount++;\n    }\n\n    /* If there are PFAIL nodes, add them at the end. */\n    if (pfail_wanted) {\n        dictIterator *di;\n        dictEntry *de;\n\n        di = dictGetSafeIterator(g_pserver->cluster->nodes);\n        while((de = dictNext(di)) != NULL && pfail_wanted > 0) {\n            clusterNode *node = (clusterNode*)dictGetVal(de);\n            if (node->flags & CLUSTER_NODE_HANDSHAKE) continue;\n            if (node->flags & CLUSTER_NODE_NOADDR) continue;\n            if (!(node->flags & CLUSTER_NODE_PFAIL)) continue;\n            clusterSetGossipEntry(hdr,gossipcount,node);\n            freshnodes--;\n            gossipcount++;\n            /* We take the count of the slots we allocated, since the\n             * PFAIL stats may not match perfectly with the current number\n             * of PFAIL nodes. */\n            pfail_wanted--;\n        }\n        dictReleaseIterator(di);\n    }\n\n    /* Ready to send... fix the totlen fiend and queue the message in the\n     * output buffer. */\n    totlen = sizeof(clusterMsg)-sizeof(union clusterMsgData);\n    totlen += (sizeof(clusterMsgDataGossip)*gossipcount);\n    hdr->count = htons(gossipcount);\n    hdr->totlen = htonl(totlen);\n    clusterSendMessage(link,buf,totlen);\n    zfree(buf);\n}\n\n/* Send a PONG packet to every connected node that's not in handshake state\n * and for which we have a valid link.\n *\n * In Redis Cluster pongs are not used just for failure detection, but also\n * to carry important configuration information. So broadcasting a pong is\n * useful when something changes in the configuration and we want to make\n * the cluster aware ASAP (for instance after a slave promotion).\n *\n * The 'target' argument specifies the receiving instances using the\n * defines below:\n *\n * CLUSTER_BROADCAST_ALL -> All known instances.\n * CLUSTER_BROADCAST_LOCAL_SLAVES -> All slaves in my master-slaves ring.\n */\n#define CLUSTER_BROADCAST_ALL 0\n#define CLUSTER_BROADCAST_LOCAL_SLAVES 1\nvoid clusterBroadcastPong(int target) {\n    dictIterator *di;\n    dictEntry *de;\n\n    di = dictGetSafeIterator(g_pserver->cluster->nodes);\n    while((de = dictNext(di)) != NULL) {\n        clusterNode *node = (clusterNode*)dictGetVal(de);\n\n        if (!node->link) continue;\n        if (node == myself || nodeInHandshake(node)) continue;\n        if (target == CLUSTER_BROADCAST_LOCAL_SLAVES) {\n            int local_slave =\n                nodeIsSlave(node) && node->slaveof &&\n                (node->slaveof == myself || node->slaveof == myself->slaveof);\n            if (!local_slave) continue;\n        }\n        clusterSendPing(node->link,CLUSTERMSG_TYPE_PONG);\n    }\n    dictReleaseIterator(di);\n}\n\n/* Send a PUBLISH message.\n *\n * If link is NULL, then the message is broadcasted to the whole cluster. */\nvoid clusterSendPublish(clusterLink *link, robj *channel, robj *message) {\n    unsigned char *payload;\n    clusterMsg buf[1];\n    clusterMsg *hdr = (clusterMsg*) buf;\n    uint32_t totlen;\n    uint32_t channel_len, message_len;\n\n    channel = getDecodedObject(channel);\n    message = getDecodedObject(message);\n    channel_len = sdslen(szFromObj(channel));\n    message_len = sdslen(szFromObj(message));\n\n    clusterBuildMessageHdr(hdr,CLUSTERMSG_TYPE_PUBLISH);\n    totlen = sizeof(clusterMsg)-sizeof(union clusterMsgData);\n    totlen += sizeof(clusterMsgDataPublish) - 8 + channel_len + message_len;\n\n    hdr->data.publish.msg.channel_len = htonl(channel_len);\n    hdr->data.publish.msg.message_len = htonl(message_len);\n    hdr->totlen = htonl(totlen);\n\n    /* Try to use the local buffer if possible */\n    if (totlen < sizeof(buf)) {\n        payload = (unsigned char*)buf;\n    } else {\n        payload = (unsigned char*)zmalloc(totlen, MALLOC_LOCAL);\n        memcpy(payload,hdr,sizeof(*hdr));\n        hdr = (clusterMsg*) payload;\n    }\n    memcpy(hdr->data.publish.msg.bulk_data,ptrFromObj(channel),sdslen(szFromObj(channel)));\n    memcpy(hdr->data.publish.msg.bulk_data+sdslen(szFromObj(channel)),\n        ptrFromObj(message),sdslen(szFromObj(message)));\n\n    if (link)\n        clusterSendMessage(link,payload,totlen);\n    else\n        clusterBroadcastMessage(payload,totlen);\n\n    decrRefCount(channel);\n    decrRefCount(message);\n    if (payload != (unsigned char*)buf) zfree(payload);\n}\n\n/* Send a FAIL message to all the nodes we are able to contact.\n * The FAIL message is sent when we detect that a node is failing\n * (CLUSTER_NODE_PFAIL) and we also receive a gossip confirmation of this:\n * we switch the node state to CLUSTER_NODE_FAIL and ask all the other\n * nodes to do the same ASAP. */\nvoid clusterSendFail(char *nodename) {\n    clusterMsg buf[1];\n    clusterMsg *hdr = (clusterMsg*) buf;\n\n    clusterBuildMessageHdr(hdr,CLUSTERMSG_TYPE_FAIL);\n    memcpy(hdr->data.fail.about.nodename,nodename,CLUSTER_NAMELEN);\n    clusterBroadcastMessage(buf,ntohl(hdr->totlen));\n}\n\n/* Send an UPDATE message to the specified link carrying the specified 'node'\n * slots configuration. The node name, slots bitmap, and configEpoch info\n * are included. */\nvoid clusterSendUpdate(clusterLink *link, clusterNode *node) {\n    clusterMsg buf[1];\n    clusterMsg *hdr = (clusterMsg*) buf;\n\n    if (link == NULL) return;\n    clusterBuildMessageHdr(hdr,CLUSTERMSG_TYPE_UPDATE);\n    memcpy(hdr->data.update.nodecfg.nodename,node->name,CLUSTER_NAMELEN);\n    hdr->data.update.nodecfg.configEpoch = htonu64(node->configEpoch);\n    memcpy(hdr->data.update.nodecfg.slots,node->slots,sizeof(node->slots));\n    clusterSendMessage(link,(unsigned char*)buf,ntohl(hdr->totlen));\n}\n\n/* Send a MODULE message.\n *\n * If link is NULL, then the message is broadcasted to the whole cluster. */\nvoid clusterSendModule(clusterLink *link, uint64_t module_id, uint8_t type,\n                       unsigned char *payload, uint32_t len) {\n    unsigned char *heapbuf;\n    clusterMsg buf[1];\n    clusterMsg *hdr = (clusterMsg*) buf;\n    uint32_t totlen;\n\n    clusterBuildMessageHdr(hdr,CLUSTERMSG_TYPE_MODULE);\n    totlen = sizeof(clusterMsg)-sizeof(union clusterMsgData);\n    totlen += sizeof(clusterMsgModule) - 3 + len;\n\n    hdr->data.module.msg.module_id = module_id; /* Already endian adjusted. */\n    hdr->data.module.msg.type = type;\n    hdr->data.module.msg.len = htonl(len);\n    hdr->totlen = htonl(totlen);\n\n    /* Try to use the local buffer if possible */\n    if (totlen < sizeof(buf)) {\n        heapbuf = (unsigned char*)buf;\n    } else {\n        heapbuf = (unsigned char*)zmalloc(totlen, MALLOC_LOCAL);\n        memcpy(heapbuf,hdr,sizeof(*hdr));\n        hdr = (clusterMsg*) heapbuf;\n    }\n    memcpy(hdr->data.module.msg.bulk_data,payload,len);\n\n    if (link)\n        clusterSendMessage(link,heapbuf,totlen);\n    else\n        clusterBroadcastMessage(heapbuf,totlen);\n\n    if (heapbuf != (unsigned char*)buf) zfree(heapbuf);\n}\n\n/* This function gets a cluster node ID string as target, the same way the nodes\n * addresses are represented in the modules side, resolves the node, and sends\n * the message. If the target is NULL the message is broadcasted.\n *\n * The function returns C_OK if the target is valid, otherwise C_ERR is\n * returned. */\nint clusterSendModuleMessageToTarget(const char *target, uint64_t module_id, uint8_t type, unsigned char *payload, uint32_t len) {\n    clusterNode *node = NULL;\n\n    if (target != NULL) {\n        node = clusterLookupNode(target);\n        if (node == NULL || node->link == NULL) return C_ERR;\n    }\n\n    clusterSendModule(target ? node->link : NULL,\n                      module_id, type, payload, len);\n    return C_OK;\n}\n\n/* -----------------------------------------------------------------------------\n * CLUSTER Pub/Sub support\n *\n * For now we do very little, just propagating PUBLISH messages across the whole\n * cluster. In the future we'll try to get smarter and avoiding propagating those\n * messages to hosts without receives for a given channel.\n * -------------------------------------------------------------------------- */\nvoid clusterPropagatePublish(robj *channel, robj *message) {\n    clusterSendPublish(NULL, channel, message);\n}\n\n/* -----------------------------------------------------------------------------\n * SLAVE node specific functions\n * -------------------------------------------------------------------------- */\n\n/* This function sends a FAILOVER_AUTH_REQUEST message to every node in order to\n * see if there is the quorum for this slave instance to failover its failing\n * master.\n *\n * Note that we send the failover request to everybody, master and slave nodes,\n * but only the masters are supposed to reply to our query. */\nvoid clusterRequestFailoverAuth(void) {\n    clusterMsg buf[1];\n    clusterMsg *hdr = (clusterMsg*) buf;\n    uint32_t totlen;\n\n    clusterBuildMessageHdr(hdr,CLUSTERMSG_TYPE_FAILOVER_AUTH_REQUEST);\n    /* If this is a manual failover, set the CLUSTERMSG_FLAG0_FORCEACK bit\n     * in the header to communicate the nodes receiving the message that\n     * they should authorized the failover even if the master is working. */\n    if (g_pserver->cluster->mf_end) hdr->mflags[0] |= CLUSTERMSG_FLAG0_FORCEACK;\n    totlen = sizeof(clusterMsg)-sizeof(union clusterMsgData);\n    hdr->totlen = htonl(totlen);\n    clusterBroadcastMessage(buf,totlen);\n}\n\n/* Send a FAILOVER_AUTH_ACK message to the specified node. */\nvoid clusterSendFailoverAuth(clusterNode *node) {\n    clusterMsg buf[1];\n    clusterMsg *hdr = (clusterMsg*) buf;\n    uint32_t totlen;\n\n    if (!node->link) return;\n    clusterBuildMessageHdr(hdr,CLUSTERMSG_TYPE_FAILOVER_AUTH_ACK);\n    totlen = sizeof(clusterMsg)-sizeof(union clusterMsgData);\n    hdr->totlen = htonl(totlen);\n    clusterSendMessage(node->link,(unsigned char*)buf,totlen);\n}\n\n/* Send a MFSTART message to the specified node. */\nvoid clusterSendMFStart(clusterNode *node) {\n    clusterMsg buf[1];\n    clusterMsg *hdr = (clusterMsg*) buf;\n    uint32_t totlen;\n\n    if (!node->link) return;\n    clusterBuildMessageHdr(hdr,CLUSTERMSG_TYPE_MFSTART);\n    totlen = sizeof(clusterMsg)-sizeof(union clusterMsgData);\n    hdr->totlen = htonl(totlen);\n    clusterSendMessage(node->link,(unsigned char*)buf,totlen);\n}\n\n/* Vote for the node asking for our vote if there are the conditions. */\nvoid clusterSendFailoverAuthIfNeeded(clusterNode *node, clusterMsg *request) {\n    clusterNode *master = node->slaveof;\n    uint64_t requestCurrentEpoch = ntohu64(request->currentEpoch);\n    uint64_t requestConfigEpoch = ntohu64(request->configEpoch);\n    unsigned char *claimed_slots = request->myslots;\n    int force_ack = request->mflags[0] & CLUSTERMSG_FLAG0_FORCEACK;\n    int j;\n\n    /* IF we are not a master serving at least 1 slot, we don't have the\n     * right to vote, as the cluster size in Redis Cluster is the number\n     * of masters serving at least one slot, and quorum is the cluster\n     * size + 1 */\n    if (nodeIsSlave(myself) || myself->numslots == 0) return;\n\n    /* Request epoch must be >= our currentEpoch.\n     * Note that it is impossible for it to actually be greater since\n     * our currentEpoch was updated as a side effect of receiving this\n     * request, if the request epoch was greater. */\n    if (requestCurrentEpoch < g_pserver->cluster->currentEpoch) {\n        serverLog(LL_WARNING,\n            \"Failover auth denied to %.40s: reqEpoch (%llu) < curEpoch(%llu)\",\n            node->name,\n            (unsigned long long) requestCurrentEpoch,\n            (unsigned long long) g_pserver->cluster->currentEpoch);\n        return;\n    }\n\n    /* I already voted for this epoch? Return ASAP. */\n    if (g_pserver->cluster->lastVoteEpoch == g_pserver->cluster->currentEpoch) {\n        serverLog(LL_WARNING,\n                \"Failover auth denied to %.40s: already voted for epoch %llu\",\n                node->name,\n                (unsigned long long) g_pserver->cluster->currentEpoch);\n        return;\n    }\n\n    /* Node must be a slave and its master down.\n     * The master can be non failing if the request is flagged\n     * with CLUSTERMSG_FLAG0_FORCEACK (manual failover). */\n    if (nodeIsMaster(node) || master == NULL ||\n        (!nodeFailed(master) && !force_ack))\n    {\n        if (nodeIsMaster(node)) {\n            serverLog(LL_WARNING,\n                    \"Failover auth denied to %.40s: it is a master node\",\n                    node->name);\n        } else if (master == NULL) {\n            serverLog(LL_WARNING,\n                    \"Failover auth denied to %.40s: I don't know its master\",\n                    node->name);\n        } else if (!nodeFailed(master)) {\n            serverLog(LL_WARNING,\n                    \"Failover auth denied to %.40s: its master is up\",\n                    node->name);\n        }\n        return;\n    }\n\n    /* We did not voted for a slave about this master for two\n     * times the node timeout. This is not strictly needed for correctness\n     * of the algorithm but makes the base case more linear. */\n    if (mstime() - node->slaveof->voted_time < g_pserver->cluster_node_timeout * 2)\n    {\n        serverLog(LL_WARNING,\n                \"Failover auth denied to %.40s: \"\n                \"can't vote about this master before %lld milliseconds\",\n                node->name,\n                (long long) ((g_pserver->cluster_node_timeout*2)-\n                             (mstime() - node->slaveof->voted_time)));\n        return;\n    }\n\n    /* The slave requesting the vote must have a configEpoch for the claimed\n     * slots that is >= the one of the masters currently serving the same\n     * slots in the current configuration. */\n    for (j = 0; j < CLUSTER_SLOTS; j++) {\n        if (bitmapTestBit(claimed_slots, j) == 0) continue;\n        if (g_pserver->cluster->slots[j] == NULL ||\n            g_pserver->cluster->slots[j]->configEpoch <= requestConfigEpoch)\n        {\n            continue;\n        }\n        /* If we reached this point we found a slot that in our current slots\n         * is served by a master with a greater configEpoch than the one claimed\n         * by the slave requesting our vote. Refuse to vote for this slave. */\n        serverLog(LL_WARNING,\n                \"Failover auth denied to %.40s: \"\n                \"slot %d epoch (%llu) > reqEpoch (%llu)\",\n                node->name, j,\n                (unsigned long long) g_pserver->cluster->slots[j]->configEpoch,\n                (unsigned long long) requestConfigEpoch);\n        return;\n    }\n\n    /* We can vote for this slave. */\n    g_pserver->cluster->lastVoteEpoch = g_pserver->cluster->currentEpoch;\n    node->slaveof->voted_time = mstime();\n    clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG|CLUSTER_TODO_FSYNC_CONFIG);\n    clusterSendFailoverAuth(node);\n    serverLog(LL_WARNING, \"Failover auth granted to %.40s for epoch %llu\",\n        node->name, (unsigned long long) g_pserver->cluster->currentEpoch);\n}\n\n/* This function returns the \"rank\" of this instance, a slave, in the context\n * of its master-slaves ring. The rank of the slave is given by the number of\n * other slaves for the same master that have a better replication offset\n * compared to the local one (better means, greater, so they claim more data).\n *\n * A slave with rank 0 is the one with the greatest (most up to date)\n * replication offset, and so forth. Note that because how the rank is computed\n * multiple slaves may have the same rank, in case they have the same offset.\n *\n * The slave rank is used to add a delay to start an election in order to\n * get voted and replace a failing master. Slaves with better replication\n * offsets are more likely to win. */\nint clusterGetSlaveRank(void) {\n    long long myoffset;\n    int j, rank = 0;\n    clusterNode *master;\n\n    serverAssert(nodeIsSlave(myself));\n    master = myself->slaveof;\n    if (master == NULL) return 0; /* Never called by slaves without master. */\n\n    myoffset = replicationGetSlaveOffset(getFirstMaster());\n    for (j = 0; j < master->numslaves; j++)\n        if (master->slaves[j] != myself &&\n            !nodeCantFailover(master->slaves[j]) &&\n            master->slaves[j]->repl_offset > myoffset) rank++;\n    return rank;\n}\n\n/* This function is called by clusterHandleSlaveFailover() in order to\n * let the slave log why it is not able to failover. Sometimes there are\n * not the conditions, but since the failover function is called again and\n * again, we can't log the same things continuously.\n *\n * This function works by logging only if a given set of conditions are\n * true:\n *\n * 1) The reason for which the failover can't be initiated changed.\n *    The reasons also include a NONE reason we reset the state to\n *    when the slave finds that its master is fine (no FAIL flag).\n * 2) Also, the log is emitted again if the master is still down and\n *    the reason for not failing over is still the same, but more than\n *    CLUSTER_CANT_FAILOVER_RELOG_PERIOD seconds elapsed.\n * 3) Finally, the function only logs if the slave is down for more than\n *    five seconds + NODE_TIMEOUT. This way nothing is logged when a\n *    failover starts in a reasonable time.\n *\n * The function is called with the reason why the slave can't failover\n * which is one of the integer macros CLUSTER_CANT_FAILOVER_*.\n *\n * The function is guaranteed to be called only if 'myself' is a slave. */\nvoid clusterLogCantFailover(int reason) {\n    const char *msg;\n    static time_t lastlog_time = 0;\n    mstime_t nolog_fail_time = g_pserver->cluster_node_timeout + 5000;\n\n    /* Don't log if we have the same reason for some time. */\n    if (reason == g_pserver->cluster->cant_failover_reason &&\n        time(NULL)-lastlog_time < CLUSTER_CANT_FAILOVER_RELOG_PERIOD)\n        return;\n\n    g_pserver->cluster->cant_failover_reason = reason;\n\n    /* We also don't emit any log if the master failed no long ago, the\n     * goal of this function is to log slaves in a stalled condition for\n     * a long time. */\n    if (myself->slaveof &&\n        nodeFailed(myself->slaveof) &&\n        (mstime() - myself->slaveof->fail_time) < nolog_fail_time) return;\n\n    switch(reason) {\n    case CLUSTER_CANT_FAILOVER_DATA_AGE:\n        msg = \"Disconnected from master for longer than allowed. \"\n              \"Please check the 'cluster-replica-validity-factor' configuration \"\n              \"option.\";\n        break;\n    case CLUSTER_CANT_FAILOVER_WAITING_DELAY:\n        msg = \"Waiting the delay before I can start a new failover.\";\n        break;\n    case CLUSTER_CANT_FAILOVER_EXPIRED:\n        msg = \"Failover attempt expired.\";\n        break;\n    case CLUSTER_CANT_FAILOVER_WAITING_VOTES:\n        msg = \"Waiting for votes, but majority still not reached.\";\n        break;\n    default:\n        msg = \"Unknown reason code.\";\n        break;\n    }\n    lastlog_time = time(NULL);\n    serverLog(LL_WARNING,\"Currently unable to failover: %s\", msg);\n}\n\n/* This function implements the final part of automatic and manual failovers,\n * where the slave grabs its master's hash slots, and propagates the new\n * configuration.\n *\n * Note that it's up to the caller to be sure that the node got a new\n * configuration epoch already. */\nvoid clusterFailoverReplaceYourMaster(void) {\n    int j;\n    clusterNode *oldmaster = myself->slaveof;\n\n    if (nodeIsMaster(myself) || oldmaster == NULL) return;\n\n    /* 1) Turn this node into a master. */\n    clusterSetNodeAsMaster(myself);\n    replicationUnsetMaster(getFirstMaster());\n\n    /* 2) Claim all the slots assigned to our master. */\n    for (j = 0; j < CLUSTER_SLOTS; j++) {\n        if (clusterNodeGetSlotBit(oldmaster,j)) {\n            clusterDelSlot(j);\n            clusterAddSlot(myself,j);\n        }\n    }\n\n    /* 3) Update state and save config. */\n    clusterUpdateState();\n    clusterSaveConfigOrDie(1);\n\n    /* 4) Pong all the other nodes so that they can update the state\n     *    accordingly and detect that we switched to master role. */\n    clusterBroadcastPong(CLUSTER_BROADCAST_ALL);\n\n    /* 5) If there was a manual failover in progress, clear the state. */\n    resetManualFailover();\n}\n\n/* This function is called if we are a slave node and our master serving\n * a non-zero amount of hash slots is in FAIL state.\n *\n * The goal of this function is:\n * 1) To check if we are able to perform a failover, is our data updated?\n * 2) Try to get elected by masters.\n * 3) Perform the failover informing all the other nodes.\n */\nvoid clusterHandleSlaveFailover(void) {\n    mstime_t data_age;\n    mstime_t auth_age = mstime() - g_pserver->cluster->failover_auth_time;\n    int needed_quorum = (g_pserver->cluster->size / 2) + 1;\n    int manual_failover = g_pserver->cluster->mf_end != 0 &&\n                          g_pserver->cluster->mf_can_start;\n    mstime_t auth_timeout, auth_retry_time;\n\n    g_pserver->cluster->todo_before_sleep &= ~CLUSTER_TODO_HANDLE_FAILOVER;\n\n    /* Compute the failover timeout (the max time we have to send votes\n     * and wait for replies), and the failover retry time (the time to wait\n     * before trying to get voted again).\n     *\n     * Timeout is MAX(NODE_TIMEOUT*2,2000) milliseconds.\n     * Retry is two times the Timeout.\n     */\n    auth_timeout = g_pserver->cluster_node_timeout*2;\n    if (auth_timeout < 2000) auth_timeout = 2000;\n    auth_retry_time = auth_timeout*2;\n\n    /* Pre conditions to run the function, that must be met both in case\n     * of an automatic or manual failover:\n     * 1) We are a slave.\n     * 2) Our master is flagged as FAIL, or this is a manual failover.\n     * 3) We don't have the no failover configuration set, and this is\n     *    not a manual failover.\n     * 4) It is serving slots. */\n    if (nodeIsMaster(myself) ||\n        myself->slaveof == NULL ||\n        (!nodeFailed(myself->slaveof) && !manual_failover) ||\n        (g_pserver->cluster_slave_no_failover && !manual_failover) ||\n        myself->slaveof->numslots == 0)\n    {\n        /* There are no reasons to failover, so we set the reason why we\n         * are returning without failing over to NONE. */\n        g_pserver->cluster->cant_failover_reason = CLUSTER_CANT_FAILOVER_NONE;\n        return;\n    }\n\n    /* Set data_age to the number of milliseconds we are disconnected from\n     * the master. */\n    if (getFirstMaster()->repl_state == REPL_STATE_CONNECTED) {\n        data_age = (mstime_t)(g_pserver->unixtime - getFirstMaster()->master->lastinteraction)\n                   * 1000;\n    } else {\n        data_age = (mstime_t)(g_pserver->unixtime - getFirstMaster()->repl_down_since) * 1000;\n    }\n\n    /* Remove the node timeout from the data age as it is fine that we are\n     * disconnected from our master at least for the time it was down to be\n     * flagged as FAIL, that's the baseline. */\n    if (data_age > g_pserver->cluster_node_timeout)\n        data_age -= g_pserver->cluster_node_timeout;\n\n    /* Check if our data is recent enough according to the slave validity\n     * factor configured by the user.\n     *\n     * Check bypassed for manual failovers. */\n    if (g_pserver->cluster_slave_validity_factor &&\n        data_age >\n        (((mstime_t)g_pserver->repl_ping_slave_period * 1000) +\n         (g_pserver->cluster_node_timeout * g_pserver->cluster_slave_validity_factor)))\n    {\n        if (!manual_failover) {\n            clusterLogCantFailover(CLUSTER_CANT_FAILOVER_DATA_AGE);\n            return;\n        }\n    }\n\n    /* If the previous failover attempt timeout and the retry time has\n     * elapsed, we can setup a new one. */\n    if (auth_age > auth_retry_time) {\n        g_pserver->cluster->failover_auth_time = mstime() +\n            500 + /* Fixed delay of 500 milliseconds, let FAIL msg propagate. */\n            random() % 500; /* Random delay between 0 and 500 milliseconds. */\n        g_pserver->cluster->failover_auth_count = 0;\n        g_pserver->cluster->failover_auth_sent = 0;\n        g_pserver->cluster->failover_auth_rank = clusterGetSlaveRank();\n        /* We add another delay that is proportional to the slave rank.\n         * Specifically 1 second * rank. This way slaves that have a probably\n         * less updated replication offset, are penalized. */\n        g_pserver->cluster->failover_auth_time +=\n            g_pserver->cluster->failover_auth_rank * 1000;\n        /* However if this is a manual failover, no delay is needed. */\n        if (g_pserver->cluster->mf_end) {\n            g_pserver->cluster->failover_auth_time = mstime();\n            g_pserver->cluster->failover_auth_rank = 0;\n\t    clusterDoBeforeSleep(CLUSTER_TODO_HANDLE_FAILOVER);\n        }\n        serverLog(LL_WARNING,\n            \"Start of election delayed for %lld milliseconds \"\n            \"(rank #%d, offset %lld).\",\n            g_pserver->cluster->failover_auth_time - mstime(),\n            g_pserver->cluster->failover_auth_rank,\n            replicationGetSlaveOffset(getFirstMaster()));\n        /* Now that we have a scheduled election, broadcast our offset\n         * to all the other slaves so that they'll updated their offsets\n         * if our offset is better. */\n        clusterBroadcastPong(CLUSTER_BROADCAST_LOCAL_SLAVES);\n        return;\n    }\n\n    /* It is possible that we received more updated offsets from other\n     * slaves for the same master since we computed our election delay.\n     * Update the delay if our rank changed.\n     *\n     * Not performed if this is a manual failover. */\n    if (g_pserver->cluster->failover_auth_sent == 0 &&\n        g_pserver->cluster->mf_end == 0)\n    {\n        int newrank = clusterGetSlaveRank();\n        if (newrank > g_pserver->cluster->failover_auth_rank) {\n            long long added_delay =\n                (newrank - g_pserver->cluster->failover_auth_rank) * 1000;\n            g_pserver->cluster->failover_auth_time += added_delay;\n            g_pserver->cluster->failover_auth_rank = newrank;\n            serverLog(LL_WARNING,\n                \"Replica rank updated to #%d, added %lld milliseconds of delay.\",\n                newrank, added_delay);\n        }\n    }\n\n    /* Return ASAP if we can't still start the election. */\n    if (mstime() < g_pserver->cluster->failover_auth_time) {\n        clusterLogCantFailover(CLUSTER_CANT_FAILOVER_WAITING_DELAY);\n        return;\n    }\n\n    /* Return ASAP if the election is too old to be valid. */\n    if (auth_age > auth_timeout) {\n        clusterLogCantFailover(CLUSTER_CANT_FAILOVER_EXPIRED);\n        return;\n    }\n\n    /* Ask for votes if needed. */\n    if (g_pserver->cluster->failover_auth_sent == 0) {\n        g_pserver->cluster->currentEpoch++;\n        g_pserver->cluster->failover_auth_epoch = g_pserver->cluster->currentEpoch;\n        serverLog(LL_WARNING,\"Starting a failover election for epoch %llu.\",\n            (unsigned long long) g_pserver->cluster->currentEpoch);\n        clusterRequestFailoverAuth();\n        g_pserver->cluster->failover_auth_sent = 1;\n        clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG|\n                             CLUSTER_TODO_UPDATE_STATE|\n                             CLUSTER_TODO_FSYNC_CONFIG);\n        return; /* Wait for replies. */\n    }\n\n    /* Check if we reached the quorum. */\n    if (g_pserver->cluster->failover_auth_count >= needed_quorum) {\n        /* We have the quorum, we can finally failover the master. */\n\n        serverLog(LL_WARNING,\n            \"Failover election won: I'm the new master.\");\n\n        /* Update my configEpoch to the epoch of the election. */\n        if (myself->configEpoch < g_pserver->cluster->failover_auth_epoch) {\n            myself->configEpoch = g_pserver->cluster->failover_auth_epoch;\n            serverLog(LL_WARNING,\n                \"configEpoch set to %llu after successful failover\",\n                (unsigned long long) myself->configEpoch);\n        }\n\n        /* Take responsibility for the cluster slots. */\n        clusterFailoverReplaceYourMaster();\n    } else {\n        clusterLogCantFailover(CLUSTER_CANT_FAILOVER_WAITING_VOTES);\n    }\n}\n\n/* -----------------------------------------------------------------------------\n * CLUSTER slave migration\n *\n * Slave migration is the process that allows a slave of a master that is\n * already covered by at least another slave, to \"migrate\" to a master that\n * is orphaned, that is, left with no working slaves.\n * ------------------------------------------------------------------------- */\n\n/* This function is responsible to decide if this replica should be migrated\n * to a different (orphaned) master. It is called by the clusterCron() function\n * only if:\n *\n * 1) We are a slave node.\n * 2) It was detected that there is at least one orphaned master in\n *    the cluster.\n * 3) We are a slave of one of the masters with the greatest number of\n *    slaves.\n *\n * This checks are performed by the caller since it requires to iterate\n * the nodes anyway, so we spend time into clusterHandleSlaveMigration()\n * if definitely needed.\n *\n * The function is called with a pre-computed max_slaves, that is the max\n * number of working (not in FAIL state) slaves for a single master.\n *\n * Additional conditions for migration are examined inside the function.\n */\nvoid clusterHandleSlaveMigration(int max_slaves) {\n    int j, okslaves = 0;\n    clusterNode *mymaster = myself->slaveof, *target = NULL, *candidate = NULL;\n    dictIterator *di;\n    dictEntry *de;\n\n    /* Step 1: Don't migrate if the cluster state is not ok. */\n    if (g_pserver->cluster->state != CLUSTER_OK) return;\n\n    /* Step 2: Don't migrate if my master will not be left with at least\n     *         'migration-barrier' slaves after my migration. */\n    if (mymaster == NULL) return;\n    for (j = 0; j < mymaster->numslaves; j++)\n        if (!nodeFailed(mymaster->slaves[j]) &&\n            !nodeTimedOut(mymaster->slaves[j])) okslaves++;\n    if (okslaves <= g_pserver->cluster_migration_barrier) return;\n\n    /* Step 3: Identify a candidate for migration, and check if among the\n     * masters with the greatest number of ok slaves, I'm the one with the\n     * smallest node ID (the \"candidate slave\").\n     *\n     * Note: this means that eventually a replica migration will occur\n     * since slaves that are reachable again always have their FAIL flag\n     * cleared, so eventually there must be a candidate. At the same time\n     * this does not mean that there are no race conditions possible (two\n     * slaves migrating at the same time), but this is unlikely to\n     * happen, and harmless when happens. */\n    candidate = myself;\n    di = dictGetSafeIterator(g_pserver->cluster->nodes);\n    while((de = dictNext(di)) != NULL) {\n        clusterNode *node = (clusterNode*)dictGetVal(de);\n        int okslaves = 0, is_orphaned = 1;\n\n        /* We want to migrate only if this master is working, orphaned, and\n         * used to have slaves or if failed over a master that had slaves\n         * (MIGRATE_TO flag). This way we only migrate to instances that were\n         * supposed to have replicas. */\n        if (nodeIsSlave(node) || nodeFailed(node)) is_orphaned = 0;\n        if (!(node->flags & CLUSTER_NODE_MIGRATE_TO)) is_orphaned = 0;\n\n        /* Check number of working slaves. */\n        if (nodeIsMaster(node)) okslaves = clusterCountNonFailingSlaves(node);\n        if (okslaves > 0) is_orphaned = 0;\n\n        if (is_orphaned) {\n            if (!target && node->numslots > 0) target = node;\n\n            /* Track the starting time of the orphaned condition for this\n             * master. */\n            if (!node->orphaned_time) node->orphaned_time = mstime();\n        } else {\n            node->orphaned_time = 0;\n        }\n\n        /* Check if I'm the slave candidate for the migration: attached\n         * to a master with the maximum number of slaves and with the smallest\n         * node ID. */\n        if (okslaves == max_slaves) {\n            for (j = 0; j < node->numslaves; j++) {\n                if (memcmp(node->slaves[j]->name,\n                           candidate->name,\n                           CLUSTER_NAMELEN) < 0)\n                {\n                    candidate = node->slaves[j];\n                }\n            }\n        }\n    }\n    dictReleaseIterator(di);\n\n    /* Step 4: perform the migration if there is a target, and if I'm the\n     * candidate, but only if the master is continuously orphaned for a\n     * couple of seconds, so that during failovers, we give some time to\n     * the natural slaves of this instance to advertise their switch from\n     * the old master to the new one. */\n    if (target && candidate == myself &&\n        (mstime()-target->orphaned_time) > CLUSTER_SLAVE_MIGRATION_DELAY &&\n       !(g_pserver->cluster_module_flags & CLUSTER_MODULE_FLAG_NO_FAILOVER))\n    {\n        serverLog(LL_WARNING,\"Migrating to orphaned master %.40s\",\n            target->name);\n        clusterSetMaster(target);\n    }\n}\n\n/* -----------------------------------------------------------------------------\n * CLUSTER manual failover\n *\n * This are the important steps performed by slaves during a manual failover:\n * 1) User send CLUSTER FAILOVER command. The failover state is initialized\n *    setting mf_end to the millisecond unix time at which we'll abort the\n *    attempt.\n * 2) Slave sends a MFSTART message to the master requesting to pause clients\n *    for two times the manual failover timeout CLUSTER_MF_TIMEOUT.\n *    When master is paused for manual failover, it also starts to flag\n *    packets with CLUSTERMSG_FLAG0_PAUSED.\n * 3) Slave waits for master to send its replication offset flagged as PAUSED.\n * 4) If slave received the offset from the master, and its offset matches,\n *    mf_can_start is set to 1, and clusterHandleSlaveFailover() will perform\n *    the failover as usually, with the difference that the vote request\n *    will be modified to force masters to vote for a slave that has a\n *    working master.\n *\n * From the point of view of the master things are simpler: when a\n * PAUSE_CLIENTS packet is received the master sets mf_end as well and\n * the sender in mf_slave. During the time limit for the manual failover\n * the master will just send PINGs more often to this slave, flagged with\n * the PAUSED flag, so that the slave will set mf_master_offset when receiving\n * a packet from the master with this flag set.\n *\n * The gaol of the manual failover is to perform a fast failover without\n * data loss due to the asynchronous master-slave replication.\n * -------------------------------------------------------------------------- */\n\n/* Reset the manual failover state. This works for both masters and slaves\n * as all the state about manual failover is cleared.\n *\n * The function can be used both to initialize the manual failover state at\n * startup or to abort a manual failover in progress. */\nvoid resetManualFailover(void) {\n    if (g_pserver->cluster->mf_end) {\n        checkClientPauseTimeoutAndReturnIfPaused();\n    }\n    g_pserver->cluster->mf_end = 0; /* No manual failover in progress. */\n    g_pserver->cluster->mf_can_start = 0;\n    g_pserver->cluster->mf_slave = NULL;\n    g_pserver->cluster->mf_master_offset = -1;\n}\n\n/* If a manual failover timed out, abort it. */\nvoid manualFailoverCheckTimeout(void) {\n    if (g_pserver->cluster->mf_end && g_pserver->cluster->mf_end < mstime()) {\n        serverLog(LL_WARNING,\"Manual failover timed out.\");\n        resetManualFailover();\n    }\n}\n\n/* This function is called from the cluster cron function in order to go\n * forward with a manual failover state machine. */\nvoid clusterHandleManualFailover(void) {\n    /* Return ASAP if no manual failover is in progress. */\n    if (g_pserver->cluster->mf_end == 0) return;\n\n    /* If mf_can_start is non-zero, the failover was already triggered so the\n     * next steps are performed by clusterHandleSlaveFailover(). */\n    if (g_pserver->cluster->mf_can_start) return;\n\n    if (g_pserver->cluster->mf_master_offset == -1) return; /* Wait for offset... */\n\n    if (g_pserver->cluster->mf_master_offset == replicationGetSlaveOffset(getFirstMaster())) {\n        /* Our replication offset matches the master replication offset\n         * announced after clients were paused. We can start the failover. */\n        g_pserver->cluster->mf_can_start = 1;\n        serverLog(LL_WARNING,\n            \"All master replication stream processed, \"\n            \"manual failover can start.\");\n        clusterDoBeforeSleep(CLUSTER_TODO_HANDLE_FAILOVER);\n        return;\n    }\n    clusterDoBeforeSleep(CLUSTER_TODO_HANDLE_MANUALFAILOVER);\n}\n\n/* -----------------------------------------------------------------------------\n * CLUSTER cron job\n * -------------------------------------------------------------------------- */\n\n/* This is executed 10 times every second */\nvoid clusterCron(void) {\n    serverAssert(ielFromEventLoop(serverTL->el) == IDX_EVENT_LOOP_MAIN);\n    dictIterator *di;\n    dictEntry *de;\n    int update_state = 0;\n    int orphaned_masters; /* How many masters there are without ok slaves. */\n    int max_slaves; /* Max number of ok slaves for a single master. */\n    int this_slaves; /* Number of ok slaves for our master (if we are slave). */\n    mstime_t min_pong = 0, now = mstime();\n    clusterNode *min_pong_node = NULL;\n    static unsigned long long iteration = 0;\n    mstime_t handshake_timeout;\n\n    iteration++; /* Number of times this function was called so far. */\n\n    /* We want to take myself->ip in sync with the cluster-announce-ip option.\n     * The option can be set at runtime via CONFIG SET, so we periodically check\n     * if the option changed to reflect this into myself->ip. */\n    {\n        static char *prev_ip = NULL;\n        char *curr_ip = g_pserver->cluster_announce_ip;\n        int changed = 0;\n\n        if (prev_ip == NULL && curr_ip != NULL) changed = 1;\n        else if (prev_ip != NULL && curr_ip == NULL) changed = 1;\n        else if (prev_ip && curr_ip && strcmp(prev_ip,curr_ip)) changed = 1;\n\n        if (changed) {\n            if (prev_ip) zfree(prev_ip);\n            prev_ip = curr_ip;\n\n            if (curr_ip) {\n                /* We always take a copy of the previous IP address, by\n                 * duplicating the string. This way later we can check if\n                 * the address really changed. */\n                prev_ip = zstrdup(prev_ip);\n                strncpy(myself->ip,g_pserver->cluster_announce_ip,NET_IP_STR_LEN-1);\n                myself->ip[NET_IP_STR_LEN-1] = '\\0';\n            } else {\n                myself->ip[0] = '\\0'; /* Force autodetection. */\n            }\n        }\n    }\n\n    /* The handshake timeout is the time after which a handshake node that was\n     * not turned into a normal node is removed from the nodes. Usually it is\n     * just the NODE_TIMEOUT value, but when NODE_TIMEOUT is too small we use\n     * the value of 1 second. */\n    handshake_timeout = g_pserver->cluster_node_timeout;\n    if (handshake_timeout < 1000) handshake_timeout = 1000;\n\n    /* Update myself flags. */\n    clusterUpdateMyselfFlags();\n\n    /* Check if we have disconnected nodes and re-establish the connection.\n     * Also update a few stats while we are here, that can be used to make\n     * better decisions in other part of the code. */\n    di = dictGetSafeIterator(g_pserver->cluster->nodes);\n    g_pserver->cluster->stats_pfail_nodes = 0;\n    while((de = dictNext(di)) != NULL) {\n        clusterNode *node = (clusterNode*)dictGetVal(de);\n\n        /* Not interested in reconnecting the link with myself or nodes\n         * for which we have no address. */\n        if (node->flags & (CLUSTER_NODE_MYSELF|CLUSTER_NODE_NOADDR)) continue;\n\n        if (node->flags & CLUSTER_NODE_PFAIL)\n            g_pserver->cluster->stats_pfail_nodes++;\n\n        /* A Node in HANDSHAKE state has a limited lifespan equal to the\n         * configured node timeout. */\n        if (nodeInHandshake(node) && now - node->ctime > handshake_timeout) {\n            clusterDelNode(node);\n            continue;\n        }\n\n        if (node->link == NULL) {\n            clusterLink *link = createClusterLink(node);\n            link->conn = g_pserver->tls_cluster ? connCreateTLS() : connCreateSocket();\n            connSetPrivateData(link->conn, link);\n            if (connConnect(link->conn, node->ip, node->cport, NET_FIRST_BIND_ADDR,\n                        clusterLinkConnectHandler) == -1) {\n                /* We got a synchronous error from connect before\n                 * clusterSendPing() had a chance to be called.\n                 * If node->ping_sent is zero, failure detection can't work,\n                 * so we claim we actually sent a ping now (that will\n                 * be really sent as soon as the link is obtained). */\n                if (node->ping_sent == 0) node->ping_sent = mstime();\n                serverLog(LL_DEBUG, \"Unable to connect to \"\n                    \"Cluster Node [%s]:%d -> %s\", node->ip,\n                    node->cport, serverTL->neterr);\n\n                freeClusterLink(link);\n                continue;\n            }\n            node->link = link;\n        }\n    }\n    dictReleaseIterator(di);\n\n    /* Ping some random node 1 time every 10 iterations, so that we usually ping\n     * one random node every second. */\n    if (!(iteration % 10)) {\n        int j;\n\n        /* Check a few random nodes and ping the one with the oldest\n         * pong_received time. */\n        for (j = 0; j < 5; j++) {\n            de = dictGetRandomKey(g_pserver->cluster->nodes);\n            clusterNode *thisNode = (clusterNode*)dictGetVal(de);\n\n            /* Don't ping nodes disconnected or with a ping currently active. */\n            if (thisNode->link == NULL || thisNode->ping_sent != 0) continue;\n            if (thisNode->flags & (CLUSTER_NODE_MYSELF|CLUSTER_NODE_HANDSHAKE))\n                continue;\n            if (min_pong_node == NULL || min_pong > thisNode->pong_received) {\n                min_pong_node = thisNode;\n                min_pong = thisNode->pong_received;\n            }\n        }\n        if (min_pong_node) {\n            serverLog(LL_DEBUG,\"Pinging node %.40s\", min_pong_node->name);\n            clusterSendPing(min_pong_node->link, CLUSTERMSG_TYPE_PING);\n        }\n    }\n\n    /* Iterate nodes to check if we need to flag something as failing.\n     * This loop is also responsible to:\n     * 1) Check if there are orphaned masters (masters without non failing\n     *    slaves).\n     * 2) Count the max number of non failing slaves for a single master.\n     * 3) Count the number of slaves for our master, if we are a slave. */\n    orphaned_masters = 0;\n    max_slaves = 0;\n    this_slaves = 0;\n    di = dictGetSafeIterator(g_pserver->cluster->nodes);\n    while((de = dictNext(di)) != NULL) {\n        clusterNode *node = (clusterNode*)dictGetVal(de);\n        now = mstime(); /* Use an updated time at every iteration. */\n\n        if (node->flags &\n            (CLUSTER_NODE_MYSELF|CLUSTER_NODE_NOADDR|CLUSTER_NODE_HANDSHAKE))\n                continue;\n\n        /* Orphaned master check, useful only if the current instance\n         * is a slave that may migrate to another master. */\n        if (nodeIsSlave(myself) && nodeIsMaster(node) && !nodeFailed(node)) {\n            int okslaves = clusterCountNonFailingSlaves(node);\n\n            /* A master is orphaned if it is serving a non-zero number of\n             * slots, have no working slaves, but used to have at least one\n             * slave, or failed over a master that used to have slaves. */\n            if (okslaves == 0 && node->numslots > 0 &&\n                node->flags & CLUSTER_NODE_MIGRATE_TO)\n            {\n                orphaned_masters++;\n            }\n            if (okslaves > max_slaves) max_slaves = okslaves;\n            if (nodeIsSlave(myself) && myself->slaveof == node)\n                this_slaves = okslaves;\n        }\n\n        /* If we are not receiving any data for more than half the cluster\n         * timeout, reconnect the link: maybe there is a connection\n         * issue even if the node is alive. */\n        mstime_t ping_delay = now - node->ping_sent;\n        mstime_t data_delay = now - node->data_received;\n        if (node->link && /* is connected */\n            now - node->link->ctime >\n            g_pserver->cluster_node_timeout && /* was not already reconnected */\n            node->ping_sent && /* we already sent a ping */\n            /* and we are waiting for the pong more than timeout/2 */\n            ping_delay > g_pserver->cluster_node_timeout/2 &&\n            /* and in such interval we are not seeing any traffic at all. */\n            data_delay > g_pserver->cluster_node_timeout/2)\n        {\n            /* Disconnect the link, it will be reconnected automatically. */\n            freeClusterLink(node->link);\n        }\n\n        /* If we have currently no active ping in this instance, and the\n         * received PONG is older than half the cluster timeout, send\n         * a new ping now, to ensure all the nodes are pinged without\n         * a too big delay. */\n        if (node->link &&\n            node->ping_sent == 0 &&\n            (now - node->pong_received) > g_pserver->cluster_node_timeout/2)\n        {\n            clusterSendPing(node->link, CLUSTERMSG_TYPE_PING);\n            continue;\n        }\n\n        /* If we are a master and one of the slaves requested a manual\n         * failover, ping it continuously. */\n        if (g_pserver->cluster->mf_end &&\n            nodeIsMaster(myself) &&\n            g_pserver->cluster->mf_slave == node &&\n            node->link)\n        {\n            clusterSendPing(node->link, CLUSTERMSG_TYPE_PING);\n            continue;\n        }\n\n        /* Check only if we have an active ping for this instance. */\n        if (node->ping_sent == 0) continue;\n\n        /* Check if this node looks unreachable.\n         * Note that if we already received the PONG, then node->ping_sent\n         * is zero, so can't reach this code at all, so we don't risk of\n         * checking for a PONG delay if we didn't sent the PING.\n         *\n         * We also consider every incoming data as proof of liveness, since\n         * our cluster bus link is also used for data: under heavy data\n         * load pong delays are possible. */\n        mstime_t node_delay = (ping_delay < data_delay) ? ping_delay :\n                                                          data_delay;\n\n        if (node_delay > g_pserver->cluster_node_timeout) {\n            /* Timeout reached. Set the node as possibly failing if it is\n             * not already in this state. */\n            if (!(node->flags & (CLUSTER_NODE_PFAIL|CLUSTER_NODE_FAIL))) {\n                serverLog(LL_DEBUG,\"*** NODE %.40s possibly failing\",\n                    node->name);\n                node->flags |= CLUSTER_NODE_PFAIL;\n                update_state = 1;\n            }\n        }\n    }\n    dictReleaseIterator(di);\n\n    /* If we are a slave node but the replication is still turned off,\n     * enable it if we know the address of our master and it appears to\n     * be up. */\n    if (nodeIsSlave(myself) &&\n        listLength(g_pserver->masters) == 0 &&\n        myself->slaveof &&\n        nodeHasAddr(myself->slaveof))\n    {\n        replicationAddMaster(myself->slaveof->ip, myself->slaveof->port);\n    }\n\n    /* Abort a manual failover if the timeout is reached. */\n    manualFailoverCheckTimeout();\n\n    if (nodeIsSlave(myself)) {\n        clusterHandleManualFailover();\n        if (!(g_pserver->cluster_module_flags & CLUSTER_MODULE_FLAG_NO_FAILOVER))\n            clusterHandleSlaveFailover();\n        /* If there are orphaned slaves, and we are a slave among the masters\n         * with the max number of non-failing slaves, consider migrating to\n         * the orphaned masters. Note that it does not make sense to try\n         * a migration if there is no master with at least *two* working\n         * slaves. */\n        if (orphaned_masters && max_slaves >= 2 && this_slaves == max_slaves &&\n\t\tg_pserver->cluster_allow_replica_migration)\n            clusterHandleSlaveMigration(max_slaves);\n    }\n\n    if (update_state || g_pserver->cluster->state == CLUSTER_FAIL)\n        clusterUpdateState();\n}\n\n/* This function is called before the event handler returns to sleep for\n * events. It is useful to perform operations that must be done ASAP in\n * reaction to events fired but that are not safe to perform inside event\n * handlers, or to perform potentially expansive tasks that we need to do\n * a single time before replying to clients. */\nvoid clusterBeforeSleep(void) {\n    int flags = g_pserver->cluster->todo_before_sleep;\n\n    /* Reset our flags (not strictly needed since every single function\n     * called for flags set should be able to clear its flag). */\n    g_pserver->cluster->todo_before_sleep = 0;\n\n    if (flags & CLUSTER_TODO_HANDLE_MANUALFAILOVER) {\n        /* Handle manual failover as soon as possible so that won't have a 100ms\n         * as it was handled only in clusterCron */\n        if(nodeIsSlave(myself)) {\n            clusterHandleManualFailover();\n            if (!(g_pserver->cluster_module_flags & CLUSTER_MODULE_FLAG_NO_FAILOVER))\n                clusterHandleSlaveFailover();\n        }\n    } else if (flags & CLUSTER_TODO_HANDLE_FAILOVER) {\n        /* Handle failover, this is needed when it is likely that there is already\n         * the quorum from masters in order to react fast. */\n        clusterHandleSlaveFailover();\n    }\n\n    /* Update the cluster state. */\n    if (flags & CLUSTER_TODO_UPDATE_STATE)\n        clusterUpdateState();\n\n    /* Save the config, possibly using fsync. */\n    if (flags & CLUSTER_TODO_SAVE_CONFIG) {\n        int fsync = flags & CLUSTER_TODO_FSYNC_CONFIG;\n        clusterSaveConfigOrDie(fsync);\n    }\n}\n\nvoid clusterDoBeforeSleep(int flags) {\n    g_pserver->cluster->todo_before_sleep |= flags;\n}\n\n/* -----------------------------------------------------------------------------\n * Slots management\n * -------------------------------------------------------------------------- */\n\n/* Test bit 'pos' in a generic bitmap. Return 1 if the bit is set,\n * otherwise 0. */\nint bitmapTestBit(unsigned char *bitmap, int pos) {\n    off_t byte = pos/8;\n    int bit = pos&7;\n    return (bitmap[byte] & (1<<bit)) != 0;\n}\n\n/* Set the bit at position 'pos' in a bitmap. */\nvoid bitmapSetBit(unsigned char *bitmap, int pos) {\n    off_t byte = pos/8;\n    int bit = pos&7;\n    bitmap[byte] |= 1<<bit;\n}\n\n/* Clear the bit at position 'pos' in a bitmap. */\nvoid bitmapClearBit(unsigned char *bitmap, int pos) {\n    off_t byte = pos/8;\n    int bit = pos&7;\n    bitmap[byte] &= ~(1<<bit);\n}\n\n/* Return non-zero if there is at least one master with slaves in the cluster.\n * Otherwise zero is returned. Used by clusterNodeSetSlotBit() to set the\n * MIGRATE_TO flag the when a master gets the first slot. */\nint clusterMastersHaveSlaves(void) {\n    dictIterator *di = dictGetSafeIterator(g_pserver->cluster->nodes);\n    dictEntry *de;\n    int slaves = 0;\n    while((de = dictNext(di)) != NULL) {\n        clusterNode *node = (clusterNode*)dictGetVal(de);\n\n        if (nodeIsSlave(node)) continue;\n        slaves += node->numslaves;\n    }\n    dictReleaseIterator(di);\n    return slaves != 0;\n}\n\n/* Set the slot bit and return the old value. */\nint clusterNodeSetSlotBit(clusterNode *n, int slot) {\n    int old = bitmapTestBit(n->slots,slot);\n    bitmapSetBit(n->slots,slot);\n    if (!old) {\n        n->numslots++;\n        /* When a master gets its first slot, even if it has no slaves,\n         * it gets flagged with MIGRATE_TO, that is, the master is a valid\n         * target for replicas migration, if and only if at least one of\n         * the other masters has slaves right now.\n         *\n         * Normally masters are valid targets of replica migration if:\n         * 1. The used to have slaves (but no longer have).\n         * 2. They are slaves failing over a master that used to have slaves.\n         *\n         * However new masters with slots assigned are considered valid\n         * migration targets if the rest of the cluster is not a slave-less.\n         *\n         * See https://github.com/redis/redis/issues/3043 for more info. */\n        if (n->numslots == 1 && clusterMastersHaveSlaves())\n            n->flags |= CLUSTER_NODE_MIGRATE_TO;\n    }\n    return old;\n}\n\n/* Clear the slot bit and return the old value. */\nint clusterNodeClearSlotBit(clusterNode *n, int slot) {\n    int old = bitmapTestBit(n->slots,slot);\n    bitmapClearBit(n->slots,slot);\n    if (old) n->numslots--;\n    return old;\n}\n\n/* Return the slot bit from the cluster node structure. */\nint clusterNodeGetSlotBit(clusterNode *n, int slot) {\n    return bitmapTestBit(n->slots,slot);\n}\n\n/* Add the specified slot to the list of slots that node 'n' will\n * serve. Return C_OK if the operation ended with success.\n * If the slot is already assigned to another instance this is considered\n * an error and C_ERR is returned. */\nint clusterAddSlot(clusterNode *n, int slot) {\n    if (g_pserver->cluster->slots[slot]) return C_ERR;\n    clusterNodeSetSlotBit(n,slot);\n    g_pserver->cluster->slots[slot] = n;\n    return C_OK;\n}\n\n/* Delete the specified slot marking it as unassigned.\n * Returns C_OK if the slot was assigned, otherwise if the slot was\n * already unassigned C_ERR is returned. */\nint clusterDelSlot(int slot) {\n    clusterNode *n = g_pserver->cluster->slots[slot];\n\n    if (!n) return C_ERR;\n    serverAssert(clusterNodeClearSlotBit(n,slot) == 1);\n    g_pserver->cluster->slots[slot] = NULL;\n    return C_OK;\n}\n\n/* Delete all the slots associated with the specified node.\n * The number of deleted slots is returned. */\nint clusterDelNodeSlots(clusterNode *node) {\n    int deleted = 0, j;\n\n    for (j = 0; j < CLUSTER_SLOTS; j++) {\n        if (clusterNodeGetSlotBit(node,j)) {\n            clusterDelSlot(j);\n            deleted++;\n        }\n    }\n    return deleted;\n}\n\n/* Clear the migrating / importing state for all the slots.\n * This is useful at initialization and when turning a master into slave. */\nvoid clusterCloseAllSlots(void) {\n    memset(g_pserver->cluster->migrating_slots_to,0,\n        sizeof(g_pserver->cluster->migrating_slots_to));\n    memset(g_pserver->cluster->importing_slots_from,0,\n        sizeof(g_pserver->cluster->importing_slots_from));\n}\n\n/* -----------------------------------------------------------------------------\n * Cluster state evaluation function\n * -------------------------------------------------------------------------- */\n\n/* The following are defines that are only used in the evaluation function\n * and are based on heuristics. Actually the main point about the rejoin and\n * writable delay is that they should be a few orders of magnitude larger\n * than the network latency. */\n#define CLUSTER_MAX_REJOIN_DELAY 5000\n#define CLUSTER_MIN_REJOIN_DELAY 500\n#define CLUSTER_WRITABLE_DELAY 2000\n\nvoid clusterUpdateState(void) {\n    int j, new_state;\n    int reachable_masters = 0;\n    static mstime_t among_minority_time;\n    static mstime_t first_call_time = 0;\n\n    g_pserver->cluster->todo_before_sleep &= ~CLUSTER_TODO_UPDATE_STATE;\n\n    /* If this is a master node, wait some time before turning the state\n     * into OK, since it is not a good idea to rejoin the cluster as a writable\n     * master, after a reboot, without giving the cluster a chance to\n     * reconfigure this node. Note that the delay is calculated starting from\n     * the first call to this function and not since the server start, in order\n     * to don't count the DB loading time. */\n    if (first_call_time == 0) first_call_time = mstime();\n    if (nodeIsMaster(myself) &&\n        g_pserver->cluster->state == CLUSTER_FAIL &&\n        mstime() - first_call_time < CLUSTER_WRITABLE_DELAY) return;\n\n    /* Start assuming the state is OK. We'll turn it into FAIL if there\n     * are the right conditions. */\n    new_state = CLUSTER_OK;\n\n    /* Check if all the slots are covered. */\n    if (g_pserver->cluster_require_full_coverage) {\n        for (j = 0; j < CLUSTER_SLOTS; j++) {\n            if (g_pserver->cluster->slots[j] == NULL ||\n                g_pserver->cluster->slots[j]->flags & (CLUSTER_NODE_FAIL))\n            {\n                new_state = CLUSTER_FAIL;\n                break;\n            }\n        }\n    }\n\n    /* Compute the cluster size, that is the number of master nodes\n     * serving at least a single slot.\n     *\n     * At the same time count the number of reachable masters having\n     * at least one slot. */\n    {\n        dictIterator *di;\n        dictEntry *de;\n\n        g_pserver->cluster->size = 0;\n        di = dictGetSafeIterator(g_pserver->cluster->nodes);\n        while((de = dictNext(di)) != NULL) {\n            clusterNode *node = (clusterNode*)dictGetVal(de);\n\n            if (nodeIsMaster(node) && node->numslots) {\n                g_pserver->cluster->size++;\n                if ((node->flags & (CLUSTER_NODE_FAIL|CLUSTER_NODE_PFAIL)) == 0)\n                    reachable_masters++;\n            }\n        }\n        dictReleaseIterator(di);\n    }\n\n    /* If we are in a minority partition, change the cluster state\n     * to FAIL. */\n    {\n        int needed_quorum = (g_pserver->cluster->size / 2) + 1;\n\n        if (reachable_masters < needed_quorum) {\n            new_state = CLUSTER_FAIL;\n            among_minority_time = mstime();\n        }\n    }\n\n    /* Log a state change */\n    if (new_state != g_pserver->cluster->state) {\n        mstime_t rejoin_delay = g_pserver->cluster_node_timeout;\n\n        /* If the instance is a master and was partitioned away with the\n         * minority, don't let it accept queries for some time after the\n         * partition heals, to make sure there is enough time to receive\n         * a configuration update. */\n        if (rejoin_delay > CLUSTER_MAX_REJOIN_DELAY)\n            rejoin_delay = CLUSTER_MAX_REJOIN_DELAY;\n        if (rejoin_delay < CLUSTER_MIN_REJOIN_DELAY)\n            rejoin_delay = CLUSTER_MIN_REJOIN_DELAY;\n\n        if (new_state == CLUSTER_OK &&\n            nodeIsMaster(myself) &&\n            mstime() - among_minority_time < rejoin_delay)\n        {\n            return;\n        }\n\n        /* Change the state and log the event. */\n        serverLog(LL_WARNING,\"Cluster state changed: %s\",\n            new_state == CLUSTER_OK ? \"ok\" : \"fail\");\n        g_pserver->cluster->state = new_state;\n    }\n}\n\n/* This function is called after the node startup in order to verify that data\n * loaded from disk is in agreement with the cluster configuration:\n *\n * 1) If we find keys about hash slots we have no responsibility for, the\n *    following happens:\n *    A) If no other node is in charge according to the current cluster\n *       configuration, we add these slots to our node.\n *    B) If according to our config other nodes are already in charge for\n *       this slots, we set the slots as IMPORTING from our point of view\n *       in order to justify we have those slots, and in order to make\n *       keydb-trib aware of the issue, so that it can try to fix it.\n * 2) If we find data in a DB different than DB0 we return C_ERR to\n *    signal the caller it should quit the server with an error message\n *    or take other actions.\n *\n * The function always returns C_OK even if it will try to correct\n * the error described in \"1\". However if data is found in DB different\n * from DB0, C_ERR is returned.\n *\n * The function also uses the logging facility in order to warn the user\n * about desynchronizations between the data we have in memory and the\n * cluster configuration. */\nint verifyClusterConfigWithData(void) {\n    int j;\n    int update_config = 0;\n\n    /* Return ASAP if a module disabled cluster redirections. In that case\n     * every master can store keys about every possible hash slot. */\n    if (g_pserver->cluster_module_flags & CLUSTER_MODULE_FLAG_NO_REDIRECTION)\n        return C_OK;\n\n    /* If this node is a slave, don't perform the check at all as we\n     * completely depend on the replication stream. */\n    if (nodeIsSlave(myself)) return C_OK;\n\n    /* Make sure we only have keys in DB0. */\n    for (j = 1; j < cserver.dbnum; j++) {\n        if (g_pserver->db[j]->size()) return C_ERR;\n    }\n\n    /* Check that all the slots we see populated memory have a corresponding\n     * entry in the cluster table. Otherwise fix the table. */\n    for (j = 0; j < CLUSTER_SLOTS; j++) {\n        if (!countKeysInSlot(j)) continue; /* No keys in this slot. */\n        /* Check if we are assigned to this slot or if we are importing it.\n         * In both cases check the next slot as the configuration makes\n         * sense. */\n        if (g_pserver->cluster->slots[j] == myself ||\n            g_pserver->cluster->importing_slots_from[j] != NULL) continue;\n\n        /* If we are here data and cluster config don't agree, and we have\n         * slot 'j' populated even if we are not importing it, nor we are\n         * assigned to this slot. Fix this condition. */\n\n        update_config++;\n        /* Case A: slot is unassigned. Take responsibility for it. */\n        if (g_pserver->cluster->slots[j] == NULL) {\n            serverLog(LL_WARNING, \"I have keys for unassigned slot %d. \"\n                                    \"Taking responsibility for it.\",j);\n            clusterAddSlot(myself,j);\n        } else {\n            serverLog(LL_WARNING, \"I have keys for slot %d, but the slot is \"\n                                    \"assigned to another node. \"\n                                    \"Setting it to importing state.\",j);\n            g_pserver->cluster->importing_slots_from[j] = g_pserver->cluster->slots[j];\n        }\n    }\n    if (update_config) clusterSaveConfigOrDie(1);\n    return C_OK;\n}\n\n/* -----------------------------------------------------------------------------\n * SLAVE nodes handling\n * -------------------------------------------------------------------------- */\n\n/* Set the specified node 'n' as master for this node.\n * If this node is currently a master, it is turned into a slave. */\nvoid clusterSetMaster(clusterNode *n) {\n    serverAssert(n != myself);\n    serverAssert(myself->numslots == 0);\n\n    if (nodeIsMaster(myself)) {\n        myself->flags &= ~(CLUSTER_NODE_MASTER|CLUSTER_NODE_MIGRATE_TO);\n        myself->flags |= CLUSTER_NODE_SLAVE;\n        clusterCloseAllSlots();\n    } else {\n        if (myself->slaveof)\n            clusterNodeRemoveSlave(myself->slaveof,myself);\n    }\n    myself->slaveof = n;\n    clusterNodeAddSlave(n,myself);\n    replicationAddMaster(n->ip, n->port);\n    resetManualFailover();\n}\n\n/* -----------------------------------------------------------------------------\n * Nodes to string representation functions.\n * -------------------------------------------------------------------------- */\n\nstruct redisNodeFlags {\n    uint16_t flag;\n    const char *name;\n};\n\nstatic struct redisNodeFlags redisNodeFlagsTable[] = {\n    {CLUSTER_NODE_MYSELF,       \"myself,\"},\n    {CLUSTER_NODE_MASTER,       \"master,\"},\n    {CLUSTER_NODE_SLAVE,        \"slave,\"},\n    {CLUSTER_NODE_PFAIL,        \"fail?,\"},\n    {CLUSTER_NODE_FAIL,         \"fail,\"},\n    {CLUSTER_NODE_HANDSHAKE,    \"handshake,\"},\n    {CLUSTER_NODE_NOADDR,       \"noaddr,\"},\n    {CLUSTER_NODE_NOFAILOVER,   \"nofailover,\"}\n};\n\n/* Concatenate the comma separated list of node flags to the given SDS\n * string 'ci'. */\nsds representClusterNodeFlags(sds ci, uint16_t flags) {\n    size_t orig_len = sdslen(ci);\n    int i, size = sizeof(redisNodeFlagsTable)/sizeof(struct redisNodeFlags);\n    for (i = 0; i < size; i++) {\n        struct redisNodeFlags *nodeflag = redisNodeFlagsTable + i;\n        if (flags & nodeflag->flag) ci = sdscat(ci, nodeflag->name);\n    }\n    /* If no flag was added, add the \"noflags\" special flag. */\n    if (sdslen(ci) == orig_len) ci = sdscat(ci,\"noflags,\");\n    sdsIncrLen(ci,-1); /* Remove trailing comma. */\n    return ci;\n}\n\n/* Generate a csv-alike representation of the specified cluster node.\n * See clusterGenNodesDescription() top comment for more information.\n *\n * The function returns the string representation as an SDS string. */\nsds clusterGenNodeDescription(clusterNode *node, int use_pport) {\n    int j, start;\n    sds ci;\n    int port = use_pport && node->pport ? node->pport : node->port;\n\n    /* Node coordinates */\n    ci = sdscatlen(sdsempty(),node->name,CLUSTER_NAMELEN);\n    ci = sdscatfmt(ci,\" %s:%i@%i \",\n        node->ip,\n        port,\n        node->cport);\n\n    /* Flags */\n    ci = representClusterNodeFlags(ci, node->flags);\n\n    /* Slave of... or just \"-\" */\n    ci = sdscatlen(ci,\" \",1);\n    if (node->slaveof)\n        ci = sdscatlen(ci,node->slaveof->name,CLUSTER_NAMELEN);\n    else\n        ci = sdscatlen(ci,\"-\",1);\n\n    unsigned long long nodeEpoch = node->configEpoch;\n    if (nodeIsSlave(node) && node->slaveof) {\n        nodeEpoch = node->slaveof->configEpoch;\n    }\n    /* Latency from the POV of this node, config epoch, link status */\n    ci = sdscatfmt(ci,\" %I %I %U %s\",\n        (long long) node->ping_sent,\n        (long long) node->pong_received,\n        nodeEpoch,\n        (node->link || node->flags & CLUSTER_NODE_MYSELF) ?\n                    \"connected\" : \"disconnected\");\n\n    /* Slots served by this instance. If we already have slots info,\n     * append it diretly, otherwise, generate slots only if it has. */\n    if (node->slots_info) {\n        ci = sdscatsds(ci, node->slots_info);\n    } else if (node->numslots > 0) {\n        start = -1;\n        for (j = 0; j < CLUSTER_SLOTS; j++) {\n            int bit;\n\n            if ((bit = clusterNodeGetSlotBit(node,j)) != 0) {\n                if (start == -1) start = j;\n            }\n            if (start != -1 && (!bit || j == CLUSTER_SLOTS-1)) {\n                if (bit && j == CLUSTER_SLOTS-1) j++;\n\n                if (start == j-1) {\n                    ci = sdscatfmt(ci,\" %i\",start);\n                } else {\n                    ci = sdscatfmt(ci,\" %i-%i\",start,j-1);\n                }\n                start = -1;\n            }\n        }\n    }\n\n    /* Just for MYSELF node we also dump info about slots that\n     * we are migrating to other instances or importing from other\n     * instances. */\n    if (node->flags & CLUSTER_NODE_MYSELF) {\n        for (j = 0; j < CLUSTER_SLOTS; j++) {\n            if (g_pserver->cluster->migrating_slots_to[j]) {\n                ci = sdscatprintf(ci,\" [%d->-%.40s]\",j,\n                    g_pserver->cluster->migrating_slots_to[j]->name);\n            } else if (g_pserver->cluster->importing_slots_from[j]) {\n                ci = sdscatprintf(ci,\" [%d-<-%.40s]\",j,\n                    g_pserver->cluster->importing_slots_from[j]->name);\n            }\n        }\n    }\n    return ci;\n}\n\n/* Generate the slot topology for all nodes and store the string representation\n * in the slots_info struct on the node. This is used to improve the efficiency\n * of clusterGenNodesDescription() because it removes looping of the slot space\n * for generating the slot info for each node individually. */\nvoid clusterGenNodesSlotsInfo(int filter) {\n    clusterNode *n = NULL;\n    int start = -1;\n\n    for (int i = 0; i <= CLUSTER_SLOTS; i++) {\n        /* Find start node and slot id. */\n        if (n == NULL) {\n            if (i == CLUSTER_SLOTS) break;\n            n = g_pserver->cluster->slots[i];\n            start = i;\n            continue;\n        }\n\n        /* Generate slots info when occur different node with start\n         * or end of slot. */\n        if (i == CLUSTER_SLOTS || n != g_pserver->cluster->slots[i]) {\n            if (!(n->flags & filter)) {\n                if (n->slots_info == NULL) n->slots_info = sdsempty();\n                if (start == i-1) {\n                    n->slots_info = sdscatfmt(n->slots_info,\" %i\",start);\n                } else {\n                    n->slots_info = sdscatfmt(n->slots_info,\" %i-%i\",start,i-1);\n                }\n            }\n            if (i == CLUSTER_SLOTS) break;\n            n = g_pserver->cluster->slots[i];\n            start = i;\n        }\n    }\n}\n\n/* Generate a csv-alike representation of the nodes we are aware of,\n * including the \"myself\" node, and return an SDS string containing the\n * representation (it is up to the caller to free it).\n *\n * All the nodes matching at least one of the node flags specified in\n * \"filter\" are excluded from the output, so using zero as a filter will\n * include all the known nodes in the representation, including nodes in\n * the HANDSHAKE state.\n *\n * Setting use_pport to 1 in a TLS cluster makes the result contain the\n * plaintext client port rather then the TLS client port of each node.\n *\n * The representation obtained using this function is used for the output\n * of the CLUSTER NODES function, and as format for the cluster\n * configuration file (nodes.conf) for a given node. */\nsds clusterGenNodesDescription(int filter, int use_pport) {\n    sds ci = sdsempty(), ni;\n    dictIterator *di;\n    dictEntry *de;\n\n    /* Generate all nodes slots info firstly. */\n    clusterGenNodesSlotsInfo(filter);\n\n    di = dictGetSafeIterator(g_pserver->cluster->nodes);\n    while((de = dictNext(di)) != NULL) {\n        clusterNode *node = (clusterNode*)dictGetVal(de);\n\n        if (node->flags & filter) continue;\n        ni = clusterGenNodeDescription(node, use_pport);\n        ci = sdscatsds(ci,ni);\n        sdsfree(ni);\n        ci = sdscatlen(ci,\"\\n\",1);\n\n        /* Release slots info. */\n        if (node->slots_info) {\n            sdsfree(node->slots_info);\n            node->slots_info = NULL;\n        }\n    }\n    dictReleaseIterator(di);\n    return ci;\n}\n\n/* -----------------------------------------------------------------------------\n * CLUSTER command\n * -------------------------------------------------------------------------- */\n\nconst char *clusterGetMessageTypeString(int type) {\n    switch(type) {\n    case CLUSTERMSG_TYPE_PING: return \"ping\";\n    case CLUSTERMSG_TYPE_PONG: return \"pong\";\n    case CLUSTERMSG_TYPE_MEET: return \"meet\";\n    case CLUSTERMSG_TYPE_FAIL: return \"fail\";\n    case CLUSTERMSG_TYPE_PUBLISH: return \"publish\";\n    case CLUSTERMSG_TYPE_FAILOVER_AUTH_REQUEST: return \"auth-req\";\n    case CLUSTERMSG_TYPE_FAILOVER_AUTH_ACK: return \"auth-ack\";\n    case CLUSTERMSG_TYPE_UPDATE: return \"update\";\n    case CLUSTERMSG_TYPE_MFSTART: return \"mfstart\";\n    case CLUSTERMSG_TYPE_MODULE: return \"module\";\n    }\n    return \"unknown\";\n}\n\nint getSlotOrReply(client *c, robj *o) {\n    long long slot;\n\n    if (getLongLongFromObject(o,&slot) != C_OK ||\n        slot < 0 || slot >= CLUSTER_SLOTS)\n    {\n        addReplyError(c,\"Invalid or out of range slot\");\n        return -1;\n    }\n    return (int) slot;\n}\n\nvoid addNodeReplyForClusterSlot(client *c, clusterNode *node, int start_slot, int end_slot) {\n    int i, nested_elements = 3; /* slots (2) + master addr (1) */\n    void *nested_replylen = addReplyDeferredLen(c);\n    addReplyLongLong(c, start_slot);\n    addReplyLongLong(c, end_slot);\n    addReplyArrayLen(c, 3);\n    addReplyBulkCString(c, node->ip);\n    /* Report non-TLS ports to non-TLS client in TLS cluster if available. */\n    int use_pport = (g_pserver->tls_cluster &&\n                     c->conn && connGetType(c->conn) != CONN_TYPE_TLS);\n    addReplyLongLong(c, use_pport && node->pport ? node->pport : node->port);\n    addReplyBulkCBuffer(c, node->name, CLUSTER_NAMELEN);\n\n    /* Remaining nodes in reply are replicas for slot range */\n    for (i = 0; i < node->numslaves; i++) {\n        /* This loop is copy/pasted from clusterGenNodeDescription()\n         * with modifications for per-slot node aggregation. */\n        if (nodeFailed(node->slaves[i])) continue;\n        addReplyArrayLen(c, 3);\n        addReplyBulkCString(c, node->slaves[i]->ip);\n        /* Report slave's non-TLS port to non-TLS client in TLS cluster */\n        addReplyLongLong(c, (use_pport && node->slaves[i]->pport ?\n                             node->slaves[i]->pport :\n                             node->slaves[i]->port));\n        addReplyBulkCBuffer(c, node->slaves[i]->name, CLUSTER_NAMELEN);\n        nested_elements++;\n    }\n    setDeferredArrayLen(c, nested_replylen, nested_elements);\n}\n\nvoid clusterReplyMultiBulkSlots(client * c) {\n    /* Format: 1) 1) start slot\n     *            2) end slot\n     *            3) 1) master IP\n     *               2) master port\n     *               3) node ID\n     *            4) 1) replica IP\n     *               2) replica port\n     *               3) node ID\n     *           ... continued until done\n     */\n    clusterNode *n = NULL;\n    int num_masters = 0, start = -1;\n    void *slot_replylen = addReplyDeferredLen(c);\n\n    for (int i = 0; i <= CLUSTER_SLOTS; i++) {\n        /* Find start node and slot id. */\n        if (n == NULL) {\n            if (i == CLUSTER_SLOTS) break;\n            n = g_pserver->cluster->slots[i];\n            start = i;\n            continue;\n        }\n\n        /* Add cluster slots info when occur different node with start\n         * or end of slot. */\n        if (i == CLUSTER_SLOTS || n != g_pserver->cluster->slots[i]) {\n            addNodeReplyForClusterSlot(c, n, start, i-1);\n            num_masters++;\n            if (i == CLUSTER_SLOTS) break;\n            n = g_pserver->cluster->slots[i];\n            start = i;\n        }\n    }\n    setDeferredArrayLen(c, slot_replylen, num_masters);\n}\n\nvoid clusterCommand(client *c) {\n    if (g_pserver->cluster_enabled == 0) {\n        addReplyError(c,\"This instance has cluster support disabled\");\n        return;\n    }\n\n    if (c->argc == 2 && !strcasecmp(szFromObj(c->argv[1]),\"help\")) {\n        const char *help[] = {\n\"ADDSLOTS <slot> [<slot> ...]\",\n\"    Assign slots to current node.\",\n\"BUMPEPOCH\",\n\"    Advance the cluster config epoch.\",\n\"COUNT-FAILURE-REPORTS <node-id>\",\n\"    Return number of failure reports for <node-id>.\",\n\"COUNTKEYSINSLOT <slot>\",\n\"    Return the number of keys in <slot>.\",\n\"DELSLOTS <slot> [<slot> ...]\",\n\"    Delete slots information from current node.\",\n\"FAILOVER [FORCE|TAKEOVER]\",\n\"    Promote current replica node to being a master.\",\n\"FORGET <node-id>\",\n\"    Remove a node from the cluster.\",\n\"GETKEYSINSLOT <slot> <count>\",\n\"    Return key names stored by current node in a slot.\",\n\"FLUSHSLOTS\",\n\"    Delete current node own slots information.\",\n\"INFO\",\n\"    Return information about the cluster.\",\n\"KEYSLOT <key>\",\n\"    Return the hash slot for <key>.\",\n\"MEET <ip> <port> [<bus-port>]\",\n\"    Connect nodes into a working cluster.\",\n\"MYID\",\n\"    Return the node id.\",\n\"NODES\",\n\"    Return cluster configuration seen by node. Output format:\",\n\"    <id> <ip:port> <flags> <master> <pings> <pongs> <epoch> <link> <slot> ...\",\n\"REPLICATE (<node-id>|NO ONE)\",\n\"    Configure current node as replica to <node-id> or turn it into empty primary.\",\n\"RESET [HARD|SOFT]\",\n\"    Reset current node (default: soft).\",\n\"SET-CONFIG-EPOCH <epoch>\",\n\"    Set config epoch of current node.\",\n\"SETSLOT <slot> (IMPORTING|MIGRATING|STABLE|NODE <node-id>)\",\n\"    Set slot state.\",\n\"REPLICAS <node-id>\",\n\"    Return <node-id> replicas.\",\n\"SAVECONFIG\",\n\"    Force saving cluster configuration on disk.\",\n\"SLOTS\",\n\"    Return information about slots range mappings. Each range is made of:\",\n\"    start, end, master and replicas IP addresses, ports and ids\",\nNULL\n        };\n        addReplyHelp(c, help);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"meet\") && (c->argc == 4 || c->argc == 5)) {\n        /* CLUSTER MEET <ip> <port> [cport] */\n        long long port, cport;\n\n        if (getLongLongFromObject(c->argv[3], &port) != C_OK) {\n            addReplyErrorFormat(c,\"Invalid TCP base port specified: %s\",\n                                (char*)ptrFromObj(c->argv[3]));\n            return;\n        }\n\n        if (c->argc == 5) {\n            if (getLongLongFromObject(c->argv[4], &cport) != C_OK) {\n                addReplyErrorFormat(c,\"Invalid TCP bus port specified: %s\",\n                                    (char*)ptrFromObj(c->argv[4]));\n                return;\n            }\n        } else {\n            cport = port + CLUSTER_PORT_INCR;\n        }\n\n        if (clusterStartHandshake(szFromObj(c->argv[2]),port,cport) == 0 &&\n            errno == EINVAL)\n        {\n            addReplyErrorFormat(c,\"Invalid node address specified: %s:%s\",\n                            (char*)ptrFromObj(c->argv[2]), (char*)ptrFromObj(c->argv[3]));\n        } else {\n            addReply(c,shared.ok);\n        }\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"nodes\") && c->argc == 2) {\n        /* CLUSTER NODES */\n        /* Report plaintext ports, only if cluster is TLS but client is known to\n         * be non-TLS). */\n        int use_pport = (g_pserver->tls_cluster &&\n                         c->conn && connGetType(c->conn) != CONN_TYPE_TLS);\n        sds nodes = clusterGenNodesDescription(0, use_pport);\n        addReplyVerbatim(c,nodes,sdslen(nodes),\"txt\");\n        sdsfree(nodes);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"myid\") && c->argc == 2) {\n        /* CLUSTER MYID */\n        addReplyBulkCBuffer(c,myself->name, CLUSTER_NAMELEN);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"slots\") && c->argc == 2) {\n        /* CLUSTER SLOTS */\n        clusterReplyMultiBulkSlots(c);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"flushslots\") && c->argc == 2) {\n        /* CLUSTER FLUSHSLOTS */\n        if (g_pserver->db[0]->size() != 0) {\n            addReplyError(c,\"DB must be empty to perform CLUSTER FLUSHSLOTS.\");\n            return;\n        }\n        clusterDelNodeSlots(myself);\n        clusterDoBeforeSleep(CLUSTER_TODO_UPDATE_STATE|CLUSTER_TODO_SAVE_CONFIG);\n        addReply(c,shared.ok);\n    } else if ((!strcasecmp(szFromObj(c->argv[1]),\"addslots\") ||\n               !strcasecmp(szFromObj(c->argv[1]),\"delslots\")) && c->argc >= 3)\n    {\n        /* CLUSTER ADDSLOTS <slot> [slot] ... */\n        /* CLUSTER DELSLOTS <slot> [slot] ... */\n        int j, slot;\n        unsigned char *slots = (unsigned char*)zmalloc(CLUSTER_SLOTS, MALLOC_LOCAL);\n        int del = !strcasecmp(szFromObj(c->argv[1]),\"delslots\");\n\n        memset(slots,0,CLUSTER_SLOTS);\n        /* Check that all the arguments are parseable and that all the\n         * slots are not already busy. */\n        for (j = 2; j < c->argc; j++) {\n            if ((slot = getSlotOrReply(c,c->argv[j])) == -1) {\n                zfree(slots);\n                return;\n            }\n            if (del && g_pserver->cluster->slots[slot] == NULL) {\n                addReplyErrorFormat(c,\"Slot %d is already unassigned\", slot);\n                zfree(slots);\n                return;\n            } else if (!del && g_pserver->cluster->slots[slot]) {\n                addReplyErrorFormat(c,\"Slot %d is already busy\", slot);\n                zfree(slots);\n                return;\n            }\n            if (slots[slot]++ == 1) {\n                addReplyErrorFormat(c,\"Slot %d specified multiple times\",\n                    (int)slot);\n                zfree(slots);\n                return;\n            }\n        }\n        for (j = 0; j < CLUSTER_SLOTS; j++) {\n            if (slots[j]) {\n                int retval;\n\n                /* If this slot was set as importing we can clear this\n                 * state as now we are the real owner of the slot. */\n                if (g_pserver->cluster->importing_slots_from[j])\n                    g_pserver->cluster->importing_slots_from[j] = NULL;\n\n                retval = del ? clusterDelSlot(j) :\n                               clusterAddSlot(myself,j);\n                serverAssertWithInfo(c,NULL,retval == C_OK);\n            }\n        }\n        zfree(slots);\n        clusterDoBeforeSleep(CLUSTER_TODO_UPDATE_STATE|CLUSTER_TODO_SAVE_CONFIG);\n        addReply(c,shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"setslot\") && c->argc >= 4) {\n        /* SETSLOT 10 MIGRATING <node ID> */\n        /* SETSLOT 10 IMPORTING <node ID> */\n        /* SETSLOT 10 STABLE */\n        /* SETSLOT 10 NODE <node ID> */\n        int slot;\n        clusterNode *n;\n\n        if (nodeIsSlave(myself)) {\n            addReplyError(c,\"Please use SETSLOT only with masters.\");\n            return;\n        }\n\n        if ((slot = getSlotOrReply(c,c->argv[2])) == -1) return;\n\n        if (!strcasecmp(szFromObj(c->argv[3]),\"migrating\") && c->argc == 5) {\n            if (g_pserver->cluster->slots[slot] != myself) {\n                addReplyErrorFormat(c,\"I'm not the owner of hash slot %u\",slot);\n                return;\n            }\n            if ((n = clusterLookupNode(szFromObj(c->argv[4]))) == NULL) {\n                addReplyErrorFormat(c,\"I don't know about node %s\",\n                    (char*)ptrFromObj(c->argv[4]));\n                return;\n            }\n            g_pserver->cluster->migrating_slots_to[slot] = n;\n        } else if (!strcasecmp(szFromObj(c->argv[3]),\"importing\") && c->argc == 5) {\n            if (g_pserver->cluster->slots[slot] == myself) {\n                addReplyErrorFormat(c,\n                    \"I'm already the owner of hash slot %u\",slot);\n                return;\n            }\n            if ((n = clusterLookupNode(szFromObj(c->argv[4]))) == NULL) {\n                addReplyErrorFormat(c,\"I don't know about node %s\",\n                    (char*)ptrFromObj(c->argv[4]));\n                return;\n            }\n            g_pserver->cluster->importing_slots_from[slot] = n;\n        } else if (!strcasecmp(szFromObj(c->argv[3]),\"stable\") && c->argc == 4) {\n            /* CLUSTER SETSLOT <SLOT> STABLE */\n            g_pserver->cluster->importing_slots_from[slot] = NULL;\n            g_pserver->cluster->migrating_slots_to[slot] = NULL;\n        } else if (!strcasecmp(szFromObj(c->argv[3]),\"node\") && c->argc == 5) {\n            /* CLUSTER SETSLOT <SLOT> NODE <NODE ID> */\n            clusterNode *n = clusterLookupNode(szFromObj(c->argv[4]));\n\n            if (!n) {\n                addReplyErrorFormat(c,\"Unknown node %s\",\n                    (char*)ptrFromObj(c->argv[4]));\n                return;\n            }\n            /* If this hash slot was served by 'myself' before to switch\n             * make sure there are no longer local keys for this hash slot. */\n            if (g_pserver->cluster->slots[slot] == myself && n != myself) {\n                if (countKeysInSlot(slot) != 0) {\n                    addReplyErrorFormat(c,\n                        \"Can't assign hashslot %d to a different node \"\n                        \"while I still hold keys for this hash slot.\", slot);\n                    return;\n                }\n            }\n            /* If this slot is in migrating status but we have no keys\n             * for it assigning the slot to another node will clear\n             * the migrating status. */\n            if (countKeysInSlot(slot) == 0 &&\n                g_pserver->cluster->migrating_slots_to[slot])\n                g_pserver->cluster->migrating_slots_to[slot] = NULL;\n\n            clusterDelSlot(slot);\n            clusterAddSlot(n,slot);\n\n            /* If this node was importing this slot, assigning the slot to\n             * itself also clears the importing status. */\n            if (n == myself &&\n                g_pserver->cluster->importing_slots_from[slot])\n            {\n                /* This slot was manually migrated, set this node configEpoch\n                 * to a new epoch so that the new version can be propagated\n                 * by the cluster.\n                 *\n                 * Note that if this ever results in a collision with another\n                 * node getting the same configEpoch, for example because a\n                 * failover happens at the same time we close the slot, the\n                 * configEpoch collision resolution will fix it assigning\n                 * a different epoch to each node. */\n                if (clusterBumpConfigEpochWithoutConsensus() == C_OK) {\n                    serverLog(LL_WARNING,\n                        \"configEpoch updated after importing slot %d\", slot);\n                }\n                g_pserver->cluster->importing_slots_from[slot] = NULL;\n                /* After importing this slot, let the other nodes know as\n                 * soon as possible. */\n                clusterBroadcastPong(CLUSTER_BROADCAST_ALL);\n            }\n        } else {\n            addReplyError(c,\n                \"Invalid CLUSTER SETSLOT action or number of arguments. Try CLUSTER HELP\");\n            return;\n        }\n        clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG|CLUSTER_TODO_UPDATE_STATE);\n        addReply(c,shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"bumpepoch\") && c->argc == 2) {\n        /* CLUSTER BUMPEPOCH */\n        int retval = clusterBumpConfigEpochWithoutConsensus();\n        sds reply = sdscatprintf(sdsempty(),\"+%s %llu\\r\\n\",\n                (retval == C_OK) ? \"BUMPED\" : \"STILL\",\n                (unsigned long long) myself->configEpoch);\n        addReplySds(c,reply);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"info\") && c->argc == 2) {\n        /* CLUSTER INFO */\n        const char *statestr[] = {\"ok\",\"fail\",\"needhelp\"};\n        int slots_assigned = 0, slots_ok = 0, slots_pfail = 0, slots_fail = 0;\n        uint64_t myepoch;\n        int j;\n\n        for (j = 0; j < CLUSTER_SLOTS; j++) {\n            clusterNode *n = g_pserver->cluster->slots[j];\n\n            if (n == NULL) continue;\n            slots_assigned++;\n            if (nodeFailed(n)) {\n                slots_fail++;\n            } else if (nodeTimedOut(n)) {\n                slots_pfail++;\n            } else {\n                slots_ok++;\n            }\n        }\n\n        myepoch = (nodeIsSlave(myself) && myself->slaveof) ?\n                  myself->slaveof->configEpoch : myself->configEpoch;\n\n        sds info = sdscatprintf(sdsempty(),\n            \"cluster_state:%s\\r\\n\"\n            \"cluster_slots_assigned:%d\\r\\n\"\n            \"cluster_slots_ok:%d\\r\\n\"\n            \"cluster_slots_pfail:%d\\r\\n\"\n            \"cluster_slots_fail:%d\\r\\n\"\n            \"cluster_known_nodes:%lu\\r\\n\"\n            \"cluster_size:%d\\r\\n\"\n            \"cluster_current_epoch:%llu\\r\\n\"\n            \"cluster_my_epoch:%llu\\r\\n\"\n            , statestr[g_pserver->cluster->state],\n            slots_assigned,\n            slots_ok,\n            slots_pfail,\n            slots_fail,\n            dictSize(g_pserver->cluster->nodes),\n            g_pserver->cluster->size,\n            (unsigned long long) g_pserver->cluster->currentEpoch,\n            (unsigned long long) myepoch\n        );\n\n        /* Show stats about messages sent and received. */\n        long long tot_msg_sent = 0;\n        long long tot_msg_received = 0;\n\n        for (int i = 0; i < CLUSTERMSG_TYPE_COUNT; i++) {\n            if (g_pserver->cluster->stats_bus_messages_sent[i] == 0) continue;\n            tot_msg_sent += g_pserver->cluster->stats_bus_messages_sent[i];\n            info = sdscatprintf(info,\n                \"cluster_stats_messages_%s_sent:%lld\\r\\n\",\n                clusterGetMessageTypeString(i),\n                g_pserver->cluster->stats_bus_messages_sent[i]);\n        }\n        info = sdscatprintf(info,\n            \"cluster_stats_messages_sent:%lld\\r\\n\", tot_msg_sent);\n\n        for (int i = 0; i < CLUSTERMSG_TYPE_COUNT; i++) {\n            if (g_pserver->cluster->stats_bus_messages_received[i] == 0) continue;\n            tot_msg_received += g_pserver->cluster->stats_bus_messages_received[i];\n            info = sdscatprintf(info,\n                \"cluster_stats_messages_%s_received:%lld\\r\\n\",\n                clusterGetMessageTypeString(i),\n                g_pserver->cluster->stats_bus_messages_received[i]);\n        }\n        info = sdscatprintf(info,\n            \"cluster_stats_messages_received:%lld\\r\\n\", tot_msg_received);\n\n        /* Produce the reply protocol. */\n        addReplyVerbatim(c,info,sdslen(info),\"txt\");\n        sdsfree(info);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"saveconfig\") && c->argc == 2) {\n        int retval = clusterSaveConfig(1);\n\n        if (retval == 0)\n            addReply(c,shared.ok);\n        else\n            addReplyErrorFormat(c,\"error saving the cluster node config: %s\",\n                strerror(errno));\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"keyslot\") && c->argc == 3) {\n        /* CLUSTER KEYSLOT <key> */\n        sds key = szFromObj(c->argv[2]);\n\n        addReplyLongLong(c,keyHashSlot(key,sdslen(key)));\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"countkeysinslot\") && c->argc == 3) {\n        /* CLUSTER COUNTKEYSINSLOT <slot> */\n        long long slot;\n\n        if (getLongLongFromObjectOrReply(c,c->argv[2],&slot,NULL) != C_OK)\n            return;\n        if (slot < 0 || slot >= CLUSTER_SLOTS) {\n            addReplyError(c,\"Invalid slot\");\n            return;\n        }\n        addReplyLongLong(c,countKeysInSlot(slot));\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"getkeysinslot\") && c->argc == 4) {\n        /* CLUSTER GETKEYSINSLOT <slot> <count> */\n        long long maxkeys, slot;\n        unsigned int numkeys, j;\n        robj **keys;\n\n        if (getLongLongFromObjectOrReply(c,c->argv[2],&slot,NULL) != C_OK)\n            return;\n        if (getLongLongFromObjectOrReply(c,c->argv[3],&maxkeys,NULL)\n            != C_OK)\n            return;\n        if (slot < 0 || slot >= CLUSTER_SLOTS || maxkeys < 0) {\n            addReplyError(c,\"Invalid slot or number of keys\");\n            return;\n        }\n\n        /* Avoid allocating more than needed in case of large COUNT argument\n         * and smaller actual number of keys. */\n        unsigned int keys_in_slot = countKeysInSlot(slot);\n        if (maxkeys > keys_in_slot) maxkeys = keys_in_slot;\n\n        keys = (robj**)zmalloc(sizeof(robj*)*maxkeys, MALLOC_LOCAL);\n        numkeys = getKeysInSlot(slot, keys, maxkeys);\n        addReplyArrayLen(c,numkeys);\n        for (j = 0; j < numkeys; j++) {\n            addReplyBulk(c,keys[j]);\n            decrRefCount(keys[j]);\n        }\n        zfree(keys);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"forget\") && c->argc == 3) {\n        /* CLUSTER FORGET <NODE ID> */\n        clusterNode *n = clusterLookupNode(szFromObj(c->argv[2]));\n\n        if (!n) {\n            addReplyErrorFormat(c,\"Unknown node %s\", (char*)ptrFromObj(c->argv[2]));\n            return;\n        } else if (n == myself) {\n            addReplyError(c,\"I tried hard but I can't forget myself...\");\n            return;\n        } else if (nodeIsSlave(myself) && myself->slaveof == n) {\n            addReplyError(c,\"Can't forget my master!\");\n            return;\n        }\n        clusterBlacklistAddNode(n);\n        clusterDelNode(n);\n        clusterDoBeforeSleep(CLUSTER_TODO_UPDATE_STATE|\n                             CLUSTER_TODO_SAVE_CONFIG);\n        addReply(c,shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"replicate\") && (c->argc == 3 || c->argc == 4)) {\n        /* CLUSTER REPLICATE (<NODE ID>|NO ONE) */\n        clusterNode *n;\n        if (c->argc == 4) {\n            if (0 != strcasecmp(szFromObj(c->argv[2]),\"NO\") || 0 != strcasecmp(szFromObj(c->argv[3]),\"ONE\")) {\n                addReplySubcommandSyntaxError(c);\n                return;\n            }\n            n = nullptr;\n        } else {\n            /* Lookup the specified node in our table. */\n            n = clusterLookupNode(szFromObj(c->argv[2]));\n            if (n == nullptr) {\n                addReplyErrorFormat(c,\"Unknown node %s\", (char*)ptrFromObj(c->argv[2]));\n                return;\n            }\n        }\n\n        /* I can't replicate myself. */\n        if (n == myself) {\n            addReplyError(c,\"Can't replicate myself\");\n            return;\n        }\n\n        /* Can't replicate a slave. */\n        if (n != nullptr && nodeIsSlave(n)) {\n            addReplyError(c,\"I can only replicate a master, not a replica.\");\n            return;\n        }\n\n        /* If the instance is currently a master, it should have no assigned\n         * slots nor keys to accept to replicate some other node.\n         * Slaves can switch to another master without issues. */\n        if (nodeIsMaster(myself) &&\n            (myself->numslots != 0 || g_pserver->db[0]->size() != 0)) {\n            addReplyError(c,\n                \"To set a master the node must be empty and \"\n                \"without assigned slots.\");\n            return;\n        }\n\n        if (n == nullptr) {\n            if (nodeIsMaster(myself)) {\n                addReply(c,shared.ok);\n                return;\n            }\n            serverLog(LL_NOTICE,\"Stop replication and turning myself into empty primary.\");\n            clusterSetNodeAsMaster(myself);\n            if (listLength(g_pserver->masters) > 0)\n            {\n                serverAssert(listLength(g_pserver->masters) == 1);\n                replicationUnsetMaster((redisMaster*)listFirst(g_pserver->masters)->value);\n            }\n            int empty_db_flags = g_pserver->repl_slave_lazy_flush ? EMPTYDB_ASYNC : EMPTYDB_NO_FLAGS;\n            emptyDb(-1,empty_db_flags, nullptr);\n            /* Reset manual failover state. */\n            resetManualFailover();\n        } else {\n            /* Set the master. */\n            clusterSetMaster(n);\n        }\n        clusterDoBeforeSleep(CLUSTER_TODO_UPDATE_STATE|CLUSTER_TODO_SAVE_CONFIG);\n        addReply(c,shared.ok);\n    } else if ((!strcasecmp(szFromObj(c->argv[1]),\"slaves\") ||\n                !strcasecmp(szFromObj(c->argv[1]),\"replicas\")) && c->argc == 3) {\n        /* CLUSTER SLAVES <NODE ID> */\n        clusterNode *n = clusterLookupNode(szFromObj(c->argv[2]));\n        int j;\n\n        /* Lookup the specified node in our table. */\n        if (!n) {\n            addReplyErrorFormat(c,\"Unknown node %s\", (char*)ptrFromObj(c->argv[2]));\n            return;\n        }\n\n        if (nodeIsSlave(n)) {\n            addReplyError(c,\"The specified node is not a master\");\n            return;\n        }\n\n        /* Use plaintext port if cluster is TLS but client is non-TLS. */\n        int use_pport = (g_pserver->tls_cluster &&\n                         c->conn && connGetType(c->conn) != CONN_TYPE_TLS);\n        addReplyArrayLen(c,n->numslaves);\n        for (j = 0; j < n->numslaves; j++) {\n            sds ni = clusterGenNodeDescription(n->slaves[j], use_pport);\n            addReplyBulkCString(c,ni);\n            sdsfree(ni);\n        }\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"count-failure-reports\") &&\n               c->argc == 3)\n    {\n        /* CLUSTER COUNT-FAILURE-REPORTS <NODE ID> */\n        clusterNode *n = clusterLookupNode(szFromObj(c->argv[2]));\n\n        if (!n) {\n            addReplyErrorFormat(c,\"Unknown node %s\", (char*)ptrFromObj(c->argv[2]));\n            return;\n        } else {\n            addReplyLongLong(c,clusterNodeFailureReportsCount(n));\n        }\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"failover\") &&\n               (c->argc == 2 || c->argc == 3))\n    {\n        /* CLUSTER FAILOVER [FORCE|TAKEOVER] */\n        int force = 0, takeover = 0;\n\n        if (c->argc == 3) {\n            if (!strcasecmp(szFromObj(c->argv[2]),\"force\")) {\n                force = 1;\n            } else if (!strcasecmp(szFromObj(c->argv[2]),\"takeover\")) {\n                takeover = 1;\n                force = 1; /* Takeover also implies force. */\n            } else {\n                addReplyErrorObject(c,shared.syntaxerr);\n                return;\n            }\n        }\n\n        /* Check preconditions. */\n        if (nodeIsMaster(myself)) {\n            addReplyError(c,\"You should send CLUSTER FAILOVER to a replica\");\n            return;\n        } else if (myself->slaveof == NULL) {\n            addReplyError(c,\"I'm a replica but my master is unknown to me\");\n            return;\n        } else if (!force &&\n                   (nodeFailed(myself->slaveof) ||\n                    myself->slaveof->link == NULL))\n        {\n            addReplyError(c,\"Master is down or failed, \"\n                            \"please use CLUSTER FAILOVER FORCE\");\n            return;\n        }\n        resetManualFailover();\n        g_pserver->cluster->mf_end = mstime() + CLUSTER_MF_TIMEOUT;\n\n        if (takeover) {\n            /* A takeover does not perform any initial check. It just\n             * generates a new configuration epoch for this node without\n             * consensus, claims the master's slots, and broadcast the new\n             * configuration. */\n            serverLog(LL_WARNING,\"Taking over the master (user request).\");\n            clusterBumpConfigEpochWithoutConsensus();\n            clusterFailoverReplaceYourMaster();\n        } else if (force) {\n            /* If this is a forced failover, we don't need to talk with our\n             * master to agree about the offset. We just failover taking over\n             * it without coordination. */\n            serverLog(LL_WARNING,\"Forced failover user request accepted.\");\n            g_pserver->cluster->mf_can_start = 1;\n        } else {\n            serverLog(LL_WARNING,\"Manual failover user request accepted.\");\n            clusterSendMFStart(myself->slaveof);\n        }\n        addReply(c,shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"set-config-epoch\") && c->argc == 3)\n    {\n        /* CLUSTER SET-CONFIG-EPOCH <epoch>\n         *\n         * The user is allowed to set the config epoch only when a node is\n         * totally fresh: no config epoch, no other known node, and so forth.\n         * This happens at cluster creation time to start with a cluster where\n         * every node has a different node ID, without to rely on the conflicts\n         * resolution system which is too slow when a big cluster is created. */\n        long long epoch;\n\n        if (getLongLongFromObjectOrReply(c,c->argv[2],&epoch,NULL) != C_OK)\n            return;\n\n        if (epoch < 0) {\n            addReplyErrorFormat(c,\"Invalid config epoch specified: %lld\",epoch);\n        } else if (dictSize(g_pserver->cluster->nodes) > 1) {\n            addReplyError(c,\"The user can assign a config epoch only when the \"\n                            \"node does not know any other node.\");\n        } else if (myself->configEpoch != 0) {\n            addReplyError(c,\"Node config epoch is already non-zero\");\n        } else {\n            myself->configEpoch = epoch;\n            serverLog(LL_WARNING,\n                \"configEpoch set to %llu via CLUSTER SET-CONFIG-EPOCH\",\n                (unsigned long long) myself->configEpoch);\n\n            if (g_pserver->cluster->currentEpoch < (uint64_t)epoch)\n                g_pserver->cluster->currentEpoch = epoch;\n            /* No need to fsync the config here since in the unlucky event\n             * of a failure to persist the config, the conflict resolution code\n             * will assign a unique config to this node. */\n            clusterDoBeforeSleep(CLUSTER_TODO_UPDATE_STATE|\n                                 CLUSTER_TODO_SAVE_CONFIG);\n            addReply(c,shared.ok);\n        }\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"reset\") &&\n               (c->argc == 2 || c->argc == 3))\n    {\n        /* CLUSTER RESET [SOFT|HARD] */\n        int hard = 0;\n\n        /* Parse soft/hard argument. Default is soft. */\n        if (c->argc == 3) {\n            if (!strcasecmp(szFromObj(c->argv[2]),\"hard\")) {\n                hard = 1;\n            } else if (!strcasecmp(szFromObj(c->argv[2]),\"soft\")) {\n                hard = 0;\n            } else {\n                addReplyErrorObject(c,shared.syntaxerr);\n                return;\n            }\n        }\n\n        /* Slaves can be reset while containing data, but not master nodes\n         * that must be empty. */\n        if (nodeIsMaster(myself) && c->db->size() != 0) {\n            addReplyError(c,\"CLUSTER RESET can't be called with \"\n                            \"master nodes containing keys\");\n            return;\n        }\n        clusterReset(hard);\n        addReply(c,shared.ok);\n    } else {\n        addReplySubcommandSyntaxError(c);\n        return;\n    }\n}\n\n/* -----------------------------------------------------------------------------\n * DUMP, RESTORE and MIGRATE commands\n * -------------------------------------------------------------------------- */\nssize_t rdbSaveAuxFieldStrStr(rio *rdb, const char *key, const char *val);\n\n/* Generates a DUMP-format representation of the object 'o', adding it to the\n * io stream pointed by 'rio'. This function can't fail. */\nvoid createDumpPayload(rio *payload, robj_roptr o, robj *key) {\n    unsigned char buf[2];\n    uint64_t crc;\n\n    /* Serialize the object in an RDB-like format. It consist of an object type\n     * byte followed by the serialized object. This is understood by RESTORE. */\n    rioInitWithBuffer(payload,sdsempty());\n    serverAssert(rdbSaveObjectType(payload,o));\n    serverAssert(rdbSaveObject(payload,o,key));\n    char szT[32];\n    uint64_t mvcc = mvccFromObj(o);\n    snprintf(szT, sizeof(szT), \"%\" PRIu64, mvcc);\n    serverAssert(rdbSaveAuxFieldStrStr(payload,\"mvcc-tstamp\", szT) != -1);\n\n    /* Write the footer, this is how it looks like:\n     * ----------------+---------------------+---------------+\n     * ... RDB payload | 2 bytes RDB version | 8 bytes CRC64 |\n     * ----------------+---------------------+---------------+\n     * RDB version and CRC are both in little endian.\n     */\n\n    /* RDB version */\n    buf[0] = RDB_VERSION & 0xff;\n    buf[1] = (RDB_VERSION >> 8) & 0xff;\n    payload->io.buffer.ptr = sdscatlen(payload->io.buffer.ptr,buf,2);\n\n    /* CRC64 */\n    crc = crc64(0,(unsigned char*)payload->io.buffer.ptr,\n                sdslen(payload->io.buffer.ptr));\n    memrev64ifbe(&crc);\n    payload->io.buffer.ptr = sdscatlen(payload->io.buffer.ptr,&crc,8);\n}\n\n/* Verify that the RDB version of the dump payload matches the one of this Redis\n * instance and that the checksum is ok.\n * If the DUMP payload looks valid C_OK is returned, otherwise C_ERR\n * is returned. */\nint verifyDumpPayload(unsigned char *p, size_t len) {\n    unsigned char *footer;\n    uint16_t rdbver;\n    uint64_t crc;\n\n    /* At least 2 bytes of RDB version and 8 of CRC64 should be present. */\n    if (len < 10) return C_ERR;\n    footer = p+(len-10);\n\n    /* Verify RDB version */\n    rdbver = (footer[1] << 8) | footer[0];\n    if (rdbver > RDB_VERSION) return C_ERR;\n\n    if (cserver.skip_checksum_validation)\n        return C_OK;\n\n    /* Verify CRC64 */\n    crc = crc64(0,p,len-8);\n    memrev64ifbe(&crc);\n    return (memcmp(&crc,footer+2,8) == 0) ? C_OK : C_ERR;\n}\n\n/* DUMP keyname\n * DUMP is actually not used by Redis Cluster but it is the obvious\n * complement of RESTORE and can be useful for different applications. */\nvoid dumpCommand(client *c) {\n    robj_roptr o;\n    rio payload;\n\n    /* Check if the key is here. */\n    if ((o = lookupKeyRead(c->db,c->argv[1])) == nullptr) {\n        addReplyNull(c);\n        return;\n    }\n\n    /* Create the DUMP encoded representation. */\n    createDumpPayload(&payload,o,c->argv[1]);\n\n    /* Transfer to the client */\n    addReplyBulkSds(c,payload.io.buffer.ptr);\n    return;\n}\n\n/* KEYDB.MVCCRESTORE key mvcc expire serialized-value */\nvoid mvccrestoreCommand(client *c) {\n    long long expire;\n    uint64_t mvcc;\n    robj *key = c->argv[1], *obj = nullptr;\n    int type;\n    \n    if (getUnsignedLongLongFromObjectOrReply(c, c->argv[2], &mvcc, \"Invalid MVCC Tstamp\") != C_OK)\n        return;\n\n    if (getLongLongFromObjectOrReply(c, c->argv[3], &expire, \"Invalid expire\") != C_OK)\n        return;\n\n    /* Verify RDB version and data checksum unles the client is already a replica or master */\n    if (!(c->flags & (CLIENT_SLAVE | CLIENT_MASTER))) {\n        if (verifyDumpPayload((unsigned char*)ptrFromObj(c->argv[4]),sdslen(szFromObj(c->argv[4]))) == C_ERR)\n        {\n            addReplyError(c,\"DUMP payload version or checksum are wrong\");\n            return;\n        }\n    }\n\n    rio payload;\n    rioInitWithBuffer(&payload,szFromObj(c->argv[4]));\n    if (((type = rdbLoadObjectType(&payload)) == -1) ||\n        ((obj = rdbLoadObject(type,&payload,szFromObj(key),NULL,OBJ_MVCC_INVALID)) == NULL))\n    {\n        addReplyError(c,\"Bad data format\");\n        return;\n    }\n    setMvccTstamp(obj, mvcc);\n\n    /* Create the key and set the TTL if any */\n    if (dbMerge(c->db,szFromObj(key),obj,true)) {\n        if (expire >= 0) {\n            setExpire(c,c->db,key,nullptr,expire);\n        }\n        signalModifiedKey(c,c->db,key);\n        notifyKeyspaceEvent(NOTIFY_GENERIC,\"restore\",key,c->db->id);\n    } else {\n        decrRefCount(obj);\n    }\n    addReply(c,shared.ok);\n    g_pserver->dirty++;\n}\n\n/* RESTORE key ttl serialized-value [REPLACE] */\nvoid restoreCommand(client *c) {\n    long long ttl, lfu_freq = -1, lru_idle = -1, lru_clock = -1;\n    rio payload;\n    int j, type, replace = 0, absttl = 0;\n    robj *obj;\n\n    /* Parse additional options */\n    for (j = 4; j < c->argc; j++) {\n        int additional = c->argc-j-1;\n        if (!strcasecmp(szFromObj(c->argv[j]),\"replace\")) {\n            replace = 1;\n        } else if (!strcasecmp(szFromObj(c->argv[j]),\"absttl\")) {\n            absttl = 1;\n        } else if (!strcasecmp(szFromObj(c->argv[j]),\"idletime\") && additional >= 1 &&\n                   lfu_freq == -1)\n        {\n            if (getLongLongFromObjectOrReply(c,c->argv[j+1],&lru_idle,NULL)\n                    != C_OK) return;\n            if (lru_idle < 0) {\n                addReplyError(c,\"Invalid IDLETIME value, must be >= 0\");\n                return;\n            }\n            lru_clock = LRU_CLOCK();\n            j++; /* Consume additional arg. */\n        } else if (!strcasecmp(szFromObj(c->argv[j]),\"freq\") && additional >= 1 &&\n                   lru_idle == -1)\n        {\n            if (getLongLongFromObjectOrReply(c,c->argv[j+1],&lfu_freq,NULL)\n                    != C_OK) return;\n            if (lfu_freq < 0 || lfu_freq > 255) {\n                addReplyError(c,\"Invalid FREQ value, must be >= 0 and <= 255\");\n                return;\n            }\n            j++; /* Consume additional arg. */\n        } else {\n            addReplyErrorObject(c,shared.syntaxerr);\n            return;\n        }\n    }\n\n    /* Make sure this key does not already exist here... */\n    robj *key = c->argv[1];\n    if (!replace && lookupKeyWrite(c->db,key) != NULL) {\n        addReplyErrorObject(c,shared.busykeyerr);\n        return;\n    }\n\n    /* Check if the TTL value makes sense */\n    if (getLongLongFromObjectOrReply(c,c->argv[2],&ttl,NULL) != C_OK) {\n        return;\n    } else if (ttl < 0) {\n        addReplyError(c,\"Invalid TTL value, must be >= 0\");\n        return;\n    }\n\n    /* Verify RDB version and data checksum. */\n    if (verifyDumpPayload((unsigned char*)ptrFromObj(c->argv[3]),sdslen(szFromObj(c->argv[3]))) == C_ERR)\n    {\n        addReplyError(c,\"DUMP payload version or checksum are wrong\");\n        return;\n    }\n\n    rioInitWithBuffer(&payload,szFromObj(c->argv[3]));\n    if (((type = rdbLoadObjectType(&payload)) == -1) ||\n        ((obj = rdbLoadObject(type,&payload,szFromObj(key),NULL,OBJ_MVCC_INVALID)) == NULL))\n    {\n        addReplyError(c,\"Bad data format\");\n        return;\n    }\n    if (rdbLoadType(&payload) == RDB_OPCODE_AUX)\n    {\n        robj *auxkey, *auxval;\n        if ((auxkey = rdbLoadStringObject(&payload)) == NULL) goto eoferr;\n        if ((auxval = rdbLoadStringObject(&payload)) == NULL) {\n            decrRefCount(auxkey);\n            goto eoferr;\n        }\n        if (strcasecmp(szFromObj(auxkey), \"mvcc-tstamp\") == 0) {\n            setMvccTstamp(obj, strtoull(szFromObj(auxval), nullptr, 10));\n        }\n        decrRefCount(auxkey);\n        decrRefCount(auxval);\n    }\neoferr:\n\n    /* Remove the old key if needed. */\n    int deleted = 0;\n    if (replace)\n        deleted = dbDelete(c->db,key);\n\n    if (ttl && !absttl) ttl+=mstime();\n    if (ttl && checkAlreadyExpired(ttl)) {\n        if (deleted) {\n            rewriteClientCommandVector(c,2,shared.del,key);\n            signalModifiedKey(c,c->db,key);\n            notifyKeyspaceEvent(NOTIFY_GENERIC,\"del\",key,c->db->id);\n            g_pserver->dirty++;\n        }\n        decrRefCount(obj);\n        addReply(c, shared.ok);\n        return;\n    }\n\n    /* Create the key and set the TTL if any */\n    dbAdd(c->db,key,obj);\n    if (ttl) {\n        setExpire(c,c->db,key,nullptr,ttl);\n    }\n    objectSetLRUOrLFU(obj,lfu_freq,lru_idle,lru_clock,1000);\n    signalModifiedKey(c,c->db,key);\n    notifyKeyspaceEvent(NOTIFY_GENERIC,\"restore\",key,c->db->id);\n    addReply(c,shared.ok);\n    g_pserver->dirty++;\n}\n\n/* MIGRATE socket cache implementation.\n *\n * We take a map between host:ip and a TCP socket that we used to connect\n * to this instance in recent time.\n * This sockets are closed when the max number we cache is reached, and also\n * in serverCron() when they are around for more than a few seconds. */\n#define MIGRATE_SOCKET_CACHE_ITEMS 64 /* max num of items in the cache. */\n#define MIGRATE_SOCKET_CACHE_TTL 10 /* close cached sockets after 10 sec. */\n\ntypedef struct migrateCachedSocket {\n    connection *conn;\n    long last_dbid;\n    time_t last_use_time;\n} migrateCachedSocket;\n\n/* Return a migrateCachedSocket containing a TCP socket connected with the\n * target instance, possibly returning a cached one.\n *\n * This function is responsible of sending errors to the client if a\n * connection can't be established. In this case -1 is returned.\n * Otherwise on success the socket is returned, and the caller should not\n * attempt to free it after usage.\n *\n * If the caller detects an error while using the socket, migrateCloseSocket()\n * should be called so that the connection will be created from scratch\n * the next time. */\nmigrateCachedSocket* migrateGetSocket(client *c, robj *host, robj *port, long timeout) {\n    connection *conn;\n    sds name = sdsempty();\n    migrateCachedSocket *cs;\n\n    /* Check if we have an already cached socket for this ip:port pair. */\n    name = sdscatlen(name,ptrFromObj(host),sdslen(szFromObj(host)));\n    name = sdscatlen(name,\":\",1);\n    name = sdscatlen(name,ptrFromObj(port),sdslen(szFromObj(port)));\n    cs = (migrateCachedSocket*)dictFetchValue(g_pserver->migrate_cached_sockets,name);\n    if (cs) {\n        sdsfree(name);\n        cs->last_use_time = g_pserver->unixtime;\n        return cs;\n    }\n\n    /* No cached socket, create one. */\n    if (dictSize(g_pserver->migrate_cached_sockets) == MIGRATE_SOCKET_CACHE_ITEMS) {\n        /* Too many items, drop one at random. */\n        dictEntry *de = dictGetRandomKey(g_pserver->migrate_cached_sockets);\n        cs = (migrateCachedSocket*)dictGetVal(de);\n        connClose(cs->conn);\n        zfree(cs);\n        dictDelete(g_pserver->migrate_cached_sockets,dictGetKey(de));\n    }\n\n    /* Create the socket */\n    conn = g_pserver->tls_cluster ? connCreateTLS() : connCreateSocket();\n    if (connBlockingConnect(conn, szFromObj(c->argv[1]), atoi(szFromObj(c->argv[2])), timeout)\n            != C_OK) {\n        addReplyError(c,\"-IOERR error or timeout connecting to the client\");\n        connClose(conn);\n        sdsfree(name);\n        return NULL;\n    }\n    connEnableTcpNoDelay(conn);\n\n    /* Add to the cache and return it to the caller. */\n    cs = (migrateCachedSocket*)zmalloc(sizeof(*cs), MALLOC_LOCAL);\n    cs->conn = conn;\n    cs->last_dbid = -1;\n    cs->last_use_time = g_pserver->unixtime;\n    dictAdd(g_pserver->migrate_cached_sockets,name,cs);\n    return cs;\n}\n\n/* Free a migrate cached connection. */\nvoid migrateCloseSocket(robj *host, robj *port) {\n    sds name = sdsempty();\n    migrateCachedSocket *cs;\n\n    name = sdscatlen(name,ptrFromObj(host),sdslen(szFromObj(host)));\n    name = sdscatlen(name,\":\",1);\n    name = sdscatlen(name,ptrFromObj(port),sdslen(szFromObj(port)));\n    cs = (migrateCachedSocket*)dictFetchValue(g_pserver->migrate_cached_sockets,name);\n    if (!cs) {\n        sdsfree(name);\n        return;\n    }\n\n    connClose(cs->conn);\n    zfree(cs);\n    dictDelete(g_pserver->migrate_cached_sockets,name);\n    sdsfree(name);\n}\n\nvoid migrateCloseTimedoutSockets(void) {\n    dictIterator *di = dictGetSafeIterator(g_pserver->migrate_cached_sockets);\n    dictEntry *de;\n\n    while((de = dictNext(di)) != NULL) {\n        migrateCachedSocket *cs = (migrateCachedSocket*)dictGetVal(de);\n\n        if ((g_pserver->unixtime - cs->last_use_time) > MIGRATE_SOCKET_CACHE_TTL) {\n            connClose(cs->conn);\n            zfree(cs);\n            dictDelete(g_pserver->migrate_cached_sockets,dictGetKey(de));\n        }\n    }\n    dictReleaseIterator(di);\n}\n\n/* MIGRATE host port key dbid timeout [COPY | REPLACE | AUTH password |\n *         AUTH2 username password]\n *\n * On in the multiple keys form:\n *\n * MIGRATE host port \"\" dbid timeout [COPY | REPLACE | AUTH password |\n *         AUTH2 username password] KEYS key1 key2 ... keyN */\nvoid migrateCommand(client *c) {\n    migrateCachedSocket *cs;\n    int copy = 0, replace = 0, j;\n    char *username = NULL;\n    char *password = NULL;\n    long timeout;\n    long dbid;\n    robj_roptr *ov = NULL; /* Objects to migrate. */\n    robj **kv = NULL; /* Key names. */\n    robj **newargv = NULL; /* Used to rewrite the command as DEL ... keys ... */\n    rio cmd, payload;\n    int may_retry = 1;\n    int write_error = 0;\n    int argv_rewritten = 0;\n\n    /* To support the KEYS option we need the following additional state. */\n    int first_key = 3; /* Argument index of the first key. */\n    int num_keys = 1;  /* By default only migrate the 'key' argument. */\n\n    /* Parse additional options */\n    for (j = 6; j < c->argc; j++) {\n        int moreargs = (c->argc-1) - j;\n        if (!strcasecmp(szFromObj(c->argv[j]),\"copy\")) {\n            copy = 1;\n        } else if (!strcasecmp(szFromObj(c->argv[j]),\"replace\")) {\n            replace = 1;\n        } else if (!strcasecmp(szFromObj(c->argv[j]),\"auth\")) {\n            if (!moreargs) {\n                addReplyErrorObject(c,shared.syntaxerr);\n                return;\n            }\n            j++;\n            password = szFromObj(c->argv[j]);\n            redactClientCommandArgument(c,j);\n        } else if (!strcasecmp(szFromObj(c->argv[j]),\"auth2\")) {\n            if (moreargs < 2) {\n                addReply(c,shared.syntaxerr);\n                return;\n            }\n            username = szFromObj(c->argv[++j]);\n            redactClientCommandArgument(c,j);\n            password = szFromObj(c->argv[++j]);\n            redactClientCommandArgument(c,j);\n        } else if (!strcasecmp(szFromObj(c->argv[j]),\"keys\")) {\n            if (sdslen(szFromObj(c->argv[3])) != 0) {\n                addReplyError(c,\n                    \"When using MIGRATE KEYS option, the key argument\"\n                    \" must be set to the empty string\");\n                return;\n            }\n            first_key = j+1;\n            num_keys = c->argc - j - 1;\n            break; /* All the remaining args are keys. */\n        } else {\n            addReplyErrorObject(c,shared.syntaxerr);\n            return;\n        }\n    }\n\n    /* Sanity check */\n    if (getLongFromObjectOrReply(c,c->argv[5],&timeout,NULL) != C_OK ||\n        getLongFromObjectOrReply(c,c->argv[4],&dbid,NULL) != C_OK)\n    {\n        return;\n    }\n    if (timeout <= 0) timeout = 1000;\n\n    /* Check if the keys are here. If at least one key is to migrate, do it\n     * otherwise if all the keys are missing reply with \"NOKEY\" to signal\n     * the caller there was nothing to migrate. We don't return an error in\n     * this case, since often this is due to a normal condition like the key\n     * expiring in the meantime. */\n    ov = (robj_roptr*)zrealloc(ov,sizeof(robj_roptr)*num_keys, MALLOC_LOCAL);\n    kv = (robj**)zrealloc(kv,sizeof(robj*)*num_keys, MALLOC_LOCAL);\n    int oi = 0;\n\n    for (j = 0; j < num_keys; j++) {\n        if ((ov[oi] = lookupKeyRead(c->db,c->argv[first_key+j])) != nullptr) {\n            kv[oi] = c->argv[first_key+j];\n            oi++;\n        }\n    }\n    num_keys = oi;\n    if (num_keys == 0) {\n        zfree(ov); zfree(kv);\n        addReplySds(c,sdsnew(\"+NOKEY\\r\\n\"));\n        return;\n    }\n\ntry_again:\n    write_error = 0;\n\n    /* Connect */\n    cs = migrateGetSocket(c,c->argv[1],c->argv[2],timeout);\n    if (cs == NULL) {\n        zfree(ov); zfree(kv);\n        return; /* error sent to the client by migrateGetSocket() */\n    }\n    connMarshalThread(cs->conn);\n\n    rioInitWithBuffer(&cmd,sdsempty());\n\n    /* Authentication */\n    if (password) {\n        int arity = username ? 3 : 2;\n        serverAssertWithInfo(c,NULL,rioWriteBulkCount(&cmd,'*',arity));\n        serverAssertWithInfo(c,NULL,rioWriteBulkString(&cmd,\"AUTH\",4));\n        if (username) {\n            serverAssertWithInfo(c,NULL,rioWriteBulkString(&cmd,username,\n                                 sdslen(username)));\n        }\n        serverAssertWithInfo(c,NULL,rioWriteBulkString(&cmd,password,\n            sdslen(password)));\n    }\n\n    /* Send the SELECT command if the current DB is not already selected. */\n    int select = cs->last_dbid != dbid; /* Should we emit SELECT? */\n    if (select) {\n        serverAssertWithInfo(c,NULL,rioWriteBulkCount(&cmd,'*',2));\n        serverAssertWithInfo(c,NULL,rioWriteBulkString(&cmd,\"SELECT\",6));\n        serverAssertWithInfo(c,NULL,rioWriteBulkLongLong(&cmd,dbid));\n    }\n\n    int non_expired = 0; /* Number of keys that we'll find non expired.\n                            Note that serializing large keys may take some time\n                            so certain keys that were found non expired by the\n                            lookupKey() function, may be expired later. */\n\n    /* Create RESTORE payload and generate the protocol to call the command. */\n    for (j = 0; j < num_keys; j++) {\n        long long ttl = 0;\n        expireEntry *pexpire = c->db->getExpire(kv[j]);\n        long long expireat = INVALID_EXPIRE;\n        if (pexpire != nullptr)\n            pexpire->FGetPrimaryExpire(&expireat);\n\n        if (expireat != INVALID_EXPIRE) {\n            ttl = expireat-mstime();\n            if (ttl < 0) {\n                continue;\n            }\n            if (ttl < 1) ttl = 1;\n        }\n\n        /* Relocate valid (non expired) keys and values into the array in successive\n         * positions to remove holes created by the keys that were present\n         * in the first lookup but are now expired after the second lookup. */\n        ov[non_expired] = ov[j];\n        kv[non_expired++] = kv[j];\n\n        serverAssertWithInfo(c,NULL,\n            rioWriteBulkCount(&cmd,'*',replace ? 5 : 4));\n\n        if (g_pserver->cluster_enabled)\n            serverAssertWithInfo(c,NULL,\n                rioWriteBulkString(&cmd,\"RESTORE-ASKING\",14));\n        else\n            serverAssertWithInfo(c,NULL,rioWriteBulkString(&cmd,\"RESTORE\",7));\n        serverAssertWithInfo(c,NULL,sdsEncodedObject(kv[j]));\n        serverAssertWithInfo(c,NULL,rioWriteBulkString(&cmd,szFromObj(kv[j]),\n                sdslen(szFromObj(kv[j]))));\n        serverAssertWithInfo(c,NULL,rioWriteBulkLongLong(&cmd,ttl));\n\n        /* Emit the payload argument, that is the serialized object using\n         * the DUMP format. */\n        createDumpPayload(&payload,ov[j],kv[j]);\n        serverAssertWithInfo(c,NULL,\n            rioWriteBulkString(&cmd,payload.io.buffer.ptr,\n                               sdslen(payload.io.buffer.ptr)));\n        sdsfree(payload.io.buffer.ptr);\n\n        /* Add the REPLACE option to the RESTORE command if it was specified\n         * as a MIGRATE option. */\n        if (replace)\n            serverAssertWithInfo(c,NULL,rioWriteBulkString(&cmd,\"REPLACE\",7));\n    }\n\n    /* Fix the actual number of keys we are migrating. */\n    num_keys = non_expired;\n\n    char buf0[1024]; /* Auth reply. */\n    char buf1[1024]; /* Select reply. */\n    char buf2[1024]; /* Restore reply. */\n    int error_from_target = 0;\n    int socket_error = 0;\n    int del_idx = 1; /* Index of the key argument for the replicated DEL op. */\n\n    /* Transfer the query to the other node in 64K chunks. */\n    errno = 0;\n    {\n        sds buf = cmd.io.buffer.ptr;\n        size_t pos = 0, towrite;\n        int nwritten = 0;\n\n        while ((towrite = sdslen(buf)-pos) > 0) {\n            towrite = (towrite > (64*1024) ? (64*1024) : towrite);\n            nwritten = connSyncWrite(cs->conn,buf+pos,towrite,timeout);\n            if (nwritten != (signed)towrite) {\n                write_error = 1;\n                goto socket_err;\n            }\n            pos += nwritten;\n        }\n    }\n\n\n    /* Read the AUTH reply if needed. */\n    if (password && connSyncReadLine(cs->conn, buf0, sizeof(buf0), timeout) <= 0)\n        goto socket_err;\n\n    /* Read the SELECT reply if needed. */\n    if (select && connSyncReadLine(cs->conn, buf1, sizeof(buf1), timeout) <= 0)\n        goto socket_err;\n\n    /* Allocate the new argument vector that will replace the current command,\n     * to propagate the MIGRATE as a DEL command (if no COPY option was given).\n     * We allocate num_keys+1 because the additional argument is for \"DEL\"\n     * command name itself. */\n    if (!copy) newargv = (robj**)zmalloc(sizeof(robj*)*(num_keys+1), MALLOC_LOCAL);\n\n    /* Read the RESTORE replies. */\n    for (j = 0; j < num_keys; j++) {\n        if (connSyncReadLine(cs->conn, buf2, sizeof(buf2), timeout) <= 0) {\n            socket_error = 1;\n            break;\n        }\n        if ((password && buf0[0] == '-') ||\n            (select && buf1[0] == '-') ||\n            buf2[0] == '-')\n        {\n            /* On error assume that last_dbid is no longer valid. */\n            if (!error_from_target) {\n                cs->last_dbid = -1;\n                char *errbuf;\n                if (password && buf0[0] == '-') errbuf = buf0;\n                else if (select && buf1[0] == '-') errbuf = buf1;\n                else errbuf = buf2;\n\n                error_from_target = 1;\n                addReplyErrorFormat(c,\"Target instance replied with error: %s\",\n                    errbuf+1);\n            }\n        } else {\n            if (!copy) {\n                /* No COPY option: remove the local key, signal the change. */\n                dbDelete(c->db,kv[j]);\n                signalModifiedKey(c,c->db,kv[j]);\n                notifyKeyspaceEvent(NOTIFY_GENERIC,\"del\",kv[j],c->db->id);\n                g_pserver->dirty++;\n\n                /* Populate the argument vector to replace the old one. */\n                newargv[del_idx++] = kv[j];\n                incrRefCount(kv[j]);\n            }\n        }\n    }\n\n    /* On socket error, if we want to retry, do it now before rewriting the\n     * command vector. We only retry if we are sure nothing was processed\n     * and we failed to read the first reply (j == 0 test). */\n    if (!error_from_target && socket_error && j == 0 && may_retry &&\n        errno != ETIMEDOUT)\n    {\n        goto socket_err; /* A retry is guaranteed because of tested conditions.*/\n    }\n\n    /* On socket errors, close the migration socket now that we still have\n     * the original host/port in the ARGV. Later the original command may be\n     * rewritten to DEL and will be too later. */\n    if (socket_error) migrateCloseSocket(c->argv[1],c->argv[2]);\n\n    if (!copy) {\n        /* Translate MIGRATE as DEL for replication/AOF. Note that we do\n         * this only for the keys for which we received an acknowledgement\n         * from the receiving Redis server, by using the del_idx index. */\n        if (del_idx > 1) {\n            newargv[0] = createStringObject(\"DEL\",3);\n            /* Note that the following call takes ownership of newargv. */\n            replaceClientCommandVector(c,del_idx,newargv);\n            argv_rewritten = 1;\n        } else {\n            /* No key transfer acknowledged, no need to rewrite as DEL. */\n            zfree(newargv);\n        }\n        newargv = NULL; /* Make it safe to call zfree() on it in the future. */\n    }\n\n    /* If we are here and a socket error happened, we don't want to retry.\n     * Just signal the problem to the client, but only do it if we did not\n     * already queue a different error reported by the destination g_pserver-> */\n    if (!error_from_target && socket_error) {\n        may_retry = 0;\n        goto socket_err;\n    }\n\n    if (!error_from_target) {\n        /* Success! Update the last_dbid in migrateCachedSocket, so that we can\n         * avoid SELECT the next time if the target DB is the same. Reply +OK.\n         *\n         * Note: If we reached this point, even if socket_error is true\n         * still the SELECT command succeeded (otherwise the code jumps to\n         * socket_err label. */\n        cs->last_dbid = dbid;\n        addReply(c,shared.ok);\n    } else {\n        /* On error we already sent it in the for loop above, and set\n         * the currently selected socket to -1 to force SELECT the next time. */\n    }\n\n    sdsfree(cmd.io.buffer.ptr);\n    zfree(ov); zfree(kv); zfree(newargv);\n    return;\n\n/* On socket errors we try to close the cached socket and try again.\n * It is very common for the cached socket to get closed, if just reopening\n * it works it's a shame to notify the error to the caller. */\nsocket_err:\n    /* Cleanup we want to perform in both the retry and no retry case.\n     * Note: Closing the migrate socket will also force SELECT next time. */\n    sdsfree(cmd.io.buffer.ptr);\n\n    /* If the command was rewritten as DEL and there was a socket error,\n     * we already closed the socket earlier. While migrateCloseSocket()\n     * is idempotent, the host/port arguments are now gone, so don't do it\n     * again. */\n    if (!argv_rewritten) migrateCloseSocket(c->argv[1],c->argv[2]);\n    zfree(newargv);\n    newargv = NULL; /* This will get reallocated on retry. */\n\n    /* Retry only if it's not a timeout and we never attempted a retry\n     * (or the code jumping here did not set may_retry to zero). */\n    if (errno != ETIMEDOUT && may_retry) {\n        may_retry = 0;\n        goto try_again;\n    }\n\n    /* Cleanup we want to do if no retry is attempted. */\n    zfree(ov); zfree(kv);\n    addReplySds(c,\n        sdscatprintf(sdsempty(),\n            \"-IOERR error or timeout %s to target instance\\r\\n\",\n            write_error ? \"writing\" : \"reading\"));\n    return;\n}\n\n/* -----------------------------------------------------------------------------\n * Cluster functions related to serving / redirecting clients\n * -------------------------------------------------------------------------- */\n\n/* The ASKING command is required after a -ASK redirection.\n * The client should issue ASKING before to actually send the command to\n * the target instance. See the Redis Cluster specification for more\n * information. */\nvoid askingCommand(client *c) {\n    serverAssert(GlobalLocksAcquired());\n    if (g_pserver->cluster_enabled == 0) {\n        addReplyError(c,\"This instance has cluster support disabled\");\n        return;\n    }\n    c->flags |= CLIENT_ASKING;\n    addReply(c,shared.ok);\n}\n\n/* The READONLY command is used by clients to enter the read-only mode.\n * In this mode slaves will not redirect clients as long as clients access\n * with read-only commands to keys that are served by the slave's master. */\nvoid readonlyCommand(client *c) {\n    serverAssert(GlobalLocksAcquired());\n    if (g_pserver->cluster_enabled == 0) {\n        addReplyError(c,\"This instance has cluster support disabled\");\n        return;\n    }\n    c->flags |= CLIENT_READONLY;\n    addReply(c,shared.ok);\n}\n\n/* The READWRITE command just clears the READONLY command state. */\nvoid readwriteCommand(client *c) {\n    serverAssert(GlobalLocksAcquired());\n    c->flags &= ~CLIENT_READONLY;\n    addReply(c,shared.ok);\n}\n\n/* Return the pointer to the cluster node that is able to serve the command.\n * For the function to succeed the command should only target either:\n *\n * 1) A single key (even multiple times like LPOPRPUSH mylist mylist).\n * 2) Multiple keys in the same hash slot, while the slot is stable (no\n *    resharding in progress).\n *\n * On success the function returns the node that is able to serve the request.\n * If the node is not 'myself' a redirection must be performed. The kind of\n * redirection is specified setting the integer passed by reference\n * 'error_code', which will be set to CLUSTER_REDIR_ASK or\n * CLUSTER_REDIR_MOVED.\n *\n * When the node is 'myself' 'error_code' is set to CLUSTER_REDIR_NONE.\n *\n * If the command fails NULL is returned, and the reason of the failure is\n * provided via 'error_code', which will be set to:\n *\n * CLUSTER_REDIR_CROSS_SLOT if the request contains multiple keys that\n * don't belong to the same hash slot.\n *\n * CLUSTER_REDIR_UNSTABLE if the request contains multiple keys\n * belonging to the same slot, but the slot is not stable (in migration or\n * importing state, likely because a resharding is in progress).\n *\n * CLUSTER_REDIR_DOWN_UNBOUND if the request addresses a slot which is\n * not bound to any node. In this case the cluster global state should be\n * already \"down\" but it is fragile to rely on the update of the global state,\n * so we also handle it here.\n *\n * CLUSTER_REDIR_DOWN_STATE and CLUSTER_REDIR_DOWN_RO_STATE if the cluster is\n * down but the user attempts to execute a command that addresses one or more keys. */\nclusterNode *getNodeByQuery(client *c, struct redisCommand *cmd, robj **argv, int argc, int *hashslot, int *error_code) {\n    clusterNode *n = NULL;\n    robj *firstkey = NULL;\n    int multiple_keys = 0;\n    multiState *ms, _ms;\n    multiCmd mc;\n    int i, slot = 0, migrating_slot = 0, importing_slot = 0, missing_keys = 0;\n    serverAssert((c->cmd->flags & CMD_ASYNC_OK) || GlobalLocksAcquired());\n\n    /* Allow any key to be set if a module disabled cluster redirections. */\n    if (g_pserver->cluster_module_flags & CLUSTER_MODULE_FLAG_NO_REDIRECTION)\n        return myself;\n\n    /* Allow any key to be set if a module disabled cluster redirections. */\n    if (g_pserver->cluster_module_flags & CLUSTER_MODULE_FLAG_NO_REDIRECTION)\n        return myself;\n\n    /* Set error code optimistically for the base case. */\n    if (error_code) *error_code = CLUSTER_REDIR_NONE;\n\n    /* Modules can turn off Redis Cluster redirection: this is useful\n     * when writing a module that implements a completely different\n     * distributed system. */\n\n    /* We handle all the cases as if they were EXEC commands, so we have\n     * a common code path for everything */\n    if (cmd->proc == execCommand) {\n        /* If CLIENT_MULTI flag is not set EXEC is just going to return an\n         * error. */\n        if (!(c->flags & CLIENT_MULTI)) return myself;\n        ms = &c->mstate;\n    } else {\n        /* In order to have a single codepath create a fake Multi State\n         * structure if the client is not in MULTI/EXEC state, this way\n         * we have a single codepath below. */\n        ms = &_ms;\n        _ms.commands = &mc;\n        _ms.count = 1;\n        mc.argv = argv;\n        mc.argc = argc;\n        mc.cmd = cmd;\n    }\n\n    /* Check that all the keys are in the same hash slot, and obtain this\n     * slot and the node associated. */\n    for (i = 0; i < ms->count; i++) {\n        struct redisCommand *mcmd;\n        robj **margv;\n        int margc, *keyindex, numkeys, j;\n\n        mcmd = ms->commands[i].cmd;\n        margc = ms->commands[i].argc;\n        margv = ms->commands[i].argv;\n\n        getKeysResult result = GETKEYS_RESULT_INIT;\n        numkeys = getKeysFromCommand(mcmd,margv,margc,&result);\n        keyindex = result.keys;\n\n        for (j = 0; j < numkeys; j++) {\n            robj *thiskey = margv[keyindex[j]];\n            int thisslot = keyHashSlot((char*)ptrFromObj(thiskey),\n                                       sdslen(szFromObj(thiskey)));\n\n            if (firstkey == NULL) {\n                /* This is the first key we see. Check what is the slot\n                 * and node. */\n                firstkey = thiskey;\n                slot = thisslot;\n                n = g_pserver->cluster->slots[slot];\n\n                /* Error: If a slot is not served, we are in \"cluster down\"\n                 * state. However the state is yet to be updated, so this was\n                 * not trapped earlier in processCommand(). Report the same\n                 * error to the client. */\n                if (n == NULL) {\n                    getKeysFreeResult(&result);\n                    if (error_code)\n                        *error_code = CLUSTER_REDIR_DOWN_UNBOUND;\n                    return NULL;\n                }\n\n                /* If we are migrating or importing this slot, we need to check\n                 * if we have all the keys in the request (the only way we\n                 * can safely serve the request, otherwise we return a TRYAGAIN\n                 * error). To do so we set the importing/migrating state and\n                 * increment a counter for every missing key. */\n                if (n == myself &&\n                    g_pserver->cluster->migrating_slots_to[slot] != NULL)\n                {\n                    migrating_slot = 1;\n                } else if (g_pserver->cluster->importing_slots_from[slot] != NULL) {\n                    importing_slot = 1;\n                }\n            } else {\n                /* If it is not the first key, make sure it is exactly\n                 * the same key as the first we saw. */\n                if (!equalStringObjects(firstkey,thiskey)) {\n                    clusterNode* nThisKey = g_pserver->cluster->slots[thisslot];\n\n                    if ((slot != thisslot) && (nThisKey != n || migrating_slot || importing_slot || g_pserver->cluster->migrating_slots_to[slot] != nullptr || g_pserver->cluster->importing_slots_from[slot] != nullptr)) {\n                        /* Error: multiple keys from different slots. */\n                        getKeysFreeResult(&result);\n                        if (error_code)\n                            *error_code = CLUSTER_REDIR_CROSS_SLOT;\n                        return NULL;\n                    } else {\n                        /* Flag this request as one with multiple different\n                         * keys. */\n                        multiple_keys = 1;\n                    }\n                }\n            }\n\n            /* Migrating / Importing slot? Count keys we don't have. */\n            if ((migrating_slot || importing_slot) &&\n                lookupKeyRead(g_pserver->db[0],thiskey) == nullptr)\n            {\n                missing_keys++;\n            }\n        }\n        getKeysFreeResult(&result);\n    }\n\n    /* No key at all in command? then we can serve the request\n     * without redirections or errors in all the cases. */\n    if (n == NULL) return myself;\n\n    /* Cluster is globally down but we got keys? We only serve the request\n     * if it is a read command and when allow_reads_when_down is enabled. */\n    if (g_pserver->cluster->state != CLUSTER_OK) {\n        if (!g_pserver->cluster_allow_reads_when_down) {\n            /* The cluster is configured to block commands when the\n             * cluster is down. */\n            if (error_code) *error_code = CLUSTER_REDIR_DOWN_STATE;\n            return NULL;\n        } else if (cmd->flags & CMD_WRITE) {\n            /* The cluster is configured to allow read only commands */\n            if (error_code) *error_code = CLUSTER_REDIR_DOWN_RO_STATE;\n            return NULL;\n        } else {\n            /* Fall through and allow the command to be executed:\n             * this happens when server.cluster_allow_reads_when_down is\n             * true and the command is not a write command */\n        }\n    }\n\n    /* Return the hashslot by reference. */\n    if (hashslot) *hashslot = slot;\n\n    /* MIGRATE always works in the context of the local node if the slot\n     * is open (migrating or importing state). We need to be able to freely\n     * move keys among instances in this case. */\n    if ((migrating_slot || importing_slot) && cmd->proc == migrateCommand)\n        return myself;\n\n    /* If we don't have all the keys and we are migrating the slot, send\n     * an ASK redirection. */\n    if (migrating_slot && missing_keys) {\n        if (error_code) *error_code = CLUSTER_REDIR_ASK;\n        return g_pserver->cluster->migrating_slots_to[slot];\n    }\n\n    /* If we are receiving the slot, and the client correctly flagged the\n     * request as \"ASKING\", we can serve the request. However if the request\n     * involves multiple keys and we don't have them all, the only option is\n     * to send a TRYAGAIN error. */\n    if (importing_slot &&\n        (c->flags & CLIENT_ASKING || cmd->flags & CMD_ASKING))\n    {\n        if (multiple_keys && missing_keys) {\n            if (error_code) *error_code = CLUSTER_REDIR_UNSTABLE;\n            return NULL;\n        } else {\n            return myself;\n        }\n    }\n\n    /* Handle the read-only client case reading from a slave: if this\n     * node is a slave and the request is about a hash slot our master\n     * is serving, we can reply without redirection. */\n    int is_write_command = (c->cmd->flags & CMD_WRITE) ||\n                           (c->cmd->proc == execCommand && (c->mstate.cmd_flags & CMD_WRITE));\n    if (c->flags & CLIENT_READONLY &&\n        !is_write_command &&\n        nodeIsSlave(myself) &&\n        myself->slaveof == n)\n    {\n        return myself;\n    }\n\n    /* Base case: just return the right node. However if this node is not\n     * myself, set error_code to MOVED since we need to issue a redirection. */\n    if (n != myself && error_code) *error_code = CLUSTER_REDIR_MOVED;\n    return n;\n}\n\n/* Send the client the right redirection code, according to error_code\n * that should be set to one of CLUSTER_REDIR_* macros.\n *\n * If CLUSTER_REDIR_ASK or CLUSTER_REDIR_MOVED error codes\n * are used, then the node 'n' should not be NULL, but should be the\n * node we want to mention in the redirection. Moreover hashslot should\n * be set to the hash slot that caused the redirection. */\nvoid clusterRedirectClient(client *c, clusterNode *n, int hashslot, int error_code) {\n    if (error_code == CLUSTER_REDIR_CROSS_SLOT) {\n        addReplyError(c,\"-CROSSSLOT Keys in request don't hash to the same slot\");\n    } else if (error_code == CLUSTER_REDIR_UNSTABLE) {\n        /* The request spawns multiple keys in the same slot,\n         * but the slot is not \"stable\" currently as there is\n         * a migration or import in progress. */\n        addReplyError(c,\"-TRYAGAIN Multiple keys request during rehashing of slot\");\n    } else if (error_code == CLUSTER_REDIR_DOWN_STATE) {\n        addReplyError(c,\"-CLUSTERDOWN The cluster is down\");\n    } else if (error_code == CLUSTER_REDIR_DOWN_RO_STATE) {\n        addReplyError(c,\"-CLUSTERDOWN The cluster is down and only accepts read commands\");\n    } else if (error_code == CLUSTER_REDIR_DOWN_UNBOUND) {\n        addReplyError(c,\"-CLUSTERDOWN Hash slot not served\");\n    } else if (error_code == CLUSTER_REDIR_MOVED ||\n               error_code == CLUSTER_REDIR_ASK)\n    {\n        /* Redirect to IP:port. Include plaintext port if cluster is TLS but\n         * client is non-TLS. */\n        int use_pport = (g_pserver->tls_cluster &&\n                         c->conn && connGetType(c->conn) != CONN_TYPE_TLS);\n        int port = use_pport && n->pport ? n->pport : n->port;\n        addReplyErrorSds(c,sdscatprintf(sdsempty(),\n            \"-%s %d %s:%d\",\n            (error_code == CLUSTER_REDIR_ASK) ? \"ASK\" : \"MOVED\",\n            hashslot, n->ip, port));\n    } else {\n        serverPanic(\"getNodeByQuery() unknown error.\");\n    }\n}\n\n/* This function is called by the function processing clients incrementally\n * to detect timeouts, in order to handle the following case:\n *\n * 1) A client blocks with BLPOP or similar blocking operation.\n * 2) The master migrates the hash slot elsewhere or turns into a slave.\n * 3) The client may remain blocked forever (or up to the max timeout time)\n *    waiting for a key change that will never happen.\n *\n * If the client is found to be blocked into a hash slot this node no\n * longer handles, the client is sent a redirection error, and the function\n * returns 1. Otherwise 0 is returned and no operation is performed. */\nint clusterRedirectBlockedClientIfNeeded(client *c) {\n    serverAssert(GlobalLocksAcquired());\n    if (c->flags & CLIENT_BLOCKED &&\n        (c->btype == BLOCKED_LIST ||\n         c->btype == BLOCKED_ZSET ||\n         c->btype == BLOCKED_STREAM))\n    {\n        dictEntry *de;\n        dictIterator *di;\n\n        /* If the cluster is down, unblock the client with the right error.\n         * If the cluster is configured to allow reads on cluster down, we\n         * still want to emit this error since a write will be required\n         * to unblock them which may never come.  */\n        if (g_pserver->cluster->state == CLUSTER_FAIL) {\n            clusterRedirectClient(c,NULL,0,CLUSTER_REDIR_DOWN_STATE);\n            return 1;\n        }\n\n        /* All keys must belong to the same slot, so check first key only. */\n        di = dictGetIterator(c->bpop.keys);\n        if ((de = dictNext(di)) != NULL) {\n            robj *key = (robj*)dictGetKey(de);\n            int slot = keyHashSlot((char*)ptrFromObj(key), sdslen(szFromObj(key)));\n            clusterNode *node = g_pserver->cluster->slots[slot];\n\n            /* if the client is read-only and attempting to access key that our\n             * replica can handle, allow it. */\n            if ((c->flags & CLIENT_READONLY) &&\n                (c->lastcmd->flags & CMD_READONLY) &&\n                nodeIsSlave(myself) && myself->slaveof == node)\n            {\n                node = myself;\n            }\n\n            /* if the client is read-only and attempting to access key that our\n             * replica can handle, allow it. */\n            if ((c->flags & CLIENT_READONLY) &&\n                !(c->lastcmd->flags & CMD_WRITE) &&\n                nodeIsSlave(myself) && myself->slaveof == node)\n            {\n                node = myself;\n            }\n\n            /* We send an error and unblock the client if:\n             * 1) The slot is unassigned, emitting a cluster down error.\n             * 2) The slot is not handled by this node, nor being imported. */\n            if (node != myself &&\n                g_pserver->cluster->importing_slots_from[slot] == NULL)\n            {\n                if (node == NULL) {\n                    clusterRedirectClient(c,NULL,0,\n                        CLUSTER_REDIR_DOWN_UNBOUND);\n                } else {\n                    clusterRedirectClient(c,node,slot,\n                        CLUSTER_REDIR_MOVED);\n                }\n                dictReleaseIterator(di);\n                return 1;\n            }\n        }\n        dictReleaseIterator(di);\n    }\n    return 0;\n}\n"
  },
  {
    "path": "src/cluster.h",
    "content": "#ifndef __CLUSTER_H\n#define __CLUSTER_H\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/*-----------------------------------------------------------------------------\n * Redis cluster data structures, defines, exported API.\n *----------------------------------------------------------------------------*/\n\n#define CLUSTER_SLOTS 16384\n#define CLUSTER_OK 0          /* Everything looks ok */\n#define CLUSTER_FAIL 1        /* The cluster can't work */\n#define CLUSTER_NAMELEN 40    /* sha1 hex length */\n#define CLUSTER_PORT_INCR 10000 /* Cluster port = baseport + PORT_INCR */\n\n/* The following defines are amount of time, sometimes expressed as\n * multiplicators of the node timeout value (when ending with MULT). */\n#define CLUSTER_FAIL_REPORT_VALIDITY_MULT 2 /* Fail report validity. */\n#define CLUSTER_FAIL_UNDO_TIME_MULT 2 /* Undo fail if master is back. */\n#define CLUSTER_FAIL_UNDO_TIME_ADD 10 /* Some additional time. */\n#define CLUSTER_FAILOVER_DELAY 5 /* Seconds */\n#define CLUSTER_MF_TIMEOUT 5000 /* Milliseconds to do a manual failover. */\n#define CLUSTER_MF_PAUSE_MULT 2 /* Master pause manual failover mult. */\n#define CLUSTER_SLAVE_MIGRATION_DELAY 5000 /* Delay for slave migration. */\n\n/* Redirection errors returned by getNodeByQuery(). */\n#define CLUSTER_REDIR_NONE 0          /* Node can serve the request. */\n#define CLUSTER_REDIR_CROSS_SLOT 1    /* -CROSSSLOT request. */\n#define CLUSTER_REDIR_UNSTABLE 2      /* -TRYAGAIN redirection required */\n#define CLUSTER_REDIR_ASK 3           /* -ASK redirection required. */\n#define CLUSTER_REDIR_MOVED 4         /* -MOVED redirection required. */\n#define CLUSTER_REDIR_DOWN_STATE 5    /* -CLUSTERDOWN, global state. */\n#define CLUSTER_REDIR_DOWN_UNBOUND 6  /* -CLUSTERDOWN, unbound slot. */\n#define CLUSTER_REDIR_DOWN_RO_STATE 7 /* -CLUSTERDOWN, allow reads. */\n\nstruct clusterNode;\n\n/* clusterLink encapsulates everything needed to talk with a remote node. */\ntypedef struct clusterLink {\n    mstime_t ctime;             /* Link creation time */\n    connection *conn;           /* Connection to remote node */\n    sds sndbuf;                 /* Packet send buffer */\n    char *rcvbuf;               /* Packet reception buffer */\n    size_t rcvbuf_len;          /* Used size of rcvbuf */\n    size_t rcvbuf_alloc;        /* Used size of rcvbuf */\n    struct clusterNode *node;   /* Node related to this link if any, or NULL */\n} clusterLink;\n\n/* Cluster node flags and macros. */\n#define CLUSTER_NODE_MASTER 1     /* The node is a master */\n#define CLUSTER_NODE_SLAVE 2      /* The node is a slave */\n#define CLUSTER_NODE_PFAIL 4      /* Failure? Need acknowledge */\n#define CLUSTER_NODE_FAIL 8       /* The node is believed to be malfunctioning */\n#define CLUSTER_NODE_MYSELF 16    /* This node is myself */\n#define CLUSTER_NODE_HANDSHAKE 32 /* We have still to exchange the first ping */\n#define CLUSTER_NODE_NOADDR   64  /* We don't know the address of this node */\n#define CLUSTER_NODE_MEET 128     /* Send a MEET message to this node */\n#define CLUSTER_NODE_MIGRATE_TO 256 /* Master eligible for replica migration. */\n#define CLUSTER_NODE_NOFAILOVER 512 /* Slave will not try to failover. */\n#define CLUSTER_NODE_NULL_NAME \"\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\"\n\n#define nodeIsMaster(n) ((n)->flags & CLUSTER_NODE_MASTER)\n#define nodeIsSlave(n) ((n)->flags & CLUSTER_NODE_SLAVE)\n#define nodeInHandshake(n) ((n)->flags & CLUSTER_NODE_HANDSHAKE)\n#define nodeHasAddr(n) (!((n)->flags & CLUSTER_NODE_NOADDR))\n#define nodeWithoutAddr(n) ((n)->flags & CLUSTER_NODE_NOADDR)\n#define nodeTimedOut(n) ((n)->flags & CLUSTER_NODE_PFAIL)\n#define nodeFailed(n) ((n)->flags & CLUSTER_NODE_FAIL)\n#define nodeCantFailover(n) ((n)->flags & CLUSTER_NODE_NOFAILOVER)\n\n/* Reasons why a slave is not able to failover. */\n#define CLUSTER_CANT_FAILOVER_NONE 0\n#define CLUSTER_CANT_FAILOVER_DATA_AGE 1\n#define CLUSTER_CANT_FAILOVER_WAITING_DELAY 2\n#define CLUSTER_CANT_FAILOVER_EXPIRED 3\n#define CLUSTER_CANT_FAILOVER_WAITING_VOTES 4\n#define CLUSTER_CANT_FAILOVER_RELOG_PERIOD (60*5) /* seconds. */\n\n/* clusterState todo_before_sleep flags. */\n#define CLUSTER_TODO_HANDLE_FAILOVER (1<<0)\n#define CLUSTER_TODO_UPDATE_STATE (1<<1)\n#define CLUSTER_TODO_SAVE_CONFIG (1<<2)\n#define CLUSTER_TODO_FSYNC_CONFIG (1<<3)\n#define CLUSTER_TODO_HANDLE_MANUALFAILOVER (1<<4)\n\n/* Message types.\n *\n * Note that the PING, PONG and MEET messages are actually the same exact\n * kind of packet. PONG is the reply to ping, in the exact format as a PING,\n * while MEET is a special PING that forces the receiver to add the sender\n * as a node (if it is not already in the list). */\n#define CLUSTERMSG_TYPE_PING 0          /* Ping */\n#define CLUSTERMSG_TYPE_PONG 1          /* Pong (reply to Ping) */\n#define CLUSTERMSG_TYPE_MEET 2          /* Meet \"let's join\" message */\n#define CLUSTERMSG_TYPE_FAIL 3          /* Mark node xxx as failing */\n#define CLUSTERMSG_TYPE_PUBLISH 4       /* Pub/Sub Publish propagation */\n#define CLUSTERMSG_TYPE_FAILOVER_AUTH_REQUEST 5 /* May I failover? */\n#define CLUSTERMSG_TYPE_FAILOVER_AUTH_ACK 6     /* Yes, you have my vote */\n#define CLUSTERMSG_TYPE_UPDATE 7        /* Another node slots configuration */\n#define CLUSTERMSG_TYPE_MFSTART 8       /* Pause clients for manual failover */\n#define CLUSTERMSG_TYPE_MODULE 9        /* Module cluster API message. */\n#define CLUSTERMSG_TYPE_COUNT 10        /* Total number of message types. */\n\n/* Flags that a module can set in order to prevent certain Redis Cluster\n * features to be enabled. Useful when implementing a different distributed\n * system on top of Redis Cluster message bus, using modules. */\n#define CLUSTER_MODULE_FLAG_NONE 0\n#define CLUSTER_MODULE_FLAG_NO_FAILOVER (1<<1)\n#define CLUSTER_MODULE_FLAG_NO_REDIRECTION (1<<2)\n\n/* This structure represent elements of node->fail_reports. */\ntypedef struct clusterNodeFailReport {\n    struct clusterNode *node;  /* Node reporting the failure condition. */\n    mstime_t time;             /* Time of the last report from this node. */\n} clusterNodeFailReport;\n\ntypedef struct clusterNode {\n    mstime_t ctime; /* Node object creation time. */\n    char name[CLUSTER_NAMELEN]; /* Node name, hex string, sha1-size */\n    int flags;      /* CLUSTER_NODE_... */\n    uint64_t configEpoch; /* Last configEpoch observed for this node */\n    unsigned char slots[CLUSTER_SLOTS/8]; /* slots handled by this node */\n    sds slots_info; /* Slots info represented by string. */\n    int numslots;   /* Number of slots handled by this node */\n    int numslaves;  /* Number of slave nodes, if this is a master */\n    struct clusterNode **slaves; /* pointers to slave nodes */\n    struct clusterNode *slaveof; /* pointer to the master node. Note that it\n                                    may be NULL even if the node is a slave\n                                    if we don't have the master node in our\n                                    tables. */\n    mstime_t ping_sent;      /* Unix time we sent latest ping */\n    mstime_t pong_received;  /* Unix time we received the pong */\n    mstime_t data_received;  /* Unix time we received any data */\n    mstime_t fail_time;      /* Unix time when FAIL flag was set */\n    mstime_t voted_time;     /* Last time we voted for a slave of this master */\n    mstime_t repl_offset_time;  /* Unix time we received offset for this node */\n    mstime_t orphaned_time;     /* Starting time of orphaned master condition */\n    long long repl_offset;      /* Last known repl offset for this node. */\n    char ip[NET_IP_STR_LEN];  /* Latest known IP address of this node */\n    int port;                   /* Latest known clients port (TLS or plain). */\n    int pport;                  /* Latest known clients plaintext port. Only used\n                                   if the main clients port is for TLS. */\n    int cport;                  /* Latest known cluster port of this node. */\n    clusterLink *link;          /* TCP/IP link with this node */\n    list *fail_reports;         /* List of nodes signaling this as failing */\n} clusterNode;\n\ntypedef struct clusterState {\n    clusterNode *myself;  /* This node */\n    uint64_t currentEpoch;\n    int state;            /* CLUSTER_OK, CLUSTER_FAIL, ... */\n    int size;             /* Num of master nodes with at least one slot */\n    dict *nodes;          /* Hash table of name -> clusterNode structures */\n    dict *nodes_black_list; /* Nodes we don't re-add for a few seconds. */\n    clusterNode *migrating_slots_to[CLUSTER_SLOTS];\n    clusterNode *importing_slots_from[CLUSTER_SLOTS];\n    clusterNode *slots[CLUSTER_SLOTS];\n    uint64_t slots_keys_count[CLUSTER_SLOTS];\n    rax *slots_to_keys;\n    /* The following fields are used to take the slave state on elections. */\n    mstime_t failover_auth_time; /* Time of previous or next election. */\n    int failover_auth_count;    /* Number of votes received so far. */\n    int failover_auth_sent;     /* True if we already asked for votes. */\n    int failover_auth_rank;     /* This slave rank for current auth request. */\n    uint64_t failover_auth_epoch; /* Epoch of the current election. */\n    int cant_failover_reason;   /* Why a slave is currently not able to\n                                   failover. See the CANT_FAILOVER_* macros. */\n    /* Manual failover state in common. */\n    mstime_t mf_end;            /* Manual failover time limit (ms unixtime).\n                                   It is zero if there is no MF in progress. */\n    /* Manual failover state of master. */\n    clusterNode *mf_slave;      /* Slave performing the manual failover. */\n    /* Manual failover state of slave. */\n    long long mf_master_offset; /* Master offset the slave needs to start MF\n                                   or -1 if still not received. */\n    int mf_can_start;           /* If non-zero signal that the manual failover\n                                   can start requesting masters vote. */\n    /* The following fields are used by masters to take state on elections. */\n    uint64_t lastVoteEpoch;     /* Epoch of the last vote granted. */\n    int todo_before_sleep; /* Things to do in clusterBeforeSleep(). */\n    /* Messages received and sent by type. */\n    long long stats_bus_messages_sent[CLUSTERMSG_TYPE_COUNT];\n    long long stats_bus_messages_received[CLUSTERMSG_TYPE_COUNT];\n    long long stats_pfail_nodes;    /* Number of nodes in PFAIL status,\n                                       excluding nodes without address. */\n} clusterState;\n\n/* Redis cluster messages header */\n\n/* Initially we don't know our \"name\", but we'll find it once we connect\n * to the first node, using the getsockname() function. Then we'll use this\n * address for all the next messages. */\ntypedef struct {\n    char nodename[CLUSTER_NAMELEN];\n    uint32_t ping_sent;\n    uint32_t pong_received;\n    char ip[NET_IP_STR_LEN];  /* IP address last time it was seen */\n    uint16_t port;              /* base port last time it was seen */\n    uint16_t cport;             /* cluster port last time it was seen */\n    uint16_t flags;             /* node->flags copy */\n    uint16_t pport;             /* plaintext-port, when base port is TLS */\n    uint16_t notused1;\n} clusterMsgDataGossip;\n\ntypedef struct {\n    char nodename[CLUSTER_NAMELEN];\n} clusterMsgDataFail;\n\ntypedef struct {\n    uint32_t channel_len;\n    uint32_t message_len;\n    unsigned char bulk_data[8]; /* 8 bytes just as placeholder. */\n} clusterMsgDataPublish;\n\ntypedef struct {\n    uint64_t configEpoch; /* Config epoch of the specified instance. */\n    char nodename[CLUSTER_NAMELEN]; /* Name of the slots owner. */\n    unsigned char slots[CLUSTER_SLOTS/8]; /* Slots bitmap. */\n} clusterMsgDataUpdate;\n\ntypedef struct {\n    uint64_t module_id;     /* ID of the sender module. */\n    uint32_t len;           /* ID of the sender module. */\n    uint8_t type;           /* Type from 0 to 255. */\n    unsigned char bulk_data[3]; /* 3 bytes just as placeholder. */\n} clusterMsgModule;\n\nunion clusterMsgData {\n    /* PING, MEET and PONG */\n    struct {\n        /* Array of N clusterMsgDataGossip structures */\n        clusterMsgDataGossip gossip[1];\n    } ping;\n\n    /* FAIL */\n    struct {\n        clusterMsgDataFail about;\n    } fail;\n\n    /* PUBLISH */\n    struct {\n        clusterMsgDataPublish msg;\n    } publish;\n\n    /* UPDATE */\n    struct {\n        clusterMsgDataUpdate nodecfg;\n    } update;\n\n    /* MODULE */\n    struct {\n        clusterMsgModule msg;\n    } module;\n};\n\n#define CLUSTER_PROTO_VER 1 /* Cluster bus protocol version. */\n\ntypedef struct {\n    char sig[4];        /* Signature \"RCmb\" (Redis Cluster message bus). */\n    uint32_t totlen;    /* Total length of this message */\n    uint16_t ver;       /* Protocol version, currently set to 1. */\n    uint16_t port;      /* TCP base port number. */\n    uint16_t type;      /* Message type */\n    uint16_t count;     /* Only used for some kind of messages. */\n    uint64_t currentEpoch;  /* The epoch accordingly to the sending node. */\n    uint64_t configEpoch;   /* The config epoch if it's a master, or the last\n                               epoch advertised by its master if it is a\n                               slave. */\n    uint64_t offset;    /* Master replication offset if node is a master or\n                           processed replication offset if node is a slave. */\n    char sender[CLUSTER_NAMELEN]; /* Name of the sender node */\n    unsigned char myslots[CLUSTER_SLOTS/8];\n    char slaveof[CLUSTER_NAMELEN];\n    char myip[NET_IP_STR_LEN];    /* Sender IP, if not all zeroed. */\n    char notused1[32];  /* 32 bytes reserved for future usage. */\n    uint16_t pport;      /* Sender TCP plaintext port, if base port is TLS */\n    uint16_t cport;      /* Sender TCP cluster bus port */\n    uint16_t flags;      /* Sender node flags */\n    unsigned char state; /* Cluster state from the POV of the sender */\n    unsigned char mflags[3]; /* Message flags: CLUSTERMSG_FLAG[012]_... */\n    union clusterMsgData data;\n} clusterMsg;\n\n#define CLUSTERMSG_MIN_LEN (sizeof(clusterMsg)-sizeof(union clusterMsgData))\n\n/* Message flags better specify the packet content or are used to\n * provide some information about the node state. */\n#define CLUSTERMSG_FLAG0_PAUSED (1<<0) /* Master paused for manual failover. */\n#define CLUSTERMSG_FLAG0_FORCEACK (1<<1) /* Give ACK to AUTH_REQUEST even if\n                                            master is up. */\n\n/* ---------------------- API exported outside cluster.c -------------------- */\nclusterNode *getNodeByQuery(client *c, struct redisCommand *cmd, robj **argv, int argc, int *hashslot, int *ask);\nint clusterRedirectBlockedClientIfNeeded(client *c);\nvoid clusterRedirectClient(client *c, clusterNode *n, int hashslot, int error_code);\nunsigned long getClusterConnectionsCount(void);\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif /* __CLUSTER_H */\n"
  },
  {
    "path": "src/compactvector.h",
    "content": "#pragma once\n\n#include <type_traits>\n#include <assert.h>\n\n/*************************************************\n * compactvector - similar to std::vector but optimized for minimal memory\n *  \n *  Notable differences:\n *      - Limited to 2^32 elements\n *      - Grows linearly not exponentially\n * \n *************************************************/\n\ntemplate<typename T, bool MEMMOVE_SAFE=false>\nclass compactvector\n{\n    static_assert(MEMMOVE_SAFE || std::is_trivially_copyable<T>::value, \"compactvector requires trivially copyable types\");\n    T *m_data = nullptr;\n    unsigned m_celem = 0;\n    unsigned m_max = 0;\n\npublic:\n    typedef T* iterator;\n\n    compactvector() noexcept = default;\n    ~compactvector() noexcept\n    {\n        clear();    // call dtors\n        zfree(m_data);\n    }\n\n    compactvector(const compactvector &src)\n    {\n        m_celem = src.m_celem;\n        m_max = src.m_max;\n        m_data = (T*)zmalloc(sizeof(T) * m_max, MALLOC_LOCAL);\n        for (size_t ielem = 0; ielem < m_celem; ++ielem)\n        {\n            new (m_data+ielem) T(src[ielem]);\n        }\n    }\n\n    compactvector(compactvector &&src) noexcept\n    {\n        m_data = src.m_data;\n        m_celem = src.m_celem;\n        m_max = src.m_max;\n        src.m_data = nullptr;\n        src.m_celem = 0;\n        src.m_max = 0;\n    }\n\n    compactvector &operator=(compactvector &&src) noexcept\n    {\n        zfree(m_data);\n        m_data = src.m_data;\n        m_celem = src.m_celem;\n        m_max = src.m_max;\n        src.m_data = nullptr;\n        src.m_celem = 0;\n        src.m_max = 0;\n        return *this;\n    }\n\n    inline T* begin() { return m_data; }\n    inline const T* begin() const { return m_data; }\n\n    inline T* end() { return m_data + m_celem; }\n    inline const T* end() const { return m_data + m_celem; }\n\n    T* insert(T* where, const T &val)\n    {\n        assert(where >= m_data);\n        size_t idx = where - m_data;\n        if (m_celem >= m_max)\n        {\n            if (m_max < 2)\n                m_max = 2;\n            else\n                m_max = m_max + 4;\n            \n            m_data = (T*)zrealloc(m_data, sizeof(T) * m_max, MALLOC_LOCAL);\n            m_max = zmalloc_usable_size(m_data) / sizeof(T);\n        }\n        assert(idx < m_max);\n        where = m_data + idx;\n        memmove(reinterpret_cast<void*>(m_data + idx + 1), reinterpret_cast<const void*>(m_data + idx), (m_celem - idx)*sizeof(T));\n        new(m_data + idx) T(std::move(val));\n        ++m_celem;\n        return where;\n    }\n\n    T &operator[](size_t idx)\n    {\n        assert(idx < m_celem);\n        return m_data[idx];\n    }\n    const T &operator[](size_t idx) const\n    {\n        assert(idx < m_celem);\n        return m_data[idx];\n    }\n\n    T& back() { assert(m_celem > 0); return m_data[m_celem-1]; }\n    const T& back() const { assert(m_celem > 0); return m_data[m_celem-1]; }\n\n    void erase(T* where)\n    {\n        assert(where >= m_data);\n        size_t idx = where - m_data;\n        assert(idx < m_celem);\n        where->~T();\n        memmove(reinterpret_cast<void*>(where), reinterpret_cast<const void*>(where+1), ((m_celem - idx - 1)*sizeof(T)));\n        --m_celem;\n\n        if (m_celem == 0)\n        {\n            zfree(m_data);\n            m_data = nullptr;\n            m_max = 0;\n        }\n    }\n\n    void shrink_to_fit()\n    {\n        if (m_max == m_celem)\n            return;\n        m_data = (T*)zrealloc(m_data, sizeof(T) * m_celem, MALLOC_LOCAL);\n        m_max = m_celem;    // NOTE: We do not get the usable size here, because this could cause us to continually realloc\n    }\n\n    size_t bytes_used() const\n    {\n        return sizeof(this) + (m_max * sizeof(T));\n    }\n\n    void clear()\n    {\n        for (size_t idx = 0; idx < m_celem; ++idx)\n            m_data[idx].~T();\n        zfree(m_data);\n        m_data = nullptr;\n        m_celem = 0;\n        m_max = 0;\n    }\n\n    bool empty() const noexcept\n    {\n        return m_celem == 0;\n    }\n\n    size_t size() const noexcept\n    {\n        return m_celem;\n    }\n\n    T* data() noexcept { return m_data; }\n    const T* data() const noexcept { return m_data; }\n};\nstatic_assert(sizeof(compactvector<void*>) <= 16, \"not compact\");\n"
  },
  {
    "path": "src/config.cpp",
    "content": "/* Configuration file parsing and CONFIG GET/SET commands implementation.\n *\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n#include \"storage/rocksdbfactory.h\"\n#include \"storage/teststorageprovider.h\"\n#include \"cluster.h\"\n\n#include <fcntl.h>\n#include <sys/stat.h>\n#include <sys/wait.h>\n#ifdef __linux__\n#include <sys/sysinfo.h>\n#endif\n\nconst char *KEYDB_SET_VERSION = KEYDB_REAL_VERSION;\nsize_t g_semiOrderedSetTargetBucketSize = 0;    // Its a header only class so nowhere else for this to go\n\n/*-----------------------------------------------------------------------------\n * Config file name-value maps.\n *----------------------------------------------------------------------------*/\n\ntypedef struct configEnum {\n    const char *name;\n    const int val;\n} configEnum;\n\nconfigEnum maxmemory_policy_enum[] = {\n    {\"volatile-lru\", MAXMEMORY_VOLATILE_LRU},\n    {\"volatile-lfu\", MAXMEMORY_VOLATILE_LFU},\n    {\"volatile-random\",MAXMEMORY_VOLATILE_RANDOM},\n    {\"volatile-ttl\",MAXMEMORY_VOLATILE_TTL},\n    {\"allkeys-lru\",MAXMEMORY_ALLKEYS_LRU},\n    {\"allkeys-lfu\",MAXMEMORY_ALLKEYS_LFU},\n    {\"allkeys-random\",MAXMEMORY_ALLKEYS_RANDOM},\n    {\"noeviction\",MAXMEMORY_NO_EVICTION},\n    {NULL, 0}\n};\n\nconfigEnum syslog_facility_enum[] = {\n    {\"user\",    LOG_USER},\n    {\"local0\",  LOG_LOCAL0},\n    {\"local1\",  LOG_LOCAL1},\n    {\"local2\",  LOG_LOCAL2},\n    {\"local3\",  LOG_LOCAL3},\n    {\"local4\",  LOG_LOCAL4},\n    {\"local5\",  LOG_LOCAL5},\n    {\"local6\",  LOG_LOCAL6},\n    {\"local7\",  LOG_LOCAL7},\n    {NULL, 0}\n};\n\nconfigEnum loglevel_enum[] = {\n    {\"debug\", LL_DEBUG},\n    {\"verbose\", LL_VERBOSE},\n    {\"notice\", LL_NOTICE},\n    {\"warning\", LL_WARNING},\n    {NULL,0}\n};\n\nconfigEnum supervised_mode_enum[] = {\n    {\"upstart\", SUPERVISED_UPSTART},\n    {\"systemd\", SUPERVISED_SYSTEMD},\n    {\"auto\", SUPERVISED_AUTODETECT},\n    {\"no\", SUPERVISED_NONE},\n    {NULL, 0}\n};\n\nconfigEnum aof_fsync_enum[] = {\n    {\"everysec\", AOF_FSYNC_EVERYSEC},\n    {\"always\", AOF_FSYNC_ALWAYS},\n    {\"no\", AOF_FSYNC_NO},\n    {NULL, 0}\n};\n\nconfigEnum repl_diskless_load_enum[] = {\n    {\"disabled\", REPL_DISKLESS_LOAD_DISABLED},\n    {\"on-empty-db\", REPL_DISKLESS_LOAD_WHEN_DB_EMPTY},\n    {\"swapdb\", REPL_DISKLESS_LOAD_SWAPDB},\n    {NULL, 0}\n};\n\nconfigEnum storage_memory_model_enum[] = {\n    {\"writeback\", STORAGE_WRITEBACK},\n    {\"writethrough\", STORAGE_WRITETHROUGH},\n};\n\nconfigEnum tls_auth_clients_enum[] = {\n    {\"no\", TLS_CLIENT_AUTH_NO},\n    {\"yes\", TLS_CLIENT_AUTH_YES},\n    {\"optional\", TLS_CLIENT_AUTH_OPTIONAL},\n    {NULL, 0}\n};\n\nconfigEnum oom_score_adj_enum[] = {\n    {\"no\", OOM_SCORE_ADJ_NO},\n    {\"yes\", OOM_SCORE_RELATIVE},\n    {\"relative\", OOM_SCORE_RELATIVE},\n    {\"absolute\", OOM_SCORE_ADJ_ABSOLUTE},\n    {NULL, 0}\n};\n\nconfigEnum acl_pubsub_default_enum[] = {\n    {\"allchannels\", USER_FLAG_ALLCHANNELS},\n    {\"resetchannels\", 0},\n    {NULL, 0}\n};\n\nconfigEnum sanitize_dump_payload_enum[] = {\n    {\"no\", SANITIZE_DUMP_NO},\n    {\"yes\", SANITIZE_DUMP_YES},\n    {\"clients\", SANITIZE_DUMP_CLIENTS},\n    {NULL, 0}\n};\n\n/* Output buffer limits presets. */\nclientBufferLimitsConfig clientBufferLimitsDefaults[CLIENT_TYPE_OBUF_COUNT] = {\n    {0, 0, 0}, /* normal */\n    {1024*1024*256, 1024*1024*64, 60}, /* replica */\n    {1024*1024*32, 1024*1024*8, 60}  /* pubsub */\n};\n\n/* OOM Score defaults */\nint configOOMScoreAdjValuesDefaults[CONFIG_OOM_COUNT] = { 0, 200, 800 };\n\n/* Generic config infrastructure function pointers\n * int is_valid_fn(val, err)\n *     Return 1 when val is valid, and 0 when invalid.\n *     Optionally set err to a static error string.\n * int update_fn(val, prev, err)\n *     This function is called only for CONFIG SET command (not at config file parsing)\n *     It is called after the actual config is applied,\n *     Return 1 for success, and 0 for failure.\n *     Optionally set err to a static error string.\n *     On failure the config change will be reverted.\n */\n\n/* Configuration values that require no special handling to set, get, load or\n * rewrite. */\ntypedef struct boolConfigData {\n    int *config; /* The pointer to the server config this value is stored in */\n    int default_value; /* The default value of the config on rewrite */\n    int (*is_valid_fn)(int val, const char **err); /* Optional function to check validity of new value (generic doc above) */\n    int (*update_fn)(int val, int prev, const char **err); /* Optional function to apply new value at runtime (generic doc above) */\n} boolConfigData;\n\ntypedef struct stringConfigData {\n    char **config; /* Pointer to the server config this value is stored in. */\n    const char *default_value; /* Default value of the config on rewrite. */\n    int (*is_valid_fn)(char* val, const char **err); /* Optional function to check validity of new value (generic doc above) */\n    int (*update_fn)(char* val, char* prev, const char **err); /* Optional function to apply new value at runtime (generic doc above) */\n    int convert_empty_to_null; /* Boolean indicating if empty strings should\n                                  be stored as a NULL value. */\n} stringConfigData;\n\ntypedef struct sdsConfigData {\n    sds *config; /* Pointer to the server config this value is stored in. */\n    const char *default_value; /* Default value of the config on rewrite. */\n    int (*is_valid_fn)(sds val, const char **err); /* Optional function to check validity of new value (generic doc above) */\n    int (*update_fn)(sds val, sds prev, const char **err); /* Optional function to apply new value at runtime (generic doc above) */\n    int convert_empty_to_null; /* Boolean indicating if empty SDS strings should\n                                  be stored as a NULL value. */\n} sdsConfigData;\n\ntypedef struct enumConfigData {\n    int *config; /* The pointer to the server config this value is stored in */\n    configEnum *enum_value; /* The underlying enum type this data represents */\n    int default_value; /* The default value of the config on rewrite */\n    int (*is_valid_fn)(int val, const char **err); /* Optional function to check validity of new value (generic doc above) */\n    int (*update_fn)(int val, int prev, const char **err); /* Optional function to apply new value at runtime (generic doc above) */\n} enumConfigData;\n\ntypedef enum numericType {\n    NUMERIC_TYPE_INT,\n    NUMERIC_TYPE_UINT,\n    NUMERIC_TYPE_LONG,\n    NUMERIC_TYPE_ULONG,\n    NUMERIC_TYPE_LONG_LONG,\n    NUMERIC_TYPE_ULONG_LONG,\n    NUMERIC_TYPE_SIZE_T,\n    NUMERIC_TYPE_SSIZE_T,\n    NUMERIC_TYPE_OFF_T,\n    NUMERIC_TYPE_TIME_T,\n} numericType;\n\ntypedef struct numericConfigData {\n    int is_memory; /* Indicates if this value can be loaded as a memory value */\n    long long lower_bound; /* The lower bound of this numeric value */\n    long long upper_bound; /* The upper bound of this numeric value */\n    long long default_value; /* The default value of the config on rewrite */\n    int (*is_valid_fn)(long long val, const char **err); /* Optional function to check validity of new value (generic doc above) */\n    int (*update_fn)(long long val, long long prev, const char **err); /* Optional function to apply new value at runtime (generic doc above) */\n    numericType numeric_type; /* An enum indicating the type of this value */\n    union {\n        int *i;\n        unsigned int *ui;\n        long *l;\n        unsigned long *ul;\n        long long *ll;\n        unsigned long long *ull;\n        size_t *st;\n        ssize_t *sst;\n        off_t *ot;\n        time_t *tt;\n    } config; /* The pointer to the numeric config this value is stored in */\n} numericConfigData;\n\ntypedef union typeData {\n    boolConfigData yesno;\n    stringConfigData string;\n    sdsConfigData sds;\n    enumConfigData enumd;\n    numericConfigData numeric;\n} typeData;\n\ntypedef struct typeInterface {\n    /* Called on server start, to init the server with default value */\n    void (*init)(typeData data);\n    /* Called on server startup and CONFIG SET, returns 1 on success, 0 on error\n     * and can set a verbose err string, update is true when called from CONFIG SET */\n    int (*set)(typeData data, sds value, int update, const char **err);\n    /* Called on CONFIG GET, required to add output to the client */\n    void (*get)(client *c, typeData data);\n    /* Called on CONFIG REWRITE, required to rewrite the config state */\n    void (*rewrite)(typeData data, const char *name, struct rewriteConfigState *state);\n} typeInterface;\n\ntypedef struct standardConfig {\n    const char *name; /* The user visible name of this config */\n    const char *alias; /* An alias that can also be used for this config */\n    const unsigned int flags; /* Flags for this specific config */\n    typeInterface interface; /* The function pointers that define the type interface */\n    typeData data; /* The type specific data exposed used by the interface */\n} standardConfig;\n\n#define MODIFIABLE_CONFIG 0 /* This is the implied default for a standard \n                             * config, which is mutable. */\n#define IMMUTABLE_CONFIG (1ULL<<0) /* Can this value only be set at startup? */\n#define SENSITIVE_CONFIG (1ULL<<1) /* Does this value contain sensitive information */\n\nextern standardConfig configs[];\n\n/*-----------------------------------------------------------------------------\n * Enum access functions\n *----------------------------------------------------------------------------*/\n\n/* Get enum value from name. If there is no match INT_MIN is returned. */\nint configEnumGetValue(configEnum *ce, char *name) {\n    while(ce->name != NULL) {\n        if (!strcasecmp(ce->name,name)) return ce->val;\n        ce++;\n    }\n    return INT_MIN;\n}\n\n/* Get enum name from value. If no match is found NULL is returned. */\nconst char *configEnumGetName(configEnum *ce, int val) {\n    while(ce->name != NULL) {\n        if (ce->val == val) return ce->name;\n        ce++;\n    }\n    return NULL;\n}\n\n/* Wrapper for configEnumGetName() returning \"unknown\" instead of NULL if\n * there is no match. */\nconst char *configEnumGetNameOrUnknown(configEnum *ce, int val) {\n    const char *name = configEnumGetName(ce,val);\n    return name ? name : \"unknown\";\n}\n\n/* Used for INFO generation. */\nconst char *evictPolicyToString(void) {\n    return configEnumGetNameOrUnknown(maxmemory_policy_enum,g_pserver->maxmemory_policy);\n}\n\n/*-----------------------------------------------------------------------------\n * Config file parsing\n *----------------------------------------------------------------------------*/\n\nint truefalsetoi(char *s) {\n    if (!strcasecmp(s,\"true\")) return 1;\n    else if (!strcasecmp(s,\"false\")) return 0;\n    else return -1;\n}\n\nint yesnotoi(char *s) {\n    if (!strcasecmp(s,\"yes\")) return 1;\n    else if (!strcasecmp(s,\"no\")) return 0;\n    else return truefalsetoi(s);\n}\n\n\nvoid appendServerSaveParams(time_t seconds, int changes) {\n    g_pserver->saveparams = (saveparam*)zrealloc(g_pserver->saveparams,sizeof(struct saveparam)*(g_pserver->saveparamslen+1), MALLOC_LOCAL);\n    g_pserver->saveparams[g_pserver->saveparamslen].seconds = seconds;\n    g_pserver->saveparams[g_pserver->saveparamslen].changes = changes;\n    g_pserver->saveparamslen++;\n}\n\nvoid resetServerSaveParams(void) {\n    zfree(g_pserver->saveparams);\n    g_pserver->saveparams = NULL;\n    g_pserver->saveparamslen = 0;\n}\n\nvoid queueLoadModule(sds path, sds *argv, int argc) {\n    int i;\n    struct moduleLoadQueueEntry *loadmod;\n\n    loadmod = (moduleLoadQueueEntry*)zmalloc(sizeof(struct moduleLoadQueueEntry), MALLOC_LOCAL);\n    loadmod->argv = (robj**)zmalloc(sizeof(robj*)*argc, MALLOC_LOCAL);\n    loadmod->path = sdsnew(path);\n    loadmod->argc = argc;\n    for (i = 0; i < argc; i++) {\n        loadmod->argv[i] = createRawStringObject(argv[i],sdslen(argv[i]));\n    }\n    listAddNodeTail(g_pserver->loadmodule_queue,loadmod);\n}\n\nsds g_sdsProvider = nullptr;\nsds g_sdsArgs = nullptr;\n\nbool initializeStorageProvider(const char **err)\n{\n    try\n    {\n        bool fTest = false;\n        if (g_sdsProvider == nullptr)\n            return true;\n        if (!strcasecmp(g_sdsProvider, \"flash\") && g_sdsArgs != nullptr)\n        {\n#ifdef ENABLE_ROCKSDB\n            // Create The Storage Factory (if necessary)\n            serverLog(LL_NOTICE, \"Initializing FLASH storage provider (this may take a long time)\");\n            adjustOpenFilesLimit();\n            g_pserver->m_pstorageFactory = CreateRocksDBStorageFactory(g_sdsArgs, cserver.dbnum, cserver.storage_conf, cserver.storage_conf ? strlen(cserver.storage_conf) : 0);\n#else\n            serverLog(LL_WARNING, \"To use the flash storage provider please compile KeyDB with ENABLE_FLASH=yes\");\n            serverLog(LL_WARNING, \"Exiting due to the use of an unsupported storage provider\");\n            exit(EXIT_FAILURE);\n#endif\n\t    }\n        else if (!strcasecmp(g_sdsProvider, \"test\") && g_sdsArgs == nullptr)\n        {\n            g_pserver->m_pstorageFactory = new (MALLOC_LOCAL) TestStorageFactory();\n            fTest = true;\n        }\n\n        if (g_pserver->m_pstorageFactory != nullptr && !fTest)\n        {\n            // We need to set max memory to a sane default so keys are actually evicted properly\n            if (g_pserver->maxmemory == 0 && g_pserver->maxmemory_policy == MAXMEMORY_NO_EVICTION)\n            {\n#ifdef __linux__\n                struct sysinfo sys;\n                if (sysinfo(&sys) == 0)\n                {\n                    // By default it's a little under half the memory.  This gives sufficient room for background saving\n                    g_pserver->maxmemory = static_cast<unsigned long long>(sys.totalram / 2.2);\n                    g_pserver->maxmemory_policy = MAXMEMORY_ALLKEYS_LRU;\n                }\n#else\n                serverLog(LL_WARNING, \"Unable to dynamically set maxmemory, please set maxmemory and maxmemory-policy if you are using a storage provier\");\n#endif\n            }\n            else if (g_pserver->maxmemory_policy == MAXMEMORY_NO_EVICTION)\n            {\n                g_pserver->maxmemory_policy = MAXMEMORY_ALLKEYS_LRU;\n            }\n        }\n        else\n        {\n            *err = \"Unknown storage provider\";\n        }\n        return g_pserver->m_pstorageFactory != nullptr;\n    }\n    catch(std::string str)\n    {\n        serverLog(LL_WARNING, \"ERROR: Failed to initialize %s storage provider. Details to follow below.\", g_sdsProvider);\n        serverLog(LL_WARNING, \"\\t%s\", str.c_str());\n        serverLog(LL_WARNING, \"KeyDB cannot start. Exiting.\");\n        exit(EXIT_FAILURE);\n    }\n}\n\n/* Parse an array of CONFIG_OOM_COUNT sds strings, validate and populate\n * g_pserver->oom_score_adj_values if valid.\n */\n\nstatic int updateOOMScoreAdjValues(sds *args, const char **err, int apply) {\n    int i;\n    int values[CONFIG_OOM_COUNT];\n\n    for (i = 0; i < CONFIG_OOM_COUNT; i++) {\n        char *eptr;\n        long long val = strtoll(args[i], &eptr, 10);\n\n        if (*eptr != '\\0' || val < -2000 || val > 2000) {\n            if (err) *err = \"Invalid oom-score-adj-values, elements must be between -2000 and 2000.\";\n            return C_ERR;\n        }\n\n        values[i] = val;\n    }\n\n    /* Verify that the values make sense. If they don't omit a warning but\n     * keep the configuration, which may still be valid for privileged processes.\n     */\n\n    if (values[CONFIG_OOM_REPLICA] < values[CONFIG_OOM_MASTER] ||\n        values[CONFIG_OOM_BGCHILD] < values[CONFIG_OOM_REPLICA]) {\n            serverLog(LL_WARNING,\n                    \"The oom-score-adj-values configuration may not work for non-privileged processes! \"\n                    \"Please consult the documentation.\");\n    }\n\n    /* Store values, retain previous config for rollback in case we fail. */\n    int old_values[CONFIG_OOM_COUNT];\n    for (i = 0; i < CONFIG_OOM_COUNT; i++) {\n        old_values[i] = g_pserver->oom_score_adj_values[i];\n        g_pserver->oom_score_adj_values[i] = values[i];\n    }\n    \n    /* When parsing the config file, we want to apply only when all is done. */\n    if (!apply)\n        return C_OK;\n\n    /* Update */\n    if (setOOMScoreAdj(-1) == C_ERR) {\n        /* Roll back */\n        for (i = 0; i < CONFIG_OOM_COUNT; i++)\n            g_pserver->oom_score_adj_values[i] = old_values[i];\n\n        if (err)\n            *err = \"Failed to apply oom-score-adj-values configuration, check server logs.\";\n\n        return C_ERR;\n    }\n\n    return C_OK;\n}\n\nvoid initConfigValues() {\n    for (standardConfig *config = configs; config->name != NULL; config++) {\n        config->interface.init(config->data);\n    }\n}\n\nvoid loadServerConfigFromString(char *config) {\n    const char *err = NULL;\n    int linenum = 0, totlines, i;\n    int slaveof_linenum = 0;\n    sds *lines;\n    int save_loaded = 0;\n\n    lines = sdssplitlen(config,strlen(config),\"\\n\",1,&totlines);\n\n    for (i = 0; i < totlines; i++) {\n        sds *argv;\n        int argc;\n\n        linenum = i+1;\n        lines[i] = sdstrim(lines[i],\" \\t\\r\\n\");\n\n        /* Skip comments and blank lines */\n        if (lines[i][0] == '#' || lines[i][0] == '\\0') continue;\n\n        /* Split into arguments */\n        argv = sdssplitargs(lines[i],&argc);\n        if (argv == NULL) {\n            err = \"Unbalanced quotes in configuration line\";\n            goto loaderr;\n        }\n\n        /* Skip this line if the resulting command vector is empty. */\n        if (argc == 0) {\n            sdsfreesplitres(argv,argc);\n            continue;\n        }\n        sdstolower(argv[0]);\n\n        /* Iterate the configs that are standard */\n        int match = 0;\n        for (standardConfig *config = configs; config->name != NULL; config++) {\n            if ((!strcasecmp(argv[0],config->name) ||\n                (config->alias && !strcasecmp(argv[0],config->alias))))\n            {\n                if (argc != 2) {\n                    err = \"wrong number of arguments\";\n                    goto loaderr;\n                }\n                if (!config->interface.set(config->data, argv[1], 0, &err)) {\n                    goto loaderr;\n                }\n\n                match = 1;\n                break;\n            }\n        }\n\n        if (match) {\n            sdsfreesplitres(argv,argc);\n            continue;\n        }\n\n        /* Execute config directives */\n        if (!strcasecmp(argv[0],\"bind\") && argc >= 2) {\n            int j, addresses = argc-1;\n\n            if (addresses > CONFIG_BINDADDR_MAX) {\n                err = \"Too many bind addresses specified\"; goto loaderr;\n            }\n            /* Free old bind addresses */\n            for (j = 0; j < g_pserver->bindaddr_count; j++) {\n                zfree(g_pserver->bindaddr[j]);\n            }\n            for (j = 0; j < addresses; j++)\n                g_pserver->bindaddr[j] = zstrdup(argv[j+1]);\n            g_pserver->bindaddr_count = addresses;\n        } else if (!strcasecmp(argv[0],\"unixsocketperm\") && argc == 2) {\n            errno = 0;\n            g_pserver->unixsocketperm = (mode_t)strtol(argv[1], NULL, 8);\n            if (errno || g_pserver->unixsocketperm > 0777) {\n                err = \"Invalid socket file permissions\"; goto loaderr;\n            }\n        } else if (!strcasecmp(argv[0],\"save\")) {\n            /* We don't reset save params before loading, because if they're not part\n             * of the file the defaults should be used.\n             */\n            if (!save_loaded) {\n                save_loaded = 1;\n                resetServerSaveParams();\n            }\n\n            if (argc == 3) {\n                int seconds = atoi(argv[1]);\n                int changes = atoi(argv[2]);\n                if (seconds < 1 || changes < 0) {\n                    err = \"Invalid save parameters\"; goto loaderr;\n                }\n                appendServerSaveParams(seconds,changes);\n            } else if (argc == 2 && !strcasecmp(argv[1],\"\")) {\n                resetServerSaveParams();\n            }\n        } else if (!strcasecmp(argv[0],\"dir\") && argc == 2) {\n            if (chdir(argv[1]) == -1) {\n                serverLog(LL_WARNING,\"Can't chdir to '%s': %s\",\n                    argv[1], strerror(errno));\n                exit(1);\n            }\n        } else if (!strcasecmp(argv[0],\"logfile\") && argc == 2) {\n            FILE *logfp;\n\n            zfree(g_pserver->logfile);\n            g_pserver->logfile = zstrdup(argv[1]);\n            if (g_pserver->logfile[0] != '\\0') {\n                /* Test if we are able to open the file. The server will not\n                 * be able to abort just for this problem later... */\n                logfp = fopen(g_pserver->logfile,\"a\");\n                if (logfp == NULL) {\n                    err = sdscatprintf(sdsempty(),\n                        \"Can't open the log file: %s\", strerror(errno));\n                    goto loaderr;\n                }\n                fclose(logfp);\n            }\n        } else if (!strcasecmp(argv[0],\"include\") && argc == 2) {\n            loadServerConfig(argv[1], 0, NULL);\n        } else if ((!strcasecmp(argv[0],\"slaveof\") ||\n                    !strcasecmp(argv[0],\"replicaof\")) && argc == 3) {\n            slaveof_linenum = linenum;\n            if (!strcasecmp(argv[1], \"no\") && !strcasecmp(argv[2], \"one\")) {\n                if (listLength(g_pserver->masters)) {\n                    listIter li;\n                    listNode *ln;\n                    listRewind(g_pserver->masters, &li);\n                    while ((ln = listNext(&li)))\n                    {\n                        struct redisMaster *mi = (struct redisMaster*)listNodeValue(ln);\n                        sdsfree(mi->masterauth);\n                        zfree(mi->masteruser);\n                        zfree(mi->repl_transfer_tmpfile);\n                        delete mi->staleKeyMap;\n                        zfree(mi);\n                        listDelNode(g_pserver->masters, ln);\n                    }\n                }\n                continue;\n            }\n            char *ptr;\n            int port = strtol(argv[2], &ptr, 10);\n            if (port < 0 || port > 65535 || *ptr != '\\0') {\n                err= \"Invalid master port\"; goto loaderr;\n            }\n            replicationAddMaster(argv[1], port);\n        } else if (!strcasecmp(argv[0],\"requirepass\") && argc == 2) {\n            if (strlen(argv[1]) > CONFIG_AUTHPASS_MAX_LEN) {\n                err = \"Password is longer than CONFIG_AUTHPASS_MAX_LEN\";\n                goto loaderr;\n            }\n        } else if (!strcasecmp(argv[0],\"list-max-ziplist-entries\") && argc == 2){\n            /* DEAD OPTION */\n        } else if (!strcasecmp(argv[0],\"list-max-ziplist-value\") && argc == 2) {\n            /* DEAD OPTION */\n        } else if (!strcasecmp(argv[0],\"rename-command\") && argc == 3) {\n            struct redisCommand *cmd = lookupCommand(argv[1]);\n            int retval;\n\n            if (!cmd) {\n                err = \"No such command in rename-command\";\n                goto loaderr;\n            }\n\n            /* If the target command name is the empty string we just\n             * remove it from the command table. */\n            retval = dictDelete(g_pserver->commands, argv[1]);\n            serverAssert(retval == DICT_OK);\n\n            /* Otherwise we re-add the command under a different name. */\n            if (sdslen(argv[2]) != 0) {\n                sds copy = sdsdup(argv[2]);\n\n                retval = dictAdd(g_pserver->commands, copy, cmd);\n                if (retval != DICT_OK) {\n                    sdsfree(copy);\n                    err = \"Target command name already exists\"; goto loaderr;\n                }\n            }\n        } else if (!strcasecmp(argv[0],\"cluster-config-file\") && argc == 2) {\n            zfree(g_pserver->cluster_configfile);\n            g_pserver->cluster_configfile = zstrdup(argv[1]);\n        } else if (!strcasecmp(argv[0],\"client-output-buffer-limit\") &&\n                   argc == 5)\n        {\n            int type = getClientTypeByName(argv[1]);\n            unsigned long long hard, soft;\n            int soft_seconds;\n\n            if (type == -1 || type == CLIENT_TYPE_MASTER) {\n                err = \"Unrecognized client limit class: the user specified \"\n                \"an invalid one, or 'master' which has no buffer limits.\";\n                goto loaderr;\n            }\n            hard = memtoll(argv[2],NULL);\n            soft = memtoll(argv[3],NULL);\n            soft_seconds = atoi(argv[4]);\n            if (soft_seconds < 0) {\n                err = \"Negative number of seconds in soft limit is invalid\";\n                goto loaderr;\n            }\n            cserver.client_obuf_limits[type].hard_limit_bytes = hard;\n            cserver.client_obuf_limits[type].soft_limit_bytes = soft;\n            cserver.client_obuf_limits[type].soft_limit_seconds = soft_seconds;\n        } else if (!strcasecmp(argv[0],\"oom-score-adj-values\") && argc == 1 + CONFIG_OOM_COUNT) {\n            if (updateOOMScoreAdjValues(&argv[1], &err, 0) == C_ERR) goto loaderr;\n        } else if (!strcasecmp(argv[0],\"notify-keyspace-events\") && argc == 2) {\n            int flags = keyspaceEventsStringToFlags(argv[1]);\n\n            if (flags == -1) {\n                err = \"Invalid event class character. Use 'g$lshzxeA'.\";\n                goto loaderr;\n            }\n            g_pserver->notify_keyspace_events = flags;\n        } else if (!strcasecmp(argv[0],\"user\") && argc >= 2) {\n            int argc_err;\n            if (ACLAppendUserForLoading(argv,argc,&argc_err) == C_ERR) {\n                char buf[1024];\n                const char *errmsg = ACLSetUserStringError();\n                snprintf(buf,sizeof(buf),\"Error in user declaration '%s': %s\",\n                    argv[argc_err],errmsg);\n                err = buf;\n                goto loaderr;\n            }\n        } else if (!strcasecmp(argv[0],\"loadmodule\") && argc >= 2) {\n            queueLoadModule(argv[1],&argv[2],argc-2);\n        } else if (!strcasecmp(argv[0],\"sentinel\")) {\n            /* argc == 1 is handled by main() as we need to enter the sentinel\n             * mode ASAP. */\n            if (argc != 1) {\n                if (!g_pserver->sentinel_mode) {\n                    err = \"sentinel directive while not in sentinel mode\";\n                    goto loaderr;\n                }\n                queueSentinelConfig(argv+1,argc-1,linenum,lines[i]);\n            }\n        } else if (!strcasecmp(argv[0],\"scratch-file-path\")) {\n#ifdef USE_MEMKIND\n            storage_init(argv[1], g_pserver->maxmemory);\n#else\n            err = \"KeyDB not compliled with scratch-file support.\";\n            goto loaderr;\n#endif\n        } else if ((!strcasecmp(argv[0],\"server-threads\") || !strcasecmp(argv[0],\"io-threads\")) && argc == 2) {\n            cserver.cthreads = atoi(argv[1]);\n            if (cserver.cthreads <= 0 || cserver.cthreads > MAX_EVENT_LOOPS) {\n                err = \"Invalid number of threads specified\";\n                goto loaderr;\n            }\n        } else if (!strcasecmp(argv[0],\"server-thread-affinity\") && argc == 2) {\n            if (strcasecmp(argv[1], \"true\") == 0) {\n                cserver.fThreadAffinity = TRUE;\n            } else if (strcasecmp(argv[1], \"false\") == 0) {\n                cserver.fThreadAffinity = FALSE;\n            } else {\n                int offset = atoi(argv[1]);\n                if (offset > 0) {\n                    cserver.fThreadAffinity = TRUE;\n                    cserver.threadAffinityOffset = offset-1;\n                } else {\n                    err = \"Unknown argument: server-thread-affinity expects either true or false\";\n                    goto loaderr;\n                }\n            }\n        } else if (!strcasecmp(argv[0], \"active-replica\") && argc == 2) {\n            g_pserver->fActiveReplica = yesnotoi(argv[1]);\n            if (g_pserver->fActiveReplica && g_pserver->repl_slave_ro) {\n                g_pserver->repl_slave_ro = FALSE;\n                serverLog(LL_NOTICE, \"Notice: \\\"active-replica yes\\\" implies \\\"replica-read-only no\\\"\");\n            }\n            if (g_pserver->fActiveReplica == -1) {\n                g_pserver->fActiveReplica = CONFIG_DEFAULT_ACTIVE_REPLICA;\n                err = \"argument must be 'yes' or 'no'\"; goto loaderr;\n            }\n            if (listLength(g_pserver->masters) && g_pserver->fActiveReplica) {\n                err = \"must not set replica-of config before active-replica config\"; goto loaderr;\n            }\n        } else if (!strcasecmp(argv[0], \"multi-master\") && argc == 2) {\n            g_pserver->enable_multimaster = yesnotoi(argv[1]);\n            if (g_pserver->enable_multimaster == -1) {\n                g_pserver->enable_multimaster = CONFIG_DEFAULT_ENABLE_MULTIMASTER;\n                err = \"argument must be 'yes' or 'no'\"; goto loaderr;\n            }\n            if (listLength(g_pserver->masters) && g_pserver->enable_multimaster) {\n                err = \"must not set replica-of config before multi-master config\"; goto loaderr;\n            }\n        } else if (!strcasecmp(argv[0], \"tls-allowlist\")) {\n            if (argc < 2) {\n                err = \"must supply at least one element in the allow list\"; goto loaderr;\n            }\n            if (!g_pserver->tls_allowlist.empty()) {\n                err = \"tls-allowlist may only be set once\"; goto loaderr;\n            }\n            for (int i = 1; i < argc; i++)\n                g_pserver->tls_allowlist.emplace(argv[i], strlen(argv[i]));\n        } else if (!strcasecmp(argv[0], \"tls-auditlog-blocklist\")) {\n            if (argc < 2) {\n                err = \"must supply at least one element in the block list\"; goto loaderr;\n            }\n            if (!g_pserver->tls_auditlog_blocklist.empty()) {\n                err = \"tls-auditlog-blocklist may only be set once\"; goto loaderr;\n            }\n            for (int i = 1; i < argc; i++)\n                g_pserver->tls_auditlog_blocklist.emplace(argv[i], strlen(argv[i]));\n        } else if (!strcasecmp(argv[0], \"version-override\") && argc == 2) {\n            KEYDB_SET_VERSION = zstrdup(argv[1]);\n            serverLog(LL_WARNING, \"Warning version is overriden to: %s\\n\", KEYDB_SET_VERSION);\n        } else if (!strcasecmp(argv[0],\"testmode\") && argc == 2){\n            g_fTestMode = yesnotoi(argv[1]);\n        } else if (!strcasecmp(argv[0],\"rdbfuzz-mode\")) {\n            // NOP, handled in main\n        } else if (!strcasecmp(argv[0],\"storage-provider\") && argc >= 2) {\n            g_sdsProvider = sdsdup(argv[1]);\n            if (argc > 2)\n                g_sdsArgs = sdsdup(argv[2]);\n\t    } else if (!strcasecmp(argv[0],\"is-flash-enabled\") && argc == 1) {\n#ifdef ENABLE_ROCKSDB\n            exit(EXIT_SUCCESS);\n#else\n            exit(EXIT_FAILURE);\n#endif\n        } else {\n            err = \"Bad directive or wrong number of arguments\"; goto loaderr;\n        }\n        sdsfreesplitres(argv,argc);\n    }\n\n    /* Sanity checks. */\n    if (g_pserver->cluster_enabled && listLength(g_pserver->masters)) {\n        linenum = slaveof_linenum;\n        i = linenum-1;\n        err = \"replicaof directive not allowed in cluster mode\";\n        goto loaderr;\n    }\n\n    /* To ensure backward compatibility and work while hz is out of range */\n    if (g_pserver->config_hz < CONFIG_MIN_HZ) g_pserver->config_hz = CONFIG_MIN_HZ;\n    if (g_pserver->config_hz > CONFIG_MAX_HZ) g_pserver->config_hz = CONFIG_MAX_HZ;\n\n    sdsfreesplitres(lines,totlines);\n    return;\n\nloaderr:\n    fprintf(stderr, \"\\n*** FATAL CONFIG FILE ERROR (KeyDB %s) ***\\n\",\n        KEYDB_REAL_VERSION);\n    fprintf(stderr, \"Reading the configuration file, at line %d\\n\", linenum);\n    fprintf(stderr, \">>> '%s'\\n\", lines[i]);\n    fprintf(stderr, \"%s\\n\", err);\n    exit(1);\n}\n\n/* Load the server configuration from the specified filename.\n * The function appends the additional configuration directives stored\n * in the 'options' string to the config file before loading.\n *\n * Both filename and options can be NULL, in such a case are considered\n * empty. This way loadServerConfig can be used to just load a file or\n * just load a string. */\nvoid loadServerConfig(char *filename, char config_from_stdin, char *options) {\n    sds config = sdsempty();\n    char buf[CONFIG_MAX_LINE+1];\n    FILE *fp;\n\n    /* Load the file content */\n    if (filename) {\n        if ((fp = fopen(filename,\"r\")) == NULL) {\n            serverLog(LL_WARNING,\n                    \"Fatal error, can't open config file '%s': %s\",\n                    filename, strerror(errno));\n            exit(1);\n        }\n        while(fgets(buf,CONFIG_MAX_LINE+1,fp) != NULL)\n            config = sdscat(config,buf);\n        fclose(fp);\n    }\n    /* Append content from stdin */\n    if (config_from_stdin) {\n        serverLog(LL_WARNING,\"Reading config from stdin\");\n        fp = stdin;\n        while(fgets(buf,CONFIG_MAX_LINE+1,fp) != NULL)\n            config = sdscat(config,buf);\n    }\n\n    /* Append the additional options */\n    if (options) {\n        config = sdscat(config,\"\\n\");\n        config = sdscat(config,options);\n    }\n    loadServerConfigFromString(config);\n    sdsfree(config);\n}\n\n/*-----------------------------------------------------------------------------\n * CONFIG SET implementation\n *----------------------------------------------------------------------------*/\n\n#define config_set_bool_field(_name,_var) \\\n    } else if (!strcasecmp(szFromObj(c->argv[2]),_name)) { \\\n        int yn = yesnotoi(szFromObj(o)); \\\n        if (yn == -1) goto badfmt; \\\n        _var = yn;\n\n#define config_set_numerical_field(_name,_var,min,max) \\\n    } else if (!strcasecmp(szFromObj(c->argv[2]),_name)) { \\\n        if (getLongLongFromObject(o,&ll) == C_ERR) goto badfmt; \\\n        if (min != LLONG_MIN && ll < min) goto badfmt; \\\n        if (max != LLONG_MAX && ll > max) goto badfmt; \\\n        _var = ll;\n\n#define config_set_memory_field(_name,_var) \\\n    } else if (!strcasecmp(szFromObj(c->argv[2]),_name)) { \\\n        ll = memtoll(szFromObj(o),&err); \\\n        if (err || ll < 0) goto badfmt; \\\n        _var = ll;\n\n#define config_set_special_field(_name) \\\n    } else if (!strcasecmp(szFromObj(c->argv[2]),_name)) {\n\n#define config_set_special_field_with_alias(_name1,_name2) \\\n    } else if (!strcasecmp(szFromObj(c->argv[2]),_name1) || \\\n               !strcasecmp(szFromObj(c->argv[2]),_name2)) {\n\n#define config_set_else } else\n\nvoid configSetCommand(client *c) {\n    robj *o;\n    long long ll;\n    int err;\n    const char *errstr = NULL;\n    serverAssertWithInfo(c,c->argv[2],sdsEncodedObject(c->argv[2]));\n\n    if (c->argc < 4 || c->argc > 4) {\n        o = nullptr;\n        // Variadic set is only supported for tls-allowlist\n        if (strcasecmp(szFromObj(c->argv[2]), \"tls-allowlist\")) {\n            addReplySubcommandSyntaxError(c);\n            return;\n        }\n    } else {\n        o = c->argv[3];\n        serverAssertWithInfo(c,c->argv[3],sdsEncodedObject(c->argv[3]));\n    }\n\n    /* Iterate the configs that are standard */\n    for (standardConfig *config = configs; config->name != NULL; config++) {\n        if (!(config->flags & IMMUTABLE_CONFIG) && \n            (!strcasecmp(szFromObj(c->argv[2]),config->name) ||\n            (config->alias && !strcasecmp(szFromObj(c->argv[2]),config->alias))))\n        {\n            if (config->flags & SENSITIVE_CONFIG) {\n                redactClientCommandArgument(c,3);\n            }\n            if (!config->interface.set(config->data,szFromObj(o),1,&errstr)) {\n                goto badfmt;\n            }\n            addReply(c,shared.ok);\n            return;\n        }\n    }\n\n    if (0) { /* this starts the config_set macros else-if chain. */\n\n    /* Special fields that can't be handled with general macros. */\n    config_set_special_field(\"bind\") {\n        int vlen;\n        sds *v = sdssplitlen(szFromObj(o),sdslen(szFromObj(o)),\" \",1,&vlen);\n\n        if (vlen < 1 || vlen > CONFIG_BINDADDR_MAX) {\n            addReplyError(c, \"Too many bind addresses specified.\");\n            sdsfreesplitres(v, vlen);\n            return;\n        }\n\n        if (changeBindAddr(v, vlen, true) == C_ERR) {\n            addReplyError(c, \"Failed to bind to specified addresses.\");\n            sdsfreesplitres(v, vlen);\n            return;\n        }\n        // Now run the config change on the other threads\n        for (int ithread = 0; ithread < cserver.cthreads; ++ithread) {\n            if (&g_pserver->rgthreadvar[ithread] != serverTL) {\n                incrRefCount(o);\n                aePostFunction(g_pserver->rgthreadvar[ithread].el, [o]{\n                    int vlen;\n                    sds *v = sdssplitlen(szFromObj(o),sdslen(szFromObj(o)),\" \",1,&vlen);\n                    if (changeBindAddr(v, vlen, false) == C_ERR) {\n                        serverLog(LL_WARNING, \"Failed to change the bind address for a thread.  Server will still be listening on old addresses.\");\n                    }\n                    sdsfreesplitres(v, vlen);\n                    decrRefCount(o);\n                });\n            }\n        }\n\n        sdsfreesplitres(v, vlen);\n    } config_set_special_field(\"save\") {\n        int vlen, j;\n        sds *v = sdssplitlen(szFromObj(o),sdslen(szFromObj(o)),\" \",1,&vlen);\n\n        /* Perform sanity check before setting the new config:\n         * - Even number of args\n         * - Seconds >= 1, changes >= 0 */\n        if (vlen & 1) {\n            sdsfreesplitres(v,vlen);\n            goto badfmt;\n        }\n        for (j = 0; j < vlen; j++) {\n            char *eptr;\n            long val;\n\n            val = strtoll(v[j], &eptr, 10);\n            if (eptr[0] != '\\0' ||\n                ((j & 1) == 0 && val < 1) ||\n                ((j & 1) == 1 && val < 0)) {\n                sdsfreesplitres(v,vlen);\n                goto badfmt;\n            }\n        }\n        /* Finally set the new config */\n        resetServerSaveParams();\n        for (j = 0; j < vlen; j += 2) {\n            time_t seconds;\n            int changes;\n\n            seconds = strtoll(v[j],NULL,10);\n            changes = strtoll(v[j+1],NULL,10);\n            appendServerSaveParams(seconds, changes);\n        }\n        sdsfreesplitres(v,vlen);\n    } config_set_special_field(\"dir\") {\n        if (chdir((char*)ptrFromObj(o)) == -1) {\n            addReplyErrorFormat(c,\"Changing directory: %s\", strerror(errno));\n            return;\n        }\n    } config_set_special_field(\"client-output-buffer-limit\") {\n        int vlen, j;\n        sds *v = sdssplitlen(szFromObj(o),sdslen(szFromObj(o)),\" \",1,&vlen);\n\n        /* We need a multiple of 4: <class> <hard> <soft> <soft_seconds> */\n        if (vlen % 4) {\n            sdsfreesplitres(v,vlen);\n            goto badfmt;\n        }\n\n        /* Sanity check of single arguments, so that we either refuse the\n         * whole configuration string or accept it all, even if a single\n         * error in a single client class is present. */\n        for (j = 0; j < vlen; j++) {\n            long val;\n\n            if ((j % 4) == 0) {\n                int type = getClientTypeByName(v[j]);\n                if (type == -1 || type == CLIENT_TYPE_MASTER) {\n                    sdsfreesplitres(v,vlen);\n                    goto badfmt;\n                }\n            } else {\n                val = memtoll(v[j], &err);\n                if (err || val < 0) {\n                    sdsfreesplitres(v,vlen);\n                    goto badfmt;\n                }\n            }\n        }\n        /* Finally set the new config */\n        for (j = 0; j < vlen; j += 4) {\n            unsigned long long hard, soft;\n            int soft_seconds;\n\n            int type = getClientTypeByName(v[j]);\n            hard = memtoll(v[j+1],NULL);\n            soft = memtoll(v[j+2],NULL);\n            soft_seconds = strtoll(v[j+3],NULL,10);\n\n            cserver.client_obuf_limits[type].hard_limit_bytes = hard;\n            cserver.client_obuf_limits[type].soft_limit_bytes = soft;\n            cserver.client_obuf_limits[type].soft_limit_seconds = soft_seconds;\n        }\n        sdsfreesplitres(v,vlen);\n    } config_set_special_field(\"oom-score-adj-values\") {\n        int vlen;\n        int success = 1;\n\n        sds *v = sdssplitlen(szFromObj(o), sdslen(szFromObj(o)), \" \", 1, &vlen);\n        if (vlen != CONFIG_OOM_COUNT || updateOOMScoreAdjValues(v, &errstr, 1) == C_ERR)\n            success = 0;\n\n        sdsfreesplitres(v, vlen);\n        if (!success)\n            goto badfmt;\n    } config_set_special_field(\"notify-keyspace-events\") {\n        int flags = keyspaceEventsStringToFlags(szFromObj(o));\n\n        if (flags == -1) goto badfmt;\n        g_pserver->notify_keyspace_events = flags;\n    /* Numerical fields.\n     * config_set_numerical_field(name,var,min,max) */\n    } config_set_numerical_field(\n      \"watchdog-period\",ll,0,INT_MAX) {\n        if (ll)\n            enableWatchdog(ll);\n        else\n            disableWatchdog();\n    } config_set_special_field(\"tls-allowlist\") {\n        g_pserver->tls_allowlist.clear();\n        for (int i = 3; i < c->argc; ++i) {\n            robj *val = c->argv[i];\n            g_pserver->tls_allowlist.emplace(szFromObj(val), sdslen(szFromObj(val)));\n        }\n    /* Everything else is an error... */\n    } config_set_else {\n        addReplyErrorFormat(c,\"Unsupported CONFIG parameter: %s\",\n            (char*)ptrFromObj(c->argv[2]));\n        return;\n    }\n\n    /* On success we just return a generic OK for all the options. */\n    addReply(c,shared.ok);\n    return;\n\nbadfmt: /* Bad format errors */\n    if (errstr) {\n        addReplyErrorFormat(c,\"Invalid argument '%s' for CONFIG SET '%s' - %s\",\n                szFromObj(o),\n                szFromObj(c->argv[2]),\n                errstr);\n    } else {\n        addReplyErrorFormat(c,\"Invalid argument '%s' for CONFIG SET '%s'\",\n                szFromObj(o),\n                szFromObj(c->argv[2]));\n    }\n}\n\n/*-----------------------------------------------------------------------------\n * CONFIG GET implementation\n *----------------------------------------------------------------------------*/\n\n#define config_get_string_field(_name,_var) do { \\\n    if (stringmatch(pattern,_name,1)) { \\\n        addReplyBulkCString(c,_name); \\\n        addReplyBulkCString(c,_var ? _var : \"\"); \\\n        matches++; \\\n    } \\\n} while(0)\n\n#define config_get_bool_field(_name,_var) do { \\\n    if (stringmatch(pattern,_name,1)) { \\\n        addReplyBulkCString(c,_name); \\\n        addReplyBulkCString(c,_var ? \"yes\" : \"no\"); \\\n        matches++; \\\n    } \\\n} while(0)\n\n#define config_get_numerical_field(_name,_var) do { \\\n    if (stringmatch(pattern,_name,1)) { \\\n        ll2string(buf,sizeof(buf),_var); \\\n        addReplyBulkCString(c,_name); \\\n        addReplyBulkCString(c,buf); \\\n        matches++; \\\n    } \\\n} while(0)\n\nvoid configGetCommand(client *c) {\n    robj *o = c->argv[2];\n    void *replylen = addReplyDeferredLen(c);\n    char *pattern = szFromObj(o);\n    char buf[128];\n    int matches = 0;\n    serverAssertWithInfo(c,o,sdsEncodedObject(o));\n\n    /* Iterate the configs that are standard */\n    for (standardConfig *config = configs; config->name != NULL; config++) {\n        if (stringmatch(pattern,config->name,1)) {\n            addReplyBulkCString(c,config->name);\n            config->interface.get(c,config->data);\n            matches++;\n        }\n        if (config->alias && stringmatch(pattern,config->alias,1)) {\n            addReplyBulkCString(c,config->alias);\n            config->interface.get(c,config->data);\n            matches++;\n        }\n    }\n\n    /* String values */\n    config_get_string_field(\"logfile\",g_pserver->logfile);\n\n    /* Numerical values */\n    config_get_numerical_field(\"watchdog-period\",g_pserver->watchdog_period);\n\n    /* Everything we can't handle with macros follows. */\n\n    if (stringmatch(pattern,\"dir\",1)) {\n        char buf[1024];\n\n        if (getcwd(buf,sizeof(buf)) == NULL)\n            buf[0] = '\\0';\n\n        addReplyBulkCString(c,\"dir\");\n        addReplyBulkCString(c,buf);\n        matches++;\n    }\n    if (stringmatch(pattern,\"save\",1)) {\n        sds buf = sdsempty();\n        int j;\n\n        for (j = 0; j < g_pserver->saveparamslen; j++) {\n            buf = sdscatprintf(buf,\"%jd %d\",\n                    (intmax_t)g_pserver->saveparams[j].seconds,\n                    g_pserver->saveparams[j].changes);\n            if (j != g_pserver->saveparamslen-1)\n                buf = sdscatlen(buf,\" \",1);\n        }\n        addReplyBulkCString(c,\"save\");\n        addReplyBulkCString(c,buf);\n        sdsfree(buf);\n        matches++;\n    }\n    if (stringmatch(pattern,\"client-output-buffer-limit\",1)) {\n        sds buf = sdsempty();\n        int j;\n\n        for (j = 0; j < CLIENT_TYPE_OBUF_COUNT; j++) {\n            buf = sdscatprintf(buf,\"%s %llu %llu %ld\",\n                    getClientTypeName(j),\n                    cserver.client_obuf_limits[j].hard_limit_bytes,\n                    cserver.client_obuf_limits[j].soft_limit_bytes,\n                    (long) cserver.client_obuf_limits[j].soft_limit_seconds);\n            if (j != CLIENT_TYPE_OBUF_COUNT-1)\n                buf = sdscatlen(buf,\" \",1);\n        }\n        addReplyBulkCString(c,\"client-output-buffer-limit\");\n        addReplyBulkCString(c,buf);\n        sdsfree(buf);\n        matches++;\n    }\n    if (stringmatch(pattern,\"unixsocketperm\",1)) {\n        char buf[32];\n        snprintf(buf,sizeof(buf),\"%lo\",(unsigned long)g_pserver->unixsocketperm);\n        addReplyBulkCString(c,\"unixsocketperm\");\n        addReplyBulkCString(c,buf);\n        matches++;\n    }\n    if (stringmatch(pattern,\"slaveof\",1) ||\n        stringmatch(pattern,\"replicaof\",1))\n    {\n        const char *optname = stringmatch(pattern,\"slaveof\",1) ?\n                        \"slaveof\" : \"replicaof\";\n        char buf[256];\n        addReplyBulkCString(c,optname);\n        if (listLength(g_pserver->masters) == 0)\n        {\n            buf[0] = '\\0';\n            addReplyBulkCString(c,buf);\n        }\n        else\n        {\n            listIter li;\n            listNode *ln;\n            listRewind(g_pserver->masters, &li);\n            bool fFirst = true;\n            while ((ln = listNext(&li)))\n            {\n                if (!fFirst)\n                {\n                    addReplyBulkCString(c,optname);\n                    matches++;\n                }\n                fFirst = false;\n                struct redisMaster *mi = (struct redisMaster*)listNodeValue(ln);\n                snprintf(buf,sizeof(buf),\"%s %d\",\n                    mi->masterhost, mi->masterport);\n                addReplyBulkCString(c,buf);\n            }\n        }\n        matches++;\n    }\n    if (stringmatch(pattern,\"notify-keyspace-events\",1)) {\n        sds flags = keyspaceEventsFlagsToString(g_pserver->notify_keyspace_events);\n\n        addReplyBulkCString(c,\"notify-keyspace-events\");\n        addReplyBulkSds(c,flags);\n        matches++;\n    }\n    if (stringmatch(pattern,\"bind\",1)) {\n        sds aux = sdsjoin(g_pserver->bindaddr,g_pserver->bindaddr_count,\" \");\n\n        addReplyBulkCString(c,\"bind\");\n        addReplyBulkCString(c,aux);\n        sdsfree(aux);\n        matches++;\n    }\n    if (stringmatch(pattern,\"oom-score-adj-values\",0)) {\n        sds buf = sdsempty();\n        int j;\n\n        for (j = 0; j < CONFIG_OOM_COUNT; j++) {\n            buf = sdscatprintf(buf,\"%d\", g_pserver->oom_score_adj_values[j]);\n            if (j != CONFIG_OOM_COUNT-1)\n                buf = sdscatlen(buf,\" \",1);\n        }\n\n        addReplyBulkCString(c,\"oom-score-adj-values\");\n        addReplyBulkCString(c,buf);\n        sdsfree(buf);\n        matches++;\n    }\n    if (stringmatch(pattern,\"active-replica\",1)) {\n        addReplyBulkCString(c,\"active-replica\");\n        addReplyBulkCString(c, g_pserver->fActiveReplica ? \"yes\" : \"no\");\n        matches++;\n    }\n    if (stringmatch(pattern, \"tls-allowlist\", 1)) {\n        addReplyBulkCString(c,\"tls-allowlist\");\n        addReplyArrayLen(c, (long)g_pserver->tls_allowlist.size());\n        for (auto &elem : g_pserver->tls_allowlist) {\n            addReplyBulkCBuffer(c, elem.get(), elem.size()); // addReplyBulkSds will free which we absolutely don't want\n        }\n        matches++;\n    }\n\n    setDeferredMapLen(c,replylen,matches);\n}\n\n/*-----------------------------------------------------------------------------\n * CONFIG REWRITE implementation\n *----------------------------------------------------------------------------*/\n\n#define REDIS_CONFIG_REWRITE_SIGNATURE \"# Generated by CONFIG REWRITE\"\n\n/* We use the following dictionary type to store where a configuration\n * option is mentioned in the old configuration file, so it's\n * like \"maxmemory\" -> list of line numbers (first line is zero). */\nuint64_t dictSdsCaseHash(const void *key);\nint dictSdsKeyCaseCompare(void *privdata, const void *key1, const void *key2);\nvoid dictSdsDestructor(void *privdata, void *val);\nvoid dictListDestructor(void *privdata, void *val);\n\n/* Sentinel config rewriting is implemented inside sentinel.c by\n * rewriteConfigSentinelOption(). */\nvoid rewriteConfigSentinelOption(struct rewriteConfigState *state);\n\ndictType optionToLineDictType = {\n    dictSdsCaseHash,            /* hash function */\n    NULL,                       /* key dup */\n    NULL,                       /* val dup */\n    dictSdsKeyCaseCompare,      /* key compare */\n    dictSdsDestructor,          /* key destructor */\n    dictListDestructor,         /* val destructor */\n    NULL                        /* allow to expand */\n};\n\ndictType optionSetDictType = {\n    dictSdsCaseHash,            /* hash function */\n    NULL,                       /* key dup */\n    NULL,                       /* val dup */\n    dictSdsKeyCaseCompare,      /* key compare */\n    dictSdsDestructor,          /* key destructor */\n    NULL,                       /* val destructor */\n    NULL                        /* allow to expand */\n};\n\n/* The config rewrite state. */\nstruct rewriteConfigState {\n    dict *option_to_line; /* Option -> list of config file lines map */\n    dict *rewritten;      /* Dictionary of already processed options */\n    int numlines;         /* Number of lines in current config */\n    sds *lines;           /* Current lines as an array of sds strings */\n    int has_tail;         /* True if we already added directives that were\n                             not present in the original config file. */\n    int force_all;        /* True if we want all keywords to be force\n                             written. Currently only used for testing. */\n};\n\n/* Append the new line to the current configuration state. */\nvoid rewriteConfigAppendLine(struct rewriteConfigState *state, sds line) {\n    state->lines = (sds*)zrealloc(state->lines, sizeof(char*) * (state->numlines+1), MALLOC_LOCAL);\n    state->lines[state->numlines++] = line;\n}\n\n/* Populate the option -> list of line numbers map. */\nvoid rewriteConfigAddLineNumberToOption(struct rewriteConfigState *state, sds option, int linenum) {\n    list *l = (list*)dictFetchValue(state->option_to_line,option);\n\n    if (l == NULL) {\n        l = listCreate();\n        dictAdd(state->option_to_line,sdsdup(option),l);\n    }\n    listAddNodeTail(l,(void*)(long)linenum);\n}\n\n/* Add the specified option to the set of processed options.\n * This is useful as only unused lines of processed options will be blanked\n * in the config file, while options the rewrite process does not understand\n * remain untouched. */\nvoid rewriteConfigMarkAsProcessed(struct rewriteConfigState *state, const char *option) {\n    sds opt = sdsnew(option);\n\n    if (dictAdd(state->rewritten,opt,NULL) != DICT_OK) sdsfree(opt);\n}\n\n/* Read the old file, split it into lines to populate a newly created\n * config rewrite state, and return it to the caller.\n *\n * If it is impossible to read the old file, NULL is returned.\n * If the old file does not exist at all, an empty state is returned. */\nstruct rewriteConfigState *rewriteConfigReadOldFile(char *path) {\n    FILE *fp = fopen(path,\"r\");\n    struct rewriteConfigState *state = (rewriteConfigState*)zmalloc(sizeof(*state), MALLOC_LOCAL);\n    char buf[CONFIG_MAX_LINE+1];\n    int linenum = -1;\n\n    if (fp == NULL && errno != ENOENT) return NULL;\n\n    state->option_to_line = dictCreate(&optionToLineDictType,NULL);\n    state->rewritten = dictCreate(&optionSetDictType,NULL);\n    state->numlines = 0;\n    state->lines = NULL;\n    state->has_tail = 0;\n    state->force_all = 0;\n    if (fp == NULL) return state;\n\n    /* Read the old file line by line, populate the state. */\n    while(fgets(buf,CONFIG_MAX_LINE+1,fp) != NULL) {\n        int argc;\n        sds *argv;\n        sds line = sdstrim(sdsnew(buf),\"\\r\\n\\t \");\n\n        linenum++; /* Zero based, so we init at -1 */\n\n        /* Handle comments and empty lines. */\n        if (line[0] == '#' || line[0] == '\\0') {\n            if (!state->has_tail && !strcmp(line,REDIS_CONFIG_REWRITE_SIGNATURE))\n                state->has_tail = 1;\n            rewriteConfigAppendLine(state,line);\n            continue;\n        }\n\n        /* Not a comment, split into arguments. */\n        argv = sdssplitargs(line,&argc);\n        if (argv == NULL) {\n            /* Apparently the line is unparsable for some reason, for\n             * instance it may have unbalanced quotes. Load it as a\n             * comment. */\n            sds aux = sdsnew(\"# ??? \");\n            aux = sdscatsds(aux,line);\n            sdsfree(line);\n            rewriteConfigAppendLine(state,aux);\n            continue;\n        }\n\n        sdstolower(argv[0]); /* We only want lowercase config directives. */\n\n        /* Now we populate the state according to the content of this line.\n         * Append the line and populate the option -> line numbers map. */\n        rewriteConfigAppendLine(state,line);\n\n        /* Translate options using the word \"slave\" to the corresponding name\n         * \"replica\", before adding such option to the config name -> lines\n         * mapping. */\n        char *p = strstr(argv[0],\"slave\");\n        if (p) {\n            sds alt = sdsempty();\n            alt = sdscatlen(alt,argv[0],p-argv[0]);\n            alt = sdscatlen(alt,\"replica\",7);\n            alt = sdscatlen(alt,p+5,strlen(p+5));\n            sdsfree(argv[0]);\n            argv[0] = alt;\n        }\n        /* If this is sentinel config, we use sentinel \"sentinel <config>\" as option \n            to avoid messing up the sequence. */\n        if (g_pserver->sentinel_mode && argc > 1 && !strcasecmp(argv[0],\"sentinel\")) {\n            sds sentinelOption = sdsempty();\n            sentinelOption = sdscatfmt(sentinelOption,\"%S %S\",argv[0],argv[1]);\n            rewriteConfigAddLineNumberToOption(state,sentinelOption,linenum);\n            sdsfree(sentinelOption);\n        } else {\n            rewriteConfigAddLineNumberToOption(state,argv[0],linenum);\n        }\n        sdsfreesplitres(argv,argc);\n    }\n    fclose(fp);\n    return state;\n}\n\n/* Rewrite the specified configuration option with the new \"line\".\n * It progressively uses lines of the file that were already used for the same\n * configuration option in the old version of the file, removing that line from\n * the map of options -> line numbers.\n *\n * If there are lines associated with a given configuration option and\n * \"force\" is non-zero, the line is appended to the configuration file.\n * Usually \"force\" is true when an option has not its default value, so it\n * must be rewritten even if not present previously.\n *\n * The first time a line is appended into a configuration file, a comment\n * is added to show that starting from that point the config file was generated\n * by CONFIG REWRITE.\n *\n * \"line\" is either used, or freed, so the caller does not need to free it\n * in any way. */\nvoid rewriteConfigRewriteLine(struct rewriteConfigState *state, const char *option, sds line, int force) {\n    sds o = sdsnew(option);\n    list *l = (list*)dictFetchValue(state->option_to_line,o);\n\n    rewriteConfigMarkAsProcessed(state,option);\n\n    if (!l && !force && !state->force_all) {\n        /* Option not used previously, and we are not forced to use it. */\n        sdsfree(line);\n        sdsfree(o);\n        return;\n    }\n\n    if (l) {\n        listNode *ln = listFirst(l);\n        int linenum = (long) ln->value;\n\n        /* There are still lines in the old configuration file we can reuse\n         * for this option. Replace the line with the new one. */\n        listDelNode(l,ln);\n        if (listLength(l) == 0) dictDelete(state->option_to_line,o);\n        sdsfree(state->lines[linenum]);\n        state->lines[linenum] = line;\n    } else {\n        /* Append a new line. */\n        if (!state->has_tail) {\n            rewriteConfigAppendLine(state,\n                sdsnew(REDIS_CONFIG_REWRITE_SIGNATURE));\n            state->has_tail = 1;\n        }\n        rewriteConfigAppendLine(state,line);\n    }\n    sdsfree(o);\n}\n\n/* Write the long long 'bytes' value as a string in a way that is parsable\n * inside keydb.conf. If possible uses the GB, MB, KB notation. */\nint rewriteConfigFormatMemory(char *buf, size_t len, long long bytes) {\n    int gb = 1024*1024*1024;\n    int mb = 1024*1024;\n    int kb = 1024;\n\n    if (bytes && (bytes % gb) == 0) {\n        return snprintf(buf,len,\"%lldgb\",bytes/gb);\n    } else if (bytes && (bytes % mb) == 0) {\n        return snprintf(buf,len,\"%lldmb\",bytes/mb);\n    } else if (bytes && (bytes % kb) == 0) {\n        return snprintf(buf,len,\"%lldkb\",bytes/kb);\n    } else {\n        return snprintf(buf,len,\"%lld\",bytes);\n    }\n}\n\n/* Rewrite a simple \"option-name <bytes>\" configuration option. */\nvoid rewriteConfigBytesOption(struct rewriteConfigState *state, const char *option, long long value, long long defvalue) {\n    char buf[64];\n    int force = value != defvalue;\n    sds line;\n\n    rewriteConfigFormatMemory(buf,sizeof(buf),value);\n    line = sdscatprintf(sdsempty(),\"%s %s\",option,buf);\n    rewriteConfigRewriteLine(state,option,line,force);\n}\n\n/* Rewrite a yes/no option. */\nvoid rewriteConfigYesNoOption(struct rewriteConfigState *state, const char *option, int value, int defvalue) {\n    int force = value != defvalue;\n    sds line = sdscatprintf(sdsempty(),\"%s %s\",option,\n        value ? \"yes\" : \"no\");\n\n    rewriteConfigRewriteLine(state,option,line,force);\n}\n\n/* Rewrite a string option. */\nvoid rewriteConfigStringOption(struct rewriteConfigState *state, const char *option, const char *value, const char *defvalue) {\n    int force = 1;\n    sds line;\n\n    /* String options set to NULL need to be not present at all in the\n     * configuration file to be set to NULL again at the next reboot. */\n    if (value == NULL) {\n        rewriteConfigMarkAsProcessed(state,option);\n        return;\n    }\n\n    /* Set force to zero if the value is set to its default. */\n    if (defvalue && strcmp(value,defvalue) == 0) force = 0;\n\n    line = sdsnew(option);\n    line = sdscatlen(line, \" \", 1);\n    line = sdscatrepr(line, value, strlen(value));\n\n    rewriteConfigRewriteLine(state,option,line,force);\n}\n\n/* Rewrite a SDS string option. */\nvoid rewriteConfigSdsOption(struct rewriteConfigState *state, const char *option, sds value, const sds defvalue) {\n    int force = 1;\n    sds line;\n\n    /* If there is no value set, we don't want the SDS option\n     * to be present in the configuration at all. */\n    if (value == NULL) {\n        rewriteConfigMarkAsProcessed(state, option);\n        return;\n    }\n\n    /* Set force to zero if the value is set to its default. */\n    if (defvalue && sdscmp(value, defvalue) == 0) force = 0;\n\n    line = sdsnew(option);\n    line = sdscatlen(line, \" \", 1);\n    line = sdscatrepr(line, value, sdslen(value));\n\n    rewriteConfigRewriteLine(state, option, line, force);\n}\n\n/* Rewrite a numerical (long long range) option. */\nvoid rewriteConfigNumericalOption(struct rewriteConfigState *state, const char *option, long long value, long long defvalue) {\n    int force = value != defvalue;\n    sds line = sdscatprintf(sdsempty(),\"%s %lld\",option,value);\n\n    rewriteConfigRewriteLine(state,option,line,force);\n}\n\n/* Rewrite an octal option. */\nvoid rewriteConfigOctalOption(struct rewriteConfigState *state, const char *option, int value, int defvalue) {\n    int force = value != defvalue;\n    sds line = sdscatprintf(sdsempty(),\"%s %o\",option,value);\n\n    rewriteConfigRewriteLine(state,option,line,force);\n}\n\n/* Rewrite an enumeration option. It takes as usually state and option name,\n * and in addition the enumeration array and the default value for the\n * option. */\nvoid rewriteConfigEnumOption(struct rewriteConfigState *state, const char *option, int value, configEnum *ce, int defval) {\n    sds line;\n    const char *name = configEnumGetNameOrUnknown(ce,value);\n    int force = value != defval;\n\n    line = sdscatprintf(sdsempty(),\"%s %s\",option,name);\n    rewriteConfigRewriteLine(state,option,line,force);\n}\n\n/* Rewrite the save option. */\nvoid rewriteConfigSaveOption(struct rewriteConfigState *state) {\n    int j;\n    sds line;\n\n    /* In Sentinel mode we don't need to rewrite the save parameters */\n    if (g_pserver->sentinel_mode) {\n        rewriteConfigMarkAsProcessed(state,\"save\");\n        return;\n    }\n\n    /* Rewrite save parameters, or an empty 'save \"\"' line to avoid the\n     * defaults from being used.\n     */\n    if (!g_pserver->saveparamslen) {\n        rewriteConfigRewriteLine(state,\"save\",sdsnew(\"save \\\"\\\"\"),1);\n    } else {\n        for (j = 0; j < g_pserver->saveparamslen; j++) {\n            line = sdscatprintf(sdsempty(),\"save %ld %d\",\n                (long) g_pserver->saveparams[j].seconds, g_pserver->saveparams[j].changes);\n            rewriteConfigRewriteLine(state,\"save\",line,1);\n        }\n    }\n\n    /* Mark \"save\" as processed in case server.saveparamslen is zero. */\n    rewriteConfigMarkAsProcessed(state,\"save\");\n}\n\n/* Rewrite the user option. */\nvoid rewriteConfigUserOption(struct rewriteConfigState *state) {\n    /* If there is a user file defined we just mark this configuration\n     * directive as processed, so that all the lines containing users\n     * inside the config file gets discarded. */\n    if (g_pserver->acl_filename[0] != '\\0') {\n        rewriteConfigMarkAsProcessed(state,\"user\");\n        return;\n    }\n\n    /* Otherwise scan the list of users and rewrite every line. Note that\n     * in case the list here is empty, the effect will just be to comment\n     * all the users directive inside the config file. */\n    raxIterator ri;\n    raxStart(&ri,Users);\n    raxSeek(&ri,\"^\",NULL,0);\n    while(raxNext(&ri)) {\n        user *u = (user*)ri.data;\n        sds line = sdsnew(\"user \");\n        line = sdscatsds(line,u->name);\n        line = sdscatlen(line,\" \",1);\n        sds descr = ACLDescribeUser(u);\n        line = sdscatsds(line,descr);\n        sdsfree(descr);\n        rewriteConfigRewriteLine(state,\"user\",line,1);\n    }\n    raxStop(&ri);\n\n    /* Mark \"user\" as processed in case there are no defined users. */\n    rewriteConfigMarkAsProcessed(state,\"user\");\n}\n\n/* Rewrite the dir option, always using absolute paths.*/\nvoid rewriteConfigDirOption(struct rewriteConfigState *state) {\n    char cwd[1024];\n\n    if (getcwd(cwd,sizeof(cwd)) == NULL) {\n        rewriteConfigMarkAsProcessed(state,\"dir\");\n        return; /* no rewrite on error. */\n    }\n    rewriteConfigStringOption(state,\"dir\",cwd,NULL);\n}\n\n/* Rewrite the slaveof option. */\nvoid rewriteConfigSlaveofOption(struct rewriteConfigState *state, const char *option) {\n    /* If this is a master, we want all the slaveof config options\n    * in the file to be removed. Note that if this is a cluster instance\n    * we don't want a slaveof directive inside keydb.conf. */\n    if (g_pserver->cluster_enabled || listLength(g_pserver->masters) == 0) {\n        rewriteConfigMarkAsProcessed(state,option);\n        return;\n    }\n\n    listIter li;\n    listNode *ln;\n    listRewind(g_pserver->masters, &li);\n    while ((ln = listNext(&li)))\n    {\n        struct redisMaster *mi = (struct redisMaster*)listNodeValue(ln);\n        sds line;\n\n        line = sdscatprintf(sdsempty(),\"%s %s %d\", option,\n            mi->masterhost, mi->masterport);\n        rewriteConfigRewriteLine(state,option,line,1);\n    }\n}\n\n/* Rewrite the notify-keyspace-events option. */\nvoid rewriteConfigNotifykeyspaceeventsOption(struct rewriteConfigState *state) {\n    int force = g_pserver->notify_keyspace_events != 0;\n    const char *option = \"notify-keyspace-events\";\n    sds line, flags;\n\n    flags = keyspaceEventsFlagsToString(g_pserver->notify_keyspace_events);\n    line = sdsnew(option);\n    line = sdscatlen(line, \" \", 1);\n    line = sdscatrepr(line, flags, sdslen(flags));\n    sdsfree(flags);\n    rewriteConfigRewriteLine(state,option,line,force);\n}\n\n/* Rewrite the client-output-buffer-limit option. */\nvoid rewriteConfigClientoutputbufferlimitOption(struct rewriteConfigState *state) {\n    int j;\n    const char *option = \"client-output-buffer-limit\";\n\n    for (j = 0; j < CLIENT_TYPE_OBUF_COUNT; j++) {\n        int force = (cserver.client_obuf_limits[j].hard_limit_bytes !=\n                    clientBufferLimitsDefaults[j].hard_limit_bytes) ||\n                    (cserver.client_obuf_limits[j].soft_limit_bytes !=\n                    clientBufferLimitsDefaults[j].soft_limit_bytes) ||\n                    (cserver.client_obuf_limits[j].soft_limit_seconds !=\n                    clientBufferLimitsDefaults[j].soft_limit_seconds);\n        sds line;\n        char hard[64], soft[64];\n\n        rewriteConfigFormatMemory(hard,sizeof(hard),\n                cserver.client_obuf_limits[j].hard_limit_bytes);\n        rewriteConfigFormatMemory(soft,sizeof(soft),\n                cserver.client_obuf_limits[j].soft_limit_bytes);\n\n        const char *tname = getClientTypeName(j);\n        if (!strcmp(tname,\"slave\")) tname = \"replica\";\n        line = sdscatprintf(sdsempty(),\"%s %s %s %s %ld\",\n                option, tname, hard, soft,\n                (long) cserver.client_obuf_limits[j].soft_limit_seconds);\n        rewriteConfigRewriteLine(state,option,line,force);\n    }\n}\n\n/* Rewrite the oom-score-adj-values option. */\nvoid rewriteConfigOOMScoreAdjValuesOption(struct rewriteConfigState *state) {\n    int force = 0;\n    int j;\n    const char *option = \"oom-score-adj-values\";\n    sds line;\n\n    line = sdsnew(option);\n    line = sdscatlen(line, \" \", 1);\n    for (j = 0; j < CONFIG_OOM_COUNT; j++) {\n        if (g_pserver->oom_score_adj_values[j] != configOOMScoreAdjValuesDefaults[j])\n            force = 1;\n\n        line = sdscatprintf(line, \"%d\", g_pserver->oom_score_adj_values[j]);\n        if (j+1 != CONFIG_OOM_COUNT)\n            line = sdscatlen(line, \" \", 1);\n    }\n    rewriteConfigRewriteLine(state,option,line,force);\n}\n\n/* Rewrite the bind option. */\nvoid rewriteConfigBindOption(struct rewriteConfigState *state) {\n    int force = 1;\n    sds line, addresses;\n    const char *option = \"bind\";\n\n    /* Nothing to rewrite if we don't have bind addresses. */\n    if (g_pserver->bindaddr_count == 0) {\n        rewriteConfigMarkAsProcessed(state,option);\n        return;\n    }\n\n    /* Rewrite as bind <addr1> <addr2> ... <addrN> */\n    addresses = sdsjoin(g_pserver->bindaddr,g_pserver->bindaddr_count,\" \");\n    line = sdsnew(option);\n    line = sdscatlen(line, \" \", 1);\n    line = sdscatsds(line, addresses);\n    sdsfree(addresses);\n\n    rewriteConfigRewriteLine(state,option,line,force);\n}\n\n/* Glue together the configuration lines in the current configuration\n * rewrite state into a single string, stripping multiple empty lines. */\nsds rewriteConfigGetContentFromState(struct rewriteConfigState *state) {\n    sds content = sdsempty();\n    int j, was_empty = 0;\n\n    for (j = 0; j < state->numlines; j++) {\n        /* Every cluster of empty lines is turned into a single empty line. */\n        if (sdslen(state->lines[j]) == 0) {\n            if (was_empty) continue;\n            was_empty = 1;\n        } else {\n            was_empty = 0;\n        }\n        content = sdscatsds(content,state->lines[j]);\n        content = sdscatlen(content,\"\\n\",1);\n    }\n    return content;\n}\n\n/* Free the configuration rewrite state. */\nvoid rewriteConfigReleaseState(struct rewriteConfigState *state) {\n    sdsfreesplitres(state->lines,state->numlines);\n    dictRelease(state->option_to_line);\n    dictRelease(state->rewritten);\n    zfree(state);\n}\n\n/* At the end of the rewrite process the state contains the remaining\n * map between \"option name\" => \"lines in the original config file\".\n * Lines used by the rewrite process were removed by the function\n * rewriteConfigRewriteLine(), all the other lines are \"orphaned\" and\n * should be replaced by empty lines.\n *\n * This function does just this, iterating all the option names and\n * blanking all the lines still associated. */\nvoid rewriteConfigRemoveOrphaned(struct rewriteConfigState *state) {\n    dictIterator *di = dictGetIterator(state->option_to_line);\n    dictEntry *de;\n\n    while((de = dictNext(di)) != NULL) {\n        list *l = (list*)dictGetVal(de);\n        sds option = (sds)dictGetKey(de);\n\n        /* Don't blank lines about options the rewrite process\n         * don't understand. */\n        if (dictFind(state->rewritten,option) == NULL) {\n            serverLog(LL_DEBUG,\"Not rewritten option: %s\", option);\n            continue;\n        }\n\n        while(listLength(l)) {\n            listNode *ln = listFirst(l);\n            int linenum = (long) ln->value;\n\n            sdsfree(state->lines[linenum]);\n            state->lines[linenum] = sdsempty();\n            listDelNode(l,ln);\n        }\n    }\n    dictReleaseIterator(di);\n}\n\n/* This function replaces the old configuration file with the new content\n * in an atomic manner.\n *\n * The function returns 0 on success, otherwise -1 is returned and errno\n * is set accordingly. */\nint rewriteConfigOverwriteFile(char *configfile, sds content) {\n    int fd = -1;\n    int retval = -1;\n    char tmp_conffile[PATH_MAX];\n    const char *tmp_suffix = \".XXXXXX\";\n    size_t offset = 0;\n    ssize_t written_bytes = 0;\n\n    int tmp_path_len = snprintf(tmp_conffile, sizeof(tmp_conffile), \"%s%s\", configfile, tmp_suffix);\n    if (tmp_path_len <= 0 || (unsigned int)tmp_path_len >= sizeof(tmp_conffile)) {\n        serverLog(LL_WARNING, \"Config file full path is too long\");\n        errno = ENAMETOOLONG;\n        return retval;\n    }\n\n#ifdef _GNU_SOURCE\n    fd = mkostemp(tmp_conffile, O_CLOEXEC);\n#else\n    /* There's a theoretical chance here to leak the FD if a module thread forks & execv in the middle */\n    fd = mkstemp(tmp_conffile);\n#endif\n\n    if (fd == -1) {\n        serverLog(LL_WARNING, \"Could not create tmp config file (%s)\", strerror(errno));\n        return retval;\n    }\n\n    while (offset < sdslen(content)) {\n         written_bytes = write(fd, content + offset, sdslen(content) - offset);\n         if (written_bytes <= 0) {\n             if (errno == EINTR) continue; /* FD is blocking, no other retryable errors */\n             serverLog(LL_WARNING, \"Failed after writing (%zd) bytes to tmp config file (%s)\", offset, strerror(errno));\n             goto cleanup;\n         }\n         offset+=written_bytes;\n    }\n\n    if (fsync(fd))\n        serverLog(LL_WARNING, \"Could not sync tmp config file to disk (%s)\", strerror(errno));\n    else if (fchmod(fd, 0644 & ~g_pserver->umask) == -1)\n        serverLog(LL_WARNING, \"Could not chmod config file (%s)\", strerror(errno));\n    else if (rename(tmp_conffile, configfile) == -1)\n        serverLog(LL_WARNING, \"Could not rename tmp config file (%s)\", strerror(errno));\n    else {\n        retval = 0;\n        serverLog(LL_DEBUG, \"Rewritten config file (%s) successfully\", configfile);\n    }\n\ncleanup:\n    close(fd);\n    if (retval) unlink(tmp_conffile);\n    return retval;\n}\n\n/* Rewrite the configuration file at \"path\".\n * If the configuration file already exists, we try at best to retain comments\n * and overall structure.\n *\n * Configuration parameters that are at their default value, unless already\n * explicitly included in the old configuration file, are not rewritten.\n * The force_all flag overrides this behavior and forces everything to be\n * written. This is currently only used for testing purposes.\n *\n * On error -1 is returned and errno is set accordingly, otherwise 0. */\nint rewriteConfig(char *path, int force_all) {\n    struct rewriteConfigState *state;\n    sds newcontent;\n    int retval;\n\n    /* Step 1: read the old config into our rewrite state. */\n    if ((state = rewriteConfigReadOldFile(path)) == NULL) return -1;\n    if (force_all) state->force_all = 1;\n\n    /* Step 2: rewrite every single option, replacing or appending it inside\n     * the rewrite state. */\n\n    /* Iterate the configs that are standard */\n    for (standardConfig *config = configs; config->name != NULL; config++) {\n        config->interface.rewrite(config->data, config->name, state);\n    }\n\n    rewriteConfigBindOption(state);\n    rewriteConfigOctalOption(state,\"unixsocketperm\",g_pserver->unixsocketperm,CONFIG_DEFAULT_UNIX_SOCKET_PERM);\n    rewriteConfigStringOption(state,\"logfile\",g_pserver->logfile,CONFIG_DEFAULT_LOGFILE);\n    rewriteConfigSaveOption(state);\n    rewriteConfigUserOption(state);\n    rewriteConfigDirOption(state);\n    rewriteConfigSlaveofOption(state,\"replicaof\");\n    rewriteConfigStringOption(state,\"cluster-config-file\",g_pserver->cluster_configfile,CONFIG_DEFAULT_CLUSTER_CONFIG_FILE);\n    rewriteConfigNotifykeyspaceeventsOption(state);\n    rewriteConfigClientoutputbufferlimitOption(state);\n    rewriteConfigYesNoOption(state,\"active-replica\",g_pserver->fActiveReplica,CONFIG_DEFAULT_ACTIVE_REPLICA);\n    rewriteConfigStringOption(state, \"version-override\",KEYDB_SET_VERSION,KEYDB_REAL_VERSION);\n    rewriteConfigOOMScoreAdjValuesOption(state);\n\n    if (!g_pserver->tls_allowlist.empty()) {\n        sds conf = sdsnew(\"tls-allowlist \");\n        for (auto &elem : g_pserver->tls_allowlist) {\n            conf = sdscatsds(conf, (sds)elem.get());\n            conf = sdscat(conf, \" \");\n        }\n        // trim the trailing space\n        sdsrange(conf, 0, -1);\n        rewriteConfigRewriteLine(state,\"tls-allowlist\",conf,1 /*force*/);\n        // note: conf is owned by rewriteConfigRewriteLine - no need to free\n    } else {\n        rewriteConfigMarkAsProcessed(state, \"tls-allowlist\"); // ensure the line is removed if it existed\n    }\n\n    /* Rewrite Sentinel config if in Sentinel mode. */\n    if (g_pserver->sentinel_mode) rewriteConfigSentinelOption(state);\n\n    /* Step 3: remove all the orphaned lines in the old file, that is, lines\n     * that were used by a config option and are no longer used, like in case\n     * of multiple \"save\" options or duplicated options. */\n    rewriteConfigRemoveOrphaned(state);\n\n    /* Step 4: generate a new configuration file from the modified state\n     * and write it into the original file. */\n    newcontent = rewriteConfigGetContentFromState(state);\n    retval = rewriteConfigOverwriteFile(cserver.configfile,newcontent);\n\n    sdsfree(newcontent);\n    rewriteConfigReleaseState(state);\n    return retval;\n}\n\n/*-----------------------------------------------------------------------------\n * Configs that fit one of the major types and require no special handling\n *----------------------------------------------------------------------------*/\n#define LOADBUF_SIZE 256\nstatic char loadbuf[LOADBUF_SIZE];\n\n#define embedCommonConfig(config_name, config_alias, config_flags) \\\n    config_name, config_alias, config_flags,\n\n#define embedConfigInterface(initfn, setfn, getfn, rewritefn) { \\\n    initfn, setfn, getfn, rewritefn, \\\n},\n\n/* What follows is the generic config types that are supported. To add a new\n * config with one of these types, add it to the standardConfig table with\n * the creation macro for each type.\n *\n * Each type contains the following:\n * * A function defining how to load this type on startup.\n * * A function defining how to update this type on CONFIG SET.\n * * A function defining how to serialize this type on CONFIG SET.\n * * A function defining how to rewrite this type on CONFIG REWRITE.\n * * A Macro defining how to create this type.\n */\n\n/* Bool Configs */\nstatic void boolConfigInit(typeData data) {\n    *data.yesno.config = data.yesno.default_value;\n}\n\nstatic int boolConfigSet(typeData data, sds value, int update, const char **err) {\n    int yn = yesnotoi(value);\n    if (yn == -1) {\n        if ((yn = truefalsetoi(value)) == -1) {\n            *err = \"argument must be 'yes' or 'no'\";\n            return 0;\n        }\n    }\n    if (data.yesno.is_valid_fn && !data.yesno.is_valid_fn(yn, err))\n        return 0;\n    int prev = *(data.yesno.config);\n    *(data.yesno.config) = yn;\n    if (update && data.yesno.update_fn && !data.yesno.update_fn(yn, prev, err)) {\n        *(data.yesno.config) = prev;\n        return 0;\n    }\n    return 1;\n}\n\nstatic void boolConfigGet(client *c, typeData data) {\n    addReplyBulkCString(c, *data.yesno.config ? \"yes\" : \"no\");\n}\n\nstatic void boolConfigRewrite(typeData data, const char *name, struct rewriteConfigState *state) {\n    rewriteConfigYesNoOption(state, name,*(data.yesno.config), data.yesno.default_value);\n}\n\nconstexpr standardConfig createBoolConfig(const char *name, const char *alias, unsigned flags, int &config_addr, int defaultValue, int (*is_valid)(int val, const char **err), int (*update)(int val, int prev, const char **err))\n{\n    standardConfig conf = {\n        embedCommonConfig(name, alias, flags)\n        { boolConfigInit, boolConfigSet, boolConfigGet, boolConfigRewrite }\n    };\n    conf.data.yesno.config = &config_addr;\n    conf.data.yesno.default_value = defaultValue;\n    conf.data.yesno.is_valid_fn = is_valid;\n    conf.data.yesno.update_fn = update;\n    return conf;\n}\n\n/* String Configs */\nstatic void stringConfigInit(typeData data) {\n    *data.string.config = (data.string.convert_empty_to_null && !data.string.default_value) ? NULL : zstrdup(data.string.default_value);\n}\n\nstatic int stringConfigSet(typeData data, sds value, int update, const char **err) {\n    if (data.string.is_valid_fn && !data.string.is_valid_fn(value, err))\n        return 0;\n    char *prev = *data.string.config;\n    *data.string.config = (data.string.convert_empty_to_null && !value[0]) ? NULL : zstrdup(value);\n    if (update && data.string.update_fn && !data.string.update_fn(*data.string.config, prev, err)) {\n        zfree(*data.string.config);\n        *data.string.config = prev;\n        return 0;\n    }\n    zfree(prev);\n    return 1;\n}\n\nstatic void stringConfigGet(client *c, typeData data) {\n    addReplyBulkCString(c, *data.string.config ? *data.string.config : \"\");\n}\n\nstatic void stringConfigRewrite(typeData data, const char *name, struct rewriteConfigState *state) {\n    rewriteConfigStringOption(state, name,*(data.string.config), data.string.default_value);\n}\n\n/* SDS Configs */\nstatic void sdsConfigInit(typeData data) {\n    *data.sds.config = (data.sds.convert_empty_to_null && !data.sds.default_value) ? NULL: sdsnew(data.sds.default_value);\n}\n\nstatic int sdsConfigSet(typeData data, sds value, int update, const char **err) {\n    if (data.sds.is_valid_fn && !data.sds.is_valid_fn(value, err))\n        return 0;\n    sds prev = *data.sds.config;\n    *data.sds.config = (data.sds.convert_empty_to_null && (sdslen(value) == 0)) ? NULL : sdsdup(value);\n    if (update && data.sds.update_fn && !data.sds.update_fn(*data.sds.config, prev, err)) {\n        sdsfree(*data.sds.config);\n        *data.sds.config = prev;\n        return 0;\n    }\n    sdsfree(prev);\n    return 1;\n}\n\nstatic void sdsConfigGet(client *c, typeData data) {\n    if (*data.sds.config) {\n        addReplyBulkSds(c, sdsdup(*data.sds.config));\n    } else {\n        addReplyBulkCString(c, \"\");\n    }\n}\n\nstatic void sdsConfigRewrite(typeData data, const char *name, struct rewriteConfigState *state) {\n    sds sdsDefault = data.sds.default_value ? sdsnew(data.sds.default_value) : NULL;\n    rewriteConfigSdsOption(state, name, *(data.sds.config), sdsDefault);\n    if (sdsDefault)\n        sdsfree(sdsDefault);\n}\n\n\n#define ALLOW_EMPTY_STRING 0\n#define EMPTY_STRING_IS_NULL 1\n\nconstexpr standardConfig createStringConfig(const char *name, const char *alias, unsigned flags, int empty_to_null, char *&config_addr, const char *defaultValue, int (*is_valid)(char*,const char**), int (*update)(char*,char*,const char**)) {\n    standardConfig conf = {\n        embedCommonConfig(name, alias, flags)\n        embedConfigInterface(stringConfigInit, stringConfigSet, stringConfigGet, stringConfigRewrite)\n    };\n    conf.data.string = {\n        &(config_addr),\n        (defaultValue),\n        (is_valid),\n        (update),\n        (empty_to_null),\n    };\n    return conf;\n}\n\nconstexpr standardConfig createSDSConfig(const char *name, const char *alias, unsigned flags, int empty_to_null, sds &config_addr, const char *defaultValue, int (*is_valid)(char*,const char**), int (*update)(char*,char*,const char**)) {\n    standardConfig conf = {\n        embedCommonConfig(name, alias, flags)\n        embedConfigInterface(sdsConfigInit, sdsConfigSet, sdsConfigGet, sdsConfigRewrite)\n    };\n    conf.data.sds = {\n        &(config_addr),\n        (defaultValue),\n        (is_valid),\n        (update),\n        (empty_to_null),\n    };\n    return conf;\n}\n\n/* Enum configs */\nstatic void enumConfigInit(typeData data) {\n    *data.enumd.config = data.enumd.default_value;\n}\n\nstatic int enumConfigSet(typeData data, sds value, int update, const char **err) {\n    int enumval = configEnumGetValue(data.enumd.enum_value, value);\n    if (enumval == INT_MIN) {\n        sds enumerr = sdsnew(\"argument must be one of the following: \");\n        configEnum *enumNode = data.enumd.enum_value;\n        while(enumNode->name != NULL) {\n            enumerr = sdscatlen(enumerr, enumNode->name,\n                                strlen(enumNode->name));\n            enumerr = sdscatlen(enumerr, \", \", 2);\n            enumNode++;\n        }\n        sdsrange(enumerr,0,-3); /* Remove final \", \". */\n\n        strncpy(loadbuf, enumerr, LOADBUF_SIZE);\n        loadbuf[LOADBUF_SIZE - 1] = '\\0';\n\n        sdsfree(enumerr);\n        *err = loadbuf;\n        return 0;\n    }\n    if (data.enumd.is_valid_fn && !data.enumd.is_valid_fn(enumval, err))\n        return 0;\n    int prev = *(data.enumd.config);\n    *(data.enumd.config) = enumval;\n    if (update && data.enumd.update_fn && !data.enumd.update_fn(enumval, prev, err)) {\n        *(data.enumd.config) = prev;\n        return 0;\n    }\n    return 1;\n}\n\nstatic void enumConfigGet(client *c, typeData data) {\n    addReplyBulkCString(c, configEnumGetNameOrUnknown(data.enumd.enum_value,*data.enumd.config));\n}\n\nstatic void enumConfigRewrite(typeData data, const char *name, struct rewriteConfigState *state) {\n    rewriteConfigEnumOption(state, name,*(data.enumd.config), data.enumd.enum_value, data.enumd.default_value);\n}\n\nconstexpr standardConfig createEnumConfig(const char *name, const char *alias, unsigned flags, configEnum *enumVal, int &config_addr, int defaultValue, int (*is_valid)(int,const char**), int (*update)(int,int,const char**)) {\n    standardConfig c = {\n        embedCommonConfig(name, alias, flags)\n        embedConfigInterface(enumConfigInit, enumConfigSet, enumConfigGet, enumConfigRewrite)\n    };\n    c.data.enumd = {\n        &(config_addr),\n        (enumVal),\n        (defaultValue),\n        (is_valid),\n        (update),\n    };\n\n    return c;\n}\n\n/* Gets a 'long long val' and sets it into the union, using a macro to get\n * compile time type check. */\n#define SET_NUMERIC_TYPE(val) \\\n    if (data.numeric.numeric_type == NUMERIC_TYPE_INT) { \\\n        *(data.numeric.config.i) = (int) val; \\\n    } else if (data.numeric.numeric_type == NUMERIC_TYPE_UINT) { \\\n        *(data.numeric.config.ui) = (unsigned int) val; \\\n    } else if (data.numeric.numeric_type == NUMERIC_TYPE_LONG) { \\\n        *(data.numeric.config.l) = (long) val; \\\n    } else if (data.numeric.numeric_type == NUMERIC_TYPE_ULONG) { \\\n        *(data.numeric.config.ul) = (unsigned long) val; \\\n    } else if (data.numeric.numeric_type == NUMERIC_TYPE_LONG_LONG) { \\\n        *(data.numeric.config.ll) = (long long) val; \\\n    } else if (data.numeric.numeric_type == NUMERIC_TYPE_ULONG_LONG) { \\\n        *(data.numeric.config.ull) = (unsigned long long) val; \\\n    } else if (data.numeric.numeric_type == NUMERIC_TYPE_SIZE_T) { \\\n        *(data.numeric.config.st) = (size_t) val; \\\n    } else if (data.numeric.numeric_type == NUMERIC_TYPE_SSIZE_T) { \\\n        *(data.numeric.config.sst) = (ssize_t) val; \\\n    } else if (data.numeric.numeric_type == NUMERIC_TYPE_OFF_T) { \\\n        *(data.numeric.config.ot) = (off_t) val; \\\n    } else if (data.numeric.numeric_type == NUMERIC_TYPE_TIME_T) { \\\n        *(data.numeric.config.tt) = (time_t) val; \\\n    }\n\n/* Gets a 'long long val' and sets it with the value from the union, using a\n * macro to get compile time type check. */\n#define GET_NUMERIC_TYPE(val) \\\n    if (data.numeric.numeric_type == NUMERIC_TYPE_INT) { \\\n        val = *(data.numeric.config.i); \\\n    } else if (data.numeric.numeric_type == NUMERIC_TYPE_UINT) { \\\n        val = *(data.numeric.config.ui); \\\n    } else if (data.numeric.numeric_type == NUMERIC_TYPE_LONG) { \\\n        val = *(data.numeric.config.l); \\\n    } else if (data.numeric.numeric_type == NUMERIC_TYPE_ULONG) { \\\n        val = *(data.numeric.config.ul); \\\n    } else if (data.numeric.numeric_type == NUMERIC_TYPE_LONG_LONG) { \\\n        val = *(data.numeric.config.ll); \\\n    } else if (data.numeric.numeric_type == NUMERIC_TYPE_ULONG_LONG) { \\\n        val = *(data.numeric.config.ull); \\\n    } else if (data.numeric.numeric_type == NUMERIC_TYPE_SIZE_T) { \\\n        val = *(data.numeric.config.st); \\\n    } else if (data.numeric.numeric_type == NUMERIC_TYPE_SSIZE_T) { \\\n        val = *(data.numeric.config.sst); \\\n    } else if (data.numeric.numeric_type == NUMERIC_TYPE_OFF_T) { \\\n        val = *(data.numeric.config.ot); \\\n    } else if (data.numeric.numeric_type == NUMERIC_TYPE_TIME_T) { \\\n        val = *(data.numeric.config.tt); \\\n    }\n\n/* Numeric configs */\nstatic void numericConfigInit(typeData data) {\n    SET_NUMERIC_TYPE(data.numeric.default_value)\n}\n\nstatic int numericBoundaryCheck(typeData data, long long ll, const char **err) {\n    if (data.numeric.numeric_type == NUMERIC_TYPE_ULONG_LONG ||\n        data.numeric.numeric_type == NUMERIC_TYPE_UINT ||\n        data.numeric.numeric_type == NUMERIC_TYPE_SIZE_T) {\n        /* Boundary check for unsigned types */\n        unsigned long long ull = ll;\n        unsigned long long upper_bound = data.numeric.upper_bound;\n        unsigned long long lower_bound = data.numeric.lower_bound;\n        if (ull > upper_bound || ull < lower_bound) {\n            snprintf(loadbuf, LOADBUF_SIZE,\n                \"argument must be between %llu and %llu inclusive\",\n                lower_bound,\n                upper_bound);\n            *err = loadbuf;\n            return 0;\n        }\n    } else {\n        /* Boundary check for signed types */\n        if (ll > data.numeric.upper_bound || ll < data.numeric.lower_bound) {\n            snprintf(loadbuf, LOADBUF_SIZE,\n                \"argument must be between %lld and %lld inclusive\",\n                data.numeric.lower_bound,\n                data.numeric.upper_bound);\n            *err = loadbuf;\n            return 0;\n        }\n    }\n    return 1;\n}\n\nstatic int numericConfigSet(typeData data, sds value, int update, const char **err) {\n    long long ll, prev = 0;\n    if (data.numeric.is_memory) {\n        int memerr;\n        ll = memtoll(value, &memerr);\n        if (memerr || ll < 0) {\n            *err = \"argument must be a memory value\";\n            return 0;\n        }\n    } else {\n        if (!string2ll(value, sdslen(value),&ll)) {\n            *err = \"argument couldn't be parsed into an integer\" ;\n            return 0;\n        }\n    }\n\n    if (!numericBoundaryCheck(data, ll, err))\n        return 0;\n\n    if (data.numeric.is_valid_fn && !data.numeric.is_valid_fn(ll, err))\n        return 0;\n\n    GET_NUMERIC_TYPE(prev)\n    SET_NUMERIC_TYPE(ll)\n\n    if (update && data.numeric.update_fn && !data.numeric.update_fn(ll, prev, err)) {\n        SET_NUMERIC_TYPE(prev)\n        return 0;\n    }\n    return 1;\n}\n\nstatic void numericConfigGet(client *c, typeData data) {\n    char buf[128];\n    long long value = 0;\n\n    GET_NUMERIC_TYPE(value)\n\n    ll2string(buf, sizeof(buf), value);\n    addReplyBulkCString(c, buf);\n}\n\nstatic void numericConfigRewrite(typeData data, const char *name, struct rewriteConfigState *state) {\n    long long value = 0;\n\n    GET_NUMERIC_TYPE(value)\n\n    if (data.numeric.is_memory) {\n        rewriteConfigBytesOption(state, name, value, data.numeric.default_value);\n    } else {\n        rewriteConfigNumericalOption(state, name, value, data.numeric.default_value);\n    }\n}\n\n#define INTEGER_CONFIG 0\n#define MEMORY_CONFIG 1\n\nconstexpr standardConfig embedCommonNumericalConfig(const char *name, const char *alias, unsigned flags, long long lower, long long upper, long long defaultValue, int memory, int (*is_valid)(long long, const char**), int (*update)(long long, long long, const char**)) {\n    standardConfig conf = {\n        embedCommonConfig(name, alias, flags)\n        embedConfigInterface(numericConfigInit, numericConfigSet, numericConfigGet, numericConfigRewrite)\n    };\n    conf.data.numeric.is_memory = (memory);\n    conf.data.numeric.lower_bound = (lower);\n    conf.data.numeric.upper_bound = (upper);\n    conf.data.numeric.default_value = (defaultValue);\n    conf.data.numeric.is_valid_fn = (is_valid);\n    conf.data.numeric.update_fn = (update);\n    return conf;\n}\n\nconstexpr standardConfig createIntConfig(const char *name, const char *alias, unsigned flags, long long lower, long long upper, int &config_addr, long long defaultValue, int memory, int (*is_valid)(long long, const char**), int (*update)(long long, long long, const char**))\n{\n    standardConfig conf =  embedCommonNumericalConfig(name, alias, flags, lower, upper, defaultValue, memory, is_valid, update);\n    conf.data.numeric.numeric_type = NUMERIC_TYPE_INT;\n    conf.data.numeric.config.i = &config_addr;\n    return conf;\n}\n\nconstexpr standardConfig createUIntConfig(const char *name, const char *alias, unsigned flags, long long lower, long long upper, unsigned int &config_addr, long long defaultValue, int memory, int (*is_valid)(long long, const char**), int (*update)(long long, long long, const char**))\n{\n    auto conf = embedCommonNumericalConfig(name, alias, flags, lower, upper, defaultValue, memory, is_valid, update);\n    conf.data.numeric.numeric_type = NUMERIC_TYPE_UINT;\n    conf.data.numeric.config.ui = &(config_addr);\n    return conf;\n}\n\nconstexpr standardConfig createLongConfig(const char *name, const char *alias, unsigned flags, long long lower, long long upper, long &config_addr, long long defaultValue, int memory, int (*is_valid)(long long, const char**), int (*update)(long long, long long, const char**)) {\n    auto conf = embedCommonNumericalConfig(name, alias, flags, lower, upper, defaultValue, memory, is_valid, update);\n    conf.data.numeric.numeric_type = NUMERIC_TYPE_LONG;\n    conf.data.numeric.config.l = &(config_addr);\n    return conf;\n}\n\nconstexpr standardConfig createULongConfig(const char *name, const char *alias, unsigned flags, long long lower, long long upper, unsigned long &config_addr, long long defaultValue, int memory, int (*is_valid)(long long, const char**), int (*update)(long long, long long, const char**)) {\n    auto conf = embedCommonNumericalConfig(name, alias, flags, lower, upper, defaultValue, memory, is_valid, update);\n    conf.data.numeric.numeric_type = NUMERIC_TYPE_ULONG;\n    conf.data.numeric.config.ul = &(config_addr);\n    return conf;\n}\n\nconstexpr standardConfig createLongLongConfig(const char *name, const char *alias, unsigned flags, long long lower, long long upper, long long &config_addr, long long defaultValue, int memory, int (*is_valid)(long long, const char**), int (*update)(long long, long long, const char**)) {\n    auto conf = embedCommonNumericalConfig(name, alias, flags, lower, upper, defaultValue, memory, is_valid, update);\n    conf.data.numeric.numeric_type = NUMERIC_TYPE_LONG_LONG;\n    conf.data.numeric.config.ll = &(config_addr);\n    return conf;\n}\n\nconstexpr standardConfig createULongLongConfig(const char *name, const char *alias, unsigned flags, long long lower, long long upper, unsigned long long &config_addr, long long defaultValue, int memory, int (*is_valid)(long long, const char**), int (*update)(long long, long long, const char**)) {\n    auto conf = embedCommonNumericalConfig(name, alias, flags, lower, upper, defaultValue, memory, is_valid, update);\n    conf.data.numeric.numeric_type = NUMERIC_TYPE_ULONG_LONG;\n    conf.data.numeric.config.ull = &(config_addr);\n    return conf;\n}\n\nconstexpr standardConfig createSizeTConfig(const char *name, const char *alias, unsigned flags, long long lower, long long upper, size_t &config_addr, long long defaultValue, int memory, int (*is_valid)(long long, const char**), int (*update)(long long, long long, const char**)) {\n    auto conf = embedCommonNumericalConfig(name, alias, flags, lower, upper, defaultValue, memory, is_valid, update);\n    conf.data.numeric.numeric_type = NUMERIC_TYPE_SIZE_T;\n    conf.data.numeric.config.st = &(config_addr);\n    return conf;\n}\n\nconstexpr standardConfig createSSizeTConfig(const char *name, const char *alias, unsigned flags, long long lower, long long upper, ssize_t &config_addr, long long defaultValue, int memory, int (*is_valid)(long long, const char**), int (*update)(long long, long long, const char**)) {\n    auto conf = embedCommonNumericalConfig(name, alias, flags, lower, upper, defaultValue, memory, is_valid, update);\n    conf.data.numeric.numeric_type = NUMERIC_TYPE_SSIZE_T;\n    conf.data.numeric.config.sst = &(config_addr);\n    return conf;\n}\n\nconstexpr standardConfig createTimeTConfig(const char *name, const char *alias, unsigned flags, long long lower, long long upper, time_t &config_addr, long long defaultValue, int memory, int (*is_valid)(long long, const char**), int (*update)(long long, long long, const char**)) {\n    auto conf = embedCommonNumericalConfig(name, alias, flags, lower, upper, defaultValue, memory, is_valid, update);\n    conf.data.numeric.numeric_type = NUMERIC_TYPE_TIME_T;\n    conf.data.numeric.config.tt = &(config_addr);\n    return conf;\n}\n\nconstexpr standardConfig createOffTConfig(const char *name, const char *alias, unsigned flags, long long lower, long long upper, off_t &config_addr, long long defaultValue, int memory, int (*is_valid)(long long, const char**), int (*update)(long long, long long, const char**)) {\n    auto conf = embedCommonNumericalConfig(name, alias, flags, lower, upper, defaultValue, memory, is_valid, update);\n    conf.data.numeric.numeric_type = NUMERIC_TYPE_OFF_T;\n    conf.data.numeric.config.ot = &(config_addr);\n    return conf;\n}\n\nstatic int isValidActiveDefrag(int val, const char **err) {\n#ifndef HAVE_DEFRAG\n    if (val) {\n        *err = \"Active defragmentation cannot be enabled: it \"\n               \"requires a KeyDB server compiled with a modified Jemalloc \"\n               \"like the one shipped by default with the KeyDB source \"\n               \"distribution\";\n        return 0;\n    }\n#else\n    UNUSED(val);\n    UNUSED(err);\n#endif\n    return 1;\n}\n\nstatic int isValidDBfilename(char *val, const char **err) {\n    if (!pathIsBaseName(val)) {\n        *err = \"dbfilename can't be a path, just a filename\";\n        return 0;\n    }\n    return 1;\n}\n\nstatic int isValidAOFfilename(char *val, const char **err) {\n    if (!pathIsBaseName(val)) {\n        *err = \"appendfilename can't be a path, just a filename\";\n        return 0;\n    }\n    return 1;\n}\n\nstatic int isValidS3Bucket(char *s3bucket, const char **err) {\n    int status = EXIT_FAILURE;\n    pid_t pid = fork();\n    if (pid < 0)\n    {\n        *err = \"couldn't fork to call aws cli\";\n        return 0;\n    }\n\n    if (pid == 0)\n    {\n        execlp(\"aws\", \"aws\", \"s3\", \"ls\", s3bucket, nullptr);\n        exit(EXIT_FAILURE);\n    }\n    else \n    {\n        waitpid(pid, &status, 0);\n    }\n\n    if (status != EXIT_SUCCESS) {\n        *err = \"could not access s3 bucket\";\n        return 0;\n    }\n    return 1;\n}\n\n/* Validate specified string is a valid proc-title-template */\nstatic int isValidProcTitleTemplate(char *val, const char **err) {\n    if (!validateProcTitleTemplate(val)) {\n        *err = \"template format is invalid or contains unknown variables\";\n        return 0;\n    }\n    return 1;\n}\n\nstatic int updateProcTitleTemplate(char *val, char *prev, const char **err) {\n    UNUSED(val);\n    UNUSED(prev);\n    if (redisSetProcTitle(NULL) == C_ERR) {\n        *err = \"failed to set process title\";\n        return 0;\n    }\n    return 1;\n}\n\nstatic int updateHZ(long long val, long long prev, const char **err) {\n    UNUSED(prev);\n    UNUSED(err);\n    /* Hz is more a hint from the user, so we accept values out of range\n     * but cap them to reasonable values. */\n    g_pserver->config_hz = val;\n    if (g_pserver->config_hz < CONFIG_MIN_HZ) g_pserver->config_hz = CONFIG_MIN_HZ;\n    if (g_pserver->config_hz > CONFIG_MAX_HZ) g_pserver->config_hz = CONFIG_MAX_HZ;\n    g_pserver->hz = g_pserver->config_hz;\n    return 1;\n}\n\nstatic int updatePort(long long val, long long prev, const char **err) {\n    /* Do nothing if port is unchanged */\n    if (val == prev) {\n        return 1;\n    }\n\n    // Run this thread to make sure its valid\n    if (changeListenPort(val, &serverTL->ipfd, acceptTcpHandler, true) == C_ERR) {\n        *err = \"Unable to listen on this port. Check server logs.\";\n        return 0;\n    }\n\n    // Now run the config change on the other threads\n    for (int ithread = 0; ithread < cserver.cthreads; ++ithread) {\n        if (&g_pserver->rgthreadvar[ithread] != serverTL) {\n            aePostFunction(g_pserver->rgthreadvar[ithread].el, [val]{\n                if (changeListenPort(val, &serverTL->ipfd, acceptTcpHandler, false) == C_ERR) {\n                    serverLog(LL_WARNING, \"Failed to change the listen port for a thread.  Server will still be listening on old ports.\");\n                }\n            });\n        }\n    }\n\n    return 1;\n}\n\nstatic int updateJemallocBgThread(int val, int prev, const char **err) {\n    UNUSED(prev);\n    UNUSED(err);\n    set_jemalloc_bg_thread(val);\n    return 1;\n}\n\nstatic int updateReplBacklogSize(long long val, long long prev, const char **err) {\n    /* resizeReplicationBacklog sets g_pserver->repl_backlog_size, and relies on\n     * being able to tell when the size changes, so restore prev before calling it. */\n    if (cserver.repl_backlog_disk_size) {\n        *err = \"Unable to dynamically resize the backlog because disk backlog is enabled\";\n        return 0;\n    }\n    g_pserver->repl_backlog_size = prev;\n    g_pserver->repl_backlog_config_size = val;\n    resizeReplicationBacklog(val);\n    return 1;\n}\n\nstatic int updateMaxmemory(long long val, long long prev, const char **err) {\n    UNUSED(prev);\n    UNUSED(err);\n    if (val) {\n        size_t used = zmalloc_used_memory()-freeMemoryGetNotCountedMemory();\n        if ((unsigned long long)val < used) {\n            serverLog(LL_WARNING,\"WARNING: the new maxmemory value set via CONFIG SET (%llu) is smaller than the current memory usage (%zu). This will result in key eviction and/or the inability to accept new write commands depending on the maxmemory-policy.\", g_pserver->maxmemory, used);\n        }\n        performEvictions(false /*fPreSnapshot*/);\n    }\n    return 1;\n}\n\nstatic int updateFlashMaxmemory(long long val, long long prev, const char **err) {\n    UNUSED(prev);\n    UNUSED(err);\n    if (val && g_pserver->m_pstorageFactory) {\n        size_t used = g_pserver->m_pstorageFactory->totalDiskspaceUsed();\n        if ((unsigned long long)val < used) {\n            serverLog(LL_WARNING,\"WARNING: the new maxstorage value set via CONFIG SET (%llu) is smaller than the current storage usage (%zu). This will result in key eviction and/or the inability to accept new write commands depending on the maxmemory-policy.\", g_pserver->maxstorage, used);\n        }\n        performEvictions(false /*fPreSnapshot*/);\n    }\n    return 1;\n}\n\nstatic int updateGoodSlaves(long long val, long long prev, const char **err) {\n    UNUSED(val);\n    UNUSED(prev);\n    UNUSED(err);\n    refreshGoodSlavesCount();\n    return 1;\n}\n\nstatic int updateMasterAuthConfig(sds, sds, const char **) {\n    updateMasterAuth();\n    return 1;\n}\n\nstatic int updateAppendonly(int val, int prev, const char **err) {\n    UNUSED(prev);\n    if (val == 0 && g_pserver->aof_state != AOF_OFF) {\n        stopAppendOnly();\n    } else if (val && g_pserver->aof_state == AOF_OFF) {\n        if (startAppendOnly() == C_ERR) {\n            *err = \"Unable to turn on AOF. Check server logs.\";\n            return 0;\n        }\n    }\n    return 1;\n}\n\nstatic int updateSighandlerEnabled(int val, int prev, const char **err) {\n    UNUSED(err);\n    UNUSED(prev);\n    if (val)\n        setupSignalHandlers();\n    else\n        removeSignalHandlers();\n    return 1;\n}\n\nstatic int updateMaxclients(long long val, long long prev, const char **err) {\n    /* Try to check if the OS is capable of supporting so many FDs. */\n    if (val > prev) {\n        adjustOpenFilesLimit();\n        if (g_pserver->maxclients != val) {\n            static char msg[128];\n            snprintf(msg, sizeof(msg), \"The operating system is not able to handle the specified number of clients, try with %d\", g_pserver->maxclients);\n            *err = msg;\n            if (g_pserver->maxclients > prev) {\n                g_pserver->maxclients = prev;\n                adjustOpenFilesLimit();\n            }\n            return 0;\n        }\n        /* Change the SetSize for the current thread first. If any error, return the error message to the client, \n         * otherwise, continue to do the same for other threads */\n        if ((unsigned int) aeGetSetSize(aeGetCurrentEventLoop()) <\n            g_pserver->maxclients + CONFIG_FDSET_INCR)\n        {\n            if (aeResizeSetSize(aeGetCurrentEventLoop(),\n                g_pserver->maxclients + CONFIG_FDSET_INCR) == AE_ERR)\n            {\n                *err = \"The event loop API used by KeyDB is not able to handle the specified number of clients\";\n                return 0;\n            }\n            serverLog(LL_DEBUG,\"Successfully changed the setsize for current thread %d\", ielFromEventLoop(aeGetCurrentEventLoop()));\n        }\n\n        for (int iel = 0; iel < cserver.cthreads; ++iel)\n        {\n            if (g_pserver->rgthreadvar[iel].el == aeGetCurrentEventLoop()){\n                continue;\n            }\n\n            if ((unsigned int) aeGetSetSize(g_pserver->rgthreadvar[iel].el) <\n                g_pserver->maxclients + CONFIG_FDSET_INCR)\n            {\n                int res = aePostFunction(g_pserver->rgthreadvar[iel].el, [iel] {\n                    if (aeResizeSetSize(g_pserver->rgthreadvar[iel].el, g_pserver->maxclients + CONFIG_FDSET_INCR) == AE_ERR) {\n                        serverLog(LL_WARNING,\"Failed to change the setsize for Thread %d\", iel);\n                    }\n                });\n\n                if (res != AE_OK){\n                    static char msg[128];\n                    snprintf(msg, sizeof(msg),\"Failed to post the request to change setsize for Thread %d\", iel);\n                    *err = msg;\n                    return 0;\n                }\n                serverLog(LL_DEBUG,\"Successfully post the request to change the setsize for thread %d\", iel);\n            }\n        }\n    }\n    return 1;\n}\n\nstatic int validateMultiMasterNoForward(int val, const char **) {\n    if (val) {\n        serverLog(LL_WARNING, \"WARNING: multi-master-no-forward is set, you *must* use a mesh topology or dataloss will occur\");\n    }\n    return 1;\n}\n\nstatic int updateOOMScoreAdj(int val, int prev, const char **err) {\n    UNUSED(prev);\n\n    if (val) {\n        if (setOOMScoreAdj(-1) == C_ERR) {\n            *err = \"Failed to set current oom_score_adj. Check server logs.\";\n            return 0;\n        }\n    }\n\n    return 1;\n}\n\nint updateRequirePass(sds val, sds prev, const char **err) {\n    UNUSED(prev);\n    UNUSED(err);\n    /* The old \"requirepass\" directive just translates to setting\n     * a password to the default user. The only thing we do\n     * additionally is to remember the cleartext password in this\n     * case, for backward compatibility with Redis <= 5. */\n    ACLUpdateDefaultUserPassword(val);\n    return 1;\n}\n\n#ifdef USE_OPENSSL\nstatic int updateTlsCfg(char *val, char *prev, const char **err) {\n    UNUSED(val);\n    UNUSED(prev);\n    UNUSED(err);\n\n    /* If TLS is enabled, try to configure OpenSSL. */\n    if ((g_pserver->tls_port || g_pserver->tls_replication || g_pserver->tls_cluster)\n            && tlsConfigure(&g_pserver->tls_ctx_config) == C_ERR) {\n        *err = \"Unable to update TLS configuration. Check server logs.\";\n        return 0;\n    }\n    return 1;\n}\nstatic int updateTlsCfgBool(int val, int prev, const char **err) {\n    UNUSED(val);\n    UNUSED(prev);\n    return updateTlsCfg(NULL, NULL, err);\n}\n\nstatic int updateTlsCfgInt(long long val, long long prev, const char **err) {\n    UNUSED(val);\n    UNUSED(prev);\n    return updateTlsCfg(NULL, NULL, err);\n}\n\nstatic int updateTLSPortThread(long long val, bool fFirstCall, const char **err)\n{\n    if (changeListenPort(val, &serverTL->tlsfd, acceptTLSHandler, fFirstCall) == C_ERR) {\n        *err = \"Unable to listen on this port. Check server logs.\";\n        return 0;\n    }\n\n    return 1;\n}\n\nstatic int updateTLSPort(long long val, long long prev, const char **err) {\n    /* Do nothing if port is unchanged */\n    if (val == prev) {\n        return 1;\n    }\n\n    /* Configure TLS if tls is enabled */\n    if (prev == 0 && tlsConfigure(&g_pserver->tls_ctx_config) == C_ERR) {\n        *err = \"Unable to update TLS configuration. Check server logs.\";\n        return 0;\n    }\n\n    // Do our thread first in case there is a config issue\n    if (!updateTLSPortThread(val, true /*fFirstCall*/, err))\n        return 0;\n\n    for (int ithread = 0; ithread < cserver.cthreads; ++ithread) {\n        if  (ithread == serverTL - g_pserver->rgthreadvar)\n            continue;   // we already did our thread\n        aePostFunction(g_pserver->rgthreadvar[ithread].el, [val]{\n            const char **err = nullptr;\n            if (!updateTLSPortThread(val, false /*fFirstCall*/, err)) {\n                serverLog(LL_WARNING, \"Failed to update TLS port for a thread: %s\", *err);\n                serverLog(LL_WARNING, \"\\tKeyDB will still be listening on the old port for some threads.\");\n            }\n        });\n    }\n\n    return 1;\n}\n\n#endif  /* USE_OPENSSL */\n\nint fDummy = false;\nstandardConfig configs[] = {\n    /* Bool configs */\n    createBoolConfig(\"rdbchecksum\", NULL, IMMUTABLE_CONFIG, g_pserver->rdb_checksum, 1, NULL, NULL),\n    createBoolConfig(\"daemonize\", NULL, IMMUTABLE_CONFIG, cserver.daemonize, 0, NULL, NULL),\n    createBoolConfig(\"lua-replicate-commands\", NULL, MODIFIABLE_CONFIG, g_pserver->lua_always_replicate_commands, 1, NULL, NULL),\n    createBoolConfig(\"always-show-logo\", NULL, IMMUTABLE_CONFIG, g_pserver->always_show_logo, 0, NULL, NULL),\n    createBoolConfig(\"enable-motd\", NULL, IMMUTABLE_CONFIG, cserver.enable_motd, 1, NULL, NULL),\n    createBoolConfig(\"protected-mode\", NULL, MODIFIABLE_CONFIG, g_pserver->protected_mode, 1, NULL, NULL),\n    createBoolConfig(\"rdbcompression\", NULL, MODIFIABLE_CONFIG, g_pserver->rdb_compression, 1, NULL, NULL),\n    createBoolConfig(\"rdb-del-sync-files\", NULL, MODIFIABLE_CONFIG, g_pserver->rdb_del_sync_files, 0, NULL, NULL),\n    createBoolConfig(\"activerehashing\", NULL, MODIFIABLE_CONFIG, g_pserver->activerehashing, 1, NULL, NULL),\n    createBoolConfig(\"stop-writes-on-bgsave-error\", NULL, MODIFIABLE_CONFIG, g_pserver->stop_writes_on_bgsave_err, 1, NULL, NULL),\n    createBoolConfig(\"set-proc-title\", NULL, IMMUTABLE_CONFIG, cserver.set_proc_title, 1, NULL, NULL), /* Should setproctitle be used? */\n    createBoolConfig(\"dynamic-hz\", NULL, MODIFIABLE_CONFIG, g_pserver->dynamic_hz, 1, NULL, NULL), /* Adapt hz to # of clients.*/\n    createBoolConfig(\"lazyfree-lazy-eviction\", NULL, MODIFIABLE_CONFIG, g_pserver->lazyfree_lazy_eviction, 0, NULL, NULL),\n    createBoolConfig(\"lazyfree-lazy-expire\", NULL, MODIFIABLE_CONFIG, g_pserver->lazyfree_lazy_expire, 0, NULL, NULL),\n    createBoolConfig(\"lazyfree-lazy-server-del\", NULL, MODIFIABLE_CONFIG, g_pserver->lazyfree_lazy_server_del, 0, NULL, NULL),\n    createBoolConfig(\"lazyfree-lazy-user-del\", NULL, MODIFIABLE_CONFIG, g_pserver->lazyfree_lazy_user_del , 0, NULL, NULL),\n    createBoolConfig(\"lazyfree-lazy-user-flush\", NULL, MODIFIABLE_CONFIG, g_pserver->lazyfree_lazy_user_flush , 0, NULL, NULL),\n    createBoolConfig(\"repl-disable-tcp-nodelay\", NULL, MODIFIABLE_CONFIG, g_pserver->repl_disable_tcp_nodelay, 0, NULL, NULL),\n    createBoolConfig(\"repl-diskless-sync\", NULL, MODIFIABLE_CONFIG, g_pserver->repl_diskless_sync, 0, NULL, NULL),\n    createBoolConfig(\"aof-rewrite-incremental-fsync\", NULL, MODIFIABLE_CONFIG, g_pserver->aof_rewrite_incremental_fsync, 1, NULL, NULL),\n    createBoolConfig(\"no-appendfsync-on-rewrite\", NULL, MODIFIABLE_CONFIG, g_pserver->aof_no_fsync_on_rewrite, 0, NULL, NULL),\n    createBoolConfig(\"cluster-require-full-coverage\", NULL, MODIFIABLE_CONFIG, g_pserver->cluster_require_full_coverage, 1, NULL, NULL),\n    createBoolConfig(\"rdb-save-incremental-fsync\", NULL, MODIFIABLE_CONFIG, g_pserver->rdb_save_incremental_fsync, 1, NULL, NULL),\n    createBoolConfig(\"aof-load-truncated\", NULL, MODIFIABLE_CONFIG, g_pserver->aof_load_truncated, 1, NULL, NULL),\n    createBoolConfig(\"aof-use-rdb-preamble\", NULL, MODIFIABLE_CONFIG, g_pserver->aof_use_rdb_preamble, 1, NULL, NULL),\n    createBoolConfig(\"cluster-replica-no-failover\", \"cluster-slave-no-failover\", MODIFIABLE_CONFIG, g_pserver->cluster_slave_no_failover, 0, NULL, NULL), /* Failover by default. */\n    createBoolConfig(\"replica-lazy-flush\", \"slave-lazy-flush\", MODIFIABLE_CONFIG, g_pserver->repl_slave_lazy_flush, 0, NULL, NULL),\n    createBoolConfig(\"replica-serve-stale-data\", \"slave-serve-stale-data\", MODIFIABLE_CONFIG, g_pserver->repl_serve_stale_data, 1, NULL, NULL),\n    createBoolConfig(\"replica-read-only\", \"slave-read-only\", MODIFIABLE_CONFIG, g_pserver->repl_slave_ro, 1, NULL, NULL),\n    createBoolConfig(\"replica-ignore-maxmemory\", \"slave-ignore-maxmemory\", MODIFIABLE_CONFIG, g_pserver->repl_slave_ignore_maxmemory, 1, NULL, NULL),\n    createBoolConfig(\"jemalloc-bg-thread\", NULL, MODIFIABLE_CONFIG, cserver.jemalloc_bg_thread, 1, NULL, updateJemallocBgThread),\n    createBoolConfig(\"activedefrag\", NULL, MODIFIABLE_CONFIG, cserver.active_defrag_enabled, 0, isValidActiveDefrag, NULL),\n    createBoolConfig(\"syslog-enabled\", NULL, IMMUTABLE_CONFIG, g_pserver->syslog_enabled, 0, NULL, NULL),\n    createBoolConfig(\"cluster-enabled\", NULL, IMMUTABLE_CONFIG, g_pserver->cluster_enabled, 0, NULL, NULL),\n    createBoolConfig(\"appendonly\", NULL, MODIFIABLE_CONFIG, g_pserver->aof_enabled, 0, NULL, updateAppendonly),\n    createBoolConfig(\"cluster-allow-reads-when-down\", NULL, MODIFIABLE_CONFIG, g_pserver->cluster_allow_reads_when_down, 0, NULL, NULL),\n    createBoolConfig(\"delete-on-evict\", NULL, MODIFIABLE_CONFIG, cserver.delete_on_evict, 0, NULL, NULL),\n    createBoolConfig(\"use-fork\", NULL, IMMUTABLE_CONFIG, cserver.fForkBgSave, 1, NULL, NULL),\n    createBoolConfig(\"io-threads-do-reads\", NULL, IMMUTABLE_CONFIG, fDummy, 0, NULL, NULL),\n    createBoolConfig(\"time-thread-priority\", NULL, IMMUTABLE_CONFIG, cserver.time_thread_priority, 0, NULL, NULL),\n    createBoolConfig(\"prefetch-enabled\", NULL, MODIFIABLE_CONFIG, g_pserver->prefetch_enabled, 1, NULL, NULL),\n    createBoolConfig(\"allow-rdb-resize-op\", NULL, MODIFIABLE_CONFIG, g_pserver->allowRdbResizeOp, 1, NULL, NULL),\n    createBoolConfig(\"crash-log-enabled\", NULL, MODIFIABLE_CONFIG, g_pserver->crashlog_enabled, 1, NULL, updateSighandlerEnabled),\n    createBoolConfig(\"crash-memcheck-enabled\", NULL, MODIFIABLE_CONFIG, g_pserver->memcheck_enabled, 1, NULL, NULL),\n    createBoolConfig(\"use-exit-on-panic\", NULL, MODIFIABLE_CONFIG, g_pserver->use_exit_on_panic, 0, NULL, NULL),\n    createBoolConfig(\"disable-thp\", NULL, MODIFIABLE_CONFIG, g_pserver->disable_thp, 1, NULL, NULL),\n    createBoolConfig(\"cluster-allow-replica-migration\", NULL, MODIFIABLE_CONFIG, g_pserver->cluster_allow_replica_migration, 1, NULL, NULL),\n    createBoolConfig(\"replica-announced\", NULL, MODIFIABLE_CONFIG, g_pserver->replica_announced, 1, NULL, NULL),\n    createBoolConfig(\"enable-async-commands\", NULL, MODIFIABLE_CONFIG, g_pserver->enable_async_commands, 0, NULL, NULL),\n    createBoolConfig(\"multithread-load-enabled\", NULL, MODIFIABLE_CONFIG, g_pserver->multithread_load_enabled, 0, NULL, NULL),\n    createBoolConfig(\"active-client-balancing\", NULL, MODIFIABLE_CONFIG, g_pserver->active_client_balancing, 1, NULL, NULL),\n\n    /* String Configs */\n    createStringConfig(\"aclfile\", NULL, IMMUTABLE_CONFIG, ALLOW_EMPTY_STRING, g_pserver->acl_filename, \"\", NULL, NULL),\n    createStringConfig(\"unixsocket\", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->unixsocket, NULL, NULL, NULL),\n    createStringConfig(\"pidfile\", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, cserver.pidfile, NULL, NULL, NULL),\n    createStringConfig(\"replica-announce-ip\", \"slave-announce-ip\", MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->slave_announce_ip, NULL, NULL, NULL),\n    createStringConfig(\"masteruser\", NULL, MODIFIABLE_CONFIG | SENSITIVE_CONFIG, EMPTY_STRING_IS_NULL, cserver.default_masteruser, NULL, NULL, updateMasterAuthConfig),\n    createStringConfig(\"cluster-announce-ip\", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->cluster_announce_ip, NULL, NULL, NULL),\n    createStringConfig(\"syslog-ident\", NULL, IMMUTABLE_CONFIG, ALLOW_EMPTY_STRING, g_pserver->syslog_ident, \"redis\", NULL, NULL),\n    createStringConfig(\"dbfilename\", NULL, MODIFIABLE_CONFIG, ALLOW_EMPTY_STRING, g_pserver->rdb_filename, CONFIG_DEFAULT_RDB_FILENAME, isValidDBfilename, NULL),\n    createStringConfig(\"db-s3-object\", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->rdb_s3bucketpath, NULL, isValidS3Bucket, NULL),\n    createStringConfig(\"appendfilename\", NULL, IMMUTABLE_CONFIG, ALLOW_EMPTY_STRING, g_pserver->aof_filename, \"appendonly.aof\", isValidAOFfilename, NULL),\n    createStringConfig(\"server_cpulist\", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->server_cpulist, NULL, NULL, NULL),\n    createStringConfig(\"bio_cpulist\", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->bio_cpulist, NULL, NULL, NULL),\n    createStringConfig(\"aof_rewrite_cpulist\", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->aof_rewrite_cpulist, NULL, NULL, NULL),\n    createStringConfig(\"bgsave_cpulist\", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->bgsave_cpulist, NULL, NULL, NULL),\n    createStringConfig(\"storage-provider-options\", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, cserver.storage_conf, NULL, NULL, NULL),\n    createStringConfig(\"ignore-warnings\", NULL, MODIFIABLE_CONFIG, ALLOW_EMPTY_STRING, g_pserver->ignore_warnings, \"\", NULL, NULL),\n    createStringConfig(\"proc-title-template\", NULL, MODIFIABLE_CONFIG, ALLOW_EMPTY_STRING, cserver.proc_title_template, CONFIG_DEFAULT_PROC_TITLE_TEMPLATE, isValidProcTitleTemplate, updateProcTitleTemplate),\n\n    /* SDS Configs */\n    createSDSConfig(\"masterauth\", NULL, MODIFIABLE_CONFIG | SENSITIVE_CONFIG, EMPTY_STRING_IS_NULL, cserver.default_masterauth, NULL, NULL, updateMasterAuthConfig),\n    createSDSConfig(\"requirepass\", NULL, MODIFIABLE_CONFIG | SENSITIVE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->requirepass, NULL, NULL, updateRequirePass),\n\n    /* Enum Configs */\n    createEnumConfig(\"supervised\", NULL, IMMUTABLE_CONFIG, supervised_mode_enum, cserver.supervised_mode, SUPERVISED_NONE, NULL, NULL),\n    createEnumConfig(\"syslog-facility\", NULL, IMMUTABLE_CONFIG, syslog_facility_enum, g_pserver->syslog_facility, LOG_LOCAL0, NULL, NULL),\n    createEnumConfig(\"repl-diskless-load\", NULL, MODIFIABLE_CONFIG, repl_diskless_load_enum, g_pserver->repl_diskless_load, REPL_DISKLESS_LOAD_DISABLED, NULL, NULL),\n    createEnumConfig(\"loglevel\", NULL, MODIFIABLE_CONFIG, loglevel_enum, cserver.verbosity, LL_NOTICE, NULL, NULL),\n    createEnumConfig(\"maxmemory-policy\", NULL, MODIFIABLE_CONFIG, maxmemory_policy_enum, g_pserver->maxmemory_policy, MAXMEMORY_NO_EVICTION, NULL, NULL),\n    createEnumConfig(\"appendfsync\", NULL, MODIFIABLE_CONFIG, aof_fsync_enum, g_pserver->aof_fsync, AOF_FSYNC_EVERYSEC, NULL, NULL),\n    createEnumConfig(\"storage-cache-mode\", NULL, IMMUTABLE_CONFIG, storage_memory_model_enum, cserver.storage_memory_model, STORAGE_WRITETHROUGH, NULL, NULL),\n    createEnumConfig(\"oom-score-adj\", NULL, MODIFIABLE_CONFIG, oom_score_adj_enum, g_pserver->oom_score_adj, OOM_SCORE_ADJ_NO, NULL, updateOOMScoreAdj),\n    createEnumConfig(\"acl-pubsub-default\", NULL, MODIFIABLE_CONFIG, acl_pubsub_default_enum, g_pserver->acl_pubsub_default, USER_FLAG_ALLCHANNELS, NULL, NULL),\n    createEnumConfig(\"sanitize-dump-payload\", NULL, MODIFIABLE_CONFIG, sanitize_dump_payload_enum, cserver.sanitize_dump_payload, SANITIZE_DUMP_NO, NULL, NULL),\n\n    /* Integer configs */\n    createIntConfig(\"databases\", NULL, IMMUTABLE_CONFIG, 1, INT_MAX, cserver.dbnum, 16, INTEGER_CONFIG, NULL, NULL),\n    createIntConfig(\"port\", NULL, MODIFIABLE_CONFIG, 0, 65535, g_pserver->port, 6379, INTEGER_CONFIG, NULL, updatePort), /* TCP port. */\n    createIntConfig(\"auto-aof-rewrite-percentage\", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, g_pserver->aof_rewrite_perc, 100, INTEGER_CONFIG, NULL, NULL),\n    createIntConfig(\"cluster-replica-validity-factor\", \"cluster-slave-validity-factor\", MODIFIABLE_CONFIG, 0, INT_MAX, g_pserver->cluster_slave_validity_factor, 10, INTEGER_CONFIG, NULL, NULL), /* Slave max data age factor. */\n    createIntConfig(\"list-max-ziplist-size\", NULL, MODIFIABLE_CONFIG, INT_MIN, INT_MAX, g_pserver->list_max_ziplist_size, -2, INTEGER_CONFIG, NULL, NULL),\n    createIntConfig(\"tcp-keepalive\", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, cserver.tcpkeepalive, 300, INTEGER_CONFIG, NULL, NULL),\n    createIntConfig(\"cluster-migration-barrier\", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, g_pserver->cluster_migration_barrier, 1, INTEGER_CONFIG, NULL, NULL),\n    createIntConfig(\"active-defrag-cycle-min\", NULL, MODIFIABLE_CONFIG, 1, 99, cserver.active_defrag_cycle_min, 1, INTEGER_CONFIG, NULL, NULL), /* Default: 1% CPU min (at lower threshold) */\n    createIntConfig(\"active-defrag-cycle-max\", NULL, MODIFIABLE_CONFIG, 1, 99, cserver.active_defrag_cycle_max, 25, INTEGER_CONFIG, NULL, NULL), /* Default: 25% CPU max (at upper threshold) */\n    createIntConfig(\"active-defrag-threshold-lower\", NULL, MODIFIABLE_CONFIG, 0, 1000, cserver.active_defrag_threshold_lower, 10, INTEGER_CONFIG, NULL, NULL), /* Default: don't defrag when fragmentation is below 10% */\n    createIntConfig(\"active-defrag-threshold-upper\", NULL, MODIFIABLE_CONFIG, 0, 1000, cserver.active_defrag_threshold_upper, 100, INTEGER_CONFIG, NULL, NULL), /* Default: maximum defrag force at 100% fragmentation */\n    createIntConfig(\"lfu-log-factor\", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, g_pserver->lfu_log_factor, 10, INTEGER_CONFIG, NULL, NULL),\n    createIntConfig(\"lfu-decay-time\", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, g_pserver->lfu_decay_time, 1, INTEGER_CONFIG, NULL, NULL),\n    createIntConfig(\"replica-priority\", \"slave-priority\", MODIFIABLE_CONFIG, 0, INT_MAX, g_pserver->slave_priority, 100, INTEGER_CONFIG, NULL, NULL),\n    createIntConfig(\"repl-diskless-sync-delay\", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, g_pserver->repl_diskless_sync_delay, 5, INTEGER_CONFIG, NULL, NULL),\n    createIntConfig(\"maxmemory-samples\", NULL, MODIFIABLE_CONFIG, 1, INT_MAX, g_pserver->maxmemory_samples, 16, INTEGER_CONFIG, NULL, NULL),\n    createIntConfig(\"maxmemory-eviction-tenacity\", NULL, MODIFIABLE_CONFIG, 0, 100, g_pserver->maxmemory_eviction_tenacity, 10, INTEGER_CONFIG, NULL, NULL),\n    createIntConfig(\"timeout\", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, cserver.maxidletime, 0, INTEGER_CONFIG, NULL, NULL), /* Default client timeout: infinite */\n    createIntConfig(\"replica-announce-port\", \"slave-announce-port\", MODIFIABLE_CONFIG, 0, 65535, g_pserver->slave_announce_port, 0, INTEGER_CONFIG, NULL, NULL),\n    createIntConfig(\"tcp-backlog\", NULL, IMMUTABLE_CONFIG, 0, INT_MAX, g_pserver->tcp_backlog, 511, INTEGER_CONFIG, NULL, NULL), /* TCP listen backlog. */\n    createIntConfig(\"cluster-announce-bus-port\", NULL, MODIFIABLE_CONFIG, 0, 65535, g_pserver->cluster_announce_bus_port, 0, INTEGER_CONFIG, NULL, NULL), /* Default: Use +10000 offset. */\n    createIntConfig(\"cluster-announce-port\", NULL, MODIFIABLE_CONFIG, 0, 65535, g_pserver->cluster_announce_port, 0, INTEGER_CONFIG, NULL, NULL), /* Use g_pserver->port */\n    createIntConfig(\"cluster-announce-tls-port\", NULL, MODIFIABLE_CONFIG, 0, 65535, g_pserver->cluster_announce_tls_port, 0, INTEGER_CONFIG, NULL, NULL), /* Use server.tls_port */\n    createIntConfig(\"repl-timeout\", NULL, MODIFIABLE_CONFIG, 1, INT_MAX, g_pserver->repl_timeout, 60, INTEGER_CONFIG, NULL, NULL),\n    createIntConfig(\"repl-ping-replica-period\", \"repl-ping-slave-period\", MODIFIABLE_CONFIG, 1, INT_MAX, g_pserver->repl_ping_slave_period, 10, INTEGER_CONFIG, NULL, NULL),\n    createIntConfig(\"list-compress-depth\", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, g_pserver->list_compress_depth, 0, INTEGER_CONFIG, NULL, NULL),\n    createIntConfig(\"rdb-key-save-delay\", NULL, MODIFIABLE_CONFIG, INT_MIN, INT_MAX, g_pserver->rdb_key_save_delay, 0, INTEGER_CONFIG, NULL, NULL),\n    createIntConfig(\"key-load-delay\", NULL, MODIFIABLE_CONFIG, INT_MIN, INT_MAX, g_pserver->key_load_delay, 0, INTEGER_CONFIG, NULL, NULL),\n    createIntConfig(\"active-expire-effort\", NULL, MODIFIABLE_CONFIG, 1, 10, g_pserver->active_expire_effort, 1, INTEGER_CONFIG, NULL, NULL), /* From 1 to 10. */\n    createIntConfig(\"hz\", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, g_pserver->config_hz, CONFIG_DEFAULT_HZ, INTEGER_CONFIG, NULL, updateHZ),\n    createIntConfig(\"min-replicas-to-write\", \"min-slaves-to-write\", MODIFIABLE_CONFIG, 0, INT_MAX, g_pserver->repl_min_slaves_to_write, 0, INTEGER_CONFIG, NULL, updateGoodSlaves),\n    createIntConfig(\"min-replicas-max-lag\", \"min-slaves-max-lag\", MODIFIABLE_CONFIG, 0, INT_MAX, g_pserver->repl_min_slaves_max_lag, 10, INTEGER_CONFIG, NULL, updateGoodSlaves),\n    createIntConfig(\"min-clients-per-thread\", NULL, MODIFIABLE_CONFIG, 0, 400, cserver.thread_min_client_threshold, 20, INTEGER_CONFIG, NULL, NULL),\n    createIntConfig(\"storage-flush-period\", NULL, MODIFIABLE_CONFIG, 1, 10000, g_pserver->storage_flush_period, 500, INTEGER_CONFIG, NULL, NULL),\n    createIntConfig(\"replica-quorum\", NULL, MODIFIABLE_CONFIG, -1, INT_MAX, g_pserver->repl_quorum, -1, INTEGER_CONFIG, NULL, NULL),\n    createIntConfig(\"replica-weighting-factor\", NULL, MODIFIABLE_CONFIG, 1, INT_MAX, g_pserver->replicaIsolationFactor, 2, INTEGER_CONFIG, NULL, NULL),\n    /* Unsigned int configs */\n    createUIntConfig(\"maxclients\", NULL, MODIFIABLE_CONFIG, 1, UINT_MAX, g_pserver->maxclients, 10000, INTEGER_CONFIG, NULL, updateMaxclients),\n    createUIntConfig(\"loading-process-events-interval-keys\", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, g_pserver->loading_process_events_interval_keys, 8192, MEMORY_CONFIG, NULL, NULL),\n    createUIntConfig(\"maxclients-reserved\", NULL, MODIFIABLE_CONFIG, 0, 100, g_pserver->maxclientsReserved, 0, INTEGER_CONFIG, NULL,  NULL),\n\n    /* Unsigned Long configs */\n    createULongConfig(\"active-defrag-max-scan-fields\", NULL, MODIFIABLE_CONFIG, 1, LONG_MAX, cserver.active_defrag_max_scan_fields, 1000, INTEGER_CONFIG, NULL, NULL), /* Default: keys with more than 1000 fields will be processed separately */\n    createULongConfig(\"slowlog-max-len\", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, g_pserver->slowlog_max_len, 128, INTEGER_CONFIG, NULL, NULL),\n    createULongConfig(\"acllog-max-len\", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, g_pserver->acllog_max_len, 128, INTEGER_CONFIG, NULL, NULL),\n\n    /* Long Long configs */\n    createLongLongConfig(\"lua-time-limit\", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, g_pserver->lua_time_limit, 5000, INTEGER_CONFIG, NULL, NULL),/* milliseconds */\n    createLongLongConfig(\"cluster-node-timeout\", NULL, MODIFIABLE_CONFIG, 0, LLONG_MAX, g_pserver->cluster_node_timeout, 15000, INTEGER_CONFIG, NULL, NULL),\n    createLongLongConfig(\"slowlog-log-slower-than\", NULL, MODIFIABLE_CONFIG, -1, LLONG_MAX, g_pserver->slowlog_log_slower_than, 10000, INTEGER_CONFIG, NULL, NULL),\n    createLongLongConfig(\"latency-monitor-threshold\", NULL, MODIFIABLE_CONFIG, 0, LLONG_MAX, g_pserver->latency_monitor_threshold, 0, INTEGER_CONFIG, NULL, NULL),\n    createLongLongConfig(\"proto-max-bulk-len\", NULL, MODIFIABLE_CONFIG, 1024*1024, LLONG_MAX, g_pserver->proto_max_bulk_len, 512ll*1024*1024, MEMORY_CONFIG, NULL, NULL), /* Bulk request max size */\n    createLongLongConfig(\"stream-node-max-entries\", NULL, MODIFIABLE_CONFIG, 0, LLONG_MAX, g_pserver->stream_node_max_entries, 100, INTEGER_CONFIG, NULL, NULL),\n    createLongLongConfig(\"repl-backlog-size\", NULL, MODIFIABLE_CONFIG, 1, LLONG_MAX, g_pserver->repl_backlog_config_size, 1024*1024, MEMORY_CONFIG, NULL, updateReplBacklogSize), /* Default: 1mb */\n    createLongLongConfig(\"repl-backlog-disk-reserve\", NULL, IMMUTABLE_CONFIG, 0, LLONG_MAX, cserver.repl_backlog_disk_size, 0, MEMORY_CONFIG, NULL, NULL),\n    createLongLongConfig(\"max-snapshot-slip\", NULL, MODIFIABLE_CONFIG, 0, 5000, g_pserver->snapshot_slip, 400, 0, NULL, NULL),\n    createLongLongConfig(\"max-rand-count\", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX/2, g_pserver->rand_total_threshold, LONG_MAX/2, 0, NULL, NULL),\n\n    /* Unsigned Long Long configs */\n    createULongLongConfig(\"maxmemory\", NULL, MODIFIABLE_CONFIG, 0, LLONG_MAX, g_pserver->maxmemory, 0, MEMORY_CONFIG, NULL, updateMaxmemory),\n    createULongLongConfig(\"maxstorage\", NULL, MODIFIABLE_CONFIG, 0, LLONG_MAX, g_pserver->maxstorage, 0, MEMORY_CONFIG, NULL, updateFlashMaxmemory),\n\n    /* Size_t configs */\n    createSizeTConfig(\"hash-max-ziplist-entries\", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, g_pserver->hash_max_ziplist_entries, 512, INTEGER_CONFIG, NULL, NULL),\n    createSizeTConfig(\"set-max-intset-entries\", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, g_pserver->set_max_intset_entries, 512, INTEGER_CONFIG, NULL, NULL),\n    createSizeTConfig(\"zset-max-ziplist-entries\", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, g_pserver->zset_max_ziplist_entries, 128, INTEGER_CONFIG, NULL, NULL),\n    createSizeTConfig(\"active-defrag-ignore-bytes\", NULL, MODIFIABLE_CONFIG, 1, LLONG_MAX, cserver.active_defrag_ignore_bytes, 100<<20, MEMORY_CONFIG, NULL, NULL), /* Default: don't defrag if frag overhead is below 100mb */\n    createSizeTConfig(\"hash-max-ziplist-value\", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, g_pserver->hash_max_ziplist_value, 64, MEMORY_CONFIG, NULL, NULL),\n    createSizeTConfig(\"stream-node-max-bytes\", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, g_pserver->stream_node_max_bytes, 4096, MEMORY_CONFIG, NULL, NULL),\n    createSizeTConfig(\"zset-max-ziplist-value\", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, g_pserver->zset_max_ziplist_value, 64, MEMORY_CONFIG, NULL, NULL),\n    createSizeTConfig(\"hll-sparse-max-bytes\", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, g_pserver->hll_sparse_max_bytes, 3000, MEMORY_CONFIG, NULL, NULL),\n    createSizeTConfig(\"tracking-table-max-keys\", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, g_pserver->tracking_table_max_keys, 1000000, INTEGER_CONFIG, NULL, NULL), /* Default: 1 million keys max. */\n    createSizeTConfig(\"client-query-buffer-limit\", NULL, MODIFIABLE_CONFIG, 1024*1024, LONG_MAX, cserver.client_max_querybuf_len, 1024*1024*1024, MEMORY_CONFIG, NULL, NULL), /* Default: 1GB max query buffer. */\n\n    /* Other configs */\n    createTimeTConfig(\"repl-backlog-ttl\", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, g_pserver->repl_backlog_time_limit, 60*60, INTEGER_CONFIG, NULL, NULL), /* Default: 1 hour */\n    createOffTConfig(\"auto-aof-rewrite-min-size\", NULL, MODIFIABLE_CONFIG, 0, LLONG_MAX, g_pserver->aof_rewrite_min_size, 64*1024*1024, MEMORY_CONFIG, NULL, NULL),\n\n    /* KeyDB Specific Configs */\n    createULongConfig(\"loading-process-events-interval-bytes\", NULL, MODIFIABLE_CONFIG, 0, LONG_MAX, g_pserver->loading_process_events_interval_bytes, 2*1024*1024, MEMORY_CONFIG, NULL, NULL),\n    createBoolConfig(\"multi-master-no-forward\", NULL, MODIFIABLE_CONFIG, cserver.multimaster_no_forward, 0, validateMultiMasterNoForward, NULL),\n    createBoolConfig(\"allow-write-during-load\", NULL, MODIFIABLE_CONFIG, g_pserver->fWriteDuringActiveLoad, 0, NULL, NULL),\n    createBoolConfig(\"force-backlog-disk-reserve\", NULL, MODIFIABLE_CONFIG, cserver.force_backlog_disk, 0, NULL, NULL),\n    createBoolConfig(\"soft-shutdown\", NULL, MODIFIABLE_CONFIG, g_pserver->config_soft_shutdown, 0, NULL, NULL),\n    createBoolConfig(\"flash-disable-key-cache\", NULL, MODIFIABLE_CONFIG, g_pserver->flash_disable_key_cache, 0, NULL, NULL),\n    createSizeTConfig(\"semi-ordered-set-bucket-size\", NULL, MODIFIABLE_CONFIG, 0, 1024, g_semiOrderedSetTargetBucketSize, 0, INTEGER_CONFIG, NULL, NULL),\n    createSDSConfig(\"availability-zone\", NULL, MODIFIABLE_CONFIG, 0, g_pserver->sdsAvailabilityZone, \"\", NULL, NULL),\n    createIntConfig(\"overload-protect-percent\", NULL, MODIFIABLE_CONFIG, 0, 200, g_pserver->overload_protect_threshold, 0, INTEGER_CONFIG, NULL, NULL),\n    createIntConfig(\"force-eviction-percent\", NULL, MODIFIABLE_CONFIG, 0, 100, g_pserver->force_eviction_percent, 0, INTEGER_CONFIG, NULL, NULL),\n    createBoolConfig(\"enable-async-rehash\", NULL, MODIFIABLE_CONFIG, g_pserver->enable_async_rehash, 1, NULL, NULL),\n    createBoolConfig(\"enable-keydb-fastsync\", NULL, MODIFIABLE_CONFIG, g_pserver->fEnableFastSync, 0, NULL, NULL),\n\n#ifdef USE_OPENSSL\n    createIntConfig(\"tls-port\", NULL, MODIFIABLE_CONFIG, 0, 65535, g_pserver->tls_port, 0, INTEGER_CONFIG, NULL, updateTLSPort), /* TCP port. */\n    createIntConfig(\"tls-session-cache-size\", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, g_pserver->tls_ctx_config.session_cache_size, 20*1024, INTEGER_CONFIG, NULL, updateTlsCfgInt),\n    createIntConfig(\"tls-session-cache-timeout\", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, g_pserver->tls_ctx_config.session_cache_timeout, 300, INTEGER_CONFIG, NULL, updateTlsCfgInt),\n    createBoolConfig(\"tls-cluster\", NULL, MODIFIABLE_CONFIG, g_pserver->tls_cluster, 0, NULL, updateTlsCfgBool),\n    createBoolConfig(\"tls-replication\", NULL, MODIFIABLE_CONFIG, g_pserver->tls_replication, 0, NULL, updateTlsCfgBool),\n    createEnumConfig(\"tls-auth-clients\", NULL, MODIFIABLE_CONFIG, tls_auth_clients_enum, g_pserver->tls_auth_clients, TLS_CLIENT_AUTH_YES, NULL, NULL),\n    createBoolConfig(\"tls-prefer-server-ciphers\", NULL, MODIFIABLE_CONFIG, g_pserver->tls_ctx_config.prefer_server_ciphers, 0, NULL, updateTlsCfgBool),\n    createBoolConfig(\"tls-session-caching\", NULL, MODIFIABLE_CONFIG, g_pserver->tls_ctx_config.session_caching, 1, NULL, updateTlsCfgBool),\n    createBoolConfig(\"tls-rotation\", NULL, MODIFIABLE_CONFIG, g_pserver->tls_rotation, 0, NULL, updateTlsCfgBool),\n    createStringConfig(\"tls-cert-file\", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->tls_ctx_config.cert_file, NULL, NULL, updateTlsCfg),\n    createStringConfig(\"tls-key-file\", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->tls_ctx_config.key_file, NULL, NULL, updateTlsCfg),\n    createStringConfig(\"tls-key-file-pass\", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->tls_ctx_config.key_file_pass, NULL, NULL, updateTlsCfg),\n    createStringConfig(\"tls-client-cert-file\", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->tls_ctx_config.client_cert_file, NULL, NULL, updateTlsCfg),\n    createStringConfig(\"tls-client-key-file\", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->tls_ctx_config.client_key_file, NULL, NULL, updateTlsCfg),\n    createStringConfig(\"tls-client-key-file-pass\", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->tls_ctx_config.client_key_file_pass, NULL, NULL, updateTlsCfg),\n    createStringConfig(\"tls-dh-params-file\", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->tls_ctx_config.dh_params_file, NULL, NULL, updateTlsCfg),\n    createStringConfig(\"tls-ca-cert-file\", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->tls_ctx_config.ca_cert_file, NULL, NULL, updateTlsCfg),\n    createStringConfig(\"tls-ca-cert-dir\", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->tls_ctx_config.ca_cert_dir, NULL, NULL, updateTlsCfg),\n    createStringConfig(\"tls-protocols\", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->tls_ctx_config.protocols, NULL, NULL, updateTlsCfg),\n    createStringConfig(\"tls-ciphers\", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->tls_ctx_config.ciphers, NULL, NULL, updateTlsCfg),\n    createStringConfig(\"tls-ciphersuites\", NULL, MODIFIABLE_CONFIG, EMPTY_STRING_IS_NULL, g_pserver->tls_ctx_config.ciphersuites, NULL, NULL, updateTlsCfg),\n#endif\n\n    /* NULL Terminator */\n    {NULL}\n};\n\n/*-----------------------------------------------------------------------------\n * CONFIG command entry point\n *----------------------------------------------------------------------------*/\n\nvoid configCommand(client *c) {\n    /* Only allow CONFIG GET while loading. */\n    if (g_pserver->loading && strcasecmp(szFromObj(c->argv[1]),\"get\")) {\n        addReplyError(c,\"Only CONFIG GET is allowed during loading\");\n        return;\n    }\n\n    if (c->argc == 2 && !strcasecmp(szFromObj(c->argv[1]),\"help\")) {\n        const char *help[] = {\n\"GET <pattern>\",\n\"    Return parameters matching the glob-like <pattern> and their values.\",\n\"SET <directive> <value>\",\n\"    Set the configuration <directive> to <value>.\",\n\"RESETSTAT\",\n\"    Reset statistics reported by the INFO command.\",\n\"REWRITE\",\n\"    Rewrite the configuration file.\",\nNULL\n        };\n\n        addReplyHelp(c, help);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"set\") && c->argc >= 3) {\n        configSetCommand(c);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"get\") && c->argc == 3) {\n        configGetCommand(c);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"resetstat\") && c->argc == 2) {\n        resetServerStats();\n        resetCommandTableStats();\n        resetErrorTableStats();\n        addReply(c,shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"rewrite\") && c->argc == 2) {\n        if (cserver.configfile == NULL) {\n            addReplyError(c,\"The server is running without a config file\");\n            return;\n        }\n        if (rewriteConfig(cserver.configfile, 0) == -1) {\n            serverLog(LL_WARNING,\"CONFIG REWRITE failed: %s\", strerror(errno));\n            addReplyErrorFormat(c,\"Rewriting config file: %s\", strerror(errno));\n        } else {\n            serverLog(LL_WARNING,\"CONFIG REWRITE executed with success.\");\n            addReply(c,shared.ok);\n        }\n    } else {\n        addReplySubcommandSyntaxError(c);\n        return;\n    }\n}\n"
  },
  {
    "path": "src/config.h",
    "content": "/*\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __CONFIG_H\n#define __CONFIG_H\n\n#ifdef __APPLE__\n#include <AvailabilityMacros.h>\n#endif\n\n#ifdef __linux__\n#include <features.h>\n#include <fcntl.h>\n#endif\n\n#define CONFIG_DEFAULT_RDB_FILENAME \"dump.rdb\"\n\n/* Define redis_fstat to fstat */\n#define redis_fstat fstat\n#define redis_stat stat\n\n\n/* Test for proc filesystem */\n#ifdef __linux__\n#define HAVE_PROC_STAT 1\n#define HAVE_PROC_MAPS 1\n#define HAVE_PROC_SMAPS 1\n#define HAVE_PROC_SOMAXCONN 1\n#define HAVE_PROC_OOM_SCORE_ADJ 1\n#endif\n\n/* Test for task_info() */\n#if defined(__APPLE__)\n#define HAVE_TASKINFO 1\n#endif\n\n/* Test for backtrace() */\n#if defined(__APPLE__) || (defined(__linux__) && defined(__GLIBC__)) || \\\n    defined(__FreeBSD__) || ((defined(__OpenBSD__) || defined(__NetBSD__)) && defined(USE_BACKTRACE))\\\n || defined(__DragonFly__) || (defined(__UCLIBC__) && defined(__UCLIBC_HAS_BACKTRACE__))\n#define HAVE_BACKTRACE 1\n#endif\n\n/* MSG_NOSIGNAL. */\n#ifdef __linux__\n#define HAVE_MSG_NOSIGNAL 1\n#endif\n\n/* Test for polling API */\n#ifdef __linux__\n#define HAVE_EPOLL 1\n#endif\n\n#if (defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_6)) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined (__NetBSD__)\n#define HAVE_KQUEUE 1\n#endif\n\n#ifdef __sun\n#include <sys/feature_tests.h>\n#ifdef _DTRACE_VERSION\n#define HAVE_EVPORT 1\n#define HAVE_PSINFO 1\n#endif\n#endif\n\n/* Define redis_fsync to fdatasync() in Linux and fsync() for all the rest */\n#ifdef __linux__\n#define redis_fsync fdatasync\n#else\n#define redis_fsync fsync\n#endif\n\n#if __GNUC__ >= 4\n#define redis_unreachable __builtin_unreachable\n#else\n#define redis_unreachable abort\n#endif\n\n#if __GNUC__ >= 3\n#define likely(x) __builtin_expect(!!(x), 1)\n#define unlikely(x) __builtin_expect(!!(x), 0)\n#else\n#define likely(x) (x)\n#define unlikely(x) (x)\n#endif\n\n/* Define rdb_fsync_range to sync_file_range() on Linux, otherwise we use\n * the plain fsync() call. */\n#if (defined(__linux__) && defined(SYNC_FILE_RANGE_WAIT_BEFORE))\n#define rdb_fsync_range(fd,off,size) sync_file_range(fd,off,size,SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE)\n#else\n#define rdb_fsync_range(fd,off,size) fsync(fd)\n#endif\n\n/* Check if we can use setproctitle().\n * BSD systems have support for it, we provide an implementation for\n * Linux and osx. */\n#if (defined __NetBSD__ || defined __FreeBSD__ || defined __OpenBSD__)\n#define USE_SETPROCTITLE\n#endif\n\n#if defined(__HAIKU__)\n#define ESOCKTNOSUPPORT 0\n#endif\n\n#if (defined __linux || defined __APPLE__)\n#define USE_SETPROCTITLE\n#define INIT_SETPROCTITLE_REPLACEMENT\n#ifdef __cplusplus\nextern \"C\" {\n#endif\nvoid spt_init(int argc, char *argv[]);\nvoid setproctitle(const char *fmt, ...);\n#ifdef __cplusplus\n}\n#endif\n#endif\n\n/* Byte ordering detection */\n#include <sys/types.h> /* This will likely define BYTE_ORDER */\n\n#ifndef BYTE_ORDER\n#if (BSD >= 199103)\n# include <machine/endian.h>\n#else\n#if defined(linux) || defined(__linux__)\n# include <endian.h>\n#else\n#define\tLITTLE_ENDIAN\t1234\t/* least-significant byte first (vax, pc) */\n#define\tBIG_ENDIAN\t4321\t/* most-significant byte first (IBM, net) */\n#define\tPDP_ENDIAN\t3412\t/* LSB first in word, MSW first in long (pdp)*/\n\n#if defined(__i386__) || defined(__x86_64__) || defined(__amd64__) || \\\n   defined(vax) || defined(ns32000) || defined(sun386) || \\\n   defined(MIPSEL) || defined(_MIPSEL) || defined(BIT_ZERO_ON_RIGHT) || \\\n   defined(__alpha__) || defined(__alpha)\n#define BYTE_ORDER    LITTLE_ENDIAN\n#endif\n\n#if defined(sel) || defined(pyr) || defined(mc68000) || defined(sparc) || \\\n    defined(is68k) || defined(tahoe) || defined(ibm032) || defined(ibm370) || \\\n    defined(MIPSEB) || defined(_MIPSEB) || defined(_IBMR2) || defined(DGUX) ||\\\n    defined(apollo) || defined(__convex__) || defined(_CRAY) || \\\n    defined(__hppa) || defined(__hp9000) || \\\n    defined(__hp9000s300) || defined(__hp9000s700) || \\\n    defined (BIT_ZERO_ON_LEFT) || defined(m68k) || defined(__sparc)\n#define BYTE_ORDER\tBIG_ENDIAN\n#endif\n#endif /* linux */\n#endif /* BSD */\n#endif /* BYTE_ORDER */\n\n/* Sometimes after including an OS-specific header that defines the\n * endianness we end with __BYTE_ORDER but not with BYTE_ORDER that is what\n * the Redis code uses. In this case let's define everything without the\n * underscores. */\n#ifndef BYTE_ORDER\n#ifdef __BYTE_ORDER\n#if defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN)\n#ifndef LITTLE_ENDIAN\n#define LITTLE_ENDIAN __LITTLE_ENDIAN\n#endif\n#ifndef BIG_ENDIAN\n#define BIG_ENDIAN __BIG_ENDIAN\n#endif\n#if (__BYTE_ORDER == __LITTLE_ENDIAN)\n#define BYTE_ORDER LITTLE_ENDIAN\n#else\n#define BYTE_ORDER BIG_ENDIAN\n#endif\n#endif\n#endif\n#endif\n\n#if !defined(BYTE_ORDER) || \\\n    (BYTE_ORDER != BIG_ENDIAN && BYTE_ORDER != LITTLE_ENDIAN)\n\t/* you must determine what the correct bit order is for\n\t * your compiler - the next line is an intentional error\n\t * which will force your compiles to bomb until you fix\n\t * the above macros.\n\t */\n#error \"Undefined or invalid BYTE_ORDER\"\n#endif\n\n#if (__i386 || __amd64 || __powerpc__) && __GNUC__\n#define GNUC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)\n#if defined(__clang__)\n#define HAVE_ATOMIC\n#endif\n#if (defined(__GLIBC__) && defined(__GLIBC_PREREQ))\n#if (GNUC_VERSION >= 40100 && __GLIBC_PREREQ(2, 6))\n#define HAVE_ATOMIC\n#endif\n#endif\n#endif\n\n/* Make sure we can test for ARM just checking for __arm__, since sometimes\n * __arm is defined but __arm__ is not. */\n#if defined(__arm) && !defined(__arm__)\n#define __arm__\n#endif\n#if defined (__aarch64__) && !defined(__arm64__)\n#define __arm64__\n#endif\n\n/* Make sure we can test for SPARC just checking for __sparc__. */\n#if defined(__sparc) && !defined(__sparc__)\n#define __sparc__\n#endif\n\n#if defined(__sparc__) || defined(__arm__)\n#define USE_ALIGNED_ACCESS\n#endif\n\n/* Define for redis_set_thread_title */\n#ifdef __linux__\n#define redis_set_thread_title(name) pthread_setname_np(pthread_self(), name)\n#else\n#if (defined __FreeBSD__ || defined __OpenBSD__)\n#include <pthread_np.h>\n#define redis_set_thread_title(name) pthread_set_name_np(pthread_self(), name)\n#elif defined __NetBSD__\n#include <pthread.h>\n#define redis_set_thread_title(name) pthread_setname_np(pthread_self(), \"%s\", name)\n#elif defined __HAIKU__\n#include <kernel/OS.h>\n#define redis_set_thread_title(name) rename_thread(find_thread(0), name)\n#else\n#if (defined __APPLE__ && defined(MAC_OS_X_VERSION_10_7))\n#ifdef __cplusplus\nextern \"C\" \n#endif\nint pthread_setname_np(const char *name);\n#include <pthread.h>\n#define redis_set_thread_title(name) pthread_setname_np(name)\n#else\n#define redis_set_thread_title(name)\n#endif\n#endif\n#endif\n\n/* Check if we can use setcpuaffinity(). */\n#if (defined __linux || defined __NetBSD__ || defined __FreeBSD__ || defined __DragonFly__)\n#define USE_SETCPUAFFINITY\n#ifdef __cplusplus\nextern \"C\" \n#endif\nvoid setcpuaffinity(const char *cpulist);\n#endif\n\n#endif\n"
  },
  {
    "path": "src/connection.cpp",
    "content": "/*\n * Copyright (c) 2019, Redis Labs\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n#include \"connhelpers.h\"\n#include \"aelocker.h\"\n\n/* The connections module provides a lean abstraction of network connections\n * to avoid direct socket and async event management across the Redis code base.\n *\n * It does NOT provide advanced connection features commonly found in similar\n * libraries such as complete in/out buffer management, throttling, etc. These\n * functions remain in networking.c.\n *\n * The primary goal is to allow transparent handling of TCP and TLS based\n * connections. To do so, connections have the following properties:\n *\n * 1. A connection may live before its corresponding socket exists.  This\n *    allows various context and configuration setting to be handled before\n *    establishing the actual connection.\n * 2. The caller may register/unregister logical read/write handlers to be\n *    called when the connection has data to read from/can accept writes.\n *    These logical handlers may or may not correspond to actual AE events,\n *    depending on the implementation (for TCP they are; for TLS they aren't).\n */\n\nextern ConnectionType CT_Socket;\n\n/* When a connection is created we must know its type already, but the\n * underlying socket may or may not exist:\n *\n * - For accepted connections, it exists as we do not model the listen/accept\n *   part; So caller calls connCreateSocket() followed by connAccept().\n * - For outgoing connections, the socket is created by the connection module\n *   itself; So caller calls connCreateSocket() followed by connConnect(),\n *   which registers a connect callback that fires on connected/error state\n *   (and after any transport level handshake was done).\n *\n * NOTE: An earlier version relied on connections being part of other structs\n * and not independently allocated. This could lead to further optimizations\n * like using container_of(), etc.  However it was discontinued in favor of\n * this approach for these reasons:\n *\n * 1. In some cases conns are created/handled outside the context of the\n * containing struct, in which case it gets a bit awkward to copy them.\n * 2. Future implementations may wish to allocate arbitrary data for the\n * connection.\n * 3. The container_of() approach is anyway risky because connections may\n * be embedded in different structs, not just client.\n */\n\nconnection *connCreateSocket() {\n    connection *conn = (connection*)zcalloc(sizeof(connection), MALLOC_LOCAL);\n    conn->type = &CT_Socket;\n    conn->fd = -1;\n\n    return conn;\n}\n\n/* Create a new socket-type connection that is already associated with\n * an accepted connection.\n *\n * The socket is not ready for I/O until connAccept() was called and\n * invoked the connection-level accept handler.\n *\n * Callers should use connGetState() and verify the created connection\n * is not in an error state (which is not possible for a socket connection,\n * but could but possible with other protocols).\n */\nconnection *connCreateAcceptedSocket(int fd) {\n    connection *conn = connCreateSocket();\n    conn->fd = fd;\n    conn->state = CONN_STATE_ACCEPTING;\n    return conn;\n}\n\nstatic int connSocketConnect(connection *conn, const char *addr, int port, const char *src_addr,\n        ConnectionCallbackFunc connect_handler) {\n    int fd = anetTcpNonBlockBestEffortBindConnect(NULL,addr,port,src_addr);\n    if (fd == -1) {\n        conn->state = CONN_STATE_ERROR;\n        conn->last_errno = errno;\n        return C_ERR;\n    }\n\n    conn->fd = fd;\n    conn->state = CONN_STATE_CONNECTING;\n\n    conn->conn_handler = connect_handler;\n    aeCreateFileEvent(serverTL->el, conn->fd, AE_WRITABLE|AE_WRITE_THREADSAFE,\n            conn->type->ae_handler, conn);\n\n    return C_OK;\n}\n\n/* Returns true if a write handler is registered */\nint connHasWriteHandler(connection *conn) {\n    return conn->write_handler != NULL;\n}\n\n/* Returns true if a read handler is registered */\nint connHasReadHandler(connection *conn) {\n    return conn->read_handler != NULL;\n}\n\n/* Associate a private data pointer with the connection */\nvoid connSetPrivateData(connection *conn, void *data) {\n    conn->private_data = data;\n}\n\n/* Get the associated private data pointer */\nvoid *connGetPrivateData(connection *conn) {\n    return conn->private_data;\n}\n\n/* ------ Pure socket connections ------- */\n\n/* A very incomplete list of implementation-specific calls.  Much of the above shall\n * move here as we implement additional connection types.\n */\n\n/* Close the connection and free resources. */\nstatic void connSocketClose(connection *conn) {\n    if (conn->fd != -1) {\n        aeDeleteFileEvent(serverTL->el,conn->fd,AE_READABLE | AE_WRITABLE);\n        close(conn->fd);\n        conn->fd = -1;\n    }\n\n    /* If called from within a handler, schedule the close but\n     * keep the connection until the handler returns.\n     */\n    if (connHasRefs(conn)) {\n        conn->flags |= CONN_FLAG_CLOSE_SCHEDULED;\n        return;\n    }\n\n    if (conn->fprint) {\n        zfree(conn->fprint);\n        conn->fprint = NULL;\n    }\n\n    zfree(conn);\n}\n\nstatic int connSocketWrite(connection *conn, const void *data, size_t data_len) {\n    int ret = write(conn->fd, data, data_len);\n    if (ret < 0 && errno != EAGAIN) {\n        conn->last_errno = errno;\n\n        /* Don't overwrite the state of a connection that is not already\n         * connected, not to mess with handler callbacks.\n         */\n        ConnectionState expected = CONN_STATE_CONNECTED;\n        conn->state.compare_exchange_strong(expected, CONN_STATE_ERROR, std::memory_order_relaxed);\n    }\n\n    return ret;\n}\n\nstatic int connSocketRead(connection *conn, void *buf, size_t buf_len) {\n    int ret = read(conn->fd, buf, buf_len);\n    if (!ret) {\n        conn->state.store(CONN_STATE_CLOSED, std::memory_order_release);\n    } else if (ret < 0 && errno != EAGAIN) {\n        conn->last_errno = errno;\n\n        /* Don't overwrite the state of a connection that is not already\n         * connected, not to mess with handler callbacks.\n         */\n        ConnectionState expected = CONN_STATE_CONNECTED;\n        conn->state.compare_exchange_strong(expected, CONN_STATE_ERROR, std::memory_order_release);\n    }\n\n    return ret;\n}\n\nstatic int connSocketAccept(connection *conn, ConnectionCallbackFunc accept_handler) {\n    int ret = C_OK;\n\n    if (conn->state != CONN_STATE_ACCEPTING) return C_ERR;\n    conn->state = CONN_STATE_CONNECTED;\n\n    connIncrRefs(conn);\n    if (!callHandler(conn, accept_handler)) ret = C_ERR;\n    connDecrRefs(conn);\n\n    return ret;\n}\n\n/* Register a write handler, to be called when the connection is writable.\n * If NULL, the existing handler is removed.\n *\n * The barrier flag indicates a write barrier is requested, resulting with\n * CONN_FLAG_WRITE_BARRIER set. This will ensure that the write handler is\n * always called before and not after the read handler in a single event\n * loop.\n */\nstatic int connSocketSetWriteHandler(connection *conn, ConnectionCallbackFunc func, int barrier, bool fThreadSafe) {\n    if (func == conn->write_handler) return C_OK;\n\n    conn->write_handler = func;\n    if (barrier)\n        conn->flags |= CONN_FLAG_WRITE_BARRIER;\n    else\n        conn->flags &= ~CONN_FLAG_WRITE_BARRIER;\n\n    if (fThreadSafe)\n        conn->flags |= CONN_FLAG_WRITE_THREADSAFE;\n    else\n        conn->flags &= ~CONN_FLAG_WRITE_THREADSAFE;\n\n    if (!conn->write_handler)\n        aeDeleteFileEvent(serverTL->el,conn->fd,AE_WRITABLE);\n    else\n        if (aeCreateFileEvent(serverTL->el,conn->fd,AE_WRITABLE|AE_WRITE_THREADSAFE,\n                    conn->type->ae_handler,conn) == AE_ERR) return C_ERR;\n    return C_OK;\n}\n\n/* Register a read handler, to be called when the connection is readable.\n * If NULL, the existing handler is removed.\n */\nstatic int connSocketSetReadHandler(connection *conn, ConnectionCallbackFunc func, bool fThreadSafe) {\n    if (func == conn->read_handler) return C_OK;\n    \n    if (fThreadSafe)\n        conn->flags |= CONN_FLAG_READ_THREADSAFE;\n    else\n        conn->flags &= ~CONN_FLAG_READ_THREADSAFE;\n\n    conn->read_handler = func;\n    if (!conn->read_handler)\n        aeDeleteFileEvent(serverTL->el,conn->fd,AE_READABLE);\n    else\n        if (aeCreateFileEvent(serverTL->el,conn->fd,\n                    AE_READABLE|AE_READ_THREADSAFE,conn->type->ae_handler,conn) == AE_ERR) return C_ERR;\n    return C_OK;\n}\n\nstatic const char *connSocketGetLastError(connection *conn) {\n    return strerror(conn->last_errno);\n}\n\nvoid connSocketEventHandler(struct aeEventLoop *el, int fd, void *clientData, int mask)\n{\n    UNUSED(el);\n    UNUSED(fd);\n    connection *conn = (connection*)clientData;\n\n    if (conn->state.load(std::memory_order_relaxed) == CONN_STATE_CONNECTING &&\n            (mask & AE_WRITABLE) && conn->conn_handler) {\n\n        int conn_error = connGetSocketError(conn);\n        if (conn_error) {\n            conn->last_errno = conn_error;\n            conn->state.store(CONN_STATE_ERROR, std::memory_order_release);\n        } else {\n            conn->state.store(CONN_STATE_CONNECTED, std::memory_order_release);\n        }\n\n        if (!conn->write_handler) aeDeleteFileEvent(serverTL->el,conn->fd,AE_WRITABLE);\n\n        {\n        AeLocker locker;\n        locker.arm(nullptr);\n        if (!callHandler(conn, conn->conn_handler)) return;\n        }\n        conn->conn_handler = NULL;\n    }\n\n    /* Normally we execute the readable event first, and the writable\n     * event later. This is useful as sometimes we may be able\n     * to serve the reply of a query immediately after processing the\n     * query.\n     *\n     * However if WRITE_BARRIER is set in the mask, our application is\n     * asking us to do the reverse: never fire the writable event\n     * after the readable. In such a case, we invert the calls.\n     * This is useful when, for instance, we want to do things\n     * in the beforeSleep() hook, like fsync'ing a file to disk,\n     * before replying to a client. */\n    int invert = conn->flags & CONN_FLAG_WRITE_BARRIER;\n\n    int call_write = (mask & AE_WRITABLE) && conn->write_handler;\n    int call_read = (mask & AE_READABLE) && conn->read_handler;\n\n    /* Handle normal I/O flows */\n    if (!invert && call_read) {\n        AeLocker lock;\n        if (!(conn->flags & CONN_FLAG_READ_THREADSAFE))\n            lock.arm(nullptr);\n        \n        if (!callHandler(conn, conn->read_handler)) return;\n    }\n    /* Fire the writable event. */\n    if (call_write) {\n        AeLocker lock;\n        if (!(conn->flags & CONN_FLAG_WRITE_THREADSAFE))\n            lock.arm(nullptr);\n\n        if (!callHandler(conn, conn->write_handler)) return;\n    }\n    /* If we have to invert the call, fire the readable event now\n     * after the writable one. */\n    if (invert && call_read) {\n        AeLocker lock;\n        if (!(conn->flags & CONN_FLAG_READ_THREADSAFE))\n            lock.arm(nullptr);\n        \n        if (!callHandler(conn, conn->read_handler)) return;\n    }\n}\n\nstatic int connSocketBlockingConnect(connection *conn, const char *addr, int port, long long timeout) {\n    int fd = anetTcpNonBlockConnect(NULL,addr,port);\n    if (fd == -1) {\n        conn->state = CONN_STATE_ERROR;\n        conn->last_errno = errno;\n        return C_ERR;\n    }\n\n    if ((aeWait(fd, AE_WRITABLE, timeout) & AE_WRITABLE) == 0) {\n        conn->state = CONN_STATE_ERROR;\n        conn->last_errno = ETIMEDOUT;\n    }\n\n    conn->fd = fd;\n    conn->state = CONN_STATE_CONNECTED;\n    return C_OK;\n}\n\n/* Connection-based versions of syncio.c functions.\n * NOTE: This should ideally be refactored out in favor of pure async work.\n */\n\nstatic ssize_t connSocketSyncWrite(connection *conn, const char *ptr, ssize_t size, long long timeout) {\n    return syncWrite(conn->fd, ptr, size, timeout);\n}\n\nstatic ssize_t connSocketSyncRead(connection *conn, char *ptr, ssize_t size, long long timeout) {\n    return syncRead(conn->fd, ptr, size, timeout);\n}\n\nstatic ssize_t connSocketSyncReadLine(connection *conn, char *ptr, ssize_t size, long long timeout) {\n    return syncReadLine(conn->fd, ptr, size, timeout);\n}\n\nstatic int connSocketGetType(struct connection *conn) {\n    (void) conn;\n\n    return CONN_TYPE_SOCKET;\n}\n\nConnectionType CT_Socket = {\n    connSocketEventHandler,\n    connSocketConnect,\n    connSocketWrite,\n    connSocketRead,\n    connSocketClose,\n    connSocketAccept,\n    connSocketSetWriteHandler,\n    connSocketSetReadHandler,\n    connSocketGetLastError,\n    connSocketBlockingConnect,\n    connSocketSyncWrite,\n    connSocketSyncRead,\n    connSocketSyncReadLine,\n    nullptr,\n    connSocketGetType\n};\n\n\nint connGetSocketError(connection *conn) {\n    int sockerr = 0;\n    socklen_t errlen = sizeof(sockerr);\n\n    if (getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &sockerr, &errlen) == -1)\n        sockerr = errno;\n    return sockerr;\n}\n\nint connPeerToString(connection *conn, char *ip, size_t ip_len, int *port) {\n    return anetFdToString(conn ? conn->fd : -1, ip, ip_len, port, FD_TO_PEER_NAME);\n}\n\nint connSockName(connection *conn, char *ip, size_t ip_len, int *port) {\n    return anetFdToString(conn->fd, ip, ip_len, port, FD_TO_SOCK_NAME);\n}\n\nint connFormatFdAddr(connection *conn, char *buf, size_t buf_len, int fd_to_str_type) {\n    return anetFormatFdAddr(conn ? conn->fd : -1, buf, buf_len, fd_to_str_type);\n}\n\nint connBlock(connection *conn) {\n    if (conn->fd == -1) return C_ERR;\n    return anetBlock(NULL, conn->fd);\n}\n\nint connNonBlock(connection *conn) {\n    if (conn->fd == -1) return C_ERR;\n    return anetNonBlock(NULL, conn->fd);\n}\n\nint connEnableTcpNoDelay(connection *conn) {\n    if (conn->fd == -1) return C_ERR;\n    return anetEnableTcpNoDelay(NULL, conn->fd);\n}\n\nint connDisableTcpNoDelay(connection *conn) {\n    if (conn->fd == -1) return C_ERR;\n    return anetDisableTcpNoDelay(NULL, conn->fd);\n}\n\nint connKeepAlive(connection *conn, int interval) {\n    if (conn->fd == -1) return C_ERR;\n    return anetKeepAlive(NULL, conn->fd, interval);\n}\n\nint connSendTimeout(connection *conn, long long ms) {\n    return anetSendTimeout(NULL, conn->fd, ms);\n}\n\nint connRecvTimeout(connection *conn, long long ms) {\n    return anetRecvTimeout(NULL, conn->fd, ms);\n}\n\nint connGetState(connection *conn) {\n    return conn->state.load(std::memory_order_relaxed);\n}\n\nvoid connSetThreadAffinity(connection *conn, int cpu) {\n#ifdef HAVE_SO_INCOMING_CPU\n    if (setsockopt(conn->fd, SOL_SOCKET, SO_INCOMING_CPU, &cpu, sizeof(cpu)) != 0)\n    {\n        serverLog(LL_WARNING, \"Failed to set socket affinity\");\n    }\n#else\n    (void)conn;\n    (void)cpu;\n#endif\n}\n\n/* Return a text that describes the connection, suitable for inclusion\n * in CLIENT LIST and similar outputs.\n *\n * For sockets, we always return \"fd=<fdnum>\" to maintain compatibility.\n */\nconst char *connGetInfo(connection *conn, char *buf, size_t buf_len) {\n    snprintf(buf, buf_len-1, \"fd=%i\", conn == NULL ? -1 : conn->fd);\n    return buf;\n}\n"
  },
  {
    "path": "src/connection.h",
    "content": "\n/*\n * Copyright (c) 2019, Redis Labs\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __REDIS_CONNECTION_H\n#define __REDIS_CONNECTION_H\n\n#include <atomic>\n\n#define CONN_INFO_LEN   32\n\nstruct aeEventLoop;\ntypedef struct connection connection;\n\ntypedef enum {\n    CONN_STATE_NONE = 0,\n    CONN_STATE_CONNECTING,\n    CONN_STATE_ACCEPTING,\n    CONN_STATE_CONNECTED,\n    CONN_STATE_CLOSED,\n    CONN_STATE_ERROR\n} ConnectionState;\n\n#define CONN_FLAG_CLOSE_SCHEDULED   (1<<0)      /* Closed scheduled by a handler */\n#define CONN_FLAG_WRITE_BARRIER     (1<<1)      /* Write barrier requested */\n#define CONN_FLAG_READ_THREADSAFE        (1<<2)\n#define CONN_FLAG_WRITE_THREADSAFE       (1<<3)\n#define CONN_FLAG_AUDIT_LOGGING_REQUIRED (1<<4)\n\n#define CONN_TYPE_SOCKET            1\n#define CONN_TYPE_TLS               2\n\ntypedef void (*ConnectionCallbackFunc)(struct connection *conn);\n\ntypedef struct ConnectionType {\n    void (*ae_handler)(struct aeEventLoop *el, int fd, void *clientData, int mask);\n    int (*connect)(struct connection *conn, const char *addr, int port, const char *source_addr, ConnectionCallbackFunc connect_handler);\n    int (*write)(struct connection *conn, const void *data, size_t data_len);\n    int (*read)(struct connection *conn, void *buf, size_t buf_len);\n    void (*close)(struct connection *conn);\n    int (*accept)(struct connection *conn, ConnectionCallbackFunc accept_handler);\n    int (*set_write_handler)(struct connection *conn, ConnectionCallbackFunc handler, int barrier, bool fThreadSafe);\n    int (*set_read_handler)(struct connection *conn, ConnectionCallbackFunc handler, bool fThreadSafe);\n    const char *(*get_last_error)(struct connection *conn);\n    int (*blocking_connect)(struct connection *conn, const char *addr, int port, long long timeout);\n    ssize_t (*sync_write)(struct connection *conn, const char *ptr, ssize_t size, long long timeout);\n    ssize_t (*sync_read)(struct connection *conn, char *ptr, ssize_t size, long long timeout);\n    ssize_t (*sync_readline)(struct connection *conn, char *ptr, ssize_t size, long long timeout);\n    void (*marshal_thread)(struct connection *conn);\n    int (*get_type)(struct connection *conn);\n} ConnectionType;\n\nstruct connection {\n    ConnectionType *type;\n    std::atomic<ConnectionState> state;\n    short int flags;\n    short int refs;\n    int last_errno;\n    void *private_data;\n    ConnectionCallbackFunc conn_handler;\n    ConnectionCallbackFunc write_handler;\n    ConnectionCallbackFunc read_handler;\n    int fd;\n    char* fprint;\n};\n\n/* The connection module does not deal with listening and accepting sockets,\n * so we assume we have a socket when an incoming connection is created.\n *\n * The fd supplied should therefore be associated with an already accept()ed\n * socket.\n *\n * connAccept() may directly call accept_handler(), or return and call it\n * at a later time. This behavior is a bit awkward but aims to reduce the need\n * to wait for the next event loop, if no additional handshake is required.\n *\n * IMPORTANT: accept_handler may decide to close the connection, calling connClose().\n * To make this safe, the connection is only marked with CONN_FLAG_CLOSE_SCHEDULED\n * in this case, and connAccept() returns with an error.\n *\n * connAccept() callers must always check the return value and on error (C_ERR)\n * a connClose() must be called.\n */\n\nstatic inline int connAccept(connection *conn, ConnectionCallbackFunc accept_handler) {\n    return conn->type->accept(conn, accept_handler);\n}\n\n/* Establish a connection.  The connect_handler will be called when the connection\n * is established, or if an error has occurred.\n *\n * The connection handler will be responsible to set up any read/write handlers\n * as needed.\n *\n * If C_ERR is returned, the operation failed and the connection handler shall\n * not be expected.\n */\nstatic inline int connConnect(connection *conn, const char *addr, int port, const char *src_addr,\n        ConnectionCallbackFunc connect_handler) {\n    return conn->type->connect(conn, addr, port, src_addr, connect_handler);\n}\n\n/* Blocking connect.\n *\n * NOTE: This is implemented in order to simplify the transition to the abstract\n * connections, but should probably be refactored out of cluster.c and replication.c,\n * in favor of a pure async implementation.\n */\nstatic inline int connBlockingConnect(connection *conn, const char *addr, int port, long long timeout) {\n    return conn->type->blocking_connect(conn, addr, port, timeout);\n}\n\n/* Write to connection, behaves the same as write(2).\n *\n * Like write(2), a short write is possible. A -1 return indicates an error.\n *\n * The caller should NOT rely on errno. Testing for an EAGAIN-like condition, use\n * connGetState() to see if the connection state is still CONN_STATE_CONNECTED.\n */\nstatic inline int connWrite(connection *conn, const void *data, size_t data_len) {\n    return conn->type->write(conn, data, data_len);\n}\n\n/* Read from the connection, behaves the same as read(2).\n * \n * Like read(2), a short read is possible.  A return value of 0 will indicate the\n * connection was closed, and -1 will indicate an error.\n *\n * The caller should NOT rely on errno. Testing for an EAGAIN-like condition, use\n * connGetState() to see if the connection state is still CONN_STATE_CONNECTED.\n */\nstatic inline int connRead(connection *conn, void *buf, size_t buf_len) {\n    return conn->type->read(conn, buf, buf_len);\n}\n\n/* Register a write handler, to be called when the connection is writable.\n * If NULL, the existing handler is removed.\n */\nstatic inline int connSetWriteHandler(connection *conn, ConnectionCallbackFunc func, bool fThreadSafe = false) {\n    return conn->type->set_write_handler(conn, func, 0, fThreadSafe);\n}\n\n/* Register a read handler, to be called when the connection is readable.\n * If NULL, the existing handler is removed.\n */\nstatic inline int connSetReadHandler(connection *conn, ConnectionCallbackFunc func, bool fThreadSafe = false) {\n    return conn->type->set_read_handler(conn, func, fThreadSafe);\n}\n\n/* Set a write handler, and possibly enable a write barrier, this flag is\n * cleared when write handler is changed or removed.\n * With barrier enabled, we never fire the event if the read handler already\n * fired in the same event loop iteration. Useful when you want to persist\n * things to disk before sending replies, and want to do that in a group fashion. */\nstatic inline int connSetWriteHandlerWithBarrier(connection *conn, ConnectionCallbackFunc func, int barrier, bool fThreadSafe = false) {\n    return conn->type->set_write_handler(conn, func, barrier, fThreadSafe);\n}\n\nstatic inline void connClose(connection *conn) {\n    conn->type->close(conn);\n}\n\n/* Returns the last error encountered by the connection, as a string.  If no error,\n * a NULL is returned.\n */\nstatic inline const char *connGetLastError(connection *conn) {\n    return conn->type->get_last_error(conn);\n}\n\nstatic inline ssize_t connSyncWrite(connection *conn, const char *ptr, ssize_t size, long long timeout) {\n    return conn->type->sync_write(conn, ptr, size, timeout);\n}\n\nstatic inline ssize_t connSyncRead(connection *conn, char *ptr, ssize_t size, long long timeout) {\n    return conn->type->sync_read(conn, ptr, size, timeout);\n}\n\nstatic inline ssize_t connSyncReadLine(connection *conn, char *ptr, ssize_t size, long long timeout) {\n    return conn->type->sync_readline(conn, ptr, size, timeout);\n}\n\nstatic inline void connMarshalThread(connection *conn) {\n    if (conn->type->marshal_thread != nullptr)\n        conn->type->marshal_thread(conn);\n}\n\n/* Return CONN_TYPE_* for the specified connection */\nstatic inline int connGetType(connection *conn) {\n    return conn->type->get_type(conn);\n}\n\nconnection *connCreateSocket();\nconnection *connCreateAcceptedSocket(int fd);\n\nconnection *connCreateTLS();\nconnection *connCreateAcceptedTLS(int fd, int require_auth);\n\nvoid connSetPrivateData(connection *conn, void *data);\nvoid *connGetPrivateData(connection *conn);\nint connGetState(connection *conn);\nint connHasWriteHandler(connection *conn);\nint connHasReadHandler(connection *conn);\nint connGetSocketError(connection *conn);\nvoid connSetThreadAffinity(connection *conn, int cpu);\n\n/* anet-style wrappers to conns */\nint connBlock(connection *conn);\nint connNonBlock(connection *conn);\nint connEnableTcpNoDelay(connection *conn);\nint connDisableTcpNoDelay(connection *conn);\nint connKeepAlive(connection *conn, int interval);\nint connSendTimeout(connection *conn, long long ms);\nint connRecvTimeout(connection *conn, long long ms);\nint connPeerToString(connection *conn, char *ip, size_t ip_len, int *port);\nint connFormatFdAddr(connection *conn, char *buf, size_t buf_len, int fd_to_str_type);\nint connSockName(connection *conn, char *ip, size_t ip_len, int *port);\nconst char *connGetInfo(connection *conn, char *buf, size_t buf_len);\n\n/* Helpers for tls special considerations */\nsds connTLSGetPeerCert(connection *conn);\nint tlsHasPendingData();\nint tlsProcessPendingData();\n\n#endif  /* __REDIS_CONNECTION_H */\n"
  },
  {
    "path": "src/connhelpers.h",
    "content": "\n/*\n * Copyright (c) 2019, Redis Labs\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __REDIS_CONNHELPERS_H\n#define __REDIS_CONNHELPERS_H\n\n#include \"connection.h\"\n\n/* These are helper functions that are common to different connection\n * implementations (currently sockets in connection.c and TLS in tls.c).\n *\n * Currently helpers implement the mechanisms for invoking connection\n * handlers and tracking connection references, to allow safe destruction\n * of connections from within a handler.\n */\n\n/* Incremenet connection references.\n *\n * Inside a connection handler, we guarantee refs >= 1 so it is always\n * safe to connClose().\n *\n * In other cases where we don't want to prematurely lose the connection,\n * it can go beyond 1 as well; currently it is only done by connAccept().\n */\nstatic inline void connIncrRefs(connection *conn) {\n    conn->refs++;\n}\n\n/* Decrement connection references.\n *\n * Note that this is not intended to provide any automatic free logic!\n * callHandler() takes care of that for the common flows, and anywhere an\n * explicit connIncrRefs() is used, the caller is expected to take care of\n * that.\n */\n\nstatic inline void connDecrRefs(connection *conn) {\n    conn->refs--;\n}\n\nstatic inline int connHasRefs(connection *conn) {\n    return conn->refs;\n}\n\n/* Helper for connection implementations to call handlers:\n * 1. Increment refs to protect the connection.\n * 2. Execute the handler (if set).\n * 3. Decrement refs and perform deferred close, if refs==0.\n */\nstatic inline int callHandler(connection *conn, ConnectionCallbackFunc handler) {\n    connIncrRefs(conn);\n    if (handler) handler(conn);\n    connDecrRefs(conn);\n    if (conn->flags & CONN_FLAG_CLOSE_SCHEDULED) {\n        if (!connHasRefs(conn)) connClose(conn);\n        return 0;\n    }\n    return 1;\n}\n\n#endif  /* __REDIS_CONNHELPERS_H */\n"
  },
  {
    "path": "src/cowptr.h",
    "content": "#pragma once\n\ntemplate <class T>\nclass CowPtr\n{\n    public:\n        typedef std::shared_ptr<T> RefPtr;\n\n    private:\n        mutable RefPtr m_sp;\n\n        void detach()\n        {\n            if( !( m_sp == nullptr || m_sp.unique() ) ) {\n                m_sp = std::make_shared<T>(*m_sp);\n            }\n        }\n\n    public:\n        CowPtr() = default;\n\n        CowPtr(const RefPtr& refptr)\n            :   m_sp(refptr)\n        {\n        }\n\n        bool operator==(std::nullptr_t) const\n        {\n            return m_sp == nullptr;\n        }\n\n        bool operator!=(std::nullptr_t) const\n        {\n            return m_sp != nullptr;\n        }\n        \n        const T& operator*() const\n        {\n            return *m_sp;\n        }\n        T& operator*()\n        {\n            detach();\n            return *m_sp;\n        }\n        const T* operator->() const\n        {\n            return m_sp.operator->();\n        }\n        T* operator->()\n        {\n            detach();\n            return m_sp.operator->();\n        }\n};\n"
  },
  {
    "path": "src/crc16.c",
    "content": "/*\n * Copyright 2001-2010 Georges Menie (www.menie.org)\n * Copyright 2010-2012 Salvatore Sanfilippo (adapted to Redis coding style)\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *     * Redistributions of source code must retain the above copyright\n *       notice, this list of conditions and the following disclaimer.\n *     * Redistributions in binary form must reproduce the above copyright\n *       notice, this list of conditions and the following disclaimer in the\n *       documentation and/or other materials provided with the distribution.\n *     * Neither the name of the University of California, Berkeley nor the\n *       names of its contributors may be used to endorse or promote products\n *       derived from this software without specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY\n * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY\n * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\n * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\n * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <inttypes.h>\n\n/* CRC16 implementation according to CCITT standards.\n *\n * Note by @antirez: this is actually the XMODEM CRC 16 algorithm, using the\n * following parameters:\n *\n * Name                       : \"XMODEM\", also known as \"ZMODEM\", \"CRC-16/ACORN\"\n * Width                      : 16 bit\n * Poly                       : 1021 (That is actually x^16 + x^12 + x^5 + 1)\n * Initialization             : 0000\n * Reflect Input byte         : False\n * Reflect Output CRC         : False\n * Xor constant to output CRC : 0000\n * Output for \"123456789\"     : 31C3\n */\n\nstatic const uint16_t crc16tab[256]= {\n    0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,\n    0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef,\n    0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6,\n    0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de,\n    0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485,\n    0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d,\n    0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4,\n    0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc,\n    0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823,\n    0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b,\n    0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12,\n    0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a,\n    0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41,\n    0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49,\n    0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70,\n    0xff9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78,\n    0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f,\n    0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067,\n    0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e,\n    0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256,\n    0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d,\n    0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405,\n    0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c,\n    0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634,\n    0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab,\n    0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3,\n    0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a,\n    0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92,\n    0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9,\n    0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1,\n    0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8,\n    0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0\n};\n\nuint16_t crc16(const char *buf, int len) {\n    int counter;\n    uint16_t crc = 0;\n    for (counter = 0; counter < len; counter++)\n            crc = (crc<<8) ^ crc16tab[((crc>>8) ^ *buf++)&0x00FF];\n    return crc;\n}\n"
  },
  {
    "path": "src/crc16_slottable.h",
    "content": "#ifndef _CRC16_TABLE_H__\n#define _CRC16_TABLE_H__\n\n/* A table of the shortest possible alphanumeric string that is mapped by redis' crc16\n * to any given redis cluster slot. \n * \n * The array indexes are slot numbers, so that given a desired slot, this string is guaranteed\n * to make redis cluster route a request to the shard holding this slot \n */\n\nconst char *crc16_slot_table[] = {\n\"06S\", \"Qi\", \"5L5\", \"4Iu\", \"4gY\", \"460\", \"1Y7\", \"1LV\", \"0QG\", \"ru\", \"7Ok\", \"4ji\", \"4DE\", \"65n\", \"2JH\", \"I8\", \"F9\", \"SX\", \"7nF\", \"4KD\", \n\"4eh\", \"6PK\", \"2ke\", \"1Ng\", \"0Sv\", \"4L\", \"491\", \"4hX\", \"4Ft\", \"5C4\", \"2Hy\", \"09R\", \"021\", \"0cX\", \"4Xv\", \"6mU\", \"6Cy\", \"42R\", \"0Mt\", \"nF\", \n\"cv\", \"1Pe\", \"5kK\", \"6NI\", \"74L\", \"4UF\", \"0nh\", \"MZ\", \"2TJ\", \"0ai\", \"4ZG\", \"6od\", \"6AH\", \"40c\", \"0OE\", \"lw\", \"aG\", \"0Bu\", \"5iz\", \"6Lx\", \n\"5R7\", \"4Ww\", \"0lY\", \"Ok\", \"5n3\", \"4ks\", \"8YE\", \"7g\", \"2KR\", \"1nP\", \"714\", \"64t\", \"69D\", \"4Ho\", \"07I\", \"Ps\", \"2hN\", \"1ML\", \"4fC\", \"7CA\", \n\"avs\", \"4iB\", \"0Rl\", \"5V\", \"2Ic\", \"08H\", \"4Gn\", \"66E\", \"aUo\", \"b4e\", \"05x\", \"RB\", \"8f\", \"8VD\", \"4dr\", \"5a2\", \"4zp\", \"6OS\", \"bl\", \"355\", \n\"0or\", \"1j2\", \"75V\", \"bno\", \"4Yl\", \"6lO\", \"Ap\", \"0bB\", \"0Ln\", \"2yM\", \"6Bc\", \"43H\", \"4xA\", \"6Mb\", \"22D\", \"14\", \"0mC\", \"Nq\", \"6cN\", \"4Vm\", \n\"ban\", \"aDl\", \"CA\", \"14Z\", \"8GG\", \"mm\", \"549\", \"41y\", \"53t\", \"464\", \"1Y3\", \"1LR\", \"06W\", \"Qm\", \"5L1\", \"4Iq\", \"4DA\", \"65j\", \"2JL\", \"1oN\", \n\"0QC\", \"6y\", \"7Oo\", \"4jm\", \"4el\", \"6PO\", \"9x\", \"1Nc\", \"04f\", \"2EM\", \"7nB\", \"bqs\", \"4Fp\", \"5C0\", \"d6F\", \"09V\", \"0Sr\", \"4H\", \"495\", \"bRo\", \n\"aio\", \"42V\", \"0Mp\", \"nB\", \"025\", \"17u\", \"4Xr\", \"6mQ\", \"74H\", \"4UB\", \"0nl\", \"3Kn\", \"cr\", \"1Pa\", \"5kO\", \"6NM\", \"6AL\", \"40g\", \"0OA\", \"ls\", \n\"2TN\", \"0am\", \"4ZC\", \"aEr\", \"5R3\", \"4Ws\", \"18t\", \"Oo\", \"aC\", \"0Bq\", \"bCl\", \"afn\", \"2KV\", \"1nT\", \"5Uz\", \"64p\", \"5n7\", \"4kw\", \"0PY\", \"7c\", \n\"2hJ\", \"1MH\", \"4fG\", \"6Sd\", \"7mi\", \"4Hk\", \"07M\", \"Pw\", \"2Ig\", \"08L\", \"4Gj\", \"66A\", \"7LD\", \"4iF\", \"0Rh\", \"5R\", \"8b\", \"1Oy\", \"4dv\", \"5a6\", \n\"7oX\", \"4JZ\", \"0qt\", \"RF\", \"0ov\", \"LD\", \"4A9\", \"4TX\", \"4zt\", \"6OW\", \"bh\", \"0AZ\", \"z9\", \"oX\", \"6Bg\", \"43L\", \"4Yh\", \"6lK\", \"At\", \"0bF\", \n\"0mG\", \"Nu\", \"6cJ\", \"4Vi\", \"4xE\", \"6Mf\", \"2vH\", \"10\", \"8GC\", \"mi\", \"5p5\", \"4uu\", \"5Kx\", \"4N8\", \"CE\", \"1pV\", \"0QO\", \"6u\", \"7Oc\", \"4ja\", \n\"4DM\", \"65f\", \"3Za\", \"I0\", \"0rS\", \"Qa\", \"68V\", \"b7F\", \"4gQ\", \"468\", \"dSo\", \"285\", \"274\", \"4D\", \"499\", \"4hP\", \"b8G\", \"67W\", \"0h3\", \"09Z\", \n\"F1\", \"SP\", \"7nN\", \"4KL\", \"51I\", \"6PC\", \"9t\", \"1No\", \"21g\", \"1Pm\", \"5kC\", \"6NA\", \"74D\", \"4UN\", \"X3\", \"MR\", \"029\", \"0cP\", \"bbM\", \"79t\", \n\"4c3\", \"42Z\", \"8Dd\", \"nN\", \"aO\", \"8Ke\", \"4yS\", \"4l2\", \"76u\", \"635\", \"0lQ\", \"Oc\", \"BS\", \"W2\", \"4ZO\", \"6ol\", \"7Qa\", \"40k\", \"0OM\", \"2zn\", \n\"69L\", \"4Hg\", \"07A\", \"2Fj\", \"2hF\", \"k6\", \"4fK\", \"6Sh\", \"7Ny\", \"6K9\", \"0PU\", \"7o\", \"2KZ\", \"1nX\", \"4EW\", \"4P6\", \"7oT\", \"4JV\", \"05p\", \"RJ\", \n\"8n\", \"1Ou\", \"4dz\", \"6QY\", \"7LH\", \"4iJ\", \"d7\", \"qV\", \"2Ik\", \"1li\", \"4Gf\", \"66M\", \"4Yd\", \"6lG\", \"Ax\", \"0bJ\", \"z5\", \"oT\", \"6Bk\", \"4wH\", \n\"4zx\", \"aeI\", \"bd\", \"0AV\", \"0oz\", \"LH\", \"4A5\", \"4TT\", \"5Kt\", \"4N4\", \"CI\", \"14R\", \"0NW\", \"me\", \"541\", \"41q\", \"4xI\", \"6Mj\", \"22L\", \"u4\", \n\"0mK\", \"Ny\", \"6cF\", \"4Ve\", \"4DI\", \"65b\", \"2JD\", \"I4\", \"0QK\", \"6q\", \"7Og\", \"4je\", \"4gU\", \"4r4\", \"2iX\", \"1LZ\", \"0rW\", \"Qe\", \"5L9\", \"4Iy\", \n\"4Fx\", \"5C8\", \"0h7\", \"1mw\", \"0Sz\", \"pH\", \"7MV\", \"4hT\", \"4ed\", \"6PG\", \"9p\", \"1Nk\", \"F5\", \"ST\", \"7nJ\", \"4KH\", \"7pH\", \"4UJ\", \"X7\", \"MV\", \n\"cz\", \"1Pi\", \"5kG\", \"6NE\", \"4c7\", \"4vV\", \"0Mx\", \"nJ\", \"0v5\", \"0cT\", \"4Xz\", \"6mY\", \"6bX\", \"5GZ\", \"0lU\", \"Og\", \"aK\", \"0By\", \"4yW\", \"4l6\", \n\"6AD\", \"40o\", \"0OI\", \"2zj\", \"BW\", \"W6\", \"4ZK\", \"6oh\", \"2hB\", \"k2\", \"4fO\", \"6Sl\", \"69H\", \"4Hc\", \"07E\", \"2Fn\", \"d5e\", \"83m\", \"4ES\", \"4P2\", \n\"a0F\", \"bQL\", \"0PQ\", \"7k\", \"8j\", \"1Oq\", \"50W\", \"hbv\", \"7oP\", \"4JR\", \"05t\", \"RN\", \"2Io\", \"08D\", \"4Gb\", \"66I\", \"7LL\", \"4iN\", \"d3\", \"5Z\", \n\"z1\", \"oP\", \"6Bo\", \"43D\", \"5IA\", \"6lC\", \"2Wm\", \"0bN\", \"8ff\", \"LL\", \"4A1\", \"4TP\", \"cPn\", \"aeM\", \"0T3\", \"0AR\", \"0NS\", \"ma\", \"545\", \"41u\", \n\"5Kp\", \"4N0\", \"CM\", \"14V\", \"0mO\", \"2Xl\", \"6cB\", \"4Va\", \"4xM\", \"6Mn\", \"22H\", \"18\", \"04s\", \"SI\", \"7nW\", \"4KU\", \"4ey\", \"6PZ\", \"9m\", \"1Nv\", \n\"e4\", \"pU\", \"7MK\", \"4hI\", \"4Fe\", \"67N\", \"2Hh\", \"09C\", \"06B\", \"Qx\", \"68O\", \"4Id\", \"4gH\", \"6Rk\", \"2iE\", \"j5\", \"0QV\", \"6l\", \"5o8\", \"4jx\", \n\"4DT\", \"4Q5\", \"2JY\", \"82j\", \"BJ\", \"0ax\", \"4ZV\", \"4O7\", \"552\", \"40r\", \"0OT\", \"lf\", \"aV\", \"t7\", \"4yJ\", \"6Li\", \"6bE\", \"4Wf\", \"0lH\", \"Oz\", \n\"2Vj\", \"0cI\", \"4Xg\", \"6mD\", \"6Ch\", \"42C\", \"0Me\", \"nW\", \"cg\", \"1Pt\", \"5kZ\", \"6NX\", \"7pU\", \"4UW\", \"0ny\", \"MK\", \"7LQ\", \"4iS\", \"267\", \"5G\", \n\"0i0\", \"08Y\", \"b9D\", \"66T\", \"7oM\", \"4JO\", \"G2\", \"RS\", \"8w\", \"1Ol\", \"4dc\", \"7Aa\", \"atS\", \"4kb\", \"0PL\", \"7v\", \"2KC\", \"H3\", \"4EN\", \"64e\", \n\"69U\", \"b6E\", \"07X\", \"Pb\", \"dRl\", \"296\", \"4fR\", \"4s3\", \"4xP\", \"4m1\", \"22U\", \"8Jf\", \"0mR\", \"0x3\", \"77v\", \"626\", \"5Km\", \"6no\", \"CP\", \"V1\", \n\"0NN\", \"3kL\", \"7Pb\", \"41h\", \"4za\", \"6OB\", \"20d\", \"0AO\", \"Y0\", \"LQ\", \"6an\", \"4TM\", \"bcN\", \"78w\", \"Aa\", \"0bS\", \"8Eg\", \"oM\", \"4b0\", \"43Y\", \n\"51T\", \"azL\", \"9i\", \"1Nr\", \"04w\", \"SM\", \"7nS\", \"4KQ\", \"4Fa\", \"67J\", \"2Hl\", \"09G\", \"e0\", \"4Y\", \"7MO\", \"4hM\", \"4gL\", \"6Ro\", \"2iA\", \"j1\", \n\"06F\", \"2Gm\", \"68K\", \"5YA\", \"4DP\", \"4Q1\", \"d4f\", \"82n\", \"0QR\", \"6h\", \"a1E\", \"bPO\", \"556\", \"40v\", \"0OP\", \"lb\", \"BN\", \"15U\", \"4ZR\", \"4O3\", \n\"6bA\", \"4Wb\", \"0lL\", \"2Yo\", \"aR\", \"t3\", \"4yN\", \"6Lm\", \"6Cl\", \"42G\", \"0Ma\", \"nS\", \"2Vn\", \"0cM\", \"4Xc\", \"79i\", \"74Y\", \"4US\", \"8ge\", \"MO\", \n\"cc\", \"1Pp\", \"bAL\", \"adN\", \"0i4\", \"1lt\", \"5WZ\", \"66P\", \"7LU\", \"4iW\", \"0Ry\", \"5C\", \"8s\", \"1Oh\", \"4dg\", \"6QD\", \"7oI\", \"4JK\", \"G6\", \"RW\", \n\"2KG\", \"H7\", \"4EJ\", \"64a\", \"7Nd\", \"4kf\", \"0PH\", \"7r\", \"1X8\", \"1MY\", \"4fV\", \"4s7\", \"69Q\", \"4Hz\", \"0sT\", \"Pf\", \"0mV\", \"Nd\", \"5S8\", \"4Vx\", \n\"4xT\", \"4m5\", \"22Q\", \"0Cz\", \"0NJ\", \"mx\", \"7Pf\", \"41l\", \"5Ki\", \"6nk\", \"CT\", \"V5\", \"Y4\", \"LU\", \"6aj\", \"4TI\", \"4ze\", \"6OF\", \"by\", \"0AK\", \n\"2l9\", \"oI\", \"4b4\", \"4wU\", \"4Yy\", \"6lZ\", \"Ae\", \"0bW\", \"0So\", \"4U\", \"7MC\", \"4hA\", \"4Fm\", \"67F\", \"3XA\", \"09K\", \"0ps\", \"SA\", \"aTl\", \"b5f\", \n\"4eq\", \"6PR\", \"9e\", \"8WG\", \"8XF\", \"6d\", \"5o0\", \"4jp\", \"707\", \"65w\", \"1z2\", \"1oS\", \"06J\", \"Qp\", \"68G\", \"4Il\", \"53i\", \"6Rc\", \"2iM\", \"1LO\", \n\"23G\", \"07\", \"4yB\", \"6La\", \"6bM\", \"4Wn\", \"18i\", \"Or\", \"BB\", \"0ap\", \"c4D\", \"aEo\", \"5q2\", \"40z\", \"8FD\", \"ln\", \"co\", \"346\", \"5kR\", \"6NP\", \n\"74U\", \"bol\", \"0nq\", \"MC\", \"2Vb\", \"0cA\", \"4Xo\", \"6mL\", \"7SA\", \"42K\", \"0Mm\", \"2xN\", \"7oE\", \"4JG\", \"05a\", \"2DJ\", \"2jf\", \"1Od\", \"4dk\", \"6QH\", \n\"482\", \"5yz\", \"0Ru\", \"5O\", \"0i8\", \"08Q\", \"4Gw\", \"5B7\", \"5M6\", \"4Hv\", \"07P\", \"Pj\", \"1X4\", \"1MU\", \"4fZ\", \"473\", \"7Nh\", \"4kj\", \"0PD\", \"sv\", \n\"2KK\", \"1nI\", \"4EF\", \"64m\", \"5Ke\", \"6ng\", \"CX\", \"V9\", \"0NF\", \"mt\", \"7Pj\", \"4uh\", \"4xX\", \"4m9\", \"1F6\", \"0Cv\", \"0mZ\", \"Nh\", \"5S4\", \"4Vt\", \n\"4Yu\", \"6lV\", \"Ai\", \"16r\", \"0Lw\", \"oE\", \"4b8\", \"43Q\", \"4zi\", \"6OJ\", \"bu\", \"0AG\", \"Y8\", \"LY\", \"6af\", \"4TE\", \"4Fi\", \"67B\", \"2Hd\", \"09O\", \n\"e8\", \"4Q\", \"7MG\", \"4hE\", \"4eu\", \"6PV\", \"9a\", \"1Nz\", \"0pw\", \"SE\", \"aTh\", \"4KY\", \"4DX\", \"4Q9\", \"1z6\", \"1oW\", \"0QZ\", \"rh\", \"5o4\", \"4jt\", \n\"4gD\", \"6Rg\", \"2iI\", \"j9\", \"06N\", \"Qt\", \"68C\", \"4Ih\", \"6bI\", \"4Wj\", \"0lD\", \"Ov\", \"aZ\", \"03\", \"4yF\", \"6Le\", \"5q6\", \"4tv\", \"0OX\", \"lj\", \n\"BF\", \"0at\", \"4ZZ\", \"6oy\", \"74Q\", \"5Ez\", \"0nu\", \"MG\", \"ck\", \"1Px\", \"5kV\", \"6NT\", \"6Cd\", \"42O\", \"0Mi\", \"2xJ\", \"2Vf\", \"0cE\", \"4Xk\", \"6mH\", \n\"2jb\", \"8VY\", \"4do\", \"6QL\", \"7oA\", \"4JC\", \"05e\", \"2DN\", \"d7E\", \"08U\", \"4Gs\", \"5B3\", \"486\", \"bSl\", \"0Rq\", \"5K\", \"1X0\", \"1MQ\", \"52w\", \"477\", \n\"5M2\", \"4Hr\", \"07T\", \"Pn\", \"2KO\", \"1nM\", \"4EB\", \"64i\", \"7Nl\", \"4kn\", \"8YX\", \"7z\", \"0NB\", \"mp\", \"7Pn\", \"41d\", \"5Ka\", \"6nc\", \"2UM\", \"14G\", \n\"19w\", \"Nl\", \"5S0\", \"4Vp\", \"bBo\", \"agm\", \"1F2\", \"0Cr\", \"0Ls\", \"oA\", \"ahl\", \"43U\", \"4Yq\", \"6lR\", \"Am\", \"16v\", \"0oo\", \"2ZL\", \"6ab\", \"4TA\", \n\"4zm\", \"6ON\", \"bq\", \"0AC\", \"2VY\", \"0cz\", \"4XT\", \"4M5\", \"570\", \"42p\", \"0MV\", \"nd\", \"cT\", \"v5\", \"5ki\", \"6Nk\", \"74n\", \"4Ud\", \"0nJ\", \"Mx\", \n\"By\", \"0aK\", \"4Ze\", \"6oF\", \"6Aj\", \"40A\", \"y4\", \"lU\", \"ae\", \"0BW\", \"4yy\", \"581\", \"4B4\", \"4WU\", \"18R\", \"OI\", \"06q\", \"QK\", \"7lU\", \"4IW\", \n\"53R\", \"6RX\", \"0I4\", \"1Lt\", \"g6\", \"rW\", \"7OI\", \"4jK\", \"4Dg\", \"65L\", \"2Jj\", \"1oh\", \"0pH\", \"Sz\", \"7nd\", \"4Kf\", \"4eJ\", \"6Pi\", \"2kG\", \"h7\", \n\"0ST\", \"4n\", \"7Mx\", \"4hz\", \"4FV\", \"4S7\", \"1x8\", \"09p\", \"4zR\", \"4o3\", \"bN\", \"8Hd\", \"0oP\", \"Lb\", \"75t\", \"604\", \"4YN\", \"6lm\", \"AR\", \"T3\", \n\"0LL\", \"2yo\", \"6BA\", \"43j\", \"4xc\", \"agR\", \"22f\", \"0CM\", \"0ma\", \"NS\", \"6cl\", \"4VO\", \"baL\", \"aDN\", \"Cc\", \"14x\", \"8Ge\", \"mO\", \"7PQ\", \"4uS\", \n\"7NS\", \"4kQ\", \"245\", \"7E\", \"0k2\", \"1nr\", \"coo\", \"64V\", \"69f\", \"4HM\", \"E0\", \"PQ\", \"2hl\", \"1Mn\", \"4fa\", \"6SB\", \"7Lb\", \"5yA\", \"0RN\", \"5t\", \n\"2IA\", \"J1\", \"4GL\", \"66g\", \"aUM\", \"b4G\", \"05Z\", \"0d3\", \"8D\", \"8Vf\", \"4dP\", \"459\", \"574\", \"42t\", \"0MR\", \"0X3\", \"dln\", \"17W\", \"4XP\", \"4M1\", \n\"74j\", \"5EA\", \"0nN\", \"3KL\", \"cP\", \"29\", \"5km\", \"6No\", \"6An\", \"40E\", \"y0\", \"lQ\", \"2Tl\", \"0aO\", \"4Za\", \"6oB\", \"4B0\", \"4WQ\", \"18V\", \"OM\", \n\"aa\", \"0BS\", \"bCN\", \"585\", \"53V\", \"axN\", \"0I0\", \"1Lp\", \"06u\", \"QO\", \"68x\", \"4IS\", \"4Dc\", \"65H\", \"2Jn\", \"1ol\", \"g2\", \"rS\", \"7OM\", \"4jO\", \n\"4eN\", \"6Pm\", \"9Z\", \"h3\", \"04D\", \"2Eo\", \"aTS\", \"4Kb\", \"4FR\", \"4S3\", \"d6d\", \"09t\", \"0SP\", \"4j\", \"a3G\", \"bRM\", \"0oT\", \"Lf\", \"6aY\", \"4Tz\", \n\"4zV\", \"4o7\", \"bJ\", \"0Ax\", \"0LH\", \"oz\", \"6BE\", \"43n\", \"4YJ\", \"6li\", \"AV\", \"T7\", \"0me\", \"NW\", \"6ch\", \"4VK\", \"4xg\", \"6MD\", \"22b\", \"0CI\", \n\"0Ny\", \"mK\", \"7PU\", \"4uW\", \"5KZ\", \"6nX\", \"Cg\", \"1pt\", \"0k6\", \"1nv\", \"4Ey\", \"64R\", \"7NW\", \"4kU\", \"241\", \"7A\", \"2hh\", \"1Mj\", \"4fe\", \"6SF\", \n\"69b\", \"4HI\", \"E4\", \"PU\", \"2IE\", \"J5\", \"4GH\", \"66c\", \"7Lf\", \"4id\", \"0RJ\", \"5p\", \"2jY\", \"8Vb\", \"4dT\", \"4q5\", \"5O8\", \"4Jx\", \"0qV\", \"Rd\", \n\"21E\", \"25\", \"5ka\", \"6Nc\", \"74f\", \"4Ul\", \"0nB\", \"Mp\", \"1f2\", \"0cr\", \"bbo\", \"79V\", \"578\", \"42x\", \"395\", \"nl\", \"am\", \"364\", \"4yq\", \"589\", \n\"76W\", \"bmn\", \"0ls\", \"OA\", \"Bq\", \"0aC\", \"4Zm\", \"6oN\", \"6Ab\", \"40I\", \"0Oo\", \"2zL\", \"0Qm\", \"6W\", \"7OA\", \"4jC\", \"4Do\", \"65D\", \"2Jb\", \"82Q\", \n\"06y\", \"QC\", \"68t\", \"b7d\", \"4gs\", \"5b3\", \"dSM\", \"8UE\", \"8ZD\", \"4f\", \"5m2\", \"4hr\", \"725\", \"67u\", \"1x0\", \"09x\", \"04H\", \"Sr\", \"7nl\", \"4Kn\", \n\"4eB\", \"6Pa\", \"9V\", \"1NM\", \"4YF\", \"6le\", \"AZ\", \"0bh\", \"0LD\", \"ov\", \"6BI\", \"43b\", \"4zZ\", \"6Oy\", \"bF\", \"0At\", \"0oX\", \"Lj\", \"5Q6\", \"4Tv\", \n\"5KV\", \"6nT\", \"Ck\", \"14p\", \"0Nu\", \"mG\", \"7PY\", \"41S\", \"4xk\", \"6MH\", \"22n\", \"0CE\", \"0mi\", \"2XJ\", \"6cd\", \"4VG\", \"69n\", \"4HE\", \"E8\", \"PY\", \n\"2hd\", \"1Mf\", \"4fi\", \"6SJ\", \"ath\", \"4kY\", \"0Pw\", \"7M\", \"2Kx\", \"1nz\", \"4Eu\", \"6pV\", \"5O4\", \"4Jt\", \"05R\", \"Rh\", \"8L\", \"1OW\", \"4dX\", \"451\", \n\"7Lj\", \"4ih\", \"0RF\", \"qt\", \"2II\", \"J9\", \"4GD\", \"66o\", \"74b\", \"4Uh\", \"0nF\", \"Mt\", \"cX\", \"21\", \"5ke\", \"6Ng\", \"5s4\", \"4vt\", \"0MZ\", \"nh\", \n\"1f6\", \"0cv\", \"4XX\", \"4M9\", \"4B8\", \"4WY\", \"0lw\", \"OE\", \"ai\", \"1Rz\", \"4yu\", \"6LV\", \"6Af\", \"40M\", \"y8\", \"lY\", \"Bu\", \"0aG\", \"4Zi\", \"6oJ\", \n\"4Dk\", \"6qH\", \"2Jf\", \"1od\", \"0Qi\", \"6S\", \"7OE\", \"4jG\", \"4gw\", \"5b7\", \"0I8\", \"1Lx\", \"0ru\", \"QG\", \"68p\", \"5Yz\", \"4FZ\", \"67q\", \"1x4\", \"1mU\", \n\"0SX\", \"4b\", \"5m6\", \"4hv\", \"4eF\", \"6Pe\", \"9R\", \"1NI\", \"04L\", \"Sv\", \"7nh\", \"4Kj\", \"8EX\", \"or\", \"6BM\", \"43f\", \"4YB\", \"6la\", \"2WO\", \"0bl\", \n\"8fD\", \"Ln\", \"5Q2\", \"4Tr\", \"cPL\", \"aeo\", \"bB\", \"0Ap\", \"0Nq\", \"mC\", \"ajn\", \"41W\", \"5KR\", \"6nP\", \"Co\", \"14t\", \"0mm\", \"2XN\", \"77I\", \"4VC\", \n\"4xo\", \"6ML\", \"22j\", \"0CA\", \"3xA\", \"1Mb\", \"4fm\", \"6SN\", \"69j\", \"4HA\", \"07g\", \"2FL\", \"d5G\", \"83O\", \"4Eq\", \"64Z\", \"a0d\", \"bQn\", \"0Ps\", \"7I\", \n\"8H\", \"1OS\", \"50u\", \"455\", \"5O0\", \"4Jp\", \"05V\", \"Rl\", \"2IM\", \"08f\", \"5Wa\", \"66k\", \"7Ln\", \"4il\", \"0RB\", \"5x\", \"Bh\", \"0aZ\", \"4Zt\", \"6oW\", \n\"4a9\", \"40P\", \"0Ov\", \"lD\", \"at\", \"0BF\", \"4yh\", \"6LK\", \"6bg\", \"4WD\", \"Z9\", \"OX\", \"2VH\", \"U8\", \"4XE\", \"6mf\", \"6CJ\", \"42a\", \"0MG\", \"nu\", \n\"cE\", \"1PV\", \"5kx\", \"4n8\", \"5P5\", \"4Uu\", \"8gC\", \"Mi\", \"04Q\", \"Sk\", \"5N7\", \"4Kw\", \"51r\", \"442\", \"9O\", \"1NT\", \"0SE\", \"pw\", \"7Mi\", \"4hk\", \n\"4FG\", \"67l\", \"2HJ\", \"09a\", \"3\", \"QZ\", \"68m\", \"4IF\", \"4gj\", \"6RI\", \"2ig\", \"1Le\", \"0Qt\", \"6N\", \"7OX\", \"4jZ\", \"4Dv\", \"5A6\", \"0j9\", \"1oy\", \n\"4xr\", \"6MQ\", \"22w\", \"377\", \"0mp\", \"NB\", \"77T\", \"blm\", \"5KO\", \"6nM\", \"Cr\", \"14i\", \"0Nl\", \"3kn\", \"ajs\", \"41J\", \"4zC\", \"aer\", \"20F\", \"36\", \n\"0oA\", \"Ls\", \"6aL\", \"4To\", \"bcl\", \"78U\", \"AC\", \"0bq\", \"386\", \"oo\", \"5r3\", \"4ws\", \"5l1\", \"4iq\", \"9Kf\", \"5e\", \"1y3\", \"1lR\", \"736\", \"66v\", \n\"7oo\", \"4Jm\", \"05K\", \"Rq\", \"8U\", \"1ON\", \"4dA\", \"6Qb\", \"7NB\", \"bQs\", \"0Pn\", \"7T\", \"2Ka\", \"1nc\", \"4El\", \"64G\", \"69w\", \"b6g\", \"07z\", \"1v2\", \n\"dRN\", \"8TF\", \"4fp\", \"5c0\", \"akm\", \"40T\", \"0Or\", \"1J2\", \"Bl\", \"15w\", \"4Zp\", \"6oS\", \"6bc\", \"5Ga\", \"0ln\", \"2YM\", \"ap\", \"0BB\", \"4yl\", \"6LO\", \n\"6CN\", \"42e\", \"0MC\", \"nq\", \"2VL\", \"0co\", \"4XA\", \"6mb\", \"5P1\", \"4Uq\", \"8gG\", \"Mm\", \"cA\", \"1PR\", \"bAn\", \"adl\", \"51v\", \"446\", \"9K\", \"1NP\", \n\"04U\", \"So\", \"5N3\", \"4Ks\", \"4FC\", \"67h\", \"2HN\", \"09e\", \"0SA\", \"ps\", \"7Mm\", \"4ho\", \"4gn\", \"6RM\", \"2ic\", \"1La\", \"7\", \"2GO\", \"68i\", \"4IB\", \n\"4Dr\", \"5A2\", \"d4D\", \"82L\", \"0Qp\", \"6J\", \"a1g\", \"bPm\", \"0mt\", \"NF\", \"6cy\", \"4VZ\", \"4xv\", \"6MU\", \"0V9\", \"0CX\", \"0Nh\", \"mZ\", \"7PD\", \"41N\", \n\"5KK\", \"6nI\", \"Cv\", \"14m\", \"0oE\", \"Lw\", \"6aH\", \"4Tk\", \"4zG\", \"6Od\", \"20B\", \"32\", \"0LY\", \"ok\", \"5r7\", \"4ww\", \"5Iz\", \"6lx\", \"AG\", \"0bu\", \n\"1y7\", \"1lV\", \"4GY\", \"4R8\", \"5l5\", \"4iu\", \"1Bz\", \"5a\", \"8Q\", \"i8\", \"4dE\", \"6Qf\", \"7ok\", \"4Ji\", \"05O\", \"Ru\", \"2Ke\", \"1ng\", \"4Eh\", \"64C\", \n\"7NF\", \"4kD\", \"f9\", \"7P\", \"2hy\", \"3m9\", \"4ft\", \"5c4\", \"69s\", \"4HX\", \"0sv\", \"PD\", \"23e\", \"0BN\", \"5iA\", \"6LC\", \"6bo\", \"4WL\", \"Z1\", \"OP\", \n\"0t3\", \"0aR\", \"c4f\", \"aEM\", \"4a1\", \"40X\", \"8Ff\", \"lL\", \"cM\", \"8Ig\", \"5kp\", \"4n0\", \"74w\", \"617\", \"0nS\", \"Ma\", \"3Fa\", \"U0\", \"4XM\", \"6mn\", \n\"6CB\", \"42i\", \"0MO\", \"2xl\", \"0SM\", \"4w\", \"7Ma\", \"4hc\", \"4FO\", \"67d\", \"2HB\", \"K2\", \"04Y\", \"Sc\", \"aTN\", \"b5D\", \"4eS\", \"4p2\", \"9G\", \"8We\", \n\"256\", \"6F\", \"7OP\", \"4jR\", \"cnl\", \"65U\", \"0j1\", \"1oq\", \"D3\", \"QR\", \"68e\", \"4IN\", \"4gb\", \"6RA\", \"2io\", \"1Lm\", \"5KG\", \"6nE\", \"Cz\", \"14a\", \n\"x7\", \"mV\", \"7PH\", \"41B\", \"4xz\", \"592\", \"0V5\", \"0CT\", \"0mx\", \"NJ\", \"4C7\", \"4VV\", \"4YW\", \"4L6\", \"AK\", \"0by\", \"0LU\", \"og\", \"563\", \"43s\", \n\"4zK\", \"6Oh\", \"bW\", \"w6\", \"0oI\", \"2Zj\", \"6aD\", \"4Tg\", \"7og\", \"4Je\", \"05C\", \"Ry\", \"2jD\", \"i4\", \"4dI\", \"6Qj\", \"5l9\", \"4iy\", \"0RW\", \"5m\", \n\"2IX\", \"08s\", \"4GU\", \"4R4\", \"7mV\", \"4HT\", \"07r\", \"PH\", \"0H7\", \"1Mw\", \"4fx\", \"5c8\", \"7NJ\", \"4kH\", \"f5\", \"sT\", \"2Ki\", \"1nk\", \"4Ed\", \"64O\", \n\"6bk\", \"4WH\", \"Z5\", \"OT\", \"ax\", \"0BJ\", \"4yd\", \"6LG\", \"4a5\", \"4tT\", \"0Oz\", \"lH\", \"Bd\", \"0aV\", \"4Zx\", \"aEI\", \"5P9\", \"4Uy\", \"0nW\", \"Me\", \n\"cI\", \"1PZ\", \"5kt\", \"4n4\", \"6CF\", \"42m\", \"0MK\", \"ny\", \"2VD\", \"U4\", \"4XI\", \"6mj\", \"4FK\", \"6sh\", \"2HF\", \"K6\", \"0SI\", \"4s\", \"7Me\", \"4hg\", \n\"4eW\", \"4p6\", \"9C\", \"1NX\", \"0pU\", \"Sg\", \"7ny\", \"6k9\", \"4Dz\", \"65Q\", \"0j5\", \"1ou\", \"0Qx\", \"6B\", \"7OT\", \"4jV\", \"4gf\", \"6RE\", \"2ik\", \"1Li\", \n\"D7\", \"QV\", \"68a\", \"4IJ\", \"x3\", \"mR\", \"7PL\", \"41F\", \"5KC\", \"6nA\", \"2Uo\", \"14e\", \"19U\", \"NN\", \"4C3\", \"4VR\", \"bBM\", \"596\", \"0V1\", \"0CP\", \n\"0LQ\", \"oc\", \"567\", \"43w\", \"4YS\", \"4L2\", \"AO\", \"16T\", \"0oM\", \"2Zn\", \"75i\", \"4Tc\", \"4zO\", \"6Ol\", \"bS\", \"w2\", \"8Y\", \"i0\", \"4dM\", \"6Qn\", \n\"7oc\", \"4Ja\", \"05G\", \"2Dl\", \"d7g\", \"08w\", \"4GQ\", \"4R0\", \"a2D\", \"bSN\", \"0RS\", \"5i\", \"0H3\", \"1Ms\", \"52U\", \"ayM\", \"7mR\", \"4HP\", \"07v\", \"PL\", \n\"2Km\", \"1no\", \"5UA\", \"64K\", \"7NN\", \"4kL\", \"f1\", \"7X\", \"5nw\", \"4k7\", \"fJ\", \"0Ex\", \"0kT\", \"Hf\", \"6eY\", \"4Pz\", \"5Mk\", \"6hi\", \"EV\", \"P7\", \n\"0HH\", \"kz\", \"6FE\", \"47n\", \"48o\", \"6ID\", \"26b\", \"0GI\", \"0ie\", \"JW\", \"6gh\", \"4RK\", \"5OZ\", \"6jX\", \"Gg\", \"0dU\", \"0Jy\", \"iK\", \"4d6\", \"4qW\", \n\"4z4\", \"4oU\", \"1DZ\", \"3A\", \"Ye\", \"0zW\", \"4Ay\", \"5D9\", \"6yj\", \"4LI\", \"A4\", \"TU\", \"zy\", \"0YK\", \"4be\", \"6WF\", \"6XG\", \"4md\", \"0VJ\", \"1p\", \n\"2ME\", \"N5\", \"4CH\", \"62c\", \"5K8\", \"4Nx\", \"0uV\", \"Vd\", \"xH\", \"8Rb\", \"5pu\", \"4u5\", \"D\", \"13W\", \"5Lq\", \"4I1\", \"534\", \"46t\", \"0IR\", \"28y\", \n\"gP\", \"69\", \"5om\", \"6Jo\", \"6dC\", \"5AA\", \"0jN\", \"3OL\", \"2Pl\", \"0eO\", \"aT1\", \"6kB\", \"6En\", \"44E\", \"98\", \"hQ\", \"ea\", \"0FS\", \"49u\", \"abL\", \n\"4F0\", \"4SQ\", \"8ag\", \"KM\", \"02u\", \"UO\", \"4X2\", \"4MS\", \"57V\", \"a8F\", \"0M0\", \"0XQ\", \"c2\", \"vS\", \"7KM\", \"4nO\", \"5PB\", \"61H\", \"2Nn\", \"1kl\", \n\"00D\", \"2Ao\", \"6zA\", \"4Ob\", \"4aN\", \"6Tm\", \"yR\", \"l3\", \"0WP\", \"0j\", \"a7G\", \"58W\", \"4BR\", \"4W3\", \"ZN\", \"84l\", \"0kP\", \"Hb\", \"71t\", \"644\", \n\"5ns\", \"4k3\", \"fN\", \"8Ld\", \"0HL\", \"29g\", \"6FA\", \"47j\", \"5Mo\", \"6hm\", \"ER\", \"P3\", \"0ia\", \"JS\", \"6gl\", \"4RO\", \"48k\", \"7Ya\", \"26f\", \"0GM\", \n\"8Ce\", \"iO\", \"4d2\", \"4qS\", \"beL\", \"hYw\", \"Gc\", \"0dQ\", \"Ya\", \"0zS\", \"cko\", \"60V\", \"4z0\", \"4oQ\", \"205\", \"3E\", \"2ll\", \"0YO\", \"4ba\", \"6WB\", \n\"6yn\", \"4LM\", \"A0\", \"TQ\", \"2MA\", \"N1\", \"4CL\", \"62g\", \"6XC\", \"59I\", \"0VN\", \"1t\", \"xL\", \"8Rf\", \"54y\", \"419\", \"aQM\", \"b0G\", \"01Z\", \"3PP\", \n\"530\", \"46p\", \"0IV\", \"jd\", \"DH\", \"0gz\", \"5Lu\", \"4I5\", \"6dG\", \"4Qd\", \"0jJ\", \"Ix\", \"gT\", \"r5\", \"5oi\", \"6Jk\", \"6Ej\", \"44A\", \"0Kg\", \"hU\", \n\"Fy\", \"0eK\", \"5ND\", \"6kF\", \"4F4\", \"4SU\", \"1xZ\", \"KI\", \"ee\", \"0FW\", \"49q\", \"5x9\", \"57R\", \"6VX\", \"0M4\", \"0XU\", \"02q\", \"UK\", \"4X6\", \"4MW\", \n\"5PF\", \"61L\", \"2Nj\", \"1kh\", \"c6\", \"vW\", \"7KI\", \"4nK\", \"4aJ\", \"6Ti\", \"yV\", \"l7\", \"0tH\", \"Wz\", \"6zE\", \"4Of\", \"4BV\", \"4W7\", \"ZJ\", \"0yx\", \n\"0WT\", \"0n\", \"6YY\", \"4lz\", \"5Mc\", \"6ha\", \"2SO\", \"0fl\", \"1Xa\", \"kr\", \"6FM\", \"47f\", \"bDm\", \"aao\", \"fB\", \"0Ep\", \"8bD\", \"Hn\", \"5U2\", \"4Pr\", \n\"5OR\", \"5Z3\", \"Go\", \"10t\", \"0Jq\", \"iC\", \"ann\", \"45W\", \"48g\", \"6IL\", \"ds\", \"0GA\", \"0im\", \"3Lo\", \"73I\", \"4RC\", \"6yb\", \"4LA\", \"03g\", \"2BL\", \n\"zq\", \"0YC\", \"4bm\", \"6WN\", \"a4d\", \"bUn\", \"0Ts\", \"3I\", \"Ym\", \"87O\", \"4Aq\", \"5D1\", \"5K0\", \"4Np\", \"01V\", \"Vl\", \"2nQ\", \"1KS\", \"54u\", \"415\", \n\"6XO\", \"4ml\", \"0VB\", \"1x\", \"2MM\", \"0xn\", \"5Sa\", \"62k\", \"gX\", \"61\", \"5oe\", \"6Jg\", \"6dK\", \"4Qh\", \"0jF\", \"It\", \"L\", \"0gv\", \"5Ly\", \"4I9\", \n\"5w4\", \"4rt\", \"0IZ\", \"jh\", \"ei\", \"1Vz\", \"5mT\", \"5x5\", \"4F8\", \"4SY\", \"0hw\", \"KE\", \"Fu\", \"0eG\", \"5NH\", \"6kJ\", \"6Ef\", \"44M\", \"90\", \"hY\", \n\"0Ui\", \"2S\", \"7KE\", \"4nG\", \"5PJ\", \"6uH\", \"Xw\", \"1kd\", \"0vu\", \"UG\", \"6xx\", \"790\", \"4cw\", \"5f7\", \"0M8\", \"0XY\", \"0WX\", \"0b\", \"5i6\", \"4lv\", \n\"4BZ\", \"63q\", \"ZF\", \"0yt\", \"00L\", \"Wv\", \"6zI\", \"4Oj\", \"4aF\", \"6Te\", \"yZ\", \"0Zh\", \"0HD\", \"kv\", \"6FI\", \"47b\", \"5Mg\", \"6he\", \"EZ\", \"0fh\", \n\"0kX\", \"Hj\", \"5U6\", \"4Pv\", \"7N9\", \"6Ky\", \"fF\", \"0Et\", \"0Ju\", \"iG\", \"6Dx\", \"45S\", \"5OV\", \"5Z7\", \"Gk\", \"0dY\", \"0ii\", \"3Lk\", \"6gd\", \"4RG\", \n\"48c\", \"6IH\", \"dw\", \"0GE\", \"zu\", \"0YG\", \"4bi\", \"6WJ\", \"6yf\", \"4LE\", \"A8\", \"TY\", \"Yi\", \"1jz\", \"4Au\", \"5D5\", \"4z8\", \"4oY\", \"0Tw\", \"3M\", \n\"xD\", \"1KW\", \"54q\", \"411\", \"5K4\", \"4Nt\", \"01R\", \"Vh\", \"2MI\", \"N9\", \"4CD\", \"62o\", \"6XK\", \"4mh\", \"0VF\", \"ut\", \"6dO\", \"4Ql\", \"0jB\", \"Ip\", \n\"25E\", \"65\", \"5oa\", \"6Jc\", \"538\", \"46x\", \"9Pg\", \"jl\", \"H\", \"0gr\", \"bfo\", \"aCm\", \"72W\", \"bin\", \"0hs\", \"KA\", \"em\", \"324\", \"49y\", \"5x1\", \n\"6Eb\", \"44I\", \"94\", \"3nm\", \"Fq\", \"0eC\", \"5NL\", \"6kN\", \"5PN\", \"61D\", \"Xs\", \"86Q\", \"0Um\", \"2W\", \"7KA\", \"4nC\", \"4cs\", \"5f3\", \"39W\", \"8QE\", \n\"02y\", \"UC\", \"aRn\", \"794\", \"765\", \"63u\", \"ZB\", \"0yp\", \"9Ne\", \"0f\", \"5i2\", \"4lr\", \"4aB\", \"6Ta\", \"2oO\", \"0Zl\", \"00H\", \"Wr\", \"6zM\", \"4On\", \n\"5lW\", \"5y6\", \"dj\", \"0GX\", \"0it\", \"JF\", \"6gy\", \"4RZ\", \"5OK\", \"6jI\", \"Gv\", \"0dD\", \"83\", \"iZ\", \"6De\", \"45N\", \"5nf\", \"6Kd\", \"24B\", \"72\", \n\"0kE\", \"Hw\", \"6eH\", \"4Pk\", \"5Mz\", \"6hx\", \"EG\", \"0fu\", \"0HY\", \"kk\", \"5v7\", \"4sw\", \"5h5\", \"4mu\", \"1Fz\", \"1a\", \"2MT\", \"0xw\", \"4CY\", \"4V8\", \n\"7kk\", \"4Ni\", \"01O\", \"Vu\", \"xY\", \"m8\", \"54l\", \"6Uf\", \"6Zg\", \"4oD\", \"b9\", \"3P\", \"Yt\", \"0zF\", \"4Ah\", \"60C\", \"4Y9\", \"4LX\", \"0wv\", \"TD\", \n\"zh\", \"0YZ\", \"4bt\", \"5g4\", \"Fl\", \"11w\", \"5NQ\", \"6kS\", \"aom\", \"44T\", \"0Kr\", \"1N2\", \"ep\", \"0FB\", \"49d\", \"6HO\", \"6fc\", \"5Ca\", \"0hn\", \"3Ml\", \n\"U\", \"0go\", \"bfr\", \"6ib\", \"6GN\", \"46e\", \"0IC\", \"jq\", \"gA\", \"0Ds\", \"bEn\", \"hyU\", \"5T1\", \"4Qq\", \"8cG\", \"Im\", \"00U\", \"Wo\", \"5J3\", \"4Os\", \n\"55v\", \"406\", \"yC\", \"0Zq\", \"0WA\", \"ts\", \"6YL\", \"4lo\", \"4BC\", \"63h\", \"2LN\", \"0ym\", \"02d\", \"2CO\", \"6xa\", \"4MB\", \"4cn\", \"6VM\", \"2mc\", \"1Ha\", \n\"0Up\", \"2J\", \"a5g\", \"bTm\", \"5PS\", \"5E2\", \"Xn\", \"86L\", \"0ip\", \"JB\", \"73T\", \"bhm\", \"48z\", \"5y2\", \"dn\", \"337\", \"87\", \"3on\", \"6Da\", \"45J\", \n\"5OO\", \"6jM\", \"Gr\", \"10i\", \"0kA\", \"Hs\", \"6eL\", \"4Po\", \"5nb\", \"aar\", \"24F\", \"76\", \"8AE\", \"ko\", \"5v3\", \"4ss\", \"bgl\", \"aBn\", \"EC\", \"0fq\", \n\"2MP\", \"0xs\", \"776\", \"62v\", \"5h1\", \"4mq\", \"9Of\", \"1e\", \"2nL\", \"1KN\", \"54h\", \"6Ub\", \"7ko\", \"4Nm\", \"01K\", \"Vq\", \"Yp\", \"0zB\", \"4Al\", \"60G\", \n\"6Zc\", \"bUs\", \"0Tn\", \"3T\", \"zl\", \"8PF\", \"4bp\", \"5g0\", \"aSm\", \"787\", \"03z\", \"1r2\", \"4e9\", \"44P\", \"0Kv\", \"hD\", \"Fh\", \"0eZ\", \"5NU\", \"6kW\", \n\"6fg\", \"4SD\", \"0hj\", \"KX\", \"et\", \"0FF\", \"5mI\", \"6HK\", \"6GJ\", \"46a\", \"0IG\", \"ju\", \"Q\", \"Q8\", \"5Ld\", \"6if\", \"5T5\", \"4Qu\", \"1zz\", \"Ii\", \n\"gE\", \"0Dw\", \"5ox\", \"4j8\", \"55r\", \"402\", \"yG\", \"0Zu\", \"00Q\", \"Wk\", \"5J7\", \"4Ow\", \"4BG\", \"63l\", \"2LJ\", \"0yi\", \"0WE\", \"tw\", \"6YH\", \"4lk\", \n\"4cj\", \"6VI\", \"2mg\", \"0XD\", \"0vh\", \"UZ\", \"6xe\", \"4MF\", \"5PW\", \"5E6\", \"Xj\", \"1ky\", \"0Ut\", \"2N\", \"7KX\", \"4nZ\", \"5OC\", \"6jA\", \"2Qo\", \"0dL\", \n\"1ZA\", \"iR\", \"6Dm\", \"45F\", \"48v\", \"acO\", \"db\", \"0GP\", \"94M\", \"JN\", \"4G3\", \"4RR\", \"5Mr\", \"4H2\", \"EO\", \"12T\", \"0HQ\", \"kc\", \"527\", \"47w\", \n\"5nn\", \"6Kl\", \"fS\", \"s2\", \"0kM\", \"3NO\", \"71i\", \"4Pc\", \"7kc\", \"4Na\", \"01G\", \"3PM\", \"xQ\", \"m0\", \"54d\", \"6Un\", \"a6D\", \"59T\", \"0VS\", \"1i\", \n\"197\", \"85o\", \"4CQ\", \"4V0\", \"4Y1\", \"4LP\", \"03v\", \"TL\", \"0L3\", \"0YR\", \"56U\", \"a9E\", \"6Zo\", \"4oL\", \"b1\", \"3X\", \"2Om\", \"0zN\", \"5QA\", \"60K\", \n\"ex\", \"0FJ\", \"49l\", \"6HG\", \"6fk\", \"4SH\", \"0hf\", \"KT\", \"Fd\", \"0eV\", \"5NY\", \"aAI\", \"4e5\", \"4pT\", \"0Kz\", \"hH\", \"gI\", \"1TZ\", \"5ot\", \"4j4\", \n\"5T9\", \"4Qy\", \"0jW\", \"Ie\", \"DU\", \"Q4\", \"5Lh\", \"6ij\", \"6GF\", \"46m\", \"0IK\", \"jy\", \"0WI\", \"0s\", \"6YD\", \"4lg\", \"4BK\", \"6wh\", \"ZW\", \"O6\", \n\"0tU\", \"Wg\", \"6zX\", \"6o9\", \"4aW\", \"4t6\", \"yK\", \"0Zy\", \"0Ux\", \"2B\", \"7KT\", \"4nV\", \"bzI\", \"61Q\", \"Xf\", \"1ku\", \"02l\", \"UV\", \"6xi\", \"4MJ\", \n\"4cf\", \"6VE\", \"2mk\", \"0XH\", \"0Jd\", \"iV\", \"6Di\", \"45B\", \"5OG\", \"6jE\", \"Gz\", \"0dH\", \"0ix\", \"JJ\", \"4G7\", \"4RV\", \"48r\", \"6IY\", \"df\", \"0GT\", \n\"0HU\", \"kg\", \"523\", \"47s\", \"5Mv\", \"4H6\", \"EK\", \"0fy\", \"0kI\", \"3NK\", \"6eD\", \"4Pg\", \"5nj\", \"6Kh\", \"fW\", \"s6\", \"xU\", \"m4\", \"5ph\", \"6Uj\", \n\"7kg\", \"4Ne\", \"01C\", \"Vy\", \"193\", \"1hZ\", \"4CU\", \"4V4\", \"5h9\", \"4my\", \"0VW\", \"1m\", \"zd\", \"0YV\", \"4bx\", \"5g8\", \"4Y5\", \"4LT\", \"03r\", \"TH\", \n\"Yx\", \"0zJ\", \"4Ad\", \"60O\", \"6Zk\", \"4oH\", \"b5\", \"wT\", \"6fo\", \"4SL\", \"0hb\", \"KP\", \"27e\", \"0FN\", \"49h\", \"6HC\", \"4e1\", \"44X\", \"8Bf\", \"hL\", \n\"0p3\", \"0eR\", \"bdO\", \"aAM\", \"70w\", \"657\", \"0jS\", \"Ia\", \"gM\", \"8Mg\", \"5op\", \"4j0\", \"6GB\", \"46i\", \"0IO\", \"28d\", \"Y\", \"Q0\", \"5Ll\", \"6in\", \n\"4BO\", \"63d\", \"ZS\", \"O2\", \"0WM\", \"0w\", \"7Ia\", \"4lc\", \"4aS\", \"4t2\", \"yO\", \"8Se\", \"00Y\", \"Wc\", \"aPN\", \"b1D\", \"bzM\", \"61U\", \"Xb\", \"1kq\", \n\"216\", \"2F\", \"7KP\", \"4nR\", \"4cb\", \"6VA\", \"2mo\", \"0XL\", \"02h\", \"UR\", \"6xm\", \"4MN\", \"5j7\", \"4ow\", \"0TY\", \"3c\", \"YG\", \"0zu\", \"5Qz\", \"60p\", \n\"6yH\", \"4Lk\", \"03M\", \"Tw\", \"2lJ\", \"0Yi\", \"4bG\", \"6Wd\", \"6Xe\", \"4mF\", \"0Vh\", \"1R\", \"2Mg\", \"0xD\", \"4Cj\", \"62A\", \"7kX\", \"4NZ\", \"0ut\", \"VF\", \n\"xj\", \"1Ky\", \"5pW\", \"5e6\", \"5nU\", \"6KW\", \"fh\", \"0EZ\", \"0kv\", \"HD\", \"4E9\", \"4PX\", \"5MI\", \"6hK\", \"Et\", \"0fF\", \"0Hj\", \"kX\", \"6Fg\", \"47L\", \n\"48M\", \"6If\", \"dY\", \"50\", \"0iG\", \"Ju\", \"6gJ\", \"4Ri\", \"5Ox\", \"4J8\", \"GE\", \"0dw\", \"1Zz\", \"ii\", \"5t5\", \"4qu\", \"02W\", \"Um\", \"5H1\", \"4Mq\", \n\"57t\", \"424\", \"2mP\", \"0Xs\", \"0UC\", \"2y\", \"7Ko\", \"4nm\", \"bzr\", \"61j\", \"2NL\", \"1kN\", \"00f\", \"2AM\", \"6zc\", \"bus\", \"4al\", \"6TO\", \"yp\", \"0ZB\", \n\"0Wr\", \"0H\", \"a7e\", \"58u\", \"4Bp\", \"5G0\", \"Zl\", \"84N\", \"f\", \"13u\", \"5LS\", \"5Y2\", \"amo\", \"46V\", \"0Ip\", \"jB\", \"gr\", \"1Ta\", \"5oO\", \"6JM\", \n\"6da\", \"4QB\", \"0jl\", \"3On\", \"2PN\", \"0em\", \"5Nb\", \"aAr\", \"6EL\", \"44g\", \"0KA\", \"hs\", \"eC\", \"0Fq\", \"49W\", \"abn\", \"5V3\", \"4Ss\", \"8aE\", \"Ko\", \n\"YC\", \"0zq\", \"754\", \"60t\", \"5j3\", \"4os\", \"9Md\", \"3g\", \"2lN\", \"0Ym\", \"4bC\", \"7GA\", \"6yL\", \"4Lo\", \"03I\", \"Ts\", \"2Mc\", \"1ha\", \"4Cn\", \"62E\", \n\"6Xa\", \"4mB\", \"0Vl\", \"1V\", \"xn\", \"8RD\", \"5pS\", \"5e2\", \"aQo\", \"b0e\", \"01x\", \"VB\", \"0kr\", \"1n2\", \"71V\", \"bjo\", \"5nQ\", \"6KS\", \"fl\", \"315\", \n\"0Hn\", \"29E\", \"6Fc\", \"47H\", \"5MM\", \"6hO\", \"Ep\", \"0fB\", \"0iC\", \"Jq\", \"6gN\", \"4Rm\", \"48I\", \"6Ib\", \"26D\", \"54\", \"8CG\", \"im\", \"509\", \"45y\", \n\"ben\", \"hYU\", \"GA\", \"0ds\", \"4cY\", \"420\", \"2mT\", \"0Xw\", \"02S\", \"Ui\", \"5H5\", \"4Mu\", \"5Pd\", \"61n\", \"XY\", \"M8\", \"0UG\", \"vu\", \"7Kk\", \"4ni\", \n\"4ah\", \"6TK\", \"yt\", \"0ZF\", \"B9\", \"WX\", \"6zg\", \"4OD\", \"4Bt\", \"5G4\", \"Zh\", \"0yZ\", \"0Wv\", \"0L\", \"4y9\", \"4lX\", \"6Gy\", \"46R\", \"0It\", \"jF\", \n\"b\", \"0gX\", \"5LW\", \"5Y6\", \"6de\", \"4QF\", \"0jh\", \"IZ\", \"gv\", \"0DD\", \"5oK\", \"6JI\", \"6EH\", \"44c\", \"0KE\", \"hw\", \"2PJ\", \"0ei\", \"5Nf\", \"6kd\", \n\"5V7\", \"4Sw\", \"0hY\", \"Kk\", \"eG\", \"0Fu\", \"49S\", \"6Hx\", \"7ia\", \"4Lc\", \"03E\", \"2Bn\", \"zS\", \"o2\", \"4bO\", \"6Wl\", \"a4F\", \"bUL\", \"0TQ\", \"3k\", \n\"YO\", \"87m\", \"4AS\", \"4T2\", \"7kP\", \"4NR\", \"01t\", \"VN\", \"xb\", \"1Kq\", \"54W\", \"hfv\", \"6Xm\", \"4mN\", \"1FA\", \"1Z\", \"2Mo\", \"0xL\", \"4Cb\", \"62I\", \n\"5MA\", \"6hC\", \"2Sm\", \"0fN\", \"0Hb\", \"kP\", \"6Fo\", \"47D\", \"bDO\", \"aaM\", \"0P3\", \"0ER\", \"8bf\", \"HL\", \"4E1\", \"4PP\", \"5Op\", \"4J0\", \"GM\", \"10V\", \n\"0JS\", \"ia\", \"505\", \"45u\", \"48E\", \"6In\", \"dQ\", \"58\", \"0iO\", \"3LM\", \"6gB\", \"4Ra\", \"0UK\", \"2q\", \"7Kg\", \"4ne\", \"5Ph\", \"61b\", \"XU\", \"M4\", \n\"0vW\", \"Ue\", \"5H9\", \"4My\", \"4cU\", \"4v4\", \"2mX\", \"1HZ\", \"0Wz\", \"tH\", \"4y5\", \"4lT\", \"4Bx\", \"5G8\", \"Zd\", \"0yV\", \"B5\", \"WT\", \"6zk\", \"4OH\", \n\"4ad\", \"6TG\", \"yx\", \"0ZJ\", \"gz\", \"0DH\", \"5oG\", \"6JE\", \"6di\", \"4QJ\", \"0jd\", \"IV\", \"n\", \"0gT\", \"680\", \"6iY\", \"4g7\", \"4rV\", \"0Ix\", \"jJ\", \n\"eK\", \"0Fy\", \"5mv\", \"4h6\", \"6fX\", \"5CZ\", \"0hU\", \"Kg\", \"FW\", \"S6\", \"5Nj\", \"6kh\", \"6ED\", \"44o\", \"0KI\", \"3nK\", \"zW\", \"o6\", \"4bK\", \"6Wh\", \n\"6yD\", \"4Lg\", \"03A\", \"2Bj\", \"YK\", \"0zy\", \"4AW\", \"4T6\", \"6ZX\", \"6O9\", \"0TU\", \"3o\", \"xf\", \"1Ku\", \"54S\", \"6UY\", \"7kT\", \"4NV\", \"01p\", \"VJ\", \n\"2Mk\", \"0xH\", \"4Cf\", \"62M\", \"6Xi\", \"4mJ\", \"0Vd\", \"uV\", \"0Hf\", \"kT\", \"6Fk\", \"4sH\", \"5ME\", \"6hG\", \"Ex\", \"0fJ\", \"0kz\", \"HH\", \"4E5\", \"4PT\", \n\"5nY\", \"aaI\", \"fd\", \"0EV\", \"0JW\", \"ie\", \"501\", \"45q\", \"5Ot\", \"4J4\", \"GI\", \"10R\", \"0iK\", \"Jy\", \"6gF\", \"4Re\", \"48A\", \"6Ij\", \"dU\", \"q4\", \n\"5Pl\", \"61f\", \"XQ\", \"M0\", \"0UO\", \"2u\", \"7Kc\", \"4na\", \"4cQ\", \"428\", \"39u\", \"8Qg\", \"0vS\", \"Ua\", \"aRL\", \"b3F\", \"bxO\", \"63W\", \"0l3\", \"0yR\", \n\"234\", \"0D\", \"4y1\", \"4lP\", \"55I\", \"6TC\", \"2om\", \"0ZN\", \"B1\", \"WP\", \"6zo\", \"4OL\", \"6dm\", \"4QN\", \"1zA\", \"IR\", \"25g\", \"0DL\", \"5oC\", \"6JA\", \n\"4g3\", \"46Z\", \"9PE\", \"jN\", \"j\", \"0gP\", \"684\", \"aCO\", \"72u\", \"675\", \"0hQ\", \"Kc\", \"eO\", \"8Oe\", \"5mr\", \"4h2\", \"7Ua\", \"44k\", \"0KM\", \"3nO\", \n\"FS\", \"S2\", \"5Nn\", \"6kl\", \"4x6\", \"4mW\", \"0Vy\", \"1C\", \"0m4\", \"0xU\", \"5SZ\", \"62P\", \"7kI\", \"4NK\", \"C6\", \"VW\", \"2nj\", \"1Kh\", \"54N\", \"6UD\", \n\"6ZE\", \"4of\", \"0TH\", \"3r\", \"YV\", \"L7\", \"4AJ\", \"60a\", \"6yY\", \"4Lz\", \"0wT\", \"Tf\", \"zJ\", \"0Yx\", \"4bV\", \"4w7\", \"5lu\", \"4i5\", \"dH\", \"0Gz\", \n\"0iV\", \"Jd\", \"5W8\", \"4Rx\", \"5Oi\", \"6jk\", \"GT\", \"R5\", \"0JJ\", \"ix\", \"6DG\", \"45l\", \"5nD\", \"6KF\", \"fy\", \"0EK\", \"0kg\", \"HU\", \"6ej\", \"4PI\", \n\"5MX\", \"5X9\", \"Ee\", \"0fW\", \"1XZ\", \"kI\", \"4f4\", \"4sU\", \"00w\", \"WM\", \"4Z0\", \"4OQ\", \"55T\", \"hgu\", \"ya\", \"0ZS\", \"a0\", \"0Y\", \"6Yn\", \"4lM\", \n\"4Ba\", \"63J\", \"2Ll\", \"0yO\", \"02F\", \"2Cm\", \"6xC\", \"aG0\", \"4cL\", \"6Vo\", \"2mA\", \"n1\", \"0UR\", \"2h\", \"a5E\", \"bTO\", \"5Pq\", \"4U1\", \"XL\", \"86n\", \n\"FN\", \"11U\", \"5Ns\", \"4K3\", \"516\", \"44v\", \"0KP\", \"hb\", \"eR\", \"p3\", \"49F\", \"6Hm\", \"6fA\", \"4Sb\", \"0hL\", \"3MN\", \"w\", \"0gM\", \"5LB\", \"7ya\", \n\"6Gl\", \"46G\", \"0Ia\", \"jS\", \"gc\", \"0DQ\", \"bEL\", \"hyw\", \"4D2\", \"4QS\", \"8ce\", \"IO\", \"0m0\", \"0xQ\", \"byL\", \"62T\", \"4x2\", \"4mS\", \"227\", \"1G\", \n\"2nn\", \"1Kl\", \"54J\", \"7Ea\", \"7kM\", \"4NO\", \"C2\", \"VS\", \"YR\", \"L3\", \"4AN\", \"60e\", \"6ZA\", \"4ob\", \"0TL\", \"3v\", \"zN\", \"8Pd\", \"4bR\", \"4w3\", \n\"aSO\", \"b2E\", \"03X\", \"Tb\", \"0iR\", \"3LP\", \"73v\", \"666\", \"48X\", \"4i1\", \"dL\", \"8Nf\", \"0JN\", \"3oL\", \"6DC\", \"45h\", \"5Om\", \"6jo\", \"GP\", \"R1\", \n\"0kc\", \"HQ\", \"6en\", \"4PM\", \"a09\", \"6KB\", \"24d\", \"0EO\", \"8Ag\", \"kM\", \"4f0\", \"47Y\", \"697\", \"aBL\", \"Ea\", \"0fS\", \"4ay\", \"5d9\", \"ye\", \"0ZW\", \n\"00s\", \"WI\", \"4Z4\", \"4OU\", \"4Be\", \"63N\", \"Zy\", \"0yK\", \"a4\", \"tU\", \"6Yj\", \"4lI\", \"4cH\", \"6Vk\", \"2mE\", \"n5\", \"02B\", \"Ux\", \"6xG\", \"4Md\", \n\"5Pu\", \"4U5\", \"XH\", \"86j\", \"0UV\", \"2l\", \"5k8\", \"4nx\", \"512\", \"44r\", \"0KT\", \"hf\", \"FJ\", \"0ex\", \"5Nw\", \"4K7\", \"6fE\", \"4Sf\", \"0hH\", \"Kz\", \n\"eV\", \"p7\", \"49B\", \"6Hi\", \"6Gh\", \"46C\", \"0Ie\", \"jW\", \"s\", \"0gI\", \"5LF\", \"6iD\", \"4D6\", \"4QW\", \"0jy\", \"IK\", \"gg\", \"0DU\", \"5oZ\", \"6JX\", \n\"7kA\", \"4NC\", \"01e\", \"3Po\", \"xs\", \"8RY\", \"54F\", \"6UL\", \"a6f\", \"59v\", \"0Vq\", \"1K\", \"d3E\", \"85M\", \"4Cs\", \"5F3\", \"5I2\", \"4Lr\", \"03T\", \"Tn\", \n\"zB\", \"0Yp\", \"56w\", \"437\", \"6ZM\", \"4on\", \"1Da\", \"3z\", \"2OO\", \"0zl\", \"4AB\", \"60i\", \"5Oa\", \"6jc\", \"2QM\", \"0dn\", \"0JB\", \"ip\", \"6DO\", \"45d\", \n\"48T\", \"acm\", \"1B2\", \"0Gr\", \"94o\", \"Jl\", \"5W0\", \"4Rp\", \"5MP\", \"5X1\", \"Em\", \"12v\", \"0Hs\", \"kA\", \"all\", \"47U\", \"5nL\", \"6KN\", \"fq\", \"0EC\", \n\"0ko\", \"3Nm\", \"6eb\", \"4PA\", \"a8\", \"0Q\", \"6Yf\", \"4lE\", \"4Bi\", \"63B\", \"Zu\", \"0yG\", \"0tw\", \"WE\", \"4Z8\", \"4OY\", \"4au\", \"5d5\", \"yi\", \"1Jz\", \n\"0UZ\", \"vh\", \"5k4\", \"4nt\", \"5Py\", \"4U9\", \"XD\", \"1kW\", \"02N\", \"Ut\", \"6xK\", \"4Mh\", \"4cD\", \"6Vg\", \"2mI\", \"n9\", \"eZ\", \"43\", \"49N\", \"6He\", \n\"6fI\", \"4Sj\", \"0hD\", \"Kv\", \"FF\", \"0et\", \"7n9\", \"6ky\", \"5u6\", \"4pv\", \"0KX\", \"hj\", \"gk\", \"0DY\", \"5oV\", \"5z7\", \"6dx\", \"5Az\", \"0ju\", \"IG\", \n\"Dw\", \"0gE\", \"5LJ\", \"6iH\", \"6Gd\", \"46O\", \"0Ii\", \"28B\", \"xw\", \"1Kd\", \"54B\", \"6UH\", \"7kE\", \"4NG\", \"01a\", \"3Pk\", \"0m8\", \"0xY\", \"4Cw\", \"5F7\", \n\"6Xx\", \"59r\", \"0Vu\", \"1O\", \"zF\", \"0Yt\", \"4bZ\", \"433\", \"5I6\", \"4Lv\", \"03P\", \"Tj\", \"YZ\", \"0zh\", \"4AF\", \"60m\", \"6ZI\", \"4oj\", \"0TD\", \"wv\", \n\"0JF\", \"it\", \"6DK\", \"4qh\", \"5Oe\", \"6jg\", \"GX\", \"R9\", \"0iZ\", \"Jh\", \"5W4\", \"4Rt\", \"48P\", \"4i9\", \"dD\", \"0Gv\", \"0Hw\", \"kE\", \"4f8\", \"47Q\", \n\"5MT\", \"5X5\", \"Ei\", \"12r\", \"0kk\", \"HY\", \"6ef\", \"4PE\", \"5nH\", \"6KJ\", \"fu\", \"0EG\", \"4Bm\", \"63F\", \"Zq\", \"0yC\", \"0Wo\", \"0U\", \"6Yb\", \"4lA\", \n\"4aq\", \"5d1\", \"ym\", \"8SG\", \"0ts\", \"WA\", \"aPl\", \"b1f\", \"747\", \"61w\", \"2NQ\", \"1kS\", \"9Lg\", \"2d\", \"5k0\", \"4np\", \"57i\", \"6Vc\", \"2mM\", \"0Xn\", \n\"02J\", \"Up\", \"6xO\", \"4Ml\", \"6fM\", \"4Sn\", \"1xa\", \"Kr\", \"27G\", \"47\", \"49J\", \"6Ha\", \"5u2\", \"44z\", \"8BD\", \"hn\", \"FB\", \"0ep\", \"bdm\", \"aAo\", \n\"70U\", \"bkl\", \"0jq\", \"IC\", \"go\", \"306\", \"5oR\", \"5z3\", \"7WA\", \"46K\", \"0Im\", \"28F\", \"Ds\", \"0gA\", \"5LN\", \"6iL\", \"0cY\", \"020\", \"6mT\", \"4Xw\", \n\"42S\", \"6Cx\", \"nG\", \"0Mu\", \"1Pd\", \"cw\", \"6NH\", \"5kJ\", \"4UG\", \"74M\", \"3Kk\", \"0ni\", \"0ah\", \"BZ\", \"6oe\", \"4ZF\", \"40b\", \"6AI\", \"lv\", \"0OD\", \n\"0Bt\", \"aF\", \"6Ly\", \"4yZ\", \"4Wv\", \"5R6\", \"Oj\", \"0lX\", \"Qh\", \"06R\", \"4It\", \"5L4\", \"461\", \"4gX\", \"1LW\", \"1Y6\", \"rt\", \"0QF\", \"4jh\", \"7Oj\", \n\"65o\", \"4DD\", \"I9\", \"2JI\", \"SY\", \"F8\", \"4KE\", \"7nG\", \"6PJ\", \"4ei\", \"1Nf\", \"2kd\", \"4M\", \"0Sw\", \"4hY\", \"490\", \"5C5\", \"4Fu\", \"09S\", \"2Hx\", \n\"6OR\", \"4zq\", \"354\", \"bm\", \"LA\", \"0os\", \"bnn\", \"75W\", \"6lN\", \"4Ym\", \"0bC\", \"Aq\", \"2yL\", \"0Lo\", \"43I\", \"6Bb\", \"6Mc\", \"5ha\", \"15\", \"22E\", \n\"Np\", \"0mB\", \"4Vl\", \"6cO\", \"aDm\", \"bao\", \"1pS\", \"1e2\", \"ml\", \"8GF\", \"41x\", \"548\", \"4kr\", \"5n2\", \"7f\", \"8YD\", \"1nQ\", \"2KS\", \"64u\", \"715\", \n\"4Hn\", \"69E\", \"Pr\", \"07H\", \"1MM\", \"2hO\", \"6Sa\", \"4fB\", \"4iC\", \"7LA\", \"5W\", \"0Rm\", \"08I\", \"2Ib\", \"66D\", \"4Go\", \"b4d\", \"aUn\", \"RC\", \"05y\", \n\"8VE\", \"8g\", \"5a3\", \"4ds\", \"42W\", \"ain\", \"nC\", \"0Mq\", \"17t\", \"024\", \"6mP\", \"4Xs\", \"4UC\", \"74I\", \"3Ko\", \"0nm\", \"8IY\", \"cs\", \"6NL\", \"5kN\", \n\"40f\", \"6AM\", \"lr\", \"8FX\", \"0al\", \"2TO\", \"6oa\", \"4ZB\", \"4Wr\", \"5R2\", \"On\", \"18u\", \"0Bp\", \"aB\", \"afo\", \"bCm\", \"465\", \"53u\", \"1LS\", \"1Y2\", \n\"Ql\", \"06V\", \"4Ip\", \"5L0\", \"65k\", \"5Ta\", \"1oO\", \"2JM\", \"6x\", \"0QB\", \"4jl\", \"7On\", \"6PN\", \"4em\", \"1Nb\", \"9y\", \"2EL\", \"04g\", \"4KA\", \"7nC\", \n\"5C1\", \"4Fq\", \"09W\", \"d6G\", \"4I\", \"0Ss\", \"bRn\", \"494\", \"LE\", \"0ow\", \"4TY\", \"4A8\", \"6OV\", \"4zu\", \"1Qz\", \"bi\", \"oY\", \"z8\", \"43M\", \"6Bf\", \n\"6lJ\", \"4Yi\", \"0bG\", \"Au\", \"Nt\", \"0mF\", \"4Vh\", \"6cK\", \"6Mg\", \"4xD\", \"11\", \"22A\", \"mh\", \"0NZ\", \"4ut\", \"5p4\", \"4N9\", \"5Ky\", \"1pW\", \"CD\", \n\"1nU\", \"2KW\", \"64q\", \"4EZ\", \"4kv\", \"5n6\", \"7b\", \"0PX\", \"1MI\", \"2hK\", \"6Se\", \"4fF\", \"4Hj\", \"69A\", \"Pv\", \"07L\", \"08M\", \"2If\", \"6rH\", \"4Gk\", \n\"4iG\", \"7LE\", \"5S\", \"0Ri\", \"1Ox\", \"8c\", \"5a7\", \"4dw\", \"5Zz\", \"7oY\", \"RG\", \"0qu\", \"1Pl\", \"21f\", \"adR\", \"5kB\", \"4UO\", \"74E\", \"MS\", \"X2\", \n\"0cQ\", \"028\", \"79u\", \"bbL\", \"4vS\", \"4c2\", \"nO\", \"8De\", \"8Kd\", \"aN\", \"4l3\", \"4yR\", \"634\", \"76t\", \"Ob\", \"0lP\", \"W3\", \"BR\", \"6om\", \"4ZN\", \n\"40j\", \"6AA\", \"2zo\", \"0OL\", \"6t\", \"0QN\", \"5zA\", \"7Ob\", \"65g\", \"4DL\", \"I1\", \"2JA\", \"0g3\", \"06Z\", \"b7G\", \"68W\", \"469\", \"4gP\", \"284\", \"dSn\", \n\"4E\", \"275\", \"4hQ\", \"498\", \"67V\", \"b8F\", \"1mr\", \"0h2\", \"SQ\", \"F0\", \"4KM\", \"7nO\", \"6PB\", \"4ea\", \"1Nn\", \"9u\", \"6lF\", \"4Ye\", \"0bK\", \"Ay\", \n\"oU\", \"z4\", \"43A\", \"6Bj\", \"6OZ\", \"4zy\", \"0AW\", \"be\", \"LI\", \"2O9\", \"4TU\", \"4A4\", \"4N5\", \"5Ku\", \"14S\", \"CH\", \"md\", \"0NV\", \"41p\", \"540\", \n\"6Mk\", \"4xH\", \"u5\", \"22M\", \"Nx\", \"0mJ\", \"4Vd\", \"6cG\", \"4Hf\", \"69M\", \"Pz\", \"0sH\", \"k7\", \"2hG\", \"6Si\", \"4fJ\", \"4kz\", \"7Nx\", \"7n\", \"0PT\", \n\"1nY\", \"dqh\", \"4P7\", \"4EV\", \"4JW\", \"7oU\", \"RK\", \"05q\", \"1Ot\", \"8o\", \"6QX\", \"50R\", \"4iK\", \"7LI\", \"qW\", \"d6\", \"08A\", \"2Ij\", \"66L\", \"4Gg\", \n\"4UK\", \"74A\", \"MW\", \"X6\", \"1Ph\", \"21b\", \"6ND\", \"5kF\", \"4vW\", \"4c6\", \"nK\", \"0My\", \"0cU\", \"0v4\", \"6mX\", \"5HZ\", \"4Wz\", \"6bY\", \"Of\", \"0lT\", \n\"0Bx\", \"aJ\", \"4l7\", \"4yV\", \"40n\", \"6AE\", \"lz\", \"0OH\", \"W7\", \"BV\", \"6oi\", \"4ZJ\", \"65c\", \"4DH\", \"I5\", \"2JE\", \"6p\", \"0QJ\", \"4jd\", \"7Of\", \n\"4r5\", \"4gT\", \"280\", \"2iY\", \"Qd\", \"0rV\", \"4Ix\", \"5L8\", \"5C9\", \"4Fy\", \"1mv\", \"0h6\", \"4A\", \"1CZ\", \"4hU\", \"7MW\", \"6PF\", \"4ee\", \"1Nj\", \"9q\", \n\"SU\", \"F4\", \"4KI\", \"7nK\", \"oQ\", \"z0\", \"43E\", \"6Bn\", \"6lB\", \"4Ya\", \"0bO\", \"2Wl\", \"LM\", \"8fg\", \"4TQ\", \"4A0\", \"aeL\", \"cPo\", \"0AS\", \"ba\", \n\"3kP\", \"0NR\", \"41t\", \"544\", \"4N1\", \"5Kq\", \"14W\", \"CL\", \"2Xm\", \"0mN\", \"5FA\", \"6cC\", \"6Mo\", \"4xL\", \"19\", \"22I\", \"k3\", \"2hC\", \"6Sm\", \"4fN\", \n\"4Hb\", \"69I\", \"2Fo\", \"07D\", \"83l\", \"d5d\", \"4P3\", \"4ER\", \"bQM\", \"a0G\", \"7j\", \"0PP\", \"1Op\", \"8k\", \"hbw\", \"50V\", \"4JS\", \"7oQ\", \"RO\", \"05u\", \n\"08E\", \"2In\", \"66H\", \"4Gc\", \"4iO\", \"7LM\", \"qS\", \"d2\", \"0ay\", \"BK\", \"4O6\", \"4ZW\", \"40s\", \"553\", \"lg\", \"0OU\", \"t6\", \"aW\", \"6Lh\", \"4yK\", \n\"4Wg\", \"6bD\", \"2Yj\", \"0lI\", \"0cH\", \"2Vk\", \"6mE\", \"4Xf\", \"42B\", \"6Ci\", \"nV\", \"0Md\", \"1Pu\", \"cf\", \"6NY\", \"bAI\", \"4UV\", \"7pT\", \"MJ\", \"0nx\", \n\"SH\", \"04r\", \"4KT\", \"7nV\", \"azI\", \"4ex\", \"1Nw\", \"9l\", \"pT\", \"e5\", \"4hH\", \"7MJ\", \"67O\", \"4Fd\", \"09B\", \"2Hi\", \"Qy\", \"06C\", \"4Ie\", \"68N\", \n\"6Rj\", \"4gI\", \"j4\", \"2iD\", \"6m\", \"0QW\", \"4jy\", \"5o9\", \"4Q4\", \"4DU\", \"1oZ\", \"2JX\", \"4m0\", \"4xQ\", \"8Jg\", \"22T\", \"Na\", \"0mS\", \"627\", \"77w\", \n\"6nn\", \"5Kl\", \"V0\", \"CQ\", \"3kM\", \"0NO\", \"41i\", \"7Pc\", \"6OC\", \"5jA\", \"0AN\", \"20e\", \"LP\", \"Y1\", \"4TL\", \"6ao\", \"78v\", \"bcO\", \"0bR\", \"0w3\", \n\"oL\", \"8Ef\", \"43X\", \"4b1\", \"4iR\", \"7LP\", \"5F\", \"266\", \"08X\", \"0i1\", \"66U\", \"b9E\", \"4JN\", \"7oL\", \"RR\", \"G3\", \"1Om\", \"8v\", \"6QA\", \"4db\", \n\"4kc\", \"7Na\", \"7w\", \"0PM\", \"H2\", \"2KB\", \"64d\", \"4EO\", \"b6D\", \"69T\", \"Pc\", \"07Y\", \"297\", \"dRm\", \"4s2\", \"4fS\", \"40w\", \"557\", \"lc\", \"0OQ\", \n\"15T\", \"BO\", \"4O2\", \"4ZS\", \"4Wc\", \"76i\", \"2Yn\", \"0lM\", \"t2\", \"aS\", \"6Ll\", \"4yO\", \"42F\", \"6Cm\", \"nR\", \"8Dx\", \"0cL\", \"2Vo\", \"6mA\", \"4Xb\", \n\"4UR\", \"74X\", \"MN\", \"8gd\", \"1Pq\", \"cb\", \"adO\", \"bAM\", \"azM\", \"51U\", \"1Ns\", \"9h\", \"SL\", \"04v\", \"4KP\", \"7nR\", \"67K\", \"5VA\", \"09F\", \"2Hm\", \n\"4X\", \"e1\", \"4hL\", \"7MN\", \"6Rn\", \"4gM\", \"j0\", \"3ya\", \"2Gl\", \"06G\", \"4Ia\", \"68J\", \"4Q0\", \"4DQ\", \"82o\", \"d4g\", \"6i\", \"0QS\", \"bPN\", \"a1D\", \n\"Ne\", \"0mW\", \"4Vy\", \"5S9\", \"4m4\", \"4xU\", \"1SZ\", \"22P\", \"my\", \"0NK\", \"41m\", \"7Pg\", \"6nj\", \"5Kh\", \"V4\", \"CU\", \"LT\", \"Y5\", \"4TH\", \"6ak\", \n\"6OG\", \"4zd\", \"0AJ\", \"bx\", \"oH\", \"0Lz\", \"4wT\", \"4b5\", \"78r\", \"4Yx\", \"0bV\", \"Ad\", \"1lu\", \"0i5\", \"66Q\", \"4Gz\", \"4iV\", \"7LT\", \"5B\", \"0Rx\", \n\"1Oi\", \"8r\", \"6QE\", \"4df\", \"4JJ\", \"7oH\", \"RV\", \"G7\", \"H6\", \"2KF\", \"6ph\", \"4EK\", \"4kg\", \"7Ne\", \"7s\", \"0PI\", \"1MX\", \"1X9\", \"4s6\", \"4fW\", \n\"5XZ\", \"69P\", \"Pg\", \"0sU\", \"06\", \"23F\", \"afr\", \"4yC\", \"4Wo\", \"6bL\", \"Os\", \"0lA\", \"0aq\", \"BC\", \"aEn\", \"c4E\", \"4ts\", \"5q3\", \"lo\", \"8FE\", \n\"347\", \"cn\", \"6NQ\", \"5kS\", \"bom\", \"74T\", \"MB\", \"0np\", \"17i\", \"2Vc\", \"6mM\", \"4Xn\", \"42J\", \"6Ca\", \"2xO\", \"0Ml\", \"4T\", \"0Sn\", \"5xa\", \"7MB\", \n\"67G\", \"4Fl\", \"09J\", \"2Ha\", \"1u2\", \"04z\", \"b5g\", \"aTm\", \"6PS\", \"4ep\", \"8WF\", \"9d\", \"6e\", \"8XG\", \"4jq\", \"5o1\", \"65v\", \"706\", \"1oR\", \"1z3\", \n\"Qq\", \"06K\", \"4Im\", \"68F\", \"6Rb\", \"4gA\", \"1LN\", \"2iL\", \"6nf\", \"5Kd\", \"V8\", \"CY\", \"mu\", \"0NG\", \"41a\", \"7Pk\", \"4m8\", \"4xY\", \"0Cw\", \"1F7\", \n\"Ni\", \"19r\", \"4Vu\", \"5S5\", \"6lW\", \"4Yt\", \"0bZ\", \"Ah\", \"oD\", \"0Lv\", \"43P\", \"4b9\", \"6OK\", \"4zh\", \"0AF\", \"bt\", \"LX\", \"Y9\", \"4TD\", \"6ag\", \n\"4JF\", \"7oD\", \"RZ\", \"0qh\", \"1Oe\", \"2jg\", \"6QI\", \"4dj\", \"4iZ\", \"483\", \"5N\", \"0Rt\", \"08P\", \"0i9\", \"5B6\", \"4Gv\", \"4Hw\", \"5M7\", \"Pk\", \"07Q\", \n\"1MT\", \"1X5\", \"472\", \"52r\", \"4kk\", \"7Ni\", \"sw\", \"0PE\", \"1nH\", \"2KJ\", \"64l\", \"4EG\", \"4Wk\", \"6bH\", \"Ow\", \"0lE\", \"02\", \"23B\", \"6Ld\", \"4yG\", \n\"4tw\", \"5q7\", \"lk\", \"0OY\", \"0au\", \"BG\", \"6ox\", \"5Jz\", \"4UZ\", \"74P\", \"MF\", \"0nt\", \"1Py\", \"cj\", \"6NU\", \"5kW\", \"42N\", \"6Ce\", \"nZ\", \"0Mh\", \n\"0cD\", \"2Vg\", \"6mI\", \"4Xj\", \"67C\", \"4Fh\", \"09N\", \"2He\", \"4P\", \"e9\", \"4hD\", \"7MF\", \"6PW\", \"4et\", \"3n9\", \"2ky\", \"SD\", \"0pv\", \"4KX\", \"7nZ\", \n\"4Q8\", \"4DY\", \"1oV\", \"1z7\", \"6a\", \"1Az\", \"4ju\", \"5o5\", \"6Rf\", \"4gE\", \"j8\", \"2iH\", \"Qu\", \"06O\", \"4Ii\", \"68B\", \"mq\", \"0NC\", \"41e\", \"7Po\", \n\"6nb\", \"bar\", \"14F\", \"2UL\", \"Nm\", \"19v\", \"4Vq\", \"5S1\", \"agl\", \"bBn\", \"0Cs\", \"1F3\", \"1I2\", \"0Lr\", \"43T\", \"ahm\", \"6lS\", \"4Yp\", \"16w\", \"Al\", \n\"2ZM\", \"0on\", \"5Da\", \"6ac\", \"6OO\", \"4zl\", \"0AB\", \"bp\", \"1Oa\", \"8z\", \"6QM\", \"4dn\", \"4JB\", \"aUs\", \"2DO\", \"05d\", \"08T\", \"d7D\", \"5B2\", \"4Gr\", \n\"bSm\", \"487\", \"5J\", \"0Rp\", \"1MP\", \"1X1\", \"476\", \"52v\", \"4Hs\", \"5M3\", \"Po\", \"07U\", \"1nL\", \"2KN\", \"64h\", \"4EC\", \"4ko\", \"7Nm\", \"ss\", \"0PA\", \n\"QJ\", \"06p\", \"4IV\", \"7lT\", \"6RY\", \"4gz\", \"1Lu\", \"0I5\", \"rV\", \"g7\", \"4jJ\", \"7OH\", \"65M\", \"4Df\", \"1oi\", \"2Jk\", \"2Ej\", \"04A\", \"4Kg\", \"7ne\", \n\"6Ph\", \"4eK\", \"h6\", \"2kF\", \"4o\", \"0SU\", \"5xZ\", \"7My\", \"4S6\", \"4FW\", \"09q\", \"1x9\", \"17R\", \"2VX\", \"4M4\", \"4XU\", \"42q\", \"571\", \"ne\", \"0MW\", \n\"v4\", \"cU\", \"6Nj\", \"5kh\", \"4Ue\", \"74o\", \"My\", \"0nK\", \"0aJ\", \"Bx\", \"6oG\", \"4Zd\", \"4tH\", \"6Ak\", \"lT\", \"y5\", \"0BV\", \"ad\", \"580\", \"4yx\", \n\"4WT\", \"4B5\", \"OH\", \"0lz\", \"4kP\", \"7NR\", \"7D\", \"244\", \"1ns\", \"0k3\", \"64W\", \"con\", \"4HL\", \"69g\", \"PP\", \"E1\", \"1Mo\", \"2hm\", \"6SC\", \"52I\", \n\"4ia\", \"7Lc\", \"5u\", \"0RO\", \"J0\", \"3Ya\", \"66f\", \"4GM\", \"b4F\", \"aUL\", \"Ra\", \"0qS\", \"8Vg\", \"8E\", \"458\", \"4dQ\", \"4o2\", \"4zS\", \"8He\", \"bO\", \n\"Lc\", \"0oQ\", \"605\", \"75u\", \"6ll\", \"4YO\", \"T2\", \"AS\", \"2yn\", \"0LM\", \"43k\", \"7Ra\", \"6MA\", \"4xb\", \"0CL\", \"22g\", \"NR\", \"19I\", \"4VN\", \"6cm\", \n\"aDO\", \"baM\", \"14y\", \"Cb\", \"mN\", \"8Gd\", \"41Z\", \"7PP\", \"axO\", \"53W\", \"1Lq\", \"0I1\", \"QN\", \"06t\", \"4IR\", \"68y\", \"65I\", \"4Db\", \"1om\", \"2Jo\", \n\"6Z\", \"g3\", \"4jN\", \"7OL\", \"6Pl\", \"4eO\", \"h2\", \"2kB\", \"2En\", \"04E\", \"4Kc\", \"7na\", \"4S2\", \"4FS\", \"09u\", \"d6e\", \"4k\", \"0SQ\", \"bRL\", \"a3F\", \n\"42u\", \"575\", \"na\", \"0MS\", \"17V\", \"dlo\", \"4M0\", \"4XQ\", \"4Ua\", \"74k\", \"3KM\", \"0nO\", \"28\", \"cQ\", \"6Nn\", \"5kl\", \"40D\", \"6Ao\", \"lP\", \"y1\", \n\"0aN\", \"2Tm\", \"6oC\", \"5JA\", \"4WP\", \"4B1\", \"OL\", \"18W\", \"0BR\", \"0W3\", \"584\", \"bCO\", \"1nw\", \"0k7\", \"64S\", \"4Ex\", \"4kT\", \"7NV\", \"sH\", \"0Pz\", \n\"1Mk\", \"2hi\", \"6SG\", \"4fd\", \"4HH\", \"69c\", \"PT\", \"E5\", \"J4\", \"2ID\", \"66b\", \"4GI\", \"4ie\", \"7Lg\", \"5q\", \"0RK\", \"1OZ\", \"8A\", \"4q4\", \"4dU\", \n\"4Jy\", \"5O9\", \"Re\", \"0qW\", \"Lg\", \"0oU\", \"5DZ\", \"6aX\", \"4o6\", \"4zW\", \"0Ay\", \"bK\", \"2yj\", \"0LI\", \"43o\", \"6BD\", \"6lh\", \"4YK\", \"T6\", \"AW\", \n\"NV\", \"0md\", \"4VJ\", \"6ci\", \"6ME\", \"4xf\", \"0CH\", \"22c\", \"mJ\", \"0Nx\", \"4uV\", \"7PT\", \"6nY\", \"baI\", \"1pu\", \"Cf\", \"6V\", \"0Ql\", \"4jB\", \"aus\", \n\"65E\", \"4Dn\", \"1oa\", \"2Jc\", \"QB\", \"06x\", \"b7e\", \"68u\", \"5b2\", \"4gr\", \"8UD\", \"dSL\", \"4g\", \"8ZE\", \"4hs\", \"5m3\", \"67t\", \"724\", \"09y\", \"1x1\", \n\"Ss\", \"04I\", \"4Ko\", \"7nm\", \"azr\", \"4eC\", \"1NL\", \"9W\", \"24\", \"21D\", \"6Nb\", \"bAr\", \"4Um\", \"74g\", \"Mq\", \"0nC\", \"0cs\", \"1f3\", \"79W\", \"bbn\", \n\"42y\", \"579\", \"nm\", \"394\", \"365\", \"al\", \"588\", \"4yp\", \"bmo\", \"76V\", \"1i2\", \"0lr\", \"0aB\", \"Bp\", \"6oO\", \"4Zl\", \"40H\", \"6Ac\", \"2zM\", \"0On\", \n\"4HD\", \"69o\", \"PX\", \"E9\", \"1Mg\", \"2he\", \"6SK\", \"4fh\", \"4kX\", \"7NZ\", \"7L\", \"0Pv\", \"3N9\", \"2Ky\", \"6pW\", \"4Et\", \"4Ju\", \"5O5\", \"Ri\", \"05S\", \n\"1OV\", \"8M\", \"450\", \"4dY\", \"4ii\", \"7Lk\", \"qu\", \"0RG\", \"J8\", \"2IH\", \"66n\", \"4GE\", \"6ld\", \"4YG\", \"0bi\", \"2WJ\", \"ow\", \"0LE\", \"43c\", \"6BH\", \n\"6Ox\", \"5jz\", \"0Au\", \"bG\", \"Lk\", \"0oY\", \"4Tw\", \"5Q7\", \"6nU\", \"5KW\", \"14q\", \"Cj\", \"mF\", \"0Nt\", \"41R\", \"7PX\", \"6MI\", \"4xj\", \"0CD\", \"22o\", \n\"NZ\", \"0mh\", \"4VF\", \"6ce\", \"65A\", \"4Dj\", \"1oe\", \"2Jg\", \"6R\", \"0Qh\", \"4jF\", \"7OD\", \"5b6\", \"4gv\", \"1Ly\", \"0I9\", \"QF\", \"0rt\", \"4IZ\", \"68q\", \n\"67p\", \"5Vz\", \"1mT\", \"1x5\", \"4c\", \"0SY\", \"4hw\", \"5m7\", \"6Pd\", \"4eG\", \"1NH\", \"9S\", \"Sw\", \"04M\", \"4Kk\", \"7ni\", \"4Ui\", \"74c\", \"Mu\", \"0nG\", \n\"20\", \"cY\", \"6Nf\", \"5kd\", \"4vu\", \"5s5\", \"ni\", \"390\", \"0cw\", \"1f7\", \"4M8\", \"4XY\", \"4WX\", \"4B9\", \"OD\", \"0lv\", \"0BZ\", \"ah\", \"6LW\", \"4yt\", \n\"40L\", \"6Ag\", \"lX\", \"y9\", \"0aF\", \"Bt\", \"6oK\", \"4Zh\", \"1Mc\", \"2ha\", \"6SO\", \"4fl\", \"5Xa\", \"69k\", \"2FM\", \"07f\", \"83N\", \"d5F\", \"6pS\", \"4Ep\", \n\"bQo\", \"a0e\", \"7H\", \"0Pr\", \"1OR\", \"8I\", \"454\", \"50t\", \"4Jq\", \"5O1\", \"Rm\", \"05W\", \"08g\", \"2IL\", \"66j\", \"4GA\", \"4im\", \"7Lo\", \"5y\", \"0RC\", \n\"os\", \"0LA\", \"43g\", \"6BL\", \"78I\", \"4YC\", \"0bm\", \"2WN\", \"Lo\", \"8fE\", \"4Ts\", \"5Q3\", \"aen\", \"cPM\", \"0Aq\", \"bC\", \"mB\", \"0Np\", \"41V\", \"ajo\", \n\"6nQ\", \"5KS\", \"14u\", \"Cn\", \"2XO\", \"0ml\", \"4VB\", \"6ca\", \"6MM\", \"4xn\", \"1Sa\", \"22k\", \"Sj\", \"04P\", \"4Kv\", \"5N6\", \"443\", \"4eZ\", \"1NU\", \"9N\", \n\"pv\", \"0SD\", \"4hj\", \"7Mh\", \"67m\", \"4FF\", \"1mI\", \"2HK\", \"2GJ\", \"2\", \"4IG\", \"68l\", \"6RH\", \"4gk\", \"1Ld\", \"2if\", \"6O\", \"0Qu\", \"5zz\", \"7OY\", \n\"5A7\", \"4Dw\", \"1ox\", \"0j8\", \"15r\", \"Bi\", \"6oV\", \"4Zu\", \"40Q\", \"4a8\", \"lE\", \"0Ow\", \"0BG\", \"au\", \"6LJ\", \"4yi\", \"4WE\", \"6bf\", \"OY\", \"Z8\", \n\"U9\", \"2VI\", \"6mg\", \"4XD\", \"4vh\", \"6CK\", \"nt\", \"0MF\", \"1PW\", \"cD\", \"4n9\", \"5ky\", \"4Ut\", \"5P4\", \"Mh\", \"0nZ\", \"4ip\", \"5l0\", \"5d\", \"9Kg\", \n\"08z\", \"1y2\", \"66w\", \"737\", \"4Jl\", \"7on\", \"Rp\", \"05J\", \"1OO\", \"8T\", \"6Qc\", \"50i\", \"4kA\", \"7NC\", \"7U\", \"0Po\", \"1nb\", \"dqS\", \"64F\", \"4Em\", \n\"b6f\", \"69v\", \"PA\", \"0ss\", \"8TG\", \"dRO\", \"5c1\", \"4fq\", \"6MP\", \"4xs\", \"376\", \"22v\", \"NC\", \"0mq\", \"bll\", \"77U\", \"6nL\", \"5KN\", \"14h\", \"Cs\", \n\"3ko\", \"0Nm\", \"41K\", \"7PA\", \"6Oa\", \"4zB\", \"37\", \"20G\", \"Lr\", \"8fX\", \"4Tn\", \"6aM\", \"78T\", \"bcm\", \"0bp\", \"AB\", \"on\", \"387\", \"43z\", \"5r2\", \n\"447\", \"51w\", \"1NQ\", \"9J\", \"Sn\", \"04T\", \"4Kr\", \"5N2\", \"67i\", \"4FB\", \"09d\", \"2HO\", \"4z\", \"1Ca\", \"4hn\", \"7Ml\", \"6RL\", \"4go\", \"8UY\", \"2ib\", \n\"2GN\", \"6\", \"4IC\", \"68h\", \"5A3\", \"4Ds\", \"82M\", \"d4E\", \"6K\", \"0Qq\", \"bPl\", \"a1f\", \"40U\", \"akl\", \"lA\", \"0Os\", \"15v\", \"Bm\", \"6oR\", \"4Zq\", \n\"4WA\", \"6bb\", \"2YL\", \"0lo\", \"0BC\", \"aq\", \"6LN\", \"4ym\", \"42d\", \"6CO\", \"np\", \"0MB\", \"0cn\", \"2VM\", \"6mc\", \"5Ha\", \"4Up\", \"5P0\", \"Ml\", \"8gF\", \n\"1PS\", \"1E2\", \"adm\", \"bAo\", \"1lW\", \"1y6\", \"4R9\", \"4GX\", \"4it\", \"5l4\", \"qh\", \"0RZ\", \"i9\", \"8P\", \"6Qg\", \"4dD\", \"4Jh\", \"7oj\", \"Rt\", \"05N\", \n\"1nf\", \"2Kd\", \"64B\", \"4Ei\", \"4kE\", \"7NG\", \"7Q\", \"f8\", \"1Mz\", \"2hx\", \"5c5\", \"4fu\", \"4HY\", \"69r\", \"PE\", \"0sw\", \"NG\", \"0mu\", \"5Fz\", \"6cx\", \n\"6MT\", \"4xw\", \"0CY\", \"0V8\", \"3kk\", \"0Ni\", \"41O\", \"7PE\", \"6nH\", \"5KJ\", \"14l\", \"Cw\", \"Lv\", \"0oD\", \"4Tj\", \"6aI\", \"6Oe\", \"4zF\", \"33\", \"bZ\", \n\"oj\", \"0LX\", \"4wv\", \"5r6\", \"6ly\", \"4YZ\", \"0bt\", \"AF\", \"4v\", \"0SL\", \"4hb\", \"awS\", \"67e\", \"4FN\", \"K3\", \"2HC\", \"Sb\", \"04X\", \"b5E\", \"aTO\", \n\"4p3\", \"4eR\", \"8Wd\", \"9F\", \"6G\", \"257\", \"4jS\", \"7OQ\", \"65T\", \"cnm\", \"1op\", \"0j0\", \"QS\", \"D2\", \"4IO\", \"68d\", \"7Ba\", \"4gc\", \"1Ll\", \"2in\", \n\"0BO\", \"23d\", \"6LB\", \"4ya\", \"4WM\", \"6bn\", \"OQ\", \"Z0\", \"0aS\", \"Ba\", \"aEL\", \"c4g\", \"40Y\", \"4a0\", \"lM\", \"8Fg\", \"8If\", \"cL\", \"4n1\", \"5kq\", \n\"616\", \"74v\", \"3KP\", \"0nR\", \"U1\", \"2VA\", \"6mo\", \"4XL\", \"42h\", \"6CC\", \"2xm\", \"0MN\", \"4Jd\", \"7of\", \"Rx\", \"05B\", \"i5\", \"2jE\", \"6Qk\", \"4dH\", \n\"4ix\", \"5l8\", \"5l\", \"0RV\", \"08r\", \"2IY\", \"4R5\", \"4GT\", \"4HU\", \"7mW\", \"PI\", \"07s\", \"1Mv\", \"0H6\", \"5c9\", \"4fy\", \"4kI\", \"7NK\", \"sU\", \"f4\", \n\"1nj\", \"2Kh\", \"64N\", \"4Ee\", \"6nD\", \"5KF\", \"1ph\", \"2Uj\", \"mW\", \"x6\", \"41C\", \"7PI\", \"593\", \"5hZ\", \"0CU\", \"0V4\", \"NK\", \"0my\", \"4VW\", \"4C6\", \n\"4L7\", \"4YV\", \"0bx\", \"AJ\", \"of\", \"0LT\", \"43r\", \"562\", \"6Oi\", \"4zJ\", \"w7\", \"bV\", \"Lz\", \"0oH\", \"4Tf\", \"6aE\", \"67a\", \"4FJ\", \"K7\", \"2HG\", \n\"4r\", \"0SH\", \"4hf\", \"7Md\", \"4p7\", \"4eV\", \"1NY\", \"9B\", \"Sf\", \"0pT\", \"4Kz\", \"7nx\", \"65P\", \"5TZ\", \"1ot\", \"0j4\", \"6C\", \"0Qy\", \"4jW\", \"7OU\", \n\"6RD\", \"4gg\", \"1Lh\", \"2ij\", \"QW\", \"D6\", \"4IK\", \"7lI\", \"4WI\", \"6bj\", \"OU\", \"Z4\", \"0BK\", \"ay\", \"6LF\", \"4ye\", \"4tU\", \"4a4\", \"lI\", \"2o9\", \n\"0aW\", \"Be\", \"6oZ\", \"4Zy\", \"4Ux\", \"5P8\", \"Md\", \"0nV\", \"8Ib\", \"cH\", \"4n5\", \"5ku\", \"42l\", \"6CG\", \"nx\", \"0MJ\", \"U5\", \"2VE\", \"6mk\", \"4XH\", \n\"i1\", \"8X\", \"6Qo\", \"4dL\", \"5ZA\", \"7ob\", \"2Dm\", \"05F\", \"08v\", \"d7f\", \"4R1\", \"4GP\", \"bSO\", \"a2E\", \"5h\", \"0RR\", \"1Mr\", \"0H2\", \"ayL\", \"52T\", \n\"4HQ\", \"69z\", \"PM\", \"07w\", \"1nn\", \"2Kl\", \"64J\", \"4Ea\", \"4kM\", \"7NO\", \"7Y\", \"f0\", \"mS\", \"x2\", \"41G\", \"7PM\", \"aDR\", \"5KB\", \"14d\", \"2Un\", \n\"NO\", \"19T\", \"4VS\", \"4C2\", \"597\", \"bBL\", \"0CQ\", \"0V0\", \"ob\", \"0LP\", \"43v\", \"566\", \"4L3\", \"4YR\", \"16U\", \"AN\", \"2Zo\", \"0oL\", \"4Tb\", \"6aA\", \n\"6Om\", \"4zN\", \"w3\", \"bR\", \"4oT\", \"4z5\", \"wH\", \"0Tz\", \"0zV\", \"Yd\", \"5D8\", \"4Ax\", \"4LH\", \"6yk\", \"TT\", \"A5\", \"0YJ\", \"zx\", \"6WG\", \"4bd\", \n\"4me\", \"6XF\", \"1q\", \"0VK\", \"N4\", \"2MD\", \"62b\", \"4CI\", \"4Ny\", \"5K9\", \"Ve\", \"0uW\", \"1KZ\", \"xI\", \"4u4\", \"5pt\", \"4k6\", \"5nv\", \"0Ey\", \"fK\", \n\"Hg\", \"0kU\", \"641\", \"6eX\", \"6hh\", \"5Mj\", \"P6\", \"EW\", \"29b\", \"0HI\", \"47o\", \"6FD\", \"6IE\", \"48n\", \"0GH\", \"dz\", \"JV\", \"0id\", \"4RJ\", \"6gi\", \n\"6jY\", \"beI\", \"0dT\", \"Gf\", \"iJ\", \"0Jx\", \"4qV\", \"4d7\", \"UN\", \"02t\", \"4MR\", \"4X3\", \"a8G\", \"57W\", \"0XP\", \"0M1\", \"2Z\", \"c3\", \"4nN\", \"7KL\", \n\"61I\", \"5PC\", \"1km\", \"2No\", \"2An\", \"00E\", \"4Oc\", \"7ja\", \"6Tl\", \"4aO\", \"l2\", \"yS\", \"0k\", \"0WQ\", \"58V\", \"a7F\", \"4W2\", \"4BS\", \"84m\", \"ZO\", \n\"13V\", \"E\", \"4I0\", \"5Lp\", \"46u\", \"535\", \"ja\", \"0IS\", \"68\", \"gQ\", \"6Jn\", \"5ol\", \"4Qa\", \"6dB\", \"3OM\", \"0jO\", \"0eN\", \"2Pm\", \"6kC\", \"5NA\", \n\"44D\", \"6Eo\", \"hP\", \"99\", \"0FR\", \"0S3\", \"abM\", \"49t\", \"4SP\", \"4F1\", \"KL\", \"8af\", \"0zR\", \"0o3\", \"60W\", \"ckn\", \"4oP\", \"4z1\", \"3D\", \"204\", \n\"0YN\", \"2lm\", \"6WC\", \"56I\", \"4LL\", \"6yo\", \"TP\", \"A1\", \"N0\", \"903\", \"62f\", \"4CM\", \"4ma\", \"6XB\", \"1u\", \"0VO\", \"8Rg\", \"xM\", \"418\", \"54x\", \n\"b0F\", \"aQL\", \"Va\", \"0uS\", \"Hc\", \"0kQ\", \"645\", \"71u\", \"4k2\", \"5nr\", \"8Le\", \"fO\", \"29f\", \"0HM\", \"47k\", \"7Va\", \"6hl\", \"5Mn\", \"P2\", \"ES\", \n\"JR\", \"1yA\", \"4RN\", \"6gm\", \"6IA\", \"48j\", \"0GL\", \"26g\", \"iN\", \"8Cd\", \"45Z\", \"4d3\", \"hYv\", \"beM\", \"0dP\", \"Gb\", \"6VY\", \"4cz\", \"0XT\", \"0M5\", \n\"UJ\", \"02p\", \"4MV\", \"4X7\", \"61M\", \"5PG\", \"1ki\", \"Xz\", \"vV\", \"c7\", \"4nJ\", \"7KH\", \"6Th\", \"4aK\", \"l6\", \"yW\", \"2Aj\", \"00A\", \"4Og\", \"6zD\", \n\"4W6\", \"4BW\", \"0yy\", \"ZK\", \"0o\", \"0WU\", \"58R\", \"6YX\", \"46q\", \"531\", \"je\", \"0IW\", \"13R\", \"A\", \"4I4\", \"5Lt\", \"4Qe\", \"6dF\", \"Iy\", \"0jK\", \n\"r4\", \"gU\", \"6Jj\", \"5oh\", \"4pH\", \"6Ek\", \"hT\", \"0Kf\", \"0eJ\", \"Fx\", \"6kG\", \"5NE\", \"4ST\", \"4F5\", \"KH\", \"0hz\", \"0FV\", \"ed\", \"5x8\", \"49p\", \n\"bvs\", \"6yc\", \"2BM\", \"03f\", \"0YB\", \"zp\", \"6WO\", \"4bl\", \"bUo\", \"a4e\", \"3H\", \"0Tr\", \"87N\", \"Yl\", \"5D0\", \"4Ap\", \"4Nq\", \"5K1\", \"Vm\", \"01W\", \n\"1KR\", \"xA\", \"414\", \"54t\", \"4mm\", \"6XN\", \"1y\", \"0VC\", \"0xo\", \"2ML\", \"62j\", \"4CA\", \"7xA\", \"5Mb\", \"0fm\", \"2SN\", \"ks\", \"0HA\", \"47g\", \"6FL\", \n\"aan\", \"bDl\", \"0Eq\", \"fC\", \"Ho\", \"8bE\", \"4Ps\", \"5U3\", \"5Z2\", \"5OS\", \"10u\", \"Gn\", \"iB\", \"0Jp\", \"45V\", \"ano\", \"6IM\", \"48f\", \"1Wa\", \"dr\", \n\"3Ln\", \"0il\", \"4RB\", \"6ga\", \"2R\", \"0Uh\", \"4nF\", \"7KD\", \"61A\", \"5PK\", \"1ke\", \"Xv\", \"UF\", \"0vt\", \"4MZ\", \"6xy\", \"5f6\", \"4cv\", \"0XX\", \"0M9\", \n\"0c\", \"0WY\", \"4lw\", \"5i7\", \"63p\", \"5Rz\", \"0yu\", \"ZG\", \"Ww\", \"00M\", \"4Ok\", \"6zH\", \"6Td\", \"4aG\", \"0Zi\", \"2oJ\", \"60\", \"gY\", \"6Jf\", \"5od\", \n\"4Qi\", \"6dJ\", \"Iu\", \"0jG\", \"0gw\", \"M\", \"4I8\", \"5Lx\", \"4ru\", \"5w5\", \"ji\", \"1Yz\", \"0FZ\", \"eh\", \"5x4\", \"5mU\", \"4SX\", \"4F9\", \"KD\", \"0hv\", \n\"0eF\", \"Ft\", \"6kK\", \"5NI\", \"44L\", \"6Eg\", \"hX\", \"91\", \"0YF\", \"zt\", \"6WK\", \"4bh\", \"4LD\", \"6yg\", \"TX\", \"A9\", \"0zZ\", \"Yh\", \"5D4\", \"4At\", \n\"4oX\", \"4z9\", \"3L\", \"0Tv\", \"1KV\", \"xE\", \"410\", \"54p\", \"4Nu\", \"5K5\", \"Vi\", \"01S\", \"N8\", \"2MH\", \"62n\", \"4CE\", \"4mi\", \"6XJ\", \"uu\", \"0VG\", \n\"kw\", \"0HE\", \"47c\", \"6FH\", \"6hd\", \"5Mf\", \"0fi\", \"2SJ\", \"Hk\", \"0kY\", \"4Pw\", \"5U7\", \"6Kx\", \"5nz\", \"0Eu\", \"fG\", \"iF\", \"0Jt\", \"45R\", \"6Dy\", \n\"5Z6\", \"5OW\", \"0dX\", \"Gj\", \"JZ\", \"0ih\", \"4RF\", \"6ge\", \"6II\", \"48b\", \"0GD\", \"dv\", \"61E\", \"5PO\", \"1ka\", \"Xr\", \"2V\", \"0Ul\", \"4nB\", \"aqs\", \n\"5f2\", \"4cr\", \"8QD\", \"39V\", \"UB\", \"02x\", \"795\", \"aRo\", \"63t\", \"764\", \"0yq\", \"ZC\", \"0g\", \"9Nd\", \"4ls\", \"5i3\", \"7DA\", \"4aC\", \"0Zm\", \"2oN\", \n\"Ws\", \"00I\", \"4Oo\", \"6zL\", \"4Qm\", \"6dN\", \"Iq\", \"0jC\", \"64\", \"25D\", \"6Jb\", \"bEr\", \"46y\", \"539\", \"jm\", \"9Pf\", \"0gs\", \"I\", \"aCl\", \"bfn\", \n\"bio\", \"72V\", \"1m2\", \"0hr\", \"325\", \"el\", \"5x0\", \"49x\", \"44H\", \"6Ec\", \"3nl\", \"95\", \"0eB\", \"Fp\", \"6kO\", \"5NM\", \"4mt\", \"5h4\", \"uh\", \"0VZ\", \n\"0xv\", \"2MU\", \"4V9\", \"4CX\", \"4Nh\", \"7kj\", \"Vt\", \"01N\", \"m9\", \"xX\", \"6Ug\", \"54m\", \"4oE\", \"6Zf\", \"3Q\", \"b8\", \"0zG\", \"Yu\", \"60B\", \"4Ai\", \n\"4LY\", \"4Y8\", \"TE\", \"0ww\", \"1Iz\", \"zi\", \"5g5\", \"4bu\", \"5y7\", \"5lV\", \"0GY\", \"dk\", \"JG\", \"0iu\", \"5Bz\", \"6gx\", \"6jH\", \"5OJ\", \"0dE\", \"Gw\", \n\"3ok\", \"82\", \"45O\", \"6Dd\", \"6Ke\", \"5ng\", \"73\", \"fZ\", \"Hv\", \"0kD\", \"4Pj\", \"6eI\", \"6hy\", \"7m9\", \"0ft\", \"EF\", \"kj\", \"0HX\", \"4sv\", \"5v6\", \n\"Wn\", \"00T\", \"4Or\", \"5J2\", \"407\", \"55w\", \"0Zp\", \"yB\", \"0z\", \"1Ga\", \"4ln\", \"6YM\", \"63i\", \"4BB\", \"0yl\", \"2LO\", \"2CN\", \"02e\", \"4MC\", \"7hA\", \n\"6VL\", \"4co\", \"0XA\", \"2mb\", \"2K\", \"0Uq\", \"bTl\", \"a5f\", \"5E3\", \"5PR\", \"86M\", \"Xo\", \"11v\", \"Fm\", \"6kR\", \"5NP\", \"44U\", \"aol\", \"hA\", \"0Ks\", \n\"0FC\", \"eq\", \"6HN\", \"49e\", \"4SA\", \"6fb\", \"3Mm\", \"0ho\", \"0gn\", \"T\", \"6ic\", \"5La\", \"46d\", \"6GO\", \"jp\", \"0IB\", \"0Dr\", \"1A2\", \"hyT\", \"bEo\", \n\"4Qp\", \"5T0\", \"Il\", \"8cF\", \"0xr\", \"2MQ\", \"62w\", \"777\", \"4mp\", \"5h0\", \"1d\", \"9Og\", \"1KO\", \"2nM\", \"6Uc\", \"54i\", \"4Nl\", \"7kn\", \"Vp\", \"01J\", \n\"0zC\", \"Yq\", \"60F\", \"4Am\", \"4oA\", \"6Zb\", \"3U\", \"0To\", \"8PG\", \"zm\", \"5g1\", \"4bq\", \"786\", \"aSl\", \"TA\", \"0ws\", \"JC\", \"0iq\", \"bhl\", \"73U\", \n\"5y3\", \"5lR\", \"336\", \"do\", \"3oo\", \"86\", \"45K\", \"7TA\", \"6jL\", \"5ON\", \"0dA\", \"Gs\", \"Hr\", \"8bX\", \"4Pn\", \"6eM\", \"6Ka\", \"5nc\", \"77\", \"24G\", \n\"kn\", \"8AD\", \"47z\", \"5v2\", \"aBo\", \"bgm\", \"0fp\", \"EB\", \"403\", \"4aZ\", \"0Zt\", \"yF\", \"Wj\", \"00P\", \"4Ov\", \"5J6\", \"63m\", \"4BF\", \"0yh\", \"ZZ\", \n\"tv\", \"0WD\", \"4lj\", \"6YI\", \"6VH\", \"4ck\", \"0XE\", \"2mf\", \"2CJ\", \"02a\", \"4MG\", \"6xd\", \"5E7\", \"5PV\", \"1kx\", \"Xk\", \"2O\", \"0Uu\", \"bTh\", \"7KY\", \n\"44Q\", \"4e8\", \"hE\", \"0Kw\", \"11r\", \"Fi\", \"6kV\", \"5NT\", \"4SE\", \"6ff\", \"KY\", \"0hk\", \"0FG\", \"eu\", \"6HJ\", \"49a\", \"4rh\", \"6GK\", \"jt\", \"0IF\", \n\"Q9\", \"P\", \"6ig\", \"5Le\", \"4Qt\", \"5T4\", \"Ih\", \"0jZ\", \"0Dv\", \"gD\", \"4j9\", \"5oy\", \"aD0\", \"7kb\", \"3PL\", \"01F\", \"m1\", \"xP\", \"6Uo\", \"54e\", \n\"59U\", \"a6E\", \"1h\", \"0VR\", \"85n\", \"196\", \"4V1\", \"4CP\", \"4LQ\", \"4Y0\", \"TM\", \"03w\", \"0YS\", \"za\", \"a9D\", \"56T\", \"4oM\", \"6Zn\", \"3Y\", \"b0\", \n\"0zO\", \"2Ol\", \"60J\", \"4Aa\", \"7za\", \"5OB\", \"0dM\", \"2Qn\", \"iS\", \"0Ja\", \"45G\", \"6Dl\", \"acN\", \"48w\", \"0GQ\", \"dc\", \"JO\", \"94L\", \"4RS\", \"4G2\", \n\"4H3\", \"5Ms\", \"12U\", \"EN\", \"kb\", \"0HP\", \"47v\", \"526\", \"6Km\", \"5no\", \"s3\", \"fR\", \"3NN\", \"0kL\", \"4Pb\", \"6eA\", \"0r\", \"0WH\", \"4lf\", \"6YE\", \n\"63a\", \"4BJ\", \"O7\", \"ZV\", \"Wf\", \"0tT\", \"4Oz\", \"6zY\", \"4t7\", \"4aV\", \"0Zx\", \"yJ\", \"2C\", \"0Uy\", \"4nW\", \"7KU\", \"61P\", \"5PZ\", \"1kt\", \"Xg\", \n\"UW\", \"02m\", \"4MK\", \"6xh\", \"6VD\", \"4cg\", \"0XI\", \"2mj\", \"0FK\", \"ey\", \"6HF\", \"49m\", \"4SI\", \"6fj\", \"KU\", \"0hg\", \"0eW\", \"Fe\", \"6kZ\", \"5NX\", \n\"4pU\", \"4e4\", \"hI\", \"2k9\", \"0Dz\", \"gH\", \"4j5\", \"5ou\", \"4Qx\", \"5T8\", \"Id\", \"0jV\", \"Q5\", \"DT\", \"6ik\", \"5Li\", \"46l\", \"6GG\", \"jx\", \"0IJ\", \n\"m5\", \"xT\", \"6Uk\", \"54a\", \"4Nd\", \"7kf\", \"Vx\", \"01B\", \"0xz\", \"192\", \"4V5\", \"4CT\", \"4mx\", \"5h8\", \"1l\", \"0VV\", \"0YW\", \"ze\", \"5g9\", \"4by\", \n\"4LU\", \"4Y4\", \"TI\", \"03s\", \"0zK\", \"Yy\", \"60N\", \"4Ae\", \"4oI\", \"6Zj\", \"wU\", \"b4\", \"iW\", \"0Je\", \"45C\", \"6Dh\", \"6jD\", \"5OF\", \"0dI\", \"2Qj\", \n\"JK\", \"0iy\", \"4RW\", \"4G6\", \"6IX\", \"48s\", \"0GU\", \"dg\", \"kf\", \"0HT\", \"47r\", \"522\", \"4H7\", \"5Mw\", \"0fx\", \"EJ\", \"Hz\", \"0kH\", \"4Pf\", \"6eE\", \n\"6Ki\", \"5nk\", \"s7\", \"fV\", \"63e\", \"4BN\", \"O3\", \"ZR\", \"0v\", \"0WL\", \"4lb\", \"6YA\", \"4t3\", \"4aR\", \"8Sd\", \"yN\", \"Wb\", \"00X\", \"b1E\", \"aPO\", \n\"61T\", \"bzL\", \"1kp\", \"Xc\", \"2G\", \"217\", \"4nS\", \"7KQ\", \"7Fa\", \"4cc\", \"0XM\", \"2mn\", \"US\", \"02i\", \"4MO\", \"6xl\", \"4SM\", \"6fn\", \"KQ\", \"0hc\", \n\"0FO\", \"27d\", \"6HB\", \"49i\", \"44Y\", \"4e0\", \"hM\", \"8Bg\", \"0eS\", \"Fa\", \"aAL\", \"bdN\", \"656\", \"70v\", \"3OP\", \"0jR\", \"8Mf\", \"gL\", \"4j1\", \"5oq\", \n\"46h\", \"6GC\", \"28e\", \"0IN\", \"Q1\", \"X\", \"6io\", \"5Lm\", \"6KV\", \"5nT\", \"1Uz\", \"fi\", \"HE\", \"0kw\", \"4PY\", \"4E8\", \"6hJ\", \"5MH\", \"0fG\", \"Eu\", \n\"kY\", \"0Hk\", \"47M\", \"6Ff\", \"6Ig\", \"48L\", \"51\", \"dX\", \"Jt\", \"0iF\", \"4Rh\", \"6gK\", \"4J9\", \"5Oy\", \"0dv\", \"GD\", \"ih\", \"0JZ\", \"4qt\", \"5t4\", \n\"4ov\", \"5j6\", \"3b\", \"0TX\", \"0zt\", \"YF\", \"60q\", \"4AZ\", \"4Lj\", \"6yI\", \"Tv\", \"03L\", \"0Yh\", \"zZ\", \"6We\", \"4bF\", \"4mG\", \"6Xd\", \"1S\", \"0Vi\", \n\"0xE\", \"2Mf\", \"6vH\", \"4Ck\", \"bth\", \"7kY\", \"VG\", \"0uu\", \"1Kx\", \"xk\", \"5e7\", \"5pV\", \"13t\", \"g\", \"5Y3\", \"5LR\", \"46W\", \"amn\", \"jC\", \"0Iq\", \n\"0DA\", \"gs\", \"6JL\", \"5oN\", \"4QC\", \"70I\", \"3Oo\", \"0jm\", \"0el\", \"2PO\", \"6ka\", \"5Nc\", \"44f\", \"6EM\", \"hr\", \"8BX\", \"0Fp\", \"eB\", \"abo\", \"49V\", \n\"4Sr\", \"5V2\", \"Kn\", \"8aD\", \"Ul\", \"02V\", \"4Mp\", \"5H0\", \"425\", \"57u\", \"0Xr\", \"2mQ\", \"2x\", \"0UB\", \"4nl\", \"7Kn\", \"61k\", \"5Pa\", \"1kO\", \"2NM\", \n\"2AL\", \"00g\", \"4OA\", \"6zb\", \"6TN\", \"4am\", \"0ZC\", \"yq\", \"0I\", \"0Ws\", \"58t\", \"a7d\", \"5G1\", \"4Bq\", \"84O\", \"Zm\", \"HA\", \"0ks\", \"bjn\", \"71W\", \n\"6KR\", \"5nP\", \"314\", \"fm\", \"29D\", \"0Ho\", \"47I\", \"6Fb\", \"6hN\", \"5ML\", \"0fC\", \"Eq\", \"Jp\", \"0iB\", \"4Rl\", \"6gO\", \"6Ic\", \"48H\", \"55\", \"26E\", \n\"il\", \"8CF\", \"45x\", \"508\", \"hYT\", \"beo\", \"0dr\", \"1a2\", \"0zp\", \"YB\", \"60u\", \"755\", \"4or\", \"5j2\", \"3f\", \"9Me\", \"0Yl\", \"2lO\", \"6Wa\", \"4bB\", \n\"4Ln\", \"6yM\", \"Tr\", \"03H\", \"0xA\", \"2Mb\", \"62D\", \"4Co\", \"4mC\", \"7HA\", \"1W\", \"0Vm\", \"8RE\", \"xo\", \"5e3\", \"54Z\", \"b0d\", \"aQn\", \"VC\", \"01y\", \n\"46S\", \"6Gx\", \"jG\", \"0Iu\", \"0gY\", \"c\", \"5Y7\", \"5LV\", \"4QG\", \"6dd\", \"3Ok\", \"0ji\", \"0DE\", \"gw\", \"6JH\", \"5oJ\", \"44b\", \"6EI\", \"hv\", \"0KD\", \n\"0eh\", \"FZ\", \"6ke\", \"5Ng\", \"4Sv\", \"5V6\", \"Kj\", \"0hX\", \"0Ft\", \"eF\", \"6Hy\", \"49R\", \"421\", \"4cX\", \"0Xv\", \"2mU\", \"Uh\", \"02R\", \"4Mt\", \"5H4\", \n\"61o\", \"5Pe\", \"M9\", \"XX\", \"vt\", \"0UF\", \"4nh\", \"7Kj\", \"6TJ\", \"4ai\", \"0ZG\", \"yu\", \"WY\", \"B8\", \"4OE\", \"6zf\", \"5G5\", \"4Bu\", \"1iz\", \"Zi\", \n\"0M\", \"0Ww\", \"4lY\", \"4y8\", \"6hB\", \"aW1\", \"0fO\", \"2Sl\", \"kQ\", \"0Hc\", \"47E\", \"6Fn\", \"aaL\", \"bDN\", \"0ES\", \"fa\", \"HM\", \"8bg\", \"4PQ\", \"4E0\", \n\"4J1\", \"5Oq\", \"10W\", \"GL\", \"3oP\", \"0JR\", \"45t\", \"504\", \"6Io\", \"48D\", \"59\", \"dP\", \"3LL\", \"0iN\", \"5BA\", \"6gC\", \"4Lb\", \"6yA\", \"2Bo\", \"03D\", \n\"o3\", \"zR\", \"6Wm\", \"4bN\", \"bUM\", \"a4G\", \"3j\", \"0TP\", \"87l\", \"YN\", \"4T3\", \"4AR\", \"4NS\", \"7kQ\", \"VO\", \"01u\", \"1Kp\", \"xc\", \"hfw\", \"54V\", \n\"4mO\", \"6Xl\", \"uS\", \"0Va\", \"0xM\", \"2Mn\", \"62H\", \"4Cc\", \"0DI\", \"25b\", \"6JD\", \"5oF\", \"4QK\", \"6dh\", \"IW\", \"0je\", \"0gU\", \"o\", \"6iX\", \"5LZ\", \n\"4rW\", \"4g6\", \"jK\", \"0Iy\", \"0Fx\", \"eJ\", \"4h7\", \"5mw\", \"4Sz\", \"6fY\", \"Kf\", \"0hT\", \"S7\", \"FV\", \"6ki\", \"5Nk\", \"44n\", \"6EE\", \"hz\", \"0KH\", \n\"2p\", \"0UJ\", \"4nd\", \"7Kf\", \"61c\", \"5Pi\", \"M5\", \"XT\", \"Ud\", \"0vV\", \"4Mx\", \"5H8\", \"4v5\", \"4cT\", \"0Xz\", \"2mY\", \"0A\", \"1GZ\", \"4lU\", \"4y4\", \n\"5G9\", \"4By\", \"0yW\", \"Ze\", \"WU\", \"B4\", \"4OI\", \"6zj\", \"6TF\", \"4ae\", \"0ZK\", \"yy\", \"kU\", \"0Hg\", \"47A\", \"6Fj\", \"6hF\", \"5MD\", \"0fK\", \"Ey\", \n\"HI\", \"2K9\", \"4PU\", \"4E4\", \"6KZ\", \"5nX\", \"0EW\", \"fe\", \"id\", \"0JV\", \"45p\", \"500\", \"4J5\", \"5Ou\", \"0dz\", \"GH\", \"Jx\", \"0iJ\", \"4Rd\", \"6gG\", \n\"6Ik\", \"5li\", \"q5\", \"dT\", \"o7\", \"zV\", \"6Wi\", \"4bJ\", \"4Lf\", \"6yE\", \"Tz\", \"0wH\", \"0zx\", \"YJ\", \"4T7\", \"4AV\", \"4oz\", \"6ZY\", \"3n\", \"0TT\", \n\"1Kt\", \"xg\", \"6UX\", \"54R\", \"4NW\", \"7kU\", \"VK\", \"01q\", \"0xI\", \"2Mj\", \"62L\", \"4Cg\", \"4mK\", \"6Xh\", \"uW\", \"0Ve\", \"4QO\", \"6dl\", \"IS\", \"0ja\", \n\"0DM\", \"25f\", \"7Za\", \"5oB\", \"4rS\", \"4g2\", \"jO\", \"9PD\", \"0gQ\", \"k\", \"aCN\", \"685\", \"674\", \"72t\", \"Kb\", \"0hP\", \"8Od\", \"eN\", \"4h3\", \"49Z\", \n\"44j\", \"6EA\", \"3nN\", \"0KL\", \"S3\", \"FR\", \"6km\", \"5No\", \"61g\", \"5Pm\", \"M1\", \"XP\", \"2t\", \"0UN\", \"ad0\", \"7Kb\", \"429\", \"4cP\", \"8Qf\", \"39t\", \n\"0c3\", \"02Z\", \"b3G\", \"aRM\", \"63V\", \"bxN\", \"0yS\", \"Za\", \"0E\", \"235\", \"4lQ\", \"4y0\", \"6TB\", \"4aa\", \"0ZO\", \"2ol\", \"WQ\", \"B0\", \"4OM\", \"6zn\", \n\"4i4\", \"5lt\", \"1WZ\", \"dI\", \"Je\", \"0iW\", \"4Ry\", \"5W9\", \"6jj\", \"5Oh\", \"R4\", \"GU\", \"iy\", \"0JK\", \"45m\", \"6DF\", \"6KG\", \"5nE\", \"0EJ\", \"fx\", \n\"HT\", \"0kf\", \"4PH\", \"6ek\", \"5X8\", \"5MY\", \"0fV\", \"Ed\", \"kH\", \"0Hz\", \"4sT\", \"4f5\", \"4mV\", \"4x7\", \"1B\", \"0Vx\", \"0xT\", \"0m5\", \"62Q\", \"4Cz\", \n\"4NJ\", \"7kH\", \"VV\", \"C7\", \"1Ki\", \"xz\", \"6UE\", \"54O\", \"4og\", \"6ZD\", \"3s\", \"0TI\", \"L6\", \"YW\", \"6th\", \"4AK\", \"6l9\", \"6yX\", \"Tg\", \"0wU\", \n\"0Yy\", \"zK\", \"4w6\", \"4bW\", \"11T\", \"FO\", \"4K2\", \"5Nr\", \"44w\", \"517\", \"hc\", \"0KQ\", \"p2\", \"eS\", \"6Hl\", \"49G\", \"4Sc\", \"72i\", \"3MO\", \"0hM\", \n\"0gL\", \"v\", \"6iA\", \"5LC\", \"46F\", \"6Gm\", \"jR\", \"1YA\", \"0DP\", \"gb\", \"hyv\", \"bEM\", \"4QR\", \"4D3\", \"IN\", \"8cd\", \"WL\", \"00v\", \"4OP\", \"4Z1\", \n\"hgt\", \"55U\", \"0ZR\", \"0O3\", \"0X\", \"a1\", \"4lL\", \"6Yo\", \"63K\", \"5RA\", \"0yN\", \"2Lm\", \"2Cl\", \"02G\", \"4Ma\", \"6xB\", \"6Vn\", \"4cM\", \"n0\", \"39i\", \n\"2i\", \"0US\", \"bTN\", \"a5D\", \"4U0\", \"5Pp\", \"86o\", \"XM\", \"Ja\", \"0iS\", \"667\", \"73w\", \"4i0\", \"48Y\", \"8Ng\", \"dM\", \"3oM\", \"0JO\", \"45i\", \"6DB\", \n\"6jn\", \"5Ol\", \"R0\", \"GQ\", \"HP\", \"0kb\", \"4PL\", \"6eo\", \"6KC\", \"5nA\", \"0EN\", \"24e\", \"kL\", \"8Af\", \"47X\", \"4f1\", \"aBM\", \"696\", \"0fR\", \"0s3\", \n\"0xP\", \"0m1\", \"62U\", \"byM\", \"4mR\", \"4x3\", \"1F\", \"226\", \"1Km\", \"2no\", \"6UA\", \"54K\", \"4NN\", \"7kL\", \"VR\", \"C3\", \"L2\", \"YS\", \"60d\", \"4AO\", \n\"4oc\", \"7Ja\", \"3w\", \"0TM\", \"8Pe\", \"zO\", \"4w2\", \"4bS\", \"b2D\", \"aSN\", \"Tc\", \"03Y\", \"44s\", \"513\", \"hg\", \"0KU\", \"0ey\", \"FK\", \"4K6\", \"5Nv\", \n\"4Sg\", \"6fD\", \"3MK\", \"0hI\", \"p6\", \"eW\", \"6Hh\", \"49C\", \"46B\", \"6Gi\", \"jV\", \"0Id\", \"0gH\", \"r\", \"6iE\", \"5LG\", \"4QV\", \"4D7\", \"IJ\", \"0jx\", \n\"0DT\", \"gf\", \"6JY\", \"bEI\", \"5d8\", \"4ax\", \"0ZV\", \"yd\", \"WH\", \"00r\", \"4OT\", \"4Z5\", \"63O\", \"4Bd\", \"0yJ\", \"Zx\", \"tT\", \"a5\", \"4lH\", \"6Yk\", \n\"6Vj\", \"4cI\", \"n4\", \"2mD\", \"Uy\", \"02C\", \"4Me\", \"6xF\", \"4U4\", \"5Pt\", \"1kZ\", \"XI\", \"2m\", \"0UW\", \"4ny\", \"5k9\", \"6jb\", \"ber\", \"0do\", \"2QL\", \n\"iq\", \"0JC\", \"45e\", \"6DN\", \"acl\", \"48U\", \"0Gs\", \"dA\", \"Jm\", \"94n\", \"4Rq\", \"5W1\", \"5X0\", \"5MQ\", \"12w\", \"El\", \"1M2\", \"0Hr\", \"47T\", \"alm\", \n\"6KO\", \"5nM\", \"0EB\", \"fp\", \"3Nl\", \"0kn\", \"bjs\", \"6ec\", \"4NB\", \"aQs\", \"3Pn\", \"01d\", \"1Ka\", \"xr\", \"6UM\", \"54G\", \"59w\", \"a6g\", \"1J\", \"0Vp\", \n\"85L\", \"d3D\", \"5F2\", \"4Cr\", \"4Ls\", \"5I3\", \"To\", \"03U\", \"0Yq\", \"zC\", \"436\", \"56v\", \"4oo\", \"6ZL\", \"ws\", \"0TA\", \"0zm\", \"2ON\", \"60h\", \"4AC\", \n\"42\", \"27B\", \"6Hd\", \"49O\", \"4Sk\", \"6fH\", \"Kw\", \"0hE\", \"0eu\", \"FG\", \"6kx\", \"5Nz\", \"4pw\", \"5u7\", \"hk\", \"0KY\", \"0DX\", \"gj\", \"5z6\", \"5oW\", \n\"4QZ\", \"6dy\", \"IF\", \"0jt\", \"0gD\", \"Dv\", \"6iI\", \"5LK\", \"46N\", \"6Ge\", \"jZ\", \"0Ih\", \"0P\", \"a9\", \"4lD\", \"6Yg\", \"63C\", \"4Bh\", \"0yF\", \"Zt\", \n\"WD\", \"0tv\", \"4OX\", \"4Z9\", \"5d4\", \"4at\", \"0ZZ\", \"yh\", \"2a\", \"1Ez\", \"4nu\", \"5k5\", \"4U8\", \"5Px\", \"1kV\", \"XE\", \"Uu\", \"02O\", \"4Mi\", \"6xJ\", \n\"6Vf\", \"4cE\", \"n8\", \"2mH\", \"iu\", \"0JG\", \"45a\", \"6DJ\", \"6jf\", \"5Od\", \"R8\", \"GY\", \"Ji\", \"1yz\", \"4Ru\", \"5W5\", \"4i8\", \"48Q\", \"0Gw\", \"dE\", \n\"kD\", \"0Hv\", \"47P\", \"4f9\", \"5X4\", \"5MU\", \"0fZ\", \"Eh\", \"HX\", \"0kj\", \"4PD\", \"6eg\", \"6KK\", \"5nI\", \"0EF\", \"ft\", \"1Ke\", \"xv\", \"6UI\", \"54C\", \n\"4NF\", \"7kD\", \"VZ\", \"0uh\", \"0xX\", \"0m9\", \"5F6\", \"4Cv\", \"4mZ\", \"6Xy\", \"1N\", \"0Vt\", \"0Yu\", \"zG\", \"432\", \"56r\", \"4Lw\", \"5I7\", \"Tk\", \"03Q\", \n\"0zi\", \"2OJ\", \"60l\", \"4AG\", \"4ok\", \"6ZH\", \"ww\", \"0TE\", \"4So\", \"6fL\", \"Ks\", \"0hA\", \"46\", \"27F\", \"7XA\", \"49K\", \"4ps\", \"5u3\", \"ho\", \"8BE\", \n\"0eq\", \"FC\", \"aAn\", \"bdl\", \"bkm\", \"70T\", \"IB\", \"0jp\", \"307\", \"gn\", \"5z2\", \"5oS\", \"46J\", \"6Ga\", \"28G\", \"0Il\", \"13i\", \"z\", \"6iM\", \"5LO\", \n\"63G\", \"4Bl\", \"0yB\", \"Zp\", \"0T\", \"0Wn\", \"58i\", \"6Yc\", \"5d0\", \"4ap\", \"8SF\", \"yl\", \"1q2\", \"00z\", \"b1g\", \"aPm\", \"61v\", \"746\", \"1kR\", \"XA\", \n\"2e\", \"9Lf\", \"4nq\", \"5k1\", \"6Vb\", \"4cA\", \"0Xo\", \"2mL\", \"Uq\", \"02K\", \"4Mm\", \"6xN\", \"8YG\", \"7e\", \"5n1\", \"4kq\", \"716\", \"64v\", \"2KP\", \"1nR\", \n\"07K\", \"Pq\", \"69F\", \"4Hm\", \"4fA\", \"6Sb\", \"2hL\", \"1MN\", \"0Rn\", \"5T\", \"7LB\", \"5ya\", \"4Gl\", \"66G\", \"2Ia\", \"08J\", \"05z\", \"1t2\", \"aUm\", \"b4g\", \n\"4dp\", \"5a0\", \"8d\", \"8VF\", \"bn\", \"357\", \"4zr\", \"6OQ\", \"75T\", \"bnm\", \"0op\", \"LB\", \"Ar\", \"16i\", \"4Yn\", \"6lM\", \"6Ba\", \"43J\", \"0Ll\", \"2yO\", \n\"22F\", \"16\", \"4xC\", \"agr\", \"6cL\", \"4Vo\", \"0mA\", \"Ns\", \"CC\", \"14X\", \"bal\", \"aDn\", \"5p3\", \"4us\", \"8GE\", \"mo\", \"5L7\", \"4Iw\", \"06Q\", \"Qk\", \n\"1Y5\", \"1LT\", \"53r\", \"462\", \"7Oi\", \"4jk\", \"0QE\", \"rw\", \"2JJ\", \"1oH\", \"4DG\", \"65l\", \"7nD\", \"4KF\", \"0ph\", \"SZ\", \"2kg\", \"1Ne\", \"4ej\", \"6PI\", \n\"493\", \"4hZ\", \"0St\", \"4N\", \"0h9\", \"09P\", \"4Fv\", \"5C6\", \"4Xt\", \"6mW\", \"023\", \"0cZ\", \"0Mv\", \"nD\", \"4c9\", \"42P\", \"5kI\", \"6NK\", \"ct\", \"1Pg\", \n\"X9\", \"MX\", \"74N\", \"4UD\", \"4ZE\", \"6of\", \"BY\", \"W8\", \"0OG\", \"lu\", \"6AJ\", \"40a\", \"4yY\", \"4l8\", \"aE\", \"0Bw\", \"18r\", \"Oi\", \"5R5\", \"4Wu\", \n\"4EY\", \"4P8\", \"2KT\", \"1nV\", \"8YC\", \"7a\", \"5n5\", \"4ku\", \"4fE\", \"6Sf\", \"2hH\", \"k8\", \"07O\", \"Pu\", \"69B\", \"4Hi\", \"4Gh\", \"66C\", \"2Ie\", \"08N\", \n\"d9\", \"5P\", \"7LF\", \"4iD\", \"4dt\", \"5a4\", \"2jy\", \"3o9\", \"0qv\", \"RD\", \"7oZ\", \"4JX\", \"6ay\", \"4TZ\", \"0ot\", \"LF\", \"bj\", \"0AX\", \"4zv\", \"6OU\", \n\"6Be\", \"43N\", \"0Lh\", \"oZ\", \"Av\", \"0bD\", \"4Yj\", \"6lI\", \"6cH\", \"4Vk\", \"0mE\", \"Nw\", \"22B\", \"12\", \"4xG\", \"6Md\", \"5p7\", \"4uw\", \"0NY\", \"mk\", \n\"CG\", \"1pT\", \"5Kz\", \"6nx\", \"1Y1\", \"1LP\", \"53v\", \"466\", \"5L3\", \"4Is\", \"06U\", \"Qo\", \"2JN\", \"1oL\", \"4DC\", \"65h\", \"7Om\", \"4jo\", \"0QA\", \"rs\", \n\"9z\", \"1Na\", \"4en\", \"6PM\", \"aTs\", \"4KB\", \"04d\", \"2EO\", \"d6D\", \"09T\", \"4Fr\", \"5C2\", \"497\", \"bRm\", \"0Sp\", \"4J\", \"0Mr\", \"1H2\", \"aim\", \"42T\", \n\"4Xp\", \"6mS\", \"027\", \"17w\", \"0nn\", \"3Kl\", \"74J\", \"5Ea\", \"5kM\", \"6NO\", \"cp\", \"1Pc\", \"0OC\", \"lq\", \"6AN\", \"40e\", \"4ZA\", \"6ob\", \"2TL\", \"0ao\", \n\"18v\", \"Om\", \"5R1\", \"4Wq\", \"bCn\", \"afl\", \"aA\", \"0Bs\", \"07C\", \"Py\", \"69N\", \"4He\", \"4fI\", \"6Sj\", \"2hD\", \"k4\", \"0PW\", \"7m\", \"5n9\", \"4ky\", \n\"4EU\", \"4P4\", \"2KX\", \"1nZ\", \"05r\", \"RH\", \"7oV\", \"4JT\", \"4dx\", \"5a8\", \"8l\", \"1Ow\", \"d5\", \"qT\", \"7LJ\", \"4iH\", \"4Gd\", \"66O\", \"2Ii\", \"08B\", \n\"Az\", \"0bH\", \"4Yf\", \"6lE\", \"6Bi\", \"43B\", \"z7\", \"oV\", \"bf\", \"0AT\", \"4zz\", \"6OY\", \"4A7\", \"4TV\", \"0ox\", \"LJ\", \"CK\", \"14P\", \"5Kv\", \"4N6\", \n\"543\", \"41s\", \"0NU\", \"mg\", \"22N\", \"u6\", \"4xK\", \"6Mh\", \"6cD\", \"4Vg\", \"0mI\", \"2Xj\", \"7Oa\", \"4jc\", \"0QM\", \"6w\", \"2JB\", \"I2\", \"4DO\", \"65d\", \n\"68T\", \"b7D\", \"06Y\", \"Qc\", \"dSm\", \"287\", \"4gS\", \"4r2\", \"7MP\", \"4hR\", \"276\", \"4F\", \"0h1\", \"09X\", \"b8E\", \"67U\", \"7nL\", \"4KN\", \"F3\", \"SR\", \n\"9v\", \"1Nm\", \"4eb\", \"6PA\", \"5kA\", \"6NC\", \"21e\", \"1Po\", \"X1\", \"MP\", \"74F\", \"4UL\", \"bbO\", \"79v\", \"0v3\", \"0cR\", \"8Df\", \"nL\", \"4c1\", \"42X\", \n\"4yQ\", \"4l0\", \"aM\", \"8Kg\", \"0lS\", \"Oa\", \"76w\", \"637\", \"4ZM\", \"6on\", \"BQ\", \"W0\", \"0OO\", \"2zl\", \"6AB\", \"40i\", \"4fM\", \"6Sn\", \"3xa\", \"k0\", \n\"07G\", \"2Fl\", \"69J\", \"4Ha\", \"4EQ\", \"4P0\", \"d5g\", \"83o\", \"0PS\", \"7i\", \"a0D\", \"bQN\", \"50U\", \"hbt\", \"8h\", \"1Os\", \"05v\", \"RL\", \"7oR\", \"4JP\", \n\"5WA\", \"66K\", \"2Im\", \"08F\", \"d1\", \"5X\", \"7LN\", \"4iL\", \"6Bm\", \"43F\", \"z3\", \"oR\", \"2Wo\", \"0bL\", \"4Yb\", \"6lA\", \"4A3\", \"4TR\", \"8fd\", \"LN\", \n\"bb\", \"0AP\", \"cPl\", \"aeO\", \"547\", \"41w\", \"0NQ\", \"mc\", \"CO\", \"14T\", \"5Kr\", \"4N2\", \"77i\", \"4Vc\", \"0mM\", \"2Xn\", \"22J\", \"u2\", \"4xO\", \"6Ml\", \n\"2JF\", \"I6\", \"4DK\", \"6qh\", \"7Oe\", \"4jg\", \"0QI\", \"6s\", \"1Y9\", \"1LX\", \"4gW\", \"4r6\", \"68P\", \"5YZ\", \"0rU\", \"Qg\", \"0h5\", \"1mu\", \"4Fz\", \"67Q\", \n\"7MT\", \"4hV\", \"0Sx\", \"4B\", \"9r\", \"1Ni\", \"4ef\", \"6PE\", \"7nH\", \"4KJ\", \"F7\", \"SV\", \"X5\", \"MT\", \"74B\", \"4UH\", \"5kE\", \"6NG\", \"cx\", \"1Pk\", \n\"0Mz\", \"nH\", \"4c5\", \"4vT\", \"4Xx\", \"79r\", \"0v7\", \"0cV\", \"0lW\", \"Oe\", \"5R9\", \"4Wy\", \"4yU\", \"4l4\", \"aI\", \"1RZ\", \"0OK\", \"ly\", \"6AF\", \"40m\", \n\"4ZI\", \"6oj\", \"BU\", \"W4\", \"265\", \"5E\", \"488\", \"4iQ\", \"b9F\", \"66V\", \"0i2\", \"1lr\", \"G0\", \"RQ\", \"7oO\", \"4JM\", \"4da\", \"6QB\", \"8u\", \"1On\", \n\"0PN\", \"7t\", \"7Nb\", \"aa0\", \"4EL\", \"64g\", \"2KA\", \"H1\", \"07Z\", \"0f3\", \"69W\", \"b6G\", \"4fP\", \"479\", \"dRn\", \"294\", \"22W\", \"8Jd\", \"4xR\", \"4m3\", \n\"77t\", \"624\", \"0mP\", \"Nb\", \"CR\", \"V3\", \"5Ko\", \"6nm\", \"ajS\", \"41j\", \"0NL\", \"3kN\", \"20f\", \"0AM\", \"4zc\", \"aeR\", \"6al\", \"4TO\", \"Y2\", \"LS\", \n\"Ac\", \"0bQ\", \"bcL\", \"78u\", \"4b2\", \"4wS\", \"8Ee\", \"oO\", \"7nU\", \"4KW\", \"04q\", \"SK\", \"9o\", \"1Nt\", \"51R\", \"6PX\", \"7MI\", \"4hK\", \"e6\", \"pW\", \n\"2Hj\", \"09A\", \"4Fg\", \"67L\", \"68M\", \"4If\", \"0rH\", \"Qz\", \"2iG\", \"j7\", \"4gJ\", \"6Ri\", \"7Ox\", \"4jz\", \"0QT\", \"6n\", \"1z8\", \"1oY\", \"4DV\", \"4Q7\", \n\"4ZT\", \"4O5\", \"BH\", \"0az\", \"0OV\", \"ld\", \"550\", \"40p\", \"4yH\", \"6Lk\", \"aT\", \"t5\", \"0lJ\", \"Ox\", \"6bG\", \"4Wd\", \"4Xe\", \"6mF\", \"2Vh\", \"0cK\", \n\"0Mg\", \"nU\", \"6Cj\", \"42A\", \"5kX\", \"6NZ\", \"ce\", \"1Pv\", \"2N9\", \"MI\", \"7pW\", \"4UU\", \"4Gy\", \"5B9\", \"0i6\", \"1lv\", \"1BZ\", \"5A\", \"7LW\", \"4iU\", \n\"4de\", \"6QF\", \"8q\", \"1Oj\", \"G4\", \"RU\", \"7oK\", \"4JI\", \"4EH\", \"64c\", \"2KE\", \"H5\", \"0PJ\", \"7p\", \"7Nf\", \"4kd\", \"4fT\", \"4s5\", \"2hY\", \"290\", \n\"0sV\", \"Pd\", \"5M8\", \"4Hx\", \"6cY\", \"4Vz\", \"0mT\", \"Nf\", \"1F8\", \"0Cx\", \"4xV\", \"4m7\", \"7Pd\", \"41n\", \"0NH\", \"mz\", \"CV\", \"V7\", \"5Kk\", \"6ni\", \n\"6ah\", \"4TK\", \"Y6\", \"LW\", \"20b\", \"0AI\", \"4zg\", \"6OD\", \"4b6\", \"4wW\", \"0Ly\", \"oK\", \"Ag\", \"0bU\", \"5IZ\", \"6lX\", \"9k\", \"1Np\", \"51V\", \"azN\", \n\"7nQ\", \"4KS\", \"04u\", \"SO\", \"2Hn\", \"09E\", \"4Fc\", \"67H\", \"7MM\", \"4hO\", \"e2\", \"pS\", \"2iC\", \"j3\", \"4gN\", \"6Rm\", \"68I\", \"4Ib\", \"06D\", \"2Go\", \n\"d4d\", \"82l\", \"4DR\", \"4Q3\", \"a1G\", \"bPM\", \"0QP\", \"6j\", \"0OR\", \"0Z3\", \"554\", \"40t\", \"4ZP\", \"4O1\", \"BL\", \"15W\", \"0lN\", \"2Ym\", \"6bC\", \"5GA\", \n\"4yL\", \"6Lo\", \"aP\", \"09\", \"0Mc\", \"nQ\", \"6Cn\", \"42E\", \"4Xa\", \"6mB\", \"2Vl\", \"0cO\", \"8gg\", \"MM\", \"7pS\", \"4UQ\", \"bAN\", \"adL\", \"ca\", \"1Pr\", \n\"G8\", \"RY\", \"7oG\", \"4JE\", \"4di\", \"6QJ\", \"2jd\", \"1Of\", \"0Rw\", \"5M\", \"480\", \"4iY\", \"4Gu\", \"5B5\", \"2Ix\", \"08S\", \"07R\", \"Ph\", \"5M4\", \"4Ht\", \n\"4fX\", \"471\", \"1X6\", \"1MW\", \"0PF\", \"st\", \"7Nj\", \"4kh\", \"4ED\", \"64o\", \"2KI\", \"H9\", \"CZ\", \"14A\", \"5Kg\", \"6ne\", \"7Ph\", \"41b\", \"0ND\", \"mv\", \n\"1F4\", \"0Ct\", \"4xZ\", \"6My\", \"5S6\", \"4Vv\", \"0mX\", \"Nj\", \"Ak\", \"0bY\", \"4Yw\", \"6lT\", \"6Bx\", \"43S\", \"0Lu\", \"oG\", \"bw\", \"0AE\", \"4zk\", \"6OH\", \n\"6ad\", \"4TG\", \"0oi\", \"2ZJ\", \"7MA\", \"4hC\", \"0Sm\", \"4W\", \"2Hb\", \"09I\", \"4Fo\", \"67D\", \"aTn\", \"b5d\", \"04y\", \"SC\", \"9g\", \"8WE\", \"4es\", \"6PP\", \n\"5o2\", \"4jr\", \"8XD\", \"6f\", \"1z0\", \"1oQ\", \"705\", \"65u\", \"68E\", \"4In\", \"06H\", \"Qr\", \"2iO\", \"1LM\", \"4gB\", \"6Ra\", \"5ia\", \"6Lc\", \"23E\", \"05\", \n\"0lB\", \"Op\", \"6bO\", \"4Wl\", \"c4F\", \"aEm\", \"1d2\", \"0ar\", \"8FF\", \"ll\", \"558\", \"40x\", \"5kP\", \"6NR\", \"cm\", \"344\", \"0ns\", \"MA\", \"74W\", \"bon\", \n\"4Xm\", \"6mN\", \"3FA\", \"0cC\", \"0Mo\", \"2xL\", \"6Cb\", \"42I\", \"4dm\", \"6QN\", \"8y\", \"1Ob\", \"05g\", \"2DL\", \"7oC\", \"4JA\", \"4Gq\", \"5B1\", \"d7G\", \"08W\", \n\"0Rs\", \"5I\", \"484\", \"bSn\", \"52u\", \"475\", \"1X2\", \"1MS\", \"07V\", \"Pl\", \"5M0\", \"4Hp\", \"5Ua\", \"64k\", \"2KM\", \"1nO\", \"0PB\", \"7x\", \"7Nn\", \"4kl\", \n\"7Pl\", \"41f\", \"8GX\", \"mr\", \"2UO\", \"14E\", \"5Kc\", \"6na\", \"5S2\", \"4Vr\", \"19u\", \"Nn\", \"1F0\", \"0Cp\", \"bBm\", \"ago\", \"ahn\", \"43W\", \"0Lq\", \"oC\", \n\"Ao\", \"16t\", \"4Ys\", \"6lP\", \"75I\", \"4TC\", \"0om\", \"2ZN\", \"bs\", \"0AA\", \"4zo\", \"6OL\", \"2Hf\", \"09M\", \"4Fk\", \"6sH\", \"7ME\", \"4hG\", \"0Si\", \"4S\", \n\"9c\", \"1Nx\", \"4ew\", \"6PT\", \"7nY\", \"bqh\", \"0pu\", \"SG\", \"1z4\", \"1oU\", \"4DZ\", \"65q\", \"5o6\", \"4jv\", \"0QX\", \"6b\", \"2iK\", \"1LI\", \"4gF\", \"6Re\", \n\"68A\", \"4Ij\", \"06L\", \"Qv\", \"0lF\", \"Ot\", \"6bK\", \"4Wh\", \"4yD\", \"6Lg\", \"aX\", \"01\", \"0OZ\", \"lh\", \"5q4\", \"4tt\", \"4ZX\", \"4O9\", \"BD\", \"0av\", \n\"0nw\", \"ME\", \"74S\", \"4UY\", \"5kT\", \"6NV\", \"ci\", \"1Pz\", \"0Mk\", \"nY\", \"6Cf\", \"42M\", \"4Xi\", \"6mJ\", \"2Vd\", \"0cG\", \"bL\", \"8Hf\", \"4zP\", \"4o1\", \n\"75v\", \"606\", \"0oR\", \"0z3\", \"AP\", \"T1\", \"4YL\", \"6lo\", \"6BC\", \"43h\", \"0LN\", \"2ym\", \"22d\", \"0CO\", \"4xa\", \"6MB\", \"6cn\", \"4VM\", \"0mc\", \"NQ\", \n\"Ca\", \"14z\", \"baN\", \"aDL\", \"7PS\", \"41Y\", \"8Gg\", \"mM\", \"247\", \"7G\", \"7NQ\", \"4kS\", \"com\", \"64T\", \"0k0\", \"1np\", \"E2\", \"PS\", \"69d\", \"4HO\", \n\"4fc\", \"7Ca\", \"2hn\", \"1Ml\", \"0RL\", \"5v\", \"avS\", \"4ib\", \"4GN\", \"66e\", \"2IC\", \"J3\", \"05X\", \"Rb\", \"aUO\", \"b4E\", \"4dR\", \"4q3\", \"8F\", \"8Vd\", \n\"4XV\", \"4M7\", \"1f8\", \"0cx\", \"0MT\", \"nf\", \"572\", \"42r\", \"5kk\", \"6Ni\", \"cV\", \"v7\", \"0nH\", \"Mz\", \"74l\", \"4Uf\", \"4Zg\", \"6oD\", \"2Tj\", \"0aI\", \n\"y6\", \"lW\", \"6Ah\", \"40C\", \"5iZ\", \"583\", \"ag\", \"0BU\", \"0ly\", \"OK\", \"4B6\", \"4WW\", \"7lW\", \"4IU\", \"06s\", \"QI\", \"0I6\", \"1Lv\", \"4gy\", \"5b9\", \n\"7OK\", \"4jI\", \"g4\", \"rU\", \"2Jh\", \"1oj\", \"4De\", \"65N\", \"7nf\", \"4Kd\", \"04B\", \"Sx\", \"2kE\", \"h5\", \"4eH\", \"6Pk\", \"5m8\", \"4hx\", \"0SV\", \"4l\", \n\"2HY\", \"09r\", \"4FT\", \"4S5\", \"5Q8\", \"4Tx\", \"0oV\", \"Ld\", \"bH\", \"0Az\", \"4zT\", \"4o5\", \"6BG\", \"43l\", \"0LJ\", \"ox\", \"AT\", \"T5\", \"4YH\", \"6lk\", \n\"6cj\", \"4VI\", \"0mg\", \"NU\", \"2vh\", \"0CK\", \"4xe\", \"6MF\", \"7PW\", \"4uU\", \"2n9\", \"mI\", \"Ce\", \"1pv\", \"5KX\", \"6nZ\", \"5UZ\", \"64P\", \"0k4\", \"1nt\", \n\"0Py\", \"7C\", \"7NU\", \"4kW\", \"4fg\", \"6SD\", \"2hj\", \"1Mh\", \"E6\", \"PW\", \"7mI\", \"4HK\", \"4GJ\", \"66a\", \"2IG\", \"J7\", \"0RH\", \"5r\", \"7Ld\", \"4if\", \n\"4dV\", \"4q7\", \"8B\", \"1OY\", \"0qT\", \"Rf\", \"7ox\", \"4Jz\", \"0MP\", \"nb\", \"576\", \"42v\", \"4XR\", \"4M3\", \"dll\", \"17U\", \"0nL\", \"3KN\", \"74h\", \"4Ub\", \n\"5ko\", \"6Nm\", \"cR\", \"v3\", \"y2\", \"lS\", \"6Al\", \"40G\", \"4Zc\", \"aER\", \"2Tn\", \"0aM\", \"18T\", \"OO\", \"4B2\", \"4WS\", \"bCL\", \"587\", \"ac\", \"0BQ\", \n\"0I2\", \"1Lr\", \"53T\", \"axL\", \"68z\", \"4IQ\", \"06w\", \"QM\", \"2Jl\", \"1on\", \"4Da\", \"65J\", \"7OO\", \"4jM\", \"g0\", \"6Y\", \"9X\", \"h1\", \"4eL\", \"6Po\", \n\"7nb\", \"aA0\", \"04F\", \"2Em\", \"d6f\", \"09v\", \"4FP\", \"4S1\", \"a3E\", \"bRO\", \"0SR\", \"4h\", \"AX\", \"T9\", \"4YD\", \"6lg\", \"6BK\", \"4wh\", \"0LF\", \"ot\", \n\"bD\", \"0Av\", \"4zX\", \"4o9\", \"5Q4\", \"4Tt\", \"0oZ\", \"Lh\", \"Ci\", \"14r\", \"5KT\", \"6nV\", \"ajh\", \"41Q\", \"0Nw\", \"mE\", \"22l\", \"0CG\", \"4xi\", \"6MJ\", \n\"6cf\", \"4VE\", \"0mk\", \"NY\", \"07a\", \"2FJ\", \"69l\", \"4HG\", \"4fk\", \"6SH\", \"2hf\", \"1Md\", \"0Pu\", \"7O\", \"7NY\", \"bQh\", \"4Ew\", \"6pT\", \"0k8\", \"1nx\", \n\"05P\", \"Rj\", \"5O6\", \"4Jv\", \"4dZ\", \"453\", \"8N\", \"1OU\", \"0RD\", \"qv\", \"7Lh\", \"4ij\", \"4GF\", \"66m\", \"2IK\", \"1lI\", \"5kc\", \"6Na\", \"21G\", \"27\", \n\"8gX\", \"Mr\", \"74d\", \"4Un\", \"bbm\", \"79T\", \"1f0\", \"0cp\", \"397\", \"nn\", \"5s2\", \"42z\", \"4ys\", \"6LP\", \"ao\", \"366\", \"0lq\", \"OC\", \"76U\", \"bml\", \n\"4Zo\", \"6oL\", \"Bs\", \"0aA\", \"0Om\", \"2zN\", \"7QA\", \"40K\", \"7OC\", \"4jA\", \"0Qo\", \"6U\", \"3ZA\", \"1ob\", \"4Dm\", \"65F\", \"68v\", \"b7f\", \"0rs\", \"QA\", \n\"dSO\", \"8UG\", \"4gq\", \"5b1\", \"5m0\", \"4hp\", \"8ZF\", \"4d\", \"1x2\", \"09z\", \"727\", \"67w\", \"7nn\", \"4Kl\", \"04J\", \"Sp\", \"9T\", \"1NO\", \"51i\", \"6Pc\", \n\"6BO\", \"43d\", \"0LB\", \"op\", \"2WM\", \"0bn\", \"5Ia\", \"6lc\", \"5Q0\", \"4Tp\", \"8fF\", \"Ll\", \"1D2\", \"0Ar\", \"cPN\", \"aem\", \"ajl\", \"41U\", \"0Ns\", \"mA\", \n\"Cm\", \"14v\", \"5KP\", \"6nR\", \"6cb\", \"4VA\", \"0mo\", \"2XL\", \"22h\", \"0CC\", \"4xm\", \"6MN\", \"4fo\", \"6SL\", \"2hb\", \"8TY\", \"07e\", \"2FN\", \"69h\", \"4HC\", \n\"4Es\", \"64X\", \"d5E\", \"83M\", \"0Pq\", \"7K\", \"a0f\", \"bQl\", \"50w\", \"457\", \"8J\", \"1OQ\", \"05T\", \"Rn\", \"5O2\", \"4Jr\", \"4GB\", \"66i\", \"2IO\", \"08d\", \n\"1Ba\", \"5z\", \"7Ll\", \"4in\", \"0nD\", \"Mv\", \"7ph\", \"4Uj\", \"5kg\", \"6Ne\", \"cZ\", \"23\", \"0MX\", \"nj\", \"5s6\", \"4vv\", \"4XZ\", \"6my\", \"1f4\", \"0ct\", \n\"0lu\", \"OG\", \"6bx\", \"5Gz\", \"4yw\", \"6LT\", \"ak\", \"0BY\", \"0Oi\", \"2zJ\", \"6Ad\", \"40O\", \"4Zk\", \"6oH\", \"Bw\", \"0aE\", \"2Jd\", \"1of\", \"4Di\", \"65B\", \n\"7OG\", \"4jE\", \"g8\", \"6Q\", \"2ix\", \"1Lz\", \"4gu\", \"5b5\", \"68r\", \"4IY\", \"0rw\", \"QE\", \"1x6\", \"1mW\", \"4FX\", \"4S9\", \"5m4\", \"4ht\", \"0SZ\", \"ph\", \n\"9P\", \"h9\", \"4eD\", \"6Pg\", \"7nj\", \"4Kh\", \"04N\", \"St\", \"22u\", \"375\", \"4xp\", \"598\", \"77V\", \"blo\", \"0mr\", \"1h2\", \"Cp\", \"14k\", \"5KM\", \"6nO\", \n\"7PB\", \"41H\", \"0Nn\", \"3kl\", \"20D\", \"34\", \"4zA\", \"6Ob\", \"6aN\", \"4Tm\", \"0oC\", \"Lq\", \"AA\", \"0bs\", \"bcn\", \"78W\", \"569\", \"43y\", \"384\", \"om\", \n\"9Kd\", \"5g\", \"5l3\", \"4is\", \"734\", \"66t\", \"1y1\", \"08y\", \"05I\", \"Rs\", \"7om\", \"4Jo\", \"4dC\", \"7AA\", \"8W\", \"1OL\", \"0Pl\", \"7V\", \"ats\", \"4kB\", \n\"4En\", \"64E\", \"2Kc\", \"1na\", \"07x\", \"PB\", \"69u\", \"b6e\", \"4fr\", \"5c2\", \"dRL\", \"8TD\", \"4Zv\", \"6oU\", \"Bj\", \"0aX\", \"0Ot\", \"lF\", \"6Ay\", \"40R\", \n\"4yj\", \"6LI\", \"av\", \"0BD\", \"0lh\", \"OZ\", \"6be\", \"4WF\", \"4XG\", \"6md\", \"2VJ\", \"0ci\", \"0ME\", \"nw\", \"6CH\", \"42c\", \"5kz\", \"6Nx\", \"cG\", \"1PT\", \n\"0nY\", \"Mk\", \"5P7\", \"4Uw\", \"5N5\", \"4Ku\", \"04S\", \"Si\", \"9M\", \"1NV\", \"4eY\", \"440\", \"7Mk\", \"4hi\", \"0SG\", \"pu\", \"2HH\", \"K8\", \"4FE\", \"67n\", \n\"68o\", \"4ID\", \"1\", \"QX\", \"2ie\", \"1Lg\", \"4gh\", \"6RK\", \"7OZ\", \"4jX\", \"0Qv\", \"6L\", \"2Jy\", \"3O9\", \"4Dt\", \"5A4\", \"4C9\", \"4VX\", \"0mv\", \"ND\", \n\"22q\", \"0CZ\", \"4xt\", \"6MW\", \"7PF\", \"41L\", \"x9\", \"mX\", \"Ct\", \"14o\", \"5KI\", \"6nK\", \"6aJ\", \"4Ti\", \"0oG\", \"Lu\", \"bY\", \"30\", \"4zE\", \"6Of\", \n\"5r5\", \"4wu\", \"380\", \"oi\", \"AE\", \"0bw\", \"4YY\", \"4L8\", \"5Wz\", \"66p\", \"1y5\", \"1lT\", \"0RY\", \"5c\", \"5l7\", \"4iw\", \"4dG\", \"6Qd\", \"8S\", \"1OH\", \n\"05M\", \"Rw\", \"7oi\", \"4Jk\", \"4Ej\", \"64A\", \"2Kg\", \"1ne\", \"0Ph\", \"7R\", \"7ND\", \"4kF\", \"4fv\", \"5c6\", \"0H9\", \"1My\", \"0st\", \"PF\", \"69q\", \"4HZ\", \n\"0Op\", \"lB\", \"ako\", \"40V\", \"4Zr\", \"6oQ\", \"Bn\", \"15u\", \"0ll\", \"2YO\", \"6ba\", \"4WB\", \"4yn\", \"6LM\", \"ar\", \"1Ra\", \"0MA\", \"ns\", \"6CL\", \"42g\", \n\"4XC\", \"79I\", \"2VN\", \"0cm\", \"8gE\", \"Mo\", \"5P3\", \"4Us\", \"bAl\", \"adn\", \"cC\", \"1PP\", \"9I\", \"1NR\", \"51t\", \"444\", \"5N1\", \"4Kq\", \"04W\", \"Sm\", \n\"2HL\", \"09g\", \"4FA\", \"67j\", \"7Mo\", \"4hm\", \"0SC\", \"4y\", \"2ia\", \"1Lc\", \"4gl\", \"6RO\", \"68k\", \"5Ya\", \"5\", \"2GM\", \"d4F\", \"82N\", \"4Dp\", \"5A0\", \n\"a1e\", \"bPo\", \"0Qr\", \"6H\", \"Cx\", \"14c\", \"5KE\", \"6nG\", \"7PJ\", \"4uH\", \"x5\", \"mT\", \"0V7\", \"0CV\", \"4xx\", \"590\", \"4C5\", \"4VT\", \"0mz\", \"NH\", \n\"AI\", \"16R\", \"4YU\", \"4L4\", \"561\", \"43q\", \"0LW\", \"oe\", \"bU\", \"w4\", \"4zI\", \"6Oj\", \"6aF\", \"4Te\", \"0oK\", \"Ly\", \"05A\", \"2Dj\", \"7oe\", \"4Jg\", \n\"4dK\", \"6Qh\", \"2jF\", \"i6\", \"0RU\", \"5o\", \"7Ly\", \"5yZ\", \"4GW\", \"4R6\", \"1y9\", \"08q\", \"07p\", \"PJ\", \"7mT\", \"4HV\", \"4fz\", \"6SY\", \"0H5\", \"1Mu\", \n\"f7\", \"sV\", \"7NH\", \"4kJ\", \"4Ef\", \"64M\", \"2Kk\", \"1ni\", \"4yb\", \"6LA\", \"23g\", \"0BL\", \"Z3\", \"OR\", \"6bm\", \"4WN\", \"c4d\", \"aEO\", \"Bb\", \"0aP\", \n\"8Fd\", \"lN\", \"4a3\", \"40Z\", \"5kr\", \"4n2\", \"cO\", \"8Ie\", \"0nQ\", \"Mc\", \"74u\", \"615\", \"4XO\", \"6ml\", \"2VB\", \"U2\", \"0MM\", \"2xn\", \"7Sa\", \"42k\", \n\"7Mc\", \"4ha\", \"0SO\", \"4u\", \"3Xa\", \"K0\", \"4FM\", \"67f\", \"aTL\", \"b5F\", \"0pS\", \"Sa\", \"9E\", \"8Wg\", \"4eQ\", \"448\", \"7OR\", \"4jP\", \"254\", \"6D\", \n\"0j3\", \"1os\", \"cnn\", \"65W\", \"68g\", \"4IL\", \"9\", \"QP\", \"2im\", \"1Lo\", \"53I\", \"6RC\", \"7PN\", \"41D\", \"x1\", \"mP\", \"2Um\", \"14g\", \"5KA\", \"6nC\", \n\"4C1\", \"4VP\", \"19W\", \"NL\", \"0V3\", \"0CR\", \"bBO\", \"594\", \"565\", \"43u\", \"0LS\", \"oa\", \"AM\", \"16V\", \"4YQ\", \"4L0\", \"6aB\", \"4Ta\", \"0oO\", \"2Zl\", \n\"bQ\", \"38\", \"4zM\", \"6On\", \"4dO\", \"6Ql\", \"2jB\", \"i2\", \"05E\", \"2Dn\", \"7oa\", \"4Jc\", \"4GS\", \"4R2\", \"d7e\", \"08u\", \"0RQ\", \"5k\", \"a2F\", \"bSL\", \n\"52W\", \"ayO\", \"0H1\", \"1Mq\", \"07t\", \"PN\", \"69y\", \"4HR\", \"4Eb\", \"64I\", \"2Ko\", \"1nm\", \"f3\", \"7Z\", \"7NL\", \"4kN\", \"Z7\", \"OV\", \"6bi\", \"4WJ\", \n\"4yf\", \"6LE\", \"az\", \"0BH\", \"0Ox\", \"lJ\", \"4a7\", \"4tV\", \"4Zz\", \"6oY\", \"Bf\", \"0aT\", \"0nU\", \"Mg\", \"74q\", \"5EZ\", \"5kv\", \"4n6\", \"cK\", \"1PX\", \n\"0MI\", \"2xj\", \"6CD\", \"42o\", \"4XK\", \"6mh\", \"2VF\", \"U6\", \"2HD\", \"K4\", \"4FI\", \"67b\", \"7Mg\", \"4he\", \"0SK\", \"4q\", \"9A\", \"1NZ\", \"4eU\", \"4p4\", \n\"5N9\", \"4Ky\", \"0pW\", \"Se\", \"0j7\", \"1ow\", \"4Dx\", \"5A8\", \"7OV\", \"4jT\", \"0Qz\", \"rH\", \"2ii\", \"1Lk\", \"4gd\", \"6RG\", \"68c\", \"4IH\", \"D5\", \"QT\", \n\"5Ls\", \"4I3\", \"F\", \"13U\", \"0IP\", \"jb\", \"536\", \"46v\", \"5oo\", \"6Jm\", \"gR\", \"r3\", \"0jL\", \"3ON\", \"6dA\", \"4Qb\", \"5NB\", \"aAR\", \"2Pn\", \"0eM\", \n\"0Ka\", \"hS\", \"6El\", \"44G\", \"49w\", \"abN\", \"ec\", \"0FQ\", \"8ae\", \"KO\", \"4F2\", \"4SS\", \"4X0\", \"4MQ\", \"02w\", \"UM\", \"0M2\", \"0XS\", \"57T\", \"a8D\", \n\"7KO\", \"4nM\", \"c0\", \"2Y\", \"2Nl\", \"1kn\", \"aJ1\", \"61J\", \"6zC\", \"aE0\", \"00F\", \"2Am\", \"yP\", \"l1\", \"4aL\", \"6To\", \"a7E\", \"58U\", \"0WR\", \"0h\", \n\"ZL\", \"84n\", \"4BP\", \"4W1\", \"fH\", \"0Ez\", \"5nu\", \"4k5\", \"5U8\", \"4Px\", \"0kV\", \"Hd\", \"ET\", \"P5\", \"5Mi\", \"6hk\", \"6FG\", \"47l\", \"0HJ\", \"kx\", \n\"dy\", \"0GK\", \"48m\", \"6IF\", \"6gj\", \"4RI\", \"0ig\", \"JU\", \"Ge\", \"0dW\", \"5OX\", \"5Z9\", \"4d4\", \"4qU\", \"1ZZ\", \"iI\", \"0Ty\", \"3C\", \"4z6\", \"4oW\", \n\"5QZ\", \"60P\", \"Yg\", \"0zU\", \"A6\", \"TW\", \"6yh\", \"4LK\", \"4bg\", \"6WD\", \"2lj\", \"0YI\", \"0VH\", \"1r\", \"6XE\", \"4mf\", \"4CJ\", \"62a\", \"2MG\", \"N7\", \n\"0uT\", \"Vf\", \"7kx\", \"4Nz\", \"5pw\", \"4u7\", \"xJ\", \"1KY\", \"0IT\", \"jf\", \"532\", \"46r\", \"5Lw\", \"4I7\", \"B\", \"0gx\", \"0jH\", \"Iz\", \"6dE\", \"4Qf\", \n\"5ok\", \"6Ji\", \"gV\", \"r7\", \"0Ke\", \"hW\", \"6Eh\", \"44C\", \"5NF\", \"6kD\", \"2Pj\", \"0eI\", \"0hy\", \"KK\", \"4F6\", \"4SW\", \"49s\", \"6HX\", \"eg\", \"0FU\", \n\"0M6\", \"0XW\", \"4cy\", \"5f9\", \"4X4\", \"4MU\", \"02s\", \"UI\", \"Xy\", \"1kj\", \"5PD\", \"61N\", \"7KK\", \"4nI\", \"c4\", \"vU\", \"yT\", \"l5\", \"4aH\", \"6Tk\", \n\"6zG\", \"4Od\", \"00B\", \"Wx\", \"ZH\", \"0yz\", \"4BT\", \"4W5\", \"5i8\", \"4lx\", \"0WV\", \"0l\", \"71v\", \"646\", \"0kR\", \"3NP\", \"fL\", \"8Lf\", \"5nq\", \"4k1\", \n\"6FC\", \"47h\", \"0HN\", \"29e\", \"EP\", \"P1\", \"5Mm\", \"6ho\", \"6gn\", \"4RM\", \"0ic\", \"JQ\", \"26d\", \"0GO\", \"48i\", \"6IB\", \"4d0\", \"45Y\", \"8Cg\", \"iM\", \n\"Ga\", \"0dS\", \"beN\", \"hYu\", \"ckm\", \"60T\", \"Yc\", \"0zQ\", \"207\", \"3G\", \"4z2\", \"4oS\", \"4bc\", \"7Ga\", \"2ln\", \"0YM\", \"A2\", \"TS\", \"6yl\", \"4LO\", \n\"4CN\", \"62e\", \"2MC\", \"N3\", \"0VL\", \"1v\", \"6XA\", \"4mb\", \"5ps\", \"4u3\", \"xN\", \"8Rd\", \"01X\", \"Vb\", \"aQO\", \"b0E\", \"5og\", \"6Je\", \"gZ\", \"63\", \n\"0jD\", \"Iv\", \"6dI\", \"4Qj\", \"7l9\", \"6iy\", \"N\", \"0gt\", \"0IX\", \"jj\", \"5w6\", \"4rv\", \"5mV\", \"5x7\", \"ek\", \"0FY\", \"0hu\", \"KG\", \"6fx\", \"5Cz\", \n\"5NJ\", \"6kH\", \"Fw\", \"0eE\", \"92\", \"3nk\", \"6Ed\", \"44O\", \"7KG\", \"4nE\", \"c8\", \"2Q\", \"Xu\", \"1kf\", \"5PH\", \"61B\", \"4X8\", \"4MY\", \"0vw\", \"UE\", \n\"2mx\", \"1Hz\", \"4cu\", \"5f5\", \"5i4\", \"4lt\", \"0WZ\", \"th\", \"ZD\", \"0yv\", \"4BX\", \"4W9\", \"6zK\", \"4Oh\", \"00N\", \"Wt\", \"yX\", \"l9\", \"4aD\", \"6Tg\", \n\"2SM\", \"0fn\", \"5Ma\", \"6hc\", \"6FO\", \"47d\", \"0HB\", \"kp\", \"24Y\", \"0Er\", \"bDo\", \"aam\", \"5U0\", \"4Pp\", \"8bF\", \"Hl\", \"Gm\", \"10v\", \"5OP\", \"5Z1\", \n\"anl\", \"45U\", \"0Js\", \"iA\", \"dq\", \"0GC\", \"48e\", \"6IN\", \"6gb\", \"4RA\", \"0io\", \"3Lm\", \"03e\", \"2BN\", \"7iA\", \"4LC\", \"4bo\", \"6WL\", \"zs\", \"0YA\", \n\"0Tq\", \"3K\", \"a4f\", \"bUl\", \"4As\", \"5D3\", \"Yo\", \"87M\", \"01T\", \"Vn\", \"5K2\", \"4Nr\", \"54w\", \"417\", \"xB\", \"1KQ\", \"1Fa\", \"1z\", \"6XM\", \"4mn\", \n\"4CB\", \"62i\", \"2MO\", \"0xl\", \"1za\", \"Ir\", \"6dM\", \"4Qn\", \"5oc\", \"6Ja\", \"25G\", \"67\", \"9Pe\", \"jn\", \"5w2\", \"46z\", \"bfm\", \"aCo\", \"J\", \"0gp\", \n\"0hq\", \"KC\", \"72U\", \"bil\", \"5mR\", \"5x3\", \"eo\", \"326\", \"96\", \"3no\", \"7UA\", \"44K\", \"5NN\", \"6kL\", \"Fs\", \"0eA\", \"Xq\", \"1kb\", \"5PL\", \"61F\", \n\"7KC\", \"4nA\", \"0Uo\", \"2U\", \"39U\", \"8QG\", \"4cq\", \"5f1\", \"aRl\", \"796\", \"0vs\", \"UA\", \"2LQ\", \"0yr\", \"767\", \"63w\", \"5i0\", \"4lp\", \"9Ng\", \"0d\", \n\"2oM\", \"0Zn\", \"55i\", \"6Tc\", \"6zO\", \"4Ol\", \"00J\", \"Wp\", \"6FK\", \"4sh\", \"0HF\", \"kt\", \"EX\", \"P9\", \"5Me\", \"6hg\", \"5U4\", \"4Pt\", \"0kZ\", \"Hh\", \n\"fD\", \"0Ev\", \"5ny\", \"4k9\", \"4d8\", \"45Q\", \"0Jw\", \"iE\", \"Gi\", \"10r\", \"5OT\", \"5Z5\", \"6gf\", \"4RE\", \"0ik\", \"JY\", \"du\", \"0GG\", \"48a\", \"6IJ\", \n\"4bk\", \"6WH\", \"zw\", \"0YE\", \"03a\", \"2BJ\", \"6yd\", \"4LG\", \"4Aw\", \"5D7\", \"Yk\", \"0zY\", \"0Tu\", \"3O\", \"6Zx\", \"bUh\", \"54s\", \"413\", \"xF\", \"1KU\", \n\"01P\", \"Vj\", \"5K6\", \"4Nv\", \"4CF\", \"62m\", \"2MK\", \"0xh\", \"0VD\", \"uv\", \"6XI\", \"4mj\", \"5NS\", \"6kQ\", \"Fn\", \"11u\", \"0Kp\", \"hB\", \"aoo\", \"44V\", \n\"49f\", \"6HM\", \"er\", \"1Va\", \"0hl\", \"3Mn\", \"6fa\", \"4SB\", \"5Lb\", \"7yA\", \"W\", \"0gm\", \"0IA\", \"js\", \"6GL\", \"46g\", \"bEl\", \"hyW\", \"gC\", \"0Dq\", \n\"8cE\", \"Io\", \"5T3\", \"4Qs\", \"5J1\", \"4Oq\", \"00W\", \"Wm\", \"yA\", \"0Zs\", \"55t\", \"404\", \"6YN\", \"4lm\", \"0WC\", \"0y\", \"2LL\", \"0yo\", \"4BA\", \"63j\", \n\"6xc\", \"bws\", \"02f\", \"2CM\", \"2ma\", \"0XB\", \"4cl\", \"6VO\", \"a5e\", \"bTo\", \"0Ur\", \"2H\", \"Xl\", \"86N\", \"5PQ\", \"5E0\", \"dh\", \"0GZ\", \"5lU\", \"5y4\", \n\"4G9\", \"4RX\", \"0iv\", \"JD\", \"Gt\", \"0dF\", \"5OI\", \"6jK\", \"6Dg\", \"45L\", \"81\", \"iX\", \"fY\", \"70\", \"5nd\", \"6Kf\", \"6eJ\", \"4Pi\", \"0kG\", \"Hu\", \n\"EE\", \"0fw\", \"5Mx\", \"4H8\", \"5v5\", \"4su\", \"1Xz\", \"ki\", \"0VY\", \"1c\", \"5h7\", \"4mw\", \"5Sz\", \"62p\", \"2MV\", \"0xu\", \"01M\", \"Vw\", \"7ki\", \"4Nk\", \n\"54n\", \"6Ud\", \"2nJ\", \"1KH\", \"0Th\", \"3R\", \"6Ze\", \"4oF\", \"4Aj\", \"60A\", \"Yv\", \"0zD\", \"0wt\", \"TF\", \"6yy\", \"4LZ\", \"4bv\", \"5g6\", \"zj\", \"0YX\", \n\"0Kt\", \"hF\", \"6Ey\", \"44R\", \"5NW\", \"6kU\", \"Fj\", \"0eX\", \"0hh\", \"KZ\", \"6fe\", \"4SF\", \"49b\", \"6HI\", \"ev\", \"0FD\", \"0IE\", \"jw\", \"6GH\", \"46c\", \n\"5Lf\", \"6id\", \"S\", \"0gi\", \"0jY\", \"Ik\", \"5T7\", \"4Qw\", \"5oz\", \"6Jx\", \"gG\", \"0Du\", \"yE\", \"0Zw\", \"4aY\", \"400\", \"5J5\", \"4Ou\", \"00S\", \"Wi\", \n\"ZY\", \"O8\", \"4BE\", \"63n\", \"6YJ\", \"4li\", \"0WG\", \"tu\", \"2me\", \"0XF\", \"4ch\", \"6VK\", \"6xg\", \"4MD\", \"02b\", \"UX\", \"Xh\", \"3K9\", \"5PU\", \"5E4\", \n\"7KZ\", \"4nX\", \"0Uv\", \"2L\", \"73V\", \"bho\", \"0ir\", \"1l2\", \"dl\", \"335\", \"48x\", \"5y0\", \"6Dc\", \"45H\", \"85\", \"3ol\", \"Gp\", \"0dB\", \"5OM\", \"6jO\", \n\"6eN\", \"4Pm\", \"0kC\", \"Hq\", \"24D\", \"74\", \"bDr\", \"6Kb\", \"529\", \"47y\", \"8AG\", \"km\", \"EA\", \"0fs\", \"bgn\", \"aBl\", \"774\", \"62t\", \"199\", \"0xq\", \n\"9Od\", \"1g\", \"5h3\", \"4ms\", \"54j\", \"7EA\", \"2nN\", \"1KL\", \"01I\", \"Vs\", \"7km\", \"4No\", \"4An\", \"60E\", \"Yr\", \"1ja\", \"0Tl\", \"3V\", \"6Za\", \"4oB\", \n\"4br\", \"5g2\", \"zn\", \"8PD\", \"03x\", \"TB\", \"aSo\", \"785\", \"49n\", \"6HE\", \"ez\", \"0FH\", \"0hd\", \"KV\", \"6fi\", \"4SJ\", \"bdI\", \"6kY\", \"Ff\", \"0eT\", \n\"0Kx\", \"hJ\", \"4e7\", \"4pV\", \"5ov\", \"4j6\", \"gK\", \"0Dy\", \"0jU\", \"Ig\", \"6dX\", \"5AZ\", \"5Lj\", \"6ih\", \"DW\", \"Q6\", \"0II\", \"28b\", \"6GD\", \"46o\", \n\"6YF\", \"4le\", \"0WK\", \"0q\", \"ZU\", \"O4\", \"4BI\", \"63b\", \"5J9\", \"4Oy\", \"0tW\", \"We\", \"yI\", \"1JZ\", \"4aU\", \"4t4\", \"7KV\", \"4nT\", \"0Uz\", \"vH\", \n\"Xd\", \"1kw\", \"5PY\", \"5E8\", \"6xk\", \"4MH\", \"02n\", \"UT\", \"2mi\", \"0XJ\", \"4cd\", \"6VG\", \"2Qm\", \"0dN\", \"5OA\", \"6jC\", \"6Do\", \"45D\", \"89\", \"iP\", \n\"0R3\", \"0GR\", \"48t\", \"acM\", \"4G1\", \"4RP\", \"94O\", \"JL\", \"EM\", \"12V\", \"5Mp\", \"4H0\", \"525\", \"47u\", \"0HS\", \"ka\", \"fQ\", \"78\", \"5nl\", \"6Kn\", \n\"6eB\", \"4Pa\", \"0kO\", \"3NM\", \"01E\", \"3PO\", \"7ka\", \"4Nc\", \"54f\", \"6Ul\", \"xS\", \"m2\", \"0VQ\", \"1k\", \"a6F\", \"59V\", \"4CS\", \"4V2\", \"195\", \"85m\", \n\"03t\", \"TN\", \"4Y3\", \"4LR\", \"56W\", \"a9G\", \"zb\", \"0YP\", \"b3\", \"3Z\", \"6Zm\", \"4oN\", \"4Ab\", \"60I\", \"2Oo\", \"0zL\", \"1xA\", \"KR\", \"6fm\", \"4SN\", \n\"49j\", \"6HA\", \"27g\", \"0FL\", \"8Bd\", \"hN\", \"4e3\", \"44Z\", \"bdM\", \"aAO\", \"Fb\", \"0eP\", \"0jQ\", \"Ic\", \"70u\", \"655\", \"5or\", \"4j2\", \"gO\", \"8Me\", \n\"0IM\", \"28f\", \"7Wa\", \"46k\", \"5Ln\", \"6il\", \"DS\", \"Q2\", \"ZQ\", \"O0\", \"4BM\", \"63f\", \"6YB\", \"4la\", \"0WO\", \"0u\", \"yM\", \"8Sg\", \"4aQ\", \"408\", \n\"aPL\", \"b1F\", \"0tS\", \"Wa\", \"0n3\", \"1ks\", \"bzO\", \"61W\", \"7KR\", \"4nP\", \"214\", \"2D\", \"2mm\", \"0XN\", \"57I\", \"6VC\", \"6xo\", \"4ML\", \"02j\", \"UP\", \n\"6Dk\", \"4qH\", \"0Jf\", \"iT\", \"Gx\", \"0dJ\", \"5OE\", \"6jG\", \"4G5\", \"4RT\", \"0iz\", \"JH\", \"dd\", \"0GV\", \"48p\", \"5y8\", \"521\", \"47q\", \"0HW\", \"ke\", \n\"EI\", \"12R\", \"5Mt\", \"4H4\", \"6eF\", \"4Pe\", \"0kK\", \"Hy\", \"fU\", \"s4\", \"5nh\", \"6Kj\", \"54b\", \"6Uh\", \"xW\", \"m6\", \"01A\", \"3PK\", \"7ke\", \"4Ng\", \n\"4CW\", \"4V6\", \"191\", \"0xy\", \"0VU\", \"1o\", \"6XX\", \"59R\", \"4bz\", \"6WY\", \"zf\", \"0YT\", \"03p\", \"TJ\", \"4Y7\", \"4LV\", \"4Af\", \"60M\", \"Yz\", \"0zH\", \n\"b7\", \"wV\", \"6Zi\", \"4oJ\", \"5H3\", \"4Ms\", \"02U\", \"Uo\", \"2mR\", \"0Xq\", \"57v\", \"426\", \"7Km\", \"4no\", \"0UA\", \"vs\", \"2NN\", \"1kL\", \"5Pb\", \"61h\", \n\"6za\", \"4OB\", \"00d\", \"2AO\", \"yr\", \"1Ja\", \"4an\", \"6TM\", \"a7g\", \"58w\", \"0Wp\", \"0J\", \"Zn\", \"84L\", \"4Br\", \"5G2\", \"5LQ\", \"5Y0\", \"d\", \"13w\", \n\"0Ir\", \"1L2\", \"amm\", \"46T\", \"5oM\", \"6JO\", \"gp\", \"0DB\", \"0jn\", \"3Ol\", \"6dc\", \"5Aa\", \"bdr\", \"6kb\", \"2PL\", \"0eo\", \"0KC\", \"hq\", \"6EN\", \"44e\", \n\"49U\", \"abl\", \"eA\", \"0Fs\", \"8aG\", \"Km\", \"5V1\", \"4Sq\", \"1Dz\", \"3a\", \"5j5\", \"4ou\", \"4AY\", \"4T8\", \"YE\", \"0zw\", \"03O\", \"Tu\", \"6yJ\", \"4Li\", \n\"4bE\", \"6Wf\", \"zY\", \"o8\", \"0Vj\", \"1P\", \"6Xg\", \"4mD\", \"4Ch\", \"62C\", \"2Me\", \"0xF\", \"0uv\", \"VD\", \"7kZ\", \"4NX\", \"5pU\", \"5e4\", \"xh\", \"3k9\", \n\"fj\", \"0EX\", \"5nW\", \"6KU\", \"6ey\", \"4PZ\", \"0kt\", \"HF\", \"Ev\", \"0fD\", \"5MK\", \"6hI\", \"6Fe\", \"47N\", \"0Hh\", \"kZ\", \"26B\", \"52\", \"48O\", \"6Id\", \n\"6gH\", \"4Rk\", \"0iE\", \"Jw\", \"GG\", \"0du\", \"5Oz\", \"6jx\", \"5t7\", \"4qw\", \"0JY\", \"ik\", \"2mV\", \"0Xu\", \"57r\", \"422\", \"5H7\", \"4Mw\", \"02Q\", \"Uk\", \n\"2NJ\", \"1kH\", \"5Pf\", \"61l\", \"7Ki\", \"4nk\", \"0UE\", \"vw\", \"yv\", \"0ZD\", \"4aj\", \"6TI\", \"6ze\", \"4OF\", \"0th\", \"WZ\", \"Zj\", \"0yX\", \"4Bv\", \"5G6\", \n\"6Yy\", \"4lZ\", \"0Wt\", \"0N\", \"0Iv\", \"jD\", \"4g9\", \"46P\", \"5LU\", \"5Y4\", \"Dh\", \"0gZ\", \"0jj\", \"IX\", \"6dg\", \"4QD\", \"5oI\", \"6JK\", \"gt\", \"0DF\", \n\"0KG\", \"hu\", \"6EJ\", \"44a\", \"5Nd\", \"6kf\", \"FY\", \"S8\", \"1xz\", \"Ki\", \"5V5\", \"4Su\", \"49Q\", \"4h8\", \"eE\", \"0Fw\", \"756\", \"60v\", \"YA\", \"0zs\", \n\"9Mf\", \"3e\", \"5j1\", \"4oq\", \"4bA\", \"6Wb\", \"2lL\", \"0Yo\", \"03K\", \"Tq\", \"6yN\", \"4Lm\", \"4Cl\", \"62G\", \"2Ma\", \"0xB\", \"0Vn\", \"1T\", \"6Xc\", \"59i\", \n\"54Y\", \"5e0\", \"xl\", \"8RF\", \"01z\", \"1p2\", \"aQm\", \"b0g\", \"71T\", \"bjm\", \"0kp\", \"HB\", \"fn\", \"317\", \"5nS\", \"6KQ\", \"6Fa\", \"47J\", \"0Hl\", \"29G\", \n\"Er\", \"12i\", \"5MO\", \"6hM\", \"6gL\", \"4Ro\", \"0iA\", \"Js\", \"26F\", \"56\", \"48K\", \"7YA\", \"5t3\", \"4qs\", \"8CE\", \"io\", \"GC\", \"0dq\", \"bel\", \"hYW\", \n\"7Ke\", \"4ng\", \"0UI\", \"2s\", \"XW\", \"M6\", \"5Pj\", \"6uh\", \"6xX\", \"6m9\", \"0vU\", \"Ug\", \"2mZ\", \"0Xy\", \"4cW\", \"4v6\", \"4y7\", \"4lV\", \"0Wx\", \"0B\", \n\"Zf\", \"0yT\", \"4Bz\", \"63Q\", \"6zi\", \"4OJ\", \"B7\", \"WV\", \"yz\", \"0ZH\", \"4af\", \"6TE\", \"5oE\", \"6JG\", \"gx\", \"0DJ\", \"0jf\", \"IT\", \"6dk\", \"4QH\", \n\"5LY\", \"5Y8\", \"l\", \"0gV\", \"0Iz\", \"jH\", \"4g5\", \"4rT\", \"5mt\", \"4h4\", \"eI\", \"1VZ\", \"0hW\", \"Ke\", \"5V9\", \"4Sy\", \"5Nh\", \"6kj\", \"FU\", \"S4\", \n\"0KK\", \"hy\", \"6EF\", \"44m\", \"03G\", \"2Bl\", \"6yB\", \"4La\", \"4bM\", \"6Wn\", \"zQ\", \"o0\", \"0TS\", \"3i\", \"a4D\", \"bUN\", \"4AQ\", \"4T0\", \"YM\", \"87o\", \n\"01v\", \"VL\", \"7kR\", \"4NP\", \"54U\", \"hft\", \"0N3\", \"1Ks\", \"0Vb\", \"1X\", \"6Xo\", \"4mL\", \"5SA\", \"62K\", \"2Mm\", \"0xN\", \"2So\", \"0fL\", \"5MC\", \"6hA\", \n\"6Fm\", \"47F\", \"1XA\", \"kR\", \"fb\", \"0EP\", \"bDM\", \"aaO\", \"4E3\", \"4PR\", \"8bd\", \"HN\", \"GO\", \"10T\", \"5Or\", \"4J2\", \"507\", \"45w\", \"0JQ\", \"ic\", \n\"dS\", \"q2\", \"48G\", \"6Il\", \"73i\", \"4Rc\", \"0iM\", \"3LO\", \"XS\", \"M2\", \"5Pn\", \"61d\", \"7Ka\", \"4nc\", \"0UM\", \"2w\", \"39w\", \"8Qe\", \"4cS\", \"4v2\", \n\"aRN\", \"b3D\", \"02Y\", \"Uc\", \"Zb\", \"0yP\", \"bxM\", \"63U\", \"4y3\", \"4lR\", \"236\", \"0F\", \"2oo\", \"0ZL\", \"4ab\", \"6TA\", \"6zm\", \"4ON\", \"B3\", \"WR\", \n\"0jb\", \"IP\", \"6do\", \"4QL\", \"5oA\", \"6JC\", \"25e\", \"0DN\", \"9PG\", \"jL\", \"4g1\", \"46X\", \"686\", \"aCM\", \"h\", \"0gR\", \"0hS\", \"Ka\", \"72w\", \"677\", \n\"49Y\", \"4h0\", \"eM\", \"8Og\", \"0KO\", \"3nM\", \"6EB\", \"44i\", \"5Nl\", \"6kn\", \"FQ\", \"S0\", \"4bI\", \"6Wj\", \"zU\", \"o4\", \"03C\", \"Ty\", \"6yF\", \"4Le\", \n\"4AU\", \"4T4\", \"YI\", \"1jZ\", \"0TW\", \"3m\", \"5j9\", \"4oy\", \"54Q\", \"5e8\", \"xd\", \"1Kw\", \"01r\", \"VH\", \"7kV\", \"4NT\", \"4Cd\", \"62O\", \"2Mi\", \"0xJ\", \n\"0Vf\", \"uT\", \"6Xk\", \"4mH\", \"6Fi\", \"47B\", \"0Hd\", \"kV\", \"Ez\", \"0fH\", \"5MG\", \"6hE\", \"4E7\", \"4PV\", \"0kx\", \"HJ\", \"ff\", \"0ET\", \"bDI\", \"6KY\", \n\"503\", \"45s\", \"0JU\", \"ig\", \"GK\", \"0dy\", \"5Ov\", \"4J6\", \"6gD\", \"4Rg\", \"0iI\", \"3LK\", \"dW\", \"q6\", \"48C\", \"6Ih\", \"4Z2\", \"4OS\", \"00u\", \"WO\", \n\"yc\", \"0ZQ\", \"55V\", \"hgw\", \"6Yl\", \"4lO\", \"a2\", \"tS\", \"2Ln\", \"0yM\", \"4Bc\", \"63H\", \"6xA\", \"4Mb\", \"02D\", \"2Co\", \"2mC\", \"n3\", \"4cN\", \"6Vm\", \n\"a5G\", \"bTM\", \"0UP\", \"2j\", \"XN\", \"86l\", \"5Ps\", \"4U3\", \"5Nq\", \"4K1\", \"FL\", \"11W\", \"0KR\", \"3nP\", \"514\", \"44t\", \"49D\", \"6Ho\", \"eP\", \"49\", \n\"0hN\", \"3ML\", \"6fC\", \"5CA\", \"aV1\", \"6iB\", \"u\", \"0gO\", \"0Ic\", \"jQ\", \"6Gn\", \"46E\", \"bEN\", \"hyu\", \"ga\", \"0DS\", \"8cg\", \"IM\", \"4D0\", \"4QQ\", \n\"1FZ\", \"1A\", \"4x4\", \"4mU\", \"4Cy\", \"5F9\", \"0m6\", \"0xW\", \"C4\", \"VU\", \"7kK\", \"4NI\", \"54L\", \"6UF\", \"xy\", \"1Kj\", \"0TJ\", \"3p\", \"6ZG\", \"4od\", \n\"4AH\", \"60c\", \"YT\", \"L5\", \"0wV\", \"Td\", \"5I8\", \"4Lx\", \"4bT\", \"4w5\", \"zH\", \"0Yz\", \"dJ\", \"0Gx\", \"5lw\", \"4i7\", \"6gY\", \"4Rz\", \"0iT\", \"Jf\", \n\"GV\", \"R7\", \"5Ok\", \"6ji\", \"6DE\", \"45n\", \"0JH\", \"iz\", \"24b\", \"0EI\", \"5nF\", \"6KD\", \"6eh\", \"4PK\", \"0ke\", \"HW\", \"Eg\", \"0fU\", \"5MZ\", \"6hX\", \n\"4f6\", \"4sW\", \"0Hy\", \"kK\", \"yg\", \"0ZU\", \"55R\", \"6TX\", \"4Z6\", \"4OW\", \"00q\", \"WK\", \"2Lj\", \"0yI\", \"4Bg\", \"63L\", \"6Yh\", \"4lK\", \"a6\", \"tW\", \n\"2mG\", \"n7\", \"4cJ\", \"6Vi\", \"6xE\", \"4Mf\", \"0vH\", \"Uz\", \"XJ\", \"1kY\", \"5Pw\", \"4U7\", \"7Kx\", \"4nz\", \"0UT\", \"2n\", \"0KV\", \"hd\", \"510\", \"44p\", \n\"5Nu\", \"4K5\", \"FH\", \"0ez\", \"0hJ\", \"Kx\", \"6fG\", \"4Sd\", \"5mi\", \"6Hk\", \"eT\", \"p5\", \"0Ig\", \"jU\", \"6Gj\", \"46A\", \"5LD\", \"6iF\", \"q\", \"0gK\", \n\"1zZ\", \"II\", \"4D4\", \"4QU\", \"5oX\", \"5z9\", \"ge\", \"0DW\", \"byN\", \"62V\", \"0m2\", \"0xS\", \"225\", \"1E\", \"4x0\", \"4mQ\", \"54H\", \"6UB\", \"2nl\", \"1Kn\", \n\"C0\", \"VQ\", \"7kO\", \"4NM\", \"4AL\", \"60g\", \"YP\", \"L1\", \"0TN\", \"3t\", \"6ZC\", \"ae0\", \"4bP\", \"439\", \"zL\", \"8Pf\", \"03Z\", \"0b3\", \"aSM\", \"b2G\", \n\"73t\", \"664\", \"0iP\", \"Jb\", \"dN\", \"8Nd\", \"48Z\", \"4i3\", \"6DA\", \"45j\", \"0JL\", \"3oN\", \"GR\", \"R3\", \"5Oo\", \"6jm\", \"6el\", \"4PO\", \"0ka\", \"HS\", \n\"24f\", \"0EM\", \"5nB\", \"aaR\", \"4f2\", \"4sS\", \"8Ae\", \"kO\", \"Ec\", \"0fQ\", \"695\", \"aBN\", \"6Yd\", \"4lG\", \"0Wi\", \"0S\", \"Zw\", \"0yE\", \"4Bk\", \"6wH\", \n\"6zx\", \"buh\", \"0tu\", \"WG\", \"yk\", \"0ZY\", \"4aw\", \"5d7\", \"5k6\", \"4nv\", \"0UX\", \"2b\", \"XF\", \"1kU\", \"741\", \"61q\", \"6xI\", \"4Mj\", \"02L\", \"Uv\", \n\"2mK\", \"0Xh\", \"4cF\", \"6Ve\", \"49L\", \"6Hg\", \"eX\", \"41\", \"0hF\", \"Kt\", \"6fK\", \"4Sh\", \"5Ny\", \"4K9\", \"FD\", \"0ev\", \"0KZ\", \"hh\", \"5u4\", \"4pt\", \n\"5oT\", \"5z5\", \"gi\", \"1Tz\", \"0jw\", \"IE\", \"4D8\", \"4QY\", \"5LH\", \"6iJ\", \"Du\", \"0gG\", \"0Ik\", \"jY\", \"6Gf\", \"46M\", \"01g\", \"3Pm\", \"7kC\", \"4NA\", \n\"54D\", \"6UN\", \"xq\", \"1Kb\", \"0Vs\", \"1I\", \"a6d\", \"59t\", \"4Cq\", \"5F1\", \"d3G\", \"85O\", \"03V\", \"Tl\", \"5I0\", \"4Lp\", \"56u\", \"435\", \"2lQ\", \"0Yr\", \n\"0TB\", \"3x\", \"6ZO\", \"4ol\", \"5Qa\", \"60k\", \"2OM\", \"0zn\", \"2QO\", \"0dl\", \"5Oc\", \"6ja\", \"6DM\", \"45f\", \"1Za\", \"ir\", \"dB\", \"0Gp\", \"48V\", \"aco\", \n\"5W2\", \"4Rr\", \"94m\", \"Jn\", \"Eo\", \"12t\", \"5MR\", \"5X3\", \"aln\", \"47W\", \"0Hq\", \"kC\", \"fs\", \"0EA\", \"5nN\", \"6KL\", \"71I\", \"4PC\", \"0km\", \"3No\", \n\"Zs\", \"0yA\", \"4Bo\", \"63D\", \"7IA\", \"4lC\", \"0Wm\", \"0W\", \"yo\", \"8SE\", \"4as\", \"5d3\", \"aPn\", \"b1d\", \"00y\", \"WC\", \"XB\", \"1kQ\", \"745\", \"61u\", \n\"5k2\", \"4nr\", \"9Le\", \"2f\", \"2mO\", \"0Xl\", \"4cB\", \"6Va\", \"6xM\", \"4Mn\", \"02H\", \"Ur\", \"0hB\", \"Kp\", \"6fO\", \"4Sl\", \"49H\", \"6Hc\", \"27E\", \"45\", \n\"8BF\", \"hl\", \"518\", \"44x\", \"bdo\", \"aAm\", \"2PQ\", \"0er\", \"0js\", \"IA\", \"70W\", \"bkn\", \"5oP\", \"5z1\", \"gm\", \"304\", \"0Io\", \"28D\", \"6Gb\", \"46I\", \n\"5LL\", \"6iN\", \"y\", \"0gC\", \"5pH\", \"6UJ\", \"xu\", \"1Kf\", \"C8\", \"VY\", \"7kG\", \"4NE\", \"4Cu\", \"5F5\", \"2Mx\", \"1hz\", \"0Vw\", \"1M\", \"4x8\", \"4mY\", \n\"4bX\", \"431\", \"zD\", \"0Yv\", \"03R\", \"Th\", \"5I4\", \"4Lt\", \"4AD\", \"60o\", \"YX\", \"L9\", \"0TF\", \"wt\", \"6ZK\", \"4oh\", \"6DI\", \"45b\", \"0JD\", \"iv\", \n\"GZ\", \"0dh\", \"5Og\", \"6je\", \"5W6\", \"4Rv\", \"0iX\", \"Jj\", \"dF\", \"0Gt\", \"48R\", \"6Iy\", \"6Fx\", \"47S\", \"0Hu\", \"kG\", \"Ek\", \"0fY\", \"5MV\", \"5X7\", \n\"6ed\", \"4PG\", \"0ki\", \"3Nk\", \"fw\", \"0EE\", \"5nJ\", \"6KH\", \"356\", \"bo\", \"6OP\", \"4zs\", \"bnl\", \"75U\", \"LC\", \"0oq\", \"0bA\", \"As\", \"6lL\", \"4Yo\", \n\"43K\", \"7RA\", \"2yN\", \"0Lm\", \"17\", \"22G\", \"6Ma\", \"4xB\", \"4Vn\", \"6cM\", \"Nr\", \"19i\", \"14Y\", \"CB\", \"aDo\", \"bam\", \"41z\", \"5p2\", \"mn\", \"8GD\", \n\"7d\", \"8YF\", \"4kp\", \"5n0\", \"64w\", \"717\", \"1nS\", \"2KQ\", \"Pp\", \"07J\", \"4Hl\", \"69G\", \"6Sc\", \"52i\", \"1MO\", \"2hM\", \"5U\", \"0Ro\", \"4iA\", \"7LC\", \n\"66F\", \"4Gm\", \"08K\", \"3YA\", \"RA\", \"0qs\", \"b4f\", \"aUl\", \"5a1\", \"4dq\", \"8VG\", \"8e\", \"6mV\", \"4Xu\", \"17r\", \"022\", \"nE\", \"0Mw\", \"42Q\", \"4c8\", \n\"6NJ\", \"5kH\", \"1Pf\", \"cu\", \"MY\", \"X8\", \"4UE\", \"74O\", \"6og\", \"4ZD\", \"W9\", \"BX\", \"lt\", \"0OF\", \"4th\", \"6AK\", \"4l9\", \"4yX\", \"0Bv\", \"aD\", \n\"Oh\", \"0lZ\", \"4Wt\", \"5R4\", \"4Iv\", \"5L6\", \"Qj\", \"06P\", \"1LU\", \"1Y4\", \"463\", \"4gZ\", \"4jj\", \"7Oh\", \"rv\", \"0QD\", \"1oI\", \"2JK\", \"65m\", \"4DF\", \n\"4KG\", \"7nE\", \"2EJ\", \"04a\", \"1Nd\", \"2kf\", \"6PH\", \"4ek\", \"5xz\", \"492\", \"4O\", \"0Su\", \"09Q\", \"0h8\", \"5C7\", \"4Fw\", \"5Dz\", \"6ax\", \"LG\", \"0ou\", \n\"0AY\", \"bk\", \"6OT\", \"4zw\", \"43O\", \"6Bd\", \"2yJ\", \"0Li\", \"0bE\", \"Aw\", \"6lH\", \"4Yk\", \"4Vj\", \"6cI\", \"Nv\", \"0mD\", \"13\", \"22C\", \"6Me\", \"4xF\", \n\"4uv\", \"5p6\", \"mj\", \"0NX\", \"1pU\", \"CF\", \"6ny\", \"7k9\", \"4P9\", \"4EX\", \"1nW\", \"2KU\", \"sh\", \"0PZ\", \"4kt\", \"5n4\", \"6Sg\", \"4fD\", \"k9\", \"2hI\", \n\"Pt\", \"07N\", \"4Hh\", \"69C\", \"66B\", \"4Gi\", \"08O\", \"2Id\", \"5Q\", \"d8\", \"4iE\", \"7LG\", \"5a5\", \"4du\", \"1Oz\", \"8a\", \"RE\", \"0qw\", \"4JY\", \"aUh\", \n\"nA\", \"0Ms\", \"42U\", \"ail\", \"6mR\", \"4Xq\", \"17v\", \"026\", \"3Km\", \"0no\", \"4UA\", \"74K\", \"6NN\", \"5kL\", \"1Pb\", \"cq\", \"lp\", \"0OB\", \"40d\", \"6AO\", \n\"6oc\", \"5Ja\", \"0an\", \"2TM\", \"Ol\", \"18w\", \"4Wp\", \"5R0\", \"afm\", \"bCo\", \"0Br\", \"1G2\", \"1LQ\", \"1Y0\", \"467\", \"53w\", \"4Ir\", \"5L2\", \"Qn\", \"06T\", \n\"1oM\", \"2JO\", \"65i\", \"4DB\", \"4jn\", \"7Ol\", \"6z\", \"1Aa\", \"8WY\", \"2kb\", \"6PL\", \"4eo\", \"4KC\", \"7nA\", \"2EN\", \"04e\", \"09U\", \"d6E\", \"5C3\", \"4Fs\", \n\"bRl\", \"496\", \"4K\", \"0Sq\", \"0bI\", \"2Wj\", \"6lD\", \"4Yg\", \"43C\", \"6Bh\", \"oW\", \"z6\", \"0AU\", \"bg\", \"6OX\", \"5jZ\", \"4TW\", \"4A6\", \"LK\", \"0oy\", \n\"14Q\", \"CJ\", \"4N7\", \"5Kw\", \"41r\", \"542\", \"mf\", \"0NT\", \"u7\", \"22O\", \"6Mi\", \"4xJ\", \"4Vf\", \"6cE\", \"Nz\", \"0mH\", \"Px\", \"07B\", \"4Hd\", \"69O\", \n\"6Sk\", \"4fH\", \"k5\", \"2hE\", \"7l\", \"0PV\", \"4kx\", \"5n8\", \"4P5\", \"4ET\", \"83j\", \"2KY\", \"RI\", \"05s\", \"4JU\", \"7oW\", \"5a9\", \"4dy\", \"1Ov\", \"8m\", \n\"qU\", \"d4\", \"4iI\", \"7LK\", \"66N\", \"4Ge\", \"08C\", \"2Ih\", \"6NB\", \"a59\", \"1Pn\", \"21d\", \"MQ\", \"X0\", \"4UM\", \"74G\", \"79w\", \"bbN\", \"0cS\", \"0v2\", \n\"nM\", \"8Dg\", \"42Y\", \"4c0\", \"4l1\", \"4yP\", \"8Kf\", \"aL\", \"0y3\", \"0lR\", \"636\", \"76v\", \"6oo\", \"4ZL\", \"W1\", \"BP\", \"2zm\", \"0ON\", \"40h\", \"6AC\", \n\"4jb\", \"auS\", \"6v\", \"0QL\", \"I3\", \"2JC\", \"65e\", \"4DN\", \"b7E\", \"68U\", \"Qb\", \"06X\", \"286\", \"dSl\", \"4r3\", \"4gR\", \"4hS\", \"7MQ\", \"4G\", \"277\", \n\"09Y\", \"0h0\", \"67T\", \"b8D\", \"4KO\", \"7nM\", \"SS\", \"F2\", \"1Nl\", \"9w\", \"azR\", \"4ec\", \"43G\", \"6Bl\", \"oS\", \"z2\", \"0bM\", \"2Wn\", \"78i\", \"4Yc\", \n\"4TS\", \"4A2\", \"LO\", \"8fe\", \"0AQ\", \"bc\", \"aeN\", \"cPm\", \"41v\", \"546\", \"mb\", \"0NP\", \"14U\", \"CN\", \"4N3\", \"5Ks\", \"4Vb\", \"6cA\", \"2Xo\", \"0mL\", \n\"u3\", \"22K\", \"6Mm\", \"4xN\", \"6So\", \"4fL\", \"k1\", \"2hA\", \"2Fm\", \"07F\", \"5XA\", \"69K\", \"4P1\", \"4EP\", \"83n\", \"d5f\", \"7h\", \"0PR\", \"bQO\", \"a0E\", \n\"hbu\", \"50T\", \"1Or\", \"8i\", \"RM\", \"05w\", \"4JQ\", \"7oS\", \"66J\", \"4Ga\", \"08G\", \"2Il\", \"5Y\", \"d0\", \"4iM\", \"7LO\", \"MU\", \"X4\", \"4UI\", \"74C\", \n\"6NF\", \"5kD\", \"1Pj\", \"cy\", \"nI\", \"2m9\", \"4vU\", \"4c4\", \"6mZ\", \"4Xy\", \"0cW\", \"0v6\", \"Od\", \"0lV\", \"4Wx\", \"5R8\", \"4l5\", \"4yT\", \"0Bz\", \"aH\", \n\"lx\", \"0OJ\", \"40l\", \"6AG\", \"6ok\", \"4ZH\", \"W5\", \"BT\", \"I7\", \"2JG\", \"65a\", \"4DJ\", \"4jf\", \"7Od\", \"6r\", \"0QH\", \"1LY\", \"1Y8\", \"4r7\", \"4gV\", \n\"4Iz\", \"68Q\", \"Qf\", \"0rT\", \"1mt\", \"0h4\", \"67P\", \"5VZ\", \"4hW\", \"7MU\", \"4C\", \"0Sy\", \"1Nh\", \"9s\", \"6PD\", \"4eg\", \"4KK\", \"7nI\", \"SW\", \"F6\", \n\"8Je\", \"22V\", \"4m2\", \"4xS\", \"625\", \"77u\", \"Nc\", \"0mQ\", \"V2\", \"CS\", \"6nl\", \"5Kn\", \"41k\", \"7Pa\", \"3kO\", \"0NM\", \"0AL\", \"20g\", \"6OA\", \"4zb\", \n\"4TN\", \"6am\", \"LR\", \"Y3\", \"0bP\", \"Ab\", \"78t\", \"bcM\", \"43Z\", \"4b3\", \"oN\", \"8Ed\", \"5D\", \"264\", \"4iP\", \"489\", \"66W\", \"b9G\", \"08Z\", \"0i3\", \n\"RP\", \"G1\", \"4JL\", \"7oN\", \"6QC\", \"50I\", \"1Oo\", \"8t\", \"7u\", \"0PO\", \"4ka\", \"7Nc\", \"64f\", \"4EM\", \"H0\", \"963\", \"Pa\", \"0sS\", \"b6F\", \"69V\", \n\"478\", \"4fQ\", \"295\", \"dRo\", \"4O4\", \"4ZU\", \"15R\", \"BI\", \"le\", \"0OW\", \"40q\", \"551\", \"6Lj\", \"4yI\", \"t4\", \"aU\", \"Oy\", \"0lK\", \"4We\", \"6bF\", \n\"6mG\", \"4Xd\", \"0cJ\", \"2Vi\", \"nT\", \"0Mf\", \"4vH\", \"6Ck\", \"adI\", \"5kY\", \"1Pw\", \"cd\", \"MH\", \"0nz\", \"4UT\", \"7pV\", \"4KV\", \"7nT\", \"SJ\", \"04p\", \n\"1Nu\", \"9n\", \"6PY\", \"4ez\", \"4hJ\", \"7MH\", \"pV\", \"e7\", \"1mi\", \"2Hk\", \"67M\", \"4Ff\", \"4Ig\", \"68L\", \"2Gj\", \"06A\", \"j6\", \"2iF\", \"6Rh\", \"4gK\", \n\"5zZ\", \"7Oy\", \"6o\", \"0QU\", \"1oX\", \"1z9\", \"4Q6\", \"4DW\", \"5FZ\", \"6cX\", \"Ng\", \"0mU\", \"0Cy\", \"1F9\", \"4m6\", \"4xW\", \"41o\", \"7Pe\", \"3kK\", \"0NI\", \n\"V6\", \"CW\", \"6nh\", \"5Kj\", \"4TJ\", \"6ai\", \"LV\", \"Y7\", \"0AH\", \"bz\", \"6OE\", \"4zf\", \"4wV\", \"4b7\", \"oJ\", \"0Lx\", \"0bT\", \"Af\", \"6lY\", \"4Yz\", \n\"5B8\", \"4Gx\", \"1lw\", \"0i7\", \"qH\", \"0Rz\", \"4iT\", \"7LV\", \"6QG\", \"4dd\", \"1Ok\", \"8p\", \"RT\", \"G5\", \"4JH\", \"7oJ\", \"64b\", \"4EI\", \"H4\", \"2KD\", \n\"7q\", \"0PK\", \"4ke\", \"7Ng\", \"4s4\", \"4fU\", \"1MZ\", \"2hX\", \"Pe\", \"0sW\", \"4Hy\", \"5M9\", \"la\", \"0OS\", \"40u\", \"555\", \"4O0\", \"4ZQ\", \"15V\", \"BM\", \n\"2Yl\", \"0lO\", \"4Wa\", \"6bB\", \"6Ln\", \"4yM\", \"08\", \"aQ\", \"nP\", \"0Mb\", \"42D\", \"6Co\", \"6mC\", \"5HA\", \"0cN\", \"2Vm\", \"ML\", \"8gf\", \"4UP\", \"74Z\", \n\"adM\", \"bAO\", \"1Ps\", \"0U3\", \"1Nq\", \"9j\", \"azO\", \"51W\", \"4KR\", \"7nP\", \"SN\", \"04t\", \"09D\", \"2Ho\", \"67I\", \"4Fb\", \"4hN\", \"7ML\", \"4Z\", \"e3\", \n\"j2\", \"2iB\", \"6Rl\", \"4gO\", \"4Ic\", \"68H\", \"2Gn\", \"06E\", \"82m\", \"d4e\", \"4Q2\", \"4DS\", \"bPL\", \"a1F\", \"6k\", \"0QQ\", \"1pH\", \"2UJ\", \"6nd\", \"5Kf\", \n\"41c\", \"7Pi\", \"mw\", \"0NE\", \"0Cu\", \"1F5\", \"6Mx\", \"5hz\", \"4Vw\", \"5S7\", \"Nk\", \"0mY\", \"0bX\", \"Aj\", \"6lU\", \"4Yv\", \"43R\", \"6By\", \"oF\", \"0Lt\", \n\"0AD\", \"bv\", \"6OI\", \"4zj\", \"4TF\", \"6ae\", \"LZ\", \"0oh\", \"RX\", \"G9\", \"4JD\", \"7oF\", \"6QK\", \"4dh\", \"1Og\", \"2je\", \"5L\", \"0Rv\", \"4iX\", \"481\", \n\"5B4\", \"4Gt\", \"08R\", \"2Iy\", \"Pi\", \"07S\", \"4Hu\", \"5M5\", \"470\", \"4fY\", \"1MV\", \"1X7\", \"su\", \"0PG\", \"4ki\", \"7Nk\", \"64n\", \"4EE\", \"H8\", \"2KH\", \n\"6Lb\", \"4yA\", \"04\", \"23D\", \"Oq\", \"0lC\", \"4Wm\", \"6bN\", \"aEl\", \"c4G\", \"0as\", \"BA\", \"lm\", \"8FG\", \"40y\", \"559\", \"6NS\", \"5kQ\", \"345\", \"cl\", \n\"1k2\", \"0nr\", \"boo\", \"74V\", \"6mO\", \"4Xl\", \"0cB\", \"2Va\", \"2xM\", \"0Mn\", \"42H\", \"6Cc\", \"4hB\", \"aws\", \"4V\", \"0Sl\", \"09H\", \"2Hc\", \"67E\", \"4Fn\", \n\"b5e\", \"aTo\", \"SB\", \"04x\", \"8WD\", \"9f\", \"6PQ\", \"4er\", \"4js\", \"5o3\", \"6g\", \"8XE\", \"1oP\", \"1z1\", \"65t\", \"704\", \"4Io\", \"68D\", \"Qs\", \"06I\", \n\"1LL\", \"2iN\", \"7BA\", \"4gC\", \"41g\", \"7Pm\", \"ms\", \"0NA\", \"14D\", \"2UN\", \"aDr\", \"5Kb\", \"4Vs\", \"5S3\", \"No\", \"19t\", \"0Cq\", \"1F1\", \"agn\", \"bBl\", \n\"43V\", \"aho\", \"oB\", \"0Lp\", \"16u\", \"An\", \"6lQ\", \"4Yr\", \"4TB\", \"6aa\", \"2ZO\", \"0ol\", \"1Qa\", \"br\", \"6OM\", \"4zn\", \"6QO\", \"4dl\", \"1Oc\", \"8x\", \n\"2DM\", \"05f\", \"5Za\", \"7oB\", \"5B0\", \"4Gp\", \"08V\", \"d7F\", \"5H\", \"0Rr\", \"bSo\", \"485\", \"474\", \"52t\", \"1MR\", \"1X3\", \"Pm\", \"07W\", \"4Hq\", \"5M1\", \n\"64j\", \"4EA\", \"1nN\", \"2KL\", \"7y\", \"0PC\", \"4km\", \"7No\", \"Ou\", \"0lG\", \"4Wi\", \"6bJ\", \"6Lf\", \"4yE\", \"00\", \"aY\", \"li\", \"8FC\", \"4tu\", \"5q5\", \n\"4O8\", \"4ZY\", \"0aw\", \"BE\", \"MD\", \"0nv\", \"4UX\", \"74R\", \"6NW\", \"5kU\", \"341\", \"ch\", \"nX\", \"0Mj\", \"42L\", \"6Cg\", \"6mK\", \"4Xh\", \"0cF\", \"2Ve\", \n\"09L\", \"2Hg\", \"67A\", \"4Fj\", \"4hF\", \"7MD\", \"4R\", \"0Sh\", \"1Ny\", \"9b\", \"6PU\", \"4ev\", \"4KZ\", \"7nX\", \"SF\", \"0pt\", \"1oT\", \"1z5\", \"65p\", \"5Tz\", \n\"4jw\", \"5o7\", \"6c\", \"0QY\", \"1LH\", \"2iJ\", \"6Rd\", \"4gG\", \"4Ik\", \"7li\", \"Qw\", \"06M\", \"7F\", \"246\", \"4kR\", \"7NP\", \"64U\", \"col\", \"1nq\", \"0k1\", \n\"PR\", \"E3\", \"4HN\", \"69e\", \"6SA\", \"4fb\", \"1Mm\", \"2ho\", \"5w\", \"0RM\", \"4ic\", \"7La\", \"66d\", \"4GO\", \"J2\", \"2IB\", \"Rc\", \"05Y\", \"b4D\", \"aUN\", \n\"4q2\", \"4dS\", \"8Ve\", \"8G\", \"8Hg\", \"bM\", \"4o0\", \"4zQ\", \"607\", \"75w\", \"La\", \"0oS\", \"T0\", \"AQ\", \"6ln\", \"4YM\", \"43i\", \"6BB\", \"2yl\", \"0LO\", \n\"0CN\", \"22e\", \"6MC\", \"5hA\", \"4VL\", \"6co\", \"NP\", \"0mb\", \"1ps\", \"0u3\", \"aDM\", \"baO\", \"41X\", \"7PR\", \"mL\", \"8Gf\", \"4IT\", \"7lV\", \"QH\", \"06r\", \n\"1Lw\", \"0I7\", \"5b8\", \"4gx\", \"4jH\", \"7OJ\", \"rT\", \"g5\", \"1ok\", \"2Ji\", \"65O\", \"4Dd\", \"4Ke\", \"7ng\", \"Sy\", \"04C\", \"h4\", \"2kD\", \"6Pj\", \"4eI\", \n\"4hy\", \"5m9\", \"4m\", \"0SW\", \"09s\", \"2HX\", \"4S4\", \"4FU\", \"4M6\", \"4XW\", \"0cy\", \"1f9\", \"ng\", \"0MU\", \"42s\", \"573\", \"6Nh\", \"5kj\", \"v6\", \"cW\", \n\"3KK\", \"0nI\", \"4Ug\", \"74m\", \"6oE\", \"4Zf\", \"0aH\", \"Bz\", \"lV\", \"y7\", \"40B\", \"6Ai\", \"582\", \"4yz\", \"0BT\", \"af\", \"OJ\", \"0lx\", \"4WV\", \"4B7\", \n\"64Q\", \"4Ez\", \"1nu\", \"0k5\", \"7B\", \"0Px\", \"4kV\", \"7NT\", \"6SE\", \"4ff\", \"1Mi\", \"2hk\", \"PV\", \"E7\", \"4HJ\", \"69a\", \"6rh\", \"4GK\", \"J6\", \"2IF\", \n\"5s\", \"0RI\", \"4ig\", \"7Le\", \"4q6\", \"4dW\", \"1OX\", \"8C\", \"Rg\", \"0qU\", \"5ZZ\", \"7oy\", \"4Ty\", \"5Q9\", \"Le\", \"0oW\", \"1QZ\", \"bI\", \"4o4\", \"4zU\", \n\"43m\", \"6BF\", \"oy\", \"0LK\", \"T4\", \"AU\", \"6lj\", \"4YI\", \"4VH\", \"6ck\", \"NT\", \"0mf\", \"0CJ\", \"22a\", \"6MG\", \"4xd\", \"4uT\", \"7PV\", \"mH\", \"0Nz\", \n\"1pw\", \"Cd\", \"aDI\", \"5KY\", \"1Ls\", \"0I3\", \"axM\", \"53U\", \"4IP\", \"7lR\", \"QL\", \"06v\", \"1oo\", \"2Jm\", \"65K\", \"5TA\", \"4jL\", \"7ON\", \"6X\", \"g1\", \n\"h0\", \"9Y\", \"6Pn\", \"4eM\", \"4Ka\", \"7nc\", \"2El\", \"04G\", \"09w\", \"d6g\", \"4S0\", \"4FQ\", \"bRN\", \"a3D\", \"4i\", \"0SS\", \"nc\", \"0MQ\", \"42w\", \"577\", \n\"4M2\", \"4XS\", \"17T\", \"dlm\", \"3KO\", \"0nM\", \"4Uc\", \"74i\", \"6Nl\", \"5kn\", \"v2\", \"cS\", \"lR\", \"y3\", \"40F\", \"6Am\", \"6oA\", \"4Zb\", \"0aL\", \"2To\", \n\"ON\", \"18U\", \"4WR\", \"4B3\", \"586\", \"bCM\", \"0BP\", \"ab\", \"PZ\", \"0sh\", \"4HF\", \"69m\", \"6SI\", \"4fj\", \"1Me\", \"2hg\", \"7N\", \"0Pt\", \"4kZ\", \"7NX\", \n\"6pU\", \"4Ev\", \"1ny\", \"0k9\", \"Rk\", \"05Q\", \"4Jw\", \"5O7\", \"452\", \"50r\", \"1OT\", \"8O\", \"qw\", \"0RE\", \"4ik\", \"7Li\", \"66l\", \"4GG\", \"08a\", \"2IJ\", \n\"T8\", \"AY\", \"6lf\", \"4YE\", \"43a\", \"6BJ\", \"ou\", \"0LG\", \"0Aw\", \"bE\", \"4o8\", \"4zY\", \"4Tu\", \"5Q5\", \"Li\", \"8fC\", \"14s\", \"Ch\", \"6nW\", \"5KU\", \n\"41P\", \"7PZ\", \"mD\", \"0Nv\", \"0CF\", \"22m\", \"6MK\", \"4xh\", \"4VD\", \"6cg\", \"NX\", \"0mj\", \"5za\", \"7OB\", \"6T\", \"0Qn\", \"1oc\", \"2Ja\", \"65G\", \"4Dl\", \n\"b7g\", \"68w\", \"1w2\", \"06z\", \"8UF\", \"dSN\", \"5b0\", \"4gp\", \"4hq\", \"5m1\", \"4e\", \"8ZG\", \"1mR\", \"1x3\", \"67v\", \"726\", \"4Km\", \"7no\", \"Sq\", \"04K\", \n\"1NN\", \"9U\", \"6Pb\", \"4eA\", \"adr\", \"5kb\", \"26\", \"21F\", \"Ms\", \"0nA\", \"4Uo\", \"74e\", \"79U\", \"bbl\", \"0cq\", \"1f1\", \"no\", \"396\", \"4vs\", \"5s3\", \n\"6LQ\", \"4yr\", \"367\", \"an\", \"OB\", \"0lp\", \"bmm\", \"76T\", \"6oM\", \"4Zn\", \"15i\", \"Br\", \"2zO\", \"0Ol\", \"40J\", \"6Aa\", \"6SM\", \"4fn\", \"1Ma\", \"2hc\", \n\"2FO\", \"07d\", \"4HB\", \"69i\", \"64Y\", \"4Er\", \"83L\", \"d5D\", \"7J\", \"0Pp\", \"bQm\", \"a0g\", \"456\", \"50v\", \"1OP\", \"8K\", \"Ro\", \"05U\", \"4Js\", \"5O3\", \n\"66h\", \"4GC\", \"08e\", \"2IN\", \"qs\", \"0RA\", \"4io\", \"7Lm\", \"43e\", \"6BN\", \"oq\", \"0LC\", \"0bo\", \"2WL\", \"6lb\", \"4YA\", \"4Tq\", \"5Q1\", \"Lm\", \"8fG\", \n\"0As\", \"bA\", \"ael\", \"cPO\", \"41T\", \"ajm\", \"1K2\", \"0Nr\", \"14w\", \"Cl\", \"6nS\", \"5KQ\", \"5Fa\", \"6cc\", \"2XM\", \"0mn\", \"0CB\", \"22i\", \"6MO\", \"4xl\", \n\"1og\", \"2Je\", \"65C\", \"4Dh\", \"4jD\", \"7OF\", \"6P\", \"g9\", \"3l9\", \"2iy\", \"5b4\", \"4gt\", \"4IX\", \"68s\", \"QD\", \"0rv\", \"1mV\", \"1x7\", \"4S8\", \"4FY\", \n\"4hu\", \"5m5\", \"4a\", \"1Cz\", \"h8\", \"9Q\", \"6Pf\", \"4eE\", \"4Ki\", \"7nk\", \"Su\", \"04O\", \"Mw\", \"0nE\", \"4Uk\", \"74a\", \"6Nd\", \"5kf\", \"22\", \"21B\", \n\"nk\", \"0MY\", \"4vw\", \"5s7\", \"6mx\", \"5Hz\", \"0cu\", \"1f5\", \"OF\", \"0lt\", \"4WZ\", \"6by\", \"6LU\", \"4yv\", \"0BX\", \"aj\", \"lZ\", \"0Oh\", \"40N\", \"6Ae\", \n\"6oI\", \"4Zj\", \"0aD\", \"Bv\", \"5f\", \"9Ke\", \"4ir\", \"5l2\", \"66u\", \"735\", \"08x\", \"1y0\", \"Rr\", \"05H\", \"4Jn\", \"7ol\", \"6Qa\", \"4dB\", \"1OM\", \"8V\", \n\"7W\", \"0Pm\", \"4kC\", \"7NA\", \"64D\", \"4Eo\", \"83Q\", \"2Kb\", \"PC\", \"07y\", \"b6d\", \"69t\", \"5c3\", \"4fs\", \"8TE\", \"dRM\", \"374\", \"22t\", \"599\", \"4xq\", \n\"bln\", \"77W\", \"NA\", \"0ms\", \"14j\", \"Cq\", \"6nN\", \"5KL\", \"41I\", \"7PC\", \"3km\", \"0No\", \"35\", \"20E\", \"6Oc\", \"5ja\", \"4Tl\", \"6aO\", \"Lp\", \"0oB\", \n\"0br\", \"1g2\", \"78V\", \"bco\", \"43x\", \"568\", \"ol\", \"385\", \"4Kt\", \"5N4\", \"Sh\", \"04R\", \"1NW\", \"9L\", \"441\", \"4eX\", \"4hh\", \"7Mj\", \"pt\", \"0SF\", \n\"K9\", \"2HI\", \"67o\", \"4FD\", \"4IE\", \"68n\", \"QY\", \"0\", \"1Lf\", \"2id\", \"6RJ\", \"4gi\", \"4jY\", \"auh\", \"6M\", \"0Qw\", \"1oz\", \"2Jx\", \"5A5\", \"4Du\", \n\"6oT\", \"4Zw\", \"0aY\", \"Bk\", \"lG\", \"0Ou\", \"40S\", \"6Ax\", \"6LH\", \"4yk\", \"0BE\", \"aw\", \"2YJ\", \"0li\", \"4WG\", \"6bd\", \"6me\", \"4XF\", \"0ch\", \"2VK\", \n\"nv\", \"0MD\", \"42b\", \"6CI\", \"6Ny\", \"7K9\", \"1PU\", \"cF\", \"Mj\", \"0nX\", \"4Uv\", \"5P6\", \"66q\", \"4GZ\", \"1lU\", \"1y4\", \"5b\", \"0RX\", \"4iv\", \"5l6\", \n\"6Qe\", \"4dF\", \"1OI\", \"8R\", \"Rv\", \"05L\", \"4Jj\", \"7oh\", \"6pH\", \"4Ek\", \"1nd\", \"2Kf\", \"7S\", \"0Pi\", \"4kG\", \"7NE\", \"5c7\", \"4fw\", \"1Mx\", \"0H8\", \n\"PG\", \"0su\", \"5Xz\", \"69p\", \"4VY\", \"4C8\", \"NE\", \"0mw\", \"1Sz\", \"22p\", \"6MV\", \"4xu\", \"41M\", \"7PG\", \"mY\", \"x8\", \"14n\", \"Cu\", \"6nJ\", \"5KH\", \n\"4Th\", \"6aK\", \"Lt\", \"0oF\", \"31\", \"bX\", \"6Og\", \"4zD\", \"4wt\", \"5r4\", \"oh\", \"0LZ\", \"0bv\", \"AD\", \"4L9\", \"4YX\", \"1NS\", \"9H\", \"445\", \"51u\", \n\"4Kp\", \"5N0\", \"Sl\", \"04V\", \"09f\", \"2HM\", \"67k\", \"5Va\", \"4hl\", \"7Mn\", \"4x\", \"0SB\", \"1Lb\", \"3yA\", \"6RN\", \"4gm\", \"4IA\", \"68j\", \"2GL\", \"4\", \n\"82O\", \"d4G\", \"5A1\", \"4Dq\", \"bPn\", \"a1d\", \"6I\", \"0Qs\", \"lC\", \"0Oq\", \"40W\", \"akn\", \"6oP\", \"4Zs\", \"15t\", \"Bo\", \"2YN\", \"0lm\", \"4WC\", \"76I\", \n\"6LL\", \"4yo\", \"0BA\", \"as\", \"nr\", \"8DX\", \"42f\", \"6CM\", \"6ma\", \"4XB\", \"0cl\", \"2VO\", \"Mn\", \"8gD\", \"4Ur\", \"5P2\", \"ado\", \"bAm\", \"1PQ\", \"cB\", \n\"Rz\", \"0qH\", \"4Jf\", \"7od\", \"6Qi\", \"4dJ\", \"i7\", \"2jG\", \"5n\", \"0RT\", \"4iz\", \"7Lx\", \"4R7\", \"4GV\", \"08p\", \"1y8\", \"PK\", \"07q\", \"4HW\", \"7mU\", \n\"6SX\", \"52R\", \"1Mt\", \"0H4\", \"sW\", \"f6\", \"4kK\", \"7NI\", \"64L\", \"4Eg\", \"1nh\", \"2Kj\", \"14b\", \"Cy\", \"6nF\", \"5KD\", \"41A\", \"7PK\", \"mU\", \"x4\", \n\"0CW\", \"0V6\", \"591\", \"4xy\", \"4VU\", \"4C4\", \"NI\", \"19R\", \"0bz\", \"AH\", \"4L5\", \"4YT\", \"43p\", \"560\", \"od\", \"0LV\", \"w5\", \"bT\", \"6Ok\", \"4zH\", \n\"4Td\", \"6aG\", \"Lx\", \"0oJ\", \"5xA\", \"7Mb\", \"4t\", \"0SN\", \"K1\", \"2HA\", \"67g\", \"4FL\", \"b5G\", \"aTM\", \"0e3\", \"04Z\", \"8Wf\", \"9D\", \"449\", \"4eP\", \n\"4jQ\", \"7OS\", \"6E\", \"255\", \"1or\", \"0j2\", \"65V\", \"cno\", \"4IM\", \"68f\", \"QQ\", \"8\", \"1Ln\", \"2il\", \"6RB\", \"4ga\", \"afR\", \"4yc\", \"0BM\", \"23f\", \n\"OS\", \"Z2\", \"4WO\", \"6bl\", \"aEN\", \"c4e\", \"0aQ\", \"Bc\", \"lO\", \"8Fe\", \"4tS\", \"4a2\", \"4n3\", \"5ks\", \"8Id\", \"cN\", \"Mb\", \"0nP\", \"614\", \"74t\", \n\"6mm\", \"4XN\", \"U3\", \"2VC\", \"2xo\", \"0ML\", \"42j\", \"6CA\", \"6Qm\", \"4dN\", \"i3\", \"8Z\", \"2Do\", \"05D\", \"4Jb\", \"aUS\", \"4R3\", \"4GR\", \"08t\", \"d7d\", \n\"5j\", \"0RP\", \"bSM\", \"a2G\", \"ayN\", \"52V\", \"1Mp\", \"0H0\", \"PO\", \"07u\", \"4HS\", \"69x\", \"64H\", \"4Ec\", \"1nl\", \"2Kn\", \"sS\", \"f2\", \"4kO\", \"7NM\", \n\"41E\", \"7PO\", \"mQ\", \"x0\", \"14f\", \"2Ul\", \"6nB\", \"aQ1\", \"4VQ\", \"4C0\", \"NM\", \"19V\", \"0CS\", \"0V2\", \"595\", \"bBN\", \"43t\", \"564\", \"0Y3\", \"0LR\", \n\"16W\", \"AL\", \"4L1\", \"4YP\", \"5DA\", \"6aC\", \"2Zm\", \"0oN\", \"39\", \"bP\", \"6Oo\", \"4zL\", \"K5\", \"2HE\", \"67c\", \"4FH\", \"4hd\", \"7Mf\", \"4p\", \"0SJ\", \n\"8Wb\", \"2kY\", \"4p5\", \"4eT\", \"4Kx\", \"5N8\", \"Sd\", \"0pV\", \"1ov\", \"0j6\", \"5A9\", \"4Dy\", \"4jU\", \"7OW\", \"6A\", \"1AZ\", \"1Lj\", \"2ih\", \"6RF\", \"4ge\", \n\"4II\", \"68b\", \"QU\", \"D4\", \"OW\", \"Z6\", \"4WK\", \"6bh\", \"6LD\", \"4yg\", \"0BI\", \"23b\", \"lK\", \"0Oy\", \"4tW\", \"4a6\", \"6oX\", \"5JZ\", \"0aU\", \"Bg\", \n\"Mf\", \"0nT\", \"4Uz\", \"74p\", \"4n7\", \"5kw\", \"1PY\", \"cJ\", \"nz\", \"0MH\", \"42n\", \"6CE\", \"6mi\", \"4XJ\", \"U7\", \"2VG\", \"4MP\", \"4X1\", \"UL\", \"02v\", \n\"0XR\", \"0M3\", \"a8E\", \"57U\", \"4nL\", \"7KN\", \"2X\", \"c1\", \"1ko\", \"2Nm\", \"61K\", \"5PA\", \"4Oa\", \"6zB\", \"2Al\", \"00G\", \"l0\", \"yQ\", \"6Tn\", \"4aM\", \n\"58T\", \"a7D\", \"0i\", \"0WS\", \"84o\", \"ZM\", \"4W0\", \"4BQ\", \"4I2\", \"5Lr\", \"13T\", \"G\", \"jc\", \"0IQ\", \"46w\", \"537\", \"6Jl\", \"5on\", \"r2\", \"gS\", \n\"3OO\", \"0jM\", \"4Qc\", \"70i\", \"6kA\", \"5NC\", \"0eL\", \"2Po\", \"hR\", \"8Bx\", \"44F\", \"6Em\", \"abO\", \"49v\", \"0FP\", \"eb\", \"KN\", \"8ad\", \"4SR\", \"4F3\", \n\"3B\", \"0Tx\", \"4oV\", \"4z7\", \"60Q\", \"4Az\", \"0zT\", \"Yf\", \"TV\", \"A7\", \"4LJ\", \"6yi\", \"6WE\", \"4bf\", \"0YH\", \"zz\", \"1s\", \"0VI\", \"4mg\", \"6XD\", \n\"6vh\", \"4CK\", \"N6\", \"2MF\", \"Vg\", \"0uU\", \"6n9\", \"7ky\", \"4u6\", \"5pv\", \"1KX\", \"xK\", \"1UZ\", \"fI\", \"4k4\", \"5nt\", \"4Py\", \"5U9\", \"He\", \"0kW\", \n\"P4\", \"EU\", \"6hj\", \"5Mh\", \"47m\", \"6FF\", \"ky\", \"0HK\", \"0GJ\", \"dx\", \"6IG\", \"48l\", \"4RH\", \"6gk\", \"JT\", \"0if\", \"0dV\", \"Gd\", \"5Z8\", \"5OY\", \n\"4qT\", \"4d5\", \"iH\", \"0Jz\", \"0XV\", \"0M7\", \"5f8\", \"4cx\", \"4MT\", \"4X5\", \"UH\", \"02r\", \"1kk\", \"Xx\", \"61O\", \"5PE\", \"4nH\", \"7KJ\", \"vT\", \"c5\", \n\"l4\", \"yU\", \"6Tj\", \"4aI\", \"4Oe\", \"6zF\", \"Wy\", \"00C\", \"1iZ\", \"ZI\", \"4W4\", \"4BU\", \"4ly\", \"5i9\", \"0m\", \"0WW\", \"jg\", \"0IU\", \"46s\", \"533\", \n\"4I6\", \"5Lv\", \"0gy\", \"C\", \"3OK\", \"0jI\", \"4Qg\", \"6dD\", \"6Jh\", \"5oj\", \"r6\", \"gW\", \"hV\", \"0Kd\", \"44B\", \"6Ei\", \"6kE\", \"5NG\", \"0eH\", \"Fz\", \n\"KJ\", \"0hx\", \"4SV\", \"4F7\", \"6HY\", \"49r\", \"0FT\", \"ef\", \"60U\", \"ckl\", \"0zP\", \"Yb\", \"3F\", \"206\", \"4oR\", \"4z3\", \"6WA\", \"4bb\", \"0YL\", \"2lo\", \n\"TR\", \"A3\", \"4LN\", \"6ym\", \"62d\", \"4CO\", \"N2\", \"2MB\", \"1w\", \"0VM\", \"4mc\", \"7Ha\", \"4u2\", \"54z\", \"8Re\", \"xO\", \"Vc\", \"01Y\", \"b0D\", \"aQN\", \n\"647\", \"71w\", \"Ha\", \"0kS\", \"8Lg\", \"fM\", \"4k0\", \"5np\", \"47i\", \"6FB\", \"29d\", \"0HO\", \"P0\", \"EQ\", \"6hn\", \"5Ml\", \"4RL\", \"6go\", \"JP\", \"0ib\", \n\"0GN\", \"26e\", \"6IC\", \"48h\", \"45X\", \"4d1\", \"iL\", \"8Cf\", \"0dR\", \"0q3\", \"hYt\", \"beO\", \"4nD\", \"7KF\", \"2P\", \"c9\", \"1kg\", \"Xt\", \"61C\", \"5PI\", \n\"4MX\", \"4X9\", \"UD\", \"0vv\", \"0XZ\", \"2my\", \"5f4\", \"4ct\", \"4lu\", \"5i5\", \"0a\", \"1Gz\", \"0yw\", \"ZE\", \"4W8\", \"4BY\", \"4Oi\", \"6zJ\", \"Wu\", \"00O\", \n\"l8\", \"yY\", \"6Tf\", \"4aE\", \"6Jd\", \"5of\", \"62\", \"25B\", \"Iw\", \"0jE\", \"4Qk\", \"6dH\", \"6ix\", \"5Lz\", \"0gu\", \"O\", \"jk\", \"0IY\", \"4rw\", \"5w7\", \n\"5x6\", \"5mW\", \"0FX\", \"ej\", \"KF\", \"0ht\", \"4SZ\", \"6fy\", \"6kI\", \"5NK\", \"0eD\", \"Fv\", \"hZ\", \"93\", \"44N\", \"6Ee\", \"2BO\", \"03d\", \"4LB\", \"6ya\", \n\"6WM\", \"4bn\", \"1Ia\", \"zr\", \"3J\", \"0Tp\", \"bUm\", \"a4g\", \"5D2\", \"4Ar\", \"87L\", \"Yn\", \"Vo\", \"01U\", \"4Ns\", \"5K3\", \"416\", \"54v\", \"1KP\", \"xC\", \n\"us\", \"0VA\", \"4mo\", \"6XL\", \"62h\", \"4CC\", \"0xm\", \"2MN\", \"0fo\", \"2SL\", \"6hb\", \"bgr\", \"47e\", \"6FN\", \"kq\", \"0HC\", \"0Es\", \"fA\", \"aal\", \"bDn\", \n\"4Pq\", \"5U1\", \"Hm\", \"8bG\", \"10w\", \"Gl\", \"5Z0\", \"5OQ\", \"45T\", \"anm\", \"1O2\", \"0Jr\", \"0GB\", \"dp\", \"6IO\", \"48d\", \"5Ba\", \"6gc\", \"3Ll\", \"0in\", \n\"1kc\", \"Xp\", \"61G\", \"5PM\", \"bTs\", \"7KB\", \"2T\", \"0Un\", \"8QF\", \"39T\", \"5f0\", \"4cp\", \"797\", \"aRm\", \"1s2\", \"02z\", \"0ys\", \"ZA\", \"63v\", \"766\", \n\"4lq\", \"5i1\", \"0e\", \"9Nf\", \"0Zo\", \"2oL\", \"6Tb\", \"4aA\", \"4Om\", \"6zN\", \"Wq\", \"00K\", \"Is\", \"0jA\", \"4Qo\", \"6dL\", \"7ZA\", \"5ob\", \"66\", \"25F\", \n\"jo\", \"9Pd\", \"4rs\", \"5w3\", \"aCn\", \"bfl\", \"0gq\", \"K\", \"KB\", \"0hp\", \"bim\", \"72T\", \"5x2\", \"49z\", \"327\", \"en\", \"3nn\", \"97\", \"44J\", \"6Ea\", \n\"6kM\", \"5NO\", \"11i\", \"Fr\", \"6WI\", \"4bj\", \"0YD\", \"zv\", \"TZ\", \"0wh\", \"4LF\", \"6ye\", \"5D6\", \"4Av\", \"0zX\", \"Yj\", \"3N\", \"0Tt\", \"4oZ\", \"6Zy\", \n\"412\", \"54r\", \"1KT\", \"xG\", \"Vk\", \"01Q\", \"4Nw\", \"5K7\", \"62l\", \"4CG\", \"0xi\", \"2MJ\", \"uw\", \"0VE\", \"4mk\", \"6XH\", \"47a\", \"6FJ\", \"ku\", \"0HG\", \n\"P8\", \"EY\", \"6hf\", \"5Md\", \"4Pu\", \"5U5\", \"Hi\", \"8bC\", \"0Ew\", \"fE\", \"4k8\", \"5nx\", \"45P\", \"4d9\", \"iD\", \"0Jv\", \"0dZ\", \"Gh\", \"5Z4\", \"5OU\", \n\"4RD\", \"6gg\", \"JX\", \"0ij\", \"0GF\", \"dt\", \"6IK\", \"5lI\", \"4Op\", \"5J0\", \"Wl\", \"00V\", \"0Zr\", \"2oQ\", \"405\", \"55u\", \"4ll\", \"6YO\", \"0x\", \"0WB\", \n\"0yn\", \"2LM\", \"63k\", \"5Ra\", \"4MA\", \"6xb\", \"2CL\", \"02g\", \"0XC\", \"39I\", \"6VN\", \"4cm\", \"bTn\", \"a5d\", \"2I\", \"0Us\", \"86O\", \"Xm\", \"5E1\", \"5PP\", \n\"6kP\", \"5NR\", \"11t\", \"Fo\", \"hC\", \"0Kq\", \"44W\", \"aon\", \"6HL\", \"49g\", \"0FA\", \"es\", \"3Mo\", \"0hm\", \"4SC\", \"72I\", \"6ia\", \"5Lc\", \"0gl\", \"V\", \n\"jr\", \"1Ya\", \"46f\", \"6GM\", \"hyV\", \"bEm\", \"0Dp\", \"gB\", \"In\", \"8cD\", \"4Qr\", \"5T2\", \"1b\", \"0VX\", \"4mv\", \"5h6\", \"62q\", \"4CZ\", \"0xt\", \"2MW\", \n\"Vv\", \"01L\", \"4Nj\", \"7kh\", \"6Ue\", \"54o\", \"1KI\", \"xZ\", \"3S\", \"0Ti\", \"4oG\", \"6Zd\", \"6tH\", \"4Ak\", \"0zE\", \"Yw\", \"TG\", \"0wu\", \"780\", \"6yx\", \n\"5g7\", \"4bw\", \"0YY\", \"zk\", \"1Wz\", \"di\", \"5y5\", \"5lT\", \"4RY\", \"4G8\", \"JE\", \"0iw\", \"0dG\", \"Gu\", \"6jJ\", \"5OH\", \"45M\", \"6Df\", \"iY\", \"80\", \n\"71\", \"fX\", \"6Kg\", \"5ne\", \"4Ph\", \"6eK\", \"Ht\", \"0kF\", \"0fv\", \"ED\", \"4H9\", \"5My\", \"4st\", \"5v4\", \"kh\", \"0HZ\", \"0Zv\", \"yD\", \"401\", \"4aX\", \n\"4Ot\", \"5J4\", \"Wh\", \"00R\", \"O9\", \"ZX\", \"63o\", \"4BD\", \"4lh\", \"6YK\", \"tt\", \"0WF\", \"0XG\", \"2md\", \"6VJ\", \"4ci\", \"4ME\", \"6xf\", \"UY\", \"02c\", \n\"1kz\", \"Xi\", \"5E5\", \"5PT\", \"4nY\", \"aqh\", \"2M\", \"0Uw\", \"hG\", \"0Ku\", \"44S\", \"6Ex\", \"6kT\", \"5NV\", \"0eY\", \"Fk\", \"3Mk\", \"0hi\", \"4SG\", \"6fd\", \n\"6HH\", \"49c\", \"0FE\", \"ew\", \"jv\", \"0ID\", \"46b\", \"6GI\", \"6ie\", \"5Lg\", \"0gh\", \"R\", \"Ij\", \"0jX\", \"4Qv\", \"5T6\", \"6Jy\", \"7O9\", \"0Dt\", \"gF\", \n\"62u\", \"775\", \"0xp\", \"198\", \"1f\", \"9Oe\", \"4mr\", \"5h2\", \"6Ua\", \"54k\", \"1KM\", \"2nO\", \"Vr\", \"01H\", \"4Nn\", \"7kl\", \"60D\", \"4Ao\", \"0zA\", \"Ys\", \n\"3W\", \"0Tm\", \"4oC\", \"7JA\", \"5g3\", \"4bs\", \"8PE\", \"zo\", \"TC\", \"03y\", \"784\", \"aSn\", \"bhn\", \"73W\", \"JA\", \"0is\", \"334\", \"dm\", \"5y1\", \"48y\", \n\"45I\", \"6Db\", \"3om\", \"84\", \"0dC\", \"Gq\", \"6jN\", \"5OL\", \"4Pl\", \"6eO\", \"Hp\", \"0kB\", \"75\", \"24E\", \"6Kc\", \"5na\", \"47x\", \"528\", \"kl\", \"8AF\", \n\"0fr\", \"1c2\", \"aBm\", \"bgo\", \"4ld\", \"6YG\", \"0p\", \"0WJ\", \"O5\", \"ZT\", \"63c\", \"4BH\", \"4Ox\", \"5J8\", \"Wd\", \"0tV\", \"0Zz\", \"yH\", \"4t5\", \"4aT\", \n\"4nU\", \"7KW\", \"2A\", \"1EZ\", \"1kv\", \"Xe\", \"5E9\", \"5PX\", \"4MI\", \"6xj\", \"UU\", \"02o\", \"0XK\", \"2mh\", \"6VF\", \"4ce\", \"6HD\", \"49o\", \"0FI\", \"27b\", \n\"KW\", \"0he\", \"4SK\", \"6fh\", \"6kX\", \"5NZ\", \"0eU\", \"Fg\", \"hK\", \"0Ky\", \"4pW\", \"4e6\", \"4j7\", \"5ow\", \"0Dx\", \"gJ\", \"If\", \"0jT\", \"4Qz\", \"6dY\", \n\"6ii\", \"5Lk\", \"Q7\", \"DV\", \"jz\", \"0IH\", \"46n\", \"6GE\", \"3PN\", \"01D\", \"4Nb\", \"aQS\", \"6Um\", \"54g\", \"m3\", \"xR\", \"1j\", \"0VP\", \"59W\", \"a6G\", \n\"4V3\", \"4CR\", \"85l\", \"194\", \"TO\", \"03u\", \"4LS\", \"4Y2\", \"a9F\", \"56V\", \"0YQ\", \"zc\", \"wS\", \"b2\", \"4oO\", \"6Zl\", \"60H\", \"4Ac\", \"0zM\", \"2On\", \n\"0dO\", \"2Ql\", \"6jB\", \"aU1\", \"45E\", \"6Dn\", \"iQ\", \"88\", \"0GS\", \"da\", \"acL\", \"48u\", \"4RQ\", \"4G0\", \"JM\", \"94N\", \"12W\", \"EL\", \"4H1\", \"5Mq\", \n\"47t\", \"524\", \"29y\", \"0HR\", \"79\", \"fP\", \"6Ko\", \"5nm\", \"aZ0\", \"6eC\", \"3NL\", \"0kN\", \"O1\", \"ZP\", \"63g\", \"4BL\", \"58I\", \"6YC\", \"0t\", \"0WN\", \n\"8Sf\", \"yL\", \"409\", \"4aP\", \"b1G\", \"aPM\", \"0a3\", \"00Z\", \"1kr\", \"Xa\", \"61V\", \"bzN\", \"4nQ\", \"7KS\", \"2E\", \"215\", \"0XO\", \"2ml\", \"6VB\", \"4ca\", \n\"4MM\", \"6xn\", \"UQ\", \"02k\", \"KS\", \"0ha\", \"4SO\", \"6fl\", \"7Xa\", \"49k\", \"0FM\", \"27f\", \"hO\", \"8Be\", \"4pS\", \"4e2\", \"aAN\", \"bdL\", \"0eQ\", \"Fc\", \n\"Ib\", \"0jP\", \"654\", \"70t\", \"4j3\", \"5os\", \"8Md\", \"gN\", \"28g\", \"0IL\", \"46j\", \"6GA\", \"6im\", \"5Lo\", \"Q3\", \"Z\", \"6Ui\", \"54c\", \"m7\", \"xV\", \n\"Vz\", \"0uH\", \"4Nf\", \"7kd\", \"4V7\", \"4CV\", \"0xx\", \"190\", \"1n\", \"0VT\", \"4mz\", \"6XY\", \"6WX\", \"56R\", \"0YU\", \"zg\", \"TK\", \"03q\", \"4LW\", \"4Y6\", \n\"60L\", \"4Ag\", \"0zI\", \"2Oj\", \"wW\", \"b6\", \"4oK\", \"6Zh\", \"45A\", \"6Dj\", \"iU\", \"0Jg\", \"0dK\", \"Gy\", \"6jF\", \"5OD\", \"4RU\", \"4G4\", \"JI\", \"1yZ\", \n\"0GW\", \"de\", \"5y9\", \"48q\", \"47p\", \"520\", \"kd\", \"0HV\", \"0fz\", \"EH\", \"4H5\", \"5Mu\", \"4Pd\", \"6eG\", \"Hx\", \"0kJ\", \"s5\", \"fT\", \"6Kk\", \"5ni\", \n\"5Y1\", \"5LP\", \"13v\", \"e\", \"jA\", \"0Is\", \"46U\", \"aml\", \"6JN\", \"5oL\", \"0DC\", \"gq\", \"3Om\", \"0jo\", \"4QA\", \"6db\", \"6kc\", \"5Na\", \"0en\", \"2PM\", \n\"hp\", \"0KB\", \"44d\", \"6EO\", \"abm\", \"49T\", \"0Fr\", \"1C2\", \"Kl\", \"8aF\", \"4Sp\", \"5V0\", \"4Mr\", \"5H2\", \"Un\", \"02T\", \"0Xp\", \"2mS\", \"427\", \"57w\", \n\"4nn\", \"7Kl\", \"2z\", \"1Ea\", \"1kM\", \"2NO\", \"61i\", \"5Pc\", \"4OC\", \"7jA\", \"2AN\", \"00e\", \"0ZA\", \"ys\", \"6TL\", \"4ao\", \"58v\", \"a7f\", \"0K\", \"0Wq\", \n\"84M\", \"Zo\", \"5G3\", \"4Bs\", \"0EY\", \"fk\", \"6KT\", \"5nV\", \"bjh\", \"6ex\", \"HG\", \"0ku\", \"0fE\", \"Ew\", \"6hH\", \"5MJ\", \"47O\", \"6Fd\", \"29B\", \"0Hi\", \n\"53\", \"dZ\", \"6Ie\", \"48N\", \"4Rj\", \"6gI\", \"Jv\", \"0iD\", \"0dt\", \"GF\", \"6jy\", \"7o9\", \"4qv\", \"5t6\", \"ij\", \"0JX\", \"wh\", \"0TZ\", \"4ot\", \"5j4\", \n\"4T9\", \"4AX\", \"0zv\", \"YD\", \"Tt\", \"03N\", \"4Lh\", \"6yK\", \"6Wg\", \"4bD\", \"o9\", \"zX\", \"1Q\", \"0Vk\", \"4mE\", \"6Xf\", \"62B\", \"4Ci\", \"0xG\", \"2Md\", \n\"VE\", \"0uw\", \"4NY\", \"aQh\", \"5e5\", \"5pT\", \"1Kz\", \"xi\", \"jE\", \"0Iw\", \"46Q\", \"4g8\", \"5Y5\", \"5LT\", \"13r\", \"a\", \"IY\", \"0jk\", \"4QE\", \"6df\", \n\"6JJ\", \"5oH\", \"0DG\", \"gu\", \"ht\", \"0KF\", \"4ph\", \"6EK\", \"6kg\", \"5Ne\", \"S9\", \"FX\", \"Kh\", \"0hZ\", \"4St\", \"5V4\", \"4h9\", \"49P\", \"0Fv\", \"eD\", \n\"0Xt\", \"2mW\", \"423\", \"4cZ\", \"4Mv\", \"5H6\", \"Uj\", \"02P\", \"1kI\", \"XZ\", \"61m\", \"5Pg\", \"4nj\", \"7Kh\", \"vv\", \"0UD\", \"0ZE\", \"yw\", \"6TH\", \"4ak\", \n\"4OG\", \"6zd\", \"2AJ\", \"00a\", \"0yY\", \"Zk\", \"5G7\", \"4Bw\", \"58r\", \"6Yx\", \"0O\", \"0Wu\", \"bjl\", \"71U\", \"HC\", \"0kq\", \"316\", \"fo\", \"6KP\", \"5nR\", \n\"47K\", \"7VA\", \"29F\", \"0Hm\", \"0fA\", \"Es\", \"6hL\", \"5MN\", \"4Rn\", \"6gM\", \"Jr\", \"1ya\", \"57\", \"26G\", \"6Ia\", \"48J\", \"45z\", \"5t2\", \"in\", \"8CD\", \n\"0dp\", \"GB\", \"hYV\", \"bem\", \"60w\", \"757\", \"0zr\", \"2OQ\", \"3d\", \"9Mg\", \"4op\", \"5j0\", \"6Wc\", \"56i\", \"0Yn\", \"2lM\", \"Tp\", \"03J\", \"4Ll\", \"6yO\", \n\"62F\", \"4Cm\", \"0xC\", \"dwS\", \"1U\", \"0Vo\", \"4mA\", \"6Xb\", \"5e1\", \"54X\", \"8RG\", \"xm\", \"VA\", \"0us\", \"b0f\", \"aQl\", \"6JF\", \"5oD\", \"0DK\", \"gy\", \n\"IU\", \"0jg\", \"4QI\", \"6dj\", \"5Y9\", \"5LX\", \"0gW\", \"m\", \"jI\", \"1YZ\", \"4rU\", \"4g4\", \"4h5\", \"5mu\", \"0Fz\", \"eH\", \"Kd\", \"0hV\", \"4Sx\", \"5V8\", \n\"6kk\", \"5Ni\", \"S5\", \"FT\", \"hx\", \"0KJ\", \"44l\", \"6EG\", \"4nf\", \"7Kd\", \"2r\", \"0UH\", \"M7\", \"XV\", \"61a\", \"5Pk\", \"4Mz\", \"6xY\", \"Uf\", \"0vT\", \n\"0Xx\", \"39r\", \"4v7\", \"4cV\", \"4lW\", \"4y6\", \"0C\", \"0Wy\", \"0yU\", \"Zg\", \"63P\", \"5RZ\", \"4OK\", \"6zh\", \"WW\", \"B6\", \"0ZI\", \"2oj\", \"6TD\", \"4ag\", \n\"0fM\", \"2Sn\", \"7xa\", \"5MB\", \"47G\", \"6Fl\", \"kS\", \"0Ha\", \"0EQ\", \"fc\", \"aaN\", \"bDL\", \"4PS\", \"4E2\", \"HO\", \"8be\", \"10U\", \"GN\", \"4J3\", \"5Os\", \n\"45v\", \"506\", \"ib\", \"0JP\", \"q3\", \"dR\", \"6Im\", \"48F\", \"4Rb\", \"6gA\", \"3LN\", \"0iL\", \"2Bm\", \"03F\", \"aF0\", \"6yC\", \"6Wo\", \"4bL\", \"o1\", \"zP\", \n\"3h\", \"0TR\", \"bUO\", \"a4E\", \"4T1\", \"4AP\", \"87n\", \"YL\", \"VM\", \"01w\", \"4NQ\", \"7kS\", \"hfu\", \"54T\", \"1Kr\", \"xa\", \"1Y\", \"0Vc\", \"4mM\", \"6Xn\", \n\"62J\", \"4Ca\", \"0xO\", \"2Ml\", \"IQ\", \"0jc\", \"4QM\", \"6dn\", \"6JB\", \"a19\", \"0DO\", \"25d\", \"jM\", \"9PF\", \"46Y\", \"4g0\", \"aCL\", \"687\", \"0gS\", \"i\", \n\"3MP\", \"0hR\", \"676\", \"72v\", \"4h1\", \"49X\", \"8Of\", \"eL\", \"3nL\", \"0KN\", \"44h\", \"6EC\", \"6ko\", \"5Nm\", \"S1\", \"FP\", \"M3\", \"XR\", \"61e\", \"5Po\", \n\"4nb\", \"aqS\", \"2v\", \"0UL\", \"8Qd\", \"39v\", \"4v3\", \"4cR\", \"b3E\", \"aRO\", \"Ub\", \"02X\", \"0yQ\", \"Zc\", \"63T\", \"bxL\", \"4lS\", \"4y2\", \"0G\", \"237\", \n\"0ZM\", \"2on\", \"7Da\", \"4ac\", \"4OO\", \"6zl\", \"WS\", \"B2\", \"47C\", \"6Fh\", \"kW\", \"0He\", \"0fI\", \"2Sj\", \"6hD\", \"5MF\", \"4PW\", \"4E6\", \"HK\", \"0ky\", \n\"0EU\", \"fg\", \"6KX\", \"5nZ\", \"45r\", \"502\", \"if\", \"0JT\", \"0dx\", \"GJ\", \"4J7\", \"5Ow\", \"4Rf\", \"6gE\", \"Jz\", \"0iH\", \"q7\", \"dV\", \"6Ii\", \"48B\", \n\"6Wk\", \"4bH\", \"o5\", \"zT\", \"Tx\", \"03B\", \"4Ld\", \"6yG\", \"4T5\", \"4AT\", \"0zz\", \"YH\", \"3l\", \"0TV\", \"4ox\", \"5j8\", \"5e9\", \"54P\", \"1Kv\", \"xe\", \n\"VI\", \"01s\", \"4NU\", \"7kW\", \"62N\", \"4Ce\", \"0xK\", \"2Mh\", \"uU\", \"0Vg\", \"4mI\", \"6Xj\", \"4K0\", \"5Np\", \"11V\", \"FM\", \"ha\", \"0KS\", \"44u\", \"515\", \n\"6Hn\", \"49E\", \"48\", \"eQ\", \"3MM\", \"0hO\", \"4Sa\", \"6fB\", \"6iC\", \"5LA\", \"0gN\", \"t\", \"jP\", \"0Ib\", \"46D\", \"6Go\", \"hyt\", \"bEO\", \"0DR\", \"0Q3\", \n\"IL\", \"8cf\", \"4QP\", \"4D1\", \"4OR\", \"4Z3\", \"WN\", \"00t\", \"0ZP\", \"yb\", \"hgv\", \"55W\", \"4lN\", \"6Ym\", \"0Z\", \"a3\", \"0yL\", \"2Lo\", \"63I\", \"4Bb\", \n\"4Mc\", \"7ha\", \"2Cn\", \"02E\", \"n2\", \"2mB\", \"6Vl\", \"4cO\", \"bTL\", \"a5F\", \"2k\", \"0UQ\", \"86m\", \"XO\", \"4U2\", \"5Pr\", \"0Gy\", \"dK\", \"4i6\", \"5lv\", \n\"5BZ\", \"6gX\", \"Jg\", \"0iU\", \"R6\", \"GW\", \"6jh\", \"5Oj\", \"45o\", \"6DD\", \"3oK\", \"0JI\", \"0EH\", \"fz\", \"6KE\", \"5nG\", \"4PJ\", \"6ei\", \"HV\", \"0kd\", \n\"0fT\", \"Ef\", \"6hY\", \"690\", \"4sV\", \"4f7\", \"kJ\", \"0Hx\", \"uH\", \"0Vz\", \"4mT\", \"4x5\", \"5F8\", \"4Cx\", \"0xV\", \"0m7\", \"VT\", \"C5\", \"4NH\", \"7kJ\", \n\"6UG\", \"54M\", \"1Kk\", \"xx\", \"3q\", \"0TK\", \"4oe\", \"6ZF\", \"60b\", \"4AI\", \"L4\", \"YU\", \"Te\", \"0wW\", \"4Ly\", \"5I9\", \"4w4\", \"4bU\", \"1IZ\", \"zI\", \n\"he\", \"0KW\", \"44q\", \"511\", \"4K4\", \"5Nt\", \"11R\", \"FI\", \"Ky\", \"0hK\", \"4Se\", \"6fF\", \"6Hj\", \"49A\", \"p4\", \"eU\", \"jT\", \"0If\", \"4rH\", \"6Gk\", \n\"6iG\", \"5LE\", \"0gJ\", \"p\", \"IH\", \"0jz\", \"4QT\", \"4D5\", \"5z8\", \"5oY\", \"0DV\", \"gd\", \"0ZT\", \"yf\", \"6TY\", \"4az\", \"4OV\", \"4Z7\", \"WJ\", \"00p\", \n\"0yH\", \"Zz\", \"63M\", \"4Bf\", \"4lJ\", \"6Yi\", \"tV\", \"a7\", \"n6\", \"2mF\", \"6Vh\", \"4cK\", \"4Mg\", \"6xD\", \"2Cj\", \"02A\", \"1kX\", \"XK\", \"4U6\", \"5Pv\", \n\"6N9\", \"7Ky\", \"2o\", \"0UU\", \"665\", \"73u\", \"Jc\", \"0iQ\", \"8Ne\", \"dO\", \"4i2\", \"5lr\", \"45k\", \"7Ta\", \"3oO\", \"0JM\", \"R2\", \"GS\", \"6jl\", \"5On\", \n\"4PN\", \"6em\", \"HR\", \"8bx\", \"0EL\", \"24g\", \"6KA\", \"5nC\", \"47Z\", \"4f3\", \"kN\", \"8Ad\", \"0fP\", \"Eb\", \"aBO\", \"694\", \"62W\", \"byO\", \"0xR\", \"0m3\", \n\"1D\", \"224\", \"4mP\", \"4x1\", \"6UC\", \"54I\", \"1Ko\", \"2nm\", \"VP\", \"C1\", \"4NL\", \"7kN\", \"60f\", \"4AM\", \"L0\", \"YQ\", \"3u\", \"0TO\", \"4oa\", \"6ZB\", \n\"438\", \"4bQ\", \"8Pg\", \"zM\", \"Ta\", \"0wS\", \"b2F\", \"aSL\", \"6Hf\", \"49M\", \"40\", \"eY\", \"Ku\", \"0hG\", \"4Si\", \"6fJ\", \"4K8\", \"5Nx\", \"0ew\", \"FE\", \n\"hi\", \"8BC\", \"4pu\", \"5u5\", \"5z4\", \"5oU\", \"0DZ\", \"gh\", \"ID\", \"0jv\", \"4QX\", \"4D9\", \"6iK\", \"5LI\", \"0gF\", \"Dt\", \"jX\", \"0Ij\", \"46L\", \"6Gg\", \n\"4lF\", \"6Ye\", \"0R\", \"0Wh\", \"0yD\", \"Zv\", \"63A\", \"4Bj\", \"4OZ\", \"6zy\", \"WF\", \"0tt\", \"0ZX\", \"yj\", \"5d6\", \"4av\", \"4nw\", \"5k7\", \"2c\", \"0UY\", \n\"1kT\", \"XG\", \"61p\", \"5Pz\", \"4Mk\", \"6xH\", \"Uw\", \"02M\", \"0Xi\", \"2mJ\", \"6Vd\", \"4cG\", \"0dm\", \"2QN\", \"7zA\", \"5Ob\", \"45g\", \"6DL\", \"is\", \"0JA\", \n\"0Gq\", \"dC\", \"acn\", \"48W\", \"4Rs\", \"5W3\", \"Jo\", \"94l\", \"12u\", \"En\", \"5X2\", \"5MS\", \"47V\", \"alo\", \"kB\", \"0Hp\", \"1Ua\", \"fr\", \"6KM\", \"5nO\", \n\"4PB\", \"6ea\", \"3Nn\", \"0kl\", \"3Pl\", \"01f\", \"bts\", \"7kB\", \"6UO\", \"54E\", \"1Kc\", \"xp\", \"1H\", \"0Vr\", \"59u\", \"a6e\", \"5F0\", \"4Cp\", \"85N\", \"d3F\", \n\"Tm\", \"03W\", \"4Lq\", \"5I1\", \"434\", \"56t\", \"0Ys\", \"zA\", \"3y\", \"0TC\", \"4om\", \"6ZN\", \"60j\", \"4AA\", \"0zo\", \"2OL\", \"Kq\", \"0hC\", \"4Sm\", \"6fN\", \n\"6Hb\", \"49I\", \"44\", \"27D\", \"hm\", \"8BG\", \"44y\", \"519\", \"aAl\", \"bdn\", \"0es\", \"FA\", \"1o2\", \"0jr\", \"bko\", \"70V\", \"5z0\", \"5oQ\", \"305\", \"gl\", \n\"28E\", \"0In\", \"46H\", \"6Gc\", \"6iO\", \"5LM\", \"0gB\", \"x\", \"1ia\", \"Zr\", \"63E\", \"4Bn\", \"4lB\", \"6Ya\", \"0V\", \"0Wl\", \"8SD\", \"yn\", \"5d2\", \"4ar\", \n\"b1e\", \"aPo\", \"WB\", \"00x\", \"1kP\", \"XC\", \"61t\", \"744\", \"4ns\", \"5k3\", \"2g\", \"9Ld\", \"0Xm\", \"2mN\", \"7FA\", \"4cC\", \"4Mo\", \"6xL\", \"Us\", \"02I\", \n\"45c\", \"6DH\", \"iw\", \"0JE\", \"0di\", \"2QJ\", \"6jd\", \"5Of\", \"4Rw\", \"5W7\", \"Jk\", \"0iY\", \"0Gu\", \"dG\", \"6Ix\", \"48S\", \"47R\", \"6Fy\", \"kF\", \"0Ht\", \n\"0fX\", \"Ej\", \"5X6\", \"5MW\", \"4PF\", \"6ee\", \"HZ\", \"0kh\", \"0ED\", \"fv\", \"6KI\", \"5nK\", \"6UK\", \"54A\", \"1Kg\", \"xt\", \"VX\", \"C9\", \"4ND\", \"7kF\", \n\"5F4\", \"4Ct\", \"0xZ\", \"2My\", \"1L\", \"0Vv\", \"4mX\", \"4x9\", \"430\", \"4bY\", \"0Yw\", \"zE\", \"Ti\", \"03S\", \"4Lu\", \"5I5\", \"60n\", \"4AE\", \"L8\", \"YY\", \n\"wu\", \"0TG\", \"4oi\", \"6ZJ\" };\n\n\n#endif\n\n"
  },
  {
    "path": "src/crc64.c",
    "content": "/* Copyright (c) 2014, Matt Stancliff <matt@genges.com>\n * Copyright (c) 2020, Amazon Web Services\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE. */\n\n#include \"crc64.h\"\n#include \"crcspeed.h\"\nstatic uint64_t crc64_table[8][256] = {{0}};\n\n#define POLY UINT64_C(0xad93d23594c935a9)\n/******************** BEGIN GENERATED PYCRC FUNCTIONS ********************/\n/**\n * Generated on Sun Dec 21 14:14:07 2014,\n * by pycrc v0.8.2, https://www.tty1.net/pycrc/\n *\n * LICENSE ON GENERATED CODE:\n * ==========================\n * As of version 0.6, pycrc is released under the terms of the MIT licence.\n * The code generated by pycrc is not considered a substantial portion of the\n * software, therefore the author of pycrc will not claim any copyright on\n * the generated code.\n * ==========================\n *\n * CRC configuration:\n *    Width        = 64\n *    Poly         = 0xad93d23594c935a9\n *    XorIn        = 0xffffffffffffffff\n *    ReflectIn    = True\n *    XorOut       = 0x0000000000000000\n *    ReflectOut   = True\n *    Algorithm    = bit-by-bit-fast\n *\n * Modifications after generation (by matt):\n *   - included finalize step in-line with update for single-call generation\n *   - re-worked some inner variable architectures\n *   - adjusted function parameters to match expected prototypes.\n *****************************************************************************/\n\n/**\n * Reflect all bits of a \\a data word of \\a data_len bytes.\n *\n * \\param data         The data word to be reflected.\n * \\param data_len     The width of \\a data expressed in number of bits.\n * \\return             The reflected data.\n *****************************************************************************/\nstatic inline uint_fast64_t crc_reflect(uint_fast64_t data, size_t data_len) {\n    uint_fast64_t ret = data & 0x01;\n\n    for (size_t i = 1; i < data_len; i++) {\n        data >>= 1;\n        ret = (ret << 1) | (data & 0x01);\n    }\n\n    return ret;\n}\n\n/**\n *  Update the crc value with new data.\n *\n * \\param crc      The current crc value.\n * \\param data     Pointer to a buffer of \\a data_len bytes.\n * \\param data_len Number of bytes in the \\a data buffer.\n * \\return         The updated crc value.\n ******************************************************************************/\nuint64_t _crc64(uint_fast64_t crc, const void *in_data, const uint64_t len) {\n    const uint8_t *data = in_data;\n    unsigned long long bit;\n\n    for (uint64_t offset = 0; offset < len; offset++) {\n        uint8_t c = data[offset];\n        for (uint_fast8_t i = 0x01; i & 0xff; i <<= 1) {\n            bit = crc & 0x8000000000000000;\n            if (c & i) {\n                bit = !bit;\n            }\n\n            crc <<= 1;\n            if (bit) {\n                crc ^= POLY;\n            }\n        }\n\n        crc &= 0xffffffffffffffff;\n    }\n\n    crc = crc & 0xffffffffffffffff;\n    return crc_reflect(crc, 64) ^ 0x0000000000000000;\n}\n\n/******************** END GENERATED PYCRC FUNCTIONS ********************/\n\n/* Initializes the 16KB lookup tables. */\nvoid crc64_init(void) {\n    crcspeed64native_init(_crc64, crc64_table);\n}\n\n/* Compute crc64 */\nuint64_t crc64(uint64_t crc, const unsigned char *s, uint64_t l) {\n    return crcspeed64native(crc64_table, crc, (void *) s, l);\n}\n\n/* Test main */\n#ifdef REDIS_TEST\n#include <stdio.h>\n\n#define UNUSED(x) (void)(x)\nint crc64Test(int argc, char *argv[], int accurate) {\n    UNUSED(argc);\n    UNUSED(argv);\n    UNUSED(accurate);\n    crc64_init();\n    printf(\"[calcula]: e9c6d914c4b8d9ca == %016\" PRIx64 \"\\n\",\n           (uint64_t)_crc64(0, \"123456789\", 9));\n    printf(\"[64speed]: e9c6d914c4b8d9ca == %016\" PRIx64 \"\\n\",\n           (uint64_t)crc64(0, (unsigned char*)\"123456789\", 9));\n    char li[] = \"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed \"\n                \"do eiusmod tempor incididunt ut labore et dolore magna \"\n                \"aliqua. Ut enim ad minim veniam, quis nostrud exercitation \"\n                \"ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis \"\n                \"aute irure dolor in reprehenderit in voluptate velit esse \"\n                \"cillum dolore eu fugiat nulla pariatur. Excepteur sint \"\n                \"occaecat cupidatat non proident, sunt in culpa qui officia \"\n                \"deserunt mollit anim id est laborum.\";\n    printf(\"[calcula]: c7794709e69683b3 == %016\" PRIx64 \"\\n\",\n           (uint64_t)_crc64(0, li, sizeof(li)));\n    printf(\"[64speed]: c7794709e69683b3 == %016\" PRIx64 \"\\n\",\n           (uint64_t)crc64(0, (unsigned char*)li, sizeof(li)));\n    return 0;\n}\n\n#endif\n\n#ifdef REDIS_TEST_MAIN\nint main(int argc, char *argv[]) {\n    return crc64Test(argc, argv);\n}\n\n#endif\n"
  },
  {
    "path": "src/crc64.h",
    "content": "#ifndef CRC64_H\n#define CRC64_H\n\n#include <stdint.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\nvoid crc64_init(void);\nuint64_t crc64(uint64_t crc, const unsigned char *s, uint64_t l);\n\n#ifdef REDIS_TEST\nint crc64Test(int argc, char *argv[], int accurate);\n#endif\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n"
  },
  {
    "path": "src/crcspeed.c",
    "content": "/*\n * Copyright (C) 2013 Mark Adler\n * Originally by: crc64.c Version 1.4  16 Dec 2013  Mark Adler\n * Modifications by Matt Stancliff <matt@genges.com>:\n *   - removed CRC64-specific behavior\n *   - added generation of lookup tables by parameters\n *   - removed inversion of CRC input/result\n *   - removed automatic initialization in favor of explicit initialization\n\n  This software is provided 'as-is', without any express or implied\n  warranty.  In no event will the author be held liable for any damages\n  arising from the use of this software.\n\n  Permission is granted to anyone to use this software for any purpose,\n  including commercial applications, and to alter it and redistribute it\n  freely, subject to the following restrictions:\n\n  1. The origin of this software must not be misrepresented; you must not\n     claim that you wrote the original software. If you use this software\n     in a product, an acknowledgment in the product documentation would be\n     appreciated but is not required.\n  2. Altered source versions must be plainly marked as such, and must not be\n     misrepresented as being the original software.\n  3. This notice may not be removed or altered from any source distribution.\n\n  Mark Adler\n  madler@alumni.caltech.edu\n */\n\n#include \"crcspeed.h\"\n\n/* Fill in a CRC constants table. */\nvoid crcspeed64little_init(crcfn64 crcfn, uint64_t table[8][256]) {\n    uint64_t crc;\n\n    /* generate CRCs for all single byte sequences */\n    for (int n = 0; n < 256; n++) {\n        unsigned char v = n;\n        table[0][n] = crcfn(0, &v, 1);\n    }\n\n    /* generate nested CRC table for future slice-by-8 lookup */\n    for (int n = 0; n < 256; n++) {\n        crc = table[0][n];\n        for (int k = 1; k < 8; k++) {\n            crc = table[0][crc & 0xff] ^ (crc >> 8);\n            table[k][n] = crc;\n        }\n    }\n}\n\nvoid crcspeed16little_init(crcfn16 crcfn, uint16_t table[8][256]) {\n    uint16_t crc;\n\n    /* generate CRCs for all single byte sequences */\n    for (int n = 0; n < 256; n++) {\n        table[0][n] = crcfn(0, &n, 1);\n    }\n\n    /* generate nested CRC table for future slice-by-8 lookup */\n    for (int n = 0; n < 256; n++) {\n        crc = table[0][n];\n        for (int k = 1; k < 8; k++) {\n            crc = table[0][(crc >> 8) & 0xff] ^ (crc << 8);\n            table[k][n] = crc;\n        }\n    }\n}\n\n/* Reverse the bytes in a 64-bit word. */\nstatic inline uint64_t rev8(uint64_t a) {\n#if defined(__GNUC__) || defined(__clang__)\n    return __builtin_bswap64(a);\n#else\n    uint64_t m;\n\n    m = UINT64_C(0xff00ff00ff00ff);\n    a = ((a >> 8) & m) | (a & m) << 8;\n    m = UINT64_C(0xffff0000ffff);\n    a = ((a >> 16) & m) | (a & m) << 16;\n    return a >> 32 | a << 32;\n#endif\n}\n\n/* This function is called once to initialize the CRC table for use on a\n   big-endian architecture. */\nvoid crcspeed64big_init(crcfn64 fn, uint64_t big_table[8][256]) {\n    /* Create the little endian table then reverse all the entries. */\n    crcspeed64little_init(fn, big_table);\n    for (int k = 0; k < 8; k++) {\n        for (int n = 0; n < 256; n++) {\n            big_table[k][n] = rev8(big_table[k][n]);\n        }\n    }\n}\n\nvoid crcspeed16big_init(crcfn16 fn, uint16_t big_table[8][256]) {\n    /* Create the little endian table then reverse all the entries. */\n    crcspeed16little_init(fn, big_table);\n    for (int k = 0; k < 8; k++) {\n        for (int n = 0; n < 256; n++) {\n            big_table[k][n] = rev8(big_table[k][n]);\n        }\n    }\n}\n\n/* Calculate a non-inverted CRC multiple bytes at a time on a little-endian\n * architecture. If you need inverted CRC, invert *before* calling and invert\n * *after* calling.\n * 64 bit crc = process 8 bytes at once;\n */\nuint64_t crcspeed64little(uint64_t little_table[8][256], uint64_t crc,\n                          void *buf, size_t len) {\n    unsigned char *next = buf;\n\n    /* process individual bytes until we reach an 8-byte aligned pointer */\n    while (len && ((uintptr_t)next & 7) != 0) {\n        crc = little_table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8);\n        len--;\n    }\n\n    /* fast middle processing, 8 bytes (aligned!) per loop */\n    while (len >= 8) {\n        crc ^= *(uint64_t *)next;\n        crc = little_table[7][crc & 0xff] ^\n              little_table[6][(crc >> 8) & 0xff] ^\n              little_table[5][(crc >> 16) & 0xff] ^\n              little_table[4][(crc >> 24) & 0xff] ^\n              little_table[3][(crc >> 32) & 0xff] ^\n              little_table[2][(crc >> 40) & 0xff] ^\n              little_table[1][(crc >> 48) & 0xff] ^\n              little_table[0][crc >> 56];\n        next += 8;\n        len -= 8;\n    }\n\n    /* process remaining bytes (can't be larger than 8) */\n    while (len) {\n        crc = little_table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8);\n        len--;\n    }\n\n    return crc;\n}\n\nuint16_t crcspeed16little(uint16_t little_table[8][256], uint16_t crc,\n                          void *buf, size_t len) {\n    unsigned char *next = buf;\n\n    /* process individual bytes until we reach an 8-byte aligned pointer */\n    while (len && ((uintptr_t)next & 7) != 0) {\n        crc = little_table[0][((crc >> 8) ^ *next++) & 0xff] ^ (crc << 8);\n        len--;\n    }\n\n    /* fast middle processing, 8 bytes (aligned!) per loop */\n    while (len >= 8) {\n        uint64_t n = *(uint64_t *)next;\n        crc = little_table[7][(n & 0xff) ^ ((crc >> 8) & 0xff)] ^\n              little_table[6][((n >> 8) & 0xff) ^ (crc & 0xff)] ^\n              little_table[5][(n >> 16) & 0xff] ^\n              little_table[4][(n >> 24) & 0xff] ^\n              little_table[3][(n >> 32) & 0xff] ^\n              little_table[2][(n >> 40) & 0xff] ^\n              little_table[1][(n >> 48) & 0xff] ^\n              little_table[0][n >> 56];\n        next += 8;\n        len -= 8;\n    }\n\n    /* process remaining bytes (can't be larger than 8) */\n    while (len) {\n        crc = little_table[0][((crc >> 8) ^ *next++) & 0xff] ^ (crc << 8);\n        len--;\n    }\n\n    return crc;\n}\n\n/* Calculate a non-inverted CRC eight bytes at a time on a big-endian\n * architecture.\n */\nuint64_t crcspeed64big(uint64_t big_table[8][256], uint64_t crc, void *buf,\n                       size_t len) {\n    unsigned char *next = buf;\n\n    crc = rev8(crc);\n    while (len && ((uintptr_t)next & 7) != 0) {\n        crc = big_table[0][(crc >> 56) ^ *next++] ^ (crc << 8);\n        len--;\n    }\n\n    while (len >= 8) {\n        crc ^= *(uint64_t *)next;\n        crc = big_table[0][crc & 0xff] ^\n              big_table[1][(crc >> 8) & 0xff] ^\n              big_table[2][(crc >> 16) & 0xff] ^\n              big_table[3][(crc >> 24) & 0xff] ^\n              big_table[4][(crc >> 32) & 0xff] ^\n              big_table[5][(crc >> 40) & 0xff] ^\n              big_table[6][(crc >> 48) & 0xff] ^\n              big_table[7][crc >> 56];\n        next += 8;\n        len -= 8;\n    }\n\n    while (len) {\n        crc = big_table[0][(crc >> 56) ^ *next++] ^ (crc << 8);\n        len--;\n    }\n\n    return rev8(crc);\n}\n\n/* WARNING: Completely untested on big endian architecture.  Possibly broken. */\nuint16_t crcspeed16big(uint16_t big_table[8][256], uint16_t crc_in, void *buf,\n                       size_t len) {\n    unsigned char *next = buf;\n    uint64_t crc = crc_in;\n\n    crc = rev8(crc);\n    while (len && ((uintptr_t)next & 7) != 0) {\n        crc = big_table[0][((crc >> (56 - 8)) ^ *next++) & 0xff] ^ (crc >> 8);\n        len--;\n    }\n\n    while (len >= 8) {\n        uint64_t n = *(uint64_t *)next;\n        crc = big_table[0][(n & 0xff) ^ ((crc >> (56 - 8)) & 0xff)] ^\n              big_table[1][((n >> 8) & 0xff) ^ (crc & 0xff)] ^\n              big_table[2][(n >> 16) & 0xff] ^\n              big_table[3][(n >> 24) & 0xff] ^\n              big_table[4][(n >> 32) & 0xff] ^\n              big_table[5][(n >> 40) & 0xff] ^\n              big_table[6][(n >> 48) & 0xff] ^\n              big_table[7][n >> 56];\n        next += 8;\n        len -= 8;\n    }\n\n    while (len) {\n        crc = big_table[0][((crc >> (56 - 8)) ^ *next++) & 0xff] ^ (crc >> 8);\n        len--;\n    }\n\n    return rev8(crc);\n}\n\n/* Return the CRC of buf[0..len-1] with initial crc, processing eight bytes\n   at a time using passed-in lookup table.\n   This selects one of two routines depending on the endianess of\n   the architecture. */\nuint64_t crcspeed64native(uint64_t table[8][256], uint64_t crc, void *buf,\n                          size_t len) {\n    uint64_t n = 1;\n\n    return *(char *)&n ? crcspeed64little(table, crc, buf, len)\n                       : crcspeed64big(table, crc, buf, len);\n}\n\nuint16_t crcspeed16native(uint16_t table[8][256], uint16_t crc, void *buf,\n                          size_t len) {\n    uint64_t n = 1;\n\n    return *(char *)&n ? crcspeed16little(table, crc, buf, len)\n                       : crcspeed16big(table, crc, buf, len);\n}\n\n/* Initialize CRC lookup table in architecture-dependent manner. */\nvoid crcspeed64native_init(crcfn64 fn, uint64_t table[8][256]) {\n    uint64_t n = 1;\n\n    *(char *)&n ? crcspeed64little_init(fn, table)\n                : crcspeed64big_init(fn, table);\n}\n\nvoid crcspeed16native_init(crcfn16 fn, uint16_t table[8][256]) {\n    uint64_t n = 1;\n\n    *(char *)&n ? crcspeed16little_init(fn, table)\n                : crcspeed16big_init(fn, table);\n}\n"
  },
  {
    "path": "src/crcspeed.h",
    "content": "/* Copyright (c) 2014, Matt Stancliff <matt@genges.com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE. */\n\n#ifndef CRCSPEED_H\n#define CRCSPEED_H\n\n#include <inttypes.h>\n#include <stdio.h>\n\ntypedef uint64_t (*crcfn64)(uint64_t, const void *, const uint64_t);\ntypedef uint16_t (*crcfn16)(uint16_t, const void *, const uint64_t);\n\n/* CRC-64 */\nvoid crcspeed64little_init(crcfn64 fn, uint64_t table[8][256]);\nvoid crcspeed64big_init(crcfn64 fn, uint64_t table[8][256]);\nvoid crcspeed64native_init(crcfn64 fn, uint64_t table[8][256]);\n\nuint64_t crcspeed64little(uint64_t table[8][256], uint64_t crc, void *buf,\n                          size_t len);\nuint64_t crcspeed64big(uint64_t table[8][256], uint64_t crc, void *buf,\n                       size_t len);\nuint64_t crcspeed64native(uint64_t table[8][256], uint64_t crc, void *buf,\n                          size_t len);\n\n/* CRC-16 */\nvoid crcspeed16little_init(crcfn16 fn, uint16_t table[8][256]);\nvoid crcspeed16big_init(crcfn16 fn, uint16_t table[8][256]);\nvoid crcspeed16native_init(crcfn16 fn, uint16_t table[8][256]);\n\nuint16_t crcspeed16little(uint16_t table[8][256], uint16_t crc, void *buf,\n                          size_t len);\nuint16_t crcspeed16big(uint16_t table[8][256], uint16_t crc, void *buf,\n                       size_t len);\nuint16_t crcspeed16native(uint16_t table[8][256], uint16_t crc, void *buf,\n                          size_t len);\n#endif\n"
  },
  {
    "path": "src/cron.cpp",
    "content": "#include \"server.h\"\n#include \"cron.h\"\n\nvoid freeCronObject(robj_roptr o)\n{\n    delete reinterpret_cast<const cronjob*>(ptrFromObj(o));\n}\n\n// CRON [name] [single shot] [optional: start] [delay] [script] [numkeys] [key N] [arg N]\nvoid cronCommand(client *c)\n{\n    int arg_offset = 0;\n    static const int ARG_NAME = 1;\n    static const int ARG_SINGLESHOT = 2;\n    static const int ARG_EXPIRE = 3;\n    #define ARG_SCRIPT (4+arg_offset)\n    #define ARG_NUMKEYS (5+arg_offset)\n    #define ARG_KEYSTART (6+arg_offset)\n\n    bool fSingleShot = false;\n    if (strcasecmp(\"single\", szFromObj(c->argv[ARG_SINGLESHOT])) == 0) {\n        fSingleShot = true;\n    } else {\n        if (strcasecmp(\"repeat\", szFromObj(c->argv[ARG_SINGLESHOT])) != 0) {\n            addReply(c, shared.syntaxerr);\n            return;\n        }\n    }\n\n    long long interval;\n    if (getLongLongFromObjectOrReply(c, c->argv[ARG_EXPIRE], &interval, \"missing expire time\") != C_OK)\n        return;\n\n    long long base;\n    __atomic_load(&g_pserver->mstime, &base, __ATOMIC_ACQUIRE);\n    if (getLongLongFromObject(c->argv[ARG_EXPIRE+1], &base) == C_OK) {\n        arg_offset++;\n        std::swap(base, interval);\n    }\n\n    if (interval <= 0)\n    {\n        addReplyError(c, \"interval must be positive\");\n        return;\n    }\n\n    long numkeys = 0;\n    if (c->argc > ARG_NUMKEYS)\n    {\n        if (getLongFromObjectOrReply(c, c->argv[ARG_NUMKEYS], &numkeys, NULL) != C_OK)\n            return;\n        \n        if (c->argc < (6 + numkeys)) {\n            addReplyError(c, \"Missing arguments or numkeys is too big\");\n            return;\n        }\n    }\n\n    std::unique_ptr<cronjob> spjob = std::make_unique<cronjob>();\n    spjob->script = sdsstring(sdsdup(szFromObj(c->argv[ARG_SCRIPT])));\n    spjob->interval = (uint64_t)interval;\n    spjob->startTime = (uint64_t)base;\n    spjob->fSingleShot = fSingleShot;\n    spjob->dbNum = dbnumFromDb(c->db);\n    for (long i = 0; i < numkeys; ++i)\n        spjob->veckeys.emplace_back(sdsdup(szFromObj(c->argv[ARG_KEYSTART+i])));\n    for (long i = ARG_KEYSTART + numkeys; i < c->argc; ++i)\n        spjob->vecargs.emplace_back(sdsdup(szFromObj(c->argv[i])));\n\n    robj *o = createObject(OBJ_CRON, spjob.release());\n    setKey(c, c->db, c->argv[ARG_NAME], o);\n    decrRefCount(o);\n    // use an expire to trigger execution.  Note: We use a subkey expire here so legacy clients don't delete it.\n    setExpire(c, c->db, c->argv[ARG_NAME], c->argv[ARG_NAME], base + interval);\n    ++g_pserver->dirty;\n    addReply(c, shared.ok);\n}\n\nvoid executeCronJobExpireHook(const char *key, robj *o)\n{\n    serverAssert(o->type == OBJ_CRON);\n    cronjob *job = (cronjob*)ptrFromObj(o);\n    \n    client *cFake = createClient(nullptr, IDX_EVENT_LOOP_MAIN);\n    cFake->lock.lock();\n    cFake->authenticated = 1;\n    cFake->user = nullptr;\n    selectDb(cFake, job->dbNum);\n    serverAssert(cFake->argc == 0);\n\n    // Setup the args for the EVAL command\n    cFake->cmd = lookupCommandByCString(\"EVAL\");\n    cFake->argc = 3 + job->veckeys.size() + job->vecargs.size();\n    cFake->argv = (robj**)zmalloc(sizeof(robj*) * cFake->argc, MALLOC_LOCAL);\n    cFake->argv[0] = createStringObject(\"EVAL\", 4);\n    cFake->argv[1] = createStringObject(job->script.get(), job->script.size());\n    cFake->argv[2] = createStringObjectFromLongLong(job->veckeys.size());\n    for (size_t i = 0; i < job->veckeys.size(); ++i)\n        cFake->argv[3+i] = createStringObject(job->veckeys[i].get(), job->veckeys[i].size());\n    for (size_t i = 0; i < job->vecargs.size(); ++i)\n        cFake->argv[3+job->veckeys.size()+i] = createStringObject(job->vecargs[i].get(), job->vecargs[i].size());\n\n    int lua_replicate_backup = g_pserver->lua_always_replicate_commands;\n    g_pserver->lua_always_replicate_commands = 0;\n    evalCommand(cFake);\n    g_pserver->lua_always_replicate_commands = lua_replicate_backup;\n\n    if (g_pserver->aof_state != AOF_OFF)\n        feedAppendOnlyFile(cFake->cmd,cFake->db->id,cFake->argv,cFake->argc);\n    // Active replicas do their own expiries, do not propogate\n    if (!g_pserver->fActiveReplica)\n        replicationFeedSlaves(g_pserver->slaves,cFake->db->id,cFake->argv,cFake->argc);\n\n    resetClient(cFake);\n\n    robj *keyobj = createStringObject(key,sdslen(key));\n    int dbId = job->dbNum;\n    if (job->fSingleShot)\n    {\n        serverAssert(dbSyncDelete(cFake->db, keyobj));\n    }\n    else\n    {\n        job->startTime += job->interval;\n        mstime_t mstime;\n        __atomic_load(&g_pserver->mstime, &mstime, __ATOMIC_ACQUIRE);\n        if (job->startTime < (uint64_t)mstime)\n        {\n            // If we are more than one interval in the past then fast forward to\n            //  the first interval still in the future.  If startTime wasn't zero align\n            //  this to the original startTime, if it was zero align to now\n            if (job->startTime == job->interval)\n            {   // startTime was 0\n                job->startTime = mstime + job->interval;\n            }\n            else\n            {\n                auto delta = mstime - job->startTime;\n                auto multiple = (delta / job->interval)+1;\n                job->startTime += job->interval * multiple;\n            }\n        }\n        setExpire(cFake, cFake->db, keyobj, keyobj, job->startTime);\n    }\n\n    notifyKeyspaceEvent(NOTIFY_KEYEVENT, \"CRON Executed\", keyobj, dbId);\n    decrRefCount(keyobj);\n\n    // o is invalid at this point\n    freeClient(cFake);\n}"
  },
  {
    "path": "src/cron.h",
    "content": "#pragma once\n\nstruct cronjob\n{\n    sdsstring script;\n    uint64_t interval;\n    uint64_t startTime;\n    std::vector<sdsstring> veckeys;\n    std::vector<sdsstring> vecargs;\n    int dbNum = 0;\n    bool fSingleShot = false;\n};\n\nvoid freeCronObject(robj_roptr o);\nvoid executeCronJobExpireHook(const char *key, robj *o);\nvoid cronCommand(client *c);"
  },
  {
    "path": "src/db.cpp",
    "content": "/*\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n#include \"cluster.h\"\n#include \"atomicvar.h\"\n#include \"aelocker.h\"\n#include \"latency.h\"\n\n#include <signal.h>\n#include <ctype.h>\n\n// Needed for prefetch\n#if defined(__x86_64__) || defined(__i386__)\n#include <xmmintrin.h>\n#endif\n\n/* Database backup. */\nstruct dbBackup {\n    const redisDbPersistentDataSnapshot **dbarray;\n    rax *slots_to_keys;\n    uint64_t slots_keys_count[CLUSTER_SLOTS];\n};\n\n/*-----------------------------------------------------------------------------\n * C-level DB API\n *----------------------------------------------------------------------------*/\n\nint expireIfNeeded(redisDb *db, robj *key, robj *o);\nvoid slotToKeyUpdateKeyCore(const char *key, size_t keylen, int add);\n\nstd::unique_ptr<expireEntry> deserializeExpire(const char *str, size_t cch, size_t *poffset);\n\ndictType dictChangeDescType {\n    dictSdsHash,                /* hash function */\n    NULL,                       /* key dup */\n    NULL,                       /* val dup */\n    dictSdsKeyCompare,          /* key compare */\n    dictSdsDestructor,          /* key destructor */\n    nullptr                     /* val destructor */\n};\n\n/* Update LFU when an object is accessed.\n * Firstly, decrement the counter if the decrement time is reached.\n * Then logarithmically increment the counter, and update the access time. */\nvoid updateLFU(robj *val) {\n    unsigned long counter = LFUDecrAndReturn(val);\n    counter = LFULogIncr(counter);\n    val->lru = (LFUGetTimeInMinutes()<<8) | counter;\n}\n\nvoid updateExpire(redisDb *db, sds key, robj *valOld, robj *valNew)\n{\n    serverAssert(valOld->FExpires());\n    serverAssert(!valNew->FExpires());\n    \n    serverAssert(db->FKeyExpires((const char*)key));\n    \n    valNew->expire = std::move(valOld->expire);\n    valNew->SetFExpires(true);\n    valOld->SetFExpires(false);\n    return;\n}\n\nstatic void lookupKeyUpdateObj(robj *val, int flags)\n{\n    /* Update the access time for the ageing algorithm.\n     * Don't do it if we have a saving child, as this will trigger\n     * a copy on write madness. */\n    if (!hasActiveChildProcessOrBGSave() && !(flags & LOOKUP_NOTOUCH))\n    {\n        if (g_pserver->maxmemory_policy & MAXMEMORY_FLAG_LFU) {\n            updateLFU(val);\n        } else {\n            val->lru = LRU_CLOCK();\n        }\n    }\n}\n\n/* Low level key lookup API, not actually called directly from commands\n * implementations that should instead rely on lookupKeyRead(),\n * lookupKeyWrite() and lookupKeyReadWithFlags(). */\nstatic robj* lookupKey(redisDb *db, robj *key, int flags) {\n    auto itr = db->find(key);\n    if (itr) {\n        robj *val = itr.val();\n        lookupKeyUpdateObj(val, flags);\n        if (flags & LOOKUP_UPDATEMVCC) {\n            setMvccTstamp(val, getMvccTstamp());\n            db->trackkey(key, true /* fUpdate */);\n        }\n        return val;\n    } else {\n        return NULL;\n    }\n}\nstatic robj_roptr lookupKeyConst(redisDb *db, robj *key, int flags) {\n    serverAssert((flags & LOOKUP_UPDATEMVCC) == 0);\n    robj_roptr val;\n    if (g_pserver->m_pstorageFactory)\n        val = db->find(szFromObj(key)).val();\n    else\n        val = db->find_cached_threadsafe(szFromObj(key)).val();\n    \n    if (val != nullptr) {\n        lookupKeyUpdateObj(val.unsafe_robjcast(), flags);\n        return val;\n    }\n    return nullptr;\n}\n\n/* Lookup a key for read operations, or return NULL if the key is not found\n * in the specified DB.\n *\n * As a side effect of calling this function:\n * 1. A key gets expired if it reached it's TTL.\n * 2. The key last access time is updated.\n * 3. The global keys hits/misses stats are updated (reported in INFO).\n * 4. If keyspace notifications are enabled, a \"keymiss\" notification is fired.\n *\n * This API should not be used when we write to the key after obtaining\n * the object linked to the key, but only for read only operations.\n *\n * Flags change the behavior of this command:\n *\n *  LOOKUP_NONE (or zero): no special flags are passed.\n *  LOOKUP_NOTOUCH: don't alter the last access time of the key.\n *\n * Note: this function also returns NULL if the key is logically expired\n * but still existing, in case this is a replica, since this API is called only\n * for read operations. Even if the key expiry is master-driven, we can\n * correctly report a key is expired on slaves even if the master is lagging\n * expiring our key via DELs in the replication link. */\nrobj_roptr lookupKeyReadWithFlags(redisDb *db, robj *key, int flags) {\n    robj_roptr val;\n\n    if (expireIfNeeded(db,key) == 1) {\n        /* If we are in the context of a master, expireIfNeeded() returns 1\n         * when the key is no longer valid, so we can return NULL ASAP. */\n        if (listLength(g_pserver->masters) == 0)\n            goto keymiss;\n\n        /* However if we are in the context of a replica, expireIfNeeded() will\n         * not really try to expire the key, it only returns information\n         * about the \"logical\" status of the key: key expiring is up to the\n         * master in order to have a consistent view of master's data set.\n         *\n         * However, if the command caller is not the master, and as additional\n         * safety measure, the command invoked is a read-only command, we can\n         * safely return NULL here, and provide a more consistent behavior\n         * to clients accessing expired values in a read-only fashion, that\n         * will say the key as non existing.\n         *\n         * Notably this covers GETs when slaves are used to scale reads. */\n        if (serverTL->current_client &&\n            !FActiveMaster(serverTL->current_client) &&\n            serverTL->current_client->cmd &&\n            serverTL->current_client->cmd->flags & CMD_READONLY)\n        {\n            goto keymiss;\n        }\n    }\n    val = lookupKeyConst(db,key,flags);\n    if (val == nullptr)\n        goto keymiss;\n    g_pserver->stat_keyspace_hits++;\n    return val;\n\nkeymiss:\n    if (!(flags & LOOKUP_NONOTIFY)) {\n        notifyKeyspaceEvent(NOTIFY_KEY_MISS, \"keymiss\", key, db->id);\n    }\n    g_pserver->stat_keyspace_misses++;\n    return NULL;\n}\n\n/* Like lookupKeyReadWithFlags(), but does not use any flag, which is the\n * common case. */\nrobj_roptr lookupKeyRead(redisDb *db, robj *key) {\n    serverAssert(GlobalLocksAcquired());\n    return lookupKeyReadWithFlags(db,key,LOOKUP_NONE);\n}\nrobj_roptr lookupKeyRead(redisDb *db, robj *key, uint64_t mvccCheckpoint, AeLocker &locker) {\n    robj_roptr o;\n\n    if (aeThreadOwnsLock()) {\n        return lookupKeyReadWithFlags(db,key,LOOKUP_NONE);\n    } else {\n        // This is an async command\n        if (keyIsExpired(db,key))\n            return nullptr;\n        int idb = db->id;\n        if (serverTL->rgdbSnapshot[idb] == nullptr || serverTL->rgdbSnapshot[idb]->mvccCheckpoint() < mvccCheckpoint) {\n            locker.arm(serverTL->current_client);\n            if (serverTL->rgdbSnapshot[idb] != nullptr) {\n                db->endSnapshot(serverTL->rgdbSnapshot[idb]);\n                serverTL->rgdbSnapshot[idb] = nullptr;\n            } else {\n                serverTL->rgdbSnapshot[idb] = db->createSnapshot(mvccCheckpoint, true);\n            }\n            if (serverTL->rgdbSnapshot[idb] == nullptr) {\n                // We still need to service the read\n                o = lookupKeyReadWithFlags(db,key,LOOKUP_NONE);\n                serverTL->disable_async_commands = true; // don't try this again\n            }\n            else {\n                locker.disarm();\n            }\n        }\n        if (serverTL->rgdbSnapshot[idb] != nullptr) {\n            o = serverTL->rgdbSnapshot[idb]->find_cached_threadsafe(szFromObj(key)).val();\n        }\n    }\n\n    return o;\n}\n\n/* Lookup a key for write operations, and as a side effect, if needed, expires\n * the key if its TTL is reached.\n *\n * Returns the linked value object if the key exists or NULL if the key\n * does not exist in the specified DB. */\nrobj *lookupKeyWriteWithFlags(redisDb *db, robj *key, int flags) {\n    expireIfNeeded(db,key);\n    robj *o = lookupKey(db,key,flags|LOOKUP_UPDATEMVCC);\n    return o;\n}\n\nrobj *lookupKeyWrite(redisDb *db, robj *key) {\n    return lookupKeyWriteWithFlags(db, key, LOOKUP_NONE);\n}\nvoid SentReplyOnKeyMiss(client *c, robj *reply){\n    serverAssert(sdsEncodedObject(reply));\n    sds rep = szFromObj(reply);\n    if (sdslen(rep) > 1 && rep[0] == '-'){\n        addReplyErrorObject(c, reply);\n    } else {\n        addReply(c,reply);\n    }\n}\nrobj_roptr lookupKeyReadOrReply(client *c, robj *key, robj *reply) {\n    robj_roptr o = lookupKeyRead(c->db, key);\n    if (!o) SentReplyOnKeyMiss(c, reply);\n    return o;\n}\nrobj_roptr lookupKeyReadOrReply(client *c, robj *key, robj *reply, AeLocker &locker) {\n    robj_roptr o = lookupKeyRead(c->db, key, c->mvccCheckpoint, locker);\n    if (!o) SentReplyOnKeyMiss(c, reply);\n    return o;\n}\n\nrobj *lookupKeyWriteOrReply(client *c, robj *key, robj *reply) {\n    robj *o = lookupKeyWrite(c->db, key);\n    if (!o) SentReplyOnKeyMiss(c, reply);\n    return o;\n}\n\nbool dbAddCore(redisDb *db, sds key, robj *val, bool fUpdateMvcc, bool fAssumeNew = false, dict_iter *piterExisting = nullptr, bool fValExpires = false) {\n    serverAssert(fValExpires || !val->FExpires());\n    sds copy = sdsdupshared(key);\n    \n    uint64_t mvcc = getMvccTstamp();\n    if (fUpdateMvcc) {\n        setMvccTstamp(val, mvcc);\n    }\n\n    bool fInserted = db->insert(copy, val, fAssumeNew, piterExisting);\n\n    if (fInserted)\n    {\n        signalKeyAsReady(db, key, val->type);\n        if (g_pserver->cluster_enabled) slotToKeyAdd(key);\n    }\n    else\n    {\n        sdsfree(copy);\n    }\n\n    return fInserted;\n}\n\n/* Add the key to the DB. It's up to the caller to increment the reference\n * counter of the value if needed.\n *\n * The program is aborted if the key already exists. */\nvoid dbAdd(redisDb *db, robj *key, robj *val)\n{\n    bool fInserted = dbAddCore(db, szFromObj(key), val, true /* fUpdateMvcc */);\n    serverAssertWithInfo(NULL,key,fInserted);\n}\n\nvoid redisDb::dbOverwriteCore(redisDb::iter itr, sds keySds, robj *val, bool fUpdateMvcc, bool fRemoveExpire)\n{\n    robj *old = itr.val();\n    redisObjectStack keyO;\n    initStaticStringObject(keyO, keySds);\n    robj *key = &keyO;\n\n    if (old->FExpires()) {\n        if (fRemoveExpire) {\n            ::removeExpire(this, key);\n        }\n        else {\n            if (val->getrefcount(std::memory_order_relaxed) == OBJ_SHARED_REFCOUNT)\n                val = dupStringObject(val);\n            ::updateExpire(this, itr.key(), old, val);\n        }\n    }\n\n    if (g_pserver->maxmemory_policy & MAXMEMORY_FLAG_LFU) {\n        val->lru = old->lru;\n    }\n    if (fUpdateMvcc) {\n        if (val->getrefcount(std::memory_order_relaxed) == OBJ_SHARED_REFCOUNT)\n            val = dupStringObject(val);\n        setMvccTstamp(val, getMvccTstamp());\n    }\n\n    /* Although the key is not really deleted from the database, we regard \n    overwrite as two steps of unlink+add, so we still need to call the unlink \n    callback of the module. */\n    moduleNotifyKeyUnlink(key,old);\n    \n    if (g_pserver->lazyfree_lazy_server_del)\n        freeObjAsync(key, itr.val());\n    else\n        decrRefCount(itr.val());\n\n    updateValue(itr, val);\n}\n\n/* Overwrite an existing key with a new value. Incrementing the reference\n * count of the new value is up to the caller.\n * This function does not modify the expire time of the existing key.\n *\n * The program is aborted if the key was not already present. */\nvoid dbOverwrite(redisDb *db, robj *key, robj *val, bool fRemoveExpire, dict_iter *pitrExisting) {\n    redisDb::iter itr;\n    if (pitrExisting != nullptr)\n        itr = *pitrExisting;\n    else\n        itr = db->find(key);\n\n    serverAssertWithInfo(NULL,key,itr != nullptr);\n    lookupKeyUpdateObj(itr.val(), LOOKUP_NONE);\n    db->dbOverwriteCore(itr, szFromObj(key), val, !!g_pserver->fActiveReplica, fRemoveExpire);\n}\n\n/* Insert a key, handling duplicate keys according to fReplace */\nint dbMerge(redisDb *db, sds key, robj *val, int fReplace)\n{\n    if (fReplace)\n    {\n        auto itr = db->find(key);\n        if (itr == nullptr)\n            return (dbAddCore(db, key, val, false /* fUpdateMvcc */) == true);\n\n        robj_roptr old = itr.val();\n        if (mvccFromObj(old) <= mvccFromObj(val))\n        {\n            db->dbOverwriteCore(itr, key, val, false, true);\n            return true;\n        }\n\n        return false;\n    }\n    else\n    {\n        return (dbAddCore(db, key, val, true /* fUpdateMvcc */, true /* fAssumeNew */) == true);\n    }\n}\n\n/* High level Set operation. This function can be used in order to set\n * a key, whatever it was existing or not, to a new object.\n *\n * 1) The ref count of the value object is incremented.\n * 2) clients WATCHing for the destination key notified.\n * 3) The expire time of the key is reset (the key is made persistent),\n *    unless 'keepttl' is true.\n *\n * All the new keys in the database should be created via this interface.\n * The client 'c' argument may be set to NULL if the operation is performed\n * in a context where there is no clear client performing the operation. */\nvoid genericSetKey(client *c, redisDb *db, robj *key, robj *val, int keepttl, int signal) {\n    db->prepOverwriteForSnapshot(szFromObj(key));\n    dict_iter iter;\n    if (!dbAddCore(db, szFromObj(key), val, true /* fUpdateMvcc */, false /*fAssumeNew*/, &iter)) {\n        dbOverwrite(db, key, val, !keepttl, &iter);\n    }\n    incrRefCount(val);\n    if (signal) signalModifiedKey(c,db,key);\n}\n\n/* Common case for genericSetKey() where the TTL is not retained. */\nvoid setKey(client *c, redisDb *db, robj *key, robj *val) {\n    genericSetKey(c,db,key,val,0,1);\n}\n\n/* Return a random key, in form of a Redis object.\n * If there are no keys, NULL is returned.\n *\n * The function makes sure to return keys not already expired. */\nrobj *dbRandomKey(redisDb *db) {\n    int maxtries = 100;\n    bool allvolatile = db->expireSize() == db->size();\n\n    while(1) {\n        sds key;\n        robj *keyobj;\n\n        auto itr = db->random();\n        if (itr == nullptr) return NULL;\n\n        key = itr.key();\n        keyobj = createStringObject(key,sdslen(key));\n\n        if (itr.val()->FExpires())\n        {\n            if (allvolatile && listLength(g_pserver->masters) && --maxtries == 0) {\n                /* If the DB is composed only of keys with an expire set,\n                    * it could happen that all the keys are already logically\n                    * expired in the replica, so the function cannot stop because\n                    * expireIfNeeded() is false, nor it can stop because\n                    * dictGetRandomKey() returns NULL (there are keys to return).\n                    * To prevent the infinite loop we do some tries, but if there\n                    * are the conditions for an infinite loop, eventually we\n                    * return a key name that may be already expired. */\n                return keyobj;\n            }\n        }\n            \n        if (itr.val()->FExpires())\n        {\n             if (expireIfNeeded(db,keyobj)) {\n                decrRefCount(keyobj);\n                continue; /* search for another key. This expired. */\n             }\n        }\n        \n        return keyobj;\n    }\n}\n\nbool redisDbPersistentData::syncDelete(robj *key)\n{\n    /* Deleting an entry from the expires dict will not free the sds of\n     * the key, because it is shared with the main dictionary. */\n\n    auto itr = find(szFromObj(key));\n    if (itr != nullptr && itr.val()->FExpires())\n        removeExpire(key, itr);\n    \n    robj_sharedptr val(itr.val());\n    bool fDeleted = false;\n    if (m_spstorage != nullptr)\n        fDeleted = m_spstorage->erase(szFromObj(key));\n    fDeleted = (dictDelete(m_pdict,ptrFromObj(key)) == DICT_OK) || fDeleted;\n\n    if (fDeleted) {\n        /* Tells the module that the key has been unlinked from the database. */\n        moduleNotifyKeyUnlink(key,val); // MODULE Compat Note: We should be giving the actual key value here\n        \n        dictEntry *de = dictUnlink(m_dictChanged, szFromObj(key));\n        if (de != nullptr)\n        {\n            bool fUpdate = (bool)dictGetVal(de);\n            if (!fUpdate)\n                --m_cnewKeysPending;\n            dictFreeUnlinkedEntry(m_dictChanged, de);\n        }\n        \n        if (m_pdbSnapshot != nullptr)\n        {\n            auto itr = m_pdbSnapshot->find_cached_threadsafe(szFromObj(key));\n            if (itr != nullptr)\n            {\n                sds keyTombstone = sdsdupshared(itr.key());\n                uint64_t hash = dictGetHash(m_pdict, keyTombstone);\n                if (dictAdd(m_pdictTombstone, keyTombstone, (void*)hash) != DICT_OK)\n                    sdsfree(keyTombstone);\n            }\n        }\n        if (g_pserver->cluster_enabled) slotToKeyDel(szFromObj(key));\n        return 1;\n    } else {\n        return 0;\n    }\n}\n\n/* Delete a key, value, and associated expiration entry if any, from the DB */\nint dbSyncDelete(redisDb *db, robj *key) {\n    return db->syncDelete(key);\n}\n\n/* This is a wrapper whose behavior depends on the Redis lazy free\n * configuration. Deletes the key synchronously or asynchronously. */\nint dbDelete(redisDb *db, robj *key) {\n    return g_pserver->lazyfree_lazy_server_del ? dbAsyncDelete(db,key) :\n                                             dbSyncDelete(db,key);\n}\n\n/* Prepare the string object stored at 'key' to be modified destructively\n * to implement commands like SETBIT or APPEND.\n *\n * An object is usually ready to be modified unless one of the two conditions\n * are true:\n *\n * 1) The object 'o' is shared (refcount > 1), we don't want to affect\n *    other users.\n * 2) The object encoding is not \"RAW\".\n *\n * If the object is found in one of the above conditions (or both) by the\n * function, an unshared / not-encoded copy of the string object is stored\n * at 'key' in the specified 'db'. Otherwise the object 'o' itself is\n * returned.\n *\n * USAGE:\n *\n * The object 'o' is what the caller already obtained by looking up 'key'\n * in 'db', the usage pattern looks like this:\n *\n * o = lookupKeyWrite(db,key);\n * if (checkType(c,o,OBJ_STRING)) return;\n * o = dbUnshareStringValue(db,key,o);\n *\n * At this point the caller is ready to modify the object, for example\n * using an sdscat() call to append some data, or anything else.\n */\nrobj *dbUnshareStringValue(redisDb *db, robj *key, robj *o) {\n    serverAssert(o->type == OBJ_STRING);\n    if (o->getrefcount(std::memory_order_relaxed) != 1 || o->encoding != OBJ_ENCODING_RAW) {\n        robj *decoded = getDecodedObject(o);\n        o = createRawStringObject(szFromObj(decoded), sdslen(szFromObj(decoded)));\n        decrRefCount(decoded);\n        dbOverwrite(db,key,o);\n    }\n    return o;\n}\n\n/* Remove all keys from the database(s) structure. The dbarray argument\n * may not be the server main DBs (could be a backup).\n *\n * The dbnum can be -1 if all the DBs should be emptied, or the specified\n * DB index if we want to empty only a single database.\n * The function returns the number of keys removed from the database(s). */\nlong long emptyDbStructure(redisDb **dbarray, int dbnum, int async,\n                           void(callback)(void*))\n{\n    long long removed = 0;\n    int startdb, enddb;\n\n    if (dbnum == -1) {\n        startdb = 0;\n        enddb = cserver.dbnum-1;\n    } else {\n        startdb = enddb = dbnum;\n    }\n\n    for (int j = startdb; j <= enddb; j++) {\n        removed += dbarray[j]->size();\n        dbarray[j]->clear(async, callback);\n        /* Because all keys of database are removed, reset average ttl. */\n        dbarray[j]->avg_ttl = 0;\n    }\n\n    return removed;\n}\n\n/* Remove all keys from all the databases in a Redis DB.\n * If callback is given the function is called from time to time to\n * signal that work is in progress.\n *\n * The dbnum can be -1 if all the DBs should be flushed, or the specified\n * DB number if we want to flush only a single Redis database number.\n *\n * Flags are be EMPTYDB_NO_FLAGS if no special flags are specified or\n * EMPTYDB_ASYNC if we want the memory to be freed in a different thread\n * and the function to return ASAP.\n *\n * On success the function returns the number of keys removed from the\n * database(s). Otherwise -1 is returned in the specific case the\n * DB number is out of range, and errno is set to EINVAL. */\nlong long emptyDb(int dbnum, int flags, void(callback)(void*)) {\n    int async = (flags & EMPTYDB_ASYNC);\n    RedisModuleFlushInfoV1 fi = {REDISMODULE_FLUSHINFO_VERSION,!async,dbnum};\n    long long removed = 0;\n\n    if (dbnum < -1 || dbnum >= cserver.dbnum) {\n        errno = EINVAL;\n        return -1;\n    }\n\n    /* Fire the flushdb modules event. */\n    moduleFireServerEvent(REDISMODULE_EVENT_FLUSHDB,\n                          REDISMODULE_SUBEVENT_FLUSHDB_START,\n                          &fi);\n\n    /* Make sure the WATCHed keys are affected by the FLUSH* commands.\n     * Note that we need to call the function while the keys are still\n     * there. */\n    signalFlushedDb(dbnum, async);\n\n    /* Empty redis database structure. */\n    removed = emptyDbStructure(g_pserver->db, dbnum, async, callback);\n\n    /* Flush slots to keys map if enable cluster, we can flush entire\n     * slots to keys map whatever dbnum because only support one DB\n     * in cluster mode. */\n    if (g_pserver->cluster_enabled) slotToKeyFlush(async);\n\n    if (dbnum == -1) flushSlaveKeysWithExpireList();\n\n    /* Also fire the end event. Note that this event will fire almost\n     * immediately after the start event if the flush is asynchronous. */\n    moduleFireServerEvent(REDISMODULE_EVENT_FLUSHDB,\n                          REDISMODULE_SUBEVENT_FLUSHDB_END,\n                          &fi);\n\n    return removed;\n}\n\n/* Store a backup of the database for later use, and put an empty one\n * instead of it. */\nconst dbBackup *backupDb(void) {\n    dbBackup *backup = new dbBackup();\n    \n    backup->dbarray = (const redisDbPersistentDataSnapshot**)zmalloc(sizeof(redisDbPersistentDataSnapshot*) * cserver.dbnum);\n    for (int i=0; i<cserver.dbnum; i++) {\n        backup->dbarray[i] = g_pserver->db[i]->createSnapshot(LLONG_MAX, false);\n    }\n\n    /* Backup cluster slots to keys map if enable cluster. */\n    if (g_pserver->cluster_enabled) {\n        backup->slots_to_keys = g_pserver->cluster->slots_to_keys;\n        memcpy(backup->slots_keys_count, g_pserver->cluster->slots_keys_count,\n            sizeof(g_pserver->cluster->slots_keys_count));\n        g_pserver->cluster->slots_to_keys = raxNew();\n        memset(g_pserver->cluster->slots_keys_count, 0,\n            sizeof(g_pserver->cluster->slots_keys_count));\n    }\n\n    moduleFireServerEvent(REDISMODULE_EVENT_REPL_BACKUP,\n                          REDISMODULE_SUBEVENT_REPL_BACKUP_CREATE,\n                          NULL);\n\n    return backup;\n}\n\n/* Discard a previously created backup, this can be slow (similar to FLUSHALL)\n * Arguments are similar to the ones of emptyDb, see EMPTYDB_ flags. */\nvoid discardDbBackup(const dbBackup *backup, int flags, void(callback)(void*)) {\n    UNUSED(callback);\n    int async = (flags & EMPTYDB_ASYNC);\n\n    /* Release main DBs backup . */\n    for (int i=0; i<cserver.dbnum; i++) {\n        g_pserver->db[i]->endSnapshot(backup->dbarray[i]);\n    }\n\n    /* Release slots to keys map backup if enable cluster. */\n    if (g_pserver->cluster_enabled) freeSlotsToKeysMap(backup->slots_to_keys, async);\n\n    /* Release buckup. */\n    zfree(backup->dbarray);\n    delete backup;\n\n    moduleFireServerEvent(REDISMODULE_EVENT_REPL_BACKUP,\n                          REDISMODULE_SUBEVENT_REPL_BACKUP_DISCARD,\n                          NULL);\n}\n\n/* Restore the previously created backup (discarding what currently resides\n * in the db).\n * This function should be called after the current contents of the database\n * was emptied with a previous call to emptyDb (possibly using the async mode). */\nvoid restoreDbBackup(const dbBackup *backup) {\n    /* Restore main DBs. */\n    for (int i=0; i<cserver.dbnum; i++) {\n        g_pserver->db[i]->restoreSnapshot(backup->dbarray[i]);\n    }\n    \n    /* Restore slots to keys map backup if enable cluster. */\n    if (g_pserver->cluster_enabled) {\n        serverAssert(g_pserver->cluster->slots_to_keys->numele == 0);\n        raxFree(g_pserver->cluster->slots_to_keys);\n        g_pserver->cluster->slots_to_keys = backup->slots_to_keys;\n        memcpy(g_pserver->cluster->slots_keys_count, backup->slots_keys_count,\n                sizeof(g_pserver->cluster->slots_keys_count));\n    }\n\n    /* Release buckup. */\n    zfree(backup->dbarray);\n    delete backup;\n\n    moduleFireServerEvent(REDISMODULE_EVENT_REPL_BACKUP,\n                          REDISMODULE_SUBEVENT_REPL_BACKUP_RESTORE,\n                          NULL);\n}\n\nint selectDb(client *c, int id) {\n    if (id < 0 || id >= cserver.dbnum)\n        return C_ERR;\n    c->db = g_pserver->db[id];\n    return C_OK;\n}\n\nlong long dbTotalServerKeyCount() {\n    long long total = 0;\n    int j;\n    for (j = 0; j < cserver.dbnum; j++) {\n        total += g_pserver->db[j]->size();\n    }\n    return total;\n}\n\n/*-----------------------------------------------------------------------------\n * Hooks for key space changes.\n *\n * Every time a key in the database is modified the function\n * signalModifiedKey() is called.\n *\n * Every time a DB is flushed the function signalFlushDb() is called.\n *----------------------------------------------------------------------------*/\n\n/* Note that the 'c' argument may be NULL if the key was modified out of\n * a context of a client. */\nvoid signalModifiedKey(client *c, redisDb *db, robj *key) {\n    touchWatchedKey(db,key);\n    trackingInvalidateKey(c,key);\n}\n\nvoid signalFlushedDb(int dbid, int async) {\n    int startdb, enddb;\n    if (dbid == -1) {\n        startdb = 0;\n        enddb = cserver.dbnum-1;\n    } else {\n        startdb = enddb = dbid;\n    }\n\n    for (int j = startdb; j <= enddb; j++) {\n        touchAllWatchedKeysInDb(g_pserver->db[j], NULL);\n    }\n\n    trackingInvalidateKeysOnFlush(async);\n}\n\n/*-----------------------------------------------------------------------------\n * Type agnostic commands operating on the key space\n *----------------------------------------------------------------------------*/\n\n/* Return the set of flags to use for the emptyDb() call for FLUSHALL\n * and FLUSHDB commands.\n *\n * sync: flushes the database in an sync manner.\n * async: flushes the database in an async manner.\n * no option: determine sync or async according to the value of lazyfree-lazy-user-flush.\n *\n * On success C_OK is returned and the flags are stored in *flags, otherwise\n * C_ERR is returned and the function sends an error to the client. */\nint getFlushCommandFlags(client *c, int *flags) {\n    /* Parse the optional ASYNC option. */\n    if (c->argc == 2 && !strcasecmp(szFromObj(c->argv[1]),\"sync\")) {\n        *flags = EMPTYDB_NO_FLAGS;\n    } else if (c->argc == 2 && !strcasecmp(szFromObj(c->argv[1]),\"async\")) {\n        *flags = EMPTYDB_ASYNC;\n    } else if (c->argc == 1) {\n        *flags = g_pserver->lazyfree_lazy_user_flush ? EMPTYDB_ASYNC : EMPTYDB_NO_FLAGS;\n    } else {\n        addReplyErrorObject(c,shared.syntaxerr);\n        return C_ERR;\n    }\n    return C_OK;\n}\n\n/* Flushes the whole server data set. */\nvoid flushAllDataAndResetRDB(int flags) {\n    g_pserver->dirty += emptyDb(-1,flags,NULL);\n    if (g_pserver->FRdbSaveInProgress()) killRDBChild();\n    if (g_pserver->saveparamslen > 0) {\n        /* Normally rdbSave() will reset dirty, but we don't want this here\n         * as otherwise FLUSHALL will not be replicated nor put into the AOF. */\n        int saved_dirty = g_pserver->dirty;\n        rdbSaveInfo rsi, *rsiptr;\n        rsiptr = rdbPopulateSaveInfo(&rsi);\n        rdbSave(nullptr, rsiptr);\n        g_pserver->dirty = saved_dirty;\n    }\n    \n    /* Without that extra dirty++, when db was already empty, FLUSHALL will\n     * not be replicated nor put into the AOF. */\n    g_pserver->dirty++;\n#if defined(USE_JEMALLOC)\n    /* jemalloc 5 doesn't release pages back to the OS when there's no traffic.\n     * for large databases, flushdb blocks for long anyway, so a bit more won't\n     * harm and this way the flush and purge will be synchroneus. */\n    if (!(flags & EMPTYDB_ASYNC))\n        jemalloc_purge();\n#endif\n}\n\n/* FLUSHDB [ASYNC]\n *\n * Flushes the currently SELECTed Redis DB. */\nvoid flushdbCommand(client *c) {\n    int flags;\n\n    if (c->argc == 2)\n    {\n        if (!strcasecmp(szFromObj(c->argv[1]), \"cache\"))\n        {\n            if (g_pserver->m_pstorageFactory == nullptr)\n            {\n                addReplyError(c, \"Cannot flush cache without a storage provider set\");\n                return;\n            }\n            c->db->removeAllCachedValues();\n            addReply(c,shared.ok);\n            return;\n        }\n    }\n\n    if (getFlushCommandFlags(c,&flags) == C_ERR) return;\n    g_pserver->dirty += emptyDb(c->db->id,flags,NULL);\n    addReply(c,shared.ok);\n#if defined(USE_JEMALLOC)\n    /* jemalloc 5 doesn't release pages back to the OS when there's no traffic.\n     * for large databases, flushdb blocks for long anyway, so a bit more won't\n     * harm and this way the flush and purge will be synchroneus. */\n    if (!(flags & EMPTYDB_ASYNC))\n        jemalloc_purge();\n#endif\n}\n\n/* FLUSHALL [ASYNC]\n *\n * Flushes the whole server data set. */\nvoid flushallCommand(client *c) {\n    int flags;\n\n    if (c->argc == 2)\n    {\n        if (!strcasecmp(szFromObj(c->argv[1]), \"cache\"))\n        {\n            if (g_pserver->m_pstorageFactory == nullptr)\n            {\n                addReplyError(c, \"Cannot flush cache without a storage provider set\");\n                return;\n            }\n            for (int idb = 0; idb < cserver.dbnum; ++idb)\n                g_pserver->db[idb]->removeAllCachedValues();\n            addReply(c,shared.ok);\n            return;\n        }\n    }\n\n    if (getFlushCommandFlags(c,&flags) == C_ERR) return;\n    flushAllDataAndResetRDB(flags);\n    addReply(c,shared.ok);\n}\n\n/* This command implements DEL and LAZYDEL. */\nvoid delGenericCommand(client *c, int lazy) {\n    int numdel = 0, j;\n\n    for (j = 1; j < c->argc; j++) {\n        expireIfNeeded(c->db,c->argv[j]);\n        int deleted  = lazy ? dbAsyncDelete(c->db,c->argv[j]) :\n                              dbSyncDelete(c->db,c->argv[j]);\n        if (deleted) {\n            signalModifiedKey(c,c->db,c->argv[j]);\n            notifyKeyspaceEvent(NOTIFY_GENERIC,\n                \"del\",c->argv[j],c->db->id);\n            g_pserver->dirty++;\n            numdel++;\n        }\n    }\n    addReplyLongLong(c,numdel);\n}\n\nvoid delCommand(client *c) {\n    delGenericCommand(c,g_pserver->lazyfree_lazy_user_del);\n}\n\nvoid unlinkCommand(client *c) {\n    delGenericCommand(c,1);\n}\n\n/* EXISTS key1 key2 ... key_N.\n * Return value is the number of keys existing. */\nvoid existsCommand(client *c) {\n    long long count = 0;\n    int j;\n\n    for (j = 1; j < c->argc; j++) {\n        if (lookupKeyReadWithFlags(c->db,c->argv[j],LOOKUP_NOTOUCH)) count++;\n    }\n    addReplyLongLong(c,count);\n}\n\nvoid mexistsCommand(client *c) {\n    addReplyArrayLen(c, c->argc - 1);\n    for (int j = 1; j < c->argc; ++j) {\n        addReplyBool(c, lookupKeyRead(c->db, c->argv[j]));\n    }\n}\n\nvoid selectCommand(client *c) {\n    int id;\n\n    if (getIntFromObjectOrReply(c, c->argv[1], &id, NULL) != C_OK)\n        return;\n\n    if (g_pserver->cluster_enabled && id != 0) {\n        addReplyError(c,\"SELECT is not allowed in cluster mode\");\n        return;\n    }\n    if (selectDb(c,id) == C_ERR) {\n        addReplyError(c,\"DB index is out of range\");\n    } else {\n        addReply(c,shared.ok);\n    }\n}\n\nvoid randomkeyCommand(client *c) {\n    robj *key;\n\n    if ((key = dbRandomKey(c->db)) == NULL) {\n        addReplyNull(c);\n        return;\n    }\n\n    addReplyBulk(c,key);\n    decrRefCount(key);\n}\n\nbool redisDbPersistentData::iterate(std::function<bool(const char*, robj*)> fn)\n{\n    dictIterator *di = dictGetSafeIterator(m_pdict);\n    dictEntry *de = nullptr;\n    bool fResult = true;\n    while(fResult && ((de = dictNext(di)) != nullptr))\n    {\n        if (!fn((const char*)dictGetKey(de), (robj*)dictGetVal(de)))\n            fResult = false;\n    }\n    dictReleaseIterator(di);\n\n    if (m_spstorage != nullptr)\n    {\n        bool fSawAll = fResult && m_spstorage->enumerate([&](const char *key, size_t cchKey, const void *, size_t )->bool{\n            sds sdsKey = sdsnewlen(key, cchKey);\n            bool fContinue = true;\n            if (dictFind(m_pdict, sdsKey) == nullptr)\n            {\n                ensure(sdsKey, &de);\n                fContinue = fn((const char*)dictGetKey(de), (robj*)dictGetVal(de));\n                removeCachedValue(sdsKey);\n            }\n            sdsfree(sdsKey);\n            return fContinue;\n        });\n        return fSawAll;\n    }\n\n    if (fResult && m_pdbSnapshot != nullptr)\n    {\n        fResult = m_pdbSnapshot->iterate_threadsafe([&](const char *key, robj_roptr){\n            // Before passing off to the user we need to make sure it's not already in the\n            //  the current set, and not deleted\n            dictEntry *deCurrent = dictFind(m_pdict, key);\n            if (deCurrent != nullptr)\n                return true;\n            dictEntry *deTombstone = dictFind(m_pdictTombstone, key);\n            if (deTombstone != nullptr)\n                return true;\n\n            // Alright it's a key in the use keyspace, lets ensure it and then pass it off\n            ensure(key);\n            deCurrent = dictFind(m_pdict, key);\n            return fn(key, (robj*)dictGetVal(deCurrent));\n        }, true /*fKeyOnly*/);\n    }\n    \n    return fResult;\n}\n\nclient *createAOFClient(void);\nvoid freeFakeClient(client *);\nvoid keysCommandCore(client *cIn, const redisDbPersistentDataSnapshot *db, sds pattern)\n{\n    int plen = sdslen(pattern), allkeys;\n    unsigned long numkeys = 0;\n\n    client *c = createAOFClient();\n    c->flags |= CLIENT_FORCE_REPLY;\n\n    void *replylen = addReplyDeferredLen(c);\n\n    allkeys = (pattern[0] == '*' && plen == 1);\n    db->iterate_threadsafe([&](const char *key, robj_roptr)->bool {\n        robj *keyobj;\n\n        if (allkeys || stringmatchlen(pattern,plen,key,sdslen(key),0)) {\n            keyobj = createStringObject(key,sdslen(key));\n            if (!keyIsExpired(db,keyobj)) {\n                addReplyBulk(c,keyobj);\n                numkeys++;\n            }\n            decrRefCount(keyobj);\n        }\n        return !(cIn->flags.load(std::memory_order_relaxed) & CLIENT_CLOSE_ASAP);\n    }, true /*fKeyOnly*/);\n    \n    setDeferredArrayLen(c,replylen,numkeys);\n\n    aeAcquireLock();\n    addReplyProto(cIn, c->buf, c->bufpos);\n    listIter li;\n    listNode *ln;\n    listRewind(c->reply, &li);\n    while ((ln = listNext(&li)) != nullptr)\n    {\n        clientReplyBlock *block = (clientReplyBlock*)listNodeValue(ln);\n        addReplyProto(cIn, block->buf(), block->used);\n    }\n    aeReleaseLock();\n    freeFakeClient(c);\n}\n\nint prepareClientToWrite(client *c, bool fAsync);\nvoid keysCommand(client *c) {\n    sds pattern = szFromObj(c->argv[1]);\n\n    const redisDbPersistentDataSnapshot *snapshot = nullptr;\n    if (!(c->flags & (CLIENT_MULTI | CLIENT_BLOCKED | CLIENT_DENY_BLOCKING)) && !(serverTL->in_eval || serverTL->in_exec))\n        snapshot = c->db->createSnapshot(c->mvccCheckpoint, true /* fOptional */);\n    if (snapshot != nullptr)\n    {\n        sds patternCopy = sdsdup(pattern);\n        aeEventLoop *el = serverTL->el;\n        blockClient(c, BLOCKED_ASYNC);\n        redisDb *db = c->db;\n        g_pserver->asyncworkqueue->AddWorkFunction([el, c, db, patternCopy, snapshot]{\n            keysCommandCore(c, snapshot, patternCopy);\n            sdsfree(patternCopy);\n            aePostFunction(el, [c, db, snapshot]{\n                aeReleaseLock();    // we need to lock with coordination of the client\n\n                std::unique_lock<decltype(c->lock)> lock(c->lock);\n                AeLocker locker;\n                locker.arm(c);\n\n                unblockClient(c);\n\n                locker.disarm();\n                lock.unlock();\n                db->endSnapshotAsync(snapshot);\n                aeAcquireLock();\n            });\n        });\n    }\n    else\n    {\n        keysCommandCore(c, c->db, pattern);\n    }\n}\n\n/* This callback is used by scanGenericCommand in order to collect elements\n * returned by the dictionary iterator into a list. */\nvoid scanCallback(void *privdata, const dictEntry *de) {\n    void **pd = (void**) privdata;\n    list *keys = (list*)pd[0];\n    robj *o = (robj*)pd[1];\n    robj *key, *val = NULL;\n\n    if (o == NULL) {\n        sds sdskey = (sds)dictGetKey(de);\n        key = createStringObject(sdskey, sdslen(sdskey));\n    } else if (o->type == OBJ_SET) {\n        sds keysds = (sds)dictGetKey(de);\n        key = createStringObject(keysds,sdslen(keysds));\n    } else if (o->type == OBJ_HASH) {\n        sds sdskey = (sds)dictGetKey(de);\n        sds sdsval = (sds)dictGetVal(de);\n        key = createStringObject(sdskey,sdslen(sdskey));\n        val = createStringObject(sdsval,sdslen(sdsval));\n    } else if (o->type == OBJ_ZSET) {\n        sds sdskey = (sds)dictGetKey(de);\n        key = createStringObject(sdskey,sdslen(sdskey));\n        val = createStringObjectFromLongDouble(*(double*)dictGetVal(de),0);\n    } else {\n        serverPanic(\"Type not handled in SCAN callback.\");\n    }\n\n    listAddNodeTail(keys, key);\n    if (val) listAddNodeTail(keys, val);\n}\n\n/* Try to parse a SCAN cursor stored at object 'o':\n * if the cursor is valid, store it as unsigned integer into *cursor and\n * returns C_OK. Otherwise return C_ERR and send an error to the\n * client. */\nint parseScanCursorOrReply(client *c, robj *o, unsigned long *cursor) {\n    char *eptr;\n\n    /* Use strtoul() because we need an *unsigned* long, so\n     * getLongLongFromObject() does not cover the whole cursor space. */\n    errno = 0;\n    *cursor = strtoul(szFromObj(o), &eptr, 10);\n    if (isspace(((char*)ptrFromObj(o))[0]) || eptr[0] != '\\0' || errno == ERANGE)\n    {\n        addReplyError(c, \"invalid cursor\");\n        return C_ERR;\n    }\n    return C_OK;\n}\n\n\nstatic bool filterKey(robj_roptr kobj, sds pat, int patlen)\n{\n    bool filter = false;\n    if (sdsEncodedObject(kobj)) {\n        if (!stringmatchlen(pat, patlen, szFromObj(kobj), sdslen(szFromObj(kobj)), 0))\n            filter = true;\n    } else {\n        char buf[LONG_STR_SIZE];\n        int len;\n\n        serverAssert(kobj->encoding == OBJ_ENCODING_INT);\n        len = ll2string(buf,sizeof(buf),(long)ptrFromObj(kobj));\n        if (!stringmatchlen(pat, patlen, buf, len, 0)) filter = true;\n    }\n    return filter;\n}\n\n/* This command implements SCAN, HSCAN and SSCAN commands.\n * If object 'o' is passed, then it must be a Hash, Set or Zset object, otherwise\n * if 'o' is NULL the command will operate on the dictionary associated with\n * the current database.\n *\n * When 'o' is not NULL the function assumes that the first argument in\n * the client arguments vector is a key so it skips it before iterating\n * in order to parse options.\n *\n * In the case of a Hash object the function returns both the field and value\n * of every element on the Hash. */\nvoid scanFilterAndReply(client *c, list *keys, sds pat, sds type, int use_pattern, robj_roptr o, unsigned long cursor);\nvoid scanGenericCommand(client *c, robj_roptr o, unsigned long cursor) {\n    int i, j;\n    list *keys = listCreate();\n    long count = 10;\n    sds pat = NULL;\n    sds type = NULL;\n    int patlen = 0, use_pattern = 0;\n    dict *ht;\n\n    /* Object must be NULL (to iterate keys names), or the type of the object\n     * must be Set, Sorted Set, or Hash. */\n    serverAssert(o == nullptr || o->type == OBJ_SET || o->type == OBJ_HASH ||\n                o->type == OBJ_ZSET);\n\n    /* Set i to the first option argument. The previous one is the cursor. */\n    i = (o == nullptr) ? 2 : 3; /* Skip the key argument if needed. */\n\n    /* Step 1: Parse options. */\n    while (i < c->argc) {\n        j = c->argc - i;\n        if (!strcasecmp(szFromObj(c->argv[i]), \"count\") && j >= 2) {\n            if (getLongFromObjectOrReply(c, c->argv[i+1], &count, NULL)\n                != C_OK)\n            {\n                goto cleanup;\n            }\n\n            if (count < 1) {\n                addReplyErrorObject(c,shared.syntaxerr);\n                goto cleanup;\n            }\n\n            i += 2;\n        } else if (!strcasecmp(szFromObj(c->argv[i]), \"match\") && j >= 2) {\n            pat = szFromObj(c->argv[i+1]);\n            patlen = sdslen(pat);\n\n            /* The pattern always matches if it is exactly \"*\", so it is\n             * equivalent to disabling it. */\n            use_pattern = !(pat[0] == '*' && patlen == 1);\n\n            i += 2;\n        } else if (!strcasecmp(szFromObj(c->argv[i]), \"type\") && o == nullptr && j >= 2) {\n            /* SCAN for a particular type only applies to the db dict */\n            type = szFromObj(c->argv[i+1]);\n            i+= 2;\n        } else {\n            addReplyErrorObject(c,shared.syntaxerr);\n            goto cleanup;\n        }\n    }\n\n    if (o == nullptr && count >= 100 && !(serverTL->in_eval || serverTL->in_exec))\n    {\n        // Do an async version\n        if (c->asyncCommand(\n            [c, keys, pat, type, cursor, count, use_pattern] (const redisDbPersistentDataSnapshot *snapshot, const std::vector<robj_sharedptr> &) {\n                sds patCopy = pat ? sdsdup(pat) : nullptr;\n                sds typeCopy = type ? sdsdup(type) : nullptr;\n                auto cursorResult = snapshot->scan_threadsafe(cursor, count, typeCopy, keys);\n                if (use_pattern) {\n                    listNode *ln = listFirst(keys);\n                    int patlen = sdslen(patCopy);\n                    while (ln != nullptr)\n                    {\n                        listNode *next = ln->next;\n                        if (filterKey((robj*)listNodeValue(ln), patCopy, patlen))\n                        {\n                            robj *kobj = (robj*)listNodeValue(ln);\n                            decrRefCount(kobj);\n                            listDelNode(keys, ln);\n                        }\n                        ln = next;\n                    }\n                }\n                if (patCopy != nullptr)\n                    sdsfree(patCopy);\n                if (typeCopy != nullptr)\n                    sdsfree(typeCopy);\n                mstime_t timeScanFilter;\n                latencyStartMonitor(timeScanFilter);\n                scanFilterAndReply(c, keys, nullptr, nullptr, false, nullptr, cursorResult);\n                latencyEndMonitor(timeScanFilter);\n                latencyAddSampleIfNeeded(\"scan-async-filter\", timeScanFilter);\n            },\n            [keys] (const redisDbPersistentDataSnapshot *) {\n                listSetFreeMethod(keys,decrRefCountVoid);\n                listRelease(keys);\n            }\n            )) {\n            return;\n        }\n    }\n\n    /* Step 2: Iterate the collection.\n     *\n     * Note that if the object is encoded with a ziplist, intset, or any other\n     * representation that is not a hash table, we are sure that it is also\n     * composed of a small number of elements. So to avoid taking state we\n     * just return everything inside the object in a single call, setting the\n     * cursor to zero to signal the end of the iteration. */\n\n    /* Handle the case of a hash table. */\n    ht = NULL;\n    if (o == nullptr) {\n        ht = c->db->dictUnsafeKeyOnly();\n    } else if (o->type == OBJ_SET && o->encoding == OBJ_ENCODING_HT) {\n        ht = (dict*)ptrFromObj(o);\n    } else if (o->type == OBJ_HASH && o->encoding == OBJ_ENCODING_HT) {\n        ht = (dict*)ptrFromObj(o);\n        count *= 2; /* We return key / value for this type. */\n    } else if (o->type == OBJ_ZSET && o->encoding == OBJ_ENCODING_SKIPLIST) {\n        zset *zs = (zset*)ptrFromObj(o);\n        ht = zs->dict;\n        count *= 2; /* We return key / value for this type. */\n    }\n\n    if (ht) {\n        if (ht == c->db->dictUnsafeKeyOnly())\n        {\n            cursor = c->db->scan_threadsafe(cursor, count, nullptr, keys);\n        }\n        else\n        {\n            void *privdata[2];\n            /* We set the max number of iterations to ten times the specified\n            * COUNT, so if the hash table is in a pathological state (very\n            * sparsely populated) we avoid to block too much time at the cost\n            * of returning no or very few elements. */\n            long maxiterations = count*10;\n\n            /* We pass two pointers to the callback: the list to which it will\n            * add new elements, and the object containing the dictionary so that\n            * it is possible to fetch more data in a type-dependent way. */\n            privdata[0] = keys;\n            privdata[1] = o.unsafe_robjcast();\n            do {\n                cursor = dictScan(ht, cursor, scanCallback, NULL, privdata);\n            } while (cursor &&\n                maxiterations-- &&\n                listLength(keys) < (unsigned long)count);\n        }\n    } else if (o->type == OBJ_SET) {\n        int pos = 0;\n        int64_t ll;\n\n        while(intsetGet((intset*)ptrFromObj(o),pos++,&ll))\n            listAddNodeTail(keys,createStringObjectFromLongLong(ll));\n        cursor = 0;\n    } else if (o->type == OBJ_HASH || o->type == OBJ_ZSET) {\n        unsigned char *p = ziplistIndex((unsigned char*)ptrFromObj(o),0);\n        unsigned char *vstr;\n        unsigned int vlen;\n        long long vll;\n\n        while(p) {\n            ziplistGet(p,&vstr,&vlen,&vll);\n            listAddNodeTail(keys,\n                (vstr != NULL) ? createStringObject((char*)vstr,vlen) :\n                                 createStringObjectFromLongLong(vll));\n            p = ziplistNext((unsigned char*)ptrFromObj(o),p);\n        }\n        cursor = 0;\n    } else {\n        serverPanic(\"Not handled encoding in SCAN.\");\n    }\n\n    scanFilterAndReply(c, keys, pat, type, use_pattern, o, cursor);\n\ncleanup:\n    listSetFreeMethod(keys,decrRefCountVoid);\n    listRelease(keys);\n}\n\nvoid scanFilterAndReply(client *c, list *keys, sds pat, sds type, int use_pattern, robj_roptr o, unsigned long cursor)\n{\n    listNode *node, *nextnode;\n    int patlen = (pat != nullptr) ? sdslen(pat) : 0;\n    \n    /* Step 3: Filter elements. */\n    node = listFirst(keys);\n    while (node) {\n        robj *kobj = (robj*)listNodeValue(node);\n        nextnode = listNextNode(node);\n        int filter = 0;\n\n        /* Filter element if it does not match the pattern. */\n        if (use_pattern) {\n            if (filterKey(kobj, pat, patlen))\n                filter = 1;\n        }\n\n        /* Filter an element if it isn't the type we want. */\n        if (!filter && o == nullptr && type){\n            robj_roptr typecheck = lookupKeyReadWithFlags(c->db, kobj, LOOKUP_NOTOUCH);\n            const char* typeT = getObjectTypeName(typecheck);\n            if (strcasecmp((char*) type, typeT)) filter = 1;\n        }\n\n        /* Filter element if it is an expired key. */\n        if (!filter && o == nullptr && expireIfNeeded(c->db, kobj)) filter = 1;\n\n        /* Remove the element and its associated value if needed. */\n        if (filter) {\n            decrRefCount(kobj);\n            listDelNode(keys, node);\n        }\n\n        /* If this is a hash or a sorted set, we have a flat list of\n         * key-value elements, so if this element was filtered, remove the\n         * value, or skip it if it was not filtered: we only match keys. */\n        if (o && (o->type == OBJ_ZSET || o->type == OBJ_HASH)) {\n            node = nextnode;\n            serverAssert(node); /* assertion for valgrind (avoid NPD) */\n            nextnode = listNextNode(node);\n            if (filter) {\n                kobj = (robj*)listNodeValue(node);\n                decrRefCount(kobj);\n                listDelNode(keys, node);\n            }\n        }\n        node = nextnode;\n    }\n\n    /* Step 4: Reply to the client. */\n    addReplyArrayLen(c, 2);\n    addReplyBulkLongLong(c,cursor);\n\n    addReplyArrayLen(c, listLength(keys));\n    while ((node = listFirst(keys)) != NULL) {\n        robj *kobj = (robj*)listNodeValue(node);\n        addReplyBulk(c, kobj);\n        decrRefCount(kobj);\n        listDelNode(keys, node);\n    }\n}\n\n/* The SCAN command completely relies on scanGenericCommand. */\nvoid scanCommand(client *c) {\n    unsigned long cursor;\n    if (parseScanCursorOrReply(c,c->argv[1],&cursor) == C_ERR) return;\n    scanGenericCommand(c,nullptr,cursor);\n}\n\nvoid dbsizeCommand(client *c) {\n    addReplyLongLong(c,c->db->size());\n}\n\nvoid lastsaveCommand(client *c) {\n    addReplyLongLong(c,g_pserver->lastsave);\n}\n\nconst char* getObjectTypeName(robj_roptr o) {\n    const char* type;\n    if (o == nullptr) {\n        type = \"none\";\n    } else {\n        switch(o->type) {\n        case OBJ_STRING: type = \"string\"; break;\n        case OBJ_LIST: type = \"list\"; break;\n        case OBJ_SET: type = \"set\"; break;\n        case OBJ_ZSET: type = \"zset\"; break;\n        case OBJ_HASH: type = \"hash\"; break;\n        case OBJ_STREAM: type = \"stream\"; break;\n        case OBJ_MODULE: {\n            moduleValue *mv = (moduleValue*)ptrFromObj(o);\n            type = mv->type->name;\n        }; break;\n        default: type = \"unknown\"; break;\n        }\n    }\n    return type;\n}\n\nvoid typeCommand(client *c) {\n    robj_roptr o = lookupKeyReadWithFlags(c->db,c->argv[1],LOOKUP_NOTOUCH);\n    addReplyStatus(c, getObjectTypeName(o));\n}\n\nvoid shutdownCommand(client *c) {\n    int flags = 0;\n\n    if (c->argc > 2) {\n        addReplyErrorObject(c,shared.syntaxerr);\n        return;\n    } else if (c->argc == 2) {\n        if (!strcasecmp(szFromObj(c->argv[1]),\"nosave\")) {\n            flags |= SHUTDOWN_NOSAVE;\n        } else if (!strcasecmp(szFromObj(c->argv[1]),\"save\")) {\n            flags |= SHUTDOWN_SAVE;\n        } else if (!strcasecmp(szFromObj(c->argv[1]), \"soft\")) {\n            g_pserver->soft_shutdown = true;\n            serverLog(LL_WARNING, \"Soft Shutdown Initiated\");\n            addReply(c, shared.ok);\n            return;\n        } else {\n            addReplyErrorObject(c,shared.syntaxerr);\n            return;\n        }\n    }\n    if (prepareForShutdown(flags) == C_OK) throw ShutdownException();\n    addReplyError(c,\"Errors trying to SHUTDOWN. Check logs.\");\n}\n\nvoid renameGenericCommand(client *c, int nx) {\n    robj *o;\n    int samekey = 0;\n\n    /* When source and dest key is the same, no operation is performed,\n     * if the key exists, however we still return an error on unexisting key. */\n    if (sdscmp(szFromObj(c->argv[1]),szFromObj(c->argv[2])) == 0) samekey = 1;\n\n    if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr)) == NULL)\n        return;\n\n    if (samekey) {\n        addReply(c,nx ? shared.czero : shared.ok);\n        return;\n    }\n\n    incrRefCount(o);\n\n    if (lookupKeyWrite(c->db,c->argv[2]) != NULL) {\n        if (nx) {\n            decrRefCount(o);\n            addReply(c,shared.czero);\n            return;\n        }\n        /* Overwrite: delete the old key before creating the new one\n         * with the same name. */\n        dbDelete(c->db,c->argv[2]);\n    }\n    bool fExpires = o->FExpires();\n    long long whenT = o->expire.when();\n    dbDelete(c->db,c->argv[1]);\n    o->SetFExpires(fExpires);\n    dbAddCore(c->db,szFromObj(c->argv[2]),o,true /*fUpdateMvcc*/,true/*fAssumeNew*/,nullptr,true/*fValExpires*/);\n    serverAssert(whenT == o->expire.when());    // dbDelete and dbAdd must not modify the expire, just the FExpire bit\n    signalModifiedKey(c,c->db,c->argv[1]);\n    signalModifiedKey(c,c->db,c->argv[2]);\n    notifyKeyspaceEvent(NOTIFY_GENERIC,\"rename_from\",\n        c->argv[1],c->db->id);\n    notifyKeyspaceEvent(NOTIFY_GENERIC,\"rename_to\",\n        c->argv[2],c->db->id);\n    g_pserver->dirty++;\n    addReply(c,nx ? shared.cone : shared.ok);\n}\n\nvoid renameCommand(client *c) {\n    renameGenericCommand(c,0);\n}\n\nvoid renamenxCommand(client *c) {\n    renameGenericCommand(c,1);\n}\n\nvoid moveCommand(client *c) {\n    robj *o;\n    redisDb *src, *dst;\n    int srcid, dbid;\n\n    if (g_pserver->cluster_enabled) {\n        addReplyError(c,\"MOVE is not allowed in cluster mode\");\n        return;\n    }\n\n    /* Obtain source and target DB pointers */\n    src = c->db;\n    srcid = c->db->id;\n\n    if (getIntFromObjectOrReply(c, c->argv[2], &dbid, NULL) != C_OK)\n        return;\n\n    if (selectDb(c,dbid) == C_ERR) {\n        addReplyError(c,\"DB index is out of range\");\n        return;\n    }\n    dst = c->db;\n    selectDb(c,srcid); /* Back to the source DB */\n\n    /* If the user is moving using as target the same\n     * DB as the source DB it is probably an error. */\n    if (src == dst) {\n        addReplyErrorObject(c,shared.sameobjecterr);\n        return;\n    }\n\n    /* Check if the element exists and get a reference */\n    o = lookupKeyWrite(c->db,c->argv[1]);\n    if (!o) {\n        addReply(c,shared.czero);\n        return;\n    }\n\n    /* Return zero if the key already exists in the target DB */\n    if (lookupKeyWrite(dst,c->argv[1]) != NULL) {\n        addReply(c,shared.czero);\n        return;\n    }\n\n    incrRefCount(o);\n    bool fExpire = o->FExpires();\n    long long whenT = o->expire.when();\n    dbDelete(src,c->argv[1]);\n    g_pserver->dirty++;\n\n    o->SetFExpires(fExpire);\n    dbAddCore(dst, szFromObj(c->argv[1]), o, true /*fUpdateMvcc*/, true /*fAssumeNew*/, nullptr, true /*fValExpires*/);\n    serverAssert(whenT == o->expire.when());    // add/delete must not modify the expire time\n\n    signalModifiedKey(c,src,c->argv[1]);\n    signalModifiedKey(c,dst,c->argv[1]);\n    notifyKeyspaceEvent(NOTIFY_GENERIC,\n                \"move_from\",c->argv[1],src->id);\n    notifyKeyspaceEvent(NOTIFY_GENERIC,\n                \"move_to\",c->argv[1],dst->id);\n\n    addReply(c,shared.cone);\n}\n\nvoid copyCommand(client *c) {\n    robj *o;\n    redisDb *src, *dst;\n    int srcid, dbid;\n    expireEntry *expire = nullptr;\n    int j, replace = 0, fdelete = 0;\n\n    /* Obtain source and target DB pointers \n     * Default target DB is the same as the source DB \n     * Parse the REPLACE option and targetDB option. */\n    src = c->db;\n    dst = c->db;\n    srcid = c->db->id;\n    dbid = c->db->id;\n    for (j = 3; j < c->argc; j++) {\n        int additional = c->argc - j - 1;\n        if (!strcasecmp(szFromObj(c->argv[j]),\"replace\")) {\n            replace = 1;\n        } else if (!strcasecmp(szFromObj(c->argv[j]), \"db\") && additional >= 1) {\n            if (getIntFromObjectOrReply(c, c->argv[j+1], &dbid, NULL) != C_OK)\n                return;\n\n            if (selectDb(c, dbid) == C_ERR) {\n                addReplyError(c,\"DB index is out of range\");\n                return;\n            }\n            dst = c->db;\n            selectDb(c,srcid); /* Back to the source DB */\n            j++; /* Consume additional arg. */\n        } else {\n            addReplyErrorObject(c,shared.syntaxerr);\n            return;\n        }\n    }\n\n    if ((g_pserver->cluster_enabled == 1) && (srcid != 0 || dbid != 0)) {\n        addReplyError(c,\"Copying to another database is not allowed in cluster mode\");\n        return;\n    }\n\n    /* If the user select the same DB as\n     * the source DB and using newkey as the same key\n     * it is probably an error. */\n    robj *key = c->argv[1];\n    robj *newkey = c->argv[2];\n    if (src == dst && (sdscmp(szFromObj(key), szFromObj(newkey)) == 0)) {\n        addReplyErrorObject(c,shared.sameobjecterr);\n        return;\n    }\n\n    /* Check if the element exists and get a reference */\n    o = lookupKeyWrite(c->db, key);\n    if (!o) {\n        addReply(c,shared.czero);\n        return;\n    }\n    expire = o->FExpires() ? &o->expire : nullptr;\n\n    /* Return zero if the key already exists in the target DB. \n     * If REPLACE option is selected, delete newkey from targetDB. */\n    if (lookupKeyWrite(dst,newkey) != NULL) {\n        if (replace) {\n            fdelete = 1;\n        } else {\n            addReply(c,shared.czero);\n            return;\n        }\n    }\n\n    /* Duplicate object according to object's type. */\n    robj *newobj;\n    switch(o->type) {\n        case OBJ_STRING: newobj = dupStringObject(o); break;\n        case OBJ_LIST: newobj = listTypeDup(o); break;\n        case OBJ_SET: newobj = setTypeDup(o); break;\n        case OBJ_ZSET: newobj = zsetDup(o); break;\n        case OBJ_HASH: newobj = hashTypeDup(o); break;\n        case OBJ_STREAM: newobj = streamDup(o); break;\n        case OBJ_MODULE:\n            newobj = moduleTypeDupOrReply(c, key, newkey, o);\n            if (!newobj) return;\n            break;\n        default:\n            addReplyError(c, \"unknown type object\");\n            return;\n    }\n\n    if (fdelete) {\n        dbDelete(dst,newkey);\n    }\n\n    dbAdd(dst,newkey,newobj);\n    if (expire != nullptr)\n        setExpire(c, dst, newkey, expire->duplicate());\n\n    /* OK! key copied */\n    signalModifiedKey(c,dst,c->argv[2]);\n    notifyKeyspaceEvent(NOTIFY_GENERIC,\"copy_to\",c->argv[2],dst->id);\n\n    g_pserver->dirty++;\n    addReply(c,shared.cone);\n}\n\n/* Helper function for dbSwapDatabases(): scans the list of keys that have\n * one or more blocked clients for B[LR]POP or other blocking commands\n * and signal the keys as ready if they are of the right type. See the comment\n * where the function is used for more info. */\nvoid scanDatabaseForReadyLists(redisDb *db) {\n    dictEntry *de;\n    dictIterator *di = dictGetSafeIterator(db->blocking_keys);\n    while((de = dictNext(di)) != NULL) {\n        robj *key = (robj*)dictGetKey(de);\n        robj *value = lookupKey(db,key,LOOKUP_NOTOUCH);\n        if (value) signalKeyAsReady(db, key, value->type);\n    }\n    dictReleaseIterator(di);\n}\n\n/* Swap two databases at runtime so that all clients will magically see\n * the new database even if already connected. Note that the client\n * structure c->db points to a given DB, so we need to be smarter and\n * swap the underlying referenced structures, otherwise we would need\n * to fix all the references to the Redis DB structure.\n *\n * Returns C_ERR if at least one of the DB ids are out of range, otherwise\n * C_OK is returned. */\nint dbSwapDatabases(int id1, int id2) {\n    if (id1 < 0 || id1 >= cserver.dbnum ||\n        id2 < 0 || id2 >= cserver.dbnum) return C_ERR;\n    if (id1 == id2) return C_OK;\n    std::swap(g_pserver->db[id1], g_pserver->db[id2]);\n\n    /* Note that we don't swap blocking_keys,\n     * ready_keys and watched_keys, since we want clients to\n     * remain in the same DB they were. so put them back */\n    std::swap(g_pserver->db[id1]->blocking_keys, g_pserver->db[id2]->blocking_keys);\n    std::swap(g_pserver->db[id1]->ready_keys, g_pserver->db[id2]->ready_keys);\n    std::swap(g_pserver->db[id1]->watched_keys, g_pserver->db[id2]->watched_keys);\n\n    /* Don't swap the redisdb id, as it is expected to be same as database index everywhere else.\n       For example, flushdb, master/slave replication, etc. */\n    std::swap(g_pserver->db[id1]->id, g_pserver->db[id2]->id);\n\n    /* Now we need to handle clients blocked on lists: as an effect\n     * of swapping the two DBs, a client that was waiting for list\n     * X in a given DB, may now actually be unblocked if X happens\n     * to exist in the new version of the DB, after the swap.\n     *\n     * However normally we only do this check for efficiency reasons\n     * in dbAdd() when a list is created. So here we need to rescan\n     * the list of clients blocked on lists and signal lists as ready\n     * if needed.\n     *\n     * Also the swapdb should make transaction fail if there is any\n     * client watching keys */\n    scanDatabaseForReadyLists(g_pserver->db[id1]);\n    touchAllWatchedKeysInDb(g_pserver->db[id1], g_pserver->db[id2]);\n    scanDatabaseForReadyLists(g_pserver->db[id2]);\n    touchAllWatchedKeysInDb(g_pserver->db[id2], g_pserver->db[id1]);\n    return C_OK;\n}\n\n/* SWAPDB db1 db2 */\nvoid swapdbCommand(client *c) {\n    int id1, id2;\n\n    listNode *ln;\n    listIter li;\n    client *cl;\n\n    /* Not allowed in cluster mode: we have just DB 0 there. */\n    if (g_pserver->cluster_enabled) {\n        addReplyError(c,\"SWAPDB is not allowed in cluster mode\");\n        return;\n    }\n\n    /* Get the two DBs indexes. */\n    if (getIntFromObjectOrReply(c, c->argv[1], &id1,\n        \"invalid first DB index\") != C_OK)\n        return;\n\n    if (getIntFromObjectOrReply(c, c->argv[2], &id2,\n        \"invalid second DB index\") != C_OK)\n        return;\n\n    /* Swap... */\n    if (dbSwapDatabases(id1,id2) == C_ERR) {\n        addReplyError(c,\"DB index is out of range\");\n        return;\n    } else {\n        RedisModuleSwapDbInfo si = {REDISMODULE_SWAPDBINFO_VERSION,(int32_t)id1,(int32_t)id2};\n        moduleFireServerEvent(REDISMODULE_EVENT_SWAPDB,0,&si);\n        g_pserver->dirty++;\n\n        // Persist the databse index to storage dbid mapping into FLASH for later recovery.\n        if (g_pserver->m_pstorageFactory != nullptr && g_pserver->metadataDb != nullptr) {\n            std::string dbid_key = \"db-\" + std::to_string(id1);\n            g_pserver->metadataDb->insert(dbid_key.c_str(), dbid_key.length(), &g_pserver->db[id1]->storage_id, sizeof(g_pserver->db[id1]->storage_id), true);\n\n            dbid_key = \"db-\" + std::to_string(id2);\n            g_pserver->metadataDb->insert(dbid_key.c_str(), dbid_key.length(), &g_pserver->db[id2]->storage_id, sizeof(g_pserver->db[id2]->storage_id), true);\n        }\n        addReply(c,shared.ok);\n        \n        listRewind(g_pserver->clients,&li);\n        while ((ln = listNext(&li)) != NULL) {\n            cl = reinterpret_cast<struct client*>(listNodeValue(ln));\n            std::unique_lock<decltype(cl->lock)> lock(cl->lock);\n            if (cl->db->id == g_pserver->db[id1]->id) {\n                cl->db = g_pserver->db[id2];\n                updateDBWatchedKey(id1, cl);\n            }\n            else if (cl->db->id == g_pserver->db[id2]->id) {\n                cl->db = g_pserver->db[id1];\n                updateDBWatchedKey(id2, cl);\n            }\n        }\n    }\n}\n\n/*-----------------------------------------------------------------------------\n * Expires API\n *----------------------------------------------------------------------------*/\nint removeExpire(redisDb *db, robj *key) {\n    auto itr = db->find(key);\n    return db->removeExpire(key, itr);\n}\nint redisDbPersistentData::removeExpire(robj *key, dict_iter itr) {\n    /* An expire may only be removed if there is a corresponding entry in the\n     * main dict. Otherwise, the key will never be freed. */\n    serverAssertWithInfo(NULL,key,itr != nullptr);\n\n    robj *val = itr.val();\n    if (!val->FExpires())\n        return 0;\n\n    trackkey(key, true /* fUpdate */);\n    val->SetFExpires(false);\n    serverAssert(m_numexpires > 0);\n    m_numexpires--;\n    return 1;\n}\n\nint redisDbPersistentData::removeSubkeyExpire(robj *key, robj *subkey) {\n    auto de = find(szFromObj(key));\n    serverAssertWithInfo(NULL,key,de != nullptr);\n\n    robj *val = de.val();\n    if (!val->FExpires())\n        return 0;\n\n    if (!val->expire.FFat())\n        return 0;\n\n    int found = 0;\n    for (auto subitr : val->expire)\n    {\n        if (subitr.subkey() == nullptr)\n            continue;\n        if (sdscmp((sds)subitr.subkey(), szFromObj(subkey)) == 0)\n        {\n            val->expire.erase(subitr);\n            found = 1;\n            break;\n        }\n    }\n\n    if (val->expire.pfatentry()->size() == 0)\n        this->removeExpire(key, de);\n\n    return found;\n}\n\n/* Set an expire to the specified key. If the expire is set in the context\n * of an user calling a command 'c' is the client, otherwise 'c' is set\n * to NULL. The 'when' parameter is the absolute unix time in milliseconds\n * after which the key will no longer be considered valid. */\nvoid setExpire(client *c, redisDb *db, robj *key, robj *subkey, long long when) {\n    serverAssert(GlobalLocksAcquired());\n\n    /* Update TTL stats (exponential moving average) */\n    /*  Note: We never have to update this on expiry since we reduce it by the current elapsed time here */\n    mstime_t now;\n    __atomic_load(&g_pserver->mstime, &now, __ATOMIC_ACQUIRE);\n    db->avg_ttl -= (now - db->last_expire_set); // reduce the TTL by the time that has elapsed\n    if (db->expireSize() == 0)\n        db->avg_ttl = 0;\n    else\n        db->avg_ttl -= db->avg_ttl / db->expireSize(); // slide one entry out the window\n    if (db->avg_ttl < 0)\n        db->avg_ttl = 0;    // TTLs are never negative\n    db->avg_ttl += (double)(when-now) / (db->expireSize()+1);    // add the new entry\n    db->last_expire_set = now;\n\n    /* Update the expire set */\n    db->setExpire(key, subkey, when);\n\n    int writable_slave = listLength(g_pserver->masters) && g_pserver->repl_slave_ro == 0 && !g_pserver->fActiveReplica;\n    if (c && writable_slave && !(c->flags & CLIENT_MASTER))\n        rememberSlaveKeyWithExpire(db,key);\n}\n\nredisDb::~redisDb()\n{\n    dictRelease(watched_keys);\n    dictRelease(ready_keys);\n    dictRelease(blocking_keys);\n    listRelease(defrag_later);\n}\n\nvoid setExpire(client *c, redisDb *db, robj *key, expireEntry &&e)\n{\n    serverAssert(GlobalLocksAcquired());\n\n    /* Reuse the sds from the main dict in the expire dict */\n    auto kde = db->find(key);\n    serverAssertWithInfo(NULL,key,kde != NULL);\n\n    if (kde.val()->getrefcount(std::memory_order_relaxed) == OBJ_SHARED_REFCOUNT)\n    {\n        // shared objects cannot have the expire bit set, create a real object\n        db->updateValue(kde, dupStringObject(kde.val()));\n    }\n\n    if (kde.val()->FExpires())\n        removeExpire(db, key);\n\n    db->setExpire(kde.key(), std::move(e));\n\n    int writable_slave = listLength(g_pserver->masters) && g_pserver->repl_slave_ro == 0 && !g_pserver->fActiveReplica;\n    if (c && writable_slave && !(c->flags & CLIENT_MASTER))\n        rememberSlaveKeyWithExpire(db,key);\n}\n\n/* Return the expire time of the specified key, or null if no expire\n * is associated with this key (i.e. the key is non volatile) */\nexpireEntry *redisDbPersistentDataSnapshot::getExpire(const char *key) {\n    /* No expire? return ASAP */\n    if (expireSize() == 0)\n        return nullptr;\n\n    auto itr = find_cached_threadsafe(key);\n    if (itr == end())\n        return nullptr;\n    if (!itr.val()->FExpires())\n        return nullptr;\n    return &itr.val()->expire;\n}\n\nconst expireEntry *redisDbPersistentDataSnapshot::getExpire(const char *key) const\n{\n    return const_cast<redisDbPersistentDataSnapshot*>(this)->getExpire(key);\n}\n\n/* Delete the specified expired key and propagate expire. */\nvoid deleteExpiredKeyAndPropagate(redisDb *db, robj *keyobj) {\n    mstime_t expire_latency;\n    latencyStartMonitor(expire_latency);\n    if (g_pserver->lazyfree_lazy_expire) {\n        dbAsyncDelete(db,keyobj);\n    } else {\n        dbSyncDelete(db,keyobj);\n    }\n    latencyEndMonitor(expire_latency);\n    latencyAddSampleIfNeeded(\"expire-del\",expire_latency);\n    notifyKeyspaceEvent(NOTIFY_EXPIRED,\"expired\",keyobj,db->id);\n    signalModifiedKey(NULL,db,keyobj);\n    propagateExpire(db,keyobj,g_pserver->lazyfree_lazy_expire);\n    g_pserver->stat_expiredkeys++;\n}\n\n/* Propagate expires into slaves and the AOF file.\n * When a key expires in the master, a DEL operation for this key is sent\n * to all the slaves and the AOF file if enabled.\n *\n * This way the key expiry is centralized in one place, and since both\n * AOF and the master->replica link guarantee operation ordering, everything\n * will be consistent even if we allow write operations against expiring\n * keys. */\nvoid propagateExpire(redisDb *db, robj *key, int lazy) {\n    serverAssert(GlobalLocksAcquired());\n\n    robj *argv[2];\n\n    argv[0] = lazy ? shared.unlink : shared.del;\n    argv[1] = key;\n    incrRefCount(argv[0]);\n    incrRefCount(argv[1]);\n\n    /* If the master decided to expire a key we must propagate it to replicas no matter what..\n     * Even if module executed a command without asking for propagation. */\n    int prev_replication_allowed = g_pserver->replication_allowed;\n    g_pserver->replication_allowed = 1;\n    if (!g_pserver->fActiveReplica) // Active replicas do their own expiries, do not propogate\n        propagate(cserver.delCommand,db->id,argv,2,PROPAGATE_AOF|PROPAGATE_REPL);\n    g_pserver->replication_allowed = prev_replication_allowed;\n\n    decrRefCount(argv[0]);\n    decrRefCount(argv[1]);\n}\n\nvoid propagateSubkeyExpire(redisDb *db, int type, robj *key, robj *subkey)\n{\n    robj *argv[3];\n    redisCommand *cmd = nullptr;\n    switch (type)\n    {\n    case OBJ_SET:\n        argv[0] = shared.srem;\n        argv[1] = key;\n        argv[2] = subkey;\n        cmd = cserver.sremCommand;\n        break;\n\n    case OBJ_HASH:\n        argv[0] = shared.hdel;\n        argv[1] = key;\n        argv[2] = subkey;\n        cmd = cserver.hdelCommand;\n        break;\n\n    case OBJ_ZSET:\n        argv[0] = shared.zrem;\n        argv[1] = key;\n        argv[2] = subkey;\n        cmd = cserver.zremCommand;\n        break;\n\n    case OBJ_CRON:\n        return; // CRON jobs replicate in their own handler\n\n    default:\n        serverPanic(\"Unknown subkey type\");\n    }\n\n    if (g_pserver->aof_state != AOF_OFF)\n        feedAppendOnlyFile(cmd,db->id,argv,3);\n    // Active replicas do their own expiries, do not propogate\n    if (!g_pserver->fActiveReplica)\n        replicationFeedSlaves(g_pserver->slaves,db->id,argv,3);\n}\n\n/* Check if the key is expired. Note, this does not check subexpires */\nint keyIsExpired(const redisDbPersistentDataSnapshot *db, robj *key) {\n    /* Don't expire anything while loading. It will be done later. */\n    if (g_pserver->loading) return 0;\n    \n    const expireEntry *pexpire = db->getExpire(key);\n    mstime_t now;\n    long long when;\n\n    if (pexpire == nullptr) return 0; /* No expire for this key */\n\n    if (!pexpire->FGetPrimaryExpire(&when))\n        return 0;\n\n    /* If we are in the context of a Lua script, we pretend that time is\n     * blocked to when the Lua script started. This way a key can expire\n     * only the first time it is accessed and not in the middle of the\n     * script execution, making propagation to slaves / AOF consistent.\n     * See issue #1525 on Github for more information. */\n    if (g_pserver->lua_caller) {\n        now = g_pserver->lua_time_snapshot;\n    }\n    /* If we are in the middle of a command execution, we still want to use\n     * a reference time that does not change: in that case we just use the\n     * cached time, that we update before each call in the call() function.\n     * This way we avoid that commands such as RPOPLPUSH or similar, that\n     * may re-open the same key multiple times, can invalidate an already\n     * open object in a next call, if the next call will see the key expired,\n     * while the first did not. */\n    else if (serverTL->fixed_time_expire > 0) {\n        __atomic_load(&g_pserver->mstime, &now, __ATOMIC_ACQUIRE);\n    }\n    /* For the other cases, we want to use the most fresh time we have. */\n    else {\n        now = mstime();\n    }\n\n    /* The key expired if the current (virtual or real) time is greater\n     * than the expire time of the key. */\n    return now > when;\n}\n\n/* This function is called when we are going to perform some operation\n * in a given key, but such key may be already logically expired even if\n * it still exists in the database. The main way this function is called\n * is via lookupKey*() family of functions.\n *\n * The behavior of the function depends on the replication role of the\n * instance, because replica instances do not expire keys, they wait\n * for DELs from the master for consistency matters. However even\n * slaves will try to have a coherent return value for the function,\n * so that read commands executed in the replica side will be able to\n * behave like if the key is expired even if still present (because the\n * master has yet to propagate the DEL).\n *\n * In masters as a side effect of finding a key which is expired, such\n * key will be evicted from the database. Also this may trigger the\n * propagation of a DEL/UNLINK command in AOF / replication stream.\n *\n * The return value of the function is 0 if the key is still valid,\n * otherwise the function returns 1 if the key is expired. */\nint expireIfNeeded(redisDb *db, robj *key) {\n    if (!keyIsExpired(db,key)) return 0;\n\n    /* If we are running in the context of a replica, instead of\n     * evicting the expired key from the database, we return ASAP:\n     * the replica key expiration is controlled by the master that will\n     * send us synthesized DEL operations for expired keys.\n     *\n     * Still we try to return the right information to the caller,\n     * that is, 0 if we think the key should be still valid, 1 if\n     * we think the key is expired at this time. */\n    if (listLength(g_pserver->masters) && !g_pserver->fActiveReplica) return 1;\n\n    /* If clients are paused, we keep the current dataset constant,\n     * but return to the client what we believe is the right state. Typically,\n     * at the end of the pause we will properly expire the key OR we will\n     * have failed over and the new primary will send us the expire. */\n    if (checkClientPauseTimeoutAndReturnIfPaused()) return 1;\n\n    /* Delete the key */\n    deleteExpiredKeyAndPropagate(db,key);\n    return 1;\n}\n\n/* -----------------------------------------------------------------------------\n * API to get key arguments from commands\n * ---------------------------------------------------------------------------*/\n\n/* Prepare the getKeysResult struct to hold numkeys, either by using the\n * pre-allocated keysbuf or by allocating a new array on the heap.\n *\n * This function must be called at least once before starting to populate\n * the result, and can be called repeatedly to enlarge the result array.\n */\nint *getKeysPrepareResult(getKeysResult *result, int numkeys) {\n    /* GETKEYS_RESULT_INIT initializes keys to NULL, point it to the pre-allocated stack\n     * buffer here. */\n    if (!result->keys) {\n        serverAssert(!result->numkeys);\n        result->keys = result->keysbuf;\n    }\n\n    /* Resize if necessary */\n    if (numkeys > result->size) {\n        if (result->keys != result->keysbuf) {\n            /* We're not using a static buffer, just (re)alloc */\n            result->keys = (int*)zrealloc(result->keys, numkeys * sizeof(int));\n        } else {\n            /* We are using a static buffer, copy its contents */\n            result->keys = (int*)zmalloc(numkeys * sizeof(int));\n            if (result->numkeys)\n                memcpy(result->keys, result->keysbuf, result->numkeys * sizeof(int));\n        }\n        result->size = numkeys;\n    }\n\n    return result->keys;\n}\n\n/* The base case is to use the keys position as given in the command table\n * (firstkey, lastkey, step). */\nint getKeysUsingCommandTable(struct redisCommand *cmd,robj **argv, int argc, getKeysResult *result) {\n    int j, i = 0, last, *keys;\n    UNUSED(argv);\n\n    if (cmd->firstkey == 0) {\n        result->numkeys = 0;\n        return 0;\n    }\n\n    last = cmd->lastkey;\n    if (last < 0) last = argc+last;\n\n    int count = ((last - cmd->firstkey)+1);\n    keys = getKeysPrepareResult(result, count);\n\n    for (j = cmd->firstkey; j <= last; j += cmd->keystep) {\n        if (j >= argc) {\n            /* Modules commands, and standard commands with a not fixed number\n             * of arguments (negative arity parameter) do not have dispatch\n             * time arity checks, so we need to handle the case where the user\n             * passed an invalid number of arguments here. In this case we\n             * return no keys and expect the command implementation to report\n             * an arity or syntax error. */\n            if (cmd->flags & CMD_MODULE || cmd->arity < 0) {\n                result->numkeys = 0;\n                return 0;\n            } else {\n                serverPanic(\"KeyDB built-in command declared keys positions not matching the arity requirements.\");\n            }\n        }\n        keys[i++] = j;\n    }\n    result->numkeys = i;\n    return i;\n}\n\n/* Return all the arguments that are keys in the command passed via argc / argv.\n *\n * The command returns the positions of all the key arguments inside the array,\n * so the actual return value is a heap allocated array of integers. The\n * length of the array is returned by reference into *numkeys.\n *\n * 'cmd' must be point to the corresponding entry into the redisCommand\n * table, according to the command name in argv[0].\n *\n * This function uses the command table if a command-specific helper function\n * is not required, otherwise it calls the command-specific function. */\nint getKeysFromCommand(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result) {\n    if (cmd->flags & CMD_MODULE_GETKEYS) {\n        return moduleGetCommandKeysViaAPI(cmd,argv,argc,result);\n    } else if (!(cmd->flags & CMD_MODULE) && cmd->getkeys_proc) {\n        return cmd->getkeys_proc(cmd,argv,argc,result);\n    } else {\n        return getKeysUsingCommandTable(cmd,argv,argc,result);\n    }\n}\n\n/* Free the result of getKeysFromCommand. */\nvoid getKeysFreeResult(getKeysResult *result) {\n    if (result && result->keys != result->keysbuf)\n        zfree(result->keys);\n}\n\n/* Helper function to extract keys from following commands:\n * COMMAND [destkey] <num-keys> <key> [...] <key> [...] ... <options>\n *\n * eg:\n * ZUNION <num-keys> <key> <key> ... <key> <options>\n * ZUNIONSTORE <destkey> <num-keys> <key> <key> ... <key> <options>\n *\n * 'storeKeyOfs': destkey index, 0 means destkey not exists.\n * 'keyCountOfs': num-keys index.\n * 'firstKeyOfs': firstkey index.\n * 'keyStep': the interval of each key, usually this value is 1.\n * */\nint genericGetKeys(int storeKeyOfs, int keyCountOfs, int firstKeyOfs, int keyStep,\n                    robj **argv, int argc, getKeysResult *result) {\n    int i, num, *keys;\n\n    num = atoi(szFromObj(argv[keyCountOfs]));\n    /* Sanity check. Don't return any key if the command is going to\n     * reply with syntax error. (no input keys). */\n    if (num < 1 || num > (argc - firstKeyOfs)/keyStep) {\n        result->numkeys = 0;\n        return 0;\n    }\n\n    int numkeys = storeKeyOfs ? num + 1 : num;\n    keys = getKeysPrepareResult(result, numkeys);\n    result->numkeys = numkeys;\n\n    /* Add all key positions for argv[firstKeyOfs...n] to keys[] */\n    for (i = 0; i < num; i++) keys[i] = firstKeyOfs+(i*keyStep);\n\n    if (storeKeyOfs) keys[num] = storeKeyOfs;\n    return result->numkeys;\n}\n\nint zunionInterDiffStoreGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result) {\n    UNUSED(cmd);\n    return genericGetKeys(1, 2, 3, 1, argv, argc, result);\n}\n\nint zunionInterDiffGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result) {\n    UNUSED(cmd);\n    return genericGetKeys(0, 1, 2, 1, argv, argc, result);\n}\n\nint evalGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result) {\n    UNUSED(cmd);\n    return genericGetKeys(0, 2, 3, 1, argv, argc, result);\n}\n\n/* Helper function to extract keys from the SORT command.\n *\n * SORT <sort-key> ... STORE <store-key> ...\n *\n * The first argument of SORT is always a key, however a list of options\n * follow in SQL-alike style. Here we parse just the minimum in order to\n * correctly identify keys in the \"STORE\" option. */\nint sortGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result) {\n    int i, j, num, *keys, found_store = 0;\n    UNUSED(cmd);\n\n    num = 0;\n    keys = getKeysPrepareResult(result, 2); /* Alloc 2 places for the worst case. */\n    keys[num++] = 1; /* <sort-key> is always present. */\n\n    /* Search for STORE option. By default we consider options to don't\n     * have arguments, so if we find an unknown option name we scan the\n     * next. However there are options with 1 or 2 arguments, so we\n     * provide a list here in order to skip the right number of args. */\n    struct {\n        const char *name;\n        int skip;\n    } skiplist[] = {\n        {\"limit\", 2},\n        {\"get\", 1},\n        {\"by\", 1},\n        {NULL, 0} /* End of elements. */\n    };\n\n    for (i = 2; i < argc; i++) {\n        for (j = 0; skiplist[j].name != NULL; j++) {\n            if (!strcasecmp(szFromObj(argv[i]),skiplist[j].name)) {\n                i += skiplist[j].skip;\n                break;\n            } else if (!strcasecmp(szFromObj(argv[i]),\"store\") && i+1 < argc) {\n                /* Note: we don't increment \"num\" here and continue the loop\n                 * to be sure to process the *last* \"STORE\" option if multiple\n                 * ones are provided. This is same behavior as SORT. */\n                found_store = 1;\n                keys[num] = i+1; /* <store-key> */\n                break;\n            }\n        }\n    }\n    result->numkeys = num + found_store;\n    return result->numkeys;\n}\n\nint migrateGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result) {\n    int i, num, first, *keys;\n    UNUSED(cmd);\n\n    /* Assume the obvious form. */\n    first = 3;\n    num = 1;\n\n    /* But check for the extended one with the KEYS option. */\n    if (argc > 6) {\n        for (i = 6; i < argc; i++) {\n            if (!strcasecmp(szFromObj(argv[i]),\"keys\") &&\n                sdslen(szFromObj(argv[3])) == 0)\n            {\n                first = i+1;\n                num = argc-first;\n                break;\n            }\n        }\n    }\n\n    keys = getKeysPrepareResult(result, num);\n    for (i = 0; i < num; i++) keys[i] = first+i;\n    result->numkeys = num;\n    return num;\n}\n\n/* Helper function to extract keys from following commands:\n * GEORADIUS key x y radius unit [WITHDIST] [WITHHASH] [WITHCOORD] [ASC|DESC]\n *                             [COUNT count] [STORE key] [STOREDIST key]\n * GEORADIUSBYMEMBER key member radius unit ... options ... */\nint georadiusGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result) {\n    int i, num, *keys;\n    UNUSED(cmd);\n\n    /* Check for the presence of the stored key in the command */\n    int stored_key = -1;\n    for (i = 5; i < argc; i++) {\n        char *arg = szFromObj(argv[i]);\n        /* For the case when user specifies both \"store\" and \"storedist\" options, the\n         * second key specified would override the first key. This behavior is kept\n         * the same as in georadiusCommand method.\n         */\n        if ((!strcasecmp(arg, \"store\") || !strcasecmp(arg, \"storedist\")) && ((i+1) < argc)) {\n            stored_key = i+1;\n            i++;\n        }\n    }\n    num = 1 + (stored_key == -1 ? 0 : 1);\n\n    /* Keys in the command come from two places:\n     * argv[1] = key,\n     * argv[5...n] = stored key if present\n     */\n    keys = getKeysPrepareResult(result, num);\n\n    /* Add all key positions to keys[] */\n    keys[0] = 1;\n    if(num > 1) {\n         keys[1] = stored_key;\n    }\n    result->numkeys = num;\n    return num;\n}\n\n/* LCS ... [KEYS <key1> <key2>] ... */\nint lcsGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result) {\n    int i;\n    int *keys = getKeysPrepareResult(result, 2);\n    UNUSED(cmd);\n\n    /* We need to parse the options of the command in order to check for the\n     * \"KEYS\" argument before the \"STRINGS\" argument. */\n    for (i = 1; i < argc; i++) {\n        char *arg = szFromObj(argv[i]);\n        int moreargs = (argc-1) - i;\n\n        if (!strcasecmp(arg, \"strings\")) {\n            break;\n        } else if (!strcasecmp(arg, \"keys\") && moreargs >= 2) {\n            keys[0] = i+1;\n            keys[1] = i+2;\n            result->numkeys = 2;\n            return result->numkeys;\n        }\n    }\n    result->numkeys = 0;\n    return result->numkeys;\n}\n\n/* Helper function to extract keys from memory command.\n * MEMORY USAGE <key> */\nint memoryGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result) {\n    UNUSED(cmd);\n\n    getKeysPrepareResult(result, 1);\n    if (argc >= 3 && !strcasecmp(szFromObj(argv[1]),\"usage\")) {\n        result->keys[0] = 2;\n        result->numkeys = 1;\n        return result->numkeys;\n    }\n    result->numkeys = 0;\n    return 0;\n}\n\n/* XREAD [BLOCK <milliseconds>] [COUNT <count>] [GROUP <groupname> <ttl>]\n *       STREAMS key_1 key_2 ... key_N ID_1 ID_2 ... ID_N */\nint xreadGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result) {\n    int i, num = 0, *keys;\n    UNUSED(cmd);\n\n    /* We need to parse the options of the command in order to seek the first\n     * \"STREAMS\" string which is actually the option. This is needed because\n     * \"STREAMS\" could also be the name of the consumer group and even the\n     * name of the stream key. */\n    int streams_pos = -1;\n    for (i = 1; i < argc; i++) {\n        char *arg = szFromObj(argv[i]);\n        if (!strcasecmp(arg, \"block\")) {\n            i++; /* Skip option argument. */\n        } else if (!strcasecmp(arg, \"count\")) {\n            i++; /* Skip option argument. */\n        } else if (!strcasecmp(arg, \"group\")) {\n            i += 2; /* Skip option argument. */\n        } else if (!strcasecmp(arg, \"noack\")) {\n            /* Nothing to do. */\n        } else if (!strcasecmp(arg, \"streams\")) {\n            streams_pos = i;\n            break;\n        } else {\n            break; /* Syntax error. */\n        }\n    }\n    if (streams_pos != -1) num = argc - streams_pos - 1;\n\n    /* Syntax error. */\n    if (streams_pos == -1 || num == 0 || num % 2 != 0) {\n        result->numkeys = 0;\n        return 0;\n    }\n    num /= 2; /* We have half the keys as there are arguments because\n                 there are also the IDs, one per key. */\n\n    keys = getKeysPrepareResult(result, num);\n    for (i = streams_pos+1; i < argc-num; i++) keys[i-streams_pos-1] = i;\n    result->numkeys = num;\n    return num;\n}\n\n/* Slot to Key API. This is used by Redis Cluster in order to obtain in\n * a fast way a key that belongs to a specified hash slot. This is useful\n * while rehashing the cluster and in other conditions when we need to\n * understand if we have keys for a given hash slot. */\nvoid slotToKeyUpdateKey(sds key, int add) {\n    slotToKeyUpdateKeyCore(key, sdslen(key), add);\n}\n\nvoid slotToKeyUpdateKeyCore(const char *key, size_t keylen, int add) {\n    serverAssert(GlobalLocksAcquired());\n\n    unsigned int hashslot = keyHashSlot(key,keylen);\n    g_pserver->cluster->slots_keys_count[hashslot] += add ? 1 : -1;\n\n    if (g_pserver->m_pstorageFactory == nullptr) {\n        unsigned char buf[64];\n        unsigned char *indexed = buf;\n\n        if (keylen+2 > 64) indexed = (unsigned char*)zmalloc(keylen+2, MALLOC_SHARED);\n        indexed[0] = (hashslot >> 8) & 0xff;\n        indexed[1] = hashslot & 0xff;\n        memcpy(indexed+2,key,keylen);\n        int fModified = false;\n        if (add) {\n            fModified = raxInsert(g_pserver->cluster->slots_to_keys,indexed,keylen+2,NULL,NULL);\n        } else {\n            fModified = raxRemove(g_pserver->cluster->slots_to_keys,indexed,keylen+2,NULL);\n        }\n        // This assert is disabled when a snapshot depth is >0 because prepOverwriteForSnapshot will add in a tombstone,\n        //  this prevents ensure from adding the key to the dictionary which means the caller isn't aware we're already tracking\n        //  the key.\n        serverAssert(fModified || g_pserver->db[0]->snapshot_depth() > 0);\n        if (indexed != buf) zfree(indexed);\n    }\n}\n\nvoid slotToKeyAdd(sds key) {\n    slotToKeyUpdateKey(key,1);\n}\n\nvoid slotToKeyDel(sds key) {\n    slotToKeyUpdateKey(key,0);\n}\n\n/* Release the radix tree mapping Redis Cluster keys to slots. If 'async'\n * is true, we release it asynchronously. */\nvoid freeSlotsToKeysMap(rax *rt, int async) {\n    if (async) {\n        freeSlotsToKeysMapAsync(rt);\n    } else {\n        raxFree(rt);\n    }\n}\n\n/* Empty the slots-keys map of Redis CLuster by creating a new empty one and\n * freeing the old one. */\nvoid slotToKeyFlush(int async) {\n    rax *old = g_pserver->cluster->slots_to_keys;\n\n    g_pserver->cluster->slots_to_keys = raxNew();\n    memset(g_pserver->cluster->slots_keys_count,0,\n           sizeof(g_pserver->cluster->slots_keys_count));\n    freeSlotsToKeysMap(old, async);\n}\n\n/* Populate the specified array of objects with keys in the specified slot.\n * New objects are returned to represent keys, it's up to the caller to\n * decrement the reference count to release the keys names. */\nunsigned int getKeysInSlot(unsigned int hashslot, robj **keys, unsigned int count) {\n    if (g_pserver->m_pstorageFactory != nullptr) {\n        int j = 0;\n        g_pserver->db[0]->getStorageCache()->enumerate_hashslot([&](const char *key, size_t cchKey, const void *, size_t )->bool {\n            keys[j++] = createStringObject(key, cchKey);\n            return --count;\n        }, hashslot);\n        return j;\n    } else {\n        raxIterator iter;\n        int j = 0;\n        unsigned char indexed[2];\n\n        indexed[0] = (hashslot >> 8) & 0xff;\n        indexed[1] = hashslot & 0xff;\n        raxStart(&iter,g_pserver->cluster->slots_to_keys);\n        raxSeek(&iter,\">=\",indexed,2);\n        while(count-- && raxNext(&iter)) {\n            if (iter.key[0] != indexed[0] || iter.key[1] != indexed[1]) break;\n            keys[j++] = createStringObject((char*)iter.key+2,iter.key_len-2);\n        }\n        raxStop(&iter);\n        return j;\n    }\n}\n\n/* Remove all the keys in the specified hash slot.\n * The number of removed items is returned. */\nunsigned int delKeysInSlot(unsigned int hashslot) {\n    serverAssert(GlobalLocksAcquired());\n    if (g_pserver->m_pstorageFactory != nullptr) {\n        int j = 0;\n        g_pserver->db[0]->getStorageCache()->enumerate_hashslot([&](const char *key, size_t cchKey, const void *, size_t )->bool {\n            robj *keyobj = createStringObject(key, cchKey);\n            dbDelete(g_pserver->db[0], keyobj);\n            decrRefCount(keyobj);\n            j++;\n            return true;\n        }, hashslot);\n        return j;\n    } else {\n        raxIterator iter;\n        int j = 0;\n        unsigned char indexed[2];\n\n        indexed[0] = (hashslot >> 8) & 0xff;\n        indexed[1] = hashslot & 0xff;\n        raxStart(&iter,g_pserver->cluster->slots_to_keys);\n        while(g_pserver->cluster->slots_keys_count[hashslot]) {\n            raxSeek(&iter,\">=\",indexed,2);\n            raxNext(&iter);\n\n            auto count = g_pserver->cluster->slots_keys_count[hashslot];\n            robj *key = createStringObject((char*)iter.key+2,iter.key_len-2);\n            dbDelete(g_pserver->db[0],key);\n            serverAssert(count > g_pserver->cluster->slots_keys_count[hashslot]);   // we should have deleted something or we will be in an infinite loop\n            decrRefCount(key);\n            j++;\n        }\n        raxStop(&iter);\n        return j;\n    }\n}\n\nunsigned int countKeysInSlot(unsigned int hashslot) {\n    return g_pserver->cluster->slots_keys_count[hashslot];\n}\n\nvoid redisDbPersistentData::initialize()\n{\n    m_pdbSnapshot = nullptr;\n    m_pdict = dictCreate(&dbDictType,this);\n    m_pdictTombstone = dictCreate(&dbTombstoneDictType,this);\n    m_fAllChanged = 0;\n    m_fTrackingChanges = 0;\n}\n\nvoid redisDbPersistentData::setStorageProvider(StorageCache *pstorage)\n{\n    serverAssert(m_spstorage == nullptr);\n    m_spstorage = std::unique_ptr<StorageCache>(pstorage);\n}\n\nvoid redisDbPersistentData::endStorageProvider()\n{\n    serverAssert(m_spstorage != nullptr);\n    m_spstorage.reset();\n}\n\nvoid clusterStorageLoadCallback(const char *rgchkey, size_t cch, void *)\n{\n    slotToKeyUpdateKeyCore(rgchkey, cch, true /*add*/);\n}\n\nvoid moduleLoadCallback(const char * rgchKey, size_t, void *data) {\n    redisObjectStack keyobj;\n    initStaticStringObject(keyobj, const_cast<char *>(rgchKey));\n    moduleNotifyKeyspaceEvent(NOTIFY_LOADED, \"loaded\", &keyobj, *(int *)data);\n}\n\nvoid moduleClusterLoadCallback(const char * rgchKey, size_t cchKey, void *data) {\n    clusterStorageLoadCallback(rgchKey, cchKey, data);\n    moduleLoadCallback(rgchKey, cchKey, data);\n}\n\nvoid redisDb::initialize(int id, int storage_id /* default no storage */)\n{\n    redisDbPersistentData::initialize();\n    this->blocking_keys = dictCreate(&keylistDictType,NULL);\n    this->ready_keys = dictCreate(&objectKeyPointerValueDictType,NULL);\n    this->watched_keys = dictCreate(&keylistDictType,NULL);\n    this->id = id;\n    this->storage_id = storage_id;\n    this->avg_ttl = 0;\n    this->last_expire_set = 0;\n    this->defrag_later = listCreate();\n    listSetFreeMethod(this->defrag_later,(void (*)(const void*))sdsfree);\n}\n\nvoid redisDb::storageProviderInitialize()\n{\n    if (g_pserver->m_pstorageFactory != nullptr)\n    {\n        IStorageFactory::key_load_iterator itr = g_pserver->cluster_enabled ? moduleClusterLoadCallback : moduleLoadCallback;\n        this->setStorageProvider(StorageCache::create(g_pserver->m_pstorageFactory, storage_id, itr, &id));\n    }\n}\n\nvoid redisDb::storageProviderDelete()\n{\n    if (g_pserver->m_pstorageFactory != nullptr)\n    {\n        this->endStorageProvider();\n    }\n}\n\nbool redisDbPersistentData::insert(char *key, robj *o, bool fAssumeNew, dict_iter *piterExisting)\n{\n    if (!fAssumeNew && (g_pserver->m_pstorageFactory != nullptr || m_pdbSnapshot != nullptr))\n        ensure(key);\n    dictEntry *de;\n    int res = dictAdd(m_pdict, key, o, &de);\n    if (!FImplies(fAssumeNew, res == DICT_OK)) {\n        serverLog(LL_WARNING,\n            \"Assumed new key %s existed in DB.\", key);\n    }\n    if (res == DICT_OK)\n    {\n#ifdef CHECKED_BUILD\n        if (m_pdbSnapshot != nullptr && m_pdbSnapshot->find_cached_threadsafe(key) != nullptr)\n        {\n            serverAssert(dictFind(m_pdictTombstone, key) != nullptr);\n        }\n#endif\n        if (o->FExpires())\n            ++m_numexpires;\n        trackkey(key, false /* fUpdate */);\n    }\n    else\n    {\n        if (piterExisting)\n            *piterExisting = dict_iter(m_pdict, de);\n    }\n    return (res == DICT_OK);\n}\n\n// This is a performance tool to prevent us copying over an object we're going to overwrite anyways\nvoid redisDbPersistentData::prepOverwriteForSnapshot(char *key)\n{\n    if (g_pserver->maxmemory_policy & MAXMEMORY_FLAG_LFU)\n        return;\n\n    if (m_pdbSnapshot != nullptr)\n    {\n        auto itr = m_pdbSnapshot->find_cached_threadsafe(key);\n        if (itr.key() != nullptr)\n        {\n            sds keyNew = sdsdupshared(itr.key());\n            if (dictAdd(m_pdictTombstone, keyNew, (void*)dictHashKey(m_pdict, key)) != DICT_OK)\n                sdsfree(keyNew);\n        }\n    }\n}\n\nvoid redisDbPersistentData::tryResize()\n{\n    if (htNeedsResize(m_pdict))\n        dictResize(m_pdict);\n}\n\nsize_t redisDb::clear(bool fAsync, void(callback)(void*))\n{\n    size_t removed = size();\n    if (fAsync) {\n        redisDbPersistentData::emptyDbAsync();\n    } else {\n        redisDbPersistentData::clear(callback);\n    }\n    expires_cursor = 0;\n    return removed;\n}\n\nvoid redisDbPersistentData::clear(void(callback)(void*))\n{\n    dictEmpty(m_pdict,callback);\n    if (m_fTrackingChanges)\n    {\n        dictEmpty(m_dictChanged, nullptr);\n        m_cnewKeysPending = 0;\n        m_fAllChanged++;\n    }\n    if (m_spstorage != nullptr)\n        m_spstorage->clear(callback);\n    dictEmpty(m_pdictTombstone,callback);\n\n    // To avoid issues with async rehash we completly free the old dict and create a fresh one\n    dictRelease(m_pdict);\n    dictRelease(m_pdictTombstone);\n    m_pdict = dictCreate(&dbDictType, this);\n    m_pdictTombstone = dictCreate(&dbTombstoneDictType, this);\n\n    m_pdbSnapshot = nullptr;\n    m_numexpires = 0;\n}\n\nvoid redisDbPersistentData::setExpire(robj *key, robj *subkey, long long when)\n{\n    /* Reuse the sds from the main dict in the expire dict */\n    dictEntry *kde = dictFind(m_pdict,ptrFromObj(key));\n    serverAssertWithInfo(NULL,key,kde != NULL);\n    trackkey(key, true /* fUpdate */);\n\n    robj *o = (robj*)dictGetVal(kde);\n    if (o->getrefcount(std::memory_order_relaxed) == OBJ_SHARED_REFCOUNT)\n    {\n        // shared objects cannot have the expire bit set, create a real object\n        dictSetVal(m_pdict, kde, dupStringObject(o));\n        o = (robj*)dictGetVal(kde);\n    }\n\n    const char *szSubKey = (subkey != nullptr) ? szFromObj(subkey) : nullptr;\n    if (o->FExpires()) {\n        o->expire.update(szSubKey, when);\n    }\n    else\n    {\n        expireEntry e(szSubKey, when);\n        o->expire = std::move(e);\n        o->SetFExpires(true);\n        ++m_numexpires;\n    }\n}\n\nvoid redisDbPersistentData::setExpire(const char *key, expireEntry &&e)\n{\n    trackkey(key, true /* fUpdate */);\n    auto itr = find(key);\n    if (!itr->FExpires())\n        m_numexpires++;\n    itr->expire = std::move(e);\n    itr->SetFExpires(true);\n}\n\nbool redisDb::FKeyExpires(const char *key)\n{\n    auto itr = find(key);\n    if (itr == end())\n        return false;\n    return itr->FExpires();\n}\n\nvoid redisDbPersistentData::updateValue(dict_iter itr, robj *val)\n{\n    trackkey(itr.key(), true /* fUpdate */);\n    dictSetVal(m_pdict, itr.de, val);\n}\n\nvoid redisDbPersistentData::ensure(const char *key)\n{\n    if (m_pdbSnapshot == nullptr && m_spstorage == nullptr)\n        return;\n    dictEntry *de = dictFind(m_pdict, key);\n    ensure(key, &de);\n}\n\nvoid redisDbPersistentData::ensure(const char *sdsKey, dictEntry **pde)\n{\n    serverAssert(sdsKey != nullptr);\n    serverAssert(FImplies(*pde != nullptr, dictGetVal(*pde) != nullptr));    // early versions set a NULL object, this is no longer valid\n    serverAssert(m_refCount == 0);\n    if (m_pdbSnapshot == nullptr && g_pserver->m_pstorageFactory == nullptr)\n        return;\n\n    // First see if the key can be obtained from a snapshot\n    if (*pde == nullptr && m_pdbSnapshot != nullptr)\n    {\n        dictEntry *deTombstone = dictFind(m_pdictTombstone, sdsKey);\n        if (deTombstone == nullptr)\n        {\n            auto itr = m_pdbSnapshot->find_cached_threadsafe(sdsKey);\n            if (itr == m_pdbSnapshot->end())\n                goto LNotFound;\n\n            sds keyNew = sdsdupshared(itr.key());   // note: we use the iterator's key because the sdsKey may not be a shared string\n            if (itr.val() != nullptr)\n            {\n                if (itr.val()->getrefcount(std::memory_order_relaxed) == OBJ_SHARED_REFCOUNT)\n                {\n                    dictAdd(m_pdict, keyNew, itr.val());\n                }\n                else\n                {\n                    sds strT = serializeStoredObject(itr.val());\n                    robj *objNew = deserializeStoredObject(strT, sdslen(strT));\n                    if (itr->FExpires()) {\n                        objNew->expire = itr->expire;\n                        objNew->SetFExpires(true);\n                    }\n                    sdsfree(strT);\n                    dictAdd(m_pdict, keyNew, objNew);\n                    serverAssert(objNew->getrefcount(std::memory_order_relaxed) == 1);\n                    serverAssert(mvccFromObj(objNew) == mvccFromObj(itr.val()));\n                }\n            }\n            else\n            {\n                dictAdd(m_pdict, keyNew, nullptr);\n            }\n            uint64_t hash = dictGetHash(m_pdict, sdsKey);\n            dictEntry **deT;\n            dictht *ht;\n            *pde = dictFindWithPrev(m_pdict, sdsKey, hash, &deT, &ht);\n            dictAdd(m_pdictTombstone, sdsdupshared(itr.key()), (void*)hash);\n        }\n    }\n    \nLNotFound:\n    // If we haven't found it yet check our storage engine\n    if (*pde == nullptr && m_spstorage != nullptr)\n    {\n        if (dictSize(m_pdict) != size())    // if all keys are cached then no point in looking up the database\n        {\n            robj *o = nullptr;\n            sds sdsNewKey = sdsdupshared(sdsKey);\n            std::unique_ptr<expireEntry> spexpire;\n            m_spstorage->retrieve((sds)sdsKey, [&](const char *, size_t, const void *data, size_t cb){\n                size_t offset = 0;\n                spexpire = deserializeExpire((const char*)data, cb, &offset);    \n                o = deserializeStoredObject(reinterpret_cast<const char*>(data) + offset, cb - offset);\n                serverAssert(o != nullptr);\n            });\n            \n            if (o != nullptr)\n            {\n                dictAdd(m_pdict, sdsNewKey, o);\n                \n                o->SetFExpires(spexpire != nullptr);\n                if (spexpire != nullptr) {\n                    o->expire = std::move(*spexpire);\n                }\n                g_pserver->stat_storage_provider_read_hits++;\n            } else {\n                sdsfree(sdsNewKey);\n                g_pserver->stat_storage_provider_read_misses++;\n            }\n\n            *pde = dictFind(m_pdict, sdsKey);\n        }\n    }\n}\n\nvoid redisDbPersistentData::storeKey(sds key, robj *o, bool fOverwrite)\n{\n    sds temp = serializeStoredObjectAndExpire(o);\n    m_spstorage->insert(key, temp, sdslen(temp), fOverwrite);\n    sdsfree(temp);\n}\n\nvoid redisDbPersistentData::storeDatabase()\n{\n    dictIterator *di = dictGetIterator(m_pdict);\n    dictEntry *de;\n    while ((de = dictNext(di)) != NULL) {\n        sds key = (sds)dictGetKey(de);\n        robj *o = (robj*)dictGetVal(de);\n        storeKey(key, o, false);\n    }\n    serverAssert(dictSize(m_pdict) == m_spstorage->count());\n    dictReleaseIterator(di);\n}\n\n/* static */ void redisDbPersistentData::serializeAndStoreChange(StorageCache *storage, redisDbPersistentData *db, const char *key, bool fUpdate)\n{\n    auto itr = db->find_cached_threadsafe(key);\n    if (itr == nullptr)\n        return;\n    robj *o = itr.val();\n    sds temp = serializeStoredObjectAndExpire(o);\n    storage->insert((sds)key, temp, sdslen(temp), fUpdate);\n    sdsfree(temp);\n}\n\nbool redisDbPersistentData::processChanges(bool fSnapshot)\n{\n    serverAssert(GlobalLocksAcquired());\n\n    --m_fTrackingChanges;\n    serverAssert(m_fTrackingChanges >= 0);\n\n    if (m_spstorage != nullptr)\n    {\n        if (!m_fAllChanged && dictSize(m_dictChanged) == 0 && m_cnewKeysPending == 0)\n            return false;\n        m_spstorage->beginWriteBatch();\n        serverAssert(m_pdbSnapshotStorageFlush == nullptr);\n        if (fSnapshot && !m_fAllChanged && dictSize(m_dictChanged) > 100)\n        {\n            // Do a snapshot based process if possible\n            m_pdbSnapshotStorageFlush = createSnapshot(getMvccTstamp(), true /* optional */);\n            if (m_pdbSnapshotStorageFlush)\n            {\n                if (m_dictChangedStorageFlush)\n                    dictRelease(m_dictChangedStorageFlush);\n                m_dictChangedStorageFlush = m_dictChanged;\n                m_dictChanged = dictCreate(&dictChangeDescType, nullptr);\n            }\n        }\n        \n        if (m_pdbSnapshotStorageFlush == nullptr)\n        {\n            if (m_fAllChanged)\n            {\n                if (dictSize(m_pdict) > 0 || m_spstorage->count() > 0) { // in some cases we may have pre-sized the StorageCache's dict, and we don't want clear to ruin it\n                    m_spstorage->clearAsync();\n                    storeDatabase();\n                }\n                m_fAllChanged = 0;\n            }\n            else\n            {\n                dictIterator *di = dictGetIterator(m_dictChanged);\n                dictEntry *de;\n                while ((de = dictNext(di)) != nullptr)\n                {\n                    serializeAndStoreChange(m_spstorage.get(), this, (const char*)dictGetKey(de), (bool)dictGetVal(de));\n                }\n                dictReleaseIterator(di);\n            }\n        }\n        dictEmpty(m_dictChanged, nullptr);\n        m_cnewKeysPending = 0;\n    }\n    return (m_spstorage != nullptr);\n}\n\nvoid redisDbPersistentData::processChangesAsync(std::atomic<int> &pendingJobs)\n{\n    ++pendingJobs;\n    serverAssert(!m_fAllChanged);\n    dictEmpty(m_dictChanged, nullptr);\n    dict *dictNew = dictCreate(&dbDictType, nullptr);\n    std::swap(dictNew, m_pdict);\n    m_cnewKeysPending = 0;\n    g_pserver->asyncworkqueue->AddWorkFunction([dictNew, this, &pendingJobs]{\n        dictIterator *di = dictGetIterator(dictNew);\n        dictEntry *de;\n        std::vector<sds> veckeys;\n        std::vector<size_t> veccbkeys;\n        std::vector<sds> vecvals;\n        std::vector<size_t> veccbvals;\n        while ((de = dictNext(di)) != nullptr)\n        {\n            robj *o = (robj*)dictGetVal(de);\n            sds temp = serializeStoredObjectAndExpire(o);\n            veckeys.push_back((sds)dictGetKey(de));\n            veccbkeys.push_back(sdslen((sds)dictGetKey(de)));\n            vecvals.push_back(temp);\n            veccbvals.push_back(sdslen(temp));\n        }\n        m_spstorage->bulkInsert(veckeys.data(), veccbkeys.data(), vecvals.data(), veccbvals.data(), veckeys.size());\n        for (auto val : vecvals)\n            sdsfree(val);\n        dictReleaseIterator(di);\n        dictRelease(dictNew);\n        --pendingJobs;\n    });\n}\n\n/* This function is to bulk insert directly to storage provider bypassing in memory, assumes rgKeys and rgVals are not sds strings */\nvoid redisDbPersistentData::bulkDirectStorageInsert(char **rgKeys, size_t *rgcbKeys, char **rgVals, size_t *rgcbVals, size_t celem)\n{\n    if (g_pserver->cluster_enabled) {\n        aeAcquireLock();\n        for (size_t i = 0; i < celem; i++) {\n            slotToKeyUpdateKeyCore(rgKeys[i], rgcbKeys[i], 1);\n        }\n        aeReleaseLock();\n    }\n    m_spstorage->bulkInsert(rgKeys, rgcbKeys, rgVals, rgcbVals, celem);\n}\n\nvoid redisDbPersistentData::commitChanges(const redisDbPersistentDataSnapshot **psnapshotFree)\n{\n    if (m_pdbSnapshotStorageFlush)\n    {\n        dictIterator *di = dictGetIterator(m_dictChangedStorageFlush);\n        dictEntry *de;\n        while ((de = dictNext(di)) != nullptr)\n        {\n            serializeAndStoreChange(m_spstorage.get(), (redisDbPersistentData*)m_pdbSnapshotStorageFlush, (const char*)dictGetKey(de), (bool)dictGetVal(de));\n        }\n        dictReleaseIterator(di);\n        dictRelease(m_dictChangedStorageFlush);\n        m_dictChangedStorageFlush = nullptr;\n        *psnapshotFree = m_pdbSnapshotStorageFlush;\n        m_pdbSnapshotStorageFlush = nullptr;\n    }\n    if (m_spstorage != nullptr)\n        m_spstorage->endWriteBatch();\n}\n\nredisDbPersistentData::~redisDbPersistentData()\n{\n    if (m_spdbSnapshotHOLDER != nullptr)\n        endSnapshot(m_spdbSnapshotHOLDER.get());\n    \n    //serverAssert(m_pdbSnapshot == nullptr);\n    serverAssert(m_refCount == 0);\n    //serverAssert(m_pdict->iterators == 0);\n    serverAssert(m_pdictTombstone == nullptr || m_pdictTombstone->pauserehash == 0);\n    dictRelease(m_pdict);\n    if (m_pdictTombstone)\n        dictRelease(m_pdictTombstone);\n\n    if (m_dictChanged)\n        dictRelease(m_dictChanged);\n    if (m_dictChangedStorageFlush)\n        dictRelease(m_dictChangedStorageFlush);    \n}\n\ndict_iter redisDbPersistentData::random()\n{\n    if (size() == 0)\n        return dict_iter(nullptr);\n    if (m_pdbSnapshot != nullptr && m_pdbSnapshot->size() > 0)\n    {\n        dict_iter iter(nullptr);\n        double pctInSnapshot = (double)m_pdbSnapshot->size() / (size() + m_pdbSnapshot->size());\n        double randval = (double)rand()/RAND_MAX;\n        if (randval <= pctInSnapshot)\n        {\n            iter = m_pdbSnapshot->random_cache_threadsafe();    // BUG: RANDOM doesn't consider keys not in RAM\n            ensure(iter.key());\n            dictEntry *de = dictFind(m_pdict, iter.key());\n            return dict_iter(m_pdict, de);\n        }\n    }\n    dictEntry *de = dictGetRandomKey(m_pdict);\n    if (de != nullptr)\n        ensure((const char*)dictGetKey(de), &de);\n    return dict_iter(m_pdict, de);\n}\n\nsize_t redisDbPersistentData::size(bool fCachedOnly) const \n{ \n    if (m_spstorage != nullptr && !m_fAllChanged && !fCachedOnly)\n        return m_spstorage->count() + m_cnewKeysPending;\n    \n    return dictSize(m_pdict) \n        + (m_pdbSnapshot ? (m_pdbSnapshot->size(fCachedOnly) - dictSize(m_pdictTombstone)) : 0); \n}\n\nbool redisDbPersistentData::removeCachedValue(const char *key, dictEntry **ppde)\n{\n    serverAssert(m_spstorage != nullptr);\n    // First ensure its not a pending key\n    if (m_spstorage != nullptr)\n        m_spstorage->batch_lock();\n    \n    dictEntry *de = dictFind(m_dictChanged, key);\n    if (de != nullptr)\n    {\n        if (m_spstorage != nullptr)\n            m_spstorage->batch_unlock();\n        return false; // can't evict\n    }\n\n    // since we write ASAP the database already has a valid copy so safe to delete\n    if (ppde != nullptr) {\n        *ppde = dictUnlink(m_pdict, key);\n    } else {\n        dictDelete(m_pdict, key);\n    }\n\n    if (m_spstorage != nullptr)\n        m_spstorage->batch_unlock();\n    \n    return true;\n}\n\nredisDbPersistentData::redisDbPersistentData() {\n    m_dictChanged = dictCreate(&dictChangeDescType, nullptr);\n}\n\nvoid redisDbPersistentData::trackChanges(bool fBulk, size_t sizeHint)\n{\n    m_fTrackingChanges.fetch_add(1, std::memory_order_relaxed);\n    if (fBulk)\n        m_fAllChanged.fetch_add(1, std::memory_order_acq_rel);\n\n    if (sizeHint > 0)\n        dictExpand(m_dictChanged, sizeHint, false);\n}\n\nvoid redisDbPersistentData::removeAllCachedValues()\n{\n    // First we have to flush the tracked changes\n    if (m_fTrackingChanges)\n    {\n        if (processChanges(false))\n            commitChanges();\n        trackChanges(false);\n    }\n\n    if (m_pdict->pauserehash == 0 && m_pdict->refcount == 1) {\n        dict *dT = m_pdict;\n        m_pdict = dictCreate(&dbDictType, this);\n        dictExpand(m_pdict, dictSize(dT)/2, false); // Make room for about half so we don't excessively rehash\n        g_pserver->asyncworkqueue->AddWorkFunction([dT]{\n            dictRelease(dT);\n        }, false);\n    } else {\n        dictEmpty(m_pdict, nullptr);\n    }\n}\n\nvoid redisDbPersistentData::disableKeyCache()\n{\n    if (m_spstorage == nullptr)\n        return;\n    m_spstorage->emergencyFreeCache();\n}\n\nbool redisDbPersistentData::keycacheIsEnabled()\n{\n    if (m_spstorage == nullptr)\n        return false;\n    return m_spstorage->keycacheIsEnabled();\n}\n\nvoid redisDbPersistentData::trackkey(const char *key, bool fUpdate)\n{\n    if (m_fTrackingChanges && !m_fAllChanged && m_spstorage) {\n        dictEntry *de = dictFind(m_dictChanged, key);\n        if (de == nullptr) {\n            dictAdd(m_dictChanged, (void*)sdsdupshared(key), (void*)fUpdate);\n            if (!fUpdate)\n                ++m_cnewKeysPending;\n        }\n    }\n}\n\nsds serializeExpire(const expireEntry *pexpire)\n{\n    sds str = sdsnewlen(nullptr, sizeof(unsigned));\n\n    if (pexpire == nullptr)\n    {\n        unsigned zero = 0;\n        memcpy(str, &zero, sizeof(unsigned));\n        return str;\n    }\n\n    auto &e = *pexpire;\n    unsigned celem = (unsigned)e.size();\n    memcpy(str, &celem, sizeof(unsigned));\n    \n    for (auto itr = e.begin(); itr != e.end(); ++itr)\n    {\n        unsigned subkeylen = itr.subkey() ? (unsigned)sdslen(itr.subkey()) : 0;\n        size_t strOffset = sdslen(str);\n        str = sdsgrowzero(str, sdslen(str) + sizeof(unsigned) + subkeylen + sizeof(long long));\n        memcpy(str + strOffset, &subkeylen, sizeof(unsigned));\n        if (itr.subkey())\n            memcpy(str + strOffset + sizeof(unsigned), itr.subkey(), subkeylen);\n        long long when = itr.when();\n        memcpy(str + strOffset + sizeof(unsigned) + subkeylen, &when, sizeof(when));\n    }\n    return str;\n}\n\nstd::unique_ptr<expireEntry> deserializeExpire(const char *str, size_t cch, size_t *poffset)\n{\n    unsigned celem;\n    if (cch < sizeof(unsigned))\n        throw \"Corrupt expire entry\";\n    memcpy(&celem, str, sizeof(unsigned));\n    std::unique_ptr<expireEntry> spexpire;\n\n    size_t offset = sizeof(unsigned);\n    for (; celem > 0; --celem)\n    {\n        serverAssert(cch > (offset+sizeof(unsigned)));\n        \n        unsigned subkeylen;\n        memcpy(&subkeylen, str + offset, sizeof(unsigned));\n        offset += sizeof(unsigned);\n\n        sds subkey = nullptr;\n        if (subkeylen != 0)\n        {\n            serverAssert(cch > (offset + subkeylen));\n            subkey = sdsnewlen(nullptr, subkeylen);\n            memcpy(subkey, str + offset, subkeylen);\n            offset += subkeylen;\n        }\n        \n        long long when;\n        serverAssert(cch >= (offset + sizeof(long long)));\n        memcpy(&when, str + offset, sizeof(long long));\n        offset += sizeof(long long);\n\n        if (spexpire == nullptr)\n            spexpire = std::make_unique<expireEntry>(subkey, when);\n        else\n            spexpire->update(subkey, when);\n\n        if (subkey)\n            sdsfree(subkey);\n    }\n    if (poffset != nullptr)\n        *poffset = offset;\n    return spexpire;\n}\n\nsds serializeStoredObjectAndExpire(robj_roptr o)\n{\n    const expireEntry *pexpire = o->FExpires() ?  &o->expire : nullptr;\n\n    sds str = serializeExpire(pexpire);\n    str = serializeStoredObject(o, str);\n    return str;\n}\n\nint dbnumFromDb(redisDb *db)\n{\n    for (int i = 0; i < cserver.dbnum; ++i)\n    {\n        if (g_pserver->db[i] == db)\n            return i;\n    }\n    serverPanic(\"invalid database pointer\");\n}\n\nvoid redisDbPersistentData::prefetchKeysAsync(client *c, parsed_command &command)\n{\n    if (m_spstorage == nullptr) {\n#if defined(__x86_64__) || defined(__i386__)\n        // We do a quick 'n dirty check for set & get.  Anything else is too slow.\n        //  Should the user do something weird like remap them then the worst that will\n        //  happen is we don't prefetch or we prefetch wrong data.  A mild perf hit, but\n        //  not dangerous\n        if (command.argc >= 2) {\n            const char *cmd = szFromObj(command.argv[0]);\n            if (!strcasecmp(cmd, \"set\") || !strcasecmp(cmd, \"get\")) {\n                if (c->db->m_spdbSnapshotHOLDER != nullptr)\n                    return; // this is dangerous enough without a snapshot around\n                auto h = dictSdsHash(szFromObj(command.argv[1]));\n                for (int iht = 0; iht < 2; ++iht) {\n                    auto hT = h & c->db->m_pdict->ht[iht].sizemask;\n                    dictEntry **table;\n                    __atomic_load(&c->db->m_pdict->ht[iht].table, &table, __ATOMIC_RELAXED);\n                    if (table != nullptr) {\n                        dictEntry *de;\n                        __atomic_load(&table[hT], &de, __ATOMIC_ACQUIRE);\n                        while (de != nullptr) {\n                            _mm_prefetch(dictGetKey(de), _MM_HINT_T2);\n                            __atomic_load(&de->next, &de, __ATOMIC_ACQUIRE);\n                        }\n                    }\n                    if (!dictIsRehashing(c->db->m_pdict))\n                        break;\n                }\n            }\n        }\n#endif\n        return;\n    }\n\n    AeLocker lock;\n\n    std::vector<robj*> veckeys;\n    lock.arm(c);\n    getKeysResult result = GETKEYS_RESULT_INIT;\n    auto cmd = lookupCommand(szFromObj(command.argv[0]));\n    if (cmd == nullptr)\n        return; // Bad command? It's not for us to judge, just bail\n    \n    if (command.argc < std::abs(cmd->arity))\n        return; // Invalid number of args\n    \n    int numkeys = getKeysFromCommand(cmd, command.argv, command.argc, &result);\n    for (int ikey = 0; ikey < numkeys; ++ikey)\n    {\n        robj *objKey = command.argv[result.keys[ikey]];\n        if (this->find_cached_threadsafe(szFromObj(objKey)) == nullptr)\n            veckeys.push_back(objKey);\n    }\n    lock.disarm();\n\n    getKeysFreeResult(&result);\n\n    std::vector<std::tuple<sds, robj*, std::unique_ptr<expireEntry>>> vecInserts;\n    for (robj *objKey : veckeys)\n    {\n        sds sharedKey = sdsdupshared((sds)szFromObj(objKey));\n        std::unique_ptr<expireEntry> spexpire;\n        robj *o = nullptr;\n        m_spstorage->retrieve((sds)szFromObj(objKey), [&](const char *, size_t, const void *data, size_t cb){\n                size_t offset = 0;\n                spexpire = deserializeExpire((const char*)data, cb, &offset);    \n                o = deserializeStoredObject(reinterpret_cast<const char*>(data) + offset, cb - offset);\n                serverAssert(o != nullptr);\n        });\n\n        if (o != nullptr) {\n            vecInserts.emplace_back(sharedKey, o, std::move(spexpire));\n        } else if (sharedKey != nullptr) {\n            sdsfree(sharedKey);\n        }\n    }\n\n    if (!vecInserts.empty()) {\n        lock.arm(c);\n        for (auto &tuple : vecInserts)\n        {\n            sds sharedKey = std::get<0>(tuple);\n            robj *o = std::get<1>(tuple);\n            std::unique_ptr<expireEntry> spexpire = std::move(std::get<2>(tuple));\n\n            if (o != nullptr)\n            {\n                if (this->find_cached_threadsafe(sharedKey) != nullptr)\n                {\n                    // While unlocked this was already ensured\n                    decrRefCount(o);\n                    sdsfree(sharedKey);\n                }\n                else\n                {\n                    if (spexpire != nullptr) {\n                        if (spexpire->when() < mstime()) {\n                            break;\n                        }\n                    }\n                    dictAdd(m_pdict, sharedKey, o);\n                    if (spexpire != nullptr)\n                        o->expire = std::move(*spexpire);\n                    o->SetFExpires(spexpire != nullptr);\n                }\n            }\n            else\n            {\n                if (sharedKey != nullptr)\n                    sdsfree(sharedKey); // BUG but don't bother crashing\n            }\n        }\n        lock.disarm();\n    }\n\n    return;\n}\n"
  },
  {
    "path": "src/debug.cpp",
    "content": "/*\n * Copyright (c) 2009-2020, Salvatore Sanfilippo <antirez at gmail dot com>\n * Copyright (c) 2020, Redis Labs, Inc\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#define NO_DEPRECATE_FREE 1 // we are required to call the real free() in this CU\n#include \"server.h\"\n#include \"sha1.h\"   /* SHA1 is used for DEBUG DIGEST */\n#include \"crc64.h\"\n#include \"cron.h\"\n#include \"bio.h\"\n\n#include <arpa/inet.h>\n#include <signal.h>\n#include <dlfcn.h>\n#include <fcntl.h>\n#include <unistd.h>\n\n#ifdef HAVE_BACKTRACE\n#include <execinfo.h>\n#ifndef __OpenBSD__\n#include <ucontext.h>\n#else\ntypedef ucontext_t sigcontext_t;\n#endif\n#include <cxxabi.h>\n#endif /* HAVE_BACKTRACE */\n\n//UNW_LOCAL_ONLY being set means we use libunwind for backtraces instead of execinfo\n#ifdef UNW_LOCAL_ONLY\n#include <libunwind.h>\n#include <cxxabi.h>\n#endif\n\n#ifdef __CYGWIN__\n#ifndef SA_ONSTACK\n#define SA_ONSTACK 0x08000000\n#endif\n#endif\n\nint g_fInCrash = false;\n\n#if defined(__APPLE__) && defined(__arm64__)\n#include <mach/mach.h>\n#endif\n\n/* Globals */\nstatic int bug_report_start = 0; /* True if bug report header was already logged. */\nstatic pthread_mutex_t bug_report_start_mutex = PTHREAD_MUTEX_INITIALIZER;\n\n/* Forward declarations */\nvoid bugReportStart(void);\nvoid printCrashReport(void);\nvoid bugReportEnd(int killViaSignal, int sig);\nvoid logStackTrace(void *eip, int uplevel);\nvoid getTempFileName(char tmpfile[], int tmpfileNum);\nbool initializeStorageProvider(const char **err);\n\n/* ================================= Debugging ============================== */\n\n/* Compute the sha1 of string at 's' with 'len' bytes long.\n * The SHA1 is then xored against the string pointed by digest.\n * Since xor is commutative, this operation is used in order to\n * \"add\" digests relative to unordered elements.\n *\n * So digest(a,b,c,d) will be the same of digest(b,a,c,d) */\nvoid xorDigest(unsigned char *digest, const void *ptr, size_t len) {\n    SHA1_CTX ctx;\n    unsigned char hash[20];\n    const unsigned char *s = (const unsigned char*)ptr;\n    int j;\n\n    SHA1Init(&ctx);\n    SHA1Update(&ctx,s,len);\n    SHA1Final(hash,&ctx);\n\n    for (j = 0; j < 20; j++)\n        digest[j] ^= hash[j];\n}\n\nvoid xorStringObjectDigest(unsigned char *digest, robj *o) {\n    o = getDecodedObject(o);\n    xorDigest(digest,(const unsigned char*)ptrFromObj(o),sdslen(szFromObj(o)));\n    decrRefCount(o);\n}\n\n/* This function instead of just computing the SHA1 and xoring it\n * against digest, also perform the digest of \"digest\" itself and\n * replace the old value with the new one.\n *\n * So the final digest will be:\n *\n * digest = SHA1(digest xor SHA1(data))\n *\n * This function is used every time we want to preserve the order so\n * that digest(a,b,c,d) will be different than digest(b,c,d,a)\n *\n * Also note that mixdigest(\"foo\") followed by mixdigest(\"bar\")\n * will lead to a different digest compared to \"fo\", \"obar\".\n */\nvoid mixDigest(unsigned char *digest, const void *ptr, size_t len) {\n    SHA1_CTX ctx;\n    const char *s = (const char*)ptr;\n\n    xorDigest(digest,s,len);\n    SHA1Init(&ctx);\n    SHA1Update(&ctx,digest,20);\n    SHA1Final(digest,&ctx);\n}\n\nvoid mixStringObjectDigest(unsigned char *digest, robj_roptr o) {\n    o = getDecodedObject(o);\n    mixDigest(digest,ptrFromObj(o),sdslen(szFromObj(o)));\n    decrRefCount(o);\n}\n\n/* This function computes the digest of a data structure stored in the\n * object 'o'. It is the core of the DEBUG DIGEST command: when taking the\n * digest of a whole dataset, we take the digest of the key and the value\n * pair, and xor all those together.\n *\n * Note that this function does not reset the initial 'digest' passed, it\n * will continue mixing this object digest to anything that was already\n * present. */\nvoid xorObjectDigest(unsigned char *digest, robj_roptr o) {\n    uint32_t aux = htonl(o->type);\n    mixDigest(digest,&aux,sizeof(aux));\n    const expireEntry *pexpire = o->FExpires() ? &o->expire : nullptr;\n    long long expiretime = INVALID_EXPIRE;\n    char buf[128];\n\n    if (pexpire != nullptr)\n        pexpire->FGetPrimaryExpire(&expiretime);\n\n    /* Save the key and associated value */\n    if (o->type == OBJ_STRING) {\n        mixStringObjectDigest(digest,o);\n    } else if (o->type == OBJ_LIST) {\n        listTypeIterator *li = listTypeInitIterator(o,0,LIST_TAIL);\n        listTypeEntry entry;\n        while(listTypeNext(li,&entry)) {\n            robj *eleobj = listTypeGet(&entry);\n            mixStringObjectDigest(digest,eleobj);\n            decrRefCount(eleobj);\n        }\n        listTypeReleaseIterator(li);\n    } else if (o->type == OBJ_SET) {\n        setTypeIterator *si = setTypeInitIterator(o);\n        sds sdsele;\n        while((sdsele = setTypeNextObject(si)) != NULL) {\n            xorDigest(digest,sdsele,sdslen(sdsele));\n            sdsfree(sdsele);\n        }\n        setTypeReleaseIterator(si);\n    } else if (o->type == OBJ_ZSET) {\n        unsigned char eledigest[20];\n\n        if (o->encoding == OBJ_ENCODING_ZIPLIST) {\n            unsigned char *zl = (unsigned char*)ptrFromObj(o);\n            unsigned char *eptr, *sptr;\n            unsigned char *vstr;\n            unsigned int vlen;\n            long long vll;\n            double score;\n\n            eptr = ziplistIndex(zl,0);\n            serverAssert(eptr != NULL);\n            sptr = ziplistNext(zl,eptr);\n            serverAssert(sptr != NULL);\n\n            while (eptr != NULL) {\n                serverAssert(ziplistGet(eptr,&vstr,&vlen,&vll));\n                score = zzlGetScore(sptr);\n\n                memset(eledigest,0,20);\n                if (vstr != NULL) {\n                    mixDigest(eledigest,vstr,vlen);\n                } else {\n                    ll2string(buf,sizeof(buf),vll);\n                    mixDigest(eledigest,buf,strlen(buf));\n                }\n\n                snprintf(buf,sizeof(buf),\"%.17g\",score);\n                mixDigest(eledigest,buf,strlen(buf));\n                xorDigest(digest,eledigest,20);\n                zzlNext(zl,&eptr,&sptr);\n            }\n        } else if (o->encoding == OBJ_ENCODING_SKIPLIST) {\n            zset *zs = (zset*)ptrFromObj(o);\n            dictIterator *di = dictGetIterator(zs->dict);\n            dictEntry *de;\n\n            while((de = dictNext(di)) != NULL) {\n                sds sdsele = (sds)dictGetKey(de);\n                double *score = (double*)dictGetVal(de);\n\n                snprintf(buf,sizeof(buf),\"%.17g\",*score);\n                memset(eledigest,0,20);\n                mixDigest(eledigest,sdsele,sdslen(sdsele));\n                mixDigest(eledigest,buf,strlen(buf));\n                xorDigest(digest,eledigest,20);\n            }\n            dictReleaseIterator(di);\n        } else {\n            serverPanic(\"Unknown sorted set encoding\");\n        }\n    } else if (o->type == OBJ_HASH) {\n        hashTypeIterator *hi = hashTypeInitIterator(o);\n        while (hashTypeNext(hi) != C_ERR) {\n            unsigned char eledigest[20];\n            sds sdsele;\n\n            memset(eledigest,0,20);\n            sdsele = hashTypeCurrentObjectNewSds(hi,OBJ_HASH_KEY);\n            mixDigest(eledigest,sdsele,sdslen(sdsele));\n            sdsfree(sdsele);\n            sdsele = hashTypeCurrentObjectNewSds(hi,OBJ_HASH_VALUE);\n            mixDigest(eledigest,sdsele,sdslen(sdsele));\n            sdsfree(sdsele);\n            xorDigest(digest,eledigest,20);\n        }\n        hashTypeReleaseIterator(hi);\n    } else if (o->type == OBJ_STREAM) {\n        streamIterator si;\n        streamIteratorStart(&si,(stream*)ptrFromObj(o),NULL,NULL,0);\n        streamID id;\n        int64_t numfields;\n\n        while(streamIteratorGetID(&si,&id,&numfields)) {\n            sds itemid = sdscatfmt(sdsempty(),\"%U.%U\",id.ms,id.seq);\n            mixDigest(digest,itemid,sdslen(itemid));\n            sdsfree(itemid);\n\n            while(numfields--) {\n                unsigned char *field, *value;\n                int64_t field_len, value_len;\n                streamIteratorGetField(&si,&field,&value,\n                                           &field_len,&value_len);\n                mixDigest(digest,field,field_len);\n                mixDigest(digest,value,value_len);\n            }\n        }\n        streamIteratorStop(&si);\n    } else if (o->type == OBJ_MODULE) {\n        RedisModuleDigest md = {{0},{0}};\n        moduleValue *mv = (moduleValue*)ptrFromObj(o);\n        moduleType *mt = mv->type;\n        moduleInitDigestContext(md);\n        if (mt->digest) {\n            mt->digest(&md,mv->value);\n            xorDigest(digest,md.x,sizeof(md.x));\n        }\n    } else if (o->type == OBJ_CRON) {\n        cronjob *job = (cronjob*)ptrFromObj(o);\n        mixDigest(digest, &job->interval, sizeof(job->interval));\n        mixDigest(digest, job->script.get(), job->script.size());\n    } else {\n        serverPanic(\"Unknown object type\");\n    }\n    /* If the key has an expire, add it to the mix */\n    if (expiretime != INVALID_EXPIRE) xorDigest(digest,\"!!expire!!\",10);\n}\n\n/* Compute the dataset digest. Since keys, sets elements, hashes elements\n * are not ordered, we use a trick: every aggregate digest is the xor\n * of the digests of their elements. This way the order will not change\n * the result. For list instead we use a feedback entering the output digest\n * as input in order to ensure that a different ordered list will result in\n * a different digest. */\nvoid computeDatasetDigest(unsigned char *final) {\n    int j;\n    uint32_t aux;\n\n    memset(final,0,20); /* Start with a clean result */\n\n    for (j = 0; j < cserver.dbnum; j++) {\n        redisDb *db = g_pserver->db[j];\n\n        if (db->size() == 0) continue;\n\n        /* hash the DB id, so the same dataset moved in a different\n         * DB will lead to a different digest */\n        aux = htonl(j);\n        mixDigest(final,&aux,sizeof(aux));\n\n        /* Iterate this DB writing every entry */\n        db->iterate_threadsafe([final](const char *key, robj_roptr o)->bool {\n            unsigned char digest[20];\n            robj *keyobj;\n\n            memset(digest,0,20); /* This key-val digest */\n            keyobj = createStringObject(key,sdslen(key));\n\n            mixDigest(digest,key,sdslen(key));\n\n            xorObjectDigest(digest,o);\n\n            /* We can finally xor the key-val digest to the final digest */\n            xorDigest(final,digest,20);\n            decrRefCount(keyobj);\n            return true;\n        });\n    }\n}\n\n#ifdef USE_JEMALLOC\nvoid mallctl_int(client *c, robj **argv, int argc) {\n    int ret;\n    /* start with the biggest size (int64), and if that fails, try smaller sizes (int32, bool) */\n    int64_t old = 0, val;\n    if (argc > 1) {\n        long long ll;\n        if (getLongLongFromObjectOrReply(c, argv[1], &ll, NULL) != C_OK)\n            return;\n        val = ll;\n    }\n    size_t sz = sizeof(old);\n    while (sz > 0) {\n        if ((ret=mallctl(szFromObj(argv[0]), &old, &sz, argc > 1? &val: NULL, argc > 1?sz: 0))) {\n            if (ret == EPERM && argc > 1) {\n                /* if this option is write only, try just writing to it. */\n                if (!(ret=mallctl(szFromObj(argv[0]), NULL, 0, &val, sz))) {\n                    addReply(c, shared.ok);\n                    return;\n                }\n            }\n            if (ret==EINVAL) {\n                /* size might be wrong, try a smaller one */\n                sz /= 2;\n#if BYTE_ORDER == BIG_ENDIAN\n                val <<= 8*sz;\n#endif\n                continue;\n            }\n            addReplyErrorFormat(c,\"%s\", strerror(ret));\n            return;\n        } else {\n#if BYTE_ORDER == BIG_ENDIAN\n            old >>= 64 - 8*sz;\n#endif\n            addReplyLongLong(c, old);\n            return;\n        }\n    }\n    addReplyErrorFormat(c,\"%s\", strerror(EINVAL));\n}\n\nvoid mallctl_string(client *c, robj **argv, int argc) {\n    int rret, wret;\n    char *old;\n    size_t sz = sizeof(old);\n    /* for strings, it seems we need to first get the old value, before overriding it. */\n    if ((rret=mallctl(szFromObj(argv[0]), &old, &sz, NULL, 0))) {\n        /* return error unless this option is write only. */\n        if (!(rret == EPERM && argc > 1)) {\n            addReplyErrorFormat(c,\"%s\", strerror(rret));\n            return;\n        }\n    }\n    if(argc > 1) {\n        char *val = szFromObj(argv[1]);\n        char **valref = &val;\n        if ((!strcmp(val,\"VOID\")))\n            valref = NULL, sz = 0;\n        wret = mallctl(szFromObj(argv[0]), NULL, 0, valref, sz);\n    }\n    if (!rret)\n        addReplyBulkCString(c, old);\n    else if (wret)\n        addReplyErrorFormat(c,\"%s\", strerror(wret));\n    else\n        addReply(c, shared.ok);\n}\n#endif\n\nvoid debugCommand(client *c) {\n    if (c->argc == 2 && !strcasecmp(szFromObj(c->argv[1]),\"help\")) {\n        const char *help[] = {\n\"AOF-FLUSH-SLEEP <microsec>\",\n\"    Server will sleep before flushing the AOF, this is used for testing.\",\n\"ASSERT\",\n\"    Crash by assertion failed.\",\n\"CHANGE-REPL-ID\"\n\"    Change the replication IDs of the instance.\",\n\"    Dangerous: should be used only for testing the replication subsystem.\",\n\"CONFIG-REWRITE-FORCE-ALL\",\n\"    Like CONFIG REWRITE but writes all configuration options, including\",\n\"    keywords not listed in original configuration file or default values.\",\n\"CRASH-AND-RECOVER <milliseconds>\",\n\"    Hard crash and restart after a <milliseconds> delay.\",\n\"DIGEST\",\n\"    Output a hex signature representing the current DB content.\",\n\"DIGEST-VALUE <key> [<key> ...]\",\n\"    Output a hex signature of the values of all the specified keys.\",\n\"ERROR <string>\",\n\"    Return a Redis protocol error with <string> as message. Useful for clients\",\n\"    unit tests to simulate Redis errors.\",\n\"LOG <message>\",\n\"    Write <message> to the server log.\",\n\"HTSTATS <dbid>\",\n\"    Return hash table statistics of the specified Redis database.\",\n\"HTSTATS-KEY <key>\",\n\"    Like HTSTATS but for the hash table stored at <key>'s value.\",\n\"LOADAOF\",\n\"    Flush the AOF buffers on disk and reload the AOF in memory.\",\n\"LUA-ALWAYS-REPLICATE-COMMANDS <0|1>\",\n\"    Setting it to 1 makes Lua replication defaulting to replicating single\",\n\"    commands, without the script having to enable effects replication.\",\n#ifdef USE_JEMALLOC\n\"MALLCTL <key> [<val>]\",\n\"    Get or set a malloc tuning integer.\",\n\"MALLCTL-STR <key> [<val>]\",\n\"    Get or set a malloc tuning string.\",\n#endif\n\"OBJECT <key>\",\n\"    Show low level info about `key` and associated value.\",\n\"OOM\",\n\"    Crash the server simulating an out-of-memory error.\",\n\"PANIC\",\n\"    Crash the server simulating a panic.\",\n\"POPULATE <count> [<prefix>] [<size>]\",\n\"    Create <count> string keys named key:<num>. If <prefix> is specified then\",\n\"    it is used instead of the 'key' prefix.\",\n\"DEBUG PROTOCOL <type>\",\n\"    Reply with a test value of the specified type. <type> can be: string,\",\n\"    integer, double, bignum, null, array, set, map, attrib, push, verbatim,\",\n\"    true, false.\",\n\"RELOAD [option ...]\",\n\"    Save the RDB on disk and reload it back to memory. Valid <option> values:\",\n\"    * MERGE: conflicting keys will be loaded from RDB.\",\n\"    * NOFLUSH: the existing database will not be removed before load, but\",\n\"      conflicting keys will generate an exception and kill the server.\"\n\"    * NOSAVE: the database will be loaded from an existing RDB file.\",\n\"    Examples:\",\n\"    * DEBUG RELOAD: verify that the server is able to persist, flush and reload\",\n\"      the database.\",\n\"    * DEBUG RELOAD NOSAVE: replace the current database with the contents of an\",\n\"      existing RDB file.\",\n\"    * DEBUG RELOAD NOSAVE NOFLUSH MERGE: add the contents of an existing RDB\",\n\"      file to the database.\",\n\"RESTART\",\n\"    Graceful restart: save config, db, restart.\",\n\"SDSLEN <key>\",\n\"    Show low level SDS string info representing `key` and value.\",\n\"SEGFAULT\",\n\"    Crash the server with sigsegv.\",\n\"SET-ACTIVE-EXPIRE <0|1>\",\n\"    Setting it to 0 disables expiring keys in background when they are not\",\n\"    accessed (otherwise the Redis behavior). Setting it to 1 reenables back the\",\n\"    default.\",\n\"SET-SKIP-CHECKSUM-VALIDATION <0|1>\",\n\"    Enables or disables checksum checks for RDB files and RESTORE's payload.\",\n\"SLEEP <seconds>\",\n\"    Stop the server for <seconds>. Decimals allowed.\",\n\"STRINGMATCH-TEST\",\n\"    Run a fuzz tester against the stringmatchlen() function.\",\n\"STRUCTSIZE\",\n\"    Return the size of different Redis core C structures.\",\n\"ZIPLIST <key>\",\n\"    Show low level info about the ziplist encoding of <key>.\",\nNULL\n        };\n        addReplyHelp(c, help);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"segfault\")) {\n        *((char*)-1) = 'x';\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"panic\")) {\n        serverPanic(\"DEBUG PANIC called at Unix time %lld\", (long long)time(NULL));\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"restart\") ||\n               !strcasecmp(szFromObj(c->argv[1]),\"crash-and-recover\"))\n    {\n        long long delay = 0;\n        if (c->argc >= 3) {\n            if (getLongLongFromObjectOrReply(c, c->argv[2], &delay, NULL)\n                != C_OK) return;\n            if (delay < 0) delay = 0;\n        }\n        int flags = !strcasecmp(szFromObj(c->argv[1]),\"restart\") ?\n            (RESTART_SERVER_GRACEFULLY|RESTART_SERVER_CONFIG_REWRITE) :\n             RESTART_SERVER_NONE;\n        restartServer(flags,delay);\n        addReplyError(c,\"failed to restart the g_pserver-> Check server logs.\");\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"oom\")) {\n        void *ptr = zmalloc(ULONG_MAX, MALLOC_LOCAL); /* Should trigger an out of memory. */\n        zfree(ptr);\n        addReply(c,shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"assert\")) {\n        serverAssertWithInfo(c,c->argv[0],1 == 2);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"log\") && c->argc == 3) {\n        serverLog(LL_WARNING, \"DEBUG LOG: %s\", (char*)ptrFromObj(c->argv[2]));\n        addReply(c,shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"leak\") && c->argc == 3) {\n        sdsdup(szFromObj(c->argv[2]));\n        addReply(c,shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"reload\")) {\n        int flush = 1, save = 1;\n        int flags = RDBFLAGS_NONE;\n\n        /* Parse the additional options that modify the RELOAD\n         * behavior. */\n        for (int j = 2; j < c->argc; j++) {\n            char *opt = szFromObj(c->argv[j]);\n            if (!strcasecmp(opt,\"MERGE\")) {\n                flags |= RDBFLAGS_ALLOW_DUP;\n            } else if (!strcasecmp(opt,\"NOFLUSH\")) {\n                flush = 0;\n            } else if (!strcasecmp(opt,\"NOSAVE\")) {\n                save = 0;\n            } else {\n                addReplyError(c,\"DEBUG RELOAD only supports the \"\n                                \"MERGE, NOFLUSH and NOSAVE options.\");\n                return;\n            }\n        }\n        \n        if (g_pserver->m_pstorageFactory) {\n            protectClient(c);\n\n            for (int idb = 0; idb < cserver.dbnum; ++idb) {\n                if (g_pserver->db[idb]->processChanges(false))\n                    g_pserver->db[idb]->commitChanges();\n                g_pserver->db[idb]->storageProviderDelete();\n            }\n            delete g_pserver->metadataDb;\n\n            if (flush) emptyDb(-1,EMPTYDB_NO_FLAGS,NULL);\n\n            delete g_pserver->m_pstorageFactory;\n\n            const char *err;\n            if (!initializeStorageProvider(&err))\n            {\n                serverLog(LL_WARNING, \"Failed to initialize storage provider: %s\",err);\n                exit(EXIT_FAILURE);\n            }\n\n            g_pserver->metadataDb = g_pserver->m_pstorageFactory->createMetadataDb();\n            for (int idb = 0; idb < cserver.dbnum; ++idb)\n            {\n                int dbid = idb;\n                std::string dbid_key = \"db-\" + std::to_string(idb);\n                g_pserver->metadataDb->retrieve(dbid_key.c_str(), dbid_key.length(), [&](const char *, size_t, const void *data, size_t){\n                    dbid = *(int*)data;\n                });\n                delete g_pserver->db[idb];\n                g_pserver->db[idb] = new (MALLOC_LOCAL) redisDb();\n                g_pserver->db[idb]->initialize(dbid);\n            }\n\n            moduleFireServerEvent(REDISMODULE_EVENT_LOADING, REDISMODULE_SUBEVENT_LOADING_FLASH_START, NULL);\n            for (int idb = 0; idb < cserver.dbnum; ++idb)\n            {\n                g_pserver->db[idb]->storageProviderInitialize();\n                g_pserver->db[idb]->trackChanges(false);\n            }\n            moduleFireServerEvent(REDISMODULE_EVENT_LOADING, REDISMODULE_SUBEVENT_LOADING_ENDED, NULL);\n            \n            unprotectClient(c);\n        } else {\n            /* The default behavior is to save the RDB file before loading\n            * it back. */\n            if (save) {\n                rdbSaveInfo rsi, *rsiptr;\n                rsiptr = rdbPopulateSaveInfo(&rsi);\n                if (rdbSave(nullptr, rsiptr) != C_OK) {\n                    addReply(c,shared.err);\n                    return;\n                }\n            }\n\n            /* The default behavior is to remove the current dataset from\n            * memory before loading the RDB file, however when MERGE is\n            * used together with NOFLUSH, we are able to merge two datasets. */\n            if (flush) emptyDb(-1,EMPTYDB_NO_FLAGS,NULL);\n\n            protectClient(c);\n            int ret = rdbLoadFile(g_pserver->rdb_filename,NULL,flags);\n            unprotectClient(c);\n            if (ret != C_OK) {\n                addReplyError(c,\"Error trying to load the RDB dump\");\n                return;\n            }\n        }\n        serverLog(LL_WARNING,\"DB reloaded by DEBUG RELOAD\");\n        addReply(c,shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"loadaof\")) {\n        if (g_pserver->aof_state != AOF_OFF) flushAppendOnlyFile(1);\n        emptyDb(-1,EMPTYDB_NO_FLAGS,NULL);\n        protectClient(c);\n        int ret = loadAppendOnlyFile(g_pserver->aof_filename);\n        unprotectClient(c);\n        if (ret != C_OK) {\n            addReplyErrorObject(c,shared.err);\n            return;\n        }\n        g_pserver->dirty = 0; /* Prevent AOF / replication */\n        serverLog(LL_WARNING,\"Append Only File loaded by DEBUG LOADAOF\");\n        addReply(c,shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"object\") && c->argc == 3) {\n        robj *val;\n        const char *strenc;\n\n        val = c->db->find(c->argv[2]);\n        if (val == NULL) {\n            addReplyErrorObject(c,shared.nokeyerr);\n            return;\n        }\n        strenc = strEncoding(val->encoding);\n\n        char extra[138] = {0};\n        if (val->encoding == OBJ_ENCODING_QUICKLIST) {\n            char *nextra = extra;\n            int remaining = sizeof(extra);\n            quicklist *ql = (quicklist*)val->m_ptr;\n            /* Add number of quicklist nodes */\n            int used = snprintf(nextra, remaining, \" ql_nodes:%lu\", ql->len);\n            nextra += used;\n            remaining -= used;\n            /* Add average quicklist fill factor */\n            double avg = (double)ql->count/ql->len;\n            used = snprintf(nextra, remaining, \" ql_avg_node:%.2f\", avg);\n            nextra += used;\n            remaining -= used;\n            /* Add quicklist fill level / max ziplist size */\n            used = snprintf(nextra, remaining, \" ql_ziplist_max:%d\", ql->fill);\n            nextra += used;\n            remaining -= used;\n            /* Add isCompressed? */\n            int compressed = ql->compress != 0;\n            used = snprintf(nextra, remaining, \" ql_compressed:%d\", compressed);\n            nextra += used;\n            remaining -= used;\n            /* Add total uncompressed size */\n            unsigned long sz = 0;\n            for (quicklistNode *node = ql->head; node; node = node->next) {\n                sz += node->sz;\n            }\n            used = snprintf(nextra, remaining, \" ql_uncompressed_size:%lu\", sz);\n            nextra += used;\n            remaining -= used;\n        }\n\n        addReplyStatusFormat(c,\n            \"Value at:%p refcount:%d \"\n            \"encoding:%s serializedlength:%zu \"\n            \"lru:%d lru_seconds_idle:%llu%s\",\n            (void*)val, static_cast<int>(val->getrefcount(std::memory_order_relaxed)),\n            strenc, rdbSavedObjectLen(val, c->argv[2]),\n            val->lru, estimateObjectIdleTime(val)/1000, extra);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"sdslen\") && c->argc == 3) {\n        auto itr = c->db->find(c->argv[2]);\n        robj *val = itr.val();\n        const char *key = itr.key();\n\n        if (val == NULL) {\n            addReplyErrorObject(c,shared.nokeyerr);\n            return;\n        }\n\n        if (val->type != OBJ_STRING || !sdsEncodedObject(val)) {\n            addReplyError(c,\"Not an sds encoded string.\");\n        } else {\n            addReplyStatusFormat(c,\n                \"key_sds_len:%lld, key_sds_avail:%lld, key_zmalloc: %lld, \"\n                \"val_sds_len:%lld, val_sds_avail:%lld, val_zmalloc: %lld\",\n                (long long) sdslen(key),\n                (long long) sdsavail(key),\n                (long long) sdsZmallocSize((sds)key),\n                (long long) sdslen(szFromObj(val)),\n                (long long) sdsavail(szFromObj(val)),\n                (long long) getStringObjectSdsUsedMemory(val));\n        }\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"ziplist\") && c->argc == 3) {\n        robj_roptr o;\n\n        if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.nokeyerr))\n                == nullptr) return;\n\n        if (o->encoding != OBJ_ENCODING_ZIPLIST) {\n            addReplyError(c,\"Not a ziplist encoded object.\");\n        } else {\n            ziplistRepr((unsigned char*)ptrFromObj(o));\n            addReplyStatus(c,\"Ziplist structure printed on stdout\");\n        }\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"populate\") &&\n               c->argc >= 3 && c->argc <= 5) {\n        long keys, j;\n        robj *key, *val;\n        char buf[128];\n\n        if (getPositiveLongFromObjectOrReply(c, c->argv[2], &keys, NULL) != C_OK)\n            return;\n        \n        c->db->expand(keys);\n        long valsize = 0;\n        if ( c->argc == 5 && getPositiveLongFromObjectOrReply(c, c->argv[4], &valsize, NULL) != C_OK ) \n            return;\n\n        for (j = 0; j < keys; j++) {\n            snprintf(buf,sizeof(buf),\"%s:%lu\",\n                (c->argc == 3) ? \"key\" : (char*)ptrFromObj(c->argv[3]), j);\n            key = createStringObject(buf,strlen(buf));\n            if (lookupKeyWrite(c->db,key) != NULL) {\n                decrRefCount(key);\n                continue;\n            }\n            snprintf(buf,sizeof(buf),\"value:%lu\",j);\n            if (valsize==0)\n                val = createStringObject(buf,strlen(buf));\n            else {\n                int buflen = strlen(buf);\n                val = createStringObject(NULL,valsize);\n                memcpy(ptrFromObj(val), buf, valsize<=buflen? valsize: buflen);\n            }\n            dbAdd(c->db,key,val);\n            signalModifiedKey(c,c->db,key);\n            decrRefCount(key);\n        }\n        addReply(c,shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"digest\") && c->argc == 2) {\n        /* DEBUG DIGEST (form without keys specified) */\n        unsigned char digest[20];\n        sds d = sdsempty();\n\n        computeDatasetDigest(digest);\n        for (int i = 0; i < 20; i++) d = sdscatprintf(d, \"%02x\",digest[i]);\n        addReplyStatus(c,d);\n        sdsfree(d);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"digest-value\") && c->argc >= 2) {\n        /* DEBUG DIGEST-VALUE key key key ... key. */\n        addReplyArrayLen(c,c->argc-2);\n        for (int j = 2; j < c->argc; j++) {\n            unsigned char digest[20];\n            memset(digest,0,20); /* Start with a clean result */\n\n            /* We don't use lookupKey because a debug command should\n             * work on logically expired keys */\n            auto itr = c->db->find(c->argv[j]);\n            robj* o = (robj*)(itr == NULL ? NULL : itr.val());\n            if (o) xorObjectDigest(digest,o);\n\n            sds d = sdsempty();\n            for (int i = 0; i < 20; i++) d = sdscatprintf(d, \"%02x\",digest[i]);\n            addReplyStatus(c,d);\n            sdsfree(d);\n        }\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"protocol\") && c->argc == 3) {\n        /* DEBUG PROTOCOL [string|integer|double|bignum|null|array|set|map|\n         *                 attrib|push|verbatim|true|false] */\n        const char *name = szFromObj(c->argv[2]);\n        if (!strcasecmp(name,\"string\")) {\n            addReplyBulkCString(c,\"Hello World\");\n        } else if (!strcasecmp(name,\"integer\")) {\n            addReplyLongLong(c,12345);\n        } else if (!strcasecmp(name,\"double\")) {\n            addReplyDouble(c,3.14159265359);\n        } else if (!strcasecmp(name,\"bignum\")) {\n            addReplyBigNum(c,\"1234567999999999999999999999999999999\",37);\n        } else if (!strcasecmp(name,\"null\")) {\n            addReplyNull(c);\n        } else if (!strcasecmp(name,\"array\")) {\n            addReplyArrayLen(c,3);\n            for (int j = 0; j < 3; j++) addReplyLongLong(c,j);\n        } else if (!strcasecmp(name,\"set\")) {\n            addReplySetLen(c,3);\n            for (int j = 0; j < 3; j++) addReplyLongLong(c,j);\n        } else if (!strcasecmp(name,\"map\")) {\n            addReplyMapLen(c,3);\n            for (int j = 0; j < 3; j++) {\n                addReplyLongLong(c,j);\n                addReplyBool(c, j == 1);\n            }\n        } else if (!strcasecmp(name,\"attrib\")) {\n            if (c->resp >= 3) {\n                addReplyAttributeLen(c,1);\n                addReplyBulkCString(c,\"key-popularity\");\n                addReplyArrayLen(c,2);\n                addReplyBulkCString(c,\"key:123\");\n                addReplyLongLong(c,90);\n            }\n            /* Attributes are not real replies, so a well formed reply should\n             * also have a normal reply type after the attribute. */\n            addReplyBulkCString(c,\"Some real reply following the attribute\");\n        } else if (!strcasecmp(name,\"push\")) {\n            addReplyPushLen(c,2);\n            addReplyBulkCString(c,\"server-cpu-usage\");\n            addReplyLongLong(c,42);\n            /* Push replies are not synchronous replies, so we emit also a\n             * normal reply in order for blocking clients just discarding the\n             * push reply, to actually consume the reply and continue. */\n            addReplyBulkCString(c,\"Some real reply following the push reply\");\n        } else if (!strcasecmp(name,\"true\")) {\n            addReplyBool(c,1);\n        } else if (!strcasecmp(name,\"false\")) {\n            addReplyBool(c,0);\n        } else if (!strcasecmp(name,\"verbatim\")) {\n            addReplyVerbatim(c,\"This is a verbatim\\nstring\",25,\"txt\");\n        } else {\n            addReplyError(c,\"Wrong protocol type name. Please use one of the following: string|integer|double|bignum|null|array|set|map|attrib|push|verbatim|true|false\");\n        }\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"sleep\") && c->argc == 3) {\n        double dtime = strtod(szFromObj(c->argv[2]),NULL);\n        long long utime = dtime*1000000;\n        struct timespec tv;\n\n        tv.tv_sec = utime / 1000000;\n        tv.tv_nsec = (utime % 1000000) * 1000;\n        nanosleep(&tv, NULL);\n        addReply(c,shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"set-active-expire\") &&\n               c->argc == 3)\n    {\n        g_pserver->active_expire_enabled = atoi(szFromObj(c->argv[2]));\n        addReply(c,shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"set-skip-checksum-validation\") &&\n               c->argc == 3)\n    {\n        cserver.skip_checksum_validation = atoi(szFromObj(c->argv[2]));\n        addReply(c,shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"aof-flush-sleep\") &&\n               c->argc == 3)\n    {\n        g_pserver->aof_flush_sleep = atoi(szFromObj(c->argv[2]));\n        addReply(c,shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"lua-always-replicate-commands\") &&\n               c->argc == 3)\n    {\n        g_pserver->lua_always_replicate_commands = atoi(szFromObj(c->argv[2]));\n        addReply(c,shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"error\") && c->argc == 3) {\n        sds errstr = sdsnewlen(\"-\",1);\n\n        errstr = sdscatsds(errstr,szFromObj(c->argv[2]));\n        errstr = sdsmapchars(errstr,\"\\n\\r\",\"  \",2); /* no newlines in errors. */\n        errstr = sdscatlen(errstr,\"\\r\\n\",2);\n        addReplySds(c,errstr);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"structsize\") && c->argc == 2) {\n        sds sizes = sdsempty();\n        sizes = sdscatprintf(sizes,\"bits:%d \",(sizeof(void*) == 8)?64:32);\n        sizes = sdscatprintf(sizes,\"robj:%d \",(int)sizeof(robj));\n        sizes = sdscatprintf(sizes,\"dictentry:%d \",(int)sizeof(dictEntry));\n        sizes = sdscatprintf(sizes,\"sdshdr5:%d \",(int)sizeof(struct sdshdr5));\n        sizes = sdscatprintf(sizes,\"sdshdr8:%d \",(int)sizeof(struct sdshdr8));\n        sizes = sdscatprintf(sizes,\"sdshdr16:%d \",(int)sizeof(struct sdshdr16));\n        sizes = sdscatprintf(sizes,\"sdshdr32:%d \",(int)sizeof(struct sdshdr32));\n        sizes = sdscatprintf(sizes,\"sdshdr64:%d \",(int)sizeof(struct sdshdr64));\n        addReplyBulkSds(c,sizes);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"htstats\") && c->argc == 3) {\n        long dbid;\n        sds stats = sdsempty();\n        char buf[4096];\n\n        if (getLongFromObjectOrReply(c, c->argv[2], &dbid, NULL) != C_OK) {\n            sdsfree(stats);\n            return;\n        }\n        if (dbid < 0 || dbid >= cserver.dbnum) {\n            sdsfree(stats);\n            addReplyError(c,\"Out of range database\");\n            return;\n        }\n\n        stats = sdscatprintf(stats,\"[Dictionary HT]\\n\");\n        g_pserver->db[dbid]->getStats(buf,sizeof(buf));\n        stats = sdscat(stats,buf);\n\n        addReplyVerbatim(c,stats,sdslen(stats),\"txt\");\n        sdsfree(stats);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"htstats-key\") && c->argc == 3) {\n        robj_roptr o;\n        dict *ht = NULL;\n\n        if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.nokeyerr))\n                == nullptr) return;\n\n        /* Get the hash table reference from the object, if possible. */\n        switch (o->encoding) {\n        case OBJ_ENCODING_SKIPLIST:\n            {\n                zset *zs = (zset*)ptrFromObj(o);\n                ht = zs->dict;\n            }\n            break;\n        case OBJ_ENCODING_HT:\n            ht = (dict*)ptrFromObj(o);\n            break;\n        }\n\n        if (ht == NULL) {\n            addReplyError(c,\"The value stored at the specified key is not \"\n                            \"represented using an hash table\");\n        } else {\n            char buf[4096];\n            dictGetStats(buf,sizeof(buf),ht);\n            addReplyVerbatim(c,buf,strlen(buf),\"txt\");\n        }\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"change-repl-id\") && c->argc == 2) {\n        serverLog(LL_WARNING,\"Changing replication IDs after receiving DEBUG change-repl-id\");\n        changeReplicationId();\n        clearReplicationId2();\n        addReply(c,shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"stringmatch-test\") && c->argc == 2) {\n        stringmatchlen_fuzz_test();\n        addReplyStatus(c,\"Apparently Redis did not crash: test passed\");\n    } else if (!strcasecmp(szFromObj(c->argv[1]), \"force-master\") && c->argc == 3) {\n        c->flags |= CLIENT_MASTER | CLIENT_MASTER_FORCE_REPLY;\n        if (!strcasecmp(szFromObj(c->argv[2]), \"yes\"))\n        {\n            redisMaster *mi = (redisMaster*)zcalloc(sizeof(redisMaster), MALLOC_LOCAL);\n            mi->master = c;\n            listAddNodeHead(g_pserver->masters, mi);\n        }\n        else if (strcasecmp(szFromObj(c->argv[2]), \"flagonly\")) // if we didn't set flagonly assume its an unset\n        {\n            serverAssert(c->flags & CLIENT_MASTER);\n            if (listLength(g_pserver->masters))\n            {\n                redisMaster *mi = (redisMaster*)listNodeValue(listFirst(g_pserver->masters));\n                serverAssert(mi->master == c);\n                listDelNode(g_pserver->masters, listFirst(g_pserver->masters));\n                zfree(mi);\n            }\n            c->flags &= ~(CLIENT_MASTER | CLIENT_MASTER_FORCE_REPLY);\n        }\n        addReply(c, shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]), \"get-temp-file\") && c->argc == 2) {\n        char tmpfile[256];\n        getTempFileName(tmpfile, g_pserver->rdbThreadVars.tmpfileNum);\n        addReplyBulkCString(c, tmpfile);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"truncate-repl-backlog\") && c->argc == 2) {\n        g_pserver->repl_backlog_idx = 0;\n        g_pserver->repl_backlog_off = g_pserver->master_repl_offset+1;\n        g_pserver->repl_backlog_histlen = 0;\n        if (g_pserver->repl_batch_idxStart >= 0) g_pserver->repl_batch_idxStart = -1;\n        if (g_pserver->repl_batch_offStart >= 0) g_pserver->repl_batch_offStart = -1;\n        addReply(c, shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"config-rewrite-force-all\") && c->argc == 2)\n    {\n        if (rewriteConfig(cserver.configfile, 1) == -1)\n            addReplyError(c, \"CONFIG-REWRITE-FORCE-ALL failed\");\n        else\n            addReply(c, shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"config-rewrite-force-all\") && c->argc == 2)\n    {\n        if (rewriteConfig(cserver.configfile, 1) == -1)\n            addReplyError(c, \"CONFIG-REWRITE-FORCE-ALL failed\");\n        else\n            addReply(c, shared.ok);\n#ifdef USE_JEMALLOC\n    } else if(!strcasecmp(szFromObj(c->argv[1]),\"mallctl\") && c->argc >= 3) {\n        mallctl_int(c, c->argv+2, c->argc-2);\n        return;\n    } else if(!strcasecmp(szFromObj(c->argv[1]),\"mallctl-str\") && c->argc >= 3) {\n        mallctl_string(c, c->argv+2, c->argc-2);\n        return;\n#endif\n    } else if(!strcasecmp(szFromObj(c->argv[1]),\"flush-storage\") && c->argc == 2) {\n        if (g_pserver->m_pstorageFactory != nullptr) {\n            for (int i = 0; i < cserver.dbnum; i++) {\n                g_pserver->db[i]->getStorageCache()->flush();\n            }\n            addReply(c,shared.ok);\n        } else {\n            addReplyError(c, \"Can't flush storage if no storage provider is set\");\n        }\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"get-storage-usage\") && c->argc == 2) {\n        if (g_pserver->m_pstorageFactory != nullptr) {\n            addReplyLongLong(c, g_pserver->m_pstorageFactory->totalDiskspaceUsed());\n        } else {\n            addReplyLongLong(c, 0);\n        }\n    } else {\n        addReplySubcommandSyntaxError(c);\n        return;\n    }\n}\n\n/* =========================== Crash handling  ============================== */\n\nvoid _serverAssert(const char *estr, const char *file, int line) {\n    g_fInCrash = true;\n    bugReportStart();\n    serverLog(LL_WARNING,\"=== ASSERTION FAILED ===\");\n    serverLog(LL_WARNING,\"==> %s:%d '%s' is not true\",file,line,estr);\n\n    if (g_pserver->crashlog_enabled) {\n#if defined HAVE_BACKTRACE || defined UNW_LOCAL_ONLY\n        logStackTrace(NULL, 1);\n#endif\n        printCrashReport();\n    }\n\n    // remove the signal handler so on abort() we will output the crash report.\n    removeSignalHandlers();\n    bugReportEnd(0, 0);\n}\n\nvoid _serverAssertPrintClientInfo(const client *c) {\n    int j;\n    char conninfo[CONN_INFO_LEN];\n\n    bugReportStart();\n    serverLog(LL_WARNING,\"=== ASSERTION FAILED CLIENT CONTEXT ===\");\n    serverLog(LL_WARNING,\"client->flags = %llu\", (unsigned long long) c->flags);\n    serverLog(LL_WARNING,\"client->conn = %s\", connGetInfo(c->conn, conninfo, sizeof(conninfo)));\n    serverLog(LL_WARNING,\"client->argc = %d\", c->argc);\n    for (j=0; j < c->argc; j++) {\n        char buf[128];\n        char *arg;\n\n        if (c->argv[j]->type == OBJ_STRING && sdsEncodedObject(c->argv[j])) {\n            arg = (char*) ptrFromObj(c->argv[j]);\n        } else {\n            snprintf(buf,sizeof(buf),\"Object type: %u, encoding: %u\",\n                c->argv[j]->type, c->argv[j]->encoding);\n            arg = buf;\n        }\n        serverLog(LL_WARNING,\"client->argv[%d] = \\\"%s\\\" (refcount: %d)\",\n            j, arg, static_cast<int>(c->argv[j]->getrefcount(std::memory_order_relaxed)));\n    }\n}\n\nvoid serverLogObjectDebugInfo(robj_roptr o) {\n    serverLog(LL_WARNING,\"Object type: %d\", o->type);\n    serverLog(LL_WARNING,\"Object encoding: %d\", o->encoding);\n    serverLog(LL_WARNING,\"Object refcount: %d\", static_cast<int>(o->getrefcount(std::memory_order_relaxed)));\n#if UNSAFE_CRASH_REPORT\n    /* This code is now disabled. o->ptr may be unreliable to print. in some\n     * cases a ziplist could have already been freed by realloc, but not yet\n     * updated to o->ptr. in other cases the call to ziplistLen may need to\n     * iterate on all the items in the list (and possibly crash again).\n     * For some cases it may be ok to crash here again, but these could cause\n     * invalid memory access which will bother valgrind and also possibly cause\n     * random memory portion to be \"leaked\" into the logfile. */\n    if (o->type == OBJ_STRING && sdsEncodedObject(o)) {\n        serverLog(LL_WARNING,\"Object raw string len: %zu\", sdslen(szFromObj(o)));\n        if (sdslen(szFromObj(o)) < 4096) {\n            sds repr = sdscatrepr(sdsempty(),szFromObj(o),sdslen(szFromObj(o)));\n            serverLog(LL_WARNING,\"Object raw string content: %s\", repr);\n            sdsfree(repr);\n        }\n    } else if (o->type == OBJ_LIST) {\n        serverLog(LL_WARNING,\"List length: %d\", (int) listTypeLength(o));\n    } else if (o->type == OBJ_SET) {\n        serverLog(LL_WARNING,\"Set size: %d\", (int) setTypeSize(o));\n    } else if (o->type == OBJ_HASH) {\n        serverLog(LL_WARNING,\"Hash size: %d\", (int) hashTypeLength(o));\n    } else if (o->type == OBJ_ZSET) {\n        serverLog(LL_WARNING,\"Sorted set size: %d\", (int) zsetLength(o));\n        if (o->encoding == OBJ_ENCODING_SKIPLIST)\n            serverLog(LL_WARNING,\"Skiplist level: %d\", (int) ((const zset*)ptrFromObj(o))->zsl->level);\n    } else if (o->type == OBJ_STREAM) {\n        serverLog(LL_WARNING,\"Stream size: %d\", (int) streamLength(o));\n    }\n#endif\n}\n\nvoid _serverAssertPrintObject(robj_roptr o) {\n    bugReportStart();\n    serverLog(LL_WARNING,\"=== ASSERTION FAILED OBJECT CONTEXT ===\");\n    serverLogObjectDebugInfo(o);\n}\n\nvoid _serverAssertWithInfo(const client *c, robj_roptr o, const char *estr, const char *file, int line) {\n    if (c) _serverAssertPrintClientInfo(c);\n    if (o) _serverAssertPrintObject(o);\n    _serverAssert(estr,file,line);\n}\n\nvoid _serverPanic(const char *file, int line, const char *msg, ...) {\n    g_fInCrash = true;\n    va_list ap;\n    va_start(ap,msg);\n    char fmtmsg[256];\n    vsnprintf(fmtmsg,sizeof(fmtmsg),msg,ap);\n    va_end(ap);\n\n    bugReportStart();\n    serverLog(LL_WARNING,\"------------------------------------------------\");\n    serverLog(LL_WARNING,\"!!! Software Failure. Press left mouse button to continue\");\n    serverLog(LL_WARNING,\"Guru Meditation: %s #%s:%d\",fmtmsg,file,line);\n\n    if (g_pserver->crashlog_enabled) {\n#if defined HAVE_BACKTRACE || defined UNW_LOCAL_ONLY\n        logStackTrace(NULL, 1);\n#endif\n        printCrashReport();\n    }\n\n    // remove the signal handler so on abort() we will output the crash report.\n    removeSignalHandlers();\n    bugReportEnd(0, 0);\n}\n\nvoid bugReportStart(void) {\n    pthread_mutex_lock(&bug_report_start_mutex);\n    if (bug_report_start == 0) {\n        serverLogRaw(LL_WARNING|LL_RAW,\n        \"\\n\\n=== KEYDB BUG REPORT START: Cut & paste starting from here ===\\n\");\n        bug_report_start = 1;\n    }\n    pthread_mutex_unlock(&bug_report_start_mutex);\n}\n\n#ifdef HAVE_BACKTRACE\nstatic void *getMcontextEip(ucontext_t *uc) {\n#if defined(__APPLE__) && !defined(MAC_OS_X_VERSION_10_6)\n    /* OSX < 10.6 */\n    #if defined(__x86_64__)\n    return (void*) uc->uc_mcontext->__ss.__rip;\n    #elif defined(__i386__)\n    return (void*) uc->uc_mcontext->__ss.__eip;\n    #else\n    return (void*) uc->uc_mcontext->__ss.__pc;\n    #endif\n#elif defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_6)\n    /* OSX >= 10.6 */\n    #if defined(_STRUCT_X86_THREAD_STATE64) && !defined(__i386__)\n    return (void*) uc->uc_mcontext->__ss.__rip;\n    #elif defined(__i386__)\n    return (void*) uc->uc_mcontext->__ss.__eip;\n    #else\n    /* OSX ARM64 */\n    return (void*) arm_thread_state64_get_pc(uc->uc_mcontext->__ss);\n    #endif\n#elif defined(__linux__)\n    /* Linux */\n    #if defined(__i386__) || ((defined(__X86_64__) || defined(__x86_64__)) && defined(__ILP32__))\n    return (void*) uc->uc_mcontext.gregs[14]; /* Linux 32 */\n    #elif defined(__X86_64__) || defined(__x86_64__)\n    return (void*) uc->uc_mcontext.gregs[16]; /* Linux 64 */\n    #elif defined(__ia64__) /* Linux IA64 */\n    return (void*) uc->uc_mcontext.sc_ip;\n    #elif defined(__arm__) /* Linux ARM */\n    return (void*) uc->uc_mcontext.arm_pc;\n    #elif defined(__aarch64__) /* Linux AArch64 */\n    return (void*) uc->uc_mcontext.pc;\n    #endif\n#elif defined(__FreeBSD__)\n    /* FreeBSD */\n    #if defined(__i386__)\n    return (void*) uc->uc_mcontext.mc_eip;\n    #elif defined(__x86_64__)\n    return (void*) uc->uc_mcontext.mc_rip;\n    #endif\n#elif defined(__OpenBSD__)\n    /* OpenBSD */\n    #if defined(__i386__)\n    return (void*) uc->sc_eip;\n    #elif defined(__x86_64__)\n    return (void*) uc->sc_rip;\n    #endif\n#elif defined(__NetBSD__)\n    #if defined(__i386__)\n    return (void*) uc->uc_mcontext.__gregs[_REG_EIP];\n    #elif defined(__x86_64__)\n    return (void*) uc->uc_mcontext.__gregs[_REG_RIP];\n    #endif\n#elif defined(__DragonFly__)\n    return (void*) uc->uc_mcontext.mc_rip;\n#else\n    return NULL;\n#endif\n}\n\nvoid logStackContent(void **sp) {\n    int i;\n    for (i = 15; i >= 0; i--) {\n        unsigned long addr = (unsigned long) sp+i;\n        unsigned long val = (unsigned long) sp[i];\n\n        if (sizeof(long) == 4)\n            serverLog(LL_WARNING, \"(%08lx) -> %08lx\", addr, val);\n        else\n            serverLog(LL_WARNING, \"(%016lx) -> %016lx\", addr, val);\n    }\n}\n\n/* Log dump of processor registers */\nvoid logRegisters(ucontext_t *uc) {\n    serverLog(LL_WARNING|LL_RAW, \"\\n------ REGISTERS ------\\n\");\n\n/* OSX */\n#if defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_6)\n  /* OSX AMD64 */\n    #if defined(_STRUCT_X86_THREAD_STATE64) && !defined(__i386__)\n    serverLog(LL_WARNING,\n    \"\\n\"\n    \"RAX:%016lx RBX:%016lx\\nRCX:%016lx RDX:%016lx\\n\"\n    \"RDI:%016lx RSI:%016lx\\nRBP:%016lx RSP:%016lx\\n\"\n    \"R8 :%016lx R9 :%016lx\\nR10:%016lx R11:%016lx\\n\"\n    \"R12:%016lx R13:%016lx\\nR14:%016lx R15:%016lx\\n\"\n    \"RIP:%016lx EFL:%016lx\\nCS :%016lx FS:%016lx  GS:%016lx\",\n        (unsigned long) uc->uc_mcontext->__ss.__rax,\n        (unsigned long) uc->uc_mcontext->__ss.__rbx,\n        (unsigned long) uc->uc_mcontext->__ss.__rcx,\n        (unsigned long) uc->uc_mcontext->__ss.__rdx,\n        (unsigned long) uc->uc_mcontext->__ss.__rdi,\n        (unsigned long) uc->uc_mcontext->__ss.__rsi,\n        (unsigned long) uc->uc_mcontext->__ss.__rbp,\n        (unsigned long) uc->uc_mcontext->__ss.__rsp,\n        (unsigned long) uc->uc_mcontext->__ss.__r8,\n        (unsigned long) uc->uc_mcontext->__ss.__r9,\n        (unsigned long) uc->uc_mcontext->__ss.__r10,\n        (unsigned long) uc->uc_mcontext->__ss.__r11,\n        (unsigned long) uc->uc_mcontext->__ss.__r12,\n        (unsigned long) uc->uc_mcontext->__ss.__r13,\n        (unsigned long) uc->uc_mcontext->__ss.__r14,\n        (unsigned long) uc->uc_mcontext->__ss.__r15,\n        (unsigned long) uc->uc_mcontext->__ss.__rip,\n        (unsigned long) uc->uc_mcontext->__ss.__rflags,\n        (unsigned long) uc->uc_mcontext->__ss.__cs,\n        (unsigned long) uc->uc_mcontext->__ss.__fs,\n        (unsigned long) uc->uc_mcontext->__ss.__gs\n    );\n    logStackContent((void**)uc->uc_mcontext->__ss.__rsp);\n    #elif defined(__i386__)\n    /* OSX x86 */\n    serverLog(LL_WARNING,\n    \"\\n\"\n    \"EAX:%08lx EBX:%08lx ECX:%08lx EDX:%08lx\\n\"\n    \"EDI:%08lx ESI:%08lx EBP:%08lx ESP:%08lx\\n\"\n    \"SS:%08lx  EFL:%08lx EIP:%08lx CS :%08lx\\n\"\n    \"DS:%08lx  ES:%08lx  FS :%08lx GS :%08lx\",\n        (unsigned long) uc->uc_mcontext->__ss.__eax,\n        (unsigned long) uc->uc_mcontext->__ss.__ebx,\n        (unsigned long) uc->uc_mcontext->__ss.__ecx,\n        (unsigned long) uc->uc_mcontext->__ss.__edx,\n        (unsigned long) uc->uc_mcontext->__ss.__edi,\n        (unsigned long) uc->uc_mcontext->__ss.__esi,\n        (unsigned long) uc->uc_mcontext->__ss.__ebp,\n        (unsigned long) uc->uc_mcontext->__ss.__esp,\n        (unsigned long) uc->uc_mcontext->__ss.__ss,\n        (unsigned long) uc->uc_mcontext->__ss.__eflags,\n        (unsigned long) uc->uc_mcontext->__ss.__eip,\n        (unsigned long) uc->uc_mcontext->__ss.__cs,\n        (unsigned long) uc->uc_mcontext->__ss.__ds,\n        (unsigned long) uc->uc_mcontext->__ss.__es,\n        (unsigned long) uc->uc_mcontext->__ss.__fs,\n        (unsigned long) uc->uc_mcontext->__ss.__gs\n    );\n    logStackContent((void**)uc->uc_mcontext->__ss.__esp);\n    #else\n    /* OSX ARM64 */\n    serverLog(LL_WARNING,\n    \"\\n\"\n    \"x0:%016lx x1:%016lx x2:%016lx x3:%016lx\\n\"\n    \"x4:%016lx x5:%016lx x6:%016lx x7:%016lx\\n\"\n    \"x8:%016lx x9:%016lx x10:%016lx x11:%016lx\\n\"\n    \"x12:%016lx x13:%016lx x14:%016lx x15:%016lx\\n\"\n    \"x16:%016lx x17:%016lx x18:%016lx x19:%016lx\\n\"\n    \"x20:%016lx x21:%016lx x22:%016lx x23:%016lx\\n\"\n    \"x24:%016lx x25:%016lx x26:%016lx x27:%016lx\\n\"\n    \"x28:%016lx fp:%016lx lr:%016lx\\n\"\n    \"sp:%016lx pc:%016lx cpsr:%08lx\\n\",\n        (unsigned long) uc->uc_mcontext->__ss.__x[0],\n        (unsigned long) uc->uc_mcontext->__ss.__x[1],\n        (unsigned long) uc->uc_mcontext->__ss.__x[2],\n        (unsigned long) uc->uc_mcontext->__ss.__x[3],\n        (unsigned long) uc->uc_mcontext->__ss.__x[4],\n        (unsigned long) uc->uc_mcontext->__ss.__x[5],\n        (unsigned long) uc->uc_mcontext->__ss.__x[6],\n        (unsigned long) uc->uc_mcontext->__ss.__x[7],\n        (unsigned long) uc->uc_mcontext->__ss.__x[8],\n        (unsigned long) uc->uc_mcontext->__ss.__x[9],\n        (unsigned long) uc->uc_mcontext->__ss.__x[10],\n        (unsigned long) uc->uc_mcontext->__ss.__x[11],\n        (unsigned long) uc->uc_mcontext->__ss.__x[12],\n        (unsigned long) uc->uc_mcontext->__ss.__x[13],\n        (unsigned long) uc->uc_mcontext->__ss.__x[14],\n        (unsigned long) uc->uc_mcontext->__ss.__x[15],\n        (unsigned long) uc->uc_mcontext->__ss.__x[16],\n        (unsigned long) uc->uc_mcontext->__ss.__x[17],\n        (unsigned long) uc->uc_mcontext->__ss.__x[18],\n        (unsigned long) uc->uc_mcontext->__ss.__x[19],\n        (unsigned long) uc->uc_mcontext->__ss.__x[20],\n        (unsigned long) uc->uc_mcontext->__ss.__x[21],\n        (unsigned long) uc->uc_mcontext->__ss.__x[22],\n        (unsigned long) uc->uc_mcontext->__ss.__x[23],\n        (unsigned long) uc->uc_mcontext->__ss.__x[24],\n        (unsigned long) uc->uc_mcontext->__ss.__x[25],\n        (unsigned long) uc->uc_mcontext->__ss.__x[26],\n        (unsigned long) uc->uc_mcontext->__ss.__x[27],\n        (unsigned long) uc->uc_mcontext->__ss.__x[28],\n        (unsigned long) arm_thread_state64_get_fp(uc->uc_mcontext->__ss),\n        (unsigned long) arm_thread_state64_get_lr(uc->uc_mcontext->__ss),\n        (unsigned long) arm_thread_state64_get_sp(uc->uc_mcontext->__ss),\n        (unsigned long) arm_thread_state64_get_pc(uc->uc_mcontext->__ss),\n        (unsigned long) uc->uc_mcontext->__ss.__cpsr\n    );\n    logStackContent((void**) arm_thread_state64_get_sp(uc->uc_mcontext->__ss));\n    #endif\n/* Linux */\n#elif defined(__linux__)\n    /* Linux x86 */\n    #if defined(__i386__) || ((defined(__X86_64__) || defined(__x86_64__)) && defined(__ILP32__))\n    serverLog(LL_WARNING,\n    \"\\n\"\n    \"EAX:%08lx EBX:%08lx ECX:%08lx EDX:%08lx\\n\"\n    \"EDI:%08lx ESI:%08lx EBP:%08lx ESP:%08lx\\n\"\n    \"SS :%08lx EFL:%08lx EIP:%08lx CS:%08lx\\n\"\n    \"DS :%08lx ES :%08lx FS :%08lx GS:%08lx\",\n        (unsigned long) uc->uc_mcontext.gregs[11],\n        (unsigned long) uc->uc_mcontext.gregs[8],\n        (unsigned long) uc->uc_mcontext.gregs[10],\n        (unsigned long) uc->uc_mcontext.gregs[9],\n        (unsigned long) uc->uc_mcontext.gregs[4],\n        (unsigned long) uc->uc_mcontext.gregs[5],\n        (unsigned long) uc->uc_mcontext.gregs[6],\n        (unsigned long) uc->uc_mcontext.gregs[7],\n        (unsigned long) uc->uc_mcontext.gregs[18],\n        (unsigned long) uc->uc_mcontext.gregs[17],\n        (unsigned long) uc->uc_mcontext.gregs[14],\n        (unsigned long) uc->uc_mcontext.gregs[15],\n        (unsigned long) uc->uc_mcontext.gregs[3],\n        (unsigned long) uc->uc_mcontext.gregs[2],\n        (unsigned long) uc->uc_mcontext.gregs[1],\n        (unsigned long) uc->uc_mcontext.gregs[0]\n    );\n    logStackContent((void**)uc->uc_mcontext.gregs[7]);\n    #elif defined(__X86_64__) || defined(__x86_64__)\n    /* Linux AMD64 */\n    serverLog(LL_WARNING,\n    \"\\n\"\n    \"RAX:%016lx RBX:%016lx\\nRCX:%016lx RDX:%016lx\\n\"\n    \"RDI:%016lx RSI:%016lx\\nRBP:%016lx RSP:%016lx\\n\"\n    \"R8 :%016lx R9 :%016lx\\nR10:%016lx R11:%016lx\\n\"\n    \"R12:%016lx R13:%016lx\\nR14:%016lx R15:%016lx\\n\"\n    \"RIP:%016lx EFL:%016lx\\nCSGSFS:%016lx\",\n        (unsigned long) uc->uc_mcontext.gregs[13],\n        (unsigned long) uc->uc_mcontext.gregs[11],\n        (unsigned long) uc->uc_mcontext.gregs[14],\n        (unsigned long) uc->uc_mcontext.gregs[12],\n        (unsigned long) uc->uc_mcontext.gregs[8],\n        (unsigned long) uc->uc_mcontext.gregs[9],\n        (unsigned long) uc->uc_mcontext.gregs[10],\n        (unsigned long) uc->uc_mcontext.gregs[15],\n        (unsigned long) uc->uc_mcontext.gregs[0],\n        (unsigned long) uc->uc_mcontext.gregs[1],\n        (unsigned long) uc->uc_mcontext.gregs[2],\n        (unsigned long) uc->uc_mcontext.gregs[3],\n        (unsigned long) uc->uc_mcontext.gregs[4],\n        (unsigned long) uc->uc_mcontext.gregs[5],\n        (unsigned long) uc->uc_mcontext.gregs[6],\n        (unsigned long) uc->uc_mcontext.gregs[7],\n        (unsigned long) uc->uc_mcontext.gregs[16],\n        (unsigned long) uc->uc_mcontext.gregs[17],\n        (unsigned long) uc->uc_mcontext.gregs[18]\n    );\n    logStackContent((void**)uc->uc_mcontext.gregs[15]);\n    #elif defined(__aarch64__) /* Linux AArch64 */\n    serverLog(LL_WARNING,\n\t      \"\\n\"\n\t      \"X18:%016lx X19:%016lx\\nX20:%016lx X21:%016lx\\n\"\n\t      \"X22:%016lx X23:%016lx\\nX24:%016lx X25:%016lx\\n\"\n\t      \"X26:%016lx X27:%016lx\\nX28:%016lx X29:%016lx\\n\"\n\t      \"X30:%016lx\\n\"\n\t      \"pc:%016lx sp:%016lx\\npstate:%016lx fault_address:%016lx\\n\",\n\t      (unsigned long) uc->uc_mcontext.regs[18],\n\t      (unsigned long) uc->uc_mcontext.regs[19],\n\t      (unsigned long) uc->uc_mcontext.regs[20],\n\t      (unsigned long) uc->uc_mcontext.regs[21],\n\t      (unsigned long) uc->uc_mcontext.regs[22],\n\t      (unsigned long) uc->uc_mcontext.regs[23],\n\t      (unsigned long) uc->uc_mcontext.regs[24],\n\t      (unsigned long) uc->uc_mcontext.regs[25],\n\t      (unsigned long) uc->uc_mcontext.regs[26],\n\t      (unsigned long) uc->uc_mcontext.regs[27],\n\t      (unsigned long) uc->uc_mcontext.regs[28],\n\t      (unsigned long) uc->uc_mcontext.regs[29],\n\t      (unsigned long) uc->uc_mcontext.regs[30],\n\t      (unsigned long) uc->uc_mcontext.pc,\n\t      (unsigned long) uc->uc_mcontext.sp,\n\t      (unsigned long) uc->uc_mcontext.pstate,\n\t      (unsigned long) uc->uc_mcontext.fault_address\n\t\t      );\n\t      logStackContent((void**)uc->uc_mcontext.sp);\n    #elif defined(__arm__) /* Linux ARM */\n    serverLog(LL_WARNING,\n\t      \"\\n\"\n\t      \"R10:%016lx R9 :%016lx\\nR8 :%016lx R7 :%016lx\\n\"\n\t      \"R6 :%016lx R5 :%016lx\\nR4 :%016lx R3 :%016lx\\n\"\n\t      \"R2 :%016lx R1 :%016lx\\nR0 :%016lx EC :%016lx\\n\"\n\t      \"fp: %016lx ip:%016lx\\n\"\n\t      \"pc:%016lx sp:%016lx\\ncpsr:%016lx fault_address:%016lx\\n\",\n\t      (unsigned long) uc->uc_mcontext.arm_r10,\n\t      (unsigned long) uc->uc_mcontext.arm_r9,\n\t      (unsigned long) uc->uc_mcontext.arm_r8,\n\t      (unsigned long) uc->uc_mcontext.arm_r7,\n\t      (unsigned long) uc->uc_mcontext.arm_r6,\n\t      (unsigned long) uc->uc_mcontext.arm_r5,\n\t      (unsigned long) uc->uc_mcontext.arm_r4,\n\t      (unsigned long) uc->uc_mcontext.arm_r3,\n\t      (unsigned long) uc->uc_mcontext.arm_r2,\n\t      (unsigned long) uc->uc_mcontext.arm_r1,\n\t      (unsigned long) uc->uc_mcontext.arm_r0,\n\t      (unsigned long) uc->uc_mcontext.error_code,\n\t      (unsigned long) uc->uc_mcontext.arm_fp,\n\t      (unsigned long) uc->uc_mcontext.arm_ip,\n\t      (unsigned long) uc->uc_mcontext.arm_pc,\n\t      (unsigned long) uc->uc_mcontext.arm_sp,\n\t      (unsigned long) uc->uc_mcontext.arm_cpsr,\n\t      (unsigned long) uc->uc_mcontext.fault_address\n\t\t      );\n\t      logStackContent((void**)uc->uc_mcontext.arm_sp);\n    #endif\n#elif defined(__FreeBSD__)\n    #if defined(__x86_64__)\n    serverLog(LL_WARNING,\n    \"\\n\"\n    \"RAX:%016lx RBX:%016lx\\nRCX:%016lx RDX:%016lx\\n\"\n    \"RDI:%016lx RSI:%016lx\\nRBP:%016lx RSP:%016lx\\n\"\n    \"R8 :%016lx R9 :%016lx\\nR10:%016lx R11:%016lx\\n\"\n    \"R12:%016lx R13:%016lx\\nR14:%016lx R15:%016lx\\n\"\n    \"RIP:%016lx EFL:%016lx\\nCSGSFS:%016lx\",\n        (unsigned long) uc->uc_mcontext.mc_rax,\n        (unsigned long) uc->uc_mcontext.mc_rbx,\n        (unsigned long) uc->uc_mcontext.mc_rcx,\n        (unsigned long) uc->uc_mcontext.mc_rdx,\n        (unsigned long) uc->uc_mcontext.mc_rdi,\n        (unsigned long) uc->uc_mcontext.mc_rsi,\n        (unsigned long) uc->uc_mcontext.mc_rbp,\n        (unsigned long) uc->uc_mcontext.mc_rsp,\n        (unsigned long) uc->uc_mcontext.mc_r8,\n        (unsigned long) uc->uc_mcontext.mc_r9,\n        (unsigned long) uc->uc_mcontext.mc_r10,\n        (unsigned long) uc->uc_mcontext.mc_r11,\n        (unsigned long) uc->uc_mcontext.mc_r12,\n        (unsigned long) uc->uc_mcontext.mc_r13,\n        (unsigned long) uc->uc_mcontext.mc_r14,\n        (unsigned long) uc->uc_mcontext.mc_r15,\n        (unsigned long) uc->uc_mcontext.mc_rip,\n        (unsigned long) uc->uc_mcontext.mc_rflags,\n        (unsigned long) uc->uc_mcontext.mc_cs\n    );\n    logStackContent((void**)uc->uc_mcontext.mc_rsp);\n    #elif defined(__i386__)\n    serverLog(LL_WARNING,\n    \"\\n\"\n    \"EAX:%08lx EBX:%08lx ECX:%08lx EDX:%08lx\\n\"\n    \"EDI:%08lx ESI:%08lx EBP:%08lx ESP:%08lx\\n\"\n    \"SS :%08lx EFL:%08lx EIP:%08lx CS:%08lx\\n\"\n    \"DS :%08lx ES :%08lx FS :%08lx GS:%08lx\",\n        (unsigned long) uc->uc_mcontext.mc_eax,\n        (unsigned long) uc->uc_mcontext.mc_ebx,\n        (unsigned long) uc->uc_mcontext.mc_ebx,\n        (unsigned long) uc->uc_mcontext.mc_edx,\n        (unsigned long) uc->uc_mcontext.mc_edi,\n        (unsigned long) uc->uc_mcontext.mc_esi,\n        (unsigned long) uc->uc_mcontext.mc_ebp,\n        (unsigned long) uc->uc_mcontext.mc_esp,\n        (unsigned long) uc->uc_mcontext.mc_ss,\n        (unsigned long) uc->uc_mcontext.mc_eflags,\n        (unsigned long) uc->uc_mcontext.mc_eip,\n        (unsigned long) uc->uc_mcontext.mc_cs,\n        (unsigned long) uc->uc_mcontext.mc_es,\n        (unsigned long) uc->uc_mcontext.mc_fs,\n        (unsigned long) uc->uc_mcontext.mc_gs\n    );\n    logStackContent((void**)uc->uc_mcontext.mc_esp);\n    #endif\n#elif defined(__OpenBSD__)\n    #if defined(__x86_64__)\n    serverLog(LL_WARNING,\n    \"\\n\"\n    \"RAX:%016lx RBX:%016lx\\nRCX:%016lx RDX:%016lx\\n\"\n    \"RDI:%016lx RSI:%016lx\\nRBP:%016lx RSP:%016lx\\n\"\n    \"R8 :%016lx R9 :%016lx\\nR10:%016lx R11:%016lx\\n\"\n    \"R12:%016lx R13:%016lx\\nR14:%016lx R15:%016lx\\n\"\n    \"RIP:%016lx EFL:%016lx\\nCSGSFS:%016lx\",\n        (unsigned long) uc->sc_rax,\n        (unsigned long) uc->sc_rbx,\n        (unsigned long) uc->sc_rcx,\n        (unsigned long) uc->sc_rdx,\n        (unsigned long) uc->sc_rdi,\n        (unsigned long) uc->sc_rsi,\n        (unsigned long) uc->sc_rbp,\n        (unsigned long) uc->sc_rsp,\n        (unsigned long) uc->sc_r8,\n        (unsigned long) uc->sc_r9,\n        (unsigned long) uc->sc_r10,\n        (unsigned long) uc->sc_r11,\n        (unsigned long) uc->sc_r12,\n        (unsigned long) uc->sc_r13,\n        (unsigned long) uc->sc_r14,\n        (unsigned long) uc->sc_r15,\n        (unsigned long) uc->sc_rip,\n        (unsigned long) uc->sc_rflags,\n        (unsigned long) uc->sc_cs\n    );\n    logStackContent((void**)uc->sc_rsp);\n    #elif defined(__i386__)\n    serverLog(LL_WARNING,\n    \"\\n\"\n    \"EAX:%08lx EBX:%08lx ECX:%08lx EDX:%08lx\\n\"\n    \"EDI:%08lx ESI:%08lx EBP:%08lx ESP:%08lx\\n\"\n    \"SS :%08lx EFL:%08lx EIP:%08lx CS:%08lx\\n\"\n    \"DS :%08lx ES :%08lx FS :%08lx GS:%08lx\",\n        (unsigned long) uc->sc_eax,\n        (unsigned long) uc->sc_ebx,\n        (unsigned long) uc->sc_ebx,\n        (unsigned long) uc->sc_edx,\n        (unsigned long) uc->sc_edi,\n        (unsigned long) uc->sc_esi,\n        (unsigned long) uc->sc_ebp,\n        (unsigned long) uc->sc_esp,\n        (unsigned long) uc->sc_ss,\n        (unsigned long) uc->sc_eflags,\n        (unsigned long) uc->sc_eip,\n        (unsigned long) uc->sc_cs,\n        (unsigned long) uc->sc_es,\n        (unsigned long) uc->sc_fs,\n        (unsigned long) uc->sc_gs\n    );\n    logStackContent((void**)uc->sc_esp);\n    #endif\n#elif defined(__NetBSD__)\n    #if defined(__x86_64__)\n    serverLog(LL_WARNING,\n    \"\\n\"\n    \"RAX:%016lx RBX:%016lx\\nRCX:%016lx RDX:%016lx\\n\"\n    \"RDI:%016lx RSI:%016lx\\nRBP:%016lx RSP:%016lx\\n\"\n    \"R8 :%016lx R9 :%016lx\\nR10:%016lx R11:%016lx\\n\"\n    \"R12:%016lx R13:%016lx\\nR14:%016lx R15:%016lx\\n\"\n    \"RIP:%016lx EFL:%016lx\\nCSGSFS:%016lx\",\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_RAX],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_RBX],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_RCX],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_RDX],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_RDI],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_RSI],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_RBP],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_RSP],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_R8],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_R9],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_R10],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_R11],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_R12],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_R13],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_R14],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_R15],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_RIP],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_RFLAGS],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_CS]\n    );\n    logStackContent((void**)uc->uc_mcontext.__gregs[_REG_RSP]);\n    #elif defined(__i386__)\n    serverLog(LL_WARNING,\n    \"\\n\"\n    \"EAX:%08lx EBX:%08lx ECX:%08lx EDX:%08lx\\n\"\n    \"EDI:%08lx ESI:%08lx EBP:%08lx ESP:%08lx\\n\"\n    \"SS :%08lx EFL:%08lx EIP:%08lx CS:%08lx\\n\"\n    \"DS :%08lx ES :%08lx FS :%08lx GS:%08lx\",\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_EAX],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_EBX],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_EDX],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_EDI],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_ESI],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_EBP],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_ESP],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_SS],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_EFLAGS],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_EIP],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_CS],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_ES],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_FS],\n        (unsigned long) uc->uc_mcontext.__gregs[_REG_GS]\n    );\n    #endif\n#elif defined(__DragonFly__)\n    serverLog(LL_WARNING,\n    \"\\n\"\n    \"RAX:%016lx RBX:%016lx\\nRCX:%016lx RDX:%016lx\\n\"\n    \"RDI:%016lx RSI:%016lx\\nRBP:%016lx RSP:%016lx\\n\"\n    \"R8 :%016lx R9 :%016lx\\nR10:%016lx R11:%016lx\\n\"\n    \"R12:%016lx R13:%016lx\\nR14:%016lx R15:%016lx\\n\"\n    \"RIP:%016lx EFL:%016lx\\nCSGSFS:%016lx\",\n        (unsigned long) uc->uc_mcontext.mc_rax,\n        (unsigned long) uc->uc_mcontext.mc_rbx,\n        (unsigned long) uc->uc_mcontext.mc_rcx,\n        (unsigned long) uc->uc_mcontext.mc_rdx,\n        (unsigned long) uc->uc_mcontext.mc_rdi,\n        (unsigned long) uc->uc_mcontext.mc_rsi,\n        (unsigned long) uc->uc_mcontext.mc_rbp,\n        (unsigned long) uc->uc_mcontext.mc_rsp,\n        (unsigned long) uc->uc_mcontext.mc_r8,\n        (unsigned long) uc->uc_mcontext.mc_r9,\n        (unsigned long) uc->uc_mcontext.mc_r10,\n        (unsigned long) uc->uc_mcontext.mc_r11,\n        (unsigned long) uc->uc_mcontext.mc_r12,\n        (unsigned long) uc->uc_mcontext.mc_r13,\n        (unsigned long) uc->uc_mcontext.mc_r14,\n        (unsigned long) uc->uc_mcontext.mc_r15,\n        (unsigned long) uc->uc_mcontext.mc_rip,\n        (unsigned long) uc->uc_mcontext.mc_rflags,\n        (unsigned long) uc->uc_mcontext.mc_cs\n    );\n    logStackContent((void**)uc->uc_mcontext.mc_rsp);\n#else\n    serverLog(LL_WARNING,\n        \"  Dumping of registers not supported for this OS/arch\");\n#endif\n}\n\n#endif /* HAVE_BACKTRACE */\n\n/* Return a file descriptor to write directly to the Redis log with the\n * write(2) syscall, that can be used in critical sections of the code\n * where the rest of Redis can't be trusted (for example during the memory\n * test) or when an API call requires a raw fd.\n *\n * Close it with closeDirectLogFiledes(). */\nint openDirectLogFiledes(void) {\n    int log_to_stdout = g_pserver->logfile[0] == '\\0';\n    int fd = log_to_stdout ?\n        STDOUT_FILENO :\n        open(g_pserver->logfile, O_APPEND|O_CREAT|O_WRONLY, 0644);\n    return fd;\n}\n\n/* Used to close what closeDirectLogFiledes() returns. */\nvoid closeDirectLogFiledes(int fd) {\n    int log_to_stdout = g_pserver->logfile[0] == '\\0';\n    if (!log_to_stdout) close(fd);\n}\n\nvoid safe_write(int fd, const void *pv, ssize_t cb)\n{\n    ssize_t offset = 0;\n    do\n    {\n        ssize_t cbWrite = write(fd, reinterpret_cast<const char*>(pv)+offset, cb-offset);\n        if (cbWrite <= 0)\n            return;\n        offset += cbWrite;\n    } while (offset < cb);\n}\n\n#ifdef UNW_LOCAL_ONLY\n\n/* Logs the stack trace using the libunwind call.\n * The eip argument is unused as libunwind only gets local context.\n * The uplevel argument indicates how many of the calling functions to skip.\n */\nvoid logStackTrace(void * eip, int uplevel) {\n    (void)eip;//UNUSED\n    const char *msg;\n    int fd = openDirectLogFiledes();\n\n    if (fd == -1) return; /* If we can't log there is anything to do. */\n\n    msg = \"\\n------ STACK TRACE ------\\n\";\n    if (write(fd,msg,strlen(msg)) == -1) {/* Avoid warning. */};\n    unw_cursor_t cursor;\n    unw_context_t context;\n\n    unw_getcontext(&context);\n    unw_init_local(&cursor, &context);\n\n    /* Write symbols to log file */\n    msg = \"\\nBacktrace:\\n\";\n    if (write(fd,msg,strlen(msg)) == -1) {/* Avoid warning. */};\n\n    for (int i = 0; i < uplevel; i++) {\n        unw_step(&cursor);\n    }\n\n    while ( unw_step(&cursor) ) {\n    unw_word_t ip, sp, off;\n\n    unw_get_reg(&cursor, UNW_REG_IP, &ip);\n    unw_get_reg(&cursor, UNW_REG_SP, &sp);\n\n    char symbol[256] = {\"<unknown>\"};\n    char *name = symbol;\n\n    if ( !unw_get_proc_name(&cursor, symbol, sizeof(symbol), &off) ) {\n        int status;\n        if ( (name = abi::__cxa_demangle(symbol, NULL, NULL, &status)) == 0 )\n        name = symbol;\n    }\n\n    dprintf(fd, \"%s(+0x%\" PRIxPTR \") [0x%016\" PRIxPTR \"] sp=0x%016\" PRIxPTR \"\\n\",\n        name,\n        static_cast<uintptr_t>(off),\n        static_cast<uintptr_t>(ip),\n        static_cast<uintptr_t>(sp));\n\n    if ( name != symbol )\n        free(name);\n    }\n}\n\n#endif /* UNW_LOCAL_ONLY */\n\n#ifdef HAVE_BACKTRACE\n\nvoid backtrace_symbols_demangle_fd(void **trace, size_t csym, int fd)\n{\n    char **syms = backtrace_symbols(trace, csym);\n    char symbuf[1024];\n    for (size_t itrace = 0; itrace < csym; ++itrace)\n    {\n        int status = 0;\n\n        // First find the symbol (preceded by a '(')\n        char *pchSymStart = syms[itrace];\n        while (*pchSymStart != '(' && *pchSymStart != '\\0')\n            ++pchSymStart;\n        if (*pchSymStart != '\\0')\n            ++pchSymStart;  // skip the '('\n        char *pchSymEnd = pchSymStart;\n        while (*pchSymEnd != '+' && *pchSymEnd != '\\0')\n            ++pchSymEnd;\n\n        if ((pchSymEnd - pchSymStart) < 1023)\n        {\n            memcpy(symbuf, pchSymStart, pchSymEnd - pchSymStart);\n            symbuf[pchSymEnd - pchSymStart] = '\\0';\n            char *sz = abi::__cxa_demangle(symbuf, nullptr, nullptr, &status);\n            if (status == 0)\n            {\n                safe_write(fd, syms[itrace], pchSymStart - syms[itrace]);\n                safe_write(fd, sz, strlen(sz));\n                safe_write(fd, pchSymEnd, (syms[itrace] + strlen(syms[itrace])-pchSymEnd));\n            }\n            else\n            {\n                safe_write(fd, syms[itrace], strlen(syms[itrace]));\n            }\n            free(sz);\n        }\n        else {\n            safe_write(fd, syms[itrace], strlen(syms[itrace]));\n        }\n        safe_write(fd, \"\\n\", 1);   \n    }\n    free(syms);\n}\n\n/* Logs the stack trace using the backtrace() call. This function is designed\n * to be called from signal handlers safely.\n * The eip argument is optional (can take NULL).\n * The uplevel argument indicates how many of the calling functions to skip.\n */\nvoid logStackTrace(void *eip, int uplevel) {\n    void *trace[100];\n    int trace_size = 0, fd = openDirectLogFiledes();\n    const char *msg;\n    uplevel++; /* skip this function */\n\n    if (fd == -1) return; /* If we can't log there is anything to do. */\n\n    /* Get the stack trace first! */\n    trace_size = backtrace(trace, 100);\n\n    msg = \"\\n------ STACK TRACE ------\\n\";\n    if (write(fd,msg,strlen(msg)) == -1) {/* Avoid warning. */};\n\n    if (eip) {\n        /* Write EIP to the log file*/\n        msg = \"EIP:\\n\";\n        if (write(fd,msg,strlen(msg)) == -1) {/* Avoid warning. */};\n        backtrace_symbols_demangle_fd(&eip, 1, fd);\n    }\n\n    /* Write symbols to log file */\n    msg = \"\\nBacktrace:\\n\";\n    if (write(fd,msg,strlen(msg)) == -1) {/* Avoid warning. */};\n    backtrace_symbols_demangle_fd(trace+uplevel, trace_size-uplevel, fd);\n\n    /* Cleanup */\n    closeDirectLogFiledes(fd);\n}\n\n#endif /* HAVE_BACKTRACE */\n\n/* Log global server info */\nvoid logServerInfo(void) {\n    sds infostring, clients;\n    serverLogRaw(LL_WARNING|LL_RAW, \"\\n------ INFO OUTPUT ------\\n\");\n    infostring = genRedisInfoString(\"all\");\n    serverLogRaw(LL_WARNING|LL_RAW, infostring);\n    serverLogRaw(LL_WARNING|LL_RAW, \"\\n------ CLIENT LIST OUTPUT ------\\n\");\n    clients = getAllClientsInfoString(-1);\n    serverLogRaw(LL_WARNING|LL_RAW, clients);\n    sdsfree(infostring);\n    sdsfree(clients);\n}\n\n/* Log modules info. Something we wanna do last since we fear it may crash. */\nvoid logModulesInfo(void) {\n    serverLogRaw(LL_WARNING|LL_RAW, \"\\n------ MODULES INFO OUTPUT ------\\n\");\n    sds infostring = modulesCollectInfo(sdsempty(), NULL, 1, 0);\n    serverLogRaw(LL_WARNING|LL_RAW, infostring);\n    sdsfree(infostring);\n}\n\n/* Log information about the \"current\" client, that is, the client that is\n * currently being served by Redis. May be NULL if Redis is not serving a\n * client right now. */\nvoid logCurrentClient(void) {\n    if (serverTL->current_client == NULL) return;\n\n    client *cc = serverTL->current_client;\n    sds client;\n    int j;\n\n    serverLogRaw(LL_WARNING|LL_RAW, \"\\n------ CURRENT CLIENT INFO ------\\n\");\n    client = catClientInfoString(sdsempty(),cc);\n    serverLog(LL_WARNING|LL_RAW,\"%s\\n\", client);\n    sdsfree(client);\n    for (j = 0; j < cc->argc; j++) {\n        robj *decoded;\n\n        decoded = getDecodedObject(cc->argv[j]);\n        serverLog(LL_WARNING|LL_RAW,\"argv[%d]: '%s'\\n\", j,\n            (char*)ptrFromObj(decoded));\n        decrRefCount(decoded);\n    }\n    /* Check if the first argument, usually a key, is found inside the\n     * selected DB, and if so print info about the associated object. */\n    if (cc->argc > 1) {\n        robj *val, *key;\n\n        key = getDecodedObject(cc->argv[1]);\n        val = cc->db->find(key);\n        if (val) {\n            serverLog(LL_WARNING,\"key '%s' found in DB containing the following object:\", (char*)ptrFromObj(key));\n            serverLogObjectDebugInfo(val);\n        }\n        decrRefCount(key);\n    }\n}\n\n#if defined(HAVE_PROC_MAPS)\n\n#define MEMTEST_MAX_REGIONS 128\n\n/* A non destructive memory test executed during segfault. */\nint memtest_test_linux_anonymous_maps(void) {\n    FILE *fp;\n    char line[1024];\n    char logbuf[1024];\n    size_t start_addr, end_addr, size;\n    size_t start_vect[MEMTEST_MAX_REGIONS];\n    size_t size_vect[MEMTEST_MAX_REGIONS];\n    int regions = 0, j;\n\n    int fd = openDirectLogFiledes();\n    if (!fd) return 0;\n\n    fp = fopen(\"/proc/self/maps\",\"r\");\n    if (!fp) return 0;\n    while(fgets(line,sizeof(line),fp) != NULL) {\n        char *start, *end, *p = line;\n\n        start = p;\n        p = strchr(p,'-');\n        if (!p) continue;\n        *p++ = '\\0';\n        end = p;\n        p = strchr(p,' ');\n        if (!p) continue;\n        *p++ = '\\0';\n        if (strstr(p,\"stack\") ||\n            strstr(p,\"vdso\") ||\n            strstr(p,\"vsyscall\")) continue;\n        if (!strstr(p,\"00:00\")) continue;\n        if (!strstr(p,\"rw\")) continue;\n\n        start_addr = strtoul(start,NULL,16);\n        end_addr = strtoul(end,NULL,16);\n        size = end_addr-start_addr;\n\n        start_vect[regions] = start_addr;\n        size_vect[regions] = size;\n        snprintf(logbuf,sizeof(logbuf),\n            \"*** Preparing to test memory region %lx (%lu bytes)\\n\",\n                (unsigned long) start_vect[regions],\n                (unsigned long) size_vect[regions]);\n        if (write(fd,logbuf,strlen(logbuf)) == -1) { /* Nothing to do. */ }\n        regions++;\n    }\n\n    int errors = 0;\n    for (j = 0; j < regions; j++) {\n        if (write(fd,\".\",1) == -1) { /* Nothing to do. */ }\n        errors += memtest_preserving_test((unsigned long*)start_vect[j],size_vect[j],1);\n        if (write(fd, errors ? \"E\" : \"O\",1) == -1) { /* Nothing to do. */ }\n    }\n    if (write(fd,\"\\n\",1) == -1) { /* Nothing to do. */ }\n\n    /* NOTE: It is very important to close the file descriptor only now\n     * because closing it before may result into unmapping of some memory\n     * region that we are testing. */\n    fclose(fp);\n    closeDirectLogFiledes(fd);\n    return errors;\n}\n#endif /* HAVE_PROC_MAPS */\n\nstatic void killServerThreads(void) {\n    int err;\n    for (int i = 0; i < cserver.cthreads; i++) {\n        if (g_pserver->rgthread[i] != pthread_self()) {\n            pthread_cancel(g_pserver->rgthread[i]);\n        }\n    }\n    if (pthread_self() != cserver.main_thread_id && pthread_cancel(cserver.main_thread_id) == 0) {\n        if ((err = pthread_join(cserver.main_thread_id,NULL)) != 0) {\n            serverLog(LL_WARNING, \"main thread can not be joined: %s\", strerror(err));\n        } else {\n            serverLog(LL_WARNING, \"main thread terminated\");\n        }\n    }\n}\n\n/* Kill the running threads (other than current) in an unclean way. This function\n * should be used only when it's critical to stop the threads for some reason.\n * Currently Redis does this only on crash (for instance on SIGSEGV) in order\n * to perform a fast memory check without other threads messing with memory. */\nvoid killThreads(void) {\n    killServerThreads();\n    bioKillThreads();\n}\n\nvoid doFastMemoryTest(void) {\n#if defined(HAVE_PROC_MAPS)\n    if (g_pserver->memcheck_enabled) {\n        /* Test memory */\n        serverLogRaw(LL_WARNING|LL_RAW, \"\\n------ FAST MEMORY TEST ------\\n\");\n        killThreads();\n        if (memtest_test_linux_anonymous_maps()) {\n            serverLogRaw(LL_WARNING|LL_RAW,\n                \"!!! MEMORY ERROR DETECTED! Check your memory ASAP !!!\\n\");\n        } else {\n            serverLogRaw(LL_WARNING|LL_RAW,\n                \"Fast memory test PASSED, however your memory can still be broken. Please run a memory test for several hours if possible.\\n\");\n        }\n    }\n#endif /* HAVE_PROC_MAPS */\n}\n\n/* Scans the (assumed) x86 code starting at addr, for a max of `len`\n * bytes, searching for E8 (callq) opcodes, and dumping the symbols\n * and the call offset if they appear to be valid. */\nvoid dumpX86Calls(void *addr, size_t len) {\n    size_t j;\n    unsigned char *p = (unsigned char*)addr;\n    Dl_info info;\n    /* Hash table to best-effort avoid printing the same symbol\n     * multiple times. */\n    unsigned long ht[256] = {0};\n\n    if (len < 5) return;\n    for (j = 0; j < len-4; j++) {\n        if (p[j] != 0xE8) continue; /* Not an E8 CALL opcode. */\n        unsigned long target = (unsigned long)addr+j+5;\n        target += *((int32_t*)(p+j+1));\n        if (dladdr((void*)target, &info) != 0 && info.dli_sname != NULL) {\n            if (ht[target&0xff] != target) {\n                printf(\"Function at 0x%lx is %s\\n\",target,info.dli_sname);\n                ht[target&0xff] = target;\n            }\n            j += 4; /* Skip the 32 bit immediate. */\n        }\n    }\n}\n\nvoid dumpCodeAroundEIP(void *eip) {\n    Dl_info info;\n    if (dladdr(eip, &info) != 0) {\n        serverLog(LL_WARNING|LL_RAW,\n            \"\\n------ DUMPING CODE AROUND EIP ------\\n\"\n            \"Symbol: %s (base: %p)\\n\"\n            \"Module: %s (base %p)\\n\"\n            \"$ xxd -r -p /tmp/dump.hex /tmp/dump.bin\\n\"\n            \"$ objdump --adjust-vma=%p -D -b binary -m i386:x86-64 /tmp/dump.bin\\n\"\n            \"------\\n\",\n            info.dli_sname, info.dli_saddr, info.dli_fname, info.dli_fbase,\n            info.dli_saddr);\n        size_t len = (long)eip - (long)info.dli_saddr;\n        unsigned long sz = sysconf(_SC_PAGESIZE);\n        if (len < 1<<13) { /* we don't have functions over 8k (verified) */\n            /* Find the address of the next page, which is our \"safety\"\n             * limit when dumping. Then try to dump just 128 bytes more\n             * than EIP if there is room, or stop sooner. */\n            void *base = (void *)info.dli_saddr;\n            unsigned long next = ((unsigned long)eip + sz) & ~(sz-1);\n            unsigned long end = (unsigned long)eip + 128;\n            if (end > next) end = next;\n            len = end - (unsigned long)base;\n            serverLogHexDump(LL_WARNING, \"dump of function\",\n                base, len);\n            dumpX86Calls(base, len);\n        }\n    }\n}\n\nvoid sigsegvHandler(int sig, siginfo_t *info, void *secret) {\n    UNUSED(secret);\n    UNUSED(info);\n    g_fInCrash = true;\n\n    bugReportStart();\n    serverLog(LL_WARNING,\n        \"KeyDB %s crashed by signal: %d, si_code: %d\", KEYDB_REAL_VERSION, sig, info->si_code);\n    if (sig == SIGSEGV || sig == SIGBUS) {\n        serverLog(LL_WARNING,\n        \"Accessing address: %p\", (void*)info->si_addr);\n    }\n    if (info->si_code <= SI_USER && info->si_pid != -1) {\n        serverLog(LL_WARNING, \"Killed by PID: %ld, UID: %d\", (long) info->si_pid, info->si_uid);\n    }\n\n#ifdef HAVE_BACKTRACE\n    ucontext_t *uc = (ucontext_t*) secret;\n    void *eip = getMcontextEip(uc);\n    if (eip != NULL) {\n        serverLog(LL_WARNING,\n        \"Crashed running the instruction at: %p\", eip);\n    }\n\n    logStackTrace(getMcontextEip(uc), 1);\n\n    logRegisters(uc);\n#endif\n#ifdef UNW_LOCAL_ONLY\n    logStackTrace(NULL, 1);\n#endif \n\n    printCrashReport();\n\n#ifdef HAVE_BACKTRACE\n    if (eip != NULL)\n        dumpCodeAroundEIP(eip);\n#endif\n\n    bugReportEnd(1, sig);\n}\n\nvoid printCrashReport(void) {\n    g_fInCrash = true;\n\n    /* Log INFO and CLIENT LIST */\n    logServerInfo();\n\n    /* Log the current client */\n    logCurrentClient();\n\n    /* Log modules info. Something we wanna do last since we fear it may crash. */\n    logModulesInfo();\n\n    /* Run memory test in case the crash was triggered by memory corruption. */\n    doFastMemoryTest();\n}\n\nvoid bugReportEnd(int killViaSignal, int sig) {\n    struct sigaction act;\n\n    serverLogRaw(LL_WARNING|LL_RAW,\n\"\\n=== KEYDB BUG REPORT END. Make sure to include from START to END. ===\\n\\n\"\n\"       Please report the crash by opening an issue on github:\\n\\n\"\n\"           https://github.com/JohnSully/KeyDB/issues\\n\\n\"\n\"  Suspect RAM error? Use keydb-server --test-memory to verify it.\\n\\n\"\n);\n\n    /* free(messages); Don't call free() with possibly corrupted memory. */\n    if (cserver.daemonize && cserver.supervised == 0 && cserver.pidfile) unlink(cserver.pidfile);\n\n    if (!killViaSignal) {\n        if (g_pserver->use_exit_on_panic)\n            exit(1);\n        abort();\n    }\n\n    /* Make sure we exit with the right signal at the end. So for instance\n     * the core will be dumped if enabled. */\n    sigemptyset (&act.sa_mask);\n    act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_RESETHAND;\n    act.sa_handler = SIG_DFL;\n    sigaction (sig, &act, NULL);\n    kill(getpid(),sig);\n}\n\n/* ==================== Logging functions for debugging ===================== */\n\nvoid serverLogHexDump(int level, const char *descr, void *value, size_t len) {\n    char buf[65], *b;\n    unsigned char *v = (unsigned char*)value;\n    char charset[] = \"0123456789abcdef\";\n\n    serverLog(level,\"%s (hexdump of %zu bytes):\", descr, len);\n    b = buf;\n    while(len) {\n        b[0] = charset[(*v)>>4];\n        b[1] = charset[(*v)&0xf];\n        b[2] = '\\0';\n        b += 2;\n        len--;\n        v++;\n        if (b-buf == 64 || len == 0) {\n            serverLogRaw(level|LL_RAW,buf);\n            b = buf;\n        }\n    }\n    serverLogRaw(level|LL_RAW,\"\\n\");\n}\n\n/* =========================== Software Watchdog ============================ */\n#include <sys/time.h>\n\nvoid watchdogSignalHandler(int sig, siginfo_t *info, void *secret) {\n#ifdef HAVE_BACKTRACE\n    ucontext_t *uc = (ucontext_t*) secret;\n#else\n    (void)secret;\n#endif\n    UNUSED(info);\n    UNUSED(sig);\n\n    serverLogFromHandler(LL_WARNING,\"\\n--- WATCHDOG TIMER EXPIRED ---\");\n#ifdef HAVE_BACKTRACE\n    logStackTrace(getMcontextEip(uc), 1);\n#elif defined UNW_LOCAL_ONLY\n    logStackTrace(NULL, 1);\n#else\n    serverLogFromHandler(LL_WARNING,\"Sorry: no support for backtrace().\");\n#endif\n    serverLogFromHandler(LL_WARNING,\"--------\\n\");\n}\n\n/* Schedule a SIGALRM delivery after the specified period in milliseconds.\n * If a timer is already scheduled, this function will re-schedule it to the\n * specified time. If period is 0 the current timer is disabled. */\nvoid watchdogScheduleSignal(int period) {\n    struct itimerval it;\n\n    /* Will stop the timer if period is 0. */\n    it.it_value.tv_sec = period/1000;\n    it.it_value.tv_usec = (period%1000)*1000;\n    /* Don't automatically restart. */\n    it.it_interval.tv_sec = 0;\n    it.it_interval.tv_usec = 0;\n    setitimer(ITIMER_REAL, &it, NULL);\n}\n\n/* Enable the software watchdog with the specified period in milliseconds. */\nvoid enableWatchdog(int period) {\n    int min_period;\n\n    if (g_pserver->watchdog_period == 0) {\n        struct sigaction act;\n\n        /* Watchdog was actually disabled, so we have to setup the signal\n         * handler. */\n        sigemptyset(&act.sa_mask);\n        act.sa_flags = SA_SIGINFO;\n        act.sa_sigaction = watchdogSignalHandler;\n        sigaction(SIGALRM, &act, NULL);\n    }\n    /* If the configured period is smaller than twice the timer period, it is\n     * too short for the software watchdog to work reliably. Fix it now\n     * if needed. */\n    min_period = (1000/g_pserver->hz)*2;\n    if (period < min_period) period = min_period;\n    watchdogScheduleSignal(period); /* Adjust the current timer. */\n    g_pserver->watchdog_period = period;\n}\n\n/* Disable the software watchdog. */\nvoid disableWatchdog(void) {\n    struct sigaction act;\n    if (g_pserver->watchdog_period == 0) return; /* Already disabled. */\n    watchdogScheduleSignal(0); /* Stop the current timer. */\n\n    /* Set the signal handler to SIG_IGN, this will also remove pending\n     * signals from the queue. */\n    sigemptyset(&act.sa_mask);\n    act.sa_flags = 0;\n    act.sa_handler = SIG_IGN;\n    sigaction(SIGALRM, &act, NULL);\n    g_pserver->watchdog_period = 0;\n}\n\n/* Positive input is sleep time in microseconds. Negative input is fractions\n * of microseconds, i.e. -10 means 100 nanoseconds. */\nvoid debugDelay(int usec) {\n    /* Since even the shortest sleep results in context switch and system call,\n     * the way we achive short sleeps is by statistically sleeping less often. */\n    if (usec < 0) usec = (rand() % -usec) == 0 ? 1: 0;\n    if (usec) usleep(usec);\n}\n"
  },
  {
    "path": "src/debugmacro.h",
    "content": "/* This file contains debugging macros to be used when investigating issues.\n *\n * -----------------------------------------------------------------------------\n *\n * Copyright (c) 2016, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <stdio.h>\n#define D(...)                                                               \\\n    do {                                                                     \\\n        FILE *fp = fopen(\"/tmp/log.txt\",\"a\");                                \\\n        fprintf(fp,\"%s:%s:%d:\\t\", __FILE__, __func__, __LINE__);             \\\n        fprintf(fp,__VA_ARGS__);                                             \\\n        fprintf(fp,\"\\n\");                                                    \\\n        fclose(fp);                                                          \\\n    } while (0)\n"
  },
  {
    "path": "src/defrag.cpp",
    "content": "/* \n * Active memory defragmentation\n * Try to find key / value allocations that need to be re-allocated in order \n * to reduce external fragmentation.\n * We do that by scanning the keyspace and for each pointer we have, we can try to\n * ask the allocator if moving it to a new address will help reduce fragmentation.\n *\n * Copyright (c) 2020, Redis Labs, Inc\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n#include <time.h>\n#include <assert.h>\n#include <stddef.h>\n\n#ifdef HAVE_DEFRAG\n\n/* this method was added to jemalloc in order to help us understand which\n * pointers are worthwhile moving and which aren't */\nextern \"C\" int je_get_defrag_hint(void* ptr);\n\n/* forward declarations*/\nvoid defragDictBucketCallback(void *privdata, dictEntry **bucketref);\ndictEntry* replaceSatelliteDictKeyPtrAndOrDefragDictEntry(dict *d, sds oldkey, sds newkey, uint64_t hash, long *defragged);\n\n/* Defrag helper for generic allocations.\n *\n * returns NULL in case the allocation wasn't moved.\n * when it returns a non-null value, the old pointer was already released\n * and should NOT be accessed. */\ntemplate<typename TPTR>\nTPTR* activeDefragAlloc(TPTR *ptr) {\n    size_t size;\n    void *newptr;\n    if(!je_get_defrag_hint(ptr)) {\n        g_pserver->stat_active_defrag_misses++;\n        return NULL;\n    }\n    /* move this allocation to a new allocation.\n     * make sure not to use the thread cache. so that we don't get back the same\n     * pointers we try to free */\n    size = zmalloc_size(ptr);\n    newptr = zmalloc_no_tcache(size);\n    memcpy(newptr, ptr, size);\n    zfree_no_tcache(ptr);\n    return (TPTR*)newptr;\n}\n\ntemplate<>\nrobj* activeDefragAlloc(robj *o) {\n    void *pvSrc = allocPtrFromObj(o);\n    void *pvDst = activeDefragAlloc(pvSrc);\n    return objFromAllocPtr(pvDst);\n}\n\nvoid* activeDefragAlloc(void *ptr) {\n    size_t size;\n    void *newptr;\n    if(!je_get_defrag_hint(ptr)) {\n        g_pserver->stat_active_defrag_misses++;\n        return NULL;\n    }\n    /* move this allocation to a new allocation.\n     * make sure not to use the thread cache. so that we don't get back the same\n     * pointers we try to free */\n    size = zmalloc_size(ptr);\n    newptr = zmalloc_no_tcache(size);\n    memcpy(newptr, ptr, size);\n    zfree_no_tcache(ptr);\n    return newptr;\n}\n\n/*Defrag helper for sds strings\n *\n * returns NULL in case the allocation wasn't moved.\n * when it returns a non-null value, the old pointer was already released\n * and should NOT be accessed. */\nsds activeDefragSds(sds sdsptr) {\n    void* ptr = sdsAllocPtr(sdsptr);\n    void* newptr = activeDefragAlloc(ptr);\n    if (newptr) {\n        size_t offset = sdsptr - (char*)ptr;\n        sdsptr = (char*)newptr + offset;\n        return sdsptr;\n    }\n    return NULL;\n}\n\n/* Defrag helper for robj and/or string objects\n *\n * returns NULL in case the allocation wasn't moved.\n * when it returns a non-null value, the old pointer was already released\n * and should NOT be accessed. */\nrobj *activeDefragStringOb(robj* ob, long *defragged) {\n    robj *ret = NULL;\n    if (ob->getrefcount(std::memory_order_relaxed)!=1)\n        return NULL;\n\n    /* try to defrag robj (only if not an EMBSTR type (handled below). */\n    if (ob->type!=OBJ_STRING || ob->encoding!=OBJ_ENCODING_EMBSTR) {\n        if ((ret = (robj*)activeDefragAlloc(ob))) {\n            ob = ret;\n            (*defragged)++;\n        }\n    }\n\n    /* try to defrag string object */\n    if (ob->type == OBJ_STRING) {\n        if(ob->encoding==OBJ_ENCODING_RAW) {\n            sds newsds = activeDefragSds((sds)ptrFromObj(ob));\n            if (newsds) {\n                ob->m_ptr = newsds;\n                (*defragged)++;\n            }\n        } else if (ob->encoding==OBJ_ENCODING_EMBSTR) {\n            /* The sds is embedded in the object allocation, calculate the\n             * offset and update the pointer in the new allocation. */\n            if ((ret = (robj*)activeDefragAlloc(ob))) {\n                (*defragged)++;\n            }\n        } else if (ob->encoding!=OBJ_ENCODING_INT) {\n            serverPanic(\"Unknown string encoding\");\n        }\n    }\n    return ret;\n}\n\n/* Defrag helper for dictEntries to be used during dict iteration (called on\n * each step). Returns a stat of how many pointers were moved. */\nlong dictIterDefragEntry(dictIterator *iter) {\n    /* This function is a little bit dirty since it messes with the internals\n     * of the dict and it's iterator, but the benefit is that it is very easy\n     * to use, and require no other changes in the dict. */\n    long defragged = 0;\n    dictht *ht;\n    /* Handle the next entry (if there is one), and update the pointer in the\n     * current entry. */\n    if (iter->nextEntry) {\n        dictEntry *newde = (dictEntry*)activeDefragAlloc(iter->nextEntry);\n        if (newde) {\n            defragged++;\n            iter->nextEntry = newde;\n            iter->entry->next = newde;\n        }\n    }\n    /* handle the case of the first entry in the hash bucket. */\n    ht = &iter->d->ht[iter->table];\n    if (ht->table[iter->index] == iter->entry) {\n        dictEntry *newde = (dictEntry*)activeDefragAlloc(iter->entry);\n        if (newde) {\n            iter->entry = newde;\n            ht->table[iter->index] = newde;\n            defragged++;\n        }\n    }\n    return defragged;\n}\n\n/* Defrag helper for dict main allocations (dict struct, and hash tables).\n * receives a pointer to the dict* and implicitly updates it when the dict\n * struct itself was moved. Returns a stat of how many pointers were moved. */\nlong dictDefragTables(dict* d) {\n    dictEntry **newtable;\n    long defragged = 0;\n    /* handle the first hash table */\n    newtable = (dictEntry**)activeDefragAlloc(d->ht[0].table);\n    if (newtable)\n        defragged++, d->ht[0].table = newtable;\n    /* handle the second hash table */\n    if (d->ht[1].table) {\n        newtable = (dictEntry**)activeDefragAlloc(d->ht[1].table);\n        if (newtable)\n            defragged++, d->ht[1].table = newtable;\n    }\n    return defragged;\n}\n\n/* Internal function used by zslDefrag */\nvoid zslUpdateNode(zskiplist *zsl, zskiplistNode *oldnode, zskiplistNode *newnode, zskiplistNode **update) {\n    int i;\n    for (i = 0; i < zsl->level; i++) {\n        if (update[i]->level(i)->forward == oldnode)\n            update[i]->level(i)->forward = newnode;\n    }\n    serverAssert(zsl->header!=oldnode);\n    if (newnode->level(0)->forward) {\n        serverAssert(newnode->level(0)->forward->backward==oldnode);\n        newnode->level(0)->forward->backward = newnode;\n    } else {\n        serverAssert(zsl->tail==oldnode);\n        zsl->tail = newnode;\n    }\n}\n\n/* Defrag helper for sorted set.\n * Update the robj pointer, defrag the skiplist struct and return the new score\n * reference. We may not access oldele pointer (not even the pointer stored in\n * the skiplist), as it was already freed. Newele may be null, in which case we\n * only need to defrag the skiplist, but not update the obj pointer.\n * When return value is non-NULL, it is the score reference that must be updated\n * in the dict record. */\ndouble *zslDefrag(zskiplist *zsl, double score, sds oldele, sds newele) {\n    zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x, *newx;\n    int i;\n    sds ele = newele? newele: oldele;\n\n    /* find the skiplist node referring to the object that was moved,\n     * and all pointers that need to be updated if we'll end up moving the skiplist node. */\n    x = zsl->header;\n    for (i = zsl->level-1; i >= 0; i--) {\n        while (x->level(i)->forward &&\n            x->level(i)->forward->ele != oldele && /* make sure not to access the\n                                                     ->obj pointer if it matches\n                                                     oldele */\n            (x->level(i)->forward->score < score ||\n                (x->level(i)->forward->score == score &&\n                sdscmp(x->level(i)->forward->ele,ele) < 0)))\n            x = x->level(i)->forward;\n        update[i] = x;\n    }\n\n    /* update the robj pointer inside the skip list record. */\n    x = x->level(0)->forward;\n    serverAssert(x && score == x->score && x->ele==oldele);\n    if (newele)\n        x->ele = newele;\n\n    /* try to defrag the skiplist record itself */\n    newx = (zskiplistNode*)activeDefragAlloc(x);\n    if (newx) {\n        zslUpdateNode(zsl, x, newx, update);\n        return &newx->score;\n    }\n    return NULL;\n}\n\n/* Defrag helper for sorted set.\n * Defrag a single dict entry key name, and corresponding skiplist struct */\nlong activeDefragZsetEntry(zset *zs, dictEntry *de) {\n    sds newsds;\n    double* newscore;\n    long defragged = 0;\n    sds sdsele = (sds)dictGetKey(de);\n    if ((newsds = activeDefragSds(sdsele)))\n        defragged++, de->key = newsds;\n    newscore = zslDefrag(zs->zsl, *(double*)dictGetVal(de), sdsele, newsds);\n    if (newscore) {\n        dictSetVal(zs->dict, de, newscore);\n        defragged++;\n    }\n    return defragged;\n}\n\n#define DEFRAG_SDS_DICT_NO_VAL 0\n#define DEFRAG_SDS_DICT_VAL_IS_SDS 1\n#define DEFRAG_SDS_DICT_VAL_IS_STROB 2\n#define DEFRAG_SDS_DICT_VAL_VOID_PTR 3\n\n/* Defrag a dict with sds key and optional value (either ptr, sds or robj string) */\nlong activeDefragSdsDict(dict* d, int val_type) {\n    dictIterator *di;\n    dictEntry *de;\n    long defragged = 0;\n    di = dictGetIterator(d);\n    while((de = dictNext(di)) != NULL) {\n        sds sdsele = (sds)dictGetKey(de), newsds;\n        if ((newsds = activeDefragSds(sdsele)))\n            de->key = newsds, defragged++;\n        /* defrag the value */\n        if (val_type == DEFRAG_SDS_DICT_VAL_IS_SDS) {\n            sdsele = (sds)dictGetVal(de);\n            if ((newsds = activeDefragSds(sdsele)))\n                de->v.val = newsds, defragged++;\n        } else if (val_type == DEFRAG_SDS_DICT_VAL_IS_STROB) {\n            robj *newele, *ele = (robj*)dictGetVal(de);\n            if ((newele = activeDefragStringOb(ele, &defragged)))\n                de->v.val = newele;\n        } else if (val_type == DEFRAG_SDS_DICT_VAL_VOID_PTR) {\n            void *newptr, *ptr = dictGetVal(de);\n            if ((newptr = activeDefragAlloc(ptr)))\n                de->v.val = newptr, defragged++;\n        }\n        defragged += dictIterDefragEntry(di);\n    }\n    dictReleaseIterator(di);\n    return defragged;\n}\n\n/* Defrag a list of ptr, sds or robj string values */\nlong activeDefragList(list *l, int val_type) {\n    long defragged = 0;\n    listNode *ln, *newln;\n    for (ln = l->head; ln; ln = ln->next) {\n        if ((newln = (listNode*)activeDefragAlloc(ln))) {\n            if (newln->prev)\n                newln->prev->next = newln;\n            else\n                l->head = newln;\n            if (newln->next)\n                newln->next->prev = newln;\n            else\n                l->tail = newln;\n            ln = newln;\n            defragged++;\n        }\n        if (val_type == DEFRAG_SDS_DICT_VAL_IS_SDS) {\n            sds newsds, sdsele = (sds)ln->value;\n            if ((newsds = activeDefragSds(sdsele)))\n                ln->value = newsds, defragged++;\n        } else if (val_type == DEFRAG_SDS_DICT_VAL_IS_STROB) {\n            robj *newele, *ele = (robj*)ln->value;\n            if ((newele = activeDefragStringOb(ele, &defragged)))\n                ln->value = newele;\n        } else if (val_type == DEFRAG_SDS_DICT_VAL_VOID_PTR) {\n            void *newptr, *ptr = ln->value;\n            if ((newptr = activeDefragAlloc(ptr)))\n                ln->value = newptr, defragged++;\n        }\n    }\n    return defragged;\n}\n\n/* Defrag a list of sds values and a dict with the same sds keys */\nlong activeDefragSdsListAndDict(list *l, dict *d, int dict_val_type) {\n    long defragged = 0;\n    sds newsds, sdsele;\n    listNode *ln, *newln;\n    dictIterator *di;\n    dictEntry *de;\n    /* Defrag the list and it's sds values */\n    for (ln = l->head; ln; ln = ln->next) {\n        if ((newln = (listNode*)activeDefragAlloc(ln))) {\n            if (newln->prev)\n                newln->prev->next = newln;\n            else\n                l->head = newln;\n            if (newln->next)\n                newln->next->prev = newln;\n            else\n                l->tail = newln;\n            ln = newln;\n            defragged++;\n        }\n        sdsele = (sds)ln->value;\n        if ((newsds = activeDefragSds(sdsele))) {\n            /* When defragging an sds value, we need to update the dict key */\n            uint64_t hash = dictGetHash(d, newsds);\n            dictEntry **deref = dictFindEntryRefByPtrAndHash(d, sdsele, hash);\n            if (deref)\n                (*deref)->key = newsds;\n            ln->value = newsds;\n            defragged++;\n        }\n    }\n\n    /* Defrag the dict values (keys were already handled) */\n    di = dictGetIterator(d);\n    while((de = dictNext(di)) != NULL) {\n        if (dict_val_type == DEFRAG_SDS_DICT_VAL_IS_SDS) {\n            sds newsds, sdsele = (sds)dictGetVal(de);\n            if ((newsds = activeDefragSds(sdsele)))\n                de->v.val = newsds, defragged++;\n        } else if (dict_val_type == DEFRAG_SDS_DICT_VAL_IS_STROB) {\n            robj *newele, *ele = (robj*)dictGetVal(de);\n            if ((newele = activeDefragStringOb(ele, &defragged)))\n                de->v.val = newele, defragged++;\n        } else if (dict_val_type == DEFRAG_SDS_DICT_VAL_VOID_PTR) {\n            void *newptr, *ptr = dictGetVal(de);\n            if ((newptr = activeDefragAlloc(ptr)))\n                de->v.val = newptr, defragged++;\n        }\n        defragged += dictIterDefragEntry(di);\n    }\n    dictReleaseIterator(di);\n\n    return defragged;\n}\n\n/* Utility function that replaces an old key pointer in the dictionary with a\n * new pointer. Additionally, we try to defrag the dictEntry in that dict.\n * Oldkey mey be a dead pointer and should not be accessed (we get a\n * pre-calculated hash value). Newkey may be null if the key pointer wasn't\n * moved. Return value is the the dictEntry if found, or NULL if not found.\n * NOTE: this is very ugly code, but it let's us avoid the complication of\n * doing a scan on another dict. */\ndictEntry* replaceSatelliteDictKeyPtrAndOrDefragDictEntry(dict *d, sds oldkey, sds newkey, uint64_t hash, long *defragged) {\n    dictEntry **deref = dictFindEntryRefByPtrAndHash(d, oldkey, hash);\n    if (deref) {\n        dictEntry *de = *deref;\n        dictEntry *newde = (dictEntry*)activeDefragAlloc(de);\n        if (newde) {\n            de = *deref = newde;\n            (*defragged)++;\n        }\n        if (newkey)\n            de->key = newkey;\n        return de;\n    }\n    return NULL;\n}\n\nlong activeDefragQuickListNode(quicklist *ql, quicklistNode **node_ref) {\n    quicklistNode *newnode, *node = *node_ref;\n    long defragged = 0;\n    unsigned char *newzl;\n    if ((newnode = (quicklistNode*)activeDefragAlloc(node))) {\n        if (newnode->prev)\n            newnode->prev->next = newnode;\n        else\n            ql->head = newnode;\n        if (newnode->next)\n            newnode->next->prev = newnode;\n        else\n            ql->tail = newnode;\n        *node_ref = node = newnode;\n        defragged++;\n    }\n    if ((newzl = (unsigned char*)activeDefragAlloc(node->zl)))\n        defragged++, node->zl = newzl;\n    return defragged;\n}\n\nlong activeDefragQuickListNodes(quicklist *ql) {\n    quicklistNode *node = ql->head;\n    long defragged = 0;\n    while (node) {\n        defragged += activeDefragQuickListNode(ql, &node);\n        node = node->next;\n    }\n    return defragged;\n}\n\n/* when the value has lots of elements, we want to handle it later and not as\n * part of the main dictionary scan. this is needed in order to prevent latency\n * spikes when handling large items */\nvoid defragLater(redisDb *db, dictEntry *kde) {\n    sds key = sdsdup((sds)dictGetKey(kde));\n    listAddNodeTail(db->defrag_later, key);\n}\n\n/* returns 0 if no more work needs to be been done, and 1 if time is up and more work is needed. */\nlong scanLaterList(robj *ob, unsigned long *cursor, long long endtime, long long *defragged) {\n    quicklist *ql = (quicklist*)ptrFromObj(ob);\n    quicklistNode *node;\n    long iterations = 0;\n    int bookmark_failed = 0;\n    if (ob->type != OBJ_LIST || ob->encoding != OBJ_ENCODING_QUICKLIST)\n        return 0;\n\n    if (*cursor == 0) {\n        /* if cursor is 0, we start new iteration */\n        node = ql->head;\n    } else {\n        node = quicklistBookmarkFind(ql, \"_AD\");\n        if (!node) {\n            /* if the bookmark was deleted, it means we reached the end. */\n            *cursor = 0;\n            return 0;\n        }\n        node = node->next;\n    }\n\n    (*cursor)++;\n    while (node) {\n        (*defragged) += activeDefragQuickListNode(ql, &node);\n        g_pserver->stat_active_defrag_scanned++;\n        if (++iterations > 128 && !bookmark_failed) {\n            if (ustime() > endtime) {\n                if (!quicklistBookmarkCreate(&ql, \"_AD\", node)) {\n                    bookmark_failed = 1;\n                } else {\n                    ob->m_ptr = ql; /* bookmark creation may have re-allocated the quicklist */\n                    return 1;\n                }\n            }\n            iterations = 0;\n        }\n        node = node->next;\n    }\n    quicklistBookmarkDelete(ql, \"_AD\");\n    *cursor = 0;\n    return bookmark_failed? 1: 0;\n}\n\ntypedef struct {\n    zset *zs;\n    long defragged;\n} scanLaterZsetData;\n\nvoid scanLaterZsetCallback(void *privdata, const dictEntry *_de) {\n    dictEntry *de = (dictEntry*)_de;\n    scanLaterZsetData *data = (scanLaterZsetData*)privdata;\n    data->defragged += activeDefragZsetEntry(data->zs, de);\n    g_pserver->stat_active_defrag_scanned++;\n}\n\nlong scanLaterZset(robj *ob, unsigned long *cursor) {\n    if (ob->type != OBJ_ZSET || ob->encoding != OBJ_ENCODING_SKIPLIST)\n        return 0;\n    zset *zs = (zset*)ptrFromObj(ob);\n    dict *d = zs->dict;\n    scanLaterZsetData data = {zs, 0};\n    *cursor = dictScan(d, *cursor, scanLaterZsetCallback, defragDictBucketCallback, &data);\n    return data.defragged;\n}\n\nvoid scanLaterSetCallback(void *privdata, const dictEntry *_de) {\n    dictEntry *de = (dictEntry*)_de;\n    long *defragged = (long*)privdata;\n    sds sdsele = (sds)dictGetKey(de), newsds;\n    if ((newsds = activeDefragSds(sdsele)))\n        (*defragged)++, de->key = newsds;\n    g_pserver->stat_active_defrag_scanned++;\n}\n\nlong scanLaterSet(robj *ob, unsigned long *cursor) {\n    long defragged = 0;\n    if (ob->type != OBJ_SET || ob->encoding != OBJ_ENCODING_HT)\n        return 0;\n    dict *d = (dict*)ptrFromObj(ob);\n    *cursor = dictScan(d, *cursor, scanLaterSetCallback, defragDictBucketCallback, &defragged);\n    return defragged;\n}\n\nvoid scanLaterHashCallback(void *privdata, const dictEntry *_de) {\n    dictEntry *de = (dictEntry*)_de;\n    long *defragged = (long*)privdata;\n    sds sdsele = (sds)dictGetKey(de), newsds;\n    if ((newsds = activeDefragSds(sdsele)))\n        (*defragged)++, de->key = newsds;\n    sdsele = (sds)dictGetVal(de);\n    if ((newsds = activeDefragSds(sdsele)))\n        (*defragged)++, de->v.val = newsds;\n    g_pserver->stat_active_defrag_scanned++;\n}\n\nlong scanLaterHash(robj *ob, unsigned long *cursor) {\n    long defragged = 0;\n    if (ob->type != OBJ_HASH || ob->encoding != OBJ_ENCODING_HT)\n        return 0;\n    dict *d = (dict*)ptrFromObj(ob);\n    *cursor = dictScan(d, *cursor, scanLaterHashCallback, defragDictBucketCallback, &defragged);\n    return defragged;\n}\n\nlong defragQuicklist(redisDb *db, dictEntry *kde) {\n    robj *ob = (robj*)dictGetVal(kde);\n    long defragged = 0;\n    quicklist *ql = (quicklist*)ptrFromObj(ob), *newql;\n    serverAssert(ob->type == OBJ_LIST && ob->encoding == OBJ_ENCODING_QUICKLIST);\n    if ((newql = (quicklist*)activeDefragAlloc(ql)))\n        defragged++, ob->m_ptr = ql = newql;\n    if (ql->len > cserver.active_defrag_max_scan_fields)\n        defragLater(db, kde);\n    else\n        defragged += activeDefragQuickListNodes(ql);\n    return defragged;\n}\n\nlong defragZsetSkiplist(redisDb *db, dictEntry *kde) {\n    robj *ob = (robj*)dictGetVal(kde);\n    long defragged = 0;\n    zset *zs = (zset*)ptrFromObj(ob);\n    zset *newzs;\n    zskiplist *newzsl;\n    dict *newdict;\n    dictEntry *de;\n    struct zskiplistNode *newheader;\n    serverAssert(ob->type == OBJ_ZSET && ob->encoding == OBJ_ENCODING_SKIPLIST);\n    if ((newzs = (zset*)activeDefragAlloc(zs)))\n        defragged++, ob->m_ptr = zs = newzs;\n    if ((newzsl = (zskiplist*)activeDefragAlloc(zs->zsl)))\n        defragged++, zs->zsl = newzsl;\n    if ((newheader = (zskiplistNode*)activeDefragAlloc(zs->zsl->header)))\n        defragged++, zs->zsl->header = newheader;\n    if (dictSize(zs->dict) > cserver.active_defrag_max_scan_fields)\n        defragLater(db, kde);\n    else {\n        dictIterator *di = dictGetIterator(zs->dict);\n        while((de = dictNext(di)) != NULL) {\n            defragged += activeDefragZsetEntry(zs, de);\n        }\n        dictReleaseIterator(di);\n    }\n    /* handle the dict struct */\n    if ((newdict = (dict*)activeDefragAlloc(zs->dict)))\n        defragged++, zs->dict = newdict;\n    /* defrag the dict tables */\n    defragged += dictDefragTables(zs->dict);\n    return defragged;\n}\n\nlong defragHash(redisDb *db, dictEntry *kde) {\n    long defragged = 0;\n    robj *ob = (robj*)dictGetVal(kde);\n    dict *d, *newd;\n    serverAssert(ob->type == OBJ_HASH && ob->encoding == OBJ_ENCODING_HT);\n    d = (dict*)ptrFromObj(ob);\n    if (dictSize(d) > cserver.active_defrag_max_scan_fields)\n        defragLater(db, kde);\n    else\n        defragged += activeDefragSdsDict(d, DEFRAG_SDS_DICT_VAL_IS_SDS);\n    /* handle the dict struct */\n    if ((newd = (dict*)activeDefragAlloc(ptrFromObj(ob))))\n        defragged++, ob->m_ptr = newd;\n    /* defrag the dict tables */\n    defragged += dictDefragTables((dict*)ptrFromObj(ob));\n    return defragged;\n}\n\nlong defragSet(redisDb *db, dictEntry *kde) {\n    long defragged = 0;\n    robj *ob = (robj*)dictGetVal(kde);\n    dict *d, *newd;\n    serverAssert(ob->type == OBJ_SET && ob->encoding == OBJ_ENCODING_HT);\n    d = (dict*)ptrFromObj(ob);\n    if (dictSize(d) > cserver.active_defrag_max_scan_fields)\n        defragLater(db, kde);\n    else\n        defragged += activeDefragSdsDict(d, DEFRAG_SDS_DICT_NO_VAL);\n    /* handle the dict struct */\n    if ((newd = (dict*)activeDefragAlloc(ptrFromObj(ob))))\n        defragged++, ob->m_ptr = newd;\n    /* defrag the dict tables */\n    defragged += dictDefragTables((dict*)ptrFromObj(ob));\n    return defragged;\n}\n\n/* Defrag callback for radix tree iterator, called for each node,\n * used in order to defrag the nodes allocations. */\nint defragRaxNode(raxNode **noderef) {\n    raxNode *newnode = (raxNode*)activeDefragAlloc(*noderef);\n    if (newnode) {\n        *noderef = newnode;\n        return 1;\n    }\n    return 0;\n}\n\n/* returns 0 if no more work needs to be been done, and 1 if time is up and more work is needed. */\nint scanLaterStreamListpacks(robj *ob, unsigned long *cursor, long long endtime, long long *defragged) {\n    static unsigned char last[sizeof(streamID)];\n    raxIterator ri;\n    long iterations = 0;\n    if (ob->type != OBJ_STREAM || ob->encoding != OBJ_ENCODING_STREAM) {\n        *cursor = 0;\n        return 0;\n    }\n\n    stream *s = (stream*)ptrFromObj(ob);\n    raxStart(&ri,s->rax);\n    if (*cursor == 0) {\n        /* if cursor is 0, we start new iteration */\n        defragRaxNode(&s->rax->head);\n        /* assign the iterator node callback before the seek, so that the\n         * initial nodes that are processed till the first item are covered */\n        ri.node_cb = defragRaxNode;\n        raxSeek(&ri,\"^\",NULL,0);\n    } else {\n        /* if cursor is non-zero, we seek to the static 'last' */\n        if (!raxSeek(&ri,\">\", last, sizeof(last))) {\n            *cursor = 0;\n            raxStop(&ri);\n            return 0;\n        }\n        /* assign the iterator node callback after the seek, so that the\n         * initial nodes that are processed till now aren't covered */\n        ri.node_cb = defragRaxNode;\n    }\n\n    (*cursor)++;\n    while (raxNext(&ri)) {\n        void *newdata = activeDefragAlloc(ri.data);\n        if (newdata)\n            raxSetData(ri.node, ri.data=newdata), (*defragged)++;\n        g_pserver->stat_active_defrag_scanned++;\n        if (++iterations > 128) {\n            if (ustime() > endtime) {\n                serverAssert(ri.key_len==sizeof(last));\n                memcpy(last,ri.key,ri.key_len);\n                raxStop(&ri);\n                return 1;\n            }\n            iterations = 0;\n        }\n    }\n    raxStop(&ri);\n    *cursor = 0;\n    return 0;\n}\n\n/* optional callback used defrag each rax element (not including the element pointer itself) */\ntypedef void *(raxDefragFunction)(raxIterator *ri, void *privdata, long *defragged);\n\n/* defrag radix tree including:\n * 1) rax struct\n * 2) rax nodes\n * 3) rax entry data (only if defrag_data is specified)\n * 4) call a callback per element, and allow the callback to return a new pointer for the element */\nlong defragRadixTree(rax **raxref, int defrag_data, raxDefragFunction *element_cb, void *element_cb_data) {\n    long defragged = 0;\n    raxIterator ri;\n    ::rax* rax;\n    if ((rax = (::rax*)activeDefragAlloc(*raxref)))\n        defragged++, *raxref = rax;\n    rax = *raxref;\n    raxStart(&ri,rax);\n    ri.node_cb = defragRaxNode;\n    defragRaxNode(&rax->head);\n    raxSeek(&ri,\"^\",NULL,0);\n    while (raxNext(&ri)) {\n        void *newdata = NULL;\n        if (element_cb)\n            newdata = element_cb(&ri, element_cb_data, &defragged);\n        if (defrag_data && !newdata)\n            newdata = activeDefragAlloc(ri.data);\n        if (newdata)\n            raxSetData(ri.node, ri.data=newdata), defragged++;\n    }\n    raxStop(&ri);\n    return defragged;\n}\n\ntypedef struct {\n    streamCG *cg;\n    streamConsumer *c;\n} PendingEntryContext;\n\nvoid* defragStreamConsumerPendingEntry(raxIterator *ri, void *privdata, long *defragged) {\n    UNUSED(defragged);\n    PendingEntryContext *ctx = (PendingEntryContext*)privdata;\n    streamNACK *nack = (streamNACK*)ri->data, *newnack;\n    nack->consumer = ctx->c; /* update nack pointer to consumer */\n    newnack = (streamNACK*)activeDefragAlloc(nack);\n    if (newnack) {\n        /* update consumer group pointer to the nack */\n        void *prev;\n        raxInsert(ctx->cg->pel, ri->key, ri->key_len, newnack, &prev);\n        serverAssert(prev==nack);\n        /* note: we don't increment 'defragged' that's done by the caller */\n    }\n    return newnack;\n}\n\nvoid* defragStreamConsumer(raxIterator *ri, void *privdata, long *defragged) {\n    streamConsumer *c = (streamConsumer*)ri->data;\n    streamCG *cg = (streamCG*)privdata;\n    void *newc = activeDefragAlloc(c);\n    if (newc) {\n        /* note: we don't increment 'defragged' that's done by the caller */\n        c = (streamConsumer*)newc;\n    }\n    sds newsds = activeDefragSds(c->name);\n    if (newsds)\n        (*defragged)++, c->name = newsds;\n    if (c->pel) {\n        PendingEntryContext pel_ctx = {cg, c};\n        *defragged += defragRadixTree(&c->pel, 0, defragStreamConsumerPendingEntry, &pel_ctx);\n    }\n    return newc; /* returns NULL if c was not defragged */\n}\n\nvoid* defragStreamConsumerGroup(raxIterator *ri, void *privdata, long *defragged) {\n    streamCG *cg = (streamCG*)ri->data;\n    UNUSED(privdata);\n    if (cg->consumers)\n        *defragged += defragRadixTree(&cg->consumers, 0, defragStreamConsumer, cg);\n    if (cg->pel)\n        *defragged += defragRadixTree(&cg->pel, 0, NULL, NULL);\n    return NULL;\n}\n\nlong defragStream(redisDb *db, dictEntry *kde) {\n    long defragged = 0;\n    robj *ob = (robj*)dictGetVal(kde);\n    serverAssert(ob->type == OBJ_STREAM && ob->encoding == OBJ_ENCODING_STREAM);\n    stream *s = (stream*)ptrFromObj(ob), *news;\n\n    /* handle the main struct */\n    if ((news = (stream*)activeDefragAlloc(s)))\n        defragged++, ob->m_ptr = s = news;\n\n    if (raxSize(s->rax) > cserver.active_defrag_max_scan_fields) {\n        rax *newrax = (rax*)activeDefragAlloc(s->rax);\n        if (newrax)\n            defragged++, s->rax = newrax;\n        defragLater(db, kde);\n    } else\n        defragged += defragRadixTree(&s->rax, 1, NULL, NULL);\n\n    if (s->cgroups)\n        defragged += defragRadixTree(&s->cgroups, 1, defragStreamConsumerGroup, NULL);\n    return defragged;\n}\n\n/* Defrag a module key. This is either done immediately or scheduled\n * for later. Returns then number of pointers defragged.\n */\nlong defragModule(redisDb *db, dictEntry *kde) {\n    robj *obj = (robj*)dictGetVal(kde);\n    serverAssert(obj->type == OBJ_MODULE);\n    long defragged = 0;\n\n    if (!moduleDefragValue((robj*)dictGetKey(kde), obj, &defragged))\n        defragLater(db, kde);\n\n    return defragged;\n}\n\n/* for each key we scan in the main dict, this function will attempt to defrag\n * all the various pointers it has. Returns a stat of how many pointers were\n * moved. */\nlong defragKey(redisDb *db, dictEntry *de) {\n    sds keysds = (sds)dictGetKey(de);\n    robj *newob, *ob;\n    unsigned char *newzl;\n    long defragged = 0;\n    sds newsds;\n\n    ob = (robj*)dictGetVal(de);\n\n    /* Try to defrag the key name. */\n    newsds = activeDefragSds(keysds);\n    if (newsds) {\n        defragged++, de->key = newsds;\n    }\n\n    if ((newob = activeDefragStringOb(ob, &defragged))) {\n        de->v.val = newob;\n        ob = newob;\n    }\n\n    if (ob->type == OBJ_STRING) {\n        /* Already handled in activeDefragStringOb. */\n    } else if (ob->type == OBJ_LIST) {\n        if (ob->encoding == OBJ_ENCODING_QUICKLIST) {\n            defragged += defragQuicklist(db, de);\n        } else if (ob->encoding == OBJ_ENCODING_ZIPLIST) {\n            if ((newzl = (unsigned char*)activeDefragAlloc(ptrFromObj(ob))))\n                defragged++, ob->m_ptr = newzl;\n        } else {\n            serverPanic(\"Unknown list encoding\");\n        }\n    } else if (ob->type == OBJ_SET) {\n        if (ob->encoding == OBJ_ENCODING_HT) {\n            defragged += defragSet(db, de);\n        } else if (ob->encoding == OBJ_ENCODING_INTSET) {\n            intset *newis, *is = (intset*)ptrFromObj(ob);\n            if ((newis = (intset*)activeDefragAlloc(is)))\n                defragged++, ob->m_ptr = newis;\n        } else {\n            serverPanic(\"Unknown set encoding\");\n        }\n    } else if (ob->type == OBJ_ZSET) {\n        if (ob->encoding == OBJ_ENCODING_ZIPLIST) {\n            if ((newzl = (unsigned char*)activeDefragAlloc(ptrFromObj(ob))))\n                defragged++, ob->m_ptr = newzl;\n        } else if (ob->encoding == OBJ_ENCODING_SKIPLIST) {\n            defragged += defragZsetSkiplist(db, de);\n        } else {\n            serverPanic(\"Unknown sorted set encoding\");\n        }\n    } else if (ob->type == OBJ_HASH) {\n        if (ob->encoding == OBJ_ENCODING_ZIPLIST) {\n            if ((newzl = (unsigned char*)activeDefragAlloc(ptrFromObj(ob))))\n                defragged++, ob->m_ptr = newzl;\n        } else if (ob->encoding == OBJ_ENCODING_HT) {\n            defragged += defragHash(db, de);\n        } else {\n            serverPanic(\"Unknown hash encoding\");\n        }\n    } else if (ob->type == OBJ_STREAM) {\n        defragged += defragStream(db, de);\n    } else if (ob->type == OBJ_MODULE) {\n        defragged += defragModule(db, de);\n    } else {\n        serverPanic(\"Unknown object type\");\n    }\n\n    return defragged;\n}\n\n/* Defrag scan callback for the main db dictionary. */\nvoid defragScanCallback(void *privdata, const dictEntry *de) {\n    long defragged = defragKey((redisDb*)privdata, (dictEntry*)de);\n    g_pserver->stat_active_defrag_hits += defragged;\n    if(defragged)\n        g_pserver->stat_active_defrag_key_hits++;\n    else\n        g_pserver->stat_active_defrag_key_misses++;\n    g_pserver->stat_active_defrag_scanned++;\n}\n\n/* Defrag scan callback for each hash table bucket,\n * used in order to defrag the dictEntry allocations. */\nvoid defragDictBucketCallback(void *privdata, dictEntry **bucketref) {\n    UNUSED(privdata); /* NOTE: this function is also used by both activeDefragCycle and scanLaterHash, etc. don't use privdata */\n    while(*bucketref) {\n        dictEntry *de = *bucketref, *newde;\n        if ((newde = (dictEntry*)activeDefragAlloc(de))) {\n            *bucketref = newde;\n        }\n        bucketref = &(*bucketref)->next;\n    }\n}\n\n/* Utility function to get the fragmentation ratio from jemalloc.\n * It is critical to do that by comparing only heap maps that belong to\n * jemalloc, and skip ones the jemalloc keeps as spare. Since we use this\n * fragmentation ratio in order to decide if a defrag action should be taken\n * or not, a false detection can cause the defragmenter to waste a lot of CPU\n * without the possibility of getting any results. */\nfloat getAllocatorFragmentation(size_t *out_frag_bytes) {\n    size_t resident, active, allocated;\n    zmalloc_get_allocator_info(&allocated, &active, &resident);\n    float frag_pct = ((float)active / allocated)*100 - 100;\n    size_t frag_bytes = active - allocated;\n    float rss_pct = ((float)resident / allocated)*100 - 100;\n    size_t rss_bytes = resident - allocated;\n    if(out_frag_bytes)\n        *out_frag_bytes = frag_bytes;\n    serverLog(LL_DEBUG,\n        \"allocated=%zu, active=%zu, resident=%zu, frag=%.0f%% (%.0f%% rss), frag_bytes=%zu (%zu rss)\",\n        allocated, active, resident, frag_pct, rss_pct, frag_bytes, rss_bytes);\n    return frag_pct;\n}\n\n/* We may need to defrag other globals, one small allocation can hold a full allocator run.\n * so although small, it is still important to defrag these */\nlong defragOtherGlobals() {\n    long defragged = 0;\n\n    /* there are many more pointers to defrag (e.g. client argv, output / aof buffers, etc.\n     * but we assume most of these are short lived, we only need to defrag allocations\n     * that remain static for a long time */\n    defragged += activeDefragSdsDict(g_pserver->lua_scripts, DEFRAG_SDS_DICT_VAL_IS_STROB);\n    defragged += activeDefragSdsListAndDict(g_pserver->repl_scriptcache_fifo, g_pserver->repl_scriptcache_dict, DEFRAG_SDS_DICT_NO_VAL);\n    defragged += moduleDefragGlobals();\n    return defragged;\n}\n\n/* returns 0 more work may or may not be needed (see non-zero cursor),\n * and 1 if time is up and more work is needed. */\nint defragLaterItem(sds key, robj *ob, unsigned long *cursor, long long endtime) {\n    if (ob) {\n        if (ob->type == OBJ_LIST) {\n            return scanLaterList(ob, cursor, endtime, &g_pserver->stat_active_defrag_hits);\n        } else if (ob->type == OBJ_SET) {\n            g_pserver->stat_active_defrag_hits += scanLaterSet(ob, cursor);\n        } else if (ob->type == OBJ_ZSET) {\n            g_pserver->stat_active_defrag_hits += scanLaterZset(ob, cursor);\n        } else if (ob->type == OBJ_HASH) {\n            g_pserver->stat_active_defrag_hits += scanLaterHash(ob, cursor);\n        } else if (ob->type == OBJ_STREAM) {\n            return scanLaterStreamListpacks(ob, cursor, endtime, &g_pserver->stat_active_defrag_hits);\n        } else if (ob->type == OBJ_MODULE) {\n            redisObjectStack oT;\n            initStaticStringObject(oT, key);\n            return moduleLateDefrag(&oT, ob, cursor, endtime, &g_pserver->stat_active_defrag_hits);\n        } else {\n            *cursor = 0; /* object type may have changed since we schedule it for later */\n        }\n    } else {\n        *cursor = 0; /* object may have been deleted already */\n    }\n    return 0;\n}\n\n/* static variables serving defragLaterStep to continue scanning a key from were we stopped last time. */\nstatic sds defrag_later_current_key = NULL;\nstatic unsigned long defrag_later_cursor = 0;\n\n/* returns 0 if no more work needs to be been done, and 1 if time is up and more work is needed. */\nint defragLaterStep(redisDb *db, long long endtime) {\n    unsigned int iterations = 0;\n    unsigned long long prev_defragged = g_pserver->stat_active_defrag_hits;\n    unsigned long long prev_scanned = g_pserver->stat_active_defrag_scanned;\n    long long key_defragged;\n\n    do {\n        /* if we're not continuing a scan from the last call or loop, start a new one */\n        if (!defrag_later_cursor) {\n            listNode *head = listFirst(db->defrag_later);\n\n            /* Move on to next key */\n            if (defrag_later_current_key) {\n                serverAssert(defrag_later_current_key == head->value);\n                listDelNode(db->defrag_later, head);\n                defrag_later_cursor = 0;\n                defrag_later_current_key = NULL;\n            }\n\n            /* stop if we reached the last one. */\n            head = listFirst(db->defrag_later);\n            if (!head)\n                return 0;\n\n            /* start a new key */\n            defrag_later_current_key = (sds)head->value;\n            defrag_later_cursor = 0;\n        }\n\n        /* each time we enter this function we need to fetch the key from the dict again (if it still exists) */\n        robj *o = db->find(defrag_later_current_key);\n        key_defragged = g_pserver->stat_active_defrag_hits;\n        do {\n            int quit = 0;\n            if (defragLaterItem(defrag_later_current_key, o, &defrag_later_cursor, endtime))\n                quit = 1; /* time is up, we didn't finish all the work */\n\n            /* Once in 16 scan iterations, 512 pointer reallocations, or 64 fields\n             * (if we have a lot of pointers in one hash bucket, or rehashing),\n             * check if we reached the time limit. */\n            if (quit || (++iterations > 16 ||\n                            g_pserver->stat_active_defrag_hits - prev_defragged > 512 ||\n                            g_pserver->stat_active_defrag_scanned - prev_scanned > 64)) {\n                if (quit || ustime() > endtime) {\n                    if(key_defragged != g_pserver->stat_active_defrag_hits)\n                        g_pserver->stat_active_defrag_key_hits++;\n                    else\n                        g_pserver->stat_active_defrag_key_misses++;\n                    return 1;\n                }\n                iterations = 0;\n                prev_defragged = g_pserver->stat_active_defrag_hits;\n                prev_scanned = g_pserver->stat_active_defrag_scanned;\n            }\n        } while(defrag_later_cursor);\n        if(key_defragged != g_pserver->stat_active_defrag_hits)\n            g_pserver->stat_active_defrag_key_hits++;\n        else\n            g_pserver->stat_active_defrag_key_misses++;\n    } while(1);\n}\n\n#define INTERPOLATE(x, x1, x2, y1, y2) ( (y1) + ((x)-(x1)) * ((y2)-(y1)) / ((x2)-(x1)) )\n#define LIMIT(y, min, max) ((y)<(min)? min: ((y)>(max)? max: (y)))\n\n/* decide if defrag is needed, and at what CPU effort to invest in it */\nvoid computeDefragCycles() {\n    size_t frag_bytes;\n    float frag_pct = getAllocatorFragmentation(&frag_bytes);\n    /* If we're not already running, and below the threshold, exit. */\n    if (!g_pserver->active_defrag_running) {\n        if(frag_pct < cserver.active_defrag_threshold_lower || frag_bytes < cserver.active_defrag_ignore_bytes)\n            return;\n    }\n\n    /* Calculate the adaptive aggressiveness of the defrag */\n    int cpu_pct = INTERPOLATE(frag_pct,\n            cserver.active_defrag_threshold_lower,\n            cserver.active_defrag_threshold_upper,\n            cserver.active_defrag_cycle_min,\n            cserver.active_defrag_cycle_max);\n    cpu_pct = LIMIT(cpu_pct,\n            cserver.active_defrag_cycle_min,\n            cserver.active_defrag_cycle_max);\n     /* We allow increasing the aggressiveness during a scan, but don't\n      * reduce it. */\n    if (!g_pserver->active_defrag_running ||\n        cpu_pct > g_pserver->active_defrag_running)\n    {\n        g_pserver->active_defrag_running = cpu_pct;\n        serverLog(LL_VERBOSE,\n            \"Starting active defrag, frag=%.0f%%, frag_bytes=%zu, cpu=%d%%\",\n            frag_pct, frag_bytes, cpu_pct);\n    }\n}\n\n/* Perform incremental defragmentation work from the serverCron.\n * This works in a similar way to activeExpireCycle, in the sense that\n * we do incremental work across calls. */\nvoid activeDefragCycle(void) {\n    static int current_db = -1;\n    static unsigned long cursor = 0;\n    static redisDb *db = NULL;\n    static long long start_scan, start_stat;\n    unsigned int iterations = 0;\n    unsigned long long prev_defragged = g_pserver->stat_active_defrag_hits;\n    unsigned long long prev_scanned = g_pserver->stat_active_defrag_scanned;\n    long long start, timelimit, endtime;\n    mstime_t latency;\n    int quit = 0;\n\n    if (!cserver.active_defrag_enabled) {\n        if (g_pserver->active_defrag_running) {\n            /* if active defrag was disabled mid-run, start from fresh next time. */\n            g_pserver->active_defrag_running = 0;\n            if (db)\n                listEmpty(db->defrag_later);\n            defrag_later_current_key = NULL;\n            defrag_later_cursor = 0;\n            current_db = -1;\n            cursor = 0;\n            db = NULL;\n        }\n        return;\n    }\n\n    if (hasActiveChildProcess())\n        return; /* Defragging memory while there's a fork will just do damage. */\n\n    /* Once a second, check if the fragmentation justfies starting a scan\n     * or making it more aggressive. */\n    run_with_period(1000) {\n        computeDefragCycles();\n    }\n    if (!g_pserver->active_defrag_running)\n        return;\n\n    /* See activeExpireCycle for how timelimit is handled. */\n    start = ustime();\n    timelimit = 1000000*g_pserver->active_defrag_running/g_pserver->hz/100;\n    if (timelimit <= 0) timelimit = 1;\n    endtime = start + timelimit;\n    latencyStartMonitor(latency);\n\n    do {\n        /* if we're not continuing a scan from the last call or loop, start a new one */\n        if (!cursor) {\n            /* finish any leftovers from previous db before moving to the next one */\n            if (db && defragLaterStep(db, endtime)) {\n                quit = 1; /* time is up, we didn't finish all the work */\n                break; /* this will exit the function and we'll continue on the next cycle */\n            }\n\n            /* Move on to next database, and stop if we reached the last one. */\n            if (++current_db >= cserver.dbnum) {\n                /* defrag other items not part of the db / keys */\n                defragOtherGlobals();\n\n                long long now = ustime();\n                size_t frag_bytes;\n                float frag_pct = getAllocatorFragmentation(&frag_bytes);\n                serverLog(LL_VERBOSE,\n                    \"Active defrag done in %dms, reallocated=%d, frag=%.0f%%, frag_bytes=%zu\",\n                    (int)((now - start_scan)/1000), (int)(g_pserver->stat_active_defrag_hits - start_stat), frag_pct, frag_bytes);\n\n                start_scan = now;\n                current_db = -1;\n                cursor = 0;\n                db = NULL;\n                g_pserver->active_defrag_running = 0;\n\n                computeDefragCycles(); /* if another scan is needed, start it right away */\n                if (g_pserver->active_defrag_running != 0 && ustime() < endtime)\n                    continue;\n                break;\n            }\n            else if (current_db==0) {\n                /* Start a scan from the first database. */\n                start_scan = ustime();\n                start_stat = g_pserver->stat_active_defrag_hits;\n            }\n\n            db = g_pserver->db[current_db];\n            cursor = 0;\n        }\n\n        do {\n            /* before scanning the next bucket, see if we have big keys left from the previous bucket to scan */\n            if (defragLaterStep(db, endtime)) {\n                quit = 1; /* time is up, we didn't finish all the work */\n                break; /* this will exit the function and we'll continue on the next cycle */\n            }\n\n            // we actually look at the objects too but defragScanCallback can handle missing values\n            cursor = dictScan(db->dictUnsafeKeyOnly(), cursor, defragScanCallback, defragDictBucketCallback, db);\n\n            /* Once in 16 scan iterations, 512 pointer reallocations. or 64 keys\n             * (if we have a lot of pointers in one hash bucket or rehasing),\n             * check if we reached the time limit.\n             * But regardless, don't start a new db in this loop, this is because after\n             * the last db we call defragOtherGlobals, which must be done in one cycle */\n            if (!cursor || (++iterations > 16 ||\n                            g_pserver->stat_active_defrag_hits - prev_defragged > 512 ||\n                            g_pserver->stat_active_defrag_scanned - prev_scanned > 64)) {\n                if (!cursor || ustime() > endtime) {\n                    quit = 1;\n                    break;\n                }\n                iterations = 0;\n                prev_defragged = g_pserver->stat_active_defrag_hits;\n                prev_scanned = g_pserver->stat_active_defrag_scanned;\n            }\n        } while(cursor && !quit);\n    } while(!quit);\n\n    latencyEndMonitor(latency);\n    latencyAddSampleIfNeeded(\"active-defrag-cycle\",latency);\n}\n\n#else /* HAVE_DEFRAG */\n\nvoid activeDefragCycle(void) {\n    /* Not implemented yet. */\n}\n\nvoid *activeDefragAlloc(void *ptr) {\n    UNUSED(ptr);\n    return NULL;\n}\n\nrobj *activeDefragStringOb(robj *ob, long *defragged) {\n    UNUSED(ob);\n    UNUSED(defragged);\n    return NULL;\n}\n\n#endif\n"
  },
  {
    "path": "src/dict.cpp",
    "content": "/* Hash Tables Implementation.\n *\n * This file implements in memory hash tables with insert/del/replace/find/\n * get-random-element operations. Hash tables will auto resize if needed\n * tables of power of two in size are used, collisions are handled by\n * chaining. See the source code for more information... :)\n *\n * Copyright (c) 2006-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"fmacros.h\"\n\n#include <stdio.h>\n#include <stdlib.h>\n#include <stdint.h>\n#include <string.h>\n#include <stdarg.h>\n#include <limits.h>\n#include <sys/time.h>\n#include <algorithm>\n\n#include \"dict.h\"\n#include \"zmalloc.h\"\n#include \"redisassert.h\"\n\n/* Using dictEnableResize() / dictDisableResize() we make possible to\n * enable/disable resizing of the hash table as needed. This is very important\n * for Redis, as we use copy-on-write and don't want to move too much memory\n * around when there is a child performing saving operations.\n *\n * Note that even when dict_can_resize is set to 0, not all resizes are\n * prevented: a hash table is still allowed to grow if the ratio between\n * the number of elements and the buckets > dict_force_resize_ratio. */\nstatic int dict_can_resize = 1;\nstatic unsigned int dict_force_resize_ratio = 5;\n\n/* -------------------------- private prototypes ---------------------------- */\n\nstatic int _dictExpandIfNeeded(dict *ht);\nstatic unsigned long _dictNextPower(unsigned long size);\nlong _dictKeyIndex(dict *ht, const void *key, uint64_t hash, dictEntry **existing);\nstatic int _dictInit(dict *ht, dictType *type, void *privDataPtr);\n\n/* -------------------------- hash functions -------------------------------- */\n\nstatic uint8_t dict_hash_function_seed[16];\n\nextern \"C\" void asyncFreeDictTable(dictEntry **de);\n\nvoid dictSetHashFunctionSeed(uint8_t *seed) {\n    memcpy(dict_hash_function_seed,seed,sizeof(dict_hash_function_seed));\n}\n\nuint8_t *dictGetHashFunctionSeed(void) {\n    return dict_hash_function_seed;\n}\n\n/* The default hashing function uses SipHash implementation\n * in siphash.c. */\n\nextern \"C\" uint64_t siphash(const uint8_t *in, const size_t inlen, const uint8_t *k);\nextern \"C\" uint64_t siphash_nocase(const uint8_t *in, const size_t inlen, const uint8_t *k);\n\nuint64_t dictGenHashFunction(const void *key, int len) {\n    return siphash((const uint8_t*)key,len,dict_hash_function_seed);\n}\n\nuint64_t dictGenCaseHashFunction(const unsigned char *buf, int len) {\n    return siphash_nocase(buf,len,dict_hash_function_seed);\n}\n\n/* ----------------------------- API implementation ------------------------- */\n\n/* Reset a hash table already initialized with ht_init().\n * NOTE: This function should only be called by ht_destroy(). */\nstatic void _dictReset(dictht *ht)\n{\n    ht->table = NULL;\n    ht->size = 0;\n    ht->sizemask = 0;\n    ht->used = 0;\n}\n\n/* Create a new hash table */\ndict *dictCreate(dictType *type,\n        void *privDataPtr)\n{\n    dict *d = (dict*)zmalloc(sizeof(*d), MALLOC_SHARED);\n\n    _dictInit(d,type,privDataPtr);\n    return d;\n}\n\n/* Initialize the hash table */\nint _dictInit(dict *d, dictType *type,\n        void *privDataPtr)\n{\n    _dictReset(&d->ht[0]);\n    _dictReset(&d->ht[1]);\n    d->type = type;\n    d->privdata = privDataPtr;\n    d->rehashidx = -1;\n    d->pauserehash = 0;\n    d->asyncdata = nullptr;\n    d->refcount = 1;\n    d->noshrink = false;\n    return DICT_OK;\n}\n\n/* Expand or create the hash table,\n * when malloc_failed is non-NULL, it'll avoid panic if malloc fails (in which case it'll be set to 1).\n * Returns DICT_OK if expand was performed, and DICT_ERR if skipped. */\nint _dictExpand(dict *d, unsigned long size, bool fShrink, int* malloc_failed)\n{\n    if (malloc_failed) *malloc_failed = 0;\n\n    /* the size is invalid if it is smaller than the number of\n     * elements already inside the hash table */\n    if (dictIsRehashing(d) || (d->ht[0].used > size && !fShrink) || size == 0)\n        return DICT_ERR;\n\n    dictht n; /* the new hash table */\n    unsigned long realsize = _dictNextPower(size);\n\n    /* Detect overflows */\n    if (realsize < size || realsize * sizeof(dictEntry*) < realsize)\n        return DICT_ERR;\n\n    /* Rehashing to the same table size is not useful. */\n    if (realsize == d->ht[0].size) return DICT_ERR;\n\n    /* Allocate the new hash table and initialize all pointers to NULL */\n    n.size = realsize;\n    n.sizemask = realsize-1;\n    if (malloc_failed) {\n        n.table = (dictEntry**)ztrycalloc(realsize*sizeof(dictEntry*));\n        *malloc_failed = n.table == NULL;\n        if (*malloc_failed)\n            return DICT_ERR;\n    } else {\n        n.table = (dictEntry**)zcalloc(realsize*sizeof(dictEntry*));\n    }\n\n    n.used = 0;\n\n    /* Is this the first initialization? If so it's not really a rehashing\n     * we just set the first hash table so that it can accept keys. */\n    if (d->ht[0].table == NULL) {\n        d->ht[0] = n;\n        return DICT_OK;\n    }\n\n    /* Prepare a second hash table for incremental rehashing */\n    d->ht[1] = n;\n    d->rehashidx = 0;\n    return DICT_OK;\n}\n\n/* Resize the table to the minimal size that contains all the elements,\n * but with the invariant of a USED/BUCKETS ratio near to <= 1 */\nint dictResize(dict *d)\n{\n    unsigned long minimal;\n\n    if (!dict_can_resize || dictIsRehashing(d)) return DICT_ERR;\n    minimal = d->ht[0].used;\n    if (minimal < DICT_HT_INITIAL_SIZE)\n        minimal = DICT_HT_INITIAL_SIZE;\n    return _dictExpand(d, minimal, false /*fShirnk*/, nullptr);\n}\n\nint dictMerge(dict *dst, dict *src)\n{\n#define MERGE_BLOCK_SIZE 4\n    dictEntry *rgdeT[MERGE_BLOCK_SIZE];\n\n    assert(dst != src);\n    if (dictSize(src) == 0)\n        return DICT_OK;\n    \n    if (dictSize(dst) == 0)\n    {\n        dict::swap(*dst, *src);\n        std::swap(dst->pauserehash, src->pauserehash);\n        return DICT_OK;\n    }\n\n    size_t expectedSize = dictSize(src) + dictSize(dst);\n    if (dictSize(src) > dictSize(dst) && src->asyncdata == nullptr && dst->asyncdata == nullptr)\n    {\n        dict::swap(*dst, *src);\n        std::swap(dst->pauserehash, src->pauserehash);\n    }\n\n    if (!dictIsRehashing(dst) && !dictIsRehashing(src))\n    {\n        if (dst->ht[0].size >= src->ht[0].size)\n        {\n            dst->ht[1] = dst->ht[0];\n            dst->ht[0] = src->ht[0];\n        }\n        else\n        {\n            dst->ht[1] = src->ht[0];\n        }\n        _dictReset(&src->ht[0]);\n        dst->rehashidx = 0;\n        assert(dictIsRehashing(dst));\n        assert((dictSize(src)+dictSize(dst)) == expectedSize);\n        return DICT_OK;\n    }\n\n    if (!dictIsRehashing(src) && dictSize(src) > 0 &&\n        (src->ht[0].size == dst->ht[0].size || src->ht[0].size == dst->ht[1].size))\n    {\n        auto &htDst = (src->ht[0].size == dst->ht[0].size) ? dst->ht[0] : dst->ht[1];\n\n        assert(src->ht[0].size == htDst.size);\n        for (size_t ide = 0; ide < src->ht[0].size; ide += MERGE_BLOCK_SIZE)\n        {\n            if (src->ht[0].used == 0)\n                break;\n\n            for (int dde = 0; dde < MERGE_BLOCK_SIZE; ++dde) {\n                rgdeT[dde] = src->ht[0].table[ide + dde];\n                src->ht[0].table[ide + dde] = nullptr;\n            }\n\n            for (;;) {\n                bool fAnyFound = false;\n                for (int dde = 0; dde < MERGE_BLOCK_SIZE; ++dde) {\n                    if (rgdeT[dde] == nullptr)\n                        continue;\n                    dictEntry *deNext = rgdeT[dde]->next;\n                    rgdeT[dde]->next = htDst.table[ide+dde];\n                    htDst.table[ide+dde] = rgdeT[dde];\n                    rgdeT[dde] = deNext;\n                    htDst.used++;\n                    src->ht[0].used--;\n\n                    fAnyFound = fAnyFound || (deNext != nullptr);\n                }\n\n                if (!fAnyFound)\n                    break;\n            }\n        }\n        // If we copied to the base hash table of a rehashing dst, reset the rehash\n        if (dictIsRehashing(dst) && src->ht[0].size == dst->ht[0].size)\n            dst->rehashidx = 0;\n        assert(dictSize(src) == 0);\n        assert((dictSize(src)+dictSize(dst)) == expectedSize);\n        return DICT_OK;\n    }\n\n    _dictExpand(dst, dictSize(dst)+dictSize(src), false /* fShrink */, nullptr);    // start dst rehashing if necessary\n    auto &htDst = dictIsRehashing(dst) ? dst->ht[1] : dst->ht[0];\n    for (int iht = 0; iht < 2; ++iht)\n    {\n        for (size_t ide = 0; ide < src->ht[iht].size; ide += MERGE_BLOCK_SIZE)\n        {\n            if (src->ht[iht].used == 0)\n                break;\n\n            for (int dde = 0; dde < MERGE_BLOCK_SIZE; ++dde) {\n                rgdeT[dde] = src->ht[iht].table[ide + dde];\n                src->ht[iht].table[ide + dde] = nullptr;\n            }\n\n            for (;;) {\n                bool fAnyFound = false;\n                for (int dde = 0; dde < MERGE_BLOCK_SIZE; ++dde) {\n                    if (rgdeT[dde] == nullptr)\n                        continue;\n                    uint64_t h = dictHashKey(dst, rgdeT[dde]->key) & htDst.sizemask;\n                    dictEntry *deNext = rgdeT[dde]->next;\n                    rgdeT[dde]->next = htDst.table[h];\n                    htDst.table[h] = rgdeT[dde];\n                    rgdeT[dde] = deNext;\n                    htDst.used++;\n                    src->ht[iht].used--;\n                    fAnyFound = fAnyFound || (deNext != nullptr);\n                }\n                if (!fAnyFound)\n                    break;\n            }\n#if 0\n            dictEntry *de = src->ht[iht].table[ide];\n            src->ht[iht].table[ide] = nullptr;\n            while (de != nullptr)\n            {\n                dictEntry *deNext = de->next;\n                \n                uint64_t h = dictHashKey(dst, de->key) & htDst.sizemask;\n                de->next = htDst.table[h];\n                htDst.table[h] = de;\n                htDst.used++;\n\n                de = deNext;\n                src->ht[iht].used--;\n            }\n#endif\n        }\n    }\n    assert((dictSize(src)+dictSize(dst)) == expectedSize);\n    return DICT_OK;\n}\n\n/* return DICT_ERR if expand was not performed */\nint dictExpand(dict *d, unsigned long size, bool fShrink) {\n    // External expand likely means mass insertion, and we don't want to shrink during that\n    d->noshrink = true;\n    return _dictExpand(d, size, fShrink, NULL);\n}\n\n/* return DICT_ERR if expand failed due to memory allocation failure */\nint dictTryExpand(dict *d, unsigned long size, bool fShrink) {\n    int malloc_failed;\n    // External expand likely means mass insertion, and we don't want to shrink during that\n    d->noshrink = true;\n    _dictExpand(d, size, fShrink, &malloc_failed);\n    return malloc_failed? DICT_ERR : DICT_OK;\n}\n\n/* Performs N steps of incremental rehashing. Returns 1 if there are still\n * keys to move from the old to the new hash table, otherwise 0 is returned.\n *\n * Note that a rehashing step consists in moving a bucket (that may have more\n * than one key as we use chaining) from the old to the new hash table, however\n * since part of the hash table may be composed of empty spaces, it is not\n * guaranteed that this function will rehash even a single bucket, since it\n * will visit at max N*10 empty buckets in total, otherwise the amount of\n * work it does would be unbound and the function may block for a long time. */\nint dictRehash(dict *d, int n) {\n    int empty_visits = n*10; /* Max number of empty buckets to visit. */\n    if (!dictIsRehashing(d)) return 0;\n    if (d->asyncdata) return 0;\n\n    while(n-- && d->ht[0].used != 0) {\n        dictEntry *de, *nextde;\n\n        /* Note that rehashidx can't overflow as we are sure there are more\n         * elements because ht[0].used != 0 */\n        while(d->ht[0].table[d->rehashidx] == NULL) {\n            d->rehashidx++;\n            if (--empty_visits == 0) return 1;\n        }\n        de = d->ht[0].table[d->rehashidx];\n        /* Move all the keys in this bucket from the old to the new hash HT */\n        while(de) {\n            uint64_t h;\n\n            nextde = de->next;\n            /* Get the index in the new hash table */\n            h = dictHashKey(d, de->key) & d->ht[1].sizemask;\n            de->next = d->ht[1].table[h];\n            d->ht[1].table[h] = de;\n            d->ht[0].used--;\n            d->ht[1].used++;\n            de = nextde;\n        }\n        d->ht[0].table[d->rehashidx] = NULL;\n        d->rehashidx++;\n    }\n\n    /* Check if we already rehashed the whole table... */\n    if (d->ht[0].used == 0) {\n        asyncFreeDictTable(d->ht[0].table);\n        d->ht[0] = d->ht[1];\n        _dictReset(&d->ht[1]);\n        d->rehashidx = -1;\n        return 0;\n    }\n\n    /* More to rehash... */\n    return 1;\n}\n\ndictAsyncRehashCtl::dictAsyncRehashCtl(struct dict *d, dictAsyncRehashCtl *next) : dict(d), next(next) {\n    queue.reserve(c_targetQueueSize);\n    __atomic_fetch_add(&d->refcount, 1, __ATOMIC_ACQ_REL);\n    this->rehashIdxBase = d->rehashidx;\n}\n\ndictAsyncRehashCtl *dictRehashAsyncStart(dict *d, int buckets) {\n    assert(d->type->asyncfree != nullptr);\n    if (!dictIsRehashing(d) || d->pauserehash != 0) return nullptr;\n\n    d->asyncdata = new dictAsyncRehashCtl(d, d->asyncdata);\n\n    int empty_visits = buckets*10;\n\n    while (d->asyncdata->queue.size() < (size_t)buckets && (size_t)d->rehashidx < d->ht[0].size) {\n        dictEntry *de;\n\n        /* Note that rehashidx can't overflow as we are sure there are more\n         * elements because ht[0].used != 0 */\n        while(d->ht[0].table[d->rehashidx] == NULL) {\n            d->rehashidx++;\n            if (--empty_visits == 0) goto LDone;\n            if ((size_t)d->rehashidx >= d->ht[0].size) goto LDone;\n        }\n\n        de = d->ht[0].table[d->rehashidx];\n        // We have to queue every node in the bucket, even if we go over our target size\n        while (de != nullptr) {\n            d->asyncdata->queue.emplace_back(de);\n            de = de->next;\n        }\n        d->rehashidx++;\n    }\n\nLDone:\n    if (d->asyncdata->queue.empty()) {\n        // We didn't find any work to do (can happen if there is a large gap in the hash table)\n        auto asyncT = d->asyncdata;\n        d->asyncdata = d->asyncdata->next;\n        delete asyncT;\n        return nullptr;\n    }\n    return d->asyncdata;\n}\n\nvoid dictRehashAsync(dictAsyncRehashCtl *ctl) {\n    if (ctl->abondon.load(std::memory_order_acquire)) {\n        ctl->hashIdx = ctl->queue.size();\n    }\n    for (size_t idx = ctl->hashIdx; idx < ctl->queue.size(); ++idx) {\n        auto &wi = ctl->queue[idx];\n        wi.hash = dictHashKey(ctl->dict, dictGetKey(wi.de));\n    }\n    ctl->hashIdx = ctl->queue.size();\n    ctl->done = true;\n}\n\nbool dictRehashSomeAsync(dictAsyncRehashCtl *ctl, size_t hashes) {\n    if (ctl->abondon.load(std::memory_order_acquire)) {\n        ctl->hashIdx = ctl->queue.size();\n    }\n    size_t max = std::min(ctl->hashIdx + hashes, ctl->queue.size());\n    for (; ctl->hashIdx < max; ++ctl->hashIdx) {\n        auto &wi = ctl->queue[ctl->hashIdx];\n        wi.hash = dictHashKey(ctl->dict, dictGetKey(wi.de));\n    }\n\n    if (ctl->hashIdx == ctl->queue.size()) ctl->done = true;\n    return ctl->hashIdx < ctl->queue.size();\n}\n\n\nvoid discontinueAsyncRehash(dict *d) {\n    // We inform our async rehashers and the completion function the results are to be\n    //  abandoned.  We keep the asyncdata linked in so that dictEntry's are still added\n    //  to the GC list.  This is because we can't gurantee when the other threads will\n    //  stop looking at them.\n    if (d->asyncdata != nullptr) {\n        auto adata = d->asyncdata;\n        while (adata != nullptr && !adata->abondon.load(std::memory_order_relaxed)) {\n            adata->abondon = true;\n            adata = adata->next;\n        }\n        if (dictIsRehashing(d))\n            d->rehashidx = 0;\n    }\n}\n\nvoid dictCompleteRehashAsync(dictAsyncRehashCtl *ctl, bool fFree) {\n    dict *d = ctl->dict;\n    assert(ctl->done);\n    \n    // Unlink ourselves\n    bool fUnlinked = false;\n    dictAsyncRehashCtl **pprev = &d->asyncdata;\n\n    while (*pprev != nullptr) {\n        if (*pprev == ctl) {\n            *pprev = ctl->next;\n            fUnlinked = true;\n            break;\n        }\n        pprev = &((*pprev)->next);\n    }\n\n    if (fUnlinked) {\n        if (ctl->next != nullptr && ctl->deGCList != nullptr) {\n            // An older work item may depend on our free list, so pass our free list to them\n            dictEntry **deGC = &ctl->next->deGCList;\n            while (*deGC != nullptr) deGC = &((*deGC)->next);\n            *deGC = ctl->deGCList;\n            ctl->deGCList = nullptr;\n        }\n    }\n\n    if (fUnlinked && !ctl->abondon) {\n        if (d->ht[0].table != nullptr) {    // can be null if we're cleared during the rehash\n            for (auto &wi : ctl->queue) {\n                // We need to remove it from the source hash table, and store it in the dest.\n                //  Note that it may have been deleted in the meantime and therefore not exist.\n                //  In this case it will be in the garbage collection list\n                \n                dictEntry **pdePrev = &d->ht[0].table[wi.hash & d->ht[0].sizemask];\n                while (*pdePrev != nullptr && *pdePrev != wi.de) {\n                    pdePrev = &((*pdePrev)->next);\n                }\n                if (*pdePrev != nullptr) {  // The element may be NULL if its in the GC list\n                    assert(*pdePrev == wi.de);\n                    *pdePrev = wi.de->next;\n                    // Now link it to the dest hash table\n                    wi.de->next = d->ht[1].table[wi.hash & d->ht[1].sizemask];\n                    d->ht[1].table[wi.hash & d->ht[1].sizemask] = wi.de;\n                    d->ht[0].used--;\n                    d->ht[1].used++;\n                }\n            }\n        }\n\n        /* Check if we already rehashed the whole table... */\n        if (d->ht[0].used == 0 && d->asyncdata == nullptr) {\n            asyncFreeDictTable(d->ht[0].table);\n            d->ht[0] = d->ht[1];\n            _dictReset(&d->ht[1]);\n            d->rehashidx = -1;\n        }\n    }\n\n    if (fFree) {\n        d->type->asyncfree(ctl);\n    }\n}\n\nlong long timeInMilliseconds(void) {\n    struct timeval tv;\n\n    gettimeofday(&tv,NULL);\n    return (((long long)tv.tv_sec)*1000)+(tv.tv_usec/1000);\n}\n\ndictAsyncRehashCtl::~dictAsyncRehashCtl() {\n    while (deGCList != nullptr) {\n        auto next = deGCList->next;\n        dictFreeKey(dict, deGCList);\n        if (deGCList->v.val != nullptr)\n            dictFreeVal(dict, deGCList);\n        zfree(deGCList);\n        deGCList = next;\n    }\n    dictRelease(this->dict);\n}\n\n/* Rehash in ms+\"delta\" milliseconds. The value of \"delta\" is larger \n * than 0, and is smaller than 1 in most cases. The exact upper bound \n * depends on the running time of dictRehash(d,100).*/\nint dictRehashMilliseconds(dict *d, int ms) {\n    if (d->pauserehash > 0) return 0;\n\n    long long start = timeInMilliseconds();\n    int rehashes = 0;\n\n    while(dictRehash(d,1000)) {\n        rehashes += 1000;\n        if (timeInMilliseconds()-start > ms) break;\n    }\n    return rehashes;\n}\n\n/* This function performs just a step of rehashing, and only if hashing has\n * not been paused for our hash table. When we have iterators in the\n * middle of a rehashing we can't mess with the two hash tables otherwise\n * some element can be missed or duplicated.\n *\n * This function is called by common lookup or update operations in the\n * dictionary so that the hash table automatically migrates from H1 to H2\n * while it is actively used. */\nstatic void _dictRehashStep(dict *d) {\n    int16_t pauserehash;\n    __atomic_load(&d->pauserehash, &pauserehash, __ATOMIC_RELAXED);\n    if (pauserehash == 0) dictRehash(d,1);\n}\n\n/* Add an element to the target hash table */\nint dictAdd(dict *d, void *key, void *val, dictEntry **existing)\n{\n    dictEntry *entry = dictAddRaw(d,key,existing);\n\n    if (!entry) return DICT_ERR;\n    dictSetVal(d, entry, val);\n    return DICT_OK;\n}\n\n/* Low level add or find:\n * This function adds the entry but instead of setting a value returns the\n * dictEntry structure to the user, that will make sure to fill the value\n * field as they wish.\n *\n * This function is also directly exposed to the user API to be called\n * mainly in order to store non-pointers inside the hash value, example:\n *\n * entry = dictAddRaw(dict,mykey,NULL);\n * if (entry != NULL) dictSetSignedIntegerVal(entry,1000);\n *\n * Return values:\n *\n * If key already exists NULL is returned, and \"*existing\" is populated\n * with the existing entry if existing is not NULL.\n *\n * If key was added, the hash entry is returned to be manipulated by the caller.\n */\ndictEntry *dictAddRaw(dict *d, void *key, dictEntry **existing)\n{\n    long index;\n    dictEntry *entry;\n    dictht *ht;\n\n    if (dictIsRehashing(d)) _dictRehashStep(d);\n\n    /* Get the index of the new element, or -1 if\n     * the element already exists. */\n    if ((index = _dictKeyIndex(d, key, dictHashKey(d,key), existing)) == -1)\n        return NULL;\n\n    /* Allocate the memory and store the new entry.\n     * Insert the element in top, with the assumption that in a database\n     * system it is more likely that recently added entries are accessed\n     * more frequently. */\n    ht = dictIsRehashing(d) ? &d->ht[1] : &d->ht[0];\n    entry = (dictEntry*)zmalloc(sizeof(*entry), MALLOC_SHARED);\n    entry->next = ht->table[index];\n    ht->table[index] = entry;\n    ht->used++;\n\n    /* Set the hash entry fields. */\n    dictSetKey(d, entry, key);\n    return entry;\n}\n\n/* Add or Overwrite:\n * Add an element, discarding the old value if the key already exists.\n * Return 1 if the key was added from scratch, 0 if there was already an\n * element with such key and dictReplace() just performed a value update\n * operation. */\nint dictReplace(dict *d, void *key, void *val)\n{\n    dictEntry *entry, *existing, auxentry;\n\n    /* Try to add the element. If the key\n     * does not exists dictAdd will succeed. */\n    entry = dictAddRaw(d,key,&existing);\n    if (entry) {\n        dictSetVal(d, entry, val);\n        return 1;\n    }\n\n    /* Set the new value and free the old one. Note that it is important\n     * to do that in this order, as the value may just be exactly the same\n     * as the previous one. In this context, think to reference counting,\n     * you want to increment (set), and then decrement (free), and not the\n     * reverse. */\n    auxentry = *existing;\n    dictSetVal(d, existing, val);\n    dictFreeVal(d, &auxentry);\n    return 0;\n}\n\n/* Add or Find:\n * dictAddOrFind() is simply a version of dictAddRaw() that always\n * returns the hash entry of the specified key, even if the key already\n * exists and can't be added (in that case the entry of the already\n * existing key is returned.)\n *\n * See dictAddRaw() for more information. */\ndictEntry *dictAddOrFind(dict *d, void *key) {\n    dictEntry *entry, *existing;\n    entry = dictAddRaw(d,key,&existing);\n    return entry ? entry : existing;\n}\n\n/* Search and remove an element. This is an helper function for\n * dictDelete() and dictUnlink(), please check the top comment\n * of those functions. */\nstatic dictEntry *dictGenericDelete(dict *d, const void *key, int nofree) {\n    uint64_t h, idx;\n    dictEntry *he, *prevHe;\n    int table;\n\n    // if we are deleting elements we probably aren't mass inserting anymore and it is safe to shrink\n    d->noshrink = false;\n\n    if (d->ht[0].used == 0 && d->ht[1].used == 0) return NULL;\n\n    if (dictIsRehashing(d)) _dictRehashStep(d);\n    h = dictHashKey(d, key);\n\n    for (table = 0; table <= 1; table++) {\n        idx = h & d->ht[table].sizemask;\n        he = d->ht[table].table[idx];\n        prevHe = NULL;\n        while(he) {\n            if (key==he->key || dictCompareKeys(d, key, he->key)) {\n                /* Unlink the element from the list */\n                if (prevHe)\n                    prevHe->next = he->next;\n                else\n                    d->ht[table].table[idx] = he->next;\n                if (!nofree) {\n                    if (table == 0 && d->asyncdata != nullptr && (ssize_t)idx < d->rehashidx) {\n                        dictFreeVal(d, he);\n                        he->v.val = nullptr;\n                        he->next = d->asyncdata->deGCList;\n                        d->asyncdata->deGCList = he;\n                    } else {\n                        dictFreeKey(d, he);\n                        dictFreeVal(d, he);\n                        zfree(he);\n                    }\n                }\n                d->ht[table].used--;\n                _dictExpandIfNeeded(d);\n                return he;\n            }\n            prevHe = he;\n            he = he->next;\n        }\n        if (!dictIsRehashing(d)) break;\n    }\n    _dictExpandIfNeeded(d);\n    return NULL; /* not found */\n}\n\n/* Remove an element, returning DICT_OK on success or DICT_ERR if the\n * element was not found. */\nint dictDelete(dict *ht, const void *key) {\n    return dictGenericDelete(ht,key,0) ? DICT_OK : DICT_ERR;\n}\n\n/* Remove an element from the table, but without actually releasing\n * the key, value and dictionary entry. The dictionary entry is returned\n * if the element was found (and unlinked from the table), and the user\n * should later call `dictFreeUnlinkedEntry()` with it in order to release it.\n * Otherwise if the key is not found, NULL is returned.\n *\n * This function is useful when we want to remove something from the hash\n * table but want to use its value before actually deleting the entry.\n * Without this function the pattern would require two lookups:\n *\n *  entry = dictFind(...);\n *  // Do something with entry\n *  dictDelete(dictionary,entry);\n *\n * Thanks to this function it is possible to avoid this, and use\n * instead:\n *\n * entry = dictUnlink(dictionary,entry);\n * // Do something with entry\n * dictFreeUnlinkedEntry(entry); // <- This does not need to lookup again.\n */\ndictEntry *dictUnlink(dict *ht, const void *key) {\n    return dictGenericDelete(ht,key,1);\n}\n\n/* You need to call this function to really free the entry after a call\n * to dictUnlink(). It's safe to call this function with 'he' = NULL. */\nvoid dictFreeUnlinkedEntry(dict *d, dictEntry *he) {\n    if (he == NULL) return;\n\n    if (d->asyncdata) {\n        dictFreeVal(d, he);\n        he->v.val = nullptr;\n        he->next = d->asyncdata->deGCList;\n        d->asyncdata->deGCList = he;\n    } else { \n        dictFreeKey(d, he);\n        dictFreeVal(d, he);\n        zfree(he);\n    }\n}\n\n/* Destroy an entire dictionary */\nint _dictClear(dict *d, dictht *ht, void(callback)(void *)) {\n    unsigned long i;\n\n    /* Free all the elements */\n    for (i = 0; i < ht->size && ht->used > 0; i++) {\n        dictEntry *he, *nextHe;\n\n        if (callback && (i & 65535) == 0) callback(d->privdata);\n\n        if ((he = ht->table[i]) == NULL) continue;\n        dictEntry *deNull = nullptr;\n        __atomic_store(&ht->table[i], &deNull, __ATOMIC_RELEASE);\n        while(he) {\n            nextHe = he->next;\n            if (d->asyncdata && (ssize_t)i < d->rehashidx) {\n                dictFreeVal(d, he);\n                he->v.val = nullptr;\n                he->next = d->asyncdata->deGCList;\n                d->asyncdata->deGCList = he;\n            } else {\n                dictFreeKey(d, he);\n                dictFreeVal(d, he);\n                zfree(he);\n            }\n            ht->used--;\n            he = nextHe;\n        }\n    }\n    /* Free the table and the allocated cache structure */\n    asyncFreeDictTable(ht->table);\n    /* Re-initialize the table */\n    _dictReset(ht);\n    return DICT_OK; /* never fails */\n}\n\n/* Clear & Release the hash table */\nvoid dictRelease(dict *d)\n{\n    if (__atomic_sub_fetch(&d->refcount, 1, __ATOMIC_ACQ_REL) == 0) {\n        _dictClear(d,&d->ht[0],NULL);\n        _dictClear(d,&d->ht[1],NULL);\n        zfree(d);\n    }\n}\n\ndictEntry *dictFindWithPrev(dict *d, const void *key, uint64_t h, dictEntry ***dePrevPtr, dictht **pht, bool fShallowCompare)\n{\n    dictEntry *he;\n    uint64_t idx, table;\n\n    if (dictSize(d) == 0) return NULL; /* dict is empty */\n    if (dictIsRehashing(d)) _dictRehashStep(d);\n    for (table = 0; table <= 1; table++) {\n        *pht = d->ht + table;\n        idx = h & d->ht[table].sizemask;\n        he = d->ht[table].table[idx];\n        *dePrevPtr = &d->ht[table].table[idx];\n        while(he) {\n            if (key==he->key || (!fShallowCompare && dictCompareKeys(d, key, he->key))) {\n                return he;\n            }\n            *dePrevPtr = &he->next;\n            he = he->next;\n        }\n        if (!dictIsRehashing(d)) return NULL;\n    }\n    return NULL;\n}\n\ndictEntry *dictFind(dict *d, const void *key)\n{\n    dictEntry **deT;\n    dictht *ht;\n    uint64_t h = dictHashKey(d, key);\n    return dictFindWithPrev(d, key, h, &deT, &ht);\n}\n\nvoid *dictFetchValue(dict *d, const void *key) {\n    dictEntry *he;\n\n    he = dictFind(d,key);\n    return he ? dictGetVal(he) : NULL;\n}\n\n/* A fingerprint is a 64 bit number that represents the state of the dictionary\n * at a given time, it's just a few dict properties xored together.\n * When an unsafe iterator is initialized, we get the dict fingerprint, and check\n * the fingerprint again when the iterator is released.\n * If the two fingerprints are different it means that the user of the iterator\n * performed forbidden operations against the dictionary while iterating. */\nlong long dictFingerprint(dict *d) {\n    long long integers[6], hash = 0;\n    int j;\n\n    integers[0] = (long) d->ht[0].table;\n    integers[1] = d->ht[0].size;\n    integers[2] = d->ht[0].used;\n    integers[3] = (long) d->ht[1].table;\n    integers[4] = d->ht[1].size;\n    integers[5] = d->ht[1].used;\n\n    /* We hash N integers by summing every successive integer with the integer\n     * hashing of the previous sum. Basically:\n     *\n     * Result = hash(hash(hash(int1)+int2)+int3) ...\n     *\n     * This way the same set of integers in a different order will (likely) hash\n     * to a different number. */\n    for (j = 0; j < 6; j++) {\n        hash += integers[j];\n        /* For the hashing step we use Tomas Wang's 64 bit integer hash. */\n        hash = (~hash) + (hash << 21); // hash = (hash << 21) - hash - 1;\n        hash = hash ^ (hash >> 24);\n        hash = (hash + (hash << 3)) + (hash << 8); // hash * 265\n        hash = hash ^ (hash >> 14);\n        hash = (hash + (hash << 2)) + (hash << 4); // hash * 21\n        hash = hash ^ (hash >> 28);\n        hash = hash + (hash << 31);\n    }\n    return hash;\n}\n\ndictIterator *dictGetIterator(dict *d)\n{\n    dictIterator *iter = (dictIterator*)zmalloc(sizeof(*iter), MALLOC_LOCAL);\n\n    iter->d = d;\n    iter->table = 0;\n    iter->index = -1;\n    iter->safe = 0;\n    iter->entry = NULL;\n    iter->nextEntry = NULL;\n    return iter;\n}\n\ndictIterator *dictGetSafeIterator(dict *d) {\n    dictIterator *i = dictGetIterator(d);\n\n    i->safe = 1;\n    return i;\n}\n\ndictEntry *dictNext(dictIterator *iter)\n{\n    while (1) {\n        if (iter->entry == NULL) {\n            dictht *ht = &iter->d->ht[iter->table];\n            if (iter->index == -1 && iter->table == 0) {\n                if (iter->safe)\n                    dictPauseRehashing(iter->d);\n                else\n                    iter->fingerprint = dictFingerprint(iter->d);\n            }\n            iter->index++;\n            if (iter->index >= (long) ht->size) {\n                if (dictIsRehashing(iter->d) && iter->table == 0) {\n                    iter->table++;\n                    iter->index = 0;\n                    ht = &iter->d->ht[1];\n                } else {\n                    break;\n                }\n            }\n            iter->entry = ht->table[iter->index];\n        } else {\n            iter->entry = iter->nextEntry;\n        }\n        if (iter->entry) {\n            /* We need to save the 'next' here, the iterator user\n             * may delete the entry we are returning. */\n            iter->nextEntry = iter->entry->next;\n            return iter->entry;\n        }\n    }\n    return NULL;\n}\n\nvoid dictReleaseIterator(dictIterator *iter)\n{\n    if (!(iter->index == -1 && iter->table == 0)) {\n        if (iter->safe)\n            dictResumeRehashing(iter->d);\n        else\n            assert(iter->fingerprint == dictFingerprint(iter->d));\n    }\n    zfree(iter);\n}\n\n/* Return a random entry from the hash table. Useful to\n * implement randomized algorithms */\ndictEntry *dictGetRandomKey(dict *d)\n{\n    dictEntry *he, *orighe;\n    unsigned long h;\n    int listlen, listele;\n\n    if (dictSize(d) == 0) return NULL;\n    if (dictIsRehashing(d)) _dictRehashStep(d);\n    if (dictIsRehashing(d)) {\n        long rehashidx = d->rehashidx;\n        auto async = d->asyncdata;\n        while (async != nullptr) {\n            rehashidx = std::min((long)async->rehashIdxBase, rehashidx);\n            async = async->next;\n        }\n        do {\n            /* We are sure there are no elements in indexes from 0\n             * to rehashidx-1 */\n            h = rehashidx + (randomULong() % (dictSlots(d) - rehashidx));\n            he = (h >= d->ht[0].size) ? d->ht[1].table[h - d->ht[0].size] :\n                                      d->ht[0].table[h];\n        } while(he == NULL);\n    } else {\n        do {\n            h = randomULong() & d->ht[0].sizemask;\n            he = d->ht[0].table[h];\n        } while(he == NULL);\n    }\n\n    /* Now we found a non empty bucket, but it is a linked\n     * list and we need to get a random element from the list.\n     * The only sane way to do so is counting the elements and\n     * select a random index. */\n    listlen = 0;\n    orighe = he;\n    while(he) {\n        he = he->next;\n        listlen++;\n    }\n    listele = random() % listlen;\n    he = orighe;\n    while(listele--) he = he->next;\n    return he;\n}\n\n/* This function samples the dictionary to return a few keys from random\n * locations.\n *\n * It does not guarantee to return all the keys specified in 'count', nor\n * it does guarantee to return non-duplicated elements, however it will make\n * some effort to do both things.\n *\n * Returned pointers to hash table entries are stored into 'des' that\n * points to an array of dictEntry pointers. The array must have room for\n * at least 'count' elements, that is the argument we pass to the function\n * to tell how many random elements we need.\n *\n * The function returns the number of items stored into 'des', that may\n * be less than 'count' if the hash table has less than 'count' elements\n * inside, or if not enough elements were found in a reasonable amount of\n * steps.\n *\n * Note that this function is not suitable when you need a good distribution\n * of the returned items, but only when you need to \"sample\" a given number\n * of continuous elements to run some kind of algorithm or to produce\n * statistics. However the function is much faster than dictGetRandomKey()\n * at producing N elements. */\nunsigned int dictGetSomeKeys(dict *d, dictEntry **des, unsigned int count) {\n    unsigned long j; /* internal hash table id, 0 or 1. */\n    unsigned long tables; /* 1 or 2 tables? */\n    unsigned long stored = 0, maxsizemask;\n    unsigned long maxsteps;\n\n    if (dictSize(d) < count) count = dictSize(d);\n    maxsteps = count*10;\n\n    /* Try to do a rehashing work proportional to 'count'. */\n    for (j = 0; j < count; j++) {\n        if (dictIsRehashing(d))\n            _dictRehashStep(d);\n        else\n            break;\n    }\n\n    tables = dictIsRehashing(d) ? 2 : 1;\n    maxsizemask = d->ht[0].sizemask;\n    if (tables > 1 && maxsizemask < d->ht[1].sizemask)\n        maxsizemask = d->ht[1].sizemask;\n\n    /* Pick a random point inside the larger table. */\n    unsigned long i = randomULong() & maxsizemask;\n    unsigned long emptylen = 0; /* Continuous empty entries so far. */\n    while(stored < count && maxsteps--) {\n        for (j = 0; j < tables; j++) {\n            /* Invariant of the dict.c rehashing: up to the indexes already\n             * visited in ht[0] during the rehashing, there are no populated\n             * buckets, so we can skip ht[0] for indexes between 0 and idx-1. */\n            if (tables == 2 && j == 0 && i < (unsigned long) d->rehashidx) {\n                /* Moreover, if we are currently out of range in the second\n                 * table, there will be no elements in both tables up to\n                 * the current rehashing index, so we jump if possible.\n                 * (this happens when going from big to small table). */\n                if (i >= d->ht[1].size)\n                    i = d->rehashidx;\n                else\n                    continue;\n            }\n            if (i >= d->ht[j].size) continue; /* Out of range for this table. */\n            dictEntry *he = d->ht[j].table[i];\n\n            /* Count contiguous empty buckets, and jump to other\n             * locations if they reach 'count' (with a minimum of 5). */\n            if (he == NULL) {\n                emptylen++;\n                if (emptylen >= 5 && emptylen > count) {\n                    i = randomULong() & maxsizemask;\n                    emptylen = 0;\n                }\n            } else {\n                emptylen = 0;\n                while (he) {\n                    /* Collect all the elements of the buckets found non\n                     * empty while iterating. */\n                    *des = he;\n                    des++;\n                    he = he->next;\n                    stored++;\n                    if (stored == count) return stored;\n                }\n            }\n        }\n        i = (i+1) & maxsizemask;\n    }\n    return stored;\n}\n\n/* This is like dictGetRandomKey() from the POV of the API, but will do more\n * work to ensure a better distribution of the returned element.\n *\n * This function improves the distribution because the dictGetRandomKey()\n * problem is that it selects a random bucket, then it selects a random\n * element from the chain in the bucket. However elements being in different\n * chain lengths will have different probabilities of being reported. With\n * this function instead what we do is to consider a \"linear\" range of the table\n * that may be constituted of N buckets with chains of different lengths\n * appearing one after the other. Then we report a random element in the range.\n * In this way we smooth away the problem of different chain lengths. */\n#define GETFAIR_NUM_ENTRIES 15\ndictEntry *dictGetFairRandomKey(dict *d) {\n    dictEntry *entries[GETFAIR_NUM_ENTRIES];\n    unsigned int count = dictGetSomeKeys(d,entries,GETFAIR_NUM_ENTRIES);\n    /* Note that dictGetSomeKeys() may return zero elements in an unlucky\n     * run() even if there are actually elements inside the hash table. So\n     * when we get zero, we call the true dictGetRandomKey() that will always\n     * yield the element if the hash table has at least one. */\n    if (count == 0) return dictGetRandomKey(d);\n    unsigned int idx = rand() % count;\n    return entries[idx];\n}\n\n/* Function to reverse bits. Algorithm from:\n * http://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel */\nstatic unsigned long rev(unsigned long v) {\n    unsigned long s = CHAR_BIT * sizeof(v); // bit size; must be power of 2\n    unsigned long mask = ~0UL;\n    while ((s >>= 1) > 0) {\n        mask ^= (mask << s);\n        v = ((v >> s) & mask) | ((v << s) & ~mask);\n    }\n    return v;\n}\n\n/* dictScan() is used to iterate over the elements of a dictionary.\n *\n * Iterating works the following way:\n *\n * 1) Initially you call the function using a cursor (v) value of 0.\n * 2) The function performs one step of the iteration, and returns the\n *    new cursor value you must use in the next call.\n * 3) When the returned cursor is 0, the iteration is complete.\n *\n * The function guarantees all elements present in the\n * dictionary get returned between the start and end of the iteration.\n * However it is possible some elements get returned multiple times.\n *\n * For every element returned, the callback argument 'fn' is\n * called with 'privdata' as first argument and the dictionary entry\n * 'de' as second argument.\n *\n * HOW IT WORKS.\n *\n * The iteration algorithm was designed by Pieter Noordhuis.\n * The main idea is to increment a cursor starting from the higher order\n * bits. That is, instead of incrementing the cursor normally, the bits\n * of the cursor are reversed, then the cursor is incremented, and finally\n * the bits are reversed again.\n *\n * This strategy is needed because the hash table may be resized between\n * iteration calls.\n *\n * dict.c hash tables are always power of two in size, and they\n * use chaining, so the position of an element in a given table is given\n * by computing the bitwise AND between Hash(key) and SIZE-1\n * (where SIZE-1 is always the mask that is equivalent to taking the rest\n *  of the division between the Hash of the key and SIZE).\n *\n * For example if the current hash table size is 16, the mask is\n * (in binary) 1111. The position of a key in the hash table will always be\n * the last four bits of the hash output, and so forth.\n *\n * WHAT HAPPENS IF THE TABLE CHANGES IN SIZE?\n *\n * If the hash table grows, elements can go anywhere in one multiple of\n * the old bucket: for example let's say we already iterated with\n * a 4 bit cursor 1100 (the mask is 1111 because hash table size = 16).\n *\n * If the hash table will be resized to 64 elements, then the new mask will\n * be 111111. The new buckets you obtain by substituting in ??1100\n * with either 0 or 1 can be targeted only by keys we already visited\n * when scanning the bucket 1100 in the smaller hash table.\n *\n * By iterating the higher bits first, because of the inverted counter, the\n * cursor does not need to restart if the table size gets bigger. It will\n * continue iterating using cursors without '1100' at the end, and also\n * without any other combination of the final 4 bits already explored.\n *\n * Similarly when the table size shrinks over time, for example going from\n * 16 to 8, if a combination of the lower three bits (the mask for size 8\n * is 111) were already completely explored, it would not be visited again\n * because we are sure we tried, for example, both 0111 and 1111 (all the\n * variations of the higher bit) so we don't need to test it again.\n *\n * WAIT... YOU HAVE *TWO* TABLES DURING REHASHING!\n *\n * Yes, this is true, but we always iterate the smaller table first, then\n * we test all the expansions of the current cursor into the larger\n * table. For example if the current cursor is 101 and we also have a\n * larger table of size 16, we also test (0)101 and (1)101 inside the larger\n * table. This reduces the problem back to having only one table, where\n * the larger one, if it exists, is just an expansion of the smaller one.\n *\n * LIMITATIONS\n *\n * This iterator is completely stateless, and this is a huge advantage,\n * including no additional memory used.\n *\n * The disadvantages resulting from this design are:\n *\n * 1) It is possible we return elements more than once. However this is usually\n *    easy to deal with in the application level.\n * 2) The iterator must return multiple elements per call, as it needs to always\n *    return all the keys chained in a given bucket, and all the expansions, so\n *    we are sure we don't miss keys moving during rehashing.\n * 3) The reverse cursor is somewhat hard to understand at first, but this\n *    comment is supposed to help.\n */\nunsigned long dictScan(dict *d,\n                       unsigned long v,\n                       dictScanFunction *fn,\n                       dictScanBucketFunction* bucketfn,\n                       void *privdata)\n{\n    dictht *t0, *t1;\n    const dictEntry *de, *next;\n    unsigned long m0, m1;\n\n    if (dictSize(d) == 0) return 0;\n\n    /* This is needed in case the scan callback tries to do dictFind or alike. */\n    dictPauseRehashing(d);\n\n    if (!dictIsRehashing(d)) {\n        t0 = &(d->ht[0]);\n        m0 = t0->sizemask;\n\n        /* Emit entries at cursor */\n        if (bucketfn) bucketfn(privdata, &t0->table[v & m0]);\n        de = t0->table[v & m0];\n        while (de) {\n            next = de->next;\n            fn(privdata, de);\n            de = next;\n        }\n\n        /* Set unmasked bits so incrementing the reversed cursor\n         * operates on the masked bits */\n        v |= ~m0;\n\n        /* Increment the reverse cursor */\n        v = rev(v);\n        v++;\n        v = rev(v);\n\n    } else {\n        t0 = &d->ht[0];\n        t1 = &d->ht[1];\n\n        /* Make sure t0 is the smaller and t1 is the bigger table */\n        if (t0->size > t1->size) {\n            t0 = &d->ht[1];\n            t1 = &d->ht[0];\n        }\n\n        m0 = t0->sizemask;\n        m1 = t1->sizemask;\n\n        /* Emit entries at cursor */\n        if (bucketfn) bucketfn(privdata, &t0->table[v & m0]);\n        de = t0->table[v & m0];\n        while (de) {\n            next = de->next;\n            fn(privdata, de);\n            de = next;\n        }\n\n        /* Iterate over indices in larger table that are the expansion\n         * of the index pointed to by the cursor in the smaller table */\n        do {\n            /* Emit entries at cursor */\n            if (bucketfn) bucketfn(privdata, &t1->table[v & m1]);\n            de = t1->table[v & m1];\n            while (de) {\n                next = de->next;\n                fn(privdata, de);\n                de = next;\n            }\n\n            /* Increment the reverse cursor not covered by the smaller mask.*/\n            v |= ~m1;\n            v = rev(v);\n            v++;\n            v = rev(v);\n\n            /* Continue while bits covered by mask difference is non-zero */\n        } while (v & (m0 ^ m1));\n    }\n\n    dictResumeRehashing(d);\n\n    return v;\n}\n\n/* ------------------------- private functions ------------------------------ */\n\n/* Because we may need to allocate huge memory chunk at once when dict\n * expands, we will check this allocation is allowed or not if the dict\n * type has expandAllowed member function. */\nstatic int dictTypeExpandAllowed(dict *d) {\n    if (d->type->expandAllowed == NULL) return 1;\n    return d->type->expandAllowed(\n                    _dictNextPower(d->ht[0].used + 1) * sizeof(dictEntry*),\n                    (double)d->ht[0].used / d->ht[0].size);\n}\n\n/* Expand the hash table if needed */\nstatic int _dictExpandIfNeeded(dict *d)\n{\n    static const size_t SHRINK_FACTOR = 4;\n    /* Incremental rehashing already in progress. Return. */\n    if (dictIsRehashing(d)) return DICT_OK;\n\n    /* If the hash table is empty expand it to the initial size. */\n    if (d->ht[0].size == 0) return _dictExpand(d, DICT_HT_INITIAL_SIZE, false /*fShrink*/, nullptr);\n\n    /* If we reached the 1:1 ratio, and we are allowed to resize the hash\n     * table (global setting) or we should avoid it but the ratio between\n     * elements/buckets is over the \"safe\" threshold, we resize doubling\n     * the number of buckets. */\n    if (d->ht[0].used >= d->ht[0].size &&\n        (dict_can_resize ||\n         d->ht[0].used/d->ht[0].size > dict_force_resize_ratio) &&\n        dictTypeExpandAllowed(d))\n    {\n        return _dictExpand(d, d->ht[0].used + 1, false /*fShrink*/, nullptr);\n    }\n    else if (d->ht[0].used > 0 && d->ht[0].size >= (1024*SHRINK_FACTOR) && (d->ht[0].used * 16) < d->ht[0].size && dict_can_resize && !d->noshrink)\n    {\n        // If the dictionary has shurnk a lot we'll need to shrink the hash table instead\n        return _dictExpand(d, d->ht[0].size/SHRINK_FACTOR, true /*fShrink*/, nullptr);\n    }\n    return DICT_OK;\n}\n\n/* Our hash table capability is a power of two */\nstatic unsigned long _dictNextPower(unsigned long size)\n{\n    unsigned long i = DICT_HT_INITIAL_SIZE;\n\n    if (size >= LONG_MAX) return LONG_MAX + 1LU;\n    while(1) {\n        if (i >= size)\n            return i;\n        i *= 2;\n    }\n}\n\n/* Returns the index of a free slot that can be populated with\n * a hash entry for the given 'key'.\n * If the key already exists, -1 is returned\n * and the optional output parameter may be filled.\n *\n * Note that if we are in the process of rehashing the hash table, the\n * index is always returned in the context of the second (new) hash table. */\nlong _dictKeyIndex(dict *d, const void *key, uint64_t hash, dictEntry **existing)\n{\n    unsigned long idx, table;\n    dictEntry *he;\n    if (existing) *existing = NULL;\n\n    /* Expand the hash table if needed */\n    if (_dictExpandIfNeeded(d) == DICT_ERR)\n        return -1;\n    for (table = 0; table <= 1; table++) {\n        idx = hash & d->ht[table].sizemask;\n        /* Search if this slot does not already contain the given key */\n        he = d->ht[table].table[idx];\n        while(he) {\n            if (key==he->key || dictCompareKeys(d, key, he->key)) {\n                if (existing) *existing = he;\n                return -1;\n            }\n            he = he->next;\n        }\n        if (!dictIsRehashing(d)) break;\n    }\n    return idx;\n}\n\nvoid dictEmpty(dict *d, void(callback)(void*)) {\n    _dictClear(d,&d->ht[0],callback);\n    _dictClear(d,&d->ht[1],callback);\n    d->rehashidx = -1;\n    d->pauserehash = 0;\n}\n\nvoid dictEnableResize(void) {\n    dict_can_resize = 1;\n}\n\nvoid dictDisableResize(void) {\n    dict_can_resize = 0;\n}\n\nuint64_t dictGetHash(dict *d, const void *key) {\n    return dictHashKey(d, key);\n}\n\n/* Finds the dictEntry reference by using pointer and pre-calculated hash.\n * oldkey is a dead pointer and should not be accessed.\n * the hash value should be provided using dictGetHash.\n * no string / key comparison is performed.\n * return value is the reference to the dictEntry if found, or NULL if not found. */\ndictEntry **dictFindEntryRefByPtrAndHash(dict *d, const void *oldptr, uint64_t hash) {\n    dictEntry *he, **heref;\n    unsigned long idx, table;\n\n    if (dictSize(d) == 0) return NULL; /* dict is empty */\n    for (table = 0; table <= 1; table++) {\n        idx = hash & d->ht[table].sizemask;\n        heref = &d->ht[table].table[idx];\n        he = *heref;\n        while(he) {\n            if (oldptr==he->key)\n                return heref;\n            heref = &he->next;\n            he = *heref;\n        }\n        if (!dictIsRehashing(d)) return NULL;\n    }\n    return NULL;\n}\n\n/* ------------------------------- Debugging ---------------------------------*/\n\n#define DICT_STATS_VECTLEN 50\nsize_t _dictGetStatsHt(char *buf, size_t bufsize, dictht *ht, int tableid) {\n    unsigned long i, slots = 0, chainlen, maxchainlen = 0;\n    unsigned long totchainlen = 0;\n    unsigned long clvector[DICT_STATS_VECTLEN];\n    size_t l = 0;\n\n    if (ht->used == 0) {\n        return snprintf(buf,bufsize,\n            \"No stats available for empty dictionaries\\n\");\n    }\n\n    /* Compute stats. */\n    for (i = 0; i < DICT_STATS_VECTLEN; i++) clvector[i] = 0;\n    for (i = 0; i < ht->size; i++) {\n        dictEntry *he;\n\n        if (ht->table[i] == NULL) {\n            clvector[0]++;\n            continue;\n        }\n        slots++;\n        /* For each hash entry on this slot... */\n        chainlen = 0;\n        he = ht->table[i];\n        while(he) {\n            chainlen++;\n            he = he->next;\n        }\n        clvector[(chainlen < DICT_STATS_VECTLEN) ? chainlen : (DICT_STATS_VECTLEN-1)]++;\n        if (chainlen > maxchainlen) maxchainlen = chainlen;\n        totchainlen += chainlen;\n    }\n\n    /* Generate human readable stats. */\n    l += snprintf(buf+l,bufsize-l,\n        \"Hash table %d stats (%s):\\n\"\n        \" table size: %lu\\n\"\n        \" number of elements: %lu\\n\"\n        \" different slots: %lu\\n\"\n        \" max chain length: %lu\\n\"\n        \" avg chain length (counted): %.02f\\n\"\n        \" avg chain length (computed): %.02f\\n\"\n        \" Chain length distribution:\\n\",\n        tableid, (tableid == 0) ? \"main hash table\" : \"rehashing target\",\n        ht->size, ht->used, slots, maxchainlen,\n        (float)totchainlen/slots, (float)ht->used/slots);\n\n    for (i = 0; i < DICT_STATS_VECTLEN-1; i++) {\n        if (clvector[i] == 0) continue;\n        if (l >= bufsize) break;\n        l += snprintf(buf+l,bufsize-l,\n            \"   %s%ld: %ld (%.02f%%)\\n\",\n            (i == DICT_STATS_VECTLEN-1)?\">= \":\"\",\n            i, clvector[i], ((float)clvector[i]/ht->size)*100);\n    }\n\n    /* Unlike snprintf(), return the number of characters actually written. */\n    if (bufsize) buf[bufsize-1] = '\\0';\n    return strlen(buf);\n}\n\nvoid dictGetStats(char *buf, size_t bufsize, dict *d) {\n    size_t l;\n    char *orig_buf = buf;\n    size_t orig_bufsize = bufsize;\n\n    l = _dictGetStatsHt(buf,bufsize,&d->ht[0],0);\n    buf += l;\n    bufsize -= l;\n    if (dictIsRehashing(d) && bufsize > 0) {\n        _dictGetStatsHt(buf,bufsize,&d->ht[1],1);\n    }\n    /* Make sure there is a NULL term at the end. */\n    if (orig_bufsize) orig_buf[orig_bufsize-1] = '\\0';\n}\n\nvoid dictForceRehash(dict *d)\n{\n    int16_t pauserehash;\n    __atomic_load(&d->pauserehash, &pauserehash, __ATOMIC_RELAXED);\n    while (pauserehash == 0 && dictIsRehashing(d)) _dictRehashStep(d);\n}\n\n/* ------------------------------- Benchmark ---------------------------------*/\n\n#ifdef REDIS_TEST\n\nuint64_t hashCallback(const void *key) {\n    return dictGenHashFunction((unsigned char*)key, strlen((char*)key));\n}\n\nint compareCallback(void *privdata, const void *key1, const void *key2) {\n    int l1,l2;\n    DICT_NOTUSED(privdata);\n\n    l1 = strlen((char*)key1);\n    l2 = strlen((char*)key2);\n    if (l1 != l2) return 0;\n    return memcmp(key1, key2, l1) == 0;\n}\n\nvoid freeCallback(void *privdata, void *val) {\n    DICT_NOTUSED(privdata);\n\n    zfree(val);\n}\n\nchar *stringFromLongLong(long long value) {\n    char buf[32];\n    int len;\n    char *s;\n\n    len = snprintf(buf,sizeof(buf),\"%lld\",value);\n    s = zmalloc(len+1);\n    memcpy(s, buf, len);\n    s[len] = '\\0';\n    return s;\n}\n\ndictType BenchmarkDictType = {\n    hashCallback,\n    NULL,\n    NULL,\n    compareCallback,\n    freeCallback,\n    NULL,\n    NULL\n};\n\n#define start_benchmark() start = timeInMilliseconds()\n#define end_benchmark(msg) do { \\\n    elapsed = timeInMilliseconds()-start; \\\n    printf(msg \": %ld items in %lld ms\\n\", count, elapsed); \\\n} while(0)\n\n/* ./redis-server test dict [<count> | --accurate] */\nint dictTest(int argc, char **argv, int accurate) {\n    long j;\n    long long start, elapsed;\n    dict *dict = dictCreate(&BenchmarkDictType,NULL);\n    long count = 0;\n\n    if (argc == 4) {\n        if (accurate) {\n            count = 5000000;\n        } else {\n            count = strtol(argv[3],NULL,10);\n        }\n    } else {\n        count = 5000;\n    }\n\n    start_benchmark();\n    for (j = 0; j < count; j++) {\n        int retval = dictAdd(dict,stringFromLongLong(j),(void*)j);\n        assert(retval == DICT_OK);\n    }\n    end_benchmark(\"Inserting\");\n    assert((long)dictSize(dict) == count);\n\n    /* Wait for rehashing. */\n    while (dictIsRehashing(dict)) {\n        dictRehashMilliseconds(dict,100);\n    }\n\n    start_benchmark();\n    for (j = 0; j < count; j++) {\n        char *key = stringFromLongLong(j);\n        dictEntry *de = dictFind(dict,key);\n        assert(de != NULL);\n        zfree(key);\n    }\n    end_benchmark(\"Linear access of existing elements\");\n\n    start_benchmark();\n    for (j = 0; j < count; j++) {\n        char *key = stringFromLongLong(j);\n        dictEntry *de = dictFind(dict,key);\n        assert(de != NULL);\n        zfree(key);\n    }\n    end_benchmark(\"Linear access of existing elements (2nd round)\");\n\n    start_benchmark();\n    for (j = 0; j < count; j++) {\n        char *key = stringFromLongLong(rand() % count);\n        dictEntry *de = dictFind(dict,key);\n        assert(de != NULL);\n        zfree(key);\n    }\n    end_benchmark(\"Random access of existing elements\");\n\n    start_benchmark();\n    for (j = 0; j < count; j++) {\n        dictEntry *de = dictGetRandomKey(dict);\n        assert(de != NULL);\n    }\n    end_benchmark(\"Accessing random keys\");\n\n    start_benchmark();\n    for (j = 0; j < count; j++) {\n        char *key = stringFromLongLong(rand() % count);\n        key[0] = 'X';\n        dictEntry *de = dictFind(dict,key);\n        assert(de == NULL);\n        zfree(key);\n    }\n    end_benchmark(\"Accessing missing\");\n\n    start_benchmark();\n    for (j = 0; j < count; j++) {\n        char *key = stringFromLongLong(j);\n        int retval = dictDelete(dict,key);\n        assert(retval == DICT_OK);\n        key[0] += 17; /* Change first number to letter. */\n        retval = dictAdd(dict,key,(void*)j);\n        assert(retval == DICT_OK);\n    }\n    end_benchmark(\"Removing and adding\");\n    dictRelease(dict);\n    return 0;\n}\n#endif\n"
  },
  {
    "path": "src/dict.h",
    "content": "/* Hash Tables Implementation.\n *\n * This file implements in-memory hash tables with insert/del/replace/find/\n * get-random-element operations. Hash tables will auto-resize if needed\n * tables of power of two in size are used, collisions are handled by\n * chaining. See the source code for more information... :)\n *\n * Copyright (c) 2006-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifdef __cplusplus\n#include <vector>\n#include <atomic>\nextern \"C\" {\n#endif\n\n#ifndef __cplusplus\n#error \"C++ Only\"\n#endif\n\n#ifndef __DICT_H\n#define __DICT_H\n\n#include \"mt19937-64.h\"\n#include <limits.h>\n#include <stdint.h>\n#include <stdlib.h>\n\n#define DICT_OK 0\n#define DICT_ERR 1\n\n/* Unused arguments generate annoying warnings... */\n#define DICT_NOTUSED(V) ((void) V)\nstruct dictAsyncRehashCtl;\n\ntypedef struct dictEntry {\n    void *key;\n    union {\n        void *val;\n        uint64_t u64;\n        int64_t s64;\n        double d;\n    } v;\n    struct dictEntry *next;\n} dictEntry;\n\ntypedef struct dictType {\n    uint64_t (*hashFunction)(const void *key);\n    void *(*keyDup)(void *privdata, const void *key);\n    void *(*valDup)(void *privdata, const void *obj);\n    int (*keyCompare)(void *privdata, const void *key1, const void *key2);\n    void (*keyDestructor)(void *privdata, void *key);\n    void (*valDestructor)(void *privdata, void *obj);\n    int (*expandAllowed)(size_t moreMem, double usedRatio);\n    void (*asyncfree)(dictAsyncRehashCtl *);\n} dictType;\n\n/* This is our hash table structure. Every dictionary has two of this as we\n * implement incremental rehashing, for the old to the new table. */\ntypedef struct dictht {\n    dictEntry **table;\n    unsigned long size;\n    unsigned long sizemask;\n    unsigned long used;\n} dictht;\n\n#ifdef __cplusplus\nstruct dictAsyncRehashCtl {\n    struct workItem {\n        dictEntry *de;\n        uint64_t hash;\n        workItem(dictEntry *de) {\n            this->de = de;\n        }\n    };\n\n    static const int c_targetQueueSize = 512;\n    dictEntry *deGCList = nullptr;\n    struct dict *dict = nullptr;\n    std::vector<workItem> queue;\n    size_t hashIdx = 0;\n    long rehashIdxBase;\n    dictAsyncRehashCtl *next = nullptr;\n    std::atomic<bool> done { false };\n    std::atomic<bool> abondon { false };\n\n    dictAsyncRehashCtl(struct dict *d, dictAsyncRehashCtl *next);\n    dictAsyncRehashCtl(const dictAsyncRehashCtl&) = delete;\n    dictAsyncRehashCtl(dictAsyncRehashCtl&&) = delete;\n    ~dictAsyncRehashCtl();\n};\n#else\nstruct  dictAsyncRehashCtl;\n#endif\n\nvoid discontinueAsyncRehash(dict *d);\n\ntypedef struct dict {\n    dictType *type;\n    void *privdata;\n    dictht ht[2];\n    long rehashidx; /* rehashing not in progress if rehashidx == -1 */\n    unsigned refcount;\n    dictAsyncRehashCtl *asyncdata;\n    int16_t pauserehash; /* If >0 rehashing is paused (<0 indicates coding error) */\n    uint8_t noshrink = false;\n\n#ifdef __cplusplus\n    dict() = default;\n    dict(dict &) = delete;  // No Copy Ctor\n\n    static void swap(dict& a, dict& b) {\n        discontinueAsyncRehash(&a);\n        discontinueAsyncRehash(&b);\n        std::swap(a.type, b.type);\n        std::swap(a.privdata, b.privdata);\n        std::swap(a.ht[0], b.ht[0]);\n        std::swap(a.ht[1], b.ht[1]);\n        std::swap(a.rehashidx, b.rehashidx);\n        // Never swap refcount - they are attached to the specific dict obj\n        std::swap(a.pauserehash, b.pauserehash);\n        std::swap(a.noshrink, b.noshrink);\n    }\n#endif\n} dict;\n\n/* If safe is set to 1 this is a safe iterator, that means, you can call\n * dictAdd, dictFind, and other functions against the dictionary even while\n * iterating. Otherwise it is a non safe iterator, and only dictNext()\n * should be called while iterating. */\ntypedef struct dictIterator {\n    dict *d;\n    long index;\n    int table, safe;\n    dictEntry *entry, *nextEntry;\n    /* unsafe iterator fingerprint for misuse detection. */\n    long long fingerprint;\n} dictIterator;\n\ntypedef void (dictScanFunction)(void *privdata, const dictEntry *de);\ntypedef void (dictScanBucketFunction)(void *privdata, dictEntry **bucketref);\n\n/* This is the initial size of every hash table */\n#define DICT_HT_INITIAL_SIZE     4\n\n/* ------------------------------- Macros ------------------------------------*/\n#define dictFreeVal(d, entry) \\\n    if ((d)->type->valDestructor) \\\n        (d)->type->valDestructor((d)->privdata, (entry)->v.val)\n\n#define dictSetVal(d, entry, _val_) do { \\\n    if ((d)->type->valDup) \\\n        (entry)->v.val = (d)->type->valDup((d)->privdata, _val_); \\\n    else \\\n        (entry)->v.val = (_val_); \\\n} while(0)\n\n#define dictSetSignedIntegerVal(entry, _val_) \\\n    do { (entry)->v.s64 = _val_; } while(0)\n\n#define dictSetUnsignedIntegerVal(entry, _val_) \\\n    do { (entry)->v.u64 = _val_; } while(0)\n\n#define dictSetDoubleVal(entry, _val_) \\\n    do { (entry)->v.d = _val_; } while(0)\n\n#define dictFreeKey(d, entry) \\\n    if ((d)->type->keyDestructor) \\\n        (d)->type->keyDestructor((d)->privdata, (entry)->key)\n\n#define dictSetKey(d, entry, _key_) do { \\\n    if ((d)->type->keyDup) \\\n        (entry)->key = (d)->type->keyDup((d)->privdata, _key_); \\\n    else \\\n        (entry)->key = (_key_); \\\n} while(0)\n\n#define dictCompareKeys(d, key1, key2) \\\n    (((d)->type->keyCompare) ? \\\n        (d)->type->keyCompare((d)->privdata, key1, key2) : \\\n        (key1) == (key2))\n\n#define dictHashKey(d, key) (d)->type->hashFunction(key)\n#define dictGetKey(he) ((he)->key)\n#define dictGetVal(he) ((he)->v.val)\n#define dictGetSignedIntegerVal(he) ((he)->v.s64)\n#define dictGetUnsignedIntegerVal(he) ((he)->v.u64)\n#define dictGetDoubleVal(he) ((he)->v.d)\n#define dictSlots(d) ((d)->ht[0].size+(d)->ht[1].size)\n#define dictSize(d) ((d)->ht[0].used+(d)->ht[1].used)\n#define dictIsRehashing(d) ((d)->rehashidx != -1)\n#define dictPauseRehashing(d) __atomic_fetch_add(&(d)->pauserehash, 1, __ATOMIC_SEQ_CST)\n#define dictResumeRehashing(d) __atomic_fetch_sub(&(d)->pauserehash, 1, __ATOMIC_SEQ_CST)\n\n/* If our unsigned long type can store a 64 bit number, use a 64 bit PRNG. */\n#if ULONG_MAX >= 0xffffffffffffffff\n#define randomULong() ((unsigned long) genrand64_int64())\n#else\n#define randomULong() random()\n#endif\n\n/* API */\ndict *dictCreate(dictType *type, void *privDataPtr);\nint dictExpand(dict *d, unsigned long size, bool fShrink = false);\nint dictTryExpand(dict *d, unsigned long size, bool fShrink);\nint dictAdd(dict *d, void *key, void *val, dictEntry **existing = nullptr);\ndictEntry *dictAddRaw(dict *d, void *key, dictEntry **existing);\ndictEntry *dictAddOrFind(dict *d, void *key);\nint dictReplace(dict *d, void *key, void *val);\nint dictDelete(dict *d, const void *key);\ndictEntry *dictUnlink(dict *ht, const void *key);\nvoid dictFreeUnlinkedEntry(dict *d, dictEntry *he);\nvoid dictRelease(dict *d);\ndictEntry * dictFind(dict *d, const void *key);\ndictEntry * dictFindWithPrev(dict *d, const void *key, uint64_t h, dictEntry ***dePrevPtr, dictht **ht, bool fShallowCompare = false);\nvoid *dictFetchValue(dict *d, const void *key);\nint dictResize(dict *d);\ndictIterator *dictGetIterator(dict *d);\ndictIterator *dictGetSafeIterator(dict *d);\ndictEntry *dictNext(dictIterator *iter);\nvoid dictReleaseIterator(dictIterator *iter);\ndictEntry *dictGetRandomKey(dict *d);\ndictEntry *dictGetFairRandomKey(dict *d);\nunsigned int dictGetSomeKeys(dict *d, dictEntry **des, unsigned int count);\nvoid dictGetStats(char *buf, size_t bufsize, dict *d);\nuint64_t dictGenHashFunction(const void *key, int len);\nuint64_t dictGenCaseHashFunction(const unsigned char *buf, int len);\nvoid dictEmpty(dict *d, void(callback)(void*));\nvoid dictEnableResize(void);\nvoid dictDisableResize(void);\nint dictRehash(dict *d, int n);\nint dictRehashMilliseconds(dict *d, int ms);\nvoid dictSetHashFunctionSeed(uint8_t *seed);\nuint8_t *dictGetHashFunctionSeed(void);\nunsigned long dictScan(dict *d, unsigned long v, dictScanFunction *fn, dictScanBucketFunction *bucketfn, void *privdata);\nuint64_t dictGetHash(dict *d, const void *key);\ndictEntry **dictFindEntryRefByPtrAndHash(dict *d, const void *oldptr, uint64_t hash);\nvoid dictForceRehash(dict *d);\nint dictMerge(dict *dst, dict *src);\n\n/* Async API */\ndictAsyncRehashCtl *dictRehashAsyncStart(dict *d, int buckets);\nbool dictRehashSomeAsync(dictAsyncRehashCtl *ctl, size_t hashes);\nvoid dictCompleteRehashAsync(dictAsyncRehashCtl *ctl, bool fFree);\nvoid dictRehashAsync(dictAsyncRehashCtl *ctl);\n\n/* Hash table types */\nextern dictType dictTypeHeapStringCopyKey;\nextern dictType dictTypeHeapStrings;\nextern dictType dictTypeHeapStringCopyKeyValue;\n\n#ifdef REDIS_TEST\nint dictTest(int argc, char *argv[], int accurate);\n#endif\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif /* __DICT_H */\n"
  },
  {
    "path": "src/endianconv.c",
    "content": "/* endinconv.c -- Endian conversions utilities.\n *\n * This functions are never called directly, but always using the macros\n * defined into endianconv.h, this way we define everything is a non-operation\n * if the arch is already little endian.\n *\n * Redis tries to encode everything as little endian (but a few things that need\n * to be backward compatible are still in big endian) because most of the\n * production environments are little endian, and we have a lot of conversions\n * in a few places because ziplists, intsets, zipmaps, need to be endian-neutral\n * even in memory, since they are serialized on RDB files directly with a single\n * write(2) without other additional steps.\n *\n * ----------------------------------------------------------------------------\n *\n * Copyright (c) 2011-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n\n#include <stdint.h>\n\n/* Toggle the 16 bit unsigned integer pointed by *p from little endian to\n * big endian */\nvoid memrev16(void *p) {\n    unsigned char *x = p, t;\n\n    t = x[0];\n    x[0] = x[1];\n    x[1] = t;\n}\n\n/* Toggle the 32 bit unsigned integer pointed by *p from little endian to\n * big endian */\nvoid memrev32(void *p) {\n    unsigned char *x = p, t;\n\n    t = x[0];\n    x[0] = x[3];\n    x[3] = t;\n    t = x[1];\n    x[1] = x[2];\n    x[2] = t;\n}\n\n/* Toggle the 64 bit unsigned integer pointed by *p from little endian to\n * big endian */\nvoid memrev64(void *p) {\n    unsigned char *x = p, t;\n\n    t = x[0];\n    x[0] = x[7];\n    x[7] = t;\n    t = x[1];\n    x[1] = x[6];\n    x[6] = t;\n    t = x[2];\n    x[2] = x[5];\n    x[5] = t;\n    t = x[3];\n    x[3] = x[4];\n    x[4] = t;\n}\n\nuint16_t intrev16(uint16_t v) {\n    memrev16(&v);\n    return v;\n}\n\nuint32_t intrev32(uint32_t v) {\n    memrev32(&v);\n    return v;\n}\n\nuint64_t intrev64(uint64_t v) {\n    memrev64(&v);\n    return v;\n}\n\n#ifdef REDIS_TEST\n#include <stdio.h>\n\n#define UNUSED(x) (void)(x)\nint endianconvTest(int argc, char *argv[], int accurate) {\n    char buf[32];\n\n    UNUSED(argc);\n    UNUSED(argv);\n    UNUSED(accurate);\n\n    snprintf(buf,sizeof(buf),\"ciaoroma\");\n    memrev16(buf);\n    printf(\"%s\\n\", buf);\n\n    snprintf(buf,sizeof(buf),\"ciaoroma\");\n    memrev32(buf);\n    printf(\"%s\\n\", buf);\n\n    snprintf(buf,sizeof(buf),\"ciaoroma\");\n    memrev64(buf);\n    printf(\"%s\\n\", buf);\n\n    return 0;\n}\n#endif\n"
  },
  {
    "path": "src/endianconv.h",
    "content": "/* See endianconv.c top comments for more information\n *\n * ----------------------------------------------------------------------------\n *\n * Copyright (c) 2011-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __ENDIANCONV_H\n#define __ENDIANCONV_H\n\n#include \"config.h\"\n#include <stdint.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\nvoid memrev16(void *p);\nvoid memrev32(void *p);\nvoid memrev64(void *p);\nuint16_t intrev16(uint16_t v);\nuint32_t intrev32(uint32_t v);\nuint64_t intrev64(uint64_t v);\n\n/* variants of the function doing the actual conversion only if the target\n * host is big endian */\n#if (BYTE_ORDER == LITTLE_ENDIAN)\n#define memrev16ifbe(p) ((void)(0))\n#define memrev32ifbe(p) ((void)(0))\n#define memrev64ifbe(p) ((void)(0))\n#define intrev16ifbe(v) (v)\n#define intrev32ifbe(v) (v)\n#define intrev64ifbe(v) (v)\n#else\n#define memrev16ifbe(p) memrev16(p)\n#define memrev32ifbe(p) memrev32(p)\n#define memrev64ifbe(p) memrev64(p)\n#define intrev16ifbe(v) intrev16(v)\n#define intrev32ifbe(v) intrev32(v)\n#define intrev64ifbe(v) intrev64(v)\n#endif\n\n/* The functions htonu64() and ntohu64() convert the specified value to\n * network byte ordering and back. In big endian systems they are no-ops. */\n#if (BYTE_ORDER == BIG_ENDIAN)\n#define htonu64(v) (v)\n#define ntohu64(v) (v)\n#else\n#define htonu64(v) intrev64(v)\n#define ntohu64(v) intrev64(v)\n#endif\n\n#ifdef REDIS_TEST\nint endianconvTest(int argc, char *argv[], int accurate);\n#endif\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n"
  },
  {
    "path": "src/evict.cpp",
    "content": "/* Maxmemory directive handling (LRU eviction and other policies).\n *\n * ----------------------------------------------------------------------------\n *\n * Copyright (c) 2009-2016, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n#include \"bio.h\"\n#include \"atomicvar.h\"\n#include <mutex>\n#include <map>\n#include <math.h>\n\n/* ----------------------------------------------------------------------------\n * Data structures\n * --------------------------------------------------------------------------*/\n\n/* To improve the quality of the LRU approximation we take a set of keys\n * that are good candidate for eviction across performEvictions() calls.\n *\n * Entries inside the eviction pool are taken ordered by idle time, putting\n * greater idle times to the right (ascending order).\n *\n * When an LFU policy is used instead, a reverse frequency indication is used\n * instead of the idle time, so that we still evict by larger value (larger\n * inverse frequency means to evict keys with the least frequent accesses).\n *\n * Empty entries have the key pointer set to NULL. */\n#define EVPOOL_SIZE 16\n#define EVPOOL_CACHED_SDS_SIZE 255\nstruct evictionPoolEntry {\n    unsigned long long idle;    /* Object idle time (inverse frequency for LFU) */\n    sds key;                    /* Key name. */\n    sds cached;                 /* Cached SDS object for key name. */\n    int dbid;                   /* Key DB number. */\n};\n\nstatic struct evictionPoolEntry *EvictionPoolLRU;\n\n/* ----------------------------------------------------------------------------\n * Implementation of eviction, aging and LRU\n * --------------------------------------------------------------------------*/\n\n/* Return the LRU clock, based on the clock resolution. This is a time\n * in a reduced-bits format that can be used to set and check the\n * object->lru field of redisObject structures. */\nunsigned int getLRUClock(void) {\n    return (mstime()/LRU_CLOCK_RESOLUTION) & LRU_CLOCK_MAX;\n}\n\n/* This function is used to obtain the current LRU clock.\n * If the current resolution is lower than the frequency we refresh the\n * LRU clock (as it should be in production servers) we return the\n * precomputed value, otherwise we need to resort to a system call. */\nunsigned int LRU_CLOCK(void) {\n    unsigned int lruclock;\n    if (1000/g_pserver->hz <= LRU_CLOCK_RESOLUTION) {\n        lruclock = g_pserver->lruclock;\n    } else {\n        lruclock = getLRUClock();\n    }\n    return lruclock;\n}\n\n/* Given an object returns the min number of milliseconds the object was never\n * requested, using an approximated LRU algorithm. */\nunsigned long long estimateObjectIdleTime(robj_roptr o) {\n    unsigned long long lruclock = LRU_CLOCK();\n    if (lruclock >= o->lru) {\n        return (lruclock - o->lru) * LRU_CLOCK_RESOLUTION;\n    } else {\n        return (lruclock + (LRU_CLOCK_MAX - o->lru)) *\n                    LRU_CLOCK_RESOLUTION;\n    }\n}\n\nunsigned long long getIdle(robj *obj, const expireEntry *e) {\n    unsigned long long idle;\n    /* Calculate the idle time according to the policy. This is called\n        * idle just because the code initially handled LRU, but is in fact\n        * just a score where an higher score means better candidate. */\n    if (g_pserver->maxmemory_policy & MAXMEMORY_FLAG_LRU) {\n        idle = (obj != nullptr) ? estimateObjectIdleTime(obj) : 0;\n    } else if (g_pserver->maxmemory_policy & MAXMEMORY_FLAG_LFU) {\n        /* When we use an LRU policy, we sort the keys by idle time\n            * so that we expire keys starting from greater idle time.\n            * However when the policy is an LFU one, we have a frequency\n            * estimation, and we want to evict keys with lower frequency\n            * first. So inside the pool we put objects using the inverted\n            * frequency subtracting the actual frequency to the maximum\n            * frequency of 255. */\n        idle = 255-LFUDecrAndReturn(obj);\n    } else if (g_pserver->maxmemory_policy == MAXMEMORY_VOLATILE_TTL) {\n        /* In this case the sooner the expire the better. */\n        if (e != nullptr)\n            idle = ULLONG_MAX - e->when();\n        else\n            idle = 0;\n    } else if (g_pserver->maxmemory_policy & MAXMEMORY_FLAG_ALLKEYS) {\n        idle = ULLONG_MAX;\n    } else {\n        serverPanic(\"Unknown eviction policy in storage eviction\");\n    }\n    return idle;\n}\n\n/* LRU approximation algorithm\n *\n * Redis uses an approximation of the LRU algorithm that runs in constant\n * memory. Every time there is a key to expire, we sample N keys (with\n * N very small, usually in around 5) to populate a pool of best keys to\n * evict of M keys (the pool size is defined by EVPOOL_SIZE).\n *\n * The N keys sampled are added in the pool of good keys to expire (the one\n * with an old access time) if they are better than one of the current keys\n * in the pool.\n *\n * After the pool is populated, the best key we have in the pool is expired.\n * However note that we don't remove keys from the pool when they are deleted\n * so the pool may contain keys that no longer exist.\n *\n * When we try to evict a key, and all the entries in the pool don't exist\n * we populate it again. This time we'll be sure that the pool has at least\n * one key that can be evicted, if there is at least one key that can be\n * evicted in the whole database. */\n\n/* Create a new eviction pool. */\nvoid evictionPoolAlloc(void) {\n    struct evictionPoolEntry *ep;\n    int j;\n\n    ep = (evictionPoolEntry*)zmalloc(sizeof(*ep)*EVPOOL_SIZE, MALLOC_LOCAL);\n    for (j = 0; j < EVPOOL_SIZE; j++) {\n        ep[j].idle = 0;\n        ep[j].key = NULL;\n        ep[j].cached = sdsnewlen(NULL,EVPOOL_CACHED_SDS_SIZE);\n        ep[j].dbid = 0;\n    }\n    EvictionPoolLRU = ep;\n}\n\nvoid processEvictionCandidate(int dbid, sds key, robj *o, const expireEntry *e, struct evictionPoolEntry *pool)\n{\n    unsigned long long idle = getIdle(o,e);\n\n    /* Insert the element inside the pool.\n        * First, find the first empty bucket or the first populated\n        * bucket that has an idle time smaller than our idle time. */\n    int k = 0;\n    while (k < EVPOOL_SIZE &&\n            pool[k].key &&\n            pool[k].idle < idle) k++;\n    if (k == 0 && pool[EVPOOL_SIZE-1].key != NULL) {\n        /* Can't insert if the element is < the worst element we have\n            * and there are no empty buckets. */\n        return;\n    } else if (k < EVPOOL_SIZE && pool[k].key == NULL) {\n        /* Inserting into empty position. No setup needed before insert. */\n    } else {\n        /* Inserting in the middle. Now k points to the first element\n            * greater than the element to insert.  */\n        if (pool[EVPOOL_SIZE-1].key == NULL) {\n            /* Free space on the right? Insert at k shifting\n                * all the elements from k to end to the right. */\n\n            /* Save SDS before overwriting. */\n            sds cached = pool[EVPOOL_SIZE-1].cached;\n            memmove(pool+k+1,pool+k,\n                sizeof(pool[0])*(EVPOOL_SIZE-k-1));\n            pool[k].cached = cached;\n        } else {\n            /* No free space on right? Insert at k-1 */\n            k--;\n            /* Shift all elements on the left of k (included) to the\n                * left, so we discard the element with smaller idle time. */\n            sds cached = pool[0].cached; /* Save SDS before overwriting. */\n            if (pool[0].key != pool[0].cached) sdsfree(pool[0].key);\n            memmove(pool,pool+1,sizeof(pool[0])*k);\n            pool[k].cached = cached;\n        }\n    }\n\n    /* Try to reuse the cached SDS string allocated in the pool entry,\n        * because allocating and deallocating this object is costly\n        * (according to the profiler, not my fantasy. Remember:\n        * premature optimization bla bla bla bla. */\n    int klen = sdslen(key);\n    if (klen > EVPOOL_CACHED_SDS_SIZE) {\n        pool[k].key = sdsdup(key);\n    } else {\n        memcpy(pool[k].cached,key,klen+1);\n        sdssetlen(pool[k].cached,klen);\n        pool[k].key = pool[k].cached;\n    }\n    pool[k].idle = idle;\n    pool[k].dbid = dbid;\n}\n\n/* This is an helper function for performEvictions(), it is used in order\n * to populate the evictionPool with a few entries every time we want to\n * expire a key. Keys with idle time bigger than one of the current\n * keys are added. Keys are always added if there are free entries.\n *\n * We insert keys on place in ascending order, so keys with the smaller\n * idle time are on the left, and keys with the higher idle time on the\n * right. */\n\nint evictionPoolPopulate(int dbid, redisDb *db, bool fVolatile, struct evictionPoolEntry *pool)\n{\n    int returnCount = 0;\n    dictEntry **samples = (dictEntry**)alloca(g_pserver->maxmemory_samples * sizeof(dictEntry*));\n    int count = dictGetSomeKeys(db->dictUnsafeKeyOnly(),samples,g_pserver->maxmemory_samples);\n    for (int j = 0; j < count; j++) {\n        robj *o = (robj*)dictGetVal(samples[j]);\n        // If the object is in second tier storage we don't need to evict it (since it already is)\n        if (o != nullptr)\n        {\n            if (!fVolatile || o->FExpires()) {\n                processEvictionCandidate(dbid, (sds)dictGetKey(samples[j]), o, &o->expire, pool);\n                ++returnCount;\n            }\n        }\n    }\n    return returnCount;\n}\n\n/* ----------------------------------------------------------------------------\n * LFU (Least Frequently Used) implementation.\n\n * We have 24 total bits of space in each object in order to implement\n * an LFU (Least Frequently Used) eviction policy, since we re-use the\n * LRU field for this purpose.\n *\n * We split the 24 bits into two fields:\n *\n *          16 bits      8 bits\n *     +----------------+--------+\n *     + Last decr time | LOG_C  |\n *     +----------------+--------+\n *\n * LOG_C is a logarithmic counter that provides an indication of the access\n * frequency. However this field must also be decremented otherwise what used\n * to be a frequently accessed key in the past, will remain ranked like that\n * forever, while we want the algorithm to adapt to access pattern changes.\n *\n * So the remaining 16 bits are used in order to store the \"decrement time\",\n * a reduced-precision Unix time (we take 16 bits of the time converted\n * in minutes since we don't care about wrapping around) where the LOG_C\n * counter is halved if it has an high value, or just decremented if it\n * has a low value.\n *\n * New keys don't start at zero, in order to have the ability to collect\n * some accesses before being trashed away, so they start at COUNTER_INIT_VAL.\n * The logarithmic increment performed on LOG_C takes care of COUNTER_INIT_VAL\n * when incrementing the key, so that keys starting at COUNTER_INIT_VAL\n * (or having a smaller value) have a very high chance of being incremented\n * on access.\n *\n * During decrement, the value of the logarithmic counter is halved if\n * its current value is greater than two times the COUNTER_INIT_VAL, otherwise\n * it is just decremented by one.\n * --------------------------------------------------------------------------*/\n\n/* Return the current time in minutes, just taking the least significant\n * 16 bits. The returned time is suitable to be stored as LDT (last decrement\n * time) for the LFU implementation. */\nunsigned long LFUGetTimeInMinutes(void) {\n    return (g_pserver->unixtime/60) & 65535;\n}\n\n/* Given an object last access time, compute the minimum number of minutes\n * that elapsed since the last access. Handle overflow (ldt greater than\n * the current 16 bits minutes time) considering the time as wrapping\n * exactly once. */\nunsigned long LFUTimeElapsed(unsigned long ldt) {\n    unsigned long now = LFUGetTimeInMinutes();\n    if (now >= ldt) return now-ldt;\n    return 65535-ldt+now;\n}\n\n/* Logarithmically increment a counter. The greater is the current counter value\n * the less likely is that it gets really implemented. Saturate it at 255. */\nuint8_t LFULogIncr(uint8_t counter) {\n    if (counter == 255) return 255;\n    double r = (double)rand()/RAND_MAX;\n    double baseval = counter - LFU_INIT_VAL;\n    if (baseval < 0) baseval = 0;\n    double p = 1.0/(baseval*g_pserver->lfu_log_factor+1);\n    if (r < p) counter++;\n    return counter;\n}\n\n/* If the object decrement time is reached decrement the LFU counter but\n * do not update LFU fields of the object, we update the access time\n * and counter in an explicit way when the object is really accessed.\n * And we will times halve the counter according to the times of\n * elapsed time than g_pserver->lfu_decay_time.\n * Return the object frequency counter.\n *\n * This function is used in order to scan the dataset for the best object\n * to fit: as we check for the candidate, we incrementally decrement the\n * counter of the scanned objects if needed. */\nunsigned long LFUDecrAndReturn(robj_roptr o) {\n    unsigned long ldt = o->lru >> 8;\n    unsigned long counter = o->lru & 255;\n    unsigned long num_periods = g_pserver->lfu_decay_time ? LFUTimeElapsed(ldt) / g_pserver->lfu_decay_time : 0;\n    if (num_periods)\n        counter = (num_periods > counter) ? 0 : counter - num_periods;\n    return counter;\n}\n\nunsigned long getClientReplicationBacklogSharedUsage(client *c);\n\n/* We don't want to count AOF buffers and slaves output buffers as\n * used memory: the eviction should use mostly data size. This function\n * returns the sum of AOF and slaves buffer. */\nsize_t freeMemoryGetNotCountedMemory(void) {\n    serverAssert(GlobalLocksAcquired());\n    size_t overhead = 0;\n    int slaves = listLength(g_pserver->slaves);\n\n    if (slaves) {\n        listIter li;\n        listNode *ln;\n\n        listRewind(g_pserver->slaves,&li);\n        while((ln = listNext(&li))) {\n            client *replica = (client*)listNodeValue(ln);\n            std::unique_lock<fastlock>(replica->lock);\n            /* we don't wish to multiple count the replication backlog shared usage */\n            overhead += (getClientOutputBufferMemoryUsage(replica) - getClientReplicationBacklogSharedUsage(replica));\n        }\n    }\n\n    /* also don't count the replication backlog memory\n     * that's where the replication clients get their memory from */\n    overhead += g_pserver->repl_backlog_size - g_pserver->repl_backlog_config_size;\n\n    if (g_pserver->aof_state != AOF_OFF) {\n        overhead += sdsalloc(g_pserver->aof_buf)+aofRewriteBufferSize();\n    }\n    return overhead;\n}\n\n/* Get the memory status from the point of view of the maxmemory directive:\n * if the memory used is under the maxmemory setting then C_OK is returned.\n * Otherwise, if we are over the memory limit, the function returns\n * C_ERR.\n *\n * The function may return additional info via reference, only if the\n * pointers to the respective arguments is not NULL. Certain fields are\n * populated only when C_ERR is returned:\n *\n *  'total'     total amount of bytes used.\n *              (Populated both for C_ERR and C_OK)\n *\n *  'logical'   the amount of memory used minus the slaves/AOF buffers.\n *              (Populated when C_ERR is returned)\n *\n *  'tofree'    the amount of memory that should be released\n *              in order to return back into the memory limits.\n *              (Populated when C_ERR is returned)\n *\n *  'level'     this usually ranges from 0 to 1, and reports the amount of\n *              memory currently used. May be > 1 if we are over the memory\n *              limit.\n *              (Populated both for C_ERR and C_OK)\n * \n *  'reason'    the reason why the memory limit was exceeded\n *              EVICT_REASON_USER: reported user memory exceeded maxmemory\n *              EVICT_REASON_SYS: available system memory under configurable threshold \n *              (Populated when C_ERR is returned)\n */\nint getMaxmemoryState(size_t *total, size_t *logical, size_t *tofree, float *level, EvictReason *reason, bool fQuickCycle, bool fPreSnapshot) {\n    size_t mem_reported, mem_used, mem_tofree;\n\n    /* Check if we are over the memory usage limit. If we are not, no need\n     * to subtract the slaves output buffers. We can just return ASAP. */\n    mem_reported = zmalloc_used_memory();\n    if (total) *total = mem_reported;\n    size_t maxmemory = g_pserver->maxmemory;\n    if (fPreSnapshot)\n        maxmemory = static_cast<size_t>(maxmemory*0.9);   // derate memory by 10% since we won't be able to free during snapshot\n    if (g_pserver->FRdbSaveInProgress() && !cserver.fForkBgSave)\n        maxmemory = static_cast<size_t>(maxmemory*1.2);\n\n    /* If available system memory is below a certain threshold, force eviction */\n    long long sys_available_mem_buffer = 0;\n    if (g_pserver->force_eviction_percent && g_pserver->cron_malloc_stats.sys_total) {\n        float available_mem_ratio = (float)(100 - g_pserver->force_eviction_percent)/100;\n        size_t min_available_mem = static_cast<size_t>(g_pserver->cron_malloc_stats.sys_total * available_mem_ratio);\n        sys_available_mem_buffer = static_cast<long>(g_pserver->cron_malloc_stats.sys_available - min_available_mem);\n        if (sys_available_mem_buffer < 0) {\n            long long mem_threshold = mem_reported + sys_available_mem_buffer;\n            maxmemory = ((long long)maxmemory < mem_threshold) ? maxmemory : static_cast<size_t>(mem_threshold);\n        }\n    }\n\n    /* We may return ASAP if there is no need to compute the level. */\n    int return_ok_asap = !maxmemory || mem_reported <= maxmemory;\n    if (return_ok_asap && !level) return C_OK;\n\n    /* Remove the size of slaves output buffers and AOF buffer from the\n     * count of used memory. */\n    mem_used = mem_reported;\n    size_t overhead = freeMemoryGetNotCountedMemory();\n    mem_used = (mem_used > overhead) ? mem_used-overhead : 0;\n\n     /* If system available memory is too low, we want to force evictions no matter\n     * what so we also offset the overhead from maxmemory. */\n    if (sys_available_mem_buffer < 0) {\n        maxmemory = (maxmemory > overhead) ? maxmemory-overhead : 0;\n    }\n\n    /* Compute the ratio of memory usage. */\n    if (level) {\n        if (!maxmemory) {\n            *level = 0;\n        } else {\n            *level = (float)mem_used / (float)maxmemory;\n        }\n    }\n\n    if (return_ok_asap) return C_OK;\n\n    /* Check if we are still over the memory limit. */\n    if (mem_used <= maxmemory) return C_OK;\n\n    /* Compute how much memory we need to free. */\n    mem_tofree = mem_used - maxmemory;\n    if (g_pserver->m_pstorageFactory && !fQuickCycle)\n    {\n        mem_tofree += static_cast<size_t>(maxmemory * 0.05); // if we have a storage provider be much more aggressive\n    }\n\n    if (logical) *logical = mem_used;\n    if (tofree) *tofree = mem_tofree;\n\n    if (reason) *reason = sys_available_mem_buffer < 0 ? EvictReason::System : EvictReason::User;\n\n    return C_ERR;\n}\n\nclass FreeMemoryLazyFree : public ICollectable\n{    \n    ssize_t m_cb = 0;\n    std::vector<std::pair<dict*, std::vector<dictEntry*>>> vecdictvecde;\n\npublic:\n    static std::atomic<int> s_clazyFreesInProgress;\n\n    FreeMemoryLazyFree() {\n        s_clazyFreesInProgress++;\n    }\n\n    FreeMemoryLazyFree(const FreeMemoryLazyFree&) = delete;\n    FreeMemoryLazyFree(FreeMemoryLazyFree&&) = default;\n\n    ~FreeMemoryLazyFree() {\n        aeAcquireLock();\n        for (auto &pair : vecdictvecde) {\n            for (auto de : pair.second) {\n                dictFreeUnlinkedEntry(pair.first, de);\n            }\n            dictRelease(pair.first);\n        }\n        aeReleaseLock();\n        --s_clazyFreesInProgress;\n    }\n\n    ssize_t addEntry(dict *d, dictEntry *de) {\n        ssize_t cbFreedNow = 0;\n        ssize_t cb = sizeof(dictEntry);\n        cb += sdsAllocSize((sds)dictGetKey(de));\n        robj *o = (robj*)dictGetVal(de);\n        switch (o->type) {\n        case OBJ_STRING:\n            cb += getStringObjectSdsUsedMemory(o)+sizeof(robj);\n            break;\n\n        default:\n            // If we don't know about it we can't accurately track the memory so free now\n            cbFreedNow = zmalloc_used_memory();\n            decrRefCount(o);\n            cbFreedNow -= zmalloc_used_memory();\n            de->v.val = nullptr;\n        }\n\n        auto itr = std::lower_bound(vecdictvecde.begin(), vecdictvecde.end(), d, \n            [](const std::pair<dict*, std::vector<dictEntry*>> &a, dict *d) -> bool {\n                return a.first < d;\n            }\n        );\n        if (itr == vecdictvecde.end() || itr->first != d) {\n            itr = vecdictvecde.insert(itr, std::make_pair(d, std::vector<dictEntry*>()));\n            __atomic_fetch_add(&d->refcount, 1, __ATOMIC_ACQ_REL);\n        }\n        serverAssert(itr->first == d);\n        itr->second.push_back(de);\n        m_cb += cb;\n        return cb + cbFreedNow;\n    }\n\n    size_t memory_queued() { return m_cb; }\n};\n\nstd::atomic<int> FreeMemoryLazyFree::s_clazyFreesInProgress {0};\n\n/* Return 1 if used memory is more than maxmemory after allocating more memory,\n * return 0 if not. Redis may reject user's requests or evict some keys if used\n * memory exceeds maxmemory, especially, when we allocate huge memory at once. */\nint overMaxmemoryAfterAlloc(size_t moremem) {\n    if (!g_pserver->maxmemory) return  0; /* No limit. */\n\n    /* Check quickly. */\n    size_t mem_used = zmalloc_used_memory();\n    if (mem_used + moremem <= g_pserver->maxmemory) return 0;\n\n    size_t overhead = freeMemoryGetNotCountedMemory();\n    mem_used = (mem_used > overhead) ? mem_used - overhead : 0;\n    return mem_used + moremem > g_pserver->maxmemory;\n}\n\n/* The evictionTimeProc is started when \"maxmemory\" has been breached and\n * could not immediately be resolved.  This will spin the event loop with short\n * eviction cycles until the \"maxmemory\" condition has resolved or there are no\n * more evictable items.  */\nstatic int isEvictionProcRunning = 0;\nstatic int evictionTimeProc(\n        struct aeEventLoop *eventLoop, long long id, void *clientData) {\n    UNUSED(eventLoop);\n    UNUSED(id);\n    UNUSED(clientData);\n    serverAssert(GlobalLocksAcquired());\n\n    if (performEvictions((bool)clientData) == EVICT_RUNNING) return 0;  /* keep evicting */\n\n    /* For EVICT_OK - things are good, no need to keep evicting.\n     * For EVICT_FAIL - there is nothing left to evict.  */\n    isEvictionProcRunning = 0;\n    return AE_NOMORE;\n}\n\n/* Check if it's safe to perform evictions.\n *   Returns 1 if evictions can be performed\n *   Returns 0 if eviction processing should be skipped\n */\nstatic int isSafeToPerformEvictions(void) {\n    /* - There must be no script in timeout condition.\n     * - Nor we are loading data right now.  */\n    if (g_pserver->shutdown_asap || g_pserver->lua_timedout || g_pserver->loading) return 0;\n\n    /* By default replicas should ignore maxmemory\n     * and just be masters exact copies. */\n    if (g_pserver->m_pstorageFactory == nullptr && listLength(g_pserver->masters) && g_pserver->repl_slave_ignore_maxmemory && !g_pserver->fActiveReplica) return 0;\n\n    /* If we have a lazy free obj pending, our amounts will be off, wait for it to go away */\n    if (FreeMemoryLazyFree::s_clazyFreesInProgress > 0) return 0;\n\n    /* When clients are paused the dataset should be static not just from the\n     * POV of clients not being able to write, but also from the POV of\n     * expires and evictions of keys not being performed. */\n    if (checkClientPauseTimeoutAndReturnIfPaused()) return 0;\n\n    return 1;\n}\n\n/* Algorithm for converting tenacity (0-100) to a time limit.  */\nstatic unsigned long evictionTimeLimitUs() {\n    serverAssert(g_pserver->maxmemory_eviction_tenacity >= 0);\n    serverAssert(g_pserver->maxmemory_eviction_tenacity <= 100);\n\n    if (g_pserver->maxmemory_eviction_tenacity <= 10) {\n        /* A linear progression from 0..500us */\n        return 50uL * g_pserver->maxmemory_eviction_tenacity;\n    }\n\n    if (g_pserver->maxmemory_eviction_tenacity < 100) {\n        /* A 15% geometric progression, resulting in a limit of ~2 min at tenacity==99  */\n        return (unsigned long)(500.0 * pow(1.15, g_pserver->maxmemory_eviction_tenacity - 10.0));\n    }\n\n    return ULONG_MAX;   /* No limit to eviction time */\n}\n\nvoid evict(redisDb *db, robj *keyobj) {\n    mstime_t eviction_latency;\n    propagateExpire(db,keyobj,g_pserver->lazyfree_lazy_eviction);\n    /* We compute the amount of memory freed by db*Delete() alone.\n    * It is possible that actually the memory needed to propagate\n    * the DEL in AOF and replication link is greater than the one\n    * we are freeing removing the key, but we can't account for\n    * that otherwise we would never exit the loop.\n    *\n    * AOF and Output buffer memory will be freed eventually so\n    * we only care about memory used by the key space. */\n    latencyStartMonitor(eviction_latency);\n    if (g_pserver->lazyfree_lazy_eviction)\n        dbAsyncDelete(db,keyobj);\n    else\n        dbSyncDelete(db,keyobj);\n    latencyEndMonitor(eviction_latency);\n    latencyAddSampleIfNeeded(\"eviction-del\",eviction_latency);\n\n    signalModifiedKey(NULL,db,keyobj);\n    notifyKeyspaceEvent(NOTIFY_EVICTED, \"evicted\",\n        keyobj, db->id);\n    decrRefCount(keyobj);\n}\n\nstatic void updateSysAvailableMemory() {\n    if (g_pserver->force_eviction_percent) {\n        g_pserver->cron_malloc_stats.sys_available = getMemAvailable();\n    }\n}\n\n/* Check that memory usage is within the current \"maxmemory\" limit.  If over\n * \"maxmemory\", attempt to free memory by evicting data (if it's safe to do so).\n *\n * It's possible for Redis to suddenly be significantly over the \"maxmemory\"\n * setting.  This can happen if there is a large allocation (like a hash table\n * resize) or even if the \"maxmemory\" setting is manually adjusted.  Because of\n * this, it's important to evict for a managed period of time - otherwise Redis\n * would become unresponsive while evicting.\n *\n * The goal of this function is to improve the memory situation - not to\n * immediately resolve it.  In the case that some items have been evicted but\n * the \"maxmemory\" limit has not been achieved, an aeTimeProc will be started\n * which will continue to evict items until memory limits are achieved or\n * nothing more is evictable.\n *\n * This should be called before execution of commands.  If EVICT_FAIL is\n * returned, commands which will result in increased memory usage should be\n * rejected.\n *\n * Returns:\n *   EVICT_OK       - memory is OK or it's not possible to perform evictions now\n *   EVICT_RUNNING  - memory is over the limit, but eviction is still processing\n *   EVICT_FAIL     - memory is over the limit, and there's nothing to evict\n * */\nint performEvictions(bool fPreSnapshot) {\n    if (!isSafeToPerformEvictions()) return EVICT_OK;\n    serverAssert(GlobalLocksAcquired());\n\n    int keys_freed = 0;\n    size_t mem_reported, mem_tofree;\n    long long mem_freed; /* May be negative */\n    mstime_t latency;\n    long long delta;\n    int slaves = listLength(g_pserver->slaves);\n    const bool fEvictToStorage = !cserver.delete_on_evict && g_pserver->db[0]->FStorageProvider();\n    int result = EVICT_FAIL;\n    int ckeysFailed = 0;\n    EvictReason evictReason;\n\n    std::unique_ptr<FreeMemoryLazyFree> splazy = std::make_unique<FreeMemoryLazyFree>();\n\n    if (getMaxmemoryState(&mem_reported,NULL,&mem_tofree,NULL,&evictReason,false,fPreSnapshot) == C_OK)\n        return EVICT_OK;\n\n    if (g_pserver->maxmemory_policy == MAXMEMORY_NO_EVICTION)\n        return EVICT_FAIL;  /* We need to free memory, but policy forbids. */\n\n    unsigned long eviction_time_limit_us = evictionTimeLimitUs();\n\n    mem_freed = 0;\n\n    latencyStartMonitor(latency);\n\n    monotime evictionTimer;\n    elapsedStart(&evictionTimer);\n\n    if (g_pserver->maxstorage && g_pserver->m_pstorageFactory != nullptr) {\n        while (g_pserver->m_pstorageFactory->totalDiskspaceUsed() >= g_pserver->maxstorage && elapsedUs(evictionTimer) < eviction_time_limit_us) {\n            redisDb *db;\n            std::vector<std::string> evictionPool;\n            robj *bestkey = nullptr;\n            redisDb *bestdb = nullptr;\n            unsigned long long bestidle = 0;\n            for (int i = 0; i < cserver.dbnum; i++) {\n                db = g_pserver->db[i];\n                evictionPool = db->getStorageCache()->getEvictionCandidates(g_pserver->maxmemory_samples);\n                for (std::string key : evictionPool) {\n                    robj *keyobj = createStringObject(key.c_str(), key.size());\n                    robj *obj = db->find(szFromObj(keyobj));\n                    if (obj != nullptr) {\n                        expireEntry *e = db->getExpire(keyobj);\n                        unsigned long long idle = getIdle(obj, e);\n\n                        if (bestkey == nullptr || bestidle < idle) {\n                            if (bestkey != nullptr)\n                                decrRefCount(bestkey);\n                            incrRefCount(keyobj);\n                            bestkey = keyobj;\n                            bestidle = idle;\n                            bestdb = db;\n                        }\n                    }\n                    decrRefCount(keyobj);\n                }\n            }\n            if (bestkey) {\n                evict(bestdb, bestkey);\n            } else {\n                break; //could not find a key to evict so stop now\n            }\n        }\n    }\n\n    if (g_pserver->maxstorage && g_pserver->m_pstorageFactory != nullptr && g_pserver->m_pstorageFactory->totalDiskspaceUsed() >= g_pserver->maxstorage)\n        goto cant_free_storage;\n\n    while (mem_freed < (long long)mem_tofree) {\n        int j, k, i;\n        static unsigned int next_db = 0;\n        sds bestkey = NULL;\n        int bestdbid;\n        redisDb *db;\n        bool fFallback = false;\n        \n        if (g_pserver->maxmemory_policy & (MAXMEMORY_FLAG_LRU|MAXMEMORY_FLAG_LFU) ||\n            g_pserver->maxmemory_policy == MAXMEMORY_VOLATILE_TTL)\n        {\n            struct evictionPoolEntry *pool = EvictionPoolLRU;\n\n            while(bestkey == NULL) {\n                unsigned long total_keys = 0, keys;\n\n                /* We don't want to make local-db choices when expiring keys,\n                 * so to start populate the eviction pool sampling keys from\n                 * every DB. */\n                for (i = 0; i < cserver.dbnum; i++) {\n                    db = g_pserver->db[i];\n                    if (g_pserver->maxmemory_policy & MAXMEMORY_FLAG_ALLKEYS)\n                    {\n                        if ((keys = db->size()) != 0) {\n                            total_keys += evictionPoolPopulate(i, db, false, pool);\n                        }\n                    }\n                    else\n                    {\n                        keys = db->expireSize();\n                        if (keys != 0)\n                            total_keys += evictionPoolPopulate(i, db, true, pool);\n                    }\n                }\n                if (!total_keys) break; /* No keys to evict. */\n\n                /* Go backward from best to worst element to evict. */\n                for (k = EVPOOL_SIZE-1; k >= 0; k--) {\n                    if (pool[k].key == NULL) {\n                        continue;\n                    } \n                    bestdbid = pool[k].dbid;\n                    sds key = nullptr;\n\n                    auto itr = g_pserver->db[pool[k].dbid]->find(pool[k].key);\n                    if (itr != nullptr && (g_pserver->maxmemory_policy & MAXMEMORY_FLAG_ALLKEYS || itr.val()->FExpires()))\n                        key = itr.key();\n\n                    /* Remove the entry from the pool. */\n                    if (pool[k].key != pool[k].cached)\n                        sdsfree(pool[k].key);\n                    pool[k].key = NULL;\n                    pool[k].idle = 0;\n\n                    /* If the key exists, is our pick. Otherwise it is\n                     * a ghost and we need to try the next element. */\n                    if (key) {\n                        bestkey = key;\n                        break;\n                    } else {\n                        /* Ghost... Iterate again. */\n                    }\n                }\n            }\n            if (bestkey == nullptr && fEvictToStorage)\n                fFallback = true;\n        }\n\n        /* volatile-random and allkeys-random policy */\n        if (g_pserver->maxmemory_policy == MAXMEMORY_ALLKEYS_RANDOM ||\n                 g_pserver->maxmemory_policy == MAXMEMORY_VOLATILE_RANDOM\n                 || fFallback)\n        {\n            /* When evicting a random key, we try to evict a key for\n             * each DB, so we use the static 'next_db' variable to\n             * incrementally visit all DBs. */\n            for (i = 0; i < cserver.dbnum; i++) {\n                j = (++next_db) % cserver.dbnum;\n                db = g_pserver->db[j];\n                if (g_pserver->maxmemory_policy == MAXMEMORY_ALLKEYS_RANDOM || fFallback)\n                {\n                    if (db->size() != 0) {\n                        auto itr = db->random_cache_threadsafe(true /*fPrimaryOnly*/);  // primary only because we can't evict a snapshot key\n                        bestkey = itr.key();\n                        bestdbid = j;\n                        break;\n                    }\n                }\n                else\n                {\n                    if (db->expireSize())\n                    {\n                        db->random_expire(&bestkey);\n                        bestdbid = j;\n                        break;\n                    }\n                }\n            }\n        }\n\n        /* Finally remove the selected key. */\n        if (bestkey) {\n            db = g_pserver->db[bestdbid];\n\n            if (fEvictToStorage)\n            {\n                // This key is in the storage so we only need to free the object\n                dictEntry *deT;\n                if (db->removeCachedValue(bestkey, &deT)) {\n                    mem_freed += splazy->addEntry(db->dictUnsafeKeyOnly(), deT);\n                    ckeysFailed = 0;\n                    g_pserver->stat_evictedkeys++;\n                }\n                else {\n                    delta = 0;\n                    ckeysFailed++;\n                    if (ckeysFailed > 1024)\n                        goto cant_free;\n                }\n            }\n            else\n            {\n                robj *keyobj = createStringObject(bestkey,sdslen(bestkey));\n                delta = (long long) zmalloc_used_memory();\n                evict(db, keyobj);\n                delta -= (long long) zmalloc_used_memory();\n                mem_freed += delta;\n                g_pserver->stat_evictedkeys++;\n            }\n            keys_freed++;\n\n            if (keys_freed % 16 == 0) {\n                /* When the memory to free starts to be big enough, we may\n                 * start spending so much time here that is impossible to\n                 * deliver data to the replicas fast enough, so we force the\n                 * transmission here inside the loop. */\n                if (slaves) flushSlavesOutputBuffers();\n\n                /* Normally our stop condition is the ability to release\n                 * a fixed, pre-computed amount of memory. However when we\n                 * are deleting objects in another thread, it's better to\n                 * check, from time to time, if we already reached our target\n                 * memory, since the \"mem_freed\" amount is computed only\n                 * across the dbAsyncDelete() call, while the thread can\n                 * release the memory all the time. */\n                if (g_pserver->lazyfree_lazy_eviction) {\n                    if (evictReason == EvictReason::System) {\n                        updateSysAvailableMemory();\n                    }\n                    if (getMaxmemoryState(NULL,NULL,NULL,NULL) == C_OK) {\n                        break;\n                    }\n                }\n\n                /* After some time, exit the loop early - even if memory limit\n                 * hasn't been reached.  If we suddenly need to free a lot of\n                 * memory, don't want to spend too much time here.  */\n                if (g_pserver->m_pstorageFactory == nullptr && elapsedUs(evictionTimer) > eviction_time_limit_us) {\n                    // We still need to free memory - start eviction timer proc\n                    if (!isEvictionProcRunning && serverTL->el != nullptr) {\n                        isEvictionProcRunning = 1;\n                        aeCreateTimeEvent(serverTL->el, 0,\n                                evictionTimeProc, (void*)fPreSnapshot, NULL);\n                    }\n                    break;\n                }\n            }\n        } else {\n            goto cant_free; /* nothing to free... */\n        }\n    }\n    /* at this point, the memory is OK, or we have reached the time limit */\n    result = (isEvictionProcRunning) ? EVICT_RUNNING : EVICT_OK;\n\n    if (splazy != nullptr && splazy->memory_queued() > 0 && !serverTL->gcEpoch.isReset()) {\n        g_pserver->garbageCollector.enqueue(serverTL->gcEpoch, std::move(splazy));\n    } \n\ncant_free:\n    if (mem_freed > 0 && evictReason == EvictReason::System) {\n        updateSysAvailableMemory();\n    }\n\n    if (g_pserver->m_pstorageFactory)\n    {\n        if (mem_reported < g_pserver->maxmemory*1.2) {\n            return EVICT_OK;    // Allow us to temporarily go over without OOMing\n        }\n    }\n\n    if (!cserver.delete_on_evict && result == EVICT_FAIL)\n    {\n        for (int idb = 0; idb < cserver.dbnum; ++idb)\n        {\n            redisDb *db = g_pserver->db[idb];\n            if (db->FStorageProvider())\n            {\n                if (db->size() != 0 && db->size(true /*fcachedOnly*/) == 0 && db->keycacheIsEnabled()) {\n                    serverLog(LL_WARNING, \"Key cache exceeds maxmemory, freeing - performance may be affected increase maxmemory if possible\");\n                    db->disableKeyCache();\n                } else if (db->size(true /*fCachedOnly*/)) {\n                    serverLog(LL_WARNING, \"Failed to evict keys, falling back to flushing entire cache.  Consider increasing maxmemory-samples.\");\n                    db->removeAllCachedValues();\n                    if (((mem_reported - zmalloc_used_memory()) + mem_freed) >= mem_tofree)\n                        result = EVICT_OK;\n                }\n            }\n        }\n    }\n\n    if (result == EVICT_FAIL) {\n        /* At this point, we have run out of evictable items.  It's possible\n         * that some items are being freed in the lazyfree thread.  Perform a\n         * short wait here if such jobs exist, but don't wait long.  */\n        if (bioPendingJobsOfType(BIO_LAZY_FREE)) {\n            usleep(eviction_time_limit_us);\n            if (getMaxmemoryState(NULL,NULL,NULL,NULL) == C_OK) {\n                result = EVICT_OK;\n            }\n        }\n    }\n\n    latencyEndMonitor(latency);\n    latencyAddSampleIfNeeded(\"eviction-cycle\",latency);\ncant_free_storage:\n    return result;\n}\n\n"
  },
  {
    "path": "src/expire.cpp",
    "content": "/* Implementation of EXPIRE (keys with fixed time to live).\n *\n * ----------------------------------------------------------------------------\n *\n * Copyright (c) 2009-2016, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n#include \"cron.h\"\n\n/* Helper function for the activeExpireCycle() function.\n * This function will try to expire the key that is stored in the hash table\n * entry 'de' of the 'expires' hash table of a Redis database.\n *\n * If the key is found to be expired, it is removed from the database and\n * 1 is returned. Otherwise no operation is performed and 0 is returned.\n *\n * When a key is expired, g_pserver->stat_expiredkeys is incremented.\n *\n * The parameter 'now' is the current time in milliseconds as is passed\n * to the function to avoid too many gettimeofday() syscalls. */\nvoid activeExpireCycleExpireFullKey(redisDb *db, const char *key) {\n    robj *keyobj = createStringObject(key,sdslen(key));\n    mstime_t expire_latency;\n\n    propagateExpire(db,keyobj,g_pserver->lazyfree_lazy_expire);\n    latencyStartMonitor(expire_latency);\n    if (g_pserver->lazyfree_lazy_expire)\n        dbAsyncDelete(db,keyobj);\n    else\n        dbSyncDelete(db,keyobj);\n    latencyEndMonitor(expire_latency);\n    latencyAddSampleIfNeeded(\"expire-del\",expire_latency);\n    notifyKeyspaceEvent(NOTIFY_EXPIRED,\n        \"expired\",keyobj,db->id);\n    signalModifiedKey(NULL, db, keyobj);\n    decrRefCount(keyobj);\n    g_pserver->stat_expiredkeys++;\n}\n\n/*-----------------------------------------------------------------------------\n * Incremental collection of expired keys.\n *\n * When keys are accessed they are expired on-access. However we need a\n * mechanism in order to ensure keys are eventually removed when expired even\n * if no access is performed on them.\n *----------------------------------------------------------------------------*/\n\n\nint activeExpireCycleExpire(redisDb *db, const char *key, expireEntry &e, long long now, size_t &tried) {\n    if (!e.FFat())\n    {\n        activeExpireCycleExpireFullKey(db, key);\n        ++tried;\n        return 1;\n    }\n\n    expireEntryFat *pfat = e.pfatentry();\n    robj *val = db->find(key);\n    int deleted = 0;\n\n    redisObjectStack objKey;\n    initStaticStringObject(objKey, (char*)key);\n\n    while (!pfat->FEmpty())\n    {\n        ++tried;\n        if (pfat->nextExpireEntry().when > now)\n            break;\n\n        // Is it the full key expiration?\n        if (pfat->nextExpireEntry().spsubkey == nullptr)\n        {\n            activeExpireCycleExpireFullKey(db, key);\n            return ++deleted;\n        }\n\n        switch (val->type)\n        {\n        case OBJ_SET:\n            if (setTypeRemove(val,pfat->nextExpireEntry().spsubkey.get())) {\n                deleted++;\n                if (setTypeSize(val) == 0) {\n                    activeExpireCycleExpireFullKey(db, key);\n                    return deleted;\n                }\n            }\n            break;\n\n        case OBJ_HASH:\n            if (hashTypeDelete(val,(sds)pfat->nextExpireEntry().spsubkey.get())) {\n                deleted++;\n                if (hashTypeLength(val) == 0) {\n                    activeExpireCycleExpireFullKey(db, key);\n                    return deleted;\n                }\n            }\n            break;\n\n        case OBJ_ZSET:\n            if (zsetDel(val,(sds)pfat->nextExpireEntry().spsubkey.get())) {\n                deleted++;\n                if (zsetLength(val) == 0) {\n                    activeExpireCycleExpireFullKey(db, key);\n                    return deleted;\n                }\n            }\n            break;\n\n        case OBJ_CRON:\n        {\n            sds keyCopy = sdsdup(key);\n            incrRefCount(val);\n            aePostFunction(g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].el, [keyCopy, val]{\n                executeCronJobExpireHook(keyCopy, val);\n                sdsfree(keyCopy);\n                decrRefCount(val);\n            }, true /*fLock*/, true /*fForceQueue*/);\n            break;\n        }\n\n        case OBJ_LIST:\n        default:\n            serverAssert(false);\n        }\n        \n        redisObjectStack objSubkey;\n        initStaticStringObject(objSubkey, (char*)pfat->nextExpireEntry().spsubkey.get());\n        propagateSubkeyExpire(db, val->type, &objKey, &objSubkey);\n        \n        pfat->popfrontExpireEntry();\n        if ((tried % ACTIVE_EXPIRE_CYCLE_SUBKEY_LOOKUPS_PER_LOOP) == 0) {\n            break;\n        }\n    }\n\n    if (pfat->FEmpty())\n    {\n        removeExpire(db, &objKey);\n    }\n\n    if (deleted)\n    {\n        switch (val->type)\n        {\n        case OBJ_SET:\n            signalModifiedKey(nullptr, db,&objKey);\n            notifyKeyspaceEvent(NOTIFY_SET,\"srem\",&objKey,db->id);\n            break;\n        }\n    }\n\n    return deleted;\n}\n\nint parseUnitString(const char *sz)\n{\n    if (strcasecmp(sz, \"s\") == 0)\n        return UNIT_SECONDS;\n    if (strcasecmp(sz, \"ms\") == 0)\n        return UNIT_MILLISECONDS;\n    return -1;\n}\n\nvoid expireMemberCore(client *c, robj *key, robj *subkey, long long basetime, long long when, int unit)\n{\n    switch (unit)\n    {\n    case UNIT_SECONDS:\n        when *= 1000;\n    case UNIT_MILLISECONDS:\n        break;\n    \n    default:\n        addReplyError(c, \"Invalid unit arg\");\n        return;\n    }\n    \n    when += basetime;\n\n    /* No key, return zero. */\n    robj *val = lookupKeyWriteOrReply(c, key, shared.czero);\n    if (val == nullptr) {\n        return;\n    }\n\n    double dblT;\n    switch (val->type)\n    {\n    case OBJ_SET:\n        if (!setTypeIsMember(val, szFromObj(subkey))) {\n            addReply(c,shared.czero);\n            return;\n        }\n        break;\n\n    case OBJ_HASH:\n        if (!hashTypeExists(val, szFromObj(subkey))) {\n            addReply(c,shared.czero);\n            return;\n        }\n        break;\n\n    case OBJ_ZSET:\n        if (zsetScore(val, szFromObj(subkey), &dblT) == C_ERR) {\n            addReply(c,shared.czero);\n            return;\n        }\n        break;\n\n    default:\n        addReplyError(c, \"object type is unsupported\");\n        return;\n    }\n\n    setExpire(c, c->db, key, subkey, when);\n    signalModifiedKey(c, c->db, key);\n    g_pserver->dirty++;\n    addReply(c, shared.cone);\n}\n\nvoid expireMemberCommand(client *c)\n{\n    long long when;\n    if (getLongLongFromObjectOrReply(c, c->argv[3], &when, NULL) != C_OK)\n        return;\n\n    if (c->argc > 5) {\n        addReplyError(c, \"Invalid number of arguments\");\n        return;\n    }\n\n    int unit = UNIT_SECONDS;\n    if (c->argc == 5) {\n        unit = parseUnitString(szFromObj(c->argv[4]));\n    }\n\n    expireMemberCore(c, c->argv[1], c->argv[2], mstime(), when, unit);\n}\n\nvoid expireMemberAtCommand(client *c)\n{\n    long long when;\n    if (getLongLongFromObjectOrReply(c, c->argv[3], &when, NULL) != C_OK)\n        return;\n\n    expireMemberCore(c, c->argv[1], c->argv[2], 0, when, UNIT_SECONDS);\n}\n\nvoid pexpireMemberAtCommand(client *c)\n{\n    long long when;\n    if (getLongLongFromObjectOrReply(c, c->argv[3], &when, NULL) != C_OK)\n        return;\n\n    expireMemberCore(c, c->argv[1], c->argv[2], 0, when, UNIT_MILLISECONDS);\n}\n\n/* Try to expire a few timed out keys. The algorithm used is adaptive and\n * will use few CPU cycles if there are few expiring keys, otherwise\n * it will get more aggressive to avoid that too much memory is used by\n * keys that can be removed from the keyspace.\n *\n * Every expire cycle tests multiple databases: the next call will start\n * again from the next db. No more than CRON_DBS_PER_CALL databases are\n * tested at every iteration.\n *\n * The function can perform more or less work, depending on the \"type\"\n * argument. It can execute a \"fast cycle\" or a \"slow cycle\". The slow\n * cycle is the main way we collect expired cycles: this happens with\n * the \"server.hz\" frequency (usually 10 hertz).\n *\n * This kind of call is used when Redis detects that timelimit_exit is\n * true, so there is more work to do, and we do it more incrementally from\n * the beforeSleep() function of the event loop.\n *\n * Expire cycle type:\n *\n * If type is ACTIVE_EXPIRE_CYCLE_FAST the function will try to run a\n * \"fast\" expire cycle that takes no longer than ACTIVE_EXPIRE_CYCLE_FAST_DURATION\n * microseconds, and is not repeated again before the same amount of time.\n *\n * If type is ACTIVE_EXPIRE_CYCLE_SLOW, that normal expire cycle is\n * executed, where the time limit is a percentage of the REDIS_HZ period\n * as specified by the ACTIVE_EXPIRE_CYCLE_SLOW_TIME_PERC define. */\n#define ACTIVE_EXPIRE_CYCLE_KEYS_PER_LOOP 20 /* Keys for each DB loop. */\n#define ACTIVE_EXPIRE_CYCLE_FAST_DURATION 1000 /* Microseconds. */\n#define ACTIVE_EXPIRE_CYCLE_SLOW_TIME_PERC 25 /* Max % of CPU to use. */\n#define ACTIVE_EXPIRE_CYCLE_ACCEPTABLE_STALE 10 /* % of stale keys after which\n                                                   we do extra efforts. */\n/*static*/ void redisDbPersistentData::activeExpireCycleCore(int type) {\n    /* Adjust the running parameters according to the configured expire\n     * effort. The default effort is 1, and the maximum configurable effort\n     * is 10. */\n    unsigned long\n    effort = g_pserver->active_expire_effort-1, /* Rescale from 0 to 9. */\n    config_keys_per_loop = ACTIVE_EXPIRE_CYCLE_KEYS_PER_LOOP +\n                           ACTIVE_EXPIRE_CYCLE_KEYS_PER_LOOP/4*effort,\n    config_cycle_fast_duration = ACTIVE_EXPIRE_CYCLE_FAST_DURATION +\n                                 ACTIVE_EXPIRE_CYCLE_FAST_DURATION/4*effort,\n    config_cycle_slow_time_perc = ACTIVE_EXPIRE_CYCLE_SLOW_TIME_PERC +\n                                  2*effort,\n    config_cycle_acceptable_stale = ACTIVE_EXPIRE_CYCLE_ACCEPTABLE_STALE-\n                                    effort;\n\n    /* This function has some global state in order to continue the work\n     * incrementally across calls. */\n    static unsigned int current_db = 0; /* Next DB to test. */\n    static int timelimit_exit = 0;      /* Time limit hit in previous call? */\n    static long long last_fast_cycle = 0; /* When last fast cycle ran. */\n\n    int j, iteration = 0;\n    int dbs_per_call = CRON_DBS_PER_CALL;\n    long long start = ustime(), timelimit, elapsed;\n\n    /* When clients are paused the dataset should be static not just from the\n     * POV of clients not being able to write, but also from the POV of\n     * expires and evictions of keys not being performed. */\n    if (checkClientPauseTimeoutAndReturnIfPaused()) return;\n\n    if (type == ACTIVE_EXPIRE_CYCLE_FAST) {\n        /* Don't start a fast cycle if the previous cycle did not exit\n         * for time limit, unless the percentage of estimated stale keys is\n         * too high. Also never repeat a fast cycle for the same period\n         * as the fast cycle total duration itself. */\n        if (!timelimit_exit &&\n            g_pserver->stat_expired_stale_perc < config_cycle_acceptable_stale)\n            return;\n\n        if (start < last_fast_cycle + (long long)config_cycle_fast_duration*2)\n            return;\n\n        last_fast_cycle = start;\n    }\n\n    /* We usually should test CRON_DBS_PER_CALL per iteration, with\n     * two exceptions:\n     *\n     * 1) Don't test more DBs than we have.\n     * 2) If last time we hit the time limit, we want to scan all DBs\n     * in this iteration, as there is work to do in some DB and we don't want\n     * expired keys to use memory for too much time. */\n    if (dbs_per_call > cserver.dbnum || timelimit_exit)\n        dbs_per_call = cserver.dbnum;\n\n    /* We can use at max 'config_cycle_slow_time_perc' percentage of CPU\n     * time per iteration. Since this function gets called with a frequency of\n     * server.hz times per second, the following is the max amount of\n     * microseconds we can spend in this function. */\n    timelimit = config_cycle_slow_time_perc*1000000/g_pserver->hz/100;\n    timelimit_exit = 0;\n    if (timelimit <= 0) timelimit = 1;\n\n    if (type == ACTIVE_EXPIRE_CYCLE_FAST)\n        timelimit = config_cycle_fast_duration; /* in microseconds. */\n\n    /* Accumulate some global stats as we expire keys, to have some idea\n     * about the number of keys that are already logically expired, but still\n     * existing inside the database. */\n    long total_sampled = 0;\n    long total_expired = 0;\n\n    for (j = 0; j < dbs_per_call && timelimit_exit == 0; j++) {\n        /* Expired and checked in a single loop. */\n        unsigned long expired, sampled;\n\n        redisDb *db = g_pserver->db[(current_db % cserver.dbnum)];\n\n        /* Increment the DB now so we are sure if we run out of time\n         * in the current DB we'll restart from the next. This allows to\n         * distribute the time evenly across DBs. */\n        current_db++;\n\n        if (g_pserver->m_pstorageFactory == nullptr) {\n            /* Continue to expire if at the end of the cycle there are still\n            * a big percentage of keys to expire, compared to the number of keys\n            * we scanned. The percentage, stored in config_cycle_acceptable_stale\n            * is not fixed, but depends on the Redis configured \"expire effort\". */\n            do {\n                unsigned long num, slots;\n                long long now, ttl_sum;\n                int ttl_samples;\n                iteration++;\n\n                /* If there is nothing to expire try next DB ASAP. */\n                if (db->expireSize() == 0) {\n                    db->avg_ttl = 0;\n                    break;\n                }\n                num = dictSize(db->m_pdict);\n                slots = dictSlots(db->m_pdict);\n                now = mstime();\n\n                /* When there are less than 1% filled slots, sampling the key\n                * space is expensive, so stop here waiting for better times...\n                * The dictionary will be resized asap. */\n                if (slots > DICT_HT_INITIAL_SIZE &&\n                    (num*100/slots < 1)) break;\n\n                /* The main collection cycle. Sample random keys among keys\n                * with an expire set, checking for expired ones. */\n                expired = 0;\n                sampled = 0;\n                ttl_sum = 0;\n                ttl_samples = 0;\n\n                if (num > config_keys_per_loop)\n                    num = config_keys_per_loop;\n\n                /* Here we access the low level representation of the hash table\n                * for speed concerns: this makes this code coupled with dict.c,\n                * but it hardly changed in ten years.\n                *\n                * Note that certain places of the hash table may be empty,\n                * so we want also a stop condition about the number of\n                * buckets that we scanned. However scanning for free buckets\n                * is very fast: we are in the cache line scanning a sequential\n                * array of NULL pointers, so we can scan a lot more buckets\n                * than keys in the same time. */\n                long max_buckets = num*20;\n                long checked_buckets = 0;\n\n                while (sampled < num && checked_buckets < max_buckets) {\n                    for (int table = 0; table < 2; table++) {\n                        if (table == 1 && !dictIsRehashing(db->m_pdict)) break;\n\n                        unsigned long idx = db->expires_cursor;\n                        idx &= db->m_pdict->ht[table].sizemask;\n                        dictEntry *de = db->m_pdict->ht[table].table[idx];\n                        long long ttl;\n\n                        /* Scan the current bucket of the current table. */\n                        checked_buckets++;\n                        while(de) {\n                            /* Get the next entry now since this entry may get\n                            * deleted. */\n                            dictEntry *e = de;\n                            robj *o = (robj*)dictGetVal(de);\n                            de = de->next;\n                            if (!o->FExpires())\n                                continue;\n\n                            expireEntry *exp = &o->expire;\n\n                            serverAssert(exp->when() > 0);\n                            ttl = exp->when()-now;\n                            size_t tried = 0;\n                            if (exp->when() <= now) {\n                                if (activeExpireCycleExpire(db,(const char*)dictGetKey(e),*exp,now,tried)) expired++;\n                                serverAssert(ttl <= 0);\n                            } else {\n                                serverAssert(ttl > 0);\n                            }\n                            if (ttl > 0) {\n                                /* We want the average TTL of keys yet\n                                * not expired. */\n                                ttl_sum += ttl;\n                                ttl_samples++;\n                            }\n                            sampled++;\n                        }\n                    }\n                    db->expires_cursor++;\n                }\n                total_expired += expired;\n                total_sampled += sampled;\n\n                /* Update the average TTL stats for this database. */\n                if (ttl_samples) {\n                    long long avg_ttl = ttl_sum/ttl_samples;\n\n                    /* Do a simple running average with a few samples.\n                    * We just use the current estimate with a weight of 2%\n                    * and the previous estimate with a weight of 98%. */\n                    if (db->avg_ttl == 0) db->avg_ttl = avg_ttl;\n                    db->avg_ttl = (db->avg_ttl/50)*49 + (avg_ttl/50);\n                }\n\n                /* We can't block forever here even if there are many keys to\n                * expire. So after a given amount of milliseconds return to the\n                * caller waiting for the other active expire cycle. */\n                if ((iteration & 0xf) == 0) { /* check once every 16 iterations. */\n                    elapsed = ustime()-start;\n                    if (elapsed > timelimit) {\n                        timelimit_exit = 1;\n                        g_pserver->stat_expired_time_cap_reached_count++;\n                        break;\n                    }\n                }\n                /* We don't repeat the cycle for the current database if there are\n                * an acceptable amount of stale keys (logically expired but yet\n                * not reclaimed). */\n            } while (sampled == 0 ||\n                    (expired*100/sampled) > config_cycle_acceptable_stale);\n        } else {\n            long prev_expired;\n            long long now = mstime();\n            size_t tried = 0;\n            std::vector<std::string> keys;\n            do {\n                prev_expired = total_expired;\n                keys = db->getStorageCache()->getExpirationCandidates(ACTIVE_EXPIRE_CYCLE_LOOKUPS_PER_LOOP);\n                for (std::string key : keys) {\n                    robj* keyobj = createStringObject(key.c_str(), key.size());\n                    db->find(szFromObj(keyobj));\n                    expireEntry *e = db->getExpire(keyobj);\n                    if (e != nullptr && e->when() < now)\n                        total_expired += activeExpireCycleExpire(db, szFromObj(keyobj), *e, now, tried);\n                    decrRefCount(keyobj);\n                }\n                total_sampled += keys.size();\n                elapsed = ustime()-start;\n            } while (keys.size() > 0 && (elapsed < timelimit) && (total_expired - prev_expired) > 0);\n\n            if (ustime()-start > timelimit) {\n                timelimit_exit = 1;\n                g_pserver->stat_expired_time_cap_reached_count++;\n            }\n        }\n    }\n\n    elapsed = ustime()-start;\n    g_pserver->stat_expire_cycle_time_used += elapsed;\n    latencyAddSampleIfNeeded(\"expire-cycle\",elapsed/1000);\n\n    /* Update our estimate of keys existing but yet to be expired.\n     * Running average with this sample accounting for 5%. */\n    double current_perc;\n    if (total_sampled) {\n        current_perc = (double)total_expired/total_sampled;\n    } else\n        current_perc = 0;\n    g_pserver->stat_expired_stale_perc = (current_perc*0.05)+\n                                     (g_pserver->stat_expired_stale_perc*0.95);\n}\n\nvoid activeExpireCycle(int type)\n{\n    runAndPropogateToReplicas(redisDbPersistentData::activeExpireCycleCore, type);\n}\n\n/*-----------------------------------------------------------------------------\n * Expires of keys created in writable slaves\n *\n * Normally slaves do not process expires: they wait the masters to synthesize\n * DEL operations in order to retain consistency. However writable slaves are\n * an exception: if a key is created in the replica and an expire is assigned\n * to it, we need a way to expire such a key, since the master does not know\n * anything about such a key.\n *\n * In order to do so, we track keys created in the replica side with an expire\n * set, and call the expireSlaveKeys() function from time to time in order to\n * reclaim the keys if they already expired.\n *\n * Note that the use case we are trying to cover here, is a popular one where\n * slaves are put in writable mode in order to compute slow operations in\n * the replica side that are mostly useful to actually read data in a more\n * processed way. Think at sets intersections in a tmp key, with an expire so\n * that it is also used as a cache to avoid intersecting every time.\n *\n * This implementation is currently not perfect but a lot better than leaking\n * the keys as implemented in 3.2.\n *----------------------------------------------------------------------------*/\n\n/* The dictionary where we remember key names and database ID of keys we may\n * want to expire from the replica. Since this function is not often used we\n * don't even care to initialize the database at startup. We'll do it once\n * the feature is used the first time, that is, when rememberSlaveKeyWithExpire()\n * is called.\n *\n * The dictionary has an SDS string representing the key as the hash table\n * key, while the value is a 64 bit unsigned integer with the bits corresponding\n * to the DB where the keys may exist set to 1. Currently the keys created\n * with a DB id > 63 are not expired, but a trivial fix is to set the bitmap\n * to the max 64 bit unsigned value when we know there is a key with a DB\n * ID greater than 63, and check all the configured DBs in such a case. */\ndict *slaveKeysWithExpire = NULL;\n\n/* Check the set of keys created by the master with an expire set in order to\n * check if they should be evicted. */\nvoid expireSlaveKeys(void) {\n    if (slaveKeysWithExpire == NULL ||\n        dictSize(slaveKeysWithExpire) == 0) return;\n\n    int cycles = 0, noexpire = 0;\n    mstime_t start = mstime();\n    while(1) {\n        dictEntry *de = dictGetRandomKey(slaveKeysWithExpire);\n        sds keyname = (sds)dictGetKey(de);\n        uint64_t dbids = dictGetUnsignedIntegerVal(de);\n        uint64_t new_dbids = 0;\n\n        /* Check the key against every database corresponding to the\n         * bits set in the value bitmap. */\n        int dbid = 0;\n        while(dbids && dbid < cserver.dbnum) {\n            if ((dbids & 1) != 0) {\n                redisDb *db = g_pserver->db[dbid];\n                auto itrDB = db->find(keyname);\n                int expired = 0;\n\n                if (itrDB != db->end() && itrDB->FExpires())\n                {\n                    if (itrDB->expire.when() < start) {\n                        size_t tried = 0;\n                        expired = activeExpireCycleExpire(g_pserver->db[dbid],itrDB.key(),itrDB->expire,start,tried);\n                    }\n                }\n\n                /* If the key was not expired in this DB, we need to set the\n                 * corresponding bit in the new bitmap we set as value.\n                 * At the end of the loop if the bitmap is zero, it means we\n                 * no longer need to keep track of this key. */\n                if (itrDB != db->end() && itrDB->FExpires() && !expired) {\n                    noexpire++;\n                    new_dbids |= (uint64_t)1 << dbid;\n                }\n            }\n            dbid++;\n            dbids >>= 1;\n        }\n\n        /* Set the new bitmap as value of the key, in the dictionary\n         * of keys with an expire set directly in the writable replica. Otherwise\n         * if the bitmap is zero, we no longer need to keep track of it. */\n        if (new_dbids)\n            dictSetUnsignedIntegerVal(de,new_dbids);\n        else\n            dictDelete(slaveKeysWithExpire,keyname);\n\n        /* Stop conditions: found 3 keys we can't expire in a row or\n         * time limit was reached. */\n        cycles++;\n        if (noexpire > 3) break;\n        if ((cycles % 64) == 0 && mstime()-start > 1) break;\n        if (dictSize(slaveKeysWithExpire) == 0) break;\n    }\n}\n\n/* Track keys that received an EXPIRE or similar command in the context\n * of a writable replica. */\nvoid rememberSlaveKeyWithExpire(redisDb *db, robj *key) {\n    if (slaveKeysWithExpire == NULL) {\n        static dictType dt = {\n            dictSdsHash,                /* hash function */\n            NULL,                       /* key dup */\n            NULL,                       /* val dup */\n            dictSdsKeyCompare,          /* key compare */\n            dictSdsDestructor,          /* key destructor */\n            NULL,                       /* val destructor */\n            NULL                        /* allow to expand */\n        };\n        slaveKeysWithExpire = dictCreate(&dt,NULL);\n    }\n    if (db->id > 63) return;\n\n    dictEntry *de = dictAddOrFind(slaveKeysWithExpire,ptrFromObj(key));\n    /* If the entry was just created, set it to a copy of the SDS string\n     * representing the key: we don't want to need to take those keys\n     * in sync with the main DB. The keys will be removed by expireSlaveKeys()\n     * as it scans to find keys to remove. */\n    if (de->key == ptrFromObj(key)) {\n        de->key = sdsdup(szFromObj(key));\n        dictSetUnsignedIntegerVal(de,0);\n    }\n\n    uint64_t dbids = dictGetUnsignedIntegerVal(de);\n    dbids |= (uint64_t)1 << db->id;\n    dictSetUnsignedIntegerVal(de,dbids);\n}\n\n/* Return the number of keys we are tracking. */\nsize_t getSlaveKeyWithExpireCount(void) {\n    if (slaveKeysWithExpire == NULL) return 0;\n    return dictSize(slaveKeysWithExpire);\n}\n\n/* Remove the keys in the hash table. We need to do that when data is\n * flushed from the g_pserver-> We may receive new keys from the master with\n * the same name/db and it is no longer a good idea to expire them.\n *\n * Note: technically we should handle the case of a single DB being flushed\n * but it is not worth it since anyway race conditions using the same set\n * of key names in a writable replica and in its master will lead to\n * inconsistencies. This is just a best-effort thing we do. */\nvoid flushSlaveKeysWithExpireList(void) {\n    if (slaveKeysWithExpire) {\n        dictRelease(slaveKeysWithExpire);\n        slaveKeysWithExpire = NULL;\n    }\n}\n\nint checkAlreadyExpired(long long when) {\n    /* EXPIRE with negative TTL, or EXPIREAT with a timestamp into the past\n     * should never be executed as a DEL when load the AOF or in the context\n     * of a slave instance.\n     *\n     * Instead we add the already expired key to the database with expire time\n     * (possibly in the past) and wait for an explicit DEL from the master. */\n    return (when <= mstime() && !g_pserver->loading && (!listLength(g_pserver->masters) || g_pserver->fActiveReplica));\n}\n\n/*-----------------------------------------------------------------------------\n * Expires Commands\n *----------------------------------------------------------------------------*/\n\n/* This is the generic command implementation for EXPIRE, PEXPIRE, EXPIREAT\n * and PEXPIREAT. Because the command second argument may be relative or absolute\n * the \"basetime\" argument is used to signal what the base time is (either 0\n * for *AT variants of the command, or the current time for relative expires).\n *\n * unit is either UNIT_SECONDS or UNIT_MILLISECONDS, and is only used for\n * the argv[2] parameter. The basetime is always specified in milliseconds. */\nvoid expireGenericCommand(client *c, long long basetime, int unit) {\n    robj *key = c->argv[1], *param = c->argv[2];\n    long long when; /* unix time in milliseconds when the key will expire. */\n\n    if (getLongLongFromObjectOrReply(c, param, &when, NULL) != C_OK)\n        return;\n    int negative_when = when < 0;\n    if (unit == UNIT_SECONDS) when *= 1000;\n    when += basetime;\n    if (((when < 0) && !negative_when) || ((when-basetime > 0) && negative_when)) {\n        /* EXPIRE allows negative numbers, but we can at least detect an\n         * overflow by either unit conversion or basetime addition. */\n        addReplyErrorFormat(c, \"invalid expire time in %s\", c->cmd->name);\n        return;\n    }\n    /* No key, return zero. */\n    if (lookupKeyWrite(c->db,key) == NULL) {\n        addReply(c,shared.czero);\n        return;\n    }\n\n    if (checkAlreadyExpired(when)) {\n        robj *aux;\n\n        int deleted = g_pserver->lazyfree_lazy_expire ? dbAsyncDelete(c->db,key) :\n                                                    dbSyncDelete(c->db,key);\n        serverAssertWithInfo(c,key,deleted);\n        g_pserver->dirty++;\n\n        /* Replicate/AOF this as an explicit DEL or UNLINK. */\n        aux = g_pserver->lazyfree_lazy_expire ? shared.unlink : shared.del;\n        rewriteClientCommandVector(c,2,aux,key);\n        signalModifiedKey(c,c->db,key);\n        notifyKeyspaceEvent(NOTIFY_GENERIC,\"del\",key,c->db->id);\n        addReply(c, shared.cone);\n        return;\n    } else {\n        setExpire(c,c->db,key,nullptr,when);\n        addReply(c,shared.cone);\n        signalModifiedKey(c,c->db,key);\n        notifyKeyspaceEvent(NOTIFY_GENERIC,\"expire\",key,c->db->id);\n        g_pserver->dirty++;\n        return;\n    }\n}\n\n/* EXPIRE key seconds */\nvoid expireCommand(client *c) {\n    expireGenericCommand(c,mstime(),UNIT_SECONDS);\n}\n\n/* EXPIREAT key time */\nvoid expireatCommand(client *c) {\n    expireGenericCommand(c,0,UNIT_SECONDS);\n}\n\n/* PEXPIRE key milliseconds */\nvoid pexpireCommand(client *c) {\n    expireGenericCommand(c,mstime(),UNIT_MILLISECONDS);\n}\n\n/* PEXPIREAT key ms_time */\nvoid pexpireatCommand(client *c) {\n    expireGenericCommand(c,0,UNIT_MILLISECONDS);\n}\n\n/* Implements TTL and PTTL */\nvoid ttlGenericCommand(client *c, int output_ms) {\n    long long expire = INVALID_EXPIRE, ttl = -1;\n\n    /* If the key does not exist at all, return -2 */\n    if (lookupKeyReadWithFlags(c->db,c->argv[1],LOOKUP_NOTOUCH) == nullptr) {\n        addReplyLongLong(c,-2);\n        return;\n    }\n\n    /* The key exists. Return -1 if it has no expire, or the actual\n        * TTL value otherwise. */\n    expireEntry *pexpire = c->db->getExpire(c->argv[1]);\n\n    if (c->argc == 2) {\n        // primary expire    \n        if (pexpire != nullptr)\n            pexpire->FGetPrimaryExpire(&expire);\n    } else if (c->argc == 3) {\n        // We want a subkey expire\n        if (pexpire && pexpire->FFat()) {\n            for (auto itr : *pexpire) {\n                if (itr.subkey() == nullptr)\n                    continue;\n                if (sdscmp((sds)itr.subkey(), szFromObj(c->argv[2])) == 0) {\n                    expire = itr.when();\n                    break;\n                }\n            }\n        }\n    } else {\n        addReplyError(c, \"Invalid arguments\");\n        return;\n    }\n\n    \n    if (expire != INVALID_EXPIRE) {\n        ttl = expire-mstime();\n        if (ttl < 0) ttl = 0;\n    }\n    if (ttl == -1) {\n        addReplyLongLong(c,-1);\n    } else {\n        addReplyLongLong(c,output_ms ? ttl : ((ttl+500)/1000));\n    }\n}\n\n/* TTL key */\nvoid ttlCommand(client *c) {\n    ttlGenericCommand(c, 0);\n}\n\n/* PTTL key */\nvoid pttlCommand(client *c) {\n    ttlGenericCommand(c, 1);\n}\n\n/* PERSIST key */\nvoid persistCommand(client *c) {\n    if (lookupKeyWrite(c->db,c->argv[1])) {\n        if (c->argc == 2) {\n            if (removeExpire(c->db,c->argv[1])) {\n                signalModifiedKey(c,c->db,c->argv[1]);\n                notifyKeyspaceEvent(NOTIFY_GENERIC,\"persist\",c->argv[1],c->db->id);\n                addReply(c,shared.cone);\n                g_pserver->dirty++;\n            } else {\n                addReply(c,shared.czero);\n            }\n        } else if (c->argc == 3) {\n            if (c->db->removeSubkeyExpire(c->argv[1], c->argv[2])) {\n                signalModifiedKey(c,c->db,c->argv[1]);\n                notifyKeyspaceEvent(NOTIFY_GENERIC,\"persist\",c->argv[1],c->db->id);\n                addReply(c,shared.cone);\n                g_pserver->dirty++;\n            } else {\n                addReply(c,shared.czero);\n            }\n        } else {\n            addReplyError(c, \"Invalid arguments\");\n        }\n    } else {\n        addReply(c,shared.czero);\n    }\n}\n\n/* TOUCH key1 [key2 key3 ... keyN] */\nvoid touchCommand(client *c) {\n    int touched = 0;\n    for (int j = 1; j < c->argc; j++)\n        if (lookupKeyRead(c->db,c->argv[j]) != nullptr) touched++;\n    addReplyLongLong(c,touched);\n}\n\nexpireEntryFat::~expireEntryFat()\n{\n    if (m_dictIndex != nullptr)\n        dictRelease(m_dictIndex);\n}\n\nexpireEntryFat::expireEntryFat(const expireEntryFat &e)\n    : m_vecexpireEntries(e.m_vecexpireEntries)\n{\n    // Note: dictExpires is not copied\n}\n\nvoid expireEntryFat::createIndex()\n{\n    serverAssert(m_dictIndex == nullptr);\n    m_dictIndex = dictCreate(&dbExpiresDictType, nullptr);\n\n    for (auto &entry : m_vecexpireEntries)\n    {\n        if (entry.spsubkey != nullptr)\n        {\n            dictEntry *de = dictAddRaw(m_dictIndex, (void*)entry.spsubkey.get(), nullptr);\n            de->v.s64 = entry.when;\n        }\n    }\n}\n\nvoid expireEntryFat::expireSubKey(const char *szSubkey, long long when)\n{\n    if (m_vecexpireEntries.size() >= INDEX_THRESHOLD && m_dictIndex == nullptr)\n        createIndex();\n\n    // First check if the subkey already has an expiration\n    if (m_dictIndex != nullptr && szSubkey != nullptr)\n    {\n        dictEntry *de = dictFind(m_dictIndex, szSubkey);\n        if (de != nullptr)\n        {\n            auto itr = std::lower_bound(m_vecexpireEntries.begin(), m_vecexpireEntries.end(), de->v.u64);\n            while (itr != m_vecexpireEntries.end() && itr->when == de->v.s64)\n            {\n                bool fFound = false;\n                if (szSubkey == nullptr && itr->spsubkey == nullptr) {\n                    fFound = true;\n                } else if (szSubkey != nullptr && itr->spsubkey != nullptr && sdscmp((sds)itr->spsubkey.get(), (sds)szSubkey) == 0) {\n                    fFound = true;\n                }\n                if (fFound) {\n                    dictDelete(m_dictIndex, szSubkey);\n                    m_vecexpireEntries.erase(itr);\n                    break;\n                }\n                ++itr;\n            }\n        }\n    }\n    else\n    {\n        for (auto &entry : m_vecexpireEntries)\n        {\n            if (szSubkey != nullptr)\n            {\n                // if this is a subkey expiry then its not a match if the expireEntry is either for the\n                //  primary key or a different subkey\n                if (entry.spsubkey == nullptr || sdscmp((sds)entry.spsubkey.get(), (sds)szSubkey) != 0)\n                    continue;\n            }\n            else\n            {\n                if (entry.spsubkey != nullptr)\n                    continue;\n            }\n            m_vecexpireEntries.erase(m_vecexpireEntries.begin() + (&entry - m_vecexpireEntries.data()));\n            break;\n        }\n    }\n    auto itrInsert = std::lower_bound(m_vecexpireEntries.begin(), m_vecexpireEntries.end(), when);\n    const char *subkey = (szSubkey) ? sdsdup(szSubkey) : nullptr;\n    auto itr = m_vecexpireEntries.emplace(itrInsert, when, subkey);\n    if (m_dictIndex && subkey) {\n        dictEntry *de = dictAddRaw(m_dictIndex, (void*)itr->spsubkey.get(), nullptr);\n        de->v.s64 = when;\n    }\n}\n\nvoid expireEntryFat::popfrontExpireEntry()\n{ \n    if (m_dictIndex != nullptr && m_vecexpireEntries.begin()->spsubkey) {\n        int res = dictDelete(m_dictIndex, (void*)m_vecexpireEntries.begin()->spsubkey.get());\n        serverAssert(res == DICT_OK);\n    }\n    m_vecexpireEntries.erase(m_vecexpireEntries.begin());\n}\n"
  },
  {
    "path": "src/expire.h",
    "content": "#pragma once\n\n/* \n * INVALID_EXPIRE is the value we set the expireEntry's m_when value to when the main key is not expired and the value we return when we try to get the expire time of a key or subkey that is not expired\n * Want this value to be LLONG_MAX however we use the most significant bit of m_when as a flag to see if the expireEntry is Fat or not so we want to ensure that it is unset hence the (& ~((1LL) << (sizeof(long long)*CHAR_BIT - 1)))\n * Eventually this number might actually end up being a valid expire time, this could cause bugs so at that time it might be a good idea to use a larger data type.\n */\n#define INVALID_EXPIRE (LLONG_MAX & ~((1LL) << (sizeof(long long)*CHAR_BIT - 1)))\n\nclass expireEntryFat\n{\n    friend class expireEntry;\n    static const int INDEX_THRESHOLD = 16;\npublic:\n    struct subexpireEntry\n    {\n        long long when;\n        std::unique_ptr<const char, void(*)(const char*)> spsubkey;\n\n        subexpireEntry(long long when, const char *subkey)\n            : when(when), spsubkey(subkey, sdsfree)\n        {}\n\n        subexpireEntry(const subexpireEntry &other)\n            : spsubkey(nullptr, sdsfree)\n        {\n            when = other.when;\n            if (other.spsubkey != nullptr)\n                spsubkey = std::unique_ptr<const char, void(*)(const char*)>((const char*)sdsdupshared(other.spsubkey.get()), sdsfree);\n        }\n\n        subexpireEntry(subexpireEntry &&) = default;\n        subexpireEntry& operator=(subexpireEntry&&) = default;\n\n        subexpireEntry& operator=(const subexpireEntry &src) {\n            when = src.when;\n            spsubkey = std::unique_ptr<const char, void(*)(const char*)>((const char*)sdsdupshared(src.spsubkey.get()), sdsfree);\n            return *this;\n        }\n\n        bool operator<(long long when) const noexcept { return this->when < when; }\n        bool operator<(const subexpireEntry &se) { return this->when < se.when; }\n    };\n\nprivate:\n    std::vector<subexpireEntry> m_vecexpireEntries;  // Note a NULL for the sds portion means the expire is for the primary key\n    dict *m_dictIndex = nullptr;\n    long long m_whenPrimary = LLONG_MAX;\n\n    void createIndex();\npublic:\n    expireEntryFat() = default;\n    expireEntryFat(const expireEntryFat &);\n    ~expireEntryFat();\n\n    long long when() const noexcept { return m_vecexpireEntries.front().when; }\n\n    bool operator<(long long when) const noexcept { return this->when() <  when; }\n\n    void expireSubKey(const char *szSubkey, long long when);\n\n    bool FGetPrimaryExpire(long long *pwhen) const {\n        if (m_whenPrimary != LLONG_MAX) {\n            *pwhen = m_whenPrimary;\n            return true;\n        }\n        return false;\n    }\n\n    bool FEmpty() const noexcept { return m_vecexpireEntries.empty(); }\n    const subexpireEntry &nextExpireEntry() const noexcept { return m_vecexpireEntries.front(); }\n    void popfrontExpireEntry();\n    const subexpireEntry &operator[](size_t idx) const { return m_vecexpireEntries[idx]; }\n    size_t size() const noexcept { return m_vecexpireEntries.size(); }\n};\n\nclass expireEntry {\n    struct {\n        uint64_t m_whenAndPtrUnion : 63,\n                fFat : 1;\n    } s;\n    static_assert(sizeof(expireEntryFat*) <= sizeof(int64_t), \"The pointer must fit in the union\");\npublic:\n    class iter\n    {\n        friend class expireEntry;\n        const expireEntry *m_pentry = nullptr;\n        size_t m_idx = 0;\n\n    public:\n        iter(const expireEntry *pentry, size_t idx)\n            : m_pentry(pentry), m_idx(idx)\n        {}\n\n        iter &operator++() { ++m_idx; return *this; }\n        \n        const char *subkey() const\n        {\n            if (m_pentry->FFat())\n                return (*m_pentry->pfatentry())[m_idx].spsubkey.get();\n            return nullptr;\n        }\n        long long when() const\n        {\n            if (m_pentry->FFat())\n                return (*m_pentry->pfatentry())[m_idx].when;\n            return m_pentry->when();\n        }\n\n        bool operator!=(const iter &other)\n        {\n            return m_idx != other.m_idx;\n        }\n\n        const iter &operator*() const { return *this; }\n    };\n\n    expireEntry() \n    {\n        s.fFat = 0;\n        s.m_whenAndPtrUnion = 0;\n    }\n\n    expireEntry(const char *subkey, long long when)\n    {\n        if (subkey != nullptr)\n        {\n            auto pfatentry = new (MALLOC_LOCAL) expireEntryFat();\n            pfatentry->expireSubKey(subkey, when);\n            s.m_whenAndPtrUnion = reinterpret_cast<long long>(pfatentry);\n            s.fFat = true;\n        }\n        else\n        {\n            s.m_whenAndPtrUnion = when;\n            s.fFat = false;\n        }\n    }\n\n    expireEntry(expireEntryFat *pfatentry)\n    {\n        assert(pfatentry != nullptr);\n        s.m_whenAndPtrUnion = reinterpret_cast<long long>(pfatentry);\n        s.fFat = true;\n    }\n\n    expireEntry(const expireEntry &e) {\n        if (e.FFat()) {\n            s.m_whenAndPtrUnion = reinterpret_cast<long long>(new expireEntryFat(*e.pfatentry()));\n            s.fFat = true;\n        } else {\n            s = e.s;\n        }\n    }\n\n    expireEntry(expireEntry &&e)\n    {\n        s = e.s;\n    }\n\n    expireEntry &operator=(expireEntry &&e)\n    {\n        if (FFat())\n            delete pfatentry();\n        s = e.s;\n        e.s.m_whenAndPtrUnion = 0;\n        e.s.fFat = false;\n        return *this;\n    }\n\n    expireEntry &operator=(expireEntry &e) {\n        if (FFat())\n            delete pfatentry();\n        if (e.FFat()) {\n            s.m_whenAndPtrUnion = reinterpret_cast<long long>(new expireEntryFat(*e.pfatentry()));\n            s.fFat = true;\n        } else {\n            s = e.s;\n        }\n        return *this;\n    }\n\n    // Duplicate the expire, note this is intended to be passed directly to setExpire\n    expireEntry duplicate() const {\n        expireEntry dst;\n        if (FFat()) {\n            auto pfatentry = new expireEntryFat(*expireEntry::pfatentry());\n            dst.s.m_whenAndPtrUnion = reinterpret_cast<long long>(pfatentry);\n            dst.s.fFat = true;\n        } else {\n            dst.s.m_whenAndPtrUnion = s.m_whenAndPtrUnion;\n            dst.s.fFat = false;\n        }\n        return dst;\n    }\n\n    void reset() {\n        if (FFat())\n            delete pfatentry();\n        s.fFat = false;\n        s.m_whenAndPtrUnion = 0;\n    }\n\n    ~expireEntry()\n    {\n        if (FFat())\n            delete pfatentry();\n    }\n\n    inline bool FFat() const noexcept { return s.fFat; }\n    expireEntryFat *pfatentry() { \n        assert(FFat()); \n        return reinterpret_cast<expireEntryFat*>(s.m_whenAndPtrUnion);\n    }\n    const expireEntryFat *pfatentry() const { \n        return const_cast<expireEntry*>(this)->pfatentry();\n    }\n\n\n    bool operator<(const expireEntry &e) const noexcept\n    { \n        return when() < e.when(); \n    }\n    bool operator<(long long when) const noexcept\n    { \n        return this->when() < when;\n    }\n\n    long long when() const noexcept\n    { \n        if (FFat())\n            return pfatentry()->when();\n        return s.m_whenAndPtrUnion;\n    }\n\n    void update(const char *subkey, long long when)\n    {\n        if (!FFat())\n        {\n            if (subkey == nullptr)\n            {\n                s.m_whenAndPtrUnion = when;\n                return;\n            }\n            else\n            {\n                // we have to upgrade to a fat entry\n                auto pfatentry = new (MALLOC_LOCAL) expireEntryFat();\n                pfatentry->expireSubKey(nullptr, s.m_whenAndPtrUnion);\n                s.m_whenAndPtrUnion = reinterpret_cast<long long>(pfatentry);\n                s.fFat = true;\n                // at this point we're fat so fall through\n            }\n        }\n        pfatentry()->expireSubKey(subkey, when);\n    }\n    \n    iter begin() const { return iter(this, 0); }\n    iter end() const\n    {\n        if (FFat())\n            return iter(this, pfatentry()->size());\n        return iter(this, 1);\n    }\n    \n    void erase(iter &itr)\n    {\n        if (!FFat())\n            throw -1;   // assert\n        pfatentry()->m_vecexpireEntries.erase(\n            pfatentry()->m_vecexpireEntries.begin() + itr.m_idx);\n    }\n\n    size_t size() const {\n        if (FFat())\n            return pfatentry()->size();\n        return 1;\n    }\n\n    bool FGetPrimaryExpire(long long *pwhen) const noexcept\n    { \n        if (FFat()) {\n            return pfatentry()->FGetPrimaryExpire(pwhen);\n        } else {\n            *pwhen = s.m_whenAndPtrUnion;\n            return true;\n        }\n    }\n\n    void *release_as_void() {\n        uint64_t whenT = s.m_whenAndPtrUnion;\n        whenT |= static_cast<uint64_t>(s.fFat) << 63;\n        s.m_whenAndPtrUnion = 0;\n        s.fFat = 0;\n        return reinterpret_cast<void*>(whenT);\n    }\n\n    static expireEntry *from_void(void **src) {\n        uintptr_t llV = reinterpret_cast<uintptr_t>(src);\n        return reinterpret_cast<expireEntry*>(llV);\n    }\n    static const expireEntry *from_void(void *const*src) {\n        uintptr_t llV = reinterpret_cast<uintptr_t>(src);\n        return reinterpret_cast<expireEntry*>(llV);\n    }\n\n    explicit operator long long() const noexcept { return when(); }\n};\nstatic_assert(sizeof(expireEntry) == sizeof(long long), \"This must fit in a long long so it can be put in a dictEntry\");\n"
  },
  {
    "path": "src/fastlock.cpp",
    "content": "/* \n * Copyright (c) 2019, John Sully <john at eqalpha dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"fmacros.h\"\n#include \"fastlock.h\"\n#include <unistd.h>\n#include <sys/syscall.h>\n#include <sys/types.h>\n#include <sched.h>\n#include <atomic>\n#include <assert.h>\n#ifdef __FreeBSD__\n#include <pthread_np.h>\n#else\n#include <pthread.h>\n#endif\n#include <limits.h>\n#include <map>\n#ifdef __linux__\n#include <linux/futex.h>\n#include <sys/sysinfo.h>\n#endif\n#include <string.h>\n#include <stdarg.h>\n#include <stdio.h>\n#include \"config.h\"\n#include \"serverassert.h\"\n\n#ifdef __APPLE__\n#include <TargetConditionals.h>\n#ifdef TARGET_OS_MAC\n/* The CLANG that ships with Mac OS doesn't have these builtins.\n    but on x86 they are just normal reads/writes anyways */\n#define __atomic_load_4(ptr, csq) (*(reinterpret_cast<const volatile uint32_t*>(ptr)))\n#define __atomic_load_2(ptr, csq) (*(reinterpret_cast<const volatile uint16_t*>(ptr)))\n\n#define __atomic_store_4(ptr, val, csq) (*(reinterpret_cast<volatile uint32_t*>(ptr)) = val)\n#endif\n#endif\n\n#ifndef UNUSED\n#define UNUSED(x) ((void)x)\n#endif\n\n#ifdef HAVE_BACKTRACE\n#include <ucontext.h>\n__attribute__((weak)) void logStackTrace(void *, int) {\n    printf(\"\\tFailed to generate stack trace\\n\");\n}\n#endif\n\nextern int g_fInCrash;\nextern int g_fTestMode;\nint g_fHighCpuPressure = false;\n\n/****************************************************\n *\n *      Implementation of a fair spinlock.  To promote fairness we\n *      use a ticket lock instead of a raw spinlock\n * \n ****************************************************/\n\n\n#if !defined(__has_feature)\n    #define __has_feature(x) 0\n#endif\n\n#ifdef __linux__\nextern \"C\" void unlock_futex(struct fastlock *lock, uint16_t ifutex);\n#endif\n\n#if __has_feature(thread_sanitizer)\n\n    /* Report that a lock has been created at address \"lock\". */\n    #define ANNOTATE_RWLOCK_CREATE(lock) \\\n        AnnotateRWLockCreate(__FILE__, __LINE__, lock)\n\n    /* Report that the lock at address \"lock\" is about to be destroyed. */\n    #define ANNOTATE_RWLOCK_DESTROY(lock) \\\n        AnnotateRWLockDestroy(__FILE__, __LINE__, lock)\n\n    /* Report that the lock at address \"lock\" has been acquired.\n       is_w=1 for writer lock, is_w=0 for reader lock. */\n    #define ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) \\\n        AnnotateRWLockAcquired(__FILE__, __LINE__, lock, is_w)\n\n    /* Report that the lock at address \"lock\" is about to be released. */\n    #define ANNOTATE_RWLOCK_RELEASED(lock, is_w) \\\n      AnnotateRWLockReleased(__FILE__, __LINE__, lock, is_w)\n\n    #if defined(DYNAMIC_ANNOTATIONS_WANT_ATTRIBUTE_WEAK)\n        #if defined(__GNUC__)\n            #define DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK __attribute__((weak))\n        #else\n            /* TODO(glider): for Windows support we may want to change this macro in order\n               to prepend __declspec(selectany) to the annotations' declarations. */\n            #error weak annotations are not supported for your compiler\n        #endif\n    #else\n        #define DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK\n    #endif\n\n    extern \"C\" {\n    void AnnotateRWLockCreate(\n        const char *file, int line,\n        const volatile void *lock) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;\n    void AnnotateRWLockDestroy(\n        const char *file, int line,\n        const volatile void *lock) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;\n    void AnnotateRWLockAcquired(\n        const char *file, int line,\n        const volatile void *lock, long is_w) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;\n    void AnnotateRWLockReleased(\n        const char *file, int line,\n        const volatile void *lock, long is_w) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;\n    }\n\n#else\n\n    #define ANNOTATE_RWLOCK_CREATE(lock)\n    #define ANNOTATE_RWLOCK_DESTROY(lock)\n    #define ANNOTATE_RWLOCK_ACQUIRED(lock, is_w)\n    #define ANNOTATE_RWLOCK_RELEASED(lock, is_w)\n\n#endif\n\nextern \"C\"  __attribute__((weak)) void _serverPanic(const char * /*file*/, int /*line*/, const char * /*msg*/, ...)\n{\n    *((char*)-1) = 'x';\n}\n\n__attribute__((weak)) void serverLog(int , const char *fmt, ...)\n{\n    va_list args;\n    va_start(args, fmt);\n    vprintf(fmt, args);\n    va_end(args);\n    printf(\"\\n\");\n}\n\nextern \"C\" pid_t gettid()\n{\n    static thread_local int pidCache = -1;\n#ifdef __linux__\n    if (pidCache == -1)\n        pidCache = syscall(SYS_gettid);\n#else\n\tif (pidCache == -1) {\n\t\tuint64_t tidT;\n#ifdef __FreeBSD__\n// Check https://github.com/ClickHouse/ClickHouse/commit/8d51824ddcb604b6f179a0216f0d32ba5612bd2e\n                tidT = pthread_getthreadid_np();\n#else\n\t\tpthread_threadid_np(nullptr, &tidT);\n#endif\n\t\tserverAssert(tidT < UINT_MAX);\n\t\tpidCache = (int)tidT;\n\t}\n#endif\n    return pidCache;\n}\n\nvoid printTrace()\n{\n#ifdef HAVE_BACKTRACE\n    serverLog(3 /*LL_WARNING*/, \"printing backtrace for thread %d\", gettid());\n    logStackTrace(nullptr, 1);\n#endif\n}\n\n\n#ifdef __linux__\nstatic int futex(volatile unsigned *uaddr, int futex_op, int val,\n    const struct timespec *timeout, int val3)\n{\n    return syscall(SYS_futex, uaddr, futex_op, val,\n                    timeout, uaddr, val3);\n}\n#endif\n\nclass DeadlockDetector\n{\n    fastlock m_lock { \"deadlock detector\" };    // destruct this first\n    std::map<pid_t, fastlock *> m_mapwait;\n\npublic:\n    void registerwait(fastlock *lock, pid_t thispid)\n    {\n        static volatile bool fInDeadlock = false;\n\n        if (lock == &m_lock || g_fInCrash)\n            return;\n        fastlock_lock(&m_lock);\n        \n        if (fInDeadlock)\n        {\n            printTrace();\n            fastlock_unlock(&m_lock);\n            return;\n        }\n\n        m_mapwait.insert(std::make_pair(thispid, lock));\n\n        // Detect cycles\n        pid_t pidCheck = thispid;\n        size_t cchecks = 0;\n        for (;;)\n        {\n            auto itr = m_mapwait.find(pidCheck);\n            if (itr == m_mapwait.end())\n                break;\n\n            __atomic_load(&itr->second->m_pidOwner, &pidCheck, __ATOMIC_RELAXED);\n            if (pidCheck == thispid)\n            {\n                // Deadlock detected, printout some debugging info and crash\n                serverLog(3 /*LL_WARNING*/, \"\\n\\n\");\n                serverLog(3 /*LL_WARNING*/, \"!!! ERROR: Deadlock detected !!!\");\n                pidCheck = thispid;\n                for (;;)\n                {\n                    auto itr = m_mapwait.find(pidCheck);\n                    serverLog(3 /* LL_WARNING */, \"\\t%d: (%p) %s\", pidCheck, itr->second, itr->second->szName);\n                    __atomic_load(&itr->second->m_pidOwner, &pidCheck, __ATOMIC_RELAXED);\n                    if (pidCheck == thispid)\n                        break;\n                }\n                // Wake All sleeping threads so they can print their callstacks\n#ifdef HAVE_BACKTRACE\n#ifdef __linux__\n                int mask = -1;\n                fInDeadlock = true;\n                fastlock_unlock(&m_lock);\n                futex(&lock->m_ticket.u, FUTEX_WAKE_BITSET_PRIVATE, INT_MAX, nullptr, mask);\n                futex(&itr->second->m_ticket.u, FUTEX_WAKE_BITSET_PRIVATE, INT_MAX, nullptr, mask);\n                sleep(2);\n                fastlock_lock(&m_lock);\n                printTrace();\n#endif\n#endif\n                serverLog(3 /*LL_WARNING*/, \"!!! KeyDB Will Now Crash !!!\");\n                _serverPanic(__FILE__, __LINE__, \"Deadlock detected\");\n            }\n\n            if (cchecks > m_mapwait.size())\n                break;  // There is a cycle but we're not in it\n            ++cchecks;\n        }\n        fastlock_unlock(&m_lock);\n    }\n\n    void clearwait(fastlock *lock, pid_t thispid)\n    {\n        if (lock == &m_lock || g_fInCrash)\n            return;\n        fastlock_lock(&m_lock);\n        m_mapwait.erase(thispid);\n        fastlock_unlock(&m_lock);\n    }\n};\n\nDeadlockDetector g_dlock;\n\nstatic_assert(sizeof(pid_t) <= sizeof(fastlock::m_pidOwner), \"fastlock::m_pidOwner not large enough\");\nuint64_t g_longwaits = 0;\n\nextern \"C\" void fastlock_panic(struct fastlock *lock)\n{\n    _serverPanic(__FILE__, __LINE__, \"fastlock lock/unlock mismatch for: %s\", lock->szName);\n}\n\nuint64_t fastlock_getlongwaitcount()\n{\n    uint64_t rval;\n    __atomic_load(&g_longwaits, &rval, __ATOMIC_RELAXED);\n    return rval;\n}\n\nextern \"C\" void fastlock_sleep(fastlock *lock, pid_t pid, unsigned wake, unsigned myticket)\n{\n    UNUSED(lock);\n    UNUSED(pid);\n    UNUSED(wake);\n    UNUSED(myticket);\n#ifdef __linux__\n    g_dlock.registerwait(lock, pid);\n    unsigned mask = (1U << (myticket % 32));\n    __atomic_fetch_or(&lock->futex, mask, __ATOMIC_ACQUIRE);\n\n    // double check the lock wasn't release between the last check and us setting the futex mask\n    uint32_t u;\n    __atomic_load(&lock->m_ticket.u, &u, __ATOMIC_ACQUIRE);\n    if ((u & 0xffff) != myticket)\n    {\n        futex(&lock->m_ticket.u, FUTEX_WAIT_BITSET_PRIVATE, wake, nullptr, mask);\n    }\n    \n    __atomic_fetch_and(&lock->futex, ~mask, __ATOMIC_RELEASE);\n    g_dlock.clearwait(lock, pid);\n#endif\n    __atomic_fetch_add(&g_longwaits, 1, __ATOMIC_RELAXED);\n}\n\nextern \"C\" void fastlock_init(struct fastlock *lock, const char *name)\n{\n    lock->m_ticket.m_active = 0;\n    lock->m_ticket.m_avail = 0;\n    lock->m_depth = 0;\n    lock->m_pidOwner = -1;\n    lock->futex = 0;\n    int cch = strlen(name);\n    cch = std::min<int>(cch, sizeof(lock->szName)-1);\n    memcpy(lock->szName, name, cch);\n    lock->szName[cch] = '\\0';\n    ANNOTATE_RWLOCK_CREATE(lock);\n}\n\n#ifndef ASM_SPINLOCK\nextern \"C\" void fastlock_lock(struct fastlock *lock, spin_worker worker)\n{\n    int pidOwner;\n    __atomic_load(&lock->m_pidOwner, &pidOwner, __ATOMIC_ACQUIRE);\n    if (pidOwner == gettid())\n    {\n        ++lock->m_depth;\n        return;\n    }\n\n    int tid = gettid();\n    unsigned myticket = __atomic_fetch_add(&lock->m_ticket.m_avail, 1, __ATOMIC_RELEASE);\n    unsigned cloops = 0;\n    ticket ticketT;\n    int fHighPressure;\n    __atomic_load(&g_fHighCpuPressure, &fHighPressure, __ATOMIC_RELAXED);\n    unsigned loopLimit = fHighPressure ? 0x10000 : 0x100000;\n\n    if (worker != nullptr) {\n        for (;;) {\n            __atomic_load(&lock->m_ticket.u, &ticketT.u, __ATOMIC_ACQUIRE);\n            if ((ticketT.u & 0xffff) == myticket)\n                break;\n            if (!worker())\n                goto LNormalLoop;\n        }\n    } else {\nLNormalLoop:\n        for (;;)\n        {\n            __atomic_load(&lock->m_ticket.u, &ticketT.u, __ATOMIC_ACQUIRE);\n            if ((ticketT.u & 0xffff) == myticket)\n                break;\n\n#if defined(__i386__) || defined(__amd64__)\n            __asm__ __volatile__ (\"pause\");\n#elif defined(__aarch64__)\n            __asm__ __volatile__ (\"yield\");\n#endif\n\n            if ((++cloops % loopLimit) == 0)\n            {\n                fastlock_sleep(lock, tid, ticketT.u, myticket);\n            }\n        }\n    }\n\n    lock->m_depth = 1;\n    __atomic_store(&lock->m_pidOwner, &tid, __ATOMIC_RELEASE);\n    ANNOTATE_RWLOCK_ACQUIRED(lock, true);\n    std::atomic_thread_fence(std::memory_order_acquire);\n}\n\nextern \"C\" int fastlock_trylock(struct fastlock *lock, int fWeak)\n{\n    int tid;\n    __atomic_load(&lock->m_pidOwner, &tid, __ATOMIC_ACQUIRE);\n    if (tid == gettid())\n    {\n        ++lock->m_depth;\n        return true;\n    }\n\n    // cheap test\n    struct ticket ticketT;\n    __atomic_load(&lock->m_ticket.u, &ticketT.u, __ATOMIC_ACQUIRE);\n    if (ticketT.m_active != ticketT.m_avail)\n        return false;\n\n    uint16_t active = ticketT.m_active;\n    uint16_t next = active + 1;\n\n    struct ticket ticket_expect { { { active, active } } };\n    struct ticket ticket_setiflocked { { { active, next } } };\n    if (__atomic_compare_exchange(&lock->m_ticket.u, &ticket_expect.u, &ticket_setiflocked.u, fWeak /*weak*/, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED))\n    {\n        lock->m_depth = 1;\n        tid = gettid();\n        __atomic_store(&lock->m_pidOwner, &tid,  __ATOMIC_RELEASE);\n        ANNOTATE_RWLOCK_ACQUIRED(lock, true);\n        return true;\n    }\n    return false;\n}\n\nextern \"C\" void fastlock_unlock(struct fastlock *lock)\n{\n    --lock->m_depth;\n    if (lock->m_depth == 0)\n    {\n        int pidT;\n        __atomic_load(&lock->m_pidOwner, &pidT, __ATOMIC_RELAXED);\n        serverAssert(pidT >= 0);  // unlock after free\n        int t = -1;\n        __atomic_store(&lock->m_pidOwner, &t, __ATOMIC_RELEASE);\n        std::atomic_thread_fence(std::memory_order_acq_rel);\n        ANNOTATE_RWLOCK_RELEASED(lock, true);\n        uint16_t activeNew = __atomic_add_fetch(&lock->m_ticket.m_active, 1, __ATOMIC_RELEASE);  // on x86 the atomic is not required here, but ASM handles that case\n#ifdef __linux__\n        unlock_futex(lock, activeNew);\n#else\n\t\tUNUSED(activeNew);\n#endif\n    }\n}\n#endif\n\n#ifdef __linux__\n#define ROL32(v, shift) ((v << shift) | (v >> (32-shift)))\nextern \"C\" void unlock_futex(struct fastlock *lock, uint16_t ifutex)\n{\n    unsigned mask = (1U << (ifutex % 32));\n    unsigned futexT;\n    \n    for (;;)\n    {\n        __atomic_load(&lock->futex, &futexT, __ATOMIC_ACQUIRE);\n        futexT &= mask;\n        if (!futexT)\n            break;\n\n        if (futex(&lock->m_ticket.u, FUTEX_WAKE_BITSET_PRIVATE, INT_MAX, nullptr, mask) == 1)\n            break;\n    }\n}\n#endif\n\nextern \"C\" void fastlock_free(struct fastlock *lock)\n{\n    // NOP\n    serverAssert((lock->m_ticket.m_active == lock->m_ticket.m_avail)                             // Assert the lock is unlocked\n        || (lock->m_pidOwner == gettid() \n            && (lock->m_ticket.m_active == static_cast<uint16_t>(lock->m_ticket.m_avail-1U))));  // OR we own the lock and nobody else is waiting\n    lock->m_pidOwner = -2;  // sentinal value indicating free\n    ANNOTATE_RWLOCK_DESTROY(lock);\n}\n\n\nbool fastlock::fOwnLock()\n{\n    int tid;\n    __atomic_load(&m_pidOwner, &tid, __ATOMIC_RELAXED);\n    return gettid() == tid;\n}\n\nint fastlock_unlock_recursive(struct fastlock *lock)\n{\n    int rval = lock->m_depth;\n    lock->m_depth = 1;\n    fastlock_unlock(lock);\n    return rval;\n}\n\nvoid fastlock_lock_recursive(struct fastlock *lock, int nesting)\n{\n    fastlock_lock(lock);\n    lock->m_depth = nesting;\n}\n\nvoid fastlock_auto_adjust_waits()\n{\n#ifdef __linux__\n    struct sysinfo sysinf;\n    int fHighPressurePrev, fHighPressureNew;\n    __atomic_load(&g_fHighCpuPressure, &fHighPressurePrev, __ATOMIC_RELAXED);\n    fHighPressureNew = fHighPressurePrev;\n    memset(&sysinf, 0, sizeof sysinf);\n    if (!sysinfo(&sysinf)) {\n        auto avgCoreLoad = sysinf.loads[0] / get_nprocs();\n        int fHighPressureNew = (avgCoreLoad > ((1 << SI_LOAD_SHIFT) * 0.9));\n        __atomic_store(&g_fHighCpuPressure, &fHighPressureNew, __ATOMIC_RELEASE);\n        if (fHighPressureNew)\n            serverLog(!fHighPressurePrev ?  3 /*LL_WARNING*/ : 1 /* LL_VERBOSE */, \"NOTICE: Detuning locks due to high load per core: %.2f%%\", avgCoreLoad / (double)(1 << SI_LOAD_SHIFT)*100.0);\n    }\n\n    if (!fHighPressureNew && fHighPressurePrev) {\n        serverLog(3 /*LL_WARNING*/, \"NOTICE: CPU pressure reduced\");\n    }\n#else\n    g_fHighCpuPressure = g_fTestMode;\n#endif\n}\n"
  },
  {
    "path": "src/fastlock.h",
    "content": "#pragma once\n#include <inttypes.h>\n#include <stddef.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\ntypedef int (*spin_worker)();\n\n/* Begin C API */\nstruct fastlock;\nvoid fastlock_init(struct fastlock *lock, const char *name);\n#ifdef __cplusplus\nvoid fastlock_lock(struct fastlock *lock, spin_worker worker = nullptr);\n#else\nvoid fastlock_lock(struct fastlock *lock, spin_worker worker);\n#endif\nint fastlock_trylock(struct fastlock *lock, int fWeak);\nvoid fastlock_unlock(struct fastlock *lock);\nvoid fastlock_free(struct fastlock *lock);\nint fastlock_unlock_recursive(struct fastlock *lock);\nvoid fastlock_lock_recursive(struct fastlock *lock, int nesting);\nvoid fastlock_auto_adjust_waits();\n\nuint64_t fastlock_getlongwaitcount();   // this is a global value\n\n/* End C API */\n#ifdef __cplusplus\n}\n#endif\n\n#pragma GCC diagnostic push\n#pragma GCC diagnostic ignored \"-Wpedantic\"\nstruct ticket\n{\n    union\n    {\n        struct\n        {\n            uint16_t m_active;\n            uint16_t m_avail;\n        };\n        unsigned u;\n    };\n};\n#pragma GCC diagnostic pop\n\nstruct fastlock\n{\n    volatile int m_pidOwner;\n    volatile int m_depth;\n    char szName[56];\n    /* Volatile data on seperate cache line */\n    volatile struct ticket m_ticket;\n    unsigned futex;\n    char padding[56];   // ensure ticket and futex are on their own independent cache line\n\n#ifdef __cplusplus\n    fastlock(const char *name)\n    {\n        fastlock_init(this, name);\n    }\n\n    inline void lock(spin_worker worker = nullptr)\n    {\n        fastlock_lock(this, worker);\n    }\n\n    inline bool try_lock(bool fWeak = false)\n    {\n        return !!fastlock_trylock(this, fWeak);\n    }\n\n    inline void unlock()\n    {\n        fastlock_unlock(this);\n    }\n\n    int unlock_recursive()\n    {\n        return fastlock_unlock_recursive(this);\n    }\n\n    void lock_recursive(int nesting)\n    {\n        fastlock_lock_recursive(this, nesting);\n    }\n\n    bool fOwnLock();   // true if this thread owns the lock, NOTE: not 100% reliable, use for debugging only\n#endif\n};\n\n#ifdef __cplusplus\nstatic_assert(offsetof(struct fastlock, m_ticket) == 64, \"ensure padding is correct\");\n#endif\n\n"
  },
  {
    "path": "src/fastlock_x64.asm",
    "content": ".intel_syntax noprefix\n.text\n\n.extern gettid\n.extern fastlock_sleep\n.extern g_fHighCpuPressure\n\n#\tThis is the first use of assembly in this codebase, a valid question is WHY?\n#\tThe spinlock we implement here is performance critical, and simply put GCC\n#\temits awful code.  The original C code is left in fastlock.cpp for reference\n#\tand x-plat.\n\n.ALIGN 16\n.global fastlock_lock\n.type   fastlock_lock,@function\nfastlock_lock:\n\t.cfi_startproc\n\t.cfi_def_cfa rsp, 8\n\t# RDI points to the struct:\n\t#\tint32_t m_pidOwner\n\t#\tint32_t m_depth\n\t# [rdi+64] ...\n\t#\tuint16_t active\n\t#\tuint16_t avail\n\t#\n\t# RSI points to a spin function to call, or NULL\n\t\n\t# First get our TID and put it in ecx\n\tsub rsp, 24\t\t\t\t# We only use 16 bytes, but we also need the stack aligned\n\t.cfi_adjust_cfa_offset 24\n\tmov [rsp], rdi\t\t\t# we need our struct pointer (also balance the stack for the call)\n\tmov [rsp+8], rsi\t\t# backup the spin  function\n\t\n\tcall gettid             # get our thread ID (TLS is nasty in ASM so don't bother inlining)\n\tmov esi, eax            # back it up in esi\n\n\tmov rdi, [rsp]          # Restore spin struct\n\tmov r8, [rsp+8]         # restore the function (in a different register)\n\tadd rsp, 24\n\t.cfi_adjust_cfa_offset -24\n\n\tcmp [rdi], esi          # Is the TID we got back the owner of the lock?\n\tje .LLocked             # Don't spin in that case\n\n\tmov r9d, 0x1000         #\t1000h is set so we overflow on the 1024*1024'th iteration (like the C code)\n\tmov eax, [rip+g_fHighCpuPressure]\n\ttest eax, eax\n\tjz .LNoTestMode\n\tmov r9d, 0x10000\n.LNoTestMode:\n\n\txor eax, eax            # eliminate partial register dependency\n\tinc eax                 # we want to add one\n\tlock xadd [rdi+66], ax  # do the xadd, ax contains the value before the addition\n\t# ax now contains the ticket\n\t# OK Start the wait loop\n\txor ecx, ecx\n\ttest r8, r8\n\tjnz .LLoopFunction\n.ALIGN 16\n.LLoop:\n\tmov edx, [rdi+64]\n\tcmp dx, ax              # is our ticket up?\n\tje .LLocked             # leave the loop\n\tpause\n\tadd ecx, r9d            # Have we been waiting a long time? (oflow if we have)\n\tjnc .LLoop              # If so, give up our timeslice to someone who's doing real work\n\t# Like the compiler, you're probably thinking: \"Hey! I should take these pushs out of the loop\"\n\t#\tBut the compiler doesn't know that we rarely hit this, and when we do we know the lock is\n\t#\ttaking a long time to be released anyways.  We optimize for the common case of short\n\t#\tlock intervals.  That's why we're using a spinlock in the first place\n\t# If we get here we're going to sleep in the kernel with a futex\n\tpush rdi\n\tpush rsi\n\tpush rax\n\t.cfi_adjust_cfa_offset 24\n\t# Setup the syscall args\n\n                            # rdi ARG1 futex (already in rdi)\n\t                        # rsi ARG2 tid (already in esi)\n                            # rdx ARG3 ticketT.u (already in edx)\n\tmov ecx, eax            # rcx ARG4 myticket\n\tcall fastlock_sleep\n\t# cleanup and continue\n\tpop rax\n\tpop rsi\n\tpop rdi\n\t.cfi_adjust_cfa_offset -24\n\txor ecx, ecx            # Reset our loop counter\n\tjmp .LLoop              # Get back in the game\n.ALIGN 16\n.LLocked:\n\tmov [rdi], esi          # lock->m_pidOwner = gettid()\n\tinc dword ptr [rdi+4]   # lock->m_depth++\n\tret\n\n.LLoopFunction:\n\tsub rsp, 40\n\t.cfi_adjust_cfa_offset 40\n\txor ecx, ecx\n\tmov [rsp], rcx\n\tmov [rsp+8], r8\n\tmov [rsp+16], rdi\n\tmov [rsp+24], rsi\n\tmov [rsp+32], eax\n.LLoopFunctionCore:\n\tmov edx, [rdi+64]\n\tcmp dx, ax\n\tje .LExitLoopFunction\n\tmov r8, [rsp+8]\n\tcall r8\n\ttest eax, eax\n\tjz .LExitLoopFunctionForNormal\n\tmov eax, [rsp+32]    # restore clobbered eax\n\tmov rdi, [rsp+16]\n\tjmp .LLoopFunctionCore\n.LExitLoopFunction:\n\tmov rsi, [rsp+24]\n\tadd rsp, 40\n\t.cfi_adjust_cfa_offset -40\n\tjmp .LLocked\n.LExitLoopFunctionForNormal:\n\txor ecx, ecx\n\tmov rdi, [rsp+16]\n\tmov rsi, [rsp+24]\n\tmov eax, [rsp+32]\n\tadd rsp, 40\n\t.cfi_adjust_cfa_offset -40\n\tjmp .LLoop\n.cfi_endproc\n\n.ALIGN 16\n.global fastlock_trylock\n.type   fastlock_trylock,@function\nfastlock_trylock:\n\t# RDI points to the struct:\n\t#\tint32_t m_pidOwner\n\t#\tint32_t m_depth\n\t# [rdi+64] ...\n\t#\tuint16_t active\n\t#\tuint16_t avail\n\t\n\t# First get our TID and put it in ecx\n\tpush rdi                # we need our struct pointer (also balance the stack for the call)\n\tcall gettid             # get our thread ID (TLS is nasty in ASM so don't bother inlining)\n\tmov esi, eax            # back it up in esi\n\tpop rdi                 # get our pointer back\n\n\tcmp [rdi], esi          # Is the TID we got back the owner of the lock?\n\tje .LRecursive          # Don't spin in that case\n\n\tmov eax, [rdi+64]       # get both active and avail counters\n\tmov ecx, eax            # duplicate in ecx\n\tror ecx, 16             # swap upper and lower 16-bits\n\tcmp eax, ecx            # are the upper and lower 16-bits the same?\n\tjnz .LAlreadyLocked     #\tIf not return failure\n\n\t# at this point we know eax+ecx have [avail][active] and they are both the same\n\tadd ecx, 0x10000            # increment avail, ecx is now our wanted value\n\tlock cmpxchg [rdi+64], ecx  #\tIf rdi still contains the value in eax, put in ecx (inc avail)\n\tjnz .LAlreadyLocked         # If Z is not set then someone locked it while we were preparing\n\txor eax, eax\n\tinc eax                     # return SUCCESS! (eax=1)\n\tmov [rdi], esi              # lock->m_pidOwner = gettid()\n\tmov dword ptr [rdi+4], eax  # lock->m_depth = 1\n\tret\n.ALIGN 16\n.LRecursive:\n\txor eax, eax\n\tinc eax                 # return SUCCESS! (eax=1)\n\tinc dword ptr [rdi+4]   # lock->m_depth++\n\tret\n.ALIGN 16\n.LAlreadyLocked:\n\txor eax, eax            # return 0\n\tret\n\n.ALIGN 16\n.global fastlock_unlock\n.type   fastlock_unlock,@function\nfastlock_unlock:\n\t# RDI points to the struct:\n\t#\tint32_t m_pidOwner\n\t#\tint32_t m_depth\n\t# [rdi+64] ...\n\t#\tuint16_t active\n\t#\tuint16_t avail\n\tsub dword ptr [rdi+4], 1     # decrement m_depth, don't use dec because it partially writes the flag register and we don't know its state\n\tjnz .LDone                   # if depth is non-zero this is a recursive unlock, and we still hold it\n\tmov dword ptr [rdi], -1      # pidOwner = -1 (we don't own it anymore)\n\tmov esi, [rdi+64]            # get current active (this one)\n\tinc esi                      # bump it to the next thread\n\tsfence                       # ensure whatever was written in the lock is visible\n\tmov word ptr [rdi+64], si    # give up our ticket (note: lock is not required here because the spinlock itself guards this variable)\n\t# At this point the lock is removed, however we must wake up any pending futexs\n\tmov rdx, [rdi+64]            # load the futex mask, note we intentionally also read the ticket we just wrote to ensure this is ordered with the above mov\n\tshr rdx, 32                  # isolate the mask\n\tbt edx, esi                  # is the next thread waiting on a futex?\n\tjc unlock_futex              # unlock the futex if necessary\n\tret                          # if not we're done.\n.ALIGN 16\n.LDone:\n\tjs fastlock_panic            # panic if we made m_depth negative\n\tret\n"
  },
  {
    "path": "src/fmacros.h",
    "content": "/*\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef _REDIS_FMACRO_H\n#define _REDIS_FMACRO_H\n\n#define _DEFAULT_SOURCE 1\n\n#if defined(__linux__)\n#define _GNU_SOURCE 1\n#define _DEFAULT_SOURCE 1\n\n#include <linux/version.h>\n#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,19,0)\n#define HAVE_SO_INCOMING_CPU 1\n#endif\n#endif  // __linux__\n\n#if defined(_AIX)\n#define _ALL_SOURCE\n#endif\n\n#if defined(__linux__) || defined(__OpenBSD__)\n#define _XOPEN_SOURCE 700\n/*\n * On NetBSD, _XOPEN_SOURCE undefines _NETBSD_SOURCE and\n * thus hides inet_aton etc.\n */\n#elif !defined(__NetBSD__)\n#define _XOPEN_SOURCE\n#endif\n\n#if defined(__sun)\n#define _POSIX_C_SOURCE 199506L\n#endif\n\n#ifndef _LARGEFILE_SOURCE\n#define _LARGEFILE_SOURCE\n#endif\n#define _FILE_OFFSET_BITS 64\n\n#ifdef __linux__\n/* features.h uses the defines above to set feature specific defines.  */\n#include <features.h>\n#endif\n\n#endif\n"
  },
  {
    "path": "src/gc.h",
    "content": "#pragma once\n#include <vector>\n#include <assert.h>\n#include <unordered_set>\n#include <list>\n\nstruct ICollectable\n{\n    virtual ~ICollectable() {}\n    bool FWillFreeChildDebug() { return false; }\n};\n\ntemplate<typename T>\nclass GarbageCollector\n{\n    struct EpochHolder\n    {\n        uint64_t tstamp;\n        std::unique_ptr<std::vector<std::unique_ptr<T>>> m_spvecObjs;\n\n        EpochHolder() {\n            m_spvecObjs = std::make_unique<std::vector<std::unique_ptr<T>>>();\n        }\n\n        // Support move operators\n        EpochHolder(EpochHolder &&other) = default;\n        EpochHolder &operator=(EpochHolder &&) = default;\n\n\n\n        bool operator<(uint64_t tstamp) const\n        {\n            return this->tstamp < tstamp;\n        }\n\n        bool operator==(uint64_t tstamp) const\n        {\n            return this->tstamp == tstamp;\n        }\n    };\n\npublic:\n    ~GarbageCollector() {\n        // Silence TSAN errors\n        m_lock.lock();\n    }\n\n    uint64_t startEpoch()\n    {\n        std::unique_lock<fastlock> lock(m_lock);\n        ++m_epochNext;\n        m_setepochOutstanding.insert(m_epochNext);\n        return m_epochNext;\n    }\n\n    void shutdown()\n    {\n        std::unique_lock<fastlock> lock(m_lock);\n        m_listepochs.clear();\n        m_setepochOutstanding.clear();\n    }\n\n    bool empty() const\n    {\n        std::unique_lock<fastlock> lock(m_lock);\n        return m_listepochs.empty();\n    }\n\n    void endEpoch(uint64_t epoch, bool fNoFree = false)\n    {\n        std::unique_lock<fastlock> lock(m_lock);\n        serverAssert(m_setepochOutstanding.find(epoch) != m_setepochOutstanding.end());\n        bool fMinElement = *std::min_element(m_setepochOutstanding.begin(), m_setepochOutstanding.end());\n        m_setepochOutstanding.erase(epoch);\n        if (fNoFree)\n            return;\n        std::list<EpochHolder> listclean;\n        \n        // No outstanding epochs?\n        if (m_setepochOutstanding.empty())\n        {\n            listclean = std::move(m_listepochs);  // Everything goes!\n        }\n        else\n        {\n            uint64_t minepoch = *std::min_element(m_setepochOutstanding.begin(), m_setepochOutstanding.end());\n            if (minepoch == 0)\n                return; // No available epochs to free\n\n            // Clean any epochs available (after the lock)\n            for (auto itr = m_listepochs.begin(); itr != m_listepochs.end(); /* itr incremented in loop*/)\n            {\n                auto &e = *itr;\n                auto itrNext = itr;\n                ++itrNext;\n                if (e < minepoch)\n                {\n                    listclean.emplace_back(std::move(e));\n                    m_listepochs.erase(itr);\n                }\n                itr = itrNext;\n            }\n\n            assert(listclean.empty() || fMinElement);\n        }\n\n        lock.unlock();  // don't hold it for the potentially long delete of vecclean\n    }\n\n    void enqueue(uint64_t epoch, std::unique_ptr<T> &&sp)\n    {\n        std::unique_lock<fastlock> lock(m_lock);\n        serverAssert(m_setepochOutstanding.find(epoch) != m_setepochOutstanding.end());\n        serverAssert(sp->FWillFreeChildDebug() == false);\n\n        auto itr = std::find(m_listepochs.begin(), m_listepochs.end(), m_epochNext+1);\n        if (itr == m_listepochs.end())\n        {\n            EpochHolder e;\n            e.tstamp = m_epochNext+1;\n            e.m_spvecObjs->push_back(std::move(sp));\n            m_listepochs.emplace_back(std::move(e));\n        }\n        else\n        {\n            itr->m_spvecObjs->push_back(std::move(sp));\n        }\n    }\n\nprivate:\n    mutable fastlock m_lock { \"Garbage Collector\"};\n\n    std::list<EpochHolder> m_listepochs;\n    std::unordered_set<uint64_t> m_setepochOutstanding;\n    uint64_t m_epochNext = 0;\n};\n"
  },
  {
    "path": "src/geo.cpp",
    "content": "/*\n * Copyright (c) 2014, Matt Stancliff <matt@genges.com>.\n * Copyright (c) 2015-2016, Salvatore Sanfilippo <antirez@gmail.com>.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"geo.h\"\n#include \"geohash_helper.h\"\n#include \"debugmacro.h\"\n#include \"pqsort.h\"\n\n/* Things exported from t_zset.c only for geo.c, since it is the only other\n * part of Redis that requires close zset introspection. */\nunsigned char *zzlFirstInRange(unsigned char *zl, zrangespec *range);\nint zslValueLteMax(double value, zrangespec *spec);\n\n/* ====================================================================\n * This file implements the following commands:\n *\n *   - geoadd - add coordinates for value to geoset\n *   - georadius - search radius by coordinates in geoset\n *   - georadiusbymember - search radius based on geoset member position\n * ==================================================================== */\n\n/* ====================================================================\n * geoArray implementation\n * ==================================================================== */\n\n/* Create a new array of geoPoints. */\ngeoArray *geoArrayCreate(void) {\n    geoArray *ga = (geoArray*)zmalloc(sizeof(*ga), MALLOC_SHARED);\n    /* It gets allocated on first geoArrayAppend() call. */\n    ga->array = NULL;\n    ga->buckets = 0;\n    ga->used = 0;\n    return ga;\n}\n\n/* Add a new entry and return its pointer so that the caller can populate\n * it with data. */\ngeoPoint *geoArrayAppend(geoArray *ga) {\n    if (ga->used == ga->buckets) {\n        ga->buckets = (ga->buckets == 0) ? 8 : ga->buckets*2;\n        ga->array = (geoPoint*)zrealloc(ga->array,sizeof(geoPoint)*ga->buckets, MALLOC_SHARED);\n    }\n    geoPoint *gp = ga->array+ga->used;\n    ga->used++;\n    return gp;\n}\n\n/* Destroy a geoArray created with geoArrayCreate(). */\nvoid geoArrayFree(geoArray *ga) {\n    size_t i;\n    for (i = 0; i < ga->used; i++) sdsfree(ga->array[i].member);\n    zfree(ga->array);\n    zfree(ga);\n}\n\n/* ====================================================================\n * Helpers\n * ==================================================================== */\nint decodeGeohash(double bits, double *xy) {\n    GeoHashBits hash = { (uint64_t)bits, GEO_STEP_MAX };\n    return geohashDecodeToLongLatWGS84(hash, xy);\n}\n\n/* Input Argument Helper */\n/* Take a pointer to the latitude arg then use the next arg for longitude.\n * On parse error C_ERR is returned, otherwise C_OK. */\nint extractLongLatOrReply(client *c, robj **argv, double *xy) {\n    int i;\n    for (i = 0; i < 2; i++) {\n        if (getDoubleFromObjectOrReply(c, argv[i], xy + i, NULL) !=\n            C_OK) {\n            return C_ERR;\n        }\n    }\n    if (xy[0] < GEO_LONG_MIN || xy[0] > GEO_LONG_MAX ||\n        xy[1] < GEO_LAT_MIN  || xy[1] > GEO_LAT_MAX) {\n        addReplyErrorFormat(c,\n            \"-ERR invalid longitude,latitude pair %f,%f\\r\\n\",xy[0],xy[1]);\n        return C_ERR;\n    }\n    return C_OK;\n}\n\n/* Input Argument Helper */\n/* Decode lat/long from a zset member's score.\n * Returns C_OK on successful decoding, otherwise C_ERR is returned. */\nint longLatFromMember(robj_roptr zobj, robj *member, double *xy) {\n    double score = 0;\n\n    if (zsetScore(zobj, szFromObj(member), &score) == C_ERR) return C_ERR;\n    if (!decodeGeohash(score, xy)) return C_ERR;\n    return C_OK;\n}\n\n/* Check that the unit argument matches one of the known units, and returns\n * the conversion factor to meters (you need to divide meters by the conversion\n * factor to convert to the right unit).\n *\n * If the unit is not valid, an error is reported to the client, and a value\n * less than zero is returned. */\ndouble extractUnitOrReply(client *c, robj *unit) {\n    char *u = szFromObj(unit);\n\n    if (!strcmp(u, \"m\")) {\n        return 1;\n    } else if (!strcmp(u, \"km\")) {\n        return 1000;\n    } else if (!strcmp(u, \"ft\")) {\n        return 0.3048;\n    } else if (!strcmp(u, \"mi\")) {\n        return 1609.34;\n    } else {\n        addReplyError(c,\n            \"unsupported unit provided. please use m, km, ft, mi\");\n        return -1;\n    }\n}\n\n/* Input Argument Helper.\n * Extract the distance from the specified two arguments starting at 'argv'\n * that should be in the form: <number> <unit>, and return C_OK or C_ERR means success or failure\n * *conversions is populated with the coefficient to use in order to convert meters to the unit.*/\nint extractDistanceOrReply(client *c, robj **argv,\n                              double *conversion, double *radius) {\n    double distance;\n    if (getDoubleFromObjectOrReply(c, argv[0], &distance,\n                                   \"need numeric radius\") != C_OK) {\n        return C_ERR;\n    }\n\n    if (distance < 0) {\n        addReplyError(c,\"radius cannot be negative\");\n        return C_ERR;\n    }\n    if (radius) *radius = distance;\n\n    double to_meters = extractUnitOrReply(c,argv[1]);\n    if (to_meters < 0) {\n        return C_ERR;\n    }\n\n    if (conversion) *conversion = to_meters;\n    return C_OK;\n}\n\n/* Input Argument Helper.\n * Extract height and width from the specified three arguments starting at 'argv'\n * that should be in the form: <number> <number> <unit>, and return C_OK or C_ERR means success or failure\n * *conversions is populated with the coefficient to use in order to convert meters to the unit.*/\nint extractBoxOrReply(client *c, robj **argv, double *conversion,\n                         double *width, double *height) {\n    double h, w;\n    if ((getDoubleFromObjectOrReply(c, argv[0], &w, \"need numeric width\") != C_OK) ||\n        (getDoubleFromObjectOrReply(c, argv[1], &h, \"need numeric height\") != C_OK)) {\n        return C_ERR;\n    }\n\n    if (h < 0 || w < 0) {\n        addReplyError(c, \"height or width cannot be negative\");\n        return C_ERR;\n    }\n    if (height) *height = h;\n    if (width) *width = w;\n\n    double to_meters = extractUnitOrReply(c,argv[2]);\n    if (to_meters < 0) {\n        return C_ERR;\n    }\n\n    if (conversion) *conversion = to_meters;\n    return C_OK;\n}\n\n/* The default addReplyDouble has too much accuracy.  We use this\n * for returning location distances. \"5.2145 meters away\" is nicer\n * than \"5.2144992818115 meters away.\" We provide 4 digits after the dot\n * so that the returned value is decently accurate even when the unit is\n * the kilometer. */\nvoid addReplyDoubleDistance(client *c, double d) {\n    char dbuf[128];\n    int dlen = snprintf(dbuf, sizeof(dbuf), \"%.4f\", d);\n    addReplyBulkCBuffer(c, dbuf, dlen);\n}\n\n/* Helper function for geoGetPointsInRange(): given a sorted set score\n * representing a point, and a GeoShape, appends this entry as a geoPoint\n * into the specified geoArray only if the point is within the search area.\n *\n * returns C_OK if the point is included, or REIDS_ERR if it is outside. */\nint geoAppendIfWithinShape(geoArray *ga, GeoShape *shape, double score, sds member) {\n    double distance = 0, xy[2];\n\n    if (!decodeGeohash(score,xy)) return C_ERR; /* Can't decode. */\n    /* Note that geohashGetDistanceIfInRadiusWGS84() takes arguments in\n     * reverse order: longitude first, latitude later. */\n    if (shape->type == CIRCULAR_TYPE) {\n        if (!geohashGetDistanceIfInRadiusWGS84(shape->xy[0], shape->xy[1], xy[0], xy[1],\n                                               shape->t.radius*shape->conversion, &distance)) return C_ERR;\n    } else if (shape->type == RECTANGLE_TYPE) {\n        if (!geohashGetDistanceIfInRectangle(shape->t.r.width * shape->conversion,\n                                             shape->t.r.height * shape->conversion,\n                                             shape->xy[0], shape->xy[1], xy[0], xy[1], &distance))\n            return C_ERR;\n    }\n\n    /* Append the new element. */\n    geoPoint *gp = geoArrayAppend(ga);\n    gp->longitude = xy[0];\n    gp->latitude = xy[1];\n    gp->dist = distance;\n    gp->member = member;\n    gp->score = score;\n    return C_OK;\n}\n\n/* Query a Redis sorted set to extract all the elements between 'min' and\n * 'max', appending them into the array of geoPoint structures 'gparray'.\n * The command returns the number of elements added to the array.\n *\n * Elements which are farest than 'radius' from the specified 'x' and 'y'\n * coordinates are not included.\n *\n * The ability of this function to append to an existing set of points is\n * important for good performances because querying by radius is performed\n * using multiple queries to the sorted set, that we later need to sort\n * via qsort. Similarly we need to be able to reject points outside the search\n * radius area ASAP in order to allocate and process more points than needed. */\nint geoGetPointsInRange(robj_roptr zobj, double min, double max, GeoShape *shape, geoArray *ga, unsigned long limit) {\n    /* minex 0 = include min in range; maxex 1 = exclude max in range */\n    /* That's: min <= val < max */\n    zrangespec range = { min, max, 0, 1 };\n    size_t origincount = ga->used;\n    sds member;\n\n    if (zobj->encoding == OBJ_ENCODING_ZIPLIST) {\n        unsigned char *zl = (unsigned char*)zobj->m_ptr;\n        unsigned char *eptr, *sptr;\n        unsigned char *vstr = NULL;\n        unsigned int vlen = 0;\n        long long vlong = 0;\n        double score = 0;\n\n        if ((eptr = zzlFirstInRange(zl, &range)) == NULL) {\n            /* Nothing exists starting at our min.  No results. */\n            return 0;\n        }\n\n        sptr = ziplistNext(zl, eptr);\n        while (eptr) {\n            score = zzlGetScore(sptr);\n\n            /* If we fell out of range, break. */\n            if (!zslValueLteMax(score, &range))\n                break;\n\n            /* We know the element exists. ziplistGet should always succeed */\n            ziplistGet(eptr, &vstr, &vlen, &vlong);\n            member = (vstr == NULL) ? sdsfromlonglong(vlong) :\n                                      sdsnewlen(vstr,vlen);\n            if (geoAppendIfWithinShape(ga,shape,score,member)\n                == C_ERR) sdsfree(member);\n            if (ga->used && limit && ga->used >= limit) break;\n            zzlNext(zl, &eptr, &sptr);\n        }\n    } else if (zobj->encoding == OBJ_ENCODING_SKIPLIST) {\n        zset *zs = (zset*)zobj->m_ptr;\n        zskiplist *zsl = zs->zsl;\n        zskiplistNode *ln;\n\n        if ((ln = zslFirstInRange(zsl, &range)) == NULL) {\n            /* Nothing exists starting at our min.  No results. */\n            return 0;\n        }\n\n        while (ln) {\n            sds ele = ln->ele;\n            /* Abort when the node is no longer in range. */\n            if (!zslValueLteMax(ln->score, &range))\n                break;\n\n            ele = sdsdup(ele);\n            if (geoAppendIfWithinShape(ga,shape,ln->score,ele)\n                == C_ERR) sdsfree(ele);\n            if (ga->used && limit && ga->used >= limit) break;\n            ln = ln->level(0)->forward;\n        }\n    }\n    return ga->used - origincount;\n}\n\n/* Compute the sorted set scores min (inclusive), max (exclusive) we should\n * query in order to retrieve all the elements inside the specified area\n * 'hash'. The two scores are returned by reference in *min and *max. */\nvoid scoresOfGeoHashBox(GeoHashBits hash, GeoHashFix52Bits *min, GeoHashFix52Bits *max) {\n    /* We want to compute the sorted set scores that will include all the\n     * elements inside the specified Geohash 'hash', which has as many\n     * bits as specified by hash.step * 2.\n     *\n     * So if step is, for example, 3, and the hash value in binary\n     * is 101010, since our score is 52 bits we want every element which\n     * is in binary: 101010?????????????????????????????????????????????\n     * Where ? can be 0 or 1.\n     *\n     * To get the min score we just use the initial hash value left\n     * shifted enough to get the 52 bit value. Later we increment the\n     * 6 bit prefis (see the hash.bits++ statement), and get the new\n     * prefix: 101011, which we align again to 52 bits to get the maximum\n     * value (which is excluded from the search). So we get everything\n     * between the two following scores (represented in binary):\n     *\n     * 1010100000000000000000000000000000000000000000000000 (included)\n     * and\n     * 1010110000000000000000000000000000000000000000000000 (excluded).\n     */\n    *min = geohashAlign52Bits(hash);\n    hash.bits++;\n    *max = geohashAlign52Bits(hash);\n}\n\n/* Obtain all members between the min/max of this geohash bounding box.\n * Populate a geoArray of GeoPoints by calling geoGetPointsInRange().\n * Return the number of points added to the array. */\nint membersOfGeoHashBox(robj_roptr zobj, GeoHashBits hash, geoArray *ga, GeoShape *shape, unsigned long limit) {\n    GeoHashFix52Bits min, max;\n\n    scoresOfGeoHashBox(hash,&min,&max);\n    return geoGetPointsInRange(zobj, min, max, shape, ga, limit);\n}\n\n/* Search all eight neighbors + self geohash box */\nint membersOfAllNeighbors(robj_roptr zobj, GeoHashRadius n, GeoShape *shape, geoArray *ga, unsigned long limit) {\n    GeoHashBits neighbors[9];\n    unsigned int i, count = 0, last_processed = 0;\n    int debugmsg = 0;\n\n    neighbors[0] = n.hash;\n    neighbors[1] = n.neighbors.north;\n    neighbors[2] = n.neighbors.south;\n    neighbors[3] = n.neighbors.east;\n    neighbors[4] = n.neighbors.west;\n    neighbors[5] = n.neighbors.north_east;\n    neighbors[6] = n.neighbors.north_west;\n    neighbors[7] = n.neighbors.south_east;\n    neighbors[8] = n.neighbors.south_west;\n\n    /* For each neighbor (*and* our own hashbox), get all the matching\n     * members and add them to the potential result list. */\n    for (i = 0; i < sizeof(neighbors) / sizeof(*neighbors); i++) {\n        if (HASHISZERO(neighbors[i])) {\n            if (debugmsg) D(\"neighbors[%d] is zero\",i);\n            continue;\n        }\n\n        /* Debugging info. */\n        if (debugmsg) {\n            GeoHashRange long_range, lat_range;\n            geohashGetCoordRange(&long_range,&lat_range);\n            GeoHashArea myarea = {{0}};\n            geohashDecode(long_range, lat_range, neighbors[i], &myarea);\n\n            /* Dump center square. */\n            D(\"neighbors[%d]:\\n\",i);\n            D(\"area.longitude.min: %f\\n\", myarea.longitude.min);\n            D(\"area.longitude.max: %f\\n\", myarea.longitude.max);\n            D(\"area.latitude.min: %f\\n\", myarea.latitude.min);\n            D(\"area.latitude.max: %f\\n\", myarea.latitude.max);\n            D(\"\\n\");\n        }\n\n        /* When a huge Radius (in the 5000 km range or more) is used,\n         * adjacent neighbors can be the same, leading to duplicated\n         * elements. Skip every range which is the same as the one\n         * processed previously. */\n        if (last_processed &&\n            neighbors[i].bits == neighbors[last_processed].bits &&\n            neighbors[i].step == neighbors[last_processed].step)\n        {\n            if (debugmsg)\n                D(\"Skipping processing of %d, same as previous\\n\",i);\n            continue;\n        }\n        if (ga->used && limit && ga->used >= limit) break;\n        count += membersOfGeoHashBox(zobj, neighbors[i], ga, shape, limit);\n        last_processed = i;\n    }\n    return count;\n}\n\n/* Sort comparators for qsort() */\nstatic int sort_gp_asc(const void *a, const void *b) {\n    const struct geoPoint *gpa = (geoPoint*)a, *gpb = (geoPoint*)b;\n    /* We can't do adist - bdist because they are doubles and\n     * the comparator returns an int. */\n    if (gpa->dist > gpb->dist)\n        return 1;\n    else if (gpa->dist == gpb->dist)\n        return 0;\n    else\n        return -1;\n}\n\nstatic int sort_gp_desc(const void *a, const void *b) {\n    return -sort_gp_asc(a, b);\n}\n\n/* ====================================================================\n * Commands\n * ==================================================================== */\n\n/* GEOADD key [CH] [NX|XX] long lat name [long2 lat2 name2 ... longN latN nameN] */\nvoid geoaddCommand(client *c) {\n    int xx = 0, nx = 0, longidx = 2;\n    int i;\n\n    /* Parse options. At the end 'longidx' is set to the argument position\n     * of the longitude of the first element. */\n    while (longidx < c->argc) {\n        char *opt = szFromObj(c->argv[longidx]);\n        if (!strcasecmp(opt,\"nx\")) nx = 1;\n        else if (!strcasecmp(opt,\"xx\")) xx = 1;\n        else if (!strcasecmp(opt,\"ch\")) {}\n        else break;\n        longidx++;\n    }\n\n    if ((c->argc - longidx) % 3 || (xx && nx)) {\n        /* Need an odd number of arguments if we got this far... */\n            addReplyErrorObject(c,shared.syntaxerr);\n        return;\n    }\n\n    /* Set up the vector for calling ZADD. */\n    int elements = (c->argc - longidx) / 3;\n    int argc = longidx+elements*2; /* ZADD key [CH] [NX|XX] score ele ... */\n    robj **argv = (robj**)zcalloc(argc*sizeof(robj*), MALLOC_LOCAL);\n    argv[0] = createRawStringObject(\"zadd\",4);\n    for (i = 1; i < longidx; i++) {\n        argv[i] = c->argv[i];\n        incrRefCount(argv[i]);\n    }\n\n    /* Create the argument vector to call ZADD in order to add all\n     * the score,value pairs to the requested zset, where score is actually\n     * an encoded version of lat,long. */\n    for (i = 0; i < elements; i++) {\n        double xy[2];\n\n        if (extractLongLatOrReply(c, (c->argv+longidx)+(i*3),xy) == C_ERR) {\n            for (i = 0; i < argc; i++)\n                if (argv[i]) decrRefCount(argv[i]);\n            zfree(argv);\n            return;\n        }\n\n        /* Turn the coordinates into the score of the element. */\n        GeoHashBits hash;\n        geohashEncodeWGS84(xy[0], xy[1], GEO_STEP_MAX, &hash);\n        GeoHashFix52Bits bits = geohashAlign52Bits(hash);\n        robj *score = createObject(OBJ_STRING, sdsfromlonglong(bits));\n        robj *val = c->argv[longidx + i * 3 + 2];\n        argv[longidx+i*2] = score;\n        argv[longidx+1+i*2] = val;\n        incrRefCount(val);\n    }\n\n    /* Finally call ZADD that will do the work for us. */\n    replaceClientCommandVector(c,argc,argv);\n    zaddCommand(c);\n}\n\n#define SORT_NONE 0\n#define SORT_ASC 1\n#define SORT_DESC 2\n\n#define RADIUS_COORDS (1<<0)    /* Search around coordinates. */\n#define RADIUS_MEMBER (1<<1)    /* Search around member. */\n#define RADIUS_NOSTORE (1<<2)   /* Do not accept STORE/STOREDIST option. */\n#define GEOSEARCH (1<<3)        /* GEOSEARCH command variant (different arguments supported) */\n#define GEOSEARCHSTORE (1<<4)   /* GEOSEARCHSTORE just accept STOREDIST option */\n\n/* GEORADIUS key x y radius unit [WITHDIST] [WITHHASH] [WITHCOORD] [ASC|DESC]\n *                               [COUNT count [ANY]] [STORE key] [STOREDIST key]\n * GEORADIUSBYMEMBER key member radius unit ... options ...\n * GEOSEARCH key [FROMMEMBER member] [FROMLONLAT long lat] [BYRADIUS radius unit]\n *               [BYBOX width height unit] [WITHCOORD] [WITHDIST] [WITHASH] [COUNT count [ANY]] [ASC|DESC]\n * GEOSEARCHSTORE dest_key src_key [FROMMEMBER member] [FROMLONLAT long lat] [BYRADIUS radius unit]\n *               [BYBOX width height unit] [WITHCORD] [WITHDIST] [WITHASH] [COUNT count [ANY]] [ASC|DESC] [STOREDIST]\n *  */\nvoid georadiusGeneric(client *c, int srcKeyIndex, int flags) {\n    robj *storekey = NULL;\n    int storedist = 0; /* 0 for STORE, 1 for STOREDIST. */\n\n    /* Look up the requested zset */\n    robj_roptr zobj = lookupKeyRead(c->db, c->argv[srcKeyIndex]);\n    if (checkType(c, zobj, OBJ_ZSET)) return;\n\n    /* Find long/lat to use for radius or box search based on inquiry type */\n    int base_args;\n    GeoShape shape = {0};\n    if (flags & RADIUS_COORDS) {\n        /* GEORADIUS or GEORADIUS_RO */\n        base_args = 6;\n        shape.type = CIRCULAR_TYPE;\n        if (extractLongLatOrReply(c, c->argv + 2, shape.xy) == C_ERR) return;\n        if (extractDistanceOrReply(c, c->argv+base_args-2, &shape.conversion, &shape.t.radius) != C_OK) return;\n    } else if ((flags & RADIUS_MEMBER) && !zobj) {\n        /* We don't have a source key, but we need to proceed with argument\n         * parsing, so we know which reply to use depending on the STORE flag. */\n        base_args = 5;\n    } else if (flags & RADIUS_MEMBER) {\n        /* GEORADIUSBYMEMBER or GEORADIUSBYMEMBER_RO */\n        base_args = 5;\n        shape.type = CIRCULAR_TYPE;\n        robj *member = c->argv[2];\n        if (longLatFromMember(zobj, member, shape.xy) == C_ERR) {\n            addReplyError(c, \"could not decode requested zset member\");\n            return;\n        }\n        if (extractDistanceOrReply(c, c->argv+base_args-2, &shape.conversion, &shape.t.radius) != C_OK) return;\n    } else if (flags & GEOSEARCH) {\n        /* GEOSEARCH or GEOSEARCHSTORE */\n        base_args = 2;\n        if (flags & GEOSEARCHSTORE) {\n            base_args = 3;\n            storekey = c->argv[1];\n        }\n    } else {\n        addReplyError(c, \"Unknown georadius search type\");\n        return;\n    }\n\n    /* Discover and populate all optional parameters. */\n    int withdist = 0, withhash = 0, withcoords = 0;\n    int frommember = 0, fromloc = 0, byradius = 0, bybox = 0;\n    int sort = SORT_NONE;\n    int any = 0; /* any=1 means a limited search, stop as soon as enough results were found. */\n    long long count = 0;  /* Max number of results to return. 0 means unlimited. */\n    if (c->argc > base_args) {\n        int remaining = c->argc - base_args;\n        for (int i = 0; i < remaining; i++) {\n            char *arg = szFromObj(c->argv[base_args + i]);\n            if (!strcasecmp(arg, \"withdist\")) {\n                withdist = 1;\n            } else if (!strcasecmp(arg, \"withhash\")) {\n                withhash = 1;\n            } else if (!strcasecmp(arg, \"withcoord\")) {\n                withcoords = 1;\n            } else if (!strcasecmp(arg, \"any\")) {\n                any = 1;\n            } else if (!strcasecmp(arg, \"asc\")) {\n                sort = SORT_ASC;\n            } else if (!strcasecmp(arg, \"desc\")) {\n                sort = SORT_DESC;\n            } else if (!strcasecmp(arg, \"count\") && (i+1) < remaining) {\n                if (getLongLongFromObjectOrReply(c, c->argv[base_args+i+1],\n                                                 &count, NULL) != C_OK) return;\n                if (count <= 0) {\n                    addReplyError(c,\"COUNT must be > 0\");\n                    return;\n                }\n                i++;\n            } else if (!strcasecmp(arg, \"store\") &&\n                       (i+1) < remaining &&\n                       !(flags & RADIUS_NOSTORE) &&\n                       !(flags & GEOSEARCH))\n            {\n                storekey = c->argv[base_args+i+1];\n                storedist = 0;\n                i++;\n            } else if (!strcasecmp(arg, \"storedist\") &&\n                       (i+1) < remaining &&\n                       !(flags & RADIUS_NOSTORE) &&\n                       !(flags & GEOSEARCH))\n            {\n                storekey = c->argv[base_args+i+1];\n                storedist = 1;\n                i++;\n            } else if (!strcasecmp(arg, \"storedist\") &&\n                       (flags & GEOSEARCH) &&\n                       (flags & GEOSEARCHSTORE))\n            {\n                storedist = 1;\n            } else if (!strcasecmp(arg, \"frommember\") &&\n                      (i+1) < remaining &&\n                      flags & GEOSEARCH &&\n                      !fromloc)\n            {\n                /* No source key, proceed with argument parsing and return an error when done. */\n                if (!zobj) {\n                    frommember = 1;\n                    i++;\n                    continue;\n                }\n\n                if (longLatFromMember(zobj, c->argv[base_args+i+1], shape.xy) == C_ERR) {\n                    addReplyError(c, \"could not decode requested zset member\");\n                    return;\n                }\n                frommember = 1;\n                i++;\n            } else if (!strcasecmp(arg, \"fromlonlat\") &&\n                       (i+2) < remaining &&\n                       flags & GEOSEARCH &&\n                       !frommember)\n            {\n                if (extractLongLatOrReply(c, c->argv+base_args+i+1, shape.xy) == C_ERR) return;\n                fromloc = 1;\n                i += 2;\n            } else if (!strcasecmp(arg, \"byradius\") &&\n                       (i+2) < remaining &&\n                       flags & GEOSEARCH &&\n                       !bybox)\n            {\n                if (extractDistanceOrReply(c, c->argv+base_args+i+1, &shape.conversion, &shape.t.radius) != C_OK)\n                    return;\n                shape.type = CIRCULAR_TYPE;\n                byradius = 1;\n                i += 2;\n            } else if (!strcasecmp(arg, \"bybox\") &&\n                       (i+3) < remaining &&\n                       flags & GEOSEARCH &&\n                       !byradius)\n            {\n                if (extractBoxOrReply(c, c->argv+base_args+i+1, &shape.conversion, &shape.t.r.width,\n                        &shape.t.r.height) != C_OK) return;\n                shape.type = RECTANGLE_TYPE;\n                bybox = 1;\n                i += 3;\n            } else {\n                addReplyErrorObject(c,shared.syntaxerr);\n                return;\n            }\n        }\n    }\n\n    /* Trap options not compatible with STORE and STOREDIST. */\n    if (storekey && (withdist || withhash || withcoords)) {\n        addReplyError(c,\n            \"STORE option in GEORADIUS is not compatible with \"\n            \"WITHDIST, WITHHASH and WITHCOORDS options\");\n        return;\n    }\n\n    if ((flags & GEOSEARCH) && !(frommember || fromloc)) {\n        addReplyErrorFormat(c,\n            \"exactly one of FROMMEMBER or FROMLONLAT can be specified for %s\",\n            (char *)szFromObj(c->argv[0]));\n        return;\n    }\n\n    if ((flags & GEOSEARCH) && !(byradius || bybox)) {\n        addReplyErrorFormat(c,\n            \"exactly one of BYRADIUS and BYBOX can be specified for %s\",\n            (char *)szFromObj(c->argv[0]));\n        return;\n    }\n\n    if (any && !count) {\n        addReplyErrorFormat(c, \"the ANY argument requires COUNT argument\");\n        return;\n    }\n\n    /* Return ASAP when src key does not exist. */\n    if (!zobj) {\n        if (storekey) {\n            /* store key is not NULL, try to delete it and return 0. */\n            if (dbDelete(c->db, storekey)) {\n                signalModifiedKey(c, c->db, storekey);\n                notifyKeyspaceEvent(NOTIFY_GENERIC, \"del\", storekey, c->db->id);\n                g_pserver->dirty++;\n            }\n            addReply(c, shared.czero);\n        } else {\n            /* Otherwise we return an empty array. */\n            addReply(c, shared.emptyarray);\n        }\n        return;\n    }\n\n    /* COUNT without ordering does not make much sense (we need to\n     * sort in order to return the closest N entries),\n     * force ASC ordering if COUNT was specified but no sorting was\n     * requested. Note that this is not needed for ANY option. */\n    if (count != 0 && sort == SORT_NONE && !any) sort = SORT_ASC;\n\n    /* Get all neighbor geohash boxes for our radius search */\n    GeoHashRadius georadius = geohashCalculateAreasByShapeWGS84(&shape);\n\n    /* Search the zset for all matching points */\n    geoArray *ga = geoArrayCreate();\n    membersOfAllNeighbors(zobj, georadius, &shape, ga, any ? count : 0);\n\n    /* If no matching results, the user gets an empty reply. */\n    if (ga->used == 0 && storekey == NULL) {\n        addReply(c,shared.emptyarray);\n        geoArrayFree(ga);\n        return;\n    }\n\n    long result_length = ga->used;\n    long returned_items = (count == 0 || result_length < count) ?\n                          result_length : count;\n    long option_length = 0;\n\n    /* Process [optional] requested sorting */\n    if (sort != SORT_NONE) {\n        int (*sort_gp_callback)(const void *a, const void *b) = NULL;\n        if (sort == SORT_ASC) {\n            sort_gp_callback = sort_gp_asc;\n        } else if (sort == SORT_DESC) {\n            sort_gp_callback = sort_gp_desc;\n        }\n\n        if (returned_items == result_length) {\n            qsort(ga->array, result_length, sizeof(geoPoint), sort_gp_callback);\n        } else {\n            pqsort(ga->array, result_length, sizeof(geoPoint), sort_gp_callback,\n                0, (returned_items - 1));\n        }\n    }\n\n    if (storekey == NULL) {\n        /* No target key, return results to user. */\n\n        /* Our options are self-contained nested multibulk replies, so we\n         * only need to track how many of those nested replies we return. */\n        if (withdist)\n            option_length++;\n\n        if (withcoords)\n            option_length++;\n\n        if (withhash)\n            option_length++;\n\n        /* The array len we send is exactly result_length. The result is\n         * either all strings of just zset members  *or* a nested multi-bulk\n         * reply containing the zset member string _and_ all the additional\n         * options the user enabled for this request. */\n        addReplyArrayLen(c, returned_items);\n\n        /* Finally send results back to the caller */\n        int i;\n        for (i = 0; i < returned_items; i++) {\n            geoPoint *gp = ga->array+i;\n            gp->dist /= shape.conversion; /* Fix according to unit. */\n\n            /* If we have options in option_length, return each sub-result\n             * as a nested multi-bulk.  Add 1 to account for result value\n             * itself. */\n            if (option_length)\n                addReplyArrayLen(c, option_length + 1);\n\n            addReplyBulkSds(c,gp->member);\n            gp->member = NULL;\n\n            if (withdist)\n                addReplyDoubleDistance(c, gp->dist);\n\n            if (withhash)\n                addReplyLongLong(c, gp->score);\n\n            if (withcoords) {\n                addReplyArrayLen(c, 2);\n                addReplyHumanLongDouble(c, gp->longitude);\n                addReplyHumanLongDouble(c, gp->latitude);\n            }\n        }\n    } else {\n        /* Target key, create a sorted set with the results. */\n        robj *zobj;\n        zset *zs;\n        int i;\n        size_t maxelelen = 0, totelelen = 0;\n\n        if (returned_items) {\n            zobj = createZsetObject();\n            zs = (zset*)zobj->m_ptr;\n        }\n\n        for (i = 0; i < returned_items; i++) {\n            zskiplistNode *znode;\n            geoPoint *gp = ga->array+i;\n            gp->dist /= shape.conversion; /* Fix according to unit. */\n            double score = storedist ? gp->dist : gp->score;\n            size_t elelen = sdslen(gp->member);\n\n            if (maxelelen < elelen) maxelelen = elelen;\n            totelelen += elelen;\n            znode = zslInsert(zs->zsl,score,gp->member);\n            serverAssert(dictAdd(zs->dict,gp->member,&znode->score) == DICT_OK);\n            gp->member = NULL;\n        }\n\n        if (returned_items) {\n            zsetConvertToZiplistIfNeeded(zobj,maxelelen,totelelen);\n            setKey(c,c->db,storekey,zobj);\n            decrRefCount(zobj);\n            notifyKeyspaceEvent(NOTIFY_ZSET,flags & GEOSEARCH ? \"geosearchstore\" : \"georadiusstore\",storekey,\n                                c->db->id);\n            g_pserver->dirty += returned_items;\n        } else if (dbDelete(c->db,storekey)) {\n            signalModifiedKey(c,c->db,storekey);\n            notifyKeyspaceEvent(NOTIFY_GENERIC,\"del\",storekey,c->db->id);\n            g_pserver->dirty++;\n        }\n        addReplyLongLong(c, returned_items);\n    }\n    geoArrayFree(ga);\n}\n\n/* GEORADIUS wrapper function. */\nvoid georadiusCommand(client *c) {\n    georadiusGeneric(c, 1, RADIUS_COORDS);\n}\n\n/* GEORADIUSBYMEMBER wrapper function. */\nvoid georadiusbymemberCommand(client *c) {\n    georadiusGeneric(c, 1, RADIUS_MEMBER);\n}\n\n/* GEORADIUS_RO wrapper function. */\nvoid georadiusroCommand(client *c) {\n    georadiusGeneric(c, 1, RADIUS_COORDS|RADIUS_NOSTORE);\n}\n\n/* GEORADIUSBYMEMBER_RO wrapper function. */\nvoid georadiusbymemberroCommand(client *c) {\n    georadiusGeneric(c, 1, RADIUS_MEMBER|RADIUS_NOSTORE);\n}\n\nvoid geosearchCommand(client *c) {\n    georadiusGeneric(c, 1, GEOSEARCH);\n}\n\nvoid geosearchstoreCommand(client *c) {\n    georadiusGeneric(c, 2, GEOSEARCH|GEOSEARCHSTORE);\n}\n\n/* GEOHASH key ele1 ele2 ... eleN\n *\n * Returns an array with an 11 characters geohash representation of the\n * position of the specified elements. */\nvoid geohashCommand(client *c) {\n    const char *geoalphabet= \"0123456789bcdefghjkmnpqrstuvwxyz\";\n    int j;\n\n    /* Look up the requested zset */\n    robj_roptr zobj = lookupKeyRead(c->db, c->argv[1]);\n    if (checkType(c, zobj, OBJ_ZSET)) return;\n\n    /* Geohash elements one after the other, using a null bulk reply for\n     * missing elements. */\n    addReplyArrayLen(c,c->argc-2);\n    for (j = 2; j < c->argc; j++) {\n        double score;\n        if (!zobj || zsetScore(zobj, szFromObj(c->argv[j]), &score) == C_ERR) {\n            addReplyNull(c);\n        } else {\n            /* The internal format we use for geocoding is a bit different\n             * than the standard, since we use as initial latitude range\n             * -85,85, while the normal geohashing algorithm uses -90,90.\n             * So we have to decode our position and re-encode using the\n             * standard ranges in order to output a valid geohash string. */\n\n            /* Decode... */\n            double xy[2];\n            if (!decodeGeohash(score,xy)) {\n                addReplyNull(c);\n                continue;\n            }\n\n            /* Re-encode */\n            GeoHashRange r[2];\n            GeoHashBits hash;\n            r[0].min = -180;\n            r[0].max = 180;\n            r[1].min = -90;\n            r[1].max = 90;\n            geohashEncode(&r[0],&r[1],xy[0],xy[1],26,&hash);\n\n            char buf[12];\n            int i;\n            for (i = 0; i < 11; i++) {\n                int idx;\n                if (i == 10) {\n                    /* We have just 52 bits, but the API used to output\n                     * an 11 bytes geohash. For compatibility we assume\n                     * zero. */\n                    idx = 0;\n                } else {\n                    idx = (hash.bits >> (52-((i+1)*5))) & 0x1f;\n                }\n                buf[i] = geoalphabet[idx];\n            }\n            buf[11] = '\\0';\n            addReplyBulkCBuffer(c,buf,11);\n        }\n    }\n}\n\n/* GEOPOS key ele1 ele2 ... eleN\n *\n * Returns an array of two-items arrays representing the x,y position of each\n * element specified in the arguments. For missing elements NULL is returned. */\nvoid geoposCommand(client *c) {\n    int j;\n\n    /* Look up the requested zset */\n    robj_roptr zobj = lookupKeyRead(c->db, c->argv[1]);\n    if (checkType(c, zobj, OBJ_ZSET)) return;\n\n    /* Report elements one after the other, using a null bulk reply for\n     * missing elements. */\n    addReplyArrayLen(c,c->argc-2);\n    for (j = 2; j < c->argc; j++) {\n        double score;\n        if (!zobj || zsetScore(zobj, szFromObj(c->argv[j]), &score) == C_ERR) {\n            addReplyNullArray(c);\n        } else {\n            /* Decode... */\n            double xy[2];\n            if (!decodeGeohash(score,xy)) {\n                addReplyNullArray(c);\n                continue;\n            }\n            addReplyArrayLen(c,2);\n            addReplyHumanLongDouble(c,xy[0]);\n            addReplyHumanLongDouble(c,xy[1]);\n        }\n    }\n}\n\n/* GEODIST key ele1 ele2 [unit]\n *\n * Return the distance, in meters by default, otherwise according to \"unit\",\n * between points ele1 and ele2. If one or more elements are missing NULL\n * is returned. */\nvoid geodistCommand(client *c) {\n    double to_meter = 1;\n\n    /* Check if there is the unit to extract, otherwise assume meters. */\n    if (c->argc == 5) {\n        to_meter = extractUnitOrReply(c,c->argv[4]);\n        if (to_meter < 0) return;\n    } else if (c->argc > 5) {\n        addReplyErrorObject(c,shared.syntaxerr);\n        return;\n    }\n\n    /* Look up the requested zset */\n    robj_roptr zobj = NULL;\n    if ((zobj = lookupKeyReadOrReply(c, c->argv[1], shared.null[c->resp]))\n        == nullptr || checkType(c, zobj, OBJ_ZSET)) return;\n\n    /* Get the scores. We need both otherwise NULL is returned. */\n    double score1, score2, xyxy[4];\n    if (zsetScore(zobj, szFromObj(c->argv[2]), &score1) == C_ERR ||\n        zsetScore(zobj, szFromObj(c->argv[3]), &score2) == C_ERR)\n    {\n        addReplyNull(c);\n        return;\n    }\n\n    /* Decode & compute the distance. */\n    if (!decodeGeohash(score1,xyxy) || !decodeGeohash(score2,xyxy+2))\n        addReplyNull(c);\n    else\n        addReplyDoubleDistance(c,\n            geohashGetDistance(xyxy[0],xyxy[1],xyxy[2],xyxy[3]) / to_meter);\n}\n"
  },
  {
    "path": "src/geo.h",
    "content": "#ifndef __GEO_H__\n#define __GEO_H__\n\n#include \"server.h\"\n\n/* Structures used inside geo.c in order to represent points and array of\n * points on the earth. */\ntypedef struct geoPoint {\n    double longitude;\n    double latitude;\n    double dist;\n    double score;\n    char *member;\n} geoPoint;\n\ntypedef struct geoArray {\n    struct geoPoint *array;\n    size_t buckets;\n    size_t used;\n} geoArray;\n\n#endif\n"
  },
  {
    "path": "src/geohash.c",
    "content": "/*\n * Copyright (c) 2013-2014, yinqiwen <yinqiwen@gmail.com>\n * Copyright (c) 2014, Matt Stancliff <matt@genges.com>.\n * Copyright (c) 2015-2016, Salvatore Sanfilippo <antirez@gmail.com>.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *  * Redistributions of source code must retain the above copyright notice,\n *    this list of conditions and the following disclaimer.\n *  * Redistributions in binary form must reproduce the above copyright\n *    notice, this list of conditions and the following disclaimer in the\n *    documentation and/or other materials provided with the distribution.\n *  * Neither the name of Redis nor the names of its contributors may be used\n *    to endorse or promote products derived from this software without\n *    specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS\n * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF\n * THE POSSIBILITY OF SUCH DAMAGE.\n */\n#include \"geohash.h\"\n\n/**\n * Hashing works like this:\n * Divide the world into 4 buckets.  Label each one as such:\n *  -----------------\n *  |       |       |\n *  |       |       |\n *  | 0,1   | 1,1   |\n *  -----------------\n *  |       |       |\n *  |       |       |\n *  | 0,0   | 1,0   |\n *  -----------------\n */\n\n/* Interleave lower bits of x and y, so the bits of x\n * are in the even positions and bits from y in the odd;\n * x and y must initially be less than 2**32 (65536).\n * From:  https://graphics.stanford.edu/~seander/bithacks.html#InterleaveBMN\n */\nstatic inline uint64_t interleave64(uint32_t xlo, uint32_t ylo) {\n    static const uint64_t B[] = {0x5555555555555555ULL, 0x3333333333333333ULL,\n                                 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,\n                                 0x0000FFFF0000FFFFULL};\n    static const unsigned int S[] = {1, 2, 4, 8, 16};\n\n    uint64_t x = xlo;\n    uint64_t y = ylo;\n\n    x = (x | (x << S[4])) & B[4];\n    y = (y | (y << S[4])) & B[4];\n\n    x = (x | (x << S[3])) & B[3];\n    y = (y | (y << S[3])) & B[3];\n\n    x = (x | (x << S[2])) & B[2];\n    y = (y | (y << S[2])) & B[2];\n\n    x = (x | (x << S[1])) & B[1];\n    y = (y | (y << S[1])) & B[1];\n\n    x = (x | (x << S[0])) & B[0];\n    y = (y | (y << S[0])) & B[0];\n\n    return x | (y << 1);\n}\n\n/* reverse the interleave process\n * derived from http://stackoverflow.com/questions/4909263\n */\nstatic inline uint64_t deinterleave64(uint64_t interleaved) {\n    static const uint64_t B[] = {0x5555555555555555ULL, 0x3333333333333333ULL,\n                                 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,\n                                 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL};\n    static const unsigned int S[] = {0, 1, 2, 4, 8, 16};\n\n    uint64_t x = interleaved;\n    uint64_t y = interleaved >> 1;\n\n    x = (x | (x >> S[0])) & B[0];\n    y = (y | (y >> S[0])) & B[0];\n\n    x = (x | (x >> S[1])) & B[1];\n    y = (y | (y >> S[1])) & B[1];\n\n    x = (x | (x >> S[2])) & B[2];\n    y = (y | (y >> S[2])) & B[2];\n\n    x = (x | (x >> S[3])) & B[3];\n    y = (y | (y >> S[3])) & B[3];\n\n    x = (x | (x >> S[4])) & B[4];\n    y = (y | (y >> S[4])) & B[4];\n\n    x = (x | (x >> S[5])) & B[5];\n    y = (y | (y >> S[5])) & B[5];\n\n    return x | (y << 32);\n}\n\nvoid geohashGetCoordRange(GeoHashRange *long_range, GeoHashRange *lat_range) {\n    /* These are constraints from EPSG:900913 / EPSG:3785 / OSGEO:41001 */\n    /* We can't geocode at the north/south pole. */\n    long_range->max = GEO_LONG_MAX;\n    long_range->min = GEO_LONG_MIN;\n    lat_range->max = GEO_LAT_MAX;\n    lat_range->min = GEO_LAT_MIN;\n}\n\nint geohashEncode(const GeoHashRange *long_range, const GeoHashRange *lat_range,\n                  double longitude, double latitude, uint8_t step,\n                  GeoHashBits *hash) {\n    /* Check basic arguments sanity. */\n    if (hash == NULL || step > 32 || step == 0 ||\n        RANGEPISZERO(lat_range) || RANGEPISZERO(long_range)) return 0;\n\n    /* Return an error when trying to index outside the supported\n     * constraints. */\n    if (longitude > GEO_LONG_MAX || longitude < GEO_LONG_MIN ||\n        latitude > GEO_LAT_MAX || latitude < GEO_LAT_MIN) return 0;\n\n    hash->bits = 0;\n    hash->step = step;\n\n    if (latitude < lat_range->min || latitude > lat_range->max ||\n        longitude < long_range->min || longitude > long_range->max) {\n        return 0;\n    }\n\n    double lat_offset =\n        (latitude - lat_range->min) / (lat_range->max - lat_range->min);\n    double long_offset =\n        (longitude - long_range->min) / (long_range->max - long_range->min);\n\n    /* convert to fixed point based on the step size */\n    lat_offset *= (1ULL << step);\n    long_offset *= (1ULL << step);\n    hash->bits = interleave64(lat_offset, long_offset);\n    return 1;\n}\n\nint geohashEncodeType(double longitude, double latitude, uint8_t step, GeoHashBits *hash) {\n    GeoHashRange r[2] = {{0}};\n    geohashGetCoordRange(&r[0], &r[1]);\n    return geohashEncode(&r[0], &r[1], longitude, latitude, step, hash);\n}\n\nint geohashEncodeWGS84(double longitude, double latitude, uint8_t step,\n                       GeoHashBits *hash) {\n    return geohashEncodeType(longitude, latitude, step, hash);\n}\n\nint geohashDecode(const GeoHashRange long_range, const GeoHashRange lat_range,\n                   const GeoHashBits hash, GeoHashArea *area) {\n    if (HASHISZERO(hash) || NULL == area || RANGEISZERO(lat_range) ||\n        RANGEISZERO(long_range)) {\n        return 0;\n    }\n\n    area->hash = hash;\n    uint8_t step = hash.step;\n    uint64_t hash_sep = deinterleave64(hash.bits); /* hash = [LAT][LONG] */\n\n    double lat_scale = lat_range.max - lat_range.min;\n    double long_scale = long_range.max - long_range.min;\n\n    uint32_t ilato = hash_sep;       /* get lat part of deinterleaved hash */\n    uint32_t ilono = hash_sep >> 32; /* shift over to get long part of hash */\n\n    /* divide by 2**step.\n     * Then, for 0-1 coordinate, multiply times scale and add\n       to the min to get the absolute coordinate. */\n    area->latitude.min =\n        lat_range.min + (ilato * 1.0 / (1ull << step)) * lat_scale;\n    area->latitude.max =\n        lat_range.min + ((ilato + 1) * 1.0 / (1ull << step)) * lat_scale;\n    area->longitude.min =\n        long_range.min + (ilono * 1.0 / (1ull << step)) * long_scale;\n    area->longitude.max =\n        long_range.min + ((ilono + 1) * 1.0 / (1ull << step)) * long_scale;\n\n    return 1;\n}\n\nint geohashDecodeType(const GeoHashBits hash, GeoHashArea *area) {\n    GeoHashRange r[2] = {{0}};\n    geohashGetCoordRange(&r[0], &r[1]);\n    return geohashDecode(r[0], r[1], hash, area);\n}\n\nint geohashDecodeWGS84(const GeoHashBits hash, GeoHashArea *area) {\n    return geohashDecodeType(hash, area);\n}\n\nint geohashDecodeAreaToLongLat(const GeoHashArea *area, double *xy) {\n    if (!xy) return 0;\n    xy[0] = (area->longitude.min + area->longitude.max) / 2;\n    if (xy[0] > GEO_LONG_MAX) xy[0] = GEO_LONG_MAX;\n    if (xy[0] < GEO_LONG_MIN) xy[0] = GEO_LONG_MIN;\n    xy[1] = (area->latitude.min + area->latitude.max) / 2;\n    if (xy[1] > GEO_LAT_MAX) xy[1] = GEO_LAT_MAX;\n    if (xy[1] < GEO_LAT_MIN) xy[1] = GEO_LAT_MIN;\n    return 1;\n}\n\nint geohashDecodeToLongLatType(const GeoHashBits hash, double *xy) {\n    GeoHashArea area = {{0}};\n    if (!xy || !geohashDecodeType(hash, &area))\n        return 0;\n    return geohashDecodeAreaToLongLat(&area, xy);\n}\n\nint geohashDecodeToLongLatWGS84(const GeoHashBits hash, double *xy) {\n    return geohashDecodeToLongLatType(hash, xy);\n}\n\nstatic void geohash_move_x(GeoHashBits *hash, int8_t d) {\n    if (d == 0)\n        return;\n\n    uint64_t x = hash->bits & 0xaaaaaaaaaaaaaaaaULL;\n    uint64_t y = hash->bits & 0x5555555555555555ULL;\n\n    uint64_t zz = 0x5555555555555555ULL >> (64 - hash->step * 2);\n\n    if (d > 0) {\n        x = x + (zz + 1);\n    } else {\n        x = x | zz;\n        x = x - (zz + 1);\n    }\n\n    x &= (0xaaaaaaaaaaaaaaaaULL >> (64 - hash->step * 2));\n    hash->bits = (x | y);\n}\n\nstatic void geohash_move_y(GeoHashBits *hash, int8_t d) {\n    if (d == 0)\n        return;\n\n    uint64_t x = hash->bits & 0xaaaaaaaaaaaaaaaaULL;\n    uint64_t y = hash->bits & 0x5555555555555555ULL;\n\n    uint64_t zz = 0xaaaaaaaaaaaaaaaaULL >> (64 - hash->step * 2);\n    if (d > 0) {\n        y = y + (zz + 1);\n    } else {\n        y = y | zz;\n        y = y - (zz + 1);\n    }\n    y &= (0x5555555555555555ULL >> (64 - hash->step * 2));\n    hash->bits = (x | y);\n}\n\nvoid geohashNeighbors(const GeoHashBits *hash, GeoHashNeighbors *neighbors) {\n    neighbors->east = *hash;\n    neighbors->west = *hash;\n    neighbors->north = *hash;\n    neighbors->south = *hash;\n    neighbors->south_east = *hash;\n    neighbors->south_west = *hash;\n    neighbors->north_east = *hash;\n    neighbors->north_west = *hash;\n\n    geohash_move_x(&neighbors->east, 1);\n    geohash_move_y(&neighbors->east, 0);\n\n    geohash_move_x(&neighbors->west, -1);\n    geohash_move_y(&neighbors->west, 0);\n\n    geohash_move_x(&neighbors->south, 0);\n    geohash_move_y(&neighbors->south, -1);\n\n    geohash_move_x(&neighbors->north, 0);\n    geohash_move_y(&neighbors->north, 1);\n\n    geohash_move_x(&neighbors->north_west, -1);\n    geohash_move_y(&neighbors->north_west, 1);\n\n    geohash_move_x(&neighbors->north_east, 1);\n    geohash_move_y(&neighbors->north_east, 1);\n\n    geohash_move_x(&neighbors->south_east, 1);\n    geohash_move_y(&neighbors->south_east, -1);\n\n    geohash_move_x(&neighbors->south_west, -1);\n    geohash_move_y(&neighbors->south_west, -1);\n}\n"
  },
  {
    "path": "src/geohash.h",
    "content": "/*\n * Copyright (c) 2013-2014, yinqiwen <yinqiwen@gmail.com>\n * Copyright (c) 2014, Matt Stancliff <matt@genges.com>.\n * Copyright (c) 2015, Salvatore Sanfilippo <antirez@gmail.com>.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *  * Redistributions of source code must retain the above copyright notice,\n *    this list of conditions and the following disclaimer.\n *  * Redistributions in binary form must reproduce the above copyright\n *    notice, this list of conditions and the following disclaimer in the\n *    documentation and/or other materials provided with the distribution.\n *  * Neither the name of Redis nor the names of its contributors may be used\n *    to endorse or promote products derived from this software without\n *    specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS\n * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF\n * THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef GEOHASH_H_\n#define GEOHASH_H_\n\n#include <stddef.h>\n#include <stdint.h>\n#include <stdint.h>\n\n#if defined(__cplusplus)\nextern \"C\" {\n#endif\n\n#define HASHISZERO(r) (!(r).bits && !(r).step)\n#define RANGEISZERO(r) (!(r).max && !(r).min)\n#define RANGEPISZERO(r) (r == NULL || RANGEISZERO(*r))\n\n#define GEO_STEP_MAX 26 /* 26*2 = 52 bits. */\n\n/* Limits from EPSG:900913 / EPSG:3785 / OSGEO:41001 */\n#define GEO_LAT_MIN -85.05112878\n#define GEO_LAT_MAX 85.05112878\n#define GEO_LONG_MIN -180\n#define GEO_LONG_MAX 180\n\ntypedef enum {\n    GEOHASH_NORTH = 0,\n    GEOHASH_EAST,\n    GEOHASH_WEST,\n    GEOHASH_SOUTH,\n    GEOHASH_SOUTH_WEST,\n    GEOHASH_SOUTH_EAST,\n    GEOHASH_NORT_WEST,\n    GEOHASH_NORT_EAST\n} GeoDirection;\n\ntypedef struct {\n    uint64_t bits;\n    uint8_t step;\n} GeoHashBits;\n\ntypedef struct {\n    double min;\n    double max;\n} GeoHashRange;\n\ntypedef struct {\n    GeoHashBits hash;\n    GeoHashRange longitude;\n    GeoHashRange latitude;\n} GeoHashArea;\n\ntypedef struct {\n    GeoHashBits north;\n    GeoHashBits east;\n    GeoHashBits west;\n    GeoHashBits south;\n    GeoHashBits north_east;\n    GeoHashBits south_east;\n    GeoHashBits north_west;\n    GeoHashBits south_west;\n} GeoHashNeighbors;\n\n#define CIRCULAR_TYPE 1\n#define RECTANGLE_TYPE 2\ntypedef struct {\n    int type; /* search type */\n    double xy[2]; /* search center point, xy[0]: lon, xy[1]: lat */\n    double conversion; /* km: 1000 */\n    double bounds[4]; /* bounds[0]: min_lon, bounds[1]: min_lat\n                       * bounds[2]: max_lon, bounds[3]: max_lat */\n    union {\n        /* CIRCULAR_TYPE */\n        double radius;\n        /* RECTANGLE_TYPE */\n        struct {\n            double height;\n            double width;\n        } r;\n    } t;\n} GeoShape;\n\n/*\n * 0:success\n * -1:failed\n */\nvoid geohashGetCoordRange(GeoHashRange *long_range, GeoHashRange *lat_range);\nint geohashEncode(const GeoHashRange *long_range, const GeoHashRange *lat_range,\n                  double longitude, double latitude, uint8_t step,\n                  GeoHashBits *hash);\nint geohashEncodeType(double longitude, double latitude,\n                      uint8_t step, GeoHashBits *hash);\nint geohashEncodeWGS84(double longitude, double latitude, uint8_t step,\n                       GeoHashBits *hash);\nint geohashDecode(const GeoHashRange long_range, const GeoHashRange lat_range,\n                  const GeoHashBits hash, GeoHashArea *area);\nint geohashDecodeType(const GeoHashBits hash, GeoHashArea *area);\nint geohashDecodeWGS84(const GeoHashBits hash, GeoHashArea *area);\nint geohashDecodeAreaToLongLat(const GeoHashArea *area, double *xy);\nint geohashDecodeToLongLatType(const GeoHashBits hash, double *xy);\nint geohashDecodeToLongLatWGS84(const GeoHashBits hash, double *xy);\nint geohashDecodeToLongLatMercator(const GeoHashBits hash, double *xy);\nvoid geohashNeighbors(const GeoHashBits *hash, GeoHashNeighbors *neighbors);\n\n#if defined(__cplusplus)\n}\n#endif\n#endif /* GEOHASH_H_ */\n"
  },
  {
    "path": "src/geohash_helper.cpp",
    "content": "/*\n * Copyright (c) 2013-2014, yinqiwen <yinqiwen@gmail.com>\n * Copyright (c) 2014, Matt Stancliff <matt@genges.com>.\n * Copyright (c) 2015-2016, Salvatore Sanfilippo <antirez@gmail.com>.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *  * Redistributions of source code must retain the above copyright notice,\n *    this list of conditions and the following disclaimer.\n *  * Redistributions in binary form must reproduce the above copyright\n *    notice, this list of conditions and the following disclaimer in the\n *    documentation and/or other materials provided with the distribution.\n *  * Neither the name of Redis nor the names of its contributors may be used\n *    to endorse or promote products derived from this software without\n *    specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS\n * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF\n * THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n/* This is a C++ to C conversion from the ardb project.\n * This file started out as:\n * https://github.com/yinqiwen/ardb/blob/d42503/src/geo/geohash_helper.cpp\n */\n\n#include \"fmacros.h\"\n#include \"geohash_helper.h\"\n#include \"debugmacro.h\"\n#include <math.h>\n\n#define D_R (M_PI / 180.0)\n#define R_MAJOR 6378137.0\n#define R_MINOR 6356752.3142\n#define RATIO (R_MINOR / R_MAJOR)\n#define ECCENT (sqrt(1.0 - (RATIO *RATIO)))\n#define COM (0.5 * ECCENT)\n\n/// @brief Earth's quatratic mean radius for WGS-84\nconst double EARTH_RADIUS_IN_METERS = 6372797.560856;\n\nconst double MERCATOR_MAX = 20037726.37;\n\nstatic inline double deg_rad(double ang) { return ang * D_R; }\nstatic inline double rad_deg(double ang) { return ang / D_R; }\n\n/* This function is used in order to estimate the step (bits precision)\n * of the 9 search area boxes during radius queries. */\nuint8_t geohashEstimateStepsByRadius(double range_meters, double lat) {\n    if (range_meters == 0) return 26;\n    int step = 1;\n    while (range_meters < MERCATOR_MAX) {\n        range_meters *= 2;\n        step++;\n    }\n    step -= 2; /* Make sure range is included in most of the base cases. */\n\n    /* Wider range towards the poles... Note: it is possible to do better\n     * than this approximation by computing the distance between meridians\n     * at this latitude, but this does the trick for now. */\n    if (lat > 66 || lat < -66) {\n        step--;\n        if (lat > 80 || lat < -80) step--;\n    }\n\n    /* Frame to valid range. */\n    if (step < 1) step = 1;\n    if (step > 26) step = 26;\n    return step;\n}\n\n/* Return the bounding box of the search area by shape (see geohash.h GeoShape)\n * bounds[0] - bounds[2] is the minimum and maximum longitude\n * while bounds[1] - bounds[3] is the minimum and maximum latitude.\n * since the higher the latitude, the shorter the arc length, the box shape is as follows\n * (left and right edges are actually bent), as shown in the following diagram:\n *\n *    \\-----------------/          --------               \\-----------------/\n *     \\               /         /          \\              \\               /\n *      \\  (long,lat) /         / (long,lat) \\              \\  (long,lat) /\n *       \\           /         /              \\              /            \\\n *         ---------          /----------------\\            /--------------\\\n *  Northern Hemisphere       Southern Hemisphere         Around the equator\n */\nint geohashBoundingBox(GeoShape *shape, double *bounds) {\n    if (!bounds) return 0;\n    double longitude = shape->xy[0];\n    double latitude = shape->xy[1];\n    double height = shape->conversion * (shape->type == CIRCULAR_TYPE ? shape->t.radius : shape->t.r.height/2);\n    double width = shape->conversion * (shape->type == CIRCULAR_TYPE ? shape->t.radius : shape->t.r.width/2);\n\n    const double lat_delta = rad_deg(height/EARTH_RADIUS_IN_METERS);\n    const double long_delta_top = rad_deg(width/EARTH_RADIUS_IN_METERS/cos(deg_rad(latitude+lat_delta)));\n    const double long_delta_bottom = rad_deg(width/EARTH_RADIUS_IN_METERS/cos(deg_rad(latitude-lat_delta)));\n    /* The directions of the northern and southern hemispheres\n     * are opposite, so we choice different points as min/max long/lat */\n    int southern_hemisphere = latitude < 0 ? 1 : 0;\n    bounds[0] = southern_hemisphere ? longitude-long_delta_bottom : longitude-long_delta_top;\n    bounds[2] = southern_hemisphere ? longitude+long_delta_bottom : longitude+long_delta_top;\n    bounds[1] = latitude - lat_delta;\n    bounds[3] = latitude + lat_delta;\n    return 1;\n}\n\n/* Calculate a set of areas (center + 8) that are able to cover a range query\n * for the specified position and shape (see geohash.h GeoShape).\n * the bounding box saved in shaple.bounds */\nGeoHashRadius geohashCalculateAreasByShapeWGS84(GeoShape *shape) {\n    GeoHashRange long_range, lat_range;\n    GeoHashRadius radius;\n    GeoHashBits hash;\n    GeoHashNeighbors neighbors;\n    GeoHashArea area;\n    double min_lon, max_lon, min_lat, max_lat;\n    int steps;\n\n    geohashBoundingBox(shape, shape->bounds);\n    min_lon = shape->bounds[0];\n    min_lat = shape->bounds[1];\n    max_lon = shape->bounds[2];\n    max_lat = shape->bounds[3];\n\n    double longitude = shape->xy[0];\n    double latitude = shape->xy[1];\n    /* radius_meters is calculated differently in different search types:\n     * 1) CIRCULAR_TYPE, just use radius.\n     * 2) RECTANGLE_TYPE, we use sqrt((width/2)^2 + (height/2)^2) to\n     * calculate the distance from the center point to the corner */\n    double radius_meters = shape->type == CIRCULAR_TYPE ? shape->t.radius :\n            sqrt((shape->t.r.width/2)*(shape->t.r.width/2) + (shape->t.r.height/2)*(shape->t.r.height/2));\n    radius_meters *= shape->conversion;\n\n    steps = geohashEstimateStepsByRadius(radius_meters,latitude);\n\n    geohashGetCoordRange(&long_range,&lat_range);\n    geohashEncode(&long_range,&lat_range,longitude,latitude,steps,&hash);\n    geohashNeighbors(&hash,&neighbors);\n    geohashDecode(long_range,lat_range,hash,&area);\n\n    /* Check if the step is enough at the limits of the covered area.\n     * Sometimes when the search area is near an edge of the\n     * area, the estimated step is not small enough, since one of the\n     * north / south / west / east square is too near to the search area\n     * to cover everything. */\n    int decrease_step = 0;\n    {\n        GeoHashArea north, south, east, west;\n\n        geohashDecode(long_range, lat_range, neighbors.north, &north);\n        geohashDecode(long_range, lat_range, neighbors.south, &south);\n        geohashDecode(long_range, lat_range, neighbors.east, &east);\n        geohashDecode(long_range, lat_range, neighbors.west, &west);\n\n        if (geohashGetDistance(longitude,latitude,longitude,north.latitude.max)\n            < radius_meters) decrease_step = 1;\n        if (geohashGetDistance(longitude,latitude,longitude,south.latitude.min)\n            < radius_meters) decrease_step = 1;\n        if (geohashGetDistance(longitude,latitude,east.longitude.max,latitude)\n            < radius_meters) decrease_step = 1;\n        if (geohashGetDistance(longitude,latitude,west.longitude.min,latitude)\n            < radius_meters) decrease_step = 1;\n    }\n\n    if (steps > 1 && decrease_step) {\n        steps--;\n        geohashEncode(&long_range,&lat_range,longitude,latitude,steps,&hash);\n        geohashNeighbors(&hash,&neighbors);\n        geohashDecode(long_range,lat_range,hash,&area);\n    }\n\n    /* Exclude the search areas that are useless. */\n    if (steps >= 2) {\n        if (area.latitude.min < min_lat) {\n            GZERO(neighbors.south);\n            GZERO(neighbors.south_west);\n            GZERO(neighbors.south_east);\n        }\n        if (area.latitude.max > max_lat) {\n            GZERO(neighbors.north);\n            GZERO(neighbors.north_east);\n            GZERO(neighbors.north_west);\n        }\n        if (area.longitude.min < min_lon) {\n            GZERO(neighbors.west);\n            GZERO(neighbors.south_west);\n            GZERO(neighbors.north_west);\n        }\n        if (area.longitude.max > max_lon) {\n            GZERO(neighbors.east);\n            GZERO(neighbors.south_east);\n            GZERO(neighbors.north_east);\n        }\n    }\n    radius.hash = hash;\n    radius.neighbors = neighbors;\n    radius.area = area;\n    return radius;\n}\n\nGeoHashFix52Bits geohashAlign52Bits(const GeoHashBits hash) {\n    uint64_t bits = hash.bits;\n    bits <<= (52 - hash.step * 2);\n    return bits;\n}\n\n/* Calculate distance using haversin great circle distance formula. */\ndouble geohashGetDistance(double lon1d, double lat1d, double lon2d, double lat2d) {\n    double lat1r, lon1r, lat2r, lon2r, u, v;\n    lat1r = deg_rad(lat1d);\n    lon1r = deg_rad(lon1d);\n    lat2r = deg_rad(lat2d);\n    lon2r = deg_rad(lon2d);\n    u = sin((lat2r - lat1r) / 2);\n    v = sin((lon2r - lon1r) / 2);\n    return 2.0 * EARTH_RADIUS_IN_METERS *\n           asin(sqrt(u * u + cos(lat1r) * cos(lat2r) * v * v));\n}\n\nint geohashGetDistanceIfInRadius(double x1, double y1,\n                                 double x2, double y2, double radius,\n                                 double *distance) {\n    *distance = geohashGetDistance(x1, y1, x2, y2);\n    if (*distance > radius) return 0;\n    return 1;\n}\n\nint geohashGetDistanceIfInRadiusWGS84(double x1, double y1, double x2,\n                                      double y2, double radius,\n                                      double *distance) {\n    return geohashGetDistanceIfInRadius(x1, y1, x2, y2, radius, distance);\n}\n\n/* Judge whether a point is in the axis-aligned rectangle, when the distance\n * between a searched point and the center point is less than or equal to\n * height/2 or width/2 in height and width, the point is in the rectangle.\n *\n * width_m, height_m: the rectangle\n * x1, y1 : the center of the box\n * x2, y2 : the point to be searched\n */\nint geohashGetDistanceIfInRectangle(double width_m, double height_m, double x1, double y1,\n                                    double x2, double y2, double *distance) {\n    double lon_distance = geohashGetDistance(x2, y2, x1, y2);\n    double lat_distance = geohashGetDistance(x2, y2, x2, y1);\n    if (lon_distance > width_m/2 || lat_distance > height_m/2) {\n        return 0;\n    }\n    *distance = geohashGetDistance(x1, y1, x2, y2);\n    return 1;\n}\n"
  },
  {
    "path": "src/geohash_helper.h",
    "content": "/*\n * Copyright (c) 2013-2014, yinqiwen <yinqiwen@gmail.com>\n * Copyright (c) 2014, Matt Stancliff <matt@genges.com>.\n * Copyright (c) 2015, Salvatore Sanfilippo <antirez@gmail.com>.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *  * Redistributions of source code must retain the above copyright notice,\n *    this list of conditions and the following disclaimer.\n *  * Redistributions in binary form must reproduce the above copyright\n *    notice, this list of conditions and the following disclaimer in the\n *    documentation and/or other materials provided with the distribution.\n *  * Neither the name of Redis nor the names of its contributors may be used\n *    to endorse or promote products derived from this software without\n *    specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS\n * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF\n * THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef GEOHASH_HELPER_HPP_\n#define GEOHASH_HELPER_HPP_\n\n#include \"geohash.h\"\n\n#define GZERO(s) s.bits = s.step = 0;\n#define GISZERO(s) (!s.bits && !s.step)\n#define GISNOTZERO(s) (s.bits || s.step)\n\ntypedef uint64_t GeoHashFix52Bits;\ntypedef uint64_t GeoHashVarBits;\n\ntypedef struct {\n    GeoHashBits hash;\n    GeoHashArea area;\n    GeoHashNeighbors neighbors;\n} GeoHashRadius;\n\nint GeoHashBitsComparator(const GeoHashBits *a, const GeoHashBits *b);\nuint8_t geohashEstimateStepsByRadius(double range_meters, double lat);\nint geohashBoundingBox(GeoShape *shape, double *bounds);\nGeoHashRadius geohashCalculateAreasByShapeWGS84(GeoShape *shape);\nGeoHashFix52Bits geohashAlign52Bits(const GeoHashBits hash);\ndouble geohashGetDistance(double lon1d, double lat1d,\n                          double lon2d, double lat2d);\nint geohashGetDistanceIfInRadius(double x1, double y1,\n                                 double x2, double y2, double radius,\n                                 double *distance);\nint geohashGetDistanceIfInRadiusWGS84(double x1, double y1, double x2,\n                                      double y2, double radius,\n                                      double *distance);\nint geohashGetDistanceIfInRectangle(double width_m, double height_m, double x1, double y1,\n                                    double x2, double y2, double *distance);\n\n#endif /* GEOHASH_HELPER_HPP_ */\n"
  },
  {
    "path": "src/help.h",
    "content": "/* Automatically generated by ./utils/generate-command-help.rb, do not edit. */\n\n#ifndef __REDIS_HELP_H\n#define __REDIS_HELP_H\n\nstatic char *commandGroups[] = {\n    \"generic\",\n    \"string\",\n    \"list\",\n    \"set\",\n    \"sorted_set\",\n    \"hash\",\n    \"pubsub\",\n    \"transactions\",\n    \"connection\",\n    \"server\",\n    \"scripting\",\n    \"hyperloglog\",\n    \"cluster\",\n    \"geo\",\n    \"stream\",\n    \"replication\"\n};\n\nstruct commandHelp {\n  char *name;\n  char *params;\n  char *summary;\n  int group;\n  char *since;\n} commandHelp[] = {\n    { \"ACL CAT\",\n    \"[categoryname]\",\n    \"List the ACL categories or the commands inside a category\",\n    9,\n    \"6.0.0\" },\n    { \"ACL DELUSER\",\n    \"username [username ...]\",\n    \"Remove the specified ACL users and the associated rules\",\n    9,\n    \"6.0.0\" },\n    { \"ACL GENPASS\",\n    \"[bits]\",\n    \"Generate a pseudorandom secure password to use for ACL users\",\n    9,\n    \"6.0.0\" },\n    { \"ACL GETUSER\",\n    \"username\",\n    \"Get the rules for a specific ACL user\",\n    9,\n    \"6.0.0\" },\n    { \"ACL HELP\",\n    \"-\",\n    \"Show helpful text about the different subcommands\",\n    9,\n    \"6.0.0\" },\n    { \"ACL LIST\",\n    \"-\",\n    \"List the current ACL rules in ACL config file format\",\n    9,\n    \"6.0.0\" },\n    { \"ACL LOAD\",\n    \"-\",\n    \"Reload the ACLs from the configured ACL file\",\n    9,\n    \"6.0.0\" },\n    { \"ACL LOG\",\n    \"[count or RESET]\",\n    \"List latest events denied because of ACLs in place\",\n    9,\n    \"6.0.0\" },\n    { \"ACL SAVE\",\n    \"-\",\n    \"Save the current ACL rules in the configured ACL file\",\n    9,\n    \"6.0.0\" },\n    { \"ACL SETUSER\",\n    \"username [rule [rule ...]]\",\n    \"Modify or create the rules for a specific ACL user\",\n    9,\n    \"6.0.0\" },\n    { \"ACL USERS\",\n    \"-\",\n    \"List the username of all the configured ACL rules\",\n    9,\n    \"6.0.0\" },\n    { \"ACL WHOAMI\",\n    \"-\",\n    \"Return the name of the user associated to the current connection\",\n    9,\n    \"6.0.0\" },\n    { \"APPEND\",\n    \"key value\",\n    \"Append a value to a key\",\n    1,\n    \"2.0.0\" },\n    { \"AUTH\",\n    \"[username] password\",\n    \"Authenticate to the server\",\n    8,\n    \"1.0.0\" },\n    { \"BGREWRITEAOF\",\n    \"-\",\n    \"Asynchronously rewrite the append-only file\",\n    9,\n    \"1.0.0\" },\n    { \"BGSAVE\",\n    \"[SCHEDULE]\",\n    \"Asynchronously save the dataset to disk\",\n    9,\n    \"1.0.0\" },\n    { \"BITCOUNT\",\n    \"key [start end]\",\n    \"Count set bits in a string\",\n    1,\n    \"2.6.0\" },\n    { \"BITFIELD\",\n    \"key [GET type offset] [SET type offset value] [INCRBY type offset increment] [OVERFLOW WRAP|SAT|FAIL]\",\n    \"Perform arbitrary bitfield integer operations on strings\",\n    1,\n    \"3.2.0\" },\n    { \"BITOP\",\n    \"operation destkey key [key ...]\",\n    \"Perform bitwise operations between strings\",\n    1,\n    \"2.6.0\" },\n    { \"BITPOS\",\n    \"key bit [start] [end]\",\n    \"Find first bit set or clear in a string\",\n    1,\n    \"2.8.7\" },\n    { \"BLMOVE\",\n    \"source destination LEFT|RIGHT LEFT|RIGHT timeout\",\n    \"Pop an element from a list, push it to another list and return it; or block until one is available\",\n    2,\n    \"6.2.0\" },\n    { \"BLPOP\",\n    \"key [key ...] timeout\",\n    \"Remove and get the first element in a list, or block until one is available\",\n    2,\n    \"2.0.0\" },\n    { \"BRPOP\",\n    \"key [key ...] timeout\",\n    \"Remove and get the last element in a list, or block until one is available\",\n    2,\n    \"2.0.0\" },\n    { \"BRPOPLPUSH\",\n    \"source destination timeout\",\n    \"Pop an element from a list, push it to another list and return it; or block until one is available\",\n    2,\n    \"2.2.0\" },\n    { \"BZPOPMAX\",\n    \"key [key ...] timeout\",\n    \"Remove and return the member with the highest score from one or more sorted sets, or block until one is available\",\n    4,\n    \"5.0.0\" },\n    { \"BZPOPMIN\",\n    \"key [key ...] timeout\",\n    \"Remove and return the member with the lowest score from one or more sorted sets, or block until one is available\",\n    4,\n    \"5.0.0\" },\n    { \"CLIENT CACHING\",\n    \"YES|NO\",\n    \"Instruct the server about tracking or not keys in the next request\",\n    8,\n    \"6.0.0\" },\n    { \"CLIENT GETNAME\",\n    \"-\",\n    \"Get the current connection name\",\n    8,\n    \"2.6.9\" },\n    { \"CLIENT GETREDIR\",\n    \"-\",\n    \"Get tracking notifications redirection client ID if any\",\n    8,\n    \"6.0.0\" },\n    { \"CLIENT ID\",\n    \"-\",\n    \"Returns the client ID for the current connection\",\n    8,\n    \"5.0.0\" },\n    { \"CLIENT INFO\",\n    \"-\",\n    \"Returns information about the current client connection.\",\n    8,\n    \"6.2.0\" },\n    { \"CLIENT KILL\",\n    \"[ip:port] [ID client-id] [TYPE normal|master|slave|pubsub] [USER username] [ADDR ip:port] [LADDR ip:port] [SKIPME yes/no]\",\n    \"Kill the connection of a client\",\n    8,\n    \"2.4.0\" },\n    { \"CLIENT LIST\",\n    \"[TYPE normal|master|replica|pubsub] [ID client-id [client-id ...]]\",\n    \"Get the list of client connections\",\n    8,\n    \"2.4.0\" },\n    { \"CLIENT PAUSE\",\n    \"timeout [WRITE|ALL]\",\n    \"Stop processing commands from clients for some time\",\n    8,\n    \"2.9.50\" },\n    { \"CLIENT REPLY\",\n    \"ON|OFF|SKIP\",\n    \"Instruct the server whether to reply to commands\",\n    8,\n    \"3.2.0\" },\n    { \"CLIENT SETNAME\",\n    \"connection-name\",\n    \"Set the current connection name\",\n    8,\n    \"2.6.9\" },\n    { \"CLIENT TRACKING\",\n    \"ON|OFF [REDIRECT client-id] [PREFIX prefix [PREFIX prefix ...]] [BCAST] [OPTIN] [OPTOUT] [NOLOOP]\",\n    \"Enable or disable server assisted client side caching support\",\n    8,\n    \"6.0.0\" },\n    { \"CLIENT TRACKINGINFO\",\n    \"-\",\n    \"Return information about server assisted client side caching for the current connection\",\n    8,\n    \"6.2.0\" },\n    { \"CLIENT UNBLOCK\",\n    \"client-id [TIMEOUT|ERROR]\",\n    \"Unblock a client blocked in a blocking command from a different connection\",\n    8,\n    \"5.0.0\" },\n    { \"CLIENT UNPAUSE\",\n    \"-\",\n    \"Resume processing of clients that were paused\",\n    8,\n    \"6.2.0\" },\n    { \"CLUSTER ADDSLOTS\",\n    \"slot [slot ...]\",\n    \"Assign new hash slots to receiving node\",\n    12,\n    \"3.0.0\" },\n    { \"CLUSTER BUMPEPOCH\",\n    \"-\",\n    \"Advance the cluster config epoch\",\n    12,\n    \"3.0.0\" },\n    { \"CLUSTER COUNT-FAILURE-REPORTS\",\n    \"node-id\",\n    \"Return the number of failure reports active for a given node\",\n    12,\n    \"3.0.0\" },\n    { \"CLUSTER COUNTKEYSINSLOT\",\n    \"slot\",\n    \"Return the number of local keys in the specified hash slot\",\n    12,\n    \"3.0.0\" },\n    { \"CLUSTER DELSLOTS\",\n    \"slot [slot ...]\",\n    \"Set hash slots as unbound in receiving node\",\n    12,\n    \"3.0.0\" },\n    { \"CLUSTER FAILOVER\",\n    \"[FORCE|TAKEOVER]\",\n    \"Forces a replica to perform a manual failover of its master.\",\n    12,\n    \"3.0.0\" },\n    { \"CLUSTER FLUSHSLOTS\",\n    \"-\",\n    \"Delete a node's own slots information\",\n    12,\n    \"3.0.0\" },\n    { \"CLUSTER FORGET\",\n    \"node-id\",\n    \"Remove a node from the nodes table\",\n    12,\n    \"3.0.0\" },\n    { \"CLUSTER GETKEYSINSLOT\",\n    \"slot count\",\n    \"Return local key names in the specified hash slot\",\n    12,\n    \"3.0.0\" },\n    { \"CLUSTER INFO\",\n    \"-\",\n    \"Provides info about KeyDB Cluster node state\",\n    12,\n    \"3.0.0\" },\n    { \"CLUSTER KEYSLOT\",\n    \"key\",\n    \"Returns the hash slot of the specified key\",\n    12,\n    \"3.0.0\" },\n    { \"CLUSTER MEET\",\n    \"ip port\",\n    \"Force a node cluster to handshake with another node\",\n    12,\n    \"3.0.0\" },\n    { \"CLUSTER MYID\",\n    \"-\",\n    \"Return the node id\",\n    12,\n    \"3.0.0\" },\n    { \"CLUSTER NODES\",\n    \"-\",\n    \"Get Cluster config for the node\",\n    12,\n    \"3.0.0\" },\n    { \"CLUSTER REPLICAS\",\n    \"node-id\",\n    \"List replica nodes of the specified master node\",\n    12,\n    \"5.0.0\" },\n    { \"CLUSTER REPLICATE\",\n    \"node-id\",\n    \"Reconfigure a node as a replica of the specified master node\",\n    12,\n    \"3.0.0\" },\n    { \"CLUSTER RESET\",\n    \"[HARD|SOFT]\",\n    \"Reset a KeyDB Cluster node\",\n    12,\n    \"3.0.0\" },\n    { \"CLUSTER SAVECONFIG\",\n    \"-\",\n    \"Forces the node to save cluster state on disk\",\n    12,\n    \"3.0.0\" },\n    { \"CLUSTER SET-CONFIG-EPOCH\",\n    \"config-epoch\",\n    \"Set the configuration epoch in a new node\",\n    12,\n    \"3.0.0\" },\n    { \"CLUSTER SETSLOT\",\n    \"slot IMPORTING|MIGRATING|STABLE|NODE [node-id]\",\n    \"Bind a hash slot to a specific node\",\n    12,\n    \"3.0.0\" },\n    { \"CLUSTER SLAVES\",\n    \"node-id\",\n    \"List replica nodes of the specified master node\",\n    12,\n    \"3.0.0\" },\n    { \"CLUSTER SLOTS\",\n    \"-\",\n    \"Get array of Cluster slot to node mappings\",\n    12,\n    \"3.0.0\" },\n    { \"COMMAND\",\n    \"-\",\n    \"Get array of KeyDB command details\",\n    9,\n    \"2.8.13\" },\n    { \"COMMAND COUNT\",\n    \"-\",\n    \"Get total number of KeyDB commands\",\n    9,\n    \"2.8.13\" },\n    { \"COMMAND GETKEYS\",\n    \"-\",\n    \"Extract keys given a full KeyDB command\",\n    9,\n    \"2.8.13\" },\n    { \"COMMAND INFO\",\n    \"command-name [command-name ...]\",\n    \"Get array of specific KeyDB command details\",\n    9,\n    \"2.8.13\" },\n    { \"CONFIG GET\",\n    \"parameter\",\n    \"Get the value of a configuration parameter\",\n    9,\n    \"2.0.0\" },\n    { \"CONFIG RESETSTAT\",\n    \"-\",\n    \"Reset the stats returned by INFO\",\n    9,\n    \"2.0.0\" },\n    { \"CONFIG REWRITE\",\n    \"-\",\n    \"Rewrite the configuration file with the in memory configuration\",\n    9,\n    \"2.8.0\" },\n    { \"CONFIG SET\",\n    \"parameter value\",\n    \"Set a configuration parameter to the given value\",\n    9,\n    \"2.0.0\" },\n    { \"COPY\",\n    \"source destination [DB destination-db] [REPLACE]\",\n    \"Copy a key\",\n    0,\n    \"6.2.0\" },\n    { \"DBSIZE\",\n    \"-\",\n    \"Return the number of keys in the selected database\",\n    9,\n    \"1.0.0\" },\n    { \"DEBUG OBJECT\",\n    \"key\",\n    \"Get debugging information about a key\",\n    9,\n    \"1.0.0\" },\n    { \"DEBUG SEGFAULT\",\n    \"-\",\n    \"Make the server crash\",\n    9,\n    \"1.0.0\" },\n    { \"DECR\",\n    \"key\",\n    \"Decrement the integer value of a key by one\",\n    1,\n    \"1.0.0\" },\n    { \"DECRBY\",\n    \"key decrement\",\n    \"Decrement the integer value of a key by the given number\",\n    1,\n    \"1.0.0\" },\n    { \"DEL\",\n    \"key [key ...]\",\n    \"Delete a key\",\n    0,\n    \"1.0.0\" },\n    { \"DISCARD\",\n    \"-\",\n    \"Discard all commands issued after MULTI\",\n    7,\n    \"2.0.0\" },\n    { \"DUMP\",\n    \"key\",\n    \"Return a serialized version of the value stored at the specified key.\",\n    0,\n    \"2.6.0\" },\n    { \"ECHO\",\n    \"message\",\n    \"Echo the given string\",\n    8,\n    \"1.0.0\" },\n    { \"EVAL\",\n    \"script numkeys key [key ...] arg [arg ...]\",\n    \"Execute a Lua script server side\",\n    10,\n    \"2.6.0\" },\n    { \"EVALSHA\",\n    \"sha1 numkeys key [key ...] arg [arg ...]\",\n    \"Execute a Lua script server side\",\n    10,\n    \"2.6.0\" },\n    { \"EXEC\",\n    \"-\",\n    \"Execute all commands issued after MULTI\",\n    7,\n    \"1.2.0\" },\n    { \"EXISTS\",\n    \"key [key ...]\",\n    \"Determine if a key exists\",\n    0,\n    \"1.0.0\" },\n    { \"EXPIRE\",\n    \"key seconds\",\n    \"Set a key's time to live in seconds\",\n    0,\n    \"1.0.0\" },\n    { \"EXPIREAT\",\n    \"key timestamp\",\n    \"Set the expiration for a key as a UNIX timestamp\",\n    0,\n    \"1.2.0\" },\n    { \"EXPIREMEMBER\",\n    \"key subkey delay [Unit: s,ms]\",\n    \"set a subkey's time to live in seconds (or milliseconds)\"},\n    { \"EXPIREMEMBERAT\",\n    \"key subkey timestamp\",\n    \"Set the expiration for a subkey as a UNIX timestamp\"},\n    { \"FAILOVER\",\n    \"[TO host port [FORCE]] [ABORT] [TIMEOUT milliseconds]\",\n    \"Start a coordinated failover between this server and one of its replicas.\",\n    9,\n    \"6.2.0\" },\n    { \"FLUSHALL\",\n    \"[ASYNC|SYNC]\",\n    \"Remove all keys from all databases\",\n    9,\n    \"1.0.0\" },\n    { \"FLUSHDB\",\n    \"[ASYNC|SYNC]\",\n    \"Remove all keys from the current database\",\n    9,\n    \"1.0.0\" },\n    { \"GEOADD\",\n    \"key [NX|XX] [CH] longitude latitude member [longitude latitude member ...]\",\n    \"Add one or more geospatial items in the geospatial index represented using a sorted set\",\n    13,\n    \"3.2.0\" },\n    { \"GEODIST\",\n    \"key member1 member2 [m|km|ft|mi]\",\n    \"Returns the distance between two members of a geospatial index\",\n    13,\n    \"3.2.0\" },\n    { \"GEOHASH\",\n    \"key member [member ...]\",\n    \"Returns members of a geospatial index as standard geohash strings\",\n    13,\n    \"3.2.0\" },\n    { \"GEOPOS\",\n    \"key member [member ...]\",\n    \"Returns longitude and latitude of members of a geospatial index\",\n    13,\n    \"3.2.0\" },\n    { \"GEORADIUS\",\n    \"key longitude latitude radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count [ANY]] [ASC|DESC] [STORE key] [STOREDIST key]\",\n    \"Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a point\",\n    13,\n    \"3.2.0\" },\n    { \"GEORADIUSBYMEMBER\",\n    \"key member radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count [ANY]] [ASC|DESC] [STORE key] [STOREDIST key]\",\n    \"Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a member\",\n    13,\n    \"3.2.0\" },\n    { \"GEOSEARCH\",\n    \"key [FROMMEMBER member] [FROMLONLAT longitude latitude] [BYRADIUS radius m|km|ft|mi] [BYBOX width height m|km|ft|mi] [ASC|DESC] [COUNT count [ANY]] [WITHCOORD] [WITHDIST] [WITHHASH]\",\n    \"Query a sorted set representing a geospatial index to fetch members inside an area of a box or a circle.\",\n    13,\n    \"6.2\" },\n    { \"GEOSEARCHSTORE\",\n    \"destination source [FROMMEMBER member] [FROMLONLAT longitude latitude] [BYRADIUS radius m|km|ft|mi] [BYBOX width height m|km|ft|mi] [ASC|DESC] [COUNT count [ANY]] [WITHCOORD] [WITHDIST] [WITHHASH] [STOREDIST]\",\n    \"Query a sorted set representing a geospatial index to fetch members inside an area of a box or a circle, and store the result in another key.\",\n    13,\n    \"6.2\" },\n    { \"GET\",\n    \"key\",\n    \"Get the value of a key\",\n    1,\n    \"1.0.0\" },\n    { \"GETBIT\",\n    \"key offset\",\n    \"Returns the bit value at offset in the string value stored at key\",\n    1,\n    \"2.2.0\" },\n    { \"GETDEL\",\n    \"key\",\n    \"Get the value of a key and delete the key\",\n    1,\n    \"6.2.0\" },\n    { \"GETEX\",\n    \"key [EX seconds|PX milliseconds|EXAT timestamp|PXAT milliseconds-timestamp|PERSIST]\",\n    \"Get the value of a key and optionally set its expiration\",\n    1,\n    \"6.2.0\" },\n    { \"GETRANGE\",\n    \"key start end\",\n    \"Get a substring of the string stored at a key\",\n    1,\n    \"2.4.0\" },\n    { \"GETSET\",\n    \"key value\",\n    \"Set the string value of a key and return its old value\",\n    1,\n    \"1.0.0\" },\n    { \"HDEL\",\n    \"key field [field ...]\",\n    \"Delete one or more hash fields\",\n    5,\n    \"2.0.0\" },\n    { \"HELLO\",\n    \"[protover [AUTH username password] [SETNAME clientname]]\",\n    \"Handshake with KeyDB\",\n    8,\n    \"6.0.0\" },\n    { \"HEXISTS\",\n    \"key field\",\n    \"Determine if a hash field exists\",\n    5,\n    \"2.0.0\" },\n    { \"HGET\",\n    \"key field\",\n    \"Get the value of a hash field\",\n    5,\n    \"2.0.0\" },\n    { \"HGETALL\",\n    \"key\",\n    \"Get all the fields and values in a hash\",\n    5,\n    \"2.0.0\" },\n    { \"HINCRBY\",\n    \"key field increment\",\n    \"Increment the integer value of a hash field by the given number\",\n    5,\n    \"2.0.0\" },\n    { \"HINCRBYFLOAT\",\n    \"key field increment\",\n    \"Increment the float value of a hash field by the given amount\",\n    5,\n    \"2.6.0\" },\n    { \"HKEYS\",\n    \"key\",\n    \"Get all the fields in a hash\",\n    5,\n    \"2.0.0\" },\n    { \"HLEN\",\n    \"key\",\n    \"Get the number of fields in a hash\",\n    5,\n    \"2.0.0\" },\n    { \"HMGET\",\n    \"key field [field ...]\",\n    \"Get the values of all the given hash fields\",\n    5,\n    \"2.0.0\" },\n    { \"HMSET\",\n    \"key field value [field value ...]\",\n    \"Set multiple hash fields to multiple values\",\n    5,\n    \"2.0.0\" },\n    { \"HRANDFIELD\",\n    \"key [count [WITHVALUES]]\",\n    \"Get one or multiple random fields from a hash\",\n    5,\n    \"6.2.0\" },\n    { \"HSCAN\",\n    \"key cursor [MATCH pattern] [COUNT count]\",\n    \"Incrementally iterate hash fields and associated values\",\n    5,\n    \"2.8.0\" },\n    { \"HSET\",\n    \"key field value [field value ...]\",\n    \"Set the string value of a hash field\",\n    5,\n    \"2.0.0\" },\n    { \"HSETNX\",\n    \"key field value\",\n    \"Set the value of a hash field, only if the field does not exist\",\n    5,\n    \"2.0.0\" },\n    { \"HSTRLEN\",\n    \"key field\",\n    \"Get the length of the value of a hash field\",\n    5,\n    \"3.2.0\" },\n    { \"HVALS\",\n    \"key\",\n    \"Get all the values in a hash\",\n    5,\n    \"2.0.0\" },\n    { \"INCR\",\n    \"key\",\n    \"Increment the integer value of a key by one\",\n    1,\n    \"1.0.0\" },\n    { \"INCRBY\",\n    \"key increment\",\n    \"Increment the integer value of a key by the given amount\",\n    1,\n    \"1.0.0\" },\n    { \"INCRBYFLOAT\",\n    \"key increment\",\n    \"Increment the float value of a key by the given amount\",\n    1,\n    \"2.6.0\" },\n    { \"INFO\",\n    \"[section]\",\n    \"Get information and statistics about the server\",\n    9,\n    \"1.0.0\" },\n    { \"KEYS\",\n    \"pattern\",\n    \"Find all keys matching the given pattern\",\n    0,\n    \"1.0.0\" },\n    { \"LASTSAVE\",\n    \"-\",\n    \"Get the UNIX time stamp of the last successful save to disk\",\n    9,\n    \"1.0.0\" },\n    { \"LATENCY DOCTOR\",\n    \"-\",\n    \"Return a human readable latency analysis report.\",\n    9,\n    \"2.8.13\" },\n    { \"LATENCY GRAPH\",\n    \"event\",\n    \"Return a latency graph for the event.\",\n    9,\n    \"2.8.13\" },\n    { \"LATENCY HELP\",\n    \"-\",\n    \"Show helpful text about the different subcommands.\",\n    9,\n    \"2.8.13\" },\n    { \"LATENCY HISTORY\",\n    \"event\",\n    \"Return timestamp-latency samples for the event.\",\n    9,\n    \"2.8.13\" },\n    { \"LATENCY LATEST\",\n    \"-\",\n    \"Return the latest latency samples for all events.\",\n    9,\n    \"2.8.13\" },\n    { \"LATENCY RESET\",\n    \"[event [event ...]]\",\n    \"Reset latency data for one or more events.\",\n    9,\n    \"2.8.13\" },\n    { \"LINDEX\",\n    \"key index\",\n    \"Get an element from a list by its index\",\n    2,\n    \"1.0.0\" },\n    { \"LINSERT\",\n    \"key BEFORE|AFTER pivot element\",\n    \"Insert an element before or after another element in a list\",\n    2,\n    \"2.2.0\" },\n    { \"LLEN\",\n    \"key\",\n    \"Get the length of a list\",\n    2,\n    \"1.0.0\" },\n    { \"LMOVE\",\n    \"source destination LEFT|RIGHT LEFT|RIGHT\",\n    \"Pop an element from a list, push it to another list and return it\",\n    2,\n    \"6.2.0\" },\n    { \"LOLWUT\",\n    \"[VERSION version]\",\n    \"Display some computer art and the KeyDB version\",\n    9,\n    \"5.0.0\" },\n    { \"LPOP\",\n    \"key [count]\",\n    \"Remove and get the first elements in a list\",\n    2,\n    \"1.0.0\" },\n    { \"LPOS\",\n    \"key element [RANK rank] [COUNT num-matches] [MAXLEN len]\",\n    \"Return the index of matching elements on a list\",\n    2,\n    \"6.0.6\" },\n    { \"LPUSH\",\n    \"key element [element ...]\",\n    \"Prepend one or multiple elements to a list\",\n    2,\n    \"1.0.0\" },\n    { \"LPUSHX\",\n    \"key element [element ...]\",\n    \"Prepend an element to a list, only if the list exists\",\n    2,\n    \"2.2.0\" },\n    { \"LRANGE\",\n    \"key start stop\",\n    \"Get a range of elements from a list\",\n    2,\n    \"1.0.0\" },\n    { \"LREM\",\n    \"key count element\",\n    \"Remove elements from a list\",\n    2,\n    \"1.0.0\" },\n    { \"LSET\",\n    \"key index element\",\n    \"Set the value of an element in a list by its index\",\n    2,\n    \"1.0.0\" },\n    { \"LTRIM\",\n    \"key start stop\",\n    \"Trim a list to the specified range\",\n    2,\n    \"1.0.0\" },\n    { \"MEMORY DOCTOR\",\n    \"-\",\n    \"Outputs memory problems report\",\n    9,\n    \"4.0.0\" },\n    { \"MEMORY HELP\",\n    \"-\",\n    \"Show helpful text about the different subcommands\",\n    9,\n    \"4.0.0\" },\n    { \"MEMORY MALLOC-STATS\",\n    \"-\",\n    \"Show allocator internal stats\",\n    9,\n    \"4.0.0\" },\n    { \"MEMORY PURGE\",\n    \"-\",\n    \"Ask the allocator to release memory\",\n    9,\n    \"4.0.0\" },\n    { \"MEMORY STATS\",\n    \"-\",\n    \"Show memory usage details\",\n    9,\n    \"4.0.0\" },\n    { \"MEMORY USAGE\",\n    \"key [SAMPLES count]\",\n    \"Estimate the memory usage of a key\",\n    9,\n    \"4.0.0\" },\n    { \"MGET\",\n    \"key [key ...]\",\n    \"Get the values of all the given keys\",\n    1,\n    \"1.0.0\" },\n    { \"MIGRATE\",\n    \"host port key|\"\" destination-db timeout [COPY] [REPLACE] [AUTH password] [AUTH2 username password] [KEYS key]\",\n    \"Atomically transfer a key from a KeyDB instance to another one.\",\n    0,\n    \"2.6.0\" },\n    { \"MODULE LIST\",\n    \"-\",\n    \"List all modules loaded by the server\",\n    9,\n    \"4.0.0\" },\n    { \"MODULE LOAD\",\n    \"path [arg]\",\n    \"Load a module\",\n    9,\n    \"4.0.0\" },\n    { \"MODULE UNLOAD\",\n    \"name\",\n    \"Unload a module\",\n    9,\n    \"4.0.0\" },\n    { \"MONITOR\",\n    \"-\",\n    \"Listen for all requests received by the server in real time\",\n    9,\n    \"1.0.0\" },\n    { \"MOVE\",\n    \"key db\",\n    \"Move a key to another database\",\n    0,\n    \"1.0.0\" },\n    { \"MSET\",\n    \"key value [key value ...]\",\n    \"Set multiple keys to multiple values\",\n    1,\n    \"1.0.1\" },\n    { \"MSETNX\",\n    \"key value [key value ...]\",\n    \"Set multiple keys to multiple values, only if none of the keys exist\",\n    1,\n    \"1.0.1\" },\n    { \"MULTI\",\n    \"-\",\n    \"Mark the start of a transaction block\",\n    7,\n    \"1.2.0\" },\n    { \"OBJECT\",\n    \"subcommand [arguments [arguments ...]]\",\n    \"Inspect the internals of KeyDB objects\",\n    0,\n    \"2.2.3\" },\n    { \"PERSIST\",\n    \"key [subkey]\",\n    \"Remove the expiration from a key or subkey\",\n    0,\n    \"2.2.0\" },\n    { \"PEXPIRE\",\n    \"key milliseconds\",\n    \"Set a key's time to live in milliseconds\",\n    0,\n    \"2.6.0\" },\n    { \"PEXPIREAT\",\n    \"key milliseconds-timestamp\",\n    \"Set the expiration for a key as a UNIX timestamp specified in milliseconds\",\n    0,\n    \"2.6.0\" },\n    { \"PFADD\",\n    \"key element [element ...]\",\n    \"Adds the specified elements to the specified HyperLogLog.\",\n    11,\n    \"2.8.9\" },\n    { \"PFCOUNT\",\n    \"key [key ...]\",\n    \"Return the approximated cardinality of the set(s) observed by the HyperLogLog at key(s).\",\n    11,\n    \"2.8.9\" },\n    { \"PFMERGE\",\n    \"destkey sourcekey [sourcekey ...]\",\n    \"Merge N different HyperLogLogs into a single one.\",\n    11,\n    \"2.8.9\" },\n    { \"PING\",\n    \"[message]\",\n    \"Ping the server\",\n    8,\n    \"1.0.0\" },\n    { \"PSETEX\",\n    \"key milliseconds value\",\n    \"Set the value and expiration in milliseconds of a key\",\n    1,\n    \"2.6.0\" },\n    { \"PSUBSCRIBE\",\n    \"pattern [pattern ...]\",\n    \"Listen for messages published to channels matching the given patterns\",\n    6,\n    \"2.0.0\" },\n    { \"PSYNC\",\n    \"replicationid offset\",\n    \"Internal command used for replication\",\n    9,\n    \"2.8.0\" },\n    { \"PTTL\",\n    \"key [subkey]\",\n    \"Get the time to live for a key or subkey in milliseconds\",\n    0,\n    \"2.6.0\" },\n    { \"PUBLISH\",\n    \"channel message\",\n    \"Post a message to a channel\",\n    6,\n    \"2.0.0\" },\n    { \"PUBSUB\",\n    \"subcommand [argument [argument ...]]\",\n    \"Inspect the state of the Pub/Sub subsystem\",\n    6,\n    \"2.8.0\" },\n    { \"PUNSUBSCRIBE\",\n    \"[pattern [pattern ...]]\",\n    \"Stop listening for messages posted to channels matching the given patterns\",\n    6,\n    \"2.0.0\" },\n    { \"QUIT\",\n    \"-\",\n    \"Close the connection\",\n    8,\n    \"1.0.0\" },\n    { \"RANDOMKEY\",\n    \"-\",\n    \"Return a random key from the keyspace\",\n    0,\n    \"1.0.0\" },\n    { \"READONLY\",\n    \"-\",\n    \"Enables read queries for a connection to a cluster replica node\",\n    12,\n    \"3.0.0\" },\n    { \"READWRITE\",\n    \"-\",\n    \"Disables read queries for a connection to a cluster replica node\",\n    12,\n    \"3.0.0\" },\n    { \"RENAME\",\n    \"key newkey\",\n    \"Rename a key\",\n    0,\n    \"1.0.0\" },\n    { \"RENAMENX\",\n    \"key newkey\",\n    \"Rename a key, only if the new key does not exist\",\n    0,\n    \"1.0.0\" },\n    { \"REPLICAOF\",\n    \"host port\",\n    \"Make the server a replica of another instance, or promote it as master.\",\n    9,\n    \"5.0.0\" },\n    { \"RESET\",\n    \"-\",\n    \"Reset the connection\",\n    8,\n    \"6.2\" },\n    { \"RESTORE\",\n    \"key ttl serialized-value [REPLACE] [ABSTTL] [IDLETIME seconds] [FREQ frequency]\",\n    \"Create a key using the provided serialized value, previously obtained using DUMP.\",\n    0,\n    \"2.6.0\" },\n    { \"ROLE\",\n    \"-\",\n    \"Return the role of the instance in the context of replication\",\n    9,\n    \"2.8.12\" },\n    { \"RPOP\",\n    \"key [count]\",\n    \"Remove and get the last elements in a list\",\n    2,\n    \"1.0.0\" },\n    { \"RPOPLPUSH\",\n    \"source destination\",\n    \"Remove the last element in a list, prepend it to another list and return it\",\n    2,\n    \"1.2.0\" },\n    { \"RPUSH\",\n    \"key element [element ...]\",\n    \"Append one or multiple elements to a list\",\n    2,\n    \"1.0.0\" },\n    { \"RPUSHX\",\n    \"key element [element ...]\",\n    \"Append an element to a list, only if the list exists\",\n    2,\n    \"2.2.0\" },\n    { \"SADD\",\n    \"key member [member ...]\",\n    \"Add one or more members to a set\",\n    3,\n    \"1.0.0\" },\n    { \"SAVE\",\n    \"-\",\n    \"Synchronously save the dataset to disk\",\n    9,\n    \"1.0.0\" },\n    { \"SCAN\",\n    \"cursor [MATCH pattern] [COUNT count] [TYPE type]\",\n    \"Incrementally iterate the keys space\",\n    0,\n    \"2.8.0\" },\n    { \"SCARD\",\n    \"key\",\n    \"Get the number of members in a set\",\n    3,\n    \"1.0.0\" },\n    { \"SCRIPT DEBUG\",\n    \"YES|SYNC|NO\",\n    \"Set the debug mode for executed scripts.\",\n    10,\n    \"3.2.0\" },\n    { \"SCRIPT EXISTS\",\n    \"sha1 [sha1 ...]\",\n    \"Check existence of scripts in the script cache.\",\n    10,\n    \"2.6.0\" },\n    { \"SCRIPT FLUSH\",\n    \"[ASYNC|SYNC]\",\n    \"Remove all the scripts from the script cache.\",\n    10,\n    \"2.6.0\" },\n    { \"SCRIPT KILL\",\n    \"-\",\n    \"Kill the script currently in execution.\",\n    10,\n    \"2.6.0\" },\n    { \"SCRIPT LOAD\",\n    \"script\",\n    \"Load the specified Lua script into the script cache.\",\n    10,\n    \"2.6.0\" },\n    { \"SDIFF\",\n    \"key [key ...]\",\n    \"Subtract multiple sets\",\n    3,\n    \"1.0.0\" },\n    { \"SDIFFSTORE\",\n    \"destination key [key ...]\",\n    \"Subtract multiple sets and store the resulting set in a key\",\n    3,\n    \"1.0.0\" },\n    { \"SELECT\",\n    \"index\",\n    \"Change the selected database for the current connection\",\n    8,\n    \"1.0.0\" },\n    { \"SET\",\n    \"key value [EX seconds|PX milliseconds|EXAT timestamp|PXAT milliseconds-timestamp|KEEPTTL] [NX|XX] [GET]\",\n    \"Set the string value of a key\",\n    1,\n    \"1.0.0\" },\n    { \"SETBIT\",\n    \"key offset value\",\n    \"Sets or clears the bit at offset in the string value stored at key\",\n    1,\n    \"2.2.0\" },\n    { \"SETEX\",\n    \"key seconds value\",\n    \"Set the value and expiration of a key\",\n    1,\n    \"2.0.0\" },\n    { \"SETNX\",\n    \"key value\",\n    \"Set the value of a key, only if the key does not exist\",\n    1,\n    \"1.0.0\" },\n    { \"SETRANGE\",\n    \"key offset value\",\n    \"Overwrite part of a string at key starting at the specified offset\",\n    1,\n    \"2.2.0\" },\n    { \"SHUTDOWN\",\n    \"[NOSAVE|SAVE|SOFT]\",\n    \"Synchronously save the dataset to disk and then shut down the server\",\n    9,\n    \"1.0.0\" },\n    { \"SINTER\",\n    \"key [key ...]\",\n    \"Intersect multiple sets\",\n    3,\n    \"1.0.0\" },\n    { \"SINTERSTORE\",\n    \"destination key [key ...]\",\n    \"Intersect multiple sets and store the resulting set in a key\",\n    3,\n    \"1.0.0\" },\n    { \"SISMEMBER\",\n    \"key member\",\n    \"Determine if a given value is a member of a set\",\n    3,\n    \"1.0.0\" },\n    { \"SLAVEOF\",\n    \"host port\",\n    \"Make the server a replica of another instance, or promote it as master. Deprecated starting with KeyDB 5. Use REPLICAOF instead.\",\n    9,\n    \"1.0.0\" },\n    { \"SLOWLOG\",\n    \"subcommand [argument]\",\n    \"Manages the KeyDB slow queries log\",\n    9,\n    \"2.2.12\" },\n    { \"SMEMBERS\",\n    \"key\",\n    \"Get all the members in a set\",\n    3,\n    \"1.0.0\" },\n    { \"SMISMEMBER\",\n    \"key member [member ...]\",\n    \"Returns the membership associated with the given elements for a set\",\n    3,\n    \"6.2.0\" },\n    { \"SMOVE\",\n    \"source destination member\",\n    \"Move a member from one set to another\",\n    3,\n    \"1.0.0\" },\n    { \"SORT\",\n    \"key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] [STORE destination]\",\n    \"Sort the elements in a list, set or sorted set\",\n    0,\n    \"1.0.0\" },\n    { \"SPOP\",\n    \"key [count]\",\n    \"Remove and return one or multiple random members from a set\",\n    3,\n    \"1.0.0\" },\n    { \"SRANDMEMBER\",\n    \"key [count]\",\n    \"Get one or multiple random members from a set\",\n    3,\n    \"1.0.0\" },\n    { \"SREM\",\n    \"key member [member ...]\",\n    \"Remove one or more members from a set\",\n    3,\n    \"1.0.0\" },\n    { \"SSCAN\",\n    \"key cursor [MATCH pattern] [COUNT count]\",\n    \"Incrementally iterate Set elements\",\n    3,\n    \"2.8.0\" },\n    { \"STRALGO\",\n    \"LCS algo-specific-argument [algo-specific-argument ...]\",\n    \"Run algorithms (currently LCS) against strings\",\n    1,\n    \"6.0.0\" },\n    { \"STRLEN\",\n    \"key\",\n    \"Get the length of the value stored in a key\",\n    1,\n    \"2.2.0\" },\n    { \"SUBSCRIBE\",\n    \"channel [channel ...]\",\n    \"Listen for messages published to the given channels\",\n    6,\n    \"2.0.0\" },\n    { \"SUNION\",\n    \"key [key ...]\",\n    \"Add multiple sets\",\n    3,\n    \"1.0.0\" },\n    { \"SUNIONSTORE\",\n    \"destination key [key ...]\",\n    \"Add multiple sets and store the resulting set in a key\",\n    3,\n    \"1.0.0\" },\n    { \"SWAPDB\",\n    \"index1 index2\",\n    \"Swaps two KeyDB databases\",\n    9,\n    \"4.0.0\" },\n    { \"SYNC\",\n    \"-\",\n    \"Internal command used for replication\",\n    9,\n    \"1.0.0\" },\n    { \"TIME\",\n    \"-\",\n    \"Return the current server time\",\n    9,\n    \"2.6.0\" },\n    { \"TOUCH\",\n    \"key [key ...]\",\n    \"Alters the last access time of a key(s). Returns the number of existing keys specified.\",\n    0,\n    \"3.2.1\" },\n    { \"TTL\",\n    \"key [subkey]\",\n    \"Get the time to live for a key or subkey\",\n    0,\n    \"1.0.0\" },\n    { \"TYPE\",\n    \"key\",\n    \"Determine the type stored at key\",\n    0,\n    \"1.0.0\" },\n    { \"UNLINK\",\n    \"key [key ...]\",\n    \"Delete a key asynchronously in another thread. Otherwise it is just as DEL, but non blocking.\",\n    0,\n    \"4.0.0\" },\n    { \"UNSUBSCRIBE\",\n    \"[channel [channel ...]]\",\n    \"Stop listening for messages posted to the given channels\",\n    6,\n    \"2.0.0\" },\n    { \"UNWATCH\",\n    \"-\",\n    \"Forget about all watched keys\",\n    7,\n    \"2.2.0\" },\n    { \"WAIT\",\n    \"numreplicas timeout\",\n    \"Wait for the synchronous replication of all the write commands sent in the context of the current connection\",\n    0,\n    \"3.0.0\" },\n    { \"WATCH\",\n    \"key [key ...]\",\n    \"Watch the given keys to determine execution of the MULTI/EXEC block\",\n    7,\n    \"2.2.0\" },\n    { \"XACK\",\n    \"key group ID [ID ...]\",\n    \"Marks a pending message as correctly processed, effectively removing it from the pending entries list of the consumer group. Return value of the command is the number of messages successfully acknowledged, that is, the IDs we were actually able to resolve in the PEL.\",\n    14,\n    \"5.0.0\" },\n    { \"XADD\",\n    \"key [NOMKSTREAM] [MAXLEN|MINID [=|~] threshold [LIMIT count]] *|ID field value [field value ...]\",\n    \"Appends a new entry to a stream\",\n    14,\n    \"5.0.0\" },\n    { \"XAUTOCLAIM\",\n    \"key group consumer min-idle-time start [COUNT count] [justid]\",\n    \"Changes (or acquires) ownership of messages in a consumer group, as if the messages were delivered to the specified consumer.\",\n    14,\n    \"6.2.0\" },\n    { \"XCLAIM\",\n    \"key group consumer min-idle-time ID [ID ...] [IDLE ms] [TIME ms-unix-time] [RETRYCOUNT count] [force] [justid]\",\n    \"Changes (or acquires) ownership of a message in a consumer group, as if the message was delivered to the specified consumer.\",\n    14,\n    \"5.0.0\" },\n    { \"XDEL\",\n    \"key ID [ID ...]\",\n    \"Removes the specified entries from the stream. Returns the number of items actually deleted, that may be different from the number of IDs passed in case certain IDs do not exist.\",\n    14,\n    \"5.0.0\" },\n    { \"XGROUP\",\n    \"[CREATE key groupname ID|$ [MKSTREAM]] [SETID key groupname ID|$] [DESTROY key groupname] [CREATECONSUMER key groupname consumername] [DELCONSUMER key groupname consumername]\",\n    \"Create, destroy, and manage consumer groups.\",\n    14,\n    \"5.0.0\" },\n    { \"XINFO\",\n    \"[CONSUMERS key groupname] [GROUPS key] [STREAM key] [HELP]\",\n    \"Get information on streams and consumer groups\",\n    14,\n    \"5.0.0\" },\n    { \"XLEN\",\n    \"key\",\n    \"Return the number of entries in a stream\",\n    14,\n    \"5.0.0\" },\n    { \"XPENDING\",\n    \"key group [[IDLE min-idle-time] start end count [consumer]]\",\n    \"Return information and entries from a stream consumer group pending entries list, that are messages fetched but never acknowledged.\",\n    14,\n    \"5.0.0\" },\n    { \"XRANGE\",\n    \"key start end [COUNT count]\",\n    \"Return a range of elements in a stream, with IDs matching the specified IDs interval\",\n    14,\n    \"5.0.0\" },\n    { \"XREAD\",\n    \"[COUNT count] [BLOCK milliseconds] STREAMS key [key ...] id [id ...]\",\n    \"Return never seen elements in multiple streams, with IDs greater than the ones reported by the caller for each stream. Can block.\",\n    14,\n    \"5.0.0\" },\n    { \"XREADGROUP\",\n    \"GROUP group consumer [COUNT count] [BLOCK milliseconds] [NOACK] STREAMS key [key ...] ID [ID ...]\",\n    \"Return new entries from a stream using a consumer group, or access the history of the pending entries for a given consumer. Can block.\",\n    14,\n    \"5.0.0\" },\n    { \"XREVRANGE\",\n    \"key end start [COUNT count]\",\n    \"Return a range of elements in a stream, with IDs matching the specified IDs interval, in reverse order (from greater to smaller IDs) compared to XRANGE\",\n    14,\n    \"5.0.0\" },\n    { \"XTRIM\",\n    \"key MAXLEN|MINID [=|~] threshold [LIMIT count]\",\n    \"Trims the stream to (approximately if '~' is passed) a certain size\",\n    14,\n    \"5.0.0\" },\n    { \"ZADD\",\n    \"key [NX|XX] [GT|LT] [CH] [INCR] score member [score member ...]\",\n    \"Add one or more members to a sorted set, or update its score if it already exists\",\n    4,\n    \"1.2.0\" },\n    { \"ZCARD\",\n    \"key\",\n    \"Get the number of members in a sorted set\",\n    4,\n    \"1.2.0\" },\n    { \"ZCOUNT\",\n    \"key min max\",\n    \"Count the members in a sorted set with scores within the given values\",\n    4,\n    \"2.0.0\" },\n    { \"ZDIFF\",\n    \"numkeys key [key ...] [WITHSCORES]\",\n    \"Subtract multiple sorted sets\",\n    4,\n    \"6.2.0\" },\n    { \"ZDIFFSTORE\",\n    \"destination numkeys key [key ...]\",\n    \"Subtract multiple sorted sets and store the resulting sorted set in a new key\",\n    4,\n    \"6.2.0\" },\n    { \"ZINCRBY\",\n    \"key increment member\",\n    \"Increment the score of a member in a sorted set\",\n    4,\n    \"1.2.0\" },\n    { \"ZINTER\",\n    \"numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]\",\n    \"Intersect multiple sorted sets\",\n    4,\n    \"6.2.0\" },\n    { \"ZINTERSTORE\",\n    \"destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]\",\n    \"Intersect multiple sorted sets and store the resulting sorted set in a new key\",\n    4,\n    \"2.0.0\" },\n    { \"ZLEXCOUNT\",\n    \"key min max\",\n    \"Count the number of members in a sorted set between a given lexicographical range\",\n    4,\n    \"2.8.9\" },\n    { \"ZMSCORE\",\n    \"key member [member ...]\",\n    \"Get the score associated with the given members in a sorted set\",\n    4,\n    \"6.2.0\" },\n    { \"ZPOPMAX\",\n    \"key [count]\",\n    \"Remove and return members with the highest scores in a sorted set\",\n    4,\n    \"5.0.0\" },\n    { \"ZPOPMIN\",\n    \"key [count]\",\n    \"Remove and return members with the lowest scores in a sorted set\",\n    4,\n    \"5.0.0\" },\n    { \"ZRANDMEMBER\",\n    \"key [count [WITHSCORES]]\",\n    \"Get one or multiple random elements from a sorted set\",\n    4,\n    \"6.2.0\" },\n    { \"ZRANGE\",\n    \"key min max [BYSCORE|BYLEX] [REV] [LIMIT offset count] [WITHSCORES]\",\n    \"Return a range of members in a sorted set\",\n    4,\n    \"1.2.0\" },\n    { \"ZRANGEBYLEX\",\n    \"key min max [LIMIT offset count]\",\n    \"Return a range of members in a sorted set, by lexicographical range\",\n    4,\n    \"2.8.9\" },\n    { \"ZRANGEBYSCORE\",\n    \"key min max [WITHSCORES] [LIMIT offset count]\",\n    \"Return a range of members in a sorted set, by score\",\n    4,\n    \"1.0.5\" },\n    { \"ZRANGESTORE\",\n    \"dst src min max [BYSCORE|BYLEX] [REV] [LIMIT offset count]\",\n    \"Store a range of members from sorted set into another key\",\n    4,\n    \"6.2.0\" },\n    { \"ZRANK\",\n    \"key member\",\n    \"Determine the index of a member in a sorted set\",\n    4,\n    \"2.0.0\" },\n    { \"ZREM\",\n    \"key member [member ...]\",\n    \"Remove one or more members from a sorted set\",\n    4,\n    \"1.2.0\" },\n    { \"ZREMRANGEBYLEX\",\n    \"key min max\",\n    \"Remove all members in a sorted set between the given lexicographical range\",\n    4,\n    \"2.8.9\" },\n    { \"ZREMRANGEBYRANK\",\n    \"key start stop\",\n    \"Remove all members in a sorted set within the given indexes\",\n    4,\n    \"2.0.0\" },\n    { \"ZREMRANGEBYSCORE\",\n    \"key min max\",\n    \"Remove all members in a sorted set within the given scores\",\n    4,\n    \"1.2.0\" },\n    { \"ZREVRANGE\",\n    \"key start stop [WITHSCORES]\",\n    \"Return a range of members in a sorted set, by index, with scores ordered from high to low\",\n    4,\n    \"1.2.0\" },\n    { \"ZREVRANGEBYLEX\",\n    \"key max min [LIMIT offset count]\",\n    \"Return a range of members in a sorted set, by lexicographical range, ordered from higher to lower strings.\",\n    4,\n    \"2.8.9\" },\n    { \"ZREVRANGEBYSCORE\",\n    \"key max min [WITHSCORES] [LIMIT offset count]\",\n    \"Return a range of members in a sorted set, by score, with scores ordered from high to low\",\n    4,\n    \"2.2.0\" },\n    { \"ZREVRANK\",\n    \"key member\",\n    \"Determine the index of a member in a sorted set, with scores ordered from high to low\",\n    4,\n    \"2.0.0\" },\n    { \"ZSCAN\",\n    \"key cursor [MATCH pattern] [COUNT count]\",\n    \"Incrementally iterate sorted sets elements and associated scores\",\n    4,\n    \"2.8.0\" },\n    { \"ZSCORE\",\n    \"key member\",\n    \"Get the score associated with the given member in a sorted set\",\n    4,\n    \"1.2.0\" },\n    { \"ZUNION\",\n    \"numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]\",\n    \"Add multiple sorted sets\",\n    4,\n    \"6.2.0\" },\n    { \"ZUNIONSTORE\",\n    \"destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]\",\n    \"Add multiple sorted sets and store the resulting sorted set in a new key\",\n    4,\n    \"2.0.0\" },\n    { \"KEYDB.CRON\",\n    \"name [single/repeat] [optional: start] delay script numkeys [key N] [arg N]\",\n    \"Run a specified script after start + delay, optionally repeating every delay interval.  The job may be cancelled by deleting the key associated with the job (name parameter)\",\n    10,\n    \"6.5.2\"},\n    { \"KEYDB.HRENAME\",\n    \"key [src hash key] [dst hash key]\",\n    \"Rename a hash key, copying the value.\",\n    4,\n    \"6.5.3\"\n    },\n    { \"KEYDB.MEXISTS\",\n    \"key [key ...]\",\n    \"Determine if a key exists\",\n    0,\n    \"6.5.12\" },\n};\n\n#endif\n"
  },
  {
    "path": "src/hyperloglog.cpp",
    "content": "/* hyperloglog.c - Redis HyperLogLog probabilistic cardinality approximation.\n * This file implements the algorithm and the exported Redis commands.\n *\n * Copyright (c) 2014, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n\n#include <stdint.h>\n#include <math.h>\n\n/* The Redis HyperLogLog implementation is based on the following ideas:\n *\n * * The use of a 64 bit hash function as proposed in [1], in order to estimate\n *   cardinalities larger than 10^9, at the cost of just 1 additional bit per\n *   register.\n * * The use of 16384 6-bit registers for a great level of accuracy, using\n *   a total of 12k per key.\n * * The use of the Redis string data type. No new type is introduced.\n * * No attempt is made to compress the data structure as in [1]. Also the\n *   algorithm used is the original HyperLogLog Algorithm as in [2], with\n *   the only difference that a 64 bit hash function is used, so no correction\n *   is performed for values near 2^32 as in [1].\n *\n * [1] Heule, Nunkesser, Hall: HyperLogLog in Practice: Algorithmic\n *     Engineering of a State of The Art Cardinality Estimation Algorithm.\n *\n * [2] P. Flajolet, Éric Fusy, O. Gandouet, and F. Meunier. Hyperloglog: The\n *     analysis of a near-optimal cardinality estimation algorithm.\n *\n * Redis uses two representations:\n *\n * 1) A \"dense\" representation where every entry is represented by\n *    a 6-bit integer.\n * 2) A \"sparse\" representation using run length compression suitable\n *    for representing HyperLogLogs with many registers set to 0 in\n *    a memory efficient way.\n *\n *\n * HLL header\n * ===\n *\n * Both the dense and sparse representation have a 16 byte header as follows:\n *\n * +------+---+-----+----------+\n * | HYLL | E | N/U | Cardin.  |\n * +------+---+-----+----------+\n *\n * The first 4 bytes are a magic string set to the bytes \"HYLL\".\n * \"E\" is one byte encoding, currently set to HLL_DENSE or\n * HLL_SPARSE. N/U are three not used bytes.\n *\n * The \"Cardin.\" field is a 64 bit integer stored in little endian format\n * with the latest cardinality computed that can be reused if the data\n * structure was not modified since the last computation (this is useful\n * because there are high probabilities that HLLADD operations don't\n * modify the actual data structure and hence the approximated cardinality).\n *\n * When the most significant bit in the most significant byte of the cached\n * cardinality is set, it means that the data structure was modified and\n * we can't reuse the cached value that must be recomputed.\n *\n * Dense representation\n * ===\n *\n * The dense representation used by Redis is the following:\n *\n * +--------+--------+--------+------//      //--+\n * |11000000|22221111|33333322|55444444 ....     |\n * +--------+--------+--------+------//      //--+\n *\n * The 6 bits counters are encoded one after the other starting from the\n * LSB to the MSB, and using the next bytes as needed.\n *\n * Sparse representation\n * ===\n *\n * The sparse representation encodes registers using a run length\n * encoding composed of three opcodes, two using one byte, and one using\n * of two bytes. The opcodes are called ZERO, XZERO and VAL.\n *\n * ZERO opcode is represented as 00xxxxxx. The 6-bit integer represented\n * by the six bits 'xxxxxx', plus 1, means that there are N registers set\n * to 0. This opcode can represent from 1 to 64 contiguous registers set\n * to the value of 0.\n *\n * XZERO opcode is represented by two bytes 01xxxxxx yyyyyyyy. The 14-bit\n * integer represented by the bits 'xxxxxx' as most significant bits and\n * 'yyyyyyyy' as least significant bits, plus 1, means that there are N\n * registers set to 0. This opcode can represent from 0 to 16384 contiguous\n * registers set to the value of 0.\n *\n * VAL opcode is represented as 1vvvvvxx. It contains a 5-bit integer\n * representing the value of a register, and a 2-bit integer representing\n * the number of contiguous registers set to that value 'vvvvv'.\n * To obtain the value and run length, the integers vvvvv and xx must be\n * incremented by one. This opcode can represent values from 1 to 32,\n * repeated from 1 to 4 times.\n *\n * The sparse representation can't represent registers with a value greater\n * than 32, however it is very unlikely that we find such a register in an\n * HLL with a cardinality where the sparse representation is still more\n * memory efficient than the dense representation. When this happens the\n * HLL is converted to the dense representation.\n *\n * The sparse representation is purely positional. For example a sparse\n * representation of an empty HLL is just: XZERO:16384.\n *\n * An HLL having only 3 non-zero registers at position 1000, 1020, 1021\n * respectively set to 2, 3, 3, is represented by the following three\n * opcodes:\n *\n * XZERO:1000 (Registers 0-999 are set to 0)\n * VAL:2,1    (1 register set to value 2, that is register 1000)\n * ZERO:19    (Registers 1001-1019 set to 0)\n * VAL:3,2    (2 registers set to value 3, that is registers 1020,1021)\n * XZERO:15362 (Registers 1022-16383 set to 0)\n *\n * In the example the sparse representation used just 7 bytes instead\n * of 12k in order to represent the HLL registers. In general for low\n * cardinality there is a big win in terms of space efficiency, traded\n * with CPU time since the sparse representation is slower to access:\n *\n * The following table shows average cardinality vs bytes used, 100\n * samples per cardinality (when the set was not representable because\n * of registers with too big value, the dense representation size was used\n * as a sample).\n *\n * 100 267\n * 200 485\n * 300 678\n * 400 859\n * 500 1033\n * 600 1205\n * 700 1375\n * 800 1544\n * 900 1713\n * 1000 1882\n * 2000 3480\n * 3000 4879\n * 4000 6089\n * 5000 7138\n * 6000 8042\n * 7000 8823\n * 8000 9500\n * 9000 10088\n * 10000 10591\n *\n * The dense representation uses 12288 bytes, so there is a big win up to\n * a cardinality of ~2000-3000. For bigger cardinalities the constant times\n * involved in updating the sparse representation is not justified by the\n * memory savings. The exact maximum length of the sparse representation\n * when this implementation switches to the dense representation is\n * configured via the define g_pserver->hll_sparse_max_bytes.\n */\n\nstruct hllhdr {\n    char magic[4];      /* \"HYLL\" */\n    uint8_t encoding;   /* HLL_DENSE or HLL_SPARSE. */\n    uint8_t notused[3]; /* Reserved for future use, must be zero. */\n    uint8_t card[8];    /* Cached cardinality, little endian. */\n    uint8_t *registers() { /* Data bytes. */\n        return reinterpret_cast<uint8_t*>(this+1);\n    }\n};\n\n/* The cached cardinality MSB is used to signal validity of the cached value. */\n#define HLL_INVALIDATE_CACHE(hdr) (hdr)->card[7] |= (1<<7)\n#define HLL_VALID_CACHE(hdr) (((hdr)->card[7] & (1<<7)) == 0)\n\n#define HLL_P 14 /* The greater is P, the smaller the error. */\n#define HLL_Q (64-HLL_P) /* The number of bits of the hash value used for\n                            determining the number of leading zeros. */\n#define HLL_REGISTERS (1<<HLL_P) /* With P=14, 16384 registers. */\n#define HLL_P_MASK (HLL_REGISTERS-1) /* Mask to index register. */\n#define HLL_BITS 6 /* Enough to count up to 63 leading zeroes. */\n#define HLL_REGISTER_MAX ((1<<HLL_BITS)-1)\n#define HLL_HDR_SIZE sizeof(struct hllhdr)\n#define HLL_DENSE_SIZE (HLL_HDR_SIZE+((HLL_REGISTERS*HLL_BITS+7)/8))\n#define HLL_DENSE 0 /* Dense encoding. */\n#define HLL_SPARSE 1 /* Sparse encoding. */\n#define HLL_RAW 255 /* Only used internally, never exposed. */\n#define HLL_MAX_ENCODING 1\n\nstatic const char *invalid_hll_err = \"-INVALIDOBJ Corrupted HLL object detected\";\n\n/* =========================== Low level bit macros ========================= */\n\n/* Macros to access the dense representation.\n *\n * We need to get and set 6 bit counters in an array of 8 bit bytes.\n * We use macros to make sure the code is inlined since speed is critical\n * especially in order to compute the approximated cardinality in\n * HLLCOUNT where we need to access all the registers at once.\n * For the same reason we also want to avoid conditionals in this code path.\n *\n * +--------+--------+--------+------//\n * |11000000|22221111|33333322|55444444\n * +--------+--------+--------+------//\n *\n * Note: in the above representation the most significant bit (MSB)\n * of every byte is on the left. We start using bits from the LSB to MSB,\n * and so forth passing to the next byte.\n *\n * Example, we want to access to counter at pos = 1 (\"111111\" in the\n * illustration above).\n *\n * The index of the first byte b0 containing our data is:\n *\n *  b0 = 6 * pos / 8 = 0\n *\n *   +--------+\n *   |11000000|  <- Our byte at b0\n *   +--------+\n *\n * The position of the first bit (counting from the LSB = 0) in the byte\n * is given by:\n *\n *  fb = 6 * pos % 8 -> 6\n *\n * Right shift b0 of 'fb' bits.\n *\n *   +--------+\n *   |11000000|  <- Initial value of b0\n *   |00000011|  <- After right shift of 6 pos.\n *   +--------+\n *\n * Left shift b1 of bits 8-fb bits (2 bits)\n *\n *   +--------+\n *   |22221111|  <- Initial value of b1\n *   |22111100|  <- After left shift of 2 bits.\n *   +--------+\n *\n * OR the two bits, and finally AND with 111111 (63 in decimal) to\n * clean the higher order bits we are not interested in:\n *\n *   +--------+\n *   |00000011|  <- b0 right shifted\n *   |22111100|  <- b1 left shifted\n *   |22111111|  <- b0 OR b1\n *   |  111111|  <- (b0 OR b1) AND 63, our value.\n *   +--------+\n *\n * We can try with a different example, like pos = 0. In this case\n * the 6-bit counter is actually contained in a single byte.\n *\n *  b0 = 6 * pos / 8 = 0\n *\n *   +--------+\n *   |11000000|  <- Our byte at b0\n *   +--------+\n *\n *  fb = 6 * pos % 8 = 0\n *\n *  So we right shift of 0 bits (no shift in practice) and\n *  left shift the next byte of 8 bits, even if we don't use it,\n *  but this has the effect of clearing the bits so the result\n *  will not be affected after the OR.\n *\n * -------------------------------------------------------------------------\n *\n * Setting the register is a bit more complex, let's assume that 'val'\n * is the value we want to set, already in the right range.\n *\n * We need two steps, in one we need to clear the bits, and in the other\n * we need to bitwise-OR the new bits.\n *\n * Let's try with 'pos' = 1, so our first byte at 'b' is 0,\n *\n * \"fb\" is 6 in this case.\n *\n *   +--------+\n *   |11000000|  <- Our byte at b0\n *   +--------+\n *\n * To create an AND-mask to clear the bits about this position, we just\n * initialize the mask with the value 63, left shift it of \"fs\" bits,\n * and finally invert the result.\n *\n *   +--------+\n *   |00111111|  <- \"mask\" starts at 63\n *   |11000000|  <- \"mask\" after left shift of \"ls\" bits.\n *   |00111111|  <- \"mask\" after invert.\n *   +--------+\n *\n * Now we can bitwise-AND the byte at \"b\" with the mask, and bitwise-OR\n * it with \"val\" left-shifted of \"ls\" bits to set the new bits.\n *\n * Now let's focus on the next byte b1:\n *\n *   +--------+\n *   |22221111|  <- Initial value of b1\n *   +--------+\n *\n * To build the AND mask we start again with the 63 value, right shift\n * it by 8-fb bits, and invert it.\n *\n *   +--------+\n *   |00111111|  <- \"mask\" set at 2&6-1\n *   |00001111|  <- \"mask\" after the right shift by 8-fb = 2 bits\n *   |11110000|  <- \"mask\" after bitwise not.\n *   +--------+\n *\n * Now we can mask it with b+1 to clear the old bits, and bitwise-OR\n * with \"val\" left-shifted by \"rs\" bits to set the new value.\n */\n\n/* Note: if we access the last counter, we will also access the b+1 byte\n * that is out of the array, but sds strings always have an implicit null\n * term, so the byte exists, and we can skip the conditional (or the need\n * to allocate 1 byte more explicitly). */\n\n/* Store the value of the register at position 'regnum' into variable 'target'.\n * 'p' is an array of unsigned bytes. */\n#define HLL_DENSE_GET_REGISTER(target,p,regnum) do { \\\n    uint8_t *_p = (uint8_t*) p; \\\n    unsigned long _byte = regnum*HLL_BITS/8; \\\n    unsigned long _fb = regnum*HLL_BITS&7; \\\n    unsigned long _fb8 = 8 - _fb; \\\n    unsigned long b0 = _p[_byte]; \\\n    unsigned long b1 = _p[_byte+1]; \\\n    target = ((b0 >> _fb) | (b1 << _fb8)) & HLL_REGISTER_MAX; \\\n} while(0)\n\n/* Set the value of the register at position 'regnum' to 'val'.\n * 'p' is an array of unsigned bytes. */\n#define HLL_DENSE_SET_REGISTER(p,regnum,val) do { \\\n    uint8_t *_p = (uint8_t*) p; \\\n    unsigned long _byte = regnum*HLL_BITS/8; \\\n    unsigned long _fb = regnum*HLL_BITS&7; \\\n    unsigned long _fb8 = 8 - _fb; \\\n    unsigned long _v = val; \\\n    _p[_byte] &= ~(HLL_REGISTER_MAX << _fb); \\\n    _p[_byte] |= _v << _fb; \\\n    _p[_byte+1] &= ~(HLL_REGISTER_MAX >> _fb8); \\\n    _p[_byte+1] |= _v >> _fb8; \\\n} while(0)\n\n/* Macros to access the sparse representation.\n * The macros parameter is expected to be an uint8_t pointer. */\n#define HLL_SPARSE_XZERO_BIT 0x40 /* 01xxxxxx */\n#define HLL_SPARSE_VAL_BIT 0x80 /* 1vvvvvxx */\n#define HLL_SPARSE_IS_ZERO(p) (((*(p)) & 0xc0) == 0) /* 00xxxxxx */\n#define HLL_SPARSE_IS_XZERO(p) (((*(p)) & 0xc0) == HLL_SPARSE_XZERO_BIT)\n#define HLL_SPARSE_IS_VAL(p) ((*(p)) & HLL_SPARSE_VAL_BIT)\n#define HLL_SPARSE_ZERO_LEN(p) (((*(p)) & 0x3f)+1)\n#define HLL_SPARSE_XZERO_LEN(p) (((((*(p)) & 0x3f) << 8) | (*((p)+1)))+1)\n#define HLL_SPARSE_VAL_VALUE(p) ((((*(p)) >> 2) & 0x1f)+1)\n#define HLL_SPARSE_VAL_LEN(p) (((*(p)) & 0x3)+1)\n#define HLL_SPARSE_VAL_MAX_VALUE 32\n#define HLL_SPARSE_VAL_MAX_LEN 4\n#define HLL_SPARSE_ZERO_MAX_LEN 64\n#define HLL_SPARSE_XZERO_MAX_LEN 16384\n#define HLL_SPARSE_VAL_SET(p,val,len) do { \\\n    *(p) = (((val)-1)<<2|((len)-1))|HLL_SPARSE_VAL_BIT; \\\n} while(0)\n#define HLL_SPARSE_ZERO_SET(p,len) do { \\\n    *(p) = (len)-1; \\\n} while(0)\n#define HLL_SPARSE_XZERO_SET(p,len) do { \\\n    int _l = (len)-1; \\\n    *(p) = (_l>>8) | HLL_SPARSE_XZERO_BIT; \\\n    *((p)+1) = (_l&0xff); \\\n} while(0)\n#define HLL_ALPHA_INF 0.721347520444481703680 /* constant for 0.5/ln(2) */\n\n/* ========================= HyperLogLog algorithm  ========================= */\n\n/* Our hash function is MurmurHash2, 64 bit version.\n * It was modified for Redis in order to provide the same result in\n * big and little endian archs (endian neutral). */\nuint64_t MurmurHash64A (const void * key, int len, unsigned int seed) {\n    const uint64_t m = 0xc6a4a7935bd1e995;\n    const int r = 47;\n    uint64_t h = seed ^ (len * m);\n    const uint8_t *data = (const uint8_t *)key;\n    const uint8_t *end = data + (len-(len&7));\n\n    while(data != end) {\n        uint64_t k;\n\n#if (BYTE_ORDER == LITTLE_ENDIAN)\n    #ifdef USE_ALIGNED_ACCESS\n        memcpy(&k,data,sizeof(uint64_t));\n    #else\n        k = *((uint64_t*)data);\n    #endif\n#else\n        k = (uint64_t) data[0];\n        k |= (uint64_t) data[1] << 8;\n        k |= (uint64_t) data[2] << 16;\n        k |= (uint64_t) data[3] << 24;\n        k |= (uint64_t) data[4] << 32;\n        k |= (uint64_t) data[5] << 40;\n        k |= (uint64_t) data[6] << 48;\n        k |= (uint64_t) data[7] << 56;\n#endif\n\n        k *= m;\n        k ^= k >> r;\n        k *= m;\n        h ^= k;\n        h *= m;\n        data += 8;\n    }\n\n    switch(len & 7) {\n    case 7: h ^= (uint64_t)data[6] << 48; /* fall-thru */\n    case 6: h ^= (uint64_t)data[5] << 40; /* fall-thru */\n    case 5: h ^= (uint64_t)data[4] << 32; /* fall-thru */\n    case 4: h ^= (uint64_t)data[3] << 24; /* fall-thru */\n    case 3: h ^= (uint64_t)data[2] << 16; /* fall-thru */\n    case 2: h ^= (uint64_t)data[1] << 8; /* fall-thru */\n    case 1: h ^= (uint64_t)data[0];\n            h *= m; /* fall-thru */\n    };\n\n    h ^= h >> r;\n    h *= m;\n    h ^= h >> r;\n    return h;\n}\n\n/* Given a string element to add to the HyperLogLog, returns the length\n * of the pattern 000..1 of the element hash. As a side effect 'regp' is\n * set to the register index this element hashes to. */\nint hllPatLen(unsigned char *ele, size_t elesize, long *regp) {\n    uint64_t hash, bit, index;\n    int count;\n\n    /* Count the number of zeroes starting from bit HLL_REGISTERS\n     * (that is a power of two corresponding to the first bit we don't use\n     * as index). The max run can be 64-P+1 = Q+1 bits.\n     *\n     * Note that the final \"1\" ending the sequence of zeroes must be\n     * included in the count, so if we find \"001\" the count is 3, and\n     * the smallest count possible is no zeroes at all, just a 1 bit\n     * at the first position, that is a count of 1.\n     *\n     * This may sound like inefficient, but actually in the average case\n     * there are high probabilities to find a 1 after a few iterations. */\n    hash = MurmurHash64A(ele,elesize,0xadc83b19ULL);\n    index = hash & HLL_P_MASK; /* Register index. */\n    hash >>= HLL_P; /* Remove bits used to address the register. */\n    hash |= ((uint64_t)1<<HLL_Q); /* Make sure the loop terminates\n                                     and count will be <= Q+1. */\n    bit = 1;\n    count = 1; /* Initialized to 1 since we count the \"00000...1\" pattern. */\n    while((hash & bit) == 0) {\n        count++;\n        bit <<= 1;\n    }\n    *regp = (int) index;\n    return count;\n}\n\n/* ================== Dense representation implementation  ================== */\n\n/* Low level function to set the dense HLL register at 'index' to the\n * specified value if the current value is smaller than 'count'.\n *\n * 'registers' is expected to have room for HLL_REGISTERS plus an\n * additional byte on the right. This requirement is met by sds strings\n * automatically since they are implicitly null terminated.\n *\n * The function always succeed, however if as a result of the operation\n * the approximated cardinality changed, 1 is returned. Otherwise 0\n * is returned. */\nint hllDenseSet(uint8_t *registers, long index, uint8_t count) {\n    uint8_t oldcount;\n\n    HLL_DENSE_GET_REGISTER(oldcount,registers,index);\n    if (count > oldcount) {\n        HLL_DENSE_SET_REGISTER(registers,index,count);\n        return 1;\n    } else {\n        return 0;\n    }\n}\n\n/* \"Add\" the element in the dense hyperloglog data structure.\n * Actually nothing is added, but the max 0 pattern counter of the subset\n * the element belongs to is incremented if needed.\n *\n * This is just a wrapper to hllDenseSet(), performing the hashing of the\n * element in order to retrieve the index and zero-run count. */\nint hllDenseAdd(uint8_t *registers, unsigned char *ele, size_t elesize) {\n    long index;\n    uint8_t count = hllPatLen(ele,elesize,&index);\n    /* Update the register if this element produced a longer run of zeroes. */\n    return hllDenseSet(registers,index,count);\n}\n\n/* Compute the register histogram in the dense representation. */\nvoid hllDenseRegHisto(uint8_t *registers, int* reghisto) {\n    int j;\n\n    /* Redis default is to use 16384 registers 6 bits each. The code works\n     * with other values by modifying the defines, but for our target value\n     * we take a faster path with unrolled loops. */\n    if (HLL_REGISTERS == 16384 && HLL_BITS == 6) {\n        uint8_t *r = registers;\n        unsigned long r0, r1, r2, r3, r4, r5, r6, r7, r8, r9,\n                      r10, r11, r12, r13, r14, r15;\n        for (j = 0; j < 1024; j++) {\n            /* Handle 16 registers per iteration. */\n            r0 = r[0] & 63;\n            r1 = (r[0] >> 6 | r[1] << 2) & 63;\n            r2 = (r[1] >> 4 | r[2] << 4) & 63;\n            r3 = (r[2] >> 2) & 63;\n            r4 = r[3] & 63;\n            r5 = (r[3] >> 6 | r[4] << 2) & 63;\n            r6 = (r[4] >> 4 | r[5] << 4) & 63;\n            r7 = (r[5] >> 2) & 63;\n            r8 = r[6] & 63;\n            r9 = (r[6] >> 6 | r[7] << 2) & 63;\n            r10 = (r[7] >> 4 | r[8] << 4) & 63;\n            r11 = (r[8] >> 2) & 63;\n            r12 = r[9] & 63;\n            r13 = (r[9] >> 6 | r[10] << 2) & 63;\n            r14 = (r[10] >> 4 | r[11] << 4) & 63;\n            r15 = (r[11] >> 2) & 63;\n\n            reghisto[r0]++;\n            reghisto[r1]++;\n            reghisto[r2]++;\n            reghisto[r3]++;\n            reghisto[r4]++;\n            reghisto[r5]++;\n            reghisto[r6]++;\n            reghisto[r7]++;\n            reghisto[r8]++;\n            reghisto[r9]++;\n            reghisto[r10]++;\n            reghisto[r11]++;\n            reghisto[r12]++;\n            reghisto[r13]++;\n            reghisto[r14]++;\n            reghisto[r15]++;\n\n            r += 12;\n        }\n    } else {\n        for(j = 0; j < HLL_REGISTERS; j++) {\n            unsigned long reg;\n            HLL_DENSE_GET_REGISTER(reg,registers,j);\n            reghisto[reg]++;\n        }\n    }\n}\n\n/* ================== Sparse representation implementation  ================= */\n\n/* Convert the HLL with sparse representation given as input in its dense\n * representation. Both representations are represented by SDS strings, and\n * the input representation is freed as a side effect.\n *\n * The function returns C_OK if the sparse representation was valid,\n * otherwise C_ERR is returned if the representation was corrupted. */\nint hllSparseToDense(robj *o) {\n    sds sparse = szFromObj(o), dense;\n    struct hllhdr *hdr, *oldhdr = (struct hllhdr*)sparse;\n    int idx = 0, runlen, regval;\n    uint8_t *p = (uint8_t*)sparse, *end = p+sdslen(sparse);\n\n    /* If the representation is already the right one return ASAP. */\n    hdr = (struct hllhdr*) sparse;\n    if (hdr->encoding == HLL_DENSE) return C_OK;\n\n    /* Create a string of the right size filled with zero bytes.\n     * Note that the cached cardinality is set to 0 as a side effect\n     * that is exactly the cardinality of an empty HLL. */\n    dense = sdsnewlen(NULL,HLL_DENSE_SIZE);\n    hdr = (struct hllhdr*) dense;\n    *hdr = *oldhdr; /* This will copy the magic and cached cardinality. */\n    hdr->encoding = HLL_DENSE;\n\n    /* Now read the sparse representation and set non-zero registers\n     * accordingly. */\n    p += HLL_HDR_SIZE;\n    while(p < end) {\n        if (HLL_SPARSE_IS_ZERO(p)) {\n            runlen = HLL_SPARSE_ZERO_LEN(p);\n            idx += runlen;\n            p++;\n        } else if (HLL_SPARSE_IS_XZERO(p)) {\n            runlen = HLL_SPARSE_XZERO_LEN(p);\n            idx += runlen;\n            p += 2;\n        } else {\n            runlen = HLL_SPARSE_VAL_LEN(p);\n            regval = HLL_SPARSE_VAL_VALUE(p);\n            if ((runlen + idx) > HLL_REGISTERS) break; /* Overflow. */\n            while(runlen--) {\n                HLL_DENSE_SET_REGISTER(hdr->registers(),idx,regval);\n                idx++;\n            }\n            p++;\n        }\n    }\n\n    /* If the sparse representation was valid, we expect to find idx\n     * set to HLL_REGISTERS. */\n    if (idx != HLL_REGISTERS) {\n        sdsfree(dense);\n        return C_ERR;\n    }\n\n    /* Free the old representation and set the new one. */\n    sdsfree(szFromObj(o));\n    o->m_ptr = dense;\n    return C_OK;\n}\n\n/* Low level function to set the sparse HLL register at 'index' to the\n * specified value if the current value is smaller than 'count'.\n *\n * The object 'o' is the String object holding the HLL. The function requires\n * a reference to the object in order to be able to enlarge the string if\n * needed.\n *\n * On success, the function returns 1 if the cardinality changed, or 0\n * if the register for this element was not updated.\n * On error (if the representation is invalid) -1 is returned.\n *\n * As a side effect the function may promote the HLL representation from\n * sparse to dense: this happens when a register requires to be set to a value\n * not representable with the sparse representation, or when the resulting\n * size would be greater than g_pserver->hll_sparse_max_bytes. */\nint hllSparseSet(robj *o, long index, uint8_t count) {\n    struct hllhdr *hdr;\n    uint8_t oldcount, *sparse, *end, *p, *prev, *next;\n    long first, span;\n    long is_zero = 0, is_xzero = 0, is_val = 0, runlen = 0;\n    uint8_t seq[5], *n = nullptr;\n    int last;\n    int len;\n    int seqlen = 0;\n    int oldlen = 0;\n    int deltalen = 0;\n    int scanlen = 0;\n\n    /* If the count is too big to be representable by the sparse representation\n     * switch to dense representation. */\n    if (count > HLL_SPARSE_VAL_MAX_VALUE) goto promote;\n\n    /* When updating a sparse representation, sometimes we may need to\n     * enlarge the buffer for up to 3 bytes in the worst case (XZERO split\n     * into XZERO-VAL-XZERO). Make sure there is enough space right now\n     * so that the pointers we take during the execution of the function\n     * will be valid all the time. */\n    o->m_ptr = sdsMakeRoomFor(szFromObj(o),3);\n\n    /* Step 1: we need to locate the opcode we need to modify to check\n     * if a value update is actually needed. */\n    sparse = p = ((uint8_t*)ptrFromObj(o)) + HLL_HDR_SIZE;\n    end = p + sdslen(szFromObj(o)) - HLL_HDR_SIZE;\n\n    first = 0;\n    prev = NULL; /* Points to previous opcode at the end of the loop. */\n    next = NULL; /* Points to the next opcode at the end of the loop. */\n    span = 0;\n    while(p < end) {\n        long oplen;\n\n        /* Set span to the number of registers covered by this opcode.\n         *\n         * This is the most performance critical loop of the sparse\n         * representation. Sorting the conditionals from the most to the\n         * least frequent opcode in many-bytes sparse HLLs is faster. */\n        oplen = 1;\n        if (HLL_SPARSE_IS_ZERO(p)) {\n            span = HLL_SPARSE_ZERO_LEN(p);\n        } else if (HLL_SPARSE_IS_VAL(p)) {\n            span = HLL_SPARSE_VAL_LEN(p);\n        } else { /* XZERO. */\n            span = HLL_SPARSE_XZERO_LEN(p);\n            oplen = 2;\n        }\n        /* Break if this opcode covers the register as 'index'. */\n        if (index <= first+span-1) break;\n        prev = p;\n        p += oplen;\n        first += span;\n    }\n    if (span == 0 || p >= end) return -1; /* Invalid format. */\n\n    next = HLL_SPARSE_IS_XZERO(p) ? p+2 : p+1;\n    if (next >= end) next = NULL;\n\n    /* Cache current opcode type to avoid using the macro again and\n     * again for something that will not change.\n     * Also cache the run-length of the opcode. */\n    if (HLL_SPARSE_IS_ZERO(p)) {\n        is_zero = 1;\n        runlen = HLL_SPARSE_ZERO_LEN(p);\n    } else if (HLL_SPARSE_IS_XZERO(p)) {\n        is_xzero = 1;\n        runlen = HLL_SPARSE_XZERO_LEN(p);\n    } else {\n        is_val = 1;\n        runlen = HLL_SPARSE_VAL_LEN(p);\n    }\n\n    /* Step 2: After the loop:\n     *\n     * 'first' stores to the index of the first register covered\n     *  by the current opcode, which is pointed by 'p'.\n     *\n     * 'next' ad 'prev' store respectively the next and previous opcode,\n     *  or NULL if the opcode at 'p' is respectively the last or first.\n     *\n     * 'span' is set to the number of registers covered by the current\n     *  opcode.\n     *\n     * There are different cases in order to update the data structure\n     * in place without generating it from scratch:\n     *\n     * A) If it is a VAL opcode already set to a value >= our 'count'\n     *    no update is needed, regardless of the VAL run-length field.\n     *    In this case PFADD returns 0 since no changes are performed.\n     *\n     * B) If it is a VAL opcode with len = 1 (representing only our\n     *    register) and the value is less than 'count', we just update it\n     *    since this is a trivial case. */\n    if (is_val) {\n        oldcount = HLL_SPARSE_VAL_VALUE(p);\n        /* Case A. */\n        if (oldcount >= count) return 0;\n\n        /* Case B. */\n        if (runlen == 1) {\n            HLL_SPARSE_VAL_SET(p,count,1);\n            goto updated;\n        }\n    }\n\n    /* C) Another trivial to handle case is a ZERO opcode with a len of 1.\n     * We can just replace it with a VAL opcode with our value and len of 1. */\n    if (is_zero && runlen == 1) {\n        HLL_SPARSE_VAL_SET(p,count,1);\n        goto updated;\n    }\n\n    /* D) General case.\n     *\n     * The other cases are more complex: our register requires to be updated\n     * and is either currently represented by a VAL opcode with len > 1,\n     * by a ZERO opcode with len > 1, or by an XZERO opcode.\n     *\n     * In those cases the original opcode must be split into multiple\n     * opcodes. The worst case is an XZERO split in the middle resulting into\n     * XZERO - VAL - XZERO, so the resulting sequence max length is\n     * 5 bytes.\n     *\n     * We perform the split writing the new sequence into the 'new' buffer\n     * with 'newlen' as length. Later the new sequence is inserted in place\n     * of the old one, possibly moving what is on the right a few bytes\n     * if the new sequence is longer than the older one. */\n    n = seq;\n    last = first+span-1; /* Last register covered by the sequence. */\n\n    if (is_zero || is_xzero) {\n        /* Handle splitting of ZERO / XZERO. */\n        if (index != first) {\n            len = index-first;\n            if (len > HLL_SPARSE_ZERO_MAX_LEN) {\n                HLL_SPARSE_XZERO_SET(n,len);\n                n += 2;\n            } else {\n                HLL_SPARSE_ZERO_SET(n,len);\n                n++;\n            }\n        }\n        HLL_SPARSE_VAL_SET(n,count,1);\n        n++;\n        if (index != last) {\n            len = last-index;\n            if (len > HLL_SPARSE_ZERO_MAX_LEN) {\n                HLL_SPARSE_XZERO_SET(n,len);\n                n += 2;\n            } else {\n                HLL_SPARSE_ZERO_SET(n,len);\n                n++;\n            }\n        }\n    } else {\n        /* Handle splitting of VAL. */\n        int curval = HLL_SPARSE_VAL_VALUE(p);\n\n        if (index != first) {\n            len = index-first;\n            HLL_SPARSE_VAL_SET(n,curval,len);\n            n++;\n        }\n        HLL_SPARSE_VAL_SET(n,count,1);\n        n++;\n        if (index != last) {\n            len = last-index;\n            HLL_SPARSE_VAL_SET(n,curval,len);\n            n++;\n        }\n    }\n\n    /* Step 3: substitute the new sequence with the old one.\n     *\n     * Note that we already allocated space on the sds string\n     * calling sdsMakeRoomFor(). */\n     seqlen = n-seq;\n     oldlen = is_xzero ? 2 : 1;\n     deltalen = seqlen-oldlen;\n\n     if (deltalen > 0 &&\n         sdslen(szFromObj(o))+deltalen > g_pserver->hll_sparse_max_bytes) goto promote;\n     if (deltalen && next) memmove(next+deltalen,next,end-next);\n     sdsIncrLen(szFromObj(o),deltalen);\n     memcpy(p,seq,seqlen);\n     end += deltalen;\n\nupdated:\n    /* Step 4: Merge adjacent values if possible.\n     *\n     * The representation was updated, however the resulting representation\n     * may not be optimal: adjacent VAL opcodes can sometimes be merged into\n     * a single one. */\n    p = prev ? prev : sparse;\n    scanlen = 5; /* Scan up to 5 upcodes starting from prev. */\n    while (p < end && scanlen--) {\n        if (HLL_SPARSE_IS_XZERO(p)) {\n            p += 2;\n            continue;\n        } else if (HLL_SPARSE_IS_ZERO(p)) {\n            p++;\n            continue;\n        }\n        /* We need two adjacent VAL opcodes to try a merge, having\n         * the same value, and a len that fits the VAL opcode max len. */\n        if (p+1 < end && HLL_SPARSE_IS_VAL(p+1)) {\n            int v1 = HLL_SPARSE_VAL_VALUE(p);\n            int v2 = HLL_SPARSE_VAL_VALUE(p+1);\n            if (v1 == v2) {\n                int len = HLL_SPARSE_VAL_LEN(p)+HLL_SPARSE_VAL_LEN(p+1);\n                if (len <= HLL_SPARSE_VAL_MAX_LEN) {\n                    HLL_SPARSE_VAL_SET(p+1,v1,len);\n                    memmove(p,p+1,end-p);\n                    sdsIncrLen(szFromObj(o),-1);\n                    end--;\n                    /* After a merge we reiterate without incrementing 'p'\n                     * in order to try to merge the just merged value with\n                     * a value on its right. */\n                    continue;\n                }\n            }\n        }\n        p++;\n    }\n\n    /* Invalidate the cached cardinality. */\n    hdr = (hllhdr*)ptrFromObj(o);\n    HLL_INVALIDATE_CACHE(hdr);\n    return 1;\n\npromote: /* Promote to dense representation. */\n    if (hllSparseToDense(o) == C_ERR) return -1; /* Corrupted HLL. */\n    hdr = (hllhdr*)ptrFromObj(o);\n\n    /* We need to call hllDenseAdd() to perform the operation after the\n     * conversion. However the result must be 1, since if we need to\n     * convert from sparse to dense a register requires to be updated.\n     *\n     * Note that this in turn means that PFADD will make sure the command\n     * is propagated to slaves / AOF, so if there is a sparse -> dense\n     * conversion, it will be performed in all the slaves as well. */\n    int dense_retval = hllDenseSet(hdr->registers(),index,count);\n    serverAssert(dense_retval == 1);\n    return dense_retval;\n}\n\n/* \"Add\" the element in the sparse hyperloglog data structure.\n * Actually nothing is added, but the max 0 pattern counter of the subset\n * the element belongs to is incremented if needed.\n *\n * This function is actually a wrapper for hllSparseSet(), it only performs\n * the hashshing of the element to obtain the index and zeros run length. */\nint hllSparseAdd(robj *o, unsigned char *ele, size_t elesize) {\n    long index;\n    uint8_t count = hllPatLen(ele,elesize,&index);\n    /* Update the register if this element produced a longer run of zeroes. */\n    return hllSparseSet(o,index,count);\n}\n\n/* Compute the register histogram in the sparse representation. */\nvoid hllSparseRegHisto(uint8_t *sparse, int sparselen, int *invalid, int* reghisto) {\n    int idx = 0, runlen, regval;\n    uint8_t *end = sparse+sparselen, *p = sparse;\n\n    while(p < end) {\n        if (HLL_SPARSE_IS_ZERO(p)) {\n            runlen = HLL_SPARSE_ZERO_LEN(p);\n            idx += runlen;\n            reghisto[0] += runlen;\n            p++;\n        } else if (HLL_SPARSE_IS_XZERO(p)) {\n            runlen = HLL_SPARSE_XZERO_LEN(p);\n            idx += runlen;\n            reghisto[0] += runlen;\n            p += 2;\n        } else {\n            runlen = HLL_SPARSE_VAL_LEN(p);\n            regval = HLL_SPARSE_VAL_VALUE(p);\n            idx += runlen;\n            reghisto[regval] += runlen;\n            p++;\n        }\n    }\n    if (idx != HLL_REGISTERS && invalid) *invalid = 1;\n}\n\n/* ========================= HyperLogLog Count ==============================\n * This is the core of the algorithm where the approximated count is computed.\n * The function uses the lower level hllDenseRegHisto() and hllSparseRegHisto()\n * functions as helpers to compute histogram of register values part of the\n * computation, which is representation-specific, while all the rest is common. */\n\n/* Implements the register histogram calculation for uint8_t data type\n * which is only used internally as speedup for PFCOUNT with multiple keys. */\nvoid hllRawRegHisto(uint8_t *registers, int* reghisto) {\n    uint64_t *word = (uint64_t*) registers;\n    uint8_t *bytes;\n    int j;\n\n    for (j = 0; j < HLL_REGISTERS/8; j++) {\n        if (*word == 0) {\n            reghisto[0] += 8;\n        } else {\n            bytes = (uint8_t*) word;\n            reghisto[bytes[0]]++;\n            reghisto[bytes[1]]++;\n            reghisto[bytes[2]]++;\n            reghisto[bytes[3]]++;\n            reghisto[bytes[4]]++;\n            reghisto[bytes[5]]++;\n            reghisto[bytes[6]]++;\n            reghisto[bytes[7]]++;\n        }\n        word++;\n    }\n}\n\n/* Helper function sigma as defined in\n * \"New cardinality estimation algorithms for HyperLogLog sketches\"\n * Otmar Ertl, arXiv:1702.01284 */\ndouble hllSigma(double x) {\n    if (x == 1.) return INFINITY;\n    double zPrime;\n    double y = 1;\n    double z = x;\n    do {\n        x *= x;\n        zPrime = z;\n        z += x * y;\n        y += y;\n    } while(zPrime != z);\n    return z;\n}\n\n/* Helper function tau as defined in\n * \"New cardinality estimation algorithms for HyperLogLog sketches\"\n * Otmar Ertl, arXiv:1702.01284 */\ndouble hllTau(double x) {\n    if (x == 0. || x == 1.) return 0.;\n    double zPrime;\n    double y = 1.0;\n    double z = 1 - x;\n    do {\n        x = sqrt(x);\n        zPrime = z;\n        y *= 0.5;\n        z -= pow(1 - x, 2)*y;\n    } while(zPrime != z);\n    return z / 3;\n}\n\n/* Return the approximated cardinality of the set based on the harmonic\n * mean of the registers values. 'hdr' points to the start of the SDS\n * representing the String object holding the HLL representation.\n *\n * If the sparse representation of the HLL object is not valid, the integer\n * pointed by 'invalid' is set to non-zero, otherwise it is left untouched.\n *\n * hllCount() supports a special internal-only encoding of HLL_RAW, that\n * is, hdr->registers() will point to an uint8_t array of HLL_REGISTERS element.\n * This is useful in order to speedup PFCOUNT when called against multiple\n * keys (no need to work with 6-bit integers encoding). */\nuint64_t hllCount(struct hllhdr *hdr, int *invalid) {\n    double m = HLL_REGISTERS;\n    double E;\n    int j;\n    /* Note that reghisto size could be just HLL_Q+2, because HLL_Q+1 is\n     * the maximum frequency of the \"000...1\" sequence the hash function is\n     * able to return. However it is slow to check for sanity of the\n     * input: instead we history array at a safe size: overflows will\n     * just write data to wrong, but correctly allocated, places. */\n    int reghisto[64] = {0};\n\n    /* Compute register histogram */\n    if (hdr->encoding == HLL_DENSE) {\n        hllDenseRegHisto(hdr->registers(),reghisto);\n    } else if (hdr->encoding == HLL_SPARSE) {\n        hllSparseRegHisto(hdr->registers(),\n                         sdslen((sds)hdr)-HLL_HDR_SIZE,invalid,reghisto);\n    } else if (hdr->encoding == HLL_RAW) {\n        hllRawRegHisto(hdr->registers(),reghisto);\n    } else {\n        serverPanic(\"Unknown HyperLogLog encoding in hllCount()\");\n    }\n\n    /* Estimate cardinality form register histogram. See:\n     * \"New cardinality estimation algorithms for HyperLogLog sketches\"\n     * Otmar Ertl, arXiv:1702.01284 */\n    double z = m * hllTau((m-reghisto[HLL_Q+1])/(double)m);\n    for (j = HLL_Q; j >= 1; --j) {\n        z += reghisto[j];\n        z *= 0.5;\n    }\n    z += m * hllSigma(reghisto[0]/(double)m);\n    E = llroundl(HLL_ALPHA_INF*m*m/z);\n\n    return (uint64_t) E;\n}\n\n/* Call hllDenseAdd() or hllSparseAdd() according to the HLL encoding. */\nint hllAdd(robj *o, unsigned char *ele, size_t elesize) {\n    struct hllhdr *hdr = (hllhdr*)ptrFromObj(o);\n    switch(hdr->encoding) {\n    case HLL_DENSE: return hllDenseAdd(hdr->registers(),ele,elesize);\n    case HLL_SPARSE: return hllSparseAdd(o,ele,elesize);\n    default: return -1; /* Invalid representation. */\n    }\n}\n\n/* Merge by computing MAX(registers[i],hll[i]) the HyperLogLog 'hll'\n * with an array of uint8_t HLL_REGISTERS registers pointed by 'max'.\n *\n * The hll object must be already validated via isHLLObjectOrReply()\n * or in some other way.\n *\n * If the HyperLogLog is sparse and is found to be invalid, C_ERR\n * is returned, otherwise the function always succeeds. */\nint hllMerge(uint8_t *max, size_t cmax, robj_roptr hll) {\n    struct hllhdr *hdr = (hllhdr*)ptrFromObj(hll);\n    int i;\n\n    if (hdr->encoding == HLL_DENSE) {\n        uint8_t val;\n\n        for (i = 0; i < HLL_REGISTERS; i++) {\n            HLL_DENSE_GET_REGISTER(val,hdr->registers(),i);\n            if (val > max[i]) max[i] = val;\n        }\n    } else {\n        const uint8_t *p = (const uint8_t*)ptrFromObj(hll), *end = p + sdslen(szFromObj(hll));\n        long runlen, regval;\n\n        p += HLL_HDR_SIZE;\n        i = 0;\n        while(p < end) {\n            if (HLL_SPARSE_IS_ZERO(p)) {\n                runlen = HLL_SPARSE_ZERO_LEN(p);\n                i += runlen;\n                p++;\n            } else if (HLL_SPARSE_IS_XZERO(p)) {\n                runlen = HLL_SPARSE_XZERO_LEN(p);\n                i += runlen;\n                p += 2;\n            } else {\n                runlen = HLL_SPARSE_VAL_LEN(p);\n                regval = HLL_SPARSE_VAL_VALUE(p);\n                if ((runlen + i) > HLL_REGISTERS) break; /* Overflow. */\n                while(runlen--) {\n                    if (i < 0 || (size_t)i >= cmax)\n                        return C_ERR;\n                    if (regval > max[i]) max[i] = regval;\n                    i++;\n                }\n                p++;\n            }\n        }\n        if (i != HLL_REGISTERS) return C_ERR;\n    }\n    return C_OK;\n}\n\n/* ========================== HyperLogLog commands ========================== */\n\n/* Create an HLL object. We always create the HLL using sparse encoding.\n * This will be upgraded to the dense representation as needed. */\nrobj *createHLLObject(void) {\n    robj *o;\n    struct hllhdr *hdr;\n    sds s;\n    uint8_t *p;\n    int sparselen = HLL_HDR_SIZE +\n                    (((HLL_REGISTERS+(HLL_SPARSE_XZERO_MAX_LEN-1)) /\n                     HLL_SPARSE_XZERO_MAX_LEN)*2);\n    int aux;\n\n    /* Populate the sparse representation with as many XZERO opcodes as\n     * needed to represent all the registers. */\n    aux = HLL_REGISTERS;\n    s = sdsnewlen(NULL,sparselen);\n    p = (uint8_t*)s + HLL_HDR_SIZE;\n    while(aux) {\n        int xzero = HLL_SPARSE_XZERO_MAX_LEN;\n        if (xzero > aux) xzero = aux;\n        HLL_SPARSE_XZERO_SET(p,xzero);\n        p += 2;\n        aux -= xzero;\n    }\n    serverAssert((p-(uint8_t*)s) == sparselen);\n\n    /* Create the actual object. */\n    o = createObject(OBJ_STRING,s);\n    hdr = (hllhdr*)ptrFromObj(o);\n    memcpy(hdr->magic,\"HYLL\",4);\n    hdr->encoding = HLL_SPARSE;\n    return o;\n}\n\n/* Check if the object is a String with a valid HLL representation.\n * Return C_OK if this is true, otherwise reply to the client\n * with an error and return C_ERR. */\nint isHLLObjectOrReply(client *c, robj_roptr o) {\n    struct hllhdr *hdr;\n\n    /* Key exists, check type */\n    if (checkType(c,o,OBJ_STRING))\n        return C_ERR; /* Error already sent. */\n\n    if (!sdsEncodedObject(o)) goto invalid;\n    if (stringObjectLen(o) < sizeof(*hdr)) goto invalid;\n    hdr = (hllhdr*)ptrFromObj(o);\n\n    /* Magic should be \"HYLL\". */\n    if (hdr->magic[0] != 'H' || hdr->magic[1] != 'Y' ||\n        hdr->magic[2] != 'L' || hdr->magic[3] != 'L') goto invalid;\n\n    if (hdr->encoding > HLL_MAX_ENCODING) goto invalid;\n\n    /* Dense representation string length should match exactly. */\n    if (hdr->encoding == HLL_DENSE &&\n        stringObjectLen(o) != HLL_DENSE_SIZE) goto invalid;\n\n    /* All tests passed. */\n    return C_OK;\n\ninvalid:\n    addReplyError(c,\"-WRONGTYPE Key is not a valid \"\n               \"HyperLogLog string value.\");\n    return C_ERR;\n}\n\n/* PFADD var ele ele ele ... ele => :0 or :1 */\nvoid pfaddCommand(client *c) {\n    robj *o = lookupKeyWrite(c->db,c->argv[1]);\n    struct hllhdr *hdr;\n    int updated = 0, j;\n\n    if (o == NULL) {\n        /* Create the key with a string value of the exact length to\n         * hold our HLL data structure. sdsnewlen() when NULL is passed\n         * is guaranteed to return bytes initialized to zero. */\n        o = createHLLObject();\n        dbAdd(c->db,c->argv[1],o);\n        updated++;\n    } else {\n        if (isHLLObjectOrReply(c,o) != C_OK) return;\n        o = dbUnshareStringValue(c->db,c->argv[1],o);\n    }\n    /* Perform the low level ADD operation for every element. */\n    for (j = 2; j < c->argc; j++) {\n        int retval = hllAdd(o, (unsigned char*)ptrFromObj(c->argv[j]),\n                               sdslen(szFromObj(c->argv[j])));\n        switch(retval) {\n        case 1:\n            updated++;\n            break;\n        case -1:\n            addReplyError(c,invalid_hll_err);\n            return;\n        }\n    }\n    hdr = (hllhdr*)ptrFromObj(o);\n    if (updated) {\n        signalModifiedKey(c,c->db,c->argv[1]);\n        notifyKeyspaceEvent(NOTIFY_STRING,\"pfadd\",c->argv[1],c->db->id);\n        g_pserver->dirty += updated;\n        HLL_INVALIDATE_CACHE(hdr);\n    }\n    addReply(c, updated ? shared.cone : shared.czero);\n}\n\n/* PFCOUNT var -> approximated cardinality of set. */\nvoid pfcountCommand(client *c) {\n    robj *o;\n    struct hllhdr *hdr;\n    uint64_t card;\n\n    /* Case 1: multi-key keys, cardinality of the union.\n     *\n     * When multiple keys are specified, PFCOUNT actually computes\n     * the cardinality of the merge of the N HLLs specified. */\n    if (c->argc > 2) {\n        uint8_t max[HLL_HDR_SIZE+HLL_REGISTERS], *registers;\n        int j;\n\n        /* Compute an HLL with M[i] = MAX(M[i]_j). */\n        memset(max,0,sizeof(max));\n        hdr = (struct hllhdr*) max;\n        hdr->encoding = HLL_RAW; /* Special internal-only encoding. */\n        registers = max + HLL_HDR_SIZE;\n        for (j = 1; j < c->argc; j++) {\n            /* Check type and size. */\n            robj_roptr o = lookupKeyRead(c->db,c->argv[j]);\n            if (o == nullptr) continue; /* Assume empty HLL for non existing var.*/\n            if (isHLLObjectOrReply(c,o) != C_OK) return;\n\n            /* Merge with this HLL with our 'max' HLL by setting max[i]\n             * to MAX(max[i],hll[i]). */\n            if (hllMerge(registers,HLL_REGISTERS,o) == C_ERR) {\n                addReplyError(c,invalid_hll_err);\n                return;\n            }\n        }\n\n        /* Compute cardinality of the resulting set. */\n        addReplyLongLong(c,hllCount(hdr,NULL));\n        return;\n    }\n\n    /* Case 2: cardinality of the single HLL.\n     *\n     * The user specified a single key. Either return the cached value\n     * or compute one and update the cache. */\n    o = lookupKeyWrite(c->db,c->argv[1]);\n    if (o == NULL) {\n        /* No key? Cardinality is zero since no element was added, otherwise\n         * we would have a key as HLLADD creates it as a side effect. */\n        addReply(c,shared.czero);\n    } else {\n        if (isHLLObjectOrReply(c,o) != C_OK) return;\n        o = dbUnshareStringValue(c->db,c->argv[1],o);\n\n        /* Check if the cached cardinality is valid. */\n        hdr = (hllhdr*)ptrFromObj(o);\n        if (HLL_VALID_CACHE(hdr)) {\n            /* Just return the cached value. */\n            card = (uint64_t)hdr->card[0];\n            card |= (uint64_t)hdr->card[1] << 8;\n            card |= (uint64_t)hdr->card[2] << 16;\n            card |= (uint64_t)hdr->card[3] << 24;\n            card |= (uint64_t)hdr->card[4] << 32;\n            card |= (uint64_t)hdr->card[5] << 40;\n            card |= (uint64_t)hdr->card[6] << 48;\n            card |= (uint64_t)hdr->card[7] << 56;\n        } else {\n            int invalid = 0;\n            /* Recompute it and update the cached value. */\n            card = hllCount(hdr,&invalid);\n            if (invalid) {\n                addReplyError(c,invalid_hll_err);\n                return;\n            }\n            hdr->card[0] = card & 0xff;\n            hdr->card[1] = (card >> 8) & 0xff;\n            hdr->card[2] = (card >> 16) & 0xff;\n            hdr->card[3] = (card >> 24) & 0xff;\n            hdr->card[4] = (card >> 32) & 0xff;\n            hdr->card[5] = (card >> 40) & 0xff;\n            hdr->card[6] = (card >> 48) & 0xff;\n            hdr->card[7] = (card >> 56) & 0xff;\n            /* This is not considered a read-only command even if the\n             * data structure is not modified, since the cached value\n             * may be modified and given that the HLL is a Redis string\n             * we need to propagate the change. */\n            signalModifiedKey(c,c->db,c->argv[1]);\n            g_pserver->dirty++;\n        }\n        addReplyLongLong(c,card);\n    }\n}\n\n/* PFMERGE dest src1 src2 src3 ... srcN => OK */\nvoid pfmergeCommand(client *c) {\n    uint8_t max[HLL_REGISTERS];\n    struct hllhdr *hdr;\n    int j;\n    int use_dense = 0; /* Use dense representation as target? */\n\n    /* Compute an HLL with M[i] = MAX(M[i]_j).\n     * We store the maximum into the max array of registers. We'll write\n     * it to the target variable later. */\n    memset(max,0,sizeof(max));\n    for (j = 1; j < c->argc; j++) {\n        /* Check type and size. */\n        robj_roptr o = lookupKeyRead(c->db,c->argv[j]);\n        if (o == nullptr) continue; /* Assume empty HLL for non existing var. */\n        if (isHLLObjectOrReply(c,o) != C_OK) return;\n\n        /* If at least one involved HLL is dense, use the dense representation\n         * as target ASAP to save time and avoid the conversion step. */\n        hdr = (hllhdr*)ptrFromObj(o);\n        if (hdr->encoding == HLL_DENSE) use_dense = 1;\n\n        /* Merge with this HLL with our 'max' HLL by setting max[i]\n         * to MAX(max[i],hll[i]). */\n        if (hllMerge(max,sizeof(max),o) == C_ERR) {\n            addReplyError(c,invalid_hll_err);\n            return;\n        }\n    }\n\n    /* Create / unshare the destination key's value if needed. */\n    robj *o = lookupKeyWrite(c->db,c->argv[1]);\n    if (o == NULL) {\n        /* Create the key with a string value of the exact length to\n         * hold our HLL data structure. sdsnewlen() when NULL is passed\n         * is guaranteed to return bytes initialized to zero. */\n        o = createHLLObject();\n        dbAdd(c->db,c->argv[1],o);\n    } else {\n        /* If key exists we are sure it's of the right type/size\n         * since we checked when merging the different HLLs, so we\n         * don't check again. */\n        o = dbUnshareStringValue(c->db,c->argv[1],o);\n    }\n\n    /* Convert the destination object to dense representation if at least\n     * one of the inputs was dense. */\n    if (use_dense && hllSparseToDense(o) == C_ERR) {\n        addReplyError(c,invalid_hll_err);\n        return;\n    }\n\n    /* Write the resulting HLL to the destination HLL registers and\n     * invalidate the cached value. */\n    for (j = 0; j < HLL_REGISTERS; j++) {\n        if (max[j] == 0) continue;\n        hdr = (hllhdr*)ptrFromObj(o);\n        switch(hdr->encoding) {\n        case HLL_DENSE: hllDenseSet(hdr->registers(),j,max[j]); break;\n        case HLL_SPARSE: hllSparseSet(o,j,max[j]); break;\n        }\n    }\n    hdr = (hllhdr*)ptrFromObj(o); /* ptrFromObj(o) may be different now, as a side effect of\n                     last hllSparseSet() call. */\n    HLL_INVALIDATE_CACHE(hdr);\n\n    signalModifiedKey(c,c->db,c->argv[1]);\n    /* We generate a PFADD event for PFMERGE for semantical simplicity\n     * since in theory this is a mass-add of elements. */\n    notifyKeyspaceEvent(NOTIFY_STRING,\"pfadd\",c->argv[1],c->db->id);\n    g_pserver->dirty++;\n    addReply(c,shared.ok);\n}\n\n/* ========================== Testing / Debugging  ========================== */\n\n/* PFSELFTEST\n * This command performs a self-test of the HLL registers implementation.\n * Something that is not easy to test from within the outside. */\n#define HLL_TEST_CYCLES 1000\nvoid pfselftestCommand(client *c) {\n    unsigned int j, i;\n    sds bitcounters = sdsnewlen(NULL,HLL_DENSE_SIZE);\n    struct hllhdr *hdr = (struct hllhdr*) bitcounters, *hdr2;\n    robj *o = NULL;\n    uint8_t bytecounters[HLL_REGISTERS];\n    int64_t checkpoint = 1;\n    uint64_t seed = 0;\n    uint64_t ele;\n    double relerr = 0;\n\n    /* Test 1: access registers.\n     * The test is conceived to test that the different counters of our data\n     * structure are accessible and that setting their values both result in\n     * the correct value to be retained and not affect adjacent values. */\n    for (j = 0; j < HLL_TEST_CYCLES; j++) {\n        /* Set the HLL counters and an array of unsigned byes of the\n         * same size to the same set of random values. */\n        for (i = 0; i < HLL_REGISTERS; i++) {\n            unsigned int r = rand() & HLL_REGISTER_MAX;\n\n            bytecounters[i] = r;\n            HLL_DENSE_SET_REGISTER(hdr->registers(),i,r);\n        }\n        /* Check that we are able to retrieve the same values. */\n        for (i = 0; i < HLL_REGISTERS; i++) {\n            unsigned int val;\n\n            HLL_DENSE_GET_REGISTER(val,hdr->registers(),i);\n            if (val != bytecounters[i]) {\n                addReplyErrorFormat(c,\n                    \"TESTFAILED Register %d should be %d but is %d\",\n                    i, (int) bytecounters[i], (int) val);\n                goto cleanup;\n            }\n        }\n    }\n\n    /* Test 2: approximation error.\n     * The test adds unique elements and check that the estimated value\n     * is always reasonable bounds.\n     *\n     * We check that the error is smaller than a few times than the expected\n     * standard error, to make it very unlikely for the test to fail because\n     * of a \"bad\" run.\n     *\n     * The test is performed with both dense and sparse HLLs at the same\n     * time also verifying that the computed cardinality is the same. */\n    memset(hdr->registers(),0,HLL_DENSE_SIZE-HLL_HDR_SIZE);\n    o = createHLLObject();\n    relerr = 1.04/sqrt(HLL_REGISTERS);\n    checkpoint = 1;\n    seed = (uint64_t)rand() | (uint64_t)rand() << 32;\n    for (j = 1; j <= 10000000; j++) {\n        ele = j ^ seed;\n        hllDenseAdd(hdr->registers(),(unsigned char*)&ele,sizeof(ele));\n        hllAdd(o,(unsigned char*)&ele,sizeof(ele));\n\n        /* Make sure that for small cardinalities we use sparse\n         * encoding. */\n        if (j == checkpoint && j < g_pserver->hll_sparse_max_bytes/2) {\n            hdr2 = (hllhdr*)ptrFromObj(o);\n            if (hdr2->encoding != HLL_SPARSE) {\n                addReplyError(c, \"TESTFAILED sparse encoding not used\");\n                goto cleanup;\n            }\n        }\n\n        /* Check that dense and sparse representations agree. */\n        if (j == checkpoint && hllCount(hdr,NULL) != hllCount((hllhdr*)ptrFromObj(o),NULL)) {\n                addReplyError(c, \"TESTFAILED dense/sparse disagree\");\n                goto cleanup;\n        }\n\n        /* Check error. */\n        if (j == checkpoint) {\n            int64_t abserr = checkpoint - (int64_t)hllCount(hdr,NULL);\n            uint64_t maxerr = ceil(relerr*6*checkpoint);\n\n            /* Adjust the max error we expect for cardinality 10\n             * since from time to time it is statistically likely to get\n             * much higher error due to collision, resulting into a false\n             * positive. */\n            if (j == 10) maxerr = 1;\n\n            if (abserr < 0) abserr = -abserr;\n            if (abserr > (int64_t)maxerr) {\n                addReplyErrorFormat(c,\n                    \"TESTFAILED Too big error. card:%llu abserr:%llu\",\n                    (unsigned long long) checkpoint,\n                    (unsigned long long) abserr);\n                goto cleanup;\n            }\n            checkpoint *= 10;\n        }\n    }\n\n    /* Success! */\n    addReply(c,shared.ok);\n\ncleanup:\n    sdsfree(bitcounters);\n    if (o) decrRefCount(o);\n}\n\n/* PFDEBUG <subcommand> <key> ... args ...\n * Different debugging related operations about the HLL implementation. */\nvoid pfdebugCommand(client *c) {\n    char *cmd = szFromObj(c->argv[1]);\n    struct hllhdr *hdr;\n    robj *o;\n    int j;\n\n    o = lookupKeyWrite(c->db,c->argv[2]);\n    if (o == NULL) {\n        addReplyError(c,\"The specified key does not exist\");\n        return;\n    }\n    if (isHLLObjectOrReply(c,o) != C_OK) return;\n    o = dbUnshareStringValue(c->db,c->argv[2],o);\n    hdr = (hllhdr*)ptrFromObj(o);\n\n    /* PFDEBUG GETREG <key> */\n    if (!strcasecmp(cmd,\"getreg\")) {\n        if (c->argc != 3) goto arityerr;\n\n        if (hdr->encoding == HLL_SPARSE) {\n            if (hllSparseToDense(o) == C_ERR) {\n                addReplyError(c,invalid_hll_err);\n                return;\n            }\n            g_pserver->dirty++; /* Force propagation on encoding change. */\n        }\n\n        hdr = (hllhdr*)ptrFromObj(o);\n        addReplyArrayLen(c,HLL_REGISTERS);\n        for (j = 0; j < HLL_REGISTERS; j++) {\n            uint8_t val;\n\n            HLL_DENSE_GET_REGISTER(val,hdr->registers(),j);\n            addReplyLongLong(c,val);\n        }\n    }\n    /* PFDEBUG DECODE <key> */\n    else if (!strcasecmp(cmd,\"decode\")) {\n        if (c->argc != 3) goto arityerr;\n\n        uint8_t *p = (uint8_t*)ptrFromObj(o), *end = p+sdslen(szFromObj(o));\n        sds decoded = sdsempty();\n\n        if (hdr->encoding != HLL_SPARSE) {\n            sdsfree(decoded);\n            addReplyError(c,\"HLL encoding is not sparse\");\n            return;\n        }\n\n        p += HLL_HDR_SIZE;\n        while(p < end) {\n            int runlen, regval;\n\n            if (HLL_SPARSE_IS_ZERO(p)) {\n                runlen = HLL_SPARSE_ZERO_LEN(p);\n                p++;\n                decoded = sdscatprintf(decoded,\"z:%d \",runlen);\n            } else if (HLL_SPARSE_IS_XZERO(p)) {\n                runlen = HLL_SPARSE_XZERO_LEN(p);\n                p += 2;\n                decoded = sdscatprintf(decoded,\"Z:%d \",runlen);\n            } else {\n                runlen = HLL_SPARSE_VAL_LEN(p);\n                regval = HLL_SPARSE_VAL_VALUE(p);\n                p++;\n                decoded = sdscatprintf(decoded,\"v:%d,%d \",regval,runlen);\n            }\n        }\n        decoded = sdstrim(decoded,\" \");\n        addReplyBulkCBuffer(c,decoded,sdslen(decoded));\n        sdsfree(decoded);\n    }\n    /* PFDEBUG ENCODING <key> */\n    else if (!strcasecmp(cmd,\"encoding\")) {\n        const char *encodingstr[2] = {\"dense\",\"sparse\"};\n        if (c->argc != 3) goto arityerr;\n\n        addReplyStatus(c,encodingstr[hdr->encoding]);\n    }\n    /* PFDEBUG TODENSE <key> */\n    else if (!strcasecmp(cmd,\"todense\")) {\n        int conv = 0;\n        if (c->argc != 3) goto arityerr;\n\n        if (hdr->encoding == HLL_SPARSE) {\n            if (hllSparseToDense(o) == C_ERR) {\n                addReplyError(c,invalid_hll_err);\n                return;\n            }\n            conv = 1;\n            g_pserver->dirty++; /* Force propagation on encoding change. */\n        }\n        addReply(c,conv ? shared.cone : shared.czero);\n    } else {\n        addReplyErrorFormat(c,\"Unknown PFDEBUG subcommand '%s'\", cmd);\n    }\n    return;\n\narityerr:\n    addReplyErrorFormat(c,\n        \"Wrong number of arguments for the '%s' subcommand\",cmd);\n}\n\n"
  },
  {
    "path": "src/intset.c",
    "content": "/*\n * Copyright (c) 2009-2012, Pieter Noordhuis <pcnoordhuis at gmail dot com>\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include \"intset.h\"\n#include \"zmalloc.h\"\n#include \"endianconv.h\"\n#include \"redisassert.h\"\n\n/* Note that these encodings are ordered, so:\n * INTSET_ENC_INT16 < INTSET_ENC_INT32 < INTSET_ENC_INT64. */\n#define INTSET_ENC_INT16 (sizeof(int16_t))\n#define INTSET_ENC_INT32 (sizeof(int32_t))\n#define INTSET_ENC_INT64 (sizeof(int64_t))\n\n/* Return the required encoding for the provided value. */\nstatic uint8_t _intsetValueEncoding(int64_t v) {\n    if (v < INT32_MIN || v > INT32_MAX)\n        return INTSET_ENC_INT64;\n    else if (v < INT16_MIN || v > INT16_MAX)\n        return INTSET_ENC_INT32;\n    else\n        return INTSET_ENC_INT16;\n}\n\n/* Return the value at pos, given an encoding. */\nstatic int64_t _intsetGetEncoded(intset *is, int pos, uint8_t enc) {\n    int64_t v64;\n    int32_t v32;\n    int16_t v16;\n\n    if (enc == INTSET_ENC_INT64) {\n        memcpy(&v64,((int64_t*)is->contents)+pos,sizeof(v64));\n        memrev64ifbe(&v64);\n        return v64;\n    } else if (enc == INTSET_ENC_INT32) {\n        memcpy(&v32,((int32_t*)is->contents)+pos,sizeof(v32));\n        memrev32ifbe(&v32);\n        return v32;\n    } else {\n        memcpy(&v16,((int16_t*)is->contents)+pos,sizeof(v16));\n        memrev16ifbe(&v16);\n        return v16;\n    }\n}\n\n/* Return the value at pos, using the configured encoding. */\nstatic int64_t _intsetGet(intset *is, int pos) {\n    return _intsetGetEncoded(is,pos,intrev32ifbe(is->encoding));\n}\n\n/* Set the value at pos, using the configured encoding. */\nstatic void _intsetSet(intset *is, int pos, int64_t value) {\n    uint32_t encoding = intrev32ifbe(is->encoding);\n\n    if (encoding == INTSET_ENC_INT64) {\n        ((int64_t*)is->contents)[pos] = value;\n        memrev64ifbe(((int64_t*)is->contents)+pos);\n    } else if (encoding == INTSET_ENC_INT32) {\n        ((int32_t*)is->contents)[pos] = value;\n        memrev32ifbe(((int32_t*)is->contents)+pos);\n    } else {\n        ((int16_t*)is->contents)[pos] = value;\n        memrev16ifbe(((int16_t*)is->contents)+pos);\n    }\n}\n\n/* Create an empty intset. */\nintset *intsetNew(void) {\n    intset *is = zmalloc(sizeof(intset), MALLOC_SHARED);\n    is->encoding = intrev32ifbe(INTSET_ENC_INT16);\n    is->length = 0;\n    return is;\n}\n\n/* Resize the intset */\nstatic intset *intsetResize(intset *is, uint32_t len) {\n    uint64_t size = (uint64_t)len*intrev32ifbe(is->encoding);\n    assert(size <= SIZE_MAX - sizeof(intset));\n    is = zrealloc(is,sizeof(intset)+size, MALLOC_SHARED);\n    return is;\n}\n\n/* Search for the position of \"value\". Return 1 when the value was found and\n * sets \"pos\" to the position of the value within the intset. Return 0 when\n * the value is not present in the intset and sets \"pos\" to the position\n * where \"value\" can be inserted. */\nstatic uint8_t intsetSearch(intset *is, int64_t value, uint32_t *pos) {\n    int min = 0, max = intrev32ifbe(is->length)-1, mid = -1;\n    int64_t cur = -1;\n\n    /* The value can never be found when the set is empty */\n    if (intrev32ifbe(is->length) == 0) {\n        if (pos) *pos = 0;\n        return 0;\n    } else {\n        /* Check for the case where we know we cannot find the value,\n         * but do know the insert position. */\n        if (value > _intsetGet(is,max)) {\n            if (pos) *pos = intrev32ifbe(is->length);\n            return 0;\n        } else if (value < _intsetGet(is,0)) {\n            if (pos) *pos = 0;\n            return 0;\n        }\n    }\n\n    while(max >= min) {\n        mid = ((unsigned int)min + (unsigned int)max) >> 1;\n        cur = _intsetGet(is,mid);\n        if (value > cur) {\n            min = mid+1;\n        } else if (value < cur) {\n            max = mid-1;\n        } else {\n            break;\n        }\n    }\n\n    if (value == cur) {\n        if (pos) *pos = mid;\n        return 1;\n    } else {\n        if (pos) *pos = min;\n        return 0;\n    }\n}\n\n/* Upgrades the intset to a larger encoding and inserts the given integer. */\nstatic intset *intsetUpgradeAndAdd(intset *is, int64_t value) {\n    uint8_t curenc = intrev32ifbe(is->encoding);\n    uint8_t newenc = _intsetValueEncoding(value);\n    int length = intrev32ifbe(is->length);\n    int prepend = value < 0 ? 1 : 0;\n\n    /* First set new encoding and resize */\n    is->encoding = intrev32ifbe(newenc);\n    is = intsetResize(is,intrev32ifbe(is->length)+1);\n\n    /* Upgrade back-to-front so we don't overwrite values.\n     * Note that the \"prepend\" variable is used to make sure we have an empty\n     * space at either the beginning or the end of the intset. */\n    while(length--)\n        _intsetSet(is,length+prepend,_intsetGetEncoded(is,length,curenc));\n\n    /* Set the value at the beginning or the end. */\n    if (prepend)\n        _intsetSet(is,0,value);\n    else\n        _intsetSet(is,intrev32ifbe(is->length),value);\n    is->length = intrev32ifbe(intrev32ifbe(is->length)+1);\n    return is;\n}\n\nstatic void intsetMoveTail(intset *is, uint32_t from, uint32_t to) {\n    void *src, *dst;\n    uint32_t bytes = intrev32ifbe(is->length)-from;\n    uint32_t encoding = intrev32ifbe(is->encoding);\n\n    if (encoding == INTSET_ENC_INT64) {\n        src = (int64_t*)is->contents+from;\n        dst = (int64_t*)is->contents+to;\n        bytes *= sizeof(int64_t);\n    } else if (encoding == INTSET_ENC_INT32) {\n        src = (int32_t*)is->contents+from;\n        dst = (int32_t*)is->contents+to;\n        bytes *= sizeof(int32_t);\n    } else {\n        src = (int16_t*)is->contents+from;\n        dst = (int16_t*)is->contents+to;\n        bytes *= sizeof(int16_t);\n    }\n    memmove(dst,src,bytes);\n}\n\n/* Insert an integer in the intset */\nintset *intsetAdd(intset *is, int64_t value, uint8_t *success) {\n    uint8_t valenc = _intsetValueEncoding(value);\n    uint32_t pos;\n    if (success) *success = 1;\n\n    /* Upgrade encoding if necessary. If we need to upgrade, we know that\n     * this value should be either appended (if > 0) or prepended (if < 0),\n     * because it lies outside the range of existing values. */\n    if (valenc > intrev32ifbe(is->encoding)) {\n        /* This always succeeds, so we don't need to curry *success. */\n        return intsetUpgradeAndAdd(is,value);\n    } else {\n        /* Abort if the value is already present in the set.\n         * This call will populate \"pos\" with the right position to insert\n         * the value when it cannot be found. */\n        if (intsetSearch(is,value,&pos)) {\n            if (success) *success = 0;\n            return is;\n        }\n\n        is = intsetResize(is,intrev32ifbe(is->length)+1);\n        if (pos < intrev32ifbe(is->length)) intsetMoveTail(is,pos,pos+1);\n    }\n\n    _intsetSet(is,pos,value);\n    is->length = intrev32ifbe(intrev32ifbe(is->length)+1);\n    return is;\n}\n\n/* Delete integer from intset */\nintset *intsetRemove(intset *is, int64_t value, int *success) {\n    uint8_t valenc = _intsetValueEncoding(value);\n    uint32_t pos;\n    if (success) *success = 0;\n\n    if (valenc <= intrev32ifbe(is->encoding) && intsetSearch(is,value,&pos)) {\n        uint32_t len = intrev32ifbe(is->length);\n\n        /* We know we can delete */\n        if (success) *success = 1;\n\n        /* Overwrite value with tail and update length */\n        if (pos < (len-1)) intsetMoveTail(is,pos+1,pos);\n        is = intsetResize(is,len-1);\n        is->length = intrev32ifbe(len-1);\n    }\n    return is;\n}\n\n/* Determine whether a value belongs to this set */\nuint8_t intsetFind(intset *is, int64_t value) {\n    uint8_t valenc = _intsetValueEncoding(value);\n    return valenc <= intrev32ifbe(is->encoding) && intsetSearch(is,value,NULL);\n}\n\n/* Return random member */\nint64_t intsetRandom(intset *is) {\n    uint32_t len = intrev32ifbe(is->length);\n    assert(len); /* avoid division by zero on corrupt intset payload. */\n    return _intsetGet(is,rand()%len);\n}\n\n/* Get the value at the given position. When this position is\n * out of range the function returns 0, when in range it returns 1. */\nuint8_t intsetGet(intset *is, uint32_t pos, int64_t *value) {\n    if (pos < intrev32ifbe(is->length)) {\n        *value = _intsetGet(is,pos);\n        return 1;\n    }\n    return 0;\n}\n\n/* Return intset length */\nuint32_t intsetLen(const intset *is) {\n    return intrev32ifbe(is->length);\n}\n\n/* Return intset blob size in bytes. */\nsize_t intsetBlobLen(intset *is) {\n    return sizeof(intset)+(size_t)intrev32ifbe(is->length)*intrev32ifbe(is->encoding);\n}\n\n/* Validate the integrity of the data structure.\n * when `deep` is 0, only the integrity of the header is validated.\n * when `deep` is 1, we make sure there are no duplicate or out of order records. */\nint intsetValidateIntegrity(const unsigned char *p, size_t size, int deep) {\n    intset *is = (intset *)p;\n    /* check that we can actually read the header. */\n    if (size < sizeof(*is))\n        return 0;\n\n    uint32_t encoding = intrev32ifbe(is->encoding);\n\n    size_t record_size;\n    if (encoding == INTSET_ENC_INT64) {\n        record_size = INTSET_ENC_INT64;\n    } else if (encoding == INTSET_ENC_INT32) {\n        record_size = INTSET_ENC_INT32;\n    } else if (encoding == INTSET_ENC_INT16){\n        record_size = INTSET_ENC_INT16;\n    } else {\n        return 0;\n    }\n\n    /* check that the size matchies (all records are inside the buffer). */\n    uint32_t count = intrev32ifbe(is->length);\n    if (sizeof(*is) + count*record_size != size)\n        return 0;\n\n    /* check that the set is not empty. */\n    if (count==0)\n        return 0;\n\n    if (!deep)\n        return 1;\n\n    /* check that there are no dup or out of order records. */\n    int64_t prev = _intsetGet(is,0);\n    for (uint32_t i=1; i<count; i++) {\n        int64_t cur = _intsetGet(is,i);\n        if (cur <= prev)\n            return 0;\n        prev = cur;\n    }\n\n    return 1;\n}\n\n#ifdef REDIS_TEST\n#include <sys/time.h>\n#include <time.h>\n\n#if 0\nstatic void intsetRepr(intset *is) {\n    for (uint32_t i = 0; i < intrev32ifbe(is->length); i++) {\n        printf(\"%lld\\n\", (uint64_t)_intsetGet(is,i));\n    }\n    printf(\"\\n\");\n}\n\nstatic void error(char *err) {\n    printf(\"%s\\n\", err);\n    exit(1);\n}\n#endif\n\nstatic void ok(void) {\n    printf(\"OK\\n\");\n}\n\nstatic long long usec(void) {\n    struct timeval tv;\n    gettimeofday(&tv,NULL);\n    return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;\n}\n\nstatic intset *createSet(int bits, int size) {\n    uint64_t mask = (1<<bits)-1;\n    uint64_t value;\n    intset *is = intsetNew();\n\n    for (int i = 0; i < size; i++) {\n        if (bits > 32) {\n            value = (rand()*rand()) & mask;\n        } else {\n            value = rand() & mask;\n        }\n        is = intsetAdd(is,value,NULL);\n    }\n    return is;\n}\n\nstatic void checkConsistency(intset *is) {\n    for (uint32_t i = 0; i < (intrev32ifbe(is->length)-1); i++) {\n        uint32_t encoding = intrev32ifbe(is->encoding);\n\n        if (encoding == INTSET_ENC_INT16) {\n            int16_t *i16 = (int16_t*)is->contents;\n            assert(i16[i] < i16[i+1]);\n        } else if (encoding == INTSET_ENC_INT32) {\n            int32_t *i32 = (int32_t*)is->contents;\n            assert(i32[i] < i32[i+1]);\n        } else {\n            int64_t *i64 = (int64_t*)is->contents;\n            assert(i64[i] < i64[i+1]);\n        }\n    }\n}\n\n#define UNUSED(x) (void)(x)\nint intsetTest(int argc, char **argv, int accurate) {\n    uint8_t success;\n    int i;\n    intset *is;\n    srand(time(NULL));\n\n    UNUSED(argc);\n    UNUSED(argv);\n    UNUSED(accurate);\n\n    printf(\"Value encodings: \"); {\n        assert(_intsetValueEncoding(-32768) == INTSET_ENC_INT16);\n        assert(_intsetValueEncoding(+32767) == INTSET_ENC_INT16);\n        assert(_intsetValueEncoding(-32769) == INTSET_ENC_INT32);\n        assert(_intsetValueEncoding(+32768) == INTSET_ENC_INT32);\n        assert(_intsetValueEncoding(-2147483648) == INTSET_ENC_INT32);\n        assert(_intsetValueEncoding(+2147483647) == INTSET_ENC_INT32);\n        assert(_intsetValueEncoding(-2147483649) == INTSET_ENC_INT64);\n        assert(_intsetValueEncoding(+2147483648) == INTSET_ENC_INT64);\n        assert(_intsetValueEncoding(-9223372036854775808ull) ==\n                    INTSET_ENC_INT64);\n        assert(_intsetValueEncoding(+9223372036854775807ull) ==\n                    INTSET_ENC_INT64);\n        ok();\n    }\n\n    printf(\"Basic adding: \"); {\n        is = intsetNew();\n        is = intsetAdd(is,5,&success); assert(success);\n        is = intsetAdd(is,6,&success); assert(success);\n        is = intsetAdd(is,4,&success); assert(success);\n        is = intsetAdd(is,4,&success); assert(!success);\n        ok();\n        zfree(is);\n    }\n\n    printf(\"Large number of random adds: \"); {\n        uint32_t inserts = 0;\n        is = intsetNew();\n        for (i = 0; i < 1024; i++) {\n            is = intsetAdd(is,rand()%0x800,&success);\n            if (success) inserts++;\n        }\n        assert(intrev32ifbe(is->length) == inserts);\n        checkConsistency(is);\n        ok();\n        zfree(is);\n    }\n\n    printf(\"Upgrade from int16 to int32: \"); {\n        is = intsetNew();\n        is = intsetAdd(is,32,NULL);\n        assert(intrev32ifbe(is->encoding) == INTSET_ENC_INT16);\n        is = intsetAdd(is,65535,NULL);\n        assert(intrev32ifbe(is->encoding) == INTSET_ENC_INT32);\n        assert(intsetFind(is,32));\n        assert(intsetFind(is,65535));\n        checkConsistency(is);\n        zfree(is);\n\n        is = intsetNew();\n        is = intsetAdd(is,32,NULL);\n        assert(intrev32ifbe(is->encoding) == INTSET_ENC_INT16);\n        is = intsetAdd(is,-65535,NULL);\n        assert(intrev32ifbe(is->encoding) == INTSET_ENC_INT32);\n        assert(intsetFind(is,32));\n        assert(intsetFind(is,-65535));\n        checkConsistency(is);\n        ok();\n        zfree(is);\n    }\n\n    printf(\"Upgrade from int16 to int64: \"); {\n        is = intsetNew();\n        is = intsetAdd(is,32,NULL);\n        assert(intrev32ifbe(is->encoding) == INTSET_ENC_INT16);\n        is = intsetAdd(is,4294967295,NULL);\n        assert(intrev32ifbe(is->encoding) == INTSET_ENC_INT64);\n        assert(intsetFind(is,32));\n        assert(intsetFind(is,4294967295));\n        checkConsistency(is);\n        zfree(is);\n\n        is = intsetNew();\n        is = intsetAdd(is,32,NULL);\n        assert(intrev32ifbe(is->encoding) == INTSET_ENC_INT16);\n        is = intsetAdd(is,-4294967295,NULL);\n        assert(intrev32ifbe(is->encoding) == INTSET_ENC_INT64);\n        assert(intsetFind(is,32));\n        assert(intsetFind(is,-4294967295));\n        checkConsistency(is);\n        ok();\n        zfree(is);\n    }\n\n    printf(\"Upgrade from int32 to int64: \"); {\n        is = intsetNew();\n        is = intsetAdd(is,65535,NULL);\n        assert(intrev32ifbe(is->encoding) == INTSET_ENC_INT32);\n        is = intsetAdd(is,4294967295,NULL);\n        assert(intrev32ifbe(is->encoding) == INTSET_ENC_INT64);\n        assert(intsetFind(is,65535));\n        assert(intsetFind(is,4294967295));\n        checkConsistency(is);\n        zfree(is);\n\n        is = intsetNew();\n        is = intsetAdd(is,65535,NULL);\n        assert(intrev32ifbe(is->encoding) == INTSET_ENC_INT32);\n        is = intsetAdd(is,-4294967295,NULL);\n        assert(intrev32ifbe(is->encoding) == INTSET_ENC_INT64);\n        assert(intsetFind(is,65535));\n        assert(intsetFind(is,-4294967295));\n        checkConsistency(is);\n        ok();\n        zfree(is);\n    }\n\n    printf(\"Stress lookups: \"); {\n        long num = 100000, size = 10000;\n        int i, bits = 20;\n        long long start;\n        is = createSet(bits,size);\n        checkConsistency(is);\n\n        start = usec();\n        for (i = 0; i < num; i++) intsetSearch(is,rand() % ((1<<bits)-1),NULL);\n        printf(\"%ld lookups, %ld element set, %lldusec\\n\",\n               num,size,usec()-start);\n        zfree(is);\n    }\n\n    printf(\"Stress add+delete: \"); {\n        int i, v1, v2;\n        is = intsetNew();\n        for (i = 0; i < 0xffff; i++) {\n            v1 = rand() % 0xfff;\n            is = intsetAdd(is,v1,NULL);\n            assert(intsetFind(is,v1));\n\n            v2 = rand() % 0xfff;\n            is = intsetRemove(is,v2,NULL);\n            assert(!intsetFind(is,v2));\n        }\n        checkConsistency(is);\n        ok();\n        zfree(is);\n    }\n\n    return 0;\n}\n#endif\n"
  },
  {
    "path": "src/intset.h",
    "content": "/*\n * Copyright (c) 2009-2012, Pieter Noordhuis <pcnoordhuis at gmail dot com>\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __INTSET_H\n#define __INTSET_H\n#include <stdint.h>\n\n#ifdef __cplusplus\n#define ZERO_LENGTH_ARRAY_LENGTH 1\n#else\n#define ZERO_LENGTH_ARRAY_LENGTH\n#endif\n\ntypedef struct intset {\n    uint32_t encoding;\n    uint32_t length;\n#ifndef __cplusplus\n    int8_t contents[];\n#endif\n} intset;\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\nintset *intsetNew(void);\nintset *intsetAdd(intset *is, int64_t value, uint8_t *success);\nintset *intsetRemove(intset *is, int64_t value, int *success);\nuint8_t intsetFind(intset *is, int64_t value);\nint64_t intsetRandom(intset *is);\nuint8_t intsetGet(intset *is, uint32_t pos, int64_t *value);\nuint32_t intsetLen(const intset *is);\nsize_t intsetBlobLen(intset *is);\nint intsetValidateIntegrity(const unsigned char *is, size_t size, int deep);\n\n#ifdef REDIS_TEST\nint intsetTest(int argc, char *argv[], int accurate);\n#endif\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif // __INTSET_H\n"
  },
  {
    "path": "src/keydb-diagnostic-tool.cpp",
    "content": "/* KeyDB diagnostic utility.\n *\n * Copyright (c) 2009-2021, Salvatore Sanfilippo <antirez at gmail dot com>\n * Copyright (c) 2021, EQ Alpha Technology Ltd. <john at eqalpha dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"fmacros.h\"\n\n#include <stdio.h>\n#include <string.h>\n#include <stdlib.h>\n#include <unistd.h>\n#include <errno.h>\n#include <time.h>\n#include <sys/resource.h>\n#include <sys/time.h>\n#include <signal.h>\n#include <assert.h>\n#include <math.h>\n#include <pthread.h>\n#include <deque>\nextern \"C\" {\n#include <sds.h> /* Use hiredis sds. */\n#include \"sdscompat.h\"\n#include \"hiredis.h\"\n}\n#include \"ae.h\"\n#include \"adlist.h\"\n#include \"dict.h\"\n#include \"zmalloc.h\"\n#include \"storage.h\"\n#include \"atomicvar.h\"\n#include \"crc16_slottable.h\"\n\n#define UNUSED(V) ((void) V)\n#define RANDPTR_INITIAL_SIZE 8\n#define MAX_LATENCY_PRECISION 3\n#define MAX_THREADS 500\n#define CLUSTER_SLOTS 16384\n\n#define CLIENT_GET_EVENTLOOP(c) \\\n    (c->thread_id >= 0 ? config.threads[c->thread_id]->el : config.el)\n\nstruct benchmarkThread;\nstruct clusterNode;\nstruct redisConfig;\n\nint g_fTestMode = false;\n\nstatic struct config {\n    aeEventLoop *el;\n    const char *hostip;\n    int hostport;\n    const char *hostsocket;\n    int numclients;\n    int liveclients;\n    int period_ms;\n    int requests;\n    int requests_issued;\n    int requests_finished;\n    int keysize;\n    int datasize;\n    int randomkeys;\n    int randomkeys_keyspacelen;\n    int keepalive;\n    int pipeline;\n    int showerrors;\n    long long start;\n    long long totlatency;\n    long long *latency;\n    const char *title;\n    list *clients;\n    int quiet;\n    int csv;\n    int loop;\n    int idlemode;\n    int dbnum;\n    sds dbnumstr;\n    char *tests;\n    char *auth;\n    const char *user;\n    int precision;\n    int max_threads;\n    struct benchmarkThread **threads;\n    int cluster_mode;\n    int cluster_node_count;\n    struct clusterNode **cluster_nodes;\n    struct redisConfig *redis_config;\n    int is_fetching_slots;\n    int is_updating_slots;\n    int slots_last_update;\n    int enable_tracking;\n    /* Thread mutexes to be used as fallbacks by atomicvar.h */\n    pthread_mutex_t requests_issued_mutex;\n    pthread_mutex_t requests_finished_mutex;\n    pthread_mutex_t liveclients_mutex;\n    pthread_mutex_t is_fetching_slots_mutex;\n    pthread_mutex_t is_updating_slots_mutex;\n    pthread_mutex_t updating_slots_mutex;\n    pthread_mutex_t slots_last_update_mutex;\n} config;\n\ntypedef struct _client {\n    redisContext *context;\n    sds obuf;\n    char **randptr;         /* Pointers to :rand: strings inside the command buf */\n    size_t randlen;         /* Number of pointers in client->randptr */\n    size_t randfree;        /* Number of unused pointers in client->randptr */\n    char **stagptr;         /* Pointers to slot hashtags (cluster mode only) */\n    size_t staglen;         /* Number of pointers in client->stagptr */\n    size_t stagfree;        /* Number of unused pointers in client->stagptr */\n    size_t written;         /* Bytes of 'obuf' already written */\n    long long start;        /* Start time of a request */\n    long long latency;      /* Request latency */\n    int pending;            /* Number of pending requests (replies to consume) */\n    int prefix_pending;     /* If non-zero, number of pending prefix commands. Commands\n                               such as auth and select are prefixed to the pipeline of\n                               benchmark commands and discarded after the first send. */\n    int prefixlen;          /* Size in bytes of the pending prefix commands */\n    int thread_id;\n    struct clusterNode *cluster_node;\n    int slots_last_update;\n    redisReply *lastReply;\n} *client;\n\n/* Threads. */\n\ntypedef struct benchmarkThread {\n    int index;\n    pthread_t thread;\n    aeEventLoop *el;\n} benchmarkThread;\n\n/* Cluster. */\ntypedef struct clusterNode {\n    char *ip;\n    int port;\n    sds name;\n    int flags;\n    sds replicate;  /* Master ID if node is a replica */\n    int *slots;\n    int slots_count;\n    int current_slot_index;\n    int *updated_slots;         /* Used by updateClusterSlotsConfiguration */\n    int updated_slots_count;    /* Used by updateClusterSlotsConfiguration */\n    int replicas_count;\n    sds *migrating; /* An array of sds where even strings are slots and odd\n                     * strings are the destination node IDs. */\n    sds *importing; /* An array of sds where even strings are slots and odd\n                     * strings are the source node IDs. */\n    int migrating_count; /* Length of the migrating array (migrating slots*2) */\n    int importing_count; /* Length of the importing array (importing slots*2) */\n    struct redisConfig *redis_config;\n} clusterNode;\n\ntypedef struct redisConfig {\n    sds save;\n    sds appendonly;\n} redisConfig;\n\nint g_fInCrash = false;\n\n/* Prototypes */\nstatic void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask);\nstatic benchmarkThread *createBenchmarkThread(int index);\nstatic void freeBenchmarkThread(benchmarkThread *thread);\nstatic void freeBenchmarkThreads();\nstatic redisContext *getRedisContext(const char *ip, int port,\n                                     const char *hostsocket);\n\n/* Implementation */\nstatic long long ustime(void) {\n    struct timeval tv;\n    long long ust;\n\n    gettimeofday(&tv, NULL);\n    ust = ((long)tv.tv_sec)*1000000;\n    ust += tv.tv_usec;\n    return ust;\n}\n\n/* _serverAssert is needed by dict */\nextern \"C\" void _serverAssert(const char *estr, const char *file, int line) {\n    fprintf(stderr, \"=== ASSERTION FAILED ===\");\n    fprintf(stderr, \"==> %s:%d '%s' is not true\",file,line,estr);\n    *((char*)-1) = 'x';\n}\n\n/* asyncFreeDictTable is needed by dict */\nextern \"C\" void asyncFreeDictTable(struct dictEntry **de) {\n    zfree(de);\n}\n\nstatic redisContext *getRedisContext(const char *ip, int port,\n                                     const char *hostsocket)\n{\n    redisContext *ctx = NULL;\n    redisReply *reply =  NULL;\n    if (hostsocket == NULL)\n        ctx = redisConnect(ip, port);\n    else\n        ctx = redisConnectUnix(hostsocket);\n    if (ctx == NULL || ctx->err) {\n        fprintf(stderr,\"Could not connect to Redis at \");\n        const char *err = (ctx != NULL ? ctx->errstr : \"\");\n        if (hostsocket == NULL)\n            fprintf(stderr,\"%s:%d: %s\\n\",ip,port,err);\n        else\n            fprintf(stderr,\"%s: %s\\n\",hostsocket,err);\n        goto cleanup;\n    }\n    if (config.auth == NULL)\n        return ctx;\n    if (config.user == NULL)\n        reply = (redisReply*)redisCommand(ctx,\"AUTH %s\", config.auth);\n    else\n        reply = (redisReply*)redisCommand(ctx,\"AUTH %s %s\", config.user, config.auth);\n    if (reply != NULL) {\n        if (reply->type == REDIS_REPLY_ERROR) {\n            if (hostsocket == NULL)\n                fprintf(stderr, \"Node %s:%d replied with error:\\n%s\\n\", ip, port, reply->str);\n            else\n                fprintf(stderr, \"Node %s replied with error:\\n%s\\n\", hostsocket, reply->str);\n            goto cleanup;\n        }\n        freeReplyObject(reply);\n        return ctx;\n    }\n    fprintf(stderr, \"ERROR: failed to fetch reply from \");\n    if (hostsocket == NULL)\n        fprintf(stderr, \"%s:%d\\n\", ip, port);\n    else\n        fprintf(stderr, \"%s\\n\", hostsocket);\ncleanup:\n    freeReplyObject(reply);\n    redisFree(ctx);\n    return NULL;\n}\n\nstatic void freeClient(client c) {\n    aeEventLoop *el = CLIENT_GET_EVENTLOOP(c);\n    listNode *ln;\n    aeDeleteFileEvent(el,c->context->fd,AE_WRITABLE);\n    aeDeleteFileEvent(el,c->context->fd,AE_READABLE);\n    if (c->thread_id >= 0) {\n        int requests_finished = 0;\n        atomicGet(config.requests_finished, requests_finished);\n        if (requests_finished >= config.requests) {\n            aeStop(el);\n        }\n    }\n    redisFree(c->context);\n    sdsfree(c->obuf);\n    zfree(c->randptr);\n    zfree(c->stagptr);\n    zfree(c);\n    if (config.max_threads) pthread_mutex_lock(&(config.liveclients_mutex));\n    config.liveclients--;\n    ln = listSearchKey(config.clients,c);\n    assert(ln != NULL);\n    listDelNode(config.clients,ln);\n    if (config.max_threads) pthread_mutex_unlock(&(config.liveclients_mutex));\n}\n\nstatic void freeAllClients(void) {\n    listNode *ln = config.clients->head, *next;\n\n    while(ln) {\n        next = ln->next;\n        freeClient((client)ln->value);\n        ln = next;\n    }\n}\n\nstatic void resetClient(client c) {\n    aeEventLoop *el = CLIENT_GET_EVENTLOOP(c);\n    aeDeleteFileEvent(el,c->context->fd,AE_WRITABLE);\n    aeDeleteFileEvent(el,c->context->fd,AE_READABLE);\n    aeCreateFileEvent(el,c->context->fd,AE_WRITABLE,writeHandler,c);\n    c->written = 0;\n    c->pending = config.pipeline;\n}\n\nstatic void randomizeClientKey(client c) {\n    size_t i;\n\n    for (i = 0; i < c->randlen; i++) {\n        char *p = c->randptr[i]+11;\n        size_t r = 0;\n        if (config.randomkeys_keyspacelen != 0)\n            r = random() % config.randomkeys_keyspacelen;\n        size_t j;\n\n        for (j = 0; j < 12; j++) {\n            *p = '0'+r%10;\n            r/=10;\n            p--;\n        }\n    }\n}\n\nstatic void readHandler(aeEventLoop *el, int fd, void *privdata, int mask) {\n    client c = (client)privdata;\n    void *reply = NULL;\n    UNUSED(el);\n    UNUSED(fd);\n    UNUSED(mask);\n\n    /* Calculate latency only for the first read event. This means that the\n     * server already sent the reply and we need to parse it. Parsing overhead\n     * is not part of the latency, so calculate it only once, here. */\n    if (c->latency < 0) c->latency = ustime()-(c->start);\n\n    if (redisBufferRead(c->context) != REDIS_OK) {\n        fprintf(stderr,\"Error: %s\\n\",c->context->errstr);\n        exit(1);\n    } else {\n        while(c->pending) {\n            if (redisGetReply(c->context,&reply) != REDIS_OK) {\n                fprintf(stderr,\"Error: %s\\n\",c->context->errstr);\n                exit(1);\n            }\n            if (reply != NULL) {\n                if (reply == (void*)REDIS_REPLY_ERROR) {\n                    fprintf(stderr,\"Unexpected error reply, exiting...\\n\");\n                    exit(1);\n                }\n                redisReply *r = (redisReply*)reply;\n                int is_err = (r->type == REDIS_REPLY_ERROR);\n\n                if (is_err && config.showerrors) {\n                    /* TODO: static lasterr_time not thread-safe */\n                    static time_t lasterr_time = 0;\n                    time_t now = time(NULL);\n                    if (lasterr_time != now) {\n                        lasterr_time = now;\n                        if (c->cluster_node) {\n                            printf(\"Error from server %s:%d: %s\\n\",\n                                   c->cluster_node->ip,\n                                   c->cluster_node->port,\n                                   r->str);\n                        } else printf(\"Error from server: %s\\n\", r->str);\n                    }\n                }\n\n                freeReplyObject(reply);\n                /* This is an OK for prefix commands such as auth and select.*/\n                if (c->prefix_pending > 0) {\n                    c->prefix_pending--;\n                    c->pending--;\n                    /* Discard prefix commands on first response.*/\n                    if (c->prefixlen > 0) {\n                        size_t j;\n                        sdsrange(c->obuf, c->prefixlen, -1);\n                        /* We also need to fix the pointers to the strings\n                        * we need to randomize. */\n                        for (j = 0; j < c->randlen; j++)\n                            c->randptr[j] -= c->prefixlen;\n                        c->prefixlen = 0;\n                    }\n                    continue;\n                }\n                int requests_finished = 0;\n                atomicGetIncr(config.requests_finished, requests_finished, 1);\n                if (requests_finished < config.requests)\n                    config.latency[requests_finished] = c->latency;\n                c->pending--;\n                if (c->pending == 0) {\n                    resetClient(c);\n                    break;\n                }\n            } else {\n                break;\n            }\n        }\n    }\n}\n\nstatic void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask) {\n    client c = (client)privdata;\n    UNUSED(el);\n    UNUSED(fd);\n    UNUSED(mask);\n\n    /* Initialize request when nothing was written. */\n    if (c->written == 0) {\n        /* Really initialize: randomize keys and set start time. */\n        if (config.randomkeys) randomizeClientKey(c);\n        atomicGet(config.slots_last_update, c->slots_last_update);\n        c->start = ustime();\n        c->latency = -1;\n    }\n    if (sdslen(c->obuf) > c->written) {\n        void *ptr = c->obuf+c->written;\n        ssize_t nwritten = write(c->context->fd,ptr,sdslen(c->obuf)-c->written);\n        if (nwritten == -1) {\n            if (errno != EPIPE)\n                fprintf(stderr, \"Writing to socket: %s\\n\", strerror(errno));\n            freeClient(c);\n            return;\n        }\n        c->written += nwritten;\n        if (sdslen(c->obuf) == c->written) {\n            aeDeleteFileEvent(el,c->context->fd,AE_WRITABLE);\n            aeCreateFileEvent(el,c->context->fd,AE_READABLE,readHandler,c);\n        }\n    }\n}\n\n/* Create a benchmark client, configured to send the command passed as 'cmd' of\n * 'len' bytes.\n *\n * The command is copied N times in the client output buffer (that is reused\n * again and again to send the request to the server) accordingly to the configured\n * pipeline size.\n *\n * Also an initial SELECT command is prepended in order to make sure the right\n * database is selected, if needed. The initial SELECT will be discarded as soon\n * as the first reply is received.\n *\n * To create a client from scratch, the 'from' pointer is set to NULL. If instead\n * we want to create a client using another client as reference, the 'from' pointer\n * points to the client to use as reference. In such a case the following\n * information is take from the 'from' client:\n *\n * 1) The command line to use.\n * 2) The offsets of the __rand_int__ elements inside the command line, used\n *    for arguments randomization.\n *\n * Even when cloning another client, prefix commands are applied if needed.*/\nstatic client createClient(const char *cmd, size_t len, client from, int thread_id) {\n    int j;\n    int is_cluster_client = (config.cluster_mode && thread_id >= 0);\n    client c = (client)zmalloc(sizeof(struct _client), MALLOC_LOCAL);\n\n    const char *ip = NULL;\n    int port = 0;\n    c->cluster_node = NULL;\n    if (config.hostsocket == NULL || is_cluster_client) {\n        if (!is_cluster_client) {\n            ip = config.hostip;\n            port = config.hostport;\n        } else {\n            int node_idx = 0;\n            if (config.max_threads < config.cluster_node_count)\n                node_idx = config.liveclients % config.cluster_node_count;\n            else\n                node_idx = thread_id % config.cluster_node_count;\n            clusterNode *node = config.cluster_nodes[node_idx];\n            assert(node != NULL);\n            ip = (const char *) node->ip;\n            port = node->port;\n            c->cluster_node = node;\n        }\n        c->context = redisConnectNonBlock(ip,port);\n    } else {\n        c->context = redisConnectUnixNonBlock(config.hostsocket);\n    }\n    if (c->context->err) {\n        fprintf(stderr,\"Could not connect to Redis at \");\n        if (config.hostsocket == NULL || is_cluster_client)\n            fprintf(stderr,\"%s:%d: %s\\n\",ip,port,c->context->errstr);\n        else\n            fprintf(stderr,\"%s: %s\\n\",config.hostsocket,c->context->errstr);\n        exit(1);\n    }\n    c->thread_id = thread_id;\n    /* Suppress hiredis cleanup of unused buffers for max speed. */\n    c->context->reader->maxbuf = 0;\n\n    /* Build the request buffer:\n     * Queue N requests accordingly to the pipeline size, or simply clone\n     * the example client buffer. */\n    c->obuf = sdsempty();\n    /* Prefix the request buffer with AUTH and/or SELECT commands, if applicable.\n     * These commands are discarded after the first response, so if the client is\n     * reused the commands will not be used again. */\n    c->prefix_pending = 0;\n    if (config.auth) {\n        char *buf = NULL;\n        int len;\n        if (config.user == NULL)\n            len = redisFormatCommand(&buf, \"AUTH %s\", config.auth);\n        else\n            len = redisFormatCommand(&buf, \"AUTH %s %s\",\n                                     config.user, config.auth);\n        c->obuf = sdscatlen(c->obuf, buf, len);\n        free(buf);\n        c->prefix_pending++;\n    }\n\n    if (config.enable_tracking) {\n        char *buf = NULL;\n        int len = redisFormatCommand(&buf, \"CLIENT TRACKING on\");\n        c->obuf = sdscatlen(c->obuf, buf, len);\n        free(buf);\n        c->prefix_pending++;\n    }\n\n    /* If a DB number different than zero is selected, prefix our request\n     * buffer with the SELECT command, that will be discarded the first\n     * time the replies are received, so if the client is reused the\n     * SELECT command will not be used again. */\n    if (config.dbnum != 0 && !is_cluster_client) {\n        c->obuf = sdscatprintf(c->obuf,\"*2\\r\\n$6\\r\\nSELECT\\r\\n$%d\\r\\n%s\\r\\n\",\n            (int)sdslen(config.dbnumstr),config.dbnumstr);\n        c->prefix_pending++;\n    }\n    c->prefixlen = sdslen(c->obuf);\n    /* Append the request itself. */\n    if (from) {\n        c->obuf = sdscatlen(c->obuf,\n            from->obuf+from->prefixlen,\n            sdslen(from->obuf)-from->prefixlen);\n    } else {\n        for (j = 0; j < config.pipeline; j++)\n            c->obuf = sdscatlen(c->obuf,cmd,len);\n    }\n\n    c->written = 0;\n    c->pending = config.pipeline+c->prefix_pending;\n    c->randptr = NULL;\n    c->randlen = 0;\n    c->stagptr = NULL;\n    c->staglen = 0;\n\n    /* Find substrings in the output buffer that need to be randomized. */\n    if (config.randomkeys) {\n        if (from) {\n            c->randlen = from->randlen;\n            c->randfree = 0;\n            c->randptr = (char**)zmalloc(sizeof(char*)*c->randlen, MALLOC_LOCAL);\n            /* copy the offsets. */\n            for (j = 0; j < (int)c->randlen; j++) {\n                c->randptr[j] = c->obuf + (from->randptr[j]-from->obuf);\n                /* Adjust for the different select prefix length. */\n                c->randptr[j] += c->prefixlen - from->prefixlen;\n            }\n        } else {\n            char *p = c->obuf;\n\n            c->randlen = 0;\n            c->randfree = RANDPTR_INITIAL_SIZE;\n            c->randptr = (char**)zmalloc(sizeof(char*)*c->randfree, MALLOC_LOCAL);\n            while ((p = strstr(p,\"__rand_int__\")) != NULL) {\n                if (c->randfree == 0) {\n                    c->randptr = (char**)zrealloc(c->randptr,sizeof(char*)*c->randlen*2, MALLOC_LOCAL);\n                    c->randfree += c->randlen;\n                }\n                c->randptr[c->randlen++] = p;\n                c->randfree--;\n                p += 12; /* 12 is strlen(\"__rand_int__). */\n            }\n        }\n    }\n    /* If cluster mode is enabled, set slot hashtags pointers. */\n    if (config.cluster_mode) {\n        if (from) {\n            c->staglen = from->staglen;\n            c->stagfree = 0;\n            c->stagptr = (char**)zmalloc(sizeof(char*)*c->staglen, MALLOC_LOCAL);\n            /* copy the offsets. */\n            for (j = 0; j < (int)c->staglen; j++) {\n                c->stagptr[j] = c->obuf + (from->stagptr[j]-from->obuf);\n                /* Adjust for the different select prefix length. */\n                c->stagptr[j] += c->prefixlen - from->prefixlen;\n            }\n        } else {\n            char *p = c->obuf;\n\n            c->staglen = 0;\n            c->stagfree = RANDPTR_INITIAL_SIZE;\n            c->stagptr = (char**)zmalloc(sizeof(char*)*c->stagfree, MALLOC_LOCAL);\n            while ((p = strstr(p,\"{tag}\")) != NULL) {\n                if (c->stagfree == 0) {\n                    c->stagptr = (char**)zrealloc(c->stagptr,\n                                          sizeof(char*) * c->staglen*2, MALLOC_LOCAL);\n                    c->stagfree += c->staglen;\n                }\n                c->stagptr[c->staglen++] = p;\n                c->stagfree--;\n                p += 5; /* 5 is strlen(\"{tag}\"). */\n            }\n        }\n    }\n    aeEventLoop *el = NULL;\n    if (thread_id < 0) el = config.el;\n    else {\n        benchmarkThread *thread = config.threads[thread_id];\n        el = thread->el;\n    }\n    if (config.idlemode == 0)\n        aeCreateFileEvent(el,c->context->fd,AE_WRITABLE,writeHandler,c);\n    listAddNodeTail(config.clients,c);\n    atomicIncr(config.liveclients, 1);\n    atomicGet(config.slots_last_update, c->slots_last_update);\n    return c;\n}\n\nstatic void initBenchmarkThreads() {\n    int i;\n    if (config.threads) freeBenchmarkThreads();\n    config.threads = (benchmarkThread**)zmalloc(config.max_threads * sizeof(benchmarkThread*), MALLOC_LOCAL);\n    for (i = 0; i < config.max_threads; i++) {\n        benchmarkThread *thread = createBenchmarkThread(i);\n        config.threads[i] = thread;\n    }\n}\n\n/* Thread functions. */\n\nstatic benchmarkThread *createBenchmarkThread(int index) {\n    benchmarkThread *thread = (benchmarkThread*)zmalloc(sizeof(*thread), MALLOC_LOCAL);\n    if (thread == NULL) return NULL;\n    thread->index = index;\n    thread->el = aeCreateEventLoop(1024*10);\n    return thread;\n}\n\nstatic void freeBenchmarkThread(benchmarkThread *thread) {\n    if (thread->el) aeDeleteEventLoop(thread->el);\n    zfree(thread);\n}\n\nstatic void freeBenchmarkThreads() {\n    int i = 0;\n    for (; i < config.max_threads; i++) {\n        benchmarkThread *thread = config.threads[i];\n        if (thread) freeBenchmarkThread(thread);\n    }\n    zfree(config.threads);\n    config.threads = NULL;\n}\n\nstatic void *execBenchmarkThread(void *ptr) {\n    benchmarkThread *thread = (benchmarkThread *) ptr;\n    aeMain(thread->el);\n    return NULL;\n}\n\nvoid initConfigDefaults() {\n    config.numclients = 50;\n    config.requests = 100000;\n    config.liveclients = 0;\n    config.el = aeCreateEventLoop(1024*10);\n    config.keepalive = 1;\n    config.datasize = 3;\n    config.pipeline = 1;\n    config.period_ms = 5000;\n    config.showerrors = 0;\n    config.randomkeys = 0;\n    config.randomkeys_keyspacelen = 0;\n    config.quiet = 0;\n    config.csv = 0;\n    config.loop = 0;\n    config.idlemode = 0;\n    config.latency = NULL;\n    config.clients = listCreate();\n    config.hostip = \"127.0.0.1\";\n    config.hostport = 6379;\n    config.hostsocket = NULL;\n    config.tests = NULL;\n    config.dbnum = 0;\n    config.auth = NULL;\n    config.precision = 1;\n    config.max_threads = MAX_THREADS;\n    config.threads = NULL;\n    config.cluster_mode = 0;\n    config.cluster_node_count = 0;\n    config.cluster_nodes = NULL;\n    config.redis_config = NULL;\n    config.is_fetching_slots = 0;\n    config.is_updating_slots = 0;\n    config.slots_last_update = 0;\n    config.enable_tracking = 0;\n}\n\n/* Returns number of consumed options. */\nint parseOptions(int argc, const char **argv) {\n    int i;\n    int lastarg;\n    int exit_status = 1;\n\n    for (i = 1; i < argc; i++) {\n        lastarg = (i == (argc-1));\n\n        if (!strcmp(argv[i],\"-c\") || !strcmp(argv[i],\"--clients\")) {\n            if (lastarg) goto invalid;\n            config.numclients = atoi(argv[++i]);\n        } else if (!strcmp(argv[i],\"--time\")) {\n            if (lastarg) goto invalid;\n            config.period_ms = atoi(argv[++i]);\n            if (config.period_ms <= 0) {\n                printf(\"Warning: Invalid value for thread time. Defaulting to 5000ms.\\n\");\n                config.period_ms = 5000;\n            }\n        } else if (!strcmp(argv[i],\"-h\") || !strcmp(argv[i],\"--host\")) {\n            if (lastarg) goto invalid;\n            config.hostip = strdup(argv[++i]);\n        } else if (!strcmp(argv[i],\"-p\") || !strcmp(argv[i],\"--port\")) {\n            if (lastarg) goto invalid;\n            config.hostport = atoi(argv[++i]);\n        } else if (!strcmp(argv[i],\"-s\")) {\n            if (lastarg) goto invalid;\n            config.hostsocket = strdup(argv[++i]);\n        } else if (!strcmp(argv[i],\"--password\") ) {\n            if (lastarg) goto invalid;\n            config.auth = strdup(argv[++i]);\n        } else if (!strcmp(argv[i],\"--user\")) {\n            if (lastarg) goto invalid;\n            config.user = argv[++i];\n        } else if (!strcmp(argv[i],\"--dbnum\")) {\n            if (lastarg) goto invalid;\n            config.dbnum = atoi(argv[++i]);\n            config.dbnumstr = sdsfromlonglong(config.dbnum);\n        } else if (!strcmp(argv[i],\"-t\") || !strcmp(argv[i],\"--threads\")) {\n            if (lastarg) goto invalid;\n            config.max_threads = atoi(argv[++i]);\n            if (config.max_threads > MAX_THREADS) {\n                printf(\"Warning: Too many threads, limiting threads to %d.\\n\", MAX_THREADS);\n                config.max_threads = MAX_THREADS;\n            } else if (config.max_threads <= 0) {\n                printf(\"Warning: Invalid value for max threads. Defaulting to %d.\\n\", MAX_THREADS);\n                config.max_threads = MAX_THREADS;\n            }\n        } else if (!strcmp(argv[i],\"--help\")) {\n            exit_status = 0;\n            goto usage;\n        } else {\n            /* Assume the user meant to provide an option when the arg starts\n             * with a dash. We're done otherwise and should use the remainder\n             * as the command and arguments for running the benchmark. */\n            if (argv[i][0] == '-') goto invalid;\n            return i;\n        }\n    }\n\n    return i;\n\ninvalid:\n    printf(\"Invalid option \\\"%s\\\" or option argument missing\\n\\n\",argv[i]);\n\nusage:\n    printf(\n\"Usage: keydb-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests>] [-k <boolean>]\\n\\n\"\n\" -h, --host <hostname>       Server hostname (default 127.0.0.1)\\n\"\n\" -p, --port <port>           Server port (default 6379)\\n\"\n\" -c <clients>                Number of parallel connections (default 50)\\n\"\n\" -t, --threads <threads>     Maximum number of threads to start before ending\\n\"\n\" --time <time>               Time between spinning up new client threads, in milliseconds\\n\"\n\" --dbnum <db>                Select the specified DB number (default 0)\\n\"\n\" --user <username>           Used to send ACL style 'AUTH username pass'. Needs -a.\\n\"\n\" --password <password>       Password for Redis Auth\\n\\n\"\n);\n    exit(exit_status);\n}\n\nint extractPropertyFromInfo(const char *info, const char *key, double &val) {\n    char *line = strstr((char*)info, key);\n    if (line == nullptr) return 1;\n    line += strlen(key) + 1; // Skip past key name and following colon\n    char *newline = strchr(line, '\\n');\n    *newline = 0; // Terminate string after relevant line\n    val = strtod(line, nullptr);\n    return 0;\n}\n\nint extractPropertyFromInfo(const char *info, const char *key, unsigned int &val) {\n    char *line = strstr((char*)info, key);\n    if (line == nullptr) return 1;\n    line += strlen(key) + 1; // Skip past key name and following colon\n    char *newline = strchr(line, '\\n');\n    *newline = 0; // Terminate string after relevant line\n    val = atoi(line);\n    return 0;\n}\n\ndouble getSelfCpuTime(struct rusage *self_ru) {\n    getrusage(RUSAGE_SELF, self_ru);\n    double user_time = self_ru->ru_utime.tv_sec + (self_ru->ru_utime.tv_usec / (double)1000000);\n    double system_time = self_ru->ru_stime.tv_sec + (self_ru->ru_stime.tv_usec / (double)1000000);\n    return user_time + system_time;\n}\n\ndouble getServerCpuTime(redisContext *ctx) {\n    redisReply *reply = (redisReply*)redisCommand(ctx, \"INFO CPU\");\n    if (reply->type != REDIS_REPLY_STRING) {\n        freeReplyObject(reply);\n        printf(\"Error executing INFO command. Exiting.\\n\");\n        return -1;\n    }\n\n    double used_cpu_user, used_cpu_sys;\n    if (extractPropertyFromInfo(reply->str, \"used_cpu_user\", used_cpu_user)) {\n        printf(\"Error reading user CPU usage from INFO command. Exiting.\\n\");\n        return -1;\n    }\n    if (extractPropertyFromInfo(reply->str, \"used_cpu_sys\", used_cpu_sys)) {\n        printf(\"Error reading system CPU usage from INFO command. Exiting.\\n\");\n        return -1;\n    }\n    freeReplyObject(reply);\n    return used_cpu_user + used_cpu_sys;\n}\n\ndouble getMean(std::deque<double> *q) {\n    double sum = 0;\n    for (long unsigned int i = 0; i < q->size(); i++) {\n        sum += (*q)[i];\n    }\n    return sum / q->size();\n}\n\nbool isAtFullLoad(double cpuPercent, unsigned int threads) {\n    return cpuPercent / threads >= 96;\n}\n\nint main(int argc, const char **argv) {\n    int i;\n    \n    storage_init(NULL, 0);\n\n    srandom(time(NULL));\n    signal(SIGHUP, SIG_IGN);\n    signal(SIGPIPE, SIG_IGN);\n\n    initConfigDefaults();\n\n    i = parseOptions(argc,argv);\n    argc -= i;\n    argv += i;\n\n    config.latency = (long long*)zmalloc(sizeof(long long)*config.requests, MALLOC_LOCAL);\n\n    if (config.max_threads > 0) {\n        int err = 0;\n        err |= pthread_mutex_init(&(config.requests_issued_mutex), NULL);\n        err |= pthread_mutex_init(&(config.requests_finished_mutex), NULL);\n        err |= pthread_mutex_init(&(config.liveclients_mutex), NULL);\n        err |= pthread_mutex_init(&(config.is_fetching_slots_mutex), NULL);\n        err |= pthread_mutex_init(&(config.is_updating_slots_mutex), NULL);\n        err |= pthread_mutex_init(&(config.updating_slots_mutex), NULL);\n        err |= pthread_mutex_init(&(config.slots_last_update_mutex), NULL);\n        if (err != 0)\n        {\n            perror(\"Failed to initialize mutex\");\n            exit(EXIT_FAILURE);\n        }\n    }\n\n    const char *set_value = \"abcdefghijklmnopqrstuvwxyz\";\n    int self_threads = 0;\n    char command[63];\n\n    initBenchmarkThreads();\n    redisContext *ctx = getRedisContext(config.hostip, config.hostport, config.hostsocket);\n    double server_cpu_time, last_server_cpu_time = getServerCpuTime(ctx);\n    struct rusage self_ru;\n    double self_cpu_time, last_self_cpu_time = getSelfCpuTime(&self_ru);\n    double server_cpu_load, last_server_cpu_load = 0, self_cpu_load, server_cpu_gain;\n    std::deque<double> load_gain_history = {};\n    double current_gain_avg, peak_gain_avg = 0;\n\n    redisReply *reply = (redisReply*)redisCommand(ctx, \"INFO CPU\");\n    if (reply->type != REDIS_REPLY_STRING) {\n        freeReplyObject(reply);\n        printf(\"Error executing INFO command. Exiting.\\r\\n\");\n        return 1;\n    }\n    unsigned int server_threads;\n    if (extractPropertyFromInfo(reply->str, \"server_threads\", server_threads)) {\n        printf(\"Error reading server threads from INFO command. Exiting.\\r\\n\");\n        return 1;\n    }\n    freeReplyObject(reply);\n\n    printf(\"Server has %d threads.\\nStarting...\\n\", server_threads);\n    fflush(stdout);\n\n    while (self_threads < config.max_threads) {\n        for (int i = 0; i < config.numclients; i++) {\n            snprintf(command, sizeof(command), \"SET %d %s\\r\\n\", self_threads * config.numclients + i, set_value);\n            createClient(command, strlen(command), NULL,self_threads);\n        }\n\n        benchmarkThread *t = config.threads[self_threads];\n        if (pthread_create(&(t->thread), NULL, execBenchmarkThread, t)){\n            fprintf(stderr, \"FATAL: Failed to start thread %d. Exiting.\\n\", self_threads);\n            exit(1);\n        }\n        self_threads++;\n\n        usleep(config.period_ms * 1000);\n        \n        server_cpu_time = getServerCpuTime(ctx);\n        self_cpu_time = getSelfCpuTime(&self_ru);\n        server_cpu_load = (server_cpu_time - last_server_cpu_time) * 100000 / config.period_ms;\n        self_cpu_load = (self_cpu_time - last_self_cpu_time) * 100000 / config.period_ms;\n        if (server_cpu_time < 0) {\n            break;\n        }\n        printf(\"%d threads, %d total clients. CPU Usage Self: %.1f%% (%.1f%% per thread), Server: %.1f%% (%.1f%% per thread)\\r\",\n                self_threads,\n                self_threads * config.numclients,\n                self_cpu_load,\n                self_cpu_load / self_threads,\n                server_cpu_load,\n                server_cpu_load / server_threads);\n        fflush(stdout);\n        server_cpu_gain = server_cpu_load - last_server_cpu_load;\n        load_gain_history.push_back(server_cpu_gain);\n        if (load_gain_history.size() > 5) {\n            load_gain_history.pop_front();\n        }\n        current_gain_avg = getMean(&load_gain_history);\n        if (current_gain_avg > peak_gain_avg) {\n            peak_gain_avg = current_gain_avg;\n        }\n        last_server_cpu_time = server_cpu_time;\n        last_self_cpu_time = self_cpu_time;\n        last_server_cpu_load = server_cpu_load;\n\n        if (isAtFullLoad(server_cpu_load, server_threads)) {\n            printf(\"\\nServer is at full CPU load. If higher performance is expected, check server configuration.\\n\");\n            break;\n        }\n\n        if (current_gain_avg <= 0.05 * peak_gain_avg) {\n            printf(\"\\nServer CPU load appears to have stagnated with increasing clients.\\n\"\n                   \"Server does not appear to be at full load. Check network for throughput.\\n\");\n            break;\n        }\n\n        if (self_threads * config.numclients > 2000) {\n            printf(\"\\nClient limit of 2000 reached. Server is not at full load and appears to be increasing.\\n\"\n                   \"2000 clients should be more than enough to reach a bottleneck. Check all configuration.\\n\");\n        }\n    }\n\n    printf(\"Done.\\n\");\n\n    freeAllClients();\n    freeBenchmarkThreads();\n\n    return 0;\n}\n \n"
  },
  {
    "path": "src/keydbutils.cpp",
    "content": "#include \"server.h\"\n\nnamespace keydbutils\n{\n    template<>\n    size_t hash(const sdsview& t)\n    {\n        return (size_t)dictGenHashFunction(static_cast<const char*>(t), t.size());\n    }\n}"
  },
  {
    "path": "src/latency.cpp",
    "content": "/* The latency monitor allows to easily observe the sources of latency\n * in a Redis instance using the LATENCY command. Different latency\n * sources are monitored, like disk I/O, execution of commands, fork\n * system call, and so forth.\n *\n * ----------------------------------------------------------------------------\n *\n * Copyright (c) 2014, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n\n/* Dictionary type for latency events. */\nint dictStringKeyCompare(void *privdata, const void *key1, const void *key2) {\n    UNUSED(privdata);\n    return strcmp((const char*)key1,(const char*)key2) == 0;\n}\n\nuint64_t dictStringHash(const void *key) {\n    return dictGenHashFunction(key, strlen((const char*)key));\n}\n\nvoid dictVanillaFree(void *privdata, void *val);\n\ndictType latencyTimeSeriesDictType = {\n    dictStringHash,             /* hash function */\n    NULL,                       /* key dup */\n    NULL,                       /* val dup */\n    dictStringKeyCompare,       /* key compare */\n    dictVanillaFree,            /* key destructor */\n    dictVanillaFree,            /* val destructor */\n    NULL                        /* allow to expand */\n};\n\n/* ------------------------- Utility functions ------------------------------ */\n\n#ifdef __linux__\n#include <sys/prctl.h>\n/* Returns 1 if Transparent Huge Pages support is enabled in the kernel.\n * Otherwise (or if we are unable to check) 0 is returned. */\nint THPIsEnabled(void) {\n    char buf[1024];\n\n    FILE *fp = fopen(\"/sys/kernel/mm/transparent_hugepage/enabled\",\"r\");\n    if (!fp) return 0;\n    if (fgets(buf,sizeof(buf),fp) == NULL) {\n        fclose(fp);\n        return 0;\n    }\n    fclose(fp);\n    return (strstr(buf,\"[always]\") != NULL) ? 1 : 0;\n}\n\n/* since linux-3.5, kernel supports to set the state of the \"THP disable\" flag\n * for the calling thread. PR_SET_THP_DISABLE is defined in linux/prctl.h */\nint THPDisable(void) {\n    int ret = -EINVAL;\n\n    if (!g_pserver->disable_thp)\n        return ret;\n\n#ifdef PR_SET_THP_DISABLE\n    ret = prctl(PR_SET_THP_DISABLE, 1, 0, 0, 0);\n#endif\n\n    return ret;\n}\n#endif\n\n/* Report the amount of AnonHugePages in smap, in bytes. If the return\n * value of the function is non-zero, the process is being targeted by\n * THP support, and is likely to have memory usage / latency issues. */\nint THPGetAnonHugePagesSize(void) {\n    return zmalloc_get_smap_bytes_by_field(\"AnonHugePages:\",-1);\n}\n\n/* ---------------------------- Latency API --------------------------------- */\n\n/* Latency monitor initialization. We just need to create the dictionary\n * of time series, each time series is created on demand in order to avoid\n * having a fixed list to maintain. */\nvoid latencyMonitorInit(void) {\n    g_pserver->latency_events = dictCreate(&latencyTimeSeriesDictType,NULL);\n}\n\n/* Add the specified sample to the specified time series \"event\".\n * This function is usually called via latencyAddSampleIfNeeded(), that\n * is a macro that only adds the sample if the latency is higher than\n * g_pserver->latency_monitor_threshold. */\nvoid latencyAddSample(const char *event, mstime_t latency) {\n    struct latencyTimeSeries *ts = (latencyTimeSeries*)dictFetchValue(g_pserver->latency_events,event);\n    time_t now = time(NULL);\n    int prev;\n\n    /* Create the time series if it does not exist. */\n    if (ts == NULL) {\n        ts = (latencyTimeSeries*)zmalloc(sizeof(*ts), MALLOC_SHARED);\n        ts->idx = 0;\n        ts->max = 0;\n        memset(ts->samples,0,sizeof(ts->samples));\n        dictAdd(g_pserver->latency_events,zstrdup(event),ts);\n    }\n\n    if (latency > ts->max) ts->max = latency;\n\n    /* If the previous sample is in the same second, we update our old sample\n     * if this latency is > of the old one, or just return. */\n    prev = (ts->idx + LATENCY_TS_LEN - 1) % LATENCY_TS_LEN;\n    if (ts->samples[prev].time == now) {\n        if (latency > ts->samples[prev].latency)\n            ts->samples[prev].latency = latency;\n        return;\n    }\n\n    ts->samples[ts->idx].time = time(NULL);\n    ts->samples[ts->idx].latency = latency;\n\n    ts->idx++;\n    if (ts->idx == LATENCY_TS_LEN) ts->idx = 0;\n}\n\n/* Reset data for the specified event, or all the events data if 'event' is\n * NULL.\n *\n * Note: this is O(N) even when event_to_reset is not NULL because makes\n * the code simpler and we have a small fixed max number of events. */\nint latencyResetEvent(char *event_to_reset) {\n    dictIterator *di;\n    dictEntry *de;\n    int resets = 0;\n\n    di = dictGetSafeIterator(g_pserver->latency_events);\n    while((de = dictNext(di)) != NULL) {\n        char *event = (char*)dictGetKey(de);\n\n        if (event_to_reset == NULL || strcasecmp(event,event_to_reset) == 0) {\n            dictDelete(g_pserver->latency_events, event);\n            resets++;\n        }\n    }\n    dictReleaseIterator(di);\n    return resets;\n}\n\n/* ------------------------ Latency reporting (doctor) ---------------------- */\n\n/* Analyze the samples available for a given event and return a structure\n * populate with different metrics, average, MAD, min, max, and so forth.\n * Check latency.h definition of struct latencyStats for more info.\n * If the specified event has no elements the structure is populate with\n * zero values. */\nvoid analyzeLatencyForEvent(char *event, struct latencyStats *ls) {\n    struct latencyTimeSeries *ts = (latencyTimeSeries*)dictFetchValue(g_pserver->latency_events,event);\n    int j;\n    uint64_t sum;\n\n    ls->all_time_high = ts ? ts->max : 0;\n    ls->avg = 0;\n    ls->min = 0;\n    ls->max = 0;\n    ls->mad = 0;\n    ls->samples = 0;\n    ls->period = 0;\n    if (!ts) return;\n\n    /* First pass, populate everything but the MAD. */\n    sum = 0;\n    for (j = 0; j < LATENCY_TS_LEN; j++) {\n        if (ts->samples[j].time == 0) continue;\n        ls->samples++;\n        if (ls->samples == 1) {\n            ls->min = ls->max = ts->samples[j].latency;\n        } else {\n            if (ls->min > ts->samples[j].latency)\n                ls->min = ts->samples[j].latency;\n            if (ls->max < ts->samples[j].latency)\n                ls->max = ts->samples[j].latency;\n        }\n        sum += ts->samples[j].latency;\n\n        /* Track the oldest event time in ls->period. */\n        if (ls->period == 0 || ts->samples[j].time < ls->period)\n            ls->period = ts->samples[j].time;\n    }\n\n    /* So far avg is actually the sum of the latencies, and period is\n     * the oldest event time. We need to make the first an average and\n     * the second a range of seconds. */\n    if (ls->samples) {\n        ls->avg = sum / ls->samples;\n        ls->period = time(NULL) - ls->period;\n        if (ls->period == 0) ls->period = 1;\n    }\n\n    /* Second pass, compute MAD. */\n    sum = 0;\n    for (j = 0; j < LATENCY_TS_LEN; j++) {\n        int64_t delta;\n\n        if (ts->samples[j].time == 0) continue;\n        delta = (int64_t)ls->avg - ts->samples[j].latency;\n        if (delta < 0) delta = -delta;\n        sum += delta;\n    }\n    if (ls->samples) ls->mad = sum / ls->samples;\n}\n\n/* Create a human readable report of latency events for this KeyDB instance. */\nsds createLatencyReport(void) {\n    sds report = sdsempty();\n    int advise_better_vm = 0;       /* Better virtual machines. */\n    int advise_slowlog_enabled = 0; /* Enable slowlog. */\n    int advise_slowlog_tuning = 0;  /* Reconfigure slowlog. */\n    int advise_slowlog_inspect = 0; /* Check your slowlog. */\n    int advise_disk_contention = 0; /* Try to lower disk contention. */\n    int advise_scheduler = 0;       /* Intrinsic latency. */\n    int advise_data_writeback = 0;  /* data=writeback. */\n    int advise_no_appendfsync = 0;  /* don't fsync during rewrites. */\n    int advise_local_disk = 0;      /* Avoid remote disks. */\n    int advise_ssd = 0;             /* Use an SSD drive. */\n    int advise_write_load_info = 0; /* Print info about AOF and write load. */\n    int advise_hz = 0;              /* Use higher HZ. */\n    int advise_large_objects = 0;   /* Deletion of large objects. */\n    int advise_mass_eviction = 0;   /* Avoid mass eviction of keys. */\n    int advise_relax_fsync_policy = 0; /* appendfsync always is slow. */\n    int advise_disable_thp = 0;     /* AnonHugePages detected. */\n    int advices = 0;\n\n    /* Return ASAP if the latency engine is disabled and it looks like it\n     * was never enabled so far. */\n    if (dictSize(g_pserver->latency_events) == 0 &&\n        g_pserver->latency_monitor_threshold == 0)\n    {\n        report = sdscat(report,\"I'm sorry, Dave, I can't do that. Latency monitoring is disabled in this KeyDB instance. You may use \\\"CONFIG SET latency-monitor-threshold <milliseconds>.\\\" in order to enable it.\\n\");\n        return report;\n    }\n\n    /* Show all the events stats and add for each event some event-related\n     * comment depending on the values. */\n    dictIterator *di;\n    dictEntry *de;\n    int eventnum = 0;\n\n    di = dictGetSafeIterator(g_pserver->latency_events);\n    while((de = dictNext(di)) != NULL) {\n        char *event = (char*)dictGetKey(de);\n        struct latencyTimeSeries *ts = (latencyTimeSeries*)dictGetVal(de);\n        struct latencyStats ls;\n\n        if (ts == NULL) continue;\n        eventnum++;\n        if (eventnum == 1) {\n            report = sdscat(report,\"Dave, I have observed latency spikes in this KeyDB instance. You don't mind talking about it, do you Dave?\\n\\n\");\n        }\n        analyzeLatencyForEvent(event,&ls);\n\n        report = sdscatprintf(report,\n            \"%d. %s: %d latency spikes (average %lums, mean deviation %lums, period %.2f sec). Worst all time event %lums.\",\n            eventnum, event,\n            ls.samples,\n            (unsigned long) ls.avg,\n            (unsigned long) ls.mad,\n            (double) ls.period/ls.samples,\n            (unsigned long) ts->max);\n\n        /* Fork */\n        if (!strcasecmp(event,\"fork\")) {\n            const char *fork_quality;\n            if (g_pserver->stat_fork_rate < 10) {\n                fork_quality = \"terrible\";\n                advise_better_vm = 1;\n                advices++;\n            } else if (g_pserver->stat_fork_rate < 25) {\n                fork_quality = \"poor\";\n                advise_better_vm = 1;\n                advices++;\n            } else if (g_pserver->stat_fork_rate < 100) {\n                fork_quality = \"good\";\n            } else {\n                fork_quality = \"excellent\";\n            }\n            report = sdscatprintf(report,\n                \" Fork rate is %.2f GB/sec (%s).\", g_pserver->stat_fork_rate,\n                fork_quality);\n        }\n\n        /* Potentially commands. */\n        if (!strcasecmp(event,\"command\")) {\n            if (g_pserver->slowlog_log_slower_than < 0) {\n                advise_slowlog_enabled = 1;\n                advices++;\n            } else if (g_pserver->slowlog_log_slower_than/1000 >\n                       g_pserver->latency_monitor_threshold)\n            {\n                advise_slowlog_tuning = 1;\n                advices++;\n            }\n            advise_slowlog_inspect = 1;\n            advise_large_objects = 1;\n            advices += 2;\n        }\n\n        /* fast-command. */\n        if (!strcasecmp(event,\"fast-command\")) {\n            advise_scheduler = 1;\n            advices++;\n        }\n\n        /* AOF and I/O. */\n        if (!strcasecmp(event,\"aof-write-pending-fsync\")) {\n            advise_local_disk = 1;\n            advise_disk_contention = 1;\n            advise_ssd = 1;\n            advise_data_writeback = 1;\n            advices += 4;\n        }\n\n        if (!strcasecmp(event,\"aof-write-active-child\")) {\n            advise_no_appendfsync = 1;\n            advise_data_writeback = 1;\n            advise_ssd = 1;\n            advices += 3;\n        }\n\n        if (!strcasecmp(event,\"aof-write-alone\")) {\n            advise_local_disk = 1;\n            advise_data_writeback = 1;\n            advise_ssd = 1;\n            advices += 3;\n        }\n\n        if (!strcasecmp(event,\"aof-fsync-always\")) {\n            advise_relax_fsync_policy = 1;\n            advices++;\n        }\n\n        if (!strcasecmp(event,\"aof-fstat\") ||\n            !strcasecmp(event,\"rdb-unlink-temp-file\")) {\n            advise_disk_contention = 1;\n            advise_local_disk = 1;\n            advices += 2;\n        }\n\n        if (!strcasecmp(event,\"aof-rewrite-diff-write\") ||\n            !strcasecmp(event,\"aof-rename\")) {\n            advise_write_load_info = 1;\n            advise_data_writeback = 1;\n            advise_ssd = 1;\n            advise_local_disk = 1;\n            advices += 4;\n        }\n\n        /* Expire cycle. */\n        if (!strcasecmp(event,\"expire-cycle\")) {\n            advise_hz = 1;\n            advise_large_objects = 1;\n            advices += 2;\n        }\n\n        /* Eviction cycle. */\n        if (!strcasecmp(event,\"eviction-del\")) {\n            advise_large_objects = 1;\n            advices++;\n        }\n\n        if (!strcasecmp(event,\"eviction-cycle\")) {\n            advise_mass_eviction = 1;\n            advices++;\n        }\n\n        report = sdscatlen(report,\"\\n\",1);\n    }\n    dictReleaseIterator(di);\n\n    /* Add non event based advices. */\n    if (g_pserver->aof_enabled && THPGetAnonHugePagesSize() > 0) {\n        advise_disable_thp = 1;\n        advices++;\n    }\n\n    if (eventnum == 0 && advices == 0) {\n        report = sdscat(report,\"Dave, no latency spike was observed during the lifetime of this KeyDB instance, not in the slightest bit. I honestly think you ought to sit down calmly, take a stress pill, and think things over.\\n\");\n    } else if (eventnum > 0 && advices == 0) {\n        report = sdscat(report,\"\\nWhile there are latency events logged, I'm not able to suggest any easy fix. Please use the KeyDB community to get some help, providing this report in your help request.\\n\");\n    } else {\n        /* Add all the suggestions accumulated so far. */\n\n        /* Better VM. */\n        report = sdscat(report,\"\\nI have a few advices for you:\\n\\n\");\n        if (advise_better_vm) {\n            report = sdscat(report,\"- If you are using a virtual machine, consider upgrading it with a faster one using a hypervisior that provides less latency during fork() calls. Xen is known to have poor fork() performance. Even in the context of the same VM provider, certain kinds of instances can execute fork faster than others.\\n\");\n        }\n\n        /* Slow log. */\n        if (advise_slowlog_enabled) {\n            report = sdscatprintf(report,\"- There are latency issues with potentially slow commands you are using. Try to enable the Slow Log KeyDB feature using the command 'CONFIG SET slowlog-log-slower-than %llu'. If the Slow log is disabled KeyDB is not able to log slow commands execution for you.\\n\", (unsigned long long)g_pserver->latency_monitor_threshold*1000);\n        }\n\n        if (advise_slowlog_tuning) {\n            report = sdscatprintf(report,\"- Your current Slow Log configuration only logs events that are slower than your configured latency monitor threshold. Please use 'CONFIG SET slowlog-log-slower-than %llu'.\\n\", (unsigned long long)g_pserver->latency_monitor_threshold*1000);\n        }\n\n        if (advise_slowlog_inspect) {\n            report = sdscat(report,\"- Check your Slow Log to understand what are the commands you are running which are too slow to execute. Please check https://docs.keydb.dev/docs/commands#slowlog for more information.\\n\");\n        }\n\n        /* Intrinsic latency. */\n        if (advise_scheduler) {\n            report = sdscat(report,\"- The system is slow to execute KeyDB code paths not containing system calls. This usually means the system does not provide KeyDB CPU time to run for long periods. You should try to:\\n\"\n            \"  1) Lower the system load.\\n\"\n            \"  2) Use a computer / VM just for KeyDB if you are running other software in the same system.\\n\"\n            \"  3) Check if you have a \\\"noisy neighbour\\\" problem.\\n\"\n            \"  4) Check with 'keydb-cli --intrinsic-latency 100' what is the intrinsic latency in your system.\\n\"\n            \"  5) Check if the problem is allocator-related by recompiling KeyDB with MALLOC=libc, if you are using Jemalloc. However this may create fragmentation problems.\\n\");\n        }\n\n        /* AOF / Disk latency. */\n        if (advise_local_disk) {\n            report = sdscat(report,\"- It is strongly advised to use local disks for persistence, especially if you are using AOF. Remote disks provided by platform-as-a-service providers are known to be slow.\\n\");\n        }\n\n        if (advise_ssd) {\n            report = sdscat(report,\"- SSD disks are able to reduce fsync latency, and total time needed for snapshotting and AOF log rewriting (resulting in smaller memory usage and smaller final AOF rewrite buffer flushes). With extremely high write load SSD disks can be a good option. However KeyDB should perform reasonably with high load using normal disks. Use this advice as a last resort.\\n\");\n        }\n\n        if (advise_data_writeback) {\n            report = sdscat(report,\"- Mounting ext3/4 filesystems with data=writeback can provide a performance boost compared to data=ordered, however this mode of operation provides less guarantees, and sometimes it can happen that after a hard crash the AOF file will have a half-written command at the end and will require to be repaired before KeyDB restarts.\\n\");\n        }\n\n        if (advise_disk_contention) {\n            report = sdscat(report,\"- Try to lower the disk contention. This is often caused by other disk intensive processes running in the same computer (including other KeyDB instances).\\n\");\n        }\n\n        if (advise_no_appendfsync) {\n            report = sdscat(report,\"- Assuming from the point of view of data safety this is viable in your environment, you could try to enable the 'no-appendfsync-on-rewrite' option, so that fsync will not be performed while there is a child rewriting the AOF file or producing an RDB file (the moment where there is high disk contention).\\n\");\n        }\n\n        if (advise_relax_fsync_policy && g_pserver->aof_fsync == AOF_FSYNC_ALWAYS) {\n            report = sdscat(report,\"- Your fsync policy is set to 'always'. It is very hard to get good performances with such a setup, if possible try to relax the fsync policy to 'onesec'.\\n\");\n        }\n\n        if (advise_write_load_info) {\n            report = sdscat(report,\"- Latency during the AOF atomic rename operation or when the final difference is flushed to the AOF file at the end of the rewrite, sometimes is caused by very high write load, causing the AOF buffer to get very large. If possible try to send less commands to accomplish the same work, or use Lua scripts to group multiple operations into a single EVALSHA call.\\n\");\n        }\n\n        if (advise_hz && g_pserver->hz < 100) {\n            report = sdscat(report,\"- In order to make the KeyDB keys expiring process more incremental, try to set the 'hz' configuration parameter to 100 using 'CONFIG SET hz 100'.\\n\");\n        }\n\n        if (advise_large_objects) {\n            report = sdscat(report,\"- Deleting, expiring or evicting (because of maxmemory policy) large objects is a blocking operation. If you have very large objects that are often deleted, expired, or evicted, try to fragment those objects into multiple smaller objects.\\n\");\n        }\n\n        if (advise_mass_eviction) {\n            report = sdscat(report,\"- Sudden changes to the 'maxmemory' setting via 'CONFIG SET', or allocation of large objects via sets or sorted sets intersections, STORE option of SORT, KeyDB Cluster large keys migrations (RESTORE command), may create sudden memory pressure forcing the server to block trying to evict keys. \\n\");\n        }\n\n        if (advise_disable_thp) {\n            report = sdscat(report,\"- I detected a non zero amount of anonymous huge pages used by your process. This creates very serious latency events in different conditions, especially when KeyDB is persisting on disk. To disable THP support use the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled', make sure to also add it into /etc/rc.local so that the command will be executed again after a reboot. Note that even if you have already disabled THP, you still need to restart the KeyDB process to get rid of the huge pages already created.\\n\");\n        }\n    }\n\n    return report;\n}\n\n/* ---------------------- Latency command implementation -------------------- */\n\n/* latencyCommand() helper to produce a time-delay reply for all the samples\n * in memory for the specified time series. */\nvoid latencyCommandReplyWithSamples(client *c, struct latencyTimeSeries *ts) {\n    void *replylen = addReplyDeferredLen(c);\n    int samples = 0, j;\n\n    for (j = 0; j < LATENCY_TS_LEN; j++) {\n        int i = (ts->idx + j) % LATENCY_TS_LEN;\n\n        if (ts->samples[i].time == 0) continue;\n        addReplyArrayLen(c,2);\n        addReplyLongLong(c,ts->samples[i].time);\n        addReplyLongLong(c,ts->samples[i].latency);\n        samples++;\n    }\n    setDeferredArrayLen(c,replylen,samples);\n}\n\n/* latencyCommand() helper to produce the reply for the LATEST subcommand,\n * listing the last latency sample for every event type registered so far. */\nvoid latencyCommandReplyWithLatestEvents(client *c) {\n    dictIterator *di;\n    dictEntry *de;\n\n    addReplyArrayLen(c,dictSize(g_pserver->latency_events));\n    di = dictGetIterator(g_pserver->latency_events);\n    while((de = dictNext(di)) != NULL) {\n        char *event = (char*)dictGetKey(de);\n        struct latencyTimeSeries *ts = (latencyTimeSeries*)dictGetVal(de);\n        int last = (ts->idx + LATENCY_TS_LEN - 1) % LATENCY_TS_LEN;\n\n        addReplyArrayLen(c,4);\n        addReplyBulkCString(c,event);\n        addReplyLongLong(c,ts->samples[last].time);\n        addReplyLongLong(c,ts->samples[last].latency);\n        addReplyLongLong(c,ts->max);\n    }\n    dictReleaseIterator(di);\n}\n\n#define LATENCY_GRAPH_COLS 80\nsds latencyCommandGenSparkeline(char *event, struct latencyTimeSeries *ts) {\n    int j;\n    struct sequence *seq = createSparklineSequence();\n    sds graph = sdsempty();\n    uint32_t min = 0, max = 0;\n\n    for (j = 0; j < LATENCY_TS_LEN; j++) {\n        int i = (ts->idx + j) % LATENCY_TS_LEN;\n        int elapsed;\n        char buf[64];\n\n        if (ts->samples[i].time == 0) continue;\n        /* Update min and max. */\n        if (seq->length == 0) {\n            min = max = ts->samples[i].latency;\n        } else {\n            if (ts->samples[i].latency > max) max = ts->samples[i].latency;\n            if (ts->samples[i].latency < min) min = ts->samples[i].latency;\n        }\n        /* Use as label the number of seconds / minutes / hours / days\n         * ago the event happened. */\n        elapsed = time(NULL) - ts->samples[i].time;\n        if (elapsed < 60)\n            snprintf(buf,sizeof(buf),\"%ds\",elapsed);\n        else if (elapsed < 3600)\n            snprintf(buf,sizeof(buf),\"%dm\",elapsed/60);\n        else if (elapsed < 3600*24)\n            snprintf(buf,sizeof(buf),\"%dh\",elapsed/3600);\n        else\n            snprintf(buf,sizeof(buf),\"%dd\",elapsed/(3600*24));\n        sparklineSequenceAddSample(seq,ts->samples[i].latency,buf);\n    }\n\n    graph = sdscatprintf(graph,\n        \"%s - high %lu ms, low %lu ms (all time high %lu ms)\\n\", event,\n        (unsigned long) max, (unsigned long) min, (unsigned long) ts->max);\n    for (j = 0; j < LATENCY_GRAPH_COLS; j++)\n        graph = sdscatlen(graph,\"-\",1);\n    graph = sdscatlen(graph,\"\\n\",1);\n    graph = sparklineRender(graph,seq,LATENCY_GRAPH_COLS,4,SPARKLINE_FILL);\n    freeSparklineSequence(seq);\n    return graph;\n}\n\n/* LATENCY command implementations.\n *\n * LATENCY HISTORY: return time-latency samples for the specified event.\n * LATENCY LATEST: return the latest latency for all the events classes.\n * LATENCY DOCTOR: returns a human readable analysis of instance latency.\n * LATENCY GRAPH: provide an ASCII graph of the latency of the specified event.\n * LATENCY RESET: reset data of a specified event or all the data if no event provided.\n */\nvoid latencyCommand(client *c) {\n    struct latencyTimeSeries *ts;\n\n    if (!strcasecmp(szFromObj(c->argv[1]),\"history\") && c->argc == 3) {\n        /* LATENCY HISTORY <event> */\n        ts = (latencyTimeSeries*)dictFetchValue(g_pserver->latency_events,ptrFromObj(c->argv[2]));\n        if (ts == NULL) {\n            addReplyArrayLen(c,0);\n        } else {\n            latencyCommandReplyWithSamples(c,ts);\n        }\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"graph\") && c->argc == 3) {\n        /* LATENCY GRAPH <event> */\n        sds graph;\n        dictEntry *de;\n        char *event;\n\n        de = dictFind(g_pserver->latency_events,ptrFromObj(c->argv[2]));\n        if (de == NULL) goto nodataerr;\n        ts = (latencyTimeSeries*)dictGetVal(de);\n        event = (char*)dictGetKey(de);\n\n        graph = latencyCommandGenSparkeline(event,ts);\n        addReplyVerbatim(c,graph,sdslen(graph),\"txt\");\n        sdsfree(graph);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"latest\") && c->argc == 2) {\n        /* LATENCY LATEST */\n        latencyCommandReplyWithLatestEvents(c);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"doctor\") && c->argc == 2) {\n        /* LATENCY DOCTOR */\n        sds report = createLatencyReport();\n\n        addReplyVerbatim(c,report,sdslen(report),\"txt\");\n        sdsfree(report);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"reset\") && c->argc >= 2) {\n        /* LATENCY RESET */\n        if (c->argc == 2) {\n            addReplyLongLong(c,latencyResetEvent(NULL));\n        } else {\n            int j, resets = 0;\n\n            for (j = 2; j < c->argc; j++)\n                resets += latencyResetEvent(szFromObj(c->argv[j]));\n            addReplyLongLong(c,resets);\n        }\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"help\") && c->argc == 2) {\n        const char *help[] = {\n\"DOCTOR\",\n\"    Return a human readable latency analysis report.\",\n\"GRAPH <event>\",\n\"    Return an ASCII latency graph for the <event> class.\",\n\"HISTORY <event>\",\n\"    Return time-latency samples for the <event> class.\",\n\"LATEST\",\n\"    Return the latest latency samples for all events.\",\n\"RESET [<event> ...]\",\n\"    Reset latency data of one or more <event> classes.\",\n\"    (default: reset all data for all event classes)\",\nNULL\n        };\n        addReplyHelp(c, help);\n    } else {\n        addReplySubcommandSyntaxError(c);\n    }\n    return;\n\nnodataerr:\n    /* Common error when the user asks for an event we have no latency\n     * information about. */\n    addReplyErrorFormat(c,\n        \"No samples available for event '%s'\", (char*) ptrFromObj(c->argv[2]));\n}\n\n"
  },
  {
    "path": "src/latency.h",
    "content": "/* latency.h -- latency monitor API header file\n * See latency.c for more information.\n *\n * ----------------------------------------------------------------------------\n *\n * Copyright (c) 2014, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __LATENCY_H\n#define __LATENCY_H\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#define LATENCY_TS_LEN 160 /* History length for every monitored event. */\n\n/* Representation of a latency sample: the sampling time and the latency\n * observed in milliseconds. */\nstruct latencySample {\n    int32_t time; /* We don't use time_t to force 4 bytes usage everywhere. */\n    uint32_t latency; /* Latency in milliseconds. */\n};\n\n/* The latency time series for a given event. */\nstruct latencyTimeSeries {\n    int idx; /* Index of the next sample to store. */\n    uint32_t max; /* Max latency observed for this event. */\n    struct latencySample samples[LATENCY_TS_LEN]; /* Latest history. */\n};\n\n/* Latency statistics structure. */\nstruct latencyStats {\n    uint32_t all_time_high; /* Absolute max observed since latest reset. */\n    uint32_t avg;           /* Average of current samples. */\n    uint32_t min;           /* Min of current samples. */\n    uint32_t max;           /* Max of current samples. */\n    uint32_t mad;           /* Mean absolute deviation. */\n    uint32_t samples;       /* Number of non-zero samples. */\n    time_t period;          /* Number of seconds since first event and now. */\n};\n\nvoid latencyMonitorInit(void);\nvoid latencyAddSample(const char *event, mstime_t latency);\nint THPIsEnabled(void);\nint THPDisable(void);\n\n/* Latency monitoring macros. */\n\n/* Start monitoring an event. We just set the current time. */\n#define latencyStartMonitor(var) if (g_pserver->latency_monitor_threshold) { \\\n    var = mstime(); \\\n} else { \\\n    var = 0; \\\n}\n\n/* End monitoring an event, compute the difference with the current time\n * to check the amount of time elapsed. */\n#define latencyEndMonitor(var) if (g_pserver->latency_monitor_threshold) { \\\n    var = mstime() - var; \\\n}\n\n/* Add the sample only if the elapsed time is >= to the configured threshold. */\n#define latencyAddSampleIfNeeded(event,var) \\\n    if (g_pserver->latency_monitor_threshold && \\\n        (var) >= g_pserver->latency_monitor_threshold) \\\n          latencyAddSample((event),(var));\n\n/* Remove time from a nested event. */\n#define latencyRemoveNestedEvent(event_var,nested_var) \\\n    event_var += nested_var;\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif /* __LATENCY_H */\n\n"
  },
  {
    "path": "src/lazyfree.cpp",
    "content": "#include \"server.h\"\n#include \"bio.h\"\n#include \"atomicvar.h\"\n#include \"cluster.h\"\n\nstatic redisAtomic size_t lazyfree_objects = 0;\nstatic redisAtomic size_t lazyfreed_objects = 0;\n\n/* Release objects from the lazyfree thread. It's just decrRefCount()\n * updating the count of objects to release. */\nvoid lazyfreeFreeObject(void *args[]) {\n    robj *o = (robj *) args[0];\n    decrRefCount(o);\n    atomicDecr(lazyfree_objects,1);\n    atomicIncr(lazyfreed_objects,1);\n}\n\n/* Release a database from the lazyfree thread. The 'db' pointer is the\n * database which was substituted with a fresh one in the main thread\n * when the database was logically deleted. */\nvoid lazyfreeFreeDatabase(void *args[]) {\n    dict *ht1 = (dict *) args[0];\n\n    size_t numkeys = dictSize(ht1);\n    dictRelease(ht1);\n    atomicDecr(lazyfree_objects,numkeys);\n    atomicIncr(lazyfreed_objects,numkeys);\n}\n\n/* Release the skiplist mapping Redis Cluster keys to slots in the\n * lazyfree thread. */\nvoid lazyfreeFreeSlotsMap(void *args[]) {\n    rax *rt = (rax*)args[0];\n    size_t len = rt->numele;\n    raxFree(rt);\n    atomicDecr(lazyfree_objects,len);\n    atomicIncr(lazyfreed_objects,len);\n}\n\n/* Release the key tracking table. */\nvoid lazyFreeTrackingTable(void *args[]) {\n    rax *rt = (rax*)args[0];\n    size_t len = rt->numele;\n    freeTrackingRadixTree(rt);\n    atomicDecr(lazyfree_objects,len);\n    atomicIncr(lazyfreed_objects,len);\n}\n\nvoid lazyFreeLuaScripts(void *args[]) {\n    dict *lua_scripts = (dict*)args[0];\n    long long len = dictSize(lua_scripts);\n    dictRelease(lua_scripts);\n    atomicDecr(lazyfree_objects,len);\n    atomicIncr(lazyfreed_objects,len);\n}\n\n/* Return the number of currently pending objects to free. */\nsize_t lazyfreeGetPendingObjectsCount(void) {\n    size_t aux;\n    atomicGet(lazyfree_objects,aux);\n    return aux;\n}\n\n/* Return the number of objects that have been freed. */\nsize_t lazyfreeGetFreedObjectsCount(void) {\n    size_t aux;\n    atomicGet(lazyfreed_objects,aux);\n    return aux;\n}\n\n/* Return the amount of work needed in order to free an object.\n * The return value is not always the actual number of allocations the\n * object is composed of, but a number proportional to it.\n *\n * For strings the function always returns 1.\n *\n * For aggregated objects represented by hash tables or other data structures\n * the function just returns the number of elements the object is composed of.\n *\n * Objects composed of single allocations are always reported as having a\n * single item even if they are actually logical composed of multiple\n * elements.\n *\n * For lists the function returns the number of elements in the quicklist\n * representing the list. */\nsize_t lazyfreeGetFreeEffort(robj *key, robj *obj) {\n    if (obj->type == OBJ_LIST) {\n        quicklist *ql = (quicklist*)ptrFromObj(obj);\n        return ql->len;\n    } else if (obj->type == OBJ_SET && obj->encoding == OBJ_ENCODING_HT) {\n        dict *ht = (dict*)ptrFromObj(obj);\n        return dictSize(ht);\n    } else if (obj->type == OBJ_ZSET && obj->encoding == OBJ_ENCODING_SKIPLIST){\n        zset *zs = (zset*)ptrFromObj(obj);\n        return zs->zsl->length;\n    } else if (obj->type == OBJ_HASH && obj->encoding == OBJ_ENCODING_HT) {\n        dict *ht = (dict*)ptrFromObj(obj);\n        return dictSize(ht);\n    } else if (obj->type == OBJ_STREAM) {\n        size_t effort = 0;\n        stream *s = (stream*)ptrFromObj(obj);\n\n        /* Make a best effort estimate to maintain constant runtime. Every macro\n         * node in the Stream is one allocation. */\n        effort += s->rax->numnodes;\n\n        /* Every consumer group is an allocation and so are the entries in its\n         * PEL. We use size of the first group's PEL as an estimate for all\n         * others. */\n        if (s->cgroups && raxSize(s->cgroups)) {\n            raxIterator ri;\n            streamCG *cg;\n            raxStart(&ri,s->cgroups);\n            raxSeek(&ri,\"^\",NULL,0);\n            /* There must be at least one group so the following should always\n             * work. */\n            serverAssert(raxNext(&ri));\n            cg = (streamCG*)ri.data;\n            effort += raxSize(s->cgroups)*(1+raxSize(cg->pel));\n            raxStop(&ri);\n        }\n        return effort;\n    } else if (obj->type == OBJ_MODULE) {\n        moduleValue *mv = (moduleValue*)ptrFromObj(obj);\n        moduleType *mt = mv->type;\n        if (mt->free_effort != NULL) {\n            size_t effort  = mt->free_effort(key,mv->value);\n            /* If the module's free_effort returns 0, it will use asynchronous free\n             memory by default */\n            return effort == 0 ? ULONG_MAX : effort;\n        } else {\n            return 1;\n        }\n    } else {\n        return 1; /* Everything else is a single allocation. */\n    }\n}\n\n/* Delete a key, value, and associated expiration entry if any, from the DB.\n * If there are enough allocations to free the value object may be put into\n * a lazy free list instead of being freed synchronously. The lazy free list\n * will be reclaimed in a different bio.c thread. */\n#define LAZYFREE_THRESHOLD 64\nbool redisDbPersistentData::asyncDelete(robj *key) {\n    /* If the value is composed of a few allocations, to free in a lazy way\n     * is actually just slower... So under a certain limit we just free\n     * the object synchronously. */\n\n    if (m_spstorage != nullptr)\n        return syncDelete(key); // async delte never makes sense with a storage provider\n\n    dictEntry *de = dictUnlink(m_pdict,ptrFromObj(key));\n    if (de) {\n        if (m_pdbSnapshot != nullptr && m_pdbSnapshot->find_cached_threadsafe(szFromObj(key)) != nullptr)\n        {\n            uint64_t hash = dictGetHash(m_pdict, szFromObj(key));\n            dictAdd(m_pdictTombstone, sdsdup((sds)dictGetKey(de)), (void*)hash);\n        }\n\n        robj *val = (robj*)dictGetVal(de);\n        if (val->FExpires())\n        {\n            /* Deleting an entry from the expires dict will not free the sds of\n             * the key, because it is shared with the main dictionary. */\n            removeExpire(key,dict_iter(m_pdict, de));\n        }\n\n        /* Tells the module that the key has been unlinked from the database. */\n        moduleNotifyKeyUnlink(key,val);\n\n        size_t free_effort = lazyfreeGetFreeEffort(key,val);\n\n        /* If releasing the object is too much work, do it in the background\n         * by adding the object to the lazy free list.\n         * Note that if the object is shared, to reclaim it now it is not\n         * possible. This rarely happens, however sometimes the implementation\n         * of parts of the Redis core may call incrRefCount() to protect\n         * objects, and then call dbDelete(). In this case we'll fall\n         * through and reach the dictFreeUnlinkedEntry() call, that will be\n         * equivalent to just calling decrRefCount(). */\n        if (free_effort > LAZYFREE_THRESHOLD && val->getrefcount(std::memory_order_relaxed) == 1) {\n            atomicIncr(lazyfree_objects,1);\n            bioCreateLazyFreeJob(lazyfreeFreeObject,1, val);\n            dictSetVal(m_pdict,de,NULL);\n        }\n    }\n\n    /* Release the key-val pair, or just the key if we set the val\n     * field to NULL in order to lazy free it later. */\n    if (de) {\n        dictFreeUnlinkedEntry(m_pdict,de);\n        if (g_pserver->cluster_enabled) slotToKeyDel(szFromObj(key));\n        return true;\n    } else {\n        return false;\n    }\n}\n\nint dbAsyncDelete(redisDb *db, robj *key) {\n    return db->asyncDelete(key);\n}\n\n/* Free an object, if the object is huge enough, free it in async way. */\nvoid freeObjAsync(robj *key, robj *obj) {\n    size_t free_effort = lazyfreeGetFreeEffort(key,obj);\n    if (free_effort > LAZYFREE_THRESHOLD && obj->getrefcount(std::memory_order_relaxed) == 1) {\n        atomicIncr(lazyfree_objects,1);\n        bioCreateLazyFreeJob(lazyfreeFreeObject,1,obj);\n    } else {\n        decrRefCount(obj);\n    }\n}\n\n/* Empty a Redis DB asynchronously. What the function does actually is to\n * create a new empty set of hash tables and scheduling the old ones for\n * lazy freeing. */\nvoid redisDbPersistentData::emptyDbAsync() {\n    dict *oldht1 = m_pdict;\n    m_pdict = dictCreate(&dbDictType,this);\n    if (m_spstorage != nullptr)\n        m_spstorage->clearAsync();\n    if (m_fTrackingChanges)\n        m_fAllChanged = true;\n    atomicIncr(lazyfree_objects,dictSize(oldht1));\n    m_numexpires = 0;\n    bioCreateLazyFreeJob(lazyfreeFreeDatabase,2,oldht1,nullptr);\n}\n\n/* Release the radix tree mapping Redis Cluster keys to slots asynchronously. */\nvoid freeSlotsToKeysMapAsync(rax *rt) {\n    atomicIncr(lazyfree_objects,rt->numele);\n    bioCreateLazyFreeJob(lazyfreeFreeSlotsMap,1,rt);\n}\n\n/* Free an object, if the object is huge enough, free it in async way. */\nvoid freeTrackingRadixTreeAsync(rax *tracking) {\n    atomicIncr(lazyfree_objects,tracking->numele);\n    bioCreateLazyFreeJob(lazyFreeTrackingTable,1,tracking);\n}\n\n/* Free lua_scripts dict, if the dict is huge enough, free it in async way. */\nvoid freeLuaScriptsAsync(dict *lua_scripts) {\n    if (dictSize(lua_scripts) > LAZYFREE_THRESHOLD) {\n        atomicIncr(lazyfree_objects,dictSize(lua_scripts));\n        bioCreateLazyFreeJob(lazyFreeLuaScripts,1,lua_scripts);\n    } else {\n        dictRelease(lua_scripts);\n    }\n}\n"
  },
  {
    "path": "src/listpack.c",
    "content": "/* Listpack -- A lists of strings serialization format\n *\n * This file implements the specification you can find at:\n *\n *  https://github.com/antirez/listpack\n *\n * Copyright (c) 2017, Salvatore Sanfilippo <antirez at gmail dot com>\n * Copyright (c) 2020, Redis Labs, Inc\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <stdint.h>\n#include <limits.h>\n#include <sys/types.h>\n#include <stdlib.h>\n#include <string.h>\n#include <stdio.h>\n\n#include \"listpack.h\"\n#include \"listpack_malloc.h\"\n#include \"redisassert.h\"\n\n#define LP_HDR_SIZE 6       /* 32 bit total len + 16 bit number of elements. */\n#define LP_HDR_NUMELE_UNKNOWN UINT16_MAX\n#define LP_MAX_INT_ENCODING_LEN 9\n#define LP_MAX_BACKLEN_SIZE 5\n#define LP_MAX_ENTRY_BACKLEN 34359738367ULL\n#define LP_ENCODING_INT 0\n#define LP_ENCODING_STRING 1\n\n#define LP_ENCODING_7BIT_UINT 0\n#define LP_ENCODING_7BIT_UINT_MASK 0x80\n#define LP_ENCODING_IS_7BIT_UINT(byte) (((byte)&LP_ENCODING_7BIT_UINT_MASK)==LP_ENCODING_7BIT_UINT)\n\n#define LP_ENCODING_6BIT_STR 0x80\n#define LP_ENCODING_6BIT_STR_MASK 0xC0\n#define LP_ENCODING_IS_6BIT_STR(byte) (((byte)&LP_ENCODING_6BIT_STR_MASK)==LP_ENCODING_6BIT_STR)\n\n#define LP_ENCODING_13BIT_INT 0xC0\n#define LP_ENCODING_13BIT_INT_MASK 0xE0\n#define LP_ENCODING_IS_13BIT_INT(byte) (((byte)&LP_ENCODING_13BIT_INT_MASK)==LP_ENCODING_13BIT_INT)\n\n#define LP_ENCODING_12BIT_STR 0xE0\n#define LP_ENCODING_12BIT_STR_MASK 0xF0\n#define LP_ENCODING_IS_12BIT_STR(byte) (((byte)&LP_ENCODING_12BIT_STR_MASK)==LP_ENCODING_12BIT_STR)\n\n#define LP_ENCODING_16BIT_INT 0xF1\n#define LP_ENCODING_16BIT_INT_MASK 0xFF\n#define LP_ENCODING_IS_16BIT_INT(byte) (((byte)&LP_ENCODING_16BIT_INT_MASK)==LP_ENCODING_16BIT_INT)\n\n#define LP_ENCODING_24BIT_INT 0xF2\n#define LP_ENCODING_24BIT_INT_MASK 0xFF\n#define LP_ENCODING_IS_24BIT_INT(byte) (((byte)&LP_ENCODING_24BIT_INT_MASK)==LP_ENCODING_24BIT_INT)\n\n#define LP_ENCODING_32BIT_INT 0xF3\n#define LP_ENCODING_32BIT_INT_MASK 0xFF\n#define LP_ENCODING_IS_32BIT_INT(byte) (((byte)&LP_ENCODING_32BIT_INT_MASK)==LP_ENCODING_32BIT_INT)\n\n#define LP_ENCODING_64BIT_INT 0xF4\n#define LP_ENCODING_64BIT_INT_MASK 0xFF\n#define LP_ENCODING_IS_64BIT_INT(byte) (((byte)&LP_ENCODING_64BIT_INT_MASK)==LP_ENCODING_64BIT_INT)\n\n#define LP_ENCODING_32BIT_STR 0xF0\n#define LP_ENCODING_32BIT_STR_MASK 0xFF\n#define LP_ENCODING_IS_32BIT_STR(byte) (((byte)&LP_ENCODING_32BIT_STR_MASK)==LP_ENCODING_32BIT_STR)\n\n#define LP_EOF 0xFF\n\n#define LP_ENCODING_6BIT_STR_LEN(p) ((p)[0] & 0x3F)\n#define LP_ENCODING_12BIT_STR_LEN(p) ((((p)[0] & 0xF) << 8) | (p)[1])\n#define LP_ENCODING_32BIT_STR_LEN(p) (((uint32_t)(p)[1]<<0) | \\\n                                      ((uint32_t)(p)[2]<<8) | \\\n                                      ((uint32_t)(p)[3]<<16) | \\\n                                      ((uint32_t)(p)[4]<<24))\n\n#define lpGetTotalBytes(p)           (((uint32_t)(p)[0]<<0) | \\\n                                      ((uint32_t)(p)[1]<<8) | \\\n                                      ((uint32_t)(p)[2]<<16) | \\\n                                      ((uint32_t)(p)[3]<<24))\n\n#define lpGetNumElements(p)          (((uint32_t)(p)[4]<<0) | \\\n                                      ((uint32_t)(p)[5]<<8))\n#define lpSetTotalBytes(p,v) do { \\\n    (p)[0] = (v)&0xff; \\\n    (p)[1] = ((v)>>8)&0xff; \\\n    (p)[2] = ((v)>>16)&0xff; \\\n    (p)[3] = ((v)>>24)&0xff; \\\n} while(0)\n\n#define lpSetNumElements(p,v) do { \\\n    (p)[4] = (v)&0xff; \\\n    (p)[5] = ((v)>>8)&0xff; \\\n} while(0)\n\n/* Validates that 'p' is not ouside the listpack.\n * All function that return a pointer to an element in the listpack will assert\n * that this element is valid, so it can be freely used.\n * Generally functions such lpNext and lpDelete assume the input pointer is\n * already validated (since it's the return value of another function). */\n#define ASSERT_INTEGRITY(lp, p) do { \\\n    assert((p) >= (lp)+LP_HDR_SIZE && (p) < (lp)+lpGetTotalBytes((lp))); \\\n} while (0)\n\n/* Similar to the above, but validates the entire element lenth rather than just\n * it's pointer. */\n#define ASSERT_INTEGRITY_LEN(lp, p, len) do { \\\n    assert((p) >= (lp)+LP_HDR_SIZE && (p)+(len) < (lp)+lpGetTotalBytes((lp))); \\\n} while (0)\n\nstatic inline void lpAssertValidEntry(unsigned char* lp, size_t lpbytes, unsigned char *p);\n\n/* Convert a string into a signed 64 bit integer.\n * The function returns 1 if the string could be parsed into a (non-overflowing)\n * signed 64 bit int, 0 otherwise. The 'value' will be set to the parsed value\n * when the function returns success.\n *\n * Note that this function demands that the string strictly represents\n * a int64 value: no spaces or other characters before or after the string\n * representing the number are accepted, nor zeroes at the start if not\n * for the string \"0\" representing the zero number.\n *\n * Because of its strictness, it is safe to use this function to check if\n * you can convert a string into a long long, and obtain back the string\n * from the number without any loss in the string representation. *\n *\n * -----------------------------------------------------------------------------\n *\n * Credits: this function was adapted from the Redis source code, file\n * \"utils.c\", function string2ll(), and is copyright:\n *\n * Copyright(C) 2011, Pieter Noordhuis\n * Copyright(C) 2011, Salvatore Sanfilippo\n *\n * The function is released under the BSD 3-clause license.\n */\nint lpStringToInt64(const char *s, unsigned long slen, int64_t *value) {\n    const char *p = s;\n    unsigned long plen = 0;\n    int negative = 0;\n    uint64_t v;\n\n    if (plen == slen)\n        return 0;\n\n    /* Special case: first and only digit is 0. */\n    if (slen == 1 && p[0] == '0') {\n        if (value != NULL) *value = 0;\n        return 1;\n    }\n\n    if (p[0] == '-') {\n        negative = 1;\n        p++; plen++;\n\n        /* Abort on only a negative sign. */\n        if (plen == slen)\n            return 0;\n    }\n\n    /* First digit should be 1-9, otherwise the string should just be 0. */\n    if (p[0] >= '1' && p[0] <= '9') {\n        v = p[0]-'0';\n        p++; plen++;\n    } else if (p[0] == '0' && slen == 1) {\n        *value = 0;\n        return 1;\n    } else {\n        return 0;\n    }\n\n    while (plen < slen && p[0] >= '0' && p[0] <= '9') {\n        if (v > (UINT64_MAX / 10)) /* Overflow. */\n            return 0;\n        v *= 10;\n\n        if (v > (UINT64_MAX - (p[0]-'0'))) /* Overflow. */\n            return 0;\n        v += p[0]-'0';\n\n        p++; plen++;\n    }\n\n    /* Return if not all bytes were used. */\n    if (plen < slen)\n        return 0;\n\n    if (negative) {\n        if (v > ((uint64_t)(-(INT64_MIN+1))+1)) /* Overflow. */\n            return 0;\n        if (value != NULL) *value = -v;\n    } else {\n        if (v > INT64_MAX) /* Overflow. */\n            return 0;\n        if (value != NULL) *value = v;\n    }\n    return 1;\n}\n\n/* Create a new, empty listpack.\n * On success the new listpack is returned, otherwise an error is returned.\n * Pre-allocate at least `capacity` bytes of memory,\n * over-allocated memory can be shrinked by `lpShrinkToFit`.\n * */\nunsigned char *lpNew(size_t capacity) {\n    unsigned char *lp = lp_malloc(capacity > LP_HDR_SIZE+1 ? capacity : LP_HDR_SIZE+1);\n    if (lp == NULL) return NULL;\n    lpSetTotalBytes(lp,LP_HDR_SIZE+1);\n    lpSetNumElements(lp,0);\n    lp[LP_HDR_SIZE] = LP_EOF;\n    return lp;\n}\n\n/* Free the specified listpack. */\nvoid lpFree(unsigned char *lp) {\n    lp_free(lp);\n}\n\n/* Shrink the memory to fit. */\nunsigned char* lpShrinkToFit(unsigned char *lp) {\n    size_t size = lpGetTotalBytes(lp);\n    if (size < lp_malloc_size(lp)) {\n        return lp_realloc(lp, size);\n    } else {\n        return lp;\n    }\n}\n\n/* Given an element 'ele' of size 'size', determine if the element can be\n * represented inside the listpack encoded as integer, and returns\n * LP_ENCODING_INT if so. Otherwise returns LP_ENCODING_STR if no integer\n * encoding is possible.\n *\n * If the LP_ENCODING_INT is returned, the function stores the integer encoded\n * representation of the element in the 'intenc' buffer.\n *\n * Regardless of the returned encoding, 'enclen' is populated by reference to\n * the number of bytes that the string or integer encoded element will require\n * in order to be represented. */\nint lpEncodeGetType(unsigned char *ele, uint32_t size, unsigned char *intenc, uint64_t *enclen) {\n    int64_t v;\n    if (lpStringToInt64((const char*)ele, size, &v)) {\n        if (v >= 0 && v <= 127) {\n            /* Single byte 0-127 integer. */\n            intenc[0] = v;\n            *enclen = 1;\n        } else if (v >= -4096 && v <= 4095) {\n            /* 13 bit integer. */\n            if (v < 0) v = ((int64_t)1<<13)+v;\n            intenc[0] = (v>>8)|LP_ENCODING_13BIT_INT;\n            intenc[1] = v&0xff;\n            *enclen = 2;\n        } else if (v >= -32768 && v <= 32767) {\n            /* 16 bit integer. */\n            if (v < 0) v = ((int64_t)1<<16)+v;\n            intenc[0] = LP_ENCODING_16BIT_INT;\n            intenc[1] = v&0xff;\n            intenc[2] = v>>8;\n            *enclen = 3;\n        } else if (v >= -8388608 && v <= 8388607) {\n            /* 24 bit integer. */\n            if (v < 0) v = ((int64_t)1<<24)+v;\n            intenc[0] = LP_ENCODING_24BIT_INT;\n            intenc[1] = v&0xff;\n            intenc[2] = (v>>8)&0xff;\n            intenc[3] = v>>16;\n            *enclen = 4;\n        } else if (v >= -2147483648 && v <= 2147483647) {\n            /* 32 bit integer. */\n            if (v < 0) v = ((int64_t)1<<32)+v;\n            intenc[0] = LP_ENCODING_32BIT_INT;\n            intenc[1] = v&0xff;\n            intenc[2] = (v>>8)&0xff;\n            intenc[3] = (v>>16)&0xff;\n            intenc[4] = v>>24;\n            *enclen = 5;\n        } else {\n            /* 64 bit integer. */\n            uint64_t uv = v;\n            intenc[0] = LP_ENCODING_64BIT_INT;\n            intenc[1] = uv&0xff;\n            intenc[2] = (uv>>8)&0xff;\n            intenc[3] = (uv>>16)&0xff;\n            intenc[4] = (uv>>24)&0xff;\n            intenc[5] = (uv>>32)&0xff;\n            intenc[6] = (uv>>40)&0xff;\n            intenc[7] = (uv>>48)&0xff;\n            intenc[8] = uv>>56;\n            *enclen = 9;\n        }\n        return LP_ENCODING_INT;\n    } else {\n        if (size < 64) *enclen = 1+size;\n        else if (size < 4096) *enclen = 2+size;\n        else *enclen = 5+(uint64_t)size;\n        return LP_ENCODING_STRING;\n    }\n}\n\n/* Store a reverse-encoded variable length field, representing the length\n * of the previous element of size 'l', in the target buffer 'buf'.\n * The function returns the number of bytes used to encode it, from\n * 1 to 5. If 'buf' is NULL the function just returns the number of bytes\n * needed in order to encode the backlen. */\nunsigned long lpEncodeBacklen(unsigned char *buf, uint64_t l) {\n    if (l <= 127) {\n        if (buf) buf[0] = l;\n        return 1;\n    } else if (l < 16383) {\n        if (buf) {\n            buf[0] = l>>7;\n            buf[1] = (l&127)|128;\n        }\n        return 2;\n    } else if (l < 2097151) {\n        if (buf) {\n            buf[0] = l>>14;\n            buf[1] = ((l>>7)&127)|128;\n            buf[2] = (l&127)|128;\n        }\n        return 3;\n    } else if (l < 268435455) {\n        if (buf) {\n            buf[0] = l>>21;\n            buf[1] = ((l>>14)&127)|128;\n            buf[2] = ((l>>7)&127)|128;\n            buf[3] = (l&127)|128;\n        }\n        return 4;\n    } else {\n        if (buf) {\n            buf[0] = l>>28;\n            buf[1] = ((l>>21)&127)|128;\n            buf[2] = ((l>>14)&127)|128;\n            buf[3] = ((l>>7)&127)|128;\n            buf[4] = (l&127)|128;\n        }\n        return 5;\n    }\n}\n\n/* Decode the backlen and returns it. If the encoding looks invalid (more than\n * 5 bytes are used), UINT64_MAX is returned to report the problem. */\nuint64_t lpDecodeBacklen(unsigned char *p) {\n    uint64_t val = 0;\n    uint64_t shift = 0;\n    do {\n        val |= (uint64_t)(p[0] & 127) << shift;\n        if (!(p[0] & 128)) break;\n        shift += 7;\n        p--;\n        if (shift > 28) return UINT64_MAX;\n    } while(1);\n    return val;\n}\n\n/* Encode the string element pointed by 's' of size 'len' in the target\n * buffer 's'. The function should be called with 'buf' having always enough\n * space for encoding the string. This is done by calling lpEncodeGetType()\n * before calling this function. */\nvoid lpEncodeString(unsigned char *buf, unsigned char *s, uint32_t len) {\n    if (len < 64) {\n        buf[0] = len | LP_ENCODING_6BIT_STR;\n        memcpy(buf+1,s,len);\n    } else if (len < 4096) {\n        buf[0] = (len >> 8) | LP_ENCODING_12BIT_STR;\n        buf[1] = len & 0xff;\n        memcpy(buf+2,s,len);\n    } else {\n        buf[0] = LP_ENCODING_32BIT_STR;\n        buf[1] = len & 0xff;\n        buf[2] = (len >> 8) & 0xff;\n        buf[3] = (len >> 16) & 0xff;\n        buf[4] = (len >> 24) & 0xff;\n        memcpy(buf+5,s,len);\n    }\n}\n\n/* Return the encoded length of the listpack element pointed by 'p'.\n * This includes the encoding byte, length bytes, and the element data itself.\n * If the element encoding is wrong then 0 is returned.\n * Note that this method may access additional bytes (in case of 12 and 32 bit\n * str), so should only be called when we know 'p' was already validated by\n * lpCurrentEncodedSizeBytes or ASSERT_INTEGRITY_LEN (possibly since 'p' is\n * a return value of another function that validated its return. */\nuint32_t lpCurrentEncodedSizeUnsafe(unsigned char *p) {\n    if (LP_ENCODING_IS_7BIT_UINT(p[0])) return 1;\n    if (LP_ENCODING_IS_6BIT_STR(p[0])) return 1+LP_ENCODING_6BIT_STR_LEN(p);\n    if (LP_ENCODING_IS_13BIT_INT(p[0])) return 2;\n    if (LP_ENCODING_IS_16BIT_INT(p[0])) return 3;\n    if (LP_ENCODING_IS_24BIT_INT(p[0])) return 4;\n    if (LP_ENCODING_IS_32BIT_INT(p[0])) return 5;\n    if (LP_ENCODING_IS_64BIT_INT(p[0])) return 9;\n    if (LP_ENCODING_IS_12BIT_STR(p[0])) return 2+LP_ENCODING_12BIT_STR_LEN(p);\n    if (LP_ENCODING_IS_32BIT_STR(p[0])) return 5+LP_ENCODING_32BIT_STR_LEN(p);\n    if (p[0] == LP_EOF) return 1;\n    return 0;\n}\n\n/* Return bytes needed to encode the length of the listpack element pointed by 'p'.\n * This includes just the encodign byte, and the bytes needed to encode the length\n * of the element (excluding the element data itself)\n * If the element encoding is wrong then 0 is returned. */\nuint32_t lpCurrentEncodedSizeBytes(unsigned char *p) {\n    if (LP_ENCODING_IS_7BIT_UINT(p[0])) return 1;\n    if (LP_ENCODING_IS_6BIT_STR(p[0])) return 1;\n    if (LP_ENCODING_IS_13BIT_INT(p[0])) return 1;\n    if (LP_ENCODING_IS_16BIT_INT(p[0])) return 1;\n    if (LP_ENCODING_IS_24BIT_INT(p[0])) return 1;\n    if (LP_ENCODING_IS_32BIT_INT(p[0])) return 1;\n    if (LP_ENCODING_IS_64BIT_INT(p[0])) return 1;\n    if (LP_ENCODING_IS_12BIT_STR(p[0])) return 2;\n    if (LP_ENCODING_IS_32BIT_STR(p[0])) return 5;\n    if (p[0] == LP_EOF) return 1;\n    return 0;\n}\n\n/* Skip the current entry returning the next. It is invalid to call this\n * function if the current element is the EOF element at the end of the\n * listpack, however, while this function is used to implement lpNext(),\n * it does not return NULL when the EOF element is encountered. */\nunsigned char *lpSkip(unsigned char *p) {\n    unsigned long entrylen = lpCurrentEncodedSizeUnsafe(p);\n    entrylen += lpEncodeBacklen(NULL,entrylen);\n    p += entrylen;\n    return p;\n}\n\n/* If 'p' points to an element of the listpack, calling lpNext() will return\n * the pointer to the next element (the one on the right), or NULL if 'p'\n * already pointed to the last element of the listpack. */\nunsigned char *lpNext(unsigned char *lp, unsigned char *p) {\n    assert(p);\n    p = lpSkip(p);\n    if (p[0] == LP_EOF) return NULL;\n    lpAssertValidEntry(lp, lpBytes(lp), p);\n    return p;\n}\n\n/* If 'p' points to an element of the listpack, calling lpPrev() will return\n * the pointer to the previous element (the one on the left), or NULL if 'p'\n * already pointed to the first element of the listpack. */\nunsigned char *lpPrev(unsigned char *lp, unsigned char *p) {\n    assert(p);\n    if (p-lp == LP_HDR_SIZE) return NULL;\n    p--; /* Seek the first backlen byte of the last element. */\n    uint64_t prevlen = lpDecodeBacklen(p);\n    prevlen += lpEncodeBacklen(NULL,prevlen);\n    p -= prevlen-1; /* Seek the first byte of the previous entry. */\n    lpAssertValidEntry(lp, lpBytes(lp), p);\n    return p;\n}\n\n/* Return a pointer to the first element of the listpack, or NULL if the\n * listpack has no elements. */\nunsigned char *lpFirst(unsigned char *lp) {\n    unsigned char *p = lp + LP_HDR_SIZE; /* Skip the header. */\n    if (p[0] == LP_EOF) return NULL;\n    lpAssertValidEntry(lp, lpBytes(lp), p);\n    return p;\n}\n\n/* Return a pointer to the last element of the listpack, or NULL if the\n * listpack has no elements. */\nunsigned char *lpLast(unsigned char *lp) {\n    unsigned char *p = lp+lpGetTotalBytes(lp)-1; /* Seek EOF element. */\n    return lpPrev(lp,p); /* Will return NULL if EOF is the only element. */\n}\n\n/* Return the number of elements inside the listpack. This function attempts\n * to use the cached value when within range, otherwise a full scan is\n * needed. As a side effect of calling this function, the listpack header\n * could be modified, because if the count is found to be already within\n * the 'numele' header field range, the new value is set. */\nuint32_t lpLength(unsigned char *lp) {\n    uint32_t numele = lpGetNumElements(lp);\n    if (numele != LP_HDR_NUMELE_UNKNOWN) return numele;\n\n    /* Too many elements inside the listpack. We need to scan in order\n     * to get the total number. */\n    uint32_t count = 0;\n    unsigned char *p = lpFirst(lp);\n    while(p) {\n        count++;\n        p = lpNext(lp,p);\n    }\n\n    /* If the count is again within range of the header numele field,\n     * set it. */\n    if (count < LP_HDR_NUMELE_UNKNOWN) lpSetNumElements(lp,count);\n    return count;\n}\n\n/* Return the listpack element pointed by 'p'.\n *\n * The function changes behavior depending on the passed 'intbuf' value.\n * Specifically, if 'intbuf' is NULL:\n *\n * If the element is internally encoded as an integer, the function returns\n * NULL and populates the integer value by reference in 'count'. Otherwise if\n * the element is encoded as a string a pointer to the string (pointing inside\n * the listpack itself) is returned, and 'count' is set to the length of the\n * string.\n *\n * If instead 'intbuf' points to a buffer passed by the caller, that must be\n * at least LP_INTBUF_SIZE bytes, the function always returns the element as\n * it was a string (returning the pointer to the string and setting the\n * 'count' argument to the string length by reference). However if the element\n * is encoded as an integer, the 'intbuf' buffer is used in order to store\n * the string representation.\n *\n * The user should use one or the other form depending on what the value will\n * be used for. If there is immediate usage for an integer value returned\n * by the function, than to pass a buffer (and convert it back to a number)\n * is of course useless.\n *\n * If the function is called against a badly encoded ziplist, so that there\n * is no valid way to parse it, the function returns like if there was an\n * integer encoded with value 12345678900000000 + <unrecognized byte>, this may\n * be an hint to understand that something is wrong. To crash in this case is\n * not sensible because of the different requirements of the application using\n * this lib.\n *\n * Similarly, there is no error returned since the listpack normally can be\n * assumed to be valid, so that would be a very high API cost. However a function\n * in order to check the integrity of the listpack at load time is provided,\n * check lpIsValid(). */\nunsigned char *lpGet(unsigned char *p, int64_t *count, unsigned char *intbuf) {\n    int64_t val;\n    uint64_t uval, negstart, negmax;\n\n    assert(p); /* assertion for valgrind (avoid NPD) */\n    if (LP_ENCODING_IS_7BIT_UINT(p[0])) {\n        negstart = UINT64_MAX; /* 7 bit ints are always positive. */\n        negmax = 0;\n        uval = p[0] & 0x7f;\n    } else if (LP_ENCODING_IS_6BIT_STR(p[0])) {\n        *count = LP_ENCODING_6BIT_STR_LEN(p);\n        return p+1;\n    } else if (LP_ENCODING_IS_13BIT_INT(p[0])) {\n        uval = ((p[0]&0x1f)<<8) | p[1];\n        negstart = (uint64_t)1<<12;\n        negmax = 8191;\n    } else if (LP_ENCODING_IS_16BIT_INT(p[0])) {\n        uval = (uint64_t)p[1] |\n               (uint64_t)p[2]<<8;\n        negstart = (uint64_t)1<<15;\n        negmax = UINT16_MAX;\n    } else if (LP_ENCODING_IS_24BIT_INT(p[0])) {\n        uval = (uint64_t)p[1] |\n               (uint64_t)p[2]<<8 |\n               (uint64_t)p[3]<<16;\n        negstart = (uint64_t)1<<23;\n        negmax = UINT32_MAX>>8;\n    } else if (LP_ENCODING_IS_32BIT_INT(p[0])) {\n        uval = (uint64_t)p[1] |\n               (uint64_t)p[2]<<8 |\n               (uint64_t)p[3]<<16 |\n               (uint64_t)p[4]<<24;\n        negstart = (uint64_t)1<<31;\n        negmax = UINT32_MAX;\n    } else if (LP_ENCODING_IS_64BIT_INT(p[0])) {\n        uval = (uint64_t)p[1] |\n               (uint64_t)p[2]<<8 |\n               (uint64_t)p[3]<<16 |\n               (uint64_t)p[4]<<24 |\n               (uint64_t)p[5]<<32 |\n               (uint64_t)p[6]<<40 |\n               (uint64_t)p[7]<<48 |\n               (uint64_t)p[8]<<56;\n        negstart = (uint64_t)1<<63;\n        negmax = UINT64_MAX;\n    } else if (LP_ENCODING_IS_12BIT_STR(p[0])) {\n        *count = LP_ENCODING_12BIT_STR_LEN(p);\n        return p+2;\n    } else if (LP_ENCODING_IS_32BIT_STR(p[0])) {\n        *count = LP_ENCODING_32BIT_STR_LEN(p);\n        return p+5;\n    } else {\n        uval = 12345678900000000ULL + p[0];\n        negstart = UINT64_MAX;\n        negmax = 0;\n    }\n\n    /* We reach this code path only for integer encodings.\n     * Convert the unsigned value to the signed one using two's complement\n     * rule. */\n    if (uval >= negstart) {\n        /* This three steps conversion should avoid undefined behaviors\n         * in the unsigned -> signed conversion. */\n        uval = negmax-uval;\n        val = uval;\n        val = -val-1;\n    } else {\n        val = uval;\n    }\n\n    /* Return the string representation of the integer or the value itself\n     * depending on intbuf being NULL or not. */\n    if (intbuf) {\n        *count = snprintf((char*)intbuf,LP_INTBUF_SIZE,\"%lld\",(long long)val);\n        return intbuf;\n    } else {\n        *count = val;\n        return NULL;\n    }\n}\n\n/* Insert, delete or replace the specified element 'ele' of length 'len' at\n * the specified position 'p', with 'p' being a listpack element pointer\n * obtained with lpFirst(), lpLast(), lpNext(), lpPrev() or lpSeek().\n *\n * The element is inserted before, after, or replaces the element pointed\n * by 'p' depending on the 'where' argument, that can be LP_BEFORE, LP_AFTER\n * or LP_REPLACE.\n *\n * If 'ele' is set to NULL, the function removes the element pointed by 'p'\n * instead of inserting one.\n *\n * Returns NULL on out of memory or when the listpack total length would exceed\n * the max allowed size of 2^32-1, otherwise the new pointer to the listpack\n * holding the new element is returned (and the old pointer passed is no longer\n * considered valid)\n *\n * If 'newp' is not NULL, at the end of a successful call '*newp' will be set\n * to the address of the element just added, so that it will be possible to\n * continue an interation with lpNext() and lpPrev().\n *\n * For deletion operations ('ele' set to NULL) 'newp' is set to the next\n * element, on the right of the deleted one, or to NULL if the deleted element\n * was the last one. */\nunsigned char *lpInsert(unsigned char *lp, unsigned char *ele, uint32_t size, unsigned char *p, int where, unsigned char **newp) {\n    unsigned char intenc[LP_MAX_INT_ENCODING_LEN];\n    unsigned char backlen[LP_MAX_BACKLEN_SIZE];\n\n    uint64_t enclen; /* The length of the encoded element. */\n\n    /* An element pointer set to NULL means deletion, which is conceptually\n     * replacing the element with a zero-length element. So whatever we\n     * get passed as 'where', set it to LP_REPLACE. */\n    if (ele == NULL) where = LP_REPLACE;\n\n    /* If we need to insert after the current element, we just jump to the\n     * next element (that could be the EOF one) and handle the case of\n     * inserting before. So the function will actually deal with just two\n     * cases: LP_BEFORE and LP_REPLACE. */\n    if (where == LP_AFTER) {\n        p = lpSkip(p);\n        where = LP_BEFORE;\n        ASSERT_INTEGRITY(lp, p);\n    }\n\n    /* Store the offset of the element 'p', so that we can obtain its\n     * address again after a reallocation. */\n    unsigned long poff = p-lp;\n\n    /* Calling lpEncodeGetType() results into the encoded version of the\n     * element to be stored into 'intenc' in case it is representable as\n     * an integer: in that case, the function returns LP_ENCODING_INT.\n     * Otherwise if LP_ENCODING_STR is returned, we'll have to call\n     * lpEncodeString() to actually write the encoded string on place later.\n     *\n     * Whatever the returned encoding is, 'enclen' is populated with the\n     * length of the encoded element. */\n    int enctype;\n    if (ele) {\n        enctype = lpEncodeGetType(ele,size,intenc,&enclen);\n    } else {\n        enctype = -1;\n        enclen = 0;\n    }\n\n    /* We need to also encode the backward-parsable length of the element\n     * and append it to the end: this allows to traverse the listpack from\n     * the end to the start. */\n    unsigned long backlen_size = ele ? lpEncodeBacklen(backlen,enclen) : 0;\n    uint64_t old_listpack_bytes = lpGetTotalBytes(lp);\n    uint32_t replaced_len  = 0;\n    if (where == LP_REPLACE) {\n        replaced_len = lpCurrentEncodedSizeUnsafe(p);\n        replaced_len += lpEncodeBacklen(NULL,replaced_len);\n        ASSERT_INTEGRITY_LEN(lp, p, replaced_len);\n    }\n\n    uint64_t new_listpack_bytes = old_listpack_bytes + enclen + backlen_size\n                                  - replaced_len;\n    if (new_listpack_bytes > UINT32_MAX) return NULL;\n\n    /* We now need to reallocate in order to make space or shrink the\n     * allocation (in case 'when' value is LP_REPLACE and the new element is\n     * smaller). However we do that before memmoving the memory to\n     * make room for the new element if the final allocation will get\n     * larger, or we do it after if the final allocation will get smaller. */\n\n    unsigned char *dst = lp + poff; /* May be updated after reallocation. */\n\n    /* Realloc before: we need more room. */\n    if (new_listpack_bytes > old_listpack_bytes &&\n        new_listpack_bytes > lp_malloc_size(lp)) {\n        if ((lp = lp_realloc(lp,new_listpack_bytes)) == NULL) return NULL;\n        dst = lp + poff;\n    }\n\n    /* Setup the listpack relocating the elements to make the exact room\n     * we need to store the new one. */\n    if (where == LP_BEFORE) {\n        memmove(dst+enclen+backlen_size,dst,old_listpack_bytes-poff);\n    } else { /* LP_REPLACE. */\n        long lendiff = (enclen+backlen_size)-replaced_len;\n        memmove(dst+replaced_len+lendiff,\n                dst+replaced_len,\n                old_listpack_bytes-poff-replaced_len);\n    }\n\n    /* Realloc after: we need to free space. */\n    if (new_listpack_bytes < old_listpack_bytes) {\n        if ((lp = lp_realloc(lp,new_listpack_bytes)) == NULL) return NULL;\n        dst = lp + poff;\n    }\n\n    /* Store the entry. */\n    if (newp) {\n        *newp = dst;\n        /* In case of deletion, set 'newp' to NULL if the next element is\n         * the EOF element. */\n        if (!ele && dst[0] == LP_EOF) *newp = NULL;\n    }\n    if (ele) {\n        if (enctype == LP_ENCODING_INT) {\n            memcpy(dst,intenc,enclen);\n        } else {\n            lpEncodeString(dst,ele,size);\n        }\n        dst += enclen;\n        memcpy(dst,backlen,backlen_size);\n        dst += backlen_size;\n    }\n\n    /* Update header. */\n    if (where != LP_REPLACE || ele == NULL) {\n        uint32_t num_elements = lpGetNumElements(lp);\n        if (num_elements != LP_HDR_NUMELE_UNKNOWN) {\n            if (ele)\n                lpSetNumElements(lp,num_elements+1);\n            else\n                lpSetNumElements(lp,num_elements-1);\n        }\n    }\n    lpSetTotalBytes(lp,new_listpack_bytes);\n\n#if 0\n    /* This code path is normally disabled: what it does is to force listpack\n     * to return *always* a new pointer after performing some modification to\n     * the listpack, even if the previous allocation was enough. This is useful\n     * in order to spot bugs in code using listpacks: by doing so we can find\n     * if the caller forgets to set the new pointer where the listpack reference\n     * is stored, after an update. */\n    unsigned char *oldlp = lp;\n    lp = lp_malloc(new_listpack_bytes);\n    memcpy(lp,oldlp,new_listpack_bytes);\n    if (newp) {\n        unsigned long offset = (*newp)-oldlp;\n        *newp = lp + offset;\n    }\n    /* Make sure the old allocation contains garbage. */\n    memset(oldlp,'A',new_listpack_bytes);\n    lp_free(oldlp);\n#endif\n\n    return lp;\n}\n\n/* Append the specified element 'ele' of length 'len' at the end of the\n * listpack. It is implemented in terms of lpInsert(), so the return value is\n * the same as lpInsert(). */\nunsigned char *lpAppend(unsigned char *lp, unsigned char *ele, uint32_t size) {\n    uint64_t listpack_bytes = lpGetTotalBytes(lp);\n    unsigned char *eofptr = lp + listpack_bytes - 1;\n    return lpInsert(lp,ele,size,eofptr,LP_BEFORE,NULL);\n}\n\n/* Remove the element pointed by 'p', and return the resulting listpack.\n * If 'newp' is not NULL, the next element pointer (to the right of the\n * deleted one) is returned by reference. If the deleted element was the\n * last one, '*newp' is set to NULL. */\nunsigned char *lpDelete(unsigned char *lp, unsigned char *p, unsigned char **newp) {\n    return lpInsert(lp,NULL,0,p,LP_REPLACE,newp);\n}\n\n/* Return the total number of bytes the listpack is composed of. */\nuint32_t lpBytes(unsigned char *lp) {\n    return lpGetTotalBytes(lp);\n}\n\n/* Seek the specified element and returns the pointer to the seeked element.\n * Positive indexes specify the zero-based element to seek from the head to\n * the tail, negative indexes specify elements starting from the tail, where\n * -1 means the last element, -2 the penultimate and so forth. If the index\n * is out of range, NULL is returned. */\nunsigned char *lpSeek(unsigned char *lp, long index) {\n    int forward = 1; /* Seek forward by default. */\n\n    /* We want to seek from left to right or the other way around\n     * depending on the listpack length and the element position.\n     * However if the listpack length cannot be obtained in constant time,\n     * we always seek from left to right. */\n    uint32_t numele = lpGetNumElements(lp);\n    if (numele != LP_HDR_NUMELE_UNKNOWN) {\n        if (index < 0) index = (long)numele+index;\n        if (index < 0) return NULL; /* Index still < 0 means out of range. */\n        if (index >= (long)numele) return NULL; /* Out of range the other side. */\n        /* We want to scan right-to-left if the element we are looking for\n         * is past the half of the listpack. */\n        if (index > (long)numele/2) {\n            forward = 0;\n            /* Right to left scanning always expects a negative index. Convert\n             * our index to negative form. */\n            index -= numele;\n        }\n    } else {\n        /* If the listpack length is unspecified, for negative indexes we\n         * want to always scan right-to-left. */\n        if (index < 0) forward = 0;\n    }\n\n    /* Forward and backward scanning is trivially based on lpNext()/lpPrev(). */\n    if (forward) {\n        unsigned char *ele = lpFirst(lp);\n        while (index > 0 && ele) {\n            ele = lpNext(lp,ele);\n            index--;\n        }\n        return ele;\n    } else {\n        unsigned char *ele = lpLast(lp);\n        while (index < -1 && ele) {\n            ele = lpPrev(lp,ele);\n            index++;\n        }\n        return ele;\n    }\n}\n\n/* Same as lpFirst but without validation assert, to be used right before lpValidateNext. */\nunsigned char *lpValidateFirst(unsigned char *lp) {\n    unsigned char *p = lp + LP_HDR_SIZE; /* Skip the header. */\n    if (p[0] == LP_EOF) return NULL;\n    return p;\n}\n\n/* Validate the integrity of a single listpack entry and move to the next one.\n * The input argument 'pp' is a reference to the current record and is advanced on exit.\n * Returns 1 if valid, 0 if invalid. */\nint lpValidateNext(unsigned char *lp, unsigned char **pp, size_t lpbytes) {\n#define OUT_OF_RANGE(p) ( \\\n        (p) < lp + LP_HDR_SIZE || \\\n        (p) > lp + lpbytes - 1)\n    unsigned char *p = *pp;\n    if (!p)\n        return 0;\n\n    /* Before accessing p, make sure it's valid. */\n    if (OUT_OF_RANGE(p))\n        return 0;\n\n    if (*p == LP_EOF) {\n        *pp = NULL;\n        return 1;\n    }\n\n    /* check that we can read the encoded size */\n    uint32_t lenbytes = lpCurrentEncodedSizeBytes(p);\n    if (!lenbytes)\n        return 0;\n\n    /* make sure the encoded entry length doesn't rech outside the edge of the listpack */\n    if (OUT_OF_RANGE(p + lenbytes))\n        return 0;\n\n    /* get the entry length and encoded backlen. */\n    unsigned long entrylen = lpCurrentEncodedSizeUnsafe(p);\n    unsigned long encodedBacklen = lpEncodeBacklen(NULL,entrylen);\n    entrylen += encodedBacklen;\n\n    /* make sure the entry doesn't rech outside the edge of the listpack */\n    if (OUT_OF_RANGE(p + entrylen))\n        return 0;\n\n    /* move to the next entry */\n    p += entrylen;\n\n    /* make sure the encoded length at the end patches the one at the beginning. */\n    uint64_t prevlen = lpDecodeBacklen(p-1);\n    if (prevlen + encodedBacklen != entrylen)\n        return 0;\n\n    *pp = p;\n    return 1;\n#undef OUT_OF_RANGE\n}\n\n/* Validate that the entry doesn't reach outside the listpack allocation. */\nstatic inline void lpAssertValidEntry(unsigned char* lp, size_t lpbytes, unsigned char *p) {\n    assert(lpValidateNext(lp, &p, lpbytes));\n}\n\n/* Validate the integrity of the data structure.\n * when `deep` is 0, only the integrity of the header is validated.\n * when `deep` is 1, we scan all the entries one by one. */\nint lpValidateIntegrity(unsigned char *lp, size_t size, int deep){\n    /* Check that we can actually read the header. (and EOF) */\n    if (size < LP_HDR_SIZE + 1)\n        return 0;\n\n    /* Check that the encoded size in the header must match the allocated size. */\n    size_t bytes = lpGetTotalBytes(lp);\n    if (bytes != size)\n        return 0;\n\n    /* The last byte must be the terminator. */\n    if (lp[size-1] != LP_EOF)\n        return 0;\n\n    if (!deep)\n        return 1;\n\n    /* Validate the invividual entries. */\n    uint32_t count = 0;\n    unsigned char *p = lp + LP_HDR_SIZE;\n    while(p && p[0] != LP_EOF) {\n        if (!lpValidateNext(lp, &p, bytes))\n            return 0;\n        count++;\n    }\n\n    /* Check that the count in the header is correct */\n    uint32_t numele = lpGetNumElements(lp);\n    if (numele != LP_HDR_NUMELE_UNKNOWN && numele != count)\n        return 0;\n\n    return 1;\n}\n"
  },
  {
    "path": "src/listpack.h",
    "content": "/* Listpack -- A lists of strings serialization format\n *\n * This file implements the specification you can find at:\n *\n *  https://github.com/antirez/listpack\n *\n * Copyright (c) 2017, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __LISTPACK_H\n#define __LISTPACK_H\n\n#include <stdlib.h>\n#include <stdint.h>\n\n#define LP_INTBUF_SIZE 21 /* 20 digits of -2^63 + 1 null term = 21. */\n\n/* lpInsert() where argument possible values: */\n#define LP_BEFORE 0\n#define LP_AFTER 1\n#define LP_REPLACE 2\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\nunsigned char *lpNew(size_t capacity);\nvoid lpFree(unsigned char *lp);\nunsigned char* lpShrinkToFit(unsigned char *lp);\nunsigned char *lpInsert(unsigned char *lp, unsigned char *ele, uint32_t size, unsigned char *p, int where, unsigned char **newp);\nunsigned char *lpAppend(unsigned char *lp, unsigned char *ele, uint32_t size);\nunsigned char *lpDelete(unsigned char *lp, unsigned char *p, unsigned char **newp);\nuint32_t lpLength(unsigned char *lp);\nunsigned char *lpGet(unsigned char *p, int64_t *count, unsigned char *intbuf);\nunsigned char *lpFirst(unsigned char *lp);\nunsigned char *lpLast(unsigned char *lp);\nunsigned char *lpNext(unsigned char *lp, unsigned char *p);\nunsigned char *lpPrev(unsigned char *lp, unsigned char *p);\nuint32_t lpBytes(unsigned char *lp);\nunsigned char *lpSeek(unsigned char *lp, long index);\nint lpValidateIntegrity(unsigned char *lp, size_t size, int deep);\nunsigned char *lpValidateFirst(unsigned char *lp);\nint lpValidateNext(unsigned char *lp, unsigned char **pp, size_t lpbytes);\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n"
  },
  {
    "path": "src/listpack_malloc.h",
    "content": "/* Listpack -- A lists of strings serialization format\n * https://github.com/antirez/listpack\n *\n * Copyright (c) 2017, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n/* Allocator selection.\n *\n * This file is used in order to change the Rax allocator at compile time.\n * Just define the following defines to what you want to use. Also add\n * the include of your alternate allocator if needed (not needed in order\n * to use the default libc allocator). */\n\n#ifndef LISTPACK_ALLOC_H\n#define LISTPACK_ALLOC_H\n#include \"zmalloc.h\"\n#define lp_malloc(size) zmalloc(size, MALLOC_SHARED)\n#define lp_realloc(ptr, size) zrealloc(ptr, size, MALLOC_SHARED)\n#define lp_free zfree\n#define lp_malloc_size zmalloc_usable_size\n#endif\n"
  },
  {
    "path": "src/localtime.c",
    "content": "/*\n * Copyright (c) 2018, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <time.h>\n\n/* This is a safe version of localtime() which contains no locks and is\n * fork() friendly. Even the _r version of localtime() cannot be used safely\n * in Redis. Another thread may be calling localtime() while the main thread\n * forks(). Later when the child process calls localtime() again, for instance\n * in order to log something to the Redis log, it may deadlock: in the copy\n * of the address space of the forked process the lock will never be released.\n *\n * This function takes the timezone 'tz' as argument, and the 'dst' flag is\n * used to check if daylight saving time is currently in effect. The caller\n * of this function should obtain such information calling tzset() ASAP in the\n * main() function to obtain the timezone offset from the 'timezone' global\n * variable. To obtain the daylight information, if it is currently active or not,\n * one trick is to call localtime() in main() ASAP as well, and get the\n * information from the tm_isdst field of the tm structure. However the daylight\n * time may switch in the future for long running processes, so this information\n * should be refreshed at safe times.\n *\n * Note that this function does not work for dates < 1/1/1970, it is solely\n * designed to work with what time(NULL) may return, and to support Redis\n * logging of the dates, it's not really a complete implementation. */\nstatic int is_leap_year(time_t year) {\n    if (year % 4) return 0;         /* A year not divisible by 4 is not leap. */\n    else if (year % 100) return 1;  /* If div by 4 and not 100 is surely leap. */\n    else if (year % 400) return 0;  /* If div by 100 *and* not by 400 is not leap. */\n    else return 1;                  /* If div by 100 and 400 is leap. */\n}\n\nvoid nolocks_localtime(struct tm *tmp, time_t t, time_t tz, int dst) {\n    const time_t secs_min = 60;\n    const time_t secs_hour = 3600;\n    const time_t secs_day = 3600*24;\n\n    t -= tz;                            /* Adjust for timezone. */\n    t += 3600*dst;                      /* Adjust for daylight time. */\n    time_t days = t / secs_day;         /* Days passed since epoch. */\n    time_t seconds = t % secs_day;      /* Remaining seconds. */\n\n    tmp->tm_isdst = dst;\n    tmp->tm_hour = seconds / secs_hour;\n    tmp->tm_min = (seconds % secs_hour) / secs_min;\n    tmp->tm_sec = (seconds % secs_hour) % secs_min;\n\n    /* 1/1/1970 was a Thursday, that is, day 4 from the POV of the tm structure\n     * where sunday = 0, so to calculate the day of the week we have to add 4\n     * and take the modulo by 7. */\n    tmp->tm_wday = (days+4)%7;\n\n    /* Calculate the current year. */\n    tmp->tm_year = 1970;\n    while(1) {\n        /* Leap years have one day more. */\n        time_t days_this_year = 365 + is_leap_year(tmp->tm_year);\n        if (days_this_year > days) break;\n        days -= days_this_year;\n        tmp->tm_year++;\n    }\n    tmp->tm_yday = days;  /* Number of day of the current year. */\n\n    /* We need to calculate in which month and day of the month we are. To do\n     * so we need to skip days according to how many days there are in each\n     * month, and adjust for the leap year that has one more day in February. */\n    int mdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};\n    mdays[1] += is_leap_year(tmp->tm_year);\n\n    tmp->tm_mon = 0;\n    while(days >= mdays[tmp->tm_mon]) {\n        days -= mdays[tmp->tm_mon];\n        tmp->tm_mon++;\n    }\n\n    tmp->tm_mday = days+1;  /* Add 1 since our 'days' is zero-based. */\n    tmp->tm_year -= 1900;   /* Surprisingly tm_year is year-1900. */\n}\n\n#ifdef LOCALTIME_TEST_MAIN\n#include <stdio.h>\n\nint main(void) {\n    /* Obtain timezone and daylight info. */\n    tzset(); /* Now 'timezome' global is populated. */\n    time_t t = time(NULL);\n    struct tm *aux = localtime(&t);\n    int daylight_active = aux->tm_isdst;\n\n    struct tm tm;\n    char buf[1024];\n\n    nolocks_localtime(&tm,t,timezone,daylight_active);\n    strftime(buf,sizeof(buf),\"%d %b %H:%M:%S\",&tm);\n    printf(\"[timezone: %d, dl: %d] %s\\n\", (int)timezone, (int)daylight_active, buf);\n}\n#endif\n"
  },
  {
    "path": "src/lolwut.c",
    "content": "/*\n * Copyright (c) 2018, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n *\n * ----------------------------------------------------------------------------\n *\n * This file implements the LOLWUT command. The command should do something\n * fun and interesting, and should be replaced by a new implementation at\n * each new version of Redis.\n */\n\n#include \"server.h\"\n#include \"lolwut.h\"\n#include <math.h>\n\nvoid lolwut5Command(client *c);\nvoid lolwut6Command(client *c);\n\n/* The default target for LOLWUT if no matching version was found.\n * This is what unstable versions of Redis will display. */\nvoid lolwutUnstableCommand(client *c) {\n    sds rendered = sdsnew(\"Redis ver. \");\n    rendered = sdscat(rendered,REDIS_VERSION);\n    rendered = sdscatlen(rendered,\"\\n\",1);\n    addReplyVerbatim(c,rendered,sdslen(rendered),\"txt\");\n    sdsfree(rendered);\n}\n\n/* LOLWUT [VERSION <version>] [... version specific arguments ...] */\nvoid lolwutCommand(client *c) {\n    char *v = REDIS_VERSION;\n    char verstr[64];\n\n    if (c->argc >= 3 && !strcasecmp(c->argv[1]->ptr,\"version\")) {\n        long ver;\n        if (getLongFromObjectOrReply(c,c->argv[2],&ver,NULL) != C_OK) return;\n        snprintf(verstr,sizeof(verstr),\"%u.0.0\",(unsigned int)ver);\n        v = verstr;\n\n        /* Adjust argv/argc to filter the \"VERSION ...\" option, since the\n         * specific LOLWUT version implementations don't know about it\n         * and expect their arguments. */\n        c->argv += 2;\n        c->argc -= 2;\n    }\n\n    if ((v[0] == '5' && v[1] == '.' && v[2] != '9') ||\n        (v[0] == '4' && v[1] == '.' && v[2] == '9'))\n        lolwut5Command(c);\n    else if ((v[0] == '6' && v[1] == '.' && v[2] != '9') ||\n             (v[0] == '5' && v[1] == '.' && v[2] == '9'))\n        lolwut6Command(c);\n    else\n        lolwutUnstableCommand(c);\n\n    /* Fix back argc/argv in case of VERSION argument. */\n    if (v == verstr) {\n        c->argv -= 2;\n        c->argc += 2;\n    }\n}\n\n/* ========================== LOLWUT Canvase ===============================\n * Many LOLWUT versions will likely print some computer art to the screen.\n * This is the case with LOLWUT 5 and LOLWUT 6, so here there is a generic\n * canvas implementation that can be reused.  */\n\n/* Allocate and return a new canvas of the specified size. */\nlwCanvas *lwCreateCanvas(int width, int height, int bgcolor) {\n    lwCanvas *canvas = zmalloc(sizeof(*canvas));\n    canvas->width = width;\n    canvas->height = height;\n    canvas->pixels = zmalloc((size_t)width*height);\n    memset(canvas->pixels,bgcolor,(size_t)width*height);\n    return canvas;\n}\n\n/* Free the canvas created by lwCreateCanvas(). */\nvoid lwFreeCanvas(lwCanvas *canvas) {\n    zfree(canvas->pixels);\n    zfree(canvas);\n}\n\n/* Set a pixel to the specified color. Color is 0 or 1, where zero means no\n * dot will be displayed, and 1 means dot will be displayed.\n * Coordinates are arranged so that left-top corner is 0,0. You can write\n * out of the size of the canvas without issues. */\nvoid lwDrawPixel(lwCanvas *canvas, int x, int y, int color) {\n    if (x < 0 || x >= canvas->width ||\n        y < 0 || y >= canvas->height) return;\n    canvas->pixels[x+y*canvas->width] = color;\n}\n\n/* Return the value of the specified pixel on the canvas. */\nint lwGetPixel(lwCanvas *canvas, int x, int y) {\n    if (x < 0 || x >= canvas->width ||\n        y < 0 || y >= canvas->height) return 0;\n    return canvas->pixels[x+y*canvas->width];\n}\n\n/* Draw a line from x1,y1 to x2,y2 using the Bresenham algorithm. */\nvoid lwDrawLine(lwCanvas *canvas, int x1, int y1, int x2, int y2, int color) {\n    int dx = abs(x2-x1);\n    int dy = abs(y2-y1);\n    int sx = (x1 < x2) ? 1 : -1;\n    int sy = (y1 < y2) ? 1 : -1;\n    int err = dx-dy, e2;\n\n    while(1) {\n        lwDrawPixel(canvas,x1,y1,color);\n        if (x1 == x2 && y1 == y2) break;\n        e2 = err*2;\n        if (e2 > -dy) {\n            err -= dy;\n            x1 += sx;\n        }\n        if (e2 < dx) {\n            err += dx;\n            y1 += sy;\n        }\n    }\n}\n\n/* Draw a square centered at the specified x,y coordinates, with the specified\n * rotation angle and size. In order to write a rotated square, we use the\n * trivial fact that the parametric equation:\n *\n *  x = sin(k)\n *  y = cos(k)\n *\n * Describes a circle for values going from 0 to 2*PI. So basically if we start\n * at 45 degrees, that is k = PI/4, with the first point, and then we find\n * the other three points incrementing K by PI/2 (90 degrees), we'll have the\n * points of the square. In order to rotate the square, we just start with\n * k = PI/4 + rotation_angle, and we are done.\n *\n * Of course the vanilla equations above will describe the square inside a\n * circle of radius 1, so in order to draw larger squares we'll have to\n * multiply the obtained coordinates, and then translate them. However this\n * is much simpler than implementing the abstract concept of 2D shape and then\n * performing the rotation/translation transformation, so for LOLWUT it's\n * a good approach. */\nvoid lwDrawSquare(lwCanvas *canvas, int x, int y, float size, float angle, int color) {\n    int px[4], py[4];\n\n    /* Adjust the desired size according to the fact that the square inscribed\n     * into a circle of radius 1 has the side of length SQRT(2). This way\n     * size becomes a simple multiplication factor we can use with our\n     * coordinates to magnify them. */\n    size /= 1.4142135623;\n    size = round(size);\n\n    /* Compute the four points. */\n    float k = M_PI/4 + angle;\n    for (int j = 0; j < 4; j++) {\n        px[j] = round(sin(k) * size + x);\n        py[j] = round(cos(k) * size + y);\n        k += M_PI/2;\n    }\n\n    /* Draw the square. */\n    for (int j = 0; j < 4; j++)\n        lwDrawLine(canvas,px[j],py[j],px[(j+1)%4],py[(j+1)%4],color);\n}\n"
  },
  {
    "path": "src/lolwut.h",
    "content": "/*\n * Copyright (c) 2018-2019, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n/* This structure represents our canvas. Drawing functions will take a pointer\n * to a canvas to write to it. Later the canvas can be rendered to a string\n * suitable to be printed on the screen, using unicode Braille characters. */\n\n/* This represents a very simple generic canvas in order to draw stuff.\n * It's up to each LOLWUT versions to translate what they draw to the\n * screen, depending on the result to accomplish. */\n\n#ifndef __LOLWUT_H\n#define __LOLWUT_H\n\ntypedef struct lwCanvas {\n    int width;\n    int height;\n    char *pixels;\n} lwCanvas;\n\n/* Drawing functions implemented inside lolwut.c. */\nlwCanvas *lwCreateCanvas(int width, int height, int bgcolor);\nvoid lwFreeCanvas(lwCanvas *canvas);\nvoid lwDrawPixel(lwCanvas *canvas, int x, int y, int color);\nint lwGetPixel(lwCanvas *canvas, int x, int y);\nvoid lwDrawLine(lwCanvas *canvas, int x1, int y1, int x2, int y2, int color);\nvoid lwDrawSquare(lwCanvas *canvas, int x, int y, float size, float angle, int color);\n\n#endif\n"
  },
  {
    "path": "src/lolwut5.c",
    "content": "/*\n * Copyright (c) 2018, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n *\n * ----------------------------------------------------------------------------\n *\n * This file implements the LOLWUT command. The command should do something\n * fun and interesting, and should be replaced by a new implementation at\n * each new version of Redis.\n */\n\n#include \"server.h\"\n#include \"lolwut.h\"\n#include <math.h>\n\n/* Translate a group of 8 pixels (2x4 vertical rectangle) to the corresponding\n * braille character. The byte should correspond to the pixels arranged as\n * follows, where 0 is the least significant bit, and 7 the most significant\n * bit:\n *\n *   0 3\n *   1 4\n *   2 5\n *   6 7\n *\n * The corresponding utf8 encoded character is set into the three bytes\n * pointed by 'output'.\n */\n#include <stdio.h>\nvoid lwTranslatePixelsGroup(int byte, char *output) {\n    int code = 0x2800 + byte;\n    /* Convert to unicode. This is in the U0800-UFFFF range, so we need to\n     * emit it like this in three bytes:\n     * 1110xxxx 10xxxxxx 10xxxxxx. */\n    output[0] = 0xE0 | (code >> 12);          /* 1110-xxxx */\n    output[1] = 0x80 | ((code >> 6) & 0x3F);  /* 10-xxxxxx */\n    output[2] = 0x80 | (code & 0x3F);         /* 10-xxxxxx */\n}\n\n/* Schotter, the output of LOLWUT of Redis 5, is a computer graphic art piece\n * generated by Georg Nees in the 60s. It explores the relationship between\n * caos and order.\n *\n * The function creates the canvas itself, depending on the columns available\n * in the output display and the number of squares per row and per column\n * requested by the caller. */\nlwCanvas *lwDrawSchotter(int console_cols, int squares_per_row, int squares_per_col) {\n    /* Calculate the canvas size. */\n    int canvas_width = console_cols*2;\n    int padding = canvas_width > 4 ? 2 : 0;\n    float square_side = (float)(canvas_width-padding*2) / squares_per_row;\n    int canvas_height = square_side * squares_per_col + padding*2;\n    lwCanvas *canvas = lwCreateCanvas(canvas_width, canvas_height, 0);\n\n    for (int y = 0; y < squares_per_col; y++) {\n        for (int x = 0; x < squares_per_row; x++) {\n            int sx = x * square_side + square_side/2 + padding;\n            int sy = y * square_side + square_side/2 + padding;\n            /* Rotate and translate randomly as we go down to lower\n             * rows. */\n            float angle = 0;\n            if (y > 1) {\n                float r1 = (float)rand() / (float) RAND_MAX / squares_per_col * y;\n                float r2 = (float)rand() / (float) RAND_MAX / squares_per_col * y;\n                float r3 = (float)rand() / (float) RAND_MAX / squares_per_col * y;\n                if (rand() % 2) r1 = -r1;\n                if (rand() % 2) r2 = -r2;\n                if (rand() % 2) r3 = -r3;\n                angle = r1;\n                sx += r2*square_side/3;\n                sy += r3*square_side/3;\n            }\n            lwDrawSquare(canvas,sx,sy,square_side,angle,1);\n        }\n    }\n\n    return canvas;\n}\n\n/* Converts the canvas to an SDS string representing the UTF8 characters to\n * print to the terminal in order to obtain a graphical representaiton of the\n * logical canvas. The actual returned string will require a terminal that is\n * width/2 large and height/4 tall in order to hold the whole image without\n * overflowing or scrolling, since each Barille character is 2x4. */\nstatic sds renderCanvas(lwCanvas *canvas) {\n    sds text = sdsempty();\n    for (int y = 0; y < canvas->height; y += 4) {\n        for (int x = 0; x < canvas->width; x += 2) {\n            /* We need to emit groups of 8 bits according to a specific\n             * arrangement. See lwTranslatePixelsGroup() for more info. */\n            int byte = 0;\n            if (lwGetPixel(canvas,x,y)) byte |= (1<<0);\n            if (lwGetPixel(canvas,x,y+1)) byte |= (1<<1);\n            if (lwGetPixel(canvas,x,y+2)) byte |= (1<<2);\n            if (lwGetPixel(canvas,x+1,y)) byte |= (1<<3);\n            if (lwGetPixel(canvas,x+1,y+1)) byte |= (1<<4);\n            if (lwGetPixel(canvas,x+1,y+2)) byte |= (1<<5);\n            if (lwGetPixel(canvas,x,y+3)) byte |= (1<<6);\n            if (lwGetPixel(canvas,x+1,y+3)) byte |= (1<<7);\n            char unicode[3];\n            lwTranslatePixelsGroup(byte,unicode);\n            text = sdscatlen(text,unicode,3);\n        }\n        if (y != canvas->height-1) text = sdscatlen(text,\"\\n\",1);\n    }\n    return text;\n}\n\n/* The LOLWUT command:\n *\n * LOLWUT [terminal columns] [squares-per-row] [squares-per-col]\n *\n * By default the command uses 66 columns, 8 squares per row, 12 squares\n * per column.\n */\nvoid lolwut5Command(client *c) {\n    long cols = 66;\n    long squares_per_row = 8;\n    long squares_per_col = 12;\n\n    /* Parse the optional arguments if any. */\n    if (c->argc > 1 &&\n        getLongFromObjectOrReply(c,c->argv[1],&cols,NULL) != C_OK)\n        return;\n\n    if (c->argc > 2 &&\n        getLongFromObjectOrReply(c,c->argv[2],&squares_per_row,NULL) != C_OK)\n        return;\n\n    if (c->argc > 3 &&\n        getLongFromObjectOrReply(c,c->argv[3],&squares_per_col,NULL) != C_OK)\n        return;\n\n    /* Limits. We want LOLWUT to be always reasonably fast and cheap to execute\n     * so we have maximum number of columns, rows, and output resolution. */\n    if (cols < 1) cols = 1;\n    if (cols > 1000) cols = 1000;\n    if (squares_per_row < 1) squares_per_row = 1;\n    if (squares_per_row > 200) squares_per_row = 200;\n    if (squares_per_col < 1) squares_per_col = 1;\n    if (squares_per_col > 200) squares_per_col = 200;\n\n    /* Generate some computer art and reply. */\n    lwCanvas *canvas = lwDrawSchotter(cols,squares_per_row,squares_per_col);\n    sds rendered = renderCanvas(canvas);\n    rendered = sdscat(rendered,\n        \"\\nGeorg Nees - schotter, plotter on paper, 1968. Redis ver. \");\n    rendered = sdscat(rendered,REDIS_VERSION);\n    rendered = sdscatlen(rendered,\"\\n\",1);\n    addReplyVerbatim(c,rendered,sdslen(rendered),\"txt\");\n    sdsfree(rendered);\n    lwFreeCanvas(canvas);\n}\n"
  },
  {
    "path": "src/lolwut6.c",
    "content": "/*\n * Copyright (c) 2019, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n *\n * ----------------------------------------------------------------------------\n *\n * This file implements the LOLWUT command. The command should do something\n * fun and interesting, and should be replaced by a new implementation at\n * each new version of Redis.\n *\n * Thanks to Michele Hiki Falcone for the original image that ispired\n * the image, part of his game, Plaguemon.\n *\n * Thanks to the Shhh computer art collective for the help in tuning the\n * output to have a better artistic effect.\n */\n\n#include \"server.h\"\n#include \"lolwut.h\"\n\n/* Render the canvas using the four gray levels of the standard color\n * terminal: they match very well to the grayscale display of the gameboy. */\nstatic sds renderCanvas(lwCanvas *canvas) {\n    sds text = sdsempty();\n    for (int y = 0; y < canvas->height; y++) {\n        for (int x = 0; x < canvas->width; x++) {\n            int color = lwGetPixel(canvas,x,y);\n            char *ce; /* Color escape sequence. */\n\n            /* Note that we set both the foreground and background color.\n             * This way we are able to get a more consistent result among\n             * different terminals implementations. */\n            switch(color) {\n            case 0: ce = \"0;30;40m\"; break;    /* Black */\n            case 1: ce = \"0;90;100m\"; break;   /* Gray 1 */\n            case 2: ce = \"0;37;47m\"; break;    /* Gray 2 */\n            case 3: ce = \"0;97;107m\"; break;   /* White */\n            default: ce = \"0;30;40m\"; break;   /* Just for safety. */\n            }\n            text = sdscatprintf(text,\"\\033[%s \\033[0m\",ce);\n        }\n        if (y != canvas->height-1) text = sdscatlen(text,\"\\n\",1);\n    }\n    return text;\n}\n\n/* Draw a skyscraper on the canvas, according to the parameters in the\n * 'skyscraper' structure. Window colors are random and are always one\n * of the two grays. */\nstruct skyscraper {\n    int xoff;       /* X offset. */\n    int width;      /* Pixels width. */\n    int height;     /* Pixels height. */\n    int windows;    /* Draw windows if true. */\n    int color;      /* Color of the skyscraper. */\n};\n\nvoid generateSkyscraper(lwCanvas *canvas, struct skyscraper *si) {\n    int starty = canvas->height-1;\n    int endy = starty - si->height + 1;\n    for (int y = starty; y >= endy; y--) {\n        for (int x = si->xoff; x < si->xoff+si->width; x++) {\n            /* The roof is four pixels less wide. */\n            if (y == endy && (x <= si->xoff+1 || x >= si->xoff+si->width-2))\n                continue;\n            int color = si->color;\n            /* Alter the color if this is a place where we want to\n             * draw a window. We check that we are in the inner part of the\n             * skyscraper, so that windows are far from the borders. */\n            if (si->windows &&\n                x > si->xoff+1 &&\n                x < si->xoff+si->width-2 &&\n                y > endy+1 &&\n                y < starty-1)\n            {\n                /* Calculate the x,y position relative to the start of\n                 * the window area. */\n                int relx = x - (si->xoff+1);\n                int rely = y - (endy+1);\n\n                /* Note that we want the windows to be two pixels wide\n                 * but just one pixel tall, because terminal \"pixels\"\n                 * (characters) are not square. */\n                if (relx/2 % 2 && rely % 2) {\n                    do {\n                        color = 1 + rand() % 2;\n                    } while (color == si->color);\n                    /* Except we want adjacent pixels creating the same\n                     * window to be the same color. */\n                    if (relx % 2) color = lwGetPixel(canvas,x-1,y);\n                }\n            }\n            lwDrawPixel(canvas,x,y,color);\n        }\n    }\n}\n\n/* Generate a skyline inspired by the parallax backgrounds of 8 bit games. */\nvoid generateSkyline(lwCanvas *canvas) {\n    struct skyscraper si;\n\n    /* First draw the background skyscraper without windows, using the\n     * two different grays. We use two passes to make sure that the lighter\n     * ones are always in the background. */\n    for (int color = 2; color >= 1; color--) {\n        si.color = color;\n        for (int offset = -10; offset < canvas->width;) {\n            offset += rand() % 8;\n            si.xoff = offset;\n            si.width = 10 + rand()%9;\n            if (color == 2)\n                si.height = canvas->height/2 + rand()%canvas->height/2;\n            else\n                si.height = canvas->height/2 + rand()%canvas->height/3;\n            si.windows = 0;\n            generateSkyscraper(canvas, &si);\n            if (color == 2)\n                offset += si.width/2;\n            else\n                offset += si.width+1;\n        }\n    }\n\n    /* Now draw the foreground skyscraper with the windows. */\n    si.color = 0;\n    for (int offset = -10; offset < canvas->width;) {\n        offset += rand() % 8;\n        si.xoff = offset;\n        si.width = 5 + rand()%14;\n        if (si.width % 4) si.width += (si.width % 3);\n        si.height = canvas->height/3 + rand()%canvas->height/2;\n        si.windows = 1;\n        generateSkyscraper(canvas, &si);\n        offset += si.width+5;\n    }\n}\n\n/* The LOLWUT 6 command:\n *\n * LOLWUT [columns] [rows]\n *\n * By default the command uses 80 columns, 40 squares per row\n * per column.\n */\nvoid lolwut6Command(client *c) {\n    long cols = 80;\n    long rows = 20;\n\n    /* Parse the optional arguments if any. */\n    if (c->argc > 1 &&\n        getLongFromObjectOrReply(c,c->argv[1],&cols,NULL) != C_OK)\n        return;\n\n    if (c->argc > 2 &&\n        getLongFromObjectOrReply(c,c->argv[2],&rows,NULL) != C_OK)\n        return;\n\n    /* Limits. We want LOLWUT to be always reasonably fast and cheap to execute\n     * so we have maximum number of columns, rows, and output resulution. */\n    if (cols < 1) cols = 1;\n    if (cols > 1000) cols = 1000;\n    if (rows < 1) rows = 1;\n    if (rows > 1000) rows = 1000;\n\n    /* Generate the city skyline and reply. */\n    lwCanvas *canvas = lwCreateCanvas(cols,rows,3);\n    generateSkyline(canvas);\n    sds rendered = renderCanvas(canvas);\n    rendered = sdscat(rendered,\n        \"\\nDedicated to the 8 bit game developers of past and present.\\n\"\n        \"Original 8 bit image from Plaguemon by hikikomori. Redis ver. \");\n    rendered = sdscat(rendered,REDIS_VERSION);\n    rendered = sdscatlen(rendered,\"\\n\",1);\n    addReplyVerbatim(c,rendered,sdslen(rendered),\"txt\");\n    sdsfree(rendered);\n    lwFreeCanvas(canvas);\n}\n"
  },
  {
    "path": "src/lzf.h",
    "content": "/*\n * Copyright (c) 2000-2008 Marc Alexander Lehmann <schmorp@schmorp.de>\n *\n * Redistribution and use in source and binary forms, with or without modifica-\n * tion, are permitted provided that the following conditions are met:\n *\n *   1.  Redistributions of source code must retain the above copyright notice,\n *       this list of conditions and the following disclaimer.\n *\n *   2.  Redistributions in binary form must reproduce the above copyright\n *       notice, this list of conditions and the following disclaimer in the\n *       documentation and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED\n * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-\n * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-\n * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\n * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;\n * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\n * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-\n * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED\n * OF THE POSSIBILITY OF SUCH DAMAGE.\n *\n * Alternatively, the contents of this file may be used under the terms of\n * the GNU General Public License (\"GPL\") version 2 or any later version,\n * in which case the provisions of the GPL are applicable instead of\n * the above. If you wish to allow the use of your version of this file\n * only under the terms of the GPL and not to allow others to use your\n * version of this file under the BSD license, indicate your decision\n * by deleting the provisions above and replace them with the notice\n * and other provisions required by the GPL. If you do not delete the\n * provisions above, a recipient may use your version of this file under\n * either the BSD or the GPL.\n */\n\n#ifndef LZF_H\n#define LZF_H\n\n/***********************************************************************\n**\n**\tlzf -- an extremely fast/free compression/decompression-method\n**\thttp://liblzf.plan9.de/\n**\n**\tThis algorithm is believed to be patent-free.\n**\n***********************************************************************/\n\n#define LZF_VERSION 0x0105 /* 1.5, API version */\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/*\n * Compress in_len bytes stored at the memory block starting at\n * in_data and write the result to out_data, up to a maximum length\n * of out_len bytes.\n *\n * If the output buffer is not large enough or any error occurs return 0,\n * otherwise return the number of bytes used, which might be considerably\n * more than in_len (but less than 104% of the original size), so it\n * makes sense to always use out_len == in_len - 1), to ensure _some_\n * compression, and store the data uncompressed otherwise (with a flag, of\n * course.\n *\n * lzf_compress might use different algorithms on different systems and\n * even different runs, thus might result in different compressed strings\n * depending on the phase of the moon or similar factors. However, all\n * these strings are architecture-independent and will result in the\n * original data when decompressed using lzf_decompress.\n *\n * The buffers must not be overlapping.\n *\n * If the option LZF_STATE_ARG is enabled, an extra argument must be\n * supplied which is not reflected in this header file. Refer to lzfP.h\n * and lzf_c.c.\n *\n */\nunsigned int\nlzf_compress (const void *const in_data,  unsigned int in_len,\n              void             *out_data, unsigned int out_len);\n\n/*\n * Decompress data compressed with some version of the lzf_compress\n * function and stored at location in_data and length in_len. The result\n * will be stored at out_data up to a maximum of out_len characters.\n *\n * If the output buffer is not large enough to hold the decompressed\n * data, a 0 is returned and errno is set to E2BIG. Otherwise the number\n * of decompressed bytes (i.e. the original length of the data) is\n * returned.\n *\n * If an error in the compressed data is detected, a zero is returned and\n * errno is set to EINVAL.\n *\n * This function is very fast, about as fast as a copying loop.\n */\nunsigned int\nlzf_decompress (const void *const in_data,  unsigned int in_len,\n                void             *out_data, unsigned int out_len);\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n\n"
  },
  {
    "path": "src/lzfP.h",
    "content": "/*\n * Copyright (c) 2000-2007 Marc Alexander Lehmann <schmorp@schmorp.de>\n *\n * Redistribution and use in source and binary forms, with or without modifica-\n * tion, are permitted provided that the following conditions are met:\n *\n *   1.  Redistributions of source code must retain the above copyright notice,\n *       this list of conditions and the following disclaimer.\n *\n *   2.  Redistributions in binary form must reproduce the above copyright\n *       notice, this list of conditions and the following disclaimer in the\n *       documentation and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED\n * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-\n * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-\n * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\n * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;\n * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\n * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-\n * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED\n * OF THE POSSIBILITY OF SUCH DAMAGE.\n *\n * Alternatively, the contents of this file may be used under the terms of\n * the GNU General Public License (\"GPL\") version 2 or any later version,\n * in which case the provisions of the GPL are applicable instead of\n * the above. If you wish to allow the use of your version of this file\n * only under the terms of the GPL and not to allow others to use your\n * version of this file under the BSD license, indicate your decision\n * by deleting the provisions above and replace them with the notice\n * and other provisions required by the GPL. If you do not delete the\n * provisions above, a recipient may use your version of this file under\n * either the BSD or the GPL.\n */\n\n#ifndef LZFP_h\n#define LZFP_h\n\n#define STANDALONE 1 /* at the moment, this is ok. */\n\n#ifndef STANDALONE\n# include \"lzf.h\"\n#endif\n\n/*\n * Size of hashtable is (1 << HLOG) * sizeof (char *)\n * decompression is independent of the hash table size\n * the difference between 15 and 14 is very small\n * for small blocks (and 14 is usually a bit faster).\n * For a low-memory/faster configuration, use HLOG == 13;\n * For best compression, use 15 or 16 (or more, up to 22).\n */\n#ifndef HLOG\n# define HLOG 16\n#endif\n\n/*\n * Sacrifice very little compression quality in favour of compression speed.\n * This gives almost the same compression as the default code, and is\n * (very roughly) 15% faster. This is the preferred mode of operation.\n */\n#ifndef VERY_FAST\n# define VERY_FAST 1\n#endif\n\n/*\n * Sacrifice some more compression quality in favour of compression speed.\n * (roughly 1-2% worse compression for large blocks and\n * 9-10% for small, redundant, blocks and >>20% better speed in both cases)\n * In short: when in need for speed, enable this for binary data,\n * possibly disable this for text data.\n */\n#ifndef ULTRA_FAST\n# define ULTRA_FAST 0\n#endif\n\n/*\n * Unconditionally aligning does not cost very much, so do it if unsure\n */\n#ifndef STRICT_ALIGN\n# if !(defined(__i386) || defined (__amd64))\n#  define STRICT_ALIGN 1\n# else\n#  define STRICT_ALIGN 0\n# endif\n#endif\n\n/*\n * You may choose to pre-set the hash table (might be faster on some\n * modern cpus and large (>>64k) blocks, and also makes compression\n * deterministic/repeatable when the configuration otherwise is the same).\n */\n#ifndef INIT_HTAB\n# define INIT_HTAB 0\n#endif\n\n/*\n * Avoid assigning values to errno variable? for some embedding purposes\n * (linux kernel for example), this is necessary. NOTE: this breaks\n * the documentation in lzf.h. Avoiding errno has no speed impact.\n */\n#ifndef AVOID_ERRNO\n# define AVOID_ERRNO 0\n#endif\n\n/*\n * Whether to pass the LZF_STATE variable as argument, or allocate it\n * on the stack. For small-stack environments, define this to 1.\n * NOTE: this breaks the prototype in lzf.h.\n */\n#ifndef LZF_STATE_ARG\n# define LZF_STATE_ARG 0\n#endif\n\n/*\n * Whether to add extra checks for input validity in lzf_decompress\n * and return EINVAL if the input stream has been corrupted. This\n * only shields against overflowing the input buffer and will not\n * detect most corrupted streams.\n * This check is not normally noticeable on modern hardware\n * (<1% slowdown), but might slow down older cpus considerably.\n */\n#ifndef CHECK_INPUT\n# define CHECK_INPUT 1\n#endif\n\n/*\n * Whether to store pointers or offsets inside the hash table. On\n * 64 bit architectures, pointers take up twice as much space,\n * and might also be slower. Default is to autodetect.\n */\n/*#define LZF_USER_OFFSETS autodetect */\n\n/*****************************************************************************/\n/* nothing should be changed below */\n\n#ifdef __cplusplus\n# include <cstring>\n# include <climits>\nusing namespace std;\n#else\n# include <string.h>\n# include <limits.h>\n#endif\n\n#ifndef LZF_USE_OFFSETS\n# if defined (WIN32)\n#  define LZF_USE_OFFSETS defined(_M_X64)\n# else\n#  if __cplusplus > 199711L\n#   include <cstdint>\n#  else\n#   include <stdint.h>\n#  endif\n#  define LZF_USE_OFFSETS (UINTPTR_MAX > 0xffffffffU)\n# endif\n#endif\n\ntypedef unsigned char u8;\n\n#if LZF_USE_OFFSETS\n# define LZF_HSLOT_BIAS ((const u8 *)in_data)\n  typedef unsigned int LZF_HSLOT;\n#else\n# define LZF_HSLOT_BIAS 0\n  typedef const u8 *LZF_HSLOT;\n#endif\n\ntypedef LZF_HSLOT LZF_STATE[1 << (HLOG)];\n\n#if !STRICT_ALIGN\n/* for unaligned accesses we need a 16 bit datatype. */\n# if USHRT_MAX == 65535\n    typedef unsigned short u16;\n# elif UINT_MAX == 65535\n    typedef unsigned int u16;\n# else\n#  undef STRICT_ALIGN\n#  define STRICT_ALIGN 1\n# endif\n#endif\n\n#if ULTRA_FAST\n# undef VERY_FAST\n#endif\n\n#endif\n\n"
  },
  {
    "path": "src/lzf_c.c",
    "content": "/*\n * Copyright (c) 2000-2010 Marc Alexander Lehmann <schmorp@schmorp.de>\n *\n * Redistribution and use in source and binary forms, with or without modifica-\n * tion, are permitted provided that the following conditions are met:\n *\n *   1.  Redistributions of source code must retain the above copyright notice,\n *       this list of conditions and the following disclaimer.\n *\n *   2.  Redistributions in binary form must reproduce the above copyright\n *       notice, this list of conditions and the following disclaimer in the\n *       documentation and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED\n * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-\n * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-\n * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\n * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;\n * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\n * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-\n * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED\n * OF THE POSSIBILITY OF SUCH DAMAGE.\n *\n * Alternatively, the contents of this file may be used under the terms of\n * the GNU General Public License (\"GPL\") version 2 or any later version,\n * in which case the provisions of the GPL are applicable instead of\n * the above. If you wish to allow the use of your version of this file\n * only under the terms of the GPL and not to allow others to use your\n * version of this file under the BSD license, indicate your decision\n * by deleting the provisions above and replace them with the notice\n * and other provisions required by the GPL. If you do not delete the\n * provisions above, a recipient may use your version of this file under\n * either the BSD or the GPL.\n */\n\n#include \"lzfP.h\"\n\n#define HSIZE (1 << (HLOG))\n\n/*\n * don't play with this unless you benchmark!\n * the data format is not dependent on the hash function.\n * the hash function might seem strange, just believe me,\n * it works ;)\n */\n#ifndef FRST\n# define FRST(p) (((p[0]) << 8) | p[1])\n# define NEXT(v,p) (((v) << 8) | p[2])\n# if ULTRA_FAST\n#  define IDX(h) ((( h             >> (3*8 - HLOG)) - h  ) & (HSIZE - 1))\n# elif VERY_FAST\n#  define IDX(h) ((( h             >> (3*8 - HLOG)) - h*5) & (HSIZE - 1))\n# else\n#  define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) - h*5) & (HSIZE - 1))\n# endif\n#endif\n/*\n * IDX works because it is very similar to a multiplicative hash, e.g.\n * ((h * 57321 >> (3*8 - HLOG)) & (HSIZE - 1))\n * the latter is also quite fast on newer CPUs, and compresses similarly.\n *\n * the next one is also quite good, albeit slow ;)\n * (int)(cos(h & 0xffffff) * 1e6)\n */\n\n#if 0\n/* original lzv-like hash function, much worse and thus slower */\n# define FRST(p) (p[0] << 5) ^ p[1]\n# define NEXT(v,p) ((v) << 5) ^ p[2]\n# define IDX(h) ((h) & (HSIZE - 1))\n#endif\n\n#define        MAX_LIT        (1 <<  5)\n#define        MAX_OFF        (1 << 13)\n#define        MAX_REF        ((1 << 8) + (1 << 3))\n\n#if __GNUC__ >= 3\n# define expect(expr,value)         __builtin_expect ((expr),(value))\n# define inline                     inline\n#else\n# define expect(expr,value)         (expr)\n# define inline                     static\n#endif\n\n#define expect_false(expr) expect ((expr) != 0, 0)\n#define expect_true(expr)  expect ((expr) != 0, 1)\n\n/*\n * compressed format\n *\n * 000LLLLL <L+1>    ; literal, L+1=1..33 octets\n * LLLooooo oooooooo ; backref L+1=1..7 octets, o+1=1..4096 offset\n * 111ooooo LLLLLLLL oooooooo ; backref L+8 octets, o+1=1..4096 offset\n *\n */\n\nunsigned int\nlzf_compress (const void *const in_data, unsigned int in_len,\n\t      void *out_data, unsigned int out_len\n#if LZF_STATE_ARG\n              , LZF_STATE htab\n#endif\n              )\n{\n#if !LZF_STATE_ARG\n  LZF_STATE htab;\n#endif\n  const u8 *ip = (const u8 *)in_data;\n        u8 *op = (u8 *)out_data;\n  const u8 *in_end  = ip + in_len;\n        u8 *out_end = op + out_len;\n  const u8 *ref;\n\n  /* off requires a type wide enough to hold a general pointer difference.\n   * ISO C doesn't have that (size_t might not be enough and ptrdiff_t only\n   * works for differences within a single object). We also assume that no\n   * no bit pattern traps. Since the only platform that is both non-POSIX\n   * and fails to support both assumptions is windows 64 bit, we make a\n   * special workaround for it.\n   */\n#if defined (WIN32) && defined (_M_X64)\n  unsigned _int64 off; /* workaround for missing POSIX compliance */\n#else\n  unsigned long off;\n#endif\n  unsigned int hval;\n  int lit;\n\n  if (!in_len || !out_len)\n    return 0;\n\n#if INIT_HTAB\n  memset (htab, 0, sizeof (htab));\n#endif\n\n  lit = 0; op++; /* start run */\n\n  hval = FRST (ip);\n  while (ip < in_end - 2)\n    {\n      LZF_HSLOT *hslot;\n\n      hval = NEXT (hval, ip);\n      hslot = htab + IDX (hval);\n      ref = *hslot + LZF_HSLOT_BIAS; *hslot = ip - LZF_HSLOT_BIAS;\n\n      if (1\n#if INIT_HTAB\n          && ref < ip /* the next test will actually take care of this, but this is faster */\n#endif\n          && (off = ip - ref - 1) < MAX_OFF\n          && ref > (u8 *)in_data\n          && ref[2] == ip[2]\n#if STRICT_ALIGN\n          && ((ref[1] << 8) | ref[0]) == ((ip[1] << 8) | ip[0])\n#else\n          && *(u16 *)ref == *(u16 *)ip\n#endif\n        )\n        {\n          /* match found at *ref++ */\n          unsigned int len = 2;\n          unsigned int maxlen = in_end - ip - len;\n          maxlen = maxlen > MAX_REF ? MAX_REF : maxlen;\n\n          if (expect_false (op + 3 + 1 >= out_end)) /* first a faster conservative test */\n            if (op - !lit + 3 + 1 >= out_end) /* second the exact but rare test */\n              return 0;\n\n          op [- lit - 1] = lit - 1; /* stop run */\n          op -= !lit; /* undo run if length is zero */\n\n          for (;;)\n            {\n              if (expect_true (maxlen > 16))\n                {\n                  len++; if (ref [len] != ip [len]) break;\n                  len++; if (ref [len] != ip [len]) break;\n                  len++; if (ref [len] != ip [len]) break;\n                  len++; if (ref [len] != ip [len]) break;\n\n                  len++; if (ref [len] != ip [len]) break;\n                  len++; if (ref [len] != ip [len]) break;\n                  len++; if (ref [len] != ip [len]) break;\n                  len++; if (ref [len] != ip [len]) break;\n\n                  len++; if (ref [len] != ip [len]) break;\n                  len++; if (ref [len] != ip [len]) break;\n                  len++; if (ref [len] != ip [len]) break;\n                  len++; if (ref [len] != ip [len]) break;\n\n                  len++; if (ref [len] != ip [len]) break;\n                  len++; if (ref [len] != ip [len]) break;\n                  len++; if (ref [len] != ip [len]) break;\n                  len++; if (ref [len] != ip [len]) break;\n                }\n\n              do\n                len++;\n              while (len < maxlen && ref[len] == ip[len]);\n\n              break;\n            }\n\n          len -= 2; /* len is now #octets - 1 */\n          ip++;\n\n          if (len < 7)\n            {\n              *op++ = (off >> 8) + (len << 5);\n            }\n          else\n            {\n              *op++ = (off >> 8) + (  7 << 5);\n              *op++ = len - 7;\n            }\n\n          *op++ = off;\n\n          lit = 0; op++; /* start run */\n\n          ip += len + 1;\n\n          if (expect_false (ip >= in_end - 2))\n            break;\n\n#if ULTRA_FAST || VERY_FAST\n          --ip;\n# if VERY_FAST && !ULTRA_FAST\n          --ip;\n# endif\n          hval = FRST (ip);\n\n          hval = NEXT (hval, ip);\n          htab[IDX (hval)] = ip - LZF_HSLOT_BIAS;\n          ip++;\n\n# if VERY_FAST && !ULTRA_FAST\n          hval = NEXT (hval, ip);\n          htab[IDX (hval)] = ip - LZF_HSLOT_BIAS;\n          ip++;\n# endif\n#else\n          ip -= len + 1;\n\n          do\n            {\n              hval = NEXT (hval, ip);\n              htab[IDX (hval)] = ip - LZF_HSLOT_BIAS;\n              ip++;\n            }\n          while (len--);\n#endif\n        }\n      else\n        {\n          /* one more literal byte we must copy */\n          if (expect_false (op >= out_end))\n            return 0;\n\n          lit++; *op++ = *ip++;\n\n          if (expect_false (lit == MAX_LIT))\n            {\n              op [- lit - 1] = lit - 1; /* stop run */\n              lit = 0; op++; /* start run */\n            }\n        }\n    }\n\n  if (op + 3 > out_end) /* at most 3 bytes can be missing here */\n    return 0;\n\n  while (ip < in_end)\n    {\n      lit++; *op++ = *ip++;\n\n      if (expect_false (lit == MAX_LIT))\n        {\n          op [- lit - 1] = lit - 1; /* stop run */\n          lit = 0; op++; /* start run */\n        }\n    }\n\n  op [- lit - 1] = lit - 1; /* end run */\n  op -= !lit; /* undo run if length is zero */\n\n  return op - (u8 *)out_data;\n}\n\n"
  },
  {
    "path": "src/lzf_d.c",
    "content": "/*\n * Copyright (c) 2000-2010 Marc Alexander Lehmann <schmorp@schmorp.de>\n *\n * Redistribution and use in source and binary forms, with or without modifica-\n * tion, are permitted provided that the following conditions are met:\n *\n *   1.  Redistributions of source code must retain the above copyright notice,\n *       this list of conditions and the following disclaimer.\n *\n *   2.  Redistributions in binary form must reproduce the above copyright\n *       notice, this list of conditions and the following disclaimer in the\n *       documentation and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED\n * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-\n * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO\n * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-\n * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\n * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;\n * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\n * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-\n * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED\n * OF THE POSSIBILITY OF SUCH DAMAGE.\n *\n * Alternatively, the contents of this file may be used under the terms of\n * the GNU General Public License (\"GPL\") version 2 or any later version,\n * in which case the provisions of the GPL are applicable instead of\n * the above. If you wish to allow the use of your version of this file\n * only under the terms of the GPL and not to allow others to use your\n * version of this file under the BSD license, indicate your decision\n * by deleting the provisions above and replace them with the notice\n * and other provisions required by the GPL. If you do not delete the\n * provisions above, a recipient may use your version of this file under\n * either the BSD or the GPL.\n */\n\n#include \"lzfP.h\"\n\n#if AVOID_ERRNO\n# define SET_ERRNO(n)\n#else\n# include <errno.h>\n# define SET_ERRNO(n) errno = (n)\n#endif\n\n#if USE_REP_MOVSB /* small win on amd, big loss on intel */\n#if (__i386 || __amd64) && __GNUC__ >= 3\n# define lzf_movsb(dst, src, len)                \\\n   asm (\"rep movsb\"                              \\\n        : \"=D\" (dst), \"=S\" (src), \"=c\" (len)     \\\n        :  \"0\" (dst),  \"1\" (src),  \"2\" (len));\n#endif\n#endif\n\n#if defined(__GNUC__) && __GNUC__ >= 7\n#pragma GCC diagnostic push\n#pragma GCC diagnostic ignored \"-Wimplicit-fallthrough\"\n#endif\nunsigned int\nlzf_decompress (const void *const in_data,  unsigned int in_len,\n                void             *out_data, unsigned int out_len)\n{\n  u8 const *ip = (const u8 *)in_data;\n  u8       *op = (u8 *)out_data;\n  u8 const *const in_end  = ip + in_len;\n  u8       *const out_end = op + out_len;\n\n  while (ip < in_end)\n    {\n      unsigned int ctrl;\n      ctrl = *ip++;\n\n      if (ctrl < (1 << 5)) /* literal run */\n        {\n          ctrl++;\n\n          if (op + ctrl > out_end)\n            {\n              SET_ERRNO (E2BIG);\n              return 0;\n            }\n\n#if CHECK_INPUT\n          if (ip + ctrl > in_end)\n            {\n              SET_ERRNO (EINVAL);\n              return 0;\n            }\n#endif\n\n#ifdef lzf_movsb\n          lzf_movsb (op, ip, ctrl);\n#else\n          switch (ctrl)\n            {\n              case 32: *op++ = *ip++; case 31: *op++ = *ip++; case 30: *op++ = *ip++; case 29: *op++ = *ip++;\n              case 28: *op++ = *ip++; case 27: *op++ = *ip++; case 26: *op++ = *ip++; case 25: *op++ = *ip++;\n              case 24: *op++ = *ip++; case 23: *op++ = *ip++; case 22: *op++ = *ip++; case 21: *op++ = *ip++;\n              case 20: *op++ = *ip++; case 19: *op++ = *ip++; case 18: *op++ = *ip++; case 17: *op++ = *ip++;\n              case 16: *op++ = *ip++; case 15: *op++ = *ip++; case 14: *op++ = *ip++; case 13: *op++ = *ip++;\n              case 12: *op++ = *ip++; case 11: *op++ = *ip++; case 10: *op++ = *ip++; case  9: *op++ = *ip++;\n              case  8: *op++ = *ip++; case  7: *op++ = *ip++; case  6: *op++ = *ip++; case  5: *op++ = *ip++;\n              case  4: *op++ = *ip++; case  3: *op++ = *ip++; case  2: *op++ = *ip++; case  1: *op++ = *ip++;\n            }\n#endif\n        }\n      else /* back reference */\n        {\n          unsigned int len = ctrl >> 5;\n\n          u8 *ref = op - ((ctrl & 0x1f) << 8) - 1;\n\n#if CHECK_INPUT\n          if (ip >= in_end)\n            {\n              SET_ERRNO (EINVAL);\n              return 0;\n            }\n#endif\n          if (len == 7)\n            {\n              len += *ip++;\n#if CHECK_INPUT\n              if (ip >= in_end)\n                {\n                  SET_ERRNO (EINVAL);\n                  return 0;\n                }\n#endif\n            }\n\n          ref -= *ip++;\n\n          if (op + len + 2 > out_end)\n            {\n              SET_ERRNO (E2BIG);\n              return 0;\n            }\n\n          if (ref < (u8 *)out_data)\n            {\n              SET_ERRNO (EINVAL);\n              return 0;\n            }\n\n#ifdef lzf_movsb\n          len += 2;\n          lzf_movsb (op, ref, len);\n#else\n          switch (len)\n            {\n              default:\n                len += 2;\n\n                if (op >= ref + len)\n                  {\n                    /* disjunct areas */\n                    memcpy (op, ref, len);\n                    op += len;\n                  }\n                else\n                  {\n                    /* overlapping, use octte by octte copying */\n                    do\n                      *op++ = *ref++;\n                    while (--len);\n                  }\n\n                break;\n\n              case 9: *op++ = *ref++; /* fall-thru */\n              case 8: *op++ = *ref++; /* fall-thru */\n              case 7: *op++ = *ref++; /* fall-thru */\n              case 6: *op++ = *ref++; /* fall-thru */\n              case 5: *op++ = *ref++; /* fall-thru */\n              case 4: *op++ = *ref++; /* fall-thru */\n              case 3: *op++ = *ref++; /* fall-thru */\n              case 2: *op++ = *ref++; /* fall-thru */\n              case 1: *op++ = *ref++; /* fall-thru */\n              case 0: *op++ = *ref++; /* two octets more */\n                      *op++ = *ref++; /* fall-thru */\n            }\n#endif\n        }\n    }\n\n  return op - (u8 *)out_data;\n}\n#if defined(__GNUC__) && __GNUC__ >= 5\n#pragma GCC diagnostic pop\n#endif\n"
  },
  {
    "path": "src/meminfo.cpp",
    "content": "#include <string>\n#include <fstream>\n#include <limits>\n\nstatic size_t getMemKey(std::string key) {\n# ifdef __linux__\n    std::string token;\n    std::ifstream f(\"/proc/meminfo\");\n    while (f >> token) {\n        if (token == key) {\n            size_t mem_val;\n            if (f >> mem_val) {\n                return mem_val * 1024; // values are in kB\n            } else {\n                return 0;\n            }\n            f.ignore(std::numeric_limits<std::streamsize>::max(), '\\n');\n        }\n    }\n    return 0;\n# else\n    (void)key;\n    return 0;\n# endif\n}\n\nsize_t getMemAvailable() {\n    return getMemKey(\"MemAvailable:\");\n}\n\nsize_t getMemTotal() {\n    return getMemKey(\"MemTotal:\");\n}"
  },
  {
    "path": "src/memtest.c",
    "content": "/*\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n#include <stdint.h>\n#include <stdlib.h>\n#include <stdio.h>\n#include <string.h>\n#include <assert.h>\n#include <limits.h>\n#include <errno.h>\n#include <termios.h>\n#include <sys/ioctl.h>\n#if defined(__sun)\n#include <stropts.h>\n#endif\n#include \"config.h\"\n\n#if (ULONG_MAX == 4294967295UL)\n#define MEMTEST_32BIT\n#elif (ULONG_MAX == 18446744073709551615ULL)\n#define MEMTEST_64BIT\n#else\n#error \"ULONG_MAX value not supported.\"\n#endif\n\n#ifdef MEMTEST_32BIT\n#define ULONG_ONEZERO 0xaaaaaaaaUL\n#define ULONG_ZEROONE 0x55555555UL\n#else\n#define ULONG_ONEZERO 0xaaaaaaaaaaaaaaaaUL\n#define ULONG_ZEROONE 0x5555555555555555UL\n#endif\n\nstatic struct winsize ws;\nsize_t progress_printed; /* Printed chars in screen-wide progress bar. */\nsize_t progress_full; /* How many chars to write to fill the progress bar. */\n\nvoid memtest_progress_start(char *title, int pass) {\n    int j;\n\n    printf(\"\\x1b[H\\x1b[2J\");    /* Cursor home, clear screen. */\n    /* Fill with dots. */\n    for (j = 0; j < ws.ws_col*(ws.ws_row-2); j++) printf(\".\");\n    printf(\"Please keep the test running several minutes per GB of memory.\\n\");\n    printf(\"Also check http://www.memtest86.com/ and http://pyropus.ca/software/memtester/\");\n    printf(\"\\x1b[H\\x1b[2K\");          /* Cursor home, clear current line.  */\n    printf(\"%s [%d]\\n\", title, pass); /* Print title. */\n    progress_printed = 0;\n    progress_full = (size_t)ws.ws_col*(ws.ws_row-3);\n    fflush(stdout);\n}\n\nvoid memtest_progress_end(void) {\n    printf(\"\\x1b[H\\x1b[2J\");    /* Cursor home, clear screen. */\n}\n\nvoid memtest_progress_step(size_t curr, size_t size, char c) {\n    size_t chars = ((unsigned long long)curr*progress_full)/size, j;\n\n    for (j = 0; j < chars-progress_printed; j++) printf(\"%c\",c);\n    progress_printed = chars;\n    fflush(stdout);\n}\n\n/* Test that addressing is fine. Every location is populated with its own\n * address, and finally verified. This test is very fast but may detect\n * ASAP big issues with the memory subsystem. */\nint memtest_addressing(unsigned long *l, size_t bytes, int interactive) {\n    unsigned long words = bytes/sizeof(unsigned long);\n    unsigned long j, *p;\n\n    /* Fill */\n    p = l;\n    for (j = 0; j < words; j++) {\n        *p = (unsigned long)p;\n        p++;\n        if ((j & 0xffff) == 0 && interactive)\n            memtest_progress_step(j,words*2,'A');\n    }\n    /* Test */\n    p = l;\n    for (j = 0; j < words; j++) {\n        if (*p != (unsigned long)p) {\n            if (interactive) {\n                printf(\"\\n*** MEMORY ADDRESSING ERROR: %p contains %lu\\n\",\n                    (void*) p, *p);\n                exit(1);\n            }\n            return 1;\n        }\n        p++;\n        if ((j & 0xffff) == 0 && interactive)\n            memtest_progress_step(j+words,words*2,'A');\n    }\n    return 0;\n}\n\n/* Fill words stepping a single page at every write, so we continue to\n * touch all the pages in the smallest amount of time reducing the\n * effectiveness of caches, and making it hard for the OS to transfer\n * pages on the swap.\n *\n * In this test we can't call rand() since the system may be completely\n * unable to handle library calls, so we have to resort to our own\n * PRNG that only uses local state. We use an xorshift* PRNG. */\n#define xorshift64star_next() do { \\\n        rseed ^= rseed >> 12; \\\n        rseed ^= rseed << 25; \\\n        rseed ^= rseed >> 27; \\\n        rout = rseed * UINT64_C(2685821657736338717); \\\n} while(0)\n\nvoid memtest_fill_random(unsigned long *l, size_t bytes, int interactive) {\n    unsigned long step = 4096/sizeof(unsigned long);\n    unsigned long words = bytes/sizeof(unsigned long)/2;\n    unsigned long iwords = words/step;  /* words per iteration */\n    unsigned long off, w, *l1, *l2;\n    uint64_t rseed = UINT64_C(0xd13133de9afdb566); /* Just a random seed. */\n    uint64_t rout = 0;\n\n    assert((bytes & 4095) == 0);\n    for (off = 0; off < step; off++) {\n        l1 = l+off;\n        l2 = l1+words;\n        for (w = 0; w < iwords; w++) {\n            xorshift64star_next();\n            *l1 = *l2 = (unsigned long) rout;\n            l1 += step;\n            l2 += step;\n            if ((w & 0xffff) == 0 && interactive)\n                memtest_progress_step(w+iwords*off,words,'R');\n        }\n    }\n}\n\n/* Like memtest_fill_random() but uses the two specified values to fill\n * memory, in an alternated way (v1|v2|v1|v2|...) */\nvoid memtest_fill_value(unsigned long *l, size_t bytes, unsigned long v1,\n                        unsigned long v2, char sym, int interactive)\n{\n    unsigned long step = 4096/sizeof(unsigned long);\n    unsigned long words = bytes/sizeof(unsigned long)/2;\n    unsigned long iwords = words/step;  /* words per iteration */\n    unsigned long off, w, *l1, *l2, v;\n\n    assert((bytes & 4095) == 0);\n    for (off = 0; off < step; off++) {\n        l1 = l+off;\n        l2 = l1+words;\n        v = (off & 1) ? v2 : v1;\n        for (w = 0; w < iwords; w++) {\n#ifdef MEMTEST_32BIT\n            *l1 = *l2 = ((unsigned long)     v) |\n                        (((unsigned long)    v) << 16);\n#else\n            *l1 = *l2 = ((unsigned long)     v) |\n                        (((unsigned long)    v) << 16) |\n                        (((unsigned long)    v) << 32) |\n                        (((unsigned long)    v) << 48);\n#endif\n            l1 += step;\n            l2 += step;\n            if ((w & 0xffff) == 0 && interactive)\n                memtest_progress_step(w+iwords*off,words,sym);\n        }\n    }\n}\n\nint memtest_compare(unsigned long *l, size_t bytes, int interactive) {\n    unsigned long words = bytes/sizeof(unsigned long)/2;\n    unsigned long w, *l1, *l2;\n\n    assert((bytes & 4095) == 0);\n    l1 = l;\n    l2 = l1+words;\n    for (w = 0; w < words; w++) {\n        if (*l1 != *l2) {\n            if (interactive) {\n                printf(\"\\n*** MEMORY ERROR DETECTED: %p != %p (%lu vs %lu)\\n\",\n                    (void*)l1, (void*)l2, *l1, *l2);\n                exit(1);\n            }\n            return 1;\n        }\n        l1 ++;\n        l2 ++;\n        if ((w & 0xffff) == 0 && interactive)\n            memtest_progress_step(w,words,'=');\n    }\n    return 0;\n}\n\nint memtest_compare_times(unsigned long *m, size_t bytes, int pass, int times,\n                          int interactive)\n{\n    int j;\n    int errors = 0;\n\n    for (j = 0; j < times; j++) {\n        if (interactive) memtest_progress_start(\"Compare\",pass);\n        errors += memtest_compare(m,bytes,interactive);\n        if (interactive) memtest_progress_end();\n    }\n    return errors;\n}\n\n/* Test the specified memory. The number of bytes must be multiple of 4096.\n * If interactive is true the program exists with an error and prints\n * ASCII arts to show progresses. Instead when interactive is 0, it can\n * be used as an API call, and returns 1 if memory errors were found or\n * 0 if there were no errors detected. */\nint memtest_test(unsigned long *m, size_t bytes, int passes, int interactive) {\n    int pass = 0;\n    int errors = 0;\n\n    while (pass != passes) {\n        pass++;\n\n        if (interactive) memtest_progress_start(\"Addressing test\",pass);\n        errors += memtest_addressing(m,bytes,interactive);\n        if (interactive) memtest_progress_end();\n\n        if (interactive) memtest_progress_start(\"Random fill\",pass);\n        memtest_fill_random(m,bytes,interactive);\n        if (interactive) memtest_progress_end();\n        errors += memtest_compare_times(m,bytes,pass,4,interactive);\n\n        if (interactive) memtest_progress_start(\"Solid fill\",pass);\n        memtest_fill_value(m,bytes,0,(unsigned long)-1,'S',interactive);\n        if (interactive) memtest_progress_end();\n        errors += memtest_compare_times(m,bytes,pass,4,interactive);\n\n        if (interactive) memtest_progress_start(\"Checkerboard fill\",pass);\n        memtest_fill_value(m,bytes,ULONG_ONEZERO,ULONG_ZEROONE,'C',interactive);\n        if (interactive) memtest_progress_end();\n        errors += memtest_compare_times(m,bytes,pass,4,interactive);\n    }\n    return errors;\n}\n\n/* A version of memtest_test() that tests memory in small pieces\n * in order to restore the memory content at exit.\n *\n * One problem we have with this approach, is that the cache can avoid\n * real memory accesses, and we can't test big chunks of memory at the\n * same time, because we need to backup them on the stack (the allocator\n * may not be usable or we may be already in an out of memory condition).\n * So what we do is to try to trash the cache with useless memory accesses\n * between the fill and compare cycles. */\n#define MEMTEST_BACKUP_WORDS (1024*(1024/sizeof(long)))\n/* Random accesses of MEMTEST_DECACHE_SIZE are performed at the start and\n * end of the region between fill and compare cycles in order to trash\n * the cache. */\n#define MEMTEST_DECACHE_SIZE (1024*8)\nint memtest_preserving_test(unsigned long *m, size_t bytes, int passes) {\n    unsigned long backup[MEMTEST_BACKUP_WORDS];\n    unsigned long *p = m;\n    unsigned long *end = (unsigned long*) (((unsigned char*)m)+(bytes-MEMTEST_DECACHE_SIZE));\n    size_t left = bytes;\n    int errors = 0;\n\n    if (bytes & 4095) return 0; /* Can't test across 4k page boundaries. */\n    if (bytes < 4096*2) return 0; /* Can't test a single page. */\n\n    while(left) {\n        /* If we have to test a single final page, go back a single page\n         * so that we can test two pages, since the code can't test a single\n         * page but at least two. */\n        if (left == 4096) {\n            left += 4096;\n            p -= 4096/sizeof(unsigned long);\n        }\n\n        int pass = 0;\n        size_t len = (left > sizeof(backup)) ? sizeof(backup) : left;\n\n        /* Always test an even number of pages. */\n        if (len/4096 % 2) len -= 4096;\n\n        memcpy(backup,p,len); /* Backup. */\n        while(pass != passes) {\n            pass++;\n            errors += memtest_addressing(p,len,0);\n            memtest_fill_random(p,len,0);\n            if (bytes >= MEMTEST_DECACHE_SIZE) {\n                memtest_compare_times(m,MEMTEST_DECACHE_SIZE,pass,1,0);\n                memtest_compare_times(end,MEMTEST_DECACHE_SIZE,pass,1,0);\n            }\n            errors += memtest_compare_times(p,len,pass,4,0);\n            memtest_fill_value(p,len,0,(unsigned long)-1,'S',0);\n            if (bytes >= MEMTEST_DECACHE_SIZE) {\n                memtest_compare_times(m,MEMTEST_DECACHE_SIZE,pass,1,0);\n                memtest_compare_times(end,MEMTEST_DECACHE_SIZE,pass,1,0);\n            }\n            errors += memtest_compare_times(p,len,pass,4,0);\n            memtest_fill_value(p,len,ULONG_ONEZERO,ULONG_ZEROONE,'C',0);\n            if (bytes >= MEMTEST_DECACHE_SIZE) {\n                memtest_compare_times(m,MEMTEST_DECACHE_SIZE,pass,1,0);\n                memtest_compare_times(end,MEMTEST_DECACHE_SIZE,pass,1,0);\n            }\n            errors += memtest_compare_times(p,len,pass,4,0);\n        }\n        memcpy(p,backup,len); /* Restore. */\n        left -= len;\n        p += len/sizeof(unsigned long);\n    }\n    return errors;\n}\n\n/* Perform an interactive test allocating the specified number of megabytes. */\nvoid memtest_alloc_and_test(size_t megabytes, int passes) {\n    size_t bytes = megabytes*1024*1024;\n    unsigned long *m = malloc(bytes);\n\n    if (m == NULL) {\n        fprintf(stderr,\"Unable to allocate %zu megabytes: %s\",\n            megabytes, strerror(errno));\n        exit(1);\n    }\n    memtest_test(m,bytes,passes,1);\n    free(m);\n}\n\nvoid memtest(size_t megabytes, int passes) {\n#if !defined(__HAIKU__)\n    if (ioctl(1, TIOCGWINSZ, &ws) == -1) {\n        ws.ws_col = 80;\n        ws.ws_row = 20;\n    }\n#else\n    ws.ws_col = 80;\n    ws.ws_row = 20;\n#endif\n    memtest_alloc_and_test(megabytes,passes);\n    printf(\"\\nYour memory passed this test.\\n\");\n    printf(\"Please if you are still in doubt use the following two tools:\\n\");\n    printf(\"1) memtest86: http://www.memtest86.com/\\n\");\n    printf(\"2) memtester: http://pyropus.ca/software/memtester/\\n\");\n    exit(0);\n}\n"
  },
  {
    "path": "src/mkreleasehdr.sh",
    "content": "#!/bin/sh\nGIT_SHA1=`(git show-ref --head --hash=8 2> /dev/null || echo 00000000) | head -n1`\nGIT_DIRTY=`git diff --no-ext-diff 2> /dev/null | wc -l`\nBUILD_ID=`uname -n`\"-\"`date +%s`\nif [ -n \"$SOURCE_DATE_EPOCH\" ]; then\n  BUILD_ID=$(date -u -d \"@$SOURCE_DATE_EPOCH\" +%s 2>/dev/null || date -u -r \"$SOURCE_DATE_EPOCH\" +%s 2>/dev/null || date -u +%s)\nfi\ntest -f release.h || touch release.h\n(cat release.h | grep SHA1 | grep $GIT_SHA1) && \\\n(cat release.h | grep DIRTY | grep $GIT_DIRTY) && exit 0 # Already up-to-date\necho \"#define REDIS_GIT_SHA1 \\\"$GIT_SHA1\\\"\" > release.h\necho \"#define REDIS_GIT_DIRTY \\\"$GIT_DIRTY\\\"\" >> release.h\necho \"#define REDIS_BUILD_ID \\\"$BUILD_ID\\\"\" >> release.h\ntouch release.c # Force recompile of release.c\n"
  },
  {
    "path": "src/module.cpp",
    "content": "/*\n * Copyright (c) 2016, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n/* --------------------------------------------------------------------------\n * Modules API documentation information\n *\n * The comments in this file are used to generate the API documentation on the\n * Redis website.\n *\n * Each function starting with RM_ and preceded by a block comment is included\n * in the API documentation. To hide an RM_ function, put a blank line between\n * the comment and the function definition or put the comment inside the\n * function body.\n *\n * The functions are divided into sections. Each section is preceded by a\n * documentation block, which is comment block starting with a markdown level 2\n * heading, i.e. a line starting with ##, on the first line of the comment block\n * (with the exception of a ----- line which can appear first). Other comment\n * blocks, which are not intended for the modules API user, such as this comment\n * block, do NOT start with a markdown level 2 heading, so they are included in\n * the generated a API documentation.\n *\n * The documentation comments may contain markdown formatting. Some automatic\n * replacements are done, such as the replacement of RM with RedisModule in\n * function names. For details, see the script src/modules/gendoc.rb.\n * -------------------------------------------------------------------------- */\n\n#include \"server.h\"\n#include \"cluster.h\"\n#include \"slowlog.h\"\n#include \"rdb.h\"\n#include \"aelocker.h\"\n#include \"monotonic.h\"\n#include <dlfcn.h>\n#include <mutex>\n#include <condition_variable>\n#include <sys/stat.h>\n#include <sys/wait.h>\n\n#define REDISMODULE_CORE 1\n#include \"redismodule.h\"\n\n/* --------------------------------------------------------------------------\n * Private data structures used by the modules system. Those are data\n * structures that are never exposed to Redis Modules, if not as void\n * pointers that have an API the module can call with them)\n * -------------------------------------------------------------------------- */\n\ntypedef struct RedisModuleInfoCtx {\n    struct RedisModule *module;\n    const char *requested_section;\n    sds info;           /* info string we collected so far */\n    int sections;       /* number of sections we collected so far */\n    int in_section;     /* indication if we're in an active section or not */\n    int in_dict_field;  /* indication that we're currently appending to a dict */\n} RedisModuleInfoCtx;\n\ntypedef void (*RedisModuleInfoFunc)(RedisModuleInfoCtx *ctx, int for_crash_report);\ntypedef void (*RedisModuleDefragFunc)(struct RedisModuleDefragCtx *ctx);\n\n/* This structure represents a module inside the system. */\nstruct RedisModule {\n    void *handle;   /* Module dlopen() handle. */\n    char *name;     /* Module name. */\n    int ver;        /* Module version. We use just progressive integers. */\n    int apiver;     /* Module API version as requested during initialization.*/\n    list *types;    /* Module data types. */\n    list *usedby;   /* List of modules using APIs from this one. */\n    list *usingMods;    /* List of modules we use some APIs of. */\n    list *filters;  /* List of filters the module has registered. */\n    int in_call;    /* RM_Call() nesting level */\n    int in_hook;    /* Hooks callback nesting level for this module (0 or 1). */\n    int options;    /* Module options and capabilities. */\n    int blocked_clients;         /* Count of RedisModuleBlockedClient in this module. */\n    RedisModuleInfoFunc info_cb; /* Callback for module to add INFO fields. */\n    RedisModuleDefragFunc defrag_cb;    /* Callback for global data defrag. */\n};\ntypedef struct RedisModule RedisModule;\n\n/* This represents a shared API. Shared APIs will be used to populate\n * the g_pserver->sharedapi dictionary, mapping names of APIs exported by\n * modules for other modules to use, to their structure specifying the\n * function pointer that can be called. */\nstruct RedisModuleSharedAPI {\n    void *func;\n    RedisModule *module;\n};\ntypedef struct RedisModuleSharedAPI RedisModuleSharedAPI;\n\nstatic dict *modules; /* Hash table of modules. SDS -> RedisModule ptr.*/\n\n/* Entries in the context->amqueue array, representing objects to free\n * when the callback returns. */\nstruct AutoMemEntry {\n    void *ptr;\n    int type;\n};\n\n/* AutMemEntry type field values. */\n#define REDISMODULE_AM_KEY 0\n#define REDISMODULE_AM_STRING 1\n#define REDISMODULE_AM_REPLY 2\n#define REDISMODULE_AM_FREED 3 /* Explicitly freed by user already. */\n#define REDISMODULE_AM_DICT 4\n#define REDISMODULE_AM_INFO 5\n\n/* The pool allocator block. Redis Modules can allocate memory via this special\n * allocator that will automatically release it all once the callback returns.\n * This means that it can only be used for ephemeral allocations. However\n * there are two advantages for modules to use this API:\n *\n * 1) The memory is automatically released when the callback returns.\n * 2) This allocator is faster for many small allocations since whole blocks\n *    are allocated, and small pieces returned to the caller just advancing\n *    the index of the allocation.\n *\n * Allocations are always rounded to the size of the void pointer in order\n * to always return aligned memory chunks. */\n\n#define REDISMODULE_POOL_ALLOC_MIN_SIZE (1024*8)\n#define REDISMODULE_POOL_ALLOC_ALIGN (sizeof(void*))\n\ntypedef struct RedisModulePoolAllocBlock {\n    uint32_t size;\n    uint32_t used;\n    struct RedisModulePoolAllocBlock *next;\n    char *memory() {\n        return reinterpret_cast<char*>(this+1);\n    };\n} RedisModulePoolAllocBlock;\n\n/* This structure represents the context in which Redis modules operate.\n * Most APIs module can access, get a pointer to the context, so that the API\n * implementation can hold state across calls, or remember what to free after\n * the call and so forth.\n *\n * Note that not all the context structure is always filled with actual values\n * but only the fields needed in a given context. */\n\nstruct RedisModuleBlockedClient;\n\nstruct RedisModuleCtx {\n    void *getapifuncptr;            /* NOTE: Must be the first field. */\n    struct RedisModule *module;     /* Module reference. */\n    ::client *client;                 /* Client calling a command. */\n    struct RedisModuleBlockedClient *blocked_client; /* Blocked client for\n                                                        thread safe context. */\n    struct AutoMemEntry *amqueue;   /* Auto memory queue of objects to free. */\n    int amqueue_len;                /* Number of slots in amqueue. */\n    int amqueue_used;               /* Number of used slots in amqueue. */\n    int flags;                      /* REDISMODULE_CTX_... flags. */\n    void **postponed_arrays;        /* To set with RM_ReplySetArrayLength(). */\n    int postponed_arrays_count;     /* Number of entries in postponed_arrays. */\n    void *blocked_privdata;         /* Privdata set when unblocking a client. */\n    RedisModuleString *blocked_ready_key; /* Key ready when the reply callback\n                                             gets called for clients blocked\n                                             on keys. */\n\n    /* Used if there is the REDISMODULE_CTX_KEYS_POS_REQUEST flag set. */\n    getKeysResult *keys_result;\n\n    struct RedisModulePoolAllocBlock *pa_head;\n    redisOpArray saved_oparray;    /* When propagating commands in a callback\n                                      we reallocate the \"also propagate\" op\n                                      array. Here we save the old one to\n                                      restore it later. */\n};\ntypedef struct RedisModuleCtx RedisModuleCtx;\n\n#define REDISMODULE_CTX_INIT {(void*)(unsigned long)&RM_GetApi, NULL, NULL, NULL, NULL, 0, 0, 0, NULL, 0, NULL, NULL, NULL, NULL, {0}}\n#define REDISMODULE_CTX_AUTO_MEMORY (1<<0)\n#define REDISMODULE_CTX_KEYS_POS_REQUEST (1<<1)\n#define REDISMODULE_CTX_BLOCKED_REPLY (1<<2)\n#define REDISMODULE_CTX_BLOCKED_TIMEOUT (1<<3)\n#define REDISMODULE_CTX_THREAD_SAFE (1<<4)\n#define REDISMODULE_CTX_BLOCKED_DISCONNECTED (1<<5)\n#define REDISMODULE_CTX_MODULE_COMMAND_CALL (1<<6)\n#define REDISMODULE_CTX_MULTI_EMITTED (1<<7)\n\n/* This represents a Redis key opened with RM_OpenKey(). */\nstruct RedisModuleKey {\n    RedisModuleCtx *ctx;\n    redisDb *db;\n    robj *key;      /* Key name object. */\n    robj *value;    /* Value object, or NULL if the key was not found. */\n    void *iter;     /* Iterator. */\n    int mode;       /* Opening mode. */\n\n    union {\n        struct {\n            /* Zset iterator, use only if value->type == OBJ_ZSET */\n            uint32_t type;         /* REDISMODULE_ZSET_RANGE_* */\n            zrangespec rs;         /* Score range. */\n            zlexrangespec lrs;     /* Lex range. */\n            uint32_t start;        /* Start pos for positional ranges. */\n            uint32_t end;          /* End pos for positional ranges. */\n            void *current;         /* Zset iterator current node. */\n            int er;                /* Zset iterator end reached flag\n                                       (true if end was reached). */\n        } zset;\n        struct {\n            /* Stream, use only if value->type == OBJ_STREAM */\n            streamID currentid;    /* Current entry while iterating. */\n            int64_t numfieldsleft; /* Fields left to fetch for current entry. */\n            int signalready;       /* Flag that signalKeyAsReady() is needed. */\n        } stream;\n    } u;\n};\ntypedef struct RedisModuleKey RedisModuleKey;\n\n/* RedisModuleKey 'ztype' values. */\n#define REDISMODULE_ZSET_RANGE_NONE 0       /* This must always be 0. */\n#define REDISMODULE_ZSET_RANGE_LEX 1\n#define REDISMODULE_ZSET_RANGE_SCORE 2\n#define REDISMODULE_ZSET_RANGE_POS 3\n\n/* Function pointer type of a function representing a command inside\n * a Redis module. */\nstruct RedisModuleBlockedClient;\ntypedef int (*RedisModuleCmdFunc) (RedisModuleCtx *ctx, void **argv, int argc);\ntypedef void (*RedisModuleDisconnectFunc) (RedisModuleCtx *ctx, struct RedisModuleBlockedClient *bc);\n\n/* This struct holds the information about a command registered by a module.*/\nstruct RedisModuleCommandProxy {\n    struct RedisModule *module;\n    RedisModuleCmdFunc func;\n    struct redisCommand *rediscmd;\n};\ntypedef struct RedisModuleCommandProxy RedisModuleCommandProxy;\n\n#define REDISMODULE_REPLYFLAG_NONE 0\n#define REDISMODULE_REPLYFLAG_TOPARSE (1<<0) /* Protocol must be parsed. */\n#define REDISMODULE_REPLYFLAG_NESTED (1<<1)  /* Nested reply object. No proto\n                                                or struct free. */\n\n/* Reply of RM_Call() function. The function is filled in a lazy\n * way depending on the function called on the reply structure. By default\n * only the type, proto and protolen are filled. */\ntypedef struct RedisModuleCallReply {\n    RedisModuleCtx *ctx;\n    int type;       /* REDISMODULE_REPLY_... */\n    int flags;      /* REDISMODULE_REPLYFLAG_...  */\n    size_t len;     /* Len of strings or num of elements of arrays. */\n    char *proto;    /* Raw reply protocol. An SDS string at top-level object. */\n    size_t protolen;/* Length of protocol. */\n    union {\n        const char *str; /* String pointer for string and error replies. This\n                            does not need to be freed, always points inside\n                            a reply->proto buffer of the reply object or, in\n                            case of array elements, of parent reply objects. */\n        long long ll;    /* Reply value for integer reply. */\n        struct RedisModuleCallReply *array; /* Array of sub-reply elements. */\n    } val;\n} RedisModuleCallReply;\n\n/* Structure representing a blocked client. We get a pointer to such\n * an object when blocking from modules. */\ntypedef struct RedisModuleBlockedClient {\n    ::client *client;  /* Pointer to the blocked client. or NULL if the client\n                        was destroyed during the life of this object. */\n    RedisModule *module;    /* Module blocking the client. */\n    RedisModuleCmdFunc reply_callback; /* Reply callback on normal completion.*/\n    RedisModuleCmdFunc timeout_callback; /* Reply callback on timeout. */\n    RedisModuleDisconnectFunc disconnect_callback; /* Called on disconnection.*/\n    void (*free_privdata)(RedisModuleCtx*,void*);/* privdata cleanup callback.*/\n    void *privdata;     /* Module private data that may be used by the reply\n                           or timeout callback. It is set via the\n                           RedisModule_UnblockClient() API. */\n    ::client *reply_client;           /* Fake client used to accumulate replies\n                                       in thread safe contexts. */\n    int dbid;           /* Database number selected by the original client. */\n    int blocked_on_keys;    /* If blocked via RM_BlockClientOnKeys(). */\n    int unblocked;          /* Already on the moduleUnblocked list. */\n    monotime background_timer; /* Timer tracking the start of background work */\n    uint64_t background_duration; /* Current command background time duration.\n                                     Used for measuring latency of blocking cmds */\n} RedisModuleBlockedClient;\n\nstatic pthread_mutex_t moduleUnblockedClientsMutex = PTHREAD_MUTEX_INITIALIZER;\nstatic list *moduleUnblockedClients;\n\n/* We need a mutex that is unlocked / relocked in beforeSleep() in order to\n * allow thread safe contexts to execute commands at a safe moment. */\nint fModuleGILWlocked = FALSE;\n\n/* Function pointer type for keyspace event notification subscriptions from modules. */\ntypedef int (*RedisModuleNotificationFunc) (RedisModuleCtx *ctx, int type, const char *event, RedisModuleString *key);\n\n/* Keyspace notification subscriber information.\n * See RM_SubscribeToKeyspaceEvents() for more information. */\ntypedef struct RedisModuleKeyspaceSubscriber {\n    /* The module subscribed to the event */\n    RedisModule *module;\n    /* Notification callback in the module*/\n    RedisModuleNotificationFunc notify_callback;\n    /* A bit mask of the events the module is interested in */\n    int event_mask;\n    /* Active flag set on entry, to avoid reentrant subscribers\n     * calling themselves */\n    int active;\n} RedisModuleKeyspaceSubscriber;\n\n/* The module keyspace notification subscribers list */\nstatic list *moduleKeyspaceSubscribers;\n\n/* Static client recycled for when we need to provide a context with a client\n * in a situation where there is no client to provide. This avoids allocating\n * a new client per round. For instance this is used in the keyspace\n * notifications, timers and cluster messages callbacks. */\nstatic client *moduleFreeContextReusedClient;\n\n/* Data structures related to the exported dictionary data structure. */\ntypedef struct RedisModuleDict {\n    ::rax *rax;                       /* The radix tree. */\n} RedisModuleDict;\n\ntypedef struct RedisModuleDictIter {\n    RedisModuleDict *dict;\n    raxIterator ri;\n} RedisModuleDictIter;\n\ntypedef struct RedisModuleCommandFilterCtx {\n    RedisModuleString **argv;\n    int argc;\n} RedisModuleCommandFilterCtx;\n\ntypedef void (*RedisModuleCommandFilterFunc) (RedisModuleCommandFilterCtx *filter);\n\ntypedef struct RedisModuleCommandFilter {\n    /* The module that registered the filter */\n    RedisModule *module;\n    /* Filter callback function */\n    RedisModuleCommandFilterFunc callback;\n    /* REDISMODULE_CMDFILTER_* flags */\n    int flags;\n} RedisModuleCommandFilter;\n\n/* Registered filters */\nstatic list *moduleCommandFilters;\n\n/* Module GIL Variables */\nstatic readWriteLock s_moduleGIL(\"Module GIL\");\nthread_local bool g_fModuleThread = false;\n\ntypedef void (*RedisModuleForkDoneHandler) (int exitcode, int bysignal, void *user_data);\n\nstatic struct RedisModuleForkInfo {\n    RedisModuleForkDoneHandler done_handler;\n    void* done_handler_user_data;\n} moduleForkInfo = {0};\n\ntypedef struct RedisModuleServerInfoData {\n    ::rax *rax;                       /* parsed info data. */\n} RedisModuleServerInfoData;\n\n/* Flags for moduleCreateArgvFromUserFormat(). */\n#define REDISMODULE_ARGV_REPLICATE (1<<0)\n#define REDISMODULE_ARGV_NO_AOF (1<<1)\n#define REDISMODULE_ARGV_NO_REPLICAS (1<<2)\n\n/* Determine whether Redis should signalModifiedKey implicitly.\n * In case 'ctx' has no 'module' member (and therefore no module->options),\n * we assume default behavior, that is, Redis signals.\n * (see RM_GetThreadSafeContext) */\n#define SHOULD_SIGNAL_MODIFIED_KEYS(ctx) \\\n    ctx->module? !(ctx->module->options & REDISMODULE_OPTION_NO_IMPLICIT_SIGNAL_MODIFIED) : 1\n\n/* Server events hooks data structures and defines: this modules API\n * allow modules to subscribe to certain events in Redis, such as\n * the start and end of an RDB or AOF save, the change of role in replication,\n * and similar other events. */\n\ntypedef struct RedisModuleEventListener {\n    RedisModule *module;\n    RedisModuleEvent event;\n    RedisModuleEventCallback callback;\n} RedisModuleEventListener;\n\nlist *RedisModule_EventListeners; /* Global list of all the active events. */\nunsigned long long ModulesInHooks = 0; /* Total number of modules in hooks\n                                          callbacks right now. */\n\n/* Data structures related to the redis module users */\n\n/* This is the object returned by RM_CreateModuleUser(). The module API is\n * able to create users, set ACLs to such users, and later authenticate\n * clients using such newly created users. */\ntypedef struct RedisModuleUser {\n    ::user *user; /* Reference to the real redis user */\n} RedisModuleUser;\n\n\n/* --------------------------------------------------------------------------\n * Prototypes\n * -------------------------------------------------------------------------- */\n\nvoid RM_FreeCallReply(RedisModuleCallReply *reply);\nvoid RM_CloseKey(RedisModuleKey *key);\nvoid autoMemoryCollect(RedisModuleCtx *ctx);\nrobj **moduleCreateArgvFromUserFormat(const char *cmdname, const char *fmt, int *argcp, int *flags, va_list ap);\nvoid moduleReplicateMultiIfNeeded(RedisModuleCtx *ctx);\nvoid RM_ZsetRangeStop(RedisModuleKey *kp);\nstatic void zsetKeyReset(RedisModuleKey *key);\nstatic void moduleInitKeyTypeSpecific(RedisModuleKey *key);\nvoid RM_FreeDict(RedisModuleCtx *ctx, RedisModuleDict *d);\nvoid RM_FreeServerInfo(RedisModuleCtx *ctx, RedisModuleServerInfoData *data);\n\n/* --------------------------------------------------------------------------\n * ## Heap allocation raw functions\n *\n * Memory allocated with these functions are taken into account by Redis key\n * eviction algorithms and are reported in Redis memory usage information.\n * -------------------------------------------------------------------------- */\n\n/* Use like malloc(). Memory allocated with this function is reported in\n * Redis INFO memory, used for keys eviction according to maxmemory settings\n * and in general is taken into account as memory allocated by Redis.\n * You should avoid using malloc(). */\nvoid *RM_Alloc(size_t bytes) {\n    return zmalloc(bytes, MALLOC_SHARED);\n}\n\n/* Use like calloc(). Memory allocated with this function is reported in\n * Redis INFO memory, used for keys eviction according to maxmemory settings\n * and in general is taken into account as memory allocated by Redis.\n * You should avoid using calloc() directly. */\nvoid *RM_Calloc(size_t nmemb, size_t size) {\n    return zcalloc(nmemb*size, MALLOC_SHARED);\n}\n\n/* Use like realloc() for memory obtained with RedisModule_Alloc(). */\nvoid* RM_Realloc(void *ptr, size_t bytes) {\n    return zrealloc(ptr,bytes, MALLOC_SHARED);\n}\n\n/* Use like free() for memory obtained by RedisModule_Alloc() and\n * RedisModule_Realloc(). However you should never try to free with\n * RedisModule_Free() memory allocated with malloc() inside your module. */\nvoid RM_Free(void *ptr) {\n    zfree(ptr);\n}\n\n/* Like strdup() but returns memory allocated with RedisModule_Alloc(). */\nchar *RM_Strdup(const char *str) {\n    return zstrdup(str);\n}\n\n/* --------------------------------------------------------------------------\n * Pool allocator\n * -------------------------------------------------------------------------- */\n\n/* Release the chain of blocks used for pool allocations. */\nvoid poolAllocRelease(RedisModuleCtx *ctx) {\n    RedisModulePoolAllocBlock *head = ctx->pa_head, *next;\n\n    while(head != NULL) {\n        next = head->next;\n        zfree(head);\n        head = next;\n    }\n    ctx->pa_head = NULL;\n}\n\n/* Return heap allocated memory that will be freed automatically when the\n * module callback function returns. Mostly suitable for small allocations\n * that are short living and must be released when the callback returns\n * anyway. The returned memory is aligned to the architecture word size\n * if at least word size bytes are requested, otherwise it is just\n * aligned to the next power of two, so for example a 3 bytes request is\n * 4 bytes aligned while a 2 bytes request is 2 bytes aligned.\n *\n * There is no realloc style function since when this is needed to use the\n * pool allocator is not a good idea.\n *\n * The function returns NULL if `bytes` is 0. */\nvoid *RM_PoolAlloc(RedisModuleCtx *ctx, size_t bytes) {\n    if (bytes == 0) return NULL;\n    RedisModulePoolAllocBlock *b = ctx->pa_head;\n    size_t left = b ? b->size - b->used : 0;\n\n    /* Fix alignment. */\n    if (left >= bytes) {\n        size_t alignment = REDISMODULE_POOL_ALLOC_ALIGN;\n        while (bytes < alignment && alignment/2 >= bytes) alignment /= 2;\n        if (b->used % alignment)\n            b->used += alignment - (b->used % alignment);\n        left = (b->used > b->size) ? 0 : b->size - b->used;\n    }\n\n    /* Create a new block if needed. */\n    if (left < bytes) {\n        size_t blocksize = REDISMODULE_POOL_ALLOC_MIN_SIZE;\n        if (blocksize < bytes) blocksize = bytes;\n        b = (RedisModulePoolAllocBlock*)zmalloc(sizeof(*b) + blocksize, MALLOC_LOCAL);\n        b->size = blocksize;\n        b->used = 0;\n        b->next = ctx->pa_head;\n        ctx->pa_head = b;\n    }\n\n    char *retval = b->memory() + b->used;\n    b->used += bytes;\n    return retval;\n}\n\n/* --------------------------------------------------------------------------\n * Helpers for modules API implementation\n * -------------------------------------------------------------------------- */\n\n/* Create an empty key of the specified type. `key` must point to a key object\n * opened for writing where the `.value` member is set to NULL because the\n * key was found to be non existing.\n *\n * On success REDISMODULE_OK is returned and the key is populated with\n * the value of the specified type. The function fails and returns\n * REDISMODULE_ERR if:\n *\n * 1. The key is not open for writing.\n * 2. The key is not empty.\n * 3. The specified type is unknown.\n */\nint moduleCreateEmptyKey(RedisModuleKey *key, int type) {\n    robj *obj;\n\n    /* The key must be open for writing and non existing to proceed. */\n    if (!(key->mode & REDISMODULE_WRITE) || key->value)\n        return REDISMODULE_ERR;\n\n    switch(type) {\n    case REDISMODULE_KEYTYPE_LIST:\n        obj = createQuicklistObject();\n        quicklistSetOptions((quicklist*)obj->m_ptr, g_pserver->list_max_ziplist_size,\n                            g_pserver->list_compress_depth);\n        break;\n    case REDISMODULE_KEYTYPE_ZSET:\n        obj = createZsetZiplistObject();\n        break;\n    case REDISMODULE_KEYTYPE_HASH:\n        obj = createHashObject();\n        break;\n    case REDISMODULE_KEYTYPE_STREAM:\n        obj = createStreamObject();\n        break;\n    default: return REDISMODULE_ERR;\n    }\n    dbAdd(key->db,key->key,obj);\n    key->value = obj;\n    moduleInitKeyTypeSpecific(key);\n    return REDISMODULE_OK;\n}\n\n/* This function is called in low-level API implementation functions in order\n * to check if the value associated with the key remained empty after an\n * operation that removed elements from an aggregate data type.\n *\n * If this happens, the key is deleted from the DB and the key object state\n * is set to the right one in order to be targeted again by write operations\n * possibly recreating the key if needed.\n *\n * The function returns 1 if the key value object is found empty and is\n * deleted, otherwise 0 is returned. */\nint moduleDelKeyIfEmpty(RedisModuleKey *key) {\n    if (!(key->mode & REDISMODULE_WRITE) || key->value == NULL) return 0;\n    int isempty;\n    robj *o = key->value;\n\n    switch(o->type) {\n    case OBJ_LIST: isempty = listTypeLength(o) == 0; break;\n    case OBJ_SET: isempty = setTypeSize(o) == 0; break;\n    case OBJ_ZSET: isempty = zsetLength(o) == 0; break;\n    case OBJ_HASH: isempty = hashTypeLength(o) == 0; break;\n    case OBJ_STREAM: isempty = streamLength(o) == 0; break;\n    default: isempty = 0;\n    }\n\n    if (isempty) {\n        dbDelete(key->db,key->key);\n        key->value = NULL;\n        return 1;\n    } else {\n        return 0;\n    }\n}\n\n/* This function is used to set the thread local variables (serverTL) for \n * arbitrary module threads. All incoming module threads share the same set of \n * thread local variables (modulethreadvar).\n *\n * This is needed as some KeyDB functions use thread local variables to do things,\n * and we don't want to share the thread local variables of existing server threads */\nvoid moduleSetThreadVariablesIfNeeded(void) {\n    if (serverTL == nullptr) {\n        serverTL = &g_pserver->modulethreadvar; \n        g_fModuleThread = true;\n    }\n}\n\n/* --------------------------------------------------------------------------\n * Service API exported to modules\n *\n * Note that all the exported APIs are called RM_<funcname> in the core\n * and RedisModule_<funcname> in the module side (defined as function\n * pointers in redismodule.h). In this way the dynamic linker does not\n * mess with our global function pointers, overriding it with the symbols\n * defined in the main executable having the same names.\n * -------------------------------------------------------------------------- */\n\nint RM_GetApi(const char *funcname, void **targetPtrPtr) {\n    /* Lookup the requested module API and store the function pointer into the\n     * target pointer. The function returns REDISMODULE_ERR if there is no such\n     * named API, otherwise REDISMODULE_OK.\n     *\n     * This function is not meant to be used by modules developer, it is only\n     * used implicitly by including redismodule.h. */\n    dictEntry *he = dictFind(g_pserver->moduleapi, funcname);\n    if (!he) return REDISMODULE_ERR;\n    *targetPtrPtr = dictGetVal(he);\n    return REDISMODULE_OK;\n}\n\n/* Helper function for when a command callback is called, in order to handle\n * details needed to correctly replicate commands. */\nvoid moduleHandlePropagationAfterCommandCallback(RedisModuleCtx *ctx) {\n    serverAssert(GlobalLocksAcquired());\n    client *c = ctx->client;\n\n    /* We don't need to do anything here if the context was never used\n     * in order to propagate commands. */\n    if (!(ctx->flags & REDISMODULE_CTX_MULTI_EMITTED)) return;\n\n    /* We don't need to do anything here if the server isn't inside\n     * a transaction. */\n    if (!serverTL->propagate_in_transaction) return;\n\n    /* If this command is executed from with Lua or MULTI/EXEC we do not\n     * need to propagate EXEC */\n    if (serverTL->in_eval || serverTL->in_exec) return;\n\n    /* Handle the replication of the final EXEC, since whatever a command\n     * emits is always wrapped around MULTI/EXEC. */\n    alsoPropagate(cserver.execCommand,c->db->id,&shared.exec,1,\n        PROPAGATE_AOF|PROPAGATE_REPL);\n    afterPropagateExec();\n\n    /* If this is not a module command context (but is instead a simple\n     * callback context), we have to handle directly the \"also propagate\"\n     * array and emit it. In a module command call this will be handled\n     * directly by call(). */\n    if (!(ctx->flags & REDISMODULE_CTX_MODULE_COMMAND_CALL) &&\n        g_pserver->also_propagate.numops)\n    {\n        for (int j = 0; j < g_pserver->also_propagate.numops; j++) {\n            redisOp *rop = &g_pserver->also_propagate.ops[j];\n            int target = rop->target;\n            if (target)\n                propagate(rop->cmd,rop->dbid,rop->argv,rop->argc,target);\n        }\n        redisOpArrayFree(&g_pserver->also_propagate);\n        /* Restore the previous oparray in case of nexted use of the API. */\n        g_pserver->also_propagate = ctx->saved_oparray;\n        /* We're done with saved_oparray, let's invalidate it. */\n        redisOpArrayInit(&ctx->saved_oparray);\n    }\n}\n\n/* Free the context after the user function was called. */\nvoid moduleFreeContext(RedisModuleCtx *ctx, bool propogate) {\n    if (propogate)\n        moduleHandlePropagationAfterCommandCallback(ctx);\n    autoMemoryCollect(ctx);\n    poolAllocRelease(ctx);\n    if (ctx->postponed_arrays) {\n        zfree(ctx->postponed_arrays);\n        ctx->postponed_arrays_count = 0;\n        serverLog(LL_WARNING,\n            \"API misuse detected in module %s: \"\n            \"RedisModule_ReplyWithArray(REDISMODULE_POSTPONED_ARRAY_LEN) \"\n            \"not matched by the same number of RedisModule_SetReplyArrayLen() \"\n            \"calls.\",\n            ctx->module->name);\n    }\n    if (ctx->flags & REDISMODULE_CTX_THREAD_SAFE) freeClient(ctx->client);\n}\n\n/* This Redis command binds the normal Redis command invocation with commands\n * exported by modules. */\nvoid RedisModuleCommandDispatcher(client *c) {\n    RedisModuleCommandProxy *cp = (RedisModuleCommandProxy*)(unsigned long)c->cmd->getkeys_proc;\n    RedisModuleCtx ctx = REDISMODULE_CTX_INIT;\n\n    ctx.flags |= REDISMODULE_CTX_MODULE_COMMAND_CALL;\n    ctx.module = cp->module;\n    ctx.client = c;\n    cp->func(&ctx,(void**)c->argv,c->argc);\n    moduleFreeContext(&ctx);\n\n    /* In some cases processMultibulkBuffer uses sdsMakeRoomFor to\n     * expand the query buffer, and in order to avoid a big object copy\n     * the query buffer SDS may be used directly as the SDS string backing\n     * the client argument vectors: sometimes this will result in the SDS\n     * string having unused space at the end. Later if a module takes ownership\n     * of the RedisString, such space will be wasted forever. Inside the\n     * Redis core this is not a problem because tryObjectEncoding() is called\n     * before storing strings in the key space. Here we need to do it\n     * for the module. */\n    for (int i = 0; i < c->argc; i++) {\n        /* Only do the work if the module took ownership of the object:\n         * in that case the refcount is no longer 1. */\n        if (c->argv[i]->getrefcount(std::memory_order_relaxed) > 1)\n            trimStringObjectIfNeeded(c->argv[i]);\n    }\n}\n\n/* This function returns the list of keys, with the same interface as the\n * 'getkeys' function of the native commands, for module commands that exported\n * the \"getkeys-api\" flag during the registration. This is done when the\n * list of keys are not at fixed positions, so that first/last/step cannot\n * be used.\n *\n * In order to accomplish its work, the module command is called, flagging\n * the context in a way that the command can recognize this is a special\n * \"get keys\" call by calling RedisModule_IsKeysPositionRequest(ctx). */\nint moduleGetCommandKeysViaAPI(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result) {\n    RedisModuleCommandProxy *cp = (RedisModuleCommandProxy*)(unsigned long)cmd->getkeys_proc;\n    RedisModuleCtx ctx = REDISMODULE_CTX_INIT;\n\n    ctx.module = cp->module;\n    ctx.client = NULL;\n    ctx.flags |= REDISMODULE_CTX_KEYS_POS_REQUEST;\n\n    /* Initialize getKeysResult */\n    getKeysPrepareResult(result, MAX_KEYS_BUFFER);\n    ctx.keys_result = result;\n\n    cp->func(&ctx,(void**)argv,argc);\n    /* We currently always use the array allocated by RM_KeyAtPos() and don't try\n     * to optimize for the pre-allocated buffer.\n     */\n    moduleFreeContext(&ctx);\n    return result->numkeys;\n}\n\n/* --------------------------------------------------------------------------\n * ## Commands API\n *\n * These functions are used to implement custom Redis commands.\n *\n * For examples, see https://redis.io/topics/modules-intro.\n * -------------------------------------------------------------------------- */\n\n/* Return non-zero if a module command, that was declared with the\n * flag \"getkeys-api\", is called in a special way to get the keys positions\n * and not to get executed. Otherwise zero is returned. */\nint RM_IsKeysPositionRequest(RedisModuleCtx *ctx) {\n    return (ctx->flags & REDISMODULE_CTX_KEYS_POS_REQUEST) != 0;\n}\n\n/* When a module command is called in order to obtain the position of\n * keys, since it was flagged as \"getkeys-api\" during the registration,\n * the command implementation checks for this special call using the\n * RedisModule_IsKeysPositionRequest() API and uses this function in\n * order to report keys, like in the following example:\n *\n *     if (RedisModule_IsKeysPositionRequest(ctx)) {\n *         RedisModule_KeyAtPos(ctx,1);\n *         RedisModule_KeyAtPos(ctx,2);\n *     }\n *\n *  Note: in the example below the get keys API would not be needed since\n *  keys are at fixed positions. This interface is only used for commands\n *  with a more complex structure. */\nvoid RM_KeyAtPos(RedisModuleCtx *ctx, int pos) {\n    if (!(ctx->flags & REDISMODULE_CTX_KEYS_POS_REQUEST) || !ctx->keys_result) return;\n    if (pos <= 0) return;\n\n    getKeysResult *res = ctx->keys_result;\n\n    /* Check overflow */\n    if (res->numkeys == res->size) {\n        int newsize = res->size + (res->size > 8192 ? 8192 : res->size);\n        getKeysPrepareResult(res, newsize);\n    }\n\n    res->keys[res->numkeys++] = pos;\n}\n\n/* Helper for RM_CreateCommand(). Turns a string representing command\n * flags into the command flags used by the Redis core.\n *\n * It returns the set of flags, or -1 if unknown flags are found. */\nint64_t commandFlagsFromString(char *s) {\n    int count, j;\n    int64_t flags = 0;\n    sds *tokens = sdssplitlen(s,strlen(s),\" \",1,&count);\n    for (j = 0; j < count; j++) {\n        char *t = tokens[j];\n        if (!strcasecmp(t,\"write\")) flags |= CMD_WRITE;\n        else if (!strcasecmp(t,\"readonly\")) flags |= CMD_READONLY;\n        else if (!strcasecmp(t,\"admin\")) flags |= CMD_ADMIN;\n        else if (!strcasecmp(t,\"deny-oom\")) flags |= CMD_DENYOOM;\n        else if (!strcasecmp(t,\"deny-script\")) flags |= CMD_NOSCRIPT;\n        else if (!strcasecmp(t,\"allow-loading\")) flags |= CMD_LOADING;\n        else if (!strcasecmp(t,\"pubsub\")) flags |= CMD_PUBSUB;\n        else if (!strcasecmp(t,\"random\")) flags |= CMD_RANDOM;\n        else if (!strcasecmp(t,\"allow-stale\")) flags |= CMD_STALE;\n        else if (!strcasecmp(t,\"no-monitor\")) flags |= CMD_SKIP_MONITOR;\n        else if (!strcasecmp(t,\"no-slowlog\")) flags |= CMD_SKIP_SLOWLOG;\n        else if (!strcasecmp(t,\"fast\")) flags |= CMD_FAST;\n        else if (!strcasecmp(t,\"no-auth\")) flags |= CMD_NO_AUTH;\n        else if (!strcasecmp(t,\"may-replicate\")) flags |= CMD_MAY_REPLICATE;\n        else if (!strcasecmp(t,\"getkeys-api\")) flags |= CMD_MODULE_GETKEYS;\n        else if (!strcasecmp(t,\"no-cluster\")) flags |= CMD_MODULE_NO_CLUSTER;\n        else if (!strcasecmp(t,\"async\")) flags |= CMD_ASYNC_OK;\n        else break;\n    }\n    sdsfreesplitres(tokens,count);\n    if (j != count) return -1; /* Some token not processed correctly. */\n    return flags;\n}\n\n/* Register a new command in the Redis server, that will be handled by\n * calling the function pointer 'func' using the RedisModule calling\n * convention. The function returns REDISMODULE_ERR if the specified command\n * name is already busy or a set of invalid flags were passed, otherwise\n * REDISMODULE_OK is returned and the new command is registered.\n *\n * This function must be called during the initialization of the module\n * inside the RedisModule_OnLoad() function. Calling this function outside\n * of the initialization function is not defined.\n *\n * The command function type is the following:\n *\n *      int MyCommand_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc);\n *\n * And is supposed to always return REDISMODULE_OK.\n *\n * The set of flags 'strflags' specify the behavior of the command, and should\n * be passed as a C string composed of space separated words, like for\n * example \"write deny-oom\". The set of flags are:\n *\n * * **\"write\"**:     The command may modify the data set (it may also read\n *                    from it).\n * * **\"readonly\"**:  The command returns data from keys but never writes.\n * * **\"admin\"**:     The command is an administrative command (may change\n *                    replication or perform similar tasks).\n * * **\"deny-oom\"**:  The command may use additional memory and should be\n *                    denied during out of memory conditions.\n * * **\"deny-script\"**:   Don't allow this command in Lua scripts.\n * * **\"allow-loading\"**: Allow this command while the server is loading data.\n *                        Only commands not interacting with the data set\n *                        should be allowed to run in this mode. If not sure\n *                        don't use this flag.\n * * **\"pubsub\"**:    The command publishes things on Pub/Sub channels.\n * * **\"random\"**:    The command may have different outputs even starting\n *                    from the same input arguments and key values.\n * * **\"allow-stale\"**: The command is allowed to run on slaves that don't\n *                      serve stale data. Don't use if you don't know what\n *                      this means.\n * * **\"no-monitor\"**: Don't propagate the command on monitor. Use this if\n *                     the command has sensible data among the arguments.\n * * **\"no-slowlog\"**: Don't log this command in the slowlog. Use this if\n *                     the command has sensible data among the arguments.\n * * **\"fast\"**:      The command time complexity is not greater\n *                    than O(log(N)) where N is the size of the collection or\n *                    anything else representing the normal scalability\n *                    issue with the command.\n * * **\"getkeys-api\"**: The command implements the interface to return\n *                      the arguments that are keys. Used when start/stop/step\n *                      is not enough because of the command syntax.\n * * **\"no-cluster\"**: The command should not register in Redis Cluster\n *                     since is not designed to work with it because, for\n *                     example, is unable to report the position of the\n *                     keys, programmatically creates key names, or any\n *                     other reason.\n * * **\"no-auth\"**:    This command can be run by an un-authenticated client.\n *                     Normally this is used by a command that is used\n *                     to authenticate a client. \n * * **\"may-replicate\"**: This command may generate replication traffic, even\n *                        though it's not a write command.  \n */\nint RM_CreateCommand(RedisModuleCtx *ctx, const char *name, RedisModuleCmdFunc cmdfunc, const char *strflags, int firstkey, int lastkey, int keystep) {\n    int64_t flags = strflags ? commandFlagsFromString((char*)strflags) : 0;\n    if (flags == -1) return REDISMODULE_ERR;\n    if ((flags & CMD_MODULE_NO_CLUSTER) && g_pserver->cluster_enabled)\n        return REDISMODULE_ERR;\n\n    struct redisCommand *rediscmd;\n    RedisModuleCommandProxy *cp;\n    sds cmdname = sdsnew(name);\n\n    /* Check if the command name is busy. */\n    if (lookupCommand(cmdname) != NULL) {\n        sdsfree(cmdname);\n        return REDISMODULE_ERR;\n    }\n\n    /* Create a command \"proxy\", which is a structure that is referenced\n     * in the command table, so that the generic command that works as\n     * binding between modules and Redis, can know what function to call\n     * and what the module is.\n     *\n     * Note that we use the Redis command table 'getkeys_proc' in order to\n     * pass a reference to the command proxy structure. */\n    cp = (RedisModuleCommandProxy*)zmalloc(sizeof(*cp), MALLOC_LOCAL);\n    cp->module = ctx->module;\n    cp->func = cmdfunc;\n    cp->rediscmd = (redisCommand*)zmalloc(sizeof(*rediscmd), MALLOC_LOCAL);\n    cp->rediscmd->name = cmdname;\n    cp->rediscmd->proc = RedisModuleCommandDispatcher;\n    cp->rediscmd->arity = -1;\n    cp->rediscmd->flags = flags | CMD_MODULE;\n    cp->rediscmd->getkeys_proc = (redisGetKeysProc*)(unsigned long)cp;\n    cp->rediscmd->firstkey = firstkey;\n    cp->rediscmd->lastkey = lastkey;\n    cp->rediscmd->keystep = keystep;\n    cp->rediscmd->microseconds = 0;\n    cp->rediscmd->calls = 0;\n    cp->rediscmd->rejected_calls = 0;\n    cp->rediscmd->failed_calls = 0;\n    dictAdd(g_pserver->commands,sdsdup(cmdname),cp->rediscmd);\n    dictAdd(g_pserver->orig_commands,sdsdup(cmdname),cp->rediscmd);\n    cp->rediscmd->id = ACLGetCommandID(cmdname); /* ID used for ACL. */\n    return REDISMODULE_OK;\n}\n\n/* --------------------------------------------------------------------------\n * ## Module information and time measurement\n * -------------------------------------------------------------------------- */\n\nvoid RM_SetModuleAttribs(RedisModuleCtx *ctx, const char *name, int ver, int apiver) {\n    /* Called by RM_Init() to setup the `ctx->module` structure.\n     *\n     * This is an internal function, Redis modules developers don't need\n     * to use it. */\n    RedisModule *module;\n\n    if (ctx->module != NULL) return;\n    module = (RedisModule*)zmalloc(sizeof(*module), MALLOC_LOCAL);\n    module->name = sdsnew((char*)name);\n    module->ver = ver;\n    module->apiver = apiver;\n    module->types = listCreate();\n    module->usedby = listCreate();\n    module->usingMods = listCreate();\n    module->filters = listCreate();\n    module->in_call = 0;\n    module->in_hook = 0;\n    module->options = 0;\n    module->info_cb = 0;\n    module->defrag_cb = 0;\n    ctx->module = module;\n}\n\n/* Return non-zero if the module name is busy.\n * Otherwise zero is returned. */\nint RM_IsModuleNameBusy(const char *name) {\n    sds modulename = sdsnew(name);\n    dictEntry *de = dictFind(modules,modulename);\n    sdsfree(modulename);\n    return de != NULL;\n}\n\n/* Return the current UNIX time in milliseconds. */\nlong long RM_Milliseconds(void) {\n    return mstime();\n}\n\n/* Mark a point in time that will be used as the start time to calculate\n * the elapsed execution time when RM_BlockedClientMeasureTimeEnd() is called.\n * Within the same command, you can call multiple times\n * RM_BlockedClientMeasureTimeStart() and RM_BlockedClientMeasureTimeEnd()\n * to accummulate indepedent time intervals to the background duration.\n * This method always return REDISMODULE_OK. */\nint RM_BlockedClientMeasureTimeStart(RedisModuleBlockedClient *bc) {\n    elapsedStart(&(bc->background_timer));\n    return REDISMODULE_OK;\n}\n\n/* Mark a point in time that will be used as the end time\n * to calculate the elapsed execution time.\n * On success REDISMODULE_OK is returned.\n * This method only returns REDISMODULE_ERR if no start time was\n * previously defined ( meaning RM_BlockedClientMeasureTimeStart was not called ). */\nint RM_BlockedClientMeasureTimeEnd(RedisModuleBlockedClient *bc) {\n    // If the counter is 0 then we haven't called RM_BlockedClientMeasureTimeStart\n    if (!bc->background_timer)\n        return REDISMODULE_ERR;\n    bc->background_duration += elapsedUs(bc->background_timer);\n    return REDISMODULE_OK;\n}\n\n/* Set flags defining capabilities or behavior bit flags.\n *\n * REDISMODULE_OPTIONS_HANDLE_IO_ERRORS:\n * Generally, modules don't need to bother with this, as the process will just\n * terminate if a read error happens, however, setting this flag would allow\n * repl-diskless-load to work if enabled.\n * The module should use RedisModule_IsIOError after reads, before using the\n * data that was read, and in case of error, propagate it upwards, and also be\n * able to release the partially populated value and all it's allocations.\n *\n * REDISMODULE_OPTION_NO_IMPLICIT_SIGNAL_MODIFIED:\n * See RM_SignalModifiedKey().\n */\nvoid RM_SetModuleOptions(RedisModuleCtx *ctx, int options) {\n    ctx->module->options = options;\n}\n\n/* Signals that the key is modified from user's perspective (i.e. invalidate WATCH\n * and client side caching).\n *\n * This is done automatically when a key opened for writing is closed, unless\n * the option REDISMODULE_OPTION_NO_IMPLICIT_SIGNAL_MODIFIED has been set using\n * RM_SetModuleOptions().\n*/\nint RM_SignalModifiedKey(RedisModuleCtx *ctx, RedisModuleString *keyname) {\n    signalModifiedKey(ctx->client,ctx->client->db,keyname);\n    return REDISMODULE_OK;\n}\n\n/* --------------------------------------------------------------------------\n * ## Automatic memory management for modules\n * -------------------------------------------------------------------------- */\n\n/* Enable automatic memory management.\n *\n * The function must be called as the first function of a command implementation\n * that wants to use automatic memory.\n *\n * When enabled, automatic memory management tracks and automatically frees\n * keys, call replies and Redis string objects once the command returns. In most\n * cases this eliminates the need of calling the following functions:\n *\n * 1. RedisModule_CloseKey()\n * 2. RedisModule_FreeCallReply()\n * 3. RedisModule_FreeString()\n *\n * These functions can still be used with automatic memory management enabled,\n * to optimize loops that make numerous allocations for example. */\nvoid RM_AutoMemory(RedisModuleCtx *ctx) {\n    ctx->flags |= REDISMODULE_CTX_AUTO_MEMORY;\n}\n\n/* Add a new object to release automatically when the callback returns. */\nvoid autoMemoryAdd(RedisModuleCtx *ctx, int type, void *ptr) {\n    if (!(ctx->flags & REDISMODULE_CTX_AUTO_MEMORY)) return;\n    if (ctx->amqueue_used == ctx->amqueue_len) {\n        ctx->amqueue_len *= 2;\n        if (ctx->amqueue_len < 16) ctx->amqueue_len = 16;\n        ctx->amqueue = (AutoMemEntry*)zrealloc(ctx->amqueue,sizeof(struct AutoMemEntry)*ctx->amqueue_len, MALLOC_LOCAL);\n    }\n    ctx->amqueue[ctx->amqueue_used].type = type;\n    ctx->amqueue[ctx->amqueue_used].ptr = ptr;\n    ctx->amqueue_used++;\n}\n\n/* Mark an object as freed in the auto release queue, so that users can still\n * free things manually if they want.\n *\n * The function returns 1 if the object was actually found in the auto memory\n * pool, otherwise 0 is returned. */\nint autoMemoryFreed(RedisModuleCtx *ctx, int type, void *ptr) {\n    if (!(ctx->flags & REDISMODULE_CTX_AUTO_MEMORY)) return 0;\n\n    int count = (ctx->amqueue_used+1)/2;\n    for (int j = 0; j < count; j++) {\n        for (int side = 0; side < 2; side++) {\n            /* For side = 0 check right side of the array, for\n             * side = 1 check the left side instead (zig-zag scanning). */\n            int i = (side == 0) ? (ctx->amqueue_used - 1 - j) : j;\n            if (ctx->amqueue[i].type == type &&\n                ctx->amqueue[i].ptr == ptr)\n            {\n                ctx->amqueue[i].type = REDISMODULE_AM_FREED;\n\n                /* Switch the freed element and the last element, to avoid growing\n                 * the queue unnecessarily if we allocate/free in a loop */\n                if (i != ctx->amqueue_used-1) {\n                    ctx->amqueue[i] = ctx->amqueue[ctx->amqueue_used-1];\n                }\n\n                /* Reduce the size of the queue because we either moved the top\n                 * element elsewhere or freed it */\n                ctx->amqueue_used--;\n                return 1;\n            }\n        }\n    }\n    return 0;\n}\n\n/* Release all the objects in queue. */\nvoid autoMemoryCollect(RedisModuleCtx *ctx) {\n    if (!(ctx->flags & REDISMODULE_CTX_AUTO_MEMORY)) return;\n    /* Clear the AUTO_MEMORY flag from the context, otherwise the functions\n     * we call to free the resources, will try to scan the auto release\n     * queue to mark the entries as freed. */\n    ctx->flags &= ~REDISMODULE_CTX_AUTO_MEMORY;\n    int j;\n    for (j = 0; j < ctx->amqueue_used; j++) {\n        void *ptr = (robj*)ctx->amqueue[j].ptr;\n        switch(ctx->amqueue[j].type) {\n        case REDISMODULE_AM_STRING: decrRefCount((robj*)ptr); break;\n        case REDISMODULE_AM_REPLY: RM_FreeCallReply((RedisModuleCallReply*)ptr); break;\n        case REDISMODULE_AM_KEY: RM_CloseKey((RedisModuleKey*)ptr); break;\n        case REDISMODULE_AM_DICT: RM_FreeDict(NULL,(RedisModuleDict*)ptr); break;\n        case REDISMODULE_AM_INFO: RM_FreeServerInfo(NULL,(RedisModuleServerInfoData*)ptr); break;\n        }\n    }\n    ctx->flags |= REDISMODULE_CTX_AUTO_MEMORY;\n    zfree(ctx->amqueue);\n    ctx->amqueue = NULL;\n    ctx->amqueue_len = 0;\n    ctx->amqueue_used = 0;\n}\n\n/* --------------------------------------------------------------------------\n * ## String objects APIs\n * -------------------------------------------------------------------------- */\n\n/* Create a new module string object. The returned string must be freed\n * with RedisModule_FreeString(), unless automatic memory is enabled.\n *\n * The string is created by copying the `len` bytes starting\n * at `ptr`. No reference is retained to the passed buffer.\n *\n * The module context 'ctx' is optional and may be NULL if you want to create\n * a string out of the context scope. However in that case, the automatic\n * memory management will not be available, and the string memory must be\n * managed manually. */\nRedisModuleString *RM_CreateString(RedisModuleCtx *ctx, const char *ptr, size_t len) {\n    RedisModuleString *o = createStringObject(ptr,len);\n    if (ctx != NULL) autoMemoryAdd(ctx,REDISMODULE_AM_STRING,o);\n    return o;\n}\n\n/* Create a new module string object from a printf format and arguments.\n * The returned string must be freed with RedisModule_FreeString(), unless\n * automatic memory is enabled.\n *\n * The string is created using the sds formatter function sdscatvprintf().\n *\n * The passed context 'ctx' may be NULL if necessary, see the\n * RedisModule_CreateString() documentation for more info. */\nRedisModuleString *RM_CreateStringPrintf(RedisModuleCtx *ctx, const char *fmt, ...) {\n    sds s = sdsempty();\n\n    va_list ap;\n    va_start(ap, fmt);\n    s = sdscatvprintf(s, fmt, ap);\n    va_end(ap);\n\n    RedisModuleString *o = createObject(OBJ_STRING, s);\n    if (ctx != NULL) autoMemoryAdd(ctx,REDISMODULE_AM_STRING,o);\n\n    return o;\n}\n\n\n/* Like RedisModule_CreatString(), but creates a string starting from a long long\n * integer instead of taking a buffer and its length.\n *\n * The returned string must be released with RedisModule_FreeString() or by\n * enabling automatic memory management.\n *\n * The passed context 'ctx' may be NULL if necessary, see the\n * RedisModule_CreateString() documentation for more info. */\nRedisModuleString *RM_CreateStringFromLongLong(RedisModuleCtx *ctx, long long ll) {\n    char buf[LONG_STR_SIZE];\n    size_t len = ll2string(buf,sizeof(buf),ll);\n    return RM_CreateString(ctx,buf,len);\n}\n\n/* Like RedisModule_CreatString(), but creates a string starting from a double\n * instead of taking a buffer and its length.\n *\n * The returned string must be released with RedisModule_FreeString() or by\n * enabling automatic memory management. */\nRedisModuleString *RM_CreateStringFromDouble(RedisModuleCtx *ctx, double d) {\n    char buf[128];\n    size_t len = d2string(buf,sizeof(buf),d);\n    return RM_CreateString(ctx,buf,len);\n}\n\n/* Like RedisModule_CreatString(), but creates a string starting from a long\n * double.\n *\n * The returned string must be released with RedisModule_FreeString() or by\n * enabling automatic memory management.\n *\n * The passed context 'ctx' may be NULL if necessary, see the\n * RedisModule_CreateString() documentation for more info. */\nRedisModuleString *RM_CreateStringFromLongDouble(RedisModuleCtx *ctx, long double ld, int humanfriendly) {\n    char buf[MAX_LONG_DOUBLE_CHARS];\n    size_t len = ld2string(buf,sizeof(buf),ld,\n        (humanfriendly ? LD_STR_HUMAN : LD_STR_AUTO));\n    return RM_CreateString(ctx,buf,len);\n}\n\n/* Like RedisModule_CreatString(), but creates a string starting from another\n * RedisModuleString.\n *\n * The returned string must be released with RedisModule_FreeString() or by\n * enabling automatic memory management.\n *\n * The passed context 'ctx' may be NULL if necessary, see the\n * RedisModule_CreateString() documentation for more info. */\nRedisModuleString *RM_CreateStringFromString(RedisModuleCtx *ctx, const RedisModuleString *str) {\n    RedisModuleString *o = dupStringObject(str);\n    if (ctx != NULL) autoMemoryAdd(ctx,REDISMODULE_AM_STRING,o);\n    return o;\n}\n\n/* Creates a string from a stream ID. The returned string must be released with\n * RedisModule_FreeString(), unless automatic memory is enabled.\n *\n * The passed context `ctx` may be NULL if necessary. See the\n * RedisModule_CreateString() documentation for more info. */\nRedisModuleString *RM_CreateStringFromStreamID(RedisModuleCtx *ctx, const RedisModuleStreamID *id) {\n    streamID streamid = {id->ms, id->seq};\n    RedisModuleString *o = createObjectFromStreamID(&streamid);\n    if (ctx != NULL) autoMemoryAdd(ctx, REDISMODULE_AM_STRING, o);\n    return o;\n}\n\n/* Free a module string object obtained with one of the Redis modules API calls\n * that return new string objects.\n *\n * It is possible to call this function even when automatic memory management\n * is enabled. In that case the string will be released ASAP and removed\n * from the pool of string to release at the end.\n *\n * If the string was created with a NULL context 'ctx', it is also possible to\n * pass ctx as NULL when releasing the string (but passing a context will not\n * create any issue). Strings created with a context should be freed also passing\n * the context, so if you want to free a string out of context later, make sure\n * to create it using a NULL context. */\nvoid RM_FreeString(RedisModuleCtx *ctx, RedisModuleString *str) {\n    decrRefCount(str);\n    if (ctx != NULL) autoMemoryFreed(ctx,REDISMODULE_AM_STRING,str);\n}\n\n/* Every call to this function, will make the string 'str' requiring\n * an additional call to RedisModule_FreeString() in order to really\n * free the string. Note that the automatic freeing of the string obtained\n * enabling modules automatic memory management counts for one\n * RedisModule_FreeString() call (it is just executed automatically).\n *\n * Normally you want to call this function when, at the same time\n * the following conditions are true:\n *\n * 1. You have automatic memory management enabled.\n * 2. You want to create string objects.\n * 3. Those string objects you create need to live *after* the callback\n *    function(for example a command implementation) creating them returns.\n *\n * Usually you want this in order to store the created string object\n * into your own data structure, for example when implementing a new data\n * type.\n *\n * Note that when memory management is turned off, you don't need\n * any call to RetainString() since creating a string will always result\n * into a string that lives after the callback function returns, if\n * no FreeString() call is performed.\n *\n * It is possible to call this function with a NULL context. */\nvoid RM_RetainString(RedisModuleCtx *ctx, RedisModuleString *str) {\n    if (ctx == NULL || !autoMemoryFreed(ctx,REDISMODULE_AM_STRING,str)) {\n        /* Increment the string reference counting only if we can't\n         * just remove the object from the list of objects that should\n         * be reclaimed. Why we do that, instead of just incrementing\n         * the refcount in any case, and let the automatic FreeString()\n         * call at the end to bring the refcount back at the desired\n         * value? Because this way we ensure that the object refcount\n         * value is 1 (instead of going to 2 to be dropped later to 1)\n         * after the call to this function. This is needed for functions\n         * like RedisModule_StringAppendBuffer() to work. */\n        incrRefCount(str);\n    }\n}\n\n/**\n* This function can be used instead of RedisModule_RetainString().\n* The main difference between the two is that this function will always\n* succeed, whereas RedisModule_RetainString() may fail because of an\n* assertion.\n* \n* The function returns a pointer to RedisModuleString, which is owned\n* by the caller. It requires a call to RedisModule_FreeString() to free\n* the string when automatic memory management is disabled for the context.\n* When automatic memory management is enabled, you can either call\n* RedisModule_FreeString() or let the automation free it.\n* \n* This function is more efficient than RedisModule_CreateStringFromString()\n* because whenever possible, it avoids copying the underlying\n* RedisModuleString. The disadvantage of using this function is that it\n* might not be possible to use RedisModule_StringAppendBuffer() on the\n* returned RedisModuleString.\n* \n* It is possible to call this function with a NULL context.\n*/\nRedisModuleString* RM_HoldString(RedisModuleCtx *ctx, RedisModuleString *str) {\n    if (str->getrefcount(std::memory_order_relaxed) == OBJ_STATIC_REFCOUNT) {\n        return RM_CreateStringFromString(ctx, str);\n    }\n\n    incrRefCount(str);\n    if (ctx != NULL) {\n        /*\n         * Put the str in the auto memory management of the ctx.\n         * It might already be there, in this case, the ref count will\n         * be 2 and we will decrease the ref count twice and free the\n         * object in the auto memory free function.\n         *\n         * Why we can not do the same trick of just remove the object\n         * from the auto memory (like in RM_RetainString)?\n         * This code shows the issue:\n         *\n         * RM_AutoMemory(ctx);\n         * str1 = RM_CreateString(ctx, \"test\", 4);\n         * str2 = RM_HoldString(ctx, str1);\n         * RM_FreeString(str1);\n         * RM_FreeString(str2);\n         *\n         * If after the RM_HoldString we would just remove the string from\n         * the auto memory, this example will cause access to a freed memory\n         * on 'RM_FreeString(str2);' because the String will be free\n         * on 'RM_FreeString(str1);'.\n         *\n         * So it's safer to just increase the ref count\n         * and add the String to auto memory again.\n         *\n         * The limitation is that it is not possible to use RedisModule_StringAppendBuffer\n         * on the String.\n         */\n        autoMemoryAdd(ctx,REDISMODULE_AM_STRING,str);\n    }\n    return str;\n}\n\n/* Given a string module object, this function returns the string pointer\n * and length of the string. The returned pointer and length should only\n * be used for read only accesses and never modified. */\nconst char *RM_StringPtrLen(const RedisModuleString *str, size_t *len) {\n    if (str == NULL) {\n        const char *errmsg = \"(NULL string reply referenced in module)\";\n        if (len) *len = strlen(errmsg);\n        return errmsg;\n    }\n    if (len) *len = sdslen(szFromObj(str));\n    return szFromObj(str);\n}\n\n/* --------------------------------------------------------------------------\n * Higher level string operations\n * ------------------------------------------------------------------------- */\n\n/* Convert the string into a long long integer, storing it at `*ll`.\n * Returns REDISMODULE_OK on success. If the string can't be parsed\n * as a valid, strict long long (no spaces before/after), REDISMODULE_ERR\n * is returned. */\nint RM_StringToLongLong(const RedisModuleString *str, long long *ll) {\n    return string2ll(szFromObj(str),sdslen(szFromObj(str)),ll) ? REDISMODULE_OK :\n                                                     REDISMODULE_ERR;\n}\n\n/* Convert the string into a double, storing it at `*d`.\n * Returns REDISMODULE_OK on success or REDISMODULE_ERR if the string is\n * not a valid string representation of a double value. */\nint RM_StringToDouble(const RedisModuleString *str, double *d) {\n    int retval = getDoubleFromObject(str,d);\n    return (retval == C_OK) ? REDISMODULE_OK : REDISMODULE_ERR;\n}\n\n/* Convert the string into a long double, storing it at `*ld`.\n * Returns REDISMODULE_OK on success or REDISMODULE_ERR if the string is\n * not a valid string representation of a double value. */\nint RM_StringToLongDouble(const RedisModuleString *str, long double *ld) {\n    int retval = string2ld(szFromObj(str),sdslen(szFromObj(str)),ld);\n    return retval ? REDISMODULE_OK : REDISMODULE_ERR;\n}\n\n/* Convert the string into a stream ID, storing it at `*id`.\n * Returns REDISMODULE_OK on success and returns REDISMODULE_ERR if the string\n * is not a valid string representation of a stream ID. The special IDs \"+\" and\n * \"-\" are allowed.\n */\nint RM_StringToStreamID(const RedisModuleString *str, RedisModuleStreamID *id) {\n    streamID streamid;\n    if (streamParseID(str, &streamid) == C_OK) {\n        id->ms = streamid.ms;\n        id->seq = streamid.seq;\n        return REDISMODULE_OK;\n    } else {\n        return REDISMODULE_ERR;\n    }\n}\n\n/* Compare two string objects, returning -1, 0 or 1 respectively if\n * a < b, a == b, a > b. Strings are compared byte by byte as two\n * binary blobs without any encoding care / collation attempt. */\nint RM_StringCompare(RedisModuleString *a, RedisModuleString *b) {\n    return compareStringObjects(a,b);\n}\n\n/* Return the (possibly modified in encoding) input 'str' object if\n * the string is unshared, otherwise NULL is returned. */\nRedisModuleString *moduleAssertUnsharedString(RedisModuleString *str) {\n    if (str->getrefcount(std::memory_order_relaxed) != 1) {\n        serverLog(LL_WARNING,\n            \"Module attempted to use an in-place string modify operation \"\n            \"with a string referenced multiple times. Please check the code \"\n            \"for API usage correctness.\");\n        return NULL;\n    }\n    if (str->encoding == OBJ_ENCODING_EMBSTR) {\n        /* Note: here we \"leak\" the additional allocation that was\n         * used in order to store the embedded string in the object. */\n        str->m_ptr = sdsnewlen(szFromObj(str),sdslen(szFromObj(str)));\n        str->encoding = OBJ_ENCODING_RAW;\n    } else if (str->encoding == OBJ_ENCODING_INT) {\n        /* Convert the string from integer to raw encoding. */\n        str->m_ptr = sdsfromlonglong((long)str->m_ptr);\n        str->encoding = OBJ_ENCODING_RAW;\n    }\n    return str;\n}\n\n/* Append the specified buffer to the string 'str'. The string must be a\n * string created by the user that is referenced only a single time, otherwise\n * REDISMODULE_ERR is returned and the operation is not performed. */\nint RM_StringAppendBuffer(RedisModuleCtx *ctx, RedisModuleString *str, const char *buf, size_t len) {\n    UNUSED(ctx);\n    str = moduleAssertUnsharedString(str);\n    if (str == NULL) return REDISMODULE_ERR;\n    str->m_ptr = sdscatlen((sds)str->m_ptr,buf,len);\n    return REDISMODULE_OK;\n}\n\n/* --------------------------------------------------------------------------\n * ## Reply APIs\n *\n * These functions are used for sending replies to the client.\n *\n * Most functions always return REDISMODULE_OK so you can use it with\n * 'return' in order to return from the command implementation with:\n *\n *     if (... some condition ...)\n *         return RedisModule_ReplyWithLongLong(ctx,mycount);\n * -------------------------------------------------------------------------- */\n\n/* Send an error about the number of arguments given to the command,\n * citing the command name in the error message. Returns REDISMODULE_OK.\n *\n * Example:\n *\n *     if (argc != 3) return RedisModule_WrongArity(ctx);\n */\nint RM_WrongArity(RedisModuleCtx *ctx) {\n    addReplyErrorFormat(ctx->client,\n        \"wrong number of arguments for '%s' command\",\n        (char*)ptrFromObj(ctx->client->argv[0]));\n    return REDISMODULE_OK;\n}\n\n/* Return the client object the `RM_Reply*` functions should target.\n * Normally this is just `ctx->client`, that is the client that called\n * the module command, however in the case of thread safe contexts there\n * is no directly associated client (since it would not be safe to access\n * the client from a thread), so instead the blocked client object referenced\n * in the thread safe context, has a fake client that we just use to accumulate\n * the replies. Later, when the client is unblocked, the accumulated replies\n * are appended to the actual client.\n *\n * The function returns the client pointer depending on the context, or\n * NULL if there is no potential client. This happens when we are in the\n * context of a thread safe context that was not initialized with a blocked\n * client object. Other contexts without associated clients are the ones\n * initialized to run the timers callbacks. */\nclient *moduleGetReplyClient(RedisModuleCtx *ctx) {\n    if (ctx->flags & REDISMODULE_CTX_THREAD_SAFE) {\n        if (ctx->blocked_client)\n            return ctx->blocked_client->reply_client;\n        else\n            return NULL;\n    } else {\n        /* If this is a non thread safe context, just return the client\n         * that is running the command if any. This may be NULL as well\n         * in the case of contexts that are not executed with associated\n         * clients, like timer contexts. */\n        return ctx->client;\n    }\n}\n\n/* Send an integer reply to the client, with the specified long long value.\n * The function always returns REDISMODULE_OK. */\nint RM_ReplyWithLongLong(RedisModuleCtx *ctx, long long ll) {\n    client *c = moduleGetReplyClient(ctx);\n    if (c == NULL) return REDISMODULE_OK;\n    AeLocker locker;\n    std::unique_lock<fastlock> lock(c->lock);\n    locker.arm(c);\n    addReplyLongLong(c,ll);\n    return REDISMODULE_OK;\n}\n\n/* Reply with the error 'err'.\n *\n * Note that 'err' must contain all the error, including\n * the initial error code. The function only provides the initial \"-\", so\n * the usage is, for example:\n *\n *     RedisModule_ReplyWithError(ctx,\"ERR Wrong Type\");\n *\n * and not just:\n *\n *     RedisModule_ReplyWithError(ctx,\"Wrong Type\");\n *\n * The function always returns REDISMODULE_OK.\n */\nint RM_ReplyWithError(RedisModuleCtx *ctx, const char *err) {\n    client *c = moduleGetReplyClient(ctx);\n    if (c == NULL) return REDISMODULE_OK;\n    addReplyErrorFormat(c,\"-%s\",err);\n    return REDISMODULE_OK;\n}\n\n/* Reply with a simple string (`+... \\r\\n` in RESP protocol). This replies\n * are suitable only when sending a small non-binary string with small\n * overhead, like \"OK\" or similar replies.\n *\n * The function always returns REDISMODULE_OK. */\nint RM_ReplyWithSimpleString(RedisModuleCtx *ctx, const char *msg) {\n    client *c = moduleGetReplyClient(ctx);\n    if (c == NULL) return REDISMODULE_OK;\n    addReplyProto(c,\"+\",1);\n    addReplyProto(c,msg,strlen(msg));\n    addReplyProto(c,\"\\r\\n\",2);\n    return REDISMODULE_OK;\n}\n\n/* Reply with an array type of 'len' elements. However 'len' other calls\n * to `ReplyWith*` style functions must follow in order to emit the elements\n * of the array.\n *\n * When producing arrays with a number of element that is not known beforehand\n * the function can be called with the special count\n * REDISMODULE_POSTPONED_ARRAY_LEN, and the actual number of elements can be\n * later set with RedisModule_ReplySetArrayLength() (which will set the\n * latest \"open\" count if there are multiple ones).\n *\n * The function always returns REDISMODULE_OK. */\nint RM_ReplyWithArray(RedisModuleCtx *ctx, long len) {\n    client *c = moduleGetReplyClient(ctx);\n    AeLocker locker;\n\n    if (c == NULL) return REDISMODULE_OK;\n    std::unique_lock<fastlock> lock(c->lock);\n    locker.arm(c);\n    if (len == REDISMODULE_POSTPONED_ARRAY_LEN) {\n        ctx->postponed_arrays = (void**)zrealloc(ctx->postponed_arrays,sizeof(void*)*\n                (ctx->postponed_arrays_count+1), MALLOC_LOCAL);\n        ctx->postponed_arrays[ctx->postponed_arrays_count] =\n            addReplyDeferredLen(c);\n        ctx->postponed_arrays_count++;\n    } else {\n        addReplyArrayLen(c,len);\n    }\n    return REDISMODULE_OK;\n}\n\n/* Reply to the client with a null array, simply null in RESP3 \n * null array in RESP2.\n *\n * The function always returns REDISMODULE_OK. */\nint RM_ReplyWithNullArray(RedisModuleCtx *ctx) {\n    client *c = moduleGetReplyClient(ctx);\n    if (c == NULL) return REDISMODULE_OK;\n    AeLocker locker;\n    std::unique_lock<fastlock> lock(c->lock);\n    locker.arm(c);\n    addReplyNullArray(c);\n    return REDISMODULE_OK;\n}\n\n/* Reply to the client with an empty array. \n *\n * The function always returns REDISMODULE_OK. */\nint RM_ReplyWithEmptyArray(RedisModuleCtx *ctx) {\n    client *c = moduleGetReplyClient(ctx);\n    if (c == NULL) return REDISMODULE_OK;\n    AeLocker locker;\n    std::unique_lock<fastlock> lock(c->lock);\n    locker.arm(c);\n    addReply(c,shared.emptyarray);\n    return REDISMODULE_OK;\n}\n\n/* When RedisModule_ReplyWithArray() is used with the argument\n * REDISMODULE_POSTPONED_ARRAY_LEN, because we don't know beforehand the number\n * of items we are going to output as elements of the array, this function\n * will take care to set the array length.\n *\n * Since it is possible to have multiple array replies pending with unknown\n * length, this function guarantees to always set the latest array length\n * that was created in a postponed way.\n *\n * For example in order to output an array like [1,[10,20,30]] we\n * could write:\n *\n *      RedisModule_ReplyWithArray(ctx,REDISMODULE_POSTPONED_ARRAY_LEN);\n *      RedisModule_ReplyWithLongLong(ctx,1);\n *      RedisModule_ReplyWithArray(ctx,REDISMODULE_POSTPONED_ARRAY_LEN);\n *      RedisModule_ReplyWithLongLong(ctx,10);\n *      RedisModule_ReplyWithLongLong(ctx,20);\n *      RedisModule_ReplyWithLongLong(ctx,30);\n *      RedisModule_ReplySetArrayLength(ctx,3); // Set len of 10,20,30 array.\n *      RedisModule_ReplySetArrayLength(ctx,2); // Set len of top array\n *\n * Note that in the above example there is no reason to postpone the array\n * length, since we produce a fixed number of elements, but in the practice\n * the code may use an iterator or other ways of creating the output so\n * that is not easy to calculate in advance the number of elements.\n */\nvoid RM_ReplySetArrayLength(RedisModuleCtx *ctx, long len) {\n    client *c = moduleGetReplyClient(ctx);\n    if (c == NULL) return;\n    AeLocker locker;\n    std::unique_lock<fastlock> lock(c->lock);\n    locker.arm(c);\n    if (ctx->postponed_arrays_count == 0) {\n        serverLog(LL_WARNING,\n            \"API misuse detected in module %s: \"\n            \"RedisModule_ReplySetArrayLength() called without previous \"\n            \"RedisModule_ReplyWithArray(ctx,REDISMODULE_POSTPONED_ARRAY_LEN) \"\n            \"call.\", ctx->module->name);\n            return;\n    }\n    ctx->postponed_arrays_count--;\n    setDeferredArrayLen(c,\n            ctx->postponed_arrays[ctx->postponed_arrays_count],\n            len);\n    if (ctx->postponed_arrays_count == 0) {\n        zfree(ctx->postponed_arrays);\n        ctx->postponed_arrays = NULL;\n    }\n}\n\n/* Reply with a bulk string, taking in input a C buffer pointer and length.\n *\n * The function always returns REDISMODULE_OK. */\nint RM_ReplyWithStringBuffer(RedisModuleCtx *ctx, const char *buf, size_t len) {\n    client *c = moduleGetReplyClient(ctx);\n    if (c == NULL) return REDISMODULE_OK;\n    AeLocker locker;\n    std::unique_lock<fastlock> lock(c->lock);\n    locker.arm(c);\n    addReplyBulkCBuffer(c,(char*)buf,len);\n    return REDISMODULE_OK;\n}\n\n/* Reply with a bulk string, taking in input a C buffer pointer that is\n * assumed to be null-terminated.\n *\n * The function always returns REDISMODULE_OK. */\nint RM_ReplyWithCString(RedisModuleCtx *ctx, const char *buf) {\n    client *c = moduleGetReplyClient(ctx);\n    if (c == NULL) return REDISMODULE_OK;\n    AeLocker locker;\n    std::unique_lock<fastlock> lock(c->lock);\n    locker.arm(c);\n    addReplyBulkCString(c,(char*)buf);\n    return REDISMODULE_OK;\n}\n\n/* Reply with a bulk string, taking in input a RedisModuleString object.\n *\n * The function always returns REDISMODULE_OK. */\nint RM_ReplyWithString(RedisModuleCtx *ctx, RedisModuleString *str) {\n    client *c = moduleGetReplyClient(ctx);\n    if (c == NULL) return REDISMODULE_OK;\n    AeLocker locker;\n    std::unique_lock<fastlock> lock(c->lock);\n    locker.arm(c);\n    addReplyBulk(c,str);\n    return REDISMODULE_OK;\n}\n\n/* Reply with an empty string.\n *\n * The function always returns REDISMODULE_OK. */\nint RM_ReplyWithEmptyString(RedisModuleCtx *ctx) {\n    client *c = moduleGetReplyClient(ctx);\n    if (c == NULL) return REDISMODULE_OK;\n    AeLocker locker;\n    std::unique_lock<fastlock> lock(c->lock);\n    locker.arm(c);\n    addReply(c,shared.emptybulk);\n    return REDISMODULE_OK;\n}\n\n/* Reply with a binary safe string, which should not be escaped or filtered \n * taking in input a C buffer pointer and length.\n *\n * The function always returns REDISMODULE_OK. */\nint RM_ReplyWithVerbatimString(RedisModuleCtx *ctx, const char *buf, size_t len) {\n    client *c = moduleGetReplyClient(ctx);\n    if (c == NULL) return REDISMODULE_OK;\n    AeLocker locker;\n    std::unique_lock<fastlock> lock(c->lock);\n    locker.arm(c);\n    addReplyVerbatim(c, buf, len, \"txt\");\n    return REDISMODULE_OK;\n}\n\n/* Reply to the client with a NULL.\n *\n * The function always returns REDISMODULE_OK. */\nint RM_ReplyWithNull(RedisModuleCtx *ctx) {\n    client *c = moduleGetReplyClient(ctx);\n    if (c == NULL) return REDISMODULE_OK;\n    AeLocker locker;\n    std::unique_lock<fastlock> lock(c->lock);\n    locker.arm(c);\n    addReplyNull(c);\n    return REDISMODULE_OK;\n}\n\n/* Reply exactly what a Redis command returned us with RedisModule_Call().\n * This function is useful when we use RedisModule_Call() in order to\n * execute some command, as we want to reply to the client exactly the\n * same reply we obtained by the command.\n *\n * The function always returns REDISMODULE_OK. */\nint RM_ReplyWithCallReply(RedisModuleCtx *ctx, RedisModuleCallReply *reply) {\n    client *c = moduleGetReplyClient(ctx);\n    if (c == NULL) return REDISMODULE_OK;\n    AeLocker locker;\n    std::unique_lock<fastlock> lock(c->lock);\n    locker.arm(c);\n    sds proto = sdsnewlen(reply->proto, reply->protolen);\n    addReplySds(c,proto);\n    return REDISMODULE_OK;\n}\n\n/* Send a string reply obtained converting the double 'd' into a bulk string.\n * This function is basically equivalent to converting a double into\n * a string into a C buffer, and then calling the function\n * RedisModule_ReplyWithStringBuffer() with the buffer and length.\n *\n * The function always returns REDISMODULE_OK. */\nint RM_ReplyWithDouble(RedisModuleCtx *ctx, double d) {\n    client *c = moduleGetReplyClient(ctx);\n    if (c == NULL) return REDISMODULE_OK;\n    AeLocker locker;\n    std::unique_lock<fastlock> lock(c->lock);\n    locker.arm(c);\n    addReplyDouble(c,d);\n    return REDISMODULE_OK;\n}\n\n/* Send a string reply obtained converting the long double 'ld' into a bulk\n * string. This function is basically equivalent to converting a long double\n * into a string into a C buffer, and then calling the function\n * RedisModule_ReplyWithStringBuffer() with the buffer and length.\n * The double string uses human readable formatting (see\n * `addReplyHumanLongDouble` in networking.c).\n *\n * The function always returns REDISMODULE_OK. */\nint RM_ReplyWithLongDouble(RedisModuleCtx *ctx, long double ld) {\n    client *c = moduleGetReplyClient(ctx);\n    if (c == NULL) return REDISMODULE_OK;\n    AeLocker locker;\n    std::unique_lock<fastlock> lock(c->lock);\n    locker.arm(c);\n    addReplyHumanLongDouble(c, ld);\n    return REDISMODULE_OK;\n}\n\n/* --------------------------------------------------------------------------\n * ## Commands replication API\n * -------------------------------------------------------------------------- */\n\n/* Helper function to replicate MULTI the first time we replicate something\n * in the context of a command execution. EXEC will be handled by the\n * RedisModuleCommandDispatcher() function. */\nvoid moduleReplicateMultiIfNeeded(RedisModuleCtx *ctx) {\n    /* Skip this if client explicitly wrap the command with MULTI, or if\n     * the module command was called by a script. */\n    if (serverTL->in_eval || serverTL->in_exec) return;\n    /* If we already emitted MULTI return ASAP. */\n    if (serverTL->propagate_in_transaction) return;\n    /* If this is a thread safe context, we do not want to wrap commands\n     * executed into MULTI/EXEC, they are executed as single commands\n     * from an external client in essence. */\n    if (ctx->flags & REDISMODULE_CTX_THREAD_SAFE) return;\n    /* If this is a callback context, and not a module command execution\n     * context, we have to setup the op array for the \"also propagate\" API\n     * so that RM_Replicate() will work. */\n    if (!(ctx->flags & REDISMODULE_CTX_MODULE_COMMAND_CALL)) {\n        serverAssert(ctx->saved_oparray.ops == NULL);\n        ctx->saved_oparray = g_pserver->also_propagate;\n        redisOpArrayInit(&g_pserver->also_propagate);\n    }\n    execCommandPropagateMulti(ctx->client->db->id);\n    ctx->flags |= REDISMODULE_CTX_MULTI_EMITTED;\n}\n\n/* Replicate the specified command and arguments to slaves and AOF, as effect\n * of execution of the calling command implementation.\n *\n * The replicated commands are always wrapped into the MULTI/EXEC that\n * contains all the commands replicated in a given module command\n * execution. However the commands replicated with RedisModule_Call()\n * are the first items, the ones replicated with RedisModule_Replicate()\n * will all follow before the EXEC.\n *\n * Modules should try to use one interface or the other.\n *\n * This command follows exactly the same interface of RedisModule_Call(),\n * so a set of format specifiers must be passed, followed by arguments\n * matching the provided format specifiers.\n *\n * Please refer to RedisModule_Call() for more information.\n *\n * Using the special \"A\" and \"R\" modifiers, the caller can exclude either\n * the AOF or the replicas from the propagation of the specified command.\n * Otherwise, by default, the command will be propagated in both channels.\n *\n * #### Note about calling this function from a thread safe context:\n *\n * Normally when you call this function from the callback implementing a\n * module command, or any other callback provided by the Redis Module API,\n * Redis will accumulate all the calls to this function in the context of\n * the callback, and will propagate all the commands wrapped in a MULTI/EXEC\n * transaction. However when calling this function from a threaded safe context\n * that can live an undefined amount of time, and can be locked/unlocked in\n * at will, the behavior is different: MULTI/EXEC wrapper is not emitted\n * and the command specified is inserted in the AOF and replication stream\n * immediately.\n *\n * #### Return value\n *\n * The command returns REDISMODULE_ERR if the format specifiers are invalid\n * or the command name does not belong to a known command. */\nint RM_Replicate(RedisModuleCtx *ctx, const char *cmdname, const char *fmt, ...) {\n    struct redisCommand *cmd;\n    robj **argv = NULL;\n    int argc = 0, flags = 0, j;\n    va_list ap;\n\n    cmd = lookupCommandByCString((char*)cmdname);\n    if (!cmd) return REDISMODULE_ERR;\n\n    /* Create the client and dispatch the command. */\n    va_start(ap, fmt);\n    argv = moduleCreateArgvFromUserFormat(cmdname,fmt,&argc,&flags,ap);\n    va_end(ap);\n    if (argv == NULL) return REDISMODULE_ERR;\n\n    /* Select the propagation target. Usually is AOF + replicas, however\n     * the caller can exclude one or the other using the \"A\" or \"R\"\n     * modifiers. */\n    int target = 0;\n    if (!(flags & REDISMODULE_ARGV_NO_AOF)) target |= PROPAGATE_AOF;\n    if (!(flags & REDISMODULE_ARGV_NO_REPLICAS)) target |= PROPAGATE_REPL;\n\n    /* Replicate! When we are in a threaded context, we want to just insert\n     * the replicated command ASAP, since it is not clear when the context\n     * will stop being used, so accumulating stuff does not make much sense,\n     * nor we could easily use the alsoPropagate() API from threads. */\n    if (ctx->flags & REDISMODULE_CTX_THREAD_SAFE) {\n        propagate(cmd,ctx->client->db->id,argv,argc,target);\n    } else {\n        moduleReplicateMultiIfNeeded(ctx);\n        alsoPropagate(cmd,ctx->client->db->id,argv,argc,target);\n    }\n\n    /* Release the argv. */\n    for (j = 0; j < argc; j++) decrRefCount(argv[j]);\n    zfree(argv);\n    g_pserver->dirty++;\n    return REDISMODULE_OK;\n}\n\n/* This function will replicate the command exactly as it was invoked\n * by the client. Note that this function will not wrap the command into\n * a MULTI/EXEC stanza, so it should not be mixed with other replication\n * commands.\n *\n * Basically this form of replication is useful when you want to propagate\n * the command to the slaves and AOF file exactly as it was called, since\n * the command can just be re-executed to deterministically re-create the\n * new state starting from the old one.\n *\n * The function always returns REDISMODULE_OK. */\nint RM_ReplicateVerbatim(RedisModuleCtx *ctx) {\n    alsoPropagate(ctx->client->cmd,ctx->client->db->id,\n        ctx->client->argv,ctx->client->argc,\n        PROPAGATE_AOF|PROPAGATE_REPL);\n    g_pserver->dirty++;\n    return REDISMODULE_OK;\n}\n\n/* --------------------------------------------------------------------------\n * ## DB and Key APIs -- Generic API\n * -------------------------------------------------------------------------- */\n\n/* Return the ID of the current client calling the currently active module\n * command. The returned ID has a few guarantees:\n *\n * 1. The ID is different for each different client, so if the same client\n *    executes a module command multiple times, it can be recognized as\n *    having the same ID, otherwise the ID will be different.\n * 2. The ID increases monotonically. Clients connecting to the server later\n *    are guaranteed to get IDs greater than any past ID previously seen.\n *\n * Valid IDs are from 1 to 2^64 - 1. If 0 is returned it means there is no way\n * to fetch the ID in the context the function was currently called.\n *\n * After obtaining the ID, it is possible to check if the command execution\n * is actually happening in the context of AOF loading, using this macro:\n *\n *      if (RedisModule_IsAOFClient(RedisModule_GetClientId(ctx)) {\n *          // Handle it differently.\n *      }\n */\nunsigned long long RM_GetClientId(RedisModuleCtx *ctx) {\n    if (ctx->client == NULL) return 0;\n    return ctx->client->id;\n}\n\n/* Return the ACL user name used by the client with the specified client ID.\n * Client ID can be obtained with RM_GetClientId() API. If the client does not\n * exist, NULL is returned and errno is set to ENOENT. If the client isn't \n * using an ACL user, NULL is returned and errno is set to ENOTSUP */\nRedisModuleString *RM_GetClientUserNameById(RedisModuleCtx *ctx, uint64_t id) {\n    client *client = lookupClientByID(id);\n    if (client == NULL) {\n        errno = ENOENT;\n        return NULL;\n    }\n    \n    if (client->user == NULL) {\n        errno = ENOTSUP;\n        return NULL;\n    }\n\n    sds name = sdsnew(client->user->name);\n    robj *str = createObject(OBJ_STRING, name);\n    autoMemoryAdd(ctx, REDISMODULE_AM_STRING, str);\n    return str;\n}\n\n/* This is an helper for RM_GetClientInfoById() and other functions: given\n * a client, it populates the client info structure with the appropriate\n * fields depending on the version provided. If the version is not valid\n * then REDISMODULE_ERR is returned. Otherwise the function returns\n * REDISMODULE_OK and the structure pointed by 'ci' gets populated. */\n\nint modulePopulateClientInfoStructure(void *ci, client *client, int structver) {\n    if (structver != 1) return REDISMODULE_ERR;\n\n    RedisModuleClientInfoV1 *ci1 = (RedisModuleClientInfoV1*)ci;\n    memset(ci1,0,sizeof(*ci1));\n    ci1->version = structver;\n    if (client->flags & CLIENT_MULTI)\n        ci1->flags |= REDISMODULE_CLIENTINFO_FLAG_MULTI;\n    if (client->flags & CLIENT_PUBSUB)\n        ci1->flags |= REDISMODULE_CLIENTINFO_FLAG_PUBSUB;\n    if (client->flags & CLIENT_UNIX_SOCKET)\n        ci1->flags |= REDISMODULE_CLIENTINFO_FLAG_UNIXSOCKET;\n    if (client->flags & CLIENT_TRACKING)\n        ci1->flags |= REDISMODULE_CLIENTINFO_FLAG_TRACKING;\n    if (client->flags & CLIENT_BLOCKED)\n        ci1->flags |= REDISMODULE_CLIENTINFO_FLAG_BLOCKED;\n    if (connGetType(client->conn) == CONN_TYPE_TLS)\n        ci1->flags |= REDISMODULE_CLIENTINFO_FLAG_SSL;\n\n    int port;\n    connPeerToString(client->conn,ci1->addr,sizeof(ci1->addr),&port);\n    ci1->port = port;\n    ci1->db = client->db->id;\n    ci1->id = client->id;\n    return REDISMODULE_OK;\n}\n\n/* This is an helper for moduleFireServerEvent() and other functions:\n * It populates the replication info structure with the appropriate\n * fields depending on the version provided. If the version is not valid\n * then REDISMODULE_ERR is returned. Otherwise the function returns\n * REDISMODULE_OK and the structure pointed by 'ri' gets populated. */\nint modulePopulateReplicationInfoStructure(void *ri, int structver) {\n    if (structver != 1) return REDISMODULE_ERR;\n\n    RedisModuleReplicationInfoV1 *ri1 = (RedisModuleReplicationInfoV1*)ri;\n    memset(ri1,0,sizeof(*ri1));\n    ri1->version = structver;\n    ri1->master = listLength(g_pserver->masters) == 0;\n    if (!ri1->master)\n    {\n        redisMaster *mi = (redisMaster*)listNodeValue(listFirst(g_pserver->masters));\n        ri1->masterhost = (char*)(mi->masterhost? mi->masterhost: \"\");\n        ri1->masterport = mi->masterport;\n    }\n    else\n    {\n        ri1->masterhost = \"\";\n        ri1->masterport = -1;\n    }\n    ri1->repl1_offset = g_pserver->master_repl_offset;\n    ri1->repl2_offset = g_pserver->second_replid_offset;\n    ri1->replid1 = g_pserver->replid;\n    ri1->replid2 = g_pserver->replid2;\n    return REDISMODULE_OK;\n}\n\n/* Return information about the client with the specified ID (that was\n * previously obtained via the RedisModule_GetClientId() API). If the\n * client exists, REDISMODULE_OK is returned, otherwise REDISMODULE_ERR\n * is returned.\n *\n * When the client exist and the `ci` pointer is not NULL, but points to\n * a structure of type RedisModuleClientInfo, previously initialized with\n * the correct REDISMODULE_CLIENTINFO_INITIALIZER, the structure is populated\n * with the following fields:\n *\n *      uint64_t flags;         // REDISMODULE_CLIENTINFO_FLAG_*\n *      uint64_t id;            // Client ID\n *      char addr[46];          // IPv4 or IPv6 address.\n *      uint16_t port;          // TCP port.\n *      uint16_t db;            // Selected DB.\n *\n * Note: the client ID is useless in the context of this call, since we\n *       already know, however the same structure could be used in other\n *       contexts where we don't know the client ID, yet the same structure\n *       is returned.\n *\n * With flags having the following meaning:\n *\n *     REDISMODULE_CLIENTINFO_FLAG_SSL          Client using SSL connection.\n *     REDISMODULE_CLIENTINFO_FLAG_PUBSUB       Client in Pub/Sub mode.\n *     REDISMODULE_CLIENTINFO_FLAG_BLOCKED      Client blocked in command.\n *     REDISMODULE_CLIENTINFO_FLAG_TRACKING     Client with keys tracking on.\n *     REDISMODULE_CLIENTINFO_FLAG_UNIXSOCKET   Client using unix domain socket.\n *     REDISMODULE_CLIENTINFO_FLAG_MULTI        Client in MULTI state.\n *\n * However passing NULL is a way to just check if the client exists in case\n * we are not interested in any additional information.\n *\n * This is the correct usage when we want the client info structure\n * returned:\n *\n *      RedisModuleClientInfo ci = REDISMODULE_CLIENTINFO_INITIALIZER;\n *      int retval = RedisModule_GetClientInfoById(&ci,client_id);\n *      if (retval == REDISMODULE_OK) {\n *          printf(\"Address: %s\\n\", ci.addr);\n *      }\n */\nint RM_GetClientInfoById(void *ci, uint64_t id) {\n    client *client = lookupClientByID(id);\n    if (client == NULL) return REDISMODULE_ERR;\n    if (ci == NULL) return REDISMODULE_OK;\n\n    /* Fill the info structure if passed. */\n    uint64_t structver = ((uint64_t*)ci)[0];\n    return modulePopulateClientInfoStructure(ci,client,structver);\n}\n\n/* Publish a message to subscribers (see PUBLISH command). */\nint RM_PublishMessage(RedisModuleCtx *ctx, RedisModuleString *channel, RedisModuleString *message) {\n    UNUSED(ctx);\n    int receivers = pubsubPublishMessage(channel, message);\n    if (g_pserver->cluster_enabled)\n        clusterPropagatePublish(channel, message);\n    return receivers;\n}\n\n/* Return the currently selected DB. */\nint RM_GetSelectedDb(RedisModuleCtx *ctx) {\n    return ctx->client->db->id;\n}\n\n\n/* Return the current context's flags. The flags provide information on the\n * current request context (whether the client is a Lua script or in a MULTI),\n * and about the Redis instance in general, i.e replication and persistence.\n *\n * It is possible to call this function even with a NULL context, however\n * in this case the following flags will not be reported:\n *\n *  * LUA, MULTI, REPLICATED, DIRTY (see below for more info).\n *\n * Available flags and their meaning:\n *\n *  * REDISMODULE_CTX_FLAGS_LUA: The command is running in a Lua script\n *\n *  * REDISMODULE_CTX_FLAGS_MULTI: The command is running inside a transaction\n *\n *  * REDISMODULE_CTX_FLAGS_REPLICATED: The command was sent over the replication\n *    link by the MASTER\n *\n *  * REDISMODULE_CTX_FLAGS_MASTER: The Redis instance is a master\n *\n *  * REDISMODULE_CTX_FLAGS_SLAVE: The Redis instance is a replica\n *\n *  * REDISMODULE_CTX_FLAGS_READONLY: The Redis instance is read-only\n *\n *  * REDISMODULE_CTX_FLAGS_CLUSTER: The Redis instance is in cluster mode\n *\n *  * REDISMODULE_CTX_FLAGS_AOF: The Redis instance has AOF enabled\n *\n *  * REDISMODULE_CTX_FLAGS_RDB: The instance has RDB enabled\n *\n *  * REDISMODULE_CTX_FLAGS_MAXMEMORY:  The instance has Maxmemory set\n *\n *  * REDISMODULE_CTX_FLAGS_EVICT:  Maxmemory is set and has an eviction\n *    policy that may delete keys\n *\n *  * REDISMODULE_CTX_FLAGS_OOM: Redis is out of memory according to the\n *    maxmemory setting.\n *\n *  * REDISMODULE_CTX_FLAGS_OOM_WARNING: Less than 25% of memory remains before\n *                                       reaching the maxmemory level.\n *\n *  * REDISMODULE_CTX_FLAGS_LOADING: Server is loading RDB/AOF\n *\n *  * REDISMODULE_CTX_FLAGS_REPLICA_IS_STALE: No active link with the master.\n *\n *  * REDISMODULE_CTX_FLAGS_REPLICA_IS_CONNECTING: The replica is trying to\n *                                                 connect with the master.\n *\n *  * REDISMODULE_CTX_FLAGS_REPLICA_IS_TRANSFERRING: Master -> Replica RDB\n *                                                   transfer is in progress.\n *\n *  * REDISMODULE_CTX_FLAGS_REPLICA_IS_ONLINE: The replica has an active link\n *                                             with its master. This is the\n *                                             contrary of STALE state.\n *\n *  * REDISMODULE_CTX_FLAGS_ACTIVE_CHILD: There is currently some background\n *                                        process active (RDB, AUX or module).\n *\n *  * REDISMODULE_CTX_FLAGS_MULTI_DIRTY: The next EXEC will fail due to dirty\n *                                       CAS (touched keys).\n *\n *  * REDISMODULE_CTX_FLAGS_IS_CHILD: Redis is currently running inside\n *                                    background child process.\n */\nint RM_GetContextFlags(RedisModuleCtx *ctx) {\n    int flags = 0;\n\n    /* Client specific flags */\n    if (ctx) {\n        if (ctx->client) {\n            if (ctx->client->flags & CLIENT_DENY_BLOCKING)\n                flags |= REDISMODULE_CTX_FLAGS_DENY_BLOCKING;\n            /* Module command received from MASTER, is replicated. */\n            if (ctx->client->flags & CLIENT_MASTER)\n                flags |= REDISMODULE_CTX_FLAGS_REPLICATED;\n        }\n\n        /* For DIRTY flags, we need the blocked client if used */\n        client *c = ctx->blocked_client ? ctx->blocked_client->client : ctx->client;\n        if (c && (c->flags & (CLIENT_DIRTY_CAS|CLIENT_DIRTY_EXEC))) {\n            flags |= REDISMODULE_CTX_FLAGS_MULTI_DIRTY;\n        }\n    }\n\n    if (serverTL->in_eval)\n        flags |= REDISMODULE_CTX_FLAGS_LUA;\n\n    if (serverTL->in_exec)\n        flags |= REDISMODULE_CTX_FLAGS_MULTI;\n\n    if (g_pserver->cluster_enabled)\n        flags |= REDISMODULE_CTX_FLAGS_CLUSTER;\n\n    if (g_pserver->loading)\n        flags |= REDISMODULE_CTX_FLAGS_LOADING;\n\n    /* Maxmemory and eviction policy */\n    if (g_pserver->maxmemory > 0 && (!listLength(g_pserver->masters) || !g_pserver->repl_slave_ignore_maxmemory)) {\n        flags |= REDISMODULE_CTX_FLAGS_MAXMEMORY;\n\n        if (g_pserver->maxmemory_policy != MAXMEMORY_NO_EVICTION)\n            flags |= REDISMODULE_CTX_FLAGS_EVICT;\n    }\n\n    /* Persistence flags */\n    if (g_pserver->aof_state != AOF_OFF)\n        flags |= REDISMODULE_CTX_FLAGS_AOF;\n    if (g_pserver->saveparamslen > 0)\n        flags |= REDISMODULE_CTX_FLAGS_RDB;\n\n    /* Replication flags */\n    if (listLength(g_pserver->masters) == 0) {\n        flags |= REDISMODULE_CTX_FLAGS_MASTER;\n    } else {\n        flags |= REDISMODULE_CTX_FLAGS_SLAVE;\n        if (g_pserver->repl_slave_ro)\n            flags |= REDISMODULE_CTX_FLAGS_READONLY;\n\n        /* Replica state flags. */\n        redisMaster *mi = (redisMaster*)listFirst(g_pserver->masters);\n        if (mi->repl_state == REPL_STATE_CONNECT ||\n            mi->repl_state == REPL_STATE_CONNECTING)\n        {\n            flags |= REDISMODULE_CTX_FLAGS_REPLICA_IS_CONNECTING;\n        } else if (mi->repl_state == REPL_STATE_TRANSFER) {\n            flags |= REDISMODULE_CTX_FLAGS_REPLICA_IS_TRANSFERRING;\n        } else if (mi->repl_state == REPL_STATE_CONNECTED) {\n            flags |= REDISMODULE_CTX_FLAGS_REPLICA_IS_ONLINE;\n        }\n\n        if (mi->repl_state != REPL_STATE_CONNECTED)\n            flags |= REDISMODULE_CTX_FLAGS_REPLICA_IS_STALE;\n    }\n\n    /* OOM flag. */\n    float level;\n    int retval = getMaxmemoryState(NULL,NULL,NULL,&level);\n    if (retval == C_ERR) flags |= REDISMODULE_CTX_FLAGS_OOM;\n    if (level > 0.75) flags |= REDISMODULE_CTX_FLAGS_OOM_WARNING;\n\n    /* Presence of children processes. */\n    if (hasActiveChildProcess()) flags |= REDISMODULE_CTX_FLAGS_ACTIVE_CHILD;\n    if (g_pserver->in_fork_child) flags |= REDISMODULE_CTX_FLAGS_IS_CHILD;\n\n    return flags;\n}\n\n/* Returns true if some client sent the CLIENT PAUSE command to the server or\n * if Redis Cluster is doing a manual failover, and paused tue clients.\n * This is needed when we have a master with replicas, and want to write,\n * without adding further data to the replication channel, that the replicas\n * replication offset, match the one of the master. When this happens, it is\n * safe to failover the master without data loss.\n *\n * However modules may generate traffic by calling RedisModule_Call() with\n * the \"!\" flag, or by calling RedisModule_Replicate(), in a context outside\n * commands execution, for instance in timeout callbacks, threads safe\n * contexts, and so forth. When modules will generate too much traffic, it\n * will be hard for the master and replicas offset to match, because there\n * is more data to send in the replication channel.\n *\n * So modules may want to try to avoid very heavy background work that has\n * the effect of creating data to the replication channel, when this function\n * returns true. This is mostly useful for modules that have background\n * garbage collection tasks, or that do writes and replicate such writes\n * periodically in timer callbacks or other periodic callbacks.\n */\nint RM_AvoidReplicaTraffic() {\n    moduleSetThreadVariablesIfNeeded();\n    return checkClientPauseTimeoutAndReturnIfPaused();\n}\n\n/* Change the currently selected DB. Returns an error if the id\n * is out of range.\n *\n * Note that the client will retain the currently selected DB even after\n * the Redis command implemented by the module calling this function\n * returns.\n *\n * If the module command wishes to change something in a different DB and\n * returns back to the original one, it should call RedisModule_GetSelectedDb()\n * before in order to restore the old DB number before returning. */\nint RM_SelectDb(RedisModuleCtx *ctx, int newid) {\n    int retval = selectDb(ctx->client,newid);\n    return (retval == C_OK) ? REDISMODULE_OK : REDISMODULE_ERR;\n}\n\n/* Initialize a RedisModuleKey struct */\nstatic void moduleInitKey(RedisModuleKey *kp, RedisModuleCtx *ctx, robj *keyname, robj *value, int mode){\n    kp->ctx = ctx;\n    kp->db = ctx->client->db;\n    kp->key = keyname;\n    incrRefCount(keyname);\n    kp->value = value;\n    kp->iter = NULL;\n    kp->mode = mode;\n    if (kp->value) moduleInitKeyTypeSpecific(kp);\n}\n\n/* Initialize the type-specific part of the key. Only when key has a value. */\nstatic void moduleInitKeyTypeSpecific(RedisModuleKey *key) {\n    switch (key->value->type) {\n    case OBJ_ZSET: zsetKeyReset(key); break;\n    case OBJ_STREAM: key->u.stream.signalready = 0; break;\n    }\n}\n\n/* Return an handle representing a Redis key, so that it is possible\n * to call other APIs with the key handle as argument to perform\n * operations on the key.\n *\n * The return value is the handle representing the key, that must be\n * closed with RM_CloseKey().\n *\n * If the key does not exist and WRITE mode is requested, the handle\n * is still returned, since it is possible to perform operations on\n * a yet not existing key (that will be created, for example, after\n * a list push operation). If the mode is just READ instead, and the\n * key does not exist, NULL is returned. However it is still safe to\n * call RedisModule_CloseKey() and RedisModule_KeyType() on a NULL\n * value. */\nvoid *RM_OpenKey(RedisModuleCtx *ctx, robj *keyname, int mode) {\n    RedisModuleKey *kp;\n    robj *value;\n    int flags = mode & REDISMODULE_OPEN_KEY_NOTOUCH? LOOKUP_NOTOUCH: 0;\n\n    if (mode & REDISMODULE_WRITE) {\n        value = lookupKeyWriteWithFlags(ctx->client->db,keyname, flags);\n    } else {\n        value = lookupKeyReadWithFlags(ctx->client->db,keyname, flags).unsafe_robjcast();\n        if (value == NULL) {\n            return NULL;\n        }\n    }\n\n    /* Setup the key handle. */\n    kp = (RedisModuleKey*)zmalloc(sizeof(*kp));\n    moduleInitKey(kp, ctx, keyname, value, mode);\n    autoMemoryAdd(ctx,REDISMODULE_AM_KEY,kp);\n    return (void*)kp;\n}\n\n/* Destroy a RedisModuleKey struct (freeing is the responsibility of the caller). */\nstatic void moduleCloseKey(RedisModuleKey *key) {\n    int signal = SHOULD_SIGNAL_MODIFIED_KEYS(key->ctx);\n    moduleAcquireGIL(false);\n    if ((key->mode & REDISMODULE_WRITE) && signal)\n        signalModifiedKey(key->ctx->client,key->db,key->key);\n    /* TODO: if (key->iter) RM_KeyIteratorStop(kp); */\n    moduleReleaseGIL(false);\n    if (key->iter) zfree(key->iter);\n    RM_ZsetRangeStop(key);\n    if (key && key->value && key->value->type == OBJ_STREAM &&\n        key->u.stream.signalready) {\n        /* One of more RM_StreamAdd() have been done. */\n        signalKeyAsReady(key->db, key->key, OBJ_STREAM);\n    }\n    decrRefCount(key->key);\n}\n\n/* Close a key handle. */\nvoid RM_CloseKey(RedisModuleKey *key) {\n    if (key == NULL) return;\n    moduleCloseKey(key);\n    autoMemoryFreed(key->ctx,REDISMODULE_AM_KEY,key);\n    zfree(key);\n}\n\n/* Return the type of the key. If the key pointer is NULL then\n * REDISMODULE_KEYTYPE_EMPTY is returned. */\nint RM_KeyType(RedisModuleKey *key) {\n    if (key == NULL || key->value ==  NULL) return REDISMODULE_KEYTYPE_EMPTY;\n    /* We map between defines so that we are free to change the internal\n     * defines as desired. */\n    switch(key->value->type) {\n    case OBJ_STRING: return REDISMODULE_KEYTYPE_STRING;\n    case OBJ_LIST: return REDISMODULE_KEYTYPE_LIST;\n    case OBJ_SET: return REDISMODULE_KEYTYPE_SET;\n    case OBJ_ZSET: return REDISMODULE_KEYTYPE_ZSET;\n    case OBJ_HASH: return REDISMODULE_KEYTYPE_HASH;\n    case OBJ_MODULE: return REDISMODULE_KEYTYPE_MODULE;\n    case OBJ_STREAM: return REDISMODULE_KEYTYPE_STREAM;\n    default: return REDISMODULE_KEYTYPE_EMPTY;\n    }\n}\n\n/* Return the length of the value associated with the key.\n * For strings this is the length of the string. For all the other types\n * is the number of elements (just counting keys for hashes).\n *\n * If the key pointer is NULL or the key is empty, zero is returned. */\nsize_t RM_ValueLength(RedisModuleKey *key) {\n    if (key == NULL || key->value == NULL) return 0;\n    switch(key->value->type) {\n    case OBJ_STRING: return stringObjectLen(key->value);\n    case OBJ_LIST: return listTypeLength(key->value);\n    case OBJ_SET: return setTypeSize(key->value);\n    case OBJ_ZSET: return zsetLength(key->value);\n    case OBJ_HASH: return hashTypeLength(key->value);\n    case OBJ_STREAM: return streamLength(key->value);\n    default: return 0;\n    }\n}\n\n/* If the key is open for writing, remove it, and setup the key to\n * accept new writes as an empty key (that will be created on demand).\n * On success REDISMODULE_OK is returned. If the key is not open for\n * writing REDISMODULE_ERR is returned. */\nint RM_DeleteKey(RedisModuleKey *key) {\n    if (!(key->mode & REDISMODULE_WRITE)) return REDISMODULE_ERR;\n    if (key->value) {\n        dbDelete(key->db,key->key);\n        key->value = NULL;\n    }\n    return REDISMODULE_OK;\n}\n\n/* If the key is open for writing, unlink it (that is delete it in a\n * non-blocking way, not reclaiming memory immediately) and setup the key to\n * accept new writes as an empty key (that will be created on demand).\n * On success REDISMODULE_OK is returned. If the key is not open for\n * writing REDISMODULE_ERR is returned. */\nint RM_UnlinkKey(RedisModuleKey *key) {\n    if (!(key->mode & REDISMODULE_WRITE)) return REDISMODULE_ERR;\n    if (key->value) {\n        dbAsyncDelete(key->db,key->key);\n        key->value = NULL;\n    }\n    return REDISMODULE_OK;\n}\n\n/* Return the key expire value, as milliseconds of remaining TTL.\n * If no TTL is associated with the key or if the key is empty,\n * REDISMODULE_NO_EXPIRE is returned. */\nmstime_t RM_GetExpire(RedisModuleKey *key) {\n    auto itr = key->db->find(key->key);\n    mstime_t expire = INVALID_EXPIRE;\n    if (itr->FExpires())\n        itr->expire.FGetPrimaryExpire(&expire);\n    \n    if (expire == INVALID_EXPIRE || key->value == NULL)\n        return REDISMODULE_NO_EXPIRE;\n    expire -= mstime();\n    return expire >= 0 ? expire : 0;\n}\n\n/* Set a new expire for the key. If the special expire\n * REDISMODULE_NO_EXPIRE is set, the expire is cancelled if there was\n * one (the same as the PERSIST command).\n *\n * Note that the expire must be provided as a positive integer representing\n * the number of milliseconds of TTL the key should have.\n *\n * The function returns REDISMODULE_OK on success or REDISMODULE_ERR if\n * the key was not open for writing or is an empty key. */\nint RM_SetExpire(RedisModuleKey *key, mstime_t expire) {\n    if (!(key->mode & REDISMODULE_WRITE) || key->value == NULL || (expire < 0 && expire != REDISMODULE_NO_EXPIRE))\n        return REDISMODULE_ERR;\n    if (expire != REDISMODULE_NO_EXPIRE) {\n        expire += mstime();\n        setExpire(key->ctx->client,key->db,key->key,nullptr,expire);\n    } else {\n        removeExpire(key->db,key->key);\n    }\n    return REDISMODULE_OK;\n}\n\n/* Return the key expire value, as absolute Unix timestamp.\n * If no TTL is associated with the key or if the key is empty,\n * REDISMODULE_NO_EXPIRE is returned. */\nmstime_t RM_GetAbsExpire(RedisModuleKey *key) {\n    auto expire = key->db->getExpire(key->key);\n    if (expire == nullptr || key->value == NULL) \n        return REDISMODULE_NO_EXPIRE;\n    return expire->when();\n}\n\n/* Set a new expire for the key. If the special expire\n * REDISMODULE_NO_EXPIRE is set, the expire is cancelled if there was\n * one (the same as the PERSIST command).\n * \n * Note that the expire must be provided as a positive integer representing\n * the absolute Unix timestamp the key should have.\n *\n * The function returns REDISMODULE_OK on success or REDISMODULE_ERR if\n * the key was not open for writing or is an empty key. */\nint RM_SetAbsExpire(RedisModuleKey *key, mstime_t expire) {\n    if (!(key->mode & REDISMODULE_WRITE) || key->value == NULL || (expire < 0 && expire != REDISMODULE_NO_EXPIRE))\n        return REDISMODULE_ERR;\n    if (expire != REDISMODULE_NO_EXPIRE) {\n        setExpire(key->ctx->client,key->db,key->key,nullptr/*subkey*/,expire);\n    } else {\n        removeExpire(key->db,key->key);\n    }\n    return REDISMODULE_OK;\n}\n\n/* Performs similar operation to FLUSHALL, and optionally start a new AOF file (if enabled)\n * If restart_aof is true, you must make sure the command that triggered this call is not\n * propagated to the AOF file.\n * When async is set to true, db contents will be freed by a background thread. */\nvoid RM_ResetDataset(int restart_aof, int async) {\n    if (restart_aof && g_pserver->aof_state != AOF_OFF) stopAppendOnly();\n    flushAllDataAndResetRDB(async? EMPTYDB_ASYNC: EMPTYDB_NO_FLAGS);\n    if (g_pserver->aof_enabled && restart_aof) restartAOFAfterSYNC();\n}\n\n/* Returns the number of keys in the current db. */\nunsigned long long RM_DbSize(RedisModuleCtx *ctx) {\n    return ctx->client->db->size();\n}\n\n/* Returns a name of a random key, or NULL if current db is empty. */\nRedisModuleString *RM_RandomKey(RedisModuleCtx *ctx) {\n    robj *key = dbRandomKey(ctx->client->db);\n    autoMemoryAdd(ctx,REDISMODULE_AM_STRING,key);\n    return key;\n}\n\n/* --------------------------------------------------------------------------\n * ## Key API for String type\n *\n * See also RM_ValueLength(), which returns the length of a string.\n * -------------------------------------------------------------------------- */\n\n/* If the key is open for writing, set the specified string 'str' as the\n * value of the key, deleting the old value if any.\n * On success REDISMODULE_OK is returned. If the key is not open for\n * writing or there is an active iterator, REDISMODULE_ERR is returned. */\nint RM_StringSet(RedisModuleKey *key, RedisModuleString *str) {\n    if (!(key->mode & REDISMODULE_WRITE) || key->iter) return REDISMODULE_ERR;\n    RM_DeleteKey(key);\n    genericSetKey(key->ctx->client,key->db,key->key,str,0,0);\n    key->value = str;\n    return REDISMODULE_OK;\n}\n\n/* Prepare the key associated string value for DMA access, and returns\n * a pointer and size (by reference), that the user can use to read or\n * modify the string in-place accessing it directly via pointer.\n *\n * The 'mode' is composed by bitwise OR-ing the following flags:\n *\n *     REDISMODULE_READ -- Read access\n *     REDISMODULE_WRITE -- Write access\n *\n * If the DMA is not requested for writing, the pointer returned should\n * only be accessed in a read-only fashion.\n *\n * On error (wrong type) NULL is returned.\n *\n * DMA access rules:\n *\n * 1. No other key writing function should be called since the moment\n * the pointer is obtained, for all the time we want to use DMA access\n * to read or modify the string.\n *\n * 2. Each time RM_StringTruncate() is called, to continue with the DMA\n * access, RM_StringDMA() should be called again to re-obtain\n * a new pointer and length.\n *\n * 3. If the returned pointer is not NULL, but the length is zero, no\n * byte can be touched (the string is empty, or the key itself is empty)\n * so a RM_StringTruncate() call should be used if there is to enlarge\n * the string, and later call StringDMA() again to get the pointer.\n */\nconst char *RM_StringDMA(RedisModuleKey *key, size_t *len, int mode) {\n    /* We need to return *some* pointer for empty keys, we just return\n     * a string literal pointer, that is the advantage to be mapped into\n     * a read only memory page, so the module will segfault if a write\n     * attempt is performed. */\n    const char *emptystring = \"<dma-empty-string>\";\n    if (key->value == NULL) {\n        *len = 0;\n        return emptystring;\n    }\n\n    if (key->value->type != OBJ_STRING) return NULL;\n\n    /* For write access, and even for read access if the object is encoded,\n     * we unshare the string (that has the side effect of decoding it). */\n    if ((mode & REDISMODULE_WRITE) || key->value->encoding != OBJ_ENCODING_RAW)\n        key->value = dbUnshareStringValue(key->db, key->key, key->value);\n\n    *len = sdslen(szFromObj(key->value));\n    return szFromObj(key->value);\n}\n\n/* If the string is open for writing and is of string type, resize it, padding\n * with zero bytes if the new length is greater than the old one.\n *\n * After this call, RM_StringDMA() must be called again to continue\n * DMA access with the new pointer.\n *\n * The function returns REDISMODULE_OK on success, and REDISMODULE_ERR on\n * error, that is, the key is not open for writing, is not a string\n * or resizing for more than 512 MB is requested.\n *\n * If the key is empty, a string key is created with the new string value\n * unless the new length value requested is zero. */\nint RM_StringTruncate(RedisModuleKey *key, size_t newlen) {\n    if (!(key->mode & REDISMODULE_WRITE)) return REDISMODULE_ERR;\n    if (key->value && key->value->type != OBJ_STRING) return REDISMODULE_ERR;\n    if (newlen > 512*1024*1024) return REDISMODULE_ERR;\n\n    /* Empty key and new len set to 0. Just return REDISMODULE_OK without\n     * doing anything. */\n    if (key->value == NULL && newlen == 0) return REDISMODULE_OK;\n\n    if (key->value == NULL) {\n        /* Empty key: create it with the new size. */\n        robj *o = createObject(OBJ_STRING,sdsnewlen(NULL, newlen));\n        genericSetKey(key->ctx->client,key->db,key->key,o,0,0);\n        key->value = o;\n        decrRefCount(o);\n    } else {\n        /* Unshare and resize. */\n        key->value = dbUnshareStringValue(key->db, key->key, key->value);\n        size_t curlen = sdslen(szFromObj(key->value));\n        if (newlen > curlen) {\n            key->value->m_ptr = sdsgrowzero(szFromObj(key->value),newlen);\n        } else if (newlen < curlen) {\n            sdssubstr(szFromObj(key->value),0,newlen);\n            /* If the string is too wasteful, reallocate it. */\n            if (sdslen(szFromObj(key->value)) < sdsavail(szFromObj(key->value)))\n                key->value->m_ptr = sdsRemoveFreeSpace(szFromObj(key->value));\n        }\n    }\n    return REDISMODULE_OK;\n}\n\n/* --------------------------------------------------------------------------\n * ## Key API for List type\n *\n * See also RM_ValueLength(), which returns the length of a list.\n * -------------------------------------------------------------------------- */\n\n/* Push an element into a list, on head or tail depending on 'where' argument.\n * If the key pointer is about an empty key opened for writing, the key\n * is created. On error (key opened for read-only operations or of the wrong\n * type) REDISMODULE_ERR is returned, otherwise REDISMODULE_OK is returned. */\nint RM_ListPush(RedisModuleKey *key, int where, RedisModuleString *ele) {\n    if (!(key->mode & REDISMODULE_WRITE)) return REDISMODULE_ERR;\n    if (key->value && key->value->type != OBJ_LIST) return REDISMODULE_ERR;\n    if (key->value == NULL) moduleCreateEmptyKey(key,REDISMODULE_KEYTYPE_LIST);\n    listTypePush(key->value, ele,\n        (where == REDISMODULE_LIST_HEAD) ? QUICKLIST_HEAD : QUICKLIST_TAIL);\n    return REDISMODULE_OK;\n}\n\n/* Pop an element from the list, and returns it as a module string object\n * that the user should be free with RM_FreeString() or by enabling\n * automatic memory. 'where' specifies if the element should be popped from\n * head or tail. The command returns NULL if:\n *\n * 1. The list is empty.\n * 2. The key was not open for writing.\n * 3. The key is not a list. */\nRedisModuleString *RM_ListPop(RedisModuleKey *key, int where) {\n    if (!(key->mode & REDISMODULE_WRITE) ||\n        key->value == NULL ||\n        key->value->type != OBJ_LIST) return NULL;\n    robj *ele = listTypePop(key->value,\n        (where == REDISMODULE_LIST_HEAD) ? QUICKLIST_HEAD : QUICKLIST_TAIL);\n    robj *decoded = getDecodedObject(ele);\n    decrRefCount(ele);\n    moduleDelKeyIfEmpty(key);\n    autoMemoryAdd(key->ctx,REDISMODULE_AM_STRING,decoded);\n    return decoded;\n}\n\n/* --------------------------------------------------------------------------\n * ## Key API for Sorted Set type\n *\n * See also RM_ValueLength(), which returns the length of a sorted set.\n * -------------------------------------------------------------------------- */\n\n/* Conversion from/to public flags of the Modules API and our private flags,\n * so that we have everything decoupled. */\nint moduleZsetAddFlagsToCoreFlags(int flags) {\n    int retflags = 0;\n    if (flags & REDISMODULE_ZADD_XX) retflags |= ZADD_IN_XX;\n    if (flags & REDISMODULE_ZADD_NX) retflags |= ZADD_IN_NX;\n    if (flags & REDISMODULE_ZADD_GT) retflags |= ZADD_IN_GT;\n    if (flags & REDISMODULE_ZADD_LT) retflags |= ZADD_IN_LT;\n    return retflags;\n}\n\n/* See previous function comment. */\nint moduleZsetAddFlagsFromCoreFlags(int flags) {\n    int retflags = 0;\n    if (flags & ZADD_OUT_ADDED) retflags |= REDISMODULE_ZADD_ADDED;\n    if (flags & ZADD_OUT_UPDATED) retflags |= REDISMODULE_ZADD_UPDATED;\n    if (flags & ZADD_OUT_NOP) retflags |= REDISMODULE_ZADD_NOP;\n    return retflags;\n}\n\n/* Add a new element into a sorted set, with the specified 'score'.\n * If the element already exists, the score is updated.\n *\n * A new sorted set is created at value if the key is an empty open key\n * setup for writing.\n *\n * Additional flags can be passed to the function via a pointer, the flags\n * are both used to receive input and to communicate state when the function\n * returns. 'flagsptr' can be NULL if no special flags are used.\n *\n * The input flags are:\n *\n *     REDISMODULE_ZADD_XX: Element must already exist. Do nothing otherwise.\n *     REDISMODULE_ZADD_NX: Element must not exist. Do nothing otherwise.\n *     REDISMODULE_ZADD_GT: If element exists, new score must be greater than the current score. \n *                          Do nothing otherwise. Can optionally be combined with XX.\n *     REDISMODULE_ZADD_LT: If element exists, new score must be less than the current score.\n *                          Do nothing otherwise. Can optionally be combined with XX.\n *\n * The output flags are:\n *\n *     REDISMODULE_ZADD_ADDED: The new element was added to the sorted set.\n *     REDISMODULE_ZADD_UPDATED: The score of the element was updated.\n *     REDISMODULE_ZADD_NOP: No operation was performed because XX or NX flags.\n *\n * On success the function returns REDISMODULE_OK. On the following errors\n * REDISMODULE_ERR is returned:\n *\n * * The key was not opened for writing.\n * * The key is of the wrong type.\n * * 'score' double value is not a number (NaN).\n */\nint RM_ZsetAdd(RedisModuleKey *key, double score, RedisModuleString *ele, int *flagsptr) {\n    int in_flags = 0, out_flags = 0;\n    if (!(key->mode & REDISMODULE_WRITE)) return REDISMODULE_ERR;\n    if (key->value && key->value->type != OBJ_ZSET) return REDISMODULE_ERR;\n    if (key->value == NULL) moduleCreateEmptyKey(key,REDISMODULE_KEYTYPE_ZSET);\n    if (flagsptr) in_flags = moduleZsetAddFlagsToCoreFlags(*flagsptr);\n    if (zsetAdd(key->value,score,szFromObj(ele),in_flags,&out_flags,NULL) == 0) {\n        if (flagsptr) *flagsptr = 0;\n        return REDISMODULE_ERR;\n    }\n    if (flagsptr) *flagsptr = moduleZsetAddFlagsFromCoreFlags(out_flags);\n    return REDISMODULE_OK;\n}\n\n/* This function works exactly like RM_ZsetAdd(), but instead of setting\n * a new score, the score of the existing element is incremented, or if the\n * element does not already exist, it is added assuming the old score was\n * zero.\n *\n * The input and output flags, and the return value, have the same exact\n * meaning, with the only difference that this function will return\n * REDISMODULE_ERR even when 'score' is a valid double number, but adding it\n * to the existing score results into a NaN (not a number) condition.\n *\n * This function has an additional field 'newscore', if not NULL is filled\n * with the new score of the element after the increment, if no error\n * is returned. */\nint RM_ZsetIncrby(RedisModuleKey *key, double score, RedisModuleString *ele, int *flagsptr, double *newscore) {\n    int in_flags = 0, out_flags = 0;\n    if (!(key->mode & REDISMODULE_WRITE)) return REDISMODULE_ERR;\n    if (key->value && key->value->type != OBJ_ZSET) return REDISMODULE_ERR;\n    if (key->value == NULL) moduleCreateEmptyKey(key,REDISMODULE_KEYTYPE_ZSET);\n    if (flagsptr) in_flags = moduleZsetAddFlagsToCoreFlags(*flagsptr);\n    in_flags |= ZADD_IN_INCR;\n    if (zsetAdd(key->value,score,szFromObj(ele),in_flags,&out_flags,newscore) == 0) {\n        if (flagsptr) *flagsptr = 0;\n        return REDISMODULE_ERR;\n    }\n    if (flagsptr) *flagsptr = moduleZsetAddFlagsFromCoreFlags(out_flags);\n    return REDISMODULE_OK;\n}\n\n/* Remove the specified element from the sorted set.\n * The function returns REDISMODULE_OK on success, and REDISMODULE_ERR\n * on one of the following conditions:\n *\n * * The key was not opened for writing.\n * * The key is of the wrong type.\n *\n * The return value does NOT indicate the fact the element was really\n * removed (since it existed) or not, just if the function was executed\n * with success.\n *\n * In order to know if the element was removed, the additional argument\n * 'deleted' must be passed, that populates the integer by reference\n * setting it to 1 or 0 depending on the outcome of the operation.\n * The 'deleted' argument can be NULL if the caller is not interested\n * to know if the element was really removed.\n *\n * Empty keys will be handled correctly by doing nothing. */\nint RM_ZsetRem(RedisModuleKey *key, RedisModuleString *ele, int *deleted) {\n    if (!(key->mode & REDISMODULE_WRITE)) return REDISMODULE_ERR;\n    if (key->value && key->value->type != OBJ_ZSET) return REDISMODULE_ERR;\n    if (key->value != NULL && zsetDel(key->value,szFromObj(ele))) {\n        if (deleted) *deleted = 1;\n        moduleDelKeyIfEmpty(key);\n    } else {\n        if (deleted) *deleted = 0;\n    }\n    return REDISMODULE_OK;\n}\n\n/* On success retrieve the double score associated at the sorted set element\n * 'ele' and returns REDISMODULE_OK. Otherwise REDISMODULE_ERR is returned\n * to signal one of the following conditions:\n *\n * * There is no such element 'ele' in the sorted set.\n * * The key is not a sorted set.\n * * The key is an open empty key.\n */\nint RM_ZsetScore(RedisModuleKey *key, RedisModuleString *ele, double *score) {\n    if (key->value == NULL) return REDISMODULE_ERR;\n    if (key->value->type != OBJ_ZSET) return REDISMODULE_ERR;\n    if (zsetScore(key->value,szFromObj(ele),score) == C_ERR) return REDISMODULE_ERR;\n    return REDISMODULE_OK;\n}\n\n/* --------------------------------------------------------------------------\n * ## Key API for Sorted Set iterator\n * -------------------------------------------------------------------------- */\n\nvoid zsetKeyReset(RedisModuleKey *key) {\n    key->u.zset.type = REDISMODULE_ZSET_RANGE_NONE;\n    key->u.zset.current = NULL;\n    key->u.zset.er = 1;\n}\n\n/* Stop a sorted set iteration. */\nvoid RM_ZsetRangeStop(RedisModuleKey *key) {\n    if (!key->value || key->value->type != OBJ_ZSET) return;\n    /* Free resources if needed. */\n    if (key->u.zset.type == REDISMODULE_ZSET_RANGE_LEX)\n        zslFreeLexRange(&key->u.zset.lrs);\n    /* Setup sensible values so that misused iteration API calls when an\n     * iterator is not active will result into something more sensible\n     * than crashing. */\n    zsetKeyReset(key);\n}\n\n/* Return the \"End of range\" flag value to signal the end of the iteration. */\nint RM_ZsetRangeEndReached(RedisModuleKey *key) {\n    if (!key->value || key->value->type != OBJ_ZSET) return 1;\n    return key->u.zset.er;\n}\n\n/* Helper function for RM_ZsetFirstInScoreRange() and RM_ZsetLastInScoreRange().\n * Setup the sorted set iteration according to the specified score range\n * (see the functions calling it for more info). If 'first' is true the\n * first element in the range is used as a starting point for the iterator\n * otherwise the last. Return REDISMODULE_OK on success otherwise\n * REDISMODULE_ERR. */\nint zsetInitScoreRange(RedisModuleKey *key, double min, double max, int minex, int maxex, int first) {\n    if (!key->value || key->value->type != OBJ_ZSET) return REDISMODULE_ERR;\n\n    RM_ZsetRangeStop(key);\n    key->u.zset.type = REDISMODULE_ZSET_RANGE_SCORE;\n    key->u.zset.er = 0;\n\n    /* Setup the range structure used by the sorted set core implementation\n     * in order to seek at the specified element. */\n    zrangespec *zrs = &key->u.zset.rs;\n    zrs->min = min;\n    zrs->max = max;\n    zrs->minex = minex;\n    zrs->maxex = maxex;\n\n    if (key->value->encoding == OBJ_ENCODING_ZIPLIST) {\n        key->u.zset.current = first ? zzlFirstInRange((unsigned char*)ptrFromObj(key->value),zrs) :\n                                      zzlLastInRange((unsigned char*)ptrFromObj(key->value),zrs);\n    } else if (key->value->encoding == OBJ_ENCODING_SKIPLIST) {\n        zset *zs = (zset*)ptrFromObj(key->value);\n        zskiplist *zsl = zs->zsl;\n        key->u.zset.current = first ? zslFirstInRange(zsl,zrs) :\n                                      zslLastInRange(zsl,zrs);\n    } else {\n        serverPanic(\"Unsupported zset encoding\");\n    }\n    if (key->u.zset.current == NULL) key->u.zset.er = 1;\n    return REDISMODULE_OK;\n}\n\n/* Setup a sorted set iterator seeking the first element in the specified\n * range. Returns REDISMODULE_OK if the iterator was correctly initialized\n * otherwise REDISMODULE_ERR is returned in the following conditions:\n *\n * 1. The value stored at key is not a sorted set or the key is empty.\n *\n * The range is specified according to the two double values 'min' and 'max'.\n * Both can be infinite using the following two macros:\n *\n * * REDISMODULE_POSITIVE_INFINITE for positive infinite value\n * * REDISMODULE_NEGATIVE_INFINITE for negative infinite value\n *\n * 'minex' and 'maxex' parameters, if true, respectively setup a range\n * where the min and max value are exclusive (not included) instead of\n * inclusive. */\nint RM_ZsetFirstInScoreRange(RedisModuleKey *key, double min, double max, int minex, int maxex) {\n    return zsetInitScoreRange(key,min,max,minex,maxex,1);\n}\n\n/* Exactly like RedisModule_ZsetFirstInScoreRange() but the last element of\n * the range is selected for the start of the iteration instead. */\nint RM_ZsetLastInScoreRange(RedisModuleKey *key, double min, double max, int minex, int maxex) {\n    return zsetInitScoreRange(key,min,max,minex,maxex,0);\n}\n\n/* Helper function for RM_ZsetFirstInLexRange() and RM_ZsetLastInLexRange().\n * Setup the sorted set iteration according to the specified lexicographical\n * range (see the functions calling it for more info). If 'first' is true the\n * first element in the range is used as a starting point for the iterator\n * otherwise the last. Return REDISMODULE_OK on success otherwise\n * REDISMODULE_ERR.\n *\n * Note that this function takes 'min' and 'max' in the same form of the\n * Redis ZRANGEBYLEX command. */\nint zsetInitLexRange(RedisModuleKey *key, RedisModuleString *min, RedisModuleString *max, int first) {\n    if (!key->value || key->value->type != OBJ_ZSET) return REDISMODULE_ERR;\n\n    RM_ZsetRangeStop(key);\n    key->u.zset.er = 0;\n\n    /* Setup the range structure used by the sorted set core implementation\n     * in order to seek at the specified element. */\n    zlexrangespec *zlrs = &key->u.zset.lrs;\n    if (zslParseLexRange(min, max, zlrs) == C_ERR) return REDISMODULE_ERR;\n\n    /* Set the range type to lex only after successfully parsing the range,\n     * otherwise we don't want the zlexrangespec to be freed. */\n    key->u.zset.type = REDISMODULE_ZSET_RANGE_LEX;\n\n    if (key->value->encoding == OBJ_ENCODING_ZIPLIST) {\n        key->u.zset.current = first ? zzlFirstInLexRange((unsigned char*)ptrFromObj(key->value),zlrs) :\n                                zzlLastInLexRange((unsigned char*)ptrFromObj(key->value),zlrs);\n    } else if (key->value->encoding == OBJ_ENCODING_SKIPLIST) {\n        zset *zs = (zset*)ptrFromObj(key->value);\n        zskiplist *zsl = zs->zsl;\n        key->u.zset.current = first ? zslFirstInLexRange(zsl,zlrs) :\n                                      zslLastInLexRange(zsl,zlrs);\n    } else {\n        serverPanic(\"Unsupported zset encoding\");\n    }\n    if (key->u.zset.current == NULL) key->u.zset.er = 1;\n\n    return REDISMODULE_OK;\n}\n\n/* Setup a sorted set iterator seeking the first element in the specified\n * lexicographical range. Returns REDISMODULE_OK if the iterator was correctly\n * initialized otherwise REDISMODULE_ERR is returned in the\n * following conditions:\n *\n * 1. The value stored at key is not a sorted set or the key is empty.\n * 2. The lexicographical range 'min' and 'max' format is invalid.\n *\n * 'min' and 'max' should be provided as two RedisModuleString objects\n * in the same format as the parameters passed to the ZRANGEBYLEX command.\n * The function does not take ownership of the objects, so they can be released\n * ASAP after the iterator is setup. */\nint RM_ZsetFirstInLexRange(RedisModuleKey *key, RedisModuleString *min, RedisModuleString *max) {\n    return zsetInitLexRange(key,min,max,1);\n}\n\n/* Exactly like RedisModule_ZsetFirstInLexRange() but the last element of\n * the range is selected for the start of the iteration instead. */\nint RM_ZsetLastInLexRange(RedisModuleKey *key, RedisModuleString *min, RedisModuleString *max) {\n    return zsetInitLexRange(key,min,max,0);\n}\n\n/* Return the current sorted set element of an active sorted set iterator\n * or NULL if the range specified in the iterator does not include any\n * element. */\nRedisModuleString *RM_ZsetRangeCurrentElement(RedisModuleKey *key, double *score) {\n    RedisModuleString *str;\n\n    if (!key->value || key->value->type != OBJ_ZSET) return NULL;\n    if (key->u.zset.current == NULL) return NULL;\n    if (key->value->encoding == OBJ_ENCODING_ZIPLIST) {\n        unsigned char *eptr, *sptr;\n        eptr = (unsigned char*)key->u.zset.current;\n        sds ele = ziplistGetObject(eptr);\n        if (score) {\n            sptr = ziplistNext((unsigned char*)ptrFromObj(key->value),eptr);\n            *score = zzlGetScore(sptr);\n        }\n        str = createObject(OBJ_STRING,ele);\n    } else if (key->value->encoding == OBJ_ENCODING_SKIPLIST) {\n        zskiplistNode *ln = (zskiplistNode*)key->u.zset.current;\n        if (score) *score = ln->score;\n        str = createStringObject(ln->ele,sdslen(ln->ele));\n    } else {\n        serverPanic(\"Unsupported zset encoding\");\n    }\n    autoMemoryAdd(key->ctx,REDISMODULE_AM_STRING,str);\n    return str;\n}\n\n/* Go to the next element of the sorted set iterator. Returns 1 if there was\n * a next element, 0 if we are already at the latest element or the range\n * does not include any item at all. */\nint RM_ZsetRangeNext(RedisModuleKey *key) {\n    if (!key->value || key->value->type != OBJ_ZSET) return 0;\n    if (!key->u.zset.type || !key->u.zset.current) return 0; /* No active iterator. */\n\n    if (key->value->encoding == OBJ_ENCODING_ZIPLIST) {\n        unsigned char *zl = (unsigned char*)ptrFromObj(key->value);\n        unsigned char *eptr = (unsigned char*)key->u.zset.current;\n        unsigned char *next;\n        next = ziplistNext(zl,eptr); /* Skip element. */\n        if (next) next = ziplistNext(zl,next); /* Skip score. */\n        if (next == NULL) {\n            key->u.zset.er = 1;\n            return 0;\n        } else {\n            /* Are we still within the range? */\n            if (key->u.zset.type == REDISMODULE_ZSET_RANGE_SCORE) {\n                /* Fetch the next element score for the\n                 * range check. */\n                unsigned char *saved_next = next;\n                next = ziplistNext(zl,next); /* Skip next element. */\n                double score = zzlGetScore(next); /* Obtain the next score. */\n                if (!zslValueLteMax(score,&key->u.zset.rs)) {\n                    key->u.zset.er = 1;\n                    return 0;\n                }\n                next = saved_next;\n            } else if (key->u.zset.type == REDISMODULE_ZSET_RANGE_LEX) {\n                if (!zzlLexValueLteMax(next,&key->u.zset.lrs)) {\n                    key->u.zset.er = 1;\n                    return 0;\n                }\n            }\n            key->u.zset.current = next;\n            return 1;\n        }\n    } else if (key->value->encoding == OBJ_ENCODING_SKIPLIST) {\n        zskiplistNode *ln = (zskiplistNode*)key->u.zset.current, *next = (zskiplistNode*)ln->level(0)->forward;\n        if (next == NULL) {\n            key->u.zset.er = 1;\n            return 0;\n        } else {\n            /* Are we still within the range? */\n            if (key->u.zset.type == REDISMODULE_ZSET_RANGE_SCORE &&\n                !zslValueLteMax(next->score,&key->u.zset.rs))\n            {\n                key->u.zset.er = 1;\n                return 0;\n            } else if (key->u.zset.type == REDISMODULE_ZSET_RANGE_LEX) {\n                if (!zslLexValueLteMax(next->ele,&key->u.zset.lrs)) {\n                    key->u.zset.er = 1;\n                    return 0;\n                }\n            }\n            key->u.zset.current = next;\n            return 1;\n        }\n    } else {\n        serverPanic(\"Unsupported zset encoding\");\n    }\n}\n\n/* Go to the previous element of the sorted set iterator. Returns 1 if there was\n * a previous element, 0 if we are already at the first element or the range\n * does not include any item at all. */\nint RM_ZsetRangePrev(RedisModuleKey *key) {\n    if (!key->value || key->value->type != OBJ_ZSET) return 0;\n    if (!key->u.zset.type || !key->u.zset.current) return 0; /* No active iterator. */\n\n    if (key->value->encoding == OBJ_ENCODING_ZIPLIST) {\n        unsigned char *zl = (unsigned char*)ptrFromObj(key->value);\n        unsigned char *eptr = (unsigned char*)key->u.zset.current;\n        unsigned char *prev;\n        prev = ziplistPrev(zl,eptr); /* Go back to previous score. */\n        if (prev) prev = ziplistPrev(zl,prev); /* Back to previous ele. */\n        if (prev == NULL) {\n            key->u.zset.er = 1;\n            return 0;\n        } else {\n            /* Are we still within the range? */\n            if (key->u.zset.type == REDISMODULE_ZSET_RANGE_SCORE) {\n                /* Fetch the previous element score for the\n                 * range check. */\n                unsigned char *saved_prev = prev;\n                prev = ziplistNext(zl,prev); /* Skip element to get the score.*/\n                double score = zzlGetScore(prev); /* Obtain the prev score. */\n                if (!zslValueGteMin(score,&key->u.zset.rs)) {\n                    key->u.zset.er = 1;\n                    return 0;\n                }\n                prev = saved_prev;\n            } else if (key->u.zset.type == REDISMODULE_ZSET_RANGE_LEX) {\n                if (!zzlLexValueGteMin(prev,&key->u.zset.lrs)) {\n                    key->u.zset.er = 1;\n                    return 0;\n                }\n            }\n            key->u.zset.current = prev;\n            return 1;\n        }\n    } else if (key->value->encoding == OBJ_ENCODING_SKIPLIST) {\n        zskiplistNode *ln = (zskiplistNode*)key->u.zset.current, *prev = ln->backward;\n        if (prev == NULL) {\n            key->u.zset.er = 1;\n            return 0;\n        } else {\n            /* Are we still within the range? */\n            if (key->u.zset.type == REDISMODULE_ZSET_RANGE_SCORE &&\n                !zslValueGteMin(prev->score,&key->u.zset.rs))\n            {\n                key->u.zset.er = 1;\n                return 0;\n            } else if (key->u.zset.type == REDISMODULE_ZSET_RANGE_LEX) {\n                if (!zslLexValueGteMin(prev->ele,&key->u.zset.lrs)) {\n                    key->u.zset.er = 1;\n                    return 0;\n                }\n            }\n            key->u.zset.current = prev;\n            return 1;\n        }\n    } else {\n        serverPanic(\"Unsupported zset encoding\");\n    }\n}\n\n/* --------------------------------------------------------------------------\n * ## Key API for Hash type\n *\n * See also RM_ValueLength(), which returns the number of fields in a hash.\n * -------------------------------------------------------------------------- */\n\n/* Set the field of the specified hash field to the specified value.\n * If the key is an empty key open for writing, it is created with an empty\n * hash value, in order to set the specified field.\n *\n * The function is variadic and the user must specify pairs of field\n * names and values, both as RedisModuleString pointers (unless the\n * CFIELD option is set, see later). At the end of the field/value-ptr pairs, \n * NULL must be specified as last argument to signal the end of the arguments \n * in the variadic function.\n *\n * Example to set the hash argv[1] to the value argv[2]:\n *\n *      RedisModule_HashSet(key,REDISMODULE_HASH_NONE,argv[1],argv[2],NULL);\n *\n * The function can also be used in order to delete fields (if they exist)\n * by setting them to the specified value of REDISMODULE_HASH_DELETE:\n *\n *      RedisModule_HashSet(key,REDISMODULE_HASH_NONE,argv[1],\n *                          REDISMODULE_HASH_DELETE,NULL);\n *\n * The behavior of the command changes with the specified flags, that can be\n * set to REDISMODULE_HASH_NONE if no special behavior is needed.\n *\n *     REDISMODULE_HASH_NX: The operation is performed only if the field was not\n *                          already existing in the hash.\n *     REDISMODULE_HASH_XX: The operation is performed only if the field was\n *                          already existing, so that a new value could be\n *                          associated to an existing filed, but no new fields\n *                          are created.\n *     REDISMODULE_HASH_CFIELDS: The field names passed are null terminated C\n *                               strings instead of RedisModuleString objects.\n *     REDISMODULE_HASH_COUNT_ALL: Include the number of inserted fields in the\n *                                 returned number, in addition to the number of\n *                                 updated and deleted fields. (Added in Redis\n *                                 6.2.)\n *\n * Unless NX is specified, the command overwrites the old field value with\n * the new one.\n *\n * When using REDISMODULE_HASH_CFIELDS, field names are reported using\n * normal C strings, so for example to delete the field \"foo\" the following\n * code can be used:\n *\n *      RedisModule_HashSet(key,REDISMODULE_HASH_CFIELDS,\"foo\",\n *                          REDISMODULE_HASH_DELETE,NULL);\n *\n * Return value:\n *\n * The number of fields existing in the hash prior to the call, which have been\n * updated (its old value has been replaced by a new value) or deleted. If the\n * flag REDISMODULE_HASH_COUNT_ALL is set, insterted fields not previously\n * existing in the hash are also counted.\n *\n * If the return value is zero, `errno` is set (since Redis 6.2) as follows:\n *\n * - EINVAL if any unknown flags are set or if key is NULL.\n * - ENOTSUP if the key is associated with a non Hash value.\n * - EBADF if the key was not opened for writing.\n * - ENOENT if no fields were counted as described under Return value above.\n *   This is not actually an error. The return value can be zero if all fields\n *   were just created and the COUNT_ALL flag was unset, or if changes were held\n *   back due to the NX and XX flags.\n *\n * NOTICE: The return value semantics of this function are very different\n * between Redis 6.2 and older versions. Modules that use it should determine\n * the Redis version and handle it accordingly.\n */\nint RM_HashSet(RedisModuleKey *key, int flags, ...) {\n    va_list ap;\n    if (!key || (flags & ~(REDISMODULE_HASH_NX |\n                           REDISMODULE_HASH_XX |\n                           REDISMODULE_HASH_CFIELDS |\n                           REDISMODULE_HASH_COUNT_ALL))) {\n        errno = EINVAL;\n        return 0;\n    } else if (key->value && key->value->type != OBJ_HASH) {\n        errno = ENOTSUP;\n        return 0;\n    } else if (!(key->mode & REDISMODULE_WRITE)) {\n        errno = EBADF;\n        return 0;\n    }\n    if (key->value == NULL) moduleCreateEmptyKey(key,REDISMODULE_KEYTYPE_HASH);\n\n    int count = 0;\n    va_start(ap, flags);\n    while(1) {\n        RedisModuleString *field, *value;\n        /* Get the field and value objects. */\n        if (flags & REDISMODULE_HASH_CFIELDS) {\n            char *cfield = va_arg(ap,char*);\n            if (cfield == NULL) break;\n            field = createRawStringObject(cfield,strlen(cfield));\n        } else {\n            field = va_arg(ap,RedisModuleString*);\n            if (field == NULL) break;\n        }\n        value = va_arg(ap,RedisModuleString*);\n\n        /* Handle XX and NX */\n        if (flags & (REDISMODULE_HASH_XX|REDISMODULE_HASH_NX)) {\n            int exists = hashTypeExists(key->value, szFromObj(field));\n            if (((flags & REDISMODULE_HASH_XX) && !exists) ||\n                ((flags & REDISMODULE_HASH_NX) && exists))\n            {\n                if (flags & REDISMODULE_HASH_CFIELDS) decrRefCount(field);\n                continue;\n            }\n        }\n\n        /* Handle deletion if value is REDISMODULE_HASH_DELETE. */\n        if (value == REDISMODULE_HASH_DELETE) {\n            count += hashTypeDelete(key->value, szFromObj(field));\n            if (flags & REDISMODULE_HASH_CFIELDS) decrRefCount(field);\n            continue;\n        }\n\n        int low_flags = HASH_SET_COPY;\n        /* If CFIELDS is active, we can pass the ownership of the\n         * SDS object to the low level function that sets the field\n         * to avoid a useless copy. */\n        if (flags & REDISMODULE_HASH_CFIELDS)\n            low_flags |= HASH_SET_TAKE_FIELD;\n\n        robj *argv[2] = {field,value};\n        hashTypeTryConversion(key->value,argv,0,1);\n        int updated = hashTypeSet(key->value, szFromObj(field), szFromObj(value), low_flags);\n        count += (flags & REDISMODULE_HASH_COUNT_ALL) ? 1 : updated;\n\n        /* If CFIELDS is active, SDS string ownership is now of hashTypeSet(),\n         * however we still have to release the 'field' object shell. */\n        if (flags & REDISMODULE_HASH_CFIELDS) {\n           field->m_ptr = NULL; /* Prevent the SDS string from being freed. */\n           decrRefCount(field);\n        }\n    }\n    va_end(ap);\n    moduleDelKeyIfEmpty(key);\n    if (count == 0) errno = ENOENT;\n    return count;\n}\n\n/* Get fields from an hash value. This function is called using a variable\n * number of arguments, alternating a field name (as a RedisModuleString\n * pointer) with a pointer to a RedisModuleString pointer, that is set to the\n * value of the field if the field exists, or NULL if the field does not exist.\n * At the end of the field/value-ptr pairs, NULL must be specified as last\n * argument to signal the end of the arguments in the variadic function.\n *\n * This is an example usage:\n *\n *      RedisModuleString *first, *second;\n *      RedisModule_HashGet(mykey,REDISMODULE_HASH_NONE,argv[1],&first,\n *                          argv[2],&second,NULL);\n *\n * As with RedisModule_HashSet() the behavior of the command can be specified\n * passing flags different than REDISMODULE_HASH_NONE:\n *\n * REDISMODULE_HASH_CFIELDS: field names as null terminated C strings.\n *\n * REDISMODULE_HASH_EXISTS: instead of setting the value of the field\n * expecting a RedisModuleString pointer to pointer, the function just\n * reports if the field exists or not and expects an integer pointer\n * as the second element of each pair.\n *\n * Example of REDISMODULE_HASH_CFIELDS:\n *\n *      RedisModuleString *username, *hashedpass;\n *      RedisModule_HashGet(mykey,REDISMODULE_HASH_CFIELDS,\"username\",&username,\"hp\",&hashedpass, NULL);\n *\n * Example of REDISMODULE_HASH_EXISTS:\n *\n *      int exists;\n *      RedisModule_HashGet(mykey,REDISMODULE_HASH_EXISTS,argv[1],&exists,NULL);\n *\n * The function returns REDISMODULE_OK on success and REDISMODULE_ERR if\n * the key is not an hash value.\n *\n * Memory management:\n *\n * The returned RedisModuleString objects should be released with\n * RedisModule_FreeString(), or by enabling automatic memory management.\n */\nint RM_HashGet(RedisModuleKey *key, int flags, ...) {\n    va_list ap;\n    if (key->value && key->value->type != OBJ_HASH) return REDISMODULE_ERR;\n\n    va_start(ap, flags);\n    while(1) {\n        RedisModuleString *field, **valueptr;\n        int *existsptr;\n        /* Get the field object and the value pointer to pointer. */\n        if (flags & REDISMODULE_HASH_CFIELDS) {\n            char *cfield = va_arg(ap,char*);\n            if (cfield == NULL) break;\n            field = createRawStringObject(cfield,strlen(cfield));\n        } else {\n            field = va_arg(ap,RedisModuleString*);\n            if (field == NULL) break;\n        }\n\n        /* Query the hash for existence or value object. */\n        if (flags & REDISMODULE_HASH_EXISTS) {\n            existsptr = va_arg(ap,int*);\n            if (key->value)\n                *existsptr = hashTypeExists(key->value,szFromObj(field));\n            else\n                *existsptr = 0;\n        } else {\n            valueptr = va_arg(ap,RedisModuleString**);\n            if (key->value) {\n                *valueptr = hashTypeGetValueObject(key->value,szFromObj(field));\n                if (*valueptr) {\n                    robj *decoded = getDecodedObject(*valueptr);\n                    decrRefCount(*valueptr);\n                    *valueptr = decoded;\n                }\n                if (*valueptr)\n                    autoMemoryAdd(key->ctx,REDISMODULE_AM_STRING,*valueptr);\n            } else {\n                *valueptr = NULL;\n            }\n        }\n\n        /* Cleanup */\n        if (flags & REDISMODULE_HASH_CFIELDS) decrRefCount(field);\n    }\n    va_end(ap);\n    return REDISMODULE_OK;\n}\n\n/* --------------------------------------------------------------------------\n * ## Key API for Stream type\n *\n * For an introduction to streams, see https://redis.io/topics/streams-intro.\n *\n * The type RedisModuleStreamID, which is used in stream functions, is a struct\n * with two 64-bit fields and is defined as\n *\n *     typedef struct RedisModuleStreamID {\n *         uint64_t ms;\n *         uint64_t seq;\n *     } RedisModuleStreamID;\n *\n * See also RM_ValueLength(), which returns the length of a stream, and the\n * conversion functions RM_StringToStreamID() and RM_CreateStringFromStreamID().\n * -------------------------------------------------------------------------- */\n\n/* Adds an entry to a stream. Like XADD without trimming.\n *\n * - `key`: The key where the stream is (or will be) stored\n * - `flags`: A bit field of\n *   - `REDISMODULE_STREAM_ADD_AUTOID`: Assign a stream ID automatically, like\n *     `*` in the XADD command.\n * - `id`: If the `AUTOID` flag is set, this is where the assigned ID is\n *   returned. Can be NULL if `AUTOID` is set, if you don't care to receive the\n *   ID. If `AUTOID` is not set, this is the requested ID.\n * - `argv`: A pointer to an array of size `numfields * 2` containing the\n *   fields and values.\n * - `numfields`: The number of field-value pairs in `argv`.\n *\n * Returns REDISMODULE_OK if an entry has been added. On failure,\n * REDISMODULE_ERR is returned and `errno` is set as follows:\n *\n * - EINVAL if called with invalid arguments\n * - ENOTSUP if the key refers to a value of a type other than stream\n * - EBADF if the key was not opened for writing\n * - EDOM if the given ID was 0-0 or not greater than all other IDs in the\n *   stream (only if the AUTOID flag is unset)\n * - EFBIG if the stream has reached the last possible ID\n * - ERANGE if the elements are too large to be stored.\n */\nint RM_StreamAdd(RedisModuleKey *key, int flags, RedisModuleStreamID *id, RedisModuleString **argv, long numfields) {\n    /* Validate args */\n    if (!key || (numfields != 0 && !argv) || /* invalid key or argv */\n        (flags & ~(REDISMODULE_STREAM_ADD_AUTOID)) || /* invalid flags */\n        (!(flags & REDISMODULE_STREAM_ADD_AUTOID) && !id)) { /* id required */\n        errno = EINVAL;\n        return REDISMODULE_ERR;\n    } else if (key->value && key->value->type != OBJ_STREAM) {\n        errno = ENOTSUP; /* wrong type */\n        return REDISMODULE_ERR;\n    } else if (!(key->mode & REDISMODULE_WRITE)) {\n        errno = EBADF; /* key not open for writing */\n        return REDISMODULE_ERR;\n    } else if (!(flags & REDISMODULE_STREAM_ADD_AUTOID) &&\n               id->ms == 0 && id->seq == 0) {\n        errno = EDOM; /* ID out of range */\n        return REDISMODULE_ERR;\n    }\n\n    /* Create key if necessery */\n    int created = 0;\n    if (key->value == NULL) {\n        moduleCreateEmptyKey(key, REDISMODULE_KEYTYPE_STREAM);\n        created = 1;\n    }\n\n    stream *s = (stream*)ptrFromObj(key->value);\n    if (s->last_id.ms == UINT64_MAX && s->last_id.seq == UINT64_MAX) {\n        /* The stream has reached the last possible ID */\n        errno = EFBIG;\n        return REDISMODULE_ERR;\n    }\n\n    streamID added_id;\n    streamID use_id;\n    streamID *use_id_ptr = NULL;\n    if (!(flags & REDISMODULE_STREAM_ADD_AUTOID)) {\n        use_id.ms = id->ms;\n        use_id.seq = id->seq;\n        use_id_ptr = &use_id;\n    }\n    if (streamAppendItem(s, argv, numfields, &added_id, use_id_ptr) == C_ERR) {\n        /* Either the ID not greater than all existing IDs in the stream, or\n         * the elements are too large to be stored. either way, errno is already\n         * set by streamAppendItem. */\n        return REDISMODULE_ERR;\n    }\n    /* Postponed signalKeyAsReady(). Done implicitly by moduleCreateEmptyKey()\n     * so not needed if the stream has just been created. */\n    if (!created) key->u.stream.signalready = 1;\n\n    if (id != NULL) {\n        id->ms = added_id.ms;\n        id->seq = added_id.seq;\n    }\n\n    return REDISMODULE_OK;\n}\n\n/* Deletes an entry from a stream.\n *\n * - `key`: A key opened for writing, with no stream iterator started.\n * - `id`: The stream ID of the entry to delete.\n *\n * Returns REDISMODULE_OK on success. On failure, REDISMODULE_ERR is returned\n * and `errno` is set as follows:\n *\n * - EINVAL if called with invalid arguments\n * - ENOTSUP if the key refers to a value of a type other than stream or if the\n *   key is empty\n * - EBADF if the key was not opened for writing or if a stream iterator is\n *   associated with the key\n * - ENOENT if no entry with the given stream ID exists\n *\n * See also RM_StreamIteratorDelete() for deleting the current entry while\n * iterating using a stream iterator.\n */\nint RM_StreamDelete(RedisModuleKey *key, RedisModuleStreamID *id) {\n    if (!key || !id) {\n        errno = EINVAL;\n        return REDISMODULE_ERR;\n    } else if (!key->value || key->value->type != OBJ_STREAM) {\n        errno = ENOTSUP; /* wrong type */\n        return REDISMODULE_ERR;\n    } else if (!(key->mode & REDISMODULE_WRITE) ||\n               key->iter != NULL) {\n        errno = EBADF; /* key not opened for writing or iterator started */\n        return REDISMODULE_ERR;\n    }\n    stream *s = (stream*)ptrFromObj(key->value);\n    streamID streamid = {id->ms, id->seq};\n    if (streamDeleteItem(s, &streamid)) {\n        return REDISMODULE_OK;\n    } else {\n        errno = ENOENT; /* no entry with this id */\n        return REDISMODULE_ERR;\n    }\n}\n\n/* Sets up a stream iterator.\n *\n * - `key`: The stream key opened for reading using RedisModule_OpenKey().\n * - `flags`:\n *   - `REDISMODULE_STREAM_ITERATOR_EXCLUSIVE`: Don't include `start` and `end`\n *     in the iterated range.\n *   - `REDISMODULE_STREAM_ITERATOR_REVERSE`: Iterate in reverse order, starting\n *     from the `end` of the range.\n * - `start`: The lower bound of the range. Use NULL for the beginning of the\n *   stream.\n * - `end`: The upper bound of the range. Use NULL for the end of the stream.\n *\n * Returns REDISMODULE_OK on success. On failure, REDISMODULE_ERR is returned\n * and `errno` is set as follows:\n *\n * - EINVAL if called with invalid arguments\n * - ENOTSUP if the key refers to a value of a type other than stream or if the\n *   key is empty\n * - EBADF if the key was not opened for writing or if a stream iterator is\n *   already associated with the key\n * - EDOM if `start` or `end` is outside the valid range\n *\n * Returns REDISMODULE_OK on success and REDISMODULE_ERR if the key doesn't\n * refer to a stream or if invalid arguments were given.\n *\n * The stream IDs are retrieved using RedisModule_StreamIteratorNextID() and\n * for each stream ID, the fields and values are retrieved using\n * RedisModule_StreamIteratorNextField(). The iterator is freed by calling\n * RedisModule_StreamIteratorStop().\n *\n * Example (error handling omitted):\n *\n *     RedisModule_StreamIteratorStart(key, 0, startid_ptr, endid_ptr);\n *     RedisModuleStreamID id;\n *     long numfields;\n *     while (RedisModule_StreamIteratorNextID(key, &id, &numfields) ==\n *            REDISMODULE_OK) {\n *         RedisModuleString *field, *value;\n *         while (RedisModule_StreamIteratorNextField(key, &field, &value) ==\n *                REDISMODULE_OK) {\n *             //\n *             // ... Do stuff ...\n *             //\n *             RedisModule_FreeString(ctx, field);\n *             RedisModule_FreeString(ctx, value);\n *         }\n *     }\n *     RedisModule_StreamIteratorStop(key);\n */\nint RM_StreamIteratorStart(RedisModuleKey *key, int flags, RedisModuleStreamID *start, RedisModuleStreamID *end) {\n    /* check args */\n    if (!key ||\n        (flags & ~(REDISMODULE_STREAM_ITERATOR_EXCLUSIVE |\n                   REDISMODULE_STREAM_ITERATOR_REVERSE))) {\n        errno = EINVAL; /* key missing or invalid flags */\n        return REDISMODULE_ERR;\n    } else if (!key->value || key->value->type != OBJ_STREAM) {\n        errno = ENOTSUP;\n        return REDISMODULE_ERR; /* not a stream */\n    } else if (key->iter) {\n        errno = EBADF; /* iterator already started */\n        return REDISMODULE_ERR;\n    }\n\n    /* define range for streamIteratorStart() */\n    streamID lower, upper;\n    if (start) lower = {start->ms, start->seq};\n    if (end)   upper = {end->ms,   end->seq};\n    if (flags & REDISMODULE_STREAM_ITERATOR_EXCLUSIVE) {\n        if ((start && streamIncrID(&lower) != C_OK) ||\n            (end   && streamDecrID(&upper) != C_OK)) {\n            errno = EDOM; /* end is 0-0 or start is MAX-MAX? */\n            return REDISMODULE_ERR;\n        }\n    }\n\n    /* create iterator */\n    stream *s = (stream*)ptrFromObj(key->value);\n    int rev = flags & REDISMODULE_STREAM_ITERATOR_REVERSE;\n    streamIterator *si = (streamIterator*)zmalloc(sizeof(*si));\n    streamIteratorStart(si, s, start ? &lower : NULL, end ? &upper : NULL, rev);\n    key->iter = si;\n    key->u.stream.currentid.ms = 0; /* for RM_StreamIteratorDelete() */\n    key->u.stream.currentid.seq = 0;\n    key->u.stream.numfieldsleft = 0; /* for RM_StreamIteratorNextField() */\n    return REDISMODULE_OK;\n}\n\n/* Stops a stream iterator created using RedisModule_StreamIteratorStart() and\n * reclaims its memory.\n *\n * Returns REDISMODULE_OK on success. On failure, REDISMODULE_ERR is returned\n * and `errno` is set as follows:\n *\n * - EINVAL if called with a NULL key\n * - ENOTSUP if the key refers to a value of a type other than stream or if the\n *   key is empty\n * - EBADF if the key was not opened for writing or if no stream iterator is\n *   associated with the key\n */\nint RM_StreamIteratorStop(RedisModuleKey *key) {\n    if (!key) {\n        errno = EINVAL;\n        return REDISMODULE_ERR;\n    } else if (!key->value || key->value->type != OBJ_STREAM) {\n        errno = ENOTSUP;\n        return REDISMODULE_ERR;\n    } else if (!key->iter) {\n        errno = EBADF;\n        return REDISMODULE_ERR;\n    }\n    zfree(key->iter);\n    key->iter = NULL;\n    return REDISMODULE_OK;\n}\n\n/* Finds the next stream entry and returns its stream ID and the number of\n * fields.\n *\n * - `key`: Key for which a stream iterator has been started using\n *   RedisModule_StreamIteratorStart().\n * - `id`: The stream ID returned. NULL if you don't care.\n * - `numfields`: The number of fields in the found stream entry. NULL if you\n *   don't care.\n *\n * Returns REDISMODULE_OK and sets `*id` and `*numfields` if an entry was found.\n * On failure, REDISMODULE_ERR is returned and `errno` is set as follows:\n *\n * - EINVAL if called with a NULL key\n * - ENOTSUP if the key refers to a value of a type other than stream or if the\n *   key is empty\n * - EBADF if no stream iterator is associated with the key\n * - ENOENT if there are no more entries in the range of the iterator\n *\n * In practice, if RM_StreamIteratorNextID() is called after a successful call\n * to RM_StreamIteratorStart() and with the same key, it is safe to assume that\n * an REDISMODULE_ERR return value means that there are no more entries.\n *\n * Use RedisModule_StreamIteratorNextField() to retrieve the fields and values.\n * See the example at RedisModule_StreamIteratorStart().\n */\nint RM_StreamIteratorNextID(RedisModuleKey *key, RedisModuleStreamID *id, long *numfields) {\n    if (!key) {\n        errno = EINVAL;\n        return REDISMODULE_ERR;\n    } else if (!key->value || key->value->type != OBJ_STREAM) {\n        errno = ENOTSUP;\n        return REDISMODULE_ERR;\n    } else if (!key->iter) {\n        errno = EBADF;\n        return REDISMODULE_ERR;\n    }\n    streamIterator *si = (streamIterator*)key->iter;\n    int64_t *num_ptr = &key->u.stream.numfieldsleft;\n    streamID *streamid_ptr = &key->u.stream.currentid;\n    if (streamIteratorGetID(si, streamid_ptr, num_ptr)) {\n        if (id) {\n            id->ms = streamid_ptr->ms;\n            id->seq = streamid_ptr->seq;\n        }\n        if (numfields) *numfields = *num_ptr;\n        return REDISMODULE_OK;\n    } else {\n        /* No entry found. */\n        key->u.stream.currentid.ms = 0; /* for RM_StreamIteratorDelete() */\n        key->u.stream.currentid.seq = 0;\n        key->u.stream.numfieldsleft = 0; /* for RM_StreamIteratorNextField() */\n        errno = ENOENT;\n        return REDISMODULE_ERR;\n    }\n}\n\n/* Retrieves the next field of the current stream ID and its corresponding value\n * in a stream iteration. This function should be called repeatedly after calling\n * RedisModule_StreamIteratorNextID() to fetch each field-value pair.\n *\n * - `key`: Key where a stream iterator has been started.\n * - `field_ptr`: This is where the field is returned.\n * - `value_ptr`: This is where the value is returned.\n *\n * Returns REDISMODULE_OK and points `*field_ptr` and `*value_ptr` to freshly\n * allocated RedisModuleString objects. The string objects are freed\n * automatically when the callback finishes if automatic memory is enabled. On\n * failure, REDISMODULE_ERR is returned and `errno` is set as follows:\n *\n * - EINVAL if called with a NULL key\n * - ENOTSUP if the key refers to a value of a type other than stream or if the\n *   key is empty\n * - EBADF if no stream iterator is associated with the key\n * - ENOENT if there are no more fields in the current stream entry\n *\n * In practice, if RM_StreamIteratorNextField() is called after a successful\n * call to RM_StreamIteratorNextID() and with the same key, it is safe to assume\n * that an REDISMODULE_ERR return value means that there are no more fields.\n *\n * See the example at RedisModule_StreamIteratorStart().\n */\nint RM_StreamIteratorNextField(RedisModuleKey *key, RedisModuleString **field_ptr, RedisModuleString **value_ptr) {\n    if (!key) {\n        errno = EINVAL;\n        return REDISMODULE_ERR;\n    } else if (!key->value || key->value->type != OBJ_STREAM) {\n        errno = ENOTSUP;\n        return REDISMODULE_ERR;\n    } else if (!key->iter) {\n        errno = EBADF;\n        return REDISMODULE_ERR;\n    } else if (key->u.stream.numfieldsleft <= 0) {\n        errno = ENOENT;\n        return REDISMODULE_ERR;\n    }\n    streamIterator *si = (streamIterator*)key->iter;\n    unsigned char *field, *value;\n    int64_t field_len, value_len;\n    streamIteratorGetField(si, &field, &value, &field_len, &value_len);\n    if (field_ptr) {\n        *field_ptr = createRawStringObject((char *)field, field_len);\n        autoMemoryAdd(key->ctx, REDISMODULE_AM_STRING, *field_ptr);\n    }\n    if (value_ptr) {\n        *value_ptr = createRawStringObject((char *)value, value_len);\n        autoMemoryAdd(key->ctx, REDISMODULE_AM_STRING, *value_ptr);\n    }\n    key->u.stream.numfieldsleft--;\n    return REDISMODULE_OK;\n}\n\n/* Deletes the current stream entry while iterating.\n *\n * This function can be called after RM_StreamIteratorNextID() or after any\n * calls to RM_StreamIteratorNextField().\n *\n * Returns REDISMODULE_OK on success. On failure, REDISMODULE_ERR is returned\n * and `errno` is set as follows:\n *\n * - EINVAL if key is NULL\n * - ENOTSUP if the key is empty or is of another type than stream\n * - EBADF if the key is not opened for writing, if no iterator has been started\n * - ENOENT if the iterator has no current stream entry\n */\nint RM_StreamIteratorDelete(RedisModuleKey *key) {\n    if (!key) {\n        errno = EINVAL;\n        return REDISMODULE_ERR;\n    } else if (!key->value || key->value->type != OBJ_STREAM) {\n        errno = ENOTSUP;\n        return REDISMODULE_ERR;\n    } else if (!(key->mode & REDISMODULE_WRITE) || !key->iter) {\n        errno = EBADF;\n        return REDISMODULE_ERR;\n    } else if (key->u.stream.currentid.ms == 0 &&\n               key->u.stream.currentid.seq == 0) {\n        errno = ENOENT;\n        return REDISMODULE_ERR;\n    }\n    streamIterator *si = (streamIterator*)key->iter;\n    streamIteratorRemoveEntry(si, &key->u.stream.currentid);\n    key->u.stream.currentid.ms = 0; /* Make sure repeated Delete() fails */\n    key->u.stream.currentid.seq = 0;\n    key->u.stream.numfieldsleft = 0; /* Make sure NextField() fails */\n    return REDISMODULE_OK;\n}\n\n/* Trim a stream by length, similar to XTRIM with MAXLEN.\n *\n * - `key`: Key opened for writing.\n * - `flags`: A bitfield of\n *   - `REDISMODULE_STREAM_TRIM_APPROX`: Trim less if it improves performance,\n *     like XTRIM with `~`.\n * - `length`: The number of stream entries to keep after trimming.\n *\n * Returns the number of entries deleted. On failure, a negative value is\n * returned and `errno` is set as follows:\n *\n * - EINVAL if called with invalid arguments\n * - ENOTSUP if the key is empty or of a type other than stream\n * - EBADF if the key is not opened for writing\n */\nlong long RM_StreamTrimByLength(RedisModuleKey *key, int flags, long long length) {\n    if (!key || (flags & ~(REDISMODULE_STREAM_TRIM_APPROX)) || length < 0) {\n        errno = EINVAL;\n        return -1;\n    } else if (!key->value || key->value->type != OBJ_STREAM) {\n        errno = ENOTSUP;\n        return -1;\n    } else if (!(key->mode & REDISMODULE_WRITE)) {\n        errno = EBADF;\n        return -1;\n    }\n    int approx = flags & REDISMODULE_STREAM_TRIM_APPROX ? 1 : 0;\n    return streamTrimByLength((stream *)ptrFromObj(key->value), length, approx);\n}\n\n/* Trim a stream by ID, similar to XTRIM with MINID.\n *\n * - `key`: Key opened for writing.\n * - `flags`: A bitfield of\n *   - `REDISMODULE_STREAM_TRIM_APPROX`: Trim less if it improves performance,\n *     like XTRIM with `~`.\n * - `id`: The smallest stream ID to keep after trimming.\n *\n * Returns the number of entries deleted. On failure, a negative value is\n * returned and `errno` is set as follows:\n *\n * - EINVAL if called with invalid arguments\n * - ENOTSUP if the key is empty or of a type other than stream\n * - EBADF if the key is not opened for writing\n */\nlong long RM_StreamTrimByID(RedisModuleKey *key, int flags, RedisModuleStreamID *id) {\n    if (!key || (flags & ~(REDISMODULE_STREAM_TRIM_APPROX)) || !id) {\n        errno = EINVAL;\n        return -1;\n    } else if (!key->value || key->value->type != OBJ_STREAM) {\n        errno = ENOTSUP;\n        return -1;\n    } else if (!(key->mode & REDISMODULE_WRITE)) {\n        errno = EBADF;\n        return -1;\n    }\n    int approx = flags & REDISMODULE_STREAM_TRIM_APPROX ? 1 : 0;\n    streamID minid = {id->ms, id->seq};\n    return streamTrimByID((stream *)ptrFromObj(key->value), minid, approx);\n}\n\n/* --------------------------------------------------------------------------\n * ## Calling Redis commands from modules\n *\n * RM_Call() sends a command to Redis. The remaining functions handle the reply.\n * -------------------------------------------------------------------------- */\n\n/* Create a new RedisModuleCallReply object. The processing of the reply\n * is lazy, the object is just populated with the raw protocol and later\n * is processed as needed. Initially we just make sure to set the right\n * reply type, which is extremely cheap to do. */\nRedisModuleCallReply *moduleCreateCallReplyFromProto(RedisModuleCtx *ctx, sds proto) {\n    RedisModuleCallReply *reply = (RedisModuleCallReply*)zmalloc(sizeof(*reply), MALLOC_LOCAL);\n    reply->ctx = ctx;\n    reply->proto = proto;\n    reply->protolen = sdslen(proto);\n    reply->flags = REDISMODULE_REPLYFLAG_TOPARSE; /* Lazy parsing. */\n    switch(proto[0]) {\n    case '$':\n    case '+': reply->type = REDISMODULE_REPLY_STRING; break;\n    case '-': reply->type = REDISMODULE_REPLY_ERROR; break;\n    case ':': reply->type = REDISMODULE_REPLY_INTEGER; break;\n    case '*': reply->type = REDISMODULE_REPLY_ARRAY; break;\n    default: reply->type = REDISMODULE_REPLY_UNKNOWN; break;\n    }\n    if ((proto[0] == '*' || proto[0] == '$') && proto[1] == '-')\n        reply->type = REDISMODULE_REPLY_NULL;\n    return reply;\n}\n\nvoid moduleParseCallReply_Int(RedisModuleCallReply *reply);\nvoid moduleParseCallReply_BulkString(RedisModuleCallReply *reply);\nvoid moduleParseCallReply_SimpleString(RedisModuleCallReply *reply);\nvoid moduleParseCallReply_Array(RedisModuleCallReply *reply);\n\n/* Do nothing if REDISMODULE_REPLYFLAG_TOPARSE is false, otherwise\n * use the protocol of the reply in reply->proto in order to fill the\n * reply with parsed data according to the reply type. */\nvoid moduleParseCallReply(RedisModuleCallReply *reply) {\n    if (!(reply->flags & REDISMODULE_REPLYFLAG_TOPARSE)) return;\n    reply->flags &= ~REDISMODULE_REPLYFLAG_TOPARSE;\n\n    switch(reply->proto[0]) {\n    case ':': moduleParseCallReply_Int(reply); break;\n    case '$': moduleParseCallReply_BulkString(reply); break;\n    case '-': /* handled by next item. */\n    case '+': moduleParseCallReply_SimpleString(reply); break;\n    case '*': moduleParseCallReply_Array(reply); break;\n    }\n}\n\nvoid moduleParseCallReply_Int(RedisModuleCallReply *reply) {\n    char *proto = reply->proto;\n    char *p = strchr(proto+1,'\\r');\n\n    string2ll(proto+1,p-proto-1,&reply->val.ll);\n    reply->protolen = p-proto+2;\n    reply->type = REDISMODULE_REPLY_INTEGER;\n}\n\nvoid moduleParseCallReply_BulkString(RedisModuleCallReply *reply) {\n    char *proto = reply->proto;\n    char *p = strchr(proto+1,'\\r');\n    long long bulklen;\n\n    string2ll(proto+1,p-proto-1,&bulklen);\n    if (bulklen == -1) {\n        reply->protolen = p-proto+2;\n        reply->type = REDISMODULE_REPLY_NULL;\n    } else {\n        reply->val.str = p+2;\n        reply->len = bulklen;\n        reply->protolen = p-proto+2+bulklen+2;\n        reply->type = REDISMODULE_REPLY_STRING;\n    }\n}\n\nvoid moduleParseCallReply_SimpleString(RedisModuleCallReply *reply) {\n    char *proto = reply->proto;\n    char *p = strchr(proto+1,'\\r');\n\n    reply->val.str = proto+1;\n    reply->len = p-proto-1;\n    reply->protolen = p-proto+2;\n    reply->type = proto[0] == '+' ? REDISMODULE_REPLY_STRING :\n                                    REDISMODULE_REPLY_ERROR;\n}\n\nvoid moduleParseCallReply_Array(RedisModuleCallReply *reply) {\n    char *proto = reply->proto;\n    char *p = strchr(proto+1,'\\r');\n    long long arraylen, j;\n\n    string2ll(proto+1,p-proto-1,&arraylen);\n    p += 2;\n\n    if (arraylen == -1) {\n        reply->protolen = p-proto;\n        reply->type = REDISMODULE_REPLY_NULL;\n        return;\n    }\n\n    reply->val.array = (RedisModuleCallReply*)zmalloc(sizeof(RedisModuleCallReply)*arraylen, MALLOC_LOCAL);\n    reply->len = arraylen;\n    for (j = 0; j < arraylen; j++) {\n        RedisModuleCallReply *ele = reply->val.array+j;\n        ele->flags = REDISMODULE_REPLYFLAG_NESTED |\n                     REDISMODULE_REPLYFLAG_TOPARSE;\n        ele->proto = p;\n        ele->ctx = reply->ctx;\n        moduleParseCallReply(ele);\n        p += ele->protolen;\n    }\n    reply->protolen = p-proto;\n    reply->type = REDISMODULE_REPLY_ARRAY;\n}\n\n/* Recursive free reply function. */\nvoid moduleFreeCallReplyRec(RedisModuleCallReply *reply, int freenested){\n    /* Don't free nested replies by default: the user must always free the\n     * toplevel reply. However be gentle and don't crash if the module\n     * misuses the API. */\n    if (!freenested && reply->flags & REDISMODULE_REPLYFLAG_NESTED) return;\n\n    if (!(reply->flags & REDISMODULE_REPLYFLAG_TOPARSE)) {\n        if (reply->type == REDISMODULE_REPLY_ARRAY) {\n            size_t j;\n            for (j = 0; j < reply->len; j++)\n                moduleFreeCallReplyRec(reply->val.array+j,1);\n            zfree(reply->val.array);\n        }\n    }\n\n    /* For nested replies, we don't free reply->proto (which if not NULL\n     * references the parent reply->proto buffer), nor the structure\n     * itself which is allocated as an array of structures, and is freed\n     * when the array value is released. */\n    if (!(reply->flags & REDISMODULE_REPLYFLAG_NESTED)) {\n        if (reply->proto) sdsfree(reply->proto);\n        zfree(reply);\n    }\n}\n\n/* Free a Call reply and all the nested replies it contains if it's an\n * array. */\nvoid RM_FreeCallReply(RedisModuleCallReply *reply) {\n    /* This is a wrapper for the recursive free reply function. This is needed\n     * in order to have the first level function to return on nested replies,\n     * but only if called by the module API. */\n    RedisModuleCtx *ctx = reply->ctx;\n    moduleFreeCallReplyRec(reply,0);\n    autoMemoryFreed(ctx,REDISMODULE_AM_REPLY,reply);\n}\n\n/* Return the reply type. */\nint RM_CallReplyType(RedisModuleCallReply *reply) {\n    if (!reply) return REDISMODULE_REPLY_UNKNOWN;\n    return reply->type;\n}\n\n/* Return the reply type length, where applicable. */\nsize_t RM_CallReplyLength(RedisModuleCallReply *reply) {\n    moduleParseCallReply(reply);\n    switch(reply->type) {\n    case REDISMODULE_REPLY_STRING:\n    case REDISMODULE_REPLY_ERROR:\n    case REDISMODULE_REPLY_ARRAY:\n        return reply->len;\n    default:\n        return 0;\n    }\n}\n\n/* Return the 'idx'-th nested call reply element of an array reply, or NULL\n * if the reply type is wrong or the index is out of range. */\nRedisModuleCallReply *RM_CallReplyArrayElement(RedisModuleCallReply *reply, size_t idx) {\n    moduleParseCallReply(reply);\n    if (reply->type != REDISMODULE_REPLY_ARRAY) return NULL;\n    if (idx >= reply->len) return NULL;\n    return reply->val.array+idx;\n}\n\n/* Return the long long of an integer reply. */\nlong long RM_CallReplyInteger(RedisModuleCallReply *reply) {\n    moduleParseCallReply(reply);\n    if (reply->type != REDISMODULE_REPLY_INTEGER) return LLONG_MIN;\n    return reply->val.ll;\n}\n\n/* Return the pointer and length of a string or error reply. */\nconst char *RM_CallReplyStringPtr(RedisModuleCallReply *reply, size_t *len) {\n    moduleParseCallReply(reply);\n    if (reply->type != REDISMODULE_REPLY_STRING &&\n        reply->type != REDISMODULE_REPLY_ERROR) return NULL;\n    if (len) *len = reply->len;\n    return reply->val.str;\n}\n\n/* Return a new string object from a call reply of type string, error or\n * integer. Otherwise (wrong reply type) return NULL. */\nRedisModuleString *RM_CreateStringFromCallReply(RedisModuleCallReply *reply) {\n    moduleParseCallReply(reply);\n    switch(reply->type) {\n    case REDISMODULE_REPLY_STRING:\n    case REDISMODULE_REPLY_ERROR:\n        return RM_CreateString(reply->ctx,reply->val.str,reply->len);\n    case REDISMODULE_REPLY_INTEGER: {\n        char buf[64];\n        int len = ll2string(buf,sizeof(buf),reply->val.ll);\n        return RM_CreateString(reply->ctx,buf,len);\n        }\n    default: return NULL;\n    }\n}\n\n/* Returns an array of robj pointers, and populates *argc with the number\n * of items, by parsing the format specifier \"fmt\" as described for\n * the RM_Call(), RM_Replicate() and other module APIs.\n *\n * The integer pointed by 'flags' is populated with flags according\n * to special modifiers in \"fmt\". For now only one exists:\n *\n *     \"!\" -> REDISMODULE_ARGV_REPLICATE\n *     \"A\" -> REDISMODULE_ARGV_NO_AOF\n *     \"R\" -> REDISMODULE_ARGV_NO_REPLICAS\n *\n * On error (format specifier error) NULL is returned and nothing is\n * allocated. On success the argument vector is returned. */\nrobj **moduleCreateArgvFromUserFormat(const char *cmdname, const char *fmt, int *argcp, int *flags, va_list ap) {\n    int argc = 0, argv_size, j;\n    robj **argv = NULL;\n\n    /* As a first guess to avoid useless reallocations, size argv to\n     * hold one argument for each char specifier in 'fmt'. */\n    argv_size = strlen(fmt)+1; /* +1 because of the command name. */\n    argv = (robj**)zrealloc(argv,sizeof(robj*)*argv_size, MALLOC_LOCAL);\n\n    /* Build the arguments vector based on the format specifier. */\n    argv[0] = createStringObject(cmdname,strlen(cmdname));\n    argc++;\n\n    /* Create the client and dispatch the command. */\n    const char *p = fmt;\n    while(*p) {\n        if (*p == 'c') {\n            char *cstr = va_arg(ap,char*);\n            argv[argc++] = createStringObject(cstr,strlen(cstr));\n        } else if (*p == 's') {\n            robj *obj = (robj*)va_arg(ap,void*);\n            if (obj->getrefcount(std::memory_order_relaxed) == OBJ_STATIC_REFCOUNT)\n                obj = createStringObject(szFromObj(obj),sdslen(szFromObj(obj)));\n            else\n                incrRefCount(obj);\n            argv[argc++] = obj;\n        } else if (*p == 'b') {\n            char *buf = va_arg(ap,char*);\n            size_t len = va_arg(ap,size_t);\n            argv[argc++] = createStringObject(buf,len);\n        } else if (*p == 'l') {\n            long long ll = va_arg(ap,long long);\n            argv[argc++] = createObject(OBJ_STRING,sdsfromlonglong(ll));\n        } else if (*p == 'v') {\n             /* A vector of strings */\n             robj **v = (robj**)va_arg(ap, void*);\n             size_t vlen = va_arg(ap, size_t);\n\n             /* We need to grow argv to hold the vector's elements.\n              * We resize by vector_len-1 elements, because we held\n              * one element in argv for the vector already */\n             argv_size += vlen-1;\n             argv = (robj**)zrealloc(argv,sizeof(robj*)*argv_size, MALLOC_LOCAL);\n\n             size_t i = 0;\n             for (i = 0; i < vlen; i++) {\n                 incrRefCount(v[i]);\n                 argv[argc++] = v[i];\n             }\n        } else if (*p == '!') {\n            if (flags) (*flags) |= REDISMODULE_ARGV_REPLICATE;\n        } else if (*p == 'A') {\n            if (flags) (*flags) |= REDISMODULE_ARGV_NO_AOF;\n        } else if (*p == 'R') {\n            if (flags) (*flags) |= REDISMODULE_ARGV_NO_REPLICAS;\n        } else {\n            goto fmterr;\n        }\n        p++;\n    }\n    *argcp = argc;\n    return argv;\n\nfmterr:\n    for (j = 0; j < argc; j++)\n        decrRefCount(argv[j]);\n    zfree(argv);\n    return NULL;\n}\n\n/* Exported API to call any KeyDB command from modules.\n *\n * * **cmdname**: The Redis command to call.\n * * **fmt**: A format specifier string for the command's arguments. Each\n *   of the arguments should be specified by a valid type specification. The\n *   format specifier can also contain the modifiers `!`, `A` and `R` which\n *   don't have a corresponding argument.\n *\n *     * `b` -- The argument is a buffer and is immediately followed by another\n *              argument that is the buffer's length.\n *     * `c` -- The argument is a pointer to a plain C string (null-terminated).\n *     * `l` -- The argument is long long integer.\n *     * `s` -- The argument is a RedisModuleString.\n *     * `v` -- The argument(s) is a vector of RedisModuleString.\n *     * `!` -- Sends the Redis command and its arguments to replicas and AOF.\n *     * `A` -- Suppress AOF propagation, send only to replicas (requires `!`).\n *     * `R` -- Suppress replicas propagation, send only to AOF (requires `!`).\n * * **...**: The actual arguments to the Redis command.\n *\n * On success a RedisModuleCallReply object is returned, otherwise\n * NULL is returned and errno is set to the following values:\n *\n * * EBADF: wrong format specifier.\n * * EINVAL: wrong command arity.\n * * ENOENT: command does not exist.\n * * EPERM: operation in Cluster instance with key in non local slot.\n * * EROFS: operation in Cluster instance when a write command is sent\n *          in a readonly state.\n * * ENETDOWN: operation in Cluster instance when cluster is down.\n *\n * Example code fragment:\n * \n *      reply = RedisModule_Call(ctx,\"INCRBY\",\"sc\",argv[1],\"10\");\n *      if (RedisModule_CallReplyType(reply) == REDISMODULE_REPLY_INTEGER) {\n *        long long myval = RedisModule_CallReplyInteger(reply);\n *        // Do something with myval.\n *      }\n *\n * Example code fragment:\n * \n *      reply = RedisModule_Call(ctx,\"INCRBY\",\"sc\",argv[1],\"10\");\n *      if (RedisModule_CallReplyType(reply) == REDISMODULE_REPLY_INTEGER) {\n *        long long myval = RedisModule_CallReplyInteger(reply);\n *        // Do something with myval.\n *      }\n *\n * This API is documented here: https://redis.io/topics/modules-intro\n */\nRedisModuleCallReply *RM_Call(RedisModuleCtx *ctx, const char *cmdname, const char *fmt, ...) {\n    struct redisCommand *cmd;\n    client *c = NULL;\n    robj **argv = NULL;\n    int argc = 0, flags = 0;\n    va_list ap;\n    RedisModuleCallReply *reply = NULL;\n    int replicate = 0; /* Replicate this command? */\n    int call_flags;\n    sds proto = nullptr;\n    int prev_replication_allowed;\n\n    /* Handle arguments. */\n    va_start(ap, fmt);\n    argv = moduleCreateArgvFromUserFormat(cmdname,fmt,&argc,&flags,ap);\n    replicate = flags & REDISMODULE_ARGV_REPLICATE;\n    va_end(ap);\n\n    /* Setup our fake client for command execution. */\n    if (g_pserver->module_client == NULL) {\n        /* This is the first RM_Call() ever. Create reusable client. */\n        c = g_pserver->module_client = createClient(NULL, IDX_EVENT_LOOP_MAIN);\n    } else if (g_pserver->module_client->argv == NULL) {\n        /* The reusable module client is not busy with a command. Use it. */\n        c = g_pserver->module_client;\n    } else {\n        /* The reusable module client is busy. (It is probably used in a\n         * recursive call to this module.) */\n        c = createClient(NULL, IDX_EVENT_LOOP_MAIN);\n    }\n    c->user = NULL; /* Root user. */\n    c->flags = CLIENT_MODULE;\n\n    /* We do not want to allow block, the module do not expect it */\n    c->flags |= CLIENT_DENY_BLOCKING;\n    c->db = ctx->client->db;\n    c->argv = argv;\n    c->argc = argc;\n    if (ctx->module) ctx->module->in_call++;\n\n    /* We handle the above format error only when the client is setup so that\n     * we can free it normally. */\n    if (argv == NULL) {\n        errno = EBADF;\n        goto cleanup;\n    }\n\n    /* Call command filters */\n    moduleCallCommandFilters(c);\n\n    /* Lookup command now, after filters had a chance to make modifications\n     * if necessary.\n     */\n    cmd = lookupCommand(szFromObj(c->argv[0]));\n    if (!cmd) {\n        errno = ENOENT;\n        goto cleanup;\n    }\n    c->cmd = c->lastcmd = cmd;\n\n    /* Basic arity checks. */\n    if ((cmd->arity > 0 && cmd->arity != argc) || (argc < -cmd->arity)) {\n        errno = EINVAL;\n        goto cleanup;\n    }\n\n    /* If this is a Redis Cluster node, we need to make sure the module is not\n     * trying to access non-local keys, with the exception of commands\n     * received from our master. */\n    if (g_pserver->cluster_enabled && !(ctx->client->flags & CLIENT_MASTER)) {\n        int error_code;\n        /* Duplicate relevant flags in the module client. */\n        c->flags &= ~(CLIENT_READONLY|CLIENT_ASKING);\n        c->flags |= ctx->client->flags & (CLIENT_READONLY|CLIENT_ASKING);\n        if (getNodeByQuery(c,c->cmd,c->argv,c->argc,NULL,&error_code) !=\n                           g_pserver->cluster->myself)\n        {\n            if (error_code == CLUSTER_REDIR_DOWN_RO_STATE) { \n                errno = EROFS;\n            } else if (error_code == CLUSTER_REDIR_DOWN_STATE) { \n                errno = ENETDOWN;\n            } else {\n                errno = EPERM;\n            }\n            goto cleanup;\n        }\n    }\n\n    /* We need to use a global replication_allowed flag in order to prevent\n     * replication of nested RM_Calls. Example:\n     * 1. module1.foo does RM_Call of module2.bar without replication (i.e. no '!')\n     * 2. module2.bar internally calls RM_Call of INCR with '!'\n     * 3. at the end of module1.foo we call RM_ReplicateVerbatim\n     * We want the replica/AOF to see only module1.foo and not the INCR from module2.bar */\n    prev_replication_allowed = g_pserver->replication_allowed;\n    g_pserver->replication_allowed = replicate && g_pserver->replication_allowed;\n\n    /* Run the command */\n    call_flags = CMD_CALL_SLOWLOG | CMD_CALL_STATS | CMD_CALL_NOWRAP;\n    if (replicate) {\n        /* If we are using single commands replication, we need to wrap what\n         * we propagate into a MULTI/EXEC block, so that it will be atomic like\n         * a Lua script in the context of AOF and slaves. */\n        moduleReplicateMultiIfNeeded(ctx);\n\n        if (!(flags & REDISMODULE_ARGV_NO_AOF))\n            call_flags |= CMD_CALL_PROPAGATE_AOF;\n        if (!(flags & REDISMODULE_ARGV_NO_REPLICAS))\n            call_flags |= CMD_CALL_PROPAGATE_REPL;\n    }\n\n    {\n    AeLocker locker; locker.arm(nullptr);\n    std::unique_lock<fastlock> ul(c->lock);\n    call(c,call_flags);\n    }\n\n    g_pserver->replication_allowed = prev_replication_allowed;\n\n    serverAssert((c->flags & CLIENT_BLOCKED) == 0);\n\n    /* Convert the result of the Redis command into a module reply. */\n    proto = sdsnewlen(c->buf,c->bufpos);\n    c->bufpos = 0;\n    while(listLength(c->reply)) {\n        clientReplyBlock *o = (clientReplyBlock*)listNodeValue(listFirst(c->reply));\n\n        proto = sdscatlen(proto,o->buf(),o->used);\n        listDelNode(c->reply,listFirst(c->reply));\n    }\n    reply = moduleCreateCallReplyFromProto(ctx,proto);\n    autoMemoryAdd(ctx,REDISMODULE_AM_REPLY,reply);\n\ncleanup:\n    if (ctx->module) ctx->module->in_call--;\n    if (c == g_pserver->module_client) {\n        /* reset shared client so it can be reused */\n        discardTransaction(c);\n        pubsubUnsubscribeAllChannels(c,0);\n        pubsubUnsubscribeAllPatterns(c,0);\n        resetClient(c); /* frees the contents of argv */\n        zfree(c->argv);\n        c->argv = NULL;\n        c->resp = 2;\n    } else {\n        freeClient(c); /* temporary client */\n    }\n    return reply;\n}\n\n/* Return a pointer, and a length, to the protocol returned by the command\n * that returned the reply object. */\nconst char *RM_CallReplyProto(RedisModuleCallReply *reply, size_t *len) {\n    if (reply->proto) *len = sdslen(reply->proto);\n    return reply->proto;\n}\n\n/* --------------------------------------------------------------------------\n * ## Modules data types\n *\n * When String DMA or using existing data structures is not enough, it is\n * possible to create new data types from scratch and export them to\n * Redis. The module must provide a set of callbacks for handling the\n * new values exported (for example in order to provide RDB saving/loading,\n * AOF rewrite, and so forth). In this section we define this API.\n * -------------------------------------------------------------------------- */\n\n/* Turn a 9 chars name in the specified charset and a 10 bit encver into\n * a single 64 bit unsigned integer that represents this exact module name\n * and version. This final number is called a \"type ID\" and is used when\n * writing module exported values to RDB files, in order to re-associate the\n * value to the right module to load them during RDB loading.\n *\n * If the string is not of the right length or the charset is wrong, or\n * if encver is outside the unsigned 10 bit integer range, 0 is returned,\n * otherwise the function returns the right type ID.\n *\n * The resulting 64 bit integer is composed as follows:\n *\n *     (high order bits) 6|6|6|6|6|6|6|6|6|10 (low order bits)\n *\n * The first 6 bits value is the first character, name[0], while the last\n * 6 bits value, immediately before the 10 bits integer, is name[8].\n * The last 10 bits are the encoding version.\n *\n * Note that a name and encver combo of \"AAAAAAAAA\" and 0, will produce\n * zero as return value, that is the same we use to signal errors, thus\n * this combination is invalid, and also useless since type names should\n * try to be vary to avoid collisions. */\n\nconst char *ModuleTypeNameCharSet =\n             \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n             \"abcdefghijklmnopqrstuvwxyz\"\n             \"0123456789-_\";\n\nuint64_t moduleTypeEncodeId(const char *name, int encver) {\n    /* We use 64 symbols so that we can map each character into 6 bits\n     * of the final output. */\n    const char *cset = ModuleTypeNameCharSet;\n    if (strlen(name) != 9) return 0;\n    if (encver < 0 || encver > 1023) return 0;\n\n    uint64_t id = 0;\n    for (int j = 0; j < 9; j++) {\n        const char *p = strchr(cset,name[j]);\n        if (!p) return 0;\n        unsigned long pos = p-cset;\n        id = (id << 6) | pos;\n    }\n    id = (id << 10) | encver;\n    return id;\n}\n\n/* Search, in the list of exported data types of all the modules registered,\n * a type with the same name as the one given. Returns the moduleType\n * structure pointer if such a module is found, or NULL otherwise. */\nmoduleType *moduleTypeLookupModuleByName(const char *name) {\n    dictIterator *di = dictGetIterator(modules);\n    dictEntry *de;\n\n    while ((de = dictNext(di)) != NULL) {\n        struct RedisModule *module = (RedisModule*)dictGetVal(de);\n        listIter li;\n        listNode *ln;\n\n        listRewind(module->types,&li);\n        while((ln = listNext(&li))) {\n            moduleType *mt = (moduleType*)ln->value;\n            if (memcmp(name,mt->name,sizeof(mt->name)) == 0) {\n                dictReleaseIterator(di);\n                return mt;\n            }\n        }\n    }\n    dictReleaseIterator(di);\n    return NULL;\n}\n\n/* Lookup a module by ID, with caching. This function is used during RDB\n * loading. Modules exporting data types should never be able to unload, so\n * our cache does not need to expire. */\n#define MODULE_LOOKUP_CACHE_SIZE 3\n\nmoduleType *moduleTypeLookupModuleByID(uint64_t id) {\n    static struct {\n        uint64_t id;\n        moduleType *mt;\n    } cache[MODULE_LOOKUP_CACHE_SIZE];\n\n    /* Search in cache to start. */\n    int j;\n    for (j = 0; j < MODULE_LOOKUP_CACHE_SIZE && cache[j].mt != NULL; j++)\n        if (cache[j].id == id) return cache[j].mt;\n\n    /* Slow module by module lookup. */\n    moduleType *mt = NULL;\n    dictIterator *di = dictGetIterator(modules);\n    dictEntry *de;\n\n    while ((de = dictNext(di)) != NULL && mt == NULL) {\n        struct RedisModule *module = (RedisModule*)dictGetVal(de);\n        listIter li;\n        listNode *ln;\n\n        listRewind(module->types,&li);\n        while((ln = listNext(&li))) {\n            moduleType *this_mt = (moduleType*)ln->value;\n            /* Compare only the 54 bit module identifier and not the\n             * encoding version. */\n            if (this_mt->id >> 10 == id >> 10) {\n                mt = this_mt;\n                break;\n            }\n        }\n    }\n    dictReleaseIterator(di);\n\n    /* Add to cache if possible. */\n    if (mt && j < MODULE_LOOKUP_CACHE_SIZE) {\n        cache[j].id = id;\n        cache[j].mt = mt;\n    }\n    return mt;\n}\n\n/* Turn an (unresolved) module ID into a type name, to show the user an\n * error when RDB files contain module data we can't load.\n * The buffer pointed by 'name' must be 10 bytes at least. The function will\n * fill it with a null terminated module name. */\nvoid moduleTypeNameByID(char *name, uint64_t moduleid) {\n    const char *cset = ModuleTypeNameCharSet;\n\n    name[9] = '\\0';\n    char *p = name+8;\n    moduleid >>= 10;\n    for (int j = 0; j < 9; j++) {\n        *p-- = cset[moduleid & 63];\n        moduleid >>= 6;\n    }\n}\n\n/* Return the name of the module that owns the specified moduleType. */\nconst char *moduleTypeModuleName(moduleType *mt) {\n    if (!mt || !mt->module) return NULL;\n    return mt->module->name;\n}\n\n/* Create a copy of a module type value using the copy callback. If failed\n * or not supported, produce an error reply and return NULL.\n */\nrobj *moduleTypeDupOrReply(client *c, robj *fromkey, robj *tokey, robj *value) {\n    moduleValue *mv = (moduleValue*)ptrFromObj(value);\n    moduleType *mt = mv->type;\n    if (!mt->copy) {\n        addReplyError(c, \"not supported for this module key\");\n        return NULL;\n    }\n    void *newval = mt->copy(fromkey, tokey, mv->value);\n    if (!newval) {\n        addReplyError(c, \"module key failed to copy\");\n        return NULL;\n    }\n    return createModuleObject(mt, newval);\n}\n\n/* Register a new data type exported by the module. The parameters are the\n * following. Please for in depth documentation check the modules API\n * documentation, especially https://redis.io/topics/modules-native-types.\n *\n * * **name**: A 9 characters data type name that MUST be unique in the Redis\n *   Modules ecosystem. Be creative... and there will be no collisions. Use\n *   the charset A-Z a-z 9-0, plus the two \"-_\" characters. A good\n *   idea is to use, for example `<typename>-<vendor>`. For example\n *   \"tree-AntZ\" may mean \"Tree data structure by @antirez\". To use both\n *   lower case and upper case letters helps in order to prevent collisions.\n * * **encver**: Encoding version, which is, the version of the serialization\n *   that a module used in order to persist data. As long as the \"name\"\n *   matches, the RDB loading will be dispatched to the type callbacks\n *   whatever 'encver' is used, however the module can understand if\n *   the encoding it must load are of an older version of the module.\n *   For example the module \"tree-AntZ\" initially used encver=0. Later\n *   after an upgrade, it started to serialize data in a different format\n *   and to register the type with encver=1. However this module may\n *   still load old data produced by an older version if the rdb_load\n *   callback is able to check the encver value and act accordingly.\n *   The encver must be a positive value between 0 and 1023.\n *\n * * **typemethods_ptr** is a pointer to a RedisModuleTypeMethods structure\n *   that should be populated with the methods callbacks and structure\n *   version, like in the following example:\n *\n *         RedisModuleTypeMethods tm = {\n *             .version = REDISMODULE_TYPE_METHOD_VERSION,\n *             .rdb_load = myType_RDBLoadCallBack,\n *             .rdb_save = myType_RDBSaveCallBack,\n *             .aof_rewrite = myType_AOFRewriteCallBack,\n *             .free = myType_FreeCallBack,\n *\n *             // Optional fields\n *             .digest = myType_DigestCallBack,\n *             .mem_usage = myType_MemUsageCallBack,\n *             .aux_load = myType_AuxRDBLoadCallBack,\n *             .aux_save = myType_AuxRDBSaveCallBack,\n *             .free_effort = myType_FreeEffortCallBack,\n *             .unlink = myType_UnlinkCallBack,\n *             .copy = myType_CopyCallback,\n *             .defrag = myType_DefragCallback\n *         }\n *\n * * **rdb_load**: A callback function pointer that loads data from RDB files.\n * * **rdb_save**: A callback function pointer that saves data to RDB files.\n * * **aof_rewrite**: A callback function pointer that rewrites data as commands.\n * * **digest**: A callback function pointer that is used for `DEBUG DIGEST`.\n * * **free**: A callback function pointer that can free a type value.\n * * **aux_save**: A callback function pointer that saves out of keyspace data to RDB files.\n *   'when' argument is either REDISMODULE_AUX_BEFORE_RDB or REDISMODULE_AUX_AFTER_RDB.\n * * **aux_load**: A callback function pointer that loads out of keyspace data from RDB files.\n *   Similar to aux_save, returns REDISMODULE_OK on success, and ERR otherwise.\n * * **free_effort**: A callback function pointer that used to determine whether the module's\n *   memory needs to be lazy reclaimed. The module should return the complexity involved by\n *   freeing the value. for example: how many pointers are gonna be freed. Note that if it \n *   returns 0, we'll always do an async free.\n * * **unlink**: A callback function pointer that used to notifies the module that the key has \n *   been removed from the DB by redis, and may soon be freed by a background thread. Note that \n *   it won't be called on FLUSHALL/FLUSHDB (both sync and async), and the module can use the \n *   RedisModuleEvent_FlushDB to hook into that.\n * * **copy**: A callback function pointer that is used to make a copy of the specified key.\n *   The module is expected to perform a deep copy of the specified value and return it.\n *   In addition, hints about the names of the source and destination keys is provided.\n *   A NULL return value is considered an error and the copy operation fails.\n *   Note: if the target key exists and is being overwritten, the copy callback will be\n *   called first, followed by a free callback to the value that is being replaced.\n * \n * * **defrag**: A callback function pointer that is used to request the module to defrag\n *   a key. The module should then iterate pointers and call the relevant RM_Defrag*()\n *   functions to defragment pointers or complex types. The module should continue\n *   iterating as long as RM_DefragShouldStop() returns a zero value, and return a\n *   zero value if finished or non-zero value if more work is left to be done. If more work\n *   needs to be done, RM_DefragCursorSet() and RM_DefragCursorGet() can be used to track\n *   this work across different calls.\n *   Normally, the defrag mechanism invokes the callback without a time limit, so\n *   RM_DefragShouldStop() always returns zero. The \"late defrag\" mechanism which has\n *   a time limit and provides cursor support is used only for keys that are determined\n *   to have significant internal complexity. To determine this, the defrag mechanism\n *   uses the free_effort callback and the 'active-defrag-max-scan-fields' config directive.\n *   NOTE: The value is passed as a `void**` and the function is expected to update the\n *   pointer if the top-level value pointer is defragmented and consequentially changes.\n *\n * Note: the module name \"AAAAAAAAA\" is reserved and produces an error, it\n * happens to be pretty lame as well.\n *\n * If there is already a module registering a type with the same name,\n * and if the module name or encver is invalid, NULL is returned.\n * Otherwise the new type is registered into Redis, and a reference of\n * type RedisModuleType is returned: the caller of the function should store\n * this reference into a global variable to make future use of it in the\n * modules type API, since a single module may register multiple types.\n * Example code fragment:\n *\n *      static RedisModuleType *BalancedTreeType;\n *\n *      int RedisModule_OnLoad(RedisModuleCtx *ctx) {\n *          // some code here ...\n *          BalancedTreeType = RM_CreateDataType(...);\n *      }\n */\nmoduleType *RM_CreateDataType(RedisModuleCtx *ctx, const char *name, int encver, void *typemethods_ptr) {\n    uint64_t id = moduleTypeEncodeId(name,encver);\n    if (id == 0) return NULL;\n    if (moduleTypeLookupModuleByName(name) != NULL) return NULL;\n\n    long typemethods_version = ((long*)typemethods_ptr)[0];\n    if (typemethods_version == 0) return NULL;\n\n    struct typemethods {\n        uint64_t version;\n        moduleTypeLoadFunc rdb_load;\n        moduleTypeSaveFunc rdb_save;\n        moduleTypeRewriteFunc aof_rewrite;\n        moduleTypeMemUsageFunc mem_usage;\n        moduleTypeDigestFunc digest;\n        moduleTypeFreeFunc free;\n        struct {\n            moduleTypeAuxLoadFunc aux_load;\n            moduleTypeAuxSaveFunc aux_save;\n            int aux_save_triggers;\n        } v2;\n        struct {\n            moduleTypeFreeEffortFunc free_effort;\n            moduleTypeUnlinkFunc unlink;\n            moduleTypeCopyFunc copy;\n            moduleTypeDefragFunc defrag;\n        } v3;\n    } *tms = (struct typemethods*) typemethods_ptr;\n\n    moduleType *mt = (moduleType*)zcalloc(sizeof(*mt), MALLOC_LOCAL);\n    mt->id = id;\n    mt->module = ctx->module;\n    mt->rdb_load = tms->rdb_load;\n    mt->rdb_save = tms->rdb_save;\n    mt->aof_rewrite = tms->aof_rewrite;\n    mt->mem_usage = tms->mem_usage;\n    mt->digest = tms->digest;\n    mt->free = tms->free;\n    if (tms->version >= 2) {\n        mt->aux_load = tms->v2.aux_load;\n        mt->aux_save = tms->v2.aux_save;\n        mt->aux_save_triggers = tms->v2.aux_save_triggers;\n    }\n    if (tms->version >= 3) {\n        mt->free_effort = tms->v3.free_effort;\n        mt->unlink = tms->v3.unlink;\n        mt->copy = tms->v3.copy;\n        mt->defrag = tms->v3.defrag;\n    }\n    memcpy(mt->name,name,sizeof(mt->name));\n    listAddNodeTail(ctx->module->types,mt);\n    return mt;\n}\n\n/* If the key is open for writing, set the specified module type object\n * as the value of the key, deleting the old value if any.\n * On success REDISMODULE_OK is returned. If the key is not open for\n * writing or there is an active iterator, REDISMODULE_ERR is returned. */\nint RM_ModuleTypeSetValue(RedisModuleKey *key, moduleType *mt, void *value) {\n    if (!(key->mode & REDISMODULE_WRITE) || key->iter) return REDISMODULE_ERR;\n    RM_DeleteKey(key);\n    robj *o = createModuleObject(mt,value);\n    genericSetKey(key->ctx->client,key->db,key->key,o,0,0);\n    decrRefCount(o);\n    key->value = o;\n    return REDISMODULE_OK;\n}\n\n/* Assuming RedisModule_KeyType() returned REDISMODULE_KEYTYPE_MODULE on\n * the key, returns the module type pointer of the value stored at key.\n *\n * If the key is NULL, is not associated with a module type, or is empty,\n * then NULL is returned instead. */\nmoduleType *RM_ModuleTypeGetType(RedisModuleKey *key) {\n    if (key == NULL ||\n        key->value == NULL ||\n        RM_KeyType(key) != REDISMODULE_KEYTYPE_MODULE) return NULL;\n    moduleValue *mv = (moduleValue*)ptrFromObj(key->value);\n    return mv->type;\n}\n\n/* Assuming RedisModule_KeyType() returned REDISMODULE_KEYTYPE_MODULE on\n * the key, returns the module type low-level value stored at key, as\n * it was set by the user via RedisModule_ModuleTypeSetValue().\n *\n * If the key is NULL, is not associated with a module type, or is empty,\n * then NULL is returned instead. */\nvoid *RM_ModuleTypeGetValue(RedisModuleKey *key) {\n    if (key == NULL ||\n        key->value == NULL ||\n        RM_KeyType(key) != REDISMODULE_KEYTYPE_MODULE) return NULL;\n    moduleValue *mv = (moduleValue*)ptrFromObj(key->value);\n    return mv->value;\n}\n\n/* --------------------------------------------------------------------------\n * ## RDB loading and saving functions\n * -------------------------------------------------------------------------- */\n\n/* Called when there is a load error in the context of a module. On some\n * modules this cannot be recovered, but if the module declared capability\n * to handle errors, we'll raise a flag rather than exiting. */\nvoid moduleRDBLoadError(RedisModuleIO *io) {\n    if (io->type->module->options & REDISMODULE_OPTIONS_HANDLE_IO_ERRORS) {\n        io->error = 1;\n        return;\n    }\n    serverPanic(\n        \"Error loading data from RDB (short read or EOF). \"\n        \"Read performed by module '%s' about type '%s' \"\n        \"after reading '%llu' bytes of a value \"\n        \"for key named: '%s'.\",\n        io->type->module->name,\n        io->type->name,\n        (unsigned long long)io->bytes,\n        io->key? szFromObj(io->key): \"(null)\");\n}\n\n/* Returns 0 if there's at least one registered data type that did not declare\n * REDISMODULE_OPTIONS_HANDLE_IO_ERRORS, in which case diskless loading should\n * be avoided since it could cause data loss. */\nint moduleAllDatatypesHandleErrors() {\n    dictIterator *di = dictGetIterator(modules);\n    dictEntry *de;\n\n    while ((de = dictNext(di)) != NULL) {\n        struct RedisModule *module = (RedisModule*)dictGetVal(de);\n        if (listLength(module->types) &&\n            !(module->options & REDISMODULE_OPTIONS_HANDLE_IO_ERRORS))\n        {\n            dictReleaseIterator(di);\n            return 0;\n        }\n    }\n    dictReleaseIterator(di);\n    return 1;\n}\n\n/* Returns true if any previous IO API failed.\n * for `Load*` APIs the REDISMODULE_OPTIONS_HANDLE_IO_ERRORS flag must be set with\n * RedisModule_SetModuleOptions first. */\nint RM_IsIOError(RedisModuleIO *io) {\n    return io->error;\n}\n\n/* Save an unsigned 64 bit value into the RDB file. This function should only\n * be called in the context of the rdb_save method of modules implementing new\n * data types. */\nvoid RM_SaveUnsigned(RedisModuleIO *io, uint64_t value) {\n    if (io->error) return;\n    /* Save opcode. */\n    int retval = rdbSaveLen(io->prio, RDB_MODULE_OPCODE_UINT);\n    if (retval == -1) goto saveerr;\n    io->bytes += retval;\n    /* Save value. */\n    retval = rdbSaveLen(io->prio, value);\n    if (retval == -1) goto saveerr;\n    io->bytes += retval;\n    return;\n\nsaveerr:\n    io->error = 1;\n}\n\n/* Load an unsigned 64 bit value from the RDB file. This function should only\n * be called in the context of the `rdb_load` method of modules implementing\n * new data types. */\nuint64_t RM_LoadUnsigned(RedisModuleIO *io) {\n    int retval;\n    if (io->error) return 0;\n    if (io->ver == 2) {\n        uint64_t opcode = rdbLoadLen(io->prio,NULL);\n        if (opcode != RDB_MODULE_OPCODE_UINT) goto loaderr;\n    }\n    uint64_t value;\n    retval = rdbLoadLenByRef(io->prio, NULL, &value);\n    if (retval == -1) goto loaderr;\n    return value;\n\nloaderr:\n    moduleRDBLoadError(io);\n    return 0;\n}\n\n/* Like RedisModule_SaveUnsigned() but for signed 64 bit values. */\nvoid RM_SaveSigned(RedisModuleIO *io, int64_t value) {\n    union {uint64_t u; int64_t i;} conv;\n    conv.i = value;\n    RM_SaveUnsigned(io,conv.u);\n}\n\n/* Like RedisModule_LoadUnsigned() but for signed 64 bit values. */\nint64_t RM_LoadSigned(RedisModuleIO *io) {\n    union {uint64_t u; int64_t i;} conv;\n    conv.u = RM_LoadUnsigned(io);\n    return conv.i;\n}\n\n/* In the context of the rdb_save method of a module type, saves a\n * string into the RDB file taking as input a RedisModuleString.\n *\n * The string can be later loaded with RedisModule_LoadString() or\n * other Load family functions expecting a serialized string inside\n * the RDB file. */\nvoid RM_SaveString(RedisModuleIO *io, RedisModuleString *s) {\n    if (io->error) return;\n    /* Save opcode. */\n    ssize_t retval = rdbSaveLen(io->prio, RDB_MODULE_OPCODE_STRING);\n    if (retval == -1) goto saveerr;\n    io->bytes += retval;\n    /* Save value. */\n    retval = rdbSaveStringObject(io->prio, s);\n    if (retval == -1) goto saveerr;\n    io->bytes += retval;\n    return;\n\nsaveerr:\n    io->error = 1;\n}\n\n/* Like RedisModule_SaveString() but takes a raw C pointer and length\n * as input. */\nvoid RM_SaveStringBuffer(RedisModuleIO *io, const char *str, size_t len) {\n    if (io->error) return;\n    /* Save opcode. */\n    ssize_t retval = rdbSaveLen(io->prio, RDB_MODULE_OPCODE_STRING);\n    if (retval == -1) goto saveerr;\n    io->bytes += retval;\n    /* Save value. */\n    retval = rdbSaveRawString(io->prio, (unsigned char*)str,len);\n    if (retval == -1) goto saveerr;\n    io->bytes += retval;\n    return;\n\nsaveerr:\n    io->error = 1;\n}\n\n/* Implements RM_LoadString() and RM_LoadStringBuffer() */\nvoid *moduleLoadString(RedisModuleIO *io, int plain, size_t *lenptr) {\n    void *s = nullptr;\n    if (io->error) return NULL;\n    if (io->ver == 2) {\n        uint64_t opcode = rdbLoadLen(io->prio,NULL);\n        if (opcode != RDB_MODULE_OPCODE_STRING) goto loaderr;\n    }\n    s = rdbGenericLoadStringObject(io->prio,\n              plain ? RDB_LOAD_PLAIN : RDB_LOAD_NONE, lenptr);\n    if (s == NULL) goto loaderr;\n    return s;\n\nloaderr:\n    moduleRDBLoadError(io);\n    return NULL;\n}\n\n/* In the context of the rdb_load method of a module data type, loads a string\n * from the RDB file, that was previously saved with RedisModule_SaveString()\n * functions family.\n *\n * The returned string is a newly allocated RedisModuleString object, and\n * the user should at some point free it with a call to RedisModule_FreeString().\n *\n * If the data structure does not store strings as RedisModuleString objects,\n * the similar function RedisModule_LoadStringBuffer() could be used instead. */\nRedisModuleString *RM_LoadString(RedisModuleIO *io) {\n    return (RedisModuleString*)moduleLoadString(io,0,NULL);\n}\n\n/* Like RedisModule_LoadString() but returns an heap allocated string that\n * was allocated with RedisModule_Alloc(), and can be resized or freed with\n * RedisModule_Realloc() or RedisModule_Free().\n *\n * The size of the string is stored at '*lenptr' if not NULL.\n * The returned string is not automatically NULL terminated, it is loaded\n * exactly as it was stored inside the RDB file. */\nchar *RM_LoadStringBuffer(RedisModuleIO *io, size_t *lenptr) {\n    return (char*)moduleLoadString(io,1,lenptr);\n}\n\n/* In the context of the rdb_save method of a module data type, saves a double\n * value to the RDB file. The double can be a valid number, a NaN or infinity.\n * It is possible to load back the value with RedisModule_LoadDouble(). */\nvoid RM_SaveDouble(RedisModuleIO *io, double value) {\n    if (io->error) return;\n    /* Save opcode. */\n    int retval = rdbSaveLen(io->prio, RDB_MODULE_OPCODE_DOUBLE);\n    if (retval == -1) goto saveerr;\n    io->bytes += retval;\n    /* Save value. */\n    retval = rdbSaveBinaryDoubleValue(io->prio, value);\n    if (retval == -1) goto saveerr;\n    io->bytes += retval;\n    return;\n\nsaveerr:\n    io->error = 1;\n}\n\n/* In the context of the rdb_save method of a module data type, loads back the\n * double value saved by RedisModule_SaveDouble(). */\ndouble RM_LoadDouble(RedisModuleIO *io) {\n    int retval;\n    if (io->error) return 0;\n    if (io->ver == 2) {\n        uint64_t opcode = rdbLoadLen(io->prio,NULL);\n        if (opcode != RDB_MODULE_OPCODE_DOUBLE) goto loaderr;\n    }\n    double value;\n    retval = rdbLoadBinaryDoubleValue(io->prio, &value);\n    if (retval == -1) goto loaderr;\n    return value;\n\nloaderr:\n    moduleRDBLoadError(io);\n    return 0;\n}\n\n/* In the context of the rdb_save method of a module data type, saves a float\n * value to the RDB file. The float can be a valid number, a NaN or infinity.\n * It is possible to load back the value with RedisModule_LoadFloat(). */\nvoid RM_SaveFloat(RedisModuleIO *io, float value) {\n    if (io->error) return;\n    /* Save opcode. */\n    int retval = rdbSaveLen(io->prio, RDB_MODULE_OPCODE_FLOAT);\n    if (retval == -1) goto saveerr;\n    io->bytes += retval;\n    /* Save value. */\n    retval = rdbSaveBinaryFloatValue(io->prio, value);\n    if (retval == -1) goto saveerr;\n    io->bytes += retval;\n    return;\n\nsaveerr:\n    io->error = 1;\n}\n\n/* In the context of the rdb_save method of a module data type, loads back the\n * float value saved by RedisModule_SaveFloat(). */\nfloat RM_LoadFloat(RedisModuleIO *io) {\n    int retval = C_ERR;\n    if (io->error) return 0;\n    if (io->ver == 2) {\n        uint64_t opcode = rdbLoadLen(io->prio,NULL);\n        if (opcode != RDB_MODULE_OPCODE_FLOAT) goto loaderr;\n    }\n    float value;\n    retval = rdbLoadBinaryFloatValue(io->prio, &value);\n    if (retval == -1) goto loaderr;\n    return value;\n\nloaderr:\n    moduleRDBLoadError(io);\n    return 0;\n}\n\n/* In the context of the rdb_save method of a module data type, saves a long double\n * value to the RDB file. The double can be a valid number, a NaN or infinity.\n * It is possible to load back the value with RedisModule_LoadLongDouble(). */\nvoid RM_SaveLongDouble(RedisModuleIO *io, long double value) {\n    if (io->error) return;\n    char buf[MAX_LONG_DOUBLE_CHARS];\n    /* Long double has different number of bits in different platforms, so we\n     * save it as a string type. */\n    size_t len = ld2string(buf,sizeof(buf),value,LD_STR_HEX);\n    RM_SaveStringBuffer(io,buf,len);\n}\n\n/* In the context of the rdb_save method of a module data type, loads back the\n * long double value saved by RedisModule_SaveLongDouble(). */\nlong double RM_LoadLongDouble(RedisModuleIO *io) {\n    if (io->error) return 0;\n    long double value;\n    size_t len;\n    char* str = RM_LoadStringBuffer(io,&len);\n    if (!str) return 0;\n    string2ld(str,len,&value);\n    RM_Free(str);\n    return value;\n}\n\n/* Iterate over modules, and trigger rdb aux saving for the ones modules types\n * who asked for it. */\nssize_t rdbSaveModulesAux(rio *rdb, int when) {\n    size_t total_written = 0;\n    dictIterator *di = dictGetIterator(modules);\n    dictEntry *de;\n\n    while ((de = dictNext(di)) != NULL) {\n        struct RedisModule *module = (RedisModule*)dictGetVal(de);\n        listIter li;\n        listNode *ln;\n\n        listRewind(module->types,&li);\n        while((ln = listNext(&li))) {\n            moduleType *mt = (moduleType*)ln->value;\n            if (!mt->aux_save || !(mt->aux_save_triggers & when))\n                continue;\n            ssize_t ret = rdbSaveSingleModuleAux(rdb, when, mt);\n            if (ret==-1) {\n                dictReleaseIterator(di);\n                return -1;\n            }\n            total_written += ret;\n        }\n    }\n\n    dictReleaseIterator(di);\n    return total_written;\n}\n\n/* --------------------------------------------------------------------------\n * ## Key digest API (DEBUG DIGEST interface for modules types)\n * -------------------------------------------------------------------------- */\n\n/* Add a new element to the digest. This function can be called multiple times\n * one element after the other, for all the elements that constitute a given\n * data structure. The function call must be followed by the call to\n * `RedisModule_DigestEndSequence` eventually, when all the elements that are\n * always in a given order are added. See the Redis Modules data types\n * documentation for more info. However this is a quick example that uses Redis\n * data types as an example.\n *\n * To add a sequence of unordered elements (for example in the case of a Redis\n * Set), the pattern to use is:\n *\n *     foreach element {\n *         AddElement(element);\n *         EndSequence();\n *     }\n *\n * Because Sets are not ordered, so every element added has a position that\n * does not depend from the other. However if instead our elements are\n * ordered in pairs, like field-value pairs of an Hash, then one should\n * use:\n *\n *     foreach key,value {\n *         AddElement(key);\n *         AddElement(value);\n *         EndSquence();\n *     }\n *\n * Because the key and value will be always in the above order, while instead\n * the single key-value pairs, can appear in any position into a Redis hash.\n *\n * A list of ordered elements would be implemented with:\n *\n *     foreach element {\n *         AddElement(element);\n *     }\n *     EndSequence();\n *\n */\nvoid RM_DigestAddStringBuffer(RedisModuleDigest *md, unsigned char *ele, size_t len) {\n    mixDigest(md->o,ele,len);\n}\n\n/* Like `RedisModule_DigestAddStringBuffer()` but takes a long long as input\n * that gets converted into a string before adding it to the digest. */\nvoid RM_DigestAddLongLong(RedisModuleDigest *md, long long ll) {\n    char buf[LONG_STR_SIZE];\n    size_t len = ll2string(buf,sizeof(buf),ll);\n    mixDigest(md->o,buf,len);\n}\n\n/* See the documentation for `RedisModule_DigestAddElement()`. */\nvoid RM_DigestEndSequence(RedisModuleDigest *md) {\n    xorDigest(md->x,md->o,sizeof(md->o));\n    memset(md->o,0,sizeof(md->o));\n}\n\n/* Decode a serialized representation of a module data type 'mt' from string\n * 'str' and return a newly allocated value, or NULL if decoding failed.\n *\n * This call basically reuses the 'rdb_load' callback which module data types\n * implement in order to allow a module to arbitrarily serialize/de-serialize\n * keys, similar to how the Redis 'DUMP' and 'RESTORE' commands are implemented.\n *\n * Modules should generally use the REDISMODULE_OPTIONS_HANDLE_IO_ERRORS flag and\n * make sure the de-serialization code properly checks and handles IO errors\n * (freeing allocated buffers and returning a NULL).\n *\n * If this is NOT done, Redis will handle corrupted (or just truncated) serialized\n * data by producing an error message and terminating the process.\n */\nvoid *RM_LoadDataTypeFromString(const RedisModuleString *str, const moduleType *mt) {\n    rio payload;\n    RedisModuleIO io;\n    void *ret;\n\n    rioInitWithBuffer(&payload, szFromObj(str));\n    moduleInitIOContext(io,(moduleType *)mt,&payload,NULL);\n\n    /* All RM_Save*() calls always write a version 2 compatible format, so we\n     * need to make sure we read the same.\n     */\n    io.ver = 2;\n    ret = mt->rdb_load(&io,0);\n    if (io.ctx) {\n        moduleFreeContext(io.ctx);\n        zfree(io.ctx);\n    }\n    return ret;\n}\n\n/* Encode a module data type 'mt' value 'data' into serialized form, and return it\n * as a newly allocated RedisModuleString.\n *\n * This call basically reuses the 'rdb_save' callback which module data types\n * implement in order to allow a module to arbitrarily serialize/de-serialize\n * keys, similar to how the Redis 'DUMP' and 'RESTORE' commands are implemented.\n */\nRedisModuleString *RM_SaveDataTypeToString(RedisModuleCtx *ctx, void *data, const moduleType *mt) {\n    rio payload;\n    RedisModuleIO io;\n\n    rioInitWithBuffer(&payload,sdsempty());\n    moduleInitIOContext(io,(moduleType *)mt,&payload,NULL);\n    mt->rdb_save(&io,data);\n    if (io.ctx) {\n        moduleFreeContext(io.ctx);\n        zfree(io.ctx);\n    }\n    if (io.error) {\n        return NULL;\n    } else {\n        robj *str = createObject(OBJ_STRING,payload.io.buffer.ptr);\n        if (ctx != NULL) autoMemoryAdd(ctx,REDISMODULE_AM_STRING,str);\n        return str;\n    }\n}\n\n/* --------------------------------------------------------------------------\n * ## AOF API for modules data types\n * -------------------------------------------------------------------------- */\n\n/* Emits a command into the AOF during the AOF rewriting process. This function\n * is only called in the context of the aof_rewrite method of data types exported\n * by a module. The command works exactly like RedisModule_Call() in the way\n * the parameters are passed, but it does not return anything as the error\n * handling is performed by Redis itself. */\nvoid RM_EmitAOF(RedisModuleIO *io, const char *cmdname, const char *fmt, ...) {\n    if (io->error) return;\n    struct redisCommand *cmd;\n    robj **argv = NULL;\n    int argc = 0, flags = 0, j;\n    va_list ap;\n\n    cmd = lookupCommandByCString((char*)cmdname);\n    if (!cmd) {\n        serverLog(LL_WARNING,\n            \"Fatal: AOF method for module data type '%s' tried to \"\n            \"emit unknown command '%s'\",\n            io->type->name, cmdname);\n        io->error = 1;\n        errno = EINVAL;\n        return;\n    }\n\n    /* Emit the arguments into the AOF in Redis protocol format. */\n    va_start(ap, fmt);\n    argv = moduleCreateArgvFromUserFormat(cmdname,fmt,&argc,&flags,ap);\n    va_end(ap);\n    if (argv == NULL) {\n        serverLog(LL_WARNING,\n            \"Fatal: AOF method for module data type '%s' tried to \"\n            \"call RedisModule_EmitAOF() with wrong format specifiers '%s'\",\n            io->type->name, fmt);\n        io->error = 1;\n        errno = EINVAL;\n        return;\n    }\n\n    /* Bulk count. */\n    if (!io->error && rioWriteBulkCount(io->prio,'*',argc) == 0)\n        io->error = 1;\n\n    /* Arguments. */\n    for (j = 0; j < argc; j++) {\n        if (!io->error && rioWriteBulkObject(io->prio,argv[j]) == 0)\n            io->error = 1;\n        decrRefCount(argv[j]);\n    }\n    zfree(argv);\n    return;\n}\n\n/* --------------------------------------------------------------------------\n * ## IO context handling\n * -------------------------------------------------------------------------- */\n\nRedisModuleCtx *RM_GetContextFromIO(RedisModuleIO *io) {\n    if (io->ctx) return io->ctx; /* Can't have more than one... */\n    RedisModuleCtx ctxtemplate = REDISMODULE_CTX_INIT;\n    io->ctx = (RedisModuleCtx*)zmalloc(sizeof(RedisModuleCtx), MALLOC_LOCAL);\n    *(io->ctx) = ctxtemplate;\n    io->ctx->module = io->type->module;\n    io->ctx->client = NULL;\n    return io->ctx;\n}\n\n/* Returns a RedisModuleString with the name of the key currently saving or\n * loading, when an IO data type callback is called.  There is no guarantee\n * that the key name is always available, so this may return NULL.\n */\nconst RedisModuleString *RM_GetKeyNameFromIO(RedisModuleIO *io) {\n    return io->key;\n}\n\n/* Returns a RedisModuleString with the name of the key from RedisModuleKey. */\nconst RedisModuleString *RM_GetKeyNameFromModuleKey(RedisModuleKey *key) {\n    return key ? key->key : NULL;\n}\n\n/* --------------------------------------------------------------------------\n * ## Logging\n * -------------------------------------------------------------------------- */\n\n/* This is the low level function implementing both:\n *\n *      RM_Log()\n *      RM_LogIOError()\n *\n */\nvoid moduleLogRaw(RedisModule *module, const char *levelstr, const char *fmt, va_list ap) {\n    char msg[LOG_MAX_LEN];\n    size_t name_len;\n    int level;\n\n    if (!strcasecmp(levelstr,\"debug\")) level = LL_DEBUG;\n    else if (!strcasecmp(levelstr,\"verbose\")) level = LL_VERBOSE;\n    else if (!strcasecmp(levelstr,\"notice\")) level = LL_NOTICE;\n    else if (!strcasecmp(levelstr,\"warning\")) level = LL_WARNING;\n    else level = LL_VERBOSE; /* Default. */\n\n    if (level < cserver.verbosity) return;\n\n    name_len = snprintf(msg, sizeof(msg),\"<%s> \", module? module->name: \"module\");\n    vsnprintf(msg + name_len, sizeof(msg) - name_len, fmt, ap);\n    serverLogRaw(level,msg);\n}\n\n/* Produces a log message to the standard Redis log, the format accepts\n * printf-alike specifiers, while level is a string describing the log\n * level to use when emitting the log, and must be one of the following:\n *\n * * \"debug\" (`REDISMODULE_LOGLEVEL_DEBUG`)\n * * \"verbose\" (`REDISMODULE_LOGLEVEL_VERBOSE`)\n * * \"notice\" (`REDISMODULE_LOGLEVEL_NOTICE`)\n * * \"warning\" (`REDISMODULE_LOGLEVEL_WARNING`)\n *\n * If the specified log level is invalid, verbose is used by default.\n * There is a fixed limit to the length of the log line this function is able\n * to emit, this limit is not specified but is guaranteed to be more than\n * a few lines of text.\n *\n * The ctx argument may be NULL if cannot be provided in the context of the\n * caller for instance threads or callbacks, in which case a generic \"module\"\n * will be used instead of the module name.\n */\nvoid RM_Log(RedisModuleCtx *ctx, const char *levelstr, const char *fmt, ...) {\n    va_list ap;\n    va_start(ap, fmt);\n    moduleLogRaw(ctx? ctx->module: NULL,levelstr,fmt,ap);\n    va_end(ap);\n}\n\n/* Log errors from RDB / AOF serialization callbacks.\n *\n * This function should be used when a callback is returning a critical\n * error to the caller since cannot load or save the data for some\n * critical reason. */\nvoid RM_LogIOError(RedisModuleIO *io, const char *levelstr, const char *fmt, ...) {\n    va_list ap;\n    va_start(ap, fmt);\n    moduleLogRaw(io->type->module,levelstr,fmt,ap);\n    va_end(ap);\n}\n\n/* Redis-like assert function.\n *\n * The macro `RedisModule_Assert(expression)` is recommended, rather than\n * calling this function directly.\n *\n * A failed assertion will shut down the server and produce logging information\n * that looks identical to information generated by Redis itself.\n */\nvoid RM__Assert(const char *estr, const char *file, int line) {\n    _serverAssert(estr, file, line);\n}\n\n/* Allows adding event to the latency monitor to be observed by the LATENCY\n * command. The call is skipped if the latency is smaller than the configured\n * latency-monitor-threshold. */\nvoid RM_LatencyAddSample(const char *event, mstime_t latency) {\n    if (latency >= g_pserver->latency_monitor_threshold)\n        latencyAddSample(event, latency);\n}\n\n/* --------------------------------------------------------------------------\n * ## Blocking clients from modules\n *\n * For a guide about blocking commands in modules, see\n * https://redis.io/topics/modules-blocking-ops.\n * -------------------------------------------------------------------------- */\n\n/* Readable handler for the awake pipe. We do nothing here, the awake bytes\n * will be actually read in a more appropriate place in the\n * moduleHandleBlockedClients() function that is where clients are actually\n * served. */\nvoid moduleBlockedClientPipeReadable(aeEventLoop *el, int fd, void *privdata, int mask) {\n    UNUSED(el);\n    UNUSED(fd);\n    UNUSED(mask);\n    UNUSED(privdata);\n}\n\n/* This is called from blocked.c in order to unblock a client: may be called\n * for multiple reasons while the client is in the middle of being blocked\n * because the client is terminated, but is also called for cleanup when a\n * client is unblocked in a clean way after replaying.\n *\n * What we do here is just to set the client to NULL in the redis module\n * blocked client handle. This way if the client is terminated while there\n * is a pending threaded operation involving the blocked client, we'll know\n * that the client no longer exists and no reply callback should be called.\n *\n * The structure RedisModuleBlockedClient will be always deallocated when\n * running the list of clients blocked by a module that need to be unblocked. */\nvoid unblockClientFromModule(client *c) {\n    RedisModuleBlockedClient *bc = (RedisModuleBlockedClient*)c->bpop.module_blocked_handle;\n\n    /* Call the disconnection callback if any. Note that\n     * bc->disconnect_callback is set to NULL if the client gets disconnected\n     * by the module itself or because of a timeout, so the callback will NOT\n     * get called if this is not an actual disconnection event. */\n    if (bc->disconnect_callback) {\n        RedisModuleCtx ctx = REDISMODULE_CTX_INIT;\n        ctx.blocked_privdata = bc->privdata;\n        ctx.module = bc->module;\n        ctx.client = bc->client;\n        bc->disconnect_callback(&ctx,bc);\n        moduleFreeContext(&ctx);\n    }\n\n    /* If we made it here and client is still blocked it means that the command\n     * timed-out, client was killed or disconnected and disconnect_callback was\n     * not implemented (or it was, but RM_UnblockClient was not called from\n     * within it, as it should).\n     * We must call moduleUnblockClient in order to free privdata and\n     * RedisModuleBlockedClient.\n     *\n     * Note that we only do that for clients that are blocked on keys, for which\n     * the contract is that the module should not call RM_UnblockClient under\n     * normal circumstances.\n     * Clients implementing threads and working with private data should be\n     * aware that calling RM_UnblockClient for every blocked client is their\n     * responsibility, and if they fail to do so memory may leak. Ideally they\n     * should implement the disconnect and timeout callbacks and call\n     * RM_UnblockClient, but any other way is also acceptable. */\n    if (bc->blocked_on_keys && !bc->unblocked)\n        moduleUnblockClient(c);\n\n    bc->client = NULL;\n}\n\n/* Block a client in the context of a module: this function implements both\n * RM_BlockClient() and RM_BlockClientOnKeys() depending on the fact the\n * keys are passed or not.\n *\n * When not blocking for keys, the keys, numkeys, and privdata parameters are\n * not needed. The privdata in that case must be NULL, since later is\n * RM_UnblockClient() that will provide some private data that the reply\n * callback will receive.\n *\n * Instead when blocking for keys, normally RM_UnblockClient() will not be\n * called (because the client will unblock when the key is modified), so\n * 'privdata' should be provided in that case, so that once the client is\n * unlocked and the reply callback is called, it will receive its associated\n * private data.\n *\n * Even when blocking on keys, RM_UnblockClient() can be called however, but\n * in that case the privdata argument is disregarded, because we pass the\n * reply callback the privdata that is set here while blocking.\n *\n */\nRedisModuleBlockedClient *moduleBlockClient(RedisModuleCtx *ctx, RedisModuleCmdFunc reply_callback, RedisModuleCmdFunc timeout_callback, void (*free_privdata)(RedisModuleCtx*,void*), long long timeout_ms, RedisModuleString **keys, int numkeys, void *privdata) {\n    client *c = ctx->client;\n    int islua = serverTL->in_eval;\n    int ismulti = serverTL->in_exec;\n\n    c->bpop.module_blocked_handle = zmalloc(sizeof(RedisModuleBlockedClient));\n    RedisModuleBlockedClient *bc = (RedisModuleBlockedClient*)c->bpop.module_blocked_handle;\n    ctx->module->blocked_clients++;\n\n    /* We need to handle the invalid operation of calling modules blocking\n     * commands from Lua or MULTI. We actually create an already aborted\n     * (client set to NULL) blocked client handle, and actually reply with\n     * an error. */\n    mstime_t timeout = timeout_ms ? (mstime()+timeout_ms) : 0;\n    bc->client = (islua || ismulti) ? NULL : c;\n    bc->module = ctx->module;\n    bc->reply_callback = reply_callback;\n    bc->timeout_callback = timeout_callback;\n    bc->disconnect_callback = NULL; /* Set by RM_SetDisconnectCallback() */\n    bc->free_privdata = free_privdata;\n    bc->privdata = privdata;\n    bc->reply_client = createClient(NULL, IDX_EVENT_LOOP_MAIN);\n    bc->reply_client->flags |= CLIENT_MODULE;\n    bc->dbid = c->db->id;\n    bc->blocked_on_keys = keys != NULL;\n    bc->unblocked = 0;\n    bc->background_timer = 0;\n    bc->background_duration = 0;\n    c->bpop.timeout = timeout;\n\n    if (islua || ismulti) {\n        c->bpop.module_blocked_handle = NULL;\n        addReplyError(c, islua ?\n            \"Blocking module command called from Lua script\" :\n            \"Blocking module command called from transaction\");\n    } else {\n        if (keys) {\n            blockForKeys(c,BLOCKED_MODULE,keys,numkeys,timeout,NULL,NULL,NULL);\n        } else {\n            blockClient(c,BLOCKED_MODULE);\n        }\n    }\n    return bc;\n}\n\n/* This function is called from module.c in order to check if a module\n * blocked for BLOCKED_MODULE and subtype 'on keys' (bc->blocked_on_keys true)\n * can really be unblocked, since the module was able to serve the client.\n * If the callback returns REDISMODULE_OK, then the client can be unblocked,\n * otherwise the client remains blocked and we'll retry again when one of\n * the keys it blocked for becomes \"ready\" again.\n * This function returns 1 if client was served (and should be unblocked) */\nint moduleTryServeClientBlockedOnKey(client *c, robj *key) {\n    int served = 0;\n    RedisModuleBlockedClient *bc = (RedisModuleBlockedClient*)c->bpop.module_blocked_handle;\n\n    /* Protect against re-processing: don't serve clients that are already\n     * in the unblocking list for any reason (including RM_UnblockClient()\n     * explicit call). See #6798. */\n    if (bc->unblocked) return 0;\n\n    RedisModuleCtx ctx = REDISMODULE_CTX_INIT;\n    ctx.flags |= REDISMODULE_CTX_BLOCKED_REPLY;\n    ctx.blocked_ready_key = key;\n    ctx.blocked_privdata = bc->privdata;\n    ctx.module = bc->module;\n    ctx.client = bc->client;\n    ctx.blocked_client = bc;\n    if (bc->reply_callback(&ctx,(void**)c->argv,c->argc) == REDISMODULE_OK)\n        served = 1;\n    moduleFreeContext(&ctx);\n    return served;\n}\n\n/* Block a client in the context of a blocking command, returning an handle\n * which will be used, later, in order to unblock the client with a call to\n * RedisModule_UnblockClient(). The arguments specify callback functions\n * and a timeout after which the client is unblocked.\n *\n * The callbacks are called in the following contexts:\n *\n *     reply_callback:   called after a successful RedisModule_UnblockClient()\n *                       call in order to reply to the client and unblock it.\n *\n *     timeout_callback: called when the timeout is reached or if `CLIENT UNBLOCK`\n *                       is invoked, in order to send an error to the client.\n *\n *     free_privdata:    called in order to free the private data that is passed\n *                       by RedisModule_UnblockClient() call.\n *\n * Note: RedisModule_UnblockClient should be called for every blocked client,\n *       even if client was killed, timed-out or disconnected. Failing to do so\n *       will result in memory leaks.\n *\n * There are some cases where RedisModule_BlockClient() cannot be used:\n *\n * 1. If the client is a Lua script.\n * 2. If the client is executing a MULTI block.\n *\n * In these cases, a call to RedisModule_BlockClient() will **not** block the\n * client, but instead produce a specific error reply.\n *\n * A module that registers a timeout_callback function can also be unblocked\n * using the `CLIENT UNBLOCK` command, which will trigger the timeout callback.\n * If a callback function is not registered, then the blocked client will be\n * treated as if it is not in a blocked state and `CLIENT UNBLOCK` will return\n * a zero value.\n *\n * Measuring background time: By default the time spent in the blocked command\n * is not account for the total command duration. To include such time you should\n * use RM_BlockedClientMeasureTimeStart() and RM_BlockedClientMeasureTimeEnd() one,\n * or multiple times within the blocking command background work.\n */\nRedisModuleBlockedClient *RM_BlockClient(RedisModuleCtx *ctx, RedisModuleCmdFunc reply_callback, RedisModuleCmdFunc timeout_callback, void (*free_privdata)(RedisModuleCtx*,void*), long long timeout_ms) {\n    return moduleBlockClient(ctx,reply_callback,timeout_callback,free_privdata,timeout_ms, NULL,0,NULL);\n}\n\n/* This call is similar to RedisModule_BlockClient(), however in this case we\n * don't just block the client, but also ask Redis to unblock it automatically\n * once certain keys become \"ready\", that is, contain more data.\n *\n * Basically this is similar to what a typical Redis command usually does,\n * like BLPOP or BZPOPMAX: the client blocks if it cannot be served ASAP,\n * and later when the key receives new data (a list push for instance), the\n * client is unblocked and served.\n *\n * However in the case of this module API, when the client is unblocked?\n *\n * 1. If you block on a key of a type that has blocking operations associated,\n *    like a list, a sorted set, a stream, and so forth, the client may be\n *    unblocked once the relevant key is targeted by an operation that normally\n *    unblocks the native blocking operations for that type. So if we block\n *    on a list key, an RPUSH command may unblock our client and so forth.\n * 2. If you are implementing your native data type, or if you want to add new\n *    unblocking conditions in addition to \"1\", you can call the modules API\n *    RedisModule_SignalKeyAsReady().\n *\n * Anyway we can't be sure if the client should be unblocked just because the\n * key is signaled as ready: for instance a successive operation may change the\n * key, or a client in queue before this one can be served, modifying the key\n * as well and making it empty again. So when a client is blocked with\n * RedisModule_BlockClientOnKeys() the reply callback is not called after\n * RM_UnblockClient() is called, but every time a key is signaled as ready:\n * if the reply callback can serve the client, it returns REDISMODULE_OK\n * and the client is unblocked, otherwise it will return REDISMODULE_ERR\n * and we'll try again later.\n *\n * The reply callback can access the key that was signaled as ready by\n * calling the API RedisModule_GetBlockedClientReadyKey(), that returns\n * just the string name of the key as a RedisModuleString object.\n *\n * Thanks to this system we can setup complex blocking scenarios, like\n * unblocking a client only if a list contains at least 5 items or other\n * more fancy logics.\n *\n * Note that another difference with RedisModule_BlockClient(), is that here\n * we pass the private data directly when blocking the client: it will\n * be accessible later in the reply callback. Normally when blocking with\n * RedisModule_BlockClient() the private data to reply to the client is\n * passed when calling RedisModule_UnblockClient() but here the unblocking\n * is performed by Redis itself, so we need to have some private data before\n * hand. The private data is used to store any information about the specific\n * unblocking operation that you are implementing. Such information will be\n * freed using the free_privdata callback provided by the user.\n *\n * However the reply callback will be able to access the argument vector of\n * the command, so the private data is often not needed.\n *\n * Note: Under normal circumstances RedisModule_UnblockClient should not be\n *       called for clients that are blocked on keys (Either the key will\n *       become ready or a timeout will occur). If for some reason you do want\n *       to call RedisModule_UnblockClient it is possible: Client will be\n *       handled as if it were timed-out (You must implement the timeout\n *       callback in that case).\n */\nRedisModuleBlockedClient *RM_BlockClientOnKeys(RedisModuleCtx *ctx, RedisModuleCmdFunc reply_callback, RedisModuleCmdFunc timeout_callback, void (*free_privdata)(RedisModuleCtx*,void*), long long timeout_ms, RedisModuleString **keys, int numkeys, void *privdata) {\n    return moduleBlockClient(ctx,reply_callback,timeout_callback,free_privdata,timeout_ms, keys,numkeys,privdata);\n}\n\n/* This function is used in order to potentially unblock a client blocked\n * on keys with RedisModule_BlockClientOnKeys(). When this function is called,\n * all the clients blocked for this key will get their reply_callback called.\n *\n * Note: The function has no effect if the signaled key doesn't exist. */\nvoid RM_SignalKeyAsReady(RedisModuleCtx *ctx, RedisModuleString *key) {\n    signalKeyAsReady(ctx->client->db, key, OBJ_MODULE);\n}\n\n/* Implements RM_UnblockClient() and moduleUnblockClient(). */\nint moduleUnblockClientByHandle(RedisModuleBlockedClient *bc, void *privdata) {\n    pthread_mutex_lock(&moduleUnblockedClientsMutex);\n    if (!bc->blocked_on_keys) bc->privdata = privdata;\n    bc->unblocked = 1;\n    listAddNodeTail(moduleUnblockedClients,bc);\n    if (write(g_pserver->module_blocked_pipe[1],\"A\",1) != 1) {\n        /* Ignore the error, this is best-effort. */\n    }\n    pthread_mutex_unlock(&moduleUnblockedClientsMutex);\n    return REDISMODULE_OK;\n}\n\n/* This API is used by the Redis core to unblock a client that was blocked\n * by a module. */\nvoid moduleUnblockClient(client *c) {\n    RedisModuleBlockedClient *bc = (RedisModuleBlockedClient*)c->bpop.module_blocked_handle;\n    moduleUnblockClientByHandle(bc,NULL);\n}\n\n/* Return true if the client 'c' was blocked by a module using\n * RM_BlockClientOnKeys(). */\nint moduleClientIsBlockedOnKeys(client *c) {\n    RedisModuleBlockedClient *bc = (RedisModuleBlockedClient*)c->bpop.module_blocked_handle;\n    return bc->blocked_on_keys;\n}\n\n/* Unblock a client blocked by `RedisModule_BlockedClient`. This will trigger\n * the reply callbacks to be called in order to reply to the client.\n * The 'privdata' argument will be accessible by the reply callback, so\n * the caller of this function can pass any value that is needed in order to\n * actually reply to the client.\n *\n * A common usage for 'privdata' is a thread that computes something that\n * needs to be passed to the client, included but not limited some slow\n * to compute reply or some reply obtained via networking.\n *\n * Note 1: this function can be called from threads spawned by the module.\n *\n * Note 2: when we unblock a client that is blocked for keys using the API\n * RedisModule_BlockClientOnKeys(), the privdata argument here is not used.\n * Unblocking a client that was blocked for keys using this API will still\n * require the client to get some reply, so the function will use the\n * \"timeout\" handler in order to do so (The privdata provided in\n * RedisModule_BlockClientOnKeys() is accessible from the timeout\n * callback via RM_GetBlockedClientPrivateData). */\nint RM_UnblockClient(RedisModuleBlockedClient *bc, void *privdata) {\n    moduleSetThreadVariablesIfNeeded();\n    if (bc->blocked_on_keys) {\n        /* In theory the user should always pass the timeout handler as an\n         * argument, but better to be safe than sorry. */\n        if (bc->timeout_callback == NULL) return REDISMODULE_ERR;\n        if (bc->unblocked) return REDISMODULE_OK;\n        if (bc->client) moduleBlockedClientTimedOut(bc->client);\n    }\n    moduleUnblockClientByHandle(bc,privdata);\n    return REDISMODULE_OK;\n}\n\n/* Abort a blocked client blocking operation: the client will be unblocked\n * without firing any callback. */\nint RM_AbortBlock(RedisModuleBlockedClient *bc) {\n    bc->reply_callback = NULL;\n    bc->disconnect_callback = NULL;\n    return RM_UnblockClient(bc,NULL);\n}\n\n/* Set a callback that will be called if a blocked client disconnects\n * before the module has a chance to call RedisModule_UnblockClient()\n *\n * Usually what you want to do there, is to cleanup your module state\n * so that you can call RedisModule_UnblockClient() safely, otherwise\n * the client will remain blocked forever if the timeout is large.\n *\n * Notes:\n *\n * 1. It is not safe to call Reply* family functions here, it is also\n *    useless since the client is gone.\n *\n * 2. This callback is not called if the client disconnects because of\n *    a timeout. In such a case, the client is unblocked automatically\n *    and the timeout callback is called.\n */\nvoid RM_SetDisconnectCallback(RedisModuleBlockedClient *bc, RedisModuleDisconnectFunc callback) {\n    bc->disconnect_callback = callback;\n}\n\n/* This function will check the moduleUnblockedClients queue in order to\n * call the reply callback and really unblock the client.\n *\n * Clients end into this list because of calls to RM_UnblockClient(),\n * however it is possible that while the module was doing work for the\n * blocked client, it was terminated by Redis (for timeout or other reasons).\n * When this happens the RedisModuleBlockedClient structure in the queue\n * will have the 'client' field set to NULL. */\nvoid moduleHandleBlockedClients(int iel) {\n    RedisModuleBlockedClient *bc;\n    serverAssert(GlobalLocksAcquired());\n\n    pthread_mutex_lock(&moduleUnblockedClientsMutex);\n    /* Here we unblock all the pending clients blocked in modules operations\n     * so we can read every pending \"awake byte\" in the pipe. */\n    char buf[1];\n    while (read(g_pserver->module_blocked_pipe[0],buf,1) == 1);\n    listIter li;\n    listNode *ln;\n    listRewind(moduleUnblockedClients, &li);\n    while ((ln = listNext(&li))) {\n        bc = (RedisModuleBlockedClient*)ln->value;\n        client *c = bc->client;\n        if ((c != nullptr) && (iel != c->iel))\n            continue;\n        \n        std::unique_lock<fastlock> ul;\n        listDelNode(moduleUnblockedClients,ln);\n        pthread_mutex_unlock(&moduleUnblockedClientsMutex);\n\n        if (c)\n        {\n            AssertCorrectThread(c);\n            ul = std::unique_lock<fastlock>(c->lock);\n        }\n\n        /* Release the lock during the loop, as long as we don't\n         * touch the shared list. */\n\n        /* Call the reply callback if the client is valid and we have\n         * any callback. However the callback is not called if the client\n         * was blocked on keys (RM_BlockClientOnKeys()), because we already\n         * called such callback in moduleTryServeClientBlockedOnKey() when\n         * the key was signaled as ready. */\n        uint64_t reply_us = 0;\n        if (c && !bc->blocked_on_keys && bc->reply_callback) {\n            RedisModuleCtx ctx = REDISMODULE_CTX_INIT;\n            ctx.flags |= REDISMODULE_CTX_BLOCKED_REPLY;\n            ctx.blocked_privdata = bc->privdata;\n            ctx.blocked_ready_key = NULL;\n            ctx.module = bc->module;\n            ctx.client = bc->client;\n            ctx.blocked_client = bc;\n            monotime replyTimer;\n            elapsedStart(&replyTimer);\n            bc->reply_callback(&ctx,(void**)c->argv,c->argc);\n            reply_us = elapsedUs(replyTimer);\n            moduleFreeContext(&ctx);\n        }\n        /* Update stats now that we've finished the blocking operation.\n         * This needs to be out of the reply callback above given that a\n         * module might not define any callback and still do blocking ops.\n         */\n        if (c && !bc->blocked_on_keys) {\n            updateStatsOnUnblock(c, bc->background_duration, reply_us);\n        }\n\n        /* Free privdata if any. */\n        if (bc->privdata && bc->free_privdata) {\n            RedisModuleCtx ctx = REDISMODULE_CTX_INIT;\n            if (c == NULL)\n                ctx.flags |= REDISMODULE_CTX_BLOCKED_DISCONNECTED;\n            ctx.blocked_privdata = bc->privdata;\n            ctx.module = bc->module;\n            ctx.client = bc->client;\n            bc->free_privdata(&ctx,bc->privdata);\n            moduleFreeContext(&ctx);\n        }\n\n        /* It is possible that this blocked client object accumulated\n         * replies to send to the client in a thread safe context.\n         * We need to glue such replies to the client output buffer and\n         * free the temporary client we just used for the replies. */\n        if (c) AddReplyFromClient(c, bc->reply_client);\n        freeClient(bc->reply_client);\n\n        if (c != NULL) {\n            /* Before unblocking the client, set the disconnect callback\n             * to NULL, because if we reached this point, the client was\n             * properly unblocked by the module. */\n            bc->disconnect_callback = NULL;\n            unblockClient(c);\n            /* Put the client in the list of clients that need to write\n             * if there are pending replies here. This is needed since\n             * during a non blocking command the client may receive output. */\n            if (clientHasPendingReplies(c) &&\n                !(c->flags & CLIENT_PENDING_WRITE))\n            {\n                c->flags |= CLIENT_PENDING_WRITE;\n                AssertCorrectThread(c);\n                \n                fastlock_lock(&g_pserver->rgthreadvar[c->iel].lockPendingWrite);\n                g_pserver->rgthreadvar[c->iel].clients_pending_write.push_back(c);\n                fastlock_unlock(&g_pserver->rgthreadvar[c->iel].lockPendingWrite);\n            }\n        }\n\n        /* Free 'bc' only after unblocking the client, since it is\n         * referenced in the client blocking context, and must be valid\n         * when calling unblockClient(). */\n        bc->module->blocked_clients--;\n        zfree(bc);\n\n        /* Lock again before to iterate the loop. */\n        pthread_mutex_lock(&moduleUnblockedClientsMutex);\n    }\n    pthread_mutex_unlock(&moduleUnblockedClientsMutex);\n}\n\n/* Check if the specified client can be safely timed out using\n * moduleBlockedClientTimedOut().\n */\nint moduleBlockedClientMayTimeout(client *c) {\n    if (c->btype != BLOCKED_MODULE)\n        return 1;\n\n    RedisModuleBlockedClient *bc = (RedisModuleBlockedClient*)c->bpop.module_blocked_handle;\n    return (bc && bc->timeout_callback != NULL);\n}\n\n/* Called when our client timed out. After this function unblockClient()\n * is called, and it will invalidate the blocked client. So this function\n * does not need to do any cleanup. Eventually the module will call the\n * API to unblock the client and the memory will be released. */\nvoid moduleBlockedClientTimedOut(client *c) {\n    RedisModuleBlockedClient *bc = (RedisModuleBlockedClient*)c->bpop.module_blocked_handle;\n\n    /* Protect against re-processing: don't serve clients that are already\n     * in the unblocking list for any reason (including RM_UnblockClient()\n     * explicit call). See #6798. */\n    if (bc->unblocked) return;\n\n    RedisModuleCtx ctx = REDISMODULE_CTX_INIT;\n    ctx.flags |= REDISMODULE_CTX_BLOCKED_TIMEOUT;\n    ctx.module = bc->module;\n    ctx.client = bc->client;\n    ctx.blocked_client = bc;\n    ctx.blocked_privdata = bc->privdata;\n    bc->timeout_callback(&ctx,(void**)c->argv,c->argc);\n    moduleFreeContext(&ctx);\n    if (!bc->blocked_on_keys) {\n        updateStatsOnUnblock(c, bc->background_duration, 0);\n    }\n    /* For timeout events, we do not want to call the disconnect callback,\n     * because the blocked client will be automatically disconnected in\n     * this case, and the user can still hook using the timeout callback. */\n    bc->disconnect_callback = NULL;\n}\n\n/* Return non-zero if a module command was called in order to fill the\n * reply for a blocked client. */\nint RM_IsBlockedReplyRequest(RedisModuleCtx *ctx) {\n    return (ctx->flags & REDISMODULE_CTX_BLOCKED_REPLY) != 0;\n}\n\n/* Return non-zero if a module command was called in order to fill the\n * reply for a blocked client that timed out. */\nint RM_IsBlockedTimeoutRequest(RedisModuleCtx *ctx) {\n    return (ctx->flags & REDISMODULE_CTX_BLOCKED_TIMEOUT) != 0;\n}\n\n/* Get the private data set by RedisModule_UnblockClient() */\nvoid *RM_GetBlockedClientPrivateData(RedisModuleCtx *ctx) {\n    return ctx->blocked_privdata;\n}\n\n/* Get the key that is ready when the reply callback is called in the context\n * of a client blocked by RedisModule_BlockClientOnKeys(). */\nRedisModuleString *RM_GetBlockedClientReadyKey(RedisModuleCtx *ctx) {\n    return ctx->blocked_ready_key;\n}\n\n/* Get the blocked client associated with a given context.\n * This is useful in the reply and timeout callbacks of blocked clients,\n * before sometimes the module has the blocked client handle references\n * around, and wants to cleanup it. */\nRedisModuleBlockedClient *RM_GetBlockedClientHandle(RedisModuleCtx *ctx) {\n    return ctx->blocked_client;\n}\n\n/* Return true if when the free callback of a blocked client is called,\n * the reason for the client to be unblocked is that it disconnected\n * while it was blocked. */\nint RM_BlockedClientDisconnected(RedisModuleCtx *ctx) {\n    return (ctx->flags & REDISMODULE_CTX_BLOCKED_DISCONNECTED) != 0;\n}\n\n/* --------------------------------------------------------------------------\n * ## Thread Safe Contexts\n * -------------------------------------------------------------------------- */\n\n/* Return a context which can be used inside threads to make Redis context\n * calls with certain modules APIs. If 'bc' is not NULL then the module will\n * be bound to a blocked client, and it will be possible to use the\n * `RedisModule_Reply*` family of functions to accumulate a reply for when the\n * client will be unblocked. Otherwise the thread safe context will be\n * detached by a specific client.\n *\n * To call non-reply APIs, the thread safe context must be prepared with:\n *\n *     RedisModule_ThreadSafeContextLock(ctx);\n *     ... make your call here ...\n *     RedisModule_ThreadSafeContextUnlock(ctx);\n *\n * This is not needed when using `RedisModule_Reply*` functions, assuming\n * that a blocked client was used when the context was created, otherwise\n * no RedisModule_Reply* call should be made at all.\n *\n * NOTE: If you're creating a detached thread safe context (bc is NULL),\n * consider using `RM_GetDetachedThreadSafeContext` which will also retain\n * the module ID and thus be more useful for logging. */\nRedisModuleCtx *RM_GetThreadSafeContext(RedisModuleBlockedClient *bc) {\n    RedisModuleCtx *ctx = (RedisModuleCtx*)zmalloc(sizeof(*ctx), MALLOC_LOCAL);\n    RedisModuleCtx empty = REDISMODULE_CTX_INIT;\n    memcpy(ctx,&empty,sizeof(empty));\n    if (bc) {\n        ctx->blocked_client = bc;\n        ctx->module = bc->module;\n    }\n    ctx->flags |= REDISMODULE_CTX_THREAD_SAFE;\n    /* Even when the context is associated with a blocked client, we can't\n     * access it safely from another thread, so we create a fake client here\n     * in order to keep things like the currently selected database and similar\n     * things. */\n    ctx->client = createClient(NULL, IDX_EVENT_LOOP_MAIN);\n    if (bc) {\n        selectDb(ctx->client,bc->dbid);\n        if (bc->client) ctx->client->id = bc->client->id;\n    }\n    return ctx;\n}\n\n/* Return a detached thread safe context that is not associated with any\n * specific blocked client, but is associated with the module's context.\n *\n * This is useful for modules that wish to hold a global context over\n * a long term, for purposes such as logging. */\nRedisModuleCtx *RM_GetDetachedThreadSafeContext(RedisModuleCtx *ctx) {\n    RedisModuleCtx *new_ctx = (RedisModuleCtx*)zmalloc(sizeof(*new_ctx));\n    RedisModuleCtx empty = REDISMODULE_CTX_INIT;\n    memcpy(new_ctx,&empty,sizeof(empty));\n    new_ctx->module = ctx->module;\n    new_ctx->flags |= REDISMODULE_CTX_THREAD_SAFE;\n    new_ctx->client = createClient(NULL, IDX_EVENT_LOOP_MAIN);\n    return new_ctx;\n}\n\n/* Release a thread safe context. */\nvoid RM_FreeThreadSafeContext(RedisModuleCtx *ctx) {\n    moduleAcquireGIL(false /*fServerThread*/);\n    moduleFreeContext(ctx);\n    moduleReleaseGIL(false /*fServerThread*/);\n    zfree(ctx);\n}\n\n\n/* Acquire the server lock before executing a thread safe API call.\n * This is not needed for `RedisModule_Reply*` calls when there is\n * a blocked client connected to the thread safe context. */\nvoid RM_ThreadSafeContextLock(RedisModuleCtx *ctx) {\n    UNUSED(ctx);\n    moduleSetThreadVariablesIfNeeded();\n    moduleAcquireGIL(FALSE /*fServerThread*/, true /*fExclusive*/);\n}\n\n/* Similar to RM_ThreadSafeContextLock but this function\n * would not block if the server lock is already acquired.\n *\n * If successful (lock acquired) REDISMODULE_OK is returned,\n * otherwise REDISMODULE_ERR is returned and errno is set\n * accordingly. */\nint RM_ThreadSafeContextTryLock(RedisModuleCtx *ctx) {\n    UNUSED(ctx);\n\n    int res = moduleTryAcquireGIL(false /*fServerThread*/, true /*fExclusive*/);\n    if(res != 0) {\n        errno = res;\n        return REDISMODULE_ERR;\n    }\n    return REDISMODULE_OK;\n}\n\n/* Release the server lock after a thread safe API call was executed. */\nvoid RM_ThreadSafeContextUnlock(RedisModuleCtx *ctx) {\n    UNUSED(ctx);\n    moduleReleaseGIL(FALSE /*fServerThread*/, true /*fExclusive*/);\n}\n\n// A module may be triggered synchronously in a non-module context.  In this scenario we don't lock again\n//  as the server thread acquisition is sufficient.  If we did try to lock we would deadlock\nstatic bool FModuleCallBackLock(bool fServerThread)\n{\n    return !fServerThread && aeThreadOwnsLock() && !g_fModuleThread && s_moduleGIL.hasReader();\n}\nvoid moduleAcquireGIL(int fServerThread, int fExclusive) {\n    if (FModuleCallBackLock(fServerThread)) {\n        return;\n    }\n\n    if (fServerThread)\n    {\n        s_moduleGIL.acquireRead();\n    }\n    else\n    {\n        s_moduleGIL.acquireWrite(fExclusive);\n    }\n}\n\nint moduleTryAcquireGIL(bool fServerThread, int fExclusive) {\n    if (FModuleCallBackLock(fServerThread)) {\n        return 0;\n    }\n\n    if (fServerThread)\n    {\n        if (!s_moduleGIL.tryAcquireRead())\n            return 1;\n    }\n    else\n    {\n        if (!s_moduleGIL.tryAcquireWrite(fExclusive))\n            return 1;\n    }\n    return 0;\n}\n\nvoid moduleReleaseGIL(int fServerThread, int fExclusive) {\n    if (FModuleCallBackLock(fServerThread)) {\n        return;\n    }\n    \n    if (fServerThread)\n    {\n        s_moduleGIL.releaseRead();\n    }\n    else\n    {\n        s_moduleGIL.releaseWrite(fExclusive);\n    }\n}\n\nint moduleGILAcquiredByModule(void) {\n    return s_moduleGIL.hasWriter();\n}\n\n\n/* --------------------------------------------------------------------------\n * ## Module Keyspace Notifications API\n * -------------------------------------------------------------------------- */\n\n/* Subscribe to keyspace notifications. This is a low-level version of the\n * keyspace-notifications API. A module can register callbacks to be notified\n * when keyspace events occur.\n *\n * Notification events are filtered by their type (string events, set events,\n * etc), and the subscriber callback receives only events that match a specific\n * mask of event types.\n *\n * When subscribing to notifications with RedisModule_SubscribeToKeyspaceEvents \n * the module must provide an event type-mask, denoting the events the subscriber\n * is interested in. This can be an ORed mask of any of the following flags:\n *\n *  - REDISMODULE_NOTIFY_GENERIC: Generic commands like DEL, EXPIRE, RENAME\n *  - REDISMODULE_NOTIFY_STRING: String events\n *  - REDISMODULE_NOTIFY_LIST: List events\n *  - REDISMODULE_NOTIFY_SET: Set events\n *  - REDISMODULE_NOTIFY_HASH: Hash events\n *  - REDISMODULE_NOTIFY_ZSET: Sorted Set events\n *  - REDISMODULE_NOTIFY_EXPIRED: Expiration events\n *  - REDISMODULE_NOTIFY_EVICTED: Eviction events\n *  - REDISMODULE_NOTIFY_STREAM: Stream events\n *  - REDISMODULE_NOTIFY_MODULE: Module types events\n *  - REDISMODULE_NOTIFY_KEYMISS: Key-miss events\n *  - REDISMODULE_NOTIFY_ALL: All events (Excluding REDISMODULE_NOTIFY_KEYMISS)\n *  - REDISMODULE_NOTIFY_LOADED: A special notification available only for modules,\n *                               indicates that the key was loaded from persistence.\n *                               Notice, when this event fires, the given key\n *                               can not be retained, use RM_CreateStringFromString\n *                               instead.\n *\n * We do not distinguish between key events and keyspace events, and it is up\n * to the module to filter the actions taken based on the key.\n *\n * The subscriber signature is:\n *\n *     int (*RedisModuleNotificationFunc) (RedisModuleCtx *ctx, int type,\n *                                         const char *event,\n *                                         RedisModuleString *key);\n *\n * `type` is the event type bit, that must match the mask given at registration\n * time. The event string is the actual command being executed, and key is the\n * relevant Redis key.\n *\n * Notification callback gets executed with a redis context that can not be\n * used to send anything to the client, and has the db number where the event\n * occurred as its selected db number.\n *\n * Notice that it is not necessary to enable notifications in keydb.conf for\n * module notifications to work.\n *\n * Warning: the notification callbacks are performed in a synchronous manner,\n * so notification callbacks must to be fast, or they would slow Redis down.\n * If you need to take long actions, use threads to offload them.\n *\n * See https://redis.io/topics/notifications for more information.\n */\nint RM_SubscribeToKeyspaceEvents(RedisModuleCtx *ctx, int types, RedisModuleNotificationFunc callback) {\n    RedisModuleKeyspaceSubscriber *sub = (RedisModuleKeyspaceSubscriber*)zmalloc(sizeof(*sub), MALLOC_LOCAL);\n    sub->module = ctx->module;\n    sub->event_mask = types;\n    sub->notify_callback = callback;\n    sub->active = 0;\n\n    listAddNodeTail(moduleKeyspaceSubscribers, sub);\n    return REDISMODULE_OK;\n}\n\n/* Get the configured bitmap of notify-keyspace-events (Could be used\n * for additional filtering in RedisModuleNotificationFunc) */\nint RM_GetNotifyKeyspaceEvents() {\n    return g_pserver->notify_keyspace_events;\n}\n\n/* Expose notifyKeyspaceEvent to modules */\nint RM_NotifyKeyspaceEvent(RedisModuleCtx *ctx, int type, const char *event, RedisModuleString *key) {\n    if (!ctx || !ctx->client)\n        return REDISMODULE_ERR;\n    notifyKeyspaceEvent(type, (char *)event, key, ctx->client->db->id);\n    return REDISMODULE_OK;\n}\n\n/* Dispatcher for keyspace notifications to module subscriber functions.\n * This gets called  only if at least one module requested to be notified on\n * keyspace notifications */\nvoid moduleNotifyKeyspaceEvent(int type, const char *event, robj *key, int dbid) {\n    /* Don't do anything if there aren't any subscribers */\n    if (listLength(moduleKeyspaceSubscribers) == 0) return;\n\n    listIter li;\n    listNode *ln;\n    listRewind(moduleKeyspaceSubscribers,&li);\n\n    /* Remove irrelevant flags from the type mask */\n    type &= ~(NOTIFY_KEYEVENT | NOTIFY_KEYSPACE);\n\n    while((ln = listNext(&li))) {\n        RedisModuleKeyspaceSubscriber *sub = (RedisModuleKeyspaceSubscriber*)ln->value;\n        /* Only notify subscribers on events matching they registration,\n         * and avoid subscribers triggering themselves */\n        if ((sub->event_mask & type) && sub->active == 0) {\n            RedisModuleCtx ctx = REDISMODULE_CTX_INIT;\n            ctx.module = sub->module;\n            ctx.client = moduleFreeContextReusedClient;\n            selectDb(ctx.client, dbid);\n\n            /* mark the handler as active to avoid reentrant loops.\n             * If the subscriber performs an action triggering itself,\n             * it will not be notified about it. */\n            sub->active = 1;\n            sub->notify_callback(&ctx, type, event, key);\n            sub->active = 0;\n            moduleFreeContext(&ctx);\n        }\n    }\n}\n\n/* Unsubscribe any notification subscribers this module has upon unloading */\nvoid moduleUnsubscribeNotifications(RedisModule *module) {\n    listIter li;\n    listNode *ln;\n    listRewind(moduleKeyspaceSubscribers,&li);\n    while((ln = listNext(&li))) {\n        RedisModuleKeyspaceSubscriber *sub = (RedisModuleKeyspaceSubscriber*)ln->value;\n        if (sub->module == module) {\n            listDelNode(moduleKeyspaceSubscribers, ln);\n            zfree(sub);\n        }\n    }\n}\n\n/* --------------------------------------------------------------------------\n * ## Modules Cluster API\n * -------------------------------------------------------------------------- */\n\n/* The Cluster message callback function pointer type. */\ntypedef void (*RedisModuleClusterMessageReceiver)(RedisModuleCtx *ctx, const char *sender_id, uint8_t type, const unsigned char *payload, uint32_t len);\n\n/* This structure identifies a registered caller: it must match a given module\n * ID, for a given message type. The callback function is just the function\n * that was registered as receiver. */\ntypedef struct moduleClusterReceiver {\n    uint64_t module_id;\n    RedisModuleClusterMessageReceiver callback;\n    struct RedisModule *module;\n    struct moduleClusterReceiver *next;\n} moduleClusterReceiver;\n\ntypedef struct moduleClusterNodeInfo {\n    int flags;\n    char ip[NET_IP_STR_LEN];\n    int port;\n    char master_id[40]; /* Only if flags & REDISMODULE_NODE_MASTER is true. */\n} mdouleClusterNodeInfo;\n\n/* We have an array of message types: each bucket is a linked list of\n * configured receivers. */\nstatic moduleClusterReceiver *clusterReceivers[UINT8_MAX];\n\n/* Dispatch the message to the right module receiver. */\nvoid moduleCallClusterReceivers(const char *sender_id, uint64_t module_id, uint8_t type, const unsigned char *payload, uint32_t len) {\n    moduleClusterReceiver *r = clusterReceivers[type];\n    while(r) {\n        if (r->module_id == module_id) {\n            RedisModuleCtx ctx = REDISMODULE_CTX_INIT;\n            ctx.module = r->module;\n            ctx.client = moduleFreeContextReusedClient;\n            selectDb(ctx.client, 0);\n            r->callback(&ctx,sender_id,type,payload,len);\n            moduleFreeContext(&ctx);\n            return;\n        }\n        r = r->next;\n    }\n}\n\n/* Register a callback receiver for cluster messages of type 'type'. If there\n * was already a registered callback, this will replace the callback function\n * with the one provided, otherwise if the callback is set to NULL and there\n * is already a callback for this function, the callback is unregistered\n * (so this API call is also used in order to delete the receiver). */\nvoid RM_RegisterClusterMessageReceiver(RedisModuleCtx *ctx, uint8_t type, RedisModuleClusterMessageReceiver callback) {\n    if (!g_pserver->cluster_enabled) return;\n\n    uint64_t module_id = moduleTypeEncodeId(ctx->module->name,0);\n    moduleClusterReceiver *r = clusterReceivers[type], *prev = NULL;\n    while(r) {\n        if (r->module_id == module_id) {\n            /* Found! Set or delete. */\n            if (callback) {\n                r->callback = callback;\n            } else {\n                /* Delete the receiver entry if the user is setting\n                 * it to NULL. Just unlink the receiver node from the\n                 * linked list. */\n                if (prev)\n                    prev->next = r->next;\n                else\n                    clusterReceivers[type]->next = r->next;\n                zfree(r);\n            }\n            return;\n        }\n        prev = r;\n        r = r->next;\n    }\n\n    /* Not found, let's add it. */\n    if (callback) {\n        r = (moduleClusterReceiver*)zmalloc(sizeof(*r), MALLOC_LOCAL);\n        r->module_id = module_id;\n        r->module = ctx->module;\n        r->callback = callback;\n        r->next = clusterReceivers[type];\n        clusterReceivers[type] = r;\n    }\n}\n\n/* Send a message to all the nodes in the cluster if `target` is NULL, otherwise\n * at the specified target, which is a REDISMODULE_NODE_ID_LEN bytes node ID, as\n * returned by the receiver callback or by the nodes iteration functions.\n *\n * The function returns REDISMODULE_OK if the message was successfully sent,\n * otherwise if the node is not connected or such node ID does not map to any\n * known cluster node, REDISMODULE_ERR is returned. */\nint RM_SendClusterMessage(RedisModuleCtx *ctx, char *target_id, uint8_t type, unsigned char *msg, uint32_t len) {\n    if (!g_pserver->cluster_enabled) return REDISMODULE_ERR;\n    uint64_t module_id = moduleTypeEncodeId(ctx->module->name,0);\n    if (clusterSendModuleMessageToTarget(target_id,module_id,type,msg,len) == C_OK)\n        return REDISMODULE_OK;\n    else\n        return REDISMODULE_ERR;\n}\n\n/* Return an array of string pointers, each string pointer points to a cluster\n * node ID of exactly REDISMODULE_NODE_ID_SIZE bytes (without any null term).\n * The number of returned node IDs is stored into `*numnodes`.\n * However if this function is called by a module not running an a Redis\n * instance with Redis Cluster enabled, NULL is returned instead.\n *\n * The IDs returned can be used with RedisModule_GetClusterNodeInfo() in order\n * to get more information about single nodes.\n *\n * The array returned by this function must be freed using the function\n * RedisModule_FreeClusterNodesList().\n *\n * Example:\n *\n *     size_t count, j;\n *     char **ids = RedisModule_GetClusterNodesList(ctx,&count);\n *     for (j = 0; j < count; j++) {\n *         RedisModule_Log(\"notice\",\"Node %.*s\",\n *             REDISMODULE_NODE_ID_LEN,ids[j]);\n *     }\n *     RedisModule_FreeClusterNodesList(ids);\n */\nchar **RM_GetClusterNodesList(RedisModuleCtx *ctx, size_t *numnodes) {\n    UNUSED(ctx);\n\n    if (!g_pserver->cluster_enabled) return NULL;\n    size_t count = dictSize(g_pserver->cluster->nodes);\n    char **ids = (char**)zmalloc((count+1)*REDISMODULE_NODE_ID_LEN, MALLOC_LOCAL);\n    dictIterator *di = dictGetIterator(g_pserver->cluster->nodes);\n    dictEntry *de;\n    int j = 0;\n    while((de = dictNext(di)) != NULL) {\n        clusterNode *node = (clusterNode*)dictGetVal(de);\n        if (node->flags & (CLUSTER_NODE_NOADDR|CLUSTER_NODE_HANDSHAKE)) continue;\n        ids[j] = (char*)zmalloc(REDISMODULE_NODE_ID_LEN, MALLOC_LOCAL);\n        memcpy(ids[j],node->name,REDISMODULE_NODE_ID_LEN);\n        j++;\n    }\n    *numnodes = j;\n    ids[j] = NULL; /* Null term so that FreeClusterNodesList does not need\n                    * to also get the count argument. */\n    dictReleaseIterator(di);\n    return ids;\n}\n\n/* Free the node list obtained with RedisModule_GetClusterNodesList. */\nvoid RM_FreeClusterNodesList(char **ids) {\n    if (ids == NULL) return;\n    for (int j = 0; ids[j]; j++) zfree(ids[j]);\n    zfree(ids);\n}\n\n/* Return this node ID (REDISMODULE_CLUSTER_ID_LEN bytes) or NULL if the cluster\n * is disabled. */\nconst char *RM_GetMyClusterID(void) {\n    if (!g_pserver->cluster_enabled) return NULL;\n    return g_pserver->cluster->myself->name;\n}\n\n/* Return the number of nodes in the cluster, regardless of their state\n * (handshake, noaddress, ...) so that the number of active nodes may actually\n * be smaller, but not greater than this number. If the instance is not in\n * cluster mode, zero is returned. */\nsize_t RM_GetClusterSize(void) {\n    if (!g_pserver->cluster_enabled) return 0;\n    return dictSize(g_pserver->cluster->nodes);\n}\n\nclusterNode *clusterLookupNode(const char *name); /* We need access to internals */\n\n/* Populate the specified info for the node having as ID the specified 'id',\n * then returns REDISMODULE_OK. Otherwise if the node ID does not exist from\n * the POV of this local node, REDISMODULE_ERR is returned.\n *\n * The arguments `ip`, `master_id`, `port` and `flags` can be NULL in case we don't\n * need to populate back certain info. If an `ip` and `master_id` (only populated\n * if the instance is a slave) are specified, they point to buffers holding\n * at least REDISMODULE_NODE_ID_LEN bytes. The strings written back as `ip`\n * and `master_id` are not null terminated.\n *\n * The list of flags reported is the following:\n *\n * * REDISMODULE_NODE_MYSELF:       This node\n * * REDISMODULE_NODE_MASTER:       The node is a master\n * * REDISMODULE_NODE_SLAVE:        The node is a replica\n * * REDISMODULE_NODE_PFAIL:        We see the node as failing\n * * REDISMODULE_NODE_FAIL:         The cluster agrees the node is failing\n * * REDISMODULE_NODE_NOFAILOVER:   The slave is configured to never failover\n */\nint RM_GetClusterNodeInfo(RedisModuleCtx *ctx, const char *id, char *ip, char *master_id, int *port, int *flags) {\n    UNUSED(ctx);\n\n    clusterNode *node = clusterLookupNode(id);\n    if (node == NULL ||\n        node->flags & (CLUSTER_NODE_NOADDR|CLUSTER_NODE_HANDSHAKE))\n    {\n        return REDISMODULE_ERR;\n    }\n\n    if (ip) strncpy(ip,node->ip,NET_IP_STR_LEN);\n\n    if (master_id) {\n        /* If the information is not available, the function will set the\n         * field to zero bytes, so that when the field can't be populated the\n         * function kinda remains predictable. */\n        if (node->flags & CLUSTER_NODE_SLAVE && node->slaveof)\n            memcpy(master_id,node->slaveof->name,REDISMODULE_NODE_ID_LEN);\n        else\n            memset(master_id,0,REDISMODULE_NODE_ID_LEN);\n    }\n    if (port) *port = node->port;\n\n    /* As usually we have to remap flags for modules, in order to ensure\n     * we can provide binary compatibility. */\n    if (flags) {\n        *flags = 0;\n        if (node->flags & CLUSTER_NODE_MYSELF) *flags |= REDISMODULE_NODE_MYSELF;\n        if (node->flags & CLUSTER_NODE_MASTER) *flags |= REDISMODULE_NODE_MASTER;\n        if (node->flags & CLUSTER_NODE_SLAVE) *flags |= REDISMODULE_NODE_SLAVE;\n        if (node->flags & CLUSTER_NODE_PFAIL) *flags |= REDISMODULE_NODE_PFAIL;\n        if (node->flags & CLUSTER_NODE_FAIL) *flags |= REDISMODULE_NODE_FAIL;\n        if (node->flags & CLUSTER_NODE_NOFAILOVER) *flags |= REDISMODULE_NODE_NOFAILOVER;\n    }\n    return REDISMODULE_OK;\n}\n\n/* Set Redis Cluster flags in order to change the normal behavior of\n * Redis Cluster, especially with the goal of disabling certain functions.\n * This is useful for modules that use the Cluster API in order to create\n * a different distributed system, but still want to use the Redis Cluster\n * message bus. Flags that can be set:\n *\n * * CLUSTER_MODULE_FLAG_NO_FAILOVER\n * * CLUSTER_MODULE_FLAG_NO_REDIRECTION\n *\n * With the following effects:\n *\n * * NO_FAILOVER: prevent Redis Cluster slaves to failover a failing master.\n *                Also disables the replica migration feature.\n *\n * * NO_REDIRECTION: Every node will accept any key, without trying to perform\n *                   partitioning according to the user Redis Cluster algorithm.\n *                   Slots informations will still be propagated across the\n *                   cluster, but without effects. */\nvoid RM_SetClusterFlags(RedisModuleCtx *ctx, uint64_t flags) {\n    UNUSED(ctx);\n    if (flags & REDISMODULE_CLUSTER_FLAG_NO_FAILOVER)\n        g_pserver->cluster_module_flags |= CLUSTER_MODULE_FLAG_NO_FAILOVER;\n    if (flags & REDISMODULE_CLUSTER_FLAG_NO_REDIRECTION)\n        g_pserver->cluster_module_flags |= CLUSTER_MODULE_FLAG_NO_REDIRECTION;\n}\n\n/* --------------------------------------------------------------------------\n * ## Modules Timers API\n *\n * Module timers are an high precision \"green timers\" abstraction where\n * every module can register even millions of timers without problems, even if\n * the actual event loop will just have a single timer that is used to awake the\n * module timers subsystem in order to process the next event.\n *\n * All the timers are stored into a radix tree, ordered by expire time, when\n * the main Redis event loop timer callback is called, we try to process all\n * the timers already expired one after the other. Then we re-enter the event\n * loop registering a timer that will expire when the next to process module\n * timer will expire.\n *\n * Every time the list of active timers drops to zero, we unregister the\n * main event loop timer, so that there is no overhead when such feature is\n * not used.\n * -------------------------------------------------------------------------- */\n\nstatic rax *Timers;     /* The radix tree of all the timers sorted by expire. */\nlong long aeTimer = -1; /* Main event loop (ae.c) timer identifier. */\n\ntypedef void (*RedisModuleTimerProc)(RedisModuleCtx *ctx, void *data);\n\n/* The timer descriptor, stored as value in the radix tree. */\ntypedef struct RedisModuleTimer {\n    RedisModule *module;                /* Module reference. */\n    RedisModuleTimerProc callback;      /* The callback to invoke on expire. */\n    void *data;                         /* Private data for the callback. */\n    int dbid;                           /* Database number selected by the original client. */\n} RedisModuleTimer;\n\n/* This is the timer handler that is called by the main event loop. We schedule\n * this timer to be called when the nearest of our module timers will expire. */\nint moduleTimerHandler(struct aeEventLoop *eventLoop, long long id, void *clientData) {\n    UNUSED(eventLoop);\n    UNUSED(id);\n    UNUSED(clientData);\n\n    /* To start let's try to fire all the timers already expired. */\n    raxIterator ri;\n    raxStart(&ri,Timers);\n    uint64_t now = ustime();\n    long long next_period = 0;\n    while(1) {\n        raxSeek(&ri,\"^\",NULL,0);\n        if (!raxNext(&ri)) break;\n        uint64_t expiretime;\n        memcpy(&expiretime,ri.key,sizeof(expiretime));\n        expiretime = ntohu64(expiretime);\n        if (now >= expiretime) {\n            RedisModuleTimer *timer = (RedisModuleTimer*)ri.data;\n            RedisModuleCtx ctx = REDISMODULE_CTX_INIT;\n\n            ctx.module = timer->module;\n            ctx.client = moduleFreeContextReusedClient;\n            selectDb(ctx.client, timer->dbid);\n            timer->callback(&ctx,timer->data);\n            moduleFreeContext(&ctx);\n            raxRemove(Timers,(unsigned char*)ri.key,ri.key_len,NULL);\n            zfree(timer);\n        } else {\n            /* We call ustime() again instead of using the cached 'now' so that\n             * 'next_period' isn't affected by the time it took to execute\n             * previous calls to 'callback.\n             * We need to cast 'expiretime' so that the compiler will not treat\n             * the difference as unsigned (Causing next_period to be huge) in\n             * case expiretime < ustime() */\n            next_period = ((long long)expiretime-ustime())/1000; /* Scale to milliseconds. */\n            break;\n        }\n    }\n    raxStop(&ri);\n\n    /* Reschedule the next timer or cancel it. */\n    if (next_period <= 0) next_period = 1;\n    if (raxSize(Timers) > 0) {\n        return next_period;\n    } else {\n        aeTimer = -1;\n        return AE_NOMORE;\n    }\n}\n\n/* Create a new timer that will fire after `period` milliseconds, and will call\n * the specified function using `data` as argument. The returned timer ID can be\n * used to get information from the timer or to stop it before it fires.\n * Note that for the common use case of a repeating timer (Re-registration\n * of the timer inside the RedisModuleTimerProc callback) it matters when\n * this API is called:\n * If it is called at the beginning of 'callback' it means\n * the event will triggered every 'period'.\n * If it is called at the end of 'callback' it means\n * there will 'period' milliseconds gaps between events.\n * (If the time it takes to execute 'callback' is negligible the two\n * statements above mean the same) */\nRedisModuleTimerID RM_CreateTimer(RedisModuleCtx *ctx, mstime_t period, RedisModuleTimerProc callback, void *data) {\n    static uint64_t pending_key;\n    static mstime_t pending_period = -1; \n\n    RedisModuleTimer *timer = (RedisModuleTimer*)zmalloc(sizeof(*timer), MALLOC_LOCAL);\n    timer->module = ctx->module;\n    timer->callback = callback;\n    timer->data = data;\n    timer->dbid = ctx->client ? ctx->client->db->id : 0;\n    uint64_t expiretime = ustime()+period*1000;\n    uint64_t key;\n\n    while(1) {\n        key = htonu64(expiretime);\n        if (raxFind(Timers, (unsigned char*)&key,sizeof(key)) == raxNotFound) {\n            raxInsert(Timers,(unsigned char*)&key,sizeof(key),timer,NULL);\n            break;\n        } else {\n            expiretime++;\n        }\n    }\n\n    bool fNeedPost = (pending_period < 0);    // If pending_period is already set, then a PostFunction is in flight and we don't need to set a new one\n    if (pending_period < 0 || period < pending_period) {\n        pending_period = period;\n        pending_key = key;\n    }\n\n    /* We need to install the main event loop timer if it's not already\n     * installed, or we may need to refresh its period if we just installed\n     * a timer that will expire sooner than any other else (i.e. the timer\n     * we just installed is the first timer in the Timers rax). */\n    if (fNeedPost) {\n        aePostFunction(g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].el, []{\n            if (aeTimer != -1) {\n                raxIterator ri;\n                raxStart(&ri,Timers);\n                raxSeek(&ri,\"^\",NULL,0);\n                raxNext(&ri);\n                if (memcmp(ri.key,&pending_key,sizeof(key)) == 0) {\n                    /* This is the first key, we need to re-install the timer according\n                    * to the just added event. */\n                    aeDeleteTimeEvent(g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].el,aeTimer);\n                    aeTimer = -1;\n                }\n                raxStop(&ri);\n            }\n\n            /* If we have no main timer (the old one was invalidated, or this is the\n            * first module timer we have), install one. */\n            if (aeTimer == -1) {\n                aeTimer = aeCreateTimeEvent(g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].el,pending_period,moduleTimerHandler,NULL,NULL);\n            }\n\n            pending_period = -1;\n        });\n    }\n\n    return key;\n}\n\n/* Stop a timer, returns REDISMODULE_OK if the timer was found, belonged to the\n * calling module, and was stopped, otherwise REDISMODULE_ERR is returned.\n * If not NULL, the data pointer is set to the value of the data argument when\n * the timer was created. */\nint RM_StopTimer(RedisModuleCtx *ctx, RedisModuleTimerID id, void **data) {\n    RedisModuleTimer *timer = (RedisModuleTimer*)raxFind(Timers,(unsigned char*)&id,sizeof(id));\n    if (timer == raxNotFound || timer->module != ctx->module)\n        return REDISMODULE_ERR;\n    if (data) *data = timer->data;\n    raxRemove(Timers,(unsigned char*)&id,sizeof(id),NULL);\n    zfree(timer);\n    return REDISMODULE_OK;\n}\n\n/* Obtain information about a timer: its remaining time before firing\n * (in milliseconds), and the private data pointer associated with the timer.\n * If the timer specified does not exist or belongs to a different module\n * no information is returned and the function returns REDISMODULE_ERR, otherwise\n * REDISMODULE_OK is returned. The arguments remaining or data can be NULL if\n * the caller does not need certain information. */\nint RM_GetTimerInfo(RedisModuleCtx *ctx, RedisModuleTimerID id, uint64_t *remaining, void **data) {\n    RedisModuleTimer *timer = (RedisModuleTimer*)raxFind(Timers,(unsigned char*)&id,sizeof(id));\n    if (timer == raxNotFound || timer->module != ctx->module)\n        return REDISMODULE_ERR;\n    if (remaining) {\n        int64_t rem = ntohu64(id)-ustime();\n        if (rem < 0) rem = 0;\n        *remaining = rem/1000; /* Scale to milliseconds. */\n    }\n    if (data) *data = timer->data;\n    return REDISMODULE_OK;\n}\n\n/* --------------------------------------------------------------------------\n * ## Modules ACL API\n *\n * Implements a hook into the authentication and authorization within Redis. \n * --------------------------------------------------------------------------*/\n\n/* This function is called when a client's user has changed and invokes the\n * client's user changed callback if it was set. This callback should\n * cleanup any state the module was tracking about this client. \n * \n * A client's user can be changed through the AUTH command, module \n * authentication, and when a client is freed. */\nvoid moduleNotifyUserChanged(client *c) {\n    if (c->auth_callback) {\n        c->auth_callback(c->id, c->auth_callback_privdata);\n\n        /* The callback will fire exactly once, even if the user remains \n         * the same. It is expected to completely clean up the state\n         * so all references are cleared here. */\n        c->auth_callback = NULL;\n        c->auth_callback_privdata = NULL;\n        c->auth_module = NULL;\n    }\n}\n\nvoid revokeClientAuthentication(client *c) {\n    /* Freeing the client would result in moduleNotifyUserChanged() to be\n     * called later, however since we use revokeClientAuthentication() also\n     * in moduleFreeAuthenticatedClients() to implement module unloading, we\n     * do this action ASAP: this way if the module is unloaded, when the client\n     * is eventually freed we don't rely on the module to still exist. */\n    moduleNotifyUserChanged(c);\n\n    c->user = DefaultUser;\n    c->authenticated = 0;\n    /* We will write replies to this client later, so we can't close it\n     * directly even if async. */\n    if (c == serverTL->current_client) {\n        c->flags |= CLIENT_CLOSE_AFTER_COMMAND;\n    } else {\n        freeClientAsync(c);\n    }\n}\n\n/* Cleanup all clients that have been authenticated with this module. This\n * is called from onUnload() to give the module a chance to cleanup any\n * resources associated with clients it has authenticated. */\nstatic void moduleFreeAuthenticatedClients(RedisModule *module) {\n    listIter li;\n    listNode *ln;\n    listRewind(g_pserver->clients,&li);\n    while ((ln = listNext(&li)) != NULL) {\n        client *c = (client*)listNodeValue(ln);\n        if (!c->auth_module) continue;\n\n        RedisModule *auth_module = (RedisModule *) c->auth_module;\n        if (auth_module == module) { \n            revokeClientAuthentication(c);\n        }\n    }\n}\n\n/* Creates a Redis ACL user that the module can use to authenticate a client.\n * After obtaining the user, the module should set what such user can do\n * using the RM_SetUserACL() function. Once configured, the user\n * can be used in order to authenticate a connection, with the specified\n * ACL rules, using the RedisModule_AuthClientWithUser() function.\n *\n * Note that:\n *\n * * Users created here are not listed by the ACL command.\n * * Users created here are not checked for duplicated name, so it's up to\n *   the module calling this function to take care of not creating users\n *   with the same name.\n * * The created user can be used to authenticate multiple Redis connections.\n *\n * The caller can later free the user using the function\n * RM_FreeModuleUser(). When this function is called, if there are\n * still clients authenticated with this user, they are disconnected.\n * The function to free the user should only be used when the caller really\n * wants to invalidate the user to define a new one with different\n * capabilities. */\nRedisModuleUser *RM_CreateModuleUser(const char *name) {\n    RedisModuleUser *new_user = (RedisModuleUser*)zmalloc(sizeof(RedisModuleUser));\n    new_user->user = ACLCreateUnlinkedUser();\n\n    /* Free the previous temporarily assigned name to assign the new one */\n    sdsfree(new_user->user->name);\n    new_user->user->name = sdsnew(name);\n    return new_user;\n}\n\n/* Frees a given user and disconnects all of the clients that have been\n * authenticated with it. See RM_CreateModuleUser for detailed usage.*/\nint RM_FreeModuleUser(RedisModuleUser *user) {\n    ACLFreeUserAndKillClients(user->user);\n    zfree(user);\n    return REDISMODULE_OK;\n}\n\n/* Sets the permissions of a user created through the redis module \n * interface. The syntax is the same as ACL SETUSER, so refer to the \n * documentation in acl.c for more information. See RM_CreateModuleUser\n * for detailed usage.\n * \n * Returns REDISMODULE_OK on success and REDISMODULE_ERR on failure\n * and will set an errno describing why the operation failed. */\nint RM_SetModuleUserACL(RedisModuleUser *user, const char* acl) {\n    return ACLSetUser(user->user, acl, -1);\n}\n\n/* Authenticate the client associated with the context with \n * the provided user. Returns REDISMODULE_OK on success and\n * REDISMODULE_ERR on error.\n * \n * This authentication can be tracked with the optional callback and private\n * data fields. The callback will be called whenever the user of the client\n * changes. This callback should be used to cleanup any state that is being\n * kept in the module related to the client authentication. It will only be\n * called once, even when the user hasn't changed, in order to allow for a\n * new callback to be specified. If this authentication does not need to be\n * tracked, pass in NULL for the callback and privdata. \n * \n * If client_id is not NULL, it will be filled with the id of the client\n * that was authenticated. This can be used with the \n * RM_DeauthenticateAndCloseClient() API in order to deauthenticate a \n * previously authenticated client if the authentication is no longer valid. \n * \n * For expensive authentication operations, it is recommended to block the\n * client and do the authentication in the background and then attach the user\n * to the client in a threadsafe context.  */\nstatic int authenticateClientWithUser(RedisModuleCtx *ctx, user *user, RedisModuleUserChangedFunc callback, void *privdata, uint64_t *client_id) {\n    if (user->flags & USER_FLAG_DISABLED) {\n        return REDISMODULE_ERR;\n    }\n\n    /* Avoid settings which are meaningless and will be lost */\n    if (!ctx->client || (ctx->client->flags & CLIENT_MODULE)) {\n        return REDISMODULE_ERR;\n    }\n\n    moduleNotifyUserChanged(ctx->client);\n\n    ctx->client->user = user;\n    ctx->client->authenticated = 1;\n\n    if (callback) {\n        ctx->client->auth_callback = callback;\n        ctx->client->auth_callback_privdata = privdata;\n        ctx->client->auth_module = ctx->module;\n    }\n\n    if (client_id) {\n        *client_id = ctx->client->id;\n    }\n\n    return REDISMODULE_OK;\n}\n\n\n/* Authenticate the current context's user with the provided redis acl user. \n * Returns REDISMODULE_ERR if the user is disabled.\n * \n * See authenticateClientWithUser for information about callback, client_id,\n * and general usage for authentication. */\nint RM_AuthenticateClientWithUser(RedisModuleCtx *ctx, RedisModuleUser *module_user, RedisModuleUserChangedFunc callback, void *privdata, uint64_t *client_id) {\n    return authenticateClientWithUser(ctx, module_user->user, callback, privdata, client_id);\n}\n\n/* Authenticate the current context's user with the provided redis acl user. \n * Returns REDISMODULE_ERR if the user is disabled or the user does not exist.\n * \n * See authenticateClientWithUser for information about callback, client_id,\n * and general usage for authentication. */\nint RM_AuthenticateClientWithACLUser(RedisModuleCtx *ctx, const char *name, size_t len, RedisModuleUserChangedFunc callback, void *privdata, uint64_t *client_id) {\n    user *acl_user = ACLGetUserByName(name, len);\n\n    if (!acl_user) {\n        return REDISMODULE_ERR;\n    }\n    return authenticateClientWithUser(ctx, acl_user, callback, privdata, client_id);\n}\n\n/* Deauthenticate and close the client. The client resources will not be\n * be immediately freed, but will be cleaned up in a background job. This is \n * the recommended way to deauthenicate a client since most clients can't \n * handle users becoming deauthenticated. Returns REDISMODULE_ERR when the\n * client doesn't exist and REDISMODULE_OK when the operation was successful. \n * \n * The client ID is returned from the RM_AuthenticateClientWithUser and\n * RM_AuthenticateClientWithACLUser APIs, but can be obtained through\n * the CLIENT api or through server events. \n * \n * This function is not thread safe, and must be executed within the context\n * of a command or thread safe context. */\nint RM_DeauthenticateAndCloseClient(RedisModuleCtx *ctx, uint64_t client_id) {\n    UNUSED(ctx);\n    client *c = lookupClientByID(client_id);\n    if (c == NULL) return REDISMODULE_ERR;\n\n    /* Revoke also marks client to be closed ASAP */\n    revokeClientAuthentication(c);\n    return REDISMODULE_OK;\n}\n\n/* Return the X.509 client-side certificate used by the client to authenticate\n * this connection.\n *\n * The return value is an allocated RedisModuleString that is a X.509 certificate\n * encoded in PEM (Base64) format. It should be freed (or auto-freed) by the caller.\n *\n * A NULL value is returned in the following conditions:\n *\n * - Connection ID does not exist\n * - Connection is not a TLS connection\n * - Connection is a TLS connection but no client ceritifcate was used\n */\nRedisModuleString *RM_GetClientCertificate(RedisModuleCtx *ctx, uint64_t client_id) {\n    client *c = lookupClientByID(client_id);\n    if (c == NULL) return NULL;\n\n    sds cert = connTLSGetPeerCert(c->conn);\n    if (!cert) return NULL;\n\n    RedisModuleString *s = createObject(OBJ_STRING, cert);\n    if (ctx != NULL) autoMemoryAdd(ctx, REDISMODULE_AM_STRING, s);\n\n    return s;\n}\n\n/* --------------------------------------------------------------------------\n * ## Modules Dictionary API\n *\n * Implements a sorted dictionary (actually backed by a radix tree) with\n * the usual get / set / del / num-items API, together with an iterator\n * capable of going back and forth.\n * -------------------------------------------------------------------------- */\n\n/* Create a new dictionary. The 'ctx' pointer can be the current module context\n * or NULL, depending on what you want. Please follow the following rules:\n *\n * 1. Use a NULL context if you plan to retain a reference to this dictionary\n *    that will survive the time of the module callback where you created it.\n * 2. Use a NULL context if no context is available at the time you are creating\n *    the dictionary (of course...).\n * 3. However use the current callback context as 'ctx' argument if the\n *    dictionary time to live is just limited to the callback scope. In this\n *    case, if enabled, you can enjoy the automatic memory management that will\n *    reclaim the dictionary memory, as well as the strings returned by the\n *    Next / Prev dictionary iterator calls.\n */\nRedisModuleDict *RM_CreateDict(RedisModuleCtx *ctx) {\n    struct RedisModuleDict *d = (RedisModuleDict*)zmalloc(sizeof(*d), MALLOC_LOCAL);\n    d->rax = raxNew();\n    if (ctx != NULL) autoMemoryAdd(ctx,REDISMODULE_AM_DICT,d);\n    return d;\n}\n\n/* Free a dictionary created with RM_CreateDict(). You need to pass the\n * context pointer 'ctx' only if the dictionary was created using the\n * context instead of passing NULL. */\nvoid RM_FreeDict(RedisModuleCtx *ctx, RedisModuleDict *d) {\n    if (ctx != NULL) autoMemoryFreed(ctx,REDISMODULE_AM_DICT,d);\n    raxFree(d->rax);\n    zfree(d);\n}\n\n/* Return the size of the dictionary (number of keys). */\nuint64_t RM_DictSize(RedisModuleDict *d) {\n    return raxSize(d->rax);\n}\n\n/* Store the specified key into the dictionary, setting its value to the\n * pointer 'ptr'. If the key was added with success, since it did not\n * already exist, REDISMODULE_OK is returned. Otherwise if the key already\n * exists the function returns REDISMODULE_ERR. */\nint RM_DictSetC(RedisModuleDict *d, void *key, size_t keylen, void *ptr) {\n    int retval = raxTryInsert(d->rax,(unsigned char*)key,keylen,ptr,NULL);\n    return (retval == 1) ? REDISMODULE_OK : REDISMODULE_ERR;\n}\n\n/* Like RedisModule_DictSetC() but will replace the key with the new\n * value if the key already exists. */\nint RM_DictReplaceC(RedisModuleDict *d, void *key, size_t keylen, void *ptr) {\n    int retval = raxInsert(d->rax,(unsigned char*)key,keylen,ptr,NULL);\n    return (retval == 1) ? REDISMODULE_OK : REDISMODULE_ERR;\n}\n\n/* Like RedisModule_DictSetC() but takes the key as a RedisModuleString. */\nint RM_DictSet(RedisModuleDict *d, RedisModuleString *key, void *ptr) {\n    return RM_DictSetC(d,ptrFromObj(key),sdslen(szFromObj(key)),ptr);\n}\n\n/* Like RedisModule_DictReplaceC() but takes the key as a RedisModuleString. */\nint RM_DictReplace(RedisModuleDict *d, RedisModuleString *key, void *ptr) {\n    return RM_DictReplaceC(d,ptrFromObj(key),sdslen(szFromObj(key)),ptr);\n}\n\n/* Return the value stored at the specified key. The function returns NULL\n * both in the case the key does not exist, or if you actually stored\n * NULL at key. So, optionally, if the 'nokey' pointer is not NULL, it will\n * be set by reference to 1 if the key does not exist, or to 0 if the key\n * exists. */\nvoid *RM_DictGetC(RedisModuleDict *d, void *key, size_t keylen, int *nokey) {\n    void *res = raxFind(d->rax,(unsigned char*)key,keylen);\n    if (nokey) *nokey = (res == raxNotFound);\n    return (res == raxNotFound) ? NULL : res;\n}\n\n/* Like RedisModule_DictGetC() but takes the key as a RedisModuleString. */\nvoid *RM_DictGet(RedisModuleDict *d, RedisModuleString *key, int *nokey) {\n    return RM_DictGetC(d,ptrFromObj(key),sdslen(szFromObj(key)),nokey);\n}\n\n/* Remove the specified key from the dictionary, returning REDISMODULE_OK if\n * the key was found and delted, or REDISMODULE_ERR if instead there was\n * no such key in the dictionary. When the operation is successful, if\n * 'oldval' is not NULL, then '*oldval' is set to the value stored at the\n * key before it was deleted. Using this feature it is possible to get\n * a pointer to the value (for instance in order to release it), without\n * having to call RedisModule_DictGet() before deleting the key. */\nint RM_DictDelC(RedisModuleDict *d, void *key, size_t keylen, void *oldval) {\n    int retval = raxRemove(d->rax,(unsigned char*)key,keylen,(void**)oldval);\n    return retval ? REDISMODULE_OK : REDISMODULE_ERR;\n}\n\n/* Like RedisModule_DictDelC() but gets the key as a RedisModuleString. */\nint RM_DictDel(RedisModuleDict *d, RedisModuleString *key, void *oldval) {\n    return RM_DictDelC(d,ptrFromObj(key),sdslen(szFromObj(key)),oldval);\n}\n\n/* Return an iterator, setup in order to start iterating from the specified\n * key by applying the operator 'op', which is just a string specifying the\n * comparison operator to use in order to seek the first element. The\n * operators available are:\n *\n * * `^`   -- Seek the first (lexicographically smaller) key.\n * * `$`   -- Seek the last  (lexicographically biffer) key.\n * * `>`   -- Seek the first element greater than the specified key.\n * * `>=`  -- Seek the first element greater or equal than the specified key.\n * * `<`   -- Seek the first element smaller than the specified key.\n * * `<=`  -- Seek the first element smaller or equal than the specified key.\n * * `==`  -- Seek the first element matching exactly the specified key.\n *\n * Note that for `^` and `$` the passed key is not used, and the user may\n * just pass NULL with a length of 0.\n *\n * If the element to start the iteration cannot be seeked based on the\n * key and operator passed, RedisModule_DictNext() / Prev() will just return\n * REDISMODULE_ERR at the first call, otherwise they'll produce elements.\n */\nRedisModuleDictIter *RM_DictIteratorStartC(RedisModuleDict *d, const char *op, void *key, size_t keylen) {\n    RedisModuleDictIter *di = (RedisModuleDictIter*)zmalloc(sizeof(*di), MALLOC_LOCAL);\n    di->dict = d;\n    raxStart(&di->ri,d->rax);\n    raxSeek(&di->ri,op,(unsigned char*)key,keylen);\n    return di;\n}\n\n/* Exactly like RedisModule_DictIteratorStartC, but the key is passed as a\n * RedisModuleString. */\nRedisModuleDictIter *RM_DictIteratorStart(RedisModuleDict *d, const char *op, RedisModuleString *key) {\n    return RM_DictIteratorStartC(d,op,ptrFromObj(key),sdslen(szFromObj(key)));\n}\n\n/* Release the iterator created with RedisModule_DictIteratorStart(). This call\n * is mandatory otherwise a memory leak is introduced in the module. */\nvoid RM_DictIteratorStop(RedisModuleDictIter *di) {\n    raxStop(&di->ri);\n    zfree(di);\n}\n\n/* After its creation with RedisModule_DictIteratorStart(), it is possible to\n * change the currently selected element of the iterator by using this\n * API call. The result based on the operator and key is exactly like\n * the function RedisModule_DictIteratorStart(), however in this case the\n * return value is just REDISMODULE_OK in case the seeked element was found,\n * or REDISMODULE_ERR in case it was not possible to seek the specified\n * element. It is possible to reseek an iterator as many times as you want. */\nint RM_DictIteratorReseekC(RedisModuleDictIter *di, const char *op, void *key, size_t keylen) {\n    return raxSeek(&di->ri,op,(unsigned char*)key,keylen);\n}\n\n/* Like RedisModule_DictIteratorReseekC() but takes the key as as a\n * RedisModuleString. */\nint RM_DictIteratorReseek(RedisModuleDictIter *di, const char *op, RedisModuleString *key) {\n    return RM_DictIteratorReseekC(di,op,ptrFromObj(key),sdslen(szFromObj(key)));\n}\n\n/* Return the current item of the dictionary iterator `di` and steps to the\n * next element. If the iterator already yield the last element and there\n * are no other elements to return, NULL is returned, otherwise a pointer\n * to a string representing the key is provided, and the `*keylen` length\n * is set by reference (if keylen is not NULL). The `*dataptr`, if not NULL\n * is set to the value of the pointer stored at the returned key as auxiliary\n * data (as set by the RedisModule_DictSet API).\n *\n * Usage example:\n *\n *      ... create the iterator here ...\n *      char *key;\n *      void *data;\n *      while((key = RedisModule_DictNextC(iter,&keylen,&data)) != NULL) {\n *          printf(\"%.*s %p\\n\", (int)keylen, key, data);\n *      }\n *\n * The returned pointer is of type void because sometimes it makes sense\n * to cast it to a `char*` sometimes to an unsigned `char*` depending on the\n * fact it contains or not binary data, so this API ends being more\n * comfortable to use.\n *\n * The validity of the returned pointer is until the next call to the\n * next/prev iterator step. Also the pointer is no longer valid once the\n * iterator is released. */\nvoid *RM_DictNextC(RedisModuleDictIter *di, size_t *keylen, void **dataptr) {\n    if (!raxNext(&di->ri)) return NULL;\n    if (keylen) *keylen = di->ri.key_len;\n    if (dataptr) *dataptr = di->ri.data;\n    return di->ri.key;\n}\n\n/* This function is exactly like RedisModule_DictNext() but after returning\n * the currently selected element in the iterator, it selects the previous\n * element (laxicographically smaller) instead of the next one. */\nvoid *RM_DictPrevC(RedisModuleDictIter *di, size_t *keylen, void **dataptr) {\n    if (!raxPrev(&di->ri)) return NULL;\n    if (keylen) *keylen = di->ri.key_len;\n    if (dataptr) *dataptr = di->ri.data;\n    return di->ri.key;\n}\n\n/* Like RedisModuleNextC(), but instead of returning an internally allocated\n * buffer and key length, it returns directly a module string object allocated\n * in the specified context 'ctx' (that may be NULL exactly like for the main\n * API RedisModule_CreateString).\n *\n * The returned string object should be deallocated after use, either manually\n * or by using a context that has automatic memory management active. */\nRedisModuleString *RM_DictNext(RedisModuleCtx *ctx, RedisModuleDictIter *di, void **dataptr) {\n    size_t keylen;\n    void *key = RM_DictNextC(di,&keylen,dataptr);\n    if (key == NULL) return NULL;\n    return RM_CreateString(ctx,(const char*)key,keylen);\n}\n\n/* Like RedisModule_DictNext() but after returning the currently selected\n * element in the iterator, it selects the previous element (laxicographically\n * smaller) instead of the next one. */\nRedisModuleString *RM_DictPrev(RedisModuleCtx *ctx, RedisModuleDictIter *di, void **dataptr) {\n    size_t keylen;\n    void *key = RM_DictPrevC(di,&keylen,dataptr);\n    if (key == NULL) return NULL;\n    return RM_CreateString(ctx,(const char*)key,keylen);\n}\n\n/* Compare the element currently pointed by the iterator to the specified\n * element given by key/keylen, according to the operator 'op' (the set of\n * valid operators are the same valid for RedisModule_DictIteratorStart).\n * If the comparison is successful the command returns REDISMODULE_OK\n * otherwise REDISMODULE_ERR is returned.\n *\n * This is useful when we want to just emit a lexicographical range, so\n * in the loop, as we iterate elements, we can also check if we are still\n * on range.\n *\n * The function return REDISMODULE_ERR if the iterator reached the\n * end of elements condition as well. */\nint RM_DictCompareC(RedisModuleDictIter *di, const char *op, void *key, size_t keylen) {\n    if (raxEOF(&di->ri)) return REDISMODULE_ERR;\n    int res = raxCompare(&di->ri,op,(unsigned char*)key,keylen);\n    return res ? REDISMODULE_OK : REDISMODULE_ERR;\n}\n\n/* Like RedisModule_DictCompareC but gets the key to compare with the current\n * iterator key as a RedisModuleString. */\nint RM_DictCompare(RedisModuleDictIter *di, const char *op, RedisModuleString *key) {\n    if (raxEOF(&di->ri)) return REDISMODULE_ERR;\n    int res = raxCompare(&di->ri,op,(unsigned char*)szFromObj(key),sdslen(szFromObj(key)));\n    return res ? REDISMODULE_OK : REDISMODULE_ERR;\n}\n\n\n\n\n/* --------------------------------------------------------------------------\n * ## Modules Info fields\n * -------------------------------------------------------------------------- */\n\nint RM_InfoEndDictField(RedisModuleInfoCtx *ctx);\n\n/* Used to start a new section, before adding any fields. the section name will\n * be prefixed by `<modulename>_` and must only include A-Z,a-z,0-9.\n * NULL or empty string indicates the default section (only `<modulename>`) is used.\n * When return value is REDISMODULE_ERR, the section should and will be skipped. */\nint RM_InfoAddSection(RedisModuleInfoCtx *ctx, char *name) {\n    sds full_name = sdsdup(ctx->module->name);\n    if (name != NULL && strlen(name) > 0)\n        full_name = sdscatfmt(full_name, \"_%s\", name);\n\n    /* Implicitly end dicts, instead of returning an error which is likely un checked. */\n    if (ctx->in_dict_field)\n        RM_InfoEndDictField(ctx);\n\n    /* proceed only if:\n     * 1) no section was requested (emit all)\n     * 2) the module name was requested (emit all)\n     * 3) this specific section was requested. */\n    if (ctx->requested_section) {\n        if (strcasecmp(ctx->requested_section, full_name) &&\n            strcasecmp(ctx->requested_section, ctx->module->name)) {\n            sdsfree(full_name);\n            ctx->in_section = 0;\n            return REDISMODULE_ERR;\n        }\n    }\n    if (ctx->sections++) ctx->info = sdscat(ctx->info,\"\\r\\n\");\n    ctx->info = sdscatfmt(ctx->info, \"# %S\\r\\n\", full_name);\n    ctx->in_section = 1;\n    sdsfree(full_name);\n    return REDISMODULE_OK;\n}\n\n/* Starts a dict field, similar to the ones in INFO KEYSPACE. Use normal\n * RedisModule_InfoAddField* functions to add the items to this field, and\n * terminate with RedisModule_InfoEndDictField. */\nint RM_InfoBeginDictField(RedisModuleInfoCtx *ctx, char *name) {\n    if (!ctx->in_section)\n        return REDISMODULE_ERR;\n    /* Implicitly end dicts, instead of returning an error which is likely un checked. */\n    if (ctx->in_dict_field)\n        RM_InfoEndDictField(ctx);\n    char *tmpmodname, *tmpname;\n    ctx->info = sdscatfmt(ctx->info,\n        \"%s_%s:\",\n        getSafeInfoString(ctx->module->name, strlen(ctx->module->name), &tmpmodname),\n        getSafeInfoString(name, strlen(name), &tmpname));\n    if (tmpmodname != NULL) zfree(tmpmodname);\n    if (tmpname != NULL) zfree(tmpname);\n    ctx->in_dict_field = 1;\n    return REDISMODULE_OK;\n}\n\n/* Ends a dict field, see RedisModule_InfoBeginDictField */\nint RM_InfoEndDictField(RedisModuleInfoCtx *ctx) {\n    if (!ctx->in_dict_field)\n        return REDISMODULE_ERR;\n    /* trim the last ',' if found. */\n    if (ctx->info[sdslen(ctx->info)-1]==',')\n        sdsIncrLen(ctx->info, -1);\n    ctx->info = sdscat(ctx->info, \"\\r\\n\");\n    ctx->in_dict_field = 0;\n    return REDISMODULE_OK;\n}\n\n/* Used by RedisModuleInfoFunc to add info fields.\n * Each field will be automatically prefixed by `<modulename>_`.\n * Field names or values must not include `\\r\\n` or `:`. */\nint RM_InfoAddFieldString(RedisModuleInfoCtx *ctx, char *field, RedisModuleString *value) {\n    if (!ctx->in_section)\n        return REDISMODULE_ERR;\n    if (ctx->in_dict_field) {\n        ctx->info = sdscatfmt(ctx->info,\n            \"%s=%S,\",\n            field,\n            (sds)szFromObj(value));\n        return REDISMODULE_OK;\n    }\n    ctx->info = sdscatfmt(ctx->info,\n        \"%s_%s:%S\\r\\n\",\n        ctx->module->name,\n        field,\n        (sds)szFromObj(value));\n    return REDISMODULE_OK;\n}\n\n/* See RedisModule_InfoAddFieldString(). */\nint RM_InfoAddFieldCString(RedisModuleInfoCtx *ctx, char *field, char *value) {\n    if (!ctx->in_section)\n        return REDISMODULE_ERR;\n    if (ctx->in_dict_field) {\n        ctx->info = sdscatfmt(ctx->info,\n            \"%s=%s,\",\n            field,\n            value);\n        return REDISMODULE_OK;\n    }\n    ctx->info = sdscatfmt(ctx->info,\n        \"%s_%s:%s\\r\\n\",\n        ctx->module->name,\n        field,\n        value);\n    return REDISMODULE_OK;\n}\n\n/* See RedisModule_InfoAddFieldString(). */\nint RM_InfoAddFieldDouble(RedisModuleInfoCtx *ctx, char *field, double value) {\n    if (!ctx->in_section)\n        return REDISMODULE_ERR;\n    if (ctx->in_dict_field) {\n        ctx->info = sdscatprintf(ctx->info,\n            \"%s=%.17g,\",\n            field,\n            value);\n        return REDISMODULE_OK;\n    }\n    ctx->info = sdscatprintf(ctx->info,\n        \"%s_%s:%.17g\\r\\n\",\n        ctx->module->name,\n        field,\n        value);\n    return REDISMODULE_OK;\n}\n\n/* See RedisModule_InfoAddFieldString(). */\nint RM_InfoAddFieldLongLong(RedisModuleInfoCtx *ctx, char *field, long long value) {\n    if (!ctx->in_section)\n        return REDISMODULE_ERR;\n    if (ctx->in_dict_field) {\n        ctx->info = sdscatfmt(ctx->info,\n            \"%s=%I,\",\n            field,\n            value);\n        return REDISMODULE_OK;\n    }\n    ctx->info = sdscatfmt(ctx->info,\n        \"%s_%s:%I\\r\\n\",\n        ctx->module->name,\n        field,\n        value);\n    return REDISMODULE_OK;\n}\n\n/* See RedisModule_InfoAddFieldString(). */\nint RM_InfoAddFieldULongLong(RedisModuleInfoCtx *ctx, char *field, unsigned long long value) {\n    if (!ctx->in_section)\n        return REDISMODULE_ERR;\n    if (ctx->in_dict_field) {\n        ctx->info = sdscatfmt(ctx->info,\n            \"%s=%U,\",\n            field,\n            value);\n        return REDISMODULE_OK;\n    }\n    ctx->info = sdscatfmt(ctx->info,\n        \"%s_%s:%U\\r\\n\",\n        ctx->module->name,\n        field,\n        value);\n    return REDISMODULE_OK;\n}\n\n/* Registers callback for the INFO command. The callback should add INFO fields\n * by calling the `RedisModule_InfoAddField*()` functions. */\nint RM_RegisterInfoFunc(RedisModuleCtx *ctx, RedisModuleInfoFunc cb) {\n    ctx->module->info_cb = cb;\n    return REDISMODULE_OK;\n}\n\nsds modulesCollectInfo(sds info, const char *section, int for_crash_report, int sections) {\n    dictIterator *di = dictGetIterator(modules);\n    dictEntry *de;\n\n    while ((de = dictNext(di)) != NULL) {\n        struct RedisModule *module = (RedisModule*)dictGetVal(de);\n        if (!module->info_cb)\n            continue;\n        RedisModuleInfoCtx info_ctx = {module, section, info, sections, 0, 0};\n        module->info_cb(&info_ctx, for_crash_report);\n        /* Implicitly end dicts (no way to handle errors, and we must add the newline). */\n        if (info_ctx.in_dict_field)\n            RM_InfoEndDictField(&info_ctx);\n        info = info_ctx.info;\n        sections = info_ctx.sections;\n    }\n    dictReleaseIterator(di);\n    return info;\n}\n\n/* Get information about the server similar to the one that returns from the\n * INFO command. This function takes an optional 'section' argument that may\n * be NULL. The return value holds the output and can be used with\n * RedisModule_ServerInfoGetField and alike to get the individual fields.\n * When done, it needs to be freed with RedisModule_FreeServerInfo or with the\n * automatic memory management mechanism if enabled. */\nRedisModuleServerInfoData *RM_GetServerInfo(RedisModuleCtx *ctx, const char *section) {\n    struct RedisModuleServerInfoData *d = (RedisModuleServerInfoData*)zmalloc(sizeof(*d));\n    d->rax = raxNew();\n    if (ctx != NULL) autoMemoryAdd(ctx,REDISMODULE_AM_INFO,d);\n    sds info = genRedisInfoString(section);\n    int totlines, i;\n    sds *lines = sdssplitlen(info, sdslen(info), \"\\r\\n\", 2, &totlines);\n    for(i=0; i<totlines; i++) {\n        sds line = lines[i];\n        if (line[0]=='#') continue;\n        char *sep = strchr(line, ':');\n        if (!sep) continue;\n        unsigned char *key = (unsigned char*)line;\n        size_t keylen = (intptr_t)sep-(intptr_t)line;\n        sds val = sdsnewlen(sep+1,sdslen(line)-((intptr_t)sep-(intptr_t)line)-1);\n        if (!raxTryInsert(d->rax,key,keylen,val,NULL))\n            sdsfree(val);\n    }\n    sdsfree(info);\n    sdsfreesplitres(lines,totlines);\n    return d;\n}\n\n/* Free data created with RM_GetServerInfo(). You need to pass the\n * context pointer 'ctx' only if the dictionary was created using the\n * context instead of passing NULL. */\nvoid RM_FreeServerInfo(RedisModuleCtx *ctx, RedisModuleServerInfoData *data) {\n    if (ctx != NULL) autoMemoryFreed(ctx,REDISMODULE_AM_INFO,data);\n    raxIterator ri;\n    raxStart(&ri,data->rax);\n    while(1) {\n        raxSeek(&ri,\"^\",NULL,0);\n        if (!raxNext(&ri)) break;\n        raxRemove(data->rax,(unsigned char*)ri.key,ri.key_len,NULL);\n        sdsfree((sds)ri.data);\n    }\n    raxStop(&ri);\n    raxFree(data->rax);\n    zfree(data);\n}\n\n/* Get the value of a field from data collected with RM_GetServerInfo(). You\n * need to pass the context pointer 'ctx' only if you want to use auto memory\n * mechanism to release the returned string. Return value will be NULL if the\n * field was not found. */\nRedisModuleString *RM_ServerInfoGetField(RedisModuleCtx *ctx, RedisModuleServerInfoData *data, const char* field) {\n    sds val = (sds)raxFind(data->rax, (unsigned char *)field, strlen(field));\n    if (val == raxNotFound) return NULL;\n    RedisModuleString *o = createStringObject(val,sdslen(val));\n    if (ctx != NULL) autoMemoryAdd(ctx,REDISMODULE_AM_STRING,o);\n    return o;\n}\n\n/* Similar to RM_ServerInfoGetField, but returns a char* which should not be freed but the caller. */\nconst char *RM_ServerInfoGetFieldC(RedisModuleServerInfoData *data, const char* field) {\n    sds val = (sds)raxFind(data->rax, (unsigned char *)field, strlen(field));\n    if (val == raxNotFound) return NULL;\n    return val;\n}\n\n/* Get the value of a field from data collected with RM_GetServerInfo(). If the\n * field is not found, or is not numerical or out of range, return value will be\n * 0, and the optional out_err argument will be set to REDISMODULE_ERR. */\nlong long RM_ServerInfoGetFieldSigned(RedisModuleServerInfoData *data, const char* field, int *out_err) {\n    long long ll;\n    sds val = (sds)raxFind(data->rax, (unsigned char *)field, strlen(field));\n    if (val == raxNotFound) {\n        if (out_err) *out_err = REDISMODULE_ERR;\n        return 0;\n    }\n    if (!string2ll(val,sdslen(val),&ll)) {\n        if (out_err) *out_err = REDISMODULE_ERR;\n        return 0;\n    }\n    if (out_err) *out_err = REDISMODULE_OK;\n    return ll;\n}\n\n/* Get the value of a field from data collected with RM_GetServerInfo(). If the\n * field is not found, or is not numerical or out of range, return value will be\n * 0, and the optional out_err argument will be set to REDISMODULE_ERR. */\nunsigned long long RM_ServerInfoGetFieldUnsigned(RedisModuleServerInfoData *data, const char* field, int *out_err) {\n    unsigned long long ll;\n    sds val = (sds)raxFind(data->rax, (unsigned char *)field, strlen(field));\n    if (val == raxNotFound) {\n        if (out_err) *out_err = REDISMODULE_ERR;\n        return 0;\n    }\n    if (!string2ull(val,&ll)) {\n        if (out_err) *out_err = REDISMODULE_ERR;\n        return 0;\n    }\n    if (out_err) *out_err = REDISMODULE_OK;\n    return ll;\n}\n\n/* Get the value of a field from data collected with RM_GetServerInfo(). If the\n * field is not found, or is not a double, return value will be 0, and the\n * optional out_err argument will be set to REDISMODULE_ERR. */\ndouble RM_ServerInfoGetFieldDouble(RedisModuleServerInfoData *data, const char* field, int *out_err) {\n    double dbl;\n    sds val = (sds)raxFind(data->rax, (unsigned char *)field, strlen(field));\n    if (val == raxNotFound) {\n        if (out_err) *out_err = REDISMODULE_ERR;\n        return 0;\n    }\n    if (!string2d(val,sdslen(val),&dbl)) {\n        if (out_err) *out_err = REDISMODULE_ERR;\n        return 0;\n    }\n    if (out_err) *out_err = REDISMODULE_OK;\n    return dbl;\n}\n\n/* --------------------------------------------------------------------------\n * ## Modules utility APIs\n * -------------------------------------------------------------------------- */\n\n/* Return random bytes using SHA1 in counter mode with a /dev/urandom\n * initialized seed. This function is fast so can be used to generate\n * many bytes without any effect on the operating system entropy pool.\n * Currently this function is not thread safe. */\nvoid RM_GetRandomBytes(unsigned char *dst, size_t len) {\n    getRandomBytes(dst,len);\n}\n\n/* Like RedisModule_GetRandomBytes() but instead of setting the string to\n * random bytes the string is set to random characters in the in the\n * hex charset [0-9a-f]. */\nvoid RM_GetRandomHexChars(char *dst, size_t len) {\n    getRandomHexChars(dst,len);\n}\n\n/* --------------------------------------------------------------------------\n * ## Modules API exporting / importing\n * -------------------------------------------------------------------------- */\n\n/* This function is called by a module in order to export some API with a\n * given name. Other modules will be able to use this API by calling the\n * symmetrical function RM_GetSharedAPI() and casting the return value to\n * the right function pointer.\n *\n * The function will return REDISMODULE_OK if the name is not already taken,\n * otherwise REDISMODULE_ERR will be returned and no operation will be\n * performed.\n *\n * IMPORTANT: the apiname argument should be a string literal with static\n * lifetime. The API relies on the fact that it will always be valid in\n * the future. */\nint RM_ExportSharedAPI(RedisModuleCtx *ctx, const char *apiname, void *func) {\n    RedisModuleSharedAPI *sapi = (RedisModuleSharedAPI*)zmalloc(sizeof(*sapi));\n    sapi->module = ctx->module;\n    sapi->func = func;\n    if (dictAdd(g_pserver->sharedapi, (char*)apiname, sapi) != DICT_OK) {\n        zfree(sapi);\n        return REDISMODULE_ERR;\n    }\n    return REDISMODULE_OK;\n}\n\n/* Request an exported API pointer. The return value is just a void pointer\n * that the caller of this function will be required to cast to the right\n * function pointer, so this is a private contract between modules.\n *\n * If the requested API is not available then NULL is returned. Because\n * modules can be loaded at different times with different order, this\n * function calls should be put inside some module generic API registering\n * step, that is called every time a module attempts to execute a\n * command that requires external APIs: if some API cannot be resolved, the\n * command should return an error.\n *\n * Here is an example:\n *\n *     int ... myCommandImplementation() {\n *        if (getExternalAPIs() == 0) {\n *             reply with an error here if we cannot have the APIs\n *        }\n *        // Use the API:\n *        myFunctionPointer(foo);\n *     }\n *\n * And the function registerAPI() is:\n *\n *     int getExternalAPIs(void) {\n *         static int api_loaded = 0;\n *         if (api_loaded != 0) return 1; // APIs already resolved.\n *\n *         myFunctionPointer = RedisModule_GetOtherModuleAPI(\"...\");\n *         if (myFunctionPointer == NULL) return 0;\n *\n *         return 1;\n *     }\n */\nvoid *RM_GetSharedAPI(RedisModuleCtx *ctx, const char *apiname) {\n    dictEntry *de = dictFind(g_pserver->sharedapi, apiname);\n    if (de == NULL) return NULL;\n    RedisModuleSharedAPI *sapi = (RedisModuleSharedAPI*)dictGetVal(de);\n    if (listSearchKey(sapi->module->usedby,ctx->module) == NULL) {\n        listAddNodeTail(sapi->module->usedby,ctx->module);\n        listAddNodeTail(ctx->module->usingMods,sapi->module);\n    }\n    return sapi->func;\n}\n\n/* Remove all the APIs registered by the specified module. Usually you\n * want this when the module is going to be unloaded. This function\n * assumes that's caller responsibility to make sure the APIs are not\n * used by other modules.\n *\n * The number of unregistered APIs is returned. */\nint moduleUnregisterSharedAPI(RedisModule *module) {\n    int count = 0;\n    dictIterator *di = dictGetSafeIterator(g_pserver->sharedapi);\n    dictEntry *de;\n    while ((de = dictNext(di)) != NULL) {\n        const char *apiname = (const char*)dictGetKey(de);\n        RedisModuleSharedAPI *sapi = (RedisModuleSharedAPI*)dictGetVal(de);\n        if (sapi->module == module) {\n            dictDelete(g_pserver->sharedapi,apiname);\n            zfree(sapi);\n            count++;\n        }\n    }\n    dictReleaseIterator(di);\n    return count;\n}\n\n/* Remove the specified module as an user of APIs of ever other module.\n * This is usually called when a module is unloaded.\n *\n * Returns the number of modules this module was using APIs from. */\nint moduleUnregisterUsedAPI(RedisModule *module) {\n    listIter li;\n    listNode *ln;\n    int count = 0;\n\n    listRewind(module->usingMods,&li);\n    while((ln = listNext(&li))) {\n        RedisModule *used = (RedisModule*)ln->value;\n        listNode *ln = listSearchKey(used->usedby,module);\n        if (ln) {\n            listDelNode(used->usedby,ln);\n            count++;\n        }\n    }\n    return count;\n}\n\n/* Unregister all filters registered by a module.\n * This is called when a module is being unloaded.\n * \n * Returns the number of filters unregistered. */\nint moduleUnregisterFilters(RedisModule *module) {\n    listIter li;\n    listNode *ln;\n    int count = 0;\n\n    listRewind(module->filters,&li);\n    while((ln = listNext(&li))) {\n        RedisModuleCommandFilter *filter = (RedisModuleCommandFilter*)ln->value;\n        listNode *ln = listSearchKey(moduleCommandFilters,filter);\n        if (ln) {\n            listDelNode(moduleCommandFilters,ln);\n            count++;\n        }\n        zfree(filter);\n    }\n    return count;\n}\n\n/* --------------------------------------------------------------------------\n * ## Module Command Filter API\n * -------------------------------------------------------------------------- */\n\n/* Register a new command filter function.\n *\n * Command filtering makes it possible for modules to extend Redis by plugging\n * into the execution flow of all commands.\n *\n * A registered filter gets called before Redis executes *any* command.  This\n * includes both core Redis commands and commands registered by any module.  The\n * filter applies in all execution paths including:\n *\n * 1. Invocation by a client.\n * 2. Invocation through `RedisModule_Call()` by any module.\n * 3. Invocation through Lua 'redis.call()`.\n * 4. Replication of a command from a master.\n *\n * The filter executes in a special filter context, which is different and more\n * limited than a RedisModuleCtx.  Because the filter affects any command, it\n * must be implemented in a very efficient way to reduce the performance impact\n * on Redis.  All Redis Module API calls that require a valid context (such as\n * `RedisModule_Call()`, `RedisModule_OpenKey()`, etc.) are not supported in a\n * filter context.\n *\n * The `RedisModuleCommandFilterCtx` can be used to inspect or modify the\n * executed command and its arguments.  As the filter executes before Redis\n * begins processing the command, any change will affect the way the command is\n * processed.  For example, a module can override Redis commands this way:\n *\n * 1. Register a `MODULE.SET` command which implements an extended version of\n *    the Redis `SET` command.\n * 2. Register a command filter which detects invocation of `SET` on a specific\n *    pattern of keys.  Once detected, the filter will replace the first\n *    argument from `SET` to `MODULE.SET`.\n * 3. When filter execution is complete, Redis considers the new command name\n *    and therefore executes the module's own command.\n *\n * Note that in the above use case, if `MODULE.SET` itself uses\n * `RedisModule_Call()` the filter will be applied on that call as well.  If\n * that is not desired, the `REDISMODULE_CMDFILTER_NOSELF` flag can be set when\n * registering the filter.\n *\n * The `REDISMODULE_CMDFILTER_NOSELF` flag prevents execution flows that\n * originate from the module's own `RM_Call()` from reaching the filter.  This\n * flag is effective for all execution flows, including nested ones, as long as\n * the execution begins from the module's command context or a thread-safe\n * context that is associated with a blocking command.\n *\n * Detached thread-safe contexts are *not* associated with the module and cannot\n * be protected by this flag.\n *\n * If multiple filters are registered (by the same or different modules), they\n * are executed in the order of registration.\n */\n\nRedisModuleCommandFilter *RM_RegisterCommandFilter(RedisModuleCtx *ctx, RedisModuleCommandFilterFunc callback, int flags) {\n    RedisModuleCommandFilter *filter = (RedisModuleCommandFilter*)zmalloc(sizeof(*filter), MALLOC_LOCAL);\n    filter->module = ctx->module;\n    filter->callback = callback;\n    filter->flags = flags;\n\n    listAddNodeTail(moduleCommandFilters, filter);\n    listAddNodeTail(ctx->module->filters, filter);\n    return filter;\n}\n\n/* Unregister a command filter.\n */\nint RM_UnregisterCommandFilter(RedisModuleCtx *ctx, RedisModuleCommandFilter *filter) {\n    listNode *ln;\n\n    /* A module can only remove its own filters */\n    if (filter->module != ctx->module) return REDISMODULE_ERR;\n\n    ln = listSearchKey(moduleCommandFilters,filter);\n    if (!ln) return REDISMODULE_ERR;\n    listDelNode(moduleCommandFilters,ln);\n\n    ln = listSearchKey(ctx->module->filters,filter);\n    if (!ln) return REDISMODULE_ERR;    /* Shouldn't happen */\n    listDelNode(ctx->module->filters,ln);\n\n    zfree(filter);\n\n    return REDISMODULE_OK;\n}\n\nint moduleHasCommandFilters()\n{\n    // Note: called outside the global lock\n    return listLength(moduleCommandFilters);   \n}\n\nvoid moduleCallCommandFilters(client *c) {\n    if (listLength(moduleCommandFilters) == 0) return;\n\n    listIter li;\n    listNode *ln;\n    listRewind(moduleCommandFilters,&li);\n\n    RedisModuleCommandFilterCtx filter = { c->argv, c->argc };\n\n    while((ln = listNext(&li))) {\n        RedisModuleCommandFilter *f = (RedisModuleCommandFilter*)ln->value;\n\n        /* Skip filter if REDISMODULE_CMDFILTER_NOSELF is set and module is\n         * currently processing a command.\n         */\n        if ((f->flags & REDISMODULE_CMDFILTER_NOSELF) && f->module->in_call) continue;\n\n        /* Call filter */\n        f->callback(&filter);\n    }\n\n    c->argv = filter.argv;\n    c->argc = filter.argc;\n}\n\n/* Return the number of arguments a filtered command has.  The number of\n * arguments include the command itself.\n */\nint RM_CommandFilterArgsCount(RedisModuleCommandFilterCtx *fctx)\n{\n    return fctx->argc;\n}\n\n/* Return the specified command argument.  The first argument (position 0) is\n * the command itself, and the rest are user-provided args.\n */\nconst RedisModuleString *RM_CommandFilterArgGet(RedisModuleCommandFilterCtx *fctx, int pos)\n{\n    if (pos < 0 || pos >= fctx->argc) return NULL;\n    return fctx->argv[pos];\n}\n\n/* Modify the filtered command by inserting a new argument at the specified\n * position.  The specified RedisModuleString argument may be used by Redis\n * after the filter context is destroyed, so it must not be auto-memory\n * allocated, freed or used elsewhere.\n */\nint RM_CommandFilterArgInsert(RedisModuleCommandFilterCtx *fctx, int pos, RedisModuleString *arg)\n{\n    int i;\n\n    if (pos < 0 || pos > fctx->argc) return REDISMODULE_ERR;\n\n    fctx->argv = (robj**)zrealloc(fctx->argv, (fctx->argc+1)*sizeof(RedisModuleString *), MALLOC_LOCAL);\n    for (i = fctx->argc; i > pos; i--) {\n        fctx->argv[i] = fctx->argv[i-1];\n    }\n    fctx->argv[pos] = arg;\n    fctx->argc++;\n\n    return REDISMODULE_OK;\n}\n\n/* Modify the filtered command by replacing an existing argument with a new one.\n * The specified RedisModuleString argument may be used by Redis after the\n * filter context is destroyed, so it must not be auto-memory allocated, freed\n * or used elsewhere.\n */\nint RM_CommandFilterArgReplace(RedisModuleCommandFilterCtx *fctx, int pos, RedisModuleString *arg)\n{\n    if (pos < 0 || pos >= fctx->argc) return REDISMODULE_ERR;\n\n    decrRefCount(fctx->argv[pos]);\n    fctx->argv[pos] = arg;\n\n    return REDISMODULE_OK;\n}\n\n/* Modify the filtered command by deleting an argument at the specified\n * position.\n */\nint RM_CommandFilterArgDelete(RedisModuleCommandFilterCtx *fctx, int pos)\n{\n    int i;\n    if (pos < 0 || pos >= fctx->argc) return REDISMODULE_ERR;\n\n    decrRefCount(fctx->argv[pos]);\n    for (i = pos; i < fctx->argc-1; i++) {\n        fctx->argv[i] = fctx->argv[i+1];\n    }\n    fctx->argc--;\n\n    return REDISMODULE_OK;\n}\n\n/* For a given pointer allocated via RedisModule_Alloc() or\n * RedisModule_Realloc(), return the amount of memory allocated for it.\n * Note that this may be different (larger) than the memory we allocated\n * with the allocation calls, since sometimes the underlying allocator\n * will allocate more memory.\n */\nsize_t RM_MallocSize(void* ptr){\n    return zmalloc_size(ptr);\n}\n\n/* Return the a number between 0 to 1 indicating the amount of memory\n * currently used, relative to the Redis \"maxmemory\" configuration.\n *\n * * 0 - No memory limit configured.\n * * Between 0 and 1 - The percentage of the memory used normalized in 0-1 range.\n * * Exactly 1 - Memory limit reached.\n * * Greater 1 - More memory used than the configured limit.\n */\nfloat RM_GetUsedMemoryRatio(){\n    float level;\n    getMaxmemoryState(NULL, NULL, NULL, &level);\n    return level;\n}\n\n/* --------------------------------------------------------------------------\n * ## Scanning keyspace and hashes\n * -------------------------------------------------------------------------- */\n\ntypedef void (*RedisModuleScanCB)(RedisModuleCtx *ctx, RedisModuleString *keyname, RedisModuleKey *key, void *privdata);\ntypedef struct {\n    RedisModuleCtx *ctx;\n    void* user_data;\n    RedisModuleScanCB fn;\n} ScanCBData;\n\ntypedef struct RedisModuleScanCursor{\n    int cursor;\n    int done;\n}RedisModuleScanCursor;\n\nstatic void moduleScanCallback(void *privdata, const dictEntry *de) {\n    ScanCBData *data = (ScanCBData*)privdata;\n    sds key = (sds)dictGetKey(de);\n    robj* val = (robj*)dictGetVal(de);\n    RedisModuleString *keyname = createObject(OBJ_STRING,sdsdup(key));\n\n    /* Setup the key handle. */\n    RedisModuleKey kp = {0};\n    moduleInitKey(&kp, data->ctx, keyname, val, REDISMODULE_READ);\n\n    data->fn(data->ctx, keyname, &kp, data->user_data);\n\n    moduleCloseKey(&kp);\n    decrRefCount(keyname);\n}\n\n/* Create a new cursor to be used with RedisModule_Scan */\nRedisModuleScanCursor *RM_ScanCursorCreate() {\n    RedisModuleScanCursor* cursor = (RedisModuleScanCursor*)zmalloc(sizeof(*cursor));\n    cursor->cursor = 0;\n    cursor->done = 0;\n    return cursor;\n}\n\n/* Restart an existing cursor. The keys will be rescanned. */\nvoid RM_ScanCursorRestart(RedisModuleScanCursor *cursor) {\n    cursor->cursor = 0;\n    cursor->done = 0;\n}\n\n/* Destroy the cursor struct. */\nvoid RM_ScanCursorDestroy(RedisModuleScanCursor *cursor) {\n    zfree(cursor);\n}\n\n/* Scan API that allows a module to scan all the keys and value in\n * the selected db.\n *\n * Callback for scan implementation.\n *\n *     void scan_callback(RedisModuleCtx *ctx, RedisModuleString *keyname,\n *                        RedisModuleKey *key, void *privdata);\n *\n * - `ctx`: the redis module context provided to for the scan.\n * - `keyname`: owned by the caller and need to be retained if used after this\n *   function.\n * - `key`: holds info on the key and value, it is provided as best effort, in\n *   some cases it might be NULL, in which case the user should (can) use\n *   RedisModule_OpenKey() (and CloseKey too).\n *   when it is provided, it is owned by the caller and will be free when the\n *   callback returns.\n * - `privdata`: the user data provided to RedisModule_Scan().\n *\n * The way it should be used:\n *\n *      RedisModuleCursor *c = RedisModule_ScanCursorCreate();\n *      while(RedisModule_Scan(ctx, c, callback, privateData));\n *      RedisModule_ScanCursorDestroy(c);\n *\n * It is also possible to use this API from another thread while the lock\n * is acquired during the actuall call to RM_Scan:\n *\n *      RedisModuleCursor *c = RedisModule_ScanCursorCreate();\n *      RedisModule_ThreadSafeContextLock(ctx);\n *      while(RedisModule_Scan(ctx, c, callback, privateData)){\n *          RedisModule_ThreadSafeContextUnlock(ctx);\n *          // do some background job\n *          RedisModule_ThreadSafeContextLock(ctx);\n *      }\n *      RedisModule_ScanCursorDestroy(c);\n *\n * The function will return 1 if there are more elements to scan and\n * 0 otherwise, possibly setting errno if the call failed.\n *\n * It is also possible to restart an existing cursor using RM_ScanCursorRestart.\n *\n * IMPORTANT: This API is very similar to the Redis SCAN command from the\n * point of view of the guarantees it provides. This means that the API\n * may report duplicated keys, but guarantees to report at least one time\n * every key that was there from the start to the end of the scanning process.\n *\n * NOTE: If you do database changes within the callback, you should be aware\n * that the internal state of the database may change. For instance it is safe\n * to delete or modify the current key, but may not be safe to delete any\n * other key.\n * Moreover playing with the Redis keyspace while iterating may have the\n * effect of returning more duplicates. A safe pattern is to store the keys\n * names you want to modify elsewhere, and perform the actions on the keys\n * later when the iteration is complete. However this can cost a lot of\n * memory, so it may make sense to just operate on the current key when\n * possible during the iteration, given that this is safe. */\nint RM_Scan(RedisModuleCtx *ctx, RedisModuleScanCursor *cursor, RedisModuleScanCB fn, void *privdata) {\n    if (cursor->done) {\n        errno = ENOENT;\n        return 0;\n    }\n    int ret = 1;\n    ScanCBData data = { ctx, privdata, fn };\n    cursor->cursor = dictScan(ctx->client->db->dictUnsafeKeyOnly(), cursor->cursor, moduleScanCallback, NULL, &data);\n    if (cursor->cursor == 0) {\n        cursor->done = 1;\n        ret = 0;\n    }\n    errno = 0;\n    return ret;\n}\n\ntypedef void (*RedisModuleScanKeyCB)(RedisModuleKey *key, RedisModuleString *field, RedisModuleString *value, void *privdata);\ntypedef struct {\n    RedisModuleKey *key;\n    void* user_data;\n    RedisModuleScanKeyCB fn;\n} ScanKeyCBData;\n\nstatic void moduleScanKeyCallback(void *privdata, const dictEntry *de) {\n    ScanKeyCBData *data = (ScanKeyCBData*)privdata;\n    sds key = (sds)dictGetKey(de);\n    robj *o = data->key->value;\n    robj *field = createStringObject(key, sdslen(key));\n    robj *value = NULL;\n    if (o->type == OBJ_SET) {\n        value = NULL;\n    } else if (o->type == OBJ_HASH) {\n        sds val = (sds)dictGetVal(de);\n        value = createStringObject(val, sdslen(val));\n    } else if (o->type == OBJ_ZSET) {\n        double *val = (double*)dictGetVal(de);\n        value = createStringObjectFromLongDouble(*val, 0);\n    }\n\n    data->fn(data->key, field, value, data->user_data);\n    decrRefCount(field);\n    if (value) decrRefCount(value);\n}\n\n/* Scan api that allows a module to scan the elements in a hash, set or sorted set key\n *\n * Callback for scan implementation.\n *\n *     void scan_callback(RedisModuleKey *key, RedisModuleString* field, RedisModuleString* value, void *privdata);\n *\n * - key - the redis key context provided to for the scan.\n * - field - field name, owned by the caller and need to be retained if used\n *   after this function.\n * - value - value string or NULL for set type, owned by the caller and need to\n *   be retained if used after this function.\n * - privdata - the user data provided to RedisModule_ScanKey.\n *\n * The way it should be used:\n *\n *      RedisModuleCursor *c = RedisModule_ScanCursorCreate();\n *      RedisModuleKey *key = RedisModule_OpenKey(...)\n *      while(RedisModule_ScanKey(key, c, callback, privateData));\n *      RedisModule_CloseKey(key);\n *      RedisModule_ScanCursorDestroy(c);\n *\n * It is also possible to use this API from another thread while the lock is acquired during\n * the actuall call to RM_ScanKey, and re-opening the key each time:\n *\n *      RedisModuleCursor *c = RedisModule_ScanCursorCreate();\n *      RedisModule_ThreadSafeContextLock(ctx);\n *      RedisModuleKey *key = RedisModule_OpenKey(...)\n *      while(RedisModule_ScanKey(ctx, c, callback, privateData)){\n *          RedisModule_CloseKey(key);\n *          RedisModule_ThreadSafeContextUnlock(ctx);\n *          // do some background job\n *          RedisModule_ThreadSafeContextLock(ctx);\n *          RedisModuleKey *key = RedisModule_OpenKey(...)\n *      }\n *      RedisModule_CloseKey(key);\n *      RedisModule_ScanCursorDestroy(c);\n *\n * The function will return 1 if there are more elements to scan and 0 otherwise,\n * possibly setting errno if the call failed.\n * It is also possible to restart an existing cursor using RM_ScanCursorRestart.\n *\n * NOTE: Certain operations are unsafe while iterating the object. For instance\n * while the API guarantees to return at least one time all the elements that\n * are present in the data structure consistently from the start to the end\n * of the iteration (see HSCAN and similar commands documentation), the more\n * you play with the elements, the more duplicates you may get. In general\n * deleting the current element of the data structure is safe, while removing\n * the key you are iterating is not safe. */\nint RM_ScanKey(RedisModuleKey *key, RedisModuleScanCursor *cursor, RedisModuleScanKeyCB fn, void *privdata) {\n    if (key == NULL || key->value == NULL) {\n        errno = EINVAL;\n        return 0;\n    }\n    dict *ht = NULL;\n    robj *o = key->value;\n    if (o->type == OBJ_SET) {\n        if (o->encoding == OBJ_ENCODING_HT)\n            ht = (dict*)ptrFromObj(o);\n    } else if (o->type == OBJ_HASH) {\n        if (o->encoding == OBJ_ENCODING_HT)\n            ht = (dict*)ptrFromObj(o);\n    } else if (o->type == OBJ_ZSET) {\n        if (o->encoding == OBJ_ENCODING_SKIPLIST)\n            ht = ((zset *)ptrFromObj(o))->dict;\n    } else {\n        errno = EINVAL;\n        return 0;\n    }\n    if (cursor->done) {\n        errno = ENOENT;\n        return 0;\n    }\n    int ret = 1;\n    if (ht) {\n        ScanKeyCBData data = { key, privdata, fn };\n        cursor->cursor = dictScan(ht, cursor->cursor, moduleScanKeyCallback, NULL, &data);\n        if (cursor->cursor == 0) {\n            cursor->done = 1;\n            ret = 0;\n        }\n    } else if (o->type == OBJ_SET && o->encoding == OBJ_ENCODING_INTSET) {\n        int pos = 0;\n        int64_t ll;\n        while(intsetGet((intset*)ptrFromObj(o),pos++,&ll)) {\n            robj *field = createObject(OBJ_STRING,sdsfromlonglong(ll));\n            fn(key, field, NULL, privdata);\n            decrRefCount(field);\n        }\n        cursor->cursor = 1;\n        cursor->done = 1;\n        ret = 0;\n    } else if (o->type == OBJ_HASH || o->type == OBJ_ZSET) {\n        unsigned char *p = ziplistIndex((unsigned char*)ptrFromObj(o),0);\n        unsigned char *vstr;\n        unsigned int vlen;\n        long long vll;\n        while(p) {\n            ziplistGet(p,&vstr,&vlen,&vll);\n            robj *field = (vstr != NULL) ?\n                createStringObject((char*)vstr,vlen) :\n                createObject(OBJ_STRING,sdsfromlonglong(vll));\n            p = ziplistNext((unsigned char*)ptrFromObj(o),p);\n            ziplistGet(p,&vstr,&vlen,&vll);\n            robj *value = (vstr != NULL) ?\n                createStringObject((char*)vstr,vlen) :\n                createObject(OBJ_STRING,sdsfromlonglong(vll));\n            fn(key, field, value, privdata);\n            p = ziplistNext((unsigned char*)ptrFromObj(o),p);\n            decrRefCount(field);\n            decrRefCount(value);\n        }\n        cursor->cursor = 1;\n        cursor->done = 1;\n        ret = 0;\n    }\n    errno = 0;\n    return ret;\n}\n\n\n/* --------------------------------------------------------------------------\n * ## Module fork API\n * -------------------------------------------------------------------------- */\n\n/* Create a background child process with the current frozen snaphost of the\n * main process where you can do some processing in the background without\n * affecting / freezing the traffic and no need for threads and GIL locking.\n * Note that Redis allows for only one concurrent fork.\n * When the child wants to exit, it should call RedisModule_ExitFromChild.\n * If the parent wants to kill the child it should call RedisModule_KillForkChild\n * The done handler callback will be executed on the parent process when the\n * child existed (but not when killed)\n * Return: -1 on failure, on success the parent process will get a positive PID\n * of the child, and the child process will get 0.\n */\nint RM_Fork(RedisModuleForkDoneHandler cb, void *user_data) {\n    pid_t childpid;\n\n    if ((childpid = redisFork(CHILD_TYPE_MODULE)) == 0) {\n        /* Child */\n        redisSetProcTitle(\"redis-module-fork\");\n    } else if (childpid == -1) {\n        serverLog(LL_WARNING,\"Can't fork for module: %s\", strerror(errno));\n    } else {\n        /* Parent */\n        moduleForkInfo.done_handler = cb;\n        moduleForkInfo.done_handler_user_data = user_data;\n        updateDictResizePolicy();\n        serverLog(LL_VERBOSE, \"Module fork started pid: %ld \", (long) childpid);\n    }\n    return childpid;\n}\n\n/* The module is advised to call this function from the fork child once in a while,\n * so that it can report progress and COW memory to the parent which will be\n * reported in INFO.\n * The `progress` argument should between 0 and 1, or -1 when not available. */\nvoid RM_SendChildHeartbeat(double progress) {\n    sendChildInfoGeneric(CHILD_INFO_TYPE_CURRENT_INFO, 0, progress, \"Module fork\");\n}\n\n/* Call from the child process when you want to terminate it.\n * retcode will be provided to the done handler executed on the parent process.\n */\nint RM_ExitFromChild(int retcode) {\n    sendChildCowInfo(CHILD_INFO_TYPE_MODULE_COW_SIZE, \"Module fork\");\n    exitFromChild(retcode);\n    return REDISMODULE_OK;\n}\n\n/* Kill the active module forked child, if there is one active and the\n * pid matches, and returns C_OK. Otherwise if there is no active module\n * child or the pid does not match, return C_ERR without doing anything. */\nint TerminateModuleForkChild(int child_pid, int wait) {\n    /* Module child should be active and pid should match. */\n    if (g_pserver->child_type != CHILD_TYPE_MODULE ||\n        g_pserver->child_pid != child_pid) return C_ERR;\n\n    int statloc;\n    serverLog(LL_VERBOSE,\"Killing running module fork child: %ld\",\n        (long) g_pserver->child_pid);\n    if (kill(g_pserver->child_pid,SIGUSR1) != -1 && wait) {\n        while(waitpid(g_pserver->child_pid,&statloc,0) !=\n              g_pserver->child_pid);\n    }\n    /* Reset the buffer accumulating changes while the child saves. */\n    resetChildState();\n    moduleForkInfo.done_handler = NULL;\n    moduleForkInfo.done_handler_user_data = NULL;\n    return C_OK;\n}\n\n/* Can be used to kill the forked child process from the parent process.\n * child_pid would be the return value of RedisModule_Fork. */\nint RM_KillForkChild(int child_pid) {\n    /* Kill module child, wait for child exit. */\n    if (TerminateModuleForkChild(child_pid,1) == C_OK)\n        return REDISMODULE_OK;\n    else\n        return REDISMODULE_ERR;\n}\n\nvoid ModuleForkDoneHandler(int exitcode, int bysignal) {\n    serverLog(LL_NOTICE,\n        \"Module fork exited pid: %ld, retcode: %d, bysignal: %d\",\n        (long) g_pserver->child_pid, exitcode, bysignal);\n    if (moduleForkInfo.done_handler) {\n        moduleForkInfo.done_handler(exitcode, bysignal,\n            moduleForkInfo.done_handler_user_data);\n    }\n    moduleForkInfo.done_handler = NULL;\n    moduleForkInfo.done_handler_user_data = NULL;\n}\n\n/* --------------------------------------------------------------------------\n * ## Server hooks implementation\n * -------------------------------------------------------------------------- */\n\n/* Register to be notified, via a callback, when the specified server event\n * happens. The callback is called with the event as argument, and an additional\n * argument which is a void pointer and should be cased to a specific type\n * that is event-specific (but many events will just use NULL since they do not\n * have additional information to pass to the callback).\n *\n * If the callback is NULL and there was a previous subscription, the module\n * will be unsubscribed. If there was a previous subscription and the callback\n * is not null, the old callback will be replaced with the new one.\n *\n * The callback must be of this type:\n *\n *     int (*RedisModuleEventCallback)(RedisModuleCtx *ctx,\n *                                     RedisModuleEvent eid,\n *                                     uint64_t subevent,\n *                                     void *data);\n *\n * The 'ctx' is a normal Redis module context that the callback can use in\n * order to call other modules APIs. The 'eid' is the event itself, this\n * is only useful in the case the module subscribed to multiple events: using\n * the 'id' field of this structure it is possible to check if the event\n * is one of the events we registered with this callback. The 'subevent' field\n * depends on the event that fired.\n *\n * Finally the 'data' pointer may be populated, only for certain events, with\n * more relevant data.\n *\n * Here is a list of events you can use as 'eid' and related sub events:\n *\n * * RedisModuleEvent_ReplicationRoleChanged:\n *\n *     This event is called when the instance switches from master\n *     to replica or the other way around, however the event is\n *     also called when the replica remains a replica but starts to\n *     replicate with a different master.\n *\n *     The following sub events are available:\n *\n *     * `REDISMODULE_SUBEVENT_REPLROLECHANGED_NOW_MASTER`\n *     * `REDISMODULE_SUBEVENT_REPLROLECHANGED_NOW_REPLICA`\n *\n *     The 'data' field can be casted by the callback to a\n *     `RedisModuleReplicationInfo` structure with the following fields:\n *\n *         int master; // true if master, false if replica\n *         char *masterhost; // master instance hostname for NOW_REPLICA\n *         int masterport; // master instance port for NOW_REPLICA\n *         char *replid1; // Main replication ID\n *         char *replid2; // Secondary replication ID\n *         uint64_t repl1_offset; // Main replication offset\n *         uint64_t repl2_offset; // Offset of replid2 validity\n *\n * * RedisModuleEvent_Persistence\n *\n *     This event is called when RDB saving or AOF rewriting starts\n *     and ends. The following sub events are available:\n *\n *     * `REDISMODULE_SUBEVENT_PERSISTENCE_RDB_START`\n *     * `REDISMODULE_SUBEVENT_PERSISTENCE_AOF_START`\n *     * `REDISMODULE_SUBEVENT_PERSISTENCE_SYNC_RDB_START`\n *     * `REDISMODULE_SUBEVENT_PERSISTENCE_ENDED`\n *     * `REDISMODULE_SUBEVENT_PERSISTENCE_FAILED`\n *\n *     The above events are triggered not just when the user calls the\n *     relevant commands like BGSAVE, but also when a saving operation\n *     or AOF rewriting occurs because of internal server triggers.\n *     The SYNC_RDB_START sub events are happening in the forground due to\n *     SAVE command, FLUSHALL, or server shutdown, and the other RDB and\n *     AOF sub events are executed in a background fork child, so any\n *     action the module takes can only affect the generated AOF or RDB,\n *     but will not be reflected in the parent process and affect connected\n *     clients and commands. Also note that the AOF_START sub event may end\n *     up saving RDB content in case of an AOF with rdb-preamble.\n *\n * * RedisModuleEvent_FlushDB\n *\n *     The FLUSHALL, FLUSHDB or an internal flush (for instance\n *     because of replication, after the replica synchronization)\n *     happened. The following sub events are available:\n *\n *     * `REDISMODULE_SUBEVENT_FLUSHDB_START`\n *     * `REDISMODULE_SUBEVENT_FLUSHDB_END`\n *\n *     The data pointer can be casted to a RedisModuleFlushInfo\n *     structure with the following fields:\n *\n *         int32_t async;  // True if the flush is done in a thread.\n *                         // See for instance FLUSHALL ASYNC.\n *                         // In this case the END callback is invoked\n *                         // immediately after the database is put\n *                         // in the free list of the thread.\n *         int32_t dbnum;  // Flushed database number, -1 for all the DBs\n *                         // in the case of the FLUSHALL operation.\n *\n *     The start event is called *before* the operation is initated, thus\n *     allowing the callback to call DBSIZE or other operation on the\n *     yet-to-free keyspace.\n *\n * * RedisModuleEvent_Loading\n *\n *     Called on loading operations: at startup when the server is\n *     started, but also after a first synchronization when the\n *     replica is loading the RDB file from the master.\n *     The following sub events are available:\n *\n *     * `REDISMODULE_SUBEVENT_LOADING_RDB_START`\n *     * `REDISMODULE_SUBEVENT_LOADING_AOF_START`\n *     * `REDISMODULE_SUBEVENT_LOADING_REPL_START`\n *     * `REDISMODULE_SUBEVENT_LOADING_ENDED`\n *     * `REDISMODULE_SUBEVENT_LOADING_FAILED`\n *\n *     Note that AOF loading may start with an RDB data in case of\n *     rdb-preamble, in which case you'll only receive an AOF_START event.\n *\n * * RedisModuleEvent_ClientChange\n *\n *     Called when a client connects or disconnects.\n *     The data pointer can be casted to a RedisModuleClientInfo\n *     structure, documented in RedisModule_GetClientInfoById().\n *     The following sub events are available:\n *\n *     * `REDISMODULE_SUBEVENT_CLIENT_CHANGE_CONNECTED`\n *     * `REDISMODULE_SUBEVENT_CLIENT_CHANGE_DISCONNECTED`\n *\n * * RedisModuleEvent_Shutdown\n *\n *     The server is shutting down. No subevents are available.\n *\n * * RedisModuleEvent_ReplicaChange\n *\n *     This event is called when the instance (that can be both a\n *     master or a replica) get a new online replica, or lose a\n *     replica since it gets disconnected.\n *     The following sub events are available:\n *\n *     * `REDISMODULE_SUBEVENT_REPLICA_CHANGE_ONLINE`\n *     * `REDISMODULE_SUBEVENT_REPLICA_CHANGE_OFFLINE`\n *\n *     No additional information is available so far: future versions\n *     of Redis will have an API in order to enumerate the replicas\n *     connected and their state.\n *\n * * RedisModuleEvent_CronLoop\n *\n *     This event is called every time Redis calls the serverCron()\n *     function in order to do certain bookkeeping. Modules that are\n *     required to do operations from time to time may use this callback.\n *     Normally Redis calls this function 10 times per second, but\n *     this changes depending on the \"hz\" configuration.\n *     No sub events are available.\n *\n *     The data pointer can be casted to a RedisModuleCronLoop\n *     structure with the following fields:\n *\n *         int32_t hz;  // Approximate number of events per second.\n *\n * * RedisModuleEvent_MasterLinkChange\n *\n *     This is called for replicas in order to notify when the\n *     replication link becomes functional (up) with our master,\n *     or when it goes down. Note that the link is not considered\n *     up when we just connected to the master, but only if the\n *     replication is happening correctly.\n *     The following sub events are available:\n *\n *     * `REDISMODULE_SUBEVENT_MASTER_LINK_UP`\n *     * `REDISMODULE_SUBEVENT_MASTER_LINK_DOWN`\n *\n * * RedisModuleEvent_ModuleChange\n *\n *     This event is called when a new module is loaded or one is unloaded.\n *     The following sub events are available:\n *\n *     * `REDISMODULE_SUBEVENT_MODULE_LOADED`\n *     * `REDISMODULE_SUBEVENT_MODULE_UNLOADED`\n *\n *     The data pointer can be casted to a RedisModuleModuleChange\n *     structure with the following fields:\n *\n *         const char* module_name;  // Name of module loaded or unloaded.\n *         int32_t module_version;  // Module version.\n *\n * * RedisModuleEvent_LoadingProgress\n *\n *     This event is called repeatedly called while an RDB or AOF file\n *     is being loaded.\n *     The following sub events are availble:\n *\n *     * `REDISMODULE_SUBEVENT_LOADING_PROGRESS_RDB`\n *     * `REDISMODULE_SUBEVENT_LOADING_PROGRESS_AOF`\n *\n *     The data pointer can be casted to a RedisModuleLoadingProgress\n *     structure with the following fields:\n *\n *         int32_t hz;  // Approximate number of events per second.\n *         int32_t progress;  // Approximate progress between 0 and 1024,\n *                            // or -1 if unknown.\n *\n * * RedisModuleEvent_SwapDB\n *\n *     This event is called when a SWAPDB command has been successfully\n *     Executed.\n *     For this event call currently there is no subevents available.\n *\n *     The data pointer can be casted to a RedisModuleSwapDbInfo\n *     structure with the following fields:\n *\n *         int32_t dbnum_first;    // Swap Db first dbnum\n *         int32_t dbnum_second;   // Swap Db second dbnum\n *\n * * RedisModuleEvent_ReplBackup\n *\n *     Called when diskless-repl-load config is set to swapdb,\n *     And redis needs to backup the the current database for the\n *     possibility to be restored later. A module with global data and\n *     maybe with aux_load and aux_save callbacks may need to use this\n *     notification to backup / restore / discard its globals.\n *     The following sub events are available:\n *\n *     * `REDISMODULE_SUBEVENT_REPL_BACKUP_CREATE`\n *     * `REDISMODULE_SUBEVENT_REPL_BACKUP_RESTORE`\n *     * `REDISMODULE_SUBEVENT_REPL_BACKUP_DISCARD`\n *\n * * RedisModuleEvent_ForkChild\n *\n *     Called when a fork child (AOFRW, RDBSAVE, module fork...) is born/dies\n *     The following sub events are available:\n *\n *     * `REDISMODULE_SUBEVENT_FORK_CHILD_BORN`\n *     * `REDISMODULE_SUBEVENT_FORK_CHILD_DIED`\n *\n * The function returns REDISMODULE_OK if the module was successfully subscribed\n * for the specified event. If the API is called from a wrong context or unsupported event\n * is given then REDISMODULE_ERR is returned. */\nint RM_SubscribeToServerEvent(RedisModuleCtx *ctx, RedisModuleEvent event, RedisModuleEventCallback callback) {\n    RedisModuleEventListener *el;\n\n    /* Protect in case of calls from contexts without a module reference. */\n    if (ctx->module == NULL) return REDISMODULE_ERR;\n    if (event.id >= _REDISMODULE_EVENT_NEXT) return REDISMODULE_ERR;\n\n    /* Search an event matching this module and event ID. */\n    listIter li;\n    listNode *ln;\n    listRewind(RedisModule_EventListeners,&li);\n    while((ln = listNext(&li))) {\n        el = (RedisModuleEventListener*)ln->value;\n        if (el->module == ctx->module && el->event.id == event.id)\n            break; /* Matching event found. */\n    }\n\n    /* Modify or remove the event listener if we already had one. */\n    if (ln) {\n        if (callback == NULL) {\n            listDelNode(RedisModule_EventListeners,ln);\n            zfree(el);\n        } else {\n            el->callback = callback; /* Update the callback with the new one. */\n        }\n        return REDISMODULE_OK;\n    }\n\n    /* No event found, we need to add a new one. */\n    el = (RedisModuleEventListener*)zmalloc(sizeof(*el));\n    el->module = ctx->module;\n    el->event = event;\n    el->callback = callback;\n    listAddNodeTail(RedisModule_EventListeners,el);\n    return REDISMODULE_OK;\n}\n\n/**\n * For a given server event and subevent, return zero if the\n * subevent is not supported and non-zero otherwise.\n */\nint RM_IsSubEventSupported(RedisModuleEvent event, int64_t subevent) {\n    switch (event.id) {\n    case REDISMODULE_EVENT_REPLICATION_ROLE_CHANGED:\n        return subevent < _REDISMODULE_EVENT_REPLROLECHANGED_NEXT;\n    case REDISMODULE_EVENT_PERSISTENCE:\n        return subevent < _REDISMODULE_SUBEVENT_PERSISTENCE_NEXT;\n    case REDISMODULE_EVENT_FLUSHDB:\n        return subevent < _REDISMODULE_SUBEVENT_FLUSHDB_NEXT;\n    case REDISMODULE_EVENT_LOADING:\n        return subevent < _REDISMODULE_SUBEVENT_LOADING_NEXT;\n    case REDISMODULE_EVENT_CLIENT_CHANGE:\n        return subevent < _REDISMODULE_SUBEVENT_CLIENT_CHANGE_NEXT;\n    case REDISMODULE_EVENT_SHUTDOWN:\n        return subevent < _REDISMODULE_SUBEVENT_SHUTDOWN_NEXT;\n    case REDISMODULE_EVENT_REPLICA_CHANGE:\n        return subevent < _REDISMODULE_EVENT_REPLROLECHANGED_NEXT;\n    case REDISMODULE_EVENT_MASTER_LINK_CHANGE:\n        return subevent < _REDISMODULE_SUBEVENT_MASTER_NEXT;\n    case REDISMODULE_EVENT_CRON_LOOP:\n        return subevent < _REDISMODULE_SUBEVENT_CRON_LOOP_NEXT;\n    case REDISMODULE_EVENT_MODULE_CHANGE:\n        return subevent < _REDISMODULE_SUBEVENT_MODULE_NEXT;\n    case REDISMODULE_EVENT_LOADING_PROGRESS:\n        return subevent < _REDISMODULE_SUBEVENT_LOADING_PROGRESS_NEXT;\n    case REDISMODULE_EVENT_SWAPDB:\n        return subevent < _REDISMODULE_SUBEVENT_SWAPDB_NEXT;\n    case REDISMODULE_EVENT_REPL_BACKUP:\n        return subevent < _REDISMODULE_SUBEVENT_REPL_BACKUP_NEXT;\n    case REDISMODULE_EVENT_FORK_CHILD:\n        return subevent < _REDISMODULE_SUBEVENT_FORK_CHILD_NEXT;\n    default:\n        break;\n    }\n    return 0;\n}\n\n/* This is called by the Redis internals every time we want to fire an\n * event that can be interceppted by some module. The pointer 'data' is useful\n * in order to populate the event-specific structure when needed, in order\n * to return the structure with more information to the callback.\n *\n * 'eid' and 'subid' are just the main event ID and the sub event associated\n * with the event, depending on what exactly happened. */\nvoid moduleFireServerEvent(uint64_t eid, int subid, void *data) {\n    /* Fast path to return ASAP if there is nothing to do, avoiding to\n     * setup the iterator and so forth: we want this call to be extremely\n     * cheap if there are no registered modules. */\n    if (listLength(RedisModule_EventListeners) == 0) return;\n\n    aeAcquireLock();\n\n    int real_client_used = 0;\n    listIter li;\n    listNode *ln;\n    listRewind(RedisModule_EventListeners,&li);\n    while((ln = listNext(&li))) {\n        RedisModuleEventListener *el = (RedisModuleEventListener*)ln->value;\n        if (el->event.id == eid) {\n            RedisModuleCtx ctx = REDISMODULE_CTX_INIT;\n            ctx.module = el->module;\n\n            if (eid == REDISMODULE_EVENT_CLIENT_CHANGE) {\n                /* In the case of client changes, we're pushing the real client\n                 * so the event handler can mutate it if needed. For example,\n                 * to change its authentication state in a way that does not\n                 * depend on specific commands executed later.\n                 */\n                ctx.client = (client *) data;\n                real_client_used = 1;\n            } else if (ModulesInHooks == 0) {\n                ctx.client = moduleFreeContextReusedClient;\n            } else {\n                ctx.client = createClient(NULL, IDX_EVENT_LOOP_MAIN);\n                ctx.client->flags |= CLIENT_MODULE;\n                ctx.client->user = NULL; /* Root user. */\n            }\n\n            void *moduledata = NULL;\n            RedisModuleClientInfoV1 civ1;\n            RedisModuleReplicationInfoV1 riv1;\n            RedisModuleModuleChangeV1 mcv1;\n            /* Start at DB zero by default when calling the handler. It's\n             * up to the specific event setup to change it when it makes\n             * sense. For instance for FLUSHDB events we select the correct\n             * DB automatically. */\n            selectDb(ctx.client, 0);\n\n            /* Event specific context and data pointer setup. */\n            if (eid == REDISMODULE_EVENT_CLIENT_CHANGE) {\n                modulePopulateClientInfoStructure(&civ1,(client*)data,\n                                                  el->event.dataver);\n                moduledata = &civ1;\n            } else if (eid == REDISMODULE_EVENT_REPLICATION_ROLE_CHANGED) {\n                modulePopulateReplicationInfoStructure(&riv1,el->event.dataver);\n                moduledata = &riv1;\n            } else if (eid == REDISMODULE_EVENT_FLUSHDB) {\n                moduledata = data;\n                RedisModuleFlushInfoV1 *fi = (RedisModuleFlushInfoV1*)data;\n                if (fi->dbnum != -1)\n                    selectDb(ctx.client, fi->dbnum);\n            } else if (eid == REDISMODULE_EVENT_MODULE_CHANGE) {\n                RedisModule *m = (RedisModule*)data;\n                if (m == el->module)\n                    continue;\n                mcv1.version = REDISMODULE_MODULE_CHANGE_VERSION;\n                mcv1.module_name = m->name;\n                mcv1.module_version = m->ver;\n                moduledata = &mcv1;\n            } else if (eid == REDISMODULE_EVENT_LOADING_PROGRESS) {\n                moduledata = data;\n            } else if (eid == REDISMODULE_EVENT_CRON_LOOP) {\n                moduledata = data;\n            } else if (eid == REDISMODULE_EVENT_SWAPDB) {\n                moduledata = data;\n            }\n\n            ModulesInHooks++;\n            el->module->in_hook++;\n            el->callback(&ctx,el->event,subid,moduledata);\n            el->module->in_hook--;\n            ModulesInHooks--;\n\n            if (ModulesInHooks != 0 && !real_client_used) freeClient(ctx.client);\n            moduleFreeContext(&ctx);\n        }\n    }\n\n    aeReleaseLock();\n}\n\n/* Remove all the listeners for this module: this is used before unloading\n * a module. */\nvoid moduleUnsubscribeAllServerEvents(RedisModule *module) {\n    RedisModuleEventListener *el;\n    listIter li;\n    listNode *ln;\n    listRewind(RedisModule_EventListeners,&li);\n\n    while((ln = listNext(&li))) {\n        el = (RedisModuleEventListener*)ln->value;\n        if (el->module == module) {\n            listDelNode(RedisModule_EventListeners,ln);\n            zfree(el);\n        }\n    }\n}\n\nvoid processModuleLoadingProgressEvent(int is_aof) {\n    long long now = g_pserver->ustime;\n    static long long next_event = 0;\n    if (now >= next_event) {\n        /* Fire the loading progress modules end event. */\n        int progress = -1;\n        if (g_pserver->loading_total_bytes)\n            progress = (g_pserver->loading_loaded_bytes<<10) / g_pserver->loading_total_bytes;\n        RedisModuleLoadingProgressV1 fi = {REDISMODULE_LOADING_PROGRESS_VERSION,\n                                     g_pserver->hz,\n                                     progress};\n        moduleFireServerEvent(REDISMODULE_EVENT_LOADING_PROGRESS,\n                              is_aof?\n                                REDISMODULE_SUBEVENT_LOADING_PROGRESS_AOF:\n                                REDISMODULE_SUBEVENT_LOADING_PROGRESS_RDB,\n                              &fi);\n        /* decide when the next event should fire. */\n        next_event = now + 1000000 / g_pserver->hz;\n    }\n}\n\n/* When a module key is deleted (in dbAsyncDelete/dbSyncDelete/dbOverwrite), it \n*  will be called to tell the module which key is about to be released. */\nvoid moduleNotifyKeyUnlink(robj *key, robj *val) {\n    if (val->type == OBJ_MODULE) {\n        moduleValue *mv = (moduleValue*)ptrFromObj(val);\n        moduleType *mt = mv->type;\n        if (mt->unlink != NULL) {\n            mt->unlink(key,mv->value);\n        } \n    }\n}\n\n/* --------------------------------------------------------------------------\n * Modules API internals\n * -------------------------------------------------------------------------- */\n\n/* g_pserver->moduleapi dictionary type. Only uses plain C strings since\n * this gets queries from modules. */\n\nuint64_t dictCStringKeyHash(const void *key) {\n    return dictGenHashFunction((unsigned char*)key, strlen((char*)key));\n}\n\nint dictCStringKeyCompare(void *privdata, const void *key1, const void *key2) {\n    UNUSED(privdata);\n    return strcmp((char*)key1,(char*)key2) == 0;\n}\n\ndictType moduleAPIDictType = {\n    dictCStringKeyHash,        /* hash function */\n    NULL,                      /* key dup */\n    NULL,                      /* val dup */\n    dictCStringKeyCompare,     /* key compare */\n    NULL,                      /* key destructor */\n    NULL,                      /* val destructor */\n    NULL                       /* allow to expand */\n};\n\nextern \"C\" int moduleRegisterApi(const char *funcname, void *funcptr) {\n    return dictAdd(g_pserver->moduleapi, (char*)funcname, funcptr);\n}\n\n#define REGISTER_API(name) \\\n    moduleRegisterApi(\"RedisModule_\" #name, (void *)(unsigned long)RM_ ## name)\n\n/* Global initialization at Redis startup. */\nvoid moduleRegisterCoreAPI(void);\n\n/* Some steps in module initialization need to be done last after server\n * initialization.\n * For example, selectDb() in createClient() requires that g_pserver->db has\n * been initialized, see #7323. */\nvoid moduleInitModulesSystemLast(void) {\n    moduleFreeContextReusedClient = createClient(NULL, IDX_EVENT_LOOP_MAIN);\n    moduleFreeContextReusedClient->flags |= CLIENT_MODULE;\n    moduleFreeContextReusedClient->user = NULL; /* root user. */\n}\n\nvoid moduleInitModulesSystem(void) {\n    moduleUnblockedClients = listCreate();\n    g_pserver->loadmodule_queue = listCreate();\n    modules = dictCreate(&modulesDictType,NULL);\n\n    /* Set up the keyspace notification subscriber list and static client */\n    moduleKeyspaceSubscribers = listCreate();\n\n    /* Set up filter list */\n    moduleCommandFilters = listCreate();\n\n    /* Reusable client for RM_Call() is created on first use */\n    g_pserver->module_client = NULL;\n\n    moduleRegisterCoreAPI();\n    if (pipe(g_pserver->module_blocked_pipe) == -1) {\n        serverLog(LL_WARNING,\n            \"Can't create the pipe for module blocking commands: %s\",\n            strerror(errno));\n        exit(1);\n    }\n\n    /* Make the pipe non blocking. This is just a best effort aware mechanism\n     * and we do not want to block not in the read nor in the write half. */\n    anetNonBlock(NULL,g_pserver->module_blocked_pipe[0]);\n    anetNonBlock(NULL,g_pserver->module_blocked_pipe[1]);\n\n    /* Enable close-on-exec flag on pipes in case of the fork-exec system calls in\n     * sentinels or redis servers. */\n    anetCloexec(g_pserver->module_blocked_pipe[0]);\n    anetCloexec(g_pserver->module_blocked_pipe[1]);\n\n    /* Create the timers radix tree. */\n    Timers = raxNew();\n\n    /* Setup the event listeners data structures. */\n    RedisModule_EventListeners = listCreate();\n\n    /* Our thread-safe contexts GIL must start with already locked:\n     * it is just unlocked when it's safe. */\n    moduleAcquireGIL(true);\n}\n\n/* Load all the modules in the g_pserver->loadmodule_queue list, which is\n * populated by `loadmodule` directives in the configuration file.\n * We can't load modules directly when processing the configuration file\n * because the server must be fully initialized before loading modules.\n *\n * The function aborts the server on errors, since to start with missing\n * modules is not considered sane: clients may rely on the existence of\n * given commands, loading AOF also may need some modules to exist, and\n * if this instance is a replica, it must understand commands from master. */\nvoid moduleLoadFromQueue(void) {\n    listIter li;\n    listNode *ln;\n\n    listRewind(g_pserver->loadmodule_queue,&li);\n    while((ln = listNext(&li))) {\n        struct moduleLoadQueueEntry *loadmod = (moduleLoadQueueEntry*)ln->value;\n        if (moduleLoad(loadmod->path,(void **)loadmod->argv,loadmod->argc)\n            == C_ERR)\n        {\n            serverLog(LL_WARNING,\n                \"Can't load module from %s: server aborting\",\n                loadmod->path);\n            exit(1);\n        }\n    }\n}\n\nvoid moduleFreeModuleStructure(struct RedisModule *module) {\n    listRelease(module->types);\n    listRelease(module->filters);\n    listRelease(module->usedby);\n    listRelease(module->usingMods);\n    sdsfree(module->name);\n    zfree(module);\n}\n\nvoid moduleUnregisterCommands(struct RedisModule *module) {\n    /* Unregister all the commands registered by this module. */\n    dictIterator *di = dictGetSafeIterator(g_pserver->commands);\n    dictEntry *de;\n    while ((de = dictNext(di)) != NULL) {\n        struct redisCommand *cmd = (redisCommand*)dictGetVal(de);\n        if (cmd->proc == RedisModuleCommandDispatcher) {\n            RedisModuleCommandProxy *cp =\n                (RedisModuleCommandProxy*)(unsigned long)cmd->getkeys_proc;\n            sds cmdname = (sds)cp->rediscmd->name;\n            if (cp->module == module) {\n                dictDelete(g_pserver->commands,cmdname);\n                dictDelete(g_pserver->orig_commands,cmdname);\n                sdsfree(cmdname);\n                zfree(cp->rediscmd);\n                zfree(cp);\n            }\n        }\n    }\n    dictReleaseIterator(di);\n}\n\n/* Load a module and initialize it. On success C_OK is returned, otherwise\n * C_ERR is returned. */\nint moduleLoad(const char *path, void **module_argv, int module_argc) {\n    int (*onload)(void *, void **, int);\n    void *handle;\n    RedisModuleCtx ctx = REDISMODULE_CTX_INIT;\n    ctx.client = moduleFreeContextReusedClient;\n    selectDb(ctx.client, 0);\n\n    struct stat st;\n    if (stat(path, &st) == 0)\n    {   // this check is best effort\n        if (!(st.st_mode & (S_IXUSR  | S_IXGRP | S_IXOTH))) {\n            serverLog(LL_WARNING, \"Module %s failed to load: It does not have execute permissions.\", path);\n            return C_ERR;\n        }\n    }\n\n    handle = dlopen(path,RTLD_NOW|RTLD_LOCAL);\n    if (handle == NULL) {\n        serverLog(LL_WARNING, \"Module %s failed to load: %s\", path, dlerror());\n        return C_ERR;\n    }\n    onload = (int (*)(void *, void **, int))(unsigned long) dlsym(handle,\"RedisModule_OnLoad\");\n    if (onload == NULL) {\n        dlclose(handle);\n        serverLog(LL_WARNING,\n            \"Module %s does not export RedisModule_OnLoad() \"\n            \"symbol. Module not loaded.\",path);\n        return C_ERR;\n    }\n    if (onload((void*)&ctx,module_argv,module_argc) == REDISMODULE_ERR) {\n        if (ctx.module) {\n            moduleUnregisterCommands(ctx.module);\n            moduleUnregisterSharedAPI(ctx.module);\n            moduleUnregisterUsedAPI(ctx.module);\n            moduleFreeModuleStructure(ctx.module);\n        }\n        dlclose(handle);\n        serverLog(LL_WARNING,\n            \"Module %s initialization failed. Module not loaded\",path);\n        return C_ERR;\n    }\n\n    /* Redis module loaded! Register it. */\n    dictAdd(modules,ctx.module->name,ctx.module);\n    ctx.module->blocked_clients = 0;\n    ctx.module->handle = handle;\n    serverLog(LL_NOTICE,\"Module '%s' loaded from %s\",ctx.module->name,path);\n    /* Fire the loaded modules event. */\n    moduleFireServerEvent(REDISMODULE_EVENT_MODULE_CHANGE,\n                          REDISMODULE_SUBEVENT_MODULE_LOADED,\n                          ctx.module);\n\n    moduleFreeContext(&ctx);\n    return C_OK;\n}\n\n\n/* Unload the module registered with the specified name. On success\n * C_OK is returned, otherwise C_ERR is returned and errno is set\n * to the following values depending on the type of error:\n *\n * * ENONET: No such module having the specified name.\n * * EBUSY: The module exports a new data type and can only be reloaded. */\nint moduleUnload(sds name) {\n    struct RedisModule *module = (RedisModule*)dictFetchValue(modules,name);\n\n    if (module == NULL) {\n        errno = ENOENT;\n        return REDISMODULE_ERR;\n    } else if (listLength(module->types)) {\n        errno = EBUSY;\n        return REDISMODULE_ERR;\n    } else if (listLength(module->usedby)) {\n        errno = EPERM;\n        return REDISMODULE_ERR;\n    } else if (module->blocked_clients) {\n        errno = EAGAIN;\n        return REDISMODULE_ERR;\n    }\n\n    /* Give module a chance to clean up. */\n    int (*onunload)(void *);\n    onunload = (int (*)(void *))(unsigned long) dlsym(module->handle, \"RedisModule_OnUnload\");\n    if (onunload) {\n        RedisModuleCtx ctx = REDISMODULE_CTX_INIT;\n        ctx.module = module;\n        ctx.client = moduleFreeContextReusedClient;\n        int unload_status = onunload((void*)&ctx);\n        moduleFreeContext(&ctx);\n\n        if (unload_status == REDISMODULE_ERR) {\n            serverLog(LL_WARNING, \"Module %s OnUnload failed.  Unload canceled.\", name);\n            errno = ECANCELED;\n            return REDISMODULE_ERR;\n        }\n    }\n\n    moduleFreeAuthenticatedClients(module);\n    moduleUnregisterCommands(module);\n    moduleUnregisterSharedAPI(module);\n    moduleUnregisterUsedAPI(module);\n    moduleUnregisterFilters(module);\n\n    /* Remove any notification subscribers this module might have */\n    moduleUnsubscribeNotifications(module);\n    moduleUnsubscribeAllServerEvents(module);\n\n    /* Unload the dynamic library. */\n    if (dlclose(module->handle) == -1) {\n        const char *error = dlerror();\n        if (error == NULL) error = \"Unknown error\";\n        serverLog(LL_WARNING,\"Error when trying to close the %s module: %s\",\n            module->name, error);\n    }\n\n    /* Fire the unloaded modules event. */\n    moduleFireServerEvent(REDISMODULE_EVENT_MODULE_CHANGE,\n                          REDISMODULE_SUBEVENT_MODULE_UNLOADED,\n                          module);\n\n    /* Remove from list of modules. */\n    serverLog(LL_NOTICE,\"Module %s unloaded\",module->name);\n    dictDelete(modules,module->name);\n    module->name = NULL; /* The name was already freed by dictDelete(). */\n    moduleFreeModuleStructure(module);\n\n    return REDISMODULE_OK;\n}\n\n/* Helper function for the MODULE and HELLO command: send the list of the\n * loaded modules to the client. */\nvoid addReplyLoadedModules(client *c) {\n    dictIterator *di = dictGetIterator(modules);\n    dictEntry *de;\n\n    addReplyArrayLen(c,dictSize(modules));\n    while ((de = dictNext(di)) != NULL) {\n        sds name = (sds)dictGetKey(de);\n        struct RedisModule *module = (RedisModule*)dictGetVal(de);\n        addReplyMapLen(c,2);\n        addReplyBulkCString(c,\"name\");\n        addReplyBulkCBuffer(c,name,sdslen(name));\n        addReplyBulkCString(c,\"ver\");\n        addReplyLongLong(c,module->ver);\n    }\n    dictReleaseIterator(di);\n}\n\n/* Helper for genModulesInfoString(): given a list of modules, return\n * am SDS string in the form \"[modulename|modulename2|...]\" */\nsds genModulesInfoStringRenderModulesList(list *l) {\n    listIter li;\n    listNode *ln;\n    listRewind(l,&li);\n    sds output = sdsnew(\"[\");\n    while((ln = listNext(&li))) {\n        RedisModule *module = (RedisModule*)ln->value;\n        output = sdscat(output,module->name);\n        if (ln != listLast(l))\n            output = sdscat(output,\"|\");\n    }\n    output = sdscat(output,\"]\");\n    return output;\n}\n\n/* Helper for genModulesInfoString(): render module options as an SDS string. */\nsds genModulesInfoStringRenderModuleOptions(struct RedisModule *module) {\n    sds output = sdsnew(\"[\");\n    if (module->options & REDISMODULE_OPTIONS_HANDLE_IO_ERRORS)\n        output = sdscat(output,\"handle-io-errors|\");\n    output = sdstrim(output,\"|\");\n    output = sdscat(output,\"]\");\n    return output;\n}\n\n\n/* Helper function for the INFO command: adds loaded modules as to info's\n * output.\n *\n * After the call, the passed sds info string is no longer valid and all the\n * references must be substituted with the new pointer returned by the call. */\nsds genModulesInfoString(sds info) {\n    dictIterator *di = dictGetIterator(modules);\n    dictEntry *de;\n\n    while ((de = dictNext(di)) != NULL) {\n        sds name = (sds)dictGetKey(de);\n        struct RedisModule *module = (RedisModule*)dictGetVal(de);\n\n        sds usedby = genModulesInfoStringRenderModulesList(module->usedby);\n        sds usingMods = genModulesInfoStringRenderModulesList(module->usingMods);\n        sds options = genModulesInfoStringRenderModuleOptions(module);\n        info = sdscatfmt(info,\n            \"module:name=%S,ver=%i,api=%i,filters=%i,\"\n            \"usedby=%S,using=%S,options=%S\\r\\n\",\n                name, module->ver, module->apiver,\n                (int)listLength(module->filters), usedby, usingMods, options);\n        sdsfree(usedby);\n        sdsfree(usingMods);\n        sdsfree(options);\n    }\n    dictReleaseIterator(di);\n    return info;\n}\n\n/* Redis MODULE command.\n *\n * MODULE LIST\n * MODULE LOAD <path> [args...]\n * MODULE UNLOAD <name>\n */\nvoid moduleCommand(client *c) {\n    char *subcmd = szFromObj(c->argv[1]);\n\n    if (c->argc == 2 && !strcasecmp(subcmd,\"help\")) {\n        const char *help[] = {\n\"LIST\",\n\"    Return a list of loaded modules.\",\n\"LOAD <path> [<arg> ...]\",\n\"    Load a module library from <path>, passing to it any optional arguments.\",\n\"UNLOAD <name>\",\n\"    Unload a module.\",\nNULL\n        };\n        addReplyHelp(c, help);\n    } else\n    if (!strcasecmp(subcmd,\"load\") && c->argc >= 3) {\n        robj **argv = NULL;\n        int argc = 0;\n\n        if (c->argc > 3) {\n            argc = c->argc - 3;\n            argv = &c->argv[3];\n        }\n\n        if (moduleLoad(szFromObj(c->argv[2]),(void **)argv,argc) == C_OK)\n            addReply(c,shared.ok);\n        else\n            addReplyError(c,\n                \"Error loading the extension. Please check the server logs.\");\n    } else if (!strcasecmp(subcmd,\"unload\") && c->argc == 3) {\n        if (moduleUnload(szFromObj(c->argv[2])) == C_OK)\n            addReply(c,shared.ok);\n        else {\n            const char *errmsg;\n            switch(errno) {\n            case ENOENT:\n                errmsg = \"no such module with that name\";\n                break;\n            case EBUSY:\n                errmsg = \"the module exports one or more module-side data \"\n                         \"types, can't unload\";\n                break;\n            case EPERM:\n                errmsg = \"the module exports APIs used by other modules. \"\n                         \"Please unload them first and try again\";\n                break;\n            case EAGAIN:\n                errmsg = \"the module has blocked clients. \"\n                         \"Please wait them unblocked and try again\";\n                break;\n            default:\n                errmsg = \"operation not possible.\";\n                break;\n            }\n            addReplyErrorFormat(c,\"Error unloading module: %s\",errmsg);\n        }\n    } else if (!strcasecmp(subcmd,\"list\") && c->argc == 2) {\n        addReplyLoadedModules(c);\n    } else {\n        addReplySubcommandSyntaxError(c);\n        return;\n    }\n}\n\n/* Return the number of registered modules. */\nsize_t moduleCount(void) {\n    return dictSize(modules);\n}\n\n/* --------------------------------------------------------------------------\n * ## Key eviction API\n * -------------------------------------------------------------------------- */\n\n/* Set the key last access time for LRU based eviction. not relevant if the\n * servers's maxmemory policy is LFU based. Value is idle time in milliseconds.\n * returns REDISMODULE_OK if the LRU was updated, REDISMODULE_ERR otherwise. */\nint RM_SetLRU(RedisModuleKey *key, mstime_t lru_idle) {\n    if (!key->value)\n        return REDISMODULE_ERR;\n    if (objectSetLRUOrLFU(key->value, -1, lru_idle, lru_idle>=0 ? LRU_CLOCK() : 0, 1))\n        return REDISMODULE_OK;\n    return REDISMODULE_ERR;\n}\n\n/* Gets the key last access time.\n * Value is idletime in milliseconds or -1 if the server's eviction policy is\n * LFU based.\n * returns REDISMODULE_OK if when key is valid. */\nint RM_GetLRU(RedisModuleKey *key, mstime_t *lru_idle) {\n    *lru_idle = -1;\n    if (!key->value)\n        return REDISMODULE_ERR;\n    if (g_pserver->maxmemory_policy & MAXMEMORY_FLAG_LFU)\n        return REDISMODULE_OK;\n    *lru_idle = estimateObjectIdleTime(key->value);\n    return REDISMODULE_OK;\n}\n\n/* Set the key access frequency. only relevant if the server's maxmemory policy\n * is LFU based.\n * The frequency is a logarithmic counter that provides an indication of\n * the access frequencyonly (must be <= 255).\n * returns REDISMODULE_OK if the LFU was updated, REDISMODULE_ERR otherwise. */\nint RM_SetLFU(RedisModuleKey *key, long long lfu_freq) {\n    if (!key->value)\n        return REDISMODULE_ERR;\n    if (objectSetLRUOrLFU(key->value, lfu_freq, -1, 0, 1))\n        return REDISMODULE_OK;\n    return REDISMODULE_ERR;\n}\n\n/* Gets the key access frequency or -1 if the server's eviction policy is not\n * LFU based.\n * returns REDISMODULE_OK if when key is valid. */\nint RM_GetLFU(RedisModuleKey *key, long long *lfu_freq) {\n    *lfu_freq = -1;\n    if (!key->value)\n        return REDISMODULE_ERR;\n    if (g_pserver->maxmemory_policy & MAXMEMORY_FLAG_LFU)\n    {\n        *lfu_freq = LFUDecrAndReturn(key->value);\n    }\n    return REDISMODULE_OK;\n}\n\n/* --------------------------------------------------------------------------\n * ## Miscellaneous APIs\n * -------------------------------------------------------------------------- */\n\n/**\n * Returns the full ContextFlags mask, using the return value\n * the module can check if a certain set of flags are supported\n * by the redis server version in use.\n * Example:\n *\n *        int supportedFlags = RM_GetContextFlagsAll();\n *        if (supportedFlags & REDISMODULE_CTX_FLAGS_MULTI) {\n *              // REDISMODULE_CTX_FLAGS_MULTI is supported\n *        } else{\n *              // REDISMODULE_CTX_FLAGS_MULTI is not supported\n *        }\n */\nint RM_GetContextFlagsAll() {\n    return _REDISMODULE_CTX_FLAGS_NEXT - 1;\n}\n\n/**\n * Returns the full KeyspaceNotification mask, using the return value\n * the module can check if a certain set of flags are supported\n * by the redis server version in use.\n * Example:\n *\n *        int supportedFlags = RM_GetKeyspaceNotificationFlagsAll();\n *        if (supportedFlags & REDISMODULE_NOTIFY_LOADED) {\n *              // REDISMODULE_NOTIFY_LOADED is supported\n *        } else{\n *              // REDISMODULE_NOTIFY_LOADED is not supported\n *        }\n */\nint RM_GetKeyspaceNotificationFlagsAll() {\n    return _REDISMODULE_NOTIFY_NEXT - 1;\n}\n\n/**\n * Return the redis version in format of 0x00MMmmpp.\n * Example for 6.0.7 the return value will be 0x00060007.\n */\nint RM_GetServerVersion() {\n    return KEYDB_VERSION_NUM;\n}\n\n/**\n * Return the current redis-server runtime value of REDISMODULE_TYPE_METHOD_VERSION.\n * You can use that when calling RM_CreateDataType to know which fields of\n * RedisModuleTypeMethods are gonna be supported and which will be ignored.\n */\nint RM_GetTypeMethodVersion() {\n    return REDISMODULE_TYPE_METHOD_VERSION;\n}\n\n/* Replace the value assigned to a module type.\n *\n * The key must be open for writing, have an existing value, and have a moduleType\n * that matches the one specified by the caller.\n *\n * Unlike RM_ModuleTypeSetValue() which will free the old value, this function\n * simply swaps the old value with the new value.\n *\n * The function returns REDISMODULE_OK on success, REDISMODULE_ERR on errors\n * such as:\n *\n * 1. Key is not opened for writing.\n * 2. Key is not a module data type key.\n * 3. Key is a module datatype other than 'mt'.\n *\n * If old_value is non-NULL, the old value is returned by reference.\n */\nint RM_ModuleTypeReplaceValue(RedisModuleKey *key, moduleType *mt, void *new_value, void **old_value) {\n    if (!(key->mode & REDISMODULE_WRITE) || key->iter)\n        return REDISMODULE_ERR;\n    if (!key->value || key->value->type != OBJ_MODULE)\n        return REDISMODULE_ERR;\n\n    moduleValue *mv = (moduleValue*)ptrFromObj(key->value);\n    if (mv->type != mt)\n        return REDISMODULE_ERR;\n\n    if (old_value)\n        *old_value = mv->value;\n    mv->value = new_value;\n\n    return REDISMODULE_OK;\n}\n\n/* For a specified command, parse its arguments and return an array that\n * contains the indexes of all key name arguments. This function is\n * essnetially a more efficient way to do COMMAND GETKEYS.\n *\n * A NULL return value indicates the specified command has no keys, or\n * an error condition. Error conditions are indicated by setting errno\n * as folllows:\n *\n * * ENOENT: Specified command does not exist.\n * * EINVAL: Invalid command arity specified.\n *\n * NOTE: The returned array is not a Redis Module object so it does not\n * get automatically freed even when auto-memory is used. The caller\n * must explicitly call RM_Free() to free it.\n */\nint *RM_GetCommandKeys(RedisModuleCtx *ctx, RedisModuleString **argv, int argc, int *num_keys) {\n    UNUSED(ctx);\n    struct redisCommand *cmd;\n    int *res = NULL;\n\n    /* Find command */\n    if ((cmd = lookupCommand(szFromObj(argv[0]))) == NULL) {\n        errno = ENOENT;\n        return NULL;\n    }\n\n    /* Bail out if command has no keys */\n    if (cmd->getkeys_proc == NULL && cmd->firstkey == 0) {\n        errno = 0;\n        return NULL;\n    }\n\n    if ((cmd->arity > 0 && cmd->arity != argc) || (argc < -cmd->arity)) {\n        errno = EINVAL;\n        return NULL;\n    }\n\n    getKeysResult result = GETKEYS_RESULT_INIT;\n    getKeysFromCommand(cmd, argv, argc, &result);\n\n    *num_keys = result.numkeys;\n    if (!result.numkeys) {\n        errno = 0;\n        getKeysFreeResult(&result);\n        return NULL;\n    }\n\n    if (result.keys == result.keysbuf) {\n        /* If the result is using a stack based array, copy it. */\n        unsigned long int size = sizeof(int) * result.numkeys;\n        res = (int*)zmalloc(size);\n        memcpy(res, result.keys, size);\n    } else {\n        /* We return the heap based array and intentionally avoid calling\n         * getKeysFreeResult() here, as it is the caller's responsibility\n         * to free this array.\n         */\n        res = result.keys;\n    }\n\n    return res;\n}\n\n/* Return the name of the command currently running */\nconst char *RM_GetCurrentCommandName(RedisModuleCtx *ctx) {\n    if (!ctx || !ctx->client || !ctx->client->cmd)\n        return NULL;\n\n    return (const char*)ctx->client->cmd->name;\n}\n\n/* --------------------------------------------------------------------------\n * ## Defrag API\n * -------------------------------------------------------------------------- */\n\n/* The defrag context, used to manage state during calls to the data type\n * defrag callback.\n */\ntypedef struct RedisModuleDefragCtx {\n    long defragged;\n    long long int endtime;\n    unsigned long *cursor;\n} RedisModuleDefragCtx;\n\n/* Register a defrag callback for global data, i.e. anything that the module\n * may allocate that is not tied to a specific data type.\n */\nint RM_RegisterDefragFunc(RedisModuleCtx *ctx, RedisModuleDefragFunc cb) {\n    ctx->module->defrag_cb = cb;\n    return REDISMODULE_OK;\n}\n\n/* When the data type defrag callback iterates complex structures, this\n * function should be called periodically. A zero (false) return\n * indicates the callback may continue its work. A non-zero value (true)\n * indicates it should stop.\n *\n * When stopped, the callback may use RM_DefragCursorSet() to store its\n * position so it can later use RM_DefragCursorGet() to resume defragging.\n *\n * When stopped and more work is left to be done, the callback should\n * return 1. Otherwise, it should return 0.\n *\n * NOTE: Modules should consider the frequency in which this function is called,\n * so it generally makes sense to do small batches of work in between calls.\n */\nint RM_DefragShouldStop(RedisModuleDefragCtx *ctx) {\n    return (ctx->endtime != 0 && ctx->endtime < ustime());\n}\n\n/* Store an arbitrary cursor value for future re-use.\n *\n * This should only be called if RM_DefragShouldStop() has returned a non-zero\n * value and the defrag callback is about to exit without fully iterating its\n * data type.\n *\n * This behavior is reserved to cases where late defrag is performed. Late\n * defrag is selected for keys that implement the `free_effort` callback and\n * return a `free_effort` value that is larger than the defrag\n * 'active-defrag-max-scan-fields' configuration directive.\n *\n * Smaller keys, keys that do not implement `free_effort` or the global\n * defrag callback are not called in late-defrag mode. In those cases, a\n * call to this function will return REDISMODULE_ERR.\n *\n * The cursor may be used by the module to represent some progress into the\n * module's data type. Modules may also store additional cursor-related\n * information locally and use the cursor as a flag that indicates when\n * traversal of a new key begins. This is possible because the API makes\n * a guarantee that concurrent defragmentation of multiple keys will\n * not be performed.\n */\nint RM_DefragCursorSet(RedisModuleDefragCtx *ctx, unsigned long cursor) {\n    if (!ctx->cursor)\n        return REDISMODULE_ERR;\n\n    *ctx->cursor = cursor;\n    return REDISMODULE_OK;\n}\n\n/* Fetch a cursor value that has been previously stored using RM_DefragCursorSet().\n *\n * If not called for a late defrag operation, REDISMODULE_ERR will be returned and\n * the cursor should be ignored. See RM_DefragCursorSet() for more details on\n * defrag cursors.\n */\nint RM_DefragCursorGet(RedisModuleDefragCtx *ctx, unsigned long *cursor) {\n    if (!ctx->cursor)\n        return REDISMODULE_ERR;\n\n    *cursor = *ctx->cursor;\n    return REDISMODULE_OK;\n}\n\n/* Defrag a memory allocation previously allocated by RM_Alloc, RM_Calloc, etc.\n * The defragmentation process involves allocating a new memory block and copying\n * the contents to it, like realloc().\n *\n * If defragmentation was not necessary, NULL is returned and the operation has\n * no other effect.\n *\n * If a non-NULL value is returned, the caller should use the new pointer instead\n * of the old one and update any reference to the old pointer, which must not\n * be used again.\n */\nvoid *RM_DefragAlloc(RedisModuleDefragCtx *ctx, void *ptr) {\n    void *newptr = activeDefragAlloc(ptr);\n    if (newptr)\n        ctx->defragged++;\n\n    return newptr;\n}\n\n/* Defrag a RedisModuleString previously allocated by RM_Alloc, RM_Calloc, etc.\n * See RM_DefragAlloc() for more information on how the defragmentation process\n * works.\n *\n * NOTE: It is only possible to defrag strings that have a single reference.\n * Typically this means strings retained with RM_RetainString or RM_HoldString\n * may not be defragmentable. One exception is command argvs which, if retained\n * by the module, will end up with a single reference (because the reference\n * on the Redis side is dropped as soon as the command callback returns).\n */\nRedisModuleString *RM_DefragRedisModuleString(RedisModuleDefragCtx *ctx, RedisModuleString *str) {\n    return activeDefragStringOb(str, &ctx->defragged);\n}\n\n\n/* Perform a late defrag of a module datatype key.\n *\n * Returns a zero value (and initializes the cursor) if no more needs to be done,\n * or a non-zero value otherwise.\n */\nint moduleLateDefrag(robj *key, robj *value, unsigned long *cursor, long long endtime, long long *defragged) {\n    moduleValue *mv = (moduleValue*)ptrFromObj(value);\n    moduleType *mt = mv->type;\n\n    RedisModuleDefragCtx defrag_ctx = { 0, endtime, cursor };\n\n    /* Invoke callback. Note that the callback may be missing if the key has been\n     * replaced with a different type since our last visit.\n     */\n    int ret = 0;\n    if (mt->defrag)\n        ret = mt->defrag(&defrag_ctx, key, &mv->value);\n\n    *defragged += defrag_ctx.defragged;\n    if (!ret) {\n        *cursor = 0;    /* No more work to do */\n        return 0;\n    }\n\n    return 1;\n}\n\n/* Attempt to defrag a module data type value. Depending on complexity,\n * the operation may happen immediately or be scheduled for later.\n *\n * Returns 1 if the operation has been completed or 0 if it needs to\n * be scheduled for late defrag.\n */\nint moduleDefragValue(robj *key, robj *value, long *defragged) {\n    moduleValue *mv = (moduleValue*)ptrFromObj(value);\n    moduleType *mt = mv->type;\n\n    /* Try to defrag moduleValue itself regardless of whether or not\n     * defrag callbacks are provided.\n     */\n    moduleValue *newmv = (moduleValue*)activeDefragAlloc(mv);\n    if (newmv) {\n        (*defragged)++;\n        value->m_ptr = mv = newmv;\n    }\n\n    if (!mt->defrag)\n        return 1;\n\n    /* Use free_effort to determine complexity of module value, and if\n     * necessary schedule it for defragLater instead of quick immediate\n     * defrag.\n     */\n    if (mt->free_effort) {\n        size_t effort = mt->free_effort(key, mv->value);\n        if (!effort)\n            effort = SIZE_MAX;\n        if (effort > cserver.active_defrag_max_scan_fields) {\n            return 0;  /* Defrag later */\n        }\n    }\n\n    RedisModuleDefragCtx defrag_ctx = { 0, 0, NULL };\n    mt->defrag(&defrag_ctx, key, &mv->value);\n    (*defragged) += defrag_ctx.defragged;\n    return 1;\n}\n\n/* Call registered module API defrag functions */\nlong moduleDefragGlobals(void) {\n    dictIterator *di = dictGetIterator(modules);\n    dictEntry *de;\n    long defragged = 0;\n\n    while ((de = dictNext(di)) != NULL) {\n        struct RedisModule *module = (RedisModule*)dictGetVal(de);\n        if (!module->defrag_cb)\n            continue;\n        RedisModuleDefragCtx defrag_ctx = { 0, 0, NULL };\n        module->defrag_cb(&defrag_ctx);\n        defragged += defrag_ctx.defragged;\n    }\n    dictReleaseIterator(di);\n\n    return defragged;\n}\n\n/* Register all the APIs we export. Keep this function at the end of the\n * file so that's easy to seek it to add new entries. */\nvoid moduleRegisterCoreAPI(void) {\n    g_pserver->moduleapi = dictCreate(&moduleAPIDictType,NULL);\n    g_pserver->sharedapi = dictCreate(&moduleAPIDictType,NULL);\n    REGISTER_API(Alloc);\n    REGISTER_API(Calloc);\n    REGISTER_API(Realloc);\n    REGISTER_API(Free);\n    REGISTER_API(Strdup);\n    REGISTER_API(CreateCommand);\n    REGISTER_API(SetModuleAttribs);\n    REGISTER_API(IsModuleNameBusy);\n    REGISTER_API(WrongArity);\n    REGISTER_API(ReplyWithLongLong);\n    REGISTER_API(ReplyWithError);\n    REGISTER_API(ReplyWithSimpleString);\n    REGISTER_API(ReplyWithArray);\n    REGISTER_API(ReplyWithNullArray);\n    REGISTER_API(ReplyWithEmptyArray);\n    REGISTER_API(ReplySetArrayLength);\n    REGISTER_API(ReplyWithString);\n    REGISTER_API(ReplyWithEmptyString);\n    REGISTER_API(ReplyWithVerbatimString);\n    REGISTER_API(ReplyWithStringBuffer);\n    REGISTER_API(ReplyWithCString);\n    REGISTER_API(ReplyWithNull);\n    REGISTER_API(ReplyWithCallReply);\n    REGISTER_API(ReplyWithDouble);\n    REGISTER_API(ReplyWithLongDouble);\n    REGISTER_API(GetSelectedDb);\n    REGISTER_API(SelectDb);\n    REGISTER_API(OpenKey);\n    REGISTER_API(CloseKey);\n    REGISTER_API(KeyType);\n    REGISTER_API(ValueLength);\n    REGISTER_API(ListPush);\n    REGISTER_API(ListPop);\n    REGISTER_API(StringToLongLong);\n    REGISTER_API(StringToDouble);\n    REGISTER_API(StringToLongDouble);\n    REGISTER_API(StringToStreamID);\n    REGISTER_API(Call);\n    REGISTER_API(CallReplyProto);\n    REGISTER_API(FreeCallReply);\n    REGISTER_API(CallReplyInteger);\n    REGISTER_API(CallReplyType);\n    REGISTER_API(CallReplyLength);\n    REGISTER_API(CallReplyArrayElement);\n    REGISTER_API(CallReplyStringPtr);\n    REGISTER_API(CreateStringFromCallReply);\n    REGISTER_API(CreateString);\n    REGISTER_API(CreateStringFromLongLong);\n    REGISTER_API(CreateStringFromDouble);\n    REGISTER_API(CreateStringFromLongDouble);\n    REGISTER_API(CreateStringFromString);\n    REGISTER_API(CreateStringFromStreamID);\n    REGISTER_API(CreateStringPrintf);\n    REGISTER_API(FreeString);\n    REGISTER_API(StringPtrLen);\n    REGISTER_API(AutoMemory);\n    REGISTER_API(Replicate);\n    REGISTER_API(ReplicateVerbatim);\n    REGISTER_API(DeleteKey);\n    REGISTER_API(UnlinkKey);\n    REGISTER_API(StringSet);\n    REGISTER_API(StringDMA);\n    REGISTER_API(StringTruncate);\n    REGISTER_API(SetExpire);\n    REGISTER_API(GetExpire);\n    REGISTER_API(ResetDataset);\n    REGISTER_API(DbSize);\n    REGISTER_API(RandomKey);\n    REGISTER_API(ZsetAdd);\n    REGISTER_API(ZsetIncrby);\n    REGISTER_API(ZsetScore);\n    REGISTER_API(ZsetRem);\n    REGISTER_API(ZsetRangeStop);\n    REGISTER_API(ZsetFirstInScoreRange);\n    REGISTER_API(ZsetLastInScoreRange);\n    REGISTER_API(ZsetFirstInLexRange);\n    REGISTER_API(ZsetLastInLexRange);\n    REGISTER_API(ZsetRangeCurrentElement);\n    REGISTER_API(ZsetRangeNext);\n    REGISTER_API(ZsetRangePrev);\n    REGISTER_API(ZsetRangeEndReached);\n    REGISTER_API(HashSet);\n    REGISTER_API(HashGet);\n    REGISTER_API(StreamAdd);\n    REGISTER_API(StreamDelete);\n    REGISTER_API(StreamIteratorStart);\n    REGISTER_API(StreamIteratorStop);\n    REGISTER_API(StreamIteratorNextID);\n    REGISTER_API(StreamIteratorNextField);\n    REGISTER_API(StreamIteratorDelete);\n    REGISTER_API(StreamTrimByLength);\n    REGISTER_API(StreamTrimByID);\n    REGISTER_API(IsKeysPositionRequest);\n    REGISTER_API(KeyAtPos);\n    REGISTER_API(GetClientId);\n    REGISTER_API(GetClientUserNameById);\n    REGISTER_API(GetContextFlags);\n    REGISTER_API(AvoidReplicaTraffic);\n    REGISTER_API(PoolAlloc);\n    REGISTER_API(CreateDataType);\n    REGISTER_API(ModuleTypeSetValue);\n    REGISTER_API(ModuleTypeReplaceValue);\n    REGISTER_API(ModuleTypeGetType);\n    REGISTER_API(ModuleTypeGetValue);\n    REGISTER_API(IsIOError);\n    REGISTER_API(SetModuleOptions);\n    REGISTER_API(SignalModifiedKey);\n    REGISTER_API(SaveUnsigned);\n    REGISTER_API(LoadUnsigned);\n    REGISTER_API(SaveSigned);\n    REGISTER_API(LoadSigned);\n    REGISTER_API(SaveString);\n    REGISTER_API(SaveStringBuffer);\n    REGISTER_API(LoadString);\n    REGISTER_API(LoadStringBuffer);\n    REGISTER_API(SaveDouble);\n    REGISTER_API(LoadDouble);\n    REGISTER_API(SaveFloat);\n    REGISTER_API(LoadFloat);\n    REGISTER_API(SaveLongDouble);\n    REGISTER_API(LoadLongDouble);\n    REGISTER_API(SaveDataTypeToString);\n    REGISTER_API(LoadDataTypeFromString);\n    REGISTER_API(EmitAOF);\n    REGISTER_API(Log);\n    REGISTER_API(LogIOError);\n    REGISTER_API(_Assert);\n    REGISTER_API(LatencyAddSample);\n    REGISTER_API(StringAppendBuffer);\n    REGISTER_API(RetainString);\n    REGISTER_API(HoldString);\n    REGISTER_API(StringCompare);\n    REGISTER_API(GetContextFromIO);\n    REGISTER_API(GetKeyNameFromIO);\n    REGISTER_API(GetKeyNameFromModuleKey);\n    REGISTER_API(BlockClient);\n    REGISTER_API(UnblockClient);\n    REGISTER_API(IsBlockedReplyRequest);\n    REGISTER_API(IsBlockedTimeoutRequest);\n    REGISTER_API(GetBlockedClientPrivateData);\n    REGISTER_API(AbortBlock);\n    REGISTER_API(Milliseconds);\n    REGISTER_API(BlockedClientMeasureTimeStart);\n    REGISTER_API(BlockedClientMeasureTimeEnd);\n    REGISTER_API(GetThreadSafeContext);\n    REGISTER_API(GetDetachedThreadSafeContext);\n    REGISTER_API(FreeThreadSafeContext);\n    REGISTER_API(ThreadSafeContextLock);\n    REGISTER_API(ThreadSafeContextTryLock);\n    REGISTER_API(ThreadSafeContextUnlock);\n    REGISTER_API(DigestAddStringBuffer);\n    REGISTER_API(DigestAddLongLong);\n    REGISTER_API(DigestEndSequence);\n    REGISTER_API(NotifyKeyspaceEvent);\n    REGISTER_API(GetNotifyKeyspaceEvents);\n    REGISTER_API(SubscribeToKeyspaceEvents);\n    REGISTER_API(RegisterClusterMessageReceiver);\n    REGISTER_API(SendClusterMessage);\n    REGISTER_API(GetClusterNodeInfo);\n    REGISTER_API(GetClusterNodesList);\n    REGISTER_API(FreeClusterNodesList);\n    REGISTER_API(CreateTimer);\n    REGISTER_API(StopTimer);\n    REGISTER_API(GetTimerInfo);\n    REGISTER_API(GetMyClusterID);\n    REGISTER_API(GetClusterSize);\n    REGISTER_API(GetRandomBytes);\n    REGISTER_API(GetRandomHexChars);\n    REGISTER_API(BlockedClientDisconnected);\n    REGISTER_API(SetDisconnectCallback);\n    REGISTER_API(GetBlockedClientHandle);\n    REGISTER_API(SetClusterFlags);\n    REGISTER_API(CreateDict);\n    REGISTER_API(FreeDict);\n    REGISTER_API(DictSize);\n    REGISTER_API(DictSetC);\n    REGISTER_API(DictReplaceC);\n    REGISTER_API(DictSet);\n    REGISTER_API(DictReplace);\n    REGISTER_API(DictGetC);\n    REGISTER_API(DictGet);\n    REGISTER_API(DictDelC);\n    REGISTER_API(DictDel);\n    REGISTER_API(DictIteratorStartC);\n    REGISTER_API(DictIteratorStart);\n    REGISTER_API(DictIteratorStop);\n    REGISTER_API(DictIteratorReseekC);\n    REGISTER_API(DictIteratorReseek);\n    REGISTER_API(DictNextC);\n    REGISTER_API(DictPrevC);\n    REGISTER_API(DictNext);\n    REGISTER_API(DictPrev);\n    REGISTER_API(DictCompareC);\n    REGISTER_API(DictCompare);\n    REGISTER_API(ExportSharedAPI);\n    REGISTER_API(GetSharedAPI);\n    REGISTER_API(RegisterCommandFilter);\n    REGISTER_API(UnregisterCommandFilter);\n    REGISTER_API(CommandFilterArgsCount);\n    REGISTER_API(CommandFilterArgGet);\n    REGISTER_API(CommandFilterArgInsert);\n    REGISTER_API(CommandFilterArgReplace);\n    REGISTER_API(CommandFilterArgDelete);\n    REGISTER_API(Fork);\n    REGISTER_API(SendChildHeartbeat);\n    REGISTER_API(ExitFromChild);\n    REGISTER_API(KillForkChild);\n    REGISTER_API(RegisterInfoFunc);\n    REGISTER_API(InfoAddSection);\n    REGISTER_API(InfoBeginDictField);\n    REGISTER_API(InfoEndDictField);\n    REGISTER_API(InfoAddFieldString);\n    REGISTER_API(InfoAddFieldCString);\n    REGISTER_API(InfoAddFieldDouble);\n    REGISTER_API(InfoAddFieldLongLong);\n    REGISTER_API(InfoAddFieldULongLong);\n    REGISTER_API(GetServerInfo);\n    REGISTER_API(FreeServerInfo);\n    REGISTER_API(ServerInfoGetField);\n    REGISTER_API(ServerInfoGetFieldC);\n    REGISTER_API(ServerInfoGetFieldSigned);\n    REGISTER_API(ServerInfoGetFieldUnsigned);\n    REGISTER_API(ServerInfoGetFieldDouble);\n    REGISTER_API(GetClientInfoById);\n    REGISTER_API(PublishMessage);\n    REGISTER_API(SubscribeToServerEvent);\n    REGISTER_API(SetLRU);\n    REGISTER_API(GetLRU);\n    REGISTER_API(SetLFU);\n    REGISTER_API(GetLFU);\n    REGISTER_API(BlockClientOnKeys);\n    REGISTER_API(SignalKeyAsReady);\n    REGISTER_API(GetBlockedClientReadyKey);\n    REGISTER_API(GetUsedMemoryRatio);\n    REGISTER_API(MallocSize);\n    REGISTER_API(ScanCursorCreate);\n    REGISTER_API(ScanCursorDestroy);\n    REGISTER_API(ScanCursorRestart);\n    REGISTER_API(Scan);\n    REGISTER_API(ScanKey);\n    REGISTER_API(CreateModuleUser);\n    REGISTER_API(SetModuleUserACL);\n    REGISTER_API(FreeModuleUser);\n    REGISTER_API(DeauthenticateAndCloseClient);\n    REGISTER_API(AuthenticateClientWithACLUser);\n    REGISTER_API(AuthenticateClientWithUser);\n    REGISTER_API(GetContextFlagsAll);\n    REGISTER_API(GetKeyspaceNotificationFlagsAll);\n    REGISTER_API(IsSubEventSupported);\n    REGISTER_API(GetServerVersion);\n    REGISTER_API(GetClientCertificate);\n    REGISTER_API(GetCommandKeys);\n    REGISTER_API(GetCurrentCommandName);\n    REGISTER_API(GetTypeMethodVersion);\n    REGISTER_API(RegisterDefragFunc);\n    REGISTER_API(DefragAlloc);\n    REGISTER_API(DefragRedisModuleString);\n    REGISTER_API(DefragShouldStop);\n    REGISTER_API(DefragCursorSet);\n    REGISTER_API(DefragCursorGet);\n}\n"
  },
  {
    "path": "src/modules/.gitignore",
    "content": "*.so\n*.xo\n"
  },
  {
    "path": "src/modules/Makefile",
    "content": "\n# find the OS\nuname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')\n\n# Compile flags for linux / osx\nifeq ($(uname_S),Linux)\n\tSHOBJ_CFLAGS ?= -W -Wall -fno-common -g -ggdb -std=c99 -O2\n\tSHOBJ_LDFLAGS ?= -shared\nelse\n\tSHOBJ_CFLAGS ?= -W -Wall -dynamic -fno-common -g -ggdb -std=c99 -O2\n\tSHOBJ_LDFLAGS ?= -bundle -undefined dynamic_lookup\nendif\n\n.SUFFIXES: .c .so .xo .o\n\nall: helloworld.so hellotype.so helloblock.so hellocluster.so hellotimer.so hellodict.so hellohook.so helloacl.so build-keydb_modstatsd\n\nbuild-keydb_modstatsd:\n\t$(MAKE) -C keydb_modstatsd\n\n.c.xo:\n\t$(CC) -I. $(CFLAGS) $(SHOBJ_CFLAGS) -fPIC -c $< -o $@\n\nhelloworld.xo: ../redismodule.h\n\nhelloworld.so: helloworld.xo\n\t$(LD) -o $@ $< $(SHOBJ_LDFLAGS) $(LIBS) -lc\n\nhellotype.xo: ../redismodule.h\n\nhellotype.so: hellotype.xo\n\t$(LD) -o $@ $< $(SHOBJ_LDFLAGS) $(LIBS) -lc\n\nhelloblock.xo: ../redismodule.h\n\nhelloblock.so: helloblock.xo\n\t$(LD) -o $@ $< $(SHOBJ_LDFLAGS) $(LIBS) -lpthread -lc\n\nhellocluster.xo: ../redismodule.h\n\nhellocluster.so: hellocluster.xo\n\t$(LD) -o $@ $< $(SHOBJ_LDFLAGS) $(LIBS) -lc\n\nhellotimer.xo: ../redismodule.h\n\nhellotimer.so: hellotimer.xo\n\t$(LD) -o $@ $< $(SHOBJ_LDFLAGS) $(LIBS) -lc\n\nhellodict.xo: ../redismodule.h\n\nhellodict.so: hellodict.xo\n\t$(LD) -o $@ $< $(SHOBJ_LDFLAGS) $(LIBS) -lc\n\nhellohook.xo: ../redismodule.h\n\nhellohook.so: hellohook.xo\n\t$(LD) -o $@ $< $(SHOBJ_LDFLAGS) $(LIBS) -lc\n\nhelloacl.xo: ../redismodule.h\n\nhelloacl.so: helloacl.xo\n\t$(LD) -o $@ $< $(SHOBJ_LDFLAGS) $(LIBS) -lc\n\nclean:\n\trm -rf *.xo *.so\n\t$(MAKE) -C keydb_modstatsd clean\n"
  },
  {
    "path": "src/modules/gendoc.rb",
    "content": "# coding: utf-8\n# gendoc.rb -- Converts the top-comments inside module.c to modules API\n#              reference documentation in markdown format.\n\n# Convert the C comment to markdown\ndef markdown(s)\n    s = s.gsub(/\\*\\/$/,\"\")\n    s = s.gsub(/^ ?\\* ?/,\"\")\n    s = s.gsub(/^\\/\\*\\*? ?/,\"\")\n    s.chop! while s[-1] == \"\\n\" || s[-1] == \" \"\n    lines = s.split(\"\\n\")\n    newlines = []\n    # Fix some markdown, except in code blocks indented by 4 spaces.\n    lines.each{|l|\n        if not l.start_with?('    ')\n            # Rewrite RM_Xyz() to `RedisModule_Xyz()`. The () suffix is\n            # optional. Even RM_Xyz*() with * as wildcard is handled.\n            l = l.gsub(/(?<!`)RM_([A-z]+(?:\\*?\\(\\))?)/, '`RedisModule_\\1`')\n            # Add backquotes around RedisModule functions and type where missing.\n            l = l.gsub(/(?<!`)RedisModule[A-z]+(?:\\*?\\(\\))?/){|x| \"`#{x}`\"}\n            # Add backquotes around c functions like malloc() where missing.\n            l = l.gsub(/(?<![`A-z])[a-z_]+\\(\\)/, '`\\0`')\n            # Add backquotes around macro and var names containing underscores.\n            l = l.gsub(/(?<![`A-z\\*])[A-Za-z]+_[A-Za-z0-9_]+/){|x| \"`#{x}`\"}\n            # Link URLs preceded by space or newline (not already linked)\n            l = l.gsub(/(^| )(https?:\\/\\/[A-Za-z0-9_\\/\\.\\-]+[A-Za-z0-9\\/])/,\n                       '\\1[\\2](\\2)')\n            # Replace double-dash with unicode ndash\n            l = l.gsub(/ -- /, ' – ')\n        end\n        # Link function names to their definition within the page\n        l = l.gsub(/`(RedisModule_[A-z0-9]+)[()]*`/) {|x|\n            $index[$1] ? \"[#{x}](\\##{$1})\" : x\n        }\n        newlines << l\n    }\n    return newlines.join(\"\\n\")\nend\n\n# Linebreak a prototype longer than 80 characters on the commas, but only\n# between balanced parentheses so that we don't linebreak args which are\n# function pointers, and then aligning each arg under each other.\ndef linebreak_proto(proto, indent)\n    if proto.bytesize <= 80\n        return proto\n    end\n    parts = proto.split(/,\\s*/);\n    if parts.length == 1\n        return proto;\n    end\n    align_pos = proto.index(\"(\") + 1;\n    align = \" \" * align_pos\n    result = parts.shift;\n    bracket_balance = 0;\n    parts.each{|part|\n        if bracket_balance == 0\n            result += \",\\n\" + indent + align\n        else\n            result += \", \"\n        end\n        result += part\n        bracket_balance += part.count(\"(\") - part.count(\")\")\n    }\n    return result;\nend\n\n# Given the source code array and the index at which an exported symbol was\n# detected, extracts and outputs the documentation.\ndef docufy(src,i)\n    m = /RM_[A-z0-9]+/.match(src[i])\n    name = m[0]\n    name = name.sub(\"RM_\",\"RedisModule_\")\n    proto = src[i].sub(\"{\",\"\").strip+\";\\n\"\n    proto = proto.sub(\"RM_\",\"RedisModule_\")\n    proto = linebreak_proto(proto, \"    \");\n    # Add a link target with the function name. (We don't trust the exact id of\n    # the generated one, which depends on the Markdown implementation.)\n    puts \"<span id=\\\"#{name}\\\"></span>\\n\\n\"\n    puts \"### `#{name}`\\n\\n\"\n    puts \"    #{proto}\\n\"\n    comment = \"\"\n    while true\n        i = i-1\n        comment = src[i]+comment\n        break if src[i] =~ /\\/\\*/\n    end\n    comment = markdown(comment)\n    puts comment+\"\\n\\n\"\nend\n\n# Print a comment from line until */ is found, as markdown.\ndef section_doc(src, i)\n    name = get_section_heading(src, i)\n    comment = \"<span id=\\\"#{section_name_to_id(name)}\\\"></span>\\n\\n\"\n    while true\n         # append line, except if it's a horizontal divider\n        comment = comment + src[i] if src[i] !~ /^[\\/ ]?\\*{1,2} ?-{50,}/\n        break if src[i] =~ /\\*\\//\n        i = i+1\n    end\n    comment = markdown(comment)\n    puts comment+\"\\n\\n\"\nend\n\n# generates an id suitable for links within the page\ndef section_name_to_id(name)\n    return \"section-\" +\n           name.strip.downcase.gsub(/[^a-z0-9]+/, '-').gsub(/^-+|-+$/, '')\nend\n\n# Returns the name of the first section heading in the comment block for which\n# is_section_doc(src, i) is true\ndef get_section_heading(src, i)\n    if src[i] =~ /^\\/\\*\\*? \\#+ *(.*)/\n        heading = $1\n    elsif src[i+1] =~ /^ ?\\* \\#+ *(.*)/\n        heading = $1\n    end\n    return heading.gsub(' -- ', ' – ')\nend\n\n# Returns true if the line is the start of a generic documentation section. Such\n# section must start with the # symbol, i.e. a markdown heading, on the first or\n# the second line.\ndef is_section_doc(src, i)\n    return src[i] =~ /^\\/\\*\\*? \\#/ ||\n           (src[i] =~ /^\\/\\*/ && src[i+1] =~ /^ ?\\* \\#/)\nend\n\ndef is_func_line(src, i)\n  line = src[i]\n  return line =~ /RM_/ &&\n         line[0] != ' ' && line[0] != '#' && line[0] != '/' &&\n         src[i-1] =~ /\\*\\//\nend\n\nputs \"# Modules API reference\\n\\n\"\nputs \"<!-- This file is generated from module.c using gendoc.rb -->\\n\\n\"\nsrc = File.open(File.dirname(__FILE__) ++ \"/../module.c\").to_a\n\n# Build function index\n$index = {}\nsrc.each_with_index do |line,i|\n    if is_func_line(src, i)\n        line =~ /RM_([A-z0-9]+)/\n        name = \"RedisModule_#{$1}\"\n        $index[name] = true\n    end\nend\n\n# Print TOC\nputs \"## Sections\\n\\n\"\nsrc.each_with_index do |_line,i|\n    if is_section_doc(src, i)\n        name = get_section_heading(src, i)\n        puts \"* [#{name}](\\##{section_name_to_id(name)})\\n\"\n    end\nend\nputs \"* [Function index](#section-function-index)\\n\\n\"\n\n# Docufy: Print function prototype and markdown docs\nsrc.each_with_index do |_line,i|\n    if is_func_line(src, i)\n        docufy(src, i)\n    elsif is_section_doc(src, i)\n        section_doc(src, i)\n    end\nend\n\n# Print function index\nputs \"<span id=\\\"section-function-index\\\"></span>\\n\\n\"\nputs \"## Function index\\n\\n\"\n$index.keys.sort.each{|x| puts \"* [`#{x}`](\\##{x})\\n\"}\nputs \"\\n\"\n"
  },
  {
    "path": "src/modules/helloacl.c",
    "content": "/* ACL API example - An example for performing custom synchronous and\n * asynchronous password authentication.\n *\n * -----------------------------------------------------------------------------\n *\n * Copyright 2019 Amazon.com, Inc. or its affiliates.\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#define REDISMODULE_EXPERIMENTAL_API\n#include \"../redismodule.h\"\n#include <pthread.h>\n#include <unistd.h>\n\n// A simple global user\nstatic RedisModuleUser *global;\nstatic uint64_t global_auth_client_id = 0;\n\n/* HELLOACL.REVOKE \n * Synchronously revoke access from a user. */\nint RevokeCommand_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    REDISMODULE_NOT_USED(argv);\n    REDISMODULE_NOT_USED(argc);\n\n    if (global_auth_client_id) {\n        RedisModule_DeauthenticateAndCloseClient(ctx, global_auth_client_id);\n        return RedisModule_ReplyWithSimpleString(ctx, \"OK\");\n    } else {\n        return RedisModule_ReplyWithError(ctx, \"Global user currently not used\");    \n    }\n}\n\n/* HELLOACL.RESET \n * Synchronously delete and re-create a module user. */\nint ResetCommand_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    REDISMODULE_NOT_USED(argv);\n    REDISMODULE_NOT_USED(argc);\n\n    RedisModule_FreeModuleUser(global);\n    global = RedisModule_CreateModuleUser(\"global\");\n    RedisModule_SetModuleUserACL(global, \"allcommands\");\n    RedisModule_SetModuleUserACL(global, \"allkeys\");\n    RedisModule_SetModuleUserACL(global, \"on\");\n\n    return RedisModule_ReplyWithSimpleString(ctx, \"OK\");\n}\n\n/* Callback handler for user changes, use this to notify a module of \n * changes to users authenticated by the module */\nvoid HelloACL_UserChanged(uint64_t client_id, void *privdata) {\n    REDISMODULE_NOT_USED(privdata);\n    REDISMODULE_NOT_USED(client_id);\n    global_auth_client_id = 0;\n}\n\n/* HELLOACL.AUTHGLOBAL \n * Synchronously assigns a module user to the current context. */\nint AuthGlobalCommand_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    REDISMODULE_NOT_USED(argv);\n    REDISMODULE_NOT_USED(argc);\n\n    if (global_auth_client_id) {\n        return RedisModule_ReplyWithError(ctx, \"Global user currently used\");    \n    }\n\n    RedisModule_AuthenticateClientWithUser(ctx, global, HelloACL_UserChanged, NULL, &global_auth_client_id);\n\n    return RedisModule_ReplyWithSimpleString(ctx, \"OK\");\n}\n\n#define TIMEOUT_TIME 1000\n\n/* Reply callback for auth command HELLOACL.AUTHASYNC */\nint HelloACL_Reply(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    REDISMODULE_NOT_USED(argv);\n    REDISMODULE_NOT_USED(argc);\n    size_t length;\n\n    RedisModuleString *user_string = RedisModule_GetBlockedClientPrivateData(ctx);\n    const char *name = RedisModule_StringPtrLen(user_string, &length);\n\n    if (RedisModule_AuthenticateClientWithACLUser(ctx, name, length, NULL, NULL, NULL) == \n            REDISMODULE_ERR) {\n        return RedisModule_ReplyWithError(ctx, \"Invalid Username or password\");    \n    }\n    return RedisModule_ReplyWithSimpleString(ctx, \"OK\");\n}\n\n/* Timeout callback for auth command HELLOACL.AUTHASYNC */\nint HelloACL_Timeout(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    REDISMODULE_NOT_USED(argv);\n    REDISMODULE_NOT_USED(argc);\n    return RedisModule_ReplyWithSimpleString(ctx, \"Request timedout\");\n}\n\n/* Private data frees data for HELLOACL.AUTHASYNC command. */\nvoid HelloACL_FreeData(RedisModuleCtx *ctx, void *privdata) {\n    REDISMODULE_NOT_USED(ctx);\n    RedisModule_FreeString(NULL, privdata);\n}\n\n/* Background authentication can happen here. */\nvoid *HelloACL_ThreadMain(void *args) {\n    void **targs = args;\n    RedisModuleBlockedClient *bc = targs[0];\n    RedisModuleString *user = targs[1];\n    RedisModule_Free(targs);\n\n    RedisModule_UnblockClient(bc,user);\n    return NULL;\n}\n\n/* HELLOACL.AUTHASYNC \n * Asynchronously assigns an ACL user to the current context. */\nint AuthAsyncCommand_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    if (argc != 2) return RedisModule_WrongArity(ctx);\n\n    pthread_t tid;\n    RedisModuleBlockedClient *bc = RedisModule_BlockClient(ctx, HelloACL_Reply, HelloACL_Timeout, HelloACL_FreeData, TIMEOUT_TIME);\n    \n\n    void **targs = RedisModule_Alloc(sizeof(void*)*2);\n    targs[0] = bc;\n    targs[1] = RedisModule_CreateStringFromString(NULL, argv[1]);\n\n    if (pthread_create(&tid, NULL, HelloACL_ThreadMain, targs) != 0) {\n        RedisModule_AbortBlock(bc);\n        return RedisModule_ReplyWithError(ctx, \"-ERR Can't start thread\");\n    }\n\n    return REDISMODULE_OK;\n}\n\n/* This function must be present on each Redis module. It is used in order to\n * register the commands into the Redis server. */\nint RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    REDISMODULE_NOT_USED(argv);\n    REDISMODULE_NOT_USED(argc);\n\n    if (RedisModule_Init(ctx,\"helloacl\",1,REDISMODULE_APIVER_1)\n        == REDISMODULE_ERR) return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"helloacl.reset\",\n        ResetCommand_RedisCommand,\"\",0,0,0) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"helloacl.revoke\",\n        RevokeCommand_RedisCommand,\"\",0,0,0) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"helloacl.authglobal\",\n        AuthGlobalCommand_RedisCommand,\"no-auth\",0,0,0) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"helloacl.authasync\",\n        AuthAsyncCommand_RedisCommand,\"no-auth\",0,0,0) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    global = RedisModule_CreateModuleUser(\"global\");\n    RedisModule_SetModuleUserACL(global, \"allcommands\");\n    RedisModule_SetModuleUserACL(global, \"allkeys\");\n    RedisModule_SetModuleUserACL(global, \"on\");\n\n    global_auth_client_id = 0;\n\n    return REDISMODULE_OK;\n}\n"
  },
  {
    "path": "src/modules/helloblock.c",
    "content": "/* Helloblock module -- An example of blocking command implementation\n * with threads.\n *\n * -----------------------------------------------------------------------------\n *\n * Copyright (c) 2016, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#define REDISMODULE_EXPERIMENTAL_API\n#include \"../redismodule.h\"\n#include <stdio.h>\n#include <stdlib.h>\n#include <pthread.h>\n#include <unistd.h>\n\n/* Reply callback for blocking command HELLO.BLOCK */\nint HelloBlock_Reply(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    REDISMODULE_NOT_USED(argv);\n    REDISMODULE_NOT_USED(argc);\n    int *myint = RedisModule_GetBlockedClientPrivateData(ctx);\n    return RedisModule_ReplyWithLongLong(ctx,*myint);\n}\n\n/* Timeout callback for blocking command HELLO.BLOCK */\nint HelloBlock_Timeout(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    REDISMODULE_NOT_USED(argv);\n    REDISMODULE_NOT_USED(argc);\n    return RedisModule_ReplyWithSimpleString(ctx,\"Request timedout\");\n}\n\n/* Private data freeing callback for HELLO.BLOCK command. */\nvoid HelloBlock_FreeData(RedisModuleCtx *ctx, void *privdata) {\n    REDISMODULE_NOT_USED(ctx);\n    RedisModule_Free(privdata);\n}\n\n/* The thread entry point that actually executes the blocking part\n * of the command HELLO.BLOCK. */\nvoid *HelloBlock_ThreadMain(void *arg) {\n    void **targ = arg;\n    RedisModuleBlockedClient *bc = targ[0];\n    long long delay = (unsigned long)targ[1];\n    RedisModule_Free(targ);\n\n    sleep(delay);\n    int *r = RedisModule_Alloc(sizeof(int));\n    *r = rand();\n    RedisModule_UnblockClient(bc,r);\n    return NULL;\n}\n\n/* An example blocked client disconnection callback.\n *\n * Note that in the case of the HELLO.BLOCK command, the blocked client is now\n * owned by the thread calling sleep(). In this specific case, there is not\n * much we can do, however normally we could instead implement a way to\n * signal the thread that the client disconnected, and sleep the specified\n * amount of seconds with a while loop calling sleep(1), so that once we\n * detect the client disconnection, we can terminate the thread ASAP. */\nvoid HelloBlock_Disconnected(RedisModuleCtx *ctx, RedisModuleBlockedClient *bc) {\n    RedisModule_Log(ctx,\"warning\",\"Blocked client %p disconnected!\",\n        (void*)bc);\n\n    /* Here you should cleanup your state / threads, and if possible\n     * call RedisModule_UnblockClient(), or notify the thread that will\n     * call the function ASAP. */\n}\n\n/* HELLO.BLOCK <delay> <timeout> -- Block for <count> seconds, then reply with\n * a random number. Timeout is the command timeout, so that you can test\n * what happens when the delay is greater than the timeout. */\nint HelloBlock_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    if (argc != 3) return RedisModule_WrongArity(ctx);\n    long long delay;\n    long long timeout;\n\n    if (RedisModule_StringToLongLong(argv[1],&delay) != REDISMODULE_OK) {\n        return RedisModule_ReplyWithError(ctx,\"ERR invalid count\");\n    }\n\n    if (RedisModule_StringToLongLong(argv[2],&timeout) != REDISMODULE_OK) {\n        return RedisModule_ReplyWithError(ctx,\"ERR invalid count\");\n    }\n\n    pthread_t tid;\n    RedisModuleBlockedClient *bc = RedisModule_BlockClient(ctx,HelloBlock_Reply,HelloBlock_Timeout,HelloBlock_FreeData,timeout);\n\n    /* Here we set a disconnection handler, however since this module will\n     * block in sleep() in a thread, there is not much we can do in the\n     * callback, so this is just to show you the API. */\n    RedisModule_SetDisconnectCallback(bc,HelloBlock_Disconnected);\n\n    /* Now that we setup a blocking client, we need to pass the control\n     * to the thread. However we need to pass arguments to the thread:\n     * the delay and a reference to the blocked client handle. */\n    void **targ = RedisModule_Alloc(sizeof(void*)*2);\n    targ[0] = bc;\n    targ[1] = (void*)(unsigned long) delay;\n\n    if (pthread_create(&tid,NULL,HelloBlock_ThreadMain,targ) != 0) {\n        RedisModule_AbortBlock(bc);\n        return RedisModule_ReplyWithError(ctx,\"-ERR Can't start thread\");\n    }\n    return REDISMODULE_OK;\n}\n\n/* The thread entry point that actually executes the blocking part\n * of the command HELLO.KEYS.\n *\n * Note: this implementation is very simple on purpose, so no duplicated\n * keys (returned by SCAN) are filtered. However adding such a functionality\n * would be trivial just using any data structure implementing a dictionary\n * in order to filter the duplicated items. */\nvoid *HelloKeys_ThreadMain(void *arg) {\n    RedisModuleBlockedClient *bc = arg;\n    RedisModuleCtx *ctx = RedisModule_GetThreadSafeContext(bc);\n    long long cursor = 0;\n    size_t replylen = 0;\n\n    RedisModule_ReplyWithArray(ctx,REDISMODULE_POSTPONED_ARRAY_LEN);\n    do {\n        RedisModule_ThreadSafeContextLock(ctx);\n        RedisModuleCallReply *reply = RedisModule_Call(ctx,\n            \"SCAN\",\"l\",(long long)cursor);\n        RedisModule_ThreadSafeContextUnlock(ctx);\n\n        RedisModuleCallReply *cr_cursor =\n            RedisModule_CallReplyArrayElement(reply,0);\n        RedisModuleCallReply *cr_keys =\n            RedisModule_CallReplyArrayElement(reply,1);\n\n        RedisModuleString *s = RedisModule_CreateStringFromCallReply(cr_cursor);\n        RedisModule_StringToLongLong(s,&cursor);\n        RedisModule_FreeString(ctx,s);\n\n        size_t items = RedisModule_CallReplyLength(cr_keys);\n        for (size_t j = 0; j < items; j++) {\n            RedisModuleCallReply *ele =\n                RedisModule_CallReplyArrayElement(cr_keys,j);\n            RedisModule_ReplyWithCallReply(ctx,ele);\n            replylen++;\n        }\n        RedisModule_FreeCallReply(reply);\n    } while (cursor != 0);\n    RedisModule_ReplySetArrayLength(ctx,replylen);\n\n    RedisModule_FreeThreadSafeContext(ctx);\n    RedisModule_UnblockClient(bc,NULL);\n    return NULL;\n}\n\n/* HELLO.KEYS -- Return all the keys in the current database without blocking\n * the server. The keys do not represent a point-in-time state so only the keys\n * that were in the database from the start to the end are guaranteed to be\n * there. */\nint HelloKeys_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    REDISMODULE_NOT_USED(argv);\n    if (argc != 1) return RedisModule_WrongArity(ctx);\n\n    pthread_t tid;\n\n    /* Note that when blocking the client we do not set any callback: no\n     * timeout is possible since we passed '0', nor we need a reply callback\n     * because we'll use the thread safe context to accumulate a reply. */\n    RedisModuleBlockedClient *bc = RedisModule_BlockClient(ctx,NULL,NULL,NULL,0);\n\n    /* Now that we setup a blocking client, we need to pass the control\n     * to the thread. However we need to pass arguments to the thread:\n     * the reference to the blocked client handle. */\n    if (pthread_create(&tid,NULL,HelloKeys_ThreadMain,bc) != 0) {\n        RedisModule_AbortBlock(bc);\n        return RedisModule_ReplyWithError(ctx,\"-ERR Can't start thread\");\n    }\n    return REDISMODULE_OK;\n}\n\n/* This function must be present on each Redis module. It is used in order to\n * register the commands into the Redis server. */\nint RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    REDISMODULE_NOT_USED(argv);\n    REDISMODULE_NOT_USED(argc);\n\n    if (RedisModule_Init(ctx,\"helloblock\",1,REDISMODULE_APIVER_1)\n        == REDISMODULE_ERR) return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"hello.block\",\n        HelloBlock_RedisCommand,\"\",0,0,0) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n    if (RedisModule_CreateCommand(ctx,\"hello.keys\",\n        HelloKeys_RedisCommand,\"\",0,0,0) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    return REDISMODULE_OK;\n}\n"
  },
  {
    "path": "src/modules/hellocluster.c",
    "content": "/* Helloworld cluster -- A ping/pong cluster API example.\n *\n * -----------------------------------------------------------------------------\n *\n * Copyright (c) 2018, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#define REDISMODULE_EXPERIMENTAL_API\n#include \"../redismodule.h\"\n#include <stdio.h>\n#include <stdlib.h>\n#include <ctype.h>\n#include <string.h>\n\n#define MSGTYPE_PING 1\n#define MSGTYPE_PONG 2\n\n/* HELLOCLUSTER.PINGALL */\nint PingallCommand_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    REDISMODULE_NOT_USED(argv);\n    REDISMODULE_NOT_USED(argc);\n\n    RedisModule_SendClusterMessage(ctx,NULL,MSGTYPE_PING,(unsigned char*)\"Hey\",3);\n    return RedisModule_ReplyWithSimpleString(ctx, \"OK\");\n}\n\n/* HELLOCLUSTER.LIST */\nint ListCommand_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    REDISMODULE_NOT_USED(argv);\n    REDISMODULE_NOT_USED(argc);\n\n    size_t numnodes;\n    char **ids = RedisModule_GetClusterNodesList(ctx,&numnodes);\n    if (ids == NULL) {\n        return RedisModule_ReplyWithError(ctx,\"Cluster not enabled\");\n    }\n\n    RedisModule_ReplyWithArray(ctx,numnodes);\n    for (size_t j = 0; j < numnodes; j++) {\n        int port;\n        RedisModule_GetClusterNodeInfo(ctx,ids[j],NULL,NULL,&port,NULL);\n        RedisModule_ReplyWithArray(ctx,2);\n        RedisModule_ReplyWithStringBuffer(ctx,ids[j],REDISMODULE_NODE_ID_LEN);\n        RedisModule_ReplyWithLongLong(ctx,port);\n    }\n    RedisModule_FreeClusterNodesList(ids);\n    return REDISMODULE_OK;\n}\n\n/* Callback for message MSGTYPE_PING */\nvoid PingReceiver(RedisModuleCtx *ctx, const char *sender_id, uint8_t type, const unsigned char *payload, uint32_t len) {\n    RedisModule_Log(ctx,\"notice\",\"PING (type %d) RECEIVED from %.*s: '%.*s'\",\n        type,REDISMODULE_NODE_ID_LEN,sender_id,(int)len, payload);\n    RedisModule_SendClusterMessage(ctx,NULL,MSGTYPE_PONG,(unsigned char*)\"Ohi!\",4);\n    RedisModule_Call(ctx, \"INCR\", \"c\", \"pings_received\");\n}\n\n/* Callback for message MSGTYPE_PONG. */\nvoid PongReceiver(RedisModuleCtx *ctx, const char *sender_id, uint8_t type, const unsigned char *payload, uint32_t len) {\n    RedisModule_Log(ctx,\"notice\",\"PONG (type %d) RECEIVED from %.*s: '%.*s'\",\n        type,REDISMODULE_NODE_ID_LEN,sender_id,(int)len, payload);\n}\n\n/* This function must be present on each Redis module. It is used in order to\n * register the commands into the Redis server. */\nint RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    REDISMODULE_NOT_USED(argv);\n    REDISMODULE_NOT_USED(argc);\n\n    if (RedisModule_Init(ctx,\"hellocluster\",1,REDISMODULE_APIVER_1)\n        == REDISMODULE_ERR) return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"hellocluster.pingall\",\n        PingallCommand_RedisCommand,\"readonly\",0,0,0) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"hellocluster.list\",\n        ListCommand_RedisCommand,\"readonly\",0,0,0) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    /* Disable Redis Cluster sharding and redirections. This way every node\n     * will be able to access every possible key, regardless of the hash slot.\n     * This way the PING message handler will be able to increment a specific\n     * variable. Normally you do that in order for the distributed system\n     * you create as a module to have total freedom in the keyspace\n     * manipulation. */\n    RedisModule_SetClusterFlags(ctx,REDISMODULE_CLUSTER_FLAG_NO_REDIRECTION);\n\n    /* Register our handlers for different message types. */\n    RedisModule_RegisterClusterMessageReceiver(ctx,MSGTYPE_PING,PingReceiver);\n    RedisModule_RegisterClusterMessageReceiver(ctx,MSGTYPE_PONG,PongReceiver);\n    return REDISMODULE_OK;\n}\n"
  },
  {
    "path": "src/modules/hellodict.c",
    "content": "/* Hellodict -- An example of modules dictionary API\n *\n * This module implements a volatile key-value store on top of the\n * dictionary exported by the Redis modules API.\n *\n * -----------------------------------------------------------------------------\n *\n * Copyright (c) 2018, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#define REDISMODULE_EXPERIMENTAL_API\n#include \"../redismodule.h\"\n#include <stdio.h>\n#include <stdlib.h>\n#include <ctype.h>\n#include <string.h>\n\nstatic RedisModuleDict *Keyspace;\n\n/* HELLODICT.SET <key> <value>\n *\n * Set the specified key to the specified value. */\nint cmd_SET(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    if (argc != 3) return RedisModule_WrongArity(ctx);\n    RedisModule_DictSet(Keyspace,argv[1],argv[2]);\n    /* We need to keep a reference to the value stored at the key, otherwise\n     * it would be freed when this callback returns. */\n    RedisModule_RetainString(NULL,argv[2]);\n    return RedisModule_ReplyWithSimpleString(ctx, \"OK\");\n}\n\n/* HELLODICT.GET <key>\n *\n * Return the value of the specified key, or a null reply if the key\n * is not defined. */\nint cmd_GET(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    if (argc != 2) return RedisModule_WrongArity(ctx);\n    RedisModuleString *val = RedisModule_DictGet(Keyspace,argv[1],NULL);\n    if (val == NULL) {\n        return RedisModule_ReplyWithNull(ctx);\n    } else {\n        return RedisModule_ReplyWithString(ctx, val);\n    }\n}\n\n/* HELLODICT.KEYRANGE <startkey> <endkey> <count>\n *\n * Return a list of matching keys, lexicographically between startkey\n * and endkey. No more than 'count' items are emitted. */\nint cmd_KEYRANGE(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    if (argc != 4) return RedisModule_WrongArity(ctx);\n\n    /* Parse the count argument. */\n    long long count;\n    if (RedisModule_StringToLongLong(argv[3],&count) != REDISMODULE_OK) {\n        return RedisModule_ReplyWithError(ctx,\"ERR invalid count\");\n    }\n\n    /* Seek the iterator. */\n    RedisModuleDictIter *iter = RedisModule_DictIteratorStart(\n        Keyspace, \">=\", argv[1]);\n\n    /* Reply with the matching items. */\n    char *key;\n    size_t keylen;\n    long long replylen = 0; /* Keep track of the amitted array len. */\n    RedisModule_ReplyWithArray(ctx,REDISMODULE_POSTPONED_ARRAY_LEN);\n    while((key = RedisModule_DictNextC(iter,&keylen,NULL)) != NULL) {\n        if (replylen >= count) break;\n        if (RedisModule_DictCompare(iter,\"<=\",argv[2]) == REDISMODULE_ERR)\n            break;\n        RedisModule_ReplyWithStringBuffer(ctx,key,keylen);\n        replylen++;\n    }\n    RedisModule_ReplySetArrayLength(ctx,replylen);\n\n    /* Cleanup. */\n    RedisModule_DictIteratorStop(iter);\n    return REDISMODULE_OK;\n}\n\n/* This function must be present on each Redis module. It is used in order to\n * register the commands into the Redis server. */\nint RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    REDISMODULE_NOT_USED(argv);\n    REDISMODULE_NOT_USED(argc);\n\n    if (RedisModule_Init(ctx,\"hellodict\",1,REDISMODULE_APIVER_1)\n        == REDISMODULE_ERR) return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"hellodict.set\",\n        cmd_SET,\"write deny-oom\",1,1,0) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"hellodict.get\",\n        cmd_GET,\"readonly\",1,1,0) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"hellodict.keyrange\",\n        cmd_KEYRANGE,\"readonly\",1,1,0) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    /* Create our global dictionary. Here we'll set our keys and values. */\n    Keyspace = RedisModule_CreateDict(NULL);\n\n    return REDISMODULE_OK;\n}\n"
  },
  {
    "path": "src/modules/hellohook.c",
    "content": "/* Server hooks API example\n *\n * -----------------------------------------------------------------------------\n *\n * Copyright (c) 2019, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#define REDISMODULE_EXPERIMENTAL_API\n#include \"../redismodule.h\"\n#include <stdio.h>\n#include <stdlib.h>\n#include <ctype.h>\n#include <string.h>\n\n/* Client state change callback. */\nvoid clientChangeCallback(RedisModuleCtx *ctx, RedisModuleEvent e, uint64_t sub, void *data)\n{\n    REDISMODULE_NOT_USED(ctx);\n    REDISMODULE_NOT_USED(e);\n\n    RedisModuleClientInfo *ci = data;\n    printf(\"Client %s event for client #%llu %s:%d\\n\",\n        (sub == REDISMODULE_SUBEVENT_CLIENT_CHANGE_CONNECTED) ?\n            \"connection\" : \"disconnection\",\n        (unsigned long long)ci->id,ci->addr,ci->port);\n}\n\nvoid flushdbCallback(RedisModuleCtx *ctx, RedisModuleEvent e, uint64_t sub, void *data)\n{\n    REDISMODULE_NOT_USED(ctx);\n    REDISMODULE_NOT_USED(e);\n\n    RedisModuleFlushInfo *fi = data;\n    if (sub == REDISMODULE_SUBEVENT_FLUSHDB_START) {\n        if (fi->dbnum != -1) {\n            RedisModuleCallReply *reply;\n            reply = RedisModule_Call(ctx,\"DBSIZE\",\"\");\n            long long numkeys = RedisModule_CallReplyInteger(reply);\n            printf(\"FLUSHDB event of database %d started (%lld keys in DB)\\n\",\n                fi->dbnum, numkeys);\n            RedisModule_FreeCallReply(reply);\n        } else {\n            printf(\"FLUSHALL event started\\n\");\n        }\n    } else {\n        if (fi->dbnum != -1) {\n            printf(\"FLUSHDB event of database %d ended\\n\",fi->dbnum);\n        } else {\n            printf(\"FLUSHALL event ended\\n\");\n        }\n    }\n}\n\n/* This function must be present on each Redis module. It is used in order to\n * register the commands into the Redis server. */\nint RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    REDISMODULE_NOT_USED(argv);\n    REDISMODULE_NOT_USED(argc);\n\n    if (RedisModule_Init(ctx,\"hellohook\",1,REDISMODULE_APIVER_1)\n        == REDISMODULE_ERR) return REDISMODULE_ERR;\n\n    RedisModule_SubscribeToServerEvent(ctx,\n        RedisModuleEvent_ClientChange, clientChangeCallback);\n    RedisModule_SubscribeToServerEvent(ctx,\n        RedisModuleEvent_FlushDB, flushdbCallback);\n    return REDISMODULE_OK;\n}\n"
  },
  {
    "path": "src/modules/hellotimer.c",
    "content": "/* Timer API example -- Register and handle timer events\n *\n * -----------------------------------------------------------------------------\n *\n * Copyright (c) 2018, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#define REDISMODULE_EXPERIMENTAL_API\n#include \"../redismodule.h\"\n#include <stdio.h>\n#include <stdlib.h>\n#include <ctype.h>\n#include <string.h>\n\n/* Timer callback. */\nvoid timerHandler(RedisModuleCtx *ctx, void *data) {\n    REDISMODULE_NOT_USED(ctx);\n    printf(\"Fired %s!\\n\", (char *)data);\n    RedisModule_Free(data);\n}\n\n/* HELLOTIMER.TIMER*/\nint TimerCommand_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    REDISMODULE_NOT_USED(argv);\n    REDISMODULE_NOT_USED(argc);\n\n    for (int j = 0; j < 10; j++) {\n        int delay = rand() % 5000;\n        int bufsize = 256;\n        char *buf = RedisModule_Alloc(bufsize);\n        snprintf(buf,bufsize,\"After %d\", delay);\n        RedisModuleTimerID tid = RedisModule_CreateTimer(ctx,delay,timerHandler,buf);\n        REDISMODULE_NOT_USED(tid);\n    }\n    return RedisModule_ReplyWithSimpleString(ctx, \"OK\");\n}\n\n/* This function must be present on each Redis module. It is used in order to\n * register the commands into the Redis server. */\nint RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    REDISMODULE_NOT_USED(argv);\n    REDISMODULE_NOT_USED(argc);\n\n    if (RedisModule_Init(ctx,\"hellotimer\",1,REDISMODULE_APIVER_1)\n        == REDISMODULE_ERR) return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"hellotimer.timer\",\n        TimerCommand_RedisCommand,\"readonly\",0,0,0) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    return REDISMODULE_OK;\n}\n"
  },
  {
    "path": "src/modules/hellotype.c",
    "content": "/* This file implements a new module native data type called \"HELLOTYPE\".\n * The data structure implemented is a very simple ordered linked list of\n * 64 bit integers, in order to have something that is real world enough, but\n * at the same time, extremely simple to understand, to show how the API\n * works, how a new data type is created, and how to write basic methods\n * for RDB loading, saving and AOF rewriting.\n *\n * -----------------------------------------------------------------------------\n *\n * Copyright (c) 2016, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"../redismodule.h\"\n#include <stdio.h>\n#include <stdlib.h>\n#include <ctype.h>\n#include <string.h>\n#include <stdint.h>\n\nstatic RedisModuleType *HelloType;\n\n/* ========================== Internal data structure  =======================\n * This is just a linked list of 64 bit integers where elements are inserted\n * in-place, so it's ordered. There is no pop/push operation but just insert\n * because it is enough to show the implementation of new data types without\n * making things complex. */\n\nstruct HelloTypeNode {\n    int64_t value;\n    struct HelloTypeNode *next;\n};\n\nstruct HelloTypeObject {\n    struct HelloTypeNode *head;\n    size_t len; /* Number of elements added. */\n};\n\nstruct HelloTypeObject *createHelloTypeObject(void) {\n    struct HelloTypeObject *o;\n    o = RedisModule_Alloc(sizeof(*o));\n    o->head = NULL;\n    o->len = 0;\n    return o;\n}\n\nvoid HelloTypeInsert(struct HelloTypeObject *o, int64_t ele) {\n    struct HelloTypeNode *next = o->head, *newnode, *prev = NULL;\n\n    while(next && next->value < ele) {\n        prev = next;\n        next = next->next;\n    }\n    newnode = RedisModule_Alloc(sizeof(*newnode));\n    newnode->value = ele;\n    newnode->next = next;\n    if (prev) {\n        prev->next = newnode;\n    } else {\n        o->head = newnode;\n    }\n    o->len++;\n}\n\nvoid HelloTypeReleaseObject(struct HelloTypeObject *o) {\n    struct HelloTypeNode *cur, *next;\n    cur = o->head;\n    while(cur) {\n        next = cur->next;\n        RedisModule_Free(cur);\n        cur = next;\n    }\n    RedisModule_Free(o);\n}\n\n/* ========================= \"hellotype\" type commands ======================= */\n\n/* HELLOTYPE.INSERT key value */\nint HelloTypeInsert_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    RedisModule_AutoMemory(ctx); /* Use automatic memory management. */\n\n    if (argc != 3) return RedisModule_WrongArity(ctx);\n    RedisModuleKey *key = RedisModule_OpenKey(ctx,argv[1],\n        REDISMODULE_READ|REDISMODULE_WRITE);\n    int type = RedisModule_KeyType(key);\n    if (type != REDISMODULE_KEYTYPE_EMPTY &&\n        RedisModule_ModuleTypeGetType(key) != HelloType)\n    {\n        return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE);\n    }\n\n    long long value;\n    if ((RedisModule_StringToLongLong(argv[2],&value) != REDISMODULE_OK)) {\n        return RedisModule_ReplyWithError(ctx,\"ERR invalid value: must be a signed 64 bit integer\");\n    }\n\n    /* Create an empty value object if the key is currently empty. */\n    struct HelloTypeObject *hto;\n    if (type == REDISMODULE_KEYTYPE_EMPTY) {\n        hto = createHelloTypeObject();\n        RedisModule_ModuleTypeSetValue(key,HelloType,hto);\n    } else {\n        hto = RedisModule_ModuleTypeGetValue(key);\n    }\n\n    /* Insert the new element. */\n    HelloTypeInsert(hto,value);\n    RedisModule_SignalKeyAsReady(ctx,argv[1]);\n\n    RedisModule_ReplyWithLongLong(ctx,hto->len);\n    RedisModule_ReplicateVerbatim(ctx);\n    return REDISMODULE_OK;\n}\n\n/* HELLOTYPE.RANGE key first count */\nint HelloTypeRange_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    RedisModule_AutoMemory(ctx); /* Use automatic memory management. */\n\n    if (argc != 4) return RedisModule_WrongArity(ctx);\n    RedisModuleKey *key = RedisModule_OpenKey(ctx,argv[1],\n        REDISMODULE_READ|REDISMODULE_WRITE);\n    int type = RedisModule_KeyType(key);\n    if (type != REDISMODULE_KEYTYPE_EMPTY &&\n        RedisModule_ModuleTypeGetType(key) != HelloType)\n    {\n        return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE);\n    }\n\n    long long first, count;\n    if (RedisModule_StringToLongLong(argv[2],&first) != REDISMODULE_OK ||\n        RedisModule_StringToLongLong(argv[3],&count) != REDISMODULE_OK ||\n        first < 0 || count < 0)\n    {\n        return RedisModule_ReplyWithError(ctx,\n            \"ERR invalid first or count parameters\");\n    }\n\n    struct HelloTypeObject *hto = RedisModule_ModuleTypeGetValue(key);\n    struct HelloTypeNode *node = hto ? hto->head : NULL;\n    RedisModule_ReplyWithArray(ctx,REDISMODULE_POSTPONED_ARRAY_LEN);\n    long long arraylen = 0;\n    while(node && count--) {\n        RedisModule_ReplyWithLongLong(ctx,node->value);\n        arraylen++;\n        node = node->next;\n    }\n    RedisModule_ReplySetArrayLength(ctx,arraylen);\n    return REDISMODULE_OK;\n}\n\n/* HELLOTYPE.LEN key */\nint HelloTypeLen_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    RedisModule_AutoMemory(ctx); /* Use automatic memory management. */\n\n    if (argc != 2) return RedisModule_WrongArity(ctx);\n    RedisModuleKey *key = RedisModule_OpenKey(ctx,argv[1],\n        REDISMODULE_READ|REDISMODULE_WRITE);\n    int type = RedisModule_KeyType(key);\n    if (type != REDISMODULE_KEYTYPE_EMPTY &&\n        RedisModule_ModuleTypeGetType(key) != HelloType)\n    {\n        return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE);\n    }\n\n    struct HelloTypeObject *hto = RedisModule_ModuleTypeGetValue(key);\n    RedisModule_ReplyWithLongLong(ctx,hto ? hto->len : 0);\n    return REDISMODULE_OK;\n}\n\n/* ====================== Example of a blocking command ==================== */\n\n/* Reply callback for blocking command HELLOTYPE.BRANGE, this will get\n * called when the key we blocked for is ready: we need to check if we\n * can really serve the client, and reply OK or ERR accordingly. */\nint HelloBlock_Reply(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    REDISMODULE_NOT_USED(argv);\n    REDISMODULE_NOT_USED(argc);\n\n    RedisModuleString *keyname = RedisModule_GetBlockedClientReadyKey(ctx);\n    RedisModuleKey *key = RedisModule_OpenKey(ctx,keyname,REDISMODULE_READ);\n    int type = RedisModule_KeyType(key);\n    if (type != REDISMODULE_KEYTYPE_MODULE ||\n        RedisModule_ModuleTypeGetType(key) != HelloType)\n    {\n        RedisModule_CloseKey(key);\n        return REDISMODULE_ERR;\n    }\n\n    /* In case the key is able to serve our blocked client, let's directly\n     * use our original command implementation to make this example simpler. */\n    RedisModule_CloseKey(key);\n    return HelloTypeRange_RedisCommand(ctx,argv,argc-1);\n}\n\n/* Timeout callback for blocking command HELLOTYPE.BRANGE */\nint HelloBlock_Timeout(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    REDISMODULE_NOT_USED(argv);\n    REDISMODULE_NOT_USED(argc);\n    return RedisModule_ReplyWithSimpleString(ctx,\"Request timedout\");\n}\n\n/* Private data freeing callback for HELLOTYPE.BRANGE command. */\nvoid HelloBlock_FreeData(RedisModuleCtx *ctx, void *privdata) {\n    REDISMODULE_NOT_USED(ctx);\n    RedisModule_Free(privdata);\n}\n\n/* HELLOTYPE.BRANGE key first count timeout -- This is a blocking verison of\n * the RANGE operation, in order to show how to use the API\n * RedisModule_BlockClientOnKeys(). */\nint HelloTypeBRange_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    if (argc != 5) return RedisModule_WrongArity(ctx);\n    RedisModule_AutoMemory(ctx); /* Use automatic memory management. */\n    RedisModuleKey *key = RedisModule_OpenKey(ctx,argv[1],\n        REDISMODULE_READ|REDISMODULE_WRITE);\n    int type = RedisModule_KeyType(key);\n    if (type != REDISMODULE_KEYTYPE_EMPTY &&\n        RedisModule_ModuleTypeGetType(key) != HelloType)\n    {\n        return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE);\n    }\n\n    /* Parse the timeout before even trying to serve the client synchronously,\n     * so that we always fail ASAP on syntax errors. */\n    long long timeout;\n    if (RedisModule_StringToLongLong(argv[4],&timeout) != REDISMODULE_OK) {\n        return RedisModule_ReplyWithError(ctx,\n            \"ERR invalid timeout parameter\");\n    }\n\n    /* Can we serve the reply synchronously? */\n    if (type != REDISMODULE_KEYTYPE_EMPTY) {\n        return HelloTypeRange_RedisCommand(ctx,argv,argc-1);\n    }\n\n    /* Otherwise let's block on the key. */\n    void *privdata = RedisModule_Alloc(100);\n    RedisModule_BlockClientOnKeys(ctx,HelloBlock_Reply,HelloBlock_Timeout,HelloBlock_FreeData,timeout,argv+1,1,privdata);\n    return REDISMODULE_OK;\n}\n\n/* ========================== \"hellotype\" type methods ======================= */\n\nvoid *HelloTypeRdbLoad(RedisModuleIO *rdb, int encver) {\n    if (encver != 0) {\n        /* RedisModule_Log(\"warning\",\"Can't load data with version %d\", encver);*/\n        return NULL;\n    }\n    uint64_t elements = RedisModule_LoadUnsigned(rdb);\n    struct HelloTypeObject *hto = createHelloTypeObject();\n    while(elements--) {\n        int64_t ele = RedisModule_LoadSigned(rdb);\n        HelloTypeInsert(hto,ele);\n    }\n    return hto;\n}\n\nvoid HelloTypeRdbSave(RedisModuleIO *rdb, void *value) {\n    struct HelloTypeObject *hto = value;\n    struct HelloTypeNode *node = hto->head;\n    RedisModule_SaveUnsigned(rdb,hto->len);\n    while(node) {\n        RedisModule_SaveSigned(rdb,node->value);\n        node = node->next;\n    }\n}\n\nvoid HelloTypeAofRewrite(RedisModuleIO *aof, RedisModuleString *key, void *value) {\n    struct HelloTypeObject *hto = value;\n    struct HelloTypeNode *node = hto->head;\n    while(node) {\n        RedisModule_EmitAOF(aof,\"HELLOTYPE.INSERT\",\"sl\",key,node->value);\n        node = node->next;\n    }\n}\n\n/* The goal of this function is to return the amount of memory used by\n * the HelloType value. */\nsize_t HelloTypeMemUsage(const void *value) {\n    const struct HelloTypeObject *hto = value;\n    struct HelloTypeNode *node = hto->head;\n    return sizeof(*hto) + sizeof(*node)*hto->len;\n}\n\nvoid HelloTypeFree(void *value) {\n    HelloTypeReleaseObject(value);\n}\n\nvoid HelloTypeDigest(RedisModuleDigest *md, void *value) {\n    struct HelloTypeObject *hto = value;\n    struct HelloTypeNode *node = hto->head;\n    while(node) {\n        RedisModule_DigestAddLongLong(md,node->value);\n        node = node->next;\n    }\n    RedisModule_DigestEndSequence(md);\n}\n\n/* This function must be present on each Redis module. It is used in order to\n * register the commands into the Redis server. */\nint RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    REDISMODULE_NOT_USED(argv);\n    REDISMODULE_NOT_USED(argc);\n\n    if (RedisModule_Init(ctx,\"hellotype\",1,REDISMODULE_APIVER_1)\n        == REDISMODULE_ERR) return REDISMODULE_ERR;\n\n    RedisModuleTypeMethods tm = {\n        .version = REDISMODULE_TYPE_METHOD_VERSION,\n        .rdb_load = HelloTypeRdbLoad,\n        .rdb_save = HelloTypeRdbSave,\n        .aof_rewrite = HelloTypeAofRewrite,\n        .mem_usage = HelloTypeMemUsage,\n        .free = HelloTypeFree,\n        .digest = HelloTypeDigest\n    };\n\n    HelloType = RedisModule_CreateDataType(ctx,\"hellotype\",0,&tm);\n    if (HelloType == NULL) return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"hellotype.insert\",\n        HelloTypeInsert_RedisCommand,\"write deny-oom\",1,1,1) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"hellotype.range\",\n        HelloTypeRange_RedisCommand,\"readonly\",1,1,1) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"hellotype.len\",\n        HelloTypeLen_RedisCommand,\"readonly\",1,1,1) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"hellotype.brange\",\n        HelloTypeBRange_RedisCommand,\"readonly\",1,1,1) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    return REDISMODULE_OK;\n}\n"
  },
  {
    "path": "src/modules/helloworld.c",
    "content": "/* Helloworld module -- A few examples of the Redis Modules API in the form\n * of commands showing how to accomplish common tasks.\n *\n * This module does not do anything useful, if not for a few commands. The\n * examples are designed in order to show the API.\n *\n * -----------------------------------------------------------------------------\n *\n * Copyright (c) 2016, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"../redismodule.h\"\n#include <stdio.h>\n#include <stdlib.h>\n#include <ctype.h>\n#include <string.h>\n\n/* HELLO.SIMPLE is among the simplest commands you can implement.\n * It just returns the currently selected DB id, a functionality which is\n * missing in Redis. The command uses two important API calls: one to\n * fetch the currently selected DB, the other in order to send the client\n * an integer reply as response. */\nint HelloSimple_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    REDISMODULE_NOT_USED(argv);\n    REDISMODULE_NOT_USED(argc);\n    RedisModule_ReplyWithLongLong(ctx,RedisModule_GetSelectedDb(ctx));\n    return REDISMODULE_OK;\n}\n\n/* HELLO.PUSH.NATIVE re-implements RPUSH, and shows the low level modules API\n * where you can \"open\" keys, make low level operations, create new keys by\n * pushing elements into non-existing keys, and so forth.\n *\n * You'll find this command to be roughly as fast as the actual RPUSH\n * command. */\nint HelloPushNative_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)\n{\n    if (argc != 3) return RedisModule_WrongArity(ctx);\n\n    RedisModuleKey *key = RedisModule_OpenKey(ctx,argv[1],\n        REDISMODULE_READ|REDISMODULE_WRITE);\n\n    RedisModule_ListPush(key,REDISMODULE_LIST_TAIL,argv[2]);\n    size_t newlen = RedisModule_ValueLength(key);\n    RedisModule_CloseKey(key);\n    RedisModule_ReplyWithLongLong(ctx,newlen);\n    return REDISMODULE_OK;\n}\n\n/* HELLO.PUSH.CALL implements RPUSH using an higher level approach, calling\n * a Redis command instead of working with the key in a low level way. This\n * approach is useful when you need to call Redis commands that are not\n * available as low level APIs, or when you don't need the maximum speed\n * possible but instead prefer implementation simplicity. */\nint HelloPushCall_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)\n{\n    if (argc != 3) return RedisModule_WrongArity(ctx);\n\n    RedisModuleCallReply *reply;\n\n    reply = RedisModule_Call(ctx,\"RPUSH\",\"ss\",argv[1],argv[2]);\n    long long len = RedisModule_CallReplyInteger(reply);\n    RedisModule_FreeCallReply(reply);\n    RedisModule_ReplyWithLongLong(ctx,len);\n    return REDISMODULE_OK;\n}\n\n/* HELLO.PUSH.CALL2\n * This is exactly as HELLO.PUSH.CALL, but shows how we can reply to the\n * client using directly a reply object that Call() returned. */\nint HelloPushCall2_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)\n{\n    if (argc != 3) return RedisModule_WrongArity(ctx);\n\n    RedisModuleCallReply *reply;\n\n    reply = RedisModule_Call(ctx,\"RPUSH\",\"ss\",argv[1],argv[2]);\n    RedisModule_ReplyWithCallReply(ctx,reply);\n    RedisModule_FreeCallReply(reply);\n    return REDISMODULE_OK;\n}\n\n/* HELLO.LIST.SUM.LEN returns the total length of all the items inside\n * a Redis list, by using the high level Call() API.\n * This command is an example of the array reply access. */\nint HelloListSumLen_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)\n{\n    if (argc != 2) return RedisModule_WrongArity(ctx);\n\n    RedisModuleCallReply *reply;\n\n    reply = RedisModule_Call(ctx,\"LRANGE\",\"sll\",argv[1],(long long)0,(long long)-1);\n    size_t strlen = 0;\n    size_t items = RedisModule_CallReplyLength(reply);\n    size_t j;\n    for (j = 0; j < items; j++) {\n        RedisModuleCallReply *ele = RedisModule_CallReplyArrayElement(reply,j);\n        strlen += RedisModule_CallReplyLength(ele);\n    }\n    RedisModule_FreeCallReply(reply);\n    RedisModule_ReplyWithLongLong(ctx,strlen);\n    return REDISMODULE_OK;\n}\n\n/* HELLO.LIST.SPLICE srclist dstlist count\n * Moves 'count' elements from the tail of 'srclist' to the head of\n * 'dstlist'. If less than count elements are available, it moves as much\n * elements as possible. */\nint HelloListSplice_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    if (argc != 4) return RedisModule_WrongArity(ctx);\n\n    RedisModuleKey *srckey = RedisModule_OpenKey(ctx,argv[1],\n        REDISMODULE_READ|REDISMODULE_WRITE);\n    RedisModuleKey *dstkey = RedisModule_OpenKey(ctx,argv[2],\n        REDISMODULE_READ|REDISMODULE_WRITE);\n\n    /* Src and dst key must be empty or lists. */\n    if ((RedisModule_KeyType(srckey) != REDISMODULE_KEYTYPE_LIST &&\n         RedisModule_KeyType(srckey) != REDISMODULE_KEYTYPE_EMPTY) ||\n        (RedisModule_KeyType(dstkey) != REDISMODULE_KEYTYPE_LIST &&\n         RedisModule_KeyType(dstkey) != REDISMODULE_KEYTYPE_EMPTY))\n    {\n        RedisModule_CloseKey(srckey);\n        RedisModule_CloseKey(dstkey);\n        return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE);\n    }\n\n    long long count;\n    if ((RedisModule_StringToLongLong(argv[3],&count) != REDISMODULE_OK) ||\n        (count < 0)) {\n        RedisModule_CloseKey(srckey);\n        RedisModule_CloseKey(dstkey);\n        return RedisModule_ReplyWithError(ctx,\"ERR invalid count\");\n    }\n\n    while(count-- > 0) {\n        RedisModuleString *ele;\n\n        ele = RedisModule_ListPop(srckey,REDISMODULE_LIST_TAIL);\n        if (ele == NULL) break;\n        RedisModule_ListPush(dstkey,REDISMODULE_LIST_HEAD,ele);\n        RedisModule_FreeString(ctx,ele);\n    }\n\n    size_t len = RedisModule_ValueLength(srckey);\n    RedisModule_CloseKey(srckey);\n    RedisModule_CloseKey(dstkey);\n    RedisModule_ReplyWithLongLong(ctx,len);\n    return REDISMODULE_OK;\n}\n\n/* Like the HELLO.LIST.SPLICE above, but uses automatic memory management\n * in order to avoid freeing stuff. */\nint HelloListSpliceAuto_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    if (argc != 4) return RedisModule_WrongArity(ctx);\n\n    RedisModule_AutoMemory(ctx);\n\n    RedisModuleKey *srckey = RedisModule_OpenKey(ctx,argv[1],\n        REDISMODULE_READ|REDISMODULE_WRITE);\n    RedisModuleKey *dstkey = RedisModule_OpenKey(ctx,argv[2],\n        REDISMODULE_READ|REDISMODULE_WRITE);\n\n    /* Src and dst key must be empty or lists. */\n    if ((RedisModule_KeyType(srckey) != REDISMODULE_KEYTYPE_LIST &&\n         RedisModule_KeyType(srckey) != REDISMODULE_KEYTYPE_EMPTY) ||\n        (RedisModule_KeyType(dstkey) != REDISMODULE_KEYTYPE_LIST &&\n         RedisModule_KeyType(dstkey) != REDISMODULE_KEYTYPE_EMPTY))\n    {\n        return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE);\n    }\n\n    long long count;\n    if ((RedisModule_StringToLongLong(argv[3],&count) != REDISMODULE_OK) ||\n        (count < 0))\n    {\n        return RedisModule_ReplyWithError(ctx,\"ERR invalid count\");\n    }\n\n    while(count-- > 0) {\n        RedisModuleString *ele;\n\n        ele = RedisModule_ListPop(srckey,REDISMODULE_LIST_TAIL);\n        if (ele == NULL) break;\n        RedisModule_ListPush(dstkey,REDISMODULE_LIST_HEAD,ele);\n    }\n\n    size_t len = RedisModule_ValueLength(srckey);\n    RedisModule_ReplyWithLongLong(ctx,len);\n    return REDISMODULE_OK;\n}\n\n/* HELLO.RAND.ARRAY <count>\n * Shows how to generate arrays as commands replies.\n * It just outputs <count> random numbers. */\nint HelloRandArray_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    if (argc != 2) return RedisModule_WrongArity(ctx);\n    long long count;\n    if (RedisModule_StringToLongLong(argv[1],&count) != REDISMODULE_OK ||\n        count < 0)\n        return RedisModule_ReplyWithError(ctx,\"ERR invalid count\");\n\n    /* To reply with an array, we call RedisModule_ReplyWithArray() followed\n     * by other \"count\" calls to other reply functions in order to generate\n     * the elements of the array. */\n    RedisModule_ReplyWithArray(ctx,count);\n    while(count--) RedisModule_ReplyWithLongLong(ctx,rand());\n    return REDISMODULE_OK;\n}\n\n/* This is a simple command to test replication. Because of the \"!\" modified\n * in the RedisModule_Call() call, the two INCRs get replicated.\n * Also note how the ECHO is replicated in an unexpected position (check\n * comments the function implementation). */\nint HelloRepl1_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)\n{\n    REDISMODULE_NOT_USED(argv);\n    REDISMODULE_NOT_USED(argc);\n    RedisModule_AutoMemory(ctx);\n\n    /* This will be replicated *after* the two INCR statements, since\n     * the Call() replication has precedence, so the actual replication\n     * stream will be:\n     *\n     * MULTI\n     * INCR foo\n     * INCR bar\n     * ECHO c foo\n     * EXEC\n     */\n    RedisModule_Replicate(ctx,\"ECHO\",\"c\",\"foo\");\n\n    /* Using the \"!\" modifier we replicate the command if it\n     * modified the dataset in some way. */\n    RedisModule_Call(ctx,\"INCR\",\"c!\",\"foo\");\n    RedisModule_Call(ctx,\"INCR\",\"c!\",\"bar\");\n\n    RedisModule_ReplyWithLongLong(ctx,0);\n\n    return REDISMODULE_OK;\n}\n\n/* Another command to show replication. In this case, we call\n * RedisModule_ReplicateVerbatim() to mean we want just the command to be\n * propagated to slaves / AOF exactly as it was called by the user.\n *\n * This command also shows how to work with string objects.\n * It takes a list, and increments all the elements (that must have\n * a numerical value) by 1, returning the sum of all the elements\n * as reply.\n *\n * Usage: HELLO.REPL2 <list-key> */\nint HelloRepl2_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    if (argc != 2) return RedisModule_WrongArity(ctx);\n\n    RedisModule_AutoMemory(ctx); /* Use automatic memory management. */\n    RedisModuleKey *key = RedisModule_OpenKey(ctx,argv[1],\n        REDISMODULE_READ|REDISMODULE_WRITE);\n\n    if (RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_LIST)\n        return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE);\n\n    size_t listlen = RedisModule_ValueLength(key);\n    long long sum = 0;\n\n    /* Rotate and increment. */\n    while(listlen--) {\n        RedisModuleString *ele = RedisModule_ListPop(key,REDISMODULE_LIST_TAIL);\n        long long val;\n        if (RedisModule_StringToLongLong(ele,&val) != REDISMODULE_OK) val = 0;\n        val++;\n        sum += val;\n        RedisModuleString *newele = RedisModule_CreateStringFromLongLong(ctx,val);\n        RedisModule_ListPush(key,REDISMODULE_LIST_HEAD,newele);\n    }\n    RedisModule_ReplyWithLongLong(ctx,sum);\n    RedisModule_ReplicateVerbatim(ctx);\n    return REDISMODULE_OK;\n}\n\n/* This is an example of strings DMA access. Given a key containing a string\n * it toggles the case of each character from lower to upper case or the\n * other way around.\n *\n * No automatic memory management is used in this example (for the sake\n * of variety).\n *\n * HELLO.TOGGLE.CASE key */\nint HelloToggleCase_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    if (argc != 2) return RedisModule_WrongArity(ctx);\n\n    RedisModuleKey *key = RedisModule_OpenKey(ctx,argv[1],\n        REDISMODULE_READ|REDISMODULE_WRITE);\n\n    int keytype = RedisModule_KeyType(key);\n    if (keytype != REDISMODULE_KEYTYPE_STRING &&\n        keytype != REDISMODULE_KEYTYPE_EMPTY)\n    {\n        RedisModule_CloseKey(key);\n        return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE);\n    }\n\n    if (keytype == REDISMODULE_KEYTYPE_STRING) {\n        size_t len, j;\n        char *s = RedisModule_StringDMA(key,&len,REDISMODULE_WRITE);\n        for (j = 0; j < len; j++) {\n            if (isupper(s[j])) {\n                s[j] = tolower(s[j]);\n            } else {\n                s[j] = toupper(s[j]);\n            }\n        }\n    }\n\n    RedisModule_CloseKey(key);\n    RedisModule_ReplyWithSimpleString(ctx,\"OK\");\n    RedisModule_ReplicateVerbatim(ctx);\n    return REDISMODULE_OK;\n}\n\n/* HELLO.MORE.EXPIRE key milliseconds.\n *\n * If the key has already an associated TTL, extends it by \"milliseconds\"\n * milliseconds. Otherwise no operation is performed. */\nint HelloMoreExpire_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    RedisModule_AutoMemory(ctx); /* Use automatic memory management. */\n    if (argc != 3) return RedisModule_WrongArity(ctx);\n\n    mstime_t addms, expire;\n\n    if (RedisModule_StringToLongLong(argv[2],&addms) != REDISMODULE_OK)\n        return RedisModule_ReplyWithError(ctx,\"ERR invalid expire time\");\n\n    RedisModuleKey *key = RedisModule_OpenKey(ctx,argv[1],\n        REDISMODULE_READ|REDISMODULE_WRITE);\n    expire = RedisModule_GetExpire(key);\n    if (expire != REDISMODULE_NO_EXPIRE) {\n        expire += addms;\n        RedisModule_SetExpire(key,expire);\n    }\n    return RedisModule_ReplyWithSimpleString(ctx,\"OK\");\n}\n\n/* HELLO.ZSUMRANGE key startscore endscore\n * Return the sum of all the scores elements between startscore and endscore.\n *\n * The computation is performed two times, one time from start to end and\n * another time backward. The two scores, returned as a two element array,\n * should match.*/\nint HelloZsumRange_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    double score_start, score_end;\n    if (argc != 4) return RedisModule_WrongArity(ctx);\n\n    if (RedisModule_StringToDouble(argv[2],&score_start) != REDISMODULE_OK ||\n        RedisModule_StringToDouble(argv[3],&score_end) != REDISMODULE_OK)\n    {\n        return RedisModule_ReplyWithError(ctx,\"ERR invalid range\");\n    }\n\n    RedisModuleKey *key = RedisModule_OpenKey(ctx,argv[1],\n        REDISMODULE_READ|REDISMODULE_WRITE);\n    if (RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_ZSET) {\n        return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE);\n    }\n\n    double scoresum_a = 0;\n    double scoresum_b = 0;\n\n    RedisModule_ZsetFirstInScoreRange(key,score_start,score_end,0,0);\n    while(!RedisModule_ZsetRangeEndReached(key)) {\n        double score;\n        RedisModuleString *ele = RedisModule_ZsetRangeCurrentElement(key,&score);\n        RedisModule_FreeString(ctx,ele);\n        scoresum_a += score;\n        RedisModule_ZsetRangeNext(key);\n    }\n    RedisModule_ZsetRangeStop(key);\n\n    RedisModule_ZsetLastInScoreRange(key,score_start,score_end,0,0);\n    while(!RedisModule_ZsetRangeEndReached(key)) {\n        double score;\n        RedisModuleString *ele = RedisModule_ZsetRangeCurrentElement(key,&score);\n        RedisModule_FreeString(ctx,ele);\n        scoresum_b += score;\n        RedisModule_ZsetRangePrev(key);\n    }\n\n    RedisModule_ZsetRangeStop(key);\n\n    RedisModule_CloseKey(key);\n\n    RedisModule_ReplyWithArray(ctx,2);\n    RedisModule_ReplyWithDouble(ctx,scoresum_a);\n    RedisModule_ReplyWithDouble(ctx,scoresum_b);\n    return REDISMODULE_OK;\n}\n\n/* HELLO.LEXRANGE key min_lex max_lex min_age max_age\n * This command expects a sorted set stored at key in the following form:\n * - All the elements have score 0.\n * - Elements are pairs of \"<name>:<age>\", for example \"Anna:52\".\n * The command will return all the sorted set items that are lexicographically\n * between the specified range (using the same format as ZRANGEBYLEX)\n * and having an age between min_age and max_age. */\nint HelloLexRange_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    RedisModule_AutoMemory(ctx); /* Use automatic memory management. */\n\n    if (argc != 6) return RedisModule_WrongArity(ctx);\n\n    RedisModuleKey *key = RedisModule_OpenKey(ctx,argv[1],\n        REDISMODULE_READ|REDISMODULE_WRITE);\n    if (RedisModule_KeyType(key) != REDISMODULE_KEYTYPE_ZSET) {\n        return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE);\n    }\n\n    if (RedisModule_ZsetFirstInLexRange(key,argv[2],argv[3]) != REDISMODULE_OK) {\n        return RedisModule_ReplyWithError(ctx,\"invalid range\");\n    }\n\n    int arraylen = 0;\n    RedisModule_ReplyWithArray(ctx,REDISMODULE_POSTPONED_ARRAY_LEN);\n    while(!RedisModule_ZsetRangeEndReached(key)) {\n        double score;\n        RedisModuleString *ele = RedisModule_ZsetRangeCurrentElement(key,&score);\n        RedisModule_ReplyWithString(ctx,ele);\n        RedisModule_FreeString(ctx,ele);\n        RedisModule_ZsetRangeNext(key);\n        arraylen++;\n    }\n    RedisModule_ZsetRangeStop(key);\n    RedisModule_ReplySetArrayLength(ctx,arraylen);\n    RedisModule_CloseKey(key);\n    return REDISMODULE_OK;\n}\n\n/* HELLO.HCOPY key srcfield dstfield\n * This is just an example command that sets the hash field dstfield to the\n * same value of srcfield. If srcfield does not exist no operation is\n * performed.\n *\n * The command returns 1 if the copy is performed (srcfield exists) otherwise\n * 0 is returned. */\nint HelloHCopy_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    RedisModule_AutoMemory(ctx); /* Use automatic memory management. */\n\n    if (argc != 4) return RedisModule_WrongArity(ctx);\n    RedisModuleKey *key = RedisModule_OpenKey(ctx,argv[1],\n        REDISMODULE_READ|REDISMODULE_WRITE);\n    int type = RedisModule_KeyType(key);\n    if (type != REDISMODULE_KEYTYPE_HASH &&\n        type != REDISMODULE_KEYTYPE_EMPTY)\n    {\n        return RedisModule_ReplyWithError(ctx,REDISMODULE_ERRORMSG_WRONGTYPE);\n    }\n\n    /* Get the old field value. */\n    RedisModuleString *oldval;\n    RedisModule_HashGet(key,REDISMODULE_HASH_NONE,argv[2],&oldval,NULL);\n    if (oldval) {\n        RedisModule_HashSet(key,REDISMODULE_HASH_NONE,argv[3],oldval,NULL);\n    }\n    RedisModule_ReplyWithLongLong(ctx,oldval != NULL);\n    return REDISMODULE_OK;\n}\n\n/* HELLO.LEFTPAD str len ch\n * This is an implementation of the infamous LEFTPAD function, that\n * was at the center of an issue with the npm modules system in March 2016.\n *\n * LEFTPAD is a good example of using a Redis Modules API called\n * \"pool allocator\", that was a famous way to allocate memory in yet another\n * open source project, the Apache web server.\n *\n * The concept is very simple: there is memory that is useful to allocate\n * only in the context of serving a request, and must be freed anyway when\n * the callback implementing the command returns. So in that case the module\n * does not need to retain a reference to these allocations, it is just\n * required to free the memory before returning. When this is the case the\n * module can call RedisModule_PoolAlloc() instead, that works like malloc()\n * but will automatically free the memory when the module callback returns.\n *\n * Note that PoolAlloc() does not necessarily require AutoMemory to be\n * active. */\nint HelloLeftPad_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    RedisModule_AutoMemory(ctx); /* Use automatic memory management. */\n    long long padlen;\n\n    if (argc != 4) return RedisModule_WrongArity(ctx);\n\n    if ((RedisModule_StringToLongLong(argv[2],&padlen) != REDISMODULE_OK) ||\n        (padlen< 0)) {\n        return RedisModule_ReplyWithError(ctx,\"ERR invalid padding length\");\n    }\n    size_t strlen, chlen;\n    const char *str = RedisModule_StringPtrLen(argv[1], &strlen);\n    const char *ch = RedisModule_StringPtrLen(argv[3], &chlen);\n\n    /* If the string is already larger than the target len, just return\n     * the string itself. */\n    if (strlen >= (size_t)padlen)\n        return RedisModule_ReplyWithString(ctx,argv[1]);\n\n    /* Padding must be a single character in this simple implementation. */\n    if (chlen != 1)\n        return RedisModule_ReplyWithError(ctx,\n            \"ERR padding must be a single char\");\n\n    /* Here we use our pool allocator, for our throw-away allocation. */\n    padlen -= strlen;\n    char *buf = RedisModule_PoolAlloc(ctx,padlen+strlen);\n    for (long long j = 0; j < padlen; j++) buf[j] = *ch;\n    memcpy(buf+padlen,str,strlen);\n\n    RedisModule_ReplyWithStringBuffer(ctx,buf,padlen+strlen);\n    return REDISMODULE_OK;\n}\n\n/* This function must be present on each Redis module. It is used in order to\n * register the commands into the Redis server. */\nint RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    if (RedisModule_Init(ctx,\"helloworld\",1,REDISMODULE_APIVER_1)\n        == REDISMODULE_ERR) return REDISMODULE_ERR;\n\n    /* Log the list of parameters passing loading the module. */\n    for (int j = 0; j < argc; j++) {\n        const char *s = RedisModule_StringPtrLen(argv[j],NULL);\n        printf(\"Module loaded with ARGV[%d] = %s\\n\", j, s);\n    }\n\n    if (RedisModule_CreateCommand(ctx,\"hello.simple\",\n        HelloSimple_RedisCommand,\"readonly\",0,0,0) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"hello.push.native\",\n        HelloPushNative_RedisCommand,\"write deny-oom\",1,1,1) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"hello.push.call\",\n        HelloPushCall_RedisCommand,\"write deny-oom\",1,1,1) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"hello.push.call2\",\n        HelloPushCall2_RedisCommand,\"write deny-oom\",1,1,1) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"hello.list.sum.len\",\n        HelloListSumLen_RedisCommand,\"readonly\",1,1,1) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"hello.list.splice\",\n        HelloListSplice_RedisCommand,\"write deny-oom\",1,2,1) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"hello.list.splice.auto\",\n        HelloListSpliceAuto_RedisCommand,\n        \"write deny-oom\",1,2,1) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"hello.rand.array\",\n        HelloRandArray_RedisCommand,\"readonly\",0,0,0) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"hello.repl1\",\n        HelloRepl1_RedisCommand,\"write\",0,0,0) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"hello.repl2\",\n        HelloRepl2_RedisCommand,\"write\",1,1,1) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"hello.toggle.case\",\n        HelloToggleCase_RedisCommand,\"write\",1,1,1) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"hello.more.expire\",\n        HelloMoreExpire_RedisCommand,\"write\",1,1,1) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"hello.zsumrange\",\n        HelloZsumRange_RedisCommand,\"readonly\",1,1,1) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"hello.lexrange\",\n        HelloLexRange_RedisCommand,\"readonly\",1,1,1) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"hello.hcopy\",\n        HelloHCopy_RedisCommand,\"write deny-oom\",1,1,1) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    if (RedisModule_CreateCommand(ctx,\"hello.leftpad\",\n        HelloLeftPad_RedisCommand,\"\",1,1,1) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    return REDISMODULE_OK;\n}\n"
  },
  {
    "path": "src/modules/keydb_modstatsd/Makefile",
    "content": "MODULE_FLAGS := -fPIC -O2 -Wall -Werror\n\nOBJECT_FILES := modmain.o\nMODSNAP_CXX_FLAGS := -std=gnu++14\n\n%.o: %.cpp\n\t$(CXX) -o $@ -c $< $(MODULE_FLAGS) -I../../../deps/cpp-statsd-client/include $(MODSNAP_CXX_FLAGS) -g\n\nmodstatsd.so: $(OBJECT_FILES)\n\t$(CXX) -shared $(OBJECT_FILES) -o modstatsd.so\n\nclean:\n\trm -f $(OBJECT_FILES) modstatsd.so\n"
  },
  {
    "path": "src/modules/keydb_modstatsd/modmain.cpp",
    "content": "#include \"redismodule.h\"\n#include <stdlib.h>\n#include <cpp-statsd-client/StatsdClient.hpp>\n#include <unordered_map>\n#include <sys/utsname.h>\n#include <algorithm>\n#ifdef __linux__\n#include <sys/sysinfo.h>\n#include <sys/resource.h>\n#endif\n#include <inttypes.h>\n#include <sys/time.h>\n#include <regex>\n#include <sstream>\n#include <iostream>\n#include <fstream>\n\nusing namespace Statsd;\n\nclass StatsdClientWrapper \n{\nprivate:\n    StatsdClient *m_stats;\n    StatsdClient *m_stats_noprefix;\n\npublic:\n    StatsdClientWrapper(const std::string& host,\n                        const uint16_t port,\n                        const std::string& prefix,\n                        const uint64_t batchsize,\n                        const uint64_t sendInterval) {\n        m_stats = new StatsdClient(host, port, prefix, batchsize, sendInterval);\n        m_stats_noprefix = new StatsdClient(host, port, \"keydb\", batchsize, sendInterval);\n    }\n\n    ~StatsdClientWrapper() {\n        delete m_stats;\n        delete m_stats_noprefix;\n    }\n\n    void increment(const std::string& key, const bool prefixOnly = true) {\n        m_stats->increment(key);\n        if (!prefixOnly) m_stats_noprefix->increment(key);\n    }\n\n    void decrement(const std::string& key, const bool prefixOnly = true) {\n        m_stats->decrement(key);\n        if (!prefixOnly) m_stats_noprefix->decrement(key);\n    }\n\n    void count(const std::string& key, const int delta, const bool prefixOnly = true) {\n        m_stats->count(key, delta);\n        if (!prefixOnly) m_stats_noprefix->count(key, delta);\n    }\n\n    template <typename T>\n    void gauge(const std::string& key, const T value, const bool prefixOnly = true) {\n        m_stats->gauge(key, value);\n        if (!prefixOnly) m_stats_noprefix->gauge(key, value);\n    }\n\n    void timing(const std::string& key, const unsigned int ms, const bool prefixOnly = true) {\n        m_stats->timing(key, ms);\n        if (!prefixOnly) m_stats_noprefix->timing(key, ms);\n    }\n};\n\n/* constants */\nstatic time_t c_infoUpdateSeconds = 10;\n// the current Redis Cluster setup we configure replication factor as 2, each non-empty master node should have 2 replicas, given that there are 3 zones in each regions\nstatic const int EXPECTED_NUMBER_OF_REPLICAS = 2;\n\nStatsdClientWrapper *g_stats = nullptr;\nstd::string m_strPrefix { \"keydb\" };\n\nconst std::regex g_replica_or_db_info_regex { \"^(slave|db)(\\\\d+)\" };\nconst char *g_string_counter_separator = \"__\";\nconst uint64_t g_stats_buffer_size_bytes = 1600;\nstd::string nodeName;\nint unameResult;\n\nenum class StatsD_Type {\n    STATSD_GAUGE_LONGLONG,\n    STATSD_GAUGE_FLOAT,\n    STATSD_GAUGE_BYTES,\n    STATSD_DELTA,\n    STATSD_COUNTER_STRING\n};\n\nstruct StatsRecord {\n    StatsD_Type type;\n    bool prefixOnly = true;\n    const char *szAlternate = nullptr;\n\n    /* Dynamic Values */\n    long long prevVal = 0;\n};\n\nstd::unordered_map<std::string, StatsRecord> g_mapInfoFields = {\n    // info\n    { \"used_memory\", { StatsD_Type::STATSD_GAUGE_BYTES, false /* prefixOnly */}},\n    { \"used_memory_rss\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"maxmemory\", { StatsD_Type::STATSD_GAUGE_BYTES, false /* prefixOnly */}},\n    { \"used_memory_dataset_perc\", { StatsD_Type::STATSD_GAUGE_FLOAT }},\n    { \"avg_lock_contention\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"repl_backlog_size\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"connected_slaves\", { StatsD_Type::STATSD_GAUGE_LONGLONG, true, \"connected_replicas\" }},\n    { \"errorstat_ERR\", { StatsD_Type::STATSD_DELTA }},\n    { \"connected_clients\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"cluster_connections\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"instantaneous_ops_per_sec\", { StatsD_Type::STATSD_GAUGE_LONGLONG, false /* prefixOnly */}},\n    { \"instantaneous_input_kbps\", { StatsD_Type::STATSD_GAUGE_FLOAT, false /* prefixOnly */}},\n    { \"instantaneous_output_kbps\", { StatsD_Type::STATSD_GAUGE_FLOAT, false /* prefixOnly */}},\n    { \"server_threads\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"mvcc_depth\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"sync_full\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"sync_partial_ok\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"sync_partial_err\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"rdb_bgsave_in_progress\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"rdb_last_bgsave_time_sec\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"used_memory_overhead\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"master_sync_in_progress\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"uptime_in_seconds\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"hz\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"configured_hz\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"maxclients\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"client_recent_max_input_buffer\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"client_recent_max_output_buffer\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"blocked_clients\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"tracking_clients\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"clients_in_timeout_table\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"used_memory_peak\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"used_memory_startup\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"used_memory_dataset\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"allocator_allocated\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"allocator_active\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"allocator_resident\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"total_system_memory\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"used_memory_lua\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"used_memory_scripts\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"number_of_cached_scripts\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"allocator_frag_ratio\", { StatsD_Type::STATSD_GAUGE_FLOAT }},\n    { \"allocator_frag_bytes\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"allocator_rss_ratio\", { StatsD_Type::STATSD_GAUGE_FLOAT }},\n    { \"allocator_rss_bytes\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"rss_overhead_ratio\", { StatsD_Type::STATSD_GAUGE_FLOAT }},\n    { \"rss_overhead_bytes\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"mem_fragmentation_ratio\", { StatsD_Type::STATSD_GAUGE_FLOAT }},\n    { \"mem_fragmentation_bytes\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"mem_not_counted_for_evict\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"mem_replication_backlog\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"mem_clients_slaves\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"mem_clients_normal\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"mem_aof_buffer\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"active_defrag_running\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"lazyfree_pending_objects\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"lazyfreed_objects\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"loading\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"current_cow_peak\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"current_cow_size\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"current_cow_size_age\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"current_fork_perc\", { StatsD_Type::STATSD_GAUGE_FLOAT }},\n    { \"current_save_keys_processed\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"current_save_keys_total\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"rdb_changes_since_last_save\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"rdb_last_save_time\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"rdb_last_cow_size\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"aof_enabled\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"aof_rewrite_in_progress\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"aof_rewrite_scheduled\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"aof_last_cow_size\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"module_fork_in_progress\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"module_fork_last_cow_size\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"aof_current_size\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"aof_base_size\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"aof_pending_rewrite\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"aof_buffer_length\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"aof_rewrite_buffer_length\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"aof_pending_bio_fsync\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"aof_delayed_fsync\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"total_connections_received\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"total_commands_processed\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"total_net_input_bytes\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"total_net_output_bytes\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"rejected_connections\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"expired_keys\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"expired_stale_perc\", { StatsD_Type::STATSD_GAUGE_FLOAT }},\n    { \"expired_time_cap_reached_count\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"expire_cycle_cpu_milliseconds\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"evicted_keys\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"keyspace_hits\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"keyspace_misses\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"pubsub_channels\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"pubsub_patterns\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"latest_fork_usec\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"total_forks\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"migrate_cached_sockets\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"slave_expires_tracked_keys\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"active_defrag_hits\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"active_defrag_misses\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"active_defrag_key_hits\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"active_defrag_key_misses\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"tracking_total_keys\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"tracking_total_items\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"tracking_total_prefixes\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"unexpected_error_replies\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"total_error_replies\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"dump_payload_sanitizations\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"total_reads_processed\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"total_writes_processed\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"instantaneous_lock_contention\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"avg_lock_contention\", { StatsD_Type::STATSD_GAUGE_FLOAT }},\n    { \"storage_provider_read_hits\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"storage_provider_read_misses\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"repl_backlog_active\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"repl_backlog_size\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"repl_backlog_first_byte_offset\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"repl_backlog_histlen\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"used_cpu_sys\", { StatsD_Type::STATSD_GAUGE_FLOAT }},\n    { \"used_cpu_user\", { StatsD_Type::STATSD_GAUGE_FLOAT }},\n    { \"used_cpu_sys_children\", { StatsD_Type::STATSD_GAUGE_FLOAT }},\n    { \"used_cpu_user_children\", { StatsD_Type::STATSD_GAUGE_FLOAT }},\n    { \"used_cpu_user_children\", { StatsD_Type::STATSD_GAUGE_FLOAT }},\n    { \"long_lock_waits\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"used_cpu_sys_main_thread\", { StatsD_Type::STATSD_GAUGE_FLOAT }},\n    { \"used_cpu_user_main_thread\", { StatsD_Type::STATSD_GAUGE_FLOAT }},\n    { \"master_sync_total_bytes\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"master_sync_read_bytes\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"master_sync_last_io_seconds_ago\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"master_link_down_since_seconds\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"maxmemory_policy\", { StatsD_Type::STATSD_COUNTER_STRING }},\n    { \"role\", { StatsD_Type::STATSD_COUNTER_STRING }},\n    { \"master_global_link_status\", { StatsD_Type::STATSD_COUNTER_STRING }},\n    { \"master_link_status\", { StatsD_Type::STATSD_COUNTER_STRING }},\n    { \"master_failover_state\", { StatsD_Type::STATSD_COUNTER_STRING }},\n    { \"rdb_last_bgsave_status\", { StatsD_Type::STATSD_COUNTER_STRING }},\n    { \"rdb_saves\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"instantaneous_input_repl_kbps\", { StatsD_Type::STATSD_GAUGE_FLOAT }},\n    { \"instantaneous_output_repl_kbps\", { StatsD_Type::STATSD_GAUGE_FLOAT }},\n    { \"master_host\", { StatsD_Type::STATSD_COUNTER_STRING }},\n    { \"master_repl_offset\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"second_repl_offset\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"slave_repl_offset\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"redis_version\", { StatsD_Type::STATSD_COUNTER_STRING }},\n    { \"redis_git_sha1\", { StatsD_Type::STATSD_COUNTER_STRING }},\n    // cluster info\n    { \"cluster_state\", { StatsD_Type::STATSD_COUNTER_STRING }},\n    { \"cluster_slots_assigned\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"cluster_slots_ok\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"cluster_slots_pfail\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"cluster_slots_fail\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"cluster_known_nodes\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"cluster_size\", { StatsD_Type::STATSD_GAUGE_LONGLONG }},\n    { \"storage_flash_available_bytes\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n    { \"storage_flash_total_bytes\", { StatsD_Type::STATSD_GAUGE_BYTES }},\n};\n\n/* Globals */\nstatic uint64_t g_cclients = 0;\n\nlong long ustime(void) {\n    struct timeval tv;\n    long long ust;\n\n    gettimeofday(&tv, NULL);\n    ust = ((long long)tv.tv_sec)*1000000;\n    ust += tv.tv_usec;\n    return ust;\n}\n\nvoid event_client_change_handler(struct RedisModuleCtx *ctx, RedisModuleEvent eid, uint64_t subevent, void *data) {\n    if (eid.id != REDISMODULE_EVENT_CLIENT_CHANGE)\n        return;\n\n    uint64_t clientsStart = g_cclients;\n    switch (subevent) {\n        case REDISMODULE_SUBEVENT_CLIENT_CHANGE_CONNECTED:\n            ++g_cclients;\n            g_stats->increment(\"clients_added\");\n            RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, \"Emitting metric increment for \\\"clients_added\\\"\");\n            break;\n\n        case REDISMODULE_SUBEVENT_CLIENT_CHANGE_DISCONNECTED:\n            --g_cclients;\n            g_stats->increment(\"clients_disconnected\");\n            RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, \"Emitting metric increment for \\\"clients_disconnected\\\"\");\n            break;\n    }\n\n    if (g_cclients != clientsStart) {\n        g_stats->gauge(\"clients\", g_cclients);\n        RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, \"Emitting metric \\\"clients\\\": %\" PRIu64, g_cclients);\n    }\n}\n\nvoid handleStatItem(struct RedisModuleCtx *ctx, std::string name, StatsRecord &record, const char *pchValue) {\n    switch (record.type) {\n        case StatsD_Type::STATSD_GAUGE_LONGLONG: {\n            long long val = strtoll(pchValue, nullptr, 10);\n            g_stats->gauge(name, val, record.prefixOnly);\n            RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, \"Emitting metric \\\"%s\\\": %lld\", name.c_str(), val);\n            break;\n        }\n\n        case StatsD_Type::STATSD_GAUGE_FLOAT: {\n            double val = strtod(pchValue, nullptr);\n            g_stats->gauge(name, val, record.prefixOnly);\n            RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, \"Emitting metric \\\"%s\\\": %f\", name.c_str(), val);\n            break;\n        }\n\n        case StatsD_Type::STATSD_GAUGE_BYTES: {\n            long long val = strtoll(pchValue, nullptr, 10);\n            g_stats->gauge(name + \"_MB\", val / 1024/1024, record.prefixOnly);\n            RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, \"Emitting metric \\\"%s\\\": %llu\", (name + \"_MB\").c_str(), val / 1024/1024);\n            break;\n        }\n\n        case StatsD_Type::STATSD_DELTA: {\n            long long val = strtoll(pchValue, nullptr, 10);\n            g_stats->count(name, val - record.prevVal, record.prefixOnly);\n            RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, \"Emitting metric count for \\\"%s\\\": %lld\", name.c_str() , val - record.prevVal);\n            record.prevVal = val;\n            break;\n        }\n\n        case StatsD_Type::STATSD_COUNTER_STRING: {\n            // parse val string\n            const char *pNewLine = strchr(pchValue, '\\r');\n            if (pNewLine == nullptr) {\n                pNewLine = strchr(pchValue, '\\n');\n            }\n            if (pNewLine == nullptr) {\n                g_stats->increment(\"STATSD_COUNTER_STRING_failed\", 1);\n                return;\n            }\n            std::string val(pchValue, pNewLine - pchValue);\n            std::replace(val.begin(), val.end(), '.', '-');\n            // metrics emit\n            std::string metricsName = name + g_string_counter_separator + val;\n            g_stats->increment(metricsName, 1);\n            RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, \"Emitting metric \\\"%s\\\"\", metricsName.c_str());\n            break;\n        }\n\n        default:\n            RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_WARNING, \"Unknown stats record type for the key \\\"%s\\\": %u\", name.c_str(), (unsigned)record.type);\n            break;\n    }\n}\n\nvoid handleErrorStatItem(struct RedisModuleCtx *ctx, std::string name, std::string rest) {\n    size_t idx = rest.find('=');\n    if (idx != std::string::npos) {\n        std::string statValue = rest.substr(idx + 1);\n        long long val = strtoll(statValue.c_str(), nullptr, 10);\n        g_stats->gauge(name, val);\n        RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, \"Emitting metric \\\"%s\\\": %lld\", name.c_str(), val);\n    } else {\n        RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_WARNING, \"Unexpected errorstat line format returned by \\\"INFO\\\" command: \\\"%s\\\"\", (name + rest).c_str());\n    }\n}\n\nvoid handleReplicaOrDbInfoItem(struct RedisModuleCtx *ctx, std::string name, std::string rest) {\n    //use a stringstream to extract each metric of the form <name>=<value>\n    std::stringstream metrics(rest);\n    while (metrics.good()) {\n        std::string metric;\n        std::getline(metrics, metric, ',');\n        size_t idx = metric.find('=');\n        if (idx != std::string::npos) {\n            std::string stat = metric.substr(0, idx);\n            std::string statName = name + '-' + stat;\n            //idx + 1 to ignore the = sign\n            std::string statValue = metric.substr(idx + 1);\n            // string values\n            if (stat == \"ip\" || stat == \"state\") {\n                std::replace(statValue.begin(), statValue.end(), '.', '-');\n                statName += g_string_counter_separator + statValue;\n                g_stats->increment(statName, 1);\n                RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, \"Emitting metric \\\"%s\\\"\", statName.c_str());\n            } else {\n                long long val = strtoll(statValue.c_str(), nullptr, 10);\n                g_stats->gauge(statName, val);\n                RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, \"Emitting metric \\\"%s\\\": %lld\", statName.c_str(), val);\n            }\n        }\n    }\n}\n\nvoid handle_info_response(struct RedisModuleCtx *ctx, const char *szReply, size_t len, const char *command) {\n    \n    #define SAFETY_CHECK_POINTER(_p) ((_p) < (szReply + len))\n\n    // Parse the INFO reply string line by line\n    const char *pchLineStart = szReply;\n\n    while (SAFETY_CHECK_POINTER(pchLineStart) && *pchLineStart != '\\0') {\n        // Loop Each Line\n        const char *pchColon = pchLineStart;\n        while (SAFETY_CHECK_POINTER(pchColon) && *pchColon != ':' && *pchColon != '\\r') {\n            ++pchColon;\n        }\n        if (!SAFETY_CHECK_POINTER(pchColon)) {\n            RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_WARNING, \"Unexpected line termination when parsing response from %s command: \\\"%s\\\"\", command, pchLineStart);\n            break; // BUG\n        }\n        const char *pchLineEnd = pchColon;\n        while (SAFETY_CHECK_POINTER(pchLineEnd) && *pchLineEnd != '\\n')\n            ++pchLineEnd;\n        \n        std::string strCheck(pchLineStart, pchColon - pchLineStart);\n        if (strCheck.find(\"errorstat_\") != std::string::npos) {\n            std::string remainder(pchColon + 1, pchLineEnd - (pchColon + 1));\n            handleErrorStatItem(ctx, strCheck, remainder);\n        } else if (std::regex_match(strCheck, g_replica_or_db_info_regex)) {\n            std::string remainder(pchColon + 1, pchLineEnd - (pchColon + 1));\n            handleReplicaOrDbInfoItem(ctx, strCheck, remainder);\n        } else {\n            auto itr = g_mapInfoFields.find(strCheck);\n            if (itr != g_mapInfoFields.end()) {\n                // This is an info field we care about\n                if (itr->second.szAlternate != nullptr)\n                    strCheck = itr->second.szAlternate;\n                handleStatItem(ctx, strCheck, itr->second, pchColon+1);\n            }\n        }\n\n        RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, \"INFO response line: \\\"%s\\\"\", std::string(pchLineStart, pchLineEnd - pchLineStart).c_str());\n        pchLineStart = pchLineEnd + 1;    // start of next line, if we're over the loop will catch it\n    }\n\n    #undef SAFETY_CHECK_POINTER\n}\n\nvoid handle_cluster_nodes_response(struct RedisModuleCtx *ctx, const char *szReply, size_t len) {\n    #define SAFETY_CHECK_POINTER(_p) ((_p) < (szReply + len))\n    const char *pchLineStart = szReply;\n    long long primaries = 0;\n    long long replicas = 0;\n    while (SAFETY_CHECK_POINTER(pchLineStart) && *pchLineStart != '\\0') {\n        // Loop Each Line\n        const char *pchLineEnd = pchLineStart;\n        while (SAFETY_CHECK_POINTER(pchLineEnd) && (*pchLineEnd != '\\r') && (*pchLineEnd != '\\n')) {\n            ++pchLineEnd;\n        }\n        std::string line(pchLineStart, pchLineEnd - pchLineStart);\n        RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, \"Cluster Nodes Line: \\\"%s\\\"\", line.c_str());\n        if (std::string::npos != line.find(\"master\")) {\n            ++primaries;\n        } else if (std::string::npos != line.find(\"slave\")) {\n            ++replicas;\n        } else {\n            RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_WARNING, \"Unexpected NODE format returned by \\\"CLUSTER NODES\\\" command: \\\"%s\\\"\", line.c_str());\n        }\n        // emit myself stat\n        if (line.find(\"myself\") != std::string::npos) {\n            size_t firstSpaceIdx = line.find(' ');\n            // emit cluster node id\n            if (firstSpaceIdx != std::string::npos) {\n                std::string nodeIdStat = \"cluster_node_id\";\n                nodeIdStat += g_string_counter_separator + line.substr(0, firstSpaceIdx);\n                g_stats->increment(nodeIdStat);\n            }\n            // emit node ip\n            size_t firstColonIdx = line.find(':');\n            if (firstColonIdx != std::string::npos) {\n                std::string nodeIpStat = \"cluster_node_ip\";\n                std::string nodeIP = line.substr(firstSpaceIdx+1, firstColonIdx-firstSpaceIdx-1);\n                std::replace(nodeIP.begin(), nodeIP.end(), '.', '-');\n                nodeIpStat += g_string_counter_separator + nodeIP;\n                g_stats->increment(nodeIpStat);\n            }\n        }\n        pchLineStart = pchLineEnd;\n        while (SAFETY_CHECK_POINTER(pchLineStart) && ((*pchLineStart == '\\r') || (*pchLineStart == '\\n'))) {\n            ++pchLineStart;\n        }\n    }\n    g_stats->gauge(\"primaries\", primaries);\n    RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, \"Emitting metric \\\"primaries\\\": %llu\", primaries);\n    g_stats->gauge(\"replicas\", replicas);\n    RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, \"Emitting metric \\\"replicas\\\": %llu\", replicas);\n    #undef SAFETY_CHECK_POINTER\n}\n\nvoid handle_client_list_response(struct RedisModuleCtx *ctx, const char *szReply, size_t len) {\n    size_t totalClientOutputBuffer = 0;\n    size_t totalReplicaClientOutputBuffer = 0;\n    #define SAFETY_CHECK_POINTER(_p) ((_p) < (szReply + len))\n    const char *pchLineStart = szReply;\n    while (SAFETY_CHECK_POINTER(pchLineStart) && *pchLineStart != '\\0') {\n        // Loop Each Line\n        const char *pchLineEnd = pchLineStart;\n        while (SAFETY_CHECK_POINTER(pchLineEnd) && (*pchLineEnd != '\\r') && (*pchLineEnd != '\\n')) {\n            ++pchLineEnd;\n        }\n        std::string line(pchLineStart, pchLineEnd - pchLineStart);\n        RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, \"Client List Line: \\\"%s\\\"\", line.c_str());\n\n        // recover output buffer size for client\n        bool lineFailed = false;\n        bool replica = line.find(\"flags=S\") != std::string::npos;\n        size_t idx = line.find(\"omem\");\n        if (!(lineFailed = (idx == std::string::npos))) {\n            std::string rest = line.substr(idx);\n            size_t startIdx = rest.find(\"=\");\n            if (!(lineFailed = (startIdx == std::string::npos))) {\n                size_t endIdx = rest.find(\" \");\n                if (!(lineFailed = (endIdx == std::string::npos))) {\n                    // use startIdx + 1 and endIdx - 1 to exclude the '=' and ' ' characters\n                    std::string valueString = rest.substr(startIdx + 1, (endIdx - 1) - (startIdx + 1));\n                    size_t value = strtoll(valueString.c_str(), nullptr, 10);\n                    totalClientOutputBuffer += value;\n                    if (replica) {\n                        totalReplicaClientOutputBuffer += value;\n                    }\n                }\n            }\n        }\n\n        if (lineFailed) {\n            RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_WARNING, \"Unexpected CLIENT format returned by \\\"CLIENT LIST\\\" command: \\\"%s\\\"\", line.c_str());\n        }\n\n        pchLineStart = pchLineEnd;\n        while (SAFETY_CHECK_POINTER(pchLineStart) && ((*pchLineStart == '\\r') || (*pchLineStart == '\\n'))) {\n            ++pchLineStart;\n        }\n    }\n    #undef SAFETY_CHECK_POINTER\n    g_stats->gauge(\"total_client_output_buffer\", totalClientOutputBuffer);\n    g_stats->gauge(\"total_replica_client_output_buffer\", totalReplicaClientOutputBuffer);\n}\n\nvoid emit_system_free_memory() {\n    std::ifstream meminfo(\"/proc/meminfo\");\n    std::string line;\n    while (std::getline(meminfo, line)) {\n        if (line.find(\"MemAvailable:\") != std::string::npos) {\n            unsigned long memAvailableInKB;\n            std::sscanf(line.c_str(), \"MemAvailable: %lu kB\", &memAvailableInKB);\n            g_stats->gauge(\"systemAvailableMemory_MB\", memAvailableInKB / 1024);\n            return;\n        }\n    }\n}\n\nvoid emit_metrics_for_insufficient_replicas(struct RedisModuleCtx *ctx, long long keys) {\n    // non-empty\n    if (keys <= 0) {\n        return;\n    }\n    RedisModuleCallReply *reply = RedisModule_Call(ctx, \"ROLE\", \"\");\n    if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_ARRAY) {\n        RedisModule_FreeCallReply(reply);\n        return;\n    }\n    RedisModuleCallReply *roleReply = RedisModule_CallReplyArrayElement(reply, 0);\n    if (RedisModule_CallReplyType(roleReply) != REDISMODULE_REPLY_STRING) {\n        RedisModule_FreeCallReply(reply);\n        return;\n    }\n    size_t len;\n    const char *role = RedisModule_CallReplyStringPtr(roleReply, &len);\n    // check if the current node is a primary\n    if (strncmp(role, \"master\", len) == 0) {\n        RedisModuleCallReply *replicasReply = RedisModule_CallReplyArrayElement(reply, 2);\n        // check if there are less than 2 connected replicas\n        if (RedisModule_CallReplyLength(replicasReply) < EXPECTED_NUMBER_OF_REPLICAS) {\n            g_stats->increment(\"lessThanExpectedReplicas_error\", 1);\n        }\n    }\n    RedisModule_FreeCallReply(reply);\n}\n\nvoid event_cron_handler(struct RedisModuleCtx *ctx, RedisModuleEvent eid, uint64_t subevent, void *data) {\n    static time_t lastTime = 0;\n    time_t curTime = time(nullptr);\n\n    if ((curTime - lastTime) > c_infoUpdateSeconds) {\n        size_t startTime = ustime();\n\n#ifdef __linux__\n\t/* Log CPU Usage */\n\tstatic long long s_mscpuLast = 0;\n\tstruct rusage self_ru;\n\tgetrusage(RUSAGE_SELF, &self_ru);\n\n\tlong long mscpuCur = (self_ru.ru_utime.tv_sec * 1000) + (self_ru.ru_utime.tv_usec / 1000)\n\t\t\t+ (self_ru.ru_stime.tv_sec * 1000) + (self_ru.ru_stime.tv_usec / 1000);\n\n\n\tg_stats->gauge(\"cpu_load_perc\", ((double)(mscpuCur - s_mscpuLast) / ((curTime - lastTime)*1000))*100, false /* prefixOnly */);   \n\ts_mscpuLast = mscpuCur;\n#endif\n\n        /* Log clients */\n        g_stats->gauge(\"clients\", g_cclients);\n\n        /* node name */\n        if (unameResult == 0) {\n            g_stats->increment(\"node_name\" + std::string(g_string_counter_separator) + nodeName);\n        }\n\n        /* Log INFO Fields */\n        size_t commandStartTime = ustime();\n        RedisModuleCallReply *reply = RedisModule_Call(ctx, \"INFO\", \"\");\n        size_t len = 0;\n        const char *szReply = RedisModule_CallReplyStringPtr(reply, &len);\n        g_stats->timing(\"info_time_taken_us\", ustime() - commandStartTime);\n        commandStartTime = ustime();\n        handle_info_response(ctx, szReply, len, \"INFO\");\n        g_stats->timing(\"handle_info_time_taken_us\", ustime() - commandStartTime);\n        RedisModule_FreeCallReply(reply);\n\n        /* Log CLUSTER INFO Fields */\n        commandStartTime = ustime();\n        reply = RedisModule_Call(ctx, \"CLUSTER\", \"c\", \"INFO\");\n        szReply = RedisModule_CallReplyStringPtr(reply, &len);\n        g_stats->timing(\"cluster_info_time_taken_us\", ustime() - commandStartTime);\n        commandStartTime = ustime();\n        handle_info_response(ctx, szReply, len, \"CLUSTER INFO\");\n        g_stats->timing(\"handle_cluster_info_time_taken_us\", ustime() - commandStartTime);\n        RedisModule_FreeCallReply(reply);\n\n        /* Log Cluster Topology */\n        commandStartTime = ustime();\n        reply = RedisModule_Call(ctx, \"CLUSTER\", \"c\", \"NODES\");\n        szReply = RedisModule_CallReplyStringPtr(reply, &len);\n        g_stats->timing(\"cluster_nodes_time_taken_us\", ustime() - commandStartTime);\n        commandStartTime = ustime();\n        handle_cluster_nodes_response(ctx, szReply, len);\n        g_stats->timing(\"handle_cluster_nodes_time_taken_us\", ustime() - commandStartTime);\n        RedisModule_FreeCallReply(reply);\n\n        /* Log Client Info */\n        // commandStartTime = ustime();\n        // reply = RedisModule_Call(ctx, \"CLIENT\", \"c\", \"LIST\");\n        // szReply = RedisModule_CallReplyStringPtr(reply, &len);\n        // g_stats->timing(\"client_info_time_taken_us\", ustime() - commandStartTime);\n        // commandStartTime = ustime();\n        // handle_client_list_response(ctx, szReply, len);\n        // g_stats->timing(\"handle_client_info_time_taken_us\", ustime() - commandStartTime);\n        // RedisModule_FreeCallReply(reply);\n\n        commandStartTime = ustime();\n        emit_system_free_memory();\n        g_stats->timing(\"emit_free_system_memory_time_taken_us\", ustime() - commandStartTime);\n\n        /* Log Keys */\n        commandStartTime = ustime();\n        reply = RedisModule_Call(ctx, \"dbsize\", \"\");\n        long long keys = RedisModule_CallReplyInteger(reply);\n        RedisModule_FreeCallReply(reply);\n        g_stats->gauge(\"keys\", keys, false /* prefixOnly */);\n        RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, \"Emitting metric \\\"keys\\\": %llu\", keys);\n        g_stats->timing(\"emit_keys_metric_time_taken_us\", ustime() - commandStartTime);\n\n        emit_metrics_for_insufficient_replicas(ctx, keys);\n\n        g_stats->timing(\"metrics_time_taken_us\", ustime() - startTime);\n        \n\tlastTime = curTime;\n    }\n}\n\nextern \"C\" int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {\n    if (RedisModule_Init(ctx,\"statsd\",1,REDISMODULE_APIVER_1) == REDISMODULE_ERR) \n        return REDISMODULE_ERR;\n\n    RedisModule_AutoMemory(ctx);\n    /* Use pod name if available*/\n    const char *podName = getenv(\"POD_NAME\");\n    utsname sysName;\n    unameResult = uname(&sysName);\n    if (unameResult == 0) {\n        nodeName = std::string(sysName.nodename);\n        std::replace(nodeName.begin(), nodeName.end(), '.', '-');\n    }\n    if (podName != nullptr) {\n        m_strPrefix = podName;\n        std::replace(m_strPrefix.begin(), m_strPrefix.end(), '.', '-');\n    }\n    else if (unameResult == 0) {\n        m_strPrefix = nodeName;\n        unameResult = 1;\n    }\n\n    for (int iarg = 0; iarg < argc; ++iarg) {\n        size_t len = 0;\n        const char *rgchArg = RedisModule_StringPtrLen(argv[iarg], &len);\n        if (len == 6 && memcmp(rgchArg, \"prefix\", 6) == 0) {\n            if ((iarg+1) >= argc) {\n                RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_WARNING, \"Expected a value after 'prefix'\");\n                return REDISMODULE_ERR;\n            }\n            ++iarg;\n            size_t lenPrefix = 0;\n            const char *rgchPrefix = RedisModule_StringPtrLen(argv[iarg], &lenPrefix);\n            m_strPrefix = std::string(rgchPrefix, lenPrefix);\n        } else {\n            RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_WARNING, \"Unrecognized configuration flag\");\n            return REDISMODULE_ERR;\n        }\n    }\n\n    g_stats = new StatsdClientWrapper(\"localhost\", 8125, m_strPrefix, g_stats_buffer_size_bytes, c_infoUpdateSeconds * 1000);\n\n    if (RedisModule_SubscribeToServerEvent(ctx, RedisModuleEvent_ClientChange, event_client_change_handler) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n\n    if (RedisModule_SubscribeToServerEvent(ctx, RedisModuleEvent_CronLoop, event_cron_handler) == REDISMODULE_ERR)\n        return REDISMODULE_ERR;\n    \n    return REDISMODULE_OK;\n}\n\nextern \"C\" int RedisModule_OnUnload(RedisModuleCtx *ctx) {\n    delete g_stats;\n    return REDISMODULE_OK;\n}\n"
  },
  {
    "path": "src/modules/keydb_modstatsd/redismodule.h",
    "content": "#ifndef REDISMODULE_H\n#define REDISMODULE_H\n\n#include <sys/types.h>\n#include <stdint.h>\n#include <stdio.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/* ---------------- Defines common between core and modules --------------- */\n\n/* Error status return values. */\n#define REDISMODULE_OK 0\n#define REDISMODULE_ERR 1\n\n/* API versions. */\n#define REDISMODULE_APIVER_1 1\n\n/* Version of the RedisModuleTypeMethods structure. Once the RedisModuleTypeMethods \n * structure is changed, this version number needs to be changed synchronistically. */\n#define REDISMODULE_TYPE_METHOD_VERSION 3\n\n/* API flags and constants */\n#define REDISMODULE_READ (1<<0)\n#define REDISMODULE_WRITE (1<<1)\n\n/* RedisModule_OpenKey extra flags for the 'mode' argument.\n * Avoid touching the LRU/LFU of the key when opened. */\n#define REDISMODULE_OPEN_KEY_NOTOUCH (1<<16)\n\n#define REDISMODULE_LIST_HEAD 0\n#define REDISMODULE_LIST_TAIL 1\n\n/* Key types. */\n#define REDISMODULE_KEYTYPE_EMPTY 0\n#define REDISMODULE_KEYTYPE_STRING 1\n#define REDISMODULE_KEYTYPE_LIST 2\n#define REDISMODULE_KEYTYPE_HASH 3\n#define REDISMODULE_KEYTYPE_SET 4\n#define REDISMODULE_KEYTYPE_ZSET 5\n#define REDISMODULE_KEYTYPE_MODULE 6\n#define REDISMODULE_KEYTYPE_STREAM 7\n\n/* Reply types. */\n#define REDISMODULE_REPLY_UNKNOWN -1\n#define REDISMODULE_REPLY_STRING 0\n#define REDISMODULE_REPLY_ERROR 1\n#define REDISMODULE_REPLY_INTEGER 2\n#define REDISMODULE_REPLY_ARRAY 3\n#define REDISMODULE_REPLY_NULL 4\n\n/* Postponed array length. */\n#define REDISMODULE_POSTPONED_ARRAY_LEN -1\n\n/* Expire */\n#define REDISMODULE_NO_EXPIRE -1\n\n/* Sorted set API flags. */\n#define REDISMODULE_ZADD_XX      (1<<0)\n#define REDISMODULE_ZADD_NX      (1<<1)\n#define REDISMODULE_ZADD_ADDED   (1<<2)\n#define REDISMODULE_ZADD_UPDATED (1<<3)\n#define REDISMODULE_ZADD_NOP     (1<<4)\n#define REDISMODULE_ZADD_GT      (1<<5)\n#define REDISMODULE_ZADD_LT      (1<<6)\n\n/* Hash API flags. */\n#define REDISMODULE_HASH_NONE       0\n#define REDISMODULE_HASH_NX         (1<<0)\n#define REDISMODULE_HASH_XX         (1<<1)\n#define REDISMODULE_HASH_CFIELDS    (1<<2)\n#define REDISMODULE_HASH_EXISTS     (1<<3)\n#define REDISMODULE_HASH_COUNT_ALL  (1<<4)\n\n/* StreamID type. */\ntypedef struct RedisModuleStreamID {\n    uint64_t ms;\n    uint64_t seq;\n} RedisModuleStreamID;\n\n/* StreamAdd() flags. */\n#define REDISMODULE_STREAM_ADD_AUTOID (1<<0)\n/* StreamIteratorStart() flags. */\n#define REDISMODULE_STREAM_ITERATOR_EXCLUSIVE (1<<0)\n#define REDISMODULE_STREAM_ITERATOR_REVERSE (1<<1)\n/* StreamIteratorTrim*() flags. */\n#define REDISMODULE_STREAM_TRIM_APPROX (1<<0)\n\n/* Context Flags: Info about the current context returned by\n * RM_GetContextFlags(). */\n\n/* The command is running in the context of a Lua script */\n#define REDISMODULE_CTX_FLAGS_LUA (1<<0)\n/* The command is running inside a Redis transaction */\n#define REDISMODULE_CTX_FLAGS_MULTI (1<<1)\n/* The instance is a master */\n#define REDISMODULE_CTX_FLAGS_MASTER (1<<2)\n/* The instance is a replica */\n#define REDISMODULE_CTX_FLAGS_SLAVE (1<<3)\n/* The instance is read-only (usually meaning it's a replica as well) */\n#define REDISMODULE_CTX_FLAGS_READONLY (1<<4)\n/* The instance is running in cluster mode */\n#define REDISMODULE_CTX_FLAGS_CLUSTER (1<<5)\n/* The instance has AOF enabled */\n#define REDISMODULE_CTX_FLAGS_AOF (1<<6)\n/* The instance has RDB enabled */\n#define REDISMODULE_CTX_FLAGS_RDB (1<<7)\n/* The instance has Maxmemory set */\n#define REDISMODULE_CTX_FLAGS_MAXMEMORY (1<<8)\n/* Maxmemory is set and has an eviction policy that may delete keys */\n#define REDISMODULE_CTX_FLAGS_EVICT (1<<9)\n/* Redis is out of memory according to the maxmemory flag. */\n#define REDISMODULE_CTX_FLAGS_OOM (1<<10)\n/* Less than 25% of memory available according to maxmemory. */\n#define REDISMODULE_CTX_FLAGS_OOM_WARNING (1<<11)\n/* The command was sent over the replication link. */\n#define REDISMODULE_CTX_FLAGS_REPLICATED (1<<12)\n/* Redis is currently loading either from AOF or RDB. */\n#define REDISMODULE_CTX_FLAGS_LOADING (1<<13)\n/* The replica has no link with its master, note that\n * there is the inverse flag as well:\n *\n *  REDISMODULE_CTX_FLAGS_REPLICA_IS_ONLINE\n *\n * The two flags are exclusive, one or the other can be set. */\n#define REDISMODULE_CTX_FLAGS_REPLICA_IS_STALE (1<<14)\n/* The replica is trying to connect with the master.\n * (REPL_STATE_CONNECT and REPL_STATE_CONNECTING states) */\n#define REDISMODULE_CTX_FLAGS_REPLICA_IS_CONNECTING (1<<15)\n/* THe replica is receiving an RDB file from its master. */\n#define REDISMODULE_CTX_FLAGS_REPLICA_IS_TRANSFERRING (1<<16)\n/* The replica is online, receiving updates from its master. */\n#define REDISMODULE_CTX_FLAGS_REPLICA_IS_ONLINE (1<<17)\n/* There is currently some background process active. */\n#define REDISMODULE_CTX_FLAGS_ACTIVE_CHILD (1<<18)\n/* The next EXEC will fail due to dirty CAS (touched keys). */\n#define REDISMODULE_CTX_FLAGS_MULTI_DIRTY (1<<19)\n/* Redis is currently running inside background child process. */\n#define REDISMODULE_CTX_FLAGS_IS_CHILD (1<<20)\n/* The current client does not allow blocking, either called from\n * within multi, lua, or from another module using RM_Call */\n#define REDISMODULE_CTX_FLAGS_DENY_BLOCKING (1<<21)\n\n/* Next context flag, must be updated when adding new flags above!\nThis flag should not be used directly by the module.\n * Use RedisModule_GetContextFlagsAll instead. */\n#define _REDISMODULE_CTX_FLAGS_NEXT (1<<22)\n\n/* Keyspace changes notification classes. Every class is associated with a\n * character for configuration purposes.\n * NOTE: These have to be in sync with NOTIFY_* in server.h */\n#define REDISMODULE_NOTIFY_KEYSPACE (1<<0)    /* K */\n#define REDISMODULE_NOTIFY_KEYEVENT (1<<1)    /* E */\n#define REDISMODULE_NOTIFY_GENERIC (1<<2)     /* g */\n#define REDISMODULE_NOTIFY_STRING (1<<3)      /* $ */\n#define REDISMODULE_NOTIFY_LIST (1<<4)        /* l */\n#define REDISMODULE_NOTIFY_SET (1<<5)         /* s */\n#define REDISMODULE_NOTIFY_HASH (1<<6)        /* h */\n#define REDISMODULE_NOTIFY_ZSET (1<<7)        /* z */\n#define REDISMODULE_NOTIFY_EXPIRED (1<<8)     /* x */\n#define REDISMODULE_NOTIFY_EVICTED (1<<9)     /* e */\n#define REDISMODULE_NOTIFY_STREAM (1<<10)     /* t */\n#define REDISMODULE_NOTIFY_KEY_MISS (1<<11)   /* m (Note: This one is excluded from REDISMODULE_NOTIFY_ALL on purpose) */\n#define REDISMODULE_NOTIFY_LOADED (1<<12)     /* module only key space notification, indicate a key loaded from rdb */\n#define REDISMODULE_NOTIFY_MODULE (1<<13)     /* d, module key space notification */\n\n/* Next notification flag, must be updated when adding new flags above!\nThis flag should not be used directly by the module.\n * Use RedisModule_GetKeyspaceNotificationFlagsAll instead. */\n#define _REDISMODULE_NOTIFY_NEXT (1<<14)\n\n#define REDISMODULE_NOTIFY_ALL (REDISMODULE_NOTIFY_GENERIC | REDISMODULE_NOTIFY_STRING | REDISMODULE_NOTIFY_LIST | REDISMODULE_NOTIFY_SET | REDISMODULE_NOTIFY_HASH | REDISMODULE_NOTIFY_ZSET | REDISMODULE_NOTIFY_EXPIRED | REDISMODULE_NOTIFY_EVICTED | REDISMODULE_NOTIFY_STREAM | REDISMODULE_NOTIFY_MODULE)      /* A */\n\n/* A special pointer that we can use between the core and the module to signal\n * field deletion, and that is impossible to be a valid pointer. */\n#define REDISMODULE_HASH_DELETE ((RedisModuleString*)(long)1)\n\n/* Error messages. */\n#define REDISMODULE_ERRORMSG_WRONGTYPE \"WRONGTYPE Operation against a key holding the wrong kind of value\"\n\n#define REDISMODULE_POSITIVE_INFINITE (1.0/0.0)\n#define REDISMODULE_NEGATIVE_INFINITE (-1.0/0.0)\n\n/* Cluster API defines. */\n#define REDISMODULE_NODE_ID_LEN 40\n#define REDISMODULE_NODE_MYSELF     (1<<0)\n#define REDISMODULE_NODE_MASTER     (1<<1)\n#define REDISMODULE_NODE_SLAVE      (1<<2)\n#define REDISMODULE_NODE_PFAIL      (1<<3)\n#define REDISMODULE_NODE_FAIL       (1<<4)\n#define REDISMODULE_NODE_NOFAILOVER (1<<5)\n\n#define REDISMODULE_CLUSTER_FLAG_NONE 0\n#define REDISMODULE_CLUSTER_FLAG_NO_FAILOVER (1<<1)\n#define REDISMODULE_CLUSTER_FLAG_NO_REDIRECTION (1<<2)\n\n#define REDISMODULE_NOT_USED(V) ((void) V)\n\n/* Logging level strings */\n#define REDISMODULE_LOGLEVEL_DEBUG \"debug\"\n#define REDISMODULE_LOGLEVEL_VERBOSE \"verbose\"\n#define REDISMODULE_LOGLEVEL_NOTICE \"notice\"\n#define REDISMODULE_LOGLEVEL_WARNING \"warning\"\n\n/* Bit flags for aux_save_triggers and the aux_load and aux_save callbacks */\n#define REDISMODULE_AUX_BEFORE_RDB (1<<0)\n#define REDISMODULE_AUX_AFTER_RDB (1<<1)\n\n/* This type represents a timer handle, and is returned when a timer is\n * registered and used in order to invalidate a timer. It's just a 64 bit\n * number, because this is how each timer is represented inside the radix tree\n * of timers that are going to expire, sorted by expire time. */\ntypedef uint64_t RedisModuleTimerID;\n\n/* CommandFilter Flags */\n\n/* Do filter RedisModule_Call() commands initiated by module itself. */\n#define REDISMODULE_CMDFILTER_NOSELF    (1<<0)\n\n/* Declare that the module can handle errors with RedisModule_SetModuleOptions. */\n#define REDISMODULE_OPTIONS_HANDLE_IO_ERRORS    (1<<0)\n/* When set, Redis will not call RedisModule_SignalModifiedKey(), implicitly in\n * RedisModule_CloseKey, and the module needs to do that when manually when keys\n * are modified from the user's sperspective, to invalidate WATCH. */\n#define REDISMODULE_OPTION_NO_IMPLICIT_SIGNAL_MODIFIED (1<<1)\n\n/* Server events definitions.\n * Those flags should not be used directly by the module, instead\n * the module should use RedisModuleEvent_* variables */\n#define REDISMODULE_EVENT_REPLICATION_ROLE_CHANGED 0\n#define REDISMODULE_EVENT_PERSISTENCE 1\n#define REDISMODULE_EVENT_FLUSHDB 2\n#define REDISMODULE_EVENT_LOADING 3\n#define REDISMODULE_EVENT_CLIENT_CHANGE 4\n#define REDISMODULE_EVENT_SHUTDOWN 5\n#define REDISMODULE_EVENT_REPLICA_CHANGE 6\n#define REDISMODULE_EVENT_MASTER_LINK_CHANGE 7\n#define REDISMODULE_EVENT_CRON_LOOP 8\n#define REDISMODULE_EVENT_MODULE_CHANGE 9\n#define REDISMODULE_EVENT_LOADING_PROGRESS 10\n#define REDISMODULE_EVENT_SWAPDB 11\n#define REDISMODULE_EVENT_REPL_BACKUP 12\n#define REDISMODULE_EVENT_FORK_CHILD 13\n#define _REDISMODULE_EVENT_NEXT 14 /* Next event flag, should be updated if a new event added. */\n\ntypedef struct RedisModuleEvent {\n    uint64_t id;        /* REDISMODULE_EVENT_... defines. */\n    uint64_t dataver;   /* Version of the structure we pass as 'data'. */\n} RedisModuleEvent;\n\nstruct RedisModuleCtx;\nstruct RedisModuleDefragCtx;\ntypedef void (*RedisModuleEventCallback)(struct RedisModuleCtx *ctx, RedisModuleEvent eid, uint64_t subevent, void *data);\n\nstatic const RedisModuleEvent\n    RedisModuleEvent_ReplicationRoleChanged = {\n        REDISMODULE_EVENT_REPLICATION_ROLE_CHANGED,\n        1\n    },\n    RedisModuleEvent_Persistence = {\n        REDISMODULE_EVENT_PERSISTENCE,\n        1\n    },\n    RedisModuleEvent_FlushDB = {\n        REDISMODULE_EVENT_FLUSHDB,\n        1\n    },\n    RedisModuleEvent_Loading = {\n        REDISMODULE_EVENT_LOADING,\n        1\n    },\n    RedisModuleEvent_ClientChange = {\n        REDISMODULE_EVENT_CLIENT_CHANGE,\n        1\n    },\n    RedisModuleEvent_Shutdown = {\n        REDISMODULE_EVENT_SHUTDOWN,\n        1\n    },\n    RedisModuleEvent_ReplicaChange = {\n        REDISMODULE_EVENT_REPLICA_CHANGE,\n        1\n    },\n    RedisModuleEvent_CronLoop = {\n        REDISMODULE_EVENT_CRON_LOOP,\n        1\n    },\n    RedisModuleEvent_MasterLinkChange = {\n        REDISMODULE_EVENT_MASTER_LINK_CHANGE,\n        1\n    },\n    RedisModuleEvent_ModuleChange = {\n        REDISMODULE_EVENT_MODULE_CHANGE,\n        1\n    },\n    RedisModuleEvent_LoadingProgress = {\n        REDISMODULE_EVENT_LOADING_PROGRESS,\n        1\n    },\n    RedisModuleEvent_SwapDB = {\n        REDISMODULE_EVENT_SWAPDB,\n        1\n    },\n    RedisModuleEvent_ReplBackup = {\n        REDISMODULE_EVENT_REPL_BACKUP,\n        1\n    },\n    RedisModuleEvent_ForkChild = {\n        REDISMODULE_EVENT_FORK_CHILD,\n        1\n    };\n\n/* Those are values that are used for the 'subevent' callback argument. */\n#define REDISMODULE_SUBEVENT_PERSISTENCE_RDB_START 0\n#define REDISMODULE_SUBEVENT_PERSISTENCE_AOF_START 1\n#define REDISMODULE_SUBEVENT_PERSISTENCE_SYNC_RDB_START 2\n#define REDISMODULE_SUBEVENT_PERSISTENCE_ENDED 3\n#define REDISMODULE_SUBEVENT_PERSISTENCE_FAILED 4\n#define _REDISMODULE_SUBEVENT_PERSISTENCE_NEXT 5\n\n#define REDISMODULE_SUBEVENT_LOADING_RDB_START 0\n#define REDISMODULE_SUBEVENT_LOADING_AOF_START 1\n#define REDISMODULE_SUBEVENT_LOADING_REPL_START 2\n#define REDISMODULE_SUBEVENT_LOADING_ENDED 3\n#define REDISMODULE_SUBEVENT_LOADING_FAILED 4\n#define _REDISMODULE_SUBEVENT_LOADING_NEXT 5\n\n#define REDISMODULE_SUBEVENT_CLIENT_CHANGE_CONNECTED 0\n#define REDISMODULE_SUBEVENT_CLIENT_CHANGE_DISCONNECTED 1\n#define _REDISMODULE_SUBEVENT_CLIENT_CHANGE_NEXT 2\n\n#define REDISMODULE_SUBEVENT_MASTER_LINK_UP 0\n#define REDISMODULE_SUBEVENT_MASTER_LINK_DOWN 1\n#define _REDISMODULE_SUBEVENT_MASTER_NEXT 2\n\n#define REDISMODULE_SUBEVENT_REPLICA_CHANGE_ONLINE 0\n#define REDISMODULE_SUBEVENT_REPLICA_CHANGE_OFFLINE 1\n#define _REDISMODULE_SUBEVENT_REPLICA_CHANGE_NEXT 2\n\n#define REDISMODULE_EVENT_REPLROLECHANGED_NOW_MASTER 0\n#define REDISMODULE_EVENT_REPLROLECHANGED_NOW_REPLICA 1\n#define _REDISMODULE_EVENT_REPLROLECHANGED_NEXT 2\n\n#define REDISMODULE_SUBEVENT_FLUSHDB_START 0\n#define REDISMODULE_SUBEVENT_FLUSHDB_END 1\n#define _REDISMODULE_SUBEVENT_FLUSHDB_NEXT 2\n\n#define REDISMODULE_SUBEVENT_MODULE_LOADED 0\n#define REDISMODULE_SUBEVENT_MODULE_UNLOADED 1\n#define _REDISMODULE_SUBEVENT_MODULE_NEXT 2\n\n#define REDISMODULE_SUBEVENT_LOADING_PROGRESS_RDB 0\n#define REDISMODULE_SUBEVENT_LOADING_PROGRESS_AOF 1\n#define _REDISMODULE_SUBEVENT_LOADING_PROGRESS_NEXT 2\n\n#define REDISMODULE_SUBEVENT_REPL_BACKUP_CREATE 0\n#define REDISMODULE_SUBEVENT_REPL_BACKUP_RESTORE 1\n#define REDISMODULE_SUBEVENT_REPL_BACKUP_DISCARD 2\n#define _REDISMODULE_SUBEVENT_REPL_BACKUP_NEXT 3\n\n#define REDISMODULE_SUBEVENT_FORK_CHILD_BORN 0\n#define REDISMODULE_SUBEVENT_FORK_CHILD_DIED 1\n#define _REDISMODULE_SUBEVENT_FORK_CHILD_NEXT 2\n\n#define _REDISMODULE_SUBEVENT_SHUTDOWN_NEXT 0\n#define _REDISMODULE_SUBEVENT_CRON_LOOP_NEXT 0\n#define _REDISMODULE_SUBEVENT_SWAPDB_NEXT 0\n\n/* RedisModuleClientInfo flags. */\n#define REDISMODULE_CLIENTINFO_FLAG_SSL (1<<0)\n#define REDISMODULE_CLIENTINFO_FLAG_PUBSUB (1<<1)\n#define REDISMODULE_CLIENTINFO_FLAG_BLOCKED (1<<2)\n#define REDISMODULE_CLIENTINFO_FLAG_TRACKING (1<<3)\n#define REDISMODULE_CLIENTINFO_FLAG_UNIXSOCKET (1<<4)\n#define REDISMODULE_CLIENTINFO_FLAG_MULTI (1<<5)\n\n/* Here we take all the structures that the module pass to the core\n * and the other way around. Notably the list here contains the structures\n * used by the hooks API RedisModule_RegisterToServerEvent().\n *\n * The structures always start with a 'version' field. This is useful\n * when we want to pass a reference to the structure to the core APIs,\n * for the APIs to fill the structure. In that case, the structure 'version'\n * field is initialized before passing it to the core, so that the core is\n * able to cast the pointer to the appropriate structure version. In this\n * way we obtain ABI compatibility.\n *\n * Here we'll list all the structure versions in case they evolve over time,\n * however using a define, we'll make sure to use the last version as the\n * public name for the module to use. */\n\n#define REDISMODULE_CLIENTINFO_VERSION 1\ntypedef struct RedisModuleClientInfo {\n    uint64_t version;       /* Version of this structure for ABI compat. */\n    uint64_t flags;         /* REDISMODULE_CLIENTINFO_FLAG_* */\n    uint64_t id;            /* Client ID. */\n    char addr[46];          /* IPv4 or IPv6 address. */\n    uint16_t port;          /* TCP port. */\n    uint16_t db;            /* Selected DB. */\n} RedisModuleClientInfoV1;\n\n#define RedisModuleClientInfo RedisModuleClientInfoV1\n\n#define REDISMODULE_REPLICATIONINFO_VERSION 1\ntypedef struct RedisModuleReplicationInfo {\n    uint64_t version;       /* Not used since this structure is never passed\n                               from the module to the core right now. Here\n                               for future compatibility. */\n    int master;             /* true if master, false if replica */\n    const char *masterhost;       /* master instance hostname for NOW_REPLICA */\n    int masterport;         /* master instance port for NOW_REPLICA */\n    char *replid1;          /* Main replication ID */\n    char *replid2;          /* Secondary replication ID */\n    uint64_t repl1_offset;  /* Main replication offset */\n    uint64_t repl2_offset;  /* Offset of replid2 validity */\n} RedisModuleReplicationInfoV1;\n\n#define RedisModuleReplicationInfo RedisModuleReplicationInfoV1\n\n#define REDISMODULE_FLUSHINFO_VERSION 1\ntypedef struct RedisModuleFlushInfo {\n    uint64_t version;       /* Not used since this structure is never passed\n                               from the module to the core right now. Here\n                               for future compatibility. */\n    int32_t sync;           /* Synchronous or threaded flush?. */\n    int32_t dbnum;          /* Flushed database number, -1 for ALL. */\n} RedisModuleFlushInfoV1;\n\n#define RedisModuleFlushInfo RedisModuleFlushInfoV1\n\n#define REDISMODULE_MODULE_CHANGE_VERSION 1\ntypedef struct RedisModuleModuleChange {\n    uint64_t version;       /* Not used since this structure is never passed\n                               from the module to the core right now. Here\n                               for future compatibility. */\n    const char* module_name;/* Name of module loaded or unloaded. */\n    int32_t module_version; /* Module version. */\n} RedisModuleModuleChangeV1;\n\n#define RedisModuleModuleChange RedisModuleModuleChangeV1\n\n#define REDISMODULE_CRON_LOOP_VERSION 1\ntypedef struct RedisModuleCronLoopInfo {\n    uint64_t version;       /* Not used since this structure is never passed\n                               from the module to the core right now. Here\n                               for future compatibility. */\n    int32_t hz;             /* Approximate number of events per second. */\n} RedisModuleCronLoopV1;\n\n#define RedisModuleCronLoop RedisModuleCronLoopV1\n\n#define REDISMODULE_LOADING_PROGRESS_VERSION 1\ntypedef struct RedisModuleLoadingProgressInfo {\n    uint64_t version;       /* Not used since this structure is never passed\n                               from the module to the core right now. Here\n                               for future compatibility. */\n    int32_t hz;             /* Approximate number of events per second. */\n    int32_t progress;       /* Approximate progress between 0 and 1024, or -1\n                             * if unknown. */\n} RedisModuleLoadingProgressV1;\n\n#define RedisModuleLoadingProgress RedisModuleLoadingProgressV1\n\n#define REDISMODULE_SWAPDBINFO_VERSION 1\ntypedef struct RedisModuleSwapDbInfo {\n    uint64_t version;       /* Not used since this structure is never passed\n                               from the module to the core right now. Here\n                               for future compatibility. */\n    int32_t dbnum_first;    /* Swap Db first dbnum */\n    int32_t dbnum_second;   /* Swap Db second dbnum */\n} RedisModuleSwapDbInfoV1;\n\n#define RedisModuleSwapDbInfo RedisModuleSwapDbInfoV1\n\n/* ------------------------- End of common defines ------------------------ */\n\n#ifndef REDISMODULE_CORE\n\ntypedef long long mstime_t;\n\n/* Macro definitions specific to individual compilers */\n#ifndef REDISMODULE_ATTR_UNUSED\n#    ifdef __GNUC__\n#        define REDISMODULE_ATTR_UNUSED __attribute__((unused))\n#    else\n#        define REDISMODULE_ATTR_UNUSED\n#    endif\n#endif\n\n#ifndef REDISMODULE_ATTR_PRINTF\n#    ifdef __GNUC__\n#        define REDISMODULE_ATTR_PRINTF(idx,cnt) __attribute__((format(printf,idx,cnt)))\n#    else\n#        define REDISMODULE_ATTR_PRINTF(idx,cnt)\n#    endif\n#endif\n\n#ifndef REDISMODULE_ATTR_COMMON\n#    if defined(__GNUC__) && !defined(__clang__)\n#        define REDISMODULE_ATTR_COMMON __attribute__((__common__))\n#    else\n#        define REDISMODULE_ATTR_COMMON\n#    endif\n#endif\n\n/* Incomplete structures for compiler checks but opaque access. */\ntypedef struct RedisModuleCtx RedisModuleCtx;\ntypedef struct RedisModuleKey RedisModuleKey;\ntypedef struct RedisModuleString RedisModuleString;\ntypedef struct RedisModuleCallReply RedisModuleCallReply;\ntypedef struct RedisModuleIO RedisModuleIO;\ntypedef struct RedisModuleType RedisModuleType;\ntypedef struct RedisModuleDigest RedisModuleDigest;\ntypedef struct RedisModuleBlockedClient RedisModuleBlockedClient;\ntypedef struct RedisModuleClusterInfo RedisModuleClusterInfo;\ntypedef struct RedisModuleDict RedisModuleDict;\ntypedef struct RedisModuleDictIter RedisModuleDictIter;\ntypedef struct RedisModuleCommandFilterCtx RedisModuleCommandFilterCtx;\ntypedef struct RedisModuleCommandFilter RedisModuleCommandFilter;\ntypedef struct RedisModuleInfoCtx RedisModuleInfoCtx;\ntypedef struct RedisModuleServerInfoData RedisModuleServerInfoData;\ntypedef struct RedisModuleScanCursor RedisModuleScanCursor;\ntypedef struct RedisModuleDefragCtx RedisModuleDefragCtx;\ntypedef struct RedisModuleUser RedisModuleUser;\n\ntypedef int (*RedisModuleCmdFunc)(RedisModuleCtx *ctx, RedisModuleString **argv, int argc);\ntypedef void (*RedisModuleDisconnectFunc)(RedisModuleCtx *ctx, RedisModuleBlockedClient *bc);\ntypedef int (*RedisModuleNotificationFunc)(RedisModuleCtx *ctx, int type, const char *event, RedisModuleString *key);\ntypedef void *(*RedisModuleTypeLoadFunc)(RedisModuleIO *rdb, int encver);\ntypedef void (*RedisModuleTypeSaveFunc)(RedisModuleIO *rdb, void *value);\ntypedef int (*RedisModuleTypeAuxLoadFunc)(RedisModuleIO *rdb, int encver, int when);\ntypedef void (*RedisModuleTypeAuxSaveFunc)(RedisModuleIO *rdb, int when);\ntypedef void (*RedisModuleTypeRewriteFunc)(RedisModuleIO *aof, RedisModuleString *key, void *value);\ntypedef size_t (*RedisModuleTypeMemUsageFunc)(const void *value);\ntypedef void (*RedisModuleTypeDigestFunc)(RedisModuleDigest *digest, void *value);\ntypedef void (*RedisModuleTypeFreeFunc)(void *value);\ntypedef size_t (*RedisModuleTypeFreeEffortFunc)(RedisModuleString *key, const void *value);\ntypedef void (*RedisModuleTypeUnlinkFunc)(RedisModuleString *key, const void *value);\ntypedef void *(*RedisModuleTypeCopyFunc)(RedisModuleString *fromkey, RedisModuleString *tokey, const void *value);\ntypedef int (*RedisModuleTypeDefragFunc)(RedisModuleDefragCtx *ctx, RedisModuleString *key, void **value);\ntypedef void (*RedisModuleClusterMessageReceiver)(RedisModuleCtx *ctx, const char *sender_id, uint8_t type, const unsigned char *payload, uint32_t len);\ntypedef void (*RedisModuleTimerProc)(RedisModuleCtx *ctx, void *data);\ntypedef void (*RedisModuleCommandFilterFunc) (RedisModuleCommandFilterCtx *filter);\ntypedef void (*RedisModuleForkDoneHandler) (int exitcode, int bysignal, void *user_data);\ntypedef void (*RedisModuleInfoFunc)(RedisModuleInfoCtx *ctx, int for_crash_report);\ntypedef void (*RedisModuleScanCB)(RedisModuleCtx *ctx, RedisModuleString *keyname, RedisModuleKey *key, void *privdata);\ntypedef void (*RedisModuleScanKeyCB)(RedisModuleKey *key, RedisModuleString *field, RedisModuleString *value, void *privdata);\ntypedef void (*RedisModuleUserChangedFunc) (uint64_t client_id, void *privdata);\ntypedef int (*RedisModuleDefragFunc)(RedisModuleDefragCtx *ctx);\n\ntypedef struct RedisModuleTypeMethods {\n    uint64_t version;\n    RedisModuleTypeLoadFunc rdb_load;\n    RedisModuleTypeSaveFunc rdb_save;\n    RedisModuleTypeRewriteFunc aof_rewrite;\n    RedisModuleTypeMemUsageFunc mem_usage;\n    RedisModuleTypeDigestFunc digest;\n    RedisModuleTypeFreeFunc free;\n    RedisModuleTypeAuxLoadFunc aux_load;\n    RedisModuleTypeAuxSaveFunc aux_save;\n    int aux_save_triggers;\n    RedisModuleTypeFreeEffortFunc free_effort;\n    RedisModuleTypeUnlinkFunc unlink;\n    RedisModuleTypeCopyFunc copy;\n    RedisModuleTypeDefragFunc defrag;\n} RedisModuleTypeMethods;\n\n#define REDISMODULE_GET_API(name) \\\n    RedisModule_GetApi(\"RedisModule_\" #name, ((void **)&RedisModule_ ## name))\n\n/* Default API declaration prefix (not 'extern' for backwards compatibility) */\n#ifndef REDISMODULE_API\n#define REDISMODULE_API\n#endif\n\n/* Default API declaration suffix (compiler attributes) */\n#ifndef REDISMODULE_ATTR\n#define REDISMODULE_ATTR REDISMODULE_ATTR_COMMON\n#endif\n\nREDISMODULE_API void * (*RedisModule_Alloc)(size_t bytes) REDISMODULE_ATTR;\nREDISMODULE_API void * (*RedisModule_Realloc)(void *ptr, size_t bytes) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_Free)(void *ptr) REDISMODULE_ATTR;\nREDISMODULE_API void * (*RedisModule_Calloc)(size_t nmemb, size_t size) REDISMODULE_ATTR;\nREDISMODULE_API char * (*RedisModule_Strdup)(const char *str) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_GetApi)(const char *, void *) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_CreateCommand)(RedisModuleCtx *ctx, const char *name, RedisModuleCmdFunc cmdfunc, const char *strflags, int firstkey, int lastkey, int keystep) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_SetModuleAttribs)(RedisModuleCtx *ctx, const char *name, int ver, int apiver) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_IsModuleNameBusy)(const char *name) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_WrongArity)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithLongLong)(RedisModuleCtx *ctx, long long ll) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_GetSelectedDb)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_SelectDb)(RedisModuleCtx *ctx, int newid) REDISMODULE_ATTR;\nREDISMODULE_API void * (*RedisModule_OpenKey)(RedisModuleCtx *ctx, RedisModuleString *keyname, int mode) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_CloseKey)(RedisModuleKey *kp) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_KeyType)(RedisModuleKey *kp) REDISMODULE_ATTR;\nREDISMODULE_API size_t (*RedisModule_ValueLength)(RedisModuleKey *kp) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ListPush)(RedisModuleKey *kp, int where, RedisModuleString *ele) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_ListPop)(RedisModuleKey *key, int where) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleCallReply * (*RedisModule_Call)(RedisModuleCtx *ctx, const char *cmdname, const char *fmt, ...) REDISMODULE_ATTR;\nREDISMODULE_API const char * (*RedisModule_CallReplyProto)(RedisModuleCallReply *reply, size_t *len) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_FreeCallReply)(RedisModuleCallReply *reply) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_CallReplyType)(RedisModuleCallReply *reply) REDISMODULE_ATTR;\nREDISMODULE_API long long (*RedisModule_CallReplyInteger)(RedisModuleCallReply *reply) REDISMODULE_ATTR;\nREDISMODULE_API size_t (*RedisModule_CallReplyLength)(RedisModuleCallReply *reply) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleCallReply * (*RedisModule_CallReplyArrayElement)(RedisModuleCallReply *reply, size_t idx) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_CreateString)(RedisModuleCtx *ctx, const char *ptr, size_t len) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_CreateStringFromLongLong)(RedisModuleCtx *ctx, long long ll) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_CreateStringFromDouble)(RedisModuleCtx *ctx, double d) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_CreateStringFromLongDouble)(RedisModuleCtx *ctx, long double ld, int humanfriendly) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_CreateStringFromString)(RedisModuleCtx *ctx, const RedisModuleString *str) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_CreateStringFromStreamID)(RedisModuleCtx *ctx, const RedisModuleStreamID *id) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_CreateStringPrintf)(RedisModuleCtx *ctx, const char *fmt, ...) REDISMODULE_ATTR_PRINTF(2,3) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_FreeString)(RedisModuleCtx *ctx, RedisModuleString *str) REDISMODULE_ATTR;\nREDISMODULE_API const char * (*RedisModule_StringPtrLen)(const RedisModuleString *str, size_t *len) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithError)(RedisModuleCtx *ctx, const char *err) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithSimpleString)(RedisModuleCtx *ctx, const char *msg) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithArray)(RedisModuleCtx *ctx, long len) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithNullArray)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithEmptyArray)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_ReplySetArrayLength)(RedisModuleCtx *ctx, long len) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithStringBuffer)(RedisModuleCtx *ctx, const char *buf, size_t len) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithCString)(RedisModuleCtx *ctx, const char *buf) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithString)(RedisModuleCtx *ctx, RedisModuleString *str) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithEmptyString)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithVerbatimString)(RedisModuleCtx *ctx, const char *buf, size_t len) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithNull)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithDouble)(RedisModuleCtx *ctx, double d) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithLongDouble)(RedisModuleCtx *ctx, long double d) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithCallReply)(RedisModuleCtx *ctx, RedisModuleCallReply *reply) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StringToLongLong)(const RedisModuleString *str, long long *ll) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StringToDouble)(const RedisModuleString *str, double *d) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StringToLongDouble)(const RedisModuleString *str, long double *d) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StringToStreamID)(const RedisModuleString *str, RedisModuleStreamID *id) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_AutoMemory)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_Replicate)(RedisModuleCtx *ctx, const char *cmdname, const char *fmt, ...) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplicateVerbatim)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API const char * (*RedisModule_CallReplyStringPtr)(RedisModuleCallReply *reply, size_t *len) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_CreateStringFromCallReply)(RedisModuleCallReply *reply) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DeleteKey)(RedisModuleKey *key) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_UnlinkKey)(RedisModuleKey *key) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StringSet)(RedisModuleKey *key, RedisModuleString *str) REDISMODULE_ATTR;\nREDISMODULE_API char * (*RedisModule_StringDMA)(RedisModuleKey *key, size_t *len, int mode) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StringTruncate)(RedisModuleKey *key, size_t newlen) REDISMODULE_ATTR;\nREDISMODULE_API mstime_t (*RedisModule_GetExpire)(RedisModuleKey *key) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_SetExpire)(RedisModuleKey *key, mstime_t expire) REDISMODULE_ATTR;\nREDISMODULE_API mstime_t (*RedisModule_GetAbsExpire)(RedisModuleKey *key) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_SetAbsExpire)(RedisModuleKey *key, mstime_t expire) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_ResetDataset)(int restart_aof, int async) REDISMODULE_ATTR;\nREDISMODULE_API unsigned long long (*RedisModule_DbSize)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_RandomKey)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ZsetAdd)(RedisModuleKey *key, double score, RedisModuleString *ele, int *flagsptr) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ZsetIncrby)(RedisModuleKey *key, double score, RedisModuleString *ele, int *flagsptr, double *newscore) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ZsetScore)(RedisModuleKey *key, RedisModuleString *ele, double *score) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ZsetRem)(RedisModuleKey *key, RedisModuleString *ele, int *deleted) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_ZsetRangeStop)(RedisModuleKey *key) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ZsetFirstInScoreRange)(RedisModuleKey *key, double min, double max, int minex, int maxex) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ZsetLastInScoreRange)(RedisModuleKey *key, double min, double max, int minex, int maxex) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ZsetFirstInLexRange)(RedisModuleKey *key, RedisModuleString *min, RedisModuleString *max) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ZsetLastInLexRange)(RedisModuleKey *key, RedisModuleString *min, RedisModuleString *max) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_ZsetRangeCurrentElement)(RedisModuleKey *key, double *score) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ZsetRangeNext)(RedisModuleKey *key) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ZsetRangePrev)(RedisModuleKey *key) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ZsetRangeEndReached)(RedisModuleKey *key) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_HashSet)(RedisModuleKey *key, int flags, ...) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_HashGet)(RedisModuleKey *key, int flags, ...) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StreamAdd)(RedisModuleKey *key, int flags, RedisModuleStreamID *id, RedisModuleString **argv, int64_t numfields) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StreamDelete)(RedisModuleKey *key, RedisModuleStreamID *id) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StreamIteratorStart)(RedisModuleKey *key, int flags, RedisModuleStreamID *startid, RedisModuleStreamID *endid) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StreamIteratorStop)(RedisModuleKey *key) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StreamIteratorNextID)(RedisModuleKey *key, RedisModuleStreamID *id, long *numfields) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StreamIteratorNextField)(RedisModuleKey *key, RedisModuleString **field_ptr, RedisModuleString **value_ptr) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StreamIteratorDelete)(RedisModuleKey *key) REDISMODULE_ATTR;\nREDISMODULE_API long long (*RedisModule_StreamTrimByLength)(RedisModuleKey *key, int flags, long long length) REDISMODULE_ATTR;\nREDISMODULE_API long long (*RedisModule_StreamTrimByID)(RedisModuleKey *key, int flags, RedisModuleStreamID *id) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_IsKeysPositionRequest)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_KeyAtPos)(RedisModuleCtx *ctx, int pos) REDISMODULE_ATTR;\nREDISMODULE_API unsigned long long (*RedisModule_GetClientId)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_GetClientUserNameById)(RedisModuleCtx *ctx, uint64_t id) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_GetClientInfoById)(void *ci, uint64_t id) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_PublishMessage)(RedisModuleCtx *ctx, RedisModuleString *channel, RedisModuleString *message) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_GetContextFlags)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_AvoidReplicaTraffic)() REDISMODULE_ATTR;\nREDISMODULE_API void * (*RedisModule_PoolAlloc)(RedisModuleCtx *ctx, size_t bytes) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleType * (*RedisModule_CreateDataType)(RedisModuleCtx *ctx, const char *name, int encver, RedisModuleTypeMethods *typemethods) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ModuleTypeSetValue)(RedisModuleKey *key, RedisModuleType *mt, void *value) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ModuleTypeReplaceValue)(RedisModuleKey *key, RedisModuleType *mt, void *new_value, void **old_value) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleType * (*RedisModule_ModuleTypeGetType)(RedisModuleKey *key) REDISMODULE_ATTR;\nREDISMODULE_API void * (*RedisModule_ModuleTypeGetValue)(RedisModuleKey *key) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_IsIOError)(RedisModuleIO *io) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_SetModuleOptions)(RedisModuleCtx *ctx, int options) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_SignalModifiedKey)(RedisModuleCtx *ctx, RedisModuleString *keyname) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_SaveUnsigned)(RedisModuleIO *io, uint64_t value) REDISMODULE_ATTR;\nREDISMODULE_API uint64_t (*RedisModule_LoadUnsigned)(RedisModuleIO *io) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_SaveSigned)(RedisModuleIO *io, int64_t value) REDISMODULE_ATTR;\nREDISMODULE_API int64_t (*RedisModule_LoadSigned)(RedisModuleIO *io) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_EmitAOF)(RedisModuleIO *io, const char *cmdname, const char *fmt, ...) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_SaveString)(RedisModuleIO *io, RedisModuleString *s) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_SaveStringBuffer)(RedisModuleIO *io, const char *str, size_t len) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_LoadString)(RedisModuleIO *io) REDISMODULE_ATTR;\nREDISMODULE_API char * (*RedisModule_LoadStringBuffer)(RedisModuleIO *io, size_t *lenptr) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_SaveDouble)(RedisModuleIO *io, double value) REDISMODULE_ATTR;\nREDISMODULE_API double (*RedisModule_LoadDouble)(RedisModuleIO *io) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_SaveFloat)(RedisModuleIO *io, float value) REDISMODULE_ATTR;\nREDISMODULE_API float (*RedisModule_LoadFloat)(RedisModuleIO *io) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_SaveLongDouble)(RedisModuleIO *io, long double value) REDISMODULE_ATTR;\nREDISMODULE_API long double (*RedisModule_LoadLongDouble)(RedisModuleIO *io) REDISMODULE_ATTR;\nREDISMODULE_API void * (*RedisModule_LoadDataTypeFromString)(const RedisModuleString *str, const RedisModuleType *mt) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_SaveDataTypeToString)(RedisModuleCtx *ctx, void *data, const RedisModuleType *mt) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_Log)(RedisModuleCtx *ctx, const char *level, const char *fmt, ...) REDISMODULE_ATTR REDISMODULE_ATTR_PRINTF(3,4);\nREDISMODULE_API void (*RedisModule_LogIOError)(RedisModuleIO *io, const char *levelstr, const char *fmt, ...) REDISMODULE_ATTR REDISMODULE_ATTR_PRINTF(3,4);\nREDISMODULE_API void (*RedisModule__Assert)(const char *estr, const char *file, int line) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_LatencyAddSample)(const char *event, mstime_t latency) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StringAppendBuffer)(RedisModuleCtx *ctx, RedisModuleString *str, const char *buf, size_t len) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_RetainString)(RedisModuleCtx *ctx, RedisModuleString *str) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_HoldString)(RedisModuleCtx *ctx, RedisModuleString *str) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StringCompare)(RedisModuleString *a, RedisModuleString *b) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleCtx * (*RedisModule_GetContextFromIO)(RedisModuleIO *io) REDISMODULE_ATTR;\nREDISMODULE_API const RedisModuleString * (*RedisModule_GetKeyNameFromIO)(RedisModuleIO *io) REDISMODULE_ATTR;\nREDISMODULE_API const RedisModuleString * (*RedisModule_GetKeyNameFromModuleKey)(RedisModuleKey *key) REDISMODULE_ATTR;\nREDISMODULE_API long long (*RedisModule_Milliseconds)(void) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_DigestAddStringBuffer)(RedisModuleDigest *md, unsigned char *ele, size_t len) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_DigestAddLongLong)(RedisModuleDigest *md, long long ele) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_DigestEndSequence)(RedisModuleDigest *md) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleDict * (*RedisModule_CreateDict)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_FreeDict)(RedisModuleCtx *ctx, RedisModuleDict *d) REDISMODULE_ATTR;\nREDISMODULE_API uint64_t (*RedisModule_DictSize)(RedisModuleDict *d) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DictSetC)(RedisModuleDict *d, void *key, size_t keylen, void *ptr) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DictReplaceC)(RedisModuleDict *d, void *key, size_t keylen, void *ptr) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DictSet)(RedisModuleDict *d, RedisModuleString *key, void *ptr) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DictReplace)(RedisModuleDict *d, RedisModuleString *key, void *ptr) REDISMODULE_ATTR;\nREDISMODULE_API void * (*RedisModule_DictGetC)(RedisModuleDict *d, void *key, size_t keylen, int *nokey) REDISMODULE_ATTR;\nREDISMODULE_API void * (*RedisModule_DictGet)(RedisModuleDict *d, RedisModuleString *key, int *nokey) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DictDelC)(RedisModuleDict *d, void *key, size_t keylen, void *oldval) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DictDel)(RedisModuleDict *d, RedisModuleString *key, void *oldval) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleDictIter * (*RedisModule_DictIteratorStartC)(RedisModuleDict *d, const char *op, void *key, size_t keylen) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleDictIter * (*RedisModule_DictIteratorStart)(RedisModuleDict *d, const char *op, RedisModuleString *key) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_DictIteratorStop)(RedisModuleDictIter *di) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DictIteratorReseekC)(RedisModuleDictIter *di, const char *op, void *key, size_t keylen) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DictIteratorReseek)(RedisModuleDictIter *di, const char *op, RedisModuleString *key) REDISMODULE_ATTR;\nREDISMODULE_API void * (*RedisModule_DictNextC)(RedisModuleDictIter *di, size_t *keylen, void **dataptr) REDISMODULE_ATTR;\nREDISMODULE_API void * (*RedisModule_DictPrevC)(RedisModuleDictIter *di, size_t *keylen, void **dataptr) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_DictNext)(RedisModuleCtx *ctx, RedisModuleDictIter *di, void **dataptr) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_DictPrev)(RedisModuleCtx *ctx, RedisModuleDictIter *di, void **dataptr) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DictCompareC)(RedisModuleDictIter *di, const char *op, void *key, size_t keylen) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DictCompare)(RedisModuleDictIter *di, const char *op, RedisModuleString *key) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_RegisterInfoFunc)(RedisModuleCtx *ctx, RedisModuleInfoFunc cb) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_InfoAddSection)(RedisModuleInfoCtx *ctx, char *name) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_InfoBeginDictField)(RedisModuleInfoCtx *ctx, char *name) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_InfoEndDictField)(RedisModuleInfoCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_InfoAddFieldString)(RedisModuleInfoCtx *ctx, char *field, RedisModuleString *value) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_InfoAddFieldCString)(RedisModuleInfoCtx *ctx, char *field, char *value) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_InfoAddFieldDouble)(RedisModuleInfoCtx *ctx, char *field, double value) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_InfoAddFieldLongLong)(RedisModuleInfoCtx *ctx, char *field, long long value) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_InfoAddFieldULongLong)(RedisModuleInfoCtx *ctx, char *field, unsigned long long value) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleServerInfoData * (*RedisModule_GetServerInfo)(RedisModuleCtx *ctx, const char *section) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_FreeServerInfo)(RedisModuleCtx *ctx, RedisModuleServerInfoData *data) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_ServerInfoGetField)(RedisModuleCtx *ctx, RedisModuleServerInfoData *data, const char* field) REDISMODULE_ATTR;\nREDISMODULE_API const char * (*RedisModule_ServerInfoGetFieldC)(RedisModuleServerInfoData *data, const char* field) REDISMODULE_ATTR;\nREDISMODULE_API long long (*RedisModule_ServerInfoGetFieldSigned)(RedisModuleServerInfoData *data, const char* field, int *out_err) REDISMODULE_ATTR;\nREDISMODULE_API unsigned long long (*RedisModule_ServerInfoGetFieldUnsigned)(RedisModuleServerInfoData *data, const char* field, int *out_err) REDISMODULE_ATTR;\nREDISMODULE_API double (*RedisModule_ServerInfoGetFieldDouble)(RedisModuleServerInfoData *data, const char* field, int *out_err) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_SubscribeToServerEvent)(RedisModuleCtx *ctx, RedisModuleEvent event, RedisModuleEventCallback callback) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_SetLRU)(RedisModuleKey *key, mstime_t lru_idle) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_GetLRU)(RedisModuleKey *key, mstime_t *lru_idle) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_SetLFU)(RedisModuleKey *key, long long lfu_freq) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_GetLFU)(RedisModuleKey *key, long long *lfu_freq) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleBlockedClient * (*RedisModule_BlockClientOnKeys)(RedisModuleCtx *ctx, RedisModuleCmdFunc reply_callback, RedisModuleCmdFunc timeout_callback, void (*free_privdata)(RedisModuleCtx*,void*), long long timeout_ms, RedisModuleString **keys, int numkeys, void *privdata) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_SignalKeyAsReady)(RedisModuleCtx *ctx, RedisModuleString *key) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_GetBlockedClientReadyKey)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleScanCursor * (*RedisModule_ScanCursorCreate)() REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_ScanCursorRestart)(RedisModuleScanCursor *cursor) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_ScanCursorDestroy)(RedisModuleScanCursor *cursor) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_Scan)(RedisModuleCtx *ctx, RedisModuleScanCursor *cursor, RedisModuleScanCB fn, void *privdata) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ScanKey)(RedisModuleKey *key, RedisModuleScanCursor *cursor, RedisModuleScanKeyCB fn, void *privdata) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_GetContextFlagsAll)() REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_GetKeyspaceNotificationFlagsAll)() REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_IsSubEventSupported)(RedisModuleEvent event, uint64_t subevent) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_GetServerVersion)() REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_GetTypeMethodVersion)() REDISMODULE_ATTR;\n\n/* Experimental APIs */\n#ifdef REDISMODULE_EXPERIMENTAL_API\n#define REDISMODULE_EXPERIMENTAL_API_VERSION 3\nREDISMODULE_API RedisModuleBlockedClient * (*RedisModule_BlockClient)(RedisModuleCtx *ctx, RedisModuleCmdFunc reply_callback, RedisModuleCmdFunc timeout_callback, void (*free_privdata)(RedisModuleCtx*,void*), long long timeout_ms) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_UnblockClient)(RedisModuleBlockedClient *bc, void *privdata) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_IsBlockedReplyRequest)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_IsBlockedTimeoutRequest)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API void * (*RedisModule_GetBlockedClientPrivateData)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleBlockedClient * (*RedisModule_GetBlockedClientHandle)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_AbortBlock)(RedisModuleBlockedClient *bc) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_BlockedClientMeasureTimeStart)(RedisModuleBlockedClient *bc) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_BlockedClientMeasureTimeEnd)(RedisModuleBlockedClient *bc) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleCtx * (*RedisModule_GetThreadSafeContext)(RedisModuleBlockedClient *bc) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleCtx * (*RedisModule_GetDetachedThreadSafeContext)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_FreeThreadSafeContext)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_ThreadSafeContextLock)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ThreadSafeContextTryLock)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_ThreadSafeContextUnlock)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_SubscribeToKeyspaceEvents)(RedisModuleCtx *ctx, int types, RedisModuleNotificationFunc cb) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_NotifyKeyspaceEvent)(RedisModuleCtx *ctx, int type, const char *event, RedisModuleString *key) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_GetNotifyKeyspaceEvents)() REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_BlockedClientDisconnected)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_RegisterClusterMessageReceiver)(RedisModuleCtx *ctx, uint8_t type, RedisModuleClusterMessageReceiver callback) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_SendClusterMessage)(RedisModuleCtx *ctx, char *target_id, uint8_t type, unsigned char *msg, uint32_t len) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_GetClusterNodeInfo)(RedisModuleCtx *ctx, const char *id, char *ip, char *master_id, int *port, int *flags) REDISMODULE_ATTR;\nREDISMODULE_API char ** (*RedisModule_GetClusterNodesList)(RedisModuleCtx *ctx, size_t *numnodes) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_FreeClusterNodesList)(char **ids) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleTimerID (*RedisModule_CreateTimer)(RedisModuleCtx *ctx, mstime_t period, RedisModuleTimerProc callback, void *data) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StopTimer)(RedisModuleCtx *ctx, RedisModuleTimerID id, void **data) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_GetTimerInfo)(RedisModuleCtx *ctx, RedisModuleTimerID id, uint64_t *remaining, void **data) REDISMODULE_ATTR;\nREDISMODULE_API const char * (*RedisModule_GetMyClusterID)(void) REDISMODULE_ATTR;\nREDISMODULE_API size_t (*RedisModule_GetClusterSize)(void) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_GetRandomBytes)(unsigned char *dst, size_t len) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_GetRandomHexChars)(char *dst, size_t len) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_SetDisconnectCallback)(RedisModuleBlockedClient *bc, RedisModuleDisconnectFunc callback) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_SetClusterFlags)(RedisModuleCtx *ctx, uint64_t flags) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ExportSharedAPI)(RedisModuleCtx *ctx, const char *apiname, void *func) REDISMODULE_ATTR;\nREDISMODULE_API void * (*RedisModule_GetSharedAPI)(RedisModuleCtx *ctx, const char *apiname) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleCommandFilter * (*RedisModule_RegisterCommandFilter)(RedisModuleCtx *ctx, RedisModuleCommandFilterFunc cb, int flags) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_UnregisterCommandFilter)(RedisModuleCtx *ctx, RedisModuleCommandFilter *filter) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_CommandFilterArgsCount)(RedisModuleCommandFilterCtx *fctx) REDISMODULE_ATTR;\nREDISMODULE_API const RedisModuleString * (*RedisModule_CommandFilterArgGet)(RedisModuleCommandFilterCtx *fctx, int pos) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_CommandFilterArgInsert)(RedisModuleCommandFilterCtx *fctx, int pos, RedisModuleString *arg) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_CommandFilterArgReplace)(RedisModuleCommandFilterCtx *fctx, int pos, RedisModuleString *arg) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_CommandFilterArgDelete)(RedisModuleCommandFilterCtx *fctx, int pos) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_Fork)(RedisModuleForkDoneHandler cb, void *user_data) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_SendChildHeartbeat)(double progress) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ExitFromChild)(int retcode) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_KillForkChild)(int child_pid) REDISMODULE_ATTR;\nREDISMODULE_API float (*RedisModule_GetUsedMemoryRatio)() REDISMODULE_ATTR;\nREDISMODULE_API size_t (*RedisModule_MallocSize)(void* ptr) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleUser * (*RedisModule_CreateModuleUser)(const char *name) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_FreeModuleUser)(RedisModuleUser *user) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_SetModuleUserACL)(RedisModuleUser *user, const char* acl) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_AuthenticateClientWithACLUser)(RedisModuleCtx *ctx, const char *name, size_t len, RedisModuleUserChangedFunc callback, void *privdata, uint64_t *client_id) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_AuthenticateClientWithUser)(RedisModuleCtx *ctx, RedisModuleUser *user, RedisModuleUserChangedFunc callback, void *privdata, uint64_t *client_id) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DeauthenticateAndCloseClient)(RedisModuleCtx *ctx, uint64_t client_id) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_GetClientCertificate)(RedisModuleCtx *ctx, uint64_t id) REDISMODULE_ATTR;\nREDISMODULE_API int *(*RedisModule_GetCommandKeys)(RedisModuleCtx *ctx, RedisModuleString **argv, int argc, int *num_keys) REDISMODULE_ATTR;\nREDISMODULE_API const char *(*RedisModule_GetCurrentCommandName)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_RegisterDefragFunc)(RedisModuleCtx *ctx, RedisModuleDefragFunc func) REDISMODULE_ATTR;\nREDISMODULE_API void *(*RedisModule_DefragAlloc)(RedisModuleDefragCtx *ctx, void *ptr) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString *(*RedisModule_DefragRedisModuleString)(RedisModuleDefragCtx *ctx, RedisModuleString *str) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DefragShouldStop)(RedisModuleDefragCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DefragCursorSet)(RedisModuleDefragCtx *ctx, unsigned long cursor) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DefragCursorGet)(RedisModuleDefragCtx *ctx, unsigned long *cursor) REDISMODULE_ATTR;\n#endif\n\n#define RedisModule_IsAOFClient(id) ((id) == UINT64_MAX)\n\n/* This is included inline inside each Redis module. */\nstatic int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int apiver) REDISMODULE_ATTR_UNUSED;\nstatic int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int apiver) {\n    void *getapifuncptr = ((void**)ctx)[0];\n    RedisModule_GetApi = (int (*)(const char *, void *)) (unsigned long)getapifuncptr;\n    REDISMODULE_GET_API(Alloc);\n    REDISMODULE_GET_API(Calloc);\n    REDISMODULE_GET_API(Free);\n    REDISMODULE_GET_API(Realloc);\n    REDISMODULE_GET_API(Strdup);\n    REDISMODULE_GET_API(CreateCommand);\n    REDISMODULE_GET_API(SetModuleAttribs);\n    REDISMODULE_GET_API(IsModuleNameBusy);\n    REDISMODULE_GET_API(WrongArity);\n    REDISMODULE_GET_API(ReplyWithLongLong);\n    REDISMODULE_GET_API(ReplyWithError);\n    REDISMODULE_GET_API(ReplyWithSimpleString);\n    REDISMODULE_GET_API(ReplyWithArray);\n    REDISMODULE_GET_API(ReplyWithNullArray);\n    REDISMODULE_GET_API(ReplyWithEmptyArray);\n    REDISMODULE_GET_API(ReplySetArrayLength);\n    REDISMODULE_GET_API(ReplyWithStringBuffer);\n    REDISMODULE_GET_API(ReplyWithCString);\n    REDISMODULE_GET_API(ReplyWithString);\n    REDISMODULE_GET_API(ReplyWithEmptyString);\n    REDISMODULE_GET_API(ReplyWithVerbatimString);\n    REDISMODULE_GET_API(ReplyWithNull);\n    REDISMODULE_GET_API(ReplyWithCallReply);\n    REDISMODULE_GET_API(ReplyWithDouble);\n    REDISMODULE_GET_API(ReplyWithLongDouble);\n    REDISMODULE_GET_API(GetSelectedDb);\n    REDISMODULE_GET_API(SelectDb);\n    REDISMODULE_GET_API(OpenKey);\n    REDISMODULE_GET_API(CloseKey);\n    REDISMODULE_GET_API(KeyType);\n    REDISMODULE_GET_API(ValueLength);\n    REDISMODULE_GET_API(ListPush);\n    REDISMODULE_GET_API(ListPop);\n    REDISMODULE_GET_API(StringToLongLong);\n    REDISMODULE_GET_API(StringToDouble);\n    REDISMODULE_GET_API(StringToLongDouble);\n    REDISMODULE_GET_API(StringToStreamID);\n    REDISMODULE_GET_API(Call);\n    REDISMODULE_GET_API(CallReplyProto);\n    REDISMODULE_GET_API(FreeCallReply);\n    REDISMODULE_GET_API(CallReplyInteger);\n    REDISMODULE_GET_API(CallReplyType);\n    REDISMODULE_GET_API(CallReplyLength);\n    REDISMODULE_GET_API(CallReplyArrayElement);\n    REDISMODULE_GET_API(CallReplyStringPtr);\n    REDISMODULE_GET_API(CreateStringFromCallReply);\n    REDISMODULE_GET_API(CreateString);\n    REDISMODULE_GET_API(CreateStringFromLongLong);\n    REDISMODULE_GET_API(CreateStringFromDouble);\n    REDISMODULE_GET_API(CreateStringFromLongDouble);\n    REDISMODULE_GET_API(CreateStringFromString);\n    REDISMODULE_GET_API(CreateStringFromStreamID);\n    REDISMODULE_GET_API(CreateStringPrintf);\n    REDISMODULE_GET_API(FreeString);\n    REDISMODULE_GET_API(StringPtrLen);\n    REDISMODULE_GET_API(AutoMemory);\n    REDISMODULE_GET_API(Replicate);\n    REDISMODULE_GET_API(ReplicateVerbatim);\n    REDISMODULE_GET_API(DeleteKey);\n    REDISMODULE_GET_API(UnlinkKey);\n    REDISMODULE_GET_API(StringSet);\n    REDISMODULE_GET_API(StringDMA);\n    REDISMODULE_GET_API(StringTruncate);\n    REDISMODULE_GET_API(GetExpire);\n    REDISMODULE_GET_API(SetExpire);\n    REDISMODULE_GET_API(GetAbsExpire);\n    REDISMODULE_GET_API(SetAbsExpire);\n    REDISMODULE_GET_API(ResetDataset);\n    REDISMODULE_GET_API(DbSize);\n    REDISMODULE_GET_API(RandomKey);\n    REDISMODULE_GET_API(ZsetAdd);\n    REDISMODULE_GET_API(ZsetIncrby);\n    REDISMODULE_GET_API(ZsetScore);\n    REDISMODULE_GET_API(ZsetRem);\n    REDISMODULE_GET_API(ZsetRangeStop);\n    REDISMODULE_GET_API(ZsetFirstInScoreRange);\n    REDISMODULE_GET_API(ZsetLastInScoreRange);\n    REDISMODULE_GET_API(ZsetFirstInLexRange);\n    REDISMODULE_GET_API(ZsetLastInLexRange);\n    REDISMODULE_GET_API(ZsetRangeCurrentElement);\n    REDISMODULE_GET_API(ZsetRangeNext);\n    REDISMODULE_GET_API(ZsetRangePrev);\n    REDISMODULE_GET_API(ZsetRangeEndReached);\n    REDISMODULE_GET_API(HashSet);\n    REDISMODULE_GET_API(HashGet);\n    REDISMODULE_GET_API(StreamAdd);\n    REDISMODULE_GET_API(StreamDelete);\n    REDISMODULE_GET_API(StreamIteratorStart);\n    REDISMODULE_GET_API(StreamIteratorStop);\n    REDISMODULE_GET_API(StreamIteratorNextID);\n    REDISMODULE_GET_API(StreamIteratorNextField);\n    REDISMODULE_GET_API(StreamIteratorDelete);\n    REDISMODULE_GET_API(StreamTrimByLength);\n    REDISMODULE_GET_API(StreamTrimByID);\n    REDISMODULE_GET_API(IsKeysPositionRequest);\n    REDISMODULE_GET_API(KeyAtPos);\n    REDISMODULE_GET_API(GetClientId);\n    REDISMODULE_GET_API(GetClientUserNameById);\n    REDISMODULE_GET_API(GetContextFlags);\n    REDISMODULE_GET_API(AvoidReplicaTraffic);\n    REDISMODULE_GET_API(PoolAlloc);\n    REDISMODULE_GET_API(CreateDataType);\n    REDISMODULE_GET_API(ModuleTypeSetValue);\n    REDISMODULE_GET_API(ModuleTypeReplaceValue);\n    REDISMODULE_GET_API(ModuleTypeGetType);\n    REDISMODULE_GET_API(ModuleTypeGetValue);\n    REDISMODULE_GET_API(IsIOError);\n    REDISMODULE_GET_API(SetModuleOptions);\n    REDISMODULE_GET_API(SignalModifiedKey);\n    REDISMODULE_GET_API(SaveUnsigned);\n    REDISMODULE_GET_API(LoadUnsigned);\n    REDISMODULE_GET_API(SaveSigned);\n    REDISMODULE_GET_API(LoadSigned);\n    REDISMODULE_GET_API(SaveString);\n    REDISMODULE_GET_API(SaveStringBuffer);\n    REDISMODULE_GET_API(LoadString);\n    REDISMODULE_GET_API(LoadStringBuffer);\n    REDISMODULE_GET_API(SaveDouble);\n    REDISMODULE_GET_API(LoadDouble);\n    REDISMODULE_GET_API(SaveFloat);\n    REDISMODULE_GET_API(LoadFloat);\n    REDISMODULE_GET_API(SaveLongDouble);\n    REDISMODULE_GET_API(LoadLongDouble);\n    REDISMODULE_GET_API(SaveDataTypeToString);\n    REDISMODULE_GET_API(LoadDataTypeFromString);\n    REDISMODULE_GET_API(EmitAOF);\n    REDISMODULE_GET_API(Log);\n    REDISMODULE_GET_API(LogIOError);\n    REDISMODULE_GET_API(_Assert);\n    REDISMODULE_GET_API(LatencyAddSample);\n    REDISMODULE_GET_API(StringAppendBuffer);\n    REDISMODULE_GET_API(RetainString);\n    REDISMODULE_GET_API(HoldString);\n    REDISMODULE_GET_API(StringCompare);\n    REDISMODULE_GET_API(GetContextFromIO);\n    REDISMODULE_GET_API(GetKeyNameFromIO);\n    REDISMODULE_GET_API(GetKeyNameFromModuleKey);\n    REDISMODULE_GET_API(Milliseconds);\n    REDISMODULE_GET_API(DigestAddStringBuffer);\n    REDISMODULE_GET_API(DigestAddLongLong);\n    REDISMODULE_GET_API(DigestEndSequence);\n    REDISMODULE_GET_API(CreateDict);\n    REDISMODULE_GET_API(FreeDict);\n    REDISMODULE_GET_API(DictSize);\n    REDISMODULE_GET_API(DictSetC);\n    REDISMODULE_GET_API(DictReplaceC);\n    REDISMODULE_GET_API(DictSet);\n    REDISMODULE_GET_API(DictReplace);\n    REDISMODULE_GET_API(DictGetC);\n    REDISMODULE_GET_API(DictGet);\n    REDISMODULE_GET_API(DictDelC);\n    REDISMODULE_GET_API(DictDel);\n    REDISMODULE_GET_API(DictIteratorStartC);\n    REDISMODULE_GET_API(DictIteratorStart);\n    REDISMODULE_GET_API(DictIteratorStop);\n    REDISMODULE_GET_API(DictIteratorReseekC);\n    REDISMODULE_GET_API(DictIteratorReseek);\n    REDISMODULE_GET_API(DictNextC);\n    REDISMODULE_GET_API(DictPrevC);\n    REDISMODULE_GET_API(DictNext);\n    REDISMODULE_GET_API(DictPrev);\n    REDISMODULE_GET_API(DictCompare);\n    REDISMODULE_GET_API(DictCompareC);\n    REDISMODULE_GET_API(RegisterInfoFunc);\n    REDISMODULE_GET_API(InfoAddSection);\n    REDISMODULE_GET_API(InfoBeginDictField);\n    REDISMODULE_GET_API(InfoEndDictField);\n    REDISMODULE_GET_API(InfoAddFieldString);\n    REDISMODULE_GET_API(InfoAddFieldCString);\n    REDISMODULE_GET_API(InfoAddFieldDouble);\n    REDISMODULE_GET_API(InfoAddFieldLongLong);\n    REDISMODULE_GET_API(InfoAddFieldULongLong);\n    REDISMODULE_GET_API(GetServerInfo);\n    REDISMODULE_GET_API(FreeServerInfo);\n    REDISMODULE_GET_API(ServerInfoGetField);\n    REDISMODULE_GET_API(ServerInfoGetFieldC);\n    REDISMODULE_GET_API(ServerInfoGetFieldSigned);\n    REDISMODULE_GET_API(ServerInfoGetFieldUnsigned);\n    REDISMODULE_GET_API(ServerInfoGetFieldDouble);\n    REDISMODULE_GET_API(GetClientInfoById);\n    REDISMODULE_GET_API(PublishMessage);\n    REDISMODULE_GET_API(SubscribeToServerEvent);\n    REDISMODULE_GET_API(SetLRU);\n    REDISMODULE_GET_API(GetLRU);\n    REDISMODULE_GET_API(SetLFU);\n    REDISMODULE_GET_API(GetLFU);\n    REDISMODULE_GET_API(BlockClientOnKeys);\n    REDISMODULE_GET_API(SignalKeyAsReady);\n    REDISMODULE_GET_API(GetBlockedClientReadyKey);\n    REDISMODULE_GET_API(ScanCursorCreate);\n    REDISMODULE_GET_API(ScanCursorRestart);\n    REDISMODULE_GET_API(ScanCursorDestroy);\n    REDISMODULE_GET_API(Scan);\n    REDISMODULE_GET_API(ScanKey);\n    REDISMODULE_GET_API(GetContextFlagsAll);\n    REDISMODULE_GET_API(GetKeyspaceNotificationFlagsAll);\n    REDISMODULE_GET_API(IsSubEventSupported);\n    REDISMODULE_GET_API(GetServerVersion);\n    REDISMODULE_GET_API(GetTypeMethodVersion);\n\n#ifdef REDISMODULE_EXPERIMENTAL_API\n    REDISMODULE_GET_API(GetThreadSafeContext);\n    REDISMODULE_GET_API(GetDetachedThreadSafeContext);\n    REDISMODULE_GET_API(FreeThreadSafeContext);\n    REDISMODULE_GET_API(ThreadSafeContextLock);\n    REDISMODULE_GET_API(ThreadSafeContextTryLock);\n    REDISMODULE_GET_API(ThreadSafeContextUnlock);\n    REDISMODULE_GET_API(BlockClient);\n    REDISMODULE_GET_API(UnblockClient);\n    REDISMODULE_GET_API(IsBlockedReplyRequest);\n    REDISMODULE_GET_API(IsBlockedTimeoutRequest);\n    REDISMODULE_GET_API(GetBlockedClientPrivateData);\n    REDISMODULE_GET_API(GetBlockedClientHandle);\n    REDISMODULE_GET_API(AbortBlock);\n    REDISMODULE_GET_API(BlockedClientMeasureTimeStart);\n    REDISMODULE_GET_API(BlockedClientMeasureTimeEnd);\n    REDISMODULE_GET_API(SetDisconnectCallback);\n    REDISMODULE_GET_API(SubscribeToKeyspaceEvents);\n    REDISMODULE_GET_API(NotifyKeyspaceEvent);\n    REDISMODULE_GET_API(GetNotifyKeyspaceEvents);\n    REDISMODULE_GET_API(BlockedClientDisconnected);\n    REDISMODULE_GET_API(RegisterClusterMessageReceiver);\n    REDISMODULE_GET_API(SendClusterMessage);\n    REDISMODULE_GET_API(GetClusterNodeInfo);\n    REDISMODULE_GET_API(GetClusterNodesList);\n    REDISMODULE_GET_API(FreeClusterNodesList);\n    REDISMODULE_GET_API(CreateTimer);\n    REDISMODULE_GET_API(StopTimer);\n    REDISMODULE_GET_API(GetTimerInfo);\n    REDISMODULE_GET_API(GetMyClusterID);\n    REDISMODULE_GET_API(GetClusterSize);\n    REDISMODULE_GET_API(GetRandomBytes);\n    REDISMODULE_GET_API(GetRandomHexChars);\n    REDISMODULE_GET_API(SetClusterFlags);\n    REDISMODULE_GET_API(ExportSharedAPI);\n    REDISMODULE_GET_API(GetSharedAPI);\n    REDISMODULE_GET_API(RegisterCommandFilter);\n    REDISMODULE_GET_API(UnregisterCommandFilter);\n    REDISMODULE_GET_API(CommandFilterArgsCount);\n    REDISMODULE_GET_API(CommandFilterArgGet);\n    REDISMODULE_GET_API(CommandFilterArgInsert);\n    REDISMODULE_GET_API(CommandFilterArgReplace);\n    REDISMODULE_GET_API(CommandFilterArgDelete);\n    REDISMODULE_GET_API(Fork);\n    REDISMODULE_GET_API(SendChildHeartbeat);\n    REDISMODULE_GET_API(ExitFromChild);\n    REDISMODULE_GET_API(KillForkChild);\n    REDISMODULE_GET_API(GetUsedMemoryRatio);\n    REDISMODULE_GET_API(MallocSize);\n    REDISMODULE_GET_API(CreateModuleUser);\n    REDISMODULE_GET_API(FreeModuleUser);\n    REDISMODULE_GET_API(SetModuleUserACL);\n    REDISMODULE_GET_API(DeauthenticateAndCloseClient);\n    REDISMODULE_GET_API(AuthenticateClientWithACLUser);\n    REDISMODULE_GET_API(AuthenticateClientWithUser);\n    REDISMODULE_GET_API(GetClientCertificate);\n    REDISMODULE_GET_API(GetCommandKeys);\n    REDISMODULE_GET_API(GetCurrentCommandName);\n    REDISMODULE_GET_API(RegisterDefragFunc);\n    REDISMODULE_GET_API(DefragAlloc);\n    REDISMODULE_GET_API(DefragRedisModuleString);\n    REDISMODULE_GET_API(DefragShouldStop);\n    REDISMODULE_GET_API(DefragCursorSet);\n    REDISMODULE_GET_API(DefragCursorGet);\n#endif\n\n    if (RedisModule_IsModuleNameBusy && RedisModule_IsModuleNameBusy(name)) return REDISMODULE_ERR;\n    RedisModule_SetModuleAttribs(ctx,name,ver,apiver);\n    return REDISMODULE_OK;\n}\n\n#define RedisModule_Assert(_e) ((_e)?(void)0 : (RedisModule__Assert(#_e,__FILE__,__LINE__),exit(1)))\n\n#define RMAPI_FUNC_SUPPORTED(func) (func != NULL)\n\n#else\n\n/* Things only defined for the modules core, not exported to modules\n * including this file. */\n#define RedisModuleString robj\n\n#endif /* REDISMODULE_CORE */\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif /* REDISMODULE_H */\n"
  },
  {
    "path": "src/monotonic.c",
    "content": "#include \"monotonic.h\"\n#include <stddef.h>\n#include <stdlib.h>\n#include <stdio.h>\n#include <time.h>\n\n#undef NDEBUG\n#include <assert.h>\n\n\n/* The function pointer for clock retrieval.  */\nmonotime (*getMonotonicUs)(void) = NULL;\n\nstatic char monotonic_info_string[32];\n\n\n/* Using the processor clock (aka TSC on x86) can provide improved performance\n * throughout Redis wherever the monotonic clock is used.  The processor clock\n * is significantly faster than calling 'clock_getting' (POSIX).  While this is\n * generally safe on modern systems, this link provides additional information\n * about use of the x86 TSC: http://oliveryang.net/2015/09/pitfalls-of-TSC-usage\n *\n * To use the processor clock, either uncomment this line, or build with\n *   CFLAGS=\"-DUSE_PROCESSOR_CLOCK\"\n#define USE_PROCESSOR_CLOCK\n */\n\n\n#if defined(USE_PROCESSOR_CLOCK) && defined(__x86_64__) && defined(__linux__)\n#include <regex.h>\n#include <x86intrin.h>\n\nstatic long mono_ticksPerMicrosecond = 0;\n\nstatic monotime getMonotonicUs_x86() {\n    return __rdtsc() / mono_ticksPerMicrosecond;\n}\n\nstatic void monotonicInit_x86linux() {\n    const int bufflen = 256;\n    char buf[bufflen];\n    regex_t cpuGhzRegex, constTscRegex;\n    const size_t nmatch = 2;\n    regmatch_t pmatch[nmatch];\n    int constantTsc = 0;\n    int rc;\n\n    /* Determine the number of TSC ticks in a micro-second.  This is\n     * a constant value matching the standard speed of the processor.\n     * On modern processors, this speed remains constant even though\n     * the actual clock speed varies dynamically for each core.  */\n    rc = regcomp(&cpuGhzRegex, \"^model name\\\\s+:.*@ ([0-9.]+)GHz\", REG_EXTENDED);\n    assert(rc == 0);\n\n    /* Also check that the constant_tsc flag is present.  (It should be\n     * unless this is a really old CPU.  */\n    rc = regcomp(&constTscRegex, \"^flags\\\\s+:.* constant_tsc\", REG_EXTENDED);\n    assert(rc == 0);\n\n    FILE *cpuinfo = fopen(\"/proc/cpuinfo\", \"r\");\n    if (cpuinfo != NULL) {\n        while (fgets(buf, bufflen, cpuinfo) != NULL) {\n            if (regexec(&cpuGhzRegex, buf, nmatch, pmatch, 0) == 0) {\n                buf[pmatch[1].rm_eo] = '\\0';\n                double ghz = atof(&buf[pmatch[1].rm_so]);\n                mono_ticksPerMicrosecond = (long)(ghz * 1000);\n                break;\n            }\n        }\n        while (fgets(buf, bufflen, cpuinfo) != NULL) {\n            if (regexec(&constTscRegex, buf, nmatch, pmatch, 0) == 0) {\n                constantTsc = 1;\n                break;\n            }\n        }\n\n        fclose(cpuinfo);\n    }\n    regfree(&cpuGhzRegex);\n    regfree(&constTscRegex);\n\n    if (mono_ticksPerMicrosecond == 0) {\n        fprintf(stderr, \"monotonic: x86 linux, unable to determine clock rate\");\n        return;\n    }\n    if (!constantTsc) {\n        fprintf(stderr, \"monotonic: x86 linux, 'constant_tsc' flag not present\");\n        return;\n    }\n\n    snprintf(monotonic_info_string, sizeof(monotonic_info_string),\n            \"X86 TSC @ %ld ticks/us\", mono_ticksPerMicrosecond);\n    getMonotonicUs = getMonotonicUs_x86;\n}\n#endif\n\n\n#if defined(USE_PROCESSOR_CLOCK) && defined(__aarch64__)\nstatic long mono_ticksPerMicrosecond = 0;\n\n/* Read the clock value.  */\nstatic inline uint64_t __cntvct() {\n    uint64_t virtual_timer_value;\n    __asm__ volatile(\"mrs %0, cntvct_el0\" : \"=r\"(virtual_timer_value));\n    return virtual_timer_value;\n}\n\n/* Read the Count-timer Frequency.  */\nstatic inline uint32_t cntfrq_hz() {\n    uint64_t virtual_freq_value;\n    __asm__ volatile(\"mrs %0, cntfrq_el0\" : \"=r\"(virtual_freq_value));\n    return (uint32_t)virtual_freq_value;    /* top 32 bits are reserved */\n}\n\nstatic monotime getMonotonicUs_aarch64() {\n    return __cntvct() / mono_ticksPerMicrosecond;\n}\n\nstatic void monotonicInit_aarch64() {\n    mono_ticksPerMicrosecond = (long)cntfrq_hz() / 1000L / 1000L;\n    if (mono_ticksPerMicrosecond == 0) {\n        fprintf(stderr, \"monotonic: aarch64, unable to determine clock rate\");\n        return;\n    }\n\n    snprintf(monotonic_info_string, sizeof(monotonic_info_string),\n            \"ARM CNTVCT @ %ld ticks/us\", mono_ticksPerMicrosecond);\n    getMonotonicUs = getMonotonicUs_aarch64;\n}\n#endif\n\n\nstatic monotime getMonotonicUs_posix() {\n    /* clock_gettime() is specified in POSIX.1b (1993).  Even so, some systems\n     * did not support this until much later.  CLOCK_MONOTONIC is technically\n     * optional and may not be supported - but it appears to be universal.\n     * If this is not supported, provide a system-specific alternate version.  */\n    struct timespec ts;\n    clock_gettime(CLOCK_MONOTONIC, &ts);\n    return ((uint64_t)ts.tv_sec) * 1000000 + ts.tv_nsec / 1000;\n}\n\nstatic void monotonicInit_posix() {\n    /* Ensure that CLOCK_MONOTONIC is supported.  This should be supported\n     * on any reasonably current OS.  If the assertion below fails, provide\n     * an appropriate alternate implementation.  */\n    struct timespec ts;\n    int rc = clock_gettime(CLOCK_MONOTONIC, &ts);\n    assert(rc == 0);\n\n    snprintf(monotonic_info_string, sizeof(monotonic_info_string),\n            \"POSIX clock_gettime\");\n    getMonotonicUs = getMonotonicUs_posix;\n}\n\n\n\nconst char * monotonicInit() {\n    #if defined(USE_PROCESSOR_CLOCK) && defined(__x86_64__) && defined(__linux__)\n    if (getMonotonicUs == NULL) monotonicInit_x86linux();\n    #endif\n\n    #if defined(USE_PROCESSOR_CLOCK) && defined(__aarch64__)\n    if (getMonotonicUs == NULL) monotonicInit_aarch64();\n    #endif\n\n    if (getMonotonicUs == NULL) monotonicInit_posix();\n\n    return monotonic_info_string;\n}\n"
  },
  {
    "path": "src/monotonic.h",
    "content": "#ifndef __MONOTONIC_H\n#define __MONOTONIC_H\n/* The monotonic clock is an always increasing clock source.  It is unrelated to\n * the actual time of day and should only be used for relative timings.  The\n * monotonic clock is also not guaranteed to be chronologically precise; there\n * may be slight skew/shift from a precise clock.\n *\n * Depending on system architecture, the monotonic time may be able to be\n * retrieved much faster than a normal clock source by using an instruction\n * counter on the CPU.  On x86 architectures (for example), the RDTSC\n * instruction is a very fast clock source for this purpose.\n */\n\n#include \"fmacros.h\"\n#include <stdint.h>\n#include <unistd.h>\n\n/* A counter in micro-seconds.  The 'monotime' type is provided for variables\n * holding a monotonic time.  This will help distinguish & document that the\n * variable is associated with the monotonic clock and should not be confused\n * with other types of time.*/\ntypedef uint64_t monotime;\n\n/* Retrieve counter of micro-seconds relative to an arbitrary point in time.  */\nextern monotime (*getMonotonicUs)(void);\n\n\n/* Call once at startup to initialize the monotonic clock.  Though this only\n * needs to be called once, it may be called additional times without impact.\n * Returns a printable string indicating the type of clock initialized.\n * (The returned string is static and doesn't need to be freed.)  */\n#ifdef __cplusplus\nextern \"C\" \n#endif\nconst char * monotonicInit();\n\n\n/* Functions to measure elapsed time.  Example:\n *     monotime myTimer;\n *     elapsedStart(&myTimer);\n *     while (elapsedMs(myTimer) < 10) {} // loops for 10ms\n */\nstatic inline void elapsedStart(monotime *start_time) {\n    *start_time = getMonotonicUs();\n}\n\nstatic inline uint64_t elapsedUs(monotime start_time) {\n    return getMonotonicUs() - start_time;\n}\n\nstatic inline uint64_t elapsedMs(monotime start_time) {\n    return elapsedUs(start_time) / 1000;\n}\n\n#endif\n"
  },
  {
    "path": "src/motd.cpp",
    "content": "#ifdef CLIENT\nextern \"C\" {\n#include \"sdscompat.h\"\n#include <sds.h>\n}\n#else\n#include \"sds.h\"\n#endif\n#include <cstring>\n#include <unistd.h>\n#include <sys/types.h>\n#include <pwd.h>\n#include <sys/stat.h>\n#include \"motd.h\"\n\n/*------------------------------------------------------------------------------\n * Message of the day\n *--------------------------------------------------------------------------- */\n#ifdef MOTD\n#include <curl/curl.h> \n\n#if !defined(USE_SYSTEM_HIREDIS) && defined(CLIENT)\nextern \"C\" {\n__attribute__ ((weak)) hisds hi_sdscatlen(hisds s, const void *t, size_t len) {\n    return sdscatlen(s, t, len);\n}\n__attribute__ ((weak)) hisds hi_sdscat(hisds s, const char *t) {\n    return sdscat(s, t);\n}\n}\n#endif\n\nstatic const char *szMotdCachePath()\n{\n    static sds sdsMotdCachePath = NULL;\n    if (sdsMotdCachePath != NULL)\n        return sdsMotdCachePath;\n\n    struct passwd *pw = getpwuid(getuid());\n    if (pw == NULL)\n        return \"\";\n    const char *homedir = pw->pw_dir;\n    sdsMotdCachePath = sdsnew(homedir);\n    sdsMotdCachePath = sdscat(sdsMotdCachePath, motd_cache_file);\n    return sdsMotdCachePath;\n}\nstatic size_t motd_write_callback(void *ptr, size_t size, size_t nmemb, sds *str)\n{\n    *str = sdscatlen(*str, ptr, size*nmemb);\n    return (size*nmemb);\n}\n\nstatic char *fetchMOTDFromCache()\n{\n    struct stat attrib;\n    if (stat(szMotdCachePath(), &attrib) != 0)\n        return NULL;\n    time_t t = attrib.st_mtim.tv_sec;\n    time_t now = time(NULL);\n    if ((now - t) < 14400)\n    {\n        // If our cache was updated no more than 4 hours ago use it instead of fetching the MOTD\n        FILE *pf = fopen(szMotdCachePath(), \"rb\");\n        if (pf == NULL)\n            return NULL;\n        fseek(pf, 0L, SEEK_END);\n        long cb = ftell(pf);\n        fseek(pf, 0L, SEEK_SET);    // rewind\n        sds str = sdsnewlen(NULL, cb);\n        size_t cbRead = fread(str, 1, cb, pf);\n        fclose(pf);\n        if ((long)cbRead != cb)\n        {\n            sdsfree(str);\n            return NULL;\n        }\n        return str;\n    }\n    return NULL;\n}\n\nstatic void setMOTDCache(const char *sz)\n{\n    FILE *pf = fopen(szMotdCachePath(), \"wb\");\n    if (pf == NULL)\n        return;\n    size_t celem = fwrite(sz, strlen(sz), 1, pf);\n    (void)celem;    // best effort\n    fclose(pf);\n}\n\nextern \"C\" char *fetchMOTD(int cache, int enable_motd)\n{\n    sds str;\n    CURL *curl;\n    CURLcode res;\n\n    /* Do not try the CURL if the motd is disabled*/\n    if (!enable_motd) {\n        return NULL;\n    }\n    /* First try and get the string from the cache */\n    if (cache) {\n        str = fetchMOTDFromCache();\n        if (str != NULL)\n            return str;\n    }\n\n    str = sdsnew(\"\");\n    curl = curl_easy_init();\n    if(curl) {\n        curl_easy_setopt(curl, CURLOPT_URL, motd_url);\n        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // follow redirects\n        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 2); // take no more than two seconds\n        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, motd_write_callback);\n        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &str);\n\n        /* Perform the request, res will get the return code */ \n        res = curl_easy_perform(curl);\n        /* Check for errors */ \n        if(res != CURLE_OK)\n        {\n            sdsfree(str);\n            str = NULL;\n        }\n        else\n        {\n            long response_code;\n            curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);\n            if ((response_code / 100) != 2)\n            {\n                // An error code not in the 200s implies an error\n                sdsfree(str);\n                str = NULL;\n            }\n        }\n\n        /* always cleanup */ \n        curl_easy_cleanup(curl);\n\n        if (str != NULL && cache)\n            setMOTDCache(str);\n    }\n    return str;\n}\n\n#else\n\nextern \"C\" char *fetchMOTD(int /* cache */, int /* enable_motd */)\n{\n    return NULL;\n}\n\n#endif\n\nvoid freeMOTD(const char *sz) {\n    sdsfree((sds)sz);\n}\n"
  },
  {
    "path": "src/motd.h",
    "content": "#pragma once\n\nextern const char *motd_url;\nextern const char *motd_cache_file;\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\nchar *fetchMOTD(int fCache, int enable_motd);\nvoid freeMOTD(const char*);\n#ifdef __cplusplus\n}\n#endif\n"
  },
  {
    "path": "src/mt19937-64.c",
    "content": "/*\n   A C-program for MT19937-64 (2004/9/29 version).\n   Coded by Takuji Nishimura and Makoto Matsumoto.\n\n   This is a 64-bit version of Mersenne Twister pseudorandom number\n   generator.\n\n   Before using, initialize the state by using init_genrand64(seed)\n   or init_by_array64(init_key, key_length).\n\n   Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura,\n   All rights reserved.\n\n   Redistribution and use in source and binary forms, with or without\n   modification, are permitted provided that the following conditions\n   are met:\n\n     1. Redistributions of source code must retain the above copyright\n        notice, this list of conditions and the following disclaimer.\n\n     2. Redistributions in binary form must reproduce the above copyright\n        notice, this list of conditions and the following disclaimer in the\n        documentation and/or other materials provided with the distribution.\n\n     3. The names of its contributors may not be used to endorse or promote\n        products derived from this software without specific prior written\n        permission.\n\n   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR\n   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\n   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\n   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\n   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\n   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\n   References:\n   T. Nishimura, ``Tables of 64-bit Mersenne Twisters''\n     ACM Transactions on Modeling and\n     Computer Simulation 10. (2000) 348--357.\n   M. Matsumoto and T. Nishimura,\n     ``Mersenne Twister: a 623-dimensionally equidistributed\n       uniform pseudorandom number generator''\n     ACM Transactions on Modeling and\n     Computer Simulation 8. (Jan. 1998) 3--30.\n\n   Any feedback is very welcome.\n   http://www.math.hiroshima-u.ac.jp/~m-mat/MT/emt.html\n   email: m-mat @ math.sci.hiroshima-u.ac.jp (remove spaces)\n*/\n\n\n#include \"mt19937-64.h\"\n#include <stdio.h>\n\n#define NN 312\n#define MM 156\n#define MATRIX_A 0xB5026F5AA96619E9ULL\n#define UM 0xFFFFFFFF80000000ULL /* Most significant 33 bits */\n#define LM 0x7FFFFFFFULL /* Least significant 31 bits */\n\n\n/* The array for the state vector */\nstatic unsigned long long mt[NN];\n/* mti==NN+1 means mt[NN] is not initialized */\nstatic int mti=NN+1;\n\n/* initializes mt[NN] with a seed */\nvoid init_genrand64(unsigned long long seed)\n{\n    mt[0] = seed;\n    for (mti=1; mti<NN; mti++)\n        mt[mti] =  (6364136223846793005ULL * (mt[mti-1] ^ (mt[mti-1] >> 62)) + mti);\n}\n\n/* initialize by an array with array-length */\n/* init_key is the array for initializing keys */\n/* key_length is its length */\nvoid init_by_array64(unsigned long long init_key[],\n                     unsigned long long key_length)\n{\n    unsigned long long i, j, k;\n    init_genrand64(19650218ULL);\n    i=1; j=0;\n    k = (NN>key_length ? NN : key_length);\n    for (; k; k--) {\n        mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 62)) * 3935559000370003845ULL))\n          + init_key[j] + j; /* non linear */\n        i++; j++;\n        if (i>=NN) { mt[0] = mt[NN-1]; i=1; }\n        if (j>=key_length) j=0;\n    }\n    for (k=NN-1; k; k--) {\n        mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 62)) * 2862933555777941757ULL))\n          - i; /* non linear */\n        i++;\n        if (i>=NN) { mt[0] = mt[NN-1]; i=1; }\n    }\n\n    mt[0] = 1ULL << 63; /* MSB is 1; assuring non-zero initial array */\n}\n\n/* generates a random number on [0, 2^64-1]-interval */\nunsigned long long genrand64_int64(void)\n{\n    int i;\n    unsigned long long x;\n    static unsigned long long mag01[2]={0ULL, MATRIX_A};\n\n    if (mti >= NN) { /* generate NN words at one time */\n\n        /* if init_genrand64() has not been called, */\n        /* a default initial seed is used     */\n        if (mti == NN+1)\n            init_genrand64(5489ULL);\n\n        for (i=0;i<NN-MM;i++) {\n            x = (mt[i]&UM)|(mt[i+1]&LM);\n            mt[i] = mt[i+MM] ^ (x>>1) ^ mag01[(int)(x&1ULL)];\n        }\n        for (;i<NN-1;i++) {\n            x = (mt[i]&UM)|(mt[i+1]&LM);\n            mt[i] = mt[i+(MM-NN)] ^ (x>>1) ^ mag01[(int)(x&1ULL)];\n        }\n        x = (mt[NN-1]&UM)|(mt[0]&LM);\n        mt[NN-1] = mt[MM-1] ^ (x>>1) ^ mag01[(int)(x&1ULL)];\n\n        mti = 0;\n    }\n\n    x = mt[mti++];\n\n    x ^= (x >> 29) & 0x5555555555555555ULL;\n    x ^= (x << 17) & 0x71D67FFFEDA60000ULL;\n    x ^= (x << 37) & 0xFFF7EEE000000000ULL;\n    x ^= (x >> 43);\n\n    return x;\n}\n\n/* generates a random number on [0, 2^63-1]-interval */\nlong long genrand64_int63(void)\n{\n    return (long long)(genrand64_int64() >> 1);\n}\n\n/* generates a random number on [0,1]-real-interval */\ndouble genrand64_real1(void)\n{\n    return (genrand64_int64() >> 11) * (1.0/9007199254740991.0);\n}\n\n/* generates a random number on [0,1)-real-interval */\ndouble genrand64_real2(void)\n{\n    return (genrand64_int64() >> 11) * (1.0/9007199254740992.0);\n}\n\n/* generates a random number on (0,1)-real-interval */\ndouble genrand64_real3(void)\n{\n    return ((genrand64_int64() >> 12) + 0.5) * (1.0/4503599627370496.0);\n}\n\n#ifdef MT19937_64_MAIN\nint main(void)\n{\n    int i;\n    unsigned long long init[4]={0x12345ULL, 0x23456ULL, 0x34567ULL, 0x45678ULL}, length=4;\n    init_by_array64(init, length);\n    printf(\"1000 outputs of genrand64_int64()\\n\");\n    for (i=0; i<1000; i++) {\n      printf(\"%20llu \", genrand64_int64());\n      if (i%5==4) printf(\"\\n\");\n    }\n    printf(\"\\n1000 outputs of genrand64_real2()\\n\");\n    for (i=0; i<1000; i++) {\n      printf(\"%10.8f \", genrand64_real2());\n      if (i%5==4) printf(\"\\n\");\n    }\n    return 0;\n}\n#endif\n"
  },
  {
    "path": "src/mt19937-64.h",
    "content": "/*\n   A C-program for MT19937-64 (2004/9/29 version).\n   Coded by Takuji Nishimura and Makoto Matsumoto.\n\n   This is a 64-bit version of Mersenne Twister pseudorandom number\n   generator.\n\n   Before using, initialize the state by using init_genrand64(seed)\n   or init_by_array64(init_key, key_length).\n\n   Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura,\n   All rights reserved.\n\n   Redistribution and use in source and binary forms, with or without\n   modification, are permitted provided that the following conditions\n   are met:\n\n     1. Redistributions of source code must retain the above copyright\n        notice, this list of conditions and the following disclaimer.\n\n     2. Redistributions in binary form must reproduce the above copyright\n        notice, this list of conditions and the following disclaimer in the\n        documentation and/or other materials provided with the distribution.\n\n     3. The names of its contributors may not be used to endorse or promote\n        products derived from this software without specific prior written\n        permission.\n\n   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n   \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR\n   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\n   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\n   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\n   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\n   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\n   References:\n   T. Nishimura, ``Tables of 64-bit Mersenne Twisters''\n     ACM Transactions on Modeling and\n     Computer Simulation 10. (2000) 348--357.\n   M. Matsumoto and T. Nishimura,\n     ``Mersenne Twister: a 623-dimensionally equidistributed\n       uniform pseudorandom number generator''\n     ACM Transactions on Modeling and\n     Computer Simulation 8. (Jan. 1998) 3--30.\n\n   Any feedback is very welcome.\n   http://www.math.hiroshima-u.ac.jp/~m-mat/MT/emt.html\n   email: m-mat @ math.sci.hiroshima-u.ac.jp (remove spaces)\n*/\n\n#ifndef __MT19937_64_H\n#define __MT19937_64_H\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/* initializes mt[NN] with a seed */\nvoid init_genrand64(unsigned long long seed);\n\n/* initialize by an array with array-length */\n/* init_key is the array for initializing keys */\n/* key_length is its length */\nvoid init_by_array64(unsigned long long init_key[],\n                     unsigned long long key_length);\n\n/* generates a random number on [0, 2^64-1]-interval */\nunsigned long long genrand64_int64(void);\n\n\n/* generates a random number on [0, 2^63-1]-interval */\nlong long genrand64_int63(void);\n\n/* generates a random number on [0,1]-real-interval */\ndouble genrand64_real1(void);\n\n/* generates a random number on [0,1)-real-interval */\ndouble genrand64_real2(void);\n\n/* generates a random number on (0,1)-real-interval */\ndouble genrand64_real3(void);\n\n/* generates a random number on (0,1]-real-interval */\ndouble genrand64_real4(void);\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n"
  },
  {
    "path": "src/multi.cpp",
    "content": "/*\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\nbool FInReplicaReplay();\n\n/* ================================ MULTI/EXEC ============================== */\n\n/* Client state initialization for MULTI/EXEC */\nvoid initClientMultiState(client *c) {\n    c->mstate.commands = NULL;\n    c->mstate.count = 0;\n    c->mstate.cmd_flags = 0;\n    c->mstate.cmd_inv_flags = 0;\n}\n\n/* Release all the resources associated with MULTI/EXEC state */\nvoid freeClientMultiState(client *c) {\n    int j;\n\n    for (j = 0; j < c->mstate.count; j++) {\n        int i;\n        multiCmd *mc = c->mstate.commands+j;\n\n        for (i = 0; i < mc->argc; i++)\n            decrRefCount(mc->argv[i]);\n        zfree(mc->argv);\n    }\n    zfree(c->mstate.commands);\n}\n\n/* Add a new command into the MULTI commands queue */\nvoid queueMultiCommand(client *c) {\n    multiCmd *mc;\n    int j;\n\n    /* No sense to waste memory if the transaction is already aborted.\n     * this is useful in case client sends these in a pipeline, or doesn't\n     * bother to read previous responses and didn't notice the multi was already\n     * aborted. */\n    if (c->flags & CLIENT_DIRTY_EXEC)\n        return;\n\n    c->mstate.commands = (multiCmd*)zrealloc(c->mstate.commands,\n            sizeof(multiCmd)*(c->mstate.count+1), MALLOC_LOCAL);\n    mc = c->mstate.commands+c->mstate.count;\n    mc->cmd = c->cmd;\n    mc->argc = c->argc;\n    mc->argv = (robj**)zmalloc(sizeof(robj*)*c->argc, MALLOC_LOCAL);\n    memcpy(mc->argv,c->argv,sizeof(robj*)*c->argc);\n    for (j = 0; j < c->argc; j++)\n        incrRefCount(mc->argv[j]);\n    c->mstate.count++;\n    c->mstate.cmd_flags |= c->cmd->flags;\n    c->mstate.cmd_inv_flags |= ~c->cmd->flags;\n}\n\nvoid discardTransaction(client *c) {\n    serverAssert(GlobalLocksAcquired());\n    freeClientMultiState(c);\n    initClientMultiState(c);\n    c->flags &= ~(CLIENT_MULTI|CLIENT_DIRTY_CAS|CLIENT_DIRTY_EXEC);\n    unwatchAllKeys(c);\n}\n\n/* Flag the transaction as DIRTY_EXEC so that EXEC will fail.\n * Should be called every time there is an error while queueing a command. */\nvoid flagTransaction(client *c) {\n    if (c->flags & CLIENT_MULTI)\n        c->flags |= CLIENT_DIRTY_EXEC;\n}\n\nvoid multiCommand(client *c) {\n    serverAssert(GlobalLocksAcquired());\n    if (c->flags & CLIENT_MULTI) {\n        addReplyError(c,\"MULTI calls can not be nested\");\n        return;\n    }\n    c->flags |= CLIENT_MULTI;\n\n    addReply(c,shared.ok);\n}\n\nvoid discardCommand(client *c) {\n    if (!(c->flags & CLIENT_MULTI)) {\n        addReplyError(c,\"DISCARD without MULTI\");\n        return;\n    }\n    discardTransaction(c);\n    addReply(c,shared.ok);\n}\n\nvoid beforePropagateMulti() {\n    /* Propagating MULTI */\n    serverAssert(!serverTL->propagate_in_transaction);\n    serverTL->propagate_in_transaction = 1;\n}\n\nvoid afterPropagateExec() {\n    /* Propagating EXEC */\n    serverAssert(serverTL->propagate_in_transaction == 1);\n    serverTL->propagate_in_transaction = 0;\n}\n\n/* Send a MULTI command to all the slaves and AOF file. Check the execCommand\n * implementation for more information. */\nvoid execCommandPropagateMulti(int dbid) {\n    beforePropagateMulti();\n    propagate(cserver.multiCommand,dbid,&shared.multi,1,\n              PROPAGATE_AOF|PROPAGATE_REPL);\n}\n\nvoid execCommandPropagateExec(int dbid) {\n    propagate(cserver.execCommand,dbid,&shared.exec,1,\n              PROPAGATE_AOF|PROPAGATE_REPL);\n    afterPropagateExec();\n}\n\n/* Aborts a transaction, with a specific error message.\n * The transaction is always aborted with -EXECABORT so that the client knows\n * the server exited the multi state, but the actual reason for the abort is\n * included too.\n * Note: 'error' may or may not end with \\r\\n. see addReplyErrorFormat. */\nvoid execCommandAbort(client *c, sds error) {\n    discardTransaction(c);\n\n    if (error[0] == '-') error++;\n    addReplyErrorFormat(c, \"-EXECABORT Transaction discarded because of: %s\", error);\n\n    /* Send EXEC to clients waiting data from MONITOR. We did send a MULTI\n     * already, and didn't send any of the queued commands, now we'll just send\n     * EXEC so it is clear that the transaction is over. */\n    replicationFeedMonitors(c,g_pserver->monitors,c->db->id,c->argv,c->argc);\n}\n\nvoid execCommand(client *c) {\n    int j;\n    robj **orig_argv;\n    int orig_argc;\n    struct redisCommand *orig_cmd;\n    int was_master = listLength(g_pserver->masters) == 0;\n\n    if (!(c->flags & CLIENT_MULTI)) {\n        addReplyError(c,\"EXEC without MULTI\");\n        return;\n    }\n\n    /* EXEC with expired watched key is disallowed*/\n    if (isWatchedKeyExpired(c)) {\n        c->flags |= (CLIENT_DIRTY_CAS);\n    }\n\n    /* Check if we need to abort the EXEC because:\n     * 1) Some WATCHed key was touched.\n     * 2) There was a previous error while queueing commands.\n     * A failed EXEC in the first case returns a multi bulk nil object\n     * (technically it is not an error but a special behavior), while\n     * in the second an EXECABORT error is returned. */\n    if (c->flags & (CLIENT_DIRTY_CAS | CLIENT_DIRTY_EXEC)) {\n        if (c->flags & CLIENT_DIRTY_EXEC) {\n            addReplyErrorObject(c, shared.execaborterr);\n        } else {\n            addReply(c, shared.nullarray[c->resp]);\n        }\n\n        discardTransaction(c);\n        return;\n    }\n\n    uint64_t old_flags = c->flags;\n\n    /* we do not want to allow blocking commands inside multi */\n    c->flags |= CLIENT_DENY_BLOCKING;\n\n    /* Exec all the queued commands */\n    unwatchAllKeys(c); /* Unwatch ASAP otherwise we'll waste CPU cycles */\n\n    serverTL->in_exec = 1;\n\n    orig_argv = c->argv;\n    orig_argc = c->argc;\n    orig_cmd = c->cmd;\n    addReplyArrayLen(c,c->mstate.count);\n    for (j = 0; j < c->mstate.count; j++) {\n        c->argc = c->mstate.commands[j].argc;\n        c->argv = c->mstate.commands[j].argv;\n        c->cmd = c->mstate.commands[j].cmd;\n\n        /* ACL permissions are also checked at the time of execution in case\n         * they were changed after the commands were queued. */\n        int acl_errpos;\n        int acl_retval = ACLCheckAllPerm(c,&acl_errpos);\n        if (acl_retval != ACL_OK) {\n            const char *reason;\n            switch (acl_retval) {\n            case ACL_DENIED_CMD:\n                reason = \"no permission to execute the command or subcommand\";\n                break;\n            case ACL_DENIED_KEY:\n                reason = \"no permission to touch the specified keys\";\n                break;\n            case ACL_DENIED_CHANNEL:\n                reason = \"no permission to access one of the channels used \"\n                         \"as arguments\";\n                break;\n            default:\n                reason = \"no permission\";\n                break;\n            }\n            addACLLogEntry(c,acl_retval,acl_errpos,NULL);\n            addReplyErrorFormat(c,\n                \"-NOPERM ACLs rules changed between the moment the \"\n                \"transaction was accumulated and the EXEC call. \"\n                \"This command is no longer allowed for the \"\n                \"following reason: %s\", reason);\n        } else {\n            int flags = g_pserver->loading ? CMD_CALL_NONE : CMD_CALL_FULL;\n            if (FInReplicaReplay())\n                flags &= ~CMD_CALL_PROPAGATE;\n            call(c,flags);\n            serverAssert((c->flags & CLIENT_BLOCKED) == 0);\n        }\n\n        /* Commands may alter argc/argv, restore mstate. */\n        c->mstate.commands[j].argc = c->argc;\n        c->mstate.commands[j].argv = c->argv;\n        c->mstate.commands[j].cmd = c->cmd;\n    }\n\n    // restore old DENY_BLOCKING value\n    if (!(old_flags & CLIENT_DENY_BLOCKING))\n        c->flags &= ~CLIENT_DENY_BLOCKING;\n\n    c->argv = orig_argv;\n    c->argc = orig_argc;\n    c->cmd = orig_cmd;\n    discardTransaction(c);\n\n    /* Make sure the EXEC command will be propagated as well if MULTI\n     * was already propagated. */\n    if (serverTL->propagate_in_transaction) {\n        int is_master = listLength(g_pserver->masters) == 0;\n        g_pserver->dirty++;\n        /* If inside the MULTI/EXEC block this instance was suddenly\n         * switched from master to replica (using the SLAVEOF command), the\n         * initial MULTI was propagated into the replication backlog, but the\n         * rest was not. We need to make sure to at least terminate the\n         * backlog with the final EXEC. */\n        if (g_pserver->repl_backlog && was_master && !is_master) {\n            const char *execcmd = \"*1\\r\\n$4\\r\\nEXEC\\r\\n\";\n            feedReplicationBacklog(execcmd,strlen(execcmd));\n        }\n        afterPropagateExec();\n    }\n\n    serverTL->in_exec = 0;\n}\n\n/* ===================== WATCH (CAS alike for MULTI/EXEC) ===================\n *\n * The implementation uses a per-DB hash table mapping keys to list of clients\n * WATCHing those keys, so that given a key that is going to be modified\n * we can mark all the associated clients as dirty.\n *\n * Also every client contains a list of WATCHed keys so that's possible to\n * un-watch such keys when the client is freed or when UNWATCH is called. */\n\n/* In the client->watched_keys list we need to use watchedKey structures\n * as in order to identify a key in Redis we need both the key name and the\n * DB */\ntypedef struct watchedKey {\n    robj *key;\n    redisDb *db;\n} watchedKey;\n\n/* Watch for the specified key */\nvoid watchForKey(client *c, robj *key) {\n    list *clients = NULL;\n    listIter li;\n    listNode *ln;\n    watchedKey *wk;\n\n    /* Check if we are already watching for this key */\n    listRewind(c->watched_keys,&li);\n    while((ln = listNext(&li))) {\n        wk = (watchedKey*)listNodeValue(ln);\n        if (wk->db == c->db && equalStringObjects(key,wk->key))\n            return; /* Key already watched */\n    }\n    /* This key is not already watched in this DB. Let's add it */\n    clients = (decltype(clients)) dictFetchValue(c->db->watched_keys,key);\n    if (!clients) {\n        clients = listCreate();\n        dictAdd(c->db->watched_keys,key,clients);\n        incrRefCount(key);\n    }\n    listAddNodeTail(clients,c);\n    /* Add the new key to the list of keys watched by this client */\n    wk = (watchedKey*)zmalloc(sizeof(*wk), MALLOC_SHARED);\n    wk->key = key;\n    wk->db = c->db;\n    incrRefCount(key);\n    listAddNodeTail(c->watched_keys,wk);\n}\n\n/* Unwatch all the keys watched by this client. To clean the EXEC dirty\n * flag is up to the caller. */\nvoid unwatchAllKeys(client *c) {\n    listIter li;\n    listNode *ln;\n\n    if (listLength(c->watched_keys) == 0) return;\n    listRewind(c->watched_keys,&li);\n    while((ln = listNext(&li))) {\n        list *clients;\n        watchedKey *wk;\n\n        /* Lookup the watched key -> clients list and remove the client\n         * from the list */\n        wk = (watchedKey*)listNodeValue(ln);\n        clients = (decltype(clients))dictFetchValue(wk->db->watched_keys, wk->key);\n        serverAssertWithInfo(c,NULL,clients != NULL);\n        listDelNode(clients,listSearchKey(clients,c));\n        /* Kill the entry at all if this was the only client */\n        if (listLength(clients) == 0)\n            dictDelete(wk->db->watched_keys, wk->key);\n        /* Remove this watched key from the client->watched list */\n        listDelNode(c->watched_keys,ln);\n        decrRefCount(wk->key);\n        zfree(wk);\n    }\n}\n\n/* iterates over the watched_keys list and\n * look for an expired key . */\nint isWatchedKeyExpired(client *c) {\n    listIter li;\n    listNode *ln;\n    watchedKey *wk;\n    if (listLength(c->watched_keys) == 0) return 0;\n    listRewind(c->watched_keys,&li);\n    while ((ln = listNext(&li))) {\n        wk = (watchedKey*)listNodeValue(ln);\n        if (keyIsExpired(wk->db, wk->key)) return 1;\n    }\n\n    return 0;\n}\n\n/* \"Touch\" a key, so that if this key is being WATCHed by some client the\n * next EXEC will fail. */\nvoid touchWatchedKey(redisDb *db, robj *key) {\n    serverAssert(GlobalLocksAcquired());\n    list *clients;\n    listIter li;\n    listNode *ln;\n\n    if (dictSize(db->watched_keys) == 0) return;\n    clients = (list*)dictFetchValue(db->watched_keys, key);\n    if (!clients) return;\n\n    /* Mark all the clients watching this key as CLIENT_DIRTY_CAS */\n    /* Check if we are already watching for this key */\n    listRewind(clients,&li);\n    while((ln = listNext(&li))) {\n        client *c = (client*)listNodeValue(ln);\n\n        c->flags |= CLIENT_DIRTY_CAS;\n    }\n}\n\n/* Set CLIENT_DIRTY_CAS to all clients of DB when DB is dirty.\n * It may happen in the following situations:\n * FLUSHDB, FLUSHALL, SWAPDB\n *\n * replaced_with: for SWAPDB, the WATCH should be invalidated if\n * the key exists in either of them, and skipped only if it\n * doesn't exist in both. */\nvoid touchAllWatchedKeysInDb(redisDb *emptied, redisDb *replaced_with) {\n    listIter li;\n    listNode *ln;\n    dictEntry *de;\n\n    serverAssert(GlobalLocksAcquired());\n\n    if (dictSize(emptied->watched_keys) == 0) return;\n\n    dictIterator *di = dictGetSafeIterator(emptied->watched_keys);\n    while((de = dictNext(di)) != NULL) {\n        robj *key = (robj*)de->key;\n        list *clients = (list*)de->v.val;\n        if (!clients) continue;\n        listRewind(clients,&li);\n        while((ln = listNext(&li))) {\n            client *c = (client*)listNodeValue(ln);\n            if (emptied->find(key)) {\n                c->flags |= CLIENT_DIRTY_CAS;\n            } else if (replaced_with && replaced_with->find(key)) {\n                c->flags |= CLIENT_DIRTY_CAS;\n            }\n        }\n    }\n    dictReleaseIterator(di);\n}\n\n/* Update the DB of the watchedKey structure incase if the c->db (currently selected DB) is updated.\n * For example, it may happen during the SWAPDB.\n * watchForKey() sets the original DB of the watchedKey structure with the c->db\n * but the c->db can be updated incase of SWAPDB command. */\nvoid updateDBWatchedKey(int dbid, client *c) {\n    listIter li;\n    listNode *ln;\n    watchedKey *wk;\n\n    listRewind(c->watched_keys,&li);\n    while((ln = listNext(&li))) {\n        wk = (watchedKey*)listNodeValue(ln);\n        if (wk->db->id == dbid) {\n            wk->db = c->db;\n        }\n    }\n}\n\nvoid watchCommand(client *c) {\n    int j;\n\n    if (c->flags & CLIENT_MULTI) {\n        addReplyError(c,\"WATCH inside MULTI is not allowed\");\n        return;\n    }\n    for (j = 1; j < c->argc; j++)\n        watchForKey(c,c->argv[j]);\n    addReply(c,shared.ok);\n}\n\nvoid unwatchCommand(client *c) {\n    unwatchAllKeys(c);\n    serverAssert(GlobalLocksAcquired());\n    c->flags &= (~CLIENT_DIRTY_CAS);\n    addReply(c,shared.ok);\n}\n"
  },
  {
    "path": "src/networking.cpp",
    "content": "/*\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * Copyright (c) 2019 John Sully <john at eqalpha dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n#include \"atomicvar.h\"\n#include \"cluster.h\"\n#include <sys/socket.h>\n#include <sys/uio.h>\n#include <math.h>\n#include <ctype.h>\n#include <vector>\n#include <mutex>\n#include \"aelocker.h\"\n\nstatic void setProtocolError(const char *errstr, client *c);\n__thread int ProcessingEventsWhileBlocked = 0; /* See processEventsWhileBlocked(). */\n\n/* Return the size consumed from the allocator, for the specified SDS string,\n * including internal fragmentation. This function is used in order to compute\n * the client output buffer size. */\nsize_t sdsZmallocSize(sds s) {\n    void *sh = sdsAllocPtr(s);\n    return zmalloc_size(sh);\n}\n\n/* Return the amount of memory used by the sds string at object->ptr\n * for a string object. This includes internal fragmentation. */\nsize_t getStringObjectSdsUsedMemory(robj *o) {\n    serverAssertWithInfo(NULL,o,o->type == OBJ_STRING);\n    switch(o->encoding) {\n    case OBJ_ENCODING_RAW: return sdsZmallocSize((sds)ptrFromObj(o));\n    case OBJ_ENCODING_EMBSTR: return zmalloc_size(allocPtrFromObj(o))-sizeof(robj);\n    default: return 0; /* Just integer encoding for now. */\n    }\n}\n\n/* Return the length of a string object.\n * This does NOT includes internal fragmentation or sds unused space. */\nsize_t getStringObjectLen(robj *o) {\n    serverAssertWithInfo(NULL,o,o->type == OBJ_STRING);\n    switch(o->encoding) {\n    case OBJ_ENCODING_RAW: return sdslen(szFromObj(o));\n    case OBJ_ENCODING_EMBSTR: return sdslen(szFromObj(o));\n    default: return 0; /* Just integer encoding for now. */\n    }\n}\n\n/* Client.reply list dup and free methods. */\nvoid *dupClientReplyValue(void *o) {\n    clientReplyBlock *old = (clientReplyBlock*)o;\n    clientReplyBlock *buf = (clientReplyBlock*)zmalloc(sizeof(clientReplyBlock) + old->size, MALLOC_LOCAL);\n    memcpy(buf, o, sizeof(clientReplyBlock) + old->size);\n    return buf;\n}\n\nvoid freeClientReplyValue(const void *o) {\n    zfree(o);\n}\n\nint listMatchObjects(void *a, void *b) {\n    return equalStringObjects((robj*)a,(robj*)b);\n}\n\n/* This function links the client to the global linked list of clients.\n * unlinkClient() does the opposite, among other things. */\nvoid linkClient(client *c) {\n    serverAssert(GlobalLocksAcquired());\n    listAddNodeTail(g_pserver->clients,c);\n    /* Note that we remember the linked list node where the client is stored,\n     * this way removing the client in unlinkClient() will not require\n     * a linear scan, but just a constant time operation. */\n    c->client_list_node = listLast(g_pserver->clients);\n    if (c->conn != nullptr) atomicIncr(g_pserver->rgthreadvar[c->iel].cclients, 1);\n    uint64_t id = htonu64(c->id);\n    raxInsert(g_pserver->clients_index,(unsigned char*)&id,sizeof(id),c,NULL);\n}\n\n/* Initialize client authentication state.\n */\nstatic void clientSetDefaultAuth(client *c) {\n    /* If the default user does not require authentication, the user is\n     * directly authenticated. */\n    c->user = DefaultUser;\n    c->authenticated = (c->user->flags & USER_FLAG_NOPASS) &&\n                       !(c->user->flags & USER_FLAG_DISABLED);\n}\n\nint authRequired(client *c) {\n    /* Check if the user is authenticated. This check is skipped in case\n     * the default user is flagged as \"nopass\" and is active. */\n    int auth_required = (!(DefaultUser->flags & USER_FLAG_NOPASS) ||\n                          (DefaultUser->flags & USER_FLAG_DISABLED)) &&\n                        !c->authenticated;\n    return auth_required;\n}\n\nclient *createClient(connection *conn, int iel) {\n    client *c = new client;\n    serverAssert(conn == nullptr || (iel == (serverTL - g_pserver->rgthreadvar)));\n\n    c->iel = iel;\n    /* passing NULL as conn it is possible to create a non connected client.\n     * This is useful since all the commands needs to be executed\n     * in the context of a client. When commands are executed in other\n     * contexts (for instance a Lua script) we need a non connected client. */\n    if (conn) {\n        serverAssert(iel == (serverTL - g_pserver->rgthreadvar));\n        connNonBlock(conn);\n        connEnableTcpNoDelay(conn);\n        if (cserver.tcpkeepalive)\n            connKeepAlive(conn,cserver.tcpkeepalive);\n        connSetReadHandler(conn, readQueryFromClient, true);\n        connSetPrivateData(conn, c);\n    }\n\n    selectDb(c,0);\n    uint64_t client_id;\n    client_id = g_pserver->next_client_id.fetch_add(1);\n    c->iel = iel;\n    c->id = client_id;\n    snprintf(c->lock.szName, sizeof(c->lock.szName), \"client %\" PRIu64, client_id);\n    c->resp = 2;\n    c->conn = conn;\n    c->name = NULL;\n    c->bufpos = 0;\n    c->qb_pos = 0;\n    c->querybuf = sdsempty();\n    c->pending_querybuf = sdsempty();\n    c->querybuf_peak = 0;\n    c->reqtype = 0;\n    c->argc = 0;\n    c->argv = NULL;\n    c->original_argc = 0;\n    c->original_argv = NULL;\n    c->cmd = c->lastcmd = NULL;\n    c->multibulklen = 0;\n    c->bulklen = -1;\n    c->sentlen = 0;\n    c->flags = 0;\n    c->fPendingAsyncWrite = FALSE;\n    c->fPendingAsyncWriteHandler = FALSE;\n    c->ctime = c->lastinteraction = g_pserver->unixtime;\n    /* If the default user does not require authentication, the user is\n     * directly authenticated. */\n    clientSetDefaultAuth(c);\n    c->replstate = REPL_STATE_NONE;\n    c->repl_put_online_on_ack = 0;\n    c->reploff = 0;\n    c->read_reploff = 0;\n    c->reploff_cmd = 0;\n    c->repl_ack_off = 0;\n    c->repl_ack_time = 0;\n    c->repl_down_since = 0;\n    c->repl_last_partial_write = 0;\n    c->slave_listening_port = 0;\n    c->slave_addr = NULL;\n    c->slave_capa = SLAVE_CAPA_NONE;\n    c->reply = listCreate();\n    c->reply_bytes = 0;\n    c->obuf_soft_limit_reached_time = 0;\n    listSetFreeMethod(c->reply,freeClientReplyValue);\n    listSetDupMethod(c->reply,dupClientReplyValue);\n    c->btype = BLOCKED_NONE;\n    c->bpop.timeout = 0;\n    c->bpop.keys = dictCreate(&objectKeyHeapPointerValueDictType,NULL);\n    c->bpop.target = NULL;\n    c->bpop.xread_group = NULL;\n    c->bpop.xread_consumer = NULL;\n    c->bpop.xread_group_noack = 0;\n    c->bpop.numreplicas = 0;\n    c->bpop.reploffset = 0;\n    c->woff = 0;\n    c->watched_keys = listCreate();\n    c->pubsub_channels = dictCreate(&objectKeyPointerValueDictType,NULL);\n    c->pubsub_patterns = listCreate();\n    c->peerid = NULL;\n    c->sockname = NULL;\n    c->client_list_node = NULL;\n    c->replyAsync = NULL;\n    c->paused_list_node = NULL;\n    c->client_tracking_redirection = 0;\n    c->casyncOpsPending = 0;\n    c->mvccCheckpoint = 0;\n    c->master_error = 0;\n    memset(c->uuid, 0, UUID_BINARY_LEN);\n\n    c->client_tracking_prefixes = NULL;\n    c->client_cron_last_memory_usage = 0;\n    c->client_cron_last_memory_type = CLIENT_TYPE_NORMAL;\n    c->auth_callback = NULL;\n    c->auth_callback_privdata = NULL;\n    c->auth_module = NULL;\n    listSetFreeMethod(c->pubsub_patterns,decrRefCountVoid);\n    listSetMatchMethod(c->pubsub_patterns,listMatchObjects);\n    if (conn) linkClient(c);\n    initClientMultiState(c);\n    AssertCorrectThread(c);\n    return c;\n}\n\nsize_t client::argv_len_sum() const {\n    size_t sum = 0;\n    for (auto &cmd : vecqueuedcmd)\n        sum += cmd.argv_len_sum;\n    return sum + argv_len_sumActive;\n}\n\n/* This function puts the client in the queue of clients that should write\n * their output buffers to the socket. Note that it does not *yet* install\n * the write handler, to start clients are put in a queue of clients that need\n * to write, so we try to do that before returning in the event loop (see the\n * handleClientsWithPendingWrites() function).\n * If we fail and there is more data to write, compared to what the socket\n * buffers can hold, then we'll really install the handler. */\nvoid clientInstallWriteHandler(client *c) {\n    /* Schedule the client to write the output buffers to the socket only\n     * if not already done and, for slaves, if the replica can actually receive\n     * writes at this stage. */\n\n    if (!(c->flags & CLIENT_PENDING_WRITE) &&\n        (c->replstate == REPL_STATE_NONE || c->replstate == SLAVE_STATE_FASTSYNC_TX || c->replstate == SLAVE_STATE_FASTSYNC_DONE ||\n         (c->replstate == SLAVE_STATE_ONLINE && !c->repl_put_online_on_ack)))\n    {\n        AssertCorrectThread(c);\n        serverAssert(c->lock.fOwnLock());\n        /* Here instead of installing the write handler, we just flag the\n         * client and put it into a list of clients that have something\n         * to write to the socket. This way before re-entering the event\n         * loop, we can try to directly write to the client sockets avoiding\n         * a system call. We'll only really install the write handler if\n         * we'll not be able to write the whole reply at once. */\n        c->flags |= CLIENT_PENDING_WRITE;\n        std::unique_lock<fastlock> lockf(g_pserver->rgthreadvar[c->iel].lockPendingWrite);\n        g_pserver->rgthreadvar[c->iel].clients_pending_write.push_back(c);\n    }\n}\n\nvoid clientInstallAsyncWriteHandler(client *c) {\n    serverAssert(GlobalLocksAcquired());\n    if (!(c->fPendingAsyncWrite)) {\n        c->fPendingAsyncWrite = TRUE;\n        listAddNodeHead(serverTL->clients_pending_asyncwrite,c);\n    }\n}\n\n/* This function is called every time we are going to transmit new data\n * to the client. The behavior is the following:\n *\n * If the client should receive new data (normal clients will) the function\n * returns C_OK, and make sure to install the write handler in our event\n * loop so that when the socket is writable new data gets written.\n *\n * If the client should not receive new data, because it is a fake client\n * (used to load AOF in memory), a master or because the setup of the write\n * handler failed, the function returns C_ERR.\n *\n * The function may return C_OK without actually installing the write\n * event handler in the following cases:\n *\n * 1) The event handler should already be installed since the output buffer\n *    already contains something.\n * 2) The client is a replica but not yet online, so we want to just accumulate\n *    writes in the buffer but not actually sending them yet.\n *\n * Typically gets called every time a reply is built, before adding more\n * data to the clients output buffers. If the function returns C_ERR no\n * data should be appended to the output buffers. */\nint prepareClientToWrite(client *c) {\n    bool fAsync = !FCorrectThread(c);  // Not async if we're on the right thread\n\n\tif (!fAsync) {\n\t\tserverAssert(c->conn == nullptr || c->lock.fOwnLock());\n\t} else {\n\t\tserverAssert(GlobalLocksAcquired());\n\t}\n\n    auto flags = c->flags.load(std::memory_order_relaxed);\n\n    if (flags & CLIENT_FORCE_REPLY) return C_OK; // FORCE REPLY means we're doing something else with the buffer.\n                                                // do not install a write handler\n\n    /* If it's the Lua client we always return ok without installing any\n     * handler since there is no socket at all. */\n    if (flags & (CLIENT_LUA|CLIENT_MODULE)) return C_OK;\n\n    /* If CLIENT_CLOSE_ASAP flag is set, we need not write anything. */\n    if (c->flags & CLIENT_CLOSE_ASAP) return C_ERR;\n\n    /* CLIENT REPLY OFF / SKIP handling: don't send replies. */\n    if (flags & (CLIENT_REPLY_OFF|CLIENT_REPLY_SKIP)) return C_ERR;\n\n    /* Masters don't receive replies, unless CLIENT_MASTER_FORCE_REPLY flag\n     * is set. */\n    if ((flags & CLIENT_MASTER) &&\n        !(flags & CLIENT_MASTER_FORCE_REPLY)) return C_ERR;\n\n    if (!c->conn) return C_ERR; /* Fake client for AOF loading. */\n\n    /* Schedule the client to write the output buffers to the socket, unless\n     * it should already be setup to do so (it has already pending data). */\n    if (!fAsync && (c->flags & CLIENT_SLAVE || !clientHasPendingReplies(c))) clientInstallWriteHandler(c);\n    if (fAsync && !(c->fPendingAsyncWrite)) clientInstallAsyncWriteHandler(c);\n\n    /* Authorize the caller to queue in the output buffer of this client. */\n    return C_OK;\n}\n\n/* -----------------------------------------------------------------------------\n * Low level functions to add more data to output buffers.\n * -------------------------------------------------------------------------- */\n\nvoid _clientAsyncReplyBufferReserve(client *c, size_t len) {\n    if (c->replyAsync != nullptr)\n        return;\n    size_t newsize = std::max(len, (size_t)PROTO_ASYNC_REPLY_CHUNK_BYTES);\n    clientReplyBlock *replyNew = (clientReplyBlock*)zmalloc(sizeof(clientReplyBlock) + newsize);\n    replyNew->size = zmalloc_usable_size(replyNew) - sizeof(clientReplyBlock);\n    replyNew->used = 0;\n    c->replyAsync = replyNew;\n}\n\n/* Attempts to add the reply to the static buffer in the client struct.\n * Returns C_ERR if the buffer is full, or the reply list is not empty,\n * in which case the reply must be added to the reply list. */\nint _addReplyToBuffer(client *c, const char *s, size_t len) {\n    if (c->flags.load(std::memory_order_relaxed) & CLIENT_CLOSE_AFTER_REPLY) return C_OK;\n\n    bool fAsync = !FCorrectThread(c);\n    if (fAsync)\n    {\n        serverAssert(GlobalLocksAcquired());\n        if (c->replyAsync == nullptr || (c->replyAsync->size - c->replyAsync->used) < len)\n        {\n            if (c->replyAsync == nullptr) {\n                size_t newsize = std::max(len, (size_t)PROTO_ASYNC_REPLY_CHUNK_BYTES);\n                    \n                clientReplyBlock *replyNew = (clientReplyBlock*)zmalloc(sizeof(clientReplyBlock) + newsize);\n                replyNew->size = zmalloc_usable_size(replyNew) - sizeof(clientReplyBlock);\n                replyNew->used = 0;\n                c->replyAsync = replyNew;\n            } else {\n                size_t newsize = std::max(c->replyAsync->used + len, c->replyAsync->size*2);\n                clientReplyBlock *replyNew = (clientReplyBlock*)zmalloc(sizeof(clientReplyBlock) + newsize);\n                replyNew->size = zmalloc_usable_size(replyNew) - sizeof(clientReplyBlock);\n                replyNew->used = c->replyAsync->used;\n                memcpy(replyNew->buf(), c->replyAsync->buf(), c->replyAsync->used);\n                zfree(c->replyAsync);\n                c->replyAsync = replyNew;\n            }\n        }\n        memcpy(c->replyAsync->buf() + c->replyAsync->used,s,len);\n        c->replyAsync->used += len;\n    }\n    else\n    {\n        size_t available = sizeof(c->buf)-c->bufpos;\n\n        /* If there already are entries in the reply list, we cannot\n        * add anything more to the static buffer. */\n        if (listLength(c->reply) > 0) return C_ERR;\n\n        /* Check that the buffer has enough space available for this string. */\n        if (len > available) return C_ERR;\n\n        memcpy(c->buf+c->bufpos,s,len);\n        c->bufpos+=len;\n    }\n    return C_OK;\n}\n\n/* Adds the reply to the reply linked list.\n * Note: some edits to this function need to be relayed to AddReplyFromClient. */\nvoid _addReplyProtoToList(client *c, const char *s, size_t len) {\n    if (c->flags.load(std::memory_order_relaxed) & CLIENT_CLOSE_AFTER_REPLY) return;\n    AssertCorrectThread(c);\n\n    listNode *ln = listLast(c->reply);\n    clientReplyBlock *tail = (clientReplyBlock*) (ln? listNodeValue(ln): NULL);\n\n    /* Note that 'tail' may be NULL even if we have a tail node, because when\n     * addReplyDeferredLen() is used, it sets a dummy node to NULL just\n     * fo fill it later, when the size of the bulk length is set. */\n\n    /* Append to tail string when possible. */\n    if (tail) {\n        /* Copy the part we can fit into the tail, and leave the rest for a\n         * new node */\n        size_t avail = tail->size - tail->used;\n        size_t copy = avail >= len? len: avail;\n        memcpy(tail->buf() + tail->used, s, copy);\n        tail->used += copy;\n        s += copy;\n        len -= copy;\n    }\n    if (len) {\n        /* Create a new node, make sure it is allocated to at\n         * least PROTO_REPLY_CHUNK_BYTES */\n        size_t size = len < PROTO_REPLY_CHUNK_BYTES? PROTO_REPLY_CHUNK_BYTES: len;\n        tail = (clientReplyBlock*)zmalloc(size + sizeof(clientReplyBlock), MALLOC_LOCAL);\n        /* take over the allocation's internal fragmentation */\n        tail->size = zmalloc_usable_size(tail) - sizeof(clientReplyBlock);\n        tail->used = len;\n        memcpy(tail->buf(), s, len);\n        listAddNodeTail(c->reply, tail);\n        c->reply_bytes += tail->size;\n\n        closeClientOnOutputBufferLimitReached(c, 1);\n    }\n}\n\n/* -----------------------------------------------------------------------------\n * Higher level functions to queue data on the client output buffer.\n * The following functions are the ones that commands implementations will call.\n * -------------------------------------------------------------------------- */\n/* Add the object 'obj' string representation to the client output buffer. */\nvoid addReply(client *c, robj_roptr obj) {\n    if (prepareClientToWrite(c) != C_OK) return;\n\n    if (sdsEncodedObject(obj)) {\n        if (_addReplyToBuffer(c,(const char*)ptrFromObj(obj),sdslen((sds)ptrFromObj(obj))) != C_OK)\n            _addReplyProtoToList(c,(const char*)ptrFromObj(obj),sdslen((sds)ptrFromObj(obj)));\n    } else if (obj->encoding == OBJ_ENCODING_INT) {\n        /* For integer encoded strings we just convert it into a string\n         * using our optimized function, and attach the resulting string\n         * to the output buffer. */\n        char buf[32];\n        size_t len = ll2string(buf,sizeof(buf),(long)ptrFromObj(obj));\n        if (_addReplyToBuffer(c,buf,len) != C_OK)\n            _addReplyProtoToList(c,buf,len);\n    } else {\n        serverPanic(\"Wrong obj->encoding in addReply()\");\n    }\n}\n\n/* Add the SDS 's' string to the client output buffer, as a side effect\n * the SDS string is freed. */\nvoid addReplySds(client *c, sds s) {\n    if (prepareClientToWrite(c) != C_OK) {\n        /* The caller expects the sds to be free'd. */\n        sdsfree(s);\n        return;\n    }\n    if (_addReplyToBuffer(c,s,sdslen(s)) != C_OK)\n        _addReplyProtoToList(c,s,sdslen(s));\n    sdsfree(s);\n}\n\n/* This low level function just adds whatever protocol you send it to the\n * client buffer, trying the static buffer initially, and using the string\n * of objects if not possible.\n *\n * It is efficient because does not create an SDS object nor an Redis object\n * if not needed. The object will only be created by calling\n * _addReplyProtoToList() if we fail to extend the existing tail object\n * in the list of objects. */\nvoid addReplyProto(client *c, const char *s, size_t len) {\n    if (prepareClientToWrite(c) != C_OK) return;\n    if (_addReplyToBuffer(c,s,len) != C_OK)\n        _addReplyProtoToList(c,s,len);\n}\n\nvoid addReplyProtoCString(client *c, const char *s) {\n    addReplyProto(c, s, strlen(s));\n}\n\nstd::string escapeString(sds str)\n{\n    std::string newstr;\n    size_t len = sdslen(str);\n    for (size_t ich = 0; ich < len; ++ich)\n    {\n        char ch = str[ich];\n        switch (ch)\n        {\n        case '\\n':\n            newstr += \"\\\\n\";\n            break;\n\n        case '\\t':\n            newstr += \"\\\\t\";\n            break;\n\n        case '\\r':\n            newstr += \"\\\\r\";\n            break;\n        \n        default:\n            newstr += ch;\n        }\n    }\n    return newstr;\n}\n\n/* Low level function called by the addReplyError...() functions.\n * It emits the protocol for a Redis error, in the form:\n *\n * -ERRORCODE Error Message<CR><LF>\n *\n * If the error code is already passed in the string 's', the error\n * code provided is used, otherwise the string \"-ERR \" for the generic\n * error code is automatically added.\n * Note that 's' must NOT end with \\r\\n. */\nvoid addReplyErrorLength(client *c, const char *s, size_t len) {\n    /* If the string already starts with \"-...\" then the error code\n     * is provided by the caller. Otherwise we use \"-ERR\". */\n    if (!len || s[0] != '-') addReplyProto(c,\"-ERR \",5);\n    addReplyProto(c,s,len);\n    addReplyProto(c,\"\\r\\n\",2);\n}\n\n/* Do some actions after an error reply was sent (Log if needed, updates stats, etc.) */\nvoid afterErrorReply(client *c, const char *s, size_t len, int severity = ERR_CRITICAL) {\n    /* Increment the thread error counter */\n    serverTL->stat_total_error_replies++;\n    /* Increment the error stats\n     * If the string already starts with \"-...\" then the error prefix\n     * is provided by the caller ( we limit the search to 32 chars). Otherwise we use \"-ERR\". */\n    if (s[0] != '-') {\n        incrementErrorCount(\"ERR\", 3);\n    } else {\n        const char *spaceloc = (const char*)memchr(s, ' ', len < 32 ? len : 32);\n        if (spaceloc) {\n            const size_t errEndPos = (size_t)(spaceloc - s);\n            incrementErrorCount(s+1, errEndPos-1);\n        } else {\n            /* Fallback to ERR if we can't retrieve the error prefix */\n            incrementErrorCount(\"ERR\", 3);\n        }\n    }\n\n    int ctype = getClientType(c);\n    if (ctype == CLIENT_TYPE_MASTER || ctype == CLIENT_TYPE_SLAVE || c->id == CLIENT_ID_AOF) {\n        const char *to, *from;\n\n        if (c->id == CLIENT_ID_AOF) {\n            to = \"AOF-loading-client\";\n            from = \"server\";\n        } else if (ctype == CLIENT_TYPE_MASTER) {\n            to = \"master\";\n            from = \"replica\";\n        } else {\n            to = \"replica\";\n            from = \"master\";\n        }\n\n        if (len > 4096) len = 4096;\n        const char *cmdname = c->lastcmd ? c->lastcmd->name : \"<unknown>\";\n        switch (severity) {\n            case ERR_NOTICE:\n                serverLog(LL_NOTICE,\"== NOTICE == This %s is rejecting a command \"\n                    \"from its %s: '%.*s' after processing the command \"\n                    \"'%s'\", from, to, (int)len, s, cmdname);\n            break;\n            case ERR_WARNING:\n                serverLog(LL_WARNING,\"== WARNING == This %s is rejecting a command \"\n                    \"from its %s: '%.*s' after processing the command \"\n                    \"'%s'\", from, to, (int)len, s, cmdname);\n            break;\n            case ERR_ERROR:\n                serverLog(LL_WARNING,\"== ERROR == This %s is sending an error \"\n                    \"to its %s: '%.*s' after processing the command \"\n                    \"'%s'\", from, to, (int)len, s, cmdname);\n            break;\n            case ERR_CRITICAL:\n            default:\n                serverLog(LL_WARNING,\"== CRITICAL == This %s is sending an error \"\n                    \"to its %s: '%.*s' after processing the command \"\n                    \"'%s'\", from, to, (int)len, s, cmdname);\n            break;\n        }\n\n        if (ctype == CLIENT_TYPE_MASTER && g_pserver->repl_backlog &&\n            g_pserver->repl_backlog_histlen > 0)\n        {\n            showLatestBacklog();\n        }\n        g_pserver->stat_unexpected_error_replies++;\n    }\n}\n\n/* The 'err' object is expected to start with -ERRORCODE and end with \\r\\n.\n * Unlike addReplyErrorSds and others alike which rely on addReplyErrorLength. */\nvoid addReplyErrorObject(client *c, robj *err, int severity) {\n    addReply(c, err);\n    afterErrorReply(c, szFromObj(err), sdslen(szFromObj(err))-2, severity); /* Ignore trailing \\r\\n */\n}\n\n/* See addReplyErrorLength for expectations from the input string. */\nvoid addReplyError(client *c, const char *err) {\n    addReplyErrorLength(c,err,strlen(err));\n    afterErrorReply(c,err,strlen(err));\n}\n\n/* See addReplyErrorLength for expectations from the input string. */\n/* As a side effect the SDS string is freed. */\nvoid addReplyErrorSds(client *c, sds err) {\n    addReplyErrorLength(c,err,sdslen(err));\n    afterErrorReply(c,err,sdslen(err));\n    sdsfree(err);\n}\n\n/* See addReplyErrorLength for expectations from the formatted string.\n * The formatted string is safe to contain \\r and \\n anywhere. */\nvoid addReplyErrorFormat(client *c, const char *fmt, ...) {\n    va_list ap;\n    va_start(ap,fmt);\n    sds s = sdscatvprintf(sdsempty(),fmt,ap);\n    va_end(ap);\n    /* Trim any newlines at the end (ones will be added by addReplyErrorLength) */\n    s = sdstrim(s, \"\\r\\n\");\n    /* Make sure there are no newlines in the middle of the string, otherwise\n     * invalid protocol is emitted. */\n    s = sdsmapchars(s, \"\\r\\n\", \"  \",  2);\n    addReplyErrorLength(c,s,sdslen(s));\n    afterErrorReply(c,s,sdslen(s));\n    sdsfree(s);\n}\n\nvoid addReplyStatusLength(client *c, const char *s, size_t len) {\n    addReplyProto(c,\"+\",1);\n    addReplyProto(c,s,len);\n    addReplyProto(c,\"\\r\\n\",2);\n}\n\nvoid addReplyStatus(client *c, const char *status) {\n    addReplyStatusLength(c,status,strlen(status));\n}\n\nvoid addReplyStatusFormat(client *c, const char *fmt, ...) {\n    va_list ap;\n    va_start(ap,fmt);\n    sds s = sdscatvprintf(sdsempty(),fmt,ap);\n    va_end(ap);\n    addReplyStatusLength(c,s,sdslen(s));\n    sdsfree(s);\n}\n\n/* Sometimes we are forced to create a new reply node, and we can't append to\n * the previous one, when that happens, we wanna try to trim the unused space\n * at the end of the last reply node which we won't use anymore. */\nvoid trimReplyUnusedTailSpace(client *c) {\n    listNode *ln = listLast(c->reply);\n    clientReplyBlock *tail = ln? (clientReplyBlock*)listNodeValue(ln): NULL;\n\n    /* Note that 'tail' may be NULL even if we have a tail node, becuase when\n     * addReplyDeferredLen() is used */\n    if (!tail) return;\n\n    /* We only try to trim the space is relatively high (more than a 1/4 of the\n     * allocation), otherwise there's a high chance realloc will NOP.\n     * Also, to avoid large memmove which happens as part of realloc, we only do\n     * that if the used part is small.  */\n    if (tail->size - tail->used > tail->size / 4 &&\n        tail->used < PROTO_REPLY_CHUNK_BYTES)\n    {\n        size_t old_size = tail->size;\n        tail = (clientReplyBlock*)zrealloc(tail, tail->used + sizeof(clientReplyBlock));\n        /* take over the allocation's internal fragmentation (at least for\n         * memory usage tracking) */\n        tail->size = zmalloc_usable_size(tail) - sizeof(clientReplyBlock);\n        c->reply_bytes = c->reply_bytes + tail->size - old_size;\n        listNodeValue(ln) = tail;\n    }\n}\n\n/* Adds an empty object to the reply list that will contain the multi bulk\n * length, which is not known when this function is called. */\nvoid *addReplyDeferredLenCore(client *c) {\n    /* Note that we install the write event here even if the object is not\n     * ready to be sent, since we are sure that before returning to the\n     * event loop setDeferredAggregateLen() will be called. */\n    if (prepareClientToWrite(c) != C_OK) return NULL;\n    trimReplyUnusedTailSpace(c);\n    listAddNodeTail(c->reply,NULL); /* NULL is our placeholder. */\n    return listLast(c->reply);\n}\n\nvoid *addReplyDeferredLen(client *c) {\n    if (FCorrectThread(c))\n        return addReplyDeferredLenCore(c);\n        \n    return (void*)((ssize_t)(c->replyAsync ? c->replyAsync->used : 0));\n}\n\nvoid setDeferredReply(client *c, void *node, const char *s, size_t length) {\n    listNode *ln = (listNode*)node;\n    clientReplyBlock *next, *prev;\n\n    /* Abort when *node is NULL: when the client should not accept writes\n     * we return NULL in addReplyDeferredLen() */\n    if (node == NULL) return;\n    serverAssert(!listNodeValue(ln));\n\n    /* Normally we fill this dummy NULL node, added by addReplyDeferredLen(),\n     * with a new buffer structure containing the protocol needed to specify\n     * the length of the array following. However sometimes there might be room\n     * in the previous/next node so we can instead remove this NULL node, and\n     * suffix/prefix our data in the node immediately before/after it, in order\n     * to save a write(2) syscall later. Conditions needed to do it:\n     *\n     * - The prev node is non-NULL and has space in it or\n     * - The next node is non-NULL,\n     * - It has enough room already allocated\n     * - And not too large (avoid large memmove) */\n    if (ln->prev != NULL && (prev = (clientReplyBlock*)listNodeValue(ln->prev)) &&\n        prev->size - prev->used > 0)\n    {\n        size_t len_to_copy = prev->size - prev->used;\n        if (len_to_copy > length)\n            len_to_copy = length;\n        memcpy(prev->buf() + prev->used, s, len_to_copy);\n        prev->used += len_to_copy;\n        length -= len_to_copy;\n        if (length == 0) {\n            listDelNode(c->reply, ln);\n            return;\n        }\n        s += len_to_copy;\n    }\n\n    if (ln->next != NULL && (next = (clientReplyBlock*)listNodeValue(ln->next)) &&\n        next->size - next->used >= length &&\n        next->used < PROTO_REPLY_CHUNK_BYTES * 4)\n    {\n        memmove(next->buf() + length, next->buf(), next->used);\n        memcpy(next->buf(), s, length);\n        next->used += length;\n        listDelNode(c->reply,ln);\n    } else {\n        /* Create a new node */\n        clientReplyBlock *buf = (clientReplyBlock*)zmalloc(length + sizeof(clientReplyBlock));\n        /* Take over the allocation's internal fragmentation */\n        buf->size = zmalloc_usable_size(buf) - sizeof(clientReplyBlock);\n        buf->used = length;\n        memcpy(buf->buf(), s, length);\n        listNodeValue(ln) = buf;\n        c->reply_bytes += buf->size;\n\n        closeClientOnOutputBufferLimitReached(c, 1);\n    }\n}\n\n/* Populate the length object and try gluing it to the next chunk. */\nvoid setDeferredAggregateLen(client *c, void *node, long length, char prefix) {\n    serverAssert(length >= 0);\n\n    if (FCorrectThread(c)) {\n        /* Abort when *node is NULL: when the client should not accept writes\n         * we return NULL in addReplyDeferredLen() */\n        if (node == NULL) return;\n        char lenstr[128];\n        size_t lenstr_len = snprintf(lenstr, sizeof(lenstr), \"%c%ld\\r\\n\", prefix, length);\n        setDeferredReply(c, node, lenstr, lenstr_len);\n    } else {\n        char lenstr[128];\n        int lenstr_len = snprintf(lenstr, sizeof(lenstr), \"%c%ld\\r\\n\", prefix, length);\n\n        size_t idxSplice = (size_t)node;\n        serverAssert(idxSplice <= c->replyAsync->used);\n        if (c->replyAsync->size < (c->replyAsync->used + lenstr_len))\n        {\n            int newsize = std::max(c->replyAsync->used + lenstr_len, c->replyAsync->size*2);\n            clientReplyBlock *replyNew = (clientReplyBlock*)zmalloc(sizeof(clientReplyBlock) + newsize);\n            replyNew->size = zmalloc_usable_size(replyNew) - sizeof(clientReplyBlock);\n            replyNew->used = c->replyAsync->used;\n            memcpy(replyNew->buf(), c->replyAsync->buf(), c->replyAsync->used);\n            zfree(c->replyAsync);\n            c->replyAsync = replyNew;\n        }\n        \n        memmove(c->replyAsync->buf() + idxSplice + lenstr_len, c->replyAsync->buf() + idxSplice, c->replyAsync->used - idxSplice);\n        memcpy(c->replyAsync->buf() + idxSplice, lenstr, lenstr_len);\n        c->replyAsync->used += lenstr_len;\n    }\n}\n\nvoid setDeferredArrayLen(client *c, void *node, long length) {\n    setDeferredAggregateLen(c,node,length,'*');\n}\n\nvoid setDeferredMapLen(client *c, void *node, long length) {\n    int prefix = c->resp == 2 ? '*' : '%';\n    if (c->resp == 2) length *= 2;\n    setDeferredAggregateLen(c,node,length,prefix);\n}\n\nvoid setDeferredSetLen(client *c, void *node, long length) {\n    int prefix = c->resp == 2 ? '*' : '~';\n    setDeferredAggregateLen(c,node,length,prefix);\n}\n\nvoid setDeferredAttributeLen(client *c, void *node, long length) {\n    serverAssert(c->resp >= 3);\n    setDeferredAggregateLen(c,node,length,'|');\n}\n\nvoid setDeferredPushLen(client *c, void *node, long length) {\n    serverAssert(c->resp >= 3);\n    setDeferredAggregateLen(c,node,length,'>');\n}\n\n/* Add a double as a bulk reply */\nvoid addReplyDouble(client *c, double d) {\n    if (std::isinf(d)) {\n        /* Libc in odd systems (Hi Solaris!) will format infinite in a\n         * different way, so better to handle it in an explicit way. */\n        if (c->resp == 2) {\n            addReplyBulkCString(c, d > 0 ? \"inf\" : \"-inf\");\n        } else {\n            addReplyProto(c, d > 0 ? \",inf\\r\\n\" : \",-inf\\r\\n\",\n                              d > 0 ? 6 : 7);\n        }\n    } else {\n        char dbuf[MAX_LONG_DOUBLE_CHARS+3],\n             sbuf[MAX_LONG_DOUBLE_CHARS+32];\n        int dlen, slen;\n        if (c->resp == 2) {\n            dlen = snprintf(dbuf,sizeof(dbuf),\"%.17g\",d);\n            slen = snprintf(sbuf,sizeof(sbuf),\"$%d\\r\\n%s\\r\\n\",dlen,dbuf);\n            addReplyProto(c,sbuf,slen);\n        } else {\n            dlen = snprintf(dbuf,sizeof(dbuf),\",%.17g\\r\\n\",d);\n            addReplyProto(c,dbuf,dlen);\n        }\n    }\n}\n\nvoid addReplyBigNum(client *c, const char* num, size_t len) {\n    if (c->resp == 2) {\n        addReplyBulkCBuffer(c, num, len);\n    } else {\n        addReplyProto(c,\"(\",1);\n        addReplyProto(c,num,len);\n        addReply(c,shared.crlf);\n    }\n}\n\n/* Add a long double as a bulk reply, but uses a human readable formatting\n * of the double instead of exposing the crude behavior of doubles to the\n * dear user. */\nvoid addReplyHumanLongDouble(client *c, long double d) {\n    if (c->resp == 2) {\n        robj *o = createStringObjectFromLongDouble(d,1);\n        addReplyBulk(c,o);\n        decrRefCount(o);\n    } else {\n        char buf[MAX_LONG_DOUBLE_CHARS];\n        int len = ld2string(buf,sizeof(buf),d,LD_STR_HUMAN);\n        addReplyProto(c,\",\",1);\n        addReplyProto(c,buf,len);\n        addReplyProto(c,\"\\r\\n\",2);\n    }\n}\n\n/* Add a long long as integer reply or bulk len / multi bulk count.\n * Basically this is used to output <prefix><long long><crlf>. */\nvoid addReplyLongLongWithPrefix(client *c, long long ll, char prefix) {\n    char buf[128];\n    int len;\n\n    /* Things like $3\\r\\n or *2\\r\\n are emitted very often by the protocol\n     * so we have a few shared objects to use if the integer is small\n     * like it is most of the times. */\n    if (prefix == '*' && ll < OBJ_SHARED_BULKHDR_LEN && ll >= 0) {\n        addReply(c,shared.mbulkhdr[ll]);\n        return;\n    } else if (prefix == '$' && ll < OBJ_SHARED_BULKHDR_LEN && ll >= 0) {\n        addReply(c,shared.bulkhdr[ll]);\n        return;\n    }\n\n    buf[0] = prefix;\n    len = ll2string(buf+1,sizeof(buf)-1,ll);\n    buf[len+1] = '\\r';\n    buf[len+2] = '\\n';\n    addReplyProto(c,buf,len+3);\n}\n\nvoid addReplyLongLong(client *c, long long ll) {\n    if (ll == 0)\n        addReply(c,shared.czero);\n    else if (ll == 1)\n        addReply(c,shared.cone);\n    else\n        addReplyLongLongWithPrefix(c,ll,':');\n}\n\nvoid addReplyAggregateLen(client *c, long length, int prefix) {\n    serverAssert(length >= 0);\n    addReplyLongLongWithPrefix(c,length,prefix);\n}\n\nvoid addReplyArrayLen(client *c, long length) {\n    addReplyAggregateLen(c,length,'*');\n}\n\nvoid addReplyMapLen(client *c, long length) {\n    int prefix = c->resp == 2 ? '*' : '%';\n    if (c->resp == 2) length *= 2;\n    addReplyAggregateLen(c,length,prefix);\n}\n\nvoid addReplySetLen(client *c, long length) {\n    int prefix = c->resp == 2 ? '*' : '~';\n    addReplyAggregateLen(c,length,prefix);\n}\n\nvoid addReplyAttributeLen(client *c, long length) {\n    serverAssert(c->resp >= 3);\n    addReplyAggregateLen(c,length,'|');\n}\n\nvoid addReplyPushLen(client *c, long length) {\n    serverAssert(c->resp >= 3);\n    addReplyAggregateLen(c,length,'>');\n}\n\nvoid addReplyNull(client *c) {\n    if (c->resp == 2) {\n        addReplyProto(c,\"$-1\\r\\n\",5);\n    } else {\n        addReplyProto(c,\"_\\r\\n\",3);\n    }\n}\n\nvoid addReplyBool(client *c, int b) {\n    if (c->resp == 2) {\n        addReply(c, b ? shared.cone : shared.czero);\n    } else {\n        addReplyProto(c, b ? \"#t\\r\\n\" : \"#f\\r\\n\",4);\n    }\n}\n\n/* A null array is a concept that no longer exists in RESP3. However\n * RESP2 had it, so API-wise we have this call, that will emit the correct\n * RESP2 protocol, however for RESP3 the reply will always be just the\n * Null type \"_\\r\\n\". */\nvoid addReplyNullArray(client *c) \n{\n    if (c->resp == 2) {\n        addReplyProto(c,\"*-1\\r\\n\",5);\n    } else {\n        addReplyProto(c,\"_\\r\\n\",3);\n    }\n}\n\n/* Create the length prefix of a bulk reply, example: $2234 */\nvoid addReplyBulkLen(client *c, robj_roptr obj) {\n    size_t len = stringObjectLen(obj);\n\n    addReplyLongLongWithPrefix(c,len,'$');\n}\n\n/* Add a Redis Object as a bulk reply */\nvoid addReplyBulk(client *c, robj_roptr obj) {\n    addReplyBulkLen(c,obj);\n    addReply(c,obj);\n    addReply(c,shared.crlf);\n}\n\n/* Add a C buffer as bulk reply */\nvoid addReplyBulkCBuffer(client *c, const void *p, size_t len) {\n    addReplyLongLongWithPrefix(c,len,'$');\n    addReplyProto(c,(const char*)p,len);\n    addReply(c,shared.crlf);\n}\n\n/* Add sds to reply (takes ownership of sds and frees it) */\nvoid addReplyBulkSds(client *c, sds s)  {\n    addReplyLongLongWithPrefix(c,sdslen(s),'$');\n    addReplySds(c,s);\n    addReply(c,shared.crlf);\n}\n\n/* Set sds to a deferred reply (for symmetry with addReplyBulkSds it also frees the sds) */\nvoid setDeferredReplyBulkSds(client *c, void *node, sds s) {\n    sds reply = sdscatprintf(sdsempty(), \"$%d\\r\\n%s\\r\\n\", (unsigned)sdslen(s), s);\n    setDeferredReply(c, node, reply, sdslen(reply));\n    sdsfree(reply);\n    sdsfree(s);\n}\n\n/* Add a C null term string as bulk reply */\nvoid addReplyBulkCString(client *c, const char *s) {\n    if (s == NULL) {\n        if (c->resp < 3)\n            addReply(c,shared.nullbulk);\n        else\n            addReplyNull(c);\n    } else {\n        addReplyBulkCBuffer(c,s,strlen(s));\n    }\n}\n\n/* Add a long long as a bulk reply */\nvoid addReplyBulkLongLong(client *c, long long ll) {\n    char buf[64];\n    int len;\n\n    len = ll2string(buf,64,ll);\n    addReplyBulkCBuffer(c,buf,len);\n}\n\n/* Reply with a verbatim type having the specified extension.\n *\n * The 'ext' is the \"extension\" of the file, actually just a three\n * character type that describes the format of the verbatim string.\n * For instance \"txt\" means it should be interpreted as a text only\n * file by the receiver, \"md \" as markdown, and so forth. Only the\n * three first characters of the extension are used, and if the\n * provided one is shorter than that, the remaining is filled with\n * spaces. */\nvoid addReplyVerbatim(client *c, const char *s, size_t len, const char *ext) {\n    if (c->resp == 2) {\n        addReplyBulkCBuffer(c,s,len);\n    } else {\n        char buf[32];\n        size_t preflen = snprintf(buf,sizeof(buf),\"=%zu\\r\\nxxx:\",len+4);\n        char *p = buf+preflen-4;\n        for (int i = 0; i < 3; i++) {\n            if (*ext == '\\0') {\n                p[i] = ' ';\n            } else {\n                p[i] = *ext++;\n            }\n        }\n        addReplyProto(c,buf,preflen);\n        addReplyProto(c,s,len);\n        addReplyProto(c,\"\\r\\n\",2);\n    }\n}\n\n/* Add an array of C strings as status replies with a heading.\n * This function is typically invoked by from commands that support\n * subcommands in response to the 'help' subcommand. The help array\n * is terminated by NULL sentinel. */\nvoid addReplyHelp(client *c, const char **help) {\n    sds cmd = sdsnew((char*) ptrFromObj(c->argv[0]));\n    void *blenp = addReplyDeferredLen(c);\n    int blen = 0;\n\n    sdstoupper(cmd);\n    addReplyStatusFormat(c,\n        \"%s <subcommand> [<arg> [value] [opt] ...]. Subcommands are:\",cmd);\n    sdsfree(cmd);\n\n    while (help[blen]) addReplyStatus(c,help[blen++]);\n\n    addReplyStatus(c,\"HELP\");\n    addReplyStatus(c,\"    Prints this help.\");\n\n    blen += 1;  /* Account for the header. */\n    blen += 2;  /* Account for the footer. */\n    setDeferredArrayLen(c,blenp,blen);\n}\n\n/* Add a suggestive error reply.\n * This function is typically invoked by from commands that support\n * subcommands in response to an unknown subcommand or argument error. */\nvoid addReplySubcommandSyntaxError(client *c) {\n    sds cmd = sdsnew((char*) ptrFromObj(c->argv[0]));\n    sdstoupper(cmd);\n    addReplyErrorFormat(c,\n        \"Unknown subcommand or wrong number of arguments for '%s'. Try %s HELP.\",\n        (char*)ptrFromObj(c->argv[1]),cmd);\n    sdsfree(cmd);\n}\n\n/* Append 'src' client output buffers into 'dst' client output buffers.\n * This function clears the output buffers of 'src' */\nvoid AddReplyFromClient(client *dst, client *src) {\n    /* If the source client contains a partial response due to client output\n     * buffer limits, propagate that to the dest rather than copy a partial\n     * reply. We don't wanna run the risk of copying partial response in case\n     * for some reason the output limits don't reach the same decision (maybe\n     * they changed) */\n    if (src->flags & CLIENT_CLOSE_ASAP) {\n        sds client = catClientInfoString(sdsempty(),dst);\n        freeClientAsync(dst);\n        serverLog(LL_WARNING,\"Client %s scheduled to be closed ASAP for overcoming of output buffer limits.\", client);\n        sdsfree(client);\n        return;\n    }\n\n    /* First add the static buffer (either into the static buffer or reply list) */\n    addReplyProto(dst,src->buf, src->bufpos);\n\n    /* We need to check with prepareClientToWrite again (after addReplyProto)\n     * since addReplyProto may have changed something (like CLIENT_CLOSE_ASAP) */\n    if (prepareClientToWrite(dst) != C_OK)\n        return;\n\n    /* We're bypassing _addReplyProtoToList, so we need to add the pre/post\n     * checks in it. */\n    if (dst->flags & CLIENT_CLOSE_AFTER_REPLY) return;\n\n    /* Concatenate the reply list into the dest */\n    if (listLength(src->reply))\n        listJoin(dst->reply,src->reply);\n    dst->reply_bytes += src->reply_bytes;\n    src->reply_bytes = 0;\n    src->bufpos = 0;\n\n    /* Check output buffer limits */\n    closeClientOnOutputBufferLimitReached(dst, 1);\n}\n\n/* Copy 'src' client output buffers into 'dst' client output buffers.\n * The function takes care of freeing the old output buffers of the\n * destination client. */\nvoid copyClientOutputBuffer(client *dst, client *src) {\n    listRelease(dst->reply);\n    dst->sentlen = 0;\n    dst->reply = listDup(src->reply);\n    memcpy(dst->buf,src->buf,src->bufpos);\n    dst->bufpos = src->bufpos;\n    dst->reply_bytes = src->reply_bytes;\n}\n\n/* Return true if the specified client has pending reply buffers to write to\n * the socket. */\nint clientHasPendingReplies(client *c) {\n    return (c->bufpos || listLength(c->reply) || c->FPendingReplicaWrite());\n}\n\nstatic std::atomic<int> rgacceptsInFlight[MAX_EVENT_LOOPS];\nint chooseBestThreadForAccept()\n{\n    int ielMinLoad = 0;\n    int cclientsMin = INT_MAX;\n    for (int iel = 0; iel < cserver.cthreads; ++iel)\n    {\n        int cclientsThread;\n        atomicGet(g_pserver->rgthreadvar[iel].cclients, cclientsThread);\n        cclientsThread += rgacceptsInFlight[iel].load(std::memory_order_relaxed);\n        // Note: Its repl factor less one because cclients also includes replicas, so we don't want to double count\n        cclientsThread += (g_pserver->rgthreadvar[iel].cclientsReplica) * (g_pserver->replicaIsolationFactor-1);\n        if (cclientsThread < cserver.thread_min_client_threshold)\n            return iel;\n        if (cclientsThread < cclientsMin)\n        {\n            cclientsMin = cclientsThread;\n            ielMinLoad = iel;\n        }\n    }\n    return ielMinLoad;\n}\n\nvoid clientAcceptHandler(connection *conn) {\n    client *c = (client*)connGetPrivateData(conn);\n\n    if (conn->flags & CONN_FLAG_AUDIT_LOGGING_REQUIRED) {\n        c->flags |= CLIENT_AUDIT_LOGGING;\n        c->fprint = conn->fprint;\n    }\n\n    if (connGetState(conn) != CONN_STATE_CONNECTED) {\n        serverLog(LL_WARNING,\n                \"Error accepting a client connection: %s\",\n                connGetLastError(conn));\n        freeClientAsync(c);\n        return;\n    }\n\n    // Set thread affinity\n    if (cserver.fThreadAffinity)\n        connSetThreadAffinity(conn, c->iel);\n\n    /* If the server is running in protected mode (the default) and there\n     * is no password set, nor a specific interface is bound, we don't accept\n     * requests from non loopback interfaces. Instead we try to explain the\n     * user what to do to fix it if needed. */\n    if (g_pserver->protected_mode &&\n        g_pserver->bindaddr_count == 0 &&\n        DefaultUser->flags & USER_FLAG_NOPASS &&\n        !(c->flags & CLIENT_UNIX_SOCKET))\n    {\n        char cip[NET_IP_STR_LEN+1] = { 0 };\n        connPeerToString(conn, cip, sizeof(cip)-1, NULL);\n\n        if (strcmp(cip,\"127.0.0.1\") && strcmp(cip,\"::1\")) {\n            const char *err =\n                \"-DENIED KeyDB is running in protected mode because protected \"\n                \"mode is enabled, no bind address was specified, no \"\n                \"authentication password is requested to clients. In this mode \"\n                \"connections are only accepted from the loopback interface. \"\n                \"If you want to connect from external computers to KeyDB you \"\n                \"may adopt one of the following solutions: \"\n                \"1) Just disable protected mode sending the command \"\n                \"'CONFIG SET protected-mode no' from the loopback interface \"\n                \"by connecting to KeyDB from the same host the server is \"\n                \"running, however MAKE SURE KeyDB is not publicly accessible \"\n                \"from internet if you do so. Use CONFIG REWRITE to make this \"\n                \"change permanent. \"\n                \"2) Alternatively you can just disable the protected mode by \"\n                \"editing the KeyDB configuration file, and setting the protected \"\n                \"mode option to 'no', and then restarting the server \"\n                \"3) If you started the server manually just for testing, restart \"\n                \"it with the '--protected-mode no' option. \"\n                \"4) Setup a bind address or an authentication password. \"\n                \"NOTE: You only need to do one of the above things in order for \"\n                \"the server to start accepting connections from the outside.\\r\\n\";\n            if (connWrite(c->conn,err,strlen(err)) == -1) {\n                /* Nothing to do, Just to avoid the warning... */\n            }\n            g_pserver->stat_rejected_conn++;\n            freeClientAsync(c);\n            return;\n        }\n    }\n\n    g_pserver->stat_numconnections++;\n    moduleFireServerEvent(REDISMODULE_EVENT_CLIENT_CHANGE,\n                          REDISMODULE_SUBEVENT_CLIENT_CHANGE_CONNECTED,\n                          c);\n}\n\n#define MAX_ACCEPTS_PER_CALL 1000\n#define MAX_ACCEPTS_PER_CALL_TLS 100\n\nstatic void acceptCommonHandler(connection *conn, int flags, char *ip, int iel) {\n    client *c;\n    char conninfo[100];\n    UNUSED(ip);\n    AeLocker locker;\n    locker.arm(nullptr);\n\n    if (connGetState(conn) != CONN_STATE_ACCEPTING) {\n        serverLog(LL_VERBOSE,\n            \"Accepted client connection in error state: %s (conn: %s)\",\n            connGetLastError(conn),\n            connGetInfo(conn, conninfo, sizeof(conninfo)));\n        connClose(conn);\n        return;\n    }\n\n    /* Prevent new connections if we're in a soft shutdown situation */\n    if (g_pserver->soft_shutdown) {\n        const char *err = \"-SHUTDOWN\";\n        /* That's a best effort error message, don't check write errors.\n         * Note that for TLS connections, no handshake was done yet so nothing\n         * is written and the connection will just drop. */\n        if (connWrite(conn,err,strlen(err)) == -1) {\n            /* Nothing to do, Just to avoid the warning... */\n        }\n        g_pserver->stat_rejected_conn++;\n        connClose(conn);\n        return;\n    }\n\n    /* Limit the number of connections we take at the same time.\n     *\n     * Admission control will happen before a client is created and connAccept()\n     * called, because we don't want to even start transport-level negotiation\n     * if rejected. */\n    if (listLength(g_pserver->clients) + getClusterConnectionsCount()\n        >= (g_pserver->maxclients - g_pserver->maxclientsReserved))\n    {\n        // Allow the connection if it comes from localhost and we're within the maxclientReserved buffer range\n        if ((listLength(g_pserver->clients) + getClusterConnectionsCount()) >= g_pserver->maxclients || strcmp(\"127.0.0.1\", ip)) {\n            const char *err;\n            if (g_pserver->cluster_enabled)\n                err = \"-ERR max number of clients + cluster \"\n                    \"connections reached\\r\\n\";\n            else\n                err = \"-ERR max number of clients reached\\r\\n\";\n\n            /* That's a best effort error message, don't check write errors.\n            * Note that for TLS connections, no handshake was done yet so nothing\n            * is written and the connection will just drop. */\n            if (connWrite(conn,err,strlen(err)) == -1) {\n                /* Nothing to do, Just to avoid the warning... */\n            }\n            g_pserver->stat_rejected_conn++;\n            connClose(conn);\n            return;\n        }\n    }\n\n    /* Create connection and client */\n    if ((c = createClient(conn, iel)) == NULL) {\n        serverLog(LL_WARNING,\n            \"Error registering fd event for the new client: %s (conn: %s)\",\n            connGetLastError(conn),\n            connGetInfo(conn, conninfo, sizeof(conninfo)));\n        connClose(conn); /* May be already closed, just ignore errors */\n        return;\n    }\n\n    /* Last chance to keep flags */\n    c->flags |= flags;\n\n    /* Initiate accept.\n     *\n     * Note that connAccept() is free to do two things here:\n     * 1. Call clientAcceptHandler() immediately;\n     * 2. Schedule a future call to clientAcceptHandler().\n     *\n     * Because of that, we must do nothing else afterwards.\n     */\n    if (connAccept(conn, clientAcceptHandler) == C_ERR) {\n        char conninfo[100];\n        if (connGetState(conn) == CONN_STATE_ERROR)\n            serverLog(LL_WARNING,\n                    \"Error accepting a client connection: %s (conn: %s)\",\n                    connGetLastError(conn), connGetInfo(conn, conninfo, sizeof(conninfo)));\n        freeClient((client*)connGetPrivateData(conn));\n        return;\n    }\n}\n\nvoid acceptOnThread(connection *conn, int flags, char *cip)\n{\n    int ielCur = ielFromEventLoop(serverTL->el);\n    bool fBootLoad = (g_pserver->loading == LOADING_BOOT);\n\n    int ielTarget = ielCur;\n    if (fBootLoad)\n    {\n        ielTarget = IDX_EVENT_LOOP_MAIN;    // During load only the main thread is active\n    }\n    else if (g_fTestMode)\n    {\n        // On test mode we don't want any bunching of clients\n        while (cserver.cthreads > 1 && ielTarget == IDX_EVENT_LOOP_MAIN)\n            ielTarget = rand() % cserver.cthreads;\n    }\n    else if (g_pserver->active_client_balancing)\n    {\n        // Cluster connections are more transient, so its not worth the cost to balance\n        //  we can trust that SO_REUSEPORT is doing its job of distributing connections\n        ielTarget = g_pserver->cluster_enabled ? ielCur : chooseBestThreadForAccept();\n    }\n\n    rgacceptsInFlight[ielTarget].fetch_add(1, std::memory_order_relaxed);\n    if (ielTarget != ielCur)\n    {\n        char *szT = nullptr;\n        if (cip != nullptr)\n        {\n            szT = (char*)zmalloc(NET_IP_STR_LEN, MALLOC_LOCAL);\n            memcpy(szT, cip, NET_IP_STR_LEN);\n        }\n        int res = aePostFunction(g_pserver->rgthreadvar[ielTarget].el, [conn, flags, ielTarget, szT] {\n            connMarshalThread(conn);\n            acceptCommonHandler(conn,flags,szT,ielTarget);\n            rgacceptsInFlight[ielTarget].fetch_sub(1, std::memory_order_relaxed);\n            zfree(szT);\n        });\n\n        if (res == AE_OK)\n            return;\n        // If res != AE_OK we can still try to accept on the local thread\n    }\n    rgacceptsInFlight[ielTarget].fetch_sub(1, std::memory_order_relaxed);\n\n    aeAcquireLock();\n    acceptCommonHandler(conn,flags,cip,ielCur);\n    aeReleaseLock();\n}\n\nvoid acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask) {\n    int cport, cfd, max = MAX_ACCEPTS_PER_CALL;\n    char cip[NET_IP_STR_LEN];\n    UNUSED(mask);\n    UNUSED(privdata);\n    UNUSED(el);\n\n    while(max--) {\n        cfd = anetTcpAccept(serverTL->neterr, fd, cip, sizeof(cip), &cport);\n        if (cfd == ANET_ERR) {\n            if (errno != EWOULDBLOCK)\n                serverLog(LL_WARNING,\n                    \"Accepting client connection: %s\", serverTL->neterr);\n            return;\n        }\n        anetCloexec(cfd);\n        serverLog(LL_VERBOSE,\"Accepted %s:%d\", cip, cport);\n\n        acceptOnThread(connCreateAcceptedSocket(cfd), 0, cip);\n    }\n}\n\nvoid acceptTLSHandler(aeEventLoop *el, int fd, void *privdata, int mask) {\n    int cport, cfd, max = MAX_ACCEPTS_PER_CALL_TLS;\n    char cip[NET_IP_STR_LEN];\n    UNUSED(el);\n    UNUSED(mask);\n    UNUSED(privdata);\n\n    while(max--) {\n        cfd = anetTcpAccept(serverTL->neterr, fd, cip, sizeof(cip), &cport);\n        if (cfd == ANET_ERR) {\n            if (errno != EWOULDBLOCK)\n                serverLog(LL_WARNING,\n                    \"Accepting client connection: %s\", serverTL->neterr);\n            return;\n        }\n        anetCloexec(cfd);\n        serverLog(LL_VERBOSE,\"Accepted %s:%d\", cip, cport);\n\n        acceptOnThread(connCreateAcceptedTLS(cfd, g_pserver->tls_auth_clients), 0, cip);\n        if (aeLockContention() >= 2)\n            break;\n    }\n}\n\nvoid acceptUnixHandler(aeEventLoop *el, int fd, void *privdata, int mask) {\n    int cfd, max = MAX_ACCEPTS_PER_CALL;\n    UNUSED(el);\n    UNUSED(mask);\n    UNUSED(privdata);\n\n    while(max--) {\n        cfd = anetUnixAccept(serverTL->neterr, fd);\n        if (cfd == ANET_ERR) {\n            if (errno != EWOULDBLOCK)\n                serverLog(LL_WARNING,\n                    \"Accepting client connection: %s\", serverTL->neterr);\n            return;\n        }\n        anetCloexec(cfd);\n        serverLog(LL_VERBOSE,\"Accepted connection to %s\", g_pserver->unixsocket);\n        acceptOnThread(connCreateAcceptedSocket(cfd),CLIENT_UNIX_SOCKET,NULL);\n    }\n}\n\nvoid freeClientOriginalArgv(client *c) {\n    /* We didn't rewrite this client */\n    if (!c->original_argv) return;\n\n    for (int j = 0; j < c->original_argc; j++)\n        decrRefCount(c->original_argv[j]);\n    zfree(c->original_argv);\n    c->original_argv = NULL;\n    c->original_argc = 0;\n}\n\nstatic void freeClientArgv(client *c) {\n    int j;\n    for (j = 0; j < c->argc; j++)\n        decrRefCount(c->argv[j]);\n    c->argc = 0;\n    c->cmd = NULL;\n    c->argv_len_sumActive = 0;\n}\n\nvoid disconnectSlavesExcept(unsigned char *uuid)\n{\n    serverAssert(GlobalLocksAcquired());\n    listIter li;\n    listNode *ln;\n\n    listRewind(g_pserver->slaves, &li);\n    while ((ln = listNext(&li))) {\n        client *c = (client*)listNodeValue(ln);\n        if (uuid == nullptr || !FUuidEqual(c->uuid, uuid))\n            freeClientAsync(c);\n    }   \n}\n\n/* Close all the slaves connections. This is useful in chained replication\n * when we resync with our own master and want to force all our slaves to\n * resync with us as well. */\nvoid disconnectSlaves(void) {\n    disconnectSlavesExcept(nullptr);\n}\n\n/* Check if there is any other slave waiting dumping RDB finished expect me.\n * This function is useful to judge current dumping RDB can be used for full\n * synchronization or not. */\nint anyOtherSlaveWaitRdb(client *except_me) {\n    listIter li;\n    listNode *ln;\n\n    listRewind(g_pserver->slaves, &li);\n    while((ln = listNext(&li))) {\n        client *slave = (client*)listNodeValue(ln);\n        if (slave != except_me &&\n            slave->replstate == SLAVE_STATE_WAIT_BGSAVE_END)\n        {\n            return 1;\n        }\n    }\n    return 0;\n}\n\n/* Remove the specified client from global lists where the client could\n * be referenced, not including the Pub/Sub channels.\n * This is used by freeClient() and replicationCacheMaster(). */\nvoid unlinkClient(client *c) {\n    listNode *ln;\n    AssertCorrectThread(c);\n    serverAssert(c->conn == nullptr || GlobalLocksAcquired());\n    serverAssert(c->conn == nullptr || c->lock.fOwnLock());\n\n    /* If this is marked as current client unset it. */\n    if (serverTL && serverTL->current_client == c) serverTL->current_client = NULL;\n\n    /* Certain operations must be done only if the client has an active connection.\n     * If the client was already unlinked or if it's a \"fake client\" the\n     * conn is already set to NULL. */\n    if (c->conn) {\n        /* Remove from the list of active clients. */\n        if (c->client_list_node) {\n            uint64_t id = htonu64(c->id);\n            raxRemove(g_pserver->clients_index,(unsigned char*)&id,sizeof(id),NULL);\n            listDelNode(g_pserver->clients,c->client_list_node);\n            c->client_list_node = NULL;\n        }\n\n        /* Check if this is a replica waiting for diskless replication (rdb pipe),\n         * in which case it needs to be cleaned from that list */\n        if (c->flags & CLIENT_SLAVE &&\n            c->replstate == SLAVE_STATE_WAIT_BGSAVE_END &&\n            g_pserver->rdb_pipe_conns)\n        {\n            int i;\n            for (i=0; i < g_pserver->rdb_pipe_numconns; i++) {\n                if (g_pserver->rdb_pipe_conns[i] == c->conn) {\n                    rdbPipeWriteHandlerConnRemoved(c->conn);\n                    g_pserver->rdb_pipe_conns[i] = NULL;\n                    break;\n                }\n            }\n        }\n        connClose(c->conn);\n        c->conn = NULL;\n        atomicDecr(g_pserver->rgthreadvar[c->iel].cclients, 1);\n    }\n\n    /* Remove from the list of pending writes if needed. */\n    if (c->flags & CLIENT_PENDING_WRITE) {\n        std::unique_lock<fastlock> lockf(g_pserver->rgthreadvar[c->iel].lockPendingWrite);\n        auto itr = std::find(g_pserver->rgthreadvar[c->iel].clients_pending_write.begin(),\n            g_pserver->rgthreadvar[c->iel].clients_pending_write.end(), c);\n        serverAssert(itr != g_pserver->rgthreadvar[c->iel].clients_pending_write.end());\n        g_pserver->rgthreadvar[c->iel].clients_pending_write.erase(itr);\n        c->flags &= ~CLIENT_PENDING_WRITE;\n    }\n\n    /* When client was just unblocked because of a blocking operation,\n     * remove it from the list of unblocked clients. */\n    if (c->flags & CLIENT_UNBLOCKED) {\n        ln = listSearchKey(g_pserver->rgthreadvar[c->iel].unblocked_clients,c);\n        serverAssert(ln != NULL);\n        listDelNode(g_pserver->rgthreadvar[c->iel].unblocked_clients,ln);\n        c->flags &= ~CLIENT_UNBLOCKED;\n    }\n\n    if (c->fPendingAsyncWrite) {\n        ln = NULL;\n        bool fFound = false;\n        for (int iel = 0; iel < cserver.cthreads; ++iel)\n        {\n            ln = listSearchKey(g_pserver->rgthreadvar[iel].clients_pending_asyncwrite,c);\n            if (ln)\n            {\n                fFound = true;\n                listDelNode(g_pserver->rgthreadvar[iel].clients_pending_asyncwrite,ln);\n            }\n        }\n        fFound = g_pserver->asyncworkqueue->removeClientAsyncWrites(c) || fFound;\n        serverAssert(fFound);\n        c->fPendingAsyncWrite = FALSE;\n    }\n\n    serverTL->vecclientsProcess.erase(std::remove(serverTL->vecclientsProcess.begin(), serverTL->vecclientsProcess.end(), c), serverTL->vecclientsProcess.end());\n\n    /* Clear the tracking status. */\n    if (c->flags & CLIENT_TRACKING) disableTracking(c);\n}\n\nbool freeClient(client *c) {\n    listNode *ln;\n    serverAssert(c->conn == nullptr || GlobalLocksAcquired());\n    AssertCorrectThread(c);\n    std::unique_lock<decltype(c->lock)> ulock(c->lock);\n\n    /* If a client is protected, yet we need to free it right now, make sure\n     * to at least use asynchronous freeing. */\n    if (c->flags & CLIENT_PROTECTED || c->casyncOpsPending || c->replstate == SLAVE_STATE_FASTSYNC_TX) {\n        freeClientAsync(c);\n        return false;\n    }\n\n    /* For connected clients, call the disconnection event of modules hooks. */\n    if (c->conn) {\n        moduleFireServerEvent(REDISMODULE_EVENT_CLIENT_CHANGE,\n                              REDISMODULE_SUBEVENT_CLIENT_CHANGE_DISCONNECTED,\n                              c);\n    }\n\n    /* Notify module system that this client auth status changed. */\n    moduleNotifyUserChanged(c);\n\n    /* If this client was scheduled for async freeing we need to remove it\n     * from the queue. Note that we need to do this here, because later\n     * we may call replicationCacheMaster() and the client should already\n     * be removed from the list of clients to free. */\n    if (c->flags & CLIENT_CLOSE_ASAP) {\n        std::unique_lock<fastlock> ul(g_lockasyncfree);\n        ln = listSearchKey(g_pserver->clients_to_close,c);\n        serverAssert(ln != NULL);\n        listDelNode(g_pserver->clients_to_close,ln);\n    }\n\n    /* If it is our master that's being disconnected we should make sure\n     * to cache the state to try a partial resynchronization later.\n     *\n     * Note that before doing this we make sure that the client is not in\n     * some unexpected state, by checking its flags. */\n    if (FActiveMaster(c)) {\n        serverLog(LL_WARNING,\"Connection with master lost.\");\n        if (!(c->flags & (CLIENT_PROTOCOL_ERROR|CLIENT_BLOCKED))) {\n            c->flags &= ~(CLIENT_CLOSE_ASAP|CLIENT_CLOSE_AFTER_REPLY);\n            replicationCacheMaster(MasterInfoFromClient(c), c);\n            return false;\n        }\n    }\n\n    /* Log link disconnection with replica */\n    if (getClientType(c) == CLIENT_TYPE_SLAVE) {\n        serverLog(LL_WARNING,\"Connection with replica %s lost.\",\n            replicationGetSlaveName(c));\n    }\n\n    /* Free the query buffer */\n    sdsfree(c->querybuf);\n    sdsfree(c->pending_querybuf);\n    c->querybuf = NULL;\n\n    /* Deallocate structures used to block on blocking ops. */\n    if (c->flags & CLIENT_BLOCKED)\n    {\n        serverAssert(c->btype != BLOCKED_ASYNC);\n        unblockClient(c);\n    }\n    dictRelease(c->bpop.keys);\n\n    /* UNWATCH all the keys */\n    unwatchAllKeys(c);\n    listRelease(c->watched_keys);\n\n    /* Unsubscribe from all the pubsub channels */\n    pubsubUnsubscribeAllChannels(c,0);\n    pubsubUnsubscribeAllPatterns(c,0);\n    dictRelease(c->pubsub_channels);\n    listRelease(c->pubsub_patterns);\n\n    /* Free data structures. */\n    listRelease(c->reply);\n    freeClientArgv(c);\n    freeClientOriginalArgv(c);\n\n    /* Unlink the client: this will close the socket, remove the I/O\n     * handlers, and remove references of the client from different\n     * places where active clients may be referenced. */\n    unlinkClient(c);\n\n    /* Master/replica cleanup Case 1:\n     * we lost the connection with a replica. */\n    if (c->flags & CLIENT_SLAVE) {\n        /* If there is no any other slave waiting dumping RDB finished, the\n         * current child process need not continue to dump RDB, then we kill it.\n         * So child process won't use more memory, and we also can fork a new\n         * child process asap to dump rdb for next full synchronization or bgsave.\n         * But we also need to check if users enable 'save' RDB, if enable, we\n         * should not remove directly since that means RDB is important for users\n         * to keep data safe and we may delay configured 'save' for full sync. */\n        if (g_pserver->saveparamslen == 0 &&\n            c->replstate == SLAVE_STATE_WAIT_BGSAVE_END &&\n            g_pserver->child_type == CHILD_TYPE_RDB &&\n            g_pserver->rdb_child_type == RDB_CHILD_TYPE_DISK &&\n            anyOtherSlaveWaitRdb(c) == 0)\n        {\n            killRDBChild();\n        }\n        if (c->replstate == SLAVE_STATE_SEND_BULK) {\n            if (c->repldbfd != -1) close(c->repldbfd);\n            if (c->replpreamble) sdsfree(c->replpreamble);\n        }\n        list *l = (c->flags & CLIENT_MONITOR) ? g_pserver->monitors : g_pserver->slaves;\n        ln = listSearchKey(l,c);\n        serverAssert(ln != NULL);\n        listDelNode(l,ln);\n        g_pserver->rgthreadvar[c->iel].cclientsReplica--;\n        /* We need to remember the time when we started to have zero\n         * attached slaves, as after some time we'll free the replication\n         * backlog. */\n        if (getClientType(c) == CLIENT_TYPE_SLAVE && listLength(g_pserver->slaves) == 0)\n            g_pserver->repl_no_slaves_since = g_pserver->unixtime;\n        refreshGoodSlavesCount();\n        /* Fire the replica change modules event. */\n        if (c->replstate == SLAVE_STATE_ONLINE)\n            moduleFireServerEvent(REDISMODULE_EVENT_REPLICA_CHANGE,\n                                  REDISMODULE_SUBEVENT_REPLICA_CHANGE_OFFLINE,\n                                  NULL);\n    }\n\n    /* Master/replica cleanup Case 2:\n     * we lost the connection with the master. */\n    if (c->flags & CLIENT_MASTER) replicationHandleMasterDisconnection(MasterInfoFromClient(c));\n\n   /* Remove the contribution that this client gave to our\n     * incrementally computed memory usage. */\n    g_pserver->stat_clients_type_memory[c->client_cron_last_memory_type] -=\n        c->client_cron_last_memory_usage;\n\n    /* Release other dynamically allocated client structure fields,\n     * and finally release the client structure itself. */\n    zfree(c->replyAsync);\n    if (c->name) decrRefCount(c->name);\n    zfree(c->argv);\n    c->argv_len_sumActive = 0;\n    freeClientMultiState(c);\n    sdsfree(c->peerid);\n    sdsfree(c->sockname);\n    sdsfree(c->slave_addr);\n    ulock.unlock();\n    fastlock_free(&c->lock);\n    delete c;\n    return true;\n}\n\nfastlock g_lockasyncfree {\"async free lock\"};\n\n/* Schedule a client to free it at a safe time in the serverCron() function.\n * This function is useful when we need to terminate a client but we are in\n * a context where calling freeClient() is not possible, because the client\n * should be valid for the continuation of the flow of the program. */\nvoid freeClientAsync(client *c) {\n    /* We need to handle concurrent access to the g_pserver->clients_to_close list\n     * only in the freeClientAsync() function, since it's the only function that\n     * may access the list while Redis uses I/O threads. All the other accesses\n     * are in the context of the main thread while the other threads are\n     * idle. */\n    if (c->flags & CLIENT_CLOSE_ASAP || c->flags & CLIENT_LUA) return;  // check without the lock first\n    std::lock_guard<decltype(c->lock)> clientlock(c->lock);\n    if (c->flags & CLIENT_CLOSE_ASAP || c->flags & CLIENT_LUA) return;  // race condition after we acquire the lock\n    c->flags |= CLIENT_CLOSE_ASAP;\n    c->repl_down_since = g_pserver->unixtime;\n    std::unique_lock<fastlock> ul(g_lockasyncfree);\n    listAddNodeTail(g_pserver->clients_to_close,c);\n}\n\nint freeClientsInAsyncFreeQueue(int iel) {\n    serverAssert(GlobalLocksAcquired());\n    std::unique_lock<fastlock> ul(g_lockasyncfree);\n    listIter li;\n    listNode *ln;\n    listRewind(g_pserver->clients_to_close,&li);\n\n    // Store the clients in a temp vector since freeClient will modify this list\n    std::vector<client*> vecclientsFree;\n    while((ln = listNext(&li))) \n    {\n        client *c = (client*)listNodeValue(ln);\n        if (c->iel == iel && !(c->flags & CLIENT_PROTECTED) && !c->casyncOpsPending)\n        {\n            vecclientsFree.push_back(c);\n            listDelNode(g_pserver->clients_to_close, ln);\n        }\n    }\n    ul.unlock();\n\n    for (client *c : vecclientsFree)\n    {\n        c->flags &= ~CLIENT_CLOSE_ASAP;\n        freeClient(c);\n    }\n    return (int)vecclientsFree.size();\n}\n\n/* Return a client by ID, or NULL if the client ID is not in the set\n * of registered clients. Note that \"fake clients\", created with -1 as FD,\n * are not registered clients. */\nclient *lookupClientByID(uint64_t id) {\n    id = htonu64(id);\n    client *c = (client*)raxFind(g_pserver->clients_index,(unsigned char*)&id,sizeof(id));\n    return (c == raxNotFound) ? NULL : c;\n}\n\nlong long getReplIndexFromOffset(long long offset);\n\n/* Write data in output buffers to client. Return C_OK if the client\n * is still valid after the call, C_ERR if it was freed because of some\n * error.  If handler_installed is set, it will attempt to clear the\n * write event.\n *\n * This function is called by threads, but always with handler_installed\n * set to 0. So when handler_installed is set to 0 the function must be\n * thread safe. */\nint writeToClient(client *c, int handler_installed) {\n    /* Update total number of writes on server */\n    g_pserver->stat_total_writes_processed.fetch_add(1, std::memory_order_relaxed);\n\n    ssize_t nwritten = 0, totwritten = 0;\n    clientReplyBlock *o;\n    serverAssertDebug(FCorrectThread(c));\n\n    std::unique_lock<decltype(c->lock)> lock(c->lock);\n\n    /* We can only directly read from the replication backlog if the client \n       is a replica, so only attempt to do so if that's the case. */\n    if (c->flags & CLIENT_SLAVE && !(c->flags & CLIENT_MONITOR) && c->replstate == SLAVE_STATE_ONLINE) {\n        std::unique_lock<fastlock> repl_backlog_lock (g_pserver->repl_backlog_lock);\n        // Ensure all writes to the repl backlog are visible\n        std::atomic_thread_fence(std::memory_order_acquire);\n\n        while (clientHasPendingReplies(c)) {\n            long long repl_end_idx = getReplIndexFromOffset(c->repl_end_off);\n            serverAssert(c->repl_curr_off != -1);\n\n            if (c->repl_curr_off != c->repl_end_off){\n                long long repl_curr_idx = getReplIndexFromOffset(c->repl_curr_off); \n                long long nwritten2ndStage = 0; /* How much was written from the start of the replication backlog\n                                                * in the event of a wrap around write */\n                /* normal case with no wrap around */\n                if (repl_end_idx >= repl_curr_idx){\n                    nwritten = connWrite(c->conn, g_pserver->repl_backlog + repl_curr_idx, repl_end_idx - repl_curr_idx);\n                /* wrap around case */\n                } else {\n                    nwritten = connWrite(c->conn, g_pserver->repl_backlog + repl_curr_idx, g_pserver->repl_backlog_size - repl_curr_idx);\n                    /* only attempt wrapping if we write the correct number of bytes */\n                    if (nwritten == g_pserver->repl_backlog_size - repl_curr_idx){\n                        nwritten2ndStage = connWrite(c->conn, g_pserver->repl_backlog, repl_end_idx);\n                        if (nwritten2ndStage != -1)\n                            nwritten += nwritten2ndStage;\n                    }                \n                }\n\n                /* only increment bytes if an error didn't occur */\n                if (nwritten > 0){\n                    totwritten += nwritten;\n                    c->repl_curr_off += nwritten;\n                    serverAssert(c->repl_curr_off <= c->repl_end_off);\n                }\n\n                /* If the second part of a write didn't go through, we still need to register that */\n                if (nwritten2ndStage == -1) nwritten = -1;\n                if (nwritten == -1)\n                    break;\n            } else {\n                break;\n            }\n        }\n    } else {\n        while(clientHasPendingReplies(c)) {\n            if (c->bufpos > 0) {\n                auto bufpos = c->bufpos;\n                lock.unlock();\n                nwritten = connWrite(c->conn,c->buf+c->sentlen,bufpos-c->sentlen);\n                lock.lock();\n                if (nwritten <= 0) break;\n                c->sentlen += nwritten;\n                totwritten += nwritten;\n\n                /* If the buffer was sent, set bufpos to zero to continue with\n                * the remainder of the reply. */\n                if ((int)c->sentlen == c->bufpos) {\n                    c->bufpos = 0;\n                    c->sentlen = 0;\n                }\n            } else {\n                o = (clientReplyBlock*)listNodeValue(listFirst(c->reply));\n                if (o->used == 0) {\n                    c->reply_bytes -= o->size;\n                    listDelNode(c->reply,listFirst(c->reply));\n                    continue;\n                }\n\n                auto used = o->used;\n                lock.unlock();\n                nwritten = connWrite(c->conn, o->buf() + c->sentlen, used - c->sentlen);\n                lock.lock();\n                if (nwritten <= 0) break;\n                c->sentlen += nwritten;\n                totwritten += nwritten;\n                \n                /* If we fully sent the object on head go to the next one */\n                if (c->sentlen == o->used) {\n                    c->reply_bytes -= o->size;\n                    listDelNode(c->reply,listFirst(c->reply));\n                    c->sentlen = 0;\n                    /* If there are no longer objects in the list, we expect\n                        * the count of reply bytes to be exactly zero. */\n                    if (listLength(c->reply) == 0)\n                        serverAssert(c->reply_bytes == 0);\n                }\n            }\n            /* Note that we avoid to send more than NET_MAX_WRITES_PER_EVENT\n            * bytes, in a single threaded server it's a good idea to serve\n            * other clients as well, even if a very large request comes from\n            * super fast link that is always able to accept data (in real world\n            * scenario think about 'KEYS *' against the loopback interface).\n            *\n            * However if we are over the maxmemory limit we ignore that and\n            * just deliver as much data as it is possible to deliver.\n            *\n            * Moreover, we also send as much as possible if the client is\n            * a replica or a monitor (otherwise, on high-speed traffic, the\n            * replication/output buffer will grow indefinitely) */\n            if (totwritten > NET_MAX_WRITES_PER_EVENT &&\n                (g_pserver->maxmemory == 0 ||\n                zmalloc_used_memory() < g_pserver->maxmemory) &&\n                !(c->flags & CLIENT_SLAVE)) break;\n        }\n    }\n    g_pserver->stat_net_output_bytes += totwritten;\n    if (nwritten == -1) {\n        if (connGetState(c->conn) != CONN_STATE_CONNECTED) {\n            serverLog(LL_VERBOSE,\n                \"Error writing to client: %s\", connGetLastError(c->conn));\n            freeClientAsync(c);\n            \n            return C_ERR;\n        }\n    }\n    if (totwritten > 0) {\n        /* For clients representing masters we don't count sending data\n         * as an interaction, since we always send REPLCONF ACK commands\n         * that take some time to just fill the socket output buffer.\n         * We just rely on data / pings received for timeout detection. */\n        if (!(c->flags & CLIENT_MASTER)) c->lastinteraction = g_pserver->unixtime;\n    }\n    if (!clientHasPendingReplies(c)) {\n        c->sentlen = 0;\n        if (handler_installed) connSetWriteHandler(c->conn, NULL);\n\n        /* Close connection after entire reply has been sent. */\n        if (c->flags & CLIENT_CLOSE_AFTER_REPLY) {\n            freeClientAsync(c);\n            return C_ERR;\n        }\n    }\n    return C_OK;\n}\n\n/* Write event handler. Just send data to the client. */\nvoid sendReplyToClient(connection *conn) {\n    client *c = (client*)connGetPrivateData(conn);\n    if (writeToClient(c,1) == C_ERR)\n    {\n        AeLocker ae;\n        c->lock.lock();\n        ae.arm(c);\n        if (c->flags & CLIENT_CLOSE_ASAP)\n        {\n            if (!freeClient(c))\n                c->lock.unlock();\n        }\n    }\n}\n\nvoid ProcessPendingAsyncWrites()\n{\n    if (serverTL == nullptr)\n        return; // module fake call\n\n    serverAssert(GlobalLocksAcquired());\n\n    while(listLength(serverTL->clients_pending_asyncwrite)) {\n        client *c = (client*)listNodeValue(listFirst(serverTL->clients_pending_asyncwrite));\n        listDelNode(serverTL->clients_pending_asyncwrite, listFirst(serverTL->clients_pending_asyncwrite));\n        std::lock_guard<decltype(c->lock)> lock(c->lock);\n\n        serverAssert(c->fPendingAsyncWrite);\n        if (c->flags & (CLIENT_CLOSE_ASAP | CLIENT_CLOSE_AFTER_REPLY))\n        {\n            if (c->replyAsync != nullptr){\n                zfree(c->replyAsync);\n                c->replyAsync = nullptr;\n            }\n            c->fPendingAsyncWrite = FALSE;\n            continue;\n        }\n\n        /* since writes from master to replica can come directly from the replication backlog,\n         * writes may have been signalled without having been copied to the replyAsync buffer,\n         * thus causing the buffer to be NULL */ \n        if (c->replyAsync != nullptr){\n            size_t size = c->replyAsync->used;\n\n            if (listLength(c->reply) == 0 && size <= static_cast<size_t>(PROTO_REPLY_CHUNK_BYTES - c->bufpos)) {\n                memcpy(c->buf + c->bufpos, c->replyAsync->buf(), size);\n                c->bufpos += size;\n            } else {\n                c->reply_bytes += c->replyAsync->size;\n                listAddNodeTail(c->reply, c->replyAsync);\n                c->replyAsync = nullptr;\n            }\n\n            zfree(c->replyAsync);\n            c->replyAsync = nullptr;\n        } else {\n            /* Only replicas should have empty async reply buffers */\n            serverAssert(c->flags & CLIENT_SLAVE);\n        }\n\n        c->fPendingAsyncWrite = FALSE;\n\n        if (!((c->replstate == REPL_STATE_NONE || c->replstate == SLAVE_STATE_FASTSYNC_TX ||\n         (c->replstate == SLAVE_STATE_ONLINE && !c->repl_put_online_on_ack))))\n            continue;\n\n        closeClientOnOutputBufferLimitReached(c, 1);\n        if (c->flags & CLIENT_CLOSE_ASAP)\n            continue;   // we will never write this so don't post an op\n\n        std::atomic_thread_fence(std::memory_order_seq_cst);\n\n        if (FCorrectThread(c))\n        {\n            prepareClientToWrite(c); // queue an event\n        }\n        else\n        {\n            bool expected = false;\n            if (c->fPendingAsyncWriteHandler.compare_exchange_strong(expected, true)) {\n                bool fResult = c->postFunction([](client *c) {\n                    c->fPendingAsyncWriteHandler = false;\n                    clientInstallWriteHandler(c);\n                    c->lock.unlock();\n                    handleClientsWithPendingWrites(c->iel, g_pserver->aof_state);\n                    c->lock.lock();\n                }, false);\n\n                if (!fResult)\n                    c->fPendingAsyncWriteHandler = false;   // if we failed to set the handler then prevent this from never being reset\n            }\n        }\n    }\n}\n\n/* This function is called just before entering the event loop, in the hope\n * we can just write the replies to the client output buffer without any\n * need to use a syscall in order to install the writable event handler,\n * get it called, and so forth. */\nint handleClientsWithPendingWrites(int iel, int aof_state) {\n    int processed = 0;\n    serverAssert(iel == (serverTL - g_pserver->rgthreadvar));\n\n    if (listLength(serverTL->clients_pending_asyncwrite))\n    {\n        AeLocker locker;\n        locker.arm(nullptr);\n        ProcessPendingAsyncWrites();\n    }\n\n    int ae_flags = AE_WRITABLE|AE_WRITE_THREADSAFE;\n    /* For the fsync=always policy, we want that a given FD is never\n        * served for reading and writing in the same event loop iteration,\n        * so that in the middle of receiving the query, and serving it\n        * to the client, we'll call beforeSleep() that will do the\n        * actual fsync of AOF to disk. AE_BARRIER ensures that. */\n    if (aof_state == AOF_ON &&\n        g_pserver->aof_fsync == AOF_FSYNC_ALWAYS)\n    {\n        ae_flags |= AE_BARRIER;\n    }\n\n    std::unique_lock<fastlock> lockf(g_pserver->rgthreadvar[iel].lockPendingWrite);\n    auto vec = std::move(g_pserver->rgthreadvar[iel].clients_pending_write);\n    lockf.unlock();\n    processed += (int)vec.size();\n\n    for (client *c : vec) {\n        serverAssertDebug(FCorrectThread(c));\n\n        uint64_t flags = c->flags.fetch_and(~CLIENT_PENDING_WRITE, std::memory_order_relaxed);\n\n        /* If a client is protected, don't do anything,\n        * that may trigger write error or recreate handler. */\n        if ((flags & CLIENT_PROTECTED) && !(flags & CLIENT_SLAVE)) continue;\n\n        /* Don't write to clients that are going to be closed anyway. */\n        if (c->flags & CLIENT_CLOSE_ASAP) continue;\n\n        /* Try to write buffers to the client socket, unless its a replica in multithread mode */\n        if (writeToClient(c,0) == C_ERR) \n        {\n            if (c->flags & CLIENT_CLOSE_ASAP)\n            {\n                AeLocker ae;\n                ae.arm(nullptr);\n                freeClient(c); // writeToClient will only async close, but there's no need to wait\n            }\n            continue;\n        }\n\n        /* If after the synchronous writes above we still have data to\n        * output to the client, we need to install the writable handler. */\n        std::unique_lock<decltype(c->lock)> lock(c->lock);\n        if (clientHasPendingReplies(c)) {\n            if (connSetWriteHandlerWithBarrier(c->conn, sendReplyToClient, ae_flags, true) == C_ERR) {\n                freeClientAsync(c);\n            }\n        }\n    }\n\n    return processed;\n}\n\n/* resetClient prepare the client to process the next command */\nvoid resetClient(client *c) {\n    redisCommandProc *prevcmd = c->cmd ? c->cmd->proc : NULL;\n\n    freeClientArgv(c);\n\n    /* We clear the ASKING flag as well if we are not inside a MULTI, and\n     * if what we just executed is not the ASKING command itself. */\n    if (!(c->flags & CLIENT_MULTI) && prevcmd != askingCommand)\n        c->flags &= ~CLIENT_ASKING;\n\n    /* We do the same for the CACHING command as well. It also affects\n     * the next command or transaction executed, in a way very similar\n     * to ASKING. */\n    if (!(c->flags & CLIENT_MULTI) && prevcmd != clientCommand)\n        c->flags &= ~CLIENT_TRACKING_CACHING;\n\n    /* Remove the CLIENT_REPLY_SKIP flag if any so that the reply\n     * to the next command will be sent, but set the flag if the command\n     * we just processed was \"CLIENT REPLY SKIP\". */\n    c->flags &= ~CLIENT_REPLY_SKIP;\n    if (c->flags & CLIENT_REPLY_SKIP_NEXT) {\n        c->flags |= CLIENT_REPLY_SKIP;\n        c->flags &= ~CLIENT_REPLY_SKIP_NEXT;\n    }\n}\n\n/* This function is used when we want to re-enter the event loop but there\n * is the risk that the client we are dealing with will be freed in some\n * way. This happens for instance in:\n *\n * * DEBUG RELOAD and similar.\n * * When a Lua script is in -BUSY state.\n *\n * So the function will protect the client by doing two things:\n *\n * 1) It removes the file events. This way it is not possible that an\n *    error is signaled on the socket, freeing the client.\n * 2) Moreover it makes sure that if the client is freed in a different code\n *    path, it is not really released, but only marked for later release. */\nvoid protectClient(client *c) {\n    c->flags |= CLIENT_PROTECTED;\n    AssertCorrectThread(c);\n    if (c->conn) {\n        connSetReadHandler(c->conn,NULL);\n        connSetWriteHandler(c->conn,NULL);\n    }\n}\n\n/* This will undo the client protection done by protectClient() */\nvoid unprotectClient(client *c) {\n    AssertCorrectThread(c);\n    if (c->flags & CLIENT_PROTECTED) {\n        c->flags &= ~CLIENT_PROTECTED;\n        if (c->conn) {\n            connSetReadHandler(c->conn,readQueryFromClient, true);\n            if (clientHasPendingReplies(c)) clientInstallWriteHandler(c);\n        }\n    }\n}\n\n/* Like processMultibulkBuffer(), but for the inline protocol instead of RESP,\n * this function consumes the client query buffer and creates a command ready\n * to be executed inside the client structure. Returns C_OK if the command\n * is ready to be executed, or C_ERR if there is still protocol to read to\n * have a well formed command. The function also returns C_ERR when there is\n * a protocol error: in such a case the client structure is setup to reply\n * with the error and close the connection. */\nint processInlineBuffer(client *c) {\n    char *newline;\n    int argc, j, linefeed_chars = 1;\n    sds *argv, aux;\n    size_t querylen;\n\n    /* Search for end of line */\n    newline = strchr(c->querybuf+c->qb_pos,'\\n');\n\n    /* Nothing to do without a \\r\\n */\n    if (newline == NULL) {\n        if (sdslen(c->querybuf)-c->qb_pos > PROTO_INLINE_MAX_SIZE) {\n            addReplyError(c,\"Protocol error: too big inline request\");\n            setProtocolError(\"too big inline request\",c);\n        }\n        return C_ERR;\n    }\n\n    /* Handle the \\r\\n case. */\n    if (newline != c->querybuf+c->qb_pos && *(newline-1) == '\\r')\n        newline--, linefeed_chars++;\n\n    /* Split the input buffer up to the \\r\\n */\n    querylen = newline-(c->querybuf+c->qb_pos);\n    aux = sdsnewlen(c->querybuf+c->qb_pos,querylen);\n    argv = sdssplitargs(aux,&argc);\n    sdsfree(aux);\n    if (argv == NULL) {\n        addReplyError(c,\"Protocol error: unbalanced quotes in request\");\n        setProtocolError(\"unbalanced quotes in inline request\",c);\n        return C_ERR;\n    }\n\n    /* Newline from slaves can be used to refresh the last ACK time.\n     * This is useful for a replica to ping back while loading a big\n     * RDB file. */\n    if (querylen == 0 && getClientType(c) == CLIENT_TYPE_SLAVE)\n        c->repl_ack_time = g_pserver->unixtime;\n\n    /* Masters should never send us inline protocol to run actual\n     * commands. If this happens, it is likely due to a bug in Redis where\n     * we got some desynchronization in the protocol, for example\n     * beause of a PSYNC gone bad.\n     *\n     * However the is an exception: masters may send us just a newline\n     * to keep the connection active. */\n    if (querylen != 0 && c->flags & CLIENT_MASTER) {\n        sdsfreesplitres(argv,argc);\n        serverLog(LL_WARNING,\"WARNING: Receiving inline protocol from master, master stream corruption? Closing the master connection and discarding the cached master.\");\n        setProtocolError(\"Master using the inline protocol. Desync?\",c);\n        return C_ERR;\n    }\n\n    /* Move querybuffer position to the next query in the buffer. */\n    c->qb_pos += querylen+linefeed_chars;\n\n    /* Setup argv array on client structure */\n    if (argc) {\n        /* Create redis objects for all arguments. */\n        c->vecqueuedcmd.emplace_back(argc);\n        auto &cmd = c->vecqueuedcmd.back();\n        for (cmd.argc = 0, j = 0; j < argc; j++) {\n            cmd.argv[cmd.argc++] = createObject(OBJ_STRING,argv[j]);\n            cmd.argv_len_sum += sdslen(argv[j]);\n        }\n    }\n    sds_free(argv);\n    return C_OK;\n}\n\n/* Helper function. Record protocol erro details in server log,\n * and set the client as CLIENT_CLOSE_AFTER_REPLY and\n * CLIENT_PROTOCOL_ERROR. */\n#define PROTO_DUMP_LEN 128\nstatic void setProtocolError(const char *errstr, client *c) {\n    if (cserver.verbosity <= LL_VERBOSE || c->flags & CLIENT_MASTER) {\n        sds client = catClientInfoString(sdsempty(),c);\n\n        /* Sample some protocol to given an idea about what was inside. */\n        char buf[256];\n        if (sdslen(c->querybuf)-c->qb_pos < PROTO_DUMP_LEN) {\n            snprintf(buf,sizeof(buf),\"Query buffer during protocol error: '%s'\", c->querybuf+c->qb_pos);\n        } else {\n            snprintf(buf,sizeof(buf),\"Query buffer during protocol error: '%.*s' (... more %zu bytes ...) '%.*s'\", PROTO_DUMP_LEN/2, c->querybuf+c->qb_pos, sdslen(c->querybuf)-c->qb_pos-PROTO_DUMP_LEN, PROTO_DUMP_LEN/2, c->querybuf+sdslen(c->querybuf)-PROTO_DUMP_LEN/2);\n        }\n\n        /* Remove non printable chars. */\n        char *p = buf;\n        while (*p != '\\0') {\n            if (!isprint(*p)) *p = '.';\n            p++;\n        }\n\n        /* Log all the client and protocol info. */\n        int loglevel = (c->flags & CLIENT_MASTER) ? LL_WARNING :\n                                                    LL_VERBOSE;\n        serverLog(loglevel,\n            \"Protocol error (%s) from client: %s. %s\", errstr, client, buf);\n        sdsfree(client);\n    }\n    c->flags |= (CLIENT_CLOSE_AFTER_REPLY|CLIENT_PROTOCOL_ERROR);\n}\n\n/* Process the query buffer for client 'c', setting up the client argument\n * vector for command execution. Returns C_OK if after running the function\n * the client has a well-formed ready to be processed command, otherwise\n * C_ERR if there is still to read more buffer to get the full command.\n * The function also returns C_ERR when there is a protocol error: in such a\n * case the client structure is setup to reply with the error and close\n * the connection.\n *\n * This function is called if processInputBuffer() detects that the next\n * command is in RESP format, so the first byte in the command is found\n * to be '*'. Otherwise for inline commands processInlineBuffer() is called. */\nint processMultibulkBuffer(client *c) {\n    char *newline = NULL;\n    int ok;\n    long long ll;\n\n    if (c->multibulklen == 0) {\n        /* The client should have been reset */\n        serverAssertWithInfo(c,NULL,c->argc == 0);\n\n        /* Multi bulk length cannot be read without a \\r\\n */\n        newline = strchr(c->querybuf+c->qb_pos,'\\r');\n        if (newline == NULL) {\n            if (sdslen(c->querybuf)-c->qb_pos > PROTO_INLINE_MAX_SIZE) {\n                addReplyError(c,\"Protocol error: too big mbulk count string\");\n                setProtocolError(\"too big mbulk count string\",c);\n            }\n            return C_ERR;\n        }\n\n        /* Buffer should also contain \\n */\n        if (newline-(c->querybuf+c->qb_pos) > (ssize_t)(sdslen(c->querybuf)-c->qb_pos-2))\n            return C_ERR;\n\n        /* We know for sure there is a whole line since newline != NULL,\n         * so go ahead and find out the multi bulk length. */\n        serverAssertWithInfo(c,NULL,c->querybuf[c->qb_pos] == '*');\n        ok = string2ll(c->querybuf+1+c->qb_pos,newline-(c->querybuf+1+c->qb_pos),&ll);\n        if (!ok || ll > 1024*1024) {\n            addReplyError(c,\"Protocol error: invalid multibulk length\");\n            setProtocolError(\"invalid mbulk count\",c);\n            return C_ERR;\n        } else if (ll > 10 && authRequired(c)) {\n            addReplyError(c, \"Protocol error: unauthenticated multibulk length\");\n            setProtocolError(\"unauth mbulk count\", c);\n            return C_ERR;\n        }\n\n        c->qb_pos = (newline-c->querybuf)+2;\n\n        if (ll <= 0) return C_OK;\n\n        c->multibulklen = ll;\n\n        /* Setup argv array on client structure */\n        c->vecqueuedcmd.emplace_back(c->multibulklen);\n    }\n\n    serverAssertWithInfo(c,NULL,c->multibulklen > 0);\n    while(c->multibulklen) {\n        /* Read bulk length if unknown */\n        if (c->bulklen == -1) {\n            newline = strchr(c->querybuf+c->qb_pos,'\\r');\n            if (newline == NULL) {\n                if (sdslen(c->querybuf)-c->qb_pos > PROTO_INLINE_MAX_SIZE) {\n                    addReplyError(c,\n                        \"Protocol error: too big bulk count string\");\n                    setProtocolError(\"too big bulk count string\",c);\n                    return C_ERR;\n                }\n                break;\n            }\n\n            /* Buffer should also contain \\n */\n            if (newline-(c->querybuf+c->qb_pos) > (ssize_t)(sdslen(c->querybuf)-c->qb_pos-2))\n                break;\n\n            if (c->querybuf[c->qb_pos] != '$') {\n                addReplyErrorFormat(c,\n                    \"Protocol error: expected '$', got '%c'\",\n                    c->querybuf[c->qb_pos]);\n                setProtocolError(\"expected $ but got something else\",c);\n                return C_ERR;\n            }\n\n            ok = string2ll(c->querybuf+c->qb_pos+1,newline-(c->querybuf+c->qb_pos+1),&ll);\n            if (!ok || ll < 0 ||\n                (!(c->flags & CLIENT_MASTER) && ll > g_pserver->proto_max_bulk_len)) {\n                addReplyError(c,\"Protocol error: invalid bulk length\");\n                setProtocolError(\"invalid bulk length\",c);\n                return C_ERR;\n            } else if (ll > 16384 && authRequired(c)) {\n                addReplyError(c, \"Protocol error: unauthenticated bulk length\");\n                setProtocolError(\"unauth bulk length\", c);\n                return C_ERR;\n            }\n\n            c->qb_pos = newline-c->querybuf+2;\n            if (ll >= PROTO_MBULK_BIG_ARG) {\n                /* If we are going to read a large object from network\n                 * try to make it likely that it will start at c->querybuf\n                 * boundary so that we can optimize object creation\n                 * avoiding a large copy of data.\n                 *\n                 * But only when the data we have not parsed is less than\n                 * or equal to ll+2. If the data length is greater than\n                 * ll+2, trimming querybuf is just a waste of time, because\n                 * at this time the querybuf contains not only our bulk. */\n                if (sdslen(c->querybuf)-c->qb_pos <= (size_t)ll+2) {\n                    sdsrange(c->querybuf,c->qb_pos,-1);\n                    c->qb_pos = 0;\n                    /* Hint the sds library about the amount of bytes this string is\n                     * going to contain. */\n                    c->querybuf = sdsMakeRoomFor(c->querybuf,ll+2-sdslen(c->querybuf));\n                }\n            }\n            c->bulklen = ll;\n        }\n\n        /* Read bulk argument */\n        if (sdslen(c->querybuf)-c->qb_pos < (size_t)(c->bulklen+2)) {\n            /* Not enough data (+2 == trailing \\r\\n) */\n            break;\n        } else {\n            /* Optimization: if the buffer contains JUST our bulk element\n             * instead of creating a new object by *copying* the sds we\n             * just use the current sds string. */\n            auto &cmd = c->vecqueuedcmd.back();\n            if (c->qb_pos == 0 &&\n                c->bulklen >= PROTO_MBULK_BIG_ARG &&\n                sdslen(c->querybuf) == (size_t)(c->bulklen+2))\n            {\n                cmd.argv[cmd.argc++] = createObject(OBJ_STRING,c->querybuf);\n                cmd.argv_len_sum += c->bulklen;\n                sdsIncrLen(c->querybuf,-2); /* remove CRLF */\n                /* Assume that if we saw a fat argument we'll see another one\n                 * likely... */\n                c->querybuf = sdsnewlen(SDS_NOINIT,c->bulklen+2);\n                sdsclear(c->querybuf);\n            } else {\n                cmd.argv[cmd.argc++] =\n                    createStringObject(c->querybuf+c->qb_pos,c->bulklen);\n                cmd.argv_len_sum += c->bulklen;\n                c->qb_pos += c->bulklen+2;\n            }\n            c->bulklen = -1;\n            c->multibulklen--;\n        }\n    }\n\n    /* We're done when c->multibulk == 0 */\n    if (c->multibulklen == 0) return C_OK;\n\n    /* Still not ready to process the command */\n    return C_ERR;\n}\n\n/* Perform necessary tasks after a command was executed:\n *\n * 1. The client is reset unless there are reasons to avoid doing it. \n * 2. In the case of master clients, the replication offset is updated.\n * 3. Propagate commands we got from our master to replicas down the line. */\nvoid commandProcessed(client *c, int flags) {\n        /* If client is blocked(including paused), just return avoid reset and replicate.\n     *\n     * 1. Don't reset the client structure for blocked clients, so that the reply\n     *    callback will still be able to access the client argv and argc fields.\n     *    The client will be reset in unblockClient().\n     * 2. Don't update replication offset or propagate commands to replicas,\n     *    since we have not applied the command. */\n    if (c->flags & CLIENT_BLOCKED) return;\n\n    resetClient(c);\n\n    long long prev_offset = c->reploff;\n    if (c->flags & CLIENT_MASTER && !(c->flags & CLIENT_MULTI)) {\n        /* Update the applied replication offset of our master. */\n        serverAssert(c->reploff <= c->reploff_cmd);\n        c->reploff = c->reploff_cmd;\n    }\n\n    /* If the client is a master we need to compute the difference\n     * between the applied offset before and after processing the buffer,\n     * to understand how much of the replication stream was actually\n     * applied to the master state: this quantity, and its corresponding\n     * part of the replication stream, will be propagated to the\n     * sub-replicas and to the replication backlog. */\n    if (c->flags & CLIENT_MASTER) {\n        AeLocker ae;\n        ae.arm(c);\n        long long applied = c->reploff - prev_offset;\n        if (applied) {\n            if (!g_pserver->fActiveReplica && (flags & CMD_CALL_PROPAGATE))\n            {\n                replicationFeedSlavesFromMasterStream(c->pending_querybuf, applied);\n            }\n            sdsrange(c->pending_querybuf,applied,-1);\n        }\n    }\n}\n\n/* This function calls processCommand(), but also performs a few sub tasks\n * for the client that are useful in that context:\n *\n * 1. It sets the current client to the client 'c'.\n * 2. calls commandProcessed() if the command was handled.\n *\n * The function returns C_ERR in case the client was freed as a side effect\n * of processing the command, otherwise C_OK is returned. */\nint processCommandAndResetClient(client *c, int flags) {\n    int deadclient = 0;\n    client *old_client = serverTL->current_client;\n    serverTL->current_client = c;\n    serverAssert((flags & CMD_CALL_ASYNC) || GlobalLocksAcquired());\n    \n    if (processCommand(c, flags) == C_OK) {\n        commandProcessed(c, flags);\n    }\n    if (serverTL->current_client == NULL) deadclient = 1;\n    /*\n     * Restore the old client, this is needed because when a script\n     * times out, we will get into this code from processEventsWhileBlocked.\n     * Which will cause to set the server.current_client. If not restored\n     * we will return 1 to our caller which will falsely indicate the client\n     * is dead and will stop reading from its buffer.\n     */\n    serverTL->current_client = old_client;\n    /* performEvictions may flush slave output buffers. This may\n     * result in a replica, that may be the active client, to be\n     * freed. */\n    return deadclient ? C_ERR : C_OK;\n}\n\n/* This function will execute any fully parsed commands pending on\n * the client. Returns C_ERR if the client is no longer valid after executing\n * the command, and C_OK for all other cases. */\nint processPendingCommandsAndResetClient(client *c, int flags) {\n    if (c->flags & CLIENT_PENDING_COMMAND) {\n        c->flags &= ~CLIENT_PENDING_COMMAND;\n        if (processCommandAndResetClient(c, flags) == C_ERR) {\n            return C_ERR;\n        }\n    }\n    return C_OK;\n}\n\nbool FClientReady(client *c) {\n    /* Immediately abort if the client is in the middle of something. */\n    if (c->flags & CLIENT_BLOCKED) return false;\n    if (c->flags & CLIENT_PENDING_COMMAND) return false;\n\n    if (c->flags & CLIENT_EXECUTING_COMMAND) return false;\n\n    /* Don't process input from the master while there is a busy script\n        * condition on the replica. We want just to accumulate the replication\n        * stream (instead of replying -BUSY like we do with other clients) and\n        * later resume the processing. */\n    if (g_pserver->lua_timedout && c->flags & CLIENT_MASTER) return false;\n\n    /* CLIENT_CLOSE_AFTER_REPLY closes the connection once the reply is\n        * written to the client. Make sure to not let the reply grow after\n        * this flag has been set (i.e. don't process more commands).\n        *\n        * The same applies for clients we want to terminate ASAP. */\n    if (c->flags & (CLIENT_CLOSE_AFTER_REPLY|CLIENT_CLOSE_ASAP)) return false;\n\n    return true;\n}\n\nvoid parseClientCommandBuffer(client *c) {\n    if (!FClientReady(c))\n        return;\n    \n    while(c->qb_pos < sdslen(c->querybuf)) {    \n        /* Determine request type when unknown. */\n        if (!c->reqtype) {\n            if (c->querybuf[c->qb_pos] == '*') {\n                c->reqtype = PROTO_REQ_MULTIBULK;\n            } else {\n                c->reqtype = PROTO_REQ_INLINE;\n            }\n        }\n\n        size_t cqueriesStart = c->vecqueuedcmd.size();\n        if (c->reqtype == PROTO_REQ_INLINE) {\n            if (processInlineBuffer(c) != C_OK) break;\n        } else if (c->reqtype == PROTO_REQ_MULTIBULK) {\n            if (processMultibulkBuffer(c) != C_OK) break;\n        } else {\n            serverPanic(\"Unknown request type\");\n        }\n        if (!c->vecqueuedcmd.empty() && (c->vecqueuedcmd.back().argc <= 0 || c->vecqueuedcmd.back().argv == nullptr)) {\n            c->vecqueuedcmd.pop_back();\n        } else if (!c->vecqueuedcmd.empty()) {\n            if (c->flags & CLIENT_MASTER) c->vecqueuedcmd.back().reploff = c->read_reploff - sdslen(c->querybuf) + c->qb_pos;\n            serverAssert(c->vecqueuedcmd.back().reploff >= 0);\n        }\n\n        /* Prefetch outside the lock for better perf */\n        if (g_pserver->prefetch_enabled && (cserver.cthreads > 1 || g_pserver->m_pstorageFactory) && cqueriesStart < c->vecqueuedcmd.size() &&\n            (g_pserver->m_pstorageFactory || aeLockContested(cserver.cthreads/2) || cserver.cthreads == 1) && !GlobalLocksAcquired()) {\n            auto &query = c->vecqueuedcmd.back();\n            if (query.argc > 0 && query.argc == query.argcMax) {\n                c->db->prefetchKeysAsync(c, query);\n            }\n        }\n        c->reqtype = 0;\n        c->multibulklen = 0;\n        c->bulklen = -1;\n        c->reqtype = 0;\n    }\n\n    /* Trim to pos */\n    if (c->qb_pos) {\n        sdsrange(c->querybuf,c->qb_pos,-1);\n        c->qb_pos = 0;\n    }\n}\n\nbool FAsyncCommand(parsed_command &cmd)\n{\n    if (serverTL->in_eval || serverTL->in_exec)\n        return false;\n    auto parsedcmd = lookupCommand(szFromObj(cmd.argv[0]));\n    if (parsedcmd == nullptr)\n        return false;\n    static const long long expectedFlags = CMD_ASYNC_OK | CMD_READONLY;\n    return (parsedcmd->flags & expectedFlags) == expectedFlags;\n}\n\n/* This function is called every time, in the client structure 'c', there is\n * more query buffer to process, because we read more data from the socket\n * or because a client was blocked and later reactivated, so there could be\n * pending query buffer, already representing a full command, to process. */\nvoid processInputBuffer(client *c, bool fParse, int callFlags) {\n    AssertCorrectThread(c);\n    \n    if (fParse)\n        parseClientCommandBuffer(c);\n\n    /* Keep processing while there is something in the input buffer */\n    while (!c->vecqueuedcmd.empty()) {\n        /* Return if we're still parsing this command */\n        auto &cmd = c->vecqueuedcmd.front();\n        if (cmd.argc != cmd.argcMax) break;\n        if (c->flags & CLIENT_EXECUTING_COMMAND) break;\n\n        if (!FClientReady(c)) break;\n\n        if ((callFlags & CMD_CALL_ASYNC) && !FAsyncCommand(cmd))\n            break;\n\n        zfree(c->argv);\n        c->argc = cmd.argc;\n        c->argv = cmd.argv;\n        cmd.argv = nullptr;\n        c->argv_len_sumActive = cmd.argv_len_sum;\n        cmd.argv_len_sum = 0;\n        c->reploff_cmd = cmd.reploff;\n        serverAssert(c->argv != nullptr);\n\n        c->vecqueuedcmd.erase(c->vecqueuedcmd.begin());\n\n        /* Multibulk processing could see a <= 0 length. */\n        if (c->argc == 0) {\n            resetClient(c);\n        } else {\n            c->flags |= CLIENT_EXECUTING_COMMAND;\n            /* We are finally ready to execute the command. */\n            if (processCommandAndResetClient(c, callFlags) == C_ERR) {\n                /* If the client is no longer valid, we avoid exiting this\n                 * loop and trimming the client buffer later. So we return\n                 * ASAP in that case. */\n                c->flags &= ~CLIENT_EXECUTING_COMMAND;\n                return;\n            }\n            c->flags &= ~CLIENT_EXECUTING_COMMAND;\n        }\n    }\n}\n\nvoid readQueryFromClient(connection *conn) {\n    client *c = (client*)connGetPrivateData(conn);\n    serverAssert(conn == c->conn);\n    int nread, readlen;\n    size_t qblen;\n\n    serverAssertDebug(FCorrectThread(c));\n    serverAssertDebug(!GlobalLocksAcquired());\n    \n    AeLocker aelock;\n    AssertCorrectThread(c);\n    std::unique_lock<decltype(c->lock)> lock(c->lock, std::defer_lock);\n    if (!lock.try_lock())\n        return; // Process something else while we wait\n\n    /* Update total number of reads on server */\n    g_pserver->stat_total_reads_processed.fetch_add(1, std::memory_order_relaxed);\n\n    readlen = PROTO_IOBUF_LEN;\n    /* If this is a multi bulk request, and we are processing a bulk reply\n     * that is large enough, try to maximize the probability that the query\n     * buffer contains exactly the SDS string representing the object, even\n     * at the risk of requiring more read(2) calls. This way the function\n     * processMultiBulkBuffer() can avoid copying buffers to create the\n     * Redis Object representing the argument. */\n    if (c->reqtype == PROTO_REQ_MULTIBULK && c->multibulklen && c->bulklen != -1\n        && c->bulklen >= PROTO_MBULK_BIG_ARG)\n    {\n        ssize_t remaining = (size_t)(c->bulklen+2)-sdslen(c->querybuf);\n\n        /* Note that the 'remaining' variable may be zero in some edge case,\n         * for example once we resume a blocked client after CLIENT PAUSE. */\n        if (remaining > 0 && remaining < readlen) readlen = remaining;\n    }\n\n    qblen = sdslen(c->querybuf);\n    if (c->querybuf_peak < qblen) c->querybuf_peak = qblen;\n    c->querybuf = sdsMakeRoomFor(c->querybuf, readlen);\n\n    nread = connRead(c->conn, c->querybuf+qblen, readlen);\n    \n    if (nread == -1) {\n        if (connGetState(conn) == CONN_STATE_CONNECTED) {\n            return;\n        } else {\n            serverLog(LL_VERBOSE, \"Reading from client: %s\",connGetLastError(c->conn));\n            aelock.arm(c);\n            freeClientAsync(c);\n            return;\n        }\n    } else if (nread == 0) {\n        serverLog(LL_VERBOSE, \"Client closed connection\");\n        aelock.arm(c);\n        freeClientAsync(c);\n        return;\n    } else if (c->flags & CLIENT_MASTER) {\n        /* Append the query buffer to the pending (not applied) buffer\n         * of the master. We'll use this buffer later in order to have a\n         * copy of the string applied by the last command executed. */\n        c->pending_querybuf = sdscatlen(c->pending_querybuf,\n                                        c->querybuf+qblen,nread);\n    }\n\n    sdsIncrLen(c->querybuf,nread);\n    c->lastinteraction = g_pserver->unixtime;\n    if (c->flags & CLIENT_MASTER) c->read_reploff += nread;\n    g_pserver->stat_net_input_bytes += nread;\n    if (sdslen(c->querybuf) > cserver.client_max_querybuf_len) {\n        sds ci = catClientInfoString(sdsempty(),c), bytes = sdsempty();\n\n        bytes = sdscatrepr(bytes,c->querybuf,64);\n        serverLog(LL_WARNING,\"Closing client that reached max query buffer length: %s (qbuf initial bytes: %s)\", ci, bytes);\n        sdsfree(ci);\n        sdsfree(bytes);       \n        freeClientAsync(c);\n        return;\n    }\n\n    if (cserver.cthreads > 1 || g_pserver->m_pstorageFactory) {\n        parseClientCommandBuffer(c);\n        if (g_pserver->enable_async_commands && !serverTL->disable_async_commands && listLength(g_pserver->monitors) == 0 && (aeLockContention() || serverTL->rgdbSnapshot[c->db->id] || g_fTestMode) && !serverTL->in_eval && !serverTL->in_exec) {\n            // Frequent writers aren't good candidates for this optimization, they cause us to renew the snapshot too often\n            //  so we exclude them unless the snapshot we need already exists.\n            // Note: In test mode we want to create snapshots as often as possibl to excercise them - we don't care about perf\n            bool fSnapshotExists = c->db->mvccLastSnapshot >= c->mvccCheckpoint;\n            bool fWriteTooRecent = !g_fTestMode && (((getMvccTstamp() - c->mvccCheckpoint) >> MVCC_MS_SHIFT) < static_cast<uint64_t>(g_pserver->snapshot_slip)/2);\n\n            // The check below avoids running async commands if this is a frequent writer unless a snapshot is already there to service it\n            if (!fWriteTooRecent || fSnapshotExists) {\n                processInputBuffer(c, false, CMD_CALL_SLOWLOG | CMD_CALL_STATS | CMD_CALL_ASYNC);\n            }\n        }\n        if (!c->vecqueuedcmd.empty())\n            serverTL->vecclientsProcess.push_back(c);\n    } else {\n        // If we're single threaded its actually better to just process the command here while the query is hot in the cache\n        //  multithreaded lock contention dominates and batching is better\n        AeLocker locker;\n        locker.arm(c);\n        runAndPropogateToReplicas(processInputBuffer, c, true /*fParse*/, CMD_CALL_FULL);\n    }\n}\n\nvoid processClients()\n{\n    serverAssert(GlobalLocksAcquired());\n\n    // Note that this function is reentrant and vecclients may be modified by code called from processInputBuffer\n    while (!serverTL->vecclientsProcess.empty()) {\n        client *c = serverTL->vecclientsProcess.front();\n        serverTL->vecclientsProcess.erase(serverTL->vecclientsProcess.begin());\n\n        /* There is more data in the client input buffer, continue parsing it\n        * in case to check if there is a full command to execute. */\n        std::unique_lock<fastlock> ul(c->lock);\n        processInputBuffer(c, false /*fParse*/, CMD_CALL_FULL);\n    }\n\n    if (listLength(serverTL->clients_pending_asyncwrite))\n    {\n        ProcessPendingAsyncWrites();\n    }\n}\n\nvoid getClientsMaxBuffers(unsigned long *longest_output_list,\n                          unsigned long *biggest_input_buffer) {\n    client *c;\n    listNode *ln;\n    listIter li;\n    unsigned long lol = 0, bib = 0;\n\n    listRewind(g_pserver->clients,&li);\n    while ((ln = listNext(&li)) != NULL) {\n        c = (client*)listNodeValue(ln);\n\n        if (listLength(c->reply) > lol) lol = listLength(c->reply);\n        if (sdslen(c->querybuf) > bib) bib = sdslen(c->querybuf);\n    }\n    *longest_output_list = lol;\n    *biggest_input_buffer = bib;\n}\n\n/* A Redis \"Address String\" is a colon separated ip:port pair.\n * For IPv4 it's in the form x.y.z.k:port, example: \"127.0.0.1:1234\".\n * For IPv6 addresses we use [] around the IP part, like in \"[::1]:1234\".\n * For Unix sockets we use path:0, like in \"/tmp/redis:0\".\n *\n * An Address String always fits inside a buffer of NET_ADDR_STR_LEN bytes,\n * including the null term.\n *\n * On failure the function still populates 'addr' with the \"?:0\" string in case\n * you want to relax error checking or need to display something anyway (see\n * anetFdToString implementation for more info). */\nvoid genClientAddrString(client *client, char *addr,\n                         size_t addr_len, int fd_to_str_type) {\n    if (client->flags & CLIENT_UNIX_SOCKET) {\n        /* Unix socket client. */\n        snprintf(addr,addr_len,\"%s:0\",g_pserver->unixsocket);\n    } else {\n        /* TCP client. */\n        connFormatFdAddr(client->conn,addr,addr_len,fd_to_str_type);\n    }\n}\n\n/* This function returns the client peer id, by creating and caching it\n * if client->peerid is NULL, otherwise returning the cached value.\n * The Peer ID never changes during the life of the client, however it\n * is expensive to compute. */\nchar *getClientPeerId(client *c) {\n    char peerid[NET_ADDR_STR_LEN];\n\n    if (c->peerid == NULL) {\n        genClientAddrString(c,peerid,sizeof(peerid),FD_TO_PEER_NAME);\n        c->peerid = sdsnew(peerid);\n    }\n    return c->peerid;\n}\n\n/* This function returns the client bound socket name, by creating and caching\n * it if client->sockname is NULL, otherwise returning the cached value.\n * The Socket Name never changes during the life of the client, however it\n * is expensive to compute. */\nchar *getClientSockname(client *c) {\n    char sockname[NET_ADDR_STR_LEN];\n\n    if (c->sockname == NULL) {\n        genClientAddrString(c,sockname,sizeof(sockname),FD_TO_SOCK_NAME);\n        c->sockname = sdsnew(sockname);\n    }\n    return c->sockname;\n}\n\n/* Concatenate a string representing the state of a client in a human\n * readable format, into the sds string 's'. */\nsds catClientInfoString(sds s, client *client) {\n    char flags[16], events[3], conninfo[CONN_INFO_LEN], *p;\n\n    p = flags;\n    if (client->flags & CLIENT_SLAVE) {\n        if (client->flags & CLIENT_MONITOR)\n            *p++ = 'O';\n        else\n            *p++ = 'S';\n    }\n    if (client->flags & CLIENT_MASTER) *p++ = 'M';\n    if (client->flags & CLIENT_PUBSUB) *p++ = 'P';\n    if (client->flags & CLIENT_MULTI) *p++ = 'x';\n    if (client->flags & CLIENT_BLOCKED) *p++ = 'b';\n    if (client->flags & CLIENT_TRACKING) *p++ = 't';\n    if (client->flags & CLIENT_TRACKING_BROKEN_REDIR) *p++ = 'R';\n    if (client->flags & CLIENT_TRACKING_BCAST) *p++ = 'B';\n    if (client->flags & CLIENT_DIRTY_CAS) *p++ = 'd';\n    if (client->flags & CLIENT_CLOSE_AFTER_REPLY) *p++ = 'c';\n    if (client->flags & CLIENT_UNBLOCKED) *p++ = 'u';\n    if (client->flags & CLIENT_CLOSE_ASAP) *p++ = 'A';\n    if (client->flags & CLIENT_UNIX_SOCKET) *p++ = 'U';\n    if (client->flags & CLIENT_READONLY) *p++ = 'r';\n    if (p == flags) *p++ = 'N';\n    *p++ = '\\0';\n\n    p = events;\n    if (client->conn) {\n        if (connHasReadHandler(client->conn)) *p++ = 'r';\n        if (connHasWriteHandler(client->conn)) *p++ = 'w';\n    }\n    *p = '\\0';\n\n    /* Compute the total memory consumed by this client. */\n    size_t obufmem = getClientOutputBufferMemoryUsage(client);\n    size_t total_mem = obufmem;\n    total_mem += zmalloc_size(client); /* includes client->buf */\n    total_mem += sdsZmallocSize(client->querybuf);\n    /* For efficiency (less work keeping track of the argv memory), it doesn't include the used memory\n     * i.e. unused sds space and internal fragmentation, just the string length. but this is enough to\n     * spot problematic clients. */\n    total_mem += client->argv_len_sum();\n    if (client->argv)\n        total_mem += zmalloc_size(client->argv);\n\n    return sdscatfmt(s,\n        \"id=%U addr=%s laddr=%s %s name=%s age=%I idle=%I flags=%s db=%i sub=%i psub=%i multi=%i qbuf=%U qbuf-free=%U argv-mem=%U obl=%U oll=%U omem=%U tot-mem=%U events=%s cmd=%s user=%s redir=%I\",\n        (unsigned long long) client->id,\n        getClientPeerId(client),\n        getClientSockname(client),\n        connGetInfo(client->conn, conninfo, sizeof(conninfo)),\n        client->name ? (char*)szFromObj(client->name) : \"\",\n        (long long)(g_pserver->unixtime - client->ctime),\n        (long long)(g_pserver->unixtime - client->lastinteraction),\n        flags,\n        client->db->id,\n        (int) dictSize(client->pubsub_channels),\n        (int) listLength(client->pubsub_patterns),\n        (client->flags & CLIENT_MULTI) ? client->mstate.count : -1,\n        (unsigned long long) sdslen(client->querybuf),\n        (unsigned long long) sdsavail(client->querybuf),\n        (unsigned long long) client->argv_len_sum(),\n        (unsigned long long) client->bufpos,\n        (unsigned long long) listLength(client->reply),\n        (unsigned long long) obufmem, /* should not include client->buf since we want to see 0 for static clients. */\n        (unsigned long long) total_mem,\n        events,\n        client->lastcmd ? client->lastcmd->name : \"NULL\",\n        client->user ? client->user->name : \"(superuser)\",\n        (client->flags & CLIENT_TRACKING) ? (long long) client->client_tracking_redirection : -1);\n}\n\nsds getAllClientsInfoString(int type) {\n    listNode *ln;\n    listIter li;\n    client *client;\n    sds o = sdsnewlen(SDS_NOINIT,200*listLength(g_pserver->clients));\n    sdsclear(o);\n    listRewind(g_pserver->clients,&li);\n    while ((ln = listNext(&li)) != NULL) {\n        client = reinterpret_cast<struct client*>(listNodeValue(ln));\n        std::unique_lock<decltype(client->lock)> lock(client->lock);\n        if (client->flags & CLIENT_CLOSE_ASAP) continue;\n        if (type != -1 && getClientType(client) != type) continue;\n        o = catClientInfoString(o,client);\n        o = sdscatlen(o,\"\\n\",1);\n    }\n    return o;\n}\n\n/* This function implements CLIENT SETNAME, including replying to the\n * user with an error if the charset is wrong (in that case C_ERR is\n * returned). If the function succeeeded C_OK is returned, and it's up\n * to the caller to send a reply if needed.\n *\n * Setting an empty string as name has the effect of unsetting the\n * currently set name: the client will remain unnamed.\n *\n * This function is also used to implement the HELLO SETNAME option. */\nint clientSetNameOrReply(client *c, robj *name) {\n    int len = sdslen((sds)ptrFromObj(name));\n    char *p = (char*)ptrFromObj(name);\n\n    /* Setting the client name to an empty string actually removes\n     * the current name. */\n    if (len == 0) {\n        if (c->name) decrRefCount(c->name);\n        c->name = NULL;\n        return C_OK;\n    }\n\n    /* Otherwise check if the charset is ok. We need to do this otherwise\n     * CLIENT LIST format will break. You should always be able to\n     * split by space to get the different fields. */\n    for (int j = 0; j < len; j++) {\n        if (p[j] < '!' || p[j] > '~') { /* ASCII is assumed. */\n            addReplyError(c,\n                \"Client names cannot contain spaces, \"\n                \"newlines or special characters.\");\n            return C_ERR;\n        }\n    }\n    if (c->name) decrRefCount(c->name);\n    c->name = name;\n    incrRefCount(name);\n    return C_OK;\n}\n\n/* Reset the client state to resemble a newly connected client.\n */\nvoid resetCommand(client *c) {\n    listNode *ln;\n\n    /* MONITOR clients are also marked with CLIENT_SLAVE, we need to\n     * distinguish between the two.\n     */\n    if (c->flags & CLIENT_MONITOR) {\n        ln = listSearchKey(g_pserver->monitors,c);\n        serverAssert(ln != NULL);\n        listDelNode(g_pserver->monitors,ln);\n\n        c->flags &= ~(CLIENT_MONITOR|CLIENT_SLAVE);\n    }\n\n    if (c->flags & (CLIENT_SLAVE|CLIENT_MASTER|CLIENT_MODULE)) {\n        addReplyError(c,\"can only reset normal client connections\");\n        return;\n    }\n\n    if (c->flags & CLIENT_TRACKING) disableTracking(c);\n    selectDb(c,0);\n    c->resp = 2;\n\n    clientSetDefaultAuth(c);\n    moduleNotifyUserChanged(c);\n    discardTransaction(c);\n\n    pubsubUnsubscribeAllChannels(c,0);\n    pubsubUnsubscribeAllPatterns(c,0);\n\n    if (c->name) {\n        decrRefCount(c->name);\n        c->name = NULL;\n    }\n\n    /* Selectively clear state flags not covered above */\n    c->flags &= ~(CLIENT_ASKING|CLIENT_READONLY|CLIENT_PUBSUB|\n            CLIENT_REPLY_OFF|CLIENT_REPLY_SKIP_NEXT);\n\n    addReplyStatus(c,\"RESET\");\n}\n\nvoid clientCommand(client *c) {\n    listNode *ln;\n    listIter li;\n\n    if (c->argc == 2 && !strcasecmp((const char*)ptrFromObj(c->argv[1]),\"help\")) {\n        const char *help[] = {\n\"CACHING (YES|NO)\",\n\"    Enable/disable tracking of the keys for next command in OPTIN/OPTOUT modes.\",\n\"GETREDIR\",\n\"    Return the client ID we are redirecting to when tracking is enabled.\",\n\"GETNAME\",\n\"    Return the name of the current connection.\",\n\"ID\",\n\"    Return the ID of the current connection.\",\n\"INFO\",\n\"    Return information about the current client connection.\",\n\"KILL <ip:port>\",\n\"    Kill connection made from <ip:port>.\",\n\"KILL <option> <value> [<option> <value> [...]]\",\n\"    Kill connections. Options are:\",\n\"    * ADDR (<ip:port>|<unixsocket>:0)\",\n\"      Kill connections made from the specified address\",\n\"    * LADDR (<ip:port>|<unixsocket>:0)\",\n\"      Kill connections made to specified local address\",\n\"    * TYPE (normal|master|replica|pubsub)\",\n\"      Kill connections by type.\",\n\"    * USER <username>\",\n\"      Kill connections authenticated by <username>.\",\n\"    * SKIPME (YES|NO)\",\n\"      Skip killing current connection (default: yes).\",\n\"LIST [options ...]\",\n\"    Return information about client connections. Options:\",\n\"    * TYPE (NORMAL|MASTER|REPLICA|PUBSUB)\",\n\"      Return clients of specified type.\",\n\"UNPAUSE\",\n\"    Stop the current client pause, resuming traffic.\",\n\"PAUSE <timeout> [WRITE|ALL]\",\n\"    Suspend all, or just write, clients for <timout> milliseconds.\",\n\"REPLY (ON|OFF|SKIP)\",\n\"    Control the replies sent to the current connection.\",\n\"SETNAME <name>\",\n\"    Assign the name <name> to the current connection.\",\n\"UNBLOCK <clientid> [TIMEOUT|ERROR]\",\n\"    Unblock the specified blocked client.\",\n\"TRACKING (ON|OFF) [REDIRECT <id>] [BCAST] [PREFIX <prefix> [...]]\",\n\"         [OPTIN] [OPTOUT]\",\n\"    Control server assisted client side caching.\",\n\"TRACKINGINFO\",\n\"    Report tracking status for the current connection.\",\nNULL\n        };\n        addReplyHelp(c, help);\n    } else if (!strcasecmp((const char*)ptrFromObj(c->argv[1]),\"id\") && c->argc == 2) {\n        /* CLIENT ID */\n        addReplyLongLong(c,c->id);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"info\") && c->argc == 2) {\n        /* CLIENT INFO */\n        sds o = catClientInfoString(sdsempty(), c);\n        o = sdscatlen(o,\"\\n\",1);\n        addReplyVerbatim(c,o,sdslen(o),\"txt\");\n        sdsfree(o);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"list\")) {\n        /* CLIENT LIST */\n        int type = -1;\n        sds o = NULL;\n        if (c->argc == 4 && !strcasecmp((const char*)ptrFromObj(c->argv[2]),\"type\")) {\n            type = getClientTypeByName((char*)ptrFromObj(c->argv[3]));\n            if (type == -1) {\n                addReplyErrorFormat(c,\"Unknown client type '%s'\",\n                    (char*) ptrFromObj(c->argv[3]));\n                return;\n            }\n        } else if (c->argc > 3 && !strcasecmp(szFromObj(c->argv[2]),\"id\")) {\n            int j;\n            o = sdsempty();\n            for (j = 3; j < c->argc; j++) {\n                long long cid;\n                if (getLongLongFromObjectOrReply(c, c->argv[j], &cid,\n                            \"Invalid client ID\")) {\n                    sdsfree(o);\n                    return;\n                }\n                client *cl = lookupClientByID(cid);\n                if (cl) {\n                    o = catClientInfoString(o, cl);\n                    o = sdscatlen(o, \"\\n\", 1);\n                }\n            }\n        } else if (c->argc != 2) {\n            addReplyErrorObject(c,shared.syntaxerr);\n            return;\n        }\n\n        if (!o)\n            o = getAllClientsInfoString(type);\n        addReplyVerbatim(c,o,sdslen(o),\"txt\");\n        sdsfree(o);\n    } else if (!strcasecmp((const char*)ptrFromObj(c->argv[1]),\"reply\") && c->argc == 3) {\n        /* CLIENT REPLY ON|OFF|SKIP */\n        if (!strcasecmp((const char*)ptrFromObj(c->argv[2]),\"on\")) {\n            c->flags &= ~(CLIENT_REPLY_SKIP|CLIENT_REPLY_OFF);\n            addReply(c,shared.ok);\n        } else if (!strcasecmp((const char*)ptrFromObj(c->argv[2]),\"off\")) {\n            c->flags |= CLIENT_REPLY_OFF;\n        } else if (!strcasecmp((const char*)ptrFromObj(c->argv[2]),\"skip\")) {\n            if (!(c->flags & CLIENT_REPLY_OFF))\n                c->flags |= CLIENT_REPLY_SKIP_NEXT;\n        } else {\n            addReplyErrorObject(c,shared.syntaxerr);\n            return;\n        }\n    } else if (!strcasecmp((const char*)ptrFromObj(c->argv[1]),\"kill\")) {\n        /* CLIENT KILL <ip:port>\n         * CLIENT KILL <option> [value] ... <option> [value] */\n        char *addr = NULL;\n        char *laddr = NULL;\n        user *user = NULL;\n        int type = -1;\n        uint64_t id = 0;\n        int skipme = 1;\n        int killed = 0, close_this_client = 0;\n\n        if (c->argc == 3) {\n            /* Old style syntax: CLIENT KILL <addr> */\n            addr = (char*)ptrFromObj(c->argv[2]);\n            skipme = 0; /* With the old form, you can kill yourself. */\n        } else if (c->argc > 3) {\n            int i = 2; /* Next option index. */\n\n            /* New style syntax: parse options. */\n            while(i < c->argc) {\n                int moreargs = c->argc > i+1;\n\n                if (!strcasecmp((const char*)ptrFromObj(c->argv[i]),\"id\") && moreargs) {\n                    long long tmp;\n\n                    if (getLongLongFromObjectOrReply(c,c->argv[i+1],&tmp,NULL)\n                        != C_OK) return;\n                    id = tmp;\n                } else if (!strcasecmp((const char*)ptrFromObj(c->argv[i]),\"type\") && moreargs) {\n                    type = getClientTypeByName((const char*)ptrFromObj(c->argv[i+1]));\n                    if (type == -1) {\n                        addReplyErrorFormat(c,\"Unknown client type '%s'\",\n                            (char*) ptrFromObj(c->argv[i+1]));\n                        return;\n                    }\n                } else if (!strcasecmp(szFromObj(c->argv[i]),\"addr\") && moreargs) {\n                    addr = szFromObj(c->argv[i+1]);\n                } else if (!strcasecmp(szFromObj(c->argv[i]),\"user\") && moreargs) {\n                    user = ACLGetUserByName(szFromObj(c->argv[i+1]),\n                                            sdslen(szFromObj(c->argv[i+1])));\n                    if (user == NULL) {\n                        addReplyErrorFormat(c,\"No such user '%s'\",\n                            szFromObj(c->argv[i+1]));\n                        return;\n                    }\n                } else if (!strcasecmp(szFromObj(c->argv[i]),\"addr\") && moreargs) {\n                    addr = szFromObj(c->argv[i+1]);\n                } else if (!strcasecmp(szFromObj(c->argv[i]),\"laddr\") && moreargs) {\n                    laddr = szFromObj(c->argv[i+1]);\n                } else if (!strcasecmp(szFromObj(c->argv[i]),\"user\") && moreargs) {\n                    user = ACLGetUserByName(szFromObj(c->argv[i+1]),\n                                            sdslen(szFromObj(c->argv[i+1])));\n                    if (user == NULL) {\n                        addReplyErrorFormat(c,\"No such user '%s'\",\n                            (char*) szFromObj(c->argv[i+1]));\n                        return;\n                    }\n                } else if (!strcasecmp(szFromObj(c->argv[i]),\"skipme\") && moreargs) {\n                    if (!strcasecmp(szFromObj(c->argv[i+1]),\"yes\")) {\n                        skipme = 1;\n                    } else if (!strcasecmp((const char*)ptrFromObj(c->argv[i+1]),\"no\")) {\n                        skipme = 0;\n                    } else {\n                        addReplyErrorObject(c,shared.syntaxerr);\n                        return;\n                    }\n                } else {\n                    addReplyErrorObject(c,shared.syntaxerr);\n                    return;\n                }\n                i += 2;\n            }\n        } else {\n            addReplyErrorObject(c,shared.syntaxerr);\n            return;\n        }\n\n        /* Iterate clients killing all the matching clients. */\n        listRewind(g_pserver->clients,&li);\n        while ((ln = listNext(&li)) != NULL) {\n            client *client = (struct client*)listNodeValue(ln);\n            if (addr && strcmp(getClientPeerId(client),addr) != 0) continue;\n            if (laddr && strcmp(getClientSockname(client),laddr) != 0) continue;\n            if (type != -1 && getClientType(client) != type) continue;\n            if (id != 0 && client->id != id) continue;\n            if (user && client->user != user) continue;\n            if (c == client && skipme) continue;\n\n            /* Kill it. */\n            if (c == client) {\n                close_this_client = 1;\n            } else {\n                if (FCorrectThread(client))\n                {\n                    freeClient(client);\n                }\n                else\n                {\n                    freeClientAsync(client);\n                }\n            }\n            killed++;\n        }\n\n        for (int iel = 0; iel < cserver.cthreads; ++iel) {\n            aePostFunction(g_pserver->rgthreadvar[iel].el, [iel] {  // note: failure is OK\n                freeClientsInAsyncFreeQueue(iel);\n            });\n        }\n\n        /* Reply according to old/new format. */\n        if (c->argc == 3) {\n            if (killed == 0)\n                addReplyError(c,\"No such client\");\n            else\n                addReply(c,shared.ok);\n        } else {\n            addReplyLongLong(c,killed);\n        }\n\n        /* If this client has to be closed, flag it as CLOSE_AFTER_REPLY\n         * only after we queued the reply to its output buffers. */\n        if (close_this_client) c->flags |= CLIENT_CLOSE_AFTER_REPLY;\n    } else if (!strcasecmp((const char*)ptrFromObj(c->argv[1]),\"unblock\") && (c->argc == 3 ||\n                                                          c->argc == 4))\n    {\n        /* CLIENT UNBLOCK <id> [timeout|error] */\n        long long id;\n        int unblock_error = 0;\n\n        if (c->argc == 4) {\n            if (!strcasecmp((const char*)ptrFromObj(c->argv[3]),\"timeout\")) {\n                unblock_error = 0;\n            } else if (!strcasecmp((const char*)ptrFromObj(c->argv[3]),\"error\")) {\n                unblock_error = 1;\n            } else {\n                addReplyError(c,\n                    \"CLIENT UNBLOCK reason should be TIMEOUT or ERROR\");\n                return;\n            }\n        }\n        if (getLongLongFromObjectOrReply(c,c->argv[2],&id,NULL)\n            != C_OK) return;\n        struct client *target = lookupClientByID(id);\n        if (target && target->flags & CLIENT_BLOCKED && moduleBlockedClientMayTimeout(target)) {\n            std::unique_lock<fastlock> ul(target->lock);\n            if (unblock_error)\n                addReplyError(target,\n                    \"-UNBLOCKED client unblocked via CLIENT UNBLOCK\");\n            else\n                replyToBlockedClientTimedOut(target);\n            unblockClient(target);\n            addReply(c,shared.cone);\n        } else {\n            addReply(c,shared.czero);\n        }\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"setname\") && c->argc == 3) {\n        /* CLIENT SETNAME */\n        if (clientSetNameOrReply(c,c->argv[2]) == C_OK)\n            addReply(c,shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"getname\") && c->argc == 2) {\n        /* CLIENT GETNAME */\n        if (c->name)\n            addReplyBulk(c,c->name);\n        else\n            addReplyNull(c);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"unpause\") && c->argc == 2) {\n        /* CLIENT UNPAUSE */\n        unpauseClients();\n        addReply(c,shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"pause\") && (c->argc == 3 ||\n                                                        c->argc == 4))\n    {\n        /* CLIENT PAUSE TIMEOUT [WRITE|ALL] */\n        mstime_t end;\n        pause_type type = CLIENT_PAUSE_ALL;\n        if (c->argc == 4) {\n            if (!strcasecmp(szFromObj(c->argv[3]),\"write\")) {\n                type = CLIENT_PAUSE_WRITE;\n            } else if (!strcasecmp(szFromObj(c->argv[3]),\"all\")) {\n                type = CLIENT_PAUSE_ALL;\n            } else {\n                addReplyError(c,\n                    \"CLIENT PAUSE mode must be WRITE or ALL\");  \n                return;       \n            }\n        }\n\n        if (getTimeoutFromObjectOrReply(c,c->argv[2],&end,\n            UNIT_MILLISECONDS) != C_OK) return;\n        pauseClients(end, type);\n        addReply(c,shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"tracking\") && c->argc >= 3) {\n        /* CLIENT TRACKING (on|off) [REDIRECT <id>] [BCAST] [PREFIX first]\n         *                          [PREFIX second] [OPTIN] [OPTOUT] ... */\n        long long redir = 0;\n        uint64_t options = 0;\n        robj **prefix = NULL;\n        size_t numprefix = 0;\n\n        /* Parse the options. */\n        for (int j = 3; j < c->argc; j++) {\n            int moreargs = (c->argc-1) - j;\n\n            if (!strcasecmp(szFromObj(c->argv[j]),\"redirect\") && moreargs) {\n                j++;\n                if (redir != 0) {\n                    addReplyError(c,\"A client can only redirect to a single \"\n                                    \"other client\");\n                    zfree(prefix);\n                    return;\n                }\n\n                if (getLongLongFromObjectOrReply(c,c->argv[j],&redir,NULL) !=\n                    C_OK)\n                {\n                    zfree(prefix);\n                    return;\n                }\n                /* We will require the client with the specified ID to exist\n                 * right now, even if it is possible that it gets disconnected\n                 * later. Still a valid sanity check. */\n                if (lookupClientByID(redir) == NULL) {\n                    addReplyError(c,\"The client ID you want redirect to \"\n                                    \"does not exist\");\n                    zfree(prefix);\n                    return;\n                }\n            } else if (!strcasecmp(szFromObj(c->argv[j]),\"bcast\")) {\n                options |= CLIENT_TRACKING_BCAST;\n            } else if (!strcasecmp(szFromObj(c->argv[j]),\"optin\")) {\n                options |= CLIENT_TRACKING_OPTIN;\n            } else if (!strcasecmp(szFromObj(c->argv[j]),\"optout\")) {\n                options |= CLIENT_TRACKING_OPTOUT;\n            } else if (!strcasecmp(szFromObj(c->argv[j]),\"noloop\")) {\n                options |= CLIENT_TRACKING_NOLOOP;\n            } else if (!strcasecmp(szFromObj(c->argv[j]),\"prefix\") && moreargs) {\n                j++;\n                prefix = (robj**)zrealloc(prefix,sizeof(robj*)*(numprefix+1), MALLOC_LOCAL);\n                prefix[numprefix++] = c->argv[j];\n            } else {\n                zfree(prefix);\n                addReplyErrorObject(c,shared.syntaxerr);\n                return;\n            }\n        }\n\n        /* Options are ok: enable or disable the tracking for this client. */\n        if (!strcasecmp(szFromObj(c->argv[2]),\"on\")) {\n            /* Before enabling tracking, make sure options are compatible\n             * among each other and with the current state of the client. */\n            if (!(options & CLIENT_TRACKING_BCAST) && numprefix) {\n                addReplyError(c,\n                    \"PREFIX option requires BCAST mode to be enabled\");\n                zfree(prefix);\n                return;\n            }\n\n            if (c->flags & CLIENT_TRACKING) {\n                int oldbcast = !!(c->flags & CLIENT_TRACKING_BCAST);\n                int newbcast = !!(options & CLIENT_TRACKING_BCAST);\n                if (oldbcast != newbcast) {\n                    addReplyError(c,\n                    \"You can't switch BCAST mode on/off before disabling \"\n                    \"tracking for this client, and then re-enabling it with \"\n                    \"a different mode.\");\n                    zfree(prefix);\n                    return;\n                }\n            }\n\n            if (options & CLIENT_TRACKING_BCAST &&\n                options & (CLIENT_TRACKING_OPTIN|CLIENT_TRACKING_OPTOUT))\n            {\n                addReplyError(c,\n                \"OPTIN and OPTOUT are not compatible with BCAST\");\n                zfree(prefix);\n                return;\n            }\n\n            if (options & CLIENT_TRACKING_OPTIN && options & CLIENT_TRACKING_OPTOUT)\n            {\n                addReplyError(c,\n                \"You can't specify both OPTIN mode and OPTOUT mode\");\n                zfree(prefix);\n                return;\n            }\n\n            if ((options & CLIENT_TRACKING_OPTIN && c->flags & CLIENT_TRACKING_OPTOUT) ||\n                (options & CLIENT_TRACKING_OPTOUT && c->flags & CLIENT_TRACKING_OPTIN))\n            {\n                addReplyError(c,\n                \"You can't switch OPTIN/OPTOUT mode before disabling \"\n                \"tracking for this client, and then re-enabling it with \"\n                \"a different mode.\");\n                zfree(prefix);\n                return;\n            }\n\n            if (options & CLIENT_TRACKING_BCAST) {\n                if (!checkPrefixCollisionsOrReply(c,prefix,numprefix)) {\n                    zfree(prefix);\n                    return;\n                }\n            }\n\n            enableTracking(c,redir,options,prefix,numprefix);\n        } else if (!strcasecmp(szFromObj(c->argv[2]),\"off\")) {\n            disableTracking(c);\n        } else {\n            zfree(prefix);\n            addReplyErrorObject(c,shared.syntaxerr);\n            return;\n        }\n        zfree(prefix);\n        addReply(c,shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"caching\") && c->argc >= 3) {\n        if (!(c->flags & CLIENT_TRACKING)) {\n            addReplyError(c,\"CLIENT CACHING can be called only when the \"\n                            \"client is in tracking mode with OPTIN or \"\n                            \"OPTOUT mode enabled\");\n            return;\n        }\n\n        char *opt = szFromObj(c->argv[2]);\n        if (!strcasecmp(opt,\"yes\")) {\n            if (c->flags & CLIENT_TRACKING_OPTIN) {\n                c->flags |= CLIENT_TRACKING_CACHING;\n            } else {\n                addReplyError(c,\"CLIENT CACHING YES is only valid when tracking is enabled in OPTIN mode.\");\n                return;\n            }\n        } else if (!strcasecmp(opt,\"no\")) {\n            if (c->flags & CLIENT_TRACKING_OPTOUT) {\n                c->flags |= CLIENT_TRACKING_CACHING;\n            } else {\n                addReplyError(c,\"CLIENT CACHING NO is only valid when tracking is enabled in OPTOUT mode.\");\n                return;\n            }\n        } else {\n            addReplyErrorObject(c,shared.syntaxerr);\n            return;\n        }\n\n        /* Common reply for when we succeeded. */\n        addReply(c,shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"caching\") && c->argc >= 3) {\n        if (!(c->flags & CLIENT_TRACKING)) {\n            addReplyError(c,\"CLIENT CACHING can be called only when the \"\n                            \"client is in tracking mode with OPTIN or \"\n                            \"OPTOUT mode enabled\");\n            return;\n        }\n\n        char *opt = szFromObj(c->argv[2]);\n        if (!strcasecmp(opt,\"yes\")) {\n            if (c->flags & CLIENT_TRACKING_OPTIN) {\n                c->flags |= CLIENT_TRACKING_CACHING;\n            } else {\n                addReplyError(c,\"CLIENT CACHING YES is only valid when tracking is enabled in OPTIN mode.\");\n                return;\n            }\n        } else if (!strcasecmp(opt,\"no\")) {\n            if (c->flags & CLIENT_TRACKING_OPTOUT) {\n                c->flags |= CLIENT_TRACKING_CACHING;\n            } else {\n                addReplyError(c,\"CLIENT CACHING NO is only valid when tracking is enabled in OPTOUT mode.\");\n                return;\n            }\n        } else {\n            addReply(c,shared.syntaxerr);\n            return;\n        }\n\n        /* Common reply for when we succeeded. */\n        addReply(c,shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"getredir\") && c->argc == 2) {\n        /* CLIENT GETREDIR */\n        if (c->flags & CLIENT_TRACKING) {\n            addReplyLongLong(c,c->client_tracking_redirection);\n        } else {\n            addReplyLongLong(c,-1);\n        }\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"trackinginfo\") && c->argc == 2) {\n        addReplyMapLen(c,3);\n\n        /* Flags */\n        addReplyBulkCString(c,\"flags\");\n        void *arraylen_ptr = addReplyDeferredLen(c);\n        int numflags = 0;\n        addReplyBulkCString(c,c->flags & CLIENT_TRACKING ? \"on\" : \"off\");\n        numflags++;\n        if (c->flags & CLIENT_TRACKING_BCAST) {\n            addReplyBulkCString(c,\"bcast\");\n            numflags++;\n        }\n        if (c->flags & CLIENT_TRACKING_OPTIN) {\n            addReplyBulkCString(c,\"optin\");\n            numflags++;\n            if (c->flags & CLIENT_TRACKING_CACHING) {\n                addReplyBulkCString(c,\"caching-yes\");\n                numflags++;        \n            }\n        }\n        if (c->flags & CLIENT_TRACKING_OPTOUT) {\n            addReplyBulkCString(c,\"optout\");\n            numflags++;\n            if (c->flags & CLIENT_TRACKING_CACHING) {\n                addReplyBulkCString(c,\"caching-no\");\n                numflags++;        \n            }\n        }\n        if (c->flags & CLIENT_TRACKING_NOLOOP) {\n            addReplyBulkCString(c,\"noloop\");\n            numflags++;\n        }\n        if (c->flags & CLIENT_TRACKING_BROKEN_REDIR) {\n            addReplyBulkCString(c,\"broken_redirect\");\n            numflags++;\n        }\n        setDeferredSetLen(c,arraylen_ptr,numflags);\n\n        /* Redirect */\n        addReplyBulkCString(c,\"redirect\");\n        if (c->flags & CLIENT_TRACKING) {\n            addReplyLongLong(c,c->client_tracking_redirection);\n        } else {\n            addReplyLongLong(c,-1);\n        }\n\n        /* Prefixes */\n        addReplyBulkCString(c,\"prefixes\");\n        if (c->client_tracking_prefixes) {\n            addReplyArrayLen(c,raxSize(c->client_tracking_prefixes));\n            raxIterator ri;\n            raxStart(&ri,c->client_tracking_prefixes);\n            raxSeek(&ri,\"^\",NULL,0);\n            while(raxNext(&ri)) {\n                addReplyBulkCBuffer(c,ri.key,ri.key_len);\n            }\n            raxStop(&ri);\n        } else {\n            addReplyArrayLen(c,0);\n        }\n    } else {\n        addReplySubcommandSyntaxError(c);\n    }\n}\n\n/* HELLO [<protocol-version> [AUTH <user> <password>] [SETNAME <name>] ] */\nvoid helloCommand(client *c) {\n    long long ver = 0;\n    int next_arg = 1;\n\n    if (c->argc >= 2) {\n        if (getLongLongFromObjectOrReply(c, c->argv[next_arg++], &ver,\n            \"Protocol version is not an integer or out of range\") != C_OK) {\n            return;\n        }\n\n        if (ver < 2 || ver > 3) {\n            addReplyError(c,\"-NOPROTO unsupported protocol version\");\n            return;\n        }\n    }\n\n    for (int j = next_arg; j < c->argc; j++) {\n        int moreargs = (c->argc-1) - j;\n        const char *opt = (const char*)ptrFromObj(c->argv[j]);\n        if (!strcasecmp(opt,\"AUTH\") && moreargs >= 2) {\n            redactClientCommandArgument(c, j+1);\n            redactClientCommandArgument(c, j+2);\n            if (ACLAuthenticateUser(c, c->argv[j+1], c->argv[j+2]) == C_ERR) {\n                addReplyError(c,\"-WRONGPASS invalid username-password pair or user is disabled.\");\n                return;\n            }\n            j += 2;\n        } else if (!strcasecmp(opt,\"SETNAME\") && moreargs) {\n            if (clientSetNameOrReply(c, c->argv[j+1]) == C_ERR) return;\n            j++;\n        } else {\n            addReplyErrorFormat(c,\"Syntax error in HELLO option '%s'\",opt);\n            return;\n        }\n    }\n\n    /* At this point we need to be authenticated to continue. */\n    if (!c->authenticated) {\n        addReplyError(c,\"-NOAUTH HELLO must be called with the client already \"\n                        \"authenticated, otherwise the HELLO AUTH <user> <pass> \"\n                        \"option can be used to authenticate the client and \"\n                        \"select the RESP protocol version at the same time\");\n        return;\n    }\n\n    /* Let's switch to the specified RESP mode. */\n    if (ver) c->resp = ver;\n    addReplyMapLen(c,6 + !g_pserver->sentinel_mode);\n\n    addReplyBulkCString(c,\"server\");\n    addReplyBulkCString(c,\"redis\");\n\n    addReplyBulkCString(c,\"version\");\n    addReplyBulkCString(c,KEYDB_SET_VERSION);\n\n    addReplyBulkCString(c,\"proto\");\n    addReplyLongLong(c,c->resp);\n\n    addReplyBulkCString(c,\"id\");\n    addReplyLongLong(c,c->id);\n\n    addReplyBulkCString(c,\"mode\");\n    if (g_pserver->sentinel_mode) addReplyBulkCString(c,\"sentinel\");\n    else if (g_pserver->cluster_enabled) addReplyBulkCString(c,\"cluster\");\n    else addReplyBulkCString(c,\"standalone\");\n\n    if (!g_pserver->sentinel_mode) {\n        addReplyBulkCString(c,\"role\");\n        addReplyBulkCString(c,listLength(g_pserver->masters) ? \n            g_pserver->fActiveReplica ? \"active-replica\" : \"replica\" \n            : \"master\");\n    }\n\n    addReplyBulkCString(c,\"modules\");\n    addReplyLoadedModules(c);\n}\n\n/* This callback is bound to POST and \"Host:\" command names. Those are not\n * really commands, but are used in security attacks in order to talk to\n * Redis instances via HTTP, with a technique called \"cross protocol scripting\"\n * which exploits the fact that services like Redis will discard invalid\n * HTTP headers and will process what follows.\n *\n * As a protection against this attack, Redis will terminate the connection\n * when a POST or \"Host:\" header is seen, and will log the event from\n * time to time (to avoid creating a DOS as a result of too many logs). */\nvoid securityWarningCommand(client *c) {\n    static time_t logged_time;\n    time_t now = time(NULL);\n\n    if (llabs(now-logged_time) > 60) {\n        serverLog(LL_WARNING,\"Possible SECURITY ATTACK detected. It looks like somebody is sending POST or Host: commands to KeyDB. This is likely due to an attacker attempting to use Cross Protocol Scripting to compromise your KeyDB instance. Connection aborted.\");\n        logged_time = now;\n    }\n    freeClientAsync(c);\n}\n\n/* Keep track of the original command arguments so that we can generate\n * an accurate slowlog entry after the command has been executed. */\nstatic void retainOriginalCommandVector(client *c) {\n    /* We already rewrote this command, so don't rewrite it again */\n    if (c->original_argv) return;\n    c->original_argc = c->argc;\n    c->original_argv = (robj**)zmalloc(sizeof(robj*)*(c->argc));\n    for (int j = 0; j < c->argc; j++) {\n        c->original_argv[j] = c->argv[j];\n        incrRefCount(c->argv[j]);\n    }\n}\n\n/* Redact a given argument to prevent it from being shown\n * in the slowlog. This information is stored in the\n * original_argv array. */\nvoid redactClientCommandArgument(client *c, int argc) {\n    retainOriginalCommandVector(c);\n    decrRefCount(c->argv[argc]);\n    c->original_argv[argc] = shared.redacted;\n}\n\n/* Rewrite the command vector of the client. All the new objects ref count\n * is incremented. The old command vector is freed, and the old objects\n * ref count is decremented. */\nvoid rewriteClientCommandVector(client *c, int argc, ...) {\n    va_list ap;\n    int j;\n    robj **argv; /* The new argument vector */\n\n    argv = (robj**)zmalloc(sizeof(robj*)*argc, MALLOC_LOCAL);\n    va_start(ap,argc);\n    for (j = 0; j < argc; j++) {\n        robj *a;\n\n        a = va_arg(ap, robj*);\n        argv[j] = a;\n        incrRefCount(a);\n    }\n    replaceClientCommandVector(c, argc, argv);\n    va_end(ap);\n}\n\n/* Completely replace the client command vector with the provided one. */\nvoid replaceClientCommandVector(client *c, int argc, robj **argv) {\n    int j;\n    retainOriginalCommandVector(c);\n    freeClientArgv(c);\n    zfree(c->argv);\n    c->argv = argv;\n    c->argc = argc;\n    c->argv_len_sumActive = 0;\n    for (j = 0; j < c->argc; j++)\n        if (c->argv[j])\n            c->argv_len_sumActive += getStringObjectLen(c->argv[j]);\n    c->cmd = lookupCommandOrOriginal((sds)ptrFromObj(c->argv[0]));\n    serverAssertWithInfo(c,NULL,c->cmd != NULL);\n}\n\n/* Rewrite a single item in the command vector.\n * The new val ref count is incremented, and the old decremented.\n *\n * It is possible to specify an argument over the current size of the\n * argument vector: in this case the array of objects gets reallocated\n * and c->argc set to the max value. However it's up to the caller to\n *\n * 1. Make sure there are no \"holes\" and all the arguments are set.\n * 2. If the original argument vector was longer than the one we\n *    want to end with, it's up to the caller to set c->argc and\n *    free the no longer used objects on c->argv. */\nvoid rewriteClientCommandArgument(client *c, int i, robj *newval) {\n    robj *oldval;\n    retainOriginalCommandVector(c);\n    if (i >= c->argc) {\n        c->argv = (robj**)zrealloc(c->argv,sizeof(robj*)*(i+1), MALLOC_LOCAL);\n        c->argc = i+1;\n        c->argv[i] = NULL;\n    }\n    oldval = c->argv[i];\n    if (oldval) c->argv_len_sumActive -= getStringObjectLen(oldval);\n    if (newval) c->argv_len_sumActive += getStringObjectLen(newval);\n    c->argv[i] = newval;\n    incrRefCount(newval);\n    if (oldval) decrRefCount(oldval);\n\n    /* If this is the command name make sure to fix c->cmd. */\n    if (i == 0) {\n        c->cmd = lookupCommandOrOriginal((sds)ptrFromObj(c->argv[0]));\n        serverAssertWithInfo(c,NULL,c->cmd != NULL);\n    }\n}\n\n/* In the case of a replica client, writes to said replica are using data from the replication backlog\n * as opposed to it's own internal buffer, this number should keep track of that */\nunsigned long getClientReplicationBacklogSharedUsage(client *c) {\n    return (!(c->flags & CLIENT_SLAVE) || !c->FPendingReplicaWrite() ) ? 0 : g_pserver->master_repl_offset - c->repl_curr_off;\n}\n\n/* This function returns the number of bytes that Redis is\n * using to store the reply still not read by the client.\n *\n * Note: this function is very fast so can be called as many time as\n * the caller wishes. The main usage of this function currently is\n * enforcing the client output length limits. */\nunsigned long getClientOutputBufferMemoryUsage(client *c) {\n    unsigned long list_item_size = sizeof(listNode) + sizeof(clientReplyBlock);\n    return c->reply_bytes + (list_item_size*listLength(c->reply)) + (c->replyAsync ? c->replyAsync->size : 0) + getClientReplicationBacklogSharedUsage(c);\n}\n\n\n\n/* Get the class of a client, used in order to enforce limits to different\n * classes of clients.\n *\n * The function will return one of the following:\n * CLIENT_TYPE_NORMAL -> Normal client\n * CLIENT_TYPE_SLAVE  -> Slave\n * CLIENT_TYPE_PUBSUB -> Client subscribed to Pub/Sub channels\n * CLIENT_TYPE_MASTER -> The client representing our replication master.\n */\nint getClientType(client *c) {\n    if (c->flags & CLIENT_MASTER) return CLIENT_TYPE_MASTER;\n    /* Even though MONITOR clients are marked as replicas, we\n     * want the expose them as normal clients. */\n    if ((c->flags & CLIENT_SLAVE) && !(c->flags & CLIENT_MONITOR))\n        return CLIENT_TYPE_SLAVE;\n    if (c->flags & CLIENT_PUBSUB) return CLIENT_TYPE_PUBSUB;\n    return CLIENT_TYPE_NORMAL;\n}\n\nint getClientTypeByName(const char *name) {\n    if (!strcasecmp(name,\"normal\")) return CLIENT_TYPE_NORMAL;\n    else if (!strcasecmp(name,\"slave\")) return CLIENT_TYPE_SLAVE;\n    else if (!strcasecmp(name,\"replica\")) return CLIENT_TYPE_SLAVE;\n    else if (!strcasecmp(name,\"pubsub\")) return CLIENT_TYPE_PUBSUB;\n    else if (!strcasecmp(name,\"master\")) return CLIENT_TYPE_MASTER;\n    else return -1;\n}\n\nconst char *getClientTypeName(int clientType) {\n    switch(clientType) {\n    case CLIENT_TYPE_NORMAL: return \"normal\";\n    case CLIENT_TYPE_SLAVE:  return \"slave\";\n    case CLIENT_TYPE_PUBSUB: return \"pubsub\";\n    case CLIENT_TYPE_MASTER: return \"master\";\n    default:                       return NULL;\n    }\n}\n\n/* The function checks if the client reached output buffer soft or hard\n * limit, and also update the state needed to check the soft limit as\n * a side effect.\n *\n * Return value: non-zero if the client reached the soft or the hard limit.\n *               Otherwise zero is returned. */\nint checkClientOutputBufferLimits(client *c) {\n    int soft = 0, hard = 0;\n    unsigned long used_mem = getClientOutputBufferMemoryUsage(c);\n\n    int clientType = getClientType(c);\n    /* For the purpose of output buffer limiting, masters are handled\n     * like normal clients. */\n    if (clientType == CLIENT_TYPE_MASTER) clientType = CLIENT_TYPE_NORMAL;\n\n    if (cserver.client_obuf_limits[clientType].hard_limit_bytes &&\n        used_mem >= cserver.client_obuf_limits[clientType].hard_limit_bytes)\n        hard = 1;\n    if (cserver.client_obuf_limits[clientType].soft_limit_bytes &&\n        used_mem >= cserver.client_obuf_limits[clientType].soft_limit_bytes)\n        soft = 1;\n\n    /* We need to check if the soft limit is reached continuously for the\n     * specified amount of seconds. */\n    if (soft) {\n        if (c->obuf_soft_limit_reached_time == 0) {\n            c->obuf_soft_limit_reached_time = g_pserver->unixtime;\n            soft = 0; /* First time we see the soft limit reached */\n        } else {\n            time_t elapsed = g_pserver->unixtime - c->obuf_soft_limit_reached_time;\n\n            if (elapsed <=\n                cserver.client_obuf_limits[clientType].soft_limit_seconds) {\n                soft = 0; /* The client still did not reached the max number of\n                             seconds for the soft limit to be considered\n                             reached. */\n            }\n        }\n    } else {\n        c->obuf_soft_limit_reached_time = 0;\n    }\n    return soft || hard;\n}\n\n/* Asynchronously close a client if soft or hard limit is reached on the\n * output buffer size. The caller can check if the client will be closed\n * checking if the client CLIENT_CLOSE_ASAP flag is set.\n *\n * Note: we need to close the client asynchronously because this function is\n * called from contexts where the client can't be freed safely, i.e. from the\n * lower level functions pushing data inside the client output buffers.\n * When `async` is set to 0, we close the client immediately, this is\n * useful when called from cron.\n *\n * Returns 1 if client was (flagged) closed. */\nint closeClientOnOutputBufferLimitReached(client *c, int async) {\n    if (!c->conn) return 0; /* It is unsafe to free fake clients. */\n    serverAssert(c->reply_bytes < SIZE_MAX-(1024*64));\n    if (c->reply_bytes == 0 || c->flags & CLIENT_CLOSE_ASAP) return 0;\n    if (checkClientOutputBufferLimits(c) && c->replstate != SLAVE_STATE_FASTSYNC_TX) {\n        sds client = catClientInfoString(sdsempty(),c);\n\n        if (async) {\n            freeClientAsync(c);\n            serverLog(LL_WARNING,\n                      \"Client %s scheduled to be closed ASAP for overcoming of output buffer limits.\",\n                      client);\n        } else {\n            freeClient(c);\n            serverLog(LL_WARNING,\n                      \"Client %s closed for overcoming of output buffer limits.\",\n                      client);\n        }\n        sdsfree(client);\n        return  1;\n    }\n    return 0;\n}\n\n/* Helper function used by performEvictions() in order to flush slaves\n * output buffers without returning control to the event loop.\n * This is also called by SHUTDOWN for a best-effort attempt to send\n * slaves the latest writes. */\nvoid flushSlavesOutputBuffers(void) {\n    serverAssert(GlobalLocksAcquired());\n    listIter li;\n    listNode *ln;\n\n    flushReplBacklogToClients();\n\n    listRewind(g_pserver->slaves,&li);\n    while((ln = listNext(&li))) {\n        client *replica = (client*)listNodeValue(ln);\n\n        if (!FCorrectThread(replica))\n            continue;   // we cannot synchronously flush other thread's clients\n\n        int can_receive_writes = connHasWriteHandler(replica->conn) ||\n                                 (replica->flags & CLIENT_PENDING_WRITE);\n\n        /* We don't want to send the pending data to the replica in a few\n         * cases:\n         *\n         * 1. For some reason there is neither the write handler installed\n         *    nor the client is flagged as to have pending writes: for some\n         *    reason this replica may not be set to receive data. This is\n         *    just for the sake of defensive programming.\n         *\n         * 2. The put_online_on_ack flag is true. To know why we don't want\n         *    to send data to the replica in this case, please grep for the\n         *    flag for this flag.\n         *\n         * 3. Obviously if the slave is not ONLINE.\n         */\n        if (replica->replstate == SLAVE_STATE_ONLINE &&\n            can_receive_writes &&\n            !replica->repl_put_online_on_ack &&\n            clientHasPendingReplies(replica))\n        {\n            writeToClient(replica,0);\n        }\n    }\n}\n\n/* Pause clients up to the specified unixtime (in ms) for a given type of\n * commands.\n *\n * A main use case of this function is to allow pausing replication traffic\n * so that a failover without data loss to occur. Replicas will continue to receive\n * traffic to faciliate this functionality.\n * \n * This function is also internally used by Redis Cluster for the manual\n * failover procedure implemented by CLUSTER FAILOVER.\n *\n * The function always succeed, even if there is already a pause in progress.\n * In such a case, the duration is set to the maximum and new end time and the\n * type is set to the more restrictive type of pause. */\nvoid pauseClients(mstime_t end, pause_type type) {\n    if (type > g_pserver->client_pause_type) {\n        g_pserver->client_pause_type = type;\n    }\n\n    if (end > g_pserver->client_pause_end_time) {\n        g_pserver->client_pause_end_time = end;\n    }\n\n    /* We allow write commands that were queued\n     * up before and after to execute. We need\n     * to track this state so that we don't assert\n     * in propagate(). */\n    if (serverTL->in_exec) {\n        serverTL->client_pause_in_transaction = 1;\n    }\n}\n\n/* Unpause clients and queue them for reprocessing. */\nvoid unpauseClients(void) {\n    serverAssert(GlobalLocksAcquired());\n    listNode *ln;\n    listIter li;\n    client *c;\n    \n    g_pserver->client_pause_type = CLIENT_PAUSE_OFF;\n    g_pserver->client_pause_end_time = 0;\n\n    /* Unblock all of the clients so they are reprocessed. */\n    listRewind(g_pserver->paused_clients,&li);\n    while ((ln = listNext(&li)) != NULL) {\n        c = (client*)listNodeValue(ln);\n        std::unique_lock<fastlock> ul(c->lock);\n        unblockClient(c);\n    }\n}\n\n/* Returns true if clients are paused and false otherwise. */ \nint areClientsPaused(void) {\n    return g_pserver->client_pause_type != CLIENT_PAUSE_OFF;\n}\n\n/* Checks if the current client pause has elapsed and unpause clients\n * if it has. Also returns true if clients are now paused and false \n * otherwise. */\nint checkClientPauseTimeoutAndReturnIfPaused(void) {\n    if (!areClientsPaused())\n        return 0;\n    if (g_pserver->client_pause_end_time < g_pserver->mstime) {\n        unpauseClients();\n    }\n    return areClientsPaused();\n}\n\n/* This function is called by Redis in order to process a few events from\n * time to time while blocked into some not interruptible operation.\n * This allows to reply to clients with the -LOADING error while loading the\n * data set at startup or after a full resynchronization with the master\n * and so forth.\n *\n * It calls the event loop in order to process a few events. Specifically we\n * try to call the event loop 4 times as long as we receive acknowledge that\n * some event was processed, in order to go forward with the accept, read,\n * write, close sequence needed to serve a client.\n *\n * The function returns the total number of events processed. */\nvoid processEventsWhileBlocked(int iel) {\n\n    int eventsCount = 0;\n    executeWithoutGlobalLock([&](){\n        int iterations = 4; /* See the function top-comment. */\n        try\n        {\n            ProcessingEventsWhileBlocked = 1;\n            while (iterations--) {\n                long long startval = g_pserver->events_processed_while_blocked;\n                long long ae_events = aeProcessEvents(g_pserver->rgthreadvar[iel].el,\n                    AE_FILE_EVENTS|AE_DONT_WAIT|\n                    AE_CALL_BEFORE_SLEEP|AE_CALL_AFTER_SLEEP);\n                /* Note that g_pserver->events_processed_while_blocked will also get\n                * incremeted by callbacks called by the event loop handlers. */\n                eventsCount += ae_events;\n                long long events = eventsCount - startval;\n                if (!events) break;\n            }\n            ProcessingEventsWhileBlocked = 0;\n        }\n        catch (...)\n        {\n            ProcessingEventsWhileBlocked = 0;\n            throw;\n        }\n    });\n\n    // Try to complete any async rehashes (this would normally happen in dbCron, but that won't run here)\n    for (int idb = 0; idb < cserver.dbnum; ++idb) {\n        redisDb *db = g_pserver->db[idb];\n        while (db->dictUnsafeKeyOnly()->asyncdata != nullptr) {\n            if (!db->dictUnsafeKeyOnly()->asyncdata->done)\n                break;\n            dictCompleteRehashAsync(db->dictUnsafeKeyOnly()->asyncdata, false /*fFree*/);\n        }\n    }\n    g_pserver->events_processed_while_blocked += eventsCount;\n\n    whileBlockedCron();\n\n    // If a different thread processed the shutdown we need to abort the lua command or we will hang\n    if (serverTL->el->stop)\n        throw ShutdownException();\n}\n\n"
  },
  {
    "path": "src/new.cpp",
    "content": "#include <cstddef>  // std::size_t\n#include \"server.h\"\n#include \"new.h\"\n#include <new>\n\n#ifdef SANITIZE\nvoid *operator new(size_t size, enum MALLOC_CLASS mclass)\n{\n    (void)mclass;\n    return ::operator new(size);\n}\n\n#else\n[[deprecated]]\nvoid *operator new(size_t size)\n{\n    return zmalloc(size, MALLOC_LOCAL);\n}\n\nvoid *operator new(size_t size, enum MALLOC_CLASS mclass) \n{ \n    return zmalloc(size, mclass);\n}\n\nvoid *operator new(std::size_t size, const std::nothrow_t &) noexcept\n{\n    return zmalloc(size, MALLOC_LOCAL);\n}\n\n//need to do null checks for delete since the compiler can optimize out null checks in zfree\nvoid operator delete(void * p) noexcept\n{\n    if (p != nullptr)\n        zfree(p);\n}\n\nvoid operator delete(void *p, std::size_t) noexcept\n{\n    if (p != nullptr)\n        zfree(p);\n}\n\n#endif\n"
  },
  {
    "path": "src/new.h",
    "content": "#pragma once\n#include <cstddef>  // std::size_t\n#include \"storage.h\"\n\nvoid *operator new(size_t size, enum MALLOC_CLASS mclass);\n\n#ifndef SANITIZE\nvoid *operator new(size_t size);\n\nvoid operator delete(void * p) noexcept;\nvoid operator delete(void *p, std::size_t) noexcept;\n#endif"
  },
  {
    "path": "src/notify.cpp",
    "content": "/*\n * Copyright (c) 2013, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n\n/* This file implements keyspace events notification via Pub/Sub and\n * described at https://redis.io/topics/notifications. */\n\n/* Turn a string representing notification classes into an integer\n * representing notification classes flags xored.\n *\n * The function returns -1 if the input contains characters not mapping to\n * any class. */\nint keyspaceEventsStringToFlags(char *classes) {\n    char *p = classes;\n    int c, flags = 0;\n\n    while((c = *p++) != '\\0') {\n        switch(c) {\n        case 'A': flags |= NOTIFY_ALL; break;\n        case 'g': flags |= NOTIFY_GENERIC; break;\n        case '$': flags |= NOTIFY_STRING; break;\n        case 'l': flags |= NOTIFY_LIST; break;\n        case 's': flags |= NOTIFY_SET; break;\n        case 'h': flags |= NOTIFY_HASH; break;\n        case 'z': flags |= NOTIFY_ZSET; break;\n        case 'x': flags |= NOTIFY_EXPIRED; break;\n        case 'e': flags |= NOTIFY_EVICTED; break;\n        case 'K': flags |= NOTIFY_KEYSPACE; break;\n        case 'E': flags |= NOTIFY_KEYEVENT; break;\n        case 't': flags |= NOTIFY_STREAM; break;\n        case 'm': flags |= NOTIFY_KEY_MISS; break;\n        case 'd': flags |= NOTIFY_MODULE; break;\n        default: return -1;\n        }\n    }\n    return flags;\n}\n\n/* This function does exactly the reverse of the function above: it gets\n * as input an integer with the xored flags and returns a string representing\n * the selected classes. The string returned is an sds string that needs to\n * be released with sdsfree(). */\nsds keyspaceEventsFlagsToString(int flags) {\n    sds res;\n\n    res = sdsempty();\n    if ((flags & NOTIFY_ALL) == NOTIFY_ALL) {\n        res = sdscatlen(res,\"A\",1);\n    } else {\n        if (flags & NOTIFY_GENERIC) res = sdscatlen(res,\"g\",1);\n        if (flags & NOTIFY_STRING) res = sdscatlen(res,\"$\",1);\n        if (flags & NOTIFY_LIST) res = sdscatlen(res,\"l\",1);\n        if (flags & NOTIFY_SET) res = sdscatlen(res,\"s\",1);\n        if (flags & NOTIFY_HASH) res = sdscatlen(res,\"h\",1);\n        if (flags & NOTIFY_ZSET) res = sdscatlen(res,\"z\",1);\n        if (flags & NOTIFY_EXPIRED) res = sdscatlen(res,\"x\",1);\n        if (flags & NOTIFY_EVICTED) res = sdscatlen(res,\"e\",1);\n        if (flags & NOTIFY_STREAM) res = sdscatlen(res,\"t\",1);\n        if (flags & NOTIFY_MODULE) res = sdscatlen(res,\"d\",1);\n    }\n    if (flags & NOTIFY_KEYSPACE) res = sdscatlen(res,\"K\",1);\n    if (flags & NOTIFY_KEYEVENT) res = sdscatlen(res,\"E\",1);\n    if (flags & NOTIFY_KEY_MISS) res = sdscatlen(res,\"m\",1);\n    return res;\n}\n\n/* The API provided to the rest of the Redis core is a simple function:\n *\n * notifyKeyspaceEvent(char *event, robj *key, int dbid);\n *\n * 'event' is a C string representing the event name.\n * 'key' is a Redis object representing the key name.\n * 'dbid' is the database ID where the key lives.  */\nvoid notifyKeyspaceEvent(int type, const char *event, robj *key, int dbid) {\n    sds chan;\n    robj *chanobj, *eventobj;\n    int len = -1;\n    char buf[24];\n\n    /* If any modules are interested in events, notify the module system now.\n     * This bypasses the notifications configuration, but the module engine\n     * will only call event subscribers if the event type matches the types\n     * they are interested in. */\n     moduleNotifyKeyspaceEvent(type, event, key, dbid);\n\n    /* If notifications for this class of events are off, return ASAP. */\n    if (!(g_pserver->notify_keyspace_events & type)) return;\n\n    eventobj = createStringObject(event,strlen(event));\n\n    /* __keyspace@<db>__:<key> <event> notifications. */\n    if (g_pserver->notify_keyspace_events & NOTIFY_KEYSPACE) {\n        chan = sdsnewlen(\"__keyspace@\",11);\n        len = ll2string(buf,sizeof(buf),dbid);\n        chan = sdscatlen(chan, buf, len);\n        chan = sdscatlen(chan, \"__:\", 3);\n        chan = sdscatsds(chan, szFromObj(key));\n        chanobj = createObject(OBJ_STRING, chan);\n        pubsubPublishMessage(chanobj, eventobj);\n        decrRefCount(chanobj);\n    }\n\n    /* __keyevent@<db>__:<event> <key> notifications. */\n    if (g_pserver->notify_keyspace_events & NOTIFY_KEYEVENT) {\n        chan = sdsnewlen(\"__keyevent@\",11);\n        if (len == -1) len = ll2string(buf,sizeof(buf),dbid);\n        chan = sdscatlen(chan, buf, len);\n        chan = sdscatlen(chan, \"__:\", 3);\n        chan = sdscatsds(chan, szFromObj(eventobj));\n        chanobj = createObject(OBJ_STRING, chan);\n        pubsubPublishMessage(chanobj, key);\n        decrRefCount(chanobj);\n    }\n    decrRefCount(eventobj);\n}\n"
  },
  {
    "path": "src/object.cpp",
    "content": "/* Redis Object implementation.\n *\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n#include \"cron.h\"\n#include \"t_nhash.h\"\n#include <math.h>\n#include <ctype.h>\n#include <mutex>\n\n#ifdef __CYGWIN__\n#define strtold(a,b) ((long double)strtod((a),(b)))\n#endif\n\n/* ===================== Creation and parsing of objects ==================== */\n\nrobj *createObject(int type, void *ptr) {\n    size_t mvccExtraBytes = g_pserver->fActiveReplica ? sizeof(redisObjectExtended) : 0;\n    char *oB = (char*)zcalloc(sizeof(robj)+mvccExtraBytes, MALLOC_SHARED);\n    robj *o = reinterpret_cast<robj*>(oB + mvccExtraBytes);\n    \n    new (o) redisObject;\n    o->type = type;\n    o->encoding = OBJ_ENCODING_RAW;\n    o->m_ptr = ptr;\n    o->setrefcount(1);\n    setMvccTstamp(o, OBJ_MVCC_INVALID);\n\n    /* Set the LRU to the current lruclock (minutes resolution), or\n     * alternatively the LFU counter. */\n    if (g_pserver->maxmemory_policy & MAXMEMORY_FLAG_LFU) {\n        o->lru = (LFUGetTimeInMinutes()<<8) | LFU_INIT_VAL;\n    } else {\n        o->lru = LRU_CLOCK();\n    }\n    return o;\n}\n\n/* Set a special refcount in the object to make it \"shared\":\n * incrRefCount and decrRefCount() will test for this special refcount\n * and will not touch the object. This way it is free to access shared\n * objects such as small integers from different threads without any\n * mutex.\n *\n * A common patter to create shared objects:\n *\n * robj *myobject = makeObjectShared(createObject(...));\n *\n */\nrobj *makeObjectShared(robj *o) {\n    serverAssert(o->getrefcount(std::memory_order_relaxed) == 1);\n    serverAssert(!o->FExpires());\n    o->setrefcount(OBJ_SHARED_REFCOUNT);\n    return o;\n}\n\nrobj *makeObjectShared(const char *rgch, size_t cch)\n{\n    robj *o = createObject(OBJ_STRING,sdsnewlen(rgch, cch));\n    return makeObjectShared(o);\n}\n\n/* Create a string object with encoding OBJ_ENCODING_RAW, that is a plain\n * string object where ptrFromObj(o) points to a proper sds string. */\nrobj *createRawStringObject(const char *ptr, size_t len) {\n    return createObject(OBJ_STRING, sdsnewlen(ptr,len));\n}\n\n/* Create a string object with encoding OBJ_ENCODING_EMBSTR, that is\n * an object where the sds string is actually an unmodifiable string\n * allocated in the same chunk as the object itself. */\nrobj *createEmbeddedStringObject(const char *ptr, size_t len) {\n    serverAssert(len <= UINT8_MAX);\n    // Note: If the size changes update serializeStoredStringObject\n    size_t allocsize = sizeof(struct sdshdr8)+len+1;\n    if (allocsize < sizeof(void*))\n        allocsize = sizeof(void*);\n\n    size_t mvccExtraBytes = g_pserver->fActiveReplica ? sizeof(redisObjectExtended) : 0;\n    char *oB = (char*)zmalloc(sizeof(robj)+allocsize-sizeof(redisObject::m_ptr)+mvccExtraBytes, MALLOC_SHARED);\n    robj *o = reinterpret_cast<robj*>(oB + mvccExtraBytes);\n    struct sdshdr8 *sh = (sdshdr8*)(&o->m_ptr);\n\n    new (o) redisObject;\n    o->type = OBJ_STRING;\n    o->encoding = OBJ_ENCODING_EMBSTR;\n    o->setrefcount(1);\n    setMvccTstamp(o, OBJ_MVCC_INVALID);\n\n    if (g_pserver->maxmemory_policy & MAXMEMORY_FLAG_LFU) {\n        o->lru = (LFUGetTimeInMinutes()<<8) | LFU_INIT_VAL;\n    } else {\n        o->lru = LRU_CLOCK();\n    }\n\n    sh->len = len;\n    sh->alloc = len;\n    sh->flags = SDS_TYPE_8;\n    if (ptr == SDS_NOINIT)\n        sh->buf()[len] = '\\0';\n    else if (ptr) {\n        memcpy(sh->buf(),ptr,len);\n        sh->buf()[len] = '\\0';\n    } else {\n        memset(sh->buf(),0,len+1);\n    }\n    return o;\n}\n\n/* Create a string object with EMBSTR encoding if it is smaller than\n * OBJ_ENCODING_EMBSTR_SIZE_LIMIT, otherwise the RAW encoding is\n * used.\n *\n * The current limit of 52 is chosen so that the biggest string object\n * we allocate as EMBSTR will still fit into the 64 byte arena of jemalloc. */\nsize_t OBJ_ENCODING_EMBSTR_SIZE_LIMIT = 52;\n\nrobj *createStringObject(const char *ptr, size_t len) {\n    if (len <= OBJ_ENCODING_EMBSTR_SIZE_LIMIT)\n        return createEmbeddedStringObject(ptr,len);\n    else\n        return createRawStringObject(ptr,len);\n}\n\n/* Same as CreateRawStringObject, can return NULL if allocation fails */\nrobj *tryCreateRawStringObject(const char *ptr, size_t len) {\n    sds str = sdstrynewlen(ptr,len);\n    if (!str) return NULL;\n    return createObject(OBJ_STRING, str);\n}\n\n/* Same as createStringObject, can return NULL if allocation fails */\nrobj *tryCreateStringObject(const char *ptr, size_t len) {\n    if (len <= OBJ_ENCODING_EMBSTR_SIZE_LIMIT)\n        return createEmbeddedStringObject(ptr,len);\n    else\n        return tryCreateRawStringObject(ptr,len);\n}\n\n/* Create a string object from a long long value. When possible returns a\n * shared integer object, or at least an integer encoded one.\n *\n * If valueobj is non zero, the function avoids returning a shared\n * integer, because the object is going to be used as value in the Redis key\n * space (for instance when the INCR command is used), so we want LFU/LRU\n * values specific for each key. */\nrobj *createStringObjectFromLongLongWithOptions(long long value, int valueobj) {\n    robj *o;\n\n    if (g_pserver->maxmemory == 0 ||\n        !(g_pserver->maxmemory_policy & MAXMEMORY_FLAG_NO_SHARED_INTEGERS))\n    {\n        /* If the maxmemory policy permits, we can still return shared integers\n         * even if valueobj is true. */\n        valueobj = 0;\n    }\n\n    if (value >= 0 && value < OBJ_SHARED_INTEGERS && valueobj == 0) {\n        incrRefCount(shared.integers[value]);\n        o = shared.integers[value];\n    } else {\n        if (value >= LONG_MIN && value <= LONG_MAX) {\n            o = createObject(OBJ_STRING, NULL);\n            o->encoding = OBJ_ENCODING_INT;\n            o->m_ptr = (void*)((long)value);\n        } else {\n            o = createObject(OBJ_STRING,sdsfromlonglong(value));\n        }\n    }\n    return o;\n}\n\n/* Wrapper for createStringObjectFromLongLongWithOptions() always demanding\n * to create a shared object if possible. */\nrobj *createStringObjectFromLongLong(long long value) {\n    return createStringObjectFromLongLongWithOptions(value,0);\n}\n\n/* Wrapper for createStringObjectFromLongLongWithOptions() avoiding a shared\n * object when LFU/LRU info are needed, that is, when the object is used\n * as a value in the key space, and Redis is configured to evict based on\n * LFU/LRU. */\nrobj *createStringObjectFromLongLongForValue(long long value) {\n    return createStringObjectFromLongLongWithOptions(value,1);\n}\n\n/* Create a string object from a long double. If humanfriendly is non-zero\n * it does not use exponential format and trims trailing zeroes at the end,\n * however this results in loss of precision. Otherwise exp format is used\n * and the output of snprintf() is not modified.\n *\n * The 'humanfriendly' option is used for INCRBYFLOAT and HINCRBYFLOAT. */\nrobj *createStringObjectFromLongDouble(long double value, int humanfriendly) {\n    char buf[MAX_LONG_DOUBLE_CHARS];\n    int len = ld2string(buf,sizeof(buf),value,humanfriendly? LD_STR_HUMAN: LD_STR_AUTO);\n    return createStringObject(buf,len);\n}\n\n/* Duplicate a string object, with the guarantee that the returned object\n * has the same encoding as the original one.\n *\n * This function also guarantees that duplicating a small integer object\n * (or a string object that contains a representation of a small integer)\n * will always result in a fresh object that is unshared (refcount == 1).\n *\n * The resulting object always has refcount set to 1. */\nrobj *dupStringObject(const robj *o) {\n    robj *d;\n\n    serverAssert(o->type == OBJ_STRING);\n\n    switch(o->encoding) {\n    case OBJ_ENCODING_RAW:\n        return createRawStringObject(szFromObj(o),sdslen(szFromObj(o)));\n    case OBJ_ENCODING_EMBSTR:\n        return createEmbeddedStringObject(szFromObj(o),sdslen(szFromObj(o)));\n    case OBJ_ENCODING_INT:\n        d = createObject(OBJ_STRING, NULL);\n        d->encoding = OBJ_ENCODING_INT;\n        d->m_ptr = ptrFromObj(o);\n        return d;\n    default:\n        serverPanic(\"Wrong encoding.\");\n        break;\n    }\n}\n\nrobj *createQuicklistObject(void) {\n    quicklist *l = quicklistCreate();\n    robj *o = createObject(OBJ_LIST,l);\n    o->encoding = OBJ_ENCODING_QUICKLIST;\n    return o;\n}\n\nrobj *createZiplistObject(void) {\n    unsigned char *zl = ziplistNew();\n    robj *o = createObject(OBJ_LIST,zl);\n    o->encoding = OBJ_ENCODING_ZIPLIST;\n    return o;\n}\n\nrobj *createSetObject(void) {\n    dict *d = dictCreate(&setDictType,NULL);\n    robj *o = createObject(OBJ_SET,d);\n    o->encoding = OBJ_ENCODING_HT;\n    return o;\n}\n\nrobj *createIntsetObject(void) {\n    intset *is = intsetNew();\n    robj *o = createObject(OBJ_SET,is);\n    o->encoding = OBJ_ENCODING_INTSET;\n    return o;\n}\n\nrobj *createHashObject(void) {\n    unsigned char *zl = ziplistNew();\n    robj *o = createObject(OBJ_HASH, zl);\n    o->encoding = OBJ_ENCODING_ZIPLIST;\n    return o;\n}\n\nrobj *createZsetObject(void) {\n    zset *zs = (zset*)zmalloc(sizeof(*zs), MALLOC_SHARED);\n    robj *o;\n\n    zs->dict = dictCreate(&zsetDictType,NULL);\n    zs->zsl = zslCreate();\n    o = createObject(OBJ_ZSET,zs);\n    o->encoding = OBJ_ENCODING_SKIPLIST;\n    return o;\n}\n\nrobj *createZsetZiplistObject(void) {\n    unsigned char *zl = ziplistNew();\n    robj *o = createObject(OBJ_ZSET,zl);\n    o->encoding = OBJ_ENCODING_ZIPLIST;\n    return o;\n}\n\nrobj *createStreamObject(void) {\n    stream *s = streamNew();\n    robj *o = createObject(OBJ_STREAM,s);\n    o->encoding = OBJ_ENCODING_STREAM;\n    return o;\n}\n\nrobj *createModuleObject(moduleType *mt, void *value) {\n    moduleValue *mv = (moduleValue*)zmalloc(sizeof(*mv), MALLOC_SHARED);\n    mv->type = mt;\n    mv->value = value;\n    return createObject(OBJ_MODULE,mv);\n}\n\nvoid freeStringObject(robj_roptr o) {\n    if (o->encoding == OBJ_ENCODING_RAW) {\n        sdsfree(szFromObj(o));\n    }\n}\n\nvoid freeListObject(robj_roptr o) {\n    if (o->encoding == OBJ_ENCODING_QUICKLIST) {\n        quicklistRelease((quicklist*)ptrFromObj(o));\n    } else if (o->encoding == OBJ_ENCODING_ZIPLIST) {\n        zfree(ptrFromObj(o));\n    } else {\n        serverPanic(\"Unknown list encoding type: %d\", o->encoding);\n    }\n}\n\nvoid freeSetObject(robj_roptr o) {\n    switch (o->encoding) {\n    case OBJ_ENCODING_HT:\n        dictRelease((dict*) ptrFromObj(o));\n        break;\n    case OBJ_ENCODING_INTSET:\n        zfree(ptrFromObj(o));\n        break;\n    default:\n        serverPanic(\"Unknown set encoding type\");\n    }\n}\n\nvoid freeZsetObject(robj_roptr o) {\n    zset *zs;\n    switch (o->encoding) {\n    case OBJ_ENCODING_SKIPLIST:\n        zs = (zset*)ptrFromObj(o);\n        dictRelease(zs->dict);\n        zslFree(zs->zsl);\n        zfree(zs);\n        break;\n    case OBJ_ENCODING_ZIPLIST:\n        zfree(ptrFromObj(o));\n        break;\n    default:\n        serverPanic(\"Unknown sorted set encoding\");\n    }\n}\n\nvoid freeHashObject(robj_roptr o) {\n    switch (o->encoding) {\n    case OBJ_ENCODING_HT:\n        dictRelease((dict*) ptrFromObj(o));\n        break;\n    case OBJ_ENCODING_ZIPLIST:\n        zfree(ptrFromObj(o));\n        break;\n    default:\n        serverPanic(\"Unknown hash encoding type\");\n        break;\n    }\n}\n\nvoid freeModuleObject(robj_roptr o) {\n    moduleValue *mv = (moduleValue*)ptrFromObj(o);\n    mv->type->free(mv->value);\n    zfree(mv);\n}\n\nvoid freeStreamObject(robj_roptr o) {\n    freeStream((stream*)ptrFromObj(o));\n}\n\nvoid incrRefCount(robj_roptr o) {\n    auto refcount = o->getrefcount(std::memory_order_relaxed);\n    if (refcount < OBJ_FIRST_SPECIAL_REFCOUNT) {\n        o->addref();\n    } else {\n        if (refcount == OBJ_SHARED_REFCOUNT) {\n            /* Nothing to do: this refcount is immutable. */\n        } else if (refcount == OBJ_STATIC_REFCOUNT) {\n            serverPanic(\"You tried to retain an object allocated in the stack\");\n        }\n    }\n}\n\nvoid decrRefCount(robj_roptr o) {\n    if (o->getrefcount(std::memory_order_relaxed) == OBJ_SHARED_REFCOUNT)\n        return;\n    unsigned prev = o->release();\n    if (prev == 1) {\n        switch(o->type) {\n        case OBJ_STRING: freeStringObject(o); break;\n        case OBJ_LIST: freeListObject(o); break;\n        case OBJ_SET: freeSetObject(o); break;\n        case OBJ_ZSET: freeZsetObject(o); break;\n        case OBJ_HASH: freeHashObject(o); break;\n        case OBJ_MODULE: freeModuleObject(o); break;\n        case OBJ_STREAM: freeStreamObject(o); break;\n        case OBJ_CRON: freeCronObject(o); break;\n        case OBJ_NESTEDHASH: freeNestedHashObject(o); break;\n        default: serverPanic(\"Unknown object type\"); break;\n        }\n        o->~redisObject();\n        if (g_pserver->fActiveReplica) {\n            zfree(reinterpret_cast<redisObjectExtended*>(o.unsafe_robjcast())-1);\n        } else {\n            zfree(o.unsafe_robjcast());\n        }\n    } else {\n        if (prev <= 0) serverPanic(\"decrRefCount against refcount <= 0\");\n    }\n}\n\n/* This variant of decrRefCount() gets its argument as void, and is useful\n * as free method in data structures that expect a 'void free_object(void*)'\n * prototype for the free method. */\nvoid decrRefCountVoid(const void *o) {\n    decrRefCount((robj*)o);\n}\n\nint checkType(client *c, robj_roptr o, int type) {\n    /* A NULL is considered an empty key */\n    if (o && o->type != type) {\n        addReplyErrorObject(c,shared.wrongtypeerr);\n        return 1;\n    }\n    return 0;\n}\n\nint isSdsRepresentableAsLongLong(const char *s, long long *llval) {\n    return string2ll(s,sdslen(s),llval) ? C_OK : C_ERR;\n}\n\nint isObjectRepresentableAsLongLong(robj *o, long long *llval) {\n    serverAssertWithInfo(NULL,o,o->type == OBJ_STRING);\n    if (o->encoding == OBJ_ENCODING_INT) {\n        if (llval) *llval = (long) ptrFromObj(o);\n        return C_OK;\n    } else {\n        return isSdsRepresentableAsLongLong(szFromObj(o),llval);\n    }\n}\n\n/* Optimize the SDS string inside the string object to require little space,\n * in case there is more than 10% of free space at the end of the SDS\n * string. This happens because SDS strings tend to overallocate to avoid\n * wasting too much time in allocations when appending to the string. */\nvoid trimStringObjectIfNeeded(robj *o) {\n    if (o->encoding == OBJ_ENCODING_RAW &&\n        sdsavail(szFromObj(o)) > sdslen(szFromObj(o))/10)\n    {\n        o->m_ptr = sdsRemoveFreeSpace(szFromObj(o));\n    }\n}\n\n/* Try to encode a string object in order to save space */\nrobj *tryObjectEncoding(robj *o) {\n    long value;\n    sds s = szFromObj(o);\n    size_t len;\n\n    /* Make sure this is a string object, the only type we encode\n     * in this function. Other types use encoded memory efficient\n     * representations but are handled by the commands implementing\n     * the type. */\n    serverAssertWithInfo(NULL,o,o->type == OBJ_STRING);\n\n    /* We try some specialized encoding only for objects that are\n     * RAW or EMBSTR encoded, in other words objects that are still\n     * in represented by an actually array of chars. */\n    if (!sdsEncodedObject(o)) return o;\n\n    /* It's not safe to encode shared objects: shared objects can be shared\n     * everywhere in the \"object space\" of Redis and may end in places where\n     * they are not handled. We handle them only as values in the keyspace. */\n     if (o->getrefcount(std::memory_order_relaxed) > 1) return o;\n\n    /* Check if we can represent this string as a long integer.\n     * Note that we are sure that a string larger than 20 chars is not\n     * representable as a 32 nor 64 bit integer. */\n    len = sdslen(s);\n    if (len <= 20 && string2l(s,len,&value)) {\n        /* This object is encodable as a long. Try to use a shared object.\n         * Note that we avoid using shared integers when maxmemory is used\n         * because every object needs to have a private LRU field for the LRU\n         * algorithm to work well. */\n        if ((g_pserver->maxmemory == 0 ||\n            !(g_pserver->maxmemory_policy & MAXMEMORY_FLAG_NO_SHARED_INTEGERS)) &&\n            value >= 0 &&\n            value < OBJ_SHARED_INTEGERS)\n        {\n            decrRefCount(o);\n            incrRefCount(shared.integers[value]);\n            return shared.integers[value];\n        } else {\n            if (o->encoding == OBJ_ENCODING_RAW) {\n                sdsfree(szFromObj(o));\n                o->encoding = OBJ_ENCODING_INT;\n                o->m_ptr = (void*) value;\n                return o;\n            } else if (o->encoding == OBJ_ENCODING_EMBSTR) {\n                decrRefCount(o);\n                return createStringObjectFromLongLongForValue(value);\n            }\n        }\n    }\n\n    /* If the string is small and is still RAW encoded,\n     * try the EMBSTR encoding which is more efficient.\n     * In this representation the object and the SDS string are allocated\n     * in the same chunk of memory to save space and cache misses. */\n    if (len <= OBJ_ENCODING_EMBSTR_SIZE_LIMIT) {\n        robj *emb;\n\n        if (o->encoding == OBJ_ENCODING_EMBSTR) return o;\n        emb = createEmbeddedStringObject(s,sdslen(s));\n        decrRefCount(o);\n        return emb;\n    }\n\n    /* We can't encode the object...\n     *\n     * Do the last try, and at least optimize the SDS string inside\n     * the string object to require little space, in case there\n     * is more than 10% of free space at the end of the SDS string.\n     *\n     * We do that only for relatively large strings as this branch\n     * is only entered if the length of the string is greater than\n     * OBJ_ENCODING_EMBSTR_SIZE_LIMIT. */\n    trimStringObjectIfNeeded(o);\n\n    /* Return the original object. */\n    return o;\n}\n\n/* Get a decoded version of an encoded object (returned as a new object).\n * If the object is already raw-encoded just increment the ref count. */\nrobj *getDecodedObject(robj *o) {\n    robj *dec;\n\n    if (sdsEncodedObject(o)) {\n        incrRefCount(o);\n        return o;\n    }\n    if (o->type == OBJ_STRING && o->encoding == OBJ_ENCODING_INT) {\n        char buf[32];\n\n        ll2string(buf,32,(long)ptrFromObj(o));\n        dec = createStringObject(buf,strlen(buf));\n        return dec;\n    } else {\n        serverPanic(\"Unknown encoding type\");\n    }\n}\n\nrobj_roptr getDecodedObject(robj_roptr o) {\n    return getDecodedObject(o.unsafe_robjcast());\n}\n\n/* Compare two string objects via strcmp() or strcoll() depending on flags.\n * Note that the objects may be integer-encoded. In such a case we\n * use ll2string() to get a string representation of the numbers on the stack\n * and compare the strings, it's much faster than calling getDecodedObject().\n *\n * Important note: when REDIS_COMPARE_BINARY is used a binary-safe comparison\n * is used. */\n\n#define REDIS_COMPARE_BINARY (1<<0)\n#define REDIS_COMPARE_COLL (1<<1)\n\nint compareStringObjectsWithFlags(robj *a, robj *b, int flags) {\n    serverAssertWithInfo(NULL,a,a->type == OBJ_STRING && b->type == OBJ_STRING);\n    char bufa[128], bufb[128], *astr, *bstr;\n    size_t alen, blen, minlen;\n\n    if (a == b) return 0;\n    if (sdsEncodedObject(a)) {\n        astr = szFromObj(a);\n        alen = sdslen(astr);\n    } else {\n        alen = ll2string(bufa,sizeof(bufa),(long) ptrFromObj(a));\n        astr = bufa;\n    }\n    if (sdsEncodedObject(b)) {\n        bstr = szFromObj(b);\n        blen = sdslen(bstr);\n    } else {\n        blen = ll2string(bufb,sizeof(bufb),(long) ptrFromObj(b));\n        bstr = bufb;\n    }\n    if (flags & REDIS_COMPARE_COLL) {\n        return strcoll(astr,bstr);\n    } else {\n        int cmp;\n\n        minlen = (alen < blen) ? alen : blen;\n        cmp = memcmp(astr,bstr,minlen);\n        if (cmp == 0) return alen-blen;\n        return cmp;\n    }\n}\n\n/* Wrapper for compareStringObjectsWithFlags() using binary comparison. */\nint compareStringObjects(robj *a, robj *b) {\n    return compareStringObjectsWithFlags(a,b,REDIS_COMPARE_BINARY);\n}\n\n/* Wrapper for compareStringObjectsWithFlags() using collation. */\nint collateStringObjects(robj *a, robj *b) {\n    return compareStringObjectsWithFlags(a,b,REDIS_COMPARE_COLL);\n}\n\n/* Equal string objects return 1 if the two objects are the same from the\n * point of view of a string comparison, otherwise 0 is returned. Note that\n * this function is faster then checking for (compareStringObject(a,b) == 0)\n * because it can perform some more optimization. */\nint equalStringObjects(robj *a, robj *b) {\n    if (a->encoding == OBJ_ENCODING_INT &&\n        b->encoding == OBJ_ENCODING_INT){\n        /* If both strings are integer encoded just check if the stored\n         * long is the same. */\n        return a->m_ptr == b->m_ptr;\n    } else {\n        return compareStringObjects(a,b) == 0;\n    }\n}\n\nsize_t stringObjectLen(robj_roptr o) {\n    serverAssertWithInfo(NULL,o,o->type == OBJ_STRING);\n    if (sdsEncodedObject(o)) {\n        return sdslen(szFromObj(o));\n    } else {\n        return sdigits10((long)ptrFromObj(o));\n    }\n}\n\nint getDoubleFromObject(const robj *o, double *target) {\n    double value;\n\n    if (o == NULL) {\n        value = 0;\n    } else {\n        serverAssertWithInfo(NULL,o,o->type == OBJ_STRING);\n        if (sdsEncodedObject(o)) {\n            if (!string2d(szFromObj(o), sdslen(szFromObj(o)), &value))\n                return C_ERR;\n        } else if (o->encoding == OBJ_ENCODING_INT) {\n            value = (long)ptrFromObj(o);\n        } else {\n            serverPanic(\"Unknown string encoding\");\n        }\n    }\n    *target = value;\n    return C_OK;\n}\n\nint getDoubleFromObjectOrReply(client *c, robj *o, double *target, const char *msg) {\n    double value;\n    if (getDoubleFromObject(o, &value) != C_OK) {\n        if (msg != NULL) {\n            addReplyError(c,(char*)msg);\n        } else {\n            addReplyError(c,\"value is not a valid float\");\n        }\n        return C_ERR;\n    }\n    *target = value;\n    return C_OK;\n}\n\nint getLongDoubleFromObject(robj *o, long double *target) {\n    long double value;\n\n    if (o == NULL) {\n        value = 0;\n    } else {\n        serverAssertWithInfo(NULL,o,o->type == OBJ_STRING);\n        if (sdsEncodedObject(o)) {\n            if (!string2ld(szFromObj(o), sdslen(szFromObj(o)), &value))\n                return C_ERR;\n        } else if (o->encoding == OBJ_ENCODING_INT) {\n            value = (long)szFromObj(o);\n        } else {\n            serverPanic(\"Unknown string encoding\");\n        }\n    }\n    *target = value;\n    return C_OK;\n}\n\nint getLongDoubleFromObjectOrReply(client *c, robj *o, long double *target, const char *msg) {\n    long double value;\n    if (getLongDoubleFromObject(o, &value) != C_OK) {\n        if (msg != NULL) {\n            addReplyError(c,(char*)msg);\n        } else {\n            addReplyError(c,\"value is not a valid float\");\n        }\n        return C_ERR;\n    }\n    *target = value;\n    return C_OK;\n}\n\nint getLongLongFromObject(robj *o, long long *target) {\n    long long value;\n\n    if (o == NULL) {\n        value = 0;\n    } else {\n        serverAssertWithInfo(NULL,o,o->type == OBJ_STRING);\n        if (sdsEncodedObject(o)) {\n            if (string2ll(szFromObj(o),sdslen(szFromObj(o)),&value) == 0) return C_ERR;\n        } else if (o->encoding == OBJ_ENCODING_INT) {\n            value = (long)ptrFromObj(o);\n        } else {\n            serverPanic(\"Unknown string encoding\");\n        }\n    }\n    if (target) *target = value;\n    return C_OK;\n}\n\nint getUnsignedLongLongFromObject(robj *o, uint64_t *target) {\n    uint64_t value;\n\n    if (o == NULL) {\n        value = 0;\n    } else {\n        serverAssertWithInfo(NULL,o,o->type == OBJ_STRING);\n        if (sdsEncodedObject(o)) {\n            char *pchEnd = nullptr;\n            errno = 0;\n            value = strtoull(szFromObj(o), &pchEnd, 10);\n            if (value == 0) {\n                // potential error\n                if (errno != 0)\n                    return C_ERR;\n                if (pchEnd == szFromObj(o))\n                    return C_ERR;\n            }\n        } else if (o->encoding == OBJ_ENCODING_INT) {\n            value = (long)ptrFromObj(o);\n        } else {\n            serverPanic(\"Unknown string encoding\");\n        }\n    }\n    if (target) *target = value;\n    return C_OK;\n}\n\nint getLongLongFromObjectOrReply(client *c, robj *o, long long *target, const char *msg) {\n    long long value;\n    if (getLongLongFromObject(o, &value) != C_OK) {\n        if (msg != NULL) {\n            addReplyError(c,(char*)msg);\n        } else {\n            addReplyError(c,\"value is not an integer or out of range\");\n        }\n        return C_ERR;\n    }\n    *target = value;\n    return C_OK;\n}\n\nint getUnsignedLongLongFromObjectOrReply(client *c, robj *o, uint64_t *target, const char *msg) {\n    uint64_t value;\n    if (getUnsignedLongLongFromObject(o, &value) != C_OK) {\n        if (msg != NULL) {\n            addReplyError(c,(char*)msg);\n        } else {\n            addReplyError(c,\"value is not an integer or out of range\");\n        }\n        return C_ERR;\n    }\n    *target = value;\n    return C_OK;\n}\n\nint getLongFromObjectOrReply(client *c, robj *o, long *target, const char *msg) {\n    long long value;\n\n    if (getLongLongFromObjectOrReply(c, o, &value, msg) != C_OK) return C_ERR;\n    if (value < LONG_MIN || value > LONG_MAX) {\n        if (msg != NULL) {\n            addReplyError(c,(char*)msg);\n        } else {\n            addReplyError(c,\"value is out of range\");\n        }\n        return C_ERR;\n    }\n    *target = value;\n    return C_OK;\n}\n\nint getRangeLongFromObjectOrReply(client *c, robj *o, long min, long max, long *target, const char *msg) {\n    if (getLongFromObjectOrReply(c, o, target, msg) != C_OK) return C_ERR;\n    if (*target < min || *target > max) {\n        if (msg != NULL) {\n            addReplyError(c,(char*)msg);\n        } else {\n            addReplyErrorFormat(c,\"value is out of range, value must between %ld and %ld\", min, max);\n        }\n        return C_ERR;\n    }\n    return C_OK;\n}\n\nint getPositiveLongFromObjectOrReply(client *c, robj *o, long *target, const char *msg) {\n    if (msg) {\n        return getRangeLongFromObjectOrReply(c, o, 0, LONG_MAX, target, msg);\n    } else {\n        return getRangeLongFromObjectOrReply(c, o, 0, LONG_MAX, target, \"value is out of range, must be positive\");\n    }\n}\n\nint getIntFromObjectOrReply(client *c, robj *o, int *target, const char *msg) {\n    long value;\n\n    if (getRangeLongFromObjectOrReply(c, o, INT_MIN, INT_MAX, &value, msg) != C_OK)\n        return C_ERR;\n\n    *target = value;\n    return C_OK;\n}\n\nconst char *strEncoding(int encoding) {\n    switch(encoding) {\n    case OBJ_ENCODING_RAW: return \"raw\";\n    case OBJ_ENCODING_INT: return \"int\";\n    case OBJ_ENCODING_HT: return \"hashtable\";\n    case OBJ_ENCODING_QUICKLIST: return \"quicklist\";\n    case OBJ_ENCODING_ZIPLIST: return \"ziplist\";\n    case OBJ_ENCODING_INTSET: return \"intset\";\n    case OBJ_ENCODING_SKIPLIST: return \"skiplist\";\n    case OBJ_ENCODING_EMBSTR: return \"embstr\";\n    case OBJ_ENCODING_STREAM: return \"stream\";\n    default: return \"unknown\";\n    }\n}\n\n/* =========================== Memory introspection ========================= */\n\n\n/* This is an helper function with the goal of estimating the memory\n * size of a radix tree that is used to store Stream IDs.\n *\n * Note: to guess the size of the radix tree is not trivial, so we\n * approximate it considering 16 bytes of data overhead for each\n * key (the ID), and then adding the number of bare nodes, plus some\n * overhead due by the data and child pointers. This secret recipe\n * was obtained by checking the average radix tree created by real\n * workloads, and then adjusting the constants to get numbers that\n * more or less match the real memory usage.\n *\n * Actually the number of nodes and keys may be different depending\n * on the insertion speed and thus the ability of the radix tree\n * to compress prefixes. */\nsize_t streamRadixTreeMemoryUsage(rax *rax) {\n    size_t size;\n    size = rax->numele * sizeof(streamID);\n    size += rax->numnodes * sizeof(raxNode);\n    /* Add a fixed overhead due to the aux data pointer, children, ... */\n    size += rax->numnodes * sizeof(long)*30;\n    return size;\n}\n\n/* Returns the size in bytes consumed by the key's value in RAM.\n * Note that the returned value is just an approximation, especially in the\n * case of aggregated data types where only \"sample_size\" elements\n * are checked and averaged to estimate the total size. */\n#define OBJ_COMPUTE_SIZE_DEF_SAMPLES 5 /* Default sample size. */\nsize_t objectComputeSize(robj_roptr o, size_t sample_size) {\n    sds ele, ele2;\n    dict *d;\n    dictIterator *di;\n    struct dictEntry *de;\n    size_t asize = 0, elesize = 0, samples = 0;\n\n    if (o->type == OBJ_STRING) {\n        if(o->encoding == OBJ_ENCODING_INT) {\n            asize = sizeof(*o);\n        } else if(o->encoding == OBJ_ENCODING_RAW) {\n            asize = sdsZmallocSize((sds)szFromObj(o))+sizeof(*o);\n        } else if(o->encoding == OBJ_ENCODING_EMBSTR) {\n            asize = sdslen(szFromObj(o))+2+sizeof(*o);\n        } else {\n            serverPanic(\"Unknown string encoding\");\n        }\n    } else if (o->type == OBJ_LIST) {\n        if (o->encoding == OBJ_ENCODING_QUICKLIST) {\n            quicklist *ql = (quicklist*)ptrFromObj(o);\n            quicklistNode *node = ql->head;\n            asize = sizeof(*o)+sizeof(quicklist);\n            do {\n                elesize += sizeof(quicklistNode)+ziplistBlobLen(node->zl);\n                samples++;\n            } while ((node = node->next) && samples < sample_size);\n            asize += (double)elesize/samples*ql->len;\n        } else if (o->encoding == OBJ_ENCODING_ZIPLIST) {\n            asize = sizeof(*o)+ziplistBlobLen((unsigned char*)ptrFromObj(o));\n        } else {\n            serverPanic(\"Unknown list encoding\");\n        }\n    } else if (o->type == OBJ_SET) {\n        if (o->encoding == OBJ_ENCODING_HT) {\n            d = (dict*)ptrFromObj(o);\n            di = dictGetIterator(d);\n            asize = sizeof(*o)+sizeof(dict)+(sizeof(struct dictEntry*)*dictSlots(d));\n            while((de = dictNext(di)) != NULL && samples < sample_size) {\n                ele = (sds)dictGetKey(de);\n                elesize += sizeof(struct dictEntry) + sdsZmallocSize(ele);\n                samples++;\n            }\n            dictReleaseIterator(di);\n            if (samples) asize += (double)elesize/samples*dictSize(d);\n        } else if (o->encoding == OBJ_ENCODING_INTSET) {\n            intset *is = (intset*)ptrFromObj(o);\n            asize = sizeof(*o)+sizeof(*is)+(size_t)is->encoding*is->length;\n        } else {\n            serverPanic(\"Unknown set encoding\");\n        }\n    } else if (o->type == OBJ_ZSET) {\n        if (o->encoding == OBJ_ENCODING_ZIPLIST) {\n            asize = sizeof(*o)+(ziplistBlobLen((unsigned char*)ptrFromObj(o)));\n        } else if (o->encoding == OBJ_ENCODING_SKIPLIST) {\n            d = ((zset*)ptrFromObj(o))->dict;\n            zskiplist *zsl = ((zset*)ptrFromObj(o))->zsl;\n            zskiplistNode *znode = zsl->header->level(0)->forward;\n            asize = sizeof(*o)+sizeof(zset)+sizeof(zskiplist)+sizeof(dict)+\n                    (sizeof(struct dictEntry*)*dictSlots(d))+\n                    zmalloc_size(zsl->header);\n            while(znode != NULL && samples < sample_size) {\n                elesize += sdsZmallocSize(znode->ele);\n                elesize += sizeof(struct dictEntry) + zmalloc_size(znode);\n                samples++;\n                znode = znode->level(0)->forward;\n            }\n            if (samples) asize += (double)elesize/samples*dictSize(d);\n        } else {\n            serverPanic(\"Unknown sorted set encoding\");\n        }\n    } else if (o->type == OBJ_HASH) {\n        if (o->encoding == OBJ_ENCODING_ZIPLIST) {\n            asize = sizeof(*o)+(ziplistBlobLen((unsigned char*)ptrFromObj(o)));\n        } else if (o->encoding == OBJ_ENCODING_HT) {\n            d = (dict*)ptrFromObj(o);\n            di = dictGetIterator(d);\n            asize = sizeof(*o)+sizeof(dict)+(sizeof(struct dictEntry*)*dictSlots(d));\n            while((de = dictNext(di)) != NULL && samples < sample_size) {\n                ele = (sds)dictGetKey(de);\n                ele2 = (sds)dictGetVal(de);\n                elesize += sdsZmallocSize(ele) + sdsZmallocSize(ele2);\n                elesize += sizeof(struct dictEntry);\n                samples++;\n            }\n            dictReleaseIterator(di);\n            if (samples) asize += (double)elesize/samples*dictSize(d);\n        } else {\n            serverPanic(\"Unknown hash encoding\");\n        }\n    } else if (o->type == OBJ_STREAM) {\n        stream *s = (stream*)ptrFromObj(o);\n        asize = sizeof(*o)+sizeof(*s);\n        asize += streamRadixTreeMemoryUsage(s->rax);\n\n        /* Now we have to add the listpacks. The last listpack is often non\n         * complete, so we estimate the size of the first N listpacks, and\n         * use the average to compute the size of the first N-1 listpacks, and\n         * finally add the real size of the last node. */\n        raxIterator ri;\n        raxStart(&ri,s->rax);\n        raxSeek(&ri,\"^\",NULL,0);\n        size_t lpsize = 0, samples = 0;\n        while(samples < sample_size && raxNext(&ri)) {\n            unsigned char *lp = (unsigned char*)ri.data;\n            lpsize += lpBytes(lp);\n            samples++;\n        }\n        if (s->rax->numele <= samples) {\n            asize += lpsize;\n        } else {\n            if (samples) lpsize /= samples; /* Compute the average. */\n            asize += lpsize * (s->rax->numele-1);\n            /* No need to check if seek succeeded, we enter this branch only\n             * if there are a few elements in the radix tree. */\n            raxSeek(&ri,\"$\",NULL,0);\n            raxNext(&ri);\n            asize += lpBytes((unsigned char*)ri.data);\n        }\n        raxStop(&ri);\n\n        /* Consumer groups also have a non trivial memory overhead if there\n         * are many consumers and many groups, let's count at least the\n         * overhead of the pending entries in the groups and consumers\n         * PELs. */\n        if (s->cgroups) {\n            raxStart(&ri,s->cgroups);\n            raxSeek(&ri,\"^\",NULL,0);\n            while(raxNext(&ri)) {\n                streamCG *cg = (streamCG*)ri.data;\n                asize += sizeof(*cg);\n                asize += streamRadixTreeMemoryUsage(cg->pel);\n                asize += sizeof(streamNACK)*raxSize(cg->pel);\n\n                /* For each consumer we also need to add the basic data\n                 * structures and the PEL memory usage. */\n                raxIterator cri;\n                raxStart(&cri,cg->consumers);\n                raxSeek(&cri,\"^\",NULL,0);\n                while(raxNext(&cri)) {\n                    streamConsumer *consumer = (streamConsumer*)cri.data;\n                    asize += sizeof(*consumer);\n                    asize += sdslen(consumer->name);\n                    asize += streamRadixTreeMemoryUsage(consumer->pel);\n                    /* Don't count NACKs again, they are shared with the\n                     * consumer group PEL. */\n                }\n                raxStop(&cri);\n            }\n            raxStop(&ri);\n        }\n    } else if (o->type == OBJ_MODULE) {\n        moduleValue *mv = (moduleValue*)ptrFromObj(o);\n        moduleType *mt = mv->type;\n        if (mt->mem_usage != NULL) {\n            asize = mt->mem_usage(mv->value);\n        } else {\n            asize = 0;\n        }\n    } else {\n        serverPanic(\"Unknown object type\");\n    }\n    return asize;\n}\n\n/* Release data obtained with getMemoryOverheadData(). */\nvoid freeMemoryOverheadData(struct redisMemOverhead *mh) {\n    zfree(mh->db);\n    zfree(mh);\n}\n\n/* Return a struct redisMemOverhead filled with memory overhead\n * information used for the MEMORY OVERHEAD and INFO command. The returned\n * structure pointer should be freed calling freeMemoryOverheadData(). */\nstruct redisMemOverhead *getMemoryOverheadData(void) {\n    serverAssert(GlobalLocksAcquired());\n    int j;\n    size_t mem_total = 0;\n    size_t mem = 0;\n    size_t zmalloc_used = zmalloc_used_memory();\n    struct redisMemOverhead *mh = (redisMemOverhead*)zcalloc(sizeof(*mh), MALLOC_LOCAL);\n\n    mh->total_allocated = zmalloc_used;\n    mh->startup_allocated = g_pserver->initial_memory_usage;\n    mh->peak_allocated = g_pserver->stat_peak_memory;\n    mh->total_frag =\n        (float)g_pserver->cron_malloc_stats.process_rss / g_pserver->cron_malloc_stats.zmalloc_used;\n    mh->total_frag_bytes =\n        g_pserver->cron_malloc_stats.process_rss - g_pserver->cron_malloc_stats.zmalloc_used;\n    mh->allocator_frag =\n        (float)g_pserver->cron_malloc_stats.allocator_active / g_pserver->cron_malloc_stats.allocator_allocated;\n    mh->allocator_frag_bytes =\n        g_pserver->cron_malloc_stats.allocator_active - g_pserver->cron_malloc_stats.allocator_allocated;\n    mh->allocator_rss =\n        (float)g_pserver->cron_malloc_stats.allocator_resident / g_pserver->cron_malloc_stats.allocator_active;\n    mh->allocator_rss_bytes =\n        g_pserver->cron_malloc_stats.allocator_resident - g_pserver->cron_malloc_stats.allocator_active;\n    mh->rss_extra =\n        (float)g_pserver->cron_malloc_stats.process_rss / g_pserver->cron_malloc_stats.allocator_resident;\n    mh->rss_extra_bytes =\n        g_pserver->cron_malloc_stats.process_rss - g_pserver->cron_malloc_stats.allocator_resident;\n\n    mem_total += g_pserver->initial_memory_usage;\n\n    mem = 0;\n    if (g_pserver->repl_backlog && g_pserver->repl_backlog != g_pserver->repl_backlog_disk)\n        mem += zmalloc_size(g_pserver->repl_backlog);\n    mh->repl_backlog = mem;\n    mem_total += mem;\n\n    /* Computing the memory used by the clients would be O(N) if done\n     * here online. We use our values computed incrementally by\n     * clientsCronTrackClientsMemUsage(). */\n    mh->clients_slaves = g_pserver->stat_clients_type_memory[CLIENT_TYPE_SLAVE];\n    mh->clients_normal = g_pserver->stat_clients_type_memory[CLIENT_TYPE_MASTER]+\n                         g_pserver->stat_clients_type_memory[CLIENT_TYPE_PUBSUB]+\n                         g_pserver->stat_clients_type_memory[CLIENT_TYPE_NORMAL];\n    mem_total += mh->clients_slaves;\n    mem_total += mh->clients_normal;\n\n    mem = 0;\n    if (g_pserver->aof_state != AOF_OFF) {\n        mem += sdsZmallocSize(g_pserver->aof_buf);\n        mem += aofRewriteBufferSize();\n    }\n    mh->aof_buffer = mem;\n    mem_total+=mem;\n\n    mem = g_pserver->lua_scripts_mem;\n    mem += dictSize(g_pserver->lua_scripts) * sizeof(dictEntry) +\n        dictSlots(g_pserver->lua_scripts) * sizeof(dictEntry*);\n    mem += dictSize(g_pserver->repl_scriptcache_dict) * sizeof(dictEntry) +\n        dictSlots(g_pserver->repl_scriptcache_dict) * sizeof(dictEntry*);\n    if (listLength(g_pserver->repl_scriptcache_fifo) > 0) {\n        mem += listLength(g_pserver->repl_scriptcache_fifo) * (sizeof(listNode) + \n            sdsZmallocSize((sds)listNodeValue(listFirst(g_pserver->repl_scriptcache_fifo))));\n    }\n    mh->lua_caches = mem;\n    mem_total+=mem;\n\n    for (j = 0; j < cserver.dbnum; j++) {\n        redisDb *db = g_pserver->db[j];\n        long long keyscount = db->size();\n        if (keyscount==0) continue;\n\n        mh->total_keys += keyscount;\n        mh->db = (decltype(mh->db))zrealloc(mh->db,sizeof(mh->db[0])*(mh->num_dbs+1), MALLOC_LOCAL);\n        mh->db[mh->num_dbs].dbid = j;\n\n        mem = db->size() * sizeof(dictEntry) +\n              db->slots() * sizeof(dictEntry*) +\n              db->size() * sizeof(robj);\n        mh->db[mh->num_dbs].overhead_ht_main = mem;\n        mem_total+=mem;\n        \n        mh->db[mh->num_dbs].overhead_ht_expires = 0;\n\n        mh->num_dbs++;\n    }\n\n    mh->overhead_total = mem_total;\n    mh->dataset = zmalloc_used - mem_total;\n    mh->peak_perc = (float)zmalloc_used*100/mh->peak_allocated;\n\n    /* Metrics computed after subtracting the startup memory from\n     * the total memory. */\n    size_t net_usage = 1;\n    if (zmalloc_used > mh->startup_allocated)\n        net_usage = zmalloc_used - mh->startup_allocated;\n    mh->dataset_perc = (float)mh->dataset*100/net_usage;\n    mh->bytes_per_key = mh->total_keys ? (net_usage / mh->total_keys) : 0;\n\n    return mh;\n}\n\n/* Helper for \"MEMORY allocator-stats\", used as a callback for the jemalloc\n * stats output. */\nvoid inputCatSds(void *result, const char *str) {\n    /* result is actually a (sds *), so re-cast it here */\n    sds *info = (sds *)result;\n    *info = sdscat(*info, str);\n}\n\n/* This implements MEMORY DOCTOR. An human readable analysis of the Redis\n * memory condition. */\nsds getMemoryDoctorReport(void) {\n    serverAssert(GlobalLocksAcquired());\n    int empty = 0;          /* Instance is empty or almost empty. */\n    int big_peak = 0;       /* Memory peak is much larger than used mem. */\n    int high_frag = 0;      /* High fragmentation. */\n    int high_alloc_frag = 0;/* High allocator fragmentation. */\n    int high_proc_rss = 0;  /* High process rss overhead. */\n    int high_alloc_rss = 0; /* High rss overhead. */\n    int big_slave_buf = 0;  /* Slave buffers are too big. */\n    int big_client_buf = 0; /* Client buffers are too big. */\n    int many_scripts = 0;   /* Script cache has too many scripts. */\n    int num_reports = 0;\n    struct redisMemOverhead *mh = getMemoryOverheadData();\n\n    if (mh->total_allocated < (1024*1024*5)) {\n        empty = 1;\n        num_reports++;\n    } else {\n        /* Peak is > 150% of current used memory? */\n        if (((float)mh->peak_allocated / mh->total_allocated) > 1.5) {\n            big_peak = 1;\n            num_reports++;\n        }\n\n        /* Fragmentation is higher than 1.4 and 10MB ?*/\n        if (mh->total_frag > 1.4 && mh->total_frag_bytes > 10<<20) {\n            high_frag = 1;\n            num_reports++;\n        }\n\n        /* External fragmentation is higher than 1.1 and 10MB? */\n        if (mh->allocator_frag > 1.1 && mh->allocator_frag_bytes > 10<<20) {\n            high_alloc_frag = 1;\n            num_reports++;\n        }\n\n        /* Allocator rss is higher than 1.1 and 10MB ? */\n        if (mh->allocator_rss > 1.1 && mh->allocator_rss_bytes > 10<<20) {\n            high_alloc_rss = 1;\n            num_reports++;\n        }\n\n        /* Non-Allocator rss is higher than 1.1 and 10MB ? */\n        if (mh->rss_extra > 1.1 && mh->rss_extra_bytes > 10<<20) {\n            high_proc_rss = 1;\n            num_reports++;\n        }\n\n        /* Clients using more than 200k each average? */\n        long numslaves = listLength(g_pserver->slaves);\n        long numclients = listLength(g_pserver->clients)-numslaves;\n        if ((numclients > 0) && mh->clients_normal / numclients > (1024*200)) {\n            big_client_buf = 1;\n            num_reports++;\n        }\n\n        /* Slaves using more than 10 MB each? */\n        if (numslaves > 0 && mh->clients_slaves / numslaves > (1024*1024*10)) {\n            big_slave_buf = 1;\n            num_reports++;\n        }\n\n        /* Too many scripts are cached? */\n        if (dictSize(g_pserver->lua_scripts) > 1000) {\n            many_scripts = 1;\n            num_reports++;\n        }\n    }\n\n    sds s;\n    if (num_reports == 0) {\n        s = sdsnew(\n        \"Hi Sam, I can't find any memory issue in your instance. \"\n        \"I can only account for what occurs on this base.\\n\");\n    } else if (empty == 1) {\n        s = sdsnew(\n        \"Hi Sam, this instance is empty or is using very little memory, \"\n        \"my issues detector can't be used in these conditions. \"\n        \"Please, leave for your mission on Earth and fill it with some data. \"\n        \"The new Sam and I will be back to our programming as soon as I \"\n        \"finished rebooting.\\n\");\n    } else {\n        s = sdsnew(\"Sam, I detected a few issues in this KeyDB instance memory implants:\\n\\n\");\n        if (big_peak) {\n            s = sdscat(s,\" * Peak memory: In the past this instance used more than 150% the memory that is currently using. The allocator is normally not able to release memory after a peak, so you can expect to see a big fragmentation ratio, however this is actually harmless and is only due to the memory peak, and if the KeyDB instance Resident Set Size (RSS) is currently bigger than expected, the memory will be used as soon as you fill the KeyDB instance with more data. If the memory peak was only occasional and you want to try to reclaim memory, please try the MEMORY PURGE command, otherwise the only other option is to shutdown and restart the instance.\\n\\n\");\n        }\n        if (high_frag) {\n            s = sdscatprintf(s,\" * High total RSS: This instance has a memory fragmentation and RSS overhead greater than 1.4 (this means that the Resident Set Size of the KeyDB process is much larger than the sum of the logical allocations KeyDB performed). This problem is usually due either to a large peak memory (check if there is a peak memory entry above in the report) or may result from a workload that causes the allocator to fragment memory a lot. If the problem is a large peak memory, then there is no issue. Otherwise, make sure you are using the Jemalloc allocator and not the default libc malloc. Note: The currently used allocator is \\\"%s\\\".\\n\\n\", ZMALLOC_LIB);\n        }\n        if (high_alloc_frag) {\n            s = sdscatprintf(s,\" * High allocator fragmentation: This instance has an allocator external fragmentation greater than 1.1. This problem is usually due either to a large peak memory (check if there is a peak memory entry above in the report) or may result from a workload that causes the allocator to fragment memory a lot. You can try enabling 'activedefrag' config option.\\n\\n\");\n        }\n        if (high_alloc_rss) {\n            s = sdscatprintf(s,\" * High allocator RSS overhead: This instance has an RSS memory overhead is greater than 1.1 (this means that the Resident Set Size of the allocator is much larger than the sum what the allocator actually holds). This problem is usually due to a large peak memory (check if there is a peak memory entry above in the report), you can try the MEMORY PURGE command to reclaim it.\\n\\n\");\n        }\n        if (high_proc_rss) {\n            s = sdscatprintf(s,\" * High process RSS overhead: This instance has non-allocator RSS memory overhead is greater than 1.1 (this means that the Resident Set Size of the KeyDB process is much larger than the RSS the allocator holds). This problem may be due to Lua scripts or Modules.\\n\\n\");\n        }\n        if (big_slave_buf) {\n            s = sdscat(s,\" * Big replica buffers: The replica output buffers in this instance are greater than 10MB for each replica (on average). This likely means that there is some replica instance that is struggling receiving data, either because it is too slow or because of networking issues. As a result, data piles on the master output buffers. Please try to identify what replica is not receiving data correctly and why. You can use the INFO output in order to check the replicas delays and the CLIENT LIST command to check the output buffers of each replica.\\n\\n\");\n        }\n        if (big_client_buf) {\n            s = sdscat(s,\" * Big client buffers: The clients output buffers in this instance are greater than 200K per client (on average). This may result from different causes, like Pub/Sub clients subscribed to channels bot not receiving data fast enough, so that data piles on the KeyDB instance output buffer, or clients sending commands with large replies or very large sequences of commands in the same pipeline. Please use the CLIENT LIST command in order to investigate the issue if it causes problems in your instance, or to understand better why certain clients are using a big amount of memory.\\n\\n\");\n        }\n        if (many_scripts) {\n            s = sdscat(s,\" * Many scripts: There seem to be many cached scripts in this instance (more than 1000). This may be because scripts are generated and `EVAL`ed, instead of being parameterized (with KEYS and ARGV), `SCRIPT LOAD`ed and `EVALSHA`ed. Unless `SCRIPT FLUSH` is called periodically, the scripts' caches may end up consuming most of your memory.\\n\\n\");\n        }\n        s = sdscat(s,\"I'm here to keep you safe, Sam. I want to help you.\\n\");\n    }\n    freeMemoryOverheadData(mh);\n    return s;\n}\n\n/* Set the object LRU/LFU depending on g_pserver->maxmemory_policy.\n * The lfu_freq arg is only relevant if policy is MAXMEMORY_FLAG_LFU.\n * The lru_idle and lru_clock args are only relevant if policy\n * is MAXMEMORY_FLAG_LRU.\n * Either or both of them may be <0, in that case, nothing is set. */\nint objectSetLRUOrLFU(robj *val, long long lfu_freq, long long lru_idle,\n                       long long lru_clock, int lru_multiplier) {\n    if (g_pserver->maxmemory_policy & MAXMEMORY_FLAG_LFU) {\n        if (lfu_freq >= 0) {\n            serverAssert(lfu_freq <= 255);\n            val->lru = (LFUGetTimeInMinutes()<<8) | lfu_freq;\n            return 1;\n        }\n    } else if (lru_idle >= 0) {\n        /* Provided LRU idle time is in seconds. Scale\n         * according to the LRU clock resolution this Redis\n         * instance was compiled with (normally 1000 ms, so the\n         * below statement will expand to lru_idle*1000/1000. */\n        lru_idle = lru_idle*lru_multiplier/LRU_CLOCK_RESOLUTION;\n        long lru_abs = lru_clock - lru_idle; /* Absolute access time. */\n        /* If the LRU field underflows (since LRU it is a wrapping\n         * clock), the best we can do is to provide a large enough LRU\n         * that is half-way in the circlular LRU clock we use: this way\n         * the computed idle time for this object will stay high for quite\n         * some time. */\n        if (lru_abs < 0)\n            lru_abs = (lru_clock+(LRU_CLOCK_MAX/2)) % LRU_CLOCK_MAX;\n        val->lru = lru_abs;\n        return 1;\n    }\n    return 0;\n}\n\n/* ======================= The OBJECT and MEMORY commands =================== */\n\n/* This is a helper function for the OBJECT command. We need to lookup keys\n * without any modification of LRU or other parameters. */\nrobj_roptr objectCommandLookup(client *c, robj *key) {\n    return c->db->find(key);\n}\n\nrobj_roptr objectCommandLookupOrReply(client *c, robj *key, robj *reply) {\n    robj_roptr o = objectCommandLookup(c,key);\n    if (!o) SentReplyOnKeyMiss(c, reply);\n    return o;\n}\n\n/* Object command allows to inspect the internals of a Redis Object.\n * Usage: OBJECT <refcount|encoding|idletime|freq> <key> */\nvoid objectCommand(client *c) {\n    robj_roptr o;\n\n    if (c->argc == 2 && !strcasecmp(szFromObj(c->argv[1]),\"help\")) {\n        const char *help[] = {\n\"ENCODING <key>\",\n\"    Return the kind of internal representation used in order to store the value\",\n\"    associated with a <key>.\",\n\"FREQ <key>\",\n\"    Return the access frequency index of the <key>. The returned integer is\",\n\"    proportional to the logarithm of the recent access frequency of the key.\",\n\"IDLETIME <key>\",\n\"    Return the idle time of the <key>, that is the approximated number of\",\n\"    seconds elapsed since the last access to the key.\",\n\"REFCOUNT <key>\",\n\"    Return the number of references of the value associated with the specified\",\n\"    <key>.\",\nNULL\n        };\n        addReplyHelp(c, help);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"refcount\") && c->argc == 3) {\n        if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.null[c->resp]))\n                == nullptr) return;\n        addReplyLongLong(c,o->getrefcount(std::memory_order_relaxed));\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"encoding\") && c->argc == 3) {\n        if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.null[c->resp]))\n                == nullptr) return;\n        addReplyBulkCString(c,strEncoding(o->encoding));\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"idletime\") && c->argc == 3) {\n        if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.null[c->resp]))\n                == nullptr) return;\n        if (g_pserver->maxmemory_policy & MAXMEMORY_FLAG_LFU) {\n            addReplyError(c,\"An LFU maxmemory policy is selected, idle time not tracked. Please note that when switching between policies at runtime LRU and LFU data will take some time to adjust.\");\n            return;\n        }\n        addReplyLongLong(c,estimateObjectIdleTime(o)/1000);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"freq\") && c->argc == 3) {\n        if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.null[c->resp]))\n                == nullptr) return;\n        if (!(g_pserver->maxmemory_policy & MAXMEMORY_FLAG_LFU)) {\n            addReplyError(c,\"An LFU maxmemory policy is not selected, access frequency not tracked. Please note that when switching between policies at runtime LRU and LFU data will take some time to adjust.\");\n            return;\n        }\n        /* LFUDecrAndReturn should be called\n         * in case of the key has not been accessed for a long time,\n         * because we update the access time only\n         * when the key is read or overwritten. */\n        addReplyLongLong(c,LFUDecrAndReturn(o.unsafe_robjcast()));\n    } else if (!strcasecmp(szFromObj(c->argv[1]), \"lastmodified\") && c->argc == 3) {\n        if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.null[c->resp]))\n                == nullptr) return;\n        uint64_t mvcc = mvccFromObj(o);\n        addReplyLongLong(c, (g_pserver->mstime - (mvcc >> MVCC_MS_SHIFT)) / 1000);\n    } else {\n        addReplySubcommandSyntaxError(c);\n    }\n}\n\n/* The memory command will eventually be a complete interface for the\n * memory introspection capabilities of Redis.\n *\n * Usage: MEMORY usage <key> */\nvoid memoryCommand(client *c) {\n    if (!strcasecmp(szFromObj(c->argv[1]),\"help\") && c->argc == 2) {\n        const char *help[] = {\n            \"DOCTOR\",\n            \"    Return memory problems reports.\",\n            \"MALLOC-STATS\"\n            \"    Return internal statistics report from the memory allocator.\",\n            \"PURGE\",\n            \"    Attempt to purge dirty pages for reclamation by the allocator.\",\n            \"STATS\",\n            \"    Return information about the memory usage of the server.\",\n            \"USAGE <key> [SAMPLES <count>]\",\n            \"    Return memory in bytes used by <key> and its value. Nested values are\",\n            \"    sampled up to <count> times (default: 5).\",\n            NULL\n        };\n        addReplyHelp(c, help);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"usage\") && c->argc >= 3) {\n        long long samples = OBJ_COMPUTE_SIZE_DEF_SAMPLES;\n        for (int j = 3; j < c->argc; j++) {\n            if (!strcasecmp(szFromObj(c->argv[j]),\"samples\") &&\n                j+1 < c->argc)\n            {\n                if (getLongLongFromObjectOrReply(c,c->argv[j+1],&samples,NULL)\n                     == C_ERR) return;\n                if (samples < 0) {\n                    addReplyErrorObject(c,shared.syntaxerr);\n                    return;\n                }\n                if (samples == 0) samples = LLONG_MAX;\n                j++; /* skip option argument. */\n            } else {\n                addReplyErrorObject(c,shared.syntaxerr);\n                return;\n            }\n        }\n\n        auto itr = c->db->find(c->argv[2]);\n        if (itr == nullptr) {\n            addReplyNull(c);\n            return;\n        }\n        size_t usage = objectComputeSize(itr.val(),samples);\n        usage += sdsZmallocSize(itr.key());\n        usage += sizeof(dictEntry);\n        addReplyLongLong(c,usage);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"stats\") && c->argc == 2) {\n        struct redisMemOverhead *mh = getMemoryOverheadData();\n\n        addReplyMapLen(c,25+mh->num_dbs);\n\n        addReplyBulkCString(c,\"peak.allocated\");\n        addReplyLongLong(c,mh->peak_allocated);\n\n        addReplyBulkCString(c,\"total.allocated\");\n        addReplyLongLong(c,mh->total_allocated);\n\n        addReplyBulkCString(c,\"startup.allocated\");\n        addReplyLongLong(c,mh->startup_allocated);\n\n        addReplyBulkCString(c,\"replication.backlog\");\n        addReplyLongLong(c,mh->repl_backlog);\n\n        addReplyBulkCString(c,\"clients.slaves\");\n        addReplyLongLong(c,mh->clients_slaves);\n\n        addReplyBulkCString(c,\"clients.normal\");\n        addReplyLongLong(c,mh->clients_normal);\n\n        addReplyBulkCString(c,\"aof.buffer\");\n        addReplyLongLong(c,mh->aof_buffer);\n\n        addReplyBulkCString(c,\"lua.caches\");\n        addReplyLongLong(c,mh->lua_caches);\n\n        for (size_t j = 0; j < mh->num_dbs; j++) {\n            char dbname[32];\n            snprintf(dbname,sizeof(dbname),\"db.%zd\",mh->db[j].dbid);\n            addReplyBulkCString(c,dbname);\n            addReplyMapLen(c,2);\n\n            addReplyBulkCString(c,\"overhead.hashtable.main\");\n            addReplyLongLong(c,mh->db[j].overhead_ht_main);\n\n            addReplyBulkCString(c,\"overhead.hashtable.expires\");\n            addReplyLongLong(c,mh->db[j].overhead_ht_expires);\n        }\n\n        addReplyBulkCString(c,\"overhead.total\");\n        addReplyLongLong(c,mh->overhead_total);\n\n        addReplyBulkCString(c,\"keys.count\");\n        addReplyLongLong(c,mh->total_keys);\n\n        addReplyBulkCString(c,\"keys.bytes-per-key\");\n        addReplyLongLong(c,mh->bytes_per_key);\n\n        addReplyBulkCString(c,\"dataset.bytes\");\n        addReplyLongLong(c,mh->dataset);\n\n        addReplyBulkCString(c,\"dataset.percentage\");\n        addReplyDouble(c,mh->dataset_perc);\n\n        addReplyBulkCString(c,\"peak.percentage\");\n        addReplyDouble(c,mh->peak_perc);\n\n        addReplyBulkCString(c,\"allocator.allocated\");\n        addReplyLongLong(c,g_pserver->cron_malloc_stats.allocator_allocated);\n\n        addReplyBulkCString(c,\"allocator.active\");\n        addReplyLongLong(c,g_pserver->cron_malloc_stats.allocator_active);\n\n        addReplyBulkCString(c,\"allocator.resident\");\n        addReplyLongLong(c,g_pserver->cron_malloc_stats.allocator_resident);\n\n        addReplyBulkCString(c,\"allocator-fragmentation.ratio\");\n        addReplyDouble(c,mh->allocator_frag);\n\n        addReplyBulkCString(c,\"allocator-fragmentation.bytes\");\n        addReplyLongLong(c,mh->allocator_frag_bytes);\n\n        addReplyBulkCString(c,\"allocator-rss.ratio\");\n        addReplyDouble(c,mh->allocator_rss);\n\n        addReplyBulkCString(c,\"allocator-rss.bytes\");\n        addReplyLongLong(c,mh->allocator_rss_bytes);\n\n        addReplyBulkCString(c,\"rss-overhead.ratio\");\n        addReplyDouble(c,mh->rss_extra);\n\n        addReplyBulkCString(c,\"rss-overhead.bytes\");\n        addReplyLongLong(c,mh->rss_extra_bytes);\n\n        addReplyBulkCString(c,\"fragmentation\"); /* this is the total RSS overhead, including fragmentation */\n        addReplyDouble(c,mh->total_frag); /* it is kept here for backwards compatibility */\n\n        addReplyBulkCString(c,\"fragmentation.bytes\");\n        addReplyLongLong(c,mh->total_frag_bytes);\n\n        freeMemoryOverheadData(mh);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"malloc-stats\") && c->argc == 2) {\n#if defined(USE_JEMALLOC)\n        sds info = sdsempty();\n        malloc_stats_print(inputCatSds, &info, NULL);\n        addReplyVerbatim(c,info,sdslen(info),\"txt\");\n        sdsfree(info);\n#else\n        addReplyBulkCString(c,\"Stats not supported for the current allocator\");\n#endif\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"doctor\") && c->argc == 2) {\n        sds report = getMemoryDoctorReport();\n        addReplyVerbatim(c,report,sdslen(report),\"txt\");\n        sdsfree(report);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"purge\") && c->argc == 2) {\n        if (jemalloc_purge() == 0)\n            addReply(c, shared.ok);\n        else\n            addReplyError(c, \"Error purging dirty pages\");\n    } else {\n        addReplySubcommandSyntaxError(c);\n    }\n}\n\nvoid redisObject::SetFExpires(bool fExpire)\n{\n    serverAssert(this->refcount != OBJ_SHARED_REFCOUNT || fExpire == FExpires());\n    if (fExpire)\n        this->refcount.fetch_or(1U << 31, std::memory_order_relaxed);\n    else\n        this->refcount.fetch_and(~(1U << 31), std::memory_order_relaxed);\n}\n\nvoid redisObject::setrefcount(unsigned ref)\n{ \n    serverAssert(!FExpires());\n    refcount.store(ref, std::memory_order_relaxed); \n}\n\nsds serializeStoredStringObject(sds str, robj_roptr o)\n{\n    uint64_t mvcc;\n    mvcc = mvccFromObj(o);\n    str = sdscatlen(str, &mvcc, sizeof(mvcc));\n    str = sdscatlen(str, &(*o), sizeof(robj));\n    static_assert((sizeof(robj) + sizeof(mvcc)) == sizeof(redisObjectStack), \"\");\n    switch (o->encoding)\n    {\n    case OBJ_ENCODING_EMBSTR:\n    case OBJ_ENCODING_RAW:\n        str = sdscatsds(str, (sds)szFromObj(o));\n        break;\n\n    case OBJ_ENCODING_INT:\n        break;  //nop\n    }\n        \n    return str;\n}\n\nrobj *deserializeStoredStringObject(const char *data, size_t cb)\n{\n    uint64_t mvcc = *reinterpret_cast<const uint64_t*>(data);\n    const robj *oT = (const robj*)(data+sizeof(uint64_t));\n    robj *newObject = nullptr;\n    switch (oT->encoding)\n    {\n    case OBJ_ENCODING_INT:\n        newObject = createObject(OBJ_STRING, nullptr);\n        newObject->encoding = oT->encoding;\n        newObject->m_ptr = oT->m_ptr;\n        break;\n\n    case OBJ_ENCODING_EMBSTR:\n        newObject = createEmbeddedStringObject(data+sizeof(robj)+sizeof(mvcc), cb-sizeof(robj)-sizeof(uint64_t));\n        break;\n\n    case OBJ_ENCODING_RAW:\n        newObject = createObject(OBJ_STRING, sdsnewlen(SDS_NOINIT,cb-sizeof(robj)-sizeof(uint64_t)));\n        newObject->lru = oT->lru;\n        memcpy(newObject->m_ptr, data+sizeof(robj)+sizeof(mvcc), cb-sizeof(robj)-sizeof(mvcc));\n        break;\n\n    default:\n        serverPanic(\"Unknown string object encoding from storage\");\n    }\n    setMvccTstamp(newObject, mvcc);\n\n    return newObject;\n}\n\nrobj *deserializeStoredObject(const void *data, size_t cb)\n{\n    switch (((char*)data)[0])\n    {\n        case RDB_TYPE_STRING:\n            return deserializeStoredStringObject(((char*)data)+1, cb-1);\n\n        default:\n            rio payload;\n            int type;\n            robj *obj;\n            rioInitWithConstBuffer(&payload,data,cb);\n            if (((type = rdbLoadObjectType(&payload)) == -1) ||\n                ((obj = rdbLoadObject(type,&payload,nullptr,NULL,OBJ_MVCC_INVALID)) == nullptr))\n            {\n                serverPanic(\"Bad data format\");\n            }\n            if (rdbLoadType(&payload) == RDB_OPCODE_AUX)\n            {\n                robj *auxkey, *auxval;\n                if ((auxkey = rdbLoadStringObject(&payload)) == NULL) goto eoferr;\n                if ((auxval = rdbLoadStringObject(&payload)) == NULL) {\n                    decrRefCount(auxkey);\n                    goto eoferr;\n                }\n                if (strcasecmp(szFromObj(auxkey), \"mvcc-tstamp\") == 0) {\n                    setMvccTstamp(obj, strtoull(szFromObj(auxval), nullptr, 10));\n                }\n                decrRefCount(auxkey);\n                decrRefCount(auxval);\n            }\n        eoferr:\n            return obj;\n    }\n    serverPanic(\"Unknown object type loading from storage\");\n}\n\nsds serializeStoredObject(robj_roptr o, sds sdsPrefix)\n{\n    switch (o->type)\n    {\n        case OBJ_STRING:\n        {\n            sds sdsT = nullptr;\n            if (sdsPrefix)\n                sdsT = sdsgrowzero(sdsPrefix, sdslen(sdsPrefix)+1);\n            else\n                sdsT = sdsnewlen(nullptr, 1);\n            sdsT[sdslen(sdsT)-1] = RDB_TYPE_STRING;\n            return serializeStoredStringObject(sdsT, o);\n        }\n            \n        default:\n            rio rdb;\n            createDumpPayload(&rdb,o,nullptr);\n            if (sdsPrefix)\n            {\n                sds rval = sdscatsds(sdsPrefix, (sds)rdb.io.buffer.ptr);\n                sdsfree((sds)rdb.io.buffer.ptr);\n                return rval;\n            }\n            return (sds)rdb.io.buffer.ptr;\n    }\n    serverPanic(\"Attempting to store unknown object type\");\n}\n\nredisObjectStack::redisObjectStack()\n{\n    // We need to ensure the Extended Object is first in the class layout\n    serverAssert(reinterpret_cast<ptrdiff_t>(static_cast<redisObject*>(this)) != reinterpret_cast<ptrdiff_t>(this));\n}\n\nvoid *allocPtrFromObj(robj_roptr o) {\n    if (g_pserver->fActiveReplica)\n        return reinterpret_cast<redisObjectExtended*>(o.unsafe_robjcast()) - 1;\n    return o.unsafe_robjcast();\n}\n\nrobj *objFromAllocPtr(void *pv) {\n    if (pv != nullptr && g_pserver->fActiveReplica) {\n        return reinterpret_cast<robj*>(reinterpret_cast<redisObjectExtended*>(pv)+1);\n    } \n    return reinterpret_cast<robj*>(pv);\n}\n\nuint64_t mvccFromObj(robj_roptr o)\n{\n    if (g_pserver->fActiveReplica) {\n        redisObjectExtended *oe = reinterpret_cast<redisObjectExtended*>(o.unsafe_robjcast()) - 1;\n        return oe->mvcc_tstamp;\n    }\n    return OBJ_MVCC_INVALID;\n}\n\nvoid setMvccTstamp(robj *o, uint64_t mvcc)\n{\n    if (!g_pserver->fActiveReplica)\n        return;\n    redisObjectExtended *oe = reinterpret_cast<redisObjectExtended*>(o) - 1;\n    oe->mvcc_tstamp = mvcc;\n}\n"
  },
  {
    "path": "src/pqsort.c",
    "content": "/* The following is the NetBSD libc qsort implementation modified in order to\n * support partial sorting of ranges for Redis.\n *\n * Copyright(C) 2009-2012 Salvatore Sanfilippo. All rights reserved.\n *\n * The original copyright notice follows. */\n\n\n/*\t$NetBSD: qsort.c,v 1.19 2009/01/30 23:38:44 lukem Exp $\t*/\n\n/*-\n * Copyright (c) 1992, 1993\n *\tThe Regents of the University of California.  All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions\n * are met:\n * 1. Redistributions of source code must retain the above copyright\n *    notice, this list of conditions and the following disclaimer.\n * 2. Redistributions in binary form must reproduce the above copyright\n *    notice, this list of conditions and the following disclaimer in the\n *    documentation and/or other materials provided with the distribution.\n * 3. Neither the name of the University nor the names of its contributors\n *    may be used to endorse or promote products derived from this software\n *    without specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND\n * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE\n * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\n * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\n * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\n * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\n * SUCH DAMAGE.\n */\n\n#include <sys/types.h>\n\n#include <errno.h>\n#include <stdlib.h>\n#include <stdint.h>\n\nstatic inline char\t*med3 (char *, char *, char *,\n    int (*)(const void *, const void *));\nstatic inline void\t swapfunc (char *, char *, size_t, int);\n\n#define min(a, b)\t(a) < (b) ? a : b\n\n/*\n * Qsort routine from Bentley & McIlroy's \"Engineering a Sort Function\".\n */\n#define swapcode(TYPE, parmi, parmj, n) { \t\t\\\n\tsize_t i = (n) / sizeof (TYPE); \t\t\\\n\tTYPE *pi = (TYPE *)(void *)(parmi); \t\t\\\n\tTYPE *pj = (TYPE *)(void *)(parmj); \t\t\\\n\tdo { \t\t\t\t\t\t\\\n\t\tTYPE\tt = *pi;\t\t\t\\\n\t\t*pi++ = *pj;\t\t\t\t\\\n\t\t*pj++ = t;\t\t\t\t\\\n        } while (--i > 0);\t\t\t\t\\\n}\n\n#define SWAPINIT(a, es) swaptype = (uintptr_t)a % sizeof(long) || \\\n\tes % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;\n\nstatic inline void\nswapfunc(char *a, char *b, size_t n, int swaptype)\n{\n\n\tif (swaptype <= 1)\n\t\tswapcode(long, a, b, n)\n\telse\n\t\tswapcode(char, a, b, n)\n}\n\n#define swap(a, b)\t\t\t\t\t\t\\\n\tif (swaptype == 0) {\t\t\t\t\t\\\n\t\tlong t = *(long *)(void *)(a);\t\t\t\\\n\t\t*(long *)(void *)(a) = *(long *)(void *)(b);\t\\\n\t\t*(long *)(void *)(b) = t;\t\t\t\\\n\t} else\t\t\t\t\t\t\t\\\n\t\tswapfunc(a, b, es, swaptype)\n\n#define vecswap(a, b, n) if ((n) > 0) swapfunc((a), (b), (size_t)(n), swaptype)\n\nstatic inline char *\nmed3(char *a, char *b, char *c,\n    int (*cmp) (const void *, const void *))\n{\n\n\treturn cmp(a, b) < 0 ?\n\t       (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))\n              :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));\n}\n\nstatic void\n_pqsort(void *a, size_t n, size_t es,\n    int (*cmp) (const void *, const void *), void *lrange, void *rrange)\n{\n\tchar *pa, *pb, *pc, *pd, *pl, *pm, *pn;\n\tsize_t d, r;\n\tint swaptype, cmp_result;\n\nloop:\tSWAPINIT(a, es);\n\tif (n < 7) {\n\t\tfor (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)\n\t\t\tfor (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;\n\t\t\t     pl -= es)\n\t\t\t\tswap(pl, pl - es);\n\t\treturn;\n\t}\n\tpm = (char *) a + (n / 2) * es;\n\tif (n > 7) {\n\t\tpl = (char *) a;\n\t\tpn = (char *) a + (n - 1) * es;\n\t\tif (n > 40) {\n\t\t\td = (n / 8) * es;\n\t\t\tpl = med3(pl, pl + d, pl + 2 * d, cmp);\n\t\t\tpm = med3(pm - d, pm, pm + d, cmp);\n\t\t\tpn = med3(pn - 2 * d, pn - d, pn, cmp);\n\t\t}\n\t\tpm = med3(pl, pm, pn, cmp);\n\t}\n\tswap(a, pm);\n\tpa = pb = (char *) a + es;\n\n\tpc = pd = (char *) a + (n - 1) * es;\n\tfor (;;) {\n\t\twhile (pb <= pc && (cmp_result = cmp(pb, a)) <= 0) {\n\t\t\tif (cmp_result == 0) {\n\t\t\t\tswap(pa, pb);\n\t\t\t\tpa += es;\n\t\t\t}\n\t\t\tpb += es;\n\t\t}\n\t\twhile (pb <= pc && (cmp_result = cmp(pc, a)) >= 0) {\n\t\t\tif (cmp_result == 0) {\n\t\t\t\tswap(pc, pd);\n\t\t\t\tpd -= es;\n\t\t\t}\n\t\t\tpc -= es;\n\t\t}\n\t\tif (pb > pc)\n\t\t\tbreak;\n\t\tswap(pb, pc);\n\t\tpb += es;\n\t\tpc -= es;\n\t}\n\n\tpn = (char *) a + n * es;\n\tr = min(pa - (char *) a, pb - pa);\n\tvecswap(a, pb - r, r);\n\tr = min((size_t)(pd - pc), pn - pd - es);\n\tvecswap(pb, pn - r, r);\n\tif ((r = pb - pa) > es) {\n                void *_l = a, *_r = ((unsigned char*)a)+r-1;\n                if (!((lrange < _l && rrange < _l) ||\n                    (lrange > _r && rrange > _r)))\n\t\t    _pqsort(a, r / es, es, cmp, lrange, rrange);\n        }\n\tif ((r = pd - pc) > es) {\n                void *_l, *_r;\n\n\t\t/* Iterate rather than recurse to save stack space */\n\t\ta = pn - r;\n\t\tn = r / es;\n\n                _l = a;\n                _r = ((unsigned char*)a)+r-1;\n                if (!((lrange < _l && rrange < _l) ||\n                    (lrange > _r && rrange > _r)))\n\t\t    goto loop;\n\t}\n/*\t\tqsort(pn - r, r / es, es, cmp);*/\n}\n\nvoid\npqsort(void *a, size_t n, size_t es,\n    int (*cmp) (const void *, const void *), size_t lrange, size_t rrange)\n{\n    _pqsort(a,n,es,cmp,((unsigned char*)a)+(lrange*es),\n                       ((unsigned char*)a)+((rrange+1)*es)-1);\n}\n"
  },
  {
    "path": "src/pqsort.h",
    "content": "/* The following is the NetBSD libc qsort implementation modified in order to\n * support partial sorting of ranges for Redis.\n *\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n *\n * See the pqsort.c file for the original copyright notice. */\n\n#ifndef __PQSORT_H\n#define __PQSORT_H\n\nextern \"C\" void\npqsort(void *a, size_t n, size_t es,\n    int (*cmp) (const void *, const void *), size_t lrange, size_t rrange);\n\n#endif\n"
  },
  {
    "path": "src/pubsub.cpp",
    "content": "/*\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n#include <mutex>\n\nint clientSubscriptionsCount(client *c);\n\n/*-----------------------------------------------------------------------------\n * Pubsub client replies API\n *----------------------------------------------------------------------------*/\n\n/* Send a pubsub message of type \"message\" to the client.\n * Normally 'msg' is a Redis object containing the string to send as\n * message. However if the caller sets 'msg' as NULL, it will be able\n * to send a special message (for instance an Array type) by using the\n * addReply*() API family. */\nvoid addReplyPubsubMessage(client *c, robj *channel, robj *msg) {\n    if (c->resp == 2)\n        addReply(c,shared.mbulkhdr[3]);\n    else\n        addReplyPushLen(c,3);\n    addReply(c,shared.messagebulk);\n    addReplyBulk(c,channel);\n    if (msg) addReplyBulk(c,msg);\n}\n\n/* Send a pubsub message of type \"pmessage\" to the client. The difference\n * with the \"message\" type delivered by addReplyPubsubMessage() is that\n * this message format also includes the pattern that matched the message. */\nvoid addReplyPubsubPatMessage(client *c, robj *pat, robj *channel, robj *msg) {\n    if (c->resp == 2)\n        addReply(c,shared.mbulkhdr[4]);\n    else\n        addReplyPushLen(c,4);\n    addReply(c,shared.pmessagebulk);\n    addReplyBulk(c,pat);\n    addReplyBulk(c,channel);\n    addReplyBulk(c,msg);\n}\n\n/* Send the pubsub subscription notification to the client. */\nvoid addReplyPubsubSubscribed(client *c, robj *channel) {\n    if (c->resp == 2)\n        addReply(c,shared.mbulkhdr[3]);\n    else\n        addReplyPushLen(c,3);\n    addReply(c,shared.subscribebulk);\n    addReplyBulk(c,channel);\n    addReplyLongLong(c,clientSubscriptionsCount(c));\n}\n\n/* Send the pubsub unsubscription notification to the client.\n * Channel can be NULL: this is useful when the client sends a mass\n * unsubscribe command but there are no channels to unsubscribe from: we\n * still send a notification. */\nvoid addReplyPubsubUnsubscribed(client *c, robj *channel) {\n    if (c->resp == 2)\n        addReply(c,shared.mbulkhdr[3]);\n    else\n        addReplyPushLen(c,3);\n    addReply(c,shared.unsubscribebulk);\n    if (channel)\n        addReplyBulk(c,channel);\n    else\n        addReplyNull(c);\n    addReplyLongLong(c,clientSubscriptionsCount(c));\n}\n\n/* Send the pubsub pattern subscription notification to the client. */\nvoid addReplyPubsubPatSubscribed(client *c, robj *pattern) {\n    if (c->resp == 2)\n        addReply(c,shared.mbulkhdr[3]);\n    else\n        addReplyPushLen(c,3);\n    addReply(c,shared.psubscribebulk);\n    addReplyBulk(c,pattern);\n    addReplyLongLong(c,clientSubscriptionsCount(c));\n}\n\n/* Send the pubsub pattern unsubscription notification to the client.\n * Pattern can be NULL: this is useful when the client sends a mass\n * punsubscribe command but there are no pattern to unsubscribe from: we\n * still send a notification. */\nvoid addReplyPubsubPatUnsubscribed(client *c, robj *pattern) {\n    if (c->resp == 2)\n        addReply(c,shared.mbulkhdr[3]);\n    else\n        addReplyPushLen(c,3);\n    addReply(c,shared.punsubscribebulk);\n    if (pattern)\n        addReplyBulk(c,pattern);\n    else\n        addReplyNull(c);\n    addReplyLongLong(c,clientSubscriptionsCount(c));\n}\n\n/*-----------------------------------------------------------------------------\n * Pubsub low level API\n *----------------------------------------------------------------------------*/\n\n/* Return the number of channels + patterns a client is subscribed to. */\nint clientSubscriptionsCount(client *c) {\n    return dictSize(c->pubsub_channels)+\n           listLength(c->pubsub_patterns);\n}\n\n/* Subscribe a client to a channel. Returns 1 if the operation succeeded, or\n * 0 if the client was already subscribed to that channel. */\nint pubsubSubscribeChannel(client *c, robj *channel) {\n    serverAssert(GlobalLocksAcquired());\n    serverAssert(c->lock.fOwnLock());\n    dictEntry *de;\n    list *clients = NULL;\n    int retval = 0;\n\n    /* Add the channel to the client -> channels hash table */\n    if (dictAdd(c->pubsub_channels,channel,NULL) == DICT_OK) {\n        retval = 1;\n        incrRefCount(channel);\n        /* Add the client to the channel -> list of clients hash table */\n        de = dictFind(g_pserver->pubsub_channels,channel);\n        if (de == NULL) {\n            clients = listCreate();\n            dictAdd(g_pserver->pubsub_channels,channel,clients);\n            incrRefCount(channel);\n        } else {\n            clients = (list*)dictGetVal(de);\n        }\n        listAddNodeTail(clients,c);\n    }\n    /* Notify the client */\n    addReplyPubsubSubscribed(c,channel);\n    return retval;\n}\n\n/* Unsubscribe a client from a channel. Returns 1 if the operation succeeded, or\n * 0 if the client was not subscribed to the specified channel. */\nint pubsubUnsubscribeChannel(client *c, robj *channel, int notify) {\n    dictEntry *de;\n    list *clients;\n    listNode *ln;\n    int retval = 0;\n\n    /* Remove the channel from the client -> channels hash table */\n    incrRefCount(channel); /* channel may be just a pointer to the same object\n                            we have in the hash tables. Protect it... */\n    if (dictDelete(c->pubsub_channels,channel) == DICT_OK) {\n        retval = 1;\n        /* Remove the client from the channel -> clients list hash table */\n        de = dictFind(g_pserver->pubsub_channels,channel);\n        serverAssertWithInfo(c,NULL,de != NULL);\n        clients = (list*)dictGetVal(de);\n        ln = listSearchKey(clients,c);\n        serverAssertWithInfo(c,NULL,ln != NULL);\n        listDelNode(clients,ln);\n        if (listLength(clients) == 0) {\n            /* Free the list and associated hash entry at all if this was\n             * the latest client, so that it will be possible to abuse\n             * Redis PUBSUB creating millions of channels. */\n            dictDelete(g_pserver->pubsub_channels,channel);\n        }\n    }\n    /* Notify the client */\n    if (notify) addReplyPubsubUnsubscribed(c,channel);\n    decrRefCount(channel); /* it is finally safe to release it */\n    return retval;\n}\n\n/* Subscribe a client to a pattern. Returns 1 if the operation succeeded, or 0 if the client was already subscribed to that pattern. */\nint pubsubSubscribePattern(client *c, robj *pattern) {\n    serverAssert(GlobalLocksAcquired());\n    dictEntry *de;\n    list *clients;\n    int retval = 0;\n\n    if (listSearchKey(c->pubsub_patterns,pattern) == NULL) {\n        retval = 1;\n        listAddNodeTail(c->pubsub_patterns,pattern);\n        incrRefCount(pattern);\n        /* Add the client to the pattern -> list of clients hash table */\n        de = dictFind(g_pserver->pubsub_patterns,pattern);\n        if (de == NULL) {\n            clients = listCreate();\n            dictAdd(g_pserver->pubsub_patterns,pattern,clients);\n            incrRefCount(pattern);\n        } else {\n            clients = (list*)dictGetVal(de);\n        }\n        listAddNodeTail(clients,c);\n    }\n    /* Notify the client */\n    addReplyPubsubPatSubscribed(c,pattern);\n    return retval;\n}\n\n/* Unsubscribe a client from a channel. Returns 1 if the operation succeeded, or\n * 0 if the client was not subscribed to the specified channel. */\nint pubsubUnsubscribePattern(client *c, robj *pattern, int notify) {\n    dictEntry *de;\n    list *clients;\n    listNode *ln;\n    int retval = 0;\n\n    incrRefCount(pattern); /* Protect the object. May be the same we remove */\n    if ((ln = listSearchKey(c->pubsub_patterns,pattern)) != NULL) {\n        retval = 1;\n        listDelNode(c->pubsub_patterns,ln);\n        /* Remove the client from the pattern -> clients list hash table */\n        de = dictFind(g_pserver->pubsub_patterns,pattern);\n        serverAssertWithInfo(c,NULL,de != NULL);\n        clients = (list*)dictGetVal(de);\n        ln = listSearchKey(clients,c);\n        serverAssertWithInfo(c,NULL,ln != NULL);\n        listDelNode(clients,ln);\n        if (listLength(clients) == 0) {\n            /* Free the list and associated hash entry at all if this was\n             * the latest client. */\n            dictDelete(g_pserver->pubsub_patterns,pattern);\n        }\n    }\n    /* Notify the client */\n    if (notify) addReplyPubsubPatUnsubscribed(c,pattern);\n    decrRefCount(pattern);\n    return retval;\n}\n\n/* Unsubscribe from all the channels. Return the number of channels the\n * client was subscribed to. */\nint pubsubUnsubscribeAllChannels(client *c, int notify) {\n    int count = 0;\n    serverAssert(GlobalLocksAcquired());\n    if (dictSize(c->pubsub_channels) > 0) {\n        dictIterator *di = dictGetSafeIterator(c->pubsub_channels);\n        dictEntry *de;\n\n        while((de = dictNext(di)) != NULL) {\n            robj *channel = (robj*)dictGetKey(de);\n\n            count += pubsubUnsubscribeChannel(c,channel,notify);\n        }\n        dictReleaseIterator(di);\n    }\n    /* We were subscribed to nothing? Still reply to the client. */\n    if (notify && count == 0) addReplyPubsubUnsubscribed(c,NULL);\n    return count;\n}\n\n/* Unsubscribe from all the patterns. Return the number of patterns the\n * client was subscribed from. */\nint pubsubUnsubscribeAllPatterns(client *c, int notify) {\n    serverAssert(GlobalLocksAcquired());\n    listNode *ln;\n    listIter li;\n    int count = 0;\n\n    listRewind(c->pubsub_patterns,&li);\n    while ((ln = listNext(&li)) != NULL) {\n        robj *pattern = (robj*)ln->value;\n\n        count += pubsubUnsubscribePattern(c,pattern,notify);\n    }\n    if (notify && count == 0) addReplyPubsubPatUnsubscribed(c,NULL);\n    return count;\n}\n\n/* Publish a message */\nint pubsubPublishMessage(robj *channel, robj *message) {\n    serverAssert(GlobalLocksAcquired());\n    int receivers = 0;\n    dictEntry *de;\n    dictIterator *di;\n    listNode *ln;\n    listIter li;\n\n    /* Send to clients listening for that channel */\n    de = dictFind(g_pserver->pubsub_channels,channel);\n    if (de) {\n        list *list = reinterpret_cast<::list*>(dictGetVal(de));\n        listNode *ln;\n        listIter li;\n\n        listRewind(list,&li);\n        while ((ln = listNext(&li)) != NULL) {\n            client *c = reinterpret_cast<client*>(ln->value);\n            if (c->flags & CLIENT_CLOSE_ASAP)   // avoid blocking if the write will be ignored\n                continue;\n            if (FCorrectThread(c))\n                fastlock_lock(&c->lock);\n            addReplyPubsubMessage(c,channel,message);\n            if (FCorrectThread(c))\n                fastlock_unlock(&c->lock);\n            receivers++;\n        }\n    }\n    /* Send to clients listening to matching channels */\n    di = dictGetIterator(g_pserver->pubsub_patterns);\n    if (di) {\n        channel = getDecodedObject(channel);\n        while((de = dictNext(di)) != NULL) {\n            robj *pattern = (robj*)dictGetKey(de);\n            list *clients = (list*)dictGetVal(de);\n            if (!stringmatchlen(szFromObj(pattern),\n                                sdslen(szFromObj(pattern)),\n                                szFromObj(channel),\n                                sdslen(szFromObj(channel)),0)) continue;\n\n            listRewind(clients,&li);\n            while ((ln = listNext(&li)) != NULL) {\n                client *c = (client*)listNodeValue(ln);\n                if (c->flags & CLIENT_CLOSE_ASAP)\n                    continue;\n                std::unique_lock<fastlock> l(c->lock, std::defer_lock);\n                if (FCorrectThread(c))\n                    l.lock();\n                addReplyPubsubPatMessage(c,pattern,channel,message);\n                receivers++;\n            }\n        }\n        decrRefCount(channel);\n        dictReleaseIterator(di);\n    }\n    return receivers;\n}\n\n/*-----------------------------------------------------------------------------\n * Pubsub commands implementation\n *----------------------------------------------------------------------------*/\n\n/* SUBSCRIBE channel [channel ...] */\nvoid subscribeCommand(client *c) {\n    int j;\n    serverAssert(GlobalLocksAcquired());\n    if ((c->flags & CLIENT_DENY_BLOCKING) && !(c->flags & CLIENT_MULTI)) {\n        /**\n         * A client that has CLIENT_DENY_BLOCKING flag on\n         * expect a reply per command and so can not execute subscribe.\n         *\n         * Notice that we have a special treatment for multi because of\n         * backword compatibility\n         */\n        addReplyError(c, \"SUBSCRIBE isn't allowed for a DENY BLOCKING client\");\n        return;\n    }\n\n    for (j = 1; j < c->argc; j++)\n        pubsubSubscribeChannel(c,c->argv[j]);\n    c->flags |= CLIENT_PUBSUB;\n}\n\n/* UNSUBSCRIBE [channel [channel ...]] */\nvoid unsubscribeCommand(client *c) {\n    if (c->argc == 1) {\n        pubsubUnsubscribeAllChannels(c,1);\n    } else {\n        int j;\n\n        for (j = 1; j < c->argc; j++)\n            pubsubUnsubscribeChannel(c,c->argv[j],1);\n    }\n    if (clientSubscriptionsCount(c) == 0) c->flags &= ~CLIENT_PUBSUB;\n}\n\n/* PSUBSCRIBE pattern [pattern ...] */\nvoid psubscribeCommand(client *c) {\n    int j;\n    serverAssert(GlobalLocksAcquired());\n    if ((c->flags & CLIENT_DENY_BLOCKING) && !(c->flags & CLIENT_MULTI)) {\n        /**\n         * A client that has CLIENT_DENY_BLOCKING flag on\n         * expect a reply per command and so can not execute subscribe.\n         *\n         * Notice that we have a special treatment for multi because of\n         * backword compatibility\n         */\n        addReplyError(c, \"PSUBSCRIBE isn't allowed for a DENY BLOCKING client\");\n        return;\n    }\n\n    for (j = 1; j < c->argc; j++)\n        pubsubSubscribePattern(c,c->argv[j]);\n    c->flags |= CLIENT_PUBSUB;\n}\n\n/* PUNSUBSCRIBE [pattern [pattern ...]] */\nvoid punsubscribeCommand(client *c) {\n    if (c->argc == 1) {\n        pubsubUnsubscribeAllPatterns(c,1);\n    } else {\n        int j;\n\n        for (j = 1; j < c->argc; j++)\n            pubsubUnsubscribePattern(c,c->argv[j],1);\n    }\n    if (clientSubscriptionsCount(c) == 0) c->flags &= ~CLIENT_PUBSUB;\n}\n\n/* PUBLISH <channel> <message> */\nvoid publishCommand(client *c) {\n    int receivers = pubsubPublishMessage(c->argv[1],c->argv[2]);\n    if (g_pserver->cluster_enabled)\n        clusterPropagatePublish(c->argv[1],c->argv[2]);\n    else\n        forceCommandPropagation(c,PROPAGATE_REPL);\n    addReplyLongLong(c,receivers);\n}\n\n/* PUBSUB command for Pub/Sub introspection. */\nvoid pubsubCommand(client *c) {\n    if (c->argc == 2 && !strcasecmp(szFromObj(c->argv[1]),\"help\")) {\n        const char *help[] = {\n\"CHANNELS [<pattern>]\",\n\"    Return the currently active channels matching a <pattern> (default: '*').\",\n\"NUMPAT\",\n\"    Return number of subscriptions to patterns.\",\n\"NUMSUB [<channel> ...]\",\n\"    Return the number of subscribers for the specified channels, excluding\",\n\"    pattern subscriptions(default: no channels).\",\nNULL\n        };\n        addReplyHelp(c, help);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"channels\") &&\n        (c->argc == 2 || c->argc == 3))\n    {\n        /* PUBSUB CHANNELS [<pattern>] */\n        sds pat = (c->argc == 2) ? NULL : szFromObj(c->argv[2]);\n        dictIterator *di = dictGetIterator(g_pserver->pubsub_channels);\n        dictEntry *de;\n        long mblen = 0;\n        void *replylen;\n\n        replylen = addReplyDeferredLen(c);\n        while((de = dictNext(di)) != NULL) {\n            robj *cobj = (robj*)dictGetKey(de);\n            sds channel = szFromObj(cobj);\n\n            if (!pat || stringmatchlen(pat, sdslen(pat),\n                                       channel, sdslen(channel),0))\n            {\n                addReplyBulk(c,cobj);\n                mblen++;\n            }\n        }\n        dictReleaseIterator(di);\n        setDeferredArrayLen(c,replylen,mblen);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"numsub\") && c->argc >= 2) {\n        /* PUBSUB NUMSUB [Channel_1 ... Channel_N] */\n        int j;\n\n        addReplyArrayLen(c,(c->argc-2)*2);\n        for (j = 2; j < c->argc; j++) {\n            list *l = (list*)dictFetchValue(g_pserver->pubsub_channels,c->argv[j]);\n\n            addReplyBulk(c,c->argv[j]);\n            addReplyLongLong(c,l ? listLength(l) : 0);\n        }\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"numpat\") && c->argc == 2) {\n        /* PUBSUB NUMPAT */\n        addReplyLongLong(c,dictSize(g_pserver->pubsub_patterns));\n    } else {\n        addReplySubcommandSyntaxError(c);\n    }\n}\n"
  },
  {
    "path": "src/quicklist.c",
    "content": "/* quicklist.c - A doubly linked list of ziplists\n *\n * Copyright (c) 2014, Matt Stancliff <matt@genges.com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must start the above copyright notice,\n *     this quicklist of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this quicklist of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <string.h> /* for memcpy */\n#include \"quicklist.h\"\n#include \"zmalloc.h\"\n#include \"config.h\"\n#include \"ziplist.h\"\n#include \"util.h\" /* for ll2string */\n#include \"lzf.h\"\n#include \"redisassert.h\"\n\n#if defined(REDIS_TEST) || defined(REDIS_TEST_VERBOSE)\n#include <stdio.h> /* for printf (debug printing), snprintf (genstr) */\n#endif\n\n#ifndef REDIS_STATIC\n#define REDIS_STATIC static\n#endif\n\n/* Optimization levels for size-based filling.\n * Note that the largest possible limit is 16k, so even if each record takes\n * just one byte, it still won't overflow the 16 bit count field. */\nstatic const size_t optimization_level[] = {4096, 8192, 16384, 32768, 65536};\n\n/* Maximum size in bytes of any multi-element ziplist.\n * Larger values will live in their own isolated ziplists.\n * This is used only if we're limited by record count. when we're limited by\n * size, the maximum limit is bigger, but still safe.\n * 8k is a recommended / default size limit */\n#define SIZE_SAFETY_LIMIT 8192\n\n/* Minimum ziplist size in bytes for attempting compression. */\n#define MIN_COMPRESS_BYTES 48\n\n/* Minimum size reduction in bytes to store compressed quicklistNode data.\n * This also prevents us from storing compression if the compression\n * resulted in a larger size than the original data. */\n#define MIN_COMPRESS_IMPROVE 8\n\n/* If not verbose testing, remove all debug printing. */\n#ifndef REDIS_TEST_VERBOSE\n#define D(...)\n#else\n#define D(...)                                                                 \\\n    do {                                                                       \\\n        printf(\"%s:%s:%d:\\t\", __FILE__, __func__, __LINE__);                   \\\n        printf(__VA_ARGS__);                                                   \\\n        printf(\"\\n\");                                                          \\\n    } while (0)\n#endif\n\n/* Bookmarks forward declarations */\n#define QL_MAX_BM ((1 << QL_BM_BITS)-1)\nquicklistBookmark *_quicklistBookmarkFindByName(quicklist *ql, const char *name);\nquicklistBookmark *_quicklistBookmarkFindByNode(quicklist *ql, quicklistNode *node);\nvoid _quicklistBookmarkDelete(quicklist *ql, quicklistBookmark *bm);\n\n/* Simple way to give quicklistEntry structs default values with one call. */\n#define initEntry(e)                                                           \\\n    do {                                                                       \\\n        (e)->zi = (e)->value = NULL;                                           \\\n        (e)->longval = -123456789;                                             \\\n        (e)->qlist = NULL;                                                 \\\n        (e)->node = NULL;                                                      \\\n        (e)->offset = 123456789;                                               \\\n        (e)->sz = 0;                                                           \\\n    } while (0)\n\n/* Create a new quicklist.\n * Free with quicklistRelease(). */\nquicklist *quicklistCreate(void) {\n    struct quicklist *quicklist;\n\n    quicklist = zmalloc(sizeof(*quicklist), MALLOC_SHARED);\n    quicklist->head = quicklist->tail = NULL;\n    quicklist->len = 0;\n    quicklist->count = 0;\n    quicklist->compress = 0;\n    quicklist->fill = -2;\n    quicklist->bookmark_count = 0;\n    return quicklist;\n}\n\n#define COMPRESS_MAX ((1 << QL_COMP_BITS)-1)\nvoid quicklistSetCompressDepth(quicklist *quicklist, int compress) {\n    if (compress > COMPRESS_MAX) {\n        compress = COMPRESS_MAX;\n    } else if (compress < 0) {\n        compress = 0;\n    }\n    quicklist->compress = compress;\n}\n\n#define FILL_MAX ((1 << (QL_FILL_BITS-1))-1)\nvoid quicklistSetFill(quicklist *quicklist, int fill) {\n    if (fill > FILL_MAX) {\n        fill = FILL_MAX;\n    } else if (fill < -5) {\n        fill = -5;\n    }\n    quicklist->fill = fill;\n}\n\nvoid quicklistSetOptions(quicklist *quicklist, int fill, int depth) {\n    quicklistSetFill(quicklist, fill);\n    quicklistSetCompressDepth(quicklist, depth);\n}\n\n/* Create a new quicklist with some default parameters. */\nquicklist *quicklistNew(int fill, int compress) {\n    quicklist *quicklist = quicklistCreate();\n    quicklistSetOptions(quicklist, fill, compress);\n    return quicklist;\n}\n\nREDIS_STATIC quicklistNode *quicklistCreateNode(void) {\n    quicklistNode *node;\n    node = zmalloc(sizeof(*node), MALLOC_SHARED);\n    node->zl = NULL;\n    node->count = 0;\n    node->sz = 0;\n    node->next = node->prev = NULL;\n    node->encoding = QUICKLIST_NODE_ENCODING_RAW;\n    node->container = QUICKLIST_NODE_CONTAINER_ZIPLIST;\n    node->recompress = 0;\n    return node;\n}\n\n/* Return cached quicklist count */\nunsigned long quicklistCount(const quicklist *ql) { return ql->count; }\n\n/* Free entire quicklist. */\nvoid quicklistRelease(quicklist *quicklist) {\n    unsigned long len;\n    quicklistNode *current, *next;\n\n    current = quicklist->head;\n    len = quicklist->len;\n    while (len--) {\n        next = current->next;\n\n        zfree(current->zl);\n        quicklist->count -= current->count;\n\n        zfree(current);\n\n        quicklist->len--;\n        current = next;\n    }\n    quicklistBookmarksClear(quicklist);\n    zfree(quicklist);\n}\n\n/* Compress the ziplist in 'node' and update encoding details.\n * Returns 1 if ziplist compressed successfully.\n * Returns 0 if compression failed or if ziplist too small to compress. */\nREDIS_STATIC int __quicklistCompressNode(quicklistNode *node) {\n#ifdef REDIS_TEST\n    node->attempted_compress = 1;\n#endif\n\n    /* Don't bother compressing small values */\n    if (node->sz < MIN_COMPRESS_BYTES)\n        return 0;\n\n    quicklistLZF *lzf = zmalloc(sizeof(*lzf) + node->sz, MALLOC_SHARED);\n\n    /* Cancel if compression fails or doesn't compress small enough */\n    if (((lzf->sz = lzf_compress(node->zl, node->sz, lzf->compressed,\n                                 node->sz)) == 0) ||\n        lzf->sz + MIN_COMPRESS_IMPROVE >= node->sz) {\n        /* lzf_compress aborts/rejects compression if value not compressable. */\n        zfree(lzf);\n        return 0;\n    }\n    lzf = zrealloc(lzf, sizeof(*lzf) + lzf->sz, MALLOC_SHARED);\n    zfree(node->zl);\n    node->zl = (unsigned char *)lzf;\n    node->encoding = QUICKLIST_NODE_ENCODING_LZF;\n    node->recompress = 0;\n    return 1;\n}\n\n/* Compress only uncompressed nodes. */\n#define quicklistCompressNode(_node)                                           \\\n    do {                                                                       \\\n        if ((_node) && (_node)->encoding == QUICKLIST_NODE_ENCODING_RAW) {     \\\n            __quicklistCompressNode((_node));                                  \\\n        }                                                                      \\\n    } while (0)\n\n/* Uncompress the ziplist in 'node' and update encoding details.\n * Returns 1 on successful decode, 0 on failure to decode. */\nREDIS_STATIC int __quicklistDecompressNode(quicklistNode *node) {\n#ifdef REDIS_TEST\n    node->attempted_compress = 0;\n#endif\n\n    void *decompressed = zmalloc(node->sz, MALLOC_SHARED);\n    quicklistLZF *lzf = (quicklistLZF *)node->zl;\n    if (lzf_decompress(lzf->compressed, lzf->sz, decompressed, node->sz) == 0) {\n        /* Someone requested decompress, but we can't decompress.  Not good. */\n        zfree(decompressed);\n        return 0;\n    }\n    zfree(lzf);\n    node->zl = decompressed;\n    node->encoding = QUICKLIST_NODE_ENCODING_RAW;\n    return 1;\n}\n\n/* Decompress only compressed nodes. */\n#define quicklistDecompressNode(_node)                                         \\\n    do {                                                                       \\\n        if ((_node) && (_node)->encoding == QUICKLIST_NODE_ENCODING_LZF) {     \\\n            __quicklistDecompressNode((_node));                                \\\n        }                                                                      \\\n    } while (0)\n\n/* Force node to not be immediately re-compresable */\n#define quicklistDecompressNodeForUse(_node)                                   \\\n    do {                                                                       \\\n        if ((_node) && (_node)->encoding == QUICKLIST_NODE_ENCODING_LZF) {     \\\n            __quicklistDecompressNode((_node));                                \\\n            (_node)->recompress = 1;                                           \\\n        }                                                                      \\\n    } while (0)\n\n/* Extract the raw LZF data from this quicklistNode.\n * Pointer to LZF data is assigned to '*data'.\n * Return value is the length of compressed LZF data. */\nsize_t quicklistGetLzf(const quicklistNode *node, void **data) {\n    quicklistLZF *lzf = (quicklistLZF *)node->zl;\n    *data = lzf->compressed;\n    return lzf->sz;\n}\n\n#define quicklistAllowsCompression(_ql) ((_ql)->compress != 0)\n\n/* Force 'quicklist' to meet compression guidelines set by compress depth.\n * The only way to guarantee interior nodes get compressed is to iterate\n * to our \"interior\" compress depth then compress the next node we find.\n * If compress depth is larger than the entire list, we return immediately. */\nREDIS_STATIC void __quicklistCompress(const quicklist *quicklist,\n                                      quicklistNode *node) {\n    /* If length is less than our compress depth (from both sides),\n     * we can't compress anything. */\n    if (!quicklistAllowsCompression(quicklist) ||\n        quicklist->len < (unsigned int)(quicklist->compress * 2))\n        return;\n\n#if 0\n    /* Optimized cases for small depth counts */\n    if (quicklist->compress == 1) {\n        quicklistNode *h = quicklist->head, *t = quicklist->tail;\n        quicklistDecompressNode(h);\n        quicklistDecompressNode(t);\n        if (h != node && t != node)\n            quicklistCompressNode(node);\n        return;\n    } else if (quicklist->compress == 2) {\n        quicklistNode *h = quicklist->head, *hn = h->next, *hnn = hn->next;\n        quicklistNode *t = quicklist->tail, *tp = t->prev, *tpp = tp->prev;\n        quicklistDecompressNode(h);\n        quicklistDecompressNode(hn);\n        quicklistDecompressNode(t);\n        quicklistDecompressNode(tp);\n        if (h != node && hn != node && t != node && tp != node) {\n            quicklistCompressNode(node);\n        }\n        if (hnn != t) {\n            quicklistCompressNode(hnn);\n        }\n        if (tpp != h) {\n            quicklistCompressNode(tpp);\n        }\n        return;\n    }\n#endif\n\n    /* Iterate until we reach compress depth for both sides of the list.a\n     * Note: because we do length checks at the *top* of this function,\n     *       we can skip explicit null checks below. Everything exists. */\n    quicklistNode *forward = quicklist->head;\n    quicklistNode *reverse = quicklist->tail;\n    int depth = 0;\n    int in_depth = 0;\n    while (depth++ < quicklist->compress) {\n        quicklistDecompressNode(forward);\n        quicklistDecompressNode(reverse);\n\n        if (forward == node || reverse == node)\n            in_depth = 1;\n\n        /* We passed into compress depth of opposite side of the quicklist\n         * so there's no need to compress anything and we can exit. */\n        if (forward == reverse || forward->next == reverse)\n            return;\n\n        forward = forward->next;\n        reverse = reverse->prev;\n    }\n\n    if (!in_depth)\n        quicklistCompressNode(node);\n\n    /* At this point, forward and reverse are one node beyond depth */\n    quicklistCompressNode(forward);\n    quicklistCompressNode(reverse);\n}\n\n#define quicklistCompress(_ql, _node)                                          \\\n    do {                                                                       \\\n        if ((_node)->recompress)                                               \\\n            quicklistCompressNode((_node));                                    \\\n        else                                                                   \\\n            __quicklistCompress((_ql), (_node));                               \\\n    } while (0)\n\n/* If we previously used quicklistDecompressNodeForUse(), just recompress. */\n#define quicklistRecompressOnly(_ql, _node)                                    \\\n    do {                                                                       \\\n        if ((_node)->recompress)                                               \\\n            quicklistCompressNode((_node));                                    \\\n    } while (0)\n\n/* Insert 'new_node' after 'old_node' if 'after' is 1.\n * Insert 'new_node' before 'old_node' if 'after' is 0.\n * Note: 'new_node' is *always* uncompressed, so if we assign it to\n *       head or tail, we do not need to uncompress it. */\nREDIS_STATIC void __quicklistInsertNode(quicklist *quicklist,\n                                        quicklistNode *old_node,\n                                        quicklistNode *new_node, int after) {\n    if (after) {\n        new_node->prev = old_node;\n        if (old_node) {\n            new_node->next = old_node->next;\n            if (old_node->next)\n                old_node->next->prev = new_node;\n            old_node->next = new_node;\n        }\n        if (quicklist->tail == old_node)\n            quicklist->tail = new_node;\n    } else {\n        new_node->next = old_node;\n        if (old_node) {\n            new_node->prev = old_node->prev;\n            if (old_node->prev)\n                old_node->prev->next = new_node;\n            old_node->prev = new_node;\n        }\n        if (quicklist->head == old_node)\n            quicklist->head = new_node;\n    }\n    /* If this insert creates the only element so far, initialize head/tail. */\n    if (quicklist->len == 0) {\n        quicklist->head = quicklist->tail = new_node;\n    }\n\n    /* Update len first, so in __quicklistCompress we know exactly len */\n    quicklist->len++;\n\n    if (old_node)\n        quicklistCompress(quicklist, old_node);\n}\n\n/* Wrappers for node inserting around existing node. */\nREDIS_STATIC void _quicklistInsertNodeBefore(quicklist *quicklist,\n                                             quicklistNode *old_node,\n                                             quicklistNode *new_node) {\n    __quicklistInsertNode(quicklist, old_node, new_node, 0);\n}\n\nREDIS_STATIC void _quicklistInsertNodeAfter(quicklist *quicklist,\n                                            quicklistNode *old_node,\n                                            quicklistNode *new_node) {\n    __quicklistInsertNode(quicklist, old_node, new_node, 1);\n}\n\nREDIS_STATIC int\n_quicklistNodeSizeMeetsOptimizationRequirement(const size_t sz,\n                                               const int fill) {\n    if (fill >= 0)\n        return 0;\n\n    size_t offset = (-fill) - 1;\n    if (offset < (sizeof(optimization_level) / sizeof(*optimization_level))) {\n        if (sz <= optimization_level[offset]) {\n            return 1;\n        } else {\n            return 0;\n        }\n    } else {\n        return 0;\n    }\n}\n\n#define sizeMeetsSafetyLimit(sz) ((sz) <= SIZE_SAFETY_LIMIT)\n\nREDIS_STATIC int _quicklistNodeAllowInsert(const quicklistNode *node,\n                                           const int fill, const size_t sz) {\n    if (unlikely(!node))\n        return 0;\n\n    int ziplist_overhead;\n    /* size of previous offset */\n    if (sz < 254)\n        ziplist_overhead = 1;\n    else\n        ziplist_overhead = 5;\n\n    /* size of forward offset */\n    if (sz < 64)\n        ziplist_overhead += 1;\n    else if (likely(sz < 16384))\n        ziplist_overhead += 2;\n    else\n        ziplist_overhead += 5;\n\n    /* new_sz overestimates if 'sz' encodes to an integer type */\n    unsigned int new_sz = node->sz + sz + ziplist_overhead;\n    if (likely(_quicklistNodeSizeMeetsOptimizationRequirement(new_sz, fill)))\n        return 1;\n    /* when we return 1 above we know that the limit is a size limit (which is\n     * safe, see comments next to optimization_level and SIZE_SAFETY_LIMIT) */\n    else if (!sizeMeetsSafetyLimit(new_sz))\n        return 0;\n    else if ((int)node->count < fill)\n        return 1;\n    else\n        return 0;\n}\n\nREDIS_STATIC int _quicklistNodeAllowMerge(const quicklistNode *a,\n                                          const quicklistNode *b,\n                                          const int fill) {\n    if (!a || !b)\n        return 0;\n\n    /* approximate merged ziplist size (- 11 to remove one ziplist\n     * header/trailer) */\n    unsigned int merge_sz = a->sz + b->sz - 11;\n    if (likely(_quicklistNodeSizeMeetsOptimizationRequirement(merge_sz, fill)))\n        return 1;\n    /* when we return 1 above we know that the limit is a size limit (which is\n     * safe, see comments next to optimization_level and SIZE_SAFETY_LIMIT) */\n    else if (!sizeMeetsSafetyLimit(merge_sz))\n        return 0;\n    else if ((int)(a->count + b->count) <= fill)\n        return 1;\n    else\n        return 0;\n}\n\n#define quicklistNodeUpdateSz(node)                                            \\\n    do {                                                                       \\\n        (node)->sz = ziplistBlobLen((node)->zl);                               \\\n    } while (0)\n\n/* Add new entry to head node of quicklist.\n *\n * Returns 0 if used existing head.\n * Returns 1 if new head created. */\nint quicklistPushHead(quicklist *quicklist, void *value, size_t sz) {\n    quicklistNode *orig_head = quicklist->head;\n    assert(sz < UINT32_MAX); /* TODO: add support for quicklist nodes that are sds encoded (not zipped) */\n    if (likely(\n            _quicklistNodeAllowInsert(quicklist->head, quicklist->fill, sz))) {\n        quicklist->head->zl =\n            ziplistPush(quicklist->head->zl, value, sz, ZIPLIST_HEAD);\n        quicklistNodeUpdateSz(quicklist->head);\n    } else {\n        quicklistNode *node = quicklistCreateNode();\n        node->zl = ziplistPush(ziplistNew(), value, sz, ZIPLIST_HEAD);\n\n        quicklistNodeUpdateSz(node);\n        _quicklistInsertNodeBefore(quicklist, quicklist->head, node);\n    }\n    quicklist->count++;\n    quicklist->head->count++;\n    return (orig_head != quicklist->head);\n}\n\n/* Add new entry to tail node of quicklist.\n *\n * Returns 0 if used existing tail.\n * Returns 1 if new tail created. */\nint quicklistPushTail(quicklist *quicklist, void *value, size_t sz) {\n    quicklistNode *orig_tail = quicklist->tail;\n    assert(sz < UINT32_MAX); /* TODO: add support for quicklist nodes that are sds encoded (not zipped) */\n    if (likely(\n            _quicklistNodeAllowInsert(quicklist->tail, quicklist->fill, sz))) {\n        quicklist->tail->zl =\n            ziplistPush(quicklist->tail->zl, value, sz, ZIPLIST_TAIL);\n        quicklistNodeUpdateSz(quicklist->tail);\n    } else {\n        quicklistNode *node = quicklistCreateNode();\n        node->zl = ziplistPush(ziplistNew(), value, sz, ZIPLIST_TAIL);\n\n        quicklistNodeUpdateSz(node);\n        _quicklistInsertNodeAfter(quicklist, quicklist->tail, node);\n    }\n    quicklist->count++;\n    quicklist->tail->count++;\n    return (orig_tail != quicklist->tail);\n}\n\n/* Create new node consisting of a pre-formed ziplist.\n * Used for loading RDBs where entire ziplists have been stored\n * to be retrieved later. */\nvoid quicklistAppendZiplist(quicklist *quicklist, unsigned char *zl) {\n    quicklistNode *node = quicklistCreateNode();\n\n    node->zl = zl;\n    node->count = ziplistLen(node->zl);\n    node->sz = ziplistBlobLen(zl);\n\n    _quicklistInsertNodeAfter(quicklist, quicklist->tail, node);\n    quicklist->count += node->count;\n}\n\n/* Append all values of ziplist 'zl' individually into 'quicklist'.\n *\n * This allows us to restore old RDB ziplists into new quicklists\n * with smaller ziplist sizes than the saved RDB ziplist.\n *\n * Returns 'quicklist' argument. Frees passed-in ziplist 'zl' */\nquicklist *quicklistAppendValuesFromZiplist(quicklist *quicklist,\n                                            unsigned char *zl) {\n    unsigned char *value;\n    unsigned int sz;\n    long long longval;\n    char longstr[32] = {0};\n\n    unsigned char *p = ziplistIndex(zl, 0);\n    while (ziplistGet(p, &value, &sz, &longval)) {\n        if (!value) {\n            /* Write the longval as a string so we can re-add it */\n            sz = ll2string(longstr, sizeof(longstr), longval);\n            value = (unsigned char *)longstr;\n        }\n        quicklistPushTail(quicklist, value, sz);\n        p = ziplistNext(zl, p);\n    }\n    zfree(zl);\n    return quicklist;\n}\n\n/* Create new (potentially multi-node) quicklist from a single existing ziplist.\n *\n * Returns new quicklist.  Frees passed-in ziplist 'zl'. */\nquicklist *quicklistCreateFromZiplist(int fill, int compress,\n                                      unsigned char *zl) {\n    return quicklistAppendValuesFromZiplist(quicklistNew(fill, compress), zl);\n}\n\n#define quicklistDeleteIfEmpty(ql, n)                                          \\\n    do {                                                                       \\\n        if ((n)->count == 0) {                                                 \\\n            __quicklistDelNode((ql), (n));                                     \\\n            (n) = NULL;                                                        \\\n        }                                                                      \\\n    } while (0)\n\nREDIS_STATIC void __quicklistDelNode(quicklist *quicklist,\n                                     quicklistNode *node) {\n    /* Update the bookmark if any */\n    quicklistBookmark *bm = _quicklistBookmarkFindByNode(quicklist, node);\n    if (bm) {\n        bm->node = node->next;\n        /* if the bookmark was to the last node, delete it. */\n        if (!bm->node)\n            _quicklistBookmarkDelete(quicklist, bm);\n    }\n\n    if (node->next)\n        node->next->prev = node->prev;\n    if (node->prev)\n        node->prev->next = node->next;\n\n    if (node == quicklist->tail) {\n        quicklist->tail = node->prev;\n    }\n\n    if (node == quicklist->head) {\n        quicklist->head = node->next;\n    }\n\n    /* Update len first, so in __quicklistCompress we know exactly len */\n    quicklist->len--;\n    quicklist->count -= node->count;\n\n    /* If we deleted a node within our compress depth, we\n     * now have compressed nodes needing to be decompressed. */\n    __quicklistCompress(quicklist, NULL);\n\n    zfree(node->zl);\n    zfree(node);\n}\n\n/* Delete one entry from list given the node for the entry and a pointer\n * to the entry in the node.\n *\n * Note: quicklistDelIndex() *requires* uncompressed nodes because you\n *       already had to get *p from an uncompressed node somewhere.\n *\n * Returns 1 if the entire node was deleted, 0 if node still exists.\n * Also updates in/out param 'p' with the next offset in the ziplist. */\nREDIS_STATIC int quicklistDelIndex(quicklist *quicklist, quicklistNode *node,\n                                   unsigned char **p) {\n    int gone = 0;\n\n    node->zl = ziplistDelete(node->zl, p);\n    node->count--;\n    if (node->count == 0) {\n        gone = 1;\n        __quicklistDelNode(quicklist, node);\n    } else {\n        quicklistNodeUpdateSz(node);\n    }\n    quicklist->count--;\n    /* If we deleted the node, the original node is no longer valid */\n    return gone ? 1 : 0;\n}\n\n/* Delete one element represented by 'entry'\n *\n * 'entry' stores enough metadata to delete the proper position in\n * the correct ziplist in the correct quicklist node. */\nvoid quicklistDelEntry(quicklistIter *iter, quicklistEntry *entry) {\n    quicklistNode *prev = entry->node->prev;\n    quicklistNode *next = entry->node->next;\n    int deleted_node = quicklistDelIndex((quicklist *)entry->qlist,\n                                         entry->node, &entry->zi);\n\n    /* after delete, the zi is now invalid for any future usage. */\n    iter->zi = NULL;\n\n    /* If current node is deleted, we must update iterator node and offset. */\n    if (deleted_node) {\n        if (iter->direction == AL_START_HEAD) {\n            iter->current = next;\n            iter->offset = 0;\n        } else if (iter->direction == AL_START_TAIL) {\n            iter->current = prev;\n            iter->offset = -1;\n        }\n    }\n    /* else if (!deleted_node), no changes needed.\n     * we already reset iter->zi above, and the existing iter->offset\n     * doesn't move again because:\n     *   - [1, 2, 3] => delete offset 1 => [1, 3]: next element still offset 1\n     *   - [1, 2, 3] => delete offset 0 => [2, 3]: next element still offset 0\n     *  if we deleted the last element at offet N and now\n     *  length of this ziplist is N-1, the next call into\n     *  quicklistNext() will jump to the next node. */\n}\n\n/* Replace quicklist entry at offset 'index' by 'data' with length 'sz'.\n *\n * Returns 1 if replace happened.\n * Returns 0 if replace failed and no changes happened. */\nint quicklistReplaceAtIndex(quicklist *quicklist, long index, void *data,\n                            int sz) {\n    quicklistEntry entry;\n    if (likely(quicklistIndex(quicklist, index, &entry))) {\n        /* quicklistIndex provides an uncompressed node */\n        entry.node->zl = ziplistReplace(entry.node->zl, entry.zi, data, sz);\n        quicklistNodeUpdateSz(entry.node);\n        quicklistCompress(quicklist, entry.node);\n        return 1;\n    } else {\n        return 0;\n    }\n}\n\n/* Given two nodes, try to merge their ziplists.\n *\n * This helps us not have a quicklist with 3 element ziplists if\n * our fill factor can handle much higher levels.\n *\n * Note: 'a' must be to the LEFT of 'b'.\n *\n * After calling this function, both 'a' and 'b' should be considered\n * unusable.  The return value from this function must be used\n * instead of re-using any of the quicklistNode input arguments.\n *\n * Returns the input node picked to merge against or NULL if\n * merging was not possible. */\nREDIS_STATIC quicklistNode *_quicklistZiplistMerge(quicklist *quicklist,\n                                                   quicklistNode *a,\n                                                   quicklistNode *b) {\n    D(\"Requested merge (a,b) (%u, %u)\", a->count, b->count);\n\n    quicklistDecompressNode(a);\n    quicklistDecompressNode(b);\n    if ((ziplistMerge(&a->zl, &b->zl))) {\n        /* We merged ziplists! Now remove the unused quicklistNode. */\n        quicklistNode *keep = NULL, *nokeep = NULL;\n        if (!a->zl) {\n            nokeep = a;\n            keep = b;\n        } else if (!b->zl) {\n            nokeep = b;\n            keep = a;\n        }\n        keep->count = ziplistLen(keep->zl);\n        quicklistNodeUpdateSz(keep);\n\n        nokeep->count = 0;\n        __quicklistDelNode(quicklist, nokeep);\n        quicklistCompress(quicklist, keep);\n        return keep;\n    } else {\n        /* else, the merge returned NULL and nothing changed. */\n        return NULL;\n    }\n}\n\n/* Attempt to merge ziplists within two nodes on either side of 'center'.\n *\n * We attempt to merge:\n *   - (center->prev->prev, center->prev)\n *   - (center->next, center->next->next)\n *   - (center->prev, center)\n *   - (center, center->next)\n */\nREDIS_STATIC void _quicklistMergeNodes(quicklist *quicklist,\n                                       quicklistNode *center) {\n    int fill = quicklist->fill;\n    quicklistNode *prev, *prev_prev, *next, *next_next, *target;\n    prev = prev_prev = next = next_next = target = NULL;\n\n    if (center->prev) {\n        prev = center->prev;\n        if (center->prev->prev)\n            prev_prev = center->prev->prev;\n    }\n\n    if (center->next) {\n        next = center->next;\n        if (center->next->next)\n            next_next = center->next->next;\n    }\n\n    /* Try to merge prev_prev and prev */\n    if (_quicklistNodeAllowMerge(prev, prev_prev, fill)) {\n        _quicklistZiplistMerge(quicklist, prev_prev, prev);\n        prev_prev = prev = NULL; /* they could have moved, invalidate them. */\n    }\n\n    /* Try to merge next and next_next */\n    if (_quicklistNodeAllowMerge(next, next_next, fill)) {\n        _quicklistZiplistMerge(quicklist, next, next_next);\n        next = next_next = NULL; /* they could have moved, invalidate them. */\n    }\n\n    /* Try to merge center node and previous node */\n    if (_quicklistNodeAllowMerge(center, center->prev, fill)) {\n        target = _quicklistZiplistMerge(quicklist, center->prev, center);\n        center = NULL; /* center could have been deleted, invalidate it. */\n    } else {\n        /* else, we didn't merge here, but target needs to be valid below. */\n        target = center;\n    }\n\n    /* Use result of center merge (or original) to merge with next node. */\n    if (_quicklistNodeAllowMerge(target, target->next, fill)) {\n        _quicklistZiplistMerge(quicklist, target, target->next);\n    }\n}\n\n/* Split 'node' into two parts, parameterized by 'offset' and 'after'.\n *\n * The 'after' argument controls which quicklistNode gets returned.\n * If 'after'==1, returned node has elements after 'offset'.\n *                input node keeps elements up to 'offset', including 'offset'.\n * If 'after'==0, returned node has elements up to 'offset'.\n *                input node keeps elements after 'offset', including 'offset'.\n *\n * Or in other words:\n * If 'after'==1, returned node will have elements after 'offset'.\n *                The returned node will have elements [OFFSET+1, END].\n *                The input node keeps elements [0, OFFSET].\n * If 'after'==0, returned node will keep elements up to but not including 'offset'.\n *                The returned node will have elements [0, OFFSET-1].\n *                The input node keeps elements [OFFSET, END].\n *\n * The input node keeps all elements not taken by the returned node.\n *\n * Returns newly created node or NULL if split not possible. */\nREDIS_STATIC quicklistNode *_quicklistSplitNode(quicklistNode *node, int offset,\n                                                int after) {\n    size_t zl_sz = node->sz;\n\n    quicklistNode *new_node = quicklistCreateNode();\n    new_node->zl = zmalloc(zl_sz, MALLOC_SHARED);\n\n    /* Copy original ziplist so we can split it */\n    memcpy(new_node->zl, node->zl, zl_sz);\n\n    /* Ranges to be trimmed: -1 here means \"continue deleting until the list ends\" */\n    int orig_start = after ? offset + 1 : 0;\n    int orig_extent = after ? -1 : offset;\n    int new_start = after ? 0 : offset;\n    int new_extent = after ? offset + 1 : -1;\n\n    D(\"After %d (%d); ranges: [%d, %d], [%d, %d]\", after, offset, orig_start,\n      orig_extent, new_start, new_extent);\n\n    node->zl = ziplistDeleteRange(node->zl, orig_start, orig_extent);\n    node->count = ziplistLen(node->zl);\n    quicklistNodeUpdateSz(node);\n\n    new_node->zl = ziplistDeleteRange(new_node->zl, new_start, new_extent);\n    new_node->count = ziplistLen(new_node->zl);\n    quicklistNodeUpdateSz(new_node);\n\n    D(\"After split lengths: orig (%d), new (%d)\", node->count, new_node->count);\n    return new_node;\n}\n\n/* Insert a new entry before or after existing entry 'entry'.\n *\n * If after==1, the new value is inserted after 'entry', otherwise\n * the new value is inserted before 'entry'. */\nREDIS_STATIC void _quicklistInsert(quicklist *quicklist, quicklistEntry *entry,\n                                   void *value, const size_t sz, int after) {\n    int full = 0, at_tail = 0, at_head = 0, full_next = 0, full_prev = 0;\n    int fill = quicklist->fill;\n    quicklistNode *node = entry->node;\n    quicklistNode *new_node = NULL;\n    assert(sz < UINT32_MAX); /* TODO: add support for quicklist nodes that are sds encoded (not zipped) */\n\n    if (!node) {\n        /* we have no reference node, so let's create only node in the list */\n        D(\"No node given!\");\n        new_node = quicklistCreateNode();\n        new_node->zl = ziplistPush(ziplistNew(), value, sz, ZIPLIST_HEAD);\n        __quicklistInsertNode(quicklist, NULL, new_node, after);\n        new_node->count++;\n        quicklist->count++;\n        return;\n    }\n\n    /* Populate accounting flags for easier boolean checks later */\n    if (!_quicklistNodeAllowInsert(node, fill, sz)) {\n        D(\"Current node is full with count %d with requested fill %lu\",\n          node->count, fill);\n        full = 1;\n    }\n\n    if (after && (entry->offset == node->count)) {\n        D(\"At Tail of current ziplist\");\n        at_tail = 1;\n        if (!_quicklistNodeAllowInsert(node->next, fill, sz)) {\n            D(\"Next node is full too.\");\n            full_next = 1;\n        }\n    }\n\n    if (!after && (entry->offset == 0)) {\n        D(\"At Head\");\n        at_head = 1;\n        if (!_quicklistNodeAllowInsert(node->prev, fill, sz)) {\n            D(\"Prev node is full too.\");\n            full_prev = 1;\n        }\n    }\n\n    /* Now determine where and how to insert the new element */\n    if (!full && after) {\n        D(\"Not full, inserting after current position.\");\n        quicklistDecompressNodeForUse(node);\n        unsigned char *next = ziplistNext(node->zl, entry->zi);\n        if (next == NULL) {\n            node->zl = ziplistPush(node->zl, value, sz, ZIPLIST_TAIL);\n        } else {\n            node->zl = ziplistInsert(node->zl, next, value, sz);\n        }\n        node->count++;\n        quicklistNodeUpdateSz(node);\n        quicklistRecompressOnly(quicklist, node);\n    } else if (!full && !after) {\n        D(\"Not full, inserting before current position.\");\n        quicklistDecompressNodeForUse(node);\n        node->zl = ziplistInsert(node->zl, entry->zi, value, sz);\n        node->count++;\n        quicklistNodeUpdateSz(node);\n        quicklistRecompressOnly(quicklist, node);\n    } else if (full && at_tail && node->next && !full_next && after) {\n        /* If we are: at tail, next has free space, and inserting after:\n         *   - insert entry at head of next node. */\n        D(\"Full and tail, but next isn't full; inserting next node head\");\n        new_node = node->next;\n        quicklistDecompressNodeForUse(new_node);\n        new_node->zl = ziplistPush(new_node->zl, value, sz, ZIPLIST_HEAD);\n        new_node->count++;\n        quicklistNodeUpdateSz(new_node);\n        quicklistRecompressOnly(quicklist, new_node);\n    } else if (full && at_head && node->prev && !full_prev && !after) {\n        /* If we are: at head, previous has free space, and inserting before:\n         *   - insert entry at tail of previous node. */\n        D(\"Full and head, but prev isn't full, inserting prev node tail\");\n        new_node = node->prev;\n        quicklistDecompressNodeForUse(new_node);\n        new_node->zl = ziplistPush(new_node->zl, value, sz, ZIPLIST_TAIL);\n        new_node->count++;\n        quicklistNodeUpdateSz(new_node);\n        quicklistRecompressOnly(quicklist, new_node);\n    } else if (full && ((at_tail && node->next && full_next && after) ||\n                        (at_head && node->prev && full_prev && !after))) {\n        /* If we are: full, and our prev/next is full, then:\n         *   - create new node and attach to quicklist */\n        D(\"\\tprovisioning new node...\");\n        new_node = quicklistCreateNode();\n        new_node->zl = ziplistPush(ziplistNew(), value, sz, ZIPLIST_HEAD);\n        new_node->count++;\n        quicklistNodeUpdateSz(new_node);\n        __quicklistInsertNode(quicklist, node, new_node, after);\n    } else if (full) {\n        /* else, node is full we need to split it. */\n        /* covers both after and !after cases */\n        D(\"\\tsplitting node...\");\n        quicklistDecompressNodeForUse(node);\n        new_node = _quicklistSplitNode(node, entry->offset, after);\n        new_node->zl = ziplistPush(new_node->zl, value, sz,\n                                   after ? ZIPLIST_HEAD : ZIPLIST_TAIL);\n        new_node->count++;\n        quicklistNodeUpdateSz(new_node);\n        __quicklistInsertNode(quicklist, node, new_node, after);\n        _quicklistMergeNodes(quicklist, node);\n    }\n\n    quicklist->count++;\n}\n\nvoid quicklistInsertBefore(quicklist *quicklist, quicklistEntry *entry,\n                           void *value, const size_t sz) {\n    _quicklistInsert(quicklist, entry, value, sz, 0);\n}\n\nvoid quicklistInsertAfter(quicklist *quicklist, quicklistEntry *entry,\n                          void *value, const size_t sz) {\n    _quicklistInsert(quicklist, entry, value, sz, 1);\n}\n\n/* Delete a range of elements from the quicklist.\n *\n * elements may span across multiple quicklistNodes, so we\n * have to be careful about tracking where we start and end.\n *\n * Returns 1 if entries were deleted, 0 if nothing was deleted. */\nint quicklistDelRange(quicklist *quicklist, const long start,\n                      const long count) {\n    if (count <= 0)\n        return 0;\n\n    unsigned long extent = count; /* range is inclusive of start position */\n\n    if (start >= 0 && extent > (quicklist->count - start)) {\n        /* if requesting delete more elements than exist, limit to list size. */\n        extent = quicklist->count - start;\n    } else if (start < 0 && extent > (unsigned long)(-start)) {\n        /* else, if at negative offset, limit max size to rest of list. */\n        extent = -start; /* c.f. LREM -29 29; just delete until end. */\n    }\n\n    quicklistEntry entry;\n    if (!quicklistIndex(quicklist, start, &entry))\n        return 0;\n\n    D(\"Quicklist delete request for start %ld, count %ld, extent: %ld\", start,\n      count, extent);\n    quicklistNode *node = entry.node;\n\n    /* iterate over next nodes until everything is deleted. */\n    while (extent) {\n        quicklistNode *next = node->next;\n\n        unsigned long del;\n        int delete_entire_node = 0;\n        if (entry.offset == 0 && extent >= node->count) {\n            /* If we are deleting more than the count of this node, we\n             * can just delete the entire node without ziplist math. */\n            delete_entire_node = 1;\n            del = node->count;\n        } else if (entry.offset >= 0 && extent + entry.offset >= node->count) {\n            /* If deleting more nodes after this one, calculate delete based\n             * on size of current node. */\n            del = node->count - entry.offset;\n        } else if (entry.offset < 0) {\n            /* If offset is negative, we are in the first run of this loop\n             * and we are deleting the entire range\n             * from this start offset to end of list.  Since the Negative\n             * offset is the number of elements until the tail of the list,\n             * just use it directly as the deletion count. */\n            del = -entry.offset;\n\n            /* If the positive offset is greater than the remaining extent,\n             * we only delete the remaining extent, not the entire offset.\n             */\n            if (del > extent)\n                del = extent;\n        } else {\n            /* else, we are deleting less than the extent of this node, so\n             * use extent directly. */\n            del = extent;\n        }\n\n        D(\"[%ld]: asking to del: %ld because offset: %d; (ENTIRE NODE: %d), \"\n          \"node count: %u\",\n          extent, del, entry.offset, delete_entire_node, node->count);\n\n        if (delete_entire_node) {\n            __quicklistDelNode(quicklist, node);\n        } else {\n            quicklistDecompressNodeForUse(node);\n            node->zl = ziplistDeleteRange(node->zl, entry.offset, del);\n            quicklistNodeUpdateSz(node);\n            node->count -= del;\n            quicklist->count -= del;\n            quicklistDeleteIfEmpty(quicklist, node);\n            if (node)\n                quicklistRecompressOnly(quicklist, node);\n        }\n\n        extent -= del;\n\n        node = next;\n\n        entry.offset = 0;\n    }\n    return 1;\n}\n\n/* Passthrough to ziplistCompare() */\nint quicklistCompare(unsigned char *p1, unsigned char *p2, int p2_len) {\n    return ziplistCompare(p1, p2, p2_len);\n}\n\n/* Returns a quicklist iterator 'iter'. After the initialization every\n * call to quicklistNext() will return the next element of the quicklist. */\nquicklistIter *quicklistGetIterator(const quicklist *quicklist, int direction) {\n    quicklistIter *iter;\n\n    iter = zmalloc(sizeof(*iter), MALLOC_LOCAL);\n\n    if (direction == AL_START_HEAD) {\n        iter->current = quicklist->head;\n        iter->offset = 0;\n    } else if (direction == AL_START_TAIL) {\n        iter->current = quicklist->tail;\n        iter->offset = -1;\n    }\n\n    iter->direction = direction;\n    iter->qlist = quicklist;\n\n    iter->zi = NULL;\n\n    return iter;\n}\n\n/* Initialize an iterator at a specific offset 'idx' and make the iterator\n * return nodes in 'direction' direction. */\nquicklistIter *quicklistGetIteratorAtIdx(const quicklist *quicklist,\n                                         const int direction,\n                                         const long long idx) {\n    quicklistEntry entry;\n\n    if (quicklistIndex(quicklist, idx, &entry)) {\n        quicklistIter *base = quicklistGetIterator(quicklist, direction);\n        base->zi = NULL;\n        base->current = entry.node;\n        base->offset = entry.offset;\n        return base;\n    } else {\n        return NULL;\n    }\n}\n\n/* Release iterator.\n * If we still have a valid current node, then re-encode current node. */\nvoid quicklistReleaseIterator(quicklistIter *iter) {\n    if (iter->current)\n        quicklistCompress(iter->qlist, iter->current);\n\n    zfree(iter);\n}\n\n/* Get next element in iterator.\n *\n * Note: You must NOT insert into the list while iterating over it.\n * You *may* delete from the list while iterating using the\n * quicklistDelEntry() function.\n * If you insert into the quicklist while iterating, you should\n * re-create the iterator after your addition.\n *\n * iter = quicklistGetIterator(quicklist,<direction>);\n * quicklistEntry entry;\n * while (quicklistNext(iter, &entry)) {\n *     if (entry.value)\n *          [[ use entry.value with entry.sz ]]\n *     else\n *          [[ use entry.longval ]]\n * }\n *\n * Populates 'entry' with values for this iteration.\n * Returns 0 when iteration is complete or if iteration not possible.\n * If return value is 0, the contents of 'entry' are not valid.\n */\nint quicklistNext(quicklistIter *iter, quicklistEntry *entry) {\n    initEntry(entry);\n\n    if (!iter) {\n        D(\"Returning because no iter!\");\n        return 0;\n    }\n\n    entry->qlist = iter->qlist;\n    entry->node = iter->current;\n\n    if (!iter->current) {\n        D(\"Returning because current node is NULL\")\n        return 0;\n    }\n\n    unsigned char *(*nextFn)(unsigned char *, unsigned char *) = NULL;\n    int offset_update = 0;\n\n    if (!iter->zi) {\n        /* If !zi, use current index. */\n        quicklistDecompressNodeForUse(iter->current);\n        iter->zi = ziplistIndex(iter->current->zl, iter->offset);\n    } else {\n        /* else, use existing iterator offset and get prev/next as necessary. */\n        if (iter->direction == AL_START_HEAD) {\n            nextFn = ziplistNext;\n            offset_update = 1;\n        } else if (iter->direction == AL_START_TAIL) {\n            nextFn = ziplistPrev;\n            offset_update = -1;\n        }\n        iter->zi = nextFn(iter->current->zl, iter->zi);\n        iter->offset += offset_update;\n    }\n\n    entry->zi = iter->zi;\n    entry->offset = iter->offset;\n\n    if (iter->zi) {\n        /* Populate value from existing ziplist position */\n        ziplistGet(entry->zi, &entry->value, &entry->sz, &entry->longval);\n        return 1;\n    } else {\n        /* We ran out of ziplist entries.\n         * Pick next node, update offset, then re-run retrieval. */\n        quicklistCompress(iter->qlist, iter->current);\n        if (iter->direction == AL_START_HEAD) {\n            /* Forward traversal */\n            D(\"Jumping to start of next node\");\n            iter->current = iter->current->next;\n            iter->offset = 0;\n        } else if (iter->direction == AL_START_TAIL) {\n            /* Reverse traversal */\n            D(\"Jumping to end of previous node\");\n            iter->current = iter->current->prev;\n            iter->offset = -1;\n        }\n        iter->zi = NULL;\n        return quicklistNext(iter, entry);\n    }\n}\n\n/* Duplicate the quicklist.\n * On success a copy of the original quicklist is returned.\n *\n * The original quicklist both on success or error is never modified.\n *\n * Returns newly allocated quicklist. */\nquicklist *quicklistDup(quicklist *orig) {\n    quicklist *copy;\n\n    copy = quicklistNew(orig->fill, orig->compress);\n\n    for (quicklistNode *current = orig->head; current;\n         current = current->next) {\n        quicklistNode *node = quicklistCreateNode();\n\n        if (current->encoding == QUICKLIST_NODE_ENCODING_LZF) {\n            quicklistLZF *lzf = (quicklistLZF *)current->zl;\n            size_t lzf_sz = sizeof(*lzf) + lzf->sz;\n            node->zl = zmalloc(lzf_sz, MALLOC_SHARED);\n            memcpy(node->zl, current->zl, lzf_sz);\n        } else if (current->encoding == QUICKLIST_NODE_ENCODING_RAW) {\n            node->zl = zmalloc(current->sz, MALLOC_SHARED);\n            memcpy(node->zl, current->zl, current->sz);\n        }\n\n        node->count = current->count;\n        copy->count += node->count;\n        node->sz = current->sz;\n        node->encoding = current->encoding;\n\n        _quicklistInsertNodeAfter(copy, copy->tail, node);\n    }\n\n    /* copy->count must equal orig->count here */\n    return copy;\n}\n\n/* Populate 'entry' with the element at the specified zero-based index\n * where 0 is the head, 1 is the element next to head\n * and so on. Negative integers are used in order to count\n * from the tail, -1 is the last element, -2 the penultimate\n * and so on. If the index is out of range 0 is returned.\n *\n * Returns 1 if element found\n * Returns 0 if element not found */\nint quicklistIndex(const quicklist *quicklist, const long long idx,\n                   quicklistEntry *entry) {\n    quicklistNode *n;\n    unsigned long long accum = 0;\n    unsigned long long index;\n    int forward = idx < 0 ? 0 : 1; /* < 0 -> reverse, 0+ -> forward */\n\n    initEntry(entry);\n    entry->qlist = quicklist;\n\n    if (!forward) {\n        index = (-idx) - 1;\n        n = quicklist->tail;\n    } else {\n        index = idx;\n        n = quicklist->head;\n    }\n\n    if (index >= quicklist->count)\n        return 0;\n\n    while (likely(n)) {\n        if ((accum + n->count) > index) {\n            break;\n        } else {\n            D(\"Skipping over (%p) %u at accum %lld\", (void *)n, n->count,\n              accum);\n            accum += n->count;\n            n = forward ? n->next : n->prev;\n        }\n    }\n\n    if (!n)\n        return 0;\n\n    D(\"Found node: %p at accum %llu, idx %llu, sub+ %llu, sub- %llu\", (void *)n,\n      accum, index, index - accum, (-index) - 1 + accum);\n\n    entry->node = n;\n    if (forward) {\n        /* forward = normal head-to-tail offset. */\n        entry->offset = index - accum;\n    } else {\n        /* reverse = need negative offset for tail-to-head, so undo\n         * the result of the original if (index < 0) above. */\n        entry->offset = (-index) - 1 + accum;\n    }\n\n    quicklistDecompressNodeForUse(entry->node);\n    entry->zi = ziplistIndex(entry->node->zl, entry->offset);\n    if (!ziplistGet(entry->zi, &entry->value, &entry->sz, &entry->longval))\n        assert(0); /* This can happen on corrupt ziplist with fake entry count. */\n    /* The caller will use our result, so we don't re-compress here.\n     * The caller can recompress or delete the node as needed. */\n    return 1;\n}\n\n/* Rotate quicklist by moving the tail element to the head. */\nvoid quicklistRotate(quicklist *quicklist) {\n    if (quicklist->count <= 1)\n        return;\n\n    /* First, get the tail entry */\n    unsigned char *p = ziplistIndex(quicklist->tail->zl, -1);\n    unsigned char *value, *tmp;\n    long long longval;\n    unsigned int sz;\n    char longstr[32] = {0};\n    ziplistGet(p, &tmp, &sz, &longval);\n\n    /* If value found is NULL, then ziplistGet populated longval instead */\n    if (!tmp) {\n        /* Write the longval as a string so we can re-add it */\n        sz = ll2string(longstr, sizeof(longstr), longval);\n        value = (unsigned char *)longstr;\n    } else if (quicklist->len == 1) {\n        /* Copy buffer since there could be a memory overlap when move\n         * entity from tail to head in the same ziplist. */\n        value = zmalloc(sz, MALLOC_SHARED);\n        memcpy(value, tmp, sz);\n    } else {\n        value = tmp;\n    }\n\n    /* Add tail entry to head (must happen before tail is deleted). */\n    quicklistPushHead(quicklist, value, sz);\n\n    /* If quicklist has only one node, the head ziplist is also the\n     * tail ziplist and PushHead() could have reallocated our single ziplist,\n     * which would make our pre-existing 'p' unusable. */\n    if (quicklist->len == 1) {\n        p = ziplistIndex(quicklist->tail->zl, -1);\n    }\n\n    /* Remove tail entry. */\n    quicklistDelIndex(quicklist, quicklist->tail, &p);\n    if (value != (unsigned char*)longstr && value != tmp)\n        zfree(value);\n}\n\n/* pop from quicklist and return result in 'data' ptr.  Value of 'data'\n * is the return value of 'saver' function pointer if the data is NOT a number.\n *\n * If the quicklist element is a long long, then the return value is returned in\n * 'sval'.\n *\n * Return value of 0 means no elements available.\n * Return value of 1 means check 'data' and 'sval' for values.\n * If 'data' is set, use 'data' and 'sz'.  Otherwise, use 'sval'. */\nint quicklistPopCustom(quicklist *quicklist, int where, unsigned char **data,\n                       unsigned int *sz, long long *sval,\n                       void *(*saver)(unsigned char *data, unsigned int sz)) {\n    unsigned char *p;\n    unsigned char *vstr;\n    unsigned int vlen;\n    long long vlong;\n    int pos = (where == QUICKLIST_HEAD) ? 0 : -1;\n\n    if (quicklist->count == 0)\n        return 0;\n\n    if (data)\n        *data = NULL;\n    if (sz)\n        *sz = 0;\n    if (sval)\n        *sval = -123456789;\n\n    quicklistNode *node;\n    if (where == QUICKLIST_HEAD && quicklist->head) {\n        node = quicklist->head;\n    } else if (where == QUICKLIST_TAIL && quicklist->tail) {\n        node = quicklist->tail;\n    } else {\n        return 0;\n    }\n\n    p = ziplistIndex(node->zl, pos);\n    if (ziplistGet(p, &vstr, &vlen, &vlong)) {\n        if (vstr) {\n            if (data)\n                *data = saver(vstr, vlen);\n            if (sz)\n                *sz = vlen;\n        } else {\n            if (data)\n                *data = NULL;\n            if (sval)\n                *sval = vlong;\n        }\n        quicklistDelIndex(quicklist, node, &p);\n        return 1;\n    }\n    return 0;\n}\n\n/* Return a malloc'd copy of data passed in */\nREDIS_STATIC void *_quicklistSaver(unsigned char *data, unsigned int sz) {\n    unsigned char *vstr;\n    if (data) {\n        vstr = zmalloc(sz, MALLOC_SHARED);\n        memcpy(vstr, data, sz);\n        return vstr;\n    }\n    return NULL;\n}\n\n/* Default pop function\n *\n * Returns malloc'd value from quicklist */\nint quicklistPop(quicklist *quicklist, int where, unsigned char **data,\n                 unsigned int *sz, long long *slong) {\n    unsigned char *vstr;\n    unsigned int vlen;\n    long long vlong;\n    if (quicklist->count == 0)\n        return 0;\n    int ret = quicklistPopCustom(quicklist, where, &vstr, &vlen, &vlong,\n                                 _quicklistSaver);\n    if (data)\n        *data = vstr;\n    if (slong)\n        *slong = vlong;\n    if (sz)\n        *sz = vlen;\n    return ret;\n}\n\n/* Wrapper to allow argument-based switching between HEAD/TAIL pop */\nvoid quicklistPush(quicklist *quicklist, void *value, const size_t sz,\n                   int where) {\n    if (where == QUICKLIST_HEAD) {\n        quicklistPushHead(quicklist, value, sz);\n    } else if (where == QUICKLIST_TAIL) {\n        quicklistPushTail(quicklist, value, sz);\n    }\n}\n\n/* Create or update a bookmark in the list which will be updated to the next node\n * automatically when the one referenced gets deleted.\n * Returns 1 on success (creation of new bookmark or override of an existing one).\n * Returns 0 on failure (reached the maximum supported number of bookmarks).\n * NOTE: use short simple names, so that string compare on find is quick.\n * NOTE: bookmark creation may re-allocate the quicklist, so the input pointer\n         may change and it's the caller responsibilty to update the reference.\n */\nint quicklistBookmarkCreate(quicklist **ql_ref, const char *name, quicklistNode *node) {\n    quicklist *ql = *ql_ref;\n    if (ql->bookmark_count >= QL_MAX_BM)\n        return 0;\n    quicklistBookmark *bm = _quicklistBookmarkFindByName(ql, name);\n    if (bm) {\n        bm->node = node;\n        return 1;\n    }\n    ql = (quicklist*)zrealloc(ql, sizeof(quicklist) + (ql->bookmark_count+1) * sizeof(quicklistBookmark), MALLOC_SHARED);\n    *ql_ref = ql;\n    ql->bookmarks[ql->bookmark_count].node = node;\n    ql->bookmarks[ql->bookmark_count].name = zstrdup(name);\n    ql->bookmark_count++;\n    return 1;\n}\n\n/* Find the quicklist node referenced by a named bookmark.\n * When the bookmarked node is deleted the bookmark is updated to the next node,\n * and if that's the last node, the bookmark is deleted (so find returns NULL). */\nquicklistNode *quicklistBookmarkFind(quicklist *ql, const char *name) {\n    quicklistBookmark *bm = _quicklistBookmarkFindByName(ql, name);\n    if (!bm) return NULL;\n    return bm->node;\n}\n\n/* Delete a named bookmark.\n * returns 0 if bookmark was not found, and 1 if deleted.\n * Note that the bookmark memory is not freed yet, and is kept for future use. */\nint quicklistBookmarkDelete(quicklist *ql, const char *name) {\n    quicklistBookmark *bm = _quicklistBookmarkFindByName(ql, name);\n    if (!bm)\n        return 0;\n    _quicklistBookmarkDelete(ql, bm);\n    return 1;\n}\n\nquicklistBookmark *_quicklistBookmarkFindByName(quicklist *ql, const char *name) {\n    unsigned i;\n    for (i=0; i<ql->bookmark_count; i++) {\n        if (!strcmp(ql->bookmarks[i].name, name)) {\n            return &ql->bookmarks[i];\n        }\n    }\n    return NULL;\n}\n\nquicklistBookmark *_quicklistBookmarkFindByNode(quicklist *ql, quicklistNode *node) {\n    unsigned i;\n    for (i=0; i<ql->bookmark_count; i++) {\n        if (ql->bookmarks[i].node == node) {\n            return &ql->bookmarks[i];\n        }\n    }\n    return NULL;\n}\n\nvoid _quicklistBookmarkDelete(quicklist *ql, quicklistBookmark *bm) {\n    int index = bm - ql->bookmarks;\n    zfree(bm->name);\n    ql->bookmark_count--;\n    memmove(bm, bm+1, (ql->bookmark_count - index)* sizeof(*bm));\n    /* NOTE: We do not shrink (realloc) the quicklist yet (to avoid resonance,\n     * it may be re-used later (a call to realloc may NOP). */\n}\n\nvoid quicklistBookmarksClear(quicklist *ql) {\n    while (ql->bookmark_count)\n        zfree(ql->bookmarks[--ql->bookmark_count].name);\n    /* NOTE: We do not shrink (realloc) the quick list. main use case for this\n     * function is just before releasing the allocation. */\n}\n\n/* The rest of this file is test cases and test helpers. */\n#ifdef REDIS_TEST\n#include <stdint.h>\n#include <sys/time.h>\n\n#define yell(str, ...) printf(\"ERROR! \" str \"\\n\\n\", __VA_ARGS__)\n\n#define ERROR                                                                  \\\n    do {                                                                       \\\n        printf(\"\\tERROR!\\n\");                                                  \\\n        err++;                                                                 \\\n    } while (0)\n\n#define ERR(x, ...)                                                            \\\n    do {                                                                       \\\n        printf(\"%s:%s:%d:\\t\", __FILE__, __func__, __LINE__);                   \\\n        printf(\"ERROR! \" x \"\\n\", __VA_ARGS__);                                 \\\n        err++;                                                                 \\\n    } while (0)\n\n#define TEST(name) printf(\"test — %s\\n\", name);\n#define TEST_DESC(name, ...) printf(\"test — \" name \"\\n\", __VA_ARGS__);\n\n#define QL_TEST_VERBOSE 0\n\n#define UNUSED(x) (void)(x)\nstatic void ql_info(quicklist *ql) {\n#if QL_TEST_VERBOSE\n    printf(\"Container length: %lu\\n\", ql->len);\n    printf(\"Container size: %lu\\n\", ql->count);\n    if (ql->head)\n        printf(\"\\t(zsize head: %d)\\n\", ziplistLen(ql->head->zl));\n    if (ql->tail)\n        printf(\"\\t(zsize tail: %d)\\n\", ziplistLen(ql->tail->zl));\n    printf(\"\\n\");\n#else\n    UNUSED(ql);\n#endif\n}\n\n/* Return the UNIX time in microseconds */\nstatic long long ustime(void) {\n    struct timeval tv;\n    long long ust;\n\n    gettimeofday(&tv, NULL);\n    ust = ((long long)tv.tv_sec) * 1000000;\n    ust += tv.tv_usec;\n    return ust;\n}\n\n/* Return the UNIX time in milliseconds */\nstatic long long mstime(void) { return ustime() / 1000; }\n\n/* Iterate over an entire quicklist.\n * Print the list if 'print' == 1.\n *\n * Returns physical count of elements found by iterating over the list. */\nstatic int _itrprintr(quicklist *ql, int print, int forward) {\n    quicklistIter *iter =\n        quicklistGetIterator(ql, forward ? AL_START_HEAD : AL_START_TAIL);\n    quicklistEntry entry;\n    int i = 0;\n    int p = 0;\n    quicklistNode *prev = NULL;\n    while (quicklistNext(iter, &entry)) {\n        if (entry.node != prev) {\n            /* Count the number of list nodes too */\n            p++;\n            prev = entry.node;\n        }\n        if (print) {\n            printf(\"[%3d (%2d)]: [%.*s] (%lld)\\n\", i, p, entry.sz,\n                   (char *)entry.value, entry.longval);\n        }\n        i++;\n    }\n    quicklistReleaseIterator(iter);\n    return i;\n}\nstatic int itrprintr(quicklist *ql, int print) {\n    return _itrprintr(ql, print, 1);\n}\n\nstatic int itrprintr_rev(quicklist *ql, int print) {\n    return _itrprintr(ql, print, 0);\n}\n\n#define ql_verify(a, b, c, d, e)                                               \\\n    do {                                                                       \\\n        err += _ql_verify((a), (b), (c), (d), (e));                            \\\n    } while (0)\n\n/* Verify list metadata matches physical list contents. */\nstatic int _ql_verify(quicklist *ql, uint32_t len, uint32_t count,\n                      uint32_t head_count, uint32_t tail_count) {\n    int errors = 0;\n\n    ql_info(ql);\n    if (len != ql->len) {\n        yell(\"quicklist length wrong: expected %d, got %lu\", len, ql->len);\n        errors++;\n    }\n\n    if (count != ql->count) {\n        yell(\"quicklist count wrong: expected %d, got %lu\", count, ql->count);\n        errors++;\n    }\n\n    int loopr = itrprintr(ql, 0);\n    if (loopr != (int)ql->count) {\n        yell(\"quicklist cached count not match actual count: expected %lu, got \"\n             \"%d\",\n             ql->count, loopr);\n        errors++;\n    }\n\n    int rloopr = itrprintr_rev(ql, 0);\n    if (loopr != rloopr) {\n        yell(\"quicklist has different forward count than reverse count!  \"\n             \"Forward count is %d, reverse count is %d.\",\n             loopr, rloopr);\n        errors++;\n    }\n\n    if (ql->len == 0 && !errors) {\n        return errors;\n    }\n\n    if (ql->head && head_count != ql->head->count &&\n        head_count != ziplistLen(ql->head->zl)) {\n        yell(\"quicklist head count wrong: expected %d, \"\n             \"got cached %d vs. actual %d\",\n             head_count, ql->head->count, ziplistLen(ql->head->zl));\n        errors++;\n    }\n\n    if (ql->tail && tail_count != ql->tail->count &&\n        tail_count != ziplistLen(ql->tail->zl)) {\n        yell(\"quicklist tail count wrong: expected %d, \"\n             \"got cached %u vs. actual %d\",\n             tail_count, ql->tail->count, ziplistLen(ql->tail->zl));\n        errors++;\n    }\n\n    if (quicklistAllowsCompression(ql)) {\n        quicklistNode *node = ql->head;\n        unsigned int low_raw = ql->compress;\n        unsigned int high_raw = ql->len - ql->compress;\n\n        for (unsigned int at = 0; at < ql->len; at++, node = node->next) {\n            if (node && (at < low_raw || at >= high_raw)) {\n                if (node->encoding != QUICKLIST_NODE_ENCODING_RAW) {\n                    yell(\"Incorrect compression: node %d is \"\n                         \"compressed at depth %d ((%u, %u); total \"\n                         \"nodes: %lu; size: %u; recompress: %d)\",\n                         at, ql->compress, low_raw, high_raw, ql->len, node->sz,\n                         node->recompress);\n                    errors++;\n                }\n            } else {\n                if (node->encoding != QUICKLIST_NODE_ENCODING_LZF &&\n                    !node->attempted_compress) {\n                    yell(\"Incorrect non-compression: node %d is NOT \"\n                         \"compressed at depth %d ((%u, %u); total \"\n                         \"nodes: %lu; size: %u; recompress: %d; attempted: %d)\",\n                         at, ql->compress, low_raw, high_raw, ql->len, node->sz,\n                         node->recompress, node->attempted_compress);\n                    errors++;\n                }\n            }\n        }\n    }\n\n    return errors;\n}\n\n/* Generate new string concatenating integer i against string 'prefix' */\nstatic char *genstr(char *prefix, int i) {\n    static char result[64] = {0};\n    snprintf(result, sizeof(result), \"%s%d\", prefix, i);\n    return result;\n}\n\n/* main test, but callable from other files */\nint quicklistTest(int argc, char *argv[], int accurate) {\n    UNUSED(argc);\n    UNUSED(argv);\n    UNUSED(accurate);\n\n    unsigned int err = 0;\n    int optimize_start =\n        -(int)(sizeof(optimization_level) / sizeof(*optimization_level));\n\n    printf(\"Starting optimization offset at: %d\\n\", optimize_start);\n\n    int options[] = {0, 1, 2, 3, 4, 5, 6, 10};\n    int fills[] = {-5, -4, -3, -2, -1, 0,\n                   1, 2, 32, 66, 128, 999};\n    size_t option_count = sizeof(options) / sizeof(*options);\n    int fill_count = (int)(sizeof(fills) / sizeof(*fills));\n    long long runtime[option_count];\n\n    for (int _i = 0; _i < (int)option_count; _i++) {\n        printf(\"Testing Compression option %d\\n\", options[_i]);\n        long long start = mstime();\n\n        TEST(\"create list\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            ql_verify(ql, 0, 0, 0, 0);\n            quicklistRelease(ql);\n        }\n\n        TEST(\"add to tail of empty list\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            quicklistPushTail(ql, \"hello\", 6);\n            /* 1 for head and 1 for tail because 1 node = head = tail */\n            ql_verify(ql, 1, 1, 1, 1);\n            quicklistRelease(ql);\n        }\n\n        TEST(\"add to head of empty list\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            quicklistPushHead(ql, \"hello\", 6);\n            /* 1 for head and 1 for tail because 1 node = head = tail */\n            ql_verify(ql, 1, 1, 1, 1);\n            quicklistRelease(ql);\n        }\n\n        TEST_DESC(\"add to tail 5x at compress %d\", options[_i]) {\n            for (int f = 0; f < fill_count; f++) {\n                quicklist *ql = quicklistNew(fills[f], options[_i]);\n                for (int i = 0; i < 5; i++)\n                    quicklistPushTail(ql, genstr(\"hello\", i), 32);\n                if (ql->count != 5)\n                    ERROR;\n                if (fills[f] == 32)\n                    ql_verify(ql, 1, 5, 5, 5);\n                quicklistRelease(ql);\n            }\n        }\n\n        TEST_DESC(\"add to head 5x at compress %d\", options[_i]) {\n            for (int f = 0; f < fill_count; f++) {\n                quicklist *ql = quicklistNew(fills[f], options[_i]);\n                for (int i = 0; i < 5; i++)\n                    quicklistPushHead(ql, genstr(\"hello\", i), 32);\n                if (ql->count != 5)\n                    ERROR;\n                if (fills[f] == 32)\n                    ql_verify(ql, 1, 5, 5, 5);\n                quicklistRelease(ql);\n            }\n        }\n\n        TEST_DESC(\"add to tail 500x at compress %d\", options[_i]) {\n            for (int f = 0; f < fill_count; f++) {\n                quicklist *ql = quicklistNew(fills[f], options[_i]);\n                for (int i = 0; i < 500; i++)\n                    quicklistPushTail(ql, genstr(\"hello\", i), 64);\n                if (ql->count != 500)\n                    ERROR;\n                if (fills[f] == 32)\n                    ql_verify(ql, 16, 500, 32, 20);\n                quicklistRelease(ql);\n            }\n        }\n\n        TEST_DESC(\"add to head 500x at compress %d\", options[_i]) {\n            for (int f = 0; f < fill_count; f++) {\n                quicklist *ql = quicklistNew(fills[f], options[_i]);\n                for (int i = 0; i < 500; i++)\n                    quicklistPushHead(ql, genstr(\"hello\", i), 32);\n                if (ql->count != 500)\n                    ERROR;\n                if (fills[f] == 32)\n                    ql_verify(ql, 16, 500, 20, 32);\n                quicklistRelease(ql);\n            }\n        }\n\n        TEST(\"rotate empty\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            quicklistRotate(ql);\n            ql_verify(ql, 0, 0, 0, 0);\n            quicklistRelease(ql);\n        }\n\n        TEST(\"rotate one val once\") {\n            for (int f = 0; f < fill_count; f++) {\n                quicklist *ql = quicklistNew(fills[f], options[_i]);\n                quicklistPushHead(ql, \"hello\", 6);\n                quicklistRotate(ql);\n                /* Ignore compression verify because ziplist is\n                 * too small to compress. */\n                ql_verify(ql, 1, 1, 1, 1);\n                quicklistRelease(ql);\n            }\n        }\n\n        TEST_DESC(\"rotate 500 val 5000 times at compress %d\", options[_i]) {\n            for (int f = 0; f < fill_count; f++) {\n                quicklist *ql = quicklistNew(fills[f], options[_i]);\n                quicklistPushHead(ql, \"900\", 3);\n                quicklistPushHead(ql, \"7000\", 4);\n                quicklistPushHead(ql, \"-1200\", 5);\n                quicklistPushHead(ql, \"42\", 2);\n                for (int i = 0; i < 500; i++)\n                    quicklistPushHead(ql, genstr(\"hello\", i), 64);\n                ql_info(ql);\n                for (int i = 0; i < 5000; i++) {\n                    ql_info(ql);\n                    quicklistRotate(ql);\n                }\n                if (fills[f] == 1)\n                    ql_verify(ql, 504, 504, 1, 1);\n                else if (fills[f] == 2)\n                    ql_verify(ql, 252, 504, 2, 2);\n                else if (fills[f] == 32)\n                    ql_verify(ql, 16, 504, 32, 24);\n                quicklistRelease(ql);\n            }\n        }\n\n        TEST(\"pop empty\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            quicklistPop(ql, QUICKLIST_HEAD, NULL, NULL, NULL);\n            ql_verify(ql, 0, 0, 0, 0);\n            quicklistRelease(ql);\n        }\n\n        TEST(\"pop 1 string from 1\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            char *populate = genstr(\"hello\", 331);\n            quicklistPushHead(ql, populate, 32);\n            unsigned char *data;\n            unsigned int sz;\n            long long lv;\n            ql_info(ql);\n            quicklistPop(ql, QUICKLIST_HEAD, &data, &sz, &lv);\n            assert(data != NULL);\n            assert(sz == 32);\n            if (strcmp(populate, (char *)data))\n                ERR(\"Pop'd value (%.*s) didn't equal original value (%s)\", sz,\n                    data, populate);\n            zfree(data);\n            ql_verify(ql, 0, 0, 0, 0);\n            quicklistRelease(ql);\n        }\n\n        TEST(\"pop head 1 number from 1\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            quicklistPushHead(ql, \"55513\", 5);\n            unsigned char *data;\n            unsigned int sz;\n            long long lv;\n            ql_info(ql);\n            quicklistPop(ql, QUICKLIST_HEAD, &data, &sz, &lv);\n            assert(data == NULL);\n            assert(lv == 55513);\n            ql_verify(ql, 0, 0, 0, 0);\n            quicklistRelease(ql);\n        }\n\n        TEST(\"pop head 500 from 500\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            for (int i = 0; i < 500; i++)\n                quicklistPushHead(ql, genstr(\"hello\", i), 32);\n            ql_info(ql);\n            for (int i = 0; i < 500; i++) {\n                unsigned char *data;\n                unsigned int sz;\n                long long lv;\n                int ret = quicklistPop(ql, QUICKLIST_HEAD, &data, &sz, &lv);\n                assert(ret == 1);\n                assert(data != NULL);\n                assert(sz == 32);\n                if (strcmp(genstr(\"hello\", 499 - i), (char *)data))\n                    ERR(\"Pop'd value (%.*s) didn't equal original value (%s)\",\n                        sz, data, genstr(\"hello\", 499 - i));\n                zfree(data);\n            }\n            ql_verify(ql, 0, 0, 0, 0);\n            quicklistRelease(ql);\n        }\n\n        TEST(\"pop head 5000 from 500\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            for (int i = 0; i < 500; i++)\n                quicklistPushHead(ql, genstr(\"hello\", i), 32);\n            for (int i = 0; i < 5000; i++) {\n                unsigned char *data;\n                unsigned int sz;\n                long long lv;\n                int ret = quicklistPop(ql, QUICKLIST_HEAD, &data, &sz, &lv);\n                if (i < 500) {\n                    assert(ret == 1);\n                    assert(data != NULL);\n                    assert(sz == 32);\n                    if (strcmp(genstr(\"hello\", 499 - i), (char *)data))\n                        ERR(\"Pop'd value (%.*s) didn't equal original value \"\n                            \"(%s)\",\n                            sz, data, genstr(\"hello\", 499 - i));\n                    zfree(data);\n                } else {\n                    assert(ret == 0);\n                }\n            }\n            ql_verify(ql, 0, 0, 0, 0);\n            quicklistRelease(ql);\n        }\n\n        TEST(\"iterate forward over 500 list\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            quicklistSetFill(ql, 32);\n            for (int i = 0; i < 500; i++)\n                quicklistPushHead(ql, genstr(\"hello\", i), 32);\n            quicklistIter *iter = quicklistGetIterator(ql, AL_START_HEAD);\n            quicklistEntry entry;\n            int i = 499, count = 0;\n            while (quicklistNext(iter, &entry)) {\n                char *h = genstr(\"hello\", i);\n                if (strcmp((char *)entry.value, h))\n                    ERR(\"value [%s] didn't match [%s] at position %d\",\n                        entry.value, h, i);\n                i--;\n                count++;\n            }\n            if (count != 500)\n                ERR(\"Didn't iterate over exactly 500 elements (%d)\", i);\n            ql_verify(ql, 16, 500, 20, 32);\n            quicklistReleaseIterator(iter);\n            quicklistRelease(ql);\n        }\n\n        TEST(\"iterate reverse over 500 list\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            quicklistSetFill(ql, 32);\n            for (int i = 0; i < 500; i++)\n                quicklistPushHead(ql, genstr(\"hello\", i), 32);\n            quicklistIter *iter = quicklistGetIterator(ql, AL_START_TAIL);\n            quicklistEntry entry;\n            int i = 0;\n            while (quicklistNext(iter, &entry)) {\n                char *h = genstr(\"hello\", i);\n                if (strcmp((char *)entry.value, h))\n                    ERR(\"value [%s] didn't match [%s] at position %d\",\n                        entry.value, h, i);\n                i++;\n            }\n            if (i != 500)\n                ERR(\"Didn't iterate over exactly 500 elements (%d)\", i);\n            ql_verify(ql, 16, 500, 20, 32);\n            quicklistReleaseIterator(iter);\n            quicklistRelease(ql);\n        }\n\n        TEST(\"insert before with 0 elements\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            quicklistEntry entry;\n            quicklistIndex(ql, 0, &entry);\n            quicklistInsertBefore(ql, &entry, \"abc\", 4);\n            ql_verify(ql, 1, 1, 1, 1);\n            quicklistRelease(ql);\n        }\n\n        TEST(\"insert after with 0 elements\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            quicklistEntry entry;\n            quicklistIndex(ql, 0, &entry);\n            quicklistInsertAfter(ql, &entry, \"abc\", 4);\n            ql_verify(ql, 1, 1, 1, 1);\n            quicklistRelease(ql);\n        }\n\n        TEST(\"insert after 1 element\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            quicklistPushHead(ql, \"hello\", 6);\n            quicklistEntry entry;\n            quicklistIndex(ql, 0, &entry);\n            quicklistInsertAfter(ql, &entry, \"abc\", 4);\n            ql_verify(ql, 1, 2, 2, 2);\n            quicklistRelease(ql);\n        }\n\n        TEST(\"insert before 1 element\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            quicklistPushHead(ql, \"hello\", 6);\n            quicklistEntry entry;\n            quicklistIndex(ql, 0, &entry);\n            quicklistInsertAfter(ql, &entry, \"abc\", 4);\n            ql_verify(ql, 1, 2, 2, 2);\n            quicklistRelease(ql);\n        }\n\n        TEST_DESC(\"insert once in elements while iterating at compress %d\",\n                  options[_i]) {\n            for (int f = 0; f < fill_count; f++) {\n                quicklist *ql = quicklistNew(fills[f], options[_i]);\n                quicklistPushTail(ql, \"abc\", 3);\n                quicklistSetFill(ql, 1);\n                quicklistPushTail(ql, \"def\", 3); /* force to unique node */\n                quicklistSetFill(ql, f);\n                quicklistPushTail(ql, \"bob\", 3); /* force to reset for +3 */\n                quicklistPushTail(ql, \"foo\", 3);\n                quicklistPushTail(ql, \"zoo\", 3);\n\n                itrprintr(ql, 0);\n                /* insert \"bar\" before \"bob\" while iterating over list. */\n                quicklistIter *iter = quicklistGetIterator(ql, AL_START_HEAD);\n                quicklistEntry entry;\n                while (quicklistNext(iter, &entry)) {\n                    if (!strncmp((char *)entry.value, \"bob\", 3)) {\n                        /* Insert as fill = 1 so it spills into new node. */\n                        quicklistInsertBefore(ql, &entry, \"bar\", 3);\n                        break; /* didn't we fix insert-while-iterating? */\n                    }\n                }\n                itrprintr(ql, 0);\n\n                /* verify results */\n                quicklistIndex(ql, 0, &entry);\n                if (strncmp((char *)entry.value, \"abc\", 3))\n                    ERR(\"Value 0 didn't match, instead got: %.*s\", entry.sz,\n                        entry.value);\n                quicklistIndex(ql, 1, &entry);\n                if (strncmp((char *)entry.value, \"def\", 3))\n                    ERR(\"Value 1 didn't match, instead got: %.*s\", entry.sz,\n                        entry.value);\n                quicklistIndex(ql, 2, &entry);\n                if (strncmp((char *)entry.value, \"bar\", 3))\n                    ERR(\"Value 2 didn't match, instead got: %.*s\", entry.sz,\n                        entry.value);\n                quicklistIndex(ql, 3, &entry);\n                if (strncmp((char *)entry.value, \"bob\", 3))\n                    ERR(\"Value 3 didn't match, instead got: %.*s\", entry.sz,\n                        entry.value);\n                quicklistIndex(ql, 4, &entry);\n                if (strncmp((char *)entry.value, \"foo\", 3))\n                    ERR(\"Value 4 didn't match, instead got: %.*s\", entry.sz,\n                        entry.value);\n                quicklistIndex(ql, 5, &entry);\n                if (strncmp((char *)entry.value, \"zoo\", 3))\n                    ERR(\"Value 5 didn't match, instead got: %.*s\", entry.sz,\n                        entry.value);\n                quicklistReleaseIterator(iter);\n                quicklistRelease(ql);\n            }\n        }\n\n        TEST_DESC(\"insert [before] 250 new in middle of 500 elements at compress %d\",\n                  options[_i]) {\n            for (int f = 0; f < fill_count; f++) {\n                quicklist *ql = quicklistNew(fills[f], options[_i]);\n                for (int i = 0; i < 500; i++)\n                    quicklistPushTail(ql, genstr(\"hello\", i), 32);\n                for (int i = 0; i < 250; i++) {\n                    quicklistEntry entry;\n                    quicklistIndex(ql, 250, &entry);\n                    quicklistInsertBefore(ql, &entry, genstr(\"abc\", i), 32);\n                }\n                if (fills[f] == 32)\n                    ql_verify(ql, 25, 750, 32, 20);\n                quicklistRelease(ql);\n            }\n        }\n\n        TEST_DESC(\"insert [after] 250 new in middle of 500 elements at compress %d\",\n                  options[_i]) {\n            for (int f = 0; f < fill_count; f++) {\n                quicklist *ql = quicklistNew(fills[f], options[_i]);\n                for (int i = 0; i < 500; i++)\n                    quicklistPushHead(ql, genstr(\"hello\", i), 32);\n                for (int i = 0; i < 250; i++) {\n                    quicklistEntry entry;\n                    quicklistIndex(ql, 250, &entry);\n                    quicklistInsertAfter(ql, &entry, genstr(\"abc\", i), 32);\n                }\n\n                if (ql->count != 750)\n                    ERR(\"List size not 750, but rather %ld\", ql->count);\n\n                if (fills[f] == 32)\n                    ql_verify(ql, 26, 750, 20, 32);\n                quicklistRelease(ql);\n            }\n        }\n\n        TEST(\"duplicate empty list\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            ql_verify(ql, 0, 0, 0, 0);\n            quicklist *copy = quicklistDup(ql);\n            ql_verify(copy, 0, 0, 0, 0);\n            quicklistRelease(ql);\n            quicklistRelease(copy);\n        }\n\n        TEST(\"duplicate list of 1 element\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            quicklistPushHead(ql, genstr(\"hello\", 3), 32);\n            ql_verify(ql, 1, 1, 1, 1);\n            quicklist *copy = quicklistDup(ql);\n            ql_verify(copy, 1, 1, 1, 1);\n            quicklistRelease(ql);\n            quicklistRelease(copy);\n        }\n\n        TEST(\"duplicate list of 500\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            quicklistSetFill(ql, 32);\n            for (int i = 0; i < 500; i++)\n                quicklistPushHead(ql, genstr(\"hello\", i), 32);\n            ql_verify(ql, 16, 500, 20, 32);\n\n            quicklist *copy = quicklistDup(ql);\n            ql_verify(copy, 16, 500, 20, 32);\n            quicklistRelease(ql);\n            quicklistRelease(copy);\n        }\n\n        for (int f = 0; f < fill_count; f++) {\n            TEST_DESC(\"index 1,200 from 500 list at fill %d at compress %d\", f,\n                      options[_i]) {\n                quicklist *ql = quicklistNew(fills[f], options[_i]);\n                for (int i = 0; i < 500; i++)\n                    quicklistPushTail(ql, genstr(\"hello\", i + 1), 32);\n                quicklistEntry entry;\n                quicklistIndex(ql, 1, &entry);\n                if (strcmp((char *)entry.value, \"hello2\") != 0)\n                    ERR(\"Value: %s\", entry.value);\n                quicklistIndex(ql, 200, &entry);\n                if (strcmp((char *)entry.value, \"hello201\") != 0)\n                    ERR(\"Value: %s\", entry.value);\n                quicklistRelease(ql);\n            }\n\n            TEST_DESC(\"index -1,-2 from 500 list at fill %d at compress %d\",\n                      fills[f], options[_i]) {\n                quicklist *ql = quicklistNew(fills[f], options[_i]);\n                for (int i = 0; i < 500; i++)\n                    quicklistPushTail(ql, genstr(\"hello\", i + 1), 32);\n                quicklistEntry entry;\n                quicklistIndex(ql, -1, &entry);\n                if (strcmp((char *)entry.value, \"hello500\") != 0)\n                    ERR(\"Value: %s\", entry.value);\n                quicklistIndex(ql, -2, &entry);\n                if (strcmp((char *)entry.value, \"hello499\") != 0)\n                    ERR(\"Value: %s\", entry.value);\n                quicklistRelease(ql);\n            }\n\n            TEST_DESC(\"index -100 from 500 list at fill %d at compress %d\",\n                      fills[f], options[_i]) {\n                quicklist *ql = quicklistNew(fills[f], options[_i]);\n                for (int i = 0; i < 500; i++)\n                    quicklistPushTail(ql, genstr(\"hello\", i + 1), 32);\n                quicklistEntry entry;\n                quicklistIndex(ql, -100, &entry);\n                if (strcmp((char *)entry.value, \"hello401\") != 0)\n                    ERR(\"Value: %s\", entry.value);\n                quicklistRelease(ql);\n            }\n\n            TEST_DESC(\"index too big +1 from 50 list at fill %d at compress %d\",\n                      fills[f], options[_i]) {\n                quicklist *ql = quicklistNew(fills[f], options[_i]);\n                for (int i = 0; i < 50; i++)\n                    quicklistPushTail(ql, genstr(\"hello\", i + 1), 32);\n                quicklistEntry entry;\n                if (quicklistIndex(ql, 50, &entry))\n                    ERR(\"Index found at 50 with 50 list: %.*s\", entry.sz,\n                        entry.value);\n                quicklistRelease(ql);\n            }\n        }\n\n        TEST(\"delete range empty list\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            quicklistDelRange(ql, 5, 20);\n            ql_verify(ql, 0, 0, 0, 0);\n            quicklistRelease(ql);\n        }\n\n        TEST(\"delete range of entire node in list of one node\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            for (int i = 0; i < 32; i++)\n                quicklistPushHead(ql, genstr(\"hello\", i), 32);\n            ql_verify(ql, 1, 32, 32, 32);\n            quicklistDelRange(ql, 0, 32);\n            ql_verify(ql, 0, 0, 0, 0);\n            quicklistRelease(ql);\n        }\n\n        TEST(\"delete range of entire node with overflow counts\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            for (int i = 0; i < 32; i++)\n                quicklistPushHead(ql, genstr(\"hello\", i), 32);\n            ql_verify(ql, 1, 32, 32, 32);\n            quicklistDelRange(ql, 0, 128);\n            ql_verify(ql, 0, 0, 0, 0);\n            quicklistRelease(ql);\n        }\n\n        TEST(\"delete middle 100 of 500 list\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            quicklistSetFill(ql, 32);\n            for (int i = 0; i < 500; i++)\n                quicklistPushTail(ql, genstr(\"hello\", i + 1), 32);\n            ql_verify(ql, 16, 500, 32, 20);\n            quicklistDelRange(ql, 200, 100);\n            ql_verify(ql, 14, 400, 32, 20);\n            quicklistRelease(ql);\n        }\n\n        TEST(\"delete less than fill but across nodes\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            quicklistSetFill(ql, 32);\n            for (int i = 0; i < 500; i++)\n                quicklistPushTail(ql, genstr(\"hello\", i + 1), 32);\n            ql_verify(ql, 16, 500, 32, 20);\n            quicklistDelRange(ql, 60, 10);\n            ql_verify(ql, 16, 490, 32, 20);\n            quicklistRelease(ql);\n        }\n\n        TEST(\"delete negative 1 from 500 list\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            quicklistSetFill(ql, 32);\n            for (int i = 0; i < 500; i++)\n                quicklistPushTail(ql, genstr(\"hello\", i + 1), 32);\n            ql_verify(ql, 16, 500, 32, 20);\n            quicklistDelRange(ql, -1, 1);\n            ql_verify(ql, 16, 499, 32, 19);\n            quicklistRelease(ql);\n        }\n\n        TEST(\"delete negative 1 from 500 list with overflow counts\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            quicklistSetFill(ql, 32);\n            for (int i = 0; i < 500; i++)\n                quicklistPushTail(ql, genstr(\"hello\", i + 1), 32);\n            ql_verify(ql, 16, 500, 32, 20);\n            quicklistDelRange(ql, -1, 128);\n            ql_verify(ql, 16, 499, 32, 19);\n            quicklistRelease(ql);\n        }\n\n        TEST(\"delete negative 100 from 500 list\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            quicklistSetFill(ql, 32);\n            for (int i = 0; i < 500; i++)\n                quicklistPushTail(ql, genstr(\"hello\", i + 1), 32);\n            quicklistDelRange(ql, -100, 100);\n            ql_verify(ql, 13, 400, 32, 16);\n            quicklistRelease(ql);\n        }\n\n        TEST(\"delete -10 count 5 from 50 list\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            quicklistSetFill(ql, 32);\n            for (int i = 0; i < 50; i++)\n                quicklistPushTail(ql, genstr(\"hello\", i + 1), 32);\n            ql_verify(ql, 2, 50, 32, 18);\n            quicklistDelRange(ql, -10, 5);\n            ql_verify(ql, 2, 45, 32, 13);\n            quicklistRelease(ql);\n        }\n\n        TEST(\"numbers only list read\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            quicklistPushTail(ql, \"1111\", 4);\n            quicklistPushTail(ql, \"2222\", 4);\n            quicklistPushTail(ql, \"3333\", 4);\n            quicklistPushTail(ql, \"4444\", 4);\n            ql_verify(ql, 1, 4, 4, 4);\n            quicklistEntry entry;\n            quicklistIndex(ql, 0, &entry);\n            if (entry.longval != 1111)\n                ERR(\"Not 1111, %lld\", entry.longval);\n            quicklistIndex(ql, 1, &entry);\n            if (entry.longval != 2222)\n                ERR(\"Not 2222, %lld\", entry.longval);\n            quicklistIndex(ql, 2, &entry);\n            if (entry.longval != 3333)\n                ERR(\"Not 3333, %lld\", entry.longval);\n            quicklistIndex(ql, 3, &entry);\n            if (entry.longval != 4444)\n                ERR(\"Not 4444, %lld\", entry.longval);\n            if (quicklistIndex(ql, 4, &entry))\n                ERR(\"Index past elements: %lld\", entry.longval);\n            quicklistIndex(ql, -1, &entry);\n            if (entry.longval != 4444)\n                ERR(\"Not 4444 (reverse), %lld\", entry.longval);\n            quicklistIndex(ql, -2, &entry);\n            if (entry.longval != 3333)\n                ERR(\"Not 3333 (reverse), %lld\", entry.longval);\n            quicklistIndex(ql, -3, &entry);\n            if (entry.longval != 2222)\n                ERR(\"Not 2222 (reverse), %lld\", entry.longval);\n            quicklistIndex(ql, -4, &entry);\n            if (entry.longval != 1111)\n                ERR(\"Not 1111 (reverse), %lld\", entry.longval);\n            if (quicklistIndex(ql, -5, &entry))\n                ERR(\"Index past elements (reverse), %lld\", entry.longval);\n            quicklistRelease(ql);\n        }\n\n        TEST(\"numbers larger list read\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            quicklistSetFill(ql, 32);\n            char num[32];\n            long long nums[5000];\n            for (int i = 0; i < 5000; i++) {\n                nums[i] = -5157318210846258176 + i;\n                int sz = ll2string(num, sizeof(num), nums[i]);\n                quicklistPushTail(ql, num, sz);\n            }\n            quicklistPushTail(ql, \"xxxxxxxxxxxxxxxxxxxx\", 20);\n            quicklistEntry entry;\n            for (int i = 0; i < 5000; i++) {\n                quicklistIndex(ql, i, &entry);\n                if (entry.longval != nums[i])\n                    ERR(\"[%d] Not longval %lld but rather %lld\", i, nums[i],\n                        entry.longval);\n                entry.longval = 0xdeadbeef;\n            }\n            quicklistIndex(ql, 5000, &entry);\n            if (strncmp((char *)entry.value, \"xxxxxxxxxxxxxxxxxxxx\", 20))\n                ERR(\"String val not match: %s\", entry.value);\n            ql_verify(ql, 157, 5001, 32, 9);\n            quicklistRelease(ql);\n        }\n\n        TEST(\"numbers larger list read B\") {\n            quicklist *ql = quicklistNew(-2, options[_i]);\n            quicklistPushTail(ql, \"99\", 2);\n            quicklistPushTail(ql, \"98\", 2);\n            quicklistPushTail(ql, \"xxxxxxxxxxxxxxxxxxxx\", 20);\n            quicklistPushTail(ql, \"96\", 2);\n            quicklistPushTail(ql, \"95\", 2);\n            quicklistReplaceAtIndex(ql, 1, \"foo\", 3);\n            quicklistReplaceAtIndex(ql, -1, \"bar\", 3);\n            quicklistRelease(ql);\n        }\n\n        TEST_DESC(\"lrem test at compress %d\", options[_i]) {\n            for (int f = 0; f < fill_count; f++) {\n                quicklist *ql = quicklistNew(fills[f], options[_i]);\n                char *words[] = {\"abc\", \"foo\", \"bar\",  \"foobar\", \"foobared\",\n                                 \"zap\", \"bar\", \"test\", \"foo\"};\n                char *result[] = {\"abc\", \"foo\",  \"foobar\", \"foobared\",\n                                  \"zap\", \"test\", \"foo\"};\n                char *resultB[] = {\"abc\",      \"foo\", \"foobar\",\n                                   \"foobared\", \"zap\", \"test\"};\n                for (int i = 0; i < 9; i++)\n                    quicklistPushTail(ql, words[i], strlen(words[i]));\n\n                /* lrem 0 bar */\n                quicklistIter *iter = quicklistGetIterator(ql, AL_START_HEAD);\n                quicklistEntry entry;\n                int i = 0;\n                while (quicklistNext(iter, &entry)) {\n                    if (quicklistCompare(entry.zi, (unsigned char *)\"bar\", 3)) {\n                        quicklistDelEntry(iter, &entry);\n                    }\n                    i++;\n                }\n                quicklistReleaseIterator(iter);\n\n                /* check result of lrem 0 bar */\n                iter = quicklistGetIterator(ql, AL_START_HEAD);\n                i = 0;\n                while (quicklistNext(iter, &entry)) {\n                    /* Result must be: abc, foo, foobar, foobared, zap, test,\n                     * foo */\n                    if (strncmp((char *)entry.value, result[i], entry.sz)) {\n                        ERR(\"No match at position %d, got %.*s instead of %s\",\n                            i, entry.sz, entry.value, result[i]);\n                    }\n                    i++;\n                }\n                quicklistReleaseIterator(iter);\n\n                quicklistPushTail(ql, \"foo\", 3);\n\n                /* lrem -2 foo */\n                iter = quicklistGetIterator(ql, AL_START_TAIL);\n                i = 0;\n                int del = 2;\n                while (quicklistNext(iter, &entry)) {\n                    if (quicklistCompare(entry.zi, (unsigned char *)\"foo\", 3)) {\n                        quicklistDelEntry(iter, &entry);\n                        del--;\n                    }\n                    if (!del)\n                        break;\n                    i++;\n                }\n                quicklistReleaseIterator(iter);\n\n                /* check result of lrem -2 foo */\n                /* (we're ignoring the '2' part and still deleting all foo\n                 * because\n                 * we only have two foo) */\n                iter = quicklistGetIterator(ql, AL_START_TAIL);\n                i = 0;\n                size_t resB = sizeof(resultB) / sizeof(*resultB);\n                while (quicklistNext(iter, &entry)) {\n                    /* Result must be: abc, foo, foobar, foobared, zap, test,\n                     * foo */\n                    if (strncmp((char *)entry.value, resultB[resB - 1 - i],\n                                entry.sz)) {\n                        ERR(\"No match at position %d, got %.*s instead of %s\",\n                            i, entry.sz, entry.value, resultB[resB - 1 - i]);\n                    }\n                    i++;\n                }\n\n                quicklistReleaseIterator(iter);\n                quicklistRelease(ql);\n            }\n        }\n\n        TEST_DESC(\"iterate reverse + delete at compress %d\", options[_i]) {\n            for (int f = 0; f < fill_count; f++) {\n                quicklist *ql = quicklistNew(fills[f], options[_i]);\n                quicklistPushTail(ql, \"abc\", 3);\n                quicklistPushTail(ql, \"def\", 3);\n                quicklistPushTail(ql, \"hij\", 3);\n                quicklistPushTail(ql, \"jkl\", 3);\n                quicklistPushTail(ql, \"oop\", 3);\n\n                quicklistEntry entry;\n                quicklistIter *iter = quicklistGetIterator(ql, AL_START_TAIL);\n                int i = 0;\n                while (quicklistNext(iter, &entry)) {\n                    if (quicklistCompare(entry.zi, (unsigned char *)\"hij\", 3)) {\n                        quicklistDelEntry(iter, &entry);\n                    }\n                    i++;\n                }\n                quicklistReleaseIterator(iter);\n\n                if (i != 5)\n                    ERR(\"Didn't iterate 5 times, iterated %d times.\", i);\n\n                /* Check results after deletion of \"hij\" */\n                iter = quicklistGetIterator(ql, AL_START_HEAD);\n                i = 0;\n                char *vals[] = {\"abc\", \"def\", \"jkl\", \"oop\"};\n                while (quicklistNext(iter, &entry)) {\n                    if (!quicklistCompare(entry.zi, (unsigned char *)vals[i],\n                                          3)) {\n                        ERR(\"Value at %d didn't match %s\\n\", i, vals[i]);\n                    }\n                    i++;\n                }\n                quicklistReleaseIterator(iter);\n                quicklistRelease(ql);\n            }\n        }\n\n        TEST_DESC(\"iterator at index test at compress %d\", options[_i]) {\n            for (int f = 0; f < fill_count; f++) {\n                quicklist *ql = quicklistNew(fills[f], options[_i]);\n                char num[32];\n                long long nums[5000];\n                for (int i = 0; i < 760; i++) {\n                    nums[i] = -5157318210846258176 + i;\n                    int sz = ll2string(num, sizeof(num), nums[i]);\n                    quicklistPushTail(ql, num, sz);\n                }\n\n                quicklistEntry entry;\n                quicklistIter *iter =\n                    quicklistGetIteratorAtIdx(ql, AL_START_HEAD, 437);\n                int i = 437;\n                while (quicklistNext(iter, &entry)) {\n                    if (entry.longval != nums[i])\n                        ERR(\"Expected %lld, but got %lld\", entry.longval,\n                            nums[i]);\n                    i++;\n                }\n                quicklistReleaseIterator(iter);\n                quicklistRelease(ql);\n            }\n        }\n\n        TEST_DESC(\"ltrim test A at compress %d\", options[_i]) {\n            for (int f = 0; f < fill_count; f++) {\n                quicklist *ql = quicklistNew(fills[f], options[_i]);\n                char num[32];\n                long long nums[5000];\n                for (int i = 0; i < 32; i++) {\n                    nums[i] = -5157318210846258176 + i;\n                    int sz = ll2string(num, sizeof(num), nums[i]);\n                    quicklistPushTail(ql, num, sz);\n                }\n                if (fills[f] == 32)\n                    ql_verify(ql, 1, 32, 32, 32);\n                /* ltrim 25 53 (keep [25,32] inclusive = 7 remaining) */\n                quicklistDelRange(ql, 0, 25);\n                quicklistDelRange(ql, 0, 0);\n                quicklistEntry entry;\n                for (int i = 0; i < 7; i++) {\n                    quicklistIndex(ql, i, &entry);\n                    if (entry.longval != nums[25 + i])\n                        ERR(\"Deleted invalid range!  Expected %lld but got \"\n                            \"%lld\",\n                            entry.longval, nums[25 + i]);\n                }\n                if (fills[f] == 32)\n                    ql_verify(ql, 1, 7, 7, 7);\n                quicklistRelease(ql);\n            }\n        }\n\n        TEST_DESC(\"ltrim test B at compress %d\", options[_i]) {\n            for (int f = 0; f < fill_count; f++) {\n                /* Force-disable compression because our 33 sequential\n                 * integers don't compress and the check always fails. */\n                quicklist *ql = quicklistNew(fills[f], QUICKLIST_NOCOMPRESS);\n                char num[32];\n                long long nums[5000];\n                for (int i = 0; i < 33; i++) {\n                    nums[i] = i;\n                    int sz = ll2string(num, sizeof(num), nums[i]);\n                    quicklistPushTail(ql, num, sz);\n                }\n                if (fills[f] == 32)\n                    ql_verify(ql, 2, 33, 32, 1);\n                /* ltrim 5 16 (keep [5,16] inclusive = 12 remaining) */\n                quicklistDelRange(ql, 0, 5);\n                quicklistDelRange(ql, -16, 16);\n                if (fills[f] == 32)\n                    ql_verify(ql, 1, 12, 12, 12);\n                quicklistEntry entry;\n                quicklistIndex(ql, 0, &entry);\n                if (entry.longval != 5)\n                    ERR(\"A: longval not 5, but %lld\", entry.longval);\n                quicklistIndex(ql, -1, &entry);\n                if (entry.longval != 16)\n                    ERR(\"B! got instead: %lld\", entry.longval);\n                quicklistPushTail(ql, \"bobobob\", 7);\n                quicklistIndex(ql, -1, &entry);\n                if (strncmp((char *)entry.value, \"bobobob\", 7))\n                    ERR(\"Tail doesn't match bobobob, it's %.*s instead\",\n                        entry.sz, entry.value);\n                for (int i = 0; i < 12; i++) {\n                    quicklistIndex(ql, i, &entry);\n                    if (entry.longval != nums[5 + i])\n                        ERR(\"Deleted invalid range!  Expected %lld but got \"\n                            \"%lld\",\n                            entry.longval, nums[5 + i]);\n                }\n                quicklistRelease(ql);\n            }\n        }\n\n        TEST_DESC(\"ltrim test C at compress %d\", options[_i]) {\n            for (int f = 0; f < fill_count; f++) {\n                quicklist *ql = quicklistNew(fills[f], options[_i]);\n                char num[32];\n                long long nums[5000];\n                for (int i = 0; i < 33; i++) {\n                    nums[i] = -5157318210846258176 + i;\n                    int sz = ll2string(num, sizeof(num), nums[i]);\n                    quicklistPushTail(ql, num, sz);\n                }\n                if (fills[f] == 32)\n                    ql_verify(ql, 2, 33, 32, 1);\n                /* ltrim 3 3 (keep [3,3] inclusive = 1 remaining) */\n                quicklistDelRange(ql, 0, 3);\n                quicklistDelRange(ql, -29,\n                                  4000); /* make sure not loop forever */\n                if (fills[f] == 32)\n                    ql_verify(ql, 1, 1, 1, 1);\n                quicklistEntry entry;\n                quicklistIndex(ql, 0, &entry);\n                if (entry.longval != -5157318210846258173)\n                    ERROR;\n                quicklistRelease(ql);\n            }\n        }\n\n        TEST_DESC(\"ltrim test D at compress %d\", options[_i]) {\n            for (int f = 0; f < fill_count; f++) {\n                quicklist *ql = quicklistNew(fills[f], options[_i]);\n                char num[32];\n                long long nums[5000];\n                for (int i = 0; i < 33; i++) {\n                    nums[i] = -5157318210846258176 + i;\n                    int sz = ll2string(num, sizeof(num), nums[i]);\n                    quicklistPushTail(ql, num, sz);\n                }\n                if (fills[f] == 32)\n                    ql_verify(ql, 2, 33, 32, 1);\n                quicklistDelRange(ql, -12, 3);\n                if (ql->count != 30)\n                    ERR(\"Didn't delete exactly three elements!  Count is: %lu\",\n                        ql->count);\n                quicklistRelease(ql);\n            }\n        }\n\n        TEST_DESC(\"create quicklist from ziplist at compress %d\", options[_i]) {\n            for (int f = 0; f < fill_count; f++) {\n                unsigned char *zl = ziplistNew();\n                long long nums[64];\n                char num[64];\n                for (int i = 0; i < 33; i++) {\n                    nums[i] = -5157318210846258176 + i;\n                    int sz = ll2string(num, sizeof(num), nums[i]);\n                    zl =\n                        ziplistPush(zl, (unsigned char *)num, sz, ZIPLIST_TAIL);\n                }\n                for (int i = 0; i < 33; i++) {\n                    zl = ziplistPush(zl, (unsigned char *)genstr(\"hello\", i),\n                                     32, ZIPLIST_TAIL);\n                }\n                quicklist *ql = quicklistCreateFromZiplist(fills[f], options[_i], zl);\n                if (fills[f] == 1)\n                    ql_verify(ql, 66, 66, 1, 1);\n                else if (fills[f] == 32)\n                    ql_verify(ql, 3, 66, 32, 2);\n                else if (fills[f] == 66)\n                    ql_verify(ql, 1, 66, 66, 66);\n                quicklistRelease(ql);\n            }\n        }\n\n        long long stop = mstime();\n        runtime[_i] = stop - start;\n    }\n\n    /* Run a longer test of compression depth outside of primary test loop. */\n    int list_sizes[] = {250, 251, 500, 999, 1000};\n    long long start = mstime();\n    int list_count = accurate ? (int)(sizeof(list_sizes) / sizeof(*list_sizes)) : 1;\n    for (int list = 0; list < list_count; list++) {\n        TEST_DESC(\"verify specific compression of interior nodes with %d list \",\n                  list_sizes[list]) {\n            for (int f = 0; f < fill_count; f++) {\n                for (int depth = 1; depth < 40; depth++) {\n                    /* skip over many redundant test cases */\n                    quicklist *ql = quicklistNew(fills[f], depth);\n                    for (int i = 0; i < list_sizes[list]; i++) {\n                        quicklistPushTail(ql, genstr(\"hello TAIL\", i + 1), 64);\n                        quicklistPushHead(ql, genstr(\"hello HEAD\", i + 1), 64);\n                    }\n\n                    for (int step = 0; step < 2; step++) {\n                        /* test remove node */\n                        if (step == 1) {\n                            for (int i = 0; i < list_sizes[list] / 2; i++) {\n                                unsigned char *data;\n                                quicklistPop(ql, QUICKLIST_HEAD, &data, NULL, NULL);\n                                zfree(data);\n                                quicklistPop(ql, QUICKLIST_TAIL, &data, NULL, NULL);\n                                zfree(data);\n                            }\n                        }\n                        quicklistNode *node = ql->head;\n                        unsigned int low_raw = ql->compress;\n                        unsigned int high_raw = ql->len - ql->compress;\n\n                        for (unsigned int at = 0; at < ql->len;\n                            at++, node = node->next) {\n                            if (at < low_raw || at >= high_raw) {\n                                if (node->encoding != QUICKLIST_NODE_ENCODING_RAW) {\n                                    ERR(\"Incorrect compression: node %d is \"\n                                        \"compressed at depth %d ((%u, %u); total \"\n                                        \"nodes: %lu; size: %u)\",\n                                        at, depth, low_raw, high_raw, ql->len,\n                                        node->sz);\n                                }\n                            } else {\n                                if (node->encoding != QUICKLIST_NODE_ENCODING_LZF) {\n                                    ERR(\"Incorrect non-compression: node %d is NOT \"\n                                        \"compressed at depth %d ((%u, %u); total \"\n                                        \"nodes: %lu; size: %u; attempted: %d)\",\n                                        at, depth, low_raw, high_raw, ql->len,\n                                        node->sz, node->attempted_compress);\n                                }\n                            }\n                        }\n                    }\n\n                    quicklistRelease(ql);\n                }\n            }\n        }\n    }\n    long long stop = mstime();\n\n    printf(\"\\n\");\n    for (size_t i = 0; i < option_count; i++)\n        printf(\"Test Loop %02d: %0.2f seconds.\\n\", options[i],\n               (float)runtime[i] / 1000);\n    printf(\"Compressions: %0.2f seconds.\\n\", (float)(stop - start) / 1000);\n    printf(\"\\n\");\n\n    TEST(\"bookmark get updated to next item\") {\n        quicklist *ql = quicklistNew(1, 0);\n        quicklistPushTail(ql, \"1\", 1);\n        quicklistPushTail(ql, \"2\", 1);\n        quicklistPushTail(ql, \"3\", 1);\n        quicklistPushTail(ql, \"4\", 1);\n        quicklistPushTail(ql, \"5\", 1);\n        assert(ql->len==5);\n        /* add two bookmarks, one pointing to the node before the last. */\n        assert(quicklistBookmarkCreate(&ql, \"_dummy\", ql->head->next));\n        assert(quicklistBookmarkCreate(&ql, \"_test\", ql->tail->prev));\n        /* test that the bookmark returns the right node, delete it and see that the bookmark points to the last node */\n        assert(quicklistBookmarkFind(ql, \"_test\") == ql->tail->prev);\n        assert(quicklistDelRange(ql, -2, 1));\n        assert(quicklistBookmarkFind(ql, \"_test\") == ql->tail);\n        /* delete the last node, and see that the bookmark was deleted. */\n        assert(quicklistDelRange(ql, -1, 1));\n        assert(quicklistBookmarkFind(ql, \"_test\") == NULL);\n        /* test that other bookmarks aren't affected */\n        assert(quicklistBookmarkFind(ql, \"_dummy\") == ql->head->next);\n        assert(quicklistBookmarkFind(ql, \"_missing\") == NULL);\n        assert(ql->len==3);\n        quicklistBookmarksClear(ql); /* for coverage */\n        assert(quicklistBookmarkFind(ql, \"_dummy\") == NULL);\n        quicklistRelease(ql);\n    }\n\n    TEST(\"bookmark limit\") {\n        int i;\n        quicklist *ql = quicklistNew(1, 0);\n        quicklistPushHead(ql, \"1\", 1);\n        for (i=0; i<QL_MAX_BM; i++)\n            assert(quicklistBookmarkCreate(&ql, genstr(\"\",i), ql->head));\n        /* when all bookmarks are used, creation fails */\n        assert(!quicklistBookmarkCreate(&ql, \"_test\", ql->head));\n        /* delete one and see that we can now create another */\n        assert(quicklistBookmarkDelete(ql, \"0\"));\n        assert(quicklistBookmarkCreate(&ql, \"_test\", ql->head));\n        /* delete one and see that the rest survive */\n        assert(quicklistBookmarkDelete(ql, \"_test\"));\n        for (i=1; i<QL_MAX_BM; i++)\n            assert(quicklistBookmarkFind(ql, genstr(\"\",i)) == ql->head);\n        /* make sure the deleted ones are indeed gone */\n        assert(!quicklistBookmarkFind(ql, \"0\"));\n        assert(!quicklistBookmarkFind(ql, \"_test\"));\n        quicklistRelease(ql);\n    }\n\n    if (!err)\n        printf(\"ALL TESTS PASSED!\\n\");\n    else\n        ERR(\"Sorry, not all tests passed!  In fact, %d tests failed.\", err);\n\n    return err;\n}\n#endif\n"
  },
  {
    "path": "src/quicklist.h",
    "content": "/* quicklist.h - A generic doubly linked quicklist implementation\n *\n * Copyright (c) 2014, Matt Stancliff <matt@genges.com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this quicklist of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this quicklist of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <stdint.h> // for UINTPTR_MAX\n\n#ifndef __QUICKLIST_H__\n#define __QUICKLIST_H__\n\n#ifdef __cplusplus\n#define ZERO_LENGTH_ARRAY_LENGTH 1\n#else\n#define ZERO_LENGTH_ARRAY_LENGTH\n#endif\n\n/* Node, quicklist, and Iterator are the only data structures used currently. */\n\n/* quicklistNode is a 32 byte struct describing a ziplist for a quicklist.\n * We use bit fields keep the quicklistNode at 32 bytes.\n * count: 16 bits, max 65536 (max zl bytes is 65k, so max count actually < 32k).\n * encoding: 2 bits, RAW=1, LZF=2.\n * container: 2 bits, NONE=1, ZIPLIST=2.\n * recompress: 1 bit, bool, true if node is temporary decompressed for usage.\n * attempted_compress: 1 bit, boolean, used for verifying during testing.\n * extra: 10 bits, free for future use; pads out the remainder of 32 bits */\ntypedef struct quicklistNode {\n    struct quicklistNode *prev;\n    struct quicklistNode *next;\n    unsigned char *zl;\n    unsigned int sz;             /* ziplist size in bytes */\n    unsigned int count : 16;     /* count of items in ziplist */\n    unsigned int encoding : 2;   /* RAW==1 or LZF==2 */\n    unsigned int container : 2;  /* NONE==1 or ZIPLIST==2 */\n    unsigned int recompress : 1; /* was this node previous compressed? */\n    unsigned int attempted_compress : 1; /* node can't compress; too small */\n    unsigned int extra : 10; /* more bits to steal for future usage */\n} quicklistNode;\n\n/* quicklistLZF is a 4+N byte struct holding 'sz' followed by 'compressed'.\n * 'sz' is byte length of 'compressed' field.\n * 'compressed' is LZF data with total (compressed) length 'sz'\n * NOTE: uncompressed length is stored in quicklistNode->sz.\n * When quicklistNode->zl is compressed, node->zl points to a quicklistLZF */\ntypedef struct quicklistLZF {\n    unsigned int sz; /* LZF size in bytes*/\n#ifndef __cplusplus\n    char compressed[];\n#endif\n} quicklistLZF;\n\n/* Bookmarks are padded with realloc at the end of of the quicklist struct.\n * They should only be used for very big lists if thousands of nodes were the\n * excess memory usage is negligible, and there's a real need to iterate on them\n * in portions.\n * When not used, they don't add any memory overhead, but when used and then\n * deleted, some overhead remains (to avoid resonance).\n * The number of bookmarks used should be kept to minimum since it also adds\n * overhead on node deletion (searching for a bookmark to update). */\ntypedef struct quicklistBookmark {\n    quicklistNode *node;\n    char *name;\n} quicklistBookmark;\n\n#if UINTPTR_MAX == 0xffffffff\n/* 32-bit */\n#   define QL_FILL_BITS 14\n#   define QL_COMP_BITS 14\n#   define QL_BM_BITS 4\n#elif UINTPTR_MAX == 0xffffffffffffffff\n/* 64-bit */\n#   define QL_FILL_BITS 16\n#   define QL_COMP_BITS 16\n#   define QL_BM_BITS 4 /* we can encode more, but we rather limit the user\n                           since they cause performance degradation. */\n#else\n#   error unknown arch bits count\n#endif\n\n/* quicklist is a 40 byte struct (on 64-bit systems) describing a quicklist.\n * 'count' is the number of total entries.\n * 'len' is the number of quicklist nodes.\n * 'compress' is: 0 if compression disabled, otherwise it's the number\n *                of quicklistNodes to leave uncompressed at ends of quicklist.\n * 'fill' is the user-requested (or default) fill factor.\n * 'bookmakrs are an optional feature that is used by realloc this struct,\n *      so that they don't consume memory when not used. */\ntypedef struct quicklist {\n    quicklistNode *head;\n    quicklistNode *tail;\n    unsigned long count;        /* total count of all entries in all ziplists */\n    unsigned long len;          /* number of quicklistNodes */\n    int fill : QL_FILL_BITS;              /* fill factor for individual nodes */\n    unsigned int compress : QL_COMP_BITS; /* depth of end nodes not to compress;0=off */\n    unsigned int bookmark_count: QL_BM_BITS;\n#ifndef __cplusplus\n    quicklistBookmark bookmarks[];\n#endif\n} quicklist;\n\ntypedef struct quicklistIter {\n    const quicklist *qlist;\n    quicklistNode *current;\n    unsigned char *zi;\n    long offset; /* offset in current ziplist */\n    int direction;\n} quicklistIter;\n\ntypedef struct quicklistEntry {\n    const quicklist *qlist;\n    quicklistNode *node;\n    unsigned char *zi;\n    unsigned char *value;\n    long long longval;\n    unsigned int sz;\n    int offset;\n} quicklistEntry;\n\n#define QUICKLIST_HEAD 0\n#define QUICKLIST_TAIL -1\n\n/* quicklist node encodings */\n#define QUICKLIST_NODE_ENCODING_RAW 1\n#define QUICKLIST_NODE_ENCODING_LZF 2\n\n/* quicklist compression disable */\n#define QUICKLIST_NOCOMPRESS 0\n\n/* quicklist container formats */\n#define QUICKLIST_NODE_CONTAINER_NONE 1\n#define QUICKLIST_NODE_CONTAINER_ZIPLIST 2\n\n#define quicklistNodeIsCompressed(node)                                        \\\n    ((node)->encoding == QUICKLIST_NODE_ENCODING_LZF)\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/* Prototypes */\nquicklist *quicklistCreate(void);\nquicklist *quicklistNew(int fill, int compress);\nvoid quicklistSetCompressDepth(quicklist *quicklist, int depth);\nvoid quicklistSetFill(quicklist *quicklist, int fill);\nvoid quicklistSetOptions(quicklist *quicklist, int fill, int depth);\nvoid quicklistRelease(quicklist *quicklist);\nint quicklistPushHead(quicklist *quicklist, void *value, const size_t sz);\nint quicklistPushTail(quicklist *quicklist, void *value, const size_t sz);\nvoid quicklistPush(quicklist *quicklist, void *value, const size_t sz,\n                   int where);\nvoid quicklistAppendZiplist(quicklist *quicklist, unsigned char *zl);\nquicklist *quicklistAppendValuesFromZiplist(quicklist *quicklist,\n                                            unsigned char *zl);\nquicklist *quicklistCreateFromZiplist(int fill, int compress,\n                                      unsigned char *zl);\nvoid quicklistInsertAfter(quicklist *quicklist, quicklistEntry *node,\n                          void *value, const size_t sz);\nvoid quicklistInsertBefore(quicklist *quicklist, quicklistEntry *node,\n                           void *value, const size_t sz);\nvoid quicklistDelEntry(quicklistIter *iter, quicklistEntry *entry);\nint quicklistReplaceAtIndex(quicklist *quicklist, long index, void *data,\n                            int sz);\nint quicklistDelRange(quicklist *quicklist, const long start, const long stop);\nquicklistIter *quicklistGetIterator(const quicklist *quicklist, int direction);\nquicklistIter *quicklistGetIteratorAtIdx(const quicklist *quicklist,\n                                         int direction, const long long idx);\nint quicklistNext(quicklistIter *iter, quicklistEntry *node);\nvoid quicklistReleaseIterator(quicklistIter *iter);\nquicklist *quicklistDup(quicklist *orig);\nint quicklistIndex(const quicklist *quicklist, const long long index,\n                   quicklistEntry *entry);\nvoid quicklistRewind(quicklist *quicklist, quicklistIter *li);\nvoid quicklistRewindTail(quicklist *quicklist, quicklistIter *li);\nvoid quicklistRotate(quicklist *quicklist);\nint quicklistPopCustom(quicklist *quicklist, int where, unsigned char **data,\n                       unsigned int *sz, long long *sval,\n                       void *(*saver)(unsigned char *data, unsigned int sz));\nint quicklistPop(quicklist *quicklist, int where, unsigned char **data,\n                 unsigned int *sz, long long *slong);\nunsigned long quicklistCount(const quicklist *ql);\nint quicklistCompare(unsigned char *p1, unsigned char *p2, int p2_len);\nsize_t quicklistGetLzf(const quicklistNode *node, void **data);\n\n/* bookmarks */\nint quicklistBookmarkCreate(quicklist **ql_ref, const char *name, quicklistNode *node);\nint quicklistBookmarkDelete(quicklist *ql, const char *name);\nquicklistNode *quicklistBookmarkFind(quicklist *ql, const char *name);\nvoid quicklistBookmarksClear(quicklist *ql);\n\n#ifdef REDIS_TEST\nint quicklistTest(int argc, char *argv[], int accurate);\n#endif\n\n/* Directions for iterators */\n#define AL_START_HEAD 0\n#define AL_START_TAIL 1\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif /* __QUICKLIST_H__ */\n"
  },
  {
    "path": "src/rand.c",
    "content": "/* Pseudo random number generation functions derived from the drand48()\n * function obtained from pysam source code.\n *\n * This functions are used in order to replace the default math.random()\n * Lua implementation with something having exactly the same behavior\n * across different systems (by default Lua uses libc's rand() that is not\n * required to implement a specific PRNG generating the same sequence\n * in different systems if seeded with the same integer).\n *\n * The original code appears to be under the public domain.\n * I modified it removing the non needed functions and all the\n * 1960-style C coding stuff...\n *\n * ----------------------------------------------------------------------------\n *\n * Copyright (c) 2010-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <stdint.h>\n\n#define N\t16\n#define MASK\t((1 << (N - 1)) + (1 << (N - 1)) - 1)\n#define LOW(x)\t((unsigned)(x) & MASK)\n#define HIGH(x)\tLOW((x) >> N)\n#define MUL(x, y, z)\t{ int32_t l = (long)(x) * (long)(y); \\\n\t\t(z)[0] = LOW(l); (z)[1] = HIGH(l); }\n#define CARRY(x, y)\t((int32_t)(x) + (long)(y) > MASK)\n#define ADDEQU(x, y, z)\t(z = CARRY(x, (y)), x = LOW(x + (y)))\n#define X0\t0x330E\n#define X1\t0xABCD\n#define X2\t0x1234\n#define A0\t0xE66D\n#define A1\t0xDEEC\n#define A2\t0x5\n#define C\t0xB\n#define SET3(x, x0, x1, x2)\t((x)[0] = (x0), (x)[1] = (x1), (x)[2] = (x2))\n#define SETLOW(x, y, n) SET3(x, LOW((y)[n]), LOW((y)[(n)+1]), LOW((y)[(n)+2]))\n#define SEED(x0, x1, x2) (SET3(x, x0, x1, x2), SET3(a, A0, A1, A2), c = C)\n#define REST(v)\tfor (i = 0; i < 3; i++) { xsubi[i] = x[i]; x[i] = temp[i]; } \\\n\t\treturn (v);\n#define HI_BIT\t(1L << (2 * N - 1))\n\nstatic uint32_t x[3] = { X0, X1, X2 }, a[3] = { A0, A1, A2 }, c = C;\nstatic void next(void);\n\nint32_t redisLrand48() {\n    next();\n    return (((int32_t)x[2] << (N - 1)) + (x[1] >> 1));\n}\n\nvoid redisSrand48(int32_t seedval) {\n    SEED(X0, LOW(seedval), HIGH(seedval));\n}\n\nstatic void next(void) {\n    uint32_t p[2], q[2], r[2], carry0, carry1;\n\n    MUL(a[0], x[0], p);\n    ADDEQU(p[0], c, carry0);\n    ADDEQU(p[1], carry0, carry1);\n    MUL(a[0], x[1], q);\n    ADDEQU(p[1], q[0], carry0);\n    MUL(a[1], x[0], r);\n    x[2] = LOW(carry0 + carry1 + CARRY(p[1], r[0]) + q[1] + r[1] +\n            a[0] * x[2] + a[1] * x[1] + a[2] * x[0]);\n    x[1] = LOW(p[1] + r[0]);\n    x[0] = LOW(p[0]);\n}\n"
  },
  {
    "path": "src/rand.h",
    "content": "/*\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef REDIS_RANDOM_H\n#define REDIS_RANDOM_H\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\nint32_t redisLrand48();\nvoid redisSrand48(int32_t seedval);\n\n#ifdef __cplusplus\n}\n#endif\n\n#define REDIS_LRAND48_MAX INT32_MAX\n\n#endif\n"
  },
  {
    "path": "src/rax.c",
    "content": "/* Rax -- A radix tree implementation.\n *\n * Version 1.2 -- 7 February 2019\n *\n * Copyright (c) 2017-2019, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <stdlib.h>\n#include <string.h>\n#include <assert.h>\n#include <stdio.h>\n#include <errno.h>\n#include <math.h>\n#include \"rax.h\"\n\n#ifndef RAX_MALLOC_INCLUDE\n#define RAX_MALLOC_INCLUDE \"rax_malloc.h\"\n#endif\n\n#include RAX_MALLOC_INCLUDE\n\n/* This is a special pointer that is guaranteed to never have the same value\n * of a radix tree node. It's used in order to report \"not found\" error without\n * requiring the function to have multiple return values. */\nvoid *raxNotFound = (void*)\"rax-not-found-pointer\";\n\n/* -------------------------------- Debugging ------------------------------ */\n\nvoid raxDebugShowNode(const char *msg, raxNode *n);\n\n/* Turn debugging messages on/off by compiling with RAX_DEBUG_MSG macro on.\n * When RAX_DEBUG_MSG is defined by default Rax operations will emit a lot\n * of debugging info to the standard output, however you can still turn\n * debugging on/off in order to enable it only when you suspect there is an\n * operation causing a bug using the function raxSetDebugMsg(). */\n#ifdef RAX_DEBUG_MSG\n#define debugf(...)                                                            \\\n    if (raxDebugMsg) {                                                         \\\n        printf(\"%s:%s:%d:\\t\", __FILE__, __func__, __LINE__);                   \\\n        printf(__VA_ARGS__);                                                   \\\n        fflush(stdout);                                                        \\\n    }\n\n#define debugnode(msg,n) raxDebugShowNode(msg,n)\n#else\n#define debugf(...)\n#define debugnode(msg,n)\n#endif\n\n/* By default log debug info if RAX_DEBUG_MSG is defined. */\nstatic int raxDebugMsg = 1;\n\n/* When debug messages are enabled, turn them on/off dynamically. By\n * default they are enabled. Set the state to 0 to disable, and 1 to\n * re-enable. */\nvoid raxSetDebugMsg(int onoff) {\n    raxDebugMsg = onoff;\n}\n\n/* ------------------------- raxStack functions --------------------------\n * The raxStack is a simple stack of pointers that is capable of switching\n * from using a stack-allocated array to dynamic heap once a given number of\n * items are reached. It is used in order to retain the list of parent nodes\n * while walking the radix tree in order to implement certain operations that\n * need to navigate the tree upward.\n * ------------------------------------------------------------------------- */\n\n/* Initialize the stack. */\nstatic inline void raxStackInit(raxStack *ts) {\n    ts->stack = ts->static_items;\n    ts->items = 0;\n    ts->maxitems = RAX_STACK_STATIC_ITEMS;\n    ts->oom = 0;\n}\n\n/* Push an item into the stack, returns 1 on success, 0 on out of memory. */\nstatic inline int raxStackPush(raxStack *ts, void *ptr) {\n    if (ts->items == ts->maxitems) {\n        if (ts->stack == ts->static_items) {\n            ts->stack = rax_malloc(sizeof(void*)*ts->maxitems*2);\n            if (ts->stack == NULL) {\n                ts->stack = ts->static_items;\n                ts->oom = 1;\n                errno = ENOMEM;\n                return 0;\n            }\n            memcpy(ts->stack,ts->static_items,sizeof(void*)*ts->maxitems);\n        } else {\n            void **newalloc = rax_realloc(ts->stack,sizeof(void*)*ts->maxitems*2);\n            if (newalloc == NULL) {\n                ts->oom = 1;\n                errno = ENOMEM;\n                return 0;\n            }\n            ts->stack = newalloc;\n        }\n        ts->maxitems *= 2;\n    }\n    ts->stack[ts->items] = ptr;\n    ts->items++;\n    return 1;\n}\n\n/* Pop an item from the stack, the function returns NULL if there are no\n * items to pop. */\nstatic inline void *raxStackPop(raxStack *ts) {\n    if (ts->items == 0) return NULL;\n    ts->items--;\n    return ts->stack[ts->items];\n}\n\n/* Return the stack item at the top of the stack without actually consuming\n * it. */\nstatic inline void *raxStackPeek(raxStack *ts) {\n    if (ts->items == 0) return NULL;\n    return ts->stack[ts->items-1];\n}\n\n/* Free the stack in case we used heap allocation. */\nstatic inline void raxStackFree(raxStack *ts) {\n    if (ts->stack != ts->static_items) rax_free(ts->stack);\n}\n\n/* ----------------------------------------------------------------------------\n * Radix tree implementation\n * --------------------------------------------------------------------------*/\n\n/* Return the padding needed in the characters section of a node having size\n * 'nodesize'. The padding is needed to store the child pointers to aligned\n * addresses. Note that we add 4 to the node size because the node has a four\n * bytes header. */\n#define raxPadding(nodesize) ((sizeof(void*)-((nodesize+4) % sizeof(void*))) & (sizeof(void*)-1))\n\n/* Return the pointer to the last child pointer in a node. For the compressed\n * nodes this is the only child pointer. */\n#define raxNodeLastChildPtr(n) ((raxNode**) ( \\\n    ((char*)(n)) + \\\n    raxNodeCurrentLength(n) - \\\n    sizeof(raxNode*) - \\\n    (((n)->iskey && !(n)->isnull) ? sizeof(void*) : 0) \\\n))\n\n/* Return the pointer to the first child pointer. */\n#define raxNodeFirstChildPtr(n) ((raxNode**) ( \\\n    (n)->data + \\\n    (n)->size + \\\n    raxPadding((n)->size)))\n\n/* Return the current total size of the node. Note that the second line\n * computes the padding after the string of characters, needed in order to\n * save pointers to aligned addresses. */\n#define raxNodeCurrentLength(n) ( \\\n    sizeof(raxNode)+(n)->size+ \\\n    raxPadding((n)->size)+ \\\n    ((n)->iscompr ? sizeof(raxNode*) : sizeof(raxNode*)*(n)->size)+ \\\n    (((n)->iskey && !(n)->isnull)*sizeof(void*)) \\\n)\n\n/* Allocate a new non compressed node with the specified number of children.\n * If datafiled is true, the allocation is made large enough to hold the\n * associated data pointer.\n * Returns the new node pointer. On out of memory NULL is returned. */\nraxNode *raxNewNode(size_t children, int datafield) {\n    size_t nodesize = sizeof(raxNode)+children+raxPadding(children)+\n                      sizeof(raxNode*)*children;\n    if (datafield) nodesize += sizeof(void*);\n    raxNode *node = rax_malloc(nodesize);\n    if (node == NULL) return NULL;\n    node->iskey = 0;\n    node->isnull = 0;\n    node->iscompr = 0;\n    node->size = children;\n    return node;\n}\n\n/* Allocate a new rax and return its pointer. On out of memory the function\n * returns NULL. */\nrax *raxNew(void) {\n    rax *rax = rax_malloc(sizeof(*rax));\n    if (rax == NULL) return NULL;\n    rax->numele = 0;\n    rax->numnodes = 1;\n    rax->head = raxNewNode(0,0);\n    if (rax->head == NULL) {\n        rax_free(rax);\n        return NULL;\n    } else {\n        return rax;\n    }\n}\n\n/* realloc the node to make room for auxiliary data in order\n * to store an item in that node. On out of memory NULL is returned. */\nraxNode *raxReallocForData(raxNode *n, void *data) {\n    if (data == NULL) return n; /* No reallocation needed, setting isnull=1 */\n    size_t curlen = raxNodeCurrentLength(n);\n    return rax_realloc(n,curlen+sizeof(void*));\n}\n\n/* Set the node auxiliary data to the specified pointer. */\nvoid raxSetData(raxNode *n, void *data) {\n    n->iskey = 1;\n    if (data != NULL) {\n        n->isnull = 0;\n        void **ndata = (void**)\n            ((char*)n+raxNodeCurrentLength(n)-sizeof(void*));\n        memcpy(ndata,&data,sizeof(data));\n    } else {\n        n->isnull = 1;\n    }\n}\n\n/* Get the node auxiliary data. */\nvoid *raxGetData(raxNode *n) {\n    if (n->isnull) return NULL;\n    void **ndata =(void**)((char*)n+raxNodeCurrentLength(n)-sizeof(void*));\n    void *data;\n    memcpy(&data,ndata,sizeof(data));\n    return data;\n}\n\n/* Add a new child to the node 'n' representing the character 'c' and return\n * its new pointer, as well as the child pointer by reference. Additionally\n * '***parentlink' is populated with the raxNode pointer-to-pointer of where\n * the new child was stored, which is useful for the caller to replace the\n * child pointer if it gets reallocated.\n *\n * On success the new parent node pointer is returned (it may change because\n * of the realloc, so the caller should discard 'n' and use the new value).\n * On out of memory NULL is returned, and the old node is still valid. */\nraxNode *raxAddChild(raxNode *n, unsigned char c, raxNode **childptr, raxNode ***parentlink) {\n    assert(n->iscompr == 0);\n\n    size_t curlen = raxNodeCurrentLength(n);\n    n->size++;\n    size_t newlen = raxNodeCurrentLength(n);\n    n->size--; /* For now restore the orignal size. We'll update it only on\n                  success at the end. */\n\n    /* Alloc the new child we will link to 'n'. */\n    raxNode *child = raxNewNode(0,0);\n    if (child == NULL) return NULL;\n\n    /* Make space in the original node. */\n    raxNode *newn = rax_realloc(n,newlen);\n    if (newn == NULL) {\n        rax_free(child);\n        return NULL;\n    }\n    n = newn;\n\n    /* After the reallocation, we have up to 8/16 (depending on the system\n     * pointer size, and the required node padding) bytes at the end, that is,\n     * the additional char in the 'data' section, plus one pointer to the new\n     * child, plus the padding needed in order to store addresses into aligned\n     * locations.\n     *\n     * So if we start with the following node, having \"abde\" edges.\n     *\n     * Note:\n     * - We assume 4 bytes pointer for simplicity.\n     * - Each space below corresponds to one byte\n     *\n     * [HDR*][abde][Aptr][Bptr][Dptr][Eptr]|AUXP|\n     *\n     * After the reallocation we need: 1 byte for the new edge character\n     * plus 4 bytes for a new child pointer (assuming 32 bit machine).\n     * However after adding 1 byte to the edge char, the header + the edge\n     * characters are no longer aligned, so we also need 3 bytes of padding.\n     * In total the reallocation will add 1+4+3 bytes = 8 bytes:\n     *\n     * (Blank bytes are represented by \".\")\n     *\n     * [HDR*][abde][Aptr][Bptr][Dptr][Eptr]|AUXP|[....][....]\n     *\n     * Let's find where to insert the new child in order to make sure\n     * it is inserted in-place lexicographically. Assuming we are adding\n     * a child \"c\" in our case pos will be = 2 after the end of the following\n     * loop. */\n    int pos;\n    for (pos = 0; pos < n->size; pos++) {\n        if (n->data[pos] > c) break;\n    }\n\n    /* Now, if present, move auxiliary data pointer at the end\n     * so that we can mess with the other data without overwriting it.\n     * We will obtain something like that:\n     *\n     * [HDR*][abde][Aptr][Bptr][Dptr][Eptr][....][....]|AUXP|\n     */\n    unsigned char *src, *dst;\n    if (n->iskey && !n->isnull) {\n        src = ((unsigned char*)n+curlen-sizeof(void*));\n        dst = ((unsigned char*)n+newlen-sizeof(void*));\n        memmove(dst,src,sizeof(void*));\n    }\n\n    /* Compute the \"shift\", that is, how many bytes we need to move the\n     * pointers section forward because of the addition of the new child\n     * byte in the string section. Note that if we had no padding, that\n     * would be always \"1\", since we are adding a single byte in the string\n     * section of the node (where now there is \"abde\" basically).\n     *\n     * However we have padding, so it could be zero, or up to 8.\n     *\n     * Another way to think at the shift is, how many bytes we need to\n     * move child pointers forward *other than* the obvious sizeof(void*)\n     * needed for the additional pointer itself. */\n    size_t shift = newlen - curlen - sizeof(void*);\n\n    /* We said we are adding a node with edge 'c'. The insertion\n     * point is between 'b' and 'd', so the 'pos' variable value is\n     * the index of the first child pointer that we need to move forward\n     * to make space for our new pointer.\n     *\n     * To start, move all the child pointers after the insertion point\n     * of shift+sizeof(pointer) bytes on the right, to obtain:\n     *\n     * [HDR*][abde][Aptr][Bptr][....][....][Dptr][Eptr]|AUXP|\n     */\n    src = n->data+n->size+\n          raxPadding(n->size)+\n          sizeof(raxNode*)*pos;\n    memmove(src+shift+sizeof(raxNode*),src,sizeof(raxNode*)*(n->size-pos));\n\n    /* Move the pointers to the left of the insertion position as well. Often\n     * we don't need to do anything if there was already some padding to use. In\n     * that case the final destination of the pointers will be the same, however\n     * in our example there was no pre-existing padding, so we added one byte\n     * plus thre bytes of padding. After the next memmove() things will look\n     * like thata:\n     *\n     * [HDR*][abde][....][Aptr][Bptr][....][Dptr][Eptr]|AUXP|\n     */\n    if (shift) {\n        src = (unsigned char*) raxNodeFirstChildPtr(n);\n        memmove(src+shift,src,sizeof(raxNode*)*pos);\n    }\n\n    /* Now make the space for the additional char in the data section,\n     * but also move the pointers before the insertion point to the right\n     * by shift bytes, in order to obtain the following:\n     *\n     * [HDR*][ab.d][e...][Aptr][Bptr][....][Dptr][Eptr]|AUXP|\n     */\n    src = n->data+pos;\n    memmove(src+1,src,n->size-pos);\n\n    /* We can now set the character and its child node pointer to get:\n     *\n     * [HDR*][abcd][e...][Aptr][Bptr][....][Dptr][Eptr]|AUXP|\n     * [HDR*][abcd][e...][Aptr][Bptr][Cptr][Dptr][Eptr]|AUXP|\n     */\n    n->data[pos] = c;\n    n->size++;\n    src = (unsigned char*) raxNodeFirstChildPtr(n);\n    raxNode **childfield = (raxNode**)(src+sizeof(raxNode*)*pos);\n    memcpy(childfield,&child,sizeof(child));\n    *childptr = child;\n    *parentlink = childfield;\n    return n;\n}\n\n/* Turn the node 'n', that must be a node without any children, into a\n * compressed node representing a set of nodes linked one after the other\n * and having exactly one child each. The node can be a key or not: this\n * property and the associated value if any will be preserved.\n *\n * The function also returns a child node, since the last node of the\n * compressed chain cannot be part of the chain: it has zero children while\n * we can only compress inner nodes with exactly one child each. */\nraxNode *raxCompressNode(raxNode *n, unsigned char *s, size_t len, raxNode **child) {\n    assert(n->size == 0 && n->iscompr == 0);\n    void *data = NULL; /* Initialized only to avoid warnings. */\n    size_t newsize;\n\n    debugf(\"Compress node: %.*s\\n\", (int)len,s);\n\n    /* Allocate the child to link to this node. */\n    *child = raxNewNode(0,0);\n    if (*child == NULL) return NULL;\n\n    /* Make space in the parent node. */\n    newsize = sizeof(raxNode)+len+raxPadding(len)+sizeof(raxNode*);\n    if (n->iskey) {\n        data = raxGetData(n); /* To restore it later. */\n        if (!n->isnull) newsize += sizeof(void*);\n    }\n    raxNode *newn = rax_realloc(n,newsize);\n    if (newn == NULL) {\n        rax_free(*child);\n        return NULL;\n    }\n    n = newn;\n\n    n->iscompr = 1;\n    n->size = len;\n    memcpy(n->data,s,len);\n    if (n->iskey) raxSetData(n,data);\n    raxNode **childfield = raxNodeLastChildPtr(n);\n    memcpy(childfield,child,sizeof(*child));\n    return n;\n}\n\n/* Low level function that walks the tree looking for the string\n * 's' of 'len' bytes. The function returns the number of characters\n * of the key that was possible to process: if the returned integer\n * is the same as 'len', then it means that the node corresponding to the\n * string was found (however it may not be a key in case the node->iskey is\n * zero or if simply we stopped in the middle of a compressed node, so that\n * 'splitpos' is non zero).\n *\n * Otherwise if the returned integer is not the same as 'len', there was an\n * early stop during the tree walk because of a character mismatch.\n *\n * The node where the search ended (because the full string was processed\n * or because there was an early stop) is returned by reference as\n * '*stopnode' if the passed pointer is not NULL. This node link in the\n * parent's node is returned as '*plink' if not NULL. Finally, if the\n * search stopped in a compressed node, '*splitpos' returns the index\n * inside the compressed node where the search ended. This is useful to\n * know where to split the node for insertion.\n *\n * Note that when we stop in the middle of a compressed node with\n * a perfect match, this function will return a length equal to the\n * 'len' argument (all the key matched), and will return a *splitpos which is\n * always positive (that will represent the index of the character immediately\n * *after* the last match in the current compressed node).\n *\n * When instead we stop at a compressed node and *splitpos is zero, it\n * means that the current node represents the key (that is, none of the\n * compressed node characters are needed to represent the key, just all\n * its parents nodes). */\nstatic inline size_t raxLowWalk(rax *rax, unsigned char *s, size_t len, raxNode **stopnode, raxNode ***plink, int *splitpos, raxStack *ts) {\n    raxNode *h = rax->head;\n    raxNode **parentlink = &rax->head;\n\n    size_t i = 0; /* Position in the string. */\n    size_t j = 0; /* Position in the node children (or bytes if compressed).*/\n    while(h->size && i < len) {\n        debugnode(\"Lookup current node\",h);\n        unsigned char *v = h->data;\n\n        if (h->iscompr) {\n            for (j = 0; j < h->size && i < len; j++, i++) {\n                if (v[j] != s[i]) break;\n            }\n            if (j != h->size) break;\n        } else {\n            /* Even when h->size is large, linear scan provides good\n             * performances compared to other approaches that are in theory\n             * more sounding, like performing a binary search. */\n            for (j = 0; j < h->size; j++) {\n                if (v[j] == s[i]) break;\n            }\n            if (j == h->size) break;\n            i++;\n        }\n\n        if (ts) raxStackPush(ts,h); /* Save stack of parent nodes. */\n        raxNode **children = raxNodeFirstChildPtr(h);\n        if (h->iscompr) j = 0; /* Compressed node only child is at index 0. */\n        memcpy(&h,children+j,sizeof(h));\n        parentlink = children+j;\n        j = 0; /* If the new node is non compressed and we do not\n                  iterate again (since i == len) set the split\n                  position to 0 to signal this node represents\n                  the searched key. */\n    }\n    debugnode(\"Lookup stop node is\",h);\n    if (stopnode) *stopnode = h;\n    if (plink) *plink = parentlink;\n    if (splitpos && h->iscompr) *splitpos = j;\n    return i;\n}\n\n/* Insert the element 's' of size 'len', setting as auxiliary data\n * the pointer 'data'. If the element is already present, the associated\n * data is updated (only if 'overwrite' is set to 1), and 0 is returned,\n * otherwise the element is inserted and 1 is returned. On out of memory the\n * function returns 0 as well but sets errno to ENOMEM, otherwise errno will\n * be set to 0.\n */\nint raxGenericInsert(rax *rax, unsigned char *s, size_t len, void *data, void **old, int overwrite) {\n    size_t i;\n    int j = 0; /* Split position. If raxLowWalk() stops in a compressed\n                  node, the index 'j' represents the char we stopped within the\n                  compressed node, that is, the position where to split the\n                  node for insertion. */\n    raxNode *h, **parentlink;\n\n    debugf(\"### Insert %.*s with value %p\\n\", (int)len, s, data);\n    i = raxLowWalk(rax,s,len,&h,&parentlink,&j,NULL);\n\n    /* If i == len we walked following the whole string. If we are not\n     * in the middle of a compressed node, the string is either already\n     * inserted or this middle node is currently not a key, but can represent\n     * our key. We have just to reallocate the node and make space for the\n     * data pointer. */\n    if (i == len && (!h->iscompr || j == 0 /* not in the middle if j is 0 */)) {\n        debugf(\"### Insert: node representing key exists\\n\");\n        /* Make space for the value pointer if needed. */\n        if (!h->iskey || (h->isnull && overwrite)) {\n            h = raxReallocForData(h,data);\n            if (h) memcpy(parentlink,&h,sizeof(h));\n        }\n        if (h == NULL) {\n            errno = ENOMEM;\n            return 0;\n        }\n\n        /* Update the existing key if there is already one. */\n        if (h->iskey) {\n            if (old) *old = raxGetData(h);\n            if (overwrite) raxSetData(h,data);\n            errno = 0;\n            return 0; /* Element already exists. */\n        }\n\n        /* Otherwise set the node as a key. Note that raxSetData()\n         * will set h->iskey. */\n        raxSetData(h,data);\n        rax->numele++;\n        return 1; /* Element inserted. */\n    }\n\n    /* If the node we stopped at is a compressed node, we need to\n     * split it before to continue.\n     *\n     * Splitting a compressed node have a few possible cases.\n     * Imagine that the node 'h' we are currently at is a compressed\n     * node containing the string \"ANNIBALE\" (it means that it represents\n     * nodes A -> N -> N -> I -> B -> A -> L -> E with the only child\n     * pointer of this node pointing at the 'E' node, because remember that\n     * we have characters at the edges of the graph, not inside the nodes\n     * themselves.\n     *\n     * In order to show a real case imagine our node to also point to\n     * another compressed node, that finally points at the node without\n     * children, representing 'O':\n     *\n     *     \"ANNIBALE\" -> \"SCO\" -> []\n     *\n     * When inserting we may face the following cases. Note that all the cases\n     * require the insertion of a non compressed node with exactly two\n     * children, except for the last case which just requires splitting a\n     * compressed node.\n     *\n     * 1) Inserting \"ANNIENTARE\"\n     *\n     *               |B| -> \"ALE\" -> \"SCO\" -> []\n     *     \"ANNI\" -> |-|\n     *               |E| -> (... continue algo ...) \"NTARE\" -> []\n     *\n     * 2) Inserting \"ANNIBALI\"\n     *\n     *                  |E| -> \"SCO\" -> []\n     *     \"ANNIBAL\" -> |-|\n     *                  |I| -> (... continue algo ...) []\n     *\n     * 3) Inserting \"AGO\" (Like case 1, but set iscompr = 0 into original node)\n     *\n     *            |N| -> \"NIBALE\" -> \"SCO\" -> []\n     *     |A| -> |-|\n     *            |G| -> (... continue algo ...) |O| -> []\n     *\n     * 4) Inserting \"CIAO\"\n     *\n     *     |A| -> \"NNIBALE\" -> \"SCO\" -> []\n     *     |-|\n     *     |C| -> (... continue algo ...) \"IAO\" -> []\n     *\n     * 5) Inserting \"ANNI\"\n     *\n     *     \"ANNI\" -> \"BALE\" -> \"SCO\" -> []\n     *\n     * The final algorithm for insertion covering all the above cases is as\n     * follows.\n     *\n     * ============================= ALGO 1 =============================\n     *\n     * For the above cases 1 to 4, that is, all cases where we stopped in\n     * the middle of a compressed node for a character mismatch, do:\n     *\n     * Let $SPLITPOS be the zero-based index at which, in the\n     * compressed node array of characters, we found the mismatching\n     * character. For example if the node contains \"ANNIBALE\" and we add\n     * \"ANNIENTARE\" the $SPLITPOS is 4, that is, the index at which the\n     * mismatching character is found.\n     *\n     * 1. Save the current compressed node $NEXT pointer (the pointer to the\n     *    child element, that is always present in compressed nodes).\n     *\n     * 2. Create \"split node\" having as child the non common letter\n     *    at the compressed node. The other non common letter (at the key)\n     *    will be added later as we continue the normal insertion algorithm\n     *    at step \"6\".\n     *\n     * 3a. IF $SPLITPOS == 0:\n     *     Replace the old node with the split node, by copying the auxiliary\n     *     data if any. Fix parent's reference. Free old node eventually\n     *     (we still need its data for the next steps of the algorithm).\n     *\n     * 3b. IF $SPLITPOS != 0:\n     *     Trim the compressed node (reallocating it as well) in order to\n     *     contain $splitpos characters. Change child pointer in order to link\n     *     to the split node. If new compressed node len is just 1, set\n     *     iscompr to 0 (layout is the same). Fix parent's reference.\n     *\n     * 4a. IF the postfix len (the length of the remaining string of the\n     *     original compressed node after the split character) is non zero,\n     *     create a \"postfix node\". If the postfix node has just one character\n     *     set iscompr to 0, otherwise iscompr to 1. Set the postfix node\n     *     child pointer to $NEXT.\n     *\n     * 4b. IF the postfix len is zero, just use $NEXT as postfix pointer.\n     *\n     * 5. Set child[0] of split node to postfix node.\n     *\n     * 6. Set the split node as the current node, set current index at child[1]\n     *    and continue insertion algorithm as usually.\n     *\n     * ============================= ALGO 2 =============================\n     *\n     * For case 5, that is, if we stopped in the middle of a compressed\n     * node but no mismatch was found, do:\n     *\n     * Let $SPLITPOS be the zero-based index at which, in the\n     * compressed node array of characters, we stopped iterating because\n     * there were no more keys character to match. So in the example of\n     * the node \"ANNIBALE\", addig the string \"ANNI\", the $SPLITPOS is 4.\n     *\n     * 1. Save the current compressed node $NEXT pointer (the pointer to the\n     *    child element, that is always present in compressed nodes).\n     *\n     * 2. Create a \"postfix node\" containing all the characters from $SPLITPOS\n     *    to the end. Use $NEXT as the postfix node child pointer.\n     *    If the postfix node length is 1, set iscompr to 0.\n     *    Set the node as a key with the associated value of the new\n     *    inserted key.\n     *\n     * 3. Trim the current node to contain the first $SPLITPOS characters.\n     *    As usually if the new node length is just 1, set iscompr to 0.\n     *    Take the iskey / associated value as it was in the orignal node.\n     *    Fix the parent's reference.\n     *\n     * 4. Set the postfix node as the only child pointer of the trimmed\n     *    node created at step 1.\n     */\n\n    /* ------------------------- ALGORITHM 1 --------------------------- */\n    if (h->iscompr && i != len) {\n        debugf(\"ALGO 1: Stopped at compressed node %.*s (%p)\\n\",\n            h->size, h->data, (void*)h);\n        debugf(\"Still to insert: %.*s\\n\", (int)(len-i), s+i);\n        debugf(\"Splitting at %d: '%c'\\n\", j, ((char*)h->data)[j]);\n        debugf(\"Other (key) letter is '%c'\\n\", s[i]);\n\n        /* 1: Save next pointer. */\n        raxNode **childfield = raxNodeLastChildPtr(h);\n        raxNode *next;\n        memcpy(&next,childfield,sizeof(next));\n        debugf(\"Next is %p\\n\", (void*)next);\n        debugf(\"iskey %d\\n\", h->iskey);\n        if (h->iskey) {\n            debugf(\"key value is %p\\n\", raxGetData(h));\n        }\n\n        /* Set the length of the additional nodes we will need. */\n        size_t trimmedlen = j;\n        size_t postfixlen = h->size - j - 1;\n        int split_node_is_key = !trimmedlen && h->iskey && !h->isnull;\n        size_t nodesize;\n\n        /* 2: Create the split node. Also allocate the other nodes we'll need\n         *    ASAP, so that it will be simpler to handle OOM. */\n        raxNode *splitnode = raxNewNode(1, split_node_is_key);\n        raxNode *trimmed = NULL;\n        raxNode *postfix = NULL;\n\n        if (trimmedlen) {\n            nodesize = sizeof(raxNode)+trimmedlen+raxPadding(trimmedlen)+\n                       sizeof(raxNode*);\n            if (h->iskey && !h->isnull) nodesize += sizeof(void*);\n            trimmed = zcalloc(nodesize, MALLOC_LOCAL);//rax_malloc(nodesize);\n        }\n\n        if (postfixlen) {\n            nodesize = sizeof(raxNode)+postfixlen+raxPadding(postfixlen)+\n                       sizeof(raxNode*);\n            postfix = rax_malloc(nodesize);\n        }\n\n        /* OOM? Abort now that the tree is untouched. */\n        if (splitnode == NULL ||\n            (trimmedlen && trimmed == NULL) ||\n            (postfixlen && postfix == NULL))\n        {\n            rax_free(splitnode);\n            rax_free(trimmed);\n            rax_free(postfix);\n            errno = ENOMEM;\n            return 0;\n        }\n        splitnode->data[0] = h->data[j];\n\n        if (j == 0) {\n            /* 3a: Replace the old node with the split node. */\n            if (h->iskey) {\n                void *ndata = raxGetData(h);\n                raxSetData(splitnode,ndata);\n            }\n            memcpy(parentlink,&splitnode,sizeof(splitnode));\n        } else {\n            /* 3b: Trim the compressed node. */\n            trimmed->size = j;\n            memcpy(trimmed->data,h->data,j);\n            trimmed->iscompr = j > 1 ? 1 : 0;\n            trimmed->iskey = h->iskey;\n            trimmed->isnull = h->isnull;\n            if (h->iskey && !h->isnull) {\n                void *ndata = raxGetData(h);\n                raxSetData(trimmed,ndata);\n            }\n            raxNode **cp = raxNodeLastChildPtr(trimmed);\n            memcpy(cp,&splitnode,sizeof(splitnode));\n            memcpy(parentlink,&trimmed,sizeof(trimmed));\n            parentlink = cp; /* Set parentlink to splitnode parent. */\n            rax->numnodes++;\n        }\n\n        /* 4: Create the postfix node: what remains of the original\n         * compressed node after the split. */\n        if (postfixlen) {\n            /* 4a: create a postfix node. */\n            postfix->iskey = 0;\n            postfix->isnull = 0;\n            postfix->size = postfixlen;\n            postfix->iscompr = postfixlen > 1;\n            memcpy(postfix->data,h->data+j+1,postfixlen);\n            raxNode **cp = raxNodeLastChildPtr(postfix);\n            memcpy(cp,&next,sizeof(next));\n            rax->numnodes++;\n        } else {\n            /* 4b: just use next as postfix node. */\n            postfix = next;\n        }\n\n        /* 5: Set splitnode first child as the postfix node. */\n        raxNode **splitchild = raxNodeLastChildPtr(splitnode);\n        memcpy(splitchild,&postfix,sizeof(postfix));\n\n        /* 6. Continue insertion: this will cause the splitnode to\n         * get a new child (the non common character at the currently\n         * inserted key). */\n        rax_free(h);\n        h = splitnode;\n    } else if (h->iscompr && i == len) {\n    /* ------------------------- ALGORITHM 2 --------------------------- */\n        debugf(\"ALGO 2: Stopped at compressed node %.*s (%p) j = %d\\n\",\n            h->size, h->data, (void*)h, j);\n\n        /* Allocate postfix & trimmed nodes ASAP to fail for OOM gracefully. */\n        size_t postfixlen = h->size - j;\n        size_t nodesize = sizeof(raxNode)+postfixlen+raxPadding(postfixlen)+\n                          sizeof(raxNode*);\n        if (data != NULL) nodesize += sizeof(void*);\n        raxNode *postfix = rax_malloc(nodesize);\n\n        nodesize = sizeof(raxNode)+j+raxPadding(j)+sizeof(raxNode*);\n        if (h->iskey && !h->isnull) nodesize += sizeof(void*);\n        raxNode *trimmed = rax_malloc(nodesize);\n\n        if (postfix == NULL || trimmed == NULL) {\n            rax_free(postfix);\n            rax_free(trimmed);\n            errno = ENOMEM;\n            return 0;\n        }\n\n        /* 1: Save next pointer. */\n        raxNode **childfield = raxNodeLastChildPtr(h);\n        raxNode *next;\n        memcpy(&next,childfield,sizeof(next));\n\n        /* 2: Create the postfix node. */\n        postfix->size = postfixlen;\n        postfix->iscompr = postfixlen > 1;\n        postfix->iskey = 1;\n        postfix->isnull = 0;\n        memcpy(postfix->data,h->data+j,postfixlen);\n        raxSetData(postfix,data);\n        raxNode **cp = raxNodeLastChildPtr(postfix);\n        memcpy(cp,&next,sizeof(next));\n        rax->numnodes++;\n\n        /* 3: Trim the compressed node. */\n        trimmed->size = j;\n        trimmed->iscompr = j > 1;\n        trimmed->iskey = 0;\n        trimmed->isnull = 0;\n        memcpy(trimmed->data,h->data,j);\n        memcpy(parentlink,&trimmed,sizeof(trimmed));\n        if (h->iskey) {\n            void *aux = raxGetData(h);\n            raxSetData(trimmed,aux);\n        }\n\n        /* Fix the trimmed node child pointer to point to\n         * the postfix node. */\n        cp = raxNodeLastChildPtr(trimmed);\n        memcpy(cp,&postfix,sizeof(postfix));\n\n        /* Finish! We don't need to continue with the insertion\n         * algorithm for ALGO 2. The key is already inserted. */\n        rax->numele++;\n        rax_free(h);\n        return 1; /* Key inserted. */\n    }\n\n    /* We walked the radix tree as far as we could, but still there are left\n     * chars in our string. We need to insert the missing nodes. */\n    while(i < len) {\n        raxNode *child;\n\n        /* If this node is going to have a single child, and there\n         * are other characters, so that that would result in a chain\n         * of single-childed nodes, turn it into a compressed node. */\n        if (h->size == 0 && len-i > 1) {\n            debugf(\"Inserting compressed node\\n\");\n            size_t comprsize = len-i;\n            if (comprsize > RAX_NODE_MAX_SIZE)\n                comprsize = RAX_NODE_MAX_SIZE;\n            raxNode *newh = raxCompressNode(h,s+i,comprsize,&child);\n            if (newh == NULL) goto oom;\n            h = newh;\n            memcpy(parentlink,&h,sizeof(h));\n            parentlink = raxNodeLastChildPtr(h);\n            i += comprsize;\n        } else {\n            debugf(\"Inserting normal node\\n\");\n            raxNode **new_parentlink;\n            raxNode *newh = raxAddChild(h,s[i],&child,&new_parentlink);\n            if (newh == NULL) goto oom;\n            h = newh;\n            memcpy(parentlink,&h,sizeof(h));\n            parentlink = new_parentlink;\n            i++;\n        }\n        rax->numnodes++;\n        h = child;\n    }\n    raxNode *newh = raxReallocForData(h,data);\n    if (newh == NULL) goto oom;\n    h = newh;\n    if (!h->iskey) rax->numele++;\n    raxSetData(h,data);\n    memcpy(parentlink,&h,sizeof(h));\n    return 1; /* Element inserted. */\n\noom:\n    /* This code path handles out of memory after part of the sub-tree was\n     * already modified. Set the node as a key, and then remove it. However we\n     * do that only if the node is a terminal node, otherwise if the OOM\n     * happened reallocating a node in the middle, we don't need to free\n     * anything. */\n    if (h->size == 0) {\n        h->isnull = 1;\n        h->iskey = 1;\n        rax->numele++; /* Compensate the next remove. */\n        assert(raxRemove(rax,s,i,NULL) != 0);\n    }\n    errno = ENOMEM;\n    return 0;\n}\n\n/* Overwriting insert. Just a wrapper for raxGenericInsert() that will\n * update the element if there is already one for the same key. */\nint raxInsert(rax *rax, unsigned char *s, size_t len, void *data, void **old) {\n    return raxGenericInsert(rax,s,len,data,old,1);\n}\n\n/* Non overwriting insert function: this if an element with the same key\n * exists, the value is not updated and the function returns 0.\n * This is a just a wrapper for raxGenericInsert(). */\nint raxTryInsert(rax *rax, unsigned char *s, size_t len, void *data, void **old) {\n    return raxGenericInsert(rax,s,len,data,old,0);\n}\n\n/* Find a key in the rax, returns raxNotFound special void pointer value\n * if the item was not found, otherwise the value associated with the\n * item is returned. */\nvoid *raxFind(rax *rax, unsigned char *s, size_t len) {\n    raxNode *h;\n\n    debugf(\"### Lookup: %.*s\\n\", (int)len, s);\n    int splitpos = 0;\n    size_t i = raxLowWalk(rax,s,len,&h,NULL,&splitpos,NULL);\n    if (i != len || (h->iscompr && splitpos != 0) || !h->iskey)\n        return raxNotFound;\n    return raxGetData(h);\n}\n\n/* Return the memory address where the 'parent' node stores the specified\n * 'child' pointer, so that the caller can update the pointer with another\n * one if needed. The function assumes it will find a match, otherwise the\n * operation is an undefined behavior (it will continue scanning the\n * memory without any bound checking). */\nraxNode **raxFindParentLink(raxNode *parent, raxNode *child) {\n    raxNode **cp = raxNodeFirstChildPtr(parent);\n    raxNode *c;\n    while(1) {\n        memcpy(&c,cp,sizeof(c));\n        if (c == child) break;\n        cp++;\n    }\n    return cp;\n}\n\n/* Low level child removal from node. The new node pointer (after the child\n * removal) is returned. Note that this function does not fix the pointer\n * of the parent node in its parent, so this task is up to the caller.\n * The function never fails for out of memory. */\nraxNode *raxRemoveChild(raxNode *parent, raxNode *child) {\n    debugnode(\"raxRemoveChild before\", parent);\n    /* If parent is a compressed node (having a single child, as for definition\n     * of the data structure), the removal of the child consists into turning\n     * it into a normal node without children. */\n    if (parent->iscompr) {\n        void *data = NULL;\n        if (parent->iskey) data = raxGetData(parent);\n        parent->isnull = 0;\n        parent->iscompr = 0;\n        parent->size = 0;\n        if (parent->iskey) raxSetData(parent,data);\n        debugnode(\"raxRemoveChild after\", parent);\n        return parent;\n    }\n\n    /* Otherwise we need to scan for the child pointer and memmove()\n     * accordingly.\n     *\n     * 1. To start we seek the first element in both the children\n     *    pointers and edge bytes in the node. */\n    raxNode **cp = raxNodeFirstChildPtr(parent);\n    raxNode **c = cp;\n    unsigned char *e = parent->data;\n\n    /* 2. Search the child pointer to remove inside the array of children\n     *    pointers. */\n    while(1) {\n        raxNode *aux;\n        memcpy(&aux,c,sizeof(aux));\n        if (aux == child) break;\n        c++;\n        e++;\n    }\n\n    /* 3. Remove the edge and the pointer by memmoving the remaining children\n     *    pointer and edge bytes one position before. */\n    int taillen = parent->size - (e - parent->data) - 1;\n    debugf(\"raxRemoveChild tail len: %d\\n\", taillen);\n    memmove(e,e+1,taillen);\n\n    /* Compute the shift, that is the amount of bytes we should move our\n     * child pointers to the left, since the removal of one edge character\n     * and the corresponding padding change, may change the layout.\n     * We just check if in the old version of the node there was at the\n     * end just a single byte and all padding: in that case removing one char\n     * will remove a whole sizeof(void*) word. */\n    size_t shift = ((parent->size+4) % sizeof(void*)) == 1 ? sizeof(void*) : 0;\n\n    /* Move the children pointers before the deletion point. */\n    if (shift)\n        memmove(((char*)cp)-shift,cp,(parent->size-taillen-1)*sizeof(raxNode**));\n\n    /* Move the remaining \"tail\" pointers at the right position as well. */\n    size_t valuelen = (parent->iskey && !parent->isnull) ? sizeof(void*) : 0;\n    memmove(((char*)c)-shift,c+1,taillen*sizeof(raxNode**)+valuelen);\n\n    /* 4. Update size. */\n    parent->size--;\n\n    /* realloc the node according to the theoretical memory usage, to free\n     * data if we are over-allocating right now. */\n    raxNode *newnode = rax_realloc(parent,raxNodeCurrentLength(parent));\n    if (newnode) {\n        debugnode(\"raxRemoveChild after\", newnode);\n    }\n    /* Note: if rax_realloc() fails we just return the old address, which\n     * is valid. */\n    return newnode ? newnode : parent;\n}\n\n/* Remove the specified item. Returns 1 if the item was found and\n * deleted, 0 otherwise. */\nint raxRemove(rax *rax, unsigned char *s, size_t len, void **old) {\n    raxNode *h;\n    raxStack ts;\n\n    debugf(\"### Delete: %.*s\\n\", (int)len, s);\n    raxStackInit(&ts);\n    int splitpos = 0;\n    size_t i = raxLowWalk(rax,s,len,&h,NULL,&splitpos,&ts);\n    if (i != len || (h->iscompr && splitpos != 0) || !h->iskey) {\n        raxStackFree(&ts);\n        return 0;\n    }\n    if (old) *old = raxGetData(h);\n    h->iskey = 0;\n    rax->numele--;\n\n    /* If this node has no children, the deletion needs to reclaim the\n     * no longer used nodes. This is an iterative process that needs to\n     * walk the three upward, deleting all the nodes with just one child\n     * that are not keys, until the head of the rax is reached or the first\n     * node with more than one child is found. */\n\n    int trycompress = 0; /* Will be set to 1 if we should try to optimize the\n                            tree resulting from the deletion. */\n\n    if (h->size == 0) {\n        debugf(\"Key deleted in node without children. Cleanup needed.\\n\");\n        raxNode *child = NULL;\n        while(h != rax->head) {\n            child = h;\n            debugf(\"Freeing child %p [%.*s] key:%d\\n\", (void*)child,\n                (int)child->size, (char*)child->data, child->iskey);\n            rax_free(child);\n            rax->numnodes--;\n            h = raxStackPop(&ts);\n             /* If this node has more then one child, or actually holds\n              * a key, stop here. */\n            if (h->iskey || (!h->iscompr && h->size != 1)) break;\n        }\n        if (child) {\n            debugf(\"Unlinking child %p from parent %p\\n\",\n                (void*)child, (void*)h);\n            raxNode *new = raxRemoveChild(h,child);\n            if (new != h) {\n                raxNode *parent = raxStackPeek(&ts);\n                raxNode **parentlink;\n                if (parent == NULL) {\n                    parentlink = &rax->head;\n                } else {\n                    parentlink = raxFindParentLink(parent,h);\n                }\n                memcpy(parentlink,&new,sizeof(new));\n            }\n\n            /* If after the removal the node has just a single child\n             * and is not a key, we need to try to compress it. */\n            if (new->size == 1 && new->iskey == 0) {\n                trycompress = 1;\n                h = new;\n            }\n        }\n    } else if (h->size == 1) {\n        /* If the node had just one child, after the removal of the key\n         * further compression with adjacent nodes is potentially possible. */\n        trycompress = 1;\n    }\n\n    /* Don't try node compression if our nodes pointers stack is not\n     * complete because of OOM while executing raxLowWalk() */\n    if (trycompress && ts.oom) trycompress = 0;\n\n    /* Recompression: if trycompress is true, 'h' points to a radix tree node\n     * that changed in a way that could allow to compress nodes in this\n     * sub-branch. Compressed nodes represent chains of nodes that are not\n     * keys and have a single child, so there are two deletion events that\n     * may alter the tree so that further compression is needed:\n     *\n     * 1) A node with a single child was a key and now no longer is a key.\n     * 2) A node with two children now has just one child.\n     *\n     * We try to navigate upward till there are other nodes that can be\n     * compressed, when we reach the upper node which is not a key and has\n     * a single child, we scan the chain of children to collect the\n     * compressable part of the tree, and replace the current node with the\n     * new one, fixing the child pointer to reference the first non\n     * compressable node.\n     *\n     * Example of case \"1\". A tree stores the keys \"FOO\" = 1 and\n     * \"FOOBAR\" = 2:\n     *\n     *\n     * \"FOO\" -> \"BAR\" -> [] (2)\n     *           (1)\n     *\n     * After the removal of \"FOO\" the tree can be compressed as:\n     *\n     * \"FOOBAR\" -> [] (2)\n     *\n     *\n     * Example of case \"2\". A tree stores the keys \"FOOBAR\" = 1 and\n     * \"FOOTER\" = 2:\n     *\n     *          |B| -> \"AR\" -> [] (1)\n     * \"FOO\" -> |-|\n     *          |T| -> \"ER\" -> [] (2)\n     *\n     * After the removal of \"FOOTER\" the resulting tree is:\n     *\n     * \"FOO\" -> |B| -> \"AR\" -> [] (1)\n     *\n     * That can be compressed into:\n     *\n     * \"FOOBAR\" -> [] (1)\n     */\n    if (trycompress) {\n        debugf(\"After removing %.*s:\\n\", (int)len, s);\n        debugnode(\"Compression may be needed\",h);\n        debugf(\"Seek start node\\n\");\n\n        /* Try to reach the upper node that is compressible.\n         * At the end of the loop 'h' will point to the first node we\n         * can try to compress and 'parent' to its parent. */\n        raxNode *parent;\n        while(1) {\n            parent = raxStackPop(&ts);\n            if (!parent || parent->iskey ||\n                (!parent->iscompr && parent->size != 1)) break;\n            h = parent;\n            debugnode(\"Going up to\",h);\n        }\n        raxNode *start = h; /* Compression starting node. */\n\n        /* Scan chain of nodes we can compress. */\n        size_t comprsize = h->size;\n        int nodes = 1;\n        while(h->size != 0) {\n            raxNode **cp = raxNodeLastChildPtr(h);\n            memcpy(&h,cp,sizeof(h));\n            if (h->iskey || (!h->iscompr && h->size != 1)) break;\n            /* Stop here if going to the next node would result into\n             * a compressed node larger than h->size can hold. */\n            if (comprsize + h->size > RAX_NODE_MAX_SIZE) break;\n            nodes++;\n            comprsize += h->size;\n        }\n        if (nodes > 1) {\n            /* If we can compress, create the new node and populate it. */\n            size_t nodesize =\n                sizeof(raxNode)+comprsize+raxPadding(comprsize)+sizeof(raxNode*);\n            raxNode *new = rax_malloc(nodesize);\n            /* An out of memory here just means we cannot optimize this\n             * node, but the tree is left in a consistent state. */\n            if (new == NULL) {\n                raxStackFree(&ts);\n                return 1;\n            }\n            new->iskey = 0;\n            new->isnull = 0;\n            new->iscompr = 1;\n            new->size = comprsize;\n            rax->numnodes++;\n\n            /* Scan again, this time to populate the new node content and\n             * to fix the new node child pointer. At the same time we free\n             * all the nodes that we'll no longer use. */\n            comprsize = 0;\n            h = start;\n            while(h->size != 0) {\n                memcpy(new->data+comprsize,h->data,h->size);\n                comprsize += h->size;\n                raxNode **cp = raxNodeLastChildPtr(h);\n                raxNode *tofree = h;\n                memcpy(&h,cp,sizeof(h));\n                rax_free(tofree); rax->numnodes--;\n                if (h->iskey || (!h->iscompr && h->size != 1)) break;\n            }\n            debugnode(\"New node\",new);\n\n            /* Now 'h' points to the first node that we still need to use,\n             * so our new node child pointer will point to it. */\n            raxNode **cp = raxNodeLastChildPtr(new);\n            memcpy(cp,&h,sizeof(h));\n\n            /* Fix parent link. */\n            if (parent) {\n                raxNode **parentlink = raxFindParentLink(parent,start);\n                memcpy(parentlink,&new,sizeof(new));\n            } else {\n                rax->head = new;\n            }\n\n            debugf(\"Compressed %d nodes, %d total bytes\\n\",\n                nodes, (int)comprsize);\n        }\n    }\n    raxStackFree(&ts);\n    return 1;\n}\n\n/* This is the core of raxFree(): performs a depth-first scan of the\n * tree and releases all the nodes found. */\nvoid raxRecursiveFree(rax *rax, raxNode *n, void (*free_callback)(void*)) {\n    debugnode(\"free traversing\",n);\n    int numchildren = n->iscompr ? 1 : n->size;\n    raxNode **cp = raxNodeLastChildPtr(n);\n    while(numchildren--) {\n        raxNode *child;\n        memcpy(&child,cp,sizeof(child));\n        raxRecursiveFree(rax,child,free_callback);\n        cp--;\n    }\n    debugnode(\"free depth-first\",n);\n    if (free_callback && n->iskey && !n->isnull)\n        free_callback(raxGetData(n));\n    rax_free(n);\n    rax->numnodes--;\n}\n\n/* Free a whole radix tree, calling the specified callback in order to\n * free the auxiliary data. */\nvoid raxFreeWithCallback(rax *rax, void (*free_callback)(void*)) {\n    raxRecursiveFree(rax,rax->head,free_callback);\n    assert(rax->numnodes == 0);\n    rax_free(rax);\n}\n\n/* Free a whole radix tree. */\nvoid raxFree(rax *rax) {\n    raxFreeWithCallback(rax,NULL);\n}\n\n/* ------------------------------- Iterator --------------------------------- */\n\n/* Initialize a Rax iterator. This call should be performed a single time\n * to initialize the iterator, and must be followed by a raxSeek() call,\n * otherwise the raxPrev()/raxNext() functions will just return EOF. */\nvoid raxStart(raxIterator *it, rax *rt) {\n    it->flags = RAX_ITER_EOF; /* No crash if the iterator is not seeked. */\n    it->rt = rt;\n    it->key_len = 0;\n    it->key = it->key_static_string;\n    it->key_max = RAX_ITER_STATIC_LEN;\n    it->data = NULL;\n    it->node_cb = NULL;\n    raxStackInit(&it->stack);\n}\n\n/* Append characters at the current key string of the iterator 'it'. This\n * is a low level function used to implement the iterator, not callable by\n * the user. Returns 0 on out of memory, otherwise 1 is returned. */\nint raxIteratorAddChars(raxIterator *it, unsigned char *s, size_t len) {\n    if (it->key_max < it->key_len+len) {\n        unsigned char *old = (it->key == it->key_static_string) ? NULL :\n                                                                  it->key;\n        size_t new_max = (it->key_len+len)*2;\n        it->key = rax_realloc(old,new_max);\n        if (it->key == NULL) {\n            it->key = (!old) ? it->key_static_string : old;\n            errno = ENOMEM;\n            return 0;\n        }\n        if (old == NULL) memcpy(it->key,it->key_static_string,it->key_len);\n        it->key_max = new_max;\n    }\n    /* Use memmove since there could be an overlap between 's' and\n     * it->key when we use the current key in order to re-seek. */\n    memmove(it->key+it->key_len,s,len);\n    it->key_len += len;\n    return 1;\n}\n\n/* Remove the specified number of chars from the right of the current\n * iterator key. */\nvoid raxIteratorDelChars(raxIterator *it, size_t count) {\n    it->key_len -= count;\n}\n\n/* Do an iteration step towards the next element. At the end of the step the\n * iterator key will represent the (new) current key. If it is not possible\n * to step in the specified direction since there are no longer elements, the\n * iterator is flagged with RAX_ITER_EOF.\n *\n * If 'noup' is true the function starts directly scanning for the next\n * lexicographically smaller children, and the current node is already assumed\n * to be the parent of the last key node, so the first operation to go back to\n * the parent will be skipped. This option is used by raxSeek() when\n * implementing seeking a non existing element with the \">\" or \"<\" options:\n * the starting node is not a key in that particular case, so we start the scan\n * from a node that does not represent the key set.\n *\n * The function returns 1 on success or 0 on out of memory. */\nint raxIteratorNextStep(raxIterator *it, int noup) {\n    if (it->flags & RAX_ITER_EOF) {\n        return 1;\n    } else if (it->flags & RAX_ITER_JUST_SEEKED) {\n        it->flags &= ~RAX_ITER_JUST_SEEKED;\n        return 1;\n    }\n\n    /* Save key len, stack items and the node where we are currently\n     * so that on iterator EOF we can restore the current key and state. */\n    size_t orig_key_len = it->key_len;\n    size_t orig_stack_items = it->stack.items;\n    raxNode *orig_node = it->node;\n\n    while(1) {\n        int children = it->node->iscompr ? 1 : it->node->size;\n        if (!noup && children) {\n            debugf(\"GO DEEPER\\n\");\n            /* Seek the lexicographically smaller key in this subtree, which\n             * is the first one found always going towards the first child\n             * of every successive node. */\n            if (!raxStackPush(&it->stack,it->node)) return 0;\n            raxNode **cp = raxNodeFirstChildPtr(it->node);\n            if (!raxIteratorAddChars(it,it->node->data,\n                it->node->iscompr ? it->node->size : 1)) return 0;\n            memcpy(&it->node,cp,sizeof(it->node));\n            /* Call the node callback if any, and replace the node pointer\n             * if the callback returns true. */\n            if (it->node_cb && it->node_cb(&it->node))\n                memcpy(cp,&it->node,sizeof(it->node));\n            /* For \"next\" step, stop every time we find a key along the\n             * way, since the key is lexicograhically smaller compared to\n             * what follows in the sub-children. */\n            if (it->node->iskey) {\n                it->data = raxGetData(it->node);\n                return 1;\n            }\n        } else {\n            /* If we finished exploring the previous sub-tree, switch to the\n             * new one: go upper until a node is found where there are\n             * children representing keys lexicographically greater than the\n             * current key. */\n            while(1) {\n                int old_noup = noup;\n\n                /* Already on head? Can't go up, iteration finished. */\n                if (!noup && it->node == it->rt->head) {\n                    it->flags |= RAX_ITER_EOF;\n                    it->stack.items = orig_stack_items;\n                    it->key_len = orig_key_len;\n                    it->node = orig_node;\n                    return 1;\n                }\n                /* If there are no children at the current node, try parent's\n                 * next child. */\n                unsigned char prevchild = it->key[it->key_len-1];\n                if (!noup) {\n                    it->node = raxStackPop(&it->stack);\n                } else {\n                    noup = 0;\n                }\n                /* Adjust the current key to represent the node we are\n                 * at. */\n                int todel = it->node->iscompr ? it->node->size : 1;\n                raxIteratorDelChars(it,todel);\n\n                /* Try visiting the next child if there was at least one\n                 * additional child. */\n                if (!it->node->iscompr && it->node->size > (old_noup ? 0 : 1)) {\n                    raxNode **cp = raxNodeFirstChildPtr(it->node);\n                    int i = 0;\n                    while (i < it->node->size) {\n                        debugf(\"SCAN NEXT %c\\n\", it->node->data[i]);\n                        if (it->node->data[i] > prevchild) break;\n                        i++;\n                        cp++;\n                    }\n                    if (i != it->node->size) {\n                        debugf(\"SCAN found a new node\\n\");\n                        raxIteratorAddChars(it,it->node->data+i,1);\n                        if (!raxStackPush(&it->stack,it->node)) return 0;\n                        memcpy(&it->node,cp,sizeof(it->node));\n                        /* Call the node callback if any, and replace the node\n                         * pointer if the callback returns true. */\n                        if (it->node_cb && it->node_cb(&it->node))\n                            memcpy(cp,&it->node,sizeof(it->node));\n                        if (it->node->iskey) {\n                            it->data = raxGetData(it->node);\n                            return 1;\n                        }\n                        break;\n                    }\n                }\n            }\n        }\n    }\n}\n\n/* Seek the greatest key in the subtree at the current node. Return 0 on\n * out of memory, otherwise 1. This is an helper function for different\n * iteration functions below. */\nint raxSeekGreatest(raxIterator *it) {\n    while(it->node->size) {\n        if (it->node->iscompr) {\n            if (!raxIteratorAddChars(it,it->node->data,\n                it->node->size)) return 0;\n        } else {\n            if (!raxIteratorAddChars(it,it->node->data+it->node->size-1,1))\n                return 0;\n        }\n        raxNode **cp = raxNodeLastChildPtr(it->node);\n        if (!raxStackPush(&it->stack,it->node)) return 0;\n        memcpy(&it->node,cp,sizeof(it->node));\n    }\n    return 1;\n}\n\n/* Like raxIteratorNextStep() but implements an iteration step moving\n * to the lexicographically previous element. The 'noup' option has a similar\n * effect to the one of raxIteratorNextStep(). */\nint raxIteratorPrevStep(raxIterator *it, int noup) {\n    if (it->flags & RAX_ITER_EOF) {\n        return 1;\n    } else if (it->flags & RAX_ITER_JUST_SEEKED) {\n        it->flags &= ~RAX_ITER_JUST_SEEKED;\n        return 1;\n    }\n\n    /* Save key len, stack items and the node where we are currently\n     * so that on iterator EOF we can restore the current key and state. */\n    size_t orig_key_len = it->key_len;\n    size_t orig_stack_items = it->stack.items;\n    raxNode *orig_node = it->node;\n\n    while(1) {\n        int old_noup = noup;\n\n        /* Already on head? Can't go up, iteration finished. */\n        if (!noup && it->node == it->rt->head) {\n            it->flags |= RAX_ITER_EOF;\n            it->stack.items = orig_stack_items;\n            it->key_len = orig_key_len;\n            it->node = orig_node;\n            return 1;\n        }\n\n        unsigned char prevchild = it->key[it->key_len-1];\n        if (!noup) {\n            it->node = raxStackPop(&it->stack);\n        } else {\n            noup = 0;\n        }\n\n        /* Adjust the current key to represent the node we are\n         * at. */\n        int todel = it->node->iscompr ? it->node->size : 1;\n        raxIteratorDelChars(it,todel);\n\n        /* Try visiting the prev child if there is at least one\n         * child. */\n        if (!it->node->iscompr && it->node->size > (old_noup ? 0 : 1)) {\n            raxNode **cp = raxNodeLastChildPtr(it->node);\n            int i = it->node->size-1;\n            while (i >= 0) {\n                debugf(\"SCAN PREV %c\\n\", it->node->data[i]);\n                if (it->node->data[i] < prevchild) break;\n                i--;\n                cp--;\n            }\n            /* If we found a new subtree to explore in this node,\n             * go deeper following all the last children in order to\n             * find the key lexicographically greater. */\n            if (i != -1) {\n                debugf(\"SCAN found a new node\\n\");\n                /* Enter the node we just found. */\n                if (!raxIteratorAddChars(it,it->node->data+i,1)) return 0;\n                if (!raxStackPush(&it->stack,it->node)) return 0;\n                memcpy(&it->node,cp,sizeof(it->node));\n                /* Seek sub-tree max. */\n                if (!raxSeekGreatest(it)) return 0;\n            }\n        }\n\n        /* Return the key: this could be the key we found scanning a new\n         * subtree, or if we did not find a new subtree to explore here,\n         * before giving up with this node, check if it's a key itself. */\n        if (it->node->iskey) {\n            it->data = raxGetData(it->node);\n            return 1;\n        }\n    }\n}\n\n/* Seek an iterator at the specified element.\n * Return 0 if the seek failed for syntax error or out of memory. Otherwise\n * 1 is returned. When 0 is returned for out of memory, errno is set to\n * the ENOMEM value. */\nint raxSeek(raxIterator *it, const char *op, unsigned char *ele, size_t len) {\n    int eq = 0, lt = 0, gt = 0, first = 0, last = 0;\n\n    it->stack.items = 0; /* Just resetting. Initialized by raxStart(). */\n    it->flags |= RAX_ITER_JUST_SEEKED;\n    it->flags &= ~RAX_ITER_EOF;\n    it->key_len = 0;\n    it->node = NULL;\n\n    /* Set flags according to the operator used to perform the seek. */\n    if (op[0] == '>') {\n        gt = 1;\n        if (op[1] == '=') eq = 1;\n    } else if (op[0] == '<') {\n        lt = 1;\n        if (op[1] == '=') eq = 1;\n    } else if (op[0] == '=') {\n        eq = 1;\n    } else if (op[0] == '^') {\n        first = 1;\n    } else if (op[0] == '$') {\n        last = 1;\n    } else {\n        errno = 0;\n        return 0; /* Error. */\n    }\n\n    /* If there are no elements, set the EOF condition immediately and\n     * return. */\n    if (it->rt->numele == 0) {\n        it->flags |= RAX_ITER_EOF;\n        return 1;\n    }\n\n    if (first) {\n        /* Seeking the first key greater or equal to the empty string\n         * is equivalent to seeking the smaller key available. */\n        return raxSeek(it,\">=\",NULL,0);\n    }\n\n    if (last) {\n        /* Find the greatest key taking always the last child till a\n         * final node is found. */\n        it->node = it->rt->head;\n        if (!raxSeekGreatest(it)) return 0;\n        assert(it->node->iskey);\n        it->data = raxGetData(it->node);\n        return 1;\n    }\n\n    /* We need to seek the specified key. What we do here is to actually\n     * perform a lookup, and later invoke the prev/next key code that\n     * we already use for iteration. */\n    int splitpos = 0;\n    size_t i = raxLowWalk(it->rt,ele,len,&it->node,NULL,&splitpos,&it->stack);\n\n    /* Return OOM on incomplete stack info. */\n    if (it->stack.oom) return 0;\n\n    if (eq && i == len && (!it->node->iscompr || splitpos == 0) &&\n        it->node->iskey)\n    {\n        /* We found our node, since the key matches and we have an\n         * \"equal\" condition. */\n        if (!raxIteratorAddChars(it,ele,len)) return 0; /* OOM. */\n        it->data = raxGetData(it->node);\n    } else if (lt || gt) {\n        /* Exact key not found or eq flag not set. We have to set as current\n         * key the one represented by the node we stopped at, and perform\n         * a next/prev operation to seek. To reconstruct the key at this node\n         * we start from the parent and go to the current node, accumulating\n         * the characters found along the way. */\n        if (!raxStackPush(&it->stack,it->node)) return 0;\n        for (size_t j = 1; j < it->stack.items; j++) {\n            raxNode *parent = it->stack.stack[j-1];\n            raxNode *child = it->stack.stack[j];\n            if (parent->iscompr) {\n                if (!raxIteratorAddChars(it,parent->data,parent->size))\n                    return 0;\n            } else {\n                raxNode **cp = raxNodeFirstChildPtr(parent);\n                unsigned char *p = parent->data;\n                while(1) {\n                    raxNode *aux;\n                    memcpy(&aux,cp,sizeof(aux));\n                    if (aux == child) break;\n                    cp++;\n                    p++;\n                }\n                if (!raxIteratorAddChars(it,p,1)) return 0;\n            }\n        }\n        raxStackPop(&it->stack);\n\n        /* We need to set the iterator in the correct state to call next/prev\n         * step in order to seek the desired element. */\n        debugf(\"After initial seek: i=%d len=%d key=%.*s\\n\",\n            (int)i, (int)len, (int)it->key_len, it->key);\n        if (i != len && !it->node->iscompr) {\n            /* If we stopped in the middle of a normal node because of a\n             * mismatch, add the mismatching character to the current key\n             * and call the iterator with the 'noup' flag so that it will try\n             * to seek the next/prev child in the current node directly based\n             * on the mismatching character. */\n            if (!raxIteratorAddChars(it,ele+i,1)) return 0;\n            debugf(\"Seek normal node on mismatch: %.*s\\n\",\n                (int)it->key_len, (char*)it->key);\n\n            it->flags &= ~RAX_ITER_JUST_SEEKED;\n            if (lt && !raxIteratorPrevStep(it,1)) return 0;\n            if (gt && !raxIteratorNextStep(it,1)) return 0;\n            it->flags |= RAX_ITER_JUST_SEEKED; /* Ignore next call. */\n        } else if (i != len && it->node->iscompr) {\n            debugf(\"Compressed mismatch: %.*s\\n\",\n                (int)it->key_len, (char*)it->key);\n            /* In case of a mismatch within a compressed node. */\n            int nodechar = it->node->data[splitpos];\n            int keychar = ele[i];\n            it->flags &= ~RAX_ITER_JUST_SEEKED;\n            if (gt) {\n                /* If the key the compressed node represents is greater\n                 * than our seek element, continue forward, otherwise set the\n                 * state in order to go back to the next sub-tree. */\n                if (nodechar > keychar) {\n                    if (!raxIteratorNextStep(it,0)) return 0;\n                } else {\n                    if (!raxIteratorAddChars(it,it->node->data,it->node->size))\n                        return 0;\n                    if (!raxIteratorNextStep(it,1)) return 0;\n                }\n            }\n            if (lt) {\n                /* If the key the compressed node represents is smaller\n                 * than our seek element, seek the greater key in this\n                 * subtree, otherwise set the state in order to go back to\n                 * the previous sub-tree. */\n                if (nodechar < keychar) {\n                    if (!raxSeekGreatest(it)) return 0;\n                    it->data = raxGetData(it->node);\n                } else {\n                    if (!raxIteratorAddChars(it,it->node->data,it->node->size))\n                        return 0;\n                    if (!raxIteratorPrevStep(it,1)) return 0;\n                }\n            }\n            it->flags |= RAX_ITER_JUST_SEEKED; /* Ignore next call. */\n        } else {\n            debugf(\"No mismatch: %.*s\\n\",\n                (int)it->key_len, (char*)it->key);\n            /* If there was no mismatch we are into a node representing the\n             * key, (but which is not a key or the seek operator does not\n             * include 'eq'), or we stopped in the middle of a compressed node\n             * after processing all the key. Continue iterating as this was\n             * a legitimate key we stopped at. */\n            it->flags &= ~RAX_ITER_JUST_SEEKED;\n            if (it->node->iscompr && it->node->iskey && splitpos && lt) {\n                /* If we stopped in the middle of a compressed node with\n                 * perfect match, and the condition is to seek a key \"<\" than\n                 * the specified one, then if this node is a key it already\n                 * represents our match. For instance we may have nodes:\n                 *\n                 * \"f\" -> \"oobar\" = 1 -> \"\" = 2\n                 *\n                 * Representing keys \"f\" = 1, \"foobar\" = 2. A seek for\n                 * the key < \"foo\" will stop in the middle of the \"oobar\"\n                 * node, but will be our match, representing the key \"f\".\n                 *\n                 * So in that case, we don't seek backward. */\n                it->data = raxGetData(it->node);\n            } else {\n                if (gt && !raxIteratorNextStep(it,0)) return 0;\n                if (lt && !raxIteratorPrevStep(it,0)) return 0;\n            }\n            it->flags |= RAX_ITER_JUST_SEEKED; /* Ignore next call. */\n        }\n    } else {\n        /* If we are here just eq was set but no match was found. */\n        it->flags |= RAX_ITER_EOF;\n        return 1;\n    }\n    return 1;\n}\n\n/* Go to the next element in the scope of the iterator 'it'.\n * If EOF (or out of memory) is reached, 0 is returned, otherwise 1 is\n * returned. In case 0 is returned because of OOM, errno is set to ENOMEM. */\nint raxNext(raxIterator *it) {\n    if (!raxIteratorNextStep(it,0)) {\n        errno = ENOMEM;\n        return 0;\n    }\n    if (it->flags & RAX_ITER_EOF) {\n        errno = 0;\n        return 0;\n    }\n    return 1;\n}\n\n/* Go to the previous element in the scope of the iterator 'it'.\n * If EOF (or out of memory) is reached, 0 is returned, otherwise 1 is\n * returned. In case 0 is returned because of OOM, errno is set to ENOMEM. */\nint raxPrev(raxIterator *it) {\n    if (!raxIteratorPrevStep(it,0)) {\n        errno = ENOMEM;\n        return 0;\n    }\n    if (it->flags & RAX_ITER_EOF) {\n        errno = 0;\n        return 0;\n    }\n    return 1;\n}\n\n/* Perform a random walk starting in the current position of the iterator.\n * Return 0 if the tree is empty or on out of memory. Otherwise 1 is returned\n * and the iterator is set to the node reached after doing a random walk\n * of 'steps' steps. If the 'steps' argument is 0, the random walk is performed\n * using a random number of steps between 1 and two times the logarithm of\n * the number of elements.\n *\n * NOTE: if you use this function to generate random elements from the radix\n * tree, expect a disappointing distribution. A random walk produces good\n * random elements if the tree is not sparse, however in the case of a radix\n * tree certain keys will be reported much more often than others. At least\n * this function should be able to explore every possible element eventually. */\nint raxRandomWalk(raxIterator *it, size_t steps) {\n    if (it->rt->numele == 0) {\n        it->flags |= RAX_ITER_EOF;\n        return 0;\n    }\n\n    if (steps == 0) {\n        size_t fle = 1+floor(log(it->rt->numele));\n        fle *= 2;\n        steps = 1 + rand() % fle;\n    }\n\n    raxNode *n = it->node;\n    while(steps > 0 || !n->iskey) {\n        int numchildren = n->iscompr ? 1 : n->size;\n        int r = rand() % (numchildren+(n != it->rt->head));\n\n        if (r == numchildren) {\n            /* Go up to parent. */\n            n = raxStackPop(&it->stack);\n            int todel = n->iscompr ? n->size : 1;\n            raxIteratorDelChars(it,todel);\n        } else {\n            /* Select a random child. */\n            if (n->iscompr) {\n                if (!raxIteratorAddChars(it,n->data,n->size)) return 0;\n            } else {\n                if (!raxIteratorAddChars(it,n->data+r,1)) return 0;\n            }\n            raxNode **cp = raxNodeFirstChildPtr(n)+r;\n            if (!raxStackPush(&it->stack,n)) return 0;\n            memcpy(&n,cp,sizeof(n));\n        }\n        if (n->iskey) steps--;\n    }\n    it->node = n;\n    it->data = raxGetData(it->node);\n    return 1;\n}\n\n/* Compare the key currently pointed by the iterator to the specified\n * key according to the specified operator. Returns 1 if the comparison is\n * true, otherwise 0 is returned. */\nint raxCompare(raxIterator *iter, const char *op, unsigned char *key, size_t key_len) {\n    int eq = 0, lt = 0, gt = 0;\n\n    if (op[0] == '=' || op[1] == '=') eq = 1;\n    if (op[0] == '>') gt = 1;\n    else if (op[0] == '<') lt = 1;\n    else if (op[1] != '=') return 0; /* Syntax error. */\n\n    size_t minlen = key_len < iter->key_len ? key_len : iter->key_len;\n    int cmp = memcmp(iter->key,key,minlen);\n\n    /* Handle == */\n    if (lt == 0 && gt == 0) return cmp == 0 && key_len == iter->key_len;\n\n    /* Handle >, >=, <, <= */\n    if (cmp == 0) {\n        /* Same prefix: longer wins. */\n        if (eq && key_len == iter->key_len) return 1;\n        else if (lt) return iter->key_len < key_len;\n        else if (gt) return iter->key_len > key_len;\n        else return 0; /* Avoid warning, just 'eq' is handled before. */\n    } else if (cmp > 0) {\n        return gt ? 1 : 0;\n    } else /* (cmp < 0) */ {\n        return lt ? 1 : 0;\n    }\n}\n\n/* Free the iterator. */\nvoid raxStop(raxIterator *it) {\n    if (it->key != it->key_static_string) rax_free(it->key);\n    raxStackFree(&it->stack);\n}\n\n/* Return if the iterator is in an EOF state. This happens when raxSeek()\n * failed to seek an appropriate element, so that raxNext() or raxPrev()\n * will return zero, or when an EOF condition was reached while iterating\n * with raxNext() and raxPrev(). */\nint raxEOF(raxIterator *it) {\n    return it->flags & RAX_ITER_EOF;\n}\n\n/* Return the number of elements inside the radix tree. */\nuint64_t raxSize(rax *rax) {\n    return rax->numele;\n}\n\n/* ----------------------------- Introspection ------------------------------ */\n\n/* This function is mostly used for debugging and learning purposes.\n * It shows an ASCII representation of a tree on standard output, outline\n * all the nodes and the contained keys.\n *\n * The representation is as follow:\n *\n *  \"foobar\" (compressed node)\n *  [abc] (normal node with three children)\n *  [abc]=0x12345678 (node is a key, pointing to value 0x12345678)\n *  [] (a normal empty node)\n *\n *  Children are represented in new indented lines, each children prefixed by\n *  the \"`-(x)\" string, where \"x\" is the edge byte.\n *\n *  [abc]\n *   `-(a) \"ladin\"\n *   `-(b) [kj]\n *   `-(c) []\n *\n *  However when a node has a single child the following representation\n *  is used instead:\n *\n *  [abc] -> \"ladin\" -> []\n */\n\n/* The actual implementation of raxShow(). */\nvoid raxRecursiveShow(int level, int lpad, raxNode *n) {\n    char s = n->iscompr ? '\"' : '[';\n    char e = n->iscompr ? '\"' : ']';\n\n    int numchars = printf(\"%c%.*s%c\", s, n->size, n->data, e);\n    if (n->iskey) {\n        numchars += printf(\"=%p\",raxGetData(n));\n    }\n\n    int numchildren = n->iscompr ? 1 : n->size;\n    /* Note that 7 and 4 magic constants are the string length\n     * of \" `-(x) \" and \" -> \" respectively. */\n    if (level) {\n        lpad += (numchildren > 1) ? 7 : 4;\n        if (numchildren == 1) lpad += numchars;\n    }\n    raxNode **cp = raxNodeFirstChildPtr(n);\n    for (int i = 0; i < numchildren; i++) {\n        char *branch = \" `-(%c) \";\n        if (numchildren > 1) {\n            printf(\"\\n\");\n            for (int j = 0; j < lpad; j++) putchar(' ');\n            printf(branch,n->data[i]);\n        } else {\n            printf(\" -> \");\n        }\n        raxNode *child;\n        memcpy(&child,cp,sizeof(child));\n        raxRecursiveShow(level+1,lpad,child);\n        cp++;\n    }\n}\n\n/* Show a tree, as outlined in the comment above. */\nvoid raxShow(rax *rax) {\n    raxRecursiveShow(0,0,rax->head);\n    putchar('\\n');\n}\n\n/* Used by debugnode() macro to show info about a given node. */\nvoid raxDebugShowNode(const char *msg, raxNode *n) {\n    if (raxDebugMsg == 0) return;\n    printf(\"%s: %p [%.*s] key:%u size:%u children:\",\n        msg, (void*)n, (int)n->size, (char*)n->data, n->iskey, n->size);\n    int numcld = n->iscompr ? 1 : n->size;\n    raxNode **cldptr = raxNodeLastChildPtr(n) - (numcld-1);\n    while(numcld--) {\n        raxNode *child;\n        memcpy(&child,cldptr,sizeof(child));\n        cldptr++;\n        printf(\"%p \", (void*)child);\n    }\n    printf(\"\\n\");\n    fflush(stdout);\n}\n\n/* Touch all the nodes of a tree returning a check sum. This is useful\n * in order to make Valgrind detect if there is something wrong while\n * reading the data structure.\n *\n * This function was used in order to identify Rax bugs after a big refactoring\n * using this technique:\n *\n * 1. The rax-test is executed using Valgrind, adding a printf() so that for\n *    the fuzz tester we see what iteration in the loop we are in.\n * 2. After every modification of the radix tree made by the fuzz tester\n *    in rax-test.c, we add a call to raxTouch().\n * 3. Now as soon as an operation will corrupt the tree, raxTouch() will\n *    detect it (via Valgrind) immediately. We can add more calls to narrow\n *    the state.\n * 4. At this point a good idea is to enable Rax debugging messages immediately\n *    before the moment the tree is corrupted, to see what happens.\n */\nunsigned long raxTouch(raxNode *n) {\n    debugf(\"Touching %p\\n\", (void*)n);\n    unsigned long sum = 0;\n    if (n->iskey) {\n        sum += (unsigned long)raxGetData(n);\n    }\n\n    int numchildren = n->iscompr ? 1 : n->size;\n    raxNode **cp = raxNodeFirstChildPtr(n);\n    int count = 0;\n    for (int i = 0; i < numchildren; i++) {\n        if (numchildren > 1) {\n            sum += (long)n->data[i];\n        }\n        raxNode *child;\n        memcpy(&child,cp,sizeof(child));\n        if (child == (void*)0x65d1760) count++;\n        if (count > 1) exit(1);\n        sum += raxTouch(child);\n        cp++;\n    }\n    return sum;\n}\n"
  },
  {
    "path": "src/rax.h",
    "content": "/* Rax -- A radix tree implementation.\n *\n * Copyright (c) 2017-2018, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef RAX_H\n#define RAX_H\n\n#ifdef __cplusplus\n#define ZERO_LENGTH_ARRAY_LENGTH 1\n#else\n#define ZERO_LENGTH_ARRAY_LENGTH\n#endif\n\n#include <stdint.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/* Representation of a radix tree as implemented in this file, that contains\n * the strings \"foo\", \"foobar\" and \"footer\" after the insertion of each\n * word. When the node represents a key inside the radix tree, we write it\n * between [], otherwise it is written between ().\n *\n * This is the vanilla representation:\n *\n *              (f) \"\"\n *                \\\n *                (o) \"f\"\n *                  \\\n *                  (o) \"fo\"\n *                    \\\n *                  [t   b] \"foo\"\n *                  /     \\\n *         \"foot\" (e)     (a) \"foob\"\n *                /         \\\n *      \"foote\" (r)         (r) \"fooba\"\n *              /             \\\n *    \"footer\" []             [] \"foobar\"\n *\n * However, this implementation implements a very common optimization where\n * successive nodes having a single child are \"compressed\" into the node\n * itself as a string of characters, each representing a next-level child,\n * and only the link to the node representing the last character node is\n * provided inside the representation. So the above representation is turned\n * into:\n *\n *                  [\"foo\"] \"\"\n *                     |\n *                  [t   b] \"foo\"\n *                  /     \\\n *        \"foot\" (\"er\")    (\"ar\") \"foob\"\n *                 /          \\\n *       \"footer\" []          [] \"foobar\"\n *\n * However this optimization makes the implementation a bit more complex.\n * For instance if a key \"first\" is added in the above radix tree, a\n * \"node splitting\" operation is needed, since the \"foo\" prefix is no longer\n * composed of nodes having a single child one after the other. This is the\n * above tree and the resulting node splitting after this event happens:\n *\n *\n *                    (f) \"\"\n *                    /\n *                 (i o) \"f\"\n *                 /   \\\n *    \"firs\"  (\"rst\")  (o) \"fo\"\n *              /        \\\n *    \"first\" []       [t   b] \"foo\"\n *                     /     \\\n *           \"foot\" (\"er\")    (\"ar\") \"foob\"\n *                    /          \\\n *          \"footer\" []          [] \"foobar\"\n *\n * Similarly after deletion, if a new chain of nodes having a single child\n * is created (the chain must also not include nodes that represent keys),\n * it must be compressed back into a single node.\n *\n */\n\n#define RAX_NODE_MAX_SIZE ((1<<29)-1)\ntypedef struct raxNode {\n    uint32_t iskey:1;     /* Does this node contain a key? */\n    uint32_t isnull:1;    /* Associated value is NULL (don't store it). */\n    uint32_t iscompr:1;   /* Node is compressed. */\n    uint32_t size:29;     /* Number of children, or compressed string len. */\n    /* Data layout is as follows:\n     *\n     * If node is not compressed we have 'size' bytes, one for each children\n     * character, and 'size' raxNode pointers, point to each child node.\n     * Note how the character is not stored in the children but in the\n     * edge of the parents:\n     *\n     * [header iscompr=0][abc][a-ptr][b-ptr][c-ptr](value-ptr?)\n     *\n     * if node is compressed (iscompr bit is 1) the node has 1 children.\n     * In that case the 'size' bytes of the string stored immediately at\n     * the start of the data section, represent a sequence of successive\n     * nodes linked one after the other, for which only the last one in\n     * the sequence is actually represented as a node, and pointed to by\n     * the current compressed node.\n     *\n     * [header iscompr=1][xyz][z-ptr](value-ptr?)\n     *\n     * Both compressed and not compressed nodes can represent a key\n     * with associated data in the radix tree at any level (not just terminal\n     * nodes).\n     *\n     * If the node has an associated key (iskey=1) and is not NULL\n     * (isnull=0), then after the raxNode pointers pointing to the\n     * children, an additional value pointer is present (as you can see\n     * in the representation above as \"value-ptr\" field).\n     */\n#ifndef __cplusplus\n    unsigned char data[];\n#endif\n} raxNode;\n\ntypedef struct rax {\n    raxNode *head;\n    uint64_t numele;\n    uint64_t numnodes;\n} rax;\n\n/* Stack data structure used by raxLowWalk() in order to, optionally, return\n * a list of parent nodes to the caller. The nodes do not have a \"parent\"\n * field for space concerns, so we use the auxiliary stack when needed. */\n#define RAX_STACK_STATIC_ITEMS 32\ntypedef struct raxStack {\n    void **stack; /* Points to static_items or an heap allocated array. */\n    size_t items, maxitems; /* Number of items contained and total space. */\n    /* Up to RAXSTACK_STACK_ITEMS items we avoid to allocate on the heap\n     * and use this static array of pointers instead. */\n    void *static_items[RAX_STACK_STATIC_ITEMS];\n    int oom; /* True if pushing into this stack failed for OOM at some point. */\n} raxStack;\n\n/* Optional callback used for iterators and be notified on each rax node,\n * including nodes not representing keys. If the callback returns true\n * the callback changed the node pointer in the iterator structure, and the\n * iterator implementation will have to replace the pointer in the radix tree\n * internals. This allows the callback to reallocate the node to perform\n * very special operations, normally not needed by normal applications.\n *\n * This callback is used to perform very low level analysis of the radix tree\n * structure, scanning each possible node (but the root node), or in order to\n * reallocate the nodes to reduce the allocation fragmentation (this is the\n * Redis application for this callback).\n *\n * This is currently only supported in forward iterations (raxNext) */\ntypedef int (*raxNodeCallback)(raxNode **noderef);\n\n/* Radix tree iterator state is encapsulated into this data structure. */\n#define RAX_ITER_STATIC_LEN 128\n#define RAX_ITER_JUST_SEEKED (1<<0) /* Iterator was just seeked. Return current\n                                       element for the first iteration and\n                                       clear the flag. */\n#define RAX_ITER_EOF (1<<1)    /* End of iteration reached. */\n#define RAX_ITER_SAFE (1<<2)   /* Safe iterator, allows operations while\n                                  iterating. But it is slower. */\ntypedef struct raxIterator {\n    int flags;\n    rax *rt;                /* Radix tree we are iterating. */\n    unsigned char *key;     /* The current string. */\n    void *data;             /* Data associated to this key. */\n    size_t key_len;         /* Current key length. */\n    size_t key_max;         /* Max key len the current key buffer can hold. */\n    unsigned char key_static_string[RAX_ITER_STATIC_LEN];\n    raxNode *node;          /* Current node. Only for unsafe iteration. */\n    raxStack stack;         /* Stack used for unsafe iteration. */\n    raxNodeCallback node_cb; /* Optional node callback. Normally set to NULL. */\n} raxIterator;\n\n/* A special pointer returned for not found items. */\nextern void *raxNotFound;\n\n/* Exported API. */\nrax *raxNew(void);\nint raxInsert(rax *rax, unsigned char *s, size_t len, void *data, void **old);\nint raxTryInsert(rax *rax, unsigned char *s, size_t len, void *data, void **old);\nint raxRemove(rax *rax, unsigned char *s, size_t len, void **old);\nvoid *raxFind(rax *rax, unsigned char *s, size_t len);\nvoid raxFree(rax *rax);\nvoid raxFreeWithCallback(rax *rax, void (*free_callback)(void*));\nvoid raxStart(raxIterator *it, rax *rt);\nint raxSeek(raxIterator *it, const char *op, unsigned char *ele, size_t len);\nint raxNext(raxIterator *it);\nint raxPrev(raxIterator *it);\nint raxRandomWalk(raxIterator *it, size_t steps);\nint raxCompare(raxIterator *iter, const char *op, unsigned char *key, size_t key_len);\nvoid raxStop(raxIterator *it);\nint raxEOF(raxIterator *it);\nvoid raxShow(rax *rax);\nuint64_t raxSize(rax *rax);\nunsigned long raxTouch(raxNode *n);\nvoid raxSetDebugMsg(int onoff);\n\n/* Internal API. May be used by the node callback in order to access rax nodes\n * in a low level way, so this function is exported as well. */\nvoid raxSetData(raxNode *n, void *data);\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n"
  },
  {
    "path": "src/rax_malloc.h",
    "content": "/* Rax -- A radix tree implementation.\n *\n * Copyright (c) 2017, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n/* Allocator selection.\n *\n * This file is used in order to change the Rax allocator at compile time.\n * Just define the following defines to what you want to use. Also add\n * the include of your alternate allocator if needed (not needed in order\n * to use the default libc allocator). */\n\n#ifndef RAX_ALLOC_H\n#define RAX_ALLOC_H\n#include \"zmalloc.h\"\n#define rax_malloc(size) zmalloc(size, MALLOC_SHARED)\n#define rax_realloc(ptr, size) zrealloc(ptr, size, MALLOC_SHARED)\n#define rax_free zfree\n#endif\n"
  },
  {
    "path": "src/rdb-s3.cpp",
    "content": "#include \"rio.h\"\n#include \"server.h\"\n#include <unistd.h>\n#include <sys/wait.h>\n\n/* Save the DB on disk. Return C_ERR on error, C_OK on success. */\nint rdbSaveS3(char *s3bucket, const redisDbPersistentDataSnapshot **rgpdb, rdbSaveInfo *rsi)\n{\n    int status = EXIT_FAILURE;\n    int fd[2];\n    if (pipe(fd) != 0)\n        return C_ERR;\n\n    pid_t pid = fork();\n    if (pid < 0)\n    {\n        close(fd[0]);\n        close(fd[1]);\n        return C_ERR;\n    }\n\n    if (pid == 0)\n    {\n        // child process\n        dup2(fd[0], STDIN_FILENO);\n        close(fd[1]);\n        close(fd[0]);\n        execlp(\"aws\", \"aws\", \"s3\", \"cp\", \"-\", s3bucket, nullptr);\n        exit(EXIT_FAILURE);\n    }\n    else\n    {\n        close(fd[0]);\n        FILE *fp = fdopen(fd[1], \"w\");\n        if (fp == NULL)\n        {\n            close (fd[1]);\n            return C_ERR;\n        }\n\n        if (rdbSaveFp(fp, rgpdb, rsi) != C_OK)\n        {\n            fclose(fp);\n            return C_ERR;\n        }\n        fclose(fp);\n        waitpid(pid, &status, 0);\n    }\n    \n    if (status != EXIT_SUCCESS)\n        serverLog(LL_WARNING, \"Failed to save DB to AWS S3\");\n    else\n        serverLog(LL_NOTICE,\"DB saved on AWS S3\");\n        \n    return (status == EXIT_SUCCESS) ? C_OK : C_ERR;\n}\n\n\nint rdbLoadS3Core(int fd, rdbSaveInfo *rsi, int rdbflags) \n{\n    FILE *fp;\n    rio rdb;\n    int retval;\n\n    if ((fp = fdopen(fd, \"rb\")) == NULL) return C_ERR;\n    startLoading(0, rdbflags);\n    rioInitWithFile(&rdb,fp);\n    retval = rdbLoadRio(&rdb,rdbflags,rsi);\n    fclose(fp);\n    stopLoading(retval == C_OK);\n    return retval;\n}\n\nint rdbLoadS3(char *s3bucket, rdbSaveInfo *rsi, int rdbflags)\n{\n    int status = EXIT_FAILURE;\n    int fd[2];\n    if (pipe(fd) != 0)\n        return C_ERR;\n\n    pid_t pid = fork();\n    if (pid < 0)\n    {\n        close(fd[0]);\n        close(fd[1]);\n        return C_ERR;\n    }\n\n    if (pid == 0)\n    {\n        // child process\n        dup2(fd[1], STDOUT_FILENO);\n        close(fd[1]);\n        close(fd[0]);\n        execlp(\"aws\", \"aws\", \"s3\", \"cp\", s3bucket, \"-\", nullptr);\n        exit(EXIT_FAILURE);\n    }\n    else\n    {\n        close(fd[1]);\n        if (rdbLoadS3Core(fd[0], rsi, rdbflags) != C_OK)\n        {\n            close(fd[0]);\n            return C_ERR;\n        }\n        close(fd[0]);\n        waitpid(pid, &status, 0);\n    }\n    \n    if (status != EXIT_SUCCESS)\n        serverLog(LL_WARNING, \"Failed to load DB from AWS S3\");\n    else\n        serverLog(LL_NOTICE,\"DB loaded from AWS S3\");\n        \n    return (status == EXIT_SUCCESS) ? C_OK : C_ERR;\n}\n"
  },
  {
    "path": "src/rdb.cpp",
    "content": "/*\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n#include \"lzf.h\"    /* LZF compression library */\n#include \"zipmap.h\"\n#include \"endianconv.h\"\n#include \"stream.h\"\n#include \"storage.h\"\n#include \"cron.h\"\n\n#include <math.h>\n#include <fcntl.h>\n#include <sys/types.h>\n#include <sys/time.h>\n#include <sys/resource.h>\n#include <sys/wait.h>\n#include <arpa/inet.h>\n#include <sys/stat.h>\n#include <sys/param.h>\n#include <thread>\n#include <future>\n#include \"aelocker.h\"\n\n/* This macro is called when the internal RDB structure is corrupt */\n#define rdbReportCorruptRDB(...) rdbReportError(1, __LINE__,__VA_ARGS__)\n/* This macro is called when RDB read failed (possibly a short read) */\n#define rdbReportReadError(...) rdbReportError(0, __LINE__,__VA_ARGS__)\n\nconst char* rdbFileBeingLoaded = NULL; /* used for rdb checking on read error */\nextern int rdbCheckMode;\nvoid rdbCheckError(const char *fmt, ...);\nvoid rdbCheckSetError(const char *fmt, ...);\n\n#ifdef __GNUC__\nvoid rdbReportError(int corruption_error, int linenum, const char *reason, ...) __attribute__ ((format (printf, 3, 4)));\n#endif\nvoid rdbReportError(int corruption_error, int linenum, const char *reason, ...) {\n    va_list ap;\n    char msg[1024];\n    int len;\n\n    len = snprintf(msg,sizeof(msg),\n        \"Internal error in RDB reading offset %llu, function at rdb.c:%d -> \",\n        (unsigned long long)g_pserver->loading_loaded_bytes, linenum);\n    va_start(ap,reason);\n    vsnprintf(msg+len,sizeof(msg)-len,reason,ap);\n    va_end(ap);\n\n    if (!g_pserver->loading) {\n        /* If we're in the context of a RESTORE command, just propagate the error. */\n        /* log in VERBOSE, and return (don't exit). */\n        serverLog(LL_VERBOSE, \"%s\", msg);\n        return;\n    } else if (rdbCheckMode) {\n        /* If we're inside the rdb checker, let it handle the error. */\n        rdbCheckError(\"%s\",msg);\n    } else if (rdbFileBeingLoaded) {\n        /* If we're loading an rdb file form disk, run rdb check (and exit) */\n        serverLog(LL_WARNING, \"%s\", msg);\n        const char *argv[2] = {\"\",rdbFileBeingLoaded};\n        redis_check_rdb_main(2,argv,NULL);\n    } else if (corruption_error) {\n        /* In diskless loading, in case of corrupt file, log and exit. */\n        serverLog(LL_WARNING, \"%s. Failure loading rdb format\", msg);\n    } else {\n        /* In diskless loading, in case of a short read (not a corrupt\n         * file), log and proceed (don't exit). */\n        serverLog(LL_WARNING, \"%s. Failure loading rdb format from socket, assuming connection error, resuming operation.\", msg);\n        return;\n    }\n    serverLog(LL_WARNING, \"Terminating server after rdb file reading failure.\");\n    exit(1);\n}\n\nstatic ssize_t rdbWriteRaw(rio *rdb, void *p, size_t len) {\n    if (rdb && rioWrite(rdb,p,len) == 0)\n        return -1;\n    return len;\n}\n\nint rdbSaveType(rio *rdb, unsigned char type) {\n    return rdbWriteRaw(rdb,&type,1);\n}\n\n/* Load a \"type\" in RDB format, that is a one byte unsigned integer.\n * This function is not only used to load object types, but also special\n * \"types\" like the end-of-file type, the EXPIRE type, and so forth. */\nint rdbLoadType(rio *rdb) {\n    unsigned char type;\n    if (rioRead(rdb,&type,1) == 0) return -1;\n    return type;\n}\n\n/* This is only used to load old databases stored with the RDB_OPCODE_EXPIRETIME\n * opcode. New versions of Redis store using the RDB_OPCODE_EXPIRETIME_MS\n * opcode. On error -1 is returned, however this could be a valid time, so\n * to check for loading errors the caller should call rioGetReadError() after\n * calling this function. */\ntime_t rdbLoadTime(rio *rdb) {\n    int32_t t32;\n    if (rioRead(rdb,&t32,4) == 0) return -1;\n    return (time_t)t32;\n}\n\nint rdbSaveMillisecondTime(rio *rdb, long long t) {\n    int64_t t64 = (int64_t) t;\n    memrev64ifbe(&t64); /* Store in little endian. */\n    return rdbWriteRaw(rdb,&t64,8);\n}\n\n/* This function loads a time from the RDB file. It gets the version of the\n * RDB because, unfortunately, before Redis 5 (RDB version 9), the function\n * failed to convert data to/from little endian, so RDB files with keys having\n * expires could not be shared between big endian and little endian systems\n * (because the expire time will be totally wrong). The fix for this is just\n * to call memrev64ifbe(), however if we fix this for all the RDB versions,\n * this call will introduce an incompatibility for big endian systems:\n * after upgrading to Redis version 5 they will no longer be able to load their\n * own old RDB files. Because of that, we instead fix the function only for new\n * RDB versions, and load older RDB versions as we used to do in the past,\n * allowing big endian systems to load their own old RDB files.\n *\n * On I/O error the function returns LLONG_MAX, however if this is also a\n * valid stored value, the caller should use rioGetReadError() to check for\n * errors after calling this function. */\nlong long rdbLoadMillisecondTime(rio *rdb, int rdbver) {\n    int64_t t64;\n    if (rioRead(rdb,&t64,8) == 0) return LLONG_MAX;\n    if (rdbver >= 9) /* Check the top comment of this function. */\n        memrev64ifbe(&t64); /* Convert in big endian if the system is BE. */\n    return (long long)t64;\n}\n\n/* Saves an encoded length. The first two bits in the first byte are used to\n * hold the encoding type. See the RDB_* definitions for more information\n * on the types of encoding. */\nint rdbSaveLen(rio *rdb, uint64_t len) {\n    unsigned char buf[2];\n    size_t nwritten;\n\n    if (len < (1<<6)) {\n        /* Save a 6 bit len */\n        buf[0] = (len&0xFF)|(RDB_6BITLEN<<6);\n        if (rdbWriteRaw(rdb,buf,1) == -1) return -1;\n        nwritten = 1;\n    } else if (len < (1<<14)) {\n        /* Save a 14 bit len */\n        buf[0] = ((len>>8)&0xFF)|(RDB_14BITLEN<<6);\n        buf[1] = len&0xFF;\n        if (rdbWriteRaw(rdb,buf,2) == -1) return -1;\n        nwritten = 2;\n    } else if (len <= UINT32_MAX) {\n        /* Save a 32 bit len */\n        buf[0] = RDB_32BITLEN;\n        if (rdbWriteRaw(rdb,buf,1) == -1) return -1;\n        uint32_t len32 = htonl(len);\n        if (rdbWriteRaw(rdb,&len32,4) == -1) return -1;\n        nwritten = 1+4;\n    } else {\n        /* Save a 64 bit len */\n        buf[0] = RDB_64BITLEN;\n        if (rdbWriteRaw(rdb,buf,1) == -1) return -1;\n        len = htonu64(len);\n        if (rdbWriteRaw(rdb,&len,8) == -1) return -1;\n        nwritten = 1+8;\n    }\n    return nwritten;\n}\n\n\n/* Load an encoded length. If the loaded length is a normal length as stored\n * with rdbSaveLen(), the read length is set to '*lenptr'. If instead the\n * loaded length describes a special encoding that follows, then '*isencoded'\n * is set to 1 and the encoding format is stored at '*lenptr'.\n *\n * See the RDB_ENC_* definitions in rdb.h for more information on special\n * encodings.\n *\n * The function returns -1 on error, 0 on success. */\nint rdbLoadLenByRef(rio *rdb, int *isencoded, uint64_t *lenptr) {\n    unsigned char buf[2];\n    int type;\n\n    if (isencoded) *isencoded = 0;\n    if (rioRead(rdb,buf,1) == 0) return -1;\n    type = (buf[0]&0xC0)>>6;\n    if (type == RDB_ENCVAL) {\n        /* Read a 6 bit encoding type. */\n        if (isencoded) *isencoded = 1;\n        *lenptr = buf[0]&0x3F;\n    } else if (type == RDB_6BITLEN) {\n        /* Read a 6 bit len. */\n        *lenptr = buf[0]&0x3F;\n    } else if (type == RDB_14BITLEN) {\n        /* Read a 14 bit len. */\n        if (rioRead(rdb,buf+1,1) == 0) return -1;\n        *lenptr = ((buf[0]&0x3F)<<8)|buf[1];\n    } else if (buf[0] == RDB_32BITLEN) {\n        /* Read a 32 bit len. */\n        uint32_t len;\n        if (rioRead(rdb,&len,4) == 0) return -1;\n        *lenptr = ntohl(len);\n    } else if (buf[0] == RDB_64BITLEN) {\n        /* Read a 64 bit len. */\n        uint64_t len;\n        if (rioRead(rdb,&len,8) == 0) return -1;\n        *lenptr = ntohu64(len);\n    } else {\n        rdbReportCorruptRDB(\n            \"Unknown length encoding %d in rdbLoadLen()\",type);\n        return -1; /* Never reached. */\n    }\n    return 0;\n}\n\n/* This is like rdbLoadLenByRef() but directly returns the value read\n * from the RDB stream, signaling an error by returning RDB_LENERR\n * (since it is a too large count to be applicable in any Redis data\n * structure). */\nuint64_t rdbLoadLen(rio *rdb, int *isencoded) {\n    uint64_t len;\n\n    if (rdbLoadLenByRef(rdb,isencoded,&len) == -1) return RDB_LENERR;\n    return len;\n}\n\n/* Encodes the \"value\" argument as integer when it fits in the supported ranges\n * for encoded types. If the function successfully encodes the integer, the\n * representation is stored in the buffer pointer to by \"enc\" and the string\n * length is returned. Otherwise 0 is returned. */\nint rdbEncodeInteger(long long value, unsigned char *enc) {\n    if (value >= -(1<<7) && value <= (1<<7)-1) {\n        enc[0] = (RDB_ENCVAL<<6)|RDB_ENC_INT8;\n        enc[1] = value&0xFF;\n        return 2;\n    } else if (value >= -(1<<15) && value <= (1<<15)-1) {\n        enc[0] = (RDB_ENCVAL<<6)|RDB_ENC_INT16;\n        enc[1] = value&0xFF;\n        enc[2] = (value>>8)&0xFF;\n        return 3;\n    } else if (value >= -((long long)1<<31) && value <= ((long long)1<<31)-1) {\n        enc[0] = (RDB_ENCVAL<<6)|RDB_ENC_INT32;\n        enc[1] = value&0xFF;\n        enc[2] = (value>>8)&0xFF;\n        enc[3] = (value>>16)&0xFF;\n        enc[4] = (value>>24)&0xFF;\n        return 5;\n    } else {\n        return 0;\n    }\n}\n\n/* Loads an integer-encoded object with the specified encoding type \"enctype\".\n * The returned value changes according to the flags, see\n * rdbGenericLoadStringObject() for more info. */\nvoid *rdbLoadIntegerObject(rio *rdb, int enctype, int flags, size_t *lenptr) {\n    int plain = flags & RDB_LOAD_PLAIN;\n    int sds = flags & RDB_LOAD_SDS;\n    int encode = flags & RDB_LOAD_ENC;\n    unsigned char enc[4];\n    long long val;\n\n    if (enctype == RDB_ENC_INT8) {\n        if (rioRead(rdb,enc,1) == 0) return NULL;\n        val = (signed char)enc[0];\n    } else if (enctype == RDB_ENC_INT16) {\n        uint16_t v;\n        if (rioRead(rdb,enc,2) == 0) return NULL;\n        v = enc[0]|(enc[1]<<8);\n        val = (int16_t)v;\n    } else if (enctype == RDB_ENC_INT32) {\n        uint32_t v;\n        if (rioRead(rdb,enc,4) == 0) return NULL;\n        v = enc[0]|(enc[1]<<8)|(enc[2]<<16)|(enc[3]<<24);\n        val = (int32_t)v;\n    } else {\n        rdbReportCorruptRDB(\"Unknown RDB integer encoding type %d\",enctype);\n        return NULL; /* Never reached. */\n    }\n    if (plain || sds) {\n        char buf[LONG_STR_SIZE], *p;\n        int len = ll2string(buf,sizeof(buf),val);\n        if (lenptr) *lenptr = len;\n        p = (char*)(plain ? zmalloc(len, MALLOC_SHARED) : sdsnewlen(SDS_NOINIT,len));\n        memcpy(p,buf,len);\n        return p;\n    } else if (encode) {\n        return createStringObjectFromLongLongForValue(val);\n    } else {\n        return createObject(OBJ_STRING,sdsfromlonglong(val));\n    }\n}\n\n/* String objects in the form \"2391\" \"-100\" without any space and with a\n * range of values that can fit in an 8, 16 or 32 bit signed value can be\n * encoded as integers to save space */\nint rdbTryIntegerEncoding(char *s, size_t len, unsigned char *enc) {\n    long long value;\n    char *endptr, buf[32];\n\n    /* Check if it's possible to encode this value as a number */\n    value = strtoll(s, &endptr, 10);\n    if (endptr[0] != '\\0') return 0;\n    ll2string(buf,32,value);\n\n    /* If the number converted back into a string is not identical\n     * then it's not possible to encode the string as integer */\n    if (strlen(buf) != len || memcmp(buf,s,len)) return 0;\n\n    return rdbEncodeInteger(value,enc);\n}\n\nssize_t rdbSaveLzfBlob(rio *rdb, void *data, size_t compress_len,\n                       size_t original_len) {\n    unsigned char byte;\n    ssize_t n, nwritten = 0;\n\n    /* Data compressed! Let's save it on disk */\n    byte = (RDB_ENCVAL<<6)|RDB_ENC_LZF;\n    if ((n = rdbWriteRaw(rdb,&byte,1)) == -1) goto writeerr;\n    nwritten += n;\n\n    if ((n = rdbSaveLen(rdb,compress_len)) == -1) goto writeerr;\n    nwritten += n;\n\n    if ((n = rdbSaveLen(rdb,original_len)) == -1) goto writeerr;\n    nwritten += n;\n\n    if ((n = rdbWriteRaw(rdb,data,compress_len)) == -1) goto writeerr;\n    nwritten += n;\n\n    return nwritten;\n\nwriteerr:\n    return -1;\n}\n\nssize_t rdbSaveLzfStringObject(rio *rdb, const unsigned char *s, size_t len) {\n    char rgbuf[2048];\n    size_t comprlen, outlen;\n    void *out = rgbuf;\n\n    /* We require at least four bytes compression for this to be worth it */\n    if (len <= 4) return 0;\n    outlen = len-4;\n    if (outlen >= sizeof(rgbuf))\n        if ((out = zmalloc(outlen+1, MALLOC_LOCAL)) == NULL) return 0;\n    comprlen = lzf_compress(s, len, out, outlen);\n    if (comprlen == 0) {\n        if (out != rgbuf)\n            zfree(out);\n        return 0;\n    }\n    ssize_t nwritten = rdbSaveLzfBlob(rdb, out, comprlen, len);\n    if (out != rgbuf)\n        zfree(out);\n    return nwritten;\n}\n\n/* Load an LZF compressed string in RDB format. The returned value\n * changes according to 'flags'. For more info check the\n * rdbGenericLoadStringObject() function. */\nvoid *rdbLoadLzfStringObject(rio *rdb, int flags, size_t *lenptr) {\n    int plain = flags & RDB_LOAD_PLAIN;\n    int sds = flags & RDB_LOAD_SDS;\n    uint64_t len, clen;\n    unsigned char *c = NULL;\n    char *val = NULL;\n\n    if ((clen = rdbLoadLen(rdb,NULL)) == RDB_LENERR) return NULL;\n    if ((len = rdbLoadLen(rdb,NULL)) == RDB_LENERR) return NULL;\n    if ((c = (unsigned char*)ztrymalloc(clen)) == NULL) {\n        serverLog(g_pserver->loading? LL_WARNING: LL_VERBOSE, \"rdbLoadLzfStringObject failed allocating %llu bytes\", (unsigned long long)clen);\n        goto err;\n    }\n\n    /* Allocate our target according to the uncompressed size. */\n    if (plain) {\n        val = (char*)ztrymalloc(len);\n    } else {\n        val = sdstrynewlen(SDS_NOINIT,len);\n    }\n    if (!val) {\n        serverLog(g_pserver->loading? LL_WARNING: LL_VERBOSE, \"rdbLoadLzfStringObject failed allocating %llu bytes\", (unsigned long long)len);\n        goto err;\n    }\n\n    if (lenptr) *lenptr = len;\n\n    /* Load the compressed representation and uncompress it to target. */\n    if (rioRead(rdb,c,clen) == 0) goto err;\n    if (lzf_decompress(c,clen,val,len) != len) {\n        rdbReportCorruptRDB(\"Invalid LZF compressed string\");\n        goto err;\n    }\n    zfree(c);\n\n    if (plain || sds) {\n        return val;\n    } else {\n        return createObject(OBJ_STRING,val);\n    }\nerr:\n    zfree(c);\n    if (plain)\n        zfree(val);\n    else\n        sdsfree(val);\n    return NULL;\n}\n\n/* Save a string object as [len][data] on disk. If the object is a string\n * representation of an integer value we try to save it in a special form */\nssize_t rdbSaveRawString(rio *rdb, const unsigned char *s, size_t len) {\n    int enclen;\n    ssize_t n, nwritten = 0;\n\n    /* Try integer encoding */\n    if (len <= 11) {\n        unsigned char buf[5];\n        if ((enclen = rdbTryIntegerEncoding((char*)s,len,buf)) > 0) {\n            if (rdbWriteRaw(rdb,buf,enclen) == -1) return -1;\n            return enclen;\n        }\n    }\n\n    /* Try LZF compression - under 20 bytes it's unable to compress even\n     * aaaaaaaaaaaaaaaaaa so skip it */\n    if (g_pserver->rdb_compression && len > 20) {\n        n = rdbSaveLzfStringObject(rdb,(const unsigned char*)s,len);\n        if (n == -1) return -1;\n        if (n > 0) return n;\n        /* Return value of 0 means data can't be compressed, save the old way */\n    }\n\n    /* Store verbatim */\n    if ((n = rdbSaveLen(rdb,len)) == -1) return -1;\n    nwritten += n;\n    if (len > 0) {\n        if (rdbWriteRaw(rdb,(unsigned char*)s,len) == -1) return -1;\n        nwritten += len;\n    }\n    return nwritten;\n}\n\n/* Save a long long value as either an encoded string or a string. */\nssize_t rdbSaveLongLongAsStringObject(rio *rdb, long long value) {\n    unsigned char buf[32];\n    ssize_t n, nwritten = 0;\n    int enclen = rdbEncodeInteger(value,buf);\n    if (enclen > 0) {\n        return rdbWriteRaw(rdb,buf,enclen);\n    } else {\n        /* Encode as string */\n        enclen = ll2string((char*)buf,32,value);\n        serverAssert(enclen < 32);\n        if ((n = rdbSaveLen(rdb,enclen)) == -1) return -1;\n        nwritten += n;\n        if ((n = rdbWriteRaw(rdb,buf,enclen)) == -1) return -1;\n        nwritten += n;\n    }\n    return nwritten;\n}\n\n/* Like rdbSaveRawString() gets a Redis object instead. */\nssize_t rdbSaveStringObject(rio *rdb, robj_roptr obj) {\n    /* Avoid to decode the object, then encode it again, if the\n     * object is already integer encoded. */\n    if (obj->encoding == OBJ_ENCODING_INT) {\n        return rdbSaveLongLongAsStringObject(rdb,(long)ptrFromObj(obj));\n    } else {\n        serverAssertWithInfo(NULL,obj,sdsEncodedObject(obj));\n        return rdbSaveRawString(rdb,(unsigned char*)szFromObj(obj),sdslen(szFromObj(obj)));\n    }\n}\n\n/* Load a string object from an RDB file according to flags:\n *\n * RDB_LOAD_NONE (no flags): load an RDB object, unencoded.\n * RDB_LOAD_ENC: If the returned type is a Redis object, try to\n *               encode it in a special way to be more memory\n *               efficient. When this flag is passed the function\n *               no longer guarantees that ptrFromObj(obj) is an SDS string.\n * RDB_LOAD_PLAIN: Return a plain string allocated with zmalloc()\n *                 instead of a Redis object with an sds in it.\n * RDB_LOAD_SDS: Return an SDS string instead of a Redis object.\n *\n * On I/O error NULL is returned.\n */\nvoid *rdbGenericLoadStringObject(rio *rdb, int flags, size_t *lenptr) {\n    int encode = flags & RDB_LOAD_ENC;\n    int plain = flags & RDB_LOAD_PLAIN;\n    int sds = flags & RDB_LOAD_SDS;\n    int isencoded;\n    unsigned long long len;\n\n    len = rdbLoadLen(rdb,&isencoded);\n    if (len == RDB_LENERR) return NULL;\n\n    if (isencoded) {\n        switch(len) {\n        case RDB_ENC_INT8:\n        case RDB_ENC_INT16:\n        case RDB_ENC_INT32:\n            return rdbLoadIntegerObject(rdb,len,flags,lenptr);\n        case RDB_ENC_LZF:\n            return rdbLoadLzfStringObject(rdb,flags,lenptr);\n        default:\n            rdbReportCorruptRDB(\"Unknown RDB string encoding type %llu\",len);\n            return NULL;\n        }\n    }\n\n    if (plain || sds) {\n        void *buf = plain ? ztrymalloc(len) : sdstrynewlen(SDS_NOINIT,len);\n        if (!buf) {\n            serverLog(g_pserver->loading? LL_WARNING: LL_VERBOSE, \"rdbGenericLoadStringObject failed allocating %llu bytes\", len);\n            return NULL;\n        }\n        if (lenptr) *lenptr = len;\n        if (len && rioRead(rdb,buf,len) == 0) {\n            if (plain)\n                zfree(buf);\n            else\n                sdsfree((char*)buf);\n            return NULL;\n        }\n        return buf;\n    } else {\n        robj *o = encode ? tryCreateStringObject(SDS_NOINIT,len) :\n                           tryCreateRawStringObject(SDS_NOINIT,len);\n        if (!o) {\n            serverLog(g_pserver->loading? LL_WARNING: LL_VERBOSE, \"rdbGenericLoadStringObject failed allocating %llu bytes\", len);\n            return NULL;\n        }\n        if (len && rioRead(rdb,ptrFromObj(o),len) == 0) {\n            decrRefCount(o);\n            return NULL;\n        }\n        return o;\n    }\n}\n\nsdsstring rdbLoadString(rio *rdb){\n    sds str = (sds)rdbGenericLoadStringObject(rdb,RDB_LOAD_SDS,NULL);\n    return sdsstring(str);\n}\n\nrobj *rdbLoadStringObject(rio *rdb) {\n    return (robj*)rdbGenericLoadStringObject(rdb,RDB_LOAD_NONE,NULL);\n}\n\nrobj *rdbLoadEncodedStringObject(rio *rdb) {\n    return (robj*)rdbGenericLoadStringObject(rdb,RDB_LOAD_ENC,NULL);\n}\n\n/* Save a double value. Doubles are saved as strings prefixed by an unsigned\n * 8 bit integer specifying the length of the representation.\n * This 8 bit integer has special values in order to specify the following\n * conditions:\n * 253: not a number\n * 254: + inf\n * 255: - inf\n */\nint rdbSaveDoubleValue(rio *rdb, double val) {\n    unsigned char buf[128];\n    int len;\n\n    if (std::isnan(val)) {\n        buf[0] = 253;\n        len = 1;\n    } else if (!std::isfinite(val)) {\n        len = 1;\n        buf[0] = (val < 0) ? 255 : 254;\n    } else {\n#if (DBL_MANT_DIG >= 52) && (LLONG_MAX == 0x7fffffffffffffffLL)\n        /* Check if the float is in a safe range to be casted into a\n         * long long. We are assuming that long long is 64 bit here.\n         * Also we are assuming that there are no implementations around where\n         * double has precision < 52 bit.\n         *\n         * Under this assumptions we test if a double is inside an interval\n         * where casting to long long is safe. Then using two castings we\n         * make sure the decimal part is zero. If all this is true we use\n         * integer printing function that is much faster. */\n        double min = -4503599627370495; /* (2^52)-1 */\n        double max = 4503599627370496; /* -(2^52) */\n        if (val > min && val < max && val == ((double)((long long)val)))\n            ll2string((char*)buf+1,sizeof(buf)-1,(long long)val);\n        else\n#endif\n            snprintf((char*)buf+1,sizeof(buf)-1,\"%.17g\",val);\n        buf[0] = strlen((char*)buf+1);\n        len = buf[0]+1;\n    }\n    return rdbWriteRaw(rdb,buf,len);\n}\n\n/* For information about double serialization check rdbSaveDoubleValue() */\nint rdbLoadDoubleValue(rio *rdb, double *val) {\n    char buf[256];\n    unsigned char len;\n\n    if (rioRead(rdb,&len,1) == 0) return -1;\n    switch(len) {\n    case 255: *val = R_NegInf; return 0;\n    case 254: *val = R_PosInf; return 0;\n    case 253: *val = R_Nan; return 0;\n    default:\n        if (rioRead(rdb,buf,len) == 0) return -1;\n        buf[len] = '\\0';\n        if (sscanf(buf, \"%lg\", val)!=1) return -1;\n        return 0;\n    }\n}\n\n/* Saves a double for RDB 8 or greater, where IE754 binary64 format is assumed.\n * We just make sure the integer is always stored in little endian, otherwise\n * the value is copied verbatim from memory to disk.\n *\n * Return -1 on error, the size of the serialized value on success. */\nint rdbSaveBinaryDoubleValue(rio *rdb, double val) {\n    memrev64ifbe(&val);\n    return rdbWriteRaw(rdb,&val,sizeof(val));\n}\n\n/* Loads a double from RDB 8 or greater. See rdbSaveBinaryDoubleValue() for\n * more info. On error -1 is returned, otherwise 0. */\nint rdbLoadBinaryDoubleValue(rio *rdb, double *val) {\n    if (rioRead(rdb,val,sizeof(*val)) == 0) return -1;\n    memrev64ifbe(val);\n    return 0;\n}\n\n/* Like rdbSaveBinaryDoubleValue() but single precision. */\nint rdbSaveBinaryFloatValue(rio *rdb, float val) {\n    memrev32ifbe(&val);\n    return rdbWriteRaw(rdb,&val,sizeof(val));\n}\n\n/* Like rdbLoadBinaryDoubleValue() but single precision. */\nint rdbLoadBinaryFloatValue(rio *rdb, float *val) {\n    if (rioRead(rdb,val,sizeof(*val)) == 0) return -1;\n    memrev32ifbe(val);\n    return 0;\n}\n\n/* Save the object type of object \"o\". */\nint rdbSaveObjectType(rio *rdb, robj_roptr o) {\n    switch (o->type) {\n    case OBJ_STRING:\n        return rdbSaveType(rdb,RDB_TYPE_STRING);\n    case OBJ_LIST:\n        if (o->encoding == OBJ_ENCODING_QUICKLIST)\n            return rdbSaveType(rdb,RDB_TYPE_LIST_QUICKLIST);\n        else\n            serverPanic(\"Unknown list encoding: %d\", o->encoding);\n    case OBJ_SET:\n        if (o->encoding == OBJ_ENCODING_INTSET)\n            return rdbSaveType(rdb,RDB_TYPE_SET_INTSET);\n        else if (o->encoding == OBJ_ENCODING_HT)\n            return rdbSaveType(rdb,RDB_TYPE_SET);\n        else\n            serverPanic(\"Unknown set encoding: %d\", o->encoding);\n    case OBJ_ZSET:\n        if (o->encoding == OBJ_ENCODING_ZIPLIST)\n            return rdbSaveType(rdb,RDB_TYPE_ZSET_ZIPLIST);\n        else if (o->encoding == OBJ_ENCODING_SKIPLIST)\n            return rdbSaveType(rdb,RDB_TYPE_ZSET_2);\n        else\n            serverPanic(\"Unknown sorted set encoding: %d\", o->encoding);\n    case OBJ_HASH:\n        if (o->encoding == OBJ_ENCODING_ZIPLIST)\n            return rdbSaveType(rdb,RDB_TYPE_HASH_ZIPLIST);\n        else if (o->encoding == OBJ_ENCODING_HT)\n            return rdbSaveType(rdb,RDB_TYPE_HASH);\n        else\n            serverPanic(\"Unknown hash encoding: %d\", o->encoding);\n    case OBJ_STREAM:\n        return rdbSaveType(rdb,RDB_TYPE_STREAM_LISTPACKS);\n    case OBJ_MODULE:\n        return rdbSaveType(rdb,RDB_TYPE_MODULE_2);\n    case OBJ_CRON:\n        return rdbSaveType(rdb,RDB_TYPE_CRON);\n    default:\n        serverPanic(\"Unknown object type: %d\", o->type);\n    }\n    return -1; /* avoid warning */\n}\n\n/* Use rdbLoadType() to load a TYPE in RDB format, but returns -1 if the\n * type is not specifically a valid Object Type. */\nint rdbLoadObjectType(rio *rdb) {\n    int type;\n    if ((type = rdbLoadType(rdb)) == -1) return -1;\n    if (!rdbIsObjectType(type)) return -1;\n    return type;\n}\n\n/* This helper function serializes a consumer group Pending Entries List (PEL)\n * into the RDB file. The 'nacks' argument tells the function if also persist\n * the informations about the not acknowledged message, or if to persist\n * just the IDs: this is useful because for the global consumer group PEL\n * we serialized the NACKs as well, but when serializing the local consumer\n * PELs we just add the ID, that will be resolved inside the global PEL to\n * put a reference to the same structure. */\nssize_t rdbSaveStreamPEL(rio *rdb, rax *pel, int nacks) {\n    ssize_t n, nwritten = 0;\n\n    /* Number of entries in the PEL. */\n    if ((n = rdbSaveLen(rdb,raxSize(pel))) == -1) return -1;\n    nwritten += n;\n\n    /* Save each entry. */\n    raxIterator ri;\n    raxStart(&ri,pel);\n    raxSeek(&ri,\"^\",NULL,0);\n    while(raxNext(&ri)) {\n        /* We store IDs in raw form as 128 big big endian numbers, like\n         * they are inside the radix tree key. */\n        if ((n = rdbWriteRaw(rdb,ri.key,sizeof(streamID))) == -1) {\n            raxStop(&ri);\n            return -1;\n        }\n        nwritten += n;\n\n        if (nacks) {\n            streamNACK *nack = (streamNACK*)ri.data;\n            if ((n = rdbSaveMillisecondTime(rdb,nack->delivery_time)) == -1) {\n                raxStop(&ri);\n                return -1;\n            }\n            nwritten += n;\n            if ((n = rdbSaveLen(rdb,nack->delivery_count)) == -1) {\n                raxStop(&ri);\n                return -1;\n            }\n            nwritten += n;\n            /* We don't save the consumer name: we'll save the pending IDs\n             * for each consumer in the consumer PEL, and resolve the consumer\n             * at loading time. */\n        }\n    }\n    raxStop(&ri);\n    return nwritten;\n}\n\n/* Serialize the consumers of a stream consumer group into the RDB. Helper\n * function for the stream data type serialization. What we do here is to\n * persist the consumer metadata, and it's PEL, for each consumer. */\nsize_t rdbSaveStreamConsumers(rio *rdb, streamCG *cg) {\n    ssize_t n, nwritten = 0;\n\n    /* Number of consumers in this consumer group. */\n    if ((n = rdbSaveLen(rdb,raxSize(cg->consumers))) == -1) return -1;\n    nwritten += n;\n\n    /* Save each consumer. */\n    raxIterator ri;\n    raxStart(&ri,cg->consumers);\n    raxSeek(&ri,\"^\",NULL,0);\n    while(raxNext(&ri)) {\n        streamConsumer *consumer = (streamConsumer*)ri.data;\n\n        /* Consumer name. */\n        if ((n = rdbSaveRawString(rdb,ri.key,ri.key_len)) == -1) {\n            raxStop(&ri);\n            return -1;\n        }\n        nwritten += n;\n\n        /* Last seen time. */\n        if ((n = rdbSaveMillisecondTime(rdb,consumer->seen_time)) == -1) {\n            raxStop(&ri);\n            return -1;\n        }\n        nwritten += n;\n\n        /* Consumer PEL, without the ACKs (see last parameter of the function\n         * passed with value of 0), at loading time we'll lookup the ID\n         * in the consumer group global PEL and will put a reference in the\n         * consumer local PEL. */\n        if ((n = rdbSaveStreamPEL(rdb,consumer->pel,0)) == -1) {\n            raxStop(&ri);\n            return -1;\n        }\n        nwritten += n;\n    }\n    raxStop(&ri);\n    return nwritten;\n}\n\n/* Save a Redis object.\n * Returns -1 on error, number of bytes written on success. */\nssize_t rdbSaveObject(rio *rdb, robj_roptr o, robj_roptr key) {\n    ssize_t n = 0, nwritten = 0;\n\n    if (o->type == OBJ_STRING) {\n        /* Save a string value */\n        if ((n = rdbSaveStringObject(rdb,o)) == -1) return -1;\n        nwritten += n;\n    } else if (o->type == OBJ_LIST) {\n        /* Save a list value */\n        if (o->encoding == OBJ_ENCODING_QUICKLIST) {\n            quicklist *ql = (quicklist*)ptrFromObj(o);\n            quicklistNode *node = ql->head;\n\n            if ((n = rdbSaveLen(rdb,ql->len)) == -1) return -1;\n            nwritten += n;\n\n            while(node) {\n                if (quicklistNodeIsCompressed(node)) {\n                    void *data;\n                    size_t compress_len = quicklistGetLzf(node, &data);\n                    if ((n = rdbSaveLzfBlob(rdb,data,compress_len,node->sz)) == -1) return -1;\n                    nwritten += n;\n                } else {\n                    if ((n = rdbSaveRawString(rdb,node->zl,node->sz)) == -1) return -1;\n                    nwritten += n;\n                }\n                node = node->next;\n            }\n        } else {\n            serverPanic(\"Unknown list encoding\");\n        }\n    } else if (o->type == OBJ_SET) {\n        /* Save a set value */\n        if (o->encoding == OBJ_ENCODING_HT) {\n            dict *set = (dict*)ptrFromObj(o);\n            dictIterator *di = dictGetIterator(set);\n            dictEntry *de;\n\n            if ((n = rdbSaveLen(rdb,dictSize(set))) == -1) {\n                dictReleaseIterator(di);\n                return -1;\n            }\n            nwritten += n;\n\n            while((de = dictNext(di)) != NULL) {\n                sds ele = (sds)dictGetKey(de);\n                if ((n = rdbSaveRawString(rdb,(unsigned char*)ele,sdslen(ele)))\n                    == -1)\n                {\n                    dictReleaseIterator(di);\n                    return -1;\n                }\n                nwritten += n;\n            }\n            dictReleaseIterator(di);\n        } else if (o->encoding == OBJ_ENCODING_INTSET) {\n            size_t l = intsetBlobLen((intset*)ptrFromObj(o));\n\n            if ((n = rdbSaveRawString(rdb,(unsigned char*)szFromObj(o),l)) == -1) return -1;\n            nwritten += n;\n        } else {\n            serverPanic(\"Unknown set encoding\");\n        }\n    } else if (o->type == OBJ_ZSET) {\n        /* Save a sorted set value */\n        if (o->encoding == OBJ_ENCODING_ZIPLIST) {\n            size_t l = ziplistBlobLen((unsigned char*)ptrFromObj(o));\n\n            if ((n = rdbSaveRawString(rdb,(unsigned char*)ptrFromObj(o),l)) == -1) return -1;\n            nwritten += n;\n        } else if (o->encoding == OBJ_ENCODING_SKIPLIST) {\n            zset *zs = (zset*)ptrFromObj(o);\n            zskiplist *zsl = zs->zsl;\n\n            if ((n = rdbSaveLen(rdb,zsl->length)) == -1) return -1;\n            nwritten += n;\n\n            /* We save the skiplist elements from the greatest to the smallest\n             * (that's trivial since the elements are already ordered in the\n             * skiplist): this improves the load process, since the next loaded\n             * element will always be the smaller, so adding to the skiplist\n             * will always immediately stop at the head, making the insertion\n             * O(1) instead of O(log(N)). */\n            zskiplistNode *zn = zsl->tail;\n            while (zn != NULL) {\n                if ((n = rdbSaveRawString(rdb,\n                    (unsigned char*)zn->ele,sdslen(zn->ele))) == -1)\n                {\n                    return -1;\n                }\n                nwritten += n;\n                if ((n = rdbSaveBinaryDoubleValue(rdb,zn->score)) == -1)\n                    return -1;\n                nwritten += n;\n                zn = zn->backward;\n            }\n        } else {\n            serverPanic(\"Unknown sorted set encoding\");\n        }\n    } else if (o->type == OBJ_HASH) {\n        /* Save a hash value */\n        if (o->encoding == OBJ_ENCODING_ZIPLIST) {\n            size_t l = ziplistBlobLen((unsigned char*)ptrFromObj(o));\n\n            if ((n = rdbSaveRawString(rdb,(unsigned char*)ptrFromObj(o),l)) == -1) return -1;\n            nwritten += n;\n\n        } else if (o->encoding == OBJ_ENCODING_HT) {\n            dictIterator *di = dictGetIterator((dict*)ptrFromObj(o));\n            dictEntry *de;\n\n            if ((n = rdbSaveLen(rdb,dictSize((dict*)ptrFromObj(o)))) == -1) {\n                dictReleaseIterator(di);\n                return -1;\n            }\n            nwritten += n;\n\n            while((de = dictNext(di)) != NULL) {\n                sds field = (sds)dictGetKey(de);\n                sds value = (sds)dictGetVal(de);\n\n                if ((n = rdbSaveRawString(rdb,(unsigned char*)field,\n                        sdslen(field))) == -1)\n                {\n                    dictReleaseIterator(di);\n                    return -1;\n                }\n                nwritten += n;\n                if ((n = rdbSaveRawString(rdb,(unsigned char*)value,\n                        sdslen(value))) == -1)\n                {\n                    dictReleaseIterator(di);\n                    return -1;\n                }\n                nwritten += n;\n            }\n            dictReleaseIterator(di);\n        } else {\n            serverPanic(\"Unknown hash encoding\");\n        }\n    } else if (o->type == OBJ_STREAM) {\n        /* Store how many listpacks we have inside the radix tree. */\n        stream *s = (stream*)ptrFromObj(o);\n        rax *rax = s->rax;\n        if ((n = rdbSaveLen(rdb,raxSize(rax))) == -1) return -1;\n        nwritten += n;\n\n        /* Serialize all the listpacks inside the radix tree as they are,\n         * when loading back, we'll use the first entry of each listpack\n         * to insert it back into the radix tree. */\n        raxIterator ri;\n        raxStart(&ri,rax);\n        raxSeek(&ri,\"^\",NULL,0);\n        while (raxNext(&ri)) {\n            unsigned char *lp = (unsigned char*)ri.data;\n            size_t lp_bytes = lpBytes(lp);\n            if ((n = rdbSaveRawString(rdb,ri.key,ri.key_len)) == -1) {\n                raxStop(&ri);\n                return -1;\n            }\n            nwritten += n;\n            if ((n = rdbSaveRawString(rdb,lp,lp_bytes)) == -1) {\n                raxStop(&ri);\n                return -1;\n            }\n            nwritten += n;\n        }\n        raxStop(&ri);\n\n        /* Save the number of elements inside the stream. We cannot obtain\n         * this easily later, since our macro nodes should be checked for\n         * number of items: not a great CPU / space tradeoff. */\n        if ((n = rdbSaveLen(rdb,s->length)) == -1) return -1;\n        nwritten += n;\n        /* Save the last entry ID. */\n        if ((n = rdbSaveLen(rdb,s->last_id.ms)) == -1) return -1;\n        nwritten += n;\n        if ((n = rdbSaveLen(rdb,s->last_id.seq)) == -1) return -1;\n        nwritten += n;\n\n        /* The consumer groups and their clients are part of the stream\n         * type, so serialize every consumer group. */\n\n        /* Save the number of groups. */\n        size_t num_cgroups = s->cgroups ? raxSize(s->cgroups) : 0;\n        if ((n = rdbSaveLen(rdb,num_cgroups)) == -1) return -1;\n        nwritten += n;\n\n        if (num_cgroups) {\n            /* Serialize each consumer group. */\n            raxStart(&ri,s->cgroups);\n            raxSeek(&ri,\"^\",NULL,0);\n            while(raxNext(&ri)) {\n                streamCG *cg = (streamCG*)ri.data;\n\n                /* Save the group name. */\n                if ((n = rdbSaveRawString(rdb,ri.key,ri.key_len)) == -1) {\n                    raxStop(&ri);\n                    return -1;\n                }\n                nwritten += n;\n\n                /* Last ID. */\n                if ((n = rdbSaveLen(rdb,cg->last_id.ms)) == -1) {\n                    raxStop(&ri);\n                    return -1;\n                }\n                nwritten += n;\n                if ((n = rdbSaveLen(rdb,cg->last_id.seq)) == -1) {\n                    raxStop(&ri);\n                    return -1;\n                }\n                nwritten += n;\n\n                /* Save the global PEL. */\n                if ((n = rdbSaveStreamPEL(rdb,cg->pel,1)) == -1) {\n                    raxStop(&ri);\n                    return -1;\n                }\n                nwritten += n;\n\n                /* Save the consumers of this group. */\n                if ((n = rdbSaveStreamConsumers(rdb,cg)) == -1) {\n                    raxStop(&ri);\n                    return -1;\n                }\n                nwritten += n;\n            }\n            raxStop(&ri);\n        }\n    } else if (o->type == OBJ_MODULE) {\n        /* Save a module-specific value. */\n        RedisModuleIO io;\n        moduleValue *mv = (moduleValue*)ptrFromObj(o);\n        moduleType *mt = mv->type;\n\n        /* Write the \"module\" identifier as prefix, so that we'll be able\n         * to call the right module during loading. */\n        int retval = rdbSaveLen(rdb,mt->id);\n        if (retval == -1) return -1;\n        moduleInitIOContext(io,mt,rdb,key.unsafe_robjcast());\n        io.bytes += retval;\n\n        /* Then write the module-specific representation + EOF marker. */\n        mt->rdb_save(&io,mv->value);\n        retval = rdbSaveLen(rdb,RDB_MODULE_OPCODE_EOF);\n        if (retval == -1)\n            io.error = 1;\n        else\n            io.bytes += retval;\n\n        if (io.ctx) {\n            moduleFreeContext(io.ctx);\n            zfree(io.ctx);\n        }\n        return io.error ? -1 : (ssize_t)io.bytes;\n    } else if (o->type == OBJ_CRON) {\n        cronjob *job = (cronjob*)ptrFromObj(o);\n        nwritten = rdbSaveRawString(rdb, (const unsigned char*)job->script.get(), job->script.size());\n        nwritten += rdbSaveMillisecondTime(rdb, job->startTime);\n        nwritten += rdbSaveMillisecondTime(rdb, job->interval);\n        nwritten += rdbSaveLen(rdb, job->veckeys.size());\n        for (auto &key : job->veckeys)\n            nwritten += rdbSaveRawString(rdb, (const unsigned char*)key.get(), key.size());\n        nwritten += rdbSaveLen(rdb, job->vecargs.size());\n        for (auto &arg : job->vecargs)\n            nwritten += rdbSaveRawString(rdb, (const unsigned char*)arg.get(), arg.size());\n    } else {\n        serverPanic(\"Unknown object type\");\n    }\n    return nwritten;\n}\n\n/* Save an AUX field. */\nssize_t rdbSaveAuxField(rio *rdb, const void *key, size_t keylen, const void *val, size_t vallen) {\n    ssize_t ret, len = 0;\n    if ((ret = rdbSaveType(rdb,RDB_OPCODE_AUX)) == -1) return -1;\n    len += ret;\n    if ((ret = rdbSaveRawString(rdb,(const unsigned char*)key,keylen)) == -1) return -1;\n    len += ret;\n    if ((ret = rdbSaveRawString(rdb,(const unsigned char*)val,vallen)) == -1) return -1;\n    len += ret;\n    return len;\n}\n\n/* Wrapper for rdbSaveAuxField() used when key/val length can be obtained\n * with strlen(). */\nssize_t rdbSaveAuxFieldStrStr(rio *rdb, const char *key, const char *val) {\n    return rdbSaveAuxField(rdb,key,strlen(key),val,strlen(val));\n}\n\n/* Wrapper for strlen(key) + integer type (up to long long range). */\nssize_t rdbSaveAuxFieldStrInt(rio *rdb, const char *key, long long val) {\n    char buf[LONG_STR_SIZE];\n    int vlen = ll2string(buf,sizeof(buf),val);\n    return rdbSaveAuxField(rdb,key,strlen(key),buf,vlen);\n}\n\n/* Return the length the object will have on disk if saved with\n * the rdbSaveObject() function. Currently we use a trick to get\n * this length with very little changes to the code. In the future\n * we could switch to a faster solution. */\nsize_t rdbSavedObjectLen(robj *o, robj *key) {\n    ssize_t len = rdbSaveObject(NULL,o,key);\n    serverAssertWithInfo(NULL,o,len != -1);\n    return len;\n}\n\n/* Save a key-value pair, with expire time, type, key, value.\n * On error -1 is returned.\n * On success if the key was actually saved 1 is returned. */\nint rdbSaveKeyValuePair(rio *rdb, robj_roptr key, robj_roptr val, const expireEntry *pexpire) {\n    int savelru = g_pserver->maxmemory_policy & MAXMEMORY_FLAG_LRU;\n    int savelfu = g_pserver->maxmemory_policy & MAXMEMORY_FLAG_LFU;\n\n    /* Save the expire time */\n    long long expiretime = INVALID_EXPIRE;\n    if (pexpire != nullptr && pexpire->FGetPrimaryExpire(&expiretime)) {\n        if (rdbSaveType(rdb,RDB_OPCODE_EXPIRETIME_MS) == -1) return -1;\n        if (rdbSaveMillisecondTime(rdb,expiretime) == -1) return -1;\n    }\n\n    /* Save the LRU info. */\n    if (savelru) {\n        uint64_t idletime = estimateObjectIdleTime(val);\n        idletime /= 1000; /* Using seconds is enough and requires less space.*/\n        if (rdbSaveType(rdb,RDB_OPCODE_IDLE) == -1) return -1;\n        if (rdbSaveLen(rdb,idletime) == -1) return -1;\n    }\n\n    /* Save the LFU info. */\n    if (savelfu) {\n        uint8_t buf[1];\n        buf[0] = LFUDecrAndReturn(val);\n        /* We can encode this in exactly two bytes: the opcode and an 8\n         * bit counter, since the frequency is logarithmic with a 0-255 range.\n         * Note that we do not store the halving time because to reset it\n         * a single time when loading does not affect the frequency much. */\n        if (rdbSaveType(rdb,RDB_OPCODE_FREQ) == -1) return -1;\n        if (rdbWriteRaw(rdb,buf,1) == -1) return -1;\n    }\n\n    char szT[32];\n    if (g_pserver->fActiveReplica) {\n        snprintf(szT, sizeof(szT), \"%\" PRIu64, mvccFromObj(val));\n        if (rdbSaveAuxFieldStrStr(rdb,\"mvcc-tstamp\", szT) == -1) return -1;\n    }\n\n    /* Save type, key, value */\n    if (rdbSaveObjectType(rdb,val) == -1) return -1;\n    if (rdbSaveStringObject(rdb,key) == -1) return -1;\n    if (rdbSaveObject(rdb,val,key) == -1) return -1;\n\n    /* Delay return if required (for testing) */\n    if (serverTL->getRdbKeySaveDelay()) {\n        int sleepTime = serverTL->getRdbKeySaveDelay();\n        while (!g_pserver->rdbThreadVars.fRdbThreadCancel && sleepTime > 0) {\n            int sleepThisTime = std::min(100, sleepTime);\n            debugDelay(sleepThisTime);\n            sleepTime -= sleepThisTime;\n        }\n    }\n\n    /* Save expire entry after as it will apply to the previously loaded key */\n    /*  This is because we update the expire datastructure directly without buffering */\n    if (pexpire != nullptr)\n    {\n        for (auto itr : *pexpire)\n        {\n            if (itr.subkey() == nullptr)\n                continue;   // already saved\n            snprintf(szT, sizeof(szT), \"%lld\", itr.when());\n            rdbSaveAuxFieldStrStr(rdb,\"keydb-subexpire-key\",itr.subkey());\n            rdbSaveAuxFieldStrStr(rdb,\"keydb-subexpire-when\",szT);\n        }\n    }\n\n    return 1;\n}\n\n/* Save a few default AUX fields with information about the RDB generated. */\nint rdbSaveInfoAuxFields(rio *rdb, int rdbflags, rdbSaveInfo *rsi) {\n    int redis_bits = (sizeof(void*) == 8) ? 64 : 32;\n    int aof_preamble = (rdbflags & RDBFLAGS_AOF_PREAMBLE) != 0;\n\n    /* Add a few fields about the state when the RDB was created. */\n    if (rdbSaveAuxFieldStrStr(rdb,\"redis-ver\",KEYDB_REAL_VERSION) == -1) return -1;\n    if (rdbSaveAuxFieldStrInt(rdb,\"redis-bits\",redis_bits) == -1) return -1;\n    if (rdbSaveAuxFieldStrInt(rdb,\"ctime\",time(NULL)) == -1) return -1;\n    if (rdbSaveAuxFieldStrInt(rdb,\"used-mem\",zmalloc_used_memory()) == -1) return -1;\n\n    /* Handle saving options that generate aux fields. */\n    if (rsi) {\n        if (rdbSaveAuxFieldStrInt(rdb,\"repl-stream-db\",rsi->repl_stream_db)\n            == -1) return -1;\n        if (rdbSaveAuxFieldStrStr(rdb,\"repl-id\",rsi->repl_id)\n            == -1) return -1;\n        if (rdbSaveAuxFieldStrInt(rdb,\"repl-offset\",rsi->master_repl_offset)\n            == -1) return -1;\n        if (g_pserver->fActiveReplica) {\n            sdsstring val = sdsstring(sdsempty());\n\n            for (auto &msi : rsi->vecmastersaveinfo) {\n                if (msi.masterhost == nullptr)\n                    continue;\n                val = val.catfmt(\"%s:%I:%s:%i:%i;\", msi.master_replid,\n                    msi.master_initial_offset,\n                    msi.masterhost.get(),\n                    msi.masterport,\n                    msi.selected_db);\n            }\n            if (rdbSaveAuxFieldStrStr(rdb, \"repl-masters\",val.get()) == -1) return -1;\n        }\n    }\n    if (rdbSaveAuxFieldStrInt(rdb,\"aof-preamble\",aof_preamble) == -1) return -1;\n    return 1;\n}\n\nint saveKey(rio *rdb, int flags, size_t *processed, const char *keystr, robj_roptr o)\n{    \n    redisObjectStack key;\n\n    initStaticStringObject(key,(char*)keystr);\n    const expireEntry *pexpire = nullptr;\n    if (o->FExpires()) {\n        pexpire = &o->expire;\n    }\n\n    if (rdbSaveKeyValuePair(rdb,&key,o,pexpire) == -1)\n        return 0;\n\n    /* When this RDB is produced as part of an AOF rewrite, move\n        * accumulated diff from parent to child while rewriting in\n        * order to have a smaller final write. */\n    if (flags & RDBFLAGS_AOF_PREAMBLE &&\n        rdb->processed_bytes > *processed+AOF_READ_DIFF_INTERVAL_BYTES)\n    {\n        *processed = rdb->processed_bytes;\n        aofReadDiffFromParent();\n    }\n    return 1;\n}\n\nssize_t rdbSaveSingleModuleAux(rio *rdb, int when, moduleType *mt) {\n    /* Save a module-specific aux value. */\n    RedisModuleIO io;\n    int retval = rdbSaveType(rdb, RDB_OPCODE_MODULE_AUX);\n    if (retval == -1) return -1;\n    moduleInitIOContext(io,mt,rdb,NULL);\n    io.bytes += retval;\n\n    /* Write the \"module\" identifier as prefix, so that we'll be able\n     * to call the right module during loading. */\n    retval = rdbSaveLen(rdb,mt->id);\n    if (retval == -1) return -1;\n    io.bytes += retval;\n\n    /* write the 'when' so that we can provide it on loading. add a UINT opcode\n     * for backwards compatibility, everything after the MT needs to be prefixed\n     * by an opcode. */\n    retval = rdbSaveLen(rdb,RDB_MODULE_OPCODE_UINT);\n    if (retval == -1) return -1;\n    io.bytes += retval;\n    retval = rdbSaveLen(rdb,when);\n    if (retval == -1) return -1;\n    io.bytes += retval;\n\n    /* Then write the module-specific representation + EOF marker. */\n    mt->aux_save(&io,when);\n    retval = rdbSaveLen(rdb,RDB_MODULE_OPCODE_EOF);\n    if (retval == -1)\n        io.error = 1;\n    else\n        io.bytes += retval;\n\n    if (io.ctx) {\n        moduleFreeContext(io.ctx);\n        zfree(io.ctx);\n    }\n    if (io.error)\n        return -1;\n    return io.bytes;\n}\n\n/* Produces a dump of the database in RDB format sending it to the specified\n * Redis I/O channel. On success C_OK is returned, otherwise C_ERR\n * is returned and part of the output, or all the output, can be\n * missing because of I/O errors.\n *\n * When the function returns C_ERR and if 'error' is not NULL, the\n * integer pointed by 'error' is set to the value of errno just after the I/O\n * error. */\nint rdbSaveRio(rio *rdb, const redisDbPersistentDataSnapshot **rgpdb, int *error, int rdbflags, rdbSaveInfo *rsi) {\n    dictEntry *de;\n    dictIterator *di = NULL;\n    char magic[10];\n    uint64_t cksum;\n    size_t processed = 0;\n    int j;\n    long key_count = 0;\n    long long info_updated_time = 0;\n    const char *pname = (rdbflags & RDBFLAGS_AOF_PREAMBLE) ? \"AOF rewrite\" :  \"RDB\";\n\n    if (g_pserver->rdb_checksum)\n        rdb->update_cksum = rioGenericUpdateChecksum;\n    snprintf(magic,sizeof(magic),\"REDIS%04d\",RDB_VERSION);\n    if (rdbWriteRaw(rdb,magic,9) == -1) goto werr;\n    if (rdbSaveInfoAuxFields(rdb,rdbflags,rsi) == -1) goto werr;\n    if (rdbSaveModulesAux(rdb, REDISMODULE_AUX_BEFORE_RDB) == -1) goto werr;\n\n    for (j = 0; j < cserver.dbnum; j++) {\n        const redisDbPersistentDataSnapshot *db = rgpdb != nullptr ? rgpdb[j] : g_pserver->db[j];\n        if (db->size() == 0) continue;\n\n        /* Write the SELECT DB opcode */\n        if (rdbSaveType(rdb,RDB_OPCODE_SELECTDB) == -1) goto werr;\n        if (rdbSaveLen(rdb,j) == -1) goto werr;\n\n        /* Write the RESIZE DB opcode. */\n        uint64_t db_size, expires_size;\n        db_size = db->size();\n        expires_size = db->expireSize();\n        if (rdbSaveType(rdb,RDB_OPCODE_RESIZEDB) == -1) goto werr;\n        if (rdbSaveLen(rdb,db_size) == -1) goto werr;\n        if (rdbSaveLen(rdb,expires_size) == -1) goto werr;\n        \n        /* Iterate this DB writing every entry */\n        size_t ckeysExpired = 0;\n        bool fSavedAll = db->iterate_threadsafe([&](const char *keystr, robj_roptr o)->bool {\n            if (o->FExpires())\n                ++ckeysExpired;\n            \n            if (!saveKey(rdb, rdbflags, &processed, keystr, o))\n                return false;\n\n            /* Update child info every 1 second (approximately).\n             * in order to avoid calling mstime() on each iteration, we will\n             * check the diff every 1024 keys */\n            if ((key_count++ & 1023) == 0) {\n                long long now = mstime();\n                if (now - info_updated_time >= 1000) {\n                    sendChildInfo(CHILD_INFO_TYPE_CURRENT_INFO, key_count, pname);\n                    info_updated_time = now;\n                }\n            }\n\n            return !g_pserver->rdbThreadVars.fRdbThreadCancel;\n        });\n        if (!fSavedAll)\n            goto werr;\n        serverAssert(ckeysExpired == db->expireSize());\n    }\n\n    /* If we are storing the replication information on disk, persist\n     * the script cache as well: on successful PSYNC after a restart, we need\n     * to be able to process any EVALSHA inside the replication backlog the\n     * master will send us. */\n    {\n    AeLocker lock;\n    lock.arm(nullptr);\n    if (rsi && dictSize(g_pserver->lua_scripts)) {\n        di = dictGetIterator(g_pserver->lua_scripts);\n        while((de = dictNext(di)) != NULL) {\n            robj *body = (robj*)dictGetVal(de);\n            if (rdbSaveAuxField(rdb,\"lua\",3,szFromObj(body),sdslen(szFromObj(body))) == -1)\n                goto werr;\n            if (g_pserver->rdbThreadVars.fRdbThreadCancel)\n                goto werr;\n        }\n        dictReleaseIterator(di);\n        di = NULL; /* So that we don't release it again on error. */\n    }\n    }   // AeLocker end scope\n\n    if (rdbSaveModulesAux(rdb, REDISMODULE_AUX_AFTER_RDB) == -1) goto werr;\n\n    /* EOF opcode */\n    if (rdbSaveType(rdb,RDB_OPCODE_EOF) == -1) goto werr;\n\n    /* CRC64 checksum. It will be zero if checksum computation is disabled, the\n     * loading code skips the check in this case. */\n    cksum = rdb->cksum;\n    memrev64ifbe(&cksum);\n    if (rioWrite(rdb,&cksum,8) == 0) goto werr;\n    return C_OK;\n\nwerr:\n    if (error) *error = errno;\n    if (di) dictReleaseIterator(di);\n    return C_ERR;\n}\n\n/* This is just a wrapper to rdbSaveRio() that additionally adds a prefix\n * and a suffix to the generated RDB dump. The prefix is:\n *\n * $EOF:<40 bytes unguessable hex string>\\r\\n\n *\n * While the suffix is the 40 bytes hex string we announced in the prefix.\n * This way processes receiving the payload can understand when it ends\n * without doing any processing of the content. */\nint rdbSaveRioWithEOFMark(rio *rdb, const redisDbPersistentDataSnapshot **rgpdb, int *error, rdbSaveInfo *rsi) {\n    char eofmark[RDB_EOF_MARK_SIZE];\n\n    startSaving(RDBFLAGS_REPLICATION);\n    getRandomHexChars(eofmark,RDB_EOF_MARK_SIZE);\n    if (error) *error = 0;\n    if (rioWrite(rdb,\"$EOF:\",5) == 0) goto werr;\n    if (rioWrite(rdb,eofmark,RDB_EOF_MARK_SIZE) == 0) goto werr;\n    if (rioWrite(rdb,\"\\r\\n\",2) == 0) goto werr;\n    if (rdbSaveRio(rdb,rgpdb,error,RDBFLAGS_NONE,rsi) == C_ERR) goto werr;\n    if (rioWrite(rdb,eofmark,RDB_EOF_MARK_SIZE) == 0) goto werr;\n    stopSaving(1);\n    return C_OK;\n\nwerr: /* Write error. */\n    /* Set 'error' only if not already set by rdbSaveRio() call. */\n    if (error && *error == 0) *error = errno;\n    stopSaving(0);\n    return C_ERR;\n}\n\nint rdbSaveFp(FILE *fp, const redisDbPersistentDataSnapshot **rgpdb, rdbSaveInfo *rsi)\n{\n    int error = 0;\n    rio rdb;\n\n    rioInitWithFile(&rdb,fp);\n\n    if (g_pserver->rdb_save_incremental_fsync)\n        rioSetAutoSync(&rdb,REDIS_AUTOSYNC_BYTES);\n\n    if (rdbSaveRio(&rdb,rgpdb,&error,RDBFLAGS_NONE,rsi) == C_ERR) {\n        errno = error;\n        return C_ERR;\n    }\n    return C_OK;\n}\n\nint rdbSave(const redisDbPersistentDataSnapshot **rgpdb, rdbSaveInfo *rsi)\n{\n    std::vector<const redisDbPersistentDataSnapshot*> vecdb;\n    if (rgpdb == nullptr)\n    {\n        for (int idb = 0; idb < cserver.dbnum; ++idb)\n        {\n            vecdb.push_back(g_pserver->db[idb]);\n        }\n        rgpdb = vecdb.data();\n    }\n\n    int err = C_OK;\n    if (g_pserver->rdb_filename != NULL)\n        err = rdbSaveFile(g_pserver->rdb_filename, rgpdb, rsi);\n\n    if (err == C_OK && g_pserver->rdb_s3bucketpath != NULL)\n        err = rdbSaveS3(g_pserver->rdb_s3bucketpath, rgpdb, rsi);\n    return err;\n}\n\n/* Save the DB on disk. Return C_ERR on error, C_OK on success. */\nint rdbSaveFile(char *filename, const redisDbPersistentDataSnapshot **rgpdb, rdbSaveInfo *rsi) {\n    char tmpfile[256];\n    char cwd[MAXPATHLEN]; /* Current working dir path for error messages. */\n    FILE *fp = NULL;\n    rio rdb;\n    int error = 0;\n\n    getTempFileName(tmpfile, g_pserver->rdbThreadVars.tmpfileNum);\n    fp = fopen(tmpfile,\"w\");\n    if (!fp) {\n        char *cwdp = getcwd(cwd,MAXPATHLEN);\n        serverLog(LL_WARNING,\n            \"Failed opening the RDB file %s (in server root dir %s) \"\n            \"for saving: %s\",\n            filename,\n            cwdp ? cwdp : \"unknown\",\n            strerror(errno));\n        return C_ERR;\n    }\n\n    rioInitWithFile(&rdb,fp);\n    startSaving(RDBFLAGS_NONE);\n\n    if (g_pserver->rdb_save_incremental_fsync)\n        rioSetAutoSync(&rdb,REDIS_AUTOSYNC_BYTES);\n\n    if (rdbSaveRio(&rdb,rgpdb,&error,RDBFLAGS_NONE,rsi) == C_ERR) {\n        errno = error;\n        goto werr;\n    }\n\n    /* Make sure data will not remain on the OS's output buffers */\n    if (fflush(fp)) goto werr;\n    if (fsync(fileno(fp))) goto werr;\n    if (fclose(fp)) { fp = NULL; goto werr; }\n    fp = NULL;\n    \n    /* Use RENAME to make sure the DB file is changed atomically only\n     * if the generate DB file is ok. */\n    if (rename(tmpfile,filename) == -1) {\n        char *cwdp = getcwd(cwd,MAXPATHLEN);\n        serverLog(LL_WARNING,\n            \"Error moving temp DB file %s on the final \"\n            \"destination %s (in server root dir %s): %s\",\n            tmpfile,\n            filename,\n            cwdp ? cwdp : \"unknown\",\n            strerror(errno));\n        unlink(tmpfile);\n        stopSaving(0);\n        return C_ERR;\n    }\n\n    serverLog(LL_NOTICE,\"DB saved on disk\");\n    if (!g_pserver->rdbThreadVars.fRdbThreadActive)\n    {\n        // Do this only in a synchronous save, otherwise our thread controller will update these\n        g_pserver->dirty = 0;\n        g_pserver->lastsave = time(NULL);\n        g_pserver->lastbgsave_status = C_OK;\n    }\n    stopSaving(1);\n    return C_OK;\n\nwerr:\n    if (g_pserver->rdbThreadVars.fRdbThreadCancel)\n        serverLog(LL_WARNING, \"Background save cancelled\");\n    else\n        serverLog(LL_WARNING,\"Write error saving DB on disk: %s\", strerror(errno));\n    if (fp) fclose(fp);\n    unlink(tmpfile);\n    stopSaving(0);\n    return C_ERR;\n}\n\nstruct rdbSaveThreadArgs\n{\n    rdbSaveInfo rsi;\n    const redisDbPersistentDataSnapshot *rgpdb[1];    // NOTE: Variable Length\n};\n\nvoid *rdbSaveThread(void *vargs)\n{\n    aeThreadOnline();\n    serverAssert(!g_pserver->rdbThreadVars.fDone);\n    rdbSaveThreadArgs *args = reinterpret_cast<rdbSaveThreadArgs*>(vargs);\n    serverAssert(serverTL == nullptr);\n    redisServerThreadVars vars;\n    serverTL = &vars;\n    vars.gcEpoch = g_pserver->garbageCollector.startEpoch();\n\n    int retval = rdbSave(args->rgpdb, &args->rsi);    \n    if (retval == C_OK)\n        sendChildCowInfo(CHILD_INFO_TYPE_RDB_COW_SIZE, \"RDB\");\n\n    // If we were told to cancel the requesting thread holds the lock for us\n    ssize_t cbStart = zmalloc_used_memory();\n    for (int idb = 0; idb < cserver.dbnum; ++idb)\n        g_pserver->db[idb]->endSnapshotAsync(args->rgpdb[idb]);\n\n    args->~rdbSaveThreadArgs();\n    zfree(args);\n    ssize_t cbDiff = (cbStart - (ssize_t)zmalloc_used_memory());\n    g_pserver->garbageCollector.endEpoch(vars.gcEpoch);\n\n    if (cbDiff > 0)\n    {\n        serverLog(LL_NOTICE,\n                \"%s: %zd MB of memory used by copy-on-write\",\n                \"RDB\",cbDiff/(1024*1024));\n    }\n    aeThreadOffline();\n    g_pserver->rdbThreadVars.fDone = true;\n    return (retval == C_OK) ? (void*)0 : (void*)1;\n}\n\nint rdbSaveBackgroundFork(rdbSaveInfo *rsi) {\n    pid_t childpid;\n\n    if (hasActiveChildProcess() || g_pserver->rdb_child_pid != -1) return C_ERR;\n    serverAssert(g_pserver->rdb_child_pid != 10000);\n\n    g_pserver->dirty_before_bgsave = g_pserver->dirty;\n    g_pserver->lastbgsave_try = time(NULL);\n\n    if ((childpid = redisFork(CHILD_TYPE_RDB)) == 0) {\n        int retval;\n\n        /* Child */\n        g_pserver->rdb_child_pid = 10000;\n        redisSetProcTitle(\"keydb-rdb-bgsave\");\n        redisSetCpuAffinity(g_pserver->bgsave_cpulist);\n        retval = rdbSave(nullptr, rsi);\n        if (retval == C_OK) {\n            sendChildCowInfo(CHILD_INFO_TYPE_RDB_COW_SIZE, \"RDB\");\n        }\n        exitFromChild((retval == C_OK) ? 0 : 1);\n    } else {\n        /* Parent */\n        if (childpid == -1) {\n            g_pserver->lastbgsave_status = C_ERR;\n            serverLog(LL_WARNING,\"Can't save in background: fork: %s\",\n                strerror(errno));\n            return C_ERR;\n        }\n        serverLog(LL_NOTICE,\"Background saving started by pid %d\",childpid);\n        g_pserver->rdb_save_time_start = time(NULL);\n        g_pserver->rdb_child_type = RDB_CHILD_TYPE_DISK;\n        updateDictResizePolicy();\n        return C_OK;\n    }\n    return C_OK; /* unreached */\n}\n\nint launchRdbSaveThread(pthread_t &child, rdbSaveInfo *rsi)\n{\n    if (cserver.fForkBgSave) {\n        return rdbSaveBackgroundFork(rsi);\n    } else\n    {\n        rdbSaveThreadArgs *args = (rdbSaveThreadArgs*)zcalloc(sizeof(rdbSaveThreadArgs) + ((cserver.dbnum-1)*sizeof(redisDbPersistentDataSnapshot*)), MALLOC_LOCAL);\n        args = new (args) rdbSaveThreadArgs();\n        rdbSaveInfo rsiT;\n        if (rsi == nullptr)\n            rsi = &rsiT;\n        args->rsi = *rsi;\n        memcpy(&args->rsi.repl_id, g_pserver->replid, sizeof(g_pserver->replid));\n        args->rsi.master_repl_offset = g_pserver->master_repl_offset;\n            \n        for (int idb = 0; idb < cserver.dbnum; ++idb)\n            args->rgpdb[idb] = g_pserver->db[idb]->createSnapshot(getMvccTstamp(), false /* fOptional */);\n\n        g_pserver->rdbThreadVars.tmpfileNum++;\n        g_pserver->rdbThreadVars.fRdbThreadCancel = false;\n        pthread_attr_t tattr;\n        pthread_attr_init(&tattr);\n        pthread_attr_setstacksize(&tattr, 1 << 23); // 8 MB\n        openChildInfoPipe();\n        if (pthread_create(&child, &tattr, rdbSaveThread, args)) {\n            pthread_attr_destroy(&tattr);\n            for (int idb = 0; idb < cserver.dbnum; ++idb)\n                g_pserver->db[idb]->endSnapshot(args->rgpdb[idb]);\n            args->~rdbSaveThreadArgs();\n            zfree(args);\n            closeChildInfoPipe();\n            return C_ERR;\n        }\n        pthread_attr_destroy(&tattr);\n        g_pserver->child_type = CHILD_TYPE_RDB;\n    }\n    return C_OK;\n}\n\n\nint rdbSaveBackground(rdbSaveInfo *rsi) {\n    pthread_t child;\n    long long start;\n\n    if (hasActiveChildProcessOrBGSave()) return C_ERR;\n\n    g_pserver->dirty_before_bgsave = g_pserver->dirty;\n    g_pserver->lastbgsave_try = time(NULL);\n\n    start = ustime();\n    latencyStartMonitor(g_pserver->rdb_save_latency);\n\n    if (launchRdbSaveThread(child, rsi) != C_OK) {\n        g_pserver->lastbgsave_status = C_ERR;\n        serverLog(LL_WARNING,\"Can't save in background: fork: %s\",\n            strerror(errno));\n        return C_ERR;\n    }\n\n    g_pserver->stat_fork_time = ustime()-start;\n    g_pserver->stat_fork_rate = (double) zmalloc_used_memory() * 1000000 / g_pserver->stat_fork_time / (1024*1024*1024); /* GB per second. */\n    latencyAddSampleIfNeeded(\"fork\",g_pserver->stat_fork_time/1000);\n    serverLog(LL_NOTICE,\"Background saving started\");\n    g_pserver->rdb_save_time_start = time(NULL);\n    serverAssert(!g_pserver->rdbThreadVars.fRdbThreadActive);\n    g_pserver->rdbThreadVars.fRdbThreadActive = true;\n    g_pserver->rdbThreadVars.rdb_child_thread = child;\n    g_pserver->rdb_child_type = RDB_CHILD_TYPE_DISK;\n    updateDictResizePolicy();\n\n    return C_OK;\n}\n\nvoid getTempFileName(char tmpfile[], int tmpfileNum) {\n    char pid[32];\n    char tmpfileNumString[214];\n\n    /* Generate temp rdb file name using aync-signal safe functions. */\n    int pid_len = ll2string(pid, sizeof(pid), g_pserver->in_fork_child ? getppid() : getpid());\n    int tmpfileNum_len = ll2string(tmpfileNumString, sizeof(tmpfileNumString), tmpfileNum);\n    strcpy(tmpfile, \"temp-\");\n    strncpy(tmpfile+5, pid, pid_len);\n    strcpy(tmpfile+5+pid_len, \"-\");\n    strncpy(tmpfile+5+pid_len+1, tmpfileNumString, tmpfileNum_len);\n    strcpy(tmpfile+5+pid_len+1+tmpfileNum_len, \".rdb\");\n}\n\n/* Note that we may call this function in signal handle 'sigShutdownHandler',\n * so we need guarantee all functions we call are async-signal-safe.\n * If  we call this function from signal handle, we won't call bg_unlik that\n * is not async-signal-safe. */\nvoid rdbRemoveTempFile(int tmpfileNum, int from_signal) {\n    char tmpfile[256];\n    \n    getTempFileName(tmpfile, tmpfileNum);\n\n    if (from_signal) {\n        /* bg_unlink is not async-signal-safe, but in this case we don't really\n         * need to close the fd, it'll be released when the process exists. */\n        int fd = open(tmpfile, O_RDONLY|O_NONBLOCK);\n        UNUSED(fd);\n        unlink(tmpfile);\n    } else {\n        bg_unlink(tmpfile);\n    }\n}\n\n/* This function is called by rdbLoadObject() when the code is in RDB-check\n * mode and we find a module value of type 2 that can be parsed without\n * the need of the actual module. The value is parsed for errors, finally\n * a dummy redis object is returned just to conform to the API. */\nrobj *rdbLoadCheckModuleValue(rio *rdb, char *modulename) {\n    uint64_t opcode;\n    while((opcode = rdbLoadLen(rdb,NULL)) != RDB_MODULE_OPCODE_EOF) {\n        if (opcode == RDB_MODULE_OPCODE_SINT ||\n            opcode == RDB_MODULE_OPCODE_UINT)\n        {\n            uint64_t len;\n            if (rdbLoadLenByRef(rdb,NULL,&len) == -1) {\n                rdbReportCorruptRDB(\n                    \"Error reading integer from module %s value\", modulename);\n            }\n        } else if (opcode == RDB_MODULE_OPCODE_STRING) {\n            robj *o = (robj*)rdbGenericLoadStringObject(rdb,RDB_LOAD_NONE,NULL);\n            if (o == NULL) {\n                rdbReportCorruptRDB(\n                    \"Error reading string from module %s value\", modulename);\n            }\n            decrRefCount(o);\n        } else if (opcode == RDB_MODULE_OPCODE_FLOAT) {\n            float val;\n            if (rdbLoadBinaryFloatValue(rdb,&val) == -1) {\n                rdbReportCorruptRDB(\n                    \"Error reading float from module %s value\", modulename);\n            }\n        } else if (opcode == RDB_MODULE_OPCODE_DOUBLE) {\n            double val;\n            if (rdbLoadBinaryDoubleValue(rdb,&val) == -1) {\n                rdbReportCorruptRDB(\n                    \"Error reading double from module %s value\", modulename);\n            }\n        }\n    }\n    return createStringObject(\"module-dummy-value\",18);\n}\n\n/* Load a Redis object of the specified type from the specified file.\n * On success a newly allocated object is returned, otherwise NULL.\n * When the function returns NULL and if 'error' is not NULL, the\n * integer pointed by 'error' is set to the type of error that occurred */\nrobj *rdbLoadObject(int rdbtype, rio *rdb, sds key, int *error, uint64_t mvcc_tstamp) {\n    robj *o = NULL, *ele, *dec;\n    uint64_t len;\n    unsigned int i;\n\n    /* Set default error of load object, it will be set to 0 on success. */\n    if (error) *error = RDB_LOAD_ERR_OTHER;\n\n    int deep_integrity_validation = cserver.sanitize_dump_payload == SANITIZE_DUMP_YES;\n    if (cserver.sanitize_dump_payload == SANITIZE_DUMP_CLIENTS) {\n        /* Skip sanitization when loading (an RDB), or getting a RESTORE command\n         * from either the master or a client using an ACL user with the skip-sanitize-payload flag. */\n        int skip = g_pserver->loading ||\n            (serverTL->current_client && (serverTL->current_client->flags & CLIENT_MASTER));\n        if (!skip && serverTL->current_client && serverTL->current_client->user)\n            skip = !!(serverTL->current_client->user->flags & USER_FLAG_SANITIZE_PAYLOAD_SKIP);\n        deep_integrity_validation = !skip;\n    }\n\n    if (rdbtype == RDB_TYPE_STRING) {\n        /* Read string value */\n        if ((o = rdbLoadEncodedStringObject(rdb)) == NULL) return NULL;\n        o = tryObjectEncoding(o);\n    } else if (rdbtype == RDB_TYPE_LIST) {\n        /* Read list value */\n        if ((len = rdbLoadLen(rdb,NULL)) == RDB_LENERR) return NULL;\n        if (len == 0) goto emptykey;\n\n        o = createQuicklistObject();\n        quicklistSetOptions((quicklist*)ptrFromObj(o), g_pserver->list_max_ziplist_size,\n                            g_pserver->list_compress_depth);\n\n        /* Load every single element of the list */\n        while(len--) {\n            if ((ele = rdbLoadEncodedStringObject(rdb)) == NULL) {\n                decrRefCount(o);\n                return NULL;\n            }\n            dec = getDecodedObject(ele);\n            size_t len = sdslen(szFromObj(dec));\n            quicklistPushTail((quicklist*)ptrFromObj(o), ptrFromObj(dec), len);\n            decrRefCount(dec);\n            decrRefCount(ele);\n        }\n    } else if (rdbtype == RDB_TYPE_SET) {\n        /* Read Set value */\n        if ((len = rdbLoadLen(rdb,NULL)) == RDB_LENERR) return NULL;\n        if (len == 0) goto emptykey;\n\n        /* Use a regular set when there are too many entries. */\n        size_t max_entries = g_pserver->set_max_intset_entries;\n        if (max_entries >= 1<<30) max_entries = 1<<30;\n        if (len > max_entries) {\n            o = createSetObject();\n            /* It's faster to expand the dict to the right size asap in order\n             * to avoid rehashing */\n            if (len > DICT_HT_INITIAL_SIZE && dictTryExpand((dict*)ptrFromObj(o),len,false) != DICT_OK) {\n                rdbReportCorruptRDB(\"OOM in dictTryExpand %llu\", (unsigned long long)len);\n                decrRefCount(o);\n                return NULL;\n            }\n        } else {\n            o = createIntsetObject();\n        }\n\n        /* Load every single element of the set */\n        for (i = 0; i < len; i++) {\n            long long llval;\n            sds sdsele;\n\n            if ((sdsele = (sds)rdbGenericLoadStringObject(rdb,RDB_LOAD_SDS,NULL)) == NULL) {\n                decrRefCount(o);\n                return NULL;\n            }\n\n            if (o->encoding == OBJ_ENCODING_INTSET) {\n                /* Fetch integer value from element. */\n                if (isSdsRepresentableAsLongLong(sdsele,&llval) == C_OK) {\n                    uint8_t success;\n                    o->m_ptr = intsetAdd((intset*)ptrFromObj(o),llval,&success);\n                    if (!success) {\n                        rdbReportCorruptRDB(\"Duplicate set members detected\");\n                        decrRefCount(o);\n                        sdsfree(sdsele);\n                        return NULL;\n                    }\n                } else {\n                    setTypeConvert(o,OBJ_ENCODING_HT);\n                    if (dictTryExpand((dict*)ptrFromObj(o),len,false) != DICT_OK) {\n                        rdbReportCorruptRDB(\"OOM in dictTryExpand %llu\", (unsigned long long)len);\n                        sdsfree(sdsele);\n                        decrRefCount(o);\n                        return NULL;\n                    }\n                }\n            }\n\n            /* This will also be called when the set was just converted\n             * to a regular hash table encoded set. */\n            if (o->encoding == OBJ_ENCODING_HT) {\n                if (dictAdd((dict*)ptrFromObj(o),sdsele,NULL) != DICT_OK) {\n                    rdbReportCorruptRDB(\"Duplicate set members detected\");\n                    decrRefCount(o);\n                    sdsfree(sdsele);\n                    return NULL;\n                }\n            } else {\n                sdsfree(sdsele);\n            }\n        }\n    } else if (rdbtype == RDB_TYPE_ZSET_2 || rdbtype == RDB_TYPE_ZSET) {\n        /* Read list/set value. */\n        uint64_t zsetlen;\n        size_t maxelelen = 0, totelelen = 0;\n        zset *zs;\n\n        if ((zsetlen = rdbLoadLen(rdb,NULL)) == RDB_LENERR) return NULL;\n        if (zsetlen == 0) goto emptykey;\n\n        o = createZsetObject();\n        zs = (zset*)ptrFromObj(o);\n\n        if (zsetlen > DICT_HT_INITIAL_SIZE && dictTryExpand(zs->dict,zsetlen,false) != DICT_OK) {\n            rdbReportCorruptRDB(\"OOM in dictTryExpand %llu\", (unsigned long long)zsetlen);\n            decrRefCount(o);\n            return NULL;\n        }\n\n        /* Load every single element of the sorted set. */\n        while(zsetlen--) {\n            sds sdsele;\n            double score;\n            zskiplistNode *znode;\n\n            if ((sdsele = (sds)rdbGenericLoadStringObject(rdb,RDB_LOAD_SDS,NULL)) == NULL) {\n                decrRefCount(o);\n                return NULL;\n            }\n\n            if (rdbtype == RDB_TYPE_ZSET_2) {\n                if (rdbLoadBinaryDoubleValue(rdb,&score) == -1) {\n                    decrRefCount(o);\n                    sdsfree(sdsele);\n                    return NULL;\n                }\n            } else {\n                if (rdbLoadDoubleValue(rdb,&score) == -1) {\n                    decrRefCount(o);\n                    sdsfree(sdsele);\n                    return NULL;\n                }\n            }\n\n            /* Don't care about integer-encoded strings. */\n            if (sdslen(sdsele) > maxelelen) maxelelen = sdslen(sdsele);\n            totelelen += sdslen(sdsele);\n\n            znode = zslInsert(zs->zsl,score,sdsele);\n            if (dictAdd(zs->dict,sdsele,&znode->score) != DICT_OK) {\n                rdbReportCorruptRDB(\"Duplicate zset fields detected\");\n                decrRefCount(o);\n                /* no need to free 'sdsele', will be released by zslFree together with 'o' */\n                return NULL;\n            }\n        }\n\n        /* Convert *after* loading, since sorted sets are not stored ordered. */\n        if (zsetLength(o) <= g_pserver->zset_max_ziplist_entries &&\n            maxelelen <= g_pserver->zset_max_ziplist_value &&\n            ziplistSafeToAdd(NULL, totelelen))\n                zsetConvert(o,OBJ_ENCODING_ZIPLIST);\n\n    } else if (rdbtype == RDB_TYPE_HASH) {\n        uint64_t len;\n        int ret;\n        sds field, value;\n        dict *dupSearchDict = NULL;\n\n        len = rdbLoadLen(rdb, NULL);\n        if (len == RDB_LENERR) return NULL;\n        if (len == 0) goto emptykey;\n\n        o = createHashObject();\n\n        /* Too many entries? Use a hash table. */\n        if (len > g_pserver->hash_max_ziplist_entries)\n            hashTypeConvert(o, OBJ_ENCODING_HT);\n        else if (deep_integrity_validation) {\n            /* In this mode, we need to guarantee that the server won't crash\n             * later when the ziplist is converted to a dict.\n             * Create a set (dict with no values) to for a dup search.\n             * We can dismiss it as soon as we convert the ziplist to a hash. */\n            dupSearchDict = dictCreate(&hashDictType, NULL);\n        }\n\n\n        /* Load every field and value into the ziplist */\n        while (o->encoding == OBJ_ENCODING_ZIPLIST && len > 0) {\n            len--;\n            /* Load raw strings */\n            if ((field = (sds)rdbGenericLoadStringObject(rdb,RDB_LOAD_SDS,NULL)) == NULL) {\n                decrRefCount(o);\n                if (dupSearchDict) dictRelease(dupSearchDict);\n                return NULL;\n            }\n            if ((value = (sds)rdbGenericLoadStringObject(rdb,RDB_LOAD_SDS,NULL)) == NULL) {\n                sdsfree(field);\n                decrRefCount(o);\n                if (dupSearchDict) dictRelease(dupSearchDict);\n                return NULL;\n            }\n\n            if (dupSearchDict) {\n                sds field_dup = sdsdup(field);\n                if (dictAdd(dupSearchDict, field_dup, NULL) != DICT_OK) {\n                    rdbReportCorruptRDB(\"Hash with dup elements\");\n                    dictRelease(dupSearchDict);\n                    decrRefCount(o);\n                    sdsfree(field_dup);\n                    sdsfree(field);\n                    sdsfree(value);\n                    return NULL;\n                }\n            }\n\n            /* Convert to hash table if size threshold is exceeded */\n            if (sdslen(field) > g_pserver->hash_max_ziplist_value ||\n                sdslen(value) > g_pserver->hash_max_ziplist_value || \n                !ziplistSafeToAdd((unsigned char*)ptrFromObj(o), sdslen(field)+sdslen(value)))\n            {\n                hashTypeConvert(o, OBJ_ENCODING_HT);\n                ret = dictAdd((dict*)ptrFromObj(o), field, value);\n                if (ret == DICT_ERR) {\n                    rdbReportCorruptRDB(\"Duplicate hash fields detected\");\n                    if (dupSearchDict) dictRelease(dupSearchDict);\n                    sdsfree(value);\n                    sdsfree(field);\n                    decrRefCount(o);\n                    return NULL;\n                }\n                break;\n            }\n\n            /* Add pair to ziplist */\n            o->m_ptr = ziplistPush((unsigned char*)ptrFromObj(o), (unsigned char*)field,\n                    sdslen(field), ZIPLIST_TAIL);\n            o->m_ptr = ziplistPush((unsigned char*)ptrFromObj(o), (unsigned char*)value,\n                    sdslen(value), ZIPLIST_TAIL);\n\n\n            sdsfree(field);\n            sdsfree(value);\n        }\n\n        if (dupSearchDict) {\n            /* We no longer need this, from now on the entries are added\n             * to a dict so the check is performed implicitly. */\n            dictRelease(dupSearchDict);\n            dupSearchDict = NULL;\n        }\n\n        if (o->encoding == OBJ_ENCODING_HT && len > DICT_HT_INITIAL_SIZE) {\n            if (dictTryExpand((dict*)ptrFromObj(o),len,false) != DICT_OK) {\n                rdbReportCorruptRDB(\"OOM in dictTryExpand %llu\", (unsigned long long)len);\n                decrRefCount(o);\n                return NULL;\n            }\n        }\n\n        /* Load remaining fields and values into the hash table */\n        while (o->encoding == OBJ_ENCODING_HT && len > 0) {\n            len--;\n            /* Load encoded strings */\n            if ((field = (sds)rdbGenericLoadStringObject(rdb,RDB_LOAD_SDS,NULL)) == NULL) {\n                decrRefCount(o);\n                return NULL;\n            }\n            if ((value = (sds)rdbGenericLoadStringObject(rdb,RDB_LOAD_SDS,NULL)) == NULL) {\n                sdsfree(field);\n                decrRefCount(o);\n                return NULL;\n            }\n\n            /* Add pair to hash table */\n            ret = dictAdd((dict*)ptrFromObj(o), field, value);\n            if (ret == DICT_ERR) {\n                rdbReportCorruptRDB(\"Duplicate hash fields detected\");\n                sdsfree(value);\n                sdsfree(field);\n                decrRefCount(o);\n                return NULL;\n            }\n        }\n\n        /* All pairs should be read by now */\n        serverAssert(len == 0);\n    } else if (rdbtype == RDB_TYPE_LIST_QUICKLIST) {\n        if ((len = rdbLoadLen(rdb,NULL)) == RDB_LENERR) return NULL;\n        if (len == 0) goto emptykey;\n\n        o = createQuicklistObject();\n        quicklistSetOptions((quicklist*)ptrFromObj(o), g_pserver->list_max_ziplist_size,\n                            g_pserver->list_compress_depth);\n\n        while (len--) {\n            size_t encoded_len;\n            unsigned char *zl = (unsigned char*)\n                rdbGenericLoadStringObject(rdb,RDB_LOAD_PLAIN,&encoded_len);\n            if (zl == NULL) {\n                decrRefCount(o);\n                return NULL;\n            }\n            if (deep_integrity_validation) g_pserver->stat_dump_payload_sanitizations++;\n            if (!ziplistValidateIntegrity(zl, encoded_len, deep_integrity_validation, NULL, NULL)) {\n                rdbReportCorruptRDB(\"Ziplist integrity check failed.\");\n                decrRefCount(o);\n                zfree(zl);\n                return NULL;\n            }\n\n            /* Silently skip empty ziplists, if we'll end up with empty quicklist we'll fail later. */\n            if (ziplistLen(zl) == 0) {\n                zfree(zl);\n                continue;\n            } else {\n                quicklistAppendZiplist((quicklist*)ptrFromObj(o), zl);\n            }\n        }\n\n        if (quicklistCount((quicklist*)ptrFromObj(o)) == 0) {\n            decrRefCount(o);\n            goto emptykey;\n        }\n    } else if (rdbtype == RDB_TYPE_HASH_ZIPMAP  ||\n               rdbtype == RDB_TYPE_LIST_ZIPLIST ||\n               rdbtype == RDB_TYPE_SET_INTSET   ||\n               rdbtype == RDB_TYPE_ZSET_ZIPLIST ||\n               rdbtype == RDB_TYPE_HASH_ZIPLIST)\n    {\n        size_t encoded_len;\n        unsigned char *encoded = (unsigned char*)\n            rdbGenericLoadStringObject(rdb,RDB_LOAD_PLAIN,&encoded_len);\n        if (encoded == NULL) return NULL;\n\n        o = createObject(OBJ_STRING,encoded); /* Obj type fixed below. */\n\n        /* Fix the object encoding, and make sure to convert the encoded\n         * data type into the base type if accordingly to the current\n         * configuration there are too many elements in the encoded data\n         * type. Note that we only check the length and not max element\n         * size as this is an O(N) scan. Eventually everything will get\n         * converted. */\n        switch(rdbtype) {\n            case RDB_TYPE_HASH_ZIPMAP:\n                /* Since we don't keep zipmaps anymore, the rdb loading for these\n                 * is O(n) anyway, use `deep` validation. */\n                if (!zipmapValidateIntegrity(encoded, encoded_len, 1)) {\n                    rdbReportCorruptRDB(\"Zipmap integrity check failed.\");\n                    zfree(encoded);\n                    o->m_ptr = NULL;\n                    decrRefCount(o);\n                    return NULL;\n                }\n                /* Convert to ziplist encoded hash. This must be deprecated\n                 * when loading dumps created by Redis 2.4 gets deprecated. */\n                {\n                    unsigned char *zl = ziplistNew();\n                    unsigned char *zi = zipmapRewind((unsigned char*)ptrFromObj(o));\n                    unsigned char *fstr, *vstr;\n                    unsigned int flen, vlen;\n                    unsigned int maxlen = 0;\n                    dict *dupSearchDict = dictCreate(&hashDictType, NULL);\n\n                    while ((zi = zipmapNext(zi, &fstr, &flen, &vstr, &vlen)) != NULL) {\n                        if (flen > maxlen) maxlen = flen;\n                        if (vlen > maxlen) maxlen = vlen;\n\n                        /* search for duplicate records */\n                        sds field = sdstrynewlen(fstr, flen);\n                        if (!field || dictAdd(dupSearchDict, field, NULL) != DICT_OK ||\n                            !ziplistSafeToAdd(zl, (size_t)flen + vlen)) {\n                            rdbReportCorruptRDB(\"Hash zipmap with dup elements, or big length (%u)\", flen);\n                            dictRelease(dupSearchDict);\n                            sdsfree(field);\n                            zfree(encoded);\n                            o->m_ptr = NULL;\n                            decrRefCount(o);\n                            return NULL;\n                        }\n\n                        zl = ziplistPush(zl, fstr, flen, ZIPLIST_TAIL);\n                        zl = ziplistPush(zl, vstr, vlen, ZIPLIST_TAIL);\n                    }\n\n                    dictRelease(dupSearchDict);\n                    zfree(ptrFromObj(o));\n                    o->m_ptr = zl;\n                    o->type = OBJ_HASH;\n                    o->encoding = OBJ_ENCODING_ZIPLIST;\n\n                    if (hashTypeLength(o) > g_pserver->hash_max_ziplist_entries ||\n                        maxlen > g_pserver->hash_max_ziplist_value)\n                    {\n                        hashTypeConvert(o, OBJ_ENCODING_HT);\n                    }\n                }\n                break;\n            case RDB_TYPE_LIST_ZIPLIST:\n                if (deep_integrity_validation) g_pserver->stat_dump_payload_sanitizations++;\n                if (!ziplistValidateIntegrity(encoded, encoded_len, deep_integrity_validation, NULL, NULL)) {\n                    rdbReportCorruptRDB(\"List ziplist integrity check failed.\");\n                    zfree(encoded);\n                    o->m_ptr = NULL;\n                    decrRefCount(o);\n                    return NULL;\n                }\n\n                if (ziplistLen(encoded) == 0) {\n                    zfree(encoded);\n                    o->m_ptr = NULL;\n                    decrRefCount(o);\n                    goto emptykey;\n                }\n\n                o->type = OBJ_LIST;\n                o->encoding = OBJ_ENCODING_ZIPLIST;\n                listTypeConvert(o,OBJ_ENCODING_QUICKLIST);\n                break;\n            case RDB_TYPE_SET_INTSET:\n                if (deep_integrity_validation) g_pserver->stat_dump_payload_sanitizations++;\n                if (!intsetValidateIntegrity(encoded, encoded_len, deep_integrity_validation)) {\n                    rdbReportCorruptRDB(\"Intset integrity check failed.\");\n                    zfree(encoded);\n                    o->m_ptr = NULL;\n                    decrRefCount(o);\n                    return NULL;\n                }\n                o->type = OBJ_SET;\n                o->encoding = OBJ_ENCODING_INTSET;\n                if (intsetLen((intset*)ptrFromObj(o)) > g_pserver->set_max_intset_entries)\n                    setTypeConvert(o,OBJ_ENCODING_HT);\n                break;\n            case RDB_TYPE_ZSET_ZIPLIST:\n                if (deep_integrity_validation) g_pserver->stat_dump_payload_sanitizations++;\n                if (!zsetZiplistValidateIntegrity(encoded, encoded_len, deep_integrity_validation)) {\n                    rdbReportCorruptRDB(\"Zset ziplist integrity check failed.\");\n                    zfree(encoded);\n                    o->m_ptr = NULL;\n                    decrRefCount(o);\n                    return NULL;\n                }\n                o->type = OBJ_ZSET;\n                o->encoding = OBJ_ENCODING_ZIPLIST;\n                if (zsetLength(o) == 0) {\n                    zfree(encoded);\n                    o->m_ptr = NULL;\n                    decrRefCount(o);\n                    goto emptykey;\n                }\n\n                if (zsetLength(o) > g_pserver->zset_max_ziplist_entries)\n                    zsetConvert(o,OBJ_ENCODING_SKIPLIST);\n                break;\n            case RDB_TYPE_HASH_ZIPLIST:\n                if (deep_integrity_validation) g_pserver->stat_dump_payload_sanitizations++;\n                if (!hashZiplistValidateIntegrity(encoded, encoded_len, deep_integrity_validation)) {\n                    rdbReportCorruptRDB(\"Hash ziplist integrity check failed.\");\n                    zfree(encoded);\n                    o->m_ptr = NULL;\n                    decrRefCount(o);\n                    return NULL;\n                }\n                o->type = OBJ_HASH;\n                o->encoding = OBJ_ENCODING_ZIPLIST;\n                if (hashTypeLength(o) == 0) {\n                    zfree(encoded);\n                    o->m_ptr = NULL;\n                    decrRefCount(o);\n                    goto emptykey;\n                }\n\n                if (hashTypeLength(o) > g_pserver->hash_max_ziplist_entries)\n                    hashTypeConvert(o, OBJ_ENCODING_HT);\n                break;\n            default:\n                /* totally unreachable */\n                rdbReportCorruptRDB(\"Unknown RDB encoding type %d\",rdbtype);\n                break;\n        }\n    } else if (rdbtype == RDB_TYPE_STREAM_LISTPACKS) {\n        o = createStreamObject();\n        stream *s = (stream*)ptrFromObj(o);\n        uint64_t listpacks = rdbLoadLen(rdb,NULL);\n        if (listpacks == RDB_LENERR) {\n            rdbReportReadError(\"Stream listpacks len loading failed.\");\n            decrRefCount(o);\n            return NULL;\n        }\n\n        while(listpacks--) {\n            /* Get the master ID, the one we'll use as key of the radix tree\n             * node: the entries inside the listpack itself are delta-encoded\n             * relatively to this ID. */\n            sds nodekey = (sds)rdbGenericLoadStringObject(rdb,RDB_LOAD_SDS,NULL);\n            if (nodekey == NULL) {\n                rdbReportReadError(\"Stream master ID loading failed: invalid encoding or I/O error.\");\n                decrRefCount(o);\n                return NULL;\n            }\n            if (sdslen(nodekey) != sizeof(streamID)) {\n                rdbReportCorruptRDB(\"Stream node key entry is not the \"\n                                        \"size of a stream ID\");\n                sdsfree(nodekey);\n                decrRefCount(o);\n                return NULL;\n            }\n\n            /* Load the listpack. */\n            size_t lp_size;\n            unsigned char *lp = (unsigned char*)\n                rdbGenericLoadStringObject(rdb,RDB_LOAD_PLAIN,&lp_size);\n            if (lp == NULL) {\n                rdbReportReadError(\"Stream listpacks loading failed.\");\n                sdsfree(nodekey);\n                decrRefCount(o);\n                return NULL;\n            }\n            if (deep_integrity_validation) g_pserver->stat_dump_payload_sanitizations++;\n            if (!streamValidateListpackIntegrity(lp, lp_size, deep_integrity_validation)) {\n                rdbReportCorruptRDB(\"Stream listpack integrity check failed.\");\n                sdsfree(nodekey);\n                decrRefCount(o);\n                zfree(lp);\n                return NULL;\n            }\n\n            unsigned char *first = lpFirst(lp);\n            if (first == NULL) {\n                /* Serialized listpacks should never be empty, since on\n                 * deletion we should remove the radix tree key if the\n                 * resulting listpack is empty. */\n                rdbReportCorruptRDB(\"Empty listpack inside stream\");\n                sdsfree(nodekey);\n                decrRefCount(o);\n                zfree(lp);\n                return NULL;\n            }\n\n            /* Insert the key in the radix tree. */\n            int retval = raxTryInsert(s->rax,\n                (unsigned char*)nodekey,sizeof(streamID),lp,NULL);\n            sdsfree(nodekey);\n            if (!retval) {\n                rdbReportCorruptRDB(\"Listpack re-added with existing key\");\n                decrRefCount(o);\n                zfree(lp);\n                return NULL;\n            }\n        }\n        /* Load total number of items inside the stream. */\n        s->length = rdbLoadLen(rdb,NULL);\n\n        /* Load the last entry ID. */\n        s->last_id.ms = rdbLoadLen(rdb,NULL);\n        s->last_id.seq = rdbLoadLen(rdb,NULL);\n\n        if (rioGetReadError(rdb)) {\n            rdbReportReadError(\"Stream object metadata loading failed.\");\n            decrRefCount(o);\n            return NULL;\n        }\n\n        /* Consumer groups loading */\n        uint64_t cgroups_count = rdbLoadLen(rdb,NULL);\n        if (cgroups_count == RDB_LENERR) {\n            rdbReportReadError(\"Stream cgroup count loading failed.\");\n            decrRefCount(o);\n            return NULL;\n        }\n        while(cgroups_count--) {\n            /* Get the consumer group name and ID. We can then create the\n             * consumer group ASAP and populate its structure as\n             * we read more data. */\n            streamID cg_id;\n            sds cgname = (sds)rdbGenericLoadStringObject(rdb,RDB_LOAD_SDS,NULL);\n            if (cgname == NULL) {\n                rdbReportReadError(\n                    \"Error reading the consumer group name from Stream\");\n                decrRefCount(o);\n                return NULL;\n            }\n\n            cg_id.ms = rdbLoadLen(rdb,NULL);\n            cg_id.seq = rdbLoadLen(rdb,NULL);\n            if (rioGetReadError(rdb)) {\n                rdbReportReadError(\"Stream cgroup ID loading failed.\");\n                sdsfree(cgname);\n                decrRefCount(o);\n                return NULL;\n            }\n\n            streamCG *cgroup = streamCreateCG(s,cgname,sdslen(cgname),&cg_id);\n            if (cgroup == NULL) {\n                rdbReportCorruptRDB(\"Duplicated consumer group name %s\",\n                                         cgname);\n                decrRefCount(o);\n                sdsfree(cgname);\n                return NULL;\n            }\n            sdsfree(cgname);\n\n            /* Load the global PEL for this consumer group, however we'll\n             * not yet populate the NACK structures with the message\n             * owner, since consumers for this group and their messages will\n             * be read as a next step. So for now leave them not resolved\n             * and later populate it. */\n            uint64_t pel_size = rdbLoadLen(rdb,NULL);\n            if (pel_size == RDB_LENERR) {\n                rdbReportReadError(\"Stream PEL size loading failed.\");\n                decrRefCount(o);\n                return NULL;\n            }\n            while(pel_size--) {\n                unsigned char rawid[sizeof(streamID)];\n                if (rioRead(rdb,rawid,sizeof(rawid)) == 0) {\n                    rdbReportReadError(\"Stream PEL ID loading failed.\");\n                    decrRefCount(o);\n                    return NULL;\n                }\n                streamNACK *nack = streamCreateNACK(NULL);\n                nack->delivery_time = rdbLoadMillisecondTime(rdb,RDB_VERSION);\n                nack->delivery_count = rdbLoadLen(rdb,NULL);\n                if (rioGetReadError(rdb)) {\n                    rdbReportReadError(\"Stream PEL NACK loading failed.\");\n                    decrRefCount(o);\n                    streamFreeNACK(nack);\n                    return NULL;\n                }\n                if (!raxTryInsert(cgroup->pel,rawid,sizeof(rawid),nack,NULL)) {\n                    rdbReportCorruptRDB(\"Duplicated global PEL entry \"\n                                            \"loading stream consumer group\");\n                    decrRefCount(o);\n                    streamFreeNACK(nack);\n                    return NULL;\n                }\n            }\n\n            /* Now that we loaded our global PEL, we need to load the\n             * consumers and their local PELs. */\n            uint64_t consumers_num = rdbLoadLen(rdb,NULL);\n            if (consumers_num == RDB_LENERR) {\n                rdbReportReadError(\"Stream consumers num loading failed.\");\n                decrRefCount(o);\n                return NULL;\n            }\n            while(consumers_num--) {\n                sds cname = (sds)rdbGenericLoadStringObject(rdb,RDB_LOAD_SDS,NULL);\n                if (cname == NULL) {\n                    rdbReportReadError(\n                        \"Error reading the consumer name from Stream group.\");\n                    decrRefCount(o);\n                    return NULL;\n                }\n                streamConsumer *consumer =\n                    streamLookupConsumer(cgroup,cname,SLC_NONE,NULL);\n                sdsfree(cname);\n                consumer->seen_time = rdbLoadMillisecondTime(rdb,RDB_VERSION);\n                if (rioGetReadError(rdb)) {\n                    rdbReportReadError(\"Stream short read reading seen time.\");\n                    decrRefCount(o);\n                    return NULL;\n                }\n\n                /* Load the PEL about entries owned by this specific\n                 * consumer. */\n                pel_size = rdbLoadLen(rdb,NULL);\n                if (pel_size == RDB_LENERR) {\n                    rdbReportReadError(\n                        \"Stream consumer PEL num loading failed.\");\n                    decrRefCount(o);\n                    return NULL;\n                }\n                while(pel_size--) {\n                    unsigned char rawid[sizeof(streamID)];\n                    if (rioRead(rdb,rawid,sizeof(rawid)) == 0) {\n                        rdbReportReadError(\n                            \"Stream short read reading PEL streamID.\");\n                        decrRefCount(o);\n                        return NULL;\n                    }\n                    streamNACK *nack = (streamNACK*)raxFind(cgroup->pel,rawid,sizeof(rawid));\n                    if (nack == raxNotFound) {\n                        rdbReportCorruptRDB(\"Consumer entry not found in \"\n                                                \"group global PEL\");\n                        decrRefCount(o);\n                        return NULL;\n                    }\n\n                    /* Set the NACK consumer, that was left to NULL when\n                     * loading the global PEL. Then set the same shared\n                     * NACK structure also in the consumer-specific PEL. */\n                    nack->consumer = consumer;\n                    if (!raxTryInsert(consumer->pel,rawid,sizeof(rawid),nack,NULL)) {\n                        rdbReportCorruptRDB(\"Duplicated consumer PEL entry \"\n                                                \" loading a stream consumer \"\n                                                \"group\");\n                        decrRefCount(o);\n                        streamFreeNACK(nack);\n                        return NULL;\n                    }\n                }\n            }\n\n            /* Verify that each PEL eventually got a consumer assigned to it. */\n            if (deep_integrity_validation) {\n                raxIterator ri_cg_pel;\n                raxStart(&ri_cg_pel,cgroup->pel);\n                raxSeek(&ri_cg_pel,\"^\",NULL,0);\n                while(raxNext(&ri_cg_pel)) {\n                    streamNACK *nack = (streamNACK *)ri_cg_pel.data;\n                    if (!nack->consumer) {\n                        raxStop(&ri_cg_pel);\n                        rdbReportCorruptRDB(\"Stream CG PEL entry without consumer\");\n                        decrRefCount(o);\n                        return NULL;\n                    }\n                }\n                raxStop(&ri_cg_pel);\n            }\n        }\n    } else if (rdbtype == RDB_TYPE_MODULE || rdbtype == RDB_TYPE_MODULE_2) {\n        uint64_t moduleid = rdbLoadLen(rdb,NULL);\n        if (rioGetReadError(rdb)) {\n            rdbReportReadError(\"Short read module id\");\n            return NULL;\n        }\n        moduleType *mt = moduleTypeLookupModuleByID(moduleid);\n\n        if (rdbCheckMode && rdbtype == RDB_TYPE_MODULE_2) {\n            char name[10];\n            moduleTypeNameByID(name,moduleid);\n            return rdbLoadCheckModuleValue(rdb,name);\n        }\n\n        if (mt == NULL) {\n            char name[10];\n            moduleTypeNameByID(name,moduleid);\n            rdbReportCorruptRDB(\"The RDB file contains module data I can't load: no matching module type '%s'\", name);\n            return NULL;\n        }\n        RedisModuleIO io;\n        redisObjectStack keyobj;\n        initStaticStringObject(keyobj,key);\n        moduleInitIOContext(io,mt,rdb,&keyobj);\n        io.ver = (rdbtype == RDB_TYPE_MODULE) ? 1 : 2;\n        /* Call the rdb_load method of the module providing the 10 bit\n         * encoding version in the lower 10 bits of the module ID. */\n        void *ptr = mt->rdb_load(&io,moduleid&1023);\n        if (io.ctx) {\n            moduleFreeContext(io.ctx, false /* propogate */);\n            zfree(io.ctx);\n        }\n\n        /* Module v2 serialization has an EOF mark at the end. */\n        if (io.ver == 2) {\n            uint64_t eof = rdbLoadLen(rdb,NULL);\n            if (eof == RDB_LENERR) {\n                if (ptr) {\n                    o = createModuleObject(mt,ptr); /* creating just in order to easily destroy */\n                    decrRefCount(o);\n                }\n                return NULL;\n            }\n            if (eof != RDB_MODULE_OPCODE_EOF) {\n                rdbReportCorruptRDB(\"The RDB file contains module data for the module '%s' that is not terminated by \"\n                                    \"the proper module value EOF marker\", moduleTypeModuleName(mt));\n                if (ptr) {\n                    o = createModuleObject(mt,ptr); /* creating just in order to easily destroy */\n                    decrRefCount(o);\n                }\n                return NULL;\n            }\n        }\n\n        if (ptr == NULL) {\n            rdbReportCorruptRDB(\"The RDB file contains module data for the module type '%s', that the responsible \"\n                                \"module is not able to load. Check for modules log above for additional clues.\",\n                                moduleTypeModuleName(mt));\n            return NULL;\n        }\n        o = createModuleObject(mt,ptr);\n    } else if (rdbtype == RDB_TYPE_CRON) {\n        std::unique_ptr<cronjob> spjob = std::make_unique<cronjob>();\n        spjob->script = rdbLoadString(rdb);\n        spjob->startTime = rdbLoadMillisecondTime(rdb,RDB_VERSION);\n        spjob->interval = rdbLoadMillisecondTime(rdb,RDB_VERSION);\n        auto ckeys = rdbLoadLen(rdb,NULL);\n        for (uint64_t i = 0; i < ckeys; ++i)\n            spjob->veckeys.push_back(rdbLoadString(rdb));\n        auto cargs = rdbLoadLen(rdb,NULL);\n        for (uint64_t i = 0; i < cargs; ++i)\n            spjob->vecargs.push_back(rdbLoadString(rdb));\n        o = createObject(OBJ_CRON, spjob.release());\n    } else {\n        rdbReportReadError(\"Unknown RDB encoding type %d\",rdbtype);\n        return NULL;\n    }\n\n    setMvccTstamp(o, mvcc_tstamp);\n    serverAssert(!o->FExpires());\n    if (error) *error = 0;\n    return o;\n\nemptykey:\n    if (error) *error = RDB_LOAD_ERR_EMPTY_KEY;\n    return NULL;\n}\n\n/* Mark that we are loading in the global state and setup the fields\n * needed to provide loading stats. */\nvoid startLoading(size_t size, int rdbflags) {\n    /* Load the DB */\n    g_pserver->loading = (rdbflags & RDBFLAGS_REPLICATION) ? LOADING_REPLICATION : LOADING_BOOT;\n    g_pserver->loading_start_time = time(NULL);\n    g_pserver->loading_loaded_bytes = 0;\n    g_pserver->loading_total_bytes = size;\n    g_pserver->loading_rdb_used_mem = 0;\n    blockingOperationStarts();\n\n    /* Fire the loading modules start event. */\n    int subevent;\n    if (rdbflags & RDBFLAGS_AOF_PREAMBLE)\n        subevent = REDISMODULE_SUBEVENT_LOADING_AOF_START;\n    else if(rdbflags & RDBFLAGS_REPLICATION)\n        subevent = REDISMODULE_SUBEVENT_LOADING_REPL_START;\n    else\n        subevent = REDISMODULE_SUBEVENT_LOADING_RDB_START;\n    moduleFireServerEvent(REDISMODULE_EVENT_LOADING,subevent,NULL);\n}\n\n/* Mark that we are loading in the global state and setup the fields\n * needed to provide loading stats.\n * 'filename' is optional and used for rdb-check on error */\nvoid startLoadingFile(FILE *fp, const char* filename, int rdbflags) {\n    struct stat sb;\n    if (fstat(fileno(fp), &sb) == -1)\n        sb.st_size = 0;\n    rdbFileBeingLoaded = filename;\n    startLoading(sb.st_size, rdbflags);\n}\n\n/* Refresh the loading progress info */\nvoid loadingProgress(off_t pos) {\n    g_pserver->loading_loaded_bytes = pos;\n    if (g_pserver->stat_peak_memory < zmalloc_used_memory())\n        g_pserver->stat_peak_memory = zmalloc_used_memory();\n}\n\n/* Loading finished */\nvoid stopLoading(int success) {\n    g_pserver->loading = 0;\n    blockingOperationEnds();\n    rdbFileBeingLoaded = NULL;\n\n    /* Fire the loading modules end event. */\n    moduleFireServerEvent(REDISMODULE_EVENT_LOADING,\n                          success?\n                            REDISMODULE_SUBEVENT_LOADING_ENDED:\n                            REDISMODULE_SUBEVENT_LOADING_FAILED,\n                          NULL);\n}\n\nvoid startSaving(int rdbflags) {\n    /* Fire the persistence modules end event. */\n    int subevent;\n    if (rdbflags & RDBFLAGS_AOF_PREAMBLE)\n        subevent = REDISMODULE_SUBEVENT_PERSISTENCE_AOF_START;\n    else if (getpid()!=cserver.pid)\n        subevent = REDISMODULE_SUBEVENT_PERSISTENCE_RDB_START;\n    else\n        subevent = REDISMODULE_SUBEVENT_PERSISTENCE_SYNC_RDB_START;\n    moduleFireServerEvent(REDISMODULE_EVENT_PERSISTENCE,subevent,NULL);\n}\n\nvoid stopSaving(int success) {\n    /* Fire the persistence modules end event. */\n    moduleFireServerEvent(REDISMODULE_EVENT_PERSISTENCE,\n                          success?\n                            REDISMODULE_SUBEVENT_PERSISTENCE_ENDED:\n                            REDISMODULE_SUBEVENT_PERSISTENCE_FAILED,\n                          NULL);\n}\n\n\nclass JobBase\n{\npublic:\n    enum class JobType {\n        Function,\n        Insert\n    };\n\n    JobType type;\n\n    JobBase(JobType type)\n        : type(type)\n    {}\n\n    virtual ~JobBase() = default;\n};\n\nstruct rdbInsertJob : public JobBase\n{\n    redisDb *db = nullptr;\n    sds key = nullptr; \n    robj *val = nullptr; \n    long long lru_clock;\n    long long expiretime;\n    long long lru_idle;\n    long long lfu_freq;\n    std::vector<std::pair<robj_sharedptr, long long>> vecsubexpires;\n\n    void addSubexpireKey(robj *subkey, long long when) {\n        vecsubexpires.push_back(std::make_pair(robj_sharedptr(subkey), when));\n        decrRefCount(subkey);\n    }\n\n    rdbInsertJob()\n        : JobBase(JobBase::JobType::Insert)\n    {}\n\n    rdbInsertJob(rdbInsertJob &&src) \n        : JobBase(JobBase::JobType::Insert)\n    {\n        db = src.db;\n        src.db = nullptr;\n        key = src.key;\n        src.key = nullptr;\n        val = src.val;\n        src.val = nullptr;\n        lru_clock = src.lru_clock;\n        expiretime = src.expiretime;\n        lru_idle = src.lru_idle;\n        lfu_freq = src.lfu_freq;\n        vecsubexpires = std::move(src.vecsubexpires);\n    }\n\n    ~rdbInsertJob() {\n        if (key)\n            sdsfree(key);\n        if (val)\n            decrRefCount(val);\n    }\n};\n\nstruct rdbFunctionJob : public JobBase\n{\npublic:\n    std::function<void()> m_fn;\n\n    rdbFunctionJob(std::function<void()> &&fn)\n        : JobBase(JobBase::JobType::Function), m_fn(fn)\n    {}\n};\n\nclass rdbAsyncWorkThread\n{\n    rdbSaveInfo *rsi;\n    int rdbflags;\n    moodycamel::BlockingConcurrentQueue<JobBase*> queueJobs;\n    fastlock m_lockPause { \"rdbAsyncWork-Pause\"};\n    bool fLaunched = false;\n    std::atomic<int> fExit {false};\n    std::atomic<size_t> ckeysLoaded;\n    std::atomic<int> cstorageWritesInFlight;\n    std::atomic<bool> workerThreadDone;\n    std::thread m_thread;\n    std::vector<JobBase*> vecbatch;\n    long long now;\n    long long lastPing = -1;\n\n    static void listFreeMethod(const void *v) {\n        delete reinterpret_cast<const JobBase*>(v);\n    }\n\npublic:\n    \n    rdbAsyncWorkThread(rdbSaveInfo *rsi, int rdbflags, long long now)\n        : rsi(rsi), rdbflags(rdbflags), now(now)\n    {\n        ckeysLoaded = 0;\n        cstorageWritesInFlight = 0;\n    }\n\n    ~rdbAsyncWorkThread() {\n        fExit = true;\n        while (m_lockPause.fOwnLock())\n            m_lockPause.unlock();\n        if (m_thread.joinable())\n            endWork();\n    }\n\n    void start() {\n        serverAssert(!fLaunched);\n        m_thread = std::thread(&rdbAsyncWorkThread::loadWorkerThreadMain, this);\n        fLaunched = true;\n    }\n\n    void throttle() {\n        if (g_pserver->m_pstorageFactory && (getMaxmemoryState(NULL,NULL,NULL,NULL) != C_OK)) {\n            while ((cstorageWritesInFlight.load(std::memory_order_relaxed) || queueJobs.size_approx()) && (getMaxmemoryState(NULL,NULL,NULL,NULL) != C_OK)) {\n                usleep(1);\n                pauseExecution();\n                ProcessWhileBlocked();\n                resumeExecution();\n            }\n\n            if ((getMaxmemoryState(NULL,NULL,NULL,NULL) != C_OK)) {\n                for (int idb = 0; idb < cserver.dbnum; ++idb) {\n                    redisDb *db = g_pserver->db[idb];\n                    if (db->size() > 0 && db->keycacheIsEnabled()) {\n                        serverLog(LL_WARNING, \"Key cache %d exceeds maxmemory during load, freeing - performance may be affected increase maxmemory if possible\", idb);\n                        db->disableKeyCache();\n                    }\n                }\n            }\n        }\n    }\n\n    void enqueue(std::unique_ptr<rdbInsertJob> &spjob) {\n        if (!fLaunched) {\n            processJob(*spjob);\n            spjob = nullptr;\n        } else {\n            vecbatch.push_back(spjob.release());\n            if (vecbatch.size() >= 64) {\n                queueJobs.enqueue_bulk(vecbatch.data(), vecbatch.size());\n                vecbatch.clear();\n                throttle();\n            }\n        }\n    }\n\n    void pauseExecution() {\n        m_lockPause.lock();\n    }\n\n    void resumeExecution() {\n        m_lockPause.unlock();\n    }\n\n    void enqueue(std::function<void()> &&fn) {\n        if (!fLaunched) {\n            fn();\n        } else {\n            std::unique_ptr<JobBase> spjob = std::make_unique<rdbFunctionJob>(std::move(fn));\n            queueJobs.enqueue(spjob.release());\n            throttle();\n        }\n    }\n\n    void ProcessWhileBlocked() {\n        if ((mstime() - lastPing) > 1000) { // Ping if its been a second or longer\n            listIter li;\n            listNode *ln;\n            listRewind(g_pserver->masters, &li);\n            while ((ln = listNext(&li)))\n            {\n                struct redisMaster *mi = (struct redisMaster*)listNodeValue(ln);\n                if (mi->masterhost && mi->repl_state == REPL_STATE_TRANSFER)\n                    replicationSendNewlineToMaster(mi);\n            }\n            lastPing = mstime();\n        }\n\n        processEventsWhileBlocked(serverTL - g_pserver->rgthreadvar);\n    }\n\n    size_t ckeys() { return ckeysLoaded; }\n\n    size_t endWork() {\n        if (!fLaunched) {\n            return ckeysLoaded;\n        }\n        if (!vecbatch.empty()) {\n            queueJobs.enqueue_bulk(vecbatch.data(), vecbatch.size());\n            vecbatch.clear();\n        }\n        std::atomic_thread_fence(std::memory_order_seq_cst);    // The queue must have transferred to the consumer before we call fExit\n        serverAssert(fLaunched);\n        fExit = true;\n        if (g_pserver->m_pstorageFactory) {\n            // If we have a storage provider it can take some time to complete and we want to process events in the meantime\n            while (!workerThreadDone) {\n                usleep(10);\n                pauseExecution();\n                ProcessWhileBlocked();\n                resumeExecution();\n            }\n        }\n        m_thread.join();\n        while (cstorageWritesInFlight.load(std::memory_order_seq_cst)) {\n            usleep(10);\n            ProcessWhileBlocked();\n        }\n        fLaunched = false;\n        fExit = false;\n        serverAssert(queueJobs.size_approx() == 0);\n        return ckeysLoaded;\n    }\n\n    void processJob(rdbInsertJob &job) {\n        redisObjectStack keyobj;\n        initStaticStringObject(keyobj,job.key);\n\n        bool f1024thKey = false;\n        bool fStaleMvccKey = (this->rsi) ? mvccFromObj(job.val) < this->rsi->mvccMinThreshold : false;\n\n        /* Check if the key already expired. This function is used when loading\n        * an RDB file from disk, either at startup, or when an RDB was\n        * received from the master. In the latter case, the master is\n        * responsible for key expiry. If we would expire keys here, the\n        * snapshot taken by the master may not be reflected on the replica. */\n        bool fExpiredKey = iAmMaster() && !(this->rdbflags&RDBFLAGS_AOF_PREAMBLE) && job.expiretime != INVALID_EXPIRE && job.expiretime < this->now;\n        if (fStaleMvccKey || fExpiredKey) {\n            if (fStaleMvccKey && !fExpiredKey && this->rsi != nullptr && this->rsi->mi != nullptr && this->rsi->mi->staleKeyMap != nullptr && lookupKeyRead(job.db, &keyobj) == nullptr) {\n                // We have a key that we've already deleted and is not back in our database.\n                //  We'll need to inform the sending master of the delete if it is also a replica of us\n                robj_sharedptr objKeyDup(createStringObject(job.key, sdslen(job.key)));\n                this->rsi->mi->staleKeyMap->operator[](job.db->id).push_back(objKeyDup);\n            }\n            sdsfree(job.key);\n            job.key = nullptr;\n            decrRefCount(job.val);\n            job.val = nullptr;\n        } else {\n            /* Add the new object in the hash table */\n            int fInserted = dbMerge(job.db, job.key, job.val, (this->rsi && this->rsi->fForceSetKey) || (this->rdbflags & RDBFLAGS_ALLOW_DUP));   // Note: dbMerge will incrRef\n\n            if (fInserted)\n            {\n                auto ckeys = this->ckeysLoaded.fetch_add(1, std::memory_order_relaxed);\n                f1024thKey = (ckeys % 1024) == 0;\n\n                /* Set the expire time if needed */\n                if (job.expiretime != INVALID_EXPIRE)\n                {\n                    setExpire(NULL,job.db,&keyobj,nullptr,job.expiretime);\n                }\n\n                /* Set usage information (for eviction). */\n                objectSetLRUOrLFU(job.val,job.lfu_freq,job.lru_idle,job.lru_clock,1000);\n\n                /* call key space notification on key loaded for modules only */\n                moduleNotifyKeyspaceEvent(NOTIFY_LOADED, \"loaded\", &keyobj, job.db->id);\n\n                replicationNotifyLoadedKey(job.db, &keyobj, job.val, job.expiretime);\n\n                for (auto &pair : job.vecsubexpires) \n                {\n                    setExpire(NULL, job.db, &keyobj, pair.first, pair.second);\n                    replicateSubkeyExpire(job.db, &keyobj, pair.first.get(), pair.second);\n                }\n\n                job.val = nullptr;  // don't free this as we moved ownership to the DB\n            }\n        }\n\n        /* If we have a storage provider check if we need to evict some keys to stay under our memory limit,\n        do this every 16 keys to limit the perf impact */\n        if (g_pserver->m_pstorageFactory && f1024thKey)\n        {\n            bool fHighMemory = (getMaxmemoryState(NULL,NULL,NULL,NULL) != C_OK);\n            if (fHighMemory || f1024thKey)\n            {\n                for (int idb = 0; idb < cserver.dbnum; ++idb)\n                {\n                    if (g_pserver->m_pstorageFactory) {\n                        g_pserver->db[idb]->processChangesAsync(this->cstorageWritesInFlight);\n                        fHighMemory = false;\n                    }\n                }\n                if (fHighMemory)\n                    performEvictions(false /* fPreSnapshot*/);\n            }\n            g_pserver->garbageCollector.endEpoch(serverTL->gcEpoch);\n            serverTL->gcEpoch = g_pserver->garbageCollector.startEpoch();\n        }\n    }\n\n    static void loadWorkerThreadMain(rdbAsyncWorkThread *pqueue) {\n        rdbAsyncWorkThread &queue = *pqueue;\n        redisServerThreadVars vars = {};\n        vars.clients_pending_asyncwrite = listCreate();\n        serverTL = &vars;\n        aeSetThreadOwnsLockOverride(true);\n\n#ifdef __linux__\n        // We will inheret the server thread's affinity mask, clear it as we want to run on a different core.\n        cpu_set_t *cpuset = CPU_ALLOC(std::thread::hardware_concurrency());\n        if (cpuset != nullptr) {\n            size_t size = CPU_ALLOC_SIZE(std::thread::hardware_concurrency());\n            CPU_ZERO_S(size, cpuset);\n            for (unsigned i = 0; i < std::thread::hardware_concurrency(); ++i) {\n                CPU_SET_S(i, size, cpuset);\n            }\n            pthread_setaffinity_np(pthread_self(), size, cpuset);\n            CPU_FREE(cpuset);\n        }\n#endif\n\n        for (;;) {\n            if (queue.queueJobs.size_approx() == 0) {\n                if (queue.fExit.load(std::memory_order_relaxed))\n                    break;\n            }\n\n            if (queue.fExit.load(std::memory_order_seq_cst) && queue.queueJobs.size_approx() == 0)\n                break;\n\n            vars.gcEpoch = g_pserver->garbageCollector.startEpoch();\n            JobBase *rgjob[64];\n            int cjobs = 0;\n            while ((cjobs = pqueue->queueJobs.wait_dequeue_bulk_timed(rgjob, 64, std::chrono::milliseconds(5))) > 0) {\n                std::unique_lock<fastlock> ulPause(pqueue->m_lockPause);\n\n                for (int ijob = 0; ijob < cjobs; ++ijob) {\n                    JobBase *pjob = rgjob[ijob];\n                    switch (pjob->type)\n                    {\n                    case JobBase::JobType::Insert:\n                        pqueue->processJob(*static_cast<rdbInsertJob*>(pjob));\n                        break;\n\n                    case JobBase::JobType::Function:\n                        static_cast<rdbFunctionJob*>(pjob)->m_fn();\n                        break;\n                    }\n                    delete pjob;\n                }\n            }\n            g_pserver->garbageCollector.endEpoch(vars.gcEpoch);\n        }\n\n        if (g_pserver->m_pstorageFactory) {\n            for (int idb = 0; idb < cserver.dbnum; ++idb)\n                g_pserver->db[idb]->processChangesAsync(queue.cstorageWritesInFlight);\n        }\n\n        queue.workerThreadDone = true;\n        ProcessPendingAsyncWrites();\n        listRelease(vars.clients_pending_asyncwrite);\n        aeSetThreadOwnsLockOverride(false);\n    }\n};\n\n/* Track loading progress in order to serve client's from time to time\n   and if needed calculate rdb checksum  */\nvoid rdbLoadProgressCallback(rio *r, const void *buf, size_t len) {\n    if (g_pserver->rdb_checksum)\n        rioGenericUpdateChecksum(r, buf, len);\n    \n    if ((g_pserver->loading_process_events_interval_bytes &&\n        (r->processed_bytes + len)/g_pserver->loading_process_events_interval_bytes > r->processed_bytes/g_pserver->loading_process_events_interval_bytes) ||\n        (g_pserver->loading_process_events_interval_keys &&\n        (r->keys_since_last_callback >= g_pserver->loading_process_events_interval_keys)))\n    {\n        rdbAsyncWorkThread *pwthread = reinterpret_cast<rdbAsyncWorkThread*>(r->chksum_arg);\n        mstime_t mstime;\n        __atomic_load(&g_pserver->mstime, &mstime, __ATOMIC_RELAXED);\n        bool fUpdateReplication = (mstime - r->last_update) > 1000;\n\n        if (fUpdateReplication) {\n            listIter li;\n            listNode *ln;\n            listRewind(g_pserver->masters, &li);\n            while ((ln = listNext(&li)))\n            {\n                struct redisMaster *mi = (struct redisMaster*)listNodeValue(ln);\n                if (mi->masterhost && mi->repl_state == REPL_STATE_TRANSFER)\n                    replicationSendNewlineToMaster(mi);\n            }\n        }\n        loadingProgress(r->processed_bytes);\n\n        if (pwthread)\n            pwthread->pauseExecution();\n        processEventsWhileBlocked(serverTL - g_pserver->rgthreadvar);\n        if (pwthread)\n            pwthread->resumeExecution();\n\n        processModuleLoadingProgressEvent(0);\n\n        if (fUpdateReplication) {\n            robj *ping_argv[1];\n\n            ping_argv[0] = createStringObject(\"PING\",4);\n            replicationFeedSlaves(g_pserver->slaves, g_pserver->replicaseldb, ping_argv, 1);\n            decrRefCount(ping_argv[0]);\n        }\n\n        if (fUpdateReplication) r->last_update = g_pserver->mstime;\n        r->keys_since_last_callback = 0;\n    }\n}\n\n\n/* Load an RDB file from the rio stream 'rdb'. On success C_OK is returned,\n * otherwise C_ERR is returned and 'errno' is set accordingly. */\nint rdbLoadRio(rio *rdb, int rdbflags, rdbSaveInfo *rsi) {\n    uint64_t dbid = 0;\n    int type, rdbver;\n    redisDb *dbCur = g_pserver->db[dbid];\n    char buf[1024];\n    /* Key-specific attributes, set by opcodes before the key type. */\n    long long lru_idle = -1, lfu_freq = -1, expiretime = INVALID_EXPIRE, now;\n    long long lru_clock = 0;\n    unsigned long long ckeysLoaded = 0;\n    uint64_t mvcc_tstamp = OBJ_MVCC_INVALID;\n    now = mstime();\n    rdbAsyncWorkThread wqueue(rsi, rdbflags, now);\n    robj *subexpireKey = nullptr;\n    sds key = nullptr;\n    bool fLastKeyExpired = false;\n    int error;\n    long long empty_keys_skipped = 0, expired_keys_skipped = 0, keys_loaded = 0;\n    std::unique_ptr<rdbInsertJob> spjob;\n\n    // If we're tracking changes we need to reset this\n    std::vector<bool> fTracking(cserver.dbnum);\n    // We don't want to track here because processChangesAsync is outside the normal scope handling\n    for (int idb = 0; idb < cserver.dbnum; ++idb) {\n        if ((fTracking[idb] = g_pserver->db[idb]->FTrackingChanges()))\n            if (g_pserver->db[idb]->processChanges(false))\n                g_pserver->db[idb]->commitChanges();\n    }\n\n    rdb->update_cksum = rdbLoadProgressCallback;\n    rdb->chksum_arg = &wqueue;\n    rdb->max_processing_chunk = g_pserver->loading_process_events_interval_bytes;\n    if (rioRead(rdb,buf,9) == 0) goto eoferr;\n    buf[9] = '\\0';\n    if (memcmp(buf,\"REDIS\",5) != 0) {\n        serverLog(LL_WARNING,\"Wrong signature trying to load DB from file\");\n        errno = EINVAL;\n        return C_ERR;\n    }\n    rdbver = atoi(buf+5);\n    if (rdbver < 1 || rdbver > RDB_VERSION) {\n        serverLog(LL_WARNING,\"Can't handle RDB format version %d\",rdbver);\n        errno = EINVAL;\n        return C_ERR;\n    }\n\n    lru_clock = LRU_CLOCK();\n    if (g_pserver->multithread_load_enabled)\n        wqueue.start();\n\n    while(1) {\n        robj *val;\n\n        /* Read type. */\n        if ((type = rdbLoadType(rdb)) == -1) goto eoferr;\n\n        /* Handle special types. */\n        if (type == RDB_OPCODE_EXPIRETIME) {\n            /* EXPIRETIME: load an expire associated with the next key\n             * to load. Note that after loading an expire we need to\n             * load the actual type, and continue. */\n            expiretime = rdbLoadTime(rdb);\n            expiretime *= 1000;\n            if (rioGetReadError(rdb)) goto eoferr;\n            continue; /* Read next opcode. */\n        } else if (type == RDB_OPCODE_EXPIRETIME_MS) {\n            /* EXPIRETIME_MS: milliseconds precision expire times introduced\n             * with RDB v3. Like EXPIRETIME but no with more precision. */\n            expiretime = rdbLoadMillisecondTime(rdb,rdbver);\n            if (rioGetReadError(rdb)) goto eoferr;\n            continue; /* Read next opcode. */\n        } else if (type == RDB_OPCODE_FREQ) {\n            /* FREQ: LFU frequency. */\n            uint8_t byte;\n            if (rioRead(rdb,&byte,1) == 0) goto eoferr;\n            lfu_freq = byte;\n            continue; /* Read next opcode. */\n        } else if (type == RDB_OPCODE_IDLE) {\n            /* IDLE: LRU idle time. */\n            uint64_t qword;\n            if ((qword = rdbLoadLen(rdb,NULL)) == RDB_LENERR) goto eoferr;\n            lru_idle = qword;\n            continue; /* Read next opcode. */\n        } else if (type == RDB_OPCODE_EOF) {\n            /* EOF: End of file, exit the main loop. */\n            break;\n        } else if (type == RDB_OPCODE_SELECTDB) {\n            /* SELECTDB: Select the specified database. */\n            if ((dbid = rdbLoadLen(rdb,NULL)) == RDB_LENERR) goto eoferr;\n            if (dbid >= (unsigned)cserver.dbnum) {\n                serverLog(LL_WARNING,\n                    \"FATAL: Data file was created with a KeyDB \"\n                    \"server configured to handle more than %d \"\n                    \"databases. Exiting\\n\", cserver.dbnum);\n                exit(1);\n            }\n            dbCur = g_pserver->db[dbid];\n            continue; /* Read next opcode. */\n        } else if (type == RDB_OPCODE_RESIZEDB) {\n            /* RESIZEDB: Hint about the size of the keys in the currently\n             * selected data base, in order to avoid useless rehashing. */\n            uint64_t db_size, expires_size;\n            if ((db_size = rdbLoadLen(rdb,NULL)) == RDB_LENERR)\n                goto eoferr;\n            if ((expires_size = rdbLoadLen(rdb,NULL)) == RDB_LENERR)\n                goto eoferr;\n            if (g_pserver->allowRdbResizeOp && !g_pserver->m_pstorageFactory) {\n                wqueue.enqueue([dbCur, db_size]{\n                    dbCur->expand(db_size);\n                });\n            }\n            continue; /* Read next opcode. */\n        } else if (type == RDB_OPCODE_AUX) {\n            /* AUX: generic string-string fields. Use to add state to RDB\n             * which is backward compatible. Implementations of RDB loading\n             * are required to skip AUX fields they don't understand.\n             *\n             * An AUX field is composed of two strings: key and value. */\n            robj *auxkey = nullptr, *auxval = nullptr;\n            if ((auxkey = rdbLoadStringObject(rdb)) == NULL) goto eoferr;\n            if ((auxval = rdbLoadStringObject(rdb)) == NULL) goto eoferr;\n\n            if (((char*)ptrFromObj(auxkey))[0] == '%') {\n                /* All the fields with a name staring with '%' are considered\n                 * information fields and are logged at startup with a log\n                 * level of NOTICE. */\n                serverLog(LL_NOTICE,\"RDB '%s': %s\",\n                    (char*)ptrFromObj(auxkey),\n                    (char*)ptrFromObj(auxval));\n            } else if (!strcasecmp(szFromObj(auxkey),\"repl-stream-db\")) {\n                if (rsi) rsi->repl_stream_db = atoi(szFromObj(auxval));\n            } else if (!strcasecmp(szFromObj(auxkey),\"repl-id\")) {\n                if (rsi && sdslen(szFromObj(auxval)) == CONFIG_RUN_ID_SIZE) {\n                    memcpy(rsi->repl_id,ptrFromObj(auxval),CONFIG_RUN_ID_SIZE+1);\n                    rsi->repl_id_is_set = 1;\n                }\n            } else if (!strcasecmp(szFromObj(auxkey),\"repl-masters\")) {\n                if (rsi) {\n                    char *masters = szFromObj(auxval);\n                    char *saveptr;\n                    char *entry = strtok_r(masters, \":\", &saveptr);\n                    while (entry != NULL) {\n                        MasterSaveInfo msi;\n                        bool fSet = true;\n                        if (strlen(entry) == sizeof(msi.master_replid)-1)\n                            memcpy(msi.master_replid, entry, sizeof(msi.master_replid));\n                        else\n                            fSet = false;\n                        entry = strtok_r(NULL, \":\", &saveptr);\n                        if (entry == nullptr) break;\n                        msi.master_initial_offset = atoll(entry);\n                        entry = strtok_r(NULL, \":\", &saveptr);\n                        if (entry == nullptr) break;\n                        msi.masterhost = sdsstring(sdsnew(entry));\n                        entry = strtok_r(NULL, \":\", &saveptr);\n                        if (entry == nullptr) break;\n                        msi.masterport = atoi(entry);\n                        entry = strtok_r(NULL, \";\", &saveptr);\n                        if (entry == nullptr) break;\n                        msi.selected_db = atoi(entry);\n                        entry = strtok_r(NULL, \":\", &saveptr);\n                        if (fSet)\n                            rsi->addMaster(msi);\n                    }\n                }\n            } else if (!strcasecmp(szFromObj(auxkey),\"repl-offset\")) {\n                if (rsi) rsi->repl_offset = strtoll(szFromObj(auxval),NULL,10);\n            } else if (!strcasecmp(szFromObj(auxkey),\"lua\")) {\n                /* Load the script back in memory. */\n                if (luaCreateFunction(NULL,g_pserver->lua,auxval) == NULL) {\n                    rdbReportCorruptRDB(\n                        \"Can't load Lua script from RDB file! \"\n                        \"BODY: %s\", (char*)ptrFromObj(auxval));\n                }\n            } else if (!strcasecmp(szFromObj(auxkey),\"redis-ver\")) {\n                serverLog(LL_NOTICE,\"Loading RDB produced by version %s\",\n                    (const char*)ptrFromObj(auxval));\n            } else if (!strcasecmp(szFromObj(auxkey),\"ctime\")) {\n                time_t age = time(NULL)-strtol(szFromObj(auxval),NULL,10);\n                if (age < 0) age = 0;\n                serverLog(LL_NOTICE,\"RDB age %ld seconds\",\n                    (unsigned long) age);\n            } else if (!strcasecmp(szFromObj(auxkey),\"used-mem\")) {\n                long long usedmem = strtoll(szFromObj(auxval),NULL,10);\n                serverLog(LL_NOTICE,\"RDB memory usage when created %.2f Mb\",\n                    (double) usedmem / (1024*1024));\n                g_pserver->loading_rdb_used_mem = usedmem;\n            } else if (!strcasecmp(szFromObj(auxkey),\"aof-preamble\")) {\n                long long haspreamble = strtoll(szFromObj(auxval),NULL,10);\n                if (haspreamble) serverLog(LL_NOTICE,\"RDB has an AOF tail\");\n            } else if (!strcasecmp(szFromObj(auxkey),\"redis-bits\")) {\n                /* Just ignored. */\n            } else if (!strcasecmp(szFromObj(auxkey),\"mvcc-tstamp\")) {\n                static_assert(sizeof(unsigned long long) == sizeof(uint64_t), \"Ensure long long is 64-bits\");\n                mvcc_tstamp = strtoull(szFromObj(auxval), nullptr, 10);\n            } else if (!strcasecmp(szFromObj(auxkey), \"keydb-subexpire-key\")) {\n                if (subexpireKey != nullptr) {\n                    serverLog(LL_WARNING, \"Corrupt subexpire entry in RDB skipping. key: %s subkey: %s\", key != nullptr ? key : \"(null)\", subexpireKey != nullptr ? szFromObj(subexpireKey) : \"(null)\");\n                    decrRefCount(subexpireKey);\n                    subexpireKey = nullptr;\n                }\n                subexpireKey = auxval;\n                incrRefCount(subexpireKey);\n            } else if (!strcasecmp(szFromObj(auxkey), \"keydb-subexpire-when\")) {\n                if (key == nullptr || subexpireKey == nullptr) {\n                    if (!fLastKeyExpired) { // This is not an error if we just expired the key associated with this subexpire\n                        serverLog(LL_WARNING, \"Corrupt subexpire entry in RDB skipping. key: %s subkey: %s\", key != nullptr ? key : \"(null)\", subexpireKey != nullptr ? szFromObj(subexpireKey) : \"(null)\");\n                    }\n                    if (subexpireKey) {\n                        decrRefCount(subexpireKey);\n                        subexpireKey = nullptr;\n                    }\n                }\n                else {\n                    long long expireT = strtoll(szFromObj(auxval), nullptr, 10);\n                    serverAssert(spjob != nullptr);\n                    serverAssert(sdscmp(key, spjob->key) == 0);\n                    spjob->addSubexpireKey(subexpireKey, expireT);\n                    subexpireKey = nullptr;\n                }\n            } else {\n                /* We ignore fields we don't understand, as by AUX field\n                 * contract. */\n                serverLog(LL_DEBUG,\"Unrecognized RDB AUX field: '%s'\",\n                    (char*)ptrFromObj(auxkey));\n            }\n\n            decrRefCount(auxkey);\n            decrRefCount(auxval);\n            continue; /* Read type again. */\n        } else if (type == RDB_OPCODE_MODULE_AUX) {\n            /* Load module data that is not related to the Redis key space.\n             * Such data can be potentially be stored both before and after the\n             * RDB keys-values section. */\n            uint64_t moduleid = rdbLoadLen(rdb,NULL);\n            int when_opcode = rdbLoadLen(rdb,NULL);\n            int when = rdbLoadLen(rdb,NULL);\n            if (rioGetReadError(rdb)) goto eoferr;\n            if (when_opcode != RDB_MODULE_OPCODE_UINT) {\n                rdbReportReadError(\"bad when_opcode\");\n                goto eoferr;\n            }\n            moduleType *mt = moduleTypeLookupModuleByID(moduleid);\n            char name[10];\n            moduleTypeNameByID(name,moduleid);\n\n            if (!rdbCheckMode && mt == NULL) {\n                /* Unknown module. */\n                serverLog(LL_WARNING,\"The RDB file contains AUX module data I can't load: no matching module '%s'\", name);\n                exit(1);\n            } else if (!rdbCheckMode && mt != NULL) {\n                if (!mt->aux_load) {\n                    /* Module doesn't support AUX. */\n                    serverLog(LL_WARNING,\"The RDB file contains module AUX data, but the module '%s' doesn't seem to support it.\", name);\n                    exit(1);\n                }\n\n                RedisModuleIO io;\n                moduleInitIOContext(io,mt,rdb,NULL);\n                io.ver = 2;\n                /* Call the rdb_load method of the module providing the 10 bit\n                 * encoding version in the lower 10 bits of the module ID. */\n                if (mt->aux_load(&io,moduleid&1023, when) != REDISMODULE_OK || io.error) {\n                    moduleTypeNameByID(name,moduleid);\n                    serverLog(LL_WARNING,\"The RDB file contains module AUX data for the module type '%s', that the responsible module is not able to load. Check for modules log above for additional clues.\", name);\n                    goto eoferr;\n                }\n                if (io.ctx) {\n                    moduleFreeContext(io.ctx);\n                    zfree(io.ctx);\n                }\n                uint64_t eof = rdbLoadLen(rdb,NULL);\n                if (eof != RDB_MODULE_OPCODE_EOF) {\n                    serverLog(LL_WARNING,\"The RDB file contains module AUX data for the module '%s' that is not terminated by the proper module value EOF marker\", name);\n                    goto eoferr;\n                }\n                continue;\n            } else {\n                /* RDB check mode. */\n                robj *aux = rdbLoadCheckModuleValue(rdb,name);\n                decrRefCount(aux);\n                continue; /* Read next opcode. */\n            }\n        }\n\n        /* Read key */\n        if (key != nullptr)\n        {\n            sdsfree(key);\n            key = nullptr;\n        }\n\n        if ((key = (sds)rdbGenericLoadStringObject(rdb,RDB_LOAD_SDS_SHARED,NULL)) == NULL)\n            goto eoferr;\n        /* Read value */\n        val = rdbLoadObject(type,rdb,key,&error,mvcc_tstamp);\n        if (val == NULL) {\n            /* Since we used to have bug that could lead to empty keys\n             * (See #8453), we rather not fail when empty key is encountered\n             * in an RDB file, instead we will silently discard it and\n             * continue loading. */\n            if (error == RDB_LOAD_ERR_EMPTY_KEY) {\n                if(empty_keys_skipped++ < 10)\n                    serverLog(LL_WARNING, \"rdbLoadObject skipping empty key: %s\", key);\n                sdsfree(key);\n                key = nullptr;\n            } else {\n                sdsfree(key);\n                key = nullptr;\n                goto eoferr;\n            }\n        } else {\n            bool fStaleMvccKey = (rsi) ? mvccFromObj(val) < rsi->mvccMinThreshold : false;\n            if (spjob != nullptr)\n                wqueue.enqueue(spjob);\n            spjob = std::make_unique<rdbInsertJob>();\n            spjob->db = dbCur;\n            spjob->key = sdsdupshared(key);\n            spjob->val = val;\n            spjob->lru_clock = lru_clock;\n            spjob->expiretime = expiretime;\n            spjob->lru_idle = lru_idle;\n            spjob->lfu_freq = lfu_freq;\n            val = nullptr;\n\n            /* Check if the key already expired. This function is used when loading\n            * an RDB file from disk, either at startup, or when an RDB was\n            * received from the master. In the latter case, the master is\n            * responsible for key expiry. If we would expire keys here, the\n            * snapshot taken by the master may not be reflected on the replica.\n            * Similarly if the RDB is the preamble of an AOF file, we want to\n            * load all the keys as they are, since the log of operations later\n            * assume to work in an exact keyspace state. */\n            bool fExpiredKey = iAmMaster() && !(rdbflags&RDBFLAGS_AOF_PREAMBLE) && expiretime != INVALID_EXPIRE && expiretime < now;\n            fLastKeyExpired = fStaleMvccKey || fExpiredKey;\n\n            ckeysLoaded++;\n            if (g_pserver->m_pstorageFactory && (ckeysLoaded % 128) == 0)\n            {\n                if (!serverTL->gcEpoch.isReset()) {\n                    g_pserver->garbageCollector.endEpoch(serverTL->gcEpoch);\n                    serverTL->gcEpoch = g_pserver->garbageCollector.startEpoch();\n                }\n            }\n\n            if (g_pserver->key_load_delay)\n                debugDelay(g_pserver->key_load_delay);\n\n            rdb->keys_since_last_callback++;\n\n            /* Reset the state that is key-specified and is populated by\n            * opcodes before the key, so that we start from scratch again. */\n            expiretime = INVALID_EXPIRE;\n            lfu_freq = -1;\n            lru_idle = -1;\n        }\n    }\n\n    if (spjob != nullptr)\n        wqueue.enqueue(spjob);\n\n    if (key != nullptr)\n    {\n        sdsfree(key);\n        key = nullptr;\n    }\n\n    if (subexpireKey != nullptr)\n    {\n        serverLog(LL_WARNING, \"Corrupt subexpire entry in RDB.\");\n        decrRefCount(subexpireKey);\n        subexpireKey = nullptr;\n    }\n    \n    /* Verify the checksum if RDB version is >= 5 */\n    if (rdbver >= 5) {\n        uint64_t cksum, expected = rdb->cksum;\n\n        if (rioRead(rdb,&cksum,8) == 0) goto eoferr;\n        if (g_pserver->rdb_checksum && !cserver.skip_checksum_validation) {\n            memrev64ifbe(&cksum);\n            if (cksum == 0) {\n                serverLog(LL_WARNING,\"RDB file was saved with checksum disabled: no check performed.\");\n            } else if (cksum != expected) {\n                serverLog(LL_WARNING,\"Wrong RDB checksum expected: (%llx) but \"\n                    \"got (%llx). Aborting now.\",\n                        (unsigned long long)expected,\n                        (unsigned long long)cksum);\n                rdbReportCorruptRDB(\"RDB CRC error\");\n            }\n        }\n    }\n\n    wqueue.endWork();\n    // Reset track changes\n    for (int idb = 0; idb < cserver.dbnum; ++idb) {\n        if (fTracking[idb])\n            g_pserver->db[idb]->trackChanges(false);\n    }\n    if (empty_keys_skipped) {\n        serverLog(LL_WARNING,\n            \"Done loading RDB, keys loaded: %lld, keys expired: %lld, empty keys skipped: %lld.\",\n                keys_loaded, expired_keys_skipped, empty_keys_skipped);\n    } else {\n        serverLog(LL_WARNING,\n            \"Done loading RDB, keys loaded: %lld, keys expired: %lld.\",\n                keys_loaded, expired_keys_skipped);\n    }\n    return C_OK;\n\n    /* Unexpected end of file is handled here calling rdbReportReadError():\n     * this will in turn either abort Redis in most cases, or if we are loading\n     * the RDB file from a socket during initial SYNC (diskless replica mode),\n     * we'll report the error to the caller, so that we can retry. */\neoferr:\n        // Reset track changes\n    for (int idb = 0; idb < cserver.dbnum; ++idb) {\n        if (fTracking[idb])\n            g_pserver->db[idb]->trackChanges(false);\n    }\n\n    wqueue.endWork();\n    if (key != nullptr)\n    {\n        sdsfree(key);\n        key = nullptr;\n    }\n    if (subexpireKey != nullptr)\n    {\n        decrRefCount(subexpireKey);\n        subexpireKey = nullptr;\n    }\n\n    serverLog(LL_WARNING,\n        \"Short read or OOM loading DB. Unrecoverable error, aborting now.\");\n    rdbReportReadError(\"Unexpected EOF reading RDB file\");\n    return C_ERR;\n}\n\nvoid updateActiveReplicaMastersFromRsi(rdbSaveInfo *rsi) {\n    if (rsi != nullptr && g_pserver->fActiveReplica) {\n        serverLog(LL_NOTICE, \"RDB contains information on %d masters\", (int)rsi->numMasters());\n        listIter li;\n        listNode *ln;\n        \n        listRewind(g_pserver->masters, &li);\n        while ((ln = listNext(&li)))\n        {\n            redisMaster *mi = (redisMaster*)listNodeValue(ln);\n            if (mi->master != nullptr) {\n                continue; //ignore connected masters\n            }\n            for (size_t i = 0; i < rsi->numMasters(); i++) {\n                if (!sdscmp(mi->masterhost, (sds)rsi->vecmastersaveinfo[i].masterhost.get()) && mi->masterport == rsi->vecmastersaveinfo[i].masterport) {\n                    memcpy(mi->master_replid, rsi->vecmastersaveinfo[i].master_replid, sizeof(mi->master_replid));\n                    mi->master_initial_offset = rsi->vecmastersaveinfo[i].master_initial_offset;\n                    replicationCacheMasterUsingMaster(mi);\n                    serverLog(LL_NOTICE, \"Cached master recovered from RDB for %s:%d\", mi->masterhost, mi->masterport);\n                    break;\n                }\n            }\n        }\n    }\n}\n\nint rdbLoad(rdbSaveInfo *rsi, int rdbflags)\n{\n    int err = C_ERR;\n    if (g_pserver->rdb_filename != NULL)\n        err = rdbLoadFile(g_pserver->rdb_filename, rsi,rdbflags);\n\n    if ((err == C_ERR) && g_pserver->rdb_s3bucketpath != NULL)\n        err = rdbLoadS3(g_pserver->rdb_s3bucketpath, rsi, rdbflags);\n\n    return err;\n}\n\n/* Like rdbLoadRio() but takes a filename instead of a rio stream. The\n * filename is open for reading and a rio stream object created in order\n * to do the actual loading. Moreover the ETA displayed in the INFO\n * output is initialized and finalized.\n *\n * If you pass an 'rsi' structure initialied with RDB_SAVE_OPTION_INIT, the\n * loading code will fiil the information fields in the structure. */\nint rdbLoadFile(const char *filename, rdbSaveInfo *rsi, int rdbflags) {\n    FILE *fp;\n    rio rdb;\n    int retval;\n\n    if ((fp = fopen(filename,\"r\")) == NULL) return C_ERR;\n    startLoadingFile(fp, filename,rdbflags);\n    rioInitWithFile(&rdb,fp);\n    retval = rdbLoadRio(&rdb,rdbflags,rsi);\n    fclose(fp);\n    stopLoading(retval==C_OK);\n    return retval;\n}\n\n/* A background saving child (BGSAVE) terminated its work. Handle this.\n * This function covers the case of actual BGSAVEs. */\nstatic void backgroundSaveDoneHandlerDisk(int exitcode, bool fCancelled) {\n    if (!fCancelled && exitcode == 0) {\n        serverLog(LL_NOTICE,\n            \"Background saving terminated with success\");\n        g_pserver->dirty = g_pserver->dirty - g_pserver->dirty_before_bgsave;\n        g_pserver->lastsave = time(NULL);\n        g_pserver->lastbgsave_status = C_OK;\n        latencyEndMonitor(g_pserver->rdb_save_latency);\n        latencyAddSampleIfNeeded(\"rdb-save\",g_pserver->rdb_save_latency);\n    } else if (!fCancelled && exitcode != 0) {\n        serverLog(LL_WARNING, \"Background saving error\");\n        g_pserver->lastbgsave_status = C_ERR;\n    } else {\n        mstime_t latency;\n\n        serverLog(LL_WARNING,\n            \"Background saving cancelled\");\n        latencyStartMonitor(latency);\n        rdbRemoveTempFile(g_pserver->rdbThreadVars.tmpfileNum, 0);\n        latencyEndMonitor(latency);\n        latencyAddSampleIfNeeded(\"rdb-unlink-temp-file\",latency);\n    }\n}\n\n/* A background saving child (BGSAVE) terminated its work. Handle this.\n * This function covers the case of RDB -> Slaves socket transfers for\n * diskless replication. */\nstatic void backgroundSaveDoneHandlerSocket(int exitcode, bool fCancelled) {\n    serverAssert(GlobalLocksAcquired());\n\n    if (!fCancelled && exitcode == 0) {\n        serverLog(LL_NOTICE,\n            \"Background RDB transfer terminated with success\");\n    } else if (!fCancelled && exitcode != 0) {\n        serverLog(LL_WARNING, \"Background transfer error\");\n    } else {\n        serverLog(LL_WARNING,\n            \"Background transfer terminated cancelled\");\n    }\n    if (g_pserver->rdb_child_exit_pipe!=-1)\n        close(g_pserver->rdb_child_exit_pipe);\n    auto pipeT = g_pserver->rdb_pipe_read;\n    aePostFunction(g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].el, [pipeT]{\n        aeDeleteFileEvent(serverTL->el, pipeT, AE_READABLE);\n        close(pipeT);\n    });\n    g_pserver->rdb_child_exit_pipe = -1;\n    g_pserver->rdb_pipe_read = -1;\n    zfree(g_pserver->rdb_pipe_conns);\n    g_pserver->rdb_pipe_conns = NULL;\n    g_pserver->rdb_pipe_numconns = 0;\n    g_pserver->rdb_pipe_numconns_writing = 0;\n    zfree(g_pserver->rdb_pipe_buff);\n    g_pserver->rdb_pipe_buff = NULL;\n    g_pserver->rdb_pipe_bufflen = 0;\n}\n\n/* When a background RDB saving/transfer terminates, call the right handler. */\nvoid backgroundSaveDoneHandler(int exitcode, bool fCancelled) {\n    int type = g_pserver->rdb_child_type;\n    switch(g_pserver->rdb_child_type) {\n    case RDB_CHILD_TYPE_DISK:\n        backgroundSaveDoneHandlerDisk(exitcode,fCancelled);\n        break;\n    case RDB_CHILD_TYPE_SOCKET:\n        backgroundSaveDoneHandlerSocket(exitcode,fCancelled);\n        break;\n    default:\n        serverPanic(\"Unknown RDB child type.\");\n        break;\n    }\n\n    g_pserver->rdbThreadVars.fRdbThreadActive = false;\n    g_pserver->rdb_child_type = RDB_CHILD_TYPE_NONE;\n    g_pserver->rdb_save_time_last = time(NULL)-g_pserver->rdb_save_time_start;\n    g_pserver->rdb_save_time_start = -1;\n    /* Possibly there are slaves waiting for a BGSAVE in order to be served\n     * (the first stage of SYNC is a bulk transfer of dump.rdb) */\n    updateSlavesWaitingBgsave((!fCancelled && exitcode == 0) ? C_OK : C_ERR, type);\n}\n\nvoid unblockChildThreadIfNecessary()\n{\n    if (g_pserver->rdbThreadVars.fRdbThreadActive && g_pserver->rdbThreadVars.fRdbThreadCancel) {\n        char buffer[1024];\n        if (g_pserver->rdb_pipe_read >= 0) {\n            while (read(g_pserver->rdb_pipe_read, buffer, sizeof(buffer)) > 0);\n        }\n        receiveChildInfo();\n    }\n}\n\n/* Kill the RDB saving child using SIGUSR1 (so that the parent will know\n * the child did not exit for sn error, but because we wanted), and performs\n * the cleanup needed. */\nvoid killRDBChild(bool fSynchronous) {\n    serverAssert(GlobalLocksAcquired());\n\n    if (cserver.fForkBgSave) {\n        kill(g_pserver->child_pid,SIGUSR1);\n    } else { \n        g_pserver->rdbThreadVars.fRdbThreadCancel = true;\n        if (g_pserver->rdb_child_type == RDB_CHILD_TYPE_SOCKET) {\n            // Wake up the thread so it can exit\n            auto t = write(g_pserver->rdb_child_exit_pipe, &cserver.fForkBgSave, 1);\n            UNUSED(t);\n            // Flush out the rdb pipe in case the writer thread is blocked\n            unblockChildThreadIfNecessary();\n        }\n        if (fSynchronous)\n        {\n            aeReleaseLock();\n            serverAssert(!GlobalLocksAcquired());\n            void *result;\n            int err = pthread_join(g_pserver->rdbThreadVars.rdb_child_thread, &result);\n            if (err) {\n                serverLog(LL_WARNING, \"RDB child thread could not be joined: %s\", strerror(err));\n            }\n            g_pserver->rdbThreadVars.fRdbThreadCancel = false;\n            aeAcquireLock();\n        }\n    }\n}\n\nstruct rdbSaveSocketThreadArgs\n{\n    rdbSaveInfo rsi;\n    int rdb_pipe_write;\n    int safe_to_exit_pipe;\n    const redisDbPersistentDataSnapshot *rgpdb[1];\n};\nvoid *rdbSaveToSlavesSocketsThread(void *vargs)\n{\n    serverAssert(!g_pserver->rdbThreadVars.fDone);\n    /* Child */\n    serverAssert(serverTL == nullptr);\n    rdbSaveSocketThreadArgs *args = (rdbSaveSocketThreadArgs*)vargs;\n    int retval;\n    rio rdb;\n\n    aeThreadOnline();\n    serverAssert(serverTL == nullptr);\n    redisServerThreadVars vars;\n    serverTL = &vars;\n    vars.gcEpoch = g_pserver->garbageCollector.startEpoch();\n\n    rioInitWithFd(&rdb,args->rdb_pipe_write);\n\n    retval = rdbSaveRioWithEOFMark(&rdb,args->rgpdb,NULL,&args->rsi);\n    if (retval == C_OK && rioFlush(&rdb) == 0)\n        retval = C_ERR;\n\n    if (retval == C_OK) {\n        sendChildCowInfo(CHILD_INFO_TYPE_RDB_COW_SIZE, \"RDB\");\n    }\n\n    rioFreeFd(&rdb);\n    close(args->rdb_pipe_write); /* wake up the reader, tell it we're done. */\n    /* hold exit until the parent tells us it's safe. we're not expecting\n     * to read anything, just get the error when the pipe is closed. */\n    if (!g_pserver->rdbThreadVars.fRdbThreadCancel) {\n        char dummyBuffer;\n        auto dummy = read(args->safe_to_exit_pipe, &dummyBuffer, 1);\n        UNUSED(dummy);\n    }\n\n    // If we were told to cancel the requesting thread is holding the lock for us\n    for (int idb = 0; idb < cserver.dbnum; ++idb)\n        g_pserver->db[idb]->endSnapshotAsync(args->rgpdb[idb]);\n\n    g_pserver->garbageCollector.endEpoch(vars.gcEpoch);\n    aeThreadOffline();\n\n    close(args->safe_to_exit_pipe);\n    args->rsi.~rdbSaveInfo();\n    zfree(args);\n    g_pserver->rdbThreadVars.fDone = true;\n    return (retval == C_OK) ? (void*)0 : (void*)1;\n}\n\n/* Spawn an RDB child that writes the RDB to the sockets of the slaves\n * that are currently in SLAVE_STATE_WAIT_BGSAVE_START state. */\nint rdbSaveToSlavesSockets(rdbSaveInfo *rsi) {\n    serverAssert(GlobalLocksAcquired());\n    listNode *ln;\n    listIter li;\n    pthread_t child;\n    int pipefds[2];\n    rdbSaveSocketThreadArgs *args = nullptr;\n\n    if (hasActiveChildProcessOrBGSave()) return C_ERR;\n\n    /* Even if the previous fork child exited, don't start a new one until we\n     * drained the pipe. */\n    if (g_pserver->rdb_pipe_conns) return C_ERR;\n\n    /* Before to fork, create a pipe that is used to transfer the rdb bytes to\n     * the parent, we can't let it write directly to the sockets, since in case\n     * of TLS we must let the parent handle a continuous TLS state when the\n     * child terminates and parent takes over. */\n    if (pipe(pipefds) == -1) return C_ERR;\n\n    args = (rdbSaveSocketThreadArgs*)zmalloc(sizeof(rdbSaveSocketThreadArgs) + sizeof(redisDbPersistentDataSnapshot*)*(cserver.dbnum-1), MALLOC_LOCAL);\n    g_pserver->rdb_pipe_read = pipefds[0]; /* read end */\n    args->rdb_pipe_write = pipefds[1]; /* write end */\n    anetNonBlock(NULL, g_pserver->rdb_pipe_read);\n\n    args->rsi = *(new (&args->rsi) rdbSaveInfo(*rsi));\n    memcpy(&args->rsi.repl_id, g_pserver->replid, sizeof(g_pserver->replid));\n    args->rsi.master_repl_offset = g_pserver->master_repl_offset;\n\n    /* create another pipe that is used by the parent to signal to the child\n     * that it can exit. */\n    if (pipe(pipefds) == -1) {\n        close(args->rdb_pipe_write);\n        close(g_pserver->rdb_pipe_read);\n        zfree(args);\n        return C_ERR;\n    }\n    args->safe_to_exit_pipe = pipefds[0]; /* read end */\n    g_pserver->rdb_child_exit_pipe = pipefds[1]; /* write end */\n\n    /* Collect the connections of the replicas we want to transfer\n     * the RDB to, which are i WAIT_BGSAVE_START state. */\n    g_pserver->rdb_pipe_conns = (connection**)zmalloc(sizeof(connection *)*listLength(g_pserver->slaves));\n    g_pserver->rdb_pipe_numconns = 0;\n    g_pserver->rdb_pipe_numconns_writing = 0;\n    listRewind(g_pserver->slaves,&li);\n    while((ln = listNext(&li))) {\n        client *slave = (client*)ln->value;\n        if (slave->replstate == SLAVE_STATE_WAIT_BGSAVE_START) {\n            g_pserver->rdb_pipe_conns[g_pserver->rdb_pipe_numconns++] = slave->conn;\n            replicationSetupSlaveForFullResync(slave,getPsyncInitialOffset());\n        }\n    }\n\n    /* Create the child process. */\n    if (cserver.fForkBgSave) {\n        pid_t childpid;\n        if ((childpid = redisFork(CHILD_TYPE_RDB)) == 0) {\n            /* Child */\n            int retval, dummy;\n            rio rdb;\n\n            rioInitWithFd(&rdb,args->rdb_pipe_write);\n\n            redisSetProcTitle(\"keydb-rdb-to-slaves\");\n            redisSetCpuAffinity(g_pserver->bgsave_cpulist);\n\n            retval = rdbSaveRioWithEOFMark(&rdb,nullptr,nullptr,rsi);\n            if (retval == C_OK && rioFlush(&rdb) == 0)\n                retval = C_ERR;\n\n            if (retval == C_OK) {\n                sendChildCowInfo(CHILD_INFO_TYPE_RDB_COW_SIZE, \"RDB\");\n            }\n\n            rioFreeFd(&rdb);\n            /* wake up the reader, tell it we're done. */\n            close(args->rdb_pipe_write);\n            close(g_pserver->rdb_child_exit_pipe); /* close write end so that we can detect the close on the parent. */\n            /* hold exit until the parent tells us it's safe. we're not expecting\n            * to read anything, just get the error when the pipe is closed. */\n            dummy = read(args->safe_to_exit_pipe, pipefds, 1);\n            UNUSED(dummy);\n            exitFromChild((retval == C_OK) ? 0 : 1);\n        } else {\n            /* Parent */\n            close(args->safe_to_exit_pipe);\n            if (childpid == -1) {\n                serverLog(LL_WARNING,\"Can't save in background: fork: %s\",\n                    strerror(errno));\n\n                /* Undo the state change. The caller will perform cleanup on\n                * all the slaves in BGSAVE_START state, but an early call to\n                * replicationSetupSlaveForFullResync() turned it into BGSAVE_END */\n                listRewind(g_pserver->slaves,&li);\n                while((ln = listNext(&li))) {\n                    client *replica = (client*)ln->value;\n                    if (replica->replstate == SLAVE_STATE_WAIT_BGSAVE_END) {\n                        replica->replstate = SLAVE_STATE_WAIT_BGSAVE_START;\n                    }\n                }\n                close(args->rdb_pipe_write);\n                close(g_pserver->rdb_pipe_read);\n                zfree(g_pserver->rdb_pipe_conns);\n                g_pserver->rdb_pipe_conns = NULL;\n                g_pserver->rdb_pipe_numconns = 0;\n                g_pserver->rdb_pipe_numconns_writing = 0;\n                args->rsi.~rdbSaveInfo();\n                zfree(args);\n            } else {\n                serverLog(LL_NOTICE,\"Background RDB transfer started by pid %ld\",\n                    (long)childpid);\n                g_pserver->rdb_save_time_start = time(NULL);\n                g_pserver->rdb_child_type = RDB_CHILD_TYPE_SOCKET;\n                g_pserver->rdbThreadVars.fRdbThreadActive = true;\n                updateDictResizePolicy();\n                close(args->rdb_pipe_write); /* close write in parent so that it can detect the close on the child. */\n                aePostFunction(g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].el, []{\n                    if (aeCreateFileEvent(serverTL->el, g_pserver->rdb_pipe_read, AE_READABLE, rdbPipeReadHandler, nullptr) == AE_ERR) {\n                        serverPanic(\"Unrecoverable error creating g_pserver->rdb_pipe_read file event.\");\n                    }\n                });\n            }\n            return (childpid == -1) ? C_ERR : C_OK;\n        }\n    }\n    else {\n        openChildInfoPipe();\n\n        for (int idb = 0; idb < cserver.dbnum; ++idb)\n            args->rgpdb[idb] = g_pserver->db[idb]->createSnapshot(getMvccTstamp(), false /*fOptional*/);\n\n        g_pserver->rdbThreadVars.tmpfileNum++;\n        g_pserver->rdbThreadVars.fRdbThreadCancel = false;\n        pthread_attr_t tattr;\n        pthread_attr_init(&tattr);\n        pthread_attr_setstacksize(&tattr, 1 << 23); // 8 MB\n        if (pthread_create(&child, &tattr, rdbSaveToSlavesSocketsThread, args)) {\n            pthread_attr_destroy(&tattr);\n            serverLog(LL_WARNING,\"Can't save in background: fork: %s\",\n                strerror(errno));\n\n            /* Undo the state change. The caller will perform cleanup on\n                * all the slaves in BGSAVE_START state, but an early call to\n                * replicationSetupSlaveForFullResync() turned it into BGSAVE_END */\n            listRewind(g_pserver->slaves,&li);\n            while((ln = listNext(&li))) {\n                client *replica = (client*)ln->value;\n                if (replica->replstate == SLAVE_STATE_WAIT_BGSAVE_END) {\n                    replica->replstate = SLAVE_STATE_WAIT_BGSAVE_START;\n                }\n            }\n            close(args->rdb_pipe_write);\n            close(g_pserver->rdb_pipe_read);\n            zfree(g_pserver->rdb_pipe_conns);\n            close(args->safe_to_exit_pipe);\n            g_pserver->rdb_pipe_conns = NULL;\n            g_pserver->rdb_pipe_numconns = 0;\n            g_pserver->rdb_pipe_numconns_writing = 0;\n            args->rsi.~rdbSaveInfo();\n            zfree(args);\n            closeChildInfoPipe();\n            return C_ERR;\n        }\n        pthread_attr_destroy(&tattr);\n        g_pserver->child_type = CHILD_TYPE_RDB;\n\n        serverLog(LL_NOTICE,\"Background RDB transfer started\");\n        g_pserver->rdb_save_time_start = time(NULL);\n        serverAssert(!g_pserver->rdbThreadVars.fRdbThreadActive);\n        g_pserver->rdbThreadVars.rdb_child_thread = child;\n        g_pserver->rdbThreadVars.fRdbThreadActive = true;\n        g_pserver->rdb_child_type = RDB_CHILD_TYPE_SOCKET;\n        aePostFunction(g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].el, []{\n            if (aeCreateFileEvent(serverTL->el, g_pserver->rdb_pipe_read, AE_READABLE, rdbPipeReadHandler, nullptr) == AE_ERR) {\n                serverPanic(\"Unrecoverable error creating server.rdb_pipe_read file event.\");\n            }\n        });\n    }\n\n    return C_OK; /* Unreached. */\n}\n\nvoid saveCommand(client *c) {\n    if (g_pserver->FRdbSaveInProgress()) {\n        addReplyError(c,\"Background save already in progress\");\n        return;\n    }\n    rdbSaveInfo rsi, *rsiptr;\n    rsiptr = rdbPopulateSaveInfo(&rsi);\n    if (rdbSave(nullptr, rsiptr) == C_OK) {\n        addReply(c,shared.ok);\n    } else {\n        addReplyErrorObject(c,shared.err);\n    }\n}\n\n/* BGSAVE [SCHEDULE] */\nvoid bgsaveCommand(client *c) {\n    int schedule = 0;\n\n    /* The SCHEDULE option changes the behavior of BGSAVE when an AOF rewrite\n     * is in progress. Instead of returning an error a BGSAVE gets scheduled. */\n    if (c->argc > 1) {\n        if (c->argc == 2 && !strcasecmp(szFromObj(c->argv[1]),\"schedule\")) {\n            schedule = 1;\n        } else {\n            addReplyErrorObject(c,shared.syntaxerr);\n            return;\n        }\n    }\n\n    rdbSaveInfo rsi, *rsiptr;\n    rsiptr = rdbPopulateSaveInfo(&rsi);\n\n    if (g_pserver->FRdbSaveInProgress()) {\n        addReplyError(c,\"Background save already in progress\");\n    } else if (hasActiveChildProcess()) {\n        if (schedule) {\n            g_pserver->rdb_bgsave_scheduled = 1;\n            addReplyStatus(c,\"Background saving scheduled\");\n        } else {\n            addReplyError(c,\n            \"Another child process is active (AOF?): can't BGSAVE right now. \"\n            \"Use BGSAVE SCHEDULE in order to schedule a BGSAVE whenever \"\n            \"possible.\");\n        }\n    } else if (rdbSaveBackground(rsiptr) == C_OK) {\n        addReplyStatus(c,\"Background saving started\");\n    } else {\n        addReplyErrorObject(c,shared.err);\n    }\n}\n\n/* Populate the rdbSaveInfo structure used to persist the replication\n * information inside the RDB file. Currently the structure explicitly\n * contains just the currently selected DB from the master stream, however\n * if the rdbSave*() family functions receive a NULL rsi structure also\n * the Replication ID/offset is not saved. The function popultes 'rsi'\n * that is normally stack-allocated in the caller, returns the populated\n * pointer if the instance has a valid master client, otherwise NULL\n * is returned, and the RDB saving will not persist any replication related\n * information. */\nrdbSaveInfo *rdbPopulateSaveInfo(rdbSaveInfo *rsi) {\n    rdbSaveInfo rsi_init;\n    *rsi = rsi_init;\n\n    memcpy(rsi->repl_id, g_pserver->replid, sizeof(g_pserver->replid));\n    rsi->master_repl_offset = g_pserver->master_repl_offset;\n\n    if (g_pserver->fActiveReplica) {\n        listIter li;\n        listNode *ln = nullptr;\n        listRewind(g_pserver->masters, &li);\n        while ((ln = listNext(&li))) {\n            redisMaster *mi = (redisMaster*)listNodeValue(ln);\n            MasterSaveInfo msi(*mi);\n            rsi->addMaster(msi);\n        }\n    }\n\n    /* If the instance is a master, we can populate the replication info\n     * only when repl_backlog is not NULL. If the repl_backlog is NULL,\n     * it means that the instance isn't in any replication chains. In this\n     * scenario the replication info is useless, because when a replica\n     * connects to us, the NULL repl_backlog will trigger a full\n     * synchronization, at the same time we will use a new replid and clear\n     * replid2. */\n    if (g_pserver->fActiveReplica || (!listLength(g_pserver->masters) && g_pserver->repl_backlog)) {\n        /* Note that when g_pserver->replicaseldb is -1, it means that this master\n         * didn't apply any write commands after a full synchronization.\n         * So we can let repl_stream_db be 0, this allows a restarted replica\n         * to reload replication ID/offset, it's safe because the next write\n         * command must generate a SELECT statement. */\n        rsi->repl_stream_db = g_pserver->replicaseldb == -1 ? 0 : g_pserver->replicaseldb;\n        return rsi;\n    }\n\n    struct redisMaster *miFirst = (redisMaster*)(listLength(g_pserver->masters) ? listNodeValue(listFirst(g_pserver->masters)) : NULL);\n\n    /* If the instance is a replica we need a connected master\n     * in order to fetch the currently selected DB. */\n    if (miFirst && miFirst->master) {\n        rsi->repl_stream_db = miFirst->master->db->id;\n        return rsi;\n    }\n\n    /* If we have a cached master we can use it in order to populate the\n     * replication selected DB info inside the RDB file: the replica can\n     * increment the master_repl_offset only from data arriving from the\n     * master, so if we are disconnected the offset in the cached master\n     * is valid. */\n    if (miFirst && miFirst->cached_master) {\n        rsi->repl_stream_db = miFirst->cached_master->db->id;\n        return rsi;\n    }\n    return NULL;\n}\n"
  },
  {
    "path": "src/rdb.h",
    "content": "/*\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __RDB_H\n#define __RDB_H\n\n#include <stdio.h>\n#include \"rio.h\"\n\n/* TBD: include only necessary headers. */\n#include \"server.h\"\n\n/* The current RDB version. When the format changes in a way that is no longer\n * backward compatible this number gets incremented. */\n#define RDB_VERSION 9\n\n/* Defines related to the dump file format. To store 32 bits lengths for short\n * keys requires a lot of space, so we check the most significant 2 bits of\n * the first byte to interpreter the length:\n *\n * 00|XXXXXX => if the two MSB are 00 the len is the 6 bits of this byte\n * 01|XXXXXX XXXXXXXX =>  01, the len is 14 bits, 6 bits + 8 bits of next byte\n * 10|000000 [32 bit integer] => A full 32 bit len in net byte order will follow\n * 10|000001 [64 bit integer] => A full 64 bit len in net byte order will follow\n * 11|OBKIND this means: specially encoded object will follow. The six bits\n *           number specify the kind of object that follows.\n *           See the RDB_ENC_* defines.\n *\n * Lengths up to 63 are stored using a single byte, most DB keys, and may\n * values, will fit inside. */\n#define RDB_6BITLEN 0\n#define RDB_14BITLEN 1\n#define RDB_32BITLEN 0x80\n#define RDB_64BITLEN 0x81\n#define RDB_ENCVAL 3\n#define RDB_LENERR UINT64_MAX\n\n/* When a length of a string object stored on disk has the first two bits\n * set, the remaining six bits specify a special encoding for the object\n * accordingly to the following defines: */\n#define RDB_ENC_INT8 0        /* 8 bit signed integer */\n#define RDB_ENC_INT16 1       /* 16 bit signed integer */\n#define RDB_ENC_INT32 2       /* 32 bit signed integer */\n#define RDB_ENC_LZF 3         /* string compressed with FASTLZ */\n\n/* Map object types to RDB object types. Macros starting with OBJ_ are for\n * memory storage and may change. Instead RDB types must be fixed because\n * we store them on disk. */\n#define RDB_TYPE_STRING 0\n#define RDB_TYPE_LIST   1\n#define RDB_TYPE_SET    2\n#define RDB_TYPE_ZSET   3\n#define RDB_TYPE_HASH   4\n#define RDB_TYPE_ZSET_2 5 /* ZSET version 2 with doubles stored in binary. */\n#define RDB_TYPE_MODULE 6\n#define RDB_TYPE_MODULE_2 7 /* Module value with annotations for parsing without\n                               the generating module being loaded. */\n/* NOTE: WHEN ADDING NEW RDB TYPE, UPDATE rdbIsObjectType() BELOW */\n\n/* Object types for encoded objects. */\n#define RDB_TYPE_HASH_ZIPMAP    9\n#define RDB_TYPE_LIST_ZIPLIST  10\n#define RDB_TYPE_SET_INTSET    11\n#define RDB_TYPE_ZSET_ZIPLIST  12\n#define RDB_TYPE_HASH_ZIPLIST  13\n#define RDB_TYPE_LIST_QUICKLIST 14\n#define RDB_TYPE_STREAM_LISTPACKS 15\n\n/* KeyDB Specific Object Types */\n#define RDB_TYPE_CRON 64\n/* NOTE: WHEN ADDING NEW RDB TYPE, UPDATE rdbIsObjectType() BELOW */\n\n/* Test if a type is an object type. */\n#define rdbIsObjectType(t) ((t >= 0 && t <= 7) || (t >= 9 && t <= 15) || (t == RDB_TYPE_CRON))\n\n/* Special RDB opcodes (saved/loaded with rdbSaveType/rdbLoadType). */\n#define RDB_OPCODE_MODULE_AUX 247   /* Module auxiliary data. */\n#define RDB_OPCODE_IDLE       248   /* LRU idle time. */\n#define RDB_OPCODE_FREQ       249   /* LFU frequency. */\n#define RDB_OPCODE_AUX        250   /* RDB aux field. */\n#define RDB_OPCODE_RESIZEDB   251   /* Hash table resize hint. */\n#define RDB_OPCODE_EXPIRETIME_MS 252    /* Expire time in milliseconds. */\n#define RDB_OPCODE_EXPIRETIME 253       /* Old expire time in seconds. */\n#define RDB_OPCODE_SELECTDB   254   /* DB number of the following keys. */\n#define RDB_OPCODE_EOF        255   /* End of the RDB file. */\n\n/* Module serialized values sub opcodes */\n#define RDB_MODULE_OPCODE_EOF   0   /* End of module value. */\n#define RDB_MODULE_OPCODE_SINT  1   /* Signed integer. */\n#define RDB_MODULE_OPCODE_UINT  2   /* Unsigned integer. */\n#define RDB_MODULE_OPCODE_FLOAT 3   /* Float. */\n#define RDB_MODULE_OPCODE_DOUBLE 4  /* Double. */\n#define RDB_MODULE_OPCODE_STRING 5  /* String. */\n\n/* rdbLoad...() functions flags. */\n#define RDB_LOAD_NONE       0\n#define RDB_LOAD_ENC        (1<<0)\n#define RDB_LOAD_PLAIN      (1<<1)\n#define RDB_LOAD_SDS        (1<<2)\n#define RDB_LOAD_SDS_SHARED ((1 << 3) | RDB_LOAD_SDS)\n\n/* flags on the purpose of rdb save or load */\n#define RDBFLAGS_NONE 0                 /* No special RDB loading. */\n#define RDBFLAGS_AOF_PREAMBLE (1<<0)    /* Load/save the RDB as AOF preamble. */\n#define RDBFLAGS_REPLICATION (1<<1)     /* Load/save for SYNC. */\n#define RDBFLAGS_ALLOW_DUP (1<<2)       /* Allow duplicated keys when loading.*/\n\n/* When rdbLoadObject() returns NULL, the err flag is\n * set to hold the type of error that occurred */\n#define RDB_LOAD_ERR_EMPTY_KEY  1   /* Error of empty key */\n#define RDB_LOAD_ERR_OTHER      2   /* Any other errors */\n\nint rdbSaveType(rio *rdb, unsigned char type);\nint rdbLoadType(rio *rdb);\nint rdbSaveTime(rio *rdb, time_t t);\ntime_t rdbLoadTime(rio *rdb);\nint rdbSaveLen(rio *rdb, uint64_t len);\nint rdbSaveMillisecondTime(rio *rdb, long long t);\nlong long rdbLoadMillisecondTime(rio *rdb, int rdbver);\nuint64_t rdbLoadLen(rio *rdb, int *isencoded);\nint rdbLoadLenByRef(rio *rdb, int *isencoded, uint64_t *lenptr);\nint rdbSaveObjectType(rio *rdb, robj_roptr o);\nint rdbLoadObjectType(rio *rdb);\nint rdbLoad(rdbSaveInfo *rsi, int rdbflags);\nint rdbLoadFile(const char *filename, rdbSaveInfo *rsi, int rdbflags);\nint rdbSaveBackground(rdbSaveInfo *rsi);\nint rdbSaveToSlavesSockets(rdbSaveInfo *rsi);\nvoid getTempFileName(char tmpfile[], int tmpfileNum);\nvoid rdbRemoveTempFile(pid_t childpid, int from_signal);\nint rdbSave(const redisDbPersistentDataSnapshot **rgpdb, rdbSaveInfo *rsi);\nint rdbSaveFile(char *filename, const redisDbPersistentDataSnapshot **rgpdb, rdbSaveInfo *rsi);\nint rdbSaveFp(FILE *pf, const redisDbPersistentDataSnapshot **rgpdb, rdbSaveInfo *rsi);\nint rdbSaveS3(char *path, const redisDbPersistentDataSnapshot **rgpdb, rdbSaveInfo *rsi);\nint rdbLoadS3(char *path, rdbSaveInfo *rsi, int rdbflags);\nssize_t rdbSaveObject(rio *rdb, robj_roptr o, robj_roptr key);\nsize_t rdbSavedObjectLen(robj *o, robj *key);\nrobj *rdbLoadObject(int type, rio *rdb, sds key, int *error, uint64_t mvcc_tstamp);\nvoid backgroundSaveDoneHandler(int exitcode, bool fCancelled);\nint rdbSaveKeyValuePair(rio *rdb, robj *key, robj *val, long long expiretime);\nssize_t rdbSaveSingleModuleAux(rio *rdb, int when, moduleType *mt);\nrobj *rdbLoadCheckModuleValue(rio *rdb, char *modulename);\nrobj *rdbLoadStringObject(rio *rdb);\nssize_t rdbSaveStringObject(rio *rdb, robj_roptr obj);\nssize_t rdbSaveRawString(rio *rdb, const unsigned char *s, size_t len);\nvoid *rdbGenericLoadStringObject(rio *rdb, int flags, size_t *lenptr);\nint rdbSaveBinaryDoubleValue(rio *rdb, double val);\nint rdbLoadBinaryDoubleValue(rio *rdb, double *val);\nint rdbSaveBinaryFloatValue(rio *rdb, float val);\nint rdbLoadBinaryFloatValue(rio *rdb, float *val);\nint rdbLoadRio(rio *rdb, int rdbflags, rdbSaveInfo *rsi);\nint rdbSaveRio(rio *rdb, const redisDbPersistentDataSnapshot **rgpdb, int *error, int flags, rdbSaveInfo *rsi);\nrdbSaveInfo *rdbPopulateSaveInfo(rdbSaveInfo *rsi);\n\n#endif\n"
  },
  {
    "path": "src/readwritelock.h",
    "content": "#pragma once\n#include <condition_variable>\n\nclass readWriteLock {\n    fastlock m_readLock;\n    fastlock m_writeLock;\n    std::condition_variable_any m_cv;\n    int m_readCount = 0;\n    int m_writeCount = 0;\n    bool m_writeWaiting = false;\n    bool m_multi = true;\npublic:\n    readWriteLock(const char *name) : m_readLock(name), m_writeLock(name) {}\n\n    void acquireRead() {\n        std::unique_lock<fastlock> rm(m_readLock, std::defer_lock);\n        if (m_multi) {\n            rm.lock();\n            while (m_writeCount > 0 || m_writeWaiting)\n                m_cv.wait(rm);\n        }\n        m_readCount++;\n    }\n    \n    bool tryAcquireRead() {\n        std::unique_lock<fastlock> rm(m_readLock, std::defer_lock);\n        if (m_multi) {\n            if (!rm.try_lock())\n                return false;\n            if (m_writeCount > 0 || m_writeWaiting)\n                return false;\n        }\n        m_readCount++;\n        return true;\n    }\n\n    void acquireWrite(bool exclusive = true) {\n        std::unique_lock<fastlock> rm(m_readLock, std::defer_lock);\n        if (m_multi) {\n            rm.lock();\n            m_writeWaiting = true;\n            while (m_readCount > 0)\n                m_cv.wait(rm);\n            if (exclusive) {\n                /* Another thread might have the write lock while we have the internal lock\n                but won't be able to release it until they can acquire the internal lock\n                so release the internal lock and try again instead of waiting to avoid deadlock */\n                while(!m_writeLock.try_lock())\n                    m_cv.wait(rm);\n            }\n        }\n        m_writeCount++;\n        m_writeWaiting = false;\n    }\n\n    void upgradeWrite(bool exclusive = true) {\n        releaseRead();\n        acquireWrite(exclusive);\n    }\n\n    bool tryAcquireWrite(bool exclusive = true) {\n        std::unique_lock<fastlock> rm(m_readLock, std::defer_lock);\n        if (m_multi) {\n            if (!rm.try_lock())\n                return false;\n            if (m_readCount > 0)\n                return false;\n            if (exclusive)\n                if (!m_writeLock.try_lock())\n                    return false;\n        }\n        m_writeCount++;\n        return true;\n    }\n\n    void releaseRead() {\n        std::unique_lock<fastlock> rm(m_readLock, std::defer_lock);\n        if (m_multi) {\n            rm.lock();\n            m_cv.notify_all();\n        }\n        m_readCount--;\n    }\n\n    void releaseWrite(bool exclusive = true) {\n        std::unique_lock<fastlock> rm(m_readLock, std::defer_lock);\n        serverAssert(m_writeCount > 0);\n        if (m_multi) {\n            rm.lock();\n            if (exclusive)\n                m_writeLock.unlock();\n            m_cv.notify_all();\n        }\n        m_writeCount--;\n    }\n\n    void downgradeWrite(bool exclusive = true) {\n        releaseWrite(exclusive);\n        acquireRead();\n    }\n\n    void setMulti(bool multi) {\n        m_multi = multi;\n    }\n\n    bool hasReader() {\n        return m_readCount > 0;\n    }\n\n    bool hasWriter() {\n        return m_writeCount > 0;\n    }\n\n    bool writeWaiting() {\n        return m_writeWaiting;\n    }\n};"
  },
  {
    "path": "src/redis-benchmark.cpp",
    "content": "/* Redis benchmark utility.\n *\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"fmacros.h\"\n#include \"version.h\"\n\n#include <stdio.h>\n#include <string.h>\n#include <stdlib.h>\n#include <unistd.h>\n#include <errno.h>\n#include <time.h>\n#include <sys/time.h>\n#include <signal.h>\n#include <assert.h>\n#include <math.h>\n#include <pthread.h>\n#include <string>\nextern \"C\" {\n#include \"sdscompat.h\" /* Use hiredis' sds compat header that maps sds calls to their hi_ variants */\n#include <sds.h> /* Use hiredis sds. */\n#include <hiredis.h>\n}\n#include \"ae.h\"\n#ifdef USE_OPENSSL\n#include <openssl/ssl.h>\n#include <openssl/err.h>\n#include <hiredis_ssl.h>\n#endif\n#include \"adlist.h\"\n#include \"dict.h\"\n#include \"zmalloc.h\"\n#include \"storage.h\"\n#include \"atomicvar.h\"\n#include \"crc16_slottable.h\"\n#include \"hdr_histogram.h\"\n#include \"cli_common.h\"\n#include \"mt19937-64.h\"\n\n#define UNUSED(V) ((void) V)\n#define RANDPTR_INITIAL_SIZE 8\n#define DEFAULT_LATENCY_PRECISION 3\n#define MAX_LATENCY_PRECISION 4\n#define MAX_THREADS 500\n#define CLUSTER_SLOTS 16384\n#define CONFIG_LATENCY_HISTOGRAM_MIN_VALUE 10L          /* >= 10 usecs */\n#define CONFIG_LATENCY_HISTOGRAM_MAX_VALUE 3000000L          /* <= 30 secs(us precision) */\n#define CONFIG_LATENCY_HISTOGRAM_INSTANT_MAX_VALUE 3000000L   /* <= 3 secs(us precision) */\n\n#define CLIENT_GET_EVENTLOOP(c) \\\n    (c->thread_id >= 0 ? config.threads[c->thread_id]->el : config.el)\n\nstruct benchmarkThread;\nstruct clusterNode;\nstruct redisConfig;\n\nint g_fTestMode = false;\n\nstatic struct config {\n    aeEventLoop *el;\n    const char *hostip;\n    int hostport;\n    const char *hostsocket;\n    int tls;\n    struct cliSSLconfig sslconfig;\n    int numclients;\n    redisAtomic int liveclients;\n    int requests;\n    redisAtomic int requests_issued;\n    redisAtomic int requests_finished;\n    redisAtomic int previous_requests_finished;\n    int last_printed_bytes;\n    long long previous_tick;\n    int keysize;\n    int datasize;\n    int randomkeys;\n    int randomkeys_keyspacelen;\n    int keepalive;\n    int pipeline;\n    long long start;\n    long long totlatency;\n    const char *title;\n    list *clients;\n    int quiet;\n    int csv;\n    int loop;\n    int idlemode;\n    int dbnum;\n    sds dbnumstr;\n    char *tests;\n    char *auth;\n    const char *user;\n    int precision;\n    int num_threads;\n    struct benchmarkThread **threads;\n    int cluster_mode;\n    int cluster_node_count;\n    struct clusterNode **cluster_nodes;\n    struct redisConfig *redis_config;\n    struct hdr_histogram* latency_histogram;\n    struct hdr_histogram* current_sec_latency_histogram;\n    redisAtomic int is_fetching_slots;\n    redisAtomic int is_updating_slots;\n    redisAtomic int slots_last_update;\n    int enable_tracking;\n    pthread_mutex_t liveclients_mutex;\n    pthread_mutex_t is_updating_slots_mutex;\n} config;\n\ntypedef struct _client {\n    redisContext *context;\n    sds obuf;\n    char **randptr;         /* Pointers to :rand: strings inside the command buf */\n    size_t randlen;         /* Number of pointers in client->randptr */\n    size_t randfree;        /* Number of unused pointers in client->randptr */\n    char **stagptr;         /* Pointers to slot hashtags (cluster mode only) */\n    size_t staglen;         /* Number of pointers in client->stagptr */\n    size_t stagfree;        /* Number of unused pointers in client->stagptr */\n    size_t written;         /* Bytes of 'obuf' already written */\n    long long start;        /* Start time of a request */\n    long long latency;      /* Request latency */\n    int pending;            /* Number of pending requests (replies to consume) */\n    int prefix_pending;     /* If non-zero, number of pending prefix commands. Commands\n                               such as auth and select are prefixed to the pipeline of\n                               benchmark commands and discarded after the first send. */\n    int prefixlen;          /* Size in bytes of the pending prefix commands */\n    int thread_id;\n    struct clusterNode *cluster_node;\n    int slots_last_update;\n} *client;\n\n/* Threads. */\n\ntypedef struct benchmarkThread {\n    int index;\n    pthread_t thread;\n    aeEventLoop *el;\n} benchmarkThread;\n\n/* Cluster. */\ntypedef struct clusterNode {\n    char *ip;\n    int port;\n    sds name;\n    int flags;\n    sds replicate;  /* Master ID if node is a replica */\n    int *slots;\n    int slots_count;\n    int current_slot_index;\n    int *updated_slots;         /* Used by updateClusterSlotsConfiguration */\n    int updated_slots_count;    /* Used by updateClusterSlotsConfiguration */\n    int replicas_count;\n    sds *migrating; /* An array of sds where even strings are slots and odd\n                     * strings are the destination node IDs. */\n    sds *importing; /* An array of sds where even strings are slots and odd\n                     * strings are the source node IDs. */\n    int migrating_count; /* Length of the migrating array (migrating slots*2) */\n    int importing_count; /* Length of the importing array (importing slots*2) */\n    struct redisConfig *redis_config;\n} clusterNode;\n\ntypedef struct redisConfig {\n    sds save;\n    sds appendonly;\n} redisConfig;\n\nint g_fInCrash = false;\n\n/* Prototypes */\nextern \"C\" char *redisGitSHA1(void);\nextern \"C\" char *redisGitDirty(void);\nstatic void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask);\nstatic void createMissingClients(client c);\nstatic benchmarkThread *createBenchmarkThread(int index);\nstatic void freeBenchmarkThread(benchmarkThread *thread);\nstatic void freeBenchmarkThreads();\nstatic void *execBenchmarkThread(void *ptr);\nstatic clusterNode *createClusterNode(char *ip, int port);\nstatic redisConfig *getRedisConfig(const char *ip, int port,\n                                   const char *hostsocket);\nstatic redisContext *getRedisContext(const char *ip, int port,\n                                     const char *hostsocket);\nstatic void freeRedisConfig(redisConfig *cfg);\nstatic int fetchClusterSlotsConfiguration(client c);\nstatic void updateClusterSlotsConfiguration();\nint showThroughput(struct aeEventLoop *eventLoop, long long id,\n                   void *clientData);\n\nstatic sds benchmarkVersion(void) {\n    sds version;\n    version = sdscatprintf(sdsempty(), \"%s\", KEYDB_REAL_VERSION);\n\n    /* Add git commit and working tree status when available */\n    if (strtoll(redisGitSHA1(),NULL,16)) {\n        version = sdscatprintf(version, \" (git:%s\", redisGitSHA1());\n        if (strtoll(redisGitDirty(),NULL,10))\n            version = sdscatprintf(version, \"-dirty\");\n        version = sdscat(version, \")\");\n    }\n    return version;\n}\n\n/* Dict callbacks */\nstatic uint64_t dictSdsHash(const void *key);\nstatic int dictSdsKeyCompare(void *privdata, const void *key1,\n    const void *key2);\n\n/* Implementation */\nstatic long long ustime(void) {\n    struct timeval tv;\n    long long ust;\n\n    gettimeofday(&tv, NULL);\n    ust = ((long)tv.tv_sec)*1000000;\n    ust += tv.tv_usec;\n    return ust;\n}\n\nstatic long long mstime(void) {\n    struct timeval tv;\n    long long mst;\n\n    gettimeofday(&tv, NULL);\n    mst = ((long long)tv.tv_sec)*1000;\n    mst += tv.tv_usec/1000;\n    return mst;\n}\n\nstatic uint64_t dictSdsHash(const void *key) {\n    return dictGenHashFunction((unsigned char*)key, sdslen((char*)key));\n}\n\nstatic int dictSdsKeyCompare(void *privdata, const void *key1,\n        const void *key2)\n{\n    int l1,l2;\n    DICT_NOTUSED(privdata);\n\n    l1 = sdslen((sds)key1);\n    l2 = sdslen((sds)key2);\n    if (l1 != l2) return 0;\n    return memcmp(key1, key2, l1) == 0;\n}\n\n/* _serverAssert is needed by dict */\nextern \"C\" void _serverAssert(const char *estr, const char *file, int line) {\n    fprintf(stderr, \"=== ASSERTION FAILED ===\");\n    fprintf(stderr, \"==> %s:%d '%s' is not true\",file,line,estr);\n    *((char*)-1) = 'x';\n}\n\nstatic redisContext *getRedisContext(const char *ip, int port,\n                                     const char *hostsocket)\n{\n    redisContext *ctx = NULL;\n    redisReply *reply =  NULL;\n    if (hostsocket == NULL)\n        ctx = redisConnect(ip, port);\n    else\n        ctx = redisConnectUnix(hostsocket);\n    if (ctx == NULL || ctx->err) {\n        fprintf(stderr,\"Could not connect to Redis at \");\n        const char *err = (ctx != NULL ? ctx->errstr : \"\");\n        if (hostsocket == NULL)\n            fprintf(stderr,\"%s:%d: %s\\n\",ip,port,err);\n        else\n            fprintf(stderr,\"%s: %s\\n\",hostsocket,err);\n        goto cleanup;\n    }\n    if (config.tls==1) {\n        const char *err = NULL;\n        if (cliSecureConnection(ctx, config.sslconfig, &err) == REDIS_ERR && err) {\n            fprintf(stderr, \"Could not negotiate a TLS connection: %s\\n\", err);\n            goto cleanup;\n        }\n    }\n    if (config.auth == NULL)\n        return ctx;\n    if (config.user == NULL)\n        reply = (redisReply*)redisCommand(ctx,\"AUTH %s\", config.auth);\n    else\n        reply = (redisReply*)redisCommand(ctx,\"AUTH %s %s\", config.user, config.auth);\n    if (reply != NULL) {\n        if (reply->type == REDIS_REPLY_ERROR) {\n            if (hostsocket == NULL)\n                fprintf(stderr, \"Node %s:%d replied with error:\\n%s\\n\", ip, port, reply->str);\n            else\n                fprintf(stderr, \"Node %s replied with error:\\n%s\\n\", hostsocket, reply->str);\n            freeReplyObject(reply);\n            redisFree(ctx);\n            exit(1);\n        }\n        freeReplyObject(reply);\n        return ctx;\n    }\n    fprintf(stderr, \"ERROR: failed to fetch reply from \");\n    if (hostsocket == NULL)\n        fprintf(stderr, \"%s:%d\\n\", ip, port);\n    else\n        fprintf(stderr, \"%s\\n\", hostsocket);\ncleanup:\n    freeReplyObject(reply);\n    redisFree(ctx);\n    return NULL;\n}\n\nstatic redisConfig *getRedisConfig(const char *ip, int port,\n                                   const char *hostsocket)\n{\n    redisConfig *cfg = (redisConfig*)zcalloc(sizeof(*cfg));\n    if (!cfg) return NULL;\n    redisContext *c = NULL;\n    redisReply *reply = NULL, *sub_reply = NULL;\n    c = getRedisContext(ip, port, hostsocket);\n    if (c == NULL) {\n        freeRedisConfig(cfg);\n        return NULL;\n    }\n    redisAppendCommand(c, \"CONFIG GET %s\", \"save\");\n    redisAppendCommand(c, \"CONFIG GET %s\", \"appendonly\");\n    \n    void *r;\n    for (int i=0; i < 2; i++) {\n        int res = redisGetReply(c, &r);\n        if (reply) freeReplyObject(reply);\n        reply = res == REDIS_OK ? ((redisReply *) r) : NULL;\n        if (res != REDIS_OK || !r) goto fail;\n        if (reply->type == REDIS_REPLY_ERROR) {\n            fprintf(stderr, \"ERROR: %s\\n\", reply->str);\n            goto fail;\n        }\n        if (reply->type != REDIS_REPLY_ARRAY || reply->elements < 2) goto fail;\n        sub_reply = reply->element[1];\n        const char *value = sub_reply->str;\n        if (!value) value = \"\";\n        switch (i) {\n        case 0: cfg->save = sdsnew(value); break;\n        case 1: cfg->appendonly = sdsnew(value); break;\n        }\n    }\n    freeReplyObject(reply);\n    redisFree(c);\n    return cfg;\nfail:\n    fprintf(stderr, \"ERROR: failed to fetch CONFIG from \");\n    if (hostsocket == NULL) fprintf(stderr, \"%s:%d\\n\", ip, port);\n    else fprintf(stderr, \"%s\\n\", hostsocket);\n    int abort_test = 0;\n    if (reply && reply->type == REDIS_REPLY_ERROR &&\n        (!strncmp(reply->str,\"NOAUTH\",5) ||\n         !strncmp(reply->str,\"WRONGPASS\",9) ||\n         !strncmp(reply->str,\"NOPERM\",5)))\n        abort_test = 1;\n    freeReplyObject(reply);\n    redisFree(c);\n    freeRedisConfig(cfg);\n    if (abort_test) exit(1);\n    return NULL;\n}\nstatic void freeRedisConfig(redisConfig *cfg) {\n    if (cfg->save) sdsfree(cfg->save);\n    if (cfg->appendonly) sdsfree(cfg->appendonly);\n    zfree(cfg);\n}\n\nstatic void freeClient(client c) {\n    aeEventLoop *el = CLIENT_GET_EVENTLOOP(c);\n    listNode *ln;\n    aeDeleteFileEvent(el,c->context->fd,AE_WRITABLE);\n    aeDeleteFileEvent(el,c->context->fd,AE_READABLE);\n    if (c->thread_id >= 0) {\n        int requests_finished = 0;\n        atomicGet(config.requests_finished, requests_finished);\n        if (requests_finished >= config.requests) {\n            aeStop(el);\n        }\n    }\n    redisFree(c->context);\n    sdsfree(c->obuf);\n    zfree(c->randptr);\n    zfree(c->stagptr);\n    zfree(c);\n    if (config.num_threads) pthread_mutex_lock(&(config.liveclients_mutex));\n    config.liveclients--;\n    ln = listSearchKey(config.clients,c);\n    assert(ln != NULL);\n    listDelNode(config.clients,ln);\n    if (config.num_threads) pthread_mutex_unlock(&(config.liveclients_mutex));\n}\n\nstatic void freeAllClients(void) {\n    listNode *ln = config.clients->head, *next;\n\n    while(ln) {\n        next = ln->next;\n        freeClient((client)ln->value);\n        ln = next;\n    }\n}\n\nstatic void resetClient(client c) {\n    aeEventLoop *el = CLIENT_GET_EVENTLOOP(c);\n    aeDeleteFileEvent(el,c->context->fd,AE_WRITABLE);\n    aeDeleteFileEvent(el,c->context->fd,AE_READABLE);\n    aeCreateFileEvent(el,c->context->fd,AE_WRITABLE,writeHandler,c);\n    c->written = 0;\n    c->pending = config.pipeline;\n}\n\nstatic void randomizeClientKey(client c) {\n    size_t i;\n\n    for (i = 0; i < c->randlen; i++) {\n        char *p = c->randptr[i]+11;\n        size_t r = 0;\n        if (config.randomkeys_keyspacelen != 0)\n            r = random() % config.randomkeys_keyspacelen;\n        size_t j;\n\n        for (j = 0; j < 12; j++) {\n            *p = '0'+r%10;\n            r/=10;\n            p--;\n        }\n    }\n}\n\nstatic void setClusterKeyHashTag(client c) {\n    assert(c->thread_id >= 0);\n    clusterNode *node = c->cluster_node;\n    assert(node);\n    assert(node->current_slot_index < node->slots_count);\n    int is_updating_slots = 0;\n    atomicGet(config.is_updating_slots, is_updating_slots);\n    /* If updateClusterSlotsConfiguration is updating the slots array,\n     * call updateClusterSlotsConfiguration is order to block the thread\n     * since the mutex is locked. When the slots will be updated by the\n     * thread that's actually performing the update, the execution of\n     * updateClusterSlotsConfiguration won't actually do anything, since\n     * the updated_slots_count array will be already NULL. */\n    if (is_updating_slots) updateClusterSlotsConfiguration();\n    int slot = node->slots[node->current_slot_index];\n    const char *tag = crc16_slot_table[slot];\n    int taglen = strlen(tag);\n    size_t i;\n    for (i = 0; i < c->staglen; i++) {\n        char *p = c->stagptr[i] + 1;\n        p[0] = tag[0];\n        p[1] = (taglen >= 2 ? tag[1] : '}');\n        p[2] = (taglen == 3 ? tag[2] : '}');\n    }\n}\n\nstatic void clientDone(client c) {\n    int requests_finished = 0;\n    atomicGet(config.requests_finished, requests_finished);\n    if (requests_finished >= config.requests) {\n        freeClient(c);\n        if (!config.num_threads && config.el) aeStop(config.el);\n        return;\n    }\n    if (config.keepalive) {\n        resetClient(c);\n    } else {\n        if (config.num_threads) pthread_mutex_lock(&(config.liveclients_mutex));\n        config.liveclients--;\n        createMissingClients(c);\n        config.liveclients++;\n        if (config.num_threads)\n            pthread_mutex_unlock(&(config.liveclients_mutex));\n        freeClient(c);\n    }\n}\n\nstatic void readHandler(aeEventLoop *el, int fd, void *privdata, int mask) {\n    client c = (client)privdata;\n    void *reply = NULL;\n    UNUSED(el);\n    UNUSED(fd);\n    UNUSED(mask);\n\n    /* Calculate latency only for the first read event. This means that the\n     * server already sent the reply and we need to parse it. Parsing overhead\n     * is not part of the latency, so calculate it only once, here. */\n    if (c->latency < 0) c->latency = ustime()-(c->start);\n\n    if (redisBufferRead(c->context) != REDIS_OK) {\n        fprintf(stderr,\"Error: %s\\n\",c->context->errstr);\n        exit(1);\n    } else {\n        while(c->pending) {\n            if (redisGetReply(c->context,&reply) != REDIS_OK) {\n                fprintf(stderr,\"Error: %s\\n\",c->context->errstr);\n                exit(1);\n            }\n            if (reply != NULL) {\n                if (reply == (void*)REDIS_REPLY_ERROR) {\n                    fprintf(stderr,\"Unexpected error reply, exiting...\\n\");\n                    exit(1);\n                }\n                redisReply *r = (redisReply*)reply;\n                if (r->type == REDIS_REPLY_ERROR) {\n                    /* Try to update slots configuration if reply error is\n                    * MOVED/ASK/CLUSTERDOWN and the key(s) used by the command\n                    * contain(s) the slot hash tag.\n                    * If the error is not topology-update related then we\n                    * immediately exit to avoid false results. */\n                    if (c->cluster_node && c->staglen) {\n                        int fetch_slots = 0, do_wait = 0;\n                        if (!strncmp(r->str,\"MOVED\",5) || !strncmp(r->str,\"ASK\",3))\n                            fetch_slots = 1;\n                        else if (!strncmp(r->str,\"CLUSTERDOWN\",11)) {\n                            /* Usually the cluster is able to recover itself after\n                            * a CLUSTERDOWN error, so try to sleep one second\n                            * before requesting the new configuration. */\n                            fetch_slots = 1;\n                            do_wait = 1;\n                            printf(\"Error from server %s:%d: %s.\\n\",\n                                   c->cluster_node->ip,\n                                   c->cluster_node->port,\n                                   r->str);\n                        }\n                        if (do_wait) sleep(1);\n                        if (fetch_slots && !fetchClusterSlotsConfiguration(c))\n                            exit(1);\n                    } else {\n                        if (c->cluster_node) {\n                            printf(\"Error from server %s:%d: %s\\n\",\n                                c->cluster_node->ip,\n                                c->cluster_node->port,\n                                r->str);\n                        } else printf(\"Error from server: %s\\n\", r->str);\n                        exit(1);\n                    }\n                }\n\n                freeReplyObject(reply);\n                /* This is an OK for prefix commands such as auth and select.*/\n                if (c->prefix_pending > 0) {\n                    c->prefix_pending--;\n                    c->pending--;\n                    /* Discard prefix commands on first response.*/\n                    if (c->prefixlen > 0) {\n                        size_t j;\n                        sdsrange(c->obuf, c->prefixlen, -1);\n                        /* We also need to fix the pointers to the strings\n                        * we need to randomize. */\n                        for (j = 0; j < c->randlen; j++)\n                            c->randptr[j] -= c->prefixlen;\n                        /* Fix the pointers to the slot hash tags */\n                        for (j = 0; j < c->staglen; j++)\n                            c->stagptr[j] -= c->prefixlen;\n                        c->prefixlen = 0;\n                    }\n                    continue;\n                }\n                int requests_finished = 0;\n                atomicGetIncr(config.requests_finished, requests_finished, 1);\n                if (requests_finished < config.requests){\n                        if (config.num_threads == 0) {\n                            hdr_record_value(\n                            config.latency_histogram,  // Histogram to record to\n                            (long)c->latency<=CONFIG_LATENCY_HISTOGRAM_MAX_VALUE ? (long)c->latency : CONFIG_LATENCY_HISTOGRAM_MAX_VALUE);  // Value to record\n                            hdr_record_value(\n                            config.current_sec_latency_histogram,  // Histogram to record to\n                            (long)c->latency<=CONFIG_LATENCY_HISTOGRAM_INSTANT_MAX_VALUE ? (long)c->latency : CONFIG_LATENCY_HISTOGRAM_INSTANT_MAX_VALUE);  // Value to record\n                        } else {\n                            hdr_record_value_atomic(\n                            config.latency_histogram,  // Histogram to record to\n                            (long)c->latency<=CONFIG_LATENCY_HISTOGRAM_MAX_VALUE ? (long)c->latency : CONFIG_LATENCY_HISTOGRAM_MAX_VALUE);  // Value to record\n                            hdr_record_value_atomic(\n                            config.current_sec_latency_histogram,  // Histogram to record to\n                            (long)c->latency<=CONFIG_LATENCY_HISTOGRAM_INSTANT_MAX_VALUE ? (long)c->latency : CONFIG_LATENCY_HISTOGRAM_INSTANT_MAX_VALUE);  // Value to record\n                        }\n                }\n                c->pending--;\n                if (c->pending == 0) {\n                    clientDone(c);\n                    break;\n                }\n            } else {\n                break;\n            }\n        }\n    }\n}\n\nstatic void writeHandler(aeEventLoop *el, int fd, void *privdata, int mask) {\n    client c = (client)privdata;\n    UNUSED(el);\n    UNUSED(fd);\n    UNUSED(mask);\n\n    /* Initialize request when nothing was written. */\n    if (c->written == 0) {\n        /* Enforce upper bound to number of requests. */\n        int requests_issued = 0;\n        atomicGetIncr(config.requests_issued, requests_issued, config.pipeline);\n        if (requests_issued >= config.requests) {\n            return;\n        }\n\n        /* Really initialize: randomize keys and set start time. */\n        if (config.randomkeys) randomizeClientKey(c);\n        if (config.cluster_mode && c->staglen > 0) setClusterKeyHashTag(c);\n        atomicGet(config.slots_last_update, c->slots_last_update);\n        c->start = ustime();\n        c->latency = -1;\n    }\n    const ssize_t buflen = sdslen(c->obuf);\n    const ssize_t writeLen = buflen-c->written;\n    if (writeLen > 0) {\n        void *ptr = c->obuf+c->written;\n        while(1) {\n            /* Optimistically try to write before checking if the file descriptor\n             * is actually writable. At worst we get EAGAIN. */\n            const ssize_t nwritten = cliWriteConn(c->context,(const char*)ptr,writeLen);\n            if (nwritten != writeLen) {\n                if (nwritten == -1 && errno != EAGAIN) {\n                    if (errno != EPIPE)\n                        fprintf(stderr, \"Error writing to the server: %s\\n\", strerror(errno));\n                    freeClient(c);\n                    return;\n                }\n            } else {\n                aeDeleteFileEvent(el,c->context->fd,AE_WRITABLE);\n                aeCreateFileEvent(el,c->context->fd,AE_READABLE,readHandler,c);\n                return;\n            }\n        }\n    }\n}\n\n/* Create a benchmark client, configured to send the command passed as 'cmd' of\n * 'len' bytes.\n *\n * The command is copied N times in the client output buffer (that is reused\n * again and again to send the request to the server) accordingly to the configured\n * pipeline size.\n *\n * Also an initial SELECT command is prepended in order to make sure the right\n * database is selected, if needed. The initial SELECT will be discarded as soon\n * as the first reply is received.\n *\n * To create a client from scratch, the 'from' pointer is set to NULL. If instead\n * we want to create a client using another client as reference, the 'from' pointer\n * points to the client to use as reference. In such a case the following\n * information is take from the 'from' client:\n *\n * 1) The command line to use.\n * 2) The offsets of the __rand_int__ elements inside the command line, used\n *    for arguments randomization.\n *\n * Even when cloning another client, prefix commands are applied if needed.*/\nstatic client createClient(const char *cmd, size_t len, client from, int thread_id) {\n    int j;\n    int is_cluster_client = (config.cluster_mode && thread_id >= 0);\n    client c = (client)zmalloc(sizeof(struct _client), MALLOC_LOCAL);\n\n    const char *ip = NULL;\n    int port = 0;\n    c->cluster_node = NULL;\n    if (config.hostsocket == NULL || is_cluster_client) {\n        if (!is_cluster_client) {\n            ip = config.hostip;\n            port = config.hostport;\n        } else {\n            int node_idx = 0;\n            if (config.num_threads < config.cluster_node_count)\n                node_idx = config.liveclients % config.cluster_node_count;\n            else\n                node_idx = thread_id % config.cluster_node_count;\n            clusterNode *node = config.cluster_nodes[node_idx];\n            assert(node != NULL);\n            ip = (const char *) node->ip;\n            port = node->port;\n            c->cluster_node = node;\n        }\n        c->context = redisConnectNonBlock(ip,port);\n    } else {\n        c->context = redisConnectUnixNonBlock(config.hostsocket);\n    }\n    if (c->context->err) {\n        fprintf(stderr,\"Could not connect to Redis at \");\n        if (config.hostsocket == NULL || is_cluster_client)\n            fprintf(stderr,\"%s:%d: %s\\n\",ip,port,c->context->errstr);\n        else\n            fprintf(stderr,\"%s: %s\\n\",config.hostsocket,c->context->errstr);\n        exit(1);\n    }\n    if (config.tls==1) {\n        const char *err = NULL;\n        if (cliSecureConnection(c->context, config.sslconfig, &err) == REDIS_ERR && err) {\n            fprintf(stderr, \"Could not negotiate a TLS connection: %s\\n\", err);\n            exit(1);\n        }\n    }\n    c->thread_id = thread_id;\n    /* Suppress hiredis cleanup of unused buffers for max speed. */\n    c->context->reader->maxbuf = 0;\n\n    /* Build the request buffer:\n     * Queue N requests accordingly to the pipeline size, or simply clone\n     * the example client buffer. */\n    c->obuf = sdsempty();\n    /* Prefix the request buffer with AUTH and/or SELECT commands, if applicable.\n     * These commands are discarded after the first response, so if the client is\n     * reused the commands will not be used again. */\n    c->prefix_pending = 0;\n    if (config.auth) {\n        char *buf = NULL;\n        int len;\n        if (config.user == NULL)\n            len = redisFormatCommand(&buf, \"AUTH %s\", config.auth);\n        else\n            len = redisFormatCommand(&buf, \"AUTH %s %s\",\n                                     config.user, config.auth);\n        c->obuf = sdscatlen(c->obuf, buf, len);\n        free(buf);\n        c->prefix_pending++;\n    }\n\n    if (config.enable_tracking) {\n        char *buf = NULL;\n        int len = redisFormatCommand(&buf, \"CLIENT TRACKING on\");\n        c->obuf = sdscatlen(c->obuf, buf, len);\n        free(buf);\n        c->prefix_pending++;\n    }\n\n    /* If a DB number different than zero is selected, prefix our request\n     * buffer with the SELECT command, that will be discarded the first\n     * time the replies are received, so if the client is reused the\n     * SELECT command will not be used again. */\n    if (config.dbnum != 0 && !is_cluster_client) {\n        c->obuf = sdscatprintf(c->obuf,\"*2\\r\\n$6\\r\\nSELECT\\r\\n$%d\\r\\n%s\\r\\n\",\n            (int)sdslen(config.dbnumstr),config.dbnumstr);\n        c->prefix_pending++;\n    }\n    c->prefixlen = sdslen(c->obuf);\n    /* Append the request itself. */\n    if (from) {\n        c->obuf = sdscatlen(c->obuf,\n            from->obuf+from->prefixlen,\n            sdslen(from->obuf)-from->prefixlen);\n    } else {\n        for (j = 0; j < config.pipeline; j++)\n            c->obuf = sdscatlen(c->obuf,cmd,len);\n    }\n\n    c->written = 0;\n    c->pending = config.pipeline+c->prefix_pending;\n    c->randptr = NULL;\n    c->randlen = 0;\n    c->stagptr = NULL;\n    c->staglen = 0;\n\n    /* Find substrings in the output buffer that need to be randomized. */\n    if (config.randomkeys) {\n        if (from) {\n            c->randlen = from->randlen;\n            c->randfree = 0;\n            c->randptr = (char**)zmalloc(sizeof(char*)*c->randlen, MALLOC_LOCAL);\n            /* copy the offsets. */\n            for (j = 0; j < (int)c->randlen; j++) {\n                c->randptr[j] = c->obuf + (from->randptr[j]-from->obuf);\n                /* Adjust for the different select prefix length. */\n                c->randptr[j] += c->prefixlen - from->prefixlen;\n            }\n        } else {\n            char *p = c->obuf;\n\n            c->randlen = 0;\n            c->randfree = RANDPTR_INITIAL_SIZE;\n            c->randptr = (char**)zmalloc(sizeof(char*)*c->randfree, MALLOC_LOCAL);\n            while ((p = strstr(p,\"__rand_int__\")) != NULL) {\n                if (c->randfree == 0) {\n                    c->randptr = (char**)zrealloc(c->randptr,sizeof(char*)*c->randlen*2, MALLOC_LOCAL);\n                    c->randfree += c->randlen;\n                }\n                c->randptr[c->randlen++] = p;\n                c->randfree--;\n                p += 12; /* 12 is strlen(\"__rand_int__). */\n            }\n        }\n    }\n    /* If cluster mode is enabled, set slot hashtags pointers. */\n    if (config.cluster_mode) {\n        if (from) {\n            c->staglen = from->staglen;\n            c->stagfree = 0;\n            c->stagptr = (char**)zmalloc(sizeof(char*)*c->staglen, MALLOC_LOCAL);\n            /* copy the offsets. */\n            for (j = 0; j < (int)c->staglen; j++) {\n                c->stagptr[j] = c->obuf + (from->stagptr[j]-from->obuf);\n                /* Adjust for the different select prefix length. */\n                c->stagptr[j] += c->prefixlen - from->prefixlen;\n            }\n        } else {\n            char *p = c->obuf;\n\n            c->staglen = 0;\n            c->stagfree = RANDPTR_INITIAL_SIZE;\n            c->stagptr = (char**)zmalloc(sizeof(char*)*c->stagfree, MALLOC_LOCAL);\n            while ((p = strstr(p,\"{tag}\")) != NULL) {\n                if (c->stagfree == 0) {\n                    c->stagptr = (char**)zrealloc(c->stagptr,\n                                          sizeof(char*) * c->staglen*2, MALLOC_LOCAL);\n                    c->stagfree += c->staglen;\n                }\n                c->stagptr[c->staglen++] = p;\n                c->stagfree--;\n                p += 5; /* 5 is strlen(\"{tag}\"). */\n            }\n        }\n    }\n    aeEventLoop *el = NULL;\n    if (thread_id < 0) el = config.el;\n    else {\n        benchmarkThread *thread = config.threads[thread_id];\n        el = thread->el;\n    }\n    if (config.idlemode == 0)\n        aeCreateFileEvent(el,c->context->fd,AE_WRITABLE,writeHandler,c);\n    listAddNodeTail(config.clients,c);\n    atomicIncr(config.liveclients, 1);\n    atomicGet(config.slots_last_update, c->slots_last_update);\n    return c;\n}\n\nstatic void createMissingClients(client c) {\n    int n = 0;\n    while(config.liveclients < config.numclients) {\n        int thread_id = -1;\n        if (config.num_threads)\n            thread_id = config.liveclients % config.num_threads;\n        createClient(NULL,0,c,thread_id);\n\n        /* Listen backlog is quite limited on most systems */\n        if (++n > 64) {\n            usleep(50000);\n            n = 0;\n        }\n    }\n}\n\nextern \"C\" void asyncFreeDictTable(dictEntry **de) {\n    zfree(de);\n}\n\nstatic void showLatencyReport(void) {\n\n    const float reqpersec = (float)config.requests_finished/((float)config.totlatency/1000.0f);\n    const float p0 = ((float) hdr_min(config.latency_histogram))/1000.0f;\n    const float p50 = hdr_value_at_percentile(config.latency_histogram, 50.0 )/1000.0f;\n    const float p95 = hdr_value_at_percentile(config.latency_histogram, 95.0 )/1000.0f;\n    const float p99 = hdr_value_at_percentile(config.latency_histogram, 99.0 )/1000.0f;\n    const float p100 = ((float) hdr_max(config.latency_histogram))/1000.0f;\n    const float avg = hdr_mean(config.latency_histogram)/1000.0f;\n\n    if (!config.quiet && !config.csv) {\n        printf(\"%*s\\r\", config.last_printed_bytes, \" \"); // ensure there is a clean line\n        printf(\"====== %s ======\\n\", config.title);\n        printf(\"  %d requests completed in %.2f seconds\\n\", config.requests_finished,\n            (float)config.totlatency/1000);\n        printf(\"  %d parallel clients\\n\", config.numclients);\n        printf(\"  %d bytes payload\\n\", config.datasize);\n        printf(\"  keep alive: %d\\n\", config.keepalive);\n        if (config.cluster_mode) {\n            printf(\"  cluster mode: yes (%d masters)\\n\",\n                   config.cluster_node_count);\n            int m ;\n            for (m = 0; m < config.cluster_node_count; m++) {\n                clusterNode *node =  config.cluster_nodes[m];\n                redisConfig *cfg = node->redis_config;\n                if (cfg == NULL) continue;\n                printf(\"  node [%d] configuration:\\n\",m );\n                printf(\"    save: %s\\n\",\n                    sdslen(cfg->save) ? cfg->save : \"NONE\");\n                printf(\"    appendonly: %s\\n\", cfg->appendonly);\n            }\n        } else {\n            if (config.redis_config) {\n                printf(\"  host configuration \\\"save\\\": %s\\n\",\n                       config.redis_config->save);\n                printf(\"  host configuration \\\"appendonly\\\": %s\\n\",\n                       config.redis_config->appendonly);\n            }\n        }\n        printf(\"  multi-thread: %s\\n\", (config.num_threads ? \"yes\" : \"no\"));\n        if (config.num_threads)\n            printf(\"  threads: %d\\n\", config.num_threads);\n\n        printf(\"\\n\");\n        printf(\"Latency by percentile distribution:\\n\");\n        struct hdr_iter iter;\n        long long previous_cumulative_count = -1;\n        const long long total_count = config.latency_histogram->total_count;\n        hdr_iter_percentile_init(&iter, config.latency_histogram, 1);\n        struct hdr_iter_percentiles *percentiles = &iter.specifics.percentiles;\n        while (hdr_iter_next(&iter))\n        {\n            const double value = iter.highest_equivalent_value / 1000.0f;\n            const double percentile = percentiles->percentile;\n            const long long cumulative_count = iter.cumulative_count;\n            if( previous_cumulative_count != cumulative_count || cumulative_count == total_count ){\n                printf(\"%3.3f%% <= %.3f milliseconds (cumulative count %lld)\\n\", percentile, value, cumulative_count);\n            }\n            previous_cumulative_count = cumulative_count;\n        }\n        printf(\"\\n\");\n        printf(\"Cumulative distribution of latencies:\\n\");\n        previous_cumulative_count = -1;\n        hdr_iter_linear_init(&iter, config.latency_histogram, 100);\n        while (hdr_iter_next(&iter))\n        {\n            const double value = iter.highest_equivalent_value / 1000.0f;\n            const long long cumulative_count = iter.cumulative_count;\n            const double percentile = ((double)cumulative_count/(double)total_count)*100.0;\n            if( previous_cumulative_count != cumulative_count || cumulative_count == total_count ){\n                printf(\"%3.3f%% <= %.3f milliseconds (cumulative count %lld)\\n\", percentile, value, cumulative_count);\n            }\n            /* After the 2 milliseconds latency to have percentages split\n             * by decimals will just add a lot of noise to the output. */\n            if(iter.highest_equivalent_value > 2000){\n                hdr_iter_linear_set_value_units_per_bucket(&iter,1000);\n            }\n            previous_cumulative_count = cumulative_count;\n        }\n        printf(\"\\n\");\n        printf(\"Summary:\\n\");\n        printf(\"  throughput summary: %.2f requests per second\\n\", reqpersec);\n        printf(\"  latency summary (msec):\\n\");\n        printf(\"    %9s %9s %9s %9s %9s %9s\\n\", \"avg\", \"min\", \"p50\", \"p95\", \"p99\", \"max\");\n        printf(\"    %9.3f %9.3f %9.3f %9.3f %9.3f %9.3f\\n\", avg, p0, p50, p95, p99, p100);\n    } else if (config.csv) {\n        printf(\"\\\"%s\\\",\\\"%.2f\\\",\\\"%.3f\\\",\\\"%.3f\\\",\\\"%.3f\\\",\\\"%.3f\\\",\\\"%.3f\\\",\\\"%.3f\\\"\\n\", config.title, reqpersec, avg, p0, p50, p95, p99, p100);\n    } else {\n        printf(\"%*s\\r\", config.last_printed_bytes, \" \"); // ensure there is a clean line\n        printf(\"%s: %.2f requests per second, p50=%.3f msec\\n\", config.title, reqpersec, p50);\n    }\n}\n\nstatic void initBenchmarkThreads() {\n    int i;\n    if (config.threads) freeBenchmarkThreads();\n    config.threads = (benchmarkThread**)zmalloc(config.num_threads * sizeof(benchmarkThread*), MALLOC_LOCAL);\n    for (i = 0; i < config.num_threads; i++) {\n        benchmarkThread *thread = createBenchmarkThread(i);\n        config.threads[i] = thread;\n    }\n}\n\nstatic void startBenchmarkThreads() {\n    int i;\n    for (i = 0; i < config.num_threads; i++) {\n        benchmarkThread *t = config.threads[i];\n        if (pthread_create(&(t->thread), NULL, execBenchmarkThread, t)){\n            fprintf(stderr, \"FATAL: Failed to start thread %d.\\n\", i);\n            exit(1);\n        }\n    }\n    for (i = 0; i < config.num_threads; i++)\n        pthread_join(config.threads[i]->thread, NULL);\n}\n\nstatic void benchmark(const char *title, const char *cmd, int len) {\n    client c;\n\n    config.title = title;\n    config.requests_issued = 0;\n    config.requests_finished = 0;\n    config.previous_requests_finished = 0;\n    config.last_printed_bytes = 0;\n    hdr_init(\n        CONFIG_LATENCY_HISTOGRAM_MIN_VALUE,  // Minimum value\n        CONFIG_LATENCY_HISTOGRAM_MAX_VALUE,  // Maximum value\n        config.precision,  // Number of significant figures\n        &config.latency_histogram);  // Pointer to initialise\n    hdr_init(\n        CONFIG_LATENCY_HISTOGRAM_MIN_VALUE,  // Minimum value\n        CONFIG_LATENCY_HISTOGRAM_INSTANT_MAX_VALUE,  // Maximum value\n        config.precision,  // Number of significant figures\n        &config.current_sec_latency_histogram);  // Pointer to initialise\n\n    if (config.num_threads) initBenchmarkThreads();\n\n    int thread_id = config.num_threads > 0 ? 0 : -1;\n    c = createClient(cmd,len,NULL,thread_id);\n    createMissingClients(c);\n\n    config.start = mstime();\n    if (!config.num_threads) {\n        aeThreadOnline();\n        aeMain(config.el);\n        aeThreadOffline();\n    }\n    else startBenchmarkThreads();\n    config.totlatency = mstime()-config.start;\n\n    showLatencyReport();\n    freeAllClients();\n    if (config.threads) freeBenchmarkThreads();\n    if (config.current_sec_latency_histogram) hdr_close(config.current_sec_latency_histogram);\n    if (config.latency_histogram) hdr_close(config.latency_histogram);\n\n}\n\n/* Thread functions. */\n\nstatic benchmarkThread *createBenchmarkThread(int index) {\n    benchmarkThread *thread = (benchmarkThread*)zmalloc(sizeof(*thread), MALLOC_LOCAL);\n    if (thread == NULL) return NULL;\n    thread->index = index;\n    thread->el = aeCreateEventLoop(1024*10);\n    aeCreateTimeEvent(thread->el,1,showThroughput,NULL,NULL);\n    return thread;\n}\n\nstatic void freeBenchmarkThread(benchmarkThread *thread) {\n    if (thread->el) aeDeleteEventLoop(thread->el);\n    zfree(thread);\n}\n\nstatic void freeBenchmarkThreads() {\n    int i = 0;\n    for (; i < config.num_threads; i++) {\n        benchmarkThread *thread = config.threads[i];\n        if (thread) freeBenchmarkThread(thread);\n    }\n    zfree(config.threads);\n    config.threads = NULL;\n}\n\nstatic void *execBenchmarkThread(void *ptr) {\n    benchmarkThread *thread = (benchmarkThread *) ptr;\n    aeThreadOnline();\n    aeMain(thread->el);\n    aeThreadOffline();\n    return NULL;\n}\n\n/* Cluster helper functions. */\n\nstatic clusterNode *createClusterNode(char *ip, int port) {\n    clusterNode *node = (clusterNode*)zmalloc(sizeof(*node), MALLOC_LOCAL);\n    if (!node) return NULL;\n    node->ip = ip;\n    node->port = port;\n    node->name = NULL;\n    node->flags = 0;\n    node->replicate = NULL;\n    node->replicas_count = 0;\n    node->slots = (int*)zmalloc(CLUSTER_SLOTS * sizeof(int), MALLOC_LOCAL);\n    node->slots_count = 0;\n    node->current_slot_index = 0;\n    node->updated_slots = NULL;\n    node->updated_slots_count = 0;\n    node->migrating = NULL;\n    node->importing = NULL;\n    node->migrating_count = 0;\n    node->importing_count = 0;\n    node->redis_config = NULL;\n    return node;\n}\n\nstatic void freeClusterNode(clusterNode *node) {\n    int i;\n    if (node->name) sdsfree(node->name);\n    if (node->replicate) sdsfree(node->replicate);\n    if (node->migrating != NULL) {\n        for (i = 0; i < node->migrating_count; i++) sdsfree(node->migrating[i]);\n        zfree(node->migrating);\n    }\n    if (node->importing != NULL) {\n        for (i = 0; i < node->importing_count; i++) sdsfree(node->importing[i]);\n        zfree(node->importing);\n    }\n    /* If the node is not the reference node, that uses the address from\n     * config.hostip and config.hostport, then the node ip has been\n     * allocated by fetchClusterConfiguration, so it must be freed. */\n    if (node->ip && strcmp(node->ip, config.hostip) != 0) sdsfree(node->ip);\n    if (node->redis_config != NULL) freeRedisConfig(node->redis_config);\n    zfree(node->slots);\n    zfree(node);\n}\n\nstatic void freeClusterNodes() {\n    int i = 0;\n    for (; i < config.cluster_node_count; i++) {\n        clusterNode *n = config.cluster_nodes[i];\n        if (n) freeClusterNode(n);\n    }\n    zfree(config.cluster_nodes);\n    config.cluster_nodes = NULL;\n}\n\nstatic clusterNode **addClusterNode(clusterNode *node) {\n    int count = config.cluster_node_count + 1;\n    config.cluster_nodes = (clusterNode**)zrealloc(config.cluster_nodes,\n                                    count * sizeof(*node), MALLOC_LOCAL);\n    if (!config.cluster_nodes) return NULL;\n    config.cluster_nodes[config.cluster_node_count++] = node;\n    return config.cluster_nodes;\n}\n\nstatic int fetchClusterConfiguration() {\n    int success = 1;\n    redisContext *ctx = NULL;\n    redisReply *reply =  NULL;\n    char *lines, *p, *line;\n    ctx = getRedisContext(config.hostip, config.hostport, config.hostsocket);\n    if (ctx == NULL) {\n        exit(1);\n    }\n    clusterNode *firstNode = createClusterNode((char *) config.hostip,\n                                               config.hostport);\n    if (!firstNode) {success = 0; goto cleanup;}\n    reply = (redisReply*)redisCommand(ctx, \"CLUSTER NODES\");\n    success = (reply != NULL);\n    if (!success) goto cleanup;\n    success = (reply->type != REDIS_REPLY_ERROR);\n    if (!success) {\n        if (config.hostsocket == NULL) {\n            fprintf(stderr, \"Cluster node %s:%d replied with error:\\n%s\\n\",\n                    config.hostip, config.hostport, reply->str);\n        } else {\n            fprintf(stderr, \"Cluster node %s replied with error:\\n%s\\n\",\n                    config.hostsocket, reply->str);\n        }\n        goto cleanup;\n    }\n    lines = reply->str;\n    while ((p = strstr(lines, \"\\n\")) != NULL) {\n        *p = '\\0';\n        line = lines;\n        lines = p + 1;\n        char *name = NULL, *addr = NULL, *flags = NULL, *master_id = NULL;\n        int i = 0;\n        while ((p = strchr(line, ' ')) != NULL) {\n            *p = '\\0';\n            char *token = line;\n            line = p + 1;\n            switch(i++){\n            case 0: name = token; break;\n            case 1: addr = token; break;\n            case 2: flags = token; break;\n            case 3: master_id = token; break;\n            }\n            if (i == 8) break; // Slots\n        }\n        if (!flags) {\n            fprintf(stderr, \"Invalid CLUSTER NODES reply: missing flags.\\n\");\n            success = 0;\n            goto cleanup;\n        }\n        int myself = (strstr(flags, \"myself\") != NULL);\n        int is_replica = (strstr(flags, \"slave\") != NULL ||\n                         (master_id != NULL && master_id[0] != '-'));\n        if (is_replica) continue;\n        if (addr == NULL) {\n            fprintf(stderr, \"Invalid CLUSTER NODES reply: missing addr.\\n\");\n            success = 0;\n            goto cleanup;\n        }\n        clusterNode *node = NULL;\n        char *ip = NULL;\n        int port = 0;\n        char *paddr = strchr(addr, ':');\n        if (paddr != NULL) {\n            *paddr = '\\0';\n            ip = addr;\n            addr = paddr + 1;\n            /* If internal bus is specified, then just drop it. */\n            if ((paddr = strchr(addr, '@')) != NULL) *paddr = '\\0';\n            port = atoi(addr);\n        }\n        if (myself) {\n            node = firstNode;\n            if (ip != NULL && strcmp(node->ip, ip) != 0) {\n                node->ip = sdsnew(ip);\n                node->port = port;\n            }\n        } else {\n            node = createClusterNode(sdsnew(ip), port);\n        }\n        if (node == NULL) {\n            success = 0;\n            goto cleanup;\n        }\n        if (name != NULL) node->name = sdsnew(name);\n        if (i == 8) {\n            int remaining = strlen(line);\n            while (remaining > 0) {\n                p = strchr(line, ' ');\n                if (p == NULL) p = line + remaining;\n                remaining -= (p - line);\n\n                char *slotsdef = line;\n                *p = '\\0';\n                if (remaining) {\n                    line = p + 1;\n                    remaining--;\n                } else line = p;\n                char *dash = NULL;\n                if (slotsdef[0] == '[') {\n                    slotsdef++;\n                    if ((p = strstr(slotsdef, \"->-\"))) { // Migrating\n                        *p = '\\0';\n                        p += 3;\n                        char *closing_bracket = strchr(p, ']');\n                        if (closing_bracket) *closing_bracket = '\\0';\n                        sds slot = sdsnew(slotsdef);\n                        sds dst = sdsnew(p);\n                        node->migrating_count += 2;\n                        node->migrating =\n                            (char**)zrealloc(node->migrating,\n                                (node->migrating_count * sizeof(sds)), MALLOC_LOCAL);\n                        node->migrating[node->migrating_count - 2] =\n                            slot;\n                        node->migrating[node->migrating_count - 1] =\n                            dst;\n                    }  else if ((p = strstr(slotsdef, \"-<-\"))) {//Importing\n                        *p = '\\0';\n                        p += 3;\n                        char *closing_bracket = strchr(p, ']');\n                        if (closing_bracket) *closing_bracket = '\\0';\n                        sds slot = sdsnew(slotsdef);\n                        sds src = sdsnew(p);\n                        node->importing_count += 2;\n                        node->importing = (char**)zrealloc(node->importing,\n                            (node->importing_count * sizeof(sds)), MALLOC_LOCAL);\n                        node->importing[node->importing_count - 2] =\n                            slot;\n                        node->importing[node->importing_count - 1] =\n                            src;\n                    }\n                } else if ((dash = strchr(slotsdef, '-')) != NULL) {\n                    p = dash;\n                    int start, stop;\n                    *p = '\\0';\n                    start = atoi(slotsdef);\n                    stop = atoi(p + 1);\n                    while (start <= stop) {\n                        int slot = start++;\n                        node->slots[node->slots_count++] = slot;\n                    }\n                } else if (p > slotsdef) {\n                    int slot = atoi(slotsdef);\n                    node->slots[node->slots_count++] = slot;\n                }\n            }\n        }\n        if (node->slots_count == 0) {\n            printf(\"WARNING: master node %s:%d has no slots, skipping...\\n\",\n                   node->ip, node->port);\n            continue;\n        }\n        if (!addClusterNode(node)) {\n            success = 0;\n            goto cleanup;\n        }\n    }\ncleanup:\n    if (ctx) redisFree(ctx);\n    if (!success) {\n        if (config.cluster_nodes) freeClusterNodes();\n    }\n    if (reply) freeReplyObject(reply);\n    return success;\n}\n\n/* Request the current cluster slots configuration by calling CLUSTER SLOTS\n * and atomically update the slots after a successful reply. */\nstatic int fetchClusterSlotsConfiguration(client c) {\n    UNUSED(c);\n    int success = 1, is_fetching_slots = 0, last_update = 0;\n    size_t i;\n    atomicGet(config.slots_last_update, last_update);\n    if (c->slots_last_update < last_update) {\n        c->slots_last_update = last_update;\n        return -1;\n    }\n    redisReply *reply = NULL;\n    atomicGetIncr(config.is_fetching_slots, is_fetching_slots, 1);\n    if (is_fetching_slots) return -1; //TODO: use other codes || errno ?\n    atomicSet(config.is_fetching_slots, 1);\n    printf(\"WARNING: Cluster slots configuration changed, fetching new one...\\n\");\n    const char *errmsg = \"Failed to update cluster slots configuration\";\n    static dictType dtype = {\n        dictSdsHash,               /* hash function */\n        NULL,                      /* key dup */\n        NULL,                      /* val dup */\n        dictSdsKeyCompare,         /* key compare */\n        NULL,                      /* key destructor */\n        NULL,                      /* val destructor */\n        NULL                       /* allow to expand */\n    };\n    /* printf(\"[%d] fetchClusterSlotsConfiguration\\n\", c->thread_id); */\n    dict *masters = dictCreate(&dtype, NULL);\n    redisContext *ctx = NULL;\n    for (i = 0; i < (size_t) config.cluster_node_count; i++) {\n        clusterNode *node = config.cluster_nodes[i];\n        assert(node->ip != NULL);\n        assert(node->name != NULL);\n        assert(node->port);\n        /* Use first node as entry point to connect to. */\n        if (ctx == NULL) {\n            ctx = getRedisContext(node->ip, node->port, NULL);\n            if (!ctx) {\n                success = 0;\n                goto cleanup;\n            }\n        }\n        if (node->updated_slots != NULL)\n            zfree(node->updated_slots);\n        node->updated_slots = NULL;\n        node->updated_slots_count = 0;\n        dictReplace(masters, node->name, node) ;\n    }\n    reply = (redisReply*)redisCommand(ctx, \"CLUSTER SLOTS\");\n    if (reply == NULL || reply->type == REDIS_REPLY_ERROR) {\n        success = 0;\n        if (reply)\n            fprintf(stderr,\"%s\\nCLUSTER SLOTS ERROR: %s\\n\",errmsg,reply->str);\n        goto cleanup;\n    }\n    assert(reply->type == REDIS_REPLY_ARRAY);\n    for (i = 0; i < reply->elements; i++) {\n        redisReply *r = reply->element[i];\n        assert(r->type == REDIS_REPLY_ARRAY);\n        assert(r->elements >= 3);\n        int from, to, slot;\n        from = r->element[0]->integer;\n        to = r->element[1]->integer;\n        redisReply *nr =  r->element[2];\n        assert(nr->type == REDIS_REPLY_ARRAY && nr->elements >= 3);\n        assert(nr->element[2]->str != NULL);\n        sds name =  sdsnew(nr->element[2]->str);\n        dictEntry *entry = dictFind(masters, name);\n        if (entry == NULL) {\n            success = 0;\n            fprintf(stderr, \"%s: could not find node with ID %s in current \"\n                            \"configuration.\\n\", errmsg, name);\n            if (name) sdsfree(name);\n            goto cleanup;\n        }\n        sdsfree(name);\n        clusterNode *node = (clusterNode*)dictGetVal(entry);\n        if (node->updated_slots == NULL)\n            node->updated_slots = (int*)zcalloc(CLUSTER_SLOTS * sizeof(int), MALLOC_LOCAL);\n        for (slot = from; slot <= to; slot++)\n            node->updated_slots[node->updated_slots_count++] = slot;\n    }\n    updateClusterSlotsConfiguration();\ncleanup:\n    freeReplyObject(reply);\n    redisFree(ctx);\n    dictRelease(masters);\n    atomicSet(config.is_fetching_slots, 0);\n    return success;\n}\n\n/* Atomically update the new slots configuration. */\nstatic void updateClusterSlotsConfiguration() {\n    pthread_mutex_lock(&config.is_updating_slots_mutex);\n    atomicSet(config.is_updating_slots, 1);\n    int i;\n    for (i = 0; i < config.cluster_node_count; i++) {\n        clusterNode *node = config.cluster_nodes[i];\n        if (node->updated_slots != NULL) {\n            int *oldslots = node->slots;\n            node->slots = node->updated_slots;\n            node->slots_count = node->updated_slots_count;\n            node->current_slot_index = 0;\n            node->updated_slots = NULL;\n            node->updated_slots_count = 0;\n            zfree(oldslots);\n        }\n    }\n    atomicSet(config.is_updating_slots, 0);\n    atomicIncr(config.slots_last_update, 1);\n    pthread_mutex_unlock(&config.is_updating_slots_mutex);\n}\n\n/* Generate random data for redis benchmark. See #7196. */\nstatic void genBenchmarkRandomData(char *data, int count) {\n    static uint32_t state = 1234;\n    int i = 0;\n\n    while (count--) {\n        state = (state*1103515245+12345);\n        data[i++] = '0'+((state>>16)&63);\n    }\n}\n\n/* Returns number of consumed options. */\nint parseOptions(int argc, const char **argv) {\n    int i;\n    int lastarg;\n    int exit_status = 1;\n\n    for (i = 1; i < argc; i++) {\n        lastarg = (i == (argc-1));\n\n        if (!strcmp(argv[i],\"-c\")) {\n            if (lastarg) goto invalid;\n            config.numclients = atoi(argv[++i]);\n        } else if (!strcmp(argv[i],\"-v\") || !strcmp(argv[i], \"--version\")) {\n            sds version = benchmarkVersion();\n            printf(\"redis-benchmark %s\\n\", version);\n            sdsfree(version);\n            exit(0);\n        } else if (!strcmp(argv[i],\"-n\")) {\n            if (lastarg) goto invalid;\n            config.requests = atoi(argv[++i]);\n        } else if (!strcmp(argv[i],\"-k\")) {\n            if (lastarg) goto invalid;\n            config.keepalive = atoi(argv[++i]);\n        } else if (!strcmp(argv[i],\"-h\")) {\n            if (lastarg) goto invalid;\n            config.hostip = strdup(argv[++i]);\n        } else if (!strcmp(argv[i],\"-p\")) {\n            if (lastarg) goto invalid;\n            config.hostport = atoi(argv[++i]);\n        } else if (!strcmp(argv[i],\"-s\")) {\n            if (lastarg) goto invalid;\n            config.hostsocket = strdup(argv[++i]);\n        } else if (!strcmp(argv[i],\"-a\") ) {\n            if (lastarg) goto invalid;\n            config.auth = strdup(argv[++i]);\n        } else if (!strcmp(argv[i],\"--user\")) {\n            if (lastarg) goto invalid;\n            config.user = argv[++i];\n        } else if (!strcmp(argv[i],\"-d\")) {\n            if (lastarg) goto invalid;\n            config.datasize = atoi(argv[++i]);\n            if (config.datasize < 1) config.datasize=1;\n            if (config.datasize > 1024*1024*1024) config.datasize = 1024*1024*1024;\n        } else if (!strcmp(argv[i],\"-P\")) {\n            if (lastarg) goto invalid;\n            config.pipeline = atoi(argv[++i]);\n            if (config.pipeline <= 0) config.pipeline=1;\n        } else if (!strcmp(argv[i],\"-r\")) {\n            if (lastarg) goto invalid;\n            const char *next = argv[++i], *p = next;\n            if (*p == '-') {\n                p++;\n                if (*p < '0' || *p > '9') goto invalid;\n            }\n            config.randomkeys = 1;\n            config.randomkeys_keyspacelen = atoi(next);\n            if (config.randomkeys_keyspacelen < 0)\n                config.randomkeys_keyspacelen = 0;\n        } else if (!strcmp(argv[i],\"-q\")) {\n            config.quiet = 1;\n        } else if (!strcmp(argv[i],\"--csv\")) {\n            config.csv = 1;\n        } else if (!strcmp(argv[i],\"-l\")) {\n            config.loop = 1;\n        } else if (!strcmp(argv[i],\"-I\")) {\n            config.idlemode = 1;\n        } else if (!strcmp(argv[i],\"-e\")) {\n            printf(\"WARNING: -e option has been deprecated. \"\n                   \"We now immediatly exit on error to avoid false results.\\n\");\n        } else if (!strcmp(argv[i],\"-t\")) {\n            if (lastarg) goto invalid;\n            /* We get the list of tests to run as a string in the form\n             * get,set,lrange,...,test_N. Then we add a comma before and\n             * after the string in order to make sure that searching\n             * for \",testname,\" will always get a match if the test is\n             * enabled. */\n            config.tests = sdsnew(\",\");\n            config.tests = sdscat(config.tests,(char*)argv[++i]);\n            config.tests = sdscat(config.tests,\",\");\n            sdstolower(config.tests);\n        } else if (!strcmp(argv[i],\"--dbnum\")) {\n            if (lastarg) goto invalid;\n            config.dbnum = atoi(argv[++i]);\n            config.dbnumstr = sdsfromlonglong(config.dbnum);\n        } else if (!strcmp(argv[i],\"--precision\")) {\n            if (lastarg) goto invalid;\n            config.precision = atoi(argv[++i]);\n            if (config.precision < 0) config.precision = DEFAULT_LATENCY_PRECISION;\n            if (config.precision > MAX_LATENCY_PRECISION) config.precision = MAX_LATENCY_PRECISION;\n        } else if (!strcmp(argv[i],\"--threads\")) {\n             if (lastarg) goto invalid;\n             config.num_threads = atoi(argv[++i]);\n             if (config.num_threads > MAX_THREADS) {\n                printf(\"WARNING: too many threads, limiting threads to %d.\\n\",\n                       MAX_THREADS);\n                config.num_threads = MAX_THREADS;\n             } else if (config.num_threads < 0) config.num_threads = 0;\n        } else if (!strcmp(argv[i],\"--cluster\")) {\n            config.cluster_mode = 1;\n        } else if (!strcmp(argv[i],\"--enable-tracking\")) {\n            config.enable_tracking = 1;\n        } else if (!strcmp(argv[i],\"--help\")) {\n            exit_status = 0;\n            goto usage;\n        #ifdef USE_OPENSSL\n        } else if (!strcmp(argv[i],\"--tls\")) {\n            config.tls = 1;\n        } else if (!strcmp(argv[i],\"--sni\")) {\n            if (lastarg) goto invalid;\n            config.sslconfig.sni = strdup(argv[++i]);\n        } else if (!strcmp(argv[i],\"--cacertdir\")) {\n            if (lastarg) goto invalid;\n            config.sslconfig.cacertdir = strdup(argv[++i]);\n        } else if (!strcmp(argv[i],\"--cacert\")) {\n            if (lastarg) goto invalid;\n            config.sslconfig.cacert = strdup(argv[++i]);\n        } else if (!strcmp(argv[i],\"--insecure\")) {\n            config.sslconfig.skip_cert_verify = 1;\n        } else if (!strcmp(argv[i],\"--cert\")) {\n            if (lastarg) goto invalid;\n            config.sslconfig.cert = strdup(argv[++i]);\n        } else if (!strcmp(argv[i],\"--key\")) {\n            if (lastarg) goto invalid;\n            config.sslconfig.key = strdup(argv[++i]);\n        } else if (!strcmp(argv[i],\"--tls-ciphers\")) {\n            if (lastarg) goto invalid;\n            config.sslconfig.ciphers = strdup(argv[++i]);\n        #ifdef TLS1_3_VERSION\n        } else if (!strcmp(argv[i],\"--tls-ciphersuites\")) {\n            if (lastarg) goto invalid;\n            config.sslconfig.ciphersuites = strdup(argv[++i]);\n        #endif\n        #endif\n        } else {\n            /* Assume the user meant to provide an option when the arg starts\n             * with a dash. We're done otherwise and should use the remainder\n             * as the command and arguments for running the benchmark. */\n            if (argv[i][0] == '-') goto invalid;\n            return i;\n        }\n    }\n\n    return i;\n\ninvalid:\n    printf(\"Invalid option \\\"%s\\\" or option argument missing\\n\\n\",argv[i]);\n\nusage:\n    printf(\n\"Usage: keydb-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests>] [-k <boolean>]\\n\\n\"\n\" -h <hostname>      Server hostname (default 127.0.0.1)\\n\"\n\" -p <port>          Server port (default 6379)\\n\"\n\" -s <socket>        Server socket (overrides host and port)\\n\"\n\" -a <password>      Password for Redis Auth\\n\"\n\" --user <username>  Used to send ACL style 'AUTH username pass'. Needs -a.\\n\"\n\" -c <clients>       Number of parallel connections (default 50)\\n\"\n\" -n <requests>      Total number of requests (default 100000)\\n\"\n\" -d <size>          Data size of SET/GET value in bytes (default 3)\\n\"\n\" --dbnum <db>       SELECT the specified db number (default 0)\\n\"\n\" --threads <num>    Enable multi-thread mode.\\n\"\n\" --cluster          Enable cluster mode.\\n\"\n\" --enable-tracking  Send CLIENT TRACKING on before starting benchmark.\\n\"\n\" -k <boolean>       1=keep alive 0=reconnect (default 1)\\n\"\n\" -r <keyspacelen>   Use random keys for SET/GET/INCR, random values for SADD,\\n\"\n\"                    random members and scores for ZADD.\\n\"\n\"  Using this option the benchmark will expand the string __rand_int__\\n\"\n\"  inside an argument with a 12 digits number in the specified range\\n\"\n\"  from 0 to keyspacelen-1. The substitution changes every time a command\\n\"\n\"  is executed. Default tests use this to hit random keys in the\\n\"\n\"  specified range.\\n\"\n\" -P <numreq>        Pipeline <numreq> requests. Default 1 (no pipeline).\\n\"\n\" -q                 Quiet. Just show query/sec values\\n\"\n\" --precision        Number of decimal places to display in latency output (default 0)\\n\"\n\" --csv              Output in CSV format\\n\"\n\" -l                 Loop. Run the tests forever\\n\"\n\" -t <tests>         Only run the comma separated list of tests. The test\\n\"\n\"                    names are the same as the ones produced as output.\\n\"\n\" -I                 Idle mode. Just open N idle connections and wait.\\n\"\n#ifdef USE_OPENSSL\n\" --tls              Establish a secure TLS connection.\\n\"\n\" --sni <host>       Server name indication for TLS.\\n\"\n\" --cacert <file>    CA Certificate file to verify with.\\n\"\n\" --cacertdir <dir>  Directory where trusted CA certificates are stored.\\n\"\n\"                    If neither cacert nor cacertdir are specified, the default\\n\"\n\"                    system-wide trusted root certs configuration will apply.\\n\"\n\" --insecure         Allow insecure TLS connection by skipping cert validation.\\n\"\n\" --cert <file>      Client certificate to authenticate with.\\n\"\n\" --key <file>       Private key file to authenticate with.\\n\"\n\" --tls-ciphers <list> Sets the list of prefered ciphers (TLSv1.2 and below)\\n\"\n\"                    in order of preference from highest to lowest separated by colon (\\\":\\\").\\n\"\n\"                    See the ciphers(1ssl) manpage for more information about the syntax of this string.\\n\"\n#ifdef TLS1_3_VERSION\n\" --tls-ciphersuites <list> Sets the list of prefered ciphersuites (TLSv1.3)\\n\"\n\"                    in order of preference from highest to lowest separated by colon (\\\":\\\").\\n\"\n\"                    See the ciphers(1ssl) manpage for more information about the syntax of this string,\\n\"\n\"                    and specifically for TLSv1.3 ciphersuites.\\n\"\n#endif\n#endif\n\" --help             Output this help and exit.\\n\"\n\" --version          Output version and exit.\\n\\n\"\n\"Examples:\\n\\n\"\n\" Run the benchmark with the default configuration against 127.0.0.1:6379:\\n\"\n\"   $ keydb-benchmark\\n\\n\"\n\" Use 20 parallel clients, for a total of 100k requests, against 192.168.1.1:\\n\"\n\"   $ keydb-benchmark -h 192.168.1.1 -p 6379 -n 100000 -c 20\\n\\n\"\n\" Fill 127.0.0.1:6379 with about 1 million keys only using the SET test:\\n\"\n\"   $ keydb-benchmark -t set -n 1000000 -r 100000000\\n\\n\"\n\" Benchmark 127.0.0.1:6379 for a few commands producing CSV output:\\n\"\n\"   $ keydb-benchmark -t ping,set,get -n 100000 --csv\\n\\n\"\n\" Benchmark a specific command line:\\n\"\n\"   $ keydb-benchmark -r 10000 -n 10000 eval 'return redis.call(\\\"ping\\\")' 0\\n\\n\"\n\" Fill a list with 10000 random elements:\\n\"\n\"   $ keydb-benchmark -r 10000 -n 10000 lpush mylist __rand_int__\\n\\n\"\n\" On user specified command lines __rand_int__ is replaced with a random integer\\n\"\n\" with a range of values selected by the -r option.\\n\"\n    );\n    exit(exit_status);\n}\n\nint showThroughput(struct aeEventLoop *eventLoop, long long id, void *clientData) {\n    UNUSED(eventLoop);\n    UNUSED(id);\n    UNUSED(clientData);\n    int liveclients = 0;\n    int requests_finished = 0;\n    int previous_requests_finished = 0;\n    long long current_tick = mstime();\n    atomicGet(config.liveclients, liveclients);\n    atomicGet(config.requests_finished, requests_finished);\n    atomicGet(config.previous_requests_finished, previous_requests_finished);\n    \n    if (liveclients == 0 && requests_finished != config.requests) {\n        fprintf(stderr,\"All clients disconnected... aborting.\\n\");\n        exit(1);\n    }\n    if (config.num_threads && requests_finished >= config.requests) {\n        aeStop(eventLoop);\n        return AE_NOMORE;\n    }\n    if (config.csv) return 250;\n    if (config.idlemode == 1) {\n        printf(\"clients: %d\\r\", config.liveclients);\n        fflush(stdout);\n\treturn 250;\n    }\n    const float dt = (float)(current_tick-config.start)/1000.0;\n    const float rps = (float)requests_finished/dt;\n    const float instantaneous_dt = (float)(current_tick-config.previous_tick)/1000.0;\n    const float instantaneous_rps = (float)(requests_finished-previous_requests_finished)/instantaneous_dt;\n    config.previous_tick = current_tick;\n    atomicSet(config.previous_requests_finished,requests_finished);\n    int printed_bytes = printf(\"%s: rps=%.1f (overall: %.1f) avg_msec=%.3f (overall: %.3f)\\r\", config.title, instantaneous_rps, rps, hdr_mean(config.current_sec_latency_histogram)/1000.0f, hdr_mean(config.latency_histogram)/1000.0f);\n    if (printed_bytes > config.last_printed_bytes){\n       config.last_printed_bytes = printed_bytes;\n    }\n    hdr_reset(config.current_sec_latency_histogram);\n    fflush(stdout);\n    return 250; /* every 250ms */\n}\n\n/* Return true if the named test was selected using the -t command line\n * switch, or if all the tests are selected (no -t passed by user). */\nint test_is_selected(const char *name) {\n    char buf[256];\n    int l = strlen(name);\n\n    if (config.tests == NULL) return 1;\n    buf[0] = ',';\n    memcpy(buf+1,name,l);\n    buf[l+1] = ',';\n    buf[l+2] = '\\0';\n    return strstr(config.tests,buf) != NULL;\n}\n\nint main(int argc, const char **argv) {\n    int i;\n    char *data, *cmd;\n    const char *tag;\n    int len;\n\n    client c;\n    aeThreadOnline();\n    storage_init(NULL, 0);\n\n    srandom(time(NULL) ^ getpid());\n    init_genrand64(ustime() ^ getpid());\n    signal(SIGHUP, SIG_IGN);\n    signal(SIGPIPE, SIG_IGN);\n\n    memset(&config.sslconfig, 0, sizeof(config.sslconfig));\n    config.numclients = 50;\n    config.requests = 100000;\n    config.liveclients = 0;\n    config.el = aeCreateEventLoop(1024*10);\n    aeCreateTimeEvent(config.el,1,showThroughput,NULL,NULL);\n    config.keepalive = 1;\n    config.datasize = 3;\n    config.pipeline = 1;\n    config.randomkeys = 0;\n    config.randomkeys_keyspacelen = 0;\n    config.quiet = 0;\n    config.csv = 0;\n    config.loop = 0;\n    config.idlemode = 0;\n    config.clients = listCreate();\n    config.hostip = \"127.0.0.1\";\n    config.hostport = 6379;\n    config.hostsocket = NULL;\n    config.tests = NULL;\n    config.dbnum = 0;\n    config.auth = NULL;\n    config.precision = DEFAULT_LATENCY_PRECISION;\n    config.num_threads = 0;\n    config.threads = NULL;\n    config.cluster_mode = 0;\n    config.cluster_node_count = 0;\n    config.cluster_nodes = NULL;\n    config.redis_config = NULL;\n    config.is_fetching_slots = 0;\n    config.is_updating_slots = 0;\n    config.slots_last_update = 0;\n    config.enable_tracking = 0;\n\n    i = parseOptions(argc,argv);\n    argc -= i;\n    argv += i;\n\n    tag = \"\";\n\n#ifdef USE_OPENSSL\n    if (config.tls) {\n        cliSecureInit();\n    }\n#endif\n    aeThreadOffline();\n\n    if (config.cluster_mode) {\n        // We only include the slot placeholder {tag} if cluster mode is enabled\n        tag = \":{tag}\";\n\n        /* Fetch cluster configuration. */\n        if (!fetchClusterConfiguration() || !config.cluster_nodes) {\n            if (!config.hostsocket) {\n                fprintf(stderr, \"Failed to fetch cluster configuration from \"\n                                \"%s:%d\\n\", config.hostip, config.hostport);\n            } else {\n                fprintf(stderr, \"Failed to fetch cluster configuration from \"\n                                \"%s\\n\", config.hostsocket);\n            }\n            exit(1);\n        }\n        if (config.cluster_node_count <= 1) {\n            fprintf(stderr, \"Invalid cluster: %d node(s).\\n\",\n                    config.cluster_node_count);\n            exit(1);\n        }\n        printf(\"Cluster has %d master nodes:\\n\\n\", config.cluster_node_count);\n        int i = 0;\n        for (; i < config.cluster_node_count; i++) {\n            clusterNode *node = config.cluster_nodes[i];\n            if (!node) {\n                fprintf(stderr, \"Invalid cluster node #%d\\n\", i);\n                exit(1);\n            }\n            printf(\"Master %d: \", i);\n            if (node->name) printf(\"%s \", node->name);\n            printf(\"%s:%d\\n\", node->ip, node->port);\n            node->redis_config = getRedisConfig(node->ip, node->port, NULL);\n            if (node->redis_config == NULL) {\n                fprintf(stderr, \"WARN: could not fetch node CONFIG %s:%d\\n\",\n                        node->ip, node->port);\n            }\n        }\n        printf(\"\\n\");\n        /* Automatically set thread number to node count if not specified\n         * by the user. */\n        if (config.num_threads == 0)\n            config.num_threads = config.cluster_node_count;\n    } else {\n        config.redis_config =\n            getRedisConfig(config.hostip, config.hostport, config.hostsocket);\n        if (config.redis_config == NULL) {\n            fprintf(stderr, \"WARN: could not fetch server CONFIG\\n\");\n        }\n    }\n    if (config.num_threads > 0) {\n        pthread_mutex_init(&(config.liveclients_mutex), NULL);\n        pthread_mutex_init(&(config.is_updating_slots_mutex), NULL);\n    }\n\n    if (config.keepalive == 0) {\n        printf(\"WARNING: keepalive disabled, you probably need 'echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse' for Linux and 'sudo sysctl -w net.inet.tcp.msl=1000' for Mac OS X in order to use a lot of clients/requests\\n\");\n    }\n\n    if (config.idlemode) {\n        printf(\"Creating %d idle connections and waiting forever (Ctrl+C when done)\\n\", config.numclients);\n        int thread_id = -1, use_threads = (config.num_threads > 0);\n        if (use_threads) {\n            thread_id = 0;\n            initBenchmarkThreads();\n        }\n        c = createClient(\"\",0,NULL,thread_id); /* will never receive a reply */\n        createMissingClients(c);\n        if (use_threads) startBenchmarkThreads();\n        else aeMain(config.el);\n        /* and will wait for every */\n    }\n    if(config.csv){\n        printf(\"\\\"test\\\",\\\"rps\\\",\\\"avg_latency_ms\\\",\\\"min_latency_ms\\\",\\\"p50_latency_ms\\\",\\\"p95_latency_ms\\\",\\\"p99_latency_ms\\\",\\\"max_latency_ms\\\"\\n\");\n    }\n    /* Run benchmark with command in the remainder of the arguments. */\n    if (argc) {\n        sds title = sdsnew(argv[0]);\n        for (i = 1; i < argc; i++) {\n            title = sdscatlen(title, \" \", 1);\n            title = sdscatlen(title, (char*)argv[i], strlen(argv[i]));\n        }\n\n        do {\n            len = redisFormatCommandArgv(&cmd,argc,argv,NULL);\n            // adjust the datasize to the parsed command\n            config.datasize = len;\n            benchmark(title,cmd,len);\n            free(cmd);\n        } while(config.loop);\n\n        if (config.redis_config != NULL) freeRedisConfig(config.redis_config);\n        return 0;\n    }\n\n    /* Run default benchmark suite. */\n    data = (char*)zmalloc(config.datasize+1, MALLOC_LOCAL);\n    do {\n        genBenchmarkRandomData(data, config.datasize);\n        data[config.datasize] = '\\0';\n\n        if (test_is_selected(\"ping_inline\") || test_is_selected(\"ping\"))\n            benchmark(\"PING_INLINE\",\"PING\\r\\n\",6);\n\n        if (test_is_selected(\"ping_mbulk\") || test_is_selected(\"ping\")) {\n            len = redisFormatCommand(&cmd,\"PING\");\n            benchmark(\"PING_MBULK\",cmd,len);\n            free(cmd);\n        }\n\n        if (test_is_selected(\"set\")) {\n            len = redisFormatCommand(&cmd,\"SET key%s:__rand_int__ %s\",tag,data);\n            benchmark(\"SET\",cmd,len);\n            free(cmd);\n        }\n\n        if (test_is_selected(\"get\")) {\n            len = redisFormatCommand(&cmd,\"GET key%s:__rand_int__\",tag);\n            benchmark(\"GET\",cmd,len);\n            free(cmd);\n        }\n\n        if (test_is_selected(\"incr\")) {\n            len = redisFormatCommand(&cmd,\"INCR counter%s:__rand_int__\",tag);\n            benchmark(\"INCR\",cmd,len);\n            free(cmd);\n        }\n\n        if (test_is_selected(\"lpush\")) {\n            len = redisFormatCommand(&cmd,\"LPUSH mylist%s %s\",tag,data);\n            benchmark(\"LPUSH\",cmd,len);\n            free(cmd);\n        }\n\n        if (test_is_selected(\"rpush\")) {\n            len = redisFormatCommand(&cmd,\"RPUSH mylist%s %s\",tag,data);\n            benchmark(\"RPUSH\",cmd,len);\n            free(cmd);\n        }\n\n        if (test_is_selected(\"lpop\")) {\n            len = redisFormatCommand(&cmd,\"LPOP mylist%s\",tag);\n            benchmark(\"LPOP\",cmd,len);\n            free(cmd);\n        }\n\n        if (test_is_selected(\"rpop\")) {\n            len = redisFormatCommand(&cmd,\"RPOP mylist%s\",tag);\n            benchmark(\"RPOP\",cmd,len);\n            free(cmd);\n        }\n\n        if (test_is_selected(\"sadd\")) {\n            len = redisFormatCommand(&cmd,\n                \"SADD myset%s element:__rand_int__\",tag);\n            benchmark(\"SADD\",cmd,len);\n            free(cmd);\n        }\n\n        if (test_is_selected(\"hset\")) {\n            len = redisFormatCommand(&cmd,\n                \"HSET myhash%s element:__rand_int__ %s\",tag,data);\n            benchmark(\"HSET\",cmd,len);\n            free(cmd);\n        }\n\n        if (test_is_selected(\"spop\")) {\n            len = redisFormatCommand(&cmd,\"SPOP myset%s\",tag);\n            benchmark(\"SPOP\",cmd,len);\n            free(cmd);\n        }\n\n        if (test_is_selected(\"zadd\")) {\n            const char *score = \"0\";\n            if (config.randomkeys) score = \"__rand_int__\";\n            len = redisFormatCommand(&cmd,\n                \"ZADD myzset%s %s element:__rand_int__\",tag,score);\n            benchmark(\"ZADD\",cmd,len);\n            free(cmd);\n        }\n\n        if (test_is_selected(\"zpopmin\")) {\n            len = redisFormatCommand(&cmd,\"ZPOPMIN myzset%s\",tag);\n            benchmark(\"ZPOPMIN\",cmd,len);\n            free(cmd);\n        }\n\n        if (test_is_selected(\"lrange\") ||\n            test_is_selected(\"lrange_100\") ||\n            test_is_selected(\"lrange_300\") ||\n            test_is_selected(\"lrange_500\") ||\n            test_is_selected(\"lrange_600\"))\n        {\n            len = redisFormatCommand(&cmd,\"LPUSH mylist%s %s\",tag,data);\n            benchmark(\"LPUSH (needed to benchmark LRANGE)\",cmd,len);\n            free(cmd);\n        }\n\n        if (test_is_selected(\"lrange\") || test_is_selected(\"lrange_100\")) {\n            len = redisFormatCommand(&cmd,\"LRANGE mylist%s 0 99\",tag);\n            benchmark(\"LRANGE_100 (first 100 elements)\",cmd,len);\n            free(cmd);\n        }\n\n        if (test_is_selected(\"lrange\") || test_is_selected(\"lrange_300\")) {\n            len = redisFormatCommand(&cmd,\"LRANGE mylist%s 0 299\",tag);\n            benchmark(\"LRANGE_300 (first 300 elements)\",cmd,len);\n            free(cmd);\n        }\n\n        if (test_is_selected(\"lrange\") || test_is_selected(\"lrange_500\")) {\n            len = redisFormatCommand(&cmd,\"LRANGE mylist%s 0 499\",tag);\n            benchmark(\"LRANGE_500 (first 500 elements)\",cmd,len);\n            free(cmd);\n        }\n\n        if (test_is_selected(\"lrange\") || test_is_selected(\"lrange_600\")) {\n            len = redisFormatCommand(&cmd,\"LRANGE mylist%s 0 599\",tag);\n            benchmark(\"LRANGE_600 (first 600 elements)\",cmd,len);\n            free(cmd);\n        }\n\n        if (test_is_selected(\"mset\")) {\n            const char *cmd_argv[21];\n            cmd_argv[0] = \"MSET\";\n            sds key_placeholder = sdscatprintf(sdsnew(\"\"),\"key%s:__rand_int__\",tag);\n            for (i = 1; i < 21; i += 2) {\n                cmd_argv[i] = key_placeholder;\n                cmd_argv[i+1] = data;\n            }\n            len = redisFormatCommandArgv(&cmd,21,cmd_argv,NULL);\n            benchmark(\"MSET (10 keys)\",cmd,len);\n            free(cmd);\n            sdsfree(key_placeholder);\n        }\n\n        if (test_is_selected(\"mget\")) {\n            const char *cmd_argv[1002];\n            cmd_argv[0] = \"MGET\";\n            sds key_placeholder = sdscatprintf(sdsnew(\"\"),\"key%s:__rand_int__\",tag);\n            for (int keys = 1; keys < 1002; keys += 100) {\n                for (i = 1; i < keys + 1; i++) {\n                    cmd_argv[i] = key_placeholder;\n                }\n                len = redisFormatCommandArgv(&cmd,keys+1,cmd_argv,NULL);\n                std::string title = \"MGET (\" + std::to_string(keys) + \" keys)\";\n                benchmark(title.data(),cmd,len);\n                free(cmd);\n            }\n            sdsfree(key_placeholder);\n        }\n\n        if (test_is_selected(\"hmset\")) {\n            const char *cmd_argv[22];\n            cmd_argv[0] = \"HMSET\";\n            cmd_argv[1] = \"testhash\";\n            sds key_placeholder = sdscatprintf(sdsnew(\"\"),\"key%s:__rand_int__\",tag);\n            for (i = 2; i < 22; i += 2) {\n                cmd_argv[i] = key_placeholder;\n                cmd_argv[i+1] = data;\n            }\n            len = redisFormatCommandArgv(&cmd,22,cmd_argv,NULL);\n            benchmark(\"MSET (10 keys)\",cmd,len);\n            free(cmd);\n            sdsfree(key_placeholder);\n        }\n\n        if (test_is_selected(\"hmget\")) {\n            const char *cmd_argv[1003];\n            cmd_argv[0] = \"HMGET\";\n            cmd_argv[1] = \"testhash\";\n            sds key_placeholder = sdscatprintf(sdsnew(\"\"),\"key%s:__rand_int__\",tag);\n            for (int keys = 1; keys < 1002; keys += 100) {\n                for (i = 2; i < keys + 2; i++) {\n                    cmd_argv[i] = key_placeholder;\n                }\n                len = redisFormatCommandArgv(&cmd,keys+2,cmd_argv,NULL);\n                std::string title = \"HMGET (\" + std::to_string(keys) + \" keys)\";\n                benchmark(title.data(),cmd,len);\n                free(cmd);\n            }\n            sdsfree(key_placeholder);\n        }\n\n        if (!config.csv) printf(\"\\n\");\n    } while(config.loop);\n    zfree(data);\n\n    if (config.redis_config != NULL) freeRedisConfig(config.redis_config);\n\n    return 0;\n}\n"
  },
  {
    "path": "src/redis-check-aof.cpp",
    "content": "/*\n * Copyright (c) 2009-2012, Pieter Noordhuis <pcnoordhuis at gmail dot com>\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n#include <sys/stat.h>\n\n#define ERROR(...) { \\\n    char __buf[1024]; \\\n    snprintf(__buf, sizeof(__buf), __VA_ARGS__); \\\n    snprintf(error, sizeof(error), \"0x%16llx: %s\", (long long)epos, __buf); \\\n}\n\nstatic char error[1044];\nstatic off_t epos;\nstatic long long line = 1;\n\nint consumeNewline(char *buf) {\n    if (strncmp(buf,\"\\r\\n\",2) != 0) {\n        ERROR(\"Expected \\\\r\\\\n, got: %02x%02x\",buf[0],buf[1]);\n        return 0;\n    }\n    line += 1;\n    return 1;\n}\n\nint readLong(FILE *fp, char prefix, long *target) {\n    char buf[128], *eptr;\n    epos = ftello(fp);\n    if (fgets(buf,sizeof(buf),fp) == NULL) {\n        return 0;\n    }\n    if (buf[0] != prefix) {\n        ERROR(\"Expected prefix '%c', got: '%c'\",prefix,buf[0]);\n        return 0;\n    }\n    *target = strtol(buf+1,&eptr,10);\n    return consumeNewline(eptr);\n}\n\nint readBytes(FILE *fp, char *target, long length) {\n    long real;\n    epos = ftello(fp);\n    real = fread(target,1,length,fp);\n    if (real != length) {\n        ERROR(\"Expected to read %ld bytes, got %ld bytes\",length,real);\n        return 0;\n    }\n    return 1;\n}\n\nint readString(FILE *fp, char** target) {\n    long len;\n    *target = NULL;\n    if (!readLong(fp,'$',&len)) {\n        return 0;\n    }\n\n    /* Increase length to also consume \\r\\n */\n    len += 2;\n    *target = (char*)zmalloc(len, MALLOC_LOCAL);\n    if (!readBytes(fp,*target,len)) {\n        return 0;\n    }\n    if (!consumeNewline(*target+len-2)) {\n        return 0;\n    }\n    (*target)[len-2] = '\\0';\n    return 1;\n}\n\nint readArgc(FILE *fp, long *target) {\n    return readLong(fp,'*',target);\n}\n\noff_t process(FILE *fp) {\n    long argc;\n    off_t pos = 0;\n    int i, multi = 0;\n    char *str;\n\n    while(1) {\n        if (!multi) pos = ftello(fp);\n        if (!readArgc(fp, &argc)) break;\n\n        for (i = 0; i < argc; i++) {\n            if (!readString(fp,&str)) break;\n            if (i == 0) {\n                if (strcasecmp(str, \"multi\") == 0) {\n                    if (multi++) {\n                        ERROR(\"Unexpected MULTI\");\n                        break;\n                    }\n                } else if (strcasecmp(str, \"exec\") == 0) {\n                    if (--multi) {\n                        ERROR(\"Unexpected EXEC\");\n                        break;\n                    }\n                }\n            }\n            zfree(str);\n        }\n\n        /* Stop if the loop did not finish */\n        if (i < argc) {\n            if (str) zfree(str);\n            break;\n        }\n    }\n\n    if (feof(fp) && multi && strlen(error) == 0) {\n        ERROR(\"Reached EOF before reading EXEC for MULTI\");\n    }\n    if (strlen(error) > 0) {\n        printf(\"%s\\n\", error);\n    }\n    return pos;\n}\n\nint redis_check_aof_main(int argc, char **argv) {\n    char *filename;\n    int fix = 0;\n\n    if (argc < 2) {\n        printf(\"Usage: %s [--fix] <file.aof>\\n\", argv[0]);\n        exit(1);\n    } else if (argc == 2) {\n        filename = argv[1];\n    } else if (argc == 3) {\n        if (strcmp(argv[1],\"--fix\") != 0) {\n            printf(\"Invalid argument: %s\\n\", argv[1]);\n            exit(1);\n        }\n        filename = argv[2];\n        fix = 1;\n    } else {\n        printf(\"Invalid arguments\\n\");\n        exit(1);\n    }\n\n    FILE *fp = fopen(filename,\"r+\");\n    if (fp == NULL) {\n        printf(\"Cannot open file: %s\\n\", filename);\n        exit(1);\n    }\n\n    struct redis_stat sb;\n    if (redis_fstat(fileno(fp),&sb) == -1) {\n        printf(\"Cannot stat file: %s\\n\", filename);\n        exit(1);\n    }\n\n    off_t size = sb.st_size;\n    if (size == 0) {\n        printf(\"Empty file: %s\\n\", filename);\n        exit(1);\n    }\n\n    /* This AOF file may have an RDB preamble. Check this to start, and if this\n     * is the case, start processing the RDB part. */\n    if (size >= 8) {    /* There must be at least room for the RDB header. */\n        char sig[5];\n        int has_preamble = fread(sig,sizeof(sig),1,fp) == 1 &&\n                            memcmp(sig,\"REDIS\",sizeof(sig)) == 0;\n        rewind(fp);\n        if (has_preamble) {\n            printf(\"The AOF appears to start with an RDB preamble.\\n\"\n                   \"Checking the RDB preamble to start:\\n\");\n            if (redis_check_rdb_main(argc,(const char**)argv,fp) == C_ERR) {\n                printf(\"RDB preamble of AOF file is not sane, aborting.\\n\");\n                exit(1);\n            } else {\n                printf(\"RDB preamble is OK, proceeding with AOF tail...\\n\");\n            }\n        }\n    }\n\n    off_t pos = process(fp);\n    off_t diff = size-pos;\n    printf(\"AOF analyzed: size=%lld, ok_up_to=%lld, ok_up_to_line=%lld, diff=%lld\\n\",\n        (long long) size, (long long) pos, line, (long long) diff);\n    if (diff > 0) {\n        if (fix) {\n            char buf[2];\n            printf(\"This will shrink the AOF from %lld bytes, with %lld bytes, to %lld bytes\\n\",(long long)size,(long long)diff,(long long)pos);\n            printf(\"Continue? [y/N]: \");\n            if (fgets(buf,sizeof(buf),stdin) == NULL ||\n                strncasecmp(buf,\"y\",1) != 0) {\n                    printf(\"Aborting...\\n\");\n                    exit(1);\n            }\n            if (ftruncate(fileno(fp), pos) == -1) {\n                printf(\"Failed to truncate AOF\\n\");\n                exit(1);\n            } else {\n                printf(\"Successfully truncated AOF\\n\");\n            }\n        } else {\n            printf(\"AOF is not valid. \"\n                   \"Use the --fix option to try fixing it.\\n\");\n            exit(1);\n        }\n    } else {\n        printf(\"AOF is valid\\n\");\n    }\n\n    fclose(fp);\n    exit(0);\n}\n"
  },
  {
    "path": "src/redis-check-rdb.cpp",
    "content": "/*\n * Copyright (c) 2016, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"mt19937-64.h\"\n#include \"server.h\"\n#include \"rdb.h\"\n\n#include <stdarg.h>\n#include <sys/time.h>\n#include <unistd.h>\n\nvoid createSharedObjects(void);\nvoid rdbLoadProgressCallback(rio *r, const void *buf, size_t len);\nint rdbCheckMode = 0;\n\nstruct {\n    ::rio *rio;\n    robj *key;                      /* Current key we are reading. */\n    int key_type;                   /* Current key type if != -1. */\n    unsigned long keys;             /* Number of keys processed. */\n    unsigned long expires;          /* Number of keys with an expire. */\n    unsigned long already_expired;  /* Number of keys already expired. */\n    int doing;                      /* The state while reading the RDB. */\n    int error_set;                  /* True if error is populated. */\n    char error[1024];\n} rdbstate;\n\n/* At every loading step try to remember what we were about to do, so that\n * we can log this information when an error is encountered. */\n#define RDB_CHECK_DOING_START 0\n#define RDB_CHECK_DOING_READ_TYPE 1\n#define RDB_CHECK_DOING_READ_EXPIRE 2\n#define RDB_CHECK_DOING_READ_KEY 3\n#define RDB_CHECK_DOING_READ_OBJECT_VALUE 4\n#define RDB_CHECK_DOING_CHECK_SUM 5\n#define RDB_CHECK_DOING_READ_LEN 6\n#define RDB_CHECK_DOING_READ_AUX 7\n#define RDB_CHECK_DOING_READ_MODULE_AUX 8\n\nconst char *rdb_check_doing_string[] = {\n    \"start\",\n    \"read-type\",\n    \"read-expire\",\n    \"read-key\",\n    \"read-object-value\",\n    \"check-sum\",\n    \"read-len\",\n    \"read-aux\",\n    \"read-module-aux\"\n};\n\nconst char *rdb_type_string[] = {\n    \"string\",\n    \"list-linked\",\n    \"set-hashtable\",\n    \"zset-v1\",\n    \"hash-hashtable\",\n    \"zset-v2\",\n    \"module-value\",\n    \"\",\"\",\n    \"hash-zipmap\",\n    \"list-ziplist\",\n    \"set-intset\",\n    \"zset-ziplist\",\n    \"hash-ziplist\",\n    \"quicklist\",\n    \"stream\"\n};\n\n/* Show a few stats collected into 'rdbstate' */\nvoid rdbShowGenericInfo(void) {\n    printf(\"[info] %lu keys read\\n\", rdbstate.keys);\n    printf(\"[info] %lu expires\\n\", rdbstate.expires);\n    printf(\"[info] %lu already expired\\n\", rdbstate.already_expired);\n}\n\n/* Called on RDB errors. Provides details about the RDB and the offset\n * we were when the error was detected. */\nvoid rdbCheckError(const char *fmt, ...) {\n    char msg[1024];\n    va_list ap;\n\n    va_start(ap, fmt);\n    vsnprintf(msg, sizeof(msg), fmt, ap);\n    va_end(ap);\n\n    printf(\"--- RDB ERROR DETECTED ---\\n\");\n    printf(\"[offset %llu] %s\\n\",\n        (unsigned long long) (rdbstate.rio ?\n            rdbstate.rio->processed_bytes : 0), msg);\n    printf(\"[additional info] While doing: %s\\n\",\n        rdb_check_doing_string[rdbstate.doing]);\n    if (rdbstate.key)\n        printf(\"[additional info] Reading key '%s'\\n\",\n            (char*)ptrFromObj(rdbstate.key));\n    if (rdbstate.key_type != -1)\n        printf(\"[additional info] Reading type %d (%s)\\n\",\n            rdbstate.key_type,\n            ((unsigned)rdbstate.key_type <\n             sizeof(rdb_type_string)/sizeof(char*)) ?\n                rdb_type_string[rdbstate.key_type] : \"unknown\");\n    rdbShowGenericInfo();\n}\n\n/* Print informations during RDB checking. */\nvoid rdbCheckInfo(const char *fmt, ...) {\n    char msg[1024];\n    va_list ap;\n\n    va_start(ap, fmt);\n    vsnprintf(msg, sizeof(msg), fmt, ap);\n    va_end(ap);\n\n    printf(\"[offset %llu] %s\\n\",\n        (unsigned long long) (rdbstate.rio ?\n            rdbstate.rio->processed_bytes : 0), msg);\n}\n\n/* Used inside rdb.c in order to log specific errors happening inside\n * the RDB loading internals. */\nvoid rdbCheckSetError(const char *fmt, ...) {\n    va_list ap;\n\n    va_start(ap, fmt);\n    vsnprintf(rdbstate.error, sizeof(rdbstate.error), fmt, ap);\n    va_end(ap);\n    rdbstate.error_set = 1;\n}\n\n/* During RDB check we setup a special signal handler for memory violations\n * and similar conditions, so that we can log the offending part of the RDB\n * if the crash is due to broken content. */\nvoid rdbCheckHandleCrash(int sig, siginfo_t *info, void *secret) {\n    UNUSED(sig);\n    UNUSED(info);\n    UNUSED(secret);\n\n    rdbCheckError(\"Server crash checking the specified RDB file!\");\n    exit(1);\n}\n\nvoid rdbCheckSetupSignals(void) {\n    struct sigaction act;\n\n    sigemptyset(&act.sa_mask);\n    act.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;\n    act.sa_sigaction = rdbCheckHandleCrash;\n    sigaction(SIGSEGV, &act, NULL);\n    sigaction(SIGBUS, &act, NULL);\n    sigaction(SIGFPE, &act, NULL);\n    sigaction(SIGILL, &act, NULL);\n    sigaction(SIGABRT, &act, NULL);\n}\n\n/* Check the specified RDB file. Return 0 if the RDB looks sane, otherwise\n * 1 is returned.\n * The file is specified as a filename in 'rdbfilename' if 'fp' is not NULL,\n * otherwise the already open file 'fp' is checked. */\nint redis_check_rdb(const char *rdbfilename, FILE *fp) {\n    uint64_t dbid;\n    int type, rdbver;\n    char buf[1024];\n    long long expiretime, now = mstime();\n    static rio rdb; /* Pointed by global struct riostate. */\n\n    int closefile = (fp == NULL);\n    if (fp == NULL && (fp = fopen(rdbfilename,\"r\")) == NULL) return 1;\n\n    startLoadingFile(fp, rdbfilename, RDBFLAGS_NONE);\n    rioInitWithFile(&rdb,fp);\n    rdbstate.rio = &rdb;\n    rdb.update_cksum = rdbLoadProgressCallback;\n    if (rioRead(&rdb,buf,9) == 0) goto eoferr;\n    buf[9] = '\\0';\n    if (memcmp(buf,\"REDIS\",5) != 0) {\n        rdbCheckError(\"Wrong signature trying to load DB from file\");\n        goto err;\n    }\n    rdbver = atoi(buf+5);\n    if (rdbver < 1 || rdbver > RDB_VERSION) {\n        rdbCheckError(\"Can't handle RDB format version %d\",rdbver);\n        goto err;\n    }\n\n    expiretime = -1;\n    while(1) {\n        robj *key, *val;\n\n        /* Read type. */\n        rdbstate.doing = RDB_CHECK_DOING_READ_TYPE;\n        if ((type = rdbLoadType(&rdb)) == -1) goto eoferr;\n\n        /* Handle special types. */\n        if (type == RDB_OPCODE_EXPIRETIME) {\n            rdbstate.doing = RDB_CHECK_DOING_READ_EXPIRE;\n            /* EXPIRETIME: load an expire associated with the next key\n             * to load. Note that after loading an expire we need to\n             * load the actual type, and continue. */\n            expiretime = rdbLoadTime(&rdb);\n            expiretime *= 1000;\n            if (rioGetReadError(&rdb)) goto eoferr;\n            continue; /* Read next opcode. */\n        } else if (type == RDB_OPCODE_EXPIRETIME_MS) {\n            /* EXPIRETIME_MS: milliseconds precision expire times introduced\n             * with RDB v3. Like EXPIRETIME but no with more precision. */\n            rdbstate.doing = RDB_CHECK_DOING_READ_EXPIRE;\n            expiretime = rdbLoadMillisecondTime(&rdb, rdbver);\n            if (rioGetReadError(&rdb)) goto eoferr;\n            continue; /* Read next opcode. */\n        } else if (type == RDB_OPCODE_FREQ) {\n            /* FREQ: LFU frequency. */\n            uint8_t byte;\n            if (rioRead(&rdb,&byte,1) == 0) goto eoferr;\n            continue; /* Read next opcode. */\n        } else if (type == RDB_OPCODE_IDLE) {\n            /* IDLE: LRU idle time. */\n            if (rdbLoadLen(&rdb,NULL) == RDB_LENERR) goto eoferr;\n            continue; /* Read next opcode. */\n        } else if (type == RDB_OPCODE_EOF) {\n            /* EOF: End of file, exit the main loop. */\n            break;\n        } else if (type == RDB_OPCODE_SELECTDB) {\n            /* SELECTDB: Select the specified database. */\n            rdbstate.doing = RDB_CHECK_DOING_READ_LEN;\n            if ((dbid = rdbLoadLen(&rdb,NULL)) == RDB_LENERR)\n                goto eoferr;\n            rdbCheckInfo(\"Selecting DB ID %llu\", (unsigned long long)dbid);\n            continue; /* Read type again. */\n        } else if (type == RDB_OPCODE_RESIZEDB) {\n            /* RESIZEDB: Hint about the size of the keys in the currently\n             * selected data base, in order to avoid useless rehashing. */\n            uint64_t db_size, expires_size;\n            rdbstate.doing = RDB_CHECK_DOING_READ_LEN;\n            if ((db_size = rdbLoadLen(&rdb,NULL)) == RDB_LENERR)\n                goto eoferr;\n            if ((expires_size = rdbLoadLen(&rdb,NULL)) == RDB_LENERR)\n                goto eoferr;\n            continue; /* Read type again. */\n        } else if (type == RDB_OPCODE_AUX) {\n            /* AUX: generic string-string fields. Use to add state to RDB\n             * which is backward compatible. Implementations of RDB loading\n             * are requierd to skip AUX fields they don't understand.\n             *\n             * An AUX field is composed of two strings: key and value. */\n            robj *auxkey, *auxval;\n            rdbstate.doing = RDB_CHECK_DOING_READ_AUX;\n            if ((auxkey = rdbLoadStringObject(&rdb)) == NULL) goto eoferr;\n            if ((auxval = rdbLoadStringObject(&rdb)) == NULL) goto eoferr;\n\n            rdbCheckInfo(\"AUX FIELD %s = '%s'\",\n                (char*)ptrFromObj(auxkey), (char*)ptrFromObj(auxval));\n            decrRefCount(auxkey);\n            decrRefCount(auxval);\n            continue; /* Read type again. */\n        } else if (type == RDB_OPCODE_MODULE_AUX) {\n            /* AUX: Auxiliary data for modules. */\n            uint64_t moduleid, when_opcode, when;\n            rdbstate.doing = RDB_CHECK_DOING_READ_MODULE_AUX;\n            if ((moduleid = rdbLoadLen(&rdb,NULL)) == RDB_LENERR) goto eoferr;\n            if ((when_opcode = rdbLoadLen(&rdb,NULL)) == RDB_LENERR) goto eoferr;\n            if ((when = rdbLoadLen(&rdb,NULL)) == RDB_LENERR) goto eoferr;\n\n            char name[10];\n            moduleTypeNameByID(name,moduleid);\n            rdbCheckInfo(\"MODULE AUX for: %s\", name);\n\n            robj *o = rdbLoadCheckModuleValue(&rdb,name);\n            decrRefCount(o);\n            continue; /* Read type again. */\n        } else {\n            if (!rdbIsObjectType(type)) {\n                rdbCheckError(\"Invalid object type: %d\", type);\n                goto err;\n            }\n            rdbstate.key_type = type;\n        }\n\n        /* Read key */\n        rdbstate.doing = RDB_CHECK_DOING_READ_KEY;\n        if ((key = rdbLoadStringObject(&rdb)) == NULL) goto eoferr;\n        rdbstate.key = key;\n        rdbstate.keys++;\n        /* Read value */\n        rdbstate.doing = RDB_CHECK_DOING_READ_OBJECT_VALUE;\n        if ((val = rdbLoadObject(type,&rdb,szFromObj(key),NULL,OBJ_MVCC_INVALID)) == NULL) goto eoferr;\n        /* Check if the key already expired. */\n        if (expiretime != -1 && expiretime < now)\n            rdbstate.already_expired++;\n        if (expiretime != -1) rdbstate.expires++;\n        rdbstate.key = NULL;\n        decrRefCount(key);\n        decrRefCount(val);\n        rdbstate.key_type = -1;\n        expiretime = -1;\n    }\n    /* Verify the checksum if RDB version is >= 5 */\n    if (rdbver >= 5 && g_pserver->rdb_checksum) {\n        uint64_t cksum, expected = rdb.cksum;\n\n        rdbstate.doing = RDB_CHECK_DOING_CHECK_SUM;\n        if (rioRead(&rdb,&cksum,8) == 0) goto eoferr;\n        memrev64ifbe(&cksum);\n        if (cksum == 0) {\n            rdbCheckInfo(\"RDB file was saved with checksum disabled: no check performed.\");\n        } else if (cksum != expected) {\n            rdbCheckError(\"RDB CRC error\");\n            goto err;\n        } else {\n            rdbCheckInfo(\"Checksum OK\");\n        }\n    }\n\n    if (closefile) fclose(fp);\n    stopLoading(1);\n    return 0;\n\neoferr: /* unexpected end of file is handled here with a fatal exit */\n    if (rdbstate.error_set) {\n        rdbCheckError(rdbstate.error);\n    } else {\n        rdbCheckError(\"Unexpected EOF reading RDB file\");\n    }\nerr:\n    if (closefile) fclose(fp);\n    stopLoading(0);\n    return 1;\n}\n\n/* RDB check main: called form server.c when Redis is executed with the\n * keydb-check-rdb alias, on during RDB loading errors.\n *\n * The function works in two ways: can be called with argc/argv as a\n * standalone executable, or called with a non NULL 'fp' argument if we\n * already have an open file to check. This happens when the function\n * is used to check an RDB preamble inside an AOF file.\n *\n * When called with fp = NULL, the function never returns, but exits with the\n * status code according to success (RDB is sane) or error (RDB is corrupted).\n * Otherwise if called with a non NULL fp, the function returns C_OK or\n * C_ERR depending on the success or failure. */\nint redis_check_rdb_main(int argc, const char **argv, FILE *fp) {\n    struct timeval tv;\n\n    if (argc != 2 && fp == NULL) {\n        fprintf(stderr, \"Usage: %s <rdb-file-name>\\n\", argv[0]);\n        exit(1);\n    }\n\n    gettimeofday(&tv, NULL);\n    init_genrand64(((long long) tv.tv_sec * 1000000 + tv.tv_usec) ^ getpid());\n\n    /* In order to call the loading functions we need to create the shared\n     * integer objects, however since this function may be called from\n     * an already initialized Redis instance, check if we really need to. */\n    if (shared.integers[0] == NULL)\n        createSharedObjects();\n    g_pserver->loading_process_events_interval_bytes = 0;\n    g_pserver->loading_process_events_interval_keys = 0;\n    cserver.sanitize_dump_payload = SANITIZE_DUMP_YES;\n    rdbCheckMode = 1;\n    rdbCheckInfo(\"Checking RDB file %s\", argv[1]);\n    rdbCheckSetupSignals();\n    int retval = redis_check_rdb(argv[1],fp);\n    if (retval == 0) {\n        rdbCheckInfo(\"\\\\o/ RDB looks OK! \\\\o/\");\n        rdbShowGenericInfo();\n    }\n    if (fp) return (retval == 0) ? C_OK : C_ERR;\n    exit(retval);\n}\n"
  },
  {
    "path": "src/redis-cli-cpphelper.cpp",
    "content": "#include \"fmacros.h\"\n#include \"version.h\"\n\n#include <stdio.h>\n#include <string.h>\n#include <stdlib.h>\n#include <signal.h>\n#include <unistd.h>\n#include <time.h>\n#include <ctype.h>\n#include <errno.h>\n#include <sys/stat.h>\n#include <sys/time.h>\n#include <assert.h>\n#include <fcntl.h>\n#include <limits.h>\n#include <math.h>\n\nextern \"C\" {\n#include <hiredis.h>\n#include <sds.h> /* use sds.h from hiredis, so that only one set of sds functions will be present in the binary */\n}\n#include \"dict.h\"\n#include \"adlist.h\"\n#include \"zmalloc.h\"\n#include \"storage.h\"\n\n#include \"redis-cli.h\"\n\nstatic dict *clusterManagerGetLinkStatus(void);\nstatic clusterManagerNode *clusterManagerNodeMasterRandom();\n\n/* Used by clusterManagerFixSlotsCoverage */\nstruct dict *clusterManagerUncoveredSlots = NULL;\n\n/* The Cluster Manager global structure */\nstruct clusterManager cluster_manager;\n\nextern \"C\" uint64_t dictSdsHash(const void *key) {\n    return dictGenHashFunction((unsigned char*)key, sdslen((char*)key));\n}\n\nextern \"C\" int dictSdsKeyCompare(void *privdata, const void *key1,\n        const void *key2)\n{\n    int l1,l2;\n    DICT_NOTUSED(privdata);\n\n    l1 = sdslen((sds)key1);\n    l2 = sdslen((sds)key2);\n    if (l1 != l2) return 0;\n    return memcmp(key1, key2, l1) == 0;\n}\n\nextern \"C\" void dictSdsDestructor(void *privdata, void *val)\n{\n    DICT_NOTUSED(privdata);\n    sdsfree((sds)val);\n}\n\nextern \"C\" void dictListDestructor(void *privdata, void *val)\n{\n    DICT_NOTUSED(privdata);\n    listRelease((list*)val);\n}\n\nstatic dictType clusterManagerDictType = {\n    dictSdsHash,               /* hash function */\n    NULL,                      /* key dup */\n    NULL,                      /* val dup */\n    dictSdsKeyCompare,         /* key compare */\n    NULL,                      /* key destructor */\n    dictSdsDestructor,         /* val destructor */\n    NULL                       /* allow to expand */\n};\n\nstatic dictType clusterManagerLinkDictType = {\n    dictSdsHash,               /* hash function */\n    NULL,                      /* key dup */\n    NULL,                      /* val dup */\n    dictSdsKeyCompare,         /* key compare */\n    dictSdsDestructor,         /* key destructor */\n    dictListDestructor,        /* val destructor */\n    NULL                       /* allow to expand */\n};\n\n\nextern \"C\" void freeClusterManager(void) {\n    listIter li;\n    listNode *ln;\n    if (cluster_manager.nodes != NULL) {\n        listRewind(cluster_manager.nodes,&li);\n        while ((ln = listNext(&li)) != NULL) {\n            clusterManagerNode *n = (clusterManagerNode*)ln->value;\n            freeClusterManagerNode(n);\n        }\n        listRelease(cluster_manager.nodes);\n        cluster_manager.nodes = NULL;\n    }\n    if (cluster_manager.errors != NULL) {\n        listRewind(cluster_manager.errors,&li);\n        while ((ln = listNext(&li)) != NULL) {\n            sds err = (sds)ln->value;\n            sdsfree(err);\n        }\n        listRelease(cluster_manager.errors);\n        cluster_manager.errors = NULL;\n    }\n    if (clusterManagerUncoveredSlots != NULL)\n        dictRelease(clusterManagerUncoveredSlots);\n}\n\n/* This function returns a random master node, return NULL if none */\nstatic clusterManagerNode *clusterManagerNodeMasterRandom() {\n    int master_count = 0;\n    int idx;\n    listIter li;\n    listNode *ln;\n    listRewind(cluster_manager.nodes, &li);\n    while ((ln = listNext(&li)) != NULL) {\n        clusterManagerNode *n = (clusterManagerNode*) ln->value;\n        if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE) continue;\n        master_count++;\n    }\n\n    srand(time(NULL));\n    idx = rand() % master_count;\n    listRewind(cluster_manager.nodes, &li);\n    while ((ln = listNext(&li)) != NULL) {\n        clusterManagerNode *n = (clusterManagerNode*) ln->value;\n        if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE) continue;\n        if (!idx--) {\n            return n;\n        }\n    }\n    /* Can not be reached */\n    return NULL;\n}\n\nstatic int clusterManagerFixSlotsCoverage(char *all_slots) {\n    dictIterator *iter = nullptr;\n    int force_fix = config.cluster_manager_command.flags &\n                    CLUSTER_MANAGER_CMD_FLAG_FIX_WITH_UNREACHABLE_MASTERS;\n\n    if (cluster_manager.unreachable_masters > 0 && !force_fix) {\n        clusterManagerLogWarn(\"*** Fixing slots coverage with %d unreachable masters is dangerous: redis-cli will assume that slots about masters that are not reachable are not covered, and will try to reassign them to the reachable nodes. This can cause data loss and is rarely what you want to do. If you really want to proceed use the --cluster-fix-with-unreachable-masters option.\\n\", cluster_manager.unreachable_masters);\n        exit(1);\n    }\n\n    /* we want explicit manual confirmation from users for all the fix cases */\n    int ignore_force = 1;\n\n    int i, fixed = 0;\n    list *none = NULL, *single = NULL, *multi = NULL;\n    clusterManagerLogInfo(\">>> Fixing slots coverage...\\n\");\n    for (i = 0; i < CLUSTER_MANAGER_SLOTS; i++) {\n        int covered = all_slots[i];\n        if (!covered) {\n            sds slot = sdsfromlonglong((long long) i);\n            list *slot_nodes = listCreate();\n            sds slot_nodes_str = sdsempty();\n            listIter li;\n            listNode *ln;\n            listRewind(cluster_manager.nodes, &li);\n            while ((ln = listNext(&li)) != NULL) {\n                clusterManagerNode *n = (clusterManagerNode*) ln->value;\n                if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE || n->replicate)\n                    continue;\n                redisReply *reply = (redisReply*)CLUSTER_MANAGER_COMMAND(n,\n                    \"CLUSTER GETKEYSINSLOT %d %d\", i, 1);\n                if (!clusterManagerCheckRedisReply(n, reply, NULL)) {\n                    fixed = -1;\n                    if (reply) freeReplyObject(reply);\n                    goto cleanup;\n                }\n                assert(reply->type == REDIS_REPLY_ARRAY);\n                if (reply->elements > 0) {\n                    listAddNodeTail(slot_nodes, n);\n                    if (listLength(slot_nodes) > 1)\n                        slot_nodes_str = sdscat(slot_nodes_str, \", \");\n                    slot_nodes_str = sdscatfmt(slot_nodes_str,\n                                               \"%s:%u\", n->ip, n->port);\n                }\n                freeReplyObject(reply);\n            }\n            sdsfree(slot_nodes_str);\n            dictAdd(clusterManagerUncoveredSlots, slot, slot_nodes);\n        }\n    }\n\n    /* For every slot, take action depending on the actual condition:\n     * 1) No node has keys for this slot.\n     * 2) A single node has keys for this slot.\n     * 3) Multiple nodes have keys for this slot. */\n    none = listCreate();\n    single = listCreate();\n    multi = listCreate();\n    iter = dictGetIterator(clusterManagerUncoveredSlots);\n    dictEntry *entry;\n    while ((entry = dictNext(iter)) != NULL) {\n        sds slot = (sds) dictGetKey(entry);\n        list *nodes = (list *) dictGetVal(entry);\n        switch (listLength(nodes)){\n        case 0: listAddNodeTail(none, slot); break;\n        case 1: listAddNodeTail(single, slot); break;\n        default: listAddNodeTail(multi, slot); break;\n        }\n    }\n    dictReleaseIterator(iter);\n\n    /*  Handle case \"1\": keys in no node. */\n    if (listLength(none) > 0) {\n        printf(\"The following uncovered slots have no keys \"\n               \"across the cluster:\\n\");\n        clusterManagerPrintSlotsList(none);\n        if (confirmWithYes(\"Fix these slots by covering with a random node?\",\n                           ignore_force)) {\n            listIter li;\n            listNode *ln;\n            listRewind(none, &li);\n            while ((ln = listNext(&li)) != NULL) {\n                sds slot = (sds)ln->value;\n                int s = atoi(slot);\n                clusterManagerNode *n = clusterManagerNodeMasterRandom();\n                clusterManagerLogInfo(\">>> Covering slot %s with %s:%d\\n\",\n                                      slot, n->ip, n->port);\n                if (!clusterManagerSetSlotOwner(n, s, 0)) {\n                    fixed = -1;\n                    goto cleanup;\n                }\n                /* Since CLUSTER ADDSLOTS succeeded, we also update the slot\n                 * info into the node struct, in order to keep it synced */\n                n->slots[s] = 1;\n                fixed++;\n            }\n        }\n    }\n\n    /*  Handle case \"2\": keys only in one node. */\n    if (listLength(single) > 0) {\n        printf(\"The following uncovered slots have keys in just one node:\\n\");\n        clusterManagerPrintSlotsList(single);\n        if (confirmWithYes(\"Fix these slots by covering with those nodes?\",\n                           ignore_force)) {\n            listIter li;\n            listNode *ln;\n            listRewind(single, &li);\n            while ((ln = listNext(&li)) != NULL) {\n                sds slot = (sds)ln->value;\n                int s = atoi(slot);\n                dictEntry *entry = dictFind(clusterManagerUncoveredSlots, slot);\n                assert(entry != NULL);\n                list *nodes = (list *) dictGetVal(entry);\n                listNode *fn = listFirst(nodes);\n                assert(fn != NULL);\n                clusterManagerNode *n = (clusterManagerNode*) fn->value;\n                clusterManagerLogInfo(\">>> Covering slot %s with %s:%d\\n\",\n                                      slot, n->ip, n->port);\n                if (!clusterManagerSetSlotOwner(n, s, 0)) {\n                    fixed = -1;\n                    goto cleanup;\n                }\n                /* Since CLUSTER ADDSLOTS succeeded, we also update the slot\n                 * info into the node struct, in order to keep it synced */\n                n->slots[atoi(slot)] = 1;\n                fixed++;\n            }\n        }\n    }\n\n    /* Handle case \"3\": keys in multiple nodes. */\n    if (listLength(multi) > 0) {\n        printf(\"The following uncovered slots have keys in multiple nodes:\\n\");\n        clusterManagerPrintSlotsList(multi);\n        if (confirmWithYes(\"Fix these slots by moving keys \"\n                           \"into a single node?\", ignore_force)) {\n            listIter li;\n            listNode *ln;\n            listRewind(multi, &li);\n            while ((ln = listNext(&li)) != NULL) {\n                sds slot = (sds)ln->value;\n                dictEntry *entry = dictFind(clusterManagerUncoveredSlots, slot);\n                assert(entry != NULL);\n                list *nodes = (list *) dictGetVal(entry);\n                int s = atoi(slot);\n                clusterManagerNode *target =\n                    clusterManagerGetNodeWithMostKeysInSlot(nodes, s, NULL);\n                if (target == NULL) {\n                    fixed = -1;\n                    goto cleanup;\n                }\n                clusterManagerLogInfo(\">>> Covering slot %s moving keys \"\n                                      \"to %s:%d\\n\", slot,\n                                      target->ip, target->port);\n                if (!clusterManagerSetSlotOwner(target, s, 1)) {\n                    fixed = -1;\n                    goto cleanup;\n                }\n                /* Since CLUSTER ADDSLOTS succeeded, we also update the slot\n                 * info into the node struct, in order to keep it synced */\n                target->slots[atoi(slot)] = 1;\n                listIter nli;\n                listNode *nln;\n                listRewind(nodes, &nli);\n                while ((nln = listNext(&nli)) != NULL) {\n                    clusterManagerNode *src = (clusterManagerNode*) nln->value;\n                    if (src == target) continue;\n                    /* Assign the slot to target node in the source node. */\n                    if (!clusterManagerSetSlot(src, target, s, \"NODE\", NULL))\n                        fixed = -1;\n                    if (fixed < 0) goto cleanup;\n                    /* Set the source node in 'importing' state\n                     * (even if we will actually migrate keys away)\n                     * in order to avoid receiving redirections\n                     * for MIGRATE. */\n                    if (!clusterManagerSetSlot(src, target, s,\n                                               \"IMPORTING\", NULL)) fixed = -1;\n                    if (fixed < 0) goto cleanup;\n                    int opts = CLUSTER_MANAGER_OPT_VERBOSE |\n                               CLUSTER_MANAGER_OPT_COLD;\n                    if (!clusterManagerMoveSlot(src, target, s, opts, NULL)) {\n                        fixed = -1;\n                        goto cleanup;\n                    }\n                    if (!clusterManagerClearSlotStatus(src, s))\n                        fixed = -1;\n                    if (fixed < 0) goto cleanup;\n                }\n                fixed++;\n            }\n        }\n    }\ncleanup:\n    if (none) listRelease(none);\n    if (single) listRelease(single);\n    if (multi) listRelease(multi);\n    return fixed;\n}\n\n\n/* Return the anti-affinity score, which is a measure of the amount of\n * violations of anti-affinity in the current cluster layout, that is, how\n * badly the masters and slaves are distributed in the different IP\n * addresses so that slaves of the same master are not in the master\n * host and are also in different hosts.\n *\n * The score is calculated as follows:\n *\n * SAME_AS_MASTER = 10000 * each replica in the same IP of its master.\n * SAME_AS_SLAVE  = 1 * each replica having the same IP as another replica\n                      of the same master.\n * FINAL_SCORE = SAME_AS_MASTER + SAME_AS_SLAVE\n *\n * So a greater score means a worse anti-affinity level, while zero\n * means perfect anti-affinity.\n *\n * The anti affinity optimizator will try to get a score as low as\n * possible. Since we do not want to sacrifice the fact that slaves should\n * not be in the same host as the master, we assign 10000 times the score\n * to this violation, so that we'll optimize for the second factor only\n * if it does not impact the first one.\n *\n * The ipnodes argument is an array of clusterManagerNodeArray, one for\n * each IP, while ip_count is the total number of IPs in the configuration.\n *\n * The function returns the above score, and the list of\n * offending slaves can be stored into the 'offending' argument,\n * so that the optimizer can try changing the configuration of the\n * slaves violating the anti-affinity goals. */\nint clusterManagerGetAntiAffinityScore(clusterManagerNodeArray *ipnodes,\n    int ip_count, clusterManagerNode ***offending, int *offending_len)\n{\n    int score = 0, i, j;\n    int node_len = cluster_manager.nodes->len;\n    clusterManagerNode **offending_p = NULL;\n    if (offending != NULL) {\n        *offending = (clusterManagerNode**)zcalloc(node_len * sizeof(clusterManagerNode*), MALLOC_LOCAL);\n        offending_p = *offending;\n    }\n    /* For each set of nodes in the same host, split by\n     * related nodes (masters and slaves which are involved in\n     * replication of each other) */\n    for (i = 0; i < ip_count; i++) {\n        clusterManagerNodeArray *node_array = &(ipnodes[i]);\n        dict *related = dictCreate(&clusterManagerDictType, NULL);\n        char *ip = NULL;\n        for (j = 0; j < node_array->len; j++) {\n            clusterManagerNode *node = node_array->nodes[j];\n            if (node == NULL) continue;\n            if (!ip) ip = node->ip;\n            sds types;\n            /* We always use the Master ID as key. */\n            sds key = (!node->replicate ? node->name : node->replicate);\n            assert(key != NULL);\n            dictEntry *entry = dictFind(related, key);\n            if (entry) types = sdsdup((sds) dictGetVal(entry));\n            else types = sdsempty();\n            /* Master type 'm' is always set as the first character of the\n             * types string. */\n            if (node->replicate) types = sdscat(types, \"s\");\n            else {\n                sds s = sdscatsds(sdsnew(\"m\"), types);\n                sdsfree(types);\n                types = s;\n            }\n            dictReplace(related, key, types);\n        }\n        /* Now it's trivial to check, for each related group having the\n         * same host, what is their local score. */\n        dictIterator *iter = dictGetIterator(related);\n        dictEntry *entry;\n        while ((entry = dictNext(iter)) != NULL) {\n            sds types = (sds) dictGetVal(entry);\n            sds name = (sds) dictGetKey(entry);\n            int typeslen = sdslen(types);\n            if (typeslen < 2) continue;\n            if (types[0] == 'm') score += (10000 * (typeslen - 1));\n            else score += (1 * typeslen);\n            if (offending == NULL) continue;\n            /* Populate the list of offending nodes. */\n            listIter li;\n            listNode *ln;\n            listRewind(cluster_manager.nodes, &li);\n            while ((ln = listNext(&li)) != NULL) {\n                clusterManagerNode *n = (clusterManagerNode*)ln->value;\n                if (n->replicate == NULL) continue;\n                if (!strcmp(n->replicate, name) && !strcmp(n->ip, ip)) {\n                    *(offending_p++) = n;\n                    if (offending_len != NULL) (*offending_len)++;\n                    break;\n                }\n            }\n        }\n        if (offending_len != NULL) *offending_len = offending_p - *offending;\n        dictReleaseIterator(iter);\n        dictRelease(related);\n    }\n    return score;\n}\n\n\n/* Wait until the cluster configuration is consistent. */\nextern \"C\" void clusterManagerWaitForClusterJoin(void) {\n    printf(\"Waiting for the cluster to join\\n\");\n    int counter = 0,\n        check_after = CLUSTER_JOIN_CHECK_AFTER +\n                      (int)(listLength(cluster_manager.nodes) * 0.15f);\n    while(!clusterManagerIsConfigConsistent(0 /*fLog*/)) {\n        printf(\".\");\n        fflush(stdout);\n        sleep(1);\n        if (++counter > check_after) {\n            dict *status = clusterManagerGetLinkStatus();\n            dictIterator *iter = NULL;\n            if (status != NULL && dictSize(status) > 0) {\n                printf(\"\\n\");\n                clusterManagerLogErr(\"Warning: %d node(s) may \"\n                                     \"be unreachable\\n\", dictSize(status));\n                iter = dictGetIterator(status);\n                dictEntry *entry;\n                while ((entry = dictNext(iter)) != NULL) {\n                    sds nodeaddr = (sds) dictGetKey(entry);\n                    char *node_ip = NULL;\n                    int node_port = 0, node_bus_port = 0;\n                    list *from = (list *) dictGetVal(entry);\n                    if (parseClusterNodeAddress(nodeaddr, &node_ip,\n                        &node_port, &node_bus_port) && node_bus_port) {\n                        clusterManagerLogErr(\" - The port %d of node %s may \"\n                                             \"be unreachable from:\\n\",\n                                             node_bus_port, node_ip);\n                    } else {\n                        clusterManagerLogErr(\" - Node %s may be unreachable \"\n                                             \"from:\\n\", nodeaddr);\n                    }\n                    listIter li;\n                    listNode *ln;\n                    listRewind(from, &li);\n                    while ((ln = listNext(&li)) != NULL) {\n                        sds from_addr = (sds)ln->value;\n                        clusterManagerLogErr(\"   %s\\n\", from_addr);\n                        sdsfree(from_addr);\n                    }\n                    clusterManagerLogErr(\"Cluster bus ports must be reachable \"\n                                         \"by every node.\\nRemember that \"\n                                         \"cluster bus ports are different \"\n                                         \"from standard instance ports.\\n\");\n                    listEmpty(from);\n                }\n            }\n            if (iter != NULL) dictReleaseIterator(iter);\n            if (status != NULL) dictRelease(status);\n            counter = 0;\n        }\n    }\n    printf(\"\\n\");\n}\n\nlist *clusterManagerGetDisconnectedLinks(clusterManagerNode *node) {\n    list *links = NULL;\n    char *lines = NULL, *p, *line;\n    redisReply *reply = (redisReply*)CLUSTER_MANAGER_COMMAND(node, \"CLUSTER NODES\");\n    if (!clusterManagerCheckRedisReply(node, reply, NULL)) goto cleanup;\n    links = listCreate();\n    lines = reply->str;\n    while ((p = strstr(lines, \"\\n\")) != NULL) {\n        int i = 0;\n        *p = '\\0';\n        line = lines;\n        lines = p + 1;\n        char *nodename = NULL, *addr = NULL, *flags = NULL, *link_status = NULL;\n        while ((p = strchr(line, ' ')) != NULL) {\n            *p = '\\0';\n            char *token = line;\n            line = p + 1;\n            if (i == 0) nodename = token;\n            else if (i == 1) addr = token;\n            else if (i == 2) flags = token;\n            else if (i == 7) link_status = token;\n            else if (i == 8) break;\n            i++;\n        }\n        if (i == 7) link_status = line;\n        if (nodename == NULL || addr == NULL || flags == NULL ||\n            link_status == NULL) continue;\n        if (strstr(flags, \"myself\") != NULL) continue;\n        int disconnected = ((strstr(flags, \"disconnected\") != NULL) ||\n                            (strstr(link_status, \"disconnected\")));\n        int handshaking = (strstr(flags, \"handshake\") != NULL);\n        if (disconnected || handshaking) {\n            clusterManagerLink *link = (clusterManagerLink*)zmalloc(sizeof(*link), MALLOC_LOCAL);\n            link->node_name = sdsnew(nodename);\n            link->node_addr = sdsnew(addr);\n            link->connected = 0;\n            link->handshaking = handshaking;\n            listAddNodeTail(links, link);\n        }\n    }\ncleanup:\n    if (reply != NULL) freeReplyObject(reply);\n    return links;\n}\n\n/* Check for disconnected cluster links. It returns a dict whose keys\n * are the unreachable node addresses and the values are lists of\n * node addresses that cannot reach the unreachable node. */\ndict *clusterManagerGetLinkStatus(void) {\n    if (cluster_manager.nodes == NULL) return NULL;\n    dict *status = dictCreate(&clusterManagerLinkDictType, NULL);\n    listIter li;\n    listNode *ln;\n    listRewind(cluster_manager.nodes, &li);\n    while ((ln = listNext(&li)) != NULL) {\n        clusterManagerNode *node = (clusterManagerNode*)ln->value;\n        list *links = clusterManagerGetDisconnectedLinks(node);\n        if (links) {\n            listIter lli;\n            listNode *lln;\n            listRewind(links, &lli);\n            while ((lln = listNext(&lli)) != NULL) {\n                clusterManagerLink *link = (clusterManagerLink*)lln->value;\n                list *from = NULL;\n                dictEntry *entry = dictFind(status, link->node_addr);\n                if (entry) from = (list*)dictGetVal(entry);\n                else {\n                    from = listCreate();\n                    dictAdd(status, sdsdup(link->node_addr), from);\n                }\n                sds myaddr = sdsempty();\n                myaddr = sdscatfmt(myaddr, \"%s:%u\", node->ip, node->port);\n                listAddNodeTail(from, myaddr);\n                sdsfree(link->node_name);\n                sdsfree(link->node_addr);\n                zfree(link);\n            }\n            listRelease(links);\n        }\n    }\n    return status;\n}\n\nextern \"C\" int clusterManagerCheckCluster(int quiet) {\n    listNode *ln = listFirst(cluster_manager.nodes);\n    if (!ln) return 0;\n    clusterManagerNode *node = (clusterManagerNode*)ln->value;\n    clusterManagerLogInfo(\">>> Performing Cluster Check (using node %s:%d)\\n\",\n                          node->ip, node->port);\n    int result = 1, consistent = 0;\n    int do_fix = config.cluster_manager_command.flags &\n                 CLUSTER_MANAGER_CMD_FLAG_FIX;\n    if (!quiet) clusterManagerShowNodes();\n    consistent = clusterManagerIsConfigConsistent(1 /*fLog*/);\n    if (!consistent) {\n        sds err = sdsnew(\"[ERR] Nodes don't agree about configuration!\");\n        clusterManagerOnError(err);\n        result = 0;\n    } else {\n        clusterManagerLogOk(\"[OK] All nodes agree about slots \"\n                            \"configuration.\\n\");\n    }\n    /* Check open slots */\n    clusterManagerLogInfo(\">>> Check for open slots...\\n\");\n    listIter li;\n    listRewind(cluster_manager.nodes, &li);\n    int i;\n    dict *open_slots = NULL;\n    while ((ln = listNext(&li)) != NULL) {\n        clusterManagerNode *n = (clusterManagerNode*)ln->value;\n        if (n->migrating != NULL) {\n            if (open_slots == NULL)\n                open_slots = dictCreate(&clusterManagerDictType, NULL);\n            sds errstr = sdsempty();\n            errstr = sdscatprintf(errstr,\n                                \"[WARNING] Node %s:%d has slots in \"\n                                \"migrating state \",\n                                n->ip,\n                                n->port);\n            for (i = 0; i < n->migrating_count; i += 2) {\n                sds slot = n->migrating[i];\n                dictReplace(open_slots, slot, sdsdup(n->migrating[i + 1]));\n                const char *fmt = (i > 0 ? \",%S\" : \"%S\");\n                errstr = sdscatfmt(errstr, fmt, slot);\n            }\n            errstr = sdscat(errstr, \".\");\n            clusterManagerOnError(errstr);\n        }\n        if (n->importing != NULL) {\n            if (open_slots == NULL)\n                open_slots = dictCreate(&clusterManagerDictType, NULL);\n            sds errstr = sdsempty();\n            errstr = sdscatprintf(errstr,\n                                \"[WARNING] Node %s:%d has slots in \"\n                                \"importing state \",\n                                n->ip,\n                                n->port);\n            for (i = 0; i < n->importing_count; i += 2) {\n                sds slot = n->importing[i];\n                dictReplace(open_slots, slot, sdsdup(n->importing[i + 1]));\n                const char *fmt = (i > 0 ? \",%S\" : \"%S\");\n                errstr = sdscatfmt(errstr, fmt, slot);\n            }\n            errstr = sdscat(errstr, \".\");\n            clusterManagerOnError(errstr);\n        }\n    }\n    if (open_slots != NULL) {\n        result = 0;\n        dictIterator *iter = dictGetIterator(open_slots);\n        dictEntry *entry;\n        sds errstr = sdsnew(\"[WARNING] The following slots are open: \");\n        i = 0;\n        while ((entry = dictNext(iter)) != NULL) {\n            sds slot = (sds) dictGetKey(entry);\n            const char *fmt = (i++ > 0 ? \",%S\" : \"%S\");\n            errstr = sdscatfmt(errstr, fmt, slot);\n        }\n        clusterManagerLogErr(\"%s.\\n\", (char *) errstr);\n        sdsfree(errstr);\n        if (do_fix) {\n            /* Fix open slots. */\n            dictReleaseIterator(iter);\n            iter = dictGetIterator(open_slots);\n            while ((entry = dictNext(iter)) != NULL) {\n                sds slot = (sds) dictGetKey(entry);\n                result = clusterManagerFixOpenSlot(atoi(slot));\n                if (!result) break;\n            }\n        }\n        dictReleaseIterator(iter);\n        dictRelease(open_slots);\n    }\n    clusterManagerLogInfo(\">>> Check slots coverage...\\n\");\n    char slots[CLUSTER_MANAGER_SLOTS];\n    memset(slots, 0, CLUSTER_MANAGER_SLOTS);\n    int coverage = clusterManagerGetCoveredSlots(slots);\n    if (coverage == CLUSTER_MANAGER_SLOTS) {\n        clusterManagerLogOk(\"[OK] All %d slots covered.\\n\",\n                            CLUSTER_MANAGER_SLOTS);\n    } else {\n        sds err = sdsempty();\n        err = sdscatprintf(err, \"[ERR] Not all %d slots are \"\n                                \"covered by nodes.\\n\",\n                                CLUSTER_MANAGER_SLOTS);\n        clusterManagerOnError(err);\n        result = 0;\n        if (do_fix/* && result*/) {\n            dictType dtype = clusterManagerDictType;\n            dtype.keyDestructor = dictSdsDestructor;\n            dtype.valDestructor = dictListDestructor;\n            clusterManagerUncoveredSlots = dictCreate(&dtype, NULL);\n            int fixed = clusterManagerFixSlotsCoverage(slots);\n            if (fixed > 0) result = 1;\n        }\n    }\n    int search_multiple_owners = config.cluster_manager_command.flags &\n                                 CLUSTER_MANAGER_CMD_FLAG_CHECK_OWNERS;\n    if (search_multiple_owners) {\n        /* Check whether there are multiple owners, even when slots are\n         * fully covered and there are no open slots. */\n        clusterManagerLogInfo(\">>> Check for multiple slot owners...\\n\");\n        int slot = 0, slots_with_multiple_owners = 0;\n        for (; slot < CLUSTER_MANAGER_SLOTS; slot++) {\n            listIter li;\n            listNode *ln;\n            listRewind(cluster_manager.nodes, &li);\n            list *owners = listCreate();\n            while ((ln = listNext(&li)) != NULL) {\n                clusterManagerNode *n = (clusterManagerNode*)ln->value;\n                if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE) continue;\n                if (n->slots[slot]) listAddNodeTail(owners, n);\n                else {\n                    /* Nodes having keys for the slot will be considered\n                     * owners too. */\n                    int count = clusterManagerCountKeysInSlot(n, slot);\n                    if (count > 0) listAddNodeTail(owners, n);\n                }\n            }\n            if (listLength(owners) > 1) {\n                result = 0;\n                clusterManagerLogErr(\"[WARNING] Slot %d has %d owners:\\n\",\n                                     slot, listLength(owners));\n                listRewind(owners, &li);\n                while ((ln = listNext(&li)) != NULL) {\n                    clusterManagerNode *n = (clusterManagerNode*)ln->value;\n                    clusterManagerLogErr(\"    %s:%d\\n\", n->ip, n->port);\n                }\n                slots_with_multiple_owners++;\n                if (do_fix) {\n                    result = clusterManagerFixMultipleSlotOwners(slot, owners);\n                    if (!result) {\n                        clusterManagerLogErr(\"Failed to fix multiple owners \"\n                                             \"for slot %d\\n\", slot);\n                        listRelease(owners);\n                        break;\n                    } else slots_with_multiple_owners--;\n                }\n            }\n            listRelease(owners);\n        }\n        if (slots_with_multiple_owners == 0)\n            clusterManagerLogOk(\"[OK] No multiple owners found.\\n\");\n    }\n    return result;\n}\n\nstatic typeinfo* typeinfo_add(dict *types, const char* name, typeinfo* type_template) {\n    typeinfo *info = (typeinfo*)zmalloc(sizeof(typeinfo), MALLOC_LOCAL);\n    *info = *type_template;\n    info->name = sdsnew(name);\n    dictAdd(types, info->name, info);\n    return info;\n}\n\nstatic dictType typeinfoDictType = {\n    dictSdsHash,               /* hash function */\n    NULL,                      /* key dup */\n    NULL,                      /* val dup */\n    dictSdsKeyCompare,         /* key compare */\n    NULL,                      /* key destructor (owned by the value)*/\n    type_free,                 /* val destructor */\n    NULL                       /* allow to expand */\n};\n\nstatic void getKeyTypes(dict *types_dict, redisReply *keys, typeinfo **types) {\n    redisReply *reply;\n    unsigned int i;\n\n    /* Pipeline TYPE commands */\n    for(i=0;i<keys->elements;i++) {\n        const char* argv[] = {\"TYPE\", keys->element[i]->str};\n        size_t lens[] = {4, keys->element[i]->len};\n        redisAppendCommandArgv(context, 2, argv, lens);\n    }\n\n    /* Retrieve types */\n    for(i=0;i<keys->elements;i++) {\n        if(redisGetReply(context, (void**)&reply)!=REDIS_OK) {\n            fprintf(stderr, \"Error getting type for key '%s' (%d: %s)\\n\",\n                keys->element[i]->str, context->err, context->errstr);\n            exit(1);\n        } else if(reply->type != REDIS_REPLY_STATUS) {\n            if(reply->type == REDIS_REPLY_ERROR) {\n                fprintf(stderr, \"TYPE returned an error: %s\\n\", reply->str);\n            } else {\n                fprintf(stderr,\n                    \"Invalid reply type (%d) for TYPE on key '%s'!\\n\",\n                    reply->type, keys->element[i]->str);\n            }\n            exit(1);\n        }\n\n        sds typereply = sdsnew(reply->str);\n        dictEntry *de = dictFind(types_dict, typereply);\n        sdsfree(typereply);\n        typeinfo *type = NULL;\n        if (de)\n            type = (typeinfo*)dictGetVal(de);\n        else if (strcmp(reply->str, \"none\")) /* create new types for modules, (but not for deleted keys) */\n            type = typeinfo_add(types_dict, reply->str, &type_other);\n        types[i] = type;\n        freeReplyObject(reply);\n    }\n}\n\nvoid findBigKeys(int memkeys, unsigned memkeys_samples) {\n    unsigned long long sampled = 0, total_keys, totlen=0, *sizes=NULL, it=0;\n    redisReply *reply, *keys;\n    unsigned int arrsize=0, i;\n    dictIterator *di;\n    dictEntry *de;\n    typeinfo **types = NULL;\n    double pct;\n\n    dict *types_dict = dictCreate(&typeinfoDictType, NULL);\n    typeinfo_add(types_dict, \"string\", &type_string);\n    typeinfo_add(types_dict, \"list\", &type_list);\n    typeinfo_add(types_dict, \"set\", &type_set);\n    typeinfo_add(types_dict, \"hash\", &type_hash);\n    typeinfo_add(types_dict, \"zset\", &type_zset);\n    typeinfo_add(types_dict, \"stream\", &type_stream);\n\n    /* Total keys pre scanning */\n    total_keys = getDbSize();\n\n    /* Status message */\n    printf(\"\\n# Scanning the entire keyspace to find biggest keys as well as\\n\");\n    printf(\"# average sizes per key type.  You can use -i 0.1 to sleep 0.1 sec\\n\");\n    printf(\"# per 100 SCAN commands (not usually needed).\\n\\n\");\n\n    /* SCAN loop */\n    do {\n        /* Calculate approximate percentage completion */\n        pct = 100 * (double)sampled/total_keys;\n\n        /* Grab some keys and point to the keys array */\n        reply = sendScan(&it);\n        keys  = reply->element[1];\n\n        /* Reallocate our type and size array if we need to */\n        if(keys->elements > arrsize) {\n            types = (typeinfo**)zrealloc(types, sizeof(typeinfo*)*keys->elements);\n            sizes = (unsigned long long*)zrealloc(sizes, sizeof(unsigned long long)*keys->elements);\n\n            if(!types || !sizes) {\n                fprintf(stderr, \"Failed to allocate storage for keys!\\n\");\n                exit(1);\n            }\n\n            arrsize = keys->elements;\n        }\n\n        /* Retrieve types and then sizes */\n        getKeyTypes(types_dict, keys, types);\n        getKeySizes(keys, types, sizes, memkeys, memkeys_samples);\n\n        /* Now update our stats */\n        for(i=0;i<keys->elements;i++) {\n            typeinfo *type = types[i];\n            /* Skip keys that disappeared between SCAN and TYPE */\n            if(!type)\n                continue;\n\n            type->totalsize += sizes[i];\n            type->count++;\n            totlen += keys->element[i]->len;\n            sampled++;\n\n            if(type->biggest<sizes[i]) {\n                /* Keep track of biggest key name for this type */\n                if (type->biggest_key)\n                    sdsfree(type->biggest_key);\n                type->biggest_key = sdscatrepr(sdsempty(), keys->element[i]->str, keys->element[i]->len);\n                if(!type->biggest_key) {\n                    fprintf(stderr, \"Failed to allocate memory for key!\\n\");\n                    exit(1);\n                }\n\n                printf(\n                   \"[%05.2f%%] Biggest %-6s found so far '%s' with %llu %s\\n\",\n                   pct, type->name, type->biggest_key, sizes[i],\n                   !memkeys? type->sizeunit: \"bytes\");\n\n                /* Keep track of the biggest size for this type */\n                type->biggest = sizes[i];\n            }\n\n            /* Update overall progress */\n            if(sampled % 1000000 == 0) {\n                printf(\"[%05.2f%%] Sampled %llu keys so far\\n\", pct, sampled);\n            }\n        }\n\n        /* Sleep if we've been directed to do so */\n        if(sampled && (sampled %100) == 0 && config.interval) {\n            usleep(config.interval);\n        }\n\n        freeReplyObject(reply);\n    } while(it != 0);\n\n    if(types) zfree(types);\n    if(sizes) zfree(sizes);\n\n    /* We're done */\n    printf(\"\\n-------- summary -------\\n\\n\");\n\n    printf(\"Sampled %llu keys in the keyspace!\\n\", sampled);\n    printf(\"Total key length in bytes is %llu (avg len %.2f)\\n\\n\",\n       totlen, totlen ? (double)totlen/sampled : 0);\n\n    /* Output the biggest keys we found, for types we did find */\n    di = dictGetIterator(types_dict);\n    while ((de = dictNext(di))) {\n        typeinfo *type = (typeinfo*)dictGetVal(de);\n        if(type->biggest_key) {\n            printf(\"Biggest %6s found '%s' has %llu %s\\n\", type->name, type->biggest_key,\n               type->biggest, !memkeys? type->sizeunit: \"bytes\");\n        }\n    }\n    dictReleaseIterator(di);\n\n    printf(\"\\n\");\n\n    di = dictGetIterator(types_dict);\n    while ((de = dictNext(di))) {\n        typeinfo *type = (typeinfo*)dictGetVal(de);\n        printf(\"%llu %ss with %llu %s (%05.2f%% of keys, avg size %.2f)\\n\",\n           type->count, type->name, type->totalsize, !memkeys? type->sizeunit: \"bytes\",\n           sampled ? 100 * (double)type->count/sampled : 0,\n           type->count ? (double)type->totalsize/type->count : 0);\n    }\n    dictReleaseIterator(di);\n\n    dictRelease(types_dict);\n\n    /* Success! */\n    exit(0);\n}\n"
  },
  {
    "path": "src/redis-cli.c",
    "content": "/* Redis CLI (command line interface)\n *\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"fmacros.h\"\n#include \"version.h\"\n\n#include <stdio.h>\n#include <string.h>\n#include <stdlib.h>\n#include <signal.h>\n#include <unistd.h>\n#include <time.h>\n#include <ctype.h>\n#include <errno.h>\n#include <sys/stat.h>\n#include <sys/time.h>\n#include <assert.h>\n#include <fcntl.h>\n#include <limits.h>\n#include <math.h>\n#include <sys/types.h>\n#include <pwd.h>\n\n#include <hiredis.h>\n#ifdef USE_OPENSSL\n#include <openssl/ssl.h>\n#include <openssl/err.h>\n#include <hiredis_ssl.h>\n#endif\n#include \"sdscompat.h\" /* Use hiredis' sds compat header that maps sds calls to their hi_ variants */\n#include <sds.h> /* use sds.h from hiredis, so that only one set of sds functions will be present in the binary */\n#include \"adlist.h\"\n#include \"zmalloc.h\"\n#include \"linenoise.h\"\n#include \"help.h\"\n#include \"anet.h\"\n#include \"ae.h\"\n#include \"storage.h\"\n#include \"motd.h\"\n#include \"cli_common.h\"\n#include \"mt19937-64.h\"\n\n#include \"redis-cli.h\"\n\nredisContext *context;\nstruct config config;\n\nint g_fTestMode = 0;\n\n/* User preferences. */\nstatic struct pref {\n    int hints;\n} pref;\n\nstatic volatile sig_atomic_t force_cancel_loop = 0;\nstatic void usage(void);\nstatic void slaveMode(void);\nchar *redisGitSHA1(void);\nchar *redisGitDirty(void);\nstatic int cliConnect(int force);\n\nstatic char *getInfoField(char *info, char *field);\nstatic long getLongInfoField(char *info, char *field);\n\n/* --latency-dist palettes. */\nint spectrum_palette_color_size = 19;\nint spectrum_palette_color[] = {0,233,234,235,237,239,241,243,245,247,144,143,142,184,226,214,208,202,196};\n\nint spectrum_palette_mono_size = 13;\nint spectrum_palette_mono[] = {0,233,234,235,237,239,241,243,245,247,249,251,253};\n\n/* The actual palette in use. */\nint *spectrum_palette;\nint spectrum_palette_size;\n\nint g_fInCrash = 0;\n\nconst char *motd_url = \"http://api.keydb.dev/motd/motd_cli.txt\";\nconst char *motd_cache_file = \"/.keydb-cli-motd\";\n\n/*------------------------------------------------------------------------------\n * Utility functions\n *--------------------------------------------------------------------------- */\n\nstatic void cliPushHandler(void *, void *);\n\nuint16_t crc16(const char *buf, int len);\n\nstatic long long ustime(void) {\n    struct timeval tv;\n    long long ust;\n\n    gettimeofday(&tv, NULL);\n    ust = ((long long)tv.tv_sec)*1000000;\n    ust += tv.tv_usec;\n    return ust;\n}\n\nstatic long long mstime(void) {\n    return ustime()/1000;\n}\n\nstatic void cliRefreshPrompt(void) {\n    if (config.eval_ldb) return;\n\n    sds prompt = sdsempty();\n    if (config.hostsocket != NULL) {\n        prompt = sdscatfmt(prompt,\"redis %s\",config.hostsocket);\n    } else {\n        char addr[256];\n        anetFormatAddr(addr, sizeof(addr), config.hostip, config.hostport);\n        prompt = sdscatlen(prompt,addr,strlen(addr));\n    }\n\n    /* Add [dbnum] if needed */\n    if (config.dbnum != 0)\n        prompt = sdscatfmt(prompt,\"[%i]\",config.dbnum);\n\n    /* Add TX if in transaction state*/\n    if (config.in_multi)  \n        prompt = sdscatlen(prompt,\"(TX)\",4);\n\n    /* Copy the prompt in the static buffer. */\n    prompt = sdscatlen(prompt,\"> \",2);\n    snprintf(config.prompt,sizeof(config.prompt),\"%s\",prompt);\n    sdsfree(prompt);\n}\n\nstruct dictEntry;\nvoid asyncFreeDictTable(struct dictEntry **de) {\n    zfree(de);\n}\n\n/* Return the name of the dotfile for the specified 'dotfilename'.\n * Normally it just concatenates user $HOME to the file specified\n * in 'dotfilename'. However if the environment variable 'envoverride'\n * is set, its value is taken as the path.\n *\n * The function returns NULL (if the file is /dev/null or cannot be\n * obtained for some error), or an SDS string that must be freed by\n * the user. */\nstatic sds getDotfilePath(char *envoverride, char *dotfilename) {\n    char *path = NULL;\n    sds dotPath = NULL;\n\n    /* Check the env for a dotfile override. */\n    path = getenv(envoverride);\n    if (path != NULL && *path != '\\0') {\n        if (!strcmp(\"/dev/null\", path)) {\n            return NULL;\n        }\n\n        /* If the env is set, return it. */\n        dotPath = sdsnew(path);\n    } else {\n        char *home = getenv(\"HOME\");\n        if (home != NULL && *home != '\\0') {\n            /* If no override is set use $HOME/<dotfilename>. */\n            dotPath = sdscatprintf(sdsempty(), \"%s/%s\", home, dotfilename);\n        }\n    }\n    return dotPath;\n}\n\n/* URL-style percent decoding. */\n#define isHexChar(c) (isdigit(c) || (c >= 'a' && c <= 'f'))\n#define decodeHexChar(c) (isdigit(c) ? c - '0' : c - 'a' + 10)\n#define decodeHex(h, l) ((decodeHexChar(h) << 4) + decodeHexChar(l))\n\nstatic sds percentDecode(const char *pe, size_t len) {\n    const char *end = pe + len;\n    sds ret = sdsempty();\n    const char *curr = pe;\n\n    while (curr < end) {\n        if (*curr == '%') {\n            if ((end - curr) < 2) {\n                fprintf(stderr, \"Incomplete URI encoding\\n\");\n                exit(1);\n            }\n\n            char h = tolower(*(++curr));\n            char l = tolower(*(++curr));\n            if (!isHexChar(h) || !isHexChar(l)) {\n                fprintf(stderr, \"Illegal character in URI encoding\\n\");\n                exit(1);\n            }\n            char c = decodeHex(h, l);\n            ret = sdscatlen(ret, &c, 1);\n            curr++;\n        } else {\n            ret = sdscatlen(ret, curr++, 1);\n        }\n    }\n\n    return ret;\n}\n\n/* Parse a URI and extract the server connection information.\n * URI scheme is based on the the provisional specification[1] excluding support\n * for query parameters. Valid URIs are:\n *   scheme:    \"redis://\"\n *   authority: [[<username> \":\"] <password> \"@\"] [<hostname> [\":\" <port>]]\n *   path:      [\"/\" [<db>]]\n *\n *  [1]: https://www.iana.org/assignments/uri-schemes/prov/redis */\nstatic void parseRedisUri(const char *uri) {\n\n    const char *scheme = \"redis://\";\n    const char *tlsscheme = \"rediss://\";\n    const char *curr = uri;\n    const char *end = uri + strlen(uri);\n    const char *userinfo, *username, *port, *host, *path;\n\n    /* URI must start with a valid scheme. */\n    if (!strncasecmp(tlsscheme, curr, strlen(tlsscheme))) {\n#ifdef USE_OPENSSL\n        config.tls = 1;\n        curr += strlen(tlsscheme);\n#else\n        fprintf(stderr,\"rediss:// is only supported when redis-cli is compiled with OpenSSL\\n\");\n        exit(1);\n#endif\n    } else if (!strncasecmp(scheme, curr, strlen(scheme))) {\n        curr += strlen(scheme);\n    } else {\n        fprintf(stderr,\"Invalid URI scheme\\n\");\n        exit(1);\n    }\n    if (curr == end) return;\n\n    /* Extract user info. */\n    if ((userinfo = strchr(curr,'@'))) {\n        if ((username = strchr(curr, ':')) && username < userinfo) {\n            config.user = percentDecode(curr, username - curr);\n            curr = username + 1;\n        }\n\n        config.auth = percentDecode(curr, userinfo - curr);\n        curr = userinfo + 1;\n    }\n    if (curr == end) return;\n\n    /* Extract host and port. */\n    path = strchr(curr, '/');\n    if (*curr != '/') {\n        host = path ? path - 1 : end;\n        if ((port = strchr(curr, ':'))) {\n            config.hostport = atoi(port + 1);\n            host = port - 1;\n        }\n        config.hostip = sdsnewlen(curr, host - curr + 1);\n    }\n    curr = path ? path + 1 : end;\n    if (curr == end) return;\n\n    /* Extract database number. */\n    config.input_dbnum = atoi(curr);\n}\n\n/* _serverAssert is needed by dict */\nvoid _serverAssert(const char *estr, const char *file, int line) {\n    fprintf(stderr, \"=== ASSERTION FAILED ===\");\n    fprintf(stderr, \"==> %s:%d '%s' is not true\",file,line,estr);\n    *((char*)-1) = 'x';\n}\n\n/*------------------------------------------------------------------------------\n * Help functions\n *--------------------------------------------------------------------------- */\n\n#define CLI_HELP_COMMAND 1\n#define CLI_HELP_GROUP 2\n\ntypedef struct {\n    int type;\n    int argc;\n    sds *argv;\n    sds full;\n\n    /* Only used for help on commands */\n    struct commandHelp *org;\n} helpEntry;\n\nstatic helpEntry *helpEntries;\nstatic int helpEntriesLen;\n\nstatic sds cliVersion(void) {\n    sds version;\n    version = sdscatprintf(sdsempty(), \"%s\", KEYDB_REAL_VERSION);\n\n    /* Add git commit and working tree status when available */\n    if (strtoll(redisGitSHA1(),NULL,16)) {\n        version = sdscatprintf(version, \" (git:%s\", redisGitSHA1());\n        if (strtoll(redisGitDirty(),NULL,10))\n            version = sdscatprintf(version, \"-dirty\");\n        version = sdscat(version, \")\");\n    }\n    return version;\n}\n\nstatic void cliInitHelp(void) {\n    int commandslen = sizeof(commandHelp)/sizeof(struct commandHelp);\n    int groupslen = sizeof(commandGroups)/sizeof(char*);\n    int i, len, pos = 0;\n    helpEntry tmp;\n\n    helpEntriesLen = len = commandslen+groupslen;\n    helpEntries = zmalloc(sizeof(helpEntry)*len, MALLOC_LOCAL);\n\n    for (i = 0; i < groupslen; i++) {\n        tmp.argc = 1;\n        tmp.argv = zmalloc(sizeof(sds), MALLOC_LOCAL);\n        tmp.argv[0] = sdscatprintf(sdsempty(),\"@%s\",commandGroups[i]);\n        tmp.full = tmp.argv[0];\n        tmp.type = CLI_HELP_GROUP;\n        tmp.org = NULL;\n        helpEntries[pos++] = tmp;\n    }\n\n    for (i = 0; i < commandslen; i++) {\n        tmp.argv = sdssplitargs(commandHelp[i].name,&tmp.argc);\n        tmp.full = sdsnew(commandHelp[i].name);\n        tmp.type = CLI_HELP_COMMAND;\n        tmp.org = &commandHelp[i];\n        helpEntries[pos++] = tmp;\n    }\n}\n\n/* cliInitHelp() setups the helpEntries array with the command and group\n * names from the help.h file. However the Redis instance we are connecting\n * to may support more commands, so this function integrates the previous\n * entries with additional entries obtained using the COMMAND command\n * available in recent versions of Redis. */\nstatic void cliIntegrateHelp(void) {\n    if (cliConnect(CC_QUIET) == REDIS_ERR) return;\n\n    redisReply *reply = redisCommand(context, \"COMMAND\");\n    if(reply == NULL || reply->type != REDIS_REPLY_ARRAY) return;\n\n    /* Scan the array reported by COMMAND and fill only the entries that\n     * don't already match what we have. */\n    for (size_t j = 0; j < reply->elements; j++) {\n        redisReply *entry = reply->element[j];\n        if (entry->type != REDIS_REPLY_ARRAY || entry->elements < 4 ||\n            entry->element[0]->type != REDIS_REPLY_STRING ||\n            entry->element[1]->type != REDIS_REPLY_INTEGER ||\n            entry->element[3]->type != REDIS_REPLY_INTEGER) return;\n        char *cmdname = entry->element[0]->str;\n        int i;\n\n        for (i = 0; i < helpEntriesLen; i++) {\n            helpEntry *he = helpEntries+i;\n            if (!strcasecmp(he->argv[0],cmdname))\n                break;\n        }\n        if (i != helpEntriesLen) continue;\n\n        helpEntriesLen++;\n        helpEntries = zrealloc(helpEntries,sizeof(helpEntry)*helpEntriesLen, MALLOC_LOCAL);\n        helpEntry *new = helpEntries+(helpEntriesLen-1);\n\n        new->argc = 1;\n        new->argv = zmalloc(sizeof(sds), MALLOC_LOCAL);\n        new->argv[0] = sdsnew(cmdname);\n        new->full = new->argv[0];\n        new->type = CLI_HELP_COMMAND;\n        sdstoupper(new->argv[0]);\n\n        struct commandHelp *ch = zmalloc(sizeof(*ch), MALLOC_LOCAL);\n        ch->name = new->argv[0];\n        ch->params = sdsempty();\n        int args = llabs(entry->element[1]->integer);\n        args--; /* Remove the command name itself. */\n        if (entry->element[3]->integer == 1) {\n            ch->params = sdscat(ch->params,\"key \");\n            args--;\n        }\n        while(args-- > 0) ch->params = sdscat(ch->params,\"arg \");\n        if (entry->element[1]->integer < 0)\n            ch->params = sdscat(ch->params,\"...options...\");\n        ch->summary = \"Help not available\";\n        ch->group = 0;\n        ch->since = \"not known\";\n        new->org = ch;\n    }\n    freeReplyObject(reply);\n}\n\n/* Output command help to stdout. */\nstatic void cliOutputCommandHelp(struct commandHelp *help, int group) {\n    printf(\"\\r\\n  \\x1b[1m%s\\x1b[0m \\x1b[90m%s\\x1b[0m\\r\\n\", help->name, help->params);\n    printf(\"  \\x1b[33msummary:\\x1b[0m %s\\r\\n\", help->summary);\n    printf(\"  \\x1b[33msince:\\x1b[0m %s\\r\\n\", help->since);\n    if (group) {\n        printf(\"  \\x1b[33mgroup:\\x1b[0m %s\\r\\n\", commandGroups[help->group]);\n    }\n}\n\n/* Print generic help. */\nstatic void cliOutputGenericHelp(void) {\n    sds version = cliVersion();\n    printf(\n        \"keydb-cli %s\\n\"\n        \"To get help about Redis commands type:\\n\"\n        \"      \\\"help @<group>\\\" to get a list of commands in <group>\\n\"\n        \"      \\\"help <command>\\\" for help on <command>\\n\"\n        \"      \\\"help <tab>\\\" to get a list of possible help topics\\n\"\n        \"      \\\"quit\\\" to exit\\n\"\n        \"\\n\"\n        \"To set keydb-cli preferences:\\n\"\n        \"      \\\":set hints\\\" enable online hints\\n\"\n        \"      \\\":set nohints\\\" disable online hints\\n\"\n        \"Set your preferences in ~/.redisclirc\\n\",\n        version\n    );\n    sdsfree(version);\n}\n\n/* Output all command help, filtering by group or command name. */\nstatic void cliOutputHelp(int argc, char **argv) {\n    int i, j, len;\n    int group = -1;\n    helpEntry *entry;\n    struct commandHelp *help;\n\n    if (argc == 0) {\n        cliOutputGenericHelp();\n        return;\n    } else if (argc > 0 && argv[0][0] == '@') {\n        len = sizeof(commandGroups)/sizeof(char*);\n        for (i = 0; i < len; i++) {\n            if (strcasecmp(argv[0]+1,commandGroups[i]) == 0) {\n                group = i;\n                break;\n            }\n        }\n    }\n\n    assert(argc > 0);\n    for (i = 0; i < helpEntriesLen; i++) {\n        entry = &helpEntries[i];\n        if (entry->type != CLI_HELP_COMMAND) continue;\n\n        help = entry->org;\n        if (group == -1) {\n            /* Compare all arguments */\n            if (argc <= entry->argc) {\n                for (j = 0; j < argc; j++) {\n                    if (strcasecmp(argv[j],entry->argv[j]) != 0) break;\n                }\n                if (j == argc) {\n                    cliOutputCommandHelp(help,1);\n                }\n            }\n        } else {\n            if (group == help->group) {\n                cliOutputCommandHelp(help,0);\n            }\n        }\n    }\n    printf(\"\\r\\n\");\n}\n\n/* Linenoise completion callback. */\nstatic void completionCallback(const char *buf, linenoiseCompletions *lc) {\n    size_t startpos = 0;\n    int mask;\n    int i;\n    size_t matchlen;\n    sds tmp;\n\n    if (strncasecmp(buf,\"help \",5) == 0) {\n        startpos = 5;\n        while (isspace(buf[startpos])) startpos++;\n        mask = CLI_HELP_COMMAND | CLI_HELP_GROUP;\n    } else {\n        mask = CLI_HELP_COMMAND;\n    }\n\n    for (i = 0; i < helpEntriesLen; i++) {\n        if (!(helpEntries[i].type & mask)) continue;\n\n        matchlen = strlen(buf+startpos);\n        if (strncasecmp(buf+startpos,helpEntries[i].full,matchlen) == 0) {\n            tmp = sdsnewlen(buf,startpos);\n            tmp = sdscat(tmp,helpEntries[i].full);\n            linenoiseAddCompletion(lc,tmp);\n            sdsfree(tmp);\n        }\n    }\n}\n\n/* Linenoise hints callback. */\nstatic char *hintsCallback(const char *buf, int *color, int *bold) {\n    if (!pref.hints) return NULL;\n\n    int i, argc, buflen = strlen(buf);\n    sds *argv = sdssplitargs(buf,&argc);\n    int endspace = buflen && isspace(buf[buflen-1]);\n\n    /* Check if the argument list is empty and return ASAP. */\n    if (argc == 0) {\n        sdsfreesplitres(argv,argc);\n        return NULL;\n    }\n\n    for (i = 0; i < helpEntriesLen; i++) {\n        if (!(helpEntries[i].type & CLI_HELP_COMMAND)) continue;\n\n        if (strcasecmp(argv[0],helpEntries[i].full) == 0 ||\n            strcasecmp(buf,helpEntries[i].full) == 0)\n        {\n            *color = 90;\n            *bold = 0;\n            sds hint = sdsnew(helpEntries[i].org->params);\n\n            /* Remove arguments from the returned hint to show only the\n             * ones the user did not yet typed. */\n            int toremove = argc-1;\n            while(toremove > 0 && sdslen(hint)) {\n                if (hint[0] == '[') break;\n                if (hint[0] == ' ') toremove--;\n                sdsrange(hint,1,-1);\n            }\n\n            /* Add an initial space if needed. */\n            if (!endspace) {\n                sds newhint = sdsnewlen(\" \",1);\n                newhint = sdscatsds(newhint,hint);\n                sdsfree(hint);\n                hint = newhint;\n            }\n\n            sdsfreesplitres(argv,argc);\n            return hint;\n        }\n    }\n    sdsfreesplitres(argv,argc);\n    return NULL;\n}\n\nstatic void freeHintsCallback(void *ptr) {\n    sdsfree(ptr);\n}\n\n/*------------------------------------------------------------------------------\n * Networking / parsing\n *--------------------------------------------------------------------------- */\n\n/* Unquote a null-terminated string and return it as a binary-safe sds. */\nstatic sds unquoteCString(char *str) {\n    int count;\n    sds *unquoted = sdssplitargs(str, &count);\n    sds res = NULL;\n\n    if (unquoted && count == 1) {\n        res = unquoted[0];\n        unquoted[0] = NULL;\n    }\n\n    if (unquoted)\n        sdsfreesplitres(unquoted, count);\n\n    return res;\n}\n\n/* Send AUTH command to the server */\nstatic int cliAuth(redisContext *ctx, char *user, char *auth) {\n    redisReply *reply;\n    if (auth == NULL) return REDIS_OK;\n\n    if (user == NULL)\n        reply = redisCommand(ctx,\"AUTH %s\",auth);\n    else\n        reply = redisCommand(ctx,\"AUTH %s %s\",user,auth);\n\n    if (reply == NULL) {\n        fprintf(stderr, \"\\nI/O error\\n\");\n        return REDIS_ERR;\n    }\n\n    int result = REDIS_OK;\n    if (reply->type == REDIS_REPLY_ERROR) {\n        result = REDIS_ERR;\n        fprintf(stderr, \"AUTH failed: %s\\n\", reply->str);\n    }\n    freeReplyObject(reply);\n    return result;\n}\n\n/* Send SELECT input_dbnum to the server */\nstatic int cliSelect(void) {\n    redisReply *reply;\n    if (config.input_dbnum == config.dbnum) return REDIS_OK;\n\n    reply = redisCommand(context,\"SELECT %d\",config.input_dbnum);\n    if (reply == NULL) {\n        fprintf(stderr, \"\\nI/O error\\n\");\n        return REDIS_ERR;\n    }\n\n    int result = REDIS_OK;\n    if (reply->type == REDIS_REPLY_ERROR) {\n        result = REDIS_ERR;\n        fprintf(stderr,\"SELECT %d failed: %s\\n\",config.input_dbnum,reply->str);\n    } else {\n        config.dbnum = config.input_dbnum;\n        cliRefreshPrompt();\n    }\n    freeReplyObject(reply);\n    return result;\n}\n\n/* Select RESP3 mode if redis-cli was started with the -3 option.  */\nstatic int cliSwitchProto(void) {\n    redisReply *reply;\n    if (config.resp3 == 0) return REDIS_OK;\n\n    reply = redisCommand(context,\"HELLO 3\");\n    if (reply == NULL) {\n        fprintf(stderr, \"\\nI/O error\\n\");\n        return REDIS_ERR;\n    }\n\n    int result = REDIS_OK;\n    if (reply->type == REDIS_REPLY_ERROR) {\n        result = REDIS_ERR;\n        fprintf(stderr,\"HELLO 3 failed: %s\\n\",reply->str);\n    }\n    freeReplyObject(reply);\n    return result;\n}\n\n/* Connect to the server. It is possible to pass certain flags to the function:\n *      CC_FORCE: The connection is performed even if there is already\n *                a connected socket.\n *      CC_QUIET: Don't print errors if connection fails. */\nstatic int cliConnect(int flags) {\n    if (context == NULL || flags & CC_FORCE) {\n        if (context != NULL) {\n            redisFree(context);\n            config.dbnum = 0;\n            config.in_multi = 0;\n            cliRefreshPrompt();\n        }\n\n        /* Do not use hostsocket when we got redirected in cluster mode */\n        if (config.hostsocket == NULL ||\n            (config.cluster_mode && config.cluster_reissue_command)) {\n            context = redisConnect(config.hostip,config.hostport);\n        } else {\n            context = redisConnectUnix(config.hostsocket);\n        }\n\n        if (!context->err && config.tls) {\n            const char *err = NULL;\n            if (cliSecureConnection(context, config.sslconfig, &err) == REDIS_ERR && err) {\n                fprintf(stderr, \"Could not negotiate a TLS connection: %s\\n\", err);\n                redisFree(context);\n                context = NULL;\n                return REDIS_ERR;\n            }\n        }\n\n        if (context->err) {\n            if (!(flags & CC_QUIET)) {\n                fprintf(stderr,\"Could not connect to Redis at \");\n                if (config.hostsocket == NULL ||\n                    (config.cluster_mode && config.cluster_reissue_command))\n                {\n                    fprintf(stderr, \"%s:%d: %s\\n\",\n                        config.hostip,config.hostport,context->errstr);\n                } else {\n                    fprintf(stderr,\"%s: %s\\n\",\n                        config.hostsocket,context->errstr);\n                }\n            }\n            redisFree(context);\n            context = NULL;\n            return REDIS_ERR;\n        }\n\n\n        /* Set aggressive KEEP_ALIVE socket option in the Redis context socket\n         * in order to prevent timeouts caused by the execution of long\n         * commands. At the same time this improves the detection of real\n         * errors. */\n        anetKeepAlive(NULL, context->fd, REDIS_CLI_KEEPALIVE_INTERVAL);\n\n        /* Do AUTH, select the right DB, switch to RESP3 if needed. */\n        if (cliAuth(context, config.user, config.auth) != REDIS_OK)\n            return REDIS_ERR;\n        if (cliSelect() != REDIS_OK)\n            return REDIS_ERR;\n        if (cliSwitchProto() != REDIS_OK)\n            return REDIS_ERR;\n    }\n\n    /* Set a PUSH handler if configured to do so. */\n    if (config.push_output) {\n        redisSetPushCallback(context, cliPushHandler);\n    }\n\n    return REDIS_OK;\n}\n\n/* In cluster, if server replies ASK, we will redirect to a different node.\n * Before sending the real command, we need to send ASKING command first. */\nstatic int cliSendAsking() {\n    redisReply *reply;\n\n    config.cluster_send_asking = 0;\n    if (context == NULL) {\n        return REDIS_ERR;\n    }\n    reply = redisCommand(context,\"ASKING\");\n    if (reply == NULL) {\n        fprintf(stderr, \"\\nI/O error\\n\");\n        return REDIS_ERR;\n    }\n    int result = REDIS_OK;\n    if (reply->type == REDIS_REPLY_ERROR) {\n        result = REDIS_ERR;\n        fprintf(stderr,\"ASKING failed: %s\\n\",reply->str);\n    }\n    freeReplyObject(reply);\n    return result;\n}\n\nstatic void cliPrintContextError(void) {\n    if (context == NULL) return;\n    fprintf(stderr,\"Error: %s\\n\",context->errstr);\n}\n\nstatic int isInvalidateReply(redisReply *reply) {\n    return reply->type == REDIS_REPLY_PUSH && reply->elements == 2 &&\n        reply->element[0]->type == REDIS_REPLY_STRING &&\n        !strncmp(reply->element[0]->str, \"invalidate\", 10) &&\n        reply->element[1]->type == REDIS_REPLY_ARRAY;\n}\n\n/* Special display handler for RESP3 'invalidate' messages.\n * This function does not validate the reply, so it should\n * already be confirmed correct */\nstatic sds cliFormatInvalidateTTY(redisReply *r) {\n    sds out = sdsnew(\"-> invalidate: \");\n\n    for (size_t i = 0; i < r->element[1]->elements; i++) {\n        redisReply *key = r->element[1]->element[i];\n        assert(key->type == REDIS_REPLY_STRING);\n\n        out = sdscatfmt(out, \"'%s'\", key->str, key->len);\n        if (i < r->element[1]->elements - 1)\n            out = sdscatlen(out, \", \", 2);\n    }\n\n    return sdscatlen(out, \"\\n\", 1);\n}\n\nstatic sds cliFormatReplyTTY(redisReply *r, char *prefix) {\n    sds out = sdsempty();\n    switch (r->type) {\n    case REDIS_REPLY_ERROR:\n        out = sdscatprintf(out,\"(error) %s\\n\", r->str);\n    break;\n    case REDIS_REPLY_STATUS:\n        out = sdscat(out,r->str);\n        out = sdscat(out,\"\\n\");\n    break;\n    case REDIS_REPLY_INTEGER:\n        out = sdscatprintf(out,\"(integer) %lld\\n\",r->integer);\n    break;\n    case REDIS_REPLY_DOUBLE:\n        out = sdscatprintf(out,\"(double) %s\\n\",r->str);\n    break;\n    case REDIS_REPLY_STRING:\n    case REDIS_REPLY_VERB:\n        /* If you are producing output for the standard output we want\n        * a more interesting output with quoted characters and so forth,\n        * unless it's a verbatim string type. */\n        if (r->type == REDIS_REPLY_STRING) {\n            out = sdscatrepr(out,r->str,r->len);\n            out = sdscat(out,\"\\n\");\n        } else {\n            out = sdscatlen(out,r->str,r->len);\n            out = sdscat(out,\"\\n\");\n        }\n    break;\n    case REDIS_REPLY_NIL:\n        out = sdscat(out,\"(nil)\\n\");\n    break;\n    case REDIS_REPLY_BOOL:\n        out = sdscat(out,r->integer ? \"(true)\\n\" : \"(false)\\n\");\n    break;\n    case REDIS_REPLY_ARRAY:\n    case REDIS_REPLY_MAP:\n    case REDIS_REPLY_SET:\n    case REDIS_REPLY_PUSH:\n        if (r->elements == 0) {\n            if (r->type == REDIS_REPLY_ARRAY)\n                out = sdscat(out,\"(empty array)\\n\");\n            else if (r->type == REDIS_REPLY_MAP)\n                out = sdscat(out,\"(empty hash)\\n\");\n            else if (r->type == REDIS_REPLY_SET)\n                out = sdscat(out,\"(empty set)\\n\");\n            else if (r->type == REDIS_REPLY_PUSH)\n                out = sdscat(out,\"(empty push)\\n\");\n            else\n                out = sdscat(out,\"(empty aggregate type)\\n\");\n        } else {\n            unsigned int i, idxlen = 0;\n            char _prefixlen[16];\n            char _prefixfmt[16];\n            sds _prefix;\n            sds tmp;\n\n            /* Calculate chars needed to represent the largest index */\n            i = r->elements;\n            if (r->type == REDIS_REPLY_MAP) i /= 2;\n            do {\n                idxlen++;\n                i /= 10;\n            } while(i);\n\n            /* Prefix for nested multi bulks should grow with idxlen+2 spaces */\n            memset(_prefixlen,' ',idxlen+2);\n            _prefixlen[idxlen+2] = '\\0';\n            _prefix = sdscat(sdsnew(prefix),_prefixlen);\n\n            /* Setup prefix format for every entry */\n            char numsep;\n            if (r->type == REDIS_REPLY_SET) numsep = '~';\n            else if (r->type == REDIS_REPLY_MAP) numsep = '#';\n            else numsep = ')';\n            snprintf(_prefixfmt,sizeof(_prefixfmt),\"%%s%%%ud%c \",idxlen,numsep);\n\n            for (i = 0; i < r->elements; i++) {\n                unsigned int human_idx = (r->type == REDIS_REPLY_MAP) ?\n                                         i/2 : i;\n                human_idx++; /* Make it 1-based. */\n\n                /* Don't use the prefix for the first element, as the parent\n                 * caller already prepended the index number. */\n                out = sdscatprintf(out,_prefixfmt,i == 0 ? \"\" : prefix,human_idx);\n\n                /* Format the multi bulk entry */\n                tmp = cliFormatReplyTTY(r->element[i],_prefix);\n                out = sdscatlen(out,tmp,sdslen(tmp));\n                sdsfree(tmp);\n\n                /* For maps, format the value as well. */\n                if (r->type == REDIS_REPLY_MAP) {\n                    i++;\n                    sdsrange(out,0,-2);\n                    out = sdscat(out,\" => \");\n                    tmp = cliFormatReplyTTY(r->element[i],_prefix);\n                    out = sdscatlen(out,tmp,sdslen(tmp));\n                    sdsfree(tmp);\n                }\n            }\n            sdsfree(_prefix);\n        }\n    break;\n    default:\n        fprintf(stderr,\"Unknown reply type: %d\\n\", r->type);\n        exit(1);\n    }\n    return out;\n}\n\nint isColorTerm(void) {\n    char *t = getenv(\"TERM\");\n    return t != NULL && strstr(t,\"xterm\") != NULL;\n}\n\n/* Helper  function for sdsCatColorizedLdbReply() appending colorize strings\n * to an SDS string. */\nsds sdscatcolor(sds o, char *s, size_t len, char *color) {\n    if (!isColorTerm()) return sdscatlen(o,s,len);\n\n    int bold = strstr(color,\"bold\") != NULL;\n    int ccode = 37; /* Defaults to white. */\n    if (strstr(color,\"red\")) ccode = 31;\n    else if (strstr(color,\"green\")) ccode = 32;\n    else if (strstr(color,\"yellow\")) ccode = 33;\n    else if (strstr(color,\"blue\")) ccode = 34;\n    else if (strstr(color,\"magenta\")) ccode = 35;\n    else if (strstr(color,\"cyan\")) ccode = 36;\n    else if (strstr(color,\"white\")) ccode = 37;\n\n    o = sdscatfmt(o,\"\\033[%i;%i;49m\",bold,ccode);\n    o = sdscatlen(o,s,len);\n    o = sdscat(o,\"\\033[0m\");\n    return o;\n}\n\n/* Colorize Lua debugger status replies according to the prefix they\n * have. */\nsds sdsCatColorizedLdbReply(sds o, char *s, size_t len) {\n    char *color = \"white\";\n\n    if (strstr(s,\"<debug>\")) color = \"bold\";\n    if (strstr(s,\"<redis>\")) color = \"green\";\n    if (strstr(s,\"<reply>\")) color = \"cyan\";\n    if (strstr(s,\"<error>\")) color = \"red\";\n    if (strstr(s,\"<hint>\")) color = \"bold\";\n    if (strstr(s,\"<value>\") || strstr(s,\"<retval>\")) color = \"magenta\";\n    if (len > 4 && isdigit(s[3])) {\n        if (s[1] == '>') color = \"yellow\"; /* Current line. */\n        else if (s[2] == '#') color = \"bold\"; /* Break point. */\n    }\n    return sdscatcolor(o,s,len,color);\n}\n\nstatic sds cliFormatReplyRaw(redisReply *r) {\n    sds out = sdsempty(), tmp;\n    size_t i;\n\n    switch (r->type) {\n    case REDIS_REPLY_NIL:\n        /* Nothing... */\n        break;\n    case REDIS_REPLY_ERROR:\n        out = sdscatlen(out,r->str,r->len);\n        out = sdscatlen(out,\"\\n\",1);\n        break;\n    case REDIS_REPLY_STATUS:\n    case REDIS_REPLY_STRING:\n    case REDIS_REPLY_VERB:\n        if (r->type == REDIS_REPLY_STATUS && config.eval_ldb) {\n            /* The Lua debugger replies with arrays of simple (status)\n             * strings. We colorize the output for more fun if this\n             * is a debugging session. */\n\n            /* Detect the end of a debugging session. */\n            if (strstr(r->str,\"<endsession>\") == r->str) {\n                config.enable_ldb_on_eval = 0;\n                config.eval_ldb = 0;\n                config.eval_ldb_end = 1; /* Signal the caller session ended. */\n                config.output = OUTPUT_STANDARD;\n                cliRefreshPrompt();\n            } else {\n                out = sdsCatColorizedLdbReply(out,r->str,r->len);\n            }\n        } else {\n            out = sdscatlen(out,r->str,r->len);\n        }\n        break;\n    case REDIS_REPLY_BOOL:\n        out = sdscat(out,r->integer ? \"(true)\" : \"(false)\");\n    break;\n    case REDIS_REPLY_INTEGER:\n        out = sdscatprintf(out,\"%lld\",r->integer);\n        break;\n    case REDIS_REPLY_DOUBLE:\n        out = sdscatprintf(out,\"%s\",r->str);\n        break;\n    case REDIS_REPLY_SET:\n    case REDIS_REPLY_ARRAY:\n    case REDIS_REPLY_PUSH:\n        for (i = 0; i < r->elements; i++) {\n            if (i > 0) out = sdscat(out,config.mb_delim);\n            tmp = cliFormatReplyRaw(r->element[i]);\n            out = sdscatlen(out,tmp,sdslen(tmp));\n            sdsfree(tmp);\n        }\n        break;\n    case REDIS_REPLY_MAP:\n        for (i = 0; i < r->elements; i += 2) {\n            if (i > 0) out = sdscat(out,config.mb_delim);\n            tmp = cliFormatReplyRaw(r->element[i]);\n            out = sdscatlen(out,tmp,sdslen(tmp));\n            sdsfree(tmp);\n\n            out = sdscatlen(out,\" \",1);\n            tmp = cliFormatReplyRaw(r->element[i+1]);\n            out = sdscatlen(out,tmp,sdslen(tmp));\n            sdsfree(tmp);\n        }\n        break;\n    default:\n        fprintf(stderr,\"Unknown reply type: %d\\n\", r->type);\n        exit(1);\n    }\n    return out;\n}\n\nstatic sds cliFormatReplyCSV(redisReply *r) {\n    unsigned int i;\n\n    sds out = sdsempty();\n    switch (r->type) {\n    case REDIS_REPLY_ERROR:\n        out = sdscat(out,\"ERROR,\");\n        out = sdscatrepr(out,r->str,strlen(r->str));\n    break;\n    case REDIS_REPLY_STATUS:\n        out = sdscatrepr(out,r->str,r->len);\n    break;\n    case REDIS_REPLY_INTEGER:\n        out = sdscatprintf(out,\"%lld\",r->integer);\n    break;\n    case REDIS_REPLY_DOUBLE:\n        out = sdscatprintf(out,\"%s\",r->str);\n        break;\n    case REDIS_REPLY_STRING:\n    case REDIS_REPLY_VERB:\n        out = sdscatrepr(out,r->str,r->len);\n    break;\n    case REDIS_REPLY_NIL:\n        out = sdscat(out,\"NULL\");\n    break;\n    case REDIS_REPLY_BOOL:\n        out = sdscat(out,r->integer ? \"true\" : \"false\");\n    break;\n    case REDIS_REPLY_ARRAY:\n    case REDIS_REPLY_SET:\n    case REDIS_REPLY_PUSH:\n    case REDIS_REPLY_MAP: /* CSV has no map type, just output flat list. */\n        for (i = 0; i < r->elements; i++) {\n            sds tmp = cliFormatReplyCSV(r->element[i]);\n            out = sdscatlen(out,tmp,sdslen(tmp));\n            if (i != r->elements-1) out = sdscat(out,\",\");\n            sdsfree(tmp);\n        }\n    break;\n    default:\n        fprintf(stderr,\"Unknown reply type: %d\\n\", r->type);\n        exit(1);\n    }\n    return out;\n}\n\n/* Generate reply strings in various output modes */\nstatic sds cliFormatReply(redisReply *reply, int mode, int verbatim) {\n    sds out;\n\n    if (verbatim) {\n        out = cliFormatReplyRaw(reply);\n    }  else if (mode == OUTPUT_STANDARD) {\n        out = cliFormatReplyTTY(reply, \"\");\n    } else if (mode == OUTPUT_RAW) {\n        out = cliFormatReplyRaw(reply);\n        out = sdscatsds(out, config.cmd_delim);\n    } else if (mode == OUTPUT_CSV) {\n        out = cliFormatReplyCSV(reply);\n        out = sdscatlen(out, \"\\n\", 1);\n    } else {\n        fprintf(stderr, \"Error:  Unknown output encoding %d\\n\", mode);\n        exit(1);\n    }\n\n    return out;\n}\n\n/* Output any spontaneous PUSH reply we receive */\nstatic void cliPushHandler(void *privdata, void *reply) {\n    UNUSED(privdata);\n    sds out;\n\n    if (config.output == OUTPUT_STANDARD && isInvalidateReply(reply)) {\n        out = cliFormatInvalidateTTY(reply);\n    } else {\n        out = cliFormatReply(reply, config.output, 0);\n    }\n\n    fwrite(out, sdslen(out), 1, stdout);\n\n    freeReplyObject(reply);\n    sdsfree(out);\n}\n\nstatic int cliReadReply(int output_raw_strings) {\n    void *_reply;\n    redisReply *reply;\n    sds out = NULL;\n    int output = 1;\n\n    if (redisGetReply(context,&_reply) != REDIS_OK) {\n        if (config.shutdown) {\n            redisFree(context);\n            context = NULL;\n            return REDIS_OK;\n        }\n        if (config.interactive) {\n            /* Filter cases where we should reconnect */\n            if (context->err == REDIS_ERR_IO &&\n                (errno == ECONNRESET || errno == EPIPE))\n                return REDIS_ERR;\n            if (context->err == REDIS_ERR_EOF)\n                return REDIS_ERR;\n        }\n        cliPrintContextError();\n        exit(1);\n        return REDIS_ERR; /* avoid compiler warning */\n    }\n\n    reply = (redisReply*)_reply;\n\n    config.last_cmd_type = reply->type;\n\n    /* Check if we need to connect to a different node and reissue the\n     * request. */\n    if (config.cluster_mode && reply->type == REDIS_REPLY_ERROR &&\n        (!strncmp(reply->str,\"MOVED \",6) || !strncmp(reply->str,\"ASK \",4)))\n    {\n        char *p = reply->str, *s;\n        int slot;\n\n        output = 0;\n        /* Comments show the position of the pointer as:\n         *\n         * [S] for pointer 's'\n         * [P] for pointer 'p'\n         */\n        s = strchr(p,' ');      /* MOVED[S]3999 127.0.0.1:6381 */\n        p = strchr(s+1,' ');    /* MOVED[S]3999[P]127.0.0.1:6381 */\n        *p = '\\0';\n        slot = atoi(s+1);\n        s = strrchr(p+1,':');    /* MOVED 3999[P]127.0.0.1[S]6381 */\n        *s = '\\0';\n        sdsfree(config.hostip);\n        config.hostip = sdsnew(p+1);\n        config.hostport = atoi(s+1);\n        if (config.interactive)\n            printf(\"-> Redirected to slot [%d] located at %s:%d\\n\",\n                slot, config.hostip, config.hostport);\n        config.cluster_reissue_command = 1;\n        if (!strncmp(reply->str,\"ASK \",4)) {\n            config.cluster_send_asking = 1;\n        }\n        cliRefreshPrompt();\n    } else if (!config.interactive && config.set_errcode && \n        reply->type == REDIS_REPLY_ERROR) \n    {\n        fprintf(stderr,\"%s\\n\",reply->str);\n        exit(1);\n        return REDIS_ERR; /* avoid compiler warning */\n    }\n\n    if (output) {\n        out = cliFormatReply(reply, config.output, output_raw_strings);\n        fwrite(out,sdslen(out),1,stdout);\n        fflush(stdout);\n        sdsfree(out);\n    }\n    freeReplyObject(reply);\n    return REDIS_OK;\n}\n\nstatic int cliSendCommand(int argc, char **argv, long repeat) {\n    char *command = argv[0];\n    size_t *argvlen;\n    int j, output_raw;\n\n    if (!config.eval_ldb && /* In debugging mode, let's pass \"help\" to Redis. */\n        (!strcasecmp(command,\"help\") || !strcasecmp(command,\"?\"))) {\n        cliOutputHelp(--argc, ++argv);\n        return REDIS_OK;\n    }\n\n    if (context == NULL) return REDIS_ERR;\n\n    output_raw = 0;\n    if (!strcasecmp(command,\"info\") ||\n        !strcasecmp(command,\"lolwut\") ||\n        (argc >= 2 && !strcasecmp(command,\"debug\") &&\n                       !strcasecmp(argv[1],\"htstats\")) ||\n        (argc >= 2 && !strcasecmp(command,\"debug\") &&\n                       !strcasecmp(argv[1],\"htstats-key\")) ||\n        (argc >= 2 && !strcasecmp(command,\"memory\") &&\n                      (!strcasecmp(argv[1],\"malloc-stats\") ||\n                       !strcasecmp(argv[1],\"doctor\"))) ||\n        (argc == 2 && !strcasecmp(command,\"cluster\") &&\n                      (!strcasecmp(argv[1],\"nodes\") ||\n                       !strcasecmp(argv[1],\"info\"))) ||\n        (argc >= 2 && !strcasecmp(command,\"client\") &&\n                       (!strcasecmp(argv[1],\"list\") ||\n                        !strcasecmp(argv[1],\"info\"))) ||\n        (argc == 3 && !strcasecmp(command,\"latency\") &&\n                       !strcasecmp(argv[1],\"graph\")) ||\n        (argc == 2 && !strcasecmp(command,\"latency\") &&\n                       !strcasecmp(argv[1],\"doctor\")) ||\n        /* Format PROXY INFO command for Redis Cluster Proxy:\n         * https://github.com/artix75/redis-cluster-proxy */\n        (argc >= 2 && !strcasecmp(command,\"proxy\") &&\n                       !strcasecmp(argv[1],\"info\")))\n    {\n        output_raw = 1;\n    }\n\n    if (!strcasecmp(command,\"shutdown\")) config.shutdown = 1;\n    if (!strcasecmp(command,\"monitor\")) config.monitor_mode = 1;\n    if (!strcasecmp(command,\"subscribe\") ||\n        !strcasecmp(command,\"psubscribe\")) config.pubsub_mode = 1;\n    if (!strcasecmp(command,\"sync\") ||\n        !strcasecmp(command,\"psync\")) config.slave_mode = 1;\n\n    /* When the user manually calls SCRIPT DEBUG, setup the activation of\n     * debugging mode on the next eval if needed. */\n    if (argc == 3 && !strcasecmp(argv[0],\"script\") &&\n                     !strcasecmp(argv[1],\"debug\"))\n    {\n        if (!strcasecmp(argv[2],\"yes\") || !strcasecmp(argv[2],\"sync\")) {\n            config.enable_ldb_on_eval = 1;\n        } else {\n            config.enable_ldb_on_eval = 0;\n        }\n    }\n\n    /* Actually activate LDB on EVAL if needed. */\n    if (!strcasecmp(command,\"eval\") && config.enable_ldb_on_eval) {\n        config.eval_ldb = 1;\n        config.output = OUTPUT_RAW;\n    }\n\n    /* Setup argument length */\n    argvlen = zmalloc(argc*sizeof(size_t), MALLOC_LOCAL);\n    for (j = 0; j < argc; j++)\n        argvlen[j] = sdslen(argv[j]);\n\n    /* Negative repeat is allowed and causes infinite loop,\n       works well with the interval option. */\n    while(repeat < 0 || repeat-- > 0) {\n        redisAppendCommandArgv(context,argc,(const char**)argv,argvlen);\n        while (config.monitor_mode) {\n            if (cliReadReply(output_raw) != REDIS_OK) exit(1);\n            fflush(stdout);\n        }\n\n        if (config.pubsub_mode) {\n            if (config.output != OUTPUT_RAW)\n                printf(\"Reading messages... (press Ctrl-C to quit)\\n\");\n\n            /* Unset our default PUSH handler so this works in RESP2/RESP3 */\n            redisSetPushCallback(context, NULL);\n\n            while (config.pubsub_mode) {\n                if (cliReadReply(output_raw) != REDIS_OK) exit(1);\n                if (config.last_cmd_type == REDIS_REPLY_ERROR) {\n                    if (config.push_output) {\n                        redisSetPushCallback(context, cliPushHandler);\n                    }\n                    config.pubsub_mode = 0;\n                }\n            }\n            continue;\n        }\n\n        if (config.slave_mode) {\n            printf(\"Entering replica output mode...  (press Ctrl-C to quit)\\n\");\n            slaveMode();\n            config.slave_mode = 0;\n            zfree(argvlen);\n            return REDIS_ERR;  /* Error = slaveMode lost connection to master */\n        }\n\n        if (cliReadReply(output_raw) != REDIS_OK) {\n            zfree(argvlen);\n            return REDIS_ERR;\n        } else {\n            /* Store database number when SELECT was successfully executed. */\n            if (!strcasecmp(command,\"select\") && argc == 2 && \n                config.last_cmd_type != REDIS_REPLY_ERROR) \n            {\n                config.input_dbnum = config.dbnum = atoi(argv[1]);\n                cliRefreshPrompt();\n            } else if (!strcasecmp(command,\"auth\") && (argc == 2 || argc == 3)) {\n                cliSelect();\n            } else if (!strcasecmp(command,\"multi\") && argc == 1 &&\n                config.last_cmd_type != REDIS_REPLY_ERROR) \n            {\n                config.in_multi = 1;\n                config.pre_multi_dbnum = config.dbnum;\n                cliRefreshPrompt();\n            } else if (!strcasecmp(command,\"exec\") && argc == 1 && config.in_multi) {\n                config.in_multi = 0;\n                if (config.last_cmd_type == REDIS_REPLY_ERROR ||\n                    config.last_cmd_type == REDIS_REPLY_NIL)\n                {\n                    config.input_dbnum = config.dbnum = config.pre_multi_dbnum;\n                }\n                cliRefreshPrompt();\n            } else if (!strcasecmp(command,\"discard\") && argc == 1 && \n                config.last_cmd_type != REDIS_REPLY_ERROR) \n            {\n                config.in_multi = 0;\n                config.input_dbnum = config.dbnum = config.pre_multi_dbnum;\n                cliRefreshPrompt();\n            } \n        }\n        if (config.cluster_reissue_command){\n            /* If we need to reissue the command, break to prevent a\n               further 'repeat' number of dud interactions */\n            break;\n        }\n        if (config.interval) usleep(config.interval);\n        fflush(stdout); /* Make it grep friendly */\n    }\n\n    zfree(argvlen);\n    return REDIS_OK;\n}\n\n/* Send a command reconnecting the link if needed. */\nstatic redisReply *reconnectingRedisCommand(redisContext *c, const char *fmt, ...) {\n    redisReply *reply = NULL;\n    int tries = 0;\n    va_list ap;\n\n    assert(!c->err);\n    while(reply == NULL) {\n        while (c->err & (REDIS_ERR_IO | REDIS_ERR_EOF)) {\n            printf(\"\\r\\x1b[0K\"); /* Cursor to left edge + clear line. */\n            printf(\"Reconnecting... %d\\r\", ++tries);\n            fflush(stdout);\n\n            redisFree(c);\n            c = redisConnect(config.hostip,config.hostport);\n            if (!c->err && config.tls) {\n                const char *err = NULL;\n                if (cliSecureConnection(c, config.sslconfig, &err) == REDIS_ERR && err) {\n                    fprintf(stderr, \"TLS Error: %s\\n\", err);\n                    exit(1);\n                }\n            }\n            usleep(1000000);\n        }\n\n        va_start(ap,fmt);\n        reply = redisvCommand(c,fmt,ap);\n        va_end(ap);\n\n        if (c->err && !(c->err & (REDIS_ERR_IO | REDIS_ERR_EOF))) {\n            fprintf(stderr, \"Error: %s\\n\", c->errstr);\n            exit(1);\n        } else if (tries > 0) {\n            printf(\"\\r\\x1b[0K\"); /* Cursor to left edge + clear line. */\n        }\n    }\n\n    context = c;\n    return reply;\n}\n\n/*------------------------------------------------------------------------------\n * User interface\n *--------------------------------------------------------------------------- */\n\nstatic int parseOptions(int argc, char **argv) {\n    int i;\n\n    for (i = 1; i < argc; i++) {\n        int lastarg = i==argc-1;\n\n        if (!strcmp(argv[i],\"-h\") && !lastarg) {\n            sdsfree(config.hostip);\n            config.hostip = sdsnew(argv[++i]);\n        } else if (!strcmp(argv[i],\"-h\") && lastarg) {\n            usage();\n        } else if (!strcmp(argv[i],\"--help\")) {\n            usage();\n        } else if (!strcmp(argv[i],\"-x\")) {\n            config.stdinarg = 1;\n        } else if (!strcmp(argv[i],\"-p\") && !lastarg) {\n            config.hostport = atoi(argv[++i]);\n        } else if (!strcmp(argv[i],\"-s\") && !lastarg) {\n            config.hostsocket = argv[++i];\n        } else if (!strcmp(argv[i],\"-r\") && !lastarg) {\n            config.repeat = strtoll(argv[++i],NULL,10);\n        } else if (!strcmp(argv[i],\"-i\") && !lastarg) {\n            double seconds = atof(argv[++i]);\n            config.interval = seconds*1000000;\n        } else if (!strcmp(argv[i],\"-n\") && !lastarg) {\n            config.input_dbnum = atoi(argv[++i]);\n        } else if (!strcmp(argv[i], \"--no-auth-warning\")) {\n            config.no_auth_warning = 1;\n        } else if (!strcmp(argv[i], \"--askpass\")) {\n            config.askpass = 1;\n        } else if ((!strcmp(argv[i],\"-a\") || !strcmp(argv[i],\"--pass\"))\n                   && !lastarg)\n        {\n            config.auth = argv[++i];\n        } else if (!strcmp(argv[i],\"--user\") && !lastarg) {\n            config.user = argv[++i];\n        } else if (!strcmp(argv[i],\"-u\") && !lastarg) {\n            parseRedisUri(argv[++i]);\n        } else if (!strcmp(argv[i],\"--raw\")) {\n            config.output = OUTPUT_RAW;\n        } else if (!strcmp(argv[i],\"--no-raw\")) {\n            config.output = OUTPUT_STANDARD;\n        } else if (!strcmp(argv[i],\"--quoted-input\")) {\n            config.quoted_input = 1;\n        } else if (!strcmp(argv[i],\"--csv\")) {\n            config.output = OUTPUT_CSV;\n        } else if (!strcmp(argv[i],\"--latency\")) {\n            config.latency_mode = 1;\n        } else if (!strcmp(argv[i],\"--latency-dist\")) {\n            config.latency_dist_mode = 1;\n        } else if (!strcmp(argv[i],\"--mono\")) {\n            spectrum_palette = spectrum_palette_mono;\n            spectrum_palette_size = spectrum_palette_mono_size;\n        } else if (!strcmp(argv[i],\"--latency-history\")) {\n            config.latency_mode = 1;\n            config.latency_history = 1;\n        } else if (!strcmp(argv[i],\"--lru-test\") && !lastarg) {\n            config.lru_test_mode = 1;\n            config.lru_test_sample_size = strtoll(argv[++i],NULL,10);\n        } else if (!strcmp(argv[i],\"--slave\")) {\n            config.slave_mode = 1;\n        } else if (!strcmp(argv[i],\"--replica\")) {\n            config.slave_mode = 1;\n        } else if (!strcmp(argv[i],\"--stat\")) {\n            config.stat_mode = 1;\n        } else if (!strcmp(argv[i],\"--scan\")) {\n            config.scan_mode = 1;\n        } else if (!strcmp(argv[i],\"--pattern\") && !lastarg) {\n            sdsfree(config.pattern);\n            config.pattern = sdsnew(argv[++i]);\n        } else if (!strcmp(argv[i],\"--quoted-pattern\") && !lastarg) {\n            sdsfree(config.pattern);\n            config.pattern = unquoteCString(argv[++i]);\n            if (!config.pattern) {\n                fprintf(stderr,\"Invalid quoted string specified for --quoted-pattern.\\n\");\n                exit(1);\n            }\n        } else if (!strcmp(argv[i],\"--intrinsic-latency\") && !lastarg) {\n            config.intrinsic_latency_mode = 1;\n            config.intrinsic_latency_duration = atoi(argv[++i]);\n        } else if (!strcmp(argv[i],\"--rdb\") && !lastarg) {\n            config.getrdb_mode = 1;\n            config.rdb_filename = argv[++i];\n        } else if (!strcmp(argv[i],\"--pipe\")) {\n            config.pipe_mode = 1;\n        } else if (!strcmp(argv[i],\"--pipe-timeout\") && !lastarg) {\n            config.pipe_timeout = atoi(argv[++i]);\n        } else if (!strcmp(argv[i],\"--bigkeys\")) {\n            config.bigkeys = 1;\n        } else if (!strcmp(argv[i],\"--memkeys\")) {\n            config.memkeys = 1;\n            config.memkeys_samples = 0; /* use redis default */\n        } else if (!strcmp(argv[i],\"--memkeys-samples\")) {\n            config.memkeys = 1;\n            config.memkeys_samples = atoi(argv[++i]);\n        } else if (!strcmp(argv[i],\"--hotkeys\")) {\n            config.hotkeys = 1;\n        } else if (!strcmp(argv[i],\"--eval\") && !lastarg) {\n            config.eval = argv[++i];\n        } else if (!strcmp(argv[i],\"--ldb\")) {\n            config.eval_ldb = 1;\n            config.output = OUTPUT_RAW;\n        } else if (!strcmp(argv[i],\"--ldb-sync-mode\")) {\n            config.eval_ldb = 1;\n            config.eval_ldb_sync = 1;\n            config.output = OUTPUT_RAW;\n        } else if (!strcmp(argv[i],\"-c\")) {\n            config.cluster_mode = 1;\n        } else if (!strcmp(argv[i],\"-d\") && !lastarg) {\n            sdsfree(config.mb_delim);\n            config.mb_delim = sdsnew(argv[++i]);\n        } else if (!strcmp(argv[i],\"-D\") && !lastarg) {\n            sdsfree(config.cmd_delim);\n            config.cmd_delim = sdsnew(argv[++i]);\n        } else if (!strcmp(argv[i],\"-e\")) {\n            config.set_errcode = 1;\n        } else if (!strcmp(argv[i],\"--verbose\")) {\n            config.verbose = 1;\n        } else if (!strcmp(argv[i],\"--cluster\") && !lastarg) {\n            if (CLUSTER_MANAGER_MODE()) usage();\n            char *cmd = argv[++i];\n            int j = i;\n            while (j < argc && argv[j][0] != '-') j++;\n            if (j > i) j--;\n            createClusterManagerCommand(cmd, j - i, argv + i + 1);\n            i = j;\n        } else if (!strcmp(argv[i],\"--cluster\") && lastarg) {\n            usage();\n        } else if ((!strcmp(argv[i],\"--cluster-only-masters\"))) {\n            config.cluster_manager_command.flags |=\n                    CLUSTER_MANAGER_CMD_FLAG_MASTERS_ONLY;\n        } else if ((!strcmp(argv[i],\"--cluster-only-replicas\"))) {\n            config.cluster_manager_command.flags |=\n                    CLUSTER_MANAGER_CMD_FLAG_SLAVES_ONLY;\n        } else if (!strcmp(argv[i],\"--cluster-replicas\") && !lastarg) {\n            config.cluster_manager_command.replicas = atoi(argv[++i]);\n        } else if (!strcmp(argv[i],\"--cluster-master-id\") && !lastarg) {\n            config.cluster_manager_command.master_id = argv[++i];\n        } else if (!strcmp(argv[i],\"--cluster-from\") && !lastarg) {\n            config.cluster_manager_command.from = argv[++i];\n        } else if (!strcmp(argv[i],\"--cluster-to\") && !lastarg) {\n            config.cluster_manager_command.to = argv[++i];\n        } else if (!strcmp(argv[i],\"--cluster-from-user\") && !lastarg) {\n            config.cluster_manager_command.from_user = argv[++i];\n        } else if (!strcmp(argv[i],\"--cluster-from-pass\") && !lastarg) {\n            config.cluster_manager_command.from_pass = argv[++i];\n        } else if (!strcmp(argv[i], \"--cluster-from-askpass\")) {\n            config.cluster_manager_command.from_askpass = 1;\n        } else if (!strcmp(argv[i],\"--cluster-weight\") && !lastarg) {\n            if (config.cluster_manager_command.weight != NULL) {\n                fprintf(stderr, \"WARNING: you cannot use --cluster-weight \"\n                                \"more than once.\\n\"\n                                \"You can set more weights by adding them \"\n                                \"as a space-separated list, ie:\\n\"\n                                \"--cluster-weight n1=w n2=w\\n\");\n                exit(1);\n            }\n            int widx = i + 1;\n            char **weight = argv + widx;\n            int wargc = 0;\n            for (; widx < argc; widx++) {\n                if (strstr(argv[widx], \"--\") == argv[widx]) break;\n                if (strchr(argv[widx], '=') == NULL) break;\n                wargc++;\n            }\n            if (wargc > 0) {\n                config.cluster_manager_command.weight = weight;\n                config.cluster_manager_command.weight_argc = wargc;\n                i += wargc;\n            }\n        } else if (!strcmp(argv[i],\"--cluster-slots\") && !lastarg) {\n            config.cluster_manager_command.slots = atoi(argv[++i]);\n        } else if (!strcmp(argv[i],\"--cluster-timeout\") && !lastarg) {\n            config.cluster_manager_command.timeout = atoi(argv[++i]);\n        } else if (!strcmp(argv[i],\"--cluster-pipeline\") && !lastarg) {\n            config.cluster_manager_command.pipeline = atoi(argv[++i]);\n        } else if (!strcmp(argv[i],\"--cluster-threshold\") && !lastarg) {\n            config.cluster_manager_command.threshold = atof(argv[++i]);\n        } else if (!strcmp(argv[i],\"--cluster-yes\")) {\n            config.cluster_manager_command.flags |=\n                CLUSTER_MANAGER_CMD_FLAG_YES;\n        } else if (!strcmp(argv[i],\"--cluster-simulate\")) {\n            config.cluster_manager_command.flags |=\n                CLUSTER_MANAGER_CMD_FLAG_SIMULATE;\n        } else if (!strcmp(argv[i],\"--cluster-replace\")) {\n            config.cluster_manager_command.flags |=\n                CLUSTER_MANAGER_CMD_FLAG_REPLACE;\n        } else if (!strcmp(argv[i],\"--cluster-copy\")) {\n            config.cluster_manager_command.flags |=\n                CLUSTER_MANAGER_CMD_FLAG_COPY;\n        } else if (!strcmp(argv[i],\"--cluster-slave\")) {\n            config.cluster_manager_command.flags |=\n                CLUSTER_MANAGER_CMD_FLAG_SLAVE;\n        } else if (!strcmp(argv[i],\"--cluster-use-empty-masters\")) {\n            config.cluster_manager_command.flags |=\n                CLUSTER_MANAGER_CMD_FLAG_EMPTYMASTER;\n        } else if (!strcmp(argv[i],\"--cluster-search-multiple-owners\")) {\n            config.cluster_manager_command.flags |=\n                CLUSTER_MANAGER_CMD_FLAG_CHECK_OWNERS;\n        } else if (!strcmp(argv[i],\"--cluster-fix-with-unreachable-masters\")) {\n            config.cluster_manager_command.flags |=\n                CLUSTER_MANAGER_CMD_FLAG_FIX_WITH_UNREACHABLE_MASTERS;\n#ifdef USE_OPENSSL\n        } else if (!strcmp(argv[i],\"--tls\")) {\n            config.tls = 1;\n        } else if (!strcmp(argv[i],\"--sni\") && !lastarg) {\n            config.sslconfig.sni = argv[++i];\n        } else if (!strcmp(argv[i],\"--cacertdir\") && !lastarg) {\n            config.sslconfig.cacertdir = argv[++i];\n        } else if (!strcmp(argv[i],\"--cacert\") && !lastarg) {\n            config.sslconfig.cacert = argv[++i];\n        } else if (!strcmp(argv[i],\"--cert\") && !lastarg) {\n            config.sslconfig.cert = argv[++i];\n        } else if (!strcmp(argv[i],\"--key\") && !lastarg) {\n            config.sslconfig.key = argv[++i];\n        } else if (!strcmp(argv[i],\"--tls-ciphers\") && !lastarg) {\n            config.sslconfig.ciphers = argv[++i];\n        } else if (!strcmp(argv[i],\"--insecure\")) {\n            config.sslconfig.skip_cert_verify = 1;\n        #ifdef TLS1_3_VERSION\n        } else if (!strcmp(argv[i],\"--tls-ciphersuites\") && !lastarg) {\n            config.sslconfig.ciphersuites = argv[++i];\n        #endif\n#endif\n        } else if (!strcmp(argv[i],\"-v\") || !strcmp(argv[i], \"--version\")) {\n            sds version = cliVersion();\n            printf(\"keydb-cli %s\\n\", version);\n            sdsfree(version);\n            exit(0);\n        } else if (!strcmp(argv[i],\"--no-motd\")) {\n            config.disable_motd = 1;\n        } else if (!strcmp(argv[i],\"-3\")) {\n            config.resp3 = 1;\n        } else if (!strcmp(argv[i],\"--show-pushes\") && !lastarg) {\n            char *argval = argv[++i];\n            if (!strncasecmp(argval, \"n\", 1)) {\n                config.push_output = 0;\n            } else if (!strncasecmp(argval, \"y\", 1)) {\n                config.push_output = 1;\n            } else {\n                fprintf(stderr, \"Unknown --show-pushes value '%s' \"\n                        \"(valid: '[y]es', '[n]o')\\n\", argval);\n            }\n        } else if (!strcmp(argv[i],\"--force\")) {\n            config.force_mode = 1;\n        } else if (CLUSTER_MANAGER_MODE() && argv[i][0] != '-') {\n            if (config.cluster_manager_command.argc == 0) {\n                int j = i + 1;\n                while (j < argc && argv[j][0] != '-') j++;\n                int cmd_argc = j - i;\n                config.cluster_manager_command.argc = cmd_argc;\n                config.cluster_manager_command.argv = argv + i;\n                if (cmd_argc > 1) i = j - 1;\n            }\n        } else {\n            if (argv[i][0] == '-') {\n                fprintf(stderr,\n                    \"Unrecognized option or bad number of args for: '%s'\\n\",\n                    argv[i]);\n                exit(1);\n            } else {\n                /* Likely the command name, stop here. */\n                break;\n            }\n        }\n    }\n\n    if (config.hostsocket && config.cluster_mode) {\n        fprintf(stderr,\"Options -c and -s are mutually exclusive.\\n\");\n        exit(1);\n    }\n\n    /* --ldb requires --eval. */\n    if (config.eval_ldb && config.eval == NULL) {\n        fprintf(stderr,\"Options --ldb and --ldb-sync-mode require --eval.\\n\");\n        fprintf(stderr,\"Try %s --help for more information.\\n\", argv[0]);\n        exit(1);\n    }\n\n    if (!config.no_auth_warning && config.auth != NULL) {\n        fputs(\"Warning: Using a password with '-a' or '-u' option on the command\"\n              \" line interface may not be safe.\\n\", stderr);\n    }\n\n    return i;\n}\n\nstatic void parseEnv() {\n    /* Set auth from env, but do not overwrite CLI arguments if passed */\n    char *auth = getenv(REDIS_CLI_AUTH_ENV);\n    if (auth != NULL && config.auth == NULL) {\n        config.auth = auth;\n    }\n\n    char *cluster_yes = getenv(REDIS_CLI_CLUSTER_YES_ENV);\n    if (cluster_yes != NULL && !strcmp(cluster_yes, \"1\")) {\n        config.cluster_manager_command.flags |= CLUSTER_MANAGER_CMD_FLAG_YES;\n    }\n}\n\nstatic sds readArgFromStdin(void) {\n    char buf[1024];\n    sds arg = sdsempty();\n\n    while(1) {\n        int nread = read(fileno(stdin),buf,1024);\n\n        if (nread == 0) break;\n        else if (nread == -1) {\n            perror(\"Reading from standard input\");\n            exit(1);\n        }\n        arg = sdscatlen(arg,buf,nread);\n    }\n    return arg;\n}\n\nstatic void usage(void) {\n    sds version = cliVersion();\n    fprintf(stderr,\n\"keydb-cli %s\\n\"\n\"\\n\"\n\"Usage: keydb-cli [OPTIONS] [cmd [arg [arg ...]]]\\n\"\n\"  -h <hostname>      Server hostname (default: 127.0.0.1).\\n\"\n\"  -p <port>          Server port (default: 6379).\\n\"\n\"  -s <socket>        Server socket (overrides hostname and port).\\n\"\n\"  -a <password>      Password to use when connecting to the server.\\n\"\n\"                     You can also use the \" REDIS_CLI_AUTH_ENV \" environment\\n\"\n\"                     variable to pass this password more safely\\n\"\n\"                     (if both are used, this argument takes precedence).\\n\"\n\"  --user <username>  Used to send ACL style 'AUTH username pass'. Needs -a.\\n\"\n\"  --pass <password>  Alias of -a for consistency with the new --user option.\\n\"\n\"  --askpass          Force user to input password with mask from STDIN.\\n\"\n\"                     If this argument is used, '-a' and \" REDIS_CLI_AUTH_ENV \"\\n\"\n\"                     environment variable will be ignored.\\n\"\n\"  -u <uri>           Server URI.\\n\"\n\"  -r <repeat>        Execute specified command N times.\\n\"\n\"  -i <interval>      When -r is used, waits <interval> seconds per command.\\n\"\n\"                     It is possible to specify sub-second times like -i 0.1.\\n\"\n\"  -n <db>            Database number.\\n\"\n\"  -3                 Start session in RESP3 protocol mode.\\n\"\n\"  -x                 Read last argument from STDIN.\\n\"\n\"  -d <delimiter>     Delimiter between response bulks for raw formatting (default: \\\\n).\\n\"\n\"  -D <delimiter>     Delimiter between responses for raw formatting (default: \\\\n).\\n\"\n\"  -c                 Enable cluster mode (follow -ASK and -MOVED redirections).\\n\"\n\"  -e                 Return exit error code when command execution fails.\\n\"\n#ifdef USE_OPENSSL\n\"  --tls              Establish a secure TLS connection.\\n\"\n\"  --sni <host>       Server name indication for TLS.\\n\"\n\"  --cacert <file>    CA Certificate file to verify with.\\n\"\n\"  --cacertdir <dir>  Directory where trusted CA certificates are stored.\\n\"\n\"                     If neither cacert nor cacertdir are specified, the default\\n\"\n\"                     system-wide trusted root certs configuration will apply.\\n\"\n\"  --insecure         Allow insecure TLS connection by skipping cert validation.\\n\"\n\"  --cert <file>      Client certificate to authenticate with.\\n\"\n\"  --key <file>       Private key file to authenticate with.\\n\"\n\"  --tls-ciphers <list> Sets the list of prefered ciphers (TLSv1.2 and below)\\n\"\n\"                     in order of preference from highest to lowest separated by colon (\\\":\\\").\\n\"\n\"                     See the ciphers(1ssl) manpage for more information about the syntax of this string.\\n\"\n#ifdef TLS1_3_VERSION\n\"  --tls-ciphersuites <list> Sets the list of prefered ciphersuites (TLSv1.3)\\n\"\n\"                     in order of preference from highest to lowest separated by colon (\\\":\\\").\\n\"\n\"                     See the ciphers(1ssl) manpage for more information about the syntax of this string,\\n\"\n\"                     and specifically for TLSv1.3 ciphersuites.\\n\"\n#endif\n#endif\n\"  --raw              Use raw formatting for replies (default when STDOUT is\\n\"\n\"                     not a tty).\\n\"\n\"  --no-raw           Force formatted output even when STDOUT is not a tty.\\n\"\n\"  --quoted-input     Force input to be handled as quoted strings.\\n\"\n\"  --csv              Output in CSV format.\\n\"\n\"  --show-pushes <yn> Whether to print RESP3 PUSH messages.  Enabled by default when\\n\"\n\"                     STDOUT is a tty but can be overriden with --show-pushes no.\\n\"\n\"  --stat             Print rolling stats about server: mem, clients, ...\\n\"\n\"  --latency          Enter a special mode continuously sampling latency.\\n\"\n\"                     If you use this mode in an interactive session it runs\\n\"\n\"                     forever displaying real-time stats. Otherwise if --raw or\\n\"\n\"                     --csv is specified, or if you redirect the output to a non\\n\"\n\"                     TTY, it samples the latency for 1 second (you can use\\n\"\n\"                     -i to change the interval), then produces a single output\\n\"\n\"                     and exits.\\n\",version);\n\n    fprintf(stderr,\n\"  --latency-history  Like --latency but tracking latency changes over time.\\n\"\n\"                     Default time interval is 15 sec. Change it using -i.\\n\"\n\"  --latency-dist     Shows latency as a spectrum, requires xterm 256 colors.\\n\"\n\"                     Default time interval is 1 sec. Change it using -i.\\n\"\n\"  --lru-test <keys>  Simulate a cache workload with an 80-20 distribution.\\n\"\n\"  --replica          Simulate a replica showing commands received from the master.\\n\"\n\"  --rdb <filename>   Transfer an RDB dump from remote server to local file.\\n\"\n\"                     Use filename of \\\"-\\\" to write to stdout.\\n\"\n\"  --pipe             Transfer raw KeyDB protocol from stdin to server.\\n\"\n\"  --pipe-timeout <n> In --pipe mode, abort with error if after sending all data.\\n\"\n\"                     no reply is received within <n> seconds.\\n\"\n\"                     Default timeout: %d. Use 0 to wait forever.\\n\",\n    REDIS_CLI_DEFAULT_PIPE_TIMEOUT);\n    fprintf(stderr,\n\"  --bigkeys          Sample KeyDB keys looking for keys with many elements (complexity).\\n\"\n\"  --memkeys          Sample KeyDB keys looking for keys consuming a lot of memory.\\n\"\n\"  --memkeys-samples <n> Sample KeyDB keys looking for keys consuming a lot of memory.\\n\"\n\"                     And define number of key elements to sample\\n\"\n\"  --hotkeys          Sample KeyDB keys looking for hot keys.\\n\"\n\"                     only works when maxmemory-policy is *lfu.\\n\"\n\"  --scan             List all keys using the SCAN command.\\n\"\n\"  --pattern <pat>    Keys pattern when using the --scan, --bigkeys or --hotkeys\\n\"\n\"                     options (default: *).\\n\"\n\"  --quoted-pattern <pat> Same as --pattern, but the specified string can be\\n\"\n\"                         quoted, in order to pass an otherwise non binary-safe string.\\n\"\n\"  --intrinsic-latency <sec> Run a test to measure intrinsic system latency.\\n\"\n\"                     The test will run for the specified amount of seconds.\\n\"\n\"  --eval <file>      Send an EVAL command using the Lua script at <file>.\\n\"\n\"  --ldb              Used with --eval enable the Redis Lua debugger.\\n\"\n\"  --ldb-sync-mode    Like --ldb but uses the synchronous Lua debugger, in\\n\"\n\"                     this mode the server is blocked and script changes are\\n\"\n\"                     not rolled back from the server memory.\\n\"\n\"  --cluster <command> [args...] [opts...]\\n\"\n\"                     Cluster Manager command and arguments (see below).\\n\"\n\"  --verbose          Verbose mode.\\n\"\n\"  --no-auth-warning  Don't show warning message when using password on command\\n\"\n\"                     line interface.\\n\"\n\"  --force            Ignore validation and safety checks\\n\"\n\"  --help             Output this help and exit.\\n\"\n\"  --version          Output version and exit.\\n\"\n\"\\n\");\n    /* Using another fprintf call to avoid -Woverlength-strings compile warning */\n    fprintf(stderr,\n\"Cluster Manager Commands:\\n\"\n\"  Use --cluster help to list all available cluster manager commands.\\n\"\n\"\\n\"\n\"Examples:\\n\"\n\"  cat /etc/passwd | keydb-cli -x set mypasswd\\n\"\n\"  keydb-cli get mypasswd\\n\"\n\"  keydb-cli -r 100 lpush mylist x\\n\"\n\"  keydb-cli -r 100 -i 1 info | grep used_memory_human:\\n\"\n\"  keydb-cli --quoted-input set '\\\"null-\\\\x00-separated\\\"' value\\n\"\n\"  keydb-cli --eval myscript.lua key1 key2 , arg1 arg2 arg3\\n\"\n\"  keydb-cli --scan --pattern '*:12345*'\\n\"\n\"\\n\"\n\"  (Note: when using --eval the comma separates KEYS[] from ARGV[] items)\\n\"\n\"\\n\"\n\"When no command is given, keydb-cli starts in interactive mode.\\n\"\n\"Type \\\"help\\\" in interactive mode for information on available commands\\n\"\n\"and settings.\\n\"\n\"\\n\");\n    sdsfree(version);\n    exit(1);\n}\n\nint confirmWithYes(const char *msg, int ignore_force) {\n    /* if --cluster-yes option is set and ignore_force is false,\n     * do not prompt for an answer */\n    if (!ignore_force &&\n        (config.cluster_manager_command.flags & CLUSTER_MANAGER_CMD_FLAG_YES)) {\n        return 1;\n    }\n\n    printf(\"%s (type 'yes' to accept): \", msg);\n    fflush(stdout);\n    char buf[4];\n    int nread = read(fileno(stdin),buf,4);\n    buf[3] = '\\0';\n    return (nread != 0 && !strcmp(\"yes\", buf));\n}\n\n/* Create an sds array from argv, either as-is or by dequoting every\n * element. When quoted is non-zero, may return a NULL to indicate an\n * invalid quoted string.\n */\nstatic sds *getSdsArrayFromArgv(int argc, char **argv, int quoted) {\n    sds *res = sds_malloc(sizeof(sds) * argc);\n\n    for (int j = 0; j < argc; j++) {\n        if (quoted) {\n            sds unquoted = unquoteCString(argv[j]);\n            if (!unquoted) {\n                while (--j >= 0) sdsfree(res[j]);\n                sds_free(res);\n                return NULL;\n            }\n            res[j] = unquoted;\n        } else {\n            res[j] = sdsnew(argv[j]);\n        }\n    }\n\n    return res;\n}\n\nstatic int issueCommandRepeat(int argc, char **argv, long repeat) {\n    while (1) {\n        if (config.cluster_reissue_command || context == NULL ||\n            context->err == REDIS_ERR_IO || context->err == REDIS_ERR_EOF)\n        {\n            if (cliConnect(CC_FORCE) != REDIS_OK) {\n                cliPrintContextError();\n                config.cluster_reissue_command = 0;\n                return REDIS_ERR;\n            }\n        }\n        config.cluster_reissue_command = 0;\n        if (config.cluster_send_asking) {\n            if (cliSendAsking() != REDIS_OK) {\n                cliPrintContextError();\n                return REDIS_ERR;\n            }\n        }\n        if (cliSendCommand(argc,argv,repeat) != REDIS_OK) {\n            cliPrintContextError();\n            return REDIS_ERR;\n        }\n\n        /* Issue the command again if we got redirected in cluster mode */\n        if (config.cluster_mode && config.cluster_reissue_command) {\n            continue;\n        }\n        break;\n    }\n    return REDIS_OK;\n}\n\nstatic int issueCommand(int argc, char **argv) {\n    return issueCommandRepeat(argc, argv, config.repeat);\n}\n\n/* Split the user provided command into multiple SDS arguments.\n * This function normally uses sdssplitargs() from sds.c which is able\n * to understand \"quoted strings\", escapes and so forth. However when\n * we are in Lua debugging mode and the \"eval\" command is used, we want\n * the remaining Lua script (after \"e \" or \"eval \") to be passed verbatim\n * as a single big argument. */\nstatic sds *cliSplitArgs(char *line, int *argc) {\n    if (config.eval_ldb && (strstr(line,\"eval \") == line ||\n                            strstr(line,\"e \") == line))\n    {\n        sds *argv = sds_malloc(sizeof(sds)*2);\n        *argc = 2;\n        int len = strlen(line);\n        int elen = line[1] == ' ' ? 2 : 5; /* \"e \" or \"eval \"? */\n        argv[0] = sdsnewlen(line,elen-1);\n        argv[1] = sdsnewlen(line+elen,len-elen);\n        return argv;\n    } else {\n        return sdssplitargs(line,argc);\n    }\n}\n\n/* Set the CLI preferences. This function is invoked when an interactive\n * \":command\" is called, or when reading ~/.redisclirc file, in order to\n * set user preferences. */\nvoid cliSetPreferences(char **argv, int argc, int interactive) {\n    if (!strcasecmp(argv[0],\":set\") && argc >= 2) {\n        if (!strcasecmp(argv[1],\"hints\")) pref.hints = 1;\n        else if (!strcasecmp(argv[1],\"nohints\")) pref.hints = 0;\n        else {\n            printf(\"%sunknown keydb-cli preference '%s'\\n\",\n                interactive ? \"\" : \".redisclirc: \",\n                argv[1]);\n        }\n    } else {\n        printf(\"%sunknown keydb-cli internal command '%s'\\n\",\n            interactive ? \"\" : \".redisclirc: \",\n            argv[0]);\n    }\n}\n\n/* Load the ~/.redisclirc file if any. */\nvoid cliLoadPreferences(void) {\n    sds rcfile = getDotfilePath(REDIS_CLI_RCFILE_ENV,REDIS_CLI_RCFILE_DEFAULT);\n    if (rcfile == NULL) return;\n    FILE *fp = fopen(rcfile,\"r\");\n    char buf[1024];\n\n    if (fp) {\n        while(fgets(buf,sizeof(buf),fp) != NULL) {\n            sds *argv;\n            int argc;\n\n            argv = sdssplitargs(buf,&argc);\n            if (argc > 0) cliSetPreferences(argv,argc,0);\n            sdsfreesplitres(argv,argc);\n        }\n        fclose(fp);\n    }\n    sdsfree(rcfile);\n}\n\nstatic void repl(void) {\n    sds historyfile = NULL;\n    int history = 0;\n    char *line;\n    int argc;\n    sds *argv;\n\n    /* Initialize the help and, if possible, use the COMMAND command in order\n     * to retrieve missing entries. */\n    cliInitHelp();\n    cliIntegrateHelp();\n\n    config.interactive = 1;\n    linenoiseSetMultiLine(1);\n    linenoiseSetCompletionCallback(completionCallback);\n    linenoiseSetHintsCallback(hintsCallback);\n    linenoiseSetFreeHintsCallback(freeHintsCallback);\n\n    /* Only use history and load the rc file when stdin is a tty. */\n    if (isatty(fileno(stdin))) {\n        historyfile = getDotfilePath(REDIS_CLI_HISTFILE_ENV,REDIS_CLI_HISTFILE_DEFAULT);\n        //keep in-memory history always regardless if history file can be determined\n        history = 1;\n        if (historyfile != NULL) {\n            linenoiseHistoryLoad(historyfile);\n        }\n        cliLoadPreferences();\n    }\n\n    cliRefreshPrompt();\n    while((line = linenoise(context ? config.prompt : \"not connected> \")) != NULL) {\n        if (line[0] != '\\0') {\n            long repeat = 1;\n            int skipargs = 0;\n            char *endptr = NULL;\n\n            argv = cliSplitArgs(line,&argc);\n\n            /* check if we have a repeat command option and\n             * need to skip the first arg */\n            if (argv && argc > 0) {\n                errno = 0;\n                repeat = strtol(argv[0], &endptr, 10);\n                if (argc > 1 && *endptr == '\\0') {\n                    if (errno == ERANGE || errno == EINVAL || repeat <= 0) {\n                        fputs(\"Invalid keydb-cli repeat command option value.\\n\", stdout);\n                        sdsfreesplitres(argv, argc);\n                        linenoiseFree(line);\n                        continue;\n                    }\n                    skipargs = 1;\n                } else {\n                    repeat = 1;\n                }\n            }\n\n            /* Won't save auth or acl setuser commands in history file */\n            int dangerous = 0;\n            if (argv && argc > 0) {\n                if (!strcasecmp(argv[skipargs], \"auth\")) {\n                    dangerous = 1;\n                } else if (skipargs+1 < argc &&\n                           !strcasecmp(argv[skipargs], \"acl\") &&\n                           !strcasecmp(argv[skipargs+1], \"setuser\"))\n                {\n                    dangerous = 1;\n                }\n            }\n\n            if (!dangerous) {\n                if (history) linenoiseHistoryAdd(line);\n                if (historyfile) linenoiseHistorySave(historyfile);\n            }\n\n            if (argv == NULL) {\n                printf(\"Invalid argument(s)\\n\");\n                fflush(stdout);\n                linenoiseFree(line);\n                continue;\n            } else if (argc > 0) {\n                if (strcasecmp(argv[0],\"quit\") == 0 ||\n                    strcasecmp(argv[0],\"exit\") == 0)\n                {\n                    exit(0);\n                } else if (argv[0][0] == ':') {\n                    cliSetPreferences(argv,argc,1);\n                    sdsfreesplitres(argv,argc);\n                    linenoiseFree(line);\n                    continue;\n                } else if (strcasecmp(argv[0],\"restart\") == 0) {\n                    if (config.eval) {\n                        config.eval_ldb = 1;\n                        config.output = OUTPUT_RAW;\n                        sdsfreesplitres(argv,argc);\n                        linenoiseFree(line);\n                        return; /* Return to evalMode to restart the session. */\n                    } else {\n                        printf(\"Use 'restart' only in Lua debugging mode.\");\n                    }\n                } else if (argc == 3 && !strcasecmp(argv[0],\"connect\")) {\n                    sdsfree(config.hostip);\n                    config.hostip = sdsnew(argv[1]);\n                    config.hostport = atoi(argv[2]);\n                    cliRefreshPrompt();\n                    cliConnect(CC_FORCE);\n                } else if (argc == 1 && !strcasecmp(argv[0],\"clear\")) {\n                    linenoiseClearScreen();\n                } else {\n                    long long start_time = mstime(), elapsed;\n\n                    issueCommandRepeat(argc-skipargs, argv+skipargs, repeat);\n\n                    /* If our debugging session ended, show the EVAL final\n                     * reply. */\n                    if (config.eval_ldb_end) {\n                        config.eval_ldb_end = 0;\n                        cliReadReply(0);\n                        printf(\"\\n(Lua debugging session ended%s)\\n\\n\",\n                            config.eval_ldb_sync ? \"\" :\n                            \" -- dataset changes rolled back\");\n                    }\n\n                    elapsed = mstime()-start_time;\n                    if (elapsed >= 500 &&\n                        config.output == OUTPUT_STANDARD)\n                    {\n                        printf(\"(%.2fs)\\n\",(double)elapsed/1000);\n                    }\n                }\n            }\n            /* Free the argument vector */\n            sdsfreesplitres(argv,argc);\n        }\n        /* linenoise() returns malloc-ed lines like readline() */\n        linenoiseFree(line);\n    }\n    exit(0);\n}\n\nstatic int noninteractive(int argc, char **argv) {\n    int retval = 0;\n    sds *sds_args = getSdsArrayFromArgv(argc, argv, config.quoted_input);\n    if (!sds_args) {\n        printf(\"Invalid quoted string\\n\");\n        return 1;\n    }\n    if (config.stdinarg) {\n        sds_args = sds_realloc(sds_args, (argc + 1) * sizeof(sds));\n        sds_args[argc] = readArgFromStdin();\n        argc++;\n    }\n\n    retval = issueCommand(argc, sds_args);\n    sdsfreesplitres(sds_args, argc);\n    return retval;\n}\n\n/*------------------------------------------------------------------------------\n * Eval mode\n *--------------------------------------------------------------------------- */\n\nstatic int evalMode(int argc, char **argv) {\n    sds script = NULL;\n    FILE *fp;\n    char buf[1024];\n    size_t nread;\n    char **argv2;\n    int j, got_comma, keys;\n    int retval = REDIS_OK;\n\n    while(1) {\n        if (config.eval_ldb) {\n            printf(\n            \"Lua debugging session started, please use:\\n\"\n            \"quit    -- End the session.\\n\"\n            \"restart -- Restart the script in debug mode again.\\n\"\n            \"help    -- Show Lua script debugging commands.\\n\\n\"\n            );\n        }\n\n        sdsfree(script);\n        script = sdsempty();\n        got_comma = 0;\n        keys = 0;\n\n        /* Load the script from the file, as an sds string. */\n        fp = fopen(config.eval,\"r\");\n        if (!fp) {\n            fprintf(stderr,\n                \"Can't open file '%s': %s\\n\", config.eval, strerror(errno));\n            exit(1);\n        }\n        while((nread = fread(buf,1,sizeof(buf),fp)) != 0) {\n            script = sdscatlen(script,buf,nread);\n        }\n        fclose(fp);\n\n        /* If we are debugging a script, enable the Lua debugger. */\n        if (config.eval_ldb) {\n            redisReply *reply = redisCommand(context,\n                    config.eval_ldb_sync ?\n                    \"SCRIPT DEBUG sync\": \"SCRIPT DEBUG yes\");\n            if (reply) freeReplyObject(reply);\n        }\n\n        /* Create our argument vector */\n        argv2 = zmalloc(sizeof(sds)*(argc+3), MALLOC_LOCAL);\n        argv2[0] = sdsnew(\"EVAL\");\n        argv2[1] = script;\n        for (j = 0; j < argc; j++) {\n            if (!got_comma && argv[j][0] == ',' && argv[j][1] == 0) {\n                got_comma = 1;\n                continue;\n            }\n            argv2[j+3-got_comma] = sdsnew(argv[j]);\n            if (!got_comma) keys++;\n        }\n        argv2[2] = sdscatprintf(sdsempty(),\"%d\",keys);\n\n        /* Call it */\n        int eval_ldb = config.eval_ldb; /* Save it, may be reverted. */\n        retval = issueCommand(argc+3-got_comma, argv2);\n        if (eval_ldb) {\n            if (!config.eval_ldb) {\n                /* If the debugging session ended immediately, there was an\n                 * error compiling the script. Show it and they don't enter\n                 * the REPL at all. */\n                printf(\"Eval debugging session can't start:\\n\");\n                cliReadReply(0);\n                break; /* Return to the caller. */\n            } else {\n                strncpy(config.prompt,\"lua debugger> \",sizeof(config.prompt));\n                repl();\n                /* Restart the session if repl() returned. */\n                cliConnect(CC_FORCE);\n                printf(\"\\n\");\n            }\n        } else {\n            break; /* Return to the caller. */\n        }\n    }\n    return retval;\n}\n\n/*------------------------------------------------------------------------------\n * Cluster Manager\n *--------------------------------------------------------------------------- */\n\n/* The Cluster Manager global structure */\nstruct cluster_manager;\n\n\ntypedef int clusterManagerCommandProc(int argc, char **argv);\ntypedef int (*clusterManagerOnReplyError)(redisReply *reply,\n    clusterManagerNode *n, int bulk_idx);\n\n/* Cluster Manager helper functions */\n\nstatic clusterManagerNode *clusterManagerNewNode(char *ip, int port);\nstatic clusterManagerNode *clusterManagerNodeByName(const char *name);\nstatic clusterManagerNode *clusterManagerNodeByAbbreviatedName(const char *n);\nstatic void clusterManagerNodeResetSlots(clusterManagerNode *node);\nstatic int clusterManagerNodeIsCluster(clusterManagerNode *node, char **err);\nstatic void clusterManagerPrintNotClusterNodeError(clusterManagerNode *node,\n                                                   char *err);\nstatic int clusterManagerNodeLoadInfo(clusterManagerNode *node, int opts,\n                                      char **err);\nstatic int clusterManagerLoadInfoFromNode(clusterManagerNode *node, int opts);\nstatic int clusterManagerNodeIsEmpty(clusterManagerNode *node, char **err);\nstatic void clusterManagerOptimizeAntiAffinity(clusterManagerNodeArray *ipnodes,\n    int ip_count);\nstatic sds clusterManagerNodeInfo(clusterManagerNode *node, int indent);\nstatic void clusterManagerShowClusterInfo(void);\nstatic int clusterManagerFlushNodeConfig(clusterManagerNode *node, char **err);\nvoid clusterManagerWaitForClusterJoin(void);\nint clusterManagerCheckCluster(int quiet);\nvoid clusterManagerOnError(sds err);\nstatic void clusterManagerNodeArrayInit(clusterManagerNodeArray *array,\n                                        int len);\nstatic void clusterManagerNodeArrayReset(clusterManagerNodeArray *array);\nstatic void clusterManagerNodeArrayShift(clusterManagerNodeArray *array,\n                                         clusterManagerNode **nodeptr);\nstatic void clusterManagerNodeArrayAdd(clusterManagerNodeArray *array,\n                                       clusterManagerNode *node);\n\n/* Cluster Manager commands. */\n\nstatic int clusterManagerCommandCreate(int argc, char **argv);\nstatic int clusterManagerCommandAddNode(int argc, char **argv);\nstatic int clusterManagerCommandDeleteNode(int argc, char **argv);\nstatic int clusterManagerCommandInfo(int argc, char **argv);\nstatic int clusterManagerCommandCheck(int argc, char **argv);\nstatic int clusterManagerCommandFix(int argc, char **argv);\nstatic int clusterManagerCommandReshard(int argc, char **argv);\nstatic int clusterManagerCommandRebalance(int argc, char **argv);\nstatic int clusterManagerCommandSetTimeout(int argc, char **argv);\nstatic int clusterManagerCommandImport(int argc, char **argv);\nstatic int clusterManagerCommandCall(int argc, char **argv);\nstatic int clusterManagerCommandHelp(int argc, char **argv);\nstatic int clusterManagerCommandBackup(int argc, char **argv);\n\ntypedef struct clusterManagerCommandDef {\n    char *name;\n    clusterManagerCommandProc *proc;\n    int arity;\n    char *args;\n    char *options;\n} clusterManagerCommandDef;\n\nclusterManagerCommandDef clusterManagerCommands[] = {\n    {\"create\", clusterManagerCommandCreate, -2, \"host1:port1 ... hostN:portN\",\n     \"replicas <arg>\"},\n    {\"check\", clusterManagerCommandCheck, -1, \"host:port\",\n     \"search-multiple-owners\"},\n    {\"info\", clusterManagerCommandInfo, -1, \"host:port\", NULL},\n    {\"fix\", clusterManagerCommandFix, -1, \"host:port\",\n     \"search-multiple-owners,fix-with-unreachable-masters\"},\n    {\"reshard\", clusterManagerCommandReshard, -1, \"host:port\",\n     \"from <arg>,to <arg>,slots <arg>,yes,timeout <arg>,pipeline <arg>,\"\n     \"replace\"},\n    {\"rebalance\", clusterManagerCommandRebalance, -1, \"host:port\",\n     \"weight <node1=w1...nodeN=wN>,use-empty-masters,\"\n     \"timeout <arg>,simulate,pipeline <arg>,threshold <arg>,replace\"},\n    {\"add-node\", clusterManagerCommandAddNode, 2,\n     \"new_host:new_port existing_host:existing_port\", \"slave,master-id <arg>\"},\n    {\"del-node\", clusterManagerCommandDeleteNode, 2, \"host:port node_id\",NULL},\n    {\"call\", clusterManagerCommandCall, -2,\n        \"host:port command arg arg .. arg\", \"only-masters,only-replicas\"},\n    {\"set-timeout\", clusterManagerCommandSetTimeout, 2,\n     \"host:port milliseconds\", NULL},\n    {\"import\", clusterManagerCommandImport, 1, \"host:port\",\n     \"from <arg>,from-user <arg>,from-pass <arg>,from-askpass,copy,replace\"},\n    {\"backup\", clusterManagerCommandBackup, 2,  \"host:port backup_directory\",\n     NULL},\n    {\"help\", clusterManagerCommandHelp, 0, NULL, NULL}\n};\n\ntypedef struct clusterManagerOptionDef {\n    char *name;\n    char *desc;\n} clusterManagerOptionDef;\n\nclusterManagerOptionDef clusterManagerOptions[] = {\n    {\"--cluster-yes\", \"Automatic yes to cluster commands prompts\"}\n};\n\nstatic void getRDB(clusterManagerNode *node);\n\nvoid createClusterManagerCommand(char *cmdname, int argc, char **argv) {\n    clusterManagerCommand *cmd = &config.cluster_manager_command;\n    cmd->name = cmdname;\n    cmd->argc = argc;\n    cmd->argv = argc ? argv : NULL;\n    if (isColorTerm()) cmd->flags |= CLUSTER_MANAGER_CMD_FLAG_COLOR;\n}\n\n\nstatic clusterManagerCommandProc *validateClusterManagerCommand(void) {\n    int i, commands_count = sizeof(clusterManagerCommands) /\n                            sizeof(clusterManagerCommandDef);\n    clusterManagerCommandProc *proc = NULL;\n    char *cmdname = config.cluster_manager_command.name;\n    int argc = config.cluster_manager_command.argc;\n    for (i = 0; i < commands_count; i++) {\n        clusterManagerCommandDef cmddef = clusterManagerCommands[i];\n        if (!strcmp(cmddef.name, cmdname)) {\n            if ((cmddef.arity > 0 && argc != cmddef.arity) ||\n                (cmddef.arity < 0 && argc < (cmddef.arity * -1))) {\n                fprintf(stderr, \"[ERR] Wrong number of arguments for \"\n                                \"specified --cluster sub command\\n\");\n                return NULL;\n            }\n            proc = cmddef.proc;\n        }\n    }\n    if (!proc) fprintf(stderr, \"Unknown --cluster subcommand\\n\");\n    return proc;\n}\n\nint parseClusterNodeAddress(char *addr, char **ip_ptr, int *port_ptr,\n                                   int *bus_port_ptr)\n{\n    char *c = strrchr(addr, '@');\n    if (c != NULL) {\n        *c = '\\0';\n        if (bus_port_ptr != NULL)\n            *bus_port_ptr = atoi(c + 1);\n    }\n    c = strrchr(addr, ':');\n    if (c != NULL) {\n        *c = '\\0';\n        *ip_ptr = addr;\n        *port_ptr = atoi(++c);\n    } else return 0;\n    return 1;\n}\n\n/* Get host ip and port from command arguments. If only one argument has\n * been provided it must be in the form of 'ip:port', elsewhere\n * the first argument must be the ip and the second one the port.\n * If host and port can be detected, it returns 1 and it stores host and\n * port into variables referenced by'ip_ptr' and 'port_ptr' pointers,\n * elsewhere it returns 0. */\nstatic int getClusterHostFromCmdArgs(int argc, char **argv,\n                                     char **ip_ptr, int *port_ptr) {\n    int port = 0;\n    char *ip = NULL;\n    if (argc == 1) {\n        char *addr = argv[0];\n        if (!parseClusterNodeAddress(addr, &ip, &port, NULL)) return 0;\n    } else {\n        ip = argv[0];\n        port = atoi(argv[1]);\n    }\n    if (!ip || !port) return 0;\n    else {\n        *ip_ptr = ip;\n        *port_ptr = port;\n    }\n    return 1;\n}\n\nstatic void freeClusterManagerNodeFlags(list *flags) {\n    listIter li;\n    listNode *ln;\n    listRewind(flags, &li);\n    while ((ln = listNext(&li)) != NULL) {\n        sds flag = ln->value;\n        sdsfree(flag);\n    }\n    listRelease(flags);\n}\n\nvoid freeClusterManagerNode(clusterManagerNode *node) {\n    if (node->context != NULL) redisFree(node->context);\n    if (node->friends != NULL) {\n        listIter li;\n        listNode *ln;\n        listRewind(node->friends,&li);\n        while ((ln = listNext(&li)) != NULL) {\n            clusterManagerNode *fn = ln->value;\n            freeClusterManagerNode(fn);\n        }\n        listRelease(node->friends);\n        node->friends = NULL;\n    }\n    if (node->name != NULL) sdsfree(node->name);\n    if (node->replicate != NULL) sdsfree(node->replicate);\n    if ((node->flags & CLUSTER_MANAGER_FLAG_FRIEND) && node->ip)\n        sdsfree(node->ip);\n    int i;\n    if (node->migrating != NULL) {\n        for (i = 0; i < node->migrating_count; i++) sdsfree(node->migrating[i]);\n        zfree(node->migrating);\n    }\n    if (node->importing != NULL) {\n        for (i = 0; i < node->importing_count; i++) sdsfree(node->importing[i]);\n        zfree(node->importing);\n    }\n    if (node->flags_str != NULL) {\n        freeClusterManagerNodeFlags(node->flags_str);\n        node->flags_str = NULL;\n    }\n    zfree(node);\n}\n\nvoid freeClusterManager(void);\n\nstatic clusterManagerNode *clusterManagerNewNode(char *ip, int port) {\n    clusterManagerNode *node = zmalloc(sizeof(*node), MALLOC_LOCAL);\n    node->context = NULL;\n    node->name = NULL;\n    node->ip = ip;\n    node->port = port;\n    node->current_epoch = 0;\n    node->ping_sent = 0;\n    node->ping_recv = 0;\n    node->flags = 0;\n    node->flags_str = NULL;\n    node->replicate = NULL;\n    node->dirty = 0;\n    node->friends = NULL;\n    node->migrating = NULL;\n    node->importing = NULL;\n    node->migrating_count = 0;\n    node->importing_count = 0;\n    node->replicas_count = 0;\n    node->weight = 1.0f;\n    node->balance = 0;\n    clusterManagerNodeResetSlots(node);\n    return node;\n}\n\nstatic sds clusterManagerGetNodeRDBFilename(clusterManagerNode *node) {\n    assert(config.cluster_manager_command.backup_dir);\n    sds filename = sdsnew(config.cluster_manager_command.backup_dir);\n    if (filename[sdslen(filename) - 1] != '/')\n        filename = sdscat(filename, \"/\");\n    filename = sdscatprintf(filename, \"redis-node-%s-%d-%s.rdb\", node->ip,\n                            node->port, node->name);\n    return filename;\n}\n\n/* Check whether reply is NULL or its type is REDIS_REPLY_ERROR. In the\n * latest case, if the 'err' arg is not NULL, it gets allocated with a copy\n * of reply error (it's up to the caller function to free it), elsewhere\n * the error is directly printed. */\nint clusterManagerCheckRedisReply(clusterManagerNode *n,\n                                         redisReply *r, char **err)\n{\n    int is_err = 0;\n    if (!r || (is_err = (r->type == REDIS_REPLY_ERROR))) {\n        if (is_err) {\n            if (err != NULL) {\n                *err = zmalloc((r->len + 1) * sizeof(char), MALLOC_LOCAL);\n                strcpy(*err, r->str);\n            } else CLUSTER_MANAGER_PRINT_REPLY_ERROR(n, r->str);\n        }\n        return 0;\n    }\n    return 1;\n}\n\n/* Call MULTI command on a cluster node. */\nstatic int clusterManagerStartTransaction(clusterManagerNode *node) {\n    redisReply *reply = CLUSTER_MANAGER_COMMAND(node, \"MULTI\");\n    int success = clusterManagerCheckRedisReply(node, reply, NULL);\n    if (reply) freeReplyObject(reply);\n    return success;\n}\n\n/* Call EXEC command on a cluster node. */\nstatic int clusterManagerExecTransaction(clusterManagerNode *node,\n                                         clusterManagerOnReplyError onerror)\n{\n    redisReply *reply = CLUSTER_MANAGER_COMMAND(node, \"EXEC\");\n    int success = clusterManagerCheckRedisReply(node, reply, NULL);\n    if (success) {\n        if (reply->type != REDIS_REPLY_ARRAY) {\n            success = 0;\n            goto cleanup;\n        }\n        size_t i;\n        for (i = 0; i < reply->elements; i++) {\n            redisReply *r = reply->element[i];\n            char *err = NULL;\n            success = clusterManagerCheckRedisReply(node, r, &err);\n            if (!success && onerror) success = onerror(r, node, i);\n            if (err) {\n                if (!success)\n                    CLUSTER_MANAGER_PRINT_REPLY_ERROR(node, err);\n                zfree(err);\n            }\n            if (!success) break;\n        }\n    }\ncleanup:\n    if (reply) freeReplyObject(reply);\n    return success;\n}\n\nstatic int clusterManagerNodeConnect(clusterManagerNode *node) {\n    if (node->context) redisFree(node->context);\n    struct timeval tv;\n    tv.tv_sec = config.cluster_manager_command.timeout / 1000;\n    tv.tv_usec = (config.cluster_manager_command.timeout % 1000) * 1000;\n    node->context = redisConnectWithTimeout(node->ip, node->port, tv);\n    if (!node->context->err && config.tls) {\n        const char *err = NULL;\n        if (cliSecureConnection(node->context, config.sslconfig, &err) == REDIS_ERR && err) {\n            fprintf(stderr,\"TLS Error: %s\\n\", err);\n            redisFree(node->context);\n            node->context = NULL;\n            return 0;\n        }\n    }\n    if (node->context->err) {\n        fprintf(stderr,\"Could not connect to KeyDB at \");\n        fprintf(stderr,\"%s:%d: %s\\n\", node->ip, node->port,\n                node->context->errstr);\n        redisFree(node->context);\n        node->context = NULL;\n        return 0;\n    }\n    /* Set aggressive KEEP_ALIVE socket option in the Redis context socket\n     * in order to prevent timeouts caused by the execution of long\n     * commands. At the same time this improves the detection of real\n     * errors. */\n    anetKeepAlive(NULL, node->context->fd, REDIS_CLI_KEEPALIVE_INTERVAL);\n    if (config.auth) {\n        redisReply *reply;\n        if (config.user == NULL)\n            reply = redisCommand(node->context,\"AUTH %s\", config.auth);\n        else\n            reply = redisCommand(node->context,\"AUTH %s %s\",\n                                 config.user,config.auth);\n        int ok = clusterManagerCheckRedisReply(node, reply, NULL);\n        if (reply != NULL) freeReplyObject(reply);\n        if (!ok) return 0;\n    }\n    return 1;\n}\n\nstatic void clusterManagerRemoveNodeFromList(list *nodelist,\n                                             clusterManagerNode *node) {\n    listIter li;\n    listNode *ln;\n    listRewind(nodelist, &li);\n    while ((ln = listNext(&li)) != NULL) {\n        if (node == ln->value) {\n            listDelNode(nodelist, ln);\n            break;\n        }\n    }\n}\n\n/* Return the node with the specified name (ID) or NULL. */\nstatic clusterManagerNode *clusterManagerNodeByName(const char *name) {\n    if (cluster_manager.nodes == NULL) return NULL;\n    clusterManagerNode *found = NULL;\n    sds lcname = sdsempty();\n    lcname = sdscpy(lcname, name);\n    sdstolower(lcname);\n    listIter li;\n    listNode *ln;\n    listRewind(cluster_manager.nodes, &li);\n    while ((ln = listNext(&li)) != NULL) {\n        clusterManagerNode *n = ln->value;\n        if (n->name && !sdscmp(n->name, lcname)) {\n            found = n;\n            break;\n        }\n    }\n    sdsfree(lcname);\n    return found;\n}\n\n/* Like clusterManagerNodeByName but the specified name can be just the first\n * part of the node ID as long as the prefix in unique across the\n * cluster.\n */\nstatic clusterManagerNode *clusterManagerNodeByAbbreviatedName(const char*name)\n{\n    if (cluster_manager.nodes == NULL) return NULL;\n    clusterManagerNode *found = NULL;\n    sds lcname = sdsempty();\n    lcname = sdscpy(lcname, name);\n    sdstolower(lcname);\n    listIter li;\n    listNode *ln;\n    listRewind(cluster_manager.nodes, &li);\n    while ((ln = listNext(&li)) != NULL) {\n        clusterManagerNode *n = ln->value;\n        if (n->name &&\n            strstr(n->name, lcname) == n->name) {\n            found = n;\n            break;\n        }\n    }\n    sdsfree(lcname);\n    return found;\n}\n\nstatic void clusterManagerNodeResetSlots(clusterManagerNode *node) {\n    memset(node->slots, 0, sizeof(node->slots));\n    node->slots_count = 0;\n}\n\n/* Call \"INFO\" redis command on the specified node and return the reply. */\nstatic redisReply *clusterManagerGetNodeRedisInfo(clusterManagerNode *node,\n                                                  char **err)\n{\n    redisReply *info = CLUSTER_MANAGER_COMMAND(node, \"INFO\");\n    if (err != NULL) *err = NULL;\n    if (info == NULL) return NULL;\n    if (info->type == REDIS_REPLY_ERROR) {\n        if (err != NULL) {\n            *err = zmalloc((info->len + 1) * sizeof(char), MALLOC_LOCAL);\n            strcpy(*err, info->str);\n        }\n        freeReplyObject(info);\n        return  NULL;\n    }\n    return info;\n}\n\nstatic int clusterManagerNodeIsCluster(clusterManagerNode *node, char **err) {\n    redisReply *info = clusterManagerGetNodeRedisInfo(node, err);\n    if (info == NULL) return 0;\n    int is_cluster = (int) getLongInfoField(info->str, \"cluster_enabled\");\n    freeReplyObject(info);\n    return is_cluster;\n}\n\n/* Checks whether the node is empty. Node is considered not-empty if it has\n * some key or if it already knows other nodes */\nstatic int clusterManagerNodeIsEmpty(clusterManagerNode *node, char **err) {\n    redisReply *info = clusterManagerGetNodeRedisInfo(node, err);\n    int is_empty = 1;\n    if (info == NULL) return 0;\n    if (strstr(info->str, \"db0:\") != NULL) {\n        is_empty = 0;\n        goto result;\n    }\n    freeReplyObject(info);\n    info = CLUSTER_MANAGER_COMMAND(node, \"CLUSTER INFO\");\n    if (err != NULL) *err = NULL;\n    if (!clusterManagerCheckRedisReply(node, info, err)) {\n        is_empty = 0;\n        goto result;\n    }\n    long known_nodes = getLongInfoField(info->str, \"cluster_known_nodes\");\n    is_empty = (known_nodes == 1);\nresult:\n    freeReplyObject(info);\n    return is_empty;\n}\n\nstatic void clusterManagerOptimizeAntiAffinity(clusterManagerNodeArray *ipnodes,\n    int ip_count)\n{\n    clusterManagerNode **offenders = NULL;\n    int score = clusterManagerGetAntiAffinityScore(ipnodes, ip_count,\n                                                   NULL, NULL);\n    if (score == 0) goto cleanup;\n    clusterManagerLogInfo(\">>> Trying to optimize slaves allocation \"\n                          \"for anti-affinity\\n\");\n    int node_len = cluster_manager.nodes->len;\n    int maxiter = 500 * node_len; // Effort is proportional to cluster size...\n    srand(time(NULL));\n    while (maxiter > 0) {\n        int offending_len = 0;\n        if (offenders != NULL) {\n            zfree(offenders);\n            offenders = NULL;\n        }\n        score = clusterManagerGetAntiAffinityScore(ipnodes,\n                                                   ip_count,\n                                                   &offenders,\n                                                   &offending_len);\n        if (score == 0 || offending_len == 0) break; // Optimal anti affinity reached\n        /* We'll try to randomly swap a slave's assigned master causing\n         * an affinity problem with another random slave, to see if we\n         * can improve the affinity. */\n        int rand_idx = rand() % offending_len;\n        clusterManagerNode *first = offenders[rand_idx],\n                           *second = NULL;\n        clusterManagerNode **other_replicas = zcalloc((node_len - 1) *\n                                                      sizeof(*other_replicas), MALLOC_LOCAL);\n        int other_replicas_count = 0;\n        listIter li;\n        listNode *ln;\n        listRewind(cluster_manager.nodes, &li);\n        while ((ln = listNext(&li)) != NULL) {\n            clusterManagerNode *n = ln->value;\n            if (n != first && n->replicate != NULL)\n                other_replicas[other_replicas_count++] = n;\n        }\n        if (other_replicas_count == 0) {\n            zfree(other_replicas);\n            break;\n        }\n        rand_idx = rand() % other_replicas_count;\n        second = other_replicas[rand_idx];\n        char *first_master = first->replicate,\n             *second_master = second->replicate;\n        first->replicate = second_master, first->dirty = 1;\n        second->replicate = first_master, second->dirty = 1;\n        int new_score = clusterManagerGetAntiAffinityScore(ipnodes,\n                                                           ip_count,\n                                                           NULL, NULL);\n        /* If the change actually makes thing worse, revert. Otherwise\n         * leave as it is because the best solution may need a few\n         * combined swaps. */\n        if (new_score > score) {\n            first->replicate = first_master;\n            second->replicate = second_master;\n        }\n        zfree(other_replicas);\n        maxiter--;\n    }\n    score = clusterManagerGetAntiAffinityScore(ipnodes, ip_count, NULL, NULL);\n    char *msg;\n    int perfect = (score == 0);\n    int log_level = (perfect ? CLUSTER_MANAGER_LOG_LVL_SUCCESS :\n                               CLUSTER_MANAGER_LOG_LVL_WARN);\n    if (perfect) msg = \"[OK] Perfect anti-affinity obtained!\";\n    else if (score >= 10000)\n        msg = (\"[WARNING] Some slaves are in the same host as their master\");\n    else\n        msg=(\"[WARNING] Some slaves of the same master are in the same host\");\n    clusterManagerLog(log_level, \"%s\\n\", msg);\ncleanup:\n    zfree(offenders);\n}\n\n/* Return a representable string of the node's flags */\nstatic sds clusterManagerNodeFlagString(clusterManagerNode *node) {\n    sds flags = sdsempty();\n    if (!node->flags_str) return flags;\n    int empty = 1;\n    listIter li;\n    listNode *ln;\n    listRewind(node->flags_str, &li);\n    while ((ln = listNext(&li)) != NULL) {\n        sds flag = ln->value;\n        if (strcmp(flag, \"myself\") == 0) continue;\n        if (!empty) flags = sdscat(flags, \",\");\n        flags = sdscatfmt(flags, \"%S\", flag);\n        empty = 0;\n    }\n    return flags;\n}\n\n/* Return a representable string of the node's slots */\nstatic sds clusterManagerNodeSlotsString(clusterManagerNode *node) {\n    sds slots = sdsempty();\n    int first_range_idx = -1, last_slot_idx = -1, i;\n    for (i = 0; i < CLUSTER_MANAGER_SLOTS; i++) {\n        int has_slot = node->slots[i];\n        if (has_slot) {\n            if (first_range_idx == -1) {\n                if (sdslen(slots)) slots = sdscat(slots, \",\");\n                first_range_idx = i;\n                slots = sdscatfmt(slots, \"[%u\", i);\n            }\n            last_slot_idx = i;\n        } else {\n            if (last_slot_idx >= 0) {\n                if (first_range_idx == last_slot_idx)\n                    slots = sdscat(slots, \"]\");\n                else slots = sdscatfmt(slots, \"-%u]\", last_slot_idx);\n            }\n            last_slot_idx = -1;\n            first_range_idx = -1;\n        }\n    }\n    if (last_slot_idx >= 0) {\n        if (first_range_idx == last_slot_idx) slots = sdscat(slots, \"]\");\n        else slots = sdscatfmt(slots, \"-%u]\", last_slot_idx);\n    }\n    return slots;\n}\n\nstatic sds clusterManagerNodeGetJSON(clusterManagerNode *node,\n                                     unsigned long error_count)\n{\n    sds json = sdsempty();\n    sds replicate = sdsempty();\n    if (node->replicate)\n        replicate = sdscatprintf(replicate, \"\\\"%s\\\"\", node->replicate);\n    else\n        replicate = sdscat(replicate, \"null\");\n    sds slots = clusterManagerNodeSlotsString(node);\n    sds flags = clusterManagerNodeFlagString(node);\n    char *p = slots;\n    while ((p = strchr(p, '-')) != NULL)\n        *(p++) = ',';\n    json = sdscatprintf(json,\n        \"  {\\n\"\n        \"    \\\"name\\\": \\\"%s\\\",\\n\"\n        \"    \\\"host\\\": \\\"%s\\\",\\n\"\n        \"    \\\"port\\\": %d,\\n\"\n        \"    \\\"replicate\\\": %s,\\n\"\n        \"    \\\"slots\\\": [%s],\\n\"\n        \"    \\\"slots_count\\\": %d,\\n\"\n        \"    \\\"flags\\\": \\\"%s\\\",\\n\"\n        \"    \\\"current_epoch\\\": %llu\",\n        node->name,\n        node->ip,\n        node->port,\n        replicate,\n        slots,\n        node->slots_count,\n        flags,\n        (unsigned long long)node->current_epoch\n    );\n    if (error_count > 0) {\n        json = sdscatprintf(json, \",\\n    \\\"cluster_errors\\\": %lu\",\n                            error_count);\n    }\n    if (node->migrating_count > 0 && node->migrating != NULL) {\n        int i = 0;\n        sds migrating = sdsempty();\n        for (; i < node->migrating_count; i += 2) {\n            sds slot = node->migrating[i];\n            sds dest = node->migrating[i + 1];\n            if (slot && dest) {\n                if (sdslen(migrating) > 0) migrating = sdscat(migrating, \",\");\n                migrating = sdscatfmt(migrating, \"\\\"%S\\\": \\\"%S\\\"\", slot, dest);\n            }\n        }\n        if (sdslen(migrating) > 0)\n            json = sdscatfmt(json, \",\\n    \\\"migrating\\\": {%S}\", migrating);\n        sdsfree(migrating);\n    }\n    if (node->importing_count > 0 && node->importing != NULL) {\n        int i = 0;\n        sds importing = sdsempty();\n        for (; i < node->importing_count; i += 2) {\n            sds slot = node->importing[i];\n            sds from = node->importing[i + 1];\n            if (slot && from) {\n                if (sdslen(importing) > 0) importing = sdscat(importing, \",\");\n                importing = sdscatfmt(importing, \"\\\"%S\\\": \\\"%S\\\"\", slot, from);\n            }\n        }\n        if (sdslen(importing) > 0)\n            json = sdscatfmt(json, \",\\n    \\\"importing\\\": {%S}\", importing);\n        sdsfree(importing);\n    }\n    json = sdscat(json, \"\\n  }\");\n    sdsfree(replicate);\n    sdsfree(slots);\n    sdsfree(flags);\n    return json;\n}\n\n\n/* -----------------------------------------------------------------------------\n * Key space handling\n * -------------------------------------------------------------------------- */\n\n/* We have 16384 hash slots. The hash slot of a given key is obtained\n * as the least significant 14 bits of the crc16 of the key.\n *\n * However if the key contains the {...} pattern, only the part between\n * { and } is hashed. This may be useful in the future to force certain\n * keys to be in the same node (assuming no resharding is in progress). */\nstatic unsigned int clusterManagerKeyHashSlot(char *key, int keylen) {\n    int s, e; /* start-end indexes of { and } */\n\n    for (s = 0; s < keylen; s++)\n        if (key[s] == '{') break;\n\n    /* No '{' ? Hash the whole key. This is the base case. */\n    if (s == keylen) return crc16(key,keylen) & 0x3FFF;\n\n    /* '{' found? Check if we have the corresponding '}'. */\n    for (e = s+1; e < keylen; e++)\n        if (key[e] == '}') break;\n\n    /* No '}' or nothing between {} ? Hash the whole key. */\n    if (e == keylen || e == s+1) return crc16(key,keylen) & 0x3FFF;\n\n    /* If we are here there is both a { and a } on its right. Hash\n     * what is in the middle between { and }. */\n    return crc16(key+s+1,e-s-1) & 0x3FFF;\n}\n\n/* Return a string representation of the cluster node. */\nstatic sds clusterManagerNodeInfo(clusterManagerNode *node, int indent) {\n    sds info = sdsempty();\n    sds spaces = sdsempty();\n    int i;\n    for (i = 0; i < indent; i++) spaces = sdscat(spaces, \" \");\n    if (indent) info = sdscat(info, spaces);\n    int is_master = !(node->flags & CLUSTER_MANAGER_FLAG_SLAVE);\n    char *role = (is_master ? \"M\" : \"S\");\n    sds slots = NULL;\n    if (node->dirty && node->replicate != NULL)\n        info = sdscatfmt(info, \"S: %S %s:%u\", node->name, node->ip, node->port);\n    else {\n        slots = clusterManagerNodeSlotsString(node);\n        sds flags = clusterManagerNodeFlagString(node);\n        info = sdscatfmt(info, \"%s: %S %s:%u\\n\"\n                               \"%s   slots:%S (%u slots) \"\n                               \"%S\",\n                               role, node->name, node->ip, node->port, spaces,\n                               slots, node->slots_count, flags);\n        sdsfree(slots);\n        sdsfree(flags);\n    }\n    if (node->replicate != NULL)\n        info = sdscatfmt(info, \"\\n%s   replicates %S\", spaces, node->replicate);\n    else if (node->replicas_count)\n        info = sdscatfmt(info, \"\\n%s   %U additional replica(s)\",\n                         spaces, node->replicas_count);\n    sdsfree(spaces);\n    return info;\n}\n\nvoid clusterManagerShowNodes(void) {\n    listIter li;\n    listNode *ln;\n    listRewind(cluster_manager.nodes, &li);\n    while ((ln = listNext(&li)) != NULL) {\n        clusterManagerNode *node = ln->value;\n        sds info = clusterManagerNodeInfo(node, 0);\n        printf(\"%s\\n\", (char *) info);\n        sdsfree(info);\n    }\n}\n\nstatic void clusterManagerShowClusterInfo(void) {\n    int masters = 0;\n    int keys = 0;\n    listIter li;\n    listNode *ln;\n    listRewind(cluster_manager.nodes, &li);\n    while ((ln = listNext(&li)) != NULL) {\n        clusterManagerNode *node = ln->value;\n        if (!(node->flags & CLUSTER_MANAGER_FLAG_SLAVE)) {\n            if (!node->name) continue;\n            int replicas = 0;\n            int dbsize = -1;\n            char name[9];\n            memcpy(name, node->name, 8);\n            name[8] = '\\0';\n            listIter ri;\n            listNode *rn;\n            listRewind(cluster_manager.nodes, &ri);\n            while ((rn = listNext(&ri)) != NULL) {\n                clusterManagerNode *n = rn->value;\n                if (n == node || !(n->flags & CLUSTER_MANAGER_FLAG_SLAVE))\n                    continue;\n                if (n->replicate && !strcmp(n->replicate, node->name))\n                    replicas++;\n            }\n            redisReply *reply = CLUSTER_MANAGER_COMMAND(node, \"DBSIZE\");\n            if (reply != NULL && reply->type == REDIS_REPLY_INTEGER)\n                dbsize = reply->integer;\n            if (dbsize < 0) {\n                char *err = \"\";\n                if (reply != NULL && reply->type == REDIS_REPLY_ERROR)\n                    err = reply->str;\n                CLUSTER_MANAGER_PRINT_REPLY_ERROR(node, err);\n                if (reply != NULL) freeReplyObject(reply);\n                return;\n            };\n            if (reply != NULL) freeReplyObject(reply);\n            printf(\"%s:%d (%s...) -> %d keys | %d slots | %d slaves.\\n\",\n                   node->ip, node->port, name, dbsize,\n                   node->slots_count, replicas);\n            masters++;\n            keys += dbsize;\n        }\n    }\n    clusterManagerLogOk(\"[OK] %d keys in %d masters.\\n\", keys, masters);\n    float keys_per_slot = keys / (float) CLUSTER_MANAGER_SLOTS;\n    printf(\"%.2f keys per slot on average.\\n\", keys_per_slot);\n}\n\n/* Flush dirty slots configuration of the node by calling CLUSTER ADDSLOTS */\nstatic int clusterManagerAddSlots(clusterManagerNode *node, char**err)\n{\n    redisReply *reply = NULL;\n    void *_reply = NULL;\n    int success = 1;\n    /* First two args are used for the command itself. */\n    int argc = node->slots_count + 2;\n    sds *argv = zmalloc(argc * sizeof(*argv), MALLOC_LOCAL);\n    size_t *argvlen = zmalloc(argc * sizeof(*argvlen), MALLOC_LOCAL);\n    argv[0] = \"CLUSTER\";\n    argv[1] = \"ADDSLOTS\";\n    argvlen[0] = 7;\n    argvlen[1] = 8;\n    *err = NULL;\n    int i, argv_idx = 2;\n    for (i = 0; i < CLUSTER_MANAGER_SLOTS; i++) {\n        if (argv_idx >= argc) break;\n        if (node->slots[i]) {\n            argv[argv_idx] = sdsfromlonglong((long long) i);\n            argvlen[argv_idx] = sdslen(argv[argv_idx]);\n            argv_idx++;\n        }\n    }\n    if (!argv_idx) {\n        success = 0;\n        goto cleanup;\n    }\n    redisAppendCommandArgv(node->context,argc,(const char**)argv,argvlen);\n    if (redisGetReply(node->context, &_reply) != REDIS_OK) {\n        success = 0;\n        goto cleanup;\n    }\n    reply = (redisReply*) _reply;\n    success = clusterManagerCheckRedisReply(node, reply, err);\ncleanup:\n    zfree(argvlen);\n    if (argv != NULL) {\n        for (i = 2; i < argc; i++) sdsfree(argv[i]);\n        zfree(argv);\n    }\n    if (reply != NULL) freeReplyObject(reply);\n    return success;\n}\n\n/* Get the node the slot is assigned to from the point of view of node *n.\n * If the slot is unassigned or if the reply is an error, return NULL.\n * Use the **err argument in order to check wether the slot is unassigned\n * or the reply resulted in an error. */\nstatic clusterManagerNode *clusterManagerGetSlotOwner(clusterManagerNode *n,\n                                                      int slot, char **err)\n{\n    assert(slot >= 0 && slot < CLUSTER_MANAGER_SLOTS);\n    clusterManagerNode *owner = NULL;\n    redisReply *reply = CLUSTER_MANAGER_COMMAND(n, \"CLUSTER SLOTS\");\n    if (clusterManagerCheckRedisReply(n, reply, err)) {\n        assert(reply->type == REDIS_REPLY_ARRAY);\n        size_t i;\n        for (i = 0; i < reply->elements; i++) {\n            redisReply *r = reply->element[i];\n            assert(r->type == REDIS_REPLY_ARRAY && r->elements >= 3);\n            int from, to;\n            from = r->element[0]->integer;\n            to = r->element[1]->integer;\n            if (slot < from || slot > to) continue;\n            redisReply *nr =  r->element[2];\n            assert(nr->type == REDIS_REPLY_ARRAY && nr->elements >= 2);\n            char *name = NULL;\n            if (nr->elements >= 3)\n                name =  nr->element[2]->str;\n            if (name != NULL)\n                owner = clusterManagerNodeByName(name);\n            else {\n                char *ip = nr->element[0]->str;\n                assert(ip != NULL);\n                int port = (int) nr->element[1]->integer;\n                listIter li;\n                listNode *ln;\n                listRewind(cluster_manager.nodes, &li);\n                while ((ln = listNext(&li)) != NULL) {\n                    clusterManagerNode *nd = ln->value;\n                    if (strcmp(nd->ip, ip) == 0 && port == nd->port) {\n                        owner = nd;\n                        break;\n                    }\n                }\n            }\n            if (owner) break;\n        }\n    }\n    if (reply) freeReplyObject(reply);\n    return owner;\n}\n\n/* Set slot status to \"importing\" or \"migrating\" */\nint clusterManagerSetSlot(clusterManagerNode *node1,\n                                 clusterManagerNode *node2,\n                                 int slot, const char *status, char **err) {\n    redisReply *reply = CLUSTER_MANAGER_COMMAND(node1, \"CLUSTER \"\n                                                \"SETSLOT %d %s %s\",\n                                                slot, status,\n                                                (char *) node2->name);\n    if (err != NULL) *err = NULL;\n    if (!reply) return 0;\n    int success = 1;\n    if (reply->type == REDIS_REPLY_ERROR) {\n        success = 0;\n        if (err != NULL) {\n            *err = zmalloc((reply->len + 1) * sizeof(char), MALLOC_LOCAL);\n            strcpy(*err, reply->str);\n        } else CLUSTER_MANAGER_PRINT_REPLY_ERROR(node1, reply->str);\n        goto cleanup;\n    }\ncleanup:\n    freeReplyObject(reply);\n    return success;\n}\n\nint clusterManagerClearSlotStatus(clusterManagerNode *node, int slot) {\n    redisReply *reply = CLUSTER_MANAGER_COMMAND(node,\n        \"CLUSTER SETSLOT %d %s\", slot, \"STABLE\");\n    int success = clusterManagerCheckRedisReply(node, reply, NULL);\n    if (reply) freeReplyObject(reply);\n    return success;\n}\n\nstatic int clusterManagerDelSlot(clusterManagerNode *node, int slot,\n                                 int ignore_unassigned_err)\n{\n    redisReply *reply = CLUSTER_MANAGER_COMMAND(node,\n        \"CLUSTER DELSLOTS %d\", slot);\n    char *err = NULL;\n    int success = clusterManagerCheckRedisReply(node, reply, &err);\n    if (!success && reply && reply->type == REDIS_REPLY_ERROR &&\n        ignore_unassigned_err)\n    {\n        char *get_owner_err = NULL;\n        clusterManagerNode *assigned_to =\n            clusterManagerGetSlotOwner(node, slot, &get_owner_err);\n        if (!assigned_to) {\n            if (get_owner_err == NULL) success = 1;\n            else {\n                CLUSTER_MANAGER_PRINT_REPLY_ERROR(node, get_owner_err);\n                zfree(get_owner_err);\n            }\n        }\n    }\n    if (!success && err != NULL) {\n        CLUSTER_MANAGER_PRINT_REPLY_ERROR(node, err);\n        zfree(err);\n    }\n    if (reply) freeReplyObject(reply);\n    return success;\n}\n\nstatic int clusterManagerAddSlot(clusterManagerNode *node, int slot) {\n    redisReply *reply = CLUSTER_MANAGER_COMMAND(node,\n        \"CLUSTER ADDSLOTS %d\", slot);\n    int success = clusterManagerCheckRedisReply(node, reply, NULL);\n    if (reply) freeReplyObject(reply);\n    return success;\n}\n\nsigned int clusterManagerCountKeysInSlot(clusterManagerNode *node,\n                                                int slot)\n{\n    redisReply *reply = CLUSTER_MANAGER_COMMAND(node,\n        \"CLUSTER COUNTKEYSINSLOT %d\", slot);\n    int count = -1;\n    int success = clusterManagerCheckRedisReply(node, reply, NULL);\n    if (success && reply->type == REDIS_REPLY_INTEGER) count = reply->integer;\n    if (reply) freeReplyObject(reply);\n    return count;\n}\n\nstatic int clusterManagerBumpEpoch(clusterManagerNode *node) {\n    redisReply *reply = CLUSTER_MANAGER_COMMAND(node, \"CLUSTER BUMPEPOCH\");\n    int success = clusterManagerCheckRedisReply(node, reply, NULL);\n    if (reply) freeReplyObject(reply);\n    return success;\n}\n\n/* Callback used by clusterManagerSetSlotOwner transaction. It should ignore\n * errors except for ADDSLOTS errors.\n * Return 1 if the error should be ignored. */\nstatic int clusterManagerOnSetOwnerErr(redisReply *reply,\n    clusterManagerNode *n, int bulk_idx)\n{\n    UNUSED(reply);\n    UNUSED(n);\n    /* Only raise error when ADDSLOTS fail (bulk_idx == 1). */\n    return (bulk_idx != 1);\n}\n\nint clusterManagerSetSlotOwner(clusterManagerNode *owner,\n                                      int slot,\n                                      int do_clear)\n{\n    int success = clusterManagerStartTransaction(owner);\n    if (!success) return 0;\n    /* Ensure the slot is not already assigned. */\n    clusterManagerDelSlot(owner, slot, 1);\n    /* Add the slot and bump epoch. */\n    clusterManagerAddSlot(owner, slot);\n    if (do_clear) clusterManagerClearSlotStatus(owner, slot);\n    clusterManagerBumpEpoch(owner);\n    success = clusterManagerExecTransaction(owner, clusterManagerOnSetOwnerErr);\n    return success;\n}\n\n/* Get the hash for the values of the specified keys in *keys_reply for the\n * specified nodes *n1 and *n2, by calling DEBUG DIGEST-VALUE redis command\n * on both nodes. Every key with same name on both nodes but having different\n * values will be added to the *diffs list. Return 0 in case of reply\n * error. */\nstatic int clusterManagerCompareKeysValues(clusterManagerNode *n1,\n                                          clusterManagerNode *n2,\n                                          redisReply *keys_reply,\n                                          list *diffs)\n{\n    size_t i, argc = keys_reply->elements + 2;\n    static const char *hash_zero = \"0000000000000000000000000000000000000000\";\n    char **argv = zcalloc(argc * sizeof(char *), MALLOC_LOCAL);\n    size_t  *argv_len = zcalloc(argc * sizeof(size_t), MALLOC_LOCAL);\n    argv[0] = \"DEBUG\";\n    argv_len[0] = 5;\n    argv[1] = \"DIGEST-VALUE\";\n    argv_len[1] = 12;\n    for (i = 0; i < keys_reply->elements; i++) {\n        redisReply *entry = keys_reply->element[i];\n        int idx = i + 2;\n        argv[idx] = entry->str;\n        argv_len[idx] = entry->len;\n    }\n    int success = 0;\n    void *_reply1 = NULL, *_reply2 = NULL;\n    redisReply *r1 = NULL, *r2 = NULL;\n    redisAppendCommandArgv(n1->context,argc, (const char**)argv,argv_len);\n    success = (redisGetReply(n1->context, &_reply1) == REDIS_OK);\n    if (!success) goto cleanup;\n    r1 = (redisReply *) _reply1;\n    redisAppendCommandArgv(n2->context,argc, (const char**)argv,argv_len);\n    success = (redisGetReply(n2->context, &_reply2) == REDIS_OK);\n    if (!success) goto cleanup;\n    r2 = (redisReply *) _reply2;\n    success = (r1->type != REDIS_REPLY_ERROR && r2->type != REDIS_REPLY_ERROR);\n    if (r1->type == REDIS_REPLY_ERROR) {\n        CLUSTER_MANAGER_PRINT_REPLY_ERROR(n1, r1->str);\n        success = 0;\n    }\n    if (r2->type == REDIS_REPLY_ERROR) {\n        CLUSTER_MANAGER_PRINT_REPLY_ERROR(n2, r2->str);\n        success = 0;\n    }\n    if (!success) goto cleanup;\n    assert(keys_reply->elements == r1->elements &&\n           keys_reply->elements == r2->elements);\n    for (i = 0; i < keys_reply->elements; i++) {\n        char *key = keys_reply->element[i]->str;\n        char *hash1 = r1->element[i]->str;\n        char *hash2 = r2->element[i]->str;\n        /* Ignore keys that don't exist in both nodes. */\n        if (strcmp(hash1, hash_zero) == 0 || strcmp(hash2, hash_zero) == 0)\n            continue;\n        if (strcmp(hash1, hash2) != 0) listAddNodeTail(diffs, key);\n    }\ncleanup:\n    if (r1) freeReplyObject(r1);\n    if (r2) freeReplyObject(r2);\n    zfree(argv);\n    zfree(argv_len);\n    return success;\n}\n\n/* Migrate keys taken from reply->elements. It returns the reply from the\n * MIGRATE command, or NULL if something goes wrong. If the argument 'dots'\n * is not NULL, a dot will be printed for every migrated key. */\nstatic redisReply *clusterManagerMigrateKeysInReply(clusterManagerNode *source,\n                                                    clusterManagerNode *target,\n                                                    redisReply *reply,\n                                                    int replace, int timeout,\n                                                    char *dots)\n{\n    redisReply *migrate_reply = NULL;\n    char **argv = NULL;\n    size_t *argv_len = NULL;\n    int c = (replace ? 8 : 7);\n    if (config.auth) c += 2;\n    if (config.user) c += 1;\n    size_t argc = c + reply->elements;\n    size_t i, offset = 6; // Keys Offset\n    argv = zcalloc(argc * sizeof(char *), MALLOC_LOCAL);\n    argv_len = zcalloc(argc * sizeof(size_t), MALLOC_LOCAL);\n    char portstr[255];\n    char timeoutstr[255];\n    snprintf(portstr, sizeof(portstr), \"%d\", target->port);\n    snprintf(timeoutstr, sizeof(timeoutstr), \"%d\", timeout);\n    argv[0] = \"MIGRATE\";\n    argv_len[0] = 7;\n    argv[1] = target->ip;\n    argv_len[1] = strlen(target->ip);\n    argv[2] = portstr;\n    argv_len[2] = strlen(portstr);\n    argv[3] = \"\";\n    argv_len[3] = 0;\n    argv[4] = \"0\";\n    argv_len[4] = 1;\n    argv[5] = timeoutstr;\n    argv_len[5] = strlen(timeoutstr);\n    if (replace) {\n        argv[offset] = \"REPLACE\";\n        argv_len[offset] = 7;\n        offset++;\n    }\n    if (config.auth) {\n        if (config.user) {\n            argv[offset] = \"AUTH2\";\n            argv_len[offset] = 5;\n            offset++;\n            argv[offset] = config.user;\n            argv_len[offset] = strlen(config.user);\n            offset++;\n            argv[offset] = config.auth;\n            argv_len[offset] = strlen(config.auth);\n            offset++;\n        } else {\n            argv[offset] = \"AUTH\";\n            argv_len[offset] = 4;\n            offset++;\n            argv[offset] = config.auth;\n            argv_len[offset] = strlen(config.auth);\n            offset++;\n        }\n    }\n    argv[offset] = \"KEYS\";\n    argv_len[offset] = 4;\n    offset++;\n    for (i = 0; i < reply->elements; i++) {\n        redisReply *entry = reply->element[i];\n        size_t idx = i + offset;\n        assert(entry->type == REDIS_REPLY_STRING);\n        argv[idx] = (char *) sdsnewlen(entry->str, entry->len);\n        argv_len[idx] = entry->len;\n        if (dots) dots[i] = '.';\n    }\n    if (dots) dots[reply->elements] = '\\0';\n    void *_reply = NULL;\n    redisAppendCommandArgv(source->context,argc,\n                           (const char**)argv,argv_len);\n    int success = (redisGetReply(source->context, &_reply) == REDIS_OK);\n    for (i = 0; i < reply->elements; i++) sdsfree(argv[i + offset]);\n    if (!success) goto cleanup;\n    migrate_reply = (redisReply *) _reply;\ncleanup:\n    zfree(argv);\n    zfree(argv_len);\n    return migrate_reply;\n}\n\n/* Migrate all keys in the given slot from source to target.*/\nstatic int clusterManagerMigrateKeysInSlot(clusterManagerNode *source,\n                                           clusterManagerNode *target,\n                                           int slot, int timeout,\n                                           int pipeline, int verbose,\n                                           char **err)\n{\n    int success = 1;\n    int do_fix = config.cluster_manager_command.flags &\n                 CLUSTER_MANAGER_CMD_FLAG_FIX;\n    int do_replace = config.cluster_manager_command.flags &\n                     CLUSTER_MANAGER_CMD_FLAG_REPLACE;\n    while (1) {\n        char *dots = NULL;\n        redisReply *reply = NULL, *migrate_reply = NULL;\n        reply = CLUSTER_MANAGER_COMMAND(source, \"CLUSTER \"\n                                        \"GETKEYSINSLOT %d %d\", slot,\n                                        pipeline);\n        success = (reply != NULL);\n        if (!success) return 0;\n        if (reply->type == REDIS_REPLY_ERROR) {\n            success = 0;\n            if (err != NULL) {\n                *err = zmalloc((reply->len + 1) * sizeof(char), MALLOC_LOCAL);\n                strcpy(*err, reply->str);\n                CLUSTER_MANAGER_PRINT_REPLY_ERROR(source, *err);\n            }\n            goto next;\n        }\n        assert(reply->type == REDIS_REPLY_ARRAY);\n        size_t count = reply->elements;\n        if (count == 0) {\n            freeReplyObject(reply);\n            break;\n        }\n        if (verbose) dots = zmalloc((count+1) * sizeof(char), MALLOC_LOCAL);\n        /* Calling MIGRATE command. */\n        migrate_reply = clusterManagerMigrateKeysInReply(source, target,\n                                                         reply, 0, timeout,\n                                                         dots);\n        if (migrate_reply == NULL) goto next;\n        if (migrate_reply->type == REDIS_REPLY_ERROR) {\n            int is_busy = strstr(migrate_reply->str, \"BUSYKEY\") != NULL;\n            int not_served = 0;\n            if (!is_busy) {\n                /* Check if the slot is unassigned (not served) in the\n                 * source node's configuration. */\n                char *get_owner_err = NULL;\n                clusterManagerNode *served_by =\n                    clusterManagerGetSlotOwner(source, slot, &get_owner_err);\n                if (!served_by) {\n                    if (get_owner_err == NULL) not_served = 1;\n                    else {\n                        CLUSTER_MANAGER_PRINT_REPLY_ERROR(source,\n                                                          get_owner_err);\n                        zfree(get_owner_err);\n                    }\n                }\n            }\n            /* Try to handle errors. */\n            if (is_busy || not_served) {\n                /* If the key's slot is not served, try to assign slot\n                 * to the target node. */\n                if (do_fix && not_served) {\n                    clusterManagerLogWarn(\"*** Slot was not served, setting \"\n                                          \"owner to node %s:%d.\\n\",\n                                          target->ip, target->port);\n                    clusterManagerSetSlot(source, target, slot, \"node\", NULL);\n                }\n                /* If the key already exists in the target node (BUSYKEY),\n                 * check whether its value is the same in both nodes.\n                 * In case of equal values, retry migration with the\n                 * REPLACE option.\n                 * In case of different values:\n                 *  - If the migration is requested by the fix command, stop\n                 *    and warn the user.\n                 *  - In other cases (ie. reshard), proceed only if the user\n                 *    launched the command with the --cluster-replace option.*/\n                if (is_busy) {\n                    clusterManagerLogWarn(\"\\n*** Target key exists\\n\");\n                    if (!do_replace) {\n                        clusterManagerLogWarn(\"*** Checking key values on \"\n                                              \"both nodes...\\n\");\n                        list *diffs = listCreate();\n                        success = clusterManagerCompareKeysValues(source,\n                            target, reply, diffs);\n                        if (!success) {\n                            clusterManagerLogErr(\"*** Value check failed!\\n\");\n                            listRelease(diffs);\n                            goto next;\n                        }\n                        if (listLength(diffs) > 0) {\n                            success = 0;\n                            clusterManagerLogErr(\n                                \"*** Found %d key(s) in both source node and \"\n                                \"target node having different values.\\n\"\n                                \"    Source node: %s:%d\\n\"\n                                \"    Target node: %s:%d\\n\"\n                                \"    Keys(s):\\n\",\n                                listLength(diffs),\n                                source->ip, source->port,\n                                target->ip, target->port);\n                            listIter dli;\n                            listNode *dln;\n                            listRewind(diffs, &dli);\n                            while((dln = listNext(&dli)) != NULL) {\n                                char *k = dln->value;\n                                clusterManagerLogErr(\"    - %s\\n\", k);\n                            }\n                            clusterManagerLogErr(\"Please fix the above key(s) \"\n                                                 \"manually and try again \"\n                                                 \"or relaunch the command \\n\"\n                                                 \"with --cluster-replace \"\n                                                 \"option to force key \"\n                                                 \"overriding.\\n\");\n                            listRelease(diffs);\n                            goto next;\n                        }\n                        listRelease(diffs);\n                    }\n                    clusterManagerLogWarn(\"*** Replacing target keys...\\n\");\n                }\n                freeReplyObject(migrate_reply);\n                migrate_reply = clusterManagerMigrateKeysInReply(source,\n                                                                 target,\n                                                                 reply,\n                                                                 is_busy,\n                                                                 timeout,\n                                                                 NULL);\n                success = (migrate_reply != NULL &&\n                           migrate_reply->type != REDIS_REPLY_ERROR);\n            } else success = 0;\n            if (!success) {\n                if (migrate_reply != NULL) {\n                    if (err) {\n                        *err = zmalloc((migrate_reply->len + 1) * sizeof(char), MALLOC_LOCAL);\n                        strcpy(*err, migrate_reply->str);\n                    }\n                    printf(\"\\n\");\n                    CLUSTER_MANAGER_PRINT_REPLY_ERROR(source,\n                                                      migrate_reply->str);\n                }\n                goto next;\n            }\n        }\n        if (verbose) {\n            printf(\"%s\", dots);\n            fflush(stdout);\n        }\nnext:\n        if (reply != NULL) freeReplyObject(reply);\n        if (migrate_reply != NULL) freeReplyObject(migrate_reply);\n        if (dots) zfree(dots);\n        if (!success) break;\n    }\n    return success;\n}\n\n/* Move slots between source and target nodes using MIGRATE.\n *\n * Options:\n * CLUSTER_MANAGER_OPT_VERBOSE -- Print a dot for every moved key.\n * CLUSTER_MANAGER_OPT_COLD    -- Move keys without opening slots /\n *                                reconfiguring the nodes.\n * CLUSTER_MANAGER_OPT_UPDATE  -- Update node->slots for source/target nodes.\n * CLUSTER_MANAGER_OPT_QUIET   -- Don't print info messages.\n*/\nint clusterManagerMoveSlot(clusterManagerNode *source,\n                                  clusterManagerNode *target,\n                                  int slot, int opts,  char**err)\n{\n    if (!(opts & CLUSTER_MANAGER_OPT_QUIET)) {\n        printf(\"Moving slot %d from %s:%d to %s:%d: \", slot, source->ip,\n               source->port, target->ip, target->port);\n        fflush(stdout);\n    }\n    if (err != NULL) *err = NULL;\n    int pipeline = config.cluster_manager_command.pipeline,\n        timeout = config.cluster_manager_command.timeout,\n        print_dots = (opts & CLUSTER_MANAGER_OPT_VERBOSE),\n        option_cold = (opts & CLUSTER_MANAGER_OPT_COLD),\n        success = 1;\n    if (!option_cold) {\n        success = clusterManagerSetSlot(target, source, slot,\n                                        \"importing\", err);\n        if (!success) return 0;\n        success = clusterManagerSetSlot(source, target, slot,\n                                        \"migrating\", err);\n        if (!success) return 0;\n    }\n    success = clusterManagerMigrateKeysInSlot(source, target, slot, timeout,\n                                              pipeline, print_dots, err);\n    if (!(opts & CLUSTER_MANAGER_OPT_QUIET)) printf(\"\\n\");\n    if (!success) return 0;\n    /* Set the new node as the owner of the slot in all the known nodes. */\n    if (!option_cold) {\n        listIter li;\n        listNode *ln;\n        listRewind(cluster_manager.nodes, &li);\n        while ((ln = listNext(&li)) != NULL) {\n            clusterManagerNode *n = ln->value;\n            if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE) continue;\n            redisReply *r = CLUSTER_MANAGER_COMMAND(n, \"CLUSTER \"\n                                                    \"SETSLOT %d %s %s\",\n                                                    slot, \"node\",\n                                                    target->name);\n            success = (r != NULL);\n            if (!success) return 0;\n            if (r->type == REDIS_REPLY_ERROR) {\n                success = 0;\n                if (err != NULL) {\n                    *err = zmalloc((r->len + 1) * sizeof(char), MALLOC_LOCAL);\n                    strcpy(*err, r->str);\n                    CLUSTER_MANAGER_PRINT_REPLY_ERROR(n, *err);\n                }\n            }\n            freeReplyObject(r);\n            if (!success) return 0;\n        }\n    }\n    /* Update the node logical config */\n    if (opts & CLUSTER_MANAGER_OPT_UPDATE) {\n        source->slots[slot] = 0;\n        target->slots[slot] = 1;\n    }\n    return 1;\n}\n\n/* Flush the dirty node configuration by calling replicate for slaves or\n * adding the slots defined in the masters. */\nstatic int clusterManagerFlushNodeConfig(clusterManagerNode *node, char **err) {\n    if (!node->dirty) return 0;\n    redisReply *reply = NULL;\n    int is_err = 0, success = 1;\n    if (err != NULL) *err = NULL;\n    if (node->replicate != NULL) {\n        reply = CLUSTER_MANAGER_COMMAND(node, \"CLUSTER REPLICATE %s\",\n                                        node->replicate);\n        if (reply == NULL || (is_err = (reply->type == REDIS_REPLY_ERROR))) {\n            if (is_err && err != NULL) {\n                *err = zmalloc((reply->len + 1) * sizeof(char), MALLOC_LOCAL);\n                strcpy(*err, reply->str);\n            }\n            success = 0;\n            /* If the cluster did not already joined it is possible that\n             * the slave does not know the master node yet. So on errors\n             * we return ASAP leaving the dirty flag set, to flush the\n             * config later. */\n            goto cleanup;\n        }\n    } else {\n        int added = clusterManagerAddSlots(node, err);\n        if (!added || *err != NULL) success = 0;\n    }\n    node->dirty = 0;\ncleanup:\n    if (reply != NULL) freeReplyObject(reply);\n    return success;\n}\n\n/* Load node's cluster configuration by calling \"CLUSTER NODES\" command.\n * Node's configuration (name, replicate, slots, ...) is then updated.\n * If CLUSTER_MANAGER_OPT_GETFRIENDS flag is set into 'opts' argument,\n * and node already knows other nodes, the node's friends list is populated\n * with the other nodes info. */\nstatic int clusterManagerNodeLoadInfo(clusterManagerNode *node, int opts,\n                                      char **err)\n{\n    redisReply *reply = CLUSTER_MANAGER_COMMAND(node, \"CLUSTER NODES\");\n    int success = 1;\n    *err = NULL;\n    if (!clusterManagerCheckRedisReply(node, reply, err)) {\n        success = 0;\n        goto cleanup;\n    }\n    int getfriends = (opts & CLUSTER_MANAGER_OPT_GETFRIENDS);\n    char *lines = reply->str, *p, *line;\n    while ((p = strstr(lines, \"\\n\")) != NULL) {\n        *p = '\\0';\n        line = lines;\n        lines = p + 1;\n        char *name = NULL, *addr = NULL, *flags = NULL, *master_id = NULL,\n             *ping_sent = NULL, *ping_recv = NULL, *config_epoch = NULL,\n             *link_status = NULL;\n        UNUSED(link_status);\n        int i = 0;\n        while ((p = strchr(line, ' ')) != NULL) {\n            *p = '\\0';\n            char *token = line;\n            line = p + 1;\n            switch(i++){\n            case 0: name = token; break;\n            case 1: addr = token; break;\n            case 2: flags = token; break;\n            case 3: master_id = token; break;\n            case 4: ping_sent = token; break;\n            case 5: ping_recv = token; break;\n            case 6: config_epoch = token; break;\n            case 7: link_status = token; break;\n            }\n            if (i == 8) break; // Slots\n        }\n        if (!flags) {\n            success = 0;\n            goto cleanup;\n        }\n        int myself = (strstr(flags, \"myself\") != NULL);\n        clusterManagerNode *currentNode = NULL;\n        if (myself) {\n            node->flags |= CLUSTER_MANAGER_FLAG_MYSELF;\n            currentNode = node;\n            clusterManagerNodeResetSlots(node);\n            if (i == 8) {\n                int remaining = strlen(line);\n                while (remaining > 0) {\n                    p = strchr(line, ' ');\n                    if (p == NULL) p = line + remaining;\n                    remaining -= (p - line);\n\n                    char *slotsdef = line;\n                    *p = '\\0';\n                    if (remaining) {\n                        line = p + 1;\n                        remaining--;\n                    } else line = p;\n                    char *dash = NULL;\n                    if (slotsdef[0] == '[') {\n                        slotsdef++;\n                        if ((p = strstr(slotsdef, \"->-\"))) { // Migrating\n                            *p = '\\0';\n                            p += 3;\n                            char *closing_bracket = strchr(p, ']');\n                            if (closing_bracket) *closing_bracket = '\\0';\n                            sds slot = sdsnew(slotsdef);\n                            sds dst = sdsnew(p);\n                            node->migrating_count += 2;\n                            node->migrating = zrealloc(node->migrating,\n                                (node->migrating_count * sizeof(sds)), MALLOC_LOCAL);\n                            node->migrating[node->migrating_count - 2] =\n                                slot;\n                            node->migrating[node->migrating_count - 1] =\n                                dst;\n                        }  else if ((p = strstr(slotsdef, \"-<-\"))) {//Importing\n                            *p = '\\0';\n                            p += 3;\n                            char *closing_bracket = strchr(p, ']');\n                            if (closing_bracket) *closing_bracket = '\\0';\n                            sds slot = sdsnew(slotsdef);\n                            sds src = sdsnew(p);\n                            node->importing_count += 2;\n                            node->importing = zrealloc(node->importing,\n                                (node->importing_count * sizeof(sds)), MALLOC_LOCAL);\n                            node->importing[node->importing_count - 2] =\n                                slot;\n                            node->importing[node->importing_count - 1] =\n                                src;\n                        }\n                    } else if ((dash = strchr(slotsdef, '-')) != NULL) {\n                        p = dash;\n                        int start, stop;\n                        *p = '\\0';\n                        start = atoi(slotsdef);\n                        stop = atoi(p + 1);\n                        node->slots_count += (stop - (start - 1));\n                        while (start <= stop) node->slots[start++] = 1;\n                    } else if (p > slotsdef) {\n                        node->slots[atoi(slotsdef)] = 1;\n                        node->slots_count++;\n                    }\n                }\n            }\n            node->dirty = 0;\n        } else if (!getfriends) {\n            if (!(node->flags & CLUSTER_MANAGER_FLAG_MYSELF)) continue;\n            else break;\n        } else {\n            if (addr == NULL) {\n                fprintf(stderr, \"Error: invalid CLUSTER NODES reply\\n\");\n                success = 0;\n                goto cleanup;\n            }\n            char *c = strrchr(addr, '@');\n            if (c != NULL) *c = '\\0';\n            c = strrchr(addr, ':');\n            if (c == NULL) {\n                fprintf(stderr, \"Error: invalid CLUSTER NODES reply\\n\");\n                success = 0;\n                goto cleanup;\n            }\n            *c = '\\0';\n            int port = atoi(++c);\n            currentNode = clusterManagerNewNode(sdsnew(addr), port);\n            currentNode->flags |= CLUSTER_MANAGER_FLAG_FRIEND;\n            if (node->friends == NULL) node->friends = listCreate();\n            listAddNodeTail(node->friends, currentNode);\n        }\n        if (name != NULL) {\n            if (currentNode->name) sdsfree(currentNode->name);\n            currentNode->name = sdsnew(name);\n        }\n        if (currentNode->flags_str != NULL)\n            freeClusterManagerNodeFlags(currentNode->flags_str);\n        currentNode->flags_str = listCreate();\n        int flag_len;\n        while ((flag_len = strlen(flags)) > 0) {\n            sds flag = NULL;\n            char *fp = strchr(flags, ',');\n            if (fp) {\n                *fp = '\\0';\n                flag = sdsnew(flags);\n                flags = fp + 1;\n            } else {\n                flag = sdsnew(flags);\n                flags += flag_len;\n            }\n            if (strcmp(flag, \"noaddr\") == 0)\n                currentNode->flags |= CLUSTER_MANAGER_FLAG_NOADDR;\n            else if (strcmp(flag, \"disconnected\") == 0)\n                currentNode->flags |= CLUSTER_MANAGER_FLAG_DISCONNECT;\n            else if (strcmp(flag, \"fail\") == 0)\n                currentNode->flags |= CLUSTER_MANAGER_FLAG_FAIL;\n            else if (strcmp(flag, \"slave\") == 0) {\n                currentNode->flags |= CLUSTER_MANAGER_FLAG_SLAVE;\n                if (master_id != NULL) {\n                    if (currentNode->replicate) sdsfree(currentNode->replicate);\n                    currentNode->replicate = sdsnew(master_id);\n                }\n            }\n            listAddNodeTail(currentNode->flags_str, flag);\n        }\n        if (config_epoch != NULL)\n            currentNode->current_epoch = atoll(config_epoch);\n        if (ping_sent != NULL) currentNode->ping_sent = atoll(ping_sent);\n        if (ping_recv != NULL) currentNode->ping_recv = atoll(ping_recv);\n        if (!getfriends && myself) break;\n    }\ncleanup:\n    if (reply) freeReplyObject(reply);\n    return success;\n}\n\n/* Retrieves info about the cluster using argument 'node' as the starting\n * point. All nodes will be loaded inside the cluster_manager.nodes list.\n * Warning: if something goes wrong, it will free the starting node before\n * returning 0. */\nstatic int clusterManagerLoadInfoFromNode(clusterManagerNode *node, int opts) {\n    if (node->context == NULL && !clusterManagerNodeConnect(node)) {\n        freeClusterManagerNode(node);\n        return 0;\n    }\n    opts |= CLUSTER_MANAGER_OPT_GETFRIENDS;\n    char *e = NULL;\n    if (!clusterManagerNodeIsCluster(node, &e)) {\n        clusterManagerPrintNotClusterNodeError(node, e);\n        if (e) zfree(e);\n        freeClusterManagerNode(node);\n        return 0;\n    }\n    e = NULL;\n    if (!clusterManagerNodeLoadInfo(node, opts, &e)) {\n        if (e) {\n            CLUSTER_MANAGER_PRINT_REPLY_ERROR(node, e);\n            zfree(e);\n        }\n        freeClusterManagerNode(node);\n        return 0;\n    }\n    listIter li;\n    listNode *ln;\n    if (cluster_manager.nodes != NULL) {\n        listRewind(cluster_manager.nodes, &li);\n        while ((ln = listNext(&li)) != NULL)\n            freeClusterManagerNode((clusterManagerNode *) ln->value);\n        listRelease(cluster_manager.nodes);\n    }\n    cluster_manager.nodes = listCreate();\n    listAddNodeTail(cluster_manager.nodes, node);\n    if (node->friends != NULL) {\n        listRewind(node->friends, &li);\n        while ((ln = listNext(&li)) != NULL) {\n            clusterManagerNode *friend = ln->value;\n            if (!friend->ip || !friend->port) goto invalid_friend;\n            if (!friend->context && !clusterManagerNodeConnect(friend))\n                goto invalid_friend;\n            e = NULL;\n            if (clusterManagerNodeLoadInfo(friend, 0, &e)) {\n                if (friend->flags & (CLUSTER_MANAGER_FLAG_NOADDR |\n                                     CLUSTER_MANAGER_FLAG_DISCONNECT |\n                                     CLUSTER_MANAGER_FLAG_FAIL))\n                {\n                    goto invalid_friend;\n                }\n                listAddNodeTail(cluster_manager.nodes, friend);\n            } else {\n                clusterManagerLogErr(\"[ERR] Unable to load info for \"\n                                     \"node %s:%d\\n\",\n                                     friend->ip, friend->port);\n                goto invalid_friend;\n            }\n            continue;\ninvalid_friend:\n            if (!(friend->flags & CLUSTER_MANAGER_FLAG_SLAVE))\n                cluster_manager.unreachable_masters++;\n            freeClusterManagerNode(friend);\n        }\n        listRelease(node->friends);\n        node->friends = NULL;\n    }\n    // Count replicas for each node\n    listRewind(cluster_manager.nodes, &li);\n    while ((ln = listNext(&li)) != NULL) {\n        clusterManagerNode *n = ln->value;\n        if (n->replicate != NULL) {\n            clusterManagerNode *master = clusterManagerNodeByName(n->replicate);\n            if (master == NULL) {\n                clusterManagerLogWarn(\"*** WARNING: %s:%d claims to be \"\n                                      \"slave of unknown node ID %s.\\n\",\n                                      n->ip, n->port, n->replicate);\n            } else master->replicas_count++;\n        }\n    }\n    return 1;\n}\n\n/* Compare functions used by various sorting operations. */\nint clusterManagerSlotCompare(const void *slot1, const void *slot2) {\n    const char **i1 = (const char **)slot1;\n    const char **i2 = (const char **)slot2;\n    return strcmp(*i1, *i2);\n}\n\nint clusterManagerSlotCountCompareDesc(const void *n1, const void *n2) {\n    clusterManagerNode *node1 = *((clusterManagerNode **) n1);\n    clusterManagerNode *node2 = *((clusterManagerNode **) n2);\n    return node2->slots_count - node1->slots_count;\n}\n\nint clusterManagerCompareNodeBalance(const void *n1, const void *n2) {\n    clusterManagerNode *node1 = *((clusterManagerNode **) n1);\n    clusterManagerNode *node2 = *((clusterManagerNode **) n2);\n    return node1->balance - node2->balance;\n}\n\nstatic sds clusterManagerGetConfigSignature(clusterManagerNode *node) {\n    sds signature = NULL;\n    int node_count = 0, i = 0, name_len = 0;\n    char **node_configs = NULL;\n    redisReply *reply = CLUSTER_MANAGER_COMMAND(node, \"CLUSTER NODES\");\n    if (reply == NULL || reply->type == REDIS_REPLY_ERROR)\n        goto cleanup;\n    char *lines = reply->str, *p, *line;\n    while ((p = strstr(lines, \"\\n\")) != NULL) {\n        i = 0;\n        *p = '\\0';\n        line = lines;\n        lines = p + 1;\n        char *nodename = NULL;\n        int tot_size = 0;\n        while ((p = strchr(line, ' ')) != NULL) {\n            *p = '\\0';\n            char *token = line;\n            line = p + 1;\n            if (i == 0) {\n                nodename = token;\n                tot_size = (p - token);\n                name_len = tot_size++; // Make room for ':' in tot_size\n            }\n            if (++i == 8) break;\n        }\n        if (i != 8) continue;\n        if (nodename == NULL) continue;\n        int remaining = strlen(line);\n        if (remaining == 0) continue;\n        char **slots = NULL;\n        int c = 0;\n        while (remaining > 0) {\n            p = strchr(line, ' ');\n            if (p == NULL) p = line + remaining;\n            int size = (p - line);\n            remaining -= size;\n            tot_size += size;\n            char *slotsdef = line;\n            *p = '\\0';\n            if (remaining) {\n                line = p + 1;\n                remaining--;\n            } else line = p;\n            if (slotsdef[0] != '[') {\n                c++;\n                slots = zrealloc(slots, (c * sizeof(char *)), MALLOC_LOCAL);\n                slots[c - 1] = slotsdef;\n            }\n        }\n        if (c > 0) {\n            if (c > 1)\n                qsort(slots, c, sizeof(char *), clusterManagerSlotCompare);\n            node_count++;\n            node_configs =\n                zrealloc(node_configs, (node_count * sizeof(char *)), MALLOC_LOCAL);\n            /* Make room for '|' separators. */\n            tot_size += (sizeof(char) * (c - 1));\n            char *cfg = zmalloc((sizeof(char) * tot_size) + 1, MALLOC_LOCAL);\n            memcpy(cfg, nodename, name_len);\n            char *sp = cfg + name_len;\n            *(sp++) = ':';\n            for (i = 0; i < c; i++) {\n                if (i > 0) *(sp++) = ',';\n                int slen = strlen(slots[i]);\n                memcpy(sp, slots[i], slen);\n                sp += slen;\n            }\n            *(sp++) = '\\0';\n            node_configs[node_count - 1] = cfg;\n        }\n        zfree(slots);\n    }\n    if (node_count > 0) {\n        if (node_count > 1) {\n            qsort(node_configs, node_count, sizeof(char *),\n                  clusterManagerSlotCompare);\n        }\n        signature = sdsempty();\n        for (i = 0; i < node_count; i++) {\n            if (i > 0) signature = sdscatprintf(signature, \"%c\", '|');\n            signature = sdscatfmt(signature, \"%s\", node_configs[i]);\n        }\n    }\ncleanup:\n    if (reply != NULL) freeReplyObject(reply);\n    if (node_configs != NULL) {\n        for (i = 0; i < node_count; i++) zfree(node_configs[i]);\n        zfree(node_configs);\n    }\n    return signature;\n}\n\nint clusterManagerIsConfigConsistent(int fLog) {\n    if (cluster_manager.nodes == NULL) return 0;\n    int consistent = (listLength(cluster_manager.nodes) <= 1);\n    // If the Cluster has only one node, it's always consistent\n    if (consistent) return 1;\n    sds first_cfg = NULL;\n    const char *firstNode = NULL;\n    listIter li;\n    listNode *ln;\n    listRewind(cluster_manager.nodes, &li);\n    while ((ln = listNext(&li)) != NULL) {\n        clusterManagerNode *node = ln->value;\n        sds cfg = clusterManagerGetConfigSignature(node);\n        if (cfg == NULL) {\n            consistent = 0;\n            break;\n        }\n        if (first_cfg == NULL) {\n            first_cfg = cfg;\n            firstNode = node->name;\n        } else {\n            consistent = !sdscmp(first_cfg, cfg);\n            sdsfree(cfg);\n            if (fLog && !consistent)\n                clusterManagerLogInfo(\"\\tNode %s (%s:%d) is inconsistent with %s\\n\", node->name, node->ip, node->port, firstNode);\n            if (!consistent) break;\n        }\n    }\n    if (first_cfg != NULL) sdsfree(first_cfg);\n    return consistent;\n}\n\n/* Add the error string to cluster_manager.errors and print it. */\nvoid clusterManagerOnError(sds err) {\n    if (cluster_manager.errors == NULL)\n        cluster_manager.errors = listCreate();\n    listAddNodeTail(cluster_manager.errors, err);\n    clusterManagerLogErr(\"%s\\n\", (char *) err);\n}\n\n/* Check the slots coverage of the cluster. The 'all_slots' argument must be\n * and array of 16384 bytes. Every covered slot will be set to 1 in the\n * 'all_slots' array. The function returns the total number if covered slots.*/\nint clusterManagerGetCoveredSlots(char *all_slots) {\n    if (cluster_manager.nodes == NULL) return 0;\n    listIter li;\n    listNode *ln;\n    listRewind(cluster_manager.nodes, &li);\n    int totslots = 0, i;\n    while ((ln = listNext(&li)) != NULL) {\n        clusterManagerNode *node = ln->value;\n        for (i = 0; i < CLUSTER_MANAGER_SLOTS; i++) {\n            if (node->slots[i] && !all_slots[i]) {\n                all_slots[i] = 1;\n                totslots++;\n            }\n        }\n    }\n    return totslots;\n}\n\nvoid clusterManagerPrintSlotsList(list *slots) {\n    clusterManagerNode n = {0};\n    listIter li;\n    listNode *ln;\n    listRewind(slots, &li);\n    while ((ln = listNext(&li)) != NULL) {\n        int slot = atoi(ln->value);\n        if (slot >= 0 && slot < CLUSTER_MANAGER_SLOTS)\n            n.slots[slot] = 1;\n    }\n    sds nodeslist = clusterManagerNodeSlotsString(&n);\n    printf(\"%s\\n\", nodeslist);\n    sdsfree(nodeslist);\n}\n\n/* Return the node, among 'nodes' with the greatest number of keys\n * in the specified slot. */\nclusterManagerNode * clusterManagerGetNodeWithMostKeysInSlot(list *nodes,\n                                                                    int slot,\n                                                                    char **err)\n{\n    clusterManagerNode *node = NULL;\n    int numkeys = 0;\n    listIter li;\n    listNode *ln;\n    listRewind(nodes, &li);\n    if (err) *err = NULL;\n    while ((ln = listNext(&li)) != NULL) {\n        clusterManagerNode *n = ln->value;\n        if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE || n->replicate)\n            continue;\n        redisReply *r =\n            CLUSTER_MANAGER_COMMAND(n, \"CLUSTER COUNTKEYSINSLOT %d\", slot);\n        int success = clusterManagerCheckRedisReply(n, r, err);\n        if (success) {\n            if (r->integer > numkeys || node == NULL) {\n                numkeys = r->integer;\n                node = n;\n            }\n        }\n        if (r != NULL) freeReplyObject(r);\n        /* If the reply contains errors */\n        if (!success) {\n            if (err != NULL && *err != NULL)\n                CLUSTER_MANAGER_PRINT_REPLY_ERROR(n, err);\n            node = NULL;\n            break;\n        }\n    }\n    return node;\n}\n\n/* This function returns the master that has the least number of replicas\n * in the cluster. If there are multiple masters with the same smaller\n * number of replicas, one at random is returned. */\n\nstatic clusterManagerNode *clusterManagerNodeWithLeastReplicas() {\n    clusterManagerNode *node = NULL;\n    int lowest_count = 0;\n    listIter li;\n    listNode *ln;\n    listRewind(cluster_manager.nodes, &li);\n    while ((ln = listNext(&li)) != NULL) {\n        clusterManagerNode *n = ln->value;\n        if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE) continue;\n        if (node == NULL || n->replicas_count < lowest_count) {\n            node = n;\n            lowest_count = n->replicas_count;\n        }\n    }\n    return node;\n}\n\n/* Slot 'slot' was found to be in importing or migrating state in one or\n * more nodes. This function fixes this condition by migrating keys where\n * it seems more sensible. */\nint clusterManagerFixOpenSlot(int slot) {\n    int force_fix = config.cluster_manager_command.flags &\n                    CLUSTER_MANAGER_CMD_FLAG_FIX_WITH_UNREACHABLE_MASTERS;\n\n    if (cluster_manager.unreachable_masters > 0 && !force_fix) {\n        clusterManagerLogWarn(\"*** Fixing open slots with %d unreachable masters is dangerous: keydb-cli will assume that slots about masters that are not reachable are not covered, and will try to reassign them to the reachable nodes. This can cause data loss and is rarely what you want to do. If you really want to proceed use the --cluster-fix-with-unreachable-masters option.\\n\", cluster_manager.unreachable_masters);\n        exit(1);\n    }\n\n    clusterManagerLogInfo(\">>> Fixing open slot %d\\n\", slot);\n    /* Try to obtain the current slot owner, according to the current\n     * nodes configuration. */\n    int success = 1;\n    list *owners = listCreate();    /* List of nodes claiming some ownership.\n                                       it could be stating in the configuration\n                                       to have the node ownership, or just\n                                       holding keys for such slot. */\n    list *migrating = listCreate();\n    list *importing = listCreate();\n    sds migrating_str = sdsempty();\n    sds importing_str = sdsempty();\n    clusterManagerNode *owner = NULL; /* The obvious slot owner if any. */\n\n    /* Iterate all the nodes, looking for potential owners of this slot. */\n    listIter li;\n    listNode *ln;\n    listRewind(cluster_manager.nodes, &li);\n    while ((ln = listNext(&li)) != NULL) {\n        clusterManagerNode *n = ln->value;\n        if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE) continue;\n        if (n->slots[slot]) {\n            listAddNodeTail(owners, n);\n        } else {\n            redisReply *r = CLUSTER_MANAGER_COMMAND(n,\n                \"CLUSTER COUNTKEYSINSLOT %d\", slot);\n            success = clusterManagerCheckRedisReply(n, r, NULL);\n            if (success && r->integer > 0) {\n                clusterManagerLogWarn(\"*** Found keys about slot %d \"\n                                      \"in non-owner node %s:%d!\\n\", slot,\n                                      n->ip, n->port);\n                listAddNodeTail(owners, n);\n            }\n            if (r) freeReplyObject(r);\n            if (!success) goto cleanup;\n        }\n    }\n\n    /* If we have only a single potential owner for this slot,\n     * set it as \"owner\". */\n    if (listLength(owners) == 1) owner = listFirst(owners)->value;\n\n    /* Scan the list of nodes again, in order to populate the\n     * list of nodes in importing or migrating state for\n     * this slot. */\n    listRewind(cluster_manager.nodes, &li);\n    while ((ln = listNext(&li)) != NULL) {\n        clusterManagerNode *n = ln->value;\n        if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE) continue;\n        int is_migrating = 0, is_importing = 0;\n        if (n->migrating) {\n            for (int i = 0; i < n->migrating_count; i += 2) {\n                sds migrating_slot = n->migrating[i];\n                if (atoi(migrating_slot) == slot) {\n                    char *sep = (listLength(migrating) == 0 ? \"\" : \",\");\n                    migrating_str = sdscatfmt(migrating_str, \"%s%s:%u\",\n                                              sep, n->ip, n->port);\n                    listAddNodeTail(migrating, n);\n                    is_migrating = 1;\n                    break;\n                }\n            }\n        }\n        if (!is_migrating && n->importing) {\n            for (int i = 0; i < n->importing_count; i += 2) {\n                sds importing_slot = n->importing[i];\n                if (atoi(importing_slot) == slot) {\n                    char *sep = (listLength(importing) == 0 ? \"\" : \",\");\n                    importing_str = sdscatfmt(importing_str, \"%s%s:%u\",\n                                              sep, n->ip, n->port);\n                    listAddNodeTail(importing, n);\n                    is_importing = 1;\n                    break;\n                }\n            }\n        }\n\n        /* If the node is neither migrating nor importing and it's not\n         * the owner, then is added to the importing list in case\n         * it has keys in the slot. */\n        if (!is_migrating && !is_importing && n != owner) {\n            redisReply *r = CLUSTER_MANAGER_COMMAND(n,\n                \"CLUSTER COUNTKEYSINSLOT %d\", slot);\n            success = clusterManagerCheckRedisReply(n, r, NULL);\n            if (success && r->integer > 0) {\n                clusterManagerLogWarn(\"*** Found keys about slot %d \"\n                                      \"in node %s:%d!\\n\", slot, n->ip,\n                                      n->port);\n                char *sep = (listLength(importing) == 0 ? \"\" : \",\");\n                importing_str = sdscatfmt(importing_str, \"%s%S:%u\",\n                                          sep, n->ip, n->port);\n                listAddNodeTail(importing, n);\n            }\n            if (r) freeReplyObject(r);\n            if (!success) goto cleanup;\n        }\n    }\n    if (sdslen(migrating_str) > 0)\n        printf(\"Set as migrating in: %s\\n\", migrating_str);\n    if (sdslen(importing_str) > 0)\n        printf(\"Set as importing in: %s\\n\", importing_str);\n\n    /* If there is no slot owner, set as owner the node with the biggest\n     * number of keys, among the set of migrating / importing nodes. */\n    if (owner == NULL) {\n        clusterManagerLogInfo(\">>> No single clear owner for the slot, \"\n                              \"selecting an owner by # of keys...\\n\");\n        owner = clusterManagerGetNodeWithMostKeysInSlot(cluster_manager.nodes,\n                                                        slot, NULL);\n        // If we still don't have an owner, we can't fix it.\n        if (owner == NULL) {\n            clusterManagerLogErr(\"[ERR] Can't select a slot owner. \"\n                                 \"Impossible to fix.\\n\");\n            success = 0;\n            goto cleanup;\n        }\n\n        // Use ADDSLOTS to assign the slot.\n        clusterManagerLogWarn(\"*** Configuring %s:%d as the slot owner\\n\",\n                              owner->ip, owner->port);\n        success = clusterManagerClearSlotStatus(owner, slot);\n        if (!success) goto cleanup;\n        success = clusterManagerSetSlotOwner(owner, slot, 0);\n        if (!success) goto cleanup;\n        /* Since CLUSTER ADDSLOTS succeeded, we also update the slot\n         * info into the node struct, in order to keep it synced */\n        owner->slots[slot] = 1;\n        /* Make sure this information will propagate. Not strictly needed\n         * since there is no past owner, so all the other nodes will accept\n         * whatever epoch this node will claim the slot with. */\n        success = clusterManagerBumpEpoch(owner);\n        if (!success) goto cleanup;\n        /* Remove the owner from the list of migrating/importing\n         * nodes. */\n        clusterManagerRemoveNodeFromList(migrating, owner);\n        clusterManagerRemoveNodeFromList(importing, owner);\n    }\n\n    /* If there are multiple owners of the slot, we need to fix it\n     * so that a single node is the owner and all the other nodes\n     * are in importing state. Later the fix can be handled by one\n     * of the base cases above.\n     *\n     * Note that this case also covers multiple nodes having the slot\n     * in migrating state, since migrating is a valid state only for\n     * slot owners. */\n    if (listLength(owners) > 1) {\n        /* Owner cannot be NULL at this point, since if there are more owners,\n         * the owner has been set in the previous condition (owner == NULL). */\n        assert(owner != NULL);\n        listRewind(owners, &li);\n        while ((ln = listNext(&li)) != NULL) {\n            clusterManagerNode *n = ln->value;\n            if (n == owner) continue;\n            success = clusterManagerDelSlot(n, slot, 1);\n            if (!success) goto cleanup;\n            n->slots[slot] = 0;\n            /* Assign the slot to the owner in the node 'n' configuration.' */\n            success = clusterManagerSetSlot(n, owner, slot, \"node\", NULL);\n            if (!success) goto cleanup;\n            success = clusterManagerSetSlot(n, owner, slot, \"importing\", NULL);\n            if (!success) goto cleanup;\n            /* Avoid duplicates. */\n            clusterManagerRemoveNodeFromList(importing, n);\n            listAddNodeTail(importing, n);\n            /* Ensure that the node is not in the migrating list. */\n            clusterManagerRemoveNodeFromList(migrating, n);\n        }\n    }\n    int move_opts = CLUSTER_MANAGER_OPT_VERBOSE;\n\n    /* Case 1: The slot is in migrating state in one node, and in\n     *         importing state in 1 node. That's trivial to address. */\n    if (listLength(migrating) == 1 && listLength(importing) == 1) {\n        clusterManagerNode *src = listFirst(migrating)->value;\n        clusterManagerNode *dst = listFirst(importing)->value;\n        clusterManagerLogInfo(\">>> Case 1: Moving slot %d from \"\n                              \"%s:%d to %s:%d\\n\", slot,\n                              src->ip, src->port, dst->ip, dst->port);\n        move_opts |= CLUSTER_MANAGER_OPT_UPDATE;\n        success = clusterManagerMoveSlot(src, dst, slot, move_opts, NULL);\n    }\n\n    /* Case 2: There are multiple nodes that claim the slot as importing,\n     * they probably got keys about the slot after a restart so opened\n     * the slot. In this case we just move all the keys to the owner\n     * according to the configuration. */\n    else if (listLength(migrating) == 0 && listLength(importing) > 0) {\n        clusterManagerLogInfo(\">>> Case 2: Moving all the %d slot keys to its \"\n                              \"owner %s:%d\\n\", slot, owner->ip, owner->port);\n        move_opts |= CLUSTER_MANAGER_OPT_COLD;\n        listRewind(importing, &li);\n        while ((ln = listNext(&li)) != NULL) {\n            clusterManagerNode *n = ln->value;\n            if (n == owner) continue;\n            success = clusterManagerMoveSlot(n, owner, slot, move_opts, NULL);\n            if (!success) goto cleanup;\n            clusterManagerLogInfo(\">>> Setting %d as STABLE in \"\n                                  \"%s:%d\\n\", slot, n->ip, n->port);\n            success = clusterManagerClearSlotStatus(n, slot);\n            if (!success) goto cleanup;\n        }\n        /* Since the slot has been moved in \"cold\" mode, ensure that all the\n         * other nodes update their own configuration about the slot itself. */\n        listRewind(cluster_manager.nodes, &li);\n        while ((ln = listNext(&li)) != NULL) {\n            clusterManagerNode *n = ln->value;\n            if (n == owner) continue;\n            if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE) continue;\n            success = clusterManagerSetSlot(n, owner, slot, \"NODE\", NULL);\n            if (!success) goto cleanup;\n        }\n    }\n\n    /* Case 3: The slot is in migrating state in one node but multiple\n     * other nodes claim to be in importing state and don't have any key in\n     * the slot. We search for the importing node having the same ID as\n     * the destination node of the migrating node.\n     * In that case we move the slot from the migrating node to this node and\n     * we close the importing states on all the other importing nodes.\n     * If no importing node has the same ID as the destination node of the\n     * migrating node, the slot's state is closed on both the migrating node\n     * and the importing nodes. */\n    else if (listLength(migrating) == 1 && listLength(importing) > 1) {\n        int try_to_fix = 1;\n        clusterManagerNode *src = listFirst(migrating)->value;\n        clusterManagerNode *dst = NULL;\n        sds target_id = NULL;\n        for (int i = 0; i < src->migrating_count; i += 2) {\n            sds migrating_slot = src->migrating[i];\n            if (atoi(migrating_slot) == slot) {\n                target_id = src->migrating[i + 1];\n                break;\n            }\n        }\n        assert(target_id != NULL);\n        listIter li;\n        listNode *ln;\n        listRewind(importing, &li);\n        while ((ln = listNext(&li)) != NULL) {\n            clusterManagerNode *n = ln->value;\n            int count = clusterManagerCountKeysInSlot(n, slot);\n            if (count > 0) {\n                try_to_fix = 0;\n                break;\n            }\n            if (strcmp(n->name, target_id) == 0) dst = n;\n        }\n        if (!try_to_fix) goto unhandled_case;\n        if (dst != NULL) {\n            clusterManagerLogInfo(\">>> Case 3: Moving slot %d from %s:%d to \"\n                                  \"%s:%d and closing it on all the other \"\n                                  \"importing nodes.\\n\",\n                                  slot, src->ip, src->port,\n                                  dst->ip, dst->port);\n            /* Move the slot to the destination node. */\n            success = clusterManagerMoveSlot(src, dst, slot, move_opts, NULL);\n            if (!success) goto cleanup;\n            /* Close slot on all the other importing nodes. */\n            listRewind(importing, &li);\n            while ((ln = listNext(&li)) != NULL) {\n                clusterManagerNode *n = ln->value;\n                if (dst == n) continue;\n                success = clusterManagerClearSlotStatus(n, slot);\n                if (!success) goto cleanup;\n            }\n        } else {\n            clusterManagerLogInfo(\">>> Case 3: Closing slot %d on both \"\n                                  \"migrating and importing nodes.\\n\", slot);\n            /* Close the slot on both the migrating node and the importing\n             * nodes. */\n            success = clusterManagerClearSlotStatus(src, slot);\n            if (!success) goto cleanup;\n            listRewind(importing, &li);\n            while ((ln = listNext(&li)) != NULL) {\n                clusterManagerNode *n = ln->value;\n                success = clusterManagerClearSlotStatus(n, slot);\n                if (!success) goto cleanup;\n            }\n        }\n    } else {\n        int try_to_close_slot = (listLength(importing) == 0 &&\n                                 listLength(migrating) == 1);\n        if (try_to_close_slot) {\n            clusterManagerNode *n = listFirst(migrating)->value;\n            if (!owner || owner != n) {\n                redisReply *r = CLUSTER_MANAGER_COMMAND(n,\n                    \"CLUSTER GETKEYSINSLOT %d %d\", slot, 10);\n                success = clusterManagerCheckRedisReply(n, r, NULL);\n                if (r) {\n                    if (success) try_to_close_slot = (r->elements == 0);\n                    freeReplyObject(r);\n                }\n                if (!success) goto cleanup;\n            }\n        }\n        /* Case 4: There are no slots claiming to be in importing state, but\n         * there is a migrating node that actually don't have any key or is the\n         * slot owner. We can just close the slot, probably a reshard\n         * interrupted in the middle. */\n        if (try_to_close_slot) {\n            clusterManagerNode *n = listFirst(migrating)->value;\n            clusterManagerLogInfo(\">>> Case 4: Closing slot %d on %s:%d\\n\",\n                                  slot, n->ip, n->port);\n            redisReply *r = CLUSTER_MANAGER_COMMAND(n, \"CLUSTER SETSLOT %d %s\",\n                                                    slot, \"STABLE\");\n            success = clusterManagerCheckRedisReply(n, r, NULL);\n            if (r) freeReplyObject(r);\n            if (!success) goto cleanup;\n        } else {\nunhandled_case:\n            success = 0;\n            clusterManagerLogErr(\"[ERR] Sorry, keydb-cli can't fix this slot \"\n                                 \"yet (work in progress). Slot is set as \"\n                                 \"migrating in %s, as importing in %s, \"\n                                 \"owner is %s:%d\\n\", migrating_str,\n                                 importing_str, owner->ip, owner->port);\n        }\n    }\ncleanup:\n    listRelease(owners);\n    listRelease(migrating);\n    listRelease(importing);\n    sdsfree(migrating_str);\n    sdsfree(importing_str);\n    return success;\n}\n\nint clusterManagerFixMultipleSlotOwners(int slot, list *owners) {\n    clusterManagerLogInfo(\">>> Fixing multiple owners for slot %d...\\n\", slot);\n    int success = 0;\n    assert(listLength(owners) > 1);\n    clusterManagerNode *owner = clusterManagerGetNodeWithMostKeysInSlot(owners,\n                                                                        slot,\n                                                                        NULL);\n    if (!owner) owner = listFirst(owners)->value;\n    clusterManagerLogInfo(\">>> Setting slot %d owner: %s:%d\\n\",\n                          slot, owner->ip, owner->port);\n    /* Set the slot owner. */\n    if (!clusterManagerSetSlotOwner(owner, slot, 0)) return 0;\n    listIter li;\n    listNode *ln;\n    listRewind(cluster_manager.nodes, &li);\n    /* Update configuration in all the other master nodes by assigning the slot\n     * itself to the new owner, and by eventually migrating keys if the node\n     * has keys for the slot. */\n    while ((ln = listNext(&li)) != NULL) {\n        clusterManagerNode *n = ln->value;\n        if (n == owner) continue;\n        if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE) continue;\n        int count = clusterManagerCountKeysInSlot(n, slot);\n        success = (count >= 0);\n        if (!success) break;\n        clusterManagerDelSlot(n, slot, 1);\n        if (!clusterManagerSetSlot(n, owner, slot, \"node\", NULL)) return 0;\n        if (count > 0) {\n            int opts = CLUSTER_MANAGER_OPT_VERBOSE |\n                       CLUSTER_MANAGER_OPT_COLD;\n            success = clusterManagerMoveSlot(n, owner, slot, opts, NULL);\n            if (!success) break;\n        }\n    }\n    return success;\n}\n\nstatic clusterManagerNode *clusterNodeForResharding(char *id,\n                                                    clusterManagerNode *target,\n                                                    int *raise_err)\n{\n    clusterManagerNode *node = NULL;\n    const char *invalid_node_msg = \"*** The specified node (%s) is not known \"\n                                   \"or not a master, please retry.\\n\";\n    node = clusterManagerNodeByName(id);\n    *raise_err = 0;\n    if (!node || node->flags & CLUSTER_MANAGER_FLAG_SLAVE) {\n        clusterManagerLogErr(invalid_node_msg, id);\n        *raise_err = 1;\n        return NULL;\n    } else if (target != NULL) {\n        if (!strcmp(node->name, target->name)) {\n            clusterManagerLogErr( \"*** It is not possible to use \"\n                                  \"the target node as \"\n                                  \"source node.\\n\");\n            return NULL;\n        }\n    }\n    return node;\n}\n\nstatic list *clusterManagerComputeReshardTable(list *sources, int numslots) {\n    list *moved = listCreate();\n    int src_count = listLength(sources), i = 0, tot_slots = 0, j;\n    clusterManagerNode **sorted = zmalloc(src_count * sizeof(*sorted), MALLOC_LOCAL);\n    listIter li;\n    listNode *ln;\n    listRewind(sources, &li);\n    while ((ln = listNext(&li)) != NULL) {\n        clusterManagerNode *node = ln->value;\n        tot_slots += node->slots_count;\n        sorted[i++] = node;\n    }\n    qsort(sorted, src_count, sizeof(clusterManagerNode *),\n          clusterManagerSlotCountCompareDesc);\n    for (i = 0; i < src_count; i++) {\n        clusterManagerNode *node = sorted[i];\n        float n = ((float) numslots / tot_slots * node->slots_count);\n        if (i == 0) n = ceil(n);\n        else n = floor(n);\n        int max = (int) n, count = 0;\n        for (j = 0; j < CLUSTER_MANAGER_SLOTS; j++) {\n            int slot = node->slots[j];\n            if (!slot) continue;\n            if (count >= max || (int)listLength(moved) >= numslots) break;\n            clusterManagerReshardTableItem *item = zmalloc(sizeof(*item), MALLOC_LOCAL);\n            item->source = node;\n            item->slot = j;\n            listAddNodeTail(moved, item);\n            count++;\n        }\n    }\n    zfree(sorted);\n    return moved;\n}\n\nstatic void clusterManagerShowReshardTable(list *table) {\n    listIter li;\n    listNode *ln;\n    listRewind(table, &li);\n    while ((ln = listNext(&li)) != NULL) {\n        clusterManagerReshardTableItem *item = ln->value;\n        clusterManagerNode *n = item->source;\n        printf(\"    Moving slot %d from %s\\n\", item->slot, (char *) n->name);\n    }\n}\n\nstatic void clusterManagerReleaseReshardTable(list *table) {\n    if (table != NULL) {\n        listIter li;\n        listNode *ln;\n        listRewind(table, &li);\n        while ((ln = listNext(&li)) != NULL) {\n            clusterManagerReshardTableItem *item = ln->value;\n            zfree(item);\n        }\n        listRelease(table);\n    }\n}\n\nvoid clusterManagerLog(int level, const char* fmt, ...) {\n    int use_colors =\n        (config.cluster_manager_command.flags & CLUSTER_MANAGER_CMD_FLAG_COLOR);\n    if (use_colors) {\n        printf(\"\\033[\");\n        switch (level) {\n        case CLUSTER_MANAGER_LOG_LVL_INFO: printf(LOG_COLOR_BOLD); break;\n        case CLUSTER_MANAGER_LOG_LVL_WARN: printf(LOG_COLOR_YELLOW); break;\n        case CLUSTER_MANAGER_LOG_LVL_ERR: printf(LOG_COLOR_RED); break;\n        case CLUSTER_MANAGER_LOG_LVL_SUCCESS: printf(LOG_COLOR_GREEN); break;\n        default: printf(LOG_COLOR_RESET); break;\n        }\n    }\n    va_list ap;\n    va_start(ap, fmt);\n    vprintf(fmt, ap);\n    va_end(ap);\n    if (use_colors) printf(\"\\033[\" LOG_COLOR_RESET);\n}\n\nstatic void clusterManagerNodeArrayInit(clusterManagerNodeArray *array,\n                                        int alloc_len)\n{\n    array->nodes = zcalloc(alloc_len * sizeof(clusterManagerNode*), MALLOC_LOCAL);\n    array->alloc = array->nodes;\n    array->len = alloc_len;\n    array->count = 0;\n}\n\n/* Reset array->nodes to the original array allocation and re-count non-NULL\n * nodes. */\nstatic void clusterManagerNodeArrayReset(clusterManagerNodeArray *array) {\n    if (array->nodes > array->alloc) {\n        array->len = array->nodes - array->alloc;\n        array->nodes = array->alloc;\n        array->count = 0;\n        int i = 0;\n        for(; i < array->len; i++) {\n            if (array->nodes[i] != NULL) array->count++;\n        }\n    }\n}\n\n/* Shift array->nodes and store the shifted node into 'nodeptr'. */\nstatic void clusterManagerNodeArrayShift(clusterManagerNodeArray *array,\n                                         clusterManagerNode **nodeptr)\n{\n    assert(array->len > 0);\n    /* If the first node to be shifted is not NULL, decrement count. */\n    if (*array->nodes != NULL) array->count--;\n    /* Store the first node to be shifted into 'nodeptr'. */\n    *nodeptr = *array->nodes;\n    /* Shift the nodes array and decrement length. */\n    array->nodes++;\n    array->len--;\n}\n\nstatic void clusterManagerNodeArrayAdd(clusterManagerNodeArray *array,\n                                       clusterManagerNode *node)\n{\n    assert(array->len > 0);\n    assert(node != NULL);\n    assert(array->count < array->len);\n    array->nodes[array->count++] = node;\n}\n\nstatic void clusterManagerPrintNotEmptyNodeError(clusterManagerNode *node,\n                                                 char *err)\n{\n    char *msg;\n    if (err) msg = err;\n    else {\n        msg = \"is not empty. Either the node already knows other \"\n              \"nodes (check with CLUSTER NODES) or contains some \"\n              \"key in database 0.\";\n    }\n    clusterManagerLogErr(\"[ERR] Node %s:%d %s\\n\", node->ip, node->port, msg);\n}\n\nstatic void clusterManagerPrintNotClusterNodeError(clusterManagerNode *node,\n                                                   char *err)\n{\n    char *msg = (err ? err : \"is not configured as a cluster node.\");\n    clusterManagerLogErr(\"[ERR] Node %s:%d %s\\n\", node->ip, node->port, msg);\n}\n\n/* Execute keydb-cli in Cluster Manager mode */\nstatic void clusterManagerMode(clusterManagerCommandProc *proc) {\n    int argc = config.cluster_manager_command.argc;\n    char **argv = config.cluster_manager_command.argv;\n    cluster_manager.nodes = NULL;\n    if (!proc(argc, argv)) goto cluster_manager_err;\n    freeClusterManager();\n    exit(0);\ncluster_manager_err:\n    freeClusterManager();\n    exit(1);\n}\n\n/* Cluster Manager Commands */\n\nstatic int clusterManagerCommandCreate(int argc, char **argv) {\n    int i, j, success = 1;\n    cluster_manager.nodes = listCreate();\n    for (i = 0; i < argc; i++) {\n        char *addr = argv[i];\n        char *c = strrchr(addr, '@');\n        if (c != NULL) *c = '\\0';\n        c = strrchr(addr, ':');\n        if (c == NULL) {\n            fprintf(stderr, \"Invalid address format: %s\\n\", addr);\n            return 0;\n        }\n        *c = '\\0';\n        char *ip = addr;\n        int port = atoi(++c);\n        clusterManagerNode *node = clusterManagerNewNode(ip, port);\n        if (!clusterManagerNodeConnect(node)) {\n            freeClusterManagerNode(node);\n            return 0;\n        }\n        char *err = NULL;\n        if (!clusterManagerNodeIsCluster(node, &err)) {\n            clusterManagerPrintNotClusterNodeError(node, err);\n            if (err) zfree(err);\n            freeClusterManagerNode(node);\n            return 0;\n        }\n        err = NULL;\n        if (!clusterManagerNodeLoadInfo(node, 0, &err)) {\n            if (err) {\n                CLUSTER_MANAGER_PRINT_REPLY_ERROR(node, err);\n                zfree(err);\n            }\n            freeClusterManagerNode(node);\n            return 0;\n        }\n        err = NULL;\n        if (!clusterManagerNodeIsEmpty(node, &err)) {\n            clusterManagerPrintNotEmptyNodeError(node, err);\n            if (err) zfree(err);\n            freeClusterManagerNode(node);\n            return 0;\n        }\n        listAddNodeTail(cluster_manager.nodes, node);\n    }\n    int node_len = cluster_manager.nodes->len;\n    int replicas = config.cluster_manager_command.replicas;\n    int masters_count = CLUSTER_MANAGER_MASTERS_COUNT(node_len, replicas);\n    if (masters_count < 3) {\n        clusterManagerLogErr(\n            \"*** ERROR: Invalid configuration for cluster creation.\\n\"\n            \"*** KeyDB Cluster requires at least 3 master nodes.\\n\"\n            \"*** This is not possible with %d nodes and %d replicas per node.\",\n            node_len, replicas);\n        clusterManagerLogErr(\"\\n*** At least %d nodes are required.\\n\",\n                             3 * (replicas + 1));\n        return 0;\n    }\n    clusterManagerLogInfo(\">>> Performing hash slots allocation \"\n                          \"on %d nodes...\\n\", node_len);\n    int interleaved_len = 0, ip_count = 0;\n    clusterManagerNode **interleaved = zcalloc(node_len*sizeof(**interleaved), MALLOC_LOCAL);\n    char **ips = zcalloc(node_len * sizeof(char*), MALLOC_LOCAL);\n    clusterManagerNodeArray *ip_nodes = zcalloc(node_len * sizeof(*ip_nodes), MALLOC_LOCAL);\n    listIter li;\n    listNode *ln;\n    listRewind(cluster_manager.nodes, &li);\n    while ((ln = listNext(&li)) != NULL) {\n        clusterManagerNode *n = ln->value;\n        int found = 0;\n        for (i = 0; i < ip_count; i++) {\n            char *ip = ips[i];\n            if (!strcmp(ip, n->ip)) {\n                found = 1;\n                break;\n            }\n        }\n        if (!found) {\n            ips[ip_count++] = n->ip;\n        }\n        clusterManagerNodeArray *node_array = &(ip_nodes[i]);\n        if (node_array->nodes == NULL)\n            clusterManagerNodeArrayInit(node_array, node_len);\n        clusterManagerNodeArrayAdd(node_array, n);\n    }\n    while (interleaved_len < node_len) {\n        for (i = 0; i < ip_count; i++) {\n            clusterManagerNodeArray *node_array = &(ip_nodes[i]);\n            if (node_array->count > 0) {\n                clusterManagerNode *n = NULL;\n                clusterManagerNodeArrayShift(node_array, &n);\n                interleaved[interleaved_len++] = n;\n            }\n        }\n    }\n    clusterManagerNode **masters = interleaved;\n    interleaved += masters_count;\n    interleaved_len -= masters_count;\n    float slots_per_node = CLUSTER_MANAGER_SLOTS / (float) masters_count;\n    long first = 0;\n    float cursor = 0.0f;\n    for (i = 0; i < masters_count; i++) {\n        clusterManagerNode *master = masters[i];\n        long last = lround(cursor + slots_per_node - 1);\n        if (last > CLUSTER_MANAGER_SLOTS || i == (masters_count - 1))\n            last = CLUSTER_MANAGER_SLOTS - 1;\n        if (last < first) last = first;\n        printf(\"Master[%d] -> Slots %ld - %ld\\n\", i, first, last);\n        master->slots_count = 0;\n        for (j = first; j <= last; j++) {\n            master->slots[j] = 1;\n            master->slots_count++;\n        }\n        master->dirty = 1;\n        first = last + 1;\n        cursor += slots_per_node;\n    }\n\n    /* Rotating the list sometimes helps to get better initial\n     * anti-affinity before the optimizer runs. */\n    clusterManagerNode *first_node = interleaved[0];\n    for (i = 0; i < (interleaved_len - 1); i++)\n        interleaved[i] = interleaved[i + 1];\n    interleaved[interleaved_len - 1] = first_node;\n    int assign_unused = 0, available_count = interleaved_len;\nassign_replicas:\n    for (i = 0; i < masters_count; i++) {\n        clusterManagerNode *master = masters[i];\n        int assigned_replicas = 0;\n        while (assigned_replicas < replicas) {\n            if (available_count == 0) break;\n            clusterManagerNode *found = NULL, *slave = NULL;\n            int firstNodeIdx = -1;\n            for (j = 0; j < interleaved_len; j++) {\n                clusterManagerNode *n = interleaved[j];\n                if (n == NULL) continue;\n                if (strcmp(n->ip, master->ip)) {\n                    found = n;\n                    interleaved[j] = NULL;\n                    break;\n                }\n                if (firstNodeIdx < 0) firstNodeIdx = j;\n            }\n            if (found) slave = found;\n            else if (firstNodeIdx >= 0) {\n                slave = interleaved[firstNodeIdx];\n                interleaved_len -= (interleaved - (interleaved + firstNodeIdx));\n                interleaved += (firstNodeIdx + 1);\n            }\n            if (slave != NULL) {\n                assigned_replicas++;\n                available_count--;\n                if (slave->replicate) sdsfree(slave->replicate);\n                slave->replicate = sdsnew(master->name);\n                slave->dirty = 1;\n            } else break;\n            printf(\"Adding replica %s:%d to %s:%d\\n\", slave->ip, slave->port,\n                   master->ip, master->port);\n            if (assign_unused) break;\n        }\n    }\n    if (!assign_unused && available_count > 0) {\n        assign_unused = 1;\n        printf(\"Adding extra replicas...\\n\");\n        goto assign_replicas;\n    }\n    for (i = 0; i < ip_count; i++) {\n        clusterManagerNodeArray *node_array = ip_nodes + i;\n        clusterManagerNodeArrayReset(node_array);\n    }\n    clusterManagerOptimizeAntiAffinity(ip_nodes, ip_count);\n    clusterManagerShowNodes();\n    int ignore_force = 0;\n    if (confirmWithYes(\"Can I set the above configuration?\", ignore_force)) {\n        listRewind(cluster_manager.nodes, &li);\n        while ((ln = listNext(&li)) != NULL) {\n            clusterManagerNode *node = ln->value;\n            char *err = NULL;\n            int flushed = clusterManagerFlushNodeConfig(node, &err);\n            if (!flushed && node->dirty && !node->replicate) {\n                if (err != NULL) {\n                    CLUSTER_MANAGER_PRINT_REPLY_ERROR(node, err);\n                    zfree(err);\n                }\n                success = 0;\n                goto cleanup;\n            } else if (err != NULL) zfree(err);\n        }\n        clusterManagerLogInfo(\">>> Nodes configuration updated\\n\");\n        clusterManagerLogInfo(\">>> Assign a different config epoch to \"\n                              \"each node\\n\");\n        int config_epoch = 1;\n        listRewind(cluster_manager.nodes, &li);\n        while ((ln = listNext(&li)) != NULL) {\n            clusterManagerNode *node = ln->value;\n            redisReply *reply = NULL;\n            reply = CLUSTER_MANAGER_COMMAND(node,\n                                            \"cluster set-config-epoch %d\",\n                                            config_epoch++);\n            if (reply != NULL) freeReplyObject(reply);\n        }\n        clusterManagerLogInfo(\">>> Sending CLUSTER MEET messages to join \"\n                              \"the cluster\\n\");\n        clusterManagerNode *first = NULL;\n        listRewind(cluster_manager.nodes, &li);\n        while ((ln = listNext(&li)) != NULL) {\n            clusterManagerNode *node = ln->value;\n            if (first == NULL) {\n                first = node;\n                continue;\n            }\n            redisReply *reply = NULL;\n            reply = CLUSTER_MANAGER_COMMAND(node, \"cluster meet %s %d\",\n                                            first->ip, first->port);\n            int is_err = 0;\n            if (reply != NULL) {\n                if ((is_err = reply->type == REDIS_REPLY_ERROR))\n                    CLUSTER_MANAGER_PRINT_REPLY_ERROR(node, reply->str);\n                freeReplyObject(reply);\n            } else {\n                is_err = 1;\n                fprintf(stderr, \"Failed to send CLUSTER MEET command.\\n\");\n            }\n            if (is_err) {\n                success = 0;\n                goto cleanup;\n            }\n        }\n        /* Give one second for the join to start, in order to avoid that\n         * waiting for cluster join will find all the nodes agree about\n         * the config as they are still empty with unassigned slots. */\n        sleep(1);\n        clusterManagerWaitForClusterJoin();\n        /* Useful for the replicas */\n        listRewind(cluster_manager.nodes, &li);\n        while ((ln = listNext(&li)) != NULL) {\n            clusterManagerNode *node = ln->value;\n            if (!node->dirty) continue;\n            char *err = NULL;\n            int flushed = clusterManagerFlushNodeConfig(node, &err);\n            if (!flushed && !node->replicate) {\n                if (err != NULL) {\n                    CLUSTER_MANAGER_PRINT_REPLY_ERROR(node, err);\n                    zfree(err);\n                }\n                success = 0;\n                goto cleanup;\n            }\n        }\n        // Reset Nodes\n        listRewind(cluster_manager.nodes, &li);\n        clusterManagerNode *first_node = NULL;\n        while ((ln = listNext(&li)) != NULL) {\n            clusterManagerNode *node = ln->value;\n            if (!first_node) first_node = node;\n            else freeClusterManagerNode(node);\n        }\n        listEmpty(cluster_manager.nodes);\n        if (!clusterManagerLoadInfoFromNode(first_node, 0)) {\n            success = 0;\n            goto cleanup;\n        }\n        clusterManagerCheckCluster(0);\n    }\ncleanup:\n    /* Free everything */\n    zfree(masters);\n    zfree(ips);\n    for (i = 0; i < node_len; i++) {\n        clusterManagerNodeArray *node_array = ip_nodes + i;\n        CLUSTER_MANAGER_NODE_ARRAY_FREE(node_array);\n    }\n    zfree(ip_nodes);\n    return success;\n}\n\nstatic int clusterManagerCommandAddNode(int argc, char **argv) {\n    int success = 1;\n    redisReply *reply = NULL;\n    char *ref_ip = NULL, *ip = NULL;\n    int ref_port = 0, port = 0;\n    if (!getClusterHostFromCmdArgs(argc - 1, argv + 1, &ref_ip, &ref_port))\n        goto invalid_args;\n    if (!getClusterHostFromCmdArgs(1, argv, &ip, &port))\n        goto invalid_args;\n    clusterManagerLogInfo(\">>> Adding node %s:%d to cluster %s:%d\\n\", ip, port,\n                          ref_ip, ref_port);\n    // Check the existing cluster\n    clusterManagerNode *refnode = clusterManagerNewNode(ref_ip, ref_port);\n    if (!clusterManagerLoadInfoFromNode(refnode, 0)) return 0;\n    if (!clusterManagerCheckCluster(0)) return 0;\n\n    /* If --cluster-master-id was specified, try to resolve it now so that we\n     * abort before starting with the node configuration. */\n    clusterManagerNode *master_node = NULL;\n    if (config.cluster_manager_command.flags & CLUSTER_MANAGER_CMD_FLAG_SLAVE) {\n        char *master_id = config.cluster_manager_command.master_id;\n        if (master_id != NULL) {\n            master_node = clusterManagerNodeByName(master_id);\n            if (master_node == NULL) {\n                clusterManagerLogErr(\"[ERR] No such master ID %s\\n\", master_id);\n                return 0;\n            }\n        } else {\n            master_node = clusterManagerNodeWithLeastReplicas();\n            assert(master_node != NULL);\n            printf(\"Automatically selected master %s:%d\\n\", master_node->ip,\n                   master_node->port);\n        }\n    }\n\n    // Add the new node\n    clusterManagerNode *new_node = clusterManagerNewNode(ip, port);\n    int added = 0;\n    if (!clusterManagerNodeConnect(new_node)) {\n        clusterManagerLogErr(\"[ERR] Sorry, can't connect to node %s:%d\\n\",\n                             ip, port);\n        success = 0;\n        goto cleanup;\n    }\n    char *err = NULL;\n    if (!(success = clusterManagerNodeIsCluster(new_node, &err))) {\n        clusterManagerPrintNotClusterNodeError(new_node, err);\n        if (err) zfree(err);\n        goto cleanup;\n    }\n    if (!clusterManagerNodeLoadInfo(new_node, 0, &err)) {\n        if (err) {\n            CLUSTER_MANAGER_PRINT_REPLY_ERROR(new_node, err);\n            zfree(err);\n        }\n        success = 0;\n        goto cleanup;\n    }\n    if (!(success = clusterManagerNodeIsEmpty(new_node, &err))) {\n        clusterManagerPrintNotEmptyNodeError(new_node, err);\n        if (err) zfree(err);\n        goto cleanup;\n    }\n    clusterManagerNode *first = listFirst(cluster_manager.nodes)->value;\n    listAddNodeTail(cluster_manager.nodes, new_node);\n    added = 1;\n\n    // Send CLUSTER MEET command to the new node\n    clusterManagerLogInfo(\">>> Send CLUSTER MEET to node %s:%d to make it \"\n                          \"join the cluster.\\n\", ip, port);\n    reply = CLUSTER_MANAGER_COMMAND(new_node, \"CLUSTER MEET %s %d\",\n                                    first->ip, first->port);\n    if (!(success = clusterManagerCheckRedisReply(new_node, reply, NULL)))\n        goto cleanup;\n\n    /* Additional configuration is needed if the node is added as a slave. */\n    if (master_node) {\n        sleep(1);\n        clusterManagerWaitForClusterJoin();\n        clusterManagerLogInfo(\">>> Configure node as replica of %s:%d.\\n\",\n                              master_node->ip, master_node->port);\n        freeReplyObject(reply);\n        reply = CLUSTER_MANAGER_COMMAND(new_node, \"CLUSTER REPLICATE %s\",\n                                        master_node->name);\n        if (!(success = clusterManagerCheckRedisReply(new_node, reply, NULL)))\n            goto cleanup;\n    }\n    clusterManagerLogOk(\"[OK] New node added correctly.\\n\");\ncleanup:\n    if (!added && new_node) freeClusterManagerNode(new_node);\n    if (reply) freeReplyObject(reply);\n    return success;\ninvalid_args:\n    fprintf(stderr, CLUSTER_MANAGER_INVALID_HOST_ARG);\n    return 0;\n}\n\nstatic int clusterManagerCommandDeleteNode(int argc, char **argv) {\n    UNUSED(argc);\n    int success = 1;\n    int port = 0;\n    char *ip = NULL;\n    if (!getClusterHostFromCmdArgs(1, argv, &ip, &port)) goto invalid_args;\n    char *node_id = argv[1];\n    clusterManagerLogInfo(\">>> Removing node %s from cluster %s:%d\\n\",\n                          node_id, ip, port);\n    clusterManagerNode *ref_node = clusterManagerNewNode(ip, port);\n    clusterManagerNode *node = NULL;\n\n    // Load cluster information\n    if (!clusterManagerLoadInfoFromNode(ref_node, 0)) return 0;\n\n    // Check if the node exists and is not empty\n    node = clusterManagerNodeByName(node_id);\n    if (node == NULL) {\n        clusterManagerLogErr(\"[ERR] No such node ID %s\\n\", node_id);\n        return 0;\n    }\n    if (node->slots_count != 0) {\n        clusterManagerLogErr(\"[ERR] Node %s:%d is not empty! Reshard data \"\n                             \"away and try again.\\n\", node->ip, node->port);\n        return 0;\n    }\n\n    // Send CLUSTER FORGET to all the nodes but the node to remove\n    clusterManagerLogInfo(\">>> Sending CLUSTER FORGET messages to the \"\n                          \"cluster...\\n\");\n    listIter li;\n    listNode *ln;\n    listRewind(cluster_manager.nodes, &li);\n    while ((ln = listNext(&li)) != NULL) {\n        clusterManagerNode *n = ln->value;\n        if (n == node) continue;\n        if (n->replicate && !strcasecmp(n->replicate, node_id)) {\n            // Reconfigure the slave to replicate with some other node\n            clusterManagerNode *master = clusterManagerNodeWithLeastReplicas();\n            assert(master != NULL);\n            clusterManagerLogInfo(\">>> %s:%d as replica of %s:%d\\n\",\n                                  n->ip, n->port, master->ip, master->port);\n            redisReply *r = CLUSTER_MANAGER_COMMAND(n, \"CLUSTER REPLICATE %s\",\n                                                    master->name);\n            success = clusterManagerCheckRedisReply(n, r, NULL);\n            if (r) freeReplyObject(r);\n            if (!success) return 0;\n        }\n        redisReply *r = CLUSTER_MANAGER_COMMAND(n, \"CLUSTER FORGET %s\",\n                                                node_id);\n        success = clusterManagerCheckRedisReply(n, r, NULL);\n        if (r) freeReplyObject(r);\n        if (!success) return 0;\n    }\n\n    /* Finally send CLUSTER RESET to the node. */\n    clusterManagerLogInfo(\">>> Sending CLUSTER RESET SOFT to the \"\n                          \"deleted node.\\n\");\n    redisReply *r = redisCommand(node->context, \"CLUSTER RESET %s\", \"SOFT\");\n    success = clusterManagerCheckRedisReply(node, r, NULL);\n    if (r) freeReplyObject(r);\n    return success;\ninvalid_args:\n    fprintf(stderr, CLUSTER_MANAGER_INVALID_HOST_ARG);\n    return 0;\n}\n\nstatic int clusterManagerCommandInfo(int argc, char **argv) {\n    int port = 0;\n    char *ip = NULL;\n    if (!getClusterHostFromCmdArgs(argc, argv, &ip, &port)) goto invalid_args;\n    clusterManagerNode *node = clusterManagerNewNode(ip, port);\n    if (!clusterManagerLoadInfoFromNode(node, 0)) return 0;\n    clusterManagerShowClusterInfo();\n    return 1;\ninvalid_args:\n    fprintf(stderr, CLUSTER_MANAGER_INVALID_HOST_ARG);\n    return 0;\n}\n\nstatic int clusterManagerCommandCheck(int argc, char **argv) {\n    int port = 0;\n    char *ip = NULL;\n    if (!getClusterHostFromCmdArgs(argc, argv, &ip, &port)) goto invalid_args;\n    clusterManagerNode *node = clusterManagerNewNode(ip, port);\n    if (!clusterManagerLoadInfoFromNode(node, 0)) return 0;\n    clusterManagerShowClusterInfo();\n    return clusterManagerCheckCluster(0);\ninvalid_args:\n    fprintf(stderr, CLUSTER_MANAGER_INVALID_HOST_ARG);\n    return 0;\n}\n\nstatic int clusterManagerCommandFix(int argc, char **argv) {\n    config.cluster_manager_command.flags |= CLUSTER_MANAGER_CMD_FLAG_FIX;\n    return clusterManagerCommandCheck(argc, argv);\n}\n\nstatic int clusterManagerCommandReshard(int argc, char **argv) {\n    int port = 0;\n    char *ip = NULL;\n    if (!getClusterHostFromCmdArgs(argc, argv, &ip, &port)) goto invalid_args;\n    clusterManagerNode *node = clusterManagerNewNode(ip, port);\n    if (!clusterManagerLoadInfoFromNode(node, 0)) return 0;\n    clusterManagerCheckCluster(0);\n    if (cluster_manager.errors && listLength(cluster_manager.errors) > 0 && !config.force_mode) {\n        fflush(stdout);\n        fprintf(stderr,\n                \"*** Please fix your cluster problems before resharding\\n\");\n        return 0;\n    }\n    int slots = config.cluster_manager_command.slots;\n    if (!slots) {\n        while (slots <= 0 || slots > CLUSTER_MANAGER_SLOTS) {\n            printf(\"How many slots do you want to move (from 1 to %d)? \",\n                   CLUSTER_MANAGER_SLOTS);\n            fflush(stdout);\n            char buf[6];\n            int nread = read(fileno(stdin),buf,6);\n            if (nread <= 0) continue;\n            int last_idx = nread - 1;\n            if (buf[last_idx] != '\\n') {\n                int ch;\n                while ((ch = getchar()) != '\\n' && ch != EOF) {}\n            }\n            buf[last_idx] = '\\0';\n            slots = atoi(buf);\n        }\n    }\n    char buf[255];\n    char *to = config.cluster_manager_command.to,\n         *from = config.cluster_manager_command.from;\n    while (to == NULL) {\n        printf(\"What is the receiving node ID? \");\n        fflush(stdout);\n        int nread = read(fileno(stdin),buf,255);\n        if (nread <= 0) continue;\n        int last_idx = nread - 1;\n        if (buf[last_idx] != '\\n') {\n            int ch;\n            while ((ch = getchar()) != '\\n' && ch != EOF) {}\n        }\n        buf[last_idx] = '\\0';\n        if (strlen(buf) > 0) to = buf;\n    }\n    int raise_err = 0;\n    clusterManagerNode *target = clusterNodeForResharding(to, NULL, &raise_err);\n    if (target == NULL) return 0;\n    list *sources = listCreate();\n    list *table = NULL;\n    int all = 0, result = 1;\n    if (from == NULL) {\n        printf(\"Please enter all the source node IDs.\\n\");\n        printf(\"  Type 'all' to use all the nodes as source nodes for \"\n               \"the hash slots.\\n\");\n        printf(\"  Type 'done' once you entered all the source nodes IDs.\\n\");\n        while (1) {\n            printf(\"Source node #%lu: \", listLength(sources) + 1);\n            fflush(stdout);\n            int nread = read(fileno(stdin),buf,255);\n            if (nread <= 0) continue;\n            int last_idx = nread - 1;\n            if (buf[last_idx] != '\\n') {\n                int ch;\n                while ((ch = getchar()) != '\\n' && ch != EOF) {}\n            }\n            buf[last_idx] = '\\0';\n            if (!strcmp(buf, \"done\")) break;\n            else if (!strcmp(buf, \"all\")) {\n                all = 1;\n                break;\n            } else {\n                clusterManagerNode *src =\n                    clusterNodeForResharding(buf, target, &raise_err);\n                if (src != NULL) listAddNodeTail(sources, src);\n                else if (raise_err) {\n                    result = 0;\n                    goto cleanup;\n                }\n            }\n        }\n    } else {\n        char *p;\n        while((p = strchr(from, ',')) != NULL) {\n            *p = '\\0';\n            if (!strcmp(from, \"all\")) {\n                all = 1;\n                break;\n            } else {\n                clusterManagerNode *src =\n                    clusterNodeForResharding(from, target, &raise_err);\n                if (src != NULL) listAddNodeTail(sources, src);\n                else if (raise_err) {\n                    result = 0;\n                    goto cleanup;\n                }\n            }\n            from = p + 1;\n        }\n        /* Check if there's still another source to process. */\n        if (!all && strlen(from) > 0) {\n            if (!strcmp(from, \"all\")) all = 1;\n            if (!all) {\n                clusterManagerNode *src =\n                    clusterNodeForResharding(from, target, &raise_err);\n                if (src != NULL) listAddNodeTail(sources, src);\n                else if (raise_err) {\n                    result = 0;\n                    goto cleanup;\n                }\n            }\n        }\n    }\n    listIter li;\n    listNode *ln;\n    if (all) {\n        listEmpty(sources);\n        listRewind(cluster_manager.nodes, &li);\n        while ((ln = listNext(&li)) != NULL) {\n            clusterManagerNode *n = ln->value;\n            if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE || n->replicate)\n                continue;\n            if (!sdscmp(n->name, target->name)) continue;\n            listAddNodeTail(sources, n);\n        }\n    }\n    if (listLength(sources) == 0) {\n        fprintf(stderr, \"*** No source nodes given, operation aborted.\\n\");\n        result = 0;\n        goto cleanup;\n    }\n    printf(\"\\nReady to move %d slots.\\n\", slots);\n    printf(\"  Source nodes:\\n\");\n    listRewind(sources, &li);\n    while ((ln = listNext(&li)) != NULL) {\n        clusterManagerNode *src = ln->value;\n        sds info = clusterManagerNodeInfo(src, 4);\n        printf(\"%s\\n\", info);\n        sdsfree(info);\n    }\n    printf(\"  Destination node:\\n\");\n    sds info = clusterManagerNodeInfo(target, 4);\n    printf(\"%s\\n\", info);\n    sdsfree(info);\n    table = clusterManagerComputeReshardTable(sources, slots);\n    printf(\"  Resharding plan:\\n\");\n    clusterManagerShowReshardTable(table);\n    if (!(config.cluster_manager_command.flags &\n          CLUSTER_MANAGER_CMD_FLAG_YES))\n    {\n        printf(\"Do you want to proceed with the proposed \"\n               \"reshard plan (yes/no)? \");\n        fflush(stdout);\n        char buf[4];\n        int nread = read(fileno(stdin),buf,4);\n        buf[3] = '\\0';\n        if (nread <= 0 || strcmp(\"yes\", buf) != 0) {\n            result = 0;\n            goto cleanup;\n        }\n    }\n    int opts = CLUSTER_MANAGER_OPT_VERBOSE;\n    listRewind(table, &li);\n    while ((ln = listNext(&li)) != NULL) {\n        clusterManagerReshardTableItem *item = ln->value;\n        char *err = NULL;\n        result = clusterManagerMoveSlot(item->source, target, item->slot,\n                                        opts, &err);\n        if (!result) {\n            if (err != NULL) {\n                //clusterManagerLogErr(\"\\n%s\\n\", err);\n                zfree(err);\n            }\n            goto cleanup;\n        }\n    }\ncleanup:\n    listRelease(sources);\n    clusterManagerReleaseReshardTable(table);\n    return result;\ninvalid_args:\n    fprintf(stderr, CLUSTER_MANAGER_INVALID_HOST_ARG);\n    return 0;\n}\n\nstatic int clusterManagerCommandRebalance(int argc, char **argv) {\n    int port = 0;\n    char *ip = NULL;\n    clusterManagerNode **weightedNodes = NULL;\n    list *involved = NULL;\n    if (!getClusterHostFromCmdArgs(argc, argv, &ip, &port)) goto invalid_args;\n    clusterManagerNode *node = clusterManagerNewNode(ip, port);\n    if (!clusterManagerLoadInfoFromNode(node, 0)) return 0;\n    int result = 1, i;\n    if (config.cluster_manager_command.weight != NULL) {\n        for (i = 0; i < config.cluster_manager_command.weight_argc; i++) {\n            char *name = config.cluster_manager_command.weight[i];\n            char *p = strchr(name, '=');\n            if (p == NULL) {\n                result = 0;\n                goto cleanup;\n            }\n            *p = '\\0';\n            float w = atof(++p);\n            clusterManagerNode *n = clusterManagerNodeByAbbreviatedName(name);\n            if (n == NULL) {\n                clusterManagerLogErr(\"*** No such master node %s\\n\", name);\n                result = 0;\n                goto cleanup;\n            }\n            n->weight = w;\n        }\n    }\n    float total_weight = 0;\n    int nodes_involved = 0;\n    int use_empty = config.cluster_manager_command.flags &\n                    CLUSTER_MANAGER_CMD_FLAG_EMPTYMASTER;\n    involved = listCreate();\n    listIter li;\n    listNode *ln;\n    listRewind(cluster_manager.nodes, &li);\n    /* Compute the total cluster weight. */\n    while ((ln = listNext(&li)) != NULL) {\n        clusterManagerNode *n = ln->value;\n        if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE || n->replicate)\n            continue;\n        if (!use_empty && n->slots_count == 0) {\n            n->weight = 0;\n            continue;\n        }\n        total_weight += n->weight;\n        nodes_involved++;\n        listAddNodeTail(involved, n);\n    }\n    weightedNodes = zmalloc(nodes_involved * sizeof(clusterManagerNode *), MALLOC_LOCAL);\n    if (weightedNodes == NULL) goto cleanup;\n    /* Check cluster, only proceed if it looks sane. */\n    clusterManagerCheckCluster(1);\n    if (cluster_manager.errors && listLength(cluster_manager.errors) > 0 && !config.force_mode) {\n        clusterManagerLogErr(\"*** Please fix your cluster problems \"\n                             \"before rebalancing\\n\");\n        result = 0;\n        goto cleanup;\n    }\n    /* Calculate the slots balance for each node. It's the number of\n     * slots the node should lose (if positive) or gain (if negative)\n     * in order to be balanced. */\n    int threshold_reached = 0, total_balance = 0;\n    float threshold = config.cluster_manager_command.threshold;\n    i = 0;\n    listRewind(involved, &li);\n    while ((ln = listNext(&li)) != NULL) {\n        clusterManagerNode *n = ln->value;\n        weightedNodes[i++] = n;\n        int expected = (int) (((float)CLUSTER_MANAGER_SLOTS / total_weight) *\n                        n->weight);\n        n->balance = n->slots_count - expected;\n        total_balance += n->balance;\n        /* Compute the percentage of difference between the\n         * expected number of slots and the real one, to see\n         * if it's over the threshold specified by the user. */\n        int over_threshold = 0;\n        if (threshold > 0) {\n            if (n->slots_count > 0) {\n                float err_perc = fabs((100-(100.0*expected/n->slots_count)));\n                if (err_perc > threshold) over_threshold = 1;\n            } else if (expected > 1) {\n                over_threshold = 1;\n            }\n        }\n        if (over_threshold) threshold_reached = 1;\n    }\n    if (!threshold_reached) {\n        clusterManagerLogWarn(\"*** No rebalancing needed! \"\n                             \"All nodes are within the %.2f%% threshold.\\n\",\n                             config.cluster_manager_command.threshold);\n        goto cleanup;\n    }\n    /* Because of rounding, it is possible that the balance of all nodes\n     * summed does not give 0. Make sure that nodes that have to provide\n     * slots are always matched by nodes receiving slots. */\n    while (total_balance > 0) {\n        listRewind(involved, &li);\n        while ((ln = listNext(&li)) != NULL) {\n            clusterManagerNode *n = ln->value;\n            if (n->balance <= 0 && total_balance > 0) {\n                n->balance--;\n                total_balance--;\n            }\n        }\n    }\n    /* Sort nodes by their slots balance. */\n    qsort(weightedNodes, nodes_involved, sizeof(clusterManagerNode *),\n          clusterManagerCompareNodeBalance);\n    clusterManagerLogInfo(\">>> Rebalancing across %d nodes. \"\n                          \"Total weight = %.2f\\n\",\n                          nodes_involved, total_weight);\n    if (config.verbose) {\n        for (i = 0; i < nodes_involved; i++) {\n            clusterManagerNode *n = weightedNodes[i];\n            printf(\"%s:%d balance is %d slots\\n\", n->ip, n->port, n->balance);\n        }\n    }\n    /* Now we have at the start of the 'sn' array nodes that should get\n     * slots, at the end nodes that must give slots.\n     * We take two indexes, one at the start, and one at the end,\n     * incrementing or decrementing the indexes accordingly til we\n     * find nodes that need to get/provide slots. */\n    int dst_idx = 0;\n    int src_idx = nodes_involved - 1;\n    int simulate = config.cluster_manager_command.flags &\n                   CLUSTER_MANAGER_CMD_FLAG_SIMULATE;\n    while (dst_idx < src_idx) {\n        clusterManagerNode *dst = weightedNodes[dst_idx];\n        clusterManagerNode *src = weightedNodes[src_idx];\n        int db = abs(dst->balance);\n        int sb = abs(src->balance);\n        int numslots = (db < sb ? db : sb);\n        if (numslots > 0) {\n            printf(\"Moving %d slots from %s:%d to %s:%d\\n\", numslots,\n                                                            src->ip,\n                                                            src->port,\n                                                            dst->ip,\n                                                            dst->port);\n            /* Actually move the slots. */\n            list *lsrc = listCreate(), *table = NULL;\n            listAddNodeTail(lsrc, src);\n            table = clusterManagerComputeReshardTable(lsrc, numslots);\n            listRelease(lsrc);\n            int table_len = 0;\n            if (!table || (table_len = (int) listLength(table)) != numslots) {\n                clusterManagerLogErr(\"*** Assertion failed: Reshard table \"\n                                     \"!= number of slots\");\n                result = 0;\n                goto end_move;\n            }\n            if (simulate) {\n                for (i = 0; i < table_len; i++) printf(\"#\");\n            } else {\n                int opts = CLUSTER_MANAGER_OPT_QUIET |\n                           CLUSTER_MANAGER_OPT_UPDATE;\n                listRewind(table, &li);\n                while ((ln = listNext(&li)) != NULL) {\n                    clusterManagerReshardTableItem *item = ln->value;\n                    result = clusterManagerMoveSlot(item->source,\n                                                    dst,\n                                                    item->slot,\n                                                    opts, NULL);\n                    if (!result) goto end_move;\n                    printf(\"#\");\n                    fflush(stdout);\n                }\n\n            }\n            printf(\"\\n\");\nend_move:\n            clusterManagerReleaseReshardTable(table);\n            if (!result) goto cleanup;\n        }\n        /* Update nodes balance. */\n        dst->balance += numslots;\n        src->balance -= numslots;\n        if (dst->balance == 0) dst_idx++;\n        if (src->balance == 0) src_idx --;\n    }\ncleanup:\n    if (involved != NULL) listRelease(involved);\n    if (weightedNodes != NULL) zfree(weightedNodes);\n    return result;\ninvalid_args:\n    fprintf(stderr, CLUSTER_MANAGER_INVALID_HOST_ARG);\n    return 0;\n}\n\nstatic int clusterManagerCommandSetTimeout(int argc, char **argv) {\n    UNUSED(argc);\n    int port = 0;\n    char *ip = NULL;\n    if (!getClusterHostFromCmdArgs(1, argv, &ip, &port)) goto invalid_args;\n    int timeout = atoi(argv[1]);\n    if (timeout < 100) {\n        fprintf(stderr, \"Setting a node timeout of less than 100 \"\n                \"milliseconds is a bad idea.\\n\");\n        return 0;\n    }\n    // Load cluster information\n    clusterManagerNode *node = clusterManagerNewNode(ip, port);\n    if (!clusterManagerLoadInfoFromNode(node, 0)) return 0;\n    int ok_count = 0, err_count = 0;\n\n    clusterManagerLogInfo(\">>> Reconfiguring node timeout in every \"\n                          \"cluster node...\\n\");\n    listIter li;\n    listNode *ln;\n    listRewind(cluster_manager.nodes, &li);\n    while ((ln = listNext(&li)) != NULL) {\n        clusterManagerNode *n = ln->value;\n        char *err = NULL;\n        redisReply *reply = CLUSTER_MANAGER_COMMAND(n, \"CONFIG %s %s %d\",\n                                                    \"SET\",\n                                                    \"cluster-node-timeout\",\n                                                    timeout);\n        if (reply == NULL) goto reply_err;\n        int ok = clusterManagerCheckRedisReply(n, reply, &err);\n        freeReplyObject(reply);\n        if (!ok) goto reply_err;\n        reply = CLUSTER_MANAGER_COMMAND(n, \"CONFIG %s\", \"REWRITE\");\n        if (reply == NULL) goto reply_err;\n        ok = clusterManagerCheckRedisReply(n, reply, &err);\n        freeReplyObject(reply);\n        if (!ok) goto reply_err;\n        clusterManagerLogWarn(\"*** New timeout set for %s:%d\\n\", n->ip,\n                              n->port);\n        ok_count++;\n        continue;\nreply_err:;\n        int need_free = 0;\n        if (err == NULL) err = \"\";\n        else need_free = 1;\n        clusterManagerLogErr(\"ERR setting node-timeot for %s:%d: %s\\n\", n->ip,\n                             n->port, err);\n        if (need_free) zfree(err);\n        err_count++;\n    }\n    clusterManagerLogInfo(\">>> New node timeout set. %d OK, %d ERR.\\n\",\n                          ok_count, err_count);\n    return 1;\ninvalid_args:\n    fprintf(stderr, CLUSTER_MANAGER_INVALID_HOST_ARG);\n    return 0;\n}\n\nstatic int clusterManagerCommandImport(int argc, char **argv) {\n    int success = 1;\n    int port = 0, src_port = 0;\n    char *ip = NULL, *src_ip = NULL;\n    char *invalid_args_msg = NULL;\n    sds cmdfmt = NULL;\n    if (!getClusterHostFromCmdArgs(argc, argv, &ip, &port)) {\n        invalid_args_msg = CLUSTER_MANAGER_INVALID_HOST_ARG;\n        goto invalid_args;\n    }\n    if (config.cluster_manager_command.from == NULL) {\n        invalid_args_msg = \"[ERR] Option '--cluster-from' is required for \"\n                           \"subcommand 'import'.\\n\";\n        goto invalid_args;\n    }\n    char *src_host[] = {config.cluster_manager_command.from};\n    if (!getClusterHostFromCmdArgs(1, src_host, &src_ip, &src_port)) {\n        invalid_args_msg = \"[ERR] Invalid --cluster-from host. You need to \"\n                           \"pass a valid address (ie. 120.0.0.1:7000).\\n\";\n        goto invalid_args;\n    }\n    clusterManagerLogInfo(\">>> Importing data from %s:%d to cluster %s:%d\\n\",\n                          src_ip, src_port, ip, port);\n\n    clusterManagerNode *refnode = clusterManagerNewNode(ip, port);\n    if (!clusterManagerLoadInfoFromNode(refnode, 0)) return 0;\n    if (!clusterManagerCheckCluster(0)) return 0;\n    char *reply_err = NULL;\n    redisReply *src_reply = NULL;\n    // Connect to the source node.\n    struct timeval tv;\n    tv.tv_sec = config.cluster_manager_command.timeout / 1000;\n    tv.tv_usec = (config.cluster_manager_command.timeout % 1000) * 1000;\n    redisContext *src_ctx = redisConnectWithTimeout(src_ip, src_port, tv);\n    if (src_ctx->err) {\n        success = 0;\n        fprintf(stderr,\"Could not connect to KeyDB at %s:%d: %s.\\n\", src_ip,\n                src_port, src_ctx->errstr);\n        goto cleanup;\n    }\n    // Auth for the source node. \n    char *from_user = config.cluster_manager_command.from_user;\n    char *from_pass = config.cluster_manager_command.from_pass;\n    if (cliAuth(src_ctx, from_user, from_pass) == REDIS_ERR) {\n        success = 0;\n        goto cleanup;\n    }\n\n    src_reply = reconnectingRedisCommand(src_ctx, \"INFO\");\n    if (!src_reply || src_reply->type == REDIS_REPLY_ERROR) {\n        if (src_reply && src_reply->str) reply_err = src_reply->str;\n        success = 0;\n        goto cleanup;\n    }\n    if (getLongInfoField(src_reply->str, \"cluster_enabled\")) {\n        clusterManagerLogErr(\"[ERR] The source node should not be a \"\n                             \"cluster node.\\n\");\n        success = 0;\n        goto cleanup;\n    }\n    freeReplyObject(src_reply);\n    src_reply = reconnectingRedisCommand(src_ctx, \"DBSIZE\");\n    if (!src_reply || src_reply->type == REDIS_REPLY_ERROR) {\n        if (src_reply && src_reply->str) reply_err = src_reply->str;\n        success = 0;\n        goto cleanup;\n    }\n    int size = src_reply->integer, i;\n    clusterManagerLogWarn(\"*** Importing %d keys from DB 0\\n\", size);\n\n    // Build a slot -> node map\n    clusterManagerNode  *slots_map[CLUSTER_MANAGER_SLOTS];\n    memset(slots_map, 0, sizeof(slots_map));\n    listIter li;\n    listNode *ln;\n    for (i = 0; i < CLUSTER_MANAGER_SLOTS; i++) {\n        listRewind(cluster_manager.nodes, &li);\n        while ((ln = listNext(&li)) != NULL) {\n            clusterManagerNode *n = ln->value;\n            if (n->flags & CLUSTER_MANAGER_FLAG_SLAVE) continue;\n            if (n->slots_count == 0) continue;\n            if (n->slots[i]) {\n                slots_map[i] = n;\n                break;\n            }\n        }\n    }\n    cmdfmt = sdsnew(\"MIGRATE %s %d %s %d %d\");\n    if (config.auth) {\n        if (config.user) {\n            cmdfmt = sdscatfmt(cmdfmt,\" AUTH2 %s %s\", config.user, config.auth); \n        } else {\n            cmdfmt = sdscatfmt(cmdfmt,\" AUTH %s\", config.auth);\n        }\n    }\n\n    if (config.cluster_manager_command.flags & CLUSTER_MANAGER_CMD_FLAG_COPY)\n        cmdfmt = sdscat(cmdfmt,\" COPY\");\n    if (config.cluster_manager_command.flags & CLUSTER_MANAGER_CMD_FLAG_REPLACE)\n        cmdfmt = sdscat(cmdfmt,\" REPLACE\");\n\n    /* Use SCAN to iterate over the keys, migrating to the\n     * right node as needed. */\n    int cursor = -999, timeout = config.cluster_manager_command.timeout;\n    while (cursor != 0) {\n        if (cursor < 0) cursor = 0;\n        freeReplyObject(src_reply);\n        src_reply = reconnectingRedisCommand(src_ctx, \"SCAN %d COUNT %d\",\n                                             cursor, 1000);\n        if (!src_reply || src_reply->type == REDIS_REPLY_ERROR) {\n            if (src_reply && src_reply->str) reply_err = src_reply->str;\n            success = 0;\n            goto cleanup;\n        }\n        assert(src_reply->type == REDIS_REPLY_ARRAY);\n        assert(src_reply->elements >= 2);\n        assert(src_reply->element[1]->type == REDIS_REPLY_ARRAY);\n        if (src_reply->element[0]->type == REDIS_REPLY_STRING)\n            cursor = atoi(src_reply->element[0]->str);\n        else if (src_reply->element[0]->type == REDIS_REPLY_INTEGER)\n            cursor = src_reply->element[0]->integer;\n        int keycount = src_reply->element[1]->elements;\n        for (i = 0; i < keycount; i++) {\n            redisReply *kr = src_reply->element[1]->element[i];\n            assert(kr->type == REDIS_REPLY_STRING);\n            char *key = kr->str;\n            uint16_t slot = clusterManagerKeyHashSlot(key, kr->len);\n            clusterManagerNode *target = slots_map[slot];\n            printf(\"Migrating %s to %s:%d: \", key, target->ip, target->port);\n            redisReply *r = reconnectingRedisCommand(src_ctx, cmdfmt,\n                                                     target->ip, target->port,\n                                                     key, 0, timeout);\n            if (!r || r->type == REDIS_REPLY_ERROR) {\n                if (r && r->str) {\n                    clusterManagerLogErr(\"Source %s:%d replied with \"\n                                         \"error:\\n%s\\n\", src_ip, src_port,\n                                         r->str);\n                }\n                success = 0;\n            }\n            freeReplyObject(r);\n            if (!success) goto cleanup;\n            clusterManagerLogOk(\"OK\\n\");\n        }\n    }\ncleanup:\n    if (reply_err)\n        clusterManagerLogErr(\"Source %s:%d replied with error:\\n%s\\n\",\n                             src_ip, src_port, reply_err);\n    if (src_ctx) redisFree(src_ctx);\n    if (src_reply) freeReplyObject(src_reply);\n    if (cmdfmt) sdsfree(cmdfmt);\n    return success;\ninvalid_args:\n    fprintf(stderr, \"%s\", invalid_args_msg);\n    return 0;\n}\n\nstatic int clusterManagerCommandCall(int argc, char **argv) {\n    int port = 0, i;\n    char *ip = NULL;\n    if (!getClusterHostFromCmdArgs(1, argv, &ip, &port)) goto invalid_args;\n    clusterManagerNode *refnode = clusterManagerNewNode(ip, port);\n    if (!clusterManagerLoadInfoFromNode(refnode, 0)) return 0;\n    argc--;\n    argv++;\n    size_t *argvlen = zmalloc(argc*sizeof(size_t), MALLOC_LOCAL);\n    clusterManagerLogInfo(\">>> Calling\");\n    for (i = 0; i < argc; i++) {\n        argvlen[i] = strlen(argv[i]);\n        printf(\" %s\", argv[i]);\n    }\n    printf(\"\\n\");\n    listIter li;\n    listNode *ln;\n    listRewind(cluster_manager.nodes, &li);\n    while ((ln = listNext(&li)) != NULL) {\n        clusterManagerNode *n = ln->value;\n        if ((config.cluster_manager_command.flags & CLUSTER_MANAGER_CMD_FLAG_MASTERS_ONLY)\n              && (n->replicate != NULL)) continue;  // continue if node is slave\n        if ((config.cluster_manager_command.flags & CLUSTER_MANAGER_CMD_FLAG_SLAVES_ONLY)\n              && (n->replicate == NULL)) continue;   // continue if node is master\n        if (!n->context && !clusterManagerNodeConnect(n)) continue;\n        redisReply *reply = NULL;\n        redisAppendCommandArgv(n->context, argc, (const char **) argv, argvlen);\n        int status = redisGetReply(n->context, (void **)(&reply));\n        if (status != REDIS_OK || reply == NULL )\n            printf(\"%s:%d: Failed!\\n\", n->ip, n->port);\n        else {\n            sds formatted_reply = cliFormatReplyRaw(reply);\n            printf(\"%s:%d: %s\\n\", n->ip, n->port, (char *) formatted_reply);\n            sdsfree(formatted_reply);\n        }\n        if (reply != NULL) freeReplyObject(reply);\n    }\n    zfree(argvlen);\n    return 1;\ninvalid_args:\n    fprintf(stderr, CLUSTER_MANAGER_INVALID_HOST_ARG);\n    return 0;\n}\n\nstatic int clusterManagerCommandBackup(int argc, char **argv) {\n    UNUSED(argc);\n    int success = 1, port = 0;\n    char *ip = NULL;\n    if (!getClusterHostFromCmdArgs(1, argv, &ip, &port)) goto invalid_args;\n    clusterManagerNode *refnode = clusterManagerNewNode(ip, port);\n    if (!clusterManagerLoadInfoFromNode(refnode, 0)) return 0;\n    int no_issues = clusterManagerCheckCluster(0);\n    int cluster_errors_count = (no_issues ? 0 :\n                                listLength(cluster_manager.errors));\n    config.cluster_manager_command.backup_dir = argv[1];\n    /* TODO: check if backup_dir is a valid directory. */\n    sds json = sdsnew(\"[\\n\");\n    int first_node = 0;\n    listIter li;\n    listNode *ln;\n    listRewind(cluster_manager.nodes, &li);\n    while ((ln = listNext(&li)) != NULL) {\n        if (!first_node) first_node = 1;\n        else json = sdscat(json, \",\\n\");\n        clusterManagerNode *node = ln->value;\n        sds node_json = clusterManagerNodeGetJSON(node, cluster_errors_count);\n        json = sdscat(json, node_json);\n        sdsfree(node_json);\n        if (node->replicate)\n            continue;\n        clusterManagerLogInfo(\">>> Node %s:%d -> Saving RDB...\\n\",\n                              node->ip, node->port);\n        fflush(stdout);\n        getRDB(node);\n    }\n    json = sdscat(json, \"\\n]\");\n    sds jsonpath = sdsnew(config.cluster_manager_command.backup_dir);\n    if (jsonpath[sdslen(jsonpath) - 1] != '/')\n        jsonpath = sdscat(jsonpath, \"/\");\n    jsonpath = sdscat(jsonpath, \"nodes.json\");\n    fflush(stdout);\n    clusterManagerLogInfo(\"Saving cluster configuration to: %s\\n\", jsonpath);\n    FILE *out = fopen(jsonpath, \"w+\");\n    if (!out) {\n        clusterManagerLogErr(\"Could not save nodes to: %s\\n\", jsonpath);\n        success = 0;\n        goto cleanup;\n    }\n    fputs(json, out);\n    fclose(out);\ncleanup:\n    sdsfree(json);\n    sdsfree(jsonpath);\n    if (success) {\n        if (!no_issues) {\n            clusterManagerLogWarn(\"*** Cluster seems to have some problems, \"\n                                  \"please be aware of it if you're going \"\n                                  \"to restore this backup.\\n\");\n        }\n        clusterManagerLogOk(\"[OK] Backup created into: %s\\n\",\n                            config.cluster_manager_command.backup_dir);\n    } else clusterManagerLogOk(\"[ERR] Failed to back cluster!\\n\");\n    return success;\ninvalid_args:\n    fprintf(stderr, CLUSTER_MANAGER_INVALID_HOST_ARG);\n    return 0;\n}\n\nstatic int clusterManagerCommandHelp(int argc, char **argv) {\n    UNUSED(argc);\n    UNUSED(argv);\n    int commands_count = sizeof(clusterManagerCommands) /\n                         sizeof(clusterManagerCommandDef);\n    int i = 0, j;\n    fprintf(stderr, \"Cluster Manager Commands:\\n\");\n    int padding = 15;\n    for (; i < commands_count; i++) {\n        clusterManagerCommandDef *def = &(clusterManagerCommands[i]);\n        int namelen = strlen(def->name), padlen = padding - namelen;\n        fprintf(stderr, \"  %s\", def->name);\n        for (j = 0; j < padlen; j++) fprintf(stderr, \" \");\n        fprintf(stderr, \"%s\\n\", (def->args ? def->args : \"\"));\n        if (def->options != NULL) {\n            int optslen = strlen(def->options);\n            char *p = def->options, *eos = p + optslen;\n            char *comma = NULL;\n            while ((comma = strchr(p, ',')) != NULL) {\n                int deflen = (int)(comma - p);\n                char buf[255];\n                memcpy(buf, p, deflen);\n                buf[deflen] = '\\0';\n                for (j = 0; j < padding; j++) fprintf(stderr, \" \");\n                fprintf(stderr, \"  --cluster-%s\\n\", buf);\n                p = comma + 1;\n                if (p >= eos) break;\n            }\n            if (p < eos) {\n                for (j = 0; j < padding; j++) fprintf(stderr, \" \");\n                fprintf(stderr, \"  --cluster-%s\\n\", p);\n            }\n        }\n    }\n    fprintf(stderr, \"\\nFor check, fix, reshard, del-node, set-timeout you \"\n                    \"can specify the host and port of any working node in \"\n                    \"the cluster.\\n\");\n\n    int options_count = sizeof(clusterManagerOptions) /\n                        sizeof(clusterManagerOptionDef);\n    i = 0;\n    fprintf(stderr, \"\\nCluster Manager Options:\\n\");\n    for (; i < options_count; i++) {\n        clusterManagerOptionDef *def = &(clusterManagerOptions[i]);\n        int namelen = strlen(def->name), padlen = padding - namelen;\n        fprintf(stderr, \"  %s\", def->name);\n        for (j = 0; j < padlen; j++) fprintf(stderr, \" \");\n        fprintf(stderr, \"%s\\n\", def->desc);\n    }\n\n    fprintf(stderr, \"\\n\");\n    return 0;\n}\n\n/*------------------------------------------------------------------------------\n * Latency and latency history modes\n *--------------------------------------------------------------------------- */\n\nstatic void latencyModePrint(long long min, long long max, double avg, long long count) {\n    if (config.output == OUTPUT_STANDARD) {\n        printf(\"min: %lld, max: %lld, avg: %.2f (%lld samples)\",\n                min, max, avg, count);\n        fflush(stdout);\n    } else if (config.output == OUTPUT_CSV) {\n        printf(\"%lld,%lld,%.2f,%lld\\n\", min, max, avg, count);\n    } else if (config.output == OUTPUT_RAW) {\n        printf(\"%lld %lld %.2f %lld\\n\", min, max, avg, count);\n    }\n}\n\n#define LATENCY_SAMPLE_RATE 10 /* milliseconds. */\n#define LATENCY_HISTORY_DEFAULT_INTERVAL 15000 /* milliseconds. */\nstatic void latencyMode(void) {\n    redisReply *reply;\n    long long start, latency, min = 0, max = 0, tot = 0, count = 0;\n    long long history_interval =\n        config.interval ? config.interval/1000 :\n                          LATENCY_HISTORY_DEFAULT_INTERVAL;\n    double avg;\n    long long history_start = mstime();\n\n    /* Set a default for the interval in case of --latency option\n     * with --raw, --csv or when it is redirected to non tty. */\n    if (config.interval == 0) {\n        config.interval = 1000;\n    } else {\n        config.interval /= 1000; /* We need to convert to milliseconds. */\n    }\n\n    if (!context) exit(1);\n    while(1) {\n        start = mstime();\n        reply = reconnectingRedisCommand(context,\"PING\");\n        if (reply == NULL) {\n            fprintf(stderr,\"\\nI/O error\\n\");\n            exit(1);\n        }\n        latency = mstime()-start;\n        freeReplyObject(reply);\n        count++;\n        if (count == 1) {\n            min = max = tot = latency;\n            avg = (double) latency;\n        } else {\n            if (latency < min) min = latency;\n            if (latency > max) max = latency;\n            tot += latency;\n            avg = (double) tot/count;\n        }\n\n        if (config.output == OUTPUT_STANDARD) {\n            printf(\"\\x1b[0G\\x1b[2K\"); /* Clear the line. */\n            latencyModePrint(min,max,avg,count);\n        } else {\n            if (config.latency_history) {\n                latencyModePrint(min,max,avg,count);\n            } else if (mstime()-history_start > config.interval) {\n                latencyModePrint(min,max,avg,count);\n                exit(0);\n            }\n        }\n\n        if (config.latency_history && mstime()-history_start > history_interval)\n        {\n            printf(\" -- %.2f seconds range\\n\", (float)(mstime()-history_start)/1000);\n            history_start = mstime();\n            min = max = tot = count = 0;\n        }\n        usleep(LATENCY_SAMPLE_RATE * 1000);\n    }\n}\n\n/*------------------------------------------------------------------------------\n * Latency distribution mode -- requires 256 colors xterm\n *--------------------------------------------------------------------------- */\n\n#define LATENCY_DIST_DEFAULT_INTERVAL 1000 /* milliseconds. */\n\n/* Structure to store samples distribution. */\nstruct distsamples {\n    long long max;   /* Max latency to fit into this interval (usec). */\n    long long count; /* Number of samples in this interval. */\n    int character;   /* Associated character in visualization. */\n};\n\n/* Helper function for latencyDistMode(). Performs the spectrum visualization\n * of the collected samples targeting an xterm 256 terminal.\n *\n * Takes an array of distsamples structures, ordered from smaller to bigger\n * 'max' value. Last sample max must be 0, to mean that it olds all the\n * samples greater than the previous one, and is also the stop sentinel.\n *\n * \"tot' is the total number of samples in the different buckets, so it\n * is the SUM(samples[i].count) for i to 0 up to the max sample.\n *\n * As a side effect the function sets all the buckets count to 0. */\nvoid showLatencyDistSamples(struct distsamples *samples, long long tot) {\n    int j;\n\n     /* We convert samples into an index inside the palette\n     * proportional to the percentage a given bucket represents.\n     * This way intensity of the different parts of the spectrum\n     * don't change relative to the number of requests, which avoids to\n     * pollute the visualization with non-latency related info. */\n    printf(\"\\033[38;5;0m\"); /* Set foreground color to black. */\n    for (j = 0; ; j++) {\n        int coloridx =\n            ceil((double) samples[j].count / tot * (spectrum_palette_size-1));\n        int color = spectrum_palette[coloridx];\n        printf(\"\\033[48;5;%dm%c\", (int)color, samples[j].character);\n        samples[j].count = 0;\n        if (samples[j].max == 0) break; /* Last sample. */\n    }\n    printf(\"\\033[0m\\n\");\n    fflush(stdout);\n}\n\n/* Show the legend: different buckets values and colors meaning, so\n * that the spectrum is more easily readable. */\nvoid showLatencyDistLegend(void) {\n    int j;\n\n    printf(\"---------------------------------------------\\n\");\n    printf(\". - * #          .01 .125 .25 .5 milliseconds\\n\");\n    printf(\"1,2,3,...,9      from 1 to 9     milliseconds\\n\");\n    printf(\"A,B,C,D,E        10,20,30,40,50  milliseconds\\n\");\n    printf(\"F,G,H,I,J        .1,.2,.3,.4,.5       seconds\\n\");\n    printf(\"K,L,M,N,O,P,Q,?  1,2,4,8,16,30,60,>60 seconds\\n\");\n    printf(\"From 0 to 100%%: \");\n    for (j = 0; j < spectrum_palette_size; j++) {\n        printf(\"\\033[48;5;%dm \", spectrum_palette[j]);\n    }\n    printf(\"\\033[0m\\n\");\n    printf(\"---------------------------------------------\\n\");\n}\n\nstatic void latencyDistMode(void) {\n    redisReply *reply;\n    long long start, latency, count = 0;\n    long long history_interval =\n        config.interval ? config.interval/1000 :\n                          LATENCY_DIST_DEFAULT_INTERVAL;\n    long long history_start = ustime();\n    int j, outputs = 0;\n\n    struct distsamples samples[] = {\n        /* We use a mostly logarithmic scale, with certain linear intervals\n         * which are more interesting than others, like 1-10 milliseconds\n         * range. */\n        {10,0,'.'},         /* 0.01 ms */\n        {125,0,'-'},        /* 0.125 ms */\n        {250,0,'*'},        /* 0.25 ms */\n        {500,0,'#'},        /* 0.5 ms */\n        {1000,0,'1'},       /* 1 ms */\n        {2000,0,'2'},       /* 2 ms */\n        {3000,0,'3'},       /* 3 ms */\n        {4000,0,'4'},       /* 4 ms */\n        {5000,0,'5'},       /* 5 ms */\n        {6000,0,'6'},       /* 6 ms */\n        {7000,0,'7'},       /* 7 ms */\n        {8000,0,'8'},       /* 8 ms */\n        {9000,0,'9'},       /* 9 ms */\n        {10000,0,'A'},      /* 10 ms */\n        {20000,0,'B'},      /* 20 ms */\n        {30000,0,'C'},      /* 30 ms */\n        {40000,0,'D'},      /* 40 ms */\n        {50000,0,'E'},      /* 50 ms */\n        {100000,0,'F'},     /* 0.1 s */\n        {200000,0,'G'},     /* 0.2 s */\n        {300000,0,'H'},     /* 0.3 s */\n        {400000,0,'I'},     /* 0.4 s */\n        {500000,0,'J'},     /* 0.5 s */\n        {1000000,0,'K'},    /* 1 s */\n        {2000000,0,'L'},    /* 2 s */\n        {4000000,0,'M'},    /* 4 s */\n        {8000000,0,'N'},    /* 8 s */\n        {16000000,0,'O'},   /* 16 s */\n        {30000000,0,'P'},   /* 30 s */\n        {60000000,0,'Q'},   /* 1 minute */\n        {0,0,'?'},          /* > 1 minute */\n    };\n\n    if (!context) exit(1);\n    while(1) {\n        start = ustime();\n        reply = reconnectingRedisCommand(context,\"PING\");\n        if (reply == NULL) {\n            fprintf(stderr,\"\\nI/O error\\n\");\n            exit(1);\n        }\n        latency = ustime()-start;\n        freeReplyObject(reply);\n        count++;\n\n        /* Populate the relevant bucket. */\n        for (j = 0; ; j++) {\n            if (samples[j].max == 0 || latency <= samples[j].max) {\n                samples[j].count++;\n                break;\n            }\n        }\n\n        /* From time to time show the spectrum. */\n        if (count && (ustime()-history_start)/1000 > history_interval) {\n            if ((outputs++ % 20) == 0)\n                showLatencyDistLegend();\n            showLatencyDistSamples(samples,count);\n            history_start = ustime();\n            count = 0;\n        }\n        usleep(LATENCY_SAMPLE_RATE * 1000);\n    }\n}\n\n/*------------------------------------------------------------------------------\n * Slave mode\n *--------------------------------------------------------------------------- */\n\n#define RDB_EOF_MARK_SIZE 40\n\nvoid sendReplconf(const char* arg1, const char* arg2) {\n    fprintf(stderr, \"sending REPLCONF %s %s\\n\", arg1, arg2);\n    redisReply *reply = redisCommand(context, \"REPLCONF %s %s\", arg1, arg2);\n\n    /* Handle any error conditions */\n    if(reply == NULL) {\n        fprintf(stderr, \"\\nI/O error\\n\");\n        exit(1);\n    } else if(reply->type == REDIS_REPLY_ERROR) {\n        fprintf(stderr, \"REPLCONF %s error: %s\\n\", arg1, reply->str);\n        /* non fatal, old versions may not support it */\n    }\n    freeReplyObject(reply);\n}\n\nvoid sendCapa() {\n    sendReplconf(\"capa\", \"eof\");\n}\n\nvoid sendRdbOnly(void) {\n    sendReplconf(\"rdb-only\", \"1\");\n}\n\n/* Read raw bytes through a redisContext. The read operation is not greedy\n * and may not fill the buffer entirely.\n */\nstatic ssize_t readConn(redisContext *c, char *buf, size_t len)\n{\n    return c->funcs->read(c, buf, len);\n}\n\n/* Sends SYNC and reads the number of bytes in the payload. Used both by\n * slaveMode() and getRDB().\n * returns 0 in case an EOF marker is used. */\nunsigned long long sendSync(redisContext *c, char *out_eof) {\n    /* To start we need to send the SYNC command and return the payload.\n     * The hiredis client lib does not understand this part of the protocol\n     * and we don't want to mess with its buffers, so everything is performed\n     * using direct low-level I/O. */\n    char buf[4096], *p;\n    ssize_t nread;\n\n    /* Send the SYNC command. */\n    if (cliWriteConn(c, \"SYNC\\r\\n\", 6) != 6) {\n        fprintf(stderr,\"Error writing to master\\n\");\n        exit(1);\n    }\n\n    /* Read $<payload>\\r\\n, making sure to read just up to \"\\n\" */\n    p = buf;\n    while(1) {\n        nread = readConn(c,p,1);\n        if (nread <= 0) {\n            fprintf(stderr,\"Error reading bulk length while SYNCing\\n\");\n            exit(1);\n        }\n        if (*p == '\\n' && p != buf) break;\n        if (*p != '\\n') p++;\n    }\n    *p = '\\0';\n    if (buf[0] == '-') {\n        fprintf(stderr, \"SYNC with master failed: %s\\n\", buf);\n        exit(1);\n    }\n    if (strncmp(buf+1,\"EOF:\",4) == 0 && strlen(buf+5) >= RDB_EOF_MARK_SIZE) {\n        memcpy(out_eof, buf+5, RDB_EOF_MARK_SIZE);\n        return 0;\n    }\n    return strtoull(buf+1,NULL,10);\n}\n\nstatic void slaveMode(void) {\n    static char eofmark[RDB_EOF_MARK_SIZE];\n    static char lastbytes[RDB_EOF_MARK_SIZE];\n    static int usemark = 0;\n    unsigned long long payload = sendSync(context,eofmark);\n    char buf[1024];\n    int original_output = config.output;\n\n    if (payload == 0) {\n        payload = ULLONG_MAX;\n        memset(lastbytes,0,RDB_EOF_MARK_SIZE);\n        usemark = 1;\n        fprintf(stderr,\"SYNC with master, discarding \"\n                       \"bytes of bulk transfer until EOF marker...\\n\");\n    } else {\n        fprintf(stderr,\"SYNC with master, discarding %llu \"\n                       \"bytes of bulk transfer...\\n\", payload);\n    }\n\n\n    /* Discard the payload. */\n    while(payload) {\n        ssize_t nread;\n\n        nread = readConn(context,buf,(payload > sizeof(buf)) ? sizeof(buf) : payload);\n        if (nread <= 0) {\n            fprintf(stderr,\"Error reading RDB payload while SYNCing\\n\");\n            exit(1);\n        }\n        payload -= nread;\n\n        if (usemark) {\n            /* Update the last bytes array, and check if it matches our delimiter.*/\n            if (nread >= RDB_EOF_MARK_SIZE) {\n                memcpy(lastbytes,buf+nread-RDB_EOF_MARK_SIZE,RDB_EOF_MARK_SIZE);\n            } else {\n                int rem = RDB_EOF_MARK_SIZE-nread;\n                memmove(lastbytes,lastbytes+nread,rem);\n                memcpy(lastbytes+rem,buf,nread);\n            }\n            if (memcmp(lastbytes,eofmark,RDB_EOF_MARK_SIZE) == 0)\n                break;\n        }\n    }\n\n    if (usemark) {\n        unsigned long long offset = ULLONG_MAX - payload;\n        fprintf(stderr,\"SYNC done after %llu bytes. Logging commands from master.\\n\", offset);\n        /* put the slave online */\n        sleep(1);\n        sendReplconf(\"ACK\", \"0\");\n    } else\n        fprintf(stderr,\"SYNC done. Logging commands from master.\\n\");\n\n    /* Now we can use hiredis to read the incoming protocol. */\n    config.output = OUTPUT_CSV;\n    while (cliReadReply(0) == REDIS_OK);\n    config.output = original_output;\n}\n\n/*------------------------------------------------------------------------------\n * RDB transfer mode\n *--------------------------------------------------------------------------- */\n\n/* This function implements --rdb, so it uses the replication protocol in order\n * to fetch the RDB file from a remote server. */\nstatic void getRDB(clusterManagerNode *node) {\n    int fd;\n    redisContext *s;\n    char *filename;\n    if (node != NULL) {\n        assert(node->context);\n        s = node->context;\n        filename = clusterManagerGetNodeRDBFilename(node);\n    } else {\n        s = context;\n        filename = config.rdb_filename;\n    }\n    static char eofmark[RDB_EOF_MARK_SIZE];\n    static char lastbytes[RDB_EOF_MARK_SIZE];\n    static int usemark = 0;\n    unsigned long long payload = sendSync(s, eofmark);\n    char buf[4096];\n\n    if (payload == 0) {\n        payload = ULLONG_MAX;\n        memset(lastbytes,0,RDB_EOF_MARK_SIZE);\n        usemark = 1;\n        fprintf(stderr,\"SYNC sent to master, writing bytes of bulk transfer \"\n                \"until EOF marker to '%s'\\n\", filename);\n    } else {\n        fprintf(stderr,\"SYNC sent to master, writing %llu bytes to '%s'\\n\",\n            payload, filename);\n    }\n\n    int write_to_stdout = !strcmp(filename,\"-\");\n    /* Write to file. */\n    if (write_to_stdout) {\n        fd = STDOUT_FILENO;\n    } else {\n        fd = open(filename, O_CREAT|O_WRONLY, 0644);\n        if (fd == -1) {\n            fprintf(stderr, \"Error opening '%s': %s\\n\", filename,\n                strerror(errno));\n            exit(1);\n        }\n    }\n\n    while(payload) {\n        ssize_t nread, nwritten;\n\n        nread = readConn(s,buf,(payload > sizeof(buf)) ? sizeof(buf) : payload);\n        if (nread <= 0) {\n            fprintf(stderr,\"I/O Error reading RDB payload from socket\\n\");\n            exit(1);\n        }\n        nwritten = write(fd, buf, nread);\n        if (nwritten != nread) {\n            fprintf(stderr,\"Error writing data to file: %s\\n\",\n                (nwritten == -1) ? strerror(errno) : \"short write\");\n            exit(1);\n        }\n        payload -= nread;\n\n        if (usemark) {\n            /* Update the last bytes array, and check if it matches our delimiter.*/\n            if (nread >= RDB_EOF_MARK_SIZE) {\n                memcpy(lastbytes,buf+nread-RDB_EOF_MARK_SIZE,RDB_EOF_MARK_SIZE);\n            } else {\n                int rem = RDB_EOF_MARK_SIZE-nread;\n                memmove(lastbytes,lastbytes+nread,rem);\n                memcpy(lastbytes+rem,buf,nread);\n            }\n            if (memcmp(lastbytes,eofmark,RDB_EOF_MARK_SIZE) == 0)\n                break;\n        }\n    }\n    if (usemark) {\n        payload = ULLONG_MAX - payload - RDB_EOF_MARK_SIZE;\n        if (!write_to_stdout && ftruncate(fd, payload) == -1)\n            fprintf(stderr,\"ftruncate failed: %s.\\n\", strerror(errno));\n        fprintf(stderr,\"Transfer finished with success after %llu bytes\\n\", payload);\n    } else {\n        fprintf(stderr,\"Transfer finished with success.\\n\");\n    }\n    redisFree(s); /* Close the connection ASAP as fsync() may take time. */\n    if (node)\n        node->context = NULL;\n    if (!write_to_stdout && fsync(fd) == -1) {\n        fprintf(stderr,\"Fail to fsync '%s': %s\\n\", filename, strerror(errno));\n        exit(1);\n    }\n    close(fd);\n    if (node) {\n        sdsfree(filename);\n        return;\n    }\n    exit(0);\n}\n\n/*------------------------------------------------------------------------------\n * Bulk import (pipe) mode\n *--------------------------------------------------------------------------- */\n\n#define PIPEMODE_WRITE_LOOP_MAX_BYTES (128*1024)\nstatic void pipeMode(void) {\n    long long errors = 0, replies = 0, obuf_len = 0, obuf_pos = 0;\n    char obuf[1024*16]; /* Output buffer */\n    char aneterr[ANET_ERR_LEN];\n    redisReply *reply;\n    int eof = 0; /* True once we consumed all the standard input. */\n    int done = 0;\n    char magic[20]; /* Special reply we recognize. */\n    time_t last_read_time = time(NULL);\n\n    srand(time(NULL));\n\n    /* Use non blocking I/O. */\n    if (anetNonBlock(aneterr,context->fd) == ANET_ERR) {\n        fprintf(stderr, \"Can't set the socket in non blocking mode: %s\\n\",\n            aneterr);\n        exit(1);\n    }\n\n    context->flags &= ~REDIS_BLOCK;\n\n    /* Transfer raw protocol and read replies from the server at the same\n     * time. */\n    while(!done) {\n        int mask = AE_READABLE;\n\n        if (!eof || obuf_len != 0) mask |= AE_WRITABLE;\n        mask = aeWait(context->fd,mask,1000);\n\n        /* Handle the readable state: we can read replies from the server. */\n        if (mask & AE_READABLE) {\n            int read_error = 0;\n\n            do {\n                if (!read_error && redisBufferRead(context) == REDIS_ERR) {\n                    read_error = 1;\n                }\n\n                reply = NULL;\n                if (redisGetReply(context, (void **) &reply) == REDIS_ERR) {\n                    fprintf(stderr, \"Error reading replies from server\\n\");\n                    exit(1);\n                }\n                if (reply) {\n                    last_read_time = time(NULL);\n                    if (reply->type == REDIS_REPLY_ERROR) {\n                        fprintf(stderr,\"%s\\n\", reply->str);\n                        errors++;\n                    } else if (eof && reply->type == REDIS_REPLY_STRING &&\n                                      reply->len == 20) {\n                        /* Check if this is the reply to our final ECHO\n                         * command. If so everything was received\n                         * from the server. */\n                        if (memcmp(reply->str,magic,20) == 0) {\n                            printf(\"Last reply received from server.\\n\");\n                            done = 1;\n                            replies--;\n                        }\n                    }\n                    replies++;\n                    freeReplyObject(reply);\n                }\n            } while(reply);\n\n            /* Abort on read errors. We abort here because it is important\n             * to consume replies even after a read error: this way we can\n             * show a potential problem to the user. */\n            if (read_error) exit(1);\n        }\n\n        /* Handle the writable state: we can send protocol to the server. */\n        if (mask & AE_WRITABLE) {\n            ssize_t loop_nwritten = 0;\n\n            while(1) {\n                /* Transfer current buffer to server. */\n                if (obuf_len != 0) {\n                    ssize_t nwritten = cliWriteConn(context,obuf+obuf_pos,obuf_len);\n\n                    if (nwritten == -1) {\n                        if (errno != EAGAIN && errno != EINTR) {\n                            fprintf(stderr, \"Error writing to the server: %s\\n\",\n                                strerror(errno));\n                            exit(1);\n                        } else {\n                            nwritten = 0;\n                        }\n                    }\n                    obuf_len -= nwritten;\n                    obuf_pos += nwritten;\n                    loop_nwritten += nwritten;\n                    if (obuf_len != 0) break; /* Can't accept more data. */\n                }\n                if (context->err) {\n                    fprintf(stderr, \"Server I/O Error: %s\\n\", context->errstr);\n                    exit(1);\n                }\n                /* If buffer is empty, load from stdin. */\n                if (obuf_len == 0 && !eof) {\n                    ssize_t nread = read(STDIN_FILENO,obuf,sizeof(obuf));\n\n                    if (nread == 0) {\n                        /* The ECHO sequence starts with a \"\\r\\n\" so that if there\n                         * is garbage in the protocol we read from stdin, the ECHO\n                         * will likely still be properly formatted.\n                         * CRLF is ignored by Redis, so it has no effects. */\n                        char echo[] =\n                        \"\\r\\n*2\\r\\n$4\\r\\nECHO\\r\\n$20\\r\\n01234567890123456789\\r\\n\";\n                        int j;\n\n                        eof = 1;\n                        /* Everything transferred, so we queue a special\n                         * ECHO command that we can match in the replies\n                         * to make sure everything was read from the server. */\n                        for (j = 0; j < 20; j++)\n                            magic[j] = rand() & 0xff;\n                        memcpy(echo+21,magic,20);\n                        memcpy(obuf,echo,sizeof(echo)-1);\n                        obuf_len = sizeof(echo)-1;\n                        obuf_pos = 0;\n                        printf(\"All data transferred. Waiting for the last reply...\\n\");\n                    } else if (nread == -1) {\n                        fprintf(stderr, \"Error reading from stdin: %s\\n\",\n                            strerror(errno));\n                        exit(1);\n                    } else {\n                        obuf_len = nread;\n                        obuf_pos = 0;\n                    }\n                }\n                if ((obuf_len == 0 && eof) ||\n                    loop_nwritten > PIPEMODE_WRITE_LOOP_MAX_BYTES) break;\n            }\n        }\n\n        /* Handle timeout, that is, we reached EOF, and we are not getting\n         * replies from the server for a few seconds, nor the final ECHO is\n         * received. */\n        if (eof && config.pipe_timeout > 0 &&\n            time(NULL)-last_read_time > config.pipe_timeout)\n        {\n            fprintf(stderr,\"No replies for %d seconds: exiting.\\n\",\n                config.pipe_timeout);\n            errors++;\n            break;\n        }\n    }\n    printf(\"errors: %lld, replies: %lld\\n\", errors, replies);\n    if (errors)\n        exit(1);\n    else\n        exit(0);\n}\n\n/*------------------------------------------------------------------------------\n * Find big keys\n *--------------------------------------------------------------------------- */\n\nredisReply *sendScan(unsigned long long *it) {\n    redisReply *reply;\n\n    if (config.pattern)\n        reply = redisCommand(context, \"SCAN %llu MATCH %b\",\n            *it, config.pattern, sdslen(config.pattern));\n    else\n        reply = redisCommand(context,\"SCAN %llu\",*it);\n\n    /* Handle any error conditions */\n    if(reply == NULL) {\n        fprintf(stderr, \"\\nI/O error\\n\");\n        exit(1);\n    } else if(reply->type == REDIS_REPLY_ERROR) {\n        fprintf(stderr, \"SCAN error: %s\\n\", reply->str);\n        exit(1);\n    } else if(reply->type != REDIS_REPLY_ARRAY) {\n        fprintf(stderr, \"Non ARRAY response from SCAN!\\n\");\n        exit(1);\n    } else if(reply->elements != 2) {\n        fprintf(stderr, \"Invalid element count from SCAN!\\n\");\n        exit(1);\n    }\n\n    /* Validate our types are correct */\n    assert(reply->element[0]->type == REDIS_REPLY_STRING);\n    assert(reply->element[1]->type == REDIS_REPLY_ARRAY);\n\n    /* Update iterator */\n    *it = strtoull(reply->element[0]->str, NULL, 10);\n\n    return reply;\n}\n\nint getDbSize(void) {\n    redisReply *reply;\n    int size;\n\n    reply = redisCommand(context, \"DBSIZE\");\n\n    if (reply == NULL) {\n        fprintf(stderr, \"\\nI/O error\\n\");\n        exit(1);\n    } else if (reply->type == REDIS_REPLY_ERROR) {\n        fprintf(stderr, \"Couldn't determine DBSIZE: %s\\n\", reply->str);\n        exit(1);\n    } else if (reply->type != REDIS_REPLY_INTEGER) {\n        fprintf(stderr, \"Non INTEGER response from DBSIZE!\\n\");\n        exit(1);\n    }\n\n    /* Grab the number of keys and free our reply */\n    size = reply->integer;\n    freeReplyObject(reply);\n\n    return size;\n}\n\ntypeinfo type_string = { \"string\", \"STRLEN\", \"bytes\" };\ntypeinfo type_list = { \"list\", \"LLEN\", \"items\" };\ntypeinfo type_set = { \"set\", \"SCARD\", \"members\" };\ntypeinfo type_hash = { \"hash\", \"HLEN\", \"fields\" };\ntypeinfo type_zset = { \"zset\", \"ZCARD\", \"members\" };\ntypeinfo type_stream = { \"stream\", \"XLEN\", \"entries\" };\ntypeinfo type_other = { \"other\", NULL, \"?\" };\n\nvoid type_free(void* priv_data, void* val) {\n    typeinfo *info = val;\n    UNUSED(priv_data);\n    if (info->biggest_key)\n        sdsfree(info->biggest_key);\n    sdsfree(info->name);\n    zfree(info);\n}\n\nvoid getKeySizes(redisReply *keys, typeinfo **types,\n                        unsigned long long *sizes, int memkeys,\n                        unsigned memkeys_samples)\n{\n    redisReply *reply;\n    unsigned int i;\n\n    /* Pipeline size commands */\n    for(i=0;i<keys->elements;i++) {\n        /* Skip keys that disappeared between SCAN and TYPE (or unknown types when not in memkeys mode) */\n        if(!types[i] || (!types[i]->sizecmd && !memkeys))\n            continue;\n\n        if (!memkeys) {\n            const char* argv[] = {types[i]->sizecmd, keys->element[i]->str};\n            size_t lens[] = {strlen(types[i]->sizecmd), keys->element[i]->len};\n            redisAppendCommandArgv(context, 2, argv, lens);\n        } else if (memkeys_samples==0) {\n            const char* argv[] = {\"MEMORY\", \"USAGE\", keys->element[i]->str};\n            size_t lens[] = {6, 5, keys->element[i]->len};\n            redisAppendCommandArgv(context, 3, argv, lens);\n        } else {\n            sds samplesstr = sdsfromlonglong(memkeys_samples);\n            const char* argv[] = {\"MEMORY\", \"USAGE\", keys->element[i]->str, \"SAMPLES\", samplesstr};\n            size_t lens[] = {6, 5, keys->element[i]->len, 7, sdslen(samplesstr)};\n            redisAppendCommandArgv(context, 5, argv, lens);\n            sdsfree(samplesstr);\n        }\n    }\n\n    /* Retrieve sizes */\n    for(i=0;i<keys->elements;i++) {\n        /* Skip keys that disappeared between SCAN and TYPE (or unknown types when not in memkeys mode) */\n        if(!types[i] || (!types[i]->sizecmd && !memkeys)) {\n            sizes[i] = 0;\n            continue;\n        }\n\n        /* Retrieve size */\n        if(redisGetReply(context, (void**)&reply)!=REDIS_OK) {\n            fprintf(stderr, \"Error getting size for key '%s' (%d: %s)\\n\",\n                keys->element[i]->str, context->err, context->errstr);\n            exit(1);\n        } else if(reply->type != REDIS_REPLY_INTEGER) {\n            /* Theoretically the key could have been removed and\n             * added as a different type between TYPE and SIZE */\n            fprintf(stderr,\n                \"Warning:  %s on '%s' failed (may have changed type)\\n\",\n                !memkeys? types[i]->sizecmd: \"MEMORY USAGE\",\n                keys->element[i]->str);\n            sizes[i] = 0;\n        } else {\n            sizes[i] = reply->integer;\n        }\n\n        freeReplyObject(reply);\n    }\n}\n\nstatic void getKeyFreqs(redisReply *keys, unsigned long long *freqs) {\n    redisReply *reply;\n    unsigned int i;\n\n    /* Pipeline OBJECT freq commands */\n    for(i=0;i<keys->elements;i++) {\n        const char* argv[] = {\"OBJECT\", \"FREQ\", keys->element[i]->str};\n        size_t lens[] = {6, 4, keys->element[i]->len};\n        redisAppendCommandArgv(context, 3, argv, lens);\n    }\n\n    /* Retrieve freqs */\n    for(i=0;i<keys->elements;i++) {\n        if(redisGetReply(context, (void**)&reply)!=REDIS_OK) {\n            sds keyname = sdscatrepr(sdsempty(), keys->element[i]->str, keys->element[i]->len);\n            fprintf(stderr, \"Error getting freq for key '%s' (%d: %s)\\n\",\n                keyname, context->err, context->errstr);\n            sdsfree(keyname);\n            exit(1);\n        } else if(reply->type != REDIS_REPLY_INTEGER) {\n            if(reply->type == REDIS_REPLY_ERROR) {\n                fprintf(stderr, \"Error: %s\\n\", reply->str);\n                exit(1);\n            } else {\n                sds keyname = sdscatrepr(sdsempty(), keys->element[i]->str, keys->element[i]->len);\n                fprintf(stderr, \"Warning: OBJECT freq on '%s' failed (may have been deleted)\\n\", keyname);\n                sdsfree(keyname);\n                freqs[i] = 0;\n            }\n        } else {\n            freqs[i] = reply->integer;\n        }\n        freeReplyObject(reply);\n    }\n}\n\n#define HOTKEYS_SAMPLE 16\nstatic void findHotKeys(void) {\n    redisReply *keys, *reply;\n    unsigned long long counters[HOTKEYS_SAMPLE] = {0};\n    sds hotkeys[HOTKEYS_SAMPLE] = {NULL};\n    unsigned long long sampled = 0, total_keys, *freqs = NULL, it = 0;\n    unsigned int arrsize = 0, i, k;\n    double pct;\n\n    /* Total keys pre scanning */\n    total_keys = getDbSize();\n\n    /* Status message */\n    printf(\"\\n# Scanning the entire keyspace to find hot keys as well as\\n\");\n    printf(\"# average sizes per key type.  You can use -i 0.1 to sleep 0.1 sec\\n\");\n    printf(\"# per 100 SCAN commands (not usually needed).\\n\\n\");\n\n    /* SCAN loop */\n    do {\n        /* Calculate approximate percentage completion */\n        pct = 100 * (double)sampled/total_keys;\n\n        /* Grab some keys and point to the keys array */\n        reply = sendScan(&it);\n        keys  = reply->element[1];\n\n        /* Reallocate our freqs array if we need to */\n        if(keys->elements > arrsize) {\n            freqs = zrealloc(freqs, sizeof(unsigned long long)*keys->elements, MALLOC_LOCAL);\n\n            if(!freqs) {\n                fprintf(stderr, \"Failed to allocate storage for keys!\\n\");\n                exit(1);\n            }\n\n            arrsize = keys->elements;\n        }\n\n        getKeyFreqs(keys, freqs);\n\n        /* Now update our stats */\n        for(i=0;i<keys->elements;i++) {\n            sampled++;\n            /* Update overall progress */\n            if(sampled % 1000000 == 0) {\n                printf(\"[%05.2f%%] Sampled %llu keys so far\\n\", pct, sampled);\n            }\n\n            /* Use eviction pool here */\n            k = 0;\n            while (k < HOTKEYS_SAMPLE && freqs[i] > counters[k]) k++;\n            if (k == 0) continue;\n            k--;\n            if (k == 0 || counters[k] == 0) {\n                sdsfree(hotkeys[k]);\n            } else {\n                sdsfree(hotkeys[0]);\n                memmove(counters,counters+1,sizeof(counters[0])*k);\n                memmove(hotkeys,hotkeys+1,sizeof(hotkeys[0])*k);\n            }\n            counters[k] = freqs[i];\n            hotkeys[k] = sdscatrepr(sdsempty(), keys->element[i]->str, keys->element[i]->len);\n            printf(\n               \"[%05.2f%%] Hot key '%s' found so far with counter %llu\\n\",\n               pct, hotkeys[k], freqs[i]);\n        }\n\n        /* Sleep if we've been directed to do so */\n        if(sampled && (sampled %100) == 0 && config.interval) {\n            usleep(config.interval);\n        }\n\n        freeReplyObject(reply);\n    } while(it != 0);\n\n    if (freqs) zfree(freqs);\n\n    /* We're done */\n    printf(\"\\n-------- summary -------\\n\\n\");\n\n    printf(\"Sampled %llu keys in the keyspace!\\n\", sampled);\n\n    for (i=1; i<= HOTKEYS_SAMPLE; i++) {\n        k = HOTKEYS_SAMPLE - i;\n        if(counters[k]>0) {\n            printf(\"hot key found with counter: %llu\\tkeyname: %s\\n\", counters[k], hotkeys[k]);\n            sdsfree(hotkeys[k]);\n        }\n    }\n\n    exit(0);\n}\n\n/*------------------------------------------------------------------------------\n * Stats mode\n *--------------------------------------------------------------------------- */\n\n/* Return the specified INFO field from the INFO command output \"info\".\n * A new buffer is allocated for the result, that needs to be free'd.\n * If the field is not found NULL is returned. */\nstatic char *getInfoField(char *info, char *field) {\n    char *p = strstr(info,field);\n    char *n1, *n2;\n    char *result;\n\n    if (!p) return NULL;\n    p += strlen(field)+1;\n    n1 = strchr(p,'\\r');\n    n2 = strchr(p,',');\n    if (n2 && n2 < n1) n1 = n2;\n    result = zmalloc(sizeof(char)*(n1-p)+1, MALLOC_LOCAL);\n    memcpy(result,p,(n1-p));\n    result[n1-p] = '\\0';\n    return result;\n}\n\n/* Like the above function but automatically convert the result into\n * a long. On error (missing field) LONG_MIN is returned. */\nstatic long getLongInfoField(char *info, char *field) {\n    char *value = getInfoField(info,field);\n    long l;\n\n    if (!value) return LONG_MIN;\n    l = strtol(value,NULL,10);\n    zfree(value);\n    return l;\n}\n\n/* Convert number of bytes into a human readable string of the form:\n * 100B, 2G, 100M, 4K, and so forth. */\nvoid bytesToHuman(char *s, long long n, size_t bufsize) {\n    double d;\n\n    if (n < 0) {\n        *s = '-';\n        s++;\n        n = -n;\n    }\n    if (n < 1024) {\n        /* Bytes */\n        snprintf(s,bufsize,\"%lldB\",n);\n        return;\n    } else if (n < (1024*1024)) {\n        d = (double)n/(1024);\n        snprintf(s,bufsize,\"%.2fK\",d);\n    } else if (n < (1024LL*1024*1024)) {\n        d = (double)n/(1024*1024);\n        snprintf(s,bufsize,\"%.2fM\",d);\n    } else if (n < (1024LL*1024*1024*1024)) {\n        d = (double)n/(1024LL*1024*1024);\n        snprintf(s,bufsize,\"%.2fG\",d);\n    }\n}\n\nstatic void statMode(void) {\n    redisReply *reply;\n    long aux, requests = 0;\n    int i = 0;\n\n    while(1) {\n        char buf[64];\n        int j;\n\n        reply = reconnectingRedisCommand(context,\"INFO\");\n        if (reply->type == REDIS_REPLY_ERROR) {\n            printf(\"ERROR: %s\\n\", reply->str);\n            exit(1);\n        }\n\n        if ((i++ % 20) == 0) {\n            printf(\n\"------- data ------ --------------------- load -------------------- - child -\\n\"\n\"keys       mem      clients blocked requests            connections          \\n\");\n        }\n\n        /* Keys */\n        aux = 0;\n        for (j = 0; j < 20; j++) {\n            long k;\n\n            snprintf(buf,sizeof(buf),\"db%d:keys\",j);\n            k = getLongInfoField(reply->str,buf);\n            if (k == LONG_MIN) continue;\n            aux += k;\n        }\n        snprintf(buf,sizeof(buf),\"%ld\",aux);\n        printf(\"%-11s\",buf);\n\n        /* Used memory */\n        aux = getLongInfoField(reply->str,\"used_memory\");\n        bytesToHuman(buf,aux,sizeof(buf));\n        printf(\"%-8s\",buf);\n\n        /* Clients */\n        aux = getLongInfoField(reply->str,\"connected_clients\");\n        snprintf(buf,sizeof(buf),\"%ld\",aux);\n        printf(\" %-8s\",buf);\n\n        /* Blocked (BLPOPPING) Clients */\n        aux = getLongInfoField(reply->str,\"blocked_clients\");\n        snprintf(buf,sizeof(buf),\"%ld\",aux);\n        printf(\"%-8s\",buf);\n\n        /* Requests */\n        aux = getLongInfoField(reply->str,\"total_commands_processed\");\n        snprintf(buf,sizeof(buf),\"%ld (+%ld)\",aux,requests == 0 ? 0 : aux-requests);\n        printf(\"%-19s\",buf);\n        requests = aux;\n\n        /* Connections */\n        aux = getLongInfoField(reply->str,\"total_connections_received\");\n        snprintf(buf,sizeof(buf),\"%ld\",aux);\n        printf(\" %-12s\",buf);\n\n        /* Children */\n        aux = getLongInfoField(reply->str,\"bgsave_in_progress\");\n        aux |= getLongInfoField(reply->str,\"aof_rewrite_in_progress\") << 1;\n        aux |= getLongInfoField(reply->str,\"loading\") << 2;\n        switch(aux) {\n        case 0: break;\n        case 1:\n            printf(\"SAVE\");\n            break;\n        case 2:\n            printf(\"AOF\");\n            break;\n        case 3:\n            printf(\"SAVE+AOF\");\n            break;\n        case 4:\n            printf(\"LOAD\");\n            break;\n        }\n\n        printf(\"\\n\");\n        freeReplyObject(reply);\n        usleep(config.interval);\n    }\n}\n\n/*------------------------------------------------------------------------------\n * Scan mode\n *--------------------------------------------------------------------------- */\n\nstatic void scanMode(void) {\n    redisReply *reply;\n    unsigned long long cur = 0;\n\n    do {\n        reply = sendScan(&cur);\n        for (unsigned int j = 0; j < reply->element[1]->elements; j++) {\n            if (config.output == OUTPUT_STANDARD) {\n                sds out = sdscatrepr(sdsempty(), reply->element[1]->element[j]->str,\n                                     reply->element[1]->element[j]->len);\n                printf(\"%s\\n\", out);\n                sdsfree(out);\n            } else {\n                printf(\"%s\\n\", reply->element[1]->element[j]->str);\n            }\n        }\n        freeReplyObject(reply);\n    } while(cur != 0);\n\n    exit(0);\n}\n\n/*------------------------------------------------------------------------------\n * LRU test mode\n *--------------------------------------------------------------------------- */\n\n/* Return an integer from min to max (both inclusive) using a power-law\n * distribution, depending on the value of alpha: the greater the alpha\n * the more bias towards lower values.\n *\n * With alpha = 6.2 the output follows the 80-20 rule where 20% of\n * the returned numbers will account for 80% of the frequency. */\nlong long powerLawRand(long long min, long long max, double alpha) {\n    double pl, r;\n\n    max += 1;\n    r = ((double)rand()) / RAND_MAX;\n    pl = pow(\n        ((pow(max,alpha+1) - pow(min,alpha+1))*r + pow(min,alpha+1)),\n        (1.0/(alpha+1)));\n    return (max-1-(long long)pl)+min;\n}\n\n/* Generates a key name among a set of lru_test_sample_size keys, using\n * an 80-20 distribution. */\nvoid LRUTestGenKey(char *buf, size_t buflen) {\n    snprintf(buf, buflen, \"lru:%lld\",\n        powerLawRand(1, config.lru_test_sample_size, 6.2));\n}\n\n#define LRU_CYCLE_PERIOD 1000 /* 1000 milliseconds. */\n#define LRU_CYCLE_PIPELINE_SIZE 250\nstatic void LRUTestMode(void) {\n    redisReply *reply;\n    char key[128];\n    long long start_cycle;\n    int j;\n\n    srand(time(NULL)^getpid());\n    while(1) {\n        /* Perform cycles of 1 second with 50% writes and 50% reads.\n         * We use pipelining batching writes / reads N times per cycle in order\n         * to fill the target instance easily. */\n        start_cycle = mstime();\n        long long hits = 0, misses = 0;\n        while(mstime() - start_cycle < LRU_CYCLE_PERIOD) {\n            /* Write cycle. */\n            for (j = 0; j < LRU_CYCLE_PIPELINE_SIZE; j++) {\n                char val[6];\n                val[5] = '\\0';\n                for (int i = 0; i < 5; i++) val[i] = 'A'+rand()%('z'-'A');\n                LRUTestGenKey(key,sizeof(key));\n                redisAppendCommand(context, \"SET %s %s\",key,val);\n            }\n            for (j = 0; j < LRU_CYCLE_PIPELINE_SIZE; j++)\n                redisGetReply(context, (void**)&reply);\n\n            /* Read cycle. */\n            for (j = 0; j < LRU_CYCLE_PIPELINE_SIZE; j++) {\n                LRUTestGenKey(key,sizeof(key));\n                redisAppendCommand(context, \"GET %s\",key);\n            }\n            for (j = 0; j < LRU_CYCLE_PIPELINE_SIZE; j++) {\n                if (redisGetReply(context, (void**)&reply) == REDIS_OK) {\n                    switch(reply->type) {\n                        case REDIS_REPLY_ERROR:\n                            printf(\"%s\\n\", reply->str);\n                            break;\n                        case REDIS_REPLY_NIL:\n                            misses++;\n                            break;\n                        default:\n                            hits++;\n                            break;\n                    }\n                }\n            }\n\n            if (context->err) {\n                fprintf(stderr,\"I/O error during LRU test\\n\");\n                exit(1);\n            }\n        }\n        /* Print stats. */\n        printf(\n            \"%lld Gets/sec | Hits: %lld (%.2f%%) | Misses: %lld (%.2f%%)\\n\",\n            hits+misses,\n            hits, (double)hits/(hits+misses)*100,\n            misses, (double)misses/(hits+misses)*100);\n    }\n    exit(0);\n}\n\n/*------------------------------------------------------------------------------\n * Intrisic latency mode.\n *\n * Measure max latency of a running process that does not result from\n * syscalls. Basically this software should provide a hint about how much\n * time the kernel leaves the process without a chance to run.\n *--------------------------------------------------------------------------- */\n\n/* This is just some computation the compiler can't optimize out.\n * Should run in less than 100-200 microseconds even using very\n * slow hardware. Runs in less than 10 microseconds in modern HW. */\nunsigned long compute_something_fast(void) {\n    unsigned char s[256], i, j, t;\n    int count = 1000, k;\n    unsigned long output = 0;\n\n    for (k = 0; k < 256; k++) s[k] = k;\n\n    i = 0;\n    j = 0;\n    while(count--) {\n        i++;\n        j = j + s[i];\n        t = s[i];\n        s[i] = s[j];\n        s[j] = t;\n        output += s[(s[i]+s[j])&255];\n    }\n    return output;\n}\n\nstatic void intrinsicLatencyModeStop(int s) {\n    UNUSED(s);\n    force_cancel_loop = 1;\n}\n\nstatic void intrinsicLatencyMode(void) {\n    long long test_end, run_time, max_latency = 0, runs = 0;\n\n    run_time = config.intrinsic_latency_duration*1000000;\n    test_end = ustime() + run_time;\n    signal(SIGINT, intrinsicLatencyModeStop);\n\n    while(1) {\n        long long start, end, latency;\n\n        start = ustime();\n        compute_something_fast();\n        end = ustime();\n        latency = end-start;\n        runs++;\n        if (latency <= 0) continue;\n\n        /* Reporting */\n        if (latency > max_latency) {\n            max_latency = latency;\n            printf(\"Max latency so far: %lld microseconds.\\n\", max_latency);\n        }\n\n        double avg_us = (double)run_time/runs;\n        double avg_ns = avg_us * 1e3;\n        if (force_cancel_loop || end > test_end) {\n            printf(\"\\n%lld total runs \"\n                \"(avg latency: \"\n                \"%.4f microseconds / %.2f nanoseconds per run).\\n\",\n                runs, avg_us, avg_ns);\n            printf(\"Worst run took %.0fx longer than the average latency.\\n\",\n                max_latency / avg_us);\n            exit(0);\n        }\n    }\n}\n\nstatic sds askPassword(const char *msg) {\n    linenoiseMaskModeEnable();\n    sds auth = linenoise(msg);\n    linenoiseMaskModeDisable();\n    return auth;\n}\n\n/*------------------------------------------------------------------------------\n * Program main()\n *--------------------------------------------------------------------------- */\n\nint main(int argc, char **argv) {\n    int firstarg;\n    struct timeval tv;\n\n    storage_init(NULL, 0);\n    memset(&config.sslconfig, 0, sizeof(config.sslconfig));\n    config.hostip = sdsnew(\"127.0.0.1\");\n    config.hostport = 6379;\n    config.hostsocket = NULL;\n    config.repeat = 1;\n    config.interval = 0;\n    config.dbnum = 0;\n    config.input_dbnum = 0;\n    config.interactive = 0;\n    config.shutdown = 0;\n    config.monitor_mode = 0;\n    config.pubsub_mode = 0;\n    config.latency_mode = 0;\n    config.latency_dist_mode = 0;\n    config.latency_history = 0;\n    config.lru_test_mode = 0;\n    config.lru_test_sample_size = 0;\n    config.cluster_mode = 0;\n    config.cluster_send_asking = 0;\n    config.slave_mode = 0;\n    config.getrdb_mode = 0;\n    config.stat_mode = 0;\n    config.scan_mode = 0;\n    config.intrinsic_latency_mode = 0;\n    config.pattern = NULL;\n    config.rdb_filename = NULL;\n    config.pipe_mode = 0;\n    config.pipe_timeout = REDIS_CLI_DEFAULT_PIPE_TIMEOUT;\n    config.bigkeys = 0;\n    config.hotkeys = 0;\n    config.stdinarg = 0;\n    config.auth = NULL;\n    config.askpass = 0;\n    config.user = NULL;\n    config.eval = NULL;\n    config.eval_ldb = 0;\n    config.eval_ldb_end = 0;\n    config.eval_ldb_sync = 0;\n    config.enable_ldb_on_eval = 0;\n    config.last_cmd_type = -1;\n    config.verbose = 0;\n    config.set_errcode = 0;\n    config.no_auth_warning = 0;\n    config.in_multi = 0;\n    config.force_mode = 0;\n    config.cluster_manager_command.name = NULL;\n    config.cluster_manager_command.argc = 0;\n    config.cluster_manager_command.argv = NULL;\n    config.cluster_manager_command.flags = 0;\n    config.cluster_manager_command.replicas = 0;\n    config.cluster_manager_command.from = NULL;\n    config.cluster_manager_command.to = NULL;\n    config.cluster_manager_command.from_user = NULL;\n    config.cluster_manager_command.from_pass = NULL;\n    config.cluster_manager_command.from_askpass = 0;\n    config.cluster_manager_command.weight = NULL;\n    config.cluster_manager_command.weight_argc = 0;\n    config.cluster_manager_command.slots = 0;\n    config.cluster_manager_command.timeout = CLUSTER_MANAGER_MIGRATE_TIMEOUT;\n    config.cluster_manager_command.pipeline = CLUSTER_MANAGER_MIGRATE_PIPELINE;\n    config.cluster_manager_command.threshold =\n        CLUSTER_MANAGER_REBALANCE_THRESHOLD;\n    config.cluster_manager_command.backup_dir = NULL;\n    pref.hints = 1;\n\n    spectrum_palette = spectrum_palette_color;\n    spectrum_palette_size = spectrum_palette_color_size;\n\n    if (!isatty(fileno(stdout)) && (getenv(\"FAKETTY\") == NULL)) {\n        config.output = OUTPUT_RAW;\n        config.push_output = 0;\n    } else {\n        config.output = OUTPUT_STANDARD;\n        config.push_output = 1;\n    }\n    config.mb_delim = sdsnew(\"\\n\");\n    config.cmd_delim = sdsnew(\"\\n\");\n\n    firstarg = parseOptions(argc,argv);\n    argc -= firstarg;\n    argv += firstarg;\n\n    parseEnv();\n\n    if (config.askpass) {\n        config.auth = askPassword(\"Please input password: \");\n    }\n\n    if (config.cluster_manager_command.from_askpass) {\n        config.cluster_manager_command.from_pass = askPassword(\n            \"Please input import source node password: \");\n    }\n\n#ifdef USE_OPENSSL\n    if (config.tls) {\n        cliSecureInit();\n    }\n#endif\n\n    gettimeofday(&tv, NULL);\n    init_genrand64(((long long) tv.tv_sec * 1000000 + tv.tv_usec) ^ getpid());\n\n    /* Cluster Manager mode */\n    if (CLUSTER_MANAGER_MODE()) {\n        clusterManagerCommandProc *proc = validateClusterManagerCommand();\n        if (!proc) {\n            exit(1);\n        }\n        clusterManagerMode(proc);\n    }\n\n    /* Latency mode */\n    if (config.latency_mode) {\n        if (cliConnect(0) == REDIS_ERR) exit(1);\n        latencyMode();\n    }\n\n    /* Latency distribution mode */\n    if (config.latency_dist_mode) {\n        if (cliConnect(0) == REDIS_ERR) exit(1);\n        latencyDistMode();\n    }\n\n    /* Slave mode */\n    if (config.slave_mode) {\n        if (cliConnect(0) == REDIS_ERR) exit(1);\n        sendCapa();\n        slaveMode();\n    }\n\n    /* Get RDB mode. */\n    if (config.getrdb_mode) {\n        if (cliConnect(0) == REDIS_ERR) exit(1);\n        sendCapa();\n        sendRdbOnly();\n        getRDB(NULL);\n    }\n\n    /* Pipe mode */\n    if (config.pipe_mode) {\n        if (cliConnect(0) == REDIS_ERR) exit(1);\n        pipeMode();\n    }\n\n    /* Find big keys */\n    if (config.bigkeys) {\n        if (cliConnect(0) == REDIS_ERR) exit(1);\n        findBigKeys(0, 0);\n    }\n\n    /* Find large keys */\n    if (config.memkeys) {\n        if (cliConnect(0) == REDIS_ERR) exit(1);\n        findBigKeys(1, config.memkeys_samples);\n    }\n\n    /* Find hot keys */\n    if (config.hotkeys) {\n        if (cliConnect(0) == REDIS_ERR) exit(1);\n        findHotKeys();\n    }\n\n    /* Find hot keys */\n    if (config.hotkeys) {\n        if (cliConnect(0) == REDIS_ERR) exit(1);\n        findHotKeys();\n    }\n\n    /* Stat mode */\n    if (config.stat_mode) {\n        if (cliConnect(0) == REDIS_ERR) exit(1);\n        if (config.interval == 0) config.interval = 1000000;\n        statMode();\n    }\n\n    /* Scan mode */\n    if (config.scan_mode) {\n        if (cliConnect(0) == REDIS_ERR) exit(1);\n        scanMode();\n    }\n\n    /* LRU test mode */\n    if (config.lru_test_mode) {\n        if (cliConnect(0) == REDIS_ERR) exit(1);\n        LRUTestMode();\n    }\n\n    /* Intrinsic latency mode */\n    if (config.intrinsic_latency_mode) intrinsicLatencyMode();\n\n    /* Start interactive mode when no command is provided */\n    if (argc == 0 && !config.eval) {\n        /* Show the message of the day if we are interactive */\n        if (config.output == OUTPUT_STANDARD && !config.disable_motd) {\n            /*enable_motd=1 will retrieve the message of today using CURL*/\n            char *szMotd = fetchMOTD(1 /* cache */, 1 /* enable_motd */);\n            if (szMotd != NULL) {\n                printf(\"Message of the day:\\n  %s\\n\", szMotd);\n                sdsfree(szMotd);\n            }\n        }\n\n        /* Ignore SIGPIPE in interactive mode to force a reconnect */\n        signal(SIGPIPE, SIG_IGN);\n\n        /* Note that in repl mode we don't abort on connection error.\n         * A new attempt will be performed for every command send. */\n        cliConnect(0);\n        repl();\n    }\n\n    /* Otherwise, we have some arguments to execute */\n    if (cliConnect(0) != REDIS_OK) exit(1);\n\n    if (config.eval) {\n        return evalMode(argc,argv);\n    } else {\n        return noninteractive(argc,argv);\n    }\n}\n"
  },
  {
    "path": "src/redis-cli.h",
    "content": "#pragma once\n#include \"cli_common.h\"\n#include \"sdscompat.h\" /* Use hiredis' sds compat header that maps sds calls to their hi_ variants */\n#include <sds.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#define UNUSED(V) ((void) V)\n\n#define OUTPUT_STANDARD 0\n#define OUTPUT_RAW 1\n#define OUTPUT_CSV 2\n#define REDIS_CLI_KEEPALIVE_INTERVAL 15 /* seconds */\n#define REDIS_CLI_DEFAULT_PIPE_TIMEOUT 30 /* seconds */\n#define REDIS_CLI_HISTFILE_ENV \"REDISCLI_HISTFILE\"\n#define REDIS_CLI_HISTFILE_DEFAULT \".rediscli_history\"\n#define REDIS_CLI_RCFILE_ENV \"REDISCLI_RCFILE\"\n#define REDIS_CLI_RCFILE_DEFAULT \".redisclirc\"\n#define REDIS_CLI_AUTH_ENV \"REDISCLI_AUTH\"\n#define REDIS_CLI_CLUSTER_YES_ENV \"REDISCLI_CLUSTER_YES\"\n\n#define CLUSTER_MANAGER_SLOTS               16384\n#define CLUSTER_MANAGER_MIGRATE_TIMEOUT     60000\n#define CLUSTER_MANAGER_MIGRATE_PIPELINE    10\n#define CLUSTER_MANAGER_REBALANCE_THRESHOLD 2\n\n#define CLUSTER_MANAGER_INVALID_HOST_ARG \\\n    \"[ERR] Invalid arguments: you need to pass either a valid \" \\\n    \"address (ie. 120.0.0.1:7000) or space separated IP \" \\\n    \"and port (ie. 120.0.0.1 7000)\\n\"\n#define CLUSTER_MANAGER_MODE() (config.cluster_manager_command.name != NULL)\n#define CLUSTER_MANAGER_MASTERS_COUNT(nodes, replicas) (nodes/(replicas + 1))\n#define CLUSTER_MANAGER_COMMAND(n,...) \\\n        (redisCommand(n->context, __VA_ARGS__))\n\n#define CLUSTER_MANAGER_NODE_ARRAY_FREE(array) zfree(array->alloc)\n\n#define CLUSTER_MANAGER_PRINT_REPLY_ERROR(n, err) \\\n    clusterManagerLogErr(\"Node %s:%d replied with error:\\n%s\\n\", \\\n                         n->ip, n->port, err);\n\n#define clusterManagerLogInfo(...) \\\n    clusterManagerLog(CLUSTER_MANAGER_LOG_LVL_INFO,__VA_ARGS__)\n\n#define clusterManagerLogErr(...) \\\n    clusterManagerLog(CLUSTER_MANAGER_LOG_LVL_ERR,__VA_ARGS__)\n\n#define clusterManagerLogWarn(...) \\\n    clusterManagerLog(CLUSTER_MANAGER_LOG_LVL_WARN,__VA_ARGS__)\n\n#define clusterManagerLogOk(...) \\\n    clusterManagerLog(CLUSTER_MANAGER_LOG_LVL_SUCCESS,__VA_ARGS__)\n\n#define CLUSTER_MANAGER_FLAG_MYSELF     1 << 0\n#define CLUSTER_MANAGER_FLAG_SLAVE      1 << 1\n#define CLUSTER_MANAGER_FLAG_FRIEND     1 << 2\n#define CLUSTER_MANAGER_FLAG_NOADDR     1 << 3\n#define CLUSTER_MANAGER_FLAG_DISCONNECT 1 << 4\n#define CLUSTER_MANAGER_FLAG_FAIL       1 << 5\n\n#define CLUSTER_MANAGER_CMD_FLAG_FIX            1 << 0\n#define CLUSTER_MANAGER_CMD_FLAG_SLAVE          1 << 1\n#define CLUSTER_MANAGER_CMD_FLAG_YES            1 << 2\n#define CLUSTER_MANAGER_CMD_FLAG_AUTOWEIGHTS    1 << 3\n#define CLUSTER_MANAGER_CMD_FLAG_EMPTYMASTER    1 << 4\n#define CLUSTER_MANAGER_CMD_FLAG_SIMULATE       1 << 5\n#define CLUSTER_MANAGER_CMD_FLAG_REPLACE        1 << 6\n#define CLUSTER_MANAGER_CMD_FLAG_COPY           1 << 7\n#define CLUSTER_MANAGER_CMD_FLAG_COLOR          1 << 8\n#define CLUSTER_MANAGER_CMD_FLAG_CHECK_OWNERS   1 << 9\n#define CLUSTER_MANAGER_CMD_FLAG_FIX_WITH_UNREACHABLE_MASTERS 1 << 10\n#define CLUSTER_MANAGER_CMD_FLAG_MASTERS_ONLY 1 << 11\n#define CLUSTER_MANAGER_CMD_FLAG_SLAVES_ONLY 1 << 12\n\n#define CLUSTER_MANAGER_OPT_GETFRIENDS  1 << 0\n#define CLUSTER_MANAGER_OPT_COLD        1 << 1\n#define CLUSTER_MANAGER_OPT_UPDATE      1 << 2\n#define CLUSTER_MANAGER_OPT_QUIET       1 << 6\n#define CLUSTER_MANAGER_OPT_VERBOSE     1 << 7\n\n#define CLUSTER_MANAGER_LOG_LVL_INFO    1\n#define CLUSTER_MANAGER_LOG_LVL_WARN    2\n#define CLUSTER_MANAGER_LOG_LVL_ERR     3\n#define CLUSTER_MANAGER_LOG_LVL_SUCCESS 4\n\n#define CLUSTER_JOIN_CHECK_AFTER        20\n\n#define LOG_COLOR_BOLD      \"29;1m\"\n#define LOG_COLOR_RED       \"31;1m\"\n#define LOG_COLOR_GREEN     \"32;1m\"\n#define LOG_COLOR_YELLOW    \"33;1m\"\n#define LOG_COLOR_RESET     \"0m\"\n\n/* cliConnect() flags. */\n#define CC_FORCE (1<<0)         /* Re-connect if already connected. */\n#define CC_QUIET (1<<1)         /* Don't log connecting errors. */\n\n/* Dict Helpers */\n\nuint64_t dictSdsHash(const void *key);\nint dictSdsKeyCompare(void *privdata, const void *key1,\n    const void *key2);\nvoid dictSdsDestructor(void *privdata, void *val);\nvoid dictListDestructor(void *privdata, void *val);\n\n/* Cluster Manager Command Info */\ntypedef struct clusterManagerCommand {\n    char *name;\n    int argc;\n    char **argv;\n    int flags;\n    int replicas;\n    char *from;\n    char *to;\n    char **weight;\n    int weight_argc;\n    char *master_id;\n    int slots;\n    int timeout;\n    int pipeline;\n    float threshold;\n    char *backup_dir;\n    char *from_user;\n    char *from_pass;\n    int from_askpass;\n} clusterManagerCommand;\n\nvoid createClusterManagerCommand(char *cmdname, int argc, char **argv);\n\nextern redisContext *context;\nextern struct config {\n    char *hostip;\n    int hostport;\n    char *hostsocket;\n    int tls;\n    cliSSLconfig sslconfig;\n    char *sni;\n    char *cacert;\n    char *cacertdir;\n    char *cert;\n    char *key;\n    long repeat;\n    long interval;\n    int dbnum; /* db num currently selected */\n    int input_dbnum; /* db num user input */\n    int interactive;\n    int shutdown;\n    int monitor_mode;\n    int pubsub_mode;\n    int latency_mode;\n    int latency_dist_mode;\n    int latency_history;\n    int lru_test_mode;\n    long long lru_test_sample_size;\n    int cluster_mode;\n    int cluster_reissue_command;\n    int cluster_send_asking;\n    int slave_mode;\n    int pipe_mode;\n    int pipe_timeout;\n    int getrdb_mode;\n    int stat_mode;\n    int scan_mode;\n    int intrinsic_latency_mode;\n    int intrinsic_latency_duration;\n    sds pattern;\n    char *rdb_filename;\n    int bigkeys;\n    int memkeys;\n    unsigned memkeys_samples;\n    int hotkeys;\n    int stdinarg; /* get last arg from stdin. (-x option) */\n    char *auth;\n    int askpass;\n    char *user;\n    int output; /* output mode, see OUTPUT_* defines */\n    int push_output; /* Should we display spontaneous PUSH replies */\n    sds mb_delim;\n    sds cmd_delim;\n    char prompt[128];\n    char *eval;\n    int eval_ldb;\n    int eval_ldb_sync;  /* Ask for synchronous mode of the Lua debugger. */\n    int eval_ldb_end;   /* Lua debugging session ended. */\n    int enable_ldb_on_eval; /* Handle manual SCRIPT DEBUG + EVAL commands. */\n    int last_cmd_type;\n    int verbose;\n    int set_errcode;\n    clusterManagerCommand cluster_manager_command;\n    int no_auth_warning;\n    int resp3;\n    int disable_motd;\n    int in_multi;\n    int pre_multi_dbnum;\n    int quoted_input;   /* Force input args to be treated as quoted strings */\n    int force_mode;\n} config;\n\nstruct clusterManager {\n    list *nodes;    /* List of nodes in the configuration. */\n    list *errors;\n    int unreachable_masters;    /* Masters we are not able to reach. */\n};\nextern struct clusterManager cluster_manager;\n\ntypedef struct clusterManagerNode {\n    redisContext *context;\n    sds name;\n    char *ip;\n    int port;\n    uint64_t current_epoch;\n    time_t ping_sent;\n    time_t ping_recv;\n    int flags;\n    list *flags_str; /* Flags string representations */\n    sds replicate;  /* Master ID if node is a slave */\n    int dirty;      /* Node has changes that can be flushed */\n    uint8_t slots[CLUSTER_MANAGER_SLOTS];\n    int slots_count;\n    int replicas_count;\n    list *friends;\n    sds *migrating; /* An array of sds where even strings are slots and odd\n                     * strings are the destination node IDs. */\n    sds *importing; /* An array of sds where even strings are slots and odd\n                     * strings are the source node IDs. */\n    int migrating_count; /* Length of the migrating array (migrating slots*2) */\n    int importing_count; /* Length of the importing array (importing slots*2) */\n    float weight;   /* Weight used by rebalance */\n    int balance;    /* Used by rebalance */\n} clusterManagerNode;\n\n/* Data structure used to represent a sequence of cluster nodes. */\ntypedef struct clusterManagerNodeArray {\n    clusterManagerNode **nodes; /* Actual nodes array */\n    clusterManagerNode **alloc; /* Pointer to the allocated memory */\n    int len;                    /* Actual length of the array */\n    int count;                  /* Non-NULL nodes count */\n} clusterManagerNodeArray;\n\n/* Used for the reshard table. */\ntypedef struct clusterManagerReshardTableItem {\n    clusterManagerNode *source;\n    int slot;\n} clusterManagerReshardTableItem;\n\n/* Info about a cluster internal link. */\n\ntypedef struct clusterManagerLink {\n    sds node_name;\n    sds node_addr;\n    int connected;\n    int handshaking;\n} clusterManagerLink;\n\ntypedef struct typeinfo {\n    char *name;\n    char *sizecmd;\n    char *sizeunit;\n    unsigned long long biggest;\n    unsigned long long count;\n    unsigned long long totalsize;\n    sds biggest_key;\n} typeinfo;\n\nextern typeinfo type_string;\nextern typeinfo type_list;\nextern typeinfo type_set;\nextern typeinfo type_hash;\nextern typeinfo type_zset;\nextern typeinfo type_stream;\nextern typeinfo type_other;\n\nvoid findBigKeys(int memkeys, unsigned memkeys_samples);\nint clusterManagerGetAntiAffinityScore(clusterManagerNodeArray *ipnodes,\n    int ip_count, clusterManagerNode ***offending, int *offending_len);\nint clusterManagerFixMultipleSlotOwners(int slot, list *owners);\nvoid getKeySizes(redisReply *keys, struct typeinfo **types,\n    unsigned long long *sizes, int memkeys,\n    unsigned memkeys_samples);\nint clusterManagerFixOpenSlot(int slot);\nvoid clusterManagerPrintSlotsList(list *slots);\nint clusterManagerGetCoveredSlots(char *all_slots);\nvoid clusterManagerOnError(sds err);\nint clusterManagerIsConfigConsistent(int fLog);\nvoid freeClusterManagerNode(clusterManagerNode *node);\nvoid clusterManagerLog(int level, const char* fmt, ...);\nint parseClusterNodeAddress(char *addr, char **ip_ptr, int *port_ptr,\n    int *bus_port_ptr);\nint clusterManagerCheckRedisReply(clusterManagerNode *n,\n    redisReply *r, char **err);\nint confirmWithYes(const char *msg, int force);\nint clusterManagerSetSlotOwner(clusterManagerNode *owner,\n    int slot,\n    int do_clear);\nclusterManagerNode * clusterManagerGetNodeWithMostKeysInSlot(list *nodes,\n    int slot,\n    char **err);\nint clusterManagerSetSlot(clusterManagerNode *node1,\n    clusterManagerNode *node2,\n    int slot, const char *status, char **err);\nint clusterManagerMoveSlot(clusterManagerNode *source,\n    clusterManagerNode *target,\n    int slot, int opts,  char**err);\nint clusterManagerClearSlotStatus(clusterManagerNode *node, int slot);\nvoid clusterManagerShowNodes(void);\nsigned int clusterManagerCountKeysInSlot(clusterManagerNode *node,\n    int slot);\nvoid type_free(void* priv_data, void* val);\nint getDbSize(void);\nredisReply *sendScan(unsigned long long *it);\n\n#ifdef __cplusplus\n}\n#endif\n\n"
  },
  {
    "path": "src/redis-trib.rb",
    "content": "#!/usr/bin/env ruby\n\ndef colorized(str, color)\n    return str if !(ENV['TERM'] || '')[\"xterm\"]\n    color_code = {\n        white: 29,\n        bold: '29;1',\n        black: 30,\n        red: 31,\n        green: 32,\n        yellow: 33,\n        blue: 34,\n        magenta: 35,\n        cyan: 36,\n        gray: 37\n    }[color]\n    return str if !color_code\n    \"\\033[#{color_code}m#{str}\\033[0m\"\nend\n\nclass String\n\n    %w(white bold black red green yellow blue magenta cyan gray).each{|color|\n        color = :\"#{color}\"\n        define_method(color){\n            colorized(self, color)\n        }\n    }\n\nend\n\nCOMMANDS = %w(create check info fix reshard rebalance add-node \n              del-node set-timeout call import help)\n\nALLOWED_OPTIONS={\n    \"create\" => {\"replicas\" => true},\n    \"add-node\" => {\"slave\" => false, \"master-id\" => true},\n    \"import\" => {\"from\" => :required, \"copy\" => false, \"replace\" => false},\n    \"reshard\" => {\"from\" => true, \"to\" => true, \"slots\" => true, \"yes\" => false, \"timeout\" => true, \"pipeline\" => true},\n    \"rebalance\" => {\"weight\" => [], \"auto-weights\" => false, \"use-empty-masters\" => false, \"timeout\" => true, \"simulate\" => false, \"pipeline\" => true, \"threshold\" => true},\n    \"fix\" => {\"timeout\" => 0},\n}\n\ndef parse_options(cmd)\n    cmd = cmd.downcase\n    idx = 0\n    options = {}\n    args = []\n    while (arg = ARGV.shift)\n        if arg[0..1] == \"--\"\n            option = arg[2..-1]\n\n            # --verbose is a global option\n            if option == \"--verbose\"\n                options['verbose'] = true\n                next\n            end\n            if ALLOWED_OPTIONS[cmd] == nil || \n               ALLOWED_OPTIONS[cmd][option] == nil\n                next\n            end\n            if ALLOWED_OPTIONS[cmd][option] != false\n                value = ARGV.shift\n                next if !value\n            else\n                value = true\n            end\n\n            # If the option is set to [], it's a multiple arguments\n            # option. We just queue every new value into an array.\n            if ALLOWED_OPTIONS[cmd][option] == []\n                options[option] = [] if !options[option]\n                options[option] << value\n            else\n                options[option] = value\n            end\n        else\n            next if arg[0,1] == '-'\n            args << arg\n        end\n    end\n\n    return options,args\nend\n\ndef command_example(cmd, args, opts)\n    cmd = \"keydb-cli --cluster #{cmd}\"\n    args.each{|a| \n        a = a.to_s\n        a = a.inspect if a[' ']\n        cmd << \" #{a}\"\n    }\n    opts.each{|opt, val|\n        opt = \" --cluster-#{opt.downcase}\"\n        if val != true\n            val = val.join(' ') if val.is_a? Array\n            opt << \" #{val}\"\n        end\n        cmd << opt\n    }\n    cmd\nend\n\n$command = ARGV.shift\n$opts, $args = parse_options($command) if $command\n\nputs \"WARNING: keydb-trib.rb is not longer available!\".yellow\nputs \"You should use #{'keydb-cli'.bold} instead.\"\nputs ''\nputs \"All commands and features belonging to keydb-trib.rb \"+\n     \"have been moved\\nto keydb-cli.\"\nputs \"In order to use them you should call keydb-cli with the #{'--cluster'.bold}\"\nputs \"option followed by the subcommand name, arguments and options.\"\nputs ''\nputs \"Use the following syntax:\"\nputs \"keydb-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS]\".bold\nputs ''\nputs \"Example:\"\nif $command\n    example = command_example $command, $args, $opts\nelse\n    example = \"keydb-cli --cluster info 127.0.0.1:7000\"\nend\nputs example.bold\nputs ''\nputs \"To get help about all subcommands, type:\"\nputs \"keydb-cli --cluster help\".bold\nputs ''\nexit 1\n"
  },
  {
    "path": "src/redisassert.h",
    "content": "/* redisassert.h -- Drop in replacements assert.h that prints the stack trace\n *                  in the Redis logs.\n *\n * This file should be included instead of \"assert.h\" inside libraries used by\n * Redis that are using assertions, so instead of Redis disappearing with\n * SIGABORT, we get the details and stack trace inside the log file.\n *\n * ----------------------------------------------------------------------------\n *\n * Copyright (c) 2006-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __REDIS_ASSERT_H__\n#define __REDIS_ASSERT_H__\n\n#include \"config.h\"\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#undef assert\n#define assert(_e) (likely((_e))?(void)0 : (_serverAssert(#_e,__FILE__,__LINE__),redis_unreachable()))\n#define panic(...) _serverPanic(__FILE__,__LINE__,__VA_ARGS__),redis_unreachable()\n\nvoid _serverAssert(const char *estr, const char *file, int line);\nvoid _serverPanic(const char *file, int line, const char *msg, ...);\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n"
  },
  {
    "path": "src/redismodule.h",
    "content": "#ifndef REDISMODULE_H\n#define REDISMODULE_H\n\n#include <sys/types.h>\n#include <stdint.h>\n#include <stdio.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/* ---------------- Defines common between core and modules --------------- */\n\n/* Error status return values. */\n#define REDISMODULE_OK 0\n#define REDISMODULE_ERR 1\n\n/* API versions. */\n#define REDISMODULE_APIVER_1 1\n\n/* Version of the RedisModuleTypeMethods structure. Once the RedisModuleTypeMethods \n * structure is changed, this version number needs to be changed synchronistically. */\n#define REDISMODULE_TYPE_METHOD_VERSION 3\n\n/* API flags and constants */\n#define REDISMODULE_READ (1<<0)\n#define REDISMODULE_WRITE (1<<1)\n\n/* RedisModule_OpenKey extra flags for the 'mode' argument.\n * Avoid touching the LRU/LFU of the key when opened. */\n#define REDISMODULE_OPEN_KEY_NOTOUCH (1<<16)\n\n#define REDISMODULE_LIST_HEAD 0\n#define REDISMODULE_LIST_TAIL 1\n\n/* Key types. */\n#define REDISMODULE_KEYTYPE_EMPTY 0\n#define REDISMODULE_KEYTYPE_STRING 1\n#define REDISMODULE_KEYTYPE_LIST 2\n#define REDISMODULE_KEYTYPE_HASH 3\n#define REDISMODULE_KEYTYPE_SET 4\n#define REDISMODULE_KEYTYPE_ZSET 5\n#define REDISMODULE_KEYTYPE_MODULE 6\n#define REDISMODULE_KEYTYPE_STREAM 7\n\n/* Reply types. */\n#define REDISMODULE_REPLY_UNKNOWN -1\n#define REDISMODULE_REPLY_STRING 0\n#define REDISMODULE_REPLY_ERROR 1\n#define REDISMODULE_REPLY_INTEGER 2\n#define REDISMODULE_REPLY_ARRAY 3\n#define REDISMODULE_REPLY_NULL 4\n\n/* Postponed array length. */\n#define REDISMODULE_POSTPONED_ARRAY_LEN -1\n\n/* Expire */\n#define REDISMODULE_NO_EXPIRE -1\n\n/* Sorted set API flags. */\n#define REDISMODULE_ZADD_XX      (1<<0)\n#define REDISMODULE_ZADD_NX      (1<<1)\n#define REDISMODULE_ZADD_ADDED   (1<<2)\n#define REDISMODULE_ZADD_UPDATED (1<<3)\n#define REDISMODULE_ZADD_NOP     (1<<4)\n#define REDISMODULE_ZADD_GT      (1<<5)\n#define REDISMODULE_ZADD_LT      (1<<6)\n\n/* Hash API flags. */\n#define REDISMODULE_HASH_NONE       0\n#define REDISMODULE_HASH_NX         (1<<0)\n#define REDISMODULE_HASH_XX         (1<<1)\n#define REDISMODULE_HASH_CFIELDS    (1<<2)\n#define REDISMODULE_HASH_EXISTS     (1<<3)\n#define REDISMODULE_HASH_COUNT_ALL  (1<<4)\n\n/* StreamID type. */\ntypedef struct RedisModuleStreamID {\n    uint64_t ms;\n    uint64_t seq;\n} RedisModuleStreamID;\n\n/* StreamAdd() flags. */\n#define REDISMODULE_STREAM_ADD_AUTOID (1<<0)\n/* StreamIteratorStart() flags. */\n#define REDISMODULE_STREAM_ITERATOR_EXCLUSIVE (1<<0)\n#define REDISMODULE_STREAM_ITERATOR_REVERSE (1<<1)\n/* StreamIteratorTrim*() flags. */\n#define REDISMODULE_STREAM_TRIM_APPROX (1<<0)\n\n/* Context Flags: Info about the current context returned by\n * RM_GetContextFlags(). */\n\n/* The command is running in the context of a Lua script */\n#define REDISMODULE_CTX_FLAGS_LUA (1<<0)\n/* The command is running inside a Redis transaction */\n#define REDISMODULE_CTX_FLAGS_MULTI (1<<1)\n/* The instance is a master */\n#define REDISMODULE_CTX_FLAGS_MASTER (1<<2)\n/* The instance is a replica */\n#define REDISMODULE_CTX_FLAGS_SLAVE (1<<3)\n/* The instance is read-only (usually meaning it's a replica as well) */\n#define REDISMODULE_CTX_FLAGS_READONLY (1<<4)\n/* The instance is running in cluster mode */\n#define REDISMODULE_CTX_FLAGS_CLUSTER (1<<5)\n/* The instance has AOF enabled */\n#define REDISMODULE_CTX_FLAGS_AOF (1<<6)\n/* The instance has RDB enabled */\n#define REDISMODULE_CTX_FLAGS_RDB (1<<7)\n/* The instance has Maxmemory set */\n#define REDISMODULE_CTX_FLAGS_MAXMEMORY (1<<8)\n/* Maxmemory is set and has an eviction policy that may delete keys */\n#define REDISMODULE_CTX_FLAGS_EVICT (1<<9)\n/* Redis is out of memory according to the maxmemory flag. */\n#define REDISMODULE_CTX_FLAGS_OOM (1<<10)\n/* Less than 25% of memory available according to maxmemory. */\n#define REDISMODULE_CTX_FLAGS_OOM_WARNING (1<<11)\n/* The command was sent over the replication link. */\n#define REDISMODULE_CTX_FLAGS_REPLICATED (1<<12)\n/* Redis is currently loading either from AOF or RDB. */\n#define REDISMODULE_CTX_FLAGS_LOADING (1<<13)\n/* The replica has no link with its master, note that\n * there is the inverse flag as well:\n *\n *  REDISMODULE_CTX_FLAGS_REPLICA_IS_ONLINE\n *\n * The two flags are exclusive, one or the other can be set. */\n#define REDISMODULE_CTX_FLAGS_REPLICA_IS_STALE (1<<14)\n/* The replica is trying to connect with the master.\n * (REPL_STATE_CONNECT and REPL_STATE_CONNECTING states) */\n#define REDISMODULE_CTX_FLAGS_REPLICA_IS_CONNECTING (1<<15)\n/* THe replica is receiving an RDB file from its master. */\n#define REDISMODULE_CTX_FLAGS_REPLICA_IS_TRANSFERRING (1<<16)\n/* The replica is online, receiving updates from its master. */\n#define REDISMODULE_CTX_FLAGS_REPLICA_IS_ONLINE (1<<17)\n/* There is currently some background process active. */\n#define REDISMODULE_CTX_FLAGS_ACTIVE_CHILD (1<<18)\n/* The next EXEC will fail due to dirty CAS (touched keys). */\n#define REDISMODULE_CTX_FLAGS_MULTI_DIRTY (1<<19)\n/* Redis is currently running inside background child process. */\n#define REDISMODULE_CTX_FLAGS_IS_CHILD (1<<20)\n/* The current client does not allow blocking, either called from\n * within multi, lua, or from another module using RM_Call */\n#define REDISMODULE_CTX_FLAGS_DENY_BLOCKING (1<<21)\n\n/* Next context flag, must be updated when adding new flags above!\nThis flag should not be used directly by the module.\n * Use RedisModule_GetContextFlagsAll instead. */\n#define _REDISMODULE_CTX_FLAGS_NEXT (1<<22)\n\n/* Keyspace changes notification classes. Every class is associated with a\n * character for configuration purposes.\n * NOTE: These have to be in sync with NOTIFY_* in server.h */\n#define REDISMODULE_NOTIFY_KEYSPACE (1<<0)    /* K */\n#define REDISMODULE_NOTIFY_KEYEVENT (1<<1)    /* E */\n#define REDISMODULE_NOTIFY_GENERIC (1<<2)     /* g */\n#define REDISMODULE_NOTIFY_STRING (1<<3)      /* $ */\n#define REDISMODULE_NOTIFY_LIST (1<<4)        /* l */\n#define REDISMODULE_NOTIFY_SET (1<<5)         /* s */\n#define REDISMODULE_NOTIFY_HASH (1<<6)        /* h */\n#define REDISMODULE_NOTIFY_ZSET (1<<7)        /* z */\n#define REDISMODULE_NOTIFY_EXPIRED (1<<8)     /* x */\n#define REDISMODULE_NOTIFY_EVICTED (1<<9)     /* e */\n#define REDISMODULE_NOTIFY_STREAM (1<<10)     /* t */\n#define REDISMODULE_NOTIFY_KEY_MISS (1<<11)   /* m (Note: This one is excluded from REDISMODULE_NOTIFY_ALL on purpose) */\n#define REDISMODULE_NOTIFY_LOADED (1<<12)     /* module only key space notification, indicate a key loaded from rdb */\n#define REDISMODULE_NOTIFY_MODULE (1<<13)     /* d, module key space notification */\n\n/* Next notification flag, must be updated when adding new flags above!\nThis flag should not be used directly by the module.\n * Use RedisModule_GetKeyspaceNotificationFlagsAll instead. */\n#define _REDISMODULE_NOTIFY_NEXT (1<<14)\n\n#define REDISMODULE_NOTIFY_ALL (REDISMODULE_NOTIFY_GENERIC | REDISMODULE_NOTIFY_STRING | REDISMODULE_NOTIFY_LIST | REDISMODULE_NOTIFY_SET | REDISMODULE_NOTIFY_HASH | REDISMODULE_NOTIFY_ZSET | REDISMODULE_NOTIFY_EXPIRED | REDISMODULE_NOTIFY_EVICTED | REDISMODULE_NOTIFY_STREAM | REDISMODULE_NOTIFY_MODULE)      /* A */\n\n/* A special pointer that we can use between the core and the module to signal\n * field deletion, and that is impossible to be a valid pointer. */\n#define REDISMODULE_HASH_DELETE ((RedisModuleString*)(long)1)\n\n/* Error messages. */\n#define REDISMODULE_ERRORMSG_WRONGTYPE \"WRONGTYPE Operation against a key holding the wrong kind of value\"\n\n#define REDISMODULE_POSITIVE_INFINITE (1.0/0.0)\n#define REDISMODULE_NEGATIVE_INFINITE (-1.0/0.0)\n\n/* Cluster API defines. */\n#define REDISMODULE_NODE_ID_LEN 40\n#define REDISMODULE_NODE_MYSELF     (1<<0)\n#define REDISMODULE_NODE_MASTER     (1<<1)\n#define REDISMODULE_NODE_SLAVE      (1<<2)\n#define REDISMODULE_NODE_PFAIL      (1<<3)\n#define REDISMODULE_NODE_FAIL       (1<<4)\n#define REDISMODULE_NODE_NOFAILOVER (1<<5)\n\n#define REDISMODULE_CLUSTER_FLAG_NONE 0\n#define REDISMODULE_CLUSTER_FLAG_NO_FAILOVER (1<<1)\n#define REDISMODULE_CLUSTER_FLAG_NO_REDIRECTION (1<<2)\n\n#define REDISMODULE_NOT_USED(V) ((void) V)\n\n/* Logging level strings */\n#define REDISMODULE_LOGLEVEL_DEBUG \"debug\"\n#define REDISMODULE_LOGLEVEL_VERBOSE \"verbose\"\n#define REDISMODULE_LOGLEVEL_NOTICE \"notice\"\n#define REDISMODULE_LOGLEVEL_WARNING \"warning\"\n\n/* Bit flags for aux_save_triggers and the aux_load and aux_save callbacks */\n#define REDISMODULE_AUX_BEFORE_RDB (1<<0)\n#define REDISMODULE_AUX_AFTER_RDB (1<<1)\n\n/* This type represents a timer handle, and is returned when a timer is\n * registered and used in order to invalidate a timer. It's just a 64 bit\n * number, because this is how each timer is represented inside the radix tree\n * of timers that are going to expire, sorted by expire time. */\ntypedef uint64_t RedisModuleTimerID;\n\n/* CommandFilter Flags */\n\n/* Do filter RedisModule_Call() commands initiated by module itself. */\n#define REDISMODULE_CMDFILTER_NOSELF    (1<<0)\n\n/* Declare that the module can handle errors with RedisModule_SetModuleOptions. */\n#define REDISMODULE_OPTIONS_HANDLE_IO_ERRORS    (1<<0)\n/* When set, Redis will not call RedisModule_SignalModifiedKey(), implicitly in\n * RedisModule_CloseKey, and the module needs to do that when manually when keys\n * are modified from the user's sperspective, to invalidate WATCH. */\n#define REDISMODULE_OPTION_NO_IMPLICIT_SIGNAL_MODIFIED (1<<1)\n\n/* Server events definitions.\n * Those flags should not be used directly by the module, instead\n * the module should use RedisModuleEvent_* variables */\n#define REDISMODULE_EVENT_REPLICATION_ROLE_CHANGED 0\n#define REDISMODULE_EVENT_PERSISTENCE 1\n#define REDISMODULE_EVENT_FLUSHDB 2\n#define REDISMODULE_EVENT_LOADING 3\n#define REDISMODULE_EVENT_CLIENT_CHANGE 4\n#define REDISMODULE_EVENT_SHUTDOWN 5\n#define REDISMODULE_EVENT_REPLICA_CHANGE 6\n#define REDISMODULE_EVENT_MASTER_LINK_CHANGE 7\n#define REDISMODULE_EVENT_CRON_LOOP 8\n#define REDISMODULE_EVENT_MODULE_CHANGE 9\n#define REDISMODULE_EVENT_LOADING_PROGRESS 10\n#define REDISMODULE_EVENT_SWAPDB 11\n#define REDISMODULE_EVENT_REPL_BACKUP 12\n#define REDISMODULE_EVENT_FORK_CHILD 13\n#define _REDISMODULE_EVENT_NEXT 14 /* Next event flag, should be updated if a new event added. */\n\ntypedef struct RedisModuleEvent {\n    uint64_t id;        /* REDISMODULE_EVENT_... defines. */\n    uint64_t dataver;   /* Version of the structure we pass as 'data'. */\n} RedisModuleEvent;\n\nstruct RedisModuleCtx;\nstruct RedisModuleDefragCtx;\ntypedef void (*RedisModuleEventCallback)(struct RedisModuleCtx *ctx, RedisModuleEvent eid, uint64_t subevent, void *data);\n\nstatic const RedisModuleEvent\n    RedisModuleEvent_ReplicationRoleChanged = {\n        REDISMODULE_EVENT_REPLICATION_ROLE_CHANGED,\n        1\n    },\n    RedisModuleEvent_Persistence = {\n        REDISMODULE_EVENT_PERSISTENCE,\n        1\n    },\n    RedisModuleEvent_FlushDB = {\n        REDISMODULE_EVENT_FLUSHDB,\n        1\n    },\n    RedisModuleEvent_Loading = {\n        REDISMODULE_EVENT_LOADING,\n        1\n    },\n    RedisModuleEvent_ClientChange = {\n        REDISMODULE_EVENT_CLIENT_CHANGE,\n        1\n    },\n    RedisModuleEvent_Shutdown = {\n        REDISMODULE_EVENT_SHUTDOWN,\n        1\n    },\n    RedisModuleEvent_ReplicaChange = {\n        REDISMODULE_EVENT_REPLICA_CHANGE,\n        1\n    },\n    RedisModuleEvent_CronLoop = {\n        REDISMODULE_EVENT_CRON_LOOP,\n        1\n    },\n    RedisModuleEvent_MasterLinkChange = {\n        REDISMODULE_EVENT_MASTER_LINK_CHANGE,\n        1\n    },\n    RedisModuleEvent_ModuleChange = {\n        REDISMODULE_EVENT_MODULE_CHANGE,\n        1\n    },\n    RedisModuleEvent_LoadingProgress = {\n        REDISMODULE_EVENT_LOADING_PROGRESS,\n        1\n    },\n    RedisModuleEvent_SwapDB = {\n        REDISMODULE_EVENT_SWAPDB,\n        1\n    },\n    RedisModuleEvent_ReplBackup = {\n        REDISMODULE_EVENT_REPL_BACKUP,\n        1\n    },\n    RedisModuleEvent_ForkChild = {\n        REDISMODULE_EVENT_FORK_CHILD,\n        1\n    };\n\n/* Those are values that are used for the 'subevent' callback argument. */\n#define REDISMODULE_SUBEVENT_PERSISTENCE_RDB_START 0\n#define REDISMODULE_SUBEVENT_PERSISTENCE_AOF_START 1\n#define REDISMODULE_SUBEVENT_PERSISTENCE_SYNC_RDB_START 2\n#define REDISMODULE_SUBEVENT_PERSISTENCE_ENDED 3\n#define REDISMODULE_SUBEVENT_PERSISTENCE_FAILED 4\n#define _REDISMODULE_SUBEVENT_PERSISTENCE_NEXT 5\n\n#define REDISMODULE_SUBEVENT_LOADING_RDB_START 0\n#define REDISMODULE_SUBEVENT_LOADING_AOF_START 1\n#define REDISMODULE_SUBEVENT_LOADING_REPL_START 2\n#define REDISMODULE_SUBEVENT_LOADING_ENDED 3\n#define REDISMODULE_SUBEVENT_LOADING_FAILED 4\n#define _REDISMODULE_SUBEVENT_LOADING_NEXT 5\n#define REDISMODULE_SUBEVENT_LOADING_FLASH_START 6\n\n#define REDISMODULE_SUBEVENT_CLIENT_CHANGE_CONNECTED 0\n#define REDISMODULE_SUBEVENT_CLIENT_CHANGE_DISCONNECTED 1\n#define _REDISMODULE_SUBEVENT_CLIENT_CHANGE_NEXT 2\n\n#define REDISMODULE_SUBEVENT_MASTER_LINK_UP 0\n#define REDISMODULE_SUBEVENT_MASTER_LINK_DOWN 1\n#define _REDISMODULE_SUBEVENT_MASTER_NEXT 2\n\n#define REDISMODULE_SUBEVENT_REPLICA_CHANGE_ONLINE 0\n#define REDISMODULE_SUBEVENT_REPLICA_CHANGE_OFFLINE 1\n#define _REDISMODULE_SUBEVENT_REPLICA_CHANGE_NEXT 2\n\n#define REDISMODULE_EVENT_REPLROLECHANGED_NOW_MASTER 0\n#define REDISMODULE_EVENT_REPLROLECHANGED_NOW_REPLICA 1\n#define _REDISMODULE_EVENT_REPLROLECHANGED_NEXT 2\n\n#define REDISMODULE_SUBEVENT_FLUSHDB_START 0\n#define REDISMODULE_SUBEVENT_FLUSHDB_END 1\n#define _REDISMODULE_SUBEVENT_FLUSHDB_NEXT 2\n\n#define REDISMODULE_SUBEVENT_MODULE_LOADED 0\n#define REDISMODULE_SUBEVENT_MODULE_UNLOADED 1\n#define _REDISMODULE_SUBEVENT_MODULE_NEXT 2\n\n#define REDISMODULE_SUBEVENT_LOADING_PROGRESS_RDB 0\n#define REDISMODULE_SUBEVENT_LOADING_PROGRESS_AOF 1\n#define _REDISMODULE_SUBEVENT_LOADING_PROGRESS_NEXT 2\n\n#define REDISMODULE_SUBEVENT_REPL_BACKUP_CREATE 0\n#define REDISMODULE_SUBEVENT_REPL_BACKUP_RESTORE 1\n#define REDISMODULE_SUBEVENT_REPL_BACKUP_DISCARD 2\n#define _REDISMODULE_SUBEVENT_REPL_BACKUP_NEXT 3\n\n#define REDISMODULE_SUBEVENT_FORK_CHILD_BORN 0\n#define REDISMODULE_SUBEVENT_FORK_CHILD_DIED 1\n#define _REDISMODULE_SUBEVENT_FORK_CHILD_NEXT 2\n\n#define _REDISMODULE_SUBEVENT_SHUTDOWN_NEXT 0\n#define _REDISMODULE_SUBEVENT_CRON_LOOP_NEXT 0\n#define _REDISMODULE_SUBEVENT_SWAPDB_NEXT 0\n\n/* RedisModuleClientInfo flags. */\n#define REDISMODULE_CLIENTINFO_FLAG_SSL (1<<0)\n#define REDISMODULE_CLIENTINFO_FLAG_PUBSUB (1<<1)\n#define REDISMODULE_CLIENTINFO_FLAG_BLOCKED (1<<2)\n#define REDISMODULE_CLIENTINFO_FLAG_TRACKING (1<<3)\n#define REDISMODULE_CLIENTINFO_FLAG_UNIXSOCKET (1<<4)\n#define REDISMODULE_CLIENTINFO_FLAG_MULTI (1<<5)\n\n/* Here we take all the structures that the module pass to the core\n * and the other way around. Notably the list here contains the structures\n * used by the hooks API RedisModule_RegisterToServerEvent().\n *\n * The structures always start with a 'version' field. This is useful\n * when we want to pass a reference to the structure to the core APIs,\n * for the APIs to fill the structure. In that case, the structure 'version'\n * field is initialized before passing it to the core, so that the core is\n * able to cast the pointer to the appropriate structure version. In this\n * way we obtain ABI compatibility.\n *\n * Here we'll list all the structure versions in case they evolve over time,\n * however using a define, we'll make sure to use the last version as the\n * public name for the module to use. */\n\n#define REDISMODULE_CLIENTINFO_VERSION 1\ntypedef struct RedisModuleClientInfo {\n    uint64_t version;       /* Version of this structure for ABI compat. */\n    uint64_t flags;         /* REDISMODULE_CLIENTINFO_FLAG_* */\n    uint64_t id;            /* Client ID. */\n    char addr[46];          /* IPv4 or IPv6 address. */\n    uint16_t port;          /* TCP port. */\n    uint16_t db;            /* Selected DB. */\n} RedisModuleClientInfoV1;\n\n#define RedisModuleClientInfo RedisModuleClientInfoV1\n\n#define REDISMODULE_REPLICATIONINFO_VERSION 1\ntypedef struct RedisModuleReplicationInfo {\n    uint64_t version;       /* Not used since this structure is never passed\n                               from the module to the core right now. Here\n                               for future compatibility. */\n    int master;             /* true if master, false if replica */\n    const char *masterhost;       /* master instance hostname for NOW_REPLICA */\n    int masterport;         /* master instance port for NOW_REPLICA */\n    char *replid1;          /* Main replication ID */\n    char *replid2;          /* Secondary replication ID */\n    uint64_t repl1_offset;  /* Main replication offset */\n    uint64_t repl2_offset;  /* Offset of replid2 validity */\n} RedisModuleReplicationInfoV1;\n\n#define RedisModuleReplicationInfo RedisModuleReplicationInfoV1\n\n#define REDISMODULE_FLUSHINFO_VERSION 1\ntypedef struct RedisModuleFlushInfo {\n    uint64_t version;       /* Not used since this structure is never passed\n                               from the module to the core right now. Here\n                               for future compatibility. */\n    int32_t sync;           /* Synchronous or threaded flush?. */\n    int32_t dbnum;          /* Flushed database number, -1 for ALL. */\n} RedisModuleFlushInfoV1;\n\n#define RedisModuleFlushInfo RedisModuleFlushInfoV1\n\n#define REDISMODULE_MODULE_CHANGE_VERSION 1\ntypedef struct RedisModuleModuleChange {\n    uint64_t version;       /* Not used since this structure is never passed\n                               from the module to the core right now. Here\n                               for future compatibility. */\n    const char* module_name;/* Name of module loaded or unloaded. */\n    int32_t module_version; /* Module version. */\n} RedisModuleModuleChangeV1;\n\n#define RedisModuleModuleChange RedisModuleModuleChangeV1\n\n#define REDISMODULE_CRON_LOOP_VERSION 1\ntypedef struct RedisModuleCronLoopInfo {\n    uint64_t version;       /* Not used since this structure is never passed\n                               from the module to the core right now. Here\n                               for future compatibility. */\n    int32_t hz;             /* Approximate number of events per second. */\n} RedisModuleCronLoopV1;\n\n#define RedisModuleCronLoop RedisModuleCronLoopV1\n\n#define REDISMODULE_LOADING_PROGRESS_VERSION 1\ntypedef struct RedisModuleLoadingProgressInfo {\n    uint64_t version;       /* Not used since this structure is never passed\n                               from the module to the core right now. Here\n                               for future compatibility. */\n    int32_t hz;             /* Approximate number of events per second. */\n    int32_t progress;       /* Approximate progress between 0 and 1024, or -1\n                             * if unknown. */\n} RedisModuleLoadingProgressV1;\n\n#define RedisModuleLoadingProgress RedisModuleLoadingProgressV1\n\n#define REDISMODULE_SWAPDBINFO_VERSION 1\ntypedef struct RedisModuleSwapDbInfo {\n    uint64_t version;       /* Not used since this structure is never passed\n                               from the module to the core right now. Here\n                               for future compatibility. */\n    int32_t dbnum_first;    /* Swap Db first dbnum */\n    int32_t dbnum_second;   /* Swap Db second dbnum */\n} RedisModuleSwapDbInfoV1;\n\n#define RedisModuleSwapDbInfo RedisModuleSwapDbInfoV1\n\n/* ------------------------- End of common defines ------------------------ */\n\n#ifndef REDISMODULE_CORE\n\ntypedef long long mstime_t;\n\n/* Macro definitions specific to individual compilers */\n#ifndef REDISMODULE_ATTR_UNUSED\n#    ifdef __GNUC__\n#        define REDISMODULE_ATTR_UNUSED __attribute__((unused))\n#    else\n#        define REDISMODULE_ATTR_UNUSED\n#    endif\n#endif\n\n#ifndef REDISMODULE_ATTR_PRINTF\n#    ifdef __GNUC__\n#        define REDISMODULE_ATTR_PRINTF(idx,cnt) __attribute__((format(printf,idx,cnt)))\n#    else\n#        define REDISMODULE_ATTR_PRINTF(idx,cnt)\n#    endif\n#endif\n\n#ifndef REDISMODULE_ATTR_COMMON\n#    if defined(__GNUC__) && !defined(__clang__)\n#        define REDISMODULE_ATTR_COMMON __attribute__((__common__))\n#    else\n#        define REDISMODULE_ATTR_COMMON\n#    endif\n#endif\n\n/* Incomplete structures for compiler checks but opaque access. */\ntypedef struct RedisModuleCtx RedisModuleCtx;\ntypedef struct RedisModuleKey RedisModuleKey;\ntypedef struct RedisModuleString RedisModuleString;\ntypedef struct RedisModuleCallReply RedisModuleCallReply;\ntypedef struct RedisModuleIO RedisModuleIO;\ntypedef struct RedisModuleType RedisModuleType;\ntypedef struct RedisModuleDigest RedisModuleDigest;\ntypedef struct RedisModuleBlockedClient RedisModuleBlockedClient;\ntypedef struct RedisModuleClusterInfo RedisModuleClusterInfo;\ntypedef struct RedisModuleDict RedisModuleDict;\ntypedef struct RedisModuleDictIter RedisModuleDictIter;\ntypedef struct RedisModuleCommandFilterCtx RedisModuleCommandFilterCtx;\ntypedef struct RedisModuleCommandFilter RedisModuleCommandFilter;\ntypedef struct RedisModuleInfoCtx RedisModuleInfoCtx;\ntypedef struct RedisModuleServerInfoData RedisModuleServerInfoData;\ntypedef struct RedisModuleScanCursor RedisModuleScanCursor;\ntypedef struct RedisModuleDefragCtx RedisModuleDefragCtx;\ntypedef struct RedisModuleUser RedisModuleUser;\n\ntypedef int (*RedisModuleCmdFunc)(RedisModuleCtx *ctx, RedisModuleString **argv, int argc);\ntypedef void (*RedisModuleDisconnectFunc)(RedisModuleCtx *ctx, RedisModuleBlockedClient *bc);\ntypedef int (*RedisModuleNotificationFunc)(RedisModuleCtx *ctx, int type, const char *event, RedisModuleString *key);\ntypedef void *(*RedisModuleTypeLoadFunc)(RedisModuleIO *rdb, int encver);\ntypedef void (*RedisModuleTypeSaveFunc)(RedisModuleIO *rdb, void *value);\ntypedef int (*RedisModuleTypeAuxLoadFunc)(RedisModuleIO *rdb, int encver, int when);\ntypedef void (*RedisModuleTypeAuxSaveFunc)(RedisModuleIO *rdb, int when);\ntypedef void (*RedisModuleTypeRewriteFunc)(RedisModuleIO *aof, RedisModuleString *key, void *value);\ntypedef size_t (*RedisModuleTypeMemUsageFunc)(const void *value);\ntypedef void (*RedisModuleTypeDigestFunc)(RedisModuleDigest *digest, void *value);\ntypedef void (*RedisModuleTypeFreeFunc)(void *value);\ntypedef size_t (*RedisModuleTypeFreeEffortFunc)(RedisModuleString *key, const void *value);\ntypedef void (*RedisModuleTypeUnlinkFunc)(RedisModuleString *key, const void *value);\ntypedef void *(*RedisModuleTypeCopyFunc)(RedisModuleString *fromkey, RedisModuleString *tokey, const void *value);\ntypedef int (*RedisModuleTypeDefragFunc)(RedisModuleDefragCtx *ctx, RedisModuleString *key, void **value);\ntypedef void (*RedisModuleClusterMessageReceiver)(RedisModuleCtx *ctx, const char *sender_id, uint8_t type, const unsigned char *payload, uint32_t len);\ntypedef void (*RedisModuleTimerProc)(RedisModuleCtx *ctx, void *data);\ntypedef void (*RedisModuleCommandFilterFunc) (RedisModuleCommandFilterCtx *filter);\ntypedef void (*RedisModuleForkDoneHandler) (int exitcode, int bysignal, void *user_data);\ntypedef void (*RedisModuleInfoFunc)(RedisModuleInfoCtx *ctx, int for_crash_report);\ntypedef void (*RedisModuleScanCB)(RedisModuleCtx *ctx, RedisModuleString *keyname, RedisModuleKey *key, void *privdata);\ntypedef void (*RedisModuleScanKeyCB)(RedisModuleKey *key, RedisModuleString *field, RedisModuleString *value, void *privdata);\ntypedef void (*RedisModuleUserChangedFunc) (uint64_t client_id, void *privdata);\ntypedef int (*RedisModuleDefragFunc)(RedisModuleDefragCtx *ctx);\n\ntypedef struct RedisModuleTypeMethods {\n    uint64_t version;\n    RedisModuleTypeLoadFunc rdb_load;\n    RedisModuleTypeSaveFunc rdb_save;\n    RedisModuleTypeRewriteFunc aof_rewrite;\n    RedisModuleTypeMemUsageFunc mem_usage;\n    RedisModuleTypeDigestFunc digest;\n    RedisModuleTypeFreeFunc free;\n    RedisModuleTypeAuxLoadFunc aux_load;\n    RedisModuleTypeAuxSaveFunc aux_save;\n    int aux_save_triggers;\n    RedisModuleTypeFreeEffortFunc free_effort;\n    RedisModuleTypeUnlinkFunc unlink;\n    RedisModuleTypeCopyFunc copy;\n    RedisModuleTypeDefragFunc defrag;\n} RedisModuleTypeMethods;\n\n#define REDISMODULE_GET_API(name) \\\n    RedisModule_GetApi(\"RedisModule_\" #name, ((void **)&RedisModule_ ## name))\n\n/* Default API declaration prefix (not 'extern' for backwards compatibility) */\n#ifndef REDISMODULE_API\n#define REDISMODULE_API\n#endif\n\n/* Default API declaration suffix (compiler attributes) */\n#ifndef REDISMODULE_ATTR\n#define REDISMODULE_ATTR REDISMODULE_ATTR_COMMON\n#endif\n\nREDISMODULE_API void * (*RedisModule_Alloc)(size_t bytes) REDISMODULE_ATTR;\nREDISMODULE_API void * (*RedisModule_Realloc)(void *ptr, size_t bytes) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_Free)(void *ptr) REDISMODULE_ATTR;\nREDISMODULE_API void * (*RedisModule_Calloc)(size_t nmemb, size_t size) REDISMODULE_ATTR;\nREDISMODULE_API char * (*RedisModule_Strdup)(const char *str) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_GetApi)(const char *, void *) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_CreateCommand)(RedisModuleCtx *ctx, const char *name, RedisModuleCmdFunc cmdfunc, const char *strflags, int firstkey, int lastkey, int keystep) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_SetModuleAttribs)(RedisModuleCtx *ctx, const char *name, int ver, int apiver) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_IsModuleNameBusy)(const char *name) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_WrongArity)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithLongLong)(RedisModuleCtx *ctx, long long ll) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_GetSelectedDb)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_SelectDb)(RedisModuleCtx *ctx, int newid) REDISMODULE_ATTR;\nREDISMODULE_API void * (*RedisModule_OpenKey)(RedisModuleCtx *ctx, RedisModuleString *keyname, int mode) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_CloseKey)(RedisModuleKey *kp) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_KeyType)(RedisModuleKey *kp) REDISMODULE_ATTR;\nREDISMODULE_API size_t (*RedisModule_ValueLength)(RedisModuleKey *kp) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ListPush)(RedisModuleKey *kp, int where, RedisModuleString *ele) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_ListPop)(RedisModuleKey *key, int where) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleCallReply * (*RedisModule_Call)(RedisModuleCtx *ctx, const char *cmdname, const char *fmt, ...) REDISMODULE_ATTR;\nREDISMODULE_API const char * (*RedisModule_CallReplyProto)(RedisModuleCallReply *reply, size_t *len) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_FreeCallReply)(RedisModuleCallReply *reply) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_CallReplyType)(RedisModuleCallReply *reply) REDISMODULE_ATTR;\nREDISMODULE_API long long (*RedisModule_CallReplyInteger)(RedisModuleCallReply *reply) REDISMODULE_ATTR;\nREDISMODULE_API size_t (*RedisModule_CallReplyLength)(RedisModuleCallReply *reply) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleCallReply * (*RedisModule_CallReplyArrayElement)(RedisModuleCallReply *reply, size_t idx) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_CreateString)(RedisModuleCtx *ctx, const char *ptr, size_t len) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_CreateStringFromLongLong)(RedisModuleCtx *ctx, long long ll) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_CreateStringFromDouble)(RedisModuleCtx *ctx, double d) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_CreateStringFromLongDouble)(RedisModuleCtx *ctx, long double ld, int humanfriendly) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_CreateStringFromString)(RedisModuleCtx *ctx, const RedisModuleString *str) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_CreateStringFromStreamID)(RedisModuleCtx *ctx, const RedisModuleStreamID *id) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_CreateStringPrintf)(RedisModuleCtx *ctx, const char *fmt, ...) REDISMODULE_ATTR_PRINTF(2,3) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_FreeString)(RedisModuleCtx *ctx, RedisModuleString *str) REDISMODULE_ATTR;\nREDISMODULE_API const char * (*RedisModule_StringPtrLen)(const RedisModuleString *str, size_t *len) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithError)(RedisModuleCtx *ctx, const char *err) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithSimpleString)(RedisModuleCtx *ctx, const char *msg) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithArray)(RedisModuleCtx *ctx, long len) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithNullArray)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithEmptyArray)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_ReplySetArrayLength)(RedisModuleCtx *ctx, long len) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithStringBuffer)(RedisModuleCtx *ctx, const char *buf, size_t len) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithCString)(RedisModuleCtx *ctx, const char *buf) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithString)(RedisModuleCtx *ctx, RedisModuleString *str) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithEmptyString)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithVerbatimString)(RedisModuleCtx *ctx, const char *buf, size_t len) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithNull)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithDouble)(RedisModuleCtx *ctx, double d) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithLongDouble)(RedisModuleCtx *ctx, long double d) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplyWithCallReply)(RedisModuleCtx *ctx, RedisModuleCallReply *reply) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StringToLongLong)(const RedisModuleString *str, long long *ll) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StringToDouble)(const RedisModuleString *str, double *d) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StringToLongDouble)(const RedisModuleString *str, long double *d) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StringToStreamID)(const RedisModuleString *str, RedisModuleStreamID *id) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_AutoMemory)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_Replicate)(RedisModuleCtx *ctx, const char *cmdname, const char *fmt, ...) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ReplicateVerbatim)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API const char * (*RedisModule_CallReplyStringPtr)(RedisModuleCallReply *reply, size_t *len) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_CreateStringFromCallReply)(RedisModuleCallReply *reply) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DeleteKey)(RedisModuleKey *key) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_UnlinkKey)(RedisModuleKey *key) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StringSet)(RedisModuleKey *key, RedisModuleString *str) REDISMODULE_ATTR;\nREDISMODULE_API char * (*RedisModule_StringDMA)(RedisModuleKey *key, size_t *len, int mode) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StringTruncate)(RedisModuleKey *key, size_t newlen) REDISMODULE_ATTR;\nREDISMODULE_API mstime_t (*RedisModule_GetExpire)(RedisModuleKey *key) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_SetExpire)(RedisModuleKey *key, mstime_t expire) REDISMODULE_ATTR;\nREDISMODULE_API mstime_t (*RedisModule_GetAbsExpire)(RedisModuleKey *key) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_SetAbsExpire)(RedisModuleKey *key, mstime_t expire) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_ResetDataset)(int restart_aof, int async) REDISMODULE_ATTR;\nREDISMODULE_API unsigned long long (*RedisModule_DbSize)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_RandomKey)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ZsetAdd)(RedisModuleKey *key, double score, RedisModuleString *ele, int *flagsptr) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ZsetIncrby)(RedisModuleKey *key, double score, RedisModuleString *ele, int *flagsptr, double *newscore) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ZsetScore)(RedisModuleKey *key, RedisModuleString *ele, double *score) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ZsetRem)(RedisModuleKey *key, RedisModuleString *ele, int *deleted) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_ZsetRangeStop)(RedisModuleKey *key) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ZsetFirstInScoreRange)(RedisModuleKey *key, double min, double max, int minex, int maxex) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ZsetLastInScoreRange)(RedisModuleKey *key, double min, double max, int minex, int maxex) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ZsetFirstInLexRange)(RedisModuleKey *key, RedisModuleString *min, RedisModuleString *max) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ZsetLastInLexRange)(RedisModuleKey *key, RedisModuleString *min, RedisModuleString *max) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_ZsetRangeCurrentElement)(RedisModuleKey *key, double *score) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ZsetRangeNext)(RedisModuleKey *key) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ZsetRangePrev)(RedisModuleKey *key) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ZsetRangeEndReached)(RedisModuleKey *key) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_HashSet)(RedisModuleKey *key, int flags, ...) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_HashGet)(RedisModuleKey *key, int flags, ...) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StreamAdd)(RedisModuleKey *key, int flags, RedisModuleStreamID *id, RedisModuleString **argv, int64_t numfields) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StreamDelete)(RedisModuleKey *key, RedisModuleStreamID *id) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StreamIteratorStart)(RedisModuleKey *key, int flags, RedisModuleStreamID *startid, RedisModuleStreamID *endid) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StreamIteratorStop)(RedisModuleKey *key) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StreamIteratorNextID)(RedisModuleKey *key, RedisModuleStreamID *id, long *numfields) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StreamIteratorNextField)(RedisModuleKey *key, RedisModuleString **field_ptr, RedisModuleString **value_ptr) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StreamIteratorDelete)(RedisModuleKey *key) REDISMODULE_ATTR;\nREDISMODULE_API long long (*RedisModule_StreamTrimByLength)(RedisModuleKey *key, int flags, long long length) REDISMODULE_ATTR;\nREDISMODULE_API long long (*RedisModule_StreamTrimByID)(RedisModuleKey *key, int flags, RedisModuleStreamID *id) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_IsKeysPositionRequest)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_KeyAtPos)(RedisModuleCtx *ctx, int pos) REDISMODULE_ATTR;\nREDISMODULE_API unsigned long long (*RedisModule_GetClientId)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_GetClientUserNameById)(RedisModuleCtx *ctx, uint64_t id) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_GetClientInfoById)(void *ci, uint64_t id) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_PublishMessage)(RedisModuleCtx *ctx, RedisModuleString *channel, RedisModuleString *message) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_GetContextFlags)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_AvoidReplicaTraffic)() REDISMODULE_ATTR;\nREDISMODULE_API void * (*RedisModule_PoolAlloc)(RedisModuleCtx *ctx, size_t bytes) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleType * (*RedisModule_CreateDataType)(RedisModuleCtx *ctx, const char *name, int encver, RedisModuleTypeMethods *typemethods) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ModuleTypeSetValue)(RedisModuleKey *key, RedisModuleType *mt, void *value) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ModuleTypeReplaceValue)(RedisModuleKey *key, RedisModuleType *mt, void *new_value, void **old_value) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleType * (*RedisModule_ModuleTypeGetType)(RedisModuleKey *key) REDISMODULE_ATTR;\nREDISMODULE_API void * (*RedisModule_ModuleTypeGetValue)(RedisModuleKey *key) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_IsIOError)(RedisModuleIO *io) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_SetModuleOptions)(RedisModuleCtx *ctx, int options) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_SignalModifiedKey)(RedisModuleCtx *ctx, RedisModuleString *keyname) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_SaveUnsigned)(RedisModuleIO *io, uint64_t value) REDISMODULE_ATTR;\nREDISMODULE_API uint64_t (*RedisModule_LoadUnsigned)(RedisModuleIO *io) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_SaveSigned)(RedisModuleIO *io, int64_t value) REDISMODULE_ATTR;\nREDISMODULE_API int64_t (*RedisModule_LoadSigned)(RedisModuleIO *io) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_EmitAOF)(RedisModuleIO *io, const char *cmdname, const char *fmt, ...) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_SaveString)(RedisModuleIO *io, RedisModuleString *s) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_SaveStringBuffer)(RedisModuleIO *io, const char *str, size_t len) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_LoadString)(RedisModuleIO *io) REDISMODULE_ATTR;\nREDISMODULE_API char * (*RedisModule_LoadStringBuffer)(RedisModuleIO *io, size_t *lenptr) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_SaveDouble)(RedisModuleIO *io, double value) REDISMODULE_ATTR;\nREDISMODULE_API double (*RedisModule_LoadDouble)(RedisModuleIO *io) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_SaveFloat)(RedisModuleIO *io, float value) REDISMODULE_ATTR;\nREDISMODULE_API float (*RedisModule_LoadFloat)(RedisModuleIO *io) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_SaveLongDouble)(RedisModuleIO *io, long double value) REDISMODULE_ATTR;\nREDISMODULE_API long double (*RedisModule_LoadLongDouble)(RedisModuleIO *io) REDISMODULE_ATTR;\nREDISMODULE_API void * (*RedisModule_LoadDataTypeFromString)(const RedisModuleString *str, const RedisModuleType *mt) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_SaveDataTypeToString)(RedisModuleCtx *ctx, void *data, const RedisModuleType *mt) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_Log)(RedisModuleCtx *ctx, const char *level, const char *fmt, ...) REDISMODULE_ATTR REDISMODULE_ATTR_PRINTF(3,4);\nREDISMODULE_API void (*RedisModule_LogIOError)(RedisModuleIO *io, const char *levelstr, const char *fmt, ...) REDISMODULE_ATTR REDISMODULE_ATTR_PRINTF(3,4);\nREDISMODULE_API void (*RedisModule__Assert)(const char *estr, const char *file, int line) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_LatencyAddSample)(const char *event, mstime_t latency) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StringAppendBuffer)(RedisModuleCtx *ctx, RedisModuleString *str, const char *buf, size_t len) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_RetainString)(RedisModuleCtx *ctx, RedisModuleString *str) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_HoldString)(RedisModuleCtx *ctx, RedisModuleString *str) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StringCompare)(RedisModuleString *a, RedisModuleString *b) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleCtx * (*RedisModule_GetContextFromIO)(RedisModuleIO *io) REDISMODULE_ATTR;\nREDISMODULE_API const RedisModuleString * (*RedisModule_GetKeyNameFromIO)(RedisModuleIO *io) REDISMODULE_ATTR;\nREDISMODULE_API const RedisModuleString * (*RedisModule_GetKeyNameFromModuleKey)(RedisModuleKey *key) REDISMODULE_ATTR;\nREDISMODULE_API long long (*RedisModule_Milliseconds)(void) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_DigestAddStringBuffer)(RedisModuleDigest *md, unsigned char *ele, size_t len) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_DigestAddLongLong)(RedisModuleDigest *md, long long ele) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_DigestEndSequence)(RedisModuleDigest *md) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleDict * (*RedisModule_CreateDict)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_FreeDict)(RedisModuleCtx *ctx, RedisModuleDict *d) REDISMODULE_ATTR;\nREDISMODULE_API uint64_t (*RedisModule_DictSize)(RedisModuleDict *d) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DictSetC)(RedisModuleDict *d, void *key, size_t keylen, void *ptr) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DictReplaceC)(RedisModuleDict *d, void *key, size_t keylen, void *ptr) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DictSet)(RedisModuleDict *d, RedisModuleString *key, void *ptr) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DictReplace)(RedisModuleDict *d, RedisModuleString *key, void *ptr) REDISMODULE_ATTR;\nREDISMODULE_API void * (*RedisModule_DictGetC)(RedisModuleDict *d, void *key, size_t keylen, int *nokey) REDISMODULE_ATTR;\nREDISMODULE_API void * (*RedisModule_DictGet)(RedisModuleDict *d, RedisModuleString *key, int *nokey) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DictDelC)(RedisModuleDict *d, void *key, size_t keylen, void *oldval) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DictDel)(RedisModuleDict *d, RedisModuleString *key, void *oldval) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleDictIter * (*RedisModule_DictIteratorStartC)(RedisModuleDict *d, const char *op, void *key, size_t keylen) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleDictIter * (*RedisModule_DictIteratorStart)(RedisModuleDict *d, const char *op, RedisModuleString *key) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_DictIteratorStop)(RedisModuleDictIter *di) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DictIteratorReseekC)(RedisModuleDictIter *di, const char *op, void *key, size_t keylen) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DictIteratorReseek)(RedisModuleDictIter *di, const char *op, RedisModuleString *key) REDISMODULE_ATTR;\nREDISMODULE_API void * (*RedisModule_DictNextC)(RedisModuleDictIter *di, size_t *keylen, void **dataptr) REDISMODULE_ATTR;\nREDISMODULE_API void * (*RedisModule_DictPrevC)(RedisModuleDictIter *di, size_t *keylen, void **dataptr) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_DictNext)(RedisModuleCtx *ctx, RedisModuleDictIter *di, void **dataptr) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_DictPrev)(RedisModuleCtx *ctx, RedisModuleDictIter *di, void **dataptr) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DictCompareC)(RedisModuleDictIter *di, const char *op, void *key, size_t keylen) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DictCompare)(RedisModuleDictIter *di, const char *op, RedisModuleString *key) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_RegisterInfoFunc)(RedisModuleCtx *ctx, RedisModuleInfoFunc cb) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_InfoAddSection)(RedisModuleInfoCtx *ctx, char *name) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_InfoBeginDictField)(RedisModuleInfoCtx *ctx, char *name) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_InfoEndDictField)(RedisModuleInfoCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_InfoAddFieldString)(RedisModuleInfoCtx *ctx, char *field, RedisModuleString *value) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_InfoAddFieldCString)(RedisModuleInfoCtx *ctx, char *field, char *value) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_InfoAddFieldDouble)(RedisModuleInfoCtx *ctx, char *field, double value) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_InfoAddFieldLongLong)(RedisModuleInfoCtx *ctx, char *field, long long value) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_InfoAddFieldULongLong)(RedisModuleInfoCtx *ctx, char *field, unsigned long long value) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleServerInfoData * (*RedisModule_GetServerInfo)(RedisModuleCtx *ctx, const char *section) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_FreeServerInfo)(RedisModuleCtx *ctx, RedisModuleServerInfoData *data) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_ServerInfoGetField)(RedisModuleCtx *ctx, RedisModuleServerInfoData *data, const char* field) REDISMODULE_ATTR;\nREDISMODULE_API const char * (*RedisModule_ServerInfoGetFieldC)(RedisModuleServerInfoData *data, const char* field) REDISMODULE_ATTR;\nREDISMODULE_API long long (*RedisModule_ServerInfoGetFieldSigned)(RedisModuleServerInfoData *data, const char* field, int *out_err) REDISMODULE_ATTR;\nREDISMODULE_API unsigned long long (*RedisModule_ServerInfoGetFieldUnsigned)(RedisModuleServerInfoData *data, const char* field, int *out_err) REDISMODULE_ATTR;\nREDISMODULE_API double (*RedisModule_ServerInfoGetFieldDouble)(RedisModuleServerInfoData *data, const char* field, int *out_err) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_SubscribeToServerEvent)(RedisModuleCtx *ctx, RedisModuleEvent event, RedisModuleEventCallback callback) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_SetLRU)(RedisModuleKey *key, mstime_t lru_idle) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_GetLRU)(RedisModuleKey *key, mstime_t *lru_idle) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_SetLFU)(RedisModuleKey *key, long long lfu_freq) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_GetLFU)(RedisModuleKey *key, long long *lfu_freq) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleBlockedClient * (*RedisModule_BlockClientOnKeys)(RedisModuleCtx *ctx, RedisModuleCmdFunc reply_callback, RedisModuleCmdFunc timeout_callback, void (*free_privdata)(RedisModuleCtx*,void*), long long timeout_ms, RedisModuleString **keys, int numkeys, void *privdata) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_SignalKeyAsReady)(RedisModuleCtx *ctx, RedisModuleString *key) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_GetBlockedClientReadyKey)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleScanCursor * (*RedisModule_ScanCursorCreate)() REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_ScanCursorRestart)(RedisModuleScanCursor *cursor) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_ScanCursorDestroy)(RedisModuleScanCursor *cursor) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_Scan)(RedisModuleCtx *ctx, RedisModuleScanCursor *cursor, RedisModuleScanCB fn, void *privdata) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ScanKey)(RedisModuleKey *key, RedisModuleScanCursor *cursor, RedisModuleScanKeyCB fn, void *privdata) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_GetContextFlagsAll)() REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_GetKeyspaceNotificationFlagsAll)() REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_IsSubEventSupported)(RedisModuleEvent event, uint64_t subevent) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_GetServerVersion)() REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_GetTypeMethodVersion)() REDISMODULE_ATTR;\n\n/* Experimental APIs */\n#ifdef REDISMODULE_EXPERIMENTAL_API\n#define REDISMODULE_EXPERIMENTAL_API_VERSION 3\nREDISMODULE_API RedisModuleBlockedClient * (*RedisModule_BlockClient)(RedisModuleCtx *ctx, RedisModuleCmdFunc reply_callback, RedisModuleCmdFunc timeout_callback, void (*free_privdata)(RedisModuleCtx*,void*), long long timeout_ms) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_UnblockClient)(RedisModuleBlockedClient *bc, void *privdata) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_IsBlockedReplyRequest)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_IsBlockedTimeoutRequest)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API void * (*RedisModule_GetBlockedClientPrivateData)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleBlockedClient * (*RedisModule_GetBlockedClientHandle)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_AbortBlock)(RedisModuleBlockedClient *bc) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_BlockedClientMeasureTimeStart)(RedisModuleBlockedClient *bc) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_BlockedClientMeasureTimeEnd)(RedisModuleBlockedClient *bc) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleCtx * (*RedisModule_GetThreadSafeContext)(RedisModuleBlockedClient *bc) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleCtx * (*RedisModule_GetDetachedThreadSafeContext)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_FreeThreadSafeContext)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_ThreadSafeContextLock)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ThreadSafeContextTryLock)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_ThreadSafeContextUnlock)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_SubscribeToKeyspaceEvents)(RedisModuleCtx *ctx, int types, RedisModuleNotificationFunc cb) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_NotifyKeyspaceEvent)(RedisModuleCtx *ctx, int type, const char *event, RedisModuleString *key) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_GetNotifyKeyspaceEvents)() REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_BlockedClientDisconnected)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_RegisterClusterMessageReceiver)(RedisModuleCtx *ctx, uint8_t type, RedisModuleClusterMessageReceiver callback) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_SendClusterMessage)(RedisModuleCtx *ctx, char *target_id, uint8_t type, unsigned char *msg, uint32_t len) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_GetClusterNodeInfo)(RedisModuleCtx *ctx, const char *id, char *ip, char *master_id, int *port, int *flags) REDISMODULE_ATTR;\nREDISMODULE_API char ** (*RedisModule_GetClusterNodesList)(RedisModuleCtx *ctx, size_t *numnodes) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_FreeClusterNodesList)(char **ids) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleTimerID (*RedisModule_CreateTimer)(RedisModuleCtx *ctx, mstime_t period, RedisModuleTimerProc callback, void *data) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_StopTimer)(RedisModuleCtx *ctx, RedisModuleTimerID id, void **data) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_GetTimerInfo)(RedisModuleCtx *ctx, RedisModuleTimerID id, uint64_t *remaining, void **data) REDISMODULE_ATTR;\nREDISMODULE_API const char * (*RedisModule_GetMyClusterID)(void) REDISMODULE_ATTR;\nREDISMODULE_API size_t (*RedisModule_GetClusterSize)(void) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_GetRandomBytes)(unsigned char *dst, size_t len) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_GetRandomHexChars)(char *dst, size_t len) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_SetDisconnectCallback)(RedisModuleBlockedClient *bc, RedisModuleDisconnectFunc callback) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_SetClusterFlags)(RedisModuleCtx *ctx, uint64_t flags) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ExportSharedAPI)(RedisModuleCtx *ctx, const char *apiname, void *func) REDISMODULE_ATTR;\nREDISMODULE_API void * (*RedisModule_GetSharedAPI)(RedisModuleCtx *ctx, const char *apiname) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleCommandFilter * (*RedisModule_RegisterCommandFilter)(RedisModuleCtx *ctx, RedisModuleCommandFilterFunc cb, int flags) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_UnregisterCommandFilter)(RedisModuleCtx *ctx, RedisModuleCommandFilter *filter) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_CommandFilterArgsCount)(RedisModuleCommandFilterCtx *fctx) REDISMODULE_ATTR;\nREDISMODULE_API const RedisModuleString * (*RedisModule_CommandFilterArgGet)(RedisModuleCommandFilterCtx *fctx, int pos) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_CommandFilterArgInsert)(RedisModuleCommandFilterCtx *fctx, int pos, RedisModuleString *arg) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_CommandFilterArgReplace)(RedisModuleCommandFilterCtx *fctx, int pos, RedisModuleString *arg) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_CommandFilterArgDelete)(RedisModuleCommandFilterCtx *fctx, int pos) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_Fork)(RedisModuleForkDoneHandler cb, void *user_data) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_SendChildHeartbeat)(double progress) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_ExitFromChild)(int retcode) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_KillForkChild)(int child_pid) REDISMODULE_ATTR;\nREDISMODULE_API float (*RedisModule_GetUsedMemoryRatio)() REDISMODULE_ATTR;\nREDISMODULE_API size_t (*RedisModule_MallocSize)(void* ptr) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleUser * (*RedisModule_CreateModuleUser)(const char *name) REDISMODULE_ATTR;\nREDISMODULE_API void (*RedisModule_FreeModuleUser)(RedisModuleUser *user) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_SetModuleUserACL)(RedisModuleUser *user, const char* acl) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_AuthenticateClientWithACLUser)(RedisModuleCtx *ctx, const char *name, size_t len, RedisModuleUserChangedFunc callback, void *privdata, uint64_t *client_id) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_AuthenticateClientWithUser)(RedisModuleCtx *ctx, RedisModuleUser *user, RedisModuleUserChangedFunc callback, void *privdata, uint64_t *client_id) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DeauthenticateAndCloseClient)(RedisModuleCtx *ctx, uint64_t client_id) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString * (*RedisModule_GetClientCertificate)(RedisModuleCtx *ctx, uint64_t id) REDISMODULE_ATTR;\nREDISMODULE_API int *(*RedisModule_GetCommandKeys)(RedisModuleCtx *ctx, RedisModuleString **argv, int argc, int *num_keys) REDISMODULE_ATTR;\nREDISMODULE_API const char *(*RedisModule_GetCurrentCommandName)(RedisModuleCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_RegisterDefragFunc)(RedisModuleCtx *ctx, RedisModuleDefragFunc func) REDISMODULE_ATTR;\nREDISMODULE_API void *(*RedisModule_DefragAlloc)(RedisModuleDefragCtx *ctx, void *ptr) REDISMODULE_ATTR;\nREDISMODULE_API RedisModuleString *(*RedisModule_DefragRedisModuleString)(RedisModuleDefragCtx *ctx, RedisModuleString *str) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DefragShouldStop)(RedisModuleDefragCtx *ctx) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DefragCursorSet)(RedisModuleDefragCtx *ctx, unsigned long cursor) REDISMODULE_ATTR;\nREDISMODULE_API int (*RedisModule_DefragCursorGet)(RedisModuleDefragCtx *ctx, unsigned long *cursor) REDISMODULE_ATTR;\n#endif\n\n#define RedisModule_IsAOFClient(id) ((id) == UINT64_MAX)\n\n/* This is included inline inside each Redis module. */\nstatic int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int apiver) REDISMODULE_ATTR_UNUSED;\nstatic int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int apiver) {\n    void *getapifuncptr = ((void**)ctx)[0];\n    RedisModule_GetApi = (int (*)(const char *, void *)) (unsigned long)getapifuncptr;\n    REDISMODULE_GET_API(Alloc);\n    REDISMODULE_GET_API(Calloc);\n    REDISMODULE_GET_API(Free);\n    REDISMODULE_GET_API(Realloc);\n    REDISMODULE_GET_API(Strdup);\n    REDISMODULE_GET_API(CreateCommand);\n    REDISMODULE_GET_API(SetModuleAttribs);\n    REDISMODULE_GET_API(IsModuleNameBusy);\n    REDISMODULE_GET_API(WrongArity);\n    REDISMODULE_GET_API(ReplyWithLongLong);\n    REDISMODULE_GET_API(ReplyWithError);\n    REDISMODULE_GET_API(ReplyWithSimpleString);\n    REDISMODULE_GET_API(ReplyWithArray);\n    REDISMODULE_GET_API(ReplyWithNullArray);\n    REDISMODULE_GET_API(ReplyWithEmptyArray);\n    REDISMODULE_GET_API(ReplySetArrayLength);\n    REDISMODULE_GET_API(ReplyWithStringBuffer);\n    REDISMODULE_GET_API(ReplyWithCString);\n    REDISMODULE_GET_API(ReplyWithString);\n    REDISMODULE_GET_API(ReplyWithEmptyString);\n    REDISMODULE_GET_API(ReplyWithVerbatimString);\n    REDISMODULE_GET_API(ReplyWithNull);\n    REDISMODULE_GET_API(ReplyWithCallReply);\n    REDISMODULE_GET_API(ReplyWithDouble);\n    REDISMODULE_GET_API(ReplyWithLongDouble);\n    REDISMODULE_GET_API(GetSelectedDb);\n    REDISMODULE_GET_API(SelectDb);\n    REDISMODULE_GET_API(OpenKey);\n    REDISMODULE_GET_API(CloseKey);\n    REDISMODULE_GET_API(KeyType);\n    REDISMODULE_GET_API(ValueLength);\n    REDISMODULE_GET_API(ListPush);\n    REDISMODULE_GET_API(ListPop);\n    REDISMODULE_GET_API(StringToLongLong);\n    REDISMODULE_GET_API(StringToDouble);\n    REDISMODULE_GET_API(StringToLongDouble);\n    REDISMODULE_GET_API(StringToStreamID);\n    REDISMODULE_GET_API(Call);\n    REDISMODULE_GET_API(CallReplyProto);\n    REDISMODULE_GET_API(FreeCallReply);\n    REDISMODULE_GET_API(CallReplyInteger);\n    REDISMODULE_GET_API(CallReplyType);\n    REDISMODULE_GET_API(CallReplyLength);\n    REDISMODULE_GET_API(CallReplyArrayElement);\n    REDISMODULE_GET_API(CallReplyStringPtr);\n    REDISMODULE_GET_API(CreateStringFromCallReply);\n    REDISMODULE_GET_API(CreateString);\n    REDISMODULE_GET_API(CreateStringFromLongLong);\n    REDISMODULE_GET_API(CreateStringFromDouble);\n    REDISMODULE_GET_API(CreateStringFromLongDouble);\n    REDISMODULE_GET_API(CreateStringFromString);\n    REDISMODULE_GET_API(CreateStringFromStreamID);\n    REDISMODULE_GET_API(CreateStringPrintf);\n    REDISMODULE_GET_API(FreeString);\n    REDISMODULE_GET_API(StringPtrLen);\n    REDISMODULE_GET_API(AutoMemory);\n    REDISMODULE_GET_API(Replicate);\n    REDISMODULE_GET_API(ReplicateVerbatim);\n    REDISMODULE_GET_API(DeleteKey);\n    REDISMODULE_GET_API(UnlinkKey);\n    REDISMODULE_GET_API(StringSet);\n    REDISMODULE_GET_API(StringDMA);\n    REDISMODULE_GET_API(StringTruncate);\n    REDISMODULE_GET_API(GetExpire);\n    REDISMODULE_GET_API(SetExpire);\n    REDISMODULE_GET_API(GetAbsExpire);\n    REDISMODULE_GET_API(SetAbsExpire);\n    REDISMODULE_GET_API(ResetDataset);\n    REDISMODULE_GET_API(DbSize);\n    REDISMODULE_GET_API(RandomKey);\n    REDISMODULE_GET_API(ZsetAdd);\n    REDISMODULE_GET_API(ZsetIncrby);\n    REDISMODULE_GET_API(ZsetScore);\n    REDISMODULE_GET_API(ZsetRem);\n    REDISMODULE_GET_API(ZsetRangeStop);\n    REDISMODULE_GET_API(ZsetFirstInScoreRange);\n    REDISMODULE_GET_API(ZsetLastInScoreRange);\n    REDISMODULE_GET_API(ZsetFirstInLexRange);\n    REDISMODULE_GET_API(ZsetLastInLexRange);\n    REDISMODULE_GET_API(ZsetRangeCurrentElement);\n    REDISMODULE_GET_API(ZsetRangeNext);\n    REDISMODULE_GET_API(ZsetRangePrev);\n    REDISMODULE_GET_API(ZsetRangeEndReached);\n    REDISMODULE_GET_API(HashSet);\n    REDISMODULE_GET_API(HashGet);\n    REDISMODULE_GET_API(StreamAdd);\n    REDISMODULE_GET_API(StreamDelete);\n    REDISMODULE_GET_API(StreamIteratorStart);\n    REDISMODULE_GET_API(StreamIteratorStop);\n    REDISMODULE_GET_API(StreamIteratorNextID);\n    REDISMODULE_GET_API(StreamIteratorNextField);\n    REDISMODULE_GET_API(StreamIteratorDelete);\n    REDISMODULE_GET_API(StreamTrimByLength);\n    REDISMODULE_GET_API(StreamTrimByID);\n    REDISMODULE_GET_API(IsKeysPositionRequest);\n    REDISMODULE_GET_API(KeyAtPos);\n    REDISMODULE_GET_API(GetClientId);\n    REDISMODULE_GET_API(GetClientUserNameById);\n    REDISMODULE_GET_API(GetContextFlags);\n    REDISMODULE_GET_API(AvoidReplicaTraffic);\n    REDISMODULE_GET_API(PoolAlloc);\n    REDISMODULE_GET_API(CreateDataType);\n    REDISMODULE_GET_API(ModuleTypeSetValue);\n    REDISMODULE_GET_API(ModuleTypeReplaceValue);\n    REDISMODULE_GET_API(ModuleTypeGetType);\n    REDISMODULE_GET_API(ModuleTypeGetValue);\n    REDISMODULE_GET_API(IsIOError);\n    REDISMODULE_GET_API(SetModuleOptions);\n    REDISMODULE_GET_API(SignalModifiedKey);\n    REDISMODULE_GET_API(SaveUnsigned);\n    REDISMODULE_GET_API(LoadUnsigned);\n    REDISMODULE_GET_API(SaveSigned);\n    REDISMODULE_GET_API(LoadSigned);\n    REDISMODULE_GET_API(SaveString);\n    REDISMODULE_GET_API(SaveStringBuffer);\n    REDISMODULE_GET_API(LoadString);\n    REDISMODULE_GET_API(LoadStringBuffer);\n    REDISMODULE_GET_API(SaveDouble);\n    REDISMODULE_GET_API(LoadDouble);\n    REDISMODULE_GET_API(SaveFloat);\n    REDISMODULE_GET_API(LoadFloat);\n    REDISMODULE_GET_API(SaveLongDouble);\n    REDISMODULE_GET_API(LoadLongDouble);\n    REDISMODULE_GET_API(SaveDataTypeToString);\n    REDISMODULE_GET_API(LoadDataTypeFromString);\n    REDISMODULE_GET_API(EmitAOF);\n    REDISMODULE_GET_API(Log);\n    REDISMODULE_GET_API(LogIOError);\n    REDISMODULE_GET_API(_Assert);\n    REDISMODULE_GET_API(LatencyAddSample);\n    REDISMODULE_GET_API(StringAppendBuffer);\n    REDISMODULE_GET_API(RetainString);\n    REDISMODULE_GET_API(HoldString);\n    REDISMODULE_GET_API(StringCompare);\n    REDISMODULE_GET_API(GetContextFromIO);\n    REDISMODULE_GET_API(GetKeyNameFromIO);\n    REDISMODULE_GET_API(GetKeyNameFromModuleKey);\n    REDISMODULE_GET_API(Milliseconds);\n    REDISMODULE_GET_API(DigestAddStringBuffer);\n    REDISMODULE_GET_API(DigestAddLongLong);\n    REDISMODULE_GET_API(DigestEndSequence);\n    REDISMODULE_GET_API(CreateDict);\n    REDISMODULE_GET_API(FreeDict);\n    REDISMODULE_GET_API(DictSize);\n    REDISMODULE_GET_API(DictSetC);\n    REDISMODULE_GET_API(DictReplaceC);\n    REDISMODULE_GET_API(DictSet);\n    REDISMODULE_GET_API(DictReplace);\n    REDISMODULE_GET_API(DictGetC);\n    REDISMODULE_GET_API(DictGet);\n    REDISMODULE_GET_API(DictDelC);\n    REDISMODULE_GET_API(DictDel);\n    REDISMODULE_GET_API(DictIteratorStartC);\n    REDISMODULE_GET_API(DictIteratorStart);\n    REDISMODULE_GET_API(DictIteratorStop);\n    REDISMODULE_GET_API(DictIteratorReseekC);\n    REDISMODULE_GET_API(DictIteratorReseek);\n    REDISMODULE_GET_API(DictNextC);\n    REDISMODULE_GET_API(DictPrevC);\n    REDISMODULE_GET_API(DictNext);\n    REDISMODULE_GET_API(DictPrev);\n    REDISMODULE_GET_API(DictCompare);\n    REDISMODULE_GET_API(DictCompareC);\n    REDISMODULE_GET_API(RegisterInfoFunc);\n    REDISMODULE_GET_API(InfoAddSection);\n    REDISMODULE_GET_API(InfoBeginDictField);\n    REDISMODULE_GET_API(InfoEndDictField);\n    REDISMODULE_GET_API(InfoAddFieldString);\n    REDISMODULE_GET_API(InfoAddFieldCString);\n    REDISMODULE_GET_API(InfoAddFieldDouble);\n    REDISMODULE_GET_API(InfoAddFieldLongLong);\n    REDISMODULE_GET_API(InfoAddFieldULongLong);\n    REDISMODULE_GET_API(GetServerInfo);\n    REDISMODULE_GET_API(FreeServerInfo);\n    REDISMODULE_GET_API(ServerInfoGetField);\n    REDISMODULE_GET_API(ServerInfoGetFieldC);\n    REDISMODULE_GET_API(ServerInfoGetFieldSigned);\n    REDISMODULE_GET_API(ServerInfoGetFieldUnsigned);\n    REDISMODULE_GET_API(ServerInfoGetFieldDouble);\n    REDISMODULE_GET_API(GetClientInfoById);\n    REDISMODULE_GET_API(PublishMessage);\n    REDISMODULE_GET_API(SubscribeToServerEvent);\n    REDISMODULE_GET_API(SetLRU);\n    REDISMODULE_GET_API(GetLRU);\n    REDISMODULE_GET_API(SetLFU);\n    REDISMODULE_GET_API(GetLFU);\n    REDISMODULE_GET_API(BlockClientOnKeys);\n    REDISMODULE_GET_API(SignalKeyAsReady);\n    REDISMODULE_GET_API(GetBlockedClientReadyKey);\n    REDISMODULE_GET_API(ScanCursorCreate);\n    REDISMODULE_GET_API(ScanCursorRestart);\n    REDISMODULE_GET_API(ScanCursorDestroy);\n    REDISMODULE_GET_API(Scan);\n    REDISMODULE_GET_API(ScanKey);\n    REDISMODULE_GET_API(GetContextFlagsAll);\n    REDISMODULE_GET_API(GetKeyspaceNotificationFlagsAll);\n    REDISMODULE_GET_API(IsSubEventSupported);\n    REDISMODULE_GET_API(GetServerVersion);\n    REDISMODULE_GET_API(GetTypeMethodVersion);\n\n#ifdef REDISMODULE_EXPERIMENTAL_API\n    REDISMODULE_GET_API(GetThreadSafeContext);\n    REDISMODULE_GET_API(GetDetachedThreadSafeContext);\n    REDISMODULE_GET_API(FreeThreadSafeContext);\n    REDISMODULE_GET_API(ThreadSafeContextLock);\n    REDISMODULE_GET_API(ThreadSafeContextTryLock);\n    REDISMODULE_GET_API(ThreadSafeContextUnlock);\n    REDISMODULE_GET_API(BlockClient);\n    REDISMODULE_GET_API(UnblockClient);\n    REDISMODULE_GET_API(IsBlockedReplyRequest);\n    REDISMODULE_GET_API(IsBlockedTimeoutRequest);\n    REDISMODULE_GET_API(GetBlockedClientPrivateData);\n    REDISMODULE_GET_API(GetBlockedClientHandle);\n    REDISMODULE_GET_API(AbortBlock);\n    REDISMODULE_GET_API(BlockedClientMeasureTimeStart);\n    REDISMODULE_GET_API(BlockedClientMeasureTimeEnd);\n    REDISMODULE_GET_API(SetDisconnectCallback);\n    REDISMODULE_GET_API(SubscribeToKeyspaceEvents);\n    REDISMODULE_GET_API(NotifyKeyspaceEvent);\n    REDISMODULE_GET_API(GetNotifyKeyspaceEvents);\n    REDISMODULE_GET_API(BlockedClientDisconnected);\n    REDISMODULE_GET_API(RegisterClusterMessageReceiver);\n    REDISMODULE_GET_API(SendClusterMessage);\n    REDISMODULE_GET_API(GetClusterNodeInfo);\n    REDISMODULE_GET_API(GetClusterNodesList);\n    REDISMODULE_GET_API(FreeClusterNodesList);\n    REDISMODULE_GET_API(CreateTimer);\n    REDISMODULE_GET_API(StopTimer);\n    REDISMODULE_GET_API(GetTimerInfo);\n    REDISMODULE_GET_API(GetMyClusterID);\n    REDISMODULE_GET_API(GetClusterSize);\n    REDISMODULE_GET_API(GetRandomBytes);\n    REDISMODULE_GET_API(GetRandomHexChars);\n    REDISMODULE_GET_API(SetClusterFlags);\n    REDISMODULE_GET_API(ExportSharedAPI);\n    REDISMODULE_GET_API(GetSharedAPI);\n    REDISMODULE_GET_API(RegisterCommandFilter);\n    REDISMODULE_GET_API(UnregisterCommandFilter);\n    REDISMODULE_GET_API(CommandFilterArgsCount);\n    REDISMODULE_GET_API(CommandFilterArgGet);\n    REDISMODULE_GET_API(CommandFilterArgInsert);\n    REDISMODULE_GET_API(CommandFilterArgReplace);\n    REDISMODULE_GET_API(CommandFilterArgDelete);\n    REDISMODULE_GET_API(Fork);\n    REDISMODULE_GET_API(SendChildHeartbeat);\n    REDISMODULE_GET_API(ExitFromChild);\n    REDISMODULE_GET_API(KillForkChild);\n    REDISMODULE_GET_API(GetUsedMemoryRatio);\n    REDISMODULE_GET_API(MallocSize);\n    REDISMODULE_GET_API(CreateModuleUser);\n    REDISMODULE_GET_API(FreeModuleUser);\n    REDISMODULE_GET_API(SetModuleUserACL);\n    REDISMODULE_GET_API(DeauthenticateAndCloseClient);\n    REDISMODULE_GET_API(AuthenticateClientWithACLUser);\n    REDISMODULE_GET_API(AuthenticateClientWithUser);\n    REDISMODULE_GET_API(GetClientCertificate);\n    REDISMODULE_GET_API(GetCommandKeys);\n    REDISMODULE_GET_API(GetCurrentCommandName);\n    REDISMODULE_GET_API(RegisterDefragFunc);\n    REDISMODULE_GET_API(DefragAlloc);\n    REDISMODULE_GET_API(DefragRedisModuleString);\n    REDISMODULE_GET_API(DefragShouldStop);\n    REDISMODULE_GET_API(DefragCursorSet);\n    REDISMODULE_GET_API(DefragCursorGet);\n#endif\n\n    if (RedisModule_IsModuleNameBusy && RedisModule_IsModuleNameBusy(name)) return REDISMODULE_ERR;\n    RedisModule_SetModuleAttribs(ctx,name,ver,apiver);\n    return REDISMODULE_OK;\n}\n\n#define RedisModule_Assert(_e) ((_e)?(void)0 : (RedisModule__Assert(#_e,__FILE__,__LINE__),exit(1)))\n\n#define RMAPI_FUNC_SUPPORTED(func) (func != NULL)\n\n#else\n\n/* Things only defined for the modules core, not exported to modules\n * including this file. */\n#define RedisModuleString robj\n\n#endif /* REDISMODULE_CORE */\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif /* REDISMODULE_H */\n"
  },
  {
    "path": "src/release.c",
    "content": "/*\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n/* Every time the Redis Git SHA1 or Dirty status changes only this small\n * file is recompiled, as we access this information in all the other\n * files using this functions. */\n\n#include <string.h>\n#include <stdio.h>\n\n#include \"release.h\"\n#include \"version.h\"\n#include \"crc64.h\"\n\nchar *redisGitSHA1(void) {\n    return REDIS_GIT_SHA1;\n}\n\nchar *redisGitDirty(void) {\n    return REDIS_GIT_DIRTY;\n}\n\nuint64_t redisBuildId(void) {\n    char *buildid = KEYDB_REAL_VERSION REDIS_BUILD_ID REDIS_GIT_DIRTY REDIS_GIT_SHA1;\n\n    return crc64(0,(unsigned char*)buildid,strlen(buildid));\n}\n\n/* Return a cached value of the build string in order to avoid recomputing\n * and converting it in hex every time: this string is shown in the INFO\n * output that should be fast. */\nchar *redisBuildIdString(void) {\n    static char buf[32];\n    static int cached = 0;\n    if (!cached) {\n        snprintf(buf,sizeof(buf),\"%llx\",(unsigned long long) redisBuildId());\n        cached = 1;\n    }\n    return buf;\n}\n"
  },
  {
    "path": "src/replication.cpp",
    "content": "/* Asynchronous replication implementation.\n *\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * Copyright (c) 2019 John Sully <john at eqalpha dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n\n#include \"server.h\"\n#include \"cluster.h\"\n#include \"bio.h\"\n#include \"aelocker.h\"\n#include \"SnapshotPayloadParseState.h\"\n\n#include <sys/time.h>\n#include <unistd.h>\n#include <fcntl.h>\n#include <sys/socket.h>\n#include <sys/stat.h>\n#include <mutex>\n#include <algorithm>\n#include <uuid/uuid.h>\n#include <chrono>\n#include <unordered_map>\n#include <string>\n#include <sys/mman.h>\n\nvoid replicationDiscardCachedMaster(redisMaster *mi);\nvoid replicationResurrectCachedMaster(redisMaster *mi, connection *conn);\nvoid replicationSendAck(redisMaster *mi);\nvoid putSlaveOnline(client *replica);\nint cancelReplicationHandshake(redisMaster *mi, int reconnect);\nstatic void propagateMasterStaleKeys();\nextern \"C\" pid_t gettid();\n\n/* We take a global flag to remember if this instance generated an RDB\n * because of replication, so that we can remove the RDB file in case\n * the instance is configured to have no persistence. */\nint RDBGeneratedByReplication = 0;\n\n/* --------------------------- Utility functions ---------------------------- */\n\n/* Return the pointer to a string representing the replica ip:listening_port\n * pair. Mostly useful for logging, since we want to log a replica using its\n * IP address and its listening port which is more clear for the user, for\n * example: \"Closing connection with replica 10.1.2.3:6380\". */\nchar *replicationGetSlaveName(client *c) {\n    static char buf[NET_HOST_PORT_STR_LEN];\n    char ip[NET_IP_STR_LEN];\n\n    ip[0] = '\\0';\n    buf[0] = '\\0';\n    if (c->slave_addr ||\n        connPeerToString(c->conn,ip,sizeof(ip),NULL) != -1)\n    {\n        char *addr = c->slave_addr ? c->slave_addr : ip;\n        if (c->slave_listening_port)\n            anetFormatAddr(buf,sizeof(buf),addr,c->slave_listening_port);\n        else\n            snprintf(buf,sizeof(buf),\"%s:<unknown-replica-port>\",addr);\n    } else {\n        snprintf(buf,sizeof(buf),\"client id #%llu\",\n            (unsigned long long) c->id);\n    }\n    return buf;\n}\n\nstatic bool FSameUuidNoNil(const unsigned char *a, const unsigned char *b)\n{\n    unsigned char zeroCheck = 0;\n    for (int i = 0; i < UUID_BINARY_LEN; ++i)\n    {\n        if (a[i] != b[i])\n            return false;\n        zeroCheck |= a[i];\n    }\n    return (zeroCheck != 0);    // if the UUID is nil then it is never equal\n}\n\nstatic bool FSameHost(client *clientA, client *clientB)\n{\n    if (clientA == nullptr || clientB == nullptr)\n        return false;\n\n    const unsigned char *a = clientA->uuid;\n    const unsigned char *b = clientB->uuid;\n\n    return FSameUuidNoNil(a, b);\n}\n\nstatic bool FMasterHost(client *c)\n{\n    listIter li;\n    listNode *ln;\n    listRewind(g_pserver->masters, &li);\n    while ((ln = listNext(&li)))\n    {\n        redisMaster *mi = (redisMaster*)listNodeValue(ln);\n        if (FSameUuidNoNil(mi->master_uuid, c->uuid))\n            return true;\n    }\n    return false;\n}\n\nstatic bool FAnyDisconnectedMasters()\n{\n    listIter li;\n    listNode *ln;\n    listRewind(g_pserver->masters, &li);\n    while ((ln = listNext(&li)))\n    {\n        redisMaster *mi = (redisMaster*)listNodeValue(ln);\n        if (mi->repl_state != REPL_STATE_CONNECTED)\n            return true;\n    }\n    return false;\n}\n\nclient *replicaFromMaster(redisMaster *mi)\n{\n    if (mi->master == nullptr)\n        return nullptr;\n\n    listIter liReplica;\n    listNode *lnReplica;\n    listRewind(g_pserver->slaves, &liReplica);\n    while ((lnReplica = listNext(&liReplica)) != nullptr)\n    {\n        client *replica = (client*)listNodeValue(lnReplica);\n        if (FSameHost(mi->master, replica))\n            return replica;\n    }\n    return nullptr;\n}\n\n/* Plain unlink() can block for quite some time in order to actually apply\n * the file deletion to the filesystem. This call removes the file in a\n * background thread instead. We actually just do close() in the thread,\n * by using the fact that if there is another instance of the same file open,\n * the foreground unlink() will only remove the fs name, and deleting the\n * file's storage space will only happen once the last reference is lost. */\nint bg_unlink(const char *filename) {\n    int fd = open(filename,O_RDONLY|O_NONBLOCK);\n    if (fd == -1) {\n        /* Can't open the file? Fall back to unlinking in the main thread. */\n        return unlink(filename);\n    } else {\n        /* The following unlink() removes the name but doesn't free the\n         * file contents because a process still has it open. */\n        int retval = unlink(filename);\n        if (retval == -1) {\n            /* If we got an unlink error, we just return it, closing the\n             * new reference we have to the file. */\n            int old_errno = errno;\n            close(fd);  /* This would overwrite our errno. So we saved it. */\n            errno = old_errno;\n            return -1;\n        }\n        bioCreateCloseJob(fd);\n        return 0; /* Success. */\n    }\n}\n\n/* ---------------------------------- MASTER -------------------------------- */\n\nbool createDiskBacklog() {\n    if (g_pserver->repl_backlog_disk != nullptr)\n        return true;    // already exists\n    // Lets create some disk backed pages and add them here\n    std::string path = \"./repl-backlog-temp\" + std::to_string(gettid());\n#if (defined __APPLE__ || defined __FreeBSD__)\n    int fd = open(path.c_str(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);\n#else\n    int fd = open(path.c_str(), O_CREAT | O_RDWR | O_LARGEFILE, S_IRUSR | S_IWUSR);\n#endif\n    if (fd < 0) {\n        return false;\n    }\n    size_t alloc = cserver.repl_backlog_disk_size;\n    int result = truncate(path.c_str(), alloc);\n    unlink(path.c_str()); // ensure the fd is the only ref\n    if (result == -1) {\n        close (fd);\n        return false;\n    }\n\n    g_pserver->repl_backlog_disk = (char*)mmap(nullptr, alloc, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);\n    close(fd);\n    if (g_pserver->repl_backlog_disk == MAP_FAILED) {\n        g_pserver->repl_backlog_disk = nullptr;\n        return false;\n    }\n\n    serverLog(LL_VERBOSE, \"Disk Backed Replication Allocated\");\n    return true;\n}\n\nvoid createReplicationBacklog(void) {\n    serverAssert(g_pserver->repl_backlog == NULL);\n    if (cserver.repl_backlog_disk_size) {\n        if (!createDiskBacklog()) {\n            serverLog(LL_WARNING, \"Failed to create disk backlog, will use memory only\");\n        }\n    }\n    if (cserver.force_backlog_disk && g_pserver->repl_backlog_disk != nullptr) {\n        g_pserver->repl_backlog = g_pserver->repl_backlog_disk;\n        g_pserver->repl_backlog_size = cserver.repl_backlog_disk_size;\n    } else {\n        g_pserver->repl_backlog = (char*)zmalloc(g_pserver->repl_backlog_size, MALLOC_LOCAL);\n    }\n    g_pserver->repl_backlog_histlen = 0;\n    g_pserver->repl_backlog_idx = 0;\n    g_pserver->repl_backlog_start = g_pserver->master_repl_offset;\n\n    /* We don't have any data inside our buffer, but virtually the first\n     * byte we have is the next byte that will be generated for the\n     * replication stream. */\n    g_pserver->repl_backlog_off = g_pserver->master_repl_offset+1;\n\n    /* Allow transmission to clients */\n    g_pserver->repl_batch_idxStart = 0;\n    g_pserver->repl_batch_offStart = g_pserver->master_repl_offset;\n}\n\n/* Compute the corresponding index from a replication backlog offset \n * Since this computation needs the size of the replication backlog, \n * you need to have the repl_backlog_lock in order to call it */\nlong long getReplIndexFromOffset(long long offset){\n    serverAssert(g_pserver->repl_backlog_lock.fOwnLock());\n    long long index = (offset - g_pserver->repl_backlog_start) % g_pserver->repl_backlog_size;\n    return index;\n}\n\n/* This function is called when the user modifies the replication backlog\n * size at runtime. It is up to the function to both update the\n * g_pserver->repl_backlog_size and to resize the buffer and setup it so that\n * it contains the same data as the previous one (possibly less data, but\n * the most recent bytes, or the same data and more free space in case the\n * buffer is enlarged). */\nvoid resizeReplicationBacklog(long long newsize) {\n    if (newsize < CONFIG_REPL_BACKLOG_MIN_SIZE)\n        newsize = CONFIG_REPL_BACKLOG_MIN_SIZE;\n    if (g_pserver->repl_backlog_size == newsize) return;\n\n    if (g_pserver->repl_backlog != NULL) {\n        std::unique_lock<fastlock> repl_backlog_lock(g_pserver->repl_backlog_lock);\n        /* What we actually do is to flush the old buffer and realloc a new\n         * empty one. It will refill with new data incrementally.\n         * The reason is that copying a few gigabytes adds latency and even\n         * worse often we need to alloc additional space before freeing the\n         * old buffer. */\n\n        /* get the critical client size, i.e. the size of the data unflushed to clients */\n        long long earliest_off = g_pserver->repl_lowest_off.load();\n\n        if (earliest_off != -1) {\n            char *backlog = nullptr;\n            newsize = std::max(newsize, g_pserver->master_repl_offset - earliest_off);\n            \n            if (cserver.repl_backlog_disk_size != 0) {\n                if (newsize > g_pserver->repl_backlog_config_size || cserver.force_backlog_disk) {\n                    if (g_pserver->repl_backlog == g_pserver->repl_backlog_disk)\n                        return; // Can't do anything more\n                    serverLog(LL_NOTICE, \"Switching to disk backed replication backlog due to exceeding memory limits\");\n                    backlog = g_pserver->repl_backlog_disk;\n                    newsize = cserver.repl_backlog_disk_size;\n                }\n            }\n\n            // We need to keep critical data so we can't shrink less than the hot data in the buffer\n            if (backlog == nullptr)\n                backlog = (char*)zmalloc(newsize);\n            g_pserver->repl_backlog_histlen = g_pserver->master_repl_offset - earliest_off;\n            long long earliest_idx = getReplIndexFromOffset(earliest_off);\n\n            if (g_pserver->repl_backlog_idx >= earliest_idx) {\n                auto cbActiveBacklog = g_pserver->repl_backlog_idx - earliest_idx;\n                memcpy(backlog, g_pserver->repl_backlog + earliest_idx, cbActiveBacklog);\n                serverAssert(g_pserver->repl_backlog_histlen == cbActiveBacklog);\n            } else {\n                auto cbPhase1 = g_pserver->repl_backlog_size - earliest_idx;\n                memcpy(backlog, g_pserver->repl_backlog + earliest_idx, cbPhase1);\n                memcpy(backlog + cbPhase1, g_pserver->repl_backlog, g_pserver->repl_backlog_idx);\n                auto cbActiveBacklog = cbPhase1 + g_pserver->repl_backlog_idx;\n                serverAssert(g_pserver->repl_backlog_histlen == cbActiveBacklog);\n            }\n            if (g_pserver->repl_backlog != g_pserver->repl_backlog_disk)\n                zfree(g_pserver->repl_backlog);\n            else\n                serverLog(LL_NOTICE, \"Returning to memory backed replication backlog\");\n            g_pserver->repl_backlog = backlog;\n            g_pserver->repl_backlog_idx = g_pserver->repl_backlog_histlen;\n            if (g_pserver->repl_batch_idxStart >= 0) {\n                g_pserver->repl_batch_idxStart -= earliest_idx;\n                if (g_pserver->repl_batch_idxStart < 0)\n                    g_pserver->repl_batch_idxStart += g_pserver->repl_backlog_size;\n            }\n            g_pserver->repl_backlog_start = earliest_off;\n        } else {\n            if (g_pserver->repl_backlog != g_pserver->repl_backlog_disk)\n                zfree(g_pserver->repl_backlog);\n            else\n                serverLog(LL_NOTICE, \"Returning to memory backed replication backlog\");\n            g_pserver->repl_backlog = (char*)zmalloc(newsize);\n            g_pserver->repl_backlog_histlen = 0;\n            g_pserver->repl_backlog_idx = 0;\n            /* Next byte we have is... the next since the buffer is empty. */\n            g_pserver->repl_backlog_off = g_pserver->master_repl_offset+1;\n            g_pserver->repl_backlog_start = g_pserver->master_repl_offset;\n        }\n    }\n    g_pserver->repl_backlog_size = newsize;\n}\n\n\nvoid freeReplicationBacklog(void) {\n    serverAssert(GlobalLocksAcquired());\n    listIter li;\n    listNode *ln;\n    listRewind(g_pserver->slaves, &li);\n    while ((ln = listNext(&li))) {\n        // g_pserver->slaves should be empty, or filled with clients pending close\n        client *c = (client*)listNodeValue(ln);\n        serverAssert(c->flags & CLIENT_CLOSE_ASAP || FMasterHost(c));\n    }\n    if (g_pserver->repl_backlog != g_pserver->repl_backlog_disk)\n        zfree(g_pserver->repl_backlog);\n    g_pserver->repl_backlog = NULL;\n}\n\n/* Add data to the replication backlog.\n * This function also increments the global replication offset stored at\n * g_pserver->master_repl_offset, because there is no case where we want to feed\n * the backlog without incrementing the offset. */\nvoid feedReplicationBacklog(const void *ptr, size_t len) {\n    serverAssert(GlobalLocksAcquired());\n    const unsigned char *p = (const unsigned char*)ptr;\n    std::unique_lock<fastlock> repl_backlog_lock(g_pserver->repl_backlog_lock, std::defer_lock);\n\n    if (g_pserver->repl_batch_idxStart >= 0) {\n        /* We are lower bounded by the lowest replica offset, or the batch offset start if not applicable */\n        long long lower_bound = g_pserver->repl_lowest_off.load(std::memory_order_seq_cst);\n        if (lower_bound == -1)\n            lower_bound = g_pserver->repl_batch_offStart;\n        long long minimumsize = g_pserver->master_repl_offset + len - lower_bound + 1;\n\n        if (minimumsize > g_pserver->repl_backlog_size) {\n            listIter li;\n            listNode *ln;\n            listRewind(g_pserver->slaves, &li);\n            long long maxClientBuffer = (long long)cserver.client_obuf_limits[CLIENT_TYPE_SLAVE].hard_limit_bytes;\n            if (maxClientBuffer <= 0)\n                maxClientBuffer = LLONG_MAX;    // infinite essentially\n            if (cserver.repl_backlog_disk_size)\n                maxClientBuffer = std::max(g_pserver->repl_backlog_size, cserver.repl_backlog_disk_size);\n            long long min_offset = LLONG_MAX;\n            int listening_replicas = 0;\n            while ((ln = listNext(&li))) {\n                client *replica = (client*)listNodeValue(ln);\n                if (!canFeedReplicaReplBuffer(replica)) continue;\n                if (replica->flags & CLIENT_CLOSE_ASAP) continue;\n\n                std::unique_lock<fastlock> ul(replica->lock);\n\n                // Would this client overflow?  If so close it\n                long long neededBuffer = g_pserver->master_repl_offset + len - replica->repl_curr_off + 1;\n                if (neededBuffer > maxClientBuffer) {\n                    \n                    sds clientInfo = catClientInfoString(sdsempty(),replica);\n                    freeClientAsync(replica);\n                    serverLog(LL_WARNING,\"Client %s scheduled to be closed ASAP due to exceeding output buffer hard limit.\", clientInfo);\n                    sdsfree(clientInfo);\n                    continue;\n                }\n                min_offset = std::min(min_offset, replica->repl_curr_off);\n                ++listening_replicas;\n            }\n\n            if (min_offset == LLONG_MAX) {\n                min_offset = g_pserver->repl_batch_offStart;\n                g_pserver->repl_lowest_off = -1;\n            } else {\n                g_pserver->repl_lowest_off = min_offset;\n            }\n\n            minimumsize = g_pserver->master_repl_offset + len - min_offset + 1;\n            serverAssert(listening_replicas == 0 || minimumsize <= maxClientBuffer);\n\n            if (minimumsize > g_pserver->repl_backlog_size && listening_replicas) {\n                // This is an emergency overflow, we better resize to fit\n                long long newsize = std::max(g_pserver->repl_backlog_size*2, minimumsize);\n                serverLog(LL_WARNING, \"Replication backlog is too small, resizing from %lld to %lld bytes\", g_pserver->repl_backlog_size, newsize);\n                resizeReplicationBacklog(newsize);\n            } else if (!listening_replicas) {\n                // We need to update a few variables or later asserts will notice we dropped data\n                g_pserver->repl_batch_offStart = g_pserver->master_repl_offset + len;\n                g_pserver->repl_lowest_off = -1;\n                if (!repl_backlog_lock.owns_lock())\n                    repl_backlog_lock.lock();   // we need to acquire the lock if we'll be overwriting data that writeToClient may be reading\n            }\n        }\n    }\n\n    g_pserver->master_repl_offset += len;\n\n    /* This is a circular buffer, so write as much data we can at every\n     * iteration and rewind the \"idx\" index if we reach the limit. */\n    \n    while(len) {\n        size_t thislen = g_pserver->repl_backlog_size - g_pserver->repl_backlog_idx;\n        if (thislen > len) thislen = len;\n        memcpy(g_pserver->repl_backlog+g_pserver->repl_backlog_idx,p,thislen);\n        g_pserver->repl_backlog_idx += thislen;\n        if (g_pserver->repl_backlog_idx == g_pserver->repl_backlog_size)\n            g_pserver->repl_backlog_idx = 0;\n        len -= thislen;\n        p += thislen;\n        g_pserver->repl_backlog_histlen += thislen;\n    }\n    if (g_pserver->repl_backlog_histlen > g_pserver->repl_backlog_size)\n        g_pserver->repl_backlog_histlen = g_pserver->repl_backlog_size;\n    /* Set the offset of the first byte we have in the backlog. */\n    g_pserver->repl_backlog_off = g_pserver->master_repl_offset -\n                              g_pserver->repl_backlog_histlen + 1;\n}\n\n/* Wrapper for feedReplicationBacklog() that takes Redis string objects\n * as input. */\nvoid feedReplicationBacklogWithObject(robj *o) {\n    char llstr[LONG_STR_SIZE];\n    void *p;\n    size_t len;\n\n    if (o->encoding == OBJ_ENCODING_INT) {\n        len = ll2string(llstr,sizeof(llstr),(long)ptrFromObj(o));\n        p = llstr;\n    } else {\n        len = sdslen((sds)ptrFromObj(o));\n        p = ptrFromObj(o);\n    }\n    feedReplicationBacklog(p,len);\n}\n\nsds catCommandForAofAndActiveReplication(sds buf, struct redisCommand *cmd, robj **argv, int argc);\n\nvoid replicationFeedSlave(client *replica, int dictid, robj **argv, int argc, bool fSendRaw)\n{\n    char llstr[LONG_STR_SIZE];\n    std::unique_lock<decltype(replica->lock)> lock(replica->lock);\n\n    /* Send SELECT command to every replica if needed. */\n    if (g_pserver->replicaseldb != dictid) {\n        robj *selectcmd;\n\n        /* For a few DBs we have pre-computed SELECT command. */\n        if (dictid >= 0 && dictid < PROTO_SHARED_SELECT_CMDS) {\n            selectcmd = shared.select[dictid];\n        } else {\n            int dictid_len;\n\n            dictid_len = ll2string(llstr,sizeof(llstr),dictid);\n            selectcmd = createObject(OBJ_STRING,\n                sdscatprintf(sdsempty(),\n                \"*2\\r\\n$6\\r\\nSELECT\\r\\n$%d\\r\\n%s\\r\\n\",\n                dictid_len, llstr));\n        }\n\n        /* Add the SELECT command into the backlog. */\n        /* We don't do this for advanced replication because this will be done later when it adds the whole RREPLAY command */\n        if (g_pserver->repl_backlog && fSendRaw) feedReplicationBacklogWithObject(selectcmd);\n\n        /* Send it to slaves */\n        addReply(replica,selectcmd);\n\n        if (dictid < 0 || dictid >= PROTO_SHARED_SELECT_CMDS)\n            decrRefCount(selectcmd);\n    }\n    g_pserver->replicaseldb = dictid;\n\n    /* Feed slaves that are waiting for the initial SYNC (so these commands\n     * are queued in the output buffer until the initial SYNC completes),\n     * or are already in sync with the master. */\n\n    if (fSendRaw)\n    {\n        /* Add the multi bulk length. */\n        addReplyArrayLen(replica,argc);\n\n        /* Finally any additional argument that was not stored inside the\n            * static buffer if any (from j to argc). */\n        for (int j = 0; j < argc; j++)\n            addReplyBulk(replica,argv[j]);\n    }\n    else\n    {\n        struct redisCommand *cmd = lookupCommand(szFromObj(argv[0]));\n        sds buf = catCommandForAofAndActiveReplication(sdsempty(), cmd, argv, argc);\n        addReplyProto(replica, buf, sdslen(buf));\n        sdsfree(buf);\n    }\n}\n\nstatic int writeProtoNum(char *dst, const size_t cchdst, long long num)\n{\n    if (cchdst < 1)\n        return 0;\n    dst[0] = '$';\n    int cch = 1;\n    cch += ll2string(dst + cch, cchdst - cch, digits10(num));\n    int chCpyT = std::min<int>(cchdst - cch, 2);\n    memcpy(dst + cch, \"\\r\\n\", chCpyT);\n    cch += chCpyT;\n    cch += ll2string(dst + cch, cchdst-cch, num);\n    chCpyT = std::min<int>(cchdst - cch, 3);\n    memcpy(dst + cch, \"\\r\\n\", chCpyT);\n    if (chCpyT == 3)\n        cch += 2;\n    else\n        cch += chCpyT;\n    return cch;\n}\n\nint canFeedReplicaReplBuffer(client *replica) {\n    /* Don't feed replicas that only want the RDB. */\n    if (replica->flags & CLIENT_REPL_RDBONLY) return 0;\n\n    /* Don't feed replicas that are still waiting for BGSAVE to start. */\n    if (replica->replstate == SLAVE_STATE_WAIT_BGSAVE_START) return 0;\n\n    return 1;\n}\n\n/* Propagate write commands to slaves, and populate the replication backlog\n * as well. This function is used if the instance is a master: we use\n * the commands received by our clients in order to create the replication\n * stream. Instead if the instance is a replica and has sub-slaves attached,\n * we use replicationFeedSlavesFromMasterStream() */\nvoid replicationFeedSlavesCore(list *slaves, int dictid, robj **argv, int argc) {\n    int j;\n    serverAssert(GlobalLocksAcquired());\n    serverAssert(g_pserver->repl_batch_offStart >= 0);\n\n    if (dictid < 0)\n        dictid = 0; // this can happen if we send a PING before any real operation\n\n    /* If the instance is not a top level master, return ASAP: we'll just proxy\n     * the stream of data we receive from our master instead, in order to\n     * propagate *identical* replication stream. In this way this replica can\n     * advertise the same replication ID as the master (since it shares the\n     * master replication history and has the same backlog and offsets). */\n    if (!g_pserver->fActiveReplica && listLength(g_pserver->masters)) return;\n\n    /* If there aren't slaves, and there is no backlog buffer to populate,\n     * we can return ASAP. */\n    if (g_pserver->repl_backlog == NULL && listLength(slaves) == 0) return;\n\n    /* We can't have slaves attached and no backlog. */\n    serverAssert(!(listLength(slaves) != 0 && g_pserver->repl_backlog == NULL));\n\n    bool fSendRaw = !g_pserver->fActiveReplica;\n\n    /* Send SELECT command to every replica if needed. */\n    if (g_pserver->replicaseldb != dictid) {\n        char llstr[LONG_STR_SIZE];\n        robj *selectcmd;\n\n        /* For a few DBs we have pre-computed SELECT command. */\n        if (dictid >= 0 && dictid < PROTO_SHARED_SELECT_CMDS) {\n            selectcmd = shared.select[dictid];\n        } else {\n            int dictid_len;\n\n            dictid_len = ll2string(llstr,sizeof(llstr),dictid);\n            selectcmd = createObject(OBJ_STRING,\n                sdscatprintf(sdsempty(),\n                \"*2\\r\\n$6\\r\\nSELECT\\r\\n$%d\\r\\n%s\\r\\n\",\n                dictid_len, llstr));\n        }\n\n        /* Add the SELECT command into the backlog. */\n        /* We don't do this for advanced replication because this will be done later when it adds the whole RREPLAY command */\n        if (g_pserver->repl_backlog && fSendRaw) feedReplicationBacklogWithObject(selectcmd);\n\n        if (dictid < 0 || dictid >= PROTO_SHARED_SELECT_CMDS)\n            decrRefCount(selectcmd);\n    }\n    g_pserver->replicaseldb = dictid;\n\n    /* Write the command to the replication backlog if any. */\n    if (g_pserver->repl_backlog) \n    {\n        if (fSendRaw)\n        {\n            char aux[LONG_STR_SIZE+3];\n            /* Add the multi bulk reply length. */\n            aux[0] = '*';\n            int multilen = ll2string(aux+1,sizeof(aux)-1,argc);\n            aux[multilen+1] = '\\r';\n            aux[multilen+2] = '\\n';\n\n            feedReplicationBacklog(aux,multilen+3);\n\n            for (j = 0; j < argc; j++) {\n                long objlen = stringObjectLen(argv[j]);\n\n                /* We need to feed the buffer with the object as a bulk reply\n                * not just as a plain string, so create the $..CRLF payload len\n                * and add the final CRLF */\n                aux[0] = '$';\n                int len = ll2string(aux+1,sizeof(aux)-1,objlen);\n                aux[len+1] = '\\r';\n                aux[len+2] = '\\n';\n                feedReplicationBacklog(aux,len+3);\n                feedReplicationBacklogWithObject(argv[j]);\n                feedReplicationBacklog(aux+len+1,2);\n            }\n        }\n        else\n        {\n            char szDbNum[128];\n            int cchDbNum = 0;\n            if (!fSendRaw)\n                cchDbNum = writeProtoNum(szDbNum, sizeof(szDbNum), dictid);\n            \n\n            char szMvcc[128];\n            int cchMvcc = 0;\n            incrementMvccTstamp();\t// Always increment MVCC tstamp so we're consistent with active and normal replication\n            if (!fSendRaw)\n                cchMvcc = writeProtoNum(szMvcc, sizeof(szMvcc), getMvccTstamp());\n\n            //size_t cchlen = multilen+3;\n            struct redisCommand *cmd = lookupCommand(szFromObj(argv[0]));\n            sds buf = catCommandForAofAndActiveReplication(sdsempty(), cmd, argv, argc);\n            size_t cchlen = sdslen(buf);\n\n            // The code below used to be: snprintf(proto, sizeof(proto), \"*5\\r\\n$7\\r\\nRREPLAY\\r\\n$%d\\r\\n%s\\r\\n$%lld\\r\\n\", (int)strlen(uuid), uuid, cchbuf);\n            //  but that was much too slow\n            static const char *protoRREPLAY = \"*5\\r\\n$7\\r\\nRREPLAY\\r\\n$36\\r\\n00000000-0000-0000-0000-000000000000\\r\\n$\";\n            char proto[1024];\n            int cchProto = 0;\n            if (!fSendRaw)\n            {\n                char uuid[37];\n                uuid_unparse(cserver.uuid, uuid);\n\n                cchProto = strlen(protoRREPLAY);\n                memcpy(proto, protoRREPLAY, strlen(protoRREPLAY));\n                memcpy(proto + 22, uuid, 36); // Note UUID_STR_LEN includes the \\0 trailing byte which we don't want\n                cchProto += ll2string(proto + cchProto, sizeof(proto)-cchProto, cchlen);\n                memcpy(proto + cchProto, \"\\r\\n\", 3);\n                cchProto += 2;\n            }\n\n\n            feedReplicationBacklog(proto, cchProto);            \n            feedReplicationBacklog(buf, sdslen(buf));\n\n            const char *crlf = \"\\r\\n\";\n            feedReplicationBacklog(crlf, 2);\n            feedReplicationBacklog(szDbNum, cchDbNum);\n            feedReplicationBacklog(szMvcc, cchMvcc);\n\n            sdsfree(buf);\n        }\n    }\n}\n\nvoid replicationFeedSlaves(list *replicas, int dictid, robj **argv, int argc) {\n    runAndPropogateToReplicas(replicationFeedSlavesCore, replicas, dictid, argv, argc);\n}\n\n/* This is a debugging function that gets called when we detect something\n * wrong with the replication protocol: the goal is to peek into the\n * replication backlog and show a few final bytes to make simpler to\n * guess what kind of bug it could be. */\nvoid showLatestBacklog(void) {\n    if (g_pserver->repl_backlog == NULL) return;\n\n    long long dumplen = 256;\n    if (g_pserver->repl_backlog_histlen < dumplen)\n        dumplen = g_pserver->repl_backlog_histlen;\n\n    /* Identify the first byte to dump. */\n    long long idx =\n      (g_pserver->repl_backlog_idx + (g_pserver->repl_backlog_size - dumplen)) %\n       g_pserver->repl_backlog_size;\n\n    /* Scan the circular buffer to collect 'dumplen' bytes. */\n    sds dump = sdsempty();\n    while(dumplen) {\n        long long thislen =\n            ((g_pserver->repl_backlog_size - idx) < dumplen) ?\n            (g_pserver->repl_backlog_size - idx) : dumplen;\n\n        dump = sdscatrepr(dump,g_pserver->repl_backlog+idx,thislen);\n        dumplen -= thislen;\n        idx = 0;\n    }\n\n    /* Finally log such bytes: this is vital debugging info to\n     * understand what happened. */\n    serverLog(LL_WARNING,\"Latest backlog is: '%s'\", dump);\n    sdsfree(dump);\n}\n\n/* This function is used in order to proxy what we receive from our master\n * to our sub-slaves. */\n#include <ctype.h>\nvoid replicationFeedSlavesFromMasterStream(char *buf, size_t buflen) {\n    /* Debugging: this is handy to see the stream sent from master\n     * to slaves. Disabled with if(0). */\n    if (0) {\n        printf(\"%zu:\",buflen);\n        for (size_t j = 0; j < buflen; j++) {\n            printf(\"%c\", isprint(buf[j]) ? buf[j] : '.');\n        }\n        printf(\"\\n\");\n    }\n\n    if (g_pserver->repl_backlog) feedReplicationBacklog(buf,buflen);\n}\n\nvoid replicationFeedMonitors(client *c, list *monitors, int dictid, robj **argv, int argc) {\n    if (!(listLength(g_pserver->monitors) && !g_pserver->loading)) return;\n    listNode *ln;\n    listIter li;\n    int j;\n    sds cmdrepr = sdsnew(\"+\");\n    robj *cmdobj;\n    struct timeval tv;\n    serverAssert(GlobalLocksAcquired());\n\n    gettimeofday(&tv,NULL);\n    cmdrepr = sdscatprintf(cmdrepr,\"%ld.%06ld \",(long)tv.tv_sec,(long)tv.tv_usec);\n    if (c->flags & CLIENT_LUA) {\n        cmdrepr = sdscatprintf(cmdrepr,\"[%d lua] \",dictid);\n    } else if (c->flags & CLIENT_UNIX_SOCKET) {\n        cmdrepr = sdscatprintf(cmdrepr,\"[%d unix:%s] \",dictid,g_pserver->unixsocket);\n    } else {\n        cmdrepr = sdscatprintf(cmdrepr,\"[%d %s] \",dictid,getClientPeerId(c));\n    }\n\n    for (j = 0; j < argc; j++) {\n        if (argv[j]->encoding == OBJ_ENCODING_INT) {\n            cmdrepr = sdscatprintf(cmdrepr, \"\\\"%ld\\\"\", (long)ptrFromObj(argv[j]));\n        } else {\n            cmdrepr = sdscatrepr(cmdrepr,(char*)ptrFromObj(argv[j]),\n                        sdslen((sds)ptrFromObj(argv[j])));\n        }\n        if (j != argc-1)\n            cmdrepr = sdscatlen(cmdrepr,\" \",1);\n    }\n    cmdrepr = sdscatlen(cmdrepr,\"\\r\\n\",2);\n    cmdobj = createObject(OBJ_STRING,cmdrepr);\n\n    listRewind(monitors,&li);\n    while((ln = listNext(&li))) {\n        client *monitor = (client*)ln->value;\n\t\tstd::unique_lock<decltype(monitor->lock)> lock(monitor->lock, std::defer_lock);\n\t\t// When writing to clients on other threads the global lock is sufficient provided we only use AddReply*Async()\n\t\tif (FCorrectThread(c))\n\t\t\tlock.lock();\n        addReply(monitor,cmdobj);\n    }\n    decrRefCount(cmdobj);\n}\n\nint prepareClientToWrite(client *c);\n\n/* Feed the replica 'c' with the replication backlog starting from the\n * specified 'offset' up to the end of the backlog. */\nlong long addReplyReplicationBacklog(client *c, long long offset) {\n    long long skip, len;\n\n    serverLog(LL_DEBUG, \"[PSYNC] Replica request offset: %lld\", offset);\n\n    if (g_pserver->repl_backlog_histlen == 0) {\n        serverLog(LL_DEBUG, \"[PSYNC] Backlog history len is zero\");\n        c->repl_curr_off = g_pserver->master_repl_offset;\n        c->repl_end_off = g_pserver->master_repl_offset;\n        return 0;\n    }\n\n    serverLog(LL_DEBUG, \"[PSYNC] Backlog size: %lld\",\n             g_pserver->repl_backlog_size);\n    serverLog(LL_DEBUG, \"[PSYNC] First byte: %lld\",\n             g_pserver->repl_backlog_off);\n    serverLog(LL_DEBUG, \"[PSYNC] History len: %lld\",\n             g_pserver->repl_backlog_histlen);\n    serverLog(LL_DEBUG, \"[PSYNC] Current index: %lld\",\n             g_pserver->repl_backlog_idx);\n\n    /* Compute the amount of bytes we need to discard. */\n    skip = offset - g_pserver->repl_backlog_off;\n    serverLog(LL_DEBUG, \"[PSYNC] Skipping: %lld\", skip);\n\n    len = g_pserver->repl_backlog_histlen - skip;\n    serverLog(LL_DEBUG, \"[PSYNC] Reply total length: %lld\", len);\n\n    /* Set the start and end offsets for the replica so that a future\n     * writeToClient will send the backlog from the given offset to \n     * the current end of the backlog to said replica */\n    c->repl_curr_off = offset - 1;\n    c->repl_end_off = g_pserver->master_repl_offset;\n\n    /* Force the partial sync to be queued */\n    prepareClientToWrite(c);\n\n    return len;\n}\n\n/* Return the offset to provide as reply to the PSYNC command received\n * from the replica. The returned value is only valid immediately after\n * the BGSAVE process started and before executing any other command\n * from clients. */\nlong long getPsyncInitialOffset(void) {\n    return g_pserver->master_repl_offset;\n}\n\n/* Send a FULLRESYNC reply in the specific case of a full resynchronization,\n * as a side effect setup the replica for a full sync in different ways:\n *\n * 1) Remember, into the replica client structure, the replication offset\n *    we sent here, so that if new slaves will later attach to the same\n *    background RDB saving process (by duplicating this client output\n *    buffer), we can get the right offset from this replica.\n * 2) Set the replication state of the replica to WAIT_BGSAVE_END so that\n *    we start accumulating differences from this point.\n * 3) Force the replication stream to re-emit a SELECT statement so\n *    the new replica incremental differences will start selecting the\n *    right database number.\n *\n * Normally this function should be called immediately after a successful\n * BGSAVE for replication was started, or when there is one already in\n * progress that we attached our replica to. */\nint replicationSetupSlaveForFullResync(client *replica, long long offset) {\n    char buf[128];\n    int buflen;\n\n    replica->psync_initial_offset = offset;\n    replica->replstate = SLAVE_STATE_WAIT_BGSAVE_END;\n\n    replica->repl_curr_off = offset;\n    replica->repl_end_off = g_pserver->master_repl_offset;\n\n    /* We are going to accumulate the incremental changes for this\n     * replica as well. Set replicaseldb to -1 in order to force to re-emit\n     * a SELECT statement in the replication stream. */\n    g_pserver->replicaseldb = -1;\n\n    /* Don't send this reply to slaves that approached us with\n     * the old SYNC command. */\n    if (!(replica->flags & CLIENT_PRE_PSYNC)) {\n        buflen = snprintf(buf,sizeof(buf),\"+FULLRESYNC %s %lld\\r\\n\",\n                          g_pserver->replid,offset);\n        if (connWrite(replica->conn,buf,buflen) != buflen) {\n            freeClientAsync(replica);\n            return C_ERR;\n        }\n    }\n    return C_OK;\n}\n\n/* This function handles the PSYNC command from the point of view of a\n * master receiving a request for partial resynchronization.\n *\n * On success return C_OK, otherwise C_ERR is returned and we proceed\n * with the usual full resync. */\nint masterTryPartialResynchronization(client *c) {\n    serverAssert(GlobalLocksAcquired());\n    long long psync_offset, psync_len;\n    char *master_replid = (char*)ptrFromObj(c->argv[1]);\n    char buf[128];\n    int buflen;\n\n    /* Parse the replication offset asked by the replica. Go to full sync\n     * on parse error: this should never happen but we try to handle\n     * it in a robust way compared to aborting. */\n    if (getLongLongFromObjectOrReply(c,c->argv[2],&psync_offset,NULL) !=\n       C_OK) goto need_full_resync;\n\n    /* Is the replication ID of this master the same advertised by the wannabe\n     * replica via PSYNC? If the replication ID changed this master has a\n     * different replication history, and there is no way to continue.\n     *\n     * Note that there are two potentially valid replication IDs: the ID1\n     * and the ID2. The ID2 however is only valid up to a specific offset. */\n    if (strcasecmp(master_replid, g_pserver->replid) &&\n        (strcasecmp(master_replid, g_pserver->replid2) ||\n         psync_offset > g_pserver->second_replid_offset))\n    {\n        /* Replid \"?\" is used by slaves that want to force a full resync. */\n        if (master_replid[0] != '?') {\n            if (strcasecmp(master_replid, g_pserver->replid) &&\n                strcasecmp(master_replid, g_pserver->replid2))\n            {\n                serverLog(LL_NOTICE,\"Partial resynchronization not accepted: \"\n                    \"Replication ID mismatch (Replica asked for '%s', my \"\n                    \"replication IDs are '%s' and '%s')\",\n                    master_replid, g_pserver->replid, g_pserver->replid2);\n            } else {\n                serverLog(LL_NOTICE,\"Partial resynchronization not accepted: \"\n                    \"Requested offset for second ID was %lld, but I can reply \"\n                    \"up to %lld\", psync_offset, g_pserver->second_replid_offset);\n            }\n        } else {\n            serverLog(LL_NOTICE,\"Full resync requested by replica %s\",\n                replicationGetSlaveName(c));\n        }\n        goto need_full_resync;\n    }\n\n    /* We still have the data our replica is asking for? */\n    if (!g_pserver->repl_backlog ||\n        psync_offset < g_pserver->repl_backlog_off ||\n        psync_offset > (g_pserver->repl_backlog_off + g_pserver->repl_backlog_histlen))\n    {\n        serverLog(LL_NOTICE,\n            \"Unable to partial resync with replica %s for lack of backlog (Replica request was: %lld).\", replicationGetSlaveName(c), psync_offset);\n        if (psync_offset > g_pserver->master_repl_offset) {\n            serverLog(LL_WARNING,\n                \"Warning: replica %s tried to PSYNC with an offset that is greater than the master replication offset.\", replicationGetSlaveName(c));\n        }\n        goto need_full_resync;\n    }\n\n    /* If we reached this point, we are able to perform a partial resync:\n     * 1) Set client state to make it a replica.\n     * 2) Inform the client we can continue with +CONTINUE\n     * 3) Send the backlog data (from the offset to the end) to the replica. */\n    c->flags |= CLIENT_SLAVE;\n    c->replstate = SLAVE_STATE_ONLINE;\n    c->repl_ack_time = g_pserver->unixtime;\n    c->repl_put_online_on_ack = 0;\n    listAddNodeTail(g_pserver->slaves,c);\n    g_pserver->rgthreadvar[c->iel].cclientsReplica++;\n\n    /* We can't use the connection buffers since they are used to accumulate\n     * new commands at this stage. But we are sure the socket send buffer is\n     * empty so this write will never fail actually. */\n    if (c->slave_capa & SLAVE_CAPA_PSYNC2) {\n        buflen = snprintf(buf,sizeof(buf),\"+CONTINUE %s\\r\\n\", g_pserver->replid);\n    } else {\n        buflen = snprintf(buf,sizeof(buf),\"+CONTINUE\\r\\n\");\n    }\n    if (connWrite(c->conn,buf,buflen) != buflen) {\n        freeClientAsync(c);\n        return C_OK;\n    }\n    psync_len = addReplyReplicationBacklog(c,psync_offset);\n    serverLog(LL_NOTICE,\n        \"Partial resynchronization request from %s accepted. Sending %lld bytes of backlog starting from offset %lld.\",\n            replicationGetSlaveName(c),\n            psync_len, psync_offset);\n    /* Note that we don't need to set the selected DB at g_pserver->replicaseldb\n     * to -1 to force the master to emit SELECT, since the replica already\n     * has this state from the previous connection with the master. */\n\n    refreshGoodSlavesCount();\n\n    /* Fire the replica change modules event. */\n    moduleFireServerEvent(REDISMODULE_EVENT_REPLICA_CHANGE,\n                          REDISMODULE_SUBEVENT_REPLICA_CHANGE_ONLINE,\n                          NULL);\n\n    return C_OK; /* The caller can return, no full resync needed. */\n\nneed_full_resync:\n    /* We need a full resync for some reason... Note that we can't\n     * reply to PSYNC right now if a full SYNC is needed. The reply\n     * must include the master offset at the time the RDB file we transfer\n     * is generated, so we need to delay the reply to that moment. */\n    return C_ERR;\n}\n\nint checkClientOutputBufferLimits(client *c);\nvoid clientInstallAsyncWriteHandler(client *c);\nclass replicationBuffer {\n    std::vector<client *> replicas;\n    clientReplyBlock *reply = nullptr;\n    size_t writtenBytesTracker = 0;\n\npublic:\n    replicationBuffer() {\n        reply = (clientReplyBlock*)zmalloc(sizeof(clientReplyBlock) + (PROTO_REPLY_CHUNK_BYTES*2));\n        reply->size = zmalloc_usable_size(reply) - sizeof(clientReplyBlock);\n        reply->used = 0;\n    }\n\n    ~replicationBuffer() {\n        zfree(reply);\n    }\n\n    void addReplica(client *replica) {\n        replicas.push_back(replica);\n        replicationSetupSlaveForFullResync(replica,getPsyncInitialOffset());\n    }\n\n    bool isActive() const { return !replicas.empty(); }\n\n    void flushData() {\n        if (reply == nullptr)\n            return;\n        size_t written = reply->used;\n\n        for (auto replica : replicas) {\n            std::unique_lock<fastlock> ul(replica->lock);\n            while (checkClientOutputBufferLimits(replica)\n              && (replica->flags.load(std::memory_order_relaxed) & CLIENT_CLOSE_ASAP) == 0) {\n                ul.unlock();\n                usleep(0);\n                ul.lock();\n            }\n        }\n\n        aeAcquireLock();\n        for (size_t ireplica = 0; ireplica < replicas.size(); ++ireplica) {\n            auto replica = replicas[ireplica];\n            if (replica->flags.load(std::memory_order_relaxed) & CLIENT_CLOSE_ASAP) {\n                replica->replstate = REPL_STATE_NONE;\n                continue;\n            }\n            \n            std::unique_lock<fastlock> lock(replica->lock);\n            if (ireplica == (replicas.size()-1) && replica->replyAsync == nullptr) {\n                replica->replyAsync = reply;\n                reply = nullptr;\n                if (!(replica->fPendingAsyncWrite)) clientInstallAsyncWriteHandler(replica);\n            } else {\n                addReplyProto(replica, reply->buf(), reply->used);\n            }\n        }\n        ProcessPendingAsyncWrites();\n        for (auto c : replicas) {\n            if (c->flags & CLIENT_CLOSE_ASAP) {\n                std::unique_lock<fastlock> ul(c->lock);\n                c->replstate = REPL_STATE_NONE;   // otherwise the client can't be free'd\n            }\n        }\n        replicas.erase(std::remove_if(replicas.begin(), replicas.end(), [](const client *c)->bool{ return c->flags.load(std::memory_order_relaxed) & CLIENT_CLOSE_ASAP;}), replicas.end());\n        aeReleaseLock();\n        if (reply != nullptr) {\n            reply->used = 0;\n        }\n        writtenBytesTracker += written;\n    }\n\n    void addData(const char *data, unsigned long size) {\n        if (reply != nullptr && (size + reply->used) > reply->size)\n            flushData();\n\n        if (reply != nullptr && reply->size < size) {\n            serverAssert(reply->used == 0); // flush should have happened\n            zfree(reply);\n            reply = nullptr;\n        }\n        \n        if (reply == nullptr) {\n            reply = (clientReplyBlock*)zmalloc(sizeof(clientReplyBlock) + std::max(size, (unsigned long)(PROTO_REPLY_CHUNK_BYTES*64)));\n            reply->size = zmalloc_usable_size(reply) - sizeof(clientReplyBlock);\n            reply->used = 0;\n        }\n\n        serverAssert((reply->size - reply->used) >= size);\n        memcpy(reply->buf() + reply->used, data, size);\n        reply->used += size;\n    }\n\n    void addLongLongWithPrefix(long long val, char prefix) {\n        char buf[128];\n        int len;\n\n        buf[0] = prefix;\n        len = ll2string(buf+1,sizeof(buf)-1,val);\n        buf[len+1] = '\\r';\n        buf[len+2] = '\\n';\n        addData(buf, len+3);\n    }\n\n    void addArrayLen(int len) {\n        addLongLongWithPrefix(len, '*');\n    }\n\n    void addLong(long val) {\n        addLongLongWithPrefix(val, ':');\n    }\n\n    void addLongLong(long long val) {\n        addLongLongWithPrefix(val, ':');\n    }\n\n    void addString(const char *s, unsigned long len) {\n        addLongLongWithPrefix(len, '$');\n        addData(s, len);\n        addData(\"\\r\\n\", 2);\n    }\n\n    size_t cbWritten() const { return writtenBytesTracker; }\n\n    void end() {\n        flushData();\n        for (auto replica : replicas) {\n            // Return to original settings\n            if (!g_pserver->repl_disable_tcp_nodelay)\n                connEnableTcpNoDelay(replica->conn);\n        }\n    }\n\n    void putSlavesOnline() {\n        for (auto replica : replicas) {\n            replica->replstate = SLAVE_STATE_FASTSYNC_DONE;\n            replica->repl_put_online_on_ack = 1;\n        }\n    }\n\n    void abort() {\n        for (auto replica : replicas) {\n            // Close the connection to force a resync\n            freeClientAsync(replica);\n        }\n        replicas.clear();\n    }\n};\n\nint rdbSaveSnapshotForReplication(rdbSaveInfo *rsi) {\n    // TODO: This needs to be on a background thread\n    int retval = C_OK;\n    serverAssert(GlobalLocksAcquired());\n    serverLog(LL_NOTICE, \"Starting fast full sync with target: %s\", \"disk\");\n\n    std::shared_ptr<replicationBuffer> spreplBuf = std::make_shared<replicationBuffer>();\n    listNode *ln;\n    listIter li;\n    listRewind(g_pserver->slaves, &li);\n    while ((ln = listNext(&li))) {\n        client *replicaCur = (client*)listNodeValue(ln);\n        if ((replicaCur->slave_capa & SLAVE_CAPA_KEYDB_FASTSYNC) && (replicaCur->replstate == SLAVE_STATE_WAIT_BGSAVE_START)) {\n            spreplBuf->addReplica(replicaCur);\n            replicaCur->replstate = SLAVE_STATE_FASTSYNC_TX;\n        }\n    }\n\n    spreplBuf->addArrayLen(2);   // Two sections: Metadata and databases\n\n    // MetaData\n    aeAcquireLock();\n    spreplBuf->addArrayLen(3 + dictSize(g_pserver->lua_scripts));\n        spreplBuf->addArrayLen(2);\n            spreplBuf->addString(\"repl-stream-db\", 14);\n            spreplBuf->addLong(rsi->repl_stream_db);\n        spreplBuf->addArrayLen(2);\n            spreplBuf->addString(\"repl-id\", 7);\n            spreplBuf->addString(rsi->repl_id, CONFIG_RUN_ID_SIZE);\n        spreplBuf->addArrayLen(2);\n            spreplBuf->addString(\"repl-offset\", 11);\n            spreplBuf->addLongLong(rsi->master_repl_offset);\n\n    if (dictSize(g_pserver->lua_scripts)) {\n        dictEntry *de;\n        auto di = dictGetIterator(g_pserver->lua_scripts);\n        while((de = dictNext(di)) != NULL) {\n            robj *body = (robj*)dictGetVal(de);\n\n            spreplBuf->addArrayLen(2);\n            spreplBuf->addString(\"lua\", 3);\n            spreplBuf->addString(szFromObj(body), sdslen(szFromObj(body)));\n        }\n        dictReleaseIterator(di);\n        di = NULL; /* So that we don't release it again on error. */\n    }\n\n    std::shared_ptr<std::vector<const redisDbPersistentDataSnapshot*>> spvecsnapshot = std::make_shared<std::vector<const redisDbPersistentDataSnapshot*>>();\n    for (int idb = 0; idb < cserver.dbnum; ++idb) {\n        spvecsnapshot->emplace_back(g_pserver->db[idb]->createSnapshot(getMvccTstamp(), false));\n    }\n    aeReleaseLock();\n\n    g_pserver->asyncworkqueue->AddWorkFunction([spreplBuf = std::move(spreplBuf), spvecsnapshot = std::move(spvecsnapshot)]{\n        int retval = C_OK;\n        auto timeStart = ustime();\n        auto lastLogTime = timeStart;\n        size_t cbData = 0;\n        size_t cbLastUpdate = 0;\n        auto &replBuf = *spreplBuf;\n        // Databases\n        replBuf.addArrayLen(cserver.dbnum);\n        for (int idb = 0; idb < cserver.dbnum; ++idb) {\n            auto &spsnapshot = (*spvecsnapshot)[idb];\n            size_t snapshotDeclaredCount = spsnapshot->size();\n            replBuf.addArrayLen(snapshotDeclaredCount);\n            size_t count = 0;\n            bool result = spsnapshot->iterate_threadsafe([&replBuf, &count, &cbData, &lastLogTime, &cbLastUpdate](const char *strKey, robj_roptr o) -> bool{\n                replBuf.addArrayLen(2);\n\n                replBuf.addString(strKey, sdslen(strKey));\n                sds strT = serializeStoredObjectAndExpire(o);\n                replBuf.addString(strT, sdslen(strT));\n                ++count;\n                if ((count % 8092) == 0) {\n                    auto curTime = ustime();\n                    if ((curTime - lastLogTime) > 60000000) {\n                        auto usec = curTime - lastLogTime;\n                        serverLog(LL_NOTICE, \"Replication status: Transferred %zuMB (%.2fGbit/s)\", replBuf.cbWritten()/1024/1024, ((replBuf.cbWritten()-cbLastUpdate)*8.0)/(usec/1000000.0)/1000000000.0);\n                        cbLastUpdate = replBuf.cbWritten();\n                        lastLogTime = ustime();\n                    }\n                }\n                cbData += sdslen(strKey) + sdslen(strT);\n                sdsfree(strT);\n                return replBuf.isActive();\n            });\n\n            if (!result) {\n                retval = C_ERR;\n                break;\n            }\n            if (count != snapshotDeclaredCount) {\n                serverLog(LL_WARNING, \"Replication BUG: Count of keys sent does not match actual count.  Aborting full sync.\");\n                replBuf.abort();\n                break;\n            }\n        }\n        \n        replBuf.end();\n\n        if (!replBuf.isActive()) {\n            retval = C_ERR;\n        }\n\n        serverLog(LL_NOTICE, \"Snapshot replication done: %s\", (retval == C_OK) ? \"success\" : \"failed\");\n        auto usec = ustime() - timeStart;\n        serverLog(LL_NOTICE, \"Transferred %zuMB total (%zuMB data) in %.2f seconds.  (%.2fGbit/s)\", spreplBuf->cbWritten()/1024/1024, cbData/1024/1024, usec/1000000.0, (spreplBuf->cbWritten()*8.0)/(usec/1000000.0)/1000000000.0);\n        if (retval == C_OK) {\n            aeAcquireLock();\n            replBuf.putSlavesOnline();\n            aeReleaseLock();\n        }\n    });\n\n    return retval;\n}\n\n/* Start a BGSAVE for replication goals, which is, selecting the disk or\n * socket target depending on the configuration, and making sure that\n * the script cache is flushed before to start.\n *\n * The mincapa argument is the bitwise AND among all the slaves capabilities\n * of the slaves waiting for this BGSAVE, so represents the replica capabilities\n * all the slaves support. Can be tested via SLAVE_CAPA_* macros.\n *\n * Side effects, other than starting a BGSAVE:\n *\n * 1) Handle the slaves in WAIT_START state, by preparing them for a full\n *    sync if the BGSAVE was successfully started, or sending them an error\n *    and dropping them from the list of slaves.\n *\n * 2) Flush the Lua scripting script cache if the BGSAVE was actually\n *    started.\n *\n * Returns C_OK on success or C_ERR otherwise. */\nint startBgsaveForReplication(int mincapa) {\n    serverAssert(GlobalLocksAcquired());\n    int retval;\n    int socket_target = g_pserver->repl_diskless_sync && (mincapa & SLAVE_CAPA_EOF);\n    listIter li;\n    listNode *ln;\n\n    if (g_pserver->loading && g_pserver->fActiveReplica) {\n        serverLog(LL_NOTICE, \"Can't bgsave while loading in active replication mode\");\n        return C_ERR;\n    }\n\n    serverLog(LL_NOTICE,\"Starting BGSAVE for SYNC with target: %s\",\n        socket_target ? \"replicas sockets\" : \"disk\");\n\n    rdbSaveInfo rsi, *rsiptr;\n    rsiptr = rdbPopulateSaveInfo(&rsi);\n    /* Only do rdbSave* when rsiptr is not NULL,\n     * otherwise replica will miss repl-stream-db. */\n    if (rsiptr) {\n        if (mincapa & SLAVE_CAPA_KEYDB_FASTSYNC && FFastSyncEnabled())\n            retval = rdbSaveSnapshotForReplication(rsiptr);\n        else if (socket_target)\n            retval = rdbSaveToSlavesSockets(rsiptr);\n        else\n            retval = rdbSaveBackground(rsiptr);\n    } else {\n        serverLog(LL_WARNING,\"BGSAVE for replication: replication information not available, can't generate the RDB file right now. Try later.\");\n        retval = C_ERR;\n    }\n\n    /* If we succeeded to start a BGSAVE with disk target, let's remember\n     * this fact, so that we can later delete the file if needed. Note\n     * that we don't set the flag to 1 if the feature is disabled, otherwise\n     * it would never be cleared: the file is not deleted. This way if\n     * the user enables it later with CONFIG SET, we are fine. */\n    if (retval == C_OK && !socket_target && g_pserver->rdb_del_sync_files)\n        RDBGeneratedByReplication = 1;\n\n    /* If we failed to BGSAVE, remove the slaves waiting for a full\n     * resynchronization from the list of slaves, inform them with\n     * an error about what happened, close the connection ASAP. */\n    if (retval == C_ERR) {\n        serverLog(LL_WARNING,\"BGSAVE for replication failed\");\n        listRewind(g_pserver->slaves,&li);\n        while((ln = listNext(&li))) {\n            client *replica = (client*)ln->value;\n            std::unique_lock<decltype(replica->lock)> lock(replica->lock);\n\n            if (replica->replstate == SLAVE_STATE_WAIT_BGSAVE_START) {\n                replica->replstate = REPL_STATE_NONE;\n                replica->flags &= ~CLIENT_SLAVE;\n                listDelNode(g_pserver->slaves,ln);\n                g_pserver->rgthreadvar[replica->iel].cclientsReplica--;\n                addReplyError(replica,\n                    \"BGSAVE failed, replication can't continue\");\n                replica->flags |= CLIENT_CLOSE_AFTER_REPLY;\n            }\n        }\n        return retval;\n    }\n\n    /* If the target is socket, rdbSaveToSlavesSockets() already setup\n     * the slaves for a full resync. Otherwise for disk target do it now.*/\n    if (!socket_target) {\n        listRewind(g_pserver->slaves,&li);\n        while((ln = listNext(&li))) {\n            client *replica = (client*)ln->value;\n            std::unique_lock<decltype(replica->lock)> lock(replica->lock);\n\n            if (replica->replstate == SLAVE_STATE_WAIT_BGSAVE_START) {\n                    replicationSetupSlaveForFullResync(replica,\n                            getPsyncInitialOffset());\n            }\n        }\n    }\n\n    /* Flush the script cache, since we need that replica differences are\n     * accumulated without requiring slaves to match our cached scripts. */\n    if (retval == C_OK) replicationScriptCacheFlush();\n    return retval;\n}\n\n/* SYNC and PSYNC command implementation. */\nvoid syncCommand(client *c) {\n    /* ignore SYNC if already replica or in monitor mode */\n    if (c->flags & CLIENT_SLAVE) return;\n\n    /* Check if this is a failover request to a replica with the same replid and\n     * become a master if so. */\n    if (c->argc > 3 && !strcasecmp(szFromObj(c->argv[0]),\"psync\") && \n        !strcasecmp(szFromObj(c->argv[3]),\"failover\"))\n    {\n        serverLog(LL_WARNING, \"Failover request received for replid %s.\",\n            (unsigned char *)szFromObj(c->argv[1]));\n        if (!listLength(g_pserver->masters)) {\n            addReplyError(c, \"PSYNC FAILOVER can't be sent to a master.\");\n            return;\n        }\n        if (listLength(g_pserver->masters) > 1) {\n            addReplyError(c, \"PSYNC FAILOVER can't be used with multi-master.\");\n            return;\n        }\n\n        if (!strcasecmp(szFromObj(c->argv[1]),g_pserver->replid)) {\n            replicationUnsetMaster((redisMaster*)listNodeValue(listFirst(g_pserver->masters)));\n            sds client = catClientInfoString(sdsempty(),c);\n            serverLog(LL_NOTICE,\n                \"MASTER MODE enabled (failover request from '%s')\",client);\n            sdsfree(client);\n        } else {\n            addReplyError(c, \"PSYNC FAILOVER replid must match my replid.\");\n            return;            \n        }\n    }\n\n    /* Don't let replicas sync with us while we're failing over */\n    if (g_pserver->failover_state != NO_FAILOVER) {\n        addReplyError(c,\"-NOMASTERLINK Can't SYNC while failing over\");\n        return;\n    }\n\n    /* Refuse SYNC requests if we are a slave but the link with our master\n     * is not ok... */\n    if (!g_pserver->fActiveReplica) {\n        if (FAnyDisconnectedMasters()) {\n            addReplyError(c,\"-NOMASTERLINK Can't SYNC while not connected with my master\");\n            return;\n        }\n    }\n\n    /* SYNC can't be issued when the server has pending data to send to\n     * the client about already issued commands. We need a fresh reply\n     * buffer registering the differences between the BGSAVE and the current\n     * dataset, so that we can copy to other slaves if needed. */\n    if (clientHasPendingReplies(c)) {\n        addReplyError(c,\"SYNC and PSYNC are invalid with pending output\");\n        return;\n    }\n\n    serverLog(LL_NOTICE,\"Replica %s asks for synchronization\",\n        replicationGetSlaveName(c));\n\n    /* Try a partial resynchronization if this is a PSYNC command.\n     * If it fails, we continue with usual full resynchronization, however\n     * when this happens masterTryPartialResynchronization() already\n     * replied with:\n     *\n     * +FULLRESYNC <replid> <offset>\n     *\n     * So the replica knows the new replid and offset to try a PSYNC later\n     * if the connection with the master is lost. */\n    if (!strcasecmp((const char*)ptrFromObj(c->argv[0]),\"psync\")) {\n        if (masterTryPartialResynchronization(c) == C_OK) {\n            g_pserver->stat_sync_partial_ok++;\n            return; /* No full resync needed, return. */\n        } else {\n            char *master_replid = (char*)ptrFromObj(c->argv[1]);\n\n            /* Increment stats for failed PSYNCs, but only if the\n             * replid is not \"?\", as this is used by slaves to force a full\n             * resync on purpose when they are not albe to partially\n             * resync. */\n            if (master_replid[0] != '?') g_pserver->stat_sync_partial_err++;\n        }\n    } else {\n        /* If a replica uses SYNC, we are dealing with an old implementation\n         * of the replication protocol (like keydb-cli --replica). Flag the client\n         * so that we don't expect to receive REPLCONF ACK feedbacks. */\n        c->flags |= CLIENT_PRE_PSYNC;\n    }\n\n    /* Full resynchronization. */\n    g_pserver->stat_sync_full++;\n\n    /* Setup the replica as one waiting for BGSAVE to start. The following code\n     * paths will change the state if we handle the replica differently. */\n    c->replstate = SLAVE_STATE_WAIT_BGSAVE_START;\n    if (g_pserver->repl_disable_tcp_nodelay)\n        connDisableTcpNoDelay(c->conn); /* Non critical if it fails. */\n    c->repldbfd = -1;\n    c->flags |= CLIENT_SLAVE;\n    listAddNodeTail(g_pserver->slaves,c);\n    g_pserver->rgthreadvar[c->iel].cclientsReplica++;\n\n    /* Create the replication backlog if needed. */\n    if (listLength(g_pserver->slaves) == 1 && g_pserver->repl_backlog == NULL) {\n        /* When we create the backlog from scratch, we always use a new\n         * replication ID and clear the ID2, since there is no valid\n         * past history. */\n        changeReplicationId();\n        clearReplicationId2();\n        createReplicationBacklog();\n        serverLog(LL_NOTICE,\"Replication backlog created, my new \"\n                            \"replication IDs are '%s' and '%s'\",\n                            g_pserver->replid, g_pserver->replid2);\n    }\n\n    /* CASE 0: Fast Sync */\n    if (c->slave_capa & SLAVE_CAPA_KEYDB_FASTSYNC && FFastSyncEnabled()) {\n        serverLog(LL_NOTICE,\"Fast SYNC on next replication cycle\");\n    /* CASE 1: BGSAVE is in progress, with disk target. */\n    } else if (g_pserver->FRdbSaveInProgress() &&\n        g_pserver->rdb_child_type == RDB_CHILD_TYPE_DISK)\n    {\n        /* Ok a background save is in progress. Let's check if it is a good\n         * one for replication, i.e. if there is another replica that is\n         * registering differences since the server forked to save. */\n        client *replica;\n        listNode *ln;\n        listIter li;\n\n        listRewind(g_pserver->slaves,&li);\n        while((ln = listNext(&li))) {\n            replica = (client*)ln->value;\n            /* If the client needs a buffer of commands, we can't use\n             * a replica without replication buffer. */\n            if (replica->replstate == SLAVE_STATE_WAIT_BGSAVE_END &&\n                (!(replica->flags & CLIENT_REPL_RDBONLY) ||\n                 (c->flags & CLIENT_REPL_RDBONLY)))\n                break;\n        }\n        \n        /* To attach this replica, we check that it has at least all the\n         * capabilities of the replica that triggered the current BGSAVE. */\n        if (ln && ((c->slave_capa & replica->slave_capa) == replica->slave_capa)) {\n            /* Perfect, the server is already registering differences for\n             * another slave. Set the right state, and copy the buffer.\n             * We don't copy buffer if clients don't want. */\n            if (!(c->flags & CLIENT_REPL_RDBONLY)) copyClientOutputBuffer(c,replica);\n            replicationSetupSlaveForFullResync(c,replica->psync_initial_offset);\n            serverLog(LL_NOTICE,\"Waiting for end of BGSAVE for SYNC\");\n        } else {\n            /* No way, we need to wait for the next BGSAVE in order to\n             * register differences. */\n            serverLog(LL_NOTICE,\"Can't attach the replica to the current BGSAVE. Waiting for next BGSAVE for SYNC\");\n        }\n\n    /* CASE 2: BGSAVE is in progress, with socket target. */\n    } else if (g_pserver->FRdbSaveInProgress() &&\n               g_pserver->rdb_child_type == RDB_CHILD_TYPE_SOCKET)\n    {\n        /* There is an RDB child process but it is writing directly to\n         * children sockets. We need to wait for the next BGSAVE\n         * in order to synchronize. */\n        serverLog(LL_NOTICE,\"Current BGSAVE has socket target. Waiting for next BGSAVE for SYNC\");\n\n    /* CASE 3: There is no BGSAVE is progress. */\n    } else {\n        if (g_pserver->repl_diskless_sync && (c->slave_capa & SLAVE_CAPA_EOF) &&\n            g_pserver->repl_diskless_sync_delay)\n        {\n            /* Diskless replication RDB child is created inside\n             * replicationCron() since we want to delay its start a\n             * few seconds to wait for more slaves to arrive. */\n            serverLog(LL_NOTICE,\"Delay next BGSAVE for diskless SYNC\");\n        } else {\n            /* We don't have a BGSAVE in progress, let's start one. Diskless\n             * or disk-based mode is determined by replica's capacity. */\n            if (!hasActiveChildProcessOrBGSave()) {\n                startBgsaveForReplication(c->slave_capa);\n            } else {\n                serverLog(LL_NOTICE,\n                    \"No BGSAVE in progress, but another BG operation is active. \"\n                    \"BGSAVE for replication delayed\");\n            }\n        }\n    }\n    return;\n}\n\nvoid processReplconfUuid(client *c, robj *arg)\n{\n    const char *remoteUUID = nullptr;\n\n    if (arg->type != OBJ_STRING)\n        goto LError;\n\n    remoteUUID = (const char*)ptrFromObj(arg);\n    if (strlen(remoteUUID) != 36)\n        goto LError;\n\n    if (uuid_parse(remoteUUID, c->uuid) != 0)\n        goto LError;\n\n    listIter liMi;\n    listNode *lnMi;\n    listRewind(g_pserver->masters, &liMi);\n\n    // Enforce a fair ordering for connection, if they attempt to connect before us close them out\n    // This must be consistent so that both make the same decision of who should proceed first\n    while ((lnMi = listNext(&liMi))) {\n        redisMaster *mi = (redisMaster*)listNodeValue(lnMi);\n        if (mi->repl_state == REPL_STATE_CONNECTED)\n            continue;\n        if (FSameUuidNoNil(mi->master_uuid, c->uuid)) {\n            // Decide based on UUID so both clients make the same decision of which host loses \n            //  otherwise we may entere a loop where neither client can proceed\n            if (memcmp(mi->master_uuid, c->uuid, UUID_BINARY_LEN) < 0) {\n                freeClientAsync(c);\n            }\n        }\n    }\n\n    char szServerUUID[36 + 2]; // 1 for the '+', another for '\\0'\n    szServerUUID[0] = '+';\n    uuid_unparse(cserver.uuid, szServerUUID+1);\n    addReplyProto(c, szServerUUID, 37);\n    addReplyProto(c, \"\\r\\n\", 2);\n    return;\n\nLError:\n    addReplyError(c, \"Invalid UUID\");\n    return;\n}\n\nvoid processReplconfLicense(client *c, robj *)\n{\n    // Only for back-compat\n    addReply(c, shared.ok);\n}\n\n/* REPLCONF <option> <value> <option> <value> ...\n * This command is used by a replica in order to configure the replication\n * process before starting it with the SYNC command.\n * This command is also used by a master in order to get the replication\n * offset from a replica.\n *\n * Currently we support these options:\n *\n * - listening-port <port>\n * - ip-address <ip>\n * What is the listening ip and port of the Replica redis instance, so that\n * the master can accurately lists replicas and their listening ports in the\n * INFO output.\n *\n * - capa <eof|psync2>\n * What is the capabilities of this instance.\n * eof: supports EOF-style RDB transfer for diskless replication.\n * psync2: supports PSYNC v2, so understands +CONTINUE <new repl ID>.\n *\n * - ack <offset>\n * Replica informs the master the amount of replication stream that it\n * processed so far.\n *\n * - getack\n * Unlike other subcommands, this is used by master to get the replication\n * offset from a replica.\n *\n * - rdb-only\n * Only wants RDB snapshot without replication buffer. */\nvoid replconfCommand(client *c) {\n    int j;\n    bool fCapaCommand = false;\n\n    if ((c->argc % 2) == 0) {\n        /* Number of arguments must be odd to make sure that every\n         * option has a corresponding value. */\n        addReplyErrorObject(c,shared.syntaxerr);\n        return;\n    }\n\n    /* Process every option-value pair. */\n    for (j = 1; j < c->argc; j+=2) {\n        fCapaCommand = false;\n        if (!strcasecmp((const char*)ptrFromObj(c->argv[j]),\"listening-port\")) {\n            long port;\n\n            if ((getLongFromObjectOrReply(c,c->argv[j+1],\n                    &port,NULL) != C_OK))\n                return;\n            c->slave_listening_port = port;\n        } else if (!strcasecmp((const char*)ptrFromObj(c->argv[j]),\"ip-address\")) {\n            sds addr = (sds)ptrFromObj(c->argv[j+1]);\n            if (sdslen(addr) < NET_HOST_STR_LEN) {\n                if (c->slave_addr) sdsfree(c->slave_addr);\n                c->slave_addr = sdsdup(addr);\n            } else {\n                addReplyErrorFormat(c,\"REPLCONF ip-address provided by \"\n                    \"replica instance is too long: %zd bytes\", sdslen(addr));\n                return;\n            }\n        } else if (!strcasecmp((const char*)ptrFromObj(c->argv[j]),\"capa\")) {\n            /* Ignore capabilities not understood by this master. */\n            if (!strcasecmp((const char*)ptrFromObj(c->argv[j+1]),\"eof\"))\n                c->slave_capa |= SLAVE_CAPA_EOF;\n            else if (!strcasecmp((const char*)ptrFromObj(c->argv[j+1]),\"psync2\"))\n                c->slave_capa |= SLAVE_CAPA_PSYNC2;\n            else if (!strcasecmp((const char*)ptrFromObj(c->argv[j+1]), \"activeExpire\"))\n                c->slave_capa |= SLAVE_CAPA_ACTIVE_EXPIRE;\n            else if (!strcasecmp((const char*)ptrFromObj(c->argv[j+1]), \"keydb-fastsync\"))\n                c->slave_capa |= SLAVE_CAPA_KEYDB_FASTSYNC;\n\n            fCapaCommand = true;\n        } else if (!strcasecmp((const char*)ptrFromObj(c->argv[j]),\"ack\")) {\n            /* REPLCONF ACK is used by replica to inform the master the amount\n             * of replication stream that it processed so far. It is an\n             * internal only command that normal clients should never use. */\n            long long offset;\n\n            if (!(c->flags & CLIENT_SLAVE)) return;\n            if ((getLongLongFromObject(c->argv[j+1], &offset) != C_OK))\n                return;\n            if (offset > c->repl_ack_off)\n                c->repl_ack_off = offset;\n            c->repl_ack_time = g_pserver->unixtime;\n            /* If this was a diskless replication, we need to really put\n             * the replica online when the first ACK is received (which\n             * confirms slave is online and ready to get more data). This\n             * allows for simpler and less CPU intensive EOF detection\n             * when streaming RDB files.\n             * There's a chance the ACK got to us before we detected that the\n             * bgsave is done (since that depends on cron ticks), so run a\n             * quick check first (instead of waiting for the next ACK. */\n            if (g_pserver->child_type == CHILD_TYPE_RDB && c->replstate == SLAVE_STATE_WAIT_BGSAVE_END)\n                checkChildrenDone();\n            if (c->repl_put_online_on_ack && (c->replstate == SLAVE_STATE_ONLINE || c->replstate == SLAVE_STATE_FASTSYNC_DONE))\n                putSlaveOnline(c);\n            /* Note: this command does not reply anything! */\n            return;\n        } else if (!strcasecmp((const char*)ptrFromObj(c->argv[j]),\"getack\")) {\n            /* REPLCONF GETACK is used in order to request an ACK ASAP\n             * to the replica. */\n            listIter li;\n            listNode *ln;\n            listRewind(g_pserver->masters, &li);\n            while ((ln = listNext(&li)))\n            {\n                replicationSendAck((redisMaster*)listNodeValue(ln));\n            }\n            return;\n        } else if (!strcasecmp((const char*)ptrFromObj(c->argv[j]),\"uuid\")) {\n            /* REPLCONF uuid is used to set and send the UUID of each host */\n            processReplconfUuid(c, c->argv[j+1]);\n            return; // the process function replies to the client for both error and success\n        } else if (!strcasecmp(szFromObj(c->argv[j]),\"license\")) {\n            processReplconfLicense(c, c->argv[j+1]);\n            return;\n        } else if (!strcasecmp(szFromObj(c->argv[j]),\"rdb-only\")) {\n           /* REPLCONF RDB-ONLY is used to identify the client only wants\n            * RDB snapshot without replication buffer. */\n            long rdb_only = 0;\n            if (getRangeLongFromObjectOrReply(c,c->argv[j+1],\n                    0,1,&rdb_only,NULL) != C_OK)\n                return;\n            if (rdb_only == 1) c->flags |= CLIENT_REPL_RDBONLY;\n            else c->flags &= ~CLIENT_REPL_RDBONLY;\n        } else {\n            addReplyErrorFormat(c,\"Unrecognized REPLCONF option: %s\",\n                (char*)ptrFromObj(c->argv[j]));\n            return;\n        }\n    }\n\n    if (fCapaCommand) {\n        sds reply = sdsnew(\"+OK\");\n        if (g_pserver->fActiveReplica) {\n            reply = sdscat(reply, \" active-replica\");\n        }\n        if ((c->slave_capa & SLAVE_CAPA_KEYDB_FASTSYNC) && FFastSyncEnabled()) {\n            reply = sdscat(reply, \" keydb-fastsync-save\");\n        } else {\n            c->slave_capa = (c->slave_capa & (~SLAVE_CAPA_KEYDB_FASTSYNC)); // never try to fast sync for this as they won't expect it\n        }\n        reply = sdscat(reply, \"\\r\\n\");\n        addReplySds(c, reply);\n    } else {\n        addReply(c,shared.ok);\n    }\n}\n\n/* This function puts a replica in the online state, and should be called just\n * after a replica received the RDB file for the initial synchronization, and\n * we are finally ready to send the incremental stream of commands.\n *\n * It does a few things:\n * 1) Close the replica's connection async if it doesn't need replication\n *    commands buffer stream, since it actually isn't a valid replica.\n * 2) Put the slave in ONLINE state. Note that the function may also be called\n *    for a replicas that are already in ONLINE state, but having the flag\n *    repl_put_online_on_ack set to true: we still have to install the write\n *    handler in that case. This function will take care of that.\n * 3) Make sure the writable event is re-installed, since calling the SYNC\n *    command disables it, so that we can accumulate output buffer without\n *    sending it to the replica.\n * 4) Update the count of \"good replicas\". */\nvoid putSlaveOnline(client *replica) {\n    replica->replstate = SLAVE_STATE_ONLINE;\n    \n    replica->repl_put_online_on_ack = 0;\n    replica->repl_ack_time = g_pserver->unixtime; /* Prevent false timeout. */\n\n    if (replica->flags & CLIENT_REPL_RDBONLY) {\n        serverLog(LL_NOTICE,\n            \"Close the connection with replica %s as RDB transfer is complete\",\n            replicationGetSlaveName(replica));\n        freeClientAsync(replica);\n        return;\n    }\n    if (connSetWriteHandler(replica->conn, sendReplyToClient, true) == C_ERR) {\n        serverLog(LL_WARNING,\"Unable to register writable event for replica bulk transfer: %s\", strerror(errno));\n        freeClient(replica);\n        return;\n    }\n    refreshGoodSlavesCount();\n    /* Fire the replica change modules event. */\n    moduleFireServerEvent(REDISMODULE_EVENT_REPLICA_CHANGE,\n                          REDISMODULE_SUBEVENT_REPLICA_CHANGE_ONLINE,\n                          NULL);\n    serverLog(LL_NOTICE,\"Synchronization with replica %s succeeded\",\n        replicationGetSlaveName(replica));\n    \n    if (!(replica->slave_capa & SLAVE_CAPA_ACTIVE_EXPIRE) && g_pserver->fActiveReplica)\n    {\n        serverLog(LL_WARNING, \"Warning: replica %s does not support active expiration.  This client may not correctly process key expirations.\"\n            \"\\n\\tThis is OK if you are in the process of an active upgrade.\", replicationGetSlaveName(replica));\n        serverLog(LL_WARNING, \"Connections between active replicas and traditional replicas is deprecated.  This will be refused in future versions.\"\n            \"\\n\\tPlease fix your replica topology\");\n    }\n}\n\n/* We call this function periodically to remove an RDB file that was\n * generated because of replication, in an instance that is otherwise\n * without any persistence. We don't want instances without persistence\n * to take RDB files around, this violates certain policies in certain\n * environments. */\nvoid removeRDBUsedToSyncReplicas(void) {\n    serverAssert(GlobalLocksAcquired());\n\n    /* If the feature is disabled, return ASAP but also clear the\n     * RDBGeneratedByReplication flag in case it was set. Otherwise if the\n     * feature was enabled, but gets disabled later with CONFIG SET, the\n     * flag may remain set to one: then next time the feature is re-enabled\n     * via CONFIG SET we have have it set even if no RDB was generated\n     * because of replication recently. */\n    if (!g_pserver->rdb_del_sync_files) {\n        RDBGeneratedByReplication = 0;\n        return;\n    }\n\n    if (allPersistenceDisabled() && RDBGeneratedByReplication) {\n        client *slave;\n        listNode *ln;\n        listIter li;\n\n        int delrdb = 1;\n        listRewind(g_pserver->slaves,&li);\n        while((ln = listNext(&li))) {\n            slave = (client*)ln->value;\n            if (slave->replstate == SLAVE_STATE_WAIT_BGSAVE_START ||\n                slave->replstate == SLAVE_STATE_WAIT_BGSAVE_END ||\n                slave->replstate == SLAVE_STATE_SEND_BULK)\n            {\n                delrdb = 0;\n                break; /* No need to check the other replicas. */\n            }\n        }\n        if (delrdb) {\n            struct stat sb;\n            if (lstat(g_pserver->rdb_filename,&sb) != -1) {\n                RDBGeneratedByReplication = 0;\n                serverLog(LL_NOTICE,\n                    \"Removing the RDB file used to feed replicas \"\n                    \"in a persistence-less instance\");\n                bg_unlink(g_pserver->rdb_filename);\n            }\n        }\n    }\n}\n\nvoid sendBulkToSlave(connection *conn) {\n    serverAssert(GlobalLocksAcquired());\n    \n    client *replica = (client*)connGetPrivateData(conn);\n    serverAssert(FCorrectThread(replica));\n    ssize_t nwritten;\n    AeLocker aeLock;\n    std::unique_lock<fastlock> ul(replica->lock);\n\n    /* Before sending the RDB file, we send the preamble as configured by the\n     * replication process. Currently the preamble is just the bulk count of\n     * the file in the form \"$<length>\\r\\n\". */\n    if (replica->replpreamble) {\n        nwritten = connWrite(conn,replica->replpreamble,sdslen(replica->replpreamble));\n        if (nwritten == -1) {\n            serverLog(LL_VERBOSE,\n                \"Write error sending RDB preamble to replica: %s\",\n                connGetLastError(conn));\n            ul.unlock();\n            aeLock.arm(nullptr);\n            freeClient(replica);\n            return;\n        }\n        g_pserver->stat_net_output_bytes += nwritten;\n        sdsrange(replica->replpreamble,nwritten,-1);\n        if (sdslen(replica->replpreamble) == 0) {\n            sdsfree(replica->replpreamble);\n            replica->replpreamble = NULL;\n            /* fall through sending data. */\n        } else {\n            return;\n        }\n    }\n\n    /* If the preamble was already transferred, send the RDB bulk data.\n     * try to use sendfile system call if supported, unless tls is enabled.\n     * fallback to normal read+write otherwise. */\n    nwritten = 0;\n    ssize_t buflen;\n    char buf[PROTO_IOBUF_LEN];\n\n    lseek(replica->repldbfd,replica->repldboff,SEEK_SET);\n    buflen = read(replica->repldbfd,buf,PROTO_IOBUF_LEN);\n    if (buflen <= 0) {\n        serverLog(LL_WARNING,\"Read error sending DB to replica: %s\",\n            (buflen == 0) ? \"premature EOF\" : strerror(errno));\n        ul.unlock();\n        aeLock.arm(nullptr);\n        freeClient(replica);\n        return;\n    }\n    if ((nwritten = connWrite(conn,buf,buflen)) == -1) {\n        if (connGetState(conn) != CONN_STATE_CONNECTED) {\n            serverLog(LL_WARNING,\"Write error sending DB to replica: %s\",\n                connGetLastError(conn));\n            ul.unlock();\n            aeLock.arm(nullptr);\n            freeClient(replica);\n        }\n        return;\n    }\n\n    replica->repldboff += nwritten;\n    g_pserver->stat_net_output_bytes += nwritten;\n    if (replica->repldboff == replica->repldbsize) {\n        close(replica->repldbfd);\n        replica->repldbfd = -1;\n        connSetWriteHandler(replica->conn,NULL);\n        putSlaveOnline(replica);\n    }\n}\n\n/* Remove one write handler from the list of connections waiting to be writable\n * during rdb pipe transfer. */\nvoid rdbPipeWriteHandlerConnRemoved(struct connection *conn) {\n    if (!connHasWriteHandler(conn))\n        return;\n    connSetWriteHandler(conn, NULL);\n    client *slave = (client*)connGetPrivateData(conn);\n    slave->repl_last_partial_write = 0;\n    g_pserver->rdb_pipe_numconns_writing--;\n    /* if there are no more writes for now for this conn, or write error: */\n    if (g_pserver->rdb_pipe_numconns_writing == 0) {\n        aePostFunction(g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].el, []{\n            if (aeCreateFileEvent(serverTL->el, g_pserver->rdb_pipe_read, AE_READABLE, rdbPipeReadHandler,NULL) == AE_ERR) {\n                serverPanic(\"Unrecoverable error creating g_pserver->rdb_pipe_read file event.\");\n            }\n        });\n    }\n}\n\n/* Called in diskless master during transfer of data from the rdb pipe, when\n * the replica becomes writable again. */\nvoid rdbPipeWriteHandler(struct connection *conn) {\n    serverAssert(g_pserver->rdb_pipe_bufflen>0);\n    client *slave = (client*)connGetPrivateData(conn);\n    AssertCorrectThread(slave);\n    int nwritten;\n    \n    if (slave->flags & CLIENT_CLOSE_ASAP) {\n        rdbPipeWriteHandlerConnRemoved(conn);\n        return;\n    }\n    \n    if ((nwritten = connWrite(conn, g_pserver->rdb_pipe_buff + slave->repldboff,\n                              g_pserver->rdb_pipe_bufflen - slave->repldboff)) == -1)\n    {\n        if (connGetState(conn) == CONN_STATE_CONNECTED)\n            return; /* equivalent to EAGAIN */\n        serverLog(LL_WARNING,\"Write error sending DB to replica: %s\",\n            connGetLastError(conn));\n        freeClientAsync(slave);\n        return;\n    } else {\n        slave->repldboff += nwritten;\n        g_pserver->stat_net_output_bytes += nwritten;\n        if (slave->repldboff < g_pserver->rdb_pipe_bufflen) {\n            slave->repl_last_partial_write = g_pserver->unixtime;\n            return; /* more data to write.. */\n        }\n    }\n    rdbPipeWriteHandlerConnRemoved(conn);\n}\n\n/* Called in diskless master, when there's data to read from the child's rdb pipe */\nvoid rdbPipeReadHandler(struct aeEventLoop *eventLoop, int fd, void *clientData, int mask) {\n    UNUSED(mask);\n    UNUSED(clientData);\n\n    int i;\n    if (!g_pserver->rdb_pipe_buff)\n        g_pserver->rdb_pipe_buff = (char*)zmalloc(PROTO_IOBUF_LEN);\n    serverAssert(g_pserver->rdb_pipe_numconns_writing==0);\n    serverAssert(eventLoop == g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].el);\n\n    while (1) {\n        g_pserver->rdb_pipe_bufflen = read(fd, g_pserver->rdb_pipe_buff, PROTO_IOBUF_LEN);\n        if (g_pserver->rdb_pipe_bufflen < 0) {\n            if (errno == EAGAIN || errno == EWOULDBLOCK)\n                return;\n            serverLog(LL_WARNING,\"Diskless rdb transfer, read error sending DB to replicas: %s\", strerror(errno));\n            for (i=0; i < g_pserver->rdb_pipe_numconns; i++) {\n                connection *conn = g_pserver->rdb_pipe_conns[i];\n                if (!conn)\n                    continue;\n                client *slave = (client*)connGetPrivateData(conn);\n                freeClientAsync(slave);\n                g_pserver->rdb_pipe_conns[i] = NULL;\n            }\n            killRDBChild();\n            return;\n        }\n\n        if (g_pserver->rdb_pipe_bufflen == 0 && !g_pserver->rdbThreadVars.fRdbThreadCancel) {\n            /* EOF - write end was closed. */\n            int stillUp = 0;\n            aeDeleteFileEvent(eventLoop, g_pserver->rdb_pipe_read, AE_READABLE);\n            for (i=0; i < g_pserver->rdb_pipe_numconns; i++)\n            {\n                connection *conn = g_pserver->rdb_pipe_conns[i];\n                if (!conn)\n                    continue;\n                stillUp++;\n            }\n            serverLog(LL_WARNING,\"Diskless rdb transfer, done reading from pipe, %d replicas still up.\", stillUp);\n            /* Now that the replicas have finished reading, notify the child that it's safe to exit. \n             * When the server detectes the child has exited, it can mark the replica as online, and\n             * start streaming the replication buffers. */\n            close(g_pserver->rdb_child_exit_pipe);\n            g_pserver->rdb_child_exit_pipe = -1;\n            return;\n        }\n\n        int stillAlive = 0;\n        for (i=0; i < g_pserver->rdb_pipe_numconns; i++)\n        {\n            int nwritten;\n            connection *conn = g_pserver->rdb_pipe_conns[i];\n            if (!conn)\n                continue;\n\n            client *slave = (client*)connGetPrivateData(conn);\n            std::unique_lock<fastlock> ul(slave->lock);\n            serverAssert(slave->conn == conn);\n            if(slave->flags.load(std::memory_order_relaxed) & CLIENT_CLOSE_ASAP)\n                continue;   // if we asked to free the client don't send any more data\n            \n            // Normally it would be bug to talk a client conn from a different thread, but here we know nobody else will\n            //  be sending anything while in this replication state so it is OK\n            if ((nwritten = connWrite(conn, g_pserver->rdb_pipe_buff, g_pserver->rdb_pipe_bufflen)) == -1) {\n                if (connGetState(conn) != CONN_STATE_CONNECTED) {\n                    serverLog(LL_WARNING,\"Diskless rdb transfer, write error sending DB to replica: %s\",\n                        connGetLastError(conn));\n                    freeClientAsync(slave);\n                    g_pserver->rdb_pipe_conns[i] = NULL;\n                    continue;\n                }\n                /* An error and still in connected state, is equivalent to EAGAIN */\n                slave->repldboff = 0;\n            } else {\n                /* Note: when use diskless replication, 'repldboff' is the offset\n                 * of 'rdb_pipe_buff' sent rather than the offset of entire RDB. */\n                slave->repldboff = nwritten;\n                g_pserver->stat_net_output_bytes += nwritten;\n            }\n            /* If we were unable to write all the data to one of the replicas,\n             * setup write handler (and disable pipe read handler, below) */\n            if (nwritten != g_pserver->rdb_pipe_bufflen) {\n                g_pserver->rdb_pipe_numconns_writing++;\n                slave->repl_last_partial_write = g_pserver->unixtime;\n                slave->postFunction([conn](client *) {\n                    connSetWriteHandler(conn, rdbPipeWriteHandler);\n                });\n            }\n            stillAlive++;\n        }\n\n        if (stillAlive == 0) {\n            serverLog(LL_WARNING,\"Diskless rdb transfer, last replica dropped, killing fork child.\");\n            killRDBChild();\n        }\n        /*  Remove the pipe read handler if at least one write handler was set. */\n        if (g_pserver->rdb_pipe_numconns_writing || stillAlive == 0) {\n            aeDeleteFileEvent(eventLoop, g_pserver->rdb_pipe_read, AE_READABLE);\n            break;\n        }\n    }\n}\n\n/* This function is called at the end of every background saving,\n * or when the replication RDB transfer strategy is modified from\n * disk to socket or the other way around.\n *\n * The goal of this function is to handle slaves waiting for a successful\n * background saving in order to perform non-blocking synchronization, and\n * to schedule a new BGSAVE if there are slaves that attached while a\n * BGSAVE was in progress, but it was not a good one for replication (no\n * other replica was accumulating differences).\n *\n * The argument bgsaveerr is C_OK if the background saving succeeded\n * otherwise C_ERR is passed to the function.\n * The 'type' argument is the type of the child that terminated\n * (if it had a disk or socket target). */\nvoid updateSlavesWaitingBgsave(int bgsaveerr, int type)\n{\n    listNode *ln;\n    listIter li;\n    serverAssert(GlobalLocksAcquired());\n\n    listRewind(g_pserver->slaves,&li);\n    while((ln = listNext(&li))) {\n        client *replica = (client*)ln->value;\n\n        std::unique_lock<fastlock> ul(replica->lock);\n\n        if (replica->replstate == SLAVE_STATE_WAIT_BGSAVE_END) {\n            struct redis_stat buf;\n\n            if (bgsaveerr != C_OK) {\n                ul.unlock();\n                freeClientAsync(replica);\n                serverLog(LL_WARNING,\"SYNC failed. BGSAVE child returned an error\");\n                continue;\n            }\n\n            /* If this was an RDB on disk save, we have to prepare to send\n             * the RDB from disk to the replica socket. Otherwise if this was\n             * already an RDB -> Slaves socket transfer, used in the case of\n             * diskless replication, our work is trivial, we can just put\n             * the replica online. */\n            if (type == RDB_CHILD_TYPE_SOCKET) {\n                serverLog(LL_NOTICE,\n                    \"Streamed RDB transfer with replica %s succeeded (socket). Waiting for REPLCONF ACK from slave to enable streaming\",\n                        replicationGetSlaveName(replica));\n                /* Note: we wait for a REPLCONF ACK message from the replica in\n                 * order to really put it online (install the write handler\n                 * so that the accumulated data can be transferred). However\n                 * we change the replication state ASAP, since our slave\n                 * is technically online now.\n                 *\n                 * So things work like that:\n                 *\n                 * 1. We end trasnferring the RDB file via socket.\n                 * 2. The replica is put ONLINE but the write handler\n                 *    is not installed.\n                 * 3. The replica however goes really online, and pings us\n                 *    back via REPLCONF ACK commands.\n                 * 4. Now we finally install the write handler, and send\n                 *    the buffers accumulated so far to the replica.\n                 *\n                 * But why we do that? Because the replica, when we stream\n                 * the RDB directly via the socket, must detect the RDB\n                 * EOF (end of file), that is a special random string at the\n                 * end of the RDB (for streamed RDBs we don't know the length\n                 * in advance). Detecting such final EOF string is much\n                 * simpler and less CPU intensive if no more data is sent\n                 * after such final EOF. So we don't want to glue the end of\n                 * the RDB trasfer with the start of the other replication\n                 * data. */\n                replica->replstate = SLAVE_STATE_ONLINE;\n                replica->repl_put_online_on_ack = 1;\n                replica->repl_ack_time = g_pserver->unixtime; /* Timeout otherwise. */\n            } else {\n                if ((replica->repldbfd = open(g_pserver->rdb_filename,O_RDONLY)) == -1 ||\n                    redis_fstat(replica->repldbfd,&buf) == -1) {\n                    ul.unlock();\n                    freeClientAsync(replica);\n                    serverLog(LL_WARNING,\"SYNC failed. Can't open/stat DB after BGSAVE: %s\", strerror(errno));\n                    continue;\n                }\n                replica->repldboff = 0;\n                replica->repldbsize = buf.st_size;\n                replica->replstate = SLAVE_STATE_SEND_BULK;\n                replica->replpreamble = sdscatprintf(sdsempty(),\"$%lld\\r\\n\",\n                    (unsigned long long) replica->repldbsize);\n\n                if (FCorrectThread(replica))\n                {\n                    connSetWriteHandler(replica->conn,NULL);\n                    if (connSetWriteHandler(replica->conn,sendBulkToSlave) == C_ERR) {\n                        ul.unlock();\n                        freeClient(replica);\n                        continue;\n                    }\n                }\n                else\n                {\n                    replica->postFunction([](client *replica) {\n                        connSetWriteHandler(replica->conn,NULL);\n                        if (connSetWriteHandler(replica->conn,sendBulkToSlave) == C_ERR) {\n                            freeClient(replica);\n                        }\n                    });\n                }\n            }\n        }\n    }\n}\n\n/* Save the replid of yourself and any connected masters to storage.\n * Returns if no storage provider is used. */\nvoid saveMasterStatusToStorage(bool fShutdown)\n{\n    if (!g_pserver->m_pstorageFactory || !g_pserver->metadataDb) return;\n\n    g_pserver->metadataDb->insert(\"repl-id\", 7, g_pserver->replid, sizeof(g_pserver->replid), true);\n    if (fShutdown)\n        g_pserver->metadataDb->insert(\"repl-offset\", 11, &g_pserver->master_repl_offset, sizeof(g_pserver->master_repl_offset), true);\n    else\n        g_pserver->metadataDb->erase(\"repl-offset\", 11);\n\n    if (g_pserver->fActiveReplica || (!listLength(g_pserver->masters) && g_pserver->repl_backlog)) {\n        int zero = 0;\n        g_pserver->metadataDb->insert(\"repl-stream-db\", 14, g_pserver->replicaseldb == -1 ? &zero : &g_pserver->replicaseldb,\n                                        sizeof(g_pserver->replicaseldb), true);\n    } else {\n        struct redisMaster *miFirst = (redisMaster*)(listLength(g_pserver->masters) ? listNodeValue(listFirst(g_pserver->masters)) : NULL);\n\n        if (miFirst && miFirst->master) {\n            g_pserver->metadataDb->insert(\"repl-stream-db\", 14, &miFirst->master->db->id, sizeof(miFirst->master->db->id), true);\n        }\n        else if (miFirst && miFirst->cached_master) {\n            g_pserver->metadataDb->insert(\"repl-stream-db\", 14, &miFirst->cached_master->db->id, sizeof(miFirst->cached_master->db->id), true);\n        }\n    }\n\n    if (listLength(g_pserver->masters) == 0) {\n        g_pserver->metadataDb->insert(\"repl-masters\", 12, (void*)\"\", 0, true);\n        return;\n    }\n    sds val = sds(sdsempty());\n    listNode *ln;\n    listIter li;\n    redisMaster *mi;\n    listRewind(g_pserver->masters,&li);\n    while((ln = listNext(&li)) != NULL) {\n        mi = (redisMaster*)listNodeValue(ln);\n        if (mi->masterhost == NULL) {\n            // if we don't know the host, no reason to save\n            continue;\n        }\n        if (!mi->master) {\n            // If master client is not available, use info from master struct - better than nothing\n            if (mi->master_replid[0] == 0) {\n                // if replid is null, there's no reason to save it\n                continue;\n            }\n            val = sdscatfmt(val, \"%s:%I:%s:%i;\", mi->master_replid,\n                mi->master_initial_offset,\n                mi->masterhost,\n                mi->masterport);\n        }\n        else {\n            if (mi->master->replid[0] == 0) {\n                // if replid is null, there's no reason to save it\n                continue;\n            }\n            val = sdscatfmt(val, \"%s:%I:%s:%i;\", mi->master->replid,\n                mi->master->reploff,\n                mi->masterhost,\n                mi->masterport);\n        }\n    }\n    g_pserver->metadataDb->insert(\"repl-masters\", 12, (void*)val, sdslen(val), true);\n    sdsfree(val);\n}\n\n/* Change the current instance replication ID with a new, random one.\n * This will prevent successful PSYNCs between this master and other\n * slaves, so the command should be called when something happens that\n * alters the current story of the dataset. */\nvoid changeReplicationId(void) {\n    getRandomHexChars(g_pserver->replid,CONFIG_RUN_ID_SIZE);\n    g_pserver->replid[CONFIG_RUN_ID_SIZE] = '\\0';\n    saveMasterStatusToStorage(false);\n}\n\n/* Clear (invalidate) the secondary replication ID. This happens, for\n * example, after a full resynchronization, when we start a new replication\n * history. */\nvoid clearReplicationId2(void) {\n    memset(g_pserver->replid2,'0',sizeof(g_pserver->replid));\n    g_pserver->replid2[CONFIG_RUN_ID_SIZE] = '\\0';\n    g_pserver->second_replid_offset = -1;\n}\n\n/* Use the current replication ID / offset as secondary replication\n * ID, and change the current one in order to start a new history.\n * This should be used when an instance is switched from replica to master\n * so that it can serve PSYNC requests performed using the master\n * replication ID. */\nvoid shiftReplicationId(void) {\n    memcpy(g_pserver->replid2,g_pserver->replid,sizeof(g_pserver->replid));\n    /* We set the second replid offset to the master offset + 1, since\n     * the replica will ask for the first byte it has not yet received, so\n     * we need to add one to the offset: for example if, as a replica, we are\n     * sure we have the same history as the master for 50 bytes, after we\n     * are turned into a master, we can accept a PSYNC request with offset\n     * 51, since the replica asking has the same history up to the 50th\n     * byte, and is asking for the new bytes starting at offset 51. */\n    g_pserver->second_replid_offset = g_pserver->master_repl_offset+1;\n    changeReplicationId();\n    serverLog(LL_WARNING,\"Setting secondary replication ID to %s, valid up to offset: %lld. New replication ID is %s\", g_pserver->replid2, g_pserver->second_replid_offset, g_pserver->replid);\n}\n\n/* ----------------------------------- SLAVE -------------------------------- */\n\n/* Returns 1 if the given replication state is a handshake state,\n * 0 otherwise. */\nint slaveIsInHandshakeState(redisMaster *mi) {\n    return mi->repl_state >= REPL_STATE_RECEIVE_PING_REPLY &&\n           mi->repl_state <= REPL_STATE_RECEIVE_PSYNC_REPLY;\n}\n\n/* Avoid the master to detect the replica is timing out while loading the\n * RDB file in initial synchronization. We send a single newline character\n * that is valid protocol but is guaranteed to either be sent entirely or\n * not, since the byte is indivisible.\n *\n * The function is called in two contexts: while we flush the current\n * data with emptyDb(), and while we load the new data received as an\n * RDB file from the master. */\nvoid replicationSendNewlineToMaster(redisMaster *mi) {\n    static time_t newline_sent;\n    if (time(NULL) != newline_sent) {\n        newline_sent = time(NULL);\n        /* Pinging back in this stage is best-effort. */\n        if (mi->repl_transfer_s) connWrite(mi->repl_transfer_s, \"\\n\", 1);\n    }\n}\n\n/* Callback used by emptyDb() while flushing away old data to load\n * the new dataset received by the master. */\nvoid replicationEmptyDbCallback(void *privdata) {\n    UNUSED(privdata);\n    listIter li;\n    listNode *ln;\n    listRewind(g_pserver->masters, &li);\n    while ((ln = listNext(&li)))\n    {\n        replicationSendNewlineToMaster((redisMaster*)listNodeValue(ln));\n    }\n}\n\n/* Once we have a link with the master and the synchronization was\n * performed, this function materializes the master client we store\n * at g_pserver->master, starting from the specified file descriptor. */\nvoid replicationCreateMasterClient(redisMaster *mi, connection *conn, int dbid) {\n    serverAssert(mi->master == nullptr);\n    mi->master = createClient(conn, serverTL - g_pserver->rgthreadvar);\n    if (conn)\n    {\n        serverAssert(connGetPrivateData(mi->master->conn) == mi->master);\n        connSetReadHandler(mi->master->conn, readQueryFromClient, true);\n    }\n\n    /**\n     * Important note:\n     * The CLIENT_DENY_BLOCKING flag is not, and should not, be set here.\n     * For commands like BLPOP, it makes no sense to block the master\n     * connection, and such blocking attempt will probably cause deadlock and\n     * break the replication. We consider such a thing as a bug because\n     * commands as BLPOP should never be sent on the replication link.\n     * A possible use-case for blocking the replication link is if a module wants\n     * to pass the execution to a background thread and unblock after the\n     * execution is done. This is the reason why we allow blocking the replication\n     * connection. */\n    mi->master->flags |= CLIENT_MASTER;\n\n    mi->master->authenticated = 1;\n    mi->master->reploff = mi->master_initial_offset;\n    mi->master->read_reploff = mi->master->reploff;\n    mi->master->user = NULL; /* This client can do everything. */\n    \n    memcpy(mi->master->uuid, mi->master_uuid, UUID_BINARY_LEN);\n    memset(mi->master_uuid, 0, UUID_BINARY_LEN); // make sure people don't use this temp storage buffer\n\n    memcpy(mi->master->replid, mi->master_replid,\n        sizeof(mi->master_replid));\n    /* If master offset is set to -1, this master is old and is not\n     * PSYNC capable, so we flag it accordingly. */\n    if (mi->master->reploff == -1)\n        mi->master->flags |= CLIENT_PRE_PSYNC;\n    if (dbid != -1) selectDb(mi->master,dbid);\n}\n\nvoid replicationCreateCachedMasterClone(redisMaster *mi) {\n    serverAssert(mi->master != nullptr);\n    serverLog(LL_NOTICE, \"Creating cache clone of our master\");\n    if ((mi->master->flags & (CLIENT_PROTOCOL_ERROR|CLIENT_BLOCKED))) {\n        freeClientAsync(mi->master);\n        mi->master = nullptr;\n        return;\n    }\n    client *c = createClient(nullptr, ielFromEventLoop(serverTL->el));\n\n    c->flags |= mi->master->flags & ~(CLIENT_PENDING_WRITE | CLIENT_UNBLOCKED | CLIENT_CLOSE_ASAP);\n    c->authenticated = mi->master->authenticated;\n    c->reploff = mi->master->reploff;\n    c->read_reploff = mi->master->read_reploff;\n    c->user = mi->master->user;\n\n    c->replstate = mi->master->replstate;\n    c->master_error = mi->master->master_error;\n    c->psync_initial_offset = mi->master->psync_initial_offset;\n    c->repldboff = mi->master->repldboff;\n    c->repldbsize = mi->master->repldbsize;\n\n    memcpy(c->uuid, mi->master->uuid, UUID_BINARY_LEN);\n    memcpy(c->replid, mi->master->replid,\n        sizeof(mi->master->replid));\n    selectDb(c, mi->master->db->id);\n\n    // Free the old one\n    mi->master->flags &= ~CLIENT_MASTER;\n    freeClientAsync(mi->master);\n\n    // Now make this one the cache\n    mi->master = c;\n    replicationCacheMaster(mi, c);\n}\n\n/* This function will try to re-enable the AOF file after the\n * master-replica synchronization: if it fails after multiple attempts\n * the replica cannot be considered reliable and exists with an\n * error. */\nvoid restartAOFAfterSYNC() {\n    unsigned int tries, max_tries = 10;\n    for (tries = 0; tries < max_tries; ++tries) {\n        if (startAppendOnly() == C_OK) break;\n        serverLog(LL_WARNING,\n            \"Failed enabling the AOF after successful master synchronization! \"\n            \"Trying it again in one second.\");\n        sleep(1);\n    }\n    if (tries == max_tries) {\n        serverLog(LL_WARNING,\n            \"FATAL: this replica instance finished the synchronization with \"\n            \"its master, but the AOF can't be turned on. Exiting now.\");\n        exit(1);\n    }\n}\n\nstatic int useDisklessLoad() {\n    /* compute boolean decision to use diskless load */\n    int enabled = g_pserver->repl_diskless_load == REPL_DISKLESS_LOAD_SWAPDB ||\n           (g_pserver->repl_diskless_load == REPL_DISKLESS_LOAD_WHEN_DB_EMPTY && dbTotalServerKeyCount()==0);\n    /* Check all modules handle read errors, otherwise it's not safe to use diskless load. */\n    if (enabled && !moduleAllDatatypesHandleErrors()) {\n        serverLog(LL_WARNING,\n            \"Skipping diskless-load because there are modules that don't handle read errors.\");\n        enabled = 0;\n    }\n    return enabled;\n}\n\n/* Helper function for readSyncBulkPayload() to make backups of the current\n * databases before socket-loading the new ones. The backups may be restored\n * by disklessLoadRestoreBackup or freed by disklessLoadDiscardBackup later. */\nconst dbBackup *disklessLoadMakeBackup(void) {\n    return backupDb();\n}\n\n/* Helper function for readSyncBulkPayload(): when replica-side diskless\n * database loading is used, Redis makes a backup of the existing databases\n * before loading the new ones from the socket.\n *\n * If the socket loading went wrong, we want to restore the old backups\n * into the server databases. */\nvoid disklessLoadRestoreBackup(const dbBackup *buckup) {\n    restoreDbBackup(buckup);\n}\n\n/* Helper function for readSyncBulkPayload() to discard our old backups\n * when the loading succeeded. */\nvoid disklessLoadDiscardBackup(const dbBackup *buckup, int flag) {\n    discardDbBackup(buckup, flag, replicationEmptyDbCallback);\n}\n\nsize_t parseCount(const char *rgch, size_t cch, long long *pvalue) {\n    size_t cchNumeral = 0;\n\n    if (cch > cchNumeral+1 && rgch[cchNumeral+1] == '-') ++cchNumeral;\n    while ((cch > (cchNumeral+1)) && isdigit(rgch[1 + cchNumeral])) ++cchNumeral;\n\n    if (cch < (cchNumeral+1+2)) { // +2 is for the \\r\\n we expect\n        throw true; // continuable\n    }\n\n    if (rgch[cchNumeral+1] != '\\r' || rgch[cchNumeral+2] != '\\n') {\n        serverLog(LL_WARNING, \"Bad protocol from MASTER: %s\", rgch);\n        throw false;\n    }\n\n    if (!string2ll(rgch+1, cchNumeral, pvalue)) {\n        serverLog(LL_WARNING, \"Bad protocol from MASTER: %s\", rgch);\n        throw false;\n    }\n\n    return cchNumeral + 3;\n}\n\nbool readSnapshotBulkPayload(connection *conn, redisMaster *mi, rdbSaveInfo &rsi) {\n    int fUpdate = g_pserver->fActiveReplica || g_pserver->enable_multimaster;\n    serverAssert(GlobalLocksAcquired());\n    serverAssert(mi->master == nullptr);\n    bool fFinished = false;\n\n    if (mi->bulkreadBuffer == nullptr) {\n        mi->bulkreadBuffer = sdsempty();\n        mi->parseState = new SnapshotPayloadParseState();\n        if (g_pserver->aof_state != AOF_OFF) stopAppendOnly();\n        if (!fUpdate) {\n            int empty_db_flags = g_pserver->repl_slave_lazy_flush ? EMPTYDB_ASYNC :\n                EMPTYDB_NO_FLAGS;\n            serverLog(LL_NOTICE, \"MASTER <-> REPLICA sync: Flushing old data\");\n            emptyDb(-1,empty_db_flags,replicationEmptyDbCallback);\n            for (int idb = 0; idb < cserver.dbnum; ++idb) {\n                aeAcquireLock();\n                g_pserver->db[idb]->processChanges(false);\n                aeReleaseLock();\n                g_pserver->db[idb]->commitChanges();\n                g_pserver->db[idb]->trackChanges(false);\n            }\n        }\n    }\n\n    serverAssert(mi->parseState != nullptr);\n    for (int iter = 0; iter < 10; ++iter) {\n        if (mi->parseState->shouldThrottle())\n            return false;\n\n        auto readlen = PROTO_IOBUF_LEN;\n        auto qblen = sdslen(mi->bulkreadBuffer);\n        mi->bulkreadBuffer = sdsMakeRoomFor(mi->bulkreadBuffer, readlen);\n\n        auto nread = connRead(conn, mi->bulkreadBuffer+qblen, readlen);\n        if (nread <= 0) {\n            if (connGetState(conn) != CONN_STATE_CONNECTED) {\n                serverLog(LL_WARNING,\"I/O error trying to sync with MASTER: %s\",\n                    (nread == -1) ? strerror(errno) : \"connection lost\");\n                cancelReplicationHandshake(mi, true);\n            }\n            return false;\n        }\n        g_pserver->stat_net_input_bytes += nread;\n        mi->repl_transfer_lastio = g_pserver->unixtime;\n        mi->repl_transfer_read += nread;\n        sdsIncrLen(mi->bulkreadBuffer,nread);\n\n        size_t offset = 0;\n\n        try {\n            if (sdslen(mi->bulkreadBuffer) > cserver.client_max_querybuf_len) {\n                throw \"Full Sync Streaming Buffer Exceeded (increase client_max_querybuf_len)\";\n            }\n\n            while (sdslen(mi->bulkreadBuffer) > offset) {\n                // Pop completed items\n                mi->parseState->trimState();\n                if (mi->parseState->depth() == 0)\n                    break;\n\n                if (mi->bulkreadBuffer[offset] == '*') {\n                    // Starting an array\n                    long long arraySize;\n\n                    // Lets read the array length\n                    offset += parseCount(mi->bulkreadBuffer + offset, sdslen(mi->bulkreadBuffer) - offset, &arraySize);\n                    if (arraySize < 0)\n                        throw \"Invalid array size\";\n\n                    mi->parseState->pushArray(arraySize);\n                } else if (mi->bulkreadBuffer[offset] == '$') {\n                    // Loading in a string\n                    long long payloadsize = 0;\n\n                    // Lets read the string length\n                    size_t offsetCount = parseCount(mi->bulkreadBuffer + offset, sdslen(mi->bulkreadBuffer) - offset, &payloadsize);\n                    if (payloadsize < 0)\n                        throw \"Invalid array size\";\n\n                    // OK we know how long the string is, now lets make sure the payload is here.\n                    if (sdslen(mi->bulkreadBuffer) < (offset + offsetCount + payloadsize + 2)) {\n                        goto LContinue; // wait for more data (note: we could throw true here, but throw is way more expensive)\n                    }\n\n                    mi->parseState->pushValue(mi->bulkreadBuffer + offset + offsetCount, payloadsize);\n\n                    // On to the next one\n                    offset += offsetCount + payloadsize + 2;\n                } else if (mi->bulkreadBuffer[offset] == ':') {\n                    // Numeral\n                    long long value;\n\n                    size_t offsetValue = parseCount(mi->bulkreadBuffer + offset, sdslen(mi->bulkreadBuffer) - offset, &value);\n\n                    mi->parseState->pushValue(value);\n                    offset += offsetValue;\n                } else {\n                    serverLog(LL_WARNING, \"Bad protocol from MASTER: %s\", mi->bulkreadBuffer+offset);\n                    cancelReplicationHandshake(mi, true);\n                    return false;\n                }\n            }\n\n            sdsrange(mi->bulkreadBuffer, offset, -1);\n            offset = 0;\n\n            // Cleanup the remaining stack\n            mi->parseState->trimState();\n\n            if (mi->parseState->depth() != 0)\n                return false;\n\n            long long repl_stream_db = mi->parseState->getMetaDataLongLong(\"repl-stream-db\");\n            if (repl_stream_db < INT_MIN || repl_stream_db > INT_MAX) {\n                throw \"Invalid repl_stream_db value\";\n            }\n            rsi.repl_stream_db = static_cast<int>(repl_stream_db);\n            rsi.repl_offset = mi->parseState->getMetaDataLongLong(\"repl-offset\");\n            sds str = mi->parseState->getMetaDataStr(\"repl-id\");\n            if (sdslen(str) == CONFIG_RUN_ID_SIZE) {\n                memcpy(rsi.repl_id, str, CONFIG_RUN_ID_SIZE+1);\n                rsi.repl_id_is_set = 1;\n            }\n\n            fFinished = true;\n            break;  // We're done!!!\n        }\n        catch (const char *sz) {\n            serverLog(LL_WARNING, \"%s\", sz);\n            cancelReplicationHandshake(mi, true);\n            return false;\n        } catch (bool fContinue) {\n            if (!fContinue) {\n                cancelReplicationHandshake(mi, true);\n                return false;\n            }\n        }\n    LContinue:\n        sdsrange(mi->bulkreadBuffer, offset, -1);\n    }\n\n    if (!fFinished)\n        return false;\n\n    serverLog(LL_NOTICE, \"Fast sync complete\");\n    delete mi->parseState;\n    mi->parseState = nullptr;\n    return true;\n}\n\n/* Asynchronously read the SYNC payload we receive from a master */\n#define REPL_MAX_WRITTEN_BEFORE_FSYNC (1024*1024*8) /* 8 MB */\nbool readSyncBulkPayloadRdb(connection *conn, redisMaster *mi, rdbSaveInfo &rsi, int &usemark) {\n    char buf[PROTO_IOBUF_LEN];\n    ssize_t nread, readlen, nwritten;\n    int use_diskless_load = useDisklessLoad();\n    const dbBackup *diskless_load_backup = NULL;\n    rsi.fForceSetKey = !!g_pserver->fActiveReplica;\n    int empty_db_flags = g_pserver->repl_slave_lazy_flush ? EMPTYDB_ASYNC :\n                                                        EMPTYDB_NO_FLAGS;\n    off_t left;\n    // Should we update our database, or create from scratch?\n    int fUpdate = g_pserver->fActiveReplica || g_pserver->enable_multimaster;\n\n    serverAssert(GlobalLocksAcquired());\n    serverAssert(mi->master == nullptr);\n\n    /* Static vars used to hold the EOF mark, and the last bytes received\n     * from the server: when they match, we reached the end of the transfer. */\n    static char eofmark[CONFIG_RUN_ID_SIZE];\n    static char lastbytes[CONFIG_RUN_ID_SIZE];\n\n    /* If repl_transfer_size == -1 we still have to read the bulk length\n     * from the master reply. */\n    if (mi->repl_transfer_size == -1) {\n        if (connSyncReadLine(conn,buf,1024,g_pserver->repl_syncio_timeout*1000) == -1) {\n            serverLog(LL_WARNING,\n                \"I/O error reading bulk count from MASTER: %s\",\n                strerror(errno));\n            goto error;\n        }\n\n        if (buf[0] == '-') {\n            serverLog(LL_WARNING,\n                \"MASTER aborted replication with an error: %s\",\n                buf+1);\n            goto error;\n        } else if (buf[0] == '\\0') {\n            /* At this stage just a newline works as a PING in order to take\n             * the connection live. So we refresh our last interaction\n             * timestamp. */\n            mi->repl_transfer_lastio = g_pserver->unixtime;\n            return false;\n        } else if (buf[0] != '$') {\n            serverLog(LL_WARNING,\"Bad protocol from MASTER, the first byte is not '$' (we received '%s'), are you sure the host and port are right?\", buf);\n            goto error;\n        }\n\n        /* There are two possible forms for the bulk payload. One is the\n         * usual $<count> bulk format. The other is used for diskless transfers\n         * when the master does not know beforehand the size of the file to\n         * transfer. In the latter case, the following format is used:\n         *\n         * $EOF:<40 bytes delimiter>\n         *\n         * At the end of the file the announced delimiter is transmitted. The\n         * delimiter is long and random enough that the probability of a\n         * collision with the actual file content can be ignored. */\n        if (strncmp(buf+1,\"EOF:\",4) == 0 && strlen(buf+5) >= CONFIG_RUN_ID_SIZE) {\n            usemark = 1;\n            memcpy(eofmark,buf+5,CONFIG_RUN_ID_SIZE);\n            memset(lastbytes,0,CONFIG_RUN_ID_SIZE);\n            /* Set any repl_transfer_size to avoid entering this code path\n             * at the next call. */\n            mi->repl_transfer_size = 0;\n            serverLog(LL_NOTICE,\n                \"MASTER <-> REPLICA sync: receiving streamed RDB from master with EOF %s\",\n                use_diskless_load? \"to parser\":\"to disk\");\n        } else {\n            usemark = 0;\n            mi->repl_transfer_size = strtol(buf+1,NULL,10);\n            serverLog(LL_NOTICE,\n                \"MASTER <-> REPLICA sync: receiving %lld bytes from master %s\",\n                (long long) mi->repl_transfer_size,\n                use_diskless_load? \"to parser\":\"to disk\");\n        }\n        return false;\n    }\n\n    if (!use_diskless_load) {\n        /* Read the data from the socket, store it to a file and search\n         * for the EOF. */\n        if (usemark) {\n            readlen = sizeof(buf);\n        } else {\n            left = mi->repl_transfer_size - mi->repl_transfer_read;\n            readlen = (left < (signed)sizeof(buf)) ? left : (signed)sizeof(buf);\n        }\n\n        nread = connRead(conn,buf,readlen);\n        if (nread <= 0) {\n            if (connGetState(conn) == CONN_STATE_CONNECTED) {\n                /* equivalent to EAGAIN */\n                return false;\n            }\n            serverLog(LL_WARNING,\"I/O error trying to sync with MASTER: %s\",\n                (nread == -1) ? strerror(errno) : \"connection lost\");\n            cancelReplicationHandshake(mi, true);\n            return false;\n        }\n        g_pserver->stat_net_input_bytes += nread;\n\n        /* When a mark is used, we want to detect EOF asap in order to avoid\n         * writing the EOF mark into the file... */\n        int eof_reached = 0;\n\n        if (usemark) {\n            /* Update the last bytes array, and check if it matches our\n             * delimiter. */\n            if (nread >= CONFIG_RUN_ID_SIZE) {\n                memcpy(lastbytes,buf+nread-CONFIG_RUN_ID_SIZE,\n                       CONFIG_RUN_ID_SIZE);\n            } else {\n                int rem = CONFIG_RUN_ID_SIZE-nread;\n                memmove(lastbytes,lastbytes+nread,rem);\n                memcpy(lastbytes+rem,buf,nread);\n            }\n            if (memcmp(lastbytes,eofmark,CONFIG_RUN_ID_SIZE) == 0)\n                eof_reached = 1;\n        }\n\n        /* Update the last I/O time for the replication transfer (used in\n         * order to detect timeouts during replication), and write what we\n         * got from the socket to the dump file on disk. */\n        mi->repl_transfer_lastio = g_pserver->unixtime;\n        if ((nwritten = write(mi->repl_transfer_fd,buf,nread)) != nread) {\n            serverLog(LL_WARNING,\n                \"Write error or short write writing to the DB dump file \"\n                \"needed for MASTER <-> REPLICA synchronization: %s\",\n                (nwritten == -1) ? strerror(errno) : \"short write\");\n            goto error;\n        }\n        mi->repl_transfer_read += nread;\n\n        /* Delete the last 40 bytes from the file if we reached EOF. */\n        if (usemark && eof_reached) {\n            if (ftruncate(mi->repl_transfer_fd,\n                mi->repl_transfer_read - CONFIG_RUN_ID_SIZE) == -1)\n            {\n                serverLog(LL_WARNING,\n                    \"Error truncating the RDB file received from the master \"\n                    \"for SYNC: %s\", strerror(errno));\n                goto error;\n            }\n        }\n\n        /* Sync data on disk from time to time, otherwise at the end of the\n         * transfer we may suffer a big delay as the memory buffers are copied\n         * into the actual disk. */\n        if (mi->repl_transfer_read >=\n            mi->repl_transfer_last_fsync_off + REPL_MAX_WRITTEN_BEFORE_FSYNC)\n        {\n            off_t sync_size = mi->repl_transfer_read -\n                              mi->repl_transfer_last_fsync_off;\n            rdb_fsync_range(mi->repl_transfer_fd,\n                mi->repl_transfer_last_fsync_off, sync_size);\n            mi->repl_transfer_last_fsync_off += sync_size;\n        }\n\n        /* Check if the transfer is now complete */\n        if (!usemark) {\n            if (mi->repl_transfer_read == mi->repl_transfer_size)\n                eof_reached = 1;\n        }\n\n        /* If the transfer is yet not complete, we need to read more, so\n         * return ASAP and wait for the handler to be called again. */\n        if (!eof_reached) return false;\n    }\n\n    /* We reach this point in one of the following cases:\n     *\n     * 1. The replica is using diskless replication, that is, it reads data\n     *    directly from the socket to the Redis memory, without using\n     *    a temporary RDB file on disk. In that case we just block and\n     *    read everything from the socket.\n     *\n     * 2. Or when we are done reading from the socket to the RDB file, in\n     *    such case we want just to read the RDB file in memory. */\n\n    /* We need to stop any AOF rewriting child before flusing and parsing\n     * the RDB, otherwise we'll create a copy-on-write disaster. */\n    if (g_pserver->aof_state != AOF_OFF) stopAppendOnly();\n\n    if (use_diskless_load &&\n            g_pserver->repl_diskless_load == REPL_DISKLESS_LOAD_SWAPDB)\n    {\n        /* Create a backup of g_pserver->db[] and initialize to empty\n         * dictionaries. */\n        diskless_load_backup = disklessLoadMakeBackup();\n    }\n    \n    /* We call to emptyDb even in case of REPL_DISKLESS_LOAD_SWAPDB\n     * (Where disklessLoadMakeBackup left g_pserver->db empty) because we\n     * want to execute all the auxiliary logic of emptyDb (Namely,\n     * fire module events) */\n    if (!fUpdate) {\n        serverLog(LL_NOTICE, \"MASTER <-> REPLICA sync: Flushing old data\");\n        emptyDb(-1,empty_db_flags,replicationEmptyDbCallback);\n    }\n\n    /* Before loading the DB into memory we need to delete the readable\n     * handler, otherwise it will get called recursively since\n     * rdbLoad() will call the event loop to process events from time to\n     * time for non blocking loading. */\n    connSetReadHandler(conn, NULL);\n    serverLog(LL_NOTICE, \"MASTER <-> REPLICA sync: Loading DB in memory\");\n    \n    if (use_diskless_load) {\n        rio rdb;\n        rioInitWithConn(&rdb,conn,mi->repl_transfer_size);\n\n        /* Put the socket in blocking mode to simplify RDB transfer.\n         * We'll restore it when the RDB is received. */\n        connBlock(conn);\n        connRecvTimeout(conn, g_pserver->repl_timeout*1000);\n        startLoading(mi->repl_transfer_size, RDBFLAGS_REPLICATION);\n\n        if (rdbLoadRio(&rdb,RDBFLAGS_REPLICATION,&rsi) != C_OK) {\n            /* RDB loading failed. */\n            stopLoading(0);\n            serverLog(LL_WARNING,\n                \"Failed trying to load the MASTER synchronization DB \"\n                \"from socket\");\n            cancelReplicationHandshake(mi,true);\n            rioFreeConn(&rdb, NULL);\n\n            /* Remove the half-loaded data in case we started with\n             * an empty replica. */\n            emptyDb(-1,empty_db_flags,replicationEmptyDbCallback);\n\n            if (g_pserver->repl_diskless_load == REPL_DISKLESS_LOAD_SWAPDB) {\n                /* Restore the backed up databases. */\n                disklessLoadRestoreBackup(diskless_load_backup);\n            }\n\n            /* Note that there's no point in restarting the AOF on SYNC\n             * failure, it'll be restarted when sync succeeds or the replica\n             * gets promoted. */\n            return false;\n        }\n        if (g_pserver->fActiveReplica) updateActiveReplicaMastersFromRsi(&rsi);\n\n        /* RDB loading succeeded if we reach this point. */\n        if (g_pserver->repl_diskless_load == REPL_DISKLESS_LOAD_SWAPDB) {\n            /* Delete the backup databases we created before starting to load\n             * the new RDB. Now the RDB was loaded with success so the old\n             * data is useless. */\n            disklessLoadDiscardBackup(diskless_load_backup, empty_db_flags);\n        }\n\n        /* Verify the end mark is correct. */\n        if (usemark) {\n            if (!rioRead(&rdb,buf,CONFIG_RUN_ID_SIZE) ||\n                memcmp(buf,eofmark,CONFIG_RUN_ID_SIZE) != 0)\n            {\n                stopLoading(0);\n                serverLog(LL_WARNING,\"Replication stream EOF marker is broken\");\n                cancelReplicationHandshake(mi,true);\n                rioFreeConn(&rdb, NULL);\n                return false;\n            }\n        }\n\n        stopLoading(1);\n\n        /* Cleanup and restore the socket to the original state to continue\n         * with the normal replication. */\n        rioFreeConn(&rdb, NULL);\n        connNonBlock(conn);\n        connRecvTimeout(conn,0);\n    } else {\n        /* Ensure background save doesn't overwrite synced data */\n        if (g_pserver->FRdbSaveInProgress()) {\n            serverLog(LL_NOTICE,\n                \"Replica is about to load the RDB file received from the \"\n                \"master, but there is a pending RDB child running. \"\n                \"Cancelling RDB the save and removing its temp file to avoid \"\n                \"any race\");\n            killRDBChild();\n        }\n\n        const char *rdb_filename = mi->repl_transfer_tmpfile;\n\n        /* Make sure the new file (also used for persistence) is fully synced\n         * (not covered by earlier calls to rdb_fsync_range). */\n        if (fsync(mi->repl_transfer_fd) == -1) {\n            serverLog(LL_WARNING,\n                \"Failed trying to sync the temp DB to disk in \"\n                \"MASTER <-> REPLICA synchronization: %s\",\n                strerror(errno));\n            cancelReplicationHandshake(mi,true);\n            return false;\n        }\n\n        /* Rename rdb like renaming rewrite aof asynchronously. */\n        if (!fUpdate) {\n            int old_rdb_fd = open(g_pserver->rdb_filename,O_RDONLY|O_NONBLOCK);\n            if (rename(mi->repl_transfer_tmpfile,g_pserver->rdb_filename) == -1) {\n                serverLog(LL_WARNING,\n                    \"Failed trying to rename the temp DB into %s in \"\n                    \"MASTER <-> REPLICA synchronization: %s\",\n                    g_pserver->rdb_filename, strerror(errno));\n                cancelReplicationHandshake(mi,true);\n                if (old_rdb_fd != -1) close(old_rdb_fd);\n                return false;\n            }\n            rdb_filename = g_pserver->rdb_filename;\n            \n            /* Close old rdb asynchronously. */\n            if (old_rdb_fd != -1) bioCreateCloseJob(old_rdb_fd);\n        }\n\n        if (g_pserver->fActiveReplica)\n        {\n            rsi.mvccMinThreshold = mi->mvccLastSync;\n            if (mi->staleKeyMap != nullptr)\n                mi->staleKeyMap->clear();\n            else\n                mi->staleKeyMap = new (MALLOC_LOCAL) std::map<int, std::vector<robj_sharedptr>>();\n            rsi.mi = mi;\n        }\n        if (rdbLoadFile(rdb_filename,&rsi,RDBFLAGS_REPLICATION) != C_OK) {\n            serverLog(LL_WARNING,\n                \"Failed trying to load the MASTER synchronization \"\n                \"DB from disk\");\n            cancelReplicationHandshake(mi,true);\n            if (g_pserver->rdb_del_sync_files && allPersistenceDisabled()) {\n                serverLog(LL_NOTICE,\"Removing the RDB file obtained from \"\n                                    \"the master. This replica has persistence \"\n                                    \"disabled\");\n                bg_unlink(g_pserver->rdb_filename);\n            }\n            /* Note that there's no point in restarting the AOF on sync failure,\n               it'll be restarted when sync succeeds or replica promoted. */\n            return false;\n        }\n        if (g_pserver->fActiveReplica) updateActiveReplicaMastersFromRsi(&rsi);\n\n        /* Cleanup. */\n        if (g_pserver->rdb_del_sync_files && allPersistenceDisabled()) {\n            serverLog(LL_NOTICE,\"Removing the RDB file obtained from \"\n                                \"the master. This replica has persistence \"\n                                \"disabled\");\n            bg_unlink(g_pserver->rdb_filename);\n        }\n        if (fUpdate)\n            unlink(mi->repl_transfer_tmpfile);\n        zfree(mi->repl_transfer_tmpfile);\n        close(mi->repl_transfer_fd);\n        mi->repl_transfer_fd = -1;\n        mi->repl_transfer_tmpfile = NULL;\n    }\n\n    return true;\nerror:\n    cancelReplicationHandshake(mi,true);\n    return false;\n}\n\nvoid readSyncBulkPayload(connection *conn) {\n    serverAssert(GlobalLocksAcquired());\n    rdbSaveInfo rsi;\n    redisMaster *mi = (redisMaster*)connGetPrivateData(conn);\n    static int usemark = 0;\n    if (mi == nullptr || conn != mi->repl_transfer_s) {\n        // We're about to be free'd so bail out\n        return;\n    }\n\n    if (mi->isKeydbFastsync) {\n        if (!readSnapshotBulkPayload(conn, mi, rsi))\n            return;\n    } else {\n        if (!readSyncBulkPayloadRdb(conn, mi, rsi, usemark))\n            return;\n    }\n\n    if (conn != mi->repl_transfer_s)\n        return;\n\n    /* Final setup of the connected slave <- master link */\n    replicationCreateMasterClient(mi,mi->repl_transfer_s,rsi.repl_stream_db);\n    if (mi->isKeydbFastsync) {\n        /* We need to handle the case where the initial querybuf data was read by fast sync */\n        /* This should match the work readQueryFromClient would do for a master client */\n        mi->master->querybuf = sdscatsds(mi->master->querybuf, mi->bulkreadBuffer);\n        mi->master->pending_querybuf = sdscatsds(mi->master->pending_querybuf, mi->bulkreadBuffer);\n        mi->master->read_reploff += sdslen(mi->bulkreadBuffer);\n\n        sdsfree(mi->bulkreadBuffer);\n        mi->bulkreadBuffer = nullptr;\n    }\n    mi->repl_transfer_s = nullptr;\n    mi->repl_state = REPL_STATE_CONNECTED;\n    mi->repl_down_since = 0;\n\n    /* Fire the master link modules event. */\n    moduleFireServerEvent(REDISMODULE_EVENT_MASTER_LINK_CHANGE,\n                          REDISMODULE_SUBEVENT_MASTER_LINK_UP,\n                          NULL);\n\n    /* After a full resynchronization we use the replication ID and\n     * offset of the master. The secondary ID / offset are cleared since\n     * we are starting a new history. */\n    if (!g_pserver->fActiveReplica)\n    {\n        /* After a full resynchroniziation we use the replication ID and\n        * offset of the master. The secondary ID / offset are cleared since\n        * we are starting a new history. */\n        memcpy(g_pserver->replid,mi->master->replid,sizeof(g_pserver->replid));\n        g_pserver->master_repl_offset = mi->master->reploff;\n        if (g_pserver->repl_batch_offStart >= 0)\n            g_pserver->repl_batch_offStart = g_pserver->master_repl_offset;\n        saveMasterStatusToStorage(false);\n    }\n    clearReplicationId2();\n\n    /* Let's create the replication backlog if needed. Slaves need to\n     * accumulate the backlog regardless of the fact they have sub-slaves\n     * or not, in order to behave correctly if they are promoted to\n     * masters after a failover. */\n    if (g_pserver->repl_backlog == NULL) runAndPropogateToReplicas(createReplicationBacklog);\n    serverLog(LL_NOTICE, \"MASTER <-> REPLICA sync: Finished with success\");\n\n    if (cserver.supervised_mode == SUPERVISED_SYSTEMD) {\n        redisCommunicateSystemd(\"STATUS=MASTER <-> REPLICA sync: Finished with success. Ready to accept connections in read-write mode.\\n\");\n    }\n\n    /* Send the initial ACK immediately to put this replica in online state. */\n    if (usemark || mi->isKeydbFastsync) replicationSendAck(mi);\n\n    /* Restart the AOF subsystem now that we finished the sync. This\n     * will trigger an AOF rewrite, and when done will start appending\n     * to the new file. */\n    if (g_pserver->aof_enabled) restartAOFAfterSYNC();\n    if (mi->isKeydbFastsync)\n        readQueryFromClient(conn); // There may be querybuf data we just appeneded\n    return;\n}\n\nchar *receiveSynchronousResponse(redisMaster *mi, connection *conn) {\n    char buf[256];\n    /* Read the reply from the server. */\n    if (connSyncReadLine(conn,buf,sizeof(buf),g_pserver->repl_syncio_timeout*1000) == -1)\n    {\n        return sdscatprintf(sdsempty(),\"-Reading from master: %s\",\n                strerror(errno));\n    }\n    mi->repl_transfer_lastio = g_pserver->unixtime;\n    return sdsnew(buf);\n}\n\n/* Send a pre-formatted multi-bulk command to the connection. */\nchar* sendCommandRaw(connection *conn, sds cmd) {\n    if (connSyncWrite(conn,cmd,sdslen(cmd),g_pserver->repl_syncio_timeout*1000) == -1) {\n        return sdscatprintf(sdsempty(),\"-Writing to master: %s\",\n                connGetLastError(conn));\n    }\n    return NULL;\n}\n\n/* Compose a multi-bulk command and send it to the connection.\n * Used to send AUTH and REPLCONF commands to the master before starting the\n * replication.\n *\n * Takes a list of char* arguments, terminated by a NULL argument.\n *\n * The command returns an sds string representing the result of the\n * operation. On error the first byte is a \"-\".\n */\nchar *sendCommand(connection *conn, ...) {\n    va_list ap;\n    sds cmd = sdsempty();\n    sds cmdargs = sdsempty();\n    size_t argslen = 0;\n    char *arg;\n\n    /* Create the command to send to the master, we use redis binary\n     * protocol to make sure correct arguments are sent. This function\n     * is not safe for all binary data. */\n    va_start(ap,conn);\n    while(1) {\n        arg = va_arg(ap, char*);\n        if (arg == NULL) break;\n        cmdargs = sdscatprintf(cmdargs,\"$%zu\\r\\n%s\\r\\n\",strlen(arg),arg);\n        argslen++;\n    }\n\n    cmd = sdscatprintf(cmd,\"*%zu\\r\\n\",argslen);\n    cmd = sdscatsds(cmd,cmdargs);\n    sdsfree(cmdargs);\n\n    va_end(ap);\n    char* err = sendCommandRaw(conn, cmd);\n    sdsfree(cmd);\n    if(err)\n        return err;\n    return NULL;\n}\n\n/* Compose a multi-bulk command and send it to the connection. \n * Used to send AUTH and REPLCONF commands to the master before starting the\n * replication.\n *\n * argv_lens is optional, when NULL, strlen is used.\n *\n * The command returns an sds string representing the result of the\n * operation. On error the first byte is a \"-\".\n */\nchar *sendCommandArgv(connection *conn, int argc, const char **argv, size_t *argv_lens) {\n    sds cmd = sdsempty();\n    const char *arg;\n    int i;\n\n    /* Create the command to send to the master. */\n    cmd = sdscatfmt(cmd,\"*%i\\r\\n\",argc);\n    for (i=0; i<argc; i++) {\n        int len;\n        arg = argv[i];\n        len = argv_lens ? argv_lens[i] : strlen(arg);\n        cmd = sdscatfmt(cmd,\"$%i\\r\\n\",len);\n        cmd = sdscatlen(cmd,arg,len);\n        cmd = sdscatlen(cmd,\"\\r\\n\",2);\n    }\n    char* err = sendCommandRaw(conn, cmd);\n    sdsfree(cmd);\n    if (err)\n        return err;\n    return NULL;\n}\n\n/* Try a partial resynchronization with the master if we are about to reconnect.\n * If there is no cached master structure, at least try to issue a\n * \"PSYNC ? -1\" command in order to trigger a full resync using the PSYNC\n * command in order to obtain the master replid and the master replication\n * global offset.\n *\n * This function is designed to be called from syncWithMaster(), so the\n * following assumptions are made:\n *\n * 1) We pass the function an already connected socket \"fd\".\n * 2) This function does not close the file descriptor \"fd\". However in case\n *    of successful partial resynchronization, the function will reuse\n *    'fd' as file descriptor of the g_pserver->master client structure.\n *\n * The function is split in two halves: if read_reply is 0, the function\n * writes the PSYNC command on the socket, and a new function call is\n * needed, with read_reply set to 1, in order to read the reply of the\n * command. This is useful in order to support non blocking operations, so\n * that we write, return into the event loop, and read when there are data.\n *\n * When read_reply is 0 the function returns PSYNC_WRITE_ERR if there\n * was a write error, or PSYNC_WAIT_REPLY to signal we need another call\n * with read_reply set to 1. However even when read_reply is set to 1\n * the function may return PSYNC_WAIT_REPLY again to signal there were\n * insufficient data to read to complete its work. We should re-enter\n * into the event loop and wait in such a case.\n *\n * The function returns:\n *\n * PSYNC_CONTINUE: If the PSYNC command succeeded and we can continue.\n * PSYNC_FULLRESYNC: If PSYNC is supported but a full resync is needed.\n *                   In this case the master replid and global replication\n *                   offset is saved.\n * PSYNC_NOT_SUPPORTED: If the server does not understand PSYNC at all and\n *                      the caller should fall back to SYNC.\n * PSYNC_WRITE_ERROR: There was an error writing the command to the socket.\n * PSYNC_WAIT_REPLY: Call again the function with read_reply set to 1.\n * PSYNC_TRY_LATER: Master is currently in a transient error condition.\n *\n * Notable side effects:\n *\n * 1) As a side effect of the function call the function removes the readable\n *    event handler from \"fd\", unless the return value is PSYNC_WAIT_REPLY.\n * 2) g_pserver->master_initial_offset is set to the right value according\n *    to the master reply. This will be used to populate the 'g_pserver->master'\n *    structure replication offset.\n */\n\n#define PSYNC_WRITE_ERROR 0\n#define PSYNC_WAIT_REPLY 1\n#define PSYNC_CONTINUE 2\n#define PSYNC_FULLRESYNC 3\n#define PSYNC_NOT_SUPPORTED 4\n#define PSYNC_TRY_LATER 5\nint slaveTryPartialResynchronization(redisMaster *mi, connection *conn, int read_reply) {\n    const char *psync_replid;\n    char psync_offset[32];\n    sds reply;\n\n    /* Writing half */\n    if (!read_reply) {\n        /* Initially set master_initial_offset to -1 to mark the current\n         * master replid and offset as not valid. Later if we'll be able to do\n         * a FULL resync using the PSYNC command we'll set the offset at the\n         * right value, so that this information will be propagated to the\n         * client structure representing the master into g_pserver->master. */\n        mi->master_initial_offset = -1;\n\n        if (mi->cached_master) {\n            psync_replid = mi->cached_master->replid;\n            snprintf(psync_offset,sizeof(psync_offset),\"%lld\", mi->cached_master->reploff+1);\n            serverLog(LL_NOTICE,\"Trying a partial resynchronization (request %s:%s).\", psync_replid, psync_offset);\n        } else {\n            serverLog(LL_NOTICE,\"Partial resynchronization not possible (no cached master)\");\n            psync_replid = \"?\";\n            memcpy(psync_offset,\"-1\",3);\n        }\n\n        /* Issue the PSYNC command, if this is a master with a failover in\n         * progress then send the failover argument to the replica to cause it\n         * to become a master */\n        if (g_pserver->failover_state == FAILOVER_IN_PROGRESS) {\n            reply = sendCommand(conn,\"PSYNC\",psync_replid,psync_offset,\"FAILOVER\",NULL);\n        } else {\n            reply = sendCommand(conn,\"PSYNC\",psync_replid,psync_offset,NULL);\n        }\n\n        if (reply != NULL) {\n            serverLog(LL_WARNING,\"Unable to send PSYNC to master: %s\",reply);\n            sdsfree(reply);\n            connSetReadHandler(conn, NULL);\n            return PSYNC_WRITE_ERROR;\n        }\n        return PSYNC_WAIT_REPLY;\n    }\n\n    /* Reading half */\n    reply = receiveSynchronousResponse(mi, conn);\n    if (sdslen(reply) == 0) {\n        /* The master may send empty newlines after it receives PSYNC\n         * and before to reply, just to keep the connection alive. */\n        sdsfree(reply);\n        return PSYNC_WAIT_REPLY;\n    }\n\n    connSetReadHandler(conn, NULL);\n\n    if (!strncmp(reply,\"+FULLRESYNC\",11)) {\n        char *replid = NULL, *offset = NULL;\n\n        /* FULL RESYNC, parse the reply in order to extract the replid\n         * and the replication offset. */\n        replid = strchr(reply,' ');\n        if (replid) {\n            replid++;\n            offset = strchr(replid,' ');\n            if (offset) offset++;\n        }\n        if (!replid || !offset || (offset-replid-1) != CONFIG_RUN_ID_SIZE) {\n            serverLog(LL_WARNING,\n                \"Master replied with wrong +FULLRESYNC syntax.\");\n            /* This is an unexpected condition, actually the +FULLRESYNC\n             * reply means that the master supports PSYNC, but the reply\n             * format seems wrong. To stay safe we blank the master\n             * replid to make sure next PSYNCs will fail. */\n            memset(mi->master_replid,0,CONFIG_RUN_ID_SIZE+1);\n        } else {\n            memcpy(mi->master_replid, replid, offset-replid-1);\n            mi->master_replid[CONFIG_RUN_ID_SIZE] = '\\0';\n            mi->master_initial_offset = strtoll(offset,NULL,10);\n            serverLog(LL_NOTICE,\"Full resync from master: %s:%lld\",\n                mi->master_replid,\n                mi->master_initial_offset);\n        }\n        /* We are going to full resync, discard the cached master structure. */\n        replicationDiscardCachedMaster(mi);\n        sdsfree(reply);\n        return PSYNC_FULLRESYNC;\n    }\n\n    if (!strncmp(reply,\"+CONTINUE\",9)) {\n        /* Partial resync was accepted. */\n        serverLog(LL_NOTICE,\n            \"Successful partial resynchronization with master.\");\n\n        /* Check the new replication ID advertised by the master. If it\n         * changed, we need to set the new ID as primary ID, and set or\n         * secondary ID as the old master ID up to the current offset, so\n         * that our sub-slaves will be able to PSYNC with us after a\n         * disconnection. */\n        char *start = reply+10;\n        char *end = reply+9;\n        while(end[0] != '\\r' && end[0] != '\\n' && end[0] != '\\0') end++;\n        if (end-start == CONFIG_RUN_ID_SIZE) {\n            char sznew[CONFIG_RUN_ID_SIZE+1];\n            memcpy(sznew,start,CONFIG_RUN_ID_SIZE);\n            sznew[CONFIG_RUN_ID_SIZE] = '\\0';\n\n            if (strcmp(sznew,mi->cached_master->replid)) {\n                /* Master ID changed. */\n                serverLog(LL_WARNING,\"Master replication ID changed to %s\",sznew);\n\n                /* Set the old ID as our ID2, up to the current offset+1. */\n                memcpy(g_pserver->replid2,mi->cached_master->replid,\n                    sizeof(g_pserver->replid2));\n                g_pserver->second_replid_offset = g_pserver->master_repl_offset+1;\n\n                if (!g_pserver->fActiveReplica) {\n                    /* Update the cached master ID and our own primary ID to the\n                     * new one. */\n                    memcpy(g_pserver->replid,sznew,sizeof(g_pserver->replid));\n                    memcpy(mi->cached_master->replid,sznew,sizeof(g_pserver->replid));\n\n                    /* Disconnect all the replicas: they need to be notified. */\n                    disconnectSlaves();\n                }\n            }\n        }\n\n        /* Setup the replication to continue. */\n        sdsfree(reply);\n        replicationResurrectCachedMaster(mi, conn);\n\n        /* If this instance was restarted and we read the metadata to\n         * PSYNC from the persistence file, our replication backlog could\n         * be still not initialized. Create it. */\n        if (g_pserver->repl_backlog == NULL) runAndPropogateToReplicas(createReplicationBacklog);\n        return PSYNC_CONTINUE;\n    }\n\n    /* If we reach this point we received either an error (since the master does\n     * not understand PSYNC or because it is in a special state and cannot\n     * serve our request), or an unexpected reply from the master.\n     *\n     * Return PSYNC_NOT_SUPPORTED on errors we don't understand, otherwise\n     * return PSYNC_TRY_LATER if we believe this is a transient error. */\n\n    if (!strncmp(reply,\"-NOMASTERLINK\",13) ||\n        !strncmp(reply,\"-LOADING\",8))\n    {\n        serverLog(LL_NOTICE,\n            \"Master is currently unable to PSYNC \"\n            \"but should be in the future: %s\", reply);\n        sdsfree(reply);\n        return PSYNC_TRY_LATER;\n    }\n\n    if (strncmp(reply,\"-ERR\",4)) {\n        /* If it's not an error, log the unexpected event. */\n        serverLog(LL_WARNING,\n            \"Unexpected reply to PSYNC from master: %s\", reply);\n    } else {\n        serverLog(LL_NOTICE,\n            \"Master does not support PSYNC or is in \"\n            \"error state (reply: %s)\", reply);\n    }\n    sdsfree(reply);\n    replicationDiscardCachedMaster(mi);\n    return PSYNC_NOT_SUPPORTED;\n}\n\nvoid parseMasterCapa(redisMaster *mi, sds strcapa)\n{\n    if (sdslen(strcapa) < 1 || strcapa[0] != '+')\n        return;\n\n    char *szStart = strcapa + 1;    // skip the +\n    char *pchEnd = szStart;\n\n    mi->isActive = false;\n    mi->isKeydbFastsync = false;\n    for (;;)\n    {\n        if (*pchEnd == ' ' || *pchEnd == '\\0') {\n            // Parse the word\n            if (strncmp(szStart, \"active-replica\", pchEnd - szStart) == 0) {\n                mi->isActive = true;\n            } else if (strncmp(szStart, \"keydb-fastsync-save\", pchEnd - szStart) == 0) {\n                mi->isKeydbFastsync = true;\n            }\n            szStart = pchEnd + 1;\n        }\n        if (*pchEnd == '\\0')\n            break;\n        ++pchEnd;\n    }\n}\n\n/* This handler fires when the non blocking connect was able to\n * establish a connection with the master. */\nvoid syncWithMaster(connection *conn) {\n    serverAssert(GlobalLocksAcquired());\n    char tmpfile[256] = {0}, *err = NULL;\n    int dfd = -1, maxtries = 5;\n    int psync_result;\n\n    redisMaster *mi = (redisMaster*)connGetPrivateData(conn);\n    if (mi == nullptr) {\n        // We're about to be closed, bail\n        return;\n    }\n\n    /* If this event fired after the user turned the instance into a master\n     * with SLAVEOF NO ONE we must just return ASAP. */\n    if (mi->repl_state == REPL_STATE_NONE) {\n        connClose(conn);\n        return;\n    }\n\n    /* Check for errors in the socket: after a non blocking connect() we\n     * may find that the socket is in error state. */\n    if (connGetState(conn) != CONN_STATE_CONNECTED) {\n        serverLog(LL_WARNING,\"Error condition on socket for SYNC: %s\",\n                connGetLastError(conn));\n        goto error;\n    }\n\nretry_connect:\n    /* Send a PING to check the master is able to reply without errors. */\n    if (mi->repl_state == REPL_STATE_CONNECTING || mi->repl_state == REPL_STATE_RETRY_NOREPLPING) {\n        serverLog(LL_NOTICE,\"Non blocking connect for SYNC fired the event.\");\n        /* Delete the writable event so that the readable event remains\n         * registered and we can wait for the PONG reply. */\n        connSetReadHandler(conn, syncWithMaster);\n        connSetWriteHandler(conn, NULL);\n        /* Send the PING, don't check for errors at all, we have the timeout\n         * that will take care about this. */\n        err = sendCommand(conn,mi->repl_state == REPL_STATE_RETRY_NOREPLPING ? \"PING\" : \"REPLPING\",NULL);\n        if (err) goto write_error;\n        mi->repl_state = REPL_STATE_RECEIVE_PING_REPLY;\n        return;\n    }\n\n    /* Receive the PONG command. */\n    if (mi->repl_state == REPL_STATE_RECEIVE_PING_REPLY) {\n        err = receiveSynchronousResponse(mi, conn);\n\n        /* We accept only two replies as valid, a positive +PONG reply\n         * (we just check for \"+\") or an authentication error.\n         * Note that older versions of Redis replied with \"operation not\n         * permitted\" instead of using a proper error code, so we test\n         * both. */\n        if (strncmp(err,\"-ERR unknown command\",20) == 0) {\n            serverLog(LL_NOTICE,\"Master does not support REPLPING, sending PING instead...\");\n            mi->repl_state = REPL_STATE_RETRY_NOREPLPING;\n            sdsfree(err);\n            err = NULL;\n            goto retry_connect;\n        } else if (err[0] != '+' &&\n            strncmp(err,\"-NOAUTH\",7) != 0 &&\n            strncmp(err,\"-NOPERM\",7) != 0 &&\n            strncmp(err,\"-ERR operation not permitted\",28) != 0)\n        {\n            serverLog(LL_WARNING,\"Error reply to PING from master: '%s'\",err);\n            sdsfree(err);\n            goto error;\n        } else {\n            serverLog(LL_NOTICE,\n                \"Master replied to PING, replication can continue...\");\n        }\n        sdsfree(err);\n        err = NULL;\n        mi->repl_state = REPL_STATE_SEND_HANDSHAKE;\n    }\n\n    if (mi->repl_state == REPL_STATE_SEND_HANDSHAKE) {\n        char szUUID[37] = {0};\n\n        /* AUTH with the master if required. */\n        if (mi->masterauth) {\n            const char *args[3] = {\"AUTH\",NULL,NULL};\n            size_t lens[3] = {4,0,0};\n            int argc = 1;\n            if (mi->masteruser) {\n                args[argc] = mi->masteruser;\n                lens[argc] = strlen(mi->masteruser);\n                argc++;\n            }\n            args[argc] = mi->masterauth;\n            lens[argc] = sdslen(mi->masterauth);\n            argc++;\n            err = sendCommandArgv(conn, argc, args, lens);\n            if (err) goto write_error;\n        }\n\n        /* Set the slave port, so that Master's INFO command can list the\n         * slave listening port correctly. */\n        {\n            int port;\n            if (g_pserver->slave_announce_port)\n                port = g_pserver->slave_announce_port;\n            else if (g_pserver->tls_replication && g_pserver->tls_port)\n                port = g_pserver->tls_port;\n            else\n                port = g_pserver->port;\n            sds portstr = sdsfromlonglong(port);\n            err = sendCommand(conn,\"REPLCONF\",\n                    \"listening-port\",portstr, NULL);\n            sdsfree(portstr);\n            if (err) goto write_error;\n        }\n\n        /* Set the slave ip, so that Master's INFO command can list the\n         * slave IP address port correctly in case of port forwarding or NAT.\n         * Skip REPLCONF ip-address if there is no slave-announce-ip option set. */\n        if (g_pserver->slave_announce_ip) {\n            err = sendCommand(conn,\"REPLCONF\",\n                    \"ip-address\",g_pserver->slave_announce_ip, NULL);\n            if (err) goto write_error;\n        }\n\n        /* Inform the master of our (slave) capabilities.\n         *\n         * EOF: supports EOF-style RDB transfer for diskless replication.\n         * PSYNC2: supports PSYNC v2, so understands +CONTINUE <new repl ID>.\n         *\n         * The master will ignore capabilities it does not understand. */\n\n\n        std::vector<const char*> veccapabilities = {\n            \"REPLCONF\",\n            \"capa\",\"eof\",\n            \"capa\",\"psync2\",\n            \"capa\",\"activeExpire\",\n        };\n        if (FFastSyncEnabled() && g_pserver->repl_diskless_load != REPL_DISKLESS_LOAD_SWAPDB) {\n            veccapabilities.push_back(\"capa\");\n            veccapabilities.push_back(\"keydb-fastsync\");\n        }\n\n        err = sendCommandArgv(conn, veccapabilities.size(), veccapabilities.data(), nullptr);\n        if (err) goto write_error;\n\n        /* Send UUID */\n        memset(mi->master_uuid, 0, UUID_BINARY_LEN);\n        uuid_unparse((unsigned char*)cserver.uuid, szUUID);\n        err = sendCommand(conn,\"REPLCONF\",\"uuid\",szUUID,NULL);\n        if (err) goto write_error;\n\n        mi->repl_state = REPL_STATE_RECEIVE_AUTH_REPLY;\n        return;\n    }\n\n    if (mi->repl_state == REPL_STATE_RECEIVE_AUTH_REPLY && !mi->masterauth)\n        mi->repl_state = REPL_STATE_RECEIVE_PORT_REPLY;\n\n    /* Receive AUTH reply. */\n    if (mi->repl_state == REPL_STATE_RECEIVE_AUTH_REPLY) {\n        err = receiveSynchronousResponse(mi, conn);\n        if (err[0] == '-') {\n            serverLog(LL_WARNING,\"Unable to AUTH to MASTER: %s\",err);\n            sdsfree(err);\n            goto error;\n        }\n        sdsfree(err);\n        err = nullptr;\n        mi->repl_state = REPL_STATE_RECEIVE_PORT_REPLY;\n    }\n\n    /* Receive REPLCONF listening-port reply. */\n    if (mi->repl_state == REPL_STATE_RECEIVE_PORT_REPLY) {\n        err = receiveSynchronousResponse(mi, conn);\n        /* Ignore the error if any, not all the Redis versions support\n         * REPLCONF listening-port. */\n        if (err[0] == '-') {\n            serverLog(LL_NOTICE,\"(Non critical) Master does not understand \"\n                                \"REPLCONF listening-port: %s\", err);\n        }\n        sdsfree(err);\n        mi->repl_state = REPL_STATE_RECEIVE_IP_REPLY;\n        return;\n    }\n\n    if (mi->repl_state == REPL_STATE_RECEIVE_IP_REPLY && !g_pserver->slave_announce_ip)\n        mi->repl_state = REPL_STATE_RECEIVE_CAPA_REPLY;\n\n    /* Receive REPLCONF ip-address reply. */\n    if (mi->repl_state == REPL_STATE_RECEIVE_IP_REPLY) {\n        err = receiveSynchronousResponse(mi, conn);\n        /* Ignore the error if any, not all the Redis versions support\n         * REPLCONF listening-port. */\n        if (err[0] == '-') {\n            serverLog(LL_NOTICE,\"(Non critical) Master does not understand \"\n                                \"REPLCONF ip-address: %s\", err);\n        }\n        sdsfree(err);\n        mi->repl_state = REPL_STATE_RECEIVE_CAPA_REPLY;\n        return;\n    }\n\n    /* Receive CAPA reply. */\n    if (mi->repl_state == REPL_STATE_RECEIVE_CAPA_REPLY) {\n        err = receiveSynchronousResponse(mi, conn);\n        /* Ignore the error if any, not all the Redis versions support\n         * REPLCONF capa. */\n        if (err[0] == '-') {\n            serverLog(LL_NOTICE,\"(Non critical) Master does not understand \"\n                                  \"REPLCONF capa: %s\", err);\n        } else {\n            parseMasterCapa(mi, err);\n        }\n        sdsfree(err);\n        err = NULL;\n        mi->repl_state = REPL_STATE_RECEIVE_UUID;\n    }\n\n    /* Receive UUID */\n    if (mi->repl_state == REPL_STATE_RECEIVE_UUID) {\n        err = receiveSynchronousResponse(mi, conn);\n        if (err[0] == '-') {\n            serverLog(LL_WARNING, \"non-fatal: Master doesn't understand REPLCONF uuid\");\n        }\n        else {\n            if (strlen(err) != 37   // 36-byte UUID string and the leading '+'\n                || uuid_parse(err+1, mi->master_uuid) != 0)   \n            {\n                serverLog(LL_WARNING, \"Master replied with a UUID we don't understand\");\n                sdsfree(err);\n                goto error;\n            }\n        }\n        sdsfree(err);\n        err = NULL;\n        mi->repl_state = REPL_STATE_SEND_PSYNC;\n        // fallthrough\n    }\n\n    /* Try a partial resynchonization. If we don't have a cached master\n     * slaveTryPartialResynchronization() will at least try to use PSYNC\n     * to start a full resynchronization so that we get the master replid\n     * and the global offset, to try a partial resync at the next\n     * reconnection attempt. */\n    if (mi->repl_state == REPL_STATE_SEND_PSYNC) {\n        if (slaveTryPartialResynchronization(mi,conn,0) == PSYNC_WRITE_ERROR) {\n            err = sdsnew(\"Write error sending the PSYNC command.\");\n            abortFailover(mi, \"Write error to failover target\");\n            goto write_error;\n        }\n        mi->repl_state = REPL_STATE_RECEIVE_PSYNC_REPLY;\n        return;\n    }\n\n    /* If reached this point, we should be in REPL_STATE_RECEIVE_PSYNC. */\n    if (mi->repl_state != REPL_STATE_RECEIVE_PSYNC_REPLY) {\n        serverLog(LL_WARNING,\"syncWithMaster(): state machine error, \"\n                             \"state should be RECEIVE_PSYNC but is %d\",\n                             mi->repl_state);\n        goto error;\n    }\n\n    psync_result = slaveTryPartialResynchronization(mi,conn,1);\n    if (psync_result == PSYNC_WAIT_REPLY) return; /* Try again later... */\n\n    /* Check the status of the planned failover. We expect PSYNC_CONTINUE,\n     * but there is nothing technically wrong with a full resync which\n     * could happen in edge cases. */\n    if (g_pserver->failover_state == FAILOVER_IN_PROGRESS) {\n        if (psync_result == PSYNC_CONTINUE || psync_result == PSYNC_FULLRESYNC) {\n            clearFailoverState();\n        } else {\n            abortFailover(mi, \"Failover target rejected psync request\");\n            return;\n        }\n    }\n\n    /* If the master is in an transient error, we should try to PSYNC\n        * from scratch later, so go to the error path. This happens when\n        * the server is loading the dataset or is not connected with its\n        * master and so forth. */\n    if (psync_result == PSYNC_TRY_LATER) goto error;\n\n    /* Note: if PSYNC does not return WAIT_REPLY, it will take care of\n        * uninstalling the read handler from the file descriptor. */\n\n    if (psync_result == PSYNC_CONTINUE) {\n        serverLog(LL_NOTICE, \"MASTER <-> REPLICA sync: Master accepted a Partial Resynchronization.\");\n        /* Reset the bulklen information in case it is lingering from the last connection\n         * The partial sync will start from the beginning of a command so these should be reset */\n        mi->master->reqtype = 0;\n        mi->master->multibulklen = 0;\n        mi->master->bulklen = -1;\n        if (cserver.supervised_mode == SUPERVISED_SYSTEMD) {\n            redisCommunicateSystemd(\"STATUS=MASTER <-> REPLICA sync: Partial Resynchronization accepted. Ready to accept connections in read-write mode.\\n\");\n        }\n        return;\n    }\n\n    /* PSYNC failed or is not supported: we want our slaves to resync with us\n     * as well, if we have any sub-slaves. The master may transfer us an\n     * entirely different data set and we have no way to incrementally feed\n     * our slaves after that. */\n    if (!g_pserver->fActiveReplica)\n    {\n        disconnectSlavesExcept(mi->master_uuid); /* Force our slaves to resync with us as well. */\n        freeReplicationBacklog(); /* Don't allow our chained slaves to PSYNC. */\n    }\n\n    /* Fall back to SYNC if needed. Otherwise psync_result == PSYNC_FULLRESYNC\n     * and the g_pserver->master_replid and master_initial_offset are\n     * already populated. */\n    if (psync_result == PSYNC_NOT_SUPPORTED) {\n        serverLog(LL_NOTICE,\"Retrying with SYNC...\");\n        if (connSyncWrite(conn,\"SYNC\\r\\n\",6,g_pserver->repl_syncio_timeout*1000) == -1) {\n            serverLog(LL_WARNING,\"I/O error writing to MASTER: %s\",\n                strerror(errno));\n            goto error;\n        }\n    }\n\n    /* Prepare a suitable temp file for bulk transfer */\n    if (!useDisklessLoad() && !mi->isKeydbFastsync) {\n        while(maxtries--) {\n            auto dt = std::chrono::system_clock::now().time_since_epoch();\n            auto dtMillisecond = std::chrono::duration_cast<std::chrono::milliseconds>(dt);\n            snprintf(tmpfile,sizeof(tmpfile),\n                \"temp-%lld.%ld.rdb\",(long long)dtMillisecond.count(),(long int)getpid());\n            dfd = open(tmpfile,O_CREAT|O_WRONLY|O_EXCL,0644);\n            if (dfd != -1) break;\n            sleep(1);\n        }\n        if (dfd == -1) {\n            serverLog(LL_WARNING,\"Opening the temp file needed for MASTER <-> REPLICA synchronization: %s\",strerror(errno));\n            goto error;\n        }\n        mi->repl_transfer_fd = dfd;\n    }\n\n    /* Setup the non blocking download of the bulk file. */\n    serverAssert(mi->master == nullptr);\n    if (connSetReadHandler(conn, readSyncBulkPayload)\n            == C_ERR)\n    {\n        char conninfo[CONN_INFO_LEN];\n        serverLog(LL_WARNING,\n            \"Can't create readable event for SYNC: %s (%s)\",\n            strerror(errno), connGetInfo(conn, conninfo, sizeof(conninfo)));\n        goto error;\n    }\n\n    mi->repl_state = REPL_STATE_TRANSFER;\n    mi->repl_transfer_size = -1;\n    mi->repl_transfer_read = 0;\n    mi->repl_transfer_last_fsync_off = 0;\n    mi->repl_transfer_lastio = g_pserver->unixtime;\n    if (mi->repl_transfer_tmpfile)\n        zfree(mi->repl_transfer_tmpfile);\n    mi->repl_transfer_tmpfile = zstrdup(tmpfile);\n    return;\n\nerror:\n    if (dfd != -1) close(dfd);\n    connClose(conn);\n    mi->repl_transfer_s = NULL;\n    if (mi->repl_transfer_fd != -1)\n        close(mi->repl_transfer_fd);\n    if (mi->repl_transfer_tmpfile)\n        zfree(mi->repl_transfer_tmpfile);\n    mi->repl_transfer_tmpfile = NULL;\n    mi->repl_transfer_fd = -1;\n    mi->repl_state = REPL_STATE_CONNECT;\n    return;\n\nwrite_error: /* Handle sendCommand() errors. */\n    serverLog(LL_WARNING,\"Sending command to master in replication handshake: %s\", err);\n    sdsfree(err);\n    goto error;\n}\n\nint connectWithMaster(redisMaster *mi) {\n    serverAssert(mi->master == nullptr);\n    mi->repl_transfer_s = g_pserver->tls_replication ? connCreateTLS() : connCreateSocket();\n    mi->ielReplTransfer = serverTL - g_pserver->rgthreadvar;\n    connSetPrivateData(mi->repl_transfer_s, mi);\n    if (connConnect(mi->repl_transfer_s, mi->masterhost, mi->masterport,\n                NET_FIRST_BIND_ADDR, syncWithMaster) == C_ERR) {\n        const char *err = \"Unknown Error\";\n        if (mi->repl_transfer_s->last_errno != 0)\n            err = connGetLastError(mi->repl_transfer_s);\n        int sev = g_pserver->enable_multimaster ? LL_NOTICE : LL_WARNING;   // with multimaster its not unheard of to intentionally have downed masters\n        serverLog(sev, \"Unable to connect to MASTER: %s\", err);\n        connClose(mi->repl_transfer_s);\n        mi->repl_transfer_s = NULL;\n        return C_ERR;\n    }\n\n\n    mi->repl_transfer_lastio = g_pserver->unixtime;\n    mi->repl_state = REPL_STATE_CONNECTING;\n    serverLog(LL_NOTICE,\"MASTER <-> REPLICA sync started\");\n    return C_OK;\n}\n\n/* This function can be called when a non blocking connection is currently\n * in progress to undo it.\n * Never call this function directly, use cancelReplicationHandshake() instead.\n */\nvoid undoConnectWithMaster(redisMaster *mi) {\n    serverAssert(GlobalLocksAcquired());\n    auto conn = mi->repl_transfer_s;\n    connSetPrivateData(conn, nullptr);\n    mi->repl_transfer_s = NULL;\n    int result = aePostFunction(g_pserver->rgthreadvar[mi->ielReplTransfer].el, [conn]{\n        connClose(conn);\n    }, false);\n    serverAssert(result == AE_OK);\n}\n\n/* Abort the async download of the bulk dataset while SYNC-ing with master.\n * Never call this function directly, use cancelReplicationHandshake() instead.\n */\nvoid replicationAbortSyncTransfer(redisMaster *mi) {\n    serverAssert(mi->repl_state == REPL_STATE_TRANSFER);\n    undoConnectWithMaster(mi);\n    if (mi->repl_transfer_fd!=-1) {\n        close(mi->repl_transfer_fd);\n        bg_unlink(mi->repl_transfer_tmpfile);\n        zfree(mi->repl_transfer_tmpfile);\n        mi->repl_transfer_tmpfile = NULL;\n        mi->repl_transfer_fd = -1;\n    }\n}\n\n/* This function aborts a non blocking replication attempt if there is one\n * in progress, by canceling the non-blocking connect attempt or\n * the initial bulk transfer.\n *\n * If there was a replication handshake in progress 1 is returned and\n * the replication state (g_pserver->repl_state) set to REPL_STATE_CONNECT.\n *\n * Otherwise zero is returned and no operation is performed at all. */\nint cancelReplicationHandshake(redisMaster *mi, int reconnect) {\n    if (mi->bulkreadBuffer != nullptr) {\n        sdsfree(mi->bulkreadBuffer);\n        mi->bulkreadBuffer = nullptr;\n    }\n    if (mi->parseState) {\n        delete mi->parseState;\n        mi->parseState = nullptr;\n    }\n    if (mi->bulkreadBuffer) {\n        sdsfree(mi->bulkreadBuffer);\n        mi->bulkreadBuffer = nullptr;\n    }\n\n    if (mi->repl_state == REPL_STATE_TRANSFER) {\n        replicationAbortSyncTransfer(mi);\n        mi->repl_state = REPL_STATE_CONNECT;\n    } else if (mi->repl_state == REPL_STATE_CONNECTING ||\n               slaveIsInHandshakeState(mi))\n    {\n        undoConnectWithMaster(mi);\n        mi->repl_state = REPL_STATE_CONNECT;\n    } else {\n        return 0;\n    }\n\n    if (!reconnect || g_pserver->fActiveReplica)\n        return 1;\n\n    /* try to re-connect without waiting for replicationCron, this is needed\n     * for the \"diskless loading short read\" test. */\n    serverLog(LL_NOTICE,\"Reconnecting to MASTER %s:%d after failure\",\n        mi->masterhost, mi->masterport);\n    connectWithMaster(mi);\n\n    return 1;\n}\n\nvoid disconnectMaster(redisMaster *mi)\n{\n    if (mi->master) {\n        if (FCorrectThread(mi->master)) {\n            // This will cache the master and do all that fancy stuff\n            if (!freeClient(mi->master) && mi->master)\n                replicationCreateCachedMasterClone(mi);\n        } else {\n            // We're not on the same thread so we can't use the freeClient method, instead we have to clone the master\n            //  and cache that clone\n            replicationCreateCachedMasterClone(mi);\n        }\n    }\n}\n\n/* Set replication to the specified master address and port. */\nstruct redisMaster *replicationAddMaster(char *ip, int port) {\n    // pre-reqs: We must not already have a replica in the list with the same tuple\n    listIter li;\n    listNode *ln;\n    listRewind(g_pserver->masters, &li);\n    while ((ln = listNext(&li)))\n    {\n        redisMaster *miCheck = (redisMaster*)listNodeValue(ln);\n        if (strcasecmp(miCheck->masterhost, ip)==0 && miCheck->masterport == port)\n            return nullptr;\n    }\n\n    // Pre-req satisfied, lets continue\n    int was_master = listLength(g_pserver->masters) == 0;\n    redisMaster *mi = nullptr;\n    if (!g_pserver->enable_multimaster && listLength(g_pserver->masters)) {\n        serverAssert(listLength(g_pserver->masters) == 1);\n        mi = (redisMaster*)listNodeValue(listFirst(g_pserver->masters));\n    }\n    else\n    {\n        mi = (redisMaster*)zcalloc(sizeof(redisMaster), MALLOC_LOCAL);\n        initMasterInfo(mi);\n        listAddNodeTail(g_pserver->masters, mi);\n    }\n\n    sdsfree(mi->masterhost);\n    mi->masterhost = nullptr;\n    disconnectMaster(mi);\n    serverAssert(mi->master == nullptr);\n    if (!g_pserver->fActiveReplica)\n        disconnectAllBlockedClients(); /* Clients blocked in master, now replica. */\n\n    /* Setting masterhost only after the call to freeClient since it calls\n     * replicationHandleMasterDisconnection which can trigger a re-connect\n     * directly from within that call. */\n    mi->masterhost = sdsnew(ip);\n    mi->masterport = port;\n\n    /* Update oom_score_adj */\n    setOOMScoreAdj(-1);\n\n    /* Force our slaves to resync with us as well. They may hopefully be able\n     * to partially resync with us, but we can notify the replid change. */\n    if (!g_pserver->fActiveReplica)\n        disconnectSlaves();\n    cancelReplicationHandshake(mi,false);\n    /* Before destroying our master state, create a cached master using\n     * our own parameters, to later PSYNC with the new master. */\n    if (was_master) {\n        replicationDiscardCachedMaster(mi);\n        replicationCacheMasterUsingMyself(mi);\n    }\n\n    /* Fire the role change modules event. */\n    moduleFireServerEvent(REDISMODULE_EVENT_REPLICATION_ROLE_CHANGED,\n                          REDISMODULE_EVENT_REPLROLECHANGED_NOW_REPLICA,\n                          NULL);\n\n    /* Fire the master link modules event. */\n    if (mi->repl_state == REPL_STATE_CONNECTED)\n        moduleFireServerEvent(REDISMODULE_EVENT_MASTER_LINK_CHANGE,\n                              REDISMODULE_SUBEVENT_MASTER_LINK_DOWN,\n                              NULL);\n\n    mi->repl_state = REPL_STATE_CONNECT;\n    if (!g_pserver->fActiveReplica && serverTL->el != nullptr) {    // note the event loop could be NULL if we're in boot loading a config\n        serverLog(LL_NOTICE,\"Connecting to MASTER %s:%d\",\n            mi->masterhost, mi->masterport);\n        connectWithMaster(mi);\n    }\n    saveMasterStatusToStorage(false);\n    return mi;\n}\n\nvoid freeMasterInfo(redisMaster *mi)\n{\n    sdsfree(mi->masterauth);\n    zfree(mi->masteruser);\n    if (g_pserver->rdb_filename != nullptr && g_pserver->rdb_filename == mi->repl_transfer_tmpfile) {\n        unlink(g_pserver->rdb_filename);\n        g_pserver->rdb_filename = nullptr;\n    }\n    if (mi->repl_transfer_tmpfile)\n        zfree(mi->repl_transfer_tmpfile);\n    delete mi->staleKeyMap;\n    if (mi->cached_master != nullptr)\n        freeClientAsync(mi->cached_master);\n    if (mi->master != nullptr)\n        freeClientAsync(mi->master);\n    zfree(mi);\n}\n\n\n/* Cancel replication, setting the instance as a master itself. */\nvoid replicationUnsetMaster(redisMaster *mi) {\n    if (mi->masterhost == NULL) return; /* Nothing to do. */\n\n    /* Fire the master link modules event. */\n    if (mi->repl_state == REPL_STATE_CONNECTED)\n        moduleFireServerEvent(REDISMODULE_EVENT_MASTER_LINK_CHANGE,\n                              REDISMODULE_SUBEVENT_MASTER_LINK_DOWN,\n                              NULL);\n\n    /* Clear masterhost first, since the freeClient calls\n     * replicationHandleMasterDisconnection which can attempt to re-connect. */\n    sdsfree(mi->masterhost);\n    mi->masterhost = NULL;\n    disconnectMaster(mi);\n    replicationDiscardCachedMaster(mi);\n    cancelReplicationHandshake(mi,false);\n    /* When a slave is turned into a master, the current replication ID\n     * (that was inherited from the master at synchronization time) is\n     * used as secondary ID up to the current offset, and a new replication\n     * ID is created to continue with a new replication history.\n     *\n     * NOTE: this function MUST be called after we call\n     * freeClient(g_pserver->master), since there we adjust the replication\n     * offset trimming the final PINGs. See Github issue #7320. */\n    shiftReplicationId();\n    /* Disconnecting all the slaves is required: we need to inform slaves\n     * of the replication ID change (see shiftReplicationId() call). However\n     * the slaves will be able to partially resync with us, so it will be\n     * a very fast reconnection. */\n    if (!g_pserver->fActiveReplica)\n        disconnectSlaves();\n    mi->repl_state = REPL_STATE_NONE;\n\n    /* We need to make sure the new master will start the replication stream\n     * with a SELECT statement. This is forced after a full resync, but\n     * with PSYNC version 2, there is no need for full resync after a\n     * master switch. */\n    g_pserver->replicaseldb = -1;\n\n    /* Once we turn from replica to master, we consider the starting time without\n     * slaves (that is used to count the replication backlog time to live) as\n     * starting from now. Otherwise the backlog will be freed after a\n     * failover if slaves do not connect immediately. */\n    g_pserver->repl_no_slaves_since = g_pserver->unixtime;\n\n    /* Reset down time so it'll be ready for when we turn into replica again. */\n    mi->repl_down_since = 0;\n\n    listNode *ln = listSearchKey(g_pserver->masters, mi);\n    serverAssert(ln != nullptr);\n    listDelNode(g_pserver->masters, ln);\n    freeMasterInfo(mi);\n\n    /* Update oom_score_adj */\n    setOOMScoreAdj(-1);\n\n    /* Fire the role change modules event. */\n    moduleFireServerEvent(REDISMODULE_EVENT_REPLICATION_ROLE_CHANGED,\n                          REDISMODULE_EVENT_REPLROLECHANGED_NOW_MASTER,\n                          NULL);\n\n    /* Restart the AOF subsystem in case we shut it down during a sync when\n     * we were still a slave. */\n    if (g_pserver->aof_enabled && g_pserver->aof_state == AOF_OFF) restartAOFAfterSYNC();\n\n    saveMasterStatusToStorage(false);\n}\n\n/* This function is called when the replica lose the connection with the\n * master into an unexpected way. */\nvoid replicationHandleMasterDisconnection(redisMaster *mi) {\n    if (mi != nullptr)\n    {\n        /* Fire the master link modules event. */\n        if (mi->repl_state == REPL_STATE_CONNECTED)\n            moduleFireServerEvent(REDISMODULE_EVENT_MASTER_LINK_CHANGE,\n                                REDISMODULE_SUBEVENT_MASTER_LINK_DOWN,\n                                NULL);\n        if (mi->master && mi->master->repl_down_since) {\n            mi->repl_down_since = mi->master->repl_down_since;\n        }\n        else {\n            mi->repl_down_since = g_pserver->unixtime;\n        }\n        mi->master = NULL;\n        mi->repl_state = REPL_STATE_CONNECT;\n        /* We lost connection with our master, don't disconnect slaves yet,\n        * maybe we'll be able to PSYNC with our master later. We'll disconnect\n        * the slaves only if we'll have to do a full resync with our master. */\n\n       /* Try to re-connect immediately rather than wait for replicationCron\n        * waiting 1 second may risk backlog being recycled. */\n        if (mi->masterhost && !g_pserver->fActiveReplica) {\n            serverLog(LL_NOTICE,\"Reconnecting to MASTER %s:%d\",\n                mi->masterhost, mi->masterport);\n            connectWithMaster(mi);\n        }\n\n        saveMasterStatusToStorage(false);\n    }\n}\n\nvoid replicaofCommand(client *c) {\n    /* SLAVEOF is not allowed in cluster mode as replication is automatically\n     * configured using the current address of the master node. */\n    if (g_pserver->cluster_enabled) {\n        addReplyError(c,\"REPLICAOF not allowed in cluster mode.\");\n        return;\n    }\n\n    if (g_pserver->failover_state != NO_FAILOVER) {\n        addReplyError(c,\"REPLICAOF not allowed while failing over.\");\n        return;\n    }\n    \n    if (c->argc > 3) {\n        if (c->argc != 4) {\n            addReplyError(c, \"Invalid arguments\");\n            return;\n        }\n        if (!strcasecmp((const char*)ptrFromObj(c->argv[1]),\"remove\")) {\n            listIter li;\n            listNode *ln;\n            bool fRemoved = false;\n            long port;\n            string2l(szFromObj(c->argv[3]), sdslen(szFromObj(c->argv[3])), &port);\n        LRestart:\n            listRewind(g_pserver->masters, &li);\n            while ((ln = listNext(&li))) {\n                redisMaster *mi = (redisMaster*)listNodeValue(ln);\n                if (mi->masterport != port)\n                    continue;\n                if (sdscmp(szFromObj(c->argv[2]), mi->masterhost) == 0) {\n                    replicationUnsetMaster(mi);\n                    fRemoved = true;\n                    goto LRestart;\n                }\n            }\n            if (!fRemoved) {\n                addReplyError(c, \"Master not found\");\n                return;\n            } else if (listLength(g_pserver->masters) == 0) {\n                goto LLogNoMaster;\n            }\n        }\n    } else if (!strcasecmp((const char*)ptrFromObj(c->argv[1]),\"no\") &&\n        !strcasecmp((const char*)ptrFromObj(c->argv[2]),\"one\")) {\n        /* The special host/port combination \"NO\" \"ONE\" turns the instance\n         * into a master. Otherwise the new master address is set. */\n        if (listLength(g_pserver->masters)) {\n            while (listLength(g_pserver->masters))\n            {\n                replicationUnsetMaster((redisMaster*)listNodeValue(listFirst(g_pserver->masters)));\n            }\n        LLogNoMaster:\n            sds client = catClientInfoString(sdsempty(),c);\n            serverLog(LL_NOTICE,\"MASTER MODE enabled (user request from '%s')\",\n                client);\n            sdsfree(client);\n        }\n    } else {\n        long port;\n\n        if (c->flags & CLIENT_SLAVE)\n        {\n            /* If a client is already a replica they cannot run this command,\n             * because it involves flushing all replicas (including this\n             * client) */\n            addReplyError(c, \"Command is not valid when client is a replica.\");\n            return;\n        }\n\n        if ((getLongFromObjectOrReply(c, c->argv[2], &port, NULL) != C_OK))\n            return;\n\n        redisMaster *miNew = replicationAddMaster((char*)ptrFromObj(c->argv[1]), port);\n        if (miNew == nullptr)\n        {\n            // We have a duplicate\n            serverLog(LL_NOTICE,\"REPLICAOF would result into synchronization \"\n                                \"with the master we are already connected \"\n                                \"with. No operation performed.\");\n            addReplySds(c,sdsnew(\"+OK Already connected to specified \"\n                                \"master\\r\\n\"));\n            return;\n        }\n\n        sds client = catClientInfoString(sdsempty(),c);\n        serverLog(LL_NOTICE,\"REPLICAOF %s:%d enabled (user request from '%s')\",\n            miNew->masterhost, miNew->masterport, client);\n        sdsfree(client);\n    }\n    addReply(c,shared.ok);\n}\n\n/* ROLE command: provide information about the role of the instance\n * (master or replica) and additional information related to replication\n * in an easy to process format. */\nvoid roleCommand(client *c) {\n    if (listLength(g_pserver->masters) == 0) {\n        listIter li;\n        listNode *ln;\n        void *mbcount;\n        int slaves = 0;\n\n        addReplyArrayLen(c,3);\n        addReplyBulkCBuffer(c,\"master\",6);\n        addReplyLongLong(c,g_pserver->master_repl_offset);\n        mbcount = addReplyDeferredLen(c);\n        listRewind(g_pserver->slaves,&li);\n        while((ln = listNext(&li))) {\n            client *replica = (client*)ln->value;\n            char ip[NET_IP_STR_LEN], *slaveaddr = replica->slave_addr;\n\n            if (!slaveaddr) {\n                if (connPeerToString(replica->conn,ip,sizeof(ip),NULL) == -1)\n                    continue;\n                slaveaddr = ip;\n            }\n            if (replica->replstate != SLAVE_STATE_ONLINE) continue;\n            addReplyArrayLen(c,3);\n            addReplyBulkCString(c,slaveaddr);\n            addReplyBulkLongLong(c,replica->slave_listening_port);\n            addReplyBulkLongLong(c,replica->repl_ack_off);\n            slaves++;\n        }\n        setDeferredArrayLen(c,mbcount,slaves);\n    } else {\n        listIter li;\n        listNode *ln;\n        listRewind(g_pserver->masters, &li);\n\n        if (listLength(g_pserver->masters) > 1)\n            addReplyArrayLen(c,listLength(g_pserver->masters));\n        while ((ln = listNext(&li)))\n        {\n            redisMaster *mi = (redisMaster*)listNodeValue(ln);\n            std::unique_lock<fastlock> lock;\n            if (mi->master != nullptr)\n                lock = std::unique_lock<fastlock>(mi->master->lock);\n\n            const char *slavestate = NULL;\n            addReplyArrayLen(c,5);\n            if (g_pserver->fActiveReplica)\n                addReplyBulkCBuffer(c,\"active-replica\",14);\n            else\n                addReplyBulkCBuffer(c,\"slave\",5);\n            addReplyBulkCString(c,mi->masterhost);\n            addReplyLongLong(c,mi->masterport);\n            if (slaveIsInHandshakeState(mi)) {\n                slavestate = \"handshake\";\n            } else {\n                switch(mi->repl_state) {\n                case REPL_STATE_NONE: slavestate = \"none\"; break;\n                case REPL_STATE_CONNECT: slavestate = \"connect\"; break;\n                case REPL_STATE_CONNECTING: slavestate = \"connecting\"; break;\n                case REPL_STATE_TRANSFER: slavestate = \"sync\"; break;\n                case REPL_STATE_CONNECTED: slavestate = \"connected\"; break;\n                default: slavestate = \"unknown\"; break;\n                }\n            }\n            addReplyBulkCString(c,slavestate);\n            addReplyLongLong(c,mi->master ? mi->master->reploff : -1);\n        }\n    }\n}\n\n/* Send a REPLCONF ACK command to the master to inform it about the current\n * processed offset. If we are not connected with a master, the command has\n * no effects. */\nvoid replicationSendAck(redisMaster *mi) \n{\n    client *c = mi->master;\n\n    if (c != NULL) {\n        std::unique_lock<fastlock> ul(c->lock);\n        c->flags |= CLIENT_MASTER_FORCE_REPLY;\n        addReplyArrayLen(c,3);\n        addReplyBulkCString(c,\"REPLCONF\");\n        addReplyBulkCString(c,\"ACK\");\n        addReplyBulkLongLong(c,c->reploff);\n        c->flags &= ~CLIENT_MASTER_FORCE_REPLY;\n    }\n}\n\n/* ---------------------- MASTER CACHING FOR PSYNC -------------------------- */\n\n/* In order to implement partial synchronization we need to be able to cache\n * our master's client structure after a transient disconnection.\n * It is cached into g_pserver->cached_master and flushed away using the following\n * functions. */\n\n/* This function is called by freeClient() in order to cache the master\n * client structure instead of destroying it. freeClient() will return\n * ASAP after this function returns, so every action needed to avoid problems\n * with a client that is really \"suspended\" has to be done by this function.\n *\n * The other functions that will deal with the cached master are:\n *\n * replicationDiscardCachedMaster() that will make sure to kill the client\n * as for some reason we don't want to use it in the future.\n *\n * replicationResurrectCachedMaster() that is used after a successful PSYNC\n * handshake in order to reactivate the cached master.\n */\nvoid replicationCacheMaster(redisMaster *mi, client *c) {\n    serverAssert(mi->master == c);\n    serverAssert(mi->master != NULL && mi->cached_master == NULL);\n    serverLog(LL_NOTICE,\"Caching the disconnected master state.\");\n    AssertCorrectThread(c);\n    std::lock_guard<decltype(c->lock)> clientlock(c->lock);\n\n    /* Unlink the client from the server structures. */\n    unlinkClient(c);\n\n    /* Reset the master client so that's ready to accept new commands:\n     * we want to discard te non processed query buffers and non processed\n     * offsets, including pending transactions, already populated arguments,\n     * pending outputs to the master. */\n    sdsclear(mi->master->querybuf);\n    if (!mi->master->vecqueuedcmd.empty()) {\n        mi->master->vecqueuedcmd.clear();\n    }\n    mi->master->multibulklen = 0;\n    sdsclear(mi->master->pending_querybuf);\n    mi->master->read_reploff = mi->master->reploff;\n    if (c->flags & CLIENT_MULTI) discardTransaction(c);\n    listEmpty(c->reply);\n    c->sentlen = 0;\n    c->reply_bytes = 0;\n    c->bufpos = 0;\n    resetClient(c);\n\n    /* Save the master. g_pserver->master will be set to null later by\n     * replicationHandleMasterDisconnection(). */\n    mi->cached_master = mi->master;\n\n    /* Invalidate the Peer ID cache. */\n    if (c->peerid) {\n        sdsfree(c->peerid);\n        c->peerid = NULL;\n    }\n    /* Invalidate the Sock Name cache. */\n    if (c->sockname) {\n        sdsfree(c->sockname);\n        c->sockname = NULL;\n    }\n\n    /* Caching the master happens instead of the actual freeClient() call,\n     * so make sure to adjust the replication state. This function will\n     * also set g_pserver->master to NULL. */\n    replicationHandleMasterDisconnection(mi);\n    serverAssert(mi->master == nullptr);\n}\n\n/* This function is called when a master is turend into a slave, in order to\n * create from scratch a cached master for the new client, that will allow\n * to PSYNC with the slave that was promoted as the new master after a\n * failover.\n *\n * Assuming this instance was previously the master instance of the new master,\n * the new master will accept its replication ID, and potentiall also the\n * current offset if no data was lost during the failover. So we use our\n * current replication ID and offset in order to synthesize a cached master. */\nvoid replicationCacheMasterUsingMyself(redisMaster *mi) {\n    serverLog(LL_NOTICE,\n        \"Before turning into a replica, using my own master parameters \"\n        \"to synthesize a cached master: I may be able to synchronize with \"\n        \"the new master with just a partial transfer.\");\n\n    if (mi->cached_master != nullptr)\n    {\n        // This can happen on first load of the RDB, the master we created in config load is stale\n        freeClient(mi->cached_master);\n    }\n\n    /* This will be used to populate the field g_pserver->master->reploff\n     * by replicationCreateMasterClient(). We'll later set the created\n     * master as g_pserver->cached_master, so the replica will use such\n     * offset for PSYNC. */\n    mi->master_initial_offset = g_pserver->master_repl_offset;\n\n    /* The master client we create can be set to any DBID, because\n     * the new master will start its replication stream with SELECT. */\n    replicationCreateMasterClient(mi,NULL,-1);\n    std::lock_guard<decltype(mi->master->lock)> lock(mi->master->lock);\n\n    /* Use our own ID / offset. */\n    memcpy(mi->master->replid, g_pserver->replid, sizeof(g_pserver->replid));\n\n    /* Set as cached master. */\n    unlinkClient(mi->master);\n    mi->cached_master = mi->master;\n    mi->master = NULL;\n}\n\n/* This function is called when reloading master info from an RDB in Active Replica mode.\n * It creates a cached master client using the info contained in the redisMaster struct.\n *\n * Assumes that the passed struct contains valid master info. */\nvoid replicationCacheMasterUsingMaster(redisMaster *mi) {\n    if (mi->cached_master) {\n        freeClient(mi->cached_master);\n    }\n\n    replicationCreateMasterClient(mi, NULL, -1);\n    std::lock_guard<decltype(mi->master->lock)> lock(mi->master->lock);\n\n    memcpy(mi->master->replid, mi->master_replid, sizeof(mi->master_replid));\n    mi->master->reploff = mi->master_initial_offset;\n\n    unlinkClient(mi->master);\n    mi->cached_master = mi->master;\n    mi->master = NULL;\n}\n\n/* Free a cached master, called when there are no longer the conditions for\n * a partial resync on reconnection. */\nvoid replicationDiscardCachedMaster(redisMaster *mi) {\n    if (mi->cached_master == NULL) return;\n\n    serverLog(LL_NOTICE,\"Discarding previously cached master state.\");\n    mi->cached_master->flags &= ~CLIENT_MASTER;\n    freeClientAsync(mi->cached_master);\n    mi->cached_master = NULL;\n}\n\n/* Turn the cached master into the current master, using the file descriptor\n * passed as argument as the socket for the new master.\n *\n * This function is called when successfully setup a partial resynchronization\n * so the stream of data that we'll receive will start from were this\n * master left. */\nvoid replicationResurrectCachedMaster(redisMaster *mi, connection *conn) {\n    mi->master = mi->cached_master;\n    mi->cached_master = NULL;\n    mi->master->conn = conn;\n    connSetPrivateData(mi->master->conn, mi->master);\n    mi->master->flags &= ~(CLIENT_CLOSE_AFTER_REPLY|CLIENT_CLOSE_ASAP);\n    mi->master->authenticated = 1;\n    mi->master->lastinteraction = g_pserver->unixtime;\n    mi->repl_state = REPL_STATE_CONNECTED;\n    mi->repl_down_since = 0;\n    mi->master->repl_down_since = 0;\n\n    /* Normally changing the thread of a client is a BIG NONO,\n        but this client was unlinked so its OK here */\n    mi->master->iel = serverTL - g_pserver->rgthreadvar; // martial to this thread\n\n    /* Fire the master link modules event. */\n    moduleFireServerEvent(REDISMODULE_EVENT_MASTER_LINK_CHANGE,\n                          REDISMODULE_SUBEVENT_MASTER_LINK_UP,\n                          NULL);\n\n    /* Re-add to the list of clients. */\n    linkClient(mi->master);\n    serverAssert(connGetPrivateData(mi->master->conn) == mi->master);\n    serverAssert(mi->master->conn == conn);\n    AssertCorrectThread(mi->master);\n    if (connSetReadHandler(mi->master->conn, readQueryFromClient, true)) {\n        serverLog(LL_WARNING,\"Error resurrecting the cached master, impossible to add the readable handler: %s\", strerror(errno));\n        freeClientAsync(mi->master); /* Close ASAP. */\n    }\n\n    /* We may also need to install the write handler as well if there is\n     * pending data in the write buffers. */\n    if (clientHasPendingReplies(mi->master)) {\n        if (connSetWriteHandler(mi->master->conn, sendReplyToClient, true)) {\n            serverLog(LL_WARNING,\"Error resurrecting the cached master, impossible to add the writable handler: %s\", strerror(errno));\n            freeClientAsync(mi->master); /* Close ASAP. */\n        }\n    }\n}\n\n/* ------------------------- MIN-SLAVES-TO-WRITE  --------------------------- */\n\n/* This function counts the number of slaves with lag <= min-slaves-max-lag.\n * If the option is active, the server will prevent writes if there are not\n * enough connected slaves with the specified lag (or less). */\nvoid refreshGoodSlavesCount(void) {\n    listIter li;\n    listNode *ln;\n    int good = 0;\n\n    if (!g_pserver->repl_min_slaves_to_write ||\n        !g_pserver->repl_min_slaves_max_lag) return;\n\n    listRewind(g_pserver->slaves,&li);\n    while((ln = listNext(&li))) {\n        client *replica = (client*)ln->value;\n        time_t lag = g_pserver->unixtime - replica->repl_ack_time;\n\n        if (replica->replstate == SLAVE_STATE_ONLINE &&\n            lag <= g_pserver->repl_min_slaves_max_lag) good++;\n    }\n    g_pserver->repl_good_slaves_count = good;\n}\n\n/* ----------------------- REPLICATION SCRIPT CACHE --------------------------\n * The goal of this code is to keep track of scripts already sent to every\n * connected replica, in order to be able to replicate EVALSHA as it is without\n * translating it to EVAL every time it is possible.\n *\n * We use a capped collection implemented by a hash table for fast lookup\n * of scripts we can send as EVALSHA, plus a linked list that is used for\n * eviction of the oldest entry when the max number of items is reached.\n *\n * We don't care about taking a different cache for every different replica\n * since to fill the cache again is not very costly, the goal of this code\n * is to avoid that the same big script is transmitted a big number of times\n * per second wasting bandwidth and processor speed, but it is not a problem\n * if we need to rebuild the cache from scratch from time to time, every used\n * script will need to be transmitted a single time to reappear in the cache.\n *\n * This is how the system works:\n *\n * 1) Every time a new replica connects, we flush the whole script cache.\n * 2) We only send as EVALSHA what was sent to the master as EVALSHA, without\n *    trying to convert EVAL into EVALSHA specifically for slaves.\n * 3) Every time we transmit a script as EVAL to the slaves, we also add the\n *    corresponding SHA1 of the script into the cache as we are sure every\n *    replica knows about the script starting from now.\n * 4) On SCRIPT FLUSH command, we replicate the command to all the slaves\n *    and at the same time flush the script cache.\n * 5) When the last replica disconnects, flush the cache.\n * 6) We handle SCRIPT LOAD as well since that's how scripts are loaded\n *    in the master sometimes.\n */\n\n/* Initialize the script cache, only called at startup. */\nvoid replicationScriptCacheInit(void) {\n    g_pserver->repl_scriptcache_size = 10000;\n    g_pserver->repl_scriptcache_dict = dictCreate(&replScriptCacheDictType,NULL);\n    g_pserver->repl_scriptcache_fifo = listCreate();\n}\n\n/* Empty the script cache. Should be called every time we are no longer sure\n * that every replica knows about all the scripts in our set, or when the\n * current AOF \"context\" is no longer aware of the script. In general we\n * should flush the cache:\n *\n * 1) Every time a new replica reconnects to this master and performs a\n *    full SYNC (PSYNC does not require flushing).\n * 2) Every time an AOF rewrite is performed.\n * 3) Every time we are left without slaves at all, and AOF is off, in order\n *    to reclaim otherwise unused memory.\n */\nvoid replicationScriptCacheFlush(void) {\n    dictEmpty(g_pserver->repl_scriptcache_dict,NULL);\n    listRelease(g_pserver->repl_scriptcache_fifo);\n    g_pserver->repl_scriptcache_fifo = listCreate();\n}\n\n/* Add an entry into the script cache, if we reach max number of entries the\n * oldest is removed from the list. */\nvoid replicationScriptCacheAdd(sds sha1) {\n    int retval;\n    sds key = sdsdup(sha1);\n\n    /* Evict oldest. */\n    if (listLength(g_pserver->repl_scriptcache_fifo) == g_pserver->repl_scriptcache_size)\n    {\n        listNode *ln = listLast(g_pserver->repl_scriptcache_fifo);\n        sds oldest = (sds)listNodeValue(ln);\n\n        retval = dictDelete(g_pserver->repl_scriptcache_dict,oldest);\n        serverAssert(retval == DICT_OK);\n        listDelNode(g_pserver->repl_scriptcache_fifo,ln);\n    }\n\n    /* Add current. */\n    retval = dictAdd(g_pserver->repl_scriptcache_dict,key,NULL);\n    listAddNodeHead(g_pserver->repl_scriptcache_fifo,key);\n    serverAssert(retval == DICT_OK);\n}\n\n/* Returns non-zero if the specified entry exists inside the cache, that is,\n * if all the slaves are aware of this script SHA1. */\nint replicationScriptCacheExists(sds sha1) {\n    return dictFind(g_pserver->repl_scriptcache_dict,sha1) != NULL;\n}\n\n/* ----------------------- SYNCHRONOUS REPLICATION --------------------------\n * Redis synchronous replication design can be summarized in points:\n *\n * - Redis masters have a global replication offset, used by PSYNC.\n * - Master increment the offset every time new commands are sent to slaves.\n * - Slaves ping back masters with the offset processed so far.\n *\n * So synchronous replication adds a new WAIT command in the form:\n *\n *   WAIT <num_replicas> <milliseconds_timeout>\n *\n * That returns the number of replicas that processed the query when\n * we finally have at least num_replicas, or when the timeout was\n * reached.\n *\n * The command is implemented in this way:\n *\n * - Every time a client processes a command, we remember the replication\n *   offset after sending that command to the slaves.\n * - When WAIT is called, we ask slaves to send an acknowledgement ASAP.\n *   The client is blocked at the same time (see blocked.c).\n * - Once we receive enough ACKs for a given offset or when the timeout\n *   is reached, the WAIT command is unblocked and the reply sent to the\n *   client.\n */\n\n/* This just set a flag so that we broadcast a REPLCONF GETACK command\n * to all the slaves in the beforeSleep() function. Note that this way\n * we \"group\" all the clients that want to wait for synchronous replication\n * in a given event loop iteration, and send a single GETACK for them all. */\nvoid replicationRequestAckFromSlaves(void) {\n    g_pserver->get_ack_from_slaves = 1;\n}\n\n/* Return the number of slaves that already acknowledged the specified\n * replication offset. */\nint replicationCountAcksByOffset(long long offset) {\n    listIter li;\n    listNode *ln;\n    int count = 0;\n\n    listRewind(g_pserver->slaves,&li);\n    while((ln = listNext(&li))) {\n        client *replica = (client*)ln->value;\n\n        if (replica->replstate != SLAVE_STATE_ONLINE) continue;\n        if ((replica->repl_ack_off) >= offset) count++;\n    }\n    return count;\n}\n\n/* WAIT for N replicas to acknowledge the processing of our latest\n * write command (and all the previous commands). */\nvoid waitCommand(client *c) {\n    mstime_t timeout;\n    long numreplicas, ackreplicas;\n    long long offset = c->woff;\n\n    if (listLength(g_pserver->masters) && !g_pserver->fActiveReplica) {\n        addReplyError(c,\"WAIT cannot be used with replica instances. Please also note that since Redis 4.0 if a replica is configured to be writable (which is not the default) writes to replicas are just local and are not propagated.\");\n        return;\n    }\n\n    /* Argument parsing. */\n    if (getLongFromObjectOrReply(c,c->argv[1],&numreplicas,NULL) != C_OK)\n        return;\n    if (getTimeoutFromObjectOrReply(c,c->argv[2],&timeout,UNIT_MILLISECONDS)\n        != C_OK) return;\n\n    /* First try without blocking at all. */\n    ackreplicas = replicationCountAcksByOffset(c->woff);\n    if (ackreplicas >= numreplicas || c->flags & CLIENT_MULTI) {\n        addReplyLongLong(c,ackreplicas);\n        return;\n    }\n\n    /* Otherwise block the client and put it into our list of clients\n     * waiting for ack from slaves. */\n    c->bpop.timeout = timeout;\n    c->bpop.reploffset = offset;\n    c->bpop.numreplicas = numreplicas;\n    listAddNodeHead(g_pserver->clients_waiting_acks,c);\n    blockClient(c,BLOCKED_WAIT);\n\n    /* Make sure that the server will send an ACK request to all the slaves\n     * before returning to the event loop. */\n    replicationRequestAckFromSlaves();\n}\n\n/* This is called by unblockClient() to perform the blocking op type\n * specific cleanup. We just remove the client from the list of clients\n * waiting for replica acks. Never call it directly, call unblockClient()\n * instead. */\nvoid unblockClientWaitingReplicas(client *c) {\n    listNode *ln = listSearchKey(g_pserver->clients_waiting_acks,c);\n    serverAssert(ln != NULL);\n    listDelNode(g_pserver->clients_waiting_acks,ln);\n}\n\n/* Check if there are clients blocked in WAIT that can be unblocked since\n * we received enough ACKs from slaves. */\nvoid processClientsWaitingReplicas(void) {\n    long long last_offset = 0;\n    int last_numreplicas = 0;\n\n    listIter li;\n    listNode *ln;\n\n    listRewind(g_pserver->clients_waiting_acks,&li);\n    while((ln = listNext(&li))) {\n        client *c = (client*)ln->value;\n        std::unique_lock<fastlock> ul(c->lock);\n\n        /* Every time we find a client that is satisfied for a given\n         * offset and number of replicas, we remember it so the next client\n         * may be unblocked without calling replicationCountAcksByOffset()\n         * if the requested offset / replicas were equal or less. */\n        if (last_offset && last_offset >= c->bpop.reploffset &&\n                           last_numreplicas >= c->bpop.numreplicas)\n        {\n            unblockClient(c);\n            addReplyLongLong(c,last_numreplicas);\n        } else {\n            int numreplicas = replicationCountAcksByOffset(c->bpop.reploffset);\n\n            if (numreplicas >= c->bpop.numreplicas) {\n                last_offset = c->bpop.reploffset;\n                last_numreplicas = numreplicas;\n                unblockClient(c);\n                addReplyLongLong(c,numreplicas);\n            }\n        }\n    }\n}\n\n/* Return the replica replication offset for this instance, that is\n * the offset for which we already processed the master replication stream. */\nlong long replicationGetSlaveOffset(redisMaster *mi) {\n    long long offset = 0;\n\n    if (mi != NULL && mi->masterhost != NULL) {\n        if (mi->master) {\n            offset = mi->master->reploff;\n        } else if (mi->cached_master) {\n            offset = mi->cached_master->reploff;\n        }\n    }\n    /* offset may be -1 when the master does not support it at all, however\n     * this function is designed to return an offset that can express the\n     * amount of data processed by the master, so we return a positive\n     * integer. */\n    if (offset < 0) offset = 0;\n    return offset;\n}\n\n/* --------------------------- REPLICATION CRON  ---------------------------- */\n\n/* Replication cron function, called 1 time per second. */\nvoid replicationCron(void) {\n    static long long replication_cron_loops = 0;\n    serverAssert(GlobalLocksAcquired());\n\n    /* Check failover status first, to see if we need to start\n     * handling the failover. */\n    updateFailoverStatus();\n\n    listIter liMaster;\n    listNode *lnMaster;\n    listRewind(g_pserver->masters, &liMaster);\n\n    bool fInMasterConnection = false;\n    while ((lnMaster = listNext(&liMaster)) && !fInMasterConnection)\n    {\n        redisMaster *mi = (redisMaster*)listNodeValue(lnMaster);\n        if (mi->repl_state != REPL_STATE_NONE && mi->repl_state != REPL_STATE_CONNECTED && mi->repl_state != REPL_STATE_CONNECT) {\n            fInMasterConnection = true;\n        }\n    }\n\n    bool fConnectionStarted = false;\n    listRewind(g_pserver->masters, &liMaster);\n    while ((lnMaster = listNext(&liMaster)))\n    {\n        redisMaster *mi = (redisMaster*)listNodeValue(lnMaster);\n\n        std::unique_lock<decltype(mi->master->lock)> ulock;\n        if (mi->master != nullptr)\n            ulock = decltype(ulock)(mi->master->lock);\n\n        /* Non blocking connection timeout? */\n        if (mi->masterhost &&\n            (mi->repl_state == REPL_STATE_CONNECTING ||\n            slaveIsInHandshakeState(mi)) &&\n            (time(NULL)-mi->repl_transfer_lastio) > g_pserver->repl_timeout)\n        {\n            serverLog(LL_WARNING,\"Timeout connecting to the MASTER...\");\n            cancelReplicationHandshake(mi,true);\n        }\n\n        /* Bulk transfer I/O timeout? */\n        if (mi->masterhost && mi->repl_state == REPL_STATE_TRANSFER &&\n            (time(NULL)-mi->repl_transfer_lastio) > g_pserver->repl_timeout)\n        {\n            serverLog(LL_WARNING,\"Timeout receiving bulk data from MASTER... If the problem persists try to set the 'repl-timeout' parameter in keydb.conf to a larger value.\");\n            cancelReplicationHandshake(mi,true);\n        }\n\n        /* Timed out master when we are an already connected replica? */\n        if (mi->masterhost && mi->master && mi->repl_state == REPL_STATE_CONNECTED &&\n            (time(NULL)-mi->master->lastinteraction) > g_pserver->repl_timeout)\n        {\n            serverLog(LL_WARNING,\"MASTER timeout: no data nor PING received...\");\n            disconnectMaster(mi);\n        }\n\n        /* Check if we should connect to a MASTER */\n        if (mi->repl_state == REPL_STATE_CONNECT && !fInMasterConnection && !g_pserver->loading && !g_pserver->FRdbSaveInProgress()) {\n            serverLog(LL_NOTICE,\"Connecting to MASTER %s:%d\",\n                mi->masterhost, mi->masterport);\n            connectWithMaster(mi);\n            fInMasterConnection = true;\n            fConnectionStarted = true;\n        }\n\n        /* Send ACK to master from time to time.\n        * Note that we do not send periodic acks to masters that don't\n        * support PSYNC and replication offsets. */\n        if (mi->masterhost && mi->master &&\n            !(mi->master->flags & CLIENT_PRE_PSYNC))\n            replicationSendAck(mi);\n    }\n\n    if (fConnectionStarted) {\n        // If we cancel this handshake we want the next attempt to be a different master\n        listRotateHeadToTail(g_pserver->masters);\n    }\n\n    /* If we have attached slaves, PING them from time to time.\n    * So slaves can implement an explicit timeout to masters, and will\n    * be able to detect a link disconnection even if the TCP connection\n    * will not actually go down. */\n    listIter li;\n    listNode *ln;\n    robj *ping_argv[1];\n\n    /* First, send PING according to ping_slave_period. */\n    if ((replication_cron_loops % g_pserver->repl_ping_slave_period) == 0 &&\n        listLength(g_pserver->slaves))\n    {\n        /* Note that we don't send the PING if the clients are paused during\n         * a Redis Cluster manual failover: the PING we send will otherwise\n         * alter the replication offsets of master and replica, and will no longer\n         * match the one stored into 'mf_master_offset' state. */\n        int manual_failover_in_progress =\n            ((g_pserver->cluster_enabled &&\n              g_pserver->cluster->mf_end) ||\n            g_pserver->failover_end_time) &&\n            checkClientPauseTimeoutAndReturnIfPaused();\n\n        if (!manual_failover_in_progress) {\n            ping_argv[0] = shared.ping;\n            replicationFeedSlaves(g_pserver->slaves, g_pserver->replicaseldb,\n                ping_argv, 1);\n        }\n    }\n\n    /* Second, send a newline to all the slaves in pre-synchronization\n    * stage, that is, slaves waiting for the master to create the RDB file.\n    *\n    * Also send the a newline to all the chained slaves we have, if we lost\n    * connection from our master, to keep the slaves aware that their\n    * master is online. This is needed since sub-slaves only receive proxied\n    * data from top-level masters, so there is no explicit pinging in order\n    * to avoid altering the replication offsets. This special out of band\n    * pings (newlines) can be sent, they will have no effect in the offset.\n    *\n    * The newline will be ignored by the replica but will refresh the\n    * last interaction timer preventing a timeout. In this case we ignore the\n    * ping period and refresh the connection once per second since certain\n    * timeouts are set at a few seconds (example: PSYNC response). */\n    listRewind(g_pserver->slaves,&li);\n    while((ln = listNext(&li))) {\n        client *replica = (client*)ln->value;\n\n        int is_presync =\n            (replica->replstate == SLAVE_STATE_WAIT_BGSAVE_START ||\n            (replica->replstate == SLAVE_STATE_WAIT_BGSAVE_END &&\n            g_pserver->rdb_child_type != RDB_CHILD_TYPE_SOCKET));\n\n        if (is_presync) {\n            connWrite(replica->conn, \"\\n\", 1);\n        }\n    }\n\n    /* Disconnect timedout slaves. */\n    if (listLength(g_pserver->slaves)) {\n        listIter li;\n        listNode *ln;\n\n        listRewind(g_pserver->slaves,&li);\n        while((ln = listNext(&li))) {\n            client *replica = (client*)ln->value;\n            std::unique_lock<fastlock> ul(replica->lock);\n\n            if (replica->replstate == SLAVE_STATE_FASTSYNC_DONE && !clientHasPendingReplies(replica)) {\n                serverLog(LL_WARNING, \"Putting replica online\");\n                replica->postFunction([](client *c){\n                    putSlaveOnline(c);\n                });\n            }\n\n            if (replica->replstate == SLAVE_STATE_ONLINE) {\n                if (replica->flags & CLIENT_PRE_PSYNC)\n                    continue;\n                if ((g_pserver->unixtime - replica->repl_ack_time) > g_pserver->repl_timeout) {\n                    serverLog(LL_WARNING, \"Disconnecting timedout replica (streaming sync): %s\",\n                          replicationGetSlaveName(replica));\n                    freeClientAsync(replica);\n                    continue;\n                }\n            }\n            /* We consider disconnecting only diskless replicas because disk-based replicas aren't fed\n             * by the fork child so if a disk-based replica is stuck it doesn't prevent the fork child\n             * from terminating. */\n            if (replica->replstate == SLAVE_STATE_WAIT_BGSAVE_END && g_pserver->rdb_child_type == RDB_CHILD_TYPE_SOCKET) {\n                if (replica->repl_last_partial_write != 0 &&\n                    (g_pserver->unixtime - replica->repl_last_partial_write) > g_pserver->repl_timeout)\n                {\n                    serverLog(LL_WARNING, \"Disconnecting timedout replica (full sync): %s\",\n                          replicationGetSlaveName(replica));\n                    freeClientAsync(replica);\n                    continue;\n                }\n            }\n        }\n    }\n\n    /* If this is a master without attached slaves and there is a replication\n    * backlog active, in order to reclaim memory we can free it after some\n    * (configured) time. Note that this cannot be done for slaves: slaves\n    * without sub-slaves attached should still accumulate data into the\n    * backlog, in order to reply to PSYNC queries if they are turned into\n    * masters after a failover. */\n    if (listLength(g_pserver->slaves) == 0 && g_pserver->repl_backlog_time_limit &&\n        g_pserver->repl_backlog && listLength(g_pserver->masters) == 0)\n    {\n        time_t idle = g_pserver->unixtime - g_pserver->repl_no_slaves_since;\n\n        if (idle > g_pserver->repl_backlog_time_limit) {\n            /* When we free the backlog, we always use a new\n            * replication ID and clear the ID2. This is needed\n            * because when there is no backlog, the master_repl_offset\n            * is not updated, but we would still retain our replication\n            * ID, leading to the following problem:\n            *\n            * 1. We are a master instance.\n            * 2. Our replica is promoted to master. It's repl-id-2 will\n            *    be the same as our repl-id.\n            * 3. We, yet as master, receive some updates, that will not\n            *    increment the master_repl_offset.\n            * 4. Later we are turned into a replica, connect to the new\n            *    master that will accept our PSYNC request by second\n            *    replication ID, but there will be data inconsistency\n            *    because we received writes. */\n            changeReplicationId();\n            clearReplicationId2();\n            freeReplicationBacklog();\n            serverLog(LL_NOTICE,\n                \"Replication backlog freed after %d seconds \"\n                \"without connected replicas.\",\n                (int) g_pserver->repl_backlog_time_limit);\n        }\n    }\n\n    /* If AOF is disabled and we no longer have attached slaves, we can\n    * free our Replication Script Cache as there is no need to propagate\n    * EVALSHA at all. */\n    if (listLength(g_pserver->slaves) == 0 &&\n        g_pserver->aof_state == AOF_OFF &&\n        listLength(g_pserver->repl_scriptcache_fifo) != 0)\n    {\n        replicationScriptCacheFlush();\n    }\n\n    propagateMasterStaleKeys();\n    \n    replicationStartPendingFork();\n\n    trimReplicationBacklog();\n\n    /* Remove the RDB file used for replication if Redis is not running\n     * with any persistence. */\n    removeRDBUsedToSyncReplicas();\n\n    /* Refresh the number of slaves with lag <= min-slaves-max-lag. */\n    refreshGoodSlavesCount();\n    replication_cron_loops++; /* Incremented with frequency 1 HZ. */\n}\n\nvoid replicationStartPendingFork(void) {\n    /* Start a BGSAVE good for replication if we have slaves in\n     * WAIT_BGSAVE_START state.\n     *\n     * In case of diskless replication, we make sure to wait the specified\n     * number of seconds (according to configuration) so that other slaves\n     * have the time to arrive before we start streaming. */\n    if (!hasActiveChildProcessOrBGSave()) {\n        time_t idle, max_idle = 0;\n        int slaves_waiting = 0;\n        int mincapa = -1;\n        listNode *ln;\n        listIter li;\n\n        listRewind(g_pserver->slaves,&li);\n        while((ln = listNext(&li))) {\n            client *replica = (client*)ln->value;\n            if (replica->replstate == SLAVE_STATE_WAIT_BGSAVE_START) {\n                idle = g_pserver->unixtime - replica->lastinteraction;\n                if (idle > max_idle) max_idle = idle;\n                slaves_waiting++;\n                mincapa = (mincapa == -1) ? replica->slave_capa :\n                                            (mincapa & replica->slave_capa);\n            }\n        }\n\n        if (slaves_waiting &&\n            (!g_pserver->repl_diskless_sync ||\n            max_idle >= g_pserver->repl_diskless_sync_delay))\n        {\n            /* Start the BGSAVE. The called function may start a\n            * BGSAVE with socket target or disk target depending on the\n            * configuration and slaves capabilities. */\n            startBgsaveForReplication(mincapa);\n        }\n    }\n}\n\n/* Find replica at IP:PORT from replica list */\nstatic client *findReplica(char *host, int port) {\n    listIter li;\n    listNode *ln;\n    client *replica;\n\n    listRewind(g_pserver->slaves,&li);\n    while((ln = listNext(&li))) {\n        replica = (client*)listNodeValue(ln);\n        char ip[NET_IP_STR_LEN], *replicaip = replica->slave_addr;\n\n        if (!replicaip) {\n            if (connPeerToString(replica->conn, ip, sizeof(ip), NULL) == -1)\n                continue;\n            replicaip = ip;\n        }\n\n        if (!strcasecmp(host, replicaip) &&\n                (port == replica->slave_listening_port))\n            return replica;\n    }\n\n    return NULL;\n}\n\nconst char *getFailoverStateString() {\n    switch(g_pserver->failover_state) {\n        case NO_FAILOVER: return \"no-failover\";\n        case FAILOVER_IN_PROGRESS: return \"failover-in-progress\";\n        case FAILOVER_WAIT_FOR_SYNC: return \"waiting-for-sync\";\n    }\n    return \"unknown\";\n}\n\n/* Resets the internal failover configuration, this needs\n * to be called after a failover either succeeds or fails\n * as it includes the client unpause. */\nvoid clearFailoverState() {\n    g_pserver->failover_end_time = 0;\n    g_pserver->force_failover = 0;\n    zfree(g_pserver->target_replica_host);\n    g_pserver->target_replica_host = NULL;\n    g_pserver->target_replica_port = 0;\n    g_pserver->failover_state = NO_FAILOVER;\n    unpauseClients();\n}\n\n/* Abort an ongoing failover if one is going on. */\nvoid abortFailover(redisMaster *mi, const char *err) {\n    if (g_pserver->failover_state == NO_FAILOVER) return;\n\n    if (g_pserver->target_replica_host) {\n        serverLog(LL_NOTICE,\"FAILOVER to %s:%d aborted: %s\",\n            g_pserver->target_replica_host,g_pserver->target_replica_port,err);  \n    } else {\n        serverLog(LL_NOTICE,\"FAILOVER to any replica aborted: %s\",err);  \n    }\n    if (g_pserver->failover_state == FAILOVER_IN_PROGRESS) {\n        replicationUnsetMaster(mi);\n    }\n    clearFailoverState();\n}\n\n/* \n * FAILOVER [TO <HOST> <PORT> [FORCE]] [ABORT] [TIMEOUT <timeout>]\n * \n * This command will coordinate a failover between the master and one\n * of its replicas. The happy path contains the following steps:\n * 1) The master will initiate a client pause write, to stop replication\n * traffic.\n * 2) The master will periodically check if any of its replicas has\n * consumed the entire replication stream through acks. \n * 3) Once any replica has caught up, the master will itself become a replica.\n * 4) The master will send a PSYNC FAILOVER request to the target replica, which\n * if accepted will cause the replica to become the new master and start a sync.\n * \n * FAILOVER ABORT is the only way to abort a failover command, as replicaof\n * will be disabled. This may be needed if the failover is unable to progress. \n * \n * The optional arguments [TO <HOST> <IP>] allows designating a specific replica\n * to be failed over to.\n * \n * FORCE flag indicates that even if the target replica is not caught up,\n * failover to it anyway. This must be specified with a timeout and a target\n * HOST and IP.\n * \n * TIMEOUT <timeout> indicates how long should the primary wait for \n * a replica to sync up before aborting. If not specified, the failover\n * will attempt forever and must be manually aborted.\n */\nvoid failoverCommand(client *c) {\n    if (g_pserver->cluster_enabled) {\n        addReplyError(c,\"FAILOVER not allowed in cluster mode. \"\n                        \"Use CLUSTER FAILOVER command instead.\");\n        return;\n    }\n    \n    if (g_pserver->fActiveReplica) {\n        addReplyError(c,\"FAILOVER not allowed in active replication mode\");\n        return;\n    }\n\n    /* Handle special case for abort */\n    if ((c->argc == 2) && !strcasecmp(szFromObj(c->argv[1]),\"abort\")) {\n        if (g_pserver->failover_state == NO_FAILOVER) {\n            addReplyError(c, \"No failover in progress.\");\n            return;\n        }\n\n        redisMaster *mi = listLength(g_pserver->masters) ? (redisMaster*)listNodeValue(listFirst(g_pserver->masters)) : nullptr;\n        abortFailover(mi, \"Failover manually aborted\");\n        addReply(c,shared.ok);\n        return;\n    }\n\n    long timeout_in_ms = 0;\n    int force_flag = 0;\n    long port = 0;\n    char *host = NULL;\n\n    /* Parse the command for syntax and arguments. */\n    for (int j = 1; j < c->argc; j++) {\n        if (!strcasecmp(szFromObj(c->argv[j]),\"timeout\") && (j + 1 < c->argc) &&\n            timeout_in_ms == 0)\n        {\n            if (getLongFromObjectOrReply(c,c->argv[j + 1],\n                        &timeout_in_ms,NULL) != C_OK) return;\n            if (timeout_in_ms <= 0) {\n                addReplyError(c,\"FAILOVER timeout must be greater than 0\");\n                return;\n            }\n            j++;\n        } else if (!strcasecmp(szFromObj(c->argv[j]),\"to\") && (j + 2 < c->argc) &&\n            !host) \n        {\n            if (getLongFromObjectOrReply(c,c->argv[j + 2],&port,NULL) != C_OK)\n                return;\n            host = szFromObj(c->argv[j + 1]);\n            j += 2;\n        } else if (!strcasecmp(szFromObj(c->argv[j]),\"force\") && !force_flag) {\n            force_flag = 1;\n        } else {\n            addReplyErrorObject(c,shared.syntaxerr);\n            return;\n        }\n    }\n\n    if (g_pserver->failover_state != NO_FAILOVER) {\n        addReplyError(c,\"FAILOVER already in progress.\");\n        return;\n    }\n\n    if (listLength(g_pserver->masters)) {\n        addReplyError(c,\"FAILOVER is not valid when server is a replica.\");\n        return;\n    }\n\n    if (listLength(g_pserver->slaves) == 0) {\n        addReplyError(c,\"FAILOVER requires connected replicas.\");\n        return; \n    }\n\n    if (force_flag && (!timeout_in_ms || !host)) {\n        addReplyError(c,\"FAILOVER with force option requires both a timeout \"\n            \"and target HOST and IP.\");\n        return;     \n    }\n\n    /* If a replica address was provided, validate that it is connected. */\n    if (host) {\n        client *replica = findReplica(host, port);\n\n        if (replica == NULL) {\n            addReplyError(c,\"FAILOVER target HOST and PORT is not \"\n                            \"a replica.\");\n            return;\n        }\n\n        /* Check if requested replica is online */\n        if (replica->replstate != SLAVE_STATE_ONLINE) {\n            addReplyError(c,\"FAILOVER target replica is not online.\");\n            return;\n        }\n\n        g_pserver->target_replica_host = zstrdup(host);\n        g_pserver->target_replica_port = port;\n        serverLog(LL_NOTICE,\"FAILOVER requested to %s:%ld.\",host,port);\n    } else {\n        serverLog(LL_NOTICE,\"FAILOVER requested to any replica.\");\n    }\n\n    mstime_t now = mstime();\n    if (timeout_in_ms) {\n        g_pserver->failover_end_time = now + timeout_in_ms;\n    }\n    \n    g_pserver->force_failover = force_flag;\n    g_pserver->failover_state = FAILOVER_WAIT_FOR_SYNC;\n    /* Cluster failover will unpause eventually */\n    pauseClients(LLONG_MAX,CLIENT_PAUSE_WRITE);\n    addReply(c,shared.ok);\n}\n\nint FBrokenLinkToMaster(int *pmastersOnline)\n{\n    listIter li;\n    listNode *ln;\n    listRewind(g_pserver->masters, &li);\n\n    int connected = 0;\n    while ((ln = listNext(&li)))\n    {\n        redisMaster *mi = (redisMaster*)listNodeValue(ln);\n        if (mi->repl_state == REPL_STATE_CONNECTED)\n            ++connected;\n    }\n\n\n    if (pmastersOnline != nullptr)\n        *pmastersOnline = connected;\n\n    if (g_pserver->repl_quorum < 0) {\n        return connected < (int)listLength(g_pserver->masters);\n    } else {\n        return connected < g_pserver->repl_quorum;\n    }\n\n    return false;\n}\n\nint FActiveMaster(client *c)\n{\n    if (!(c->flags & CLIENT_MASTER))\n        return false;\n\n    listIter li;\n    listNode *ln;\n    listRewind(g_pserver->masters, &li);\n    while ((ln = listNext(&li)))\n    {\n        redisMaster *mi = (redisMaster*)listNodeValue(ln);\n        if (mi->master == c)\n            return true;\n    }\n    return false;\n}\n\nredisMaster *MasterInfoFromClient(client *c)\n{\n    listIter li;\n    listNode *ln;\n    listRewind(g_pserver->masters, &li);\n    while ((ln = listNext(&li)))\n    {\n        redisMaster *mi = (redisMaster*)listNodeValue(ln);\n        if (mi->master == c || mi->cached_master == c)\n            return mi;\n    }\n    return nullptr;\n}\n\n#define REPLAY_MAX_NESTING 64\nclass ReplicaNestState\n{\npublic:\n    bool FPush()\n    {\n        if (m_cnesting == REPLAY_MAX_NESTING) {\n            m_fCancelled = true;\n            return false;   // overflow\n        }\n        \n        if (m_cnesting == 0)\n            m_fCancelled = false;\n        ++m_cnesting;\n        return true;\n    }\n\n    void Pop()\n    {\n        --m_cnesting;\n    }\n\n    void Cancel()\n    {\n        m_fCancelled = true;\n    }\n\n    bool FCancelled() const\n    {\n        return m_fCancelled;\n    }\n\n    bool FFirst() const\n    {\n        return m_cnesting == 1;\n    }\n\n    redisMaster *getMi(client *c)\n    {\n        if (m_mi == nullptr)\n            m_mi = MasterInfoFromClient(c);\n        return m_mi;\n    }\n\n    int nesting() const { return m_cnesting; }\n\nprivate:\n    int m_cnesting = 0;\n    bool m_fCancelled = false;\n    redisMaster *m_mi = nullptr;\n};\n\nstatic thread_local std::unique_ptr<ReplicaNestState> s_pstate;\n\nbool FInReplicaReplay()\n{\n    return s_pstate != nullptr && s_pstate->nesting() > 0;\n}\n\nstruct RemoteMasterState\n{\n    uint64_t mvcc = 0;\n    client *cFake = nullptr;\n\n    ~RemoteMasterState()\n    {\n        aeAcquireLock();\n        freeClient(cFake);\n        aeReleaseLock();\n    }\n};\n\nstatic std::unordered_map<std::string, RemoteMasterState> g_mapremote;\n\nvoid replicaReplayCommand(client *c)\n{\n    if (s_pstate == nullptr)\n        s_pstate = std::make_unique<ReplicaNestState>();\n\n    // the replay command contains two arguments: \n    //  1: The UUID of the source\n    //  2: The raw command buffer to be replayed\n    //  3: (OPTIONAL) the database ID the command should apply to\n    \n    if (!(c->flags & CLIENT_MASTER))\n    {\n        addReplyError(c, \"Command must be sent from a master\");\n        s_pstate->Cancel();\n        return;\n    }\n\n    /* First Validate Arguments */\n    if (c->argc < 3)\n    {\n        addReplyError(c, \"Invalid number of arguments\");\n        s_pstate->Cancel();\n        return;\n    }\n\n    std::string uuid;\n    uuid.resize(UUID_BINARY_LEN);\n    if (c->argv[1]->type != OBJ_STRING || sdslen((sds)ptrFromObj(c->argv[1])) != 36 \n        || uuid_parse((sds)ptrFromObj(c->argv[1]), (unsigned char*)uuid.data()) != 0)\n    {\n        addReplyError(c, \"Expected UUID arg1\");\n        s_pstate->Cancel();\n        return;\n    }\n\n    if (c->argv[2]->type != OBJ_STRING)\n    {\n        addReplyError(c, \"Expected command buffer arg2\");\n        s_pstate->Cancel();\n        return;\n    }\n\n    if (c->argc >= 4)\n    {\n        long long db;\n        if (getLongLongFromObject(c->argv[3], &db) != C_OK || db >= cserver.dbnum || selectDb(c, (int)db) != C_OK)\n        {\n            addReplyError(c, \"Invalid database ID\");\n            s_pstate->Cancel();\n            return;\n        }\n    }\n\n    uint64_t mvcc = 0;\n    if (c->argc >= 5)\n    {\n        if (getUnsignedLongLongFromObject(c->argv[4], &mvcc) != C_OK)\n        {\n            addReplyError(c, \"Invalid MVCC Timestamp\");\n            s_pstate->Cancel();\n            return;\n        }\n    }\n\n    if (FSameUuidNoNil((unsigned char*)uuid.data(), cserver.uuid))\n    {\n        addReply(c, shared.ok);\n        s_pstate->Cancel();\n        return; // Our own commands have come back to us.  Ignore them.\n    }\n\n    if (!s_pstate->FPush())\n        return;\n\n    RemoteMasterState &remoteState = g_mapremote[uuid];\n    if (remoteState.cFake == nullptr)\n        remoteState.cFake = createClient(nullptr, c->iel);\n    else\n        remoteState.cFake->iel = c->iel;\n\n    client *cFake = remoteState.cFake;\n\n    if (mvcc != 0 && remoteState.mvcc >= mvcc)\n    {\n        s_pstate->Cancel();\n        s_pstate->Pop();\n        return;\n    }\n\n    // OK We've recieved a command lets execute\n    client *current_clientSave = serverTL->current_client;\n    cFake->lock.lock();\n    cFake->authenticated = c->authenticated;\n    cFake->user = c->user;\n    cFake->querybuf = sdscatsds(cFake->querybuf,(sds)ptrFromObj(c->argv[2]));\n    cFake->read_reploff = sdslen(cFake->querybuf);\n    cFake->reploff = 0;\n    selectDb(cFake, c->db->id);\n    auto ccmdPrev = serverTL->commandsExecuted;\n    cFake->flags |= CLIENT_MASTER | CLIENT_PREVENT_REPL_PROP;\n    processInputBuffer(cFake, true /*fParse*/, (CMD_CALL_FULL & (~CMD_CALL_PROPAGATE)));\n    cFake->flags &= ~(CLIENT_MASTER | CLIENT_PREVENT_REPL_PROP);\n    bool fExec = ccmdPrev != serverTL->commandsExecuted;\n    bool fNoPropogate = false;\n    cFake->lock.unlock();\n    if (cFake->master_error)\n    {\n        selectDb(c, cFake->db->id);\n        freeClient(cFake);\n        remoteState.cFake = cFake = nullptr;\n        addReplyError(c, \"Error in rreplay command, please check logs.\");\n    }\n    if (cFake != nullptr)\n    {\n        if (fExec || cFake->flags & CLIENT_MULTI)\n        {\n            addReply(c, shared.ok);\n            selectDb(c, cFake->db->id);\n            if (mvcc > remoteState.mvcc)\n                remoteState.mvcc = mvcc;\n            serverAssert(sdslen(cFake->querybuf) == 0);\n        }\n        else\n        {\n            serverLog(LL_WARNING, \"Command didn't execute: %s\", cFake->buf);\n            addReplyError(c, \"command did not execute\");\n            if (sdslen(cFake->querybuf)) {\n                serverLog(LL_WARNING, \"Closing connection to MASTER because of an unrecoverable protocol error\");\n                freeClientAsync(c);\n                fNoPropogate = true;    // don't keep transmitting corrupt data\n            }\n        }\n    }\n    serverTL->current_client = current_clientSave;\n\n    // call() will not propogate this for us, so we do so here\n    if (!s_pstate->FCancelled() && s_pstate->FFirst() && !cserver.multimaster_no_forward && !fNoPropogate)\n        alsoPropagate(cserver.rreplayCommand,c->db->id,c->argv,c->argc,PROPAGATE_AOF|PROPAGATE_REPL);\n    \n    s_pstate->Pop();\n    return;\n}\n\nvoid updateMasterAuth()\n{\n    listIter li;\n    listNode *ln;\n\n    listRewind(g_pserver->masters, &li);\n    while ((ln = listNext(&li)))\n    {\n        redisMaster *mi = (redisMaster*)listNodeValue(ln);\n        sdsfree(mi->masterauth); mi->masterauth = nullptr;\n        zfree(mi->masteruser); mi->masteruser = nullptr;\n\n        if (cserver.default_masterauth)\n            mi->masterauth = sdsdup(cserver.default_masterauth);\n        if (cserver.default_masteruser)\n            mi->masteruser = zstrdup(cserver.default_masteruser);\n    }\n}\n\nstatic void propagateMasterStaleKeys()\n{\n    listIter li;\n    listNode *ln;\n    listRewind(g_pserver->masters, &li);\n    robj *rgobj[2];\n\n    rgobj[0] = createEmbeddedStringObject(\"DEL\", 3);\n\n    while ((ln = listNext(&li)) != nullptr)\n    {\n        redisMaster *mi = (redisMaster*)listNodeValue(ln);\n        if (mi->staleKeyMap != nullptr)\n        {\n            if (mi->master != nullptr)\n            {\n                for (auto &pair : *mi->staleKeyMap)\n                {\n                    if (pair.second.empty())\n                        continue;\n                    \n                    client *replica = replicaFromMaster(mi);\n                    if (replica == nullptr)\n                        continue;\n\n                    for (auto &spkey : pair.second)\n                    {\n                        rgobj[1] = spkey.get();\n                        replicationFeedSlave(replica, pair.first, rgobj, 2, false);\n                    }\n                }\n                delete mi->staleKeyMap;\n                mi->staleKeyMap = nullptr;\n            }\n        }\n    }\n\n    decrRefCount(rgobj[0]);\n}\n\nvoid replicationNotifyLoadedKey(redisDb *db, robj_roptr key, robj_roptr val, long long expire) {\n    if (!g_pserver->fActiveReplica || listLength(g_pserver->slaves) == 0)\n        return;\n\n    // Send a digest over to the replicas\n    rio r;\n\n    createDumpPayload(&r, val, key.unsafe_robjcast());\n\n    redisObjectStack objPayload;\n    initStaticStringObject(objPayload, r.io.buffer.ptr);\n    redisObjectStack objTtl;\n    initStaticStringObject(objTtl, sdscatprintf(sdsempty(), \"%lld\", expire));\n    redisObjectStack objMvcc;\n    initStaticStringObject(objMvcc, sdscatprintf(sdsempty(), \"%\" PRIu64, mvccFromObj(val)));\n    redisObject *argv[5] = {shared.mvccrestore, key.unsafe_robjcast(), &objMvcc, &objTtl, &objPayload};\n\n    replicationFeedSlaves(g_pserver->slaves, db->id, argv, 5);\n\n    sdsfree(szFromObj(&objTtl));\n    sdsfree(szFromObj(&objMvcc));\n    sdsfree(r.io.buffer.ptr);\n}\n\nvoid replicateSubkeyExpire(redisDb *db, robj_roptr key, robj_roptr subkey, long long expire) {\n    if (!g_pserver->fActiveReplica || listLength(g_pserver->slaves) == 0)\n        return;\n\n    redisObjectStack objTtl;\n    initStaticStringObject(objTtl, sdscatprintf(sdsempty(), \"%lld\", expire));\n    redisObject *argv[4] = {shared.pexpirememberat, key.unsafe_robjcast(), subkey.unsafe_robjcast(), &objTtl};\n    replicationFeedSlaves(g_pserver->slaves, db->id, argv, 4);\n\n    sdsfree(szFromObj(&objTtl));\n}\n\nvoid _clientAsyncReplyBufferReserve(client *c, size_t len);\n\nvoid flushReplBacklogToClients()\n{\n    serverAssert(GlobalLocksAcquired());\n    /* If we have the repl backlog lock, we will deadlock */\n    serverAssert(!g_pserver->repl_backlog_lock.fOwnLock());\n    if (g_pserver->repl_batch_offStart < 0)\n        return;\n    \n    if (g_pserver->repl_batch_offStart != g_pserver->master_repl_offset) {\n        bool fAsyncWrite = false;\n        long long min_offset = LLONG_MAX;\n        // Ensure no overflow\n        serverAssert(g_pserver->repl_batch_offStart < g_pserver->master_repl_offset);\n        if (g_pserver->master_repl_offset - g_pserver->repl_batch_offStart > g_pserver->repl_backlog_size) {\n            // We overflowed\n            listIter li;\n            listNode *ln;\n            listRewind(g_pserver->slaves, &li);\n            while ((ln = listNext(&li))) {\n                client *c = (client*)listNodeValue(ln);\n                sds sdsClient = catClientInfoString(sdsempty(),c);\n                freeClientAsync(c);\n                serverLog(LL_WARNING,\"Client %s scheduled to be closed ASAP for overcoming of output buffer limits.\", sdsClient);\n                sdsfree(sdsClient);\n            }\n            goto LDone;\n        }\n\n        // Ensure no overflow if we get here\n        serverAssert(g_pserver->master_repl_offset - g_pserver->repl_batch_offStart <= g_pserver->repl_backlog_size);\n        serverAssert(g_pserver->repl_batch_idxStart != g_pserver->repl_backlog_idx);\n\n        // Repl backlog writes must become visible to all threads at this point\n        std::atomic_thread_fence(std::memory_order_release);\n\n        listIter li;\n        listNode *ln;\n        listRewind(g_pserver->slaves, &li);\n        /* We don't actually write any data in this function since we send data \n         * directly from the replication backlog to replicas in writeToClient.\n         * \n         * What we do however, is set the end offset of each replica here. This way, \n         * future calls to writeToClient will know up to where in the replication\n         * backlog is valid for writing. */  \n        while ((ln = listNext(&li))) {\n            client *replica = (client*)listNodeValue(ln);\n\n            if (!canFeedReplicaReplBuffer(replica)) continue;\n            if (replica->flags & CLIENT_CLOSE_ASAP) continue;\n\n            std::unique_lock<fastlock> ul(replica->lock);\n            if (!FCorrectThread(replica))\n                fAsyncWrite = true;\n\n            /* We should have set the repl_curr_off when synchronizing, so it shouldn't be -1 here */\n            serverAssert(replica->repl_curr_off != -1);\n\n            min_offset = std::min(min_offset, replica->repl_curr_off);      \n\n            replica->repl_end_off = g_pserver->master_repl_offset;\n\n            /* Only if the there isn't already a pending write do we prepare the client to write */\n            if (replica->repl_curr_off == g_pserver->master_repl_offset) {\n                serverLog(LL_DEBUG, \"Pending write when it's on repl_offset=%lld\", g_pserver->master_repl_offset);\n                continue;\n            }\n            prepareClientToWrite(replica);\n        }\n        if (fAsyncWrite)\n            ProcessPendingAsyncWrites();\n\nLDone:\n        // This may be called multiple times per \"frame\" so update with our progress flushing to clients\n        g_pserver->repl_batch_idxStart = g_pserver->repl_backlog_idx;\n        g_pserver->repl_batch_offStart = g_pserver->master_repl_offset;\n        g_pserver->repl_lowest_off.store(min_offset == LLONG_MAX ? -1 : min_offset, std::memory_order_seq_cst);\n    } \n}\n\n\n/* Failover cron function, checks coordinated failover state. \n *\n * Implementation note: The current implementation calls replicationSetMaster()\n * to start the failover request, this has some unintended side effects if the\n * failover doesn't work like blocked clients will be unblocked and replicas will\n * be disconnected. This could be optimized further.\n */\nvoid updateFailoverStatus(void) {\n    if (g_pserver->failover_state != FAILOVER_WAIT_FOR_SYNC) return;\n    serverAssert(!g_pserver->fActiveReplica);\n    mstime_t now = g_pserver->mstime;\n\n    /* Check if failover operation has timed out */\n    if (g_pserver->failover_end_time && g_pserver->failover_end_time <= now) {\n        if (g_pserver->force_failover) {\n            serverLog(LL_NOTICE,\n                \"FAILOVER to %s:%d time out exceeded, failing over.\",\n                g_pserver->target_replica_host, g_pserver->target_replica_port);\n            g_pserver->failover_state = FAILOVER_IN_PROGRESS;\n            /* If timeout has expired force a failover if requested. */\n            replicationAddMaster(g_pserver->target_replica_host,\n                g_pserver->target_replica_port);\n            return;\n        } else {\n            /* Force was not requested, so timeout. */\n            redisMaster *mi = listLength(g_pserver->masters) ? (redisMaster*)listNodeValue(listFirst(g_pserver->masters)) : nullptr;\n            abortFailover(mi, \"Replica never caught up before timeout\");\n            return;\n        }\n    }\n\n    /* Check to see if the replica has caught up so failover can start */\n    client *replica = NULL;\n    if (g_pserver->target_replica_host) {\n        replica = findReplica(g_pserver->target_replica_host, \n            g_pserver->target_replica_port);\n    } else {\n        listIter li;\n        listNode *ln;\n\n        listRewind(g_pserver->slaves,&li);\n        /* Find any replica that has matched our repl_offset */\n        while((ln = listNext(&li))) {\n            replica = (client*)listNodeValue(ln);\n            if (replica->repl_ack_off == g_pserver->master_repl_offset) {\n                char ip[NET_IP_STR_LEN], *replicaaddr = replica->slave_addr;\n\n                if (!replicaaddr) {\n                    if (connPeerToString(replica->conn,ip,sizeof(ip),NULL) == -1)\n                        continue;\n                    replicaaddr = ip;\n                }\n\n                /* We are now failing over to this specific node */\n                g_pserver->target_replica_host = zstrdup(replicaaddr);\n                g_pserver->target_replica_port = replica->slave_listening_port;\n                break;\n            }\n        }\n    }\n\n    /* We've found a replica that is caught up */\n    if (replica && (replica->repl_ack_off == g_pserver->master_repl_offset)) {\n        g_pserver->failover_state = FAILOVER_IN_PROGRESS;\n        serverLog(LL_NOTICE,\n                \"Failover target %s:%d is synced, failing over.\",\n                g_pserver->target_replica_host, g_pserver->target_replica_port);\n        /* Designated replica is caught up, failover to it. */\n        replicationAddMaster(g_pserver->target_replica_host,\n            g_pserver->target_replica_port);\n    }\n}\n\n// If we automatically grew the backlog we need to trim it back to\n//  the config setting when possible\nvoid trimReplicationBacklog() {\n    serverAssert(GlobalLocksAcquired());\n    serverAssert(g_pserver->repl_batch_offStart < 0);   // we shouldn't be in a batch\n    if (g_pserver->repl_backlog_size <= g_pserver->repl_backlog_config_size)\n        return; // We're already a good size\n    if (g_pserver->repl_lowest_off > 0 && (g_pserver->master_repl_offset - g_pserver->repl_lowest_off + 1) > g_pserver->repl_backlog_config_size)\n        return; // There is untransmitted data we can't truncate\n    if (cserver.force_backlog_disk && g_pserver->repl_backlog == g_pserver->repl_backlog_disk)\n        return; // We're already in the disk backlog and we're told to stay there\n\n    serverLog(LL_NOTICE, \"Reclaiming %lld replication backlog bytes\", g_pserver->repl_backlog_size - g_pserver->repl_backlog_config_size);\n    resizeReplicationBacklog(g_pserver->repl_backlog_config_size);\n}\n"
  },
  {
    "path": "src/rio.cpp",
    "content": "/* rio.c is a simple stream-oriented I/O abstraction that provides an interface\n * to write code that can consume/produce data using different concrete input\n * and output devices. For instance the same rdb.c code using the rio\n * abstraction can be used to read and write the RDB format using in-memory\n * buffers or files.\n *\n * A rio object provides the following methods:\n *  read: read from stream.\n *  write: write to stream.\n *  tell: get the current offset.\n *\n * It is also possible to set a 'checksum' method that is used by rio.c in order\n * to compute a checksum of the data written or read, or to query the rio object\n * for the current checksum.\n *\n * ----------------------------------------------------------------------------\n *\n * Copyright (c) 2009-2012, Pieter Noordhuis <pcnoordhuis at gmail dot com>\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n\n#include \"fmacros.h\"\n#include <string.h>\n#include <stdio.h>\n#include <unistd.h>\n#include \"rio.h\"\n#include \"util.h\"\n#include \"crc64.h\"\n#include \"config.h\"\n#include \"server.h\"\n\n/* ------------------------- Buffer I/O implementation ----------------------- */\n\n/* Returns 1 or 0 for success/failure. */\nstatic size_t rioBufferWrite(rio *r, const void *buf, size_t len) {\n    r->io.buffer.ptr = sdscatlen(r->io.buffer.ptr,(char*)buf,len);\n    r->io.buffer.pos += len;\n    return 1;\n}\n\n/* Returns 1 or 0 for success/failure. */\nstatic size_t rioBufferRead(rio *r, void *buf, size_t len) {\n    if (sdslen(r->io.buffer.ptr)-r->io.buffer.pos < len)\n        return 0; /* not enough buffer to return len bytes. */\n    memcpy(buf,r->io.buffer.ptr+r->io.buffer.pos,len);\n    r->io.buffer.pos += len;\n    return 1;\n}\n\nstatic size_t rioConstBufferRead(rio *r, void *buf, size_t len) {\n     if (r->io.buffer.len-r->io.buffer.pos < (off_t)len)\n        return 0; /* not enough buffer to return len bytes. */\n    memcpy(buf,r->io.buffer.ptr+r->io.buffer.pos,len);\n    r->io.buffer.pos += len;\n    return 1;\n}\n\n/* Returns read/write position in buffer. */\nstatic off_t rioBufferTell(rio *r) {\n    return r->io.buffer.pos;\n}\n\n/* Flushes any buffer to target device if applicable. Returns 1 on success\n * and 0 on failures. */\nstatic int rioBufferFlush(rio *r) {\n    UNUSED(r);\n    return 1; /* Nothing to do, our write just appends to the buffer. */\n}\n\nstatic const rio rioBufferIO = {\n    rioBufferRead,\n    rioBufferWrite,\n    rioBufferTell,\n    rioBufferFlush,\n    NULL,           /* update_checksum */\n    NULL,           /* update checksum arg */\n    0,              /* current checksum */\n    0,              /* flags */\n    0,              /* bytes read or written */\n    0,              /* keys since last callback */\n    0,              /* read/write chunk size */\n    0,              /* last update time */\n    { { NULL, 0 } } /* union for io-specific vars */\n};\n\nstatic const rio rioConstBufferIO = {\n    rioConstBufferRead,\n    nullptr,\n    rioBufferTell,\n    rioBufferFlush,\n    NULL,           /* update_checksum */\n    NULL,           /* update checksum arg */\n    0,              /* current checksum */\n    0,              /* flags */\n    0,              /* bytes read or written */\n    0,              /* keys since last callback */\n    0,              /* read/write chunk size */\n    0,              /* last update time */\n    { { NULL, 0 } } /* union for io-specific vars */\n};\n\nvoid rioInitWithBuffer(rio *r, sds s) {\n    *r = rioBufferIO;\n    r->io.buffer.ptr = s;\n    r->io.buffer.pos = 0;\n}\n\nvoid rioInitWithConstBuffer(rio *r, const void *buf, size_t cb)\n{\n    *r = rioConstBufferIO;\n    r->io.buffer.ptr = (sds)buf;\n    r->io.buffer.pos = 0;\n    r->io.buffer.len = cb;\n}\n\n/* --------------------- Stdio file pointer implementation ------------------- */\n\n/* Returns 1 or 0 for success/failure. */\nstatic size_t rioFileWrite(rio *r, const void *buf, size_t len) {\n    size_t retval;\n\n    retval = fwrite(buf,len,1,r->io.file.fp);\n    r->io.file.buffered += len;\n\n    if (r->io.file.autosync &&\n        r->io.file.buffered >= r->io.file.autosync)\n    {\n        fflush(r->io.file.fp);\n        if (redis_fsync(fileno(r->io.file.fp)) == -1) return 0;\n        r->io.file.buffered = 0;\n    }\n    return retval;\n}\n\n/* Returns 1 or 0 for success/failure. */\nstatic size_t rioFileRead(rio *r, void *buf, size_t len) {\n    return fread(buf,len,1,r->io.file.fp);\n}\n\n/* Returns read/write position in file. */\nstatic off_t rioFileTell(rio *r) {\n    return ftello(r->io.file.fp);\n}\n\n/* Flushes any buffer to target device if applicable. Returns 1 on success\n * and 0 on failures. */\nstatic int rioFileFlush(rio *r) {\n    return (fflush(r->io.file.fp) == 0) ? 1 : 0;\n}\n\nstatic const rio rioFileIO = {\n    rioFileRead,\n    rioFileWrite,\n    rioFileTell,\n    rioFileFlush,\n    NULL,           /* update_checksum */\n    NULL,           /* update checksum arg */\n    0,              /* current checksum */\n    0,              /* flags */\n    0,              /* bytes read or written */\n    0,              /* keys since last callback */\n    0,              /* read/write chunk size */\n    0,              /* last update time */\n    { { NULL, 0 } } /* union for io-specific vars */\n};\n\nvoid rioInitWithFile(rio *r, FILE *fp) {\n    *r = rioFileIO;\n    r->io.file.fp = fp;\n    r->io.file.buffered = 0;\n    r->io.file.autosync = 0;\n}\n\n/* ------------------- Connection implementation -------------------\n * We use this RIO implementation when reading an RDB file directly from\n * the connection to the memory via rdbLoadRio(), thus this implementation\n * only implements reading from a connection that is, normally,\n * just a socket. */\n\nstatic size_t rioConnWrite(rio *r, const void *buf, size_t len) {\n    UNUSED(r);\n    UNUSED(buf);\n    UNUSED(len);\n    return 0; /* Error, this target does not yet support writing. */\n}\n\n/* Returns 1 or 0 for success/failure. */\nstatic size_t rioConnRead(rio *r, void *buf, size_t len) {\n    size_t avail = sdslen(r->io.conn.buf)-r->io.conn.pos;\n\n    /* If the buffer is too small for the entire request: realloc. */\n    if (sdslen(r->io.conn.buf) + sdsavail(r->io.conn.buf) < len)\n        r->io.conn.buf = sdsMakeRoomFor(r->io.conn.buf, len - sdslen(r->io.conn.buf));\n\n    /* If the remaining unused buffer is not large enough: memmove so that we\n     * can read the rest. */\n    if (len > avail && sdsavail(r->io.conn.buf) < len - avail) {\n        sdsrange(r->io.conn.buf, r->io.conn.pos, -1);\n        r->io.conn.pos = 0;\n    }\n\n    /* If we don't already have all the data in the sds, read more */\n    while (len > sdslen(r->io.conn.buf) - r->io.conn.pos) {\n        size_t buffered = sdslen(r->io.conn.buf) - r->io.conn.pos;\n        size_t needs = len - buffered;\n        /* Read either what's missing, or PROTO_IOBUF_LEN, the bigger of\n         * the two. */\n        size_t toread = needs < PROTO_IOBUF_LEN ? PROTO_IOBUF_LEN: needs;\n        if (toread > sdsavail(r->io.conn.buf)) toread = sdsavail(r->io.conn.buf);\n        if (r->io.conn.read_limit != 0 &&\n            r->io.conn.read_so_far + buffered + toread > r->io.conn.read_limit)\n        {\n            /* Make sure the caller didn't request to read past the limit.\n             * If they didn't we'll buffer till the limit, if they did, we'll\n             * return an error. */\n            if (r->io.conn.read_limit >= r->io.conn.read_so_far + len)\n                toread = r->io.conn.read_limit - r->io.conn.read_so_far - buffered;\n            else {\n                errno = EOVERFLOW;\n                return 0;\n            }\n        }\n        int retval = connRead(r->io.conn.conn,\n                          (char*)r->io.conn.buf + sdslen(r->io.conn.buf),\n                          toread);\n        if (retval <= 0) {\n            if (errno == EWOULDBLOCK) errno = ETIMEDOUT;\n            return 0;\n        }\n        sdsIncrLen(r->io.conn.buf, retval);\n    }\n\n    memcpy(buf, (char*)r->io.conn.buf + r->io.conn.pos, len);\n    r->io.conn.read_so_far += len;\n    r->io.conn.pos += len;\n    return len;\n}\n\n/* Returns read/write position in file. */\nstatic off_t rioConnTell(rio *r) {\n    return r->io.conn.read_so_far;\n}\n\n/* Flushes any buffer to target device if applicable. Returns 1 on success\n * and 0 on failures. */\nstatic int rioConnFlush(rio *r) {\n    /* Our flush is implemented by the write method, that recognizes a\n     * buffer set to NULL with a count of zero as a flush request. */\n    return rioConnWrite(r,NULL,0);\n}\n\nstatic const rio rioConnIO = {\n    rioConnRead,\n    rioConnWrite,\n    rioConnTell,\n    rioConnFlush,\n    NULL,           /* update_checksum */\n    NULL,           /* update checksum arg */\n    0,              /* current checksum */\n    0,              /* flags */\n    0,              /* bytes read or written */\n    0,              /* keys since last callback */\n    0,              /* read/write chunk size */\n    0,              /* last update time */\n    { { NULL, 0 } } /* union for io-specific vars */\n};\n\n/* Create an RIO that implements a buffered read from an fd\n * read_limit argument stops buffering when the reaching the limit. */\nvoid rioInitWithConn(rio *r, connection *conn, size_t read_limit) {\n    *r = rioConnIO;\n    r->io.conn.conn = conn;\n    r->io.conn.pos = 0;\n    r->io.conn.read_limit = read_limit;\n    r->io.conn.read_so_far = 0;\n    r->io.conn.buf = sdsnewlen(NULL, PROTO_IOBUF_LEN);\n    sdsclear(r->io.conn.buf);\n}\n\n/* Release the RIO stream. Optionally returns the unread buffered data\n * when the SDS pointer 'remaining' is passed. */\nvoid rioFreeConn(rio *r, sds *remaining) {\n    if (remaining && (size_t)r->io.conn.pos < sdslen(r->io.conn.buf)) {\n        if (r->io.conn.pos > 0) sdsrange(r->io.conn.buf, r->io.conn.pos, -1);\n        *remaining = r->io.conn.buf;\n    } else {\n        sdsfree(r->io.conn.buf);\n        if (remaining) *remaining = NULL;\n    }\n    r->io.conn.buf = NULL;\n}\n\n/* ------------------- File descriptor implementation ------------------\n * This target is used to write the RDB file to pipe, when the master just\n * streams the data to the replicas without creating an RDB on-disk image\n * (diskless replication option).\n * It only implements writes. */\n\n/* Returns 1 or 0 for success/failure.\n *\n * When buf is NULL and len is 0, the function performs a flush operation\n * if there is some pending buffer, so this function is also used in order\n * to implement rioFdFlush(). */\nstatic size_t rioFdWrite(rio *r, const void *buf, size_t len) {\n    ssize_t retval;\n    unsigned char *p = (unsigned char*) buf;\n    int doflush = (buf == NULL && len == 0);\n\n    /* For small writes, we rather keep the data in user-space buffer, and flush\n     * it only when it grows. however for larger writes, we prefer to flush\n     * any pre-existing buffer, and write the new one directly without reallocs\n     * and memory copying. */\n    if (len > PROTO_IOBUF_LEN) {\n        /* First, flush any pre-existing buffered data. */\n        if (sdslen(r->io.fd.buf)) {\n            if (rioFdWrite(r, NULL, 0) == 0)\n                return 0;\n        }\n        /* Write the new data, keeping 'p' and 'len' from the input. */\n    } else {\n        if (len) {\n            r->io.fd.buf = sdscatlen(r->io.fd.buf,buf,len);\n            if (sdslen(r->io.fd.buf) > PROTO_IOBUF_LEN)\n                doflush = 1;\n            if (!doflush)\n                return 1;\n        }\n        /* Flusing the buffered data. set 'p' and 'len' accordintly. */\n        p = (unsigned char*) r->io.fd.buf;\n        len = sdslen(r->io.fd.buf);\n    }\n\n    size_t nwritten = 0;\n    while(nwritten != len) {\n        retval = write(r->io.fd.fd,p+nwritten,len-nwritten);\n        if (retval <= 0) {\n            /* With blocking io, which is the sole user of this\n             * rio target, EWOULDBLOCK is returned only because of\n             * the SO_SNDTIMEO socket option, so we translate the error\n             * into one more recognizable by the user. */\n            if (retval == -1 && errno == EWOULDBLOCK) errno = ETIMEDOUT;\n            return 0; /* error. */\n        }\n        nwritten += retval;\n    }\n\n    r->io.fd.pos += len;\n    sdsclear(r->io.fd.buf);\n    return 1;\n}\n\n/* Returns 1 or 0 for success/failure. */\nstatic size_t rioFdRead(rio *r, void *buf, size_t len) {\n    UNUSED(r);\n    UNUSED(buf);\n    UNUSED(len);\n    return 0; /* Error, this target does not support reading. */\n}\n\n/* Returns read/write position in file. */\nstatic off_t rioFdTell(rio *r) {\n    return r->io.fd.pos;\n}\n\n/* Flushes any buffer to target device if applicable. Returns 1 on success\n * and 0 on failures. */\nstatic int rioFdFlush(rio *r) {\n    /* Our flush is implemented by the write method, that recognizes a\n     * buffer set to NULL with a count of zero as a flush request. */\n    return rioFdWrite(r,NULL,0);\n}\n\nstatic const rio rioFdIO = {\n    rioFdRead,\n    rioFdWrite,\n    rioFdTell,\n    rioFdFlush,\n    NULL,           /* update_checksum */\n    NULL,           /* update checksum arg */\n    0,              /* current checksum */\n    0,              /* flags */\n    0,              /* bytes read or written */\n    0,              /* keys since last callback */\n    0,              /* read/write chunk size */\n    0,              /* last update time */\n    { { NULL, 0 } } /* union for io-specific vars */\n};\n\nvoid rioInitWithFd(rio *r, int fd) {\n    *r = rioFdIO;\n    r->io.fd.fd = fd;\n    r->io.fd.pos = 0;\n    r->io.fd.buf = sdsempty();\n}\n\n/* release the rio stream. */\nvoid rioFreeFd(rio *r) {\n    sdsfree(r->io.fd.buf);\n}\n\n/* ---------------------------- Generic functions ---------------------------- */\n\n/* This function can be installed both in memory and file streams when checksum\n * computation is needed. */\nvoid rioGenericUpdateChecksum(rio *r, const void *buf, size_t len) {\n    r->cksum = crc64(r->cksum,(const unsigned char*)buf,len);\n}\n\n/* Set the file-based rio object to auto-fsync every 'bytes' file written.\n * By default this is set to zero that means no automatic file sync is\n * performed.\n *\n * This feature is useful in a few contexts since when we rely on OS write\n * buffers sometimes the OS buffers way too much, resulting in too many\n * disk I/O concentrated in very little time. When we fsync in an explicit\n * way instead the I/O pressure is more distributed across time. */\nvoid rioSetAutoSync(rio *r, off_t bytes) {\n    if(r->write != rioFileIO.write) return;\n    r->io.file.autosync = bytes;\n}\n\n/* --------------------------- Higher level interface --------------------------\n *\n * The following higher level functions use lower level rio.c functions to help\n * generating the Redis protocol for the Append Only File. */\n\n/* Write multi bulk count in the format: \"*<count>\\r\\n\". */\nsize_t rioWriteBulkCount(rio *r, char prefix, long count) {\n    char cbuf[128];\n    int clen;\n\n    cbuf[0] = prefix;\n    clen = 1+ll2string(cbuf+1,sizeof(cbuf)-1,count);\n    cbuf[clen++] = '\\r';\n    cbuf[clen++] = '\\n';\n    if (rioWrite(r,cbuf,clen) == 0) return 0;\n    return clen;\n}\n\n/* Write binary-safe string in the format: \"$<count>\\r\\n<payload>\\r\\n\". */\nsize_t rioWriteBulkString(rio *r, const char *buf, size_t len) {\n    size_t nwritten;\n\n    if ((nwritten = rioWriteBulkCount(r,'$',len)) == 0) return 0;\n    if (len > 0 && rioWrite(r,buf,len) == 0) return 0;\n    if (rioWrite(r,\"\\r\\n\",2) == 0) return 0;\n    return nwritten+len+2;\n}\n\n/* Write a long long value in format: \"$<count>\\r\\n<payload>\\r\\n\". */\nsize_t rioWriteBulkLongLong(rio *r, long long l) {\n    char lbuf[32];\n    unsigned int llen;\n\n    llen = ll2string(lbuf,sizeof(lbuf),l);\n    return rioWriteBulkString(r,lbuf,llen);\n}\n\n/* Write a double value in the format: \"$<count>\\r\\n<payload>\\r\\n\" */\nsize_t rioWriteBulkDouble(rio *r, double d) {\n    char dbuf[128];\n    unsigned int dlen;\n\n    dlen = snprintf(dbuf,sizeof(dbuf),\"%.17g\",d);\n    return rioWriteBulkString(r,dbuf,dlen);\n}\n"
  },
  {
    "path": "src/rio.h",
    "content": "/*\n * Copyright (c) 2009-2012, Pieter Noordhuis <pcnoordhuis at gmail dot com>\n * Copyright (c) 2009-2019, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n\n#ifndef __REDIS_RIO_H\n#define __REDIS_RIO_H\n\n#include <stdio.h>\n#include <stdint.h>\n#include \"sds.h\"\n#include \"connection.h\"\n\n#define RIO_FLAG_READ_ERROR (1<<0)\n#define RIO_FLAG_WRITE_ERROR (1<<1)\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\nstruct _rio {\n    /* Backend functions.\n     * Since this functions do not tolerate short writes or reads the return\n     * value is simplified to: zero on error, non zero on complete success. */\n    size_t (*read)(struct _rio *, void *buf, size_t len);\n    size_t (*write)(struct _rio *, const void *buf, size_t len);\n    off_t (*tell)(struct _rio *);\n    int (*flush)(struct _rio *);\n    /* The update_cksum method if not NULL is used to compute the checksum of\n     * all the data that was read or written so far. The method should be\n     * designed so that can be called with the current checksum, and the buf\n     * and len fields pointing to the new block of data to add to the checksum\n     * computation. */\n    void (*update_cksum)(struct _rio *, const void *buf, size_t len);\n    void *chksum_arg;\n\n    /* The current checksum and flags (see RIO_FLAG_*) */\n    uint64_t cksum, flags;\n\n    /* number of keys loaded since last rdbLoadProgressCallback */\n    unsigned long int keys_since_last_callback;\n\n    /* number of bytes read or written */\n    size_t processed_bytes;\n\n    /* maximum single read or write chunk size */\n    size_t max_processing_chunk;\n\n    /* last update time */\n    long long last_update;\n\n    /* Backend-specific vars. */\n    union {\n        /* In-memory buffer target. */\n        struct {\n            sds ptr;\n            off_t pos;\n            off_t len;  // For const buffers only\n        } buffer;\n        /* Stdio file pointer target. */\n        struct {\n            FILE *fp;\n            off_t buffered; /* Bytes written since last fsync. */\n            off_t autosync; /* fsync after 'autosync' bytes written. */\n        } file;\n        /* Connection object (used to read from socket) */\n        struct {\n            connection *conn;   /* Connection */\n            off_t pos;    /* pos in buf that was returned */\n            sds buf;      /* buffered data */\n            size_t read_limit;  /* don't allow to buffer/read more than that */\n            size_t read_so_far; /* amount of data read from the rio (not buffered) */\n        } conn;\n        /* FD target (used to write to pipe). */\n        struct {\n            int fd;       /* File descriptor. */\n            off_t pos;\n            sds buf;\n        } fd;\n    } io;\n};\n\ntypedef struct _rio rio;\n\n/* The following functions are our interface with the stream. They'll call the\n * actual implementation of read / write / tell, and will update the checksum\n * if needed. */\n\nstatic inline size_t rioWrite(rio *r, const void *buf, size_t len) {\n    if (r->flags & RIO_FLAG_WRITE_ERROR) return 0;\n    while (len) {\n        size_t bytes_to_write = (r->max_processing_chunk && r->max_processing_chunk < len) ? r->max_processing_chunk : len;\n        if (r->update_cksum) r->update_cksum(r,buf,bytes_to_write);\n        if (r->write(r,buf,bytes_to_write) == 0) {\n            r->flags |= RIO_FLAG_WRITE_ERROR;\n            return 0;\n        }\n        buf = (char*)buf + bytes_to_write;\n        len -= bytes_to_write;\n        r->processed_bytes += bytes_to_write;\n    }\n    return 1;\n}\n\nstatic inline size_t rioRead(rio *r, void *buf, size_t len) {\n    if (r->flags & RIO_FLAG_READ_ERROR) return 0;\n    while (len) {\n        size_t bytes_to_read = (r->max_processing_chunk && r->max_processing_chunk < len) ? r->max_processing_chunk : len;\n        if (r->read(r,buf,bytes_to_read) == 0) {\n            r->flags |= RIO_FLAG_READ_ERROR;\n            return 0;\n        }\n        if (r->update_cksum) r->update_cksum(r,buf,bytes_to_read);\n        buf = (char*)buf + bytes_to_read;\n        len -= bytes_to_read;\n        r->processed_bytes += bytes_to_read;\n    }\n    return 1;\n}\n\nstatic inline off_t rioTell(rio *r) {\n    return r->tell(r);\n}\n\nstatic inline int rioFlush(rio *r) {\n    return r->flush(r);\n}\n\n/* This function allows to know if there was a read error in any past\n * operation, since the rio stream was created or since the last call\n * to rioClearError(). */\nstatic inline int rioGetReadError(rio *r) {\n    return (r->flags & RIO_FLAG_READ_ERROR) != 0;\n}\n\n/* Like rioGetReadError() but for write errors. */\nstatic inline int rioGetWriteError(rio *r) {\n    return (r->flags & RIO_FLAG_WRITE_ERROR) != 0;\n}\n\nstatic inline void rioClearErrors(rio *r) {\n    r->flags &= ~(RIO_FLAG_READ_ERROR|RIO_FLAG_WRITE_ERROR);\n}\n\nvoid rioInitWithFile(rio *r, FILE *fp);\nvoid rioInitWithBuffer(rio *r, sds s);\nvoid rioInitWithConstBuffer(rio *r, const void *rgch, size_t cch);\nvoid rioInitWithConn(rio *r, connection *conn, size_t read_limit);\nvoid rioInitWithFd(rio *r, int fd);\n\nvoid rioFreeFd(rio *r);\nvoid rioFreeConn(rio *r, sds* out_remainingBufferedData);\n\nsize_t rioWriteBulkCount(rio *r, char prefix, long count);\nsize_t rioWriteBulkString(rio *r, const char *buf, size_t len);\nsize_t rioWriteBulkLongLong(rio *r, long long l);\nsize_t rioWriteBulkDouble(rio *r, double d);\n\nstruct redisObject;\nint rioWriteBulkObject(rio *r, struct redisObject *obj);\n\nvoid rioGenericUpdateChecksum(rio *r, const void *buf, size_t len);\nvoid rioSetAutoSync(rio *r, off_t bytes);\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n"
  },
  {
    "path": "src/scripting.cpp",
    "content": "/*\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n#include \"sha1.h\"\n#include \"rand.h\"\n#include \"cluster.h\"\n#include \"monotonic.h\"\n\nextern \"C\" {\n#include <lua.h>\n#include <lauxlib.h>\n#include <lualib.h>\n}\n#include <ctype.h>\n#include <math.h>\n#include <mutex>\n\nchar *redisProtocolToLuaType_Int(lua_State *lua, char *reply);\nchar *redisProtocolToLuaType_Bulk(lua_State *lua, char *reply);\nchar *redisProtocolToLuaType_Status(lua_State *lua, char *reply);\nchar *redisProtocolToLuaType_Error(lua_State *lua, char *reply);\nchar *redisProtocolToLuaType_Aggregate(lua_State *lua, char *reply, int atype);\nchar *redisProtocolToLuaType_Null(lua_State *lua, char *reply);\nchar *redisProtocolToLuaType_Bool(lua_State *lua, char *reply, int tf);\nchar *redisProtocolToLuaType_Double(lua_State *lua, char *reply);\nint redis_math_random (lua_State *L);\nint redis_math_randomseed (lua_State *L);\nvoid ldbInit(void);\nvoid ldbDisable(client *c);\nvoid ldbEnable(client *c);\nvoid evalGenericCommandWithDebugging(client *c, int evalsha);\nvoid luaLdbLineHook(lua_State *lua, lua_Debug *ar);\nvoid ldbLog(sds entry);\nvoid ldbLogRedisReply(char *reply);\nsds ldbCatStackValue(sds s, lua_State *lua, int idx);\n\n/* Debugger shared state is stored inside this global structure. */\n#define LDB_BREAKPOINTS_MAX 64  /* Max number of breakpoints. */\n#define LDB_MAX_LEN_DEFAULT 256 /* Default len limit for replies / var dumps. */\nstruct ldbState {\n    connection *conn; /* Connection of the debugging client. */\n    int active; /* Are we debugging EVAL right now? */\n    int forked; /* Is this a fork()ed debugging session? */\n    list *logs; /* List of messages to send to the client. */\n    list *traces; /* Messages about Redis commands executed since last stop.*/\n    list *children; /* All forked debugging sessions pids. */\n    int bp[LDB_BREAKPOINTS_MAX]; /* An array of breakpoints line numbers. */\n    int bpcount; /* Number of valid entries inside bp. */\n    int step;   /* Stop at next line regardless of breakpoints. */\n    int luabp;  /* Stop at next line because redis.breakpoint() was called. */\n    sds *src;   /* Lua script source code split by line. */\n    int lines;  /* Number of lines in 'src'. */\n    int currentline;    /* Current line number. */\n    sds cbuf;   /* Debugger client command buffer. */\n    size_t maxlen;  /* Max var dump / reply length. */\n    int maxlen_hint_sent; /* Did we already hint about \"set maxlen\"? */\n} ldb;\n\n/* ---------------------------------------------------------------------------\n * Utility functions.\n * ------------------------------------------------------------------------- */\n\n/* Perform the SHA1 of the input string. We use this both for hashing script\n * bodies in order to obtain the Lua function name, and in the implementation\n * of redis.sha1().\n *\n * 'digest' should point to a 41 bytes buffer: 40 for SHA1 converted into an\n * hexadecimal number, plus 1 byte for null term. */\nvoid sha1hex(char *digest, char *script, size_t len) {\n    SHA1_CTX ctx;\n    unsigned char hash[20];\n    const char *cset = \"0123456789abcdef\";\n    int j;\n\n    SHA1Init(&ctx);\n    SHA1Update(&ctx,(unsigned char*)script,len);\n    SHA1Final(hash,&ctx);\n\n    for (j = 0; j < 20; j++) {\n        digest[j*2] = cset[((hash[j]&0xF0)>>4)];\n        digest[j*2+1] = cset[(hash[j]&0xF)];\n    }\n    digest[40] = '\\0';\n}\n\n/* ---------------------------------------------------------------------------\n * Redis reply to Lua type conversion functions.\n * ------------------------------------------------------------------------- */\n\n/* Take a Redis reply in the Redis protocol format and convert it into a\n * Lua type. Thanks to this function, and the introduction of not connected\n * clients, it is trivial to implement the redis() lua function.\n *\n * Basically we take the arguments, execute the Redis command in the context\n * of a non connected client, then take the generated reply and convert it\n * into a suitable Lua type. With this trick the scripting feature does not\n * need the introduction of a full Redis internals API. The script\n * is like a normal client that bypasses all the slow I/O paths.\n *\n * Note: in this function we do not do any sanity check as the reply is\n * generated by Redis directly. This allows us to go faster.\n *\n * Errors are returned as a table with a single 'err' field set to the\n * error string.\n */\n\nchar *redisProtocolToLuaType(lua_State *lua, char* reply) {\n\n    if (!lua_checkstack(lua, 5)) {\n        /*\n         * Increase the Lua stack if needed, to make sure there is enough room\n         * to push 5 elements to the stack. On failure, exit with panic.\n         * Notice that we need, in the worst case, 5 elements because redisProtocolToLuaType_Aggregate\n         * might push 5 elements to the Lua stack.*/\n        serverPanic(\"lua stack limit reach when parsing redis.call reply\");\n    }\n\n    char *p = reply;\n\n    switch(*p) {\n    case ':': p = redisProtocolToLuaType_Int(lua,reply); break;\n    case '$': p = redisProtocolToLuaType_Bulk(lua,reply); break;\n    case '+': p = redisProtocolToLuaType_Status(lua,reply); break;\n    case '-': p = redisProtocolToLuaType_Error(lua,reply); break;\n    case '*': p = redisProtocolToLuaType_Aggregate(lua,reply,*p); break;\n    case '%': p = redisProtocolToLuaType_Aggregate(lua,reply,*p); break;\n    case '~': p = redisProtocolToLuaType_Aggregate(lua,reply,*p); break;\n    case '_': p = redisProtocolToLuaType_Null(lua,reply); break;\n    case '#': p = redisProtocolToLuaType_Bool(lua,reply,p[1]); break;\n    case ',': p = redisProtocolToLuaType_Double(lua,reply); break;\n    }\n    return p;\n}\n\nchar *redisProtocolToLuaType_Int(lua_State *lua, char *reply) {\n    char *p = strchr(reply+1,'\\r');\n    long long value;\n\n    string2ll(reply+1,p-reply-1,&value);\n    lua_pushnumber(lua,(lua_Number)value);\n    return p+2;\n}\n\nchar *redisProtocolToLuaType_Bulk(lua_State *lua, char *reply) {\n    char *p = strchr(reply+1,'\\r');\n    long long bulklen;\n\n    string2ll(reply+1,p-reply-1,&bulklen);\n    if (bulklen == -1) {\n        lua_pushboolean(lua,0);\n        return p+2;\n    } else {\n        lua_pushlstring(lua,p+2,bulklen);\n        return p+2+bulklen+2;\n    }\n}\n\nchar *redisProtocolToLuaType_Status(lua_State *lua, char *reply) {\n    char *p = strchr(reply+1,'\\r');\n\n    lua_newtable(lua);\n    lua_pushstring(lua,\"ok\");\n    lua_pushlstring(lua,reply+1,p-reply-1);\n    lua_settable(lua,-3);\n    return p+2;\n}\n\nchar *redisProtocolToLuaType_Error(lua_State *lua, char *reply) {\n    char *p = strchr(reply+1,'\\r');\n\n    lua_newtable(lua);\n    lua_pushstring(lua,\"err\");\n    lua_pushlstring(lua,reply+1,p-reply-1);\n    lua_settable(lua,-3);\n    return p+2;\n}\n\nchar *redisProtocolToLuaType_Aggregate(lua_State *lua, char *reply, int atype) {\n    char *p = strchr(reply+1,'\\r');\n    long long mbulklen;\n    int j = 0;\n\n    string2ll(reply+1,p-reply-1,&mbulklen);\n    if (serverTL->lua_client->resp == 2 || atype == '*') {\n        p += 2;\n        if (mbulklen == -1) {\n            lua_pushboolean(lua,0);\n            return p;\n        }\n        lua_newtable(lua);\n        for (j = 0; j < mbulklen; j++) {\n            lua_pushnumber(lua,j+1);\n            p = redisProtocolToLuaType(lua,p);\n            lua_settable(lua,-3);\n        }\n    } else if (serverTL->lua_client->resp == 3) {\n        /* Here we handle only Set and Map replies in RESP3 mode, since arrays\n         * follow the above RESP2 code path. Note that those are represented\n         * as a table with the \"map\" or \"set\" field populated with the actual\n         * table representing the set or the map type. */\n        p += 2;\n        lua_newtable(lua);\n        lua_pushstring(lua,atype == '%' ? \"map\" : \"set\");\n        lua_newtable(lua);\n        for (j = 0; j < mbulklen; j++) {\n            p = redisProtocolToLuaType(lua,p);\n            if (atype == '%') {\n                p = redisProtocolToLuaType(lua,p);\n            } else {\n                if (!lua_checkstack(lua, 1)) {\n                    /* Notice that here we need to check the stack again because the recursive\n                     * call to redisProtocolToLuaType might have use the room allocated in the stack */\n                    serverPanic(\"lua stack limit reach when parsing redis.call reply\");\n                }\n                lua_pushboolean(lua,1);\n            }\n            lua_settable(lua,-3);\n        }\n        lua_settable(lua,-3);\n    }\n    return p;\n}\n\nchar *redisProtocolToLuaType_Null(lua_State *lua, char *reply) {\n    char *p = strchr(reply+1,'\\r');\n    lua_pushnil(lua);\n    return p+2;\n}\n\nchar *redisProtocolToLuaType_Bool(lua_State *lua, char *reply, int tf) {\n    char *p = strchr(reply+1,'\\r');\n    lua_pushboolean(lua,tf == 't');\n    return p+2;\n}\n\nchar *redisProtocolToLuaType_Double(lua_State *lua, char *reply) {\n    char *p = strchr(reply+1,'\\r');\n    char buf[MAX_LONG_DOUBLE_CHARS+1];\n    size_t len = p-reply-1;\n    double d;\n\n    if (len <= MAX_LONG_DOUBLE_CHARS) {\n        memcpy(buf,reply+1,len);\n        buf[len] = '\\0';\n        d = strtod(buf,NULL); /* We expect a valid representation. */\n    } else {\n        d = 0;\n    }\n\n    lua_newtable(lua);\n    lua_pushstring(lua,\"double\");\n    lua_pushnumber(lua,d);\n    lua_settable(lua,-3);\n    return p+2;\n}\n\n/* This function is used in order to push an error on the Lua stack in the\n * format used by redis.pcall to return errors, which is a lua table\n * with a single \"err\" field set to the error string. Note that this\n * table is never a valid reply by proper commands, since the returned\n * tables are otherwise always indexed by integers, never by strings. */\nvoid luaPushError(lua_State *lua, const char *error) {\n    lua_Debug dbg;\n\n    /* If debugging is active and in step mode, log errors resulting from\n     * Redis commands. */\n    if (ldb.active && ldb.step) {\n        ldbLog(sdscatprintf(sdsempty(),\"<error> %s\",error));\n    }\n\n    lua_newtable(lua);\n    lua_pushstring(lua,\"err\");\n\n    /* Attempt to figure out where this function was called, if possible */\n    if(lua_getstack(lua, 1, &dbg) && lua_getinfo(lua, \"nSl\", &dbg)) {\n        sds msg = sdscatprintf(sdsempty(), \"%s: %d: %s\",\n            dbg.source, dbg.currentline, error);\n        lua_pushstring(lua, msg);\n        sdsfree(msg);\n    } else {\n        lua_pushstring(lua, error);\n    }\n    lua_settable(lua,-3);\n}\n\n/* In case the error set into the Lua stack by luaPushError() was generated\n * by the non-error-trapping version of redis.pcall(), which is redis.call(),\n * this function will raise the Lua error so that the execution of the\n * script will be halted. */\nint luaRaiseError(lua_State *lua) {\n    lua_pushstring(lua,\"err\");\n    lua_gettable(lua,-2);\n    return lua_error(lua);\n}\n\n/* Sort the array currently in the stack. We do this to make the output\n * of commands like KEYS or SMEMBERS something deterministic when called\n * from Lua (to play well with AOf/replication).\n *\n * The array is sorted using table.sort itself, and assuming all the\n * list elements are strings. */\nvoid luaSortArray(lua_State *lua) {\n    /* Initial Stack: array */\n    lua_getglobal(lua,\"table\");\n    lua_pushstring(lua,\"sort\");\n    lua_gettable(lua,-2);       /* Stack: array, table, table.sort */\n    lua_pushvalue(lua,-3);      /* Stack: array, table, table.sort, array */\n    if (lua_pcall(lua,1,0,0)) {\n        /* Stack: array, table, error */\n\n        /* We are not interested in the error, we assume that the problem is\n         * that there are 'false' elements inside the array, so we try\n         * again with a slower function but able to handle this case, that\n         * is: table.sort(table, __redis__compare_helper) */\n        lua_pop(lua,1);             /* Stack: array, table */\n        lua_pushstring(lua,\"sort\"); /* Stack: array, table, sort */\n        lua_gettable(lua,-2);       /* Stack: array, table, table.sort */\n        lua_pushvalue(lua,-3);      /* Stack: array, table, table.sort, array */\n        lua_getglobal(lua,\"__redis__compare_helper\");\n        /* Stack: array, table, table.sort, array, __redis__compare_helper */\n        lua_call(lua,2,0);\n    }\n    /* Stack: array (sorted), table */\n    lua_pop(lua,1);             /* Stack: array (sorted) */\n}\n\n/* ---------------------------------------------------------------------------\n * Lua reply to Redis reply conversion functions.\n * ------------------------------------------------------------------------- */\n\n/* Reply to client 'c' converting the top element in the Lua stack to a\n * Redis reply. As a side effect the element is consumed from the stack.  */\nvoid luaReplyToRedisReply(client *c, lua_State *lua) {\n\n    if (!lua_checkstack(lua, 4)) {\n        /* Increase the Lua stack if needed to make sure there is enough room\n         * to push 4 elements to the stack. On failure, return error.\n         * Notice that we need, in the worst case, 4 elements because returning a map might\n         * require push 4 elements to the Lua stack.*/\n        addReplyErrorFormat(c, \"reached lua stack limit\");\n        lua_pop(lua,1); /* pop the element from the stack */\n        return;\n    }\n\n    int t = lua_type(lua,-1);\n\n    switch(t) {\n    case LUA_TSTRING:\n        addReplyBulkCBuffer(c,(char*)lua_tostring(lua,-1),lua_strlen(lua,-1));\n        break;\n    case LUA_TBOOLEAN:\n        if (serverTL->lua_client->resp == 2)\n            addReply(c,lua_toboolean(lua,-1) ? shared.cone :\n                                               shared.null[c->resp]);\n        else\n            addReplyBool(c,lua_toboolean(lua,-1));\n        break;\n    case LUA_TNUMBER:\n        addReplyLongLong(c,(long long)lua_tonumber(lua,-1));\n        break;\n    case LUA_TTABLE:\n        /* We need to check if it is an array, an error, or a status reply.\n         * Error are returned as a single element table with 'err' field.\n         * Status replies are returned as single element table with 'ok'\n         * field. */\n\n        /* Handle error reply. */\n        /* we took care of the stack size on function start */\n        lua_pushstring(lua,\"err\");\n        lua_gettable(lua,-2);\n        t = lua_type(lua,-1);\n        if (t == LUA_TSTRING) {\n            addReplyErrorFormat(c,\"-%s\",lua_tostring(lua,-1));\n            lua_pop(lua,2);\n            return;\n        }\n        lua_pop(lua,1); /* Discard field name pushed before. */\n\n        /* Handle status reply. */\n        lua_pushstring(lua,\"ok\");\n        lua_gettable(lua,-2);\n        t = lua_type(lua,-1);\n        if (t == LUA_TSTRING) {\n            sds ok = sdsnew(lua_tostring(lua,-1));\n            sdsmapchars(ok,\"\\r\\n\",\"  \",2);\n            addReplySds(c,sdscatprintf(sdsempty(),\"+%s\\r\\n\",ok));\n            sdsfree(ok);\n            lua_pop(lua,2);\n            return;\n        }\n        lua_pop(lua,1); /* Discard field name pushed before. */\n\n        /* Handle double reply. */\n        lua_pushstring(lua,\"double\");\n        lua_gettable(lua,-2);\n        t = lua_type(lua,-1);\n        if (t == LUA_TNUMBER) {\n            addReplyDouble(c,lua_tonumber(lua,-1));\n            lua_pop(lua,2);\n            return;\n        }\n        lua_pop(lua,1); /* Discard field name pushed before. */\n\n        /* Handle map reply. */\n        lua_pushstring(lua,\"map\");\n        lua_gettable(lua,-2);\n        t = lua_type(lua,-1);\n        if (t == LUA_TTABLE) {\n            int maplen = 0;\n            void *replylen = addReplyDeferredLen(c);\n            /* we took care of the stack size on function start */\n            lua_pushnil(lua); /* Use nil to start iteration. */\n            while (lua_next(lua,-2)) {\n                /* Stack now: table, key, value */\n                lua_pushvalue(lua,-2);        /* Dup key before consuming. */\n                luaReplyToRedisReply(c, lua); /* Return key. */\n                luaReplyToRedisReply(c, lua); /* Return value. */\n                /* Stack now: table, key. */\n                maplen++;\n            }\n            setDeferredMapLen(c,replylen,maplen);\n            lua_pop(lua,2);\n            return;\n        }\n        lua_pop(lua,1); /* Discard field name pushed before. */\n\n        /* Handle set reply. */\n        lua_pushstring(lua,\"set\");\n        lua_gettable(lua,-2);\n        t = lua_type(lua,-1);\n        if (t == LUA_TTABLE) {\n            int setlen = 0;\n            void *replylen = addReplyDeferredLen(c);\n            /* we took care of the stack size on function start */\n            lua_pushnil(lua); /* Use nil to start iteration. */\n            while (lua_next(lua,-2)) {\n                /* Stack now: table, key, true */\n                lua_pop(lua,1);               /* Discard the boolean value. */\n                lua_pushvalue(lua,-1);        /* Dup key before consuming. */\n                luaReplyToRedisReply(c, lua); /* Return key. */\n                /* Stack now: table, key. */\n                setlen++;\n            }\n            setDeferredSetLen(c,replylen,setlen);\n            lua_pop(lua,2);\n            return;\n        }\n        lua_pop(lua,1); /* Discard field name pushed before. */\n\n        /* Handle the array reply. */\n        {\n        void *replylen = addReplyDeferredLen(c);\n        int j = 1, mbulklen = 0;\n        while(1) {\n            /* we took care of the stack size on function start */\n            lua_pushnumber(lua,j++);\n            lua_gettable(lua,-2);\n            t = lua_type(lua,-1);\n            if (t == LUA_TNIL) {\n                lua_pop(lua,1);\n                break;\n            }\n            luaReplyToRedisReply(c, lua);\n            mbulklen++;\n        }\n        setDeferredArrayLen(c,replylen,mbulklen);\n        }\n        break;\n    default:\n        addReplyNull(c);\n    }\n    lua_pop(lua,1);\n}\n\n/* ---------------------------------------------------------------------------\n * Lua redis.* functions implementations.\n * ------------------------------------------------------------------------- */\n\n#define LUA_CMD_OBJCACHE_SIZE 32\n#define LUA_CMD_OBJCACHE_MAX_LEN 64\nint luaRedisGenericCommand(lua_State *lua, int raise_error) {\n    int j, argc = lua_gettop(lua);\n    int call_flags = CMD_CALL_SLOWLOG | CMD_CALL_STATS;\n    struct redisCommand *cmd;\n    client *c = serverTL->lua_client;\n    sds reply;\n    \n    // Ensure our client is on the right thread\n    serverAssert(!(c->flags & CLIENT_PENDING_WRITE));\n    serverAssert(!(c->flags & CLIENT_UNBLOCKED));\n    serverAssert(GlobalLocksAcquired());\n    c->iel = serverTL - g_pserver->rgthreadvar;\n\n    /* Cached across calls. */\n    static robj **argv = NULL;\n    static int argv_size = 0;\n    static robj *cached_objects[LUA_CMD_OBJCACHE_SIZE];\n    static size_t cached_objects_len[LUA_CMD_OBJCACHE_SIZE];\n    static int inuse = 0;   /* Recursive calls detection. */\n\n    /* By using Lua debug hooks it is possible to trigger a recursive call\n     * to luaRedisGenericCommand(), which normally should never happen.\n     * To make this function reentrant is futile and makes it slower, but\n     * we should at least detect such a misuse, and abort. */\n    if (inuse) {\n        const char *recursion_warning =\n            \"luaRedisGenericCommand() recursive call detected. \"\n            \"Are you doing funny stuff with Lua debug hooks?\";\n        serverLog(LL_WARNING,\"%s\",recursion_warning);\n        luaPushError(lua,recursion_warning);\n        return 1;\n    }\n    inuse++;\n    std::unique_lock<decltype(c->lock)> ulock(c->lock);\n\n{ // Begin GOTO protected variables\n    /* Require at least one argument */\n    if (argc == 0) {\n        luaPushError(lua,\n            \"Please specify at least one argument for redis.call()\");\n        inuse--;\n        return raise_error ? luaRaiseError(lua) : 1;\n    }\n\n    /* Build the arguments vector */\n    if (argv_size < argc) {\n        argv = (robj**)zrealloc(argv,sizeof(robj*)*argc, MALLOC_LOCAL);\n        argv_size = argc;\n    }\n\n    for (j = 0; j < argc; j++) {\n        char *obj_s;\n        size_t obj_len;\n        char dbuf[64];\n\n        if (lua_type(lua,j+1) == LUA_TNUMBER) {\n            /* We can't use lua_tolstring() for number -> string conversion\n             * since Lua uses a format specifier that loses precision. */\n            lua_Number num = lua_tonumber(lua,j+1);\n\n            obj_len = snprintf(dbuf,sizeof(dbuf),\"%.17g\",(double)num);\n            obj_s = dbuf;\n        } else {\n            obj_s = (char*)lua_tolstring(lua,j+1,&obj_len);\n            if (obj_s == NULL) break; /* Not a string. */\n        }\n\n        /* Try to use a cached object. */\n        if (j < LUA_CMD_OBJCACHE_SIZE && cached_objects[j] &&\n            cached_objects_len[j] >= obj_len)\n        {\n            sds s = (sds)ptrFromObj(cached_objects[j]);\n            argv[j] = cached_objects[j];\n            cached_objects[j] = NULL;\n            memcpy(s,obj_s,obj_len+1);\n            sdssetlen(s, obj_len);\n        } else {\n            argv[j] = createStringObject(obj_s, obj_len);\n        }\n    }\n\n    /* Check if one of the arguments passed by the Lua script\n     * is not a string or an integer (lua_isstring() return true for\n     * integers as well). */\n    if (j != argc) {\n        j--;\n        while (j >= 0) {\n            decrRefCount(argv[j]);\n            j--;\n        }\n        luaPushError(lua,\n            \"Lua redis() command arguments must be strings or integers\");\n        inuse--;\n        return raise_error ? luaRaiseError(lua) : 1;\n    }\n\n    /* Setup our fake client for command execution */\n    c->argv = argv;\n    c->argc = argc;\n    c->user = g_pserver->lua_caller->user;\n\n    /* Process module hooks */\n    moduleCallCommandFilters(c);\n    argv = c->argv;\n    argc = c->argc;\n\n    /* Log the command if debugging is active. */\n    if (ldb.active && ldb.step) {\n        sds cmdlog = sdsnew(\"<redis>\");\n        for (j = 0; j < c->argc; j++) {\n            if (j == 10) {\n                cmdlog = sdscatprintf(cmdlog,\" ... (%d more)\",\n                    c->argc-j-1);\n                break;\n            } else {\n                cmdlog = sdscatlen(cmdlog,\" \",1);\n                cmdlog = sdscatsds(cmdlog,(sds)ptrFromObj(c->argv[j]));\n            }\n        }\n        ldbLog(cmdlog);\n    }\n\n    /* Command lookup */\n    cmd = lookupCommand((sds)ptrFromObj(argv[0]));\n    if (!cmd || ((cmd->arity > 0 && cmd->arity != argc) ||\n                   (argc < -cmd->arity)))\n    {\n        if (cmd)\n            luaPushError(lua,\n                \"Wrong number of args calling Redis command From Lua script\");\n        else\n            luaPushError(lua,\"Unknown Redis command called from Lua script\");\n        goto cleanup;\n    }\n    c->cmd = c->lastcmd = cmd;\n\n    /* There are commands that are not allowed inside scripts. */\n    if (cmd->flags & CMD_NOSCRIPT) {\n        luaPushError(lua, \"This Redis command is not allowed from scripts\");\n        goto cleanup;\n    }\n\n    /* Check the ACLs. */\n    int acl_errpos;\n    int acl_retval = ACLCheckAllPerm(c,&acl_errpos);\n    if (acl_retval != ACL_OK) {\n        addACLLogEntry(c,acl_retval,acl_errpos,NULL);\n        switch (acl_retval) {\n        case ACL_DENIED_CMD:\n            luaPushError(lua, \"The user executing the script can't run this \"\n                              \"command or subcommand\");\n            break;\n        case ACL_DENIED_KEY:\n            luaPushError(lua, \"The user executing the script can't access \"\n                              \"at least one of the keys mentioned in the \"\n                              \"command arguments\");\n            break;\n        case ACL_DENIED_CHANNEL:\n            luaPushError(lua, \"The user executing the script can't publish \"\n                              \"to the channel mentioned in the command\");\n            break;\n        default:\n            luaPushError(lua, \"The user executing the script is lacking the \"\n                              \"permissions for the command\");\n            break;\n        }\n        goto cleanup;\n    }\n\n    /* Write commands are forbidden against read-only slaves, or if a\n     * command marked as non-deterministic was already called in the context\n     * of this script. */\n    if (cmd->flags & CMD_WRITE) {\n        int deny_write_type = writeCommandsDeniedByDiskError();\n        if (g_pserver->lua_random_dirty && !g_pserver->lua_replicate_commands) {\n            luaPushError(lua,\n                \"Write commands not allowed after non deterministic commands. Call redis.replicate_commands() at the start of your script in order to switch to single commands replication mode.\");\n            goto cleanup;\n        } else if (listLength(g_pserver->masters) && g_pserver->repl_slave_ro &&\n                   !g_pserver->loading &&\n                   !(g_pserver->lua_caller->flags & CLIENT_MASTER))\n        {\n            luaPushError(lua, (char*)ptrFromObj(shared.roslaveerr));\n            goto cleanup;\n        } else if (deny_write_type != DISK_ERROR_TYPE_NONE) {\n            if (deny_write_type == DISK_ERROR_TYPE_RDB) {\n                luaPushError(lua, (char*)ptrFromObj(shared.bgsaveerr));\n            } else {\n                sds aof_write_err = sdscatfmt(sdsempty(),\n                    \"-MISCONF Errors writing to the AOF file: %s\\r\\n\",\n                    strerror(g_pserver->aof_last_write_errno));\n                luaPushError(lua, aof_write_err);\n                sdsfree(aof_write_err);\n            }\n            goto cleanup;\n        }\n    }\n\n    /* If we reached the memory limit configured via maxmemory, commands that\n     * could enlarge the memory usage are not allowed, but only if this is the\n     * first write in the context of this script, otherwise we can't stop\n     * in the middle. */\n    if (g_pserver->maxmemory &&             /* Maxmemory is actually enabled. */\n        !g_pserver->loading &&              /* Don't care about mem if loading. */\n        !listLength(g_pserver->masters) &&           /* Slave must execute the script. */\n        g_pserver->lua_write_dirty == 0 &&  /* Script had no side effects so far. */\n        g_pserver->lua_oom &&               /* Detected OOM when script started. */\n        (cmd->flags & CMD_DENYOOM))\n    {\n        luaPushError(lua, (char*)ptrFromObj(shared.oomerr));\n        goto cleanup;\n    }\n\n    if (cmd->flags & CMD_RANDOM) g_pserver->lua_random_dirty = 1;\n    if (cmd->flags & CMD_WRITE) g_pserver->lua_write_dirty = 1;\n\n    /* If this is a Redis Cluster node, we need to make sure Lua is not\n     * trying to access non-local keys, with the exception of commands\n     * received from our master or when loading the AOF back in memory. */\n    if (g_pserver->cluster_enabled && !g_pserver->loading &&\n        !(g_pserver->lua_caller->flags & CLIENT_MASTER))\n    {\n        int error_code;\n        /* Duplicate relevant flags in the lua client. */\n        c->flags &= ~(CLIENT_READONLY|CLIENT_ASKING);\n        c->flags |= g_pserver->lua_caller->flags & (CLIENT_READONLY|CLIENT_ASKING);\n        if (getNodeByQuery(c,c->cmd,c->argv,c->argc,NULL,&error_code) !=\n                           g_pserver->cluster->myself)\n        {\n            if (error_code == CLUSTER_REDIR_DOWN_RO_STATE) {\n                luaPushError(lua,\n                    \"Lua script attempted to execute a write command while the \"\n                    \"cluster is down and readonly\");\n            } else if (error_code == CLUSTER_REDIR_DOWN_STATE) {\n                luaPushError(lua,\n                    \"Lua script attempted to execute a command while the \"\n                    \"cluster is down\");\n            } else {\n                luaPushError(lua,\n                    \"Lua script attempted to access a non local key in a \"\n                    \"cluster node\");\n            }\n\n            goto cleanup;\n        }\n    }\n\n    /* If we are using single commands replication, we need to wrap what\n     * we propagate into a MULTI/EXEC block, so that it will be atomic like\n     * a Lua script in the context of AOF and slaves. */\n    if (g_pserver->lua_replicate_commands &&\n        !g_pserver->lua_multi_emitted &&\n        !(g_pserver->lua_caller->flags & CLIENT_MULTI) &&\n        g_pserver->lua_write_dirty &&\n        g_pserver->lua_repl != PROPAGATE_NONE)\n    {\n        execCommandPropagateMulti(g_pserver->lua_caller->db->id);\n        g_pserver->lua_multi_emitted = 1;\n        /* Now we are in the MULTI context, the lua_client should be\n         * flag as CLIENT_MULTI. */\n        c->flags |= CLIENT_MULTI;\n    }\n\n    /* Run the command */\n    if (g_pserver->lua_replicate_commands) {\n        /* Set flags according to redis.set_repl() settings. */\n        if (g_pserver->lua_repl & PROPAGATE_AOF)\n            call_flags |= CMD_CALL_PROPAGATE_AOF;\n        if (g_pserver->lua_repl & PROPAGATE_REPL)\n            call_flags |= CMD_CALL_PROPAGATE_REPL;\n    }\n    call(c,call_flags);\n    serverAssert((c->flags & CLIENT_BLOCKED) == 0);\n\n    /* Convert the result of the Redis command into a suitable Lua type.\n     * The first thing we need is to create a single string from the client\n     * output buffers. */\n    if (listLength(c->reply) == 0 && c->bufpos < PROTO_REPLY_CHUNK_BYTES) {\n        /* This is a fast path for the common case of a reply inside the\n         * client static buffer. Don't create an SDS string but just use\n         * the client buffer directly. */\n        c->buf[c->bufpos] = '\\0';\n        reply = c->buf;\n        c->bufpos = 0;\n    } else {\n        reply = sdsnewlen(c->buf,c->bufpos);\n        c->bufpos = 0;\n        while(listLength(c->reply)) {\n            clientReplyBlock *o = (clientReplyBlock*)listNodeValue(listFirst(c->reply));\n\n            reply = sdscatlen(reply,o->buf(),o->used);\n            listDelNode(c->reply,listFirst(c->reply));\n        }\n    }\n    if (raise_error && reply[0] != '-') raise_error = 0;\n    redisProtocolToLuaType(lua,reply);\n\n    /* If the debugger is active, log the reply from Redis. */\n    if (ldb.active && ldb.step)\n        ldbLogRedisReply(reply);\n\n    /* Sort the output array if needed, assuming it is a non-null multi bulk\n     * reply as expected. */\n    if ((cmd->flags & CMD_SORT_FOR_SCRIPT) &&\n        (g_pserver->lua_replicate_commands == 0) &&\n        (reply[0] == '*' && reply[1] != '-')) {\n            luaSortArray(lua);\n    }\n    if (reply != c->buf) sdsfree(reply);\n    c->reply_bytes = 0;\n} // END Goto Protected Variables\n\ncleanup:\n    /* Clean up. Command code may have changed argv/argc so we use the\n     * argv/argc of the client instead of the local variables. */\n    for (j = 0; j < c->argc; j++) {\n        robj *o = c->argv[j];\n\n        /* Try to cache the object in the cached_objects array.\n         * The object must be small, SDS-encoded, and with refcount = 1\n         * (we must be the only owner) for us to cache it. */\n        if (j < LUA_CMD_OBJCACHE_SIZE &&\n            o->getrefcount(std::memory_order_relaxed) == 1 &&\n            (o->encoding == OBJ_ENCODING_RAW ||\n             o->encoding == OBJ_ENCODING_EMBSTR) &&\n            sdslen((sds)ptrFromObj(o)) <= LUA_CMD_OBJCACHE_MAX_LEN)\n        {\n            sds s = (sds)ptrFromObj(o);\n            if (cached_objects[j]) decrRefCount(cached_objects[j]);\n            cached_objects[j] = o;\n            cached_objects_len[j] = sdsalloc(s);\n        } else {\n            decrRefCount(o);\n        }\n    }\n\n    if (c->argv != argv) {\n        zfree(c->argv);\n        argv = NULL;\n        argv_size = 0;\n    }\n\n    c->user = NULL;\n\n    if (raise_error) {\n        /* If we are here we should have an error in the stack, in the\n         * form of a table with an \"err\" field. Extract the string to\n         * return the plain error. */\n        inuse--;\n        return luaRaiseError(lua);\n    }\n    inuse--;\n    return 1;\n}\n\n/* redis.call() */\nint luaRedisCallCommand(lua_State *lua) {\n    return luaRedisGenericCommand(lua,1);\n}\n\n/* redis.pcall() */\nint luaRedisPCallCommand(lua_State *lua) {\n    return luaRedisGenericCommand(lua,0);\n}\n\n/* This adds redis.sha1hex(string) to Lua scripts using the same hashing\n * function used for sha1ing lua scripts. */\nint luaRedisSha1hexCommand(lua_State *lua) {\n    int argc = lua_gettop(lua);\n    char digest[41];\n    size_t len;\n    char *s;\n\n    if (argc != 1) {\n        lua_pushstring(lua, \"wrong number of arguments\");\n        return lua_error(lua);\n    }\n\n    s = (char*)lua_tolstring(lua,1,&len);\n    sha1hex(digest,s,len);\n    lua_pushstring(lua,digest);\n    return 1;\n}\n\n/* Returns a table with a single field 'field' set to the string value\n * passed as argument. This helper function is handy when returning\n * a Redis Protocol error or status reply from Lua:\n *\n * return redis.error_reply(\"ERR Some Error\")\n * return redis.status_reply(\"ERR Some Error\")\n */\nint luaRedisReturnSingleFieldTable(lua_State *lua, const char *field) {\n    if (lua_gettop(lua) != 1 || lua_type(lua,-1) != LUA_TSTRING) {\n        luaPushError(lua, \"wrong number or type of arguments\");\n        return 1;\n    }\n\n    lua_newtable(lua);\n    lua_pushstring(lua, field);\n    lua_pushvalue(lua, -3);\n    lua_settable(lua, -3);\n    return 1;\n}\n\n/* redis.error_reply() */\nint luaRedisErrorReplyCommand(lua_State *lua) {\n    return luaRedisReturnSingleFieldTable(lua,\"err\");\n}\n\n/* redis.status_reply() */\nint luaRedisStatusReplyCommand(lua_State *lua) {\n    return luaRedisReturnSingleFieldTable(lua,\"ok\");\n}\n\n/* redis.replicate_commands()\n *\n * Turn on single commands replication if the script never called\n * a write command so far, and returns true. Otherwise if the script\n * already started to write, returns false and stick to whole scripts\n * replication, which is our default. */\nint luaRedisReplicateCommandsCommand(lua_State *lua) {\n    if (g_pserver->lua_write_dirty) {\n        lua_pushboolean(lua,0);\n    } else {\n        g_pserver->lua_replicate_commands = 1;\n        /* When we switch to single commands replication, we can provide\n         * different math.random() sequences at every call, which is what\n         * the user normally expects. */\n        redisSrand48(rand());\n        lua_pushboolean(lua,1);\n    }\n    return 1;\n}\n\n/* redis.breakpoint()\n *\n * Allows to stop execution during a debugging session from within\n * the Lua code implementation, like if a breakpoint was set in the code\n * immediately after the function. */\nint luaRedisBreakpointCommand(lua_State *lua) {\n    if (ldb.active) {\n        ldb.luabp = 1;\n        lua_pushboolean(lua,1);\n    } else {\n        lua_pushboolean(lua,0);\n    }\n    return 1;\n}\n\n/* redis.debug()\n *\n * Log a string message into the output console.\n * Can take multiple arguments that will be separated by commas.\n * Nothing is returned to the caller. */\nint luaRedisDebugCommand(lua_State *lua) {\n    if (!ldb.active) return 0;\n    int argc = lua_gettop(lua);\n    sds log = sdscatprintf(sdsempty(),\"<debug> line %d: \", ldb.currentline);\n    while(argc--) {\n        log = ldbCatStackValue(log,lua,-1 - argc);\n        if (argc != 0) log = sdscatlen(log,\", \",2);\n    }\n    ldbLog(log);\n    return 0;\n}\n\n/* redis.set_repl()\n *\n * Set the propagation of write commands executed in the context of the\n * script to on/off for AOF and slaves. */\nint luaRedisSetReplCommand(lua_State *lua) {\n    int argc = lua_gettop(lua);\n    int flags;\n\n    if (g_pserver->lua_replicate_commands == 0) {\n        lua_pushstring(lua, \"You can set the replication behavior only after turning on single commands replication with redis.replicate_commands().\");\n        return lua_error(lua);\n    } else if (argc != 1) {\n        lua_pushstring(lua, \"redis.set_repl() requires two arguments.\");\n        return lua_error(lua);\n    }\n\n    flags = lua_tonumber(lua,-1);\n    if ((flags & ~(PROPAGATE_AOF|PROPAGATE_REPL)) != 0) {\n        lua_pushstring(lua, \"Invalid replication flags. Use REPL_AOF, REPL_REPLICA, REPL_ALL or REPL_NONE.\");\n        return lua_error(lua);\n    }\n    g_pserver->lua_repl = flags;\n    return 0;\n}\n\n/* redis.log() */\nint luaLogCommand(lua_State *lua) {\n    int j, argc = lua_gettop(lua);\n    int level;\n    sds log;\n\n    if (argc < 2) {\n        lua_pushstring(lua, \"redis.log() requires two arguments or more.\");\n        return lua_error(lua);\n    } else if (!lua_isnumber(lua,-argc)) {\n        lua_pushstring(lua, \"First argument must be a number (log level).\");\n        return lua_error(lua);\n    }\n    level = lua_tonumber(lua,-argc);\n    if (level < LL_DEBUG || level > LL_WARNING) {\n        lua_pushstring(lua, \"Invalid debug level.\");\n        return lua_error(lua);\n    }\n    if (level < cserver.verbosity) return 0;\n\n    /* Glue together all the arguments */\n    log = sdsempty();\n    for (j = 1; j < argc; j++) {\n        size_t len;\n        char *s;\n\n        s = (char*)lua_tolstring(lua,(-argc)+j,&len);\n        if (s) {\n            if (j != 1) log = sdscatlen(log,\" \",1);\n            log = sdscatlen(log,s,len);\n        }\n    }\n    serverLogRaw(level,log);\n    sdsfree(log);\n    return 0;\n}\n\n/* redis.setresp() */\nint luaSetResp(lua_State *lua) {\n    int argc = lua_gettop(lua);\n\n    if (argc != 1) {\n        lua_pushstring(lua, \"redis.setresp() requires one argument.\");\n        return lua_error(lua);\n    }\n\n    int resp = lua_tonumber(lua,-argc);\n    if (resp != 2 && resp != 3) {\n        lua_pushstring(lua, \"RESP version must be 2 or 3.\");\n        return lua_error(lua);\n    }\n\n    serverTL->lua_client->resp = resp;\n    return 0;\n}\n\n/* ---------------------------------------------------------------------------\n * Lua engine initialization and reset.\n * ------------------------------------------------------------------------- */\n\nvoid luaLoadLib(lua_State *lua, const char *libname, lua_CFunction luafunc) {\n  lua_pushcfunction(lua, luafunc);\n  lua_pushstring(lua, libname);\n  lua_call(lua, 1, 0);\n}\n\nextern \"C\" {\nLUALIB_API int (luaopen_cjson) (lua_State *L);\nLUALIB_API int (luaopen_struct) (lua_State *L);\nLUALIB_API int (luaopen_cmsgpack) (lua_State *L);\nLUALIB_API int (luaopen_bit) (lua_State *L);\n}\n\nvoid luaLoadLibraries(lua_State *lua) {\n    luaLoadLib(lua, \"\", luaopen_base);\n    luaLoadLib(lua, LUA_TABLIBNAME, luaopen_table);\n    luaLoadLib(lua, LUA_STRLIBNAME, luaopen_string);\n    luaLoadLib(lua, LUA_MATHLIBNAME, luaopen_math);\n    luaLoadLib(lua, LUA_DBLIBNAME, luaopen_debug);\n    luaLoadLib(lua, \"cjson\", luaopen_cjson);\n    luaLoadLib(lua, \"struct\", luaopen_struct);\n    luaLoadLib(lua, \"cmsgpack\", luaopen_cmsgpack);\n    luaLoadLib(lua, \"bit\", luaopen_bit);\n\n#if 0 /* Stuff that we don't load currently, for sandboxing concerns. */\n    luaLoadLib(lua, LUA_LOADLIBNAME, luaopen_package);\n    luaLoadLib(lua, LUA_OSLIBNAME, luaopen_os);\n#endif\n}\n\n/* Remove a functions that we don't want to expose to the Redis scripting\n * environment. */\nvoid luaRemoveUnsupportedFunctions(lua_State *lua) {\n    lua_pushnil(lua);\n    lua_setglobal(lua,\"loadfile\");\n    lua_pushnil(lua);\n    lua_setglobal(lua,\"dofile\");\n}\n\n/* This function installs metamethods in the global table _G that prevent\n * the creation of globals accidentally.\n *\n * It should be the last to be called in the scripting engine initialization\n * sequence, because it may interact with creation of globals. */\nvoid scriptingEnableGlobalsProtection(lua_State *lua) {\n    const char *s[32];\n    sds code = sdsempty();\n    int j = 0;\n\n    /* strict.lua from: http://metalua.luaforge.net/src/lib/strict.lua.html.\n     * Modified to be adapted to Redis. */\n    s[j++]=\"local dbg=debug\\n\";\n    s[j++]=\"local mt = {}\\n\";\n    s[j++]=\"setmetatable(_G, mt)\\n\";\n    s[j++]=\"mt.__newindex = function (t, n, v)\\n\";\n    s[j++]=\"  if dbg.getinfo(2) then\\n\";\n    s[j++]=\"    local w = dbg.getinfo(2, \\\"S\\\").what\\n\";\n    s[j++]=\"    if w ~= \\\"main\\\" and w ~= \\\"C\\\" then\\n\";\n    s[j++]=\"      error(\\\"Script attempted to create global variable '\\\"..tostring(n)..\\\"'\\\", 2)\\n\";\n    s[j++]=\"    end\\n\";\n    s[j++]=\"  end\\n\";\n    s[j++]=\"  rawset(t, n, v)\\n\";\n    s[j++]=\"end\\n\";\n    s[j++]=\"mt.__index = function (t, n)\\n\";\n    s[j++]=\"  if dbg.getinfo(2) and dbg.getinfo(2, \\\"S\\\").what ~= \\\"C\\\" then\\n\";\n    s[j++]=\"    error(\\\"Script attempted to access nonexistent global variable '\\\"..tostring(n)..\\\"'\\\", 2)\\n\";\n    s[j++]=\"  end\\n\";\n    s[j++]=\"  return rawget(t, n)\\n\";\n    s[j++]=\"end\\n\";\n    s[j++]=\"debug = nil\\n\";\n    s[j++]=NULL;\n\n    for (j = 0; s[j] != NULL; j++) code = sdscatlen(code,s[j],strlen(s[j]));\n    luaL_loadbuffer(lua,code,sdslen(code),\"@enable_strict_lua\");\n    lua_pcall(lua,0,0,0);\n    sdsfree(code);\n}\n\n/* Initialize the scripting environment.\n *\n * This function is called the first time at server startup with\n * the 'setup' argument set to 1.\n *\n * It can be called again multiple times during the lifetime of the Redis\n * process, with 'setup' set to 0, and following a scriptingRelease() call,\n * in order to reset the Lua scripting environment.\n *\n * However it is simpler to just call scriptingReset() that does just that. */\nvoid scriptingInit(int setup) {\n    lua_State *lua = lua_open();\n\n    if (setup) {\n        for (int iel = 0; iel < cserver.cthreads; ++iel)\n        {\n            g_pserver->rgthreadvar[iel].lua_client = createClient(nullptr, iel);\n            g_pserver->rgthreadvar[iel].lua_client->flags |= CLIENT_LUA;\n            /* We do not want to allow blocking commands inside Lua */\n            g_pserver->rgthreadvar[iel].lua_client->flags |= CLIENT_DENY_BLOCKING;\n        }\n        g_pserver->lua_timedout = 0;\n        g_pserver->lua_caller = NULL;\n        g_pserver->lua_cur_script = NULL;\n        ldbInit();\n    }\n\n    luaLoadLibraries(lua);\n    luaRemoveUnsupportedFunctions(lua);\n\n    /* Initialize a dictionary we use to map SHAs to scripts.\n     * This is useful for replication, as we need to replicate EVALSHA\n     * as EVAL, so we need to remember the associated script. */\n    g_pserver->lua_scripts = dictCreate(&shaScriptObjectDictType,NULL);\n    g_pserver->lua_scripts_mem = 0;\n\n    /* Register the redis commands table and fields */\n    lua_newtable(lua);\n\n    /* redis.call */\n    lua_pushstring(lua,\"call\");\n    lua_pushcfunction(lua,luaRedisCallCommand);\n    lua_settable(lua,-3);\n\n    /* redis.pcall */\n    lua_pushstring(lua,\"pcall\");\n    lua_pushcfunction(lua,luaRedisPCallCommand);\n    lua_settable(lua,-3);\n\n    /* redis.log and log levels. */\n    lua_pushstring(lua,\"log\");\n    lua_pushcfunction(lua,luaLogCommand);\n    lua_settable(lua,-3);\n\n    /* redis.setresp */\n    lua_pushstring(lua,\"setresp\");\n    lua_pushcfunction(lua,luaSetResp);\n    lua_settable(lua,-3);\n\n    lua_pushstring(lua,\"LOG_DEBUG\");\n    lua_pushnumber(lua,LL_DEBUG);\n    lua_settable(lua,-3);\n\n    lua_pushstring(lua,\"LOG_VERBOSE\");\n    lua_pushnumber(lua,LL_VERBOSE);\n    lua_settable(lua,-3);\n\n    lua_pushstring(lua,\"LOG_NOTICE\");\n    lua_pushnumber(lua,LL_NOTICE);\n    lua_settable(lua,-3);\n\n    lua_pushstring(lua,\"LOG_WARNING\");\n    lua_pushnumber(lua,LL_WARNING);\n    lua_settable(lua,-3);\n\n    /* redis.sha1hex */\n    lua_pushstring(lua, \"sha1hex\");\n    lua_pushcfunction(lua, luaRedisSha1hexCommand);\n    lua_settable(lua, -3);\n\n    /* redis.error_reply and redis.status_reply */\n    lua_pushstring(lua, \"error_reply\");\n    lua_pushcfunction(lua, luaRedisErrorReplyCommand);\n    lua_settable(lua, -3);\n    lua_pushstring(lua, \"status_reply\");\n    lua_pushcfunction(lua, luaRedisStatusReplyCommand);\n    lua_settable(lua, -3);\n\n    /* redis.replicate_commands */\n    lua_pushstring(lua, \"replicate_commands\");\n    lua_pushcfunction(lua, luaRedisReplicateCommandsCommand);\n    lua_settable(lua, -3);\n\n    /* redis.set_repl and associated flags. */\n    lua_pushstring(lua,\"set_repl\");\n    lua_pushcfunction(lua,luaRedisSetReplCommand);\n    lua_settable(lua,-3);\n\n    lua_pushstring(lua,\"REPL_NONE\");\n    lua_pushnumber(lua,PROPAGATE_NONE);\n    lua_settable(lua,-3);\n\n    lua_pushstring(lua,\"REPL_AOF\");\n    lua_pushnumber(lua,PROPAGATE_AOF);\n    lua_settable(lua,-3);\n\n    lua_pushstring(lua,\"REPL_SLAVE\");\n    lua_pushnumber(lua,PROPAGATE_REPL);\n    lua_settable(lua,-3);\n\n    lua_pushstring(lua,\"REPL_REPLICA\");\n    lua_pushnumber(lua,PROPAGATE_REPL);\n    lua_settable(lua,-3);\n\n    lua_pushstring(lua,\"REPL_ALL\");\n    lua_pushnumber(lua,PROPAGATE_AOF|PROPAGATE_REPL);\n    lua_settable(lua,-3);\n\n    /* redis.breakpoint */\n    lua_pushstring(lua,\"breakpoint\");\n    lua_pushcfunction(lua,luaRedisBreakpointCommand);\n    lua_settable(lua,-3);\n\n    /* redis.debug */\n    lua_pushstring(lua,\"debug\");\n    lua_pushcfunction(lua,luaRedisDebugCommand);\n    lua_settable(lua,-3);\n\n    /* Finally set the table as 'redis' global var. */\n    lua_setglobal(lua,\"redis\");\n    /* Set table as 'keydb' global var as well */\n    lua_getglobal(lua,\"redis\");\n    lua_setglobal(lua,\"keydb\");\n\n    /* Replace math.random and math.randomseed with our implementations. */\n    lua_getglobal(lua,\"math\");\n\n    lua_pushstring(lua,\"random\");\n    lua_pushcfunction(lua,redis_math_random);\n    lua_settable(lua,-3);\n\n    lua_pushstring(lua,\"randomseed\");\n    lua_pushcfunction(lua,redis_math_randomseed);\n    lua_settable(lua,-3);\n\n    lua_setglobal(lua,\"math\");\n\n    /* Add a helper function that we use to sort the multi bulk output of non\n     * deterministic commands, when containing 'false' elements. */\n    {\n        const char *compare_func =    \"function __redis__compare_helper(a,b)\\n\"\n                                \"  if a == false then a = '' end\\n\"\n                                \"  if b == false then b = '' end\\n\"\n                                \"  return a<b\\n\"\n                                \"end\\n\";\n        luaL_loadbuffer(lua,compare_func,strlen(compare_func),\"@cmp_func_def\");\n        lua_pcall(lua,0,0,0);\n    }\n\n    /* Add a helper function we use for pcall error reporting.\n     * Note that when the error is in the C function we want to report the\n     * information about the caller, that's what makes sense from the point\n     * of view of the user debugging a script. */\n    {\n        const char *errh_func =       \"local dbg = debug\\n\"\n                                \"function __redis__err__handler(err)\\n\"\n                                \"  local i = dbg.getinfo(2,'nSl')\\n\"\n                                \"  if i and i.what == 'C' then\\n\"\n                                \"    i = dbg.getinfo(3,'nSl')\\n\"\n                                \"  end\\n\"\n                                \"  if i then\\n\"\n                                \"    return i.source .. ':' .. i.currentline .. ': ' .. err\\n\"\n                                \"  else\\n\"\n                                \"    return err\\n\"\n                                \"  end\\n\"\n                                \"end\\n\";\n        luaL_loadbuffer(lua,errh_func,strlen(errh_func),\"@err_handler_def\");\n        lua_pcall(lua,0,0,0);\n    }\n\n    /* Lua beginners often don't use \"local\", this is likely to introduce\n     * subtle bugs in their code. To prevent problems we protect accesses\n     * to global variables. */\n    scriptingEnableGlobalsProtection(lua);\n\n    g_pserver->lua = lua;\n}\n\n/* Release resources related to Lua scripting.\n * This function is used in order to reset the scripting environment. */\nvoid scriptingRelease(int async) {\n    if (async)\n        freeLuaScriptsAsync(g_pserver->lua_scripts);\n    else\n        dictRelease(g_pserver->lua_scripts);\n    g_pserver->lua_scripts_mem = 0;\n    lua_close(g_pserver->lua);\n}\n\nvoid scriptingReset(int async) {\n    scriptingRelease(async);\n    scriptingInit(0);\n}\n\n/* Set an array of Redis String Objects as a Lua array (table) stored into a\n * global variable. */\nvoid luaSetGlobalArray(lua_State *lua, const char *var, robj **elev, int elec) {\n    int j;\n\n    lua_newtable(lua);\n    for (j = 0; j < elec; j++) {\n        lua_pushlstring(lua,(char*)ptrFromObj(elev[j]),sdslen((sds)ptrFromObj(elev[j])));\n        lua_rawseti(lua,-2,j+1);\n    }\n    lua_setglobal(lua,var);\n}\n\n/* ---------------------------------------------------------------------------\n * Redis provided math.random\n * ------------------------------------------------------------------------- */\n\n/* We replace math.random() with our implementation that is not affected\n * by specific libc random() implementations and will output the same sequence\n * (for the same seed) in every arch. */\n\n/* The following implementation is the one shipped with Lua itself but with\n * rand() replaced by redisLrand48(). */\nint redis_math_random (lua_State *L) {\n  /* the `%' avoids the (rare) case of r==1, and is needed also because on\n     some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */\n  lua_Number r = (lua_Number)(redisLrand48()%REDIS_LRAND48_MAX) /\n                                (lua_Number)REDIS_LRAND48_MAX;\n  switch (lua_gettop(L)) {  /* check number of arguments */\n    case 0: {  /* no arguments */\n      lua_pushnumber(L, r);  /* Number between 0 and 1 */\n      break;\n    }\n    case 1: {  /* only upper limit */\n      int u = luaL_checkint(L, 1);\n      luaL_argcheck(L, 1<=u, 1, \"interval is empty\");\n      lua_pushnumber(L, floor(r*u)+1);  /* int between 1 and `u' */\n      break;\n    }\n    case 2: {  /* lower and upper limits */\n      int l = luaL_checkint(L, 1);\n      int u = luaL_checkint(L, 2);\n      luaL_argcheck(L, l<=u, 2, \"interval is empty\");\n      lua_pushnumber(L, floor(r*(u-l+1))+l);  /* int between `l' and `u' */\n      break;\n    }\n    default: return luaL_error(L, \"wrong number of arguments\");\n  }\n  return 1;\n}\n\nint redis_math_randomseed (lua_State *L) {\n  redisSrand48(luaL_checkint(L, 1));\n  return 0;\n}\n\n/* ---------------------------------------------------------------------------\n * EVAL and SCRIPT commands implementation\n * ------------------------------------------------------------------------- */\n\n/* Define a Lua function with the specified body.\n * The function name will be generated in the following form:\n *\n *   f_<hex sha1 sum>\n *\n * The function increments the reference count of the 'body' object as a\n * side effect of a successful call.\n *\n * On success a pointer to an SDS string representing the function SHA1 of the\n * just added function is returned (and will be valid until the next call\n * to scriptingReset() function), otherwise NULL is returned.\n *\n * The function handles the fact of being called with a script that already\n * exists, and in such a case, it behaves like in the success case.\n *\n * If 'c' is not NULL, on error the client is informed with an appropriate\n * error describing the nature of the problem and the Lua interpreter error. */\nsds luaCreateFunction(client *c, lua_State *lua, robj *body) {\n    char funcname[43];\n    dictEntry *de;\n\n    funcname[0] = 'f';\n    funcname[1] = '_';\n    sha1hex(funcname+2,(char*)ptrFromObj(body),sdslen((sds)ptrFromObj(body)));\n\n    sds sha = sdsnewlen(funcname+2,40);\n    if ((de = dictFind(g_pserver->lua_scripts,sha)) != NULL) {\n        sdsfree(sha);\n        return (sds)dictGetKey(de);\n    }\n\n    sds funcdef = sdsempty();\n    funcdef = sdscat(funcdef,\"function \");\n    funcdef = sdscatlen(funcdef,funcname,42);\n    funcdef = sdscatlen(funcdef,\"() \",3);\n    funcdef = sdscatlen(funcdef,ptrFromObj(body),sdslen((sds)ptrFromObj(body)));\n    funcdef = sdscatlen(funcdef,\"\\nend\",4);\n\n    if (luaL_loadbuffer(lua,funcdef,sdslen(funcdef),\"@user_script\")) {\n        if (c != NULL) {\n            addReplyErrorFormat(c,\n                \"Error compiling script (new function): %s\\n\",\n                lua_tostring(lua,-1));\n        }\n        lua_pop(lua,1);\n        sdsfree(sha);\n        sdsfree(funcdef);\n        return NULL;\n    }\n    sdsfree(funcdef);\n\n    if (lua_pcall(lua,0,0,0)) {\n        if (c != NULL) {\n            addReplyErrorFormat(c,\"Error running script (new function): %s\\n\",\n                lua_tostring(lua,-1));\n        }\n        lua_pop(lua,1);\n        sdsfree(sha);\n        return NULL;\n    }\n\n    /* We also save a SHA1 -> Original script map in a dictionary\n     * so that we can replicate / write in the AOF all the\n     * EVALSHA commands as EVAL using the original script. */\n    int retval = dictAdd(g_pserver->lua_scripts,sha,body);\n    serverAssertWithInfo(c ? c : serverTL->lua_client,NULL,retval == DICT_OK);\n    g_pserver->lua_scripts_mem += sdsZmallocSize(sha) + getStringObjectSdsUsedMemory(body);\n    incrRefCount(body);\n    return sha;\n}\n\n/* This is the Lua script \"count\" hook that we use to detect scripts timeout. */\nvoid luaMaskCountHook(lua_State *lua, lua_Debug *ar) {\n    long long elapsed = elapsedMs(g_pserver->lua_time_start);\n    UNUSED(ar);\n    UNUSED(lua);\n\n    /* Set the timeout condition if not already set and the maximum\n     * execution time was reached. */\n    if (elapsed >= g_pserver->lua_time_limit && g_pserver->lua_timedout == 0) {\n        serverLog(LL_WARNING,\n            \"Lua slow script detected: still in execution after %lld milliseconds. \"\n            \"You can try killing the script using the SCRIPT KILL command. \"\n            \"Script SHA1 is: %s\",\n            elapsed, g_pserver->lua_cur_script);\n        g_pserver->lua_timedout = 1;\n        blockingOperationStarts();\n        /* Once the script timeouts we reenter the event loop to permit others\n         * to call SCRIPT KILL or SHUTDOWN NOSAVE if needed. For this reason\n         * we need to mask the client executing the script from the event loop.\n         * If we don't do that the client may disconnect and could no longer be\n         * here when the EVAL command will return. */\n        protectClient(g_pserver->lua_caller);\n    }\n    if (g_pserver->lua_timedout) processEventsWhileBlocked(serverTL - g_pserver->rgthreadvar);\n    if (g_pserver->lua_kill) {\n        serverLog(LL_WARNING,\"Lua script killed by user with SCRIPT KILL.\");\n\n        /*\n         * Set the hook to invoke all the time so the user\n         * will not be able to catch the error with pcall and invoke\n         * pcall again which will prevent the script from ever been killed\n         */\n        lua_sethook(lua, luaMaskCountHook, LUA_MASKLINE, 0);\n\n        lua_pushstring(lua,\"Script killed by user with SCRIPT KILL...\");\n        lua_error(lua);\n    }\n}\n\nvoid prepareLuaClient(void) {\n    /* Select the right DB in the context of the Lua client */\n    selectDb(serverTL->lua_client,g_pserver->lua_caller->db->id);\n    serverTL->lua_client->resp = 2; /* Default is RESP2, scripts can change it. */\n\n    /* If we are in MULTI context, flag Lua client as CLIENT_MULTI. */\n    if (g_pserver->lua_caller->flags & CLIENT_MULTI) {\n        serverTL->lua_client->flags |= CLIENT_MULTI;\n    }\n}\n\nvoid resetLuaClient(void) {\n    /* After the script done, remove the MULTI state. */\n    serverTL->lua_client->flags &= ~CLIENT_MULTI;\n}\n\nvoid evalGenericCommand(client *c, int evalsha) {\n    lua_State *lua = g_pserver->lua;\n    char funcname[43];\n    long long numkeys;\n    long long initial_server_dirty = g_pserver->dirty;\n    int delhook = 0, err;\n\n    if (g_pserver->m_pstorageFactory != nullptr)\n        performEvictions(true);\n\n    /* When we replicate whole scripts, we want the same PRNG sequence at\n     * every call so that our PRNG is not affected by external state. */\n    redisSrand48(0);\n\n    /* We set this flag to zero to remember that so far no random command\n     * was called. This way we can allow the user to call commands like\n     * SRANDMEMBER or RANDOMKEY from Lua scripts as far as no write command\n     * is called (otherwise the replication and AOF would end with non\n     * deterministic sequences).\n     *\n     * Thanks to this flag we'll raise an error every time a write command\n     * is called after a random command was used. */\n    g_pserver->lua_random_dirty = 0;\n    g_pserver->lua_write_dirty = 0;\n    g_pserver->lua_replicate_commands = g_pserver->lua_always_replicate_commands;\n    g_pserver->lua_multi_emitted = 0;\n    g_pserver->lua_repl = PROPAGATE_AOF|PROPAGATE_REPL;\n\n    /* Get the number of arguments that are keys */\n    if (getLongLongFromObjectOrReply(c,c->argv[2],&numkeys,NULL) != C_OK)\n        return;\n    if (numkeys > (c->argc - 3)) {\n        addReplyError(c,\"Number of keys can't be greater than number of args\");\n        return;\n    } else if (numkeys < 0) {\n        addReplyError(c,\"Number of keys can't be negative\");\n        return;\n    }\n\n    /* We obtain the script SHA1, then check if this function is already\n     * defined into the Lua state */\n    funcname[0] = 'f';\n    funcname[1] = '_';\n    if (!evalsha) {\n        /* Hash the code if this is an EVAL call */\n        sha1hex(funcname+2,(char*)ptrFromObj(c->argv[1]),sdslen((sds)ptrFromObj(c->argv[1])));\n    } else {\n        /* We already have the SHA if it is an EVALSHA */\n        int j;\n        char *sha = (char*)ptrFromObj(c->argv[1]);\n\n        /* Convert to lowercase. We don't use tolower since the function\n         * managed to always show up in the profiler output consuming\n         * a non trivial amount of time. */\n        for (j = 0; j < 40; j++)\n            funcname[j+2] = (sha[j] >= 'A' && sha[j] <= 'Z') ?\n                sha[j]+('a'-'A') : sha[j];\n        funcname[42] = '\\0';\n    }\n\n    /* Push the pcall error handler function on the stack. */\n    lua_getglobal(lua, \"__redis__err__handler\");\n\n    /* Try to lookup the Lua function */\n    lua_getglobal(lua, funcname);\n    if (lua_isnil(lua,-1)) {\n        lua_pop(lua,1); /* remove the nil from the stack */\n        /* Function not defined... let's define it if we have the\n         * body of the function. If this is an EVALSHA call we can just\n         * return an error. */\n        if (evalsha) {\n            lua_pop(lua,1); /* remove the error handler from the stack. */\n            addReplyErrorObject(c, shared.noscripterr);\n            return;\n        }\n        if (luaCreateFunction(c,lua,c->argv[1]) == NULL) {\n            lua_pop(lua,1); /* remove the error handler from the stack. */\n            /* The error is sent to the client by luaCreateFunction()\n             * itself when it returns NULL. */\n            return;\n        }\n        /* Now the following is guaranteed to return non nil */\n        lua_getglobal(lua, funcname);\n        serverAssert(!lua_isnil(lua,-1));\n    }\n\n    /* Populate the argv and keys table accordingly to the arguments that\n     * EVAL received. */\n    luaSetGlobalArray(lua,\"KEYS\",c->argv+3,numkeys);\n    luaSetGlobalArray(lua,\"ARGV\",c->argv+3+numkeys,c->argc-3-numkeys);\n\n    /* Set a hook in order to be able to stop the script execution if it\n     * is running for too much time.\n     * We set the hook only if the time limit is enabled as the hook will\n     * make the Lua script execution slower.\n     *\n     * If we are debugging, we set instead a \"line\" hook so that the\n     * debugger is call-back at every line executed by the script. */\n    serverTL->in_eval = 1;\n    g_pserver->lua_caller = c;\n    g_pserver->lua_cur_script = funcname + 2;\n    g_pserver->lua_time_start = getMonotonicUs();\n    g_pserver->lua_time_snapshot = mstime();\n    g_pserver->lua_kill = 0;\n    if (g_pserver->lua_time_limit > 0 && ldb.active == 0) {\n        lua_sethook(lua,luaMaskCountHook,LUA_MASKCOUNT,100000);\n        delhook = 1;\n    } else if (ldb.active) {\n        lua_sethook(g_pserver->lua,luaLdbLineHook,LUA_MASKLINE|LUA_MASKCOUNT,100000);\n        delhook = 1;\n    }\n\n    prepareLuaClient();\n\n    /* At this point whether this script was never seen before or if it was\n     * already defined, we can call it. We have zero arguments and expect\n     * a single return value. */\n    err = lua_pcall(lua,0,1,-2);\n\n    resetLuaClient();\n\n    /* Perform some cleanup that we need to do both on error and success. */\n    if (delhook) lua_sethook(lua,NULL,0,0); /* Disable hook */\n    if (g_pserver->lua_timedout) {\n        g_pserver->lua_timedout = 0;\n        blockingOperationEnds();\n        /* Restore the client that was protected when the script timeout\n         * was detected. */\n        unprotectClient(c);\n        listIter li;\n        listNode *ln;\n        listRewind(g_pserver->masters, &li);\n        while ((ln = listNext(&li)))\n        {\n            struct redisMaster *mi = (struct redisMaster*)listNodeValue(ln);\n            if (mi->master)\n                queueClientForReprocessing(mi->master);\n        }\n    }\n    serverTL->in_eval = 0;\n    g_pserver->lua_caller = NULL;\n    g_pserver->lua_cur_script = NULL;\n\n    /* Call the Lua garbage collector from time to time to avoid a\n     * full cycle performed by Lua, which adds too latency.\n     *\n     * The call is performed every LUA_GC_CYCLE_PERIOD executed commands\n     * (and for LUA_GC_CYCLE_PERIOD collection steps) because calling it\n     * for every command uses too much CPU. */\n    #define LUA_GC_CYCLE_PERIOD 50\n    {\n        static long gc_count = 0;\n\n        gc_count++;\n        if (gc_count == LUA_GC_CYCLE_PERIOD) {\n            lua_gc(lua,LUA_GCSTEP,LUA_GC_CYCLE_PERIOD);\n            gc_count = 0;\n        }\n    }\n\n    if (err) {\n        addReplyErrorFormat(c,\"Error running script (call to %s): %s\\n\",\n            funcname, lua_tostring(lua,-1));\n        lua_pop(lua,2); /* Consume the Lua reply and remove error handler. */\n    } else {\n        /* On success convert the Lua return value into Redis protocol, and\n         * send it to * the client. */\n        luaReplyToRedisReply(c,lua); /* Convert and consume the reply. */\n        lua_pop(lua,1); /* Remove the error handler. */\n    }\n\n    /* If we are using single commands replication, emit EXEC if there\n     * was at least a write. */\n    if (g_pserver->lua_replicate_commands) {\n        preventCommandPropagation(c);\n        if (g_pserver->lua_multi_emitted) {\n            execCommandPropagateExec(c->db->id);\n        }\n    }\n\n    /* EVALSHA should be propagated to Slave and AOF file as full EVAL, unless\n     * we are sure that the script was already in the context of all the\n     * attached slaves *and* the current AOF file if enabled.\n     *\n     * To do so we use a cache of SHA1s of scripts that we already propagated\n     * as full EVAL, that's called the Replication Script Cache.\n     *\n     * For replication, everytime a new replica attaches to the master, we need to\n     * flush our cache of scripts that can be replicated as EVALSHA, while\n     * for AOF we need to do so every time we rewrite the AOF file. */\n    if (evalsha && !g_pserver->lua_replicate_commands) {\n        if (!replicationScriptCacheExists((sds)ptrFromObj(c->argv[1]))) {\n            /* This script is not in our script cache, replicate it as\n             * EVAL, then add it into the script cache, as from now on\n             * slaves and AOF know about it. */\n            robj *script = (robj*)dictFetchValue(g_pserver->lua_scripts,ptrFromObj(c->argv[1]));\n\n            replicationScriptCacheAdd((sds)ptrFromObj(c->argv[1]));\n            serverAssertWithInfo(c,NULL,script != NULL);\n\n            /* If the script did not produce any changes in the dataset we want\n             * just to replicate it as SCRIPT LOAD, otherwise we risk running\n             * an aborted script on slaves (that may then produce results there)\n             * or just running a CPU costly read-only script on the slaves. */\n            if (g_pserver->dirty == initial_server_dirty) {\n                rewriteClientCommandVector(c,3,\n                    shared.script,\n                    shared.load,\n                    script);\n            } else {\n                rewriteClientCommandArgument(c,0,shared.eval);\n                rewriteClientCommandArgument(c,1,script);\n            }\n            forceCommandPropagation(c,PROPAGATE_REPL|PROPAGATE_AOF);\n        }\n    }\n}\n\nvoid evalCommand(client *c) {\n    /* Explicitly feed monitor here so that lua commands appear after their\n     * script command. */\n    replicationFeedMonitors(c,g_pserver->monitors,c->db->id,c->argv,c->argc);\n    if (!(c->flags & CLIENT_LUA_DEBUG))\n        evalGenericCommand(c,0);\n    else\n        evalGenericCommandWithDebugging(c,0);\n}\n\nvoid evalShaCommand(client *c) {\n    /* Explicitly feed monitor here so that lua commands appear after their\n     * script command. */\n    replicationFeedMonitors(c,g_pserver->monitors,c->db->id,c->argv,c->argc);\n    if (sdslen((sds)ptrFromObj(c->argv[1])) != 40) {\n        /* We know that a match is not possible if the provided SHA is\n         * not the right length. So we return an error ASAP, this way\n         * evalGenericCommand() can be implemented without string length\n         * sanity check */\n        addReplyErrorObject(c, shared.noscripterr);\n        return;\n    }\n    if (!(c->flags & CLIENT_LUA_DEBUG))\n        evalGenericCommand(c,1);\n    else {\n        addReplyError(c,\"Please use EVAL instead of EVALSHA for debugging\");\n        return;\n    }\n}\n\nvoid scriptCommand(client *c) {\n    if (c->argc == 2 && !strcasecmp((const char*)ptrFromObj(c->argv[1]),\"help\")) {\n        const char *help[] = {\n\"DEBUG (YES|SYNC|NO)\",\n\"    Set the debug mode for subsequent scripts executed.\",\n\"EXISTS <sha1> [<sha1> ...]\",\n\"    Return information about the existence of the scripts in the script cache.\",\n\"FLUSH [ASYNC|SYNC]\",\n\"    Flush the Lua scripts cache. Very dangerous on replicas.\",\n\"    When called without the optional mode argument, the behavior is determined by the\",\n\"    lazyfree-lazy-user-flush configuration directive. Valid modes are:\",\n\"    * ASYNC: Asynchronously flush the scripts cache.\",\n\"    * SYNC: Synchronously flush the scripts cache.\",\n\"KILL\",\n\"    Kill the currently executing Lua script.\",\n\"LOAD <script>\",\n\"    Load a script into the scripts cache without executing it.\",\nNULL\n        };\n        addReplyHelp(c, help);\n    } else if (c->argc >= 2 && !strcasecmp(szFromObj(c->argv[1]),\"flush\")) {\n        int async = 0;\n        if (c->argc == 3 && !strcasecmp(szFromObj(c->argv[2]),\"sync\")) {\n            async = 0;\n        } else if (c->argc == 3 && !strcasecmp(szFromObj(c->argv[2]),\"async\")) {\n            async = 1;\n        } else if (c->argc == 2) {\n            async = g_pserver->lazyfree_lazy_user_flush ? 1 : 0;\n        } else {\n            addReplyError(c,\"SCRIPT FLUSH only support SYNC|ASYNC option\");\n            return;\n        }\n        scriptingReset(async);\n        addReply(c,shared.ok);\n        replicationScriptCacheFlush();\n        g_pserver->dirty++; /* Propagating this command is a good idea. */\n    } else if (c->argc >= 2 && !strcasecmp((const char*)ptrFromObj(c->argv[1]),\"exists\")) {\n        int j;\n\n        addReplyArrayLen(c, c->argc-2);\n        for (j = 2; j < c->argc; j++) {\n            if (dictFind(g_pserver->lua_scripts,ptrFromObj(c->argv[j])))\n                addReply(c,shared.cone);\n            else\n                addReply(c,shared.czero);\n        }\n    } else if (c->argc == 3 && !strcasecmp((const char*)ptrFromObj(c->argv[1]),\"load\")) {\n        sds sha = luaCreateFunction(c,g_pserver->lua,c->argv[2]);\n        if (sha == NULL) return; /* The error was sent by luaCreateFunction(). */\n        addReplyBulkCBuffer(c,sha,40);\n        forceCommandPropagation(c,PROPAGATE_REPL|PROPAGATE_AOF);\n    } else if (c->argc == 2 && !strcasecmp((const char*)ptrFromObj(c->argv[1]),\"kill\")) {\n        if (g_pserver->lua_caller == NULL) {\n            addReplyError(c,\"-NOTBUSY No scripts in execution right now.\");\n        } else if (g_pserver->lua_caller->flags & CLIENT_MASTER) {\n            addReplyError(c,\"-UNKILLABLE The busy script was sent by a master instance in the context of replication and cannot be killed.\");\n        } else if (g_pserver->lua_write_dirty) {\n            addReplyError(c,\"-UNKILLABLE Sorry the script already executed write commands against the dataset. You can either wait the script termination or kill the server in a hard way using the SHUTDOWN NOSAVE command.\");\n        } else {\n            g_pserver->lua_kill = 1;\n            addReply(c,shared.ok);\n        }\n    } else if (c->argc == 3 && !strcasecmp((const char*)ptrFromObj(c->argv[1]),\"debug\")) {\n        if (clientHasPendingReplies(c)) {\n            addReplyError(c,\"SCRIPT DEBUG must be called outside a pipeline\");\n            return;\n        }\n        if (!strcasecmp((const char*)ptrFromObj(c->argv[2]),\"no\")) {\n            ldbDisable(c);\n            addReply(c,shared.ok);\n        } else if (!strcasecmp((const char*)ptrFromObj(c->argv[2]),\"yes\")) {\n            ldbEnable(c);\n            addReply(c,shared.ok);\n        } else if (!strcasecmp((const char*)ptrFromObj(c->argv[2]),\"sync\")) {\n            ldbEnable(c);\n            addReply(c,shared.ok);\n            c->flags |= CLIENT_LUA_DEBUG_SYNC;\n        } else {\n            addReplyError(c,\"Use SCRIPT DEBUG YES/SYNC/NO\");\n            return;\n        }\n    } else {\n        addReplySubcommandSyntaxError(c);\n    }\n}\n\n/* ---------------------------------------------------------------------------\n * LDB: Redis Lua debugging facilities\n * ------------------------------------------------------------------------- */\n\n/* Initialize Lua debugger data structures. */\nvoid ldbInit(void) {\n    ldb.conn = NULL;\n    ldb.active = 0;\n    ldb.logs = listCreate();\n    listSetFreeMethod(ldb.logs,(void (*)(const void*))sdsfree);\n    ldb.children = listCreate();\n    ldb.src = NULL;\n    ldb.lines = 0;\n    ldb.cbuf = sdsempty();\n}\n\n/* Remove all the pending messages in the specified list. */\nvoid ldbFlushLog(list *log) {\n    listNode *ln;\n\n    while((ln = listFirst(log)) != NULL)\n        listDelNode(log,ln);\n}\n\n/* Enable debug mode of Lua scripts for this client. */\nvoid ldbEnable(client *c) {\n    c->flags |= CLIENT_LUA_DEBUG;\n    ldbFlushLog(ldb.logs);\n    ldb.conn = c->conn;\n    ldb.step = 1;\n    ldb.bpcount = 0;\n    ldb.luabp = 0;\n    sdsfree(ldb.cbuf);\n    ldb.cbuf = sdsempty();\n    ldb.maxlen = LDB_MAX_LEN_DEFAULT;\n    ldb.maxlen_hint_sent = 0;\n}\n\n/* Exit debugging mode from the POV of client. This function is not enough\n * to properly shut down a client debugging session, see ldbEndSession()\n * for more information. */\nvoid ldbDisable(client *c) {\n    c->flags &= ~(CLIENT_LUA_DEBUG|CLIENT_LUA_DEBUG_SYNC);\n}\n\n/* Append a log entry to the specified LDB log. */\nvoid ldbLog(sds entry) {\n    listAddNodeTail(ldb.logs,entry);\n}\n\n/* A version of ldbLog() which prevents producing logs greater than\n * ldb.maxlen. The first time the limit is reached a hint is generated\n * to inform the user that reply trimming can be disabled using the\n * debugger \"maxlen\" command. */\nvoid ldbLogWithMaxLen(sds entry) {\n    int trimmed = 0;\n    if (ldb.maxlen && sdslen(entry) > ldb.maxlen) {\n        sdsrange(entry,0,ldb.maxlen-1);\n        entry = sdscatlen(entry,\" ...\",4);\n        trimmed = 1;\n    }\n    ldbLog(entry);\n    if (trimmed && ldb.maxlen_hint_sent == 0) {\n        ldb.maxlen_hint_sent = 1;\n        ldbLog(sdsnew(\n        \"<hint> The above reply was trimmed. Use 'maxlen 0' to disable trimming.\"));\n    }\n}\n\n/* Send ldb.logs to the debugging client as a multi-bulk reply\n * consisting of simple strings. Log entries which include newlines have them\n * replaced with spaces. The entries sent are also consumed. */\nvoid ldbSendLogs(void) {\n    sds proto = sdsempty();\n    proto = sdscatfmt(proto,\"*%i\\r\\n\", (int)listLength(ldb.logs));\n    while(listLength(ldb.logs)) {\n        listNode *ln = listFirst(ldb.logs);\n        proto = sdscatlen(proto,\"+\",1);\n        sdsmapchars((sds)ln->value,\"\\r\\n\",\"  \",2);\n        proto = sdscatsds(proto,(sds)ln->value);\n        proto = sdscatlen(proto,\"\\r\\n\",2);\n        listDelNode(ldb.logs,ln);\n    }\n    if (connWrite(ldb.conn,proto,sdslen(proto)) == -1) {\n        /* Avoid warning. We don't check the return value of write()\n         * since the next read() will catch the I/O error and will\n         * close the debugging session. */\n    }\n    sdsfree(proto);\n}\n\n/* Start a debugging session before calling EVAL implementation.\n * The technique we use is to capture the client socket file descriptor,\n * in order to perform direct I/O with it from within Lua hooks. This\n * way we don't have to re-enter Redis in order to handle I/O.\n *\n * The function returns 1 if the caller should proceed to call EVAL,\n * and 0 if instead the caller should abort the operation (this happens\n * for the parent in a forked session, since it's up to the children\n * to continue, or when fork returned an error).\n *\n * The caller should call ldbEndSession() only if ldbStartSession()\n * returned 1. */\nint ldbStartSession(client *c) {\n    ldb.forked = (c->flags & CLIENT_LUA_DEBUG_SYNC) == 0;\n    if (ldb.forked) {\n        pid_t cp = redisFork(CHILD_TYPE_LDB);\n        if (cp == -1) {\n            addReplyError(c,\"Fork() failed: can't run EVAL in debugging mode.\");\n            return 0;\n        } else if (cp == 0) {\n            /* Child. Let's ignore important signals handled by the parent. */\n            struct sigaction act;\n            sigemptyset(&act.sa_mask);\n            act.sa_flags = 0;\n            act.sa_handler = SIG_IGN;\n            sigaction(SIGTERM, &act, NULL);\n            sigaction(SIGINT, &act, NULL);\n\n            /* Log the creation of the child and close the listening\n             * socket to make sure if the parent crashes a reset is sent\n             * to the clients. */\n            serverLog(LL_WARNING,\"Redis forked for debugging eval\");\n        } else {\n            /* Parent */\n            listAddNodeTail(ldb.children,(void*)(unsigned long)cp);\n            freeClientAsync(c); /* Close the client in the parent side. */\n            return 0;\n        }\n    } else {\n        serverLog(LL_WARNING,\n            \"Redis synchronous debugging eval session started\");\n    }\n\n    /* Setup our debugging session. */\n    connBlock(ldb.conn);\n    connSendTimeout(ldb.conn,5000);\n    ldb.active = 1;\n\n    /* First argument of EVAL is the script itself. We split it into different\n     * lines since this is the way the debugger accesses the source code. */\n    sds srcstring = sdsdup((sds)ptrFromObj(c->argv[1]));\n    size_t srclen = sdslen(srcstring);\n    while(srclen && (srcstring[srclen-1] == '\\n' ||\n                     srcstring[srclen-1] == '\\r'))\n    {\n        srcstring[--srclen] = '\\0';\n    }\n    sdssetlen(srcstring,srclen);\n    ldb.src = sdssplitlen(srcstring,sdslen(srcstring),\"\\n\",1,&ldb.lines);\n    sdsfree(srcstring);\n    return 1;\n}\n\n/* End a debugging session after the EVAL call with debugging enabled\n * returned. */\nvoid ldbEndSession(client *c) {\n    /* Emit the remaining logs and an <endsession> mark. */\n    ldbLog(sdsnew(\"<endsession>\"));\n    ldbSendLogs();\n\n    /* If it's a fork()ed session, we just exit. */\n    if (ldb.forked) {\n        writeToClient(c,0);\n        serverLog(LL_WARNING,\"Lua debugging session child exiting\");\n        exitFromChild(0);\n    } else {\n        serverLog(LL_WARNING,\n            \"Redis synchronous debugging eval session ended\");\n    }\n\n    /* Otherwise let's restore client's state. */\n    connNonBlock(ldb.conn);\n    connSendTimeout(ldb.conn,0);\n\n    /* Close the client connection after sending the final EVAL reply\n     * in order to signal the end of the debugging session. */\n    c->flags |= CLIENT_CLOSE_AFTER_REPLY;\n\n    /* Cleanup. */\n    sdsfreesplitres(ldb.src,ldb.lines);\n    ldb.lines = 0;\n    ldb.active = 0;\n}\n\n/* If the specified pid is among the list of children spawned for\n * forked debugging sessions, it is removed from the children list.\n * If the pid was found non-zero is returned. */\nint ldbRemoveChild(pid_t pid) {\n    listNode *ln = listSearchKey(ldb.children,(void*)(unsigned long)pid);\n    if (ln) {\n        listDelNode(ldb.children,ln);\n        return 1;\n    }\n    return 0;\n}\n\n/* Return the number of children we still did not receive termination\n * acknowledge via wait() in the parent process. */\nint ldbPendingChildren(void) {\n    return listLength(ldb.children);\n}\n\n/* Kill all the forked sessions. */\nvoid ldbKillForkedSessions(void) {\n    listIter li;\n    listNode *ln;\n\n    listRewind(ldb.children,&li);\n    while((ln = listNext(&li))) {\n        pid_t pid = (unsigned long) ln->value;\n        serverLog(LL_WARNING,\"Killing debugging session %ld\",(long)pid);\n        kill(pid,SIGKILL);\n    }\n    listRelease(ldb.children);\n    ldb.children = listCreate();\n}\n\n/* Wrapper for EVAL / EVALSHA that enables debugging, and makes sure\n * that when EVAL returns, whatever happened, the session is ended. */\nvoid evalGenericCommandWithDebugging(client *c, int evalsha) {\n    if (ldbStartSession(c)) {\n        evalGenericCommand(c,evalsha);\n        ldbEndSession(c);\n    } else {\n        ldbDisable(c);\n    }\n}\n\n/* Return a pointer to ldb.src source code line, considering line to be\n * one-based, and returning a special string for out of range lines. */\nconst char *ldbGetSourceLine(int line) {\n    int idx = line-1;\n    if (idx < 0 || idx >= ldb.lines) return \"<out of range source code line>\";\n    return ldb.src[idx];\n}\n\n/* Return true if there is a breakpoint in the specified line. */\nint ldbIsBreakpoint(int line) {\n    int j;\n\n    for (j = 0; j < ldb.bpcount; j++)\n        if (ldb.bp[j] == line) return 1;\n    return 0;\n}\n\n/* Add the specified breakpoint. Ignore it if we already reached the max.\n * Returns 1 if the breakpoint was added (or was already set). 0 if there is\n * no space for the breakpoint or if the line is invalid. */\nint ldbAddBreakpoint(int line) {\n    if (line <= 0 || line > ldb.lines) return 0;\n    if (!ldbIsBreakpoint(line) && ldb.bpcount != LDB_BREAKPOINTS_MAX) {\n        ldb.bp[ldb.bpcount++] = line;\n        return 1;\n    }\n    return 0;\n}\n\n/* Remove the specified breakpoint, returning 1 if the operation was\n * performed or 0 if there was no such breakpoint. */\nint ldbDelBreakpoint(int line) {\n    int j;\n\n    for (j = 0; j < ldb.bpcount; j++) {\n        if (ldb.bp[j] == line) {\n            ldb.bpcount--;\n            memmove(ldb.bp+j,ldb.bp+j+1,ldb.bpcount-j);\n            return 1;\n        }\n    }\n    return 0;\n}\n\n/* Expect a valid multi-bulk command in the debugging client query buffer.\n * On success the command is parsed and returned as an array of SDS strings,\n * otherwise NULL is returned and there is to read more buffer. */\nsds *ldbReplParseCommand(int *argcp, char** err) {\n    static sds protocol_error = sdsnew(\"protocol error\");\n    sds *argv = NULL;\n    int argc = 0;\n    char *plen = NULL;\n    if (sdslen(ldb.cbuf) == 0) return NULL;\n\n    /* Working on a copy is simpler in this case. We can modify it freely\n     * for the sake of simpler parsing. */\n    sds copy = sdsdup(ldb.cbuf);\n    char *p = copy;\n\n    /* This Redis protocol parser is a joke... just the simplest thing that\n     * works in this context. It is also very forgiving regarding broken\n     * protocol. */\n\n    /* Seek and parse *<count>\\r\\n. */\n    p = strchr(p,'*'); if (!p) goto protoerr;\n    plen = p+1; /* Multi bulk len pointer. */\n    p = strstr(p,\"\\r\\n\"); if (!p) goto keep_reading;\n    *p = '\\0'; p += 2;\n    *argcp = atoi(plen);\n    if (*argcp <= 0 || *argcp > 1024) goto protoerr;\n\n    /* Parse each argument. */\n    argv = (sds*)zmalloc(sizeof(sds)*(*argcp), MALLOC_LOCAL);\n    argc = 0;\n    while(argc < *argcp) {\n        /* reached the end but there should be more data to read */\n        if (*p == '\\0') goto keep_reading;\n\n        if (*p != '$') goto protoerr;\n        plen = p+1; /* Bulk string len pointer. */\n        p = strstr(p,\"\\r\\n\"); if (!p) goto keep_reading;\n        *p = '\\0'; p += 2;\n        int slen = atoi(plen); /* Length of this arg. */\n        if (slen <= 0 || slen > 1024) goto protoerr;\n        if ((size_t)(p + slen + 2 - copy) > sdslen(copy) ) goto keep_reading;\n        argv[argc++] = sdsnewlen(p,slen);\n        p += slen; /* Skip the already parsed argument. */\n        if (p[0] != '\\r' || p[1] != '\\n') goto protoerr;\n        p += 2; /* Skip \\r\\n. */\n    }\n    sdsfree(copy);\n    return argv;\n\nprotoerr:\n    *err = protocol_error;\nkeep_reading:\n    sdsfreesplitres(argv,argc);\n    sdsfree(copy);\n    return NULL;\n}\n\n/* Log the specified line in the Lua debugger output. */\nvoid ldbLogSourceLine(int lnum) {\n    const char *line = ldbGetSourceLine(lnum);\n    const char *prefix;\n    int bp = ldbIsBreakpoint(lnum);\n    int current = ldb.currentline == lnum;\n\n    if (current && bp)\n        prefix = \"->#\";\n    else if (current)\n        prefix = \"-> \";\n    else if (bp)\n        prefix = \"  #\";\n    else\n        prefix = \"   \";\n    sds thisline = sdscatprintf(sdsempty(),\"%s%-3d %s\", prefix, lnum, line);\n    ldbLog(thisline);\n}\n\n/* Implement the \"list\" command of the Lua debugger. If around is 0\n * the whole file is listed, otherwise only a small portion of the file\n * around the specified line is shown. When a line number is specified\n * the amount of context (lines before/after) is specified via the\n * 'context' argument. */\nvoid ldbList(int around, int context) {\n    int j;\n\n    for (j = 1; j <= ldb.lines; j++) {\n        if (around != 0 && abs(around-j) > context) continue;\n        ldbLogSourceLine(j);\n    }\n}\n\n/* Append a human readable representation of the Lua value at position 'idx'\n * on the stack of the 'lua' state, to the SDS string passed as argument.\n * The new SDS string with the represented value attached is returned.\n * Used in order to implement ldbLogStackValue().\n *\n * The element is not automatically removed from the stack, nor it is\n * converted to a different type. */\n#define LDB_MAX_VALUES_DEPTH (LUA_MINSTACK/2)\nsds ldbCatStackValueRec(sds s, lua_State *lua, int idx, int level) {\n    int t = lua_type(lua,idx);\n\n    if (level++ == LDB_MAX_VALUES_DEPTH)\n        return sdscat(s,\"<max recursion level reached! Nested table?>\");\n\n    switch(t) {\n    case LUA_TSTRING:\n        {\n        size_t strl;\n        char *strp = (char*)lua_tolstring(lua,idx,&strl);\n        s = sdscatrepr(s,strp,strl);\n        }\n        break;\n    case LUA_TBOOLEAN:\n        s = sdscat(s,lua_toboolean(lua,idx) ? \"true\" : \"false\");\n        break;\n    case LUA_TNUMBER:\n        s = sdscatprintf(s,\"%g\",(double)lua_tonumber(lua,idx));\n        break;\n    case LUA_TNIL:\n        s = sdscatlen(s,\"nil\",3);\n        break;\n    case LUA_TTABLE:\n        {\n        int expected_index = 1; /* First index we expect in an array. */\n        int is_array = 1; /* Will be set to null if check fails. */\n        /* Note: we create two representations at the same time, one\n         * assuming the table is an array, one assuming it is not. At the\n         * end we know what is true and select the right one. */\n        sds repr1 = sdsempty();\n        sds repr2 = sdsempty();\n        lua_pushnil(lua); /* The first key to start the iteration is nil. */\n        while (lua_next(lua,idx-1)) {\n            /* Test if so far the table looks like an array. */\n            if (is_array &&\n                (lua_type(lua,-2) != LUA_TNUMBER ||\n                 lua_tonumber(lua,-2) != expected_index)) is_array = 0;\n            /* Stack now: table, key, value */\n            /* Array repr. */\n            repr1 = ldbCatStackValueRec(repr1,lua,-1,level);\n            repr1 = sdscatlen(repr1,\"; \",2);\n            /* Full repr. */\n            repr2 = sdscatlen(repr2,\"[\",1);\n            repr2 = ldbCatStackValueRec(repr2,lua,-2,level);\n            repr2 = sdscatlen(repr2,\"]=\",2);\n            repr2 = ldbCatStackValueRec(repr2,lua,-1,level);\n            repr2 = sdscatlen(repr2,\"; \",2);\n            lua_pop(lua,1); /* Stack: table, key. Ready for next iteration. */\n            expected_index++;\n        }\n        /* Strip the last \" ;\" from both the representations. */\n        if (sdslen(repr1)) sdsrange(repr1,0,-3);\n        if (sdslen(repr2)) sdsrange(repr2,0,-3);\n        /* Select the right one and discard the other. */\n        s = sdscatlen(s,\"{\",1);\n        s = sdscatsds(s,is_array ? repr1 : repr2);\n        s = sdscatlen(s,\"}\",1);\n        sdsfree(repr1);\n        sdsfree(repr2);\n        }\n        break;\n    case LUA_TFUNCTION:\n    case LUA_TUSERDATA:\n    case LUA_TTHREAD:\n    case LUA_TLIGHTUSERDATA:\n        {\n        const void *p = lua_topointer(lua,idx);\n        const char *tname = \"unknown\";\n        if (t == LUA_TFUNCTION) tname = \"function\";\n        else if (t == LUA_TUSERDATA) tname = \"userdata\";\n        else if (t == LUA_TTHREAD) tname = \"thread\";\n        else if (t == LUA_TLIGHTUSERDATA) tname = \"light-userdata\";\n        s = sdscatprintf(s,\"\\\"%s@%p\\\"\",tname,p);\n        }\n        break;\n    default:\n        s = sdscat(s,\"\\\"<unknown-lua-type>\\\"\");\n        break;\n    }\n    return s;\n}\n\n/* Higher level wrapper for ldbCatStackValueRec() that just uses an initial\n * recursion level of '0'. */\nsds ldbCatStackValue(sds s, lua_State *lua, int idx) {\n    return ldbCatStackValueRec(s,lua,idx,0);\n}\n\n/* Produce a debugger log entry representing the value of the Lua object\n * currently on the top of the stack. The element is ot popped nor modified.\n * Check ldbCatStackValue() for the actual implementation. */\nvoid ldbLogStackValue(lua_State *lua, const char *prefix) {\n    sds s = sdsnew(prefix);\n    s = ldbCatStackValue(s,lua,-1);\n    ldbLogWithMaxLen(s);\n}\n\nchar *ldbRedisProtocolToHuman_Int(sds *o, char *reply);\nchar *ldbRedisProtocolToHuman_Bulk(sds *o, char *reply);\nchar *ldbRedisProtocolToHuman_Status(sds *o, char *reply);\nchar *ldbRedisProtocolToHuman_MultiBulk(sds *o, char *reply);\nchar *ldbRedisProtocolToHuman_Set(sds *o, char *reply);\nchar *ldbRedisProtocolToHuman_Map(sds *o, char *reply);\nchar *ldbRedisProtocolToHuman_Null(sds *o, char *reply);\nchar *ldbRedisProtocolToHuman_Bool(sds *o, char *reply);\nchar *ldbRedisProtocolToHuman_Double(sds *o, char *reply);\n\n/* Get Redis protocol from 'reply' and appends it in human readable form to\n * the passed SDS string 'o'.\n *\n * Note that the SDS string is passed by reference (pointer of pointer to\n * char*) so that we can return a modified pointer, as for SDS semantics. */\nchar *ldbRedisProtocolToHuman(sds *o, char *reply) {\n    char *p = reply;\n    switch(*p) {\n    case ':': p = ldbRedisProtocolToHuman_Int(o,reply); break;\n    case '$': p = ldbRedisProtocolToHuman_Bulk(o,reply); break;\n    case '+': p = ldbRedisProtocolToHuman_Status(o,reply); break;\n    case '-': p = ldbRedisProtocolToHuman_Status(o,reply); break;\n    case '*': p = ldbRedisProtocolToHuman_MultiBulk(o,reply); break;\n    case '~': p = ldbRedisProtocolToHuman_Set(o,reply); break;\n    case '%': p = ldbRedisProtocolToHuman_Map(o,reply); break;\n    case '_': p = ldbRedisProtocolToHuman_Null(o,reply); break;\n    case '#': p = ldbRedisProtocolToHuman_Bool(o,reply); break;\n    case ',': p = ldbRedisProtocolToHuman_Double(o,reply); break;\n    }\n    return p;\n}\n\n/* The following functions are helpers for ldbRedisProtocolToHuman(), each\n * take care of a given Redis return type. */\n\nchar *ldbRedisProtocolToHuman_Int(sds *o, char *reply) {\n    char *p = strchr(reply+1,'\\r');\n    *o = sdscatlen(*o,reply+1,p-reply-1);\n    return p+2;\n}\n\nchar *ldbRedisProtocolToHuman_Bulk(sds *o, char *reply) {\n    char *p = strchr(reply+1,'\\r');\n    long long bulklen;\n\n    string2ll(reply+1,p-reply-1,&bulklen);\n    if (bulklen == -1) {\n        *o = sdscatlen(*o,\"NULL\",4);\n        return p+2;\n    } else {\n        *o = sdscatrepr(*o,p+2,bulklen);\n        return p+2+bulklen+2;\n    }\n}\n\nchar *ldbRedisProtocolToHuman_Status(sds *o, char *reply) {\n    char *p = strchr(reply+1,'\\r');\n\n    *o = sdscatrepr(*o,reply,p-reply);\n    return p+2;\n}\n\nchar *ldbRedisProtocolToHuman_MultiBulk(sds *o, char *reply) {\n    char *p = strchr(reply+1,'\\r');\n    long long mbulklen;\n    int j = 0;\n\n    string2ll(reply+1,p-reply-1,&mbulklen);\n    p += 2;\n    if (mbulklen == -1) {\n        *o = sdscatlen(*o,\"NULL\",4);\n        return p;\n    }\n    *o = sdscatlen(*o,\"[\",1);\n    for (j = 0; j < mbulklen; j++) {\n        p = ldbRedisProtocolToHuman(o,p);\n        if (j != mbulklen-1) *o = sdscatlen(*o,\",\",1);\n    }\n    *o = sdscatlen(*o,\"]\",1);\n    return p;\n}\n\nchar *ldbRedisProtocolToHuman_Set(sds *o, char *reply) {\n    char *p = strchr(reply+1,'\\r');\n    long long mbulklen;\n    int j = 0;\n\n    string2ll(reply+1,p-reply-1,&mbulklen);\n    p += 2;\n    *o = sdscatlen(*o,\"~(\",2);\n    for (j = 0; j < mbulklen; j++) {\n        p = ldbRedisProtocolToHuman(o,p);\n        if (j != mbulklen-1) *o = sdscatlen(*o,\",\",1);\n    }\n    *o = sdscatlen(*o,\")\",1);\n    return p;\n}\n\nchar *ldbRedisProtocolToHuman_Map(sds *o, char *reply) {\n    char *p = strchr(reply+1,'\\r');\n    long long mbulklen;\n    int j = 0;\n\n    string2ll(reply+1,p-reply-1,&mbulklen);\n    p += 2;\n    *o = sdscatlen(*o,\"{\",1);\n    for (j = 0; j < mbulklen; j++) {\n        p = ldbRedisProtocolToHuman(o,p);\n        *o = sdscatlen(*o,\" => \",4);\n        p = ldbRedisProtocolToHuman(o,p);\n        if (j != mbulklen-1) *o = sdscatlen(*o,\",\",1);\n    }\n    *o = sdscatlen(*o,\"}\",1);\n    return p;\n}\n\nchar *ldbRedisProtocolToHuman_Null(sds *o, char *reply) {\n    char *p = strchr(reply+1,'\\r');\n    *o = sdscatlen(*o,\"(null)\",6);\n    return p+2;\n}\n\nchar *ldbRedisProtocolToHuman_Bool(sds *o, char *reply) {\n    char *p = strchr(reply+1,'\\r');\n    if (reply[1] == 't')\n        *o = sdscatlen(*o,\"#true\",5);\n    else\n        *o = sdscatlen(*o,\"#false\",6);\n    return p+2;\n}\n\nchar *ldbRedisProtocolToHuman_Double(sds *o, char *reply) {\n    char *p = strchr(reply+1,'\\r');\n    *o = sdscatlen(*o,\"(double) \",9);\n    *o = sdscatlen(*o,reply+1,p-reply-1);\n    return p+2;\n}\n\n/* Log a Redis reply as debugger output, in a human readable format.\n * If the resulting string is longer than 'len' plus a few more chars\n * used as prefix, it gets truncated. */\nvoid ldbLogRedisReply(char *reply) {\n    sds log = sdsnew(\"<reply> \");\n    ldbRedisProtocolToHuman(&log,reply);\n    ldbLogWithMaxLen(log);\n}\n\n/* Implements the \"print <var>\" command of the Lua debugger. It scans for Lua\n * var \"varname\" starting from the current stack frame up to the top stack\n * frame. The first matching variable is printed. */\nvoid ldbPrint(lua_State *lua, char *varname) {\n    lua_Debug ar;\n\n    int l = 0; /* Stack level. */\n    while (lua_getstack(lua,l,&ar) != 0) {\n        l++;\n        const char *name;\n        int i = 1; /* Variable index. */\n        while((name = lua_getlocal(lua,&ar,i)) != NULL) {\n            i++;\n            if (strcmp(varname,name) == 0) {\n                ldbLogStackValue(lua,\"<value> \");\n                lua_pop(lua,1);\n                return;\n            } else {\n                lua_pop(lua,1); /* Discard the var name on the stack. */\n            }\n        }\n    }\n\n    /* Let's try with global vars in two selected cases */\n    if (!strcmp(varname,\"ARGV\") || !strcmp(varname,\"KEYS\")) {\n        lua_getglobal(lua, varname);\n        ldbLogStackValue(lua,\"<value> \");\n        lua_pop(lua,1);\n    } else {\n        ldbLog(sdsnew(\"No such variable.\"));\n    }\n}\n\n/* Implements the \"print\" command (without arguments) of the Lua debugger.\n * Prints all the variables in the current stack frame. */\nvoid ldbPrintAll(lua_State *lua) {\n    lua_Debug ar;\n    int vars = 0;\n\n    if (lua_getstack(lua,0,&ar) != 0) {\n        const char *name;\n        int i = 1; /* Variable index. */\n        while((name = lua_getlocal(lua,&ar,i)) != NULL) {\n            i++;\n            if (!strstr(name,\"(*temporary)\")) {\n                sds prefix = sdscatprintf(sdsempty(),\"<value> %s = \",name);\n                ldbLogStackValue(lua,prefix);\n                sdsfree(prefix);\n                vars++;\n            }\n            lua_pop(lua,1);\n        }\n    }\n\n    if (vars == 0) {\n        ldbLog(sdsnew(\"No local variables in the current context.\"));\n    }\n}\n\n/* Implements the break command to list, add and remove breakpoints. */\nvoid ldbBreak(sds *argv, int argc) {\n    if (argc == 1) {\n        if (ldb.bpcount == 0) {\n            ldbLog(sdsnew(\"No breakpoints set. Use 'b <line>' to add one.\"));\n            return;\n        } else {\n            ldbLog(sdscatfmt(sdsempty(),\"%i breakpoints set:\",ldb.bpcount));\n            int j;\n            for (j = 0; j < ldb.bpcount; j++)\n                ldbLogSourceLine(ldb.bp[j]);\n        }\n    } else {\n        int j;\n        for (j = 1; j < argc; j++) {\n            char *arg = argv[j];\n            long line;\n            if (!string2l(arg,sdslen(arg),&line)) {\n                ldbLog(sdscatfmt(sdsempty(),\"Invalid argument:'%s'\",arg));\n            } else {\n                if (line == 0) {\n                    ldb.bpcount = 0;\n                    ldbLog(sdsnew(\"All breakpoints removed.\"));\n                } else if (line > 0) {\n                    if (ldb.bpcount == LDB_BREAKPOINTS_MAX) {\n                        ldbLog(sdsnew(\"Too many breakpoints set.\"));\n                    } else if (ldbAddBreakpoint(line)) {\n                        ldbList(line,1);\n                    } else {\n                        ldbLog(sdsnew(\"Wrong line number.\"));\n                    }\n                } else if (line < 0) {\n                    if (ldbDelBreakpoint(-line))\n                        ldbLog(sdsnew(\"Breakpoint removed.\"));\n                    else\n                        ldbLog(sdsnew(\"No breakpoint in the specified line.\"));\n                }\n            }\n        }\n    }\n}\n\n/* Implements the Lua debugger \"eval\" command. It just compiles the user\n * passed fragment of code and executes it, showing the result left on\n * the stack. */\nvoid ldbEval(lua_State *lua, sds *argv, int argc) {\n    /* Glue the script together if it is composed of multiple arguments. */\n    sds code = sdsjoinsds(argv+1,argc-1,\" \",1);\n    sds expr = sdscatsds(sdsnew(\"return \"),code);\n\n    /* Try to compile it as an expression, prepending \"return \". */\n    if (luaL_loadbuffer(lua,expr,sdslen(expr),\"@ldb_eval\")) {\n        lua_pop(lua,1);\n        /* Failed? Try as a statement. */\n        if (luaL_loadbuffer(lua,code,sdslen(code),\"@ldb_eval\")) {\n            ldbLog(sdscatfmt(sdsempty(),\"<error> %s\",lua_tostring(lua,-1)));\n            lua_pop(lua,1);\n            sdsfree(code);\n            sdsfree(expr);\n            return;\n        }\n    }\n\n    /* Call it. */\n    sdsfree(code);\n    sdsfree(expr);\n    if (lua_pcall(lua,0,1,0)) {\n        ldbLog(sdscatfmt(sdsempty(),\"<error> %s\",lua_tostring(lua,-1)));\n        lua_pop(lua,1);\n        return;\n    }\n    ldbLogStackValue(lua,\"<retval> \");\n    lua_pop(lua,1);\n}\n\n/* Implement the debugger \"redis\" command. We use a trick in order to make\n * the implementation very simple: we just call the Lua redis.call() command\n * implementation, with ldb.step enabled, so as a side effect the Redis command\n * and its reply are logged. */\nvoid ldbRedis(lua_State *lua, sds *argv, int argc) {\n    int j, saved_rc = g_pserver->lua_replicate_commands;\n\n    if (!lua_checkstack(lua, argc + 1)) {\n        /* Increase the Lua stack if needed to make sure there is enough room\n         * to push 'argc + 1' elements to the stack. On failure, return error.\n         * Notice that we need, in worst case, 'argc + 1' elements because we push all the arguments\n         * given by the user (without the first argument) and we also push the 'redis' global table and\n         * 'redis.call' function so:\n         * (1 (redis table)) + (1 (redis.call function)) + (argc - 1 (all arguments without the first)) = argc + 1*/\n        sds reply = sdsnew(\"max lua stack reached\");\n        ldbLogRedisReply(reply);\n        sdsfree(reply);\n        return;\n    }\n\n    lua_getglobal(lua,\"redis\");\n    lua_pushstring(lua,\"call\");\n    lua_gettable(lua,-2);       /* Stack: redis, redis.call */\n    for (j = 1; j < argc; j++)\n        lua_pushlstring(lua,argv[j],sdslen(argv[j]));\n    ldb.step = 1;               /* Force redis.call() to log. */\n    g_pserver->lua_replicate_commands = 1;\n    lua_pcall(lua,argc-1,1,0);  /* Stack: redis, result */\n    ldb.step = 0;               /* Disable logging. */\n    g_pserver->lua_replicate_commands = saved_rc;\n    lua_pop(lua,2);             /* Discard the result and clean the stack. */\n}\n\n/* Implements \"trace\" command of the Lua debugger. It just prints a backtrace\n * querying Lua starting from the current callframe back to the outer one. */\nvoid ldbTrace(lua_State *lua) {\n    lua_Debug ar;\n    int level = 0;\n\n    while(lua_getstack(lua,level,&ar)) {\n        lua_getinfo(lua,\"Snl\",&ar);\n        if(strstr(ar.short_src,\"user_script\") != NULL) {\n            ldbLog(sdscatprintf(sdsempty(),\"%s %s:\",\n                (level == 0) ? \"In\" : \"From\",\n                ar.name ? ar.name : \"top level\"));\n            ldbLogSourceLine(ar.currentline);\n        }\n        level++;\n    }\n    if (level == 0) {\n        ldbLog(sdsnew(\"<error> Can't retrieve Lua stack.\"));\n    }\n}\n\n/* Implements the debugger \"maxlen\" command. It just queries or sets the\n * ldb.maxlen variable. */\nvoid ldbMaxlen(sds *argv, int argc) {\n    if (argc == 2) {\n        int newval = atoi(argv[1]);\n        ldb.maxlen_hint_sent = 1; /* User knows about this command. */\n        if (newval != 0 && newval <= 60) newval = 60;\n        ldb.maxlen = newval;\n    }\n    if (ldb.maxlen) {\n        ldbLog(sdscatprintf(sdsempty(),\"<value> replies are truncated at %d bytes.\",(int)ldb.maxlen));\n    } else {\n        ldbLog(sdscatprintf(sdsempty(),\"<value> replies are unlimited.\"));\n    }\n}\n\n/* Read debugging commands from client.\n * Return C_OK if the debugging session is continuing, otherwise\n * C_ERR if the client closed the connection or is timing out. */\nint ldbRepl(lua_State *lua) {\n    sds *argv;\n    int argc;\n    char* err = NULL;\n\n    /* We continue processing commands until a command that should return\n     * to the Lua interpreter is found. */\n    while(1) {\n        while((argv = ldbReplParseCommand(&argc, &err)) == NULL) {\n            char buf[1024];\n            if (err) {\n                lua_pushstring(lua, err);\n                lua_error(lua);\n            }\n            int nread = connRead(ldb.conn,buf,sizeof(buf));\n            if (nread <= 0) {\n                /* Make sure the script runs without user input since the\n                 * client is no longer connected. */\n                ldb.step = 0;\n                ldb.bpcount = 0;\n                return C_ERR;\n            }\n            ldb.cbuf = sdscatlen(ldb.cbuf,buf,nread);\n            /* after 1M we will exit with an error\n             * so that the client will not blow the memory\n             */\n            if (sdslen(ldb.cbuf) > 1<<20) {\n                sdsfree(ldb.cbuf);\n                ldb.cbuf = sdsempty();\n                lua_pushstring(lua, \"max client buffer reached\");\n                lua_error(lua);\n            }\n        }\n\n        /* Flush the old buffer. */\n        sdsfree(ldb.cbuf);\n        ldb.cbuf = sdsempty();\n\n        /* Execute the command. */\n        if (!strcasecmp(argv[0],\"h\") || !strcasecmp(argv[0],\"help\")) {\nldbLog(sdsnew(\"Redis Lua debugger help:\"));\nldbLog(sdsnew(\"[h]elp               Show this help.\"));\nldbLog(sdsnew(\"[s]tep               Run current line and stop again.\"));\nldbLog(sdsnew(\"[n]ext               Alias for step.\"));\nldbLog(sdsnew(\"[c]continue          Run till next breakpoint.\"));\nldbLog(sdsnew(\"[l]list              List source code around current line.\"));\nldbLog(sdsnew(\"[l]list [line]       List source code around [line].\"));\nldbLog(sdsnew(\"                     line = 0 means: current position.\"));\nldbLog(sdsnew(\"[l]list [line] [ctx] In this form [ctx] specifies how many lines\"));\nldbLog(sdsnew(\"                     to show before/after [line].\"));\nldbLog(sdsnew(\"[w]hole              List all source code. Alias for 'list 1 1000000'.\"));\nldbLog(sdsnew(\"[p]rint              Show all the local variables.\"));\nldbLog(sdsnew(\"[p]rint <var>        Show the value of the specified variable.\"));\nldbLog(sdsnew(\"                     Can also show global vars KEYS and ARGV.\"));\nldbLog(sdsnew(\"[b]reak              Show all breakpoints.\"));\nldbLog(sdsnew(\"[b]reak <line>       Add a breakpoint to the specified line.\"));\nldbLog(sdsnew(\"[b]reak -<line>      Remove breakpoint from the specified line.\"));\nldbLog(sdsnew(\"[b]reak 0            Remove all breakpoints.\"));\nldbLog(sdsnew(\"[t]race              Show a backtrace.\"));\nldbLog(sdsnew(\"[e]eval <code>       Execute some Lua code (in a different callframe).\"));\nldbLog(sdsnew(\"[r]edis <cmd>        Execute a Redis command.\"));\nldbLog(sdsnew(\"[m]axlen [len]       Trim logged Redis replies and Lua var dumps to len.\"));\nldbLog(sdsnew(\"                     Specifying zero as <len> means unlimited.\"));\nldbLog(sdsnew(\"[a]bort              Stop the execution of the script. In sync\"));\nldbLog(sdsnew(\"                     mode dataset changes will be retained.\"));\nldbLog(sdsnew(\"\"));\nldbLog(sdsnew(\"Debugger functions you can call from Lua scripts:\"));\nldbLog(sdsnew(\"redis.debug()        Produce logs in the debugger console.\"));\nldbLog(sdsnew(\"redis.breakpoint()   Stop execution like if there was a breakpoint in the\"));\nldbLog(sdsnew(\"                     next line of code.\"));\n            ldbSendLogs();\n        } else if (!strcasecmp(argv[0],\"s\") || !strcasecmp(argv[0],\"step\") ||\n                   !strcasecmp(argv[0],\"n\") || !strcasecmp(argv[0],\"next\")) {\n            ldb.step = 1;\n            break;\n        } else if (!strcasecmp(argv[0],\"c\") || !strcasecmp(argv[0],\"continue\")){\n            break;\n        } else if (!strcasecmp(argv[0],\"t\") || !strcasecmp(argv[0],\"trace\")) {\n            ldbTrace(lua);\n            ldbSendLogs();\n        } else if (!strcasecmp(argv[0],\"m\") || !strcasecmp(argv[0],\"maxlen\")) {\n            ldbMaxlen(argv,argc);\n            ldbSendLogs();\n        } else if (!strcasecmp(argv[0],\"b\") || !strcasecmp(argv[0],\"break\")) {\n            ldbBreak(argv,argc);\n            ldbSendLogs();\n        } else if (!strcasecmp(argv[0],\"e\") || !strcasecmp(argv[0],\"eval\")) {\n            ldbEval(lua,argv,argc);\n            ldbSendLogs();\n        } else if (!strcasecmp(argv[0],\"a\") || !strcasecmp(argv[0],\"abort\")) {\n            lua_pushstring(lua, \"script aborted for user request\");\n            lua_error(lua);\n        } else if (argc > 1 &&\n                   (!strcasecmp(argv[0],\"r\") || !strcasecmp(argv[0],\"redis\"))) {\n            ldbRedis(lua,argv,argc);\n            ldbSendLogs();\n        } else if ((!strcasecmp(argv[0],\"p\") || !strcasecmp(argv[0],\"print\"))) {\n            if (argc == 2)\n                ldbPrint(lua,argv[1]);\n            else\n                ldbPrintAll(lua);\n            ldbSendLogs();\n        } else if (!strcasecmp(argv[0],\"l\") || !strcasecmp(argv[0],\"list\")){\n            int around = ldb.currentline, ctx = 5;\n            if (argc > 1) {\n                int num = atoi(argv[1]);\n                if (num > 0) around = num;\n            }\n            if (argc > 2) ctx = atoi(argv[2]);\n            ldbList(around,ctx);\n            ldbSendLogs();\n        } else if (!strcasecmp(argv[0],\"w\") || !strcasecmp(argv[0],\"whole\")){\n            ldbList(1,1000000);\n            ldbSendLogs();\n        } else {\n            ldbLog(sdsnew(\"<error> Unknown Redis Lua debugger command or \"\n                          \"wrong number of arguments.\"));\n            ldbSendLogs();\n        }\n\n        /* Free the command vector. */\n        sdsfreesplitres(argv,argc);\n    }\n\n    /* Free the current command argv if we break inside the while loop. */\n    sdsfreesplitres(argv,argc);\n    return C_OK;\n}\n\n/* This is the core of our Lua debugger, called each time Lua is about\n * to start executing a new line. */\nvoid luaLdbLineHook(lua_State *lua, lua_Debug *ar) {\n    lua_getstack(lua,0,ar);\n    lua_getinfo(lua,\"Sl\",ar);\n    ldb.currentline = ar->currentline;\n\n    int bp = ldbIsBreakpoint(ldb.currentline) || ldb.luabp;\n    int timeout = 0;\n\n    /* Events outside our script are not interesting. */\n    if(strstr(ar->short_src,\"user_script\") == NULL) return;\n\n    /* Check if a timeout occurred. */\n    if (ar->event == LUA_HOOKCOUNT && ldb.step == 0 && bp == 0) {\n        mstime_t elapsed = elapsedMs(g_pserver->lua_time_start);\n        mstime_t timelimit = g_pserver->lua_time_limit ?\n                             g_pserver->lua_time_limit : 5000;\n        if (elapsed >= timelimit) {\n            timeout = 1;\n            ldb.step = 1;\n        } else {\n            return; /* No timeout, ignore the COUNT event. */\n        }\n    }\n\n    if (ldb.step || bp) {\n        const char *reason = \"step over\";\n        if (bp) reason = ldb.luabp ? \"redis.breakpoint() called\" :\n                                     \"break point\";\n        else if (timeout) reason = \"timeout reached, infinite loop?\";\n        ldb.step = 0;\n        ldb.luabp = 0;\n        ldbLog(sdscatprintf(sdsempty(),\n            \"* Stopped at %d, stop reason = %s\",\n            ldb.currentline, reason));\n        ldbLogSourceLine(ldb.currentline);\n        ldbSendLogs();\n        if (ldbRepl(lua) == C_ERR && timeout) {\n            /* If the client closed the connection and we have a timeout\n             * connection, let's kill the script otherwise the process\n             * will remain blocked indefinitely. */\n            lua_pushstring(lua, \"timeout during Lua debugging with client closing connection\");\n            lua_error(lua);\n        }\n        g_pserver->lua_time_start = getMonotonicUs();\n        g_pserver->lua_time_snapshot = mstime();\n    }\n}\n"
  },
  {
    "path": "src/sds.c",
    "content": "/* SDSLib 2.0 -- A C dynamic strings library\n *\n * Copyright (c) 2006-2015, Salvatore Sanfilippo <antirez at gmail dot com>\n * Copyright (c) 2015, Oran Agra\n * Copyright (c) 2015, Redis Labs, Inc\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <ctype.h>\n#include <assert.h>\n#include <limits.h>\n#include \"sds.h\"\n#include \"sdsalloc.h\"\n\nconst char *SDS_NOINIT = \"SDS_NOINIT\";\n\nstatic inline int sdsHdrSize(char type) {\n    switch(type&SDS_TYPE_MASK) {\n        case SDS_TYPE_5:\n            return sizeof(struct sdshdr5);\n        case SDS_TYPE_8:\n            return sizeof(struct sdshdr8);\n        case SDS_TYPE_16:\n            return sizeof(struct sdshdr16);\n        case SDS_TYPE_32:\n            return sizeof(struct sdshdr32);\n        case SDS_TYPE_64:\n            return sizeof(struct sdshdr64);\n        case SDS_TYPE_REFCOUNTED:\n            return sizeof(struct sdshdrrefcount);\n    }\n    return 0;\n}\n\nstatic inline char sdsReqType(ssize_t string_size) {\n    if (string_size < 0){\n        string_size = -string_size;\n        if (string_size < 1<<16)\n            return SDS_TYPE_REFCOUNTED;\n    }\n    if (string_size < 1<<5)\n        return SDS_TYPE_5;\n    if (string_size < 1<<8)\n        return SDS_TYPE_8;\n    if (string_size < 1<<16)\n        return SDS_TYPE_16;\n#if (LONG_MAX == LLONG_MAX)\n    if (string_size < 1ll<<32)\n        return SDS_TYPE_32;\n    return SDS_TYPE_64;\n#else\n    return SDS_TYPE_32;\n#endif\n}\n\nstatic inline size_t sdsTypeMaxSize(char type) {\n    if (type == SDS_TYPE_5)\n        return (1<<5) - 1;\n    if (type == SDS_TYPE_8)\n        return (1<<8) - 1;\n    if (type == SDS_TYPE_16)\n        return (1<<16) - 1;\n#if (LONG_MAX == LLONG_MAX)\n    if (type == SDS_TYPE_32)\n        return (1ll<<32) - 1;\n#endif\n    return -1; /* this is equivalent to the max SDS_TYPE_64 or SDS_TYPE_32 */\n}\n\n/* Create a new sds string with the content specified by the 'init' pointer\n * and 'initlen'.\n * If NULL is used for 'init' the string is initialized with zero bytes.\n * If SDS_NOINIT is used, the buffer is left uninitialized;\n *\n * The string is always null-termined (all the sds strings are, always) so\n * even if you create an sds string with:\n *\n * mystring = sdsnewlen(\"abc\",3);\n *\n * You can print the string with printf() as there is an implicit \\0 at the\n * end of the string. However the string is binary safe and can contain\n * \\0 characters in the middle, as the length is stored in the sds header. */\nsds _sdsnewlen(const void *init, ssize_t initlen, int trymalloc) {\n    void *sh;\n    sds s;\n    char type = sdsReqType(initlen);\n    if (initlen < 0)\n        initlen = -initlen;\n    /* Empty strings are usually created in order to append. Use type 8\n     * since type 5 is not good at this. */\n    if (type == SDS_TYPE_5 && initlen == 0) type = SDS_TYPE_8;\n    int hdrlen = sdsHdrSize(type);\n    unsigned char *fp; /* flags pointer. */\n    size_t usable;\n\n    assert(initlen + hdrlen + 1 > initlen); /* Catch size_t overflow */\n    sh = trymalloc?\n        s_trymalloc_usable(hdrlen+initlen+1, &usable) :\n        s_malloc_usable(hdrlen+initlen+1, &usable);\n    if (sh == NULL) return NULL;\n    if (init==SDS_NOINIT)\n        init = NULL;\n    else if (!init)\n        memset(sh, 0, hdrlen+initlen+1);\n    s = (char*)sh+hdrlen;\n    fp = ((unsigned char*)s)-1;\n    usable = usable-hdrlen-1;\n    if (usable > sdsTypeMaxSize(type))\n        usable = sdsTypeMaxSize(type);\n    switch(type) {\n        case SDS_TYPE_5: {\n            *fp = type | (initlen << SDS_TYPE_BITS);\n            break;\n        }\n        case SDS_TYPE_8: {\n            SDS_HDR_VAR(8,s);\n            sh->len = initlen;\n            sh->alloc = usable;\n            *fp = type;\n            break;\n        }\n        case SDS_TYPE_16: {\n            SDS_HDR_VAR(16,s);\n            sh->len = initlen;\n            sh->alloc = usable;\n            *fp = type;\n            break;\n        }\n        case SDS_TYPE_32: {\n            SDS_HDR_VAR(32,s);\n            sh->len = initlen;\n            sh->alloc = usable;\n            *fp = type;\n            break;\n        }\n        case SDS_TYPE_64: {\n            SDS_HDR_VAR(64,s);\n            sh->len = initlen;\n            sh->alloc = usable;\n            *fp = type;\n            break;\n        }\n        case SDS_TYPE_REFCOUNTED: {\n            SDS_HDR_VAR_REFCOUNTED(s);\n            sh->len = initlen;\n            sh->refcount = 1;\n            *fp = type;\n            break;\n        }\n    }\n    if (initlen && init)\n        memcpy(s, init, initlen);\n    s[initlen] = '\\0';\n    return s;\n}\n\nsds sdsnewlen(const void *init, ssize_t initlen) {\n    return _sdsnewlen(init, initlen, 0);\n}\n\nsds sdstrynewlen(const void *init, size_t initlen) {\n    return _sdsnewlen(init, initlen, 1);\n}\n\n/* Create an empty (zero length) sds string. Even in this case the string\n * always has an implicit null term. */\nsds sdsempty(void) {\n    return sdsnewlen(\"\",0);\n}\n\n/* Create a new sds string starting from a null terminated C string. */\nsds sdsnew(const char *init) {\n    size_t initlen = (init == NULL) ? 0 : strlen(init);\n    return sdsnewlen(init, initlen);\n}\n\n/* Duplicate an sds string. */\nsds sdsdup(const char *s) {\n    return sdsnewlen(s, sdslen(s));\n}\n\nsds sdsdupshared(const char *s) {\n    if (s == NULL)\n        return NULL;\n    unsigned char flags = s[-1];\n    if ((flags & SDS_TYPE_MASK) != SDS_TYPE_REFCOUNTED)\n        return sdsnewlen(s, -sdslen(s));\n    SDS_HDR_VAR_REFCOUNTED(s);\n    __atomic_fetch_add(&sh->refcount, 1, __ATOMIC_RELAXED);\n    return (sds)s;\n}\n\n/* Free an sds string. No operation is performed if 's' is NULL. */\nvoid sdsfree(const char *s) {\n    if (s == NULL) return;\n    unsigned char flags = s[-1];\n    if ((flags & SDS_TYPE_MASK) == SDS_TYPE_REFCOUNTED)\n    {\n        SDS_HDR_VAR_REFCOUNTED(s);\n        if (__atomic_fetch_sub(&sh->refcount, 1, __ATOMIC_ACQ_REL) > 1)\n            return;\n    }\n    s_free((char*)s-sdsHdrSize(s[-1]));\n}\n\n/* Set the sds string length to the length as obtained with strlen(), so\n * considering as content only up to the first null term character.\n *\n * This function is useful when the sds string is hacked manually in some\n * way, like in the following example:\n *\n * s = sdsnew(\"foobar\");\n * s[2] = '\\0';\n * sdsupdatelen(s);\n * printf(\"%d\\n\", sdslen(s));\n *\n * The output will be \"2\", but if we comment out the call to sdsupdatelen()\n * the output will be \"6\" as the string was modified but the logical length\n * remains 6 bytes. */\nvoid sdsupdatelen(sds s) {\n    size_t reallen = strlen(s);\n    sdssetlen(s, reallen);\n}\n\n/* Modify an sds string in-place to make it empty (zero length).\n * However all the existing buffer is not discarded but set as free space\n * so that next append operations will not require allocations up to the\n * number of bytes previously available. */\nvoid sdsclear(sds s) {\n    sdssetlen(s, 0);\n    s[0] = '\\0';\n}\n\n/* Enlarge the free space at the end of the sds string so that the caller\n * is sure that after calling this function can overwrite up to addlen\n * bytes after the end of the string, plus one more byte for nul term.\n *\n * Note: this does not change the *length* of the sds string as returned\n * by sdslen(), but only the free buffer space we have. */\nsds sdsMakeRoomFor(sds s, size_t addlen) {\n    if (s == NULL)\n        return sdsnewlen(NULL, addlen);\n    \n    void *sh, *newsh;\n    size_t avail = sdsavail(s);\n    size_t len, newlen, reqlen;\n    char type, oldtype = s[-1] & SDS_TYPE_MASK;\n    int hdrlen;\n    size_t usable;\n\n    /* Return ASAP if there is enough space left. */\n    if (avail >= addlen) return s;\n\n    len = sdslen(s);\n    sh = (char*)s-sdsHdrSize(oldtype);\n    reqlen = newlen = (len+addlen);\n    assert(newlen > len);   /* Catch size_t overflow */\n    if (newlen < SDS_MAX_PREALLOC)\n        newlen *= 2;\n    else\n        newlen += SDS_MAX_PREALLOC;\n\n    type = sdsReqType(newlen);\n\n    /* Don't use type 5: the user is appending to the string and type 5 is\n     * not able to remember empty space, so sdsMakeRoomFor() must be called\n     * at every appending operation. */\n    if (type == SDS_TYPE_5) type = SDS_TYPE_8;\n\n    hdrlen = sdsHdrSize(type);\n    assert(hdrlen + newlen + 1 > reqlen);  /* Catch size_t overflow */\n    if (oldtype==type) {\n        newsh = s_realloc_usable(sh, hdrlen+newlen+1, &usable);\n        if (newsh == NULL) return NULL;\n        s = (char*)newsh+hdrlen;\n    } else {\n        /* Since the header size changes, need to move the string forward,\n         * and can't use realloc */\n        newsh = s_malloc_usable(hdrlen+newlen+1, &usable);\n        if (newsh == NULL) return NULL;\n        memcpy((char*)newsh+hdrlen, s, len+1);\n        s_free(sh);\n        s = (char*)newsh+hdrlen;\n        s[-1] = type;\n        sdssetlen(s, len);\n    }\n    usable = usable-hdrlen-1;\n    if (usable > sdsTypeMaxSize(type))\n        usable = sdsTypeMaxSize(type);\n    sdssetalloc(s, usable);\n    return s;\n}\n\n/* Reallocate the sds string so that it has no free space at the end. The\n * contained string remains not altered, but next concatenation operations\n * will require a reallocation.\n *\n * After the call, the passed sds string is no longer valid and all the\n * references must be substituted with the new pointer returned by the call. */\nsds sdsRemoveFreeSpace(sds s) {\n    void *sh, *newsh;\n    char type, oldtype = s[-1] & SDS_TYPE_MASK;\n    int hdrlen, oldhdrlen = sdsHdrSize(oldtype);\n    size_t len = sdslen(s);\n    size_t avail = sdsavail(s);\n    sh = (char*)s-oldhdrlen;\n\n    /* Return ASAP if there is no space left. */\n    if (avail == 0) return s;\n\n    /* Check what would be the minimum SDS header that is just good enough to\n     * fit this string. */\n    type = sdsReqType(len);\n    hdrlen = sdsHdrSize(type);\n\n    /* If the type is the same, or at least a large enough type is still\n     * required, we just realloc(), letting the allocator to do the copy\n     * only if really needed. Otherwise if the change is huge, we manually\n     * reallocate the string to use the different header type. */\n    if (oldtype==type || type > SDS_TYPE_8) {\n        newsh = s_realloc(sh, oldhdrlen+len+1, MALLOC_SHARED);\n        if (newsh == NULL) return NULL;\n        s = (char*)newsh+oldhdrlen;\n    } else {\n        newsh = s_malloc(hdrlen+len+1, MALLOC_SHARED);\n        if (newsh == NULL) return NULL;\n        memcpy((char*)newsh+hdrlen, s, len+1);\n        s_free(sh);\n        s = (char*)newsh+hdrlen;\n        s[-1] = type;\n        sdssetlen(s, len);\n    }\n    sdssetalloc(s, len);\n    return s;\n}\n\n/* Return the total size of the allocation of the specified sds string,\n * including:\n * 1) The sds header before the pointer.\n * 2) The string.\n * 3) The free buffer at the end if any.\n * 4) The implicit null term.\n */\nsize_t sdsAllocSize(sds s) {\n    size_t alloc = sdsalloc(s);\n    return sdsHdrSize(s[-1])+alloc+1;\n}\n\n/* Return the pointer of the actual SDS allocation (normally SDS strings\n * are referenced by the start of the string buffer). */\nvoid *sdsAllocPtr(sds s) {\n    return (void*) (s-sdsHdrSize(s[-1]));\n}\n\n/* Increment the sds length and decrements the left free space at the\n * end of the string according to 'incr'. Also set the null term\n * in the new end of the string.\n *\n * This function is used in order to fix the string length after the\n * user calls sdsMakeRoomFor(), writes something after the end of\n * the current string, and finally needs to set the new length.\n *\n * Note: it is possible to use a negative increment in order to\n * right-trim the string.\n *\n * Usage example:\n *\n * Using sdsIncrLen() and sdsMakeRoomFor() it is possible to mount the\n * following schema, to cat bytes coming from the kernel to the end of an\n * sds string without copying into an intermediate buffer:\n *\n * oldlen = sdslen(s);\n * s = sdsMakeRoomFor(s, BUFFER_SIZE);\n * nread = read(fd, s+oldlen, BUFFER_SIZE);\n * ... check for nread <= 0 and handle it ...\n * sdsIncrLen(s, nread);\n */\nvoid sdsIncrLen(sds s, ssize_t incr) {\n    unsigned char flags = s[-1];\n    size_t len;\n    switch(flags&SDS_TYPE_MASK) {\n        case SDS_TYPE_5: {\n            unsigned char *fp = ((unsigned char*)s)-1;\n            unsigned char oldlen = SDS_TYPE_5_LEN(flags);\n            assert((incr > 0 && oldlen+incr < 32) || (incr < 0 && oldlen >= (unsigned int)(-incr)));\n            *fp = SDS_TYPE_5 | ((oldlen+incr) << SDS_TYPE_BITS);\n            len = oldlen+incr;\n            break;\n        }\n        case SDS_TYPE_8: {\n            SDS_HDR_VAR(8,s);\n            assert((incr >= 0 && sh->alloc-sh->len >= incr) || (incr < 0 && sh->len >= (unsigned int)(-incr)));\n            len = (sh->len += incr);\n            break;\n        }\n        case SDS_TYPE_16: {\n            SDS_HDR_VAR(16,s);\n            assert((incr >= 0 && sh->alloc-sh->len >= incr) || (incr < 0 && sh->len >= (unsigned int)(-incr)));\n            len = (sh->len += incr);\n            break;\n        }\n        case SDS_TYPE_32: {\n            SDS_HDR_VAR(32,s);\n            assert((incr >= 0 && sh->alloc-sh->len >= (unsigned int)incr) || (incr < 0 && sh->len >= (unsigned int)(-incr)));\n            len = (sh->len += incr);\n            break;\n        }\n        case SDS_TYPE_64: {\n            SDS_HDR_VAR(64,s);\n            assert((incr >= 0 && sh->alloc-sh->len >= (uint64_t)incr) || (incr < 0 && sh->len >= (uint64_t)(-incr)));\n            len = (sh->len += incr);\n            break;\n        }\n        case SDS_TYPE_REFCOUNTED: {\n            SDS_HDR_VAR_REFCOUNTED(s);\n            len = (sh->len += incr);\n            break;\n        }\n        default: len = 0; /* Just to avoid compilation warnings. */\n    }\n    s[len] = '\\0';\n}\n\n/* Grow the sds to have the specified length. Bytes that were not part of\n * the original length of the sds will be set to zero.\n *\n * if the specified length is smaller than the current length, no operation\n * is performed. */\nsds sdsgrowzero(sds s, size_t len) {\n    size_t curlen = sdslen(s);\n\n    if (len <= curlen) return s;\n    s = sdsMakeRoomFor(s,len-curlen);\n    if (s == NULL) return NULL;\n\n    /* Make sure added region doesn't contain garbage */\n    memset(s+curlen,0,(len-curlen+1)); /* also set trailing \\0 byte */\n    sdssetlen(s, len);\n    return s;\n}\n\n/* Append the specified binary-safe string pointed by 't' of 'len' bytes to the\n * end of the specified sds string 's'.\n *\n * After the call, the passed sds string is no longer valid and all the\n * references must be substituted with the new pointer returned by the call. */\nsds sdscatlen(sds s, const void *t, size_t len) {\n    size_t curlen = sdslen(s);\n\n    s = sdsMakeRoomFor(s,len);\n    if (s == NULL) return NULL;\n    memcpy(s+curlen, t, len);\n    sdssetlen(s, curlen+len);\n    s[curlen+len] = '\\0';\n    return s;\n}\n\n/* Append the specified null terminated C string to the sds string 's'.\n *\n * After the call, the passed sds string is no longer valid and all the\n * references must be substituted with the new pointer returned by the call. */\nsds sdscat(sds s, const char *t) {\n    return sdscatlen(s, t, strlen(t));\n}\n\n/* Append the specified sds 't' to the existing sds 's'.\n *\n * After the call, the modified sds string is no longer valid and all the\n * references must be substituted with the new pointer returned by the call. */\nsds sdscatsds(sds s, const sds t) {\n    return sdscatlen(s, t, sdslen(t));\n}\n\n/* Destructively modify the sds string 's' to hold the specified binary\n * safe string pointed by 't' of length 'len' bytes. */\nsds sdscpylen(sds s, const char *t, size_t len) {\n    if (sdsalloc(s) < len) {\n        s = sdsMakeRoomFor(s,len-sdslen(s));\n        if (s == NULL) return NULL;\n    }\n    memcpy(s, t, len);\n    s[len] = '\\0';\n    sdssetlen(s, len);\n    return s;\n}\n\n/* Like sdscpylen() but 't' must be a null-termined string so that the length\n * of the string is obtained with strlen(). */\nsds sdscpy(sds s, const char *t) {\n    return sdscpylen(s, t, strlen(t));\n}\n\n/* Helper for sdscatlonglong() doing the actual number -> string\n * conversion. 's' must point to a string with room for at least\n * SDS_LLSTR_SIZE bytes.\n *\n * The function returns the length of the null-terminated string\n * representation stored at 's'. */\n#define SDS_LLSTR_SIZE 21\nint sdsll2str(char *s, long long value) {\n    char *p, aux;\n    unsigned long long v;\n    size_t l;\n\n    /* Generate the string representation, this method produces\n     * a reversed string. */\n    v = (value < 0) ? -value : value;\n    p = s;\n    do {\n        *p++ = '0'+(v%10);\n        v /= 10;\n    } while(v);\n    if (value < 0) *p++ = '-';\n\n    /* Compute length and add null term. */\n    l = p-s;\n    *p = '\\0';\n\n    /* Reverse the string. */\n    p--;\n    while(s < p) {\n        aux = *s;\n        *s = *p;\n        *p = aux;\n        s++;\n        p--;\n    }\n    return l;\n}\n\n/* Identical sdsll2str(), but for unsigned long long type. */\nint sdsull2str(char *s, unsigned long long v) {\n    char *p, aux;\n    size_t l;\n\n    /* Generate the string representation, this method produces\n     * a reversed string. */\n    p = s;\n    do {\n        *p++ = '0'+(v%10);\n        v /= 10;\n    } while(v);\n\n    /* Compute length and add null term. */\n    l = p-s;\n    *p = '\\0';\n\n    /* Reverse the string. */\n    p--;\n    while(s < p) {\n        aux = *s;\n        *s = *p;\n        *p = aux;\n        s++;\n        p--;\n    }\n    return l;\n}\n\n/* Create an sds string from a long long value. It is much faster than:\n *\n * sdscatprintf(sdsempty(),\"%lld\\n\", value);\n */\nsds sdsfromlonglong(long long value) {\n    char buf[SDS_LLSTR_SIZE];\n    int len = sdsll2str(buf,value);\n\n    return sdsnewlen(buf,len);\n}\n\n/* Like sdscatprintf() but gets va_list instead of being variadic. */\nsds sdscatvprintf(sds s, const char *fmt, va_list ap) {\n    va_list cpy;\n    char staticbuf[1024], *buf = staticbuf, *t;\n    size_t buflen = strlen(fmt)*2;\n    int bufstrlen;\n\n    /* We try to start using a static buffer for speed.\n     * If not possible we revert to heap allocation. */\n    if (buflen > sizeof(staticbuf)) {\n        buf = s_malloc(buflen, MALLOC_SHARED);\n        if (buf == NULL) return NULL;\n    } else {\n        buflen = sizeof(staticbuf);\n    }\n\n    /* Alloc enough space for buffer and \\0 after failing to\n     * fit the string in the current buffer size. */\n    while(1) {\n        va_copy(cpy,ap);\n        bufstrlen = vsnprintf(buf, buflen, fmt, cpy);\n        va_end(cpy);\n        if (bufstrlen < 0) {\n            if (buf != staticbuf) s_free(buf);\n            return NULL;\n        }\n        if (((size_t)bufstrlen) >= buflen) {\n            if (buf != staticbuf) s_free(buf);\n            buflen = ((size_t)bufstrlen) + 1;\n            buf = s_malloc(buflen, MALLOC_LOCAL);\n            if (buf == NULL) return NULL;\n            continue;\n        }\n        break;\n    }\n\n    /* Finally concat the obtained string to the SDS string and return it. */\n    t = sdscatlen(s, buf, bufstrlen);\n    if (buf != staticbuf) s_free(buf);\n    return t;\n}\n\n/* Append to the sds string 's' a string obtained using printf-alike format\n * specifier.\n *\n * After the call, the modified sds string is no longer valid and all the\n * references must be substituted with the new pointer returned by the call.\n *\n * Example:\n *\n * s = sdsnew(\"Sum is: \");\n * s = sdscatprintf(s,\"%d+%d = %d\",a,b,a+b).\n *\n * Often you need to create a string from scratch with the printf-alike\n * format. When this is the need, just use sdsempty() as the target string:\n *\n * s = sdscatprintf(sdsempty(), \"... your format ...\", args);\n */\nsds sdscatprintf(sds s, const char *fmt, ...) {\n    va_list ap;\n    char *t;\n    va_start(ap, fmt);\n    t = sdscatvprintf(s,fmt,ap);\n    va_end(ap);\n    return t;\n}\n\n/* This function is similar to sdscatprintf, but much faster as it does\n * not rely on sprintf() family functions implemented by the libc that\n * are often very slow. Moreover directly handling the sds string as\n * new data is concatenated provides a performance improvement.\n *\n * However this function only handles an incompatible subset of printf-alike\n * format specifiers:\n *\n * %s - C String\n * %S - SDS string\n * %i - signed int\n * %I - 64 bit signed integer (long long, int64_t)\n * %u - unsigned int\n * %U - 64 bit unsigned integer (unsigned long long, uint64_t)\n * %% - Verbatim \"%\" character.\n */\nsds sdscatfmt(sds s, char const *fmt, ...) {\n    size_t initlen = sdslen(s);\n    const char *f = fmt;\n    long i;\n    va_list ap;\n\n    /* To avoid continuous reallocations, let's start with a buffer that\n     * can hold at least two times the format string itself. It's not the\n     * best heuristic but seems to work in practice. */\n    s = sdsMakeRoomFor(s, strlen(fmt)*2);\n    va_start(ap,fmt);\n    f = fmt;    /* Next format specifier byte to process. */\n    i = initlen; /* Position of the next byte to write to dest str. */\n    while(*f) {\n        char next, *str;\n        size_t l;\n        long long num;\n        unsigned long long unum;\n\n        /* Make sure there is always space for at least 1 char. */\n        if (sdsavail(s)==0) {\n            s = sdsMakeRoomFor(s,1);\n        }\n\n        switch(*f) {\n        case '%':\n            next = *(f+1);\n            f++;\n            switch(next) {\n            case 's':\n            case 'S':\n                str = va_arg(ap,char*);\n                l = (next == 's') ? strlen(str) : sdslen(str);\n                if (sdsavail(s) < l) {\n                    s = sdsMakeRoomFor(s,l);\n                }\n                memcpy(s+i,str,l);\n                sdsinclen(s,l);\n                i += l;\n                break;\n            case 'i':\n            case 'I':\n                if (next == 'i')\n                    num = va_arg(ap,int);\n                else\n                    num = va_arg(ap,long long);\n                {\n                    char buf[SDS_LLSTR_SIZE];\n                    l = sdsll2str(buf,num);\n                    if (sdsavail(s) < l) {\n                        s = sdsMakeRoomFor(s,l);\n                    }\n                    memcpy(s+i,buf,l);\n                    sdsinclen(s,l);\n                    i += l;\n                }\n                break;\n            case 'u':\n            case 'U':\n                if (next == 'u')\n                    unum = va_arg(ap,unsigned int);\n                else\n                    unum = va_arg(ap,unsigned long long);\n                {\n                    char buf[SDS_LLSTR_SIZE];\n                    l = sdsull2str(buf,unum);\n                    if (sdsavail(s) < l) {\n                        s = sdsMakeRoomFor(s,l);\n                    }\n                    memcpy(s+i,buf,l);\n                    sdsinclen(s,l);\n                    i += l;\n                }\n                break;\n            default: /* Handle %% and generally %<unknown>. */\n                s[i++] = next;\n                sdsinclen(s,1);\n                break;\n            }\n            break;\n        default:\n            s[i++] = *f;\n            sdsinclen(s,1);\n            break;\n        }\n        f++;\n    }\n    va_end(ap);\n\n    /* Add null-term */\n    s[i] = '\\0';\n    return s;\n}\n\n/* Remove the part of the string from left and from right composed just of\n * contiguous characters found in 'cset', that is a null terminted C string.\n *\n * After the call, the modified sds string is no longer valid and all the\n * references must be substituted with the new pointer returned by the call.\n *\n * Example:\n *\n * s = sdsnew(\"AA...AA.a.aa.aHelloWorld     :::\");\n * s = sdstrim(s,\"Aa. :\");\n * printf(\"%s\\n\", s);\n *\n * Output will be just \"HelloWorld\".\n */\nsds sdstrim(sds s, const char *cset) {\n    char *start, *end, *sp, *ep;\n    size_t len;\n\n    sp = start = s;\n    ep = end = s+sdslen(s)-1;\n    while(sp <= end && strchr(cset, *sp)) sp++;\n    while(ep > sp && strchr(cset, *ep)) ep--;\n    len = (sp > ep) ? 0 : ((ep-sp)+1);\n    if (s != sp) memmove(s, sp, len);\n    s[len] = '\\0';\n    sdssetlen(s,len);\n    return s;\n}\n\n/* Changes the input string to be a subset of the original.\n * It does not release the free space in the string, so a call to\n * sdsRemoveFreeSpace may be wise after. */\nvoid sdssubstr(sds s, size_t start, size_t len) {\n    /* Clamp out of range input */\n    size_t oldlen = sdslen(s);\n    if (start >= oldlen) start = len = 0;\n    if (len > oldlen-start) len = oldlen-start;\n\n    /* Move the data */\n    if (len) memmove(s, s+start, len);\n    s[len] = 0;\n    sdssetlen(s,len);\n}\n\n/* Turn the string into a smaller (or equal) string containing only the\n * substring specified by the 'start' and 'end' indexes.\n *\n * start and end can be negative, where -1 means the last character of the\n * string, -2 the penultimate character, and so forth.\n *\n * The interval is inclusive, so the start and end characters will be part\n * of the resulting string.\n *\n * The string is modified in-place.\n *\n * NOTE: this function can be misleading and can have unexpected behaviour,\n * specifically when you want the length of the new string to be 0.\n * Having start==end will result in a string with one character.\n * please consider using sdssubstr instead.\n *\n * Example:\n *\n * s = sdsnew(\"Hello World\");\n * sdsrange(s,1,-1); => \"ello World\"\n */\nvoid sdsrange(sds s, ssize_t start, ssize_t end) {\n    size_t newlen, len = sdslen(s);\n    if (len == 0) return;\n    if (start < 0)\n        start = len + start;\n    if (end < 0)\n        end = len + end;\n    newlen = (start > end) ? 0 : (end-start)+1;\n    sdssubstr(s, start, newlen);\n}\n\n/* Apply tolower() to every character of the sds string 's'. */\nvoid sdstolower(sds s) {\n    size_t len = sdslen(s), j;\n\n    for (j = 0; j < len; j++) s[j] = tolower(s[j]);\n}\n\n/* Apply toupper() to every character of the sds string 's'. */\nvoid sdstoupper(sds s) {\n    size_t len = sdslen(s), j;\n\n    for (j = 0; j < len; j++) s[j] = toupper(s[j]);\n}\n\n/* Compare two sds strings s1 and s2 with memcmp().\n *\n * Return value:\n *\n *     positive if s1 > s2.\n *     negative if s1 < s2.\n *     0 if s1 and s2 are exactly the same binary string.\n *\n * If two strings share exactly the same prefix, but one of the two has\n * additional characters, the longer string is considered to be greater than\n * the smaller one. */\nint sdscmp(const char *s1, const char *s2) {\n    size_t l1, l2, minlen;\n    int cmp;\n\n    l1 = sdslen(s1);\n    l2 = sdslen(s2);\n    minlen = (l1 < l2) ? l1 : l2;\n    cmp = memcmp(s1,s2,minlen);\n    if (cmp == 0) return l1>l2? 1: (l1<l2? -1: 0);\n    return cmp;\n}\n\n/* Split 's' with separator in 'sep'. An array\n * of sds strings is returned. *count will be set\n * by reference to the number of tokens returned.\n *\n * On out of memory, zero length string, zero length\n * separator, NULL is returned.\n *\n * Note that 'sep' is able to split a string using\n * a multi-character separator. For example\n * sdssplit(\"foo_-_bar\",\"_-_\"); will return two\n * elements \"foo\" and \"bar\".\n *\n * This version of the function is binary-safe but\n * requires length arguments. sdssplit() is just the\n * same function but for zero-terminated strings.\n */\nsds *sdssplitlen(const char *s, ssize_t len, const char *sep, int seplen, int *count) {\n    int elements = 0, slots = 5;\n    long start = 0, j;\n    sds *tokens;\n\n    if (seplen < 1 || len < 0) return NULL;\n\n    tokens = s_malloc(sizeof(sds)*slots, MALLOC_SHARED);\n    if (tokens == NULL) return NULL;\n\n    if (len == 0) {\n        *count = 0;\n        return tokens;\n    }\n    for (j = 0; j < (len-(seplen-1)); j++) {\n        /* make sure there is room for the next element and the final one */\n        if (slots < elements+2) {\n            sds *newtokens;\n\n            slots *= 2;\n            newtokens = s_realloc(tokens,sizeof(sds)*slots, MALLOC_SHARED);\n            if (newtokens == NULL) goto cleanup;\n            tokens = newtokens;\n        }\n        /* search the separator */\n        if ((seplen == 1 && *(s+j) == sep[0]) || (memcmp(s+j,sep,seplen) == 0)) {\n            tokens[elements] = sdsnewlen(s+start,j-start);\n            if (tokens[elements] == NULL) goto cleanup;\n            elements++;\n            start = j+seplen;\n            j = j+seplen-1; /* skip the separator */\n        }\n    }\n    /* Add the final element. We are sure there is room in the tokens array. */\n    tokens[elements] = sdsnewlen(s+start,len-start);\n    if (tokens[elements] == NULL) goto cleanup;\n    elements++;\n    *count = elements;\n    return tokens;\n\ncleanup:\n    {\n        int i;\n        for (i = 0; i < elements; i++) sdsfree(tokens[i]);\n        s_free(tokens);\n        *count = 0;\n        return NULL;\n    }\n}\n\n/* Free the result returned by sdssplitlen(), or do nothing if 'tokens' is NULL. */\nvoid sdsfreesplitres(sds *tokens, int count) {\n    if (!tokens) return;\n    while(count--)\n        sdsfree(tokens[count]);\n    s_free(tokens);\n}\n\n/* Append to the sds string \"s\" an escaped string representation where\n * all the non-printable characters (tested with isprint()) are turned into\n * escapes in the form \"\\n\\r\\a....\" or \"\\x<hex-number>\".\n *\n * After the call, the modified sds string is no longer valid and all the\n * references must be substituted with the new pointer returned by the call. */\nsds sdscatrepr(sds s, const char *p, size_t len) {\n    s = sdscatlen(s,\"\\\"\",1);\n    while(len--) {\n        switch(*p) {\n        case '\\\\':\n        case '\"':\n            s = sdscatprintf(s,\"\\\\%c\",*p);\n            break;\n        case '\\n': s = sdscatlen(s,\"\\\\n\",2); break;\n        case '\\r': s = sdscatlen(s,\"\\\\r\",2); break;\n        case '\\t': s = sdscatlen(s,\"\\\\t\",2); break;\n        case '\\a': s = sdscatlen(s,\"\\\\a\",2); break;\n        case '\\b': s = sdscatlen(s,\"\\\\b\",2); break;\n        default:\n            if (isprint(*p))\n                s = sdscatprintf(s,\"%c\",*p);\n            else\n                s = sdscatprintf(s,\"\\\\x%02x\",(unsigned char)*p);\n            break;\n        }\n        p++;\n    }\n    return sdscatlen(s,\"\\\"\",1);\n}\n\n/* Helper function for sdssplitargs() that returns non zero if 'c'\n * is a valid hex digit. */\nint is_hex_digit(char c) {\n    return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') ||\n           (c >= 'A' && c <= 'F');\n}\n\n/* Helper function for sdssplitargs() that converts a hex digit into an\n * integer from 0 to 15 */\nint hex_digit_to_int(char c) {\n    switch(c) {\n    case '0': return 0;\n    case '1': return 1;\n    case '2': return 2;\n    case '3': return 3;\n    case '4': return 4;\n    case '5': return 5;\n    case '6': return 6;\n    case '7': return 7;\n    case '8': return 8;\n    case '9': return 9;\n    case 'a': case 'A': return 10;\n    case 'b': case 'B': return 11;\n    case 'c': case 'C': return 12;\n    case 'd': case 'D': return 13;\n    case 'e': case 'E': return 14;\n    case 'f': case 'F': return 15;\n    default: return 0;\n    }\n}\n\n/* Split a line into arguments, where every argument can be in the\n * following programming-language REPL-alike form:\n *\n * foo bar \"newline are supported\\n\" and \"\\xff\\x00otherstuff\"\n *\n * The number of arguments is stored into *argc, and an array\n * of sds is returned.\n *\n * The caller should free the resulting array of sds strings with\n * sdsfreesplitres().\n *\n * Note that sdscatrepr() is able to convert back a string into\n * a quoted string in the same format sdssplitargs() is able to parse.\n *\n * The function returns the allocated tokens on success, even when the\n * input string is empty, or NULL if the input contains unbalanced\n * quotes or closed quotes followed by non space characters\n * as in: \"foo\"bar or \"foo'\n */\nsds *sdssplitargs(const char *line, int *argc) {\n    const char *p = line;\n    char *current = NULL;\n    char **vector = NULL;\n\n    *argc = 0;\n    while(1) {\n        /* skip blanks */\n        while(*p && isspace(*p)) p++;\n        if (*p) {\n            /* get a token */\n            int inq=0;  /* set to 1 if we are in \"quotes\" */\n            int insq=0; /* set to 1 if we are in 'single quotes' */\n            int done=0;\n\n            if (current == NULL) current = sdsempty();\n            while(!done) {\n                if (inq) {\n                    if (*p == '\\\\' && *(p+1) == 'x' &&\n                                             is_hex_digit(*(p+2)) &&\n                                             is_hex_digit(*(p+3)))\n                    {\n                        unsigned char byte;\n\n                        byte = (hex_digit_to_int(*(p+2))*16)+\n                                hex_digit_to_int(*(p+3));\n                        current = sdscatlen(current,(char*)&byte,1);\n                        p += 3;\n                    } else if (*p == '\\\\' && *(p+1)) {\n                        char c;\n\n                        p++;\n                        switch(*p) {\n                        case 'n': c = '\\n'; break;\n                        case 'r': c = '\\r'; break;\n                        case 't': c = '\\t'; break;\n                        case 'b': c = '\\b'; break;\n                        case 'a': c = '\\a'; break;\n                        default: c = *p; break;\n                        }\n                        current = sdscatlen(current,&c,1);\n                    } else if (*p == '\"') {\n                        /* closing quote must be followed by a space or\n                         * nothing at all. */\n                        if (*(p+1) && !isspace(*(p+1))) goto err;\n                        done=1;\n                    } else if (!*p) {\n                        /* unterminated quotes */\n                        goto err;\n                    } else {\n                        current = sdscatlen(current,p,1);\n                    }\n                } else if (insq) {\n                    if (*p == '\\\\' && *(p+1) == '\\'') {\n                        p++;\n                        current = sdscatlen(current,\"'\",1);\n                    } else if (*p == '\\'') {\n                        /* closing quote must be followed by a space or\n                         * nothing at all. */\n                        if (*(p+1) && !isspace(*(p+1))) goto err;\n                        done=1;\n                    } else if (!*p) {\n                        /* unterminated quotes */\n                        goto err;\n                    } else {\n                        current = sdscatlen(current,p,1);\n                    }\n                } else {\n                    switch(*p) {\n                    case ' ':\n                    case '\\n':\n                    case '\\r':\n                    case '\\t':\n                    case '\\0':\n                        done=1;\n                        break;\n                    case '\"':\n                        inq=1;\n                        break;\n                    case '\\'':\n                        insq=1;\n                        break;\n                    default:\n                        current = sdscatlen(current,p,1);\n                        break;\n                    }\n                }\n                if (*p) p++;\n            }\n            /* add the token to the vector */\n            vector = s_realloc(vector,((*argc)+1)*sizeof(char*), MALLOC_SHARED);\n            vector[*argc] = current;\n            (*argc)++;\n            current = NULL;\n        } else {\n            /* Even on empty input string return something not NULL. */\n            if (vector == NULL) vector = s_malloc(sizeof(void*), MALLOC_SHARED);\n            return vector;\n        }\n    }\n\nerr:\n    while((*argc)--)\n        sdsfree(vector[*argc]);\n    s_free(vector);\n    if (current) sdsfree(current);\n    *argc = 0;\n    return NULL;\n}\n\n/* Modify the string substituting all the occurrences of the set of\n * characters specified in the 'from' string to the corresponding character\n * in the 'to' array.\n *\n * For instance: sdsmapchars(mystring, \"ho\", \"01\", 2)\n * will have the effect of turning the string \"hello\" into \"0ell1\".\n *\n * The function returns the sds string pointer, that is always the same\n * as the input pointer since no resize is needed. */\nsds sdsmapchars(sds s, const char *from, const char *to, size_t setlen) {\n    size_t j, i, l = sdslen(s);\n\n    for (j = 0; j < l; j++) {\n        for (i = 0; i < setlen; i++) {\n            if (s[j] == from[i]) {\n                s[j] = to[i];\n                break;\n            }\n        }\n    }\n    return s;\n}\n\n/* Join an array of C strings using the specified separator (also a C string).\n * Returns the result as an sds string. */\nsds sdsjoin(char **argv, int argc, const char *sep) {\n    sds join = sdsempty();\n    int j;\n\n    for (j = 0; j < argc; j++) {\n        join = sdscat(join, argv[j]);\n        if (j != argc-1) join = sdscat(join,sep);\n    }\n    return join;\n}\n\n/* Like sdsjoin, but joins an array of SDS strings. */\nsds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen) {\n    sds join = sdsempty();\n    int j;\n\n    for (j = 0; j < argc; j++) {\n        join = sdscatsds(join, argv[j]);\n        if (j != argc-1) join = sdscatlen(join,sep,seplen);\n    }\n    return join;\n}\n\n/* Wrappers to the allocators used by SDS. Note that SDS will actually\n * just use the macros defined into sdsalloc.h in order to avoid to pay\n * the overhead of function calls. Here we define these wrappers only for\n * the programs SDS is linked to, if they want to touch the SDS internals\n * even if they use a different allocator. */\nvoid *sds_malloc(size_t size) { return s_malloc(size, MALLOC_SHARED); }\nvoid *sds_realloc(void *ptr, size_t size) { return s_realloc(ptr,size, MALLOC_SHARED); }\nvoid sds_free(void *ptr) { s_free(ptr); }\n\n/* Perform expansion of a template string and return the result as a newly\n * allocated sds.\n *\n * Template variables are specified using curly brackets, e.g. {variable}.\n * An opening bracket can be quoted by repeating it twice.\n */\nsds sdstemplate(const char *template, sdstemplate_callback_t cb_func, void *cb_arg)\n{\n    sds res = sdsempty();\n    const char *p = template;\n\n    while (*p) {\n        /* Find next variable, copy everything until there */\n        const char *sv = strchr(p, '{');\n        if (!sv) {\n            /* Not found: copy till rest of template and stop */\n            res = sdscat(res, p);\n            break;\n        } else if (sv > p) {\n            /* Found: copy anything up to the begining of the variable */\n            res = sdscatlen(res, p, sv - p);\n        }\n\n        /* Skip into variable name, handle premature end or quoting */\n        sv++;\n        if (!*sv) goto error;       /* Premature end of template */\n        if (*sv == '{') {\n            /* Quoted '{' */\n            p = sv + 1;\n            res = sdscat(res, \"{\");\n            continue;\n        }\n\n        /* Find end of variable name, handle premature end of template */\n        const char *ev = strchr(sv, '}');\n        if (!ev) goto error;\n\n        /* Pass variable name to callback and obtain value. If callback failed,\n         * abort. */\n        sds varname = sdsnewlen(sv, ev - sv);\n        sds value = cb_func(varname, cb_arg);\n        sdsfree(varname);\n        if (!value) goto error;\n\n        /* Append value to result and continue */\n        res = sdscat(res, value);\n        sdsfree(value);\n        p = ev + 1;\n    }\n\n    return res;\n\nerror:\n    sdsfree(res);\n    return NULL;\n}\n\n#ifdef REDIS_TEST\n#include <stdio.h>\n#include <limits.h>\n#include \"testhelp.h\"\n\n#define UNUSED(x) (void)(x)\n\nstatic sds sdsTestTemplateCallback(sds varname, void *arg) {\n    UNUSED(arg);\n    static const char *_var1 = \"variable1\";\n    static const char *_var2 = \"variable2\";\n\n    if (!strcmp(varname, _var1)) return sdsnew(\"value1\");\n    else if (!strcmp(varname, _var2)) return sdsnew(\"value2\");\n    else return NULL;\n}\n\nint sdsTest(int argc, char **argv, int accurate) {\n    UNUSED(argc);\n    UNUSED(argv);\n    UNUSED(accurate);\n\n    {\n        sds x = sdsnew(\"foo\"), y;\n\n        test_cond(\"Create a string and obtain the length\",\n            sdslen(x) == 3 && memcmp(x,\"foo\\0\",4) == 0);\n\n        sdsfree(x);\n        x = sdsnewlen(\"foo\",2);\n        test_cond(\"Create a string with specified length\",\n            sdslen(x) == 2 && memcmp(x,\"fo\\0\",3) == 0);\n\n        x = sdscat(x,\"bar\");\n        test_cond(\"Strings concatenation\",\n            sdslen(x) == 5 && memcmp(x,\"fobar\\0\",6) == 0);\n\n        x = sdscpy(x,\"a\");\n        test_cond(\"sdscpy() against an originally longer string\",\n            sdslen(x) == 1 && memcmp(x,\"a\\0\",2) == 0);\n\n        x = sdscpy(x,\"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\");\n        test_cond(\"sdscpy() against an originally shorter string\",\n            sdslen(x) == 33 &&\n            memcmp(x,\"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\\0\",33) == 0);\n\n        sdsfree(x);\n        x = sdscatprintf(sdsempty(),\"%d\",123);\n        test_cond(\"sdscatprintf() seems working in the base case\",\n            sdslen(x) == 3 && memcmp(x,\"123\\0\",4) == 0);\n\n        sdsfree(x);\n        x = sdscatprintf(sdsempty(),\"a%cb\",0);\n        test_cond(\"sdscatprintf() seems working with \\\\0 inside of result\",\n            sdslen(x) == 3 && memcmp(x,\"a\\0\"\"b\\0\",4) == 0);\n\n        {\n            sdsfree(x);\n            char etalon[1024*1024];\n            for (size_t i = 0; i < sizeof(etalon); i++) {\n                etalon[i] = '0';\n            }\n            x = sdscatprintf(sdsempty(),\"%0*d\",(int)sizeof(etalon),0);\n            test_cond(\"sdscatprintf() can print 1MB\",\n                sdslen(x) == sizeof(etalon) && memcmp(x,etalon,sizeof(etalon)) == 0);\n        }\n\n        sdsfree(x);\n        x = sdscatprintf(sdsempty(),\"a%cb\",0);\n        test_cond(\"sdscatprintf() seems working with \\\\0 inside of result\",\n            sdslen(x) == 3 && memcmp(x,\"a\\0\"\"b\\0\",4) == 0)\n\n        {\n            sdsfree(x);\n            char etalon[1024*1024];\n            for (size_t i = 0; i < sizeof(etalon); i++) {\n                etalon[i] = '0';\n            }\n            x = sdscatprintf(sdsempty(),\"%0*d\",(int)sizeof(etalon),0);\n            test_cond(\"sdscatprintf() can print 1MB\",\n                sdslen(x) == sizeof(etalon) && memcmp(x,etalon,sizeof(etalon)) == 0)\n        }\n\n        sdsfree(x);\n        x = sdsnew(\"--\");\n        x = sdscatfmt(x, \"Hello %s World %I,%I--\", \"Hi!\", LLONG_MIN,LLONG_MAX);\n        test_cond(\"sdscatfmt() seems working in the base case\",\n            sdslen(x) == 60 &&\n            memcmp(x,\"--Hello Hi! World -9223372036854775808,\"\n                     \"9223372036854775807--\",60) == 0);\n        printf(\"[%s]\\n\",x);\n\n        sdsfree(x);\n        x = sdsnew(\"--\");\n        x = sdscatfmt(x, \"%u,%U--\", UINT_MAX, ULLONG_MAX);\n        test_cond(\"sdscatfmt() seems working with unsigned numbers\",\n            sdslen(x) == 35 &&\n            memcmp(x,\"--4294967295,18446744073709551615--\",35) == 0);\n\n        sdsfree(x);\n        x = sdsnew(\" x \");\n        sdstrim(x,\" x\");\n        test_cond(\"sdstrim() works when all chars match\",\n            sdslen(x) == 0);\n\n        sdsfree(x);\n        x = sdsnew(\" x \");\n        sdstrim(x,\" \");\n        test_cond(\"sdstrim() works when a single char remains\",\n            sdslen(x) == 1 && x[0] == 'x');\n\n        sdsfree(x);\n        x = sdsnew(\"xxciaoyyy\");\n        sdstrim(x,\"xy\");\n        test_cond(\"sdstrim() correctly trims characters\",\n            sdslen(x) == 4 && memcmp(x,\"ciao\\0\",5) == 0);\n\n        y = sdsdup(x);\n        sdsrange(y,1,1);\n        test_cond(\"sdsrange(...,1,1)\",\n            sdslen(y) == 1 && memcmp(y,\"i\\0\",2) == 0);\n\n        sdsfree(y);\n        y = sdsdup(x);\n        sdsrange(y,1,-1);\n        test_cond(\"sdsrange(...,1,-1)\",\n            sdslen(y) == 3 && memcmp(y,\"iao\\0\",4) == 0);\n\n        sdsfree(y);\n        y = sdsdup(x);\n        sdsrange(y,-2,-1);\n        test_cond(\"sdsrange(...,-2,-1)\",\n            sdslen(y) == 2 && memcmp(y,\"ao\\0\",3) == 0);\n\n        sdsfree(y);\n        y = sdsdup(x);\n        sdsrange(y,2,1);\n        test_cond(\"sdsrange(...,2,1)\",\n            sdslen(y) == 0 && memcmp(y,\"\\0\",1) == 0);\n\n        sdsfree(y);\n        y = sdsdup(x);\n        sdsrange(y,1,100);\n        test_cond(\"sdsrange(...,1,100)\",\n            sdslen(y) == 3 && memcmp(y,\"iao\\0\",4) == 0);\n\n        sdsfree(y);\n        y = sdsdup(x);\n        sdsrange(y,100,100);\n        test_cond(\"sdsrange(...,100,100)\",\n            sdslen(y) == 0 && memcmp(y,\"\\0\",1) == 0);\n\n        sdsfree(y);\n        y = sdsdup(x);\n        sdsrange(y,4,6);\n        test_cond(\"sdsrange(...,4,6)\",\n            sdslen(y) == 0 && memcmp(y,\"\\0\",1) == 0);\n\n        sdsfree(y);\n        y = sdsdup(x);\n        sdsrange(y,3,6);\n        test_cond(\"sdsrange(...,3,6)\",\n            sdslen(y) == 1 && memcmp(y,\"o\\0\",2) == 0);\n\n        sdsfree(y);\n        sdsfree(x);\n        x = sdsnew(\"foo\");\n        y = sdsnew(\"foa\");\n        test_cond(\"sdscmp(foo,foa)\", sdscmp(x,y) > 0);\n\n        sdsfree(y);\n        sdsfree(x);\n        x = sdsnew(\"bar\");\n        y = sdsnew(\"bar\");\n        test_cond(\"sdscmp(bar,bar)\", sdscmp(x,y) == 0);\n\n        sdsfree(y);\n        sdsfree(x);\n        x = sdsnew(\"aar\");\n        y = sdsnew(\"bar\");\n        test_cond(\"sdscmp(bar,bar)\", sdscmp(x,y) < 0);\n\n        sdsfree(y);\n        sdsfree(x);\n        x = sdsnewlen(\"\\a\\n\\0foo\\r\",7);\n        y = sdscatrepr(sdsempty(),x,sdslen(x));\n        test_cond(\"sdscatrepr(...data...)\",\n            memcmp(y,\"\\\"\\\\a\\\\n\\\\x00foo\\\\r\\\"\",15) == 0);\n\n        {\n            unsigned int oldfree;\n            char *p;\n            int i;\n            size_t step = 10, j;\n\n            sdsfree(x);\n            sdsfree(y);\n            x = sdsnew(\"0\");\n            test_cond(\"sdsnew() free/len buffers\", sdslen(x) == 1 && sdsavail(x) == 0);\n\n            /* Run the test a few times in order to hit the first two\n             * SDS header types. */\n            for (i = 0; i < 10; i++) {\n                size_t oldlen = sdslen(x);\n                x = sdsMakeRoomFor(x,step);\n                int type = x[-1]&SDS_TYPE_MASK;\n\n                test_cond(\"sdsMakeRoomFor() len\", sdslen(x) == oldlen);\n                if (type != SDS_TYPE_5) {\n                    test_cond(\"sdsMakeRoomFor() free\", sdsavail(x) >= step);\n                    oldfree = sdsavail(x);\n                    UNUSED(oldfree);\n                }\n                p = x+oldlen;\n                for (j = 0; j < step; j++) {\n                    p[j] = 'A'+j;\n                }\n                sdsIncrLen(x,step);\n            }\n            test_cond(\"sdsMakeRoomFor() content\",\n                memcmp(\"0ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJ\",x,101) == 0);\n            test_cond(\"sdsMakeRoomFor() final length\",sdslen(x)==101);\n\n            sdsfree(x);\n        }\n\n        /* Simple template */\n        x = sdstemplate(\"v1={variable1} v2={variable2}\", sdsTestTemplateCallback, NULL);\n        test_cond(\"sdstemplate() normal flow\",\n                  memcmp(x,\"v1=value1 v2=value2\",19) == 0);\n        sdsfree(x);\n\n        /* Template with callback error */\n        x = sdstemplate(\"v1={variable1} v3={doesnotexist}\", sdsTestTemplateCallback, NULL);\n        test_cond(\"sdstemplate() with callback error\", x == NULL);\n\n        /* Template with empty var name */\n        x = sdstemplate(\"v1={\", sdsTestTemplateCallback, NULL);\n        test_cond(\"sdstemplate() with empty var name\", x == NULL);\n\n        /* Template with truncated var name */\n        x = sdstemplate(\"v1={start\", sdsTestTemplateCallback, NULL);\n        test_cond(\"sdstemplate() with truncated var name\", x == NULL);\n\n        /* Template with quoting */\n        x = sdstemplate(\"v1={{{variable1}} {{} v2={variable2}\", sdsTestTemplateCallback, NULL);\n        test_cond(\"sdstemplate() with quoting\",\n                  memcmp(x,\"v1={value1} {} v2=value2\",24) == 0);\n        sdsfree(x);\n    }\n    test_report();\n    return 0;\n}\n#endif\n"
  },
  {
    "path": "src/sds.h",
    "content": "/* SDSLib 2.0 -- A C dynamic strings library\n *\n * Copyright (c) 2006-2015, Salvatore Sanfilippo <antirez at gmail dot com>\n * Copyright (c) 2015, Oran Agra\n * Copyright (c) 2015, Redis Labs, Inc\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __SDS_H\n#define __SDS_H\n\n#define SDS_MAX_PREALLOC (1024*1024)\nextern const char *SDS_NOINIT;\n\n#include <sys/types.h>\n#include <stdarg.h>\n#include <stdint.h>\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\ntypedef char *sds;\n\n/* Note: sdshdr5 is never used, we just access the flags byte directly.\n * However is here to document the layout of type 5 SDS strings. */\nstruct __attribute__ ((__packed__)) sdshdr5 {\n    unsigned char flags; /* 3 lsb of type, and 5 msb of string length */\n#ifndef __cplusplus\n    char buf[];\n#endif\n};\nstruct __attribute__ ((__packed__)) sdshdr8 {\n    uint8_t len; /* used */\n    uint8_t alloc; /* excluding the header and null terminator */\n    unsigned char flags; /* 3 lsb of type, 5 unused bits */\n#ifndef __cplusplus\n    char buf[];\n#else\n    char *buf() {\n        return reinterpret_cast<char*>(this+1);\n    }\n#endif\n};\nstruct __attribute__ ((__packed__)) sdshdr16 {\n    uint16_t len; /* used */\n    uint16_t alloc; /* excluding the header and null terminator */\n    unsigned char flags; /* 3 lsb of type, 5 unused bits */\n#ifndef __cplusplus\n    char buf[];\n#endif\n};\nstruct __attribute__ ((__packed__)) sdshdr32 {\n    uint32_t len; /* used */\n    uint32_t alloc; /* excluding the header and null terminator */\n    unsigned char flags; /* 3 lsb of type, 5 unused bits */\n#ifndef __cplusplus\n    char buf[];\n#endif\n};\nstruct __attribute__ ((__packed__)) sdshdr64 {\n    uint64_t len; /* used */\n    uint64_t alloc; /* excluding the header and null terminator */\n    unsigned char flags; /* 3 lsb of type, 5 unused bits */\n#ifndef __cplusplus\n    char buf[];\n#endif\n};\n\nstruct __attribute__ ((__packed__)) sdshdrrefcount {\n    uint64_t len; /* used */\n    uint16_t refcount;\n    unsigned char flags; /* 3 lsb of type, 5 unused bits */\n#ifndef __cplusplus\n    char buf[];\n#endif\n};\n\n#define SDS_TYPE_5  0\n#define SDS_TYPE_8  1\n#define SDS_TYPE_16 2\n#define SDS_TYPE_32 3\n#define SDS_TYPE_64 4\n#define SDS_TYPE_REFCOUNTED 5\n#define SDS_TYPE_MASK 7\n#define SDS_TYPE_BITS 3\n#define SDS_HDR_VAR(T,s) struct sdshdr##T *sh = (struct sdshdr##T *)(((void*)((s)-(sizeof(struct sdshdr##T)))));\n#define SDS_HDR_VAR_REFCOUNTED(s) struct sdshdrrefcount *sh = (struct sdshdrrefcount *)(((void*)((s)-(sizeof(struct sdshdrrefcount)))));\n#define SDS_HDR(T,s) ((struct sdshdr##T *)((s)-(sizeof(struct sdshdr##T))))\n#define SDS_HDR_REFCOUNTED(s) ((struct sdshdrrefcount *)((s)-(sizeof(struct sdshdrrefcount))))\n#define SDS_TYPE_5_LEN(f) ((f)>>SDS_TYPE_BITS)\n\nstatic inline size_t sdslen(const char *s) {\n    unsigned char flags = s[-1];\n    int type = flags & SDS_TYPE_MASK;\n\n    if (__builtin_expect((type == SDS_TYPE_5), 1))\n    {\n        return SDS_TYPE_5_LEN(flags);\n    }\n    else\n    {\n        switch(type) {\n            case SDS_TYPE_8:\n                return SDS_HDR(8,s)->len;\n            case SDS_TYPE_16:\n                return SDS_HDR(16,s)->len;\n            case SDS_TYPE_32:\n                return SDS_HDR(32,s)->len;\n            case SDS_TYPE_64:\n                return SDS_HDR(64,s)->len;\n            case SDS_TYPE_REFCOUNTED:\n                return SDS_HDR_REFCOUNTED(s)->len;\n        }\n    }\n    return 0;\n}\n\nstatic inline size_t sdsavail(const char * s) {\n    unsigned char flags = s[-1];\n    switch(flags&SDS_TYPE_MASK) {\n        case SDS_TYPE_5: {\n            return 0;\n        }\n        case SDS_TYPE_8: {\n            SDS_HDR_VAR(8,s);\n            return sh->alloc - sh->len;\n        }\n        case SDS_TYPE_16: {\n            SDS_HDR_VAR(16,s);\n            return sh->alloc - sh->len;\n        }\n        case SDS_TYPE_32: {\n            SDS_HDR_VAR(32,s);\n            return sh->alloc - sh->len;\n        }\n        case SDS_TYPE_64: {\n            SDS_HDR_VAR(64,s);\n            return sh->alloc - sh->len;\n        }\n        case SDS_TYPE_REFCOUNTED: {\n            return 0;   // immutable\n        }\n    }\n    return 0;\n}\n\nstatic inline void sdssetlen(sds s, size_t newlen) {\n    unsigned char flags = s[-1];\n    switch(flags&SDS_TYPE_MASK) {\n        case SDS_TYPE_5:\n            {\n                unsigned char *fp = ((unsigned char*)s)-1;\n                *fp = SDS_TYPE_5 | (newlen << SDS_TYPE_BITS);\n            }\n            break;\n        case SDS_TYPE_8:\n            SDS_HDR(8,s)->len = newlen;\n            break;\n        case SDS_TYPE_16:\n            SDS_HDR(16,s)->len = newlen;\n            break;\n        case SDS_TYPE_32:\n            SDS_HDR(32,s)->len = newlen;\n            break;\n        case SDS_TYPE_64:\n            SDS_HDR(64,s)->len = newlen;\n            break;\n        case SDS_TYPE_REFCOUNTED:\n            SDS_HDR_REFCOUNTED(s)->len = newlen;\n            break;\n    }\n}\n\nstatic inline void sdsinclen(sds s, size_t inc) {\n    unsigned char flags = s[-1];\n    switch(flags&SDS_TYPE_MASK) {\n        case SDS_TYPE_5:\n            {\n                unsigned char *fp = ((unsigned char*)s)-1;\n                unsigned char newlen = SDS_TYPE_5_LEN(flags)+inc;\n                *fp = SDS_TYPE_5 | (newlen << SDS_TYPE_BITS);\n            }\n            break;\n        case SDS_TYPE_8:\n            SDS_HDR(8,s)->len += inc;\n            break;\n        case SDS_TYPE_16:\n            SDS_HDR(16,s)->len += inc;\n            break;\n        case SDS_TYPE_32:\n            SDS_HDR(32,s)->len += inc;\n            break;\n        case SDS_TYPE_64:\n            SDS_HDR(64,s)->len += inc;\n            break;\n        case SDS_TYPE_REFCOUNTED:\n            SDS_HDR_REFCOUNTED(s)->len += inc;\n            break;\n    }\n}\n\n/* sdsalloc() = sdsavail() + sdslen() */\nstatic inline size_t sdsalloc(const sds s) {\n    unsigned char flags = s[-1];\n    switch(flags&SDS_TYPE_MASK) {\n        case SDS_TYPE_5:\n            return SDS_TYPE_5_LEN(flags);\n        case SDS_TYPE_8:\n            return SDS_HDR(8,s)->alloc;\n        case SDS_TYPE_16:\n            return SDS_HDR(16,s)->alloc;\n        case SDS_TYPE_32:\n            return SDS_HDR(32,s)->alloc;\n        case SDS_TYPE_64:\n            return SDS_HDR(64,s)->alloc;\n        case SDS_TYPE_REFCOUNTED:\n            return SDS_HDR_REFCOUNTED(s)->len;\n    }\n    return 0;\n}\n\nstatic inline void sdssetalloc(sds s, size_t newlen) {\n    unsigned char flags = s[-1];\n    switch(flags&SDS_TYPE_MASK) {\n        case SDS_TYPE_5:\n            /* Nothing to do, this type has no total allocation info. */\n            break;\n        case SDS_TYPE_8:\n            SDS_HDR(8,s)->alloc = newlen;\n            break;\n        case SDS_TYPE_16:\n            SDS_HDR(16,s)->alloc = newlen;\n            break;\n        case SDS_TYPE_32:\n            SDS_HDR(32,s)->alloc = newlen;\n            break;\n        case SDS_TYPE_64:\n            SDS_HDR(64,s)->alloc = newlen;\n            break;\n        case SDS_TYPE_REFCOUNTED:\n            break;\n    }\n}\n\nstatic inline int sdsisshared(const char *s)\n{\n    unsigned char flags = s[-1];\n    return ((flags & SDS_TYPE_MASK) == SDS_TYPE_REFCOUNTED);\n}\n\nsds sdsnewlen(const void *init, ssize_t initlen);\nsds sdstrynewlen(const void *init, size_t initlen);\nsds sdsnew(const char *init);\nsds sdsempty(void);\nsds sdsdup(const char *s);\nsds sdsdupshared(const char *s);\nvoid sdsfree(const char *s);\nsds sdsgrowzero(sds s, size_t len);\nsds sdscatlen(sds s, const void *t, size_t len);\nsds sdscat(sds s, const char *t);\nsds sdscatsds(sds s, const sds t);\nsds sdscpylen(sds s, const char *t, size_t len);\nsds sdscpy(sds s, const char *t);\n\nsds sdscatvprintf(sds s, const char *fmt, va_list ap);\n#ifdef __GNUC__\nsds sdscatprintf(sds s, const char *fmt, ...)\n    __attribute__((format(printf, 2, 3)));\n#else\nsds sdscatprintf(sds s, const char *fmt, ...);\n#endif\n\nsds sdscatfmt(sds s, char const *fmt, ...);\nsds sdstrim(sds s, const char *cset);\nvoid sdssubstr(sds s, size_t start, size_t len);\nvoid sdsrange(sds s, ssize_t start, ssize_t end);\nvoid sdsupdatelen(sds s);\nvoid sdsclear(sds s);\nint sdscmp(const char *s1, const char *s2);\nsds *sdssplitlen(const char *s, ssize_t len, const char *sep, int seplen, int *count);\nvoid sdsfreesplitres(sds *tokens, int count);\nvoid sdstolower(sds s);\nvoid sdstoupper(sds s);\nsds sdsfromlonglong(long long value);\nsds sdscatrepr(sds s, const char *p, size_t len);\nsds *sdssplitargs(const char *line, int *argc);\nsds sdsmapchars(sds s, const char *from, const char *to, size_t setlen);\nsds sdsjoin(char **argv, int argc, const char *sep);\nsds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen);\n\n/* Callback for sdstemplate. The function gets called by sdstemplate\n * every time a variable needs to be expanded. The variable name is\n * provided as variable, and the callback is expected to return a\n * substitution value. Returning a NULL indicates an error.\n */\ntypedef sds (*sdstemplate_callback_t)(const sds variable, void *arg);\nsds sdstemplate(const char *_template, sdstemplate_callback_t cb_func, void *cb_arg);\n\n/* Low level functions exposed to the user API */\nsds sdsMakeRoomFor(sds s, size_t addlen);\nvoid sdsIncrLen(sds s, ssize_t incr);\nsds sdsRemoveFreeSpace(sds s);\nsize_t sdsAllocSize(sds s);\nvoid *sdsAllocPtr(sds s);\n\n/* Export the allocator used by SDS to the program using SDS.\n * Sometimes the program SDS is linked to, may use a different set of\n * allocators, but may want to allocate or free things that SDS will\n * respectively free or allocate. */\nvoid *sds_malloc(size_t size);\nvoid *sds_realloc(void *ptr, size_t size);\nvoid sds_free(void *ptr);\n\n#ifdef REDIS_TEST\nint sdsTest(int argc, char *argv[], int accurate);\n#endif\n\n#ifdef __cplusplus\n}\n\nclass sdsview\n{\nprotected:\n    sds m_str = nullptr;\n\n    sdsview() = default;    // Not allowed to create a sdsview directly with a nullptr\npublic:\n    sdsview(sds str)\n        : m_str(str)\n    {}\n\n    sdsview(const char *str)\n        : m_str((sds)str)\n    {}\n\n    bool operator<(const sdsview &other) const\n    {\n        return sdscmp(m_str, other.m_str) < 0;\n    }\n\n    bool operator==(const sdsview &other) const\n    {\n        return sdscmp(m_str, other.m_str) == 0;\n    }\n\n    bool operator==(const char *other) const\n    {\n        if (other == nullptr || m_str == nullptr)\n            return other == m_str;\n        return sdscmp(m_str, other) == 0;\n    }\n\n    char operator[](size_t idx) const\n    {\n        return m_str[idx];\n    }\n\n    size_t size() const\n    {\n        return sdslen(m_str);\n    }\n\n    const char *get() const { return m_str; }\n\n    explicit operator const char*() const { return m_str; }\n};\n\nclass sdsstring : public sdsview\n{\npublic:\n    sdsstring() = default;\n    explicit sdsstring(sds str)\n        : sdsview(str)\n    {}\n\n    sdsstring(const sdsstring &other)\n        : sdsview()\n    {\n        if (other.m_str != nullptr)\n            m_str = sdsdup(other.m_str);\n    }\n\n    sdsstring(const char *rgch, size_t cch)\n        : sdsview(sdsnewlen(rgch, cch))\n    {}\n\n    sdsstring(sdsstring &&other)\n        : sdsview(other.m_str)\n    {\n        other.m_str = nullptr;\n    }\n\n    sdsstring &operator=(const sdsstring &other)\n    {\n        sdsfree(m_str);\n        if (other.m_str != nullptr)\n            m_str = sdsdup(other.m_str);\n        else\n            m_str = nullptr;\n        return *this;\n    }\n\n    sdsstring &operator=(sds other)\n    {\n        sdsfree(m_str);\n        m_str = sdsdup(other);\n        return *this;\n    }\n\n    sdsstring &operator=(sdsstring &&other)\n    {\n        sds tmp = m_str;\n        m_str = other.m_str;\n        other.m_str = tmp;\n        return *this;\n    }\n\n    template<typename... Args>\n    sdsstring catfmt(const char *fmt, Args... args)\n    {\n        m_str = sdscatfmt(m_str, fmt, args...);\n        return *this;\n    }\n\n    sds release() {\n        sds sdsT = m_str;\n        m_str = nullptr;\n        return sdsT;\n    }\n\n    ~sdsstring()\n    {\n        sdsfree(m_str);\n    }\n};\n\nclass sdsimmutablestring : public sdsstring\n{\npublic:\n    sdsimmutablestring() = default;\n    explicit sdsimmutablestring(sds str)\n        : sdsstring(str)\n    {}\n\n    explicit sdsimmutablestring(const char *str)\n        : sdsstring((sds)str)\n    {}\n\n    sdsimmutablestring(const sdsimmutablestring &other)\n        : sdsstring(sdsdupshared(other.m_str))\n    {}\n\n    sdsimmutablestring(sdsimmutablestring &&other)\n        : sdsstring(other.m_str)\n    {\n        other.m_str = nullptr;\n    }\n\n    auto &operator=(const sdsimmutablestring &other)\n    {\n        sdsfree(m_str);\n        m_str = sdsdupshared(other.m_str);\n        return *this;\n    }\n};\n\n#endif\n\n#endif\n"
  },
  {
    "path": "src/sdsalloc.h",
    "content": "/* SDSLib 2.0 -- A C dynamic strings library\n *\n * Copyright (c) 2006-2015, Salvatore Sanfilippo <antirez at gmail dot com>\n * Copyright (c) 2015, Redis Labs, Inc\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n/* SDS allocator selection.\n *\n * This file is used in order to change the SDS allocator at compile time.\n * Just define the following defines to what you want to use. Also add\n * the include of your alternate allocator if needed (not needed in order\n * to use the default libc allocator). */\n\n#ifndef __SDS_ALLOC_H__\n#define __SDS_ALLOC_H__\n\n#include \"zmalloc.h\"\n#include \"storage.h\"\n#define s_malloc zmalloc\n#define s_realloc zrealloc\n#define s_trymalloc ztrymalloc\n#define s_tryrealloc ztryrealloc\n#define s_free zfree\n#define s_malloc_usable zmalloc_usable\n#define s_realloc_usable zrealloc_usable\n#define s_trymalloc_usable ztrymalloc_usable\n#define s_tryrealloc_usable ztryrealloc_usable\n#define s_free_usable zfree_usable\n\n#endif\n"
  },
  {
    "path": "src/sdscompat.h",
    "content": "/*\n * Copyright (c) 2020, Michael Grunder <michael dot grunder at gmail dot com>\n *\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n/*\n * SDS compatibility header.\n *\n * This simple file maps sds types and calls to their unique hiredis symbol names.\n * It's useful when we build Hiredis as a dependency of Redis and want to call\n * Hiredis' sds symbols rather than the ones built into Redis, as the libraries\n * have slightly diverged and could cause hard to track down ABI incompatibility\n * bugs.\n *\n */\n\n#ifndef HIREDIS_SDS_COMPAT\n#define HIREDIS_SDS_COMPAT\n\n#ifndef USE_SYSTEM_HIREDIS\n\n#define sds hisds\n\n#define sdslen hi_sdslen\n#define sdsavail hi_sdsavail\n#define sdssetlen hi_sdssetlen\n#define sdsinclen hi_sdsinclen\n#define sdsalloc hi_sdsalloc\n#define sdssetalloc hi_sdssetalloc\n\n#define sdsAllocPtr hi_sdsAllocPtr\n#define sdsAllocSize hi_sdsAllocSize\n#define sdscat hi_sdscat\n#define sdscatfmt hi_sdscatfmt\n#define sdscatlen hi_sdscatlen\n#define sdscatprintf hi_sdscatprintf\n#define sdscatrepr hi_sdscatrepr\n#define sdscatsds hi_sdscatsds\n#define sdscatvprintf hi_sdscatvprintf\n#define sdsclear hi_sdsclear\n#define sdscmp hi_sdscmp\n#define sdscpy hi_sdscpy\n#define sdscpylen hi_sdscpylen\n#define sdsdup hi_sdsdup\n#define sdsempty hi_sdsempty\n#define sds_free hi_sds_free\n#define sdsfree hi_sdsfree\n#define sdsfreesplitres hi_sdsfreesplitres\n#define sdsfromlonglong hi_sdsfromlonglong\n#define sdsgrowzero hi_sdsgrowzero\n#define sdsIncrLen hi_sdsIncrLen\n#define sdsjoin hi_sdsjoin\n#define sdsjoinsds hi_sdsjoinsds\n#define sdsll2str hi_sdsll2str\n#define sdsMakeRoomFor hi_sdsMakeRoomFor\n#define sds_malloc hi_sds_malloc\n#define sdsmapchars hi_sdsmapchars\n#define sdsnew hi_sdsnew\n#define sdsnewlen hi_sdsnewlen\n#define sdsrange hi_sdsrange\n#define sds_realloc hi_sds_realloc\n#define sdsRemoveFreeSpace hi_sdsRemoveFreeSpace\n#define sdssplitargs hi_sdssplitargs\n#define sdssplitlen hi_sdssplitlen\n#define sdstolower hi_sdstolower\n#define sdstoupper hi_sdstoupper\n#define sdstrim hi_sdstrim\n#define sdsull2str hi_sdsull2str\n#define sdsupdatelen hi_sdsupdatelen\n\n#endif /* !USE_SYSTEM_HIREDIS */\n\n#endif /* HIREDIS_SDS_COMPAT */\n"
  },
  {
    "path": "src/semiorderedset.h",
    "content": "#pragma once\n#include <assert.h>\n#include \"compactvector.h\"\n#include \"cowptr.h\"\n\n/****************************************\n * semiorderedset.h:\n * \n * The ordered set is a hash set that maintains semi-ordering, that is you can iterate in sub-linear time over the set comparing a value.\n * It has a few other useful properties vs the traditional set:\n *      1. The key need not be the underlying type, the only requirement is the value type is castable to the key\n *      2. The key need not have total ordering.  The set will iterate until it finds an exact match with operator== on the value\n *          This provides additional flexibility on insert allowing us to optimize this case.\n * \n */\n\nextern uint64_t dictGenHashFunction(const void *key, int len);\n\nnamespace keydbutils\n{\n    template<typename T>\n    size_t hash(const T& key)\n    {\n        return (size_t)dictGenHashFunction(&key, sizeof(key));\n    }\n\n    template<>\n    size_t hash(const sdsview &);\n}\nextern size_t g_semiOrderedSetTargetBucketSize;\n\ntemplate<typename T, typename T_KEY = T, bool MEMMOVE_SAFE = false>\nclass semiorderedset\n{\n    typedef compactvector<T, MEMMOVE_SAFE> vector_type;\n    \n    friend struct setiter;\n    std::vector<CowPtr<vector_type>> m_data;\n    size_t celem = 0;\n    static const size_t bits_min = 8;\n    size_t bits = bits_min;\n    size_t idxRehash = (1ULL << bits_min); \n    int cfPauseRehash = 0;\n\n    inline size_t targetElementsPerBucket()\n    {\n        // Aim for roughly 4 cache lines per bucket (determined by imperical testing)\n        //  lower values are faster but use more memory\n        if (g_semiOrderedSetTargetBucketSize == 0)\n            return std::max((64/sizeof(T))*8, (size_t)2);\n        else\n            return g_semiOrderedSetTargetBucketSize;\n    }\n\npublic:\n    semiorderedset(size_t bitsStart = 0)\n    {\n        if (bitsStart < bits_min)\n            bitsStart = bits_min;\n        bits = bitsStart;\n        m_data.resize((1ULL << bits));\n    }\n\n    struct setiter\n    {\n        semiorderedset *set;\n        size_t idxPrimary = 0;\n        size_t idxSecondary = 0;\n\n        setiter(const semiorderedset *set)\n        {\n            this->set = (semiorderedset*)set;\n        }\n\n        bool operator==(const setiter &other) const\n        {\n            return (idxPrimary == other.idxPrimary) && (idxSecondary == other.idxSecondary);\n        }\n\n        bool operator!=(const setiter &other) const { return !operator==(other); }\n\n        inline T &operator*() { return set->m_data[idxPrimary]->operator[](idxSecondary); }\n        inline const T &operator*() const { return set->m_data[idxPrimary]->operator[](idxSecondary); }\n\n        inline T *operator->() { return &set->m_data[idxPrimary]->operator[](idxSecondary); }\n        inline const T *operator->() const { return &set->m_data[idxPrimary]->operator[](idxSecondary); }\n    };\n\n    setiter find(const T_KEY &key)\n    {\n        RehashStep();\n        return const_cast<const semiorderedset*>(this)->find(key);\n    }\n\n    setiter find(const T_KEY &key) const\n    {\n        setiter itr(this);\n        itr.idxPrimary = idxFromObj(key);\n \n        for (int hashset = 0; hashset < 2; ++hashset)   // rehashing may only be 1 resize behind, so we check up to two slots\n        {\n            if (m_data[itr.idxPrimary] != nullptr)\n            {\n                const auto &vecBucket = *m_data[itr.idxPrimary];\n\n                auto itrFind = std::find(vecBucket.begin(), vecBucket.end(), key);\n                if (itrFind != vecBucket.end())\n                {\n                    itr.idxSecondary =  itrFind - vecBucket.begin();\n                    return itr;\n                }\n            }\n\n            // See if we have to check the older slot\n            size_t mask = (hashmask() >> 1);\n            if (itr.idxPrimary == (itr.idxPrimary & mask))\n                break;  // same bucket we just checked\n            itr.idxPrimary &= mask;\n            if (FRehashedRow(itr.idxPrimary))\n                break;\n        }\n        \n        return end();\n    }\n\n    bool exists(const T_KEY &key) const\n    {\n        auto itr = const_cast<semiorderedset<T,T_KEY,MEMMOVE_SAFE>*>(this)->find(key);\n        return itr != this->end();\n    }\n\n    setiter end() const\n    {\n        setiter itr(const_cast<semiorderedset<T,T_KEY,MEMMOVE_SAFE>*>(this));\n        itr.idxPrimary = m_data.size();\n        return itr;\n    }\n\n    void insert(const T &e, bool fRehash = false)\n    {\n        if (!fRehash)\n            RehashStep();\n\n        auto idx = idxFromObj(static_cast<T_KEY>(e));\n        if (!fRehash)\n            ++celem;\n        \n        if (m_data[idx] == nullptr)\n            m_data[idx] = std::make_shared<vector_type>();\n        \n        typename vector_type::iterator itrInsert;        \n        if (!m_data[idx]->empty() && !(e < m_data[idx]->back()))\n            itrInsert = m_data[idx]->end();\n        else\n            itrInsert = std::upper_bound(m_data[idx]->begin(), m_data[idx]->end(), e);\n        itrInsert = m_data[idx]->insert(itrInsert, e);\n\n        if (celem > ((1ULL << bits)*targetElementsPerBucket()))\n            grow();\n    }\n\n    // enumeration starting from the 'itrStart'th key.  Note that the iter is a hint, and need no be valid anymore\n    template<typename T_VISITOR, typename T_MAX>\n    setiter enumerate(const setiter &itrStart, const T_MAX &max, T_VISITOR fn, long long *pccheck)\n    {\n        setiter itr(itrStart);\n\n        if (itrStart.set != this)   // really if this case isn't true its probably a bug\n            itr.set = this;         // but why crash the program when we can easily fix this?\n        \n        cfPauseRehash++;\n        if (itr.idxPrimary >= m_data.size())\n            itr.idxPrimary = 0;\n        \n        for (size_t ibucket = 0; ibucket < m_data.size(); ++ibucket)\n        {\n            if (!enumerate_bucket(itr, max, fn, pccheck))\n                break;\n            itr.idxSecondary = 0;\n\n            ++itr.idxPrimary;\n            if (itr.idxPrimary >= m_data.size())\n                itr.idxPrimary = 0;\n        }\n        cfPauseRehash--;\n        return itr;\n    }\n\n    // This will \"randomly\" visit nodes biased towards lower values first\n    template<typename T_VISITOR>\n    size_t random_visit(T_VISITOR &fn)\n    {\n        bool fSawAny = true;\n        size_t visited = 0;\n        size_t basePrimary = rand() % m_data.size();\n        for (size_t idxSecondary = 0; fSawAny; ++idxSecondary)\n        {\n            fSawAny = false;\n            for (size_t idxPrimaryCount = 0; idxPrimaryCount < m_data.size(); ++idxPrimaryCount)\n            {\n                size_t idxPrimary = (basePrimary + idxPrimaryCount) % m_data.size();\n                if (m_data[idxPrimary] != nullptr && idxSecondary < m_data[idxPrimary]->size())\n                {\n                    ++visited;\n                    fSawAny = true;\n                    if (!fn(m_data[idxPrimary]->operator[](idxSecondary)))\n                        return visited;\n                }\n            }\n        }\n        return visited;\n    }\n\n    const T& random_value() const\n    {\n        assert(!empty());\n        for (;;)\n        {\n            size_t idxPrimary = rand() % m_data.size();\n            if (m_data[idxPrimary] == nullptr || m_data[idxPrimary]->empty())\n                continue;\n\n            return (*m_data[idxPrimary])[rand() % m_data[idxPrimary]->size()];\n        }\n    }\n\n    void erase(const setiter &itr)\n    {\n        auto &vecRow = *m_data[itr.idxPrimary];\n        vecRow.erase(vecRow.begin() + itr.idxSecondary);\n        --celem;\n        RehashStep();\n    }\n\n    void clear()\n    {\n        m_data = decltype(m_data)();\n        bits = bits_min;\n        m_data.resize(1ULL << bits);\n        celem = 0;\n        idxRehash = m_data.size();\n    }\n    \n    bool empty() const noexcept { return celem == 0; }\n    size_t size() const noexcept { return celem; }\n\n    size_t estimated_bytes_used() const\n    {\n        // This estimate does't include all the overhead of the internal vectors\n        size_t cb = sizeof(this)\n            + (m_data.capacity() * sizeof(m_data[0]))\n            + sizeof(T) * size();\n        return cb;\n    }\n\n    #define DICT_STATS_VECTLEN 50\n    size_t getstats(char *buf, size_t bufsize) const\n    {\n        unsigned long i, slots = 0, chainlen, maxchainlen = 0;\n        unsigned long totchainlen = 0;\n        unsigned long clvector[DICT_STATS_VECTLEN] = {0};\n        size_t l = 0;\n\n        if (empty()) {\n            return snprintf(buf,bufsize,\n                \"No stats available for empty dictionaries\\n\");\n        }\n\n        /* Compute stats. */\n        for (const auto &spvec : m_data) {\n            if (spvec == nullptr)\n                continue;\n            const auto &vec = *spvec;\n            if (vec.empty()) {\n                clvector[0]++;\n                continue;\n            }\n            slots++;\n            /* For each hash entry on this slot... */\n            chainlen = vec.size();\n            \n            clvector[(chainlen < DICT_STATS_VECTLEN) ? chainlen : (DICT_STATS_VECTLEN-1)]++;\n            if (chainlen > maxchainlen) maxchainlen = chainlen;\n            totchainlen += chainlen;\n        }\n\n        size_t used = m_data.size()-clvector[0];\n        /* Generate human readable stats. */\n        l += snprintf(buf+l,bufsize-l,\n            \"semiordered set stats:\\n\"\n            \" table size: %zu\\n\"\n            \" number of slots: %zu\\n\"\n            \" used slots: %ld\\n\"\n            \" max chain length: %ld\\n\"\n            \" avg chain length (counted): %.02f\\n\"\n            \" avg chain length (computed): %.02f\\n\"\n            \" Chain length distribution:\\n\",\n            size(), used, slots, maxchainlen,\n            (float)totchainlen/slots, (float)size()/m_data.size());\n\n        for (i = 0; i < DICT_STATS_VECTLEN; i++) {\n            if (clvector[i] == 0) continue;\n            if (l >= bufsize) break;\n            l += snprintf(buf+l,bufsize-l,\n                \"   %s%ld: %ld (%.02f%%)\\n\",\n                (i == DICT_STATS_VECTLEN-1)?\">= \":\"\",\n                i, clvector[i], ((float)clvector[i]/m_data.size())*100);\n        }\n\n        /* Unlike snprintf(), teturn the number of characters actually written. */\n        if (bufsize) buf[bufsize-1] = '\\0';\n        return strlen(buf);\n    }\n\n    void pause_rehash() { ++cfPauseRehash; }\n    void unpause_rehash() { --cfPauseRehash; RehashStep(); }\n\nprivate:\n    inline size_t hashmask() const { return (1ULL << bits) - 1; }\n\n    size_t idxFromObj(const T_KEY &key) const\n    {\n        size_t v = keydbutils::hash(key);\n        return v & hashmask();\n    }\n\n    bool FRehashedRow(size_t idx) const\n    {\n        return (idx >= (m_data.size()/2)) || (idx < idxRehash);\n    }\n\n    void RehashStep()\n    {\n        if (cfPauseRehash)\n            return;\n        \n        int steps = 0;\n        for (; idxRehash < (m_data.size()/2); ++idxRehash)\n        {\n            if (m_data[idxRehash] == nullptr)\n                continue;\n\n            CowPtr<vector_type> spvecT;\n            std::swap(m_data[idxRehash], spvecT); \n\n            for (const auto &v : *spvecT)\n                insert(v, true);\n\n            if (++steps > 1024)\n                break;\n        }\n    }\n\n    void grow()\n    {\n        assert(idxRehash >= (m_data.size()/2)); // we should have finished rehashing by the time we need to grow again\n        \n        ++bits;\n        m_data.resize(1ULL << bits);\n        idxRehash = 0;\n        RehashStep();\n    }\n\n    template<typename T_VISITOR, typename T_MAX>\n    inline bool enumerate_bucket(setiter &itr, const T_MAX &max, T_VISITOR &fn, long long *pcheckLimit)\n    {\n        if (m_data[itr.idxPrimary] == nullptr)\n            return true;\n        \n        auto &vec = *m_data[itr.idxPrimary];\n        for (; itr.idxSecondary < vec.size(); ++itr.idxSecondary)\n        {\n            // Assert we're ordered by T_MAX\n            assert((itr.idxSecondary+1) >= vec.size() \n                || static_cast<T_MAX>(vec[itr.idxSecondary]) <= static_cast<T_MAX>(vec[itr.idxSecondary+1]));\n\n            (*pcheckLimit)--;\n            if (max < static_cast<T_MAX>(*itr))\n                return *pcheckLimit > 0;\n\n            size_t sizeBefore = vec.size();\n            if (!fn(*itr))\n            {\n                itr.idxSecondary++; // we still visited this node\n                return false;\n            }\n            if (vec.size() != sizeBefore)\n            {\n                assert(vec.size() == (sizeBefore-1));   // they may only remove the element passed to them\n                --itr.idxSecondary;    // they deleted the element\n            }\n        }\n        vec.shrink_to_fit();\n        return *pcheckLimit > 0;\n    }\n};\n"
  },
  {
    "path": "src/sentinel.cpp",
    "content": "/* Redis Sentinel implementation\n *\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n#include \"hiredis.h\"\n#ifdef USE_OPENSSL\n#include <openssl/ssl.h>\nextern \"C\" {\n#include \"hiredis_ssl.h\"\n}\n#endif\n#include \"async.h\"\n\n#include <ctype.h>\n#include <arpa/inet.h>\n#include <sys/socket.h>\n#include <sys/wait.h>\n#include <fcntl.h>\n\nextern char **environ;\n\n#ifdef USE_OPENSSL\nextern SSL_CTX *redis_tls_ctx;\nextern SSL_CTX *redis_tls_client_ctx;\n#endif\n\n#define REDIS_SENTINEL_PORT 26379\n\n/* ======================== Sentinel global state =========================== */\n\n/* Address object, used to describe an ip:port pair. */\ntypedef struct sentinelAddr {\n    char *hostname;         /* Hostname OR address, as specified */\n    char *ip;               /* Always a resolved address */\n    int port;\n} sentinelAddr;\n\n/* A Sentinel Redis Instance object is monitoring. */\n#define SRI_MASTER  (1<<0)\n#define SRI_SLAVE   (1<<1)\n#define SRI_SENTINEL (1<<2)\n#define SRI_S_DOWN (1<<3)   /* Subjectively down (no quorum). */\n#define SRI_O_DOWN (1<<4)   /* Objectively down (confirmed by others). */\n#define SRI_MASTER_DOWN (1<<5) /* A Sentinel with this flag set thinks that\n                                   its master is down. */\n#define SRI_FAILOVER_IN_PROGRESS (1<<6) /* Failover is in progress for\n                                           this master. */\n#define SRI_PROMOTED (1<<7)            /* Slave selected for promotion. */\n#define SRI_RECONF_SENT (1<<8)     /* SLAVEOF <newmaster> sent. */\n#define SRI_RECONF_INPROG (1<<9)   /* Slave synchronization in progress. */\n#define SRI_RECONF_DONE (1<<10)     /* Slave synchronized with new master. */\n#define SRI_FORCE_FAILOVER (1<<11)  /* Force failover with master up. */\n#define SRI_SCRIPT_KILL_SENT (1<<12) /* SCRIPT KILL already sent on -BUSY */\n\n/* Note: times are in milliseconds. */\n#define SENTINEL_INFO_PERIOD 10000\n#define SENTINEL_PING_PERIOD 1000\n#define SENTINEL_ASK_PERIOD 1000\n#define SENTINEL_PUBLISH_PERIOD 2000\n#define SENTINEL_DEFAULT_DOWN_AFTER 30000\n#define SENTINEL_HELLO_CHANNEL \"__sentinel__:hello\"\n#define SENTINEL_TILT_TRIGGER 2000\n#define SENTINEL_TILT_PERIOD (SENTINEL_PING_PERIOD*30)\n#define SENTINEL_DEFAULT_SLAVE_PRIORITY 100\n#define SENTINEL_SLAVE_RECONF_TIMEOUT 10000\n#define SENTINEL_DEFAULT_PARALLEL_SYNCS 1\n#define SENTINEL_MIN_LINK_RECONNECT_PERIOD 15000\n#define SENTINEL_DEFAULT_FAILOVER_TIMEOUT (60*3*1000)\n#define SENTINEL_MAX_PENDING_COMMANDS 100\n#define SENTINEL_ELECTION_TIMEOUT 10000\n#define SENTINEL_MAX_DESYNC 1000\n#define SENTINEL_DEFAULT_DENY_SCRIPTS_RECONFIG 1\n#define SENTINEL_DEFAULT_RESOLVE_HOSTNAMES 0\n#define SENTINEL_DEFAULT_ANNOUNCE_HOSTNAMES 0\n\n/* Failover machine different states. */\n#define SENTINEL_FAILOVER_STATE_NONE 0  /* No failover in progress. */\n#define SENTINEL_FAILOVER_STATE_WAIT_START 1  /* Wait for failover_start_time*/\n#define SENTINEL_FAILOVER_STATE_SELECT_SLAVE 2 /* Select slave to promote */\n#define SENTINEL_FAILOVER_STATE_SEND_SLAVEOF_NOONE 3 /* Slave -> Master */\n#define SENTINEL_FAILOVER_STATE_WAIT_PROMOTION 4 /* Wait slave to change role */\n#define SENTINEL_FAILOVER_STATE_RECONF_SLAVES 5 /* SLAVEOF newmaster */\n#define SENTINEL_FAILOVER_STATE_UPDATE_CONFIG 6 /* Monitor promoted slave. */\n\n#define SENTINEL_MASTER_LINK_STATUS_UP 0\n#define SENTINEL_MASTER_LINK_STATUS_DOWN 1\n\n/* Generic flags that can be used with different functions.\n * They use higher bits to avoid colliding with the function specific\n * flags. */\n#define SENTINEL_NO_FLAGS 0\n#define SENTINEL_GENERATE_EVENT (1<<16)\n#define SENTINEL_LEADER (1<<17)\n#define SENTINEL_OBSERVER (1<<18)\n\n/* Script execution flags and limits. */\n#define SENTINEL_SCRIPT_NONE 0\n#define SENTINEL_SCRIPT_RUNNING 1\n#define SENTINEL_SCRIPT_MAX_QUEUE 256\n#define SENTINEL_SCRIPT_MAX_RUNNING 16\n#define SENTINEL_SCRIPT_MAX_RUNTIME 60000 /* 60 seconds max exec time. */\n#define SENTINEL_SCRIPT_MAX_RETRY 10\n#define SENTINEL_SCRIPT_RETRY_DELAY 30000 /* 30 seconds between retries. */\n\n/* SENTINEL SIMULATE-FAILURE command flags. */\n#define SENTINEL_SIMFAILURE_NONE 0\n#define SENTINEL_SIMFAILURE_CRASH_AFTER_ELECTION (1<<0)\n#define SENTINEL_SIMFAILURE_CRASH_AFTER_PROMOTION (1<<1)\n\n/* The link to a sentinelRedisInstance. When we have the same set of Sentinels\n * monitoring many masters, we have different instances representing the\n * same Sentinels, one per master, and we need to share the hiredis connections\n * among them. Otherwise if 5 Sentinels are monitoring 100 masters we create\n * 500 outgoing connections instead of 5.\n *\n * So this structure represents a reference counted link in terms of the two\n * hiredis connections for commands and Pub/Sub, and the fields needed for\n * failure detection, since the ping/pong time are now local to the link: if\n * the link is available, the instance is available. This way we don't just\n * have 5 connections instead of 500, we also send 5 pings instead of 500.\n *\n * Links are shared only for Sentinels: master and slave instances have\n * a link with refcount = 1, always. */\ntypedef struct instanceLink {\n    int refcount;          /* Number of sentinelRedisInstance owners. */\n    int disconnected;      /* Non-zero if we need to reconnect cc or pc. */\n    int pending_commands;  /* Number of commands sent waiting for a reply. */\n    redisAsyncContext *cc; /* Hiredis context for commands. */\n    redisAsyncContext *pc; /* Hiredis context for Pub / Sub. */\n    mstime_t cc_conn_time; /* cc connection time. */\n    mstime_t pc_conn_time; /* pc connection time. */\n    mstime_t pc_last_activity; /* Last time we received any message. */\n    mstime_t last_avail_time; /* Last time the instance replied to ping with\n                                 a reply we consider valid. */\n    mstime_t act_ping_time;   /* Time at which the last pending ping (no pong\n                                 received after it) was sent. This field is\n                                 set to 0 when a pong is received, and set again\n                                 to the current time if the value is 0 and a new\n                                 ping is sent. */\n    mstime_t last_ping_time;  /* Time at which we sent the last ping. This is\n                                 only used to avoid sending too many pings\n                                 during failure. Idle time is computed using\n                                 the act_ping_time field. */\n    mstime_t last_pong_time;  /* Last time the instance replied to ping,\n                                 whatever the reply was. That's used to check\n                                 if the link is idle and must be reconnected. */\n    mstime_t last_reconn_time;  /* Last reconnection attempt performed when\n                                   the link was down. */\n} instanceLink;\n\ntypedef struct sentinelRedisInstance {\n    int flags;      /* See SRI_... defines */\n    char *name;     /* Master name from the point of view of this sentinel. */\n    char *runid;    /* Run ID of this instance, or unique ID if is a Sentinel.*/\n    uint64_t config_epoch;  /* Configuration epoch. */\n    sentinelAddr *addr; /* Master host. */\n    instanceLink *link; /* Link to the instance, may be shared for Sentinels. */\n    mstime_t last_pub_time;   /* Last time we sent hello via Pub/Sub. */\n    mstime_t last_hello_time; /* Only used if SRI_SENTINEL is set. Last time\n                                 we received a hello from this Sentinel\n                                 via Pub/Sub. */\n    mstime_t last_master_down_reply_time; /* Time of last reply to\n                                             SENTINEL is-master-down command. */\n    mstime_t s_down_since_time; /* Subjectively down since time. */\n    mstime_t o_down_since_time; /* Objectively down since time. */\n    mstime_t down_after_period; /* Consider it down after that period. */\n    mstime_t info_refresh;  /* Time at which we received INFO output from it. */\n    dict *renamed_commands;     /* Commands renamed in this instance:\n                                   Sentinel will use the alternative commands\n                                   mapped on this table to send things like\n                                   SLAVEOF, CONFING, INFO, ... */\n\n    /* Role and the first time we observed it.\n     * This is useful in order to delay replacing what the instance reports\n     * with our own configuration. We need to always wait some time in order\n     * to give a chance to the leader to report the new configuration before\n     * we do silly things. */\n    int role_reported;\n    mstime_t role_reported_time;\n    mstime_t slave_conf_change_time; /* Last time slave master addr changed. */\n\n    /* Master specific. */\n    dict *sentinels;    /* Other sentinels monitoring the same master. */\n    dict *slaves;       /* Slaves for this master instance. */\n    unsigned int quorum;/* Number of sentinels that need to agree on failure. */\n    int parallel_syncs; /* How many slaves to reconfigure at same time. */\n    char *auth_pass;    /* Password to use for AUTH against master & replica. */\n    char *auth_user;    /* Username for ACLs AUTH against master & replica. */\n\n    /* Slave specific. */\n    mstime_t master_link_down_time; /* Slave replication link down time. */\n    int slave_priority; /* Slave priority according to its INFO output. */\n    int replica_announced; /* Replica announcing according to its INFO output. */\n    mstime_t slave_reconf_sent_time; /* Time at which we sent SLAVE OF <new> */\n    struct sentinelRedisInstance *master; /* Master instance if it's slave. */\n    char *slave_master_host;    /* Master host as reported by INFO */\n    int slave_master_port;      /* Master port as reported by INFO */\n    int slave_master_link_status; /* Master link status as reported by INFO */\n    unsigned long long slave_repl_offset; /* Slave replication offset. */\n    /* Failover */\n    char *leader;       /* If this is a master instance, this is the runid of\n                           the Sentinel that should perform the failover. If\n                           this is a Sentinel, this is the runid of the Sentinel\n                           that this Sentinel voted as leader. */\n    uint64_t leader_epoch; /* Epoch of the 'leader' field. */\n    uint64_t failover_epoch; /* Epoch of the currently started failover. */\n    int failover_state; /* See SENTINEL_FAILOVER_STATE_* defines. */\n    mstime_t failover_state_change_time;\n    mstime_t failover_start_time;   /* Last failover attempt start time. */\n    mstime_t failover_timeout;      /* Max time to refresh failover state. */\n    mstime_t failover_delay_logged; /* For what failover_start_time value we\n                                       logged the failover delay. */\n    struct sentinelRedisInstance *promoted_slave; /* Promoted slave instance. */\n    /* Scripts executed to notify admin or reconfigure clients: when they\n     * are set to NULL no script is executed. */\n    char *notification_script;\n    char *client_reconfig_script;\n    sds info; /* cached INFO output */\n} sentinelRedisInstance;\n\n/* Main state. */\nstruct sentinelState {\n    char myid[CONFIG_RUN_ID_SIZE+1]; /* This sentinel ID. */\n    uint64_t current_epoch;         /* Current epoch. */\n    dict *masters;      /* Dictionary of master sentinelRedisInstances.\n                           Key is the instance name, value is the\n                           sentinelRedisInstance structure pointer. */\n    int tilt;           /* Are we in TILT mode? */\n    int running_scripts;    /* Number of scripts in execution right now. */\n    mstime_t tilt_start_time;       /* When TITL started. */\n    mstime_t previous_time;         /* Last time we ran the time handler. */\n    list *scripts_queue;            /* Queue of user scripts to execute. */\n    char *announce_ip;  /* IP addr that is gossiped to other sentinels if\n                           not NULL. */\n    int announce_port;  /* Port that is gossiped to other sentinels if\n                           non zero. */\n    unsigned long simfailure_flags; /* Failures simulation. */\n    int deny_scripts_reconfig; /* Allow SENTINEL SET ... to change script\n                                  paths at runtime? */\n    char *sentinel_auth_pass;    /* Password to use for AUTH against other sentinel */\n    char *sentinel_auth_user;    /* Username for ACLs AUTH against other sentinel. */\n    int resolve_hostnames;       /* Support use of hostnames, assuming DNS is well configured. */\n    int announce_hostnames;      /* Announce hostnames instead of IPs when we have them. */\n} sentinel;\n\n/* A script execution job. */\ntypedef struct sentinelScriptJob {\n    int flags;              /* Script job flags: SENTINEL_SCRIPT_* */\n    int retry_num;          /* Number of times we tried to execute it. */\n    char **argv;            /* Arguments to call the script. */\n    mstime_t start_time;    /* Script execution time if the script is running,\n                               otherwise 0 if we are allowed to retry the\n                               execution at any time. If the script is not\n                               running and it's not 0, it means: do not run\n                               before the specified time. */\n    pid_t pid;              /* Script execution pid. */\n} sentinelScriptJob;\n\n/* ======================= hiredis ae.c adapters =============================\n * Note: this implementation is taken from hiredis/adapters/ae.h, however\n * we have our modified copy for Sentinel in order to use our allocator\n * and to have full control over how the adapter works. */\n\ntypedef struct redisAeEvents {\n    redisAsyncContext *context;\n    aeEventLoop *loop;\n    int fd;\n    int reading, writing;\n} redisAeEvents;\n\nstatic void redisAeReadEvent(aeEventLoop *el, int fd, void *privdata, int mask) {\n    ((void)el); ((void)fd); ((void)mask);\n\n    redisAeEvents *e = (redisAeEvents*)privdata;\n    redisAsyncHandleRead(e->context);\n}\n\nstatic void redisAeWriteEvent(aeEventLoop *el, int fd, void *privdata, int mask) {\n    ((void)el); ((void)fd); ((void)mask);\n\n    redisAeEvents *e = (redisAeEvents*)privdata;\n    redisAsyncHandleWrite(e->context);\n}\n\nstatic void redisAeAddRead(void *privdata) {\n    redisAeEvents *e = (redisAeEvents*)privdata;\n    aeEventLoop *loop = e->loop;\n    if (!e->reading) {\n        e->reading = 1;\n        aeCreateFileEvent(loop,e->fd,AE_READABLE,redisAeReadEvent,e);\n    }\n}\n\nstatic void redisAeDelRead(void *privdata) {\n    redisAeEvents *e = (redisAeEvents*)privdata;\n    aeEventLoop *loop = e->loop;\n    if (e->reading) {\n        e->reading = 0;\n        aeDeleteFileEvent(loop,e->fd,AE_READABLE);\n    }\n}\n\nstatic void redisAeAddWrite(void *privdata) {\n    redisAeEvents *e = (redisAeEvents*)privdata;\n    aeEventLoop *loop = e->loop;\n    if (!e->writing) {\n        e->writing = 1;\n        aeCreateFileEvent(loop,e->fd,AE_WRITABLE,redisAeWriteEvent,e);\n    }\n}\n\nstatic void redisAeDelWrite(void *privdata) {\n    redisAeEvents *e = (redisAeEvents*)privdata;\n    aeEventLoop *loop = e->loop;\n    if (e->writing) {\n        e->writing = 0;\n        aeDeleteFileEvent(loop,e->fd,AE_WRITABLE);\n    }\n}\n\nstatic void redisAeCleanup(void *privdata) {\n    redisAeEvents *e = (redisAeEvents*)privdata;\n    redisAeDelRead(privdata);\n    redisAeDelWrite(privdata);\n    zfree(e);\n}\n\nstatic int redisAeAttach(aeEventLoop *loop, redisAsyncContext *ac) {\n    redisContext *c = &(ac->c);\n    redisAeEvents *e;\n\n    /* Nothing should be attached when something is already attached */\n    if (ac->ev.data != NULL)\n        return C_ERR;\n\n    /* Create container for context and r/w events */\n    e = (redisAeEvents*)zmalloc(sizeof(*e), MALLOC_LOCAL);\n    e->context = ac;\n    e->loop = loop;\n    e->fd = c->fd;\n    e->reading = e->writing = 0;\n\n    /* Register functions to start/stop listening for events */\n    ac->ev.addRead = redisAeAddRead;\n    ac->ev.delRead = redisAeDelRead;\n    ac->ev.addWrite = redisAeAddWrite;\n    ac->ev.delWrite = redisAeDelWrite;\n    ac->ev.cleanup = redisAeCleanup;\n    ac->ev.data = e;\n\n    return C_OK;\n}\n\n/* ============================= Prototypes ================================= */\n\nvoid sentinelLinkEstablishedCallback(const redisAsyncContext *c, int status);\nvoid sentinelDisconnectCallback(const redisAsyncContext *c, int status);\nvoid sentinelReceiveHelloMessages(redisAsyncContext *c, void *reply, void *privdata);\nsentinelRedisInstance *sentinelGetMasterByName(char *name);\nchar *sentinelGetSubjectiveLeader(sentinelRedisInstance *master);\nchar *sentinelGetObjectiveLeader(sentinelRedisInstance *master);\nint yesnotoi(char *s);\nvoid instanceLinkConnectionError(const redisAsyncContext *c);\nconst char *sentinelRedisInstanceTypeStr(sentinelRedisInstance *ri);\nvoid sentinelAbortFailover(sentinelRedisInstance *ri);\nvoid sentinelEvent(int level, char *type, sentinelRedisInstance *ri, const char *fmt, ...);\nsentinelRedisInstance *sentinelSelectSlave(sentinelRedisInstance *master);\nvoid sentinelScheduleScriptExecution(char *path, ...);\nvoid sentinelStartFailover(sentinelRedisInstance *master);\nvoid sentinelDiscardReplyCallback(redisAsyncContext *c, void *reply, void *privdata);\nint sentinelSendSlaveOf(sentinelRedisInstance *ri, const sentinelAddr *addr);\nchar *sentinelVoteLeader(sentinelRedisInstance *master, uint64_t req_epoch, char *req_runid, uint64_t *leader_epoch);\nvoid sentinelFlushConfig(void);\nvoid sentinelGenerateInitialMonitorEvents(void);\nint sentinelSendPing(sentinelRedisInstance *ri);\nint sentinelForceHelloUpdateForMaster(sentinelRedisInstance *master);\nsentinelRedisInstance *getSentinelRedisInstanceByAddrAndRunID(dict *instances, char *ip, int port, char *runid);\nvoid sentinelSimFailureCrash(void);\n\n/* ========================= Dictionary types =============================== */\n\nuint64_t dictSdsHash(const void *key);\nuint64_t dictSdsCaseHash(const void *key);\nint dictSdsKeyCompare(void *privdata, const void *key1, const void *key2);\nint dictSdsKeyCaseCompare(void *privdata, const void *key1, const void *key2);\nvoid releaseSentinelRedisInstance(sentinelRedisInstance *ri);\n\nvoid dictInstancesValDestructor (void *privdata, void *obj) {\n    UNUSED(privdata);\n    releaseSentinelRedisInstance((sentinelRedisInstance*)obj);\n}\n\n/* Instance name (sds) -> instance (sentinelRedisInstance pointer)\n *\n * also used for: sentinelRedisInstance->sentinels dictionary that maps\n * sentinels ip:port to last seen time in Pub/Sub hello message. */\ndictType instancesDictType = {\n    dictSdsHash,               /* hash function */\n    NULL,                      /* key dup */\n    NULL,                      /* val dup */\n    dictSdsKeyCompare,         /* key compare */\n    NULL,                      /* key destructor */\n    dictInstancesValDestructor,/* val destructor */\n    NULL                       /* allow to expand */\n};\n\n/* Instance runid (sds) -> votes (long casted to void*)\n *\n * This is useful into sentinelGetObjectiveLeader() function in order to\n * count the votes and understand who is the leader. */\ndictType leaderVotesDictType = {\n    dictSdsHash,               /* hash function */\n    NULL,                      /* key dup */\n    NULL,                      /* val dup */\n    dictSdsKeyCompare,         /* key compare */\n    NULL,                      /* key destructor */\n    NULL,                      /* val destructor */\n    NULL                       /* allow to expand */\n};\n\n/* Instance renamed commands table. */\ndictType renamedCommandsDictType = {\n    dictSdsCaseHash,           /* hash function */\n    NULL,                      /* key dup */\n    NULL,                      /* val dup */\n    dictSdsKeyCaseCompare,     /* key compare */\n    dictSdsDestructor,         /* key destructor */\n    dictSdsDestructor,         /* val destructor */\n    NULL                       /* allow to expand */\n};\n\n/* =========================== Initialization =============================== */\n\nvoid sentinelCommand(client *c);\nvoid sentinelInfoCommand(client *c);\nvoid sentinelSetCommand(client *c);\nvoid sentinelPublishCommand(client *c);\nvoid sentinelRoleCommand(client *c);\nvoid sentinelConfigGetCommand(client *c);\nvoid sentinelConfigSetCommand(client *c);\n\nstruct redisCommand sentinelcmds[] = {\n    {\"ping\",pingCommand,1,\"fast @connection\",0,NULL,0,0,0,0,0},\n    {\"sentinel\",sentinelCommand,-2,\"admin\",0,NULL,0,0,0,0,0},\n    {\"subscribe\",subscribeCommand,-2,\"pub-sub\",0,NULL,0,0,0,0,0},\n    {\"unsubscribe\",unsubscribeCommand,-1,\"pub-sub\",0,NULL,0,0,0,0,0},\n    {\"psubscribe\",psubscribeCommand,-2,\"pub-sub\",0,NULL,0,0,0,0,0},\n    {\"punsubscribe\",punsubscribeCommand,-1,\"pub-sub\",0,NULL,0,0,0,0,0},\n    {\"publish\",sentinelPublishCommand,3,\"pub-sub fast\",0,NULL,0,0,0,0,0},\n    {\"info\",sentinelInfoCommand,-1,\"random @dangerous\",0,NULL,0,0,0,0,0},\n    {\"role\",sentinelRoleCommand,1,\"fast read-only @dangerous\",0,NULL,0,0,0,0,0},\n    {\"client\",clientCommand,-2,\"admin random @connection\",0,NULL,0,0,0,0,0},\n    {\"shutdown\",shutdownCommand,-1,\"admin\",0,NULL,0,0,0,0,0},\n    {\"auth\",authCommand,-2,\"no-auth fast @connection\",0,NULL,0,0,0,0,0},\n    {\"hello\",helloCommand,-1,\"no-auth fast @connection\",0,NULL,0,0,0,0,0},\n    {\"acl\",aclCommand,-2,\"admin\",0,NULL,0,0,0,0,0,0},\n    {\"command\",commandCommand,-1, \"random @connection\", 0,NULL,0,0,0,0,0,0}\n};\n\n/* this array is used for sentinel config lookup, which need to be loaded\n * before monitoring masters config to avoid dependency issues */\nconst char *preMonitorCfgName[] = { \n    \"announce-ip\",\n    \"announce-port\",\n    \"deny-scripts-reconfig\",\n    \"sentinel-user\",\n    \"sentinel-pass\",\n    \"current-epoch\",\n    \"myid\",\n    \"resolve-hostnames\",\n    \"announce-hostnames\"\n};\n\n/* This function overwrites a few normal Redis config default with Sentinel\n * specific defaults. */\nvoid initSentinelConfig(void) {\n    g_pserver->port = REDIS_SENTINEL_PORT;\n    g_pserver->protected_mode = 0; /* Sentinel must be exposed. */\n}\n\nvoid freeSentinelLoadQueueEntry(const void *item);\n\n/* Perform the Sentinel mode initialization. */\nvoid initSentinel(void) {\n    unsigned int j;\n\n    /* Remove usual Redis commands from the command table, then just add\n     * the SENTINEL command. */\n    dictEmpty(g_pserver->commands,NULL);\n    dictEmpty(g_pserver->orig_commands,NULL);\n    ACLClearCommandID();\n    for (j = 0; j < sizeof(sentinelcmds)/sizeof(sentinelcmds[0]); j++) {\n        int retval;\n        struct redisCommand *cmd = sentinelcmds+j;\n        cmd->id = ACLGetCommandID(cmd->name); /* Assign the ID used for ACL. */\n        retval = dictAdd(g_pserver->commands, sdsnew(cmd->name), cmd);\n        serverAssert(retval == DICT_OK);\n        retval = dictAdd(g_pserver->orig_commands, sdsnew(cmd->name), cmd);\n        serverAssert(retval == DICT_OK);\n\n        /* Translate the command string flags description into an actual\n         * set of flags. */\n        if (populateCommandTableParseFlags(cmd,cmd->sflags) == C_ERR)\n            serverPanic(\"Unsupported command flag\");\n    }\n\n    /* Initialize various data structures. */\n    sentinel.current_epoch = 0;\n    sentinel.masters = dictCreate(&instancesDictType,NULL);\n    sentinel.tilt = 0;\n    sentinel.tilt_start_time = 0;\n    sentinel.previous_time = mstime();\n    sentinel.running_scripts = 0;\n    sentinel.scripts_queue = listCreate();\n    sentinel.announce_ip = NULL;\n    sentinel.announce_port = 0;\n    sentinel.simfailure_flags = SENTINEL_SIMFAILURE_NONE;\n    sentinel.deny_scripts_reconfig = SENTINEL_DEFAULT_DENY_SCRIPTS_RECONFIG;\n    sentinel.sentinel_auth_pass = NULL;\n    sentinel.sentinel_auth_user = NULL;\n    sentinel.resolve_hostnames = SENTINEL_DEFAULT_RESOLVE_HOSTNAMES;\n    sentinel.announce_hostnames = SENTINEL_DEFAULT_ANNOUNCE_HOSTNAMES;\n    memset(sentinel.myid,0,sizeof(sentinel.myid));\n    g_pserver->sentinel_config = NULL;\n}\n\n/* This function is for checking whether sentinel config file has been set,\n * also checking whether we have write permissions. */\nvoid sentinelCheckConfigFile(void) {\n    if (cserver.configfile == NULL) {\n        serverLog(LL_WARNING,\n            \"Sentinel needs config file on disk to save state. Exiting...\");\n        exit(1);\n    } else if (access(cserver.configfile,W_OK) == -1) {\n        serverLog(LL_WARNING,\n            \"Sentinel config file %s is not writable: %s. Exiting...\",\n            cserver.configfile,strerror(errno));\n        exit(1);\n    }\n}\n\n/* This function gets called when the server is in Sentinel mode, started,\n * loaded the configuration, and is ready for normal operations. */\nvoid sentinelIsRunning(void) {\n    int j;\n\n    /* If this Sentinel has yet no ID set in the configuration file, we\n     * pick a random one and persist the config on disk. From now on this\n     * will be this Sentinel ID across restarts. */\n    for (j = 0; j < CONFIG_RUN_ID_SIZE; j++)\n        if (sentinel.myid[j] != 0) break;\n\n    if (j == CONFIG_RUN_ID_SIZE) {\n        /* Pick ID and persist the config. */\n        getRandomHexChars(sentinel.myid,CONFIG_RUN_ID_SIZE);\n        sentinelFlushConfig();\n    }\n\n    /* Log its ID to make debugging of issues simpler. */\n    serverLog(LL_WARNING,\"Sentinel ID is %s\", sentinel.myid);\n\n    /* We want to generate a +monitor event for every configured master\n     * at startup. */\n    sentinelGenerateInitialMonitorEvents();\n}\n\n/* ============================== sentinelAddr ============================== */\n\n/* Create a sentinelAddr object and return it on success.\n * On error NULL is returned and errno is set to:\n *  ENOENT: Can't resolve the hostname.\n *  EINVAL: Invalid port number.\n */\nsentinelAddr *createSentinelAddr(char *hostname, int port) {\n    char ip[NET_IP_STR_LEN];\n    sentinelAddr *sa;\n\n    if (port < 0 || port > 65535) {\n        errno = EINVAL;\n        return NULL;\n    }\n    if (anetResolve(NULL,hostname,ip,sizeof(ip),\n                    sentinel.resolve_hostnames ? ANET_NONE : ANET_IP_ONLY) == ANET_ERR) {\n        errno = ENOENT;\n        return NULL;\n    }\n    sa = (sentinelAddr*)zmalloc(sizeof(*sa), MALLOC_LOCAL);\n    sa->hostname = sdsnew(hostname);\n    sa->ip = sdsnew(ip);\n    sa->port = port;\n    return sa;\n}\n\n/* Return a duplicate of the source address. */\nsentinelAddr *dupSentinelAddr(sentinelAddr *src) {\n    sentinelAddr *sa;\n\n    sa = (sentinelAddr*)zmalloc(sizeof(*sa), MALLOC_LOCAL);\n    sa->hostname = sdsnew(src->hostname);\n    sa->ip = sdsnew(src->ip);\n    sa->port = src->port;\n    return sa;\n}\n\n/* Free a Sentinel address. Can't fail. */\nvoid releaseSentinelAddr(sentinelAddr *sa) {\n    sdsfree(sa->hostname);\n    sdsfree(sa->ip);\n    zfree(sa);\n}\n\n/* Return non-zero if two addresses are equal. */\nint sentinelAddrIsEqual(sentinelAddr *a, sentinelAddr *b) {\n    return a->port == b->port && !strcasecmp(a->ip,b->ip);\n}\n\n/* Return non-zero if a hostname matches an address. */\nint sentinelAddrEqualsHostname(sentinelAddr *a, char *hostname) {\n    char ip[NET_IP_STR_LEN];\n\n    /* We always resolve the hostname and compare it to the address */\n    if (anetResolve(NULL, hostname, ip, sizeof(ip),\n                    sentinel.resolve_hostnames ? ANET_NONE : ANET_IP_ONLY) == ANET_ERR)\n        return 0;\n    return !strcasecmp(a->ip, ip);\n}\n\nconst char *announceSentinelAddr(const sentinelAddr *a) {\n    return sentinel.announce_hostnames ? a->hostname : a->ip;\n}\n\n/* Return an allocated sds with hostname/address:port. IPv6\n * addresses are bracketed the same way anetFormatAddr() does.\n */\nsds announceSentinelAddrAndPort(const sentinelAddr *a) {\n    const char *addr = announceSentinelAddr(a);\n    if (strchr(addr, ':') != NULL)\n        return sdscatprintf(sdsempty(), \"[%s]:%d\", addr, a->port);\n    else\n        return sdscatprintf(sdsempty(), \"%s:%d\", addr, a->port);\n}\n\n/* =========================== Events notification ========================== */\n\n/* Send an event to log, pub/sub, user notification script.\n *\n * 'level' is the log level for logging. Only LL_WARNING events will trigger\n * the execution of the user notification script.\n *\n * 'type' is the message type, also used as a pub/sub channel name.\n *\n * 'ri', is the redis instance target of this event if applicable, and is\n * used to obtain the path of the notification script to execute.\n *\n * The remaining arguments are printf-alike.\n * If the format specifier starts with the two characters \"%@\" then ri is\n * not NULL, and the message is prefixed with an instance identifier in the\n * following format:\n *\n *  <instance type> <instance name> <ip> <port>\n *\n *  If the instance type is not master, than the additional string is\n *  added to specify the originating master:\n *\n *  @ <master name> <master ip> <master port>\n *\n *  Any other specifier after \"%@\" is processed by printf itself.\n */\nvoid sentinelEvent(int level, const char *type, sentinelRedisInstance *ri,\n                   const char *fmt, ...) {\n    va_list ap;\n    char msg[LOG_MAX_LEN];\n    robj *channel, *payload;\n\n    /* Handle %@ */\n    if (fmt[0] == '%' && fmt[1] == '@') {\n        sentinelRedisInstance *master = (ri->flags & SRI_MASTER) ?\n                                         NULL : ri->master;\n\n        if (master) {\n            snprintf(msg, sizeof(msg), \"%s %s %s %d @ %s %s %d\",\n                sentinelRedisInstanceTypeStr(ri),\n                ri->name, announceSentinelAddr(ri->addr), ri->addr->port,\n                master->name, announceSentinelAddr(master->addr), master->addr->port);\n        } else {\n            snprintf(msg, sizeof(msg), \"%s %s %s %d\",\n                sentinelRedisInstanceTypeStr(ri),\n                ri->name, announceSentinelAddr(ri->addr), ri->addr->port);\n        }\n        fmt += 2;\n    } else {\n        msg[0] = '\\0';\n    }\n\n    /* Use vsprintf for the rest of the formatting if any. */\n    if (fmt[0] != '\\0') {\n        va_start(ap, fmt);\n        vsnprintf(msg+strlen(msg), sizeof(msg)-strlen(msg), fmt, ap);\n        va_end(ap);\n    }\n\n    /* Log the message if the log level allows it to be logged. */\n    if (level >= cserver.verbosity)\n        serverLog(level,\"%s %s\",type,msg);\n\n    /* Publish the message via Pub/Sub if it's not a debugging one. */\n    if (level != LL_DEBUG) {\n        channel = createStringObject(type,strlen(type));\n        payload = createStringObject(msg,strlen(msg));\n        pubsubPublishMessage(channel,payload);\n        decrRefCount(channel);\n        decrRefCount(payload);\n    }\n\n    /* Call the notification script if applicable. */\n    if (level == LL_WARNING && ri != NULL) {\n        sentinelRedisInstance *master = (ri->flags & SRI_MASTER) ?\n                                         ri : ri->master;\n        if (master && master->notification_script) {\n            sentinelScheduleScriptExecution(master->notification_script,\n                type,msg,NULL);\n        }\n    }\n}\n\n/* This function is called only at startup and is used to generate a\n * +monitor event for every configured master. The same events are also\n * generated when a master to monitor is added at runtime via the\n * SENTINEL MONITOR command. */\nvoid sentinelGenerateInitialMonitorEvents(void) {\n    dictIterator *di;\n    dictEntry *de;\n\n    di = dictGetIterator(sentinel.masters);\n    while((de = dictNext(di)) != NULL) {\n        sentinelRedisInstance *ri = (sentinelRedisInstance*)dictGetVal(de);\n        sentinelEvent(LL_WARNING,\"+monitor\",ri,\"%@ quorum %d\",ri->quorum);\n    }\n    dictReleaseIterator(di);\n}\n\n/* ============================ script execution ============================ */\n\n/* Release a script job structure and all the associated data. */\nvoid sentinelReleaseScriptJob(sentinelScriptJob *sj) {\n    int j = 0;\n\n    while(sj->argv[j]) sdsfree(sj->argv[j++]);\n    zfree(sj->argv);\n    zfree(sj);\n}\n\n#define SENTINEL_SCRIPT_MAX_ARGS 16\nvoid sentinelScheduleScriptExecution(char *path, ...) {\n    va_list ap;\n    char *argv[SENTINEL_SCRIPT_MAX_ARGS+1];\n    int argc = 1;\n    sentinelScriptJob *sj;\n\n    va_start(ap, path);\n    while(argc < SENTINEL_SCRIPT_MAX_ARGS) {\n        argv[argc] = va_arg(ap,char*);\n        if (!argv[argc]) break;\n        argv[argc] = sdsnew(argv[argc]); /* Copy the string. */\n        argc++;\n    }\n    va_end(ap);\n    argv[0] = sdsnew(path);\n\n    sj = (sentinelScriptJob*)zmalloc(sizeof(*sj), MALLOC_LOCAL);\n    sj->flags = SENTINEL_SCRIPT_NONE;\n    sj->retry_num = 0;\n    sj->argv = (char**)zmalloc(sizeof(char*)*(argc+1), MALLOC_LOCAL);\n    sj->start_time = 0;\n    sj->pid = 0;\n    memcpy(sj->argv,argv,sizeof(char*)*(argc+1));\n\n    listAddNodeTail(sentinel.scripts_queue,sj);\n\n    /* Remove the oldest non running script if we already hit the limit. */\n    if (listLength(sentinel.scripts_queue) > SENTINEL_SCRIPT_MAX_QUEUE) {\n        listNode *ln;\n        listIter li;\n\n        listRewind(sentinel.scripts_queue,&li);\n        while ((ln = listNext(&li)) != NULL) {\n            sj = (sentinelScriptJob*)ln->value;\n\n            if (sj->flags & SENTINEL_SCRIPT_RUNNING) continue;\n            /* The first node is the oldest as we add on tail. */\n            listDelNode(sentinel.scripts_queue,ln);\n            sentinelReleaseScriptJob(sj);\n            break;\n        }\n        serverAssert(listLength(sentinel.scripts_queue) <=\n                    SENTINEL_SCRIPT_MAX_QUEUE);\n    }\n}\n\n/* Lookup a script in the scripts queue via pid, and returns the list node\n * (so that we can easily remove it from the queue if needed). */\nlistNode *sentinelGetScriptListNodeByPid(pid_t pid) {\n    listNode *ln;\n    listIter li;\n\n    listRewind(sentinel.scripts_queue,&li);\n    while ((ln = listNext(&li)) != NULL) {\n        sentinelScriptJob *sj = (sentinelScriptJob*)ln->value;\n\n        if ((sj->flags & SENTINEL_SCRIPT_RUNNING) && sj->pid == pid)\n            return ln;\n    }\n    return NULL;\n}\n\n/* Run pending scripts if we are not already at max number of running\n * scripts. */\nvoid sentinelRunPendingScripts(void) {\n    listNode *ln;\n    listIter li;\n    mstime_t now = mstime();\n\n    /* Find jobs that are not running and run them, from the top to the\n     * tail of the queue, so we run older jobs first. */\n    listRewind(sentinel.scripts_queue,&li);\n    while (sentinel.running_scripts < SENTINEL_SCRIPT_MAX_RUNNING &&\n           (ln = listNext(&li)) != NULL)\n    {\n        sentinelScriptJob *sj = (sentinelScriptJob*)ln->value;\n        pid_t pid;\n\n        /* Skip if already running. */\n        if (sj->flags & SENTINEL_SCRIPT_RUNNING) continue;\n\n        /* Skip if it's a retry, but not enough time has elapsed. */\n        if (sj->start_time && sj->start_time > now) continue;\n\n        sj->flags |= SENTINEL_SCRIPT_RUNNING;\n        sj->start_time = mstime();\n        sj->retry_num++;\n        pid = fork();\n\n        if (pid == -1) {\n            /* Parent (fork error).\n             * We report fork errors as signal 99, in order to unify the\n             * reporting with other kind of errors. */\n            sentinelEvent(LL_WARNING,\"-script-error\",NULL,\n                          \"%s %d %d\", sj->argv[0], 99, 0);\n            sj->flags &= ~SENTINEL_SCRIPT_RUNNING;\n            sj->pid = 0;\n        } else if (pid == 0) {\n            /* Child */\n            tlsCleanup();\n            for (int iel = 0; iel < cserver.cthreads; ++iel) {\n                aeClosePipesForForkChild(g_pserver->rgthreadvar[iel].el);\n            }\n            aeClosePipesForForkChild(g_pserver->modulethreadvar.el);\n            execve(sj->argv[0],sj->argv,environ);\n            /* If we are here an error occurred. */\n            _exit(2); /* Don't retry execution. */\n        } else {\n            sentinel.running_scripts++;\n            sj->pid = pid;\n            sentinelEvent(LL_DEBUG,\"+script-child\",NULL,\"%ld\",(long)pid);\n        }\n    }\n}\n\n/* How much to delay the execution of a script that we need to retry after\n * an error?\n *\n * We double the retry delay for every further retry we do. So for instance\n * if RETRY_DELAY is set to 30 seconds and the max number of retries is 10\n * starting from the second attempt to execute the script the delays are:\n * 30 sec, 60 sec, 2 min, 4 min, 8 min, 16 min, 32 min, 64 min, 128 min. */\nmstime_t sentinelScriptRetryDelay(int retry_num) {\n    mstime_t delay = SENTINEL_SCRIPT_RETRY_DELAY;\n\n    while (retry_num-- > 1) delay *= 2;\n    return delay;\n}\n\n/* Check for scripts that terminated, and remove them from the queue if the\n * script terminated successfully. If instead the script was terminated by\n * a signal, or returned exit code \"1\", it is scheduled to run again if\n * the max number of retries did not already elapsed. */\nvoid sentinelCollectTerminatedScripts(void) {\n    int statloc;\n    pid_t pid;\n\n    while ((pid = waitpid(-1, &statloc, WNOHANG)) > 0) {\n        int exitcode = WEXITSTATUS(statloc);\n        int bysignal = 0;\n        listNode *ln;\n        sentinelScriptJob *sj;\n\n        if (WIFSIGNALED(statloc)) bysignal = WTERMSIG(statloc);\n        sentinelEvent(LL_DEBUG,\"-script-child\",NULL,\"%ld %d %d\",\n            (long)pid, exitcode, bysignal);\n\n        ln = sentinelGetScriptListNodeByPid(pid);\n        if (ln == NULL) {\n            serverLog(LL_WARNING,\"waitpid() returned a pid (%ld) we can't find in our scripts execution queue!\", (long)pid);\n            continue;\n        }\n        sj = (sentinelScriptJob*)ln->value;\n\n        /* If the script was terminated by a signal or returns an\n         * exit code of \"1\" (that means: please retry), we reschedule it\n         * if the max number of retries is not already reached. */\n        if ((bysignal || exitcode == 1) &&\n            sj->retry_num != SENTINEL_SCRIPT_MAX_RETRY)\n        {\n            sj->flags &= ~SENTINEL_SCRIPT_RUNNING;\n            sj->pid = 0;\n            sj->start_time = mstime() +\n                             sentinelScriptRetryDelay(sj->retry_num);\n        } else {\n            /* Otherwise let's remove the script, but log the event if the\n             * execution did not terminated in the best of the ways. */\n            if (bysignal || exitcode != 0) {\n                sentinelEvent(LL_WARNING,\"-script-error\",NULL,\n                              \"%s %d %d\", sj->argv[0], bysignal, exitcode);\n            }\n            listDelNode(sentinel.scripts_queue,ln);\n            sentinelReleaseScriptJob(sj);\n        }\n        sentinel.running_scripts--;\n    }\n}\n\n/* Kill scripts in timeout, they'll be collected by the\n * sentinelCollectTerminatedScripts() function. */\nvoid sentinelKillTimedoutScripts(void) {\n    listNode *ln;\n    listIter li;\n    mstime_t now = mstime();\n\n    listRewind(sentinel.scripts_queue,&li);\n    while ((ln = listNext(&li)) != NULL) {\n        sentinelScriptJob *sj = (sentinelScriptJob*)ln->value;\n\n        if (sj->flags & SENTINEL_SCRIPT_RUNNING &&\n            (now - sj->start_time) > SENTINEL_SCRIPT_MAX_RUNTIME)\n        {\n            sentinelEvent(LL_WARNING,\"-script-timeout\",NULL,\"%s %ld\",\n                sj->argv[0], (long)sj->pid);\n            kill(sj->pid,SIGKILL);\n        }\n    }\n}\n\n/* Implements SENTINEL PENDING-SCRIPTS command. */\nvoid sentinelPendingScriptsCommand(client *c) {\n    listNode *ln;\n    listIter li;\n\n    addReplyArrayLen(c,listLength(sentinel.scripts_queue));\n    listRewind(sentinel.scripts_queue,&li);\n    while ((ln = listNext(&li)) != NULL) {\n        sentinelScriptJob *sj = (sentinelScriptJob*)ln->value;\n        int j = 0;\n\n        addReplyMapLen(c,5);\n\n        addReplyBulkCString(c,\"argv\");\n        while (sj->argv[j]) j++;\n        addReplyArrayLen(c,j);\n        j = 0;\n        while (sj->argv[j]) addReplyBulkCString(c,sj->argv[j++]);\n\n        addReplyBulkCString(c,\"flags\");\n        addReplyBulkCString(c,\n            (sj->flags & SENTINEL_SCRIPT_RUNNING) ? \"running\" : \"scheduled\");\n\n        addReplyBulkCString(c,\"pid\");\n        addReplyBulkLongLong(c,sj->pid);\n\n        if (sj->flags & SENTINEL_SCRIPT_RUNNING) {\n            addReplyBulkCString(c,\"run-time\");\n            addReplyBulkLongLong(c,mstime() - sj->start_time);\n        } else {\n            mstime_t delay = sj->start_time ? (sj->start_time-mstime()) : 0;\n            if (delay < 0) delay = 0;\n            addReplyBulkCString(c,\"run-delay\");\n            addReplyBulkLongLong(c,delay);\n        }\n\n        addReplyBulkCString(c,\"retry-num\");\n        addReplyBulkLongLong(c,sj->retry_num);\n    }\n}\n\n/* This function calls, if any, the client reconfiguration script with the\n * following parameters:\n *\n * <master-name> <role> <state> <from-ip> <from-port> <to-ip> <to-port>\n *\n * It is called every time a failover is performed.\n *\n * <state> is currently always \"failover\".\n * <role> is either \"leader\" or \"observer\".\n *\n * from/to fields are respectively master -> promoted slave addresses for\n * \"start\" and \"end\". */\nvoid sentinelCallClientReconfScript(sentinelRedisInstance *master, int role, const char *state, sentinelAddr *from, sentinelAddr *to) {\n    char fromport[32], toport[32];\n\n    if (master->client_reconfig_script == NULL) return;\n    ll2string(fromport,sizeof(fromport),from->port);\n    ll2string(toport,sizeof(toport),to->port);\n    sentinelScheduleScriptExecution(master->client_reconfig_script,\n        master->name,\n        (role == SENTINEL_LEADER) ? \"leader\" : \"observer\",\n        state, announceSentinelAddr(from), fromport,\n        announceSentinelAddr(to), toport, NULL);\n}\n\n/* =============================== instanceLink ============================= */\n\n/* Create a not yet connected link object. */\ninstanceLink *createInstanceLink(void) {\n    instanceLink *link = (instanceLink*)zmalloc(sizeof(*link), MALLOC_LOCAL);\n\n    link->refcount = 1;\n    link->disconnected = 1;\n    link->pending_commands = 0;\n    link->cc = NULL;\n    link->pc = NULL;\n    link->cc_conn_time = 0;\n    link->pc_conn_time = 0;\n    link->last_reconn_time = 0;\n    link->pc_last_activity = 0;\n    /* We set the act_ping_time to \"now\" even if we actually don't have yet\n     * a connection with the node, nor we sent a ping.\n     * This is useful to detect a timeout in case we'll not be able to connect\n     * with the node at all. */\n    link->act_ping_time = mstime();\n    link->last_ping_time = 0;\n    link->last_avail_time = mstime();\n    link->last_pong_time = mstime();\n    return link;\n}\n\n/* Disconnect a hiredis connection in the context of an instance link. */\nvoid instanceLinkCloseConnection(instanceLink *link, redisAsyncContext *c) {\n    if (c == NULL) return;\n\n    if (link->cc == c) {\n        link->cc = NULL;\n        link->pending_commands = 0;\n    }\n    if (link->pc == c) link->pc = NULL;\n    c->data = NULL;\n    link->disconnected = 1;\n    redisAsyncFree(c);\n}\n\n/* Decrement the refcount of a link object, if it drops to zero, actually\n * free it and return NULL. Otherwise don't do anything and return the pointer\n * to the object.\n *\n * If we are not going to free the link and ri is not NULL, we rebind all the\n * pending requests in link->cc (hiredis connection for commands) to a\n * callback that will just ignore them. This is useful to avoid processing\n * replies for an instance that no longer exists. */\ninstanceLink *releaseInstanceLink(instanceLink *link, sentinelRedisInstance *ri)\n{\n    serverAssert(link->refcount > 0);\n    link->refcount--;\n    if (link->refcount != 0) {\n        if (ri && ri->link->cc) {\n            /* This instance may have pending callbacks in the hiredis async\n             * context, having as 'privdata' the instance that we are going to\n             * free. Let's rewrite the callback list, directly exploiting\n             * hiredis internal data structures, in order to bind them with\n             * a callback that will ignore the reply at all. */\n            redisCallback *cb;\n            redisCallbackList *callbacks = &link->cc->replies;\n\n            cb = callbacks->head;\n            while(cb) {\n                if (cb->privdata == ri) {\n                    cb->fn = sentinelDiscardReplyCallback;\n                    cb->privdata = NULL; /* Not strictly needed. */\n                }\n                cb = cb->next;\n            }\n        }\n        return link; /* Other active users. */\n    }\n\n    instanceLinkCloseConnection(link,link->cc);\n    instanceLinkCloseConnection(link,link->pc);\n    zfree(link);\n    return NULL;\n}\n\n/* This function will attempt to share the instance link we already have\n * for the same Sentinel in the context of a different master, with the\n * instance we are passing as argument.\n *\n * This way multiple Sentinel objects that refer all to the same physical\n * Sentinel instance but in the context of different masters will use\n * a single connection, will send a single PING per second for failure\n * detection and so forth.\n *\n * Return C_OK if a matching Sentinel was found in the context of a\n * different master and sharing was performed. Otherwise C_ERR\n * is returned. */\nint sentinelTryConnectionSharing(sentinelRedisInstance *ri) {\n    serverAssert(ri->flags & SRI_SENTINEL);\n    dictIterator *di;\n    dictEntry *de;\n\n    if (ri->runid == NULL) return C_ERR; /* No way to identify it. */\n    if (ri->link->refcount > 1) return C_ERR; /* Already shared. */\n\n    di = dictGetIterator(sentinel.masters);\n    while((de = dictNext(di)) != NULL) {\n        sentinelRedisInstance *master = (sentinelRedisInstance*)dictGetVal(de), *match;\n        /* We want to share with the same physical Sentinel referenced\n         * in other masters, so skip our master. */\n        if (master == ri->master) continue;\n        match = getSentinelRedisInstanceByAddrAndRunID(master->sentinels,\n                                                       NULL,0,ri->runid);\n        if (match == NULL) continue; /* No match. */\n        if (match == ri) continue; /* Should never happen but... safer. */\n\n        /* We identified a matching Sentinel, great! Let's free our link\n         * and use the one of the matching Sentinel. */\n        releaseInstanceLink(ri->link,NULL);\n        ri->link = match->link;\n        match->link->refcount++;\n        dictReleaseIterator(di);\n        return C_OK;\n    }\n    dictReleaseIterator(di);\n    return C_ERR;\n}\n\n/* Drop all connections to other sentinels. Returns the number of connections\n * dropped.*/\nint sentinelDropConnections(void) {\n    dictIterator *di;\n    dictEntry *de;\n    int dropped = 0;\n\n    di = dictGetIterator(sentinel.masters);\n    while ((de = dictNext(di)) != NULL) {\n        dictIterator *sdi;\n        dictEntry *sde;\n\n        sentinelRedisInstance *ri = (sentinelRedisInstance*)dictGetVal(de);\n        sdi = dictGetIterator(ri->sentinels);\n        while ((sde = dictNext(sdi)) != NULL) {\n            sentinelRedisInstance *si = (sentinelRedisInstance*)dictGetVal(sde);\n            if (!si->link->disconnected) {\n                instanceLinkCloseConnection(si->link, si->link->pc);\n                instanceLinkCloseConnection(si->link, si->link->cc);\n                dropped++;\n            }\n        }\n        dictReleaseIterator(sdi);\n    }\n    dictReleaseIterator(di);\n\n    return dropped;\n}\n\n/* When we detect a Sentinel to switch address (reporting a different IP/port\n * pair in Hello messages), let's update all the matching Sentinels in the\n * context of other masters as well and disconnect the links, so that everybody\n * will be updated.\n *\n * Return the number of updated Sentinel addresses. */\nint sentinelUpdateSentinelAddressInAllMasters(sentinelRedisInstance *ri) {\n    serverAssert(ri->flags & SRI_SENTINEL);\n    dictIterator *di;\n    dictEntry *de;\n    int reconfigured = 0;\n\n    di = dictGetIterator(sentinel.masters);\n    while((de = dictNext(di)) != NULL) {\n        sentinelRedisInstance *master = (sentinelRedisInstance*)dictGetVal(de), *match;\n        match = getSentinelRedisInstanceByAddrAndRunID(master->sentinels,\n                                                       NULL,0,ri->runid);\n        /* If there is no match, this master does not know about this\n         * Sentinel, try with the next one. */\n        if (match == NULL) continue;\n\n        /* Disconnect the old links if connected. */\n        if (match->link->cc != NULL)\n            instanceLinkCloseConnection(match->link,match->link->cc);\n        if (match->link->pc != NULL)\n            instanceLinkCloseConnection(match->link,match->link->pc);\n\n        if (match == ri) continue; /* Address already updated for it. */\n\n        /* Update the address of the matching Sentinel by copying the address\n         * of the Sentinel object that received the address update. */\n        releaseSentinelAddr(match->addr);\n        match->addr = dupSentinelAddr(ri->addr);\n        reconfigured++;\n    }\n    dictReleaseIterator(di);\n    if (reconfigured)\n        sentinelEvent(LL_NOTICE,\"+sentinel-address-update\", ri,\n                    \"%@ %d additional matching instances\", reconfigured);\n    return reconfigured;\n}\n\n/* This function is called when a hiredis connection reported an error.\n * We set it to NULL and mark the link as disconnected so that it will be\n * reconnected again.\n *\n * Note: we don't free the hiredis context as hiredis will do it for us\n * for async connections. */\nvoid instanceLinkConnectionError(const redisAsyncContext *c) {\n    instanceLink *link = (instanceLink*)c->data;\n    int pubsub;\n\n    if (!link) return;\n\n    pubsub = (link->pc == c);\n    if (pubsub)\n        link->pc = NULL;\n    else\n        link->cc = NULL;\n    link->disconnected = 1;\n}\n\n/* Hiredis connection established / disconnected callbacks. We need them\n * just to cleanup our link state. */\nvoid sentinelLinkEstablishedCallback(const redisAsyncContext *c, int status) {\n    if (status != C_OK) instanceLinkConnectionError(c);\n}\n\nvoid sentinelDisconnectCallback(const redisAsyncContext *c, int status) {\n    UNUSED(status);\n    instanceLinkConnectionError(c);\n}\n\n/* ========================== sentinelRedisInstance ========================= */\n\n/* Create a redis instance, the following fields must be populated by the\n * caller if needed:\n * runid: set to NULL but will be populated once INFO output is received.\n * info_refresh: is set to 0 to mean that we never received INFO so far.\n *\n * If SRI_MASTER is set into initial flags the instance is added to\n * sentinel.masters table.\n *\n * if SRI_SLAVE or SRI_SENTINEL is set then 'master' must be not NULL and the\n * instance is added into master->slaves or master->sentinels table.\n *\n * If the instance is a slave or sentinel, the name parameter is ignored and\n * is created automatically as hostname:port.\n *\n * The function fails if hostname can't be resolved or port is out of range.\n * When this happens NULL is returned and errno is set accordingly to the\n * createSentinelAddr() function.\n *\n * The function may also fail and return NULL with errno set to EBUSY if\n * a master with the same name, a slave with the same address, or a sentinel\n * with the same ID already exists. */\n\nsentinelRedisInstance *createSentinelRedisInstance(char *name, int flags, char *hostname, int port, int quorum, sentinelRedisInstance *master) {\n    sentinelRedisInstance *ri;\n    sentinelAddr *addr;\n    dict *table = NULL;\n    sds sdsname;\n\n    serverAssert(flags & (SRI_MASTER|SRI_SLAVE|SRI_SENTINEL));\n    serverAssert((flags & SRI_MASTER) || master != NULL);\n\n    /* Check address validity. */\n    addr = createSentinelAddr(hostname,port);\n    if (addr == NULL) return NULL;\n\n    /* For slaves use ip/host:port as name. */\n    if (flags & SRI_SLAVE)\n        sdsname = announceSentinelAddrAndPort(addr);\n    else\n        sdsname = sdsnew(name);\n\n    /* Make sure the entry is not duplicated. This may happen when the same\n     * name for a master is used multiple times inside the configuration or\n     * if we try to add multiple times a slave or sentinel with same ip/port\n     * to a master. */\n    if (flags & SRI_MASTER) table = sentinel.masters;\n    else if (flags & SRI_SLAVE) table = master->slaves;\n    else if (flags & SRI_SENTINEL) table = master->sentinels;\n    if (dictFind(table,sdsname)) {\n        releaseSentinelAddr(addr);\n        sdsfree(sdsname);\n        errno = EBUSY;\n        return NULL;\n    }\n\n    /* Create the instance object. */\n    ri = (sentinelRedisInstance*)zmalloc(sizeof(*ri), MALLOC_LOCAL);\n    /* Note that all the instances are started in the disconnected state,\n     * the event loop will take care of connecting them. */\n    ri->flags = flags;\n    ri->name = sdsname;\n    ri->runid = NULL;\n    ri->config_epoch = 0;\n    ri->addr = addr;\n    ri->link = createInstanceLink();\n    ri->last_pub_time = mstime();\n    ri->last_hello_time = mstime();\n    ri->last_master_down_reply_time = mstime();\n    ri->s_down_since_time = 0;\n    ri->o_down_since_time = 0;\n    ri->down_after_period = master ? master->down_after_period :\n                            SENTINEL_DEFAULT_DOWN_AFTER;\n    ri->master_link_down_time = 0;\n    ri->auth_pass = NULL;\n    ri->auth_user = NULL;\n    ri->slave_priority = SENTINEL_DEFAULT_SLAVE_PRIORITY;\n    ri->replica_announced = 1;\n    ri->slave_reconf_sent_time = 0;\n    ri->slave_master_host = NULL;\n    ri->slave_master_port = 0;\n    ri->slave_master_link_status = SENTINEL_MASTER_LINK_STATUS_DOWN;\n    ri->slave_repl_offset = 0;\n    ri->sentinels = dictCreate(&instancesDictType,NULL);\n    ri->quorum = quorum;\n    ri->parallel_syncs = SENTINEL_DEFAULT_PARALLEL_SYNCS;\n    ri->master = master;\n    ri->slaves = dictCreate(&instancesDictType,NULL);\n    ri->info_refresh = 0;\n    ri->renamed_commands = dictCreate(&renamedCommandsDictType,NULL);\n\n    /* Failover state. */\n    ri->leader = NULL;\n    ri->leader_epoch = 0;\n    ri->failover_epoch = 0;\n    ri->failover_state = SENTINEL_FAILOVER_STATE_NONE;\n    ri->failover_state_change_time = 0;\n    ri->failover_start_time = 0;\n    ri->failover_timeout = SENTINEL_DEFAULT_FAILOVER_TIMEOUT;\n    ri->failover_delay_logged = 0;\n    ri->promoted_slave = NULL;\n    ri->notification_script = NULL;\n    ri->client_reconfig_script = NULL;\n    ri->info = NULL;\n\n    /* Role */\n    ri->role_reported = ri->flags & (SRI_MASTER|SRI_SLAVE);\n    ri->role_reported_time = mstime();\n    ri->slave_conf_change_time = mstime();\n\n    /* Add into the right table. */\n    dictAdd(table, ri->name, ri);\n    return ri;\n}\n\n/* Release this instance and all its slaves, sentinels, hiredis connections.\n * This function does not take care of unlinking the instance from the main\n * masters table (if it is a master) or from its master sentinels/slaves table\n * if it is a slave or sentinel. */\nvoid releaseSentinelRedisInstance(sentinelRedisInstance *ri) {\n    /* Release all its slaves or sentinels if any. */\n    dictRelease(ri->sentinels);\n    dictRelease(ri->slaves);\n\n    /* Disconnect the instance. */\n    releaseInstanceLink(ri->link,ri);\n\n    /* Free other resources. */\n    sdsfree(ri->name);\n    sdsfree(ri->runid);\n    sdsfree(ri->notification_script);\n    sdsfree(ri->client_reconfig_script);\n    sdsfree(ri->slave_master_host);\n    sdsfree(ri->leader);\n    sdsfree(ri->auth_pass);\n    sdsfree(ri->auth_user);\n    sdsfree(ri->info);\n    releaseSentinelAddr(ri->addr);\n    dictRelease(ri->renamed_commands);\n\n    /* Clear state into the master if needed. */\n    if ((ri->flags & SRI_SLAVE) && (ri->flags & SRI_PROMOTED) && ri->master)\n        ri->master->promoted_slave = NULL;\n\n    zfree(ri);\n}\n\n/* Lookup a slave in a master Redis instance, by ip and port. */\nsentinelRedisInstance *sentinelRedisInstanceLookupSlave(\n                sentinelRedisInstance *ri, char *slave_addr, int port)\n{\n    sds key;\n    sentinelRedisInstance *slave;\n    sentinelAddr *addr;\n\n    serverAssert(ri->flags & SRI_MASTER);\n\n    /* We need to handle a slave_addr that is potentially a hostname.\n     * If that is the case, depending on configuration we either resolve\n     * it and use the IP addres or fail.\n     */\n    addr = createSentinelAddr(slave_addr, port);\n    if (!addr) return NULL;\n    key = announceSentinelAddrAndPort(addr);\n    releaseSentinelAddr(addr);\n\n    slave = (sentinelRedisInstance*)dictFetchValue(ri->slaves,key);\n    sdsfree(key);\n    return slave;\n}\n\n/* Return the name of the type of the instance as a string. */\nconst char *sentinelRedisInstanceTypeStr(sentinelRedisInstance *ri) {\n    if (ri->flags & SRI_MASTER) return \"master\";\n    else if (ri->flags & SRI_SLAVE) return \"slave\";\n    else if (ri->flags & SRI_SENTINEL) return \"sentinel\";\n    else return \"unknown\";\n}\n\n/* This function remove the Sentinel with the specified ID from the\n * specified master.\n *\n * If \"runid\" is NULL the function returns ASAP.\n *\n * This function is useful because on Sentinels address switch, we want to\n * remove our old entry and add a new one for the same ID but with the new\n * address.\n *\n * The function returns 1 if the matching Sentinel was removed, otherwise\n * 0 if there was no Sentinel with this ID. */\nint removeMatchingSentinelFromMaster(sentinelRedisInstance *master, char *runid) {\n    dictIterator *di;\n    dictEntry *de;\n    int removed = 0;\n\n    if (runid == NULL) return 0;\n\n    di = dictGetSafeIterator(master->sentinels);\n    while((de = dictNext(di)) != NULL) {\n        sentinelRedisInstance *ri = (sentinelRedisInstance*)dictGetVal(de);\n\n        if (ri->runid && strcmp(ri->runid,runid) == 0) {\n            dictDelete(master->sentinels,ri->name);\n            removed++;\n        }\n    }\n    dictReleaseIterator(di);\n    return removed;\n}\n\n/* Search an instance with the same runid, ip and port into a dictionary\n * of instances. Return NULL if not found, otherwise return the instance\n * pointer.\n *\n * runid or addr can be NULL. In such a case the search is performed only\n * by the non-NULL field. */\nsentinelRedisInstance *getSentinelRedisInstanceByAddrAndRunID(dict *instances, char *addr, int port, char *runid) {\n    dictIterator *di;\n    dictEntry *de;\n    sentinelRedisInstance *instance = NULL;\n    sentinelAddr *ri_addr = NULL;\n\n    serverAssert(addr || runid);   /* User must pass at least one search param. */\n    if (addr != NULL) {\n        /* Resolve addr, we use the IP as a key even if a hostname is used */\n        ri_addr = createSentinelAddr(addr, port);\n        if (!ri_addr) return NULL;\n    }\n    di = dictGetIterator(instances);\n    while((de = dictNext(di)) != NULL) {\n        sentinelRedisInstance *ri = (sentinelRedisInstance*)dictGetVal(de);\n\n        if (runid && !ri->runid) continue;\n        if ((runid == NULL || strcmp(ri->runid, runid) == 0) &&\n            (addr == NULL || (strcmp(ri->addr->ip, ri_addr->ip) == 0 &&\n                            ri->addr->port == port)))\n        {\n            instance = ri;\n            break;\n        }\n    }\n    dictReleaseIterator(di);\n    if (ri_addr != NULL)\n        releaseSentinelAddr(ri_addr);\n\n    return instance;\n}\n\n/* Master lookup by name */\nsentinelRedisInstance *sentinelGetMasterByName(char *name) {\n    sentinelRedisInstance *ri;\n    sds sdsname = sdsnew(name);\n\n    ri = (sentinelRedisInstance*)dictFetchValue(sentinel.masters,sdsname);\n    sdsfree(sdsname);\n    return ri;\n}\n\n/* Add the specified flags to all the instances in the specified dictionary. */\nvoid sentinelAddFlagsToDictOfRedisInstances(dict *instances, int flags) {\n    dictIterator *di;\n    dictEntry *de;\n\n    di = dictGetIterator(instances);\n    while((de = dictNext(di)) != NULL) {\n        sentinelRedisInstance *ri = (sentinelRedisInstance*)dictGetVal(de);\n        ri->flags |= flags;\n    }\n    dictReleaseIterator(di);\n}\n\n/* Remove the specified flags to all the instances in the specified\n * dictionary. */\nvoid sentinelDelFlagsToDictOfRedisInstances(dict *instances, int flags) {\n    dictIterator *di;\n    dictEntry *de;\n\n    di = dictGetIterator(instances);\n    while((de = dictNext(di)) != NULL) {\n        sentinelRedisInstance *ri = (sentinelRedisInstance*)dictGetVal(de);\n        ri->flags &= ~flags;\n    }\n    dictReleaseIterator(di);\n}\n\n/* Reset the state of a monitored master:\n * 1) Remove all slaves.\n * 2) Remove all sentinels.\n * 3) Remove most of the flags resulting from runtime operations.\n * 4) Reset timers to their default value. For example after a reset it will be\n *    possible to failover again the same master ASAP, without waiting the\n *    failover timeout delay.\n * 5) In the process of doing this undo the failover if in progress.\n * 6) Disconnect the connections with the master (will reconnect automatically).\n */\n\n#define SENTINEL_RESET_NO_SENTINELS (1<<0)\nvoid sentinelResetMaster(sentinelRedisInstance *ri, int flags) {\n    serverAssert(ri->flags & SRI_MASTER);\n    dictRelease(ri->slaves);\n    ri->slaves = dictCreate(&instancesDictType,NULL);\n    if (!(flags & SENTINEL_RESET_NO_SENTINELS)) {\n        dictRelease(ri->sentinels);\n        ri->sentinels = dictCreate(&instancesDictType,NULL);\n    }\n    instanceLinkCloseConnection(ri->link,ri->link->cc);\n    instanceLinkCloseConnection(ri->link,ri->link->pc);\n    ri->flags &= SRI_MASTER;\n    if (ri->leader) {\n        sdsfree(ri->leader);\n        ri->leader = NULL;\n    }\n    ri->failover_state = SENTINEL_FAILOVER_STATE_NONE;\n    ri->failover_state_change_time = 0;\n    ri->failover_start_time = 0; /* We can failover again ASAP. */\n    ri->promoted_slave = NULL;\n    sdsfree(ri->runid);\n    sdsfree(ri->slave_master_host);\n    ri->runid = NULL;\n    ri->slave_master_host = NULL;\n    ri->link->act_ping_time = mstime();\n    ri->link->last_ping_time = 0;\n    ri->link->last_avail_time = mstime();\n    ri->link->last_pong_time = mstime();\n    ri->role_reported_time = mstime();\n    ri->role_reported = SRI_MASTER;\n    if (flags & SENTINEL_GENERATE_EVENT)\n        sentinelEvent(LL_WARNING,\"+reset-master\",ri,\"%@\");\n}\n\n/* Call sentinelResetMaster() on every master with a name matching the specified\n * pattern. */\nint sentinelResetMastersByPattern(char *pattern, int flags) {\n    dictIterator *di;\n    dictEntry *de;\n    int reset = 0;\n\n    di = dictGetIterator(sentinel.masters);\n    while((de = dictNext(di)) != NULL) {\n        sentinelRedisInstance *ri = (sentinelRedisInstance*)dictGetVal(de);\n\n        if (ri->name) {\n            if (stringmatch(pattern,ri->name,0)) {\n                sentinelResetMaster(ri,flags);\n                reset++;\n            }\n        }\n    }\n    dictReleaseIterator(di);\n    return reset;\n}\n\n/* Reset the specified master with sentinelResetMaster(), and also change\n * the ip:port address, but take the name of the instance unmodified.\n *\n * This is used to handle the +switch-master event.\n *\n * The function returns C_ERR if the address can't be resolved for some\n * reason. Otherwise C_OK is returned.  */\nint sentinelResetMasterAndChangeAddress(sentinelRedisInstance *master, char *hostname, int port) {\n    sentinelAddr *oldaddr, *newaddr;\n    sentinelAddr **slaves = NULL;\n    int numslaves = 0, j;\n    dictIterator *di;\n    dictEntry *de;\n\n    newaddr = createSentinelAddr(hostname,port);\n    if (newaddr == NULL) return C_ERR;\n\n    /* There can be only 0 or 1 slave that has the newaddr.\n     * and It can add old master 1 more slave. \n     * so It allocates dictSize(master->slaves) + 1          */\n    slaves = (sentinelAddr**)zmalloc(sizeof(sentinelAddr*)*(dictSize(master->slaves) + 1));\n    \n    /* Don't include the one having the address we are switching to. */\n    di = dictGetIterator(master->slaves);\n    while((de = dictNext(di)) != NULL) {\n        sentinelRedisInstance *slave = (sentinelRedisInstance*)dictGetVal(de);\n\n        if (sentinelAddrIsEqual(slave->addr,newaddr)) continue;\n        slaves[numslaves++] = dupSentinelAddr(slave->addr);\n    }\n    dictReleaseIterator(di);\n\n    /* If we are switching to a different address, include the old address\n     * as a slave as well, so that we'll be able to sense / reconfigure\n     * the old master. */\n    if (!sentinelAddrIsEqual(newaddr,master->addr)) {\n        slaves[numslaves++] = dupSentinelAddr(master->addr);\n    }\n\n    /* Reset and switch address. */\n    sentinelResetMaster(master,SENTINEL_RESET_NO_SENTINELS);\n    oldaddr = master->addr;\n    master->addr = newaddr;\n    master->o_down_since_time = 0;\n    master->s_down_since_time = 0;\n\n    /* Add slaves back. */\n    for (j = 0; j < numslaves; j++) {\n        sentinelRedisInstance *slave;\n\n        slave = createSentinelRedisInstance(NULL,SRI_SLAVE,slaves[j]->hostname,\n                    slaves[j]->port, master->quorum, master);\n        releaseSentinelAddr(slaves[j]);\n        if (slave) sentinelEvent(LL_NOTICE,\"+slave\",slave,\"%@\");\n    }\n    zfree(slaves);\n\n    /* Release the old address at the end so we are safe even if the function\n     * gets the master->addr->ip and master->addr->port as arguments. */\n    releaseSentinelAddr(oldaddr);\n    sentinelFlushConfig();\n    return C_OK;\n}\n\n/* Return non-zero if there was no SDOWN or ODOWN error associated to this\n * instance in the latest 'ms' milliseconds. */\nint sentinelRedisInstanceNoDownFor(sentinelRedisInstance *ri, mstime_t ms) {\n    mstime_t most_recent;\n\n    most_recent = ri->s_down_since_time;\n    if (ri->o_down_since_time > most_recent)\n        most_recent = ri->o_down_since_time;\n    return most_recent == 0 || (mstime() - most_recent) > ms;\n}\n\n/* Return the current master address, that is, its address or the address\n * of the promoted slave if already operational. */\nsentinelAddr *sentinelGetCurrentMasterAddress(sentinelRedisInstance *master) {\n    /* If we are failing over the master, and the state is already\n     * SENTINEL_FAILOVER_STATE_RECONF_SLAVES or greater, it means that we\n     * already have the new configuration epoch in the master, and the\n     * slave acknowledged the configuration switch. Advertise the new\n     * address. */\n    if ((master->flags & SRI_FAILOVER_IN_PROGRESS) &&\n        master->promoted_slave &&\n        master->failover_state >= SENTINEL_FAILOVER_STATE_RECONF_SLAVES)\n    {\n        return master->promoted_slave->addr;\n    } else {\n        return master->addr;\n    }\n}\n\n/* This function sets the down_after_period field value in 'master' to all\n * the slaves and sentinel instances connected to this master. */\nvoid sentinelPropagateDownAfterPeriod(sentinelRedisInstance *master) {\n    dictIterator *di;\n    dictEntry *de;\n    int j;\n    dict *d[] = {master->slaves, master->sentinels, NULL};\n\n    for (j = 0; d[j]; j++) {\n        di = dictGetIterator(d[j]);\n        while((de = dictNext(di)) != NULL) {\n            sentinelRedisInstance *ri = (sentinelRedisInstance*)dictGetVal(de);\n            ri->down_after_period = master->down_after_period;\n        }\n        dictReleaseIterator(di);\n    }\n}\n\n/* This function is used in order to send commands to Redis instances: the\n * commands we send from Sentinel may be renamed, a common case is a master\n * with CONFIG and SLAVEOF commands renamed for security concerns. In that\n * case we check the ri->renamed_command table (or if the instance is a slave,\n * we check the one of the master), and map the command that we should send\n * to the set of renamed commands. However, if the command was not renamed,\n * we just return \"command\" itself. */\nconst char *sentinelInstanceMapCommand(sentinelRedisInstance *ri, const char *command) {\n    sds sc = sdsnew(command);\n    if (ri->master) ri = ri->master;\n    char *retval = (char*)dictFetchValue(ri->renamed_commands, sc);\n    sdsfree(sc);\n    return retval ? retval : command;\n}\n\n/* ============================ Config handling ============================= */\n\n/* Generalise handling create instance error. Use SRI_MASTER, SRI_SLAVE or\n * SRI_SENTINEL as a role value. */\nconst char *sentinelCheckCreateInstanceErrors(int role) {\n    switch(errno) {\n    case EBUSY:\n        switch (role) {\n        case SRI_MASTER:\n            return \"Duplicate master name.\";\n        case SRI_SLAVE:\n            return \"Duplicate hostname and port for replica.\";\n        case SRI_SENTINEL:\n            return \"Duplicate runid for sentinel.\";\n        default:\n            serverAssert(0);\n            break;\n        }\n        break;\n    case ENOENT:\n        return \"Can't resolve instance hostname.\";\n    case EINVAL:\n        return \"Invalid port number.\";\n    }\n    return \"Unknown Error for creating instances.\";\n}\n\n/* init function for g_pserver->sentinel_config */\nvoid initializeSentinelConfig() {\n    g_pserver->sentinel_config = (sentinelConfig*)zmalloc(sizeof(struct sentinelConfig));\n    g_pserver->sentinel_config->monitor_cfg = listCreate();\n    g_pserver->sentinel_config->pre_monitor_cfg = listCreate();\n    g_pserver->sentinel_config->post_monitor_cfg = listCreate();\n    listSetFreeMethod(g_pserver->sentinel_config->monitor_cfg,freeSentinelLoadQueueEntry);\n    listSetFreeMethod(g_pserver->sentinel_config->pre_monitor_cfg,freeSentinelLoadQueueEntry);\n    listSetFreeMethod(g_pserver->sentinel_config->post_monitor_cfg,freeSentinelLoadQueueEntry);\n}\n\n/* destroy function for g_pserver->sentinel_config */\nvoid freeSentinelConfig() {\n    /* release these three config queues since we will not use it anymore */\n    listRelease(g_pserver->sentinel_config->pre_monitor_cfg);\n    listRelease(g_pserver->sentinel_config->monitor_cfg);\n    listRelease(g_pserver->sentinel_config->post_monitor_cfg);\n    zfree(g_pserver->sentinel_config);\n    g_pserver->sentinel_config = NULL;\n}\n\n/* Search config name in pre monitor config name array, return 1 if found,\n * 0 if not found. */\nint searchPreMonitorCfgName(const char *name) {\n    for (unsigned int i = 0; i < sizeof(preMonitorCfgName)/sizeof(preMonitorCfgName[0]); i++) {\n        if (!strcasecmp(preMonitorCfgName[i],name)) return 1;\n    }\n    return 0;\n}\n\n/* free method for sentinelLoadQueueEntry when release the list */\nvoid freeSentinelLoadQueueEntry(const void *item) {\n    const sentinelLoadQueueEntry *entry = (const sentinelLoadQueueEntry*) item;\n    sdsfreesplitres(entry->argv,entry->argc);\n    sdsfree(entry->line);\n    zfree(entry);\n}\n\n/* This function is used for queuing sentinel configuration, the main\n * purpose of this function is to delay parsing the sentinel config option\n * in order to avoid the order dependent issue from the config. */\nvoid queueSentinelConfig(sds *argv, int argc, int linenum, sds line) {\n    int i;\n    struct sentinelLoadQueueEntry *entry;\n\n    /* initialize sentinel_config for the first call */\n    if (g_pserver->sentinel_config == NULL) initializeSentinelConfig();\n\n    entry = (sentinelLoadQueueEntry*)zmalloc(sizeof(struct sentinelLoadQueueEntry));\n    entry->argv = (char**)zmalloc(sizeof(char*)*argc);\n    entry->argc = argc;\n    entry->linenum = linenum;\n    entry->line = sdsdup(line);\n    for (i = 0; i < argc; i++) {\n        entry->argv[i] = sdsdup(argv[i]);\n    }\n    /*  Separate config lines with pre monitor config, monitor config and\n     *  post monitor config, in order to parsing config dependencies\n     *  correctly. */\n    if (!strcasecmp(argv[0],\"monitor\")) {\n        listAddNodeTail(g_pserver->sentinel_config->monitor_cfg,entry);\n    } else if (searchPreMonitorCfgName(argv[0])) {\n        listAddNodeTail(g_pserver->sentinel_config->pre_monitor_cfg,entry);\n    } else{\n        listAddNodeTail(g_pserver->sentinel_config->post_monitor_cfg,entry);\n    }\n}\n\n/* This function is used for loading the sentinel configuration from\n * pre_monitor_cfg, monitor_cfg and post_monitor_cfg list */\nvoid loadSentinelConfigFromQueue(void) {\n    const char *err = NULL;\n    listIter li;\n    listNode *ln;\n    int linenum = 0;\n    sds line = NULL;\n\n    /* if there is no sentinel_config entry, we can return immediately */\n    if (g_pserver->sentinel_config == NULL) return;\n\n    /* loading from pre monitor config queue first to avoid dependency issues */\n    listRewind(g_pserver->sentinel_config->pre_monitor_cfg,&li);\n    while((ln = listNext(&li))) {\n        struct sentinelLoadQueueEntry *entry = (sentinelLoadQueueEntry*)ln->value;\n        err = sentinelHandleConfiguration(entry->argv,entry->argc);\n        if (err) {\n            linenum = entry->linenum;\n            line = entry->line;\n            goto loaderr;\n        }\n    }\n\n    /* loading from monitor config queue */\n    listRewind(g_pserver->sentinel_config->monitor_cfg,&li);\n    while((ln = listNext(&li))) {\n        struct sentinelLoadQueueEntry *entry = (sentinelLoadQueueEntry*)ln->value;\n        err = sentinelHandleConfiguration(entry->argv,entry->argc);\n        if (err) {\n            linenum = entry->linenum;\n            line = entry->line;\n            goto loaderr;\n        }\n    }\n\n    /* loading from the post monitor config queue */\n    listRewind(g_pserver->sentinel_config->post_monitor_cfg,&li);\n    while((ln = listNext(&li))) {\n        struct sentinelLoadQueueEntry *entry = (sentinelLoadQueueEntry*)ln->value;\n        err = sentinelHandleConfiguration(entry->argv,entry->argc);\n        if (err) {\n            linenum = entry->linenum;\n            line = entry->line;\n            goto loaderr;\n        }\n    }\n\n    /* free sentinel_config when config loading is finished */\n    freeSentinelConfig();\n    return;\n\nloaderr:\n    fprintf(stderr, \"\\n*** FATAL CONFIG FILE ERROR (Redis %s) ***\\n\",\n        KEYDB_REAL_VERSION);\n    fprintf(stderr, \"Reading the configuration file, at line %d\\n\", linenum);\n    fprintf(stderr, \">>> '%s'\\n\", line);\n    fprintf(stderr, \"%s\\n\", err);\n    exit(1);\n}\n\nconst char *sentinelHandleConfiguration(char **argv, int argc) {\n\n    sentinelRedisInstance *ri;\n\n    if (!strcasecmp(argv[0],\"monitor\") && argc == 5) {\n        /* monitor <name> <host> <port> <quorum> */\n        int quorum = atoi(argv[4]);\n\n        if (quorum <= 0) return \"Quorum must be 1 or greater.\";\n        if (createSentinelRedisInstance(argv[1],SRI_MASTER,argv[2],\n                                        atoi(argv[3]),quorum,NULL) == NULL)\n        {\n            return sentinelCheckCreateInstanceErrors(SRI_MASTER);\n        }\n    } else if (!strcasecmp(argv[0],\"down-after-milliseconds\") && argc == 3) {\n        /* down-after-milliseconds <name> <milliseconds> */\n        ri = sentinelGetMasterByName(argv[1]);\n        if (!ri) return \"No such master with specified name.\";\n        ri->down_after_period = atoi(argv[2]);\n        if (ri->down_after_period <= 0)\n            return \"negative or zero time parameter.\";\n        sentinelPropagateDownAfterPeriod(ri);\n    } else if (!strcasecmp(argv[0],\"failover-timeout\") && argc == 3) {\n        /* failover-timeout <name> <milliseconds> */\n        ri = sentinelGetMasterByName(argv[1]);\n        if (!ri) return \"No such master with specified name.\";\n        ri->failover_timeout = atoi(argv[2]);\n        if (ri->failover_timeout <= 0)\n            return \"negative or zero time parameter.\";\n    } else if (!strcasecmp(argv[0],\"parallel-syncs\") && argc == 3) {\n        /* parallel-syncs <name> <milliseconds> */\n        ri = sentinelGetMasterByName(argv[1]);\n        if (!ri) return \"No such master with specified name.\";\n        ri->parallel_syncs = atoi(argv[2]);\n    } else if (!strcasecmp(argv[0],\"notification-script\") && argc == 3) {\n        /* notification-script <name> <path> */\n        ri = sentinelGetMasterByName(argv[1]);\n        if (!ri) return \"No such master with specified name.\";\n        if (access(argv[2],X_OK) == -1)\n            return \"Notification script seems non existing or non executable.\";\n        ri->notification_script = sdsnew(argv[2]);\n    } else if (!strcasecmp(argv[0],\"client-reconfig-script\") && argc == 3) {\n        /* client-reconfig-script <name> <path> */\n        ri = sentinelGetMasterByName(argv[1]);\n        if (!ri) return \"No such master with specified name.\";\n        if (access(argv[2],X_OK) == -1)\n            return \"Client reconfiguration script seems non existing or \"\n                   \"non executable.\";\n        ri->client_reconfig_script = sdsnew(argv[2]);\n    } else if (!strcasecmp(argv[0],\"auth-pass\") && argc == 3) {\n        /* auth-pass <name> <password> */\n        ri = sentinelGetMasterByName(argv[1]);\n        if (!ri) return \"No such master with specified name.\";\n        ri->auth_pass = sdsnew(argv[2]);\n    } else if (!strcasecmp(argv[0],\"auth-user\") && argc == 3) {\n        /* auth-user <name> <username> */\n        ri = sentinelGetMasterByName(argv[1]);\n        if (!ri) return \"No such master with specified name.\";\n        ri->auth_user = sdsnew(argv[2]);\n    } else if (!strcasecmp(argv[0],\"current-epoch\") && argc == 2) {\n        /* current-epoch <epoch> */\n        unsigned long long current_epoch = strtoull(argv[1],NULL,10);\n        if (current_epoch > sentinel.current_epoch)\n            sentinel.current_epoch = current_epoch;\n    } else if (!strcasecmp(argv[0],\"myid\") && argc == 2) {\n        if (strlen(argv[1]) != CONFIG_RUN_ID_SIZE)\n            return \"Malformed Sentinel id in myid option.\";\n        memcpy(sentinel.myid,argv[1],CONFIG_RUN_ID_SIZE);\n    } else if (!strcasecmp(argv[0],\"config-epoch\") && argc == 3) {\n        /* config-epoch <name> <epoch> */\n        ri = sentinelGetMasterByName(argv[1]);\n        if (!ri) return \"No such master with specified name.\";\n        ri->config_epoch = strtoull(argv[2],NULL,10);\n        /* The following update of current_epoch is not really useful as\n         * now the current epoch is persisted on the config file, but\n         * we leave this check here for redundancy. */\n        if (ri->config_epoch > sentinel.current_epoch)\n            sentinel.current_epoch = ri->config_epoch;\n    } else if (!strcasecmp(argv[0],\"leader-epoch\") && argc == 3) {\n        /* leader-epoch <name> <epoch> */\n        ri = sentinelGetMasterByName(argv[1]);\n        if (!ri) return \"No such master with specified name.\";\n        ri->leader_epoch = strtoull(argv[2],NULL,10);\n    } else if ((!strcasecmp(argv[0],\"known-slave\") ||\n                !strcasecmp(argv[0],\"known-replica\")) && argc == 4)\n    {\n        sentinelRedisInstance *slave;\n\n        /* known-replica <name> <ip> <port> */\n        ri = sentinelGetMasterByName(argv[1]);\n        if (!ri) return \"No such master with specified name.\";\n        if ((slave = createSentinelRedisInstance(NULL,SRI_SLAVE,argv[2],\n                    atoi(argv[3]), ri->quorum, ri)) == NULL)\n        {\n            return sentinelCheckCreateInstanceErrors(SRI_SLAVE);\n        }\n    } else if (!strcasecmp(argv[0],\"known-sentinel\") &&\n               (argc == 4 || argc == 5)) {\n        sentinelRedisInstance *si;\n\n        if (argc == 5) { /* Ignore the old form without runid. */\n            /* known-sentinel <name> <ip> <port> [runid] */\n            ri = sentinelGetMasterByName(argv[1]);\n            if (!ri) return \"No such master with specified name.\";\n            if ((si = createSentinelRedisInstance(argv[4],SRI_SENTINEL,argv[2],\n                        atoi(argv[3]), ri->quorum, ri)) == NULL)\n            {\n                return sentinelCheckCreateInstanceErrors(SRI_SENTINEL);\n            }\n            si->runid = sdsnew(argv[4]);\n            sentinelTryConnectionSharing(si);\n        }\n    } else if (!strcasecmp(argv[0],\"rename-command\") && argc == 4) {\n        /* rename-command <name> <command> <renamed-command> */\n        ri = sentinelGetMasterByName(argv[1]);\n        if (!ri) return \"No such master with specified name.\";\n        sds oldcmd = sdsnew(argv[2]);\n        sds newcmd = sdsnew(argv[3]);\n        if (dictAdd(ri->renamed_commands,oldcmd,newcmd) != DICT_OK) {\n            sdsfree(oldcmd);\n            sdsfree(newcmd);\n            return \"Same command renamed multiple times with rename-command.\";\n        }\n    } else if (!strcasecmp(argv[0],\"announce-ip\") && argc == 2) {\n        /* announce-ip <ip-address> */\n        if (strlen(argv[1]))\n            sentinel.announce_ip = sdsnew(argv[1]);\n    } else if (!strcasecmp(argv[0],\"announce-port\") && argc == 2) {\n        /* announce-port <port> */\n        sentinel.announce_port = atoi(argv[1]);\n    } else if (!strcasecmp(argv[0],\"deny-scripts-reconfig\") && argc == 2) {\n        /* deny-scripts-reconfig <yes|no> */\n        if ((sentinel.deny_scripts_reconfig = yesnotoi(argv[1])) == -1) {\n            return \"Please specify yes or no for the \"\n                   \"deny-scripts-reconfig options.\";\n        }\n    } else if (!strcasecmp(argv[0],\"sentinel-user\") && argc == 2) {\n        /* sentinel-user <user-name> */\n        if (strlen(argv[1]))\n            sentinel.sentinel_auth_user = sdsnew(argv[1]);\n    } else if (!strcasecmp(argv[0],\"sentinel-pass\") && argc == 2) {\n        /* sentinel-pass <password> */\n        if (strlen(argv[1]))\n            sentinel.sentinel_auth_pass = sdsnew(argv[1]);\n    } else if (!strcasecmp(argv[0],\"resolve-hostnames\") && argc == 2) {\n        /* resolve-hostnames <yes|no> */\n        if ((sentinel.resolve_hostnames = yesnotoi(argv[1])) == -1) {\n            return \"Please specify yes or no for the resolve-hostnames option.\";\n        }\n    } else if (!strcasecmp(argv[0],\"announce-hostnames\") && argc == 2) {\n        /* announce-hostnames <yes|no> */\n        if ((sentinel.announce_hostnames = yesnotoi(argv[1])) == -1) {\n            return \"Please specify yes or no for the announce-hostnames option.\";\n        }\n    } else {\n        return \"Unrecognized sentinel configuration statement.\";\n    }\n    return NULL;\n}\n\n/* Implements CONFIG REWRITE for \"sentinel\" option.\n * This is used not just to rewrite the configuration given by the user\n * (the configured masters) but also in order to retain the state of\n * Sentinel across restarts: config epoch of masters, associated slaves\n * and sentinel instances, and so forth. */\nvoid rewriteConfigSentinelOption(struct rewriteConfigState *state) {\n    dictIterator *di, *di2;\n    dictEntry *de;\n    sds line;\n\n    /* sentinel unique ID. */\n    line = sdscatprintf(sdsempty(), \"sentinel myid %s\", sentinel.myid);\n    rewriteConfigRewriteLine(state,\"sentinel myid\",line,1);\n\n    /* sentinel deny-scripts-reconfig. */\n    line = sdscatprintf(sdsempty(), \"sentinel deny-scripts-reconfig %s\",\n        sentinel.deny_scripts_reconfig ? \"yes\" : \"no\");\n    rewriteConfigRewriteLine(state,\"sentinel deny-scripts-reconfig\",line,\n        sentinel.deny_scripts_reconfig != SENTINEL_DEFAULT_DENY_SCRIPTS_RECONFIG);\n\n    /* sentinel resolve-hostnames.\n     * This must be included early in the file so it is already in effect\n     * when reading the file.\n     */\n    line = sdscatprintf(sdsempty(), \"sentinel resolve-hostnames %s\",\n                        sentinel.resolve_hostnames ? \"yes\" : \"no\");\n    rewriteConfigRewriteLine(state,\"sentinel resolve-hostnames\",line,\n                             sentinel.resolve_hostnames != SENTINEL_DEFAULT_RESOLVE_HOSTNAMES);\n\n    /* sentinel announce-hostnames. */\n    line = sdscatprintf(sdsempty(), \"sentinel announce-hostnames %s\",\n                        sentinel.announce_hostnames ? \"yes\" : \"no\");\n    rewriteConfigRewriteLine(state,\"sentinel announce-hostnames\",line,\n                             sentinel.announce_hostnames != SENTINEL_DEFAULT_ANNOUNCE_HOSTNAMES);\n\n    /* For every master emit a \"sentinel monitor\" config entry. */\n    di = dictGetIterator(sentinel.masters);\n    while((de = dictNext(di)) != NULL) {\n        sentinelRedisInstance *master, *ri;\n        sentinelAddr *master_addr;\n\n        /* sentinel monitor */\n        master = (sentinelRedisInstance*)dictGetVal(de);\n        master_addr = sentinelGetCurrentMasterAddress(master);\n        line = sdscatprintf(sdsempty(),\"sentinel monitor %s %s %d %d\",\n            master->name, announceSentinelAddr(master_addr), master_addr->port,\n            master->quorum);\n        rewriteConfigRewriteLine(state,\"sentinel monitor\",line,1);\n        /* rewriteConfigMarkAsProcessed is handled after the loop */\n\n        /* sentinel down-after-milliseconds */\n        if (master->down_after_period != SENTINEL_DEFAULT_DOWN_AFTER) {\n            line = sdscatprintf(sdsempty(),\n                \"sentinel down-after-milliseconds %s %ld\",\n                master->name, (long) master->down_after_period);\n            rewriteConfigRewriteLine(state,\"sentinel down-after-milliseconds\",line,1);\n            /* rewriteConfigMarkAsProcessed is handled after the loop */\n        }\n\n        /* sentinel failover-timeout */\n        if (master->failover_timeout != SENTINEL_DEFAULT_FAILOVER_TIMEOUT) {\n            line = sdscatprintf(sdsempty(),\n                \"sentinel failover-timeout %s %ld\",\n                master->name, (long) master->failover_timeout);\n            rewriteConfigRewriteLine(state,\"sentinel failover-timeout\",line,1);\n            /* rewriteConfigMarkAsProcessed is handled after the loop */\n\n        }\n\n        /* sentinel parallel-syncs */\n        if (master->parallel_syncs != SENTINEL_DEFAULT_PARALLEL_SYNCS) {\n            line = sdscatprintf(sdsempty(),\n                \"sentinel parallel-syncs %s %d\",\n                master->name, master->parallel_syncs);\n            rewriteConfigRewriteLine(state,\"sentinel parallel-syncs\",line,1);\n            /* rewriteConfigMarkAsProcessed is handled after the loop */\n        }\n\n        /* sentinel notification-script */\n        if (master->notification_script) {\n            line = sdscatprintf(sdsempty(),\n                \"sentinel notification-script %s %s\",\n                master->name, master->notification_script);\n            rewriteConfigRewriteLine(state,\"sentinel notification-script\",line,1);\n            /* rewriteConfigMarkAsProcessed is handled after the loop */\n        }\n\n        /* sentinel client-reconfig-script */\n        if (master->client_reconfig_script) {\n            line = sdscatprintf(sdsempty(),\n                \"sentinel client-reconfig-script %s %s\",\n                master->name, master->client_reconfig_script);\n            rewriteConfigRewriteLine(state,\"sentinel client-reconfig-script\",line,1);\n            /* rewriteConfigMarkAsProcessed is handled after the loop */\n        }\n\n        /* sentinel auth-pass & auth-user */\n        if (master->auth_pass) {\n            line = sdscatprintf(sdsempty(),\n                \"sentinel auth-pass %s %s\",\n                master->name, master->auth_pass);\n            rewriteConfigRewriteLine(state,\"sentinel auth-pass\",line,1);\n            /* rewriteConfigMarkAsProcessed is handled after the loop */\n        }\n\n        if (master->auth_user) {\n            line = sdscatprintf(sdsempty(),\n                \"sentinel auth-user %s %s\",\n                master->name, master->auth_user);\n            rewriteConfigRewriteLine(state,\"sentinel auth-user\",line,1);\n            /* rewriteConfigMarkAsProcessed is handled after the loop */\n        }\n\n        if (master->auth_user) {\n            line = sdscatprintf(sdsempty(),\n                \"sentinel auth-user %s %s\",\n                master->name, master->auth_user);\n            rewriteConfigRewriteLine(state,\"sentinel\",line,1);\n        }\n\n        /* sentinel config-epoch */\n        line = sdscatprintf(sdsempty(),\n            \"sentinel config-epoch %s %llu\",\n            master->name, (unsigned long long) master->config_epoch);\n        rewriteConfigRewriteLine(state,\"sentinel config-epoch\",line,1);\n        /* rewriteConfigMarkAsProcessed is handled after the loop */\n\n\n        /* sentinel leader-epoch */\n        line = sdscatprintf(sdsempty(),\n            \"sentinel leader-epoch %s %llu\",\n            master->name, (unsigned long long) master->leader_epoch);\n        rewriteConfigRewriteLine(state,\"sentinel leader-epoch\",line,1);\n        /* rewriteConfigMarkAsProcessed is handled after the loop */\n\n        /* sentinel known-slave */\n        di2 = dictGetIterator(master->slaves);\n        while((de = dictNext(di2)) != NULL) {\n            sentinelAddr *slave_addr;\n\n            ri = (sentinelRedisInstance*)dictGetVal(de);\n            slave_addr = ri->addr;\n\n            /* If master_addr (obtained using sentinelGetCurrentMasterAddress()\n             * so it may be the address of the promoted slave) is equal to this\n             * slave's address, a failover is in progress and the slave was\n             * already successfully promoted. So as the address of this slave\n             * we use the old master address instead. */\n            if (sentinelAddrIsEqual(slave_addr,master_addr))\n                slave_addr = master->addr;\n            line = sdscatprintf(sdsempty(),\n                \"sentinel known-replica %s %s %d\",\n                master->name, announceSentinelAddr(slave_addr), slave_addr->port);\n            rewriteConfigRewriteLine(state,\"sentinel known-replica\",line,1);\n            /* rewriteConfigMarkAsProcessed is handled after the loop */\n        }\n        dictReleaseIterator(di2);\n\n        /* sentinel known-sentinel */\n        di2 = dictGetIterator(master->sentinels);\n        while((de = dictNext(di2)) != NULL) {\n            ri = (sentinelRedisInstance*)dictGetVal(de);\n            if (ri->runid == NULL) continue;\n            line = sdscatprintf(sdsempty(),\n                \"sentinel known-sentinel %s %s %d %s\",\n                master->name, announceSentinelAddr(ri->addr), ri->addr->port, ri->runid);\n            rewriteConfigRewriteLine(state,\"sentinel known-sentinel\",line,1);\n            /* rewriteConfigMarkAsProcessed is handled after the loop */\n        }\n        dictReleaseIterator(di2);\n\n        /* sentinel rename-command */\n        di2 = dictGetIterator(master->renamed_commands);\n        while((de = dictNext(di2)) != NULL) {\n            sds oldname = (sds)dictGetKey(de);\n            sds newname = (sds)dictGetVal(de);\n            line = sdscatprintf(sdsempty(),\n                \"sentinel rename-command %s %s %s\",\n                master->name, oldname, newname);\n            rewriteConfigRewriteLine(state,\"sentinel rename-command\",line,1);\n            /* rewriteConfigMarkAsProcessed is handled after the loop */\n        }\n        dictReleaseIterator(di2);\n    }\n\n    /* sentinel current-epoch is a global state valid for all the masters. */\n    line = sdscatprintf(sdsempty(),\n        \"sentinel current-epoch %llu\", (unsigned long long) sentinel.current_epoch);\n    rewriteConfigRewriteLine(state,\"sentinel current-epoch\",line,1);\n\n    /* sentinel announce-ip. */\n    if (sentinel.announce_ip) {\n        line = sdsnew(\"sentinel announce-ip \");\n        line = sdscatrepr(line, sentinel.announce_ip, sdslen(sentinel.announce_ip));\n        rewriteConfigRewriteLine(state,\"sentinel announce-ip\",line,1);\n    } else {\n        rewriteConfigMarkAsProcessed(state,\"sentinel announce-ip\");\n    }\n\n    /* sentinel announce-port. */\n    if (sentinel.announce_port) {\n        line = sdscatprintf(sdsempty(),\"sentinel announce-port %d\",\n                            sentinel.announce_port);\n        rewriteConfigRewriteLine(state,\"sentinel announce-port\",line,1);\n    } else {\n        rewriteConfigMarkAsProcessed(state,\"sentinel announce-port\");\n    }\n\n    /* sentinel sentinel-user. */\n    if (sentinel.sentinel_auth_user) {\n        line = sdscatprintf(sdsempty(), \"sentinel sentinel-user %s\", sentinel.sentinel_auth_user);\n        rewriteConfigRewriteLine(state,\"sentinel sentinel-user\",line,1);\n    } else {\n        rewriteConfigMarkAsProcessed(state,\"sentinel sentinel-user\");\n    }\n\n    /* sentinel sentinel-pass. */\n    if (sentinel.sentinel_auth_pass) {\n        line = sdscatprintf(sdsempty(), \"sentinel sentinel-pass %s\", sentinel.sentinel_auth_pass);\n        rewriteConfigRewriteLine(state,\"sentinel sentinel-pass\",line,1);\n    } else {\n        rewriteConfigMarkAsProcessed(state,\"sentinel sentinel-pass\");  \n    }\n\n    dictReleaseIterator(di);\n\n    /* NOTE: the purpose here is in case due to the state change, the config rewrite \n     does not handle the configs, however, previously the config was set in the config file, \n     rewriteConfigMarkAsProcessed should be put here to mark it as processed in order to \n     delete the old config entry.\n    */\n    rewriteConfigMarkAsProcessed(state,\"sentinel monitor\");\n    rewriteConfigMarkAsProcessed(state,\"sentinel down-after-milliseconds\");\n    rewriteConfigMarkAsProcessed(state,\"sentinel failover-timeout\");\n    rewriteConfigMarkAsProcessed(state,\"sentinel parallel-syncs\");\n    rewriteConfigMarkAsProcessed(state,\"sentinel notification-script\");\n    rewriteConfigMarkAsProcessed(state,\"sentinel client-reconfig-script\");\n    rewriteConfigMarkAsProcessed(state,\"sentinel auth-pass\");\n    rewriteConfigMarkAsProcessed(state,\"sentinel auth-user\");\n    rewriteConfigMarkAsProcessed(state,\"sentinel config-epoch\");\n    rewriteConfigMarkAsProcessed(state,\"sentinel leader-epoch\");\n    rewriteConfigMarkAsProcessed(state,\"sentinel known-replica\");\n    rewriteConfigMarkAsProcessed(state,\"sentinel known-sentinel\");\n    rewriteConfigMarkAsProcessed(state,\"sentinel rename-command\");\n}\n\n/* This function uses the config rewriting Redis engine in order to persist\n * the state of the Sentinel in the current configuration file.\n *\n * Before returning the function calls fsync() against the generated\n * configuration file to make sure changes are committed to disk.\n *\n * On failure the function logs a warning on the Redis log. */\nvoid sentinelFlushConfig(void) {\n    int fd = -1;\n    int saved_hz = g_pserver->hz;\n    int rewrite_status;\n\n    g_pserver->hz = CONFIG_DEFAULT_HZ;\n    rewrite_status = rewriteConfig(cserver.configfile, 0);\n    g_pserver->hz = saved_hz;\n\n    if (rewrite_status == -1) goto werr;\n    if ((fd = open(cserver.configfile,O_RDONLY)) == -1) goto werr;\n    if (fsync(fd) == -1) goto werr;\n    if (close(fd) == EOF) goto werr;\n    return;\n\nwerr:\n    serverLog(LL_WARNING,\"WARNING: Sentinel was not able to save the new configuration on disk!!!: %s\", strerror(errno));\n    if (fd != -1) close(fd);\n}\n\n/* ====================== hiredis connection handling ======================= */\n\n/* Send the AUTH command with the specified master password if needed.\n * Note that for slaves the password set for the master is used.\n *\n * In case this Sentinel requires a password as well, via the \"requirepass\"\n * configuration directive, we assume we should use the local password in\n * order to authenticate when connecting with the other Sentinels as well.\n * So basically all the Sentinels share the same password and use it to\n * authenticate reciprocally.\n *\n * We don't check at all if the command was successfully transmitted\n * to the instance as if it fails Sentinel will detect the instance down,\n * will disconnect and reconnect the link and so forth. */\nvoid sentinelSendAuthIfNeeded(sentinelRedisInstance *ri, redisAsyncContext *c) {\n    char *auth_pass = NULL;\n    char *auth_user = NULL;\n\n    if (ri->flags & SRI_MASTER) {\n        auth_pass = ri->auth_pass;\n        auth_user = ri->auth_user;\n    } else if (ri->flags & SRI_SLAVE) {\n        auth_pass = ri->master->auth_pass;\n        auth_user = ri->master->auth_user;\n    } else if (ri->flags & SRI_SENTINEL) {\n        /* If sentinel_auth_user is NULL, AUTH will use default user\n           with sentinel_auth_pass to authenticate */\n        if (sentinel.sentinel_auth_pass) {\n            auth_pass = sentinel.sentinel_auth_pass;\n            auth_user = sentinel.sentinel_auth_user;\n        } else {\n            /* Compatibility with old configs. requirepass is used\n             * for both incoming and outgoing authentication. */\n            auth_pass = g_pserver->requirepass;\n            auth_user = NULL;\n        }\n    }\n\n    if (auth_pass && auth_user == NULL) {\n        if (redisAsyncCommand(c, sentinelDiscardReplyCallback, ri, \"%s %s\",\n            sentinelInstanceMapCommand(ri,\"AUTH\"),\n            auth_pass) == C_OK) ri->link->pending_commands++;\n    } else if (auth_pass && auth_user) {\n        /* If we also have an username, use the ACL-style AUTH command\n         * with two arguments, username and password. */\n        if (redisAsyncCommand(c, sentinelDiscardReplyCallback, ri, \"%s %s %s\",\n            sentinelInstanceMapCommand(ri,\"AUTH\"),\n            auth_user, auth_pass) == C_OK) ri->link->pending_commands++;\n    }\n}\n\n/* Use CLIENT SETNAME to name the connection in the Redis instance as\n * sentinel-<first_8_chars_of_runid>-<connection_type>\n * The connection type is \"cmd\" or \"pubsub\" as specified by 'type'.\n *\n * This makes it possible to list all the sentinel instances connected\n * to a Redis server with CLIENT LIST, grepping for a specific name format. */\nvoid sentinelSetClientName(sentinelRedisInstance *ri, redisAsyncContext *c, const char *type) {\n    char name[64];\n\n    snprintf(name,sizeof(name),\"sentinel-%.8s-%s\",sentinel.myid,type);\n    if (redisAsyncCommand(c, sentinelDiscardReplyCallback, ri,\n        \"%s SETNAME %s\",\n        sentinelInstanceMapCommand(ri,\"CLIENT\"),\n        name) == C_OK)\n    {\n        ri->link->pending_commands++;\n    }\n}\n\nstatic int instanceLinkNegotiateTLS(redisAsyncContext *context) {\n#ifndef USE_OPENSSL\n    (void) context;\n#else\n    if (!redis_tls_ctx) return C_ERR;\n    SSL *ssl = SSL_new(redis_tls_client_ctx ? redis_tls_client_ctx : redis_tls_ctx);\n    if (!ssl) return C_ERR;\n\n    if (redisInitiateSSL(&context->c, ssl) == REDIS_ERR) return C_ERR;\n#endif\n    return C_OK;\n}\n\n/* Create the async connections for the instance link if the link\n * is disconnected. Note that link->disconnected is true even if just\n * one of the two links (commands and pub/sub) is missing. */\nvoid sentinelReconnectInstance(sentinelRedisInstance *ri) {\n    if (ri->link->disconnected == 0) return;\n    if (ri->addr->port == 0) return; /* port == 0 means invalid address. */\n    instanceLink *link = ri->link;\n    mstime_t now = mstime();\n\n    if (now - ri->link->last_reconn_time < SENTINEL_PING_PERIOD) return;\n    ri->link->last_reconn_time = now;\n\n    /* Commands connection. */\n    if (link->cc == NULL) {\n        link->cc = redisAsyncConnectBind(ri->addr->ip,ri->addr->port,NET_FIRST_BIND_ADDR);\n        if (link->cc && !link->cc->err) anetCloexec(link->cc->c.fd);\n        if (!link->cc) {\n            sentinelEvent(LL_DEBUG,\"-cmd-link-reconnection\",ri,\"%@ #Failed to establish connection\");\n        } else if (!link->cc->err && g_pserver->tls_replication &&\n                (instanceLinkNegotiateTLS(link->cc) == C_ERR)) {\n            sentinelEvent(LL_DEBUG,\"-cmd-link-reconnection\",ri,\"%@ #Failed to initialize TLS\");\n            instanceLinkCloseConnection(link,link->cc);\n        } else if (link->cc->err) {\n            sentinelEvent(LL_DEBUG,\"-cmd-link-reconnection\",ri,\"%@ #%s\",\n                link->cc->errstr);\n            instanceLinkCloseConnection(link,link->cc);\n        } else {\n            link->pending_commands = 0;\n            link->cc_conn_time = mstime();\n            link->cc->data = link;\n            redisAeAttach(g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].el,link->cc);\n            redisAsyncSetConnectCallback(link->cc,\n                    sentinelLinkEstablishedCallback);\n            redisAsyncSetDisconnectCallback(link->cc,\n                    sentinelDisconnectCallback);\n            sentinelSendAuthIfNeeded(ri,link->cc);\n            sentinelSetClientName(ri,link->cc,\"cmd\");\n\n            /* Send a PING ASAP when reconnecting. */\n            sentinelSendPing(ri);\n        }\n    }\n    /* Pub / Sub */\n    if ((ri->flags & (SRI_MASTER|SRI_SLAVE)) && link->pc == NULL) {\n        link->pc = redisAsyncConnectBind(ri->addr->ip,ri->addr->port,NET_FIRST_BIND_ADDR);\n        if (link->pc && !link->pc->err) anetCloexec(link->pc->c.fd);\n        if (!link->pc) {\n            sentinelEvent(LL_DEBUG,\"-pubsub-link-reconnection\",ri,\"%@ #Failed to establish connection\");\n        } else if (!link->pc->err && g_pserver->tls_replication &&\n                (instanceLinkNegotiateTLS(link->pc) == C_ERR)) {\n            sentinelEvent(LL_DEBUG,\"-pubsub-link-reconnection\",ri,\"%@ #Failed to initialize TLS\");\n        } else if (link->pc->err) {\n            sentinelEvent(LL_DEBUG,\"-pubsub-link-reconnection\",ri,\"%@ #%s\",\n                link->pc->errstr);\n            instanceLinkCloseConnection(link,link->pc);\n        } else {\n            int retval;\n            link->pc_conn_time = mstime();\n            link->pc->data = link;\n            redisAeAttach(g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].el,link->pc);\n            redisAsyncSetConnectCallback(link->pc,\n                    sentinelLinkEstablishedCallback);\n            redisAsyncSetDisconnectCallback(link->pc,\n                    sentinelDisconnectCallback);\n            sentinelSendAuthIfNeeded(ri,link->pc);\n            sentinelSetClientName(ri,link->pc,\"pubsub\");\n            /* Now we subscribe to the Sentinels \"Hello\" channel. */\n            retval = redisAsyncCommand(link->pc,\n                sentinelReceiveHelloMessages, ri, \"%s %s\",\n                sentinelInstanceMapCommand(ri,\"SUBSCRIBE\"),\n                SENTINEL_HELLO_CHANNEL);\n            if (retval != C_OK) {\n                /* If we can't subscribe, the Pub/Sub connection is useless\n                 * and we can simply disconnect it and try again. */\n                instanceLinkCloseConnection(link,link->pc);\n                return;\n            }\n        }\n    }\n    /* Clear the disconnected status only if we have both the connections\n     * (or just the commands connection if this is a sentinel instance). */\n    if (link->cc && (ri->flags & SRI_SENTINEL || link->pc))\n        link->disconnected = 0;\n}\n\n/* ======================== Redis instances pinging  ======================== */\n\n/* Return true if master looks \"sane\", that is:\n * 1) It is actually a master in the current configuration.\n * 2) It reports itself as a master.\n * 3) It is not SDOWN or ODOWN.\n * 4) We obtained last INFO no more than two times the INFO period time ago. */\nint sentinelMasterLooksSane(sentinelRedisInstance *master) {\n    return\n        master->flags & SRI_MASTER &&\n        master->role_reported == SRI_MASTER &&\n        (master->flags & (SRI_S_DOWN|SRI_O_DOWN)) == 0 &&\n        (mstime() - master->info_refresh) < SENTINEL_INFO_PERIOD*2;\n}\n\n/* Process the INFO output from masters. */\nvoid sentinelRefreshInstanceInfo(sentinelRedisInstance *ri, const char *info) {\n    sds *lines;\n    int numlines, j;\n    int role = 0;\n\n    /* cache full INFO output for instance */\n    sdsfree(ri->info);\n    ri->info = sdsnew(info);\n\n    /* The following fields must be reset to a given value in the case they\n     * are not found at all in the INFO output. */\n    ri->master_link_down_time = 0;\n\n    /* Process line by line. */\n    lines = sdssplitlen(info,strlen(info),\"\\r\\n\",2,&numlines);\n    for (j = 0; j < numlines; j++) {\n        sentinelRedisInstance *slave;\n        sds l = lines[j];\n\n        /* run_id:<40 hex chars>*/\n        if (sdslen(l) >= 47 && !memcmp(l,\"run_id:\",7)) {\n            if (ri->runid == NULL) {\n                ri->runid = sdsnewlen(l+7,40);\n            } else {\n                if (strncmp(ri->runid,l+7,40) != 0) {\n                    sentinelEvent(LL_NOTICE,\"+reboot\",ri,\"%@\");\n                    sdsfree(ri->runid);\n                    ri->runid = sdsnewlen(l+7,40);\n                }\n            }\n        }\n\n        /* old versions: slave0:<ip>,<port>,<state>\n         * new versions: slave0:ip=127.0.0.1,port=9999,... */\n        if ((ri->flags & SRI_MASTER) &&\n            sdslen(l) >= 7 &&\n            !memcmp(l,\"slave\",5) && isdigit(l[5]))\n        {\n            char *ip, *port, *end;\n\n            if (strstr(l,\"ip=\") == NULL) {\n                /* Old format. */\n                ip = strchr(l,':'); if (!ip) continue;\n                ip++; /* Now ip points to start of ip address. */\n                port = strchr(ip,','); if (!port) continue;\n                *port = '\\0'; /* nul term for easy access. */\n                port++; /* Now port points to start of port number. */\n                end = strchr(port,','); if (!end) continue;\n                *end = '\\0'; /* nul term for easy access. */\n            } else {\n                /* New format. */\n                ip = strstr(l,\"ip=\"); if (!ip) continue;\n                ip += 3; /* Now ip points to start of ip address. */\n                port = strstr(l,\"port=\"); if (!port) continue;\n                port += 5; /* Now port points to start of port number. */\n                /* Nul term both fields for easy access. */\n                end = strchr(ip,','); if (end) *end = '\\0';\n                end = strchr(port,','); if (end) *end = '\\0';\n            }\n\n            /* Check if we already have this slave into our table,\n             * otherwise add it. */\n            if (sentinelRedisInstanceLookupSlave(ri,ip,atoi(port)) == NULL) {\n                if ((slave = createSentinelRedisInstance(NULL,SRI_SLAVE,ip,\n                            atoi(port), ri->quorum, ri)) != NULL)\n                {\n                    sentinelEvent(LL_NOTICE,\"+slave\",slave,\"%@\");\n                    sentinelFlushConfig();\n                }\n            }\n        }\n\n        /* master_link_down_since_seconds:<seconds> */\n        if (sdslen(l) >= 32 &&\n            !memcmp(l,\"master_link_down_since_seconds\",30))\n        {\n            ri->master_link_down_time = strtoll(l+31,NULL,10)*1000;\n        }\n\n        /* role:<role> */\n        if (sdslen(l) >= 11 && !memcmp(l,\"role:master\",11)) role = SRI_MASTER;\n        else if (sdslen(l) >= 10 && !memcmp(l,\"role:slave\",10)) role = SRI_SLAVE;\n\n        if (role == SRI_SLAVE) {\n            /* master_host:<host> */\n            if (sdslen(l) >= 12 && !memcmp(l,\"master_host:\",12)) {\n                if (ri->slave_master_host == NULL ||\n                    strcasecmp(l+12,ri->slave_master_host))\n                {\n                    sdsfree(ri->slave_master_host);\n                    ri->slave_master_host = sdsnew(l+12);\n                    ri->slave_conf_change_time = mstime();\n                }\n            }\n\n            /* master_port:<port> */\n            if (sdslen(l) >= 12 && !memcmp(l,\"master_port:\",12)) {\n                int slave_master_port = atoi(l+12);\n\n                if (ri->slave_master_port != slave_master_port) {\n                    ri->slave_master_port = slave_master_port;\n                    ri->slave_conf_change_time = mstime();\n                }\n            }\n\n            /* master_link_status:<status> */\n            if (sdslen(l) >= 19 && !memcmp(l,\"master_link_status:\",19)) {\n                ri->slave_master_link_status =\n                    (strcasecmp(l+19,\"up\") == 0) ?\n                    SENTINEL_MASTER_LINK_STATUS_UP :\n                    SENTINEL_MASTER_LINK_STATUS_DOWN;\n            }\n\n            /* slave_priority:<priority> */\n            if (sdslen(l) >= 15 && !memcmp(l,\"slave_priority:\",15))\n                ri->slave_priority = atoi(l+15);\n\n            /* slave_repl_offset:<offset> */\n            if (sdslen(l) >= 18 && !memcmp(l,\"slave_repl_offset:\",18))\n                ri->slave_repl_offset = strtoull(l+18,NULL,10);\n\n            /* replica_announced:<announcement> */\n            if (sdslen(l) >= 18 && !memcmp(l,\"replica_announced:\",18))\n                ri->replica_announced = atoi(l+18);\n        }\n    }\n    ri->info_refresh = mstime();\n    sdsfreesplitres(lines,numlines);\n\n    /* ---------------------------- Acting half -----------------------------\n     * Some things will not happen if sentinel.tilt is true, but some will\n     * still be processed. */\n\n    /* Remember when the role changed. */\n    if (role != ri->role_reported) {\n        ri->role_reported_time = mstime();\n        ri->role_reported = role;\n        if (role == SRI_SLAVE) ri->slave_conf_change_time = mstime();\n        /* Log the event with +role-change if the new role is coherent or\n         * with -role-change if there is a mismatch with the current config. */\n        sentinelEvent(LL_VERBOSE,\n            ((ri->flags & (SRI_MASTER|SRI_SLAVE)) == role) ?\n            \"+role-change\" : \"-role-change\",\n            ri, \"%@ new reported role is %s\",\n            role == SRI_MASTER ? \"master\" : \"slave\");\n    }\n\n    /* None of the following conditions are processed when in tilt mode, so\n     * return asap. */\n    if (sentinel.tilt) return;\n\n    /* Handle master -> slave role switch. */\n    if ((ri->flags & SRI_MASTER) && role == SRI_SLAVE) {\n        /* Nothing to do, but masters claiming to be slaves are\n         * considered to be unreachable by Sentinel, so eventually\n         * a failover will be triggered. */\n    }\n\n    /* Handle slave -> master role switch. */\n    if ((ri->flags & SRI_SLAVE) && role == SRI_MASTER) {\n        /* If this is a promoted slave we can change state to the\n         * failover state machine. */\n        if ((ri->flags & SRI_PROMOTED) &&\n            (ri->master->flags & SRI_FAILOVER_IN_PROGRESS) &&\n            (ri->master->failover_state ==\n                SENTINEL_FAILOVER_STATE_WAIT_PROMOTION))\n        {\n            /* Now that we are sure the slave was reconfigured as a master\n             * set the master configuration epoch to the epoch we won the\n             * election to perform this failover. This will force the other\n             * Sentinels to update their config (assuming there is not\n             * a newer one already available). */\n            ri->master->config_epoch = ri->master->failover_epoch;\n            ri->master->failover_state = SENTINEL_FAILOVER_STATE_RECONF_SLAVES;\n            ri->master->failover_state_change_time = mstime();\n            sentinelFlushConfig();\n            sentinelEvent(LL_WARNING,\"+promoted-slave\",ri,\"%@\");\n            if (sentinel.simfailure_flags &\n                SENTINEL_SIMFAILURE_CRASH_AFTER_PROMOTION)\n                sentinelSimFailureCrash();\n            sentinelEvent(LL_WARNING,\"+failover-state-reconf-slaves\",\n                ri->master,\"%@\");\n            sentinelCallClientReconfScript(ri->master,SENTINEL_LEADER,\n                \"start\",ri->master->addr,ri->addr);\n            sentinelForceHelloUpdateForMaster(ri->master);\n        } else {\n            /* A slave turned into a master. We want to force our view and\n             * reconfigure as slave. Wait some time after the change before\n             * going forward, to receive new configs if any. */\n            mstime_t wait_time = SENTINEL_PUBLISH_PERIOD*4;\n\n            if (!(ri->flags & SRI_PROMOTED) &&\n                 sentinelMasterLooksSane(ri->master) &&\n                 sentinelRedisInstanceNoDownFor(ri,wait_time) &&\n                 mstime() - ri->role_reported_time > wait_time)\n            {\n                int retval = sentinelSendSlaveOf(ri,ri->master->addr);\n                if (retval == C_OK)\n                    sentinelEvent(LL_NOTICE,\"+convert-to-slave\",ri,\"%@\");\n            }\n        }\n    }\n\n    /* Handle slaves replicating to a different master address. */\n    if ((ri->flags & SRI_SLAVE) &&\n        role == SRI_SLAVE &&\n        (ri->slave_master_port != ri->master->addr->port ||\n         !sentinelAddrEqualsHostname(ri->master->addr, ri->slave_master_host)))\n    {\n        mstime_t wait_time = ri->master->failover_timeout;\n\n        /* Make sure the master is sane before reconfiguring this instance\n         * into a slave. */\n        if (sentinelMasterLooksSane(ri->master) &&\n            sentinelRedisInstanceNoDownFor(ri,wait_time) &&\n            mstime() - ri->slave_conf_change_time > wait_time)\n        {\n            int retval = sentinelSendSlaveOf(ri,ri->master->addr);\n            if (retval == C_OK)\n                sentinelEvent(LL_NOTICE,\"+fix-slave-config\",ri,\"%@\");\n        }\n    }\n\n    /* Detect if the slave that is in the process of being reconfigured\n     * changed state. */\n    if ((ri->flags & SRI_SLAVE) && role == SRI_SLAVE &&\n        (ri->flags & (SRI_RECONF_SENT|SRI_RECONF_INPROG)))\n    {\n        /* SRI_RECONF_SENT -> SRI_RECONF_INPROG. */\n        if ((ri->flags & SRI_RECONF_SENT) &&\n            ri->slave_master_host &&\n            sentinelAddrEqualsHostname(ri->master->promoted_slave->addr,\n                ri->slave_master_host) &&\n            ri->slave_master_port == ri->master->promoted_slave->addr->port)\n        {\n            ri->flags &= ~SRI_RECONF_SENT;\n            ri->flags |= SRI_RECONF_INPROG;\n            sentinelEvent(LL_NOTICE,\"+slave-reconf-inprog\",ri,\"%@\");\n        }\n\n        /* SRI_RECONF_INPROG -> SRI_RECONF_DONE */\n        if ((ri->flags & SRI_RECONF_INPROG) &&\n            ri->slave_master_link_status == SENTINEL_MASTER_LINK_STATUS_UP)\n        {\n            ri->flags &= ~SRI_RECONF_INPROG;\n            ri->flags |= SRI_RECONF_DONE;\n            sentinelEvent(LL_NOTICE,\"+slave-reconf-done\",ri,\"%@\");\n        }\n    }\n}\n\nvoid sentinelInfoReplyCallback(redisAsyncContext *c, void *reply, void *privdata) {\n    sentinelRedisInstance *ri = (sentinelRedisInstance*)privdata;\n    instanceLink *link = (instanceLink*)c->data;\n    redisReply *r;\n\n    if (!reply || !link) return;\n    link->pending_commands--;\n    r = (redisReply*)reply;\n\n    if (r->type == REDIS_REPLY_STRING)\n        sentinelRefreshInstanceInfo(ri,r->str);\n}\n\n/* Just discard the reply. We use this when we are not monitoring the return\n * value of the command but its effects directly. */\nvoid sentinelDiscardReplyCallback(redisAsyncContext *c, void *reply, void *privdata) {\n    instanceLink *link = (instanceLink*)c->data;\n    UNUSED(reply);\n    UNUSED(privdata);\n\n    if (link) link->pending_commands--;\n}\n\nvoid sentinelPingReplyCallback(redisAsyncContext *c, void *reply, void *privdata) {\n    sentinelRedisInstance *ri = (sentinelRedisInstance*)privdata;\n    instanceLink *link = (instanceLink*)c->data;\n    redisReply *r;\n\n    if (!reply || !link) return;\n    link->pending_commands--;\n    r = (redisReply*)reply;\n\n    if (r->type == REDIS_REPLY_STATUS ||\n        r->type == REDIS_REPLY_ERROR) {\n        /* Update the \"instance available\" field only if this is an\n         * acceptable reply. */\n        if (strncmp(r->str,\"PONG\",4) == 0 ||\n            strncmp(r->str,\"LOADING\",7) == 0 ||\n            strncmp(r->str,\"MASTERDOWN\",10) == 0)\n        {\n            link->last_avail_time = mstime();\n            link->act_ping_time = 0; /* Flag the pong as received. */\n        } else {\n            /* Send a SCRIPT KILL command if the instance appears to be\n             * down because of a busy script. */\n            if (strncmp(r->str,\"BUSY\",4) == 0 &&\n                (ri->flags & SRI_S_DOWN) &&\n                !(ri->flags & SRI_SCRIPT_KILL_SENT))\n            {\n                if (redisAsyncCommand(ri->link->cc,\n                        sentinelDiscardReplyCallback, ri,\n                        \"%s KILL\",\n                        sentinelInstanceMapCommand(ri,\"SCRIPT\")) == C_OK)\n                {\n                    ri->link->pending_commands++;\n                }\n                ri->flags |= SRI_SCRIPT_KILL_SENT;\n            }\n        }\n    }\n    link->last_pong_time = mstime();\n}\n\n/* This is called when we get the reply about the PUBLISH command we send\n * to the master to advertise this sentinel. */\nvoid sentinelPublishReplyCallback(redisAsyncContext *c, void *reply, void *privdata) {\n    sentinelRedisInstance *ri = (sentinelRedisInstance*)privdata;\n    instanceLink *link = (instanceLink*)c->data;\n    redisReply *r;\n\n    if (!reply || !link) return;\n    link->pending_commands--;\n    r = (redisReply*)reply;\n\n    /* Only update pub_time if we actually published our message. Otherwise\n     * we'll retry again in 100 milliseconds. */\n    if (r->type != REDIS_REPLY_ERROR)\n        ri->last_pub_time = mstime();\n}\n\n/* Process a hello message received via Pub/Sub in master or slave instance,\n * or sent directly to this sentinel via the (fake) PUBLISH command of Sentinel.\n *\n * If the master name specified in the message is not known, the message is\n * discarded. */\nvoid sentinelProcessHelloMessage(const char *hello, int hello_len) {\n    /* Format is composed of 8 tokens:\n     * 0=ip,1=port,2=runid,3=current_epoch,4=master_name,\n     * 5=master_ip,6=master_port,7=master_config_epoch. */\n    int numtokens, port, removed, master_port;\n    uint64_t current_epoch, master_config_epoch;\n    char **token = sdssplitlen(hello, hello_len, \",\", 1, &numtokens);\n    sentinelRedisInstance *si, *master;\n\n    if (numtokens == 8) {\n        /* Obtain a reference to the master this hello message is about */\n        master = sentinelGetMasterByName(token[4]);\n        if (!master) goto cleanup; /* Unknown master, skip the message. */\n\n        /* First, try to see if we already have this sentinel. */\n        port = atoi(token[1]);\n        master_port = atoi(token[6]);\n        si = getSentinelRedisInstanceByAddrAndRunID(\n                        master->sentinels,token[0],port,token[2]);\n        current_epoch = strtoull(token[3],NULL,10);\n        master_config_epoch = strtoull(token[7],NULL,10);\n\n        if (!si) {\n            /* If not, remove all the sentinels that have the same runid\n             * because there was an address change, and add the same Sentinel\n             * with the new address back. */\n            removed = removeMatchingSentinelFromMaster(master,token[2]);\n            if (removed) {\n                sentinelEvent(LL_NOTICE,\"+sentinel-address-switch\",master,\n                    \"%@ ip %s port %d for %s\", token[0],port,token[2]);\n            } else {\n                /* Check if there is another Sentinel with the same address this\n                 * new one is reporting. What we do if this happens is to set its\n                 * port to 0, to signal the address is invalid. We'll update it\n                 * later if we get an HELLO message. */\n                sentinelRedisInstance *other =\n                    getSentinelRedisInstanceByAddrAndRunID(\n                        master->sentinels, token[0],port,NULL);\n                if (other) {\n                    sentinelEvent(LL_NOTICE,\"+sentinel-invalid-addr\",other,\"%@\");\n                    other->addr->port = 0; /* It means: invalid address. */\n                    sentinelUpdateSentinelAddressInAllMasters(other);\n                }\n            }\n\n            /* Add the new sentinel. */\n            si = createSentinelRedisInstance(token[2],SRI_SENTINEL,\n                            token[0],port,master->quorum,master);\n\n            if (si) {\n                if (!removed) sentinelEvent(LL_NOTICE,\"+sentinel\",si,\"%@\");\n                /* The runid is NULL after a new instance creation and\n                 * for Sentinels we don't have a later chance to fill it,\n                 * so do it now. */\n                si->runid = sdsnew(token[2]);\n                sentinelTryConnectionSharing(si);\n                if (removed) sentinelUpdateSentinelAddressInAllMasters(si);\n                sentinelFlushConfig();\n            }\n        }\n\n        /* Update local current_epoch if received current_epoch is greater.*/\n        if (current_epoch > sentinel.current_epoch) {\n            sentinel.current_epoch = current_epoch;\n            sentinelFlushConfig();\n            sentinelEvent(LL_WARNING,\"+new-epoch\",master,\"%llu\",\n                (unsigned long long) sentinel.current_epoch);\n        }\n\n        /* Update master info if received configuration is newer. */\n        if (si && master->config_epoch < master_config_epoch) {\n            master->config_epoch = master_config_epoch;\n            if (master_port != master->addr->port ||\n                !sentinelAddrEqualsHostname(master->addr, token[5]))\n            {\n                sentinelAddr *old_addr;\n\n                sentinelEvent(LL_WARNING,\"+config-update-from\",si,\"%@\");\n                sentinelEvent(LL_WARNING,\"+switch-master\",\n                    master,\"%s %s %d %s %d\",\n                    master->name,\n                    announceSentinelAddr(master->addr), master->addr->port,\n                    token[5], master_port);\n\n                old_addr = dupSentinelAddr(master->addr);\n                sentinelResetMasterAndChangeAddress(master, token[5], master_port);\n                sentinelCallClientReconfScript(master,\n                    SENTINEL_OBSERVER,\"start\",\n                    old_addr,master->addr);\n                releaseSentinelAddr(old_addr);\n            }\n        }\n\n        /* Update the state of the Sentinel. */\n        if (si) si->last_hello_time = mstime();\n    }\n\ncleanup:\n    sdsfreesplitres(token,numtokens);\n}\n\n\n/* This is our Pub/Sub callback for the Hello channel. It's useful in order\n * to discover other sentinels attached at the same master. */\nvoid sentinelReceiveHelloMessages(redisAsyncContext *c, void *reply, void *privdata) {\n    sentinelRedisInstance *ri = (sentinelRedisInstance*)privdata;\n    redisReply *r;\n    UNUSED(c);\n\n    if (!reply || !ri) return;\n    r = (redisReply*)reply;\n\n    /* Update the last activity in the pubsub channel. Note that since we\n     * receive our messages as well this timestamp can be used to detect\n     * if the link is probably disconnected even if it seems otherwise. */\n    ri->link->pc_last_activity = mstime();\n\n    /* Sanity check in the reply we expect, so that the code that follows\n     * can avoid to check for details. */\n    if (r->type != REDIS_REPLY_ARRAY ||\n        r->elements != 3 ||\n        r->element[0]->type != REDIS_REPLY_STRING ||\n        r->element[1]->type != REDIS_REPLY_STRING ||\n        r->element[2]->type != REDIS_REPLY_STRING ||\n        strcmp(r->element[0]->str,\"message\") != 0) return;\n\n    /* We are not interested in meeting ourselves */\n    if (strstr(r->element[2]->str,sentinel.myid) != NULL) return;\n\n    sentinelProcessHelloMessage(r->element[2]->str, r->element[2]->len);\n}\n\n/* Send a \"Hello\" message via Pub/Sub to the specified 'ri' Redis\n * instance in order to broadcast the current configuration for this\n * master, and to advertise the existence of this Sentinel at the same time.\n *\n * The message has the following format:\n *\n * sentinel_ip,sentinel_port,sentinel_runid,current_epoch,\n * master_name,master_ip,master_port,master_config_epoch.\n *\n * Returns C_OK if the PUBLISH was queued correctly, otherwise\n * C_ERR is returned. */\nint sentinelSendHello(sentinelRedisInstance *ri) {\n    char ip[NET_IP_STR_LEN];\n    char payload[NET_IP_STR_LEN+1024];\n    int retval;\n    char *announce_ip;\n    int announce_port;\n    sentinelRedisInstance *master = (ri->flags & SRI_MASTER) ? ri : ri->master;\n    sentinelAddr *master_addr = sentinelGetCurrentMasterAddress(master);\n\n    if (ri->link->disconnected) return C_ERR;\n\n    /* Use the specified announce address if specified, otherwise try to\n     * obtain our own IP address. */\n    if (sentinel.announce_ip) {\n        announce_ip = sentinel.announce_ip;\n    } else {\n        if (anetFdToString(ri->link->cc->c.fd,ip,sizeof(ip),NULL,FD_TO_SOCK_NAME) == -1)\n            return C_ERR;\n        announce_ip = ip;\n    }\n    if (sentinel.announce_port) announce_port = sentinel.announce_port;\n    else if (g_pserver->tls_replication && g_pserver->tls_port) announce_port = g_pserver->tls_port;\n    else announce_port = g_pserver->port;\n\n    /* Format and send the Hello message. */\n    snprintf(payload,sizeof(payload),\n        \"%s,%d,%s,%llu,\" /* Info about this sentinel. */\n        \"%s,%s,%d,%llu\", /* Info about current master. */\n        announce_ip, announce_port, sentinel.myid,\n        (unsigned long long) sentinel.current_epoch,\n        /* --- */\n        master->name,announceSentinelAddr(master_addr),master_addr->port,\n        (unsigned long long) master->config_epoch);\n    retval = redisAsyncCommand(ri->link->cc,\n        sentinelPublishReplyCallback, ri, \"%s %s %s\",\n        sentinelInstanceMapCommand(ri,\"PUBLISH\"),\n        SENTINEL_HELLO_CHANNEL,payload);\n    if (retval != C_OK) return C_ERR;\n    ri->link->pending_commands++;\n    return C_OK;\n}\n\n/* Reset last_pub_time in all the instances in the specified dictionary\n * in order to force the delivery of a Hello update ASAP. */\nvoid sentinelForceHelloUpdateDictOfRedisInstances(dict *instances) {\n    dictIterator *di;\n    dictEntry *de;\n\n    di = dictGetSafeIterator(instances);\n    while((de = dictNext(di)) != NULL) {\n        sentinelRedisInstance *ri = (sentinelRedisInstance*)dictGetVal(de);\n        if (ri->last_pub_time >= (SENTINEL_PUBLISH_PERIOD+1))\n            ri->last_pub_time -= (SENTINEL_PUBLISH_PERIOD+1);\n    }\n    dictReleaseIterator(di);\n}\n\n/* This function forces the delivery of a \"Hello\" message (see\n * sentinelSendHello() top comment for further information) to all the Redis\n * and Sentinel instances related to the specified 'master'.\n *\n * It is technically not needed since we send an update to every instance\n * with a period of SENTINEL_PUBLISH_PERIOD milliseconds, however when a\n * Sentinel upgrades a configuration it is a good idea to deliver an update\n * to the other Sentinels ASAP. */\nint sentinelForceHelloUpdateForMaster(sentinelRedisInstance *master) {\n    if (!(master->flags & SRI_MASTER)) return C_ERR;\n    if (master->last_pub_time >= (SENTINEL_PUBLISH_PERIOD+1))\n        master->last_pub_time -= (SENTINEL_PUBLISH_PERIOD+1);\n    sentinelForceHelloUpdateDictOfRedisInstances(master->sentinels);\n    sentinelForceHelloUpdateDictOfRedisInstances(master->slaves);\n    return C_OK;\n}\n\n/* Send a PING to the specified instance and refresh the act_ping_time\n * if it is zero (that is, if we received a pong for the previous ping).\n *\n * On error zero is returned, and we can't consider the PING command\n * queued in the connection. */\nint sentinelSendPing(sentinelRedisInstance *ri) {\n    int retval = redisAsyncCommand(ri->link->cc,\n        sentinelPingReplyCallback, ri, \"%s\",\n        sentinelInstanceMapCommand(ri,\"PING\"));\n    if (retval == C_OK) {\n        ri->link->pending_commands++;\n        ri->link->last_ping_time = mstime();\n        /* We update the active ping time only if we received the pong for\n         * the previous ping, otherwise we are technically waiting since the\n         * first ping that did not receive a reply. */\n        if (ri->link->act_ping_time == 0)\n            ri->link->act_ping_time = ri->link->last_ping_time;\n        return 1;\n    } else {\n        return 0;\n    }\n}\n\n/* Send periodic PING, INFO, and PUBLISH to the Hello channel to\n * the specified master or slave instance. */\nvoid sentinelSendPeriodicCommands(sentinelRedisInstance *ri) {\n    mstime_t now = mstime();\n    mstime_t info_period, ping_period;\n    int retval;\n\n    /* Return ASAP if we have already a PING or INFO already pending, or\n     * in the case the instance is not properly connected. */\n    if (ri->link->disconnected) return;\n\n    /* For INFO, PING, PUBLISH that are not critical commands to send we\n     * also have a limit of SENTINEL_MAX_PENDING_COMMANDS. We don't\n     * want to use a lot of memory just because a link is not working\n     * properly (note that anyway there is a redundant protection about this,\n     * that is, the link will be disconnected and reconnected if a long\n     * timeout condition is detected. */\n    if (ri->link->pending_commands >=\n        SENTINEL_MAX_PENDING_COMMANDS * ri->link->refcount) return;\n\n    /* If this is a slave of a master in O_DOWN condition we start sending\n     * it INFO every second, instead of the usual SENTINEL_INFO_PERIOD\n     * period. In this state we want to closely monitor slaves in case they\n     * are turned into masters by another Sentinel, or by the sysadmin.\n     *\n     * Similarly we monitor the INFO output more often if the slave reports\n     * to be disconnected from the master, so that we can have a fresh\n     * disconnection time figure. */\n    if ((ri->flags & SRI_SLAVE) &&\n        ((ri->master->flags & (SRI_O_DOWN|SRI_FAILOVER_IN_PROGRESS)) ||\n         (ri->master_link_down_time != 0)))\n    {\n        info_period = 1000;\n    } else {\n        info_period = SENTINEL_INFO_PERIOD;\n    }\n\n    /* We ping instances every time the last received pong is older than\n     * the configured 'down-after-milliseconds' time, but every second\n     * anyway if 'down-after-milliseconds' is greater than 1 second. */\n    ping_period = ri->down_after_period;\n    if (ping_period > SENTINEL_PING_PERIOD) ping_period = SENTINEL_PING_PERIOD;\n\n    /* Send INFO to masters and slaves, not sentinels. */\n    if ((ri->flags & SRI_SENTINEL) == 0 &&\n        (ri->info_refresh == 0 ||\n        (now - ri->info_refresh) > info_period))\n    {\n        retval = redisAsyncCommand(ri->link->cc,\n            sentinelInfoReplyCallback, ri, \"%s\",\n            sentinelInstanceMapCommand(ri,\"INFO\"));\n        if (retval == C_OK) ri->link->pending_commands++;\n    }\n\n    /* Send PING to all the three kinds of instances. */\n    if ((now - ri->link->last_pong_time) > ping_period &&\n               (now - ri->link->last_ping_time) > ping_period/2) {\n        sentinelSendPing(ri);\n    }\n\n    /* PUBLISH hello messages to all the three kinds of instances. */\n    if ((now - ri->last_pub_time) > SENTINEL_PUBLISH_PERIOD) {\n        sentinelSendHello(ri);\n    }\n}\n\n/* =========================== SENTINEL command ============================= */\n\n/* SENTINEL CONFIG SET <option> */\nvoid sentinelConfigSetCommand(client *c) {\n    robj *o = c->argv[3];\n    robj *val = c->argv[4];\n    long long numval;\n    int drop_conns = 0;\n\n    if (!strcasecmp(szFromObj(o), \"resolve-hostnames\")) {\n        if ((numval = yesnotoi(szFromObj(val))) == -1) goto badfmt;\n        sentinel.resolve_hostnames = numval;\n    } else if (!strcasecmp(szFromObj(o), \"announce-hostnames\")) {\n        if ((numval = yesnotoi(szFromObj(val))) == -1) goto badfmt;\n        sentinel.announce_hostnames = numval;\n    } else if (!strcasecmp(szFromObj(o), \"announce-ip\")) {\n        if (sentinel.announce_ip) sdsfree(sentinel.announce_ip);\n        sentinel.announce_ip = sdsnew(szFromObj(val));\n    } else if (!strcasecmp(szFromObj(o), \"announce-port\")) {\n        if (getLongLongFromObject(val, &numval) == C_ERR ||\n            numval < 0 || numval > 65535)\n            goto badfmt;\n        sentinel.announce_port = numval;\n    } else if (!strcasecmp(szFromObj(o), \"sentinel-user\")) {\n        sdsfree(sentinel.sentinel_auth_user);\n        sentinel.sentinel_auth_user = sdslen(szFromObj(val)) == 0 ?\n            NULL : sdsdup(szFromObj(val));\n        drop_conns = 1;\n    } else if (!strcasecmp(szFromObj(o), \"sentinel-pass\")) {\n        sdsfree(sentinel.sentinel_auth_pass);\n        sentinel.sentinel_auth_pass = sdslen(szFromObj(val)) == 0 ?\n            NULL : sdsdup(szFromObj(val));\n        drop_conns = 1;\n    } else {\n        addReplyErrorFormat(c, \"Invalid argument '%s' to SENTINEL CONFIG SET\",\n                            (char *) szFromObj(o));\n        return;\n    }\n\n    sentinelFlushConfig();\n    addReply(c, shared.ok);\n\n    /* Drop Sentinel connections to initiate a reconnect if needed. */\n    if (drop_conns)\n        sentinelDropConnections();\n\n    return;\n\nbadfmt:\n    addReplyErrorFormat(c, \"Invalid value '%s' to SENTINEL CONFIG SET '%s'\",\n                        (char *) szFromObj(val), (char *) szFromObj(o));\n}\n\n/* SENTINEL CONFIG GET <option> */\nvoid sentinelConfigGetCommand(client *c) {\n    robj *o = c->argv[3];\n    const char *pattern = szFromObj(o);\n    void *replylen = addReplyDeferredLen(c);\n    int matches = 0;\n\n    if (stringmatch(pattern,\"resolve-hostnames\",1)) {\n        addReplyBulkCString(c,\"resolve-hostnames\");\n        addReplyBulkCString(c,sentinel.resolve_hostnames ? \"yes\" : \"no\");\n        matches++;\n    }\n\n    if (stringmatch(pattern, \"announce-hostnames\", 1)) {\n        addReplyBulkCString(c,\"announce-hostnames\");\n        addReplyBulkCString(c,sentinel.announce_hostnames ? \"yes\" : \"no\");\n        matches++;\n    }\n\n    if (stringmatch(pattern, \"announce-ip\", 1)) {\n        addReplyBulkCString(c,\"announce-ip\");\n        addReplyBulkCString(c,sentinel.announce_ip ? sentinel.announce_ip : \"\");\n        matches++;\n    }\n\n    if (stringmatch(pattern, \"announce-port\", 1)) {\n        addReplyBulkCString(c, \"announce-port\");\n        addReplyBulkLongLong(c, sentinel.announce_port);\n        matches++;\n    }\n\n    if (stringmatch(pattern, \"sentinel-user\", 1)) {\n        addReplyBulkCString(c, \"sentinel-user\");\n        addReplyBulkCString(c, sentinel.sentinel_auth_user ? sentinel.sentinel_auth_user : \"\");\n        matches++;\n    }\n\n    if (stringmatch(pattern, \"sentinel-pass\", 1)) {\n        addReplyBulkCString(c, \"sentinel-pass\");\n        addReplyBulkCString(c, sentinel.sentinel_auth_pass ? sentinel.sentinel_auth_pass : \"\");\n        matches++;\n    }\n\n    setDeferredMapLen(c, replylen, matches);\n}\n\nconst char *sentinelFailoverStateStr(int state) {\n    switch(state) {\n    case SENTINEL_FAILOVER_STATE_NONE: return \"none\";\n    case SENTINEL_FAILOVER_STATE_WAIT_START: return \"wait_start\";\n    case SENTINEL_FAILOVER_STATE_SELECT_SLAVE: return \"select_slave\";\n    case SENTINEL_FAILOVER_STATE_SEND_SLAVEOF_NOONE: return \"send_slaveof_noone\";\n    case SENTINEL_FAILOVER_STATE_WAIT_PROMOTION: return \"wait_promotion\";\n    case SENTINEL_FAILOVER_STATE_RECONF_SLAVES: return \"reconf_slaves\";\n    case SENTINEL_FAILOVER_STATE_UPDATE_CONFIG: return \"update_config\";\n    default: return \"unknown\";\n    }\n}\n\n/* Redis instance to Redis protocol representation. */\nvoid addReplySentinelRedisInstance(client *c, sentinelRedisInstance *ri) {\n    char *flags = sdsempty();\n    void *mbl;\n    int fields = 0;\n\n    mbl = addReplyDeferredLen(c);\n\n    addReplyBulkCString(c,\"name\");\n    addReplyBulkCString(c,ri->name);\n    fields++;\n\n    addReplyBulkCString(c,\"ip\");\n    addReplyBulkCString(c,announceSentinelAddr(ri->addr));\n    fields++;\n\n    addReplyBulkCString(c,\"port\");\n    addReplyBulkLongLong(c,ri->addr->port);\n    fields++;\n\n    addReplyBulkCString(c,\"runid\");\n    addReplyBulkCString(c,ri->runid ? ri->runid : \"\");\n    fields++;\n\n    addReplyBulkCString(c,\"flags\");\n    if (ri->flags & SRI_S_DOWN) flags = sdscat(flags,\"s_down,\");\n    if (ri->flags & SRI_O_DOWN) flags = sdscat(flags,\"o_down,\");\n    if (ri->flags & SRI_MASTER) flags = sdscat(flags,\"master,\");\n    if (ri->flags & SRI_SLAVE) flags = sdscat(flags,\"slave,\");\n    if (ri->flags & SRI_SENTINEL) flags = sdscat(flags,\"sentinel,\");\n    if (ri->link->disconnected) flags = sdscat(flags,\"disconnected,\");\n    if (ri->flags & SRI_MASTER_DOWN) flags = sdscat(flags,\"master_down,\");\n    if (ri->flags & SRI_FAILOVER_IN_PROGRESS)\n        flags = sdscat(flags,\"failover_in_progress,\");\n    if (ri->flags & SRI_PROMOTED) flags = sdscat(flags,\"promoted,\");\n    if (ri->flags & SRI_RECONF_SENT) flags = sdscat(flags,\"reconf_sent,\");\n    if (ri->flags & SRI_RECONF_INPROG) flags = sdscat(flags,\"reconf_inprog,\");\n    if (ri->flags & SRI_RECONF_DONE) flags = sdscat(flags,\"reconf_done,\");\n    if (ri->flags & SRI_FORCE_FAILOVER) flags = sdscat(flags,\"force_failover,\");\n    if (ri->flags & SRI_SCRIPT_KILL_SENT) flags = sdscat(flags,\"script_kill_sent,\");\n\n    if (sdslen(flags) != 0) sdsrange(flags,0,-2); /* remove last \",\" */\n    addReplyBulkCString(c,flags);\n    sdsfree(flags);\n    fields++;\n\n    addReplyBulkCString(c,\"link-pending-commands\");\n    addReplyBulkLongLong(c,ri->link->pending_commands);\n    fields++;\n\n    addReplyBulkCString(c,\"link-refcount\");\n    addReplyBulkLongLong(c,ri->link->refcount);\n    fields++;\n\n    if (ri->flags & SRI_FAILOVER_IN_PROGRESS) {\n        addReplyBulkCString(c,\"failover-state\");\n        addReplyBulkCString(c,(char*)sentinelFailoverStateStr(ri->failover_state));\n        fields++;\n    }\n\n    addReplyBulkCString(c,\"last-ping-sent\");\n    addReplyBulkLongLong(c,\n        ri->link->act_ping_time ? (mstime() - ri->link->act_ping_time) : 0);\n    fields++;\n\n    addReplyBulkCString(c,\"last-ok-ping-reply\");\n    addReplyBulkLongLong(c,mstime() - ri->link->last_avail_time);\n    fields++;\n\n    addReplyBulkCString(c,\"last-ping-reply\");\n    addReplyBulkLongLong(c,mstime() - ri->link->last_pong_time);\n    fields++;\n\n    if (ri->flags & SRI_S_DOWN) {\n        addReplyBulkCString(c,\"s-down-time\");\n        addReplyBulkLongLong(c,mstime()-ri->s_down_since_time);\n        fields++;\n    }\n\n    if (ri->flags & SRI_O_DOWN) {\n        addReplyBulkCString(c,\"o-down-time\");\n        addReplyBulkLongLong(c,mstime()-ri->o_down_since_time);\n        fields++;\n    }\n\n    addReplyBulkCString(c,\"down-after-milliseconds\");\n    addReplyBulkLongLong(c,ri->down_after_period);\n    fields++;\n\n    /* Masters and Slaves */\n    if (ri->flags & (SRI_MASTER|SRI_SLAVE)) {\n        addReplyBulkCString(c,\"info-refresh\");\n        addReplyBulkLongLong(c,\n            ri->info_refresh ? (mstime() - ri->info_refresh) : 0);\n        fields++;\n\n        addReplyBulkCString(c,\"role-reported\");\n        addReplyBulkCString(c, (ri->role_reported == SRI_MASTER) ? \"master\" :\n                                                                   \"slave\");\n        fields++;\n\n        addReplyBulkCString(c,\"role-reported-time\");\n        addReplyBulkLongLong(c,mstime() - ri->role_reported_time);\n        fields++;\n    }\n\n    /* Only masters */\n    if (ri->flags & SRI_MASTER) {\n        addReplyBulkCString(c,\"config-epoch\");\n        addReplyBulkLongLong(c,ri->config_epoch);\n        fields++;\n\n        addReplyBulkCString(c,\"num-slaves\");\n        addReplyBulkLongLong(c,dictSize(ri->slaves));\n        fields++;\n\n        addReplyBulkCString(c,\"num-other-sentinels\");\n        addReplyBulkLongLong(c,dictSize(ri->sentinels));\n        fields++;\n\n        addReplyBulkCString(c,\"quorum\");\n        addReplyBulkLongLong(c,ri->quorum);\n        fields++;\n\n        addReplyBulkCString(c,\"failover-timeout\");\n        addReplyBulkLongLong(c,ri->failover_timeout);\n        fields++;\n\n        addReplyBulkCString(c,\"parallel-syncs\");\n        addReplyBulkLongLong(c,ri->parallel_syncs);\n        fields++;\n\n        if (ri->notification_script) {\n            addReplyBulkCString(c,\"notification-script\");\n            addReplyBulkCString(c,ri->notification_script);\n            fields++;\n        }\n\n        if (ri->client_reconfig_script) {\n            addReplyBulkCString(c,\"client-reconfig-script\");\n            addReplyBulkCString(c,ri->client_reconfig_script);\n            fields++;\n        }\n    }\n\n    /* Only slaves */\n    if (ri->flags & SRI_SLAVE) {\n        addReplyBulkCString(c,\"master-link-down-time\");\n        addReplyBulkLongLong(c,ri->master_link_down_time);\n        fields++;\n\n        addReplyBulkCString(c,\"master-link-status\");\n        addReplyBulkCString(c,\n            (ri->slave_master_link_status == SENTINEL_MASTER_LINK_STATUS_UP) ?\n            \"ok\" : \"err\");\n        fields++;\n\n        addReplyBulkCString(c,\"master-host\");\n        addReplyBulkCString(c,\n            ri->slave_master_host ? ri->slave_master_host : \"?\");\n        fields++;\n\n        addReplyBulkCString(c,\"master-port\");\n        addReplyBulkLongLong(c,ri->slave_master_port);\n        fields++;\n\n        addReplyBulkCString(c,\"slave-priority\");\n        addReplyBulkLongLong(c,ri->slave_priority);\n        fields++;\n\n        addReplyBulkCString(c,\"slave-repl-offset\");\n        addReplyBulkLongLong(c,ri->slave_repl_offset);\n        fields++;\n\n        addReplyBulkCString(c,\"replica-announced\");\n        addReplyBulkLongLong(c,ri->replica_announced);\n        fields++;\n    }\n\n    /* Only sentinels */\n    if (ri->flags & SRI_SENTINEL) {\n        addReplyBulkCString(c,\"last-hello-message\");\n        addReplyBulkLongLong(c,mstime() - ri->last_hello_time);\n        fields++;\n\n        addReplyBulkCString(c,\"voted-leader\");\n        addReplyBulkCString(c,ri->leader ? ri->leader : \"?\");\n        fields++;\n\n        addReplyBulkCString(c,\"voted-leader-epoch\");\n        addReplyBulkLongLong(c,ri->leader_epoch);\n        fields++;\n    }\n\n    setDeferredMapLen(c,mbl,fields);\n}\n\n/* Output a number of instances contained inside a dictionary as\n * Redis protocol. */\nvoid addReplyDictOfRedisInstances(client *c, dict *instances) {\n    dictIterator *di;\n    dictEntry *de;\n    long slaves = 0;\n    void *replylen = addReplyDeferredLen(c);\n\n    di = dictGetIterator(instances);\n    while((de = dictNext(di)) != NULL) {\n        sentinelRedisInstance *ri = (sentinelRedisInstance*)dictGetVal(de);\n\n        /* don't announce unannounced replicas */\n        if (ri->flags & SRI_SLAVE && !ri->replica_announced) continue;\n        addReplySentinelRedisInstance(c,ri);\n        slaves++;\n    }\n    dictReleaseIterator(di);\n    setDeferredArrayLen(c, replylen, slaves);\n}\n\n/* Lookup the named master into sentinel.masters.\n * If the master is not found reply to the client with an error and returns\n * NULL. */\nsentinelRedisInstance *sentinelGetMasterByNameOrReplyError(client *c,\n                        robj *name)\n{\n    sentinelRedisInstance *ri;\n\n    ri = (sentinelRedisInstance*)dictFetchValue(sentinel.masters,ptrFromObj(name));\n    if (!ri) {\n        addReplyError(c,\"No such master with that name\");\n        return NULL;\n    }\n    return ri;\n}\n\n#define SENTINEL_ISQR_OK 0\n#define SENTINEL_ISQR_NOQUORUM (1<<0)\n#define SENTINEL_ISQR_NOAUTH (1<<1)\nint sentinelIsQuorumReachable(sentinelRedisInstance *master, int *usableptr) {\n    dictIterator *di;\n    dictEntry *de;\n    int usable = 1; /* Number of usable Sentinels. Init to 1 to count myself. */\n    int result = SENTINEL_ISQR_OK;\n    int voters = dictSize(master->sentinels)+1; /* Known Sentinels + myself. */\n\n    di = dictGetIterator(master->sentinels);\n    while((de = dictNext(di)) != NULL) {\n        sentinelRedisInstance *ri = (sentinelRedisInstance*)dictGetVal(de);\n\n        if (ri->flags & (SRI_S_DOWN|SRI_O_DOWN)) continue;\n        usable++;\n    }\n    dictReleaseIterator(di);\n\n    if (usable < (int)master->quorum) result |= SENTINEL_ISQR_NOQUORUM;\n    if (usable < voters/2+1) result |= SENTINEL_ISQR_NOAUTH;\n    if (usableptr) *usableptr = usable;\n    return result;\n}\n\nvoid sentinelCommand(client *c) {\n    if (c->argc == 2 && !strcasecmp(szFromObj(c->argv[1]),\"help\")) {\n        const char *help[] = {\n\"CKQUORUM <master-name>\",\n\"    Check if the current Sentinel configuration is able to reach the quorum\",\n\"    needed to failover a master and the majority needed to authorize the\",\n\"    failover.\",\n\"CONFIG SET <param> <value>\",\n\"    Set a global Sentinel configuration parameter.\",\n\"CONFIG GET <param>\",\n\"    Get global Sentinel configuration parameter.\",\n\"GET-MASTER-ADDR-BY-NAME <master-name>\",\n\"    Return the ip and port number of the master with that name.\",\n\"FAILOVER <master-name>\",\n\"    Manually failover a master node without asking for agreement from other\",\n\"    Sentinels\",\n\"FLUSHCONFIG\",\n\"    Force Sentinel to rewrite its configuration on disk, including the current\",\n\"    Sentinel state.\",\n\"INFO-CACHE <master-name>\",\n\"    Return last cached INFO output from masters and all its replicas.\",\n\"IS-MASTER-DOWN-BY-ADDR <ip> <port> <current-epoch> <runid>\",\n\"    Check if the master specified by ip:port is down from current Sentinel's\",\n\"    point of view.\",\n\"MASTER <master-name>\",\n\"    Show the state and info of the specified master.\",\n\"MASTERS\",\n\"    Show a list of monitored masters and their state.\",\n\"MONITOR <name> <ip> <port> <quorum>\",\n\"    Start monitoring a new master with the specified name, ip, port and quorum.\",\n\"MYID\",\n\"    Return the ID of the Sentinel instance.\",\n\"PENDING-SCRIPTS\",\n\"    Get pending scripts information.\",\n\"REMOVE <master-name>\",\n\"    Remove master from Sentinel's monitor list.\",\n\"REPLICAS <master-name>\",\n\"    Show a list of replicas for this master and their state.\",\n\"RESET <pattern>\",\n\"    Reset masters for specific master name matching this pattern.\",\n\"SENTINELS <master-name>\",\n\"    Show a list of Sentinel instances for this master and their state.\",\n\"SET <master-name> <option> <value>\",\n\"    Set configuration paramters for certain masters.\",\n\"SIMULATE-FAILURE (CRASH-AFTER-ELECTION|CRASH-AFTER-PROMOTION|HELP)\",\n\"    Simulate a Sentinel crash.\",\nNULL\n        };\n        addReplyHelp(c, help);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"masters\")) {\n        /* SENTINEL MASTERS */\n        if (c->argc != 2) goto numargserr;\n        addReplyDictOfRedisInstances(c,sentinel.masters);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"master\")) {\n        /* SENTINEL MASTER <name> */\n        sentinelRedisInstance *ri;\n\n        if (c->argc != 3) goto numargserr;\n        if ((ri = sentinelGetMasterByNameOrReplyError(c,c->argv[2]))\n            == NULL) return;\n        addReplySentinelRedisInstance(c,ri);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"slaves\") ||\n               !strcasecmp(szFromObj(c->argv[1]),\"replicas\"))\n    {\n        /* SENTINEL REPLICAS <master-name> */\n        sentinelRedisInstance *ri;\n\n        if (c->argc != 3) goto numargserr;\n        if ((ri = sentinelGetMasterByNameOrReplyError(c,c->argv[2])) == NULL)\n            return;\n        addReplyDictOfRedisInstances(c,ri->slaves);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"sentinels\")) {\n        /* SENTINEL SENTINELS <master-name> */\n        sentinelRedisInstance *ri;\n\n        if (c->argc != 3) goto numargserr;\n        if ((ri = sentinelGetMasterByNameOrReplyError(c,c->argv[2])) == NULL)\n            return;\n        addReplyDictOfRedisInstances(c,ri->sentinels);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"myid\") && c->argc == 2) {\n        /* SENTINEL MYID */\n        addReplyBulkCBuffer(c,sentinel.myid,CONFIG_RUN_ID_SIZE);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"is-master-down-by-addr\")) {\n        /* SENTINEL IS-MASTER-DOWN-BY-ADDR <ip> <port> <current-epoch> <runid>\n         *\n         * Arguments:\n         *\n         * ip and port are the ip and port of the master we want to be\n         * checked by Sentinel. Note that the command will not check by\n         * name but just by master, in theory different Sentinels may monitor\n         * different masters with the same name.\n         *\n         * current-epoch is needed in order to understand if we are allowed\n         * to vote for a failover leader or not. Each Sentinel can vote just\n         * one time per epoch.\n         *\n         * runid is \"*\" if we are not seeking for a vote from the Sentinel\n         * in order to elect the failover leader. Otherwise it is set to the\n         * runid we want the Sentinel to vote if it did not already voted.\n         */\n        sentinelRedisInstance *ri;\n        long long req_epoch;\n        uint64_t leader_epoch = 0;\n        char *leader = NULL;\n        long port;\n        int isdown = 0;\n\n        if (c->argc != 6) goto numargserr;\n        if (getLongFromObjectOrReply(c,c->argv[3],&port,NULL) != C_OK ||\n            getLongLongFromObjectOrReply(c,c->argv[4],&req_epoch,NULL)\n                                                              != C_OK)\n            return;\n        ri = getSentinelRedisInstanceByAddrAndRunID(sentinel.masters,\n            szFromObj(c->argv[2]),port,NULL);\n\n        /* It exists? Is actually a master? Is subjectively down? It's down.\n         * Note: if we are in tilt mode we always reply with \"0\". */\n        if (!sentinel.tilt && ri && (ri->flags & SRI_S_DOWN) &&\n                                    (ri->flags & SRI_MASTER))\n            isdown = 1;\n\n        /* Vote for the master (or fetch the previous vote) if the request\n         * includes a runid, otherwise the sender is not seeking for a vote. */\n        if (ri && ri->flags & SRI_MASTER && strcasecmp(szFromObj(c->argv[5]),\"*\")) {\n            leader = sentinelVoteLeader(ri,(uint64_t)req_epoch,\n                                            szFromObj(c->argv[5]),\n                                            &leader_epoch);\n        }\n\n        /* Reply with a three-elements multi-bulk reply:\n         * down state, leader, vote epoch. */\n        addReplyArrayLen(c,3);\n        addReply(c, isdown ? shared.cone : shared.czero);\n        addReplyBulkCString(c, leader ? leader : \"*\");\n        addReplyLongLong(c, (long long)leader_epoch);\n        if (leader) sdsfree(leader);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"reset\")) {\n        /* SENTINEL RESET <pattern> */\n        if (c->argc != 3) goto numargserr;\n        addReplyLongLong(c,sentinelResetMastersByPattern(szFromObj(c->argv[2]),SENTINEL_GENERATE_EVENT));\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"get-master-addr-by-name\")) {\n        /* SENTINEL GET-MASTER-ADDR-BY-NAME <master-name> */\n        sentinelRedisInstance *ri;\n\n        if (c->argc != 3) goto numargserr;\n        ri = sentinelGetMasterByName(szFromObj(c->argv[2]));\n        if (ri == NULL) {\n            addReplyNullArray(c);\n        } else {\n            sentinelAddr *addr = sentinelGetCurrentMasterAddress(ri);\n\n            addReplyArrayLen(c,2);\n            addReplyBulkCString(c,announceSentinelAddr(addr));\n            addReplyBulkLongLong(c,addr->port);\n        }\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"failover\")) {\n        /* SENTINEL FAILOVER <master-name> */\n        sentinelRedisInstance *ri;\n\n        if (c->argc != 3) goto numargserr;\n        if ((ri = sentinelGetMasterByNameOrReplyError(c,c->argv[2])) == NULL)\n            return;\n        if (ri->flags & SRI_FAILOVER_IN_PROGRESS) {\n            addReplySds(c,sdsnew(\"-INPROG Failover already in progress\\r\\n\"));\n            return;\n        }\n        if (sentinelSelectSlave(ri) == NULL) {\n            addReplySds(c,sdsnew(\"-NOGOODSLAVE No suitable replica to promote\\r\\n\"));\n            return;\n        }\n        serverLog(LL_WARNING,\"Executing user requested FAILOVER of '%s'\",\n            ri->name);\n        sentinelStartFailover(ri);\n        ri->flags |= SRI_FORCE_FAILOVER;\n        addReply(c,shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"pending-scripts\")) {\n        /* SENTINEL PENDING-SCRIPTS */\n\n        if (c->argc != 2) goto numargserr;\n        sentinelPendingScriptsCommand(c);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"monitor\")) {\n        /* SENTINEL MONITOR <name> <ip> <port> <quorum> */\n        sentinelRedisInstance *ri;\n        long quorum, port;\n        char ip[NET_IP_STR_LEN];\n\n        if (c->argc != 6) goto numargserr;\n        if (getLongFromObjectOrReply(c,c->argv[5],&quorum,\"Invalid quorum\")\n            != C_OK) return;\n        if (getLongFromObjectOrReply(c,c->argv[4],&port,\"Invalid port\")\n            != C_OK) return;\n\n        if (quorum <= 0) {\n            addReplyError(c, \"Quorum must be 1 or greater.\");\n            return;\n        }\n\n        /* If resolve-hostnames is used, actual DNS resolution may take place.\n         * Otherwise just validate address.\n         */\n        if (anetResolve(NULL,szFromObj(c->argv[3]),ip,sizeof(ip),\n                        sentinel.resolve_hostnames ? ANET_NONE : ANET_IP_ONLY) == ANET_ERR) {\n            addReplyError(c, \"Invalid IP address or hostname specified\");\n            return;\n        }\n\n        /* Parameters are valid. Try to create the master instance. */\n        ri = createSentinelRedisInstance(szFromObj(c->argv[2]),SRI_MASTER,\n                szFromObj(c->argv[3]),port,quorum,NULL);\n        if (ri == NULL) {\n            addReplyError(c,sentinelCheckCreateInstanceErrors(SRI_MASTER));\n        } else {\n            sentinelFlushConfig();\n            sentinelEvent(LL_WARNING,\"+monitor\",ri,\"%@ quorum %d\",ri->quorum);\n            addReply(c,shared.ok);\n        }\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"flushconfig\")) {\n        if (c->argc != 2) goto numargserr;\n        sentinelFlushConfig();\n        addReply(c,shared.ok);\n        return;\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"remove\")) {\n        /* SENTINEL REMOVE <name> */\n        sentinelRedisInstance *ri;\n\n        if (c->argc != 3) goto numargserr;\n        if ((ri = sentinelGetMasterByNameOrReplyError(c,c->argv[2]))\n            == NULL) return;\n        sentinelEvent(LL_WARNING,\"-monitor\",ri,\"%@\");\n        dictDelete(sentinel.masters,ptrFromObj(c->argv[2]));\n        sentinelFlushConfig();\n        addReply(c,shared.ok);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"ckquorum\")) {\n        /* SENTINEL CKQUORUM <name> */\n        sentinelRedisInstance *ri;\n        int usable;\n\n        if (c->argc != 3) goto numargserr;\n        if ((ri = sentinelGetMasterByNameOrReplyError(c,c->argv[2]))\n            == NULL) return;\n        int result = sentinelIsQuorumReachable(ri,&usable);\n        if (result == SENTINEL_ISQR_OK) {\n            addReplySds(c, sdscatfmt(sdsempty(),\n                \"+OK %i usable Sentinels. Quorum and failover authorization \"\n                \"can be reached\\r\\n\",usable));\n        } else {\n            sds e = sdscatfmt(sdsempty(),\n                \"-NOQUORUM %i usable Sentinels. \",usable);\n            if (result & SENTINEL_ISQR_NOQUORUM)\n                e = sdscat(e,\"Not enough available Sentinels to reach the\"\n                             \" specified quorum for this master\");\n            if (result & SENTINEL_ISQR_NOAUTH) {\n                if (result & SENTINEL_ISQR_NOQUORUM) e = sdscat(e,\". \");\n                e = sdscat(e, \"Not enough available Sentinels to reach the\"\n                              \" majority and authorize a failover\");\n            }\n            e = sdscat(e,\"\\r\\n\");\n            addReplySds(c,e);\n        }\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"set\")) {\n        if (c->argc < 3) goto numargserr;\n        sentinelSetCommand(c);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"config\")) {\n        if (c->argc < 3) goto numargserr;\n        if (!strcasecmp(szFromObj(c->argv[2]),\"set\") && c->argc == 5)\n            sentinelConfigSetCommand(c);\n        else if (!strcasecmp(szFromObj(c->argv[2]),\"get\") && c->argc == 4)\n            sentinelConfigGetCommand(c);\n        else\n            addReplyError(c, \"Only SENTINEL CONFIG GET <option> / SET <option> <value> are supported.\");\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"info-cache\")) {\n        /* SENTINEL INFO-CACHE <name> */\n        if (c->argc < 2) goto numargserr;\n        mstime_t now = mstime();\n\n        /* Create an ad-hoc dictionary type so that we can iterate\n         * a dictionary composed of just the master groups the user\n         * requested. */\n        dictType copy_keeper = instancesDictType;\n        copy_keeper.valDestructor = NULL;\n        dict *masters_local = sentinel.masters;\n        if (c->argc > 2) {\n            masters_local = dictCreate(&copy_keeper, NULL);\n\n            for (int i = 2; i < c->argc; i++) {\n                sentinelRedisInstance *ri;\n                ri = sentinelGetMasterByName(szFromObj(c->argv[i]));\n                if (!ri) continue; /* ignore non-existing names */\n                dictAdd(masters_local, ri->name, ri);\n            }\n        }\n\n        /* Reply format:\n         *   1.) master name\n         *   2.) 1.) info from master\n         *       2.) info from replica\n         *       ...\n         *   3.) other master name\n         *   ...\n         */\n        addReplyArrayLen(c,dictSize(masters_local) * 2);\n\n        dictIterator  *di;\n        dictEntry *de;\n        di = dictGetIterator(masters_local);\n        while ((de = dictNext(di)) != NULL) {\n            sentinelRedisInstance *ri = (sentinelRedisInstance*)dictGetVal(de);\n            addReplyBulkCBuffer(c,ri->name,strlen(ri->name));\n            addReplyArrayLen(c,dictSize(ri->slaves) + 1); /* +1 for self */\n            addReplyArrayLen(c,2);\n            addReplyLongLong(c,\n                ri->info_refresh ? (now - ri->info_refresh) : 0);\n            if (ri->info)\n                addReplyBulkCBuffer(c,ri->info,sdslen(ri->info));\n            else\n                addReplyNull(c);\n\n            dictIterator *sdi;\n            dictEntry *sde;\n            sdi = dictGetIterator(ri->slaves);\n            while ((sde = dictNext(sdi)) != NULL) {\n                sentinelRedisInstance *sri = (sentinelRedisInstance*)dictGetVal(sde);\n                addReplyArrayLen(c,2);\n                addReplyLongLong(c,\n                    ri->info_refresh ? (now - sri->info_refresh) : 0);\n                if (sri->info)\n                    addReplyBulkCBuffer(c,sri->info,sdslen(sri->info));\n                else\n                    addReplyNull(c);\n            }\n            dictReleaseIterator(sdi);\n        }\n        dictReleaseIterator(di);\n        if (masters_local != sentinel.masters) dictRelease(masters_local);\n    } else if (!strcasecmp(szFromObj(c->argv[1]),\"simulate-failure\")) {\n        /* SENTINEL SIMULATE-FAILURE <flag> <flag> ... <flag> */\n        int j;\n\n        sentinel.simfailure_flags = SENTINEL_SIMFAILURE_NONE;\n        for (j = 2; j < c->argc; j++) {\n            if (!strcasecmp(szFromObj(c->argv[j]),\"crash-after-election\")) {\n                sentinel.simfailure_flags |=\n                    SENTINEL_SIMFAILURE_CRASH_AFTER_ELECTION;\n                serverLog(LL_WARNING,\"Failure simulation: this Sentinel \"\n                    \"will crash after being successfully elected as failover \"\n                    \"leader\");\n            } else if (!strcasecmp(szFromObj(c->argv[j]),\"crash-after-promotion\")) {\n                sentinel.simfailure_flags |=\n                    SENTINEL_SIMFAILURE_CRASH_AFTER_PROMOTION;\n                serverLog(LL_WARNING,\"Failure simulation: this Sentinel \"\n                    \"will crash after promoting the selected replica to master\");\n            } else if (!strcasecmp(szFromObj(c->argv[j]),\"help\")) {\n                addReplyArrayLen(c,2);\n                addReplyBulkCString(c,\"crash-after-election\");\n                addReplyBulkCString(c,\"crash-after-promotion\");\n            } else {\n                addReplyError(c,\"Unknown failure simulation specified\");\n                return;\n            }\n        }\n        addReply(c,shared.ok);\n    } else {\n        addReplySubcommandSyntaxError(c);\n    }\n    return;\n\nnumargserr:\n    addReplyErrorFormat(c,\"Wrong number of arguments for 'sentinel %s'\",\n                          (char*)ptrFromObj(c->argv[1]));\n}\n\n#define info_section_from_redis(section_name) do { \\\n    if (defsections || allsections || !strcasecmp(section,section_name)) { \\\n        sds redissection; \\\n        if (sections++) info = sdscat(info,\"\\r\\n\"); \\\n        redissection = genRedisInfoString(section_name); \\\n        info = sdscatlen(info,redissection,sdslen(redissection)); \\\n        sdsfree(redissection); \\\n    } \\\n} while(0)\n\n/* SENTINEL INFO [section] */\nvoid sentinelInfoCommand(client *c) {\n    if (c->argc > 2) {\n        addReplyErrorObject(c,shared.syntaxerr);\n        return;\n    }\n\n    int defsections = 0, allsections = 0;\n    char *section = c->argc == 2 ? szFromObj(c->argv[1]) : NULL;\n    if (section) {\n        allsections = !strcasecmp(section,\"all\");\n        defsections = !strcasecmp(section,\"default\");\n    } else {\n        defsections = 1;\n    }\n\n    int sections = 0;\n    sds info = sdsempty();\n\n    info_section_from_redis(\"server\");\n    info_section_from_redis(\"clients\");\n    info_section_from_redis(\"cpu\");\n    info_section_from_redis(\"stats\");\n\n    if (defsections || allsections || !strcasecmp(section,\"sentinel\")) {\n        dictIterator *di;\n        dictEntry *de;\n        int master_id = 0;\n\n        if (sections++) info = sdscat(info,\"\\r\\n\");\n        info = sdscatprintf(info,\n            \"# Sentinel\\r\\n\"\n            \"sentinel_masters:%lu\\r\\n\"\n            \"sentinel_tilt:%d\\r\\n\"\n            \"sentinel_running_scripts:%d\\r\\n\"\n            \"sentinel_scripts_queue_length:%ld\\r\\n\"\n            \"sentinel_simulate_failure_flags:%lu\\r\\n\",\n            dictSize(sentinel.masters),\n            sentinel.tilt,\n            sentinel.running_scripts,\n            listLength(sentinel.scripts_queue),\n            sentinel.simfailure_flags);\n\n        di = dictGetIterator(sentinel.masters);\n        while((de = dictNext(di)) != NULL) {\n            sentinelRedisInstance *ri = (sentinelRedisInstance*)dictGetVal(de);\n            const char *status = \"ok\";\n\n            if (ri->flags & SRI_O_DOWN) status = \"odown\";\n            else if (ri->flags & SRI_S_DOWN) status = \"sdown\";\n            info = sdscatprintf(info,\n                \"master%d:name=%s,status=%s,address=%s:%d,\"\n                \"slaves=%lu,sentinels=%lu\\r\\n\",\n                master_id++, ri->name, status,\n                announceSentinelAddr(ri->addr), ri->addr->port,\n                dictSize(ri->slaves),\n                dictSize(ri->sentinels)+1);\n        }\n        dictReleaseIterator(di);\n    }\n\n    addReplyBulkSds(c, info);\n}\n\n/* Implements Sentinel version of the ROLE command. The output is\n * \"sentinel\" and the list of currently monitored master names. */\nvoid sentinelRoleCommand(client *c) {\n    dictIterator *di;\n    dictEntry *de;\n\n    addReplyArrayLen(c,2);\n    addReplyBulkCBuffer(c,\"sentinel\",8);\n    addReplyArrayLen(c,dictSize(sentinel.masters));\n\n    di = dictGetIterator(sentinel.masters);\n    while((de = dictNext(di)) != NULL) {\n        sentinelRedisInstance *ri = (sentinelRedisInstance*)dictGetVal(de);\n\n        addReplyBulkCString(c,ri->name);\n    }\n    dictReleaseIterator(di);\n}\n\n/* SENTINEL SET <mastername> [<option> <value> ...] */\nvoid sentinelSetCommand(client *c) {\n    sentinelRedisInstance *ri;\n    int j, changes = 0;\n    int badarg = 0; /* Bad argument position for error reporting. */\n    char *option;\n\n    if ((ri = sentinelGetMasterByNameOrReplyError(c,c->argv[2]))\n        == NULL) return;\n\n    /* Process option - value pairs. */\n    for (j = 3; j < c->argc; j++) {\n        int moreargs = (c->argc-1) - j;\n        option = szFromObj(c->argv[j]);\n        long long ll;\n        int old_j = j; /* Used to know what to log as an event. */\n\n        if (!strcasecmp(option,\"down-after-milliseconds\") && moreargs > 0) {\n            /* down-after-millisecodns <milliseconds> */\n            robj *o = c->argv[++j];\n            if (getLongLongFromObject(o,&ll) == C_ERR || ll <= 0) {\n                badarg = j;\n                goto badfmt;\n            }\n            ri->down_after_period = ll;\n            sentinelPropagateDownAfterPeriod(ri);\n            changes++;\n        } else if (!strcasecmp(option,\"failover-timeout\") && moreargs > 0) {\n            /* failover-timeout <milliseconds> */\n            robj *o = c->argv[++j];\n            if (getLongLongFromObject(o,&ll) == C_ERR || ll <= 0) {\n                badarg = j;\n                goto badfmt;\n            }\n            ri->failover_timeout = ll;\n            changes++;\n        } else if (!strcasecmp(option,\"parallel-syncs\") && moreargs > 0) {\n            /* parallel-syncs <milliseconds> */\n            robj *o = c->argv[++j];\n            if (getLongLongFromObject(o,&ll) == C_ERR || ll <= 0) {\n                badarg = j;\n                goto badfmt;\n            }\n            ri->parallel_syncs = ll;\n            changes++;\n        } else if (!strcasecmp(option,\"notification-script\") && moreargs > 0) {\n            /* notification-script <path> */\n            char *value = szFromObj(c->argv[++j]);\n            if (sentinel.deny_scripts_reconfig) {\n                addReplyError(c,\n                    \"Reconfiguration of scripts path is denied for \"\n                    \"security reasons. Check the deny-scripts-reconfig \"\n                    \"configuration directive in your Sentinel configuration\");\n                goto seterr;\n            }\n\n            if (strlen(value) && access(value,X_OK) == -1) {\n                addReplyError(c,\n                    \"Notification script seems non existing or non executable\");\n                goto seterr;\n            }\n            sdsfree(ri->notification_script);\n            ri->notification_script = strlen(value) ? sdsnew(value) : NULL;\n            changes++;\n        } else if (!strcasecmp(option,\"client-reconfig-script\") && moreargs > 0) {\n            /* client-reconfig-script <path> */\n            char *value = szFromObj(c->argv[++j]);\n            if (sentinel.deny_scripts_reconfig) {\n                addReplyError(c,\n                    \"Reconfiguration of scripts path is denied for \"\n                    \"security reasons. Check the deny-scripts-reconfig \"\n                    \"configuration directive in your Sentinel configuration\");\n                goto seterr;\n            }\n\n            if (strlen(value) && access(value,X_OK) == -1) {\n                addReplyError(c,\n                    \"Client reconfiguration script seems non existing or \"\n                    \"non executable\");\n                goto seterr;\n            }\n            sdsfree(ri->client_reconfig_script);\n            ri->client_reconfig_script = strlen(value) ? sdsnew(value) : NULL;\n            changes++;\n        } else if (!strcasecmp(option,\"auth-pass\") && moreargs > 0) {\n            /* auth-pass <password> */\n            char *value = szFromObj(c->argv[++j]);\n            sdsfree(ri->auth_pass);\n            ri->auth_pass = strlen(value) ? sdsnew(value) : NULL;\n            changes++;\n        } else if (!strcasecmp(option,\"auth-user\") && moreargs > 0) {\n            /* auth-user <username> */\n            char *value = szFromObj(c->argv[++j]);\n            sdsfree(ri->auth_user);\n            ri->auth_user = strlen(value) ? sdsnew(value) : NULL;\n            changes++;\n        } else if (!strcasecmp(option,\"quorum\") && moreargs > 0) {\n            /* quorum <count> */\n            robj *o = c->argv[++j];\n            if (getLongLongFromObject(o,&ll) == C_ERR || ll <= 0) {\n                badarg = j;\n                goto badfmt;\n            }\n            ri->quorum = ll;\n            changes++;\n        } else if (!strcasecmp(option,\"rename-command\") && moreargs > 1) {\n            /* rename-command <oldname> <newname> */\n            sds oldname = szFromObj(c->argv[++j]);\n            sds newname = szFromObj(c->argv[++j]);\n\n            if ((sdslen(oldname) == 0) || (sdslen(newname) == 0)) {\n                badarg = sdslen(newname) ? j-1 : j;\n                goto badfmt;\n            }\n\n            /* Remove any older renaming for this command. */\n            dictDelete(ri->renamed_commands,oldname);\n\n            /* If the target name is the same as the source name there\n             * is no need to add an entry mapping to itself. */\n            if (!dictSdsKeyCaseCompare(NULL,oldname,newname)) {\n                oldname = sdsdup(oldname);\n                newname = sdsdup(newname);\n                dictAdd(ri->renamed_commands,oldname,newname);\n            }\n            changes++;\n        } else {\n            addReplyErrorFormat(c,\"Unknown option or number of arguments for \"\n                                  \"SENTINEL SET '%s'\", option);\n            goto seterr;\n        }\n\n        /* Log the event. */\n        int numargs = j-old_j+1;\n        switch(numargs) {\n        case 2:\n            sentinelEvent(LL_WARNING,\"+set\",ri,\"%@ %s %s\",szFromObj(c->argv[old_j]),\n                                                          szFromObj(c->argv[old_j+1]));\n            break;\n        case 3:\n            sentinelEvent(LL_WARNING,\"+set\",ri,\"%@ %s %s %s\",szFromObj(c->argv[old_j]),\n                                                             szFromObj(c->argv[old_j+1]),\n                                                             szFromObj(c->argv[old_j+2]));\n            break;\n        default:\n            sentinelEvent(LL_WARNING,\"+set\",ri,\"%@ %s\",szFromObj(c->argv[old_j]));\n            break;\n        }\n    }\n\n    if (changes) sentinelFlushConfig();\n    addReply(c,shared.ok);\n    return;\n\nbadfmt: /* Bad format errors */\n    addReplyErrorFormat(c,\"Invalid argument '%s' for SENTINEL SET '%s'\",\n        (char*)ptrFromObj(c->argv[badarg]),option);\nseterr:\n    if (changes) sentinelFlushConfig();\n    return;\n}\n\n/* Our fake PUBLISH command: it is actually useful only to receive hello messages\n * from the other sentinel instances, and publishing to a channel other than\n * SENTINEL_HELLO_CHANNEL is forbidden.\n *\n * Because we have a Sentinel PUBLISH, the code to send hello messages is the same\n * for all the three kind of instances: masters, slaves, sentinels. */\nvoid sentinelPublishCommand(client *c) {\n    if (strcmp(szFromObj(c->argv[1]),SENTINEL_HELLO_CHANNEL)) {\n        addReplyError(c, \"Only HELLO messages are accepted by Sentinel instances.\");\n        return;\n    }\n    sentinelProcessHelloMessage(szFromObj(c->argv[2]),sdslen(szFromObj(c->argv[2])));\n    addReplyLongLong(c,1);\n}\n\n/* ===================== SENTINEL availability checks ======================= */\n\n/* Is this instance down from our point of view? */\nvoid sentinelCheckSubjectivelyDown(sentinelRedisInstance *ri) {\n    mstime_t elapsed = 0;\n\n    if (ri->link->act_ping_time)\n        elapsed = mstime() - ri->link->act_ping_time;\n    else if (ri->link->disconnected)\n        elapsed = mstime() - ri->link->last_avail_time;\n\n    /* Check if we are in need for a reconnection of one of the\n     * links, because we are detecting low activity.\n     *\n     * 1) Check if the command link seems connected, was connected not less\n     *    than SENTINEL_MIN_LINK_RECONNECT_PERIOD, but still we have a\n     *    pending ping for more than half the timeout. */\n    if (ri->link->cc &&\n        (mstime() - ri->link->cc_conn_time) >\n        SENTINEL_MIN_LINK_RECONNECT_PERIOD &&\n        ri->link->act_ping_time != 0 && /* There is a pending ping... */\n        /* The pending ping is delayed, and we did not receive\n         * error replies as well. */\n        (mstime() - ri->link->act_ping_time) > (ri->down_after_period/2) &&\n        (mstime() - ri->link->last_pong_time) > (ri->down_after_period/2))\n    {\n        instanceLinkCloseConnection(ri->link,ri->link->cc);\n    }\n\n    /* 2) Check if the pubsub link seems connected, was connected not less\n     *    than SENTINEL_MIN_LINK_RECONNECT_PERIOD, but still we have no\n     *    activity in the Pub/Sub channel for more than\n     *    SENTINEL_PUBLISH_PERIOD * 3.\n     */\n    if (ri->link->pc &&\n        (mstime() - ri->link->pc_conn_time) >\n         SENTINEL_MIN_LINK_RECONNECT_PERIOD &&\n        (mstime() - ri->link->pc_last_activity) > (SENTINEL_PUBLISH_PERIOD*3))\n    {\n        instanceLinkCloseConnection(ri->link,ri->link->pc);\n    }\n\n    /* Update the SDOWN flag. We believe the instance is SDOWN if:\n     *\n     * 1) It is not replying.\n     * 2) We believe it is a master, it reports to be a slave for enough time\n     *    to meet the down_after_period, plus enough time to get two times\n     *    INFO report from the instance. */\n    if (elapsed > ri->down_after_period ||\n        (ri->flags & SRI_MASTER &&\n         ri->role_reported == SRI_SLAVE &&\n         mstime() - ri->role_reported_time >\n          (ri->down_after_period+SENTINEL_INFO_PERIOD*2)))\n    {\n        /* Is subjectively down */\n        if ((ri->flags & SRI_S_DOWN) == 0) {\n            sentinelEvent(LL_WARNING,\"+sdown\",ri,\"%@\");\n            ri->s_down_since_time = mstime();\n            ri->flags |= SRI_S_DOWN;\n        }\n    } else {\n        /* Is subjectively up */\n        if (ri->flags & SRI_S_DOWN) {\n            sentinelEvent(LL_WARNING,\"-sdown\",ri,\"%@\");\n            ri->flags &= ~(SRI_S_DOWN|SRI_SCRIPT_KILL_SENT);\n        }\n    }\n}\n\n/* Is this instance down according to the configured quorum?\n *\n * Note that ODOWN is a weak quorum, it only means that enough Sentinels\n * reported in a given time range that the instance was not reachable.\n * However messages can be delayed so there are no strong guarantees about\n * N instances agreeing at the same time about the down state. */\nvoid sentinelCheckObjectivelyDown(sentinelRedisInstance *master) {\n    dictIterator *di;\n    dictEntry *de;\n    unsigned int quorum = 0, odown = 0;\n\n    if (master->flags & SRI_S_DOWN) {\n        /* Is down for enough sentinels? */\n        quorum = 1; /* the current sentinel. */\n        /* Count all the other sentinels. */\n        di = dictGetIterator(master->sentinels);\n        while((de = dictNext(di)) != NULL) {\n            sentinelRedisInstance *ri = (sentinelRedisInstance*)dictGetVal(de);\n\n            if (ri->flags & SRI_MASTER_DOWN) quorum++;\n        }\n        dictReleaseIterator(di);\n        if (quorum >= master->quorum) odown = 1;\n    }\n\n    /* Set the flag accordingly to the outcome. */\n    if (odown) {\n        if ((master->flags & SRI_O_DOWN) == 0) {\n            sentinelEvent(LL_WARNING,\"+odown\",master,\"%@ #quorum %d/%d\",\n                quorum, master->quorum);\n            master->flags |= SRI_O_DOWN;\n            master->o_down_since_time = mstime();\n        }\n    } else {\n        if (master->flags & SRI_O_DOWN) {\n            sentinelEvent(LL_WARNING,\"-odown\",master,\"%@\");\n            master->flags &= ~SRI_O_DOWN;\n        }\n    }\n}\n\n/* Receive the SENTINEL is-master-down-by-addr reply, see the\n * sentinelAskMasterStateToOtherSentinels() function for more information. */\nvoid sentinelReceiveIsMasterDownReply(redisAsyncContext *c, void *reply, void *privdata) {\n    sentinelRedisInstance *ri = (sentinelRedisInstance*)privdata;\n    instanceLink *link = (instanceLink*)c->data;\n    redisReply *r;\n\n    if (!reply || !link) return;\n    link->pending_commands--;\n    r = (redisReply*)reply;\n\n    /* Ignore every error or unexpected reply.\n     * Note that if the command returns an error for any reason we'll\n     * end clearing the SRI_MASTER_DOWN flag for timeout anyway. */\n    if (r->type == REDIS_REPLY_ARRAY && r->elements == 3 &&\n        r->element[0]->type == REDIS_REPLY_INTEGER &&\n        r->element[1]->type == REDIS_REPLY_STRING &&\n        r->element[2]->type == REDIS_REPLY_INTEGER)\n    {\n        ri->last_master_down_reply_time = mstime();\n        if (r->element[0]->integer == 1) {\n            ri->flags |= SRI_MASTER_DOWN;\n        } else {\n            ri->flags &= ~SRI_MASTER_DOWN;\n        }\n        if (strcmp(r->element[1]->str,\"*\")) {\n            /* If the runid in the reply is not \"*\" the Sentinel actually\n             * replied with a vote. */\n            sdsfree(ri->leader);\n            if ((long long)ri->leader_epoch != r->element[2]->integer)\n                serverLog(LL_WARNING,\n                    \"%s voted for %s %llu\", ri->name,\n                    r->element[1]->str,\n                    (unsigned long long) r->element[2]->integer);\n            ri->leader = sdsnew(r->element[1]->str);\n            ri->leader_epoch = r->element[2]->integer;\n        }\n    }\n}\n\n/* If we think the master is down, we start sending\n * SENTINEL IS-MASTER-DOWN-BY-ADDR requests to other sentinels\n * in order to get the replies that allow to reach the quorum\n * needed to mark the master in ODOWN state and trigger a failover. */\n#define SENTINEL_ASK_FORCED (1<<0)\nvoid sentinelAskMasterStateToOtherSentinels(sentinelRedisInstance *master, int flags) {\n    dictIterator *di;\n    dictEntry *de;\n\n    di = dictGetIterator(master->sentinels);\n    while((de = dictNext(di)) != NULL) {\n        sentinelRedisInstance *ri = (sentinelRedisInstance*)dictGetVal(de);\n        mstime_t elapsed = mstime() - ri->last_master_down_reply_time;\n        char port[32];\n        int retval;\n\n        /* If the master state from other sentinel is too old, we clear it. */\n        if (elapsed > SENTINEL_ASK_PERIOD*5) {\n            ri->flags &= ~SRI_MASTER_DOWN;\n            sdsfree(ri->leader);\n            ri->leader = NULL;\n        }\n\n        /* Only ask if master is down to other sentinels if:\n         *\n         * 1) We believe it is down, or there is a failover in progress.\n         * 2) Sentinel is connected.\n         * 3) We did not receive the info within SENTINEL_ASK_PERIOD ms. */\n        if ((master->flags & SRI_S_DOWN) == 0) continue;\n        if (ri->link->disconnected) continue;\n        if (!(flags & SENTINEL_ASK_FORCED) &&\n            mstime() - ri->last_master_down_reply_time < SENTINEL_ASK_PERIOD)\n            continue;\n\n        /* Ask */\n        ll2string(port,sizeof(port),master->addr->port);\n        retval = redisAsyncCommand(ri->link->cc,\n                    sentinelReceiveIsMasterDownReply, ri,\n                    \"%s is-master-down-by-addr %s %s %llu %s\",\n                    sentinelInstanceMapCommand(ri,\"SENTINEL\"),\n                    announceSentinelAddr(master->addr), port,\n                    sentinel.current_epoch,\n                    (master->failover_state > SENTINEL_FAILOVER_STATE_NONE) ?\n                    sentinel.myid : \"*\");\n        if (retval == C_OK) ri->link->pending_commands++;\n    }\n    dictReleaseIterator(di);\n}\n\n/* =============================== FAILOVER ================================= */\n\n/* Crash because of user request via SENTINEL simulate-failure command. */\nvoid sentinelSimFailureCrash(void) {\n    serverLog(LL_WARNING,\n        \"Sentinel CRASH because of SENTINEL simulate-failure\");\n    exit(99);\n}\n\n/* Vote for the sentinel with 'req_runid' or return the old vote if already\n * voted for the specified 'req_epoch' or one greater.\n *\n * If a vote is not available returns NULL, otherwise return the Sentinel\n * runid and populate the leader_epoch with the epoch of the vote. */\nchar *sentinelVoteLeader(sentinelRedisInstance *master, uint64_t req_epoch, char *req_runid, uint64_t *leader_epoch) {\n    if (req_epoch > sentinel.current_epoch) {\n        sentinel.current_epoch = req_epoch;\n        sentinelFlushConfig();\n        sentinelEvent(LL_WARNING,\"+new-epoch\",master,\"%llu\",\n            (unsigned long long) sentinel.current_epoch);\n    }\n\n    if (master->leader_epoch < req_epoch && sentinel.current_epoch <= req_epoch)\n    {\n        sdsfree(master->leader);\n        master->leader = sdsnew(req_runid);\n        master->leader_epoch = sentinel.current_epoch;\n        sentinelFlushConfig();\n        sentinelEvent(LL_WARNING,\"+vote-for-leader\",master,\"%s %llu\",\n            master->leader, (unsigned long long) master->leader_epoch);\n        /* If we did not voted for ourselves, set the master failover start\n         * time to now, in order to force a delay before we can start a\n         * failover for the same master. */\n        if (strcasecmp(master->leader,sentinel.myid))\n            master->failover_start_time = mstime()+rand()%SENTINEL_MAX_DESYNC;\n    }\n\n    *leader_epoch = master->leader_epoch;\n    return master->leader ? sdsnew(master->leader) : NULL;\n}\n\nstruct sentinelLeader {\n    char *runid;\n    unsigned long votes;\n};\n\n/* Helper function for sentinelGetLeader, increment the counter\n * relative to the specified runid. */\nint sentinelLeaderIncr(dict *counters, char *runid) {\n    dictEntry *existing, *de;\n    uint64_t oldval;\n\n    de = dictAddRaw(counters,runid,&existing);\n    if (existing) {\n        oldval = dictGetUnsignedIntegerVal(existing);\n        dictSetUnsignedIntegerVal(existing,oldval+1);\n        return oldval+1;\n    } else {\n        serverAssert(de != NULL);\n        dictSetUnsignedIntegerVal(de,1);\n        return 1;\n    }\n}\n\n/* Scan all the Sentinels attached to this master to check if there\n * is a leader for the specified epoch.\n *\n * To be a leader for a given epoch, we should have the majority of\n * the Sentinels we know (ever seen since the last SENTINEL RESET) that\n * reported the same instance as leader for the same epoch. */\nchar *sentinelGetLeader(sentinelRedisInstance *master, uint64_t epoch) {\n    dict *counters;\n    dictIterator *di;\n    dictEntry *de;\n    unsigned int voters = 0, voters_quorum;\n    char *myvote;\n    char *winner = NULL;\n    uint64_t leader_epoch;\n    uint64_t max_votes = 0;\n\n    serverAssert(master->flags & (SRI_O_DOWN|SRI_FAILOVER_IN_PROGRESS));\n    counters = dictCreate(&leaderVotesDictType,NULL);\n\n    voters = dictSize(master->sentinels)+1; /* All the other sentinels and me.*/\n\n    /* Count other sentinels votes */\n    di = dictGetIterator(master->sentinels);\n    while((de = dictNext(di)) != NULL) {\n        sentinelRedisInstance *ri = (sentinelRedisInstance*)dictGetVal(de);\n        if (ri->leader != NULL && ri->leader_epoch == sentinel.current_epoch)\n            sentinelLeaderIncr(counters,ri->leader);\n    }\n    dictReleaseIterator(di);\n\n    /* Check what's the winner. For the winner to win, it needs two conditions:\n     * 1) Absolute majority between voters (50% + 1).\n     * 2) And anyway at least master->quorum votes. */\n    di = dictGetIterator(counters);\n    while((de = dictNext(di)) != NULL) {\n        uint64_t votes = dictGetUnsignedIntegerVal(de);\n\n        if (votes > max_votes) {\n            max_votes = votes;\n            winner = (char*)dictGetKey(de);\n        }\n    }\n    dictReleaseIterator(di);\n\n    /* Count this Sentinel vote:\n     * if this Sentinel did not voted yet, either vote for the most\n     * common voted sentinel, or for itself if no vote exists at all. */\n    if (winner)\n        myvote = sentinelVoteLeader(master,epoch,winner,&leader_epoch);\n    else\n        myvote = sentinelVoteLeader(master,epoch,sentinel.myid,&leader_epoch);\n\n    if (myvote && leader_epoch == epoch) {\n        uint64_t votes = sentinelLeaderIncr(counters,myvote);\n\n        if (votes > max_votes) {\n            max_votes = votes;\n            winner = myvote;\n        }\n    }\n\n    voters_quorum = voters/2+1;\n    if (winner && (max_votes < voters_quorum || max_votes < master->quorum))\n        winner = NULL;\n\n    winner = winner ? sdsnew(winner) : NULL;\n    sdsfree(myvote);\n    dictRelease(counters);\n    return winner;\n}\n\n/* Send SLAVEOF to the specified instance, always followed by a\n * CONFIG REWRITE command in order to store the new configuration on disk\n * when possible (that is, if the Redis instance is recent enough to support\n * config rewriting, and if the server was started with a configuration file).\n *\n * If Host is NULL the function sends \"SLAVEOF NO ONE\".\n *\n * The command returns C_OK if the SLAVEOF command was accepted for\n * (later) delivery otherwise C_ERR. The command replies are just\n * discarded. */\nint sentinelSendSlaveOf(sentinelRedisInstance *ri, const sentinelAddr *addr) {\n    char portstr[32];\n    const char *host;\n    int retval;\n\n    /* If host is NULL we send SLAVEOF NO ONE that will turn the instance\n    * into a master. */\n    if (!addr) {\n        host = \"NO\";\n        memcpy(portstr,\"ONE\",4);\n    } else {\n        host = announceSentinelAddr(addr);\n        ll2string(portstr,sizeof(portstr),addr->port);\n    }\n\n    /* In order to send SLAVEOF in a safe way, we send a transaction performing\n     * the following tasks:\n     * 1) Reconfigure the instance according to the specified host/port params.\n     * 2) Rewrite the configuration.\n     * 3) Disconnect all clients (but this one sending the command) in order\n     *    to trigger the ask-master-on-reconnection protocol for connected\n     *    clients.\n     *\n     * Note that we don't check the replies returned by commands, since we\n     * will observe instead the effects in the next INFO output. */\n    retval = redisAsyncCommand(ri->link->cc,\n        sentinelDiscardReplyCallback, ri, \"%s\",\n        sentinelInstanceMapCommand(ri,\"MULTI\"));\n    if (retval == C_ERR) return retval;\n    ri->link->pending_commands++;\n\n    retval = redisAsyncCommand(ri->link->cc,\n        sentinelDiscardReplyCallback, ri, \"%s %s %s\",\n        sentinelInstanceMapCommand(ri,\"SLAVEOF\"),\n        host, portstr);\n    if (retval == C_ERR) return retval;\n    ri->link->pending_commands++;\n\n    retval = redisAsyncCommand(ri->link->cc,\n        sentinelDiscardReplyCallback, ri, \"%s REWRITE\",\n        sentinelInstanceMapCommand(ri,\"CONFIG\"));\n    if (retval == C_ERR) return retval;\n    ri->link->pending_commands++;\n\n    /* CLIENT KILL TYPE <type> is only supported starting from Redis 2.8.12,\n     * however sending it to an instance not understanding this command is not\n     * an issue because CLIENT is variadic command, so Redis will not\n     * recognized as a syntax error, and the transaction will not fail (but\n     * only the unsupported command will fail). */\n    for (int type = 0; type < 2; type++) {\n        retval = redisAsyncCommand(ri->link->cc,\n            sentinelDiscardReplyCallback, ri, \"%s KILL TYPE %s\",\n            sentinelInstanceMapCommand(ri,\"CLIENT\"),\n            type == 0 ? \"normal\" : \"pubsub\");\n        if (retval == C_ERR) return retval;\n        ri->link->pending_commands++;\n    }\n\n    retval = redisAsyncCommand(ri->link->cc,\n        sentinelDiscardReplyCallback, ri, \"%s\",\n        sentinelInstanceMapCommand(ri,\"EXEC\"));\n    if (retval == C_ERR) return retval;\n    ri->link->pending_commands++;\n\n    return C_OK;\n}\n\n/* Setup the master state to start a failover. */\nvoid sentinelStartFailover(sentinelRedisInstance *master) {\n    serverAssert(GlobalLocksAcquired());\n    serverAssert(master->flags & SRI_MASTER);\n\n    master->failover_state = SENTINEL_FAILOVER_STATE_WAIT_START;\n    master->flags |= SRI_FAILOVER_IN_PROGRESS;\n    master->failover_epoch = ++sentinel.current_epoch;\n    sentinelEvent(LL_WARNING,\"+new-epoch\",master,\"%llu\",\n        (unsigned long long) sentinel.current_epoch);\n    sentinelEvent(LL_WARNING,\"+try-failover\",master,\"%@\");\n    master->failover_start_time = mstime()+rand()%SENTINEL_MAX_DESYNC;\n    master->failover_state_change_time = mstime();\n}\n\n/* This function checks if there are the conditions to start the failover,\n * that is:\n *\n * 1) Master must be in ODOWN condition.\n * 2) No failover already in progress.\n * 3) No failover already attempted recently.\n *\n * We still don't know if we'll win the election so it is possible that we\n * start the failover but that we'll not be able to act.\n *\n * Return non-zero if a failover was started. */\nint sentinelStartFailoverIfNeeded(sentinelRedisInstance *master) {\n    /* We can't failover if the master is not in O_DOWN state. */\n    if (!(master->flags & SRI_O_DOWN)) return 0;\n\n    /* Failover already in progress? */\n    if (master->flags & SRI_FAILOVER_IN_PROGRESS) return 0;\n\n    /* Last failover attempt started too little time ago? */\n    if (mstime() - master->failover_start_time <\n        master->failover_timeout*2)\n    {\n        if (master->failover_delay_logged != master->failover_start_time) {\n            time_t clock = (master->failover_start_time +\n                            master->failover_timeout*2) / 1000;\n            char ctimebuf[26];\n\n            ctime_r(&clock,ctimebuf);\n            ctimebuf[24] = '\\0'; /* Remove newline. */\n            master->failover_delay_logged = master->failover_start_time;\n            serverLog(LL_WARNING,\n                \"Next failover delay: I will not start a failover before %s\",\n                ctimebuf);\n        }\n        return 0;\n    }\n\n    sentinelStartFailover(master);\n    return 1;\n}\n\n/* Select a suitable slave to promote. The current algorithm only uses\n * the following parameters:\n *\n * 1) None of the following conditions: S_DOWN, O_DOWN, DISCONNECTED.\n * 2) Last time the slave replied to ping no more than 5 times the PING period.\n * 3) info_refresh not older than 3 times the INFO refresh period.\n * 4) master_link_down_time no more than:\n *     (now - master->s_down_since_time) + (master->down_after_period * 10).\n *    Basically since the master is down from our POV, the slave reports\n *    to be disconnected no more than 10 times the configured down-after-period.\n *    This is pretty much black magic but the idea is, the master was not\n *    available so the slave may be lagging, but not over a certain time.\n *    Anyway we'll select the best slave according to replication offset.\n * 5) Slave priority can't be zero, otherwise the slave is discarded.\n *\n * Among all the slaves matching the above conditions we select the slave\n * with, in order of sorting key:\n *\n * - lower slave_priority.\n * - bigger processed replication offset.\n * - lexicographically smaller runid.\n *\n * Basically if runid is the same, the slave that processed more commands\n * from the master is selected.\n *\n * The function returns the pointer to the selected slave, otherwise\n * NULL if no suitable slave was found.\n */\n\n/* Helper for sentinelSelectSlave(). This is used by qsort() in order to\n * sort suitable slaves in a \"better first\" order, to take the first of\n * the list. */\nint compareSlavesForPromotion(const void *a, const void *b) {\n    sentinelRedisInstance **sa = (sentinelRedisInstance **)a,\n                          **sb = (sentinelRedisInstance **)b;\n    char *sa_runid, *sb_runid;\n\n    if ((*sa)->slave_priority != (*sb)->slave_priority)\n        return (*sa)->slave_priority - (*sb)->slave_priority;\n\n    /* If priority is the same, select the slave with greater replication\n     * offset (processed more data from the master). */\n    if ((*sa)->slave_repl_offset > (*sb)->slave_repl_offset) {\n        return -1; /* a < b */\n    } else if ((*sa)->slave_repl_offset < (*sb)->slave_repl_offset) {\n        return 1; /* a > b */\n    }\n\n    /* If the replication offset is the same select the slave with that has\n     * the lexicographically smaller runid. Note that we try to handle runid\n     * == NULL as there are old Redis versions that don't publish runid in\n     * INFO. A NULL runid is considered bigger than any other runid. */\n    sa_runid = (*sa)->runid;\n    sb_runid = (*sb)->runid;\n    if (sa_runid == NULL && sb_runid == NULL) return 0;\n    else if (sa_runid == NULL) return 1;  /* a > b */\n    else if (sb_runid == NULL) return -1; /* a < b */\n    return strcasecmp(sa_runid, sb_runid);\n}\n\nsentinelRedisInstance *sentinelSelectSlave(sentinelRedisInstance *master) {\n    sentinelRedisInstance **instance = (sentinelRedisInstance**)\n        zmalloc(sizeof(instance[0])*dictSize(master->slaves), MALLOC_LOCAL);\n    sentinelRedisInstance *selected = NULL;\n    int instances = 0;\n    dictIterator *di;\n    dictEntry *de;\n    mstime_t max_master_down_time = 0;\n\n    if (master->flags & SRI_S_DOWN)\n        max_master_down_time += mstime() - master->s_down_since_time;\n    max_master_down_time += master->down_after_period * 10;\n\n    di = dictGetIterator(master->slaves);\n    while((de = dictNext(di)) != NULL) {\n        sentinelRedisInstance *slave = (sentinelRedisInstance*)dictGetVal(de);\n        mstime_t info_validity_time;\n\n        if (slave->flags & (SRI_S_DOWN|SRI_O_DOWN)) continue;\n        if (slave->link->disconnected) continue;\n        if (mstime() - slave->link->last_avail_time > SENTINEL_PING_PERIOD*5) continue;\n        if (slave->slave_priority == 0) continue;\n\n        /* If the master is in SDOWN state we get INFO for slaves every second.\n         * Otherwise we get it with the usual period so we need to account for\n         * a larger delay. */\n        if (master->flags & SRI_S_DOWN)\n            info_validity_time = SENTINEL_PING_PERIOD*5;\n        else\n            info_validity_time = SENTINEL_INFO_PERIOD*3;\n        if (mstime() - slave->info_refresh > info_validity_time) continue;\n        if (slave->master_link_down_time > max_master_down_time) continue;\n        instance[instances++] = slave;\n    }\n    dictReleaseIterator(di);\n    if (instances) {\n        qsort(instance,instances,sizeof(sentinelRedisInstance*),\n            compareSlavesForPromotion);\n        selected = instance[0];\n    }\n    zfree(instance);\n    return selected;\n}\n\n/* ---------------- Failover state machine implementation ------------------- */\nvoid sentinelFailoverWaitStart(sentinelRedisInstance *ri) {\n    char *leader;\n    int isleader;\n\n    /* Check if we are the leader for the failover epoch. */\n    leader = sentinelGetLeader(ri, ri->failover_epoch);\n    isleader = leader && strcasecmp(leader,sentinel.myid) == 0;\n    sdsfree(leader);\n\n    /* If I'm not the leader, and it is not a forced failover via\n     * SENTINEL FAILOVER, then I can't continue with the failover. */\n    if (!isleader && !(ri->flags & SRI_FORCE_FAILOVER)) {\n        int election_timeout = SENTINEL_ELECTION_TIMEOUT;\n\n        /* The election timeout is the MIN between SENTINEL_ELECTION_TIMEOUT\n         * and the configured failover timeout. */\n        if (election_timeout > ri->failover_timeout)\n            election_timeout = ri->failover_timeout;\n        /* Abort the failover if I'm not the leader after some time. */\n        if (mstime() - ri->failover_start_time > election_timeout) {\n            sentinelEvent(LL_WARNING,\"-failover-abort-not-elected\",ri,\"%@\");\n            sentinelAbortFailover(ri);\n        }\n        return;\n    }\n    sentinelEvent(LL_WARNING,\"+elected-leader\",ri,\"%@\");\n    if (sentinel.simfailure_flags & SENTINEL_SIMFAILURE_CRASH_AFTER_ELECTION)\n        sentinelSimFailureCrash();\n    ri->failover_state = SENTINEL_FAILOVER_STATE_SELECT_SLAVE;\n    ri->failover_state_change_time = mstime();\n    sentinelEvent(LL_WARNING,\"+failover-state-select-slave\",ri,\"%@\");\n}\n\nvoid sentinelFailoverSelectSlave(sentinelRedisInstance *ri) {\n    serverAssert(GlobalLocksAcquired());\n    sentinelRedisInstance *slave = sentinelSelectSlave(ri);\n\n    /* We don't handle the timeout in this state as the function aborts\n     * the failover or go forward in the next state. */\n    if (slave == NULL) {\n        sentinelEvent(LL_WARNING,\"-failover-abort-no-good-slave\",ri,\"%@\");\n        sentinelAbortFailover(ri);\n    } else {\n        sentinelEvent(LL_WARNING,\"+selected-slave\",slave,\"%@\");\n        slave->flags |= SRI_PROMOTED;\n        ri->promoted_slave = slave;\n        ri->failover_state = SENTINEL_FAILOVER_STATE_SEND_SLAVEOF_NOONE;\n        ri->failover_state_change_time = mstime();\n        sentinelEvent(LL_NOTICE,\"+failover-state-send-slaveof-noone\",\n            slave, \"%@\");\n    }\n}\n\nvoid sentinelFailoverSendSlaveOfNoOne(sentinelRedisInstance *ri) {\n    int retval;\n\n    /* We can't send the command to the promoted slave if it is now\n     * disconnected. Retry again and again with this state until the timeout\n     * is reached, then abort the failover. */\n    if (ri->promoted_slave->link->disconnected) {\n        if (mstime() - ri->failover_state_change_time > ri->failover_timeout) {\n            sentinelEvent(LL_WARNING,\"-failover-abort-slave-timeout\",ri,\"%@\");\n            sentinelAbortFailover(ri);\n        }\n        return;\n    }\n\n    /* Send SLAVEOF NO ONE command to turn the slave into a master.\n     * We actually register a generic callback for this command as we don't\n     * really care about the reply. We check if it worked indirectly observing\n     * if INFO returns a different role (master instead of slave). */\n    retval = sentinelSendSlaveOf(ri->promoted_slave,NULL);\n    if (retval != C_OK) return;\n    sentinelEvent(LL_NOTICE, \"+failover-state-wait-promotion\",\n        ri->promoted_slave,\"%@\");\n    ri->failover_state = SENTINEL_FAILOVER_STATE_WAIT_PROMOTION;\n    ri->failover_state_change_time = mstime();\n}\n\n/* We actually wait for promotion indirectly checking with INFO when the\n * slave turns into a master. */\nvoid sentinelFailoverWaitPromotion(sentinelRedisInstance *ri) {\n    /* Just handle the timeout. Switching to the next state is handled\n     * by the function parsing the INFO command of the promoted slave. */\n    if (mstime() - ri->failover_state_change_time > ri->failover_timeout) {\n        sentinelEvent(LL_WARNING,\"-failover-abort-slave-timeout\",ri,\"%@\");\n        sentinelAbortFailover(ri);\n    }\n}\n\nvoid sentinelFailoverDetectEnd(sentinelRedisInstance *master) {\n    int not_reconfigured = 0, timeout = 0;\n    dictIterator *di;\n    dictEntry *de;\n    mstime_t elapsed = mstime() - master->failover_state_change_time;\n\n    /* We can't consider failover finished if the promoted slave is\n     * not reachable. */\n    if (master->promoted_slave == NULL ||\n        master->promoted_slave->flags & SRI_S_DOWN) return;\n\n    /* The failover terminates once all the reachable slaves are properly\n     * configured. */\n    di = dictGetIterator(master->slaves);\n    while((de = dictNext(di)) != NULL) {\n        sentinelRedisInstance *slave = (sentinelRedisInstance*)dictGetVal(de);\n\n        if (slave->flags & (SRI_PROMOTED|SRI_RECONF_DONE)) continue;\n        if (slave->flags & SRI_S_DOWN) continue;\n        not_reconfigured++;\n    }\n    dictReleaseIterator(di);\n\n    /* Force end of failover on timeout. */\n    if (elapsed > master->failover_timeout) {\n        not_reconfigured = 0;\n        timeout = 1;\n        sentinelEvent(LL_WARNING,\"+failover-end-for-timeout\",master,\"%@\");\n    }\n\n    if (not_reconfigured == 0) {\n        sentinelEvent(LL_WARNING,\"+failover-end\",master,\"%@\");\n        master->failover_state = SENTINEL_FAILOVER_STATE_UPDATE_CONFIG;\n        master->failover_state_change_time = mstime();\n    }\n\n    /* If I'm the leader it is a good idea to send a best effort SLAVEOF\n     * command to all the slaves still not reconfigured to replicate with\n     * the new master. */\n    if (timeout) {\n        dictIterator *di;\n        dictEntry *de;\n\n        di = dictGetIterator(master->slaves);\n        while((de = dictNext(di)) != NULL) {\n            sentinelRedisInstance *slave = (sentinelRedisInstance*)dictGetVal(de);\n            int retval;\n\n            if (slave->flags & (SRI_PROMOTED|SRI_RECONF_DONE|SRI_RECONF_SENT)) continue;\n            if (slave->link->disconnected) continue;\n\n            retval = sentinelSendSlaveOf(slave,master->promoted_slave->addr);\n            if (retval == C_OK) {\n                sentinelEvent(LL_NOTICE,\"+slave-reconf-sent-be\",slave,\"%@\");\n                slave->flags |= SRI_RECONF_SENT;\n            }\n        }\n        dictReleaseIterator(di);\n    }\n}\n\n/* Send SLAVE OF <new master address> to all the remaining slaves that\n * still don't appear to have the configuration updated. */\nvoid sentinelFailoverReconfNextSlave(sentinelRedisInstance *master) {\n    dictIterator *di;\n    dictEntry *de;\n    int in_progress = 0;\n    serverAssert(GlobalLocksAcquired());\n\n    di = dictGetIterator(master->slaves);\n    while((de = dictNext(di)) != NULL) {\n        sentinelRedisInstance *slave = (sentinelRedisInstance*)dictGetVal(de);\n\n        if (slave->flags & (SRI_RECONF_SENT|SRI_RECONF_INPROG))\n            in_progress++;\n    }\n    dictReleaseIterator(di);\n\n    di = dictGetIterator(master->slaves);\n    while(in_progress < master->parallel_syncs &&\n          (de = dictNext(di)) != NULL)\n    {\n        sentinelRedisInstance *slave = (sentinelRedisInstance*)dictGetVal(de);\n        int retval;\n\n        /* Skip the promoted slave, and already configured slaves. */\n        if (slave->flags & (SRI_PROMOTED|SRI_RECONF_DONE)) continue;\n\n        /* If too much time elapsed without the slave moving forward to\n         * the next state, consider it reconfigured even if it is not.\n         * Sentinels will detect the slave as misconfigured and fix its\n         * configuration later. */\n        if ((slave->flags & SRI_RECONF_SENT) &&\n            (mstime() - slave->slave_reconf_sent_time) >\n            SENTINEL_SLAVE_RECONF_TIMEOUT)\n        {\n            sentinelEvent(LL_NOTICE,\"-slave-reconf-sent-timeout\",slave,\"%@\");\n            slave->flags &= ~SRI_RECONF_SENT;\n            slave->flags |= SRI_RECONF_DONE;\n        }\n\n        /* Nothing to do for instances that are disconnected or already\n         * in RECONF_SENT state. */\n        if (slave->flags & (SRI_RECONF_SENT|SRI_RECONF_INPROG)) continue;\n        if (slave->link->disconnected) continue;\n\n        /* Send SLAVEOF <new master>. */\n        retval = sentinelSendSlaveOf(slave,master->promoted_slave->addr);\n        if (retval == C_OK) {\n            slave->flags |= SRI_RECONF_SENT;\n            slave->slave_reconf_sent_time = mstime();\n            sentinelEvent(LL_NOTICE,\"+slave-reconf-sent\",slave,\"%@\");\n            in_progress++;\n        }\n    }\n    dictReleaseIterator(di);\n\n    /* Check if all the slaves are reconfigured and handle timeout. */\n    sentinelFailoverDetectEnd(master);\n}\n\n/* This function is called when the slave is in\n * SENTINEL_FAILOVER_STATE_UPDATE_CONFIG state. In this state we need\n * to remove it from the master table and add the promoted slave instead. */\nvoid sentinelFailoverSwitchToPromotedSlave(sentinelRedisInstance *master) {\n    sentinelRedisInstance *ref = master->promoted_slave ?\n                                 master->promoted_slave : master;\n\n    sentinelEvent(LL_WARNING,\"+switch-master\",master,\"%s %s %d %s %d\",\n        master->name, announceSentinelAddr(master->addr), master->addr->port,\n        announceSentinelAddr(ref->addr), ref->addr->port);\n\n    sentinelResetMasterAndChangeAddress(master,ref->addr->hostname,ref->addr->port);\n}\n\nvoid sentinelFailoverStateMachine(sentinelRedisInstance *ri) {\n    serverAssert(ri->flags & SRI_MASTER);\n\n    if (!(ri->flags & SRI_FAILOVER_IN_PROGRESS)) return;\n\n    switch(ri->failover_state) {\n        case SENTINEL_FAILOVER_STATE_WAIT_START:\n            sentinelFailoverWaitStart(ri);\n            break;\n        case SENTINEL_FAILOVER_STATE_SELECT_SLAVE:\n            sentinelFailoverSelectSlave(ri);\n            break;\n        case SENTINEL_FAILOVER_STATE_SEND_SLAVEOF_NOONE:\n            sentinelFailoverSendSlaveOfNoOne(ri);\n            break;\n        case SENTINEL_FAILOVER_STATE_WAIT_PROMOTION:\n            sentinelFailoverWaitPromotion(ri);\n            break;\n        case SENTINEL_FAILOVER_STATE_RECONF_SLAVES:\n            sentinelFailoverReconfNextSlave(ri);\n            break;\n    }\n}\n\n/* Abort a failover in progress:\n *\n * This function can only be called before the promoted slave acknowledged\n * the slave -> master switch. Otherwise the failover can't be aborted and\n * will reach its end (possibly by timeout). */\nvoid sentinelAbortFailover(sentinelRedisInstance *ri) {\n    serverAssert(ri->flags & SRI_FAILOVER_IN_PROGRESS);\n    serverAssert(ri->failover_state <= SENTINEL_FAILOVER_STATE_WAIT_PROMOTION);\n\n    ri->flags &= ~(SRI_FAILOVER_IN_PROGRESS|SRI_FORCE_FAILOVER);\n    ri->failover_state = SENTINEL_FAILOVER_STATE_NONE;\n    ri->failover_state_change_time = mstime();\n    if (ri->promoted_slave) {\n        ri->promoted_slave->flags &= ~SRI_PROMOTED;\n        ri->promoted_slave = NULL;\n    }\n}\n\n/* ======================== SENTINEL timer handler ==========================\n * This is the \"main\" our Sentinel, being sentinel completely non blocking\n * in design. The function is called every second.\n * -------------------------------------------------------------------------- */\n\n/* Perform scheduled operations for the specified Redis instance. */\nvoid sentinelHandleRedisInstance(sentinelRedisInstance *ri) {\n    /* ========== MONITORING HALF ============ */\n    /* Every kind of instance */\n    sentinelReconnectInstance(ri);\n    sentinelSendPeriodicCommands(ri);\n\n    /* ============== ACTING HALF ============= */\n    /* We don't proceed with the acting half if we are in TILT mode.\n     * TILT happens when we find something odd with the time, like a\n     * sudden change in the clock. */\n    if (sentinel.tilt) {\n        if (mstime()-sentinel.tilt_start_time < SENTINEL_TILT_PERIOD) return;\n        sentinel.tilt = 0;\n        sentinelEvent(LL_WARNING,\"-tilt\",NULL,\"#tilt mode exited\");\n    }\n\n    /* Every kind of instance */\n    sentinelCheckSubjectivelyDown(ri);\n\n    /* Masters and slaves */\n    if (ri->flags & (SRI_MASTER|SRI_SLAVE)) {\n        /* Nothing so far. */\n    }\n\n    /* Only masters */\n    if (ri->flags & SRI_MASTER) {\n        sentinelCheckObjectivelyDown(ri);\n        if (sentinelStartFailoverIfNeeded(ri))\n            sentinelAskMasterStateToOtherSentinels(ri,SENTINEL_ASK_FORCED);\n        sentinelFailoverStateMachine(ri);\n        sentinelAskMasterStateToOtherSentinels(ri,SENTINEL_NO_FLAGS);\n    }\n}\n\n/* Perform scheduled operations for all the instances in the dictionary.\n * Recursively call the function against dictionaries of slaves. */\nvoid sentinelHandleDictOfRedisInstances(dict *instances) {\n    dictIterator *di;\n    dictEntry *de;\n    sentinelRedisInstance *switch_to_promoted = NULL;\n\n    /* There are a number of things we need to perform against every master. */\n    di = dictGetIterator(instances);\n    while((de = dictNext(di)) != NULL) {\n        sentinelRedisInstance *ri = (sentinelRedisInstance*)dictGetVal(de);\n\n        sentinelHandleRedisInstance(ri);\n        if (ri->flags & SRI_MASTER) {\n            sentinelHandleDictOfRedisInstances(ri->slaves);\n            sentinelHandleDictOfRedisInstances(ri->sentinels);\n            if (ri->failover_state == SENTINEL_FAILOVER_STATE_UPDATE_CONFIG) {\n                switch_to_promoted = ri;\n            }\n        }\n    }\n    if (switch_to_promoted)\n        sentinelFailoverSwitchToPromotedSlave(switch_to_promoted);\n    dictReleaseIterator(di);\n}\n\n/* This function checks if we need to enter the TITL mode.\n *\n * The TILT mode is entered if we detect that between two invocations of the\n * timer interrupt, a negative amount of time, or too much time has passed.\n * Note that we expect that more or less just 100 milliseconds will pass\n * if everything is fine. However we'll see a negative number or a\n * difference bigger than SENTINEL_TILT_TRIGGER milliseconds if one of the\n * following conditions happen:\n *\n * 1) The Sentinel process for some time is blocked, for every kind of\n * random reason: the load is huge, the computer was frozen for some time\n * in I/O or alike, the process was stopped by a signal. Everything.\n * 2) The system clock was altered significantly.\n *\n * Under both this conditions we'll see everything as timed out and failing\n * without good reasons. Instead we enter the TILT mode and wait\n * for SENTINEL_TILT_PERIOD to elapse before starting to act again.\n *\n * During TILT time we still collect information, we just do not act. */\nvoid sentinelCheckTiltCondition(void) {\n    mstime_t now = mstime();\n    mstime_t delta = now - sentinel.previous_time;\n\n    if (delta < 0 || delta > SENTINEL_TILT_TRIGGER) {\n        sentinel.tilt = 1;\n        sentinel.tilt_start_time = mstime();\n        sentinelEvent(LL_WARNING,\"+tilt\",NULL,\"#tilt mode entered\");\n    }\n    sentinel.previous_time = mstime();\n}\n\nvoid sentinelTimer(void) {\n    sentinelCheckTiltCondition();\n    sentinelHandleDictOfRedisInstances(sentinel.masters);\n    sentinelRunPendingScripts();\n    sentinelCollectTerminatedScripts();\n    sentinelKillTimedoutScripts();\n\n    /* We continuously change the frequency of the Redis \"timer interrupt\"\n     * in order to desynchronize every Sentinel from every other.\n     * This non-determinism avoids that Sentinels started at the same time\n     * exactly continue to stay synchronized asking to be voted at the\n     * same time again and again (resulting in nobody likely winning the\n     * election because of split brain voting). */\n    g_pserver->hz = CONFIG_DEFAULT_HZ + rand() % CONFIG_DEFAULT_HZ;\n}\n"
  },
  {
    "path": "src/server.cpp",
    "content": "/*\n * Copyright (c) 2009-2016, Salvatore Sanfilippo <antirez at gmail dot com>\n * Copyright (c) 2019 John Sully <john at eqalpha dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n#include \"monotonic.h\"\n#include \"cluster.h\"\n#include \"slowlog.h\"\n#include \"bio.h\"\n#include \"latency.h\"\n#include \"atomicvar.h\"\n#include \"storage.h\"\n#include \"cron.h\"\n#include <thread>\n#include \"mt19937-64.h\"\n\n#include <time.h>\n#include <signal.h>\n#include <sys/wait.h>\n#include <errno.h>\n#include <assert.h>\n#include <ctype.h>\n#include <stdarg.h>\n#include <arpa/inet.h>\n#include <sys/stat.h>\n#include <fcntl.h>\n#include <sys/time.h>\n#include <sys/resource.h>\n#include <sys/uio.h>\n#include <sys/un.h>\n#include <limits.h>\n#include <float.h>\n#include <math.h>\n#include <sys/utsname.h>\n#include <locale.h>\n#include <sys/socket.h>\n#include <algorithm>\n#include <uuid/uuid.h>\n#include <condition_variable>\n#include \"aelocker.h\"\n#include \"motd.h\"\n#include \"t_nhash.h\"\n#include \"readwritelock.h\"\n#ifdef __linux__\n#include <sys/prctl.h>\n#include <sys/mman.h>\n#include <sys/sysinfo.h>\n#endif\n\nint g_fTestMode = false;\nconst char *motd_url = \"http://api.keydb.dev/motd/motd_server.txt\";\nconst char *motd_cache_file = \"/.keydb-server-motd\";\n\n/* Our shared \"common\" objects */\n\nstruct sharedObjectsStruct shared;\n\n/* Global vars that are actually used as constants. The following double\n * values are used for double on-disk serialization, and are initialized\n * at runtime to avoid strange compiler optimizations. */\n\ndouble R_Zero, R_PosInf, R_NegInf, R_Nan;\n\n/*================================= Globals ================================= */\n\n/* Global vars */\nnamespace GlobalHidden {\nstruct redisServer server; /* Server global state */\n}\nredisServer *g_pserver = &GlobalHidden::server;\nstruct redisServerConst cserver;\nthread_local struct redisServerThreadVars *serverTL = NULL;   // thread local server vars\nfastlock time_thread_lock(\"Time thread lock\");\nstd::condition_variable_any time_thread_cv;\nint sleeping_threads = 0;\nvoid wakeTimeThread();\n\n/* Our command table.\n *\n * Every entry is composed of the following fields:\n *\n * name:        A string representing the command name.\n *\n * function:    Pointer to the C function implementing the command.\n *\n * arity:       Number of arguments, it is possible to use -N to say >= N\n *\n * sflags:      Command flags as string. See below for a table of flags.\n *\n * flags:       Flags as bitmask. Computed by Redis using the 'sflags' field.\n *\n * get_keys_proc: An optional function to get key arguments from a command.\n *                This is only used when the following three fields are not\n *                enough to specify what arguments are keys.\n *\n * first_key_index: First argument that is a key\n *\n * last_key_index: Last argument that is a key\n *\n * key_step:    Step to get all the keys from first to last argument.\n *              For instance in MSET the step is two since arguments\n *              are key,val,key,val,...\n *\n * microseconds: Microseconds of total execution time for this command.\n *\n * calls:       Total number of calls of this command.\n *\n * id:          Command bit identifier for ACLs or other goals.\n *\n * The flags, microseconds and calls fields are computed by Redis and should\n * always be set to zero.\n *\n * Command flags are expressed using space separated strings, that are turned\n * into actual flags by the populateCommandTable() function.\n *\n * This is the meaning of the flags:\n *\n * write:       Write command (may modify the key space).\n *\n * read-only:   Commands just reading from keys without changing the content.\n *              Note that commands that don't read from the keyspace such as\n *              TIME, SELECT, INFO, administrative commands, and connection\n *              or transaction related commands (multi, exec, discard, ...)\n *              are not flagged as read-only commands, since they affect the\n *              server or the connection in other ways.\n *\n * use-memory:  May increase memory usage once called. Don't allow if out\n *              of memory.\n *\n * admin:       Administrative command, like SAVE or SHUTDOWN.\n *\n * pub-sub:     Pub/Sub related command.\n *\n * no-script:   Command not allowed in scripts.\n *\n * random:      Random command. Command is not deterministic, that is, the same\n *              command with the same arguments, with the same key space, may\n *              have different results. For instance SPOP and RANDOMKEY are\n *              two random commands.\n *\n * to-sort:     Sort command output array if called from script, so that the\n *              output is deterministic. When this flag is used (not always\n *              possible), then the \"random\" flag is not needed.\n *\n * ok-loading:  Allow the command while loading the database.\n *\n * ok-stale:    Allow the command while a replica has stale data but is not\n *              allowed to serve this data. Normally no command is accepted\n *              in this condition but just a few.\n *\n * no-monitor:  Do not automatically propagate the command on MONITOR.\n *\n * no-slowlog:  Do not automatically propagate the command to the slowlog.\n *\n * cluster-asking: Perform an implicit ASKING for this command, so the\n *              command will be accepted in cluster mode if the slot is marked\n *              as 'importing'.\n *\n * fast:        Fast command: O(1) or O(log(N)) command that should never\n *              delay its execution as long as the kernel scheduler is giving\n *              us time. Note that commands that may trigger a DEL as a side\n *              effect (like SET) are not fast commands.\n * \n * may-replicate: Command may produce replication traffic, but should be \n *                allowed under circumstances where write commands are disallowed. \n *                Examples include PUBLISH, which replicates pubsub messages,and \n *                EVAL, which may execute write commands, which are replicated, \n *                or may just execute read commands. A command can not be marked \n *                both \"write\" and \"may-replicate\"\n *\n * The following additional flags are only used in order to put commands\n * in a specific ACL category. Commands can have multiple ACL categories.\n *\n * @keyspace, @read, @write, @set, @sortedset, @list, @hash, @string, @bitmap,\n * @hyperloglog, @stream, @admin, @fast, @slow, @pubsub, @blocking, @dangerous,\n * @connection, @transaction, @scripting, @geo, @replication.\n *\n * Note that:\n *\n * 1) The read-only flag implies the @read ACL category.\n * 2) The write flag implies the @write ACL category.\n * 3) The fast flag implies the @fast ACL category.\n * 4) The admin flag implies the @admin and @dangerous ACL category.\n * 5) The pub-sub flag implies the @pubsub ACL category.\n * 6) The lack of fast flag implies the @slow ACL category.\n * 7) The non obvious \"keyspace\" category includes the commands\n *    that interact with keys without having anything to do with\n *    specific data structures, such as: DEL, RENAME, MOVE, SELECT,\n *    TYPE, EXPIRE*, PEXPIRE*, TTL, PTTL, ...\n */\n\nstruct redisCommand redisCommandTable[] = {\n    {\"module\",moduleCommand,-2,\n     \"admin no-script\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"get\",getCommand,2,\n     \"read-only fast async @string\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"getex\",getexCommand,-2,\n     \"write fast @string\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"getdel\",getdelCommand,2,\n     \"write fast @string\",\n     0,NULL,1,1,1,0,0,0},\n\n    /* Note that we can't flag set as fast, since it may perform an\n     * implicit DEL of a large key. */\n    {\"set\",setCommand,-3,\n     \"write use-memory @string\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"setnx\",setnxCommand,3,\n     \"write use-memory fast @string\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"setex\",setexCommand,4,\n     \"write use-memory @string\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"psetex\",psetexCommand,4,\n     \"write use-memory @string\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"append\",appendCommand,3,\n     \"write use-memory fast @string\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"strlen\",strlenCommand,2,\n     \"read-only fast @string\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"del\",delCommand,-2,\n     \"write @keyspace\",\n     0,NULL,1,-1,1,0,0,0},\n\n    {\"expdel\",delCommand,-2,\n     \"write @keyspace\",\n     0,NULL,1,-1,1,0,0,0},\n\n    {\"unlink\",unlinkCommand,-2,\n     \"write fast @keyspace\",\n     0,NULL,1,-1,1,0,0,0},\n\n    {\"exists\",existsCommand,-2,\n     \"read-only fast @keyspace\",\n     0,NULL,1,-1,1,0,0,0},\n\n    {\"keydb.mexists\",mexistsCommand,-2,\n     \"read-only fast @keyspace\",\n     0,NULL,1,-1,1,0,0,0},\n\n    {\"setbit\",setbitCommand,4,\n     \"write use-memory @bitmap\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"getbit\",getbitCommand,3,\n     \"read-only fast @bitmap\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"bitfield\",bitfieldCommand,-2,\n     \"write use-memory @bitmap\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"bitfield_ro\",bitfieldroCommand,-2,\n     \"read-only fast @bitmap\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"setrange\",setrangeCommand,4,\n     \"write use-memory @string\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"getrange\",getrangeCommand,4,\n     \"read-only @string\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"substr\",getrangeCommand,4,\n     \"read-only @string\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"incr\",incrCommand,2,\n     \"write use-memory fast @string\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"decr\",decrCommand,2,\n     \"write use-memory fast @string\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"mget\",mgetCommand,-2,\n     \"read-only fast async @string\",\n     0,NULL,1,-1,1,0,0,0},\n\n    {\"rpush\",rpushCommand,-3,\n     \"write use-memory fast @list\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"lpush\",lpushCommand,-3,\n     \"write use-memory fast @list\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"rpushx\",rpushxCommand,-3,\n     \"write use-memory fast @list\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"lpushx\",lpushxCommand,-3,\n     \"write use-memory fast @list\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"linsert\",linsertCommand,5,\n     \"write use-memory @list\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"rpop\",rpopCommand,-2,\n     \"write fast @list\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"lpop\",lpopCommand,-2,\n     \"write fast @list\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"brpop\",brpopCommand,-3,\n     \"write no-script @list @blocking\",\n     0,NULL,1,-2,1,0,0,0},\n\n    {\"brpoplpush\",brpoplpushCommand,4,\n     \"write use-memory no-script @list @blocking\",\n     0,NULL,1,2,1,0,0,0},\n\n    {\"blmove\",blmoveCommand,6,\n     \"write use-memory no-script @list @blocking\",\n     0,NULL,1,2,1,0,0,0},\n\n    {\"blpop\",blpopCommand,-3,\n     \"write no-script @list @blocking\",\n     0,NULL,1,-2,1,0,0,0},\n\n    {\"llen\",llenCommand,2,\n     \"read-only fast @list\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"lindex\",lindexCommand,3,\n     \"read-only @list\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"lset\",lsetCommand,4,\n     \"write use-memory @list\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"lrange\",lrangeCommand,4,\n     \"read-only @list\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"ltrim\",ltrimCommand,4,\n     \"write @list\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"lpos\",lposCommand,-3,\n     \"read-only @list\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"lrem\",lremCommand,4,\n     \"write @list\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"rpoplpush\",rpoplpushCommand,3,\n     \"write use-memory @list\",\n     0,NULL,1,2,1,0,0,0},\n\n    {\"lmove\",lmoveCommand,5,\n     \"write use-memory @list\",\n     0,NULL,1,2,1,0,0,0},\n\n    {\"sadd\",saddCommand,-3,\n     \"write use-memory fast @set\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"srem\",sremCommand,-3,\n     \"write fast @set\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"smove\",smoveCommand,4,\n     \"write fast @set\",\n     0,NULL,1,2,1,0,0,0},\n\n    {\"sismember\",sismemberCommand,3,\n     \"read-only fast @set\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"smismember\",smismemberCommand,-3,\n     \"read-only fast @set\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"scard\",scardCommand,2,\n     \"read-only fast @set\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"spop\",spopCommand,-2,\n     \"write random fast @set\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"srandmember\",srandmemberCommand,-2,\n     \"read-only random @set\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"sinter\",sinterCommand,-2,\n     \"read-only to-sort @set\",\n     0,NULL,1,-1,1,0,0,0},\n\n    {\"sinterstore\",sinterstoreCommand,-3,\n     \"write use-memory @set\",\n     0,NULL,1,-1,1,0,0,0},\n\n    {\"sunion\",sunionCommand,-2,\n     \"read-only to-sort @set\",\n     0,NULL,1,-1,1,0,0,0},\n\n    {\"sunionstore\",sunionstoreCommand,-3,\n     \"write use-memory @set\",\n     0,NULL,1,-1,1,0,0,0},\n\n    {\"sdiff\",sdiffCommand,-2,\n     \"read-only to-sort @set\",\n     0,NULL,1,-1,1,0,0,0},\n\n    {\"sdiffstore\",sdiffstoreCommand,-3,\n     \"write use-memory @set\",\n     0,NULL,1,-1,1,0,0,0},\n\n    {\"smembers\",sinterCommand,2,\n     \"read-only to-sort @set\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"sscan\",sscanCommand,-3,\n     \"read-only random @set\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"zadd\",zaddCommand,-4,\n     \"write use-memory fast @sortedset\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"zincrby\",zincrbyCommand,4,\n     \"write use-memory fast @sortedset\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"zrem\",zremCommand,-3,\n     \"write fast @sortedset\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"zremrangebyscore\",zremrangebyscoreCommand,4,\n     \"write @sortedset\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"zremrangebyrank\",zremrangebyrankCommand,4,\n     \"write @sortedset\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"zremrangebylex\",zremrangebylexCommand,4,\n     \"write @sortedset\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"zunionstore\",zunionstoreCommand,-4,\n     \"write use-memory @sortedset\",\n     0,zunionInterDiffStoreGetKeys,1,1,1,0,0,0},\n\n    {\"zinterstore\",zinterstoreCommand,-4,\n     \"write use-memory @sortedset\",\n     0,zunionInterDiffStoreGetKeys,1,1,1,0,0,0},\n\n    {\"zdiffstore\",zdiffstoreCommand,-4,\n     \"write use-memory @sortedset\",\n     0,zunionInterDiffStoreGetKeys,1,1,1,0,0,0},\n\n    {\"zunion\",zunionCommand,-3,\n     \"read-only @sortedset\",\n     0,zunionInterDiffGetKeys,0,0,0,0,0,0},\n\n    {\"zinter\",zinterCommand,-3,\n     \"read-only @sortedset\",\n     0,zunionInterDiffGetKeys,0,0,0,0,0,0},\n\n    {\"zdiff\",zdiffCommand,-3,\n     \"read-only @sortedset\",\n     0,zunionInterDiffGetKeys,0,0,0,0,0,0},\n\n    {\"zrange\",zrangeCommand,-4,\n     \"read-only @sortedset\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"zrangestore\",zrangestoreCommand,-5,\n     \"write use-memory @sortedset\",\n     0,NULL,1,2,1,0,0,0},\n\n    {\"zrangebyscore\",zrangebyscoreCommand,-4,\n     \"read-only @sortedset\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"zrevrangebyscore\",zrevrangebyscoreCommand,-4,\n     \"read-only @sortedset\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"zrangebylex\",zrangebylexCommand,-4,\n     \"read-only @sortedset\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"zrevrangebylex\",zrevrangebylexCommand,-4,\n     \"read-only @sortedset\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"zcount\",zcountCommand,4,\n     \"read-only fast @sortedset\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"zlexcount\",zlexcountCommand,4,\n     \"read-only fast @sortedset\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"zrevrange\",zrevrangeCommand,-4,\n     \"read-only @sortedset\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"zcard\",zcardCommand,2,\n     \"read-only fast @sortedset\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"zscore\",zscoreCommand,3,\n     \"read-only fast @sortedset\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"zmscore\",zmscoreCommand,-3,\n     \"read-only fast @sortedset\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"zrank\",zrankCommand,3,\n     \"read-only fast @sortedset\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"zrevrank\",zrevrankCommand,3,\n     \"read-only fast @sortedset\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"zscan\",zscanCommand,-3,\n     \"read-only random @sortedset\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"zpopmin\",zpopminCommand,-2,\n     \"write fast @sortedset\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"zpopmax\",zpopmaxCommand,-2,\n     \"write fast @sortedset\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"bzpopmin\",bzpopminCommand,-3,\n     \"write no-script fast @sortedset @blocking\",\n     0,NULL,1,-2,1,0,0,0},\n\n    {\"bzpopmax\",bzpopmaxCommand,-3,\n     \"write no-script fast @sortedset @blocking\",\n     0,NULL,1,-2,1,0,0,0},\n\n    {\"zrandmember\",zrandmemberCommand,-2,\n     \"read-only random @sortedset\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"hset\",hsetCommand,-4,\n     \"write use-memory fast @hash\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"hsetnx\",hsetnxCommand,4,\n     \"write use-memory fast @hash\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"hget\",hgetCommand,3,\n     \"read-only fast @hash\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"hmset\",hsetCommand,-4,\n     \"write use-memory fast @hash\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"hmget\",hmgetCommand,-3,\n     \"read-only fast @hash\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"hincrby\",hincrbyCommand,4,\n     \"write use-memory fast @hash\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"hincrbyfloat\",hincrbyfloatCommand,4,\n     \"write use-memory fast @hash\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"hdel\",hdelCommand,-3,\n     \"write fast @hash\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"hlen\",hlenCommand,2,\n     \"read-only fast @hash\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"hstrlen\",hstrlenCommand,3,\n     \"read-only fast @hash\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"hkeys\",hkeysCommand,2,\n     \"read-only to-sort @hash\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"hvals\",hvalsCommand,2,\n     \"read-only to-sort @hash\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"hgetall\",hgetallCommand,2,\n     \"read-only random @hash\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"hexists\",hexistsCommand,3,\n     \"read-only fast @hash\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"hrandfield\",hrandfieldCommand,-2,\n     \"read-only random @hash\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"hscan\",hscanCommand,-3,\n     \"read-only random @hash\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"incrby\",incrbyCommand,3,\n     \"write use-memory fast @string\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"decrby\",decrbyCommand,3,\n     \"write use-memory fast @string\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"incrbyfloat\",incrbyfloatCommand,3,\n     \"write use-memory fast @string\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"getset\",getsetCommand,3,\n     \"write use-memory fast @string\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"mset\",msetCommand,-3,\n     \"write use-memory @string\",\n     0,NULL,1,-1,2,0,0,0},\n\n    {\"msetnx\",msetnxCommand,-3,\n     \"write use-memory @string\",\n     0,NULL,1,-1,2,0,0,0},\n\n    {\"randomkey\",randomkeyCommand,1,\n     \"read-only random @keyspace\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"select\",selectCommand,2,\n     \"ok-loading fast ok-stale @keyspace\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"swapdb\",swapdbCommand,3,\n     \"write fast @keyspace @dangerous\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"move\",moveCommand,3,\n     \"write fast @keyspace\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"copy\",copyCommand,-3,\n     \"write use-memory @keyspace\",\n     0,NULL,1,2,1,0,0,0},\n\n    /* Like for SET, we can't mark rename as a fast command because\n     * overwriting the target key may result in an implicit slow DEL. */\n    {\"rename\",renameCommand,3,\n     \"write @keyspace\",\n     0,NULL,1,2,1,0,0,0},\n\n    {\"renamenx\",renamenxCommand,3,\n     \"write fast @keyspace\",\n     0,NULL,1,2,1,0,0,0},\n\n    {\"expire\",expireCommand,3,\n     \"write fast @keyspace\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"expireat\",expireatCommand,3,\n     \"write fast @keyspace\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"expiremember\", expireMemberCommand, -4,\n     \"write fast @keyspace\",\n     0,NULL,1,1,1,0,0,0},\n    \n    {\"expirememberat\", expireMemberAtCommand, 4,\n     \"write fast @keyspace\",\n     0,NULL,1,1,1,0,0,0},\n    \n    {\"pexpirememberat\", pexpireMemberAtCommand, 4,\n     \"write fast @keyspace\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"pexpire\",pexpireCommand,3,\n     \"write fast @keyspace\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"pexpireat\",pexpireatCommand,3,\n     \"write fast @keyspace\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"keys\",keysCommand,2,\n     \"read-only to-sort @keyspace @dangerous\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"scan\",scanCommand,-2,\n     \"read-only random @keyspace\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"dbsize\",dbsizeCommand,1,\n     \"read-only fast @keyspace\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"auth\",authCommand,-2,\n     \"no-auth no-script ok-loading ok-stale fast @connection\",\n     0,NULL,0,0,0,0,0,0},\n\n    /* We don't allow PING during loading since in Redis PING is used as\n     * failure detection, and a loading server is considered to be\n     * not available. */\n    {\"ping\",pingCommand,-1,\n     \"ok-stale ok-loading fast @connection @replication\",\n     0,NULL,0,0,0,0,0,0},\n\n     {\"replping\",pingCommand,-1,\n     \"ok-stale fast @connection @replication\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"echo\",echoCommand,2,\n     \"fast @connection\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"save\",saveCommand,1,\n     \"admin no-script\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"bgsave\",bgsaveCommand,-1,\n     \"admin no-script\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"bgrewriteaof\",bgrewriteaofCommand,1,\n     \"admin no-script\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"shutdown\",shutdownCommand,-1,\n     \"admin no-script ok-loading ok-stale noprop\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"lastsave\",lastsaveCommand,1,\n     \"random fast ok-loading ok-stale @admin @dangerous\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"type\",typeCommand,2,\n     \"read-only fast @keyspace\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"multi\",multiCommand,1,\n     \"no-script fast ok-loading ok-stale @transaction\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"exec\",execCommand,1,\n     \"no-script no-slowlog ok-loading ok-stale @transaction\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"discard\",discardCommand,1,\n     \"no-script fast ok-loading ok-stale @transaction\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"sync\",syncCommand,1,\n     \"admin no-script @replication\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"psync\",syncCommand,-3,\n     \"admin no-script @replication\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"replconf\",replconfCommand,-1,\n     \"admin no-script ok-loading ok-stale @replication\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"flushdb\",flushdbCommand,-1,\n     \"write @keyspace @dangerous\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"flushall\",flushallCommand,-1,\n     \"write @keyspace @dangerous\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"sort\",sortCommand,-2,\n     \"write use-memory @list @set @sortedset @dangerous\",\n     0,sortGetKeys,1,1,1,0,0,0},\n\n    {\"info\",infoCommand,-1,\n     \"ok-loading ok-stale random @dangerous\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"monitor\",monitorCommand,1,\n     \"admin no-script ok-loading ok-stale\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"ttl\",ttlCommand,-2,\n     \"read-only fast random @keyspace\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"touch\",touchCommand,-2,\n     \"read-only fast @keyspace\",\n     0,NULL,1,-1,1,0,0,0},\n\n    {\"pttl\",pttlCommand,-2,\n     \"read-only fast random @keyspace\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"persist\",persistCommand,-2,\n     \"write fast @keyspace\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"slaveof\",replicaofCommand,3,\n     \"admin no-script ok-stale\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"replicaof\",replicaofCommand,-3,\n     \"admin no-script ok-stale\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"role\",roleCommand,1,\n     \"ok-loading ok-stale no-script fast @dangerous\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"debug\",debugCommand,-2,\n     \"admin no-script ok-loading ok-stale\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"config\",configCommand,-2,\n     \"admin ok-loading ok-stale no-script\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"subscribe\",subscribeCommand,-2,\n     \"pub-sub no-script ok-loading ok-stale\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"unsubscribe\",unsubscribeCommand,-1,\n     \"pub-sub no-script ok-loading ok-stale\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"psubscribe\",psubscribeCommand,-2,\n     \"pub-sub no-script ok-loading ok-stale\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"punsubscribe\",punsubscribeCommand,-1,\n     \"pub-sub no-script ok-loading ok-stale\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"publish\",publishCommand,3,\n     \"pub-sub ok-loading ok-stale fast may-replicate\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"pubsub\",pubsubCommand,-2,\n     \"pub-sub ok-loading ok-stale random\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"watch\",watchCommand,-2,\n     \"no-script fast ok-loading ok-stale @transaction\",\n     0,NULL,1,-1,1,0,0,0},\n\n    {\"unwatch\",unwatchCommand,1,\n     \"no-script fast ok-loading ok-stale @transaction\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"cluster\",clusterCommand,-2,\n     \"admin ok-stale random\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"restore\",restoreCommand,-4,\n     \"write use-memory @keyspace @dangerous\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"restore-asking\",restoreCommand,-4,\n    \"write use-memory cluster-asking @keyspace @dangerous\",\n    0,NULL,1,1,1,0,0,0},\n\n    {\"migrate\",migrateCommand,-6,\n     \"write random @keyspace @dangerous\",\n     0,migrateGetKeys,3,3,1,0,0,0},\n\n    {\"asking\",askingCommand,1,\n     \"fast @keyspace\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"readonly\",readonlyCommand,1,\n     \"fast @keyspace\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"readwrite\",readwriteCommand,1,\n     \"fast @keyspace\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"dump\",dumpCommand,2,\n     \"read-only random @keyspace\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"object\",objectCommand,-2,\n     \"read-only random @keyspace\",\n     0,NULL,2,2,1,0,0,0},\n\n    {\"memory\",memoryCommand,-2,\n     \"random read-only\",\n     0,memoryGetKeys,0,0,0,0,0,0},\n\n    {\"client\",clientCommand,-2,\n     \"admin no-script random ok-loading ok-stale @connection\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"hello\",helloCommand,-1,\n     \"no-auth no-script fast ok-loading ok-stale @connection\",\n     0,NULL,0,0,0,0,0,0},\n\n    /* EVAL can modify the dataset, however it is not flagged as a write\n     * command since we do the check while running commands from Lua.\n     * \n     * EVAL and EVALSHA also feed monitors before the commands are executed,\n     * as opposed to after.\n      */\n    {\"eval\",evalCommand,-3,\n     \"no-script no-monitor may-replicate @scripting\",\n     0,evalGetKeys,0,0,0,0,0,0},\n\n    {\"evalsha\",evalShaCommand,-3,\n     \"no-script no-monitor may-replicate @scripting\",\n     0,evalGetKeys,0,0,0,0,0,0},\n\n    {\"slowlog\",slowlogCommand,-2,\n     \"admin random ok-loading ok-stale\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"script\",scriptCommand,-2,\n     \"no-script may-replicate @scripting\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"time\",timeCommand,1,\n     \"random fast ok-loading ok-stale\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"bitop\",bitopCommand,-4,\n     \"write use-memory @bitmap\",\n     0,NULL,2,-1,1,0,0,0},\n\n    {\"bitcount\",bitcountCommand,-2,\n     \"read-only @bitmap\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"bitpos\",bitposCommand,-3,\n     \"read-only @bitmap\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"wait\",waitCommand,3,\n     \"no-script @keyspace\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"command\",commandCommand,-1,\n     \"ok-loading ok-stale random @connection\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"geoadd\",geoaddCommand,-5,\n     \"write use-memory @geo\",\n     0,NULL,1,1,1,0,0,0},\n\n    /* GEORADIUS has store options that may write. */\n    {\"georadius\",georadiusCommand,-6,\n     \"write use-memory @geo\",\n     0,georadiusGetKeys,1,1,1,0,0,0},\n\n    {\"georadius_ro\",georadiusroCommand,-6,\n     \"read-only @geo\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"georadiusbymember\",georadiusbymemberCommand,-5,\n     \"write use-memory @geo\",\n     0,georadiusGetKeys,1,1,1,0,0,0},\n\n    {\"georadiusbymember_ro\",georadiusbymemberroCommand,-5,\n     \"read-only @geo\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"geohash\",geohashCommand,-2,\n     \"read-only @geo\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"geopos\",geoposCommand,-2,\n     \"read-only @geo\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"geodist\",geodistCommand,-4,\n     \"read-only @geo\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"geosearch\",geosearchCommand,-7,\n     \"read-only @geo\",\n      0,NULL,1,1,1,0,0,0},\n\n    {\"geosearchstore\",geosearchstoreCommand,-8,\n     \"write use-memory @geo\",\n      0,NULL,1,2,1,0,0,0},\n\n    {\"pfselftest\",pfselftestCommand,1,\n     \"admin @hyperloglog\",\n      0,NULL,0,0,0,0,0,0},\n\n    {\"pfadd\",pfaddCommand,-2,\n     \"write use-memory fast @hyperloglog\",\n     0,NULL,1,1,1,0,0,0},\n\n    /* Technically speaking PFCOUNT may change the key since it changes the\n     * final bytes in the HyperLogLog representation. However in this case\n     * we claim that the representation, even if accessible, is an internal\n     * affair, and the command is semantically read only. */\n    {\"pfcount\",pfcountCommand,-2,\n     \"read-only may-replicate @hyperloglog\",\n     0,NULL,1,-1,1,0,0,0},\n\n    {\"pfmerge\",pfmergeCommand,-2,\n     \"write use-memory @hyperloglog\",\n     0,NULL,1,-1,1,0,0,0},\n\n    /* Unlike PFCOUNT that is considered as a read-only command (although\n     * it changes a bit), PFDEBUG may change the entire key when converting\n     * from sparse to dense representation */\n    {\"pfdebug\",pfdebugCommand,-3,\n     \"admin write use-memory @hyperloglog\",\n     0,NULL,2,2,1,0,0,0},\n\n    {\"xadd\",xaddCommand,-5,\n     \"write use-memory fast random @stream\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"xrange\",xrangeCommand,-4,\n     \"read-only @stream\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"xrevrange\",xrevrangeCommand,-4,\n     \"read-only @stream\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"xlen\",xlenCommand,2,\n     \"read-only fast @stream\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"xread\",xreadCommand,-4,\n     \"read-only @stream @blocking\",\n     0,xreadGetKeys,0,0,0,0,0,0},\n\n    {\"xreadgroup\",xreadCommand,-7,\n     \"write @stream @blocking\",\n     0,xreadGetKeys,0,0,0,0,0,0},\n\n    {\"xgroup\",xgroupCommand,-2,\n     \"write use-memory @stream\",\n     0,NULL,2,2,1,0,0,0},\n\n    {\"xsetid\",xsetidCommand,3,\n     \"write use-memory fast @stream\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"xack\",xackCommand,-4,\n     \"write fast random @stream\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"xpending\",xpendingCommand,-3,\n     \"read-only random @stream\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"xclaim\",xclaimCommand,-6,\n     \"write random fast @stream\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"xautoclaim\",xautoclaimCommand,-6,\n     \"write random fast @stream\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"xinfo\",xinfoCommand,-2,\n     \"read-only random @stream\",\n     0,NULL,2,2,1,0,0,0},\n\n    {\"xdel\",xdelCommand,-3,\n     \"write fast @stream\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"xtrim\",xtrimCommand,-4,\n     \"write random @stream\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"post\",securityWarningCommand,-1,\n     \"ok-loading ok-stale read-only\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"host:\",securityWarningCommand,-1,\n     \"ok-loading ok-stale read-only\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"latency\",latencyCommand,-2,\n     \"admin no-script ok-loading ok-stale\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"acl\",aclCommand,-2,\n     \"admin no-script ok-loading ok-stale\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"rreplay\",replicaReplayCommand,-3,\n     \"read-only fast noprop ok-stale\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"keydb.cron\",cronCommand,-5,\n     \"write use-memory\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"keydb.hrename\", hrenameCommand, 4,\n     \"write fast @hash\",\n     0,NULL,0,0,0,0,0,0},\n    \n    {\"stralgo\",stralgoCommand,-2,\n     \"read-only @string\",\n     0,lcsGetKeys,0,0,0,0,0,0},\n\n    {\"keydb.nhget\",nhgetCommand,-2,\n     \"read-only fast @hash\",\n     0,NULL,1,1,1,0,0,0},\n    \n    {\"keydb.nhset\",nhsetCommand,-3,\n     \"read-only fast @hash\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"KEYDB.MVCCRESTORE\",mvccrestoreCommand, 5,\n     \"write use-memory @keyspace @dangerous\",\n     0,NULL,1,1,1,0,0,0},\n\n    {\"reset\",resetCommand,1,\n     \"no-script ok-stale ok-loading fast @connection\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"failover\",failoverCommand,-1,\n     \"admin no-script ok-stale\",\n     0,NULL,0,0,0,0,0,0},\n\n    {\"lfence\", lfenceCommand,1,\n     \"read-only random ok-stale\",\n     0,NULL,0,0,0,0,0,0}\n};\n\n/*============================ Utility functions ============================ */\n\n/* We use a private localtime implementation which is fork-safe. The logging\n * function of Redis may be called from other threads. */\nextern \"C\" void nolocks_localtime(struct tm *tmp, time_t t, time_t tz, int dst);\nextern \"C\" pid_t gettid();\n\nvoid processClients();\n\n/* Low level logging. To use only for very big messages, otherwise\n * serverLog() is to prefer. */\n#if defined(__has_feature)\n#  if __has_feature(thread_sanitizer)\n__attribute__((no_sanitize(\"thread\")))\n#  endif\n#endif\nvoid serverLogRaw(int level, const char *msg) {\n    const int syslogLevelMap[] = { LOG_DEBUG, LOG_INFO, LOG_NOTICE, LOG_WARNING };\n    const char *c = \".-*#                                                             \";\n    FILE *fp;\n    char buf[64];\n    int rawmode = (level & LL_RAW);\n    int log_to_stdout = g_pserver->logfile[0] == '\\0';\n\n    level &= 0xff; /* clear flags */\n    if (level < cserver.verbosity) return;\n\n    fp = log_to_stdout ? stdout : fopen(g_pserver->logfile,\"a\");\n    if (!fp) return;\n\n    if (rawmode) {\n        fprintf(fp,\"%s\",msg);\n    } else {\n        int off;\n        struct timeval tv;\n        int role_char;\n        pid_t pid = getpid();\n\n        gettimeofday(&tv,NULL);\n        struct tm tm;\n        int daylight_active;\n        __atomic_load(&g_pserver->daylight_active, &daylight_active, __ATOMIC_RELAXED);\n        nolocks_localtime(&tm,tv.tv_sec,g_pserver->timezone,daylight_active);\n        off = strftime(buf,sizeof(buf),\"%d %b %Y %H:%M:%S.\",&tm);\n        snprintf(buf+off,sizeof(buf)-off,\"%03d\",(int)tv.tv_usec/1000);\n        if (g_pserver->sentinel_mode) {\n            role_char = 'X'; /* Sentinel. */\n        } else if (pid != cserver.pid) {\n            role_char = 'C'; /* RDB / AOF writing child. */\n        } else {\n            role_char = (listLength(g_pserver->masters) ? 'S':'M'); /* Slave or Master. */\n        }\n        fprintf(fp,\"%d:%d:%c %s %c %s\\n\",\n            (int)getpid(),(int)gettid(),role_char, buf,c[level],msg);\n    }\n    fflush(fp);\n\n    if (!log_to_stdout) fclose(fp);\n    if (g_pserver->syslog_enabled) syslog(syslogLevelMap[level], \"%s\", msg);\n}\n\n/* Like serverLogRaw() but with printf-alike support. This is the function that\n * is used across the code. The raw version is only used in order to dump\n * the INFO output on crash. */\n#if defined(__has_feature)\n#  if __has_feature(thread_sanitizer)\n__attribute__((no_sanitize(\"thread\")))\n#  endif\n#endif\nvoid _serverLog(int level, const char *fmt, ...) {\n    va_list ap;\n    char msg[LOG_MAX_LEN];\n\n    va_start(ap, fmt);\n    vsnprintf(msg, sizeof(msg), fmt, ap);\n    va_end(ap);\n\n    serverLogRaw(level,msg);\n}\n\n/* Log a fixed message without printf-alike capabilities, in a way that is\n * safe to call from a signal handler.\n *\n * We actually use this only for signals that are not fatal from the point\n * of view of Redis. Signals that are going to kill the server anyway and\n * where we need printf-alike features are served by serverLog(). */\n#if defined(__has_feature)\n#  if __has_feature(thread_sanitizer)\n__attribute__((no_sanitize(\"thread\")))\n#  endif\n#endif\nvoid serverLogFromHandler(int level, const char *msg) {\n    int fd;\n    int log_to_stdout = g_pserver->logfile[0] == '\\0';\n    char buf[64];\n\n    if ((level&0xff) < cserver.verbosity || (log_to_stdout && cserver.daemonize))\n        return;\n    fd = log_to_stdout ? STDOUT_FILENO :\n                         open(g_pserver->logfile, O_APPEND|O_CREAT|O_WRONLY, 0644);\n    if (fd == -1) return;\n    ll2string(buf,sizeof(buf),getpid());\n    if (write(fd,buf,strlen(buf)) == -1) goto err;\n    if (write(fd,\":signal-handler (\",17) == -1) goto err;\n    ll2string(buf,sizeof(buf),time(NULL));\n    if (write(fd,buf,strlen(buf)) == -1) goto err;\n    if (write(fd,\") \",2) == -1) goto err;\n    if (write(fd,msg,strlen(msg)) == -1) goto err;\n    if (write(fd,\"\\n\",1) == -1) goto err;\nerr:\n    if (!log_to_stdout) close(fd);\n}\n\n/* Return the UNIX time in microseconds */\nlong long ustime(void) {\n    struct timeval tv;\n    long long ust;\n\n    gettimeofday(&tv, NULL);\n    ust = ((long long)tv.tv_sec)*1000000;\n    ust += tv.tv_usec;\n    return ust;\n}\n\n/* Return the UNIX time in milliseconds */\nmstime_t mstime(void) {\n    return ustime()/1000;\n}\n\n/* After an RDB dump or AOF rewrite we exit from children using _exit() instead of\n * exit(), because the latter may interact with the same file objects used by\n * the parent process. However if we are testing the coverage normal exit() is\n * used in order to obtain the right coverage information. */\nvoid exitFromChild(int retcode) {\n#ifdef COVERAGE_TEST\n    exit(retcode);\n#else\n    _exit(retcode);\n#endif\n}\n\n/*====================== Hash table type implementation  ==================== */\n\n/* This is a hash table type that uses the SDS dynamic strings library as\n * keys and redis objects as values (objects can hold SDS strings,\n * lists, sets). */\n\nvoid dictVanillaFree(void *privdata, void *val)\n{\n    DICT_NOTUSED(privdata);\n    zfree(val);\n}\n\nvoid dictListDestructor(void *privdata, void *val)\n{\n    DICT_NOTUSED(privdata);\n    listRelease((list*)val);\n}\n\nint dictSdsKeyCompare(void *privdata, const void *key1,\n        const void *key2)\n{\n    int l1,l2;\n    DICT_NOTUSED(privdata);\n\n    l1 = sdslen((sds)key1);\n    l2 = sdslen((sds)key2);\n    if (l1 != l2) return 0;\n    return memcmp(key1, key2, l1) == 0;\n}\n\nvoid dictSdsNOPDestructor(void *, void *) {}\n\nvoid dictDbKeyDestructor(void *privdata, void *key)\n{\n    DICT_NOTUSED(privdata);\n    sdsfree((sds)key);\n}\n\n/* A case insensitive version used for the command lookup table and other\n * places where case insensitive non binary-safe comparison is needed. */\nint dictSdsKeyCaseCompare(void *privdata, const void *key1,\n        const void *key2)\n{\n    DICT_NOTUSED(privdata);\n\n    return strcasecmp((const char*)key1, (const char*)key2) == 0;\n}\n\nvoid dictObjectDestructor(void *privdata, void *val)\n{\n    DICT_NOTUSED(privdata);\n\n    if (val == NULL) return; /* Lazy freeing will set value to NULL. */\n    decrRefCount((robj*)val);\n}\n\nvoid dictSdsDestructor(void *privdata, void *val)\n{\n    DICT_NOTUSED(privdata);\n\n    sdsfree((sds)val);\n}\n\nint dictObjKeyCompare(void *privdata, const void *key1,\n        const void *key2)\n{\n    const robj *o1 = (const robj*)key1, *o2 = (const robj*)key2;\n    return dictSdsKeyCompare(privdata,ptrFromObj(o1),ptrFromObj(o2));\n}\n\nuint64_t dictObjHash(const void *key) {\n    const robj *o = (const robj*)key;\n    void *ptr = ptrFromObj(o);\n    return dictGenHashFunction(ptr, sdslen((sds)ptr));\n}\n\nuint64_t dictSdsHash(const void *key) {\n    return dictGenHashFunction((unsigned char*)key, sdslen((char*)key));\n}\n\nuint64_t dictSdsCaseHash(const void *key) {\n    return dictGenCaseHashFunction((unsigned char*)key, sdslen((char*)key));\n}\n\nint dictEncObjKeyCompare(void *privdata, const void *key1,\n        const void *key2)\n{\n    robj *o1 = (robj*) key1, *o2 = (robj*) key2;\n    int cmp;\n\n    if (o1->encoding == OBJ_ENCODING_INT &&\n        o2->encoding == OBJ_ENCODING_INT)\n            return ptrFromObj(o1) == ptrFromObj(o2);\n\n    /* Due to OBJ_STATIC_REFCOUNT, we avoid calling getDecodedObject() without\n     * good reasons, because it would incrRefCount() the object, which\n     * is invalid. So we check to make sure dictFind() works with static\n     * objects as well. */\n    if (o1->getrefcount() != OBJ_STATIC_REFCOUNT) o1 = getDecodedObject(o1);\n    if (o2->getrefcount() != OBJ_STATIC_REFCOUNT) o2 = getDecodedObject(o2);\n    cmp = dictSdsKeyCompare(privdata,ptrFromObj(o1),ptrFromObj(o2));\n    if (o1->getrefcount() != OBJ_STATIC_REFCOUNT) decrRefCount(o1);\n    if (o2->getrefcount() != OBJ_STATIC_REFCOUNT) decrRefCount(o2);\n    return cmp;\n}\n\nuint64_t dictEncObjHash(const void *key) {\n    robj *o = (robj*) key;\n\n    if (sdsEncodedObject(o)) {\n        return dictGenHashFunction(ptrFromObj(o), sdslen((sds)ptrFromObj(o)));\n    } else if (o->encoding == OBJ_ENCODING_INT) {\n        char buf[32];\n        int len;\n\n        len = ll2string(buf,32,(long)ptrFromObj(o));\n        return dictGenHashFunction((unsigned char*)buf, len);\n    } else {\n        serverPanic(\"Unknown string encoding\");\n    }\n}\n\n/* Return 1 if currently we allow dict to expand. Dict may allocate huge\n * memory to contain hash buckets when dict expands, that may lead redis\n * rejects user's requests or evicts some keys, we can stop dict to expand\n * provisionally if used memory will be over maxmemory after dict expands,\n * but to guarantee the performance of redis, we still allow dict to expand\n * if dict load factor exceeds HASHTABLE_MAX_LOAD_FACTOR. */\nint dictExpandAllowed(size_t moreMem, double usedRatio) {\n    if (usedRatio <= HASHTABLE_MAX_LOAD_FACTOR) {\n        return !overMaxmemoryAfterAlloc(moreMem);\n    } else {\n        return 1;\n    }\n}\n\nvoid dictGCAsyncFree(dictAsyncRehashCtl *async);\n\n/* Generic hash table type where keys are Redis Objects, Values\n * dummy pointers. */\ndictType objectKeyPointerValueDictType = {\n    dictEncObjHash,            /* hash function */\n    NULL,                      /* key dup */\n    NULL,                      /* val dup */\n    dictEncObjKeyCompare,      /* key compare */\n    dictObjectDestructor,      /* key destructor */\n    NULL,                      /* val destructor */\n    NULL                       /* allow to expand */\n};\n\n/* Like objectKeyPointerValueDictType(), but values can be destroyed, if\n * not NULL, calling zfree(). */\ndictType objectKeyHeapPointerValueDictType = {\n    dictEncObjHash,            /* hash function */\n    NULL,                      /* key dup */\n    NULL,                      /* val dup */\n    dictEncObjKeyCompare,      /* key compare */\n    dictObjectDestructor,      /* key destructor */\n    dictVanillaFree,           /* val destructor */\n    NULL                       /* allow to expand */\n};\n\n/* Set dictionary type. Keys are SDS strings, values are not used. */\ndictType setDictType = {\n    dictSdsHash,               /* hash function */\n    NULL,                      /* key dup */\n    NULL,                      /* val dup */\n    dictSdsKeyCompare,         /* key compare */\n    dictSdsDestructor,         /* key destructor */\n    NULL                       /* val destructor */\n};\n\n/* Sorted sets hash (note: a skiplist is used in addition to the hash table) */\ndictType zsetDictType = {\n    dictSdsHash,               /* hash function */\n    NULL,                      /* key dup */\n    NULL,                      /* val dup */\n    dictSdsKeyCompare,         /* key compare */\n    NULL,                      /* Note: SDS string shared & freed by skiplist */\n    NULL,                      /* val destructor */\n    NULL                       /* allow to expand */\n};\n\n/* db->dict, keys are sds strings, vals are Redis objects. */\ndictType dbDictType = {\n    dictSdsHash,                /* hash function */\n    NULL,                       /* key dup */\n    NULL,                       /* val dup */\n    dictSdsKeyCompare,          /* key compare */\n    dictDbKeyDestructor,        /* key destructor */\n    dictObjectDestructor,       /* val destructor */\n    dictExpandAllowed,           /* allow to expand */\n    dictGCAsyncFree             /* async free destructor */\n};\n\ndictType dbExpiresDictType = {\n        dictSdsHash,                /* hash function */\n        NULL,                       /* key dup */\n        NULL,                       /* val dup */\n        dictSdsKeyCompare,          /* key compare */\n        NULL,                       /* key destructor */\n        NULL,                       /* val destructor */\n        dictExpandAllowed           /* allow to expand */\n    };\n\n/* db->pdict, keys are sds strings, vals are Redis objects. */\ndictType dbTombstoneDictType = {\n    dictSdsHash,                /* hash function */\n    NULL,                       /* key dup */\n    NULL,                       /* val dup */\n    dictSdsKeyCompare,          /* key compare */\n    dictDbKeyDestructor,        /* key destructor */\n    NULL,                       /* val destructor */\n    dictExpandAllowed           /* allow to expand */\n};\n\ndictType dbSnapshotDictType = {\n    dictSdsHash,\n    NULL,\n    NULL,\n    dictSdsKeyCompare,\n    dictSdsNOPDestructor,\n    dictObjectDestructor,\n    dictExpandAllowed           /* allow to expand */\n};\n\n/* g_pserver->lua_scripts sha (as sds string) -> scripts (as robj) cache. */\ndictType shaScriptObjectDictType = {\n    dictSdsCaseHash,            /* hash function */\n    NULL,                       /* key dup */\n    NULL,                       /* val dup */\n    dictSdsKeyCaseCompare,      /* key compare */\n    dictSdsDestructor,          /* key destructor */\n    dictObjectDestructor,       /* val destructor */\n    NULL                        /* allow to expand */\n};\n\n/* Command table. sds string -> command struct pointer. */\ndictType commandTableDictType = {\n    dictSdsCaseHash,            /* hash function */\n    NULL,                       /* key dup */\n    NULL,                       /* val dup */\n    dictSdsKeyCaseCompare,      /* key compare */\n    dictSdsDestructor,          /* key destructor */\n    NULL,                       /* val destructor */\n    NULL                        /* allow to expand */\n};\n\n/* Hash type hash table (note that small hashes are represented with ziplists) */\ndictType hashDictType = {\n    dictSdsHash,                /* hash function */\n    NULL,                       /* key dup */\n    NULL,                       /* val dup */\n    dictSdsKeyCompare,          /* key compare */\n    dictSdsDestructor,          /* key destructor */\n    dictSdsDestructor,          /* val destructor */\n    NULL                        /* allow to expand */\n};\n\n/* Dict type without destructor */\ndictType sdsReplyDictType = {\n    dictSdsHash,                /* hash function */\n    NULL,                       /* key dup */\n    NULL,                       /* val dup */\n    dictSdsKeyCompare,          /* key compare */\n    NULL,                       /* key destructor */\n    NULL,                       /* val destructor */\n    NULL                        /* allow to expand */\n};\n\n/* Keylist hash table type has unencoded redis objects as keys and\n * lists as values. It's used for blocking operations (BLPOP) and to\n * map swapped keys to a list of clients waiting for this keys to be loaded. */\ndictType keylistDictType = {\n    dictObjHash,                /* hash function */\n    NULL,                       /* key dup */\n    NULL,                       /* val dup */\n    dictObjKeyCompare,          /* key compare */\n    dictObjectDestructor,       /* key destructor */\n    dictListDestructor,         /* val destructor */\n    NULL                        /* allow to expand */\n};\n\n/* Cluster nodes hash table, mapping nodes addresses 1.2.3.4:6379 to\n * clusterNode structures. */\ndictType clusterNodesDictType = {\n    dictSdsHash,                /* hash function */\n    NULL,                       /* key dup */\n    NULL,                       /* val dup */\n    dictSdsKeyCompare,          /* key compare */\n    dictSdsDestructor,          /* key destructor */\n    NULL,                       /* val destructor */\n    NULL                        /* allow to expand */\n};\n\n/* Cluster re-addition blacklist. This maps node IDs to the time\n * we can re-add this node. The goal is to avoid readding a removed\n * node for some time. */\ndictType clusterNodesBlackListDictType = {\n    dictSdsCaseHash,            /* hash function */\n    NULL,                       /* key dup */\n    NULL,                       /* val dup */\n    dictSdsKeyCaseCompare,      /* key compare */\n    dictSdsDestructor,          /* key destructor */\n    NULL,                       /* val destructor */\n    NULL                        /* allow to expand */\n};\n\n/* Modules system dictionary type. Keys are module name,\n * values are pointer to RedisModule struct. */\ndictType modulesDictType = {\n    dictSdsCaseHash,            /* hash function */\n    NULL,                       /* key dup */\n    NULL,                       /* val dup */\n    dictSdsKeyCaseCompare,      /* key compare */\n    dictSdsDestructor,          /* key destructor */\n    NULL,                       /* val destructor */\n    NULL                        /* allow to expand */\n};\n\n/* Migrate cache dict type. */\ndictType migrateCacheDictType = {\n    dictSdsHash,                /* hash function */\n    NULL,                       /* key dup */\n    NULL,                       /* val dup */\n    dictSdsKeyCompare,          /* key compare */\n    dictSdsDestructor,          /* key destructor */\n    NULL,                       /* val destructor */\n    NULL                        /* allow to expand */\n};\n\n/* Replication cached script dict (g_pserver->repl_scriptcache_dict).\n * Keys are sds SHA1 strings, while values are not used at all in the current\n * implementation. */\ndictType replScriptCacheDictType = {\n    dictSdsCaseHash,            /* hash function */\n    NULL,                       /* key dup */\n    NULL,                       /* val dup */\n    dictSdsKeyCaseCompare,      /* key compare */\n    dictSdsDestructor,          /* key destructor */\n    NULL,                       /* val destructor */\n    NULL                        /* allow to expand */\n};\n\nint htNeedsResize(dict *dict) {\n    long long size, used;\n\n    size = dictSlots(dict);\n    used = dictSize(dict);\n    return (size > DICT_HT_INITIAL_SIZE &&\n            (used*100/size < HASHTABLE_MIN_FILL));\n}\n\n/* If the percentage of used slots in the HT reaches HASHTABLE_MIN_FILL\n * we resize the hash table to save memory */\nvoid tryResizeHashTables(int dbid) {\n    g_pserver->db[dbid]->tryResize();\n}\n\n/* Our hash table implementation performs rehashing incrementally while\n * we write/read from the hash table. Still if the server is idle, the hash\n * table will use two tables for a long time. So we try to use 1 millisecond\n * of CPU time at every call of this function to perform some rehashing.\n *\n * The function returns the number of rehashes if some rehashing was performed, otherwise 0\n * is returned. */\nint redisDbPersistentData::incrementallyRehash() {\n    /* Keys dictionary */\n    int result = 0;\n    if (dictIsRehashing(m_pdict))\n        result += dictRehashMilliseconds(m_pdict,1);\n    if (dictIsRehashing(m_pdictTombstone))\n        dictRehashMilliseconds(m_pdictTombstone,1); // don't count this\n    return result; /* already used our millisecond for this loop... */\n}\n\n/* This function is called once a background process of some kind terminates,\n * as we want to avoid resizing the hash tables when there is a child in order\n * to play well with copy-on-write (otherwise when a resize happens lots of\n * memory pages are copied). The goal of this function is to update the ability\n * for dict.c to resize the hash tables accordingly to the fact we have an\n * active fork child running. */\nvoid updateDictResizePolicy(void) {\n    if (!hasActiveChildProcess())\n        dictEnableResize();\n    else\n        dictDisableResize();\n}\n\nconst char *strChildType(int type) {\n    switch(type) {\n        case CHILD_TYPE_RDB: return \"RDB\";\n        case CHILD_TYPE_AOF: return \"AOF\";\n        case CHILD_TYPE_LDB: return \"LDB\";\n        case CHILD_TYPE_MODULE: return \"MODULE\";\n        default: return \"Unknown\";\n    }\n}\n\n/* Return true if there are active children processes doing RDB saving,\n * AOF rewriting, or some side process spawned by a loaded module. */\nint hasActiveChildProcess() {\n    return g_pserver->child_pid != -1;\n}\n\nint hasActiveChildProcessOrBGSave() {\n    return g_pserver->FRdbSaveInProgress() || hasActiveChildProcess();\n}\n\nvoid resetChildState() {\n    g_pserver->child_type = CHILD_TYPE_NONE;\n    g_pserver->child_pid = -1;\n    g_pserver->stat_current_cow_bytes = 0;\n    g_pserver->stat_current_cow_updated = 0;\n    g_pserver->stat_current_save_keys_processed = 0;\n    g_pserver->stat_module_progress = 0;\n    g_pserver->stat_current_save_keys_total = 0;\n    updateDictResizePolicy();\n    closeChildInfoPipe();\n    moduleFireServerEvent(REDISMODULE_EVENT_FORK_CHILD,\n                          REDISMODULE_SUBEVENT_FORK_CHILD_DIED,\n                          NULL);\n}\n\n/* Return if child type is mutual exclusive with other fork children */\nint isMutuallyExclusiveChildType(int type) {\n    return type == CHILD_TYPE_RDB || type == CHILD_TYPE_AOF || type == CHILD_TYPE_MODULE;\n}\n\n/* Return true if this instance has persistence completely turned off:\n * both RDB and AOF are disabled. */\nint allPersistenceDisabled(void) {\n    return g_pserver->saveparamslen == 0 && g_pserver->aof_state == AOF_OFF;\n}\n\n/* ======================= Cron: called every 100 ms ======================== */\n\n/* Add a sample to the operations per second array of samples. */\nvoid trackInstantaneousMetric(int metric, long long current_reading) {\n    long long now = mstime();\n    long long t = now - g_pserver->inst_metric[metric].last_sample_time;\n    long long ops = current_reading -\n                    g_pserver->inst_metric[metric].last_sample_count;\n    long long ops_sec;\n\n    ops_sec = t > 0 ? (ops*1000/t) : 0;\n\n    g_pserver->inst_metric[metric].samples[g_pserver->inst_metric[metric].idx] =\n        ops_sec;\n    g_pserver->inst_metric[metric].idx++;\n    g_pserver->inst_metric[metric].idx %= STATS_METRIC_SAMPLES;\n    g_pserver->inst_metric[metric].last_sample_time = now;\n    g_pserver->inst_metric[metric].last_sample_count = current_reading;\n}\n\n/* Return the mean of all the samples. */\nlong long getInstantaneousMetric(int metric) {\n    int j;\n    long long sum = 0;\n\n    for (j = 0; j < STATS_METRIC_SAMPLES; j++)\n        sum += g_pserver->inst_metric[metric].samples[j];\n    return sum / STATS_METRIC_SAMPLES;\n}\n\n/* The client query buffer is an sds.c string that can end with a lot of\n * free space not used, this function reclaims space if needed.\n *\n * The function always returns 0 as it never terminates the client. */\nint clientsCronResizeQueryBuffer(client *c) {\n    AssertCorrectThread(c);\n    size_t querybuf_size = sdsAllocSize(c->querybuf);\n    time_t idletime = g_pserver->unixtime - c->lastinteraction;\n\n    /* There are two conditions to resize the query buffer:\n     * 1) Query buffer is > BIG_ARG and too big for latest peak.\n     * 2) Query buffer is > BIG_ARG and client is idle. */\n    if (querybuf_size > PROTO_MBULK_BIG_ARG &&\n         ((querybuf_size/(c->querybuf_peak+1)) > 2 ||\n          idletime > 2))\n    {\n        /* Only resize the query buffer if it is actually wasting\n         * at least a few kbytes. */\n        if (sdsavail(c->querybuf) > 1024*4) {\n            c->querybuf = sdsRemoveFreeSpace(c->querybuf);\n        }\n    }\n    /* Reset the peak again to capture the peak memory usage in the next\n     * cycle. */\n    c->querybuf_peak = 0;\n\n    /* Clients representing masters also use a \"pending query buffer\" that\n     * is the yet not applied part of the stream we are reading. Such buffer\n     * also needs resizing from time to time, otherwise after a very large\n     * transfer (a huge value or a big MIGRATE operation) it will keep using\n     * a lot of memory. */\n    if (c->flags & CLIENT_MASTER) {\n        /* There are two conditions to resize the pending query buffer:\n         * 1) Pending Query buffer is > LIMIT_PENDING_QUERYBUF.\n         * 2) Used length is smaller than pending_querybuf_size/2 */\n        size_t pending_querybuf_size = sdsAllocSize(c->pending_querybuf);\n        if(pending_querybuf_size > LIMIT_PENDING_QUERYBUF &&\n           sdslen(c->pending_querybuf) < (pending_querybuf_size/2))\n        {\n            c->pending_querybuf = sdsRemoveFreeSpace(c->pending_querybuf);\n        }\n    }\n    return 0;\n}\n\nSymVer parseVersion(const char *version)\n{\n    SymVer ver = {-1,-1,-1};\n    long versions[3] = {-1,-1,-1};\n    const char *start = version;\n    const char *end = nullptr;\n\n    for (int iver = 0; iver < 3; ++iver)\n    {\n        end = start;\n        while (*end != '\\0' && *end != '.')\n            ++end;\n\n        if (start >= end)\n            return ver;\n\n        if (!string2l(start, end - start, versions + iver))\n            return ver;\n        if (*end != '\\0')\n            start = end+1;\n        else\n            break;\n    }\n    ver.major = versions[0];\n    ver.minor = versions[1];\n    ver.build = versions[2];\n    \n    return ver;\n}\n\nVersionCompareResult compareVersion(SymVer *pver)\n{\n    SymVer symVerThis = parseVersion(KEYDB_REAL_VERSION);\n    // Special case, 0.0.0 is equal to any version\n    if ((symVerThis.major == 0 && symVerThis.minor == 0 && symVerThis.build == 0)\n        || (pver->major == 0 && pver->minor == 0 && pver->build == 0))\n        return VersionCompareResult::EqualVersion;\n\n    if (pver->major <= 6 && pver->minor <= 3 && pver->build <= 3)\n        return VersionCompareResult::IncompatibleVersion;\n    \n    for (int iver = 0; iver < 3; ++iver)\n    {\n        long verThis, verOther;\n        switch (iver)\n        {\n        case 0:\n            verThis = symVerThis.major; verOther = pver->major;\n            break;\n        case 1:\n            verThis = symVerThis.minor; verOther = pver->minor;\n            break;\n        case 2:\n            verThis = symVerThis.build; verOther = pver->build;\n        }\n        \n        if (verThis < verOther)\n            return VersionCompareResult::NewerVersion;\n        if (verThis > verOther)\n            return VersionCompareResult::OlderVersion;\n    }\n    return VersionCompareResult::EqualVersion;\n}\n\n/* This function is used in order to track clients using the biggest amount\n * of memory in the latest few seconds. This way we can provide such information\n * in the INFO output (clients section), without having to do an O(N) scan for\n * all the clients.\n *\n * This is how it works. We have an array of CLIENTS_PEAK_MEM_USAGE_SLOTS slots\n * where we track, for each, the biggest client output and input buffers we\n * saw in that slot. Every slot correspond to one of the latest seconds, since\n * the array is indexed by doing UNIXTIME % CLIENTS_PEAK_MEM_USAGE_SLOTS.\n *\n * When we want to know what was recently the peak memory usage, we just scan\n * such few slots searching for the maximum value. */\n#define CLIENTS_PEAK_MEM_USAGE_SLOTS 8\nsize_t ClientsPeakMemInput[CLIENTS_PEAK_MEM_USAGE_SLOTS] = {0};\nsize_t ClientsPeakMemOutput[CLIENTS_PEAK_MEM_USAGE_SLOTS] = {0};\n\nint clientsCronTrackExpansiveClients(client *c, int time_idx) {\n    size_t in_usage = sdsZmallocSize(c->querybuf) + c->argv_len_sum() +\n\t              (c->argv ? zmalloc_size(c->argv) : 0);\n    size_t out_usage = getClientOutputBufferMemoryUsage(c);\n\n    /* Track the biggest values observed so far in this slot. */\n    if (in_usage > ClientsPeakMemInput[time_idx]) ClientsPeakMemInput[time_idx] = in_usage;\n    if (out_usage > ClientsPeakMemOutput[time_idx]) ClientsPeakMemOutput[time_idx] = out_usage;\n\n    return 0; /* This function never terminates the client. */\n}\n\n/* Iterating all the clients in getMemoryOverheadData() is too slow and\n * in turn would make the INFO command too slow. So we perform this\n * computation incrementally and track the (not instantaneous but updated\n * to the second) total memory used by clients using clinetsCron() in\n * a more incremental way (depending on g_pserver->hz). */\nint clientsCronTrackClientsMemUsage(client *c) {\n    size_t mem = 0;\n    int type = getClientType(c);\n    mem += getClientOutputBufferMemoryUsage(c);\n    mem += sdsZmallocSize(c->querybuf);\n    mem += zmalloc_size(c);\n    mem += c->argv_len_sum();\n    if (c->argv) mem += zmalloc_size(c->argv);\n    /* Now that we have the memory used by the client, remove the old\n     * value from the old category, and add it back. */\n    g_pserver->stat_clients_type_memory[c->client_cron_last_memory_type] -=\n        c->client_cron_last_memory_usage;\n    g_pserver->stat_clients_type_memory[type] += mem;\n    /* Remember what we added and where, to remove it next time. */\n    c->client_cron_last_memory_usage = mem;\n    c->client_cron_last_memory_type = type;\n    return 0;\n}\n\n/* Return the max samples in the memory usage of clients tracked by\n * the function clientsCronTrackExpansiveClients(). */\nvoid getExpansiveClientsInfo(size_t *in_usage, size_t *out_usage) {\n    size_t i = 0, o = 0;\n    for (int j = 0; j < CLIENTS_PEAK_MEM_USAGE_SLOTS; j++) {\n        if (ClientsPeakMemInput[j] > i) i = ClientsPeakMemInput[j];\n        if (ClientsPeakMemOutput[j] > o) o = ClientsPeakMemOutput[j];\n    }\n    *in_usage = i;\n    *out_usage = o;\n}\n\nint closeClientOnOverload(client *c) {\n    if (g_pserver->overload_closed_clients > MAX_CLIENTS_SHED_PER_PERIOD) return false;\n    if (!g_pserver->is_overloaded) return false;\n    // Don't close masters, replicas, or pub/sub clients\n    if (c->flags & (CLIENT_MASTER | CLIENT_SLAVE | CLIENT_PENDING_WRITE | CLIENT_PUBSUB | CLIENT_BLOCKED)) return false;\n    freeClient(c);\n    ++g_pserver->overload_closed_clients;\n    return true;\n}\n\n/* This function is called by serverCron() and is used in order to perform\n * operations on clients that are important to perform constantly. For instance\n * we use this function in order to disconnect clients after a timeout, including\n * clients blocked in some blocking command with a non-zero timeout.\n *\n * The function makes some effort to process all the clients every second, even\n * if this cannot be strictly guaranteed, since serverCron() may be called with\n * an actual frequency lower than g_pserver->hz in case of latency events like slow\n * commands.\n *\n * It is very important for this function, and the functions it calls, to be\n * very fast: sometimes Redis has tens of hundreds of connected clients, and the\n * default g_pserver->hz value is 10, so sometimes here we need to process thousands\n * of clients per second, turning this function into a source of latency.\n */\n#define CLIENTS_CRON_MIN_ITERATIONS 5\nvoid clientsCron(int iel) {\n    /* Try to process at least numclients/g_pserver->hz of clients\n     * per call. Since normally (if there are no big latency events) this\n     * function is called g_pserver->hz times per second, in the average case we\n     * process all the clients in 1 second. */\n    int numclients = listLength(g_pserver->clients);\n    int iterations = numclients/g_pserver->hz;\n    mstime_t now = mstime();\n\n    /* Process at least a few clients while we are at it, even if we need\n     * to process less than CLIENTS_CRON_MIN_ITERATIONS to meet our contract\n     * of processing each client once per second. */\n    if (iterations < CLIENTS_CRON_MIN_ITERATIONS)\n        iterations = (numclients < CLIENTS_CRON_MIN_ITERATIONS) ?\n                     numclients : CLIENTS_CRON_MIN_ITERATIONS;\n\n\n    int curr_peak_mem_usage_slot = g_pserver->unixtime % CLIENTS_PEAK_MEM_USAGE_SLOTS;\n    /* Always zero the next sample, so that when we switch to that second, we'll\n     * only register samples that are greater in that second without considering\n     * the history of such slot.\n     *\n     * Note: our index may jump to any random position if serverCron() is not\n     * called for some reason with the normal frequency, for instance because\n     * some slow command is called taking multiple seconds to execute. In that\n     * case our array may end containing data which is potentially older\n     * than CLIENTS_PEAK_MEM_USAGE_SLOTS seconds: however this is not a problem\n     * since here we want just to track if \"recently\" there were very expansive\n     * clients from the POV of memory usage. */\n    int zeroidx = (curr_peak_mem_usage_slot+1) % CLIENTS_PEAK_MEM_USAGE_SLOTS;\n    ClientsPeakMemInput[zeroidx] = 0;\n    ClientsPeakMemOutput[zeroidx] = 0;\n\n\n    while(listLength(g_pserver->clients) && iterations--) {\n        client *c;\n        listNode *head;\n        /* Rotate the list, take the current head, process.\n         * This way if the client must be removed from the list it's the\n         * first element and we don't incur into O(N) computation. */\n        listRotateTailToHead(g_pserver->clients);\n        head = (listNode*)listFirst(g_pserver->clients);\n        c = (client*)listNodeValue(head);\n        if (c->iel == iel)\n        {\n            fastlock_lock(&c->lock);\n            /* The following functions do different service checks on the client.\n            * The protocol is that they return non-zero if the client was\n            * terminated. */\n            if (clientsCronHandleTimeout(c,now)) continue;  // Client free'd so don't release the lock\n            if (clientsCronResizeQueryBuffer(c)) goto LContinue;\n            if (clientsCronTrackExpansiveClients(c, curr_peak_mem_usage_slot)) goto LContinue;\n            if (clientsCronTrackClientsMemUsage(c)) goto LContinue;\n            if (closeClientOnOutputBufferLimitReached(c, 0)) continue; // Client also free'd\n            if (closeClientOnOverload(c)) continue;\n        LContinue:\n            fastlock_unlock(&c->lock);\n        }        \n    }\n\n    /* Free any pending clients */\n    freeClientsInAsyncFreeQueue(iel);\n}\n\nbool expireOwnKeys()\n{\n    if (iAmMaster()) {\n        return true;\n    } else if (!g_pserver->fActiveReplica && (listLength(g_pserver->masters) == 1)) {\n        redisMaster *mi = (redisMaster*)listNodeValue(listFirst(g_pserver->masters));\n        if (mi->isActive)\n            return true;\n    }\n    return false;\n}\n\nint hash_spin_worker() {\n    auto ctl = serverTL->rehashCtl;\n    return dictRehashSomeAsync(ctl, 1);\n}\n\n/* This function handles 'background' operations we are required to do\n * incrementally in Redis databases, such as active key expiring, resizing,\n * rehashing. */\nvoid databasesCron(bool fMainThread) {\n    serverAssert(GlobalLocksAcquired());\n\n    if (fMainThread) {\n        /* Expire keys by random sampling. Not required for slaves\n        * as master will synthesize DELs for us. */\n        if (g_pserver->active_expire_enabled) {\n            if (expireOwnKeys()) {\n                activeExpireCycle(ACTIVE_EXPIRE_CYCLE_SLOW);\n            } else {\n                expireSlaveKeys();\n            }\n        }\n\n        /* Defrag keys gradually. */\n        activeDefragCycle();\n    }\n\n    /* Perform hash tables rehashing if needed, but only if there are no\n     * other processes saving the DB on disk. Otherwise rehashing is bad\n     * as will cause a lot of copy-on-write of memory pages. */\n    if (!hasActiveChildProcess()) {\n        /* We use global counters so if we stop the computation at a given\n         * DB we'll be able to start from the successive in the next\n         * cron loop iteration. */\n        static unsigned int resize_db = 0;\n        static unsigned int rehash_db = 0;\n        static int rehashes_per_ms;\n        static int async_rehashes;\n        int dbs_per_call = CRON_DBS_PER_CALL;\n        int j;\n\n        /* Don't test more DBs than we have. */\n        if (dbs_per_call > cserver.dbnum) dbs_per_call = cserver.dbnum;\n\n        if (fMainThread) {\n            /* Resize */\n            for (j = 0; j < dbs_per_call; j++) {\n                tryResizeHashTables(resize_db % cserver.dbnum);\n                resize_db++;\n            }\n        }\n\n        /* Rehash */\n        if (g_pserver->activerehashing) {\n            for (j = 0; j < dbs_per_call; j++) {\n                if (serverTL->rehashCtl != nullptr) {\n                    if (!serverTL->rehashCtl->done.load(std::memory_order_relaxed)) {\n                        aeReleaseLock();\n                        if (dictRehashSomeAsync(serverTL->rehashCtl, rehashes_per_ms)) {\n                            aeAcquireLock();\n                            break;\n                        }\n                        aeAcquireLock();\n                    }\n\n                    if (serverTL->rehashCtl->done.load(std::memory_order_relaxed)) {\n                        dictCompleteRehashAsync(serverTL->rehashCtl, true /*fFree*/);\n                        serverTL->rehashCtl = nullptr;\n                    }\n                }\n\n                serverAssert(serverTL->rehashCtl == nullptr);\n                ::dict *dict = g_pserver->db[rehash_db]->dictUnsafeKeyOnly();\n                /* Are we async rehashing? And if so is it time to re-calibrate? */\n                /* The recalibration limit is a prime number to ensure balancing across threads */\n                if (g_pserver->enable_async_rehash && rehashes_per_ms > 0 && async_rehashes < 131 && !cserver.active_defrag_enabled && cserver.cthreads > 1 && dictSize(dict) > 2048 && dictIsRehashing(dict) && !g_pserver->loading && aeLockContention() > 1) {\n                    serverTL->rehashCtl = dictRehashAsyncStart(dict, rehashes_per_ms * ((1000 / g_pserver->hz) / 10));  // Estimate 10% CPU time spent in lock contention\n                    if (serverTL->rehashCtl)\n                        ++async_rehashes;\n                }\n                if (serverTL->rehashCtl)\n                    break;\n\n                // Before starting anything new, can we end the rehash of a blocked thread?\n                while (dict->asyncdata != nullptr) {\n                    auto asyncdata = dict->asyncdata;\n                    if (asyncdata->done) {\n                        dictCompleteRehashAsync(asyncdata, false /*fFree*/);    // Don't free because we don't own the pointer\n                        serverAssert(dict->asyncdata != asyncdata);\n                    } else {\n                        break;\n                    }\n                }\n\n                if (dict->asyncdata)\n                    break;\n\n                rehashes_per_ms = g_pserver->db[rehash_db]->incrementallyRehash();\n                async_rehashes = 0;\n                if (rehashes_per_ms > 0) {\n                    /* If the function did some work, stop here, we'll do\n                    * more at the next cron loop. */\n                    if (!cserver.active_defrag_enabled) {\n                        serverLog(LL_VERBOSE, \"Calibrated rehashes per ms: %d\", rehashes_per_ms);\n                    }\n                    break;\n                } else if (dict->asyncdata == nullptr) {\n                    /* If this db didn't need rehash and we have none in flight, we'll try the next one. */\n                    rehash_db++;\n                    rehash_db %= cserver.dbnum;\n                }\n            }\n        }\n    }\n\n    if (serverTL->rehashCtl) {\n        setAeLockSetThreadSpinWorker(hash_spin_worker);\n    } else {\n        setAeLockSetThreadSpinWorker(nullptr);\n    }\n}\n\n/* We take a cached value of the unix time in the global state because with\n * virtual memory and aging there is to store the current time in objects at\n * every object access, and accuracy is not needed. To access a global var is\n * a lot faster than calling time(NULL).\n *\n * This function should be fast because it is called at every command execution\n * in call(), so it is possible to decide if to update the daylight saving\n * info or not using the 'update_daylight_info' argument. Normally we update\n * such info only when calling this function from serverCron() but not when\n * calling it from call(). */\nvoid updateCachedTime() {\n    long long t = ustime();\n    __atomic_store(&g_pserver->ustime, &t, __ATOMIC_RELAXED);\n    t /= 1000;\n    __atomic_store(&g_pserver->mstime, &t, __ATOMIC_RELAXED);\n    t /= 1000;\n    g_pserver->unixtime = t;\n\n    /* To get information about daylight saving time, we need to call\n     * localtime_r and cache the result. However calling localtime_r in this\n     * context is safe since we will never fork() while here, in the main\n     * thread. The logging function will call a thread safe version of\n     * localtime that has no locks. */\n    struct tm tm;\n    time_t ut = g_pserver->unixtime;\n    localtime_r(&ut,&tm);\n    __atomic_store(&g_pserver->daylight_active, &tm.tm_isdst, __ATOMIC_RELAXED);\n}\n\nvoid checkChildrenDone(void) {\n    int statloc = 0;\n    pid_t pid;\n\n    if (g_pserver->FRdbSaveInProgress() && !cserver.fForkBgSave)\n    {\n        void *rval = nullptr;\n        int err = EAGAIN;\n        if (!g_pserver->rdbThreadVars.fDone || (err = pthread_join(g_pserver->rdbThreadVars.rdb_child_thread, &rval)))\n        {\n            if (err != EBUSY && err != EAGAIN)\n                serverLog(LL_WARNING, \"Error joining the background RDB save thread: %s\\n\", strerror(errno));\n        }\n        else\n        {\n            int exitcode = (int)reinterpret_cast<ptrdiff_t>(rval);\n            backgroundSaveDoneHandler(exitcode,g_pserver->rdbThreadVars.fRdbThreadCancel);\n            g_pserver->rdbThreadVars.fRdbThreadCancel = false;\n            g_pserver->rdbThreadVars.fDone = false;\n            if (exitcode == 0) receiveChildInfo();\n            closeChildInfoPipe();\n        }\n    }\n    else if ((pid = waitpid(-1, &statloc, WNOHANG)) != 0) {\n        int exitcode = WIFEXITED(statloc) ? WEXITSTATUS(statloc) : -1;\n        int bysignal = 0;\n\n        if (WIFSIGNALED(statloc)) bysignal = WTERMSIG(statloc);\n\n        /* sigKillChildHandler catches the signal and calls exit(), but we\n         * must make sure not to flag lastbgsave_status, etc incorrectly.\n         * We could directly terminate the child process via SIGUSR1\n         * without handling it */\n        if (exitcode == SERVER_CHILD_NOERROR_RETVAL) {\n            bysignal = SIGUSR1;\n            exitcode = 1;\n        }\n\n        if (pid == -1) {\n            serverLog(LL_WARNING,\"waitpid() returned an error: %s. \"\n                \"child_type: %s, child_pid = %d\",\n                strerror(errno),\n                strChildType(g_pserver->child_type),\n                (int) g_pserver->child_pid);\n        } else if (pid == g_pserver->child_pid) {\n            if (g_pserver->child_type == CHILD_TYPE_RDB) {\n                backgroundSaveDoneHandler(exitcode, bysignal);\n            } else if (g_pserver->child_type == CHILD_TYPE_AOF) {\n                backgroundRewriteDoneHandler(exitcode, bysignal);\n            } else if (g_pserver->child_type == CHILD_TYPE_MODULE) {\n                ModuleForkDoneHandler(exitcode, bysignal);\n            } else {\n                serverPanic(\"Unknown child type %d for child pid %d\", g_pserver->child_type, g_pserver->child_pid);\n                exit(1);\n            }\n            if (!bysignal && exitcode == 0) receiveChildInfo();\n            resetChildState();\n        } else {\n            if (!ldbRemoveChild(pid)) {\n                serverLog(LL_WARNING,\n                          \"Warning, detected child with unmatched pid: %ld\",\n                          (long) pid);\n            }\n        }\n\n        /* start any pending forks immediately. */\n        replicationStartPendingFork();\n    }\n}\n\n/* Called from serverCron and loadingCron to update cached memory metrics. */\nvoid cronUpdateMemoryStats() {\n    /* Record the max memory used since the server was started. */\n    if (zmalloc_used_memory() > g_pserver->stat_peak_memory)\n        g_pserver->stat_peak_memory = zmalloc_used_memory();\n\n    run_with_period(100) {\n        /* Sample the RSS and other metrics here since this is a relatively slow call.\n         * We must sample the zmalloc_used at the same time we take the rss, otherwise\n         * the frag ratio calculate may be off (ratio of two samples at different times) */\n        g_pserver->cron_malloc_stats.process_rss = zmalloc_get_rss();\n        g_pserver->cron_malloc_stats.zmalloc_used = zmalloc_used_memory();\n        /* Sampling the allocator info can be slow too.\n         * The fragmentation ratio it'll show is potentially more accurate\n         * it excludes other RSS pages such as: shared libraries, LUA and other non-zmalloc\n         * allocations, and allocator reserved pages that can be pursed (all not actual frag) */\n        zmalloc_get_allocator_info(&g_pserver->cron_malloc_stats.allocator_allocated,\n                                   &g_pserver->cron_malloc_stats.allocator_active,\n                                   &g_pserver->cron_malloc_stats.allocator_resident);\n        /* in case the allocator isn't providing these stats, fake them so that\n         * fragmentation info still shows some (inaccurate metrics) */\n        if (!g_pserver->cron_malloc_stats.allocator_resident) {\n            /* LUA memory isn't part of zmalloc_used, but it is part of the process RSS,\n             * so we must deduct it in order to be able to calculate correct\n             * \"allocator fragmentation\" ratio */\n            size_t lua_memory = lua_gc(g_pserver->lua,LUA_GCCOUNT,0)*1024LL;\n            g_pserver->cron_malloc_stats.allocator_resident = g_pserver->cron_malloc_stats.process_rss - lua_memory;\n        }\n        if (!g_pserver->cron_malloc_stats.allocator_active)\n            g_pserver->cron_malloc_stats.allocator_active = g_pserver->cron_malloc_stats.allocator_resident;\n        if (!g_pserver->cron_malloc_stats.allocator_allocated)\n            g_pserver->cron_malloc_stats.allocator_allocated = g_pserver->cron_malloc_stats.zmalloc_used;\n\n        if (g_pserver->force_eviction_percent) {\n            g_pserver->cron_malloc_stats.sys_available = getMemAvailable();\n        }\n    }\n}\n\nstatic std::atomic<bool> s_fFlushInProgress { false };\nvoid flushStorageWeak()\n{\n    bool fExpected = false;\n    if (s_fFlushInProgress.compare_exchange_strong(fExpected, true /* desired */, std::memory_order_seq_cst, std::memory_order_relaxed))\n    {\n        g_pserver->asyncworkqueue->AddWorkFunction([]{\n            aeAcquireLock();\n            mstime_t storage_process_latency;\n            latencyStartMonitor(storage_process_latency);\n            std::vector<redisDb*> vecdb;\n            for (int idb = 0; idb < cserver.dbnum; ++idb) {\n                if (g_pserver->db[idb]->processChanges(true))\n                    vecdb.push_back(g_pserver->db[idb]);\n            }\n            latencyEndMonitor(storage_process_latency);\n            latencyAddSampleIfNeeded(\"storage-process-changes\", storage_process_latency);\n            aeReleaseLock();\n\n            std::vector<const redisDbPersistentDataSnapshot*> vecsnapshotFree;\n            vecsnapshotFree.resize(vecdb.size());\n            for (size_t idb = 0; idb < vecdb.size(); ++idb)\n                vecdb[idb]->commitChanges(&vecsnapshotFree[idb]);\n\n            for (size_t idb = 0; idb < vecsnapshotFree.size(); ++idb) {\n                if (vecsnapshotFree[idb] != nullptr)\n                    vecdb[idb]->endSnapshotAsync(vecsnapshotFree[idb]);\n            }\n            s_fFlushInProgress = false;\n        }, true /* fHiPri */);\n    }\n    else\n    {\n        serverLog(LOG_INFO, \"Missed storage flush due to existing flush still in flight.  Consider increasing storage-weak-flush-period\");\n    }\n}\n\n/* This is our timer interrupt, called g_pserver->hz times per second.\n * Here is where we do a number of things that need to be done asynchronously.\n * For instance:\n *\n * - Active expired keys collection (it is also performed in a lazy way on\n *   lookup).\n * - Software watchdog.\n * - Update some statistic.\n * - Incremental rehashing of the DBs hash tables.\n * - Triggering BGSAVE / AOF rewrite, and handling of terminated children.\n * - Clients timeout of different kinds.\n * - Replication reconnection.\n * - Many more...\n *\n * Everything directly called here will be called g_pserver->hz times per second,\n * so in order to throttle execution of things we want to do less frequently\n * a macro is used: run_with_period(milliseconds) { .... }\n */\n\nvoid unblockChildThreadIfNecessary();\nint serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {\n    int j;\n    UNUSED(eventLoop);\n    UNUSED(id);\n    UNUSED(clientData);\n\n    if (g_pserver->maxmemory && g_pserver->m_pstorageFactory)\n        performEvictions(false);\n\n    /* If another threads unblocked one of our clients, and this thread has been idle\n        then beforeSleep won't have a chance to process the unblocking.  So we also\n        process them here in the cron job to ensure they don't starve.\n    */\n    if (listLength(g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].unblocked_clients))\n    {\n        processUnblockedClients(IDX_EVENT_LOOP_MAIN);\n    }\n        \n    /* Software watchdog: deliver the SIGALRM that will reach the signal\n     * handler if we don't return here fast enough. */\n    if (g_pserver->watchdog_period) watchdogScheduleSignal(g_pserver->watchdog_period);\n\n    g_pserver->hz = g_pserver->config_hz;\n    /* Adapt the g_pserver->hz value to the number of configured clients. If we have\n     * many clients, we want to call serverCron() with an higher frequency. */\n    if (g_pserver->dynamic_hz) {\n        while (listLength(g_pserver->clients) / g_pserver->hz >\n               MAX_CLIENTS_PER_CLOCK_TICK)\n        {\n            g_pserver->hz += g_pserver->hz; // *= 2\n            if (g_pserver->hz > CONFIG_MAX_HZ) {\n                g_pserver->hz = CONFIG_MAX_HZ;\n                break;\n            }\n        }\n    }\n\n    /* A cancelled child thread could be hung waiting for us to read from a pipe */\n    unblockChildThreadIfNecessary();\n\n    run_with_period(100) {\n        long long stat_net_input_bytes, stat_net_output_bytes;\n        stat_net_input_bytes = g_pserver->stat_net_input_bytes.load(std::memory_order_relaxed);\n        stat_net_output_bytes = g_pserver->stat_net_output_bytes.load(std::memory_order_relaxed);\n\n        long long stat_numcommands;\n        __atomic_load(&g_pserver->stat_numcommands, &stat_numcommands, __ATOMIC_RELAXED);\n        trackInstantaneousMetric(STATS_METRIC_COMMAND,stat_numcommands);\n        trackInstantaneousMetric(STATS_METRIC_NET_INPUT,\n                stat_net_input_bytes);\n        trackInstantaneousMetric(STATS_METRIC_NET_OUTPUT,\n                stat_net_output_bytes);\n    }\n\n    /* We have just LRU_BITS bits per object for LRU information.\n     * So we use an (eventually wrapping) LRU clock.\n     *\n     * Note that even if the counter wraps it's not a big problem,\n     * everything will still work but some object will appear younger\n     * to Redis. However for this to happen a given object should never be\n     * touched for all the time needed to the counter to wrap, which is\n     * not likely.\n     *\n     * Note that you can change the resolution altering the\n     * LRU_CLOCK_RESOLUTION define. */\n    g_pserver->lruclock = getLRUClock();\n\n    cronUpdateMemoryStats();\n\n    /* We received a SIGTERM, shutting down here in a safe way, as it is\n     * not ok doing so inside the signal handler. */\n    if (g_pserver->shutdown_asap) {\n        if (prepareForShutdown(SHUTDOWN_NOFLAGS) == C_OK) throw ShutdownException();\n        serverLog(LL_WARNING,\"SIGTERM received but errors trying to shut down the server, check the logs for more information\");\n        g_pserver->shutdown_asap = 0;\n    }\n\n    /* Show some info about non-empty databases */\n    if (cserver.verbosity <= LL_VERBOSE) {\n        run_with_period(5000) {\n            for (j = 0; j < cserver.dbnum; j++) {\n                long long size, used, vkeys;\n\n                size = g_pserver->db[j]->slots();\n                used = g_pserver->db[j]->size();\n                vkeys = g_pserver->db[j]->expireSize();\n                if (used || vkeys) {\n                    serverLog(LL_VERBOSE,\"DB %d: %lld keys (%lld volatile) in %lld slots HT.\",j,used,vkeys,size);\n                }\n            }\n        }\n    }\n\n    /* Show information about connected clients */\n    if (!g_pserver->sentinel_mode) {\n        run_with_period(5000) {\n            serverLog(LL_DEBUG,\n                \"%lu clients connected (%lu replicas), %zu bytes in use\",\n                listLength(g_pserver->clients)-listLength(g_pserver->slaves),\n                listLength(g_pserver->slaves),\n                zmalloc_used_memory());\n        }\n    }\n\n    /* We need to do a few operations on clients asynchronously. */\n    clientsCron(IDX_EVENT_LOOP_MAIN);\n\n    /* Handle background operations on Redis databases. */\n    databasesCron(true /* fMainThread */);\n\n    /* Start a scheduled AOF rewrite if this was requested by the user while\n     * a BGSAVE was in progress. */\n    if (!hasActiveChildProcessOrBGSave() &&\n        g_pserver->aof_rewrite_scheduled)\n    {\n        rewriteAppendOnlyFileBackground();\n    }\n\n    /* Check if a background saving or AOF rewrite in progress terminated. */\n    if (hasActiveChildProcessOrBGSave() || ldbPendingChildren())\n    {\n        run_with_period(1000) receiveChildInfo();\n        checkChildrenDone();\n    } else {\n        /* If there is not a background saving/rewrite in progress check if\n         * we have to save/rewrite now. */\n        for (j = 0; j < g_pserver->saveparamslen; j++) {\n            struct saveparam *sp = g_pserver->saveparams+j;\n\n            /* Save if we reached the given amount of changes,\n             * the given amount of seconds, and if the latest bgsave was\n             * successful or if, in case of an error, at least\n             * CONFIG_BGSAVE_RETRY_DELAY seconds already elapsed. */\n            if (g_pserver->dirty >= sp->changes &&\n                g_pserver->unixtime-g_pserver->lastsave > sp->seconds &&\n                (g_pserver->unixtime-g_pserver->lastbgsave_try >\n                 CONFIG_BGSAVE_RETRY_DELAY ||\n                 g_pserver->lastbgsave_status == C_OK))\n            {\n                // Ensure rehashing is complete\n                bool fRehashInProgress = false;\n                if (g_pserver->activerehashing) {\n                    for (int idb = 0; idb < cserver.dbnum && !fRehashInProgress; ++idb) {\n                        if (g_pserver->db[idb]->FRehashing())\n                            fRehashInProgress = true;\n                    }\n                }\n\n                if (!fRehashInProgress) {\n                    serverLog(LL_NOTICE,\"%d changes in %d seconds. Saving...\",\n                        sp->changes, (int)sp->seconds);\n                    rdbSaveInfo rsi, *rsiptr;\n                    rsiptr = rdbPopulateSaveInfo(&rsi);\n                    rdbSaveBackground(rsiptr);\n                }\n                break;\n            }\n        }\n\n        /* Trigger an AOF rewrite if needed. */\n        if (g_pserver->aof_state == AOF_ON &&\n            !hasActiveChildProcessOrBGSave() &&\n            g_pserver->aof_rewrite_perc &&\n            g_pserver->aof_current_size > g_pserver->aof_rewrite_min_size)\n        {\n            long long base = g_pserver->aof_rewrite_base_size ?\n                g_pserver->aof_rewrite_base_size : 1;\n            long long growth = (g_pserver->aof_current_size*100/base) - 100;\n            if (growth >= g_pserver->aof_rewrite_perc) {\n                serverLog(LL_NOTICE,\"Starting automatic rewriting of AOF on %lld%% growth\",growth);\n                rewriteAppendOnlyFileBackground();\n            }\n        }\n    }\n    /* Just for the sake of defensive programming, to avoid forgeting to\n     * call this function when need. */\n    updateDictResizePolicy();\n\n\n    /* AOF postponed flush: Try at every cron cycle if the slow fsync\n     * completed. */\n    if (g_pserver->aof_state == AOF_ON && g_pserver->aof_flush_postponed_start)\n        flushAppendOnlyFile(0);\n\n    /* AOF write errors: in this case we have a buffer to flush as well and\n     * clear the AOF error in case of success to make the DB writable again,\n     * however to try every second is enough in case of 'hz' is set to\n     * a higher frequency. */\n    run_with_period(1000) {\n        if (g_pserver->aof_state == AOF_ON && g_pserver->aof_last_write_status == C_ERR)\n            flushAppendOnlyFile(0);\n    }\n\n    /* Clear the paused clients state if needed. */\n    checkClientPauseTimeoutAndReturnIfPaused();\n\n    /* Replication cron function -- used to reconnect to master,\n     * detect transfer failures, start background RDB transfers and so forth. \n     * \n     * If Redis is trying to failover then run the replication cron faster so\n     * progress on the handshake happens more quickly. */\n    if (g_pserver->failover_state != NO_FAILOVER) {\n        run_with_period(100) replicationCron();\n    } else {\n        run_with_period(1000) replicationCron();\n    }\n\n    /* Run the Redis Cluster cron. */\n    run_with_period(100) {\n        if (g_pserver->cluster_enabled) clusterCron();\n    }\n\n    /* Run the Sentinel timer if we are in sentinel mode. */\n    if (g_pserver->sentinel_mode) sentinelTimer();\n\n    /* Cleanup expired MIGRATE cached sockets. */\n    run_with_period(1000) {\n        migrateCloseTimedoutSockets();\n    }\n\n    /* Check for CPU Overload */\n    run_with_period(10'000) {\n        g_pserver->is_overloaded = false;\n        g_pserver->overload_closed_clients = 0;\n        static clock_t last = 0;\n        if (g_pserver->overload_protect_threshold > 0) {\n            clock_t cur = clock();\n            double perc = static_cast<double>(cur - last) / (CLOCKS_PER_SEC*10);\n            perc /= cserver.cthreads;\n            perc *= 100.0;\n            serverLog(LL_WARNING, \"CPU Used: %.2f\", perc);\n            if (perc > g_pserver->overload_protect_threshold) {\n                serverLog(LL_WARNING, \"\\tWARNING: CPU overload detected.\");\n                g_pserver->is_overloaded = true;\n            }\n            last = cur;\n        }\n    }\n\n    /* Tune the fastlock to CPU load */\n    run_with_period(30000) {\n        /* Tune the fastlock to CPU load */\n        fastlock_auto_adjust_waits();\n    }\n\n    /* Reload the TLS cert if neccessary. This effectively rotates the \n     * cert if a change has been made on disk, but the KeyDB server hasn't\n     * been notified. */\n    run_with_period(1000){\n        tlsReload();\n    }\n\n    /* Resize tracking keys table if needed. This is also done at every\n     * command execution, but we want to be sure that if the last command\n     * executed changes the value via CONFIG SET, the server will perform\n     * the operation even if completely idle. */\n    if (g_pserver->tracking_clients) trackingLimitUsedSlots();\n\n    /* Start a scheduled BGSAVE if the corresponding flag is set. This is\n     * useful when we are forced to postpone a BGSAVE because an AOF\n     * rewrite is in progress.\n     *\n     * Note: this code must be after the replicationCron() call above so\n     * make sure when refactoring this file to keep this order. This is useful\n     * because we want to give priority to RDB savings for replication. */\n    if (!hasActiveChildProcessOrBGSave() &&\n        g_pserver->rdb_bgsave_scheduled &&\n        (g_pserver->unixtime-g_pserver->lastbgsave_try > CONFIG_BGSAVE_RETRY_DELAY ||\n         g_pserver->lastbgsave_status == C_OK))\n    {\n        rdbSaveInfo rsi, *rsiptr;\n        rsiptr = rdbPopulateSaveInfo(&rsi);\n        if (rdbSaveBackground(rsiptr) == C_OK)\n            g_pserver->rdb_bgsave_scheduled = 0;\n    }\n\n    if (cserver.storage_memory_model == STORAGE_WRITEBACK && g_pserver->m_pstorageFactory && !g_pserver->loading) {\n        run_with_period(g_pserver->storage_flush_period) {\n            flushStorageWeak();\n        }\n    }\n\n    /* Fire the cron loop modules event. */\n    RedisModuleCronLoopV1 ei = {REDISMODULE_CRON_LOOP_VERSION,g_pserver->hz};\n    moduleFireServerEvent(REDISMODULE_EVENT_CRON_LOOP,\n                          0,\n                          &ei);\n\n\n    /* CRON functions may trigger async writes, so do this last */\n    ProcessPendingAsyncWrites();\n\n    // Measure lock contention from a different thread to be more accurate\n    g_pserver->asyncworkqueue->AddWorkFunction([]{\n        g_pserver->rglockSamples[g_pserver->ilockRingHead] = (uint16_t)aeLockContention();\n        ++g_pserver->ilockRingHead;\n        if (g_pserver->ilockRingHead >= redisServer::s_lockContentionSamples)\n            g_pserver->ilockRingHead = 0;\n    });\n\n    run_with_period(10) {\n        if (!g_pserver->garbageCollector.empty()) {\n            // Server threads don't free the GC, but if we don't have a\n            //  a bgsave or some other async task then we'll hold onto the\n            //  data for too long\n            g_pserver->asyncworkqueue->AddWorkFunction([]{\n                auto epoch = g_pserver->garbageCollector.startEpoch();\n                g_pserver->garbageCollector.endEpoch(epoch);\n            });\n        }\n    }\n\n    if (g_pserver->soft_shutdown) {\n        /* Loop through our clients list and see if there are any active clients */\n        listIter li;\n        listNode *ln;\n        listRewind(g_pserver->clients, &li);\n        bool fActiveClient = false;\n        while ((ln = listNext(&li)) && !fActiveClient) {\n            client *c = (client*)listNodeValue(ln);\n            if (c->flags & CLIENT_IGNORE_SOFT_SHUTDOWN)\n                continue;\n            fActiveClient = true;\n        }\n        if (!fActiveClient) {\n            if (prepareForShutdown(SHUTDOWN_NOFLAGS) == C_OK) {\n                serverLog(LL_WARNING, \"All active clients have disconnected while a soft shutdown is pending.  Shutting down now.\");\n                throw ShutdownException();\n            }\n        }\n    }\n\n    g_pserver->cronloops++;\n    return 1000/g_pserver->hz;\n}\n\n// serverCron for worker threads other than the main thread\nint serverCronLite(struct aeEventLoop *eventLoop, long long id, void *clientData)\n{\n    UNUSED(id);\n    UNUSED(clientData);\n\n    if (g_pserver->maxmemory && g_pserver->m_pstorageFactory)\n        performEvictions(false);\n\n    int iel = ielFromEventLoop(eventLoop);\n    serverAssert(iel != IDX_EVENT_LOOP_MAIN);\n\n    /* If another threads unblocked one of our clients, and this thread has been idle\n        then beforeSleep won't have a chance to process the unblocking.  So we also\n        process them here in the cron job to ensure they don't starve.\n    */\n    if (listLength(g_pserver->rgthreadvar[iel].unblocked_clients))\n    {\n        processUnblockedClients(iel);\n    }\n\n    /* Handle background operations on Redis databases. */\n    databasesCron(false /* fMainThread */);\n\n    /* Unpause clients if enough time has elapsed */\n    checkClientPauseTimeoutAndReturnIfPaused();\n    \n    ProcessPendingAsyncWrites();    // A bug but leave for now, events should clean up after themselves\n    clientsCron(iel);\n\n    return 1000/g_pserver->hz;\n}\n\nextern \"C\" void asyncFreeDictTable(dictEntry **de)\n{\n    if (de == nullptr || serverTL == nullptr || serverTL->gcEpoch.isReset()) {\n        zfree(de);\n    } else {\n        g_pserver->garbageCollector.enqueueCPtr(serverTL->gcEpoch, de);\n    }\n}\n\nvoid blockingOperationStarts() {\n    if(!g_pserver->blocking_op_nesting++){\n        __atomic_load(&g_pserver->mstime, &g_pserver->blocked_last_cron, __ATOMIC_ACQUIRE);\n    }\n}\n\nvoid blockingOperationEnds() {\n    if(!(--g_pserver->blocking_op_nesting)){\n        g_pserver->blocked_last_cron = 0;\n    }\n}\n\n/* This function fill in the role of serverCron during RDB or AOF loading, and\n * also during blocked scripts.\n * It attempts to do its duties at a similar rate as the configured g_pserver->hz,\n * and updates cronloops variable so that similarly to serverCron, the\n * run_with_period can be used. */\nvoid whileBlockedCron() {\n    /* Here we may want to perform some cron jobs (normally done g_pserver->hz times\n     * per second). */\n\n    /* Since this function depends on a call to blockingOperationStarts, let's\n     * make sure it was done. */\n    serverAssert(g_pserver->blocked_last_cron);\n\n    /* In case we where called too soon, leave right away. This way one time\n     * jobs after the loop below don't need an if. and we don't bother to start\n     * latency monitor if this function is called too often. */\n    if (g_pserver->blocked_last_cron >= g_pserver->mstime)\n        return;\n\n    mstime_t latency;\n    latencyStartMonitor(latency);\n\n    /* In some cases we may be called with big intervals, so we may need to do\n     * extra work here. This is because some of the functions in serverCron rely\n     * on the fact that it is performed every 10 ms or so. For instance, if\n     * activeDefragCycle needs to utilize 25% cpu, it will utilize 2.5ms, so we\n     * need to call it multiple times. */\n    long hz_ms = 1000/g_pserver->hz;\n    while (g_pserver->blocked_last_cron < g_pserver->mstime) {\n\n        /* Defrag keys gradually. */\n        activeDefragCycle();\n\n        g_pserver->blocked_last_cron += hz_ms;\n\n        /* Increment cronloop so that run_with_period works. */\n        g_pserver->cronloops++;\n    }\n\n    /* Other cron jobs do not need to be done in a loop. No need to check\n     * g_pserver->blocked_last_cron since we have an early exit at the top. */\n\n    /* Update memory stats during loading (excluding blocked scripts) */\n    if (g_pserver->loading) cronUpdateMemoryStats();\n\n    latencyEndMonitor(latency);\n    latencyAddSampleIfNeeded(\"while-blocked-cron\",latency);\n}\n\nextern __thread int ProcessingEventsWhileBlocked;\n\n/* This function gets called every time Redis is entering the\n * main loop of the event driven library, that is, before to sleep\n * for ready file descriptors.\n *\n * Note: This function is (currently) called from two functions:\n * 1. aeMain - The main server loop\n * 2. processEventsWhileBlocked - Process clients during RDB/AOF load\n *\n * If it was called from processEventsWhileBlocked we don't want\n * to perform all actions (For example, we don't want to expire\n * keys), but we do need to perform some actions.\n *\n * The most important is freeClientsInAsyncFreeQueue but we also\n * call some other low-risk functions. */\nvoid beforeSleep(struct aeEventLoop *eventLoop) {\n    AeLocker locker;\n    int iel = ielFromEventLoop(eventLoop);\n\n    tlsProcessPendingData();\n\n    locker.arm();\n\n    /* end any snapshots created by fast async commands */\n    for (int idb = 0; idb < cserver.dbnum; ++idb) {\n        if (serverTL->rgdbSnapshot[idb] != nullptr && serverTL->rgdbSnapshot[idb]->FStale()) {\n            g_pserver->db[idb]->endSnapshot(serverTL->rgdbSnapshot[idb]);\n            serverTL->rgdbSnapshot[idb] = nullptr;\n        }\n    }\n\n    size_t zmalloc_used = zmalloc_used_memory();\n    if (zmalloc_used > g_pserver->stat_peak_memory)\n        g_pserver->stat_peak_memory = zmalloc_used;\n    \n    serverAssert(g_pserver->repl_batch_offStart < 0);\n\n    runAndPropogateToReplicas(processClients);\n\n    /* Just call a subset of vital functions in case we are re-entering\n     * the event loop from processEventsWhileBlocked(). Note that in this\n     * case we keep track of the number of events we are processing, since\n     * processEventsWhileBlocked() wants to stop ASAP if there are no longer\n     * events to handle. */\n    if (ProcessingEventsWhileBlocked) {\n        uint64_t processed = 0;\n        int aof_state = g_pserver->aof_state;\n        locker.disarm();\n        processed += handleClientsWithPendingWrites(iel, aof_state);\n        locker.arm();\n        processed += freeClientsInAsyncFreeQueue(iel);\n        g_pserver->events_processed_while_blocked += processed;\n        return;\n    }\n\n    /* Handle precise timeouts of blocked clients. */\n    handleBlockedClientsTimeout();\n\n    /* If tls still has pending unread data don't sleep at all. */\n    aeSetDontWait(eventLoop, tlsHasPendingData());\n\n    /* Call the Redis Cluster before sleep function. Note that this function\n     * may change the state of Redis Cluster (from ok to fail or vice versa),\n     * so it's a good idea to call it before serving the unblocked clients\n     * later in this function. */\n    if (g_pserver->cluster_enabled) clusterBeforeSleep();\n\n    /* Run a fast expire cycle (the called function will return\n     * ASAP if a fast cycle is not needed). */\n    if (g_pserver->active_expire_enabled && (listLength(g_pserver->masters) == 0 || g_pserver->fActiveReplica))\n        activeExpireCycle(ACTIVE_EXPIRE_CYCLE_FAST);\n\n    /* Unblock all the clients blocked for synchronous replication\n     * in WAIT. */\n    if (listLength(g_pserver->clients_waiting_acks))\n        processClientsWaitingReplicas();\n\n    /* Check if there are clients unblocked by modules that implement\n     * blocking commands. */\n    if (moduleCount()) moduleHandleBlockedClients(ielFromEventLoop(eventLoop));\n\n    /* Try to process pending commands for clients that were just unblocked. */\n    if (listLength(g_pserver->rgthreadvar[iel].unblocked_clients))\n    {\n        processUnblockedClients(iel);\n    }\n\n    /* Send all the slaves an ACK request if at least one client blocked\n     * during the previous event loop iteration. Note that we do this after\n     * processUnblockedClients(), so if there are multiple pipelined WAITs\n     * and the just unblocked WAIT gets blocked again, we don't have to wait\n     * a server cron cycle in absence of other event loop events. See #6623.\n     * \n     * We also don't send the ACKs while clients are paused, since it can\n     * increment the replication backlog, they'll be sent after the pause\n     * if we are still the master. */\n    if (g_pserver->get_ack_from_slaves && !checkClientPauseTimeoutAndReturnIfPaused()) {\n        robj *argv[3];\n\n        argv[0] = shared.replconf;\n        argv[1] = shared.getack;\n        argv[2] = shared.special_asterick; /* Not used argument. */\n        replicationFeedSlaves(g_pserver->slaves, g_pserver->replicaseldb, argv, 3);\n        g_pserver->get_ack_from_slaves = 0;\n    }\n\n    /* We may have recieved updates from clients about their current offset. NOTE:\n     * this can't be done where the ACK is recieved since failover will disconnect \n     * our clients. */\n    if (iel == IDX_EVENT_LOOP_MAIN)\n        updateFailoverStatus();\n\n    /* Send the invalidation messages to clients participating to the\n     * client side caching protocol in broadcasting (BCAST) mode. */\n    trackingBroadcastInvalidationMessages();\n\n    /* Write the AOF buffer on disk */\n    if (g_pserver->aof_state == AOF_ON)\n        flushAppendOnlyFile(0);\n\n    static thread_local bool fFirstRun = true;\n    // note: we also copy the DB pointer in case a DB swap is done while the lock is released\n    std::vector<redisDb*> vecdb;    // note we cache the database pointer in case a dbswap is done while the lock is released\n    if (cserver.storage_memory_model == STORAGE_WRITETHROUGH && !g_pserver->loading)\n    {\n        if (!fFirstRun) {\n            mstime_t storage_process_latency;\n            latencyStartMonitor(storage_process_latency);\n            for (int idb = 0; idb < cserver.dbnum; ++idb) {\n                if (g_pserver->db[idb]->processChanges(false))\n                    vecdb.push_back(g_pserver->db[idb]);\n            }\n            latencyEndMonitor(storage_process_latency);\n            latencyAddSampleIfNeeded(\"storage-process-changes\", storage_process_latency);\n        } else {\n            fFirstRun = false;\n        }\n    }\n\n    int aof_state = g_pserver->aof_state;\n\n    mstime_t commit_latency;\n    latencyStartMonitor(commit_latency);\n    if (g_pserver->m_pstorageFactory != nullptr)\n    {\n        locker.disarm();\n        for (redisDb *db : vecdb)\n            db->commitChanges();\n        locker.arm();\n    }\n    latencyEndMonitor(commit_latency);\n    latencyAddSampleIfNeeded(\"storage-commit\", commit_latency);\n\n    /* We try to handle writes at the end so we don't have to reacquire the lock,\n        but if there is a pending async close we need to ensure the writes happen\n        first so perform it here */\n    bool fSentReplies = false;\n\n    std::unique_lock<fastlock> ul(g_lockasyncfree);\n    if (listLength(g_pserver->clients_to_close)) {\n        ul.unlock();\n        locker.disarm();\n        handleClientsWithPendingWrites(iel, aof_state);\n        locker.arm();\n        fSentReplies = true;\n    } else {\n        ul.unlock();\n    }\n    \n    if (!serverTL->gcEpoch.isReset())\n        g_pserver->garbageCollector.endEpoch(serverTL->gcEpoch, true /*fNoFree*/);\n    serverTL->gcEpoch.reset();\n\n    /* Close clients that need to be closed asynchronous */\n    freeClientsInAsyncFreeQueue(iel);\n\n    if (!serverTL->gcEpoch.isReset())\n        g_pserver->garbageCollector.endEpoch(serverTL->gcEpoch, true /*fNoFree*/);\n    serverTL->gcEpoch.reset();\n\n    /* Try to process blocked clients every once in while. Example: A module\n     * calls RM_SignalKeyAsReady from within a timer callback (So we don't\n     * visit processCommand() at all). */\n    handleClientsBlockedOnKeys();\n\n    /* Before we are going to sleep, let the threads access the dataset by\n     * releasing the GIL. Redis main thread will not touch anything at this\n     * time. */\n    serverAssert(g_pserver->repl_batch_offStart < 0);\n    locker.disarm();\n    if (!fSentReplies)\n        handleClientsWithPendingWrites(iel, aof_state);\n\n    aeThreadOffline();\n    // Scope lock_guard\n    {\n        std::unique_lock<fastlock> lock(time_thread_lock);\n        sleeping_threads++;\n        serverAssert(sleeping_threads <= cserver.cthreads);\n    }\n\n    if (!g_pserver->garbageCollector.empty()) {\n        // Server threads don't free the GC, but if we don't have a\n        //  a bgsave or some other async task then we'll hold onto the\n        //  data for too long\n        g_pserver->asyncworkqueue->AddWorkFunction([]{\n            auto epoch = g_pserver->garbageCollector.startEpoch();\n            g_pserver->garbageCollector.endEpoch(epoch);\n        }, true /*fHiPri*/);\n    }\n    \n    /* Determine whether the modules are enabled before sleeping, and use that result\n       both here, and after wakeup to avoid double acquire or release of the GIL */\n    serverTL->modulesEnabledThisAeLoop = !!moduleCount();\n    if (serverTL->modulesEnabledThisAeLoop) moduleReleaseGIL(TRUE /*fServerThread*/);\n\n    /* Do NOT add anything below moduleReleaseGIL !!! */\n}\n\n/* This function is called immediately after the event loop multiplexing\n * API returned, and the control is going to soon return to Redis by invoking\n * the different events callbacks. */\nvoid afterSleep(struct aeEventLoop *eventLoop) {\n    UNUSED(eventLoop);\n    /* Do NOT add anything above moduleAcquireGIL !!! */\n\n    /* Aquire the modules GIL so that their threads won't touch anything. \n       Don't check here that modules are enabled, rather use the result from beforeSleep\n       Otherwise you may double acquire the GIL and cause deadlocks in the module */\n    if (!ProcessingEventsWhileBlocked) {\n        if (serverTL->modulesEnabledThisAeLoop) moduleAcquireGIL(TRUE /*fServerThread*/);\n        aeThreadOnline();\n        wakeTimeThread();\n\n        serverAssert(serverTL->gcEpoch.isReset());\n        serverTL->gcEpoch = g_pserver->garbageCollector.startEpoch();\n\n        aeAcquireLock();\n        for (int idb = 0; idb < cserver.dbnum; ++idb)\n            g_pserver->db[idb]->trackChanges(false);\n        aeReleaseLock();\n\n        serverTL->disable_async_commands = false;\n    }\n}\n\n/* =========================== Server initialization ======================== */\n\nvoid createSharedObjects(void) {\n    int j;\n\n    /* Shared command responses */\n    shared.crlf = makeObjectShared(createObject(OBJ_STRING,sdsnew(\"\\r\\n\")));\n    shared.ok = makeObjectShared(createObject(OBJ_STRING,sdsnew(\"+OK\\r\\n\")));\n    shared.emptybulk = makeObjectShared(createObject(OBJ_STRING,sdsnew(\"$0\\r\\n\\r\\n\")));\n    shared.czero = makeObjectShared(createObject(OBJ_STRING,sdsnew(\":0\\r\\n\")));\n    shared.cone = makeObjectShared(createObject(OBJ_STRING,sdsnew(\":1\\r\\n\")));\n    shared.emptyarray = makeObjectShared(createObject(OBJ_STRING,sdsnew(\"*0\\r\\n\")));\n    shared.pong = makeObjectShared(createObject(OBJ_STRING,sdsnew(\"+PONG\\r\\n\")));\n    shared.queued = makeObjectShared(createObject(OBJ_STRING,sdsnew(\"+QUEUED\\r\\n\")));\n    shared.emptyscan = makeObjectShared(createObject(OBJ_STRING,sdsnew(\"*2\\r\\n$1\\r\\n0\\r\\n*0\\r\\n\")));\n    shared.space = makeObjectShared(createObject(OBJ_STRING,sdsnew(\" \")));\n    shared.colon = makeObjectShared(createObject(OBJ_STRING,sdsnew(\":\")));\n    shared.plus = makeObjectShared(createObject(OBJ_STRING,sdsnew(\"+\")));\n    shared.nullbulk = makeObjectShared(createObject(OBJ_STRING,sdsnew(\"$0\\r\\n\\r\\n\")));\n    \n    /* Shared command error responses */   \n    shared.wrongtypeerr = makeObjectShared(createObject(OBJ_STRING,sdsnew(\n        \"-WRONGTYPE Operation against a key holding the wrong kind of value\\r\\n\")));\n    shared.err = makeObjectShared(createObject(OBJ_STRING,sdsnew(\"-ERR\\r\\n\")));\n    shared.nokeyerr = makeObjectShared(createObject(OBJ_STRING,sdsnew(\n        \"-ERR no such key\\r\\n\")));\n    shared.syntaxerr = makeObjectShared(createObject(OBJ_STRING,sdsnew(\n        \"-ERR syntax error\\r\\n\")));\n    shared.sameobjecterr = makeObjectShared(createObject(OBJ_STRING,sdsnew(\n        \"-ERR source and destination objects are the same\\r\\n\")));\n    shared.outofrangeerr = makeObjectShared(createObject(OBJ_STRING,sdsnew(\n        \"-ERR index out of range\\r\\n\")));\n    shared.noscripterr = makeObjectShared(createObject(OBJ_STRING,sdsnew(\n        \"-NOSCRIPT No matching script. Please use EVAL.\\r\\n\")));\n    shared.loadingerr = makeObjectShared(createObject(OBJ_STRING,sdsnew(\n        \"-LOADING KeyDB is loading the dataset in memory\\r\\n\")));\n    shared.slowscripterr = makeObjectShared(createObject(OBJ_STRING,sdsnew(\n        \"-BUSY KeyDB is busy running a script. You can only call SCRIPT KILL or SHUTDOWN NOSAVE.\\r\\n\")));\n    shared.masterdownerr = makeObjectShared(createObject(OBJ_STRING,sdsnew(\n        \"-MASTERDOWN Link with MASTER is down and replica-serve-stale-data is set to 'no'.\\r\\n\")));\n    shared.bgsaveerr = makeObjectShared(createObject(OBJ_STRING,sdsnew(\n        \"-MISCONF KeyDB is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the KeyDB logs for details about the RDB error.\\r\\n\")));\n    shared.roslaveerr = makeObjectShared(createObject(OBJ_STRING,sdsnew(\n        \"-READONLY You can't write against a read only replica.\\r\\n\")));\n    shared.noautherr = makeObjectShared(createObject(OBJ_STRING,sdsnew(\n        \"-NOAUTH Authentication required.\\r\\n\")));\n    shared.oomerr = makeObjectShared(createObject(OBJ_STRING,sdsnew(\n        \"-OOM command not allowed when used memory > 'maxmemory'.\\r\\n\")));\n    shared.execaborterr = makeObjectShared(createObject(OBJ_STRING,sdsnew(\n        \"-EXECABORT Transaction discarded because of previous errors.\\r\\n\")));\n    shared.noreplicaserr = makeObjectShared(createObject(OBJ_STRING,sdsnew(\n        \"-NOREPLICAS Not enough good replicas to write.\\r\\n\")));\n    shared.busykeyerr = makeObjectShared(createObject(OBJ_STRING,sdsnew(\n        \"-BUSYKEY Target key name already exists.\\r\\n\")));\n    \n\n    /* The shared NULL depends on the protocol version. */\n    shared.null[0] = NULL;\n    shared.null[1] = NULL;\n    shared.null[2] = makeObjectShared(createObject(OBJ_STRING,sdsnew(\"$-1\\r\\n\")));\n    shared.null[3] = makeObjectShared(createObject(OBJ_STRING,sdsnew(\"_\\r\\n\")));\n\n    shared.nullarray[0] = NULL;\n    shared.nullarray[1] = NULL;\n    shared.nullarray[2] = makeObjectShared(createObject(OBJ_STRING,sdsnew(\"*-1\\r\\n\")));\n    shared.nullarray[3] = makeObjectShared(createObject(OBJ_STRING,sdsnew(\"_\\r\\n\")));\n\n    shared.emptymap[0] = NULL;\n    shared.emptymap[1] = NULL;\n    shared.emptymap[2] = createObject(OBJ_STRING,sdsnew(\"*0\\r\\n\"));\n    shared.emptymap[3] = createObject(OBJ_STRING,sdsnew(\"%0\\r\\n\"));\n\n    shared.emptyset[0] = NULL;\n    shared.emptyset[1] = NULL;\n    shared.emptyset[2] = createObject(OBJ_STRING,sdsnew(\"*0\\r\\n\"));\n    shared.emptyset[3] = createObject(OBJ_STRING,sdsnew(\"~0\\r\\n\"));\n\n    for (j = 0; j < PROTO_SHARED_SELECT_CMDS; j++) {\n        char dictid_str[64];\n        int dictid_len;\n\n        dictid_len = ll2string(dictid_str,sizeof(dictid_str),j);\n        shared.select[j] = makeObjectShared(createObject(OBJ_STRING,\n            sdscatprintf(sdsempty(),\n                \"*2\\r\\n$6\\r\\nSELECT\\r\\n$%d\\r\\n%s\\r\\n\",\n                dictid_len, dictid_str)));\n    }\n    shared.messagebulk = makeObjectShared(\"$7\\r\\nmessage\\r\\n\",13);\n    shared.pmessagebulk = makeObjectShared(\"$8\\r\\npmessage\\r\\n\",14);\n    shared.subscribebulk = makeObjectShared(\"$9\\r\\nsubscribe\\r\\n\",15);\n    shared.unsubscribebulk = makeObjectShared(\"$11\\r\\nunsubscribe\\r\\n\",18);\n    shared.psubscribebulk = makeObjectShared(\"$10\\r\\npsubscribe\\r\\n\",17);\n    shared.punsubscribebulk = makeObjectShared(\"$12\\r\\npunsubscribe\\r\\n\",19);\n\n    /* Shared command names */\n    shared.del = makeObjectShared(\"DEL\",3);\n    shared.unlink = makeObjectShared(\"UNLINK\",6);\n    shared.rpop = makeObjectShared(\"RPOP\",4);\n    shared.lpop = makeObjectShared(\"LPOP\",4);\n    shared.lpush = makeObjectShared(\"LPUSH\",5);\n    shared.rpoplpush = makeObjectShared(\"RPOPLPUSH\",9);\n    shared.lmove = makeObjectShared(\"LMOVE\",5);\n    shared.blmove = makeObjectShared(\"BLMOVE\",6);\n    shared.zpopmin = makeObjectShared(\"ZPOPMIN\",7);\n    shared.zpopmax = makeObjectShared(\"ZPOPMAX\",7);\n    shared.multi = makeObjectShared(\"MULTI\",5);\n    shared.exec = makeObjectShared(\"EXEC\",4);\n    shared.hset = makeObjectShared(\"HSET\",4);\n    shared.srem = makeObjectShared(\"SREM\",4);\n    shared.xgroup = makeObjectShared(\"XGROUP\",6);\n    shared.xclaim = makeObjectShared(\"XCLAIM\",6);\n    shared.script = makeObjectShared(\"SCRIPT\",6);\n    shared.replconf = makeObjectShared(\"REPLCONF\",8);\n    shared.pexpireat = makeObjectShared(\"PEXPIREAT\",9);\n    shared.pexpire = makeObjectShared(\"PEXPIRE\",7);\n    shared.persist = makeObjectShared(\"PERSIST\",7);\n    shared.set = makeObjectShared(\"SET\",3);\n    shared.eval = makeObjectShared(\"EVAL\",4);\n\n    /* Shared command argument */\n    shared.left = makeObjectShared(\"left\",4);\n    shared.right = makeObjectShared(\"right\",5);\n    shared.pxat = makeObjectShared(\"PXAT\", 4);\n    shared.px = makeObjectShared(\"PX\",2);\n    shared.time = makeObjectShared(\"TIME\",4);\n    shared.retrycount = makeObjectShared(\"RETRYCOUNT\",10);\n    shared.force = makeObjectShared(\"FORCE\",5);\n    shared.justid = makeObjectShared(\"JUSTID\",6);\n    shared.lastid = makeObjectShared(\"LASTID\",6);\n    shared.default_username = makeObjectShared(\"default\",7);\n    shared.ping = makeObjectShared(\"ping\",4);\n    shared.replping = makeObjectShared(\"replping\", 8);\n    shared.setid = makeObjectShared(\"SETID\",5);\n    shared.keepttl = makeObjectShared(\"KEEPTTL\",7);\n    shared.load = makeObjectShared(\"LOAD\",4);\n    shared.createconsumer = makeObjectShared(\"CREATECONSUMER\",14);\n    shared.getack = makeObjectShared(\"GETACK\",6);\n    shared.special_asterick = makeObjectShared(\"*\",1);\n    shared.special_equals = makeObjectShared(\"=\",1);\n    shared.redacted = makeObjectShared(\"(redacted)\",10);\n\n    /* KeyDB Specific */\n    shared.hdel = makeObjectShared(createStringObject(\"HDEL\", 4));\n    shared.zrem = makeObjectShared(createStringObject(\"ZREM\", 4));\n    shared.mvccrestore = makeObjectShared(createStringObject(\"KEYDB.MVCCRESTORE\", 17));\n    shared.pexpirememberat = makeObjectShared(createStringObject(\"PEXPIREMEMBERAT\",15));\n\n    for (j = 0; j < OBJ_SHARED_INTEGERS; j++) {\n        shared.integers[j] =\n            makeObjectShared(createObject(OBJ_STRING,(void*)(long)j));\n        shared.integers[j]->encoding = OBJ_ENCODING_INT;\n    }\n    for (j = 0; j < OBJ_SHARED_BULKHDR_LEN; j++) {\n        shared.mbulkhdr[j] = makeObjectShared(createObject(OBJ_STRING,\n            sdscatprintf(sdsempty(),\"*%d\\r\\n\",j)));\n        shared.bulkhdr[j] = makeObjectShared(createObject(OBJ_STRING,\n            sdscatprintf(sdsempty(),\"$%d\\r\\n\",j)));\n    }\n    /* The following two shared objects, minstring and maxstrings, are not\n     * actually used for their value but as a special object meaning\n     * respectively the minimum possible string and the maximum possible\n     * string in string comparisons for the ZRANGEBYLEX command. */\n    shared.minstring = sdsnew(\"minstring\");\n    shared.maxstring = sdsnew(\"maxstring\");\n}\n\nvoid initMasterInfo(redisMaster *master)\n{\n    if (cserver.default_masterauth)\n        master->masterauth = sdsdup(cserver.default_masterauth);\n    else\n        master->masterauth = NULL;\n\n    if (cserver.default_masteruser)\n        master->masteruser = zstrdup(cserver.default_masteruser);\n    else\n        master->masteruser = NULL;\n\n    master->masterport = 6379;\n    master->master = NULL;\n    master->cached_master = NULL;\n    master->master_initial_offset = -1;\n    \n    master->isActive = false;\n\n    master->repl_state = REPL_STATE_NONE;\n    master->repl_down_since = 0; /* Never connected, repl is down since EVER. */\n    master->mvccLastSync = 0;\n}\n\nvoid initServerConfig(void) {\n    int j;\n\n    updateCachedTime();\n    getRandomHexChars(g_pserver->runid,CONFIG_RUN_ID_SIZE);\n    g_pserver->runid[CONFIG_RUN_ID_SIZE] = '\\0';\n    changeReplicationId();\n    clearReplicationId2();\n    g_pserver->hz = CONFIG_DEFAULT_HZ; /* Initialize it ASAP, even if it may get\n                                      updated later after loading the config.\n                                      This value may be used before the server\n                                      is initialized. */\n    g_pserver->clients = listCreate();\n    g_pserver->slaves = listCreate();\n    g_pserver->monitors = listCreate();\n    g_pserver->clients_timeout_table = raxNew();\n    g_pserver->events_processed_while_blocked = 0;\n    g_pserver->timezone = getTimeZone(); /* Initialized by tzset(). */\n    cserver.configfile = NULL;\n    cserver.executable = NULL;\n    g_pserver->hz = g_pserver->config_hz = CONFIG_DEFAULT_HZ;\n    g_pserver->bindaddr_count = 0;\n    g_pserver->unixsocket = NULL;\n    g_pserver->unixsocketperm = CONFIG_DEFAULT_UNIX_SOCKET_PERM;\n    g_pserver->sofd = -1;\n    g_pserver->active_expire_enabled = 1;\n    cserver.skip_checksum_validation = 0;\n    g_pserver->saveparams = NULL;\n    g_pserver->loading = 0;\n    g_pserver->loading_rdb_used_mem = 0;\n    g_pserver->logfile = zstrdup(CONFIG_DEFAULT_LOGFILE);\n    g_pserver->syslog_facility = LOG_LOCAL0;\n    cserver.supervised = 0;\n    cserver.supervised_mode = SUPERVISED_NONE;\n    g_pserver->aof_state = AOF_OFF;\n    g_pserver->aof_rewrite_base_size = 0;\n    g_pserver->aof_rewrite_scheduled = 0;\n    g_pserver->aof_flush_sleep = 0;\n    g_pserver->aof_last_fsync = time(NULL);\n    atomicSet(g_pserver->aof_bio_fsync_status,C_OK);\n    g_pserver->aof_rewrite_time_last = -1;\n    g_pserver->aof_rewrite_time_start = -1;\n    g_pserver->aof_lastbgrewrite_status = C_OK;\n    g_pserver->aof_delayed_fsync = 0;\n    g_pserver->aof_fd = -1;\n    g_pserver->aof_selected_db = -1; /* Make sure the first time will not match */\n    g_pserver->aof_flush_postponed_start = 0;\n    cserver.pidfile = NULL;\n    g_pserver->rdb_filename = NULL;\n    g_pserver->rdb_s3bucketpath = NULL;\n    g_pserver->active_defrag_running = 0;\n    g_pserver->notify_keyspace_events = 0;\n    g_pserver->blocked_clients = 0;\n    memset(g_pserver->blocked_clients_by_type,0,\n           sizeof(g_pserver->blocked_clients_by_type));\n    g_pserver->shutdown_asap = 0;\n    g_pserver->cluster_enabled = 0;\n    g_pserver->cluster_configfile = zstrdup(CONFIG_DEFAULT_CLUSTER_CONFIG_FILE);\n    g_pserver->migrate_cached_sockets = dictCreate(&migrateCacheDictType,NULL);\n    g_pserver->next_client_id = 1; /* Client IDs, start from 1 .*/\n\n    g_pserver->lruclock = getLRUClock();\n    resetServerSaveParams();\n\n    appendServerSaveParams(60*60,1);  /* save after 1 hour and 1 change */\n    appendServerSaveParams(300,100);  /* save after 5 minutes and 100 changes */\n    appendServerSaveParams(60,10000); /* save after 1 minute and 10000 changes */\n\n    /* Replication related */\n    g_pserver->masters = listCreate();\n    g_pserver->enable_multimaster = CONFIG_DEFAULT_ENABLE_MULTIMASTER;\n    g_pserver->repl_syncio_timeout = CONFIG_REPL_SYNCIO_TIMEOUT;\n    g_pserver->master_repl_offset = 0;\n    g_pserver->repl_lowest_off.store(-1, std::memory_order_seq_cst);\n\n    /* Replication partial resync backlog */\n    g_pserver->repl_backlog = NULL;\n    g_pserver->repl_backlog_histlen = 0;\n    g_pserver->repl_backlog_idx = 0;\n    g_pserver->repl_backlog_off = 0;\n    g_pserver->repl_no_slaves_since = time(NULL);\n\n    /* Failover related */\n    g_pserver->failover_end_time = 0;\n    g_pserver->force_failover = 0;\n    g_pserver->target_replica_host = NULL;\n    g_pserver->target_replica_port = 0;\n    g_pserver->failover_state = NO_FAILOVER;\n\n    /* Client output buffer limits */\n    for (j = 0; j < CLIENT_TYPE_OBUF_COUNT; j++)\n        cserver.client_obuf_limits[j] = clientBufferLimitsDefaults[j];\n\n    /* Linux OOM Score config */\n    for (j = 0; j < CONFIG_OOM_COUNT; j++)\n        g_pserver->oom_score_adj_values[j] = configOOMScoreAdjValuesDefaults[j];\n\n    /* Double constants initialization */\n    R_Zero = 0.0;\n    R_PosInf = 1.0/R_Zero;\n    R_NegInf = -1.0/R_Zero;\n    R_Nan = R_Zero/R_Zero;\n\n    /* Command table -- we initialize it here as it is part of the\n     * initial configuration, since command names may be changed via\n     * keydb.conf using the rename-command directive. */\n    g_pserver->commands = dictCreate(&commandTableDictType,NULL);\n    g_pserver->orig_commands = dictCreate(&commandTableDictType,NULL);\n    populateCommandTable();\n    cserver.delCommand = lookupCommandByCString(\"del\");\n    cserver.multiCommand = lookupCommandByCString(\"multi\");\n    cserver.lpushCommand = lookupCommandByCString(\"lpush\");\n    cserver.lpopCommand = lookupCommandByCString(\"lpop\");\n    cserver.rpopCommand = lookupCommandByCString(\"rpop\");\n    cserver.zpopminCommand = lookupCommandByCString(\"zpopmin\");\n    cserver.zpopmaxCommand = lookupCommandByCString(\"zpopmax\");\n    cserver.sremCommand = lookupCommandByCString(\"srem\");\n    cserver.execCommand = lookupCommandByCString(\"exec\");\n    cserver.expireCommand = lookupCommandByCString(\"expire\");\n    cserver.pexpireCommand = lookupCommandByCString(\"pexpire\");\n    cserver.xclaimCommand = lookupCommandByCString(\"xclaim\");\n    cserver.xgroupCommand = lookupCommandByCString(\"xgroup\");\n    cserver.rreplayCommand = lookupCommandByCString(\"rreplay\");\n    cserver.rpoplpushCommand = lookupCommandByCString(\"rpoplpush\");\n    cserver.hdelCommand = lookupCommandByCString(\"hdel\");\n    cserver.zremCommand = lookupCommandByCString(\"zrem\");\n    cserver.lmoveCommand = lookupCommandByCString(\"lmove\");\n\n    /* Debugging */\n    g_pserver->watchdog_period = 0;\n\n    /* By default we want scripts to be always replicated by effects\n     * (single commands executed by the script), and not by sending the\n     * script to the replica / AOF. This is the new way starting from\n     * Redis 5. However it is possible to revert it via keydb.conf. */\n    g_pserver->lua_always_replicate_commands = 1;\n\n    /* Multithreading */\n    cserver.cthreads = CONFIG_DEFAULT_THREADS;\n    cserver.fThreadAffinity = CONFIG_DEFAULT_THREAD_AFFINITY;\n\n    // This will get dereferenced before the second stage init where we have the true db count\n    //  so make sure its zero and initialized\n    g_pserver->db = (redisDb**)zcalloc(sizeof(redisDb*)*std::max(cserver.dbnum, 1), MALLOC_LOCAL);\n\n    cserver.threadAffinityOffset = 0;\n\n    /* Client Pause related */\n    g_pserver->client_pause_type = CLIENT_PAUSE_OFF;\n    g_pserver->client_pause_end_time = 0; \n    initConfigValues();\n}\n\nextern char **environ;\n\n/* Restart the server, executing the same executable that started this\n * instance, with the same arguments and configuration file.\n *\n * The function is designed to directly call execve() so that the new\n * server instance will retain the PID of the previous one.\n *\n * The list of flags, that may be bitwise ORed together, alter the\n * behavior of this function:\n *\n * RESTART_SERVER_NONE              No flags.\n * RESTART_SERVER_GRACEFULLY        Do a proper shutdown before restarting.\n * RESTART_SERVER_CONFIG_REWRITE    Rewrite the config file before restarting.\n *\n * On success the function does not return, because the process turns into\n * a different process. On error C_ERR is returned. */\nint restartServer(int flags, mstime_t delay) {\n    int j;\n\n    /* Check if we still have accesses to the executable that started this\n     * server instance. */\n    if (access(cserver.executable,X_OK) == -1) {\n        serverLog(LL_WARNING,\"Can't restart: this process has no \"\n                             \"permissions to execute %s\", cserver.executable);\n        return C_ERR;\n    }\n\n    /* Config rewriting. */\n    if (flags & RESTART_SERVER_CONFIG_REWRITE &&\n        cserver.configfile &&\n        rewriteConfig(cserver.configfile, 0) == -1)\n    {\n        serverLog(LL_WARNING,\"Can't restart: configuration rewrite process \"\n                             \"failed\");\n        return C_ERR;\n    }\n\n    /* Perform a proper shutdown. */\n    if (flags & RESTART_SERVER_GRACEFULLY &&\n        prepareForShutdown(SHUTDOWN_NOFLAGS) != C_OK)\n    {\n        serverLog(LL_WARNING,\"Can't restart: error preparing for shutdown\");\n        return C_ERR;\n    }\n\n    /* Close all file descriptors, with the exception of stdin, stdout, strerr\n     * which are useful if we restart a Redis server which is not daemonized. */\n    for (j = 3; j < (int)g_pserver->maxclients + 1024; j++) {\n        /* Test the descriptor validity before closing it, otherwise\n         * Valgrind issues a warning on close(). */\n        if (fcntl(j,F_GETFD) != -1)\n        {\n            /* This user to just close() here, but sanitizers detected that as an FD race.\n                The race doesn't matter since we're about to call exec() however we want\n                to cut down on noise, so instead we ask the kernel to close when we call\n                exec(), and only do it ourselves if that fails. */\n            if (fcntl(j, F_SETFD, FD_CLOEXEC) == -1)\n            {\n                close(j);   // failed to set close on exec, close here\n            }\n        }\n    }\n\n    if (flags & RESTART_SERVER_GRACEFULLY) {\n        if (g_pserver->m_pstorageFactory) {\n            for (int idb = 0; idb < cserver.dbnum; ++idb) {\n                g_pserver->db[idb]->storageProviderDelete();\n            }\n            delete g_pserver->metadataDb;\n        }\n    }\n\n    /* Execute the server with the original command line. */\n    if (delay) usleep(delay*1000);\n    zfree(cserver.exec_argv[0]);\n    cserver.exec_argv[0] = zstrdup(cserver.executable);\n    execve(cserver.executable,cserver.exec_argv,environ);\n\n    /* If an error occurred here, there is nothing we can do, but exit. */\n    _exit(1);\n\n    return C_ERR; /* Never reached. */\n}\n\nstatic void readOOMScoreAdj(void) {\n#ifdef HAVE_PROC_OOM_SCORE_ADJ\n    char buf[64];\n    int fd = open(\"/proc/self/oom_score_adj\", O_RDONLY);\n\n    if (fd < 0) return;\n    if (read(fd, buf, sizeof(buf)) > 0)\n        g_pserver->oom_score_adj_base = atoi(buf);\n    close(fd);\n#endif\n}\n\n/* This function will configure the current process's oom_score_adj according\n * to user specified configuration. This is currently implemented on Linux\n * only.\n *\n * A process_class value of -1 implies OOM_CONFIG_MASTER or OOM_CONFIG_REPLICA,\n * depending on current role.\n */\nint setOOMScoreAdj(int process_class) {\n\n    if (g_pserver->oom_score_adj == OOM_SCORE_ADJ_NO) return C_OK;\n    if (process_class == -1)\n        process_class = (listLength(g_pserver->masters) ? CONFIG_OOM_REPLICA : CONFIG_OOM_MASTER);\n\n    serverAssert(process_class >= 0 && process_class < CONFIG_OOM_COUNT);\n\n#ifdef HAVE_PROC_OOM_SCORE_ADJ\n    int fd;\n    int val;\n    char buf[64];\n\n    val = g_pserver->oom_score_adj_values[process_class];\n    if (g_pserver->oom_score_adj == OOM_SCORE_RELATIVE)\n        val += g_pserver->oom_score_adj_base;\n    if (val > 1000) val = 1000;\n    if (val < -1000) val = -1000;\n\n    snprintf(buf, sizeof(buf) - 1, \"%d\\n\", val);\n\n    fd = open(\"/proc/self/oom_score_adj\", O_WRONLY);\n    if (fd < 0 || write(fd, buf, strlen(buf)) < 0) {\n        serverLog(LL_WARNING, \"Unable to write oom_score_adj: %s\", strerror(errno));\n        if (fd != -1) close(fd);\n        return C_ERR;\n    }\n\n    close(fd);\n    return C_OK;\n#else\n    /* Unsupported */\n    return C_ERR;\n#endif\n}\n\n/* This function will try to raise the max number of open files accordingly to\n * the configured max number of clients. It also reserves a number of file\n * descriptors (CONFIG_MIN_RESERVED_FDS) for extra operations of\n * persistence, listening sockets, log files and so forth.\n *\n * If it will not be possible to set the limit accordingly to the configured\n * max number of clients, the function will do the reverse setting\n * g_pserver->maxclients to the value that we can actually handle. */\nvoid adjustOpenFilesLimit(void) {\n    rlim_t maxfiles = g_pserver->maxclients+CONFIG_MIN_RESERVED_FDS;\n    if (g_pserver->m_pstorageFactory)\n        maxfiles += g_pserver->m_pstorageFactory->filedsRequired();\n    struct rlimit limit;\n\n    if (getrlimit(RLIMIT_NOFILE,&limit) == -1) {\n        serverLog(LL_WARNING,\"Unable to obtain the current NOFILE limit (%s), assuming 1024 and setting the max clients configuration accordingly.\",\n            strerror(errno));\n        g_pserver->maxclients = 1024-CONFIG_MIN_RESERVED_FDS;\n    } else {\n        rlim_t oldlimit = limit.rlim_cur;\n\n        /* Set the max number of files if the current limit is not enough\n         * for our needs. */\n        if (oldlimit < maxfiles) {\n            rlim_t bestlimit;\n            int setrlimit_error = 0;\n\n            /* Try to set the file limit to match 'maxfiles' or at least\n             * to the higher value supported less than maxfiles. */\n            bestlimit = maxfiles;\n            while(bestlimit > oldlimit) {\n                rlim_t decr_step = 16;\n\n                limit.rlim_cur = bestlimit;\n                limit.rlim_max = bestlimit;\n                if (setrlimit(RLIMIT_NOFILE,&limit) != -1) break;\n                setrlimit_error = errno;\n\n                /* We failed to set file limit to 'bestlimit'. Try with a\n                 * smaller limit decrementing by a few FDs per iteration. */\n                if (bestlimit < decr_step) break;\n                bestlimit -= decr_step;\n            }\n\n            /* Assume that the limit we get initially is still valid if\n             * our last try was even lower. */\n            if (bestlimit < oldlimit) bestlimit = oldlimit;\n\n            if (bestlimit < maxfiles) {\n                unsigned int old_maxclients = g_pserver->maxclients;\n                g_pserver->maxclients = bestlimit-CONFIG_MIN_RESERVED_FDS;\n                /* maxclients is unsigned so may overflow: in order\n                 * to check if maxclients is now logically less than 1\n                 * we test indirectly via bestlimit. */\n                if (bestlimit <= CONFIG_MIN_RESERVED_FDS) {\n                    serverLog(LL_WARNING,\"Your current 'ulimit -n' \"\n                        \"of %llu is not enough for the server to start. \"\n                        \"Please increase your open file limit to at least \"\n                        \"%llu. Exiting.\",\n                        (unsigned long long) oldlimit,\n                        (unsigned long long) maxfiles);\n                    exit(1);\n                }\n                serverLog(LL_WARNING,\"You requested maxclients of %d \"\n                    \"requiring at least %llu max file descriptors.\",\n                    old_maxclients,\n                    (unsigned long long) maxfiles);\n                serverLog(LL_WARNING,\"Server can't set maximum open files \"\n                    \"to %llu because of OS error: %s.\",\n                    (unsigned long long) maxfiles, strerror(setrlimit_error));\n                serverLog(LL_WARNING,\"Current maximum open files is %llu. \"\n                    \"maxclients has been reduced to %d to compensate for \"\n                    \"low ulimit. \"\n                    \"If you need higher maxclients increase 'ulimit -n'.\",\n                    (unsigned long long) bestlimit, g_pserver->maxclients);\n            } else {\n                serverLog(LL_NOTICE,\"Increased maximum number of open files \"\n                    \"to %llu (it was originally set to %llu).\",\n                    (unsigned long long) maxfiles,\n                    (unsigned long long) oldlimit);\n            }\n        }\n    }\n}\n\n/* Check that g_pserver->tcp_backlog can be actually enforced in Linux according\n * to the value of /proc/sys/net/core/somaxconn, or warn about it. */\nvoid checkTcpBacklogSettings(void) {\n#ifdef HAVE_PROC_SOMAXCONN\n    FILE *fp = fopen(\"/proc/sys/net/core/somaxconn\",\"r\");\n    char buf[1024];\n    if (!fp) return;\n    if (fgets(buf,sizeof(buf),fp) != NULL) {\n        int somaxconn = atoi(buf);\n        if (somaxconn > 0 && somaxconn < g_pserver->tcp_backlog) {\n            serverLog(LL_WARNING,\"WARNING: The TCP backlog setting of %d cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of %d.\", g_pserver->tcp_backlog, somaxconn);\n        }\n    }\n    fclose(fp);\n#endif\n}\n\nvoid closeSocketListeners(socketFds *sfd) {\n    int j;\n\n    for (j = 0; j < sfd->count; j++) {\n        if (sfd->fd[j] == -1) continue;\n\n        aeDeleteFileEvent(serverTL->el, sfd->fd[j], AE_READABLE);\n        close(sfd->fd[j]);\n    }\n\n    sfd->count = 0;\n}\n\n/* Create an event handler for accepting new connections in TCP or TLS domain sockets.\n * This works atomically for all socket fds */\nint createSocketAcceptHandler(socketFds *sfd, aeFileProc *accept_handler) {\n    int j;\n\n    for (j = 0; j < sfd->count; j++) {\n        if (aeCreateFileEvent(serverTL->el, sfd->fd[j], AE_READABLE, accept_handler,NULL) == AE_ERR) {\n            /* Rollback */\n            for (j = j-1; j >= 0; j--) aeDeleteFileEvent(serverTL->el, sfd->fd[j], AE_READABLE);\n            return C_ERR;\n        }\n    }\n    return C_OK;\n}\n\n/* Initialize a set of file descriptors to listen to the specified 'port'\n * binding the addresses specified in the Redis server configuration.\n *\n * The listening file descriptors are stored in the integer array 'fds'\n * and their number is set in '*count'.\n *\n * The addresses to bind are specified in the global g_pserver->bindaddr array\n * and their number is g_pserver->bindaddr_count. If the server configuration\n * contains no specific addresses to bind, this function will try to\n * bind * (all addresses) for both the IPv4 and IPv6 protocols.\n *\n * On success the function returns C_OK.\n *\n * On error the function returns C_ERR. For the function to be on\n * error, at least one of the g_pserver->bindaddr addresses was\n * impossible to bind, or no bind addresses were specified in the server\n * configuration but the function is not able to bind * for at least\n * one of the IPv4 or IPv6 protocols. */\nint listenToPort(int port, socketFds *sfd, int fReusePort, int fFirstListen) {\n    int j;\n    const char **bindaddr = (const char**)g_pserver->bindaddr;\n    int bindaddr_count = g_pserver->bindaddr_count;\n    const char *default_bindaddr[2] = {\"*\", \"-::*\"};\n\n    /* Force binding of 0.0.0.0 if no bind address is specified. */\n    if (g_pserver->bindaddr_count == 0) {\n        bindaddr_count = 2;\n        bindaddr = default_bindaddr;\n    }\n\n    for (j = 0; j < bindaddr_count; j++) {\n        const char* addr = bindaddr[j];\n        int optional = *addr == '-';\n        if (optional) addr++;\n        if (strchr(addr,':')) {\n            /* Bind IPv6 address. */\n            sfd->fd[sfd->count] = anetTcp6Server(serverTL->neterr,port,addr,g_pserver->tcp_backlog,fReusePort,fFirstListen);\n        } else {\n            /* Bind IPv4 address. */\n            sfd->fd[sfd->count] = anetTcpServer(serverTL->neterr,port,addr,g_pserver->tcp_backlog,fReusePort,fFirstListen);\n        }\n        if (sfd->fd[sfd->count] == ANET_ERR) {\n            int net_errno = errno;\n            serverLog(LL_WARNING,\n                \"Warning: Could not create server TCP listening socket %s:%d: %s\",\n                addr, port, serverTL->neterr);\n            if (net_errno == EADDRNOTAVAIL && optional)\n                continue;\n            if (net_errno == ENOPROTOOPT     || net_errno == EPROTONOSUPPORT ||\n                net_errno == ESOCKTNOSUPPORT || net_errno == EPFNOSUPPORT ||\n                net_errno == EAFNOSUPPORT)\n                continue;\n\n            /* Rollback successful listens before exiting */\n            closeSocketListeners(sfd);\n            return C_ERR;\n        }\n        anetNonBlock(NULL,sfd->fd[sfd->count]);\n        anetCloexec(sfd->fd[sfd->count]);\n        sfd->count++;\n    }\n    return C_OK;\n}\n\n/* Resets the stats that we expose via INFO or other means that we want\n * to reset via CONFIG RESETSTAT. The function is also used in order to\n * initialize these fields in initServer() at server startup. */\nvoid resetServerStats(void) {\n    int j;\n\n    g_pserver->stat_numcommands = 0;\n    g_pserver->stat_numconnections = 0;\n    g_pserver->stat_expiredkeys = 0;\n    g_pserver->stat_expired_stale_perc = 0;\n    g_pserver->stat_expired_time_cap_reached_count = 0;\n    g_pserver->stat_expire_cycle_time_used = 0;\n    g_pserver->stat_evictedkeys = 0;\n    g_pserver->stat_keyspace_misses = 0;\n    g_pserver->stat_keyspace_hits = 0;\n    g_pserver->stat_active_defrag_hits = 0;\n    g_pserver->stat_active_defrag_misses = 0;\n    g_pserver->stat_active_defrag_key_hits = 0;\n    g_pserver->stat_active_defrag_key_misses = 0;\n    g_pserver->stat_active_defrag_scanned = 0;\n    g_pserver->stat_fork_time = 0;\n    g_pserver->stat_fork_rate = 0;\n    g_pserver->stat_total_forks = 0;\n    g_pserver->stat_rejected_conn = 0;\n    g_pserver->stat_sync_full = 0;\n    g_pserver->stat_sync_partial_ok = 0;\n    g_pserver->stat_sync_partial_err = 0;\n    g_pserver->stat_total_reads_processed = 0;\n    g_pserver->stat_total_writes_processed = 0;\n    for (j = 0; j < STATS_METRIC_COUNT; j++) {\n        g_pserver->inst_metric[j].idx = 0;\n        g_pserver->inst_metric[j].last_sample_time = mstime();\n        g_pserver->inst_metric[j].last_sample_count = 0;\n        memset(g_pserver->inst_metric[j].samples,0,\n            sizeof(g_pserver->inst_metric[j].samples));\n    }\n    g_pserver->stat_net_input_bytes = 0;\n    g_pserver->stat_net_output_bytes = 0;\n    g_pserver->stat_unexpected_error_replies = 0;\n    for (int iel = 0; iel < cserver.cthreads; ++iel)\n        g_pserver->rgthreadvar[iel].stat_total_error_replies = 0;\n    g_pserver->stat_dump_payload_sanitizations = 0;\n    g_pserver->aof_delayed_fsync = 0;\n}\n\n/* Make the thread killable at any time, so that kill threads functions\n * can work reliably (default cancelability type is PTHREAD_CANCEL_DEFERRED).\n * Needed for pthread_cancel used by the fast memory test used by the crash report. */\nvoid makeThreadKillable(void) {\n    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);\n    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);\n}\n\nstatic void initNetworkingThread(int iel, int fReusePort)\n{\n    /* Open the TCP listening socket for the user commands. */\n    if (fReusePort || (iel == IDX_EVENT_LOOP_MAIN))\n    {\n        if (g_pserver->port != 0 &&\n            listenToPort(g_pserver->port,&g_pserver->rgthreadvar[iel].ipfd, fReusePort, (iel == IDX_EVENT_LOOP_MAIN)) == C_ERR) {\n            serverLog(LL_WARNING, \"Failed listening on port %u (TCP), aborting.\", g_pserver->port);\n            exit(1);\n        }\n        if (g_pserver->tls_port != 0 &&\n            listenToPort(g_pserver->tls_port,&g_pserver->rgthreadvar[iel].tlsfd, fReusePort, (iel == IDX_EVENT_LOOP_MAIN)) == C_ERR) {\n            serverLog(LL_WARNING, \"Failed listening on port %u (TLS), aborting.\", g_pserver->port);\n            exit(1);\n        }\n    }\n    else\n    {\n        // We use the main threads file descriptors\n        memcpy(&g_pserver->rgthreadvar[iel].ipfd, &g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].ipfd, sizeof(socketFds));\n        g_pserver->rgthreadvar[iel].ipfd.count = g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].ipfd.count;\n    }\n\n    /* Create an event handler for accepting new connections in TCP */\n    for (int j = 0; j < g_pserver->rgthreadvar[iel].ipfd.count; j++) {\n        if (aeCreateFileEvent(g_pserver->rgthreadvar[iel].el, g_pserver->rgthreadvar[iel].ipfd.fd[j], AE_READABLE|AE_READ_THREADSAFE,\n            acceptTcpHandler,NULL) == AE_ERR)\n            {\n                serverPanic(\n                    \"Unrecoverable error creating g_pserver->ipfd file event.\");\n            }\n    }\n\n    makeThreadKillable();\n\n    for (int j = 0; j < g_pserver->rgthreadvar[iel].tlsfd.count; j++) {\n        if (aeCreateFileEvent(g_pserver->rgthreadvar[iel].el, g_pserver->rgthreadvar[iel].tlsfd.fd[j], AE_READABLE|AE_READ_THREADSAFE,\n            acceptTLSHandler,NULL) == AE_ERR)\n            {\n                serverPanic(\n                    \"Unrecoverable error creating g_pserver->tlsfd file event.\");\n            }\n    }\n}\n\nstatic void initNetworking(int fReusePort)\n{\n    // We only initialize the main thread here, since RDB load is a special case that processes\n    //  clients before our server threads are launched.\n    initNetworkingThread(IDX_EVENT_LOOP_MAIN, fReusePort);\n\n    /* Open the listening Unix domain socket. */\n    if (g_pserver->unixsocket != NULL) {\n        unlink(g_pserver->unixsocket); /* don't care if this fails */\n        g_pserver->sofd = anetUnixServer(serverTL->neterr,g_pserver->unixsocket,\n            g_pserver->unixsocketperm, g_pserver->tcp_backlog);\n        if (g_pserver->sofd == ANET_ERR) {\n            serverLog(LL_WARNING, \"Opening Unix socket: %s\", serverTL->neterr);\n            exit(1);\n        }\n        anetNonBlock(NULL,g_pserver->sofd);\n    }\n\n    /* Abort if there are no listening sockets at all. */\n    if (g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].ipfd.count == 0 && g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].tlsfd.count == 0 && g_pserver->sofd < 0) {\n        serverLog(LL_WARNING, \"Configured to not listen anywhere, exiting.\");\n        exit(1);\n    }\n\n    if (g_pserver->sofd > 0 && aeCreateFileEvent(g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].el,g_pserver->sofd,AE_READABLE|AE_READ_THREADSAFE,\n        acceptUnixHandler,NULL) == AE_ERR) serverPanic(\"Unrecoverable error creating g_pserver->sofd file event.\");\n}\n\nstatic void initServerThread(struct redisServerThreadVars *pvar, int fMain)\n{\n    pvar->unblocked_clients = listCreate();\n    pvar->clients_pending_asyncwrite = listCreate();\n    pvar->ipfd.count = 0;\n    pvar->tlsfd.count = 0;\n    pvar->cclients = 0;\n    pvar->in_eval = 0;\n    pvar->in_exec = 0;\n    pvar->el = aeCreateEventLoop(g_pserver->maxclients+CONFIG_FDSET_INCR);\n    pvar->current_client = nullptr;\n    pvar->fRetrySetAofEvent = false;\n    if (pvar->el == NULL) {\n        serverLog(LL_WARNING,\n            \"Failed creating the event loop. Error message: '%s'\",\n            strerror(errno));\n        exit(1);\n    }\n    aeSetBeforeSleepProc(pvar->el, beforeSleep, AE_SLEEP_THREADSAFE);\n    aeSetAfterSleepProc(pvar->el, afterSleep, AE_SLEEP_THREADSAFE);\n\n    fastlock_init(&pvar->lockPendingWrite, \"lockPendingWrite\");\n\n    if (!fMain)\n    {\n        if (aeCreateTimeEvent(pvar->el, 1, serverCronLite, NULL, NULL) == AE_ERR) {\n            serverPanic(\"Can't create event loop timers.\");\n            exit(1);\n        }\n    }\n\n    /* Register a readable event for the pipe used to awake the event loop\n     * when a blocked client in a module needs attention. */\n    if (aeCreateFileEvent(pvar->el, g_pserver->module_blocked_pipe[0], AE_READABLE,\n        moduleBlockedClientPipeReadable,NULL) == AE_ERR) {\n            serverPanic(\n                \"Error registering the readable event for the module \"\n                \"blocked clients subsystem.\");\n    }\n}\n\nvoid initServer(void) {\n    signal(SIGHUP, SIG_IGN);\n    signal(SIGPIPE, SIG_IGN);\n    setupSignalHandlers();\n    makeThreadKillable();\n\n    zfree(g_pserver->db);   // initServerConfig created a dummy array, free that now\n    g_pserver->db = (redisDb**)zmalloc(sizeof(redisDb*)*cserver.dbnum, MALLOC_LOCAL);\n\n    /* Create the Redis databases, and initialize other internal state. */\n    if (g_pserver->m_pstorageFactory == nullptr) {\n        for (int j = 0; j < cserver.dbnum; j++) {\n            g_pserver->db[j] = new (MALLOC_LOCAL) redisDb();\n            g_pserver->db[j]->initialize(j);\n        }\n    } else {\n        // Read FLASH metadata and load the appropriate storage dbid into each databse index, as each DB index can have different storage dbid mapped due to the swapdb command.\n        g_pserver->metadataDb = g_pserver->m_pstorageFactory->createMetadataDb();\n        for (int idb = 0; idb < cserver.dbnum; ++idb)\n        {\n            int storage_dbid = idb;\n            std::string dbid_key = \"db-\" + std::to_string(idb);\n            g_pserver->metadataDb->retrieve(dbid_key.c_str(), dbid_key.length(), [&](const char *, size_t, const void *data, size_t){\n                storage_dbid = *(int*)data;\n            });\n\n            g_pserver->db[idb] = new (MALLOC_LOCAL) redisDb();\n            g_pserver->db[idb]->initialize(idb, storage_dbid);\n        }\n    }\n\n    for (int i = 0; i < MAX_EVENT_LOOPS; ++i)\n    {\n        g_pserver->rgthreadvar[i].rgdbSnapshot = (const redisDbPersistentDataSnapshot**)zcalloc(sizeof(redisDbPersistentDataSnapshot*)*cserver.dbnum, MALLOC_LOCAL);\n        serverAssert(g_pserver->rgthreadvar[i].rgdbSnapshot != nullptr);\n    }\n    g_pserver->modulethreadvar.rgdbSnapshot = (const redisDbPersistentDataSnapshot**)zcalloc(sizeof(redisDbPersistentDataSnapshot*)*cserver.dbnum, MALLOC_LOCAL);\n    serverAssert(g_pserver->modulethreadvar.rgdbSnapshot != nullptr);\n\n    serverAssert(g_pserver->rgthreadvar[0].rgdbSnapshot != nullptr);\n\n    /* Fixup Master Client Database */\n    listIter li;\n    listNode *ln;\n    listRewind(g_pserver->masters, &li);\n    while ((ln = listNext(&li)))\n    {\n        redisMaster *mi = (redisMaster*)listNodeValue(ln);\n        serverAssert(mi->master == nullptr);\n        if (mi->cached_master != nullptr)\n            selectDb(mi->cached_master, 0);\n    }\n\n    g_pserver->aof_state = g_pserver->aof_enabled ? AOF_ON : AOF_OFF;\n    g_pserver->hz = g_pserver->config_hz;\n    cserver.pid = getpid();\n    g_pserver->in_fork_child = CHILD_TYPE_NONE;\n    cserver.main_thread_id = pthread_self();\n    g_pserver->errors = raxNew();\n    g_pserver->clients_index = raxNew();\n    g_pserver->clients_to_close = listCreate();\n    g_pserver->replicaseldb = -1; /* Force to emit the first SELECT command. */\n    g_pserver->ready_keys = listCreate();\n    g_pserver->clients_waiting_acks = listCreate();\n    g_pserver->get_ack_from_slaves = 0;\n    cserver.system_memory_size = zmalloc_get_memory_size();\n    g_pserver->paused_clients = listCreate();\n    g_pserver->events_processed_while_blocked = 0;\n    g_pserver->blocked_last_cron = 0;\n    g_pserver->replication_allowed = 1;\n    g_pserver->blocking_op_nesting = 0;\n    g_pserver->rdb_pipe_read = -1;\n    g_pserver->client_pause_type = CLIENT_PAUSE_OFF;\n\n\n    if ((g_pserver->tls_port || g_pserver->tls_replication || g_pserver->tls_cluster)\n            && tlsConfigure(&g_pserver->tls_ctx_config) == C_ERR) {\n        serverLog(LL_WARNING, \"Failed to configure TLS. Check logs for more info.\");\n        exit(1);\n    }\n\n    createSharedObjects();\n    adjustOpenFilesLimit();\n    const char *clk_msg = monotonicInit();\n    serverLog(LL_NOTICE, \"monotonic clock: %s\", clk_msg);\n\n    evictionPoolAlloc(); /* Initialize the LRU keys pool. */\n    g_pserver->pubsub_channels = dictCreate(&keylistDictType,NULL);\n    g_pserver->pubsub_patterns = dictCreate(&keylistDictType,NULL);\n    g_pserver->cronloops = 0;\n    g_pserver->child_pid = -1;\n    g_pserver->child_type = CHILD_TYPE_NONE;\n    g_pserver->rdbThreadVars.fRdbThreadCancel = false;\n    g_pserver->rdb_child_type = RDB_CHILD_TYPE_NONE;\n    g_pserver->rdb_pipe_conns = NULL;\n    g_pserver->rdb_pipe_numconns = 0;\n    g_pserver->rdb_pipe_numconns_writing = 0;\n    g_pserver->rdb_pipe_buff = NULL;\n    g_pserver->rdb_pipe_bufflen = 0;\n    g_pserver->rdb_bgsave_scheduled = 0;\n    g_pserver->child_info_pipe[0] = -1;\n    g_pserver->child_info_pipe[1] = -1;\n    g_pserver->child_info_nread = 0;\n    aofRewriteBufferReset();\n    g_pserver->aof_buf = sdsempty();\n    g_pserver->lastsave = time(NULL); /* At startup we consider the DB saved. */\n    g_pserver->lastbgsave_try = 0;    /* At startup we never tried to BGSAVE. */\n    g_pserver->rdb_save_time_last = -1;\n    g_pserver->rdb_save_time_start = -1;\n    g_pserver->dirty = 0;\n    resetServerStats();\n    /* A few stats we don't want to reset: server startup time, and peak mem. */\n    cserver.stat_starttime = time(NULL);\n    g_pserver->stat_peak_memory = 0;\n    g_pserver->stat_current_cow_bytes = 0;\n    g_pserver->stat_current_cow_updated = 0;\n    g_pserver->stat_current_save_keys_processed = 0;\n    g_pserver->stat_current_save_keys_total = 0;\n    g_pserver->stat_rdb_cow_bytes = 0;\n    g_pserver->stat_aof_cow_bytes = 0;\n    g_pserver->stat_module_cow_bytes = 0;\n    g_pserver->stat_module_progress = 0;\n    for (int j = 0; j < CLIENT_TYPE_COUNT; j++)\n        g_pserver->stat_clients_type_memory[j] = 0;\n    g_pserver->cron_malloc_stats.zmalloc_used = 0;\n    g_pserver->cron_malloc_stats.process_rss = 0;\n    g_pserver->cron_malloc_stats.allocator_allocated = 0;\n    g_pserver->cron_malloc_stats.allocator_active = 0;\n    g_pserver->cron_malloc_stats.allocator_resident = 0;\n    g_pserver->cron_malloc_stats.sys_available = 0;\n    g_pserver->cron_malloc_stats.sys_total = g_pserver->force_eviction_percent ? getMemTotal() : 0;\n    g_pserver->lastbgsave_status = C_OK;\n    g_pserver->aof_last_write_status = C_OK;\n    g_pserver->aof_last_write_errno = 0;\n    g_pserver->repl_good_slaves_count = 0;\n\n    g_pserver->mvcc_tstamp = 0;\n\n\n    /* Create the timer callback, this is our way to process many background\n     * operations incrementally, like clients timeout, eviction of unaccessed\n     * expired keys and so forth. */\n    if (aeCreateTimeEvent(g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].el, 1, serverCron, NULL, NULL) == AE_ERR) {\n        serverPanic(\"Can't create event loop timers.\");\n        exit(1);\n    }\n\n    /* Open the AOF file if needed. */\n    if (g_pserver->aof_state == AOF_ON) {\n        g_pserver->aof_fd = open(g_pserver->aof_filename,\n                               O_WRONLY|O_APPEND|O_CREAT,0644);\n        if (g_pserver->aof_fd == -1) {\n            serverLog(LL_WARNING, \"Can't open the append-only file: %s\",\n                strerror(errno));\n            exit(1);\n        }\n    }\n\n    /* 32 bit instances are limited to 4GB of address space, so if there is\n     * no explicit limit in the user provided configuration we set a limit\n     * at 3 GB using maxmemory with 'noeviction' policy'. This avoids\n     * useless crashes of the Redis instance for out of memory. */\n    if (sizeof(void*) == 4 && g_pserver->maxmemory == 0) {\n        serverLog(LL_WARNING,\"Warning: 32 bit instance detected but no memory limit set. Setting 3 GB maxmemory limit with 'noeviction' policy now.\");\n        g_pserver->maxmemory = 3072LL*(1024*1024); /* 3 GB */\n        g_pserver->maxmemory_policy = MAXMEMORY_NO_EVICTION;\n    }\n\n    /* Generate UUID */\n    static_assert(sizeof(uuid_t) == sizeof(cserver.uuid), \"UUIDs are standardized at 16-bytes\");\n    uuid_generate((unsigned char*)cserver.uuid);\n\n    if (g_pserver->cluster_enabled) clusterInit();\n    replicationScriptCacheInit();\n    scriptingInit(1);\n    slowlogInit();\n    latencyMonitorInit();\n\n    if (g_pserver->m_pstorageFactory) {\n        if (g_pserver->metadataDb) {\n            g_pserver->metadataDb->retrieve(\"repl-id\", 7, [&](const char *, size_t, const void *data, size_t cb){\n                if (cb == sizeof(g_pserver->replid)) {\n                    memcpy(g_pserver->replid, data, cb);\n                }\n            });\n            g_pserver->metadataDb->retrieve(\"repl-offset\", 11, [&](const char *, size_t, const void *data, size_t cb){\n                if (cb == sizeof(g_pserver->master_repl_offset)) {\n                    g_pserver->master_repl_offset = *(long long*)data;\n                }\n            });\n\n            int repl_stream_db = -1;\n            g_pserver->metadataDb->retrieve(\"repl-stream-db\", 14, [&](const char *, size_t, const void *data, size_t){\n                repl_stream_db = *(int*)data;\n            });\n\n            /* !!! AFTER THIS POINT WE CAN NO LONGER READ FROM THE META DB AS IT WILL BE OVERWRITTEN !!! */\n            // replicationCacheMasterUsingMyself triggers the overwrite \n\n            listIter li;\n            listNode *ln;\n            listRewind(g_pserver->masters, &li);\n            while ((ln = listNext(&li)))\n            {\n                redisMaster *mi = (redisMaster*)listNodeValue(ln);\n                /* If we are a replica, create a cached master from this\n                * information, in order to allow partial resynchronizations\n                * with masters. */\n                replicationCacheMasterUsingMyself(mi);\n                selectDb(mi->cached_master, repl_stream_db);\n            }\n        }\n    }\n\n    saveMasterStatusToStorage(false); // eliminate the repl-offset field\n    \n    /* Initialize ACL default password if it exists */\n    ACLUpdateDefaultUserPassword(g_pserver->requirepass);\n}\n\n/* Some steps in server initialization need to be done last (after modules\n * are loaded).\n * Specifically, creation of threads due to a race bug in ld.so, in which\n * Thread Local Storage initialization collides with dlopen call.\n * see: https://sourceware.org/bugzilla/show_bug.cgi?id=19329 */\nvoid InitServerLast() {\n\n    /* We have to initialize storage providers after the cluster has been initialized */\n    moduleFireServerEvent(REDISMODULE_EVENT_LOADING, REDISMODULE_SUBEVENT_LOADING_FLASH_START, NULL);\n    for (int idb = 0; idb < cserver.dbnum; ++idb)\n    {\n        g_pserver->db[idb]->storageProviderInitialize();\n    }\n    moduleFireServerEvent(REDISMODULE_EVENT_LOADING, REDISMODULE_SUBEVENT_LOADING_ENDED, NULL);\n\n    bioInit();\n    set_jemalloc_bg_thread(cserver.jemalloc_bg_thread);\n    g_pserver->initial_memory_usage = zmalloc_used_memory();\n\n    g_pserver->asyncworkqueue = new (MALLOC_LOCAL) AsyncWorkQueue(cserver.cthreads);\n\n    // Allocate the repl backlog\n    \n}\n\n/* Parse the flags string description 'strflags' and set them to the\n * command 'c'. If the flags are all valid C_OK is returned, otherwise\n * C_ERR is returned (yet the recognized flags are set in the command). */\nint populateCommandTableParseFlags(struct redisCommand *c, const char *strflags) {\n    int argc;\n    sds *argv;\n\n    /* Split the line into arguments for processing. */\n    argv = sdssplitargs(strflags,&argc);\n    if (argv == NULL) return C_ERR;\n\n    for (int j = 0; j < argc; j++) {\n        char *flag = argv[j];\n        if (!strcasecmp(flag,\"write\")) {\n            c->flags |= CMD_WRITE|CMD_CATEGORY_WRITE;\n        } else if (!strcasecmp(flag,\"read-only\")) {\n            c->flags |= CMD_READONLY|CMD_CATEGORY_READ;\n        } else if (!strcasecmp(flag,\"use-memory\")) {\n            c->flags |= CMD_DENYOOM;\n        } else if (!strcasecmp(flag,\"admin\")) {\n            c->flags |= CMD_ADMIN|CMD_CATEGORY_ADMIN|CMD_CATEGORY_DANGEROUS;\n        } else if (!strcasecmp(flag,\"pub-sub\")) {\n            c->flags |= CMD_PUBSUB|CMD_CATEGORY_PUBSUB;\n        } else if (!strcasecmp(flag,\"no-script\")) {\n            c->flags |= CMD_NOSCRIPT;\n        } else if (!strcasecmp(flag,\"random\")) {\n            c->flags |= CMD_RANDOM;\n        } else if (!strcasecmp(flag,\"to-sort\")) {\n            c->flags |= CMD_SORT_FOR_SCRIPT;\n        } else if (!strcasecmp(flag,\"ok-loading\")) {\n            c->flags |= CMD_LOADING;\n        } else if (!strcasecmp(flag,\"ok-stale\")) {\n            c->flags |= CMD_STALE;\n        } else if (!strcasecmp(flag,\"no-monitor\")) {\n            c->flags |= CMD_SKIP_MONITOR;\n        } else if (!strcasecmp(flag,\"no-slowlog\")) {\n            c->flags |= CMD_SKIP_SLOWLOG;\n        } else if (!strcasecmp(flag,\"cluster-asking\")) {\n            c->flags |= CMD_ASKING;\n        } else if (!strcasecmp(flag,\"fast\")) {\n            c->flags |= CMD_FAST | CMD_CATEGORY_FAST;\n        } else if (!strcasecmp(flag,\"noprop\")) {\n            c->flags |= CMD_SKIP_PROPOGATE;\n        } else if (!strcasecmp(flag,\"no-auth\")) {\n            c->flags |= CMD_NO_AUTH;\n        } else if (!strcasecmp(flag,\"may-replicate\")) {\n            c->flags |= CMD_MAY_REPLICATE;\n        } else if (!strcasecmp(flag,\"async\")) {\n            c->flags |= CMD_ASYNC_OK;\n        } else {\n            /* Parse ACL categories here if the flag name starts with @. */\n            uint64_t catflag;\n            if (flag[0] == '@' &&\n                (catflag = ACLGetCommandCategoryFlagByName(flag+1)) != 0)\n            {\n                c->flags |= catflag;\n            } else {\n                sdsfreesplitres(argv,argc);\n                return C_ERR;\n            }\n        }\n    }\n    /* If it's not @fast is @slow in this binary world. */\n    if (!(c->flags & CMD_CATEGORY_FAST)) c->flags |= CMD_CATEGORY_SLOW;\n\n    sdsfreesplitres(argv,argc);\n    return C_OK;\n}\n\n/* Populates the KeyDB Command Table starting from the hard coded list\n * we have on top of server.cpp file. */\nvoid populateCommandTable(void) {\n    int j;\n    int numcommands = sizeof(redisCommandTable)/sizeof(struct redisCommand);\n\n    for (j = 0; j < numcommands; j++) {\n        struct redisCommand *c = redisCommandTable+j;\n        int retval1, retval2;\n\n        /* Translate the command string flags description into an actual\n         * set of flags. */\n        if (populateCommandTableParseFlags(c,c->sflags) == C_ERR)\n            serverPanic(\"Unsupported command flag\");\n\n        c->id = ACLGetCommandID(c->name); /* Assign the ID used for ACL. */\n        retval1 = dictAdd(g_pserver->commands, sdsnew(c->name), c);\n        /* Populate an additional dictionary that will be unaffected\n         * by rename-command statements in keydb.conf. */\n        retval2 = dictAdd(g_pserver->orig_commands, sdsnew(c->name), c);\n        serverAssert(retval1 == DICT_OK && retval2 == DICT_OK);\n    }\n}\n\nvoid resetCommandTableStats(void) {\n    struct redisCommand *c;\n    dictEntry *de;\n    dictIterator *di;\n\n    di = dictGetSafeIterator(g_pserver->commands);\n    while((de = dictNext(di)) != NULL) {\n        c = (struct redisCommand *) dictGetVal(de);\n        c->microseconds = 0;\n        c->calls = 0;\n        c->rejected_calls = 0;\n        c->failed_calls = 0;\n    }\n    dictReleaseIterator(di);\n\n}\n\nstatic void zfree_noconst(void *p) {\n    zfree(p);\n}\n\nvoid fuzzOutOfMemoryHandler(size_t allocation_size) {\n    serverLog(LL_WARNING,\"Out Of Memory allocating %zu bytes!\",\n        allocation_size);\n    exit(EXIT_FAILURE); // don't crash because it causes false positives\n}\n\nvoid resetErrorTableStats(void) {\n    raxFreeWithCallback(g_pserver->errors, zfree_noconst);\n    g_pserver->errors = raxNew();\n}\n\n/* ========================== Redis OP Array API ============================ */\n\nvoid redisOpArrayInit(redisOpArray *oa) {\n    oa->ops = NULL;\n    oa->numops = 0;\n}\n\nint redisOpArrayAppend(redisOpArray *oa, struct redisCommand *cmd, int dbid,\n                       robj **argv, int argc, int target)\n{\n    redisOp *op;\n\n    oa->ops = (redisOp*)zrealloc(oa->ops,sizeof(redisOp)*(oa->numops+1), MALLOC_LOCAL);\n    op = oa->ops+oa->numops;\n    op->cmd = cmd;\n    op->dbid = dbid;\n    op->argv = argv;\n    op->argc = argc;\n    op->target = target;\n    oa->numops++;\n    return oa->numops;\n}\n\nvoid redisOpArrayFree(redisOpArray *oa) {\n    while(oa->numops) {\n        int j;\n        redisOp *op;\n\n        oa->numops--;\n        op = oa->ops+oa->numops;\n        for (j = 0; j < op->argc; j++)\n            decrRefCount(op->argv[j]);\n        zfree(op->argv);\n    }\n    zfree(oa->ops);\n    oa->ops = NULL;\n}\n\n/* ====================== Commands lookup and execution ===================== */\n\nstruct redisCommand *lookupCommand(sds name) {\n    return (struct redisCommand*)dictFetchValue(g_pserver->commands, name);\n}\n\nstruct redisCommand *lookupCommandByCString(const char *s) {\n    struct redisCommand *cmd;\n    sds name = sdsnew(s);\n\n    cmd = (struct redisCommand*)dictFetchValue(g_pserver->commands, name);\n    sdsfree(name);\n    return cmd;\n}\n\n/* Lookup the command in the current table, if not found also check in\n * the original table containing the original command names unaffected by\n * keydb.conf rename-command statement.\n *\n * This is used by functions rewriting the argument vector such as\n * rewriteClientCommandVector() in order to set client->cmd pointer\n * correctly even if the command was renamed. */\nstruct redisCommand *lookupCommandOrOriginal(sds name) {\n    struct redisCommand *cmd = (struct redisCommand*)dictFetchValue(g_pserver->commands, name);\n\n    if (!cmd) cmd = (struct redisCommand*)dictFetchValue(g_pserver->orig_commands,name);\n    return cmd;\n}\n\n/* Propagate the specified command (in the context of the specified database id)\n * to AOF and Slaves.\n *\n * flags are an xor between:\n * + PROPAGATE_NONE (no propagation of command at all)\n * + PROPAGATE_AOF (propagate into the AOF file if is enabled)\n * + PROPAGATE_REPL (propagate into the replication link)\n *\n * This should not be used inside commands implementation since it will not\n * wrap the resulting commands in MULTI/EXEC. Use instead alsoPropagate(),\n * preventCommandPropagation(), forceCommandPropagation().\n *\n * However for functions that need to (also) propagate out of the context of a\n * command execution, for example when serving a blocked client, you\n * want to use propagate().\n */\nvoid propagate(struct redisCommand *cmd, int dbid, robj **argv, int argc,\n               int flags)\n{\n    serverAssert(GlobalLocksAcquired());\n    if (!g_pserver->replication_allowed)\n        return;\n\n    /* Propagate a MULTI request once we encounter the first command which\n     * is a write command.\n     * This way we'll deliver the MULTI/..../EXEC block as a whole and\n     * both the AOF and the replication link will have the same consistency\n     * and atomicity guarantees. */\n    if (serverTL->in_exec && !serverTL->propagate_in_transaction)\n        execCommandPropagateMulti(dbid);\n\n    /* This needs to be unreachable since the dataset should be fixed during \n     * client pause, otherwise data may be lossed during a failover. */\n    serverAssert(!(areClientsPaused() && !serverTL->client_pause_in_transaction));\n\n    if (g_pserver->aof_state != AOF_OFF && flags & PROPAGATE_AOF)\n        feedAppendOnlyFile(cmd,dbid,argv,argc);\n    if (flags & PROPAGATE_REPL)\n        replicationFeedSlaves(g_pserver->slaves,dbid,argv,argc);\n}\n\n/* Used inside commands to schedule the propagation of additional commands\n * after the current command is propagated to AOF / Replication.\n *\n * 'cmd' must be a pointer to the Redis command to replicate, dbid is the\n * database ID the command should be propagated into.\n * Arguments of the command to propagate are passed as an array of redis\n * objects pointers of len 'argc', using the 'argv' vector.\n *\n * The function does not take a reference to the passed 'argv' vector,\n * so it is up to the caller to release the passed argv (but it is usually\n * stack allocated).  The function automatically increments ref count of\n * passed objects, so the caller does not need to. */\nvoid alsoPropagate(struct redisCommand *cmd, int dbid, robj **argv, int argc,\n                   int target)\n{\n    robj **argvcopy;\n    int j;\n\n    if (g_pserver->loading) return; /* No propagation during loading. */\n\n    argvcopy = (robj**)zmalloc(sizeof(robj*)*argc, MALLOC_LOCAL);\n    for (j = 0; j < argc; j++) {\n        argvcopy[j] = argv[j];\n        incrRefCount(argv[j]);\n    }\n    redisOpArrayAppend(&g_pserver->also_propagate,cmd,dbid,argvcopy,argc,target);\n}\n\n/* It is possible to call the function forceCommandPropagation() inside a\n * Redis command implementation in order to to force the propagation of a\n * specific command execution into AOF / Replication. */\nvoid forceCommandPropagation(client *c, int flags) {\n    serverAssert(c->cmd->flags & (CMD_WRITE | CMD_MAY_REPLICATE));\n    if (flags & PROPAGATE_REPL) c->flags |= CLIENT_FORCE_REPL;\n    if (flags & PROPAGATE_AOF) c->flags |= CLIENT_FORCE_AOF;\n}\n\n/* Avoid that the executed command is propagated at all. This way we\n * are free to just propagate what we want using the alsoPropagate()\n * API. */\nvoid preventCommandPropagation(client *c) {\n    c->flags |= CLIENT_PREVENT_PROP;\n}\n\n/* AOF specific version of preventCommandPropagation(). */\nvoid preventCommandAOF(client *c) {\n    c->flags |= CLIENT_PREVENT_AOF_PROP;\n}\n\n/* Replication specific version of preventCommandPropagation(). */\nvoid preventCommandReplication(client *c) {\n    c->flags |= CLIENT_PREVENT_REPL_PROP;\n}\n\n/* Log the last command a client executed into the slowlog. */\nvoid slowlogPushCurrentCommand(client *c, struct redisCommand *cmd, ustime_t duration) {\n    /* Some commands may contain sensitive data that should not be available in the slowlog. */\n    if (cmd->flags & CMD_SKIP_SLOWLOG)\n        return;\n\n    /* If command argument vector was rewritten, use the original\n     * arguments. */\n    robj **argv = c->original_argv ? c->original_argv : c->argv;\n    int argc = c->original_argv ? c->original_argc : c->argc;\n    slowlogPushEntryIfNeeded(c,argv,argc,duration);\n}\n\n/* Call() is the core of Redis execution of a command.\n *\n * The following flags can be passed:\n * CMD_CALL_NONE        No flags.\n * CMD_CALL_SLOWLOG     Check command speed and log in the slow log if needed.\n * CMD_CALL_STATS       Populate command stats.\n * CMD_CALL_PROPAGATE_AOF   Append command to AOF if it modified the dataset\n *                          or if the client flags are forcing propagation.\n * CMD_CALL_PROPAGATE_REPL  Send command to slaves if it modified the dataset\n *                          or if the client flags are forcing propagation.\n * CMD_CALL_PROPAGATE   Alias for PROPAGATE_AOF|PROPAGATE_REPL.\n * CMD_CALL_FULL        Alias for SLOWLOG|STATS|PROPAGATE.\n *\n * The exact propagation behavior depends on the client flags.\n * Specifically:\n *\n * 1. If the client flags CLIENT_FORCE_AOF or CLIENT_FORCE_REPL are set\n *    and assuming the corresponding CMD_CALL_PROPAGATE_AOF/REPL is set\n *    in the call flags, then the command is propagated even if the\n *    dataset was not affected by the command.\n * 2. If the client flags CLIENT_PREVENT_REPL_PROP or CLIENT_PREVENT_AOF_PROP\n *    are set, the propagation into AOF or to slaves is not performed even\n *    if the command modified the dataset.\n *\n * Note that regardless of the client flags, if CMD_CALL_PROPAGATE_AOF\n * or CMD_CALL_PROPAGATE_REPL are not set, then respectively AOF or\n * slaves propagation will never occur.\n *\n * Client flags are modified by the implementation of a given command\n * using the following API:\n *\n * forceCommandPropagation(client *c, int flags);\n * preventCommandPropagation(client *c);\n * preventCommandAOF(client *c);\n * preventCommandReplication(client *c);\n *\n */\nvoid call(client *c, int flags) {\n    long long dirty;\n    monotime call_timer;\n    int client_old_flags = c->flags;\n    struct redisCommand *real_cmd = c->cmd;\n    serverAssert(((flags & CMD_CALL_ASYNC) && (c->cmd->flags & CMD_READONLY)) || GlobalLocksAcquired());\n\n    /* We need to transfer async writes before a client's repl state gets changed.  Otherwise\n        we won't be able to propogate them correctly. */\n    if (c->cmd->flags & CMD_CATEGORY_REPLICATION) {\n        flushReplBacklogToClients();\n        ProcessPendingAsyncWrites();\n    }\n\n    /* Initialization: clear the flags that must be set by the command on\n     * demand, and initialize the array for additional commands propagation. */\n    c->flags &= ~(CLIENT_FORCE_AOF|CLIENT_FORCE_REPL|CLIENT_PREVENT_PROP);\n    redisOpArray prev_also_propagate;\n    if (!(flags & CMD_CALL_ASYNC)) {\n        prev_also_propagate = g_pserver->also_propagate;\n        redisOpArrayInit(&g_pserver->also_propagate);\n    }\n\n    /* Call the command. */\n    dirty = g_pserver->dirty;\n    serverTL->prev_err_count = serverTL->stat_total_error_replies;\n    g_pserver->fixed_time_expire++;\n    incrementMvccTstamp();\n    elapsedStart(&call_timer);\n    try {\n        c->cmd->proc(c);\n    } catch (robj_roptr o) {\n        addReply(c, o);\n    } catch (robj *o) {\n        addReply(c, o);\n    } catch (const char *sz) {\n        addReplyError(c, sz);\n    }\n    serverTL->commandsExecuted++;\n    const long duration = elapsedUs(call_timer);\n    c->duration = duration;\n    if (flags & CMD_CALL_ASYNC)\n        dirty = 0;  // dirty is bogus in this case as there's no synchronization\n    else\n        dirty = g_pserver->dirty-dirty;\n    if (dirty < 0) dirty = 0;\n\n    if (dirty)\n        c->mvccCheckpoint = getMvccTstamp();\n\n    /* Update failed command calls if required.\n     * We leverage a static variable (prev_err_count) to retain\n     * the counter across nested function calls and avoid logging\n     * the same error twice. */\n    if ((serverTL->stat_total_error_replies - serverTL->prev_err_count) > 0) {\n        real_cmd->failed_calls++;\n    }\n\n    /* After executing command, we will close the client after writing entire\n     * reply if it is set 'CLIENT_CLOSE_AFTER_COMMAND' flag. */\n    if (c->flags & CLIENT_CLOSE_AFTER_COMMAND) {\n        c->flags &= ~CLIENT_CLOSE_AFTER_COMMAND;\n        c->flags |= CLIENT_CLOSE_AFTER_REPLY;\n    }\n\n    /* When EVAL is called loading the AOF we don't want commands called\n     * from Lua to go into the slowlog or to populate statistics. */\n    if (g_pserver->loading && c->flags & CLIENT_LUA)\n        flags &= ~(CMD_CALL_SLOWLOG | CMD_CALL_STATS);\n\n    /* If the caller is Lua, we want to force the EVAL caller to propagate\n     * the script if the command flag or client flag are forcing the\n     * propagation. */\n    if (c->flags & CLIENT_LUA && g_pserver->lua_caller) {\n        if (c->flags & CLIENT_FORCE_REPL)\n            g_pserver->lua_caller->flags |= CLIENT_FORCE_REPL;\n        if (c->flags & CLIENT_FORCE_AOF)\n            g_pserver->lua_caller->flags |= CLIENT_FORCE_AOF;\n    }\n\n    /* Note: the code below uses the real command that was executed\n     * c->cmd and c->lastcmd may be different, in case of MULTI-EXEC or\n     * re-written commands such as EXPIRE, GEOADD, etc. */\n\n    /* Record the latency this command induced on the main thread.\n     * unless instructed by the caller not to log. (happens when processing\n     * a MULTI-EXEC from inside an AOF). */\n    if (flags & CMD_CALL_SLOWLOG) {\n        const char *latency_event = (real_cmd->flags & CMD_FAST) ?\n                               \"fast-command\" : \"command\";\n        latencyAddSampleIfNeeded(latency_event,duration/1000);\n    }\n\n    /* Log the command into the Slow log if needed.\n     * If the client is blocked we will handle slowlog when it is unblocked. */\n    if ((flags & CMD_CALL_SLOWLOG) && !(c->flags & CLIENT_BLOCKED)) {\n        if (duration >= g_pserver->slowlog_log_slower_than) {\n            AeLocker locker;\n            locker.arm(c);\n            slowlogPushCurrentCommand(c, real_cmd, duration);\n        }\n    }\n\n    /* Send the command to clients in MONITOR mode if applicable.\n     * Administrative commands are considered too dangerous to be shown. */\n    if (!(c->cmd->flags & (CMD_SKIP_MONITOR|CMD_ADMIN))) {\n        robj **argv = c->original_argv ? c->original_argv : c->argv;\n        int argc = c->original_argv ? c->original_argc : c->argc;\n        replicationFeedMonitors(c,g_pserver->monitors,c->db->id,argv,argc);\n    }\n\n    /* Clear the original argv.\n     * If the client is blocked we will handle slowlog when it is unblocked. */\n    if (!(c->flags & CLIENT_BLOCKED))\n        freeClientOriginalArgv(c);\n\n    /* populate the per-command statistics that we show in INFO commandstats. */\n    if (flags & CMD_CALL_STATS) {\n        __atomic_fetch_add(&real_cmd->microseconds, duration, __ATOMIC_RELAXED);\n        __atomic_fetch_add(&real_cmd->calls, 1, __ATOMIC_RELAXED);\n    }\n\n    /* Propagate the command into the AOF and replication link */\n    if (flags & CMD_CALL_PROPAGATE &&\n        (c->flags & CLIENT_PREVENT_PROP) != CLIENT_PREVENT_PROP)\n    {\n        int propagate_flags = PROPAGATE_NONE;\n\n        /* Check if the command operated changes in the data set. If so\n         * set for replication / AOF propagation. */\n        if (dirty) propagate_flags |= (PROPAGATE_AOF|PROPAGATE_REPL);\n\n        /* If the client forced AOF / replication of the command, set\n         * the flags regardless of the command effects on the data set. */\n        if (c->flags & CLIENT_FORCE_REPL) propagate_flags |= PROPAGATE_REPL;\n        if (c->flags & CLIENT_FORCE_AOF) propagate_flags |= PROPAGATE_AOF;\n\n        /* However prevent AOF / replication propagation if the command\n         * implementation called preventCommandPropagation() or similar,\n         * or if we don't have the call() flags to do so. */\n        if (c->flags & CLIENT_PREVENT_REPL_PROP ||\n            !(flags & CMD_CALL_PROPAGATE_REPL))\n                propagate_flags &= ~PROPAGATE_REPL;\n        if (c->flags & CLIENT_PREVENT_AOF_PROP ||\n            !(flags & CMD_CALL_PROPAGATE_AOF))\n                propagate_flags &= ~PROPAGATE_AOF;\n\n        if ((c->cmd->flags & CMD_SKIP_PROPOGATE) && g_pserver->fActiveReplica)\n            propagate_flags &= ~PROPAGATE_REPL;\n\n        /* Call propagate() only if at least one of AOF / replication\n         * propagation is needed. Note that modules commands handle replication\n         * in an explicit way, so we never replicate them automatically. */\n        if (propagate_flags != PROPAGATE_NONE && !(c->cmd->flags & CMD_MODULE))\n            propagate(c->cmd,c->db->id,c->argv,c->argc,propagate_flags);\n    }\n\n    /* Restore the old replication flags, since call() can be executed\n     * recursively. */\n    c->flags &= ~(CLIENT_FORCE_AOF|CLIENT_FORCE_REPL|CLIENT_PREVENT_PROP);\n    c->flags |= client_old_flags &\n        (CLIENT_FORCE_AOF|CLIENT_FORCE_REPL|CLIENT_PREVENT_PROP);\n\n    if (!(flags & CMD_CALL_ASYNC)) {\n        /* Handle the alsoPropagate() API to handle commands that want to propagate\n        * multiple separated commands. Note that alsoPropagate() is not affected\n        * by CLIENT_PREVENT_PROP flag. */\n        if (g_pserver->also_propagate.numops) {\n            int j;\n            redisOp *rop;\n\n            if (flags & CMD_CALL_PROPAGATE) {\n                bool multi_emitted = false;\n                /* Wrap the commands in g_pserver->also_propagate array,\n                * but don't wrap it if we are already in MULTI context,\n                * in case the nested MULTI/EXEC.\n                *\n                * And if the array contains only one command, no need to\n                * wrap it, since the single command is atomic. */\n                if (g_pserver->also_propagate.numops > 1 &&\n                    !(c->cmd->flags & CMD_MODULE) &&\n                    !(c->flags & CLIENT_MULTI) &&\n                    !(flags & CMD_CALL_NOWRAP))\n                {\n                    execCommandPropagateMulti(c->db->id);\n                    multi_emitted = true;\n                }\n                \n                for (j = 0; j < g_pserver->also_propagate.numops; j++) {\n                    rop = &g_pserver->also_propagate.ops[j];\n                    int target = rop->target;\n                    /* Whatever the command wish is, we honor the call() flags. */\n                    if (!(flags&CMD_CALL_PROPAGATE_AOF)) target &= ~PROPAGATE_AOF;\n                    if (!(flags&CMD_CALL_PROPAGATE_REPL)) target &= ~PROPAGATE_REPL;\n                    if (target)\n                        propagate(rop->cmd,rop->dbid,rop->argv,rop->argc,target);\n                }\n\n                if (multi_emitted) {\n                    execCommandPropagateExec(c->db->id);\n                }\n            }\n            redisOpArrayFree(&g_pserver->also_propagate);\n        }\n        \n        g_pserver->also_propagate = prev_also_propagate;\n    }\n\n    /* Client pause takes effect after a transaction has finished. This needs\n     * to be located after everything is propagated. */\n    if (!serverTL->in_exec && serverTL->client_pause_in_transaction) {\n        serverTL->client_pause_in_transaction = 0;\n    }\n\n    /* If the client has keys tracking enabled for client side caching,\n     * make sure to remember the keys it fetched via this command. */\n    if (c->cmd->flags & CMD_READONLY) {\n        client *caller = (c->flags & CLIENT_LUA && g_pserver->lua_caller) ?\n                            g_pserver->lua_caller : c;\n        if (caller->flags & CLIENT_TRACKING &&\n            !(caller->flags & CLIENT_TRACKING_BCAST))\n        {\n            trackingRememberKeys(caller);\n        }\n    }\n\n    __atomic_fetch_add(&g_pserver->stat_numcommands, 1, __ATOMIC_RELAXED);\n    serverTL->fixed_time_expire--;\n    serverTL->prev_err_count = serverTL->stat_total_error_replies;\n\n    if (!(flags & CMD_CALL_ASYNC)) {\n        /* Record peak memory after each command and before the eviction that runs\n        * before the next command. */\n        size_t zmalloc_used = zmalloc_used_memory();\n        if (zmalloc_used > g_pserver->stat_peak_memory)\n            g_pserver->stat_peak_memory = zmalloc_used;\n    }\n}\n\n/* Used when a command that is ready for execution needs to be rejected, due to\n * varios pre-execution checks. it returns the appropriate error to the client.\n * If there's a transaction is flags it as dirty, and if the command is EXEC,\n * it aborts the transaction.\n * Note: 'reply' is expected to end with \\r\\n */\nvoid rejectCommand(client *c, robj *reply, int severity = ERR_CRITICAL) {\n    flagTransaction(c);\n    if (c->cmd) c->cmd->rejected_calls++;\n    if (c->cmd && c->cmd->proc == execCommand) {\n        execCommandAbort(c, szFromObj(reply));\n    }\n    else {\n        /* using addReplyError* rather than addReply so that the error can be logged. */\n        addReplyErrorObject(c, reply, severity);\n    }\n}\n\nvoid lfenceCommand(client *c) {\n    c->mvccCheckpoint = getMvccTstamp();\n    addReply(c, shared.ok);\n}\n\nvoid rejectCommandFormat(client *c, const char *fmt, ...) {\n    if (c->cmd) c->cmd->rejected_calls++;\n    flagTransaction(c);\n    va_list ap;\n    va_start(ap,fmt);\n    sds s = sdscatvprintf(sdsempty(),fmt,ap);\n    va_end(ap);\n    /* Make sure there are no newlines in the string, otherwise invalid protocol\n     * is emitted (The args come from the user, they may contain any character). */\n    sdsmapchars(s, \"\\r\\n\", \"  \",  2);\n    if (c->cmd && c->cmd->proc == execCommand) {\n        execCommandAbort(c, s);\n        sdsfree(s);\n    } else {\n        /* The following frees 's'. */\n        addReplyErrorSds(c, s);\n    }\n}\n\n/* Returns 1 for commands that may have key names in their arguments, but have\n * no pre-determined key positions. */\nstatic int cmdHasMovableKeys(struct redisCommand *cmd) {\n    return (cmd->getkeys_proc && !(cmd->flags & CMD_MODULE)) ||\n            cmd->flags & CMD_MODULE_GETKEYS;\n}\n\n/* If this function gets called we already read a whole\n * command, arguments are in the client argv/argc fields.\n * processCommand() execute the command or prepare the\n * server for a bulk read from the client.\n *\n * If C_OK is returned the client is still alive and valid and\n * other operations can be performed by the caller. Otherwise\n * if C_ERR is returned the client was destroyed (i.e. after QUIT). */\nint processCommand(client *c, int callFlags) {\n    AssertCorrectThread(c);\n    serverAssert((callFlags & CMD_CALL_ASYNC) || GlobalLocksAcquired());\n    if (!g_pserver->lua_timedout) {\n        /* Both EXEC and EVAL call call() directly so there should be\n         * no way in_exec or in_eval or propagate_in_transaction is 1.\n         * That is unless lua_timedout, in which case client may run\n         * some commands. Also possible that some other thread set\n         * propagate_in_transaction if this is an async command. */\n        serverAssert(!serverTL->propagate_in_transaction);\n        serverAssert(!serverTL->in_exec);\n        serverAssert(!serverTL->in_eval);\n    }\n\n    if (moduleHasCommandFilters())\n    {\n        moduleCallCommandFilters(c);\n    }\n\n    /* The QUIT command is handled separately. Normal command procs will\n     * go through checking for replication and QUIT will cause trouble\n     * when FORCE_REPLICATION is enabled and would be implemented in\n     * a regular command proc. */\n    if (!strcasecmp((const char*)ptrFromObj(c->argv[0]),\"quit\")) {\n        addReply(c,shared.ok);\n        c->flags |= CLIENT_CLOSE_AFTER_REPLY;\n        return C_ERR;\n    }\n\n    /* Now lookup the command and check ASAP about trivial error conditions\n     * such as wrong arity, bad command name and so forth. */\n    c->cmd = c->lastcmd = lookupCommand((sds)ptrFromObj(c->argv[0]));\n    if (!c->cmd) {\n        sds args = sdsempty();\n        int i;\n        for (i=1; i < c->argc && sdslen(args) < 128; i++)\n            args = sdscatprintf(args, \"`%.*s`, \", 128-(int)sdslen(args), (char*)ptrFromObj(c->argv[i]));\n        rejectCommandFormat(c,\"unknown command `%s`, with args beginning with: %s\",\n            (char*)ptrFromObj(c->argv[0]), args);\n        sdsfree(args);\n        return C_OK;\n    } else if ((c->cmd->arity > 0 && c->cmd->arity != c->argc) ||\n               (c->argc < -c->cmd->arity)) {\n        rejectCommandFormat(c,\"wrong number of arguments for '%s' command\",\n            c->cmd->name);\n        return C_OK;\n    }\n\n    int is_read_command = (c->cmd->flags & CMD_READONLY) ||\n                           (c->cmd->proc == execCommand && (c->mstate.cmd_flags & CMD_READONLY));\n    int is_write_command = (c->cmd->flags & CMD_WRITE) ||\n                           (c->cmd->proc == execCommand && (c->mstate.cmd_flags & CMD_WRITE));\n    int is_denyoom_command = (c->cmd->flags & CMD_DENYOOM) ||\n                             (c->cmd->proc == execCommand && (c->mstate.cmd_flags & CMD_DENYOOM));\n    int is_denystale_command = !(c->cmd->flags & CMD_STALE) ||\n                               (c->cmd->proc == execCommand && (c->mstate.cmd_inv_flags & CMD_STALE));\n    int is_denyloading_command = !(c->cmd->flags & CMD_LOADING) ||\n                                 (c->cmd->proc == execCommand && (c->mstate.cmd_inv_flags & CMD_LOADING));\n    int is_may_replicate_command = (c->cmd->flags & (CMD_WRITE | CMD_MAY_REPLICATE)) ||\n                                   (c->cmd->proc == execCommand && (c->mstate.cmd_flags & (CMD_WRITE | CMD_MAY_REPLICATE)));\n\n    if (authRequired(c)) {\n        /* AUTH and HELLO and no auth commands are valid even in\n         * non-authenticated state. */\n        if (!(c->cmd->flags & CMD_NO_AUTH)) {\n            rejectCommand(c,shared.noautherr);\n            return C_OK;\n        }\n    }\n\n    /* Check if the user can run this command according to the current\n     * ACLs. */\n    int acl_errpos;\n    int acl_retval = ACLCheckAllPerm(c,&acl_errpos);\n    if (acl_retval != ACL_OK) {\n        addACLLogEntry(c,acl_retval,acl_errpos,NULL);\n        switch (acl_retval) {\n        case ACL_DENIED_CMD:\n            rejectCommandFormat(c,\n                \"-NOPERM this user has no permissions to run \"\n                \"the '%s' command or its subcommand\", c->cmd->name);\n            break;\n        case ACL_DENIED_KEY:\n            rejectCommandFormat(c,\n                \"-NOPERM this user has no permissions to access \"\n                \"one of the keys used as arguments\");\n            break;\n        case ACL_DENIED_CHANNEL:\n            rejectCommandFormat(c,\n                \"-NOPERM this user has no permissions to access \"\n                \"one of the channels used as arguments\");\n            break;\n        default:\n            rejectCommandFormat(c, \"no permission\");\n            break;\n        }\n        return C_OK;\n    }\n\n    /* If cluster is enabled perform the cluster redirection here.\n     * However we don't perform the redirection if:\n     * 1) The sender of this command is our master.\n     * 2) The command has no key arguments. */\n    if (g_pserver->cluster_enabled &&\n        !(c->flags & CLIENT_MASTER) &&\n        !(c->flags & CLIENT_LUA &&\n          g_pserver->lua_caller->flags & CLIENT_MASTER) &&\n        !(!cmdHasMovableKeys(c->cmd) && c->cmd->firstkey == 0 &&\n          c->cmd->proc != execCommand))\n    {\n        int hashslot;\n        int error_code;\n        clusterNode *n = getNodeByQuery(c,c->cmd,c->argv,c->argc,\n                                        &hashslot,&error_code);\n        if (n == NULL || n != g_pserver->cluster->myself) {\n            if (c->cmd->proc == execCommand) {\n                discardTransaction(c);\n            } else {\n                flagTransaction(c);\n            }\n            clusterRedirectClient(c,n,hashslot,error_code);\n            c->cmd->rejected_calls++;\n            return C_OK;\n        }\n    }\n\n    /* Handle the maxmemory directive.\n     *\n     * Note that we do not want to reclaim memory if we are here re-entering\n     * the event loop since there is a busy Lua script running in timeout\n     * condition, to avoid mixing the propagation of scripts with the\n     * propagation of DELs due to eviction. */\n    if (g_pserver->maxmemory && !g_pserver->lua_timedout && !(callFlags & CMD_CALL_ASYNC)) {\n        int out_of_memory = (performEvictions(false /*fPreSnapshot*/) == EVICT_FAIL);\n        /* freeMemoryIfNeeded may flush replica output buffers. This may result\n         * into a replica, that may be the active client, to be freed. */\n        if (serverTL->current_client == NULL) return C_ERR;\n\n        int reject_cmd_on_oom = is_denyoom_command;\n        /* If client is in MULTI/EXEC context, queuing may consume an unlimited\n         * amount of memory, so we want to stop that.\n         * However, we never want to reject DISCARD, or even EXEC (unless it\n         * contains denied commands, in which case is_denyoom_command is already\n         * set. */\n        if (c->flags & CLIENT_MULTI &&\n            c->cmd->proc != execCommand &&\n            c->cmd->proc != discardCommand &&\n            c->cmd->proc != resetCommand) {\n            reject_cmd_on_oom = 1;\n        }\n\n        if (out_of_memory && reject_cmd_on_oom) {\n            rejectCommand(c, shared.oomerr);\n            return C_OK;\n        }\n\n        /* Save out_of_memory result at script start, otherwise if we check OOM\n         * until first write within script, memory used by lua stack and\n         * arguments might interfere. */\n        if (c->cmd->proc == evalCommand || c->cmd->proc == evalShaCommand) {\n            g_pserver->lua_oom = out_of_memory;\n        }\n    }\n\n    /* Make sure to use a reasonable amount of memory for client side\n     * caching metadata. */\n    if (g_pserver->tracking_clients) trackingLimitUsedSlots();\n\n    \n    /* Don't accept write commands if there are problems persisting on disk\n        * and if this is a master instance. */\n    int deny_write_type = writeCommandsDeniedByDiskError();\n    if (deny_write_type != DISK_ERROR_TYPE_NONE &&\n        listLength(g_pserver->masters) == 0 &&\n        (is_write_command ||c->cmd->proc == pingCommand))\n    {\n        if (deny_write_type == DISK_ERROR_TYPE_RDB)\n            rejectCommand(c, shared.bgsaveerr);\n        else\n            rejectCommandFormat(c,\n                \"-MISCONF Errors writing to the AOF file: %s\",\n                strerror(g_pserver->aof_last_write_errno));\n        return C_OK;\n    }    \n\n    /* Don't accept write commands if there are not enough good slaves and\n    * user configured the min-slaves-to-write option. */\n    if (listLength(g_pserver->masters) == 0 &&\n        g_pserver->repl_min_slaves_to_write &&\n        g_pserver->repl_min_slaves_max_lag &&\n        is_write_command &&\n        g_pserver->repl_good_slaves_count < g_pserver->repl_min_slaves_to_write)\n    {\n        rejectCommand(c, shared.noreplicaserr);\n        return C_OK;\n    }\n\n    /* Don't accept write commands if this is a read only replica. But\n    * accept write commands if this is our master. */\n    if (listLength(g_pserver->masters) && g_pserver->repl_slave_ro &&\n        !(c->flags & CLIENT_MASTER) &&\n        is_write_command)\n    {\n        rejectCommand(c, shared.roslaveerr);\n        return C_OK;\n    }\n\n    /* Only allow a subset of commands in the context of Pub/Sub if the\n     * connection is in RESP2 mode. With RESP3 there are no limits. */\n    if ((c->flags & CLIENT_PUBSUB && c->resp == 2) &&\n        c->cmd->proc != pingCommand &&\n        c->cmd->proc != subscribeCommand &&\n        c->cmd->proc != unsubscribeCommand &&\n        c->cmd->proc != psubscribeCommand &&\n        c->cmd->proc != punsubscribeCommand &&\n        c->cmd->proc != resetCommand) {\n        rejectCommandFormat(c,\n            \"Can't execute '%s': only (P)SUBSCRIBE / \"\n            \"(P)UNSUBSCRIBE / PING / QUIT / RESET are allowed in this context\",\n            c->cmd->name);\n        return C_OK;\n    }\n\n    if (listLength(g_pserver->masters))\n    {\n        /* Only allow commands with flag \"t\", such as INFO, SLAVEOF and so on,\n        * when replica-serve-stale-data is no and we are a replica with a broken\n        * link with master. */\n        if (FBrokenLinkToMaster() &&\n            g_pserver->repl_serve_stale_data == 0 &&\n            is_denystale_command &&\n            !(g_pserver->fActiveReplica && c->cmd->proc == syncCommand)\n            && !FInReplicaReplay())\n        {\n            rejectCommand(c, shared.masterdownerr);\n            return C_OK;\n        }\n    }\n\n    /* Loading DB? Return an error if the command has not the\n     * CMD_LOADING flag. */\n    if (g_pserver->loading && is_denyloading_command) {\n        /* Active Replicas can execute read only commands, and optionally write commands */\n        if (!(g_pserver->loading == LOADING_REPLICATION && g_pserver->fActiveReplica && ((c->cmd->flags & CMD_READONLY) || g_pserver->fWriteDuringActiveLoad)))\n        {\n            rejectCommand(c, shared.loadingerr, ERR_WARNING);\n            return C_OK;\n        }\n    }\n\n    /* Lua script too slow? Only allow a limited number of commands.\n     * Note that we need to allow the transactions commands, otherwise clients\n     * sending a transaction with pipelining without error checking, may have\n     * the MULTI plus a few initial commands refused, then the timeout\n     * condition resolves, and the bottom-half of the transaction gets\n     * executed, see Github PR #7022. */\n    if (g_pserver->lua_timedout &&\n          c->cmd->proc != authCommand &&\n          c->cmd->proc != helloCommand &&\n          c->cmd->proc != replconfCommand &&\n          c->cmd->proc != multiCommand &&\n          c->cmd->proc != discardCommand &&\n          c->cmd->proc != watchCommand &&\n          c->cmd->proc != unwatchCommand &&\n          c->cmd->proc != resetCommand &&\n        !(c->cmd->proc == shutdownCommand &&\n          c->argc == 2 &&\n          tolower(((char*)ptrFromObj(c->argv[1]))[0]) == 'n') &&\n        !(c->cmd->proc == scriptCommand &&\n          c->argc == 2 &&\n          tolower(((char*)ptrFromObj(c->argv[1]))[0]) == 'k'))\n    {\n        rejectCommand(c, shared.slowscripterr);\n        return C_OK;\n    }\n\n    /* Prevent a replica from sending commands that access the keyspace.\n     * The main objective here is to prevent abuse of client pause check\n     * from which replicas are exempt. */\n    if ((c->flags & CLIENT_SLAVE) && (is_may_replicate_command || is_write_command || is_read_command)) {\n        rejectCommandFormat(c, \"Replica can't interract with the keyspace\");\n        return C_OK;\n    }\n\n    /* If the server is paused, block the client until\n     * the pause has ended. Replicas are never paused. */\n    if (!(c->flags & CLIENT_SLAVE) && \n        ((g_pserver->client_pause_type == CLIENT_PAUSE_ALL) ||\n        (g_pserver->client_pause_type == CLIENT_PAUSE_WRITE && is_may_replicate_command)))\n    {\n        c->bpop.timeout = 0;\n        blockClient(c,BLOCKED_PAUSE);\n        return C_OK;       \n    }\n\n    /* Exec the command */\n    if (c->flags & CLIENT_MULTI &&\n        c->cmd->proc != execCommand && c->cmd->proc != discardCommand &&\n        c->cmd->proc != multiCommand && c->cmd->proc != watchCommand &&\n        c->cmd->proc != resetCommand)\n    {\n        queueMultiCommand(c);\n        addReply(c,shared.queued);\n    } else {\n        /* If the command was replication or admin related we *must* flush our buffers first.  This is in case\n            something happens which would modify what we would send to replicas */\n        if (c->cmd->flags & (CMD_MODULE | CMD_ADMIN))\n            flushReplBacklogToClients();\n\n        if (c->flags & CLIENT_AUDIT_LOGGING){\n            getKeysResult result = GETKEYS_RESULT_INIT;\n            int numkeys = getKeysFromCommand(c->cmd, c->argv, c->argc, &result);\n            int *keyindex = result.keys;\n\n            sds str = sdsempty();\n            for (int j = 0; j < numkeys; j++) {\n                str = sdscatsds(str, (sds)ptrFromObj(c->argv[keyindex[j]]));\n                str = sdscat(str, \" \");\n            }\n        \n            if (numkeys > 0)\n            {\n                serverLog(LL_NOTICE, \"Audit Log: %s, cmd %s, keys: %s\", c->fprint, c->cmd->name, str);\n            } else {\n                serverLog(LL_NOTICE, \"Audit Log: %s, cmd %s\", c->fprint, c->cmd->name);\n            }\n            sdsfree(str);\n        }\n\n        call(c,callFlags);\n        c->woff = g_pserver->master_repl_offset;\n\n        if (c->cmd->flags & (CMD_MODULE | CMD_ADMIN))\n            flushReplBacklogToClients();\n        \n        if (listLength(g_pserver->ready_keys))\n            handleClientsBlockedOnKeys();\n    }\n\n    return C_OK;\n}\n\nbool client::postFunction(std::function<void(client *)> fn, bool fLock) {\n    this->casyncOpsPending++;\n    return aePostFunction(g_pserver->rgthreadvar[this->iel].el, [this, fn]{\n        std::lock_guard<decltype(this->lock)> lock(this->lock);\n        fn(this);\n        --casyncOpsPending;\n    }, fLock) == AE_OK;\n}\n\nstd::vector<robj_sharedptr> clientArgs(client *c) {\n    std::vector<robj_sharedptr> args;\n    for (int j = 0; j < c->argc; j++) {\n        args.push_back(robj_sharedptr(c->argv[j]));\n    }\n    return args;\n}\n\nbool client::asyncCommand(std::function<void(const redisDbPersistentDataSnapshot *, const std::vector<robj_sharedptr> &)> &&mainFn, \n                            std::function<void(const redisDbPersistentDataSnapshot *)> &&postFn) \n{\n    serverAssert(FCorrectThread(this));\n    if (serverTL->in_eval)\n        return false;   // we cannot block clients in EVAL\n    const redisDbPersistentDataSnapshot *snapshot = nullptr;\n    if (!(this->flags & (CLIENT_MULTI | CLIENT_BLOCKED)))\n        snapshot = this->db->createSnapshot(this->mvccCheckpoint, false /* fOptional */);\n    if (snapshot == nullptr) {\n        return false;\n    }\n    aeEventLoop *el = serverTL->el;\n    blockClient(this, BLOCKED_ASYNC);\n    g_pserver->asyncworkqueue->AddWorkFunction([el, this, mainFn, postFn, snapshot] {\n        std::vector<robj_sharedptr> args = clientArgs(this);\n        aePostFunction(el, [this, mainFn, postFn, snapshot, args] {\n            aeReleaseLock();\n            std::unique_lock<decltype(this->lock)> lock(this->lock);\n            AeLocker locker;\n            locker.arm(this);\n            unblockClient(this);\n            mainFn(snapshot, args);\n            locker.disarm();\n            lock.unlock();\n            if (postFn)\n                postFn(snapshot);\n            this->db->endSnapshotAsync(snapshot);\n            aeAcquireLock();\n        });\n    });\n    return true;\n}\n\n/* ====================== Error lookup and execution ===================== */\n\nvoid incrementErrorCount(const char *fullerr, size_t namelen) {\n    struct redisError *error = (struct redisError*)raxFind(g_pserver->errors,(unsigned char*)fullerr,namelen);\n    if (error == raxNotFound) {\n        error = (struct redisError*)zmalloc(sizeof(*error));\n        error->count = 0;\n        raxInsert(g_pserver->errors,(unsigned char*)fullerr,namelen,error,NULL);\n    }\n    error->count++;\n}\n\n/*================================== Shutdown =============================== */\n\n/* Close listening sockets. Also unlink the unix domain socket if\n * unlink_unix_socket is non-zero. */\nvoid closeListeningSockets(int unlink_unix_socket) {\n    int j;\n\n    for (int iel = 0; iel < cserver.cthreads; ++iel)\n    {\n        for (j = 0; j < g_pserver->rgthreadvar[iel].ipfd.count; j++) \n            close(g_pserver->rgthreadvar[iel].ipfd.fd[j]);\n        for (j = 0; j < g_pserver->rgthreadvar[iel].tlsfd.count; j++)\n            close(g_pserver->rgthreadvar[iel].tlsfd.fd[j]);\n    }\n    if (g_pserver->sofd != -1) close(g_pserver->sofd);\n    if (g_pserver->cluster_enabled)\n        for (j = 0; j < g_pserver->cfd.count; j++) close(g_pserver->cfd.fd[j]);\n    if (unlink_unix_socket && g_pserver->unixsocket) {\n        serverLog(LL_NOTICE,\"Removing the unix socket file.\");\n        unlink(g_pserver->unixsocket); /* don't care if this fails */\n    }\n}\n\nint prepareForShutdown(int flags) {\n    /* When SHUTDOWN is called while the server is loading a dataset in\n     * memory we need to make sure no attempt is performed to save\n     * the dataset on shutdown (otherwise it could overwrite the current DB\n     * with half-read data).\n     *\n     * Also when in Sentinel mode clear the SAVE flag and force NOSAVE. */\n    if (g_pserver->loading || g_pserver->sentinel_mode)\n        flags = (flags & ~SHUTDOWN_SAVE) | SHUTDOWN_NOSAVE;\n\n    int save = flags & SHUTDOWN_SAVE;\n    int nosave = flags & SHUTDOWN_NOSAVE;\n\n    serverLog(LL_WARNING,\"User requested shutdown...\");\n    if (cserver.supervised_mode == SUPERVISED_SYSTEMD)\n        redisCommunicateSystemd(\"STOPPING=1\\n\");\n\n    /* Kill all the Lua debugger forked sessions. */\n    ldbKillForkedSessions();\n\n    /* Kill the saving child if there is a background saving in progress.\n       We want to avoid race conditions, for instance our saving child may\n       overwrite the synchronous saving did by SHUTDOWN. */\n    if (g_pserver->FRdbSaveInProgress()) {\n        serverLog(LL_WARNING,\"There is a child saving an .rdb. Killing it!\");\n        killRDBChild();\n        /* Note that, in killRDBChild normally has backgroundSaveDoneHandler\n         * doing it's cleanup, but in this case this code will not be reached,\n         * so we need to call rdbRemoveTempFile which will close fd(in order\n         * to unlink file actully) in background thread.\n         * The temp rdb file fd may won't be closed when redis exits quickly,\n         * but OS will close this fd when process exits. */\n        rdbRemoveTempFile(g_pserver->rdbThreadVars.tmpfileNum, 0);\n    }\n\n    /* Kill module child if there is one. */\n    if (g_pserver->child_type == CHILD_TYPE_MODULE) {\n        serverLog(LL_WARNING,\"There is a module fork child. Killing it!\");\n        TerminateModuleForkChild(g_pserver->child_pid,0);\n    }\n\n    if (g_pserver->aof_state != AOF_OFF) {\n        /* Kill the AOF saving child as the AOF we already have may be longer\n         * but contains the full dataset anyway. */\n        if (g_pserver->child_type == CHILD_TYPE_AOF) {\n            /* If we have AOF enabled but haven't written the AOF yet, don't\n             * shutdown or else the dataset will be lost. */\n            if (g_pserver->aof_state == AOF_WAIT_REWRITE) {\n                serverLog(LL_WARNING, \"Writing initial AOF, can't exit.\");\n                return C_ERR;\n            }\n            serverLog(LL_WARNING,\n                \"There is a child rewriting the AOF. Killing it!\");\n            killAppendOnlyChild();\n        }\n        /* Append only file: flush buffers and fsync() the AOF at exit */\n        serverLog(LL_NOTICE,\"Calling fsync() on the AOF file.\");\n        flushAppendOnlyFile(1);\n        if (redis_fsync(g_pserver->aof_fd) == -1) {\n            serverLog(LL_WARNING,\"Fail to fsync the AOF file: %s.\",\n                                 strerror(errno));\n        }\n    }\n\n    /* Create a new RDB file before exiting. */\n    if ((g_pserver->saveparamslen > 0 && !nosave) || save) {\n        serverLog(LL_NOTICE,\"Saving the final RDB snapshot before exiting.\");\n        if (cserver.supervised_mode == SUPERVISED_SYSTEMD)\n            redisCommunicateSystemd(\"STATUS=Saving the final RDB snapshot\\n\");\n        /* Snapshotting. Perform a SYNC SAVE and exit */\n        rdbSaveInfo rsi, *rsiptr;\n        rsiptr = rdbPopulateSaveInfo(&rsi);\n        if (rdbSave(nullptr, rsiptr) != C_OK) {\n            /* Ooops.. error saving! The best we can do is to continue\n             * operating. Note that if there was a background saving process,\n             * in the next cron() Redis will be notified that the background\n             * saving aborted, handling special stuff like slaves pending for\n             * synchronization... */\n            serverLog(LL_WARNING,\"Error trying to save the DB, can't exit.\");\n            if (cserver.supervised_mode == SUPERVISED_SYSTEMD)\n                redisCommunicateSystemd(\"STATUS=Error trying to save the DB, can't exit.\\n\");\n            return C_ERR;\n        }\n\n        // Also Dump To FLASH if Applicable\n        for (int idb = 0; idb < cserver.dbnum; ++idb) {\n            if (g_pserver->db[idb]->processChanges(false))\n                g_pserver->db[idb]->commitChanges();\n        }\n        saveMasterStatusToStorage(true);\n    }\n\n    /* Fire the shutdown modules event. */\n    moduleFireServerEvent(REDISMODULE_EVENT_SHUTDOWN,0,NULL);\n\n    /* Remove the pid file if possible and needed. */\n    if (cserver.daemonize || cserver.pidfile) {\n        serverLog(LL_NOTICE,\"Removing the pid file.\");\n        unlink(cserver.pidfile);\n    }\n\n    if (g_pserver->repl_batch_idxStart >= 0) {\n        flushReplBacklogToClients();\n        g_pserver->repl_batch_offStart = -1;\n        g_pserver->repl_batch_idxStart = -1;\n    }\n\n    /* Best effort flush of replica output buffers, so that we hopefully\n     * send them pending writes. */\n    flushSlavesOutputBuffers();\n    g_pserver->repl_batch_idxStart = -1;\n    g_pserver->repl_batch_offStart = -1;\n\n    /* Close the listening sockets. Apparently this allows faster restarts. */\n    closeListeningSockets(1);\n\n    if (g_pserver->asyncworkqueue)\n    {\n        aeReleaseLock();\n        g_pserver->asyncworkqueue->shutdown();\n        aeAcquireLock();\n    }\n\n    for (int iel = 0; iel < cserver.cthreads; ++iel)\n    {\n        aePostFunction(g_pserver->rgthreadvar[iel].el, [iel]{\n            g_pserver->rgthreadvar[iel].el->stop = 1;\n        });\n    }\n\n    serverLog(LL_WARNING,\"%s is now ready to exit, bye bye...\",\n        g_pserver->sentinel_mode ? \"Sentinel\" : \"KeyDB\");\n\n    return C_OK;\n}\n\n/*================================== Commands =============================== */\n\n/* Sometimes Redis cannot accept write commands because there is a persistence\n * error with the RDB or AOF file, and Redis is configured in order to stop\n * accepting writes in such situation. This function returns if such a\n * condition is active, and the type of the condition.\n *\n * Function return values:\n *\n * DISK_ERROR_TYPE_NONE:    No problems, we can accept writes.\n * DISK_ERROR_TYPE_AOF:     Don't accept writes: AOF errors.\n * DISK_ERROR_TYPE_RDB:     Don't accept writes: RDB errors.\n */\nint writeCommandsDeniedByDiskError(void) {\n    if (g_pserver->stop_writes_on_bgsave_err &&\n        g_pserver->saveparamslen > 0 &&\n        g_pserver->lastbgsave_status == C_ERR)\n    {\n        return DISK_ERROR_TYPE_RDB;\n    } else if (g_pserver->aof_state != AOF_OFF) {\n        if (g_pserver->aof_last_write_status == C_ERR) {\n            return DISK_ERROR_TYPE_AOF;\n        }\n        /* AOF fsync error. */\n        int aof_bio_fsync_status;\n        atomicGet(g_pserver->aof_bio_fsync_status,aof_bio_fsync_status);\n        if (aof_bio_fsync_status == C_ERR) {\n            atomicGet(g_pserver->aof_bio_fsync_errno,g_pserver->aof_last_write_errno);\n            return DISK_ERROR_TYPE_AOF;\n        }\n    }\n\n    return DISK_ERROR_TYPE_NONE;\n}\n\n/* The PING command. It works in a different way if the client is in\n * in Pub/Sub mode. */\nvoid pingCommand(client *c) {\n    /* The command takes zero or one arguments. */\n    if (c->argc > 2) {\n        addReplyErrorFormat(c,\"wrong number of arguments for '%s' command\",\n            c->cmd->name);\n        return;\n    }\n\n    if (g_pserver->soft_shutdown && !(c->flags & CLIENT_IGNORE_SOFT_SHUTDOWN)) {\n        addReplyError(c, \"-SHUTDOWN PENDING\");\n        return;\n    }\n\n    if (c->flags & CLIENT_PUBSUB && c->resp == 2) {\n        addReply(c,shared.mbulkhdr[2]);\n        addReplyBulkCBuffer(c,\"pong\",4);\n        if (c->argc == 1)\n            addReplyBulkCBuffer(c,\"\",0);\n        else\n            addReplyBulk(c,c->argv[1]);\n    } else {\n        if (c->argc == 1)\n            addReply(c,shared.pong);\n        else\n            addReplyBulk(c,c->argv[1]);\n    }\n}\n\nvoid echoCommand(client *c) {\n    addReplyBulk(c,c->argv[1]);\n}\n\nvoid timeCommand(client *c) {\n    struct timeval tv;\n\n    /* gettimeofday() can only fail if &tv is a bad address so we\n     * don't check for errors. */\n    gettimeofday(&tv,NULL);\n    addReplyArrayLen(c,2);\n    addReplyBulkLongLong(c,tv.tv_sec);\n    addReplyBulkLongLong(c,tv.tv_usec);\n}\n\n/* Helper function for addReplyCommand() to output flags. */\nint addReplyCommandFlag(client *c, struct redisCommand *cmd, int f, const char *reply) {\n    if (cmd->flags & f) {\n        addReplyStatus(c, reply);\n        return 1;\n    }\n    return 0;\n}\n\n/* Output the representation of a Redis command. Used by the COMMAND command. */\nvoid addReplyCommand(client *c, struct redisCommand *cmd) {\n    if (!cmd) {\n        addReplyNull(c);\n    } else {\n        /* We are adding: command name, arg count, flags, first, last, offset, categories */\n        addReplyArrayLen(c, 7);\n        addReplyBulkCString(c, cmd->name);\n        addReplyLongLong(c, cmd->arity);\n\n        int flagcount = 0;\n        void *flaglen = addReplyDeferredLen(c);\n        flagcount += addReplyCommandFlag(c,cmd,CMD_WRITE, \"write\");\n        flagcount += addReplyCommandFlag(c,cmd,CMD_READONLY, \"readonly\");\n        flagcount += addReplyCommandFlag(c,cmd,CMD_DENYOOM, \"denyoom\");\n        flagcount += addReplyCommandFlag(c,cmd,CMD_ADMIN, \"admin\");\n        flagcount += addReplyCommandFlag(c,cmd,CMD_PUBSUB, \"pubsub\");\n        flagcount += addReplyCommandFlag(c,cmd,CMD_NOSCRIPT, \"noscript\");\n        flagcount += addReplyCommandFlag(c,cmd,CMD_RANDOM, \"random\");\n        flagcount += addReplyCommandFlag(c,cmd,CMD_SORT_FOR_SCRIPT,\"sort_for_script\");\n        flagcount += addReplyCommandFlag(c,cmd,CMD_LOADING, \"loading\");\n        flagcount += addReplyCommandFlag(c,cmd,CMD_STALE, \"stale\");\n        flagcount += addReplyCommandFlag(c,cmd,CMD_SKIP_MONITOR, \"skip_monitor\");\n        flagcount += addReplyCommandFlag(c,cmd,CMD_SKIP_SLOWLOG, \"skip_slowlog\");\n        flagcount += addReplyCommandFlag(c,cmd,CMD_ASKING, \"asking\");\n        flagcount += addReplyCommandFlag(c,cmd,CMD_FAST, \"fast\");\n        flagcount += addReplyCommandFlag(c,cmd,CMD_NO_AUTH, \"no_auth\");\n        flagcount += addReplyCommandFlag(c,cmd,CMD_MAY_REPLICATE, \"may_replicate\");\n        if (cmdHasMovableKeys(cmd)) {\n            addReplyStatus(c, \"movablekeys\");\n            flagcount += 1;\n        }\n        setDeferredSetLen(c, flaglen, flagcount);\n\n        addReplyLongLong(c, cmd->firstkey);\n        addReplyLongLong(c, cmd->lastkey);\n        addReplyLongLong(c, cmd->keystep);\n\n        addReplyCommandCategories(c,cmd);\n    }\n}\n\n/* COMMAND <subcommand> <args> */\nvoid commandCommand(client *c) {\n    dictIterator *di;\n    dictEntry *de;\n\n    if (c->argc == 2 && !strcasecmp((const char*)ptrFromObj(c->argv[1]),\"help\")) {\n        const char *help[] = {\n\"(no subcommand)\",\n\"    Return details about all KeyDB commands.\",\n\"COUNT\",\n\"    Return the total number of commands in this KeyDB server.\",\n\"GETKEYS <full-command>\",\n\"    Return the keys from a full KeyDB command.\",\n\"INFO [<command-name> ...]\",\n\"    Return details about multiple KeyDB commands.\",\nNULL\n        };\n        addReplyHelp(c, help);\n    } else if (c->argc == 1) {\n        addReplyArrayLen(c, dictSize(g_pserver->commands));\n        di = dictGetIterator(g_pserver->commands);\n        while ((de = dictNext(di)) != NULL) {\n            addReplyCommand(c, (redisCommand*)dictGetVal(de));\n        }\n        dictReleaseIterator(di);\n    } else if (!strcasecmp((const char*)ptrFromObj(c->argv[1]), \"info\")) {\n        int i;\n        addReplyArrayLen(c, c->argc-2);\n        for (i = 2; i < c->argc; i++) {\n            addReplyCommand(c, (redisCommand*)dictFetchValue(g_pserver->commands, ptrFromObj(c->argv[i])));\n        }\n    } else if (!strcasecmp((const char*)ptrFromObj(c->argv[1]), \"count\") && c->argc == 2) {\n        addReplyLongLong(c, dictSize(g_pserver->commands));\n    } else if (!strcasecmp((const char*)ptrFromObj(c->argv[1]),\"getkeys\") && c->argc >= 3) {\n        struct redisCommand *cmd = (redisCommand*)lookupCommand((sds)ptrFromObj(c->argv[2]));\n        getKeysResult result = GETKEYS_RESULT_INIT;\n        int j;\n\n        if (!cmd) {\n            addReplyError(c,\"Invalid command specified\");\n            return;\n        } else if (cmd->getkeys_proc == NULL && cmd->firstkey == 0) {\n            addReplyError(c,\"The command has no key arguments\");\n            return;\n        } else if ((cmd->arity > 0 && cmd->arity != c->argc-2) ||\n                   ((c->argc-2) < -cmd->arity))\n        {\n            addReplyError(c,\"Invalid number of arguments specified for command\");\n            return;\n        }\n\n        if (!getKeysFromCommand(cmd,c->argv+2,c->argc-2,&result)) {\n            addReplyError(c,\"Invalid arguments specified for command\");\n        } else {\n            addReplyArrayLen(c,result.numkeys);\n            for (j = 0; j < result.numkeys; j++) addReplyBulk(c,c->argv[result.keys[j]+2]);\n        }\n        getKeysFreeResult(&result);\n    } else {\n        addReplySubcommandSyntaxError(c);\n    }\n}\n\n/* Convert an amount of bytes into a human readable string in the form\n * of 100B, 2G, 100M, 4K, and so forth. */\nvoid bytesToHuman(char *s, unsigned long long n, size_t bufsize) {\n    double d;\n\n    if (n < 1024) {\n        /* Bytes */\n        snprintf(s,bufsize,\"%lluB\",n);\n    } else if (n < (1024*1024)) {\n        d = (double)n/(1024);\n        snprintf(s,bufsize,\"%.2fK\",d);\n    } else if (n < (1024LL*1024*1024)) {\n        d = (double)n/(1024*1024);\n        snprintf(s,bufsize,\"%.2fM\",d);\n    } else if (n < (1024LL*1024*1024*1024)) {\n        d = (double)n/(1024LL*1024*1024);\n        snprintf(s,bufsize,\"%.2fG\",d);\n    } else if (n < (1024LL*1024*1024*1024*1024)) {\n        d = (double)n/(1024LL*1024*1024*1024);\n        snprintf(s,bufsize,\"%.2fT\",d);\n    } else if (n < (1024LL*1024*1024*1024*1024*1024)) {\n        d = (double)n/(1024LL*1024*1024*1024*1024);\n        snprintf(s,bufsize,\"%.2fP\",d);\n    } else {\n        /* Let's hope we never need this */\n        snprintf(s,bufsize,\"%lluB\",n);\n    }\n}\n\n/* Characters we sanitize on INFO output to maintain expected format. */\nstatic char unsafe_info_chars[] = \"#:\\n\\r\";\nstatic char unsafe_info_chars_substs[] = \"____\";   /* Must be same length as above */\n\n/* Returns a sanitized version of s that contains no unsafe info string chars.\n * If no unsafe characters are found, simply returns s. Caller needs to\n * free tmp if it is non-null on return.\n */\nconst char *getSafeInfoString(const char *s, size_t len, char **tmp) {\n    *tmp = NULL;\n    if (mempbrk(s, len, unsafe_info_chars,sizeof(unsafe_info_chars)-1)\n        == NULL) return s;\n    char *_new = *tmp = (char*)zmalloc(len + 1);\n    memcpy(_new, s, len);\n    _new[len] = '\\0';\n    return memmapchars(_new, len, unsafe_info_chars, unsafe_info_chars_substs,\n                       sizeof(unsafe_info_chars)-1);\n}\n\n/* Create the string returned by the INFO command. This is decoupled\n * by the INFO command itself as we need to report the same information\n * on memory corruption problems. */\nsds genRedisInfoString(const char *section) {\n    sds info = sdsempty();\n    time_t uptime = g_pserver->unixtime-cserver.stat_starttime;\n    int j;\n    int allsections = 0, defsections = 0, everything = 0, modules = 0;\n    int sections = 0;\n\n    if (section == NULL) section = \"default\";\n    allsections = strcasecmp(section,\"all\") == 0;\n    defsections = strcasecmp(section,\"default\") == 0;\n    everything = strcasecmp(section,\"everything\") == 0;\n    modules = strcasecmp(section,\"modules\") == 0;\n    if (everything) allsections = 1;\n\n    /* Server */\n    if (allsections || defsections || !strcasecmp(section,\"server\")) {\n        static int call_uname = 1;\n        static struct utsname name;\n        const char *mode;\n        const char *supervised;\n\n        if (g_pserver->cluster_enabled) mode = \"cluster\";\n        else if (g_pserver->sentinel_mode) mode = \"sentinel\";\n        else mode = \"standalone\";\n\n        if (cserver.supervised) {\n            if (cserver.supervised_mode == SUPERVISED_UPSTART) supervised = \"upstart\";\n            else if (cserver.supervised_mode == SUPERVISED_SYSTEMD) supervised = \"systemd\";\n            else supervised = \"unknown\";\n        } else {\n            supervised = \"no\";\n        }\n\n        if (sections++) info = sdscat(info,\"\\r\\n\");\n\n        if (call_uname) {\n            /* Uname can be slow and is always the same output. Cache it. */\n            uname(&name);\n            call_uname = 0;\n        }\n\n        unsigned int lruclock = g_pserver->lruclock.load();\n        ustime_t ustime;\n        __atomic_load(&g_pserver->ustime, &ustime, __ATOMIC_RELAXED);\n        info = sdscatfmt(info,\n            \"# Server\\r\\n\"\n            \"redis_version:%s\\r\\n\"\n            \"redis_git_sha1:%s\\r\\n\"\n            \"redis_git_dirty:%i\\r\\n\"\n            \"redis_build_id:%s\\r\\n\"\n            \"redis_mode:%s\\r\\n\"\n            \"os:%s %s %s\\r\\n\"\n            \"arch_bits:%i\\r\\n\"\n            \"multiplexing_api:%s\\r\\n\"\n            \"atomicvar_api:%s\\r\\n\"\n            \"gcc_version:%i.%i.%i\\r\\n\"\n            \"process_id:%I\\r\\n\"\n            \"process_supervised:%s\\r\\n\"\n            \"run_id:%s\\r\\n\"\n            \"tcp_port:%i\\r\\n\"\n            \"server_time_usec:%I\\r\\n\"\n            \"uptime_in_seconds:%I\\r\\n\"\n            \"uptime_in_days:%I\\r\\n\"\n            \"hz:%i\\r\\n\"\n            \"configured_hz:%i\\r\\n\"\n            \"lru_clock:%u\\r\\n\"\n            \"executable:%s\\r\\n\"\n            \"config_file:%s\\r\\n\"\n            \"availability_zone:%s\\r\\n\"\n            \"features:%s\\r\\n\",\n            KEYDB_SET_VERSION,\n            redisGitSHA1(),\n            strtol(redisGitDirty(),NULL,10) > 0,\n            redisBuildIdString(),\n            mode,\n            name.sysname, name.release, name.machine,\n            (int)sizeof(void*)*8,\n            aeGetApiName(),\n            REDIS_ATOMIC_API,\n#ifdef __GNUC__\n            __GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__,\n#else\n            0,0,0,\n#endif\n            (int64_t) getpid(),\n            supervised,\n            g_pserver->runid,\n            g_pserver->port ? g_pserver->port : g_pserver->tls_port,\n            (int64_t)ustime,\n            (int64_t)uptime,\n            (int64_t)(uptime/(3600*24)),\n            g_pserver->hz.load(),\n            g_pserver->config_hz,\n            lruclock,\n            cserver.executable ? cserver.executable : \"\",\n            cserver.configfile ? cserver.configfile : \"\",\n            g_pserver->sdsAvailabilityZone,\n            \"cluster_mget\");\n    }\n\n    /* Clients */\n    if (allsections || defsections || !strcasecmp(section,\"clients\")) {\n        size_t maxin, maxout;\n        getExpansiveClientsInfo(&maxin,&maxout);\n        if (sections++) info = sdscat(info,\"\\r\\n\");\n        info = sdscatprintf(info,\n            \"# Clients\\r\\n\"\n            \"connected_clients:%lu\\r\\n\"\n            \"cluster_connections:%lu\\r\\n\"\n            \"maxclients:%u\\r\\n\"\n            \"client_recent_max_input_buffer:%zu\\r\\n\"\n            \"client_recent_max_output_buffer:%zu\\r\\n\"\n            \"blocked_clients:%d\\r\\n\"\n            \"tracking_clients:%d\\r\\n\"\n            \"clients_in_timeout_table:%\" PRIu64 \"\\r\\n\"\n            \"current_client_thread:%d\\r\\n\",\n            listLength(g_pserver->clients)-listLength(g_pserver->slaves),\n            getClusterConnectionsCount(),\n            g_pserver->maxclients,\n            maxin, maxout,\n            g_pserver->blocked_clients,\n            g_pserver->tracking_clients,\n            raxSize(g_pserver->clients_timeout_table),\n            static_cast<int>(serverTL - g_pserver->rgthreadvar));\n        for (int ithread = 0; ithread < cserver.cthreads; ++ithread)\n        {\n            info = sdscatprintf(info,\n                \"thread_%d_clients:%d\\r\\n\",\n                ithread, g_pserver->rgthreadvar[ithread].cclients);\n        }\n    }\n\n    /* Memory */\n    if (allsections || defsections || !strcasecmp(section,\"memory\")) {\n        char hmem[64];\n        char peak_hmem[64];\n        char total_system_hmem[64];\n        char used_memory_lua_hmem[64];\n        char used_memory_scripts_hmem[64];\n        char used_memory_rss_hmem[64];\n        char maxmemory_hmem[64];\n        size_t zmalloc_used = zmalloc_used_memory();\n        size_t total_system_mem = cserver.system_memory_size;\n        const char *evict_policy = evictPolicyToString();\n        long long memory_lua = g_pserver->lua ? (long long)lua_gc(g_pserver->lua,LUA_GCCOUNT,0)*1024 : 0;\n        struct redisMemOverhead *mh = getMemoryOverheadData();\n        char available_system_mem[64] = \"unavailable\";\n\n        /* Peak memory is updated from time to time by serverCron() so it\n         * may happen that the instantaneous value is slightly bigger than\n         * the peak value. This may confuse users, so we update the peak\n         * if found smaller than the current memory usage. */\n        if (zmalloc_used > g_pserver->stat_peak_memory)\n            g_pserver->stat_peak_memory = zmalloc_used;\n\n        if (g_pserver->cron_malloc_stats.sys_available) {\n            snprintf(available_system_mem, 64, \"%lu\", g_pserver->cron_malloc_stats.sys_available);\n        }\n\n        bytesToHuman(hmem,zmalloc_used,sizeof(hmem));\n        bytesToHuman(peak_hmem,g_pserver->stat_peak_memory,sizeof(peak_hmem));\n        bytesToHuman(total_system_hmem,total_system_mem,sizeof(total_system_hmem));\n        bytesToHuman(used_memory_lua_hmem,memory_lua,sizeof(used_memory_lua_hmem));\n        bytesToHuman(used_memory_scripts_hmem,mh->lua_caches,sizeof(used_memory_scripts_hmem));\n        bytesToHuman(used_memory_rss_hmem,g_pserver->cron_malloc_stats.process_rss,sizeof(used_memory_rss_hmem));\n        bytesToHuman(maxmemory_hmem,g_pserver->maxmemory,sizeof(maxmemory_hmem));\n\n        if (sections++) info = sdscat(info,\"\\r\\n\");\n        info = sdscatprintf(info,\n            \"# Memory\\r\\n\"\n            \"used_memory:%zu\\r\\n\"\n            \"used_memory_human:%s\\r\\n\"\n            \"used_memory_rss:%zu\\r\\n\"\n            \"used_memory_rss_human:%s\\r\\n\"\n            \"used_memory_peak:%zu\\r\\n\"\n            \"used_memory_peak_human:%s\\r\\n\"\n            \"used_memory_peak_perc:%.2f%%\\r\\n\"\n            \"used_memory_overhead:%zu\\r\\n\"\n            \"used_memory_startup:%zu\\r\\n\"\n            \"used_memory_dataset:%zu\\r\\n\"\n            \"used_memory_dataset_perc:%.2f%%\\r\\n\"\n            \"allocator_allocated:%zu\\r\\n\"\n            \"allocator_active:%zu\\r\\n\"\n            \"allocator_resident:%zu\\r\\n\"\n            \"total_system_memory:%lu\\r\\n\"\n            \"total_system_memory_human:%s\\r\\n\"\n            \"used_memory_lua:%lld\\r\\n\"\n            \"used_memory_lua_human:%s\\r\\n\"\n            \"used_memory_scripts:%lld\\r\\n\"\n            \"used_memory_scripts_human:%s\\r\\n\"\n            \"number_of_cached_scripts:%lu\\r\\n\"\n            \"maxmemory:%lld\\r\\n\"\n            \"maxmemory_human:%s\\r\\n\"\n            \"maxmemory_policy:%s\\r\\n\"\n            \"allocator_frag_ratio:%.2f\\r\\n\"\n            \"allocator_frag_bytes:%zu\\r\\n\"\n            \"allocator_rss_ratio:%.2f\\r\\n\"\n            \"allocator_rss_bytes:%zd\\r\\n\"\n            \"rss_overhead_ratio:%.2f\\r\\n\"\n            \"rss_overhead_bytes:%zd\\r\\n\"\n            \"mem_fragmentation_ratio:%.2f\\r\\n\"\n            \"mem_fragmentation_bytes:%zd\\r\\n\"\n            \"mem_not_counted_for_evict:%zu\\r\\n\"\n            \"mem_replication_backlog:%zu\\r\\n\"\n            \"mem_clients_slaves:%zu\\r\\n\"\n            \"mem_clients_normal:%zu\\r\\n\"\n            \"mem_aof_buffer:%zu\\r\\n\"\n            \"mem_allocator:%s\\r\\n\"\n            \"active_defrag_running:%d\\r\\n\"\n            \"lazyfree_pending_objects:%zu\\r\\n\"\n            \"lazyfreed_objects:%zu\\r\\n\"\n            \"storage_provider:%s\\r\\n\"\n            \"available_system_memory:%s\\r\\n\",\n            zmalloc_used,\n            hmem,\n            g_pserver->cron_malloc_stats.process_rss,\n            used_memory_rss_hmem,\n            g_pserver->stat_peak_memory,\n            peak_hmem,\n            mh->peak_perc,\n            mh->overhead_total,\n            mh->startup_allocated,\n            mh->dataset,\n            mh->dataset_perc,\n            g_pserver->cron_malloc_stats.allocator_allocated,\n            g_pserver->cron_malloc_stats.allocator_active,\n            g_pserver->cron_malloc_stats.allocator_resident,\n            (unsigned long)total_system_mem,\n            total_system_hmem,\n            memory_lua,\n            used_memory_lua_hmem,\n            (long long) mh->lua_caches,\n            used_memory_scripts_hmem,\n            dictSize(g_pserver->lua_scripts),\n            g_pserver->maxmemory,\n            maxmemory_hmem,\n            evict_policy,\n            mh->allocator_frag,\n            mh->allocator_frag_bytes,\n            mh->allocator_rss,\n            mh->allocator_rss_bytes,\n            mh->rss_extra,\n            mh->rss_extra_bytes,\n            mh->total_frag,       /* This is the total RSS overhead, including\n                                     fragmentation, but not just it. This field\n                                     (and the next one) is named like that just\n                                     for backward compatibility. */\n            mh->total_frag_bytes,\n            freeMemoryGetNotCountedMemory(),\n            mh->repl_backlog,\n            mh->clients_slaves,\n            mh->clients_normal,\n            mh->aof_buffer,\n            ZMALLOC_LIB,\n            g_pserver->active_defrag_running,\n            lazyfreeGetPendingObjectsCount(),\n            lazyfreeGetFreedObjectsCount(),\n            g_pserver->m_pstorageFactory ? g_pserver->m_pstorageFactory->name() : \"none\",\n            available_system_mem\n        );\n        freeMemoryOverheadData(mh);\n    }\n\n    /* Persistence */\n    if (allsections || defsections || !strcasecmp(section,\"persistence\")) {\n        if (sections++) info = sdscat(info,\"\\r\\n\");\n        double fork_perc = 0;\n        if (g_pserver->stat_module_progress) {\n            fork_perc = g_pserver->stat_module_progress * 100;\n        } else if (g_pserver->stat_current_save_keys_total) {\n            fork_perc = ((double)g_pserver->stat_current_save_keys_processed / g_pserver->stat_current_save_keys_total) * 100;\n        }\n        int aof_bio_fsync_status;\n        atomicGet(g_pserver->aof_bio_fsync_status,aof_bio_fsync_status);\n\n        info = sdscatprintf(info,\n            \"# Persistence\\r\\n\"\n            \"loading:%d\\r\\n\"\n            \"current_cow_size:%zu\\r\\n\"\n            \"current_cow_size_age:%lu\\r\\n\"\n            \"current_fork_perc:%.2f\\r\\n\"\n            \"current_save_keys_processed:%zu\\r\\n\"\n            \"current_save_keys_total:%zu\\r\\n\"\n            \"rdb_changes_since_last_save:%lld\\r\\n\"\n            \"rdb_bgsave_in_progress:%d\\r\\n\"\n            \"rdb_last_save_time:%jd\\r\\n\"\n            \"rdb_last_bgsave_status:%s\\r\\n\"\n            \"rdb_last_bgsave_time_sec:%jd\\r\\n\"\n            \"rdb_current_bgsave_time_sec:%jd\\r\\n\"\n            \"rdb_last_cow_size:%zu\\r\\n\"\n            \"aof_enabled:%d\\r\\n\"\n            \"aof_rewrite_in_progress:%d\\r\\n\"\n            \"aof_rewrite_scheduled:%d\\r\\n\"\n            \"aof_last_rewrite_time_sec:%jd\\r\\n\"\n            \"aof_current_rewrite_time_sec:%jd\\r\\n\"\n            \"aof_last_bgrewrite_status:%s\\r\\n\"\n            \"aof_last_write_status:%s\\r\\n\"\n            \"aof_last_cow_size:%zu\\r\\n\"\n            \"module_fork_in_progress:%d\\r\\n\"\n            \"module_fork_last_cow_size:%zu\\r\\n\",\n            !!g_pserver->loading.load(std::memory_order_relaxed),   /* Note: libraries expect 1 or 0 here so coerce our enum */\n            g_pserver->stat_current_cow_bytes,\n            g_pserver->stat_current_cow_updated ? (unsigned long) elapsedMs(g_pserver->stat_current_cow_updated) / 1000 : 0,\n            fork_perc,\n            g_pserver->stat_current_save_keys_processed,\n            g_pserver->stat_current_save_keys_total,\n            g_pserver->dirty,\n            g_pserver->FRdbSaveInProgress(),\n            (intmax_t)g_pserver->lastsave,\n            (g_pserver->lastbgsave_status == C_OK) ? \"ok\" : \"err\",\n            (intmax_t)g_pserver->rdb_save_time_last,\n            (intmax_t)(g_pserver->FRdbSaveInProgress() ?\n                time(NULL)-g_pserver->rdb_save_time_start : -1),\n            g_pserver->stat_rdb_cow_bytes,\n            g_pserver->aof_state != AOF_OFF,\n            g_pserver->child_type == CHILD_TYPE_AOF,\n            g_pserver->aof_rewrite_scheduled,\n            (intmax_t)g_pserver->aof_rewrite_time_last,\n            (intmax_t)((g_pserver->child_type != CHILD_TYPE_AOF) ?\n                -1 : time(NULL)-g_pserver->aof_rewrite_time_start),\n            (g_pserver->aof_lastbgrewrite_status == C_OK) ? \"ok\" : \"err\",\n            (g_pserver->aof_last_write_status == C_OK &&\n                aof_bio_fsync_status == C_OK) ? \"ok\" : \"err\",\n            g_pserver->stat_aof_cow_bytes,\n            g_pserver->child_type == CHILD_TYPE_MODULE,\n            g_pserver->stat_module_cow_bytes);\n\n        if (g_pserver->aof_enabled) {\n            info = sdscatprintf(info,\n                \"aof_current_size:%lld\\r\\n\"\n                \"aof_base_size:%lld\\r\\n\"\n                \"aof_pending_rewrite:%d\\r\\n\"\n                \"aof_buffer_length:%zu\\r\\n\"\n                \"aof_rewrite_buffer_length:%lu\\r\\n\"\n                \"aof_pending_bio_fsync:%llu\\r\\n\"\n                \"aof_delayed_fsync:%lu\\r\\n\",\n                (long long) g_pserver->aof_current_size,\n                (long long) g_pserver->aof_rewrite_base_size,\n                g_pserver->aof_rewrite_scheduled,\n                sdslen(g_pserver->aof_buf),\n                aofRewriteBufferSize(),\n                bioPendingJobsOfType(BIO_AOF_FSYNC),\n                g_pserver->aof_delayed_fsync);\n        }\n\n        if (g_pserver->loading) {\n            double perc = 0;\n            time_t eta, elapsed;\n            off_t remaining_bytes = 1;\n\n            if (g_pserver->loading_total_bytes) {\n                perc = ((double)g_pserver->loading_loaded_bytes / g_pserver->loading_total_bytes) * 100;\n                remaining_bytes = g_pserver->loading_total_bytes - g_pserver->loading_loaded_bytes;\n            } else if(g_pserver->loading_rdb_used_mem) {\n                perc = ((double)g_pserver->loading_loaded_bytes / g_pserver->loading_rdb_used_mem) * 100;\n                remaining_bytes = g_pserver->loading_rdb_used_mem - g_pserver->loading_loaded_bytes;\n                /* used mem is only a (bad) estimation of the rdb file size, avoid going over 100% */\n                if (perc > 99.99) perc = 99.99;\n                if (remaining_bytes < 1) remaining_bytes = 1;\n            }\n\n            elapsed = time(NULL)-g_pserver->loading_start_time;\n            if (elapsed == 0) {\n                eta = 1; /* A fake 1 second figure if we don't have\n                            enough info */\n            } else {\n                eta = (elapsed*remaining_bytes)/(g_pserver->loading_loaded_bytes+1);\n            }\n\n            info = sdscatprintf(info,\n                \"loading_start_time:%jd\\r\\n\"\n                \"loading_total_bytes:%llu\\r\\n\"\n                \"loading_rdb_used_mem:%llu\\r\\n\"\n                \"loading_loaded_bytes:%llu\\r\\n\"\n                \"loading_loaded_perc:%.2f\\r\\n\"\n                \"loading_eta_seconds:%jd\\r\\n\",\n                (intmax_t) g_pserver->loading_start_time,\n                (unsigned long long) g_pserver->loading_total_bytes,\n                (unsigned long long) g_pserver->loading_rdb_used_mem,\n                (unsigned long long) g_pserver->loading_loaded_bytes,\n                perc,\n                (intmax_t)eta\n            );\n        }\n        if (g_pserver->m_pstorageFactory)\n        {\n            info = sdscat(info, g_pserver->m_pstorageFactory->getInfo().get());\n        }\n    }\n\n    /* Stats */\n    if (allsections || defsections || !strcasecmp(section,\"stats\")) {\n        double avgLockContention = 0;\n        for (unsigned i = 0; i < redisServer::s_lockContentionSamples; ++i)\n            avgLockContention += g_pserver->rglockSamples[i];\n        avgLockContention /= redisServer::s_lockContentionSamples;\n\n        long long stat_total_reads_processed, stat_total_writes_processed;\n        long long stat_net_input_bytes, stat_net_output_bytes;\n        stat_total_reads_processed = g_pserver->stat_total_reads_processed.load(std::memory_order_relaxed);\n        stat_total_writes_processed = g_pserver->stat_total_writes_processed.load(std::memory_order_relaxed);\n        stat_net_input_bytes = g_pserver->stat_net_input_bytes.load(std::memory_order_relaxed);\n        stat_net_output_bytes = g_pserver->stat_net_output_bytes.load(std::memory_order_relaxed);\n\n        long long stat_total_error_replies = 0;\n        for (int iel = 0; iel < cserver.cthreads; ++iel)\n            stat_total_error_replies += g_pserver->rgthreadvar[iel].stat_total_error_replies;\n\n        if (sections++) info = sdscat(info,\"\\r\\n\");\n        info = sdscatprintf(info,\n            \"# Stats\\r\\n\"\n            \"total_connections_received:%lld\\r\\n\"\n            \"total_commands_processed:%lld\\r\\n\"\n            \"instantaneous_ops_per_sec:%lld\\r\\n\"\n            \"total_net_input_bytes:%lld\\r\\n\"\n            \"total_net_output_bytes:%lld\\r\\n\"\n            \"instantaneous_input_kbps:%.2f\\r\\n\"\n            \"instantaneous_output_kbps:%.2f\\r\\n\"\n            \"rejected_connections:%lld\\r\\n\"\n            \"sync_full:%lld\\r\\n\"\n            \"sync_partial_ok:%lld\\r\\n\"\n            \"sync_partial_err:%lld\\r\\n\"\n            \"expired_keys:%lld\\r\\n\"\n            \"expired_stale_perc:%.2f\\r\\n\"\n            \"expired_time_cap_reached_count:%lld\\r\\n\"\n            \"expire_cycle_cpu_milliseconds:%lld\\r\\n\"\n            \"evicted_keys:%lld\\r\\n\"\n            \"keyspace_hits:%lld\\r\\n\"\n            \"keyspace_misses:%lld\\r\\n\"\n            \"pubsub_channels:%ld\\r\\n\"\n            \"pubsub_patterns:%lu\\r\\n\"\n            \"latest_fork_usec:%lld\\r\\n\"\n            \"total_forks:%lld\\r\\n\"\n            \"migrate_cached_sockets:%ld\\r\\n\"\n            \"slave_expires_tracked_keys:%zu\\r\\n\"\n            \"active_defrag_hits:%lld\\r\\n\"\n            \"active_defrag_misses:%lld\\r\\n\"\n            \"active_defrag_key_hits:%lld\\r\\n\"\n            \"active_defrag_key_misses:%lld\\r\\n\"\n            \"tracking_total_keys:%lld\\r\\n\"\n            \"tracking_total_items:%lld\\r\\n\"\n            \"tracking_total_prefixes:%lld\\r\\n\"\n            \"unexpected_error_replies:%lld\\r\\n\"\n            \"total_error_replies:%lld\\r\\n\"\n            \"dump_payload_sanitizations:%lld\\r\\n\"\n            \"total_reads_processed:%lld\\r\\n\"\n            \"total_writes_processed:%lld\\r\\n\"\n            \"instantaneous_lock_contention:%d\\r\\n\"\n            \"avg_lock_contention:%f\\r\\n\"\n            \"storage_provider_read_hits:%lld\\r\\n\"\n            \"storage_provider_read_misses:%lld\\r\\n\",\n            g_pserver->stat_numconnections,\n            g_pserver->stat_numcommands,\n            getInstantaneousMetric(STATS_METRIC_COMMAND),\n            stat_net_input_bytes,\n            stat_net_output_bytes,\n            (float)getInstantaneousMetric(STATS_METRIC_NET_INPUT)/1024,\n            (float)getInstantaneousMetric(STATS_METRIC_NET_OUTPUT)/1024,\n            g_pserver->stat_rejected_conn,\n            g_pserver->stat_sync_full,\n            g_pserver->stat_sync_partial_ok,\n            g_pserver->stat_sync_partial_err,\n            g_pserver->stat_expiredkeys,\n            g_pserver->stat_expired_stale_perc*100,\n            g_pserver->stat_expired_time_cap_reached_count,\n            g_pserver->stat_expire_cycle_time_used/1000,\n            g_pserver->stat_evictedkeys,\n            g_pserver->stat_keyspace_hits,\n            g_pserver->stat_keyspace_misses,\n            dictSize(g_pserver->pubsub_channels),\n            dictSize(g_pserver->pubsub_patterns),\n            g_pserver->stat_fork_time,\n            g_pserver->stat_total_forks,\n            dictSize(g_pserver->migrate_cached_sockets),\n            getSlaveKeyWithExpireCount(),\n            g_pserver->stat_active_defrag_hits,\n            g_pserver->stat_active_defrag_misses,\n            g_pserver->stat_active_defrag_key_hits,\n            g_pserver->stat_active_defrag_key_misses,\n            (unsigned long long) trackingGetTotalKeys(),\n            (unsigned long long) trackingGetTotalItems(),\n            (unsigned long long) trackingGetTotalPrefixes(),\n            g_pserver->stat_unexpected_error_replies,\n            stat_total_error_replies,\n            g_pserver->stat_dump_payload_sanitizations,\n            stat_total_reads_processed,\n            stat_total_writes_processed,\n            aeLockContention(),\n            avgLockContention,\n            g_pserver->stat_storage_provider_read_hits,\n            g_pserver->stat_storage_provider_read_misses);\n    }\n\n    /* Replication */\n    if (allsections || defsections || !strcasecmp(section,\"replication\")) {\n        if (sections++) info = sdscat(info,\"\\r\\n\");\n        info = sdscatprintf(info,\n            \"# Replication\\r\\n\"\n            \"role:%s\\r\\n\",\n            listLength(g_pserver->masters) == 0 ? \"master\" \n                : g_pserver->fActiveReplica ? \"active-replica\" : \"slave\");\n        if (listLength(g_pserver->masters)) {\n            int connectedMasters = 0;\n            info = sdscatprintf(info, \"master_global_link_status:%s\\r\\n\",\n                FBrokenLinkToMaster(&connectedMasters) ? \"down\" : \"up\");\n\n            info = sdscatprintf(info, \"connected_masters:%d\\r\\n\", connectedMasters);\n\n            int cmasters = 0;\n            listIter li;\n            listNode *ln;\n            listRewind(g_pserver->masters, &li);\n            while ((ln = listNext(&li)))\n            {\n                long long slave_repl_offset = 1;\n                long long slave_read_repl_offset = 1;\n                redisMaster *mi = (redisMaster*)listNodeValue(ln);\n\n                if (mi->master){\n                    slave_repl_offset = mi->master->reploff;\n                    slave_read_repl_offset = mi->master->read_reploff;\n                } else if (mi->cached_master){\n                    slave_repl_offset = mi->cached_master->reploff;\n                    slave_read_repl_offset = mi->cached_master->read_reploff;\n                }\n\n                char master_prefix[128] = \"\";\n                if (cmasters != 0) {\n                    snprintf(master_prefix, sizeof(master_prefix), \"_%d\", cmasters);\n                }\n\n                info = sdscatprintf(info,\n                    \"master%s_host:%s\\r\\n\"\n                    \"master%s_port:%d\\r\\n\"\n                    \"master%s_link_status:%s\\r\\n\"\n                    \"master%s_last_io_seconds_ago:%d\\r\\n\"\n                    \"master%s_sync_in_progress:%d\\r\\n\"\n                    \"slave_read_repl_offset:%lld\\r\\n\"\n                    \"slave_repl_offset:%lld\\r\\n\"\n                    ,master_prefix, mi->masterhost,\n                    master_prefix, mi->masterport,\n                    master_prefix, (mi->repl_state == REPL_STATE_CONNECTED) ?\n                        \"up\" : \"down\",\n                    master_prefix, mi->master ?\n                    ((int)(g_pserver->unixtime-mi->master->lastinteraction)) : -1,\n                    master_prefix, mi->repl_state == REPL_STATE_TRANSFER,\n                    slave_read_repl_offset, \n                    slave_repl_offset\n                );\n\n                if (mi->repl_state == REPL_STATE_TRANSFER) {\n                    double perc = 0;\n                    if (mi->repl_transfer_size) {\n                        perc = ((double)mi->repl_transfer_read / mi->repl_transfer_size) * 100;\n                    }\n                    info = sdscatprintf(info,\n                        \"master%s_sync_total_bytes:%lld\\r\\n\"\n                        \"master%s_sync_read_bytes:%lld\\r\\n\"\n                        \"master%s_sync_left_bytes:%lld\\r\\n\"\n                        \"master%s_sync_perc:%.2f\\r\\n\"\n                        \"master%s_sync_last_io_seconds_ago:%d\\r\\n\",\n                        master_prefix, (long long) mi->repl_transfer_size,\n                        master_prefix, (long long) mi->repl_transfer_read,\n                        master_prefix, (long long) (mi->repl_transfer_size - mi->repl_transfer_read),\n                        master_prefix, perc,\n                        master_prefix, (int)(g_pserver->unixtime-mi->repl_transfer_lastio)\n                    );\n                }\n\n                if (mi->repl_state != REPL_STATE_CONNECTED) {\n                    info = sdscatprintf(info,\n                        \"master%s_link_down_since_seconds:%jd\\r\\n\",\n                        master_prefix, mi->repl_down_since ? \n                            (intmax_t)(g_pserver->unixtime-mi->repl_down_since) : -1);\n                }\n                ++cmasters;\n            }\n            info = sdscatprintf(info,\n                \"slave_priority:%d\\r\\n\"\n                \"slave_read_only:%d\\r\\n\"\n                \"replica_announced:%d\\r\\n\",\n                g_pserver->slave_priority,\n                g_pserver->repl_slave_ro,\n                g_pserver->replica_announced);\n        }\n\n        info = sdscatprintf(info,\n            \"connected_slaves:%lu\\r\\n\",\n            listLength(g_pserver->slaves));\n\n        /* If min-slaves-to-write is active, write the number of slaves\n         * currently considered 'good'. */\n        if (g_pserver->repl_min_slaves_to_write &&\n            g_pserver->repl_min_slaves_max_lag) {\n            info = sdscatprintf(info,\n                \"min_slaves_good_slaves:%d\\r\\n\",\n                g_pserver->repl_good_slaves_count);\n        }\n\n        if (listLength(g_pserver->slaves)) {\n            int slaveid = 0;\n            listNode *ln;\n            listIter li;\n\n            listRewind(g_pserver->slaves,&li);\n            while((ln = listNext(&li))) {\n                client *replica = (client*)listNodeValue(ln);\n                const char *state = NULL;\n                char ip[NET_IP_STR_LEN], *slaveip = replica->slave_addr;\n                int port;\n                long lag = 0;\n\n                if (!slaveip) {\n                    if (connPeerToString(replica->conn,ip,sizeof(ip),&port) == -1)\n                        continue;\n                    slaveip = ip;\n                }\n                switch(replica->replstate) {\n                case SLAVE_STATE_WAIT_BGSAVE_START:\n                case SLAVE_STATE_WAIT_BGSAVE_END:\n                    state = \"wait_bgsave\";\n                    break;\n                case SLAVE_STATE_SEND_BULK:\n                    state = \"send_bulk\";\n                    break;\n                case SLAVE_STATE_ONLINE:\n                    state = \"online\";\n                    break;\n                }\n                if (state == NULL) continue;\n                if (replica->replstate == SLAVE_STATE_ONLINE)\n                    lag = time(NULL) - replica->repl_ack_time;\n\n                info = sdscatprintf(info,\n                    \"slave%d:ip=%s,port=%d,state=%s,\"\n                    \"offset=%lld,lag=%ld\\r\\n\",\n                    slaveid,slaveip,replica->slave_listening_port,state,\n                    (replica->repl_ack_off), lag);\n                slaveid++;\n            }\n        }\n        info = sdscatprintf(info,\n            \"master_failover_state:%s\\r\\n\"\n            \"master_replid:%s\\r\\n\"\n            \"master_replid2:%s\\r\\n\"\n            \"master_repl_offset:%lld\\r\\n\"\n            \"second_repl_offset:%lld\\r\\n\"\n            \"repl_backlog_active:%d\\r\\n\"\n            \"repl_backlog_size:%lld\\r\\n\"\n            \"repl_backlog_first_byte_offset:%lld\\r\\n\"\n            \"repl_backlog_histlen:%lld\\r\\n\",\n            getFailoverStateString(),\n            g_pserver->replid,\n            g_pserver->replid2,\n            g_pserver->master_repl_offset,\n            g_pserver->second_replid_offset,\n            g_pserver->repl_backlog != NULL,\n            g_pserver->repl_backlog_size,\n            g_pserver->repl_backlog_off,\n            g_pserver->repl_backlog_histlen);\n    }\n\n    /* CPU */\n    if (allsections || defsections || !strcasecmp(section,\"cpu\")) {\n        if (sections++) info = sdscat(info,\"\\r\\n\");\n\n        struct rusage self_ru, c_ru;\n        getrusage(RUSAGE_SELF, &self_ru);\n        getrusage(RUSAGE_CHILDREN, &c_ru);\n        info = sdscatprintf(info,\n        \"# CPU\\r\\n\"\n        \"used_cpu_sys:%ld.%06ld\\r\\n\"\n        \"used_cpu_user:%ld.%06ld\\r\\n\"\n        \"used_cpu_sys_children:%ld.%06ld\\r\\n\"\n        \"used_cpu_user_children:%ld.%06ld\\r\\n\"\n        \"server_threads:%d\\r\\n\"\n        \"long_lock_waits:%\" PRIu64 \"\\r\\n\",\n        (long)self_ru.ru_stime.tv_sec, (long)self_ru.ru_stime.tv_usec,\n        (long)self_ru.ru_utime.tv_sec, (long)self_ru.ru_utime.tv_usec,\n        (long)c_ru.ru_stime.tv_sec, (long)c_ru.ru_stime.tv_usec,\n        (long)c_ru.ru_utime.tv_sec, (long)c_ru.ru_utime.tv_usec,\n        cserver.cthreads,\n        fastlock_getlongwaitcount());\n#ifdef RUSAGE_THREAD\n        struct rusage m_ru;\n        getrusage(RUSAGE_THREAD, &m_ru);\n        info = sdscatprintf(info,\n            \"used_cpu_sys_main_thread:%ld.%06ld\\r\\n\"\n            \"used_cpu_user_main_thread:%ld.%06ld\\r\\n\",\n            (long)m_ru.ru_stime.tv_sec, (long)m_ru.ru_stime.tv_usec,\n            (long)m_ru.ru_utime.tv_sec, (long)m_ru.ru_utime.tv_usec);\n#endif  /* RUSAGE_THREAD */\n    }\n\n    /* Modules */\n    if (allsections || defsections || !strcasecmp(section,\"modules\")) {\n        if (sections++) info = sdscat(info,\"\\r\\n\");\n        info = sdscatprintf(info,\"# Modules\\r\\n\");\n        info = genModulesInfoString(info);\n    }\n\n    /* Command statistics */\n    if (allsections || !strcasecmp(section,\"commandstats\")) {\n        if (sections++) info = sdscat(info,\"\\r\\n\");\n        info = sdscatprintf(info, \"# Commandstats\\r\\n\");\n\n        struct redisCommand *c;\n        dictEntry *de;\n        dictIterator *di;\n        di = dictGetSafeIterator(g_pserver->commands);\n        while((de = dictNext(di)) != NULL) {\n            char *tmpsafe;\n            c = (struct redisCommand *) dictGetVal(de);\n            if (!c->calls && !c->failed_calls && !c->rejected_calls)\n                continue;\n            info = sdscatprintf(info,\n                \"cmdstat_%s:calls=%lld,usec=%lld,usec_per_call=%.2f\"\n                \",rejected_calls=%lld,failed_calls=%lld\\r\\n\",\n                getSafeInfoString(c->name, strlen(c->name), &tmpsafe), c->calls, c->microseconds,\n                (c->calls == 0) ? 0 : ((float)c->microseconds/c->calls),\n                c->rejected_calls, c->failed_calls);\n            if (tmpsafe != NULL) zfree(tmpsafe);\n        }\n        dictReleaseIterator(di);\n    }\n    /* Error statistics */\n    if (allsections || defsections || !strcasecmp(section,\"errorstats\")) {\n        if (sections++) info = sdscat(info,\"\\r\\n\");\n        info = sdscat(info, \"# Errorstats\\r\\n\");\n        raxIterator ri;\n        raxStart(&ri,g_pserver->errors);\n        raxSeek(&ri,\"^\",NULL,0);\n        struct redisError *e;\n        while(raxNext(&ri)) {\n            char *tmpsafe;\n            e = (struct redisError *) ri.data;\n            info = sdscatprintf(info,\n                \"errorstat_%.*s:count=%lld\\r\\n\",\n                (int)ri.key_len, getSafeInfoString((char *) ri.key, ri.key_len, &tmpsafe), e->count);\n            if (tmpsafe != NULL) zfree(tmpsafe);\n        }\n        raxStop(&ri);\n    }\n\n    /* Cluster */\n    if (allsections || defsections || !strcasecmp(section,\"cluster\")) {\n        if (sections++) info = sdscat(info,\"\\r\\n\");\n        info = sdscatprintf(info,\n        \"# Cluster\\r\\n\"\n        \"cluster_enabled:%d\\r\\n\",\n        g_pserver->cluster_enabled);\n    }\n\n    /* Key space */\n    if (allsections || defsections || !strcasecmp(section,\"keyspace\")) {\n        if (sections++) info = sdscat(info,\"\\r\\n\");\n        info = sdscatprintf(info, \"# Keyspace\\r\\n\");\n        for (j = 0; j < cserver.dbnum; j++) {\n            long long keys, vkeys, cachedKeys;\n\n            keys = g_pserver->db[j]->size();\n            vkeys = g_pserver->db[j]->expireSize();\n            cachedKeys = g_pserver->db[j]->size(true /* fCachedOnly */);\n\n            // Adjust TTL by the current time\n            mstime_t mstime;\n            __atomic_load(&g_pserver->mstime, &mstime, __ATOMIC_ACQUIRE);\n            g_pserver->db[j]->avg_ttl -= (mstime - g_pserver->db[j]->last_expire_set);\n            if (g_pserver->db[j]->avg_ttl < 0)\n                g_pserver->db[j]->avg_ttl = 0;\n            g_pserver->db[j]->last_expire_set = mstime;\n            \n            if (keys || vkeys) {\n                info = sdscatprintf(info,\n                    \"db%d:keys=%lld,expires=%lld,avg_ttl=%lld,cached_keys=%lld\\r\\n\",\n                    j, keys, vkeys, static_cast<long long>(g_pserver->db[j]->avg_ttl), cachedKeys);\n            }\n        }\n    }\n\n    if (allsections || defsections || !strcasecmp(section,\"keydb\")) {\n        // Compute the MVCC depth\n        int mvcc_depth = 0;\n        for (int idb = 0; idb < cserver.dbnum; ++idb) {\n            mvcc_depth = std::max(mvcc_depth, g_pserver->db[idb]->snapshot_depth());\n        }\n\n        if (sections++) info = sdscat(info,\"\\r\\n\");\n        info = sdscatprintf(info, \n            \"# KeyDB\\r\\n\"\n            \"mvcc_depth:%d\\r\\n\",\n            mvcc_depth\n        );\n    }\n\n    /* Get info from modules.\n     * if user asked for \"everything\" or \"modules\", or a specific section\n     * that's not found yet. */\n    if (everything || modules ||\n        (!allsections && !defsections && sections==0)) {\n        info = modulesCollectInfo(info,\n                                  everything || modules ? NULL: section,\n                                  0, /* not a crash report */\n                                  sections);\n    }\n    return info;\n}\n\nvoid infoCommand(client *c) {\n    const char *section = c->argc == 2 ? (const char*)ptrFromObj(c->argv[1]) : \"default\";\n\n    if (c->argc > 2) {\n        addReplyErrorObject(c,shared.syntaxerr);\n        return;\n    }\n    sds info = genRedisInfoString(section);\n    addReplyVerbatim(c,info,sdslen(info),\"txt\");\n    sdsfree(info);\n}\n\nvoid monitorCommand(client *c) {\n    serverAssert(GlobalLocksAcquired());\n\n    if (c->flags & CLIENT_DENY_BLOCKING) {\n        /**\n         * A client that has CLIENT_DENY_BLOCKING flag on\n         * expects a reply per command and so can't execute MONITOR. */\n        addReplyError(c, \"MONITOR isn't allowed for DENY BLOCKING client\");\n        return;\n    }\n\n    /* ignore MONITOR if already slave or in monitor mode */\n    if (c->flags & CLIENT_SLAVE) return;\n\n    c->flags |= (CLIENT_SLAVE|CLIENT_MONITOR);\n    listAddNodeTail(g_pserver->monitors,c);\n    addReply(c,shared.ok);\n}\n\n/* =================================== Main! ================================ */\n\nint checkIgnoreWarning(const char *warning) {\n    int argc, j;\n    sds *argv = sdssplitargs(g_pserver->ignore_warnings, &argc);\n    if (argv == NULL)\n        return 0;\n\n    for (j = 0; j < argc; j++) {\n        char *flag = argv[j];\n        if (!strcasecmp(flag, warning))\n            break;\n    }\n    sdsfreesplitres(argv,argc);\n    return j < argc;\n}\n\n#ifdef __linux__\nint linuxOvercommitMemoryValue(void) {\n    FILE *fp = fopen(\"/proc/sys/vm/overcommit_memory\",\"r\");\n    char buf[64];\n\n    if (!fp) return -1;\n    if (fgets(buf,64,fp) == NULL) {\n        fclose(fp);\n        return -1;\n    }\n    fclose(fp);\n\n    return atoi(buf);\n}\n\nvoid linuxMemoryWarnings(void) {\n    if (linuxOvercommitMemoryValue() == 0) {\n        serverLog(LL_WARNING,\"WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.\");\n    }\n    if (THPIsEnabled() && THPDisable()) {\n        serverLog(LL_WARNING,\"WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with KeyDB. To fix this issue run the command 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. KeyDB must be restarted after THP is disabled (set to 'madvise' or 'never').\");\n    }\n}\n\n#ifdef __arm64__\n\n/* Get size in kilobytes of the Shared_Dirty pages of the calling process for the\n * memory map corresponding to the provided address, or -1 on error. */\nstatic int smapsGetSharedDirty(unsigned long addr) {\n    int ret, in_mapping = 0, val = -1;\n    unsigned long from, to;\n    char buf[64];\n    FILE *f;\n\n    f = fopen(\"/proc/self/smaps\", \"r\");\n    serverAssert(f);\n\n    while (1) {\n        if (!fgets(buf, sizeof(buf), f))\n            break;\n\n        ret = sscanf(buf, \"%lx-%lx\", &from, &to);\n        if (ret == 2)\n            in_mapping = from <= addr && addr < to;\n\n        if (in_mapping && !memcmp(buf, \"Shared_Dirty:\", 13)) {\n            ret = sscanf(buf, \"%*s %d\", &val);\n            serverAssert(ret == 1);\n            break;\n        }\n    }\n\n    fclose(f);\n    return val;\n}\n\n/* Older arm64 Linux kernels have a bug that could lead to data corruption\n * during background save in certain scenarios. This function checks if the\n * kernel is affected.\n * The bug was fixed in commit ff1712f953e27f0b0718762ec17d0adb15c9fd0b\n * titled: \"arm64: pgtable: Ensure dirty bit is preserved across pte_wrprotect()\"\n * Return 1 if the kernel seems to be affected, and 0 otherwise. */\nint linuxMadvFreeForkBugCheck(void) {\n    int ret, pipefd[2];\n    pid_t pid;\n    char *p, *q, bug_found = 0;\n    const long map_size = 3 * 4096;\n\n    /* Create a memory map that's in our full control (not one used by the allocator). */\n    p = (char*)mmap(NULL, map_size, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);\n    serverAssert(p != MAP_FAILED);\n\n    q = p + 4096;\n\n    /* Split the memory map in 3 pages by setting their protection as RO|RW|RO to prevent\n     * Linux from merging this memory map with adjacent VMAs. */\n    ret = mprotect(q, 4096, PROT_READ | PROT_WRITE);\n    serverAssert(!ret);\n\n    /* Write to the page once to make it resident */\n    *(volatile char*)q = 0;\n\n    /* Tell the kernel that this page is free to be reclaimed. */\n#ifndef MADV_FREE\n#define MADV_FREE 8\n#endif\n    ret = madvise(q, 4096, MADV_FREE);\n    serverAssert(!ret);\n\n    /* Write to the page after being marked for freeing, this is supposed to take\n     * ownership of that page again. */\n    *(volatile char*)q = 0;\n\n    /* Create a pipe for the child to return the info to the parent. */\n    ret = pipe(pipefd);\n    serverAssert(!ret);\n\n    /* Fork the process. */\n    pid = fork();\n    serverAssert(pid >= 0);\n    if (!pid) {\n        /* Child: check if the page is marked as dirty, expecing 4 (kB).\n         * A value of 0 means the kernel is affected by the bug. */\n        if (!smapsGetSharedDirty((unsigned long)q))\n            bug_found = 1;\n\n        ret = write(pipefd[1], &bug_found, 1);\n        serverAssert(ret == 1);\n\n        exit(0);\n    } else {\n        /* Read the result from the child. */\n        ret = read(pipefd[0], &bug_found, 1);\n        serverAssert(ret == 1);\n\n        /* Reap the child pid. */\n        serverAssert(waitpid(pid, NULL, 0) == pid);\n    }\n\n    /* Cleanup */\n    ret = close(pipefd[0]);\n    serverAssert(!ret);\n    ret = close(pipefd[1]);\n    serverAssert(!ret);\n    ret = munmap(p, map_size);\n    serverAssert(!ret);\n\n    return bug_found;\n}\n#endif /* __arm64__ */\n#endif /* __linux__ */\n\nvoid createPidFile(void) {\n    /* If pidfile requested, but no pidfile defined, use\n     * default pidfile path */\n    if (!cserver.pidfile) cserver.pidfile = zstrdup(CONFIG_DEFAULT_PID_FILE);\n\n    /* Try to write the pid file in a best-effort way. */\n    FILE *fp = fopen(cserver.pidfile,\"w\");\n    if (fp) {\n        fprintf(fp,\"%d\\n\",(int)getpid());\n        fclose(fp);\n    }\n}\n\nvoid daemonize(void) {\n    int fd;\n\n    if (fork() != 0) exit(0); /* parent exits */\n    setsid(); /* create a new session */\n\n    /* Every output goes to /dev/null. If Redis is daemonized but\n     * the 'logfile' is set to 'stdout' in the configuration file\n     * it will not log at all. */\n    if ((fd = open(\"/dev/null\", O_RDWR, 0)) != -1) {\n        dup2(fd, STDIN_FILENO);\n        dup2(fd, STDOUT_FILENO);\n        dup2(fd, STDERR_FILENO);\n        if (fd > STDERR_FILENO) close(fd);\n    }\n}\n\nvoid version(void) {\n    printf(\"KeyDB server v=%s sha=%s:%d malloc=%s bits=%d build=%llx\\n\",\n        KEYDB_REAL_VERSION,\n        redisGitSHA1(),\n        atoi(redisGitDirty()) > 0,\n        ZMALLOC_LIB,\n        sizeof(long) == 4 ? 32 : 64,\n        (unsigned long long) redisBuildId());\n    exit(0);\n}\n\nvoid usage(void) {\n    fprintf(stderr,\"Usage: ./keydb-server [/path/to/keydb.conf] [options] [-]\\n\");\n    fprintf(stderr,\"       ./keydb-server - (read config from stdin)\\n\");\n    fprintf(stderr,\"       ./keydb-server -v or --version\\n\");\n    fprintf(stderr,\"       ./keydb-server -h or --help\\n\");\n    fprintf(stderr,\"       ./keydb-server --test-memory <megabytes>\\n\\n\");\n    fprintf(stderr,\"Examples:\\n\");\n    fprintf(stderr,\"       ./keydb-server (run the server with default conf)\\n\");\n    fprintf(stderr,\"       ./keydb-server /etc/keydb/6379.conf\\n\");\n    fprintf(stderr,\"       ./keydb-server --port 7777\\n\");\n    fprintf(stderr,\"       ./keydb-server --port 7777 --replicaof 127.0.0.1 8888\\n\");\n    fprintf(stderr,\"       ./keydb-server /etc/mykeydb.conf --loglevel verbose -\\n\");\n    fprintf(stderr,\"       ./keydb-server /etc/mykeydb.conf --loglevel verbose\\n\\n\");\n    fprintf(stderr,\"Sentinel mode:\\n\");\n    fprintf(stderr,\"       ./keydb-server /etc/sentinel.conf --sentinel\\n\");\n    exit(1);\n}\n\nvoid redisAsciiArt(void) {\n#include \"asciilogo.h\"\n    size_t bufsize = 1024*16;\n    char *buf = (char*)zmalloc(bufsize, MALLOC_LOCAL);\n    const char *mode;\n\n    if (g_pserver->cluster_enabled) mode = \"cluster\";\n    else if (g_pserver->sentinel_mode) mode = \"sentinel\";\n    else mode = \"standalone\";\n\n    /* Show the ASCII logo if: log file is stdout AND stdout is a\n     * tty AND syslog logging is disabled. Also show logo if the user\n     * forced us to do so via keydb.conf. */\n    int show_logo = ((!g_pserver->syslog_enabled &&\n                      g_pserver->logfile[0] == '\\0' &&\n                      isatty(fileno(stdout))) ||\n                     g_pserver->always_show_logo);\n\n    if (!show_logo) {\n        serverLog(LL_NOTICE,\n            \"Running mode=%s, port=%d.\",\n            mode, g_pserver->port ? g_pserver->port : g_pserver->tls_port\n        );\n    } else {\n        sds motd = fetchMOTD(true, cserver.enable_motd);\n        snprintf(buf,bufsize,ascii_logo,\n            KEYDB_REAL_VERSION,\n            redisGitSHA1(),\n            strtol(redisGitDirty(),NULL,10) > 0,\n            (sizeof(long) == 8) ? \"64\" : \"32\",\n            mode, g_pserver->port ? g_pserver->port : g_pserver->tls_port,\n            (long) getpid(),\n            motd ? motd : \"\"\n        );\n        if (motd)\n            freeMOTD(motd);\n        serverLogRaw(LL_NOTICE|LL_RAW,buf);\n    }\n\n    zfree(buf);\n}\n\nint changeBindAddr(sds *addrlist, int addrlist_len, bool fFirstCall) {\n    int i;\n    int result = C_OK;\n\n    char *prev_bindaddr[CONFIG_BINDADDR_MAX];\n    int prev_bindaddr_count;\n\n    /* Close old TCP and TLS servers */\n    closeSocketListeners(&serverTL->ipfd);\n    closeSocketListeners(&serverTL->tlsfd);\n\n    /* Keep previous settings */\n    prev_bindaddr_count = g_pserver->bindaddr_count;\n    memcpy(prev_bindaddr, g_pserver->bindaddr, sizeof(g_pserver->bindaddr));\n\n    /* Copy new settings */\n    memset(g_pserver->bindaddr, 0, sizeof(g_pserver->bindaddr));\n    for (i = 0; i < addrlist_len; i++) {\n        g_pserver->bindaddr[i] = zstrdup(addrlist[i]);\n    }\n    g_pserver->bindaddr_count = addrlist_len;\n\n    /* Bind to the new port */\n    if ((g_pserver->port != 0 && listenToPort(g_pserver->port, &serverTL->ipfd, (cserver.cthreads > 1), fFirstCall) != C_OK) ||\n        (g_pserver->tls_port != 0 && listenToPort(g_pserver->tls_port, &serverTL->tlsfd, (cserver.cthreads > 1), fFirstCall) != C_OK)) {\n        serverLog(LL_WARNING, \"Failed to bind, trying to restore old listening sockets.\");\n\n        /* Restore old bind addresses */\n        for (i = 0; i < addrlist_len; i++) {\n            zfree(g_pserver->bindaddr[i]);\n        }\n        memcpy(g_pserver->bindaddr, prev_bindaddr, sizeof(g_pserver->bindaddr));\n        g_pserver->bindaddr_count = prev_bindaddr_count;\n\n        /* Re-Listen TCP and TLS */\n        serverTL->ipfd.count = 0;\n        if (g_pserver->port != 0 && listenToPort(g_pserver->port, &serverTL->ipfd, (cserver.cthreads > 1), false) != C_OK) {\n            serverPanic(\"Failed to restore old listening sockets.\");\n        }\n\n        serverTL->tlsfd.count = 0;\n        if (g_pserver->tls_port != 0 && listenToPort(g_pserver->tls_port, &serverTL->tlsfd, (cserver.cthreads > 1), false) != C_OK) {\n            serverPanic(\"Failed to restore old listening sockets.\");\n        }\n\n        result = C_ERR;\n    } else {\n        /* Free old bind addresses */\n        for (i = 0; i < prev_bindaddr_count; i++) {\n            zfree(prev_bindaddr[i]);\n        }\n    }\n\n    /* Create TCP and TLS event handlers */\n    if (createSocketAcceptHandler(&serverTL->ipfd, acceptTcpHandler) != C_OK) {\n        serverPanic(\"Unrecoverable error creating TCP socket accept handler.\");\n    }\n    if (createSocketAcceptHandler(&serverTL->tlsfd, acceptTLSHandler) != C_OK) {\n        serverPanic(\"Unrecoverable error creating TLS socket accept handler.\");\n    }\n\n    if (cserver.set_proc_title && fFirstCall) redisSetProcTitle(NULL);\n\n    return result;\n}\n\nint changeListenPort(int port, socketFds *sfd, aeFileProc *accept_handler, bool fFirstCall) {\n    socketFds new_sfd = {{0}};\n\n    /* Just close the server if port disabled */\n    if (port == 0) {\n        closeSocketListeners(sfd);\n        if (cserver.set_proc_title && fFirstCall) redisSetProcTitle(NULL);\n        return C_OK;\n    }\n\n    /* Bind to the new port */\n    if (listenToPort(port, &new_sfd, (cserver.cthreads > 1), fFirstCall) != C_OK) {\n        return C_ERR;\n    }\n\n    /* Create event handlers */\n    if (createSocketAcceptHandler(&new_sfd, accept_handler) != C_OK) {\n        closeSocketListeners(&new_sfd);\n        return C_ERR;\n    }\n\n    /* Close old servers */\n    closeSocketListeners(sfd);\n\n    /* Copy new descriptors */\n    sfd->count = new_sfd.count;\n    memcpy(sfd->fd, new_sfd.fd, sizeof(new_sfd.fd));\n\n    if (cserver.set_proc_title && fFirstCall) redisSetProcTitle(NULL);\n\n    return C_OK;\n}\n\nstatic void sigShutdownHandler(int sig) {\n    const char *msg;\n\n    switch (sig) {\n    case SIGINT:\n        msg = \"Received SIGINT scheduling shutdown...\";\n        break;\n    case SIGTERM:\n        msg = \"Received SIGTERM scheduling shutdown...\";\n        break;\n    default:\n        msg = \"Received shutdown signal, scheduling shutdown...\";\n    };\n\n    /* SIGINT is often delivered via Ctrl+C in an interactive session.\n     * If we receive the signal the second time, we interpret this as\n     * the user really wanting to quit ASAP without waiting to persist\n     * on disk. */\n    if ((g_pserver->shutdown_asap || g_pserver->soft_shutdown) && sig == SIGINT) {\n        serverLogFromHandler(LL_WARNING, \"You insist... exiting now.\");\n        rdbRemoveTempFile(g_pserver->rdbThreadVars.tmpfileNum, 1);\n        g_pserver->garbageCollector.shutdown();\n        _Exit(1); /* Exit with an error since this was not a clean shutdown. */\n    } else if (g_pserver->loading) {\n        serverLogFromHandler(LL_WARNING, \"Received shutdown signal during loading, exiting now.\");\n        _Exit(0);   // calling dtors is undesirable, exit immediately\n    }\n\n    serverLogFromHandler(LL_WARNING, msg);\n    if (g_pserver->config_soft_shutdown)\n        g_pserver->soft_shutdown = true;\n    else\n        g_pserver->shutdown_asap = 1;\n}\n\nvoid setupSignalHandlers(void) {\n    struct sigaction act;\n\n    /* When the SA_SIGINFO flag is set in sa_flags then sa_sigaction is used.\n     * Otherwise, sa_handler is used. */\n    sigemptyset(&act.sa_mask);\n    act.sa_flags = 0;\n    act.sa_handler = sigShutdownHandler;\n    sigaction(SIGTERM, &act, NULL);\n    sigaction(SIGINT, &act, NULL);\n\n    sigemptyset(&act.sa_mask);\n    act.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;\n    act.sa_sigaction = sigsegvHandler;\n    if(g_pserver->crashlog_enabled) {\n        sigaction(SIGSEGV, &act, NULL);\n        sigaction(SIGBUS, &act, NULL);\n        sigaction(SIGFPE, &act, NULL);\n        sigaction(SIGILL, &act, NULL);\n        sigaction(SIGABRT, &act, NULL);\n    }\n    return;\n}\n\nvoid removeSignalHandlers(void) {\n    struct sigaction act;\n    sigemptyset(&act.sa_mask);\n    act.sa_flags = SA_NODEFER | SA_RESETHAND;\n    act.sa_handler = SIG_DFL;\n    sigaction(SIGSEGV, &act, NULL);\n    sigaction(SIGBUS, &act, NULL);\n    sigaction(SIGFPE, &act, NULL);\n    sigaction(SIGILL, &act, NULL);\n    sigaction(SIGABRT, &act, NULL);\n}\n\n/* This is the signal handler for children process. It is currently useful\n * in order to track the SIGUSR1, that we send to a child in order to terminate\n * it in a clean way, without the parent detecting an error and stop\n * accepting writes because of a write error condition. */\nstatic void sigKillChildHandler(int sig) {\n    UNUSED(sig);\n    int level = g_pserver->in_fork_child == CHILD_TYPE_MODULE? LL_VERBOSE: LL_WARNING;\n    serverLogFromHandler(level, \"Received SIGUSR1 in child, exiting now.\");\n    exitFromChild(SERVER_CHILD_NOERROR_RETVAL);\n}\n\nvoid setupChildSignalHandlers(void) {\n    struct sigaction act;\n\n    /* When the SA_SIGINFO flag is set in sa_flags then sa_sigaction is used.\n     * Otherwise, sa_handler is used. */\n    sigemptyset(&act.sa_mask);\n    act.sa_flags = 0;\n    act.sa_handler = sigKillChildHandler;\n    sigaction(SIGUSR1, &act, NULL);\n    return;\n}\n\n/* After fork, the child process will inherit the resources\n * of the parent process, e.g. fd(socket or flock) etc.\n * should close the resources not used by the child process, so that if the\n * parent restarts it can bind/lock despite the child possibly still running. */\nvoid closeChildUnusedResourceAfterFork() {\n    closeListeningSockets(0);\n    if (g_pserver->cluster_enabled && g_pserver->cluster_config_file_lock_fd != -1)\n        close(g_pserver->cluster_config_file_lock_fd);  /* don't care if this fails */\n\n    for (int iel = 0; iel < cserver.cthreads; ++iel) {\n        aeClosePipesForForkChild(g_pserver->rgthreadvar[iel].el);\n    }\n    aeClosePipesForForkChild(g_pserver->modulethreadvar.el);\n\n    /* Clear cserver.pidfile, this is the parent pidfile which should not\n     * be touched (or deleted) by the child (on exit / crash) */\n    zfree(cserver.pidfile);\n    cserver.pidfile = NULL;\n}\n\nvoid executeWithoutGlobalLock(std::function<void()> func) {\n    serverAssert(GlobalLocksAcquired());\n\n    std::vector<client*> vecclients;\n    listIter li;\n    listNode *ln;\n    listRewind(g_pserver->clients, &li);\n\n    // All client locks must be acquired *after* the global lock is reacquired to prevent deadlocks\n    //  so unlock here, and save them for reacquisition later\n    while ((ln = listNext(&li)) != nullptr)\n    {\n        client *c = (client*)listNodeValue(ln);\n        if (c->lock.fOwnLock()) {\n            serverAssert(c->flags & CLIENT_PROTECTED || c->flags & CLIENT_EXECUTING_COMMAND);  // If the client is not protected we have no gurantee they won't be free'd in the event loop\n            c->lock.unlock();\n            vecclients.push_back(c);\n        }\n    }\n    \n    /* Since we're about to release our lock we need to flush the repl backlog queue */\n    bool fReplBacklog = g_pserver->repl_batch_offStart >= 0;\n    if (fReplBacklog) {\n        flushReplBacklogToClients();\n        g_pserver->repl_batch_idxStart = -1;\n        g_pserver->repl_batch_offStart = -1;\n    }\n\n    aeReleaseLock();\n    serverAssert(!GlobalLocksAcquired());\n    try {\n        func();\n    }\n    catch (...) {\n        // Caller expects us to be locked so fix and rethrow\n        AeLocker locker;\n        locker.arm(nullptr);\n        locker.release();\n        for (client *c : vecclients)\n            c->lock.lock();\n        throw;\n    }\n    \n    AeLocker locker;\n    locker.arm(nullptr);\n    locker.release();\n\n    // Restore it so the calling code is not confused\n    if (fReplBacklog) {\n        g_pserver->repl_batch_idxStart = g_pserver->repl_backlog_idx;\n        g_pserver->repl_batch_offStart = g_pserver->master_repl_offset;\n    }\n\n    for (client *c : vecclients)\n        c->lock.lock();\n}\n\n/* purpose is one of CHILD_TYPE_ types */\nint redisFork(int purpose) {\n    int childpid;\n    long long start = ustime();\n    \n    if (isMutuallyExclusiveChildType(purpose)) {\n        if (hasActiveChildProcess())\n            return -1;\n\n        openChildInfoPipe();\n    }\n    long long startWriteLock = ustime();\n    aeAcquireForkLock();\n    latencyAddSampleIfNeeded(\"fork-lock\",(ustime()-startWriteLock)/1000);\n    if ((childpid = fork()) == 0) {\n        /* Child */\n        aeForkLockInChild();\n        aeReleaseForkLock();\n        g_pserver->in_fork_child = purpose;\n        setOOMScoreAdj(CONFIG_OOM_BGCHILD);\n        setupChildSignalHandlers();\n        closeChildUnusedResourceAfterFork();\n    } else {\n        /* Parent */\n        aeReleaseForkLock();\n        g_pserver->stat_total_forks++;\n        g_pserver->stat_fork_time = ustime()-start;\n        g_pserver->stat_fork_rate = (double) zmalloc_used_memory() * 1000000 / g_pserver->stat_fork_time / (1024*1024*1024); /* GB per second. */\n        latencyAddSampleIfNeeded(\"fork\",g_pserver->stat_fork_time/1000);\n        if (childpid == -1) {\n            if (isMutuallyExclusiveChildType(purpose)) closeChildInfoPipe();\n            return -1;\n        }\n\n        /* The child_pid and child_type are only for mutual exclusive children.\n         * other child types should handle and store their pid's in dedicated variables.\n         *\n         * Today, we allows CHILD_TYPE_LDB to run in parallel with the other fork types:\n         * - it isn't used for production, so it will not make the server be less efficient\n         * - used for debugging, and we don't want to block it from running while other\n         *   forks are running (like RDB and AOF) */\n        if (isMutuallyExclusiveChildType(purpose)) {\n            g_pserver->child_pid = childpid;\n            g_pserver->child_type = purpose;\n            g_pserver->stat_current_cow_bytes = 0;\n            g_pserver->stat_current_cow_updated = 0;\n            g_pserver->stat_current_save_keys_processed = 0;\n            g_pserver->stat_module_progress = 0;\n            g_pserver->stat_current_save_keys_total = dbTotalServerKeyCount();\n        }\n\n        updateDictResizePolicy();\n        moduleFireServerEvent(REDISMODULE_EVENT_FORK_CHILD,\n                              REDISMODULE_SUBEVENT_FORK_CHILD_BORN,\n                              NULL);\n    }\n    return childpid;\n}\n\nvoid sendChildCowInfo(childInfoType info_type, const char *pname) {\n    sendChildInfoGeneric(info_type, 0, -1, pname);\n}\n\nvoid sendChildInfo(childInfoType info_type, size_t keys, const char *pname) {\n    sendChildInfoGeneric(info_type, keys, -1, pname);\n}\n\nextern \"C\" void memtest(size_t megabytes, int passes);\n\n/* Returns 1 if there is --sentinel among the arguments or if\n * argv[0] contains \"keydb-sentinel\". */\nint checkForSentinelMode(int argc, char **argv) {\n    int j;\n\n    if (strstr(argv[0],\"keydb-sentinel\") != NULL) return 1;\n    for (j = 1; j < argc; j++)\n        if (!strcmp(argv[j],\"--sentinel\")) return 1;\n    return 0;\n}\n\n/* Function called at startup to load RDB or AOF file in memory. */\nvoid loadDataFromDisk(void) {\n    long long start = ustime();\n\n    if (g_pserver->m_pstorageFactory)\n    {\n        for (int idb = 0; idb < cserver.dbnum; ++idb)\n        {\n            if (g_pserver->db[idb]->size() > 0)\n            {\n                serverLog(LL_NOTICE, \"Not loading the RDB because a storage provider is set and the database is not empty\");\n                return;\n            }\n        }\n        serverLog(LL_NOTICE, \"Loading the RDB even though we have a storage provider because the database is empty\");\n    }\n    \n    serverTL->gcEpoch = g_pserver->garbageCollector.startEpoch();\n    if (g_pserver->aof_state == AOF_ON) {\n        if (loadAppendOnlyFile(g_pserver->aof_filename) == C_OK)\n            serverLog(LL_NOTICE,\"DB loaded from append only file: %.3f seconds\",(float)(ustime()-start)/1000000);\n    } else if (g_pserver->rdb_filename != NULL || g_pserver->rdb_s3bucketpath != NULL) {\n        rdbSaveInfo rsi;\n        rsi.fForceSetKey = false;\n        errno = 0; /* Prevent a stale value from affecting error checking */\n        if (rdbLoad(&rsi,RDBFLAGS_NONE) == C_OK) {\n            serverLog(LL_NOTICE,\"DB loaded from disk: %.3f seconds\",\n                (float)(ustime()-start)/1000000);\n\n            /* Restore the replication ID / offset from the RDB file. */\n            if ((listLength(g_pserver->masters) || \n                (g_pserver->cluster_enabled && \n                nodeIsSlave(g_pserver->cluster->myself))) &&\n                rsi.repl_id_is_set &&\n                rsi.repl_offset != -1 &&\n                /* Note that older implementations may save a repl_stream_db\n                 * of -1 inside the RDB file in a wrong way, see more\n                 * information in function rdbPopulateSaveInfo. */\n                rsi.repl_stream_db != -1)\n            {\n                memcpy(g_pserver->replid,rsi.repl_id,sizeof(g_pserver->replid));\n                g_pserver->master_repl_offset = rsi.repl_offset;\n                if (g_pserver->repl_batch_offStart >= 0)\n                    g_pserver->repl_batch_offStart = g_pserver->master_repl_offset;\n            }\n            updateActiveReplicaMastersFromRsi(&rsi);\n            if (!g_pserver->fActiveReplica && listLength(g_pserver->masters)) {\n                redisMaster *mi = (redisMaster*)listNodeValue(listFirst(g_pserver->masters));\n                /* If we are a replica, create a cached master from this\n                * information, in order to allow partial resynchronizations\n                * with masters. */\n                replicationCacheMasterUsingMyself(mi);\n                selectDb(mi->cached_master,rsi.repl_stream_db);\n            }\n        } else if (errno != ENOENT) {\n            serverLog(LL_WARNING,\"Fatal error loading the DB: %s. Exiting.\",strerror(errno));\n            exit(1);\n        }\n    }\n    g_pserver->garbageCollector.endEpoch(serverTL->gcEpoch);\n    serverTL->gcEpoch.reset();\n}\n\nvoid redisOutOfMemoryHandler(size_t allocation_size) {\n    serverLog(LL_WARNING,\"Out Of Memory allocating %zu bytes!\",\n        allocation_size);\n    serverPanic(\"KeyDB aborting for OUT OF MEMORY. Allocating %zu bytes!\", \n        allocation_size);\n}\n\n/* Callback for sdstemplate on proc-title-template. See redis.conf for\n * supported variables.\n */\nstatic sds redisProcTitleGetVariable(const sds varname, void *arg)\n{\n    if (!strcmp(varname, \"title\")) {\n        return sdsnew((const char*)arg);\n    } else if (!strcmp(varname, \"listen-addr\")) {\n        if (g_pserver->port || g_pserver->tls_port)\n            return sdscatprintf(sdsempty(), \"%s:%u\",\n                                g_pserver->bindaddr_count ? g_pserver->bindaddr[0] : \"*\",\n                                g_pserver->port ? g_pserver->port : g_pserver->tls_port);\n        else\n            return sdscatprintf(sdsempty(), \"unixsocket:%s\", g_pserver->unixsocket);\n    } else if (!strcmp(varname, \"server-mode\")) {\n        if (g_pserver->cluster_enabled) return sdsnew(\"[cluster]\");\n        else if (g_pserver->sentinel_mode) return sdsnew(\"[sentinel]\");\n        else return sdsempty();\n    } else if (!strcmp(varname, \"config-file\")) {\n        return sdsnew(cserver.configfile ? cserver.configfile : \"-\");\n    } else if (!strcmp(varname, \"port\")) {\n        return sdscatprintf(sdsempty(), \"%u\", g_pserver->port);\n    } else if (!strcmp(varname, \"tls-port\")) {\n        return sdscatprintf(sdsempty(), \"%u\", g_pserver->tls_port);\n    } else if (!strcmp(varname, \"unixsocket\")) {\n        return sdsnew(g_pserver->unixsocket);\n    } else\n        return NULL;    /* Unknown variable name */\n}\n\n/* Expand the specified proc-title-template string and return a newly\n * allocated sds, or NULL. */\nstatic sds expandProcTitleTemplate(const char *_template, const char *title) {\n    sds res = sdstemplate(_template, redisProcTitleGetVariable, (void *) title);\n    if (!res)\n        return NULL;\n    return sdstrim(res, \" \");\n}\n/* Validate the specified template, returns 1 if valid or 0 otherwise. */\nint validateProcTitleTemplate(const char *_template) {\n    int ok = 1;\n    sds res = expandProcTitleTemplate(_template, \"\");\n    if (!res)\n        return 0;\n    if (sdslen(res) == 0) ok = 0;\n    sdsfree(res);\n    return ok;\n}\n\nint redisSetProcTitle(const char *title) {\n#ifdef USE_SETPROCTITLE\n    if (!title) title = cserver.exec_argv[0];\n    sds proc_title = expandProcTitleTemplate(cserver.proc_title_template, title);\n    if (!proc_title) return C_ERR;  /* Not likely, proc_title_template is validated */\n\n    setproctitle(\"%s\", proc_title);\n    sdsfree(proc_title);\n#else\n    UNUSED(title);\n#endif\n\n    return C_OK;\n}\n\nvoid redisSetCpuAffinity(const char *cpulist) {\n#ifdef USE_SETCPUAFFINITY\n    setcpuaffinity(cpulist);\n#else\n    UNUSED(cpulist);\n#endif\n}\n\n/* Send a notify message to systemd. Returns sd_notify return code which is\n * a positive number on success. */\nint redisCommunicateSystemd(const char *sd_notify_msg) {\n#ifdef HAVE_LIBSYSTEMD\n    int ret = sd_notify(0, sd_notify_msg);\n\n    if (ret == 0)\n        serverLog(LL_WARNING, \"systemd supervision error: NOTIFY_SOCKET not found!\");\n    else if (ret < 0)\n        serverLog(LL_WARNING, \"systemd supervision error: sd_notify: %d\", ret);\n    return ret;\n#else\n    UNUSED(sd_notify_msg);\n    return 0;\n#endif\n}\n\n/* Attempt to set up upstart supervision. Returns 1 if successful. */\nstatic int redisSupervisedUpstart(void) {\n    const char *upstart_job = getenv(\"UPSTART_JOB\");\n\n    if (!upstart_job) {\n        serverLog(LL_WARNING,\n                \"upstart supervision requested, but UPSTART_JOB not found!\");\n        return 0;\n    }\n\n    serverLog(LL_NOTICE, \"supervised by upstart, will stop to signal readiness.\");\n    raise(SIGSTOP);\n    unsetenv(\"UPSTART_JOB\");\n    return 1;\n}\n\n/* Attempt to set up systemd supervision. Returns 1 if successful. */\nstatic int redisSupervisedSystemd(void) {\n#ifndef HAVE_LIBSYSTEMD\n    serverLog(LL_WARNING,\n            \"systemd supervision requested or auto-detected, but Redis is compiled without libsystemd support!\");\n    return 0;\n#else\n    if (redisCommunicateSystemd(\"STATUS=Redis is loading...\\n\") <= 0)\n        return 0;\n    serverLog(LL_NOTICE,\n        \"Supervised by systemd. Please make sure you set appropriate values for TimeoutStartSec and TimeoutStopSec in your service unit.\");\n    return 1;\n#endif\n}\n\nint redisIsSupervised(int mode) {\n    int ret = 0;\n\n    if (mode == SUPERVISED_AUTODETECT) {\n        if (getenv(\"UPSTART_JOB\")) {\n            serverLog(LL_VERBOSE, \"Upstart supervision detected.\");\n            mode = SUPERVISED_UPSTART;\n        } else if (getenv(\"NOTIFY_SOCKET\")) {\n            serverLog(LL_VERBOSE, \"Systemd supervision detected.\");\n            mode = SUPERVISED_SYSTEMD;\n        }\n    } else if (mode == SUPERVISED_UPSTART) {\n        return redisSupervisedUpstart();\n    } else if (mode == SUPERVISED_SYSTEMD) {\n        serverLog(LL_WARNING,\n            \"WARNING supervised by systemd - you MUST set appropriate values for TimeoutStartSec and TimeoutStopSec in your service unit.\");\n        return redisCommunicateSystemd(\"STATUS=KeyDB is loading...\\n\");\n    }\n\n    switch (mode) {\n        case SUPERVISED_UPSTART:\n            ret = redisSupervisedUpstart();\n            break;\n        case SUPERVISED_SYSTEMD:\n            ret = redisSupervisedSystemd();\n            break;\n        default:\n            break;\n    }\n\n    if (ret)\n        cserver.supervised_mode = mode;\n\n    return ret;\n}\n\nuint64_t getMvccTstamp()\n{\n    uint64_t rval;\n    __atomic_load(&g_pserver->mvcc_tstamp, &rval, __ATOMIC_ACQUIRE);\n    return rval;\n}\n\nvoid incrementMvccTstamp()\n{\n    uint64_t msPrev;\n    __atomic_load(&g_pserver->mvcc_tstamp, &msPrev, __ATOMIC_ACQUIRE);\n    msPrev >>= MVCC_MS_SHIFT;  // convert to milliseconds\n\n    long long mst;\n    __atomic_load(&g_pserver->mstime, &mst, __ATOMIC_ACQUIRE);\n    if (msPrev >= (uint64_t)mst)  // we can be greater if the count overflows\n    {\n        __atomic_fetch_add(&g_pserver->mvcc_tstamp, 1, __ATOMIC_RELEASE);\n    }\n    else\n    {\n        uint64_t val = ((uint64_t)mst) << MVCC_MS_SHIFT;\n        __atomic_store(&g_pserver->mvcc_tstamp, &val, __ATOMIC_RELEASE);\n    }\n}\n\nvoid OnTerminate()\n{\n    /* Any uncaught exception will call std::terminate().\n        We want this handled like a segfault (printing the stack trace etc).\n        The easiest way to achieve that is to acutally segfault, so we assert\n        here.\n    */\n    auto exception = std::current_exception();\n    if (exception != nullptr)\n    {\n        try\n        {\n            std::rethrow_exception(exception);\n        }\n        catch (const char *szErr)\n        {\n            serverLog(LL_WARNING, \"Crashing on uncaught exception: %s\", szErr);\n        }\n        catch (std::string str)\n        {\n            serverLog(LL_WARNING, \"Crashing on uncaught exception: %s\", str.c_str());\n        }\n        catch (...)\n        {\n            // NOP\n        }\n    }\n\n    serverPanic(\"std::teminate() called\");\n}\n\nvoid wakeTimeThread() {\n    updateCachedTime();\n    aeThreadOffline();\n    std::unique_lock<fastlock> lock(time_thread_lock);\n    aeThreadOnline();\n    if (sleeping_threads >= cserver.cthreads)\n        time_thread_cv.notify_one();\n    sleeping_threads--;\n    serverAssert(sleeping_threads >= 0);\n}\n\nvoid *timeThreadMain(void*) {\n    timespec delay;\n    delay.tv_sec = 0;\n    delay.tv_nsec = 100;\n    int cycle_count = 0;\n    aeThreadOnline();\n    while (true) {\n        {\n            aeThreadOffline();\n            std::unique_lock<fastlock> lock(time_thread_lock);\n            aeThreadOnline();\n            if (sleeping_threads >= cserver.cthreads) {\n                aeThreadOffline();\n                time_thread_cv.wait(lock);\n                aeThreadOnline();\n                cycle_count = 0;\n            }\n        }\n        updateCachedTime();\n        if (cycle_count == MAX_CYCLES_TO_HOLD_FORK_LOCK) {\n            aeThreadOffline();\n            aeThreadOnline();\n            cycle_count = 0;\n        }\n#if defined(__APPLE__)\n        nanosleep(&delay, nullptr);\n#else\n        clock_nanosleep(CLOCK_MONOTONIC, 0, &delay, NULL);\n#endif\n        cycle_count++;\n    }\n    aeThreadOffline();\n}\n\nvoid *workerThreadMain(void *parg)\n{\n    int iel = (int)((int64_t)parg);\n    serverLog(LL_NOTICE, \"Thread %d alive.\", iel);\n    serverTL = g_pserver->rgthreadvar+iel;  // set the TLS threadsafe global\n    tlsInitThread();\n\n    if (iel != IDX_EVENT_LOOP_MAIN)\n    {\n        aeThreadOnline();\n        aeAcquireLock();\n        initNetworkingThread(iel, cserver.cthreads > 1);\n        aeReleaseLock();\n        aeThreadOffline();\n    }\n\n    moduleAcquireGIL(true); // Normally afterSleep acquires this, but that won't be called on the first run\n    aeThreadOnline();\n    aeEventLoop *el = g_pserver->rgthreadvar[iel].el;\n    try\n    {\n        aeMain(el);\n    }\n    catch (ShutdownException)\n    {\n    }\n    aeThreadOffline();\n    moduleReleaseGIL(true);\n    serverAssert(!GlobalLocksAcquired());\n    aeDeleteEventLoop(el);\n\n    tlsCleanupThread();\n    return NULL;\n}\n\nstatic void validateConfiguration()\n{\n    updateMasterAuth();\n    \n    if (cserver.cthreads > (int)std::thread::hardware_concurrency()) {\n        serverLog(LL_WARNING, \"WARNING: server-threads is greater than this machine's core count.  Truncating to %u threads\", std::thread::hardware_concurrency());\n        cserver.cthreads = (int)std::thread::hardware_concurrency();\n        cserver.cthreads = std::max(cserver.cthreads, 1);\t// in case of any weird sign overflows\n    }\n\n    if (g_pserver->enable_multimaster && !g_pserver->fActiveReplica) {\n        serverLog(LL_WARNING, \"ERROR: Multi Master requires active replication to be enabled.\");\n        serverLog(LL_WARNING, \"\\tKeyDB will now exit.  Please update your configuration file.\");\n        exit(EXIT_FAILURE);\n    }\n\n    g_pserver->repl_backlog_size = g_pserver->repl_backlog_config_size; // this is normally set in the update logic, but not on initial config\n}\n\nint iAmMaster(void) {\n    return ((!g_pserver->cluster_enabled && (listLength(g_pserver->masters) == 0 || g_pserver->fActiveReplica)) ||\n            (g_pserver->cluster_enabled && nodeIsMaster(g_pserver->cluster->myself)));\n}\n\nbool initializeStorageProvider(const char **err);\n\n#ifdef REDIS_TEST\ntypedef int redisTestProc(int argc, char **argv, int accurate);\nstruct redisTest {\n    char *name;\n    redisTestProc *proc;\n    int failed;\n} redisTests[] = {\n    {\"ziplist\", ziplistTest},\n    {\"quicklist\", quicklistTest},\n    {\"intset\", intsetTest},\n    {\"zipmap\", zipmapTest},\n    {\"sha1test\", sha1Test},\n    {\"util\", utilTest},\n    {\"endianconv\", endianconvTest},\n    {\"crc64\", crc64Test},\n    {\"zmalloc\", zmalloc_test},\n    {\"sds\", sdsTest},\n    {\"dict\", dictTest}\n};\nredisTestProc *getTestProcByName(const char *name) {\n    int numtests = sizeof(redisTests)/sizeof(struct redisTest);\n    for (int j = 0; j < numtests; j++) {\n        if (!strcasecmp(name,redisTests[j].name)) {\n            return redisTests[j].proc;\n        }\n    }\n    return NULL;\n}\n#endif\n\nint main(int argc, char **argv) {\n    struct timeval tv;\n    int j;\n    char config_from_stdin = 0;\n\n    std::set_terminate(OnTerminate);\n\n    {\n    SymVer version;\n    version = parseVersion(KEYDB_REAL_VERSION);\n    serverAssert(version.major >= 0 && version.minor >= 0 && version.build >= 0);\n    serverAssert(compareVersion(&version) == VersionCompareResult::EqualVersion);\n    }\n\n#ifdef USE_MEMKIND\n    storage_init(NULL, 0);\n#endif\n\n#ifdef REDIS_TEST\n    if (argc >= 3 && !strcasecmp(argv[1], \"test\")) {\n        int accurate = 0;\n        for (j = 3; j < argc; j++) {\n            if (!strcasecmp(argv[j], \"--accurate\")) {\n                accurate = 1;\n            }\n        }\n\n        if (!strcasecmp(argv[2], \"all\")) {\n            int numtests = sizeof(redisTests)/sizeof(struct redisTest);\n            for (j = 0; j < numtests; j++) {\n                redisTests[j].failed = (redisTests[j].proc(argc,argv,accurate) != 0);\n            }\n\n            /* Report tests result */\n            int failed_num = 0;\n            for (j = 0; j < numtests; j++) {\n                if (redisTests[j].failed) {\n                    failed_num++;\n                    printf(\"[failed] Test - %s\\n\", redisTests[j].name);\n                } else {\n                    printf(\"[ok] Test - %s\\n\", redisTests[j].name);\n                }\n            }\n\n            printf(\"%d tests, %d passed, %d failed\\n\", numtests,\n                   numtests-failed_num, failed_num);\n\n            return failed_num == 0 ? 0 : 1;\n        } else {\n            redisTestProc *proc = getTestProcByName(argv[2]);\n            if (!proc) return -1; /* test not found */\n            return proc(argc,argv,accurate);\n        }\n\n        return 0;\n    }\n#endif\n\n    /* We need to initialize our libraries, and the server configuration. */\n#ifdef INIT_SETPROCTITLE_REPLACEMENT\n    spt_init(argc, argv);\n#endif\n    setlocale(LC_COLLATE,\"\");\n    tzset(); /* Populates 'timezone' global. */\n    zmalloc_set_oom_handler(redisOutOfMemoryHandler);\n    srand(time(NULL)^getpid());\n    srandom(time(NULL)^getpid());\n    gettimeofday(&tv,NULL);\n    init_genrand64(((long long) tv.tv_sec * 1000000 + tv.tv_usec) ^ getpid());\n    crc64_init();\n\n    /* Store umask value. Because umask(2) only offers a set-and-get API we have\n     * to reset it and restore it back. We do this early to avoid a potential\n     * race condition with threads that could be creating files or directories.\n     */\n    umask(g_pserver->umask = umask(0777));\n    \n    serverAssert(g_pserver->repl_batch_offStart < 0);\n\n    uint8_t hashseed[16];\n    getRandomHexChars((char*)hashseed,sizeof(hashseed));\n    dictSetHashFunctionSeed(hashseed);\n    g_pserver->sentinel_mode = checkForSentinelMode(argc,argv);\n    initServerConfig();\n    serverTL = &g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN];\n    aeThreadOnline();\n    aeAcquireLock();    // We own the lock on boot\n    ACLInit(); /* The ACL subsystem must be initialized ASAP because the\n                  basic networking code and client creation depends on it. */\n    moduleInitModulesSystem();\n    tlsInit();\n\n    /* Store the executable path and arguments in a safe place in order\n     * to be able to restart the server later. */\n    cserver.executable = getAbsolutePath(argv[0]);\n    cserver.exec_argv = (char**)zmalloc(sizeof(char*)*(argc+1), MALLOC_LOCAL);\n    cserver.exec_argv[argc] = NULL;\n    for (j = 0; j < argc; j++) cserver.exec_argv[j] = zstrdup(argv[j]);\n\n    /* We need to init sentinel right now as parsing the configuration file\n     * in sentinel mode will have the effect of populating the sentinel\n     * data structures with master nodes to monitor. */\n    if (g_pserver->sentinel_mode) {\n        initSentinelConfig();\n        initSentinel();\n    }\n\n    /* Check if we need to start in keydb-check-rdb/aof mode. We just execute\n     * the program main. However the program is part of the Redis executable\n     * so that we can easily execute an RDB check on loading errors. */\n    if (strstr(argv[0],\"keydb-check-rdb\") != NULL)\n        redis_check_rdb_main(argc,(const char**)argv,NULL);\n    else if (strstr(argv[0],\"keydb-check-aof\") != NULL)\n        redis_check_aof_main(argc,argv);\n\n    if (argc >= 2) {\n        j = 1; /* First option to parse in argv[] */\n        sds options = sdsempty();\n\n        /* Handle special options --help and --version */\n        if (strcmp(argv[1], \"-v\") == 0 ||\n            strcmp(argv[1], \"--version\") == 0) version();\n        if (strcmp(argv[1], \"--help\") == 0 ||\n            strcmp(argv[1], \"-h\") == 0) usage();\n        if (strcmp(argv[1], \"--test-memory\") == 0) {\n            if (argc == 3) {\n                memtest(atoi(argv[2]),50);\n                exit(0);\n            } else {\n                fprintf(stderr,\"Please specify the amount of memory to test in megabytes.\\n\");\n                fprintf(stderr,\"Example: ./keydb-server --test-memory 4096\\n\\n\");\n                exit(1);\n            }\n        }\n        /* Parse command line options\n         * Precedence wise, File, stdin, explicit options -- last config is the one that matters.\n         *\n         * First argument is the config file name? */\n        if (argv[1][0] != '-') {\n            /* Replace the config file in g_pserver->exec_argv with its absolute path. */\n            cserver.configfile = getAbsolutePath(argv[1]);\n            zfree(cserver.exec_argv[1]);\n            cserver.exec_argv[1] = zstrdup(cserver.configfile);\n            j = 2; // Skip this arg when parsing options\n        }\n        while(j < argc) {\n            /* Either first or last argument - Should we read config from stdin? */\n            if (argv[j][0] == '-' && argv[j][1] == '\\0' && (j == 1 || j == argc-1)) {\n                config_from_stdin = 1;\n            }\n            /* All the other options are parsed and conceptually appended to the\n             * configuration file. For instance --port 6380 will generate the\n             * string \"port 6380\\n\" to be parsed after the actual config file\n             * and stdin input are parsed (if they exist). */\n            else if (argv[j][0] == '-' && argv[j][1] == '-') {\n                /* Option name */\n                if (sdslen(options)) options = sdscat(options,\"\\n\");\n                options = sdscat(options,argv[j]+2);\n                options = sdscat(options,\" \");\n            } else {\n                /* Option argument */\n                options = sdscatrepr(options,argv[j],strlen(argv[j]));\n                options = sdscat(options,\" \");\n            }\n            j++;\n        }\n\n        loadServerConfig(cserver.configfile, config_from_stdin, options);\n        if (g_pserver->sentinel_mode) loadSentinelConfigFromQueue();\n        sdsfree(options);\n    }\n\n    if (g_pserver->syslog_enabled) {\n        openlog(g_pserver->syslog_ident, LOG_PID | LOG_NDELAY | LOG_NOWAIT,\n            g_pserver->syslog_facility);\n    }\n    \n    if (g_pserver->sentinel_mode) sentinelCheckConfigFile();\n\n    cserver.supervised = redisIsSupervised(cserver.supervised_mode);\n    int background = cserver.daemonize && !cserver.supervised;\n    if (background) daemonize();\n\n    serverLog(LL_WARNING, \"oO0OoO0OoO0Oo KeyDB is starting oO0OoO0OoO0Oo\");\n    serverLog(LL_WARNING,\n        \"KeyDB version=%s, bits=%d, commit=%s, modified=%d, pid=%d, just started\",\n            KEYDB_REAL_VERSION,\n            (sizeof(long) == 8) ? 64 : 32,\n            redisGitSHA1(),\n            strtol(redisGitDirty(),NULL,10) > 0,\n            (int)getpid());\n\n    if (argc == 1) {\n        serverLog(LL_WARNING, \"Warning: no config file specified, using the default config. In order to specify a config file use %s /path/to/keydb.conf\", argv[0]);\n    } else {\n        serverLog(LL_WARNING, \"Configuration loaded\");\n    }\n\n    validateConfiguration();\n\n    if (!g_pserver->sentinel_mode) {\n    #ifdef __linux__\n        linuxMemoryWarnings();\n    #if defined (__arm64__)\n        int ret;\n        if ((ret = linuxMadvFreeForkBugCheck())) {\n            if (ret == 1)\n                serverLog(LL_WARNING,\"WARNING Your kernel has a bug that could lead to data corruption during background save. \"\n                                        \"Please upgrade to the latest stable kernel.\");\n            else\n                serverLog(LL_WARNING, \"Failed to test the kernel for a bug that could lead to data corruption during background save. \"\n                                        \"Your system could be affected, please report this error.\");\n            if (!checkIgnoreWarning(\"ARM64-COW-BUG\")) {\n                serverLog(LL_WARNING,\"KeyDB will now exit to prevent data corruption. \"\n                                        \"Note that it is possible to suppress this warning by setting the following config: ignore-warnings ARM64-COW-BUG\");\n                exit(1);\n            }\n        }\n    #endif /* __arm64__ */\n    #endif /* __linux__ */\n    }\n\n\n    const char *err;\n    if (!initializeStorageProvider(&err))\n    {\n        serverLog(LL_WARNING, \"Failed to initialize storage provider: %s\",err);\n        exit(EXIT_FAILURE);\n    }\n\n    for (int iel = 0; iel < cserver.cthreads; ++iel)\n    {\n        initServerThread(g_pserver->rgthreadvar+iel, iel == IDX_EVENT_LOOP_MAIN);\n    }\n\n    initServerThread(&g_pserver->modulethreadvar, false);\n    readOOMScoreAdj();\n    initServer();\n    initNetworking(cserver.cthreads > 1 /* fReusePort */);\n\n    if (background || cserver.pidfile) createPidFile();\n    if (cserver.set_proc_title) redisSetProcTitle(NULL);\n    redisAsciiArt();\n    checkTcpBacklogSettings();\n\n    if (!g_pserver->sentinel_mode) {\n        /* Things not needed when running in Sentinel mode. */\n        serverLog(LL_WARNING,\"Server initialized\");\n        moduleInitModulesSystemLast();\n        moduleLoadFromQueue();\n        ACLLoadUsersAtStartup();\n\n        // special case of FUZZING load from stdin then quit\n        if (argc > 1 && strstr(argv[1],\"rdbfuzz-mode\") != NULL)\n        {\n            zmalloc_set_oom_handler(fuzzOutOfMemoryHandler);\n#ifdef __AFL_HAVE_MANUAL_CONTROL\n            __AFL_INIT();\n#endif\n            rio rdb;\n            rdbSaveInfo rsi;\n            startLoadingFile(stdin, (char*)\"stdin\", 0);\n            rioInitWithFile(&rdb,stdin);\n            rdbLoadRio(&rdb,0,&rsi);\n            stopLoading(true);\n            return EXIT_SUCCESS;\n        }\n\n        InitServerLast();\n\n        try {\n            loadDataFromDisk();\n        } catch (ShutdownException) {\n            _Exit(EXIT_SUCCESS);\n        }\n\n        if (g_pserver->cluster_enabled) {\n            if (verifyClusterConfigWithData() == C_ERR) {\n                serverLog(LL_WARNING,\n                    \"You can't have keys in a DB different than DB 0 when in \"\n                    \"Cluster mode. Exiting.\");\n                exit(1);\n            }\n        }\n        if (g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].ipfd.count > 0 && g_pserver->rgthreadvar[IDX_EVENT_LOOP_MAIN].tlsfd.count > 0)\n            serverLog(LL_NOTICE,\"Ready to accept connections\");\n        if (g_pserver->sofd > 0)\n            serverLog(LL_NOTICE,\"The server is now ready to accept connections at %s\", g_pserver->unixsocket);\n        if (cserver.supervised_mode == SUPERVISED_SYSTEMD) {\n            if (!listLength(g_pserver->masters)) {\n                redisCommunicateSystemd(\"STATUS=Ready to accept connections\\n\");\n            } else {\n                redisCommunicateSystemd(\"STATUS=Ready to accept connections in read-only mode. Waiting for MASTER <-> REPLICA sync\\n\");\n            }\n            redisCommunicateSystemd(\"READY=1\\n\");\n        }\n    } else {\n        ACLLoadUsersAtStartup();\n        InitServerLast();\n        sentinelIsRunning();\n        if (cserver.supervised_mode == SUPERVISED_SYSTEMD) {\n            redisCommunicateSystemd(\"STATUS=Ready to accept connections\\n\");\n            redisCommunicateSystemd(\"READY=1\\n\");\n        }\n    }\n\n    if (g_pserver->rdb_filename == nullptr)\n    {\n        if (g_pserver->rdb_s3bucketpath == nullptr)\n            g_pserver->rdb_filename = zstrdup(CONFIG_DEFAULT_RDB_FILENAME);\n        else\n            g_pserver->repl_diskless_sync = TRUE;\n    }\n\n    if (cserver.cthreads > 4) {\n        serverLog(LL_WARNING, \"Warning: server-threads is set to %d.  This is above the maximum recommend value of 4, please ensure you've verified this is actually faster on your machine.\", cserver.cthreads);\n    }\n\n    /* Warning the user about suspicious maxmemory setting. */\n    if (g_pserver->maxmemory > 0 && g_pserver->maxmemory < 1024*1024) {\n        serverLog(LL_WARNING,\"WARNING: You specified a maxmemory value that is less than 1MB (current value is %llu bytes). Are you sure this is what you really want?\", g_pserver->maxmemory);\n    }\n\n    redisSetCpuAffinity(g_pserver->server_cpulist);\n    aeReleaseLock();    //Finally we can dump the lock\n    aeThreadOffline();\n    moduleReleaseGIL(true);\n    \n    setOOMScoreAdj(-1);\n    serverAssert(cserver.cthreads > 0 && cserver.cthreads <= MAX_EVENT_LOOPS);\n\n    pthread_create(&cserver.time_thread_id, nullptr, timeThreadMain, nullptr);\n    if (cserver.time_thread_priority) {\n        struct sched_param time_thread_priority;\n        time_thread_priority.sched_priority = sched_get_priority_max(SCHED_FIFO);\n        pthread_setschedparam(cserver.time_thread_id, SCHED_FIFO, &time_thread_priority);\n    }\n\n    pthread_attr_t tattr;\n    pthread_attr_init(&tattr);\n    pthread_attr_setstacksize(&tattr, 1 << 23); // 8 MB\n    for (int iel = 0; iel < cserver.cthreads; ++iel)\n    {\n        pthread_create(g_pserver->rgthread + iel, &tattr, workerThreadMain, (void*)((int64_t)iel));\n        if (cserver.fThreadAffinity)\n        {\n#ifdef __linux__\n            cpu_set_t cpuset;\n            CPU_ZERO(&cpuset);\n            CPU_SET(iel + cserver.threadAffinityOffset, &cpuset);\n            if (pthread_setaffinity_np(g_pserver->rgthread[iel], sizeof(cpu_set_t), &cpuset) == 0)\n            {\n                serverLog(LL_NOTICE, \"Binding thread %d to cpu %d\", iel, iel + cserver.threadAffinityOffset + 1);\n            }\n#else\n\t\t\tserverLog(LL_WARNING, \"CPU pinning not available on this platform\");\n#endif\n        }\n    }\n\n    /* Block SIGALRM from this thread, it should only be received on a server thread */\n    sigset_t sigset;\n    sigemptyset(&sigset);\n    sigaddset(&sigset, SIGALRM);\n    pthread_sigmask(SIG_BLOCK, &sigset, nullptr);\n\n    /* The main thread sleeps until all the workers are done.\n        this is so that all worker threads are orthogonal in their startup/shutdown */\n    void *pvRet;\n    for (int iel = 0; iel < cserver.cthreads; ++iel)\n        pthread_join(g_pserver->rgthread[iel], &pvRet);\n\n    /* free our databases */\n    bool fLockAcquired = aeTryAcquireLock(false);\n    g_pserver->shutdown_asap = true;    // flag that we're in shutdown\n    if (!fLockAcquired)\n        g_fInCrash = true;  // We don't actually crash right away, because we want to sync any storage providers\n    \n    saveMasterStatusToStorage(true);\n    for (int idb = 0; idb < cserver.dbnum; ++idb) {\n        g_pserver->db[idb]->storageProviderDelete();\n    }\n    delete g_pserver->metadataDb;\n\n    // If we couldn't acquire the global lock it means something wasn't shutdown and we'll probably deadlock\n    serverAssert(fLockAcquired);\n\n    g_pserver->garbageCollector.shutdown();\n    delete g_pserver->m_pstorageFactory;\n\n    // Don't return because we don't want to run any global dtors\n    _Exit(EXIT_SUCCESS);\n    return 0;   // Ensure we're well formed even though this won't get hit\n}\n\n/* The End */\n"
  },
  {
    "path": "src/server.h",
    "content": "/*\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __REDIS_H\n#define __REDIS_H\n\n#define TRUE 1\n#define FALSE 0\n\n#include \"fmacros.h\"\n#include \"config.h\"\n#include \"solarisfixes.h\"\n#include \"rio.h\"\n#include \"atomicvar.h\"\n\n#include <concurrentqueue.h>\n#include <blockingconcurrentqueue.h>\n\n#include <stdio.h>\n#include <stdlib.h>\n#include <cmath>\n#include <string.h>\n#include <string>\n#include <time.h>\n#include <limits.h>\n#include <unistd.h>\n#include <errno.h>\n#include <inttypes.h>\n#include <pthread.h>\n#include <syslog.h>\n#include <netinet/in.h>\n#include <atomic>\n#include <vector>\n#include <algorithm>\n#include <memory>\n#include <set>\n#include <map>\n#include <string>\n#include <mutex>\n#include <unordered_set>\n#ifdef __cplusplus\nextern \"C\" {\n#include <lua.h>\n}\n#else\n#include <lua.h>\n#endif\n#include <sys/socket.h>\n#include <signal.h>\n\n#ifdef HAVE_LIBSYSTEMD\n#include <systemd/sd-daemon.h>\n#endif\n\ntypedef long long mstime_t; /* millisecond time type. */\ntypedef long long ustime_t; /* microsecond time type. */\n\n#include \"fastlock.h\"\n#include \"ae.h\"      /* Event driven programming library */\n#include \"sds.h\"     /* Dynamic safe strings */\n#include \"dict.h\"    /* Hash tables */\n#include \"adlist.h\"  /* Linked lists */\n#include \"zmalloc.h\" /* total memory usage aware version of malloc/free */\n#include \"anet.h\"    /* Networking the easy way */\n#include \"ziplist.h\" /* Compact list data structure */\n#include \"intset.h\"  /* Compact integer set structure */\n#include \"version.h\" /* Version macro */\n#include \"util.h\"    /* Misc functions useful in many places */\n#include \"latency.h\" /* Latency monitor API */\n#include \"sparkline.h\" /* ASCII graphs API */\n#include \"quicklist.h\"  /* Lists are encoded as linked lists of\n                           N-elements flat arrays */\n#include \"rax.h\"     /* Radix tree */\n#include \"uuid.h\"\n#include \"semiorderedset.h\"\n#include \"connection.h\" /* Connection abstraction */\n#include \"serverassert.h\"\n#include \"expire.h\"\n#include \"readwritelock.h\"\n\n#define REDISMODULE_CORE 1\n#include \"redismodule.h\"    /* Redis modules API defines. */\n\n/* Following includes allow test functions to be called from Redis main() */\n#include \"zipmap.h\"\n#include \"sha1.h\"\n#include \"endianconv.h\"\n#include \"crc64.h\"\n#include \"IStorage.h\"\n#include \"StorageCache.h\"\n#include \"AsyncWorkQueue.h\"\n#include \"gc.h\"\n\n#define FImplies(x, y) (!(x) || (y))\n\n#define LOADING_BOOT 1\n#define LOADING_REPLICATION 2\n\n#define OVERLOAD_PROTECT_PERIOD_MS 10'000 // 10 seconds\n#define MAX_CLIENTS_SHED_PER_PERIOD (OVERLOAD_PROTECT_PERIOD_MS / 10)  // Restrict to one client per 10ms\n\nextern int g_fTestMode;\nextern struct redisServer *g_pserver;\n\nstruct redisObject;\nclass robj_roptr\n{\n    const redisObject *m_ptr;\n\npublic:\n    robj_roptr()\n        : m_ptr(nullptr)\n        {}\n    robj_roptr(const redisObject *ptr)\n        : m_ptr(ptr)\n        {}\n    robj_roptr(const robj_roptr&) = default;\n    robj_roptr(robj_roptr&&) = default;\n\n    robj_roptr &operator=(const robj_roptr&) = default;\n    robj_roptr &operator=(const redisObject *ptr)\n    {\n        m_ptr = ptr;\n        return *this;\n    }\n\n    bool operator==(const robj_roptr &other) const\n    {\n        return m_ptr == other.m_ptr;\n    }\n\n    bool operator!=(const robj_roptr &other) const\n    {\n        return m_ptr != other.m_ptr;\n    }\n\n    const redisObject* operator->() const\n    {\n        return m_ptr;\n    }\n\n    const redisObject& operator*() const\n    {\n        return *m_ptr;\n    }\n\n    bool operator!() const\n    {\n        return !m_ptr;\n    }\n\n    operator bool() const{\n        return !!m_ptr;\n    }\n\n    redisObject *unsafe_robjcast()\n    {\n        return (redisObject*)m_ptr;\n    }\n};\n\nclass unique_sds_ptr\n{\n    sds m_str;\n\npublic:\n    unique_sds_ptr()\n        : m_str(nullptr)\n        {}\n    explicit unique_sds_ptr(sds str)\n        : m_str(str)\n        {}\n    \n    ~unique_sds_ptr()\n    {\n        if (m_str)\n            sdsfree(m_str);\n    }\n\n    unique_sds_ptr(unique_sds_ptr &&other)\n    {\n        m_str = other.m_str;\n        other.m_str = nullptr;\n    }\n\n    bool operator==(const unique_sds_ptr &other) const\n    {\n        return m_str == other.m_str;\n    }\n\n    bool operator!=(const unique_sds_ptr &other) const\n    {\n        return m_str != other.m_str;\n    }\n\n    sds operator->() const\n    {\n        return m_str;\n    }\n\n    bool operator!() const\n    {\n        return !m_str;\n    }\n\n    bool operator<(const unique_sds_ptr &other) const { return m_str < other.m_str; }\n\n    sds get() const { return m_str; }\n};\n\nvoid decrRefCount(robj_roptr o);\nvoid incrRefCount(robj_roptr o);\nclass robj_sharedptr\n{\n    redisObject *m_ptr;\n\npublic:\n    robj_sharedptr()\n    : m_ptr(nullptr)\n    {}\n    explicit robj_sharedptr(redisObject *ptr)\n    : m_ptr(ptr)\n    {\n        if(m_ptr)\n            incrRefCount(ptr);\n    }\n    ~robj_sharedptr()\n    {\n        if (m_ptr)\n            decrRefCount(m_ptr);\n    }\n    robj_sharedptr(const robj_sharedptr& other)\n    : m_ptr(other.m_ptr)\n    {        \n        if(m_ptr)\n            incrRefCount(m_ptr);\n    }\n\n    robj_sharedptr(robj_sharedptr&& other)\n    {\n        m_ptr = other.m_ptr;\n        other.m_ptr = nullptr;\n    }\n\n    robj_sharedptr &operator=(const robj_sharedptr& other)\n    {\n        robj_sharedptr tmp(other);\n        using std::swap;\n        swap(m_ptr, tmp.m_ptr);\n        return *this;\n    }\n    robj_sharedptr &operator=(redisObject *ptr)\n    {\n        robj_sharedptr tmp(ptr);\n        using std::swap;\n        swap(m_ptr, tmp.m_ptr);\n        return *this;\n    }\n    \n    redisObject* operator->() const\n    {\n        return m_ptr;\n    }\n\n    bool operator!() const\n    {\n        return !m_ptr;\n    }\n\n    explicit operator bool() const{\n        return !!m_ptr;\n    }\n\n    operator redisObject *()\n    {\n        return (redisObject*)m_ptr;\n    }\n\n    redisObject *get() { return m_ptr; }\n    const redisObject *get() const { return m_ptr; }\n};\n\ninline bool operator==(const robj_sharedptr &lhs, const robj_sharedptr &rhs)\n{\n    return lhs.get() == rhs.get();\n}\n\ninline bool operator!=(const robj_sharedptr &lhs, const robj_sharedptr &rhs)\n{\n    return !(lhs == rhs);\n}\n\ninline bool operator==(const robj_sharedptr &lhs, const void *p)\n{\n    return lhs.get() == p;\n}\n\ninline bool operator==(const void *p, const robj_sharedptr &rhs)\n{\n    return rhs == p;\n}\n\ninline bool operator!=(const robj_sharedptr &lhs, const void *p)\n{\n    return !(lhs == p);\n}\n\ninline bool operator!=(const void *p, const robj_sharedptr &rhs)\n{\n    return !(rhs == p);\n}\n\n/* Error codes */\n#define C_OK                    0\n#define C_ERR                   -1\n\n/* Static server configuration */\n#define CONFIG_DEFAULT_HZ        10             /* Time interrupt calls/sec. */\n#define CONFIG_MIN_HZ            1\n#define CONFIG_MAX_HZ            500\n#define MAX_CLIENTS_PER_CLOCK_TICK 200          /* HZ is adapted based on that. */\n#define CONFIG_MAX_LINE    1024\n#define CRON_DBS_PER_CALL 16\n#define NET_MAX_WRITES_PER_EVENT (1024*64)\n#define PROTO_SHARED_SELECT_CMDS 10\n#define OBJ_SHARED_INTEGERS 10000\n#define OBJ_SHARED_BULKHDR_LEN 32\n#define LOG_MAX_LEN    1024 /* Default maximum length of syslog messages.*/\n#define AOF_REWRITE_ITEMS_PER_CMD 64\n#define AOF_READ_DIFF_INTERVAL_BYTES (1024*10)\n#define CONFIG_AUTHPASS_MAX_LEN 512\n#define CONFIG_RUN_ID_SIZE 40\n#define RDB_EOF_MARK_SIZE 40\n#define CONFIG_REPL_BACKLOG_MIN_SIZE (1024*16)          /* 16k */\n#define CONFIG_BGSAVE_RETRY_DELAY 5 /* Wait a few secs before trying again. */\n#define CONFIG_DEFAULT_PID_FILE \"/var/run/keydb.pid\"\n#define CONFIG_DEFAULT_CLUSTER_CONFIG_FILE \"nodes.conf\"\n#define CONFIG_DEFAULT_UNIX_SOCKET_PERM 0\n#define CONFIG_DEFAULT_LOGFILE \"\"\n#define NET_HOST_STR_LEN 256 /* Longest valid hostname */\n#define NET_IP_STR_LEN 46 /* INET6_ADDRSTRLEN is 46, but we need to be sure */\n#define NET_ADDR_STR_LEN (NET_IP_STR_LEN+32) /* Must be enough for ip:port */\n#define NET_HOST_PORT_STR_LEN (NET_HOST_STR_LEN+32) /* Must be enough for hostname:port */\n#define CONFIG_BINDADDR_MAX 16\n#define CONFIG_MIN_RESERVED_FDS 32\n#define CONFIG_DEFAULT_THREADS 1\n#define CONFIG_DEFAULT_THREAD_AFFINITY 0\n#define CONFIG_DEFAULT_PROC_TITLE_TEMPLATE \"{title} {listen-addr} {server-mode}\"\n\n#define CONFIG_DEFAULT_ACTIVE_REPLICA 0\n#define CONFIG_DEFAULT_ENABLE_MULTIMASTER 0\n\n#define ACTIVE_EXPIRE_CYCLE_LOOKUPS_PER_LOOP 64 /* Loopkups per loop. */\n#define ACTIVE_EXPIRE_CYCLE_SUBKEY_LOOKUPS_PER_LOOP 16384 /* Subkey loopkups per loop. */\n#define ACTIVE_EXPIRE_CYCLE_FAST_DURATION 1000 /* Microseconds */\n#define ACTIVE_EXPIRE_CYCLE_SLOW_TIME_PERC 25 /* CPU max % for keys collection */\n#define ACTIVE_EXPIRE_CYCLE_SLOW 0\n#define ACTIVE_EXPIRE_CYCLE_FAST 1\n\n/* Children process will exit with this status code to signal that the\n * process terminated without an error: this is useful in order to kill\n * a saving child (RDB or AOF one), without triggering in the parent the\n * write protection that is normally turned on on write errors.\n * Usually children that are terminated with SIGUSR1 will exit with this\n * special code. */\n#define SERVER_CHILD_NOERROR_RETVAL    255\n\n/* Reading copy-on-write info is sometimes expensive and may slow down child\n * processes that report it continuously. We measure the cost of obtaining it\n * and hold back additional reading based on this factor. */\n#define CHILD_COW_DUTY_CYCLE           100\n\n/* Instantaneous metrics tracking. */\n#define STATS_METRIC_SAMPLES 16     /* Number of samples per metric. */\n#define STATS_METRIC_COMMAND 0      /* Number of commands executed. */\n#define STATS_METRIC_NET_INPUT 1    /* Bytes read to network .*/\n#define STATS_METRIC_NET_OUTPUT 2   /* Bytes written to network. */\n#define STATS_METRIC_COUNT 3\n\n/* Protocol and I/O related defines */\n#define PROTO_IOBUF_LEN         (1024*16)  /* Generic I/O buffer size */\n#define PROTO_REPLY_CHUNK_BYTES (16*1024) /* 16k output buffer */\n#define PROTO_ASYNC_REPLY_CHUNK_BYTES (1024)\n#define PROTO_INLINE_MAX_SIZE   (1024*64) /* Max size of inline reads */\n#define PROTO_MBULK_BIG_ARG     (1024*32)\n#define LONG_STR_SIZE      21          /* Bytes needed for long -> str + '\\0' */\n#define REDIS_AUTOSYNC_BYTES (1024*1024*32) /* fdatasync every 32MB */\n\n#define LIMIT_PENDING_QUERYBUF (4*1024*1024) /* 4mb */\n\n/* When configuring the server eventloop, we setup it so that the total number\n * of file descriptors we can handle are g_pserver->maxclients + RESERVED_FDS +\n * a few more to stay safe. Since RESERVED_FDS defaults to 32, we add 96\n * in order to make sure of not over provisioning more than 128 fds. */\n#define CONFIG_FDSET_INCR (CONFIG_MIN_RESERVED_FDS+96)\n\n/* OOM Score Adjustment classes. */\n#define CONFIG_OOM_MASTER 0\n#define CONFIG_OOM_REPLICA 1\n#define CONFIG_OOM_BGCHILD 2\n#define CONFIG_OOM_COUNT 3\n\nextern int configOOMScoreAdjValuesDefaults[CONFIG_OOM_COUNT];\n\n/* Hash table parameters */\n#define HASHTABLE_MIN_FILL        10      /* Minimal hash table fill 10% */\n#define HASHTABLE_MAX_LOAD_FACTOR 1.618   /* Maximum hash table load factor. */\n\n/* Command flags. Please check the command table defined in the server.cpp file\n * for more information about the meaning of every flag. */\n#define CMD_WRITE (1ULL<<0)            /* \"write\" flag */\n#define CMD_READONLY (1ULL<<1)         /* \"read-only\" flag */\n#define CMD_DENYOOM (1ULL<<2)          /* \"use-memory\" flag */\n#define CMD_MODULE (1ULL<<3)           /* Command exported by module. */\n#define CMD_ADMIN (1ULL<<4)            /* \"admin\" flag */\n#define CMD_PUBSUB (1ULL<<5)           /* \"pub-sub\" flag */\n#define CMD_NOSCRIPT (1ULL<<6)         /* \"no-script\" flag */\n#define CMD_RANDOM (1ULL<<7)           /* \"random\" flag */\n#define CMD_SORT_FOR_SCRIPT (1ULL<<8)  /* \"to-sort\" flag */\n#define CMD_LOADING (1ULL<<9)          /* \"ok-loading\" flag */\n#define CMD_STALE (1ULL<<10)           /* \"ok-stale\" flag */\n#define CMD_SKIP_MONITOR (1ULL<<11)    /* \"no-monitor\" flag */\n#define CMD_SKIP_SLOWLOG (1ULL<<12)    /* \"no-slowlog\" flag */\n#define CMD_ASKING (1ULL<<13)          /* \"cluster-asking\" flag */\n#define CMD_FAST (1ULL<<14)            /* \"fast\" flag */\n#define CMD_NO_AUTH (1ULL<<15)         /* \"no-auth\" flag */\n#define CMD_MAY_REPLICATE (1ULL<<16)   /* \"may-replicate\" flag */\n\n/* Command flags used by the module system. */\n#define CMD_MODULE_GETKEYS (1ULL<<17)  /* Use the modules getkeys interface. */\n#define CMD_MODULE_NO_CLUSTER (1ULL<<18) /* Deny on Redis Cluster. */\n\n/* Command flags that describe ACLs categories. */\n#define CMD_CATEGORY_KEYSPACE (1ULL<<18)\n#define CMD_CATEGORY_READ (1ULL<<19)\n#define CMD_CATEGORY_WRITE (1ULL<<20)\n#define CMD_CATEGORY_SET (1ULL<<21)\n#define CMD_CATEGORY_SORTEDSET (1ULL<<22)\n#define CMD_CATEGORY_LIST (1ULL<<23)\n#define CMD_CATEGORY_HASH (1ULL<<24)\n#define CMD_CATEGORY_STRING (1ULL<<25)\n#define CMD_CATEGORY_BITMAP (1ULL<<26)\n#define CMD_CATEGORY_HYPERLOGLOG (1ULL<<27)\n#define CMD_CATEGORY_GEO (1ULL<<28)\n#define CMD_CATEGORY_STREAM (1ULL<<29)\n#define CMD_CATEGORY_PUBSUB (1ULL<<30)\n#define CMD_CATEGORY_ADMIN (1ULL<<31)\n#define CMD_CATEGORY_FAST (1ULL<<32)\n#define CMD_CATEGORY_SLOW (1ULL<<33)\n#define CMD_CATEGORY_BLOCKING (1ULL<<34)\n#define CMD_CATEGORY_DANGEROUS (1ULL<<35)\n#define CMD_CATEGORY_CONNECTION (1ULL<<36)\n#define CMD_CATEGORY_TRANSACTION (1ULL<<37)\n#define CMD_CATEGORY_SCRIPTING (1ULL<<38)\n#define CMD_CATEGORY_REPLICATION (1ULL<<39)\n#define CMD_SKIP_PROPOGATE (1ULL<<40)  /* \"noprop\" flag */\n#define CMD_ASYNC_OK (1ULL<<41) /* This command is safe without a lock */\n\n/* AOF states */\n#define AOF_OFF 0             /* AOF is off */\n#define AOF_ON 1              /* AOF is on */\n#define AOF_WAIT_REWRITE 2    /* AOF waits rewrite to start appending */\n\n/* Client flags */\n#define CLIENT_SLAVE (1<<0)   /* This client is a replica */\n#define CLIENT_MASTER (1<<1)  /* This client is a master */\n#define CLIENT_MONITOR (1<<2) /* This client is a replica monitor, see MONITOR */\n#define CLIENT_MULTI (1<<3)   /* This client is in a MULTI context */\n#define CLIENT_BLOCKED (1<<4) /* The client is waiting in a blocking operation */\n#define CLIENT_DIRTY_CAS (1<<5) /* Watched keys modified. EXEC will fail. */\n#define CLIENT_CLOSE_AFTER_REPLY (1<<6) /* Close after writing entire reply. */\n#define CLIENT_UNBLOCKED (1<<7) /* This client was unblocked and is stored in\n                                  g_pserver->unblocked_clients */\n#define CLIENT_LUA (1<<8) /* This is a non connected client used by Lua */\n#define CLIENT_ASKING (1<<9)     /* Client issued the ASKING command */\n#define CLIENT_CLOSE_ASAP (1<<10)/* Close this client ASAP */\n#define CLIENT_UNIX_SOCKET (1<<11) /* Client connected via Unix domain socket */\n#define CLIENT_DIRTY_EXEC (1<<12)  /* EXEC will fail for errors while queueing */\n#define CLIENT_MASTER_FORCE_REPLY (1<<13)  /* Queue replies even if is master */\n#define CLIENT_FORCE_AOF (1<<14)   /* Force AOF propagation of current cmd. */\n#define CLIENT_FORCE_REPL (1<<15)  /* Force replication of current cmd. */\n#define CLIENT_PRE_PSYNC (1<<16)   /* Instance don't understand PSYNC. */\n#define CLIENT_READONLY (1<<17)    /* Cluster client is in read-only state. */\n#define CLIENT_PUBSUB (1<<18)      /* Client is in Pub/Sub mode. */\n#define CLIENT_PREVENT_AOF_PROP (1<<19)  /* Don't propagate to AOF. */\n#define CLIENT_PREVENT_REPL_PROP (1<<20)  /* Don't propagate to slaves. */\n#define CLIENT_PREVENT_PROP (CLIENT_PREVENT_AOF_PROP|CLIENT_PREVENT_REPL_PROP)\n#define CLIENT_IGNORE_SOFT_SHUTDOWN (CLIENT_MASTER | CLIENT_SLAVE | CLIENT_BLOCKED | CLIENT_MONITOR)\n#define CLIENT_PENDING_WRITE (1<<21) /* Client has output to send but a write\n                                        handler is yet not installed. */\n#define CLIENT_REPLY_OFF (1<<22)   /* Don't send replies to client. */\n#define CLIENT_REPLY_SKIP_NEXT (1<<23)  /* Set CLIENT_REPLY_SKIP for next cmd */\n#define CLIENT_REPLY_SKIP (1<<24)  /* Don't send just this reply. */\n#define CLIENT_LUA_DEBUG (1<<25)  /* Run EVAL in debug mode. */\n#define CLIENT_LUA_DEBUG_SYNC (1<<26)  /* EVAL debugging without fork() */\n#define CLIENT_MODULE (1<<27) /* Non connected client used by some module. */\n#define CLIENT_PROTECTED (1<<28) /* Client should not be freed for now. */\n#define CLIENT_PENDING_COMMAND (1<<29) /* Indicates the client has a fully\n                                        * parsed command ready for execution. */\n#define CLIENT_EXECUTING_COMMAND (1<<30) /* Used to handle reentrency cases in processCommandWhileBlocked \n                                            to ensure we don't process a client already executing */\n#define CLIENT_TRACKING (1ULL<<31) /* Client enabled keys tracking in order to\n                                   perform client side caching. */\n#define CLIENT_TRACKING_BROKEN_REDIR (1ULL<<32) /* Target client is invalid. */\n#define CLIENT_TRACKING_BCAST (1ULL<<33) /* Tracking in BCAST mode. */\n#define CLIENT_TRACKING_OPTIN (1ULL<<34)  /* Tracking in opt-in mode. */\n#define CLIENT_TRACKING_OPTOUT (1ULL<<35) /* Tracking in opt-out mode. */\n#define CLIENT_TRACKING_CACHING (1ULL<<36) /* CACHING yes/no was given,\n                                              depending on optin/optout mode. */\n#define CLIENT_TRACKING_NOLOOP (1ULL<<37) /* Don't send invalidation messages\n                                             about writes performed by myself.*/\n#define CLIENT_IN_TO_TABLE (1ULL<<38) /* This client is in the timeout table. */\n#define CLIENT_PROTOCOL_ERROR (1ULL<<39) /* Protocol error chatting with it. */\n#define CLIENT_CLOSE_AFTER_COMMAND (1ULL<<40) /* Close after executing commands\n                                               * and writing entire reply. */\n#define CLIENT_DENY_BLOCKING (1ULL<<41) /* Indicate that the client should not be blocked.\n                                           currently, turned on inside MULTI, Lua, RM_Call,\n                                           and AOF client */\n#define CLIENT_REPL_RDBONLY (1ULL<<42) /* This client is a replica that only wants\n                                          RDB without replication buffer. */\n#define CLIENT_FORCE_REPLY (1ULL<<44) /* Should addReply be forced to write the text? */\n#define CLIENT_AUDIT_LOGGING (1ULL<<45) /* Client commands required audit logging */\n\n/* Client block type (btype field in client structure)\n * if CLIENT_BLOCKED flag is set. */\n#define BLOCKED_NONE 0    /* Not blocked, no CLIENT_BLOCKED flag set. */\n#define BLOCKED_LIST 1    /* BLPOP & co. */\n#define BLOCKED_WAIT 2    /* WAIT for synchronous replication. */\n#define BLOCKED_MODULE 3  /* Blocked by a loadable module. */\n#define BLOCKED_STREAM 4  /* XREAD. */\n#define BLOCKED_ZSET 5    /* BZPOP et al. */\n#define BLOCKED_PAUSE 6   /* Blocked by CLIENT PAUSE */\n#define BLOCKED_ASYNC 7\n#define BLOCKED_NUM 8     /* Number of blocked states. */\n\n/* Client request types */\n#define PROTO_REQ_INLINE 1\n#define PROTO_REQ_MULTIBULK 2\n\n/* Client classes for client limits, currently used only for\n * the max-client-output-buffer limit implementation. */\n#define CLIENT_TYPE_NORMAL 0 /* Normal req-reply clients + MONITORs */\n#define CLIENT_TYPE_SLAVE 1  /* Slaves. */\n#define CLIENT_TYPE_PUBSUB 2 /* Clients subscribed to PubSub channels. */\n#define CLIENT_TYPE_MASTER 3 /* Master. */\n#define CLIENT_TYPE_COUNT 4  /* Total number of client types. */\n#define CLIENT_TYPE_OBUF_COUNT 3 /* Number of clients to expose to output\n                                    buffer configuration. Just the first\n                                    three: normal, replica, pubsub. */\n\n/* Slave replication state. Used in g_pserver->repl_state for slaves to remember\n * what to do next. */\ntypedef enum {\n    REPL_STATE_NONE = 0,            /* No active replication */\n    REPL_STATE_CONNECT,             /* Must connect to master */\n    REPL_STATE_CONNECTING,          /* Connecting to master */\n    REPL_STATE_RETRY_NOREPLPING,    /* Master does not support REPLPING, retry with PING */\n    /* --- Handshake states, must be ordered --- */\n    REPL_STATE_RECEIVE_PING_REPLY,  /* Wait for PING reply */\n    REPL_STATE_SEND_HANDSHAKE,      /* Send handshake sequance to master */\n    REPL_STATE_RECEIVE_AUTH_REPLY,  /* Wait for AUTH reply */\n    REPL_STATE_RECEIVE_PORT_REPLY,  /* Wait for REPLCONF reply */\n    REPL_STATE_RECEIVE_IP_REPLY,    /* Wait for REPLCONF reply */\n    REPL_STATE_RECEIVE_CAPA_REPLY,  /* Wait for REPLCONF reply */\n    REPL_STATE_RECEIVE_UUID,        /* they should ack with their UUID */\n    REPL_STATE_SEND_PSYNC,          /* Send PSYNC */\n    REPL_STATE_RECEIVE_PSYNC_REPLY, /* Wait for PSYNC reply */\n    /* --- End of handshake states --- */\n    REPL_STATE_TRANSFER,        /* Receiving .rdb from master */\n    REPL_STATE_CONNECTED,       /* Connected to master */\n} repl_state;\n\n/* The state of an in progress coordinated failover */\ntypedef enum {\n    NO_FAILOVER = 0,        /* No failover in progress */\n    FAILOVER_WAIT_FOR_SYNC, /* Waiting for target replica to catch up */\n    FAILOVER_IN_PROGRESS    /* Waiting for target replica to accept\n                             * PSYNC FAILOVER request. */\n} failover_state;\n\n/* State of slaves from the POV of the master. Used in client->replstate.\n * In SEND_BULK and ONLINE state the replica receives new updates\n * in its output queue. In the WAIT_BGSAVE states instead the server is waiting\n * to start the next background saving in order to send updates to it. */\n#define SLAVE_STATE_WAIT_BGSAVE_START 6 /* We need to produce a new RDB file. */\n#define SLAVE_STATE_WAIT_BGSAVE_END 7 /* Waiting RDB file creation to finish. */\n#define SLAVE_STATE_SEND_BULK 8 /* Sending RDB file to replica. */\n#define SLAVE_STATE_ONLINE 9 /* RDB file transmitted, sending just updates. */\n#define SLAVE_STATE_FASTSYNC_TX 10\n#define SLAVE_STATE_FASTSYNC_DONE 11\n\n/* Slave capabilities. */\n#define SLAVE_CAPA_NONE 0\n#define SLAVE_CAPA_EOF (1<<0)    /* Can parse the RDB EOF streaming format. */\n#define SLAVE_CAPA_PSYNC2 (1<<1) /* Supports PSYNC2 protocol. */\n#define SLAVE_CAPA_ACTIVE_EXPIRE (1<<2) /* Will the slave perform its own expirations? (Don't send delete) */\n#define SLAVE_CAPA_KEYDB_FASTSYNC (1<<3)\n\n/* Synchronous read timeout - replica side */\n#define CONFIG_REPL_SYNCIO_TIMEOUT 5\n\n/* List related stuff */\n#define LIST_HEAD 0\n#define LIST_TAIL 1\n#define ZSET_MIN 0\n#define ZSET_MAX 1\n\n/* Sort operations */\n#define SORT_OP_GET 0\n\n/* Log levels */\n#define LL_DEBUG 0\n#define LL_VERBOSE 1\n#define LL_NOTICE 2\n#define LL_WARNING 3\n#define LL_RAW (1<<10) /* Modifier to log without timestamp */\n\n/* Error severity levels */\n#define ERR_CRITICAL 0\n#define ERR_ERROR 1\n#define ERR_WARNING 2\n#define ERR_NOTICE 3\n\n/* Supervision options */\n#define SUPERVISED_NONE 0\n#define SUPERVISED_AUTODETECT 1\n#define SUPERVISED_SYSTEMD 2\n#define SUPERVISED_UPSTART 3\n\n/* Anti-warning macro... */\n#define UNUSED(V) ((void) V)\n\n#define ZSKIPLIST_MAXLEVEL 32 /* Should be enough for 2^64 elements */\n#define ZSKIPLIST_P 0.25      /* Skiplist P = 1/4 */\n\n/* Append only defines */\n#define AOF_FSYNC_NO 0\n#define AOF_FSYNC_ALWAYS 1\n#define AOF_FSYNC_EVERYSEC 2\n\n/* Replication diskless load defines */\n#define REPL_DISKLESS_LOAD_DISABLED 0\n#define REPL_DISKLESS_LOAD_WHEN_DB_EMPTY 1\n#define REPL_DISKLESS_LOAD_SWAPDB 2\n\n/* Storage Memory Model Defines */\n#define STORAGE_WRITEBACK 0\n#define STORAGE_WRITETHROUGH 1\n\n/* TLS Client Authentication */\n#define TLS_CLIENT_AUTH_NO 0\n#define TLS_CLIENT_AUTH_YES 1\n#define TLS_CLIENT_AUTH_OPTIONAL 2\n\n/* Sanitize dump payload */\n#define SANITIZE_DUMP_NO 0\n#define SANITIZE_DUMP_YES 1\n#define SANITIZE_DUMP_CLIENTS 2\n\n/* Sets operations codes */\n#define SET_OP_UNION 0\n#define SET_OP_DIFF 1\n#define SET_OP_INTER 2\n\n/* oom-score-adj defines */\n#define OOM_SCORE_ADJ_NO 0\n#define OOM_SCORE_RELATIVE 1\n#define OOM_SCORE_ADJ_ABSOLUTE 2\n\n/* Redis maxmemory strategies. Instead of using just incremental number\n * for this defines, we use a set of flags so that testing for certain\n * properties common to multiple policies is faster. */\n#define MAXMEMORY_FLAG_LRU (1<<0)\n#define MAXMEMORY_FLAG_LFU (1<<1)\n#define MAXMEMORY_FLAG_ALLKEYS (1<<2)\n#define MAXMEMORY_FLAG_NO_SHARED_INTEGERS \\\n    (MAXMEMORY_FLAG_LRU|MAXMEMORY_FLAG_LFU)\n\n#define MAXMEMORY_VOLATILE_LRU ((0<<8)|MAXMEMORY_FLAG_LRU)\n#define MAXMEMORY_VOLATILE_LFU ((1<<8)|MAXMEMORY_FLAG_LFU)\n#define MAXMEMORY_VOLATILE_TTL (2<<8)\n#define MAXMEMORY_VOLATILE_RANDOM (3<<8)\n#define MAXMEMORY_ALLKEYS_LRU ((4<<8)|MAXMEMORY_FLAG_LRU|MAXMEMORY_FLAG_ALLKEYS)\n#define MAXMEMORY_ALLKEYS_LFU ((5<<8)|MAXMEMORY_FLAG_LFU|MAXMEMORY_FLAG_ALLKEYS)\n#define MAXMEMORY_ALLKEYS_RANDOM ((6<<8)|MAXMEMORY_FLAG_ALLKEYS)\n#define MAXMEMORY_NO_EVICTION (7<<8)\n\n/* Units */\n#define UNIT_SECONDS 0\n#define UNIT_MILLISECONDS 1\n\n/* SHUTDOWN flags */\n#define SHUTDOWN_NOFLAGS 0      /* No flags. */\n#define SHUTDOWN_SAVE 1         /* Force SAVE on SHUTDOWN even if no save\n                                   points are configured. */\n#define SHUTDOWN_NOSAVE 2       /* Don't SAVE on SHUTDOWN. */\n\n/* Command call flags, see call() function */\n#define CMD_CALL_NONE 0\n#define CMD_CALL_SLOWLOG (1<<0)\n#define CMD_CALL_STATS (1<<1)\n#define CMD_CALL_PROPAGATE_AOF (1<<2)\n#define CMD_CALL_PROPAGATE_REPL (1<<3)\n#define CMD_CALL_PROPAGATE (CMD_CALL_PROPAGATE_AOF|CMD_CALL_PROPAGATE_REPL)\n#define CMD_CALL_FULL (CMD_CALL_SLOWLOG | CMD_CALL_STATS | CMD_CALL_PROPAGATE | CMD_CALL_NOWRAP)\n#define CMD_CALL_NOWRAP (1<<4)  /* Don't wrap also propagate array into\n                                   MULTI/EXEC: the caller will handle it.  */\n#define CMD_CALL_ASYNC (1<<5)\n\n/* Command propagation flags, see propagate() function */\n#define PROPAGATE_NONE 0\n#define PROPAGATE_AOF 1\n#define PROPAGATE_REPL 2\n\n/* Client pause types, larger types are more restrictive\n * pause types than smaller pause types. */\ntypedef enum {\n    CLIENT_PAUSE_OFF = 0, /* Pause no commands */\n    CLIENT_PAUSE_WRITE,   /* Pause write commands */\n    CLIENT_PAUSE_ALL      /* Pause all commands */\n} pause_type;\n\n/* RDB active child save type. */\n#define RDB_CHILD_TYPE_NONE 0\n#define RDB_CHILD_TYPE_DISK 1     /* RDB is written to disk. */\n#define RDB_CHILD_TYPE_SOCKET 2   /* RDB is written to replica socket. */\n\n/* Keyspace changes notification classes. Every class is associated with a\n * character for configuration purposes. */\n#define NOTIFY_KEYSPACE (1<<0)    /* K */\n#define NOTIFY_KEYEVENT (1<<1)    /* E */\n#define NOTIFY_GENERIC (1<<2)     /* g */\n#define NOTIFY_STRING (1<<3)      /* $ */\n#define NOTIFY_LIST (1<<4)        /* l */\n#define NOTIFY_SET (1<<5)         /* s */\n#define NOTIFY_HASH (1<<6)        /* h */\n#define NOTIFY_ZSET (1<<7)        /* z */\n#define NOTIFY_EXPIRED (1<<8)     /* x */\n#define NOTIFY_EVICTED (1<<9)     /* e */\n#define NOTIFY_STREAM (1<<10)     /* t */\n#define NOTIFY_KEY_MISS (1<<11)   /* m (Note: This one is excluded from NOTIFY_ALL on purpose) */\n#define NOTIFY_LOADED (1<<12)     /* module only key space notification, indicate a key loaded from rdb */\n#define NOTIFY_MODULE (1<<13)     /* d, module key space notification */\n#define NOTIFY_ALL (NOTIFY_GENERIC | NOTIFY_STRING | NOTIFY_LIST | NOTIFY_SET | NOTIFY_HASH | NOTIFY_ZSET | NOTIFY_EXPIRED | NOTIFY_EVICTED | NOTIFY_STREAM | NOTIFY_MODULE) /* A flag */\n\n/* Get the first bind addr or NULL */\n#define NET_FIRST_BIND_ADDR (g_pserver->bindaddr_count ? g_pserver->bindaddr[0] : NULL)\n\n/* Using the following macro you can run code inside serverCron() with the\n * specified period, specified in milliseconds.\n * The actual resolution depends on g_pserver->hz. */\n#define run_with_period(_ms_) if ((_ms_ <= 1000/g_pserver->hz) || !(g_pserver->cronloops%((_ms_)/(1000/g_pserver->hz))))\n\n/*-----------------------------------------------------------------------------\n * Data types\n *----------------------------------------------------------------------------*/\n\n/* A redis object, that is a type able to hold a string / list / set */\n\n/* The actual Redis Object */\n#define OBJ_STRING 0     /* String object. */\n#define OBJ_LIST 1       /* List object. */\n#define OBJ_SET 2        /* Set object. */\n#define OBJ_ZSET 3       /* Sorted set object. */\n#define OBJ_HASH 4       /* Hash object. */\n\n/* The \"module\" object type is a special one that signals that the object\n * is one directly managed by a Redis module. In this case the value points\n * to a moduleValue struct, which contains the object value (which is only\n * handled by the module itself) and the RedisModuleType struct which lists\n * function pointers in order to serialize, deserialize, AOF-rewrite and\n * free the object.\n *\n * Inside the RDB file, module types are encoded as OBJ_MODULE followed\n * by a 64 bit module type ID, which has a 54 bits module-specific signature\n * in order to dispatch the loading to the right module, plus a 10 bits\n * encoding version. */\n#define OBJ_MODULE 5     /* Module object. */\n#define OBJ_STREAM 6     /* Stream object. */\n#define OBJ_CRON 7       /* CRON job */\n#define OBJ_NESTEDHASH 8 /* Nested Hash Object */\n\n/* Extract encver / signature from a module type ID. */\n#define REDISMODULE_TYPE_ENCVER_BITS 10\n#define REDISMODULE_TYPE_ENCVER_MASK ((1<<REDISMODULE_TYPE_ENCVER_BITS)-1)\n#define REDISMODULE_TYPE_ENCVER(id) (id & REDISMODULE_TYPE_ENCVER_MASK)\n#define REDISMODULE_TYPE_SIGN(id) ((id & ~((uint64_t)REDISMODULE_TYPE_ENCVER_MASK)) >>REDISMODULE_TYPE_ENCVER_BITS)\n\n/* Bit flags for moduleTypeAuxSaveFunc */\n#define REDISMODULE_AUX_BEFORE_RDB (1<<0)\n#define REDISMODULE_AUX_AFTER_RDB (1<<1)\n\n/* Number of cycles before time thread gives up fork lock */\n#define MAX_CYCLES_TO_HOLD_FORK_LOCK 10\n\nstruct RedisModule;\nstruct RedisModuleIO;\nstruct RedisModuleDigest;\nstruct RedisModuleCtx;\nstruct redisObject;\nstruct RedisModuleDefragCtx;\n\n/* Each module type implementation should export a set of methods in order\n * to serialize and deserialize the value in the RDB file, rewrite the AOF\n * log, create the digest for \"DEBUG DIGEST\", and free the value when a key\n * is deleted. */\ntypedef void *(*moduleTypeLoadFunc)(struct RedisModuleIO *io, int encver);\ntypedef void (*moduleTypeSaveFunc)(struct RedisModuleIO *io, void *value);\ntypedef int (*moduleTypeAuxLoadFunc)(struct RedisModuleIO *rdb, int encver, int when);\ntypedef void (*moduleTypeAuxSaveFunc)(struct RedisModuleIO *rdb, int when);\ntypedef void (*moduleTypeRewriteFunc)(struct RedisModuleIO *io, struct redisObject *key, void *value);\ntypedef void (*moduleTypeDigestFunc)(struct RedisModuleDigest *digest, void *value);\ntypedef size_t (*moduleTypeMemUsageFunc)(const void *value);\ntypedef void (*moduleTypeFreeFunc)(void *value);\ntypedef size_t (*moduleTypeFreeEffortFunc)(struct redisObject *key, const void *value);\ntypedef void (*moduleTypeUnlinkFunc)(struct redisObject *key, void *value);\ntypedef void *(*moduleTypeCopyFunc)(struct redisObject *fromkey, struct redisObject *tokey, const void *value);\ntypedef int (*moduleTypeDefragFunc)(struct RedisModuleDefragCtx *ctx, struct redisObject *key, void **value);\n\n/* This callback type is called by moduleNotifyUserChanged() every time\n * a user authenticated via the module API is associated with a different\n * user or gets disconnected. This needs to be exposed since you can't cast\n * a function pointer to (void *). */\ntypedef void (*RedisModuleUserChangedFunc) (uint64_t client_id, void *privdata);\n\n\n/* The module type, which is referenced in each value of a given type, defines\n * the methods and links to the module exporting the type. */\ntypedef struct RedisModuleType {\n    uint64_t id; /* Higher 54 bits of type ID + 10 lower bits of encoding ver. */\n    struct RedisModule *module;\n    moduleTypeLoadFunc rdb_load;\n    moduleTypeSaveFunc rdb_save;\n    moduleTypeRewriteFunc aof_rewrite;\n    moduleTypeMemUsageFunc mem_usage;\n    moduleTypeDigestFunc digest;\n    moduleTypeFreeFunc free;\n    moduleTypeFreeEffortFunc free_effort;\n    moduleTypeUnlinkFunc unlink;\n    moduleTypeCopyFunc copy;\n    moduleTypeDefragFunc defrag;\n    moduleTypeAuxLoadFunc aux_load;\n    moduleTypeAuxSaveFunc aux_save;\n    int aux_save_triggers;\n    char name[10]; /* 9 bytes name + null term. Charset: A-Z a-z 0-9 _- */\n} moduleType;\n\n/* In Redis objects 'robj' structures of type OBJ_MODULE, the value pointer\n * is set to the following structure, referencing the moduleType structure\n * in order to work with the value, and at the same time providing a raw\n * pointer to the value, as created by the module commands operating with\n * the module type.\n *\n * So for example in order to free such a value, it is possible to use\n * the following code:\n *\n *  if (robj->type == OBJ_MODULE) {\n *      moduleValue *mt = robj->ptr;\n *      mt->type->free(mt->value);\n *      zfree(mt); // We need to release this in-the-middle struct as well.\n *  }\n */\ntypedef struct moduleValue {\n    moduleType *type;\n    void *value;\n} moduleValue;\n\n/* This is a wrapper for the 'rio' streams used inside rdb.c in Redis, so that\n * the user does not have to take the total count of the written bytes nor\n * to care about error conditions. */\ntypedef struct RedisModuleIO {\n    size_t bytes;       /* Bytes read / written so far. */\n    rio *prio;           /* Rio stream. */\n    moduleType *type;   /* Module type doing the operation. */\n    int error;          /* True if error condition happened. */\n    int ver;            /* Module serialization version: 1 (old),\n                         * 2 (current version with opcodes annotation). */\n    struct RedisModuleCtx *ctx; /* Optional context, see RM_GetContextFromIO()*/\n    struct redisObject *key;    /* Optional name of key processed */\n} RedisModuleIO;\n\n/* Macro to initialize an IO context. Note that the 'ver' field is populated\n * inside rdb.c according to the version of the value to load. */\n#define moduleInitIOContext(iovar,mtype,rioptr,keyptr) do { \\\n    iovar.prio = rioptr; \\\n    iovar.type = mtype; \\\n    iovar.bytes = 0; \\\n    iovar.error = 0; \\\n    iovar.ver = 0; \\\n    iovar.key = keyptr; \\\n    iovar.ctx = NULL; \\\n} while(0)\n\n/* This is a structure used to export DEBUG DIGEST capabilities to Redis\n * modules. We want to capture both the ordered and unordered elements of\n * a data structure, so that a digest can be created in a way that correctly\n * reflects the values. See the DEBUG DIGEST command implementation for more\n * background. */\ntypedef struct RedisModuleDigest {\n    unsigned char o[20];    /* Ordered elements. */\n    unsigned char x[20];    /* Xored elements. */\n} RedisModuleDigest;\n\n/* Just start with a digest composed of all zero bytes. */\n#define moduleInitDigestContext(mdvar) do { \\\n    memset(mdvar.o,0,sizeof(mdvar.o)); \\\n    memset(mdvar.x,0,sizeof(mdvar.x)); \\\n} while(0)\n\n/* Objects encoding. Some kind of objects like Strings and Hashes can be\n * internally represented in multiple ways. The 'encoding' field of the object\n * is set to one of this fields for this object. */\n#define OBJ_ENCODING_RAW 0     /* Raw representation */\n#define OBJ_ENCODING_INT 1     /* Encoded as integer */\n#define OBJ_ENCODING_HT 2      /* Encoded as hash table */\n#define OBJ_ENCODING_ZIPMAP 3  /* Encoded as zipmap */\n#define OBJ_ENCODING_LINKEDLIST 4 /* No longer used: old list encoding. */\n#define OBJ_ENCODING_ZIPLIST 5 /* Encoded as ziplist */\n#define OBJ_ENCODING_INTSET 6  /* Encoded as intset */\n#define OBJ_ENCODING_SKIPLIST 7  /* Encoded as skiplist */\n#define OBJ_ENCODING_EMBSTR 8  /* Embedded sds string encoding */\n#define OBJ_ENCODING_QUICKLIST 9 /* Encoded as linked list of ziplists */\n#define OBJ_ENCODING_STREAM 10 /* Encoded as a radix tree of listpacks */\n\n#define LRU_BITS 24\n#define LRU_CLOCK_MAX ((1<<LRU_BITS)-1) /* Max value of obj->lru */\n#define LRU_CLOCK_RESOLUTION 1000 /* LRU clock resolution in ms */\n\n#define OBJ_SHARED_REFCOUNT (0x7FFFFFFF) \n#define OBJ_STATIC_REFCOUNT (OBJ_SHARED_REFCOUNT-1)\n#define OBJ_FIRST_SPECIAL_REFCOUNT OBJ_STATIC_REFCOUNT\n#define OBJ_MVCC_INVALID (0xFFFFFFFFFFFFFFFFULL)\n\n#define MVCC_MS_SHIFT 20\n\n// This struct will be allocated ahead of the ROBJ when needed\nstruct redisObjectExtended {\n    uint64_t mvcc_tstamp;\n};\n\ntypedef struct redisObject {\n    friend redisObject *createEmbeddedStringObject(const char *ptr, size_t len);\n    friend redisObject *createObject(int type, void *ptr);\nprotected:\n    redisObject() {}\n\npublic:\n    unsigned type:4;\n    unsigned encoding:4;\n    unsigned lru:LRU_BITS; /* LRU time (relative to global lru_clock) or\n                            * LFU data (least significant 8 bits frequency\n                            * and most significant 16 bits access time). */\nprivate:\n    mutable std::atomic<unsigned> refcount {0};\npublic:\n    expireEntry expire;\n    void *m_ptr;\n\n    inline bool FExpires() const { return refcount.load(std::memory_order_relaxed) >> 31; }\n    void SetFExpires(bool fExpires);\n\n    void setrefcount(unsigned ref);\n    unsigned getrefcount(std::memory_order order = std::memory_order_relaxed) const { return (refcount.load(order) & ~(1U << 31)); }\n    void addref() const { refcount.fetch_add(1, std::memory_order_relaxed); }\n    unsigned release() const { return refcount.fetch_sub(1, std::memory_order_seq_cst) & ~(1U << 31); }\n} robj;\nstatic_assert(sizeof(redisObject) <= 24, \"object size is critical, don't increase\");\n\nclass redisObjectStack : public redisObjectExtended, public redisObject\n{\npublic:\n    redisObjectStack();\n};\n\nuint64_t mvccFromObj(robj_roptr o);\nvoid setMvccTstamp(redisObject *o, uint64_t mvcc);\nvoid *allocPtrFromObj(robj_roptr o);\nrobj *objFromAllocPtr(void *pv);\n\n__attribute__((always_inline)) inline const void *ptrFromObj(robj_roptr &o)\n{\n    if (o->encoding == OBJ_ENCODING_EMBSTR)\n        return ((char*)&(o)->m_ptr) + sizeof(struct sdshdr8);\n    return o->m_ptr;\n}\n\n__attribute__((always_inline)) inline void *ptrFromObj(const robj *o)\n{\n    if (o->encoding == OBJ_ENCODING_EMBSTR)\n        return ((char*)&((robj*)o)->m_ptr) + sizeof(struct sdshdr8);\n    return o->m_ptr;\n}\n\n__attribute__((always_inline)) inline const char *szFromObj(robj_roptr o)\n{\n    return (const char*)ptrFromObj(o);\n}\n\n__attribute__((always_inline)) inline char *szFromObj(const robj *o)\n{\n    return (char*)ptrFromObj(o);\n}\n\n/* The a string name for an object's type as listed above\n * Native types are checked against the OBJ_STRING, OBJ_LIST, OBJ_* defines,\n * and Module types have their registered name returned. */\nconst char *getObjectTypeName(robj_roptr o);\n\n/* Macro used to initialize a Redis object allocated on the stack.\n * Note that this macro is taken near the structure definition to make sure\n * we'll update it when the structure is changed, to avoid bugs like\n * bug #85 introduced exactly in this way. */\n#define initStaticStringObject(_var,_ptr) do { \\\n    _var.setrefcount(OBJ_STATIC_REFCOUNT); \\\n    _var.type = OBJ_STRING; \\\n    _var.encoding = OBJ_ENCODING_RAW; \\\n    _var.m_ptr = _ptr; \\\n} while(0)\n\nstruct evictionPoolEntry; /* Defined in evict.c */\n\n/* This structure is used in order to represent the output buffer of a client,\n * which is actually a linked list of blocks like that, that is: client->reply. */\ntypedef struct clientReplyBlock {\n    size_t size, used;\n#ifndef __cplusplus\n    char buf[];\n#else\n    __attribute__((always_inline)) char *buf()\n    {\n        return reinterpret_cast<char*>(this+1);\n    }\n#endif\n} clientReplyBlock;\n\nstruct dictEntry;\nclass dict_const_iter\n{\n    friend struct redisDb;\n    friend class redisDbPersistentData;\nprotected:\n    dictEntry *de;\npublic:\n    explicit dict_const_iter(dictEntry *de)\n        : de(de)\n    {}\n\n    const char *key() const { return de ? (const char*)dictGetKey(de) : nullptr; }\n    robj_roptr val() const { return de ? (robj*)dictGetVal(de) : nullptr; }\n    const robj* operator->() const { return de ? (robj*)dictGetVal(de) : nullptr; }\n    operator robj_roptr() const { return de ? (robj*)dictGetVal(de) : nullptr; }\n\n    bool operator==(std::nullptr_t) const { return de == nullptr; }\n    bool operator!=(std::nullptr_t) const { return de != nullptr; }\n    bool operator==(const dict_const_iter &other) { return de == other.de; }\n};\nclass dict_iter : public dict_const_iter\n{\n    dict *m_dict = nullptr;\npublic:\n    dict_iter()\n        : dict_const_iter(nullptr)\n    {}\n    explicit dict_iter(std::nullptr_t)\n        : dict_const_iter(nullptr)\n    {}\n    explicit dict_iter(dict *d, dictEntry *de)\n        : dict_const_iter(de), m_dict(d)\n    {}\n    sds key() { return de ? (sds)dictGetKey(de) : nullptr; }\n    robj *val() { return de ? (robj*)dictGetVal(de) : nullptr; }\n    robj *operator->() { return de ? (robj*)dictGetVal(de) : nullptr; }\n    operator robj*() const { return de ? (robj*)dictGetVal(de) : nullptr; }\n\n    void setval(robj *val) {\n        dictSetVal(m_dict, de, val);\n    }\n};\n\nclass redisDbPersistentDataSnapshot;\nclass redisDbPersistentData\n{\n    friend void dictDbKeyDestructor(void *privdata, void *key);\n    friend class redisDbPersistentDataSnapshot;\n\npublic:\n    redisDbPersistentData();\n    virtual ~redisDbPersistentData();\n\n    redisDbPersistentData(const redisDbPersistentData &) = delete;\n    redisDbPersistentData(redisDbPersistentData &&) = delete;\n\n    size_t slots() const { return dictSlots(m_pdict); }\n    size_t size(bool fCachedOnly = false) const;\n    void expand(uint64_t slots) {\n        if (m_spstorage)\n            m_spstorage->expand(slots);\n        else\n            dictExpand(m_pdict, slots); \n    }\n    \n    void trackkey(robj_roptr o, bool fUpdate)\n    {\n        trackkey(szFromObj(o), fUpdate);\n    }\n\n    void trackkey(const char *key, bool fUpdate);\n\n    dict_iter find(const char *key) \n    {\n        dictEntry *de = dictFind(m_pdict, key);\n        ensure(key, &de);\n        return dict_iter(m_pdict, de);\n    }\n\n    dict_iter find(robj_roptr key)\n    {\n        return find(szFromObj(key));\n    }\n\n    dict_iter random();\n\n    const expireEntry *random_expire(sds *key)\n    {\n        auto itr = random();\n        if (itr->FExpires()) {\n            *key = itr.key();\n            return &itr->expire;\n        }\n        return nullptr;\n    }\n\n    dict_iter end()  { return dict_iter(nullptr, nullptr); }\n    dict_const_iter end() const { return dict_const_iter(nullptr); }\n\n    void getStats(char *buf, size_t bufsize) { dictGetStats(buf, bufsize, m_pdict); }\n\n    bool insert(char *k, robj *o, bool fAssumeNew = false, dict_iter *existing = nullptr);\n    void tryResize();\n    int incrementallyRehash();\n    void updateValue(dict_iter itr, robj *val);\n    bool syncDelete(robj *key);\n    bool asyncDelete(robj *key);\n    size_t expireSize() const { return m_numexpires; }\n    int removeExpire(robj *key, dict_iter itr);\n    int removeSubkeyExpire(robj *key, robj *subkey);\n    void clear(void(callback)(void*));\n    void emptyDbAsync();\n    // Note: If you do not need the obj then use the objless iterator version.  It's faster\n    bool iterate(std::function<bool(const char*, robj*)> fn);\n    void setExpire(robj *key, robj *subkey, long long when);\n    void setExpire(const char *key, expireEntry &&e);\n    void initialize();\n    void prepOverwriteForSnapshot(char *key);\n\n    bool FRehashing() const { return dictIsRehashing(m_pdict) || dictIsRehashing(m_pdictTombstone); }\n\n    void setStorageProvider(StorageCache *pstorage);\n    void endStorageProvider();\n\n    void trackChanges(bool fBulk, size_t sizeHint = 0);\n    bool FTrackingChanges() const { return !!m_fTrackingChanges; }\n\n    // Process and commit changes for secondary storage.  Note that process and commit are seperated\n    //  to allow you to release the global lock before commiting.  To prevent deadlocks you *must*\n    //  either release the global lock or keep the same global lock between the two functions as\n    //  a second look is kept to ensure writes to secondary storage are ordered\n    bool processChanges(bool fSnapshot);\n    void processChangesAsync(std::atomic<int> &pendingJobs);\n    void commitChanges(const redisDbPersistentDataSnapshot **psnapshotFree = nullptr);\n\n    // This should only be used if you look at the key, we do not fixup\n    //  objects stored elsewhere\n    dict *dictUnsafeKeyOnly() { return m_pdict; }   \n\n    const redisDbPersistentDataSnapshot *createSnapshot(uint64_t mvccCheckpoint, bool fOptional);\n    void endSnapshot(const redisDbPersistentDataSnapshot *psnapshot);\n    void endSnapshotAsync(const redisDbPersistentDataSnapshot *psnapshot);\n    void restoreSnapshot(const redisDbPersistentDataSnapshot *psnapshot);\n\n    bool FStorageProvider() { return m_spstorage != nullptr; }\n    bool removeCachedValue(const char *key, dictEntry **ppde = nullptr);\n    void removeAllCachedValues();\n    void disableKeyCache();\n    bool keycacheIsEnabled();\n\n    void prefetchKeysAsync(client *c, struct parsed_command &command);\n\n    bool FSnapshot() const { return m_spdbSnapshotHOLDER != nullptr; }\n\n    std::unique_ptr<const StorageCache> CloneStorageCache() { return std::unique_ptr<const StorageCache>(m_spstorage->clone()); }\n    std::shared_ptr<StorageCache> getStorageCache() { return m_spstorage; }\n    void bulkDirectStorageInsert(char **rgKeys, size_t *rgcbKeys, char **rgVals, size_t *rgcbVals, size_t celem);\n\n    dict_iter find_cached_threadsafe(const char *key) const;\n\n    static void activeExpireCycleCore(int type);\n\nprotected:\n    uint64_t m_mvccCheckpoint = 0;\n\nprivate:\n    static void serializeAndStoreChange(StorageCache *storage, redisDbPersistentData *db, const char *key, bool fUpdate);\n\n    void ensure(const char *key);\n    void ensure(const char *key, dictEntry **de);\n    void storeDatabase();\n    void storeKey(sds key, robj *o, bool fOverwrite);\n    void recursiveFreeSnapshots(redisDbPersistentDataSnapshot *psnapshot);\n\n    // Keyspace\n    dict *m_pdict = nullptr;                 /* The keyspace for this DB */\n    dict *m_pdictTombstone = nullptr;        /* Track deletes when we have a snapshot */\n    std::atomic<int> m_fTrackingChanges {0};     // Note: Stack based\n    std::atomic<int> m_fAllChanged {0};\n    dict *m_dictChanged = nullptr;\n    size_t m_cnewKeysPending = 0;\n    std::shared_ptr<StorageCache> m_spstorage = nullptr;\n\n    // Expire\n    size_t m_numexpires = 0;\n\n    // These two pointers are the same, UNLESS the database has been cleared.\n    //      in which case m_pdbSnapshot is NULL and we continue as though we weren'\n    //      in a snapshot\n    const redisDbPersistentDataSnapshot *m_pdbSnapshot = nullptr;\n    std::unique_ptr<redisDbPersistentDataSnapshot> m_spdbSnapshotHOLDER;\n    const redisDbPersistentDataSnapshot *m_pdbSnapshotASYNC = nullptr;\n    \n    const redisDbPersistentDataSnapshot *m_pdbSnapshotStorageFlush = nullptr;\n    dict *m_dictChangedStorageFlush = nullptr;\n    \n    int m_refCount = 0;\n};\n\nclass redisDbPersistentDataSnapshot : protected redisDbPersistentData\n{\n    friend class redisDbPersistentData;\nprivate:\n    bool iterate_threadsafe_core(std::function<bool(const char*, robj_roptr o)> &fn, bool fKeyOnly, bool fCacheOnly, bool fTop) const;\n\nprotected:\n    static void gcDisposeSnapshot(redisDbPersistentDataSnapshot *psnapshot);\n    bool freeTombstoneObjects(int depth);\n\npublic:\n    int snapshot_depth() const;\n    bool FWillFreeChildDebug() const { return m_spdbSnapshotHOLDER != nullptr; }\n\n    bool iterate_threadsafe(std::function<bool(const char*, robj_roptr o)> fn, bool fKeyOnly = false, bool fCacheOnly = false) const;\n    unsigned long scan_threadsafe(unsigned long iterator, long count, sds type, list *keys) const;\n    \n    using redisDbPersistentData::createSnapshot;\n    using redisDbPersistentData::endSnapshot;\n    using redisDbPersistentData::endSnapshotAsync;\n    using redisDbPersistentData::end;\n    using redisDbPersistentData::find_cached_threadsafe;\n    using redisDbPersistentData::FSnapshot;\n\n    dict_iter random_cache_threadsafe(bool fPrimaryOnly = false) const;\n\n    expireEntry *getExpire(robj_roptr key) { return getExpire(szFromObj(key)); }\n    expireEntry *getExpire(const char *key);\n    const expireEntry *getExpire(const char *key) const;\n    const expireEntry *getExpire(robj_roptr key) const { return getExpire(szFromObj(key)); }\n\n    uint64_t mvccCheckpoint() const { return m_mvccCheckpoint; }\n\n    bool FStale() const;\n\n    // These need to be fixed\n    using redisDbPersistentData::size;\n    using redisDbPersistentData::expireSize;\n};\n\n/* Redis database representation. There are multiple databases identified\n * by integers from 0 (the default database) up to the max configured\n * database. The database number is the 'id' field in the structure. */\nstruct redisDb : public redisDbPersistentDataSnapshot \n{\n    // Legacy C API, Do not add more\n    friend void tryResizeHashTables(int);\n    friend int dbSyncDelete(redisDb *db, robj *key);\n    friend int dbAsyncDelete(redisDb *db, robj *key);\n    friend long long emptyDb(int dbnum, int flags, void(callback)(void*));\n    friend void scanGenericCommand(struct client *c, robj_roptr o, unsigned long cursor);\n    friend int dbSwapDatabases(int id1, int id2);\n    friend int removeExpire(redisDb *db, robj *key);\n    friend void setExpire(struct client *c, redisDb *db, robj *key, robj *subkey, long long when);\n    friend void setExpire(client *c, redisDb *db, robj *key, expireEntry &&e);\n    friend int evictionPoolPopulate(int dbid, redisDb *db, bool fVolatile, struct evictionPoolEntry *pool);\n    friend void activeDefragCycle(void);\n    friend void activeExpireCycle(int);\n    friend void expireSlaveKeys(void);\n    friend int performEvictions(bool fPreSnapshot);\n\n    typedef ::dict_const_iter const_iter;\n    typedef ::dict_iter iter;\n\n    redisDb() = default;\n\n    void initialize(int id, int storage_id=-1 /* default no storage */);\n    void storageProviderInitialize();\n    void storageProviderDelete();\n    virtual ~redisDb();\n\n    void dbOverwriteCore(redisDb::iter itr, sds keySds, robj *val, bool fUpdateMvcc, bool fRemoveExpire);\n\n    bool FKeyExpires(const char *key);\n    size_t clear(bool fAsync, void(callback)(void*));\n\n    // Import methods from redisDbPersistentData hidden by redisDbPersistentDataSnapshot\n    using redisDbPersistentData::slots;\n    using redisDbPersistentData::size;\n    using redisDbPersistentData::expand;\n    using redisDbPersistentData::trackkey;\n    using redisDbPersistentData::find;\n    using redisDbPersistentData::random;\n    using redisDbPersistentData::random_expire;\n    using redisDbPersistentData::end;\n    using redisDbPersistentData::getStats;\n    using redisDbPersistentData::insert;\n    using redisDbPersistentData::tryResize;\n    using redisDbPersistentData::incrementallyRehash;\n    using redisDbPersistentData::updateValue;\n    using redisDbPersistentData::syncDelete;\n    using redisDbPersistentData::asyncDelete;\n    using redisDbPersistentData::expireSize;\n    using redisDbPersistentData::removeExpire;\n    using redisDbPersistentData::removeSubkeyExpire;\n    using redisDbPersistentData::clear;\n    using redisDbPersistentData::emptyDbAsync;\n    using redisDbPersistentData::iterate;\n    using redisDbPersistentData::setExpire;\n    using redisDbPersistentData::trackChanges;\n    using redisDbPersistentData::processChanges;\n    using redisDbPersistentData::processChangesAsync;\n    using redisDbPersistentData::commitChanges;\n    using redisDbPersistentData::endSnapshot;\n    using redisDbPersistentData::restoreSnapshot;\n    using redisDbPersistentData::removeAllCachedValues;\n    using redisDbPersistentData::disableKeyCache;\n    using redisDbPersistentData::keycacheIsEnabled;\n    using redisDbPersistentData::dictUnsafeKeyOnly;\n    using redisDbPersistentData::prefetchKeysAsync;\n    using redisDbPersistentData::prepOverwriteForSnapshot;\n    using redisDbPersistentData::FRehashing;\n    using redisDbPersistentData::FTrackingChanges;\n    using redisDbPersistentData::CloneStorageCache;\n    using redisDbPersistentData::getStorageCache;\n    using redisDbPersistentData::bulkDirectStorageInsert;\n\npublic:\n    const redisDbPersistentDataSnapshot *createSnapshot(uint64_t mvccCheckpoint, bool fOptional) {\n        auto psnapshot = redisDbPersistentData::createSnapshot(mvccCheckpoint, fOptional);\n        if (psnapshot != nullptr)\n            mvccLastSnapshot = psnapshot->mvccCheckpoint();\n        return psnapshot;\n    }\n\n    unsigned long expires_cursor = 0;\n    dict *blocking_keys;        /* Keys with clients waiting for data (BLPOP)*/\n    dict *ready_keys;           /* Blocked keys that received a PUSH */\n    dict *watched_keys;         /* WATCHED keys for MULTI/EXEC CAS */\n    int id;                     /* Database ID */\n    int storage_id;             /* Mapped storage provider DB id which is same as the redisdb id above. But, when the database is swapped, the redisdb id above might be swapped to be consistent with the database index (id <-> g_pserver->db[index]) however the storage_id remains unchanged in order to maintain correct mapping to the underlying storage provider DB. This is valid only if there is a storage provider set.*/\n    long long last_expire_set;  /* when the last expire was set */\n    double avg_ttl;             /* Average TTL, just for stats */\n    list *defrag_later;         /* List of key names to attempt to defrag one by one, gradually. */\n    uint64_t mvccLastSnapshot = 0;\n};\n\n/* Declare database backup that include redis main DBs and slots to keys map.\n * Definition is in db.c. We can't define it here since we define CLUSTER_SLOTS\n * in cluster.h. */\ntypedef struct dbBackup dbBackup;\n\n/* Declare database backup that include redis main DBs and slots to keys map.\n * Definition is in db.c. We can't define it here since we define CLUSTER_SLOTS\n * in cluster.h. */\ntypedef struct dbBackup dbBackup;\n\n/* Client MULTI/EXEC state */\ntypedef struct multiCmd {\n    robj **argv;\n    int argc;\n    struct redisCommand *cmd;\n} multiCmd;\n\ntypedef struct multiState {\n    multiCmd *commands;     /* Array of MULTI commands */\n    int count;              /* Total number of MULTI commands */\n    int cmd_flags;          /* The accumulated command flags OR-ed together.\n                               So if at least a command has a given flag, it\n                               will be set in this field. */\n    int cmd_inv_flags;      /* Same as cmd_flags, OR-ing the ~flags. so that it\n                               is possible to know if all the commands have a\n                               certain flag. */\n} multiState;\n\nstruct listPos {\n    int wherefrom;      /* Where to pop from */\n    int whereto;        /* Where to push to */\n};                      /* The positions in the src/dst lists\n                            * where we want to pop/push an element\n                            * for BLPOP, BRPOP and BLMOVE. */\n\n/* This structure holds the blocking operation state for a client.\n * The fields used depend on client->btype. */\ntypedef struct blockingState {\n    /* Generic fields. */\n    mstime_t timeout;       /* Blocking operation timeout. If UNIX current time\n                             * is > timeout then the operation timed out. */\n\n    /* BLOCKED_LIST, BLOCKED_ZSET and BLOCKED_STREAM */\n    ::dict *keys;             /* The keys we are waiting to terminate a blocking\n                             * operation such as BLPOP or XREAD. Or NULL. */\n    robj *target;           /* The key that should receive the element,\n                             * for BLMOVE. */\n\n    listPos listpos;\n\n    /* BLOCK_STREAM */\n    size_t xread_count;     /* XREAD COUNT option. */\n    robj *xread_group;      /* XREADGROUP group name. */\n    robj *xread_consumer;   /* XREADGROUP consumer name. */\n    int xread_group_noack;\n\n    /* BLOCKED_WAIT */\n    int numreplicas;        /* Number of replicas we are waiting for ACK. */\n    long long reploffset;   /* Replication offset to reach. */\n\n    /* BLOCKED_MODULE */\n    void *module_blocked_handle; /* RedisModuleBlockedClient structure.\n                                    which is opaque for the Redis core, only\n                                    handled in module.c. */\n} blockingState;\n\n/* The following structure represents a node in the g_pserver->ready_keys list,\n * where we accumulate all the keys that had clients blocked with a blocking\n * operation such as B[LR]POP, but received new data in the context of the\n * last executed command.\n *\n * After the execution of every command or script, we run this list to check\n * if as a result we should serve data to clients blocked, unblocking them.\n * Note that g_pserver->ready_keys will not have duplicates as there dictionary\n * also called ready_keys in every structure representing a Redis database,\n * where we make sure to remember if a given key was already added in the\n * g_pserver->ready_keys list. */\ntypedef struct readyList {\n    redisDb *db;\n    robj *key;\n} readyList;\n\n/* This structure represents a Redis user. This is useful for ACLs, the\n * user is associated to the connection after the connection is authenticated.\n * If there is no associated user, the connection uses the default user. */\n#define USER_COMMAND_BITS_COUNT 1024    /* The total number of command bits\n                                           in the user structure. The last valid\n                                           command ID we can set in the user\n                                           is USER_COMMAND_BITS_COUNT-1. */\n#define USER_FLAG_ENABLED (1<<0)        /* The user is active. */\n#define USER_FLAG_DISABLED (1<<1)       /* The user is disabled. */\n#define USER_FLAG_ALLKEYS (1<<2)        /* The user can mention any key. */\n#define USER_FLAG_ALLCOMMANDS (1<<3)    /* The user can run all commands. */\n#define USER_FLAG_NOPASS      (1<<4)    /* The user requires no password, any\n                                           provided password will work. For the\n                                           default user, this also means that\n                                           no AUTH is needed, and every\n                                           connection is immediately\n                                           authenticated. */\n#define USER_FLAG_ALLCHANNELS (1<<5)    /* The user can mention any Pub/Sub\n                                           channel. */\n#define USER_FLAG_SANITIZE_PAYLOAD (1<<6)       /* The user require a deep RESTORE\n                                                 * payload sanitization. */\n#define USER_FLAG_SANITIZE_PAYLOAD_SKIP (1<<7)  /* The user should skip the\n                                                 * deep sanitization of RESTORE\n                                                 * payload. */\n\ntypedef struct {\n    sds name;       /* The username as an SDS string. */\n    uint64_t flags; /* See USER_FLAG_* */\n\n    /* The bit in allowed_commands is set if this user has the right to\n     * execute this command. In commands having subcommands, if this bit is\n     * set, then all the subcommands are also available.\n     *\n     * If the bit for a given command is NOT set and the command has\n     * subcommands, Redis will also check allowed_subcommands in order to\n     * understand if the command can be executed. */\n    uint64_t allowed_commands[USER_COMMAND_BITS_COUNT/64];\n\n    /* This array points, for each command ID (corresponding to the command\n     * bit set in allowed_commands), to an array of SDS strings, terminated by\n     * a NULL pointer, with all the sub commands that can be executed for\n     * this command. When no subcommands matching is used, the field is just\n     * set to NULL to avoid allocating USER_COMMAND_BITS_COUNT pointers. */\n    sds **allowed_subcommands;\n    list *passwords; /* A list of SDS valid passwords for this user. */\n    list *patterns;  /* A list of allowed key patterns. If this field is NULL\n                        the user cannot mention any key in a command, unless\n                        the flag ALLKEYS is set in the user. */\n    list *channels;  /* A list of allowed Pub/Sub channel patterns. If this\n                        field is NULL the user cannot mention any channel in a\n                        `PUBLISH` or [P][UNSUBSCRIBE] command, unless the flag\n                        ALLCHANNELS is set in the user. */\n} user;\n\n/* With multiplexing we need to take per-client state.\n * Clients are taken in a linked list. */\n\n#define CLIENT_ID_AOF (UINT64_MAX) /* Reserved ID for the AOF client. If you\n                                      need more reserved IDs use UINT64_MAX-1,\n                                      -2, ... and so forth. */\n\nstruct parsed_command {\n    robj** argv = nullptr;\n    int argc = 0;\n    int argcMax;\n    long long reploff = 0;\n    size_t argv_len_sum = 0;    /* Sum of lengths of objects in argv list. */\n\n    parsed_command(int maxargs) {\n        argv = (robj**)zmalloc(sizeof(robj*)*maxargs);\n        argcMax = maxargs;\n    }\n\n    parsed_command &operator=(parsed_command &&o) {\n        argv = o.argv;\n        argc = o.argc;\n        argcMax = o.argcMax;\n        reploff = o.reploff;\n        o.argv = nullptr;\n        o.argc = 0;\n        o.argcMax = 0;\n        o.reploff = 0;\n        return *this;\n    }\n\n    parsed_command(parsed_command &o) = delete;\n    parsed_command(parsed_command &&o) {\n        argv = o.argv;\n        argc = o.argc;\n        argcMax = o.argcMax;\n        reploff = o.reploff;\n        o.argv = nullptr;\n        o.argc = 0;\n        o.argcMax = 0;\n        o.reploff = 0;\n    }\n\n    ~parsed_command() {\n        if (argv != nullptr) {\n            for (int i = 0; i < argc; ++i) {\n                decrRefCount(argv[i]);\n            }\n            zfree(argv);\n        }\n    }\n};\n\nstruct client {\n    uint64_t id;            /* Client incremental unique ID. */\n    connection *conn;\n    int resp;               /* RESP protocol version. Can be 2 or 3. */\n    redisDb *db;            /* Pointer to currently SELECTed DB. */\n    robj *name;             /* As set by CLIENT SETNAME. */\n    sds querybuf;           /* Buffer we use to accumulate client queries. */\n    size_t qb_pos;          /* The position we have read in querybuf. */\n    sds pending_querybuf;   /* If this client is flagged as master, this buffer\n                               represents the yet not applied portion of the\n                               replication stream that we are receiving from\n                               the master. */\n    size_t querybuf_peak;   /* Recent (100ms or more) peak of querybuf size. */\n    int original_argc;      /* Num of arguments of original command if arguments were rewritten. */\n    robj **original_argv;   /* Arguments of original command if arguments were rewritten. */\n    struct redisCommand *cmd, *lastcmd;  /* Last command executed. */\n    ::user *user;             /* User associated with this connection. If the\n                               user is set to NULL the connection can do\n                               anything (admin). */\n    int reqtype;            /* Request protocol type: PROTO_REQ_* */\n    int multibulklen;       /* Number of multi bulk arguments left to read. */\n    long bulklen;           /* Length of bulk argument in multi bulk request. */\n    list *reply;            /* List of reply objects to send to the client. */\n    unsigned long long reply_bytes; /* Tot bytes of objects in reply list. */\n    size_t sentlen;         /* Amount of bytes already sent in the current\n                               buffer or object being sent. */\n    time_t ctime;           /* Client creation time. */\n    long duration;          /* Current command duration. Used for measuring latency of blocking/non-blocking cmds */\n    time_t lastinteraction; /* Time of the last interaction, used for timeout */\n    time_t obuf_soft_limit_reached_time;\n    std::atomic<uint64_t> flags;              /* Client flags: CLIENT_* macros. */\n    int casyncOpsPending;\n    int fPendingAsyncWrite; /* NOTE: Not a flag because it is written to outside of the client lock (locked by the global lock instead) */\n    std::atomic<bool> fPendingAsyncWriteHandler;\n    int authenticated;      /* Needed when the default user requires auth. */\n    int replstate;          /* Replication state if this is a replica. */\n    int repl_put_online_on_ack; /* Install replica write handler on ACK. */\n    int repldbfd;           /* Replication DB file descriptor. */\n    off_t repldboff;        /* Replication DB file offset. */\n    off_t repldbsize;       /* Replication DB file size. */\n    sds replpreamble;       /* Replication DB preamble. */\n    time_t repl_down_since; /* When client lost connection. */\n    long long read_reploff; /* Read replication offset if this is a master. */\n    long long reploff;      /* Applied replication offset if this is a master. */\n    long long reploff_cmd;  /* The replication offset of the executing command, reploff gets set to this after the execution completes */\n    long long repl_ack_off; /* Replication ack offset, if this is a replica. */\n    long long repl_ack_time;/* Replication ack time, if this is a replica. */\n    long long repl_last_partial_write; /* The last time the server did a partial write from the RDB child pipe to this replica  */\n    long long psync_initial_offset; /* FULLRESYNC reply offset other slaves\n                                       copying this replica output buffer\n                                       should use. */\n                                       \n    long long repl_curr_off = -1;/* Replication offset of the replica, also where in the backlog we need to start from\n                                  * when sending data to this replica. */\n    long long repl_end_off = -1; /* Replication offset to write to, stored in the replica, as opposed to using the global offset \n                                  * to prevent needing the global lock */\n\n    char replid[CONFIG_RUN_ID_SIZE+1]; /* Master replication ID (if master). */\n    int slave_listening_port; /* As configured with: REPLCONF listening-port */\n    char *slave_addr;       /* Optionally given by REPLCONF ip-address */\n    int slave_capa;         /* Slave capabilities: SLAVE_CAPA_* bitwise OR. */\n    multiState mstate;      /* MULTI/EXEC state */\n    int btype;              /* Type of blocking op if CLIENT_BLOCKED. */\n    blockingState bpop;     /* blocking state */\n    long long woff;         /* Last write global replication offset. */\n    list *watched_keys;     /* Keys WATCHED for MULTI/EXEC CAS */\n    ::dict *pubsub_channels;  /* channels a client is interested in (SUBSCRIBE) */\n    list *pubsub_patterns;  /* patterns a client is interested in (SUBSCRIBE) */\n    sds peerid;             /* Cached peer ID. */\n    sds sockname;           /* Cached connection target address. */\n    listNode *client_list_node; /* list node in client list */\n    listNode *paused_list_node; /* list node within the pause list */\n    RedisModuleUserChangedFunc auth_callback; /* Module callback to execute\n                                               * when the authenticated user\n                                               * changes. */\n    void *auth_callback_privdata; /* Private data that is passed when the auth\n                                   * changed callback is executed. Opaque for\n                                   * Redis Core. */\n    void *auth_module;      /* The module that owns the callback, which is used\n                             * to disconnect the client if the module is\n                             * unloaded for cleanup. Opaque for Redis Core.*/\n\n    /* UUID announced by the client (default nil) - used to detect multiple connections to/from the same peer */\n    /* compliant servers will announce their UUIDs when a replica connection is started, and return when asked */\n    /* UUIDs are transient and lost when the server is shut down */\n    unsigned char uuid[UUID_BINARY_LEN];\n\n    /* If this client is in tracking mode and this field is non zero,\n     * invalidation messages for keys fetched by this client will be send to\n     * the specified client ID. */\n    uint64_t client_tracking_redirection;\n    rax *client_tracking_prefixes; /* A dictionary of prefixes we are already\n                                      subscribed to in BCAST mode, in the\n                                      context of client side caching. */\n    /* In clientsCronTrackClientsMemUsage() we track the memory usage of\n     * each client and add it to the sum of all the clients of a given type,\n     * however we need to remember what was the old contribution of each\n     * client, and in which categoty the client was, in order to remove it\n     * before adding it the new value. */\n    uint64_t client_cron_last_memory_usage;\n    int      client_cron_last_memory_type;\n    /* Response buffer */\n    int bufpos;\n    char buf[PROTO_REPLY_CHUNK_BYTES];\n\n    /* Async Response Buffer - other threads write here */\n    clientReplyBlock *replyAsync;\n\n    uint64_t mvccCheckpoint = 0;    // the MVCC checkpoint of our last write\n\n    int iel; /* the event loop index we're registered with */\n    struct fastlock lock {\"client\"};\n    int master_error;\n    std::vector<parsed_command> vecqueuedcmd;\n    int argc;\n    robj **argv;\n    size_t argv_len_sumActive = 0;\n\n    bool FPendingReplicaWrite() const {\n        return repl_curr_off != repl_end_off && replstate == SLAVE_STATE_ONLINE;\n    }\n\n    // post a function from a non-client thread to run on its client thread\n    bool postFunction(std::function<void(client *)> fn, bool fLock = true);\n    size_t argv_len_sum() const;\n    bool asyncCommand(std::function<void(const redisDbPersistentDataSnapshot *, const std::vector<robj_sharedptr> &)> &&mainFn, \n                        std::function<void(const redisDbPersistentDataSnapshot *)> &&postFn = nullptr);\n    char* fprint;\n};\n\nstruct saveparam {\n    time_t seconds;\n    int changes;\n};\n\nstruct moduleLoadQueueEntry {\n    sds path;\n    int argc;\n    robj **argv;\n};\n\nstruct sentinelLoadQueueEntry {\n    int argc;\n    sds *argv;\n    int linenum;\n    sds line;\n};\n\nstruct sentinelConfig {\n    list *pre_monitor_cfg;\n    list *monitor_cfg;\n    list *post_monitor_cfg;\n};\n\nstruct sharedObjectsStruct {\n    robj *crlf, *ok, *err, *emptybulk, *czero, *cone, *pong, *space,\n    *colon, *queued, *nullbulk, *null[4], *nullarray[4], *emptymap[4], *emptyset[4],\n    *emptyarray, *wrongtypeerr, *nokeyerr, *syntaxerr, *sameobjecterr,\n    *outofrangeerr, *noscripterr, *loadingerr, *slowscripterr, *bgsaveerr,\n    *masterdownerr, *roslaveerr, *execaborterr, *noautherr, *noreplicaserr,\n    *busykeyerr, *oomerr, *plus, *messagebulk, *pmessagebulk, *subscribebulk,\n    *unsubscribebulk, *psubscribebulk, *punsubscribebulk, *del, *unlink,\n    *rpop, *lpop, *lpush, *rpoplpush, *lmove, *blmove, *zpopmin, *zpopmax,\n    *emptyscan, *multi, *exec, *left, *right, *hset, *srem, *xgroup, *xclaim,  \n    *script, *replconf, *eval, *persist, *set, *pexpireat, *pexpire, \n    *time, *pxat, *px, *retrycount, *force, *justid, \n    *lastid, *ping, *replping, *setid, *keepttl, *load, *createconsumer,\n    *getack, *special_asterick, *special_equals, *default_username,\n    *hdel, *zrem, *mvccrestore, *pexpirememberat, *redacted,\n    *select[PROTO_SHARED_SELECT_CMDS],\n    *integers[OBJ_SHARED_INTEGERS],\n    *mbulkhdr[OBJ_SHARED_BULKHDR_LEN], /* \"*<value>\\r\\n\" */\n    *bulkhdr[OBJ_SHARED_BULKHDR_LEN];  /* \"$<value>\\r\\n\" */\n    sds minstring, maxstring;\n};\n\n/* ZSETs use a specialized version of Skiplists */\nstruct zskiplistLevel {\n        struct zskiplistNode *forward;\n        unsigned long span;\n};\ntypedef struct zskiplistNode {\n    sds ele;\n    double score;\n    struct zskiplistNode *backward;\n    \n#ifdef __cplusplus\n    zskiplistLevel *level(size_t idx) {\n        return reinterpret_cast<zskiplistLevel*>(this+1) + idx;\n    }\n#else\n    struct zskiplistLevel level[];\n#endif\n} zskiplistNode;\n\ntypedef struct zskiplist {\n    struct zskiplistNode *header, *tail;\n    unsigned long length;\n    int level;\n} zskiplist;\n\ntypedef struct zset {\n    ::dict *dict;\n    zskiplist *zsl;\n} zset;\n\ntypedef struct clientBufferLimitsConfig {\n    unsigned long long hard_limit_bytes;\n    unsigned long long soft_limit_bytes;\n    time_t soft_limit_seconds;\n} clientBufferLimitsConfig;\n\nextern clientBufferLimitsConfig clientBufferLimitsDefaults[CLIENT_TYPE_OBUF_COUNT];\n\n/* The redisOp structure defines a Redis Operation, that is an instance of\n * a command with an argument vector, database ID, propagation target\n * (PROPAGATE_*), and command pointer.\n *\n * Currently only used to additionally propagate more commands to AOF/Replication\n * after the propagation of the executed command. */\ntypedef struct redisOp {\n    robj **argv;\n    int argc, dbid, target;\n    struct redisCommand *cmd;\n} redisOp;\n\n/* Defines an array of Redis operations. There is an API to add to this\n * structure in an easy way.\n *\n * redisOpArrayInit();\n * redisOpArrayAppend();\n * redisOpArrayFree();\n */\ntypedef struct redisOpArray {\n    redisOp *ops;\n    int numops;\n} redisOpArray;\n\n/* This structure is returned by the getMemoryOverheadData() function in\n * order to return memory overhead information. */\nstruct redisMemOverhead {\n    size_t peak_allocated;\n    size_t total_allocated;\n    size_t startup_allocated;\n    size_t repl_backlog;\n    size_t clients_slaves;\n    size_t clients_normal;\n    size_t aof_buffer;\n    size_t lua_caches;\n    size_t overhead_total;\n    size_t dataset;\n    size_t total_keys;\n    size_t bytes_per_key;\n    float dataset_perc;\n    float peak_perc;\n    float total_frag;\n    ssize_t total_frag_bytes;\n    float allocator_frag;\n    ssize_t allocator_frag_bytes;\n    float allocator_rss;\n    ssize_t allocator_rss_bytes;\n    float rss_extra;\n    size_t rss_extra_bytes;\n    size_t num_dbs;\n    struct {\n        size_t dbid;\n        size_t overhead_ht_main;\n        size_t overhead_ht_expires;\n    } *db;\n};\n\n\nstruct redisMaster {\n    char *masteruser;               /* AUTH with this user and masterauth with master */\n    char *masterauth;               /* AUTH with this password with master */\n    char *masterhost;               /* Hostname of master */\n    int masterport;                 /* Port of master */\n    client *cached_master;          /* Cached master to be reused for PSYNC. */\n    client *master;\n    /* The following two fields is where we store master PSYNC replid/offset\n     * while the PSYNC is in progress. At the end we'll copy the fields into\n     * the server->master client structure. */\n    char master_replid[CONFIG_RUN_ID_SIZE+1];  /* Master PSYNC runid. */\n    long long master_initial_offset;           /* Master PSYNC offset. */\n\n    bool isActive = false;\n    bool isKeydbFastsync = false;\n    int repl_state;          /* Replication status if the instance is a replica */\n    off_t repl_transfer_size; /* Size of RDB to read from master during sync. */\n    off_t repl_transfer_read; /* Amount of RDB read from master during sync. */\n    off_t repl_transfer_last_fsync_off; /* Offset when we fsync-ed last time. */\n    connection *repl_transfer_s;     /* Replica -> Master SYNC socket */\n    int repl_transfer_fd;    /* Replica -> Master SYNC temp file descriptor */\n    char *repl_transfer_tmpfile; /* Replica-> master SYNC temp file name */\n    time_t repl_transfer_lastio; /* Unix time of the latest read, for timeout */\n    time_t repl_down_since; /* Unix time at which link with master went down */\n\n    class SnapshotPayloadParseState *parseState;\n    sds bulkreadBuffer = nullptr;\n\n    unsigned char master_uuid[UUID_BINARY_LEN];  /* Used during sync with master, this is our master's UUID */\n                                                /* After we've connected with our master use the UUID in g_pserver->master */\n    uint64_t mvccLastSync;\n    /* During a handshake the server may have stale keys, we track these here to share once a reciprocal connection is made */\n    std::map<int, std::vector<robj_sharedptr>> *staleKeyMap;\n    int ielReplTransfer = -1;\n};\n\nstruct MasterSaveInfo {\n    MasterSaveInfo() {\n        memcpy(master_replid, \"0000000000000000000000000000000000000000\", sizeof(master_replid));\n    }\n    MasterSaveInfo(const redisMaster &mi) {\n        memcpy(master_replid, mi.master_replid, sizeof(mi.master_replid));\n        if (mi.master) {\n            master_initial_offset = mi.master->reploff;\n            selected_db = mi.master->db->id;\n        } else if (mi.cached_master) {\n            master_initial_offset = mi.cached_master->reploff;\n            selected_db = mi.cached_master->db->id;\n        } else {\n            master_initial_offset = -1;\n            selected_db = 0;\n        }\n        masterport = mi.masterport;\n        if (mi.masterhost)\n            masterhost = sdsstring(sdsdup(mi.masterhost));\n        masterport = mi.masterport;\n    }\n    MasterSaveInfo(const MasterSaveInfo &other) {\n        masterhost = other.masterhost;\n        masterport = other.masterport;\n        memcpy(master_replid, other.master_replid, sizeof(master_replid));\n        master_initial_offset = other.master_initial_offset;\n    }\n\n    MasterSaveInfo &operator=(const MasterSaveInfo &other) {\n        masterhost = other.masterhost;\n        masterport = other.masterport;\n        memcpy(master_replid, other.master_replid, sizeof(master_replid));\n        master_initial_offset = other.master_initial_offset;\n        return *this;\n    }\n\n    sdsstring masterhost;\n    int masterport;\n    char master_replid[CONFIG_RUN_ID_SIZE+1];\n    long long master_initial_offset;\n    int selected_db;\n};\n\n/* This structure can be optionally passed to RDB save/load functions in\n * order to implement additional functionalities, by storing and loading\n * metadata to the RDB file.\n *\n * Currently the only use is to select a DB at load time, useful in\n * replication in order to make sure that chained slaves (slaves of slaves)\n * select the correct DB and are able to accept the stream coming from the\n * top-level master. */\nclass rdbSaveInfo {\npublic:\n    rdbSaveInfo() {\n        repl_stream_db = -1;\n        repl_id_is_set = 0;\n        memcpy(repl_id, \"0000000000000000000000000000000000000000\", sizeof(repl_id));\n        repl_id[CONFIG_RUN_ID_SIZE] = '\\0';\n        repl_offset = -1;\n        fForceSetKey = TRUE;\n        mvccMinThreshold = 0;\n    }\n    rdbSaveInfo(const rdbSaveInfo &other) {\n        repl_stream_db = other.repl_stream_db;\n        repl_id_is_set = other.repl_id_is_set;\n        memcpy(repl_id, other.repl_id, sizeof(repl_id));\n        repl_offset = other.repl_offset;\n        fForceSetKey = other.fForceSetKey;\n        mvccMinThreshold = other.mvccMinThreshold;\n        vecmastersaveinfo = other.vecmastersaveinfo;\n        master_repl_offset = other.master_repl_offset;\n        mi = other.mi;\n    }\n\n    rdbSaveInfo &operator=(const rdbSaveInfo &other) {\n        repl_stream_db = other.repl_stream_db;\n        repl_id_is_set = other.repl_id_is_set;\n        memcpy(repl_id, other.repl_id, sizeof(repl_id));\n        repl_offset = other.repl_offset;\n        fForceSetKey = other.fForceSetKey;\n        mvccMinThreshold = other.mvccMinThreshold;\n        vecmastersaveinfo = other.vecmastersaveinfo;\n        master_repl_offset = other.master_repl_offset;\n        mi = other.mi;\n\n        return *this;\n    }\n\n    void addMaster(const MasterSaveInfo &si) {\n        vecmastersaveinfo.push_back(si);\n    }\n\n    size_t numMasters() {\n        return vecmastersaveinfo.size();\n    }\n\n    /* Used saving and loading. */\n    int repl_stream_db;  /* DB to select in g_pserver->master client. */\n\n    /* Used only loading. */\n    int repl_id_is_set;  /* True if repl_id field is set. */\n    char repl_id[CONFIG_RUN_ID_SIZE+1];     /* Replication ID. */\n    long long repl_offset;                  /* Replication offset. */\n    int fForceSetKey;\n\n    /* Used In Save */\n    long long master_repl_offset;\n\n    uint64_t mvccMinThreshold;\n    std::vector<MasterSaveInfo> vecmastersaveinfo;\n    struct redisMaster *mi = nullptr;\n};\n\nstruct malloc_stats {\n    size_t zmalloc_used;\n    size_t process_rss;\n    size_t allocator_allocated;\n    size_t allocator_active;\n    size_t allocator_resident;\n    size_t sys_total;\n    size_t sys_available;\n};\n\ntypedef struct socketFds {\n    int fd[CONFIG_BINDADDR_MAX];\n    int count;\n} socketFds;\n\n/*-----------------------------------------------------------------------------\n * TLS Context Configuration\n *----------------------------------------------------------------------------*/\n\ntypedef struct redisTLSContextConfig {\n    char *cert_file;                /* Server side and optionally client side cert file name */\n    char *key_file;                 /* Private key filename for cert_file */\n    char *key_file_pass;            /* Optional password for key_file */\n    char *client_cert_file;         /* Certificate to use as a client; if none, use cert_file */\n    char *client_key_file;          /* Private key filename for client_cert_file */\n    char *client_key_file_pass;     /* Optional password for client_key_file */\n    char *dh_params_file;\n    char *ca_cert_file;\n    char *ca_cert_dir;\n    char *protocols;\n    char *ciphers;\n    char *ciphersuites;\n    int prefer_server_ciphers;\n    int session_caching;\n    int session_cache_size;\n    int session_cache_timeout;\n    time_t cert_file_last_modified;\n    time_t key_file_last_modified;\n    time_t client_cert_file_last_modified;\n    time_t client_key_file_last_modified;\n    time_t ca_cert_file_last_modified;\n    time_t ca_cert_dir_last_modified;\n} redisTLSContextConfig;\n\n/*-----------------------------------------------------------------------------\n * Global server state\n *----------------------------------------------------------------------------*/\n\nstruct clusterState;\n\n/* AIX defines hz to __hz, we don't use this define and in order to allow\n * Redis build on AIX we need to undef it. */\n#ifdef _AIX\n#undef hz\n#endif\n\n#define CHILD_TYPE_NONE 0\n#define CHILD_TYPE_RDB 1\n#define CHILD_TYPE_AOF 2\n#define CHILD_TYPE_LDB 3\n#define CHILD_TYPE_MODULE 4\n\ntypedef enum childInfoType {\n    CHILD_INFO_TYPE_CURRENT_INFO,\n    CHILD_INFO_TYPE_AOF_COW_SIZE,\n    CHILD_INFO_TYPE_RDB_COW_SIZE,\n    CHILD_INFO_TYPE_MODULE_COW_SIZE\n} childInfoType;\n\n#define MAX_EVENT_LOOPS 16\n#define IDX_EVENT_LOOP_MAIN 0\n\nclass GarbageCollectorCollection\n{\n    GarbageCollector<redisDbPersistentDataSnapshot> garbageCollectorSnapshot;\n    GarbageCollector<ICollectable> garbageCollectorGeneric;\n\n    class CPtrCollectable : public ICollectable \n    {\n        void *m_pv;\n\n    public:\n        CPtrCollectable(void *pv) \n            : m_pv(pv)\n            {}\n\n        CPtrCollectable(CPtrCollectable &&move) {\n            m_pv = move.m_pv;\n            move.m_pv = nullptr;\n        }\n\n        virtual ~CPtrCollectable() {\n            zfree(m_pv);\n        }\n    };\n\npublic:\n    struct Epoch\n    {\n        uint64_t epochSnapshot = 0;\n        uint64_t epochGeneric = 0;\n\n        void reset() {\n            epochSnapshot = 0;\n            epochGeneric = 0;\n        }\n\n        Epoch() = default;\n\n        Epoch (const Epoch &other) {\n            epochSnapshot = other.epochSnapshot;\n            epochGeneric = other.epochGeneric;\n        }\n\n        Epoch &operator=(const Epoch &other) {\n            serverAssert(isReset());\n            epochSnapshot = other.epochSnapshot;\n            epochGeneric = other.epochGeneric;\n            return *this;\n        }\n\n        bool isReset() const {\n            return epochSnapshot == 0 && epochGeneric == 0;\n        }\n    };\n\n    Epoch startEpoch()\n    {\n        Epoch e;\n        e.epochSnapshot = garbageCollectorSnapshot.startEpoch();\n        e.epochGeneric = garbageCollectorGeneric.startEpoch();\n        return e;\n    }\n\n    void endEpoch(Epoch &e, bool fNoFree = false)\n    {\n        auto epochSnapshot = e.epochSnapshot;\n        auto epochGeneric = e.epochGeneric;\n        e.reset();  // We must do this early as GC'd dtors can themselves try to enqueue more data\n        garbageCollectorSnapshot.endEpoch(epochSnapshot, fNoFree);\n        garbageCollectorGeneric.endEpoch(epochGeneric, fNoFree);\n    }\n\n    bool empty()\n    {\n        return garbageCollectorGeneric.empty() && garbageCollectorSnapshot.empty();\n    }\n\n    void shutdown()\n    {\n        garbageCollectorSnapshot.shutdown();\n        garbageCollectorGeneric.shutdown();\n    }\n\n    void enqueue(Epoch e, std::unique_ptr<redisDbPersistentDataSnapshot> &&sp)\n    {\n        garbageCollectorSnapshot.enqueue(e.epochSnapshot, std::move(sp));\n    }\n\n    void enqueue(Epoch e, std::unique_ptr<ICollectable> &&sp)\n    {\n        garbageCollectorGeneric.enqueue(e.epochGeneric, std::move(sp));\n    }\n\n    template<typename T>\n    void enqueueCPtr(Epoch e, T p)\n    {\n        auto sp = std::make_unique<CPtrCollectable>(reinterpret_cast<void*>(p));\n        enqueue(e, std::move(sp));\n    }\n};\n\n// Per-thread variabels that may be accessed without a lock\nstruct redisServerThreadVars {\n    aeEventLoop *el = nullptr;\n    socketFds ipfd;             /* TCP socket file descriptors */\n    socketFds tlsfd;            /* TLS socket file descriptors */\n    int in_eval;                /* Are we inside EVAL? */\n    int in_exec;                /* Are we inside EXEC? */\n    std::vector<client*> clients_pending_write; /* There is to write or install handler. */\n    list *unblocked_clients;     /* list of clients to unblock before next loop NOT THREADSAFE */\n    list *clients_pending_asyncwrite;\n    int cclients;\n    int cclientsReplica = 0;\n    client *current_client; /* Current client */\n    long fixed_time_expire = 0;     /* If > 0, expire keys against server.mstime. */\n    client *lua_client = nullptr;   /* The \"fake client\" to query Redis from Lua */\n    struct fastlock lockPendingWrite { \"thread pending write\" };\n    char neterr[ANET_ERR_LEN];   /* Error buffer for anet.c */\n    long unsigned commandsExecuted = 0;\n    GarbageCollectorCollection::Epoch gcEpoch;\n    const redisDbPersistentDataSnapshot **rgdbSnapshot = nullptr;\n    long long stat_total_error_replies; /* Total number of issued error replies ( command + rejected errors ) */\n    long long prev_err_count; /* per thread marker of exisiting errors during a call */\n    bool fRetrySetAofEvent = false;\n    bool modulesEnabledThisAeLoop = false; /* In this loop of aeMain, were modules enabled before \n                                              the thread went to sleep? */\n    bool disable_async_commands = false; /* this is only valid for one cycle of the AE loop and is reset in afterSleep */\n    \n    int propagate_in_transaction = 0;  /* Make sure we don't propagate nested MULTI/EXEC */\n    int client_pause_in_transaction = 0; /* Was a client pause executed during this Exec? */\n    std::vector<client*> vecclientsProcess;\n    dictAsyncRehashCtl *rehashCtl = nullptr;\n\n    int getRdbKeySaveDelay();\nprivate:\n    int rdb_key_save_delay = -1; // thread local cache\n};\n\n// Const vars are not changed after worker threads are launched\nstruct redisServerConst {\n    pid_t pid;                  /* Main process pid. */\n    time_t stat_starttime;          /* Server start time */\n    pthread_t main_thread_id;         /* Main thread id */\n    pthread_t time_thread_id;\n    char *configfile;           /* Absolute config file path, or NULL */\n    char *executable;           /* Absolute executable file path. */\n    char **exec_argv;           /* Executable argv vector (copy). */\n\n    int cthreads;               /* Number of main worker threads */\n    int fThreadAffinity;        /* Should we pin threads to cores? */\n    int threadAffinityOffset = 0; /* Where should we start pinning them? */\n    char *pidfile;              /* PID file path */\n\n    /* Fast pointers to often looked up command */\n    struct redisCommand *delCommand, *multiCommand, *lpushCommand,\n                        *lpopCommand, *rpopCommand, *zpopminCommand,\n                        *zpopmaxCommand, *sremCommand, *execCommand,\n                        *expireCommand, *pexpireCommand, *xclaimCommand,\n                        *xgroupCommand, *rreplayCommand, *rpoplpushCommand,\n                        *hdelCommand, *zremCommand, *lmoveCommand;\n\n    /* Configuration */\n    char *default_masteruser;               /* AUTH with this user and masterauth with master */\n    char *default_masterauth;               /* AUTH with this password with master */\n    int verbosity;                  /* Loglevel in keydb.conf */\n    int maxidletime;                /* Client timeout in seconds */\n    int tcpkeepalive;               /* Set SO_KEEPALIVE if non-zero. */\n    int active_expire_enabled;      /* Can be disabled for testing purposes. */\n    int active_defrag_enabled;\n    int jemalloc_bg_thread;         /* Enable jemalloc background thread */\n    size_t active_defrag_ignore_bytes; /* minimum amount of fragmentation waste to start active defrag */\n    int active_defrag_threshold_lower; /* minimum percentage of fragmentation to start active defrag */\n    int active_defrag_threshold_upper; /* maximum percentage of fragmentation at which we use maximum effort */\n    int active_defrag_cycle_min;       /* minimal effort for defrag in CPU percentage */\n    int active_defrag_cycle_max;       /* maximal effort for defrag in CPU percentage */\n    unsigned long active_defrag_max_scan_fields; /* maximum number of fields of set/hash/zset/list to process from within the main dict scan */\n    size_t client_max_querybuf_len; /* Limit for client query buffer length */\n    int dbnum = 0;                      /* Total number of configured DBs */\n    int supervised;                 /* 1 if supervised, 0 otherwise. */\n    int supervised_mode;            /* See SUPERVISED_* */\n    int daemonize;                  /* True if running as a daemon */\n    int sanitize_dump_payload;      /* Enables deep sanitization for ziplist and listpack in RDB and RESTORE. */\n    int skip_checksum_validation;   /* Disables checksum validateion for RDB and RESTORE payload. */\n    int set_proc_title;             /* True if change proc title */\n    char *proc_title_template;      /* Process title template format */\n    clientBufferLimitsConfig client_obuf_limits[CLIENT_TYPE_OBUF_COUNT];\n\n    /* System hardware info */\n    size_t system_memory_size;  /* Total memory in system as reported by OS */\n\n    unsigned char uuid[UUID_BINARY_LEN];         /* This server's UUID - populated on boot */\n\n    int enable_motd;            /* Flag to retrieve the Message of today using CURL request*/\n\n    int delete_on_evict = false;   // Only valid when a storage provider is set\n    int thread_min_client_threshold = 50;\n    int multimaster_no_forward;\n    int storage_memory_model = STORAGE_WRITETHROUGH;\n    char *storage_conf = nullptr;\n    int fForkBgSave = false;\n    int time_thread_priority = false;\n    long long repl_backlog_disk_size = 0;\n    int force_backlog_disk = 0;\n};\n\nstruct redisServer {\n    /* General */\n    int dynamic_hz;             /* Change hz value depending on # of clients. */\n    int config_hz;              /* Configured HZ value. May be different than\n                                   the actual 'hz' field value if dynamic-hz\n                                   is enabled. */\n    mode_t umask;               /* The umask value of the process on startup */\n    std::atomic<int> hz;        /* serverCron() calls frequency in hertz */\n    int in_fork_child;          /* indication that this is a fork child */\n    IStorage *metadataDb = nullptr;\n    redisDb **db = nullptr;\n    dict *commands;             /* Command table */\n    dict *orig_commands;        /* Command table before command renaming. */\n\n\n    struct redisServerThreadVars rgthreadvar[MAX_EVENT_LOOPS];\n    struct redisServerThreadVars modulethreadvar; /* Server thread local variables to be used by module threads */\n    pthread_t rgthread[MAX_EVENT_LOOPS];\n\n    std::atomic<unsigned int> lruclock;      /* Clock for LRU eviction */\n    std::atomic<int> shutdown_asap;          /* SHUTDOWN needed ASAP */\n    rax *errors;                /* Errors table */\n    int activerehashing;        /* Incremental rehash in serverCron() */\n    int active_defrag_running;  /* Active defragmentation running (holds current scan aggressiveness) */\n    int enable_async_rehash = 1;    /* Should we use the async rehash feature? */\n    int cronloops;              /* Number of times the cron function run */\n    char runid[CONFIG_RUN_ID_SIZE+1];  /* ID always different at every exec. */\n    int sentinel_mode;          /* True if this instance is a Sentinel. */\n    size_t initial_memory_usage; /* Bytes used after initialization. */\n    int always_show_logo;       /* Show logo even for non-stdout logging. */\n    char *ignore_warnings;      /* Config: warnings that should be ignored. */\n    pause_type client_pause_type;      /* True if clients are currently paused */\n    /* Modules */\n    ::dict *moduleapi;            /* Exported core APIs dictionary for modules. */\n    ::dict *sharedapi;            /* Like moduleapi but containing the APIs that\n                                   modules share with each other. */\n    list *loadmodule_queue;     /* List of modules to load at startup. */\n    pid_t child_pid;            /* PID of current child */\n    int child_type;             /* Type of current child */\n    client *module_client;      /* \"Fake\" client to call Redis from modules */\n    /* Networking */\n    int port;                   /* TCP listening port */\n    int tls_port;               /* TLS listening port */\n    int tcp_backlog;            /* TCP listen() backlog */\n    char *bindaddr[CONFIG_BINDADDR_MAX]; /* Addresses we should bind to */\n    int bindaddr_count;         /* Number of addresses in g_pserver->bindaddr[] */\n    char *unixsocket;           /* UNIX socket path */\n    mode_t unixsocketperm;      /* UNIX socket permission */\n    int sofd;                   /* Unix socket file descriptor */\n    socketFds cfd;              /* Cluster bus listening socket */\n    list *clients;              /* List of active clients */\n    list *clients_to_close;     /* Clients to close asynchronously */\n    list *slaves, *monitors;    /* List of slaves and MONITORs */\n    rax *clients_timeout_table; /* Radix tree for blocked clients timeouts. */\n    long fixed_time_expire;     /* If > 0, expire keys against server.mstime. */\n    rax *clients_index;         /* Active clients dictionary by client ID. */\n    list *paused_clients;       /* List of pause clients */\n    mstime_t client_pause_end_time; /* Time when we undo clients_paused */\n    ::dict *migrate_cached_sockets;/* MIGRATE cached sockets */\n    std::atomic<uint64_t> next_client_id; /* Next client unique ID. Incremental. */\n    int protected_mode;         /* Don't accept external connections. */\n    long long events_processed_while_blocked; /* processEventsWhileBlocked() */\n\n    /* RDB / AOF loading information */\n    std::atomic<int> loading; /* We are loading data from disk if true */\n    off_t loading_total_bytes;\n    off_t loading_rdb_used_mem;\n    off_t loading_loaded_bytes;\n    time_t loading_start_time;\n    unsigned long loading_process_events_interval_bytes;\n    unsigned int loading_process_events_interval_keys;\n\n    int active_expire_enabled;      /* Can be disabled for testing purposes. */\n    int active_expire_effort;       /* From 1 (default) to 10, active effort. */\n\n    int replicaIsolationFactor = 1;\n\n    /* Fields used only for stats */\n    long long stat_numcommands;     /* Number of processed commands */\n    long long stat_numconnections;  /* Number of connections received */\n    long long stat_expiredkeys;     /* Number of expired keys */\n    double stat_expired_stale_perc; /* Percentage of keys probably expired */\n    long long stat_expired_time_cap_reached_count; /* Early expire cylce stops.*/\n    long long stat_expire_cycle_time_used; /* Cumulative microseconds used. */\n    long long stat_evictedkeys;     /* Number of evicted keys (maxmemory) */\n    long long stat_keyspace_hits;   /* Number of successful lookups of keys */\n    long long stat_keyspace_misses; /* Number of failed lookups of keys */\n    long long stat_active_defrag_hits;      /* number of allocations moved */\n    long long stat_active_defrag_misses;    /* number of allocations scanned but not moved */\n    long long stat_active_defrag_key_hits;  /* number of keys with moved allocations */\n    long long stat_active_defrag_key_misses;/* number of keys scanned and not moved */\n    long long stat_active_defrag_scanned;   /* number of dictEntries scanned */\n    size_t stat_peak_memory;        /* Max used memory record */\n    long long stat_fork_time;       /* Time needed to perform latest fork() */\n    double stat_fork_rate;          /* Fork rate in GB/sec. */\n    long long stat_total_forks;     /* Total count of fork. */\n    long long stat_rejected_conn;   /* Clients rejected because of maxclients */\n    long long stat_sync_full;       /* Number of full resyncs with slaves. */\n    long long stat_sync_partial_ok; /* Number of accepted PSYNC requests. */\n    long long stat_sync_partial_err;/* Number of unaccepted PSYNC requests. */\n    list *slowlog;                  /* SLOWLOG list of commands */\n    long long slowlog_entry_id;     /* SLOWLOG current entry ID */\n    long long slowlog_log_slower_than; /* SLOWLOG time limit (to get logged) */\n    unsigned long slowlog_max_len;     /* SLOWLOG max number of items logged */\n    struct malloc_stats cron_malloc_stats; /* sampled in serverCron(). */\n    std::atomic<long long> stat_net_input_bytes; /* Bytes read from network. */\n    std::atomic<long long> stat_net_output_bytes; /* Bytes written to network. */\n    size_t stat_current_cow_bytes;  /* Copy on write bytes while child is active. */\n    monotime stat_current_cow_updated;  /* Last update time of stat_current_cow_bytes */\n    size_t stat_current_save_keys_processed;  /* Processed keys while child is active. */\n    size_t stat_current_save_keys_total;  /* Number of keys when child started. */\n    size_t stat_rdb_cow_bytes;      /* Copy on write bytes during RDB saving. */\n    size_t stat_aof_cow_bytes;      /* Copy on write bytes during AOF rewrite. */\n    size_t stat_module_cow_bytes;   /* Copy on write bytes during module fork. */\n    double stat_module_progress;   /* Module save progress. */\n    uint64_t stat_clients_type_memory[CLIENT_TYPE_COUNT];/* Mem usage by type */\n    long long stat_unexpected_error_replies; /* Number of unexpected (aof-loading, replica to master, etc.) error replies */\n    long long stat_dump_payload_sanitizations; /* Number deep dump payloads integrity validations. */\n    std::atomic<long long> stat_total_reads_processed; /* Total number of read events processed */\n    std::atomic<long long> stat_total_writes_processed; /* Total number of write events processed */\n    long long stat_storage_provider_read_hits;\n    long long stat_storage_provider_read_misses;\n    /* The following two are used to track instantaneous metrics, like\n     * number of operations per second, network traffic. */\n    struct {\n        long long last_sample_time; /* Timestamp of last sample in ms */\n        long long last_sample_count;/* Count in last sample */\n        long long samples[STATS_METRIC_SAMPLES];\n        int idx;\n    } inst_metric[STATS_METRIC_COUNT];\n    /* AOF persistence */\n    int aof_enabled;                /* AOF configuration */\n    int aof_state;                  /* AOF_(ON|OFF|WAIT_REWRITE) */\n    int aof_fsync;                  /* Kind of fsync() policy */\n    char *aof_filename;             /* Name of the AOF file */\n    int aof_no_fsync_on_rewrite;    /* Don't fsync if a rewrite is in prog. */\n    int aof_rewrite_perc;           /* Rewrite AOF if % growth is > M and... */\n    off_t aof_rewrite_min_size;     /* the AOF file is at least N bytes. */\n    off_t aof_rewrite_base_size;    /* AOF size on latest startup or rewrite. */\n    off_t aof_current_size;         /* AOF current size. */\n    off_t aof_fsync_offset;         /* AOF offset which is already synced to disk. */\n    int aof_flush_sleep;            /* Micros to sleep before flush. (used by tests) */\n    int aof_rewrite_scheduled;      /* Rewrite once BGSAVE terminates. */\n    list *aof_rewrite_buf_blocks;   /* Hold changes during an AOF rewrite. */\n    sds aof_buf;      /* AOF buffer, written before entering the event loop */\n    int aof_fd;       /* File descriptor of currently selected AOF file */\n    int aof_selected_db; /* Currently selected DB in AOF */\n    time_t aof_flush_postponed_start; /* UNIX time of postponed AOF flush */\n    time_t aof_last_fsync;            /* UNIX time of last fsync() */\n    time_t aof_rewrite_time_last;   /* Time used by last AOF rewrite run. */\n    time_t aof_rewrite_time_start;  /* Current AOF rewrite start time. */\n    int aof_lastbgrewrite_status;   /* C_OK or C_ERR */\n    unsigned long aof_delayed_fsync;  /* delayed AOF fsync() counter */\n    int aof_rewrite_incremental_fsync;/* fsync incrementally while aof rewriting? */\n    int rdb_save_incremental_fsync;   /* fsync incrementally while rdb saving? */\n    int aof_last_write_status;      /* C_OK or C_ERR */\n    int aof_last_write_errno;       /* Valid if aof write/fsync status is ERR */\n    int aof_load_truncated;         /* Don't stop on unexpected AOF EOF. */\n    int aof_use_rdb_preamble;       /* Use RDB preamble on AOF rewrites. */\n    redisAtomic int aof_bio_fsync_status; /* Status of AOF fsync in bio job. */\n    redisAtomic int aof_bio_fsync_errno;  /* Errno of AOF fsync in bio job. */\n    /* AOF pipes used to communicate between parent and child during rewrite. */\n    int aof_pipe_write_data_to_child;\n    int aof_pipe_read_data_from_parent;\n    int aof_pipe_write_ack_to_parent;\n    int aof_pipe_read_ack_from_child;\n    aeEventLoop *el_alf_pip_read_ack_from_child;\n    int aof_pipe_write_ack_to_child;\n    int aof_pipe_read_ack_from_parent;\n    int aof_stop_sending_diff;     /* If true stop sending accumulated diffs\n                                      to child process. */\n    sds aof_child_diff;             /* AOF diff accumulator child side. */\n    int aof_rewrite_pending = 0;    /* is a call to aofChildWriteDiffData already queued? */\n    /* RDB persistence */\n    int allowRdbResizeOp;           /* Debug situations we may want rehash to be ocurring, so ignore resize */\n    long long dirty;                /* Changes to DB from the last save */\n    long long dirty_before_bgsave;  /* Used to restore dirty on failed BGSAVE */\n    struct _rdbThreadVars\n    {\n        std::atomic<bool> fRdbThreadCancel {false};\n        std::atomic<bool> fDone {false};\n        int tmpfileNum = 0;\n        pthread_t rdb_child_thread;\n        int fRdbThreadActive = false;\n    } rdbThreadVars;\n    struct saveparam *saveparams;   /* Save points array for RDB */\n    int saveparamslen;              /* Number of saving points */\n    char *rdb_filename;             /* Name of RDB file */\n    char *rdb_s3bucketpath;         /* Path for AWS S3 backup of RDB file */\n    int rdb_compression;            /* Use compression in RDB? */\n    int rdb_checksum;               /* Use RDB checksum? */\n    int rdb_del_sync_files;         /* Remove RDB files used only for SYNC if\n                                       the instance does not use persistence. */\n    time_t lastsave;                /* Unix time of last successful save */\n    time_t lastbgsave_try;          /* Unix time of last attempted bgsave */\n    time_t rdb_save_time_last;      /* Time used by last RDB save run. */\n    time_t rdb_save_time_start;     /* Current RDB save start time. */\n    mstime_t rdb_save_latency;      /* Used to track end to end latency of rdb save*/\n    pid_t rdb_child_pid = -1;       /* Used only during fork bgsave */\n    int rdb_bgsave_scheduled;       /* BGSAVE when possible if true. */\n    int rdb_child_type;             /* Type of save by active child. */\n    int lastbgsave_status;          /* C_OK or C_ERR */\n    int stop_writes_on_bgsave_err;  /* Don't allow writes if can't BGSAVE */\n    int rdb_pipe_read;              /* RDB pipe used to transfer the rdb data */\n                                    /* to the parent process in diskless repl. */\n    int rdb_child_exit_pipe;        /* Used by the diskless parent allow child exit. */\n    connection **rdb_pipe_conns;    /* Connections which are currently the */\n    int rdb_pipe_numconns;          /* target of diskless rdb fork child. */\n    int rdb_pipe_numconns_writing;  /* Number of rdb conns with pending writes. */\n    char *rdb_pipe_buff;            /* In diskless replication, this buffer holds data */\n    int rdb_pipe_bufflen;           /* that was read from the the rdb pipe. */\n    int rdb_key_save_delay;         /* Delay in microseconds between keys while\n                                     * writing the RDB. (for testings). negative\n                                     * value means fractions of microsecons (on average). */\n    int key_load_delay;             /* Delay in microseconds between keys while\n                                     * loading aof or rdb. (for testings). negative\n                                     * value means fractions of microsecons (on average). */\n    /* Pipe and data structures for child -> parent info sharing. */\n    int child_info_pipe[2];         /* Pipe used to write the child_info_data. */\n    int child_info_nread;           /* Num of bytes of the last read from pipe */\n    /* Propagation of commands in AOF / replication */\n    redisOpArray also_propagate;    /* Additional command to propagate. */\n    int replication_allowed;        /* Are we allowed to replicate? */\n    /* Logging */\n    char *logfile;                  /* Path of log file */\n    int syslog_enabled;             /* Is syslog enabled? */\n    char *syslog_ident;             /* Syslog ident */\n    int syslog_facility;            /* Syslog facility */\n    int crashlog_enabled;           /* Enable signal handler for crashlog.\n                                     * disable for clean core dumps. */\n    int memcheck_enabled;           /* Enable memory check on crash. */\n    int use_exit_on_panic;          /* Use exit() on panic and assert rather than\n                                     * abort(). useful for Valgrind. */\n    /* Replication (master) */\n    char replid[CONFIG_RUN_ID_SIZE+1];  /* My current replication ID. */\n    char replid2[CONFIG_RUN_ID_SIZE+1]; /* replid inherited from master*/\n    long long master_repl_offset;   /* My current replication offset */\n    long long second_replid_offset; /* Accept offsets up to this for replid2. */\n    int replicaseldb;                 /* Last SELECTed DB in replication output */\n    int repl_ping_slave_period;     /* Master pings the replica every N seconds */\n    char *repl_backlog;             /* Replication backlog for partial syncs */\n    char *repl_backlog_disk = nullptr;\n    long long repl_backlog_size;    /* Backlog circular buffer size */\n    long long repl_backlog_config_size; /* The repl backlog may grow but we want to know what the user set it to */\n    long long repl_backlog_histlen; /* Backlog actual data length */\n    long long repl_backlog_idx;     /* Backlog circular buffer current offset,\n                                       that is the next byte will'll write to.*/\n    long long repl_backlog_off;     /* Replication \"master offset\" of first\n                                       byte in the replication backlog buffer.*/\n    long long repl_backlog_start;   /* Used to compute indicies from offsets\n                                       basically, index = (offset - start) % size */\n    fastlock repl_backlog_lock {\"replication backlog\"};\n    time_t repl_backlog_time_limit; /* Time without slaves after the backlog\n                                       gets released. */\n    time_t repl_no_slaves_since;    /* We have no slaves since that time.\n                                       Only valid if g_pserver->slaves len is 0. */\n    int repl_min_slaves_to_write;   /* Min number of slaves to write. */\n    int repl_min_slaves_max_lag;    /* Max lag of <count> slaves to write. */\n    int repl_good_slaves_count;     /* Number of slaves with lag <= max_lag. */\n    int repl_diskless_sync;         /* Master send RDB to slaves sockets directly. */\n    int repl_diskless_load;         /* Slave parse RDB directly from the socket.\n                                     * see REPL_DISKLESS_LOAD_* enum */\n    int repl_diskless_sync_delay;   /* Delay to start a diskless repl BGSAVE. */\n    std::atomic <long long> repl_lowest_off; /* The lowest offset amongst all replicas\n                                                -1 if there are no replicas */\n    /* Replication (replica) */\n    list *masters;\n    int enable_multimaster; \n    int repl_timeout;               /* Timeout after N seconds of master idle */\n    int repl_syncio_timeout; /* Timeout for synchronous I/O calls */\n    int repl_disable_tcp_nodelay;   /* Disable TCP_NODELAY after SYNC? */\n    int repl_serve_stale_data; /* Serve stale data when link is down? */\n    int repl_quorum;           /* For multimaster what do we consider a quorum? -1 means all master must be online */\n    int repl_slave_ro;          /* Slave is read only? */\n    int repl_slave_ignore_maxmemory;    /* If true slaves do not evict. */\n    int slave_priority;             /* Reported in INFO and used by Sentinel. */\n    int replica_announced;          /* If true, replica is announced by Sentinel */\n    int slave_announce_port;        /* Give the master this listening port. */\n    char *slave_announce_ip;        /* Give the master this ip address. */\n    int repl_slave_lazy_flush;          /* Lazy FLUSHALL before loading DB? */\n    /* Replication script cache. */\n    ::dict *repl_scriptcache_dict;        /* SHA1 all slaves are aware of. */\n    list *repl_scriptcache_fifo;        /* First in, first out LRU eviction. */\n    unsigned int repl_scriptcache_size; /* Max number of elements. */\n    /* Synchronous replication. */\n    list *clients_waiting_acks;         /* Clients waiting in WAIT command. */\n    int get_ack_from_slaves;            /* If true we send REPLCONF GETACK. */\n    /* Limits */\n    unsigned int maxclients;            /* Max number of simultaneous clients */\n    unsigned int maxclientsReserved;    /* Reserved amount for health checks (localhost conns) */\n    unsigned long long maxmemory;   /* Max number of memory bytes to use */\n    unsigned long long maxstorage;  /* Max number of bytes to use in a storage provider */\n    int maxmemory_policy;           /* Policy for key eviction */\n    int maxmemory_samples;          /* Precision of random sampling */\n    int maxmemory_eviction_tenacity;/* Aggressiveness of eviction processing */\n    int force_eviction_percent;     /* Force eviction when this percent of system memory is remaining */\n    int lfu_log_factor;             /* LFU logarithmic counter factor. */\n    int lfu_decay_time;             /* LFU counter decay factor. */\n    long long proto_max_bulk_len;   /* Protocol bulk length maximum size. */\n    int oom_score_adj_base;         /* Base oom_score_adj value, as observed on startup */\n    int oom_score_adj_values[CONFIG_OOM_COUNT];   /* Linux oom_score_adj configuration */\n    int oom_score_adj;                            /* If true, oom_score_adj is managed */\n    int disable_thp;                              /* If true, disable THP by syscall */\n    /* Blocked clients */\n    unsigned int blocked_clients;   /* # of clients executing a blocking cmd.*/\n    unsigned int blocked_clients_by_type[BLOCKED_NUM];\n    list *ready_keys;        /* List of readyList structures for BLPOP & co */\n    /* Client side caching. */\n    unsigned int tracking_clients;  /* # of clients with tracking enabled.*/\n    size_t tracking_table_max_keys; /* Max number of keys in tracking table. */\n    /* Sort parameters - qsort_r() is only available under BSD so we\n     * have to take this state global, in order to pass it to sortCompare() */\n    int sort_desc;\n    int sort_alpha;\n    int sort_bypattern;\n    int sort_store;\n    /* Zip structure config, see keydb.conf for more information  */\n    size_t hash_max_ziplist_entries;\n    size_t hash_max_ziplist_value;\n    size_t set_max_intset_entries;\n    size_t zset_max_ziplist_entries;\n    size_t zset_max_ziplist_value;\n    size_t hll_sparse_max_bytes;\n    size_t stream_node_max_bytes;\n    long long stream_node_max_entries;\n    /* List parameters */\n    int list_max_ziplist_size;\n    int list_compress_depth;\n    /* time cache */\n    std::atomic<time_t> unixtime;    /* Unix time sampled every cron cycle. */\n    time_t timezone;            /* Cached timezone. As set by tzset(). */\n    int daylight_active;        /* Currently in daylight saving time. */\n    mstime_t mstime;            /* 'unixtime' in milliseconds. */\n    ustime_t ustime;            /* 'unixtime' in microseconds. */\n    size_t blocking_op_nesting; /* Nesting level of blocking operation, used to reset blocked_last_cron. */\n    long long blocked_last_cron; /* Indicate the mstime of the last time we did cron jobs from a blocking operation */\n    /* Pubsub */\n    dict *pubsub_channels;  /* Map channels to list of subscribed clients */\n    dict *pubsub_patterns;  /* A dict of pubsub_patterns */\n    int notify_keyspace_events; /* Events to propagate via Pub/Sub. This is an\n                                   xor of NOTIFY_... flags. */\n    /* Cluster */\n    int cluster_enabled;      /* Is cluster enabled? */\n    mstime_t cluster_node_timeout; /* Cluster node timeout. */\n    char *cluster_configfile; /* Cluster auto-generated config file name. */\n    struct clusterState *cluster;  /* State of the cluster */\n    int cluster_migration_barrier; /* Cluster replicas migration barrier. */\n    int cluster_allow_replica_migration; /* Automatic replica migrations to orphaned masters and from empty masters */\n    int cluster_slave_validity_factor; /* Slave max data age for failover. */\n    int cluster_require_full_coverage; /* If true, put the cluster down if\n                                          there is at least an uncovered slot.*/\n    int cluster_slave_no_failover;  /* Prevent replica from starting a failover\n                                       if the master is in failure state. */\n    char *cluster_announce_ip;  /* IP address to announce on cluster bus. */\n    int cluster_announce_port;     /* base port to announce on cluster bus. */\n    int cluster_announce_tls_port; /* TLS port to announce on cluster bus. */\n    int cluster_announce_bus_port; /* bus port to announce on cluster bus. */\n    int cluster_module_flags;      /* Set of flags that Redis modules are able\n                                      to set in order to suppress certain\n                                      native Redis Cluster features. Check the\n                                      REDISMODULE_CLUSTER_FLAG_*. */\n    int cluster_allow_reads_when_down; /* Are reads allowed when the cluster\n                                        is down? */\n    int cluster_config_file_lock_fd;   /* cluster config fd, will be flock */\n    /* Scripting */\n    lua_State *lua; /* The Lua interpreter. We use just one for all clients */\n    client *lua_caller = nullptr;   /* The client running EVAL right now, or NULL */\n    char* lua_cur_script = nullptr; /* SHA1 of the script currently running, or NULL */\n    ::dict *lua_scripts;         /* A dictionary of SHA1 -> Lua scripts */\n    unsigned long long lua_scripts_mem;  /* Cached scripts' memory + oh */\n    mstime_t lua_time_limit;  /* Script timeout in milliseconds */\n    monotime lua_time_start;  /* monotonic timer to detect timed-out script */\n    mstime_t lua_time_snapshot; /* Snapshot of mstime when script is started */\n    int lua_write_dirty;  /* True if a write command was called during the\n                             execution of the current script. */\n    int lua_random_dirty; /* True if a random command was called during the\n                             execution of the current script. */\n    int lua_replicate_commands; /* True if we are doing single commands repl. */\n    int lua_multi_emitted;/* True if we already propagated MULTI. */\n    int lua_repl;         /* Script replication flags for redis.set_repl(). */\n    int lua_timedout;     /* True if we reached the time limit for script\n                             execution. */\n    int lua_kill;         /* Kill the script if true. */\n    int lua_always_replicate_commands; /* Default replication type. */\n    int lua_oom;          /* OOM detected when script start? */\n    /* Lazy free */\n    int lazyfree_lazy_eviction;\n    int lazyfree_lazy_expire;\n    int lazyfree_lazy_server_del;\n    int lazyfree_lazy_user_del;\n    int lazyfree_lazy_user_flush;\n    /* Latency monitor */\n    long long latency_monitor_threshold;\n    ::dict *latency_events;\n    /* ACLs */\n    char *acl_filename;           /* ACL Users file. NULL if not configured. */\n    unsigned long acllog_max_len; /* Maximum length of the ACL LOG list. */\n    sds requirepass;              /* Remember the cleartext password set with\n                                     the old \"requirepass\" directive for\n                                     backward compatibility with Redis <= 5. */\n    int acl_pubsub_default;      /* Default ACL pub/sub channels flag */\n    /* Assert & bug reporting */\n    int watchdog_period;  /* Software watchdog period in ms. 0 = off */\n\n    int fActiveReplica;                          /* Can this replica also be a master? */\n    int fWriteDuringActiveLoad;                  /* Can this active-replica write during an RDB load? */\n    int fEnableFastSync = false;\n\n    // Format:\n    //  Lower 20 bits: a counter incrementing for each command executed in the same millisecond\n    //  Upper 44 bits: mstime (least significant 44-bits) enough for ~500 years before rollover from date of addition\n    uint64_t mvcc_tstamp;\n\n    AsyncWorkQueue *asyncworkqueue;\n\n    /* System hardware info */\n    size_t system_memory_size;  /* Total memory in system as reported by OS */\n\n    GarbageCollectorCollection garbageCollector;\n\n    IStorageFactory *m_pstorageFactory = nullptr;\n    int storage_flush_period;   // The time between flushes in the CRON job\n\n    long long snapshot_slip = 500;   // The amount of time in milliseconds we let a snapshot be behind the current database\n\n    /* TLS Configuration */\n    int tls_cluster;\n    int tls_replication;\n    int tls_auth_clients;\n    int tls_rotation;\n\n    std::set<sdsstring> tls_auditlog_blocklist; /* Certificates that can be excluded from audit logging */\n    std::set<sdsstring> tls_allowlist;\n    redisTLSContextConfig tls_ctx_config;\n\n    /* cpu affinity */\n    char *server_cpulist; /* cpu affinity list of redis server main/io thread. */\n    char *bio_cpulist; /* cpu affinity list of bio thread. */\n    char *aof_rewrite_cpulist; /* cpu affinity list of aof rewrite process. */\n    char *bgsave_cpulist; /* cpu affinity list of bgsave process. */\n\n    int prefetch_enabled = 1;\n    /* Sentinel config */\n    struct sentinelConfig *sentinel_config; /* sentinel config to load at startup time. */\n    /* Coordinate failover info */\n    mstime_t failover_end_time; /* Deadline for failover command. */\n    int force_failover; /* If true then failover will be foreced at the\n                         * deadline, otherwise failover is aborted. */\n    char *target_replica_host; /* Failover target host. If null during a\n                                * failover then any replica can be used. */\n    int target_replica_port; /* Failover target port */\n    int failover_state; /* Failover state */\n\n    int enable_async_commands;\n    int multithread_load_enabled = 0;\n    int active_client_balancing = 1;\n\n    long long repl_batch_offStart = -1;\n    long long repl_batch_idxStart = -1;\n\n    long long rand_total_threshold;\n\n    int config_soft_shutdown = false;\n    bool soft_shutdown = false;\n\n    int flash_disable_key_cache = false;\n\n    /* Lock Contention Ring Buffer */\n    static const size_t s_lockContentionSamples = 64;\n    uint16_t rglockSamples[s_lockContentionSamples];\n    unsigned ilockRingHead = 0;\n\n\n    sds sdsAvailabilityZone;\n    int overload_protect_threshold = 0;\n    int is_overloaded = 0;\n    int overload_closed_clients = 0;\n\n        int module_blocked_pipe[2]; /* Pipe used to awake the event loop if a\n                            client blocked on a module command needs\n                            to be processed. */\n\n    bool FRdbSaveInProgress() const { return g_pserver->rdbThreadVars.fRdbThreadActive; }\n};\n\ninline int redisServerThreadVars::getRdbKeySaveDelay() {\n    if (rdb_key_save_delay < 0) {\n        __atomic_load(&g_pserver->rdb_key_save_delay, &rdb_key_save_delay, __ATOMIC_ACQUIRE);\n    }\n    return rdb_key_save_delay;\n}\n\n\n#define MAX_KEYS_BUFFER 256\n\n/* A result structure for the various getkeys function calls. It lists the\n * keys as indices to the provided argv.\n */\ntypedef struct {\n    int keysbuf[MAX_KEYS_BUFFER];       /* Pre-allocated buffer, to save heap allocations */\n    int *keys;                          /* Key indices array, points to keysbuf or heap */\n    int numkeys;                        /* Number of key indices return */\n    int size;                           /* Available array size */\n} getKeysResult;\n#define GETKEYS_RESULT_INIT { {0}, NULL, 0, MAX_KEYS_BUFFER }\n\ntypedef void redisCommandProc(client *c);\ntypedef int redisGetKeysProc(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);\nstruct redisCommand {\n    const char *name;\n    redisCommandProc *proc;\n    int arity;\n    const char *sflags;   /* Flags as string representation, one char per flag. */\n    uint64_t flags; /* The actual flags, obtained from the 'sflags' field. */\n    /* Use a function to determine keys arguments in a command line.\n     * Used for Redis Cluster redirect. */\n    redisGetKeysProc *getkeys_proc;\n    /* What keys should be loaded in background when calling this command? */\n    int firstkey; /* The first argument that's a key (0 = no keys) */\n    int lastkey;  /* The last argument that's a key */\n    int keystep;  /* The step between first and last key */\n    long long microseconds, calls, rejected_calls, failed_calls;\n    int id;     /* Command ID. This is a progressive ID starting from 0 that\n                   is assigned at runtime, and is used in order to check\n                   ACLs. A connection is able to execute a given command if\n                   the user associated to the connection has this command\n                   bit set in the bitmap of allowed commands. */\n};\n\nstruct redisError {\n    long long count;\n};\n\nstruct redisFunctionSym {\n    char *name;\n    unsigned long pointer;\n};\n\ntypedef struct _redisSortObject {\n    robj *obj;\n    union {\n        double score;\n        robj *cmpobj;\n    } u;\n} redisSortObject;\n\ntypedef struct _redisSortOperation {\n    int type;\n    robj *pattern;\n} redisSortOperation;\n\n/* Structure to hold list iteration abstraction. */\ntypedef struct {\n    robj_roptr subject;\n    unsigned char encoding;\n    unsigned char direction; /* Iteration direction */\n    quicklistIter *iter;\n} listTypeIterator;\n\n/* Structure for an entry while iterating over a list. */\ntypedef struct {\n    listTypeIterator *li;\n    quicklistEntry entry; /* Entry in quicklist */\n} listTypeEntry;\n\n/* Structure to hold set iteration abstraction. */\ntypedef struct {\n    robj_roptr subject;\n    int encoding;\n    int ii; /* intset iterator */\n    dictIterator *di;\n} setTypeIterator;\n\n/* Structure to hold hash iteration abstraction. Note that iteration over\n * hashes involves both fields and values. Because it is possible that\n * not both are required, store pointers in the iterator to avoid\n * unnecessary memory allocation for fields/values. */\ntypedef struct {\n    robj_roptr subject;\n    int encoding;\n\n    unsigned char *fptr, *vptr;\n\n    dictIterator *di;\n    dictEntry *de;\n} hashTypeIterator;\n\n#include \"stream.h\"  /* Stream data type header file. */\n\n#define OBJ_HASH_KEY 1\n#define OBJ_HASH_VALUE 2\n\n/* Used in evict.cpp */\nenum class EvictReason {\n    User,       /* User memory exceeded limit */\n    System      /* System memory exceeded limit */\n};\n\n/*-----------------------------------------------------------------------------\n * Extern declarations\n *----------------------------------------------------------------------------*/\n\n//extern struct redisServer server;\nextern struct redisServerConst cserver;\nextern thread_local struct redisServerThreadVars *serverTL;   // thread local server vars\nextern struct sharedObjectsStruct shared;\nextern dictType objectKeyPointerValueDictType;\nextern dictType objectKeyHeapPointerValueDictType;\nextern dictType setDictType;\nextern dictType zsetDictType;\nextern dictType clusterNodesDictType;\nextern dictType clusterNodesBlackListDictType;\nextern dictType dbDictType;\nextern dictType dbTombstoneDictType;\nextern dictType dbSnapshotDictType;\nextern dictType shaScriptObjectDictType;\nextern double R_Zero, R_PosInf, R_NegInf, R_Nan;\nextern dictType hashDictType;\nextern dictType keylistDictType;\nextern dictType replScriptCacheDictType;\nextern dictType dbExpiresDictType;\nextern dictType modulesDictType;\nextern dictType sdsReplyDictType;\nextern fastlock g_lockasyncfree;\n\n/*-----------------------------------------------------------------------------\n * Functions prototypes\n *----------------------------------------------------------------------------*/\n\n/* Modules */\nvoid moduleInitModulesSystem(void);\nvoid moduleInitModulesSystemLast(void);\nint moduleLoad(const char *path, void **argv, int argc);\nvoid moduleLoadFromQueue(void);\nint moduleGetCommandKeysViaAPI(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);\nmoduleType *moduleTypeLookupModuleByID(uint64_t id);\nvoid moduleTypeNameByID(char *name, uint64_t moduleid);\nconst char *moduleTypeModuleName(moduleType *mt);\nvoid moduleFreeContext(struct RedisModuleCtx *ctx, bool propogate = true);\nvoid unblockClientFromModule(client *c);\nvoid moduleHandleBlockedClients(int iel);\nvoid moduleBlockedClientTimedOut(client *c);\nvoid moduleBlockedClientPipeReadable(aeEventLoop *el, int fd, void *privdata, int mask);\nsize_t moduleCount(void);\nvoid moduleAcquireGIL(int fServerThread, int fExclusive = FALSE);\nint moduleTryAcquireGIL(bool fServerThread, int fExclusive = FALSE);\nvoid moduleReleaseGIL(int fServerThread, int fExclusive = FALSE);\nvoid moduleNotifyKeyspaceEvent(int type, const char *event, robj *key, int dbid);\nvoid moduleCallCommandFilters(client *c);\nint moduleHasCommandFilters();\nvoid ModuleForkDoneHandler(int exitcode, int bysignal);\nint TerminateModuleForkChild(int child_pid, int wait);\nssize_t rdbSaveModulesAux(rio *rdb, int when);\nint moduleAllDatatypesHandleErrors();\nsds modulesCollectInfo(sds info, const char *section, int for_crash_report, int sections);\nvoid moduleFireServerEvent(uint64_t eid, int subid, void *data);\nvoid processModuleLoadingProgressEvent(int is_aof);\nint moduleTryServeClientBlockedOnKey(client *c, robj *key);\nvoid moduleUnblockClient(client *c);\nint moduleBlockedClientMayTimeout(client *c);\nint moduleClientIsBlockedOnKeys(client *c);\nvoid moduleNotifyUserChanged(client *c);\nvoid moduleNotifyKeyUnlink(robj *key, robj *val);\nrobj *moduleTypeDupOrReply(client *c, robj *fromkey, robj *tokey, robj *value);\nint moduleDefragValue(robj *key, robj *obj, long *defragged);\nint moduleLateDefrag(robj *key, robj *value, unsigned long *cursor, long long endtime, long long *defragged);\nlong moduleDefragGlobals(void);\n\n/* Utils */\nlong long ustime(void);\nlong long mstime(void);\nextern \"C\" void getRandomHexChars(char *p, size_t len);\nextern \"C\" void getRandomBytes(unsigned char *p, size_t len);\nuint64_t crc64(uint64_t crc, const unsigned char *s, uint64_t l);\nvoid exitFromChild(int retcode);\nlong long redisPopcount(const void *s, long count);\nint redisSetProcTitle(const char *title);\nint validateProcTitleTemplate(const char *_template);\nint redisCommunicateSystemd(const char *sd_notify_msg);\nvoid redisSetCpuAffinity(const char *cpulist);\nvoid saveMasterStatusToStorage(bool fShutdown);\n\n/* networking.c -- Networking and Client related operations */\nclient *createClient(connection *conn, int iel);\nvoid closeTimedoutClients(void);\nbool freeClient(client *c);\nvoid freeClientAsync(client *c);\nvoid resetClient(client *c);\nvoid freeClientOriginalArgv(client *c);\nvoid sendReplyToClient(connection *conn);\nvoid *addReplyDeferredLen(client *c);\nvoid setDeferredArrayLen(client *c, void *node, long length);\nvoid setDeferredMapLen(client *c, void *node, long length);\nvoid setDeferredSetLen(client *c, void *node, long length);\nvoid setDeferredAttributeLen(client *c, void *node, long length);\nvoid setDeferredPushLen(client *c, void *node, long length);\nvoid processInputBuffer(client *c, bool fParse, int callFlags);\nvoid acceptHandler(aeEventLoop *el, int fd, void *privdata, int mask);\nvoid acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask);\nvoid acceptTLSHandler(aeEventLoop *el, int fd, void *privdata, int mask);\nvoid acceptUnixHandler(aeEventLoop *el, int fd, void *privdata, int mask);\nvoid readQueryFromClient(connection *conn);\nvoid addReplyNull(client *c);\nvoid addReplyNullArray(client *c);\nvoid addReplyBool(client *c, int b);\nvoid addReplyVerbatim(client *c, const char *s, size_t len, const char *ext);\nvoid addReplyProto(client *c, const char *s, size_t len);\nvoid addReplyProtoCString(client *c, const char *s);\nvoid addReplyBulk(client *c, robj_roptr obj);\nvoid AddReplyFromClient(client *c, client *src);\nvoid addReplyBulkCString(client *c, const char *s);\nvoid addReplyBulkCBuffer(client *c, const void *p, size_t len);\nvoid addReplyBulkLongLong(client *c, long long ll);\nvoid addReply(client *c, robj_roptr obj);\nvoid addReplySds(client *c, sds s);\nvoid addReplyBulkSds(client *c, sds s);\nvoid setDeferredReplyBulkSds(client *c, void *node, sds s);\nvoid addReplyErrorObject(client *c, robj *err, int severity = 0);\nvoid addReplyErrorSds(client *c, sds err);\nvoid addReplyError(client *c, const char *err);\nvoid addReplyStatus(client *c, const char *status);\nvoid addReplyDouble(client *c, double d);\nvoid addReplyBigNum(client *c, const char* num, size_t len);\nvoid addReplyHumanLongDouble(client *c, long double d);\nvoid addReplyLongLong(client *c, long long ll);\n#ifdef __cplusplus\nvoid addReplyLongLongWithPrefix(client *c, long long ll, char prefix);\n#endif\nvoid addReplyArrayLen(client *c, long length);\nvoid addReplyMapLen(client *c, long length);\nvoid addReplySetLen(client *c, long length);\nvoid addReplyAttributeLen(client *c, long length);\nvoid addReplyPushLen(client *c, long length);\nvoid addReplyHelp(client *c, const char **help);\nvoid addReplySubcommandSyntaxError(client *c);\nvoid addReplyLoadedModules(client *c);\nvoid copyClientOutputBuffer(client *dst, client *src);\nsize_t sdsZmallocSize(sds s);\nsize_t getStringObjectSdsUsedMemory(robj *o);\nvoid freeClientReplyValue(const void *o);\nvoid *dupClientReplyValue(void *o);\nvoid getClientsMaxBuffers(unsigned long *longest_output_list,\n                          unsigned long *biggest_input_buffer);\nchar *getClientPeerId(client *client);\nchar *getClientSockName(client *client);\nsds catClientInfoString(sds s, client *client);\nsds getAllClientsInfoString(int type);\nvoid rewriteClientCommandVector(client *c, int argc, ...);\nvoid rewriteClientCommandArgument(client *c, int i, robj *newval);\nvoid replaceClientCommandVector(client *c, int argc, robj **argv);\nvoid redactClientCommandArgument(client *c, int argc);\nunsigned long getClientOutputBufferMemoryUsage(client *c);\nint freeClientsInAsyncFreeQueue(int iel);\nint closeClientOnOutputBufferLimitReached(client *c, int async);\nint getClientType(client *c);\nint getClientTypeByName(const char *name);\nconst char *getClientTypeName(int cclass);\nvoid flushSlavesOutputBuffers(void);\nvoid disconnectSlaves(void);\nvoid disconnectSlavesExcept(unsigned char *uuid);\nint listenToPort(int port, socketFds *fds, int fReusePort, int fFirstListen);\nvoid pauseClients(mstime_t duration, pause_type type);\nvoid unpauseClients(void);\nint areClientsPaused(void);\nint checkClientPauseTimeoutAndReturnIfPaused(void);\nvoid processEventsWhileBlocked(int iel);\nvoid loadingCron(void);\nvoid whileBlockedCron();\nvoid blockingOperationStarts();\nvoid blockingOperationEnds();\nint handleClientsWithPendingWrites(int iel, int aof_state);\nint clientHasPendingReplies(client *c);\nvoid unlinkClient(client *c);\nint writeToClient(client *c, int handler_installed);\nvoid linkClient(client *c);\nvoid protectClient(client *c);\nvoid unprotectClient(client *c);\n\nvoid ProcessPendingAsyncWrites(void);\nclient *lookupClientByID(uint64_t id);\nint authRequired(client *c);\n\n#ifdef __GNUC__\nvoid addReplyErrorFormat(client *c, const char *fmt, ...)\n    __attribute__((format(printf, 2, 3)));\nvoid addReplyStatusFormat(client *c, const char *fmt, ...)\n    __attribute__((format(printf, 2, 3)));\n#else\nvoid addReplyErrorFormat(client *c, const char *fmt, ...);\nvoid addReplyStatusFormat(client *c, const char *fmt, ...);\n#endif\n\n/* Client side caching (tracking mode) */\nvoid enableTracking(client *c, uint64_t redirect_to, uint64_t options, robj **prefix, size_t numprefix);\nvoid disableTracking(client *c);\nvoid trackingRememberKeys(client *c);\nvoid trackingInvalidateKey(client *c, robj *keyobj);\nvoid trackingInvalidateKeysOnFlush(int async);\nvoid freeTrackingRadixTree(rax *rt);\nvoid freeTrackingRadixTreeAsync(rax *rt);\nvoid trackingLimitUsedSlots(void);\nuint64_t trackingGetTotalItems(void);\nuint64_t trackingGetTotalKeys(void);\nuint64_t trackingGetTotalPrefixes(void);\nvoid trackingBroadcastInvalidationMessages(void);\nint checkPrefixCollisionsOrReply(client *c, robj **prefix, size_t numprefix);\n\n/* List data type */\nvoid listTypeTryConversion(robj *subject, robj *value);\nvoid listTypePush(robj *subject, robj *value, int where);\nrobj *listTypePop(robj *subject, int where);\nunsigned long listTypeLength(robj_roptr subject);\nlistTypeIterator *listTypeInitIterator(robj_roptr subject, long index, unsigned char direction);\nvoid listTypeReleaseIterator(listTypeIterator *li);\nint listTypeNext(listTypeIterator *li, listTypeEntry *entry);\nrobj *listTypeGet(listTypeEntry *entry);\nvoid listTypeInsert(listTypeEntry *entry, robj *value, int where);\nint listTypeEqual(listTypeEntry *entry, robj *o);\nvoid listTypeDelete(listTypeIterator *iter, listTypeEntry *entry);\nvoid listTypeConvert(robj *subject, int enc);\nrobj *listTypeDup(robj *o);\nvoid unblockClientWaitingData(client *c);\nvoid popGenericCommand(client *c, int where);\nvoid listElementsRemoved(client *c, robj *key, int where, robj *o, long count);\n\n/* MULTI/EXEC/WATCH... */\nvoid unwatchAllKeys(client *c);\nvoid initClientMultiState(client *c);\nvoid freeClientMultiState(client *c);\nvoid queueMultiCommand(client *c);\nvoid touchWatchedKey(redisDb *db, robj *key);\nint isWatchedKeyExpired(client *c);\nvoid touchAllWatchedKeysInDb(redisDb *emptied, redisDb *replaced_with);\nvoid updateDBWatchedKey(int dbid, client *c);\nvoid discardTransaction(client *c);\nvoid flagTransaction(client *c);\nvoid execCommandAbort(client *c, sds error);\nvoid execCommandPropagateMulti(int dbid);\nvoid execCommandPropagateExec(int dbid);\nvoid beforePropagateMulti();\nvoid afterPropagateExec();\n\n/* Redis object implementation */\nvoid decrRefCount(robj_roptr o);\nvoid decrRefCountVoid(const void *o);\nvoid incrRefCount(robj_roptr o);\nrobj *makeObjectShared(robj *o);\nrobj *makeObjectShared(const char *rgch, size_t cch);\nrobj *resetRefCount(robj *obj);\nvoid freeStringObject(robj *o);\nvoid freeListObject(robj *o);\nvoid freeSetObject(robj *o);\nvoid freeZsetObject(robj *o);\nvoid freeHashObject(robj *o);\nrobj *createObject(int type, void *ptr);\nrobj *createStringObject(const char *ptr, size_t len);\nrobj *createRawStringObject(const char *ptr, size_t len);\nrobj *createEmbeddedStringObject(const char *ptr, size_t len);\nrobj *tryCreateRawStringObject(const char *ptr, size_t len);\nrobj *tryCreateStringObject(const char *ptr, size_t len);\nrobj *dupStringObject(const robj *o);\nint isSdsRepresentableAsLongLong(const char *s, long long *llval);\nint isObjectRepresentableAsLongLong(robj *o, long long *llongval);\nrobj *tryObjectEncoding(robj *o);\nrobj *getDecodedObject(robj *o);\nrobj_roptr getDecodedObject(robj_roptr o);\nsize_t stringObjectLen(robj_roptr o);\nrobj *createStringObjectFromLongLong(long long value);\nrobj *createStringObjectFromLongLongForValue(long long value);\nrobj *createStringObjectFromLongDouble(long double value, int humanfriendly);\nrobj *createQuicklistObject(void);\nrobj *createZiplistObject(void);\nrobj *createSetObject(void);\nrobj *createIntsetObject(void);\nrobj *createHashObject(void);\nrobj *createZsetObject(void);\nrobj *createZsetZiplistObject(void);\nrobj *createStreamObject(void);\nrobj *createModuleObject(moduleType *mt, void *value);\nint getLongFromObjectOrReply(client *c, robj *o, long *target, const char *msg);\nint getUnsignedLongLongFromObjectOrReply(client *c, robj *o, uint64_t *target, const char *msg);\nint getPositiveLongFromObjectOrReply(client *c, robj *o, long *target, const char *msg);\nint getRangeLongFromObjectOrReply(client *c, robj *o, long min, long max, long *target, const char *msg);\nint checkType(client *c, robj_roptr o, int type);\nint getLongLongFromObjectOrReply(client *c, robj *o, long long *target, const char *msg);\nint getDoubleFromObjectOrReply(client *c, robj *o, double *target, const char *msg);\nint getDoubleFromObject(const robj *o, double *target);\nint getLongLongFromObject(robj *o, long long *target);\nint getUnsignedLongLongFromObject(robj *o, uint64_t *target);\nint getLongDoubleFromObject(robj *o, long double *target);\nint getLongDoubleFromObjectOrReply(client *c, robj *o, long double *target, const char *msg);\nint getIntFromObjectOrReply(client *c, robj *o, int *target, const char *msg);\nconst char *strEncoding(int encoding);\nint compareStringObjects(robj *a, robj *b);\nint collateStringObjects(robj *a, robj *b);\nint equalStringObjects(robj *a, robj *b);\nunsigned long long estimateObjectIdleTime(robj_roptr o);\nvoid trimStringObjectIfNeeded(robj *o);\n\nrobj *deserializeStoredObject(const void *data, size_t cb);\nstd::unique_ptr<expireEntry> deserializeExpire(const char *str, size_t cch, size_t *poffset);\nsds serializeStoredObject(robj_roptr o, sds sdsPrefix = nullptr);\nsds serializeStoredObjectAndExpire(robj_roptr o);\n\n#define sdsEncodedObject(objptr) (objptr->encoding == OBJ_ENCODING_RAW || objptr->encoding == OBJ_ENCODING_EMBSTR)\n\n/* Synchronous I/O with timeout */\nssize_t syncWrite(int fd, const char *ptr, ssize_t size, long long timeout);\nssize_t syncRead(int fd, char *ptr, ssize_t size, long long timeout);\nssize_t syncReadLine(int fd, char *ptr, ssize_t size, long long timeout);\n\n/* Replication */\nvoid initMasterInfo(struct redisMaster *master);\nvoid replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc);\nvoid replicationFeedSlavesFromMasterStream(char *buf, size_t buflen);\nvoid replicationFeedMonitors(client *c, list *monitors, int dictid, robj **argv, int argc);\nvoid updateSlavesWaitingBgsave(int bgsaveerr, int type);\nvoid replicationCron(void);\nvoid replicationStartPendingFork(void);\nvoid replicationHandleMasterDisconnection(struct redisMaster *mi);\nvoid replicationCacheMaster(struct redisMaster *mi, client *c);\nvoid replicationCreateCachedMasterClone(redisMaster *mi);\nvoid resizeReplicationBacklog(long long newsize);\nstruct redisMaster *replicationAddMaster(char *ip, int port);\nvoid replicationUnsetMaster(struct redisMaster *mi);\nvoid refreshGoodSlavesCount(void);\nvoid replicationScriptCacheInit(void);\nvoid replicationScriptCacheFlush(void);\nvoid replicationScriptCacheAdd(sds sha1);\nint replicationScriptCacheExists(sds sha1);\nvoid processClientsWaitingReplicas(void);\nvoid unblockClientWaitingReplicas(client *c);\nint replicationCountAcksByOffset(long long offset);\nvoid replicationSendNewlineToMaster(struct redisMaster *mi);\nlong long replicationGetSlaveOffset(struct redisMaster *mi);\nchar *replicationGetSlaveName(client *c);\nlong long getPsyncInitialOffset(void);\nint replicationSetupSlaveForFullResync(client *replica, long long offset);\nvoid changeReplicationId(void);\nvoid clearReplicationId2(void);\nvoid chopReplicationBacklog(void);\nvoid replicationCacheMasterUsingMyself(struct redisMaster *mi);\nvoid replicationCacheMasterUsingMaster(struct redisMaster *mi);\nvoid feedReplicationBacklog(const void *ptr, size_t len);\nvoid updateMasterAuth();\nvoid showLatestBacklog();\nvoid rdbPipeReadHandler(struct aeEventLoop *eventLoop, int fd, void *clientData, int mask);\nvoid rdbPipeWriteHandlerConnRemoved(struct connection *conn);\nvoid replicationNotifyLoadedKey(redisDb *db, robj_roptr key, robj_roptr val, long long expire);\nvoid replicateSubkeyExpire(redisDb *db, robj_roptr key, robj_roptr subkey, long long expire);\nvoid clearFailoverState(void);\nvoid updateFailoverStatus(void);\nvoid abortFailover(redisMaster *mi, const char *err);\nconst char *getFailoverStateString();\nint canFeedReplicaReplBuffer(client *replica);\nvoid trimReplicationBacklog();\n\n/* Generic persistence functions */\nvoid startLoadingFile(FILE* fp, const char * filename, int rdbflags);\nvoid startLoading(size_t size, int rdbflags);\nvoid loadingProgress(off_t pos);\nvoid stopLoading(int success);\nvoid startSaving(int rdbflags);\nvoid stopSaving(int success);\nint allPersistenceDisabled(void);\n\n#define DISK_ERROR_TYPE_AOF 1       /* Don't accept writes: AOF errors. */\n#define DISK_ERROR_TYPE_RDB 2       /* Don't accept writes: RDB errors. */\n#define DISK_ERROR_TYPE_NONE 0      /* No problems, we can accept writes. */\nint writeCommandsDeniedByDiskError(void);\n\n/* RDB persistence */\n#include \"rdb.h\"\nvoid killRDBChild(bool fSynchronous = false);\nint bg_unlink(const char *filename);\n\n\n/* AOF persistence */\nvoid flushAppendOnlyFile(int force);\nvoid feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv, int argc);\nvoid aofRemoveTempFile(pid_t childpid);\nint rewriteAppendOnlyFileBackground(void);\nint loadAppendOnlyFile(char *filename);\nvoid stopAppendOnly(void);\nint startAppendOnly(void);\nvoid backgroundRewriteDoneHandler(int exitcode, int bysignal);\nvoid aofRewriteBufferReset(void);\nunsigned long aofRewriteBufferSize(void);\nssize_t aofReadDiffFromParent(void);\nvoid killAppendOnlyChild(void);\nvoid restartAOFAfterSYNC();\n\n/* Child info */\nvoid openChildInfoPipe(void);\nvoid closeChildInfoPipe(void);\nvoid sendChildInfoGeneric(childInfoType info_type, size_t keys, double progress, const char *pname);\nvoid sendChildCowInfo(childInfoType info_type, const char *pname);\nvoid sendChildInfo(childInfoType info_type, size_t keys, const char *pname);\nvoid receiveChildInfo(void);\n\n/* Fork helpers */\nvoid executeWithoutGlobalLock(std::function<void()> func);\nint redisFork(int type);\nint hasActiveChildProcess();\nint hasActiveChildProcessOrBGSave();\nvoid resetChildState();\nint isMutuallyExclusiveChildType(int type);\n\n/* acl.c -- Authentication related prototypes. */\nextern rax *Users;\nextern user *DefaultUser;\nvoid ACLInit(void);\n/* Return values for ACLCheckAllPerm(). */\n#define ACL_OK 0\n#define ACL_DENIED_CMD 1\n#define ACL_DENIED_KEY 2\n#define ACL_DENIED_AUTH 3 /* Only used for ACL LOG entries. */\n#define ACL_DENIED_CHANNEL 4 /* Only used for pub/sub commands */\nint ACLCheckUserCredentials(robj *username, robj *password);\nint ACLAuthenticateUser(client *c, robj *username, robj *password);\nunsigned long ACLGetCommandID(const char *cmdname);\nvoid ACLClearCommandID(void);\nuser *ACLGetUserByName(const char *name, size_t namelen);\nint ACLCheckAllPerm(client *c, int *idxptr);\nint ACLSetUser(user *u, const char *op, ssize_t oplen);\nsds ACLDefaultUserFirstPassword(void);\nuint64_t ACLGetCommandCategoryFlagByName(const char *name);\nint ACLAppendUserForLoading(sds *argv, int argc, int *argc_err);\nconst char *ACLSetUserStringError(void);\nint ACLLoadConfiguredUsers(void);\nsds ACLDescribeUser(user *u);\nvoid ACLLoadUsersAtStartup(void);\nvoid addReplyCommandCategories(client *c, struct redisCommand *cmd);\nuser *ACLCreateUnlinkedUser();\nvoid ACLFreeUserAndKillClients(user *u);\nvoid addACLLogEntry(client *c, int reason, int keypos, sds username);\nvoid ACLUpdateDefaultUserPassword(sds password);\n\n/* Sorted sets data type */\n\n/* Input flags. */\n#define ZADD_IN_NONE 0\n#define ZADD_IN_INCR (1<<0)    /* Increment the score instead of setting it. */\n#define ZADD_IN_NX (1<<1)      /* Don't touch elements not already existing. */\n#define ZADD_IN_XX (1<<2)      /* Only touch elements already existing. */\n#define ZADD_IN_GT (1<<3)      /* Only update existing when new scores are higher. */\n#define ZADD_IN_LT (1<<4)      /* Only update existing when new scores are lower. */\n\n/* Output flags. */\n#define ZADD_OUT_NOP (1<<0)     /* Operation not performed because of conditionals.*/\n#define ZADD_OUT_NAN (1<<1)     /* Only touch elements already existing. */\n#define ZADD_OUT_ADDED (1<<2)   /* The element was new and was added. */\n#define ZADD_OUT_UPDATED (1<<3) /* The element already existed, score updated. */\n\n/* Struct to hold an inclusive/exclusive range spec by score comparison. */\ntypedef struct {\n    double min, max;\n    int minex, maxex; /* are min or max exclusive? */\n} zrangespec;\n\n/* Struct to hold an inclusive/exclusive range spec by lexicographic comparison. */\ntypedef struct {\n    sds min, max;     /* May be set to shared.(minstring|maxstring) */\n    int minex, maxex; /* are min or max exclusive? */\n} zlexrangespec;\n\nzskiplist *zslCreate(void);\nvoid zslFree(zskiplist *zsl);\nzskiplistNode *zslInsert(zskiplist *zsl, double score, sds ele);\nunsigned char *zzlInsert(unsigned char *zl, sds ele, double score);\nint zslDelete(zskiplist *zsl, double score, sds ele, zskiplistNode **node);\nzskiplistNode *zslFirstInRange(zskiplist *zsl, zrangespec *range);\nzskiplistNode *zslLastInRange(zskiplist *zsl, zrangespec *range);\ndouble zzlGetScore(unsigned char *sptr);\nvoid zzlNext(unsigned char *zl, unsigned char **eptr, unsigned char **sptr);\nvoid zzlPrev(unsigned char *zl, unsigned char **eptr, unsigned char **sptr);\nunsigned char *zzlFirstInRange(unsigned char *zl, zrangespec *range);\nunsigned char *zzlLastInRange(unsigned char *zl, zrangespec *range);\nunsigned long zsetLength(robj_roptr zobj);\nvoid zsetConvert(robj *zobj, int encoding);\nvoid zsetConvertToZiplistIfNeeded(robj *zobj, size_t maxelelen, size_t totelelen);\nint zsetScore(robj_roptr zobj, sds member, double *score);\nunsigned long zslGetRank(zskiplist *zsl, double score, sds o);\nint zsetAdd(robj *zobj, double score, sds ele, int in_flags, int *out_flags, double *newscore);\nlong zsetRank(robj_roptr zobj, sds ele, int reverse);\nint zsetDel(robj *zobj, sds ele);\nrobj *zsetDup(robj *o);\nint zsetZiplistValidateIntegrity(unsigned char *zl, size_t size, int deep);\nvoid genericZpopCommand(client *c, robj **keyv, int keyc, int where, int emitkey, robj *countarg);\nsds ziplistGetObject(unsigned char *sptr);\nint zslValueGteMin(double value, zrangespec *spec);\nint zslValueLteMax(double value, zrangespec *spec);\nvoid zslFreeLexRange(zlexrangespec *spec);\nint zslParseLexRange(robj *min, robj *max, zlexrangespec *spec);\nunsigned char *zzlFirstInLexRange(unsigned char *zl, zlexrangespec *range);\nunsigned char *zzlLastInLexRange(unsigned char *zl, zlexrangespec *range);\nzskiplistNode *zslFirstInLexRange(zskiplist *zsl, zlexrangespec *range);\nzskiplistNode *zslLastInLexRange(zskiplist *zsl, zlexrangespec *range);\nint zzlLexValueGteMin(unsigned char *p, zlexrangespec *spec);\nint zzlLexValueLteMax(unsigned char *p, zlexrangespec *spec);\nint zslLexValueGteMin(sds value, zlexrangespec *spec);\nint zslLexValueLteMax(sds value, zlexrangespec *spec);\n\n/* Core functions */\nint getMaxmemoryState(size_t *total, size_t *logical, size_t *tofree, float *level, EvictReason *reason=nullptr, bool fQuickCycle=false, bool fPreSnapshot=false);\nsize_t freeMemoryGetNotCountedMemory();\nint overMaxmemoryAfterAlloc(size_t moremem);\nint processCommand(client *c, int callFlags);\nint processPendingCommandsAndResetClient(client *c, int flags);\nvoid setupSignalHandlers(void);\nvoid removeSignalHandlers(void);\nint createSocketAcceptHandler(socketFds *sfd, aeFileProc *accept_handler);\nint changeListenPort(int port, socketFds *sfd, aeFileProc *accept_handler, bool fFirstCall);\nint changeBindAddr(sds *addrlist, int addrlist_len, bool fFirstCall);\nstruct redisCommand *lookupCommand(sds name);\nstruct redisCommand *lookupCommandByCString(const char *s);\nstruct redisCommand *lookupCommandOrOriginal(sds name);\nvoid call(client *c, int flags);\nvoid propagate(struct redisCommand *cmd, int dbid, robj **argv, int argc, int flags);\nvoid alsoPropagate(struct redisCommand *cmd, int dbid, robj **argv, int argc, int target);\nvoid redisOpArrayInit(redisOpArray *oa);\nvoid redisOpArrayFree(redisOpArray *oa);\nvoid forceCommandPropagation(client *c, int flags);\nvoid preventCommandPropagation(client *c);\nvoid preventCommandAOF(client *c);\nvoid preventCommandReplication(client *c);\nvoid slowlogPushCurrentCommand(client *c, struct redisCommand *cmd, ustime_t duration);\nint prepareForShutdown(int flags);\n#ifdef __GNUC__\nvoid _serverLog(int level, const char *fmt, ...)\n    __attribute__((format(printf, 2, 3)));\n#else\nvoid _serverLog(int level, const char *fmt, ...);\n#endif\nvoid serverLogRaw(int level, const char *msg);\nvoid serverLogFromHandler(int level, const char *msg);\nvoid usage(void);\nvoid updateDictResizePolicy(void);\nint htNeedsResize(dict *dict);\nvoid populateCommandTable(void);\nvoid resetCommandTableStats(void);\nvoid resetErrorTableStats(void);\nvoid adjustOpenFilesLimit(void);\nvoid incrementErrorCount(const char *fullerr, size_t namelen);\nvoid closeListeningSockets(int unlink_unix_socket);\nvoid updateCachedTime(void);\nvoid resetServerStats(void);\nvoid activeDefragCycle(void);\nunsigned int getLRUClock(void);\nunsigned int LRU_CLOCK(void);\nconst char *evictPolicyToString(void);\nstruct redisMemOverhead *getMemoryOverheadData(void);\nvoid freeMemoryOverheadData(struct redisMemOverhead *mh);\nvoid checkChildrenDone(void);\nint setOOMScoreAdj(int process_class);\nvoid rejectCommandFormat(client *c, const char *fmt, ...);\nextern \"C\" void *activeDefragAlloc(void *ptr);\nrobj *activeDefragStringOb(robj* ob, long *defragged);\n\n#define RESTART_SERVER_NONE 0\n#define RESTART_SERVER_GRACEFULLY (1<<0)     /* Do proper shutdown. */\n#define RESTART_SERVER_CONFIG_REWRITE (1<<1) /* CONFIG REWRITE before restart.*/\nint restartServer(int flags, mstime_t delay);\n\n/* Set data type */\nrobj *setTypeCreate(const char *value);\nint setTypeAdd(robj *subject, const char *value);\nint setTypeRemove(robj *subject, const char *value);\nint setTypeIsMember(robj_roptr subject, const char *value);\nsetTypeIterator *setTypeInitIterator(robj_roptr subject);\nvoid setTypeReleaseIterator(setTypeIterator *si);\nint setTypeNext(setTypeIterator *si, const char **sdsele, int64_t *llele);\nsds setTypeNextObject(setTypeIterator *si);\nint setTypeRandomElement(robj *setobj, sds *sdsele, int64_t *llele);\nunsigned long setTypeRandomElements(robj *set, unsigned long count, robj *aux_set);\nunsigned long setTypeSize(robj_roptr subject);\nvoid setTypeConvert(robj *subject, int enc);\nrobj *setTypeDup(robj *o);\n\n/* Hash data type */\n#define HASH_SET_TAKE_FIELD (1<<0)\n#define HASH_SET_TAKE_VALUE (1<<1)\n#define HASH_SET_COPY 0\n\nvoid hashTypeConvert(robj *o, int enc);\nvoid hashTypeTryConversion(robj *subject, robj **argv, int start, int end);\nint hashTypeExists(robj_roptr o, const char *key);\nint hashTypeDelete(robj *o, sds key);\nunsigned long hashTypeLength(robj_roptr o);\nhashTypeIterator *hashTypeInitIterator(robj_roptr subject);\nvoid hashTypeReleaseIterator(hashTypeIterator *hi);\nint hashTypeNext(hashTypeIterator *hi);\nvoid hashTypeCurrentFromZiplist(hashTypeIterator *hi, int what,\n                                unsigned char **vstr,\n                                unsigned int *vlen,\n                                long long *vll);\nsds hashTypeCurrentFromHashTable(hashTypeIterator *hi, int what);\nvoid hashTypeCurrentObject(hashTypeIterator *hi, int what, unsigned char **vstr, unsigned int *vlen, long long *vll);\nsds hashTypeCurrentObjectNewSds(hashTypeIterator *hi, int what);\nrobj *hashTypeLookupWriteOrCreate(client *c, robj *key);\nrobj *hashTypeGetValueObject(robj_roptr o, sds field);\nint hashTypeSet(robj *o, sds field, sds value, int flags);\nrobj *hashTypeDup(robj *o);\nint hashZiplistValidateIntegrity(unsigned char *zl, size_t size, int deep);\n\n/* Pub / Sub */\nint pubsubUnsubscribeAllChannels(client *c, int notify);\nint pubsubUnsubscribeAllPatterns(client *c, int notify);\nint pubsubPublishMessage(robj *channel, robj *message);\nvoid addReplyPubsubMessage(client *c, robj *channel, robj *msg);\n\n/* Keyspace events notification */\nvoid notifyKeyspaceEvent(int type, const char *event, robj *key, int dbid);\nint keyspaceEventsStringToFlags(char *classes);\nsds keyspaceEventsFlagsToString(int flags);\n\n/* Configuration */\nvoid loadServerConfig(char *filename, char config_from_stdin, char *options);\nvoid appendServerSaveParams(time_t seconds, int changes);\nvoid resetServerSaveParams(void);\nstruct rewriteConfigState; /* Forward declaration to export API. */\nvoid rewriteConfigRewriteLine(struct rewriteConfigState *state, const char *option, sds line, int force);\nvoid rewriteConfigMarkAsProcessed(struct rewriteConfigState *state, const char *option);\nint rewriteConfig(char *path, int force_all);\nvoid initConfigValues();\n\n/* db.c -- Keyspace access API */\nclass AeLocker;\nint removeExpire(redisDb *db, robj *key);\nint removeSubkeyExpire(redisDb *db, robj *key, robj *subkey);\nvoid propagateExpire(redisDb *db, robj *key, int lazy);\nvoid propagateSubkeyExpire(redisDb *db, int type, robj *key, robj *subkey);\nvoid deleteExpiredKeyAndPropagate(redisDb *db, robj *keyobj);\nint keyIsExpired(const redisDbPersistentDataSnapshot *db, robj *key);\nint expireIfNeeded(redisDb *db, robj *key);\nvoid setExpire(client *c, redisDb *db, robj *key, robj *subkey, long long when);\nvoid setExpire(client *c, redisDb *db, robj *key, expireEntry &&entry);\nrobj_roptr lookupKeyRead(redisDb *db, robj *key, uint64_t mvccCheckpoint, AeLocker &locker);\nrobj_roptr lookupKeyRead(redisDb *db, robj *key);\nint checkAlreadyExpired(long long when);\nrobj *lookupKeyWrite(redisDb *db, robj *key);\nrobj_roptr lookupKeyReadOrReply(client *c, robj *key, robj *reply, AeLocker &locker);\nrobj_roptr lookupKeyReadOrReply(client *c, robj *key, robj *reply);\nrobj *lookupKeyWriteOrReply(client *c, robj *key, robj *reply);\nrobj_roptr lookupKeyReadWithFlags(redisDb *db, robj *key, int flags);\nrobj *lookupKeyWriteWithFlags(redisDb *db, robj *key, int flags);\nrobj_roptr objectCommandLookup(client *c, robj *key);\nrobj_roptr objectCommandLookupOrReply(client *c, robj *key, robj *reply);\nvoid SentReplyOnKeyMiss(client *c, robj *reply);\nint objectSetLRUOrLFU(robj *val, long long lfu_freq, long long lru_idle,\n                       long long lru_clock, int lru_multiplier);\n#define LOOKUP_NONE 0\n#define LOOKUP_NOTOUCH (1<<0)\n#define LOOKUP_NONOTIFY (1<<1)\n#define LOOKUP_UPDATEMVCC (1<<2)\nvoid dbAdd(redisDb *db, robj *key, robj *val);\nvoid dbOverwrite(redisDb *db, robj *key, robj *val, bool fRemoveExpire = false, dict_iter *pitrExisting = nullptr);\nint dbMerge(redisDb *db, sds key, robj *val, int fReplace);\nvoid genericSetKey(client *c, redisDb *db, robj *key, robj *val, int keepttl, int signal);\nvoid setKey(client *c, redisDb *db, robj *key, robj *val);\nrobj *dbRandomKey(redisDb *db);\nint dbSyncDelete(redisDb *db, robj *key);\nint dbDelete(redisDb *db, robj *key);\nrobj *dbUnshareStringValue(redisDb *db, robj *key, robj *o);\nint dbnumFromDb(redisDb *db);\n\n#define EMPTYDB_NO_FLAGS 0      /* No flags. */\n#define EMPTYDB_ASYNC (1<<0)    /* Reclaim memory in another thread. */\nlong long emptyDb(int dbnum, int flags, void(callback)(void*));\nlong long emptyDbStructure(redisDb **dbarray, int dbnum, int flags, void(callback)(void*));\nvoid flushAllDataAndResetRDB(int flags);\nlong long dbTotalServerKeyCount();\nconst dbBackup *backupDb(void);\nvoid restoreDbBackup(const dbBackup *buckup);\nvoid discardDbBackup(const dbBackup *buckup, int flags, void(callback)(void*));\n\n\nint selectDb(client *c, int id);\nvoid signalModifiedKey(client *c, redisDb *db, robj *key);\nvoid signalFlushedDb(int dbid, int async);\nunsigned int getKeysInSlot(unsigned int hashslot, robj **keys, unsigned int count);\nunsigned int countKeysInSlot(unsigned int hashslot);\nunsigned int delKeysInSlot(unsigned int hashslot);\nint verifyClusterConfigWithData(void);\nvoid scanGenericCommand(client *c, robj_roptr o, unsigned long cursor);\nint parseScanCursorOrReply(client *c, robj *o, unsigned long *cursor);\nvoid slotToKeyAdd(sds key);\nvoid slotToKeyDel(sds key);\nint dbAsyncDelete(redisDb *db, robj *key);\nvoid slotToKeyFlush(int async);\nsize_t lazyfreeGetPendingObjectsCount(void);\nsize_t lazyfreeGetFreedObjectsCount(void);\nvoid freeObjAsync(robj *key, robj *obj);\nvoid freeSlotsToKeysMapAsync(rax *rt);\nvoid freeSlotsToKeysMap(rax *rt, int async);\n\n\n/* API to get key arguments from commands */\nint *getKeysPrepareResult(getKeysResult *result, int numkeys);\nint getKeysFromCommand(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);\nvoid getKeysFreeResult(getKeysResult *result);\nint zunionInterDiffGetKeys(struct redisCommand *cmd,robj **argv, int argc, getKeysResult *result);\nint zunionInterDiffStoreGetKeys(struct redisCommand *cmd,robj **argv, int argc, getKeysResult *result);\nint evalGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);\nint sortGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);\nint migrateGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);\nint georadiusGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);\nint xreadGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);\nint memoryGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);\nint lcsGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);\n\n/* Cluster */\nvoid clusterInit(void);\nextern \"C\" unsigned short crc16(const char *buf, int len);\nunsigned int keyHashSlot(const char *key, int keylen);\nvoid clusterCron(void);\nvoid clusterPropagatePublish(robj *channel, robj *message);\nvoid migrateCloseTimedoutSockets(void);\nvoid clusterBeforeSleep(void);\nint clusterSendModuleMessageToTarget(const char *target, uint64_t module_id, uint8_t type, unsigned char *payload, uint32_t len);\nvoid createDumpPayload(rio *payload, robj_roptr o, robj *key);\n\n/* Sentinel */\nvoid initSentinelConfig(void);\nvoid initSentinel(void);\nvoid sentinelTimer(void);\nconst char *sentinelHandleConfiguration(char **argv, int argc);\nvoid queueSentinelConfig(sds *argv, int argc, int linenum, sds line);\nvoid loadSentinelConfigFromQueue(void);\nvoid sentinelIsRunning(void);\nvoid sentinelCheckConfigFile(void);\n\n/* keydb-check-rdb & aof */\nint redis_check_rdb(const char *rdbfilename, FILE *fp);\nint redis_check_rdb_main(int argc, const char **argv, FILE *fp);\nint redis_check_aof_main(int argc, char **argv);\n\n/* Scripting */\nvoid scriptingInit(int setup);\nint ldbRemoveChild(pid_t pid);\nvoid ldbKillForkedSessions(void);\nint ldbPendingChildren(void);\nsds luaCreateFunction(client *c, lua_State *lua, robj *body);\nvoid freeLuaScriptsAsync(dict *lua_scripts);\n\n/* Blocked clients */\nvoid processUnblockedClients(int iel);\nvoid blockClient(client *c, int btype);\nvoid unblockClient(client *c);\nvoid queueClientForReprocessing(client *c);\nvoid replyToBlockedClientTimedOut(client *c);\nint getTimeoutFromObjectOrReply(client *c, robj *object, mstime_t *timeout, int unit);\nvoid disconnectAllBlockedClients(void);\nvoid handleClientsBlockedOnKeys(void);\nvoid signalKeyAsReady(redisDb *db, robj *key, int type);\nvoid signalKeyAsReady(redisDb *db, sds key, int type);\nvoid blockForKeys(client *c, int btype, robj **keys, int numkeys, mstime_t timeout, robj *target, struct listPos *listpos, streamID *ids);\nvoid updateStatsOnUnblock(client *c, long blocked_us, long reply_us);\n\n/* timeout.c -- Blocked clients timeout and connections timeout. */\nvoid addClientToTimeoutTable(client *c);\nvoid removeClientFromTimeoutTable(client *c);\nvoid handleBlockedClientsTimeout(void);\nint clientsCronHandleTimeout(client *c, mstime_t now_ms);\n\n/* timeout.c -- Blocked clients timeout and connections timeout. */\nvoid addClientToTimeoutTable(client *c);\nvoid removeClientFromTimeoutTable(client *c);\nvoid handleBlockedClientsTimeout(void);\nint clientsCronHandleTimeout(client *c, mstime_t now_ms);\n\n/* expire.c -- Handling of expired keys */\nvoid activeExpireCycle(int type);\nvoid expireSlaveKeys(void);\nvoid rememberSlaveKeyWithExpire(redisDb *db, robj *key);\nvoid flushSlaveKeysWithExpireList(void);\nsize_t getSlaveKeyWithExpireCount(void);\n\n/* evict.c -- maxmemory handling and LRU eviction. */\nvoid evictionPoolAlloc(void);\n#define LFU_INIT_VAL 5\nunsigned long LFUGetTimeInMinutes(void);\nuint8_t LFULogIncr(uint8_t value);\nunsigned long LFUDecrAndReturn(robj_roptr o);\n#define EVICT_OK 0\n#define EVICT_RUNNING 1\n#define EVICT_FAIL 2\nint performEvictions(bool fPreSnapshot);\n\n/* meminfo.cpp -- get memory info from /proc/memoryinfo for linux distros */\nsize_t getMemAvailable();\nsize_t getMemTotal();\n\n/* Keys hashing / comparison functions for dict.c hash tables. */\nuint64_t dictSdsHash(const void *key);\nint dictSdsKeyCompare(void *privdata, const void *key1, const void *key2);\nvoid dictSdsDestructor(void *privdata, void *val);\n\n/* Git SHA1 */\nextern \"C\" char *redisGitSHA1(void);\nextern \"C\" char *redisGitDirty(void);\nextern \"C\" uint64_t redisBuildId(void);\nextern \"C\" char *redisBuildIdString(void);\n\nint parseUnitString(const char *sz);\n\n/* Commands prototypes */\nvoid authCommand(client *c);\nvoid pingCommand(client *c);\nvoid echoCommand(client *c);\nvoid commandCommand(client *c);\nvoid setCommand(client *c);\nvoid setnxCommand(client *c);\nvoid setexCommand(client *c);\nvoid psetexCommand(client *c);\nvoid getCommand(client *c);\nvoid getexCommand(client *c);\nvoid getdelCommand(client *c);\nvoid delCommand(client *c);\nvoid unlinkCommand(client *c);\nvoid existsCommand(client *c);\nvoid mexistsCommand(client *c);\nvoid setbitCommand(client *c);\nvoid getbitCommand(client *c);\nvoid bitfieldCommand(client *c);\nvoid bitfieldroCommand(client *c);\nvoid setrangeCommand(client *c);\nvoid getrangeCommand(client *c);\nvoid incrCommand(client *c);\nvoid decrCommand(client *c);\nvoid incrbyCommand(client *c);\nvoid decrbyCommand(client *c);\nvoid incrbyfloatCommand(client *c);\nvoid selectCommand(client *c);\nvoid swapdbCommand(client *c);\nvoid randomkeyCommand(client *c);\nvoid keysCommand(client *c);\nvoid scanCommand(client *c);\nvoid dbsizeCommand(client *c);\nvoid lastsaveCommand(client *c);\nvoid saveCommand(client *c);\nvoid bgsaveCommand(client *c);\nvoid bgrewriteaofCommand(client *c);\nvoid shutdownCommand(client *c);\nvoid moveCommand(client *c);\nvoid copyCommand(client *c);\nvoid renameCommand(client *c);\nvoid renamenxCommand(client *c);\nvoid lpushCommand(client *c);\nvoid rpushCommand(client *c);\nvoid lpushxCommand(client *c);\nvoid rpushxCommand(client *c);\nvoid linsertCommand(client *c);\nvoid lpopCommand(client *c);\nvoid rpopCommand(client *c);\nvoid llenCommand(client *c);\nvoid lindexCommand(client *c);\nvoid lrangeCommand(client *c);\nvoid ltrimCommand(client *c);\nvoid typeCommand(client *c);\nvoid lsetCommand(client *c);\nvoid saddCommand(client *c);\nvoid sremCommand(client *c);\nvoid smoveCommand(client *c);\nvoid sismemberCommand(client *c);\nvoid smismemberCommand(client *c);\nvoid scardCommand(client *c);\nvoid spopCommand(client *c);\nvoid srandmemberCommand(client *c);\nvoid sinterCommand(client *c);\nvoid sinterstoreCommand(client *c);\nvoid sunionCommand(client *c);\nvoid sunionstoreCommand(client *c);\nvoid sdiffCommand(client *c);\nvoid sdiffstoreCommand(client *c);\nvoid sscanCommand(client *c);\nvoid syncCommand(client *c);\nvoid flushdbCommand(client *c);\nvoid flushallCommand(client *c);\nvoid sortCommand(client *c);\nvoid lremCommand(client *c);\nvoid lposCommand(client *c);\nvoid rpoplpushCommand(client *c);\nvoid lmoveCommand(client *c);\nvoid infoCommand(client *c);\nvoid mgetCommand(client *c);\nvoid monitorCommand(client *c);\nvoid expireCommand(client *c);\nvoid expireatCommand(client *c);\nvoid expireMemberCommand(client *c);\nvoid expireMemberAtCommand(client *c);\nvoid pexpireMemberAtCommand(client *c);\nvoid pexpireCommand(client *c);\nvoid pexpireatCommand(client *c);\nvoid getsetCommand(client *c);\nvoid ttlCommand(client *c);\nvoid touchCommand(client *c);\nvoid pttlCommand(client *c);\nvoid persistCommand(client *c);\nvoid replicaofCommand(client *c);\nvoid roleCommand(client *c);\nvoid debugCommand(client *c);\nvoid msetCommand(client *c);\nvoid msetnxCommand(client *c);\nvoid zaddCommand(client *c);\nvoid zincrbyCommand(client *c);\nvoid zrangeCommand(client *c);\nvoid zrangebyscoreCommand(client *c);\nvoid zrevrangebyscoreCommand(client *c);\nvoid zrangebylexCommand(client *c);\nvoid zrevrangebylexCommand(client *c);\nvoid zcountCommand(client *c);\nvoid zlexcountCommand(client *c);\nvoid zrevrangeCommand(client *c);\nvoid zcardCommand(client *c);\nvoid zremCommand(client *c);\nvoid zscoreCommand(client *c);\nvoid zmscoreCommand(client *c);\nvoid zremrangebyscoreCommand(client *c);\nvoid zremrangebylexCommand(client *c);\nvoid zpopminCommand(client *c);\nvoid zpopmaxCommand(client *c);\nvoid bzpopminCommand(client *c);\nvoid bzpopmaxCommand(client *c);\nvoid zrandmemberCommand(client *c);\nvoid multiCommand(client *c);\nvoid execCommand(client *c);\nvoid discardCommand(client *c);\nvoid blpopCommand(client *c);\nvoid brpopCommand(client *c);\nvoid brpoplpushCommand(client *c);\nvoid blmoveCommand(client *c);\nvoid appendCommand(client *c);\nvoid strlenCommand(client *c);\nvoid zrankCommand(client *c);\nvoid zrevrankCommand(client *c);\nvoid hsetCommand(client *c);\nvoid hsetnxCommand(client *c);\nvoid hgetCommand(client *c);\nvoid hmsetCommand(client *c);\nvoid hmgetCommand(client *c);\nvoid hdelCommand(client *c);\nvoid hlenCommand(client *c);\nvoid hstrlenCommand(client *c);\nvoid zremrangebyrankCommand(client *c);\nvoid zunionstoreCommand(client *c);\nvoid zinterstoreCommand(client *c);\nvoid zdiffstoreCommand(client *c);\nvoid zunionCommand(client *c);\nvoid zinterCommand(client *c);\nvoid zrangestoreCommand(client *c);\nvoid zdiffCommand(client *c);\nvoid zscanCommand(client *c);\nvoid hkeysCommand(client *c);\nvoid hvalsCommand(client *c);\nvoid hgetallCommand(client *c);\nvoid hexistsCommand(client *c);\nvoid hscanCommand(client *c);\nvoid hrandfieldCommand(client *c);\nvoid configCommand(client *c);\nvoid hincrbyCommand(client *c);\nvoid hincrbyfloatCommand(client *c);\nvoid subscribeCommand(client *c);\nvoid unsubscribeCommand(client *c);\nvoid psubscribeCommand(client *c);\nvoid punsubscribeCommand(client *c);\nvoid publishCommand(client *c);\nvoid pubsubCommand(client *c);\nvoid watchCommand(client *c);\nvoid unwatchCommand(client *c);\nvoid clusterCommand(client *c);\nvoid restoreCommand(client *c);\nvoid mvccrestoreCommand(client *c);\nvoid migrateCommand(client *c);\nvoid askingCommand(client *c);\nvoid readonlyCommand(client *c);\nvoid readwriteCommand(client *c);\nvoid dumpCommand(client *c);\nvoid objectCommand(client *c);\nvoid memoryCommand(client *c);\nvoid clientCommand(client *c);\nvoid helloCommand(client *c);\nvoid evalCommand(client *c);\nvoid evalShaCommand(client *c);\nvoid scriptCommand(client *c);\nvoid timeCommand(client *c);\nvoid bitopCommand(client *c);\nvoid bitcountCommand(client *c);\nvoid bitposCommand(client *c);\nvoid replconfCommand(client *c);\nvoid waitCommand(client *c);\nvoid geoencodeCommand(client *c);\nvoid geodecodeCommand(client *c);\nvoid georadiusbymemberCommand(client *c);\nvoid georadiusbymemberroCommand(client *c);\nvoid georadiusCommand(client *c);\nvoid georadiusroCommand(client *c);\nvoid geoaddCommand(client *c);\nvoid geohashCommand(client *c);\nvoid geoposCommand(client *c);\nvoid geodistCommand(client *c);\nvoid geosearchCommand(client *c);\nvoid geosearchstoreCommand(client *c);\nvoid pfselftestCommand(client *c);\nvoid pfaddCommand(client *c);\nvoid pfcountCommand(client *c);\nvoid pfmergeCommand(client *c);\nvoid pfdebugCommand(client *c);\nvoid latencyCommand(client *c);\nvoid moduleCommand(client *c);\nvoid securityWarningCommand(client *c);\nvoid xaddCommand(client *c);\nvoid xrangeCommand(client *c);\nvoid xrevrangeCommand(client *c);\nvoid xlenCommand(client *c);\nvoid xreadCommand(client *c);\nvoid xgroupCommand(client *c);\nvoid xsetidCommand(client *c);\nvoid xackCommand(client *c);\nvoid xpendingCommand(client *c);\nvoid xclaimCommand(client *c);\nvoid xautoclaimCommand(client *c);\nvoid xinfoCommand(client *c);\nvoid xdelCommand(client *c);\nvoid xtrimCommand(client *c);\nvoid aclCommand(client *c);\nvoid replicaReplayCommand(client *c);\nvoid hrenameCommand(client *c);\nvoid stralgoCommand(client *c);\nvoid resetCommand(client *c);\nvoid failoverCommand(client *c);\nvoid lfenceCommand(client *c);\n\n\nint FBrokenLinkToMaster(int *pconnectMasters = nullptr);\nint FActiveMaster(client *c);\nstruct redisMaster *MasterInfoFromClient(client *c);\nbool FInReplicaReplay();\nvoid updateActiveReplicaMastersFromRsi(rdbSaveInfo *rsi);\n\n/* MVCC */\nuint64_t getMvccTstamp();\nvoid incrementMvccTstamp();\n\n#if __GNUC__ >= 7 && !defined(NO_DEPRECATE_FREE) && !defined(ALPINE)\n [[deprecated]]\nvoid *calloc(size_t count, size_t size) noexcept;\n [[deprecated]]\nvoid free(void *ptr) noexcept;\n [[deprecated]]\nvoid *malloc(size_t size) noexcept;\n [[deprecated]]\nvoid *realloc(void *ptr, size_t size) noexcept;\n#endif\n\n/* Debugging stuff */\nvoid bugReportStart(void);\nvoid serverLogObjectDebugInfo(robj_roptr o);\nvoid sigsegvHandler(int sig, siginfo_t *info, void *secret);\nconst char *getSafeInfoString(const char *s, size_t len, char **tmp);\nsds genRedisInfoString(const char *section);\nsds genModulesInfoString(sds info);\nvoid enableWatchdog(int period);\nvoid disableWatchdog(void);\nvoid watchdogScheduleSignal(int period);\nvoid serverLogHexDump(int level, const char *descr, void *value, size_t len);\nextern \"C\" int memtest_preserving_test(unsigned long *m, size_t bytes, int passes);\nvoid mixDigest(unsigned char *digest, const void *ptr, size_t len);\nvoid xorDigest(unsigned char *digest, const void *ptr, size_t len);\nint populateCommandTableParseFlags(struct redisCommand *c, const char *strflags);\n\n\n\nint moduleGILAcquiredByModule(void);\nextern int g_fInCrash;\nstatic inline int GlobalLocksAcquired(void)  // Used in asserts to verify all global locks are correctly acquired for a server-thread to operate\n{\n    return aeThreadOwnsLock() || moduleGILAcquiredByModule() || g_fInCrash;\n}\n\ninline int ielFromEventLoop(const aeEventLoop *eventLoop)\n{\n    int iel = 0;\n    for (; iel < cserver.cthreads; ++iel)\n    {\n        if (g_pserver->rgthreadvar[iel].el == eventLoop)\n            break;\n    }\n    serverAssert(iel < cserver.cthreads);\n    return iel;\n}\n\ninline bool FFastSyncEnabled() {\n    return g_pserver->fEnableFastSync && !g_pserver->fActiveReplica;\n}\n\ninline int FCorrectThread(client *c)\n{\n    return (c->conn == nullptr)\n        || (c->iel == IDX_EVENT_LOOP_MAIN && moduleGILAcquiredByModule())\n        || (serverTL != NULL && (g_pserver->rgthreadvar[c->iel].el == serverTL->el));\n}\n#define AssertCorrectThread(c) serverAssert(FCorrectThread(c))\n\nvoid flushReplBacklogToClients();\n\ntemplate<typename FN_PTR, class ...TARGS>\nvoid runAndPropogateToReplicas(FN_PTR *pfn, TARGS... args) {\n    // Store the replication backlog starting params, we use this to know how much data was written.\n    //  these are TLS in case we need to expand the buffer and therefore need to update them\n    bool fNestedProcess = (g_pserver->repl_batch_idxStart >= 0);\n    if (!fNestedProcess) {\n        g_pserver->repl_batch_offStart = g_pserver->master_repl_offset;\n        g_pserver->repl_batch_idxStart = g_pserver->repl_backlog_idx;\n    }\n\n    pfn(args...);\n\n    if (!fNestedProcess) {\n        flushReplBacklogToClients();\n        g_pserver->repl_batch_offStart = -1;\n        g_pserver->repl_batch_idxStart = -1;\n    }\n}\n\nvoid debugDelay(int usec);\nvoid killIOThreads(void);\nvoid killThreads(void);\nvoid makeThreadKillable(void);\n\n/* Use macro for checking log level to avoid evaluating arguments in cases log\n * should be ignored due to low level. */\n#define serverLog(level, ...) do {\\\n        if (((level)&0xff) < cserver.verbosity) break;\\\n        _serverLog(level, __VA_ARGS__);\\\n    } while(0)\n\n/* TLS stuff */\nvoid tlsInit(void);\nvoid tlsInitThread();\nvoid tlsCleanupThread();\nvoid tlsCleanup(void);\nint tlsConfigure(redisTLSContextConfig *ctx_config);\nvoid tlsReload(void);\n\n\nclass ShutdownException\n{};\n\n#define redisDebug(fmt, ...) \\\n    printf(\"DEBUG %s:%d > \" fmt \"\\n\", __FILE__, __LINE__, __VA_ARGS__)\n#define redisDebugMark() \\\n    printf(\"-- MARK %s:%d --\\n\", __FILE__, __LINE__)\n\nint iAmMaster(void);\n\n#endif\n\n\n"
  },
  {
    "path": "src/serverassert.h",
    "content": "#pragma once\n\nvoid _serverAssertWithInfo(const struct client *c, class robj_roptr o, const char *estr, const char *file, int line);\nextern \"C\" void _serverAssert(const char *estr, const char *file, int line);\n#ifdef __GNUC__\nextern \"C\" void _serverPanic(const char *file, int line, const char *msg, ...)\n    __attribute__ ((format (printf, 3, 4)));\n#else\nextern \"C\" void _serverPanic(const char *file, int line, const char *msg, ...);\n#endif\n\nextern int g_fInCrash;\n\n/* We can print the stacktrace, so our assert is defined this way: */\n#define serverAssertWithInfo(_c,_o,_e) ((_e)?(void)0 : (_serverAssertWithInfo(_c,_o,#_e,__FILE__,__LINE__),_exit(1)))\n#define serverAssert(_e) (((_e) || g_fInCrash) ?(void)0 : (_serverAssert(#_e,__FILE__,__LINE__),_exit(1)))\n#ifdef _DEBUG\n#define serverAssertDebug(_e) serverAssert(_e)\n#else\n#define serverAssertDebug(_e)\n#endif\n#define serverPanic(...) _serverPanic(__FILE__,__LINE__,__VA_ARGS__),_exit(1)"
  },
  {
    "path": "src/setcpuaffinity.c",
    "content": "/* ==========================================================================\n * setcpuaffinity.c - Linux/BSD setcpuaffinity.\n * --------------------------------------------------------------------------\n * Copyright (C) 2020  zhenwei pi\n *\n * Permission is hereby granted, free of charge, to any person obtaining a\n * copy of this software and associated documentation files (the\n * \"Software\"), to deal in the Software without restriction, including\n * without limitation the rights to use, copy, modify, merge, publish,\n * distribute, sublicense, and/or sell copies of the Software, and to permit\n * persons to whom the Software is furnished to do so, subject to the\n * following conditions:\n *\n * The above copyright notice and this permission notice shall be included\n * in all copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS\n * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN\n * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\n * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR\n * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE\n * USE OR OTHER DEALINGS IN THE SOFTWARE.\n * ==========================================================================\n */\n#ifndef _GNU_SOURCE\n#define _GNU_SOURCE\n#endif\n#include <stdlib.h>\n#include <string.h>\n#include <ctype.h>\n#ifdef __linux__\n#include <sched.h>\n#endif\n#ifdef __FreeBSD__\n#include <sys/param.h>\n#include <sys/cpuset.h>\n#endif\n#ifdef __DragonFly__\n#include <pthread.h>\n#include <pthread_np.h>\n#endif\n#ifdef __NetBSD__\n#include <pthread.h>\n#include <sched.h>\n#endif\n#include \"config.h\"\n\n#ifdef USE_SETCPUAFFINITY\nstatic const char *next_token(const char *q,  int sep) {\n    if (q)\n        q = strchr(q, sep);\n    if (q)\n        q++;\n\n    return q;\n}\n\nstatic int next_num(const char *str, char **end, int *result) {\n    if (!str || *str == '\\0' || !isdigit(*str))\n        return -1;\n\n    *result = strtoul(str, end, 10);\n    if (str == *end)\n        return -1;\n\n    return 0;\n}\n\n/* set current thread cpu affinity to cpu list, this function works like\n * taskset command (actually cpulist parsing logic reference to util-linux).\n * example of this function: \"0,2,3\", \"0,2-3\", \"0-20:2\". */\nvoid setcpuaffinity(const char *cpulist) {\n    const char *p, *q;\n    char *end = NULL;\n#ifdef __linux__\n    cpu_set_t cpuset;\n#endif\n#if defined (__FreeBSD__) || defined(__DragonFly__)\n    cpuset_t cpuset;\n#endif\n#ifdef __NetBSD__\n    cpuset_t *cpuset;\n#endif\n\n    if (!cpulist)\n        return;\n\n#ifndef __NetBSD__\n    CPU_ZERO(&cpuset);\n#else\n    cpuset = cpuset_create();\n#endif\n\n    q = cpulist;\n    while (p = q, q = next_token(q, ','), p) {\n        int a, b, s;\n        const char *c1, *c2;\n\n        if (next_num(p, &end, &a) != 0)\n            return;\n\n        b = a;\n        s = 1;\n        p = end;\n\n        c1 = next_token(p, '-');\n        c2 = next_token(p, ',');\n\n        if (c1 != NULL && (c2 == NULL || c1 < c2)) {\n            if (next_num(c1, &end, &b) != 0)\n                return;\n\n            c1 = end && *end ? next_token(end, ':') : NULL;\n            if (c1 != NULL && (c2 == NULL || c1 < c2)) {\n                if (next_num(c1, &end, &s) != 0)\n                    return;\n\n                if (s == 0)\n                    return;\n            }\n        }\n\n        if ((a > b))\n            return;\n\n        while (a <= b) {\n#ifndef __NetBSD__\n            CPU_SET(a, &cpuset);\n#else\n            cpuset_set(a, cpuset);\n#endif\n            a += s;\n        }\n    }\n\n    if (end && *end)\n        return;\n\n#ifdef __linux__\n    sched_setaffinity(0, sizeof(cpuset), &cpuset);\n#endif\n#ifdef __FreeBSD__\n    cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(cpuset), &cpuset);\n#endif\n#ifdef __DragonFly__\n    pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset);\n#endif\n#ifdef __NetBSD__\n    pthread_setaffinity_np(pthread_self(), cpuset_size(cpuset), cpuset);\n    cpuset_destroy(cpuset);\n#endif\n}\n\n#endif /* USE_SETCPUAFFINITY */\n"
  },
  {
    "path": "src/setproctitle.c",
    "content": "/* ==========================================================================\n * setproctitle.c - Linux/Darwin setproctitle.\n * --------------------------------------------------------------------------\n * Copyright (C) 2010  William Ahern\n * Copyright (C) 2013  Salvatore Sanfilippo\n * Copyright (C) 2013  Stam He\n *\n * Permission is hereby granted, free of charge, to any person obtaining a\n * copy of this software and associated documentation files (the\n * \"Software\"), to deal in the Software without restriction, including\n * without limitation the rights to use, copy, modify, merge, publish,\n * distribute, sublicense, and/or sell copies of the Software, and to permit\n * persons to whom the Software is furnished to do so, subject to the\n * following conditions:\n *\n * The above copyright notice and this permission notice shall be included\n * in all copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS\n * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN\n * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\n * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR\n * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE\n * USE OR OTHER DEALINGS IN THE SOFTWARE.\n * ==========================================================================\n */\n#ifndef _GNU_SOURCE\n#define _GNU_SOURCE\n#endif\n\n#include <stddef.h>\t/* NULL size_t */\n#include <stdarg.h>\t/* va_list va_start va_end */\n#include <stdlib.h>\t/* malloc(3) setenv(3) clearenv(3) setproctitle(3) getprogname(3) */\n#include <stdio.h>\t/* vsnprintf(3) snprintf(3) */\n\n#include <string.h>\t/* strlen(3) strchr(3) strdup(3) memset(3) memcpy(3) */\n\n#include <errno.h>\t/* errno program_invocation_name program_invocation_short_name */\n\n#if !defined(HAVE_SETPROCTITLE)\n#if (defined __NetBSD__ || defined __FreeBSD__ || defined __OpenBSD__ || defined __DragonFly__)\n#define HAVE_SETPROCTITLE 1\n#else\n#define HAVE_SETPROCTITLE 0\n#endif\n#endif\n\n\n#if !HAVE_SETPROCTITLE\n#if (defined __linux || defined __APPLE__)\n\n#ifdef __GLIBC__\n#define HAVE_CLEARENV\n#endif\n\nextern char **environ;\n\nstatic struct {\n\t/* original value */\n\tconst char *arg0;\n\n\t/* title space available */\n\tchar *base, *end;\n\n\t /* pointer to original nul character within base */\n\tchar *nul;\n\n\t_Bool reset;\n\tint error;\n} SPT;\n\n\n#ifndef SPT_MIN\n#define SPT_MIN(a, b) (((a) < (b))? (a) : (b))\n#endif\n\nstatic inline size_t spt_min(size_t a, size_t b) {\n\treturn SPT_MIN(a, b);\n} /* spt_min() */\n\n\n/*\n * For discussion on the portability of the various methods, see\n * http://lists.freebsd.org/pipermail/freebsd-stable/2008-June/043136.html\n */\nint spt_clearenv(void) {\n#ifdef HAVE_CLEARENV\n\treturn clearenv();\n#else\n\textern char **environ;\n\tstatic char **tmp;\n\n\tif (!(tmp = malloc(sizeof *tmp)))\n\t\treturn errno;\n\n\ttmp[0]  = NULL;\n\tenviron = tmp;\n\n\treturn 0;\n#endif\n} /* spt_clearenv() */\n\n\nstatic int spt_copyenv(int envc, char *oldenv[]) {\n\textern char **environ;\n\tchar **envcopy = NULL;\n\tchar *eq;\n\tint i, error;\n\tint envsize;\n\n\tif (environ != oldenv)\n\t\treturn 0;\n\n\t/* Copy environ into envcopy before clearing it. Shallow copy is\n\t * enough as clearenv() only clears the environ array.\n\t */\n\tenvsize = (envc + 1) * sizeof(char *);\n\tenvcopy = malloc(envsize);\n\tif (!envcopy)\n\t\treturn ENOMEM;\n\tmemcpy(envcopy, oldenv, envsize);\n\n\t/* Note that the state after clearenv() failure is undefined, but we'll\n\t * just assume an error means it was left unchanged.\n\t */\n\tif ((error = spt_clearenv())) {\n\t\tenviron = oldenv;\n\t\tfree(envcopy);\n\t\treturn error;\n\t}\n\n\t/* Set environ from envcopy */\n\tfor (i = 0; envcopy[i]; i++) {\n\t\tif (!(eq = strchr(envcopy[i], '=')))\n\t\t\tcontinue;\n\n\t\t*eq = '\\0';\n\t\terror = (0 != setenv(envcopy[i], eq + 1, 1))? errno : 0;\n\t\t*eq = '=';\n\n\t\t/* On error, do our best to restore state */\n\t\tif (error) {\n#ifdef HAVE_CLEARENV\n\t\t\t/* We don't assume it is safe to free environ, so we\n\t\t\t * may leak it. As clearenv() was shallow using envcopy\n\t\t\t * here is safe.\n\t\t\t */\n\t\t\tenviron = envcopy;\n#else\n\t\t\tfree(envcopy);\n\t\t\tfree(environ);  /* Safe to free, we have just alloc'd it */\n\t\t\tenviron = oldenv;\n#endif\n\t\t\treturn error;\n\t\t}\n\t}\n\n\tfree(envcopy);\n\treturn 0;\n} /* spt_copyenv() */\n\n\nstatic int spt_copyargs(int argc, char *argv[]) {\n\tchar *tmp;\n\tint i;\n\n\tfor (i = 1; i < argc || (i >= argc && argv[i]); i++) {\n\t\tif (!argv[i])\n\t\t\tcontinue;\n\n\t\tif (!(tmp = strdup(argv[i])))\n\t\t\treturn errno;\n\n\t\targv[i] = tmp;\n\t}\n\n\treturn 0;\n} /* spt_copyargs() */\n\n/* Initialize and populate SPT to allow a future setproctitle()\n * call.\n *\n * As setproctitle() basically needs to overwrite argv[0], we're\n * trying to determine what is the largest contiguous block\n * starting at argv[0] we can use for this purpose.\n *\n * As this range will overwrite some or all of the argv and environ\n * strings, a deep copy of these two arrays is performed.\n */\nvoid spt_init(int argc, char *argv[]) {\n        char **envp = environ;\n\tchar *base, *end, *nul, *tmp;\n\tint i, error, envc;\n\n\tif (!(base = argv[0]))\n\t\treturn;\n\n\t/* We start with end pointing at the end of argv[0] */\n\tnul = &base[strlen(base)];\n\tend = nul + 1;\n\n\t/* Attempt to extend end as far as we can, while making sure\n\t * that the range between base and end is only allocated to\n\t * argv, or anything that immediately follows argv (presumably\n\t * envp).\n\t */\n\tfor (i = 0; i < argc || (i >= argc && argv[i]); i++) {\n\t\tif (!argv[i] || argv[i] < end)\n\t\t\tcontinue;\n\n\t\tif (end >= argv[i] && end <= argv[i] + strlen(argv[i]))\n\t\t\tend = argv[i] + strlen(argv[i]) + 1;\n\t}\n\n\t/* In case the envp array was not an immediate extension to argv,\n\t * scan it explicitly.\n\t */\n\tfor (i = 0; envp[i]; i++) {\n\t\tif (envp[i] < end)\n\t\t\tcontinue;\n\n\t\tif (end >= envp[i] && end <= envp[i] + strlen(envp[i]))\n\t\t\tend = envp[i] + strlen(envp[i]) + 1;\n\t}\n\tenvc = i;\n\n\t/* We're going to deep copy argv[], but argv[0] will still point to\n\t * the old memory for the purpose of updating the title so we need\n\t * to keep the original value elsewhere.\n\t */\n\tif (!(SPT.arg0 = strdup(argv[0])))\n\t\tgoto syerr;\n\n#if __linux__\n\tif (!(tmp = strdup(program_invocation_name)))\n\t\tgoto syerr;\n\n\tprogram_invocation_name = tmp;\n\n\tif (!(tmp = strdup(program_invocation_short_name)))\n\t\tgoto syerr;\n\n\tprogram_invocation_short_name = tmp;\n#elif __APPLE__\n\tif (!(tmp = strdup(getprogname())))\n\t\tgoto syerr;\n\n\tsetprogname(tmp);\n#endif\n\n    /* Now make a full deep copy of the environment and argv[] */\n\tif ((error = spt_copyenv(envc, envp)))\n\t\tgoto error;\n\n\tif ((error = spt_copyargs(argc, argv)))\n\t\tgoto error;\n\n\tSPT.nul  = nul;\n\tSPT.base = base;\n\tSPT.end  = end;\n\n\treturn;\nsyerr:\n\terror = errno;\nerror:\n\tSPT.error = error;\n} /* spt_init() */\n\n\n#ifndef SPT_MAXTITLE\n#define SPT_MAXTITLE 255\n#endif\n\nvoid setproctitle(const char *fmt, ...) {\n\tchar buf[SPT_MAXTITLE + 1]; /* use buffer in case argv[0] is passed */\n\tva_list ap;\n\tchar *nul;\n\tint len, error;\n\n\tif (!SPT.base)\n\t\treturn;\n\n\tif (fmt) {\n\t\tva_start(ap, fmt);\n\t\tlen = vsnprintf(buf, sizeof buf, fmt, ap);\n\t\tva_end(ap);\n\t} else {\n\t\tlen = snprintf(buf, sizeof buf, \"%s\", SPT.arg0);\n\t}\n\n\tif (len <= 0)\n\t\t{ error = errno; goto error; }\n\n\tif (!SPT.reset) {\n\t\tmemset(SPT.base, 0, SPT.end - SPT.base);\n\t\tSPT.reset = 1;\n\t} else {\n\t\tmemset(SPT.base, 0, spt_min(sizeof buf, SPT.end - SPT.base));\n\t}\n\n\tlen = spt_min(len, spt_min(sizeof buf, SPT.end - SPT.base) - 1);\n\tmemcpy(SPT.base, buf, len);\n\tnul = &SPT.base[len];\n\n\tif (nul < SPT.nul) {\n\t\t*SPT.nul = '.';\n\t} else if (nul == SPT.nul && &nul[1] < SPT.end) {\n\t\t*SPT.nul = ' ';\n\t\t*++nul = '\\0';\n\t}\n\n\treturn;\nerror:\n\tSPT.error = error;\n} /* setproctitle() */\n\n\n#endif /* __linux || __APPLE__ */\n#endif /* !HAVE_SETPROCTITLE */\n\n#ifdef SETPROCTITLE_TEST_MAIN\nint main(int argc, char *argv[]) {\n\tspt_init(argc, argv);\n\n\tprintf(\"SPT.arg0: [%p] '%s'\\n\", SPT.arg0, SPT.arg0);\n\tprintf(\"SPT.base: [%p] '%s'\\n\", SPT.base, SPT.base);\n\tprintf(\"SPT.end: [%p] (%d bytes after base)'\\n\", SPT.end, (int) (SPT.end - SPT.base));\n\treturn 0;\n}\n#endif\n"
  },
  {
    "path": "src/sha1.c",
    "content": "\n/* from valgrind tests */\n\n/* ================ sha1.c ================ */\n/*\nSHA-1 in C\nBy Steve Reid <steve@edmweb.com>\n100% Public Domain\n\nTest Vectors (from FIPS PUB 180-1)\n\"abc\"\n  A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D\n\"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\"\n  84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1\nA million repetitions of \"a\"\n  34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F\n*/\n\n/* #define LITTLE_ENDIAN * This should be #define'd already, if true. */\n/* #define SHA1HANDSOFF * Copies data before messing with it. */\n\n#define SHA1HANDSOFF\n\n#include <stdio.h>\n#include <string.h>\n#include <stdint.h>\n#include \"solarisfixes.h\"\n#include \"sha1.h\"\n#include \"config.h\"\n\n#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))\n\n/* blk0() and blk() perform the initial expand. */\n/* I got the idea of expanding during the round function from SSLeay */\n#if BYTE_ORDER == LITTLE_ENDIAN\n#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \\\n    |(rol(block->l[i],8)&0x00FF00FF))\n#elif BYTE_ORDER == BIG_ENDIAN\n#define blk0(i) block->l[i]\n#else\n#error \"Endianness not defined!\"\n#endif\n#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \\\n    ^block->l[(i+2)&15]^block->l[i&15],1))\n\n/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */\n#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);\n#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);\n#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);\n#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);\n#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);\n\n\n/* Hash a single 512-bit block. This is the core of the algorithm. */\n\nvoid SHA1Transform(uint32_t state[5], const unsigned char buffer[64])\n{\n    uint32_t a, b, c, d, e;\n    typedef union {\n        unsigned char c[64];\n        uint32_t l[16];\n    } CHAR64LONG16;\n#ifdef SHA1HANDSOFF\n    CHAR64LONG16 block[1];  /* use array to appear as a pointer */\n    memcpy(block, buffer, 64);\n#else\n    /* The following had better never be used because it causes the\n     * pointer-to-const buffer to be cast into a pointer to non-const.\n     * And the result is written through.  I threw a \"const\" in, hoping\n     * this will cause a diagnostic.\n     */\n    CHAR64LONG16* block = (const CHAR64LONG16*)buffer;\n#endif\n    /* Copy context->state[] to working vars */\n    a = state[0];\n    b = state[1];\n    c = state[2];\n    d = state[3];\n    e = state[4];\n    /* 4 rounds of 20 operations each. Loop unrolled. */\n    R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);\n    R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);\n    R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);\n    R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);\n    R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);\n    R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);\n    R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);\n    R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);\n    R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);\n    R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);\n    R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);\n    R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);\n    R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);\n    R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);\n    R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);\n    R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);\n    R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);\n    R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);\n    R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);\n    R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);\n    /* Add the working vars back into context.state[] */\n    state[0] += a;\n    state[1] += b;\n    state[2] += c;\n    state[3] += d;\n    state[4] += e;\n    /* Wipe variables */\n    a = b = c = d = e = 0;\n#ifdef SHA1HANDSOFF\n    memset(block, '\\0', sizeof(block));\n#endif\n}\n\n\n/* SHA1Init - Initialize new context */\n\nvoid SHA1Init(SHA1_CTX* context)\n{\n    /* SHA1 initialization constants */\n    context->state[0] = 0x67452301;\n    context->state[1] = 0xEFCDAB89;\n    context->state[2] = 0x98BADCFE;\n    context->state[3] = 0x10325476;\n    context->state[4] = 0xC3D2E1F0;\n    context->count[0] = context->count[1] = 0;\n}\n\n\n/* Run your data through this. */\n\nvoid SHA1Update(SHA1_CTX* context, const unsigned char* data, uint32_t len)\n{\n    uint32_t i, j;\n\n    j = context->count[0];\n    if ((context->count[0] += len << 3) < j)\n        context->count[1]++;\n    context->count[1] += (len>>29);\n    j = (j >> 3) & 63;\n    if ((j + len) > 63) {\n        memcpy(&context->buffer[j], data, (i = 64-j));\n        SHA1Transform(context->state, context->buffer);\n        for ( ; i + 63 < len; i += 64) {\n            SHA1Transform(context->state, &data[i]);\n        }\n        j = 0;\n    }\n    else i = 0;\n    memcpy(&context->buffer[j], &data[i], len - i);\n}\n\n\n/* Add padding and return the message digest. */\n\nvoid SHA1Final(unsigned char digest[20], SHA1_CTX* context)\n{\n    unsigned i;\n    unsigned char finalcount[8];\n    unsigned char c;\n\n#if 0\t/* untested \"improvement\" by DHR */\n    /* Convert context->count to a sequence of bytes\n     * in finalcount.  Second element first, but\n     * big-endian order within element.\n     * But we do it all backwards.\n     */\n    unsigned char *fcp = &finalcount[8];\n\n    for (i = 0; i < 2; i++)\n       {\n        uint32_t t = context->count[i];\n        int j;\n\n        for (j = 0; j < 4; t >>= 8, j++)\n\t          *--fcp = (unsigned char) t;\n    }\n#else\n    for (i = 0; i < 8; i++) {\n        finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)]\n         >> ((3-(i & 3)) * 8) ) & 255);  /* Endian independent */\n    }\n#endif\n    c = 0200;\n    SHA1Update(context, &c, 1);\n    while ((context->count[0] & 504) != 448) {\n\tc = 0000;\n        SHA1Update(context, &c, 1);\n    }\n    SHA1Update(context, finalcount, 8);  /* Should cause a SHA1Transform() */\n    for (i = 0; i < 20; i++) {\n        digest[i] = (unsigned char)\n         ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);\n    }\n    /* Wipe variables */\n    memset(context, '\\0', sizeof(*context));\n    memset(&finalcount, '\\0', sizeof(finalcount));\n}\n/* ================ end of sha1.c ================ */\n\n#ifdef REDIS_TEST\n#define BUFSIZE 4096\n\n#define UNUSED(x) (void)(x)\nint sha1Test(int argc, char **argv, int accurate)\n{\n    SHA1_CTX ctx;\n    unsigned char hash[20], buf[BUFSIZE];\n    int i;\n\n    UNUSED(argc);\n    UNUSED(argv);\n    UNUSED(accurate);\n\n    for(i=0;i<BUFSIZE;i++)\n        buf[i] = i;\n\n    SHA1Init(&ctx);\n    for(i=0;i<1000;i++)\n        SHA1Update(&ctx, buf, BUFSIZE);\n    SHA1Final(hash, &ctx);\n\n    printf(\"SHA1=\");\n    for(i=0;i<20;i++)\n        printf(\"%02x\", hash[i]);\n    printf(\"\\n\");\n    return 0;\n}\n#endif\n"
  },
  {
    "path": "src/sha1.h",
    "content": "#ifndef SHA1_H\n#define SHA1_H\n/* ================ sha1.h ================ */\n/*\nSHA-1 in C\nBy Steve Reid <steve@edmweb.com>\n100% Public Domain\n*/\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\ntypedef struct {\n    uint32_t state[5];\n    uint32_t count[2];\n    unsigned char buffer[64];\n} SHA1_CTX;\n\nvoid SHA1Transform(uint32_t state[5], const unsigned char buffer[64]);\nvoid SHA1Init(SHA1_CTX* context);\nvoid SHA1Update(SHA1_CTX* context, const unsigned char* data, uint32_t len);\nvoid SHA1Final(unsigned char digest[20], SHA1_CTX* context);\n\n#ifdef REDIS_TEST\nint sha1Test(int argc, char **argv, int accurate);\n#endif\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n"
  },
  {
    "path": "src/sha256.c",
    "content": "/*********************************************************************\n* Filename:   sha256.c\n* Author:     Brad Conte (brad AT bradconte.com)\n* Copyright:\n* Disclaimer: This code is presented \"as is\" without any guarantees.\n* Details:    Implementation of the SHA-256 hashing algorithm.\n              SHA-256 is one of the three algorithms in the SHA2\n              specification. The others, SHA-384 and SHA-512, are not\n              offered in this implementation.\n              Algorithm specification can be found here:\n               * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf\n              This implementation uses little endian byte order.\n*********************************************************************/\n\n/*************************** HEADER FILES ***************************/\n#include <stdlib.h>\n#include <string.h>\n#include \"sha256.h\"\n\n/****************************** MACROS ******************************/\n#define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b))))\n#define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b))))\n\n#define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z)))\n#define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))\n#define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22))\n#define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25))\n#define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3))\n#define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10))\n\n/**************************** VARIABLES *****************************/\nstatic const WORD k[64] = {\n\t0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,\n\t0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,\n\t0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,\n\t0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,\n\t0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,\n\t0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,\n\t0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,\n\t0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2\n};\n\n/*********************** FUNCTION DEFINITIONS ***********************/\nvoid sha256_transform(SHA256_CTX *ctx, const BYTE data[])\n{\n\tWORD a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];\n\n\tfor (i = 0, j = 0; i < 16; ++i, j += 4)\n\t\tm[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]);\n\tfor ( ; i < 64; ++i)\n\t\tm[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];\n\n\ta = ctx->state[0];\n\tb = ctx->state[1];\n\tc = ctx->state[2];\n\td = ctx->state[3];\n\te = ctx->state[4];\n\tf = ctx->state[5];\n\tg = ctx->state[6];\n\th = ctx->state[7];\n\n\tfor (i = 0; i < 64; ++i) {\n\t\tt1 = h + EP1(e) + CH(e,f,g) + k[i] + m[i];\n\t\tt2 = EP0(a) + MAJ(a,b,c);\n\t\th = g;\n\t\tg = f;\n\t\tf = e;\n\t\te = d + t1;\n\t\td = c;\n\t\tc = b;\n\t\tb = a;\n\t\ta = t1 + t2;\n\t}\n\n\tctx->state[0] += a;\n\tctx->state[1] += b;\n\tctx->state[2] += c;\n\tctx->state[3] += d;\n\tctx->state[4] += e;\n\tctx->state[5] += f;\n\tctx->state[6] += g;\n\tctx->state[7] += h;\n}\n\nvoid sha256_init(SHA256_CTX *ctx)\n{\n\tctx->datalen = 0;\n\tctx->bitlen = 0;\n\tctx->state[0] = 0x6a09e667;\n\tctx->state[1] = 0xbb67ae85;\n\tctx->state[2] = 0x3c6ef372;\n\tctx->state[3] = 0xa54ff53a;\n\tctx->state[4] = 0x510e527f;\n\tctx->state[5] = 0x9b05688c;\n\tctx->state[6] = 0x1f83d9ab;\n\tctx->state[7] = 0x5be0cd19;\n}\n\nvoid sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len)\n{\n\tWORD i;\n\n\tfor (i = 0; i < len; ++i) {\n\t\tctx->data[ctx->datalen] = data[i];\n\t\tctx->datalen++;\n\t\tif (ctx->datalen == 64) {\n\t\t\tsha256_transform(ctx, ctx->data);\n\t\t\tctx->bitlen += 512;\n\t\t\tctx->datalen = 0;\n\t\t}\n\t}\n}\n\nvoid sha256_final(SHA256_CTX *ctx, BYTE hash[])\n{\n\tWORD i;\n\n\ti = ctx->datalen;\n\n\t// Pad whatever data is left in the buffer.\n\tif (ctx->datalen < 56) {\n\t\tctx->data[i++] = 0x80;\n\t\twhile (i < 56)\n\t\t\tctx->data[i++] = 0x00;\n\t}\n\telse {\n\t\tctx->data[i++] = 0x80;\n\t\twhile (i < 64)\n\t\t\tctx->data[i++] = 0x00;\n\t\tsha256_transform(ctx, ctx->data);\n\t\tmemset(ctx->data, 0, 56);\n\t}\n\n\t// Append to the padding the total message's length in bits and transform.\n\tctx->bitlen += ctx->datalen * 8;\n\tctx->data[63] = ctx->bitlen;\n\tctx->data[62] = ctx->bitlen >> 8;\n\tctx->data[61] = ctx->bitlen >> 16;\n\tctx->data[60] = ctx->bitlen >> 24;\n\tctx->data[59] = ctx->bitlen >> 32;\n\tctx->data[58] = ctx->bitlen >> 40;\n\tctx->data[57] = ctx->bitlen >> 48;\n\tctx->data[56] = ctx->bitlen >> 56;\n\tsha256_transform(ctx, ctx->data);\n\n\t// Since this implementation uses little endian byte ordering and SHA uses big endian,\n\t// reverse all the bytes when copying the final state to the output hash.\n\tfor (i = 0; i < 4; ++i) {\n\t\thash[i]      = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff;\n\t\thash[i + 4]  = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff;\n\t\thash[i + 8]  = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff;\n\t\thash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff;\n\t\thash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff;\n\t\thash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff;\n\t\thash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff;\n\t\thash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff;\n\t}\n}\n"
  },
  {
    "path": "src/sha256.h",
    "content": "/*********************************************************************\n* Filename:   sha256.h\n* Author:     Brad Conte (brad AT bradconte.com)\n* Copyright:\n* Disclaimer: This code is presented \"as is\" without any guarantees.\n* Details:    Defines the API for the corresponding SHA1 implementation.\n*********************************************************************/\n\n#ifndef SHA256_H\n#define SHA256_H\n\n/*************************** HEADER FILES ***************************/\n#include <stddef.h>\n#include <stdint.h>\n\n/****************************** MACROS ******************************/\n#define SHA256_BLOCK_SIZE 32            // SHA256 outputs a 32 byte digest\n\n/**************************** DATA TYPES ****************************/\ntypedef uint8_t BYTE;   // 8-bit byte\ntypedef uint32_t WORD;  // 32-bit word\n\ntypedef struct {\n\tBYTE data[64];\n\tWORD datalen;\n\tunsigned long long bitlen;\n\tWORD state[8];\n} SHA256_CTX;\n\n/*********************** FUNCTION DECLARATIONS **********************/\nvoid sha256_init(SHA256_CTX *ctx);\nvoid sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len);\nvoid sha256_final(SHA256_CTX *ctx, BYTE hash[]);\n\n#endif   // SHA256_H\n"
  },
  {
    "path": "src/siphash.c",
    "content": "/*\n   SipHash reference C implementation\n\n   Copyright (c) 2012-2016 Jean-Philippe Aumasson\n   <jeanphilippe.aumasson@gmail.com>\n   Copyright (c) 2012-2014 Daniel J. Bernstein <djb@cr.yp.to>\n   Copyright (c) 2017 Salvatore Sanfilippo <antirez@gmail.com>\n\n   To the extent possible under law, the author(s) have dedicated all copyright\n   and related and neighboring rights to this software to the public domain\n   worldwide. This software is distributed without any warranty.\n\n   You should have received a copy of the CC0 Public Domain Dedication along\n   with this software. If not, see\n   <http://creativecommons.org/publicdomain/zero/1.0/>.\n\n   ----------------------------------------------------------------------------\n\n   This version was modified by Salvatore Sanfilippo <antirez@gmail.com>\n   in the following ways:\n\n   1. We use SipHash 1-2. This is not believed to be as strong as the\n      suggested 2-4 variant, but AFAIK there are not trivial attacks\n      against this reduced-rounds version, and it runs at the same speed\n      as Murmurhash2 that we used previously, while the 2-4 variant slowed\n      down Redis by a 4% figure more or less.\n   2. Hard-code rounds in the hope the compiler can optimize it more\n      in this raw from. Anyway we always want the standard 2-4 variant.\n   3. Modify the prototype and implementation so that the function directly\n      returns an uint64_t value, the hash itself, instead of receiving an\n      output buffer. This also means that the output size is set to 8 bytes\n      and the 16 bytes output code handling was removed.\n   4. Provide a case insensitive variant to be used when hashing strings that\n      must be considered identical by the hash table regardless of the case.\n      If we don't have directly a case insensitive hash function, we need to\n      perform a text transformation in some temporary buffer, which is costly.\n   5. Remove debugging code.\n   6. Modified the original test.c file to be a stand-alone function testing\n      the function in the new form (returning an uint64_t) using just the\n      relevant test vector.\n */\n#include <assert.h>\n#include <stdint.h>\n#include <stdio.h>\n#include <string.h>\n#include <ctype.h>\n\n/* Fast tolower() alike function that does not care about locale\n * but just returns a-z instead of A-Z. */\nint siptlw(int c) {\n    if (c >= 'A' && c <= 'Z') {\n        return c+('a'-'A');\n    } else {\n        return c;\n    }\n}\n\n/* Test of the CPU is Little Endian and supports not aligned accesses.\n * Two interesting conditions to speedup the function that happen to be\n * in most of x86 servers. */\n#if defined(__X86_64__) || defined(__x86_64__) || defined (__i386__) \\\n\t|| defined (__aarch64__) || defined (__arm64__)\n#define UNALIGNED_LE_CPU\n#endif\n\n#define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b))))\n\n#define U32TO8_LE(p, v)                                                        \\\n    (p)[0] = (uint8_t)((v));                                                   \\\n    (p)[1] = (uint8_t)((v) >> 8);                                              \\\n    (p)[2] = (uint8_t)((v) >> 16);                                             \\\n    (p)[3] = (uint8_t)((v) >> 24);\n\n#define U64TO8_LE(p, v)                                                        \\\n    U32TO8_LE((p), (uint32_t)((v)));                                           \\\n    U32TO8_LE((p) + 4, (uint32_t)((v) >> 32));\n\n#ifdef UNALIGNED_LE_CPU\n#define U8TO64_LE(p) (*((uint64_t*)(p)))\n#else\n#define U8TO64_LE(p)                                                           \\\n    (((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) |                        \\\n     ((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) |                 \\\n     ((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) |                 \\\n     ((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56))\n#endif\n\n#define U8TO64_LE_NOCASE(p)                                                    \\\n    (((uint64_t)(siptlw((p)[0]))) |                                           \\\n     ((uint64_t)(siptlw((p)[1])) << 8) |                                      \\\n     ((uint64_t)(siptlw((p)[2])) << 16) |                                     \\\n     ((uint64_t)(siptlw((p)[3])) << 24) |                                     \\\n     ((uint64_t)(siptlw((p)[4])) << 32) |                                              \\\n     ((uint64_t)(siptlw((p)[5])) << 40) |                                              \\\n     ((uint64_t)(siptlw((p)[6])) << 48) |                                              \\\n     ((uint64_t)(siptlw((p)[7])) << 56))\n\n#define SIPROUND                                                               \\\n    do {                                                                       \\\n        v0 += v1;                                                              \\\n        v1 = ROTL(v1, 13);                                                     \\\n        v1 ^= v0;                                                              \\\n        v0 = ROTL(v0, 32);                                                     \\\n        v2 += v3;                                                              \\\n        v3 = ROTL(v3, 16);                                                     \\\n        v3 ^= v2;                                                              \\\n        v0 += v3;                                                              \\\n        v3 = ROTL(v3, 21);                                                     \\\n        v3 ^= v0;                                                              \\\n        v2 += v1;                                                              \\\n        v1 = ROTL(v1, 17);                                                     \\\n        v1 ^= v2;                                                              \\\n        v2 = ROTL(v2, 32);                                                     \\\n    } while (0)\n\nuint64_t siphash(const uint8_t *in, const size_t inlen, const uint8_t *k) {\n#ifndef UNALIGNED_LE_CPU\n    uint64_t hash;\n    uint8_t *out = (uint8_t*) &hash;\n#endif\n    uint64_t v0 = 0x736f6d6570736575ULL;\n    uint64_t v1 = 0x646f72616e646f6dULL;\n    uint64_t v2 = 0x6c7967656e657261ULL;\n    uint64_t v3 = 0x7465646279746573ULL;\n    uint64_t k0 = U8TO64_LE(k);\n    uint64_t k1 = U8TO64_LE(k + 8);\n    uint64_t m;\n    const uint8_t *end = in + inlen - (inlen % sizeof(uint64_t));\n    const int left = inlen & 7;\n    uint64_t b = ((uint64_t)inlen) << 56;\n    v3 ^= k1;\n    v2 ^= k0;\n    v1 ^= k1;\n    v0 ^= k0;\n\n    for (; in != end; in += 8) {\n        m = U8TO64_LE(in);\n        v3 ^= m;\n\n        SIPROUND;\n\n        v0 ^= m;\n    }\n\n    switch (left) {\n    case 7: b |= ((uint64_t)in[6]) << 48; /* fall-thru */\n    case 6: b |= ((uint64_t)in[5]) << 40; /* fall-thru */\n    case 5: b |= ((uint64_t)in[4]) << 32; /* fall-thru */\n    case 4: b |= ((uint64_t)in[3]) << 24; /* fall-thru */\n    case 3: b |= ((uint64_t)in[2]) << 16; /* fall-thru */\n    case 2: b |= ((uint64_t)in[1]) << 8; /* fall-thru */\n    case 1: b |= ((uint64_t)in[0]); break;\n    case 0: break;\n    }\n\n    v3 ^= b;\n\n    SIPROUND;\n\n    v0 ^= b;\n    v2 ^= 0xff;\n\n    SIPROUND;\n    SIPROUND;\n\n    b = v0 ^ v1 ^ v2 ^ v3;\n#ifndef UNALIGNED_LE_CPU\n    U64TO8_LE(out, b);\n    return hash;\n#else\n    return b;\n#endif\n}\n\nuint64_t siphash_nocase(const uint8_t *in, const size_t inlen, const uint8_t *k)\n{\n#ifndef UNALIGNED_LE_CPU\n    uint64_t hash;\n    uint8_t *out = (uint8_t*) &hash;\n#endif\n    uint64_t v0 = 0x736f6d6570736575ULL;\n    uint64_t v1 = 0x646f72616e646f6dULL;\n    uint64_t v2 = 0x6c7967656e657261ULL;\n    uint64_t v3 = 0x7465646279746573ULL;\n    uint64_t k0 = U8TO64_LE(k);\n    uint64_t k1 = U8TO64_LE(k + 8);\n    uint64_t m;\n    const uint8_t *end = in + inlen - (inlen % sizeof(uint64_t));\n    const int left = inlen & 7;\n    uint64_t b = ((uint64_t)inlen) << 56;\n    v3 ^= k1;\n    v2 ^= k0;\n    v1 ^= k1;\n    v0 ^= k0;\n\n    for (; in != end; in += 8) {\n        m = U8TO64_LE_NOCASE(in);\n        v3 ^= m;\n\n        SIPROUND;\n\n        v0 ^= m;\n    }\n\n    switch (left) {\n    case 7: b |= ((uint64_t)siptlw(in[6])) << 48; /* fall-thru */\n    case 6: b |= ((uint64_t)siptlw(in[5])) << 40; /* fall-thru */\n    case 5: b |= ((uint64_t)siptlw(in[4])) << 32; /* fall-thru */\n    case 4: b |= ((uint64_t)siptlw(in[3])) << 24; /* fall-thru */\n    case 3: b |= ((uint64_t)siptlw(in[2])) << 16; /* fall-thru */\n    case 2: b |= ((uint64_t)siptlw(in[1])) << 8; /* fall-thru */\n    case 1: b |= ((uint64_t)siptlw(in[0])); break;\n    case 0: break;\n    }\n\n    v3 ^= b;\n\n    SIPROUND;\n\n    v0 ^= b;\n    v2 ^= 0xff;\n\n    SIPROUND;\n    SIPROUND;\n\n    b = v0 ^ v1 ^ v2 ^ v3;\n#ifndef UNALIGNED_LE_CPU\n    U64TO8_LE(out, b);\n    return hash;\n#else\n    return b;\n#endif\n}\n\n\n/* --------------------------------- TEST ------------------------------------ */\n\n#ifdef SIPHASH_TEST\n\nconst uint8_t vectors_sip64[64][8] = {\n    { 0x31, 0x0e, 0x0e, 0xdd, 0x47, 0xdb, 0x6f, 0x72, },\n    { 0xfd, 0x67, 0xdc, 0x93, 0xc5, 0x39, 0xf8, 0x74, },\n    { 0x5a, 0x4f, 0xa9, 0xd9, 0x09, 0x80, 0x6c, 0x0d, },\n    { 0x2d, 0x7e, 0xfb, 0xd7, 0x96, 0x66, 0x67, 0x85, },\n    { 0xb7, 0x87, 0x71, 0x27, 0xe0, 0x94, 0x27, 0xcf, },\n    { 0x8d, 0xa6, 0x99, 0xcd, 0x64, 0x55, 0x76, 0x18, },\n    { 0xce, 0xe3, 0xfe, 0x58, 0x6e, 0x46, 0xc9, 0xcb, },\n    { 0x37, 0xd1, 0x01, 0x8b, 0xf5, 0x00, 0x02, 0xab, },\n    { 0x62, 0x24, 0x93, 0x9a, 0x79, 0xf5, 0xf5, 0x93, },\n    { 0xb0, 0xe4, 0xa9, 0x0b, 0xdf, 0x82, 0x00, 0x9e, },\n    { 0xf3, 0xb9, 0xdd, 0x94, 0xc5, 0xbb, 0x5d, 0x7a, },\n    { 0xa7, 0xad, 0x6b, 0x22, 0x46, 0x2f, 0xb3, 0xf4, },\n    { 0xfb, 0xe5, 0x0e, 0x86, 0xbc, 0x8f, 0x1e, 0x75, },\n    { 0x90, 0x3d, 0x84, 0xc0, 0x27, 0x56, 0xea, 0x14, },\n    { 0xee, 0xf2, 0x7a, 0x8e, 0x90, 0xca, 0x23, 0xf7, },\n    { 0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1, },\n    { 0xdb, 0x9b, 0xc2, 0x57, 0x7f, 0xcc, 0x2a, 0x3f, },\n    { 0x94, 0x47, 0xbe, 0x2c, 0xf5, 0xe9, 0x9a, 0x69, },\n    { 0x9c, 0xd3, 0x8d, 0x96, 0xf0, 0xb3, 0xc1, 0x4b, },\n    { 0xbd, 0x61, 0x79, 0xa7, 0x1d, 0xc9, 0x6d, 0xbb, },\n    { 0x98, 0xee, 0xa2, 0x1a, 0xf2, 0x5c, 0xd6, 0xbe, },\n    { 0xc7, 0x67, 0x3b, 0x2e, 0xb0, 0xcb, 0xf2, 0xd0, },\n    { 0x88, 0x3e, 0xa3, 0xe3, 0x95, 0x67, 0x53, 0x93, },\n    { 0xc8, 0xce, 0x5c, 0xcd, 0x8c, 0x03, 0x0c, 0xa8, },\n    { 0x94, 0xaf, 0x49, 0xf6, 0xc6, 0x50, 0xad, 0xb8, },\n    { 0xea, 0xb8, 0x85, 0x8a, 0xde, 0x92, 0xe1, 0xbc, },\n    { 0xf3, 0x15, 0xbb, 0x5b, 0xb8, 0x35, 0xd8, 0x17, },\n    { 0xad, 0xcf, 0x6b, 0x07, 0x63, 0x61, 0x2e, 0x2f, },\n    { 0xa5, 0xc9, 0x1d, 0xa7, 0xac, 0xaa, 0x4d, 0xde, },\n    { 0x71, 0x65, 0x95, 0x87, 0x66, 0x50, 0xa2, 0xa6, },\n    { 0x28, 0xef, 0x49, 0x5c, 0x53, 0xa3, 0x87, 0xad, },\n    { 0x42, 0xc3, 0x41, 0xd8, 0xfa, 0x92, 0xd8, 0x32, },\n    { 0xce, 0x7c, 0xf2, 0x72, 0x2f, 0x51, 0x27, 0x71, },\n    { 0xe3, 0x78, 0x59, 0xf9, 0x46, 0x23, 0xf3, 0xa7, },\n    { 0x38, 0x12, 0x05, 0xbb, 0x1a, 0xb0, 0xe0, 0x12, },\n    { 0xae, 0x97, 0xa1, 0x0f, 0xd4, 0x34, 0xe0, 0x15, },\n    { 0xb4, 0xa3, 0x15, 0x08, 0xbe, 0xff, 0x4d, 0x31, },\n    { 0x81, 0x39, 0x62, 0x29, 0xf0, 0x90, 0x79, 0x02, },\n    { 0x4d, 0x0c, 0xf4, 0x9e, 0xe5, 0xd4, 0xdc, 0xca, },\n    { 0x5c, 0x73, 0x33, 0x6a, 0x76, 0xd8, 0xbf, 0x9a, },\n    { 0xd0, 0xa7, 0x04, 0x53, 0x6b, 0xa9, 0x3e, 0x0e, },\n    { 0x92, 0x59, 0x58, 0xfc, 0xd6, 0x42, 0x0c, 0xad, },\n    { 0xa9, 0x15, 0xc2, 0x9b, 0xc8, 0x06, 0x73, 0x18, },\n    { 0x95, 0x2b, 0x79, 0xf3, 0xbc, 0x0a, 0xa6, 0xd4, },\n    { 0xf2, 0x1d, 0xf2, 0xe4, 0x1d, 0x45, 0x35, 0xf9, },\n    { 0x87, 0x57, 0x75, 0x19, 0x04, 0x8f, 0x53, 0xa9, },\n    { 0x10, 0xa5, 0x6c, 0xf5, 0xdf, 0xcd, 0x9a, 0xdb, },\n    { 0xeb, 0x75, 0x09, 0x5c, 0xcd, 0x98, 0x6c, 0xd0, },\n    { 0x51, 0xa9, 0xcb, 0x9e, 0xcb, 0xa3, 0x12, 0xe6, },\n    { 0x96, 0xaf, 0xad, 0xfc, 0x2c, 0xe6, 0x66, 0xc7, },\n    { 0x72, 0xfe, 0x52, 0x97, 0x5a, 0x43, 0x64, 0xee, },\n    { 0x5a, 0x16, 0x45, 0xb2, 0x76, 0xd5, 0x92, 0xa1, },\n    { 0xb2, 0x74, 0xcb, 0x8e, 0xbf, 0x87, 0x87, 0x0a, },\n    { 0x6f, 0x9b, 0xb4, 0x20, 0x3d, 0xe7, 0xb3, 0x81, },\n    { 0xea, 0xec, 0xb2, 0xa3, 0x0b, 0x22, 0xa8, 0x7f, },\n    { 0x99, 0x24, 0xa4, 0x3c, 0xc1, 0x31, 0x57, 0x24, },\n    { 0xbd, 0x83, 0x8d, 0x3a, 0xaf, 0xbf, 0x8d, 0xb7, },\n    { 0x0b, 0x1a, 0x2a, 0x32, 0x65, 0xd5, 0x1a, 0xea, },\n    { 0x13, 0x50, 0x79, 0xa3, 0x23, 0x1c, 0xe6, 0x60, },\n    { 0x93, 0x2b, 0x28, 0x46, 0xe4, 0xd7, 0x06, 0x66, },\n    { 0xe1, 0x91, 0x5f, 0x5c, 0xb1, 0xec, 0xa4, 0x6c, },\n    { 0xf3, 0x25, 0x96, 0x5c, 0xa1, 0x6d, 0x62, 0x9f, },\n    { 0x57, 0x5f, 0xf2, 0x8e, 0x60, 0x38, 0x1b, 0xe5, },\n    { 0x72, 0x45, 0x06, 0xeb, 0x4c, 0x32, 0x8a, 0x95, },\n};\n\n\n/* Test siphash using a test vector. Returns 0 if the function passed\n * all the tests, otherwise 1 is returned.\n *\n * IMPORTANT: The test vector is for SipHash 2-4. Before running\n * the test revert back the siphash() function to 2-4 rounds since\n * now it uses 1-2 rounds. */\nint siphash_test(void) {\n    uint8_t in[64], k[16];\n    int i;\n    int fails = 0;\n\n    for (i = 0; i < 16; ++i)\n        k[i] = i;\n\n    for (i = 0; i < 64; ++i) {\n        in[i] = i;\n        uint64_t hash = siphash(in, i, k);\n        const uint8_t *v = NULL;\n        v = (uint8_t *)vectors_sip64;\n        if (memcmp(&hash, v + (i * 8), 8)) {\n            /* printf(\"fail for %d bytes\\n\", i); */\n            fails++;\n        }\n    }\n\n    /* Run a few basic tests with the case insensitive version. */\n    uint64_t h1, h2;\n    h1 = siphash((uint8_t*)\"hello world\",11,(uint8_t*)\"1234567812345678\");\n    h2 = siphash_nocase((uint8_t*)\"hello world\",11,(uint8_t*)\"1234567812345678\");\n    if (h1 != h2) fails++;\n\n    h1 = siphash((uint8_t*)\"hello world\",11,(uint8_t*)\"1234567812345678\");\n    h2 = siphash_nocase((uint8_t*)\"HELLO world\",11,(uint8_t*)\"1234567812345678\");\n    if (h1 != h2) fails++;\n\n    h1 = siphash((uint8_t*)\"HELLO world\",11,(uint8_t*)\"1234567812345678\");\n    h2 = siphash_nocase((uint8_t*)\"HELLO world\",11,(uint8_t*)\"1234567812345678\");\n    if (h1 == h2) fails++;\n\n    if (!fails) return 0;\n    return 1;\n}\n\nint main(void) {\n    if (siphash_test() == 0) {\n        printf(\"SipHash test: OK\\n\");\n        return 0;\n    } else {\n        printf(\"SipHash test: FAILED\\n\");\n        return 1;\n    }\n}\n\n#endif\n"
  },
  {
    "path": "src/slowlog.cpp",
    "content": "/* Slowlog implements a system that is able to remember the latest N\n * queries that took more than M microseconds to execute.\n *\n * The execution time to reach to be logged in the slow log is set\n * using the 'slowlog-log-slower-than' config directive, that is also\n * readable and writable using the CONFIG SET/GET command.\n *\n * The slow queries log is actually not \"logged\" in the Redis log file\n * but is accessible thanks to the SLOWLOG command.\n *\n * ----------------------------------------------------------------------------\n *\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n\n#include \"server.h\"\n#include \"slowlog.h\"\n\n/* Create a new slowlog entry.\n * Incrementing the ref count of all the objects retained is up to\n * this function. */\nslowlogEntry *slowlogCreateEntry(client *c, robj **argv, int argc, long long duration) {\n    slowlogEntry *se = (slowlogEntry*)zmalloc(sizeof(*se), MALLOC_LOCAL);\n    int j, slargc = argc;\n\n    if (slargc > SLOWLOG_ENTRY_MAX_ARGC) slargc = SLOWLOG_ENTRY_MAX_ARGC;\n    se->argc = slargc;\n    se->argv = (robj**)zmalloc(sizeof(robj*)*slargc, MALLOC_LOCAL);\n    for (j = 0; j < slargc; j++) {\n        /* Logging too many arguments is a useless memory waste, so we stop\n         * at SLOWLOG_ENTRY_MAX_ARGC, but use the last argument to specify\n         * how many remaining arguments there were in the original command. */\n        if (slargc != argc && j == slargc-1) {\n            se->argv[j] = createObject(OBJ_STRING,\n                sdscatprintf(sdsempty(),\"... (%d more arguments)\",\n                argc-slargc+1));\n        } else {\n            /* Trim too long strings as well... */\n            if (argv[j]->type == OBJ_STRING &&\n                sdsEncodedObject(argv[j]) &&\n                sdslen(szFromObj(argv[j])) > SLOWLOG_ENTRY_MAX_STRING)\n            {\n                sds s = sdsnewlen(ptrFromObj(argv[j]), SLOWLOG_ENTRY_MAX_STRING);\n\n                s = sdscatprintf(s,\"... (%lu more bytes)\",\n                    (unsigned long)\n                    sdslen(szFromObj(argv[j])) - SLOWLOG_ENTRY_MAX_STRING);\n                se->argv[j] = createObject(OBJ_STRING,s);\n            } else if (argv[j]->getrefcount(std::memory_order_relaxed) == OBJ_SHARED_REFCOUNT) {\n                se->argv[j] = argv[j];\n            } else {\n                /* Here we need to duplicate the string objects composing the\n                 * argument vector of the command, because those may otherwise\n                 * end shared with string objects stored into keys. Having\n                 * shared objects between any part of Redis, and the data\n                 * structure holding the data, is a problem: FLUSHALL ASYNC\n                 * may release the shared string object and create a race. */\n                se->argv[j] = dupStringObject(argv[j]);\n            }\n        }\n    }\n    se->time = time(NULL);\n    se->duration = duration;\n    se->id = g_pserver->slowlog_entry_id++;\n    se->peerid = sdsnew(getClientPeerId(c));\n    se->cname = c->name ? sdsnew(szFromObj(c->name)) : sdsempty();\n    return se;\n}\n\n/* Free a slow log entry. The argument is void so that the prototype of this\n * function matches the one of the 'free' method of adlist.c.\n *\n * This function will take care to release all the retained object. */\nvoid slowlogFreeEntry(const void *septr) {\n    slowlogEntry *se = (slowlogEntry*)septr;\n    int j;\n\n    for (j = 0; j < se->argc; j++)\n        decrRefCount(se->argv[j]);\n    zfree(se->argv);\n    sdsfree(se->peerid);\n    sdsfree(se->cname);\n    zfree(se);\n}\n\n/* Initialize the slow log. This function should be called a single time\n * at server startup. */\nvoid slowlogInit(void) {\n    g_pserver->slowlog = listCreate();\n    g_pserver->slowlog_entry_id = 0;\n    listSetFreeMethod(g_pserver->slowlog,slowlogFreeEntry);\n}\n\n/* Push a new entry into the slow log.\n * This function will make sure to trim the slow log accordingly to the\n * configured max length. */\nvoid slowlogPushEntryIfNeeded(client *c, robj **argv, int argc, long long duration) {\n    if (g_pserver->slowlog_log_slower_than < 0) return; /* Slowlog disabled */\n    if (duration >= g_pserver->slowlog_log_slower_than)\n        listAddNodeHead(g_pserver->slowlog,\n                        slowlogCreateEntry(c,argv,argc,duration));\n\n    /* Remove old entries if needed. */\n    while (listLength(g_pserver->slowlog) > g_pserver->slowlog_max_len)\n        listDelNode(g_pserver->slowlog,listLast(g_pserver->slowlog));\n}\n\n/* Remove all the entries from the current slow log. */\nvoid slowlogReset(void) {\n    while (listLength(g_pserver->slowlog) > 0)\n        listDelNode(g_pserver->slowlog,listLast(g_pserver->slowlog));\n}\n\n/* The SLOWLOG command. Implements all the subcommands needed to handle the\n * Redis slow log. */\nvoid slowlogCommand(client *c) {\n    if (c->argc == 2 && !strcasecmp(szFromObj(c->argv[1]),\"help\")) {\n        const char *help[] = {\n\"GET [<count>]\",\n\"    Return top <count> entries from the slowlog (default: 10). Entries are\",\n\"    made of:\",\n\"    id, timestamp, time in microseconds, arguments array, client IP and port,\",\n\"    client name\",\n\"LEN\",\n\"    Return the length of the slowlog.\",\n\"RESET\",\n\"    Reset the slowlog.\",\nNULL\n        };\n        addReplyHelp(c, help);\n    } else if (c->argc == 2 && !strcasecmp(szFromObj(c->argv[1]),\"reset\")) {\n        slowlogReset();\n        addReply(c,shared.ok);\n    } else if (c->argc == 2 && !strcasecmp(szFromObj(c->argv[1]),\"len\")) {\n        addReplyLongLong(c,listLength(g_pserver->slowlog));\n    } else if ((c->argc == 2 || c->argc == 3) &&\n               !strcasecmp(szFromObj(c->argv[1]),\"get\"))\n    {\n        long count = 10, sent = 0;\n        listIter li;\n        void *totentries;\n        listNode *ln;\n        slowlogEntry *se;\n\n        if (c->argc == 3 &&\n            getLongFromObjectOrReply(c,c->argv[2],&count,NULL) != C_OK)\n            return;\n\n        listRewind(g_pserver->slowlog,&li);\n        totentries = addReplyDeferredLen(c);\n        while(count-- && (ln = listNext(&li))) {\n            int j;\n\n            se = (slowlogEntry*)ln->value;\n            addReplyArrayLen(c,6);\n            addReplyLongLong(c,se->id);\n            addReplyLongLong(c,se->time);\n            addReplyLongLong(c,se->duration);\n            addReplyArrayLen(c,se->argc);\n            for (j = 0; j < se->argc; j++)\n                addReplyBulk(c,se->argv[j]);\n            addReplyBulkCBuffer(c,se->peerid,sdslen(se->peerid));\n            addReplyBulkCBuffer(c,se->cname,sdslen(se->cname));\n            sent++;\n        }\n        setDeferredArrayLen(c,totentries,sent);\n    } else {\n        addReplySubcommandSyntaxError(c);\n    }\n}\n"
  },
  {
    "path": "src/slowlog.h",
    "content": "/*\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __SLOWLOG_H__\n#define __SLOWLOG_H__\n\n#define SLOWLOG_ENTRY_MAX_ARGC 32\n#define SLOWLOG_ENTRY_MAX_STRING 128\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/* This structure defines an entry inside the slow log list */\ntypedef struct slowlogEntry {\n    robj **argv;\n    int argc;\n    long long id;       /* Unique entry identifier. */\n    long long duration; /* Time spent by the query, in microseconds. */\n    time_t time;        /* Unix time at which the query was executed. */\n    sds cname;          /* Client name. */\n    sds peerid;         /* Client network address. */\n} slowlogEntry;\n\n/* Exported API */\nvoid slowlogInit(void);\nvoid slowlogPushEntryIfNeeded(client *c, robj **argv, int argc, long long duration);\n\n/* Exported commands */\nvoid slowlogCommand(client *c);\n\n#ifdef __cplusplus\n}\n#endif\n#endif /* __SLOWLOG_H__ */\n"
  },
  {
    "path": "src/snapshot.cpp",
    "content": "#include \"server.h\"\n#include \"aelocker.h\"\n\nstatic const size_t c_elementsSmallLimit = 500000;\nstatic fastlock s_lock {\"consolidate_children\"};    // this lock ensures only one thread is consolidating at a time\n\nclass LazyFree : public ICollectable\n{\npublic:\n    virtual ~LazyFree()\n    {\n        for (auto *de : vecde)\n        {\n            dbDictType.keyDestructor(nullptr, dictGetKey(de));\n            dbDictType.valDestructor(nullptr, dictGetVal(de));\n            zfree(de);\n        }\n        for (robj *o : vecobjLazyFree)\n            decrRefCount(o);\n        for (dict *d : vecdictLazyFree)\n            dictRelease(d);\n    }\n\n    std::vector<dict*> vecdictLazyFree;\n    std::vector<robj*> vecobjLazyFree;\n    std::vector<dictEntry*> vecde;\n};\n\nconst redisDbPersistentDataSnapshot *redisDbPersistentData::createSnapshot(uint64_t mvccCheckpoint, bool fOptional)\n{\n    serverAssert(GlobalLocksAcquired());\n    serverAssert(m_refCount == 0);  // do not call this on a snapshot\n\n    if (performEvictions(true /*fPreSnapshot*/) != C_OK && fOptional)\n        return nullptr; // can't create snapshot due to OOM\n\n    int levels = 1;\n    redisDbPersistentDataSnapshot *psnapshot = m_spdbSnapshotHOLDER.get();\n    while (psnapshot != nullptr)\n    {\n        ++levels;\n        psnapshot = psnapshot->m_spdbSnapshotHOLDER.get();\n    }\n\n    if (m_spdbSnapshotHOLDER != nullptr)\n    {\n        // If possible reuse an existing snapshot (we want to minimize nesting)\n        if (mvccCheckpoint <= m_spdbSnapshotHOLDER->m_mvccCheckpoint)\n        {\n            if (!m_spdbSnapshotHOLDER->FStale())\n            {\n                m_spdbSnapshotHOLDER->m_refCount++;\n                return m_spdbSnapshotHOLDER.get();\n            }\n            serverLog(LL_VERBOSE, \"Existing snapshot too old, creating a new one\");\n        }\n    }\n\n    // See if we have too many levels and can bail out of this to reduce load\n    if (fOptional && (levels >= 6))\n    {\n        serverLog(LL_DEBUG, \"Snapshot nesting too deep, abondoning\");\n        return nullptr;\n    }\n\n    auto spdb = std::unique_ptr<redisDbPersistentDataSnapshot>(new (MALLOC_LOCAL) redisDbPersistentDataSnapshot());\n    \n    // We can't have async rehash modifying these.  Setting the asyncdata list to null\n    //  will cause us to throw away the async work rather than modify the tables in flight\n    discontinueAsyncRehash(m_pdict);\n    discontinueAsyncRehash(m_pdictTombstone);\n\n    spdb->m_fAllChanged = false;\n    spdb->m_fTrackingChanges = 0;\n    spdb->m_pdict = m_pdict;\n    spdb->m_pdictTombstone = m_pdictTombstone;\n    spdb->m_numexpires = m_numexpires;\n    // Add a fake iterator so the dicts don't rehash (they need to be read only)\n    dictPauseRehashing(spdb->m_pdict);\n    dictForceRehash(spdb->m_pdictTombstone);    // prevent rehashing by finishing the rehash now\n    spdb->m_spdbSnapshotHOLDER = std::move(m_spdbSnapshotHOLDER);\n    if (m_spstorage != nullptr)\n        spdb->m_spstorage = std::shared_ptr<StorageCache>(const_cast<StorageCache*>(m_spstorage->clone()));\n    spdb->m_pdbSnapshot = m_pdbSnapshot;\n    spdb->m_refCount = 1;\n    spdb->m_mvccCheckpoint = getMvccTstamp();\n\n    if (dictIsRehashing(spdb->m_pdict) || dictIsRehashing(spdb->m_pdictTombstone)) {\n        serverLog(LL_VERBOSE, \"NOTICE: Suboptimal snapshot\");\n    }\n\n    m_pdict = dictCreate(&dbDictType,this);\n    dictExpand(m_pdict, 1024);   // minimize rehash overhead\n    m_pdictTombstone = dictCreate(&dbTombstoneDictType, this);\n\n    serverAssert(spdb->m_pdict->pauserehash == 1);\n\n    m_spdbSnapshotHOLDER = std::move(spdb);\n    m_pdbSnapshot = m_spdbSnapshotHOLDER.get();\n\n    // Finally we need to take a ref on all our children snapshots.  This ensures they aren't free'd before we are\n    redisDbPersistentData *pdbSnapshotNext = m_pdbSnapshot->m_spdbSnapshotHOLDER.get();\n    while (pdbSnapshotNext != nullptr)\n    {\n        pdbSnapshotNext->m_refCount++;\n        pdbSnapshotNext = pdbSnapshotNext->m_spdbSnapshotHOLDER.get();\n    }\n\n    if (m_pdbSnapshotASYNC != nullptr)\n    {\n        // free the async snapshot, it's done its job\n        endSnapshot(m_pdbSnapshotASYNC);    // should be just a dec ref (FAST)\n        m_pdbSnapshotASYNC = nullptr;\n    }\n\n    std::atomic_thread_fence(std::memory_order_seq_cst);\n    return m_pdbSnapshot;\n}\n\nvoid redisDbPersistentData::recursiveFreeSnapshots(redisDbPersistentDataSnapshot *psnapshot)\n{\n    std::vector<redisDbPersistentDataSnapshot*> stackSnapshots;\n    // gather a stack of snapshots, we do this so we can free them in reverse\n    \n    // Note: we don't touch the incoming psnapshot since the parent is free'ing that one\n    while ((psnapshot = psnapshot->m_spdbSnapshotHOLDER.get()) != nullptr)\n    {\n        stackSnapshots.push_back(psnapshot);\n    }\n\n    for (auto itr = stackSnapshots.rbegin(); itr != stackSnapshots.rend(); ++itr)\n    {\n        endSnapshot(*itr);\n    }\n}\n\n/* static */ void redisDbPersistentDataSnapshot::gcDisposeSnapshot(redisDbPersistentDataSnapshot *psnapshot)\n{\n    psnapshot->m_refCount--;\n    if (psnapshot->m_refCount <= 0)\n    {\n        serverAssert(psnapshot->m_refCount == 0);\n        // Remove our ref from any children and dispose them too\n        redisDbPersistentDataSnapshot *psnapshotChild = psnapshot;\n        std::vector<redisDbPersistentDataSnapshot*> vecClean;\n        while ((psnapshotChild = psnapshotChild->m_spdbSnapshotHOLDER.get()) != nullptr)\n            vecClean.push_back(psnapshotChild);\n\n        for (auto psnapshotChild : vecClean)\n            gcDisposeSnapshot(psnapshotChild);\n\n        //psnapshot->m_pdict->iterators--;\n        psnapshot->m_spdbSnapshotHOLDER.release();\n        psnapshot->m_pdbSnapshot = nullptr;\n        g_pserver->garbageCollector.enqueue(serverTL->gcEpoch, std::unique_ptr<redisDbPersistentDataSnapshot>(psnapshot));\n        serverLog(LL_VERBOSE, \"Garbage collected snapshot\");\n    }\n}\n\nvoid redisDbPersistentData::restoreSnapshot(const redisDbPersistentDataSnapshot *psnapshot)\n{\n    serverAssert(psnapshot->m_refCount == 1);\n    serverAssert(m_spdbSnapshotHOLDER.get() == psnapshot);\n    \n    m_pdbSnapshot = psnapshot;   // if it was deleted restore it\n    size_t expectedSize = psnapshot->size();\n    dictEmpty(m_pdict, nullptr);\n    dictEmpty(m_pdictTombstone, nullptr);\n    endSnapshot(psnapshot);\n    serverAssert(size() == expectedSize);\n}\n\n// This function is all about minimizing the amount of work done under global lock\n//  when there has been lots of changes since snapshot creation a naive endSnapshot()\n//  will block for a very long time and will cause latency spikes.\n//\n// Note that this function uses a lot more CPU time than a simple endSnapshot(), we\n//  have some internal heuristics to do a synchronous endSnapshot if it makes sense\nvoid redisDbPersistentData::endSnapshotAsync(const redisDbPersistentDataSnapshot *psnapshot)\n{\n    mstime_t latency;\n\n    aeAcquireLock();\n    while (dictIsRehashing(m_pdict) || dictIsRehashing(m_pdictTombstone)) {\n        dictRehashMilliseconds(m_pdict, 1);\n        dictRehashMilliseconds(m_pdictTombstone, 1);\n        // Give someone else a chance\n        aeReleaseLock();\n        usleep(300);\n        aeAcquireLock();\n    }\n    \n    latencyStartMonitor(latency);\n        if (m_pdbSnapshotASYNC && m_pdbSnapshotASYNC->m_mvccCheckpoint <= psnapshot->m_mvccCheckpoint)\n        {\n            // Free a stale async snapshot so consolidate_children can clean it up later\n            endSnapshot(m_pdbSnapshotASYNC);    // FAST: just a ref decrement\n            m_pdbSnapshotASYNC = nullptr;\n        }\n\n        size_t elements = dictSize(m_pdictTombstone);\n        // if neither dict is rehashing then the merge is O(1) so don't count the size\n        if (dictIsRehashing(psnapshot->m_pdict) || dictIsRehashing(m_pdict))\n            elements += dictSize(m_pdict);\n        if (elements < c_elementsSmallLimit || psnapshot != m_spdbSnapshotHOLDER.get())  // heuristic\n        {\n            // For small snapshots it makes more sense just to merge it directly\n            endSnapshot(psnapshot);\n            latencyEndMonitor(latency);\n            latencyAddSampleIfNeeded(\"end-snapshot-async-synchronous-path\", latency);\n            aeReleaseLock();\n            return;\n        }\n\n        // OK this is a big snapshot so lets do the merge work outside the lock\n        auto psnapshotT = createSnapshot(LLONG_MAX, false);\n        endSnapshot(psnapshot); // this will just dec the ref count since our new snapshot has a ref \n        psnapshot = nullptr;\n\n    latencyEndMonitor(latency);\n    latencyAddSampleIfNeeded(\"end-snapshot-async-phase-1\", latency);\n    aeReleaseLock();\n\n    // do the expensive work of merging snapshots outside the ref\n    if (const_cast<redisDbPersistentDataSnapshot*>(psnapshotT)->freeTombstoneObjects(1))    // depth is one because we just creted it\n    {\n        aeAcquireLock();\n        if (m_pdbSnapshotASYNC != nullptr)\n            endSnapshot(m_pdbSnapshotASYNC);\n        m_pdbSnapshotASYNC = nullptr;\n        endSnapshot(psnapshotT);\n        aeReleaseLock();\n        return;\n    }\n    \n    // Final Cleanup\n    aeAcquireLock(); latencyStartMonitor(latency);\n        if (m_pdbSnapshotASYNC == nullptr)\n            m_pdbSnapshotASYNC = psnapshotT;\n        else\n            endSnapshot(psnapshotT);    // finally clean up our temp snapshot\n\n    latencyEndMonitor(latency);\n    latencyAddSampleIfNeeded(\"end-snapshot-async-phase-2\", latency);\n    aeReleaseLock();\n}\n\nbool redisDbPersistentDataSnapshot::freeTombstoneObjects(int depth)\n{\n    if (m_pdbSnapshot == nullptr)\n    {\n        serverAssert(dictSize(m_pdictTombstone) == 0);\n        return true;\n    }\n\n    if (!const_cast<redisDbPersistentDataSnapshot*>(m_pdbSnapshot)->freeTombstoneObjects(depth+1))\n        return false;\n\n    {\n    AeLocker ae;\n    ae.arm(nullptr);\n    if (m_pdbSnapshot->m_refCount != depth && (m_pdbSnapshot->m_refCount != (m_refCount+1)))\n        return false;\n    ae.disarm();\n    }\n\n    std::unique_lock<fastlock> lock(s_lock, std::defer_lock);\n    if (!lock.try_lock())\n        return false; // this is a best effort function\n    \n    std::unique_ptr<LazyFree> splazy = std::make_unique<LazyFree>();\n\n    dict *dictTombstoneNew = dictCreate(&dbTombstoneDictType, nullptr);\n    dictIterator *di = dictGetIterator(m_pdictTombstone);\n    dictEntry *de;\n    std::vector<dictEntry*> vecdeFree;\n    vecdeFree.reserve(dictSize(m_pdictTombstone));\n    unsigned rgcremoved[2] = {0};\n    while ((de = dictNext(di)) != nullptr)\n    {\n        dictEntry **dePrev = nullptr;\n        dictht *ht = nullptr;\n        sds key = (sds)dictGetKey(de);\n        // BUG BUG: Why can't we do a shallow search here?\n        dictEntry *deObj = dictFindWithPrev(m_pdbSnapshot->m_pdict, key, (uint64_t)dictGetVal(de), &dePrev, &ht, false);\n\n        if (deObj != nullptr)\n        {\n            // Now unlink the DE\n            __atomic_store(dePrev, &deObj->next, __ATOMIC_RELEASE);\n            if (ht == &m_pdbSnapshot->m_pdict->ht[0])\n                rgcremoved[0]++;\n            else\n                rgcremoved[1]++;\n            splazy->vecde.push_back(deObj);\n        } else {\n            serverAssert(dictFind(m_pdbSnapshot->m_pdict, key) == nullptr);\n            serverAssert(m_pdbSnapshot->find_cached_threadsafe(key) != nullptr);\n            dictAdd(dictTombstoneNew, sdsdupshared((sds)dictGetKey(de)), dictGetVal(de));\n        }\n    }\n    dictReleaseIterator(di);\n\n    dictForceRehash(dictTombstoneNew);\n    aeAcquireLock();\n    if (m_pdbSnapshot->m_pdict->asyncdata != nullptr) {\n        // In this case we use the asyncdata to free us, not our own lazy free\n        for (auto de : splazy->vecde)\n            dictFreeUnlinkedEntry(m_pdbSnapshot->m_pdict, de);\n        splazy->vecde.clear();\n    }\n    dict *dT = m_pdbSnapshot->m_pdict;\n    splazy->vecdictLazyFree.push_back(m_pdictTombstone);\n    __atomic_store(&m_pdictTombstone, &dictTombstoneNew, __ATOMIC_RELEASE);\n    __atomic_fetch_sub(&dT->ht[0].used, rgcremoved[0], __ATOMIC_RELEASE);\n    __atomic_fetch_sub(&dT->ht[1].used, rgcremoved[1], __ATOMIC_RELEASE);\n    serverLog(LL_WARNING, \"tombstones removed: %u, remain: %lu\", rgcremoved[0]+rgcremoved[1], dictSize(m_pdictTombstone));\n    g_pserver->garbageCollector.enqueue(serverTL->gcEpoch, std::move(splazy));\n    aeReleaseLock();\n    \n    return true;\n}\n\nvoid redisDbPersistentData::endSnapshot(const redisDbPersistentDataSnapshot *psnapshot)\n{\n    serverAssert(GlobalLocksAcquired());\n    \n    if (m_spdbSnapshotHOLDER.get() != psnapshot)\n    {\n        if (m_spdbSnapshotHOLDER == nullptr)\n        {\n            // This is an orphaned snapshot\n            redisDbPersistentDataSnapshot::gcDisposeSnapshot(const_cast<redisDbPersistentDataSnapshot*>(psnapshot));\n            return;\n        }\n        m_spdbSnapshotHOLDER->endSnapshot(psnapshot);\n        return;\n    }\n\n    mstime_t latency_endsnapshot;\n    latencyStartMonitor(latency_endsnapshot);\n\n    // Alright we're ready to be free'd, but first dump all the refs on our child snapshots\n    if (m_spdbSnapshotHOLDER->m_refCount == 1)\n        recursiveFreeSnapshots(m_spdbSnapshotHOLDER.get());\n\n    m_spdbSnapshotHOLDER->m_refCount--;\n    if (m_spdbSnapshotHOLDER->m_refCount > 0)\n        return;\n\n    size_t sizeStart = size();\n    serverAssert(m_spdbSnapshotHOLDER->m_refCount == 0);\n    serverAssert((m_refCount == 0 && m_pdict->pauserehash == 0) || (m_refCount != 0 && m_pdict->pauserehash == 1));\n\n    serverAssert(m_spdbSnapshotHOLDER->m_pdict->pauserehash == 1);  // All iterators should have been free'd except the fake one from createSnapshot\n    if (m_refCount == 0)\n    {\n        dictResumeRehashing(m_spdbSnapshotHOLDER->m_pdict);\n    }\n\n    if (m_pdbSnapshot == nullptr)\n    {\n        // the database was cleared so we don't need to recover the snapshot\n        dictEmpty(m_pdictTombstone, nullptr);\n        m_spdbSnapshotHOLDER = std::move(m_spdbSnapshotHOLDER->m_spdbSnapshotHOLDER);\n        return;\n    }\n\n    // Stage 1 Loop through all the tracked deletes and remove them from the snapshot DB\n    dictIterator *di = dictGetIterator(m_pdictTombstone);\n    dictEntry *de;\n    dictPauseRehashing(m_spdbSnapshotHOLDER->m_pdict);\n    auto splazy = std::make_unique<LazyFree>();\n    while ((de = dictNext(di)) != NULL)\n    {\n        dictEntry **dePrev;\n        dictht *ht;\n        // BUG BUG Why not a shallow search?\n        dictEntry *deSnapshot = dictFindWithPrev(m_spdbSnapshotHOLDER->m_pdict, dictGetKey(de), (uint64_t)dictGetVal(de), &dePrev, &ht, false /*!!sdsisshared((sds)dictGetKey(de))*/);\n        if (deSnapshot == nullptr && m_spdbSnapshotHOLDER->m_pdbSnapshot)\n        {\n            // The tombstone is for a grand child, propogate it (or possibly in the storage provider - but an extra tombstone won't hurt)\n#ifdef CHECKED_BUILD\n            serverAssert(m_spdbSnapshotHOLDER->m_pdbSnapshot->find_cached_threadsafe((const char*)dictGetKey(de)) != nullptr);\n#endif\n            dictAdd(m_spdbSnapshotHOLDER->m_pdictTombstone, sdsdupshared((sds)dictGetKey(de)), dictGetVal(de));\n            continue;\n        }\n        else if (deSnapshot == nullptr)\n        {\n            serverAssert(m_spdbSnapshotHOLDER->m_spstorage != nullptr); // the only case where we can have a tombstone without a snapshot child is if a storage engine is set\n            continue;\n        }\n        \n        // Delete the object from the source dict, we don't use dictDelete to avoid a second search\n        *dePrev = deSnapshot->next; // Unlink it first\n        if (deSnapshot != nullptr) {\n            if (m_spdbSnapshotHOLDER->m_pdict->asyncdata != nullptr) {\n                dictFreeUnlinkedEntry(m_spdbSnapshotHOLDER->m_pdict, deSnapshot);\n            } else {\n                splazy->vecde.push_back(deSnapshot);\n            }\n        }\n        ht->used--;\n    }\n\n    \n    dictResumeRehashing(m_spdbSnapshotHOLDER->m_pdict);\n    dictReleaseIterator(di);\n    splazy->vecdictLazyFree.push_back(m_pdictTombstone);\n    m_pdictTombstone = dictCreate(&dbTombstoneDictType, nullptr);\n\n    // Stage 2 Move all new keys to the snapshot DB\n    dictMerge(m_spdbSnapshotHOLDER->m_pdict, m_pdict);\n    \n    // Stage 3 swap the databases with the snapshot\n    std::swap(m_pdict, m_spdbSnapshotHOLDER->m_pdict);\n    if (m_spdbSnapshotHOLDER->m_pdbSnapshot != nullptr)\n        std::swap(m_pdictTombstone, m_spdbSnapshotHOLDER->m_pdictTombstone);\n    \n    // Finally free the snapshot\n    if (m_pdbSnapshot != nullptr && m_spdbSnapshotHOLDER->m_pdbSnapshot != nullptr)\n    {\n        m_pdbSnapshot = m_spdbSnapshotHOLDER->m_pdbSnapshot;\n    }\n    else\n    {\n        m_pdbSnapshot = nullptr;\n    }\n    m_spdbSnapshotHOLDER->m_pdbSnapshot = nullptr;\n\n    // Fixup the about to free'd snapshots iterator count so the dtor doesn't complain\n    if (m_refCount)\n    {\n        dictResumeRehashing(m_spdbSnapshotHOLDER->m_pdict);\n    }\n\n    auto spsnapshotFree = std::move(m_spdbSnapshotHOLDER);\n    m_spdbSnapshotHOLDER = std::move(spsnapshotFree->m_spdbSnapshotHOLDER);\n    if (serverTL != nullptr) {\n        g_pserver->garbageCollector.enqueue(serverTL->gcEpoch, std::move(spsnapshotFree));\n        g_pserver->garbageCollector.enqueue(serverTL->gcEpoch, std::move(splazy));\n    }\n\n    // Sanity Checks\n    serverAssert(m_spdbSnapshotHOLDER != nullptr || m_pdbSnapshot == nullptr);\n    serverAssert(m_pdbSnapshot == m_spdbSnapshotHOLDER.get() || m_pdbSnapshot == nullptr);\n    serverAssert((m_refCount == 0 && m_pdict->pauserehash == 0) || (m_refCount != 0 && m_pdict->pauserehash == 1));\n    serverAssert(m_spdbSnapshotHOLDER != nullptr || dictSize(m_pdictTombstone) == 0);\n    serverAssert(sizeStart == size());\n\n    latencyEndMonitor(latency_endsnapshot);\n    latencyAddSampleIfNeeded(\"end-mvcc-snapshot\", latency_endsnapshot);\n\n    performEvictions(false);\n}\n\ndict_iter redisDbPersistentDataSnapshot::random_cache_threadsafe(bool fPrimaryOnly) const\n{\n    if (size() == 0)\n        return dict_iter(nullptr);\n    if (!fPrimaryOnly && m_pdbSnapshot != nullptr && m_pdbSnapshot->size() > 0)\n    {\n        dict_iter iter(nullptr);\n        double pctInSnapshot = (double)m_pdbSnapshot->size() / (size() + m_pdbSnapshot->size());\n        double randval = (double)rand()/RAND_MAX;\n        if (randval <= pctInSnapshot)\n        {\n            return m_pdbSnapshot->random_cache_threadsafe();\n        }\n    }\n    if (dictSize(m_pdict) == 0)\n        return dict_iter(nullptr);\n    dictEntry *de = dictGetRandomKey(m_pdict);\n    return dict_iter(m_pdict, de);\n}\n\ndict_iter redisDbPersistentData::find_cached_threadsafe(const char *key) const\n{\n    dict *dictTombstone;\n    __atomic_load(&m_pdictTombstone, &dictTombstone, __ATOMIC_ACQUIRE);\n    dictEntry *de = dictFind(m_pdict, key);\n    if (de == nullptr && m_pdbSnapshot != nullptr && dictFind(dictTombstone, key) == nullptr)\n    {\n        auto itr = m_pdbSnapshot->find_cached_threadsafe(key);\n        if (itr != nullptr)\n            return itr;\n    }\n    return dict_iter(m_pdict, de);\n}\n\nstruct scan_callback_data\n{\n    dict *dictTombstone;\n    sds type;\n    list *keys;\n};\nvoid snapshot_scan_callback(void *privdata, const dictEntry *de)\n{\n    scan_callback_data *data = (scan_callback_data*)privdata;\n    if (data->dictTombstone != nullptr && dictFind(data->dictTombstone, dictGetKey(de)) != nullptr)\n        return;\n    \n    sds sdskey = (sds)dictGetKey(de);\n    if (data->type != nullptr)\n    {\n        if (strcasecmp(data->type, getObjectTypeName((robj*)dictGetVal(de))) != 0)\n            return;\n    }\n    listAddNodeHead(data->keys, createStringObject(sdskey, sdslen(sdskey)));\n}\nunsigned long redisDbPersistentDataSnapshot::scan_threadsafe(unsigned long iterator, long count, sds type, list *keys) const\n{\n    unsigned long iteratorReturn = 0;\n\n    scan_callback_data data;\n    data.dictTombstone = m_pdictTombstone;\n    data.keys = keys;\n    data.type = type;\n\n    const redisDbPersistentDataSnapshot *psnapshot;\n    __atomic_load(&m_pdbSnapshot, &psnapshot, __ATOMIC_ACQUIRE);\n    if (psnapshot != nullptr)\n    {\n        // Always process the snapshot first as we assume its bigger than we are\n        iteratorReturn = psnapshot->scan_threadsafe(iterator, count, type, keys);\n\n        // Just catch up with our snapshot\n        do\n        {\n            iterator = dictScan(m_pdict, iterator, snapshot_scan_callback, nullptr, &data);\n        } while (iterator != 0 && (iterator < iteratorReturn || iteratorReturn == 0));\n    }\n    else\n    {\n        long maxiterations = count * 10; // allow more iterations than keys for sparse tables\n        iteratorReturn = iterator;\n        do {\n            iteratorReturn = dictScan(m_pdict, iteratorReturn, snapshot_scan_callback, NULL, &data);\n        } while (iteratorReturn &&\n              maxiterations-- &&\n              listLength(keys) < (unsigned long)count);\n    }\n    \n    return iteratorReturn;\n}\n\nbool redisDbPersistentDataSnapshot::iterate_threadsafe(std::function<bool(const char*, robj_roptr o)> fn, bool fKeyOnly, bool fCacheOnly) const\n{\n    return iterate_threadsafe_core(fn, fKeyOnly, fCacheOnly, true);\n}\n\nbool redisDbPersistentDataSnapshot::iterate_threadsafe_core(std::function<bool(const char*, robj_roptr o)> &fn, bool fKeyOnly, bool fCacheOnly, bool fFirst) const\n{\n    // Take the size so we can ensure we visited every element exactly once\n    //  use volatile to ensure it's not checked too late.  This makes it more\n    //  likely we'll detect races (but it won't gurantee it)\n    aeAcquireLock();\n    dict *dictTombstone;\n    __atomic_load(&m_pdictTombstone, &dictTombstone, __ATOMIC_ACQUIRE);\n    volatile ssize_t celem = (ssize_t)size();\n    aeReleaseLock();\n\n    dictEntry *de = nullptr;\n    bool fResult = true;\n\n    dictIterator *di = dictGetSafeIterator(m_pdict);\n    while(fResult && ((de = dictNext(di)) != nullptr))\n    {\n        --celem;\n        robj *o = (robj*)dictGetVal(de);\n        if (!fn((const char*)dictGetKey(de), o))\n            fResult = false;\n    }\n    dictReleaseIterator(di);\n\n\n    if (m_spstorage != nullptr && !fCacheOnly)\n    {\n        bool fSawAll = fResult && m_spstorage->enumerate([&](const char *key, size_t cchKey, const void *data, size_t cbData){\n            sds sdsKey = sdsnewlen(key, cchKey);\n            dictEntry *de = dictFind(m_pdict, sdsKey);\n            bool fContinue = true;\n            if (de == nullptr)\n            {\n                robj *o = nullptr;\n                if (!fKeyOnly)\n                {\n                    size_t offset = 0;\n                    std::unique_ptr<expireEntry> spexpire = deserializeExpire((const char*)data, cbData, &offset);\n                    o = deserializeStoredObject(reinterpret_cast<const char*>(data)+offset, cbData-offset);\n                    o->SetFExpires(spexpire != nullptr);\n                    if (spexpire != nullptr) {\n                        o->expire = std::move(*spexpire);\n                    }\n                }\n                fContinue = fn(sdsKey, o);\n                if (o != nullptr)\n                    decrRefCount(o);\n            }\n            \n            sdsfree(sdsKey);\n            return fContinue;\n        });\n        return fSawAll;\n    }\n\n    const redisDbPersistentDataSnapshot *psnapshot;\n    __atomic_load(&m_pdbSnapshot, &psnapshot, __ATOMIC_ACQUIRE);\n    if (fResult && psnapshot != nullptr)\n    {\n        std::function<bool(const char*, robj_roptr o)> fnNew = [&fn, &celem, dictTombstone](const char *key, robj_roptr o) {\n            dictEntry *deTombstone = dictFind(dictTombstone, key);\n            if (deTombstone != nullptr)\n                return true;\n\n            // Alright it's a key in the use keyspace, lets ensure it and then pass it off\n            --celem;\n            return fn(key, o);\n        };\n        fResult = psnapshot->iterate_threadsafe_core(fnNew, fKeyOnly, fCacheOnly, false);\n    }\n\n    // we should have hit all keys or had a good reason not to\n    if (!(!fResult || celem == 0 || (m_spstorage && fCacheOnly)))\n        serverLog(LL_WARNING, \"celem: %ld\", celem);\n    serverAssert(!fResult || celem == 0 || (m_spstorage && fCacheOnly) || !fFirst);\n    return fResult;\n}\n\nint redisDbPersistentDataSnapshot::snapshot_depth() const\n{\n    if (m_pdbSnapshot)\n        return m_pdbSnapshot->snapshot_depth() + 1;\n    return 0;\n}\n\nbool redisDbPersistentDataSnapshot::FStale() const\n{\n    return ((getMvccTstamp() - m_mvccCheckpoint) >> MVCC_MS_SHIFT) >= static_cast<uint64_t>(g_pserver->snapshot_slip);\n}\n\nvoid dictGCAsyncFree(dictAsyncRehashCtl *async) {\n    if (async->deGCList != nullptr && serverTL != nullptr && !serverTL->gcEpoch.isReset()) {\n        auto splazy = std::make_unique<LazyFree>();\n        auto *de = async->deGCList;\n        while (de != nullptr) {\n            splazy->vecde.push_back(de);\n            de = de->next;\n        }\n        async->deGCList = nullptr;\n        g_pserver->garbageCollector.enqueue(serverTL->gcEpoch, std::move(splazy));\n    }\n    delete async;\n}"
  },
  {
    "path": "src/solarisfixes.h",
    "content": "/* Solaris specific fixes.\n *\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#if defined(__sun)\n\n#if defined(__GNUC__)\n#include <math.h>\n#undef isnan\n#define isnan(x) \\\n     __extension__({ __typeof (x) __x_a = (x); \\\n     __builtin_expect(__x_a != __x_a, 0); })\n\n#undef isfinite\n#define isfinite(x) \\\n     __extension__ ({ __typeof (x) __x_f = (x); \\\n     __builtin_expect(!isnan(__x_f - __x_f), 1); })\n\n#undef isinf\n#define isinf(x) \\\n     __extension__ ({ __typeof (x) __x_i = (x); \\\n     __builtin_expect(!isnan(__x_i) && !isfinite(__x_i), 0); })\n\n#define u_int uint\n#define u_int32_t uint32_t\n#endif /* __GNUC__ */\n\n#endif /* __sun */\n"
  },
  {
    "path": "src/sort.cpp",
    "content": "/* SORT command and helper functions.\n *\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n\n#include \"server.h\"\n#include \"pqsort.h\" /* Partial qsort for SORT+LIMIT */\n#include <math.h> /* isnan() */\n\nzskiplistNode* zslGetElementByRank(zskiplist *zsl, unsigned long rank);\n\nredisSortOperation *createSortOperation(int type, robj *pattern) {\n    redisSortOperation *so = (redisSortOperation*)zmalloc(sizeof(*so), MALLOC_LOCAL);\n    so->type = type;\n    so->pattern = pattern;\n    return so;\n}\n\n/* Return the value associated to the key with a name obtained using\n * the following rules:\n *\n * 1) The first occurrence of '*' in 'pattern' is substituted with 'subst'.\n *\n * 2) If 'pattern' matches the \"->\" string, everything on the left of\n *    the arrow is treated as the name of a hash field, and the part on the\n *    left as the key name containing a hash. The value of the specified\n *    field is returned.\n *\n * 3) If 'pattern' equals \"#\", the function simply returns 'subst' itself so\n *    that the SORT command can be used like: SORT key GET # to retrieve\n *    the Set/List elements directly.\n *\n * The returned object will always have its refcount increased by 1\n * when it is non-NULL. */\nrobj *lookupKeyByPattern(redisDb *db, robj *pattern, robj *subst, int writeflag) {\n    char *p, *f, *k;\n    sds spat, ssub;\n    robj *keyobj, *fieldobj = NULL;\n    robj_roptr o;\n    int prefixlen, sublen, postfixlen, fieldlen;\n\n    /* If the pattern is \"#\" return the substitution object itself in order\n     * to implement the \"SORT ... GET #\" feature. */\n    spat = szFromObj(pattern);\n    if (spat[0] == '#' && spat[1] == '\\0') {\n        incrRefCount(subst);\n        return subst;\n    }\n\n    /* The substitution object may be specially encoded. If so we create\n     * a decoded object on the fly. Otherwise getDecodedObject will just\n     * increment the ref count, that we'll decrement later. */\n    subst = getDecodedObject(subst);\n    ssub = szFromObj(subst);\n\n    /* If we can't find '*' in the pattern we return NULL as to GET a\n     * fixed key does not make sense. */\n    p = strchr(spat,'*');\n    if (!p) {\n        decrRefCount(subst);\n        return NULL;\n    }\n\n    /* Find out if we're dealing with a hash dereference. */\n    if ((f = strstr(p+1, \"->\")) != NULL && *(f+2) != '\\0') {\n        fieldlen = sdslen(spat)-(f-spat)-2;\n        fieldobj = createStringObject(f+2,fieldlen);\n    } else {\n        fieldlen = 0;\n    }\n\n    /* Perform the '*' substitution. */\n    prefixlen = p-spat;\n    sublen = sdslen(ssub);\n    postfixlen = sdslen(spat)-(prefixlen+1)-(fieldlen ? fieldlen+2 : 0);\n    keyobj = createStringObject(NULL,prefixlen+sublen+postfixlen);\n    k = szFromObj(keyobj);\n    memcpy(k,spat,prefixlen);\n    memcpy(k+prefixlen,ssub,sublen);\n    memcpy(k+prefixlen+sublen,p+1,postfixlen);\n    decrRefCount(subst); /* Incremented by decodeObject() */\n\n    /* Lookup substituted key */\n    if (!writeflag)\n        o = lookupKeyRead(db,keyobj);\n    else\n        o = lookupKeyWrite(db,keyobj);\n    if (o == nullptr) goto noobj;\n\n    if (fieldobj) {\n        if (o->type != OBJ_HASH) goto noobj;\n\n        /* Retrieve value from hash by the field name. The returned object\n         * is a new object with refcount already incremented. */\n        o = hashTypeGetValueObject(o, szFromObj(fieldobj));\n    } else {\n        if (o->type != OBJ_STRING) goto noobj;\n\n        /* Every object that this function returns needs to have its refcount\n         * increased. sortCommand decreases it again. */\n        incrRefCount(o);\n    }\n    decrRefCount(keyobj);\n    if (fieldobj) decrRefCount(fieldobj);\n    return o.unsafe_robjcast();\n\nnoobj:\n    decrRefCount(keyobj);\n    if (fieldlen) decrRefCount(fieldobj);\n    return NULL;\n}\n\n/* sortCompare() is used by qsort in sortCommand(). Given that qsort_r with\n * the additional parameter is not standard but a BSD-specific we have to\n * pass sorting parameters via the global 'server' structure */\nint sortCompare(const void *s1, const void *s2) {\n    const redisSortObject *so1 = (redisSortObject*)s1, *so2 = (redisSortObject*)s2;\n    int cmp;\n\n    if (!g_pserver->sort_alpha) {\n        /* Numeric sorting. Here it's trivial as we precomputed scores */\n        if (so1->u.score > so2->u.score) {\n            cmp = 1;\n        } else if (so1->u.score < so2->u.score) {\n            cmp = -1;\n        } else {\n            /* Objects have the same score, but we don't want the comparison\n             * to be undefined, so we compare objects lexicographically.\n             * This way the result of SORT is deterministic. */\n            cmp = compareStringObjects(so1->obj,so2->obj);\n        }\n    } else {\n        /* Alphanumeric sorting */\n        if (g_pserver->sort_bypattern) {\n            if (!so1->u.cmpobj || !so2->u.cmpobj) {\n                /* At least one compare object is NULL */\n                if (so1->u.cmpobj == so2->u.cmpobj)\n                    cmp = 0;\n                else if (so1->u.cmpobj == NULL)\n                    cmp = -1;\n                else\n                    cmp = 1;\n            } else {\n                /* We have both the objects, compare them. */\n                if (g_pserver->sort_store) {\n                    cmp = compareStringObjects(so1->u.cmpobj,so2->u.cmpobj);\n                } else {\n                    /* Here we can use strcoll() directly as we are sure that\n                     * the objects are decoded string objects. */\n                    cmp = strcoll(szFromObj(so1->u.cmpobj),szFromObj(so2->u.cmpobj));\n                }\n            }\n        } else {\n            /* Compare elements directly. */\n            if (g_pserver->sort_store) {\n                cmp = compareStringObjects(so1->obj,so2->obj);\n            } else {\n                cmp = collateStringObjects(so1->obj,so2->obj);\n            }\n        }\n    }\n    return g_pserver->sort_desc ? -cmp : cmp;\n}\n\n/* The SORT command is the most complex command in Redis. Warning: this code\n * is optimized for speed and a bit less for readability */\nvoid sortCommand(client *c) {\n    list *operations;\n    unsigned int outputlen = 0;\n    int desc = 0, alpha = 0;\n    long limit_start = 0, limit_count = -1, start, end, vectorlen;\n    int j, dontsort = 0;\n    int getop = 0; /* GET operation counter */\n    int int_conversion_error = 0;\n    int syntax_error = 0;\n    robj *sortval, *sortby = NULL, *storekey = NULL;\n    redisSortObject *vector; /* Resulting vector to sort */\n\n    /* Create a list of operations to perform for every sorted element.\n     * Operations can be GET */\n    operations = listCreate();\n    listSetFreeMethod(operations,zfree);\n    j = 2; /* options start at argv[2] */\n\n    /* The SORT command has an SQL-alike syntax, parse it */\n    while(j < c->argc) {\n        int leftargs = c->argc-j-1;\n        if (!strcasecmp(szFromObj(c->argv[j]),\"asc\")) {\n            desc = 0;\n        } else if (!strcasecmp(szFromObj(c->argv[j]),\"desc\")) {\n            desc = 1;\n        } else if (!strcasecmp(szFromObj(c->argv[j]),\"alpha\")) {\n            alpha = 1;\n        } else if (!strcasecmp(szFromObj(c->argv[j]),\"limit\") && leftargs >= 2) {\n            if ((getLongFromObjectOrReply(c, c->argv[j+1], &limit_start, NULL)\n                 != C_OK) ||\n                (getLongFromObjectOrReply(c, c->argv[j+2], &limit_count, NULL)\n                 != C_OK))\n            {\n                syntax_error++;\n                break;\n            }\n            j+=2;\n        } else if (!strcasecmp(szFromObj(c->argv[j]),\"store\") && leftargs >= 1) {\n            storekey = c->argv[j+1];\n            j++;\n        } else if (!strcasecmp(szFromObj(c->argv[j]),\"by\") && leftargs >= 1) {\n            sortby = c->argv[j+1];\n            /* If the BY pattern does not contain '*', i.e. it is constant,\n             * we don't need to sort nor to lookup the weight keys. */\n            if (strchr(szFromObj(c->argv[j+1]),'*') == NULL) {\n                dontsort = 1;\n            } else {\n                /* If BY is specified with a real patter, we can't accept\n                 * it in cluster mode. */\n                if (g_pserver->cluster_enabled) {\n                    addReplyError(c,\"BY option of SORT denied in Cluster mode.\");\n                    syntax_error++;\n                    break;\n                }\n            }\n            j++;\n        } else if (!strcasecmp(szFromObj(c->argv[j]),\"get\") && leftargs >= 1) {\n            if (g_pserver->cluster_enabled) {\n                addReplyError(c,\"GET option of SORT denied in Cluster mode.\");\n                syntax_error++;\n                break;\n            }\n            listAddNodeTail(operations,createSortOperation(\n                SORT_OP_GET,c->argv[j+1]));\n            getop++;\n            j++;\n        } else {\n            addReplyErrorObject(c,shared.syntaxerr);\n            syntax_error++;\n            break;\n        }\n        j++;\n    }\n\n    /* Handle syntax errors set during options parsing. */\n    if (syntax_error) {\n        listRelease(operations);\n        return;\n    }\n\n    /* Lookup the key to sort. It must be of the right types */\n    if (!storekey)\n        sortval = lookupKeyRead(c->db,c->argv[1]).unsafe_robjcast();\n    else\n        sortval = lookupKeyWrite(c->db,c->argv[1]);\n    if (sortval && sortval->type != OBJ_SET &&\n                   sortval->type != OBJ_LIST &&\n                   sortval->type != OBJ_ZSET)\n    {\n        listRelease(operations);\n        addReplyErrorObject(c,shared.wrongtypeerr);\n        return;\n    }\n\n    /* Now we need to protect sortval incrementing its count, in the future\n     * SORT may have options able to overwrite/delete keys during the sorting\n     * and the sorted key itself may get destroyed */\n    if (sortval)\n        incrRefCount(sortval);\n    else\n        sortval = createQuicklistObject();\n\n\n    /* When sorting a set with no sort specified, we must sort the output\n     * so the result is consistent across scripting and replication.\n     *\n     * The other types (list, sorted set) will retain their native order\n     * even if no sort order is requested, so they remain stable across\n     * scripting and replication. */\n    if (dontsort &&\n        sortval->type == OBJ_SET &&\n        (storekey || c->flags & CLIENT_LUA))\n    {\n        /* Force ALPHA sorting */\n        dontsort = 0;\n        alpha = 1;\n        sortby = NULL;\n    }\n\n    /* Destructively convert encoded sorted sets for SORT. */\n    if (sortval->type == OBJ_ZSET)\n        zsetConvert(sortval, OBJ_ENCODING_SKIPLIST);\n\n    /* Objtain the length of the object to sort. */\n    switch(sortval->type) {\n    case OBJ_LIST: vectorlen = listTypeLength(sortval); break;\n    case OBJ_SET: vectorlen =  setTypeSize(sortval); break;\n    case OBJ_ZSET: vectorlen = dictSize(((zset*)ptrFromObj(sortval))->dict); break;\n    default: vectorlen = 0; serverPanic(\"Bad SORT type\"); /* Avoid GCC warning */\n    }\n\n    /* Perform LIMIT start,count sanity checking.\n     * And avoid integer overflow by limiting inputs to object sizes. */\n    start = std::min(std::max(limit_start, (long)0), vectorlen);\n    limit_count = std::min(std::max(limit_count, (long)-1), vectorlen);\n    end = (limit_count < 0) ? vectorlen-1 : start+limit_count-1;\n    if (start >= vectorlen) {\n        start = vectorlen-1;\n        end = vectorlen-2;\n    }\n    if (end >= vectorlen) end = vectorlen-1;\n\n    /* Whenever possible, we load elements into the output array in a more\n     * direct way. This is possible if:\n     *\n     * 1) The object to sort is a sorted set or a list (internally sorted).\n     * 2) There is nothing to sort as dontsort is true (BY <constant string>).\n     *\n     * In this special case, if we have a LIMIT option that actually reduces\n     * the number of elements to fetch, we also optimize to just load the\n     * range we are interested in and allocating a vector that is big enough\n     * for the selected range length. */\n    if ((sortval->type == OBJ_ZSET || sortval->type == OBJ_LIST) &&\n        dontsort &&\n        (start != 0 || end != vectorlen-1))\n    {\n        vectorlen = end-start+1;\n    }\n\n    /* Load the sorting vector with all the objects to sort */\n    vector = (redisSortObject*)zmalloc(sizeof(redisSortObject)*vectorlen, MALLOC_LOCAL);\n    j = 0;\n\n    if (sortval->type == OBJ_LIST && dontsort) {\n        /* Special handling for a list, if 'dontsort' is true.\n         * This makes sure we return elements in the list original\n         * ordering, accordingly to DESC / ASC options.\n         *\n         * Note that in this case we also handle LIMIT here in a direct\n         * way, just getting the required range, as an optimization. */\n        if (end >= start) {\n            listTypeIterator *li;\n            listTypeEntry entry;\n            li = listTypeInitIterator(sortval,\n                    desc ? (long)(listTypeLength(sortval) - start - 1) : start,\n                    desc ? LIST_HEAD : LIST_TAIL);\n\n            while(j < vectorlen && listTypeNext(li,&entry)) {\n                vector[j].obj = listTypeGet(&entry);\n                vector[j].u.score = 0;\n                vector[j].u.cmpobj = NULL;\n                j++;\n            }\n            listTypeReleaseIterator(li);\n            /* Fix start/end: output code is not aware of this optimization. */\n            end -= start;\n            start = 0;\n        }\n    } else if (sortval->type == OBJ_LIST) {\n        listTypeIterator *li = listTypeInitIterator(sortval,0,LIST_TAIL);\n        listTypeEntry entry;\n        while(listTypeNext(li,&entry)) {\n            vector[j].obj = listTypeGet(&entry);\n            vector[j].u.score = 0;\n            vector[j].u.cmpobj = NULL;\n            j++;\n        }\n        listTypeReleaseIterator(li);\n    } else if (sortval->type == OBJ_SET) {\n        setTypeIterator *si = setTypeInitIterator(sortval);\n        sds sdsele;\n        while((sdsele = setTypeNextObject(si)) != NULL) {\n            vector[j].obj = createObject(OBJ_STRING,sdsele);\n            vector[j].u.score = 0;\n            vector[j].u.cmpobj = NULL;\n            j++;\n        }\n        setTypeReleaseIterator(si);\n    } else if (sortval->type == OBJ_ZSET && dontsort) {\n        /* Special handling for a sorted set, if 'dontsort' is true.\n         * This makes sure we return elements in the sorted set original\n         * ordering, accordingly to DESC / ASC options.\n         *\n         * Note that in this case we also handle LIMIT here in a direct\n         * way, just getting the required range, as an optimization. */\n\n        zset *zs = (zset*)ptrFromObj(sortval);\n        zskiplist *zsl = zs->zsl;\n        zskiplistNode *ln;\n        sds sdsele;\n        int rangelen = vectorlen;\n\n        /* Check if starting point is trivial, before doing log(N) lookup. */\n        if (desc) {\n            long zsetlen = dictSize(((zset*)ptrFromObj(sortval))->dict);\n\n            ln = zsl->tail;\n            if (start > 0)\n                ln = zslGetElementByRank(zsl,zsetlen-start);\n        } else {\n            ln = zsl->header->level(0)->forward;\n            if (start > 0)\n                ln = zslGetElementByRank(zsl,start+1);\n        }\n\n        while(rangelen--) {\n            serverAssertWithInfo(c,sortval,ln != NULL);\n            sdsele = ln->ele;\n            vector[j].obj = createStringObject(sdsele,sdslen(sdsele));\n            vector[j].u.score = 0;\n            vector[j].u.cmpobj = NULL;\n            j++;\n            ln = desc ? ln->backward : ln->level(0)->forward;\n        }\n        /* Fix start/end: output code is not aware of this optimization. */\n        end -= start;\n        start = 0;\n    } else if (sortval->type == OBJ_ZSET) {\n        dict *set = ((zset*)ptrFromObj(sortval))->dict;\n        dictIterator *di;\n        dictEntry *setele;\n        sds sdsele;\n        di = dictGetIterator(set);\n        while((setele = dictNext(di)) != NULL) {\n            sdsele = (sds)dictGetKey(setele);\n            vector[j].obj = createStringObject(sdsele,sdslen(sdsele));\n            vector[j].u.score = 0;\n            vector[j].u.cmpobj = NULL;\n            j++;\n        }\n        dictReleaseIterator(di);\n    } else {\n        serverPanic(\"Unknown type\");\n    }\n    serverAssertWithInfo(c,sortval,j == vectorlen);\n\n    /* Now it's time to load the right scores in the sorting vector */\n    if (!dontsort) {\n        for (j = 0; j < vectorlen; j++) {\n            robj *byval;\n            if (sortby) {\n                /* lookup value to sort by */\n                byval = lookupKeyByPattern(c->db,sortby,vector[j].obj,storekey!=NULL);\n                if (!byval) continue;\n            } else {\n                /* use object itself to sort by */\n                byval = vector[j].obj;\n            }\n\n            if (alpha) {\n                if (sortby) vector[j].u.cmpobj = getDecodedObject(byval);\n            } else {\n                if (sdsEncodedObject(byval)) {\n                    char *eptr;\n\n                    vector[j].u.score = strtod(szFromObj(byval),&eptr);\n                    if (eptr[0] != '\\0' || errno == ERANGE ||\n                        std::isnan(vector[j].u.score))\n                    {\n                        int_conversion_error = 1;\n                    }\n                } else if (byval->encoding == OBJ_ENCODING_INT) {\n                    /* Don't need to decode the object if it's\n                     * integer-encoded (the only encoding supported) so\n                     * far. We can just cast it */\n                    vector[j].u.score = (long)byval->m_ptr;\n                } else {\n                    serverAssertWithInfo(c,sortval,1 != 1);\n                }\n            }\n\n            /* when the object was retrieved using lookupKeyByPattern,\n             * its refcount needs to be decreased. */\n            if (sortby) {\n                decrRefCount(byval);\n            }\n        }\n\n        g_pserver->sort_desc = desc;\n        g_pserver->sort_alpha = alpha;\n        g_pserver->sort_bypattern = sortby ? 1 : 0;\n        g_pserver->sort_store = storekey ? 1 : 0;\n        if (sortby && (start != 0 || end != vectorlen-1))\n            pqsort(vector,vectorlen,sizeof(redisSortObject),sortCompare, start,end);\n        else\n            qsort(vector,vectorlen,sizeof(redisSortObject),sortCompare);\n    }\n\n    /* Send command output to the output buffer, performing the specified\n     * GET/DEL/INCR/DECR operations if any. */\n    outputlen = getop ? getop*(end-start+1) : end-start+1;\n    if (int_conversion_error) {\n        addReplyError(c,\"One or more scores can't be converted into double\");\n    } else if (storekey == NULL) {\n        /* STORE option not specified, sent the sorting result to client */\n        addReplyArrayLen(c,outputlen);\n        for (j = start; j <= end; j++) {\n            listNode *ln;\n            listIter li;\n\n            if (!getop) addReplyBulk(c,vector[j].obj);\n            listRewind(operations,&li);\n            while((ln = listNext(&li))) {\n                redisSortOperation *sop = (redisSortOperation*)ln->value;\n                robj *val = lookupKeyByPattern(c->db,sop->pattern,\n                    vector[j].obj,storekey!=NULL);\n\n                if (sop->type == SORT_OP_GET) {\n                    if (!val) {\n                        addReplyNull(c);\n                    } else {\n                        addReplyBulk(c,val);\n                        decrRefCount(val);\n                    }\n                } else {\n                    /* Always fails */\n                    serverAssertWithInfo(c,sortval,sop->type == SORT_OP_GET);\n                }\n            }\n        }\n    } else {\n        robj *sobj = createQuicklistObject();\n\n        /* STORE option specified, set the sorting result as a List object */\n        for (j = start; j <= end; j++) {\n            listNode *ln;\n            listIter li;\n\n            if (!getop) {\n                listTypePush(sobj,vector[j].obj,LIST_TAIL);\n            } else {\n                listRewind(operations,&li);\n                while((ln = listNext(&li))) {\n                    redisSortOperation *sop = (redisSortOperation*)ln->value;\n                    robj *val = lookupKeyByPattern(c->db,sop->pattern,\n                        vector[j].obj,storekey!=NULL);\n\n                    if (sop->type == SORT_OP_GET) {\n                        if (!val) val = createStringObject(\"\",0);\n\n                        /* listTypePush does an incrRefCount, so we should take care\n                         * care of the incremented refcount caused by either\n                         * lookupKeyByPattern or createStringObject(\"\",0) */\n                        listTypePush(sobj,val,LIST_TAIL);\n                        decrRefCount(val);\n                    } else {\n                        /* Always fails */\n                        serverAssertWithInfo(c,sortval,sop->type == SORT_OP_GET);\n                    }\n                }\n            }\n        }\n        if (outputlen) {\n            setKey(c,c->db,storekey,sobj);\n            notifyKeyspaceEvent(NOTIFY_LIST,\"sortstore\",storekey,\n                                c->db->id);\n            g_pserver->dirty += outputlen;\n        } else if (dbDelete(c->db,storekey)) {\n            signalModifiedKey(c,c->db,storekey);\n            notifyKeyspaceEvent(NOTIFY_GENERIC,\"del\",storekey,c->db->id);\n            g_pserver->dirty++;\n        }\n        decrRefCount(sobj);\n        addReplyLongLong(c,outputlen);\n    }\n\n    /* Cleanup */\n    for (j = 0; j < vectorlen; j++)\n        decrRefCount(vector[j].obj);\n\n    decrRefCount(sortval);\n    listRelease(operations);\n    for (j = 0; j < vectorlen; j++) {\n        if (alpha && vector[j].u.cmpobj)\n            decrRefCount(vector[j].u.cmpobj);\n    }\n    zfree(vector);\n}\n"
  },
  {
    "path": "src/sparkline.cpp",
    "content": "/* sparkline.c -- ASCII Sparklines\n * This code is modified from http://github.com/antirez/aspark and adapted\n * in order to return SDS strings instead of outputting directly to\n * the terminal.\n *\n * ---------------------------------------------------------------------------\n *\n * Copyright(C) 2011-2014 Salvatore Sanfilippo <antirez@gmail.com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n\n#include <math.h>\n\n/* This is the charset used to display the graphs, but multiple rows are used\n * to increase the resolution. */\nstatic char charset[] = \"_-`\";\nstatic char charset_fill[] = \"_o#\";\nstatic int charset_len = sizeof(charset)-1;\nstatic int label_margin_top = 1;\n\n/* ----------------------------------------------------------------------------\n * Sequences are arrays of samples we use to represent data to turn\n * into sparklines. This is the API in order to generate a sparkline:\n *\n * struct sequence *seq = createSparklineSequence();\n * sparklineSequenceAddSample(seq, 10, NULL);\n * sparklineSequenceAddSample(seq, 20, NULL);\n * sparklineSequenceAddSample(seq, 30, \"last sample label\");\n * sds output = sparklineRender(sdsempty(), seq, 80, 4, SPARKLINE_FILL);\n * freeSparklineSequence(seq);\n * ------------------------------------------------------------------------- */\n\n/* Create a new sequence. */\nstruct sequence *createSparklineSequence(void) {\n    struct sequence *seq = (sequence*)zmalloc(sizeof(*seq), MALLOC_LOCAL);\n    seq->length = 0;\n    seq->samples = NULL;\n    return seq;\n}\n\n/* Add a new sample into a sequence. */\nvoid sparklineSequenceAddSample(struct sequence *seq, double value, char *label) {\n    label = (label == NULL || label[0] == '\\0') ? NULL : zstrdup(label);\n    if (seq->length == 0) {\n        seq->min = seq->max = value;\n    } else {\n        if (value < seq->min) seq->min = value;\n        else if (value > seq->max) seq->max = value;\n    }\n    seq->samples = (sample*)zrealloc(seq->samples,sizeof(struct sample)*(seq->length+1), MALLOC_SHARED);\n    seq->samples[seq->length].value = value;\n    seq->samples[seq->length].label = label;\n    seq->length++;\n    if (label) seq->labels++;\n}\n\n/* Free a sequence. */\nvoid freeSparklineSequence(struct sequence *seq) {\n    int j;\n\n    for (j = 0; j < seq->length; j++)\n        zfree(seq->samples[j].label);\n    zfree(seq->samples);\n    zfree(seq);\n}\n\n/* ----------------------------------------------------------------------------\n * ASCII rendering of sequence\n * ------------------------------------------------------------------------- */\n\n/* Render part of a sequence, so that render_sequence() call call this function\n * with different parts in order to create the full output without overflowing\n * the current terminal columns. */\nsds sparklineRenderRange(sds output, struct sequence *seq, int rows, int offset, int len, int flags) {\n    int j;\n    double relmax = seq->max - seq->min;\n    int steps = charset_len*rows;\n    int row = 0;\n    char *chars = (char*)zmalloc(len, MALLOC_LOCAL);\n    int loop = 1;\n    int opt_fill = flags & SPARKLINE_FILL;\n    int opt_log = flags & SPARKLINE_LOG_SCALE;\n\n    if (opt_log) {\n        relmax = log(relmax+1);\n    } else if (relmax == 0) {\n        relmax = 1;\n    }\n\n    while(loop) {\n        loop = 0;\n        memset(chars,' ',len);\n        for (j = 0; j < len; j++) {\n            struct sample *s = &seq->samples[j+offset];\n            double relval = s->value - seq->min;\n            int step;\n\n            if (opt_log) relval = log(relval+1);\n            step = (int) (relval*steps)/relmax;\n            if (step < 0) step = 0;\n            if (step >= steps) step = steps-1;\n\n            if (row < rows) {\n                /* Print the character needed to create the sparkline */\n                int charidx = step-((rows-row-1)*charset_len);\n                loop = 1;\n                if (charidx >= 0 && charidx < charset_len) {\n                    chars[j] = opt_fill ? charset_fill[charidx] :\n                                          charset[charidx];\n                } else if(opt_fill && charidx >= charset_len) {\n                    chars[j] = '|';\n                }\n            } else {\n                /* Labels spacing */\n                if (seq->labels && row-rows < label_margin_top) {\n                    loop = 1;\n                    break;\n                }\n                /* Print the label if needed. */\n                if (s->label) {\n                    int label_len = strlen(s->label);\n                    int label_char = row - rows - label_margin_top;\n\n                    if (label_len > label_char) {\n                        loop = 1;\n                        chars[j] = s->label[label_char];\n                    }\n                }\n            }\n        }\n        if (loop) {\n            row++;\n            output = sdscatlen(output,chars,len);\n            output = sdscatlen(output,\"\\n\",1);\n        }\n    }\n    zfree(chars);\n    return output;\n}\n\n/* Turn a sequence into its ASCII representation */\nsds sparklineRender(sds output, struct sequence *seq, int columns, int rows, int flags) {\n    int j;\n\n    for (j = 0; j < seq->length; j += columns) {\n        int sublen = (seq->length-j) < columns ? (seq->length-j) : columns;\n\n        if (j != 0) output = sdscatlen(output,\"\\n\",1);\n        output = sparklineRenderRange(output, seq, rows, j, sublen, flags);\n    }\n    return output;\n}\n\n"
  },
  {
    "path": "src/sparkline.h",
    "content": "/* sparkline.h -- ASCII Sparklines header file\n *\n * ---------------------------------------------------------------------------\n *\n * Copyright(C) 2011-2014 Salvatore Sanfilippo <antirez@gmail.com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __SPARKLINE_H\n#define __SPARKLINE_H\n\n/* A sequence is represented of many \"samples\" */\nstruct sample {\n    double value;\n    char *label;\n};\n\nstruct sequence {\n    int length;\n    int labels;\n    struct sample *samples;\n    double min, max;\n};\n\n#define SPARKLINE_NO_FLAGS 0\n#define SPARKLINE_FILL 1      /* Fill the area under the curve. */\n#define SPARKLINE_LOG_SCALE 2 /* Use logarithmic scale. */\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\nstruct sequence *createSparklineSequence(void);\nvoid sparklineSequenceAddSample(struct sequence *seq, double value, char *label);\nvoid freeSparklineSequence(struct sequence *seq);\nsds sparklineRenderRange(sds output, struct sequence *seq, int rows, int offset, int len, int flags);\nsds sparklineRender(sds output, struct sequence *seq, int columns, int rows, int flags);\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif /* __SPARKLINE_H */\n"
  },
  {
    "path": "src/storage/rocksdb.cpp",
    "content": "#include \"rocksdb.h\"\n#include <string>\n#include <sstream>\n#include <mutex>\n#include <unistd.h>\n#include \"../server.h\"\n#include \"../cluster.h\"\n#include \"rocksdbfactor_internal.h\"\n\ntemplate class std::basic_string<char>;\n\nstatic const char keyprefix[] = INTERNAL_KEY_PREFIX;\n\nrocksdb::Options DefaultRocksDBOptions();\nextern \"C\" pid_t gettid();\n\nbool FInternalKey(const char *key, size_t cch)\n{\n    if (cch >= sizeof(INTERNAL_KEY_PREFIX))\n    {\n        if (memcmp(key, keyprefix, sizeof(INTERNAL_KEY_PREFIX)-1) == 0)\n            return true;\n    }\n    return false;\n}\n\nstd::string getPrefix(unsigned int hashslot)\n{\n    HASHSLOT_PREFIX_TYPE slot = HASHSLOT_PREFIX_ENDIAN((HASHSLOT_PREFIX_TYPE)hashslot);\n    char *hash_char = (char *)&slot;\n    return std::string(hash_char, HASHSLOT_PREFIX_BYTES);\n}\n\nstd::string prefixKey(const char *key, size_t cchKey)\n{\n    return FInternalKey(key, cchKey) ? std::string(key, cchKey) : getPrefix(keyHashSlot(key, cchKey)) + std::string(key, cchKey);\n}\n\nRocksDBStorageProvider::RocksDBStorageProvider(RocksDBStorageFactory *pfactory, std::shared_ptr<rocksdb::DB> &spdb, std::shared_ptr<rocksdb::ColumnFamilyHandle> &spcolfam, std::shared_ptr<rocksdb::ColumnFamilyHandle> &spexpirecolfam, const rocksdb::Snapshot *psnapshot, size_t count)\n    : m_pfactory(pfactory), m_spdb(spdb), m_psnapshot(psnapshot), m_spcolfamily(spcolfam), m_spexpirecolfamily(spexpirecolfam), m_count(count)\n{\n    m_readOptionsTemplate = rocksdb::ReadOptions();\n    m_readOptionsTemplate.verify_checksums = false;\n    m_readOptionsTemplate.snapshot = m_psnapshot;\n}\n\nvoid RocksDBStorageProvider::insert(const char *key, size_t cchKey, void *data, size_t cb, bool fOverwrite)\n{\n    rocksdb::Status status;\n    std::unique_lock<fastlock> l(m_lock);\n    std::string prefixed_key = prefixKey(key, cchKey);\n    if (m_spbatch != nullptr)\n        status = m_spbatch->Put(m_spcolfamily.get(), rocksdb::Slice(prefixed_key), rocksdb::Slice((const char*)data, cb));\n    else\n        status = m_spdb->Put(WriteOptions(), m_spcolfamily.get(), rocksdb::Slice(prefixed_key), rocksdb::Slice((const char*)data, cb));\n    if (!status.ok())\n        throw status.ToString();\n\n    if (!fOverwrite)\n        ++m_count;\n}\n\nvoid RocksDBStorageProvider::bulkInsert(char **rgkeys, size_t *rgcbkeys, char **rgvals, size_t *rgcbvals, size_t celem)\n{\n    if (celem >= 16384) {\n        rocksdb::Options options = DefaultRocksDBOptions();\n        rocksdb::SstFileWriter sst_file_writer(rocksdb::EnvOptions(), options, options.comparator);\n        std::string file_path = m_pfactory->getTempFolder() + \"/tmpIngest.\";\n        file_path += std::to_string(gettid());\n        file_path += \".sst\";\n\n        rocksdb::Status s = sst_file_writer.Open(file_path);\n        if (!s.ok())\n            goto LFallback;\n\n        // Insert rows into the SST file, note that inserted keys must be \n        // strictly increasing (based on options.comparator)\n        for (size_t ielem = 0; ielem < celem; ++ielem) {\n            std::string prefixed_key = prefixKey(rgkeys[ielem], rgcbkeys[ielem]);\n            s = sst_file_writer.Put(rocksdb::Slice(prefixed_key), rocksdb::Slice(rgvals[ielem], rgcbvals[ielem]));\n            if (!s.ok()) {\n                unlink(file_path.c_str());\n                goto LFallback;\n            }\n        }\n\n        s = sst_file_writer.Finish();\n        if (!s.ok()) {\n            unlink(file_path.c_str());\n            goto LFallback;\n        }\n\n        auto ingestOptions = rocksdb::IngestExternalFileOptions();\n        ingestOptions.move_files = true;\n        ingestOptions.write_global_seqno = false;\n        ingestOptions.failed_move_fall_back_to_copy = false;\n\n        // Ingest the external SST file into the DB\n        s = m_spdb->IngestExternalFile(m_spcolfamily.get(), {file_path}, ingestOptions);\n        if (!s.ok()) {\n            unlink(file_path.c_str());\n            goto LFallback;\n        }\n    } else {\n    LFallback:\n        auto spbatch = std::make_unique<rocksdb::WriteBatch>();\n        for (size_t ielem = 0; ielem < celem; ++ielem) {\n            std::string prefixed_key = prefixKey(rgkeys[ielem], rgcbkeys[ielem]);\n            spbatch->Put(m_spcolfamily.get(), rocksdb::Slice(prefixed_key), rocksdb::Slice(rgvals[ielem], rgcbvals[ielem]));\n        }\n        m_spdb->Write(WriteOptions(), spbatch.get());\n    }\n\n    std::unique_lock<fastlock> l(m_lock);\n    m_count += celem;\n}\n\nbool RocksDBStorageProvider::erase(const char *key, size_t cchKey)\n{\n    rocksdb::Status status;\n    std::unique_lock<fastlock> l(m_lock);\n    std::string prefixed_key = prefixKey(key, cchKey);\n    if (!FKeyExists(prefixed_key))\n        return false;\n    if (m_spbatch != nullptr)\n    {\n        status = m_spbatch->Delete(m_spcolfamily.get(), rocksdb::Slice(prefixed_key));\n    }\n    else\n    {\n        status = m_spdb->Delete(WriteOptions(), m_spcolfamily.get(), rocksdb::Slice(prefixed_key));\n    }\n    if (status.ok())\n        --m_count;\n    return status.ok();\n}\n\nvoid RocksDBStorageProvider::retrieve(const char *key, size_t cchKey, callbackSingle fn) const\n{\n    rocksdb::PinnableSlice slice;\n    std::string prefixed_key = prefixKey(key, cchKey);\n    auto status = m_spdb->Get(ReadOptions(), m_spcolfamily.get(), rocksdb::Slice(prefixed_key), &slice);\n    if (status.ok())\n        fn(key, cchKey, slice.data(), slice.size());\n}\n\nsize_t RocksDBStorageProvider::clear()\n{\n    size_t celem = count();\n    auto options = m_spdb->GetOptions(m_spcolfamily.get());\n    auto status = m_spdb->DropColumnFamily(m_spcolfamily.get());\n    auto strName = m_spcolfamily->GetName();\n\n    rocksdb::ColumnFamilyHandle *handle = nullptr;\n    rocksdb::ColumnFamilyOptions cf_options(options);\n\n    m_spdb->CreateColumnFamily(cf_options, strName, &handle);\n    m_spcolfamily = std::shared_ptr<rocksdb::ColumnFamilyHandle>(handle);\n\n    if (!status.ok())\n        throw status.ToString();\n   \n    status = m_spdb->DropColumnFamily(m_spexpirecolfamily.get());\n    strName = m_spexpirecolfamily->GetName();\n\n    m_spdb->CreateColumnFamily(cf_options, strName, &handle);\n    m_spexpirecolfamily = std::shared_ptr<rocksdb::ColumnFamilyHandle>(handle);\n\n    if (!status.ok())\n        throw status.ToString();\n\n    m_count = 0;\n    return celem;\n}\n\nsize_t RocksDBStorageProvider::count() const\n{\n    std::unique_lock<fastlock> l(m_lock);\n    return m_count;\n}\n\nbool RocksDBStorageProvider::enumerate(callback fn) const\n{\n    std::unique_ptr<rocksdb::Iterator> it = std::unique_ptr<rocksdb::Iterator>(m_spdb->NewIterator(ReadOptions(), m_spcolfamily.get()));\n    size_t count = 0;\n    for (it->SeekToFirst(); it->Valid(); it->Next()) {\n        if (FInternalKey(it->key().data(), it->key().size()))\n            continue;\n        ++count;\n        bool fContinue = fn(it->key().data()+HASHSLOT_PREFIX_BYTES, it->key().size()-HASHSLOT_PREFIX_BYTES, it->value().data(), it->value().size());\n        if (!fContinue)\n            break;\n    }\n    if (!it->Valid() && count != m_count)\n    {\n        if (const_cast<RocksDBStorageProvider*>(this)->m_count != count)\n            printf(\"WARNING: rocksdb count mismatch\");\n        const_cast<RocksDBStorageProvider*>(this)->m_count = count;\n    }\n    assert(it->status().ok()); // Check for any errors found during the scan\n    return !it->Valid();\n}\n\nbool RocksDBStorageProvider::enumerate_hashslot(callback fn, unsigned int hashslot) const\n{\n    std::string prefix = getPrefix(hashslot);\n    std::unique_ptr<rocksdb::Iterator> it = std::unique_ptr<rocksdb::Iterator>(m_spdb->NewIterator(ReadOptions(), m_spcolfamily.get()));\n    size_t count = 0;\n    for (it->Seek(prefix); it->Valid(); it->Next()) {\n        if (FInternalKey(it->key().data(), it->key().size()))\n            continue;\n        if (HASHSLOT_PREFIX_RECOVER(*(HASHSLOT_PREFIX_TYPE *)it->key().data()) != hashslot)\n            break;\n        ++count;\n        bool fContinue = fn(it->key().data()+HASHSLOT_PREFIX_BYTES, it->key().size()-HASHSLOT_PREFIX_BYTES, it->value().data(), it->value().size());\n        if (!fContinue)\n            break;\n    }\n    bool full_iter = !it->Valid() || (HASHSLOT_PREFIX_RECOVER(*(HASHSLOT_PREFIX_TYPE *)it->key().data()) != hashslot);\n    if (full_iter && count != g_pserver->cluster->slots_keys_count[hashslot])\n    {\n        printf(\"WARNING: rocksdb hashslot count mismatch\");\n    }\n    assert(!full_iter || count == g_pserver->cluster->slots_keys_count[hashslot]);\n    assert(it->status().ok()); // Check for any errors found during the scan\n    return full_iter;\n}\n\nvoid RocksDBStorageProvider::setExpire(const char *key, size_t cchKey, long long expire)\n{\n    rocksdb::Status status;\n    std::unique_lock<fastlock> l(m_lock);\n    long long beExpire = htobe64(expire);\n    std::string prefix((const char *)&beExpire,sizeof(long long));\n    std::string strKey(key, cchKey);\n    if (m_spbatch != nullptr)\n        status = m_spbatch->Put(m_spexpirecolfamily.get(), rocksdb::Slice(prefix + strKey), rocksdb::Slice(strKey));\n    else\n        status = m_spdb->Put(WriteOptions(), m_spexpirecolfamily.get(), rocksdb::Slice(prefix + strKey), rocksdb::Slice(strKey));\n    if (!status.ok())\n        throw status.ToString();\n}\n\nvoid RocksDBStorageProvider::removeExpire(const char *key, size_t cchKey, long long expire)\n{\n    rocksdb::Status status;\n    std::unique_lock<fastlock> l(m_lock);\n    long long beExpire = htobe64(expire);\n    std::string prefix((const char *)&beExpire,sizeof(long long));\n    std::string strKey(key, cchKey);\n    std::string fullKey = prefix + strKey;\n    if (!FExpireExists(fullKey))\n        return;\n    if (m_spbatch)\n        status = m_spbatch->Delete(m_spexpirecolfamily.get(), rocksdb::Slice(fullKey));\n    else\n        status = m_spdb->Delete(WriteOptions(), m_spexpirecolfamily.get(), rocksdb::Slice(fullKey));\n    if (!status.ok())\n        throw status.ToString();\n}\n\nstd::vector<std::string> RocksDBStorageProvider::getExpirationCandidates(unsigned int count)\n{\n    std::vector<std::string> result;\n    std::unique_ptr<rocksdb::Iterator> it = std::unique_ptr<rocksdb::Iterator>(m_spdb->NewIterator(ReadOptions(), m_spexpirecolfamily.get()));\n    for (it->SeekToFirst(); it->Valid() && result.size() < count; it->Next()) {\n        if (FInternalKey(it->key().data(), it->key().size()))\n            continue;\n        result.emplace_back(it->value().data(), it->value().size());\n    }\n    return result;\n}\n\nstd::string randomHashSlot() {\n    return getPrefix(genrand64_int63() % (1 << 16));\n}\n\nstd::vector<std::string> RocksDBStorageProvider::getEvictionCandidates(unsigned int count)\n{\n    std::vector<std::string> result;\n    if (g_pserver->maxmemory_policy & MAXMEMORY_FLAG_ALLKEYS) {\n        std::unique_ptr<rocksdb::Iterator> it = std::unique_ptr<rocksdb::Iterator>(m_spdb->NewIterator(ReadOptions(), m_spcolfamily.get()));\n        for (it->Seek(randomHashSlot()); it->Valid() && result.size() < count; it->Next()) {\n            if (FInternalKey(it->key().data(), it->key().size()))\n                continue;\n            result.emplace_back(it->key().data() + HASHSLOT_PREFIX_BYTES, it->key().size() - HASHSLOT_PREFIX_BYTES);\n        }\n    } else {\n        std::unique_ptr<rocksdb::Iterator> it = std::unique_ptr<rocksdb::Iterator>(m_spdb->NewIterator(ReadOptions(), m_spexpirecolfamily.get()));\n        for (it->SeekToFirst(); it->Valid() && result.size() < count; it->Next()) {\n            if (FInternalKey(it->key().data(), it->key().size()))\n                continue;\n            result.emplace_back(it->value().data(), it->value().size());\n        }\n    }\n    return result;\n}\n\nconst IStorage *RocksDBStorageProvider::clone() const\n{\n    std::unique_lock<fastlock> l(m_lock);\n    const rocksdb::Snapshot *psnapshot = const_cast<RocksDBStorageProvider*>(this)->m_spdb->GetSnapshot();\n    return new RocksDBStorageProvider(m_pfactory, const_cast<RocksDBStorageProvider*>(this)->m_spdb, const_cast<RocksDBStorageProvider*>(this)->m_spcolfamily, const_cast<RocksDBStorageProvider*>(this)->m_spexpirecolfamily, psnapshot, m_count);\n}\n\nRocksDBStorageProvider::~RocksDBStorageProvider()\n{\n    if (m_spbatch != nullptr)\n        endWriteBatch();\n    \n    if (m_spdb != nullptr && m_psnapshot == nullptr)\n    {\n        insert(count_key, sizeof(count_key), &m_count, sizeof(m_count), false);\n        flush();\n    }\n\n    if (m_spdb != nullptr)\n    {\n        if (m_psnapshot != nullptr)\n            m_spdb->ReleaseSnapshot(m_psnapshot);\n    }\n}\n\nrocksdb::WriteOptions RocksDBStorageProvider::WriteOptions() const\n{\n    auto opt = rocksdb::WriteOptions();\n    return opt;\n}\n\nvoid RocksDBStorageProvider::beginWriteBatch()\n{\n    m_lock.lock();\n    m_spbatch = std::make_unique<rocksdb::WriteBatchWithIndex>();\n}\n\nvoid RocksDBStorageProvider::endWriteBatch()\n{\n    m_spdb->Write(WriteOptions(), m_spbatch.get()->GetWriteBatch());\n    m_spbatch = nullptr;\n    m_lock.unlock();\n}\n\nvoid RocksDBStorageProvider::batch_lock()\n{\n    m_lock.lock();\n}\n\nvoid RocksDBStorageProvider::batch_unlock()\n{\n    m_lock.unlock();\n}\n\nvoid RocksDBStorageProvider::flush()\n{\n    m_spdb->SyncWAL();\n    m_spdb->Flush(rocksdb::FlushOptions());\n}\n\nbool RocksDBStorageProvider::FKeyExists(std::string& key) const\n{\n    rocksdb::PinnableSlice slice;\n    if (m_spbatch)\n        return m_spbatch->GetFromBatchAndDB(m_spdb.get(), ReadOptions(), m_spcolfamily.get(), rocksdb::Slice(key), &slice).ok();\n    return m_spdb->Get(ReadOptions(), m_spcolfamily.get(), rocksdb::Slice(key), &slice).ok();\n}\n\nbool RocksDBStorageProvider::FExpireExists(std::string& key) const\n{\n    rocksdb::PinnableSlice slice;\n    if (m_spbatch)\n        return m_spbatch->GetFromBatchAndDB(m_spdb.get(), ReadOptions(), m_spexpirecolfamily.get(), rocksdb::Slice(key), &slice).ok();\n    return m_spdb->Get(ReadOptions(), m_spexpirecolfamily.get(), rocksdb::Slice(key), &slice).ok();\n}"
  },
  {
    "path": "src/storage/rocksdb.h",
    "content": "#pragma once\n\n#include <memory>\n#include \"../IStorage.h\"\n#include <rocksdb/db.h>\n#include <rocksdb/utilities/write_batch_with_index.h>\n#ifdef __APPLE__\n#include <libkern/OSByteOrder.h>\n#define htole16 OSSwapHostToLittleInt16\n#define le16toh OSSwapLittleToHostInt16\n#define htobe64 OSSwapHostToBigInt64\n#else\n#include <endian.h>\n#endif\n#include \"../fastlock.h\"\n\n#define INTERNAL_KEY_PREFIX \"\\x00\\x04\\x03\\x00\\x05\\x02\\x04\"\n#define HASHSLOT_PREFIX_TYPE uint16_t\n#define HASHSLOT_PREFIX_BYTES sizeof(HASHSLOT_PREFIX_TYPE)\n#define HASHSLOT_PREFIX_ENDIAN htole16\n#define HASHSLOT_PREFIX_RECOVER le16toh\nstatic const char count_key[] = INTERNAL_KEY_PREFIX \"__keydb__count\\1\";\nstatic const char version_key[] = INTERNAL_KEY_PREFIX \"__keydb__version\\1\";\nstatic const char meta_key[] = INTERNAL_KEY_PREFIX \"__keydb__metadata\\1\";\nstatic const char last_expire_key[] = INTERNAL_KEY_PREFIX \"__keydb__last_expire_time\";\nclass RocksDBStorageFactory;\n\nclass RocksDBStorageProvider : public IStorage\n{\n    RocksDBStorageFactory *m_pfactory;\n    std::shared_ptr<rocksdb::DB> m_spdb;    // Note: This must be first so it is deleted last\n    std::unique_ptr<rocksdb::WriteBatchWithIndex> m_spbatch;\n    const rocksdb::Snapshot *m_psnapshot = nullptr;\n    std::shared_ptr<rocksdb::ColumnFamilyHandle> m_spcolfamily;\n    std::shared_ptr<rocksdb::ColumnFamilyHandle> m_spexpirecolfamily;\n    rocksdb::ReadOptions m_readOptionsTemplate;\n    size_t m_count = 0;\n    mutable fastlock m_lock {\"RocksDBStorageProvider\"};\n\npublic:\n    RocksDBStorageProvider(RocksDBStorageFactory *pfactory, std::shared_ptr<rocksdb::DB> &spdb, std::shared_ptr<rocksdb::ColumnFamilyHandle> &spcolfam, std::shared_ptr<rocksdb::ColumnFamilyHandle> &spexpirecolfam, const rocksdb::Snapshot *psnapshot, size_t count);\n    ~RocksDBStorageProvider();\n\n    virtual void insert(const char *key, size_t cchKey, void *data, size_t cb, bool fOverwrite) override;\n    virtual bool erase(const char *key, size_t cchKey) override;\n    virtual void retrieve(const char *key, size_t cchKey, callbackSingle fn) const override;\n    virtual size_t clear() override;\n    virtual bool enumerate(callback fn) const override;\n    virtual bool enumerate_hashslot(callback fn, unsigned int hashslot) const override;\n\n    virtual std::vector<std::string> getExpirationCandidates(unsigned int count) override;\n    virtual std::vector<std::string> getEvictionCandidates(unsigned int count) override;\n    virtual void setExpire(const char *key, size_t cchKey, long long expire) override;\n    virtual void removeExpire(const char *key, size_t cchKey, long long expire) override;\n\n    virtual const IStorage *clone() const override;\n\n    virtual void beginWriteBatch() override;\n    virtual void endWriteBatch() override;\n\n    virtual void bulkInsert(char **rgkeys, size_t *rgcbkeys, char **rgvals, size_t *rgcbvals, size_t celem) override;\n\n    virtual void batch_lock() override;\n    virtual void batch_unlock() override;\n\n    virtual void flush() override;\n\n    size_t count() const override;\n\nprotected:\n    bool FKeyExists(std::string&) const;\n    bool FExpireExists(std::string&) const;\n\n    const rocksdb::ReadOptions &ReadOptions() const { return m_readOptionsTemplate; }\n    rocksdb::WriteOptions WriteOptions() const;\n};\n\nbool FInternalKey(const char *key, size_t cch);"
  },
  {
    "path": "src/storage/rocksdbfactor_internal.h",
    "content": "#pragma once\n#include \"rocksdb.h\"\n\nclass RocksDBStorageFactory : public IStorageFactory\n{\n    std::shared_ptr<rocksdb::DB> m_spdb;    // Note: This must be first so it is deleted last\n    std::vector<std::unique_ptr<rocksdb::ColumnFamilyHandle>> m_vecspcols;\n    std::vector<std::unique_ptr<rocksdb::ColumnFamilyHandle>> m_vecspexpirecols;\n    std::shared_ptr<rocksdb::SstFileManager> m_pfilemanager;\n    std::string m_path;\n    bool m_fCreatedTempFolder = false;\n\npublic:\n    RocksDBStorageFactory(const char *dbfile, int dbnum, const char *rgchConfig, size_t cchConfig);\n    ~RocksDBStorageFactory();\n\n    virtual IStorage *create(int db, key_load_iterator iter, void *privdata) override;\n    virtual IStorage *createMetadataDb() override;\n    virtual const char *name() const override;\n\n    virtual size_t totalDiskspaceUsed() const override;\n    virtual sdsstring getInfo() const override;\n\n    virtual bool FSlow() const override { return true; }\n\n    virtual size_t filedsRequired() const override;\n    std::string getTempFolder();\n\n    rocksdb::Options RocksDbOptions();\n\nprivate:\n    void setVersion(rocksdb::ColumnFamilyHandle*);\n};"
  },
  {
    "path": "src/storage/rocksdbfactory.cpp",
    "content": "#include \"rocksdb.h\"\n#include \"../version.h\"\n#include <rocksdb/filter_policy.h>\n#include <rocksdb/table.h>\n#include <rocksdb/utilities/options_util.h>\n#include <rocksdb/sst_file_manager.h>\n#include <rocksdb/utilities/convenience.h>\n#include <rocksdb/slice_transform.h>\n#include \"rocksdbfactor_internal.h\"\n#include <sys/types.h>\n#include <sys/stat.h> \n#include <sys/statvfs.h>\n\nrocksdb::Options DefaultRocksDBOptions() {\n    rocksdb::Options options;\n    options.max_background_compactions = 4;\n    options.max_background_flushes = 2;\n    options.bytes_per_sync = 1048576;\n    options.compaction_pri = rocksdb::kMinOverlappingRatio;\n    options.compression = rocksdb::kNoCompression;\n    options.enable_pipelined_write = true;\n    options.allow_mmap_reads = true;\n    options.avoid_unnecessary_blocking_io = true;\n    options.prefix_extractor.reset(rocksdb::NewFixedPrefixTransform(0));\n\n    rocksdb::BlockBasedTableOptions table_options;\n    table_options.block_size = 16 * 1024;\n    table_options.cache_index_and_filter_blocks = true;\n    table_options.pin_l0_filter_and_index_blocks_in_cache = true;\n    table_options.data_block_index_type = rocksdb::BlockBasedTableOptions::kDataBlockBinaryAndHash;\n    table_options.checksum = rocksdb::kNoChecksum;\n    table_options.format_version = 4;\n    options.table_factory.reset(NewBlockBasedTableFactory(table_options));\n    \n    return options;\n}\n\nIStorageFactory *CreateRocksDBStorageFactory(const char *path, int dbnum, const char *rgchConfig, size_t cchConfig)\n{\n    return new RocksDBStorageFactory(path, dbnum, rgchConfig, cchConfig);\n}\n\nrocksdb::Options RocksDBStorageFactory::RocksDbOptions()\n{\n    rocksdb::Options options = DefaultRocksDBOptions();\n    options.max_open_files = filedsRequired();\n    options.sst_file_manager = m_pfilemanager;\n    options.create_if_missing = true;\n    options.create_missing_column_families = true;\n    options.info_log_level = rocksdb::ERROR_LEVEL;\n    options.max_total_wal_size = 1 * 1024 * 1024 * 1024;\n    return options;\n}\n\nRocksDBStorageFactory::RocksDBStorageFactory(const char *dbfile, int dbnum, const char *rgchConfig, size_t cchConfig)\n    : m_path(dbfile)\n{\n    dbnum++; // create an extra db for metadata\n    // Get the count of column families in the actual database\n    std::vector<std::string> vecT;\n    auto status = rocksdb::DB::ListColumnFamilies(rocksdb::Options(), dbfile, &vecT);\n    // RocksDB requires we know the count of col families before opening, if the user only wants to see less\n    //  we still have to make room for all column family handles regardless\n    if (status.ok() && (int)vecT.size()/2 > dbnum)\n        dbnum = (int)vecT.size()/2;\n\n    std::vector<rocksdb::ColumnFamilyDescriptor> veccoldesc;\n    veccoldesc.push_back(rocksdb::ColumnFamilyDescriptor(rocksdb::kDefaultColumnFamilyName, rocksdb::ColumnFamilyOptions()));  // ignore default col family\n\n    m_pfilemanager = std::shared_ptr<rocksdb::SstFileManager>(rocksdb::NewSstFileManager(rocksdb::Env::Default()));\n\n    rocksdb::DB *db = nullptr;\n\n    auto options = RocksDbOptions();\n    options.prefix_extractor.reset(rocksdb::NewFixedPrefixTransform(HASHSLOT_PREFIX_BYTES));\n\n    for (int idb = 0; idb < dbnum; ++idb)\n    {\n        rocksdb::ColumnFamilyOptions cf_options(options);\n        cf_options.level_compaction_dynamic_level_bytes = true;\n        veccoldesc.push_back(rocksdb::ColumnFamilyDescriptor(std::to_string(idb), cf_options));\n        veccoldesc.push_back(rocksdb::ColumnFamilyDescriptor(std::to_string(idb) + \"_expires\", cf_options));\n    }\n\n    if (rgchConfig != nullptr)\n    {\n        std::string options_string(rgchConfig, cchConfig);\n        rocksdb::Status status;\n        if (!(status = rocksdb::GetDBOptionsFromString(options, options_string, &options)).ok())\n        {\n            fprintf(stderr, \"Failed to parse FLASH options: %s\\r\\n\", status.ToString().c_str());\n            exit(EXIT_FAILURE);\n        }\n    }\n    \n    std::vector<rocksdb::ColumnFamilyHandle*> handles;\n    status = rocksdb::DB::Open(options, dbfile, veccoldesc, &handles, &db);\n    if (!status.ok())\n        throw status.ToString();\n\n    m_spdb = std::shared_ptr<rocksdb::DB>(db);\n    for (auto handle : handles)\n    {\n        if (handle->GetName().size() > 7 && !strncmp(handle->GetName().substr(handle->GetName().size() - 7).c_str(), \"expires\", 7)) {\n            m_vecspexpirecols.emplace_back(handle);\n        } else {\n            std::string strVersion;\n            auto status = m_spdb->Get(rocksdb::ReadOptions(), handle, rocksdb::Slice(version_key, sizeof(version_key)), &strVersion);\n            if (!status.ok())\n            {\n                setVersion(handle);\n            }\n            else\n            {\n                SymVer ver = parseVersion(strVersion.c_str());\n                auto cmp = compareVersion(&ver);\n                if (cmp == NewerVersion)\n                    throw \"Cannot load FLASH database created by newer version of KeyDB\";\n                if (cmp == IncompatibleVersion)\n                    throw \"Cannot load FLASH database from before 6.3.4\";\n                if (cmp == OlderVersion)\n                    setVersion(handle);\n            }\n            m_vecspcols.emplace_back(handle);\n        }\n    }\n}\n\nRocksDBStorageFactory::~RocksDBStorageFactory()\n{\n    m_spdb->SyncWAL();\n}\n\nvoid RocksDBStorageFactory::setVersion(rocksdb::ColumnFamilyHandle *handle)\n{\n    auto status = m_spdb->Put(rocksdb::WriteOptions(), handle, rocksdb::Slice(version_key, sizeof(version_key)), rocksdb::Slice(KEYDB_REAL_VERSION, strlen(KEYDB_REAL_VERSION)+1));\n    if (!status.ok())\n        throw status.ToString();\n}\n\nsize_t RocksDBStorageFactory::filedsRequired() const {\n    return 256;\n}\n\nstd::string RocksDBStorageFactory::getTempFolder()\n{\n    auto path = m_path + \"/keydb_tmp/\";\n    if (!m_fCreatedTempFolder) {\n        if (!mkdir(path.c_str(), 0700))\n            m_fCreatedTempFolder = true;\n    }\n    return path;\n}\n\nIStorage *RocksDBStorageFactory::createMetadataDb()\n{\n    IStorage *metadataDb = this->create(-1, nullptr, nullptr);\n    metadataDb->insert(meta_key, sizeof(meta_key), (void*)METADATA_DB_IDENTIFIER, strlen(METADATA_DB_IDENTIFIER), false);\n    return metadataDb;\n}\n\nIStorage *RocksDBStorageFactory::create(int db, key_load_iterator iter, void *privdata)\n{\n    ++db;   // skip default col family\n    std::shared_ptr<rocksdb::ColumnFamilyHandle> spcolfamily(m_vecspcols[db].release());\n    std::shared_ptr<rocksdb::ColumnFamilyHandle> spexpirecolfamily(m_vecspexpirecols[db].release());\n    size_t count = 0;\n    bool fUnclean = false;\n    \n    std::string value;\n    auto status = m_spdb->Get(rocksdb::ReadOptions(), spcolfamily.get(), rocksdb::Slice(count_key, sizeof(count_key)), &value);\n    if (status.ok() && value.size() == sizeof(size_t))\n    {\n        count = *reinterpret_cast<const size_t*>(value.data());\n        m_spdb->Delete(rocksdb::WriteOptions(), spcolfamily.get(), rocksdb::Slice(count_key, sizeof(count_key)));\n    }\n    else\n    {\n        fUnclean = true;\n    }\n    \n    if (fUnclean || iter != nullptr)\n    {\n        count = 0;\n        auto opts = rocksdb::ReadOptions();\n        opts.tailing = true;\n        std::unique_ptr<rocksdb::Iterator> it = std::unique_ptr<rocksdb::Iterator>(m_spdb->NewIterator(opts, spcolfamily.get()));\n\n        it->SeekToFirst();\n        bool fFirstRealKey = true;\n        \n        for (;it->Valid(); it->Next()) {\n            if (FInternalKey(it->key().data(), it->key().size()))\n                continue;\n            if (fUnclean && it->Valid() && fFirstRealKey)\n                printf(\"\\tDatabase %d was not shutdown cleanly, recomputing metrics\\n\", db);\n            fFirstRealKey = false;\n            if (iter != nullptr)\n                iter(it->key().data()+HASHSLOT_PREFIX_BYTES, it->key().size()-HASHSLOT_PREFIX_BYTES, privdata);\n            ++count;\n        }\n    }\n    return new RocksDBStorageProvider(this, m_spdb, spcolfamily, spexpirecolfamily, nullptr, count);\n}\n\nconst char *RocksDBStorageFactory::name() const\n{\n    return \"flash\";\n}\n\nsize_t RocksDBStorageFactory::totalDiskspaceUsed() const\n{\n    return m_pfilemanager->GetTotalSize();\n}\n\nsdsstring RocksDBStorageFactory::getInfo() const\n{\n    struct statvfs fiData;\n    int status = statvfs(m_path.c_str(), &fiData);\n    if ( status == 0 ) {\n        return sdsstring(sdscatprintf(sdsempty(),\n            \"storage_flash_used_bytes:%zu\\r\\n\"\n            \"storage_flash_total_bytes:%zu\\r\\n\"\n            \"storage_flash_rocksdb_bytes:%zu\\r\\n\",\n            fiData.f_bfree * fiData.f_frsize,\n            fiData.f_blocks * fiData.f_frsize,\n            totalDiskspaceUsed()));\n    } else {\n        fprintf(stderr, \"Failed to gather FLASH statistics with status: %d\\r\\n\", status);\n        return sdsstring(sdsempty());\n    }\n}\n"
  },
  {
    "path": "src/storage/rocksdbfactory.h",
    "content": "#pragma once\n\nclass IStorageFactory *CreateRocksDBStorageFactory(const char *path, int dbnum, const char *rgchConfig, size_t cchConfig);"
  },
  {
    "path": "src/storage/teststorageprovider.cpp",
    "content": "#include \"teststorageprovider.h\"\n#include \"../server.h\"\n\nIStorage *TestStorageFactory::create(int, key_load_iterator, void *)\n{\n    return new (MALLOC_LOCAL) TestStorageProvider();\n}\n\nIStorage *TestStorageFactory::createMetadataDb()\n{\n    IStorage *metadataDb = new (MALLOC_LOCAL) TestStorageProvider();\n    metadataDb->insert(\"KEYDB_METADATA_ID\", strlen(\"KEYDB_METADATA_ID\"), (void*)METADATA_DB_IDENTIFIER, strlen(METADATA_DB_IDENTIFIER), false);\n    return metadataDb;\n}\n\nconst char *TestStorageFactory::name() const\n{\n    return \"TEST Storage Provider\";\n}\n\nTestStorageProvider::TestStorageProvider()\n{\n}\n\nTestStorageProvider::~TestStorageProvider()\n{\n}\n\nvoid TestStorageProvider::insert(const char *key, size_t cchKey, void *data, size_t cb, bool fOverwrite)\n{\n    auto strkey = std::string(key, cchKey);\n    bool fActuallyExists = m_map.find(strkey) != m_map.end();\n    serverAssert(fActuallyExists == fOverwrite);\n    m_map.insert(std::make_pair(strkey, std::string((char*)data, cb)));\n}\n\n\n bool TestStorageProvider::erase(const char *key, size_t cchKey)\n {\n     auto itr = m_map.find(std::string(key, cchKey));\n     if (itr != m_map.end())\n     {\n         m_map.erase(itr);\n         return true;\n     }\n     return false;\n }\n\nvoid TestStorageProvider::retrieve(const char *key, size_t cchKey, callbackSingle fn) const\n{\n    auto itr = m_map.find(std::string(key, cchKey));\n    if (itr != m_map.end())\n        fn(key, cchKey, itr->second.data(), itr->second.size());\n}\n\nsize_t TestStorageProvider::clear()\n{\n    size_t size = m_map.size();\n    m_map.clear();\n    return size;\n}\n\nbool TestStorageProvider::enumerate(callback fn) const\n{\n    bool fAll = true;\n    for (auto &pair : m_map)\n    {\n        if (!fn(pair.first.data(), pair.first.size(), pair.second.data(), pair.second.size()))\n        {\n            fAll = false;\n            break;\n        }\n    }\n    return fAll;\n}\n\nbool TestStorageProvider::enumerate_hashslot(callback fn, unsigned int hashslot) const\n{\n    bool fAll = true;\n    for (auto &pair : m_map)\n    {\n        if (keyHashSlot(pair.first.data(), pair.first.size()) == hashslot)\n            if (!fn(pair.first.data(), pair.first.size(), pair.second.data(), pair.second.size()))\n            {\n                fAll = false;\n                break;\n            }\n    }\n    return fAll;\n}\n      \nsize_t TestStorageProvider::count() const\n{\n    return m_map.size();\n}\n\nvoid TestStorageProvider::flush()\n{\n    /* NOP */\n}\n\n/* This is permitted to be a shallow clone */\nconst IStorage *TestStorageProvider::clone() const\n{\n    return new (MALLOC_LOCAL) TestStorageProvider(*this);\n}"
  },
  {
    "path": "src/storage/teststorageprovider.h",
    "content": "#include \"../IStorage.h\"\n#include <string>\n#include <unordered_map>\n#include <vector>\n\nclass TestStorageFactory : public IStorageFactory\n{\n    virtual class IStorage *create(int db, key_load_iterator itr, void *privdata) override;\n    virtual class IStorage *createMetadataDb() override;\n    virtual const char *name() const override;\n    virtual size_t totalDiskspaceUsed() const override { return 0; }\n    virtual sdsstring getInfo() const override { return sdsstring(sdsempty()); }\n    virtual bool FSlow() const override { return false; }\n};\n\nclass TestStorageProvider final : public IStorage\n{\n    std::unordered_map<std::string, std::string> m_map;\n\npublic:\n    TestStorageProvider();\n    virtual ~TestStorageProvider();\n\n    virtual void insert(const char *key, size_t cchKey, void *data, size_t cb, bool fHintOverwrite) override;\n    virtual bool erase(const char *key, size_t cchKey) override;\n    virtual void retrieve(const char *key, size_t cchKey, callbackSingle fn) const override;\n    virtual size_t clear() override;\n    virtual bool enumerate(callback fn) const override;\n    virtual bool enumerate_hashslot(callback fn, unsigned int hashslot) const override;\n    virtual size_t count() const override;\n\n    virtual std::vector<std::string> getExpirationCandidates(unsigned int) override { return std::vector<std::string>(); }\n    virtual std::vector<std::string> getEvictionCandidates(unsigned int) override { return std::vector<std::string>(); }\n    virtual void setExpire(const char *, size_t, long long) override {}\n    virtual void removeExpire(const char *, size_t, long long) override {}\n\n    virtual void flush() override;\n\n    /* This is permitted to be a shallow clone */\n    virtual const IStorage *clone() const override;\n};\n"
  },
  {
    "path": "src/storage-lite.c",
    "content": "#include <stdlib.h>\n#include <stdio.h>\n#include <sys/ioctl.h>\n#include <unistd.h>\n#include <inttypes.h>\n#include \"storage.h\"\n#include <assert.h>\n#ifdef __linux__\n#include <malloc.h>\n#endif\n\n// initialize the memory subsystem. \n//  NOTE: This may be called twice, first with NULL specifying we should use ram\n//      later, after the configuration file is loaded with a path to where we should\n//      place our temporary file.\nvoid storage_init(const char *tmpfilePath, size_t cbReserve)\n{\n    assert(tmpfilePath == NULL);\n    (void)tmpfilePath;\n    (void)cbReserve;\n}\n\nvoid *salloc(size_t cb, enum MALLOC_CLASS class)\n{\n    (void)class;\n    return malloc(cb);\n}\n\nvoid *scalloc(size_t cb, size_t c, enum MALLOC_CLASS class)\n{\n    (void)class;\n    return calloc(cb, c);\n}\n\nvoid sfree(void *pv)\n{\n    free(pv);\n}\n\nvoid *srealloc(void *pv, size_t cb, enum MALLOC_CLASS class)\n{\n    (void)class;\n    return realloc(pv, cb);\n}\n\n#ifdef __linux__\nsize_t salloc_usable_size(void *ptr)\n{\n    return malloc_usable_size(ptr);\n}\n#endif\n"
  },
  {
    "path": "src/storage.cpp",
    "content": "#include \"server.h\"\n\n\nIStorage::~IStorage() {}\n\n#ifdef USE_MEMKIND\n\n#include <stdlib.h>\n#include <stdio.h>\n#include <memkind.h>\n#include <sys/ioctl.h>\n#include <linux/fs.h>\n#include <unistd.h>\n#include <inttypes.h>\n#include <fcntl.h>\n#include \"storage.h\"\n#include \"IStorage.h\"\n\nstruct memkind *mkdisk = NULL;\nstatic const char *PMEM_DIR = NULL;\n\nextern \"C\" int memkind_pmem_iskind(struct memkind *kind, const void *pv);\n\nvoid handle_prefork();\nvoid handle_postfork_parent();\nvoid handle_postfork_child();\n\n#define OBJECT_PAGE_BUFFER_SIZE 8192 //(size in objs)\n#define OBJ_PAGE_BITS_PER_WORD 64\nstruct object_page\n{\n    uint64_t allocmap[OBJECT_PAGE_BUFFER_SIZE/(8*sizeof(uint64_t))];\n    struct object_page *pnext;\n    char *rgb()\n    {\n        return reinterpret_cast<char*>(this+1);\n    };\n};\n\nstruct alloc_pool\n{\n    unsigned cbObject;\n    struct object_page *pobjpageHead;\n};\n\n\nstruct object_page *pool_allocate_page(int cbObject)\n{\n    size_t cb = (((size_t)cbObject) * OBJECT_PAGE_BUFFER_SIZE) + sizeof(struct object_page);\n    return (object_page*)scalloc(cb, 1, MALLOC_SHARED);\n}\nvoid pool_initialize(struct alloc_pool *ppool, int cbObject)\n{\n    if ((cbObject % 8) != 0)\n    {\n        cbObject += 8 - (cbObject % 8);\n    }\n    ppool->cbObject = cbObject;\n    ppool->pobjpageHead = pool_allocate_page(cbObject);\n}\nstatic int IdxAllocObject(struct object_page *page)\n{\n    for (size_t iword = 0; iword < OBJ_PAGE_BITS_PER_WORD; ++iword)\n    {\n        if ((page->allocmap[iword] + 1) != 0)\n        {\n            int ibit = 0;\n            uint64_t bitword = page->allocmap[iword];\n            while (bitword & 1)\n            {\n                bitword >>= 1;\n                ++ibit;\n            }\n            page->allocmap[iword] |= 1ULL << ibit;\n            return (iword * OBJ_PAGE_BITS_PER_WORD) + ibit;\n        }\n    }\n    return -1;\n}\nvoid *pool_alloc(struct alloc_pool *ppool)\n{\n    struct object_page *cur = ppool->pobjpageHead;\n    for (;;)\n    {\n        int idx = IdxAllocObject(cur);\n        if (idx >= 0)\n        {\n            return cur->rgb() + (((size_t)ppool->cbObject) * idx);\n        }\n\n        if (cur->pnext == NULL)\n        {\n            cur->pnext = pool_allocate_page(ppool->cbObject);\n        }\n\n        cur = cur->pnext;\n    }\n}\n\nvoid pool_free(struct alloc_pool *ppool, void *pv)\n{\n    struct object_page *cur = ppool->pobjpageHead;\n    char *obj = (char*)pv;\n\n    for (;cur != NULL;)\n    {\n        if (obj >= cur->rgb() && (obj < (cur->rgb() + (OBJECT_PAGE_BUFFER_SIZE * ppool->cbObject))))\n        {\n            // Its on this page\n            int idx = (obj - cur->rgb()) / ppool->cbObject;\n            cur->allocmap[idx / OBJ_PAGE_BITS_PER_WORD] &= ~(1ULL << (idx % OBJ_PAGE_BITS_PER_WORD));\n            return;\n        }\n        cur = cur->pnext;\n    }\n    serverLog(LL_WARNING, \"obj not from pool\");\n    sfree(obj); // we don't know where it came from\n    return;\n}\n\nextern size_t OBJ_ENCODING_EMBSTR_SIZE_LIMIT;\n#define EMBSTR_ROBJ_SIZE (sizeof(robj)+sizeof(struct sdshdr8)+OBJ_ENCODING_EMBSTR_SIZE_LIMIT+1)\nstruct alloc_pool poolobj;\nstruct alloc_pool poolembstrobj;\n\nint forkFile()\n{\n    int fdT;\n    memkind_tmpfile(PMEM_DIR, &fdT);\n    if (ioctl(fdT, FICLONE, memkind_fd(mkdisk)) == -1)\n    {\n        return -1;\n    }\n    return fdT;\n}\n\n// initialize the memory subsystem. \n//  NOTE: This may be called twice, first with NULL specifying we should use ram\n//      later, after the configuration file is loaded with a path to where we should\n//      place our temporary file.\nvoid storage_init(const char *tmpfilePath, size_t cbFileReserve)\n{\n    if (tmpfilePath == NULL)\n    {\n        serverAssert(mkdisk == NULL);\n        mkdisk = MEMKIND_DEFAULT;\n        pool_initialize(&poolobj, sizeof(robj));\n        pool_initialize(&poolembstrobj, EMBSTR_ROBJ_SIZE);\n    }\n    else\n    {\n        // First create the file\n        serverAssert(mkdisk == MEMKIND_DEFAULT);\n        PMEM_DIR = (char*)memkind_malloc(MEMKIND_DEFAULT, strlen(tmpfilePath));\n        strcpy((char*)PMEM_DIR, tmpfilePath);\n        int errv = memkind_create_pmem(PMEM_DIR, 0, &mkdisk);\n        if (errv == MEMKIND_ERROR_INVALID)\n        {\n            serverLog(LL_WARNING, \"Memory pool creation failed: %s\", strerror(errno));\n            exit(EXIT_FAILURE);\n        }\n        else if (errv)\n        {\n            char msgbuf[1024];\n            memkind_error_message(errv, msgbuf, 1024);\n            serverLog(LL_WARNING, \"Memory pool creation failed: %s\", msgbuf);\n            exit(EXIT_FAILURE);\n        }\n\n        // Next test if COW is working\n        int fdTest = forkFile();\n        if (fdTest < 0)\n        {\n            serverLog(LL_WARNING, \"Scratch file system does not support Copy on Write.  To fix this scratch-file-path must point to a path on a filesystem which supports copy on write, such as btrfs.\");\n            exit(EXIT_FAILURE);\n        }\n        close(fdTest);\n\n        // Now lets make the file big\n        if (cbFileReserve == 0)\n            cbFileReserve = 1*1024*1024*1024;   // 1 GB (enough to be interesting)\n        posix_fallocate64(memkind_fd(mkdisk), 0, cbFileReserve);\n\n        pthread_atfork(handle_prefork, handle_postfork_parent, handle_postfork_child);\n    }\n}\n\n\n\nstruct redisObject *salloc_obj()\n{\n    return (redisObject*)pool_alloc(&poolobj);\n}\nvoid sfree_obj(struct redisObject *obj)\n{\n    pool_free(&poolobj, obj);\n}\nstruct redisObject *salloc_objembstr()\n{\n    return (redisObject*)pool_alloc(&poolembstrobj);\n}\nvoid sfree_objembstr(robj *obj)\n{\n    pool_free(&poolembstrobj, obj);\n}\n\nstatic memkind_t kindFromPtr(const void *pv)\n{\n    if (mkdisk == MEMKIND_DEFAULT)\n        return MEMKIND_DEFAULT;\n    \n    if (memkind_pmem_iskind(mkdisk, pv))\n        return mkdisk;\n    return MEMKIND_DEFAULT;\n}\n\nsize_t salloc_usable_size(void *ptr)\n{\n    return memkind_malloc_usable_size(kindFromPtr(ptr), ptr);\n}\n\nstatic memkind_t kindFromClass(enum MALLOC_CLASS mclass)\n{\n    switch (mclass)\n    {\n    case MALLOC_SHARED:\n        return mkdisk;\n    default:\n        break;\n    }\n    return MEMKIND_DEFAULT;\n}\n\nvoid *salloc(size_t cb, enum MALLOC_CLASS mclass)\n{\n    if (cb == 0) \n        cb = 1;\n        \n    return memkind_malloc(kindFromClass(mclass), cb);\n}\n\nvoid *scalloc(size_t cb, size_t c, enum MALLOC_CLASS mclass)\n{\n    return memkind_calloc(kindFromClass(mclass), cb, c);\n}\n\nvoid sfree(void *pv)\n{\n    memkind_free(kindFromPtr(pv), pv);\n}\n\nvoid *srealloc(void *pv, size_t cb, enum MALLOC_CLASS mclass)\n{\n    return memkind_realloc(kindFromClass(mclass), pv, cb);\n}\n\nint fdNew = -1;\nvoid handle_prefork()\n{\n    fdNew = forkFile();\n    if (fdNew < 0)\n        serverLog(LL_WARNING, \"Failed to clone scratch file\");\n}\n\nvoid handle_postfork_parent()\n{\n    // Parent, close fdNew\n    close(fdNew);\n    fdNew = -1;\n}\n\nvoid handle_postfork_child()\n{\n    int fdOriginal = memkind_fd(mkdisk);\n    memkind_pmem_remapfd(mkdisk, fdNew);\n    close(fdOriginal);\n}\n\n#endif // USE_MEMKIND\n"
  },
  {
    "path": "src/storage.h",
    "content": "#ifndef __STORAGE_H__\n#define __STORAGE_H__\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\nenum MALLOC_CLASS\n{\n    MALLOC_LOCAL,\n    MALLOC_SHARED,\n};\n\nvoid storage_init(const char *tmpfilePath, size_t cbFileReserve);\n\nvoid *salloc(size_t cb, enum MALLOC_CLASS mclass);\nvoid *scalloc(size_t cb, size_t c, enum MALLOC_CLASS mclass);\nvoid sfree(void*);\nvoid *srealloc(void *pv, size_t cb, enum MALLOC_CLASS mclass);\nsize_t salloc_usable_size(void *ptr);\n\nstruct redisObject *salloc_objembstr();\nvoid sfree_objembstr(struct redisObject *obj);\nstruct redisObject *salloc_obj();\nvoid sfree_obj(struct redisObject *obj);\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n"
  },
  {
    "path": "src/stream.h",
    "content": "#ifndef STREAM_H\n#define STREAM_H\n\n#include \"rax.h\"\n#include \"listpack.h\"\n\n/* Stream item ID: a 128 bit number composed of a milliseconds time and\n * a sequence counter. IDs generated in the same millisecond (or in a past\n * millisecond if the clock jumped backward) will use the millisecond time\n * of the latest generated ID and an incremented sequence. */\ntypedef struct streamID {\n    uint64_t ms;        /* Unix time in milliseconds. */\n    uint64_t seq;       /* Sequence number. */\n} streamID;\n\ntypedef struct stream {\n    ::rax *rax;               /* The radix tree holding the stream. */\n    uint64_t length;        /* Number of elements inside this stream. */\n    streamID last_id;       /* Zero if there are yet no items. */\n    ::rax *cgroups;           /* Consumer groups dictionary: name -> streamCG */\n} stream;\n\n/* We define an iterator to iterate stream items in an abstract way, without\n * caring about the radix tree + listpack representation. Technically speaking\n * the iterator is only used inside streamReplyWithRange(), so could just\n * be implemented inside the function, but practically there is the AOF\n * rewriting code that also needs to iterate the stream to emit the XADD\n * commands. */\ntypedef struct streamIterator {\n    stream *pstream;         /* The stream we are iterating. */\n    streamID master_id;     /* ID of the master entry at listpack head. */\n    uint64_t master_fields_count;       /* Master entries # of fields. */\n    unsigned char *master_fields_start; /* Master entries start in listpack. */\n    unsigned char *master_fields_ptr;   /* Master field to emit next. */\n    int entry_flags;                    /* Flags of entry we are emitting. */\n    int rev;                /* True if iterating end to start (reverse). */\n    uint64_t start_key[2];  /* Start key as 128 bit big endian. */\n    uint64_t end_key[2];    /* End key as 128 bit big endian. */\n    raxIterator ri;         /* Rax iterator. */\n    unsigned char *lp;      /* Current listpack. */\n    unsigned char *lp_ele;  /* Current listpack cursor. */\n    unsigned char *lp_flags; /* Current entry flags pointer. */\n    /* Buffers used to hold the string of lpGet() when the element is\n     * integer encoded, so that there is no string representation of the\n     * element inside the listpack itself. */\n    unsigned char field_buf[LP_INTBUF_SIZE];\n    unsigned char value_buf[LP_INTBUF_SIZE];\n} streamIterator;\n\n/* Consumer group. */\ntypedef struct streamCG {\n    streamID last_id;       /* Last delivered (not acknowledged) ID for this\n                               group. Consumers that will just ask for more\n                               messages will served with IDs > than this. */\n    rax *pel;               /* Pending entries list. This is a radix tree that\n                               has every message delivered to consumers (without\n                               the NOACK option) that was yet not acknowledged\n                               as processed. The key of the radix tree is the\n                               ID as a 64 bit big endian number, while the\n                               associated value is a streamNACK structure.*/\n    rax *consumers;         /* A radix tree representing the consumers by name\n                               and their associated representation in the form\n                               of streamConsumer structures. */\n} streamCG;\n\n/* A specific consumer in a consumer group.  */\ntypedef struct streamConsumer {\n    mstime_t seen_time;         /* Last time this consumer was active. */\n    sds name;                   /* Consumer name. This is how the consumer\n                                   will be identified in the consumer group\n                                   protocol. Case sensitive. */\n    rax *pel;                   /* Consumer specific pending entries list: all\n                                   the pending messages delivered to this\n                                   consumer not yet acknowledged. Keys are\n                                   big endian message IDs, while values are\n                                   the same streamNACK structure referenced\n                                   in the \"pel\" of the consumer group structure\n                                   itself, so the value is shared. */\n} streamConsumer;\n\n/* Pending (yet not acknowledged) message in a consumer group. */\ntypedef struct streamNACK {\n    mstime_t delivery_time;     /* Last time this message was delivered. */\n    uint64_t delivery_count;    /* Number of times this message was delivered.*/\n    streamConsumer *consumer;   /* The consumer this message was delivered to\n                                   in the last delivery. */\n} streamNACK;\n\n/* Stream propagation informations, passed to functions in order to propagate\n * XCLAIM commands to AOF and slaves. */\ntypedef struct streamPropInfo {\n    robj *keyname;\n    robj *groupname;\n} streamPropInfo;\n\n/* Prototypes of exported APIs. */\nstruct client;\n\n/* Flags for streamLookupConsumer */\n#define SLC_NONE      0\n#define SLC_NOCREAT   (1<<0) /* Do not create the consumer if it doesn't exist */\n#define SLC_NOREFRESH (1<<1) /* Do not update consumer's seen-time */\n\nstream *streamNew(void);\nvoid freeStream(stream *s);\nunsigned long streamLength(robj_roptr subject);\nsize_t streamReplyWithRange(client *c, stream *s, streamID *start, streamID *end, size_t count, int rev, streamCG *group, streamConsumer *consumer, int flags, streamPropInfo *spi);\nvoid streamIteratorStart(streamIterator *si, stream *s, streamID *start, streamID *end, int rev);\nint streamIteratorGetID(streamIterator *si, streamID *id, int64_t *numfields);\nvoid streamIteratorGetField(streamIterator *si, unsigned char **fieldptr, unsigned char **valueptr, int64_t *fieldlen, int64_t *valuelen);\nvoid streamIteratorRemoveEntry(streamIterator *si, streamID *current);\nvoid streamIteratorStop(streamIterator *si);\nstreamCG *streamLookupCG(stream *s, sds groupname);\nstreamConsumer *streamLookupConsumer(streamCG *cg, sds name, int flags, int *created);\nstreamCG *streamCreateCG(stream *s, char *name, size_t namelen, streamID *id);\nstreamNACK *streamCreateNACK(streamConsumer *consumer);\nvoid streamDecodeID(void *buf, streamID *id);\nint streamCompareID(streamID *a, streamID *b);\nvoid streamFreeNACK(streamNACK *na);\nint streamIncrID(streamID *id);\nint streamDecrID(streamID *id);\nvoid streamPropagateConsumerCreation(client *c, robj *key, robj *groupname, sds consumername);\nrobj *streamDup(robj *o);\nint streamValidateListpackIntegrity(unsigned char *lp, size_t size, int deep);\nint streamParseID(const robj *o, streamID *id);\nrobj *createObjectFromStreamID(streamID *id);\nint streamAppendItem(stream *s, robj **argv, int64_t numfields, streamID *added_id, streamID *use_id);\nint streamDeleteItem(stream *s, streamID *id);\nint64_t streamTrimByLength(stream *s, long long maxlen, int approx);\nint64_t streamTrimByID(stream *s, streamID minid, int approx);\n\n#endif\n"
  },
  {
    "path": "src/syncio.cpp",
    "content": "/* Synchronous socket and file I/O operations useful across the core.\n *\n * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n\n/* ----------------- Blocking sockets I/O with timeouts --------------------- */\n\n/* Redis performs most of the I/O in a nonblocking way, with the exception\n * of the SYNC command where the replica does it in a blocking way, and\n * the MIGRATE command that must be blocking in order to be atomic from the\n * point of view of the two instances (one migrating the key and one receiving\n * the key). This is why need the following blocking I/O functions.\n *\n * All the functions take the timeout in milliseconds. */\n\n#define SYNCIO__RESOLUTION 10 /* Resolution in milliseconds */\n\n/* Write the specified payload to 'fd'. If writing the whole payload will be\n * done within 'timeout' milliseconds the operation succeeds and 'size' is\n * returned. Otherwise the operation fails, -1 is returned, and an unspecified\n * partial write could be performed against the file descriptor. */\nssize_t syncWrite(int fd, const char *ptr, ssize_t size, long long timeout) {\n    ssize_t nwritten, ret = size;\n    long long start = mstime();\n    long long remaining = timeout;\n\n    while(1) {\n        long long wait = (remaining > SYNCIO__RESOLUTION) ?\n                          remaining : SYNCIO__RESOLUTION;\n        long long elapsed;\n\n        /* Optimistically try to write before checking if the file descriptor\n         * is actually writable. At worst we get EAGAIN. */\n        nwritten = write(fd,ptr,size);\n        if (nwritten == -1) {\n            if (errno != EAGAIN) return -1;\n        } else {\n            ptr += nwritten;\n            size -= nwritten;\n        }\n        if (size == 0) return ret;\n\n        /* Wait */\n        aeWait(fd,AE_WRITABLE,wait);\n        elapsed = mstime() - start;\n        if (elapsed >= timeout) {\n            errno = ETIMEDOUT;\n            return -1;\n        }\n        remaining = timeout - elapsed;\n    }\n}\n\n/* Read the specified amount of bytes from 'fd'. If all the bytes are read\n * within 'timeout' milliseconds the operation succeed and 'size' is returned.\n * Otherwise the operation fails, -1 is returned, and an unspecified amount of\n * data could be read from the file descriptor. */\nssize_t syncRead(int fd, char *ptr, ssize_t size, long long timeout) {\n    ssize_t nread, totread = 0;\n    long long start = mstime();\n    long long remaining = timeout;\n\n    if (size == 0) return 0;\n    while(1) {\n        long long wait = (remaining > SYNCIO__RESOLUTION) ?\n                          remaining : SYNCIO__RESOLUTION;\n        long long elapsed;\n\n        /* Optimistically try to read before checking if the file descriptor\n         * is actually readable. At worst we get EAGAIN. */\n        nread = read(fd,ptr,size);\n        if (nread == 0) return -1; /* short read. */\n        if (nread == -1) {\n            if (errno != EAGAIN) return -1;\n        } else {\n            ptr += nread;\n            size -= nread;\n            totread += nread;\n        }\n        if (size == 0) return totread;\n\n        /* Wait */\n        aeWait(fd,AE_READABLE,wait);\n        elapsed = mstime() - start;\n        if (elapsed >= timeout) {\n            errno = ETIMEDOUT;\n            return -1;\n        }\n        remaining = timeout - elapsed;\n    }\n}\n\n/* Read a line making sure that every char will not require more than 'timeout'\n * milliseconds to be read.\n *\n * On success the number of bytes read is returned, otherwise -1.\n * On success the string is always correctly terminated with a 0 byte. */\nssize_t syncReadLine(int fd, char *ptr, ssize_t size, long long timeout) {\n    ssize_t nread = 0;\n\n    size--;\n    while(size) {\n        char c;\n\n        if (syncRead(fd,&c,1,timeout) == -1) return -1;\n        if (c == '\\n') {\n            *ptr = '\\0';\n            if (nread && *(ptr-1) == '\\r') *(ptr-1) = '\\0';\n            return nread;\n        } else {\n            *ptr++ = c;\n            *ptr = '\\0';\n            nread++;\n        }\n        size--;\n    }\n    return nread;\n}\n"
  },
  {
    "path": "src/t_hash.cpp",
    "content": "/*\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n#include \"aelocker.h\"\n#include <math.h>\n\n/*-----------------------------------------------------------------------------\n * Hash type API\n *----------------------------------------------------------------------------*/\n\n/* Check the length of a number of objects to see if we need to convert a\n * ziplist to a real hash. Note that we only check string encoded objects\n * as their string length can be queried in constant time. */\nvoid hashTypeTryConversion(robj *o, robj **argv, int start, int end) {\n    int i;\n    size_t sum = 0;\n\n    if (o->encoding != OBJ_ENCODING_ZIPLIST) return;\n\n    for (i = start; i <= end; i++) {\n        if (!sdsEncodedObject(argv[i]))\n            continue;\n        size_t len = sdslen(szFromObj(argv[i]));\n        if (len > g_pserver->hash_max_ziplist_value) {\n            hashTypeConvert(o, OBJ_ENCODING_HT);\n            return;\n        }\n        sum += len;\n    }\n    if (!ziplistSafeToAdd((unsigned char *)ptrFromObj(o), sum))\n        hashTypeConvert(o, OBJ_ENCODING_HT);\n}\n\n/* Get the value from a ziplist encoded hash, identified by field.\n * Returns -1 when the field cannot be found. */\nint hashTypeGetFromZiplist(robj_roptr o, const char *field,\n                           const unsigned char **vstr,\n                           unsigned int *vlen,\n                           long long *vll)\n{\n    unsigned char *zl, *fptr = NULL, *vptr = NULL;\n    int ret;\n\n    serverAssert(o->encoding == OBJ_ENCODING_ZIPLIST);\n\n    zl = (unsigned char*)(ptrFromObj(o));\n    fptr = ziplistIndex(zl, ZIPLIST_HEAD);\n    if (fptr != NULL) {\n        fptr = ziplistFind(zl, fptr, (unsigned char*)field, sdslen(field), 1);\n        if (fptr != NULL) {\n            /* Grab pointer to the value (fptr points to the field) */\n            vptr = ziplistNext(zl, fptr);\n            serverAssert(vptr != NULL);\n        }\n    }\n\n    if (vptr != NULL) {\n        ret = ziplistGet(vptr, (unsigned char**)vstr, vlen, vll);\n        serverAssert(ret);\n        return 0;\n    }\n\n    return -1;\n}\n\n/* Get the value from a hash table encoded hash, identified by field.\n * Returns NULL when the field cannot be found, otherwise the SDS value\n * is returned. */\nconst char *hashTypeGetFromHashTable(robj_roptr o, const char *field) {\n    dictEntry *de;\n\n    serverAssert(o->encoding == OBJ_ENCODING_HT);\n\n    de = dictFind((dict*)ptrFromObj(o), field);\n    if (de == NULL) return NULL;\n    return (sds)dictGetVal(de);\n}\n\n/* Higher level function of hashTypeGet*() that returns the hash value\n * associated with the specified field. If the field is found C_OK\n * is returned, otherwise C_ERR. The returned object is returned by\n * reference in either *vstr and *vlen if it's returned in string form,\n * or stored in *vll if it's returned as a number.\n *\n * If *vll is populated *vstr is set to NULL, so the caller\n * can always check the function return by checking the return value\n * for C_OK and checking if vll (or vstr) is NULL. */\nint hashTypeGetValue(robj_roptr o, sds field, const unsigned char **vstr, unsigned int *vlen, long long *vll) {\n    if (o->encoding == OBJ_ENCODING_ZIPLIST) {\n        *vstr = NULL;\n        if (hashTypeGetFromZiplist(o, field, vstr, vlen, vll) == 0)\n            return C_OK;\n    } else if (o->encoding == OBJ_ENCODING_HT) {\n        const char *value;\n        if ((value = hashTypeGetFromHashTable(o, field)) != NULL) {\n            *vstr = (const unsigned char*) value;\n            *vlen = sdslen(value);\n            return C_OK;\n        }\n    } else {\n        serverPanic(\"Unknown hash encoding\");\n    }\n    return C_ERR;\n}\n\n/* Like hashTypeGetValue() but returns a Redis object, which is useful for\n * interaction with the hash type outside t_hash.c.\n * The function returns NULL if the field is not found in the hash. Otherwise\n * a newly allocated string object with the value is returned. */\nrobj *hashTypeGetValueObject(robj_roptr o, sds field) {\n    const unsigned char *vstr;\n    unsigned int vlen;\n    long long vll;\n\n    if (hashTypeGetValue(o,field,&vstr,&vlen,&vll) == C_ERR) return NULL;\n    if (vstr) return createStringObject((char*)vstr,vlen);\n    else return createStringObjectFromLongLong(vll);\n}\n\n/* Higher level function using hashTypeGet*() to return the length of the\n * object associated with the requested field, or 0 if the field does not\n * exist. */\nsize_t hashTypeGetValueLength(robj_roptr o, const char *field) {\n    size_t len = 0;\n    if (o->encoding == OBJ_ENCODING_ZIPLIST) {\n        const unsigned char *vstr = NULL;\n        unsigned int vlen = UINT_MAX;\n        long long vll = LLONG_MAX;\n\n        if (hashTypeGetFromZiplist(o, field, &vstr, &vlen, &vll) == 0)\n            len = vstr ? vlen : sdigits10(vll);\n    } else if (o->encoding == OBJ_ENCODING_HT) {\n        const char *aux;\n\n        if ((aux = hashTypeGetFromHashTable(o, field)) != NULL)\n            len = sdslen(aux);\n    } else {\n        serverPanic(\"Unknown hash encoding\");\n    }\n    return len;\n}\n\n/* Test if the specified field exists in the given hash. Returns 1 if the field\n * exists, and 0 when it doesn't. */\nint hashTypeExists(robj_roptr o, const char *field) {\n    if (o->encoding == OBJ_ENCODING_ZIPLIST) {\n        const unsigned char *vstr = NULL;\n        unsigned int vlen = UINT_MAX;\n        long long vll = LLONG_MAX;\n\n        if (hashTypeGetFromZiplist(o, field, &vstr, &vlen, &vll) == 0) return 1;\n    } else if (o->encoding == OBJ_ENCODING_HT) {\n        if (hashTypeGetFromHashTable(o, field) != NULL) return 1;\n    } else {\n        serverPanic(\"Unknown hash encoding\");\n    }\n    return 0;\n}\n\n/* Add a new field, overwrite the old with the new value if it already exists.\n * Return 0 on insert and 1 on update.\n *\n * By default, the key and value SDS strings are copied if needed, so the\n * caller retains ownership of the strings passed. However this behavior\n * can be effected by passing appropriate flags (possibly bitwise OR-ed):\n *\n * HASH_SET_TAKE_FIELD -- The SDS field ownership passes to the function.\n * HASH_SET_TAKE_VALUE -- The SDS value ownership passes to the function.\n *\n * When the flags are used the caller does not need to release the passed\n * SDS string(s). It's up to the function to use the string to create a new\n * entry or to free the SDS string before returning to the caller.\n *\n * HASH_SET_COPY corresponds to no flags passed, and means the default\n * semantics of copying the values if needed.\n *\n */\n#define HASH_SET_TAKE_FIELD (1<<0)\n#define HASH_SET_TAKE_VALUE (1<<1)\n#define HASH_SET_COPY 0\nint hashTypeSet(robj *o, sds field, sds value, int flags) {\n    int update = 0;\n\n    if (o->encoding == OBJ_ENCODING_ZIPLIST) {\n        unsigned char *zl, *fptr, *vptr;\n\n        zl = (unsigned char*)ptrFromObj(o);\n        fptr = ziplistIndex(zl, ZIPLIST_HEAD);\n        if (fptr != NULL) {\n            fptr = ziplistFind(zl, fptr, (unsigned char*)field, sdslen(field), 1);\n            if (fptr != NULL) {\n                /* Grab pointer to the value (fptr points to the field) */\n                vptr = ziplistNext(zl, fptr);\n                serverAssert(vptr != NULL);\n                update = 1;\n\n                /* Replace value */\n                zl = ziplistReplace(zl, vptr, (unsigned char*)value,\n                        sdslen(value));\n            }\n        }\n\n        if (!update) {\n            /* Push new field/value pair onto the tail of the ziplist */\n            zl = ziplistPush(zl, (unsigned char*)field, sdslen(field),\n                    ZIPLIST_TAIL);\n            zl = ziplistPush(zl, (unsigned char*)value, sdslen(value),\n                    ZIPLIST_TAIL);\n        }\n        o->m_ptr = zl;\n\n        /* Check if the ziplist needs to be converted to a hash table */\n        if (hashTypeLength(o) > g_pserver->hash_max_ziplist_entries)\n            hashTypeConvert(o, OBJ_ENCODING_HT);\n    } else if (o->encoding == OBJ_ENCODING_HT) {\n        dictEntry *de = dictFind((dict*)ptrFromObj(o),field);\n        if (de) {\n            sdsfree((sds)dictGetVal(de));\n            if (flags & HASH_SET_TAKE_VALUE) {\n                dictGetVal(de) = value;\n                value = NULL;\n            } else {\n                dictGetVal(de) = sdsdup(value);\n            }\n            update = 1;\n        } else {\n            sds f,v;\n            if (flags & HASH_SET_TAKE_FIELD) {\n                f = field;\n                field = NULL;\n            } else {\n                f = sdsdup(field);\n            }\n            if (flags & HASH_SET_TAKE_VALUE) {\n                v = value;\n                value = NULL;\n            } else {\n                v = sdsdup(value);\n            }\n            dictAdd((dict*)ptrFromObj(o),f,v);\n        }\n    } else {\n        serverPanic(\"Unknown hash encoding\");\n    }\n\n    /* Free SDS strings we did not referenced elsewhere if the flags\n     * want this function to be responsible. */\n    if (flags & HASH_SET_TAKE_FIELD && field) sdsfree(field);\n    if (flags & HASH_SET_TAKE_VALUE && value) sdsfree(value);\n    return update;\n}\n\n/* Delete an element from a hash.\n * Return 1 on deleted and 0 on not found. */\nint hashTypeDelete(robj *o, sds field) {\n    int deleted = 0;\n\n    if (o->encoding == OBJ_ENCODING_ZIPLIST) {\n        unsigned char *zl, *fptr;\n\n        zl = (unsigned char*)ptrFromObj(o);\n        fptr = ziplistIndex(zl, ZIPLIST_HEAD);\n        if (fptr != NULL) {\n            fptr = ziplistFind(zl, fptr, (unsigned char*)field, sdslen(field), 1);\n            if (fptr != NULL) {\n                zl = ziplistDelete(zl,&fptr); /* Delete the key. */\n                zl = ziplistDelete(zl,&fptr); /* Delete the value. */\n                o->m_ptr = zl;\n                deleted = 1;\n            }\n        }\n    } else if (o->encoding == OBJ_ENCODING_HT) {\n        if (dictDelete((dict*)ptrFromObj(o), field) == C_OK) {\n            deleted = 1;\n\n            /* Always check if the dictionary needs a resize after a delete. */\n            if (htNeedsResize((dict*)ptrFromObj(o))) dictResize((dict*)ptrFromObj(o));\n        }\n\n    } else {\n        serverPanic(\"Unknown hash encoding\");\n    }\n    return deleted;\n}\n\n/* Return the number of elements in a hash. */\nunsigned long hashTypeLength(robj_roptr o) {\n    unsigned long length = ULONG_MAX;\n\n    if (o->encoding == OBJ_ENCODING_ZIPLIST) {\n        length = ziplistLen((unsigned char*)ptrFromObj(o)) / 2;\n    } else if (o->encoding == OBJ_ENCODING_HT) {\n        length = dictSize((const dict*)ptrFromObj(o));\n    } else {\n        serverPanic(\"Unknown hash encoding\");\n    }\n    return length;\n}\n\nhashTypeIterator *hashTypeInitIterator(robj_roptr subject) {\n    hashTypeIterator *hi = (hashTypeIterator*)zmalloc(sizeof(hashTypeIterator), MALLOC_LOCAL);\n    hi->subject = subject;\n    hi->encoding = subject->encoding;\n\n    if (hi->encoding == OBJ_ENCODING_ZIPLIST) {\n        hi->fptr = NULL;\n        hi->vptr = NULL;\n    } else if (hi->encoding == OBJ_ENCODING_HT) {\n        hi->di = dictGetIterator((dict*)subject->m_ptr);\n    } else {\n        serverPanic(\"Unknown hash encoding\");\n    }\n    return hi;\n}\n\nvoid hashTypeReleaseIterator(hashTypeIterator *hi) {\n    if (hi->encoding == OBJ_ENCODING_HT)\n        dictReleaseIterator(hi->di);\n    zfree(hi);\n}\n\n/* Move to the next entry in the hash. Return C_OK when the next entry\n * could be found and C_ERR when the iterator reaches the end. */\nint hashTypeNext(hashTypeIterator *hi) {\n    if (hi->encoding == OBJ_ENCODING_ZIPLIST) {\n        unsigned char *zl;\n        unsigned char *fptr, *vptr;\n\n        zl = (unsigned char*)hi->subject->m_ptr;\n        fptr = hi->fptr;\n        vptr = hi->vptr;\n\n        if (fptr == NULL) {\n            /* Initialize cursor */\n            serverAssert(vptr == NULL);\n            fptr = ziplistIndex(zl, 0);\n        } else {\n            /* Advance cursor */\n            serverAssert(vptr != NULL);\n            fptr = ziplistNext(zl, vptr);\n        }\n        if (fptr == NULL) return C_ERR;\n\n        /* Grab pointer to the value (fptr points to the field) */\n        vptr = ziplistNext(zl, fptr);\n        serverAssert(vptr != NULL);\n\n        /* fptr, vptr now point to the first or next pair */\n        hi->fptr = fptr;\n        hi->vptr = vptr;\n    } else if (hi->encoding == OBJ_ENCODING_HT) {\n        if ((hi->de = dictNext(hi->di)) == NULL) return C_ERR;\n    } else {\n        serverPanic(\"Unknown hash encoding\");\n    }\n    return C_OK;\n}\n\n/* Get the field or value at iterator cursor, for an iterator on a hash value\n * encoded as a ziplist. Prototype is similar to `hashTypeGetFromZiplist`. */\nvoid hashTypeCurrentFromZiplist(hashTypeIterator *hi, int what,\n                                unsigned char **vstr,\n                                unsigned int *vlen,\n                                long long *vll)\n{\n    int ret;\n\n    serverAssert(hi->encoding == OBJ_ENCODING_ZIPLIST);\n\n    if (what & OBJ_HASH_KEY) {\n        ret = ziplistGet(hi->fptr, vstr, vlen, vll);\n        serverAssert(ret);\n    } else {\n        ret = ziplistGet(hi->vptr, vstr, vlen, vll);\n        serverAssert(ret);\n    }\n}\n\n/* Get the field or value at iterator cursor, for an iterator on a hash value\n * encoded as a hash table. Prototype is similar to\n * `hashTypeGetFromHashTable`. */\nsds hashTypeCurrentFromHashTable(hashTypeIterator *hi, int what) {\n    serverAssert(hi->encoding == OBJ_ENCODING_HT);\n\n    if (what & OBJ_HASH_KEY) {\n        return (sds)dictGetKey(hi->de);\n    } else {\n        return (sds)dictGetVal(hi->de);\n    }\n}\n\n/* Higher level function of hashTypeCurrent*() that returns the hash value\n * at current iterator position.\n *\n * The returned element is returned by reference in either *vstr and *vlen if\n * it's returned in string form, or stored in *vll if it's returned as\n * a number.\n *\n * If *vll is populated *vstr is set to NULL, so the caller\n * can always check the function return by checking the return value\n * type checking if vstr == NULL. */\nvoid hashTypeCurrentObject(hashTypeIterator *hi, int what, unsigned char **vstr, unsigned int *vlen, long long *vll) {\n    if (hi->encoding == OBJ_ENCODING_ZIPLIST) {\n        *vstr = NULL;\n        hashTypeCurrentFromZiplist(hi, what, vstr, vlen, vll);\n    } else if (hi->encoding == OBJ_ENCODING_HT) {\n        sds ele = hashTypeCurrentFromHashTable(hi, what);\n        *vstr = (unsigned char*) ele;\n        *vlen = sdslen(ele);\n    } else {\n        serverPanic(\"Unknown hash encoding\");\n    }\n}\n\n/* Return the key or value at the current iterator position as a new\n * SDS string. */\nsds hashTypeCurrentObjectNewSds(hashTypeIterator *hi, int what) {\n    unsigned char *vstr;\n    unsigned int vlen;\n    long long vll;\n\n    hashTypeCurrentObject(hi,what,&vstr,&vlen,&vll);\n    if (vstr) return sdsnewlen(vstr,vlen);\n    return sdsfromlonglong(vll);\n}\n\nrobj *hashTypeLookupWriteOrCreate(client *c, robj *key) {\n    robj *o = lookupKeyWrite(c->db,key);\n    if (checkType(c,o,OBJ_HASH)) return NULL;\n\n    if (o == NULL) {\n        o = createHashObject();\n        dbAdd(c->db,key,o);\n    }\n    return o;\n}\n\nvoid hashTypeConvertZiplist(robj *o, int enc) {\n    serverAssert(o->encoding == OBJ_ENCODING_ZIPLIST);\n\n    if (enc == OBJ_ENCODING_ZIPLIST) {\n        /* Nothing to do... */\n\n    } else if (enc == OBJ_ENCODING_HT) {\n        hashTypeIterator *hi;\n        dict *dict;\n        int ret;\n\n        hi = hashTypeInitIterator(o);\n        dict = dictCreate(&hashDictType, NULL);\n\n        while (hashTypeNext(hi) != C_ERR) {\n            sds key, value;\n\n            key = hashTypeCurrentObjectNewSds(hi,OBJ_HASH_KEY);\n            value = hashTypeCurrentObjectNewSds(hi,OBJ_HASH_VALUE);\n            ret = dictAdd(dict, key, value);\n            if (ret != DICT_OK) {\n                serverLogHexDump(LL_WARNING,\"ziplist with dup elements dump\",\n                    ptrFromObj(o),ziplistBlobLen((unsigned char*)ptrFromObj(o)));\n                serverPanic(\"Ziplist corruption detected\");\n            }\n        }\n        hashTypeReleaseIterator(hi);\n        zfree(ptrFromObj(o));\n        o->encoding = OBJ_ENCODING_HT;\n        o->m_ptr = dict;\n    } else {\n        serverPanic(\"Unknown hash encoding\");\n    }\n}\n\nvoid hashTypeConvert(robj *o, int enc) {\n    if (o->encoding == OBJ_ENCODING_ZIPLIST) {\n        hashTypeConvertZiplist(o, enc);\n    } else if (o->encoding == OBJ_ENCODING_HT) {\n        serverPanic(\"Not implemented\");\n    } else {\n        serverPanic(\"Unknown hash encoding\");\n    }\n}\n\n/* This is a helper function for the COPY command.\n * Duplicate a hash object, with the guarantee that the returned object\n * has the same encoding as the original one.\n *\n * The resulting object always has refcount set to 1 */\nrobj *hashTypeDup(robj *o) {\n    robj *hobj;\n    hashTypeIterator *hi;\n\n    serverAssert(o->type == OBJ_HASH);\n\n    if(o->encoding == OBJ_ENCODING_ZIPLIST){\n        unsigned char *zl = (unsigned char*)ptrFromObj(o);\n        size_t sz = ziplistBlobLen(zl);\n        unsigned char *new_zl = (unsigned char*)zmalloc(sz);\n        memcpy(new_zl, zl, sz);\n        hobj = createObject(OBJ_HASH, new_zl);\n        hobj->encoding = OBJ_ENCODING_ZIPLIST;\n    } else if(o->encoding == OBJ_ENCODING_HT){\n        dict *d = dictCreate(&hashDictType, NULL);\n        dictExpand(d, dictSize((const dict*)ptrFromObj(o)));\n\n        hi = hashTypeInitIterator(o);\n        while (hashTypeNext(hi) != C_ERR) {\n            sds field, value;\n            sds newfield, newvalue;\n            /* Extract a field-value pair from an original hash object.*/\n            field = hashTypeCurrentFromHashTable(hi, OBJ_HASH_KEY);\n            value = hashTypeCurrentFromHashTable(hi, OBJ_HASH_VALUE);\n            newfield = sdsdup(field);\n            newvalue = sdsdup(value);\n\n            /* Add a field-value pair to a new hash object. */\n            dictAdd(d,newfield,newvalue);\n        }\n        hashTypeReleaseIterator(hi);\n\n        hobj = createObject(OBJ_HASH, d);\n        hobj->encoding = OBJ_ENCODING_HT;\n    } else {\n        serverPanic(\"Unknown hash encoding\");\n    }\n    return hobj;\n}\n\nstruct hash_ziplist_data {\n    long count;\n    dict *fields;\n};\n\n/* callback for to check the ziplist doesn't have duplicate recoreds */\nstatic int _hashZiplistEntryValidation(unsigned char *p, void *userdata) {\n    hash_ziplist_data *data = (hash_ziplist_data*)userdata;\n\n    /* Odd records are field names, add to dict and check that's not a dup */\n    if (((data->count) & 1) == 0) {\n        unsigned char *str;\n        unsigned int slen;\n        long long vll;\n        if (!ziplistGet(p, &str, &slen, &vll))\n            return 0;\n        sds field = str? sdsnewlen(str, slen): sdsfromlonglong(vll);;\n        if (dictAdd(data->fields, field, NULL) != DICT_OK) {\n            /* Duplicate, return an error */\n            sdsfree(field);\n            return 0;\n        }\n    }\n\n    (data->count)++;\n    return 1;\n}\n\n/* Validate the integrity of the data structure.\n * when `deep` is 0, only the integrity of the header is validated.\n * when `deep` is 1, we scan all the entries one by one. */\nint hashZiplistValidateIntegrity(unsigned char *zl, size_t size, int deep) {\n    if (!deep)\n        return ziplistValidateIntegrity(zl, size, 0, NULL, NULL);\n\n    /* Keep track of the field names to locate duplicate ones */\n    hash_ziplist_data data = {0, dictCreate(&hashDictType, NULL)};\n\n    int ret = ziplistValidateIntegrity(zl, size, 1, _hashZiplistEntryValidation, &data);\n\n    /* make sure we have an even number of records. */\n    if (data.count & 1)\n        ret = 0;\n\n    dictRelease(data.fields);\n    return ret;\n}\n\n/* Create a new sds string from the ziplist entry. */\nsds hashSdsFromZiplistEntry(ziplistEntry *e) {\n    return e->sval ? sdsnewlen(e->sval, e->slen) : sdsfromlonglong(e->lval);\n}\n\n/* Reply with bulk string from the ziplist entry. */\nvoid hashReplyFromZiplistEntry(client *c, ziplistEntry *e) {\n    if (e->sval)\n        addReplyBulkCBuffer(c, e->sval, e->slen);\n    else\n        addReplyBulkLongLong(c, e->lval);\n}\n\n/* Return random element from a non empty hash.\n * 'key' and 'val' will be set to hold the element.\n * The memory in them is not to be freed or modified by the caller.\n * 'val' can be NULL in which case it's not extracted. */\nvoid hashTypeRandomElement(robj_roptr hashobj, unsigned long hashsize, ziplistEntry *key, ziplistEntry *val) {\n    if (hashobj->encoding == OBJ_ENCODING_HT) {\n        dictEntry *de = dictGetFairRandomKey((dict*)ptrFromObj(hashobj));\n        sds s = (sds)dictGetKey(de);\n        key->sval = (unsigned char*)s;\n        key->slen = sdslen(s);\n        if (val) {\n            sds s = (sds)dictGetVal(de);\n            val->sval = (unsigned char*)s;\n            val->slen = sdslen(s);\n        }\n    } else if (hashobj->encoding == OBJ_ENCODING_ZIPLIST) {\n        ziplistRandomPair((unsigned char*)ptrFromObj(hashobj), hashsize, key, val);\n    } else {\n        serverPanic(\"Unknown hash encoding\");\n    }\n}\n\n\n/*-----------------------------------------------------------------------------\n * Hash type commands\n *----------------------------------------------------------------------------*/\n\nvoid hsetnxCommand(client *c) {\n    robj *o;\n    if ((o = hashTypeLookupWriteOrCreate(c,c->argv[1])) == NULL) return;\n    hashTypeTryConversion(o,c->argv,2,3);\n\n    if (hashTypeExists(o, szFromObj(c->argv[2]))) {\n        addReply(c, shared.czero);\n    } else {\n        hashTypeSet(o,szFromObj(c->argv[2]),szFromObj(c->argv[3]),HASH_SET_COPY);\n        addReply(c, shared.cone);\n        signalModifiedKey(c,c->db,c->argv[1]);\n        notifyKeyspaceEvent(NOTIFY_HASH,\"hset\",c->argv[1],c->db->id);\n        g_pserver->dirty++;\n    }\n}\n\nvoid hsetCommand(client *c) {\n    int i, created = 0;\n    robj *o;\n\n    if ((c->argc % 2) == 1) {\n        addReplyErrorFormat(c,\"wrong number of arguments for '%s' command\",c->cmd->name);\n        return;\n    }\n\n    if ((o = hashTypeLookupWriteOrCreate(c,c->argv[1])) == NULL) return;\n    hashTypeTryConversion(o,c->argv,2,c->argc-1);\n\n    for (i = 2; i < c->argc; i += 2)\n        created += !hashTypeSet(o,szFromObj(c->argv[i]),szFromObj(c->argv[i+1]),HASH_SET_COPY);\n\n    /* HMSET (deprecated) and HSET return value is different. */\n    char *cmdname = szFromObj(c->argv[0]);\n    if (cmdname[1] == 's' || cmdname[1] == 'S') {\n        /* HSET */\n        addReplyLongLong(c, created);\n    } else {\n        /* HMSET */\n        addReply(c, shared.ok);\n    }\n    signalModifiedKey(c,c->db,c->argv[1]);\n    notifyKeyspaceEvent(NOTIFY_HASH,\"hset\",c->argv[1],c->db->id);\n    g_pserver->dirty += (c->argc - 2)/2;\n}\n\nvoid hincrbyCommand(client *c) {\n    long long value, incr, oldvalue;\n    robj *o;\n    sds newstr;\n    const unsigned char *vstr;\n    unsigned int vlen;\n\n    if (getLongLongFromObjectOrReply(c,c->argv[3],&incr,NULL) != C_OK) return;\n    if ((o = hashTypeLookupWriteOrCreate(c,c->argv[1])) == NULL) return;\n    if (hashTypeGetValue(o,szFromObj(c->argv[2]),&vstr,&vlen,&value) == C_OK) {\n        if (vstr) {\n            if (string2ll((char*)vstr,vlen,&value) == 0) {\n                addReplyError(c,\"hash value is not an integer\");\n                return;\n            }\n        } /* Else hashTypeGetValue() already stored it into &value */\n    } else {\n        value = 0;\n    }\n\n    oldvalue = value;\n    if ((incr < 0 && oldvalue < 0 && incr < (LLONG_MIN-oldvalue)) ||\n        (incr > 0 && oldvalue > 0 && incr > (LLONG_MAX-oldvalue))) {\n        addReplyError(c,\"increment or decrement would overflow\");\n        return;\n    }\n    value += incr;\n    newstr = sdsfromlonglong(value);\n    hashTypeSet(o,szFromObj(c->argv[2]),newstr,HASH_SET_TAKE_VALUE);\n    addReplyLongLong(c,value);\n    signalModifiedKey(c,c->db,c->argv[1]);\n    notifyKeyspaceEvent(NOTIFY_HASH,\"hincrby\",c->argv[1],c->db->id);\n    g_pserver->dirty++;\n}\n\nvoid hincrbyfloatCommand(client *c) {\n    long double value, incr;\n    long long ll;\n    robj *o;\n    sds newstr;\n    const unsigned char *vstr;\n    unsigned int vlen;\n\n    if (getLongDoubleFromObjectOrReply(c,c->argv[3],&incr,NULL) != C_OK) return;\n    if (isnan(incr) || isinf(incr)) {\n        addReplyError(c,\"value is NaN or Infinity\");\n        return;\n    }\n    if ((o = hashTypeLookupWriteOrCreate(c,c->argv[1])) == NULL) return;\n    if (hashTypeGetValue(o,szFromObj(c->argv[2]),&vstr,&vlen,&ll) == C_OK) {\n        if (vstr) {\n            if (string2ld((char*)vstr,vlen,&value) == 0) {\n                addReplyError(c,\"hash value is not a float\");\n                return;\n            }\n        } else {\n            value = (long double)ll;\n        }\n    } else {\n        value = 0;\n    }\n\n    value += incr;\n    if (std::isnan(value) || std::isinf(value)) {\n        addReplyError(c,\"increment would produce NaN or Infinity\");\n        return;\n    }\n\n    char buf[MAX_LONG_DOUBLE_CHARS];\n    int len = ld2string(buf,sizeof(buf),value,LD_STR_HUMAN);\n    newstr = sdsnewlen(buf,len);\n    hashTypeSet(o,szFromObj(c->argv[2]),newstr,HASH_SET_TAKE_VALUE);\n    addReplyBulkCBuffer(c,buf,len);\n    signalModifiedKey(c,c->db,c->argv[1]);\n    notifyKeyspaceEvent(NOTIFY_HASH,\"hincrbyfloat\",c->argv[1],c->db->id);\n    g_pserver->dirty++;\n\n    /* Always replicate HINCRBYFLOAT as an HSET command with the final value\n     * in order to make sure that differences in float precision or formatting\n     * will not create differences in replicas or after an AOF restart. */\n    robj *newobj;\n    newobj = createRawStringObject(buf,len);\n    rewriteClientCommandArgument(c,0,shared.hset);\n    rewriteClientCommandArgument(c,3,newobj);\n    decrRefCount(newobj);\n}\n\nstatic void addHashFieldToReply(client *c, robj_roptr o, sds field) {\n    int ret;\n\n    if (o == nullptr) {\n        addReplyNull(c);\n        return;\n    }\n\n    if (o->encoding == OBJ_ENCODING_ZIPLIST) {\n        const unsigned char *vstr = NULL;\n        unsigned int vlen = UINT_MAX;\n        long long vll = LLONG_MAX;\n\n        ret = hashTypeGetFromZiplist(o, field, &vstr, &vlen, &vll);\n        if (ret < 0) {\n            addReplyNull(c);\n        } else {\n            if (vstr) {\n                addReplyBulkCBuffer(c, vstr, vlen);\n            } else {\n                addReplyBulkLongLong(c, vll);\n            }\n        }\n\n    } else if (o->encoding == OBJ_ENCODING_HT) {\n        const char* value = hashTypeGetFromHashTable(o, field);\n        if (value == NULL)\n            addReplyNull(c);\n        else\n            addReplyBulkCBuffer(c, value, sdslen(value));\n    } else {\n        serverPanic(\"Unknown hash encoding\");\n    }\n}\n\nvoid hgetCommand(client *c) {\n    robj_roptr o;\n    AeLocker locker;\n    if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.null[c->resp], locker)) == nullptr ||\n        checkType(c,o,OBJ_HASH)) return;\n\n    addHashFieldToReply(c, o, szFromObj(c->argv[2]));\n}\n\nvoid hmgetCommand(client *c) {\n    robj_roptr o;\n    int i;\n    AeLocker locker;\n\n    /* Don't abort when the key cannot be found. Non-existing keys are empty\n     * hashes, where HMGET should respond with a series of null bulks. */\n    o = lookupKeyRead(c->db, c->argv[1], c->mvccCheckpoint, locker);\n    if (checkType(c,o,OBJ_HASH)) return;\n\n    addReplyArrayLen(c, c->argc-2);\n    for (i = 2; i < c->argc; i++) {\n        addHashFieldToReply(c, o, szFromObj(c->argv[i]));\n    }\n}\n\nvoid hdelCommand(client *c) {\n    robj *o;\n    int j, deleted = 0, keyremoved = 0;\n\n    if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||\n        checkType(c,o,OBJ_HASH)) return;\n\n    for (j = 2; j < c->argc; j++) {\n        if (hashTypeDelete(o,szFromObj(c->argv[j]))) {\n            deleted++;\n            if (hashTypeLength(o) == 0) {\n                dbDelete(c->db,c->argv[1]);\n                keyremoved = 1;\n                break;\n            }\n        }\n    }\n    if (deleted) {\n        signalModifiedKey(c,c->db,c->argv[1]);\n        notifyKeyspaceEvent(NOTIFY_HASH,\"hdel\",c->argv[1],c->db->id);\n        if (keyremoved)\n            notifyKeyspaceEvent(NOTIFY_GENERIC,\"del\",c->argv[1],\n                                c->db->id);\n        g_pserver->dirty += deleted;\n    }\n    addReplyLongLong(c,deleted);\n}\n\nvoid hlenCommand(client *c) {\n    robj_roptr o;\n\n    if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == nullptr ||\n        checkType(c,o,OBJ_HASH)) return;\n\n    addReplyLongLong(c,hashTypeLength(o));\n}\n\nvoid hstrlenCommand(client *c) {\n    robj_roptr o;\n\n    if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == nullptr ||\n        checkType(c,o,OBJ_HASH)) return;\n    addReplyLongLong(c,hashTypeGetValueLength(o,szFromObj(c->argv[2])));\n}\n\nstatic void addHashIteratorCursorToReply(client *c, hashTypeIterator *hi, int what) {\n    if (hi->encoding == OBJ_ENCODING_ZIPLIST) {\n        unsigned char *vstr = NULL;\n        unsigned int vlen = UINT_MAX;\n        long long vll = LLONG_MAX;\n\n        hashTypeCurrentFromZiplist(hi, what, &vstr, &vlen, &vll);\n        if (vstr)\n            addReplyBulkCBuffer(c, vstr, vlen);\n        else\n            addReplyBulkLongLong(c, vll);\n    } else if (hi->encoding == OBJ_ENCODING_HT) {\n        sds value = hashTypeCurrentFromHashTable(hi, what);\n        addReplyBulkCBuffer(c, value, sdslen(value));\n    } else {\n        serverPanic(\"Unknown hash encoding\");\n    }\n}\n\nvoid genericHgetallCommand(client *c, int flags) {\n    robj_roptr o;\n    hashTypeIterator *hi;\n    int length, count = 0;\n    AeLocker locker;\n\n    robj *emptyResp = (flags & OBJ_HASH_KEY && flags & OBJ_HASH_VALUE) ?\n        shared.emptymap[c->resp] : shared.emptyarray;\n    if ((o = lookupKeyReadOrReply(c,c->argv[1],emptyResp,locker))\n        == nullptr || checkType(c,o,OBJ_HASH)) return;\n\n    /* We return a map if the user requested keys and values, like in the\n     * HGETALL case. Otherwise to use a flat array makes more sense. */\n    length = hashTypeLength(o);\n    if (flags & OBJ_HASH_KEY && flags & OBJ_HASH_VALUE) {\n        addReplyMapLen(c, length);\n    } else {\n        addReplyArrayLen(c, length);\n    }\n\n    hi = hashTypeInitIterator(o);\n    while (hashTypeNext(hi) != C_ERR) {\n        if (flags & OBJ_HASH_KEY) {\n            addHashIteratorCursorToReply(c, hi, OBJ_HASH_KEY);\n            count++;\n        }\n        if (flags & OBJ_HASH_VALUE) {\n            addHashIteratorCursorToReply(c, hi, OBJ_HASH_VALUE);\n            count++;\n        }\n    }\n\n    hashTypeReleaseIterator(hi);\n\n    /* Make sure we returned the right number of elements. */\n    if (flags & OBJ_HASH_KEY && flags & OBJ_HASH_VALUE) count /= 2;\n    serverAssert(count == length);\n}\n\nvoid hkeysCommand(client *c) {\n    genericHgetallCommand(c,OBJ_HASH_KEY);\n}\n\nvoid hvalsCommand(client *c) {\n    genericHgetallCommand(c,OBJ_HASH_VALUE);\n}\n\nvoid hgetallCommand(client *c) {\n    genericHgetallCommand(c,OBJ_HASH_KEY|OBJ_HASH_VALUE);\n}\n\nvoid hexistsCommand(client *c) {\n    robj_roptr o;\n    if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == nullptr ||\n        checkType(c,o,OBJ_HASH)) return;\n\n    addReply(c, hashTypeExists(o,szFromObj(c->argv[2])) ? shared.cone : shared.czero);\n}\n\nvoid hscanCommand(client *c) {\n    robj_roptr o;\n    unsigned long cursor;\n    AeLocker locker;\n\n    if (parseScanCursorOrReply(c,c->argv[2],&cursor) == C_ERR) return;\n    if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptyscan,locker)) == nullptr ||\n        checkType(c,o,OBJ_HASH)) return;\n    scanGenericCommand(c,o,cursor);\n}\n\nvoid hrenameCommand(client *c) {\n    robj *o = nullptr;\n    const unsigned char *vstr;\n    unsigned int vlen;\n    long long ll;\n\n    if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.null[c->resp])) == nullptr ||\n        checkType(c,o,OBJ_HASH)) return;\n\n    if (hashTypeGetValue(o, szFromObj(c->argv[2]), &vstr, &vlen, &ll) != C_OK)\n    {\n        addReplyError(c, \"hash key doesn't exist\");\n        return;\n    }\n\n    sds sdsT = nullptr;\n    if (vstr != nullptr)\n    {\n        sdsT = sdsnewlen(vstr, vlen);\n    }\n    else\n    {\n        sdsT = sdsfromlonglong(ll);\n    }\n\n    hashTypeDelete(o, szFromObj(c->argv[2]));\n    hashTypeSet(o, szFromObj(c->argv[3]), sdsT, HASH_SET_TAKE_VALUE);\n    addReplyLongLong(c, 1);\n}\n\nstatic void harndfieldReplyWithZiplist(client *c, unsigned int count, ziplistEntry *keys, ziplistEntry *vals) {\n    for (unsigned long i = 0; i < count; i++) {\n        if (vals && c->resp > 2)\n            addReplyArrayLen(c,2);\n        if (keys[i].sval)\n            addReplyBulkCBuffer(c, keys[i].sval, keys[i].slen);\n        else\n            addReplyBulkLongLong(c, keys[i].lval);\n        if (vals) {\n            if (vals[i].sval)\n                addReplyBulkCBuffer(c, vals[i].sval, vals[i].slen);\n            else\n                addReplyBulkLongLong(c, vals[i].lval);\n        }\n    }\n}\n\n/* How many times bigger should be the hash compared to the requested size\n * for us to not use the \"remove elements\" strategy? Read later in the\n * implementation for more info. */\n#define HRANDFIELD_SUB_STRATEGY_MUL 3\n\n/* If client is trying to ask for a very large number of random elements,\n * queuing may consume an unlimited amount of memory, so we want to limit\n * the number of randoms per time. */\n#define HRANDFIELD_RANDOM_SAMPLE_LIMIT 1000\n\nvoid hrandfieldWithCountCommand(client *c, long l, int withvalues) {\n    unsigned long count, size;\n    int uniq = 1;\n    robj_roptr hash;\n\n    if ((hash = lookupKeyReadOrReply(c,c->argv[1],shared.emptyarray))\n        == nullptr || checkType(c,hash,OBJ_HASH)) return;\n    size = hashTypeLength(hash);\n\n    if(l >= 0) {\n        count = (unsigned long) l;\n    } else {\n        count = -l;\n        uniq = 0;\n    }\n\n    /* If count is zero, serve it ASAP to avoid special cases later. */\n    if (count == 0) {\n        addReply(c,shared.emptyarray);\n        return;\n    }\n\n    /* CASE 1: The count was negative, so the extraction method is just:\n     * \"return N random elements\" sampling the whole set every time.\n     * This case is trivial and can be served without auxiliary data\n     * structures. This case is the only one that also needs to return the\n     * elements in random order. */\n    if (!uniq || count == 1) {\n        if (withvalues && c->resp == 2)\n            addReplyArrayLen(c, count*2);\n        else\n            addReplyArrayLen(c, count);\n        if (hash->encoding == OBJ_ENCODING_HT) {\n            sds key, value;\n            while (count--) {\n                dictEntry *de = dictGetFairRandomKey((dict*)ptrFromObj(hash));\n                key = (sds)dictGetKey(de);\n                value = (sds)dictGetVal(de);\n                if (withvalues && c->resp > 2)\n                    addReplyArrayLen(c,2);\n                addReplyBulkCBuffer(c, key, sdslen(key));\n                if (withvalues)\n                    addReplyBulkCBuffer(c, value, sdslen(value));\n                if (c->flags & CLIENT_CLOSE_ASAP)\n                    break;\n            }\n        } else if (hash->encoding == OBJ_ENCODING_ZIPLIST) {\n            ziplistEntry *keys, *vals = NULL;\n            unsigned long limit, sample_count;\n            limit = count > HRANDFIELD_RANDOM_SAMPLE_LIMIT ? HRANDFIELD_RANDOM_SAMPLE_LIMIT : count;\n            keys = (ziplistEntry*)zmalloc(sizeof(ziplistEntry)*limit);\n            if (withvalues)\n                vals = (ziplistEntry*)zmalloc(sizeof(ziplistEntry)*limit);\n            while (count) {\n                sample_count = count > limit ? limit : count;\n                count -= sample_count;\n                ziplistRandomPairs((unsigned char*)ptrFromObj(hash), sample_count, keys, vals);\n                harndfieldReplyWithZiplist(c, sample_count, keys, vals);\n                if (c->flags & CLIENT_CLOSE_ASAP)\n                    break;\n            }\n            zfree(keys);\n            zfree(vals);\n        }\n        return;\n    }\n\n    /* Initiate reply count, RESP3 responds with nested array, RESP2 with flat one. */\n    long reply_size = count < size ? count : size;\n    if (withvalues && c->resp == 2)\n        addReplyArrayLen(c, reply_size*2);\n    else\n        addReplyArrayLen(c, reply_size);\n\n    /* CASE 2:\n    * The number of requested elements is greater than the number of\n    * elements inside the hash: simply return the whole hash. */\n    if(count >= size) {\n        hashTypeIterator *hi = hashTypeInitIterator(hash);\n        while (hashTypeNext(hi) != C_ERR) {\n            if (withvalues && c->resp > 2)\n                addReplyArrayLen(c,2);\n            addHashIteratorCursorToReply(c, hi, OBJ_HASH_KEY);\n            if (withvalues)\n                addHashIteratorCursorToReply(c, hi, OBJ_HASH_VALUE);\n        }\n        hashTypeReleaseIterator(hi);\n        return;\n    }\n\n    /* CASE 3:\n     * The number of elements inside the hash is not greater than\n     * HRANDFIELD_SUB_STRATEGY_MUL times the number of requested elements.\n     * In this case we create a hash from scratch with all the elements, and\n     * subtract random elements to reach the requested number of elements.\n     *\n     * This is done because if the number of requested elements is just\n     * a bit less than the number of elements in the hash, the natural approach\n     * used into CASE 4 is highly inefficient. */\n    if (count*HRANDFIELD_SUB_STRATEGY_MUL > size) {\n        dict *d = dictCreate(&sdsReplyDictType, NULL);\n        dictExpand(d, size);\n        hashTypeIterator *hi = hashTypeInitIterator(hash);\n\n        /* Add all the elements into the temporary dictionary. */\n        while ((hashTypeNext(hi)) != C_ERR) {\n            int ret = DICT_ERR;\n            sds key, value = NULL;\n\n            key = hashTypeCurrentObjectNewSds(hi,OBJ_HASH_KEY);\n            if (withvalues)\n                value = hashTypeCurrentObjectNewSds(hi,OBJ_HASH_VALUE);\n            ret = dictAdd(d, key, value);\n\n            serverAssert(ret == DICT_OK);\n        }\n        serverAssert(dictSize(d) == size);\n        hashTypeReleaseIterator(hi);\n\n        /* Remove random elements to reach the right count. */\n        while (size > count) {\n            dictEntry *de;\n            de = dictGetRandomKey(d);\n            dictUnlink(d,dictGetKey(de));\n            sdsfree((sds)dictGetKey(de));\n            sdsfree((sds)dictGetVal(de));\n            dictFreeUnlinkedEntry(d,de);\n            size--;\n        }\n\n        /* Reply with what's in the dict and release memory */\n        dictIterator *di;\n        dictEntry *de;\n        di = dictGetIterator(d);\n        while ((de = dictNext(di)) != NULL) {\n            sds key = (sds)dictGetKey(de);\n            sds value = (sds)dictGetVal(de);\n            if (withvalues && c->resp > 2)\n                addReplyArrayLen(c,2);\n            addReplyBulkSds(c, key);\n            if (withvalues)\n                addReplyBulkSds(c, value);\n        }\n\n        dictReleaseIterator(di);\n        dictRelease(d);\n    }\n\n    /* CASE 4: We have a big hash compared to the requested number of elements.\n     * In this case we can simply get random elements from the hash and add\n     * to the temporary hash, trying to eventually get enough unique elements\n     * to reach the specified count. */\n    else {\n        if (hash->encoding == OBJ_ENCODING_ZIPLIST) {\n            /* it is inefficient to repeatedly pick one random element from a\n             * ziplist. so we use this instead: */\n            ziplistEntry *keys, *vals = NULL;\n            keys = (ziplistEntry*)zmalloc(sizeof(ziplistEntry)*count);\n            if (withvalues)\n                vals = (ziplistEntry*)zmalloc(sizeof(ziplistEntry)*count);\n            serverAssert(ziplistRandomPairsUnique((unsigned char*)ptrFromObj(hash), count, keys, vals) == count);\n            harndfieldReplyWithZiplist(c, count, keys, vals);\n            zfree(keys);\n            zfree(vals);\n            return;\n        }\n\n        /* Hashtable encoding (generic implementation) */\n        unsigned long added = 0;\n        ziplistEntry key, value;\n        dict *d = dictCreate(&hashDictType, NULL);\n        dictExpand(d, count);\n        while(added < count) {\n            hashTypeRandomElement(hash, size, &key, withvalues? &value : NULL);\n\n            /* Try to add the object to the dictionary. If it already exists\n            * free it, otherwise increment the number of objects we have\n            * in the result dictionary. */\n            sds skey = hashSdsFromZiplistEntry(&key);\n            if (dictAdd(d,skey,NULL) != DICT_OK) {\n                sdsfree(skey);\n                continue;\n            }\n            added++;\n\n            /* We can reply right away, so that we don't need to store the value in the dict. */\n            if (withvalues && c->resp > 2)\n                addReplyArrayLen(c,2);\n            hashReplyFromZiplistEntry(c, &key);\n            if (withvalues)\n                hashReplyFromZiplistEntry(c, &value);\n        }\n\n        /* Release memory */\n        dictRelease(d);\n    }\n}\n\n/* HRANDFIELD key [<count> [WITHVALUES]] */\nvoid hrandfieldCommand(client *c) {\n    long l;\n    int withvalues = 0;\n    robj_roptr hash;\n    ziplistEntry ele;\n\n    if (c->argc >= 3) {\n        if (getRangeLongFromObjectOrReply(c,c->argv[2],-LONG_MAX,LONG_MAX,&l,NULL) != C_OK) return;\n        if (c->argc > 4 || (c->argc == 4 && strcasecmp(szFromObj(c->argv[3]),\"withvalues\"))) {\n            addReplyErrorObject(c,shared.syntaxerr);\n            return;\n        } else if (c->argc == 4) {\n            withvalues = 1;\n            if (l < -g_pserver->rand_total_threshold || l > g_pserver->rand_total_threshold) {\n                addReplyError(c,\"value is out of range\");\n                return;\n            }\n        }\n        hrandfieldWithCountCommand(c, l, withvalues);\n        return;\n    }\n\n    /* Handle variant without <count> argument. Reply with simple bulk string */\n    if ((hash = lookupKeyReadOrReply(c,c->argv[1],shared.null[c->resp])) == nullptr ||\n        checkType(c,hash,OBJ_HASH)) {\n        return;\n    }\n\n    hashTypeRandomElement(hash,hashTypeLength(hash),&ele,NULL);\n    hashReplyFromZiplistEntry(c, &ele);\n}\n"
  },
  {
    "path": "src/t_list.cpp",
    "content": "/*\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n\n#define LIST_MAX_ITEM_SIZE ((1ull<<32)-1024)\n\n/*-----------------------------------------------------------------------------\n * List API\n *----------------------------------------------------------------------------*/\n\n/* The function pushes an element to the specified list object 'subject',\n * at head or tail position as specified by 'where'.\n *\n * There is no need for the caller to increment the refcount of 'value' as\n * the function takes care of it if needed. */\nvoid listTypePush(robj *subject, robj *value, int where) {\n    if (subject->encoding == OBJ_ENCODING_QUICKLIST) {\n        int pos = (where == LIST_HEAD) ? QUICKLIST_HEAD : QUICKLIST_TAIL;\n        if (value->encoding == OBJ_ENCODING_INT) {\n            char buf[32];\n            ll2string(buf, 32, (long)ptrFromObj(value));\n            quicklistPush((quicklist*)ptrFromObj(subject), buf, strlen(buf), pos);\n        } else {\n            quicklistPush((quicklist*)ptrFromObj(subject), ptrFromObj(value), sdslen(szFromObj(value)), pos);\n        }\n    } else {\n        serverPanic(\"Unknown list encoding\");\n    }\n}\n\nvoid *listPopSaver(unsigned char *data, unsigned int sz) {\n    return createStringObject((char*)data,sz);\n}\n\nrobj *listTypePop(robj *subject, int where) {\n    long long vlong;\n    robj *value = NULL;\n\n    int ql_where = where == LIST_HEAD ? QUICKLIST_HEAD : QUICKLIST_TAIL;\n    if (subject->encoding == OBJ_ENCODING_QUICKLIST) {\n        if (quicklistPopCustom((quicklist*)ptrFromObj(subject), ql_where, (unsigned char **)&value,\n                               NULL, &vlong, listPopSaver)) {\n            if (!value)\n                value = createStringObjectFromLongLong(vlong);\n        }\n    } else {\n        serverPanic(\"Unknown list encoding\");\n    }\n    return value;\n}\n\nunsigned long listTypeLength(robj_roptr subject) {\n    if (subject->encoding == OBJ_ENCODING_QUICKLIST) {\n        return quicklistCount((const quicklist*)ptrFromObj(subject));\n    } else {\n        serverPanic(\"Unknown list encoding\");\n    }\n}\n\n/* Initialize an iterator at the specified index. */\nlistTypeIterator *listTypeInitIterator(robj_roptr subject, long index,\n                                       unsigned char direction) {\n    listTypeIterator *li = (listTypeIterator*)zmalloc(sizeof(listTypeIterator), MALLOC_LOCAL);\n    li->subject = subject;\n    li->encoding = subject->encoding;\n    li->direction = direction;\n    li->iter = NULL;\n    /* LIST_HEAD means start at TAIL and move *towards* head.\n     * LIST_TAIL means start at HEAD and move *towards tail. */\n    int iter_direction =\n        direction == LIST_HEAD ? AL_START_TAIL : AL_START_HEAD;\n    if (li->encoding == OBJ_ENCODING_QUICKLIST) {\n        li->iter = quicklistGetIteratorAtIdx((const quicklist*)ptrFromObj(li->subject),\n                                             iter_direction, index);\n    } else {\n        serverPanic(\"Unknown list encoding\");\n    }\n    return li;\n}\n\n/* Clean up the iterator. */\nvoid listTypeReleaseIterator(listTypeIterator *li) {\n    zfree(li->iter);\n    zfree(li);\n}\n\n/* Stores pointer to current the entry in the provided entry structure\n * and advances the position of the iterator. Returns 1 when the current\n * entry is in fact an entry, 0 otherwise. */\nint listTypeNext(listTypeIterator *li, listTypeEntry *entry) {\n    /* Protect from converting when iterating */\n    serverAssert(li->subject->encoding == li->encoding);\n\n    entry->li = li;\n    if (li->encoding == OBJ_ENCODING_QUICKLIST) {\n        return quicklistNext(li->iter, &entry->entry);\n    } else {\n        serverPanic(\"Unknown list encoding\");\n    }\n    return 0;\n}\n\n/* Return entry or NULL at the current position of the iterator. */\nrobj *listTypeGet(listTypeEntry *entry) {\n    robj *value = NULL;\n    if (entry->li->encoding == OBJ_ENCODING_QUICKLIST) {\n        if (entry->entry.value) {\n            value = createStringObject((char *)entry->entry.value,\n                                       entry->entry.sz);\n        } else {\n            value = createStringObjectFromLongLong(entry->entry.longval);\n        }\n    } else {\n        serverPanic(\"Unknown list encoding\");\n    }\n    return value;\n}\n\nvoid listTypeInsert(listTypeEntry *entry, robj *value, int where) {\n    if (entry->li->encoding == OBJ_ENCODING_QUICKLIST) {\n        value = getDecodedObject(value);\n        sds str = szFromObj(value);\n        size_t len = sdslen(str);\n        if (where == LIST_TAIL) {\n            quicklistInsertAfter((quicklist *)entry->entry.qlist,\n                                 &entry->entry, str, len);\n        } else if (where == LIST_HEAD) {\n            quicklistInsertBefore((quicklist *)entry->entry.qlist,\n                                  &entry->entry, str, len);\n        }\n        decrRefCount(value);\n    } else {\n        serverPanic(\"Unknown list encoding\");\n    }\n}\n\n/* Compare the given object with the entry at the current position. */\nint listTypeEqual(listTypeEntry *entry, robj *o) {\n    if (entry->li->encoding == OBJ_ENCODING_QUICKLIST) {\n        serverAssertWithInfo(NULL,o,sdsEncodedObject(o));\n        return quicklistCompare(entry->entry.zi,(unsigned char*)ptrFromObj(o),sdslen(szFromObj(o)));\n    } else {\n        serverPanic(\"Unknown list encoding\");\n    }\n}\n\n/* Delete the element pointed to. */\nvoid listTypeDelete(listTypeIterator *iter, listTypeEntry *entry) {\n    if (entry->li->encoding == OBJ_ENCODING_QUICKLIST) {\n        quicklistDelEntry(iter->iter, &entry->entry);\n    } else {\n        serverPanic(\"Unknown list encoding\");\n    }\n}\n\n/* Create a quicklist from a single ziplist */\nvoid listTypeConvert(robj *subject, int enc) {\n    serverAssertWithInfo(NULL,subject,subject->type==OBJ_LIST);\n    serverAssertWithInfo(NULL,subject,subject->encoding==OBJ_ENCODING_ZIPLIST);\n\n    if (enc == OBJ_ENCODING_QUICKLIST) {\n        size_t zlen = g_pserver->list_max_ziplist_size;\n        int depth = g_pserver->list_compress_depth;\n        subject->m_ptr = quicklistCreateFromZiplist(zlen, depth, (unsigned char*)ptrFromObj(subject));\n        subject->encoding = OBJ_ENCODING_QUICKLIST;\n    } else {\n        serverPanic(\"Unsupported list conversion\");\n    }\n}\n\n/* This is a helper function for the COPY command.\n * Duplicate a list object, with the guarantee that the returned object\n * has the same encoding as the original one.\n *\n * The resulting object always has refcount set to 1 */\nrobj *listTypeDup(robj *o) {\n    robj *lobj;\n\n    serverAssert(o->type == OBJ_LIST);\n\n    switch (o->encoding) {\n        case OBJ_ENCODING_QUICKLIST:\n            lobj = createObject(OBJ_LIST, quicklistDup((quicklist*)ptrFromObj(o)));\n            lobj->encoding = OBJ_ENCODING_QUICKLIST;\n            break;\n        default:\n            serverPanic(\"Unknown list encoding\");\n            break;\n    }\n    return lobj;\n}\n\n/*-----------------------------------------------------------------------------\n * List Commands\n *----------------------------------------------------------------------------*/\n\n/* Implements LPUSH/RPUSH/LPUSHX/RPUSHX. \n * 'xx': push if key exists. */\nvoid pushGenericCommand(client *c, int where, int xx) {\n    int j;\n\n    for (j = 2; j < c->argc; j++) {\n        if (sdslen(szFromObj(c->argv[j])) > LIST_MAX_ITEM_SIZE) {\n            addReplyError(c, \"Element too large\");\n            return;\n        }\n    }\n\n    robj *lobj = lookupKeyWrite(c->db, c->argv[1]);\n    if (checkType(c,lobj,OBJ_LIST)) return;\n    if (!lobj) {\n        if (xx) {\n            addReply(c, shared.czero);\n            return;\n        }\n\n        lobj = createQuicklistObject();\n        quicklistSetOptions((quicklist*)ptrFromObj(lobj), g_pserver->list_max_ziplist_size,\n                            g_pserver->list_compress_depth);\n        dbAdd(c->db,c->argv[1],lobj);\n    }\n\n    for (j = 2; j < c->argc; j++) {\n        listTypePush(lobj,c->argv[j],where);\n        g_pserver->dirty++;\n    }\n\n    addReplyLongLong(c, listTypeLength(lobj));\n\n    const char *event = (where == LIST_HEAD) ? \"lpush\" : \"rpush\";\n    signalModifiedKey(c,c->db,c->argv[1]);\n    notifyKeyspaceEvent(NOTIFY_LIST,event,c->argv[1],c->db->id);\n}\n\n/* LPUSH <key> <element> [<element> ...] */\nvoid lpushCommand(client *c) {\n    pushGenericCommand(c,LIST_HEAD,0);\n}\n\n/* RPUSH <key> <element> [<element> ...] */\nvoid rpushCommand(client *c) {\n    pushGenericCommand(c,LIST_TAIL,0);\n}\n\n/* LPUSHX <key> <element> [<element> ...] */\nvoid lpushxCommand(client *c) {\n    pushGenericCommand(c,LIST_HEAD,1);\n}\n\n/* RPUSH <key> <element> [<element> ...] */\nvoid rpushxCommand(client *c) {\n    pushGenericCommand(c,LIST_TAIL,1);\n}\n\n/* LINSERT <key> (BEFORE|AFTER) <pivot> <element> */\nvoid linsertCommand(client *c) {\n    int where;\n    robj *subject;\n    listTypeIterator *iter;\n    listTypeEntry entry;\n    int inserted = 0;\n\n    if (strcasecmp(szFromObj(c->argv[2]),\"after\") == 0) {\n        where = LIST_TAIL;\n    } else if (strcasecmp(szFromObj(c->argv[2]),\"before\") == 0) {\n        where = LIST_HEAD;\n    } else {\n        addReplyErrorObject(c,shared.syntaxerr);\n        return;\n    }\n\n    if (sdslen(szFromObj(c->argv[4])) > LIST_MAX_ITEM_SIZE) {\n        addReplyError(c, \"Element too large\");\n        return;\n    }\n\n    if ((subject = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||\n        checkType(c,subject,OBJ_LIST)) return;\n\n    /* Seek pivot from head to tail */\n    iter = listTypeInitIterator(subject,0,LIST_TAIL);\n    while (listTypeNext(iter,&entry)) {\n        if (listTypeEqual(&entry,c->argv[3])) {\n            listTypeInsert(&entry,c->argv[4],where);\n            inserted = 1;\n            break;\n        }\n    }\n    listTypeReleaseIterator(iter);\n\n    if (inserted) {\n        signalModifiedKey(c,c->db,c->argv[1]);\n        notifyKeyspaceEvent(NOTIFY_LIST,\"linsert\",\n                            c->argv[1],c->db->id);\n        g_pserver->dirty++;\n    } else {\n        /* Notify client of a failed insert */\n        addReplyLongLong(c,-1);\n        return;\n    }\n\n    addReplyLongLong(c,listTypeLength(subject));\n}\n\n/* LLEN <key> */\nvoid llenCommand(client *c) {\n    robj_roptr o = lookupKeyReadOrReply(c,c->argv[1],shared.czero);\n    if (o == nullptr || checkType(c,o,OBJ_LIST)) return;\n    addReplyLongLong(c,listTypeLength(o));\n}\n\n/* LINDEX <key> <index> */\nvoid lindexCommand(client *c) {\n    robj_roptr o = lookupKeyReadOrReply(c,c->argv[1],shared.null[c->resp]);\n    if (o == nullptr || checkType(c,o,OBJ_LIST)) return;\n    long index;\n\n    if ((getLongFromObjectOrReply(c, c->argv[2], &index, NULL) != C_OK))\n        return;\n\n    if (o->encoding == OBJ_ENCODING_QUICKLIST) {\n        quicklistEntry entry;\n        if (quicklistIndex((quicklist*)ptrFromObj(o), index, &entry)) {\n            if (entry.value) {\n                addReplyBulkCBuffer(c, entry.value, entry.sz);\n            } else {\n                addReplyBulkLongLong(c, entry.longval);\n            }\n        } else {\n            addReplyNull(c);\n        }\n    } else {\n        serverPanic(\"Unknown list encoding\");\n    }\n}\n\n/* LSET <key> <index> <element> */\nvoid lsetCommand(client *c) {\n    robj *o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr);\n    if (o == NULL || checkType(c,o,OBJ_LIST)) return;\n    long index;\n    robj *value = c->argv[3];\n\n    if (sdslen(szFromObj(value)) > LIST_MAX_ITEM_SIZE) {\n        addReplyError(c, \"Element too large\");\n        return;\n    }\n\n    if ((getLongFromObjectOrReply(c, c->argv[2], &index, NULL) != C_OK))\n        return;\n\n    if (o->encoding == OBJ_ENCODING_QUICKLIST) {\n        quicklist *ql = (quicklist*)ptrFromObj(o);\n        int replaced = quicklistReplaceAtIndex(ql, index,\n                                               szFromObj(value), sdslen(szFromObj(value)));\n        if (!replaced) {\n            addReplyErrorObject(c,shared.outofrangeerr);\n        } else {\n            addReply(c,shared.ok);\n            signalModifiedKey(c,c->db,c->argv[1]);\n            notifyKeyspaceEvent(NOTIFY_LIST,\"lset\",c->argv[1],c->db->id);\n            g_pserver->dirty++;\n        }\n    } else {\n        serverPanic(\"Unknown list encoding\");\n    }\n}\n\n/* A helper for replying with a list's range between the inclusive start and end\n * indexes as multi-bulk, with support for negative indexes. Note that start\n * must be less than end or an empty array is returned. When the reverse\n * argument is set to a non-zero value, the reply is reversed so that elements\n * are returned from end to start. */\nvoid addListRangeReply(client *c, robj_roptr o, long start, long end, int reverse) {\n    long rangelen, llen = listTypeLength(o);\n\n    /* Convert negative indexes. */\n    if (start < 0) start = llen+start;\n    if (end < 0) end = llen+end;\n    if (start < 0) start = 0;\n\n    /* Invariant: start >= 0, so this test will be true when end < 0.\n     * The range is empty when start > end or start >= length. */\n    if (start > end || start >= llen) {\n        addReply(c,shared.emptyarray);\n        return;\n    }\n    if (end >= llen) end = llen-1;\n    rangelen = (end-start)+1;\n\n    /* Return the result in form of a multi-bulk reply */\n    addReplyArrayLen(c,rangelen);\n    if (o->encoding == OBJ_ENCODING_QUICKLIST) {\n        int from = reverse ? end : start;\n        int direction = reverse ? LIST_HEAD : LIST_TAIL;\n        listTypeIterator *iter = listTypeInitIterator(o,from,direction);\n\n        while(rangelen--) {\n            listTypeEntry entry;\n            listTypeNext(iter, &entry);\n            quicklistEntry *qe = &entry.entry;\n            if (qe->value) {\n                addReplyBulkCBuffer(c,qe->value,qe->sz);\n            } else {\n                addReplyBulkLongLong(c,qe->longval);\n            }\n        }\n        listTypeReleaseIterator(iter);\n    } else {\n        serverPanic(\"Unknown list encoding\");\n    }\n}\n\n/* A housekeeping helper for list elements popping tasks. */\nvoid listElementsRemoved(client *c, robj *key, int where, robj *o, long count) {\n    const char *event = (where == LIST_HEAD) ? \"lpop\" : \"rpop\";\n\n    notifyKeyspaceEvent(NOTIFY_LIST, event, key, c->db->id);\n    if (listTypeLength(o) == 0) {\n        notifyKeyspaceEvent(NOTIFY_GENERIC, \"del\", key, c->db->id);\n        dbDelete(c->db, key);\n    }\n    signalModifiedKey(c, c->db, key);\n    g_pserver->dirty += count;\n}\n\n/* Implements the generic list pop operation for LPOP/RPOP.\n * The where argument specifies which end of the list is operated on. An\n * optional count may be provided as the third argument of the client's\n * command. */\nvoid popGenericCommand(client *c, int where) {\n    long count = 0;\n    robj *value;\n\n    if (c->argc > 3) {\n        addReplyErrorFormat(c,\"wrong number of arguments for '%s' command\",\n                            c->cmd->name);\n        return;\n    } else if (c->argc == 3) {\n        /* Parse the optional count argument. */\n        if (getPositiveLongFromObjectOrReply(c,c->argv[2],&count,NULL) != C_OK) \n            return;\n        if (count == 0) {\n            /* Fast exit path. */\n            addReplyNullArray(c);\n            return;\n        }\n    }\n\n    robj *o = lookupKeyWriteOrReply(c, c->argv[1], shared.null[c->resp]);\n    if (o == NULL || checkType(c, o, OBJ_LIST))\n        return;\n\n    if (!count) {\n        /* Pop a single element. This is POP's original behavior that replies\n         * with a bulk string. */\n        value = listTypePop(o,where);\n        serverAssert(value != NULL);\n        addReplyBulk(c,value);\n        decrRefCount(value);\n        listElementsRemoved(c,c->argv[1],where,o,1);\n    } else {\n        /* Pop a range of elements. An addition to the original POP command,\n         *  which replies with a multi-bulk. */\n        long llen = listTypeLength(o);\n        long rangelen = (count > llen) ? llen : count;\n        long rangestart = (where == LIST_HEAD) ? 0 : -rangelen;\n        long rangeend = (where == LIST_HEAD) ? rangelen - 1 : -1;\n        int reverse = (where == LIST_HEAD) ? 0 : 1;\n\n        addListRangeReply(c,o,rangestart,rangeend,reverse);\n        quicklistDelRange((quicklist*)ptrFromObj(o),rangestart,rangelen);\n        listElementsRemoved(c,c->argv[1],where,o,rangelen);\n    }\n}\n\n/* LPOP <key> [count] */\nvoid lpopCommand(client *c) {\n    popGenericCommand(c,LIST_HEAD);\n}\n\n/* RPOP <key> [count] */\nvoid rpopCommand(client *c) {\n    popGenericCommand(c,LIST_TAIL);\n}\n\n/* LRANGE <key> <start> <stop> */\nvoid lrangeCommand(client *c) {\n    robj_roptr o;\n    long start, end;\n\n    if ((getLongFromObjectOrReply(c, c->argv[2], &start, NULL) != C_OK) ||\n        (getLongFromObjectOrReply(c, c->argv[3], &end, NULL) != C_OK)) return;\n\n    if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptyarray)) == nullptr\n         || checkType(c,o,OBJ_LIST)) return;\n\n    addListRangeReply(c,o,start,end,0);\n}\n\n/* LTRIM <key> <start> <stop> */\nvoid ltrimCommand(client *c) {\n    robj *o;\n    long start, end, llen, ltrim, rtrim;\n\n    if ((getLongFromObjectOrReply(c, c->argv[2], &start, NULL) != C_OK) ||\n        (getLongFromObjectOrReply(c, c->argv[3], &end, NULL) != C_OK)) return;\n\n    if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.ok)) == nullptr ||\n        checkType(c,o,OBJ_LIST)) return;\n    llen = listTypeLength(o);\n\n    /* convert negative indexes */\n    if (start < 0) start = llen+start;\n    if (end < 0) end = llen+end;\n    if (start < 0) start = 0;\n\n    /* Invariant: start >= 0, so this test will be true when end < 0.\n     * The range is empty when start > end or start >= length. */\n    if (start > end || start >= llen) {\n        /* Out of range start or start > end result in empty list */\n        ltrim = llen;\n        rtrim = 0;\n    } else {\n        if (end >= llen) end = llen-1;\n        ltrim = start;\n        rtrim = llen-end-1;\n    }\n\n    /* Remove list elements to perform the trim */\n    if (o->encoding == OBJ_ENCODING_QUICKLIST) {\n        quicklistDelRange((quicklist*)ptrFromObj(o),0,ltrim);\n        quicklistDelRange((quicklist*)ptrFromObj(o),-rtrim,rtrim);\n    } else {\n        serverPanic(\"Unknown list encoding\");\n    }\n\n    notifyKeyspaceEvent(NOTIFY_LIST,\"ltrim\",c->argv[1],c->db->id);\n    if (listTypeLength(o) == 0) {\n        dbDelete(c->db,c->argv[1]);\n        notifyKeyspaceEvent(NOTIFY_GENERIC,\"del\",c->argv[1],c->db->id);\n    }\n    signalModifiedKey(c,c->db,c->argv[1]);\n    g_pserver->dirty += (ltrim + rtrim);\n    addReply(c,shared.ok);\n}\n\n/* LPOS key element [RANK rank] [COUNT num-matches] [MAXLEN len]\n *\n * The \"rank\" is the position of the match, so if it is 1, the first match\n * is returned, if it is 2 the second match is returned and so forth.\n * It is 1 by default. If negative has the same meaning but the search is\n * performed starting from the end of the list.\n *\n * If COUNT is given, instead of returning the single element, a list of\n * all the matching elements up to \"num-matches\" are returned. COUNT can\n * be combiled with RANK in order to returning only the element starting\n * from the Nth. If COUNT is zero, all the matching elements are returned.\n *\n * MAXLEN tells the command to scan a max of len elements. If zero (the\n * default), all the elements in the list are scanned if needed.\n *\n * The returned elements indexes are always referring to what LINDEX\n * would return. So first element from head is 0, and so forth. */\nvoid lposCommand(client *c) {\n    robj_roptr o;\n    robj *ele = c->argv[2];\n    int direction = LIST_TAIL;\n    long rank = 1, count = -1, maxlen = 0; /* Count -1: option not given. */\n\n    if (sdslen(szFromObj(ele)) > LIST_MAX_ITEM_SIZE) {\n        addReplyError(c, \"Element too large\");\n        return;\n    }\n\n    /* Parse the optional arguments. */\n    for (int j = 3; j < c->argc; j++) {\n        char *opt = szFromObj(c->argv[j]);\n        int moreargs = (c->argc-1)-j;\n\n        if (!strcasecmp(opt,\"RANK\") && moreargs) {\n            j++;\n            if (getLongFromObjectOrReply(c, c->argv[j], &rank, NULL) != C_OK)\n                return;\n            if (rank == 0) {\n                addReplyError(c,\"RANK can't be zero: use 1 to start from \"\n                                \"the first match, 2 from the second, ...\");\n                return;\n            }\n        } else if (!strcasecmp(opt,\"COUNT\") && moreargs) {\n            j++;\n            if (getPositiveLongFromObjectOrReply(c, c->argv[j], &count,\n              \"COUNT can't be negative\") != C_OK)\n                return;\n        } else if (!strcasecmp(opt,\"MAXLEN\") && moreargs) {\n            j++;\n            if (getPositiveLongFromObjectOrReply(c, c->argv[j], &maxlen, \n              \"MAXLEN can't be negative\") != C_OK)\n                return;\n        } else {\n            addReplyErrorObject(c,shared.syntaxerr);\n            return;\n        }\n    }\n\n    /* A negative rank means start from the tail. */\n    if (rank < 0) {\n        rank = -rank;\n        direction = LIST_HEAD;\n    }\n\n    /* We return NULL or an empty array if there is no such key (or\n     * if we find no matches, depending on the presence of the COUNT option. */\n    if ((o = lookupKeyRead(c->db,c->argv[1])) == nullptr) {\n        if (count != -1)\n            addReply(c,shared.emptyarray);\n        else\n            addReply(c,shared.null[c->resp]);\n        return;\n    }\n    if (checkType(c,o,OBJ_LIST)) return;\n\n    /* If we got the COUNT option, prepare to emit an array. */\n    void *arraylenptr = NULL;\n    if (count != -1) arraylenptr = addReplyDeferredLen(c);\n\n    /* Seek the element. */\n    listTypeIterator *li;\n    li = listTypeInitIterator(o,direction == LIST_HEAD ? -1 : 0,direction);\n    listTypeEntry entry;\n    long llen = listTypeLength(o);\n    long index = 0, matches = 0, matchindex = -1, arraylen = 0;\n    while (listTypeNext(li,&entry) && (maxlen == 0 || index < maxlen)) {\n        if (listTypeEqual(&entry,ele)) {\n            matches++;\n            matchindex = (direction == LIST_TAIL) ? index : llen - index - 1;\n            if (matches >= rank) {\n                if (arraylenptr) {\n                    arraylen++;\n                    addReplyLongLong(c,matchindex);\n                    if (count && matches-rank+1 >= count) break;\n                } else {\n                    break;\n                }\n            }\n        }\n        index++;\n        matchindex = -1; /* Remember if we exit the loop without a match. */\n    }\n    listTypeReleaseIterator(li);\n\n    /* Reply to the client. Note that arraylenptr is not NULL only if\n     * the COUNT option was selected. */\n    if (arraylenptr != NULL) {\n        setDeferredArrayLen(c,arraylenptr,arraylen);\n    } else {\n        if (matchindex != -1)\n            addReplyLongLong(c,matchindex);\n        else\n            addReply(c,shared.null[c->resp]);\n    }\n}\n\n/* LREM <key> <count> <element> */\nvoid lremCommand(client *c) {\n    robj *subject, *obj;\n    obj = c->argv[3];\n    long toremove;\n    long removed = 0;\n\n    if (sdslen(szFromObj(obj)) > LIST_MAX_ITEM_SIZE) {\n        addReplyError(c, \"Element too large\");\n        return;\n    }\n\n    if ((getLongFromObjectOrReply(c, c->argv[2], &toremove, NULL) != C_OK))\n        return;\n\n    subject = lookupKeyWriteOrReply(c,c->argv[1],shared.czero);\n    if (subject == NULL || checkType(c,subject,OBJ_LIST)) return;\n\n    listTypeIterator *li;\n    if (toremove < 0) {\n        toremove = -toremove;\n        li = listTypeInitIterator(subject,-1,LIST_HEAD);\n    } else {\n        li = listTypeInitIterator(subject,0,LIST_TAIL);\n    }\n\n    listTypeEntry entry;\n    while (listTypeNext(li,&entry)) {\n        if (listTypeEqual(&entry,obj)) {\n            listTypeDelete(li, &entry);\n            g_pserver->dirty++;\n            removed++;\n            if (toremove && removed == toremove) break;\n        }\n    }\n    listTypeReleaseIterator(li);\n\n    if (removed) {\n        signalModifiedKey(c,c->db,c->argv[1]);\n        notifyKeyspaceEvent(NOTIFY_LIST,\"lrem\",c->argv[1],c->db->id);\n    }\n\n    if (listTypeLength(subject) == 0) {\n        dbDelete(c->db,c->argv[1]);\n        notifyKeyspaceEvent(NOTIFY_GENERIC,\"del\",c->argv[1],c->db->id);\n    }\n\n    addReplyLongLong(c,removed);\n}\n\nvoid lmoveHandlePush(client *c, robj *dstkey, robj *dstobj, robj *value,\n                     int where) {\n    /* Create the list if the key does not exist */\n    if (!dstobj) {\n        dstobj = createQuicklistObject();\n        quicklistSetOptions((quicklist*)ptrFromObj(dstobj), g_pserver->list_max_ziplist_size,\n                            g_pserver->list_compress_depth);\n        dbAdd(c->db,dstkey,dstobj);\n    }\n    signalModifiedKey(c,c->db,dstkey);\n    listTypePush(dstobj,value,where);\n    notifyKeyspaceEvent(NOTIFY_LIST,\n                        where == LIST_HEAD ? \"lpush\" : \"rpush\",\n                        dstkey,\n                        c->db->id);\n    /* Always send the pushed value to the client. */\n    addReplyBulk(c,value);\n}\n\nint getListPositionFromObjectOrReply(client *c, robj *arg, int *position) {\n    if (strcasecmp(szFromObj(arg),\"right\") == 0) {\n        *position = LIST_TAIL;\n    } else if (strcasecmp(szFromObj(arg),\"left\") == 0) {\n        *position = LIST_HEAD;\n    } else {\n        addReplyErrorObject(c,shared.syntaxerr);\n        return C_ERR;\n    }\n    return C_OK;\n}\n\nrobj *getStringObjectFromListPosition(int position) {\n    if (position == LIST_HEAD) {\n        return shared.left;\n    } else {\n        // LIST_TAIL\n        return shared.right;\n    }\n}\n\nvoid lmoveGenericCommand(client *c, int wherefrom, int whereto) {\n    robj *sobj, *value;\n    if ((sobj = lookupKeyWriteOrReply(c,c->argv[1],shared.null[c->resp]))\n        == NULL || checkType(c,sobj,OBJ_LIST)) return;\n\n    if (listTypeLength(sobj) == 0) {\n        /* This may only happen after loading very old RDB files. Recent\n         * versions of Redis delete keys of empty lists. */\n        addReplyNull(c);\n    } else {\n        robj *dobj = lookupKeyWrite(c->db,c->argv[2]);\n        robj *touchedkey = c->argv[1];\n\n        if (checkType(c,dobj,OBJ_LIST)) return;\n        value = listTypePop(sobj,wherefrom);\n        serverAssert(value); /* assertion for valgrind (avoid NPD) */\n        lmoveHandlePush(c,c->argv[2],dobj,value,whereto);\n\n        /* listTypePop returns an object with its refcount incremented */\n        decrRefCount(value);\n\n        /* Delete the source list when it is empty */\n        notifyKeyspaceEvent(NOTIFY_LIST,\n                            wherefrom == LIST_HEAD ? \"lpop\" : \"rpop\",\n                            touchedkey,\n                            c->db->id);\n        if (listTypeLength(sobj) == 0) {\n            dbDelete(c->db,touchedkey);\n            notifyKeyspaceEvent(NOTIFY_GENERIC,\"del\",\n                                touchedkey,c->db->id);\n        }\n        signalModifiedKey(c,c->db,touchedkey);\n        g_pserver->dirty++;\n        if (c->cmd->proc == blmoveCommand) {\n            rewriteClientCommandVector(c,5,shared.lmove,\n                                       c->argv[1],c->argv[2],c->argv[3],c->argv[4]);\n        } else if (c->cmd->proc == brpoplpushCommand) {\n            rewriteClientCommandVector(c,3,shared.rpoplpush,\n                                       c->argv[1],c->argv[2]);\n        }\n    }\n}\n\n/* LMOVE <source> <destination> (LEFT|RIGHT) (LEFT|RIGHT) */\nvoid lmoveCommand(client *c) {\n    int wherefrom, whereto;\n    if (getListPositionFromObjectOrReply(c,c->argv[3],&wherefrom)\n        != C_OK) return;\n    if (getListPositionFromObjectOrReply(c,c->argv[4],&whereto)\n        != C_OK) return;\n    lmoveGenericCommand(c, wherefrom, whereto);\n}\n\n/* This is the semantic of this command:\n *  RPOPLPUSH srclist dstlist:\n *    IF LLEN(srclist) > 0\n *      element = RPOP srclist\n *      LPUSH dstlist element\n *      RETURN element\n *    ELSE\n *      RETURN nil\n *    END\n *  END\n *\n * The idea is to be able to get an element from a list in a reliable way\n * since the element is not just returned but pushed against another list\n * as well. This command was originally proposed by Ezra Zygmuntowicz.\n */\nvoid rpoplpushCommand(client *c) {\n    lmoveGenericCommand(c, LIST_TAIL, LIST_HEAD);\n}\n\n/*-----------------------------------------------------------------------------\n * Blocking POP operations\n *----------------------------------------------------------------------------*/\n\n/* This is a helper function for handleClientsBlockedOnKeys(). Its work\n * is to serve a specific client (receiver) that is blocked on 'key'\n * in the context of the specified 'db', doing the following:\n *\n * 1) Provide the client with the 'value' element.\n * 2) If the dstkey is not NULL (we are serving a BLMOVE) also push the\n *    'value' element on the destination list (the \"push\" side of the command).\n * 3) Propagate the resulting BRPOP, BLPOP and additional xPUSH if any into\n *    the AOF and replication channel.\n *\n * The argument 'wherefrom' is LIST_TAIL or LIST_HEAD, and indicates if the\n * 'value' element was popped from the head (BLPOP) or tail (BRPOP) so that\n * we can propagate the command properly.\n *\n * The argument 'whereto' is LIST_TAIL or LIST_HEAD, and indicates if the\n * 'value' element is to be pushed to the head or tail so that we can\n * propagate the command properly.\n *\n * The function returns C_OK if we are able to serve the client, otherwise\n * C_ERR is returned to signal the caller that the list POP operation\n * should be undone as the client was not served: This only happens for\n * BLMOVE that fails to push the value to the destination key as it is\n * of the wrong type. */\nint serveClientBlockedOnList(client *receiver, robj *key, robj *dstkey, redisDb *db, robj *value, int wherefrom, int whereto)\n{\n    robj *argv[5];\n    std::unique_lock<fastlock> ul(receiver->lock);\n\n    if (dstkey == NULL) {\n        /* Propagate the [LR]POP operation. */\n        argv[0] = (wherefrom == LIST_HEAD) ? shared.lpop :\n                                             shared.rpop;\n        argv[1] = key;\n        propagate((wherefrom == LIST_HEAD) ?\n            cserver.lpopCommand : cserver.rpopCommand,\n            db->id,argv,2,PROPAGATE_AOF|PROPAGATE_REPL);\n       \n        /* BRPOP/BLPOP */\n        addReplyArrayLen(receiver,2);\n        addReplyBulk(receiver,key);\n        addReplyBulk(receiver,value);\n\n        /* Notify event. */\n        const char *event = (wherefrom == LIST_HEAD) ? \"lpop\" : \"rpop\";\n        notifyKeyspaceEvent(NOTIFY_LIST,event,key,receiver->db->id);\n    } else {\n        /* BLMOVE */\n        robj *dstobj =\n            lookupKeyWrite(receiver->db,dstkey);\n        if (!(dstobj &&\n             checkType(receiver,dstobj,OBJ_LIST)))\n        {\n            lmoveHandlePush(receiver,dstkey,dstobj,value,whereto);\n            /* Propagate the LMOVE/RPOPLPUSH operation. */\n            int isbrpoplpush = (receiver->lastcmd->proc == brpoplpushCommand);\n            argv[0] = isbrpoplpush ? shared.rpoplpush : shared.lmove;\n            argv[1] = key;\n            argv[2] = dstkey;\n            argv[3] = getStringObjectFromListPosition(wherefrom);\n            argv[4] = getStringObjectFromListPosition(whereto);\n            propagate(isbrpoplpush ? cserver.rpoplpushCommand : cserver.lmoveCommand,\n                db->id,argv,(isbrpoplpush ? 3 : 5),\n                PROPAGATE_AOF|\n                PROPAGATE_REPL);\n\n            /* Notify event (\"lpush\" or \"rpush\" was notified by lmoveHandlePush). */\n            notifyKeyspaceEvent(NOTIFY_LIST,wherefrom == LIST_TAIL ? \"rpop\" : \"lpop\",\n                                key,receiver->db->id);\n        } else {\n            /* BLMOVE failed because of wrong\n             * destination type. */\n            return C_ERR;\n        }\n    }\n    return C_OK;\n}\n\n/* Blocking RPOP/LPOP */\nvoid blockingPopGenericCommand(client *c, int where) {\n    robj *o;\n    mstime_t timeout;\n    int j;\n\n    if (getTimeoutFromObjectOrReply(c,c->argv[c->argc-1],&timeout,UNIT_SECONDS)\n        != C_OK) return;\n\n    for (j = 1; j < c->argc-1; j++) {\n        o = lookupKeyWrite(c->db,c->argv[j]);\n        if (o != NULL) {\n            if (checkType(c,o,OBJ_LIST)) {\n                return;\n            } else {\n                if (listTypeLength(o) != 0) {\n                    /* Non empty list, this is like a normal [LR]POP. */\n                    robj *value = listTypePop(o,where);\n                    serverAssert(value != NULL);\n\n                    addReplyArrayLen(c,2);\n                    addReplyBulk(c,c->argv[j]);\n                    addReplyBulk(c,value);\n                    decrRefCount(value);\n                    listElementsRemoved(c,c->argv[j],where,o,1);\n\n                    /* Replicate it as an [LR]POP instead of B[LR]POP. */\n                    rewriteClientCommandVector(c,2,\n                        (where == LIST_HEAD) ? shared.lpop : shared.rpop,\n                        c->argv[j]);\n                    return;\n                }\n            }\n        }\n    }\n\n    /* If we are not allowed to block the client, the only thing\n     * we can do is treating it as a timeout (even with timeout 0). */\n    if (c->flags & CLIENT_DENY_BLOCKING) {\n        addReplyNullArray(c);\n        return;\n    }\n\n    /* If the keys do not exist we must block */\n    listPos pos = {where};\n    blockForKeys(c,BLOCKED_LIST,c->argv + 1,c->argc - 2,timeout,NULL,&pos,NULL);\n}\n\n/* BLPOP <key> [<key> ...] <timeout> */\nvoid blpopCommand(client *c) {\n    blockingPopGenericCommand(c,LIST_HEAD);\n}\n\n/* BLPOP <key> [<key> ...] <timeout> */\nvoid brpopCommand(client *c) {\n    blockingPopGenericCommand(c,LIST_TAIL);\n}\n\nvoid blmoveGenericCommand(client *c, int wherefrom, int whereto, mstime_t timeout) {\n    robj *key = lookupKeyWrite(c->db, c->argv[1]);\n    if (checkType(c,key,OBJ_LIST)) return;\n\n    if (key == NULL) {\n        if (c->flags & CLIENT_DENY_BLOCKING) {\n            /* Blocking against an empty list when blocking is not allowed\n             * returns immediately. */\n            addReplyNull(c);\n        } else {\n            /* The list is empty and the client blocks. */\n            struct listPos pos = {wherefrom, whereto};\n            blockForKeys(c,BLOCKED_LIST,c->argv + 1,1,timeout,c->argv[2],&pos,NULL);\n        }\n    } else {\n        /* The list exists and has elements, so\n         * the regular lmoveCommand is executed. */\n        serverAssertWithInfo(c,key,listTypeLength(key) > 0);\n        lmoveGenericCommand(c,wherefrom,whereto);\n    }\n}\n\n/* BLMOVE <source> <destination> (LEFT|RIGHT) (LEFT|RIGHT) <timeout> */\nvoid blmoveCommand(client *c) {\n    mstime_t timeout;\n    int wherefrom, whereto;\n    if (getListPositionFromObjectOrReply(c,c->argv[3],&wherefrom)\n        != C_OK) return;\n    if (getListPositionFromObjectOrReply(c,c->argv[4],&whereto)\n        != C_OK) return;\n    if (getTimeoutFromObjectOrReply(c,c->argv[5],&timeout,UNIT_SECONDS)\n        != C_OK) return;\n    blmoveGenericCommand(c,wherefrom,whereto,timeout);\n}\n\n/* BRPOPLPUSH <source> <destination> <timeout> */\nvoid brpoplpushCommand(client *c) {\n    mstime_t timeout;\n    if (getTimeoutFromObjectOrReply(c,c->argv[3],&timeout,UNIT_SECONDS)\n        != C_OK) return;\n    blmoveGenericCommand(c, LIST_TAIL, LIST_HEAD, timeout);\n}\n"
  },
  {
    "path": "src/t_nhash.cpp",
    "content": "/*\n * Copyright (c) 2020, EQ Alpha Technology Ltd. <john at eqalpha dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n#include <math.h>\n\nvoid dictObjectDestructor(void *privdata, void *val);\ndictType nestedHashDictType {\n    dictSdsHash,               /* hash function */\n    NULL,                      /* key dup */\n    NULL,                      /* val dup */\n    dictSdsKeyCompare,         /* key compare */\n    dictSdsDestructor,         /* key destructor */\n    dictObjectDestructor,      /* val destructor */\n};\n\nrobj *createNestHashBucket() {\n    dict *d = dictCreate(&nestedHashDictType, nullptr);\n    return createObject(OBJ_NESTEDHASH, d);\n}\n\nvoid freeNestedHashObject(robj_roptr o) {\n    dictRelease((dict*)ptrFromObj(o));\n}\n\nclass DbDictWrapper {\npublic:\n    DbDictWrapper() = default;\n\n    DbDictWrapper(dict *d)\n        : m_dict(d)\n    {}\n\n    DbDictWrapper(redisDb *db)\n        : m_db(db)\n    {}\n\n    dict_iter find(const char *key) {\n        if (m_db != nullptr) {\n            return m_db->find(key);\n        } else if (m_dict != nullptr) {\n            dictEntry *de = dictFind(m_dict, key);\n            return dict_iter(m_dict, de);\n        }\n        return dict_iter(nullptr);\n    }\n\n    bool add(sds key, robj *val) {\n        bool result = false;\n        if (m_db != nullptr) {\n            result = m_db->insert(key, val, true);\n        } else if (m_dict != nullptr) {\n            result = dictAdd(m_dict, key, val) == DICT_OK;\n        }\n        return result;\n    }\n\nprivate:\n    redisDb *m_db = nullptr;\n    dict *m_dict = nullptr;\n};\n\nrobj *fetchFromKey(redisDb *db, robj_roptr key) {\n    const char *pchCur = szFromObj(key);\n    const char *pchStart = pchCur;\n    const char *pchMax = pchCur + sdslen(pchCur);\n    robj *o = nullptr;\n\n    while (pchCur <= pchMax) {\n        if (pchCur == pchMax || *pchCur == '.') {\n            // WARNING: Don't deref pchCur as it may be pchMax\n\n            // New word\n            if ((pchCur - pchStart) < 1) {\n                throw shared.syntaxerr; // malformed\n            }\n\n            DbDictWrapper srcDb;\n            if (o == nullptr)\n                srcDb = db;\n            else\n                srcDb = (dict*)ptrFromObj(o);\n            \n            sdsstring str(pchStart, pchCur - pchStart);\n            o = srcDb.find(str.get()).val();\n\n            if (o == nullptr) throw shared.nokeyerr;   // Not Found\n            serverAssert(o->type == OBJ_NESTEDHASH || o->type == OBJ_STRING || o->type == OBJ_LIST);\n            if (o->type == OBJ_STRING && pchCur != pchMax)\n                throw shared.nokeyerr; // Past the end\n\n            pchStart = pchCur + 1;\n        }\n        ++pchCur;\n    }\n\n    return o;\n}\n\n// Returns one if we overwrote a value\nbool setWithKey(redisDb *db, robj_roptr key, robj *val, bool fCreateBuckets) {\n    const char *pchCur = szFromObj(key);\n    const char *pchStart = pchCur;\n    const char *pchMax = pchCur + sdslen(pchCur);\n    robj *o = nullptr;\n\n    while (pchCur <= pchMax) {\n        if (pchCur == pchMax || *pchCur == '.') {\n            // WARNING: Don't deref pchCur as it may be pchMax\n\n            // New word\n            if ((pchCur - pchStart) < 1) {\n                throw shared.syntaxerr; // malformed\n            }\n\n            DbDictWrapper src;\n            if (o == nullptr)\n                src = db;\n            else\n                src = (dict*)ptrFromObj(o);\n            \n            sdsstring str(pchStart, pchCur - pchStart);\n            dict_iter di = src.find(str.get());\n\n            if (pchCur == pchMax) {\n                val->addref();\n                if (di.val() != nullptr) {\n                    decrRefCount(di.val());\n                    di.setval(val);\n                    return true;\n                } else {\n                    src.add(str.release(), val);\n                    return false;\n                }\n            } else {\n                o = di.val();\n\n                if (o == nullptr) {\n                    if (!fCreateBuckets)\n                        throw shared.nokeyerr;   // Not Found\n                    o = createNestHashBucket();\n                    serverAssert(src.add(str.release(), o));\n                } else if (o->type != OBJ_NESTEDHASH) {\n                    decrRefCount(o);\n                    o = createNestHashBucket();\n                    di.setval(o);\n                }\n            }\n\n            pchStart = pchCur + 1;\n        }\n        ++pchCur;\n    }\n    throw \"Internal Error\";\n}\n\nvoid writeNestedHashToClient(client *c, robj_roptr o) {\n    if (o == nullptr) {\n        addReply(c, shared.null[c->resp]);\n    } else if (o->type == OBJ_STRING) {\n        addReplyBulk(c, o);\n    } else if (o->type == OBJ_LIST) {\n        unsigned char *zl = (unsigned char*)ptrFromObj(o);\n        addReplyArrayLen(c, ziplistLen(zl));\n        unsigned char *p = ziplistIndex(zl, ZIPLIST_HEAD);\n        while (p != nullptr) {\n            unsigned char *str;\n            unsigned int len;\n            long long lval;\n            if (ziplistGet(p, &str, &len, &lval)) {\n                char rgT[128];\n                if (str == nullptr) {\n                    len = ll2string(rgT, 128, lval);\n                    str = (unsigned char*)rgT;\n                }\n                addReplyBulkCBuffer(c, (const char*)str, len);\n            }\n            p = ziplistNext(zl, p);\n        }\n    } else {\n        serverAssert(o->type == OBJ_NESTEDHASH );\n        dict *d = (dict*)ptrFromObj(o);\n\n        if (dictSize(d) > 1)\n            addReplyArrayLen(c, dictSize(d));\n        \n        dictIterator *di = dictGetIterator(d);\n        dictEntry *de;\n        while ((de = dictNext(di))) {\n            robj_roptr oT = (robj*)dictGetVal(de);\n            addReplyArrayLen(c, 2);\n            addReplyBulkCBuffer(c, (sds)dictGetKey(de), sdslen((sds)dictGetKey(de)));\n            if (oT->type == OBJ_STRING) {\n                addReplyBulk(c, oT);\n            } else {\n                writeNestedHashToClient(c, oT);\n            }\n        }\n        dictReleaseIterator(di);\n    }\n}\n\ninline bool FSimpleJsonEscapeCh(char ch) {\n    return (ch == '\"' || ch == '\\\\');\n}\ninline bool FExtendedJsonEscapeCh(char ch) {\n    return ch <= 0x1F;\n}\n\nsds writeJsonValue(sds output, const char *valIn, size_t cchIn) {\n    const char *val = valIn;\n    size_t cch = cchIn;\n    int cchEscapeExtra = 0;\n\n    // First scan for escaped chars\n    for (size_t ich = 0; ich < cchIn; ++ich) {\n        if (FSimpleJsonEscapeCh(valIn[ich])) {\n            ++cchEscapeExtra;\n        } else if (FExtendedJsonEscapeCh(valIn[ich])) {\n            cchEscapeExtra += 5;\n        }\n    }\n\n    if (cchEscapeExtra > 0) {\n        size_t ichDst = 0;\n        sds dst = sdsnewlen(SDS_NOINIT, cchIn+cchEscapeExtra);\n        for (size_t ich = 0; ich < cchIn; ++ich) {\n            switch (valIn[ich]) {\n                case '\"':\n                    dst[ichDst++] = '\\\\'; dst[ichDst++] = '\"';\n                    break;\n                case '\\\\':\n                    dst[ichDst++] = '\\\\'; dst[ichDst++] = '\\\\';\n                    break;\n                \n                default:\n                    serverAssert(!FSimpleJsonEscapeCh(valIn[ich]));\n                    if (FExtendedJsonEscapeCh(valIn[ich])) {\n                        dst[ichDst++] = '\\\\'; dst[ichDst++] = 'u';\n                        snprintf(dst + ichDst, cchIn+cchEscapeExtra-ichDst, \"%4x\", valIn[ich]);\n                        ichDst += 4;\n                    } else {\n                        dst[ichDst++] = valIn[ich];\n                    }\n                    break;\n            }\n        }\n        val = (const char*)dst;\n        serverAssert(ichDst == (cchIn+cchEscapeExtra));\n        cch = ichDst;\n    }\n\n    output = sdscat(output, \"\\\"\");\n    output = sdscatlen(output, val, cch);\n    output = sdscat(output, \"\\\"\");\n\n    if (val != valIn)\n        sdsfree(val);\n\n    return output;\n}\nsds writeJsonValue(sds output, sds val) {\n    return writeJsonValue(output, (const char*)val, sdslen(val));\n}\n\nsds writeNestedHashAsJson(sds output, robj_roptr o) {\n    if (o->type == OBJ_STRING) {\n        output = writeJsonValue(output, (sds)szFromObj(o));\n    } else if (o->type == OBJ_LIST) {\n        unsigned char *zl = (unsigned char*)ptrFromObj(o);\n        output = sdscat(output, \"[\");\n        unsigned char *p = ziplistIndex(zl, ZIPLIST_HEAD);\n        bool fFirst = true;\n        while (p != nullptr) {\n            unsigned char *str;\n            unsigned int len;\n            long long lval;\n            if (ziplistGet(p, &str, &len, &lval)) {\n                char rgT[128];\n                if (str == nullptr) {\n                    len = ll2string(rgT, 128, lval);\n                    str = (unsigned char*)rgT;\n                }\n                if (!fFirst)\n                    output = sdscat(output, \",\");\n                fFirst = false;\n                output = writeJsonValue(output, (const char*)str, len);\n            }\n            p = ziplistNext(zl, p);\n        }\n        output = sdscat(output, \"]\");\n    } else {\n        output = sdscat(output, \"{\");\n        dictIterator *di = dictGetIterator((dict*)ptrFromObj(o));\n        dictEntry *de;\n        bool fFirst = true;\n        while ((de = dictNext(di))) {\n            robj_roptr oT = (robj*)dictGetVal(de);\n            if (!fFirst)\n                output = sdscat(output, \",\");\n            fFirst = false;\n            output = writeJsonValue(output, (sds)dictGetKey(de));\n            output = sdscat(output, \" : \");\n            output = writeNestedHashAsJson(output, oT);\n        }\n        dictReleaseIterator(di);\n        output = sdscat(output, \"}\");\n    }\n    return output;\n}\n\nvoid nhsetCommand(client *c) {\n    if (c->argc < 3)\n        throw shared.syntaxerr;\n    \n    robj *val = c->argv[2];\n    if (c->argc > 3) {\n        // Its a list, we'll store as a ziplist\n        val = createZiplistObject();\n        for (int iarg = 2; iarg < c->argc; ++iarg) {\n            sds arg = (sds)szFromObj(c->argv[iarg]);\n            val->m_ptr = ziplistPush((unsigned char*)ptrFromObj(val), (unsigned char*)arg, sdslen(arg), ZIPLIST_TAIL);\n        }\n    }\n\n    try {\n        if (setWithKey(c->db, c->argv[1], val, true)) {\n            addReplyLongLong(c, 1); // we replaced a value\n        } else {\n            addReplyLongLong(c, 0); // we added a new value\n        }\n    } catch (...) {\n        if (val != c->argv[2])\n            decrRefCount(val);\n        throw;\n    }\n    if (val != c->argv[2])\n        decrRefCount(val);\n}\n\nvoid nhgetCommand(client *c) {\n    if (c->argc != 2 && c->argc != 3)\n        throw shared.syntaxerr;\n\n    bool fJson = false;\n    int argOffset = 0;\n    if (c->argc == 3) {\n        argOffset++;\n        if (strcasecmp(szFromObj(c->argv[1]), \"json\") == 0) {\n            fJson = true;\n        } else if (strcasecmp(szFromObj(c->argv[1]), \"resp\") != 0)  {\n            throw shared.syntaxerr;\n        }\n    }\n\n    robj *o = fetchFromKey(c->db, c->argv[argOffset + 1]);\n    if (fJson) {\n        sds val = writeNestedHashAsJson(sdsnew(nullptr), o);\n        addReplyBulkSds(c, val);\n    } else { \n        writeNestedHashToClient(c, o);\n    }\n}"
  },
  {
    "path": "src/t_nhash.h",
    "content": "#pragma once\n\nvoid freeNestedHashObject(robj_roptr o);\n\nvoid nhsetCommand(client *c);\nvoid nhgetCommand(client *c);\n"
  },
  {
    "path": "src/t_set.cpp",
    "content": "/*\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n\n/*-----------------------------------------------------------------------------\n * Set Commands\n *----------------------------------------------------------------------------*/\n\nvoid sunionDiffGenericCommand(client *c, robj **setkeys, int setnum,\n                              robj *dstkey, int op);\n\n/* Factory method to return a set that *can* hold \"value\". When the object has\n * an integer-encodable value, an intset will be returned. Otherwise a regular\n * hash table. */\nrobj *setTypeCreate(const char *value) {\n    if (isSdsRepresentableAsLongLong(value,NULL) == C_OK)\n        return createIntsetObject();\n    return createSetObject();\n}\n\n/* Add the specified value into a set.\n *\n * If the value was already member of the set, nothing is done and 0 is\n * returned, otherwise the new element is added and 1 is returned. */\nint setTypeAdd(robj *subject, const char *value) {\n    long long llval;\n    if (subject->encoding == OBJ_ENCODING_HT) {\n        dict *ht = (dict*)subject->m_ptr;\n        dictEntry *de = dictAddRaw(ht,(char*)value,NULL);\n        if (de) {\n            dictSetKey(ht,de,sdsdup(value));\n            dictSetVal(ht,de,NULL);\n            return 1;\n        }\n    } else if (subject->encoding == OBJ_ENCODING_INTSET) {\n        if (isSdsRepresentableAsLongLong(value,&llval) == C_OK) {\n            uint8_t success = 0;\n            subject->m_ptr = intsetAdd((intset*)subject->m_ptr,llval,&success);\n            if (success) {\n                /* Convert to regular set when the intset contains\n                 * too many entries. */\n                size_t max_entries = g_pserver->set_max_intset_entries;\n                /* limit to 1G entries due to intset internals. */\n                if (max_entries >= 1<<30) max_entries = 1<<30;\n                if (intsetLen((intset*)subject->m_ptr) > max_entries)\n                    setTypeConvert(subject,OBJ_ENCODING_HT);\n                return 1;\n            }\n        } else {\n            /* Failed to get integer from object, convert to regular set. */\n            setTypeConvert(subject,OBJ_ENCODING_HT);\n\n            /* The set *was* an intset and this value is not integer\n             * encodable, so dictAdd should always work. */\n            serverAssert(dictAdd((dict*)subject->m_ptr,sdsdup(value),NULL) == DICT_OK);\n            return 1;\n        }\n    } else {\n        serverPanic(\"Unknown set encoding\");\n    }\n    return 0;\n}\n\nint setTypeRemove(robj *setobj, const char *value) {\n    long long llval;\n    if (setobj->encoding == OBJ_ENCODING_HT) {\n        if (dictDelete((dict*)setobj->m_ptr,value) == DICT_OK) {\n            if (htNeedsResize((dict*)setobj->m_ptr)) dictResize((dict*)setobj->m_ptr);\n            return 1;\n        }\n    } else if (setobj->encoding == OBJ_ENCODING_INTSET) {\n        if (isSdsRepresentableAsLongLong(value,&llval) == C_OK) {\n            int success;\n            setobj->m_ptr = intsetRemove((intset*)setobj->m_ptr,llval,&success);\n            if (success) return 1;\n        }\n    } else {\n        serverPanic(\"Unknown set encoding\");\n    }\n    return 0;\n}\n\nint setTypeIsMember(robj_roptr subject, const char *value) {\n    long long llval;\n    if (subject->encoding == OBJ_ENCODING_HT) {\n        return dictFind((dict*)subject->m_ptr,value) != NULL;\n    } else if (subject->encoding == OBJ_ENCODING_INTSET) {\n        if (isSdsRepresentableAsLongLong(value,&llval) == C_OK) {\n            return intsetFind((intset*)subject->m_ptr,llval);\n        }\n    } else {\n        serverPanic(\"Unknown set encoding\");\n    }\n    return 0;\n}\n\nsetTypeIterator *setTypeInitIterator(robj_roptr subject) {\n    setTypeIterator *si = (setTypeIterator*)zmalloc(sizeof(setTypeIterator), MALLOC_LOCAL);\n    si->subject = subject;\n    si->encoding = subject->encoding;\n    if (si->encoding == OBJ_ENCODING_HT) {\n        si->di = dictGetIterator((dict*)subject->m_ptr);\n    } else if (si->encoding == OBJ_ENCODING_INTSET) {\n        si->ii = 0;\n    } else {\n        serverPanic(\"Unknown set encoding\");\n    }\n    return si;\n}\n\nvoid setTypeReleaseIterator(setTypeIterator *si) {\n    if (si->encoding == OBJ_ENCODING_HT)\n        dictReleaseIterator(si->di);\n    zfree(si);\n}\n\n/* Move to the next entry in the set. Returns the object at the current\n * position.\n *\n * Since set elements can be internally be stored as SDS strings or\n * simple arrays of integers, setTypeNext returns the encoding of the\n * set object you are iterating, and will populate the appropriate pointer\n * (sdsele) or (llele) accordingly.\n *\n * Note that both the sdsele and llele pointers should be passed and cannot\n * be NULL since the function will try to defensively populate the non\n * used field with values which are easy to trap if misused.\n *\n * When there are no longer elements -1 is returned. */\nint setTypeNext(setTypeIterator *si, const char **sdsele, int64_t *llele) {\n    if (si->encoding == OBJ_ENCODING_HT) {\n        dictEntry *de = dictNext(si->di);\n        if (de == NULL) return -1;\n        *sdsele = (sds)dictGetKey(de);\n        *llele = -123456789; /* Not needed. Defensive. */\n    } else if (si->encoding == OBJ_ENCODING_INTSET) {\n        if (!intsetGet((intset*)si->subject->m_ptr,si->ii++,llele))\n            return -1;\n        *sdsele = NULL; /* Not needed. Defensive. */\n    } else {\n        serverPanic(\"Wrong set encoding in setTypeNext\");\n    }\n    return si->encoding;\n}\n\n/* The not copy on write friendly version but easy to use version\n * of setTypeNext() is setTypeNextObject(), returning new SDS\n * strings. So if you don't retain a pointer to this object you should call\n * sdsfree() against it.\n *\n * This function is the way to go for write operations where COW is not\n * an issue. */\nsds setTypeNextObject(setTypeIterator *si) {\n    int64_t intele;\n    const char *sdsele;\n    int encoding;\n\n    encoding = setTypeNext(si,&sdsele,&intele);\n    switch(encoding) {\n        case -1:    return NULL;\n        case OBJ_ENCODING_INTSET:\n            return sdsfromlonglong(intele);\n        case OBJ_ENCODING_HT:\n            return sdsdup(sdsele);\n        default:\n            serverPanic(\"Unsupported encoding\");\n    }\n    return NULL; /* just to suppress warnings */\n}\n\n/* Return random element from a non empty set.\n * The returned element can be an int64_t value if the set is encoded\n * as an \"intset\" blob of integers, or an SDS string if the set\n * is a regular set.\n *\n * The caller provides both pointers to be populated with the right\n * object. The return value of the function is the object->encoding\n * field of the object and is used by the caller to check if the\n * int64_t pointer or the redis object pointer was populated.\n *\n * Note that both the sdsele and llele pointers should be passed and cannot\n * be NULL since the function will try to defensively populate the non\n * used field with values which are easy to trap if misused. */\nint setTypeRandomElement(robj *setobj, sds *sdsele, int64_t *llele) {\n    if (setobj->encoding == OBJ_ENCODING_HT) {\n        dictEntry *de = dictGetFairRandomKey((dict*)setobj->m_ptr);\n        *sdsele = (sds)dictGetKey(de);\n        *llele = -123456789; /* Not needed. Defensive. */\n    } else if (setobj->encoding == OBJ_ENCODING_INTSET) {\n        *llele = intsetRandom((intset*)setobj->m_ptr);\n        *sdsele = NULL; /* Not needed. Defensive. */\n    } else {\n        serverPanic(\"Unknown set encoding\");\n    }\n    return setobj->encoding;\n}\n\nint setTypeRandomElement(robj_roptr setobj, const char **sdsele, int64_t *llele)\n{\n    return setTypeRandomElement(setobj.unsafe_robjcast(), (sds*)sdsele, llele);\n}\n\nunsigned long setTypeSize(robj_roptr subject) {\n    if (subject->encoding == OBJ_ENCODING_HT) {\n        return dictSize((const dict*)subject->m_ptr);\n    } else if (subject->encoding == OBJ_ENCODING_INTSET) {\n        return intsetLen((const intset*)subject->m_ptr);\n    } else {\n        serverPanic(\"Unknown set encoding\");\n    }\n}\n\n/* Convert the set to specified encoding. The resulting dict (when converting\n * to a hash table) is presized to hold the number of elements in the original\n * set. */\nvoid setTypeConvert(robj *setobj, int enc) {\n    setTypeIterator *si;\n    serverAssertWithInfo(NULL,setobj,setobj->type == OBJ_SET &&\n                             setobj->encoding == OBJ_ENCODING_INTSET);\n\n    if (enc == OBJ_ENCODING_HT) {\n        int64_t intele;\n        dict *d = dictCreate(&setDictType,NULL);\n        const char *element;\n\n        /* Presize the dict to avoid rehashing */\n        dictExpand(d,intsetLen((intset*)setobj->m_ptr));\n\n        /* To add the elements we extract integers and create redis objects */\n        si = setTypeInitIterator(setobj);\n        while (setTypeNext(si,&element,&intele) != -1) {\n            sds elementNew = sdsfromlonglong(intele);\n            serverAssert(dictAdd(d,elementNew,NULL) == DICT_OK);\n        }\n        setTypeReleaseIterator(si);\n\n        setobj->encoding = OBJ_ENCODING_HT;\n        zfree(setobj->m_ptr);\n        setobj->m_ptr = d;\n    } else {\n        serverPanic(\"Unsupported set conversion\");\n    }\n}\n\n/* This is a helper function for the COPY command.\n * Duplicate a set object, with the guarantee that the returned object\n * has the same encoding as the original one.\n *\n * The resulting object always has refcount set to 1 */\nrobj *setTypeDup(robj *o) {\n    robj *set;\n    setTypeIterator *si;\n    const char *elesds;\n    int64_t intobj;\n\n    serverAssert(o->type == OBJ_SET);\n\n    /* Create a new set object that have the same encoding as the original object's encoding */\n    if (o->encoding == OBJ_ENCODING_INTSET) {\n        intset *is = (intset*)ptrFromObj(o);\n        size_t size = intsetBlobLen(is);\n        intset *newis = (intset*)zmalloc(size);\n        memcpy(newis,is,size);\n        set = createObject(OBJ_SET, newis);\n        set->encoding = OBJ_ENCODING_INTSET;\n    } else if (o->encoding == OBJ_ENCODING_HT) {\n        set = createSetObject();\n        dict *d = (dict*)ptrFromObj(o);\n        dictExpand((dict*)ptrFromObj(set), dictSize(d));\n        si = setTypeInitIterator(o);\n        while (setTypeNext(si, &elesds, &intobj) != -1) {\n            setTypeAdd(set, elesds);\n        }\n        setTypeReleaseIterator(si);\n    } else {\n        serverPanic(\"Unknown set encoding\");\n    }\n    return set;\n}\n\nvoid saddCommand(client *c) {\n    robj *set;\n    int j, added = 0;\n\n    set = lookupKeyWrite(c->db,c->argv[1]);\n    if (checkType(c,set,OBJ_SET)) return;\n    \n    if (set == NULL) {\n        set = setTypeCreate(szFromObj(c->argv[2]));\n        dbAdd(c->db,c->argv[1],set);\n    }\n\n    for (j = 2; j < c->argc; j++) {\n        if (setTypeAdd(set,szFromObj(c->argv[j]))) added++;\n    }\n    if (added) {\n        signalModifiedKey(c,c->db,c->argv[1]);\n        notifyKeyspaceEvent(NOTIFY_SET,\"sadd\",c->argv[1],c->db->id);\n    }\n    g_pserver->dirty += added;\n    addReplyLongLong(c,added);\n}\n\nvoid sremCommand(client *c) {\n    robj *set;\n    int j, deleted = 0, keyremoved = 0;\n\n    if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||\n        checkType(c,set,OBJ_SET)) return;\n\n    for (j = 2; j < c->argc; j++) {\n        if (setTypeRemove(set,szFromObj(c->argv[j]))) {\n            deleted++;\n            if (setTypeSize(set) == 0) {\n                dbDelete(c->db,c->argv[1]);\n                keyremoved = 1;\n                break;\n            }\n        }\n    }\n    if (deleted) {\n        signalModifiedKey(c,c->db,c->argv[1]);\n        notifyKeyspaceEvent(NOTIFY_SET,\"srem\",c->argv[1],c->db->id);\n        if (keyremoved)\n            notifyKeyspaceEvent(NOTIFY_GENERIC,\"del\",c->argv[1],\n                                c->db->id);\n        g_pserver->dirty += deleted;\n    }\n    addReplyLongLong(c,deleted);\n}\n\nvoid smoveCommand(client *c) {\n    robj *srcset, *dstset, *ele;\n    srcset = lookupKeyWrite(c->db,c->argv[1]);\n    dstset = lookupKeyWrite(c->db,c->argv[2]);\n    ele = c->argv[3];\n\n    /* If the source key does not exist return 0 */\n    if (srcset == NULL) {\n        addReply(c,shared.czero);\n        return;\n    }\n\n    /* If the source key has the wrong type, or the destination key\n     * is set and has the wrong type, return with an error. */\n    if (checkType(c,srcset,OBJ_SET) ||\n        checkType(c,dstset,OBJ_SET)) return;\n\n    /* If srcset and dstset are equal, SMOVE is a no-op */\n    if (srcset == dstset) {\n        addReply(c,setTypeIsMember(srcset,szFromObj(ele)) ?\n            shared.cone : shared.czero);\n        return;\n    }\n\n    /* If the element cannot be removed from the src set, return 0. */\n    if (!setTypeRemove(srcset,szFromObj(ele))) {\n        addReply(c,shared.czero);\n        return;\n    }\n    notifyKeyspaceEvent(NOTIFY_SET,\"srem\",c->argv[1],c->db->id);\n\n    /* Remove the src set from the database when empty */\n    if (setTypeSize(srcset) == 0) {\n        dbDelete(c->db,c->argv[1]);\n        notifyKeyspaceEvent(NOTIFY_GENERIC,\"del\",c->argv[1],c->db->id);\n    }\n\n    /* Create the destination set when it doesn't exist */\n    if (!dstset) {\n        dstset = setTypeCreate(szFromObj(ele));\n        dbAdd(c->db,c->argv[2],dstset);\n    }\n\n    signalModifiedKey(c,c->db,c->argv[1]);\n    g_pserver->dirty++;\n\n    /* An extra key has changed when ele was successfully added to dstset */\n    if (setTypeAdd(dstset,szFromObj(ele))) {\n        g_pserver->dirty++;\n        signalModifiedKey(c,c->db,c->argv[2]);\n        notifyKeyspaceEvent(NOTIFY_SET,\"sadd\",c->argv[2],c->db->id);\n    }\n    addReply(c,shared.cone);\n}\n\nvoid sismemberCommand(client *c) {\n    robj_roptr set;\n\n    if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == nullptr ||\n        checkType(c,set,OBJ_SET)) return;\n\n    if (setTypeIsMember(set,szFromObj(c->argv[2])))\n        addReply(c,shared.cone);\n    else\n        addReply(c,shared.czero);\n}\n\nvoid smismemberCommand(client *c) {\n    robj_roptr set;\n    int j;\n\n    /* Don't abort when the key cannot be found. Non-existing keys are empty\n     * sets, where SMISMEMBER should respond with a series of zeros. */\n    set = lookupKeyRead(c->db,c->argv[1]);\n    if (set && checkType(c,set,OBJ_SET)) return;\n\n    addReplyArrayLen(c,c->argc - 2);\n\n    for (j = 2; j < c->argc; j++) {\n        if (set && setTypeIsMember(set,szFromObj(c->argv[j])))\n            addReply(c,shared.cone);\n        else\n            addReply(c,shared.czero);\n    }\n}\n\nvoid scardCommand(client *c) {\n    robj_roptr o;\n\n    if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == nullptr ||\n        checkType(c,o,OBJ_SET)) return;\n\n    addReplyLongLong(c,setTypeSize(o));\n}\n\n/* Handle the \"SPOP key <count>\" variant. The normal version of the\n * command is handled by the spopCommand() function itself. */\n\n/* How many times bigger should be the set compared to the remaining size\n * for us to use the \"create new set\" strategy? Read later in the\n * implementation for more info. */\n#define SPOP_MOVE_STRATEGY_MUL 5\n\nvoid spopWithCountCommand(client *c) {\n    long l;\n    unsigned long count, size;\n    robj *set;\n\n    /* Get the count argument */\n    if (getPositiveLongFromObjectOrReply(c,c->argv[2],&l,NULL) != C_OK) return;\n    count = (unsigned long) l;\n\n    /* Make sure a key with the name inputted exists, and that it's type is\n     * indeed a set. Otherwise, return nil */\n    if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.emptyset[c->resp]))\n        == NULL || checkType(c,set,OBJ_SET)) return;\n\n    /* If count is zero, serve an empty set ASAP to avoid special\n     * cases later. */\n    if (count == 0) {\n        addReply(c,shared.emptyset[c->resp]);\n        return;\n    }\n\n    size = setTypeSize(set);\n\n    /* Generate an SPOP keyspace notification */\n    notifyKeyspaceEvent(NOTIFY_SET,\"spop\",c->argv[1],c->db->id);\n    g_pserver->dirty += (count >= size) ? size : count;\n\n    /* CASE 1:\n     * The number of requested elements is greater than or equal to\n     * the number of elements inside the set: simply return the whole set. */\n    if (count >= size) {\n        /* We just return the entire set */\n        sunionDiffGenericCommand(c,c->argv+1,1,NULL,SET_OP_UNION);\n\n        /* Delete the set as it is now empty */\n        dbDelete(c->db,c->argv[1]);\n        notifyKeyspaceEvent(NOTIFY_GENERIC,\"del\",c->argv[1],c->db->id);\n\n        /* Propagate this command as a DEL operation */\n        rewriteClientCommandVector(c,2,shared.del,c->argv[1]);\n        signalModifiedKey(c,c->db,c->argv[1]);\n        return;\n    }\n\n    /* Case 2 and 3 require to replicate SPOP as a set of SREM commands.\n     * Prepare our replication argument vector. Also send the array length\n     * which is common to both the code paths. */\n    robj *propargv[3];\n    propargv[0] = shared.srem;\n    propargv[1] = c->argv[1];\n    addReplySetLen(c,count);\n\n    /* Common iteration vars. */\n    const char *sdsele;\n    robj *objele;\n    int encoding;\n    int64_t llele = 0;\n    unsigned long remaining = size-count; /* Elements left after SPOP. */\n\n    /* If we are here, the number of requested elements is less than the\n     * number of elements inside the set. Also we are sure that count < size.\n     * Use two different strategies.\n     *\n     * CASE 2: The number of elements to return is small compared to the\n     * set size. We can just extract random elements and return them to\n     * the set. */\n    if (remaining*SPOP_MOVE_STRATEGY_MUL > count) {\n        while(count--) {\n            /* Emit and remove. */\n            encoding = setTypeRandomElement(set,&sdsele,&llele);\n            if (encoding == OBJ_ENCODING_INTSET) {\n                addReplyBulkLongLong(c,llele);\n                objele = createStringObjectFromLongLong(llele);\n                set->m_ptr = intsetRemove((intset*)set->m_ptr,llele,NULL);\n            } else {\n                addReplyBulkCBuffer(c,sdsele,sdslen(sdsele));\n                objele = createStringObject(sdsele,sdslen(sdsele));\n                setTypeRemove(set,sdsele);\n            }\n\n            /* Replicate/AOF this command as an SREM operation */\n            propargv[2] = objele;\n            alsoPropagate(cserver.sremCommand,c->db->id,propargv,3,\n                PROPAGATE_AOF|PROPAGATE_REPL);\n            decrRefCount(objele);\n        }\n    } else {\n    /* CASE 3: The number of elements to return is very big, approaching\n     * the size of the set itself. After some time extracting random elements\n     * from such a set becomes computationally expensive, so we use\n     * a different strategy, we extract random elements that we don't\n     * want to return (the elements that will remain part of the set),\n     * creating a new set as we do this (that will be stored as the original\n     * set). Then we return the elements left in the original set and\n     * release it. */\n        robj *newset = NULL;\n\n        /* Create a new set with just the remaining elements. */\n        while(remaining--) {\n            encoding = setTypeRandomElement(set,&sdsele,&llele);\n            if (encoding == OBJ_ENCODING_INTSET) {\n                sdsele = sdsfromlonglong(llele);\n            } else {\n                sdsele = sdsdup(sdsele);\n            }\n            if (!newset) newset = setTypeCreate(sdsele);\n            setTypeAdd(newset,sdsele);\n            setTypeRemove(set,sdsele);\n            sdsfree(sdsele);\n        }\n\n        /* Transfer the old set to the client. */\n        setTypeIterator *si;\n        si = setTypeInitIterator(set);\n        while((encoding = setTypeNext(si,&sdsele,&llele)) != -1) {\n            if (encoding == OBJ_ENCODING_INTSET) {\n                addReplyBulkLongLong(c,llele);\n                objele = createStringObjectFromLongLong(llele);\n            } else {\n                addReplyBulkCBuffer(c,sdsele,sdslen(sdsele));\n                objele = createStringObject(sdsele,sdslen(sdsele));\n            }\n\n            /* Replicate/AOF this command as an SREM operation */\n            propargv[2] = objele;\n            alsoPropagate(cserver.sremCommand,c->db->id,propargv,3,\n                PROPAGATE_AOF|PROPAGATE_REPL);\n            decrRefCount(objele);\n        }\n        setTypeReleaseIterator(si);\n\n        /* Assign the new set as the key value. */\n        dbOverwrite(c->db,c->argv[1],newset);\n    }\n\n    /* Don't propagate the command itself even if we incremented the\n     * dirty counter. We don't want to propagate an SPOP command since\n     * we propagated the command as a set of SREMs operations using\n     * the alsoPropagate() API. */\n    preventCommandPropagation(c);\n    signalModifiedKey(c,c->db,c->argv[1]);\n}\n\nvoid spopCommand(client *c) {\n    robj *set, *ele;\n    sds sdsele;\n    int64_t llele;\n    int encoding;\n\n    if (c->argc == 3) {\n        spopWithCountCommand(c);\n        return;\n    } else if (c->argc > 3) {\n        addReplyErrorObject(c,shared.syntaxerr);\n        return;\n    }\n\n    /* Make sure a key with the name inputted exists, and that it's type is\n     * indeed a set */\n    if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.null[c->resp]))\n         == NULL || checkType(c,set,OBJ_SET)) return;\n\n    /* Get a random element from the set */\n    encoding = setTypeRandomElement(set,&sdsele,&llele);\n\n    /* Remove the element from the set */\n    if (encoding == OBJ_ENCODING_INTSET) {\n        ele = createStringObjectFromLongLong(llele);\n        set->m_ptr = intsetRemove((intset*)set->m_ptr,llele,NULL);\n    } else {\n        ele = createStringObject(sdsele,sdslen(sdsele));\n        setTypeRemove(set,szFromObj(ele));\n    }\n\n    notifyKeyspaceEvent(NOTIFY_SET,\"spop\",c->argv[1],c->db->id);\n\n    /* Replicate/AOF this command as an SREM operation */\n    rewriteClientCommandVector(c,3,shared.srem,c->argv[1],ele);\n\n    /* Add the element to the reply */\n    addReplyBulk(c,ele);\n    decrRefCount(ele);\n\n    /* Delete the set if it's empty */\n    if (setTypeSize(set) == 0) {\n        dbDelete(c->db,c->argv[1]);\n        notifyKeyspaceEvent(NOTIFY_GENERIC,\"del\",c->argv[1],c->db->id);\n    }\n\n    /* Set has been modified */\n    signalModifiedKey(c,c->db,c->argv[1]);\n    g_pserver->dirty++;\n}\n\n/* handle the \"SRANDMEMBER key <count>\" variant. The normal version of the\n * command is handled by the srandmemberCommand() function itself. */\n\n/* How many times bigger should be the set compared to the requested size\n * for us to don't use the \"remove elements\" strategy? Read later in the\n * implementation for more info. */\n#define SRANDMEMBER_SUB_STRATEGY_MUL 3\n\nvoid srandmemberWithCountCommand(client *c) {\n    long l;\n    unsigned long count, size;\n    int uniq = 1;\n    robj_roptr set;\n    const char *ele;\n    int64_t llele = 0;\n    int encoding;\n\n    dict *d;\n\n    if (getRangeLongFromObjectOrReply(c,c->argv[2],-LONG_MAX,LONG_MAX,&l,NULL) != C_OK) return;\n    if (l < -g_pserver->rand_total_threshold || l > g_pserver->rand_total_threshold) {\n        addReplyError(c,\"value is out of range\");\n        return;\n    }\n    if (l >= 0) {\n        count = (unsigned long) l;\n    } else {\n        /* A negative count means: return the same elements multiple times\n         * (i.e. don't remove the extracted element after every extraction). */\n        count = -l;\n        uniq = 0;\n    }\n\n    if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.emptyarray))\n        == nullptr || checkType(c,set,OBJ_SET)) return;\n    size = setTypeSize(set);\n\n    /* If count is zero, serve it ASAP to avoid special cases later. */\n    if (count == 0) {\n        addReply(c,shared.emptyarray);\n        return;\n    }\n\n    /* CASE 1: The count was negative, so the extraction method is just:\n     * \"return N random elements\" sampling the whole set every time.\n     * This case is trivial and can be served without auxiliary data\n     * structures. This case is the only one that also needs to return the\n     * elements in random order. */\n    if (!uniq || count == 1) {\n        addReplyArrayLen(c,count);\n        while(count--) {\n            encoding = setTypeRandomElement(set,&ele,&llele);\n            if (encoding == OBJ_ENCODING_INTSET) {\n                addReplyBulkLongLong(c,llele);\n            } else {\n                addReplyBulkCBuffer(c,ele,sdslen(ele));\n            }\n            if (c->flags & CLIENT_CLOSE_ASAP)\n                break;\n        }\n        return;\n    }\n\n    /* CASE 2:\n     * The number of requested elements is greater than the number of\n     * elements inside the set: simply return the whole set. */\n    if (count >= size) {\n        setTypeIterator *si;\n        addReplyArrayLen(c,size);\n        si = setTypeInitIterator(set);\n        while ((encoding = setTypeNext(si,&ele,&llele)) != -1) {\n            if (encoding == OBJ_ENCODING_INTSET) {\n                addReplyBulkLongLong(c,llele);\n            } else {\n                addReplyBulkCBuffer(c,ele,sdslen(ele));\n            }\n            size--;\n        }\n        setTypeReleaseIterator(si);\n        serverAssert(size==0);\n        return;\n    }\n\n    /* For CASE 3 and CASE 4 we need an auxiliary dictionary. */\n    d = dictCreate(&sdsReplyDictType,NULL);\n\n    /* CASE 3:\n     * The number of elements inside the set is not greater than\n     * SRANDMEMBER_SUB_STRATEGY_MUL times the number of requested elements.\n     * In this case we create a set from scratch with all the elements, and\n     * subtract random elements to reach the requested number of elements.\n     *\n     * This is done because if the number of requested elements is just\n     * a bit less than the number of elements in the set, the natural approach\n     * used into CASE 4 is highly inefficient. */\n    if (count*SRANDMEMBER_SUB_STRATEGY_MUL > size) {\n        setTypeIterator *si;\n\n        /* Add all the elements into the temporary dictionary. */\n        si = setTypeInitIterator(set);\n        dictExpand(d, size);\n        while ((encoding = setTypeNext(si,&ele,&llele)) != -1) {\n            int retval = DICT_ERR;\n\n            if (encoding == OBJ_ENCODING_INTSET) {\n                retval = dictAdd(d,sdsfromlonglong(llele),NULL);\n            } else {\n                retval = dictAdd(d,sdsdup(ele),NULL);\n            }\n            serverAssert(retval == DICT_OK);\n        }\n        setTypeReleaseIterator(si);\n        serverAssert(dictSize(d) == size);\n\n        /* Remove random elements to reach the right count. */\n        while (size > count) {\n            dictEntry *de;\n            de = dictGetRandomKey(d);\n            dictUnlink(d,dictGetKey(de));\n            sdsfree((sds)dictGetKey(de));\n            dictFreeUnlinkedEntry(d,de);\n            size--;\n        }\n    }\n\n    /* CASE 4: We have a big set compared to the requested number of elements.\n     * In this case we can simply get random elements from the set and add\n     * to the temporary set, trying to eventually get enough unique elements\n     * to reach the specified count. */\n    else {\n        unsigned long added = 0;\n        sds sdsele;\n\n        dictExpand(d, count);\n        while (added < count) {\n            encoding = setTypeRandomElement(set,&ele,&llele);\n            if (encoding == OBJ_ENCODING_INTSET) {\n                sdsele = sdsfromlonglong(llele);\n            } else {\n                sdsele = sdsdup(ele);\n            }\n            /* Try to add the object to the dictionary. If it already exists\n             * free it, otherwise increment the number of objects we have\n             * in the result dictionary. */\n            if (dictAdd(d,sdsele,NULL) == DICT_OK)\n                added++;\n            else\n                sdsfree(sdsele);\n        }\n    }\n\n    /* CASE 3 & 4: send the result to the user. */\n    {\n        dictIterator *di;\n        dictEntry *de;\n\n        addReplyArrayLen(c,count);\n        di = dictGetIterator(d);\n        while((de = dictNext(di)) != NULL)\n            addReplyBulkSds(c,(sds)dictGetKey(de));\n        dictReleaseIterator(di);\n        dictRelease(d);\n    }\n}\n\n/* SRANDMEMBER [<count>] */\nvoid srandmemberCommand(client *c) {\n    robj_roptr set;\n    const char *ele;\n    int64_t llele = 0;\n    int encoding;\n\n    if (c->argc == 3) {\n        srandmemberWithCountCommand(c);\n        return;\n    } else if (c->argc > 3) {\n        addReplyErrorObject(c,shared.syntaxerr);\n        return;\n    }\n\n    /* Handle variant without <count> argument. Reply with simple bulk string */\n    if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.null[c->resp]))\n        == nullptr || checkType(c,set,OBJ_SET)) return;\n\n    encoding = setTypeRandomElement(set,&ele,&llele);\n    if (encoding == OBJ_ENCODING_INTSET) {\n        addReplyBulkLongLong(c,llele);\n    } else {\n        addReplyBulkCBuffer(c,ele,sdslen(ele));\n    }\n}\n\nint qsortCompareSetsByCardinality(const void *s1, const void *s2) {\n    if (setTypeSize(*(robj**)s1) > setTypeSize(*(robj**)s2)) return 1;\n    if (setTypeSize(*(robj**)s1) < setTypeSize(*(robj**)s2)) return -1;\n    return 0;\n}\n\n/* This is used by SDIFF and in this case we can receive NULL that should\n * be handled as empty sets. */\nint qsortCompareSetsByRevCardinality(const void *s1, const void *s2) {\n    robj *o1 = *(robj**)s1, *o2 = *(robj**)s2;\n    unsigned long first = o1 ? setTypeSize(o1) : 0;\n    unsigned long second = o2 ? setTypeSize(o2) : 0;\n\n    if (first < second) return 1;\n    if (first > second) return -1;\n    return 0;\n}\n\nvoid sinterGenericCommand(client *c, robj **setkeys,\n                          unsigned long setnum, robj *dstkey) {\n    robj **sets = (robj**)zmalloc(sizeof(robj*)*setnum, MALLOC_SHARED);\n    setTypeIterator *si;\n    robj *dstset = NULL;\n    const char *elesds;\n    int64_t intobj;\n    void *replylen = NULL;\n    unsigned long j, cardinality = 0;\n    int encoding, empty = 0;\n\n    for (j = 0; j < setnum; j++) {\n        robj *setobj = dstkey ?\n            lookupKeyWrite(c->db,setkeys[j]) :\n            lookupKeyRead(c->db,setkeys[j]).unsafe_robjcast();\n        if (!setobj) {\n            /* A NULL is considered an empty set */\n            empty += 1;\n            sets[j] = NULL;\n            continue;\n        }\n        if (checkType(c,setobj,OBJ_SET)) {\n            zfree(sets);\n            return;\n        }\n        sets[j] = setobj;\n    }\n\n    /* Set intersection with an empty set always results in an empty set.\n     * Return ASAP if there is an empty set. */\n    if (empty > 0) {\n        zfree(sets);\n        if (dstkey) {\n            if (dbDelete(c->db,dstkey)) {\n                signalModifiedKey(c,c->db,dstkey);\n                notifyKeyspaceEvent(NOTIFY_GENERIC,\"del\",dstkey,c->db->id);\n                g_pserver->dirty++;\n            }\n            addReply(c,shared.czero);\n        } else {\n            addReply(c,shared.emptyset[c->resp]);\n        }\n        return;\n    }\n\n    /* Sort sets from the smallest to largest, this will improve our\n     * algorithm's performance */\n    qsort(sets,setnum,sizeof(robj*),qsortCompareSetsByCardinality);\n\n    /* The first thing we should output is the total number of elements...\n     * since this is a multi-bulk write, but at this stage we don't know\n     * the intersection set size, so we use a trick, append an empty object\n     * to the output list and save the pointer to later modify it with the\n     * right length */\n    if (!dstkey) {\n        replylen = addReplyDeferredLen(c);\n    } else {\n        /* If we have a target key where to store the resulting set\n         * create this key with an empty set inside */\n        dstset = createIntsetObject();\n    }\n\n    /* Iterate all the elements of the first (smallest) set, and test\n     * the element against all the other sets, if at least one set does\n     * not include the element it is discarded */\n    si = setTypeInitIterator(sets[0]);\n    while((encoding = setTypeNext(si,&elesds,&intobj)) != -1) {\n        for (j = 1; j < setnum; j++) {\n            if (sets[j] == sets[0]) continue;\n            if (encoding == OBJ_ENCODING_INTSET) {\n                /* intset with intset is simple... and fast */\n                if (sets[j]->encoding == OBJ_ENCODING_INTSET &&\n                    !intsetFind((intset*)sets[j]->m_ptr,intobj))\n                {\n                    break;\n                /* in order to compare an integer with an object we\n                 * have to use the generic function, creating an object\n                 * for this */\n                } else if (sets[j]->encoding == OBJ_ENCODING_HT) {\n                    elesds = sdsfromlonglong(intobj);\n                    if (!setTypeIsMember(sets[j],elesds)) {\n                        sdsfree(elesds);\n                        break;\n                    }\n                    sdsfree(elesds);\n                }\n            } else if (encoding == OBJ_ENCODING_HT) {\n                if (!setTypeIsMember(sets[j],elesds)) {\n                    break;\n                }\n            }\n        }\n\n        /* Only take action when all sets contain the member */\n        if (j == setnum) {\n            if (!dstkey) {\n                if (encoding == OBJ_ENCODING_HT)\n                    addReplyBulkCBuffer(c,elesds,sdslen(elesds));\n                else\n                    addReplyBulkLongLong(c,intobj);\n                cardinality++;\n            } else {\n                if (encoding == OBJ_ENCODING_INTSET) {\n                    elesds = sdsfromlonglong(intobj);\n                    setTypeAdd(dstset,elesds);\n                    sdsfree(elesds);\n                } else {\n                    setTypeAdd(dstset,elesds);\n                }\n            }\n        }\n    }\n    setTypeReleaseIterator(si);\n\n    if (dstkey) {\n        /* Store the resulting set into the target, if the intersection\n         * is not an empty set. */\n        if (setTypeSize(dstset) > 0) {\n            setKey(c,c->db,dstkey,dstset);\n            addReplyLongLong(c,setTypeSize(dstset));\n            notifyKeyspaceEvent(NOTIFY_SET,\"sinterstore\",\n                dstkey,c->db->id);\n            g_pserver->dirty++;\n        } else {\n            addReply(c,shared.czero);\n            if (dbDelete(c->db,dstkey)) {\n                g_pserver->dirty++;\n                signalModifiedKey(c,c->db,dstkey);\n                notifyKeyspaceEvent(NOTIFY_GENERIC,\"del\",dstkey,c->db->id);\n            }\n        }\n        decrRefCount(dstset);\n    } else {\n        setDeferredSetLen(c,replylen,cardinality);\n    }\n    zfree(sets);\n}\n\n/* SINTER key [key ...] */\nvoid sinterCommand(client *c) {\n    sinterGenericCommand(c,c->argv+1,c->argc-1,NULL);\n}\n\n/* SINTERSTORE destination key [key ...] */\nvoid sinterstoreCommand(client *c) {\n    sinterGenericCommand(c,c->argv+2,c->argc-2,c->argv[1]);\n}\n\n#define SET_OP_UNION 0\n#define SET_OP_DIFF 1\n#define SET_OP_INTER 2\n\nvoid sunionDiffGenericCommand(client *c, robj **setkeys, int setnum,\n                              robj *dstkey, int op) {\n    robj **sets = (robj**)zmalloc(sizeof(robj*)*setnum, MALLOC_SHARED);\n    setTypeIterator *si;\n    robj *dstset = NULL;\n    sds ele;\n    int j, cardinality = 0;\n    int diff_algo = 1;\n\n    for (j = 0; j < setnum; j++) {\n        robj *setobj = dstkey ?\n            lookupKeyWrite(c->db,setkeys[j]) :\n            lookupKeyRead(c->db,setkeys[j]).unsafe_robjcast();\n        if (!setobj) {\n            sets[j] = NULL;\n            continue;\n        }\n        if (checkType(c,setobj,OBJ_SET)) {\n            zfree(sets);\n            return;\n        }\n        sets[j] = setobj;\n    }\n\n    /* Select what DIFF algorithm to use.\n     *\n     * Algorithm 1 is O(N*M) where N is the size of the element first set\n     * and M the total number of sets.\n     *\n     * Algorithm 2 is O(N) where N is the total number of elements in all\n     * the sets.\n     *\n     * We compute what is the best bet with the current input here. */\n    if (op == SET_OP_DIFF && sets[0]) {\n        long long algo_one_work = 0, algo_two_work = 0;\n\n        for (j = 0; j < setnum; j++) {\n            if (sets[j] == NULL) continue;\n\n            algo_one_work += setTypeSize(sets[0]);\n            algo_two_work += setTypeSize(sets[j]);\n        }\n\n        /* Algorithm 1 has better constant times and performs less operations\n         * if there are elements in common. Give it some advantage. */\n        algo_one_work /= 2;\n        diff_algo = (algo_one_work <= algo_two_work) ? 1 : 2;\n\n        if (diff_algo == 1 && setnum > 1) {\n            /* With algorithm 1 it is better to order the sets to subtract\n             * by decreasing size, so that we are more likely to find\n             * duplicated elements ASAP. */\n            qsort(sets+1,setnum-1,sizeof(robj*),\n                qsortCompareSetsByRevCardinality);\n        }\n    }\n\n    /* We need a temp set object to store our union. If the dstkey\n     * is not NULL (that is, we are inside an SUNIONSTORE operation) then\n     * this set object will be the resulting object to set into the target key*/\n    dstset = createIntsetObject();\n\n    if (op == SET_OP_UNION) {\n        /* Union is trivial, just add every element of every set to the\n         * temporary set. */\n        for (j = 0; j < setnum; j++) {\n            if (!sets[j]) continue; /* non existing keys are like empty sets */\n\n            si = setTypeInitIterator(sets[j]);\n            while((ele = setTypeNextObject(si)) != NULL) {\n                if (setTypeAdd(dstset,ele)) cardinality++;\n                sdsfree(ele);\n            }\n            setTypeReleaseIterator(si);\n        }\n    } else if (op == SET_OP_DIFF && sets[0] && diff_algo == 1) {\n        /* DIFF Algorithm 1:\n         *\n         * We perform the diff by iterating all the elements of the first set,\n         * and only adding it to the target set if the element does not exist\n         * into all the other sets.\n         *\n         * This way we perform at max N*M operations, where N is the size of\n         * the first set, and M the number of sets. */\n        si = setTypeInitIterator(sets[0]);\n        while((ele = setTypeNextObject(si)) != NULL) {\n            for (j = 1; j < setnum; j++) {\n                if (!sets[j]) continue; /* no key is an empty set. */\n                if (sets[j] == sets[0]) break; /* same set! */\n                if (setTypeIsMember(sets[j],ele)) break;\n            }\n            if (j == setnum) {\n                /* There is no other set with this element. Add it. */\n                setTypeAdd(dstset,ele);\n                cardinality++;\n            }\n            sdsfree(ele);\n        }\n        setTypeReleaseIterator(si);\n    } else if (op == SET_OP_DIFF && sets[0] && diff_algo == 2) {\n        /* DIFF Algorithm 2:\n         *\n         * Add all the elements of the first set to the auxiliary set.\n         * Then remove all the elements of all the next sets from it.\n         *\n         * This is O(N) where N is the sum of all the elements in every\n         * set. */\n        for (j = 0; j < setnum; j++) {\n            if (!sets[j]) continue; /* non existing keys are like empty sets */\n\n            si = setTypeInitIterator(sets[j]);\n            while((ele = setTypeNextObject(si)) != NULL) {\n                if (j == 0) {\n                    if (setTypeAdd(dstset,ele)) cardinality++;\n                } else {\n                    if (setTypeRemove(dstset,ele)) cardinality--;\n                }\n                sdsfree(ele);\n            }\n            setTypeReleaseIterator(si);\n\n            /* Exit if result set is empty as any additional removal\n             * of elements will have no effect. */\n            if (cardinality == 0) break;\n        }\n    }\n\n    /* Output the content of the resulting set, if not in STORE mode */\n    if (!dstkey) {\n        addReplySetLen(c,cardinality);\n        si = setTypeInitIterator(dstset);\n        while((ele = setTypeNextObject(si)) != NULL) {\n            addReplyBulkCBuffer(c,ele,sdslen(ele));\n            sdsfree(ele);\n        }\n        setTypeReleaseIterator(si);\n        g_pserver->lazyfree_lazy_server_del ? freeObjAsync(NULL, dstset) :\n                                          decrRefCount(dstset);\n    } else {\n        /* If we have a target key where to store the resulting set\n         * create this key with the result set inside */\n        if (setTypeSize(dstset) > 0) {\n            setKey(c,c->db,dstkey,dstset);\n            addReplyLongLong(c,setTypeSize(dstset));\n            notifyKeyspaceEvent(NOTIFY_SET,\n                op == SET_OP_UNION ? \"sunionstore\" : \"sdiffstore\",\n                dstkey,c->db->id);\n            g_pserver->dirty++;\n        } else {\n            addReply(c,shared.czero);\n            if (dbDelete(c->db,dstkey)) {\n                g_pserver->dirty++;\n                signalModifiedKey(c,c->db,dstkey);\n                notifyKeyspaceEvent(NOTIFY_GENERIC,\"del\",dstkey,c->db->id);\n            }\n        }\n        decrRefCount(dstset);\n    }\n    zfree(sets);\n}\n\n/* SUNION key [key ...] */\nvoid sunionCommand(client *c) {\n    sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,SET_OP_UNION);\n}\n\n/* SUNIONSTORE destination key [key ...] */\nvoid sunionstoreCommand(client *c) {\n    sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],SET_OP_UNION);\n}\n\n/* SDIFF key [key ...] */\nvoid sdiffCommand(client *c) {\n    sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,SET_OP_DIFF);\n}\n\n/* SDIFFSTORE destination key [key ...] */\nvoid sdiffstoreCommand(client *c) {\n    sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],SET_OP_DIFF);\n}\n\nvoid sscanCommand(client *c) {\n    robj_roptr set;\n    unsigned long cursor;\n\n    if (parseScanCursorOrReply(c,c->argv[2],&cursor) == C_ERR) return;\n    if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.emptyscan)) == nullptr ||\n        checkType(c,set,OBJ_SET)) return;\n    scanGenericCommand(c,set,cursor);\n}\n"
  },
  {
    "path": "src/t_stream.cpp",
    "content": "/*\n * Copyright (c) 2017, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n#include \"endianconv.h\"\n#include \"stream.h\"\n\n/* Every stream item inside the listpack, has a flags field that is used to\n * mark the entry as deleted, or having the same field as the \"master\"\n * entry at the start of the listpack> */\n#define STREAM_ITEM_FLAG_NONE 0             /* No special flags. */\n#define STREAM_ITEM_FLAG_DELETED (1<<0)     /* Entry is deleted. Skip it. */\n#define STREAM_ITEM_FLAG_SAMEFIELDS (1<<1)  /* Same fields as master entry. */\n\n/* For stream commands that require multiple IDs\n * when the number of IDs is less than 'STREAMID_STATIC_VECTOR_LEN',\n * avoid malloc allocation.*/\n#define STREAMID_STATIC_VECTOR_LEN 8\n\n/* Max pre-allocation for listpack. This is done to avoid abuse of a user\n * setting stream_node_max_bytes to a huge number. */\n#define STREAM_LISTPACK_MAX_PRE_ALLOCATE 4096\n\n/* Don't let listpacks grow too big, even if the user config allows it.\n * doing so can lead to an overflow (trying to store more than 32bit length\n * into the listpack header), or actually an assertion since lpInsert\n * will return NULL. */\n#define STREAM_LISTPACK_MAX_SIZE (1<<30)\n\nvoid streamFreeCG(streamCG *cg);\nvoid streamFreeNACK(streamNACK *na);\nsize_t streamReplyWithRangeFromConsumerPEL(client *c, stream *s, streamID *start, streamID *end, size_t count, streamConsumer *consumer);\nint streamParseStrictIDOrReply(client *c, robj *o, streamID *id, uint64_t missing_seq);\nint streamParseIDOrReply(client *c, robj *o, streamID *id, uint64_t missing_seq);\n\n/* -----------------------------------------------------------------------\n * Low level stream encoding: a radix tree of listpacks.\n * ----------------------------------------------------------------------- */\n\n/* Create a new stream data structure. */\nstream *streamNew(void) {\n    stream *s = (stream*)zmalloc(sizeof(*s), MALLOC_SHARED);\n    s->rax = raxNew();\n    s->length = 0;\n    s->last_id.ms = 0;\n    s->last_id.seq = 0;\n    s->cgroups = NULL; /* Created on demand to save memory when not used. */\n    return s;\n}\n\n/* Free a stream, including the listpacks stored inside the radix tree. */\nvoid freeStream(stream *s) {\n    raxFreeWithCallback(s->rax,(void(*)(void*))lpFree);\n    if (s->cgroups)\n        raxFreeWithCallback(s->cgroups,(void(*)(void*))streamFreeCG);\n    zfree(s);\n}\n\n/* Return the length of a stream. */\nunsigned long streamLength(robj_roptr subject) {\n    stream *s = (stream*)ptrFromObj(subject);\n    return s->length;\n}\n\n/* Set 'id' to be its successor stream ID.\n * If 'id' is the maximal possible id, it is wrapped around to 0-0 and a\n * C_ERR is returned. */\nint streamIncrID(streamID *id) {\n    int ret = C_OK;\n    if (id->seq == UINT64_MAX) {\n        if (id->ms == UINT64_MAX) {\n            /* Special case where 'id' is the last possible streamID... */\n            id->ms = id->seq = 0;\n            ret = C_ERR;\n        } else {\n            id->ms++;\n            id->seq = 0;\n        }\n    } else {\n        id->seq++;\n    }\n    return ret;\n}\n\n/* Set 'id' to be its predecessor stream ID.\n * If 'id' is the minimal possible id, it remains 0-0 and a C_ERR is\n * returned. */\nint streamDecrID(streamID *id) {\n    int ret = C_OK;\n    if (id->seq == 0) {\n        if (id->ms == 0) {\n            /* Special case where 'id' is the first possible streamID... */\n            id->ms = id->seq = UINT64_MAX;\n            ret = C_ERR;\n        } else {\n            id->ms--;\n            id->seq = UINT64_MAX;\n        }\n    } else {\n        id->seq--;\n    }\n    return ret;\n}\n\n/* Generate the next stream item ID given the previous one. If the current\n * milliseconds Unix time is greater than the previous one, just use this\n * as time part and start with sequence part of zero. Otherwise we use the\n * previous time (and never go backward) and increment the sequence. */\nvoid streamNextID(streamID *last_id, streamID *new_id) {\n    uint64_t ms = mstime();\n    if (ms > last_id->ms) {\n        new_id->ms = ms;\n        new_id->seq = 0;\n    } else {\n        *new_id = *last_id;\n        streamIncrID(new_id);\n    }\n}\n\n/* This is a helper function for the COPY command.\n * Duplicate a Stream object, with the guarantee that the returned object\n * has the same encoding as the original one.\n *\n * The resulting object always has refcount set to 1 */\nrobj *streamDup(robj *o) {\n    robj *sobj;\n\n    serverAssert(o->type == OBJ_STREAM);\n\n    switch (o->encoding) {\n        case OBJ_ENCODING_STREAM:\n            sobj = createStreamObject();\n            break;\n        default:\n            serverPanic(\"Wrong encoding.\");\n            break;\n    }\n\n    stream *s;\n    stream *new_s;\n    s = (stream*)ptrFromObj(o);\n    new_s = (stream*)ptrFromObj(sobj);\n\n    raxIterator ri;\n    uint64_t rax_key[2];\n    raxStart(&ri, s->rax);\n    raxSeek(&ri, \"^\", NULL, 0);\n    size_t lp_bytes = 0;      /* Total bytes in the listpack. */\n    unsigned char *lp = NULL; /* listpack pointer. */\n    /* Get a reference to the listpack node. */\n    while (raxNext(&ri)) {\n        lp = (unsigned char*)ri.data;\n        lp_bytes = lpBytes(lp);\n        unsigned char *new_lp = (unsigned char*)zmalloc(lp_bytes);\n        memcpy(new_lp, lp, lp_bytes);\n        memcpy(rax_key, ri.key, sizeof(rax_key));\n        raxInsert(new_s->rax, (unsigned char *)&rax_key, sizeof(rax_key),\n                  new_lp, NULL);\n    }\n    new_s->length = s->length;\n    new_s->last_id = s->last_id;\n    raxStop(&ri);\n\n    if (s->cgroups == NULL) return sobj;\n\n    /* Consumer Groups */\n    raxIterator ri_cgroups;\n    raxStart(&ri_cgroups, s->cgroups);\n    raxSeek(&ri_cgroups, \"^\", NULL, 0);\n    while (raxNext(&ri_cgroups)) {\n        streamCG *cg = (streamCG*)ri_cgroups.data;\n        streamCG *new_cg = streamCreateCG(new_s, (char *)ri_cgroups.key,\n                                          ri_cgroups.key_len, &cg->last_id);\n\n        serverAssert(new_cg != NULL);\n\n        /* Consumer Group PEL */\n        raxIterator ri_cg_pel;\n        raxStart(&ri_cg_pel,cg->pel);\n        raxSeek(&ri_cg_pel,\"^\",NULL,0);\n        while(raxNext(&ri_cg_pel)){\n            streamNACK *nack = (streamNACK*)ri_cg_pel.data;\n            streamNACK *new_nack = streamCreateNACK(NULL);\n            new_nack->delivery_time = nack->delivery_time;\n            new_nack->delivery_count = nack->delivery_count;\n            raxInsert(new_cg->pel, ri_cg_pel.key, sizeof(streamID), new_nack, NULL);\n        }\n        raxStop(&ri_cg_pel);\n\n        /* Consumers */\n        raxIterator ri_consumers;\n        raxStart(&ri_consumers, cg->consumers);\n        raxSeek(&ri_consumers, \"^\", NULL, 0);\n        while (raxNext(&ri_consumers)) {\n            streamConsumer *consumer = (streamConsumer*)ri_consumers.data;\n            streamConsumer *new_consumer;\n            new_consumer = (streamConsumer*)zmalloc(sizeof(*new_consumer));\n            new_consumer->name = sdsdup(consumer->name);\n            new_consumer->pel = raxNew();\n            raxInsert(new_cg->consumers,(unsigned char *)new_consumer->name,\n                        sdslen(new_consumer->name), new_consumer, NULL);\n            new_consumer->seen_time = consumer->seen_time;\n\n            /* Consumer PEL */\n            raxIterator ri_cpel;\n            raxStart(&ri_cpel, consumer->pel);\n            raxSeek(&ri_cpel, \"^\", NULL, 0);\n            while (raxNext(&ri_cpel)) {\n                streamNACK *new_nack = (streamNACK*)raxFind(new_cg->pel,ri_cpel.key,sizeof(streamID));\n\n                serverAssert(new_nack != raxNotFound);\n\n                new_nack->consumer = new_consumer;\n                raxInsert(new_consumer->pel,ri_cpel.key,sizeof(streamID),new_nack,NULL);\n            }\n            raxStop(&ri_cpel);\n        }\n        raxStop(&ri_consumers);\n    }\n    raxStop(&ri_cgroups);\n    return sobj;\n}\n\n/* This is just a wrapper for lpAppend() to directly use a 64 bit integer\n * instead of a string. */\nunsigned char *lpAppendInteger(unsigned char *lp, int64_t value) {\n    char buf[LONG_STR_SIZE];\n    int slen = ll2string(buf,sizeof(buf),value);\n    return lpAppend(lp,(unsigned char*)buf,slen);\n}\n\n/* This is just a wrapper for lpReplace() to directly use a 64 bit integer\n * instead of a string to replace the current element. The function returns\n * the new listpack as return value, and also updates the current cursor\n * by updating '*pos'. */\nunsigned char *lpReplaceInteger(unsigned char *lp, unsigned char **pos, int64_t value) {\n    char buf[LONG_STR_SIZE];\n    int slen = ll2string(buf,sizeof(buf),value);\n    return lpInsert(lp, (unsigned char*)buf, slen, *pos, LP_REPLACE, pos);\n}\n\n/* This is a wrapper function for lpGet() to directly get an integer value\n * from the listpack (that may store numbers as a string), converting\n * the string if needed.\n * The 'valid\" argument is an optional output parameter to get an indication\n * if the record was valid, when this parameter is NULL, the function will\n * fail with an assertion. */\nstatic inline int64_t lpGetIntegerIfValid(unsigned char *ele, int *valid) {\n    int64_t v;\n    unsigned char *e = lpGet(ele,&v,NULL);\n    if (e == NULL) {\n        if (valid)\n            *valid = 1;\n        return v;\n    }\n    /* The following code path should never be used for how listpacks work:\n     * they should always be able to store an int64_t value in integer\n     * encoded form. However the implementation may change. */\n    long long ll;\n    int ret = string2ll((char*)e,v,&ll);\n    if (valid)\n        *valid = ret;\n    else\n        serverAssert(ret != 0);\n    v = ll;\n    return v;\n}\n\n#define lpGetInteger(ele) lpGetIntegerIfValid(ele, NULL)\n\n/* Get an edge streamID of a given listpack.\n * 'master_id' is an input param, used to build the 'edge_id' output param */\nint lpGetEdgeStreamID(unsigned char *lp, int first, streamID *master_id, streamID *edge_id)\n{\n   if (lp == NULL)\n       return 0;\n\n   unsigned char *lp_ele;\n\n   /* We need to seek either the first or the last entry depending\n    * on the direction of the iteration. */\n   if (first) {\n       /* Get the master fields count. */\n       lp_ele = lpFirst(lp);        /* Seek items count */\n       lp_ele = lpNext(lp, lp_ele); /* Seek deleted count. */\n       lp_ele = lpNext(lp, lp_ele); /* Seek num fields. */\n       int64_t master_fields_count = lpGetInteger(lp_ele);\n       lp_ele = lpNext(lp, lp_ele); /* Seek first field. */\n\n       /* If we are iterating in normal order, skip the master fields\n        * to seek the first actual entry. */\n       for (int64_t i = 0; i < master_fields_count; i++)\n           lp_ele = lpNext(lp, lp_ele);\n\n       /* If we are going forward, skip the previous entry's\n        * lp-count field (or in case of the master entry, the zero\n        * term field) */\n       lp_ele = lpNext(lp, lp_ele);\n       if (lp_ele == NULL)\n           return 0;\n   } else {\n       /* If we are iterating in reverse direction, just seek the\n        * last part of the last entry in the listpack (that is, the\n        * fields count). */\n       lp_ele = lpLast(lp);\n\n       /* If we are going backward, read the number of elements this\n        * entry is composed of, and jump backward N times to seek\n        * its start. */\n       int64_t lp_count = lpGetInteger(lp_ele);\n       if (lp_count == 0) /* We reached the master entry. */\n           return 0;\n\n       while (lp_count--)\n           lp_ele = lpPrev(lp, lp_ele);\n   }\n\n   lp_ele = lpNext(lp, lp_ele); /* Seek ID (lp_ele currently points to 'flags'). */\n\n   /* Get the ID: it is encoded as difference between the master\n    * ID and this entry ID. */\n   streamID id = *master_id;\n   id.ms += lpGetInteger(lp_ele);\n   lp_ele = lpNext(lp, lp_ele);\n   id.seq += lpGetInteger(lp_ele);\n   *edge_id = id;\n   return 1;\n}\n\n/* Debugging function to log the full content of a listpack. Useful\n * for development and debugging. */\nvoid streamLogListpackContent(unsigned char *lp) {\n    unsigned char *p = lpFirst(lp);\n    while(p) {\n        unsigned char buf[LP_INTBUF_SIZE];\n        int64_t v;\n        unsigned char *ele = lpGet(p,&v,buf);\n        serverLog(LL_WARNING,\"- [%d] '%.*s'\", (int)v, (int)v, ele);\n        p = lpNext(lp,p);\n    }\n}\n\n/* Convert the specified stream entry ID as a 128 bit big endian number, so\n * that the IDs can be sorted lexicographically. */\nvoid streamEncodeID(void *buf, streamID *id) {\n    uint64_t e[2];\n    e[0] = htonu64(id->ms);\n    e[1] = htonu64(id->seq);\n    memcpy(buf,e,sizeof(e));\n}\n\n/* This is the reverse of streamEncodeID(): the decoded ID will be stored\n * in the 'id' structure passed by reference. The buffer 'buf' must point\n * to a 128 bit big-endian encoded ID. */\nvoid streamDecodeID(void *buf, streamID *id) {\n    uint64_t e[2];\n    memcpy(e,buf,sizeof(e));\n    id->ms = ntohu64(e[0]);\n    id->seq = ntohu64(e[1]);\n}\n\n/* Compare two stream IDs. Return -1 if a < b, 0 if a == b, 1 if a > b. */\nint streamCompareID(streamID *a, streamID *b) {\n    if (a->ms > b->ms) return 1;\n    else if (a->ms < b->ms) return -1;\n    /* The ms part is the same. Check the sequence part. */\n    else if (a->seq > b->seq) return 1;\n    else if (a->seq < b->seq) return -1;\n    /* Everything is the same: IDs are equal. */\n    return 0;\n}\n\nvoid streamGetEdgeID(stream *s, int first, streamID *edge_id)\n{\n    raxIterator ri;\n    raxStart(&ri, s->rax);\n    int empty;\n    if (first) {\n        raxSeek(&ri, \"^\", NULL, 0);\n        empty = !raxNext(&ri);\n    } else {\n        raxSeek(&ri, \"$\", NULL, 0);\n        empty = !raxPrev(&ri);\n    }\n\n    if (empty) {\n        /* Stream is empty, mark edge ID as lowest/highest possible. */\n        edge_id->ms = first ? UINT64_MAX : 0;\n        edge_id->seq = first ? UINT64_MAX : 0;\n        raxStop(&ri);\n        return;\n    }\n\n    unsigned char *lp = (unsigned char*)ri.data;\n\n    /* Read the master ID from the radix tree key. */\n    streamID master_id;\n    streamDecodeID(ri.key, &master_id);\n\n    /* Construct edge ID. */\n    lpGetEdgeStreamID(lp, first, &master_id, edge_id);\n\n    raxStop(&ri);\n}\n\n/* Adds a new item into the stream 's' having the specified number of\n * field-value pairs as specified in 'numfields' and stored into 'argv'.\n * Returns the new entry ID populating the 'added_id' structure.\n *\n * If 'use_id' is not NULL, the ID is not auto-generated by the function,\n * but instead the passed ID is used to add the new entry. In this case\n * adding the entry may fail as specified later in this comment.\n *\n * The function returns C_OK if the item was added, this is always true\n * if the ID was generated by the function. However the function may return\n * C_ERR in several cases:\n * 1. If an ID was given via 'use_id', but adding it failed since the\n *    current top ID is greater or equal. errno will be set to EDOM.\n * 2. If a size of a single element or the sum of the elements is too big to\n *    be stored into the stream. errno will be set to ERANGE. */\nint streamAppendItem(stream *s, robj **argv, int64_t numfields, streamID *added_id, streamID *use_id) {\n\n    /* Generate the new entry ID. */\n    streamID id;\n    if (use_id)\n        id = *use_id;\n    else\n        streamNextID(&s->last_id,&id);\n\n    /* Check that the new ID is greater than the last entry ID\n     * or return an error. Automatically generated IDs might\n     * overflow (and wrap-around) when incrementing the sequence\n       part. */\n    if (streamCompareID(&id,&s->last_id) <= 0) {\n        errno = EDOM;\n        return C_ERR;\n    }\n\n    /* Avoid overflow when trying to add an element to the stream (listpack\n     * can only host up to 32bit length sttrings, and also a total listpack size\n     * can't be bigger than 32bit length. */\n    size_t totelelen = 0;\n    for (int64_t i = 0; i < numfields*2; i++) {\n        sds ele = szFromObj(argv[i]);\n        totelelen += sdslen(ele);\n    }\n    if (totelelen > STREAM_LISTPACK_MAX_SIZE) {\n        errno = ERANGE;\n        return C_ERR;\n    }\n\n    /* Add the new entry. */\n    raxIterator ri;\n    raxStart(&ri,s->rax);\n    raxSeek(&ri,\"$\",NULL,0);\n\n    size_t lp_bytes = 0;        /* Total bytes in the tail listpack. */\n    unsigned char *lp = NULL;   /* Tail listpack pointer. */\n\n    /* Get a reference to the tail node listpack. */\n    if (raxNext(&ri)) {\n        lp = (unsigned char*)ri.data;\n        lp_bytes = lpBytes(lp);\n    }\n    raxStop(&ri);\n\n    /* We have to add the key into the radix tree in lexicographic order,\n     * to do so we consider the ID as a single 128 bit number written in\n     * big endian, so that the most significant bytes are the first ones. */\n    uint64_t rax_key[2];    /* Key in the radix tree containing the listpack.*/\n    streamID master_id;     /* ID of the master entry in the listpack. */\n\n    /* Create a new listpack and radix tree node if needed. Note that when\n     * a new listpack is created, we populate it with a \"master entry\". This\n     * is just a set of fields that is taken as references in order to compress\n     * the stream entries that we'll add inside the listpack.\n     *\n     * Note that while we use the first added entry fields to create\n     * the master entry, the first added entry is NOT represented in the master\n     * entry, which is a stand alone object. But of course, the first entry\n     * will compress well because it's used as reference.\n     *\n     * The master entry is composed like in the following example:\n     *\n     * +-------+---------+------------+---------+--/--+---------+---------+-+\n     * | count | deleted | num-fields | field_1 | field_2 | ... | field_N |0|\n     * +-------+---------+------------+---------+--/--+---------+---------+-+\n     *\n     * count and deleted just represent respectively the total number of\n     * entries inside the listpack that are valid, and marked as deleted\n     * (deleted flag in the entry flags set). So the total number of items\n     * actually inside the listpack (both deleted and not) is count+deleted.\n     *\n     * The real entries will be encoded with an ID that is just the\n     * millisecond and sequence difference compared to the key stored at\n     * the radix tree node containing the listpack (delta encoding), and\n     * if the fields of the entry are the same as the master entry fields, the\n     * entry flags will specify this fact and the entry fields and number\n     * of fields will be omitted (see later in the code of this function).\n     *\n     * The \"0\" entry at the end is the same as the 'lp-count' entry in the\n     * regular stream entries (see below), and marks the fact that there are\n     * no more entries, when we scan the stream from right to left. */\n\n    /* First of all, check if we can append to the current macro node or\n     * if we need to switch to the next one. 'lp' will be set to NULL if\n     * the current node is full. */\n    if (lp != NULL) {\n        size_t node_max_bytes = g_pserver->stream_node_max_bytes;\n        if (node_max_bytes == 0 || node_max_bytes > STREAM_LISTPACK_MAX_SIZE)\n            node_max_bytes = STREAM_LISTPACK_MAX_SIZE;\n        if (lp_bytes + totelelen >= node_max_bytes) {\n            lp = NULL;\n        } else if (g_pserver->stream_node_max_entries) {\n            unsigned char *lp_ele = lpFirst(lp);\n            /* Count both live entries and deleted ones. */\n            int64_t count = lpGetInteger(lp_ele) + lpGetInteger(lpNext(lp,lp_ele));\n            if (count >= g_pserver->stream_node_max_entries) {\n                /* Shrink extra pre-allocated memory */\n                lp = lpShrinkToFit(lp);\n                if (ri.data != lp)\n                    raxInsert(s->rax,ri.key,ri.key_len,lp,NULL);\n                lp = NULL;\n            }\n        }\n    }\n\n    int flags = STREAM_ITEM_FLAG_NONE;\n    if (lp == NULL) {\n        master_id = id;\n        streamEncodeID(rax_key,&id);\n        /* Create the listpack having the master entry ID and fields.\n         * Pre-allocate some bytes when creating listpack to avoid realloc on\n         * every XADD. Since listpack.c uses malloc_size, it'll grow in steps,\n         * and won't realloc on every XADD.\n         * When listpack reaches max number of entries, we'll shrink the\n         * allocation to fit the data. */\n        size_t prealloc = STREAM_LISTPACK_MAX_PRE_ALLOCATE;\n        if (g_pserver->stream_node_max_bytes > 0 && g_pserver->stream_node_max_bytes < prealloc) {\n            prealloc = g_pserver->stream_node_max_bytes;\n        }\n        lp = lpNew(prealloc);\n        lp = lpAppendInteger(lp,1); /* One item, the one we are adding. */\n        lp = lpAppendInteger(lp,0); /* Zero deleted so far. */\n        lp = lpAppendInteger(lp,numfields);\n        for (int64_t i = 0; i < numfields; i++) {\n            sds field = szFromObj(argv[i*2]);\n            lp = lpAppend(lp,(unsigned char*)field,sdslen(field));\n        }\n        lp = lpAppendInteger(lp,0); /* Master entry zero terminator. */\n        raxInsert(s->rax,(unsigned char*)&rax_key,sizeof(rax_key),lp,NULL);\n        /* The first entry we insert, has obviously the same fields of the\n         * master entry. */\n        flags |= STREAM_ITEM_FLAG_SAMEFIELDS;\n    } else {\n        serverAssert(ri.key_len == sizeof(rax_key));\n        memcpy(rax_key,ri.key,sizeof(rax_key));\n\n        /* Read the master ID from the radix tree key. */\n        streamDecodeID(rax_key,&master_id);\n        unsigned char *lp_ele = lpFirst(lp);\n\n        /* Update count and skip the deleted fields. */\n        int64_t count = lpGetInteger(lp_ele);\n        lp = lpReplaceInteger(lp,&lp_ele,count+1);\n        lp_ele = lpNext(lp,lp_ele); /* seek deleted. */\n        lp_ele = lpNext(lp,lp_ele); /* seek master entry num fields. */\n\n        /* Check if the entry we are adding, have the same fields\n         * as the master entry. */\n        int64_t master_fields_count = lpGetInteger(lp_ele);\n        lp_ele = lpNext(lp,lp_ele);\n        if (numfields == master_fields_count) {\n            int64_t i;\n            for (i = 0; i < master_fields_count; i++) {\n                sds field = szFromObj(argv[i*2]);\n                int64_t e_len;\n                unsigned char buf[LP_INTBUF_SIZE];\n                unsigned char *e = lpGet(lp_ele,&e_len,buf);\n                /* Stop if there is a mismatch. */\n                if (sdslen(field) != (size_t)e_len ||\n                    memcmp(e,field,e_len) != 0) break;\n                lp_ele = lpNext(lp,lp_ele);\n            }\n            /* All fields are the same! We can compress the field names\n             * setting a single bit in the flags. */\n            if (i == master_fields_count) flags |= STREAM_ITEM_FLAG_SAMEFIELDS;\n        }\n    }\n\n    /* Populate the listpack with the new entry. We use the following\n     * encoding:\n     *\n     * +-----+--------+----------+-------+-------+-/-+-------+-------+--------+\n     * |flags|entry-id|num-fields|field-1|value-1|...|field-N|value-N|lp-count|\n     * +-----+--------+----------+-------+-------+-/-+-------+-------+--------+\n     *\n     * However if the SAMEFIELD flag is set, we have just to populate\n     * the entry with the values, so it becomes:\n     *\n     * +-----+--------+-------+-/-+-------+--------+\n     * |flags|entry-id|value-1|...|value-N|lp-count|\n     * +-----+--------+-------+-/-+-------+--------+\n     *\n     * The entry-id field is actually two separated fields: the ms\n     * and seq difference compared to the master entry.\n     *\n     * The lp-count field is a number that states the number of listpack pieces\n     * that compose the entry, so that it's possible to travel the entry\n     * in reverse order: we can just start from the end of the listpack, read\n     * the entry, and jump back N times to seek the \"flags\" field to read\n     * the stream full entry. */\n    lp = lpAppendInteger(lp,flags);\n    lp = lpAppendInteger(lp,id.ms - master_id.ms);\n    lp = lpAppendInteger(lp,id.seq - master_id.seq);\n    if (!(flags & STREAM_ITEM_FLAG_SAMEFIELDS))\n        lp = lpAppendInteger(lp,numfields);\n    for (int64_t i = 0; i < numfields; i++) {\n        sds field = szFromObj(argv[i*2]), value = szFromObj(argv[i*2+1]);\n        if (!(flags & STREAM_ITEM_FLAG_SAMEFIELDS))\n            lp = lpAppend(lp,(unsigned char*)field,sdslen(field));\n        lp = lpAppend(lp,(unsigned char*)value,sdslen(value));\n    }\n    /* Compute and store the lp-count field. */\n    int64_t lp_count = numfields;\n    lp_count += 3; /* Add the 3 fixed fields flags + ms-diff + seq-diff. */\n    if (!(flags & STREAM_ITEM_FLAG_SAMEFIELDS)) {\n        /* If the item is not compressed, it also has the fields other than\n         * the values, and an additional num-fileds field. */\n        lp_count += numfields+1;\n    }\n    lp = lpAppendInteger(lp,lp_count);\n\n    /* Insert back into the tree in order to update the listpack pointer. */\n    if (ri.data != lp)\n        raxInsert(s->rax,(unsigned char*)&rax_key,sizeof(rax_key),lp,NULL);\n    s->length++;\n    s->last_id = id;\n    if (added_id) *added_id = id;\n    return C_OK;\n}\n\ntypedef struct {\n    /* XADD options */\n    streamID id; /* User-provided ID, for XADD only. */\n    int id_given; /* Was an ID different than \"*\" specified? for XADD only. */\n    int no_mkstream; /* if set to 1 do not create new stream */\n\n    /* XADD + XTRIM common options */\n    int trim_strategy; /* TRIM_STRATEGY_* */\n    int trim_strategy_arg_idx; /* Index of the count in MAXLEN/MINID, for rewriting. */\n    int approx_trim; /* If 1 only delete whole radix tree nodes, so\n                      * the trim argument is not applied verbatim. */\n    long long limit; /* Maximum amount of entries to trim. If 0, no limitation\n                      * on the amount of trimming work is enforced. */\n    /* TRIM_STRATEGY_MAXLEN options */\n    long long maxlen; /* After trimming, leave stream at this length . */\n    /* TRIM_STRATEGY_MINID options */\n    streamID minid; /* Trim by ID (No stream entries with ID < 'minid' will remain) */\n} streamAddTrimArgs;\n\n#define TRIM_STRATEGY_NONE 0\n#define TRIM_STRATEGY_MAXLEN 1\n#define TRIM_STRATEGY_MINID 2\n\n/* Trim the stream 's' according to args->trim_strategy, and return the\n * number of elements removed from the stream. The 'approx' option, if non-zero,\n * specifies that the trimming must be performed in a approximated way in\n * order to maximize performances. This means that the stream may contain\n * entries with IDs < 'id' in case of MINID (or more elements than 'maxlen'\n * in case of MAXLEN), and elements are only removed if we can remove\n * a *whole* node of the radix tree. The elements are removed from the head\n * of the stream (older elements).\n *\n * The function may return zero if:\n *\n * 1) The minimal entry ID of the stream is already < 'id' (MINID); or\n * 2) The stream is already shorter or equal to the specified max length (MAXLEN); or\n * 3) The 'approx' option is true and the head node did not have enough elements\n *    to be deleted.\n *\n * args->limit is the maximum number of entries to delete. The purpose is to\n * prevent this function from taking to long.\n * If 'limit' is 0 then we do not limit the number of deleted entries.\n * Much like the 'approx', if 'limit' is smaller than the number of entries\n * that should be trimmed, there is a chance we will still have entries with\n * IDs < 'id' (or number of elements >= maxlen in case of MAXLEN).\n */\nint64_t streamTrim(stream *s, streamAddTrimArgs *args) {\n    size_t maxlen = args->maxlen;\n    streamID *id = &args->minid;\n    int approx = args->approx_trim;\n    int64_t limit = args->limit;\n    int trim_strategy = args->trim_strategy;\n\n    if (trim_strategy == TRIM_STRATEGY_NONE)\n        return 0;\n\n    raxIterator ri;\n    raxStart(&ri,s->rax);\n    raxSeek(&ri,\"^\",NULL,0);\n\n    int64_t deleted = 0;\n    while (raxNext(&ri)) {\n        if (trim_strategy == TRIM_STRATEGY_MAXLEN && s->length <= maxlen)\n            break;\n\n        unsigned char *lp = (unsigned char*)ri.data, *p = lpFirst(lp);\n        int64_t entries = lpGetInteger(p);\n\n        /* Check if we exceeded the amount of work we could do */\n        if (limit && (deleted + entries) > limit)\n            break;\n\n        /* Check if we can remove the whole node. */\n        int remove_node;\n        streamID master_id = {0}; /* For MINID */\n        if (trim_strategy == TRIM_STRATEGY_MAXLEN) {\n            remove_node = s->length - entries >= maxlen;\n        } else {\n            /* Read the master ID from the radix tree key. */\n            streamDecodeID(ri.key, &master_id);\n\n            /* Read last ID. */\n            streamID last_id;\n            lpGetEdgeStreamID(lp, 0, &master_id, &last_id);\n\n            /* We can remove the entire node id its last ID < 'id' */\n            remove_node = streamCompareID(&last_id, id) < 0;\n        }\n\n        if (remove_node) {\n            lpFree(lp);\n            raxRemove(s->rax,ri.key,ri.key_len,NULL);\n            raxSeek(&ri,\">=\",ri.key,ri.key_len);\n            s->length -= entries;\n            deleted += entries;\n            continue;\n        }\n\n        /* If we cannot remove a whole element, and approx is true,\n         * stop here. */\n        if (approx) break;\n\n        /* Now we have to trim entries from within 'lp' */\n        int64_t deleted_from_lp = 0;\n\n        p = lpNext(lp, p); /* Skip deleted field. */\n        p = lpNext(lp, p); /* Skip num-of-fields in the master entry. */\n\n        /* Skip all the master fields. */\n        int64_t master_fields_count = lpGetInteger(p);\n        p = lpNext(lp,p); /* Skip the first field. */\n        for (int64_t j = 0; j < master_fields_count; j++)\n            p = lpNext(lp,p); /* Skip all master fields. */\n        p = lpNext(lp,p); /* Skip the zero master entry terminator. */\n\n        /* 'p' is now pointing to the first entry inside the listpack.\n         * We have to run entry after entry, marking entries as deleted\n         * if they are already not deleted. */\n        while (p) {\n            /* We keep a copy of p (which point to flags part) in order to\n             * update it after (and if) we actually remove the entry */\n            unsigned char *pcopy = p;\n\n            int64_t flags = lpGetInteger(p);\n            p = lpNext(lp, p); /* Skip flags. */\n            int64_t to_skip;\n\n            int64_t ms_delta = lpGetInteger(p);\n            p = lpNext(lp, p); /* Skip ID ms delta */\n            int64_t seq_delta = lpGetInteger(p);\n            p = lpNext(lp, p); /* Skip ID seq delta */\n\n            streamID currid = {0}; /* For MINID */\n            if (trim_strategy == TRIM_STRATEGY_MINID) {\n                currid.ms = master_id.ms + ms_delta;\n                currid.seq = master_id.seq + seq_delta;\n            }\n\n            int stop;\n            if (trim_strategy == TRIM_STRATEGY_MAXLEN) {\n                stop = s->length <= maxlen;\n            } else {\n                /* Following IDs will definitely be greater because the rax\n                 * tree is sorted, no point of continuing. */\n                stop = streamCompareID(&currid, id) >= 0;\n            }\n            if (stop)\n                break;\n\n            if (flags & STREAM_ITEM_FLAG_SAMEFIELDS) {\n                to_skip = master_fields_count;\n            } else {\n                to_skip = lpGetInteger(p); /* Get num-fields. */\n                p = lpNext(lp,p); /* Skip num-fields. */\n                to_skip *= 2; /* Fields and values. */\n            }\n\n            while(to_skip--) p = lpNext(lp,p); /* Skip the whole entry. */\n            p = lpNext(lp,p); /* Skip the final lp-count field. */\n\n            /* Mark the entry as deleted. */\n            if (!(flags & STREAM_ITEM_FLAG_DELETED)) {\n                intptr_t delta = p - lp;\n                flags |= STREAM_ITEM_FLAG_DELETED;\n                lp = lpReplaceInteger(lp, &pcopy, flags);\n                deleted_from_lp++;\n                s->length--;\n                p = lp + delta;\n            }\n        }\n        deleted += deleted_from_lp;\n\n        /* Now we update the entries/deleted counters. */\n        p = lpFirst(lp);\n        lp = lpReplaceInteger(lp,&p,entries-deleted_from_lp);\n        p = lpNext(lp,p); /* Skip deleted field. */\n        int64_t marked_deleted = lpGetInteger(p);\n        lp = lpReplaceInteger(lp,&p,marked_deleted+deleted_from_lp);\n        p = lpNext(lp,p); /* Skip num-of-fields in the master entry. */\n\n        /* Here we should perform garbage collection in case at this point\n         * there are too many entries deleted inside the listpack. */\n        entries -= deleted_from_lp;\n        marked_deleted += deleted_from_lp;\n        if (entries + marked_deleted > 10 && marked_deleted > entries/2) {\n            /* TODO: perform a garbage collection. */\n        }\n\n        /* Update the listpack with the new pointer. */\n        raxInsert(s->rax,ri.key,ri.key_len,lp,NULL);\n\n        break; /* If we are here, there was enough to delete in the current\n                  node, so no need to go to the next node. */\n    }\n\n    raxStop(&ri);\n    return deleted;\n}\n\n/* Trims a stream by length. Returns the number of deleted items. */\nint64_t streamTrimByLength(stream *s, long long maxlen, int approx) {\n    streamAddTrimArgs args = {{0}};\n    args.trim_strategy = TRIM_STRATEGY_MAXLEN;\n    args.approx_trim = approx;\n    args.limit = approx ? 100 * g_pserver->stream_node_max_entries : 0;\n    args.maxlen = maxlen;\n    return streamTrim(s, &args);\n}\n\n/* Trims a stream by minimum ID. Returns the number of deleted items. */\nint64_t streamTrimByID(stream *s, streamID minid, int approx) {\n    streamAddTrimArgs args = {{0}};\n    args.trim_strategy = TRIM_STRATEGY_MINID;\n    args.approx_trim = approx;\n    args.limit = approx ? 100 * g_pserver->stream_node_max_entries : 0;\n    args.minid = minid;\n    return streamTrim(s, &args);\n}\n\n/* Parse the arguments of XADD/XTRIM.\n *\n * See streamAddTrimArgs for more details about the arguments handled.\n *\n * This function returns the position of the ID argument (relevant only to XADD).\n * On error -1 is returned and a reply is sent. */\nstatic int streamParseAddOrTrimArgsOrReply(client *c, streamAddTrimArgs *args, int xadd) {\n    /* Initialize arguments to defaults */\n    memset(args, 0, sizeof(*args));\n\n    /* Parse options. */\n    int i = 2; /* This is the first argument position where we could\n                  find an option, or the ID. */\n    int limit_given = 0;\n    for (; i < c->argc; i++) {\n        int moreargs = (c->argc-1) - i; /* Number of additional arguments. */\n        char *opt = szFromObj(c->argv[i]);\n        if (xadd && opt[0] == '*' && opt[1] == '\\0') {\n            /* This is just a fast path for the common case of auto-ID\n             * creation. */\n            break;\n        } else if (!strcasecmp(opt,\"maxlen\") && moreargs) {\n            if (args->trim_strategy != TRIM_STRATEGY_NONE) {\n                addReplyError(c,\"syntax error, MAXLEN and MINID options at the same time are not compatible\");\n                return -1;\n            }\n            args->approx_trim = 0;\n            char *next = szFromObj(c->argv[i+1]);\n            /* Check for the form MAXLEN ~ <count>. */\n            if (moreargs >= 2 && next[0] == '~' && next[1] == '\\0') {\n                args->approx_trim = 1;\n                i++;\n            } else if (moreargs >= 2 && next[0] == '=' && next[1] == '\\0') {\n                i++;\n            }\n            if (getLongLongFromObjectOrReply(c,c->argv[i+1],&args->maxlen,NULL)\n                != C_OK) return -1;\n\n            if (args->maxlen < 0) {\n                addReplyError(c,\"The MAXLEN argument must be >= 0.\");\n                return -1;\n            }\n            i++;\n            args->trim_strategy = TRIM_STRATEGY_MAXLEN;\n            args->trim_strategy_arg_idx = i;\n        } else if (!strcasecmp(opt,\"minid\") && moreargs) {\n            if (args->trim_strategy != TRIM_STRATEGY_NONE) {\n                addReplyError(c,\"syntax error, MAXLEN and MINID options at the same time are not compatible\");\n                return -1;\n            }\n            args->approx_trim = 0;\n            char *next = szFromObj(c->argv[i+1]);\n            /* Check for the form MINID ~ <id>|<age>. */\n            if (moreargs >= 2 && next[0] == '~' && next[1] == '\\0') {\n                args->approx_trim = 1;\n                i++;\n            } else if (moreargs >= 2 && next[0] == '=' && next[1] == '\\0') {\n                i++;\n            }\n\n            if (streamParseStrictIDOrReply(c,c->argv[i+1],&args->minid,0) != C_OK)\n                return -1;\n            \n            i++;\n            args->trim_strategy = TRIM_STRATEGY_MINID;\n            args->trim_strategy_arg_idx = i;\n        } else if (!strcasecmp(opt,\"limit\") && moreargs) {\n            /* Note about LIMIT: If it was not provided by the caller we set\n             * it to 100*g_pserver->stream_node_max_entries, and that's to prevent the\n             * trimming from taking too long, on the expense of not deleting entries\n             * that should be trimmed.\n             * If user wanted exact trimming (i.e. no '~') we never limit the number\n             * of trimmed entries */\n            if (getLongLongFromObjectOrReply(c,c->argv[i+1],&args->limit,NULL) != C_OK)\n                return -1;\n\n            if (args->limit < 0) {\n                addReplyError(c,\"The LIMIT argument must be >= 0.\");\n                return -1;\n            }\n            limit_given = 1;\n            i++;\n        } else if (xadd && !strcasecmp(opt,\"nomkstream\")) {\n            args->no_mkstream = 1;\n        } else if (xadd) {\n            /* If we are here is a syntax error or a valid ID. */\n            if (streamParseStrictIDOrReply(c,c->argv[i],&args->id,0) != C_OK)\n                return -1;\n            args->id_given = 1;\n            break;\n        } else {\n            addReplyErrorObject(c,shared.syntaxerr);\n            return -1;\n        }\n    }\n\n    if (args->limit && args->trim_strategy == TRIM_STRATEGY_NONE) {\n        addReplyError(c,\"syntax error, LIMIT cannot be used without specifying a trimming strategy\");\n        return -1;\n    }\n\n    if (!xadd && args->trim_strategy == TRIM_STRATEGY_NONE) {\n        addReplyError(c,\"syntax error, XTRIM must be called with a trimming strategy\");\n        return -1;\n    }\n\n    if ((c->flags & CLIENT_MASTER) || c->id == CLIENT_ID_AOF) {\n        /* If command cam from master or from AOF we must not enforce maxnodes\n         * (The maxlen/minid argument was re-written to make sure there's no\n         * inconsistency). */\n        args->limit = 0;\n    } else {\n        /* We need to set the limit (only if we got '~') */\n        if (limit_given) {\n            if (!args->approx_trim) {\n                /* LIMIT was provided without ~ */\n                addReplyError(c,\"syntax error, LIMIT cannot be used without the special ~ option\");\n                return -1;\n            }\n        } else {\n            /* User didn't provide LIMIT, we must set it. */\n\n            if (args->approx_trim) {\n                /* In order to prevent from trimming to do too much work and cause\n                 * latency spikes we limit the amount of work it can do */\n                args->limit = 100 * g_pserver->stream_node_max_entries; /* Maximum 100 rax nodes. */\n            } else {\n                /* No LIMIT for exact trimming */\n                args->limit = 0;\n            }\n        }\n    }\n\n    return i;\n}\n\n/* Initialize the stream iterator, so that we can call iterating functions\n * to get the next items. This requires a corresponding streamIteratorStop()\n * at the end. The 'rev' parameter controls the direction. If it's zero the\n * iteration is from the start to the end element (inclusive), otherwise\n * if rev is non-zero, the iteration is reversed.\n *\n * Once the iterator is initialized, we iterate like this:\n *\n *  streamIterator myiterator;\n *  streamIteratorStart(&myiterator,...);\n *  int64_t numfields;\n *  while(streamIteratorGetID(&myiterator,&ID,&numfields)) {\n *      while(numfields--) {\n *          unsigned char *key, *value;\n *          size_t key_len, value_len;\n *          streamIteratorGetField(&myiterator,&key,&value,&key_len,&value_len);\n *\n *          ... do what you want with key and value ...\n *      }\n *  }\n *  streamIteratorStop(&myiterator); */\nvoid streamIteratorStart(streamIterator *si, stream *s, streamID *start, streamID *end, int rev) {\n    /* Initialize the iterator and translates the iteration start/stop\n     * elements into a 128 big big-endian number. */\n    if (start) {\n        streamEncodeID(si->start_key,start);\n    } else {\n        si->start_key[0] = 0;\n        si->start_key[1] = 0;\n    }\n\n    if (end) {\n        streamEncodeID(si->end_key,end);\n    } else {\n        si->end_key[0] = UINT64_MAX;\n        si->end_key[1] = UINT64_MAX;\n    }\n\n    /* Seek the correct node in the radix tree. */\n    raxStart(&si->ri,s->rax);\n    if (!rev) {\n        if (start && (start->ms || start->seq)) {\n            raxSeek(&si->ri,\"<=\",(unsigned char*)si->start_key,\n                    sizeof(si->start_key));\n            if (raxEOF(&si->ri)) raxSeek(&si->ri,\"^\",NULL,0);\n        } else {\n            raxSeek(&si->ri,\"^\",NULL,0);\n        }\n    } else {\n        if (end && (end->ms || end->seq)) {\n            raxSeek(&si->ri,\"<=\",(unsigned char*)si->end_key,\n                    sizeof(si->end_key));\n            if (raxEOF(&si->ri)) raxSeek(&si->ri,\"$\",NULL,0);\n        } else {\n            raxSeek(&si->ri,\"$\",NULL,0);\n        }\n    }\n    si->pstream = s;\n    si->lp = NULL; /* There is no current listpack right now. */\n    si->lp_ele = NULL; /* Current listpack cursor. */\n    si->rev = rev;  /* Direction, if non-zero reversed, from end to start. */\n}\n\n/* Return 1 and store the current item ID at 'id' if there are still\n * elements within the iteration range, otherwise return 0 in order to\n * signal the iteration terminated. */\nint streamIteratorGetID(streamIterator *si, streamID *id, int64_t *numfields) {\n    while(1) { /* Will stop when element > stop_key or end of radix tree. */\n        /* If the current listpack is set to NULL, this is the start of the\n         * iteration or the previous listpack was completely iterated.\n         * Go to the next node. */\n        if (si->lp == NULL || si->lp_ele == NULL) {\n            if (!si->rev && !raxNext(&si->ri)) return 0;\n            else if (si->rev && !raxPrev(&si->ri)) return 0;\n            serverAssert(si->ri.key_len == sizeof(streamID));\n            /* Get the master ID. */\n            streamDecodeID(si->ri.key,&si->master_id);\n            /* Get the master fields count. */\n            si->lp = (unsigned char*)si->ri.data;\n            si->lp_ele = lpFirst(si->lp);           /* Seek items count */\n            si->lp_ele = lpNext(si->lp,si->lp_ele); /* Seek deleted count. */\n            si->lp_ele = lpNext(si->lp,si->lp_ele); /* Seek num fields. */\n            si->master_fields_count = lpGetInteger(si->lp_ele);\n            si->lp_ele = lpNext(si->lp,si->lp_ele); /* Seek first field. */\n            si->master_fields_start = si->lp_ele;\n            /* We are now pointing to the first field of the master entry.\n             * We need to seek either the first or the last entry depending\n             * on the direction of the iteration. */\n            if (!si->rev) {\n                /* If we are iterating in normal order, skip the master fields\n                 * to seek the first actual entry. */\n                for (uint64_t i = 0; i < si->master_fields_count; i++)\n                    si->lp_ele = lpNext(si->lp,si->lp_ele);\n            } else {\n                /* If we are iterating in reverse direction, just seek the\n                 * last part of the last entry in the listpack (that is, the\n                 * fields count). */\n                si->lp_ele = lpLast(si->lp);\n            }\n        } else if (si->rev) {\n            /* If we are iterating in the reverse order, and this is not\n             * the first entry emitted for this listpack, then we already\n             * emitted the current entry, and have to go back to the previous\n             * one. */\n            int64_t lp_count = lpGetInteger(si->lp_ele);\n            while(lp_count--) si->lp_ele = lpPrev(si->lp,si->lp_ele);\n            /* Seek lp-count of prev entry. */\n            si->lp_ele = lpPrev(si->lp,si->lp_ele);\n        }\n\n        /* For every radix tree node, iterate the corresponding listpack,\n         * returning elements when they are within range. */\n        while(1) {\n            if (!si->rev) {\n                /* If we are going forward, skip the previous entry\n                 * lp-count field (or in case of the master entry, the zero\n                 * term field) */\n                si->lp_ele = lpNext(si->lp,si->lp_ele);\n                if (si->lp_ele == NULL) break;\n            } else {\n                /* If we are going backward, read the number of elements this\n                 * entry is composed of, and jump backward N times to seek\n                 * its start. */\n                int64_t lp_count = lpGetInteger(si->lp_ele);\n                if (lp_count == 0) { /* We reached the master entry. */\n                    si->lp = NULL;\n                    si->lp_ele = NULL;\n                    break;\n                }\n                while(lp_count--) si->lp_ele = lpPrev(si->lp,si->lp_ele);\n            }\n\n            /* Get the flags entry. */\n            si->lp_flags = si->lp_ele;\n            int64_t flags = lpGetInteger(si->lp_ele);\n            si->lp_ele = lpNext(si->lp,si->lp_ele); /* Seek ID. */\n\n            /* Get the ID: it is encoded as difference between the master\n             * ID and this entry ID. */\n            *id = si->master_id;\n            id->ms += lpGetInteger(si->lp_ele);\n            si->lp_ele = lpNext(si->lp,si->lp_ele);\n            id->seq += lpGetInteger(si->lp_ele);\n            si->lp_ele = lpNext(si->lp,si->lp_ele);\n            unsigned char buf[sizeof(streamID)];\n            streamEncodeID(buf,id);\n\n            /* The number of entries is here or not depending on the\n             * flags. */\n            if (flags & STREAM_ITEM_FLAG_SAMEFIELDS) {\n                *numfields = si->master_fields_count;\n            } else {\n                *numfields = lpGetInteger(si->lp_ele);\n                si->lp_ele = lpNext(si->lp,si->lp_ele);\n            }\n            serverAssert(*numfields>=0);\n\n            /* If current >= start, and the entry is not marked as\n             * deleted, emit it. */\n            if (!si->rev) {\n                if (memcmp(buf,si->start_key,sizeof(streamID)) >= 0 &&\n                    !(flags & STREAM_ITEM_FLAG_DELETED))\n                {\n                    if (memcmp(buf,si->end_key,sizeof(streamID)) > 0)\n                        return 0; /* We are already out of range. */\n                    si->entry_flags = flags;\n                    if (flags & STREAM_ITEM_FLAG_SAMEFIELDS)\n                        si->master_fields_ptr = si->master_fields_start;\n                    return 1; /* Valid item returned. */\n                }\n            } else {\n                if (memcmp(buf,si->end_key,sizeof(streamID)) <= 0 &&\n                    !(flags & STREAM_ITEM_FLAG_DELETED))\n                {\n                    if (memcmp(buf,si->start_key,sizeof(streamID)) < 0)\n                        return 0; /* We are already out of range. */\n                    si->entry_flags = flags;\n                    if (flags & STREAM_ITEM_FLAG_SAMEFIELDS)\n                        si->master_fields_ptr = si->master_fields_start;\n                    return 1; /* Valid item returned. */\n                }\n            }\n\n            /* If we do not emit, we have to discard if we are going\n             * forward, or seek the previous entry if we are going\n             * backward. */\n            if (!si->rev) {\n                int64_t to_discard = (flags & STREAM_ITEM_FLAG_SAMEFIELDS) ?\n                                      *numfields : *numfields*2;\n                for (int64_t i = 0; i < to_discard; i++)\n                    si->lp_ele = lpNext(si->lp,si->lp_ele);\n            } else {\n                int64_t prev_times = 4; /* flag + id ms + id seq + one more to\n                                           go back to the previous entry \"count\"\n                                           field. */\n                /* If the entry was not flagged SAMEFIELD we also read the\n                 * number of fields, so go back one more. */\n                if (!(flags & STREAM_ITEM_FLAG_SAMEFIELDS)) prev_times++;\n                while(prev_times--) si->lp_ele = lpPrev(si->lp,si->lp_ele);\n            }\n        }\n\n        /* End of listpack reached. Try the next/prev radix tree node. */\n    }\n}\n\n/* Get the field and value of the current item we are iterating. This should\n * be called immediately after streamIteratorGetID(), and for each field\n * according to the number of fields returned by streamIteratorGetID().\n * The function populates the field and value pointers and the corresponding\n * lengths by reference, that are valid until the next iterator call, assuming\n * no one touches the stream meanwhile. */\nvoid streamIteratorGetField(streamIterator *si, unsigned char **fieldptr, unsigned char **valueptr, int64_t *fieldlen, int64_t *valuelen) {\n    if (si->entry_flags & STREAM_ITEM_FLAG_SAMEFIELDS) {\n        *fieldptr = lpGet(si->master_fields_ptr,fieldlen,si->field_buf);\n        si->master_fields_ptr = lpNext(si->lp,si->master_fields_ptr);\n    } else {\n        *fieldptr = lpGet(si->lp_ele,fieldlen,si->field_buf);\n        si->lp_ele = lpNext(si->lp,si->lp_ele);\n    }\n    *valueptr = lpGet(si->lp_ele,valuelen,si->value_buf);\n    si->lp_ele = lpNext(si->lp,si->lp_ele);\n}\n\n/* Remove the current entry from the stream: can be called after the\n * GetID() API or after any GetField() call, however we need to iterate\n * a valid entry while calling this function. Moreover the function\n * requires the entry ID we are currently iterating, that was previously\n * returned by GetID().\n *\n * Note that after calling this function, next calls to GetField() can't\n * be performed: the entry is now deleted. Instead the iterator will\n * automatically re-seek to the next entry, so the caller should continue\n * with GetID(). */\nvoid streamIteratorRemoveEntry(streamIterator *si, streamID *current) {\n    unsigned char *lp = si->lp;\n    int64_t aux;\n\n    /* We do not really delete the entry here. Instead we mark it as\n     * deleted flagging it, and also incrementing the count of the\n     * deleted entries in the listpack header.\n     *\n     * We start flagging: */\n    int64_t flags = lpGetInteger(si->lp_flags);\n    flags |= STREAM_ITEM_FLAG_DELETED;\n    lp = lpReplaceInteger(lp,&si->lp_flags,flags);\n\n    /* Change the valid/deleted entries count in the master entry. */\n    unsigned char *p = lpFirst(lp);\n    aux = lpGetInteger(p);\n\n    if (aux == 1) {\n        /* If this is the last element in the listpack, we can remove the whole\n         * node. */\n        lpFree(lp);\n        raxRemove(si->pstream->rax,si->ri.key,si->ri.key_len,NULL);\n    } else {\n        /* In the base case we alter the counters of valid/deleted entries. */\n        lp = lpReplaceInteger(lp,&p,aux-1);\n        p = lpNext(lp,p); /* Seek deleted field. */\n        aux = lpGetInteger(p);\n        lp = lpReplaceInteger(lp,&p,aux+1);\n\n        /* Update the listpack with the new pointer. */\n        if (si->lp != lp)\n            raxInsert(si->pstream->rax,si->ri.key,si->ri.key_len,lp,NULL);\n    }\n\n    /* Update the number of entries counter. */\n    si->pstream->length--;\n\n    /* Re-seek the iterator to fix the now messed up state. */\n    streamID start, end;\n    if (si->rev) {\n        streamDecodeID(si->start_key,&start);\n        end = *current;\n    } else {\n        start = *current;\n        streamDecodeID(si->end_key,&end);\n    }\n    streamIteratorStop(si);\n    streamIteratorStart(si,si->pstream,&start,&end,si->rev);\n\n    /* TODO: perform a garbage collection here if the ration between\n     * deleted and valid goes over a certain limit. */\n}\n\n/* Stop the stream iterator. The only cleanup we need is to free the rax\n * iterator, since the stream iterator itself is supposed to be stack\n * allocated. */\nvoid streamIteratorStop(streamIterator *si) {\n    raxStop(&si->ri);\n}\n\n/* Delete the specified item ID from the stream, returning 1 if the item\n * was deleted 0 otherwise (if it does not exist). */\nint streamDeleteItem(stream *s, streamID *id) {\n    int deleted = 0;\n    streamIterator si;\n    streamIteratorStart(&si,s,id,id,0);\n    streamID myid;\n    int64_t numfields;\n    if (streamIteratorGetID(&si,&myid,&numfields)) {\n        streamIteratorRemoveEntry(&si,&myid);\n        deleted = 1;\n    }\n    streamIteratorStop(&si);\n    return deleted;\n}\n\n/* Get the last valid (non-tombstone) streamID of 's'. */\nvoid streamLastValidID(stream *s, streamID *maxid)\n{\n    streamIterator si;\n    streamIteratorStart(&si,s,NULL,NULL,1);\n    int64_t numfields;\n    if (!streamIteratorGetID(&si,maxid,&numfields) && s->length)\n        serverPanic(\"Corrupt stream, length is %llu, but no max id\", (unsigned long long)s->length);\n    streamIteratorStop(&si);\n}\n\n/* Emit a reply in the client output buffer by formatting a Stream ID\n * in the standard <ms>-<seq> format, using the simple string protocol\n * of REPL. */\nstatic void addReplyStreamID(client *c, streamID *id) {\n    sds replyid = sdscatfmt(sdsempty(),\"%U-%U\",id->ms,id->seq);\n    addReplyBulkSds(c,replyid);\n}\n\nvoid setDeferredReplyStreamID(client *c, void *dr, streamID *id) {\n    sds replyid = sdscatfmt(sdsempty(),\"%U-%U\",id->ms,id->seq);\n    setDeferredReplyBulkSds(c, dr, replyid);\n}\n\n/* Similar to the above function, but just creates an object, usually useful\n * for replication purposes to create arguments. */\nrobj *createObjectFromStreamID(streamID *id) {\n    return createObject(OBJ_STRING, sdscatfmt(sdsempty(),\"%U-%U\",\n                        id->ms,id->seq));\n}\n\n/* As a result of an explicit XCLAIM or XREADGROUP command, new entries\n * are created in the pending list of the stream and consumers. We need\n * to propagate this changes in the form of XCLAIM commands. */\nvoid streamPropagateXCLAIM(client *c, robj *key, streamCG *group, robj *groupname, robj *id, streamNACK *nack) {\n    /* We need to generate an XCLAIM that will work in a idempotent fashion:\n     *\n     * XCLAIM <key> <group> <consumer> 0 <id> TIME <milliseconds-unix-time>\n     *        RETRYCOUNT <count> FORCE JUSTID LASTID <id>.\n     *\n     * Note that JUSTID is useful in order to avoid that XCLAIM will do\n     * useless work in the replica side, trying to fetch the stream item. */\n    if (FInReplicaReplay())\n        return;\n\n    robj *argv[14];\n    argv[0] = shared.xclaim;\n    argv[1] = key;\n    argv[2] = groupname;\n    argv[3] = createStringObject(nack->consumer->name,sdslen(nack->consumer->name));\n    argv[4] = shared.integers[0];\n    argv[5] = id;\n    argv[6] = shared.time;\n    argv[7] = createStringObjectFromLongLong(nack->delivery_time);\n    argv[8] = shared.retrycount;\n    argv[9] = createStringObjectFromLongLong(nack->delivery_count);\n    argv[10] = shared.force;\n    argv[11] = shared.justid;\n    argv[12] = shared.lastid;\n    argv[13] = createObjectFromStreamID(&group->last_id);\n\n    /* We use progagate() because this code path is not always called from\n     * the command execution context. Moreover this will just alter the\n     * consumer group state, and we don't need MULTI/EXEC wrapping because\n     * there is no message state cross-message atomicity required. */\n    propagate(cserver.xclaimCommand,c->db->id,argv,14,PROPAGATE_AOF|PROPAGATE_REPL);\n    decrRefCount(argv[3]);\n    decrRefCount(argv[7]);\n    decrRefCount(argv[9]);\n    decrRefCount(argv[13]);\n}\n\n/* We need this when we want to propoagate the new last-id of a consumer group\n * that was consumed by XREADGROUP with the NOACK option: in that case we can't\n * propagate the last ID just using the XCLAIM LASTID option, so we emit\n *\n *  XGROUP SETID <key> <groupname> <id>\n */\nvoid streamPropagateGroupID(client *c, robj *key, streamCG *group, robj *groupname) {\n    robj *argv[5];\n    argv[0] = shared.xgroup;\n    argv[1] = shared.setid;\n    argv[2] = key;\n    argv[3] = groupname;\n    argv[4] = createObjectFromStreamID(&group->last_id);\n\n    /* We use progagate() because this code path is not always called from\n     * the command execution context. Moreover this will just alter the\n     * consumer group state, and we don't need MULTI/EXEC wrapping because\n     * there is no message state cross-message atomicity required. */\n    propagate(cserver.xgroupCommand,c->db->id,argv,5,PROPAGATE_AOF|PROPAGATE_REPL);\n    decrRefCount(argv[4]);\n}\n\n/* We need this when we want to propagate creation of consumer that was created\n * by XREADGROUP with the NOACK option. In that case, the only way to create\n * the consumer at the replica is by using XGROUP CREATECONSUMER (see issue #7140)\n *\n *  XGROUP CREATECONSUMER <key> <groupname> <consumername>\n */\nvoid streamPropagateConsumerCreation(client *c, robj *key, robj *groupname, sds consumername) {\n    robj *argv[5];\n    argv[0] = shared.xgroup;\n    argv[1] = shared.createconsumer;\n    argv[2] = key;\n    argv[3] = groupname;\n    argv[4] = createObject(OBJ_STRING,sdsdup(consumername));\n\n    /* We use progagate() because this code path is not always called from\n     * the command execution context. Moreover this will just alter the\n     * consumer group state, and we don't need MULTI/EXEC wrapping because\n     * there is no message state cross-message atomicity required. */\n    propagate(cserver.xgroupCommand,c->db->id,argv,5,PROPAGATE_AOF|PROPAGATE_REPL);\n    decrRefCount(argv[4]);\n}\n\n/* Send the stream items in the specified range to the client 'c'. The range\n * the client will receive is between start and end inclusive, if 'count' is\n * non zero, no more than 'count' elements are sent.\n *\n * The 'end' pointer can be NULL to mean that we want all the elements from\n * 'start' till the end of the stream. If 'rev' is non zero, elements are\n * produced in reversed order from end to start.\n *\n * The function returns the number of entries emitted.\n *\n * If group and consumer are not NULL, the function performs additional work:\n * 1. It updates the last delivered ID in the group in case we are\n *    sending IDs greater than the current last ID.\n * 2. If the requested IDs are already assigned to some other consumer, the\n *    function will not return it to the client.\n * 3. An entry in the pending list will be created for every entry delivered\n *    for the first time to this consumer.\n *\n * The behavior may be modified passing non-zero flags:\n *\n * STREAM_RWR_NOACK: Do not create PEL entries, that is, the point \"3\" above\n *                   is not performed.\n * STREAM_RWR_RAWENTRIES: Do not emit array boundaries, but just the entries,\n *                        and return the number of entries emitted as usually.\n *                        This is used when the function is just used in order\n *                        to emit data and there is some higher level logic.\n *\n * The final argument 'spi' (stream propagation info pointer) is a structure\n * filled with information needed to propagate the command execution to AOF\n * and slaves, in the case a consumer group was passed: we need to generate\n * XCLAIM commands to create the pending list into AOF/slaves in that case.\n *\n * If 'spi' is set to NULL no propagation will happen even if the group was\n * given, but currently such a feature is never used by the code base that\n * will always pass 'spi' and propagate when a group is passed.\n *\n * Note that this function is recursive in certain cases. When it's called\n * with a non NULL group and consumer argument, it may call\n * streamReplyWithRangeFromConsumerPEL() in order to get entries from the\n * consumer pending entries list. However such a function will then call\n * streamReplyWithRange() in order to emit single entries (found in the\n * PEL by ID) to the client. This is the use case for the STREAM_RWR_RAWENTRIES\n * flag.\n */\n#define STREAM_RWR_NOACK (1<<0)         /* Do not create entries in the PEL. */\n#define STREAM_RWR_RAWENTRIES (1<<1)    /* Do not emit protocol for array\n                                           boundaries, just the entries. */\n#define STREAM_RWR_HISTORY (1<<2)       /* Only serve consumer local PEL. */\nsize_t streamReplyWithRange(client *c, stream *s, streamID *start, streamID *end, size_t count, int rev, streamCG *group, streamConsumer *consumer, int flags, streamPropInfo *spi) {\n    void *arraylen_ptr = NULL;\n    size_t arraylen = 0;\n    streamIterator si;\n    int64_t numfields;\n    streamID id;\n    int propagate_last_id = 0;\n    int noack = flags & STREAM_RWR_NOACK;\n\n    /* If the client is asking for some history, we serve it using a\n     * different function, so that we return entries *solely* from its\n     * own PEL. This ensures each consumer will always and only see\n     * the history of messages delivered to it and not yet confirmed\n     * as delivered. */\n    if (group && (flags & STREAM_RWR_HISTORY)) {\n        return streamReplyWithRangeFromConsumerPEL(c,s,start,end,count,\n                                                   consumer);\n    }\n\n    if (!(flags & STREAM_RWR_RAWENTRIES))\n        arraylen_ptr = addReplyDeferredLen(c);\n    streamIteratorStart(&si,s,start,end,rev);\n    while(streamIteratorGetID(&si,&id,&numfields)) {\n        /* Update the group last_id if needed. */\n        if (group && streamCompareID(&id,&group->last_id) > 0) {\n            group->last_id = id;\n            /* Group last ID should be propagated only if NOACK was\n             * specified, otherwise the last id will be included\n             * in the propagation of XCLAIM itself. */\n            if (noack) propagate_last_id = 1;\n        }\n\n        /* Emit a two elements array for each item. The first is\n         * the ID, the second is an array of field-value pairs. */\n        addReplyArrayLen(c,2);\n        addReplyStreamID(c,&id);\n\n        addReplyArrayLen(c,numfields*2);\n\n        /* Emit the field-value pairs. */\n        while(numfields--) {\n            unsigned char *key, *value;\n            int64_t key_len, value_len;\n            streamIteratorGetField(&si,&key,&value,&key_len,&value_len);\n            addReplyBulkCBuffer(c,key,key_len);\n            addReplyBulkCBuffer(c,value,value_len);\n        }\n\n        /* If a group is passed, we need to create an entry in the\n         * PEL (pending entries list) of this group *and* this consumer.\n         *\n         * Note that we cannot be sure about the fact the message is not\n         * already owned by another consumer, because the admin is able\n         * to change the consumer group last delivered ID using the\n         * XGROUP SETID command. So if we find that there is already\n         * a NACK for the entry, we need to associate it to the new\n         * consumer. */\n        if (group && !noack) {\n            unsigned char buf[sizeof(streamID)];\n            streamEncodeID(buf,&id);\n\n            /* Try to add a new NACK. Most of the time this will work and\n             * will not require extra lookups. We'll fix the problem later\n             * if we find that there is already a entry for this ID. */\n            streamNACK *nack = streamCreateNACK(consumer);\n            int group_inserted =\n                raxTryInsert(group->pel,buf,sizeof(buf),nack,NULL);\n            int consumer_inserted =\n                raxTryInsert(consumer->pel,buf,sizeof(buf),nack,NULL);\n\n            /* Now we can check if the entry was already busy, and\n             * in that case reassign the entry to the new consumer,\n             * or update it if the consumer is the same as before. */\n            if (group_inserted == 0) {\n                streamFreeNACK(nack);\n                nack = (streamNACK*)raxFind(group->pel,buf,sizeof(buf));\n                serverAssert(nack != raxNotFound);\n                raxRemove(nack->consumer->pel,buf,sizeof(buf),NULL);\n                /* Update the consumer and NACK metadata. */\n                nack->consumer = consumer;\n                nack->delivery_time = mstime();\n                nack->delivery_count = 1;\n                /* Add the entry in the new consumer local PEL. */\n                raxInsert(consumer->pel,buf,sizeof(buf),nack,NULL);\n            } else if (group_inserted == 1 && consumer_inserted == 0) {\n                serverPanic(\"NACK half-created. Should not be possible.\");\n            }\n\n            /* Propagate as XCLAIM. */\n            if (spi) {\n                robj *idarg = createObjectFromStreamID(&id);\n                streamPropagateXCLAIM(c,spi->keyname,group,spi->groupname,idarg,nack);\n                decrRefCount(idarg);\n            }\n        }\n\n        arraylen++;\n        if (count && count == arraylen) break;\n    }\n\n    if (spi && propagate_last_id)\n        streamPropagateGroupID(c,spi->keyname,group,spi->groupname);\n\n    streamIteratorStop(&si);\n    if (arraylen_ptr) setDeferredArrayLen(c,arraylen_ptr,arraylen);\n    return arraylen;\n}\n\n/* This is an helper function for streamReplyWithRange() when called with\n * group and consumer arguments, but with a range that is referring to already\n * delivered messages. In this case we just emit messages that are already\n * in the history of the consumer, fetching the IDs from its PEL.\n *\n * Note that this function does not have a 'rev' argument because it's not\n * possible to iterate in reverse using a group. Basically this function\n * is only called as a result of the XREADGROUP command.\n *\n * This function is more expensive because it needs to inspect the PEL and then\n * seek into the radix tree of the messages in order to emit the full message\n * to the client. However clients only reach this code path when they are\n * fetching the history of already retrieved messages, which is rare. */\nsize_t streamReplyWithRangeFromConsumerPEL(client *c, stream *s, streamID *start, streamID *end, size_t count, streamConsumer *consumer) {\n    raxIterator ri;\n    unsigned char startkey[sizeof(streamID)];\n    unsigned char endkey[sizeof(streamID)];\n    streamEncodeID(startkey,start);\n    if (end) streamEncodeID(endkey,end);\n\n    size_t arraylen = 0;\n    void *arraylen_ptr = addReplyDeferredLen(c);\n    raxStart(&ri,consumer->pel);\n    raxSeek(&ri,\">=\",startkey,sizeof(startkey));\n    while(raxNext(&ri) && (!count || arraylen < count)) {\n        if (end && memcmp(ri.key,end,ri.key_len) > 0) break;\n        streamID thisid;\n        streamDecodeID(ri.key,&thisid);\n        if (streamReplyWithRange(c,s,&thisid,&thisid,1,0,NULL,NULL,\n                                 STREAM_RWR_RAWENTRIES,NULL) == 0)\n        {\n            /* Note that we may have a not acknowledged entry in the PEL\n             * about a message that's no longer here because was removed\n             * by the user by other means. In that case we signal it emitting\n             * the ID but then a NULL entry for the fields. */\n            addReplyArrayLen(c,2);\n            addReplyStreamID(c,&thisid);\n            addReplyNullArray(c);\n        } else {\n            streamNACK *nack = (streamNACK*)ri.data;\n            nack->delivery_time = mstime();\n            nack->delivery_count++;\n        }\n        arraylen++;\n    }\n    raxStop(&ri);\n    setDeferredArrayLen(c,arraylen_ptr,arraylen);\n    return arraylen;\n}\n\n/* -----------------------------------------------------------------------\n * Stream commands implementation\n * ----------------------------------------------------------------------- */\n\n/* Look the stream at 'key' and return the corresponding stream object.\n * The function creates a key setting it to an empty stream if needed. */\nrobj *streamTypeLookupWriteOrCreate(client *c, robj *key, int no_create) {\n    robj *o = lookupKeyWrite(c->db,key);\n    if (checkType(c,o,OBJ_STREAM)) return NULL;\n    if (o == NULL) {\n        if (no_create) {\n            addReplyNull(c);\n            return NULL;\n        }\n        o = createStreamObject();\n        dbAdd(c->db,key,o);\n    }\n    return o;\n}\n\n/* Parse a stream ID in the format given by clients to Redis, that is\n * <ms>-<seq>, and converts it into a streamID structure. If\n * the specified ID is invalid C_ERR is returned and an error is reported\n * to the client, otherwise C_OK is returned. The ID may be in incomplete\n * form, just stating the milliseconds time part of the stream. In such a case\n * the missing part is set according to the value of 'missing_seq' parameter.\n *\n * The IDs \"-\" and \"+\" specify respectively the minimum and maximum IDs\n * that can be represented. If 'strict' is set to 1, \"-\" and \"+\" will be\n * treated as an invalid ID.\n *\n * If 'c' is set to NULL, no reply is sent to the client. */\nint streamGenericParseIDOrReply(client *c, const robj *o, streamID *id, uint64_t missing_seq, int strict) {\n    char buf[128];\n    char *dot = nullptr;\n    if (sdslen(szFromObj(o)) > sizeof(buf)-1) goto invalid;\n    memcpy(buf,ptrFromObj(o),sdslen(szFromObj(o))+1);\n\n    if (strict && (buf[0] == '-' || buf[0] == '+') && buf[1] == '\\0')\n        goto invalid;\n\n    /* Handle the \"-\" and \"+\" special cases. */\n    if (buf[0] == '-' && buf[1] == '\\0') {\n        id->ms = 0;\n        id->seq = 0;\n        return C_OK;\n    } else if (buf[0] == '+' && buf[1] == '\\0') {\n        id->ms = UINT64_MAX;\n        id->seq = UINT64_MAX;\n        return C_OK;\n    }\n\n    /* Parse <ms>-<seq> form. */\n    dot = strchr(buf,'-');\n    if (dot) *dot = '\\0';\n    unsigned long long ms, seq;\n    if (string2ull(buf,&ms) == 0) goto invalid;\n    if (dot && string2ull(dot+1,&seq) == 0) goto invalid;\n    if (!dot) seq = missing_seq;\n    id->ms = ms;\n    id->seq = seq;\n    return C_OK;\n\ninvalid:\n    if (c) addReplyError(c,\"Invalid stream ID specified as stream \"\n                           \"command argument\");\n    return C_ERR;\n}\n\n/* Wrapper for streamGenericParseIDOrReply() used by module API. */\nint streamParseID(const robj *o, streamID *id) {\n    return streamGenericParseIDOrReply(NULL, o, id, 0, 0);\n}\n\n/* Wrapper for streamGenericParseIDOrReply() with 'strict' argument set to\n * 0, to be used when - and + are acceptable IDs. */\nint streamParseIDOrReply(client *c, robj *o, streamID *id, uint64_t missing_seq) {\n    return streamGenericParseIDOrReply(c,o,id,missing_seq,0);\n}\n\n/* Wrapper for streamGenericParseIDOrReply() with 'strict' argument set to\n * 1, to be used when we want to return an error if the special IDs + or -\n * are provided. */\nint streamParseStrictIDOrReply(client *c, robj *o, streamID *id, uint64_t missing_seq) {\n    return streamGenericParseIDOrReply(c,o,id,missing_seq,1);\n}\n\n/* Helper for parsing a stream ID that is a range query interval. When the\n * exclude argument is NULL, streamParseIDOrReply() is called and the interval\n * is treated as close (inclusive). Otherwise, the exclude argument is set if \n * the interval is open (the \"(\" prefix) and streamParseStrictIDOrReply() is\n * called in that case.\n */\nint streamParseIntervalIDOrReply(client *c, robj *o, streamID *id, int *exclude, uint64_t missing_seq) {\n    char *p = szFromObj(o);\n    size_t len = sdslen(p);\n    int invalid = 0;\n    \n    if (exclude != NULL) *exclude = (len > 1 && p[0] == '(');\n    if (exclude != NULL && *exclude) {\n        robj *t = createStringObject(p+1,len-1);\n        invalid = (streamParseStrictIDOrReply(c,t,id,missing_seq) == C_ERR);\n        decrRefCount(t);\n    } else \n        invalid = (streamParseIDOrReply(c,o,id,missing_seq) == C_ERR);\n    if (invalid)\n        return C_ERR;\n    return C_OK;\n}\n\nvoid streamRewriteApproxSpecifier(client *c, int idx) {\n    rewriteClientCommandArgument(c,idx,shared.special_equals);\n}\n\n/* We propagate MAXLEN/MINID ~ <count> as MAXLEN/MINID = <resulting-len-of-stream>\n * otherwise trimming is no longer deterministic on replicas / AOF. */\nvoid streamRewriteTrimArgument(client *c, stream *s, int trim_strategy, int idx) {\n    robj *arg;\n    if (trim_strategy == TRIM_STRATEGY_MAXLEN) {\n        arg = createStringObjectFromLongLong(s->length);\n    } else {\n        streamID first_id;\n        streamGetEdgeID(s, 1, &first_id);\n        arg = createObjectFromStreamID(&first_id);\n    }\n\n    rewriteClientCommandArgument(c,idx,arg);\n    decrRefCount(arg);\n}\n\n/* XADD key [(MAXLEN [~|=] <count> | MINID [~|=] <id>) [LIMIT <entries>]] [NOMKSTREAM] <ID or *> [field value] [field value] ... */\nvoid xaddCommand(client *c) {\n    /* Parse options. */\n    streamAddTrimArgs parsed_args;\n    int idpos = streamParseAddOrTrimArgsOrReply(c, &parsed_args, 1);\n    if (idpos < 0)\n        return; /* streamParseAddOrTrimArgsOrReply already replied. */\n    int field_pos = idpos+1; /* The ID is always one argument before the first field */\n\n    /* Check arity. */\n    if ((c->argc - field_pos) < 2 || ((c->argc-field_pos) % 2) == 1) {\n        addReplyError(c,\"wrong number of arguments for XADD\");\n        return;\n    }\n\n    /* Return ASAP if minimal ID (0-0) was given so we avoid possibly creating\n     * a new stream and have streamAppendItem fail, leaving an empty key in the\n     * database. */\n    if (parsed_args.id_given &&\n        parsed_args.id.ms == 0 && parsed_args.id.seq == 0)\n    {\n        addReplyError(c,\"The ID specified in XADD must be greater than 0-0\");\n        return;\n    }\n\n    /* Lookup the stream at key. */\n    robj *o;\n    stream *s;\n    if ((o = streamTypeLookupWriteOrCreate(c,c->argv[1],parsed_args.no_mkstream)) == NULL) return;\n    s = (stream*)ptrFromObj(o);\n\n    /* Return ASAP if the stream has reached the last possible ID */\n    if (s->last_id.ms == UINT64_MAX && s->last_id.seq == UINT64_MAX) {\n        addReplyError(c,\"The stream has exhausted the last possible ID, \"\n                        \"unable to add more items\");\n        return;\n    }\n\n    /* Append using the low level function and return the ID. */\n    streamID id;\n    if (streamAppendItem(s,c->argv+field_pos,(c->argc-field_pos)/2,\n        &id, parsed_args.id_given ? &parsed_args.id : NULL) == C_ERR)\n    {\n        if (errno == EDOM)\n            addReplyError(c,\"The ID specified in XADD is equal or smaller than \"\n                            \"the target stream top item\");\n        else\n            addReplyError(c,\"Elements are too large to be stored\");\n        return;\n    }\n    addReplyStreamID(c,&id);\n\n    signalModifiedKey(c,c->db,c->argv[1]);\n    notifyKeyspaceEvent(NOTIFY_STREAM,\"xadd\",c->argv[1],c->db->id);\n    g_pserver->dirty++;\n\n    /* Trim if needed. */\n    if (parsed_args.trim_strategy != TRIM_STRATEGY_NONE) {\n        if (streamTrim(s, &parsed_args)) {\n            notifyKeyspaceEvent(NOTIFY_STREAM,\"xtrim\",c->argv[1],c->db->id);\n        }\n        if (parsed_args.approx_trim) {\n            /* In case our trimming was limited (by LIMIT or by ~) we must\n             * re-write the relevant trim argument to make sure there will be\n             * no inconsistencies in AOF loading or in the replica.\n             * It's enough to check only args->approx because there is no\n             * way LIMIT is given without the ~ option. */\n            streamRewriteApproxSpecifier(c,parsed_args.trim_strategy_arg_idx-1);\n            streamRewriteTrimArgument(c,s,parsed_args.trim_strategy,parsed_args.trim_strategy_arg_idx);\n        }\n    }\n\n    /* Let's rewrite the ID argument with the one actually generated for\n     * AOF/replication propagation. */\n    robj *idarg = createObjectFromStreamID(&id);\n    rewriteClientCommandArgument(c,idpos,idarg);\n    decrRefCount(idarg);\n\n    /* We need to signal to blocked clients that there is new data on this\n     * stream. */\n    signalKeyAsReady(c->db, c->argv[1], OBJ_STREAM);\n}\n\n/* XRANGE/XREVRANGE actual implementation.\n * The 'start' and 'end' IDs are parsed as follows:\n *   Incomplete 'start' has its sequence set to 0, and 'end' to UINT64_MAX.\n *   \"-\" and \"+\"\" mean the minimal and maximal ID values, respectively.\n *   The \"(\" prefix means an open (exclusive) range, so XRANGE stream (1-0 (2-0\n *   will match anything from 1-1 and 1-UINT64_MAX.\n */\nvoid xrangeGenericCommand(client *c, int rev) {\n    robj_roptr o;\n    stream *s;\n    streamID startid, endid;\n    long long count = -1;\n    robj *startarg = rev ? c->argv[3] : c->argv[2];\n    robj *endarg = rev ? c->argv[2] : c->argv[3];\n    int startex = 0, endex = 0;\n    \n    /* Parse start and end IDs. */\n    if (streamParseIntervalIDOrReply(c,startarg,&startid,&startex,0) != C_OK)\n        return;\n    if (startex && streamIncrID(&startid) != C_OK) {\n        addReplyError(c,\"invalid start ID for the interval\");\n        return;\n    }\n    if (streamParseIntervalIDOrReply(c,endarg,&endid,&endex,UINT64_MAX) != C_OK)\n        return;\n    if (endex && streamDecrID(&endid) != C_OK) {\n        addReplyError(c,\"invalid end ID for the interval\");\n        return;\n    }\n\n    /* Parse the COUNT option if any. */\n    if (c->argc > 4) {\n        for (int j = 4; j < c->argc; j++) {\n            int additional = c->argc-j-1;\n            if (strcasecmp(szFromObj(c->argv[j]),\"COUNT\") == 0 && additional >= 1) {\n                if (getLongLongFromObjectOrReply(c,c->argv[j+1],&count,NULL)\n                    != C_OK) return;\n                if (count < 0) count = 0;\n                j++; /* Consume additional arg. */\n            } else {\n                addReplyErrorObject(c,shared.syntaxerr);\n                return;\n            }\n        }\n    }\n\n    /* Return the specified range to the user. */\n    if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptyarray)) == nullptr ||\n         checkType(c,o,OBJ_STREAM)) return;\n\n    s = (stream*)ptrFromObj(o);\n\n    if (count == 0) {\n        addReplyNullArray(c);\n    } else {\n        if (count == -1) count = 0;\n        streamReplyWithRange(c,s,&startid,&endid,count,rev,NULL,NULL,0,NULL);\n    }\n}\n\n/* XRANGE key start end [COUNT <n>] */\nvoid xrangeCommand(client *c) {\n    xrangeGenericCommand(c,0);\n}\n\n/* XREVRANGE key end start [COUNT <n>] */\nvoid xrevrangeCommand(client *c) {\n    xrangeGenericCommand(c,1);\n}\n\n/* XLEN */\nvoid xlenCommand(client *c) {\n    robj_roptr o;\n    if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == nullptr\n        || checkType(c,o,OBJ_STREAM)) return;\n    stream *s = (stream*)ptrFromObj(o);\n    addReplyLongLong(c,s->length);\n}\n\n/* XREAD [BLOCK <milliseconds>] [COUNT <count>] STREAMS key_1 key_2 ... key_N\n *       ID_1 ID_2 ... ID_N\n *\n * This function also implements the XREAD-GROUP command, which is like XREAD\n * but accepting the [GROUP group-name consumer-name] additional option.\n * This is useful because while XREAD is a read command and can be called\n * on slaves, XREAD-GROUP is not. */\n#define XREAD_BLOCKED_DEFAULT_COUNT 1000\nvoid xreadCommand(client *c) {\n    long long timeout = -1; /* -1 means, no BLOCK argument given. */\n    long long count = 0;\n    int streams_count = 0;\n    int streams_arg = 0;\n    int noack = 0;          /* True if NOACK option was specified. */\n    streamID static_ids[STREAMID_STATIC_VECTOR_LEN];\n    streamID *ids = static_ids;\n    streamCG **groups = NULL;\n    int xreadgroup = sdslen(szFromObj(c->argv[0])) == 10; /* XREAD or XREADGROUP? */\n    robj *groupname = NULL;\n    robj *consumername = NULL;\n    size_t arraylen = 0;\n    void *arraylen_ptr = NULL;\n\n    /* Parse arguments. */\n    for (int i = 1; i < c->argc; i++) {\n        int moreargs = c->argc-i-1;\n        char *o = szFromObj(c->argv[i]);\n        if (!strcasecmp(o,\"BLOCK\") && moreargs) {\n            if (c->flags & CLIENT_LUA) {\n                /*\n                 * Although the CLIENT_DENY_BLOCKING flag should protect from blocking the client\n                 * on Lua/MULTI/RM_Call we want special treatment for Lua to keep backword compatibility.\n                 * There is no sense to use BLOCK option within Lua. */\n                addReplyErrorFormat(c, \"%s command is not allowed with BLOCK option from scripts\", szFromObj(c->argv[0]));\n                return;\n            }\n            i++;\n            if (getTimeoutFromObjectOrReply(c,c->argv[i],&timeout,\n                UNIT_MILLISECONDS) != C_OK) return;\n        } else if (!strcasecmp(o,\"COUNT\") && moreargs) {\n            i++;\n            if (getLongLongFromObjectOrReply(c,c->argv[i],&count,NULL) != C_OK)\n                return;\n            if (count < 0) count = 0;\n        } else if (!strcasecmp(o,\"STREAMS\") && moreargs) {\n            streams_arg = i+1;\n            streams_count = (c->argc-streams_arg);\n            if ((streams_count % 2) != 0) {\n                addReplyError(c,\"Unbalanced XREAD list of streams: \"\n                                \"for each stream key an ID or '$' must be \"\n                                \"specified.\");\n                return;\n            }\n            streams_count /= 2; /* We have two arguments for each stream. */\n            break;\n        } else if (!strcasecmp(o,\"GROUP\") && moreargs >= 2) {\n            if (!xreadgroup) {\n                addReplyError(c,\"The GROUP option is only supported by \"\n                                \"XREADGROUP. You called XREAD instead.\");\n                return;\n            }\n            groupname = c->argv[i+1];\n            consumername = c->argv[i+2];\n            i += 2;\n        } else if (!strcasecmp(o,\"NOACK\")) {\n            if (!xreadgroup) {\n                addReplyError(c,\"The NOACK option is only supported by \"\n                                \"XREADGROUP. You called XREAD instead.\");\n                return;\n            }\n            noack = 1;\n        } else {\n            addReplyErrorObject(c,shared.syntaxerr);\n            return;\n        }\n    }\n\n    /* STREAMS option is mandatory. */\n    if (streams_arg == 0) {\n        addReplyErrorObject(c,shared.syntaxerr);\n        return;\n    }\n\n    /* If the user specified XREADGROUP then it must also\n     * provide the GROUP option. */\n    if (xreadgroup && groupname == NULL) {\n        addReplyError(c,\"Missing GROUP option for XREADGROUP\");\n        return;\n    }\n\n    /* Parse the IDs and resolve the group name. */\n    if (streams_count > STREAMID_STATIC_VECTOR_LEN)\n        ids = (streamID*)zmalloc(sizeof(streamID)*streams_count, MALLOC_SHARED);\n    if (groupname) groups = (streamCG**)zmalloc(sizeof(streamCG*)*streams_count, MALLOC_SHARED);\n\n    for (int i = streams_arg + streams_count; i < c->argc; i++) {\n        /* Specifying \"$\" as last-known-id means that the client wants to be\n         * served with just the messages that will arrive into the stream\n         * starting from now. */\n        int id_idx = i - streams_arg - streams_count;\n        robj *key = c->argv[i-streams_count];\n        robj_roptr o = lookupKeyRead(c->db,key);\n        if (checkType(c,o,OBJ_STREAM)) goto cleanup;\n        streamCG *group = NULL;\n\n        /* If a group was specified, than we need to be sure that the\n         * key and group actually exist. */\n        if (groupname) {\n            if (o == nullptr ||\n                (group = streamLookupCG((stream*)ptrFromObj(o),szFromObj(groupname))) == NULL)\n            {\n                addReplyErrorFormat(c, \"-NOGROUP No such key '%s' or consumer \"\n                                       \"group '%s' in XREADGROUP with GROUP \"\n                                       \"option\",\n                                    (char*)ptrFromObj(key),(char*)ptrFromObj(groupname));\n                goto cleanup;\n            }\n            groups[id_idx] = group;\n        }\n\n        if (strcmp(szFromObj(c->argv[i]),\"$\") == 0) {\n            if (xreadgroup) {\n                addReplyError(c,\"The $ ID is meaningless in the context of \"\n                                \"XREADGROUP: you want to read the history of \"\n                                \"this consumer by specifying a proper ID, or \"\n                                \"use the > ID to get new messages. The $ ID would \"\n                                \"just return an empty result set.\");\n                goto cleanup;\n            }\n            if (o) {\n                stream *s = (stream*)ptrFromObj(o);\n                ids[id_idx] = s->last_id;\n            } else {\n                ids[id_idx].ms = 0;\n                ids[id_idx].seq = 0;\n            }\n            continue;\n        } else if (strcmp(szFromObj(c->argv[i]),\">\") == 0) {\n            if (!xreadgroup) {\n                addReplyError(c,\"The > ID can be specified only when calling \"\n                                \"XREADGROUP using the GROUP <group> \"\n                                \"<consumer> option.\");\n                goto cleanup;\n            }\n            /* We use just the maximum ID to signal this is a \">\" ID, anyway\n             * the code handling the blocking clients will have to update the\n             * ID later in order to match the changing consumer group last ID. */\n            ids[id_idx].ms = UINT64_MAX;\n            ids[id_idx].seq = UINT64_MAX;\n            continue;\n        }\n        if (streamParseStrictIDOrReply(c,c->argv[i],ids+id_idx,0) != C_OK)\n            goto cleanup;\n    }\n\n    /* Try to serve the client synchronously. */\n    for (int i = 0; i < streams_count; i++) {\n        robj_roptr o = lookupKeyRead(c->db,c->argv[streams_arg+i]);\n        if (o == nullptr) continue;\n        stream *s = (stream*)ptrFromObj(o);\n        streamID *gt = ids+i; /* ID must be greater than this. */\n        int serve_synchronously = 0;\n        int serve_history = 0; /* True for XREADGROUP with ID != \">\". */\n\n        /* Check if there are the conditions to serve the client\n         * synchronously. */\n        if (groups) {\n            /* If the consumer is blocked on a group, we always serve it\n             * synchronously (serving its local history) if the ID specified\n             * was not the special \">\" ID. */\n            if (gt->ms != UINT64_MAX ||\n                gt->seq != UINT64_MAX)\n            {\n                serve_synchronously = 1;\n                serve_history = 1;\n            } else if (s->length) {\n                /* We also want to serve a consumer in a consumer group\n                 * synchronously in case the group top item delivered is smaller\n                 * than what the stream has inside. */\n                streamID maxid, *last = &groups[i]->last_id;\n                streamLastValidID(s, &maxid);\n                if (streamCompareID(&maxid, last) > 0) {\n                    serve_synchronously = 1;\n                    *gt = *last;\n                }\n            }\n        } else if (s->length) {\n            /* For consumers without a group, we serve synchronously if we can\n             * actually provide at least one item from the stream. */\n            streamID maxid;\n            streamLastValidID(s, &maxid);\n            if (streamCompareID(&maxid, gt) > 0) {\n                serve_synchronously = 1;\n            }\n        }\n\n        if (serve_synchronously) {\n            arraylen++;\n            if (arraylen == 1) arraylen_ptr = addReplyDeferredLen(c);\n            /* streamReplyWithRange() handles the 'start' ID as inclusive,\n             * so start from the next ID, since we want only messages with\n             * IDs greater than start. */\n            streamID start = *gt;\n            streamIncrID(&start);\n\n            /* Emit the two elements sub-array consisting of the name\n             * of the stream and the data we extracted from it. */\n            if (c->resp == 2) addReplyArrayLen(c,2);\n            addReplyBulk(c,c->argv[streams_arg+i]);\n            int created = 0;\n            streamConsumer *consumer = NULL;\n            if (groups) consumer = streamLookupConsumer(groups[i],\n                                                        szFromObj(consumername),\n                                                        SLC_NONE,\n                                                        &created);\n            streamPropInfo spi = {c->argv[i+streams_arg],groupname};\n            if (created && noack)\n                streamPropagateConsumerCreation(c,spi.keyname,\n                                                spi.groupname,\n                                                consumer->name);\n            int flags = 0;\n            if (noack) flags |= STREAM_RWR_NOACK;\n            if (serve_history) flags |= STREAM_RWR_HISTORY;\n            streamReplyWithRange(c,s,&start,NULL,count,0,\n                                 groups ? groups[i] : NULL,\n                                 consumer, flags, &spi);\n            if (groups) g_pserver->dirty++;\n        }\n    }\n\n     /* We replied synchronously! Set the top array len and return to caller. */\n    if (arraylen) {\n        if (c->resp == 2)\n            setDeferredArrayLen(c,arraylen_ptr,arraylen);\n        else\n            setDeferredMapLen(c,arraylen_ptr,arraylen);\n        goto cleanup;\n    }\n\n    /* Block if needed. */\n    if (timeout != -1) {\n        /* If we are not allowed to block the client, the only thing\n         * we can do is treating it as a timeout (even with timeout 0). */\n        if (c->flags & CLIENT_DENY_BLOCKING) {\n            addReplyNullArray(c);\n            goto cleanup;\n        }\n        blockForKeys(c, BLOCKED_STREAM, c->argv+streams_arg, streams_count,\n                     timeout, NULL, NULL, ids);\n        /* If no COUNT is given and we block, set a relatively small count:\n         * in case the ID provided is too low, we do not want the server to\n         * block just to serve this client a huge stream of messages. */\n        c->bpop.xread_count = count ? count : XREAD_BLOCKED_DEFAULT_COUNT;\n\n        /* If this is a XREADGROUP + GROUP we need to remember for which\n         * group and consumer name we are blocking, so later when one of the\n         * keys receive more data, we can call streamReplyWithRange() passing\n         * the right arguments. */\n        if (groupname) {\n            incrRefCount(groupname);\n            incrRefCount(consumername);\n            c->bpop.xread_group = groupname;\n            c->bpop.xread_consumer = consumername;\n            c->bpop.xread_group_noack = noack;\n        } else {\n            c->bpop.xread_group = NULL;\n            c->bpop.xread_consumer = NULL;\n        }\n        goto cleanup;\n    }\n\n    /* No BLOCK option, nor any stream we can serve. Reply as with a\n     * timeout happened. */\n    addReplyNullArray(c);\n    /* Continue to cleanup... */\n\ncleanup: /* Cleanup. */\n\n    /* The command is propagated (in the READGROUP form) as a side effect\n     * of calling lower level APIs. So stop any implicit propagation. */\n    preventCommandPropagation(c);\n    if (ids != static_ids) zfree(ids);\n    zfree(groups);\n}\n\n/* -----------------------------------------------------------------------\n * Low level implementation of consumer groups\n * ----------------------------------------------------------------------- */\n\n/* Create a NACK entry setting the delivery count to 1 and the delivery\n * time to the current time. The NACK consumer will be set to the one\n * specified as argument of the function. */\nstreamNACK *streamCreateNACK(streamConsumer *consumer) {\n    streamNACK *nack = (streamNACK*)zmalloc(sizeof(*nack), MALLOC_SHARED);\n    nack->delivery_time = mstime();\n    nack->delivery_count = 1;\n    nack->consumer = consumer;\n    return nack;\n}\n\n/* Free a NACK entry. */\nvoid streamFreeNACK(streamNACK *na) {\n    zfree(na);\n}\n\n/* Free a consumer and associated data structures. Note that this function\n * will not reassign the pending messages associated with this consumer\n * nor will delete them from the stream, so when this function is called\n * to delete a consumer, and not when the whole stream is destroyed, the caller\n * should do some work before. */\nvoid streamFreeConsumer(streamConsumer *sc) {\n    raxFree(sc->pel); /* No value free callback: the PEL entries are shared\n                         between the consumer and the main stream PEL. */\n    sdsfree(sc->name);\n    zfree(sc);\n}\n\n/* Create a new consumer group in the context of the stream 's', having the\n * specified name and last server ID. If a consumer group with the same name\n * already existed NULL is returned, otherwise the pointer to the consumer\n * group is returned. */\nstreamCG *streamCreateCG(stream *s, char *name, size_t namelen, streamID *id) {\n    if (s->cgroups == NULL) s->cgroups = raxNew();\n    if (raxFind(s->cgroups,(unsigned char*)name,namelen) != raxNotFound)\n        return NULL;\n\n    streamCG *cg = (streamCG*)zmalloc(sizeof(*cg), MALLOC_SHARED);\n    cg->pel = raxNew();\n    cg->consumers = raxNew();\n    cg->last_id = *id;\n    raxInsert(s->cgroups,(unsigned char*)name,namelen,cg,NULL);\n    return cg;\n}\n\n/* Free a consumer group and all its associated data. */\nvoid streamFreeCG(streamCG *cg) {\n    raxFreeWithCallback(cg->pel,(void(*)(void*))streamFreeNACK);\n    raxFreeWithCallback(cg->consumers,(void(*)(void*))streamFreeConsumer);\n    zfree(cg);\n}\n\n/* Lookup the consumer group in the specified stream and returns its\n * pointer, otherwise if there is no such group, NULL is returned. */\nstreamCG *streamLookupCG(stream *s, sds groupname) {\n    if (s->cgroups == NULL) return NULL;\n    streamCG *cg = (streamCG*)raxFind(s->cgroups,(unsigned char*)groupname,\n                           sdslen(groupname));\n    return (cg == raxNotFound) ? NULL : cg;\n}\n\n/* Lookup the consumer with the specified name in the group 'cg': if the\n * consumer does not exist it is created unless SLC_NOCREAT flag was specified.\n * Its last seen time is updated unless SLC_NOREFRESH flag was specified. */\nstreamConsumer *streamLookupConsumer(streamCG *cg, sds name, int flags, int *created) {\n    if (created) *created = 0;\n    int create = !(flags & SLC_NOCREAT);\n    int refresh = !(flags & SLC_NOREFRESH);\n    streamConsumer *consumer = (streamConsumer*)raxFind(cg->consumers,(unsigned char*)name,\n                               sdslen(name));\n    if (consumer == raxNotFound) {\n        if (!create) return NULL;\n        consumer = (streamConsumer*)zmalloc(sizeof(*consumer), MALLOC_SHARED);\n        consumer->name = sdsdup(name);\n        consumer->pel = raxNew();\n        raxInsert(cg->consumers,(unsigned char*)name,sdslen(name),\n                  consumer,NULL);\n        consumer->seen_time = mstime();\n        if (created) *created = 1;\n    } else if (refresh)\n        consumer->seen_time = mstime();\n    return consumer;\n}\n\n/* Delete the consumer specified in the consumer group 'cg'. The consumer\n * may have pending messages: they are removed from the PEL, and the number\n * of pending messages \"lost\" is returned. */\nuint64_t streamDelConsumer(streamCG *cg, sds name) {\n    streamConsumer *consumer =\n        streamLookupConsumer(cg,name,SLC_NOCREAT|SLC_NOREFRESH,NULL);\n    if (consumer == NULL) return 0;\n\n    uint64_t retval = raxSize(consumer->pel);\n\n    /* Iterate all the consumer pending messages, deleting every corresponding\n     * entry from the global entry. */\n    raxIterator ri;\n    raxStart(&ri,consumer->pel);\n    raxSeek(&ri,\"^\",NULL,0);\n    while(raxNext(&ri)) {\n        streamNACK *nack = (streamNACK*)ri.data;\n        raxRemove(cg->pel,ri.key,ri.key_len,NULL);\n        streamFreeNACK(nack);\n    }\n    raxStop(&ri);\n\n    /* Deallocate the consumer. */\n    raxRemove(cg->consumers,(unsigned char*)name,sdslen(name),NULL);\n    streamFreeConsumer(consumer);\n    return retval;\n}\n\n/* -----------------------------------------------------------------------\n * Consumer groups commands\n * ----------------------------------------------------------------------- */\n\n/* XGROUP CREATE <key> <groupname> <id or $> [MKSTREAM]\n * XGROUP SETID <key> <groupname> <id or $>\n * XGROUP DESTROY <key> <groupname>\n * XGROUP CREATECONSUMER <key> <groupname> <consumer>\n * XGROUP DELCONSUMER <key> <groupname> <consumername> */\nvoid xgroupCommand(client *c) {\n    stream *s = NULL;\n    sds grpname = NULL;\n    streamCG *cg = NULL;\n    char *opt = szFromObj(c->argv[1]); /* Subcommand name. */\n    int mkstream = 0;\n    robj *o;\n\n    /* CREATE has an MKSTREAM option that creates the stream if it\n     * does not exist. */\n    if (c->argc == 6 && !strcasecmp(opt,\"CREATE\")) {\n        if (strcasecmp(szFromObj(c->argv[5]),\"MKSTREAM\")) {\n            addReplySubcommandSyntaxError(c);\n            return;\n        }\n        mkstream = 1;\n        grpname = szFromObj(c->argv[3]);\n    }\n\n    /* Everything but the \"HELP\" option requires a key and group name. */\n    if (c->argc >= 4) {\n        o = lookupKeyWrite(c->db,c->argv[2]);\n        if (o) {\n            if (checkType(c,o,OBJ_STREAM)) return;\n            s = (stream*)ptrFromObj(o);\n        }\n        grpname = szFromObj(c->argv[3]);\n    }\n\n    /* Check for missing key/group. */\n    if (c->argc >= 4 && !mkstream) {\n        /* At this point key must exist, or there is an error. */\n        if (s == NULL) {\n            addReplyError(c,\n                \"The XGROUP subcommand requires the key to exist. \"\n                \"Note that for CREATE you may want to use the MKSTREAM \"\n                \"option to create an empty stream automatically.\");\n            return;\n        }\n\n        /* Certain subcommands require the group to exist. */\n        if ((cg = streamLookupCG(s,grpname)) == NULL &&\n            (!strcasecmp(opt,\"SETID\") ||\n             !strcasecmp(opt,\"CREATECONSUMER\") ||\n             !strcasecmp(opt,\"DELCONSUMER\")))\n        {\n            addReplyErrorFormat(c, \"-NOGROUP No such consumer group '%s' \"\n                                   \"for key name '%s'\",\n                                   (char*)grpname, (char*)ptrFromObj(c->argv[2]));\n            return;\n        }\n    }\n\n    /* Dispatch the different subcommands. */\n    if (c->argc == 2 && !strcasecmp(opt,\"HELP\")) {\n        const char *help[] = {\n\"CREATE <key> <groupname> <id|$> [option]\",\n\"    Create a new consumer group. Options are:\",\n\"    * MKSTREAM\",\n\"      Create the empty stream if it does not exist.\",\n\"CREATECONSUMER <key> <groupname> <consumer>\",\n\"    Create a new consumer in the specified group.\",\n\"DELCONSUMER <key> <groupname> <consumer>\",\n\"    Remove the specified consumer.\",\n\"DESTROY <key> <groupname>\"\n\"    Remove the specified group.\",\n\"SETID <key> <groupname> <id|$>\",\n\"    Set the current group ID.\",\nNULL\n        };\n        addReplyHelp(c, help);\n    } else if (!strcasecmp(opt,\"CREATE\") && (c->argc == 5 || c->argc == 6)) {\n        streamID id;\n        if (!strcmp(szFromObj(c->argv[4]),\"$\")) {\n            if (s) {\n                id = s->last_id;\n            } else {\n                id.ms = 0;\n                id.seq = 0;\n            }\n        } else if (streamParseStrictIDOrReply(c,c->argv[4],&id,0) != C_OK) {\n            return;\n        }\n\n        /* Handle the MKSTREAM option now that the command can no longer fail. */\n        if (s == NULL) {\n            serverAssert(mkstream);\n            o = createStreamObject();\n            dbAdd(c->db,c->argv[2],o);\n            s = (stream*)ptrFromObj(o);\n            signalModifiedKey(c,c->db,c->argv[2]);\n        }\n\n        streamCG *cg = streamCreateCG(s,grpname,sdslen(grpname),&id);\n        if (cg) {\n            addReply(c,shared.ok);\n            g_pserver->dirty++;\n            notifyKeyspaceEvent(NOTIFY_STREAM,\"xgroup-create\",\n                                c->argv[2],c->db->id);\n        } else {\n            addReplyError(c,\"-BUSYGROUP Consumer Group name already exists\");\n        }\n    } else if (!strcasecmp(opt,\"SETID\") && c->argc == 5) {\n        streamID id;\n        if (!strcmp(szFromObj(c->argv[4]),\"$\")) {\n            id = s->last_id;\n        } else if (streamParseIDOrReply(c,c->argv[4],&id,0) != C_OK) {\n            return;\n        }\n        cg->last_id = id;\n        addReply(c,shared.ok);\n        g_pserver->dirty++;\n        notifyKeyspaceEvent(NOTIFY_STREAM,\"xgroup-setid\",c->argv[2],c->db->id);\n    } else if (!strcasecmp(opt,\"DESTROY\") && c->argc == 4) {\n        if (cg) {\n            raxRemove(s->cgroups,(unsigned char*)grpname,sdslen(grpname),NULL);\n            streamFreeCG(cg);\n            addReply(c,shared.cone);\n            g_pserver->dirty++;\n            notifyKeyspaceEvent(NOTIFY_STREAM,\"xgroup-destroy\",\n                                c->argv[2],c->db->id);\n            /* We want to unblock any XREADGROUP consumers with -NOGROUP. */\n            signalKeyAsReady(c->db,c->argv[2],OBJ_STREAM);\n        } else {\n            addReply(c,shared.czero);\n        }\n    } else if (!strcasecmp(opt,\"CREATECONSUMER\") && c->argc == 5) {\n        int created = 0;\n        streamLookupConsumer(cg,szFromObj(c->argv[4]),SLC_NOREFRESH,&created);\n        if (created) {\n            g_pserver->dirty++;\n            notifyKeyspaceEvent(NOTIFY_STREAM,\"xgroup-createconsumer\",\n                                c->argv[2],c->db->id);\n        }\n        addReplyLongLong(c,created);\n    } else if (!strcasecmp(opt,\"DELCONSUMER\") && c->argc == 5) {\n        /* Delete the consumer and returns the number of pending messages\n         * that were yet associated with such a consumer. */\n        long long pending = streamDelConsumer(cg,szFromObj(c->argv[4]));\n        addReplyLongLong(c,pending);\n        g_pserver->dirty++;\n        notifyKeyspaceEvent(NOTIFY_STREAM,\"xgroup-delconsumer\",\n                            c->argv[2],c->db->id);\n    } else {\n        addReplySubcommandSyntaxError(c);\n    }\n}\n\n/* XSETID <stream> <id>\n *\n * Set the internal \"last ID\" of a stream. */\nvoid xsetidCommand(client *c) {\n    robj *o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr);\n    if (o == NULL || checkType(c,o,OBJ_STREAM)) return;\n\n    stream *s = (stream*)ptrFromObj(o);\n    streamID id;\n    if (streamParseStrictIDOrReply(c,c->argv[2],&id,0) != C_OK) return;\n\n    /* If the stream has at least one item, we want to check that the user\n     * is setting a last ID that is equal or greater than the current top\n     * item, otherwise the fundamental ID monotonicity assumption is violated. */\n    if (s->length > 0) {\n        streamID maxid;\n        streamLastValidID(s,&maxid);\n\n        if (streamCompareID(&id,&maxid) < 0) {\n            addReplyError(c,\"The ID specified in XSETID is smaller than the \"\n                            \"target stream top item\");\n            return;\n        }\n    }\n    s->last_id = id;\n    addReply(c,shared.ok);\n    g_pserver->dirty++;\n    notifyKeyspaceEvent(NOTIFY_STREAM,\"xsetid\",c->argv[1],c->db->id);\n}\n\n/* XACK <key> <group> <id> <id> ... <id>\n *\n * Acknowledge a message as processed. In practical terms we just check the\n * pendine entries list (PEL) of the group, and delete the PEL entry both from\n * the group and the consumer (pending messages are referenced in both places).\n *\n * Return value of the command is the number of messages successfully\n * acknowledged, that is, the IDs we were actually able to resolve in the PEL.\n */\nvoid xackCommand(client *c) {\n    streamCG *group = NULL;\n    robj_roptr o = lookupKeyRead(c->db,c->argv[1]);\n    streamID static_ids[STREAMID_STATIC_VECTOR_LEN];\n    streamID *ids = static_ids;\n    int id_count = c->argc-3;\n    int acknowledged = 0;\n\n    if (o) {\n        if (checkType(c,o,OBJ_STREAM)) return; /* Type error. */\n        group = streamLookupCG((stream*)ptrFromObj(o),szFromObj(c->argv[2]));\n    }\n\n    /* No key or group? Nothing to ack. */\n    if (o == nullptr || group == NULL) {\n        addReply(c,shared.czero);\n        return;\n    }\n\n    /* Start parsing the IDs, so that we abort ASAP if there is a syntax\n     * error: the return value of this command cannot be an error in case\n     * the client successfully acknowledged some messages, so it should be\n     * executed in a \"all or nothing\" fashion. */\n    if (id_count > STREAMID_STATIC_VECTOR_LEN)\n        ids = (streamID*)zmalloc(sizeof(streamID)*id_count);\n    for (int j = 3; j < c->argc; j++) {\n        if (streamParseStrictIDOrReply(c,c->argv[j],&ids[j-3],0) != C_OK) goto cleanup;\n    }\n\n    for (int j = 3; j < c->argc; j++) {\n        unsigned char buf[sizeof(streamID)];\n        streamEncodeID(buf,&ids[j-3]);\n\n        /* Lookup the ID in the group PEL: it will have a reference to the\n         * NACK structure that will have a reference to the consumer, so that\n         * we are able to remove the entry from both PELs. */\n        streamNACK *nack = (streamNACK*)raxFind(group->pel,buf,sizeof(buf));\n        if (nack != raxNotFound) {\n            raxRemove(group->pel,buf,sizeof(buf),NULL);\n            raxRemove(nack->consumer->pel,buf,sizeof(buf),NULL);\n            streamFreeNACK(nack);\n            acknowledged++;\n            g_pserver->dirty++;\n        }\n    }\n    addReplyLongLong(c,acknowledged);\ncleanup:\n    if (ids != static_ids) zfree(ids);\n}\n\n/* XPENDING <key> <group> [[IDLE <idle>] <start> <stop> <count> [<consumer>]]\n *\n * If start and stop are omitted, the command just outputs information about\n * the amount of pending messages for the key/group pair, together with\n * the minimum and maximum ID of pending messages.\n *\n * If start and stop are provided instead, the pending messages are returned\n * with information about the current owner, number of deliveries and last\n * delivery time and so forth. */\nvoid xpendingCommand(client *c) {\n    int justinfo = c->argc == 3; /* Without the range just outputs general\n                                    informations about the PEL. */\n    robj *key = c->argv[1];\n    robj *groupname = c->argv[2];\n    robj *consumername = NULL;\n    streamID startid, endid;\n    long long count = 0;\n    long long minidle = 0;\n    int startex = 0, endex = 0;\n\n    /* Start and stop, and the consumer, can be omitted. Also the IDLE modifier. */\n    if (c->argc != 3 && (c->argc < 6 || c->argc > 9)) {\n        addReplyErrorObject(c,shared.syntaxerr);\n        return;\n    }\n\n    /* Parse start/end/count arguments ASAP if needed, in order to report\n     * syntax errors before any other error. */\n    if (c->argc >= 6) {\n        int startidx = 3; /* Without IDLE */\n\n        if (!strcasecmp(szFromObj(c->argv[3]), \"IDLE\")) {\n            if (getLongLongFromObjectOrReply(c, c->argv[4], &minidle, NULL) == C_ERR)\n                return;\n            if (c->argc < 8) {\n                /* If IDLE was provided we must have at least 'start end count' */\n                addReplyErrorObject(c,shared.syntaxerr);\n                return;\n            }\n            /* Search for rest of arguments after 'IDLE <idle>' */\n            startidx += 2;\n        }\n\n        /* count argument. */\n        if (getLongLongFromObjectOrReply(c,c->argv[startidx+2],&count,NULL) == C_ERR)\n            return;\n        if (count < 0) count = 0;\n\n        /* start and end arguments. */\n        if (streamParseIntervalIDOrReply(c,c->argv[startidx],&startid,&startex,0) != C_OK)\n            return;\n        if (startex && streamIncrID(&startid) != C_OK) {\n            addReplyError(c,\"invalid start ID for the interval\");\n            return;\n        }\n        if (streamParseIntervalIDOrReply(c,c->argv[startidx+1],&endid,&endex,UINT64_MAX) != C_OK)\n            return;\n        if (endex && streamDecrID(&endid) != C_OK) {\n            addReplyError(c,\"invalid end ID for the interval\");\n            return;\n        }\n\n        if (startidx+3 < c->argc) {\n            /* 'consumer' was provided */\n            consumername = c->argv[startidx+3];\n        }\n    }\n\n    /* Lookup the key and the group inside the stream. */\n    robj_roptr o = lookupKeyRead(c->db,c->argv[1]);\n    streamCG *group;\n\n    if (checkType(c,o,OBJ_STREAM)) return;\n    if (o == nullptr ||\n        (group = streamLookupCG((stream*)ptrFromObj(o),szFromObj(groupname))) == NULL)\n    {\n        addReplyErrorFormat(c, \"-NOGROUP No such key '%s' or consumer \"\n                               \"group '%s'\",\n                               (char*)ptrFromObj(key),(char*)ptrFromObj(groupname));\n        return;\n    }\n\n    /* XPENDING <key> <group> variant. */\n    if (justinfo) {\n        addReplyArrayLen(c,4);\n        /* Total number of messages in the PEL. */\n        addReplyLongLong(c,raxSize(group->pel));\n        /* First and last IDs. */\n        if (raxSize(group->pel) == 0) {\n            addReplyNull(c); /* Start. */\n            addReplyNull(c); /* End. */\n            addReplyNullArray(c); /* Clients. */\n        } else {\n            /* Start. */\n            raxIterator ri;\n            raxStart(&ri,group->pel);\n            raxSeek(&ri,\"^\",NULL,0);\n            raxNext(&ri);\n            streamDecodeID(ri.key,&startid);\n            addReplyStreamID(c,&startid);\n\n            /* End. */\n            raxSeek(&ri,\"$\",NULL,0);\n            raxNext(&ri);\n            streamDecodeID(ri.key,&endid);\n            addReplyStreamID(c,&endid);\n            raxStop(&ri);\n\n            /* Consumers with pending messages. */\n            raxStart(&ri,group->consumers);\n            raxSeek(&ri,\"^\",NULL,0);\n            void *arraylen_ptr = addReplyDeferredLen(c);\n            size_t arraylen = 0;\n            while(raxNext(&ri)) {\n                streamConsumer *consumer = (streamConsumer*)ri.data;\n                if (raxSize(consumer->pel) == 0) continue;\n                addReplyArrayLen(c,2);\n                addReplyBulkCBuffer(c,ri.key,ri.key_len);\n                addReplyBulkLongLong(c,raxSize(consumer->pel));\n                arraylen++;\n            }\n            setDeferredArrayLen(c,arraylen_ptr,arraylen);\n            raxStop(&ri);\n        }\n    } else { /* <start>, <stop> and <count> provided, return actual pending entries (not just info) */\n        streamConsumer *consumer = NULL;\n        if (consumername) {\n            consumer = streamLookupConsumer(group,\n                                            szFromObj(consumername),\n                                            SLC_NOCREAT|SLC_NOREFRESH,\n                                            NULL);\n\n            /* If a consumer name was mentioned but it does not exist, we can\n             * just return an empty array. */\n            if (consumer == NULL) {\n                addReplyArrayLen(c,0);\n                return;\n            }\n        }\n\n        rax *pel = consumer ? consumer->pel : group->pel;\n        unsigned char startkey[sizeof(streamID)];\n        unsigned char endkey[sizeof(streamID)];\n        raxIterator ri;\n        mstime_t now = mstime();\n\n        streamEncodeID(startkey,&startid);\n        streamEncodeID(endkey,&endid);\n        raxStart(&ri,pel);\n        raxSeek(&ri,\">=\",startkey,sizeof(startkey));\n        void *arraylen_ptr = addReplyDeferredLen(c);\n        size_t arraylen = 0;\n\n        while(count && raxNext(&ri) && memcmp(ri.key,endkey,ri.key_len) <= 0) {\n            streamNACK *nack = (streamNACK*)ri.data;\n\n            if (minidle) {\n                mstime_t this_idle = now - nack->delivery_time;\n                if (this_idle < minidle) continue;\n            }\n\n            arraylen++;\n            count--;\n            addReplyArrayLen(c,4);\n\n            /* Entry ID. */\n            streamID id;\n            streamDecodeID(ri.key,&id);\n            addReplyStreamID(c,&id);\n\n            /* Consumer name. */\n            addReplyBulkCBuffer(c,nack->consumer->name,\n                                sdslen(nack->consumer->name));\n\n            /* Milliseconds elapsed since last delivery. */\n            mstime_t elapsed = now - nack->delivery_time;\n            if (elapsed < 0) elapsed = 0;\n            addReplyLongLong(c,elapsed);\n\n            /* Number of deliveries. */\n            addReplyLongLong(c,nack->delivery_count);\n        }\n        raxStop(&ri);\n        setDeferredArrayLen(c,arraylen_ptr,arraylen);\n    }\n}\n\n/* XCLAIM <key> <group> <consumer> <min-idle-time> <ID-1> <ID-2>\n *        [IDLE <milliseconds>] [TIME <mstime>] [RETRYCOUNT <count>]\n *        [FORCE] [JUSTID]\n *\n * Gets ownership of one or multiple messages in the Pending Entries List\n * of a given stream consumer group.\n *\n * If the message ID (among the specified ones) exists, and its idle\n * time greater or equal to <min-idle-time>, then the message new owner\n * becomes the specified <consumer>. If the minimum idle time specified\n * is zero, messages are claimed regardless of their idle time.\n *\n * All the messages that cannot be found inside the pending entries list\n * are ignored, but in case the FORCE option is used. In that case we\n * create the NACK (representing a not yet acknowledged message) entry in\n * the consumer group PEL.\n *\n * This command creates the consumer as side effect if it does not yet\n * exists. Moreover the command reset the idle time of the message to 0,\n * even if by using the IDLE or TIME options, the user can control the\n * new idle time.\n *\n * The options at the end can be used in order to specify more attributes\n * to set in the representation of the pending message:\n *\n * 1. IDLE <ms>:\n *      Set the idle time (last time it was delivered) of the message.\n *      If IDLE is not specified, an IDLE of 0 is assumed, that is,\n *      the time count is reset because the message has now a new\n *      owner trying to process it.\n *\n * 2. TIME <ms-unix-time>:\n *      This is the same as IDLE but instead of a relative amount of\n *      milliseconds, it sets the idle time to a specific unix time\n *      (in milliseconds). This is useful in order to rewrite the AOF\n *      file generating XCLAIM commands.\n *\n * 3. RETRYCOUNT <count>:\n *      Set the retry counter to the specified value. This counter is\n *      incremented every time a message is delivered again. Normally\n *      XCLAIM does not alter this counter, which is just served to clients\n *      when the XPENDING command is called: this way clients can detect\n *      anomalies, like messages that are never processed for some reason\n *      after a big number of delivery attempts.\n *\n * 4. FORCE:\n *      Creates the pending message entry in the PEL even if certain\n *      specified IDs are not already in the PEL assigned to a different\n *      client. However the message must be exist in the stream, otherwise\n *      the IDs of non existing messages are ignored.\n *\n * 5. JUSTID:\n *      Return just an array of IDs of messages successfully claimed,\n *      without returning the actual message.\n *\n * 6. LASTID <id>:\n *      Update the consumer group last ID with the specified ID if the\n *      current last ID is smaller than the provided one.\n *      This is used for replication / AOF, so that when we read from a\n *      consumer group, the XCLAIM that gets propagated to give ownership\n *      to the consumer, is also used in order to update the group current\n *      ID.\n *\n * The command returns an array of messages that the user\n * successfully claimed, so that the caller is able to understand\n * what messages it is now in charge of. */\nvoid xclaimCommand(client *c) {\n    streamCG *group = NULL;\n    robj_roptr o = lookupKeyRead(c->db,c->argv[1]);\n    long long minidle; /* Minimum idle time argument. */\n    long long retrycount = -1;   /* -1 means RETRYCOUNT option not given. */\n    mstime_t deliverytime = -1;  /* -1 means IDLE/TIME options not given. */\n    int force = 0;\n    int justid = 0;\n    streamID static_ids[STREAMID_STATIC_VECTOR_LEN];\n    streamID *ids = static_ids;\n\n    if (o) {\n        if (checkType(c,o,OBJ_STREAM)) return; /* Type error. */\n        group = streamLookupCG((stream*)ptrFromObj(o),szFromObj(c->argv[2]));\n    }\n\n{ // BEGIN GOTO PROTECTED VARS\n    /* No key or group? Send an error given that the group creation\n     * is mandatory. */\n    if (o == nullptr || group == NULL) {\n        addReplyErrorFormat(c,\"-NOGROUP No such key '%s' or \"\n                              \"consumer group '%s'\", (char*)ptrFromObj(c->argv[1]),\n                              (char*)ptrFromObj(c->argv[2]));\n        return;\n    }\n\n    if (getLongLongFromObjectOrReply(c,c->argv[4],&minidle,\n        \"Invalid min-idle-time argument for XCLAIM\")\n        != C_OK) return;\n    if (minidle < 0) minidle = 0;\n\n    /* Start parsing the IDs, so that we abort ASAP if there is a syntax\n     * error: the return value of this command cannot be an error in case\n     * the client successfully claimed some message, so it should be\n     * executed in a \"all or nothing\" fashion. */\n    int j;\n    int id_count = c->argc-5;\n    if (id_count > STREAMID_STATIC_VECTOR_LEN)\n        ids = (streamID*)zmalloc(sizeof(streamID)*id_count);\n    for (j = 5; j < c->argc; j++) {\n        if (streamParseStrictIDOrReply(NULL,c->argv[j],&ids[j-5],0) != C_OK) break;\n    }\n    int last_id_arg = j-1; /* Next time we iterate the IDs we now the range. */\n\n    /* If we stopped because some IDs cannot be parsed, perhaps they\n     * are trailing options. */\n    mstime_t now = mstime();\n    streamID last_id = {0,0};\n    int propagate_last_id = 0;\n    for (; j < c->argc; j++) {\n        int moreargs = (c->argc-1) - j; /* Number of additional arguments. */\n        char *opt = szFromObj(c->argv[j]);\n        if (!strcasecmp(opt,\"FORCE\")) {\n            force = 1;\n        } else if (!strcasecmp(opt,\"JUSTID\")) {\n            justid = 1;\n        } else if (!strcasecmp(opt,\"IDLE\") && moreargs) {\n            j++;\n            if (getLongLongFromObjectOrReply(c,c->argv[j],&deliverytime,\n                \"Invalid IDLE option argument for XCLAIM\")\n                != C_OK) goto cleanup;\n            deliverytime = now - deliverytime;\n        } else if (!strcasecmp(opt,\"TIME\") && moreargs) {\n            j++;\n            if (getLongLongFromObjectOrReply(c,c->argv[j],&deliverytime,\n                \"Invalid TIME option argument for XCLAIM\")\n                != C_OK) goto cleanup;\n        } else if (!strcasecmp(opt,\"RETRYCOUNT\") && moreargs) {\n            j++;\n            if (getLongLongFromObjectOrReply(c,c->argv[j],&retrycount,\n                \"Invalid RETRYCOUNT option argument for XCLAIM\")\n                != C_OK) goto cleanup;\n        } else if (!strcasecmp(opt,\"LASTID\") && moreargs) {\n            j++;\n            if (streamParseStrictIDOrReply(c,c->argv[j],&last_id,0) != C_OK) goto cleanup;\n        } else {\n            addReplyErrorFormat(c,\"Unrecognized XCLAIM option '%s'\",opt);\n            goto cleanup;\n        }\n    }\n\n    if (streamCompareID(&last_id,&group->last_id) > 0) {\n        group->last_id = last_id;\n        propagate_last_id = 1;\n    }\n\n    if (deliverytime != -1) {\n        /* If a delivery time was passed, either with IDLE or TIME, we\n         * do some sanity check on it, and set the deliverytime to now\n         * (which is a sane choice usually) if the value is bogus.\n         * To raise an error here is not wise because clients may compute\n         * the idle time doing some math starting from their local time,\n         * and this is not a good excuse to fail in case, for instance,\n         * the computer time is a bit in the future from our POV. */\n        if (deliverytime < 0 || deliverytime > now) deliverytime = now;\n    } else {\n        /* If no IDLE/TIME option was passed, we want the last delivery\n         * time to be now, so that the idle time of the message will be\n         * zero. */\n        deliverytime = now;\n    }\n\n    /* Do the actual claiming. */\n    streamConsumer *consumer = NULL;\n    void *arraylenptr = addReplyDeferredLen(c);\n    size_t arraylen = 0;\n    for (int j = 5; j <= last_id_arg; j++) {\n        streamID id = ids[j-5];\n        unsigned char buf[sizeof(streamID)];\n        streamEncodeID(buf,&id);\n\n        /* Lookup the ID in the group PEL. */\n        streamNACK *nack = (streamNACK*)raxFind(group->pel,buf,sizeof(buf));\n\n        /* If FORCE is passed, let's check if at least the entry\n         * exists in the Stream. In such case, we'll crate a new\n         * entry in the PEL from scratch, so that XCLAIM can also\n         * be used to create entries in the PEL. Useful for AOF\n         * and replication of consumer groups. */\n        if (force && nack == raxNotFound) {\n            streamIterator myiterator;\n            streamIteratorStart(&myiterator,(stream*)ptrFromObj(o),&id,&id,0);\n            int64_t numfields;\n            int found = 0;\n            streamID item_id;\n            if (streamIteratorGetID(&myiterator,&item_id,&numfields)) found = 1;\n            streamIteratorStop(&myiterator);\n\n            /* Item must exist for us to create a NACK for it. */\n            if (!found) continue;\n\n            /* Create the NACK. */\n            nack = streamCreateNACK(NULL);\n            raxInsert(group->pel,buf,sizeof(buf),nack,NULL);\n        }\n\n        if (nack != raxNotFound) {\n            /* We need to check if the minimum idle time requested\n             * by the caller is satisfied by this entry.\n             *\n             * Note that the nack could be created by FORCE, in this\n             * case there was no pre-existing entry and minidle should\n             * be ignored, but in that case nack->consumer is NULL. */\n            if (nack->consumer && minidle) {\n                mstime_t this_idle = now - nack->delivery_time;\n                if (this_idle < minidle) continue;\n            }\n            if (consumer == NULL)\n                consumer = streamLookupConsumer(group,szFromObj(c->argv[3]),SLC_NONE,NULL);\n            if (nack->consumer != consumer) {\n                /* Remove the entry from the old consumer.\n                 * Note that nack->consumer is NULL if we created the\n                 * NACK above because of the FORCE option. */\n                if (nack->consumer)\n                    raxRemove(nack->consumer->pel,buf,sizeof(buf),NULL);\n            }\n            nack->delivery_time = deliverytime;\n            /* Set the delivery attempts counter if given, otherwise\n             * autoincrement unless JUSTID option provided */\n            if (retrycount >= 0) {\n                nack->delivery_count = retrycount;\n            } else if (!justid) {\n                nack->delivery_count++;\n            }\n            if (nack->consumer != consumer) {\n                /* Add the entry in the new consumer local PEL. */\n                raxInsert(consumer->pel,buf,sizeof(buf),nack,NULL);\n                nack->consumer = consumer;\n            }\n            /* Send the reply for this entry. */\n            if (justid) {\n                addReplyStreamID(c,&id);\n            } else {\n                size_t emitted = streamReplyWithRange(c,(stream*)ptrFromObj(o),&id,&id,1,0,\n                                    NULL,NULL,STREAM_RWR_RAWENTRIES,NULL);\n                if (!emitted) addReplyNull(c);\n            }\n            arraylen++;\n\n            /* Propagate this change. */\n            streamPropagateXCLAIM(c,c->argv[1],group,c->argv[2],c->argv[j],nack);\n            propagate_last_id = 0; /* Will be propagated by XCLAIM itself. */\n            g_pserver->dirty++;\n        }\n    }\n    if (propagate_last_id) {\n        streamPropagateGroupID(c,c->argv[1],group,c->argv[2]);\n        g_pserver->dirty++;\n    }\n    setDeferredArrayLen(c,arraylenptr,arraylen);\n    preventCommandPropagation(c);\n}   // END GOTO PROTECTED VARIABLES\ncleanup:\n    if (ids != static_ids) zfree(ids);\n}\n\n/* XAUTOCLAIM <key> <group> <consumer> <min-idle-time> <start> [COUNT <count>] [JUSTID]\n *\n * Gets ownership of one or multiple messages in the Pending Entries List\n * of a given stream consumer group.\n *\n * For each PEL entry, if its idle time greater or equal to <min-idle-time>,\n * then the message new owner becomes the specified <consumer>.\n * If the minimum idle time specified is zero, messages are claimed\n * regardless of their idle time.\n *\n * This command creates the consumer as side effect if it does not yet\n * exists. Moreover the command reset the idle time of the message to 0.\n *\n * The command returns an array of messages that the user\n * successfully claimed, so that the caller is able to understand\n * what messages it is now in charge of. */\nvoid xautoclaimCommand(client *c) {\n    streamCG *group = NULL;\n    robj_roptr o = lookupKeyRead(c->db,c->argv[1]);\n    long long minidle; /* Minimum idle time argument, in milliseconds. */\n    long count = 100; /* Maximum entries to claim. */\n    streamID startid;\n    int startex;\n    int justid = 0;\n\n    /* Parse idle/start/end/count arguments ASAP if needed, in order to report\n     * syntax errors before any other error. */\n    if (getLongLongFromObjectOrReply(c,c->argv[4],&minidle,\"Invalid min-idle-time argument for XAUTOCLAIM\") != C_OK)\n        return;\n    if (minidle < 0) minidle = 0;\n\n    if (streamParseIntervalIDOrReply(c,c->argv[5],&startid,&startex,0) != C_OK)\n        return;\n    if (startex && streamIncrID(&startid) != C_OK) {\n        addReplyError(c,\"invalid start ID for the interval\");\n        return;\n    }\n\n    int j = 6; /* options start at argv[6] */\n    while(j < c->argc) {\n        int moreargs = (c->argc-1) - j; /* Number of additional arguments. */\n        char *opt = szFromObj(c->argv[j]);\n        if (!strcasecmp(opt,\"COUNT\") && moreargs) {\n            if (getRangeLongFromObjectOrReply(c,c->argv[j+1],1,LONG_MAX,&count,\"COUNT must be > 0\") != C_OK)\n                return;\n            j++;\n        } else if (!strcasecmp(opt,\"JUSTID\")) {\n            justid = 1;\n        } else {\n            addReplyErrorObject(c,shared.syntaxerr);\n            return;\n        }\n        j++;\n    }\n\n    if (o) {\n        if (checkType(c,o,OBJ_STREAM))\n            return; /* Type error. */\n        group = (streamCG*)streamLookupCG((stream*)ptrFromObj(o),szFromObj(c->argv[2]));\n    }\n\n    /* No key or group? Send an error given that the group creation\n     * is mandatory. */\n    if (o == nullptr || group == NULL) {\n        addReplyErrorFormat(c,\"-NOGROUP No such key '%s' or consumer group '%s'\",\n                            (char*)szFromObj(c->argv[1]),\n                            (char*)szFromObj(c->argv[2]));\n        return;\n    }\n\n    /* Do the actual claiming. */\n    streamConsumer *consumer = NULL;\n    long long attempts = count*10;\n\n    addReplyArrayLen(c, 2);\n    void *endidptr = addReplyDeferredLen(c);\n    void *arraylenptr = addReplyDeferredLen(c);\n\n    unsigned char startkey[sizeof(streamID)];\n    streamEncodeID(startkey,&startid);\n    raxIterator ri;\n    raxStart(&ri,group->pel);\n    raxSeek(&ri,\">=\",startkey,sizeof(startkey));\n    size_t arraylen = 0;\n    mstime_t now = mstime();\n    while (attempts-- && count && raxNext(&ri)) {\n        streamNACK *nack = (streamNACK*)ri.data;\n\n        if (minidle) {\n            mstime_t this_idle = now - nack->delivery_time;\n            if (this_idle < minidle)\n                continue;\n        }\n\n        streamID id;\n        streamDecodeID(ri.key, &id);\n\n        if (consumer == NULL)\n            consumer = (streamConsumer*)streamLookupConsumer(group,szFromObj(c->argv[3]),SLC_NONE,NULL);\n        if (nack->consumer != consumer) {\n            /* Remove the entry from the old consumer.\n             * Note that nack->consumer is NULL if we created the\n             * NACK above because of the FORCE option. */\n            if (nack->consumer)\n                raxRemove(nack->consumer->pel,ri.key,ri.key_len,NULL);\n        }\n\n        /* Update the consumer and idle time. */\n        nack->delivery_time = now;\n        /* Increment the delivery attempts counter unless JUSTID option provided */\n        if (!justid)\n            nack->delivery_count++;\n\n        if (nack->consumer != consumer) {\n            /* Add the entry in the new consumer local PEL. */\n            raxInsert(consumer->pel,ri.key,ri.key_len,nack,NULL);\n            nack->consumer = consumer;\n        }\n\n        /* Send the reply for this entry. */\n        if (justid) {\n            addReplyStreamID(c,&id);\n        } else {\n            size_t emitted =\n                streamReplyWithRange(c,(stream*)ptrFromObj(o),&id,&id,1,0,NULL,NULL,\n                                     STREAM_RWR_RAWENTRIES,NULL);\n            if (!emitted)\n                addReplyNull(c);\n        }\n        arraylen++;\n        count--;\n\n        /* Propagate this change. */\n        robj *idstr = createObjectFromStreamID(&id);\n        streamPropagateXCLAIM(c,c->argv[1],group,c->argv[2],idstr,nack);\n        decrRefCount(idstr);\n        g_pserver->dirty++;\n    }\n\n    /* We need to return the next entry as a cursor for the next XAUTOCLAIM call */\n    raxNext(&ri);\n\n    streamID endid;\n    if (raxEOF(&ri)) {\n        endid.ms = endid.seq = 0;\n    } else {\n        streamDecodeID(ri.key, &endid);\n    }\n    raxStop(&ri);\n\n    setDeferredArrayLen(c,arraylenptr,arraylen);\n    setDeferredReplyStreamID(c,endidptr,&endid);\n\n    preventCommandPropagation(c);\n}\n\n/* XDEL <key> [<ID1> <ID2> ... <IDN>]\n *\n * Removes the specified entries from the stream. Returns the number\n * of items actually deleted, that may be different from the number\n * of IDs passed in case certain IDs do not exist. */\nvoid xdelCommand(client *c) {\n    robj *o;\n    streamID static_ids[STREAMID_STATIC_VECTOR_LEN];\n    streamID *ids = static_ids;\n\n    if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL\n        || checkType(c,o,OBJ_STREAM)) return;\n    stream *s = (stream*)ptrFromObj(o);\n\n    { // BEGIN GOTO PROTECTED VARS\n    /* We need to sanity check the IDs passed to start. Even if not\n     * a big issue, it is not great that the command is only partially\n     * executed because at some point an invalid ID is parsed. */\n    int id_count = c->argc-2;\n    if (id_count > STREAMID_STATIC_VECTOR_LEN)\n        ids = (streamID*)zmalloc(sizeof(streamID)*id_count);\n    for (int j = 2; j < c->argc; j++) {\n        if (streamParseStrictIDOrReply(c,c->argv[j],&ids[j-2],0) != C_OK) goto cleanup;\n    }\n\n    /* Actually apply the command. */\n    int deleted = 0;\n    for (int j = 2; j < c->argc; j++) {\n        deleted += streamDeleteItem(s,&ids[j-2]);\n    }\n\n    /* Propagate the write if needed. */\n    if (deleted) {\n        signalModifiedKey(c,c->db,c->argv[1]);\n        notifyKeyspaceEvent(NOTIFY_STREAM,\"xdel\",c->argv[1],c->db->id);\n        g_pserver->dirty += deleted;\n    }\n    addReplyLongLong(c,deleted);\n    }// END PROTECTED GOTO VARS\ncleanup:\n    if (ids != static_ids) zfree(ids);\n}\n\n/* General form: XTRIM <key> [... options ...]\n *\n * List of options:\n *\n * Trim strategies:\n *\n * MAXLEN [~|=] <count>     -- Trim so that the stream will be capped at\n *                             the specified length. Use ~ before the\n *                             count in order to demand approximated trimming\n *                             (like XADD MAXLEN option).\n * MINID [~|=] <id>         -- Trim so that the stream will not contain entries\n *                             with IDs smaller than 'id'. Use ~ before the\n *                             count in order to demand approximated trimming\n *                             (like XADD MINID option).\n *\n * Other options:\n *\n * LIMIT <entries>          -- The maximum number of entries to trim.\n *                             0 means unlimited. Unless specified, it is set\n *                             to a default of 100*g_pserver->stream_node_max_entries,\n *                             and that's in order to keep the trimming time sane.\n *                             Has meaning only if `~` was provided.\n */\nvoid xtrimCommand(client *c) {\n    robj *o;\n\n    /* Argument parsing. */\n    streamAddTrimArgs parsed_args;\n    if (streamParseAddOrTrimArgsOrReply(c, &parsed_args, 0) < 0)\n        return; /* streamParseAddOrTrimArgsOrReply already replied. */\n\n    /* If the key does not exist, we are ok returning zero, that is, the\n     * number of elements removed from the stream. */\n    if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL\n        || checkType(c,o,OBJ_STREAM)) return;\n    stream *s = (stream*)ptrFromObj(o);\n\n    /* Perform the trimming. */\n    int64_t deleted = streamTrim(s, &parsed_args);\n    if (deleted) {\n        notifyKeyspaceEvent(NOTIFY_STREAM,\"xtrim\",c->argv[1],c->db->id);\n        if (parsed_args.approx_trim) {\n            /* In case our trimming was limited (by LIMIT or by ~) we must\n             * re-write the relevant trim argument to make sure there will be\n             * no inconsistencies in AOF loading or in the replica.\n             * It's enough to check only args->approx because there is no\n             * way LIMIT is given without the ~ option. */\n            streamRewriteApproxSpecifier(c,parsed_args.trim_strategy_arg_idx-1);\n            streamRewriteTrimArgument(c,s,parsed_args.trim_strategy,parsed_args.trim_strategy_arg_idx);\n        }\n\n        /* Propagate the write. */\n        signalModifiedKey(c, c->db,c->argv[1]);\n        g_pserver->dirty += deleted;\n    }\n    addReplyLongLong(c,deleted);\n}\n\n/* Helper function for xinfoCommand.\n * Handles the variants of XINFO STREAM */\nvoid xinfoReplyWithStreamInfo(client *c, stream *s) {\n    int full = 1;\n    long long count = 10; /* Default COUNT is 10 so we don't block the server */\n    robj **optv = c->argv + 3; /* Options start after XINFO STREAM <key> */\n    int optc = c->argc - 3;\n\n    /* Parse options. */\n    if (optc == 0) {\n        full = 0;\n    } else {\n        /* Valid options are [FULL] or [FULL COUNT <count>] */\n        if (optc != 1 && optc != 3) {\n            addReplySubcommandSyntaxError(c);\n            return;\n        }\n\n        /* First option must be \"FULL\" */\n        if (strcasecmp(szFromObj(optv[0]),\"full\")) {\n            addReplySubcommandSyntaxError(c);\n            return;\n        }\n\n        if (optc == 3) {\n            /* First option must be \"FULL\" */\n            if (strcasecmp(szFromObj(optv[1]),\"count\")) {\n                addReplySubcommandSyntaxError(c);\n                return;\n            }\n            if (getLongLongFromObjectOrReply(c,optv[2],&count,NULL) == C_ERR)\n                return;\n            if (count < 0) count = 10;\n        }\n    }\n\n    addReplyMapLen(c,full ? 6 : 7);\n    addReplyBulkCString(c,\"length\");\n    addReplyLongLong(c,s->length);\n    addReplyBulkCString(c,\"radix-tree-keys\");\n    addReplyLongLong(c,raxSize(s->rax));\n    addReplyBulkCString(c,\"radix-tree-nodes\");\n    addReplyLongLong(c,s->rax->numnodes);\n    addReplyBulkCString(c,\"last-generated-id\");\n    addReplyStreamID(c,&s->last_id);\n\n    if (!full) {\n        /* XINFO STREAM <key> */\n\n        addReplyBulkCString(c,\"groups\");\n        addReplyLongLong(c,s->cgroups ? raxSize(s->cgroups) : 0);\n\n        /* To emit the first/last entry we use streamReplyWithRange(). */\n        int emitted;\n        streamID start, end;\n        start.ms = start.seq = 0;\n        end.ms = end.seq = UINT64_MAX;\n        addReplyBulkCString(c,\"first-entry\");\n        emitted = streamReplyWithRange(c,s,&start,&end,1,0,NULL,NULL,\n                                       STREAM_RWR_RAWENTRIES,NULL);\n        if (!emitted) addReplyNull(c);\n        addReplyBulkCString(c,\"last-entry\");\n        emitted = streamReplyWithRange(c,s,&start,&end,1,1,NULL,NULL,\n                                       STREAM_RWR_RAWENTRIES,NULL);\n        if (!emitted) addReplyNull(c);\n    } else {\n        /* XINFO STREAM <key> FULL [COUNT <count>] */\n\n        /* Stream entries */\n        addReplyBulkCString(c,\"entries\");\n        streamReplyWithRange(c,s,NULL,NULL,count,0,NULL,NULL,0,NULL);\n\n        /* Consumer groups */\n        addReplyBulkCString(c,\"groups\");\n        if (s->cgroups == NULL) {\n            addReplyArrayLen(c,0);\n        } else {\n            addReplyArrayLen(c,raxSize(s->cgroups));\n            raxIterator ri_cgroups;\n            raxStart(&ri_cgroups,s->cgroups);\n            raxSeek(&ri_cgroups,\"^\",NULL,0);\n            while(raxNext(&ri_cgroups)) {\n                streamCG *cg = (streamCG*)ri_cgroups.data;\n                addReplyMapLen(c,5);\n\n                /* Name */\n                addReplyBulkCString(c,\"name\");\n                addReplyBulkCBuffer(c,ri_cgroups.key,ri_cgroups.key_len);\n\n                /* Last delivered ID */\n                addReplyBulkCString(c,\"last-delivered-id\");\n                addReplyStreamID(c,&cg->last_id);\n\n                /* Group PEL count */\n                addReplyBulkCString(c,\"pel-count\");\n                addReplyLongLong(c,raxSize(cg->pel));\n\n                /* Group PEL */\n                addReplyBulkCString(c,\"pending\");\n                long long arraylen_cg_pel = 0;\n                void *arrayptr_cg_pel = addReplyDeferredLen(c);\n                raxIterator ri_cg_pel;\n                raxStart(&ri_cg_pel,cg->pel);\n                raxSeek(&ri_cg_pel,\"^\",NULL,0);\n                while(raxNext(&ri_cg_pel) && (!count || arraylen_cg_pel < count)) {\n                    streamNACK *nack = (streamNACK*)ri_cg_pel.data;\n                    addReplyArrayLen(c,4);\n\n                    /* Entry ID. */\n                    streamID id;\n                    streamDecodeID(ri_cg_pel.key,&id);\n                    addReplyStreamID(c,&id);\n\n                    /* Consumer name. */\n                    serverAssert(nack->consumer); /* assertion for valgrind (avoid NPD) */\n                    addReplyBulkCBuffer(c,nack->consumer->name,\n                                        sdslen(nack->consumer->name));\n\n                    /* Last delivery. */\n                    addReplyLongLong(c,nack->delivery_time);\n\n                    /* Number of deliveries. */\n                    addReplyLongLong(c,nack->delivery_count);\n\n                    arraylen_cg_pel++;\n                }\n                setDeferredArrayLen(c,arrayptr_cg_pel,arraylen_cg_pel);\n                raxStop(&ri_cg_pel);\n\n                /* Consumers */\n                addReplyBulkCString(c,\"consumers\");\n                addReplyArrayLen(c,raxSize(cg->consumers));\n                raxIterator ri_consumers;\n                raxStart(&ri_consumers,cg->consumers);\n                raxSeek(&ri_consumers,\"^\",NULL,0);\n                while(raxNext(&ri_consumers)) {\n                    streamConsumer *consumer = (streamConsumer*)ri_consumers.data;\n                    addReplyMapLen(c,4);\n\n                    /* Consumer name */\n                    addReplyBulkCString(c,\"name\");\n                    addReplyBulkCBuffer(c,consumer->name,sdslen(consumer->name));\n\n                    /* Seen-time */\n                    addReplyBulkCString(c,\"seen-time\");\n                    addReplyLongLong(c,consumer->seen_time);\n\n                    /* Consumer PEL count */\n                    addReplyBulkCString(c,\"pel-count\");\n                    addReplyLongLong(c,raxSize(consumer->pel));\n\n                    /* Consumer PEL */\n                    addReplyBulkCString(c,\"pending\");\n                    long long arraylen_cpel = 0;\n                    void *arrayptr_cpel = addReplyDeferredLen(c);\n                    raxIterator ri_cpel;\n                    raxStart(&ri_cpel,consumer->pel);\n                    raxSeek(&ri_cpel,\"^\",NULL,0);\n                    while(raxNext(&ri_cpel) && (!count || arraylen_cpel < count)) {\n                        streamNACK *nack = (streamNACK*)ri_cpel.data;\n                        addReplyArrayLen(c,3);\n\n                        /* Entry ID. */\n                        streamID id;\n                        streamDecodeID(ri_cpel.key,&id);\n                        addReplyStreamID(c,&id);\n\n                        /* Last delivery. */\n                        addReplyLongLong(c,nack->delivery_time);\n\n                        /* Number of deliveries. */\n                        addReplyLongLong(c,nack->delivery_count);\n\n                        arraylen_cpel++;\n                    }\n                    setDeferredArrayLen(c,arrayptr_cpel,arraylen_cpel);\n                    raxStop(&ri_cpel);\n                }\n                raxStop(&ri_consumers);\n            }\n            raxStop(&ri_cgroups);\n        }\n    }\n}\n\n/* XINFO CONSUMERS <key> <group>\n * XINFO GROUPS <key>\n * XINFO STREAM <key> [FULL [COUNT <count>]]\n * XINFO HELP. */\nvoid xinfoCommand(client *c) {\n    stream *s = NULL;\n    char *opt;\n    robj *key;\n\n    /* HELP is special. Handle it ASAP. */\n    if (!strcasecmp(szFromObj(c->argv[1]),\"HELP\")) {\n        const char *help[] = {\n\"CONSUMERS <key> <groupname>\",\n\"    Show consumers of <groupname>.\",\n\"GROUPS <key>\",\n\"    Show the stream consumer groups.\",\n\"STREAM <key> [FULL [COUNT <count>]\",\n\"    Show information about the stream.\",\nNULL\n        };\n        addReplyHelp(c, help);\n        return;\n    } else if (c->argc < 3) {\n        addReplySubcommandSyntaxError(c);\n        return;\n    }\n\n    /* With the exception of HELP handled before any other sub commands, all\n     * the ones are in the form of \"<subcommand> <key>\". */\n    opt = szFromObj(c->argv[1]);\n    key = c->argv[2];\n\n    /* Lookup the key now, this is common for all the subcommands but HELP. */\n    robj_roptr o = lookupKeyReadOrReply(c,key,shared.nokeyerr);\n    if (o == nullptr || checkType(c,o,OBJ_STREAM)) return;\n    s = (stream*)ptrFromObj(o);\n\n    /* Dispatch the different subcommands. */\n    if (!strcasecmp(opt,\"CONSUMERS\") && c->argc == 4) {\n        /* XINFO CONSUMERS <key> <group>. */\n        streamCG *cg = streamLookupCG(s,szFromObj(c->argv[3]));\n        if (cg == NULL) {\n            addReplyErrorFormat(c, \"-NOGROUP No such consumer group '%s' \"\n                                   \"for key name '%s'\",\n                                   (char*)ptrFromObj(c->argv[3]), (char*)ptrFromObj(key));\n            return;\n        }\n\n        addReplyArrayLen(c,raxSize(cg->consumers));\n        raxIterator ri;\n        raxStart(&ri,cg->consumers);\n        raxSeek(&ri,\"^\",NULL,0);\n        mstime_t now = mstime();\n        while(raxNext(&ri)) {\n            streamConsumer *consumer = (streamConsumer*)ri.data;\n            mstime_t idle = now - consumer->seen_time;\n            if (idle < 0) idle = 0;\n\n            addReplyMapLen(c,3);\n            addReplyBulkCString(c,\"name\");\n            addReplyBulkCBuffer(c,consumer->name,sdslen(consumer->name));\n            addReplyBulkCString(c,\"pending\");\n            addReplyLongLong(c,raxSize(consumer->pel));\n            addReplyBulkCString(c,\"idle\");\n            addReplyLongLong(c,idle);\n        }\n        raxStop(&ri);\n    } else if (!strcasecmp(opt,\"GROUPS\") && c->argc == 3) {\n        /* XINFO GROUPS <key>. */\n        if (s->cgroups == NULL) {\n            addReplyArrayLen(c,0);\n            return;\n        }\n\n        addReplyArrayLen(c,raxSize(s->cgroups));\n        raxIterator ri;\n        raxStart(&ri,s->cgroups);\n        raxSeek(&ri,\"^\",NULL,0);\n        while(raxNext(&ri)) {\n            streamCG *cg = (streamCG*)ri.data;\n            addReplyMapLen(c,4);\n            addReplyBulkCString(c,\"name\");\n            addReplyBulkCBuffer(c,ri.key,ri.key_len);\n            addReplyBulkCString(c,\"consumers\");\n            addReplyLongLong(c,raxSize(cg->consumers));\n            addReplyBulkCString(c,\"pending\");\n            addReplyLongLong(c,raxSize(cg->pel));\n            addReplyBulkCString(c,\"last-delivered-id\");\n            addReplyStreamID(c,&cg->last_id);\n        }\n        raxStop(&ri);\n    } else if (!strcasecmp(opt,\"STREAM\")) {\n        /* XINFO STREAM <key> [FULL [COUNT <count>]]. */\n        xinfoReplyWithStreamInfo(c,s);\n    } else {\n        addReplySubcommandSyntaxError(c);\n    }\n}\n\n/* Validate the integrity stream listpack entries structure. Both in term of a\n * valid listpack, but also that the structure of the entires matches a valid\n * stream. return 1 if valid 0 if not valid. */\nint streamValidateListpackIntegrity(unsigned char *lp, size_t size, int deep) {\n    int valid_record;\n    unsigned char *p, *next;\n\n    /* Since we don't want to run validation of all records twice, we'll\n     * run the listpack validation of just the header and do the rest here. */\n    if (!lpValidateIntegrity(lp, size, 0))\n        return 0;\n\n    /* In non-deep mode we just validated the listpack header (encoded size) */\n    if (!deep) return 1;\n\n    next = p = lpValidateFirst(lp);\n    if (!lpValidateNext(lp, &next, size)) return 0;\n    if (!p) return 0;\n\n    /* entry count */\n    int64_t entry_count = lpGetIntegerIfValid(p, &valid_record);\n    if (!valid_record) return 0;\n    p = next; if (!lpValidateNext(lp, &next, size)) return 0;\n\n    /* deleted */\n    int64_t deleted_count = lpGetIntegerIfValid(p, &valid_record);\n    if (!valid_record) return 0;\n    p = next; if (!lpValidateNext(lp, &next, size)) return 0;\n\n    /* num-of-fields */\n    int64_t master_fields = lpGetIntegerIfValid(p, &valid_record);\n    if (!valid_record) return 0;\n    p = next; if (!lpValidateNext(lp, &next, size)) return 0;\n\n    /* the field names */\n    for (int64_t j = 0; j < master_fields; j++) {\n        p = next; if (!lpValidateNext(lp, &next, size)) return 0;\n    }\n\n    /* the zero master entry terminator. */\n    int64_t zero = lpGetIntegerIfValid(p, &valid_record);\n    if (!valid_record || zero != 0) return 0;\n    p = next; if (!lpValidateNext(lp, &next, size)) return 0;\n\n    entry_count += deleted_count;\n    while (entry_count--) {\n        if (!p) return 0;\n        int64_t fields = master_fields, extra_fields = 3;\n        int64_t flags = lpGetIntegerIfValid(p, &valid_record);\n        if (!valid_record) return 0;\n        p = next; if (!lpValidateNext(lp, &next, size)) return 0;\n\n        /* entry id */\n        lpGetIntegerIfValid(p, &valid_record);\n        if (!valid_record) return 0;\n        p = next; if (!lpValidateNext(lp, &next, size)) return 0;\n        lpGetIntegerIfValid(p, &valid_record);\n        if (!valid_record) return 0;\n        p = next; if (!lpValidateNext(lp, &next, size)) return 0;\n\n        if (!(flags & STREAM_ITEM_FLAG_SAMEFIELDS)) {\n            /* num-of-fields */\n            fields = lpGetIntegerIfValid(p, &valid_record);\n            if (!valid_record) return 0;\n            p = next; if (!lpValidateNext(lp, &next, size)) return 0;\n\n            /* the field names */\n            for (int64_t j = 0; j < fields; j++) {\n                p = next; if (!lpValidateNext(lp, &next, size)) return 0;\n            }\n\n            extra_fields += fields + 1;\n        }\n\n        /* the values */\n        for (int64_t j = 0; j < fields; j++) {\n            p = next; if (!lpValidateNext(lp, &next, size)) return 0;\n        }\n\n        /* lp-count */\n        int64_t lp_count = lpGetIntegerIfValid(p, &valid_record);\n        if (!valid_record) return 0;\n        if (lp_count != fields + extra_fields) return 0;\n        p = next; if (!lpValidateNext(lp, &next, size)) return 0;\n    }\n\n    if (next)\n        return 0;\n\n    return 1;\n}\n"
  },
  {
    "path": "src/t_string.cpp",
    "content": "/*\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n#include <cmath> /* isnan(), isinf() */\n#include \"aelocker.h\"\n\n/* Forward declarations */\nint getGenericCommand(client *c);\n\n/*-----------------------------------------------------------------------------\n * String Commands\n *----------------------------------------------------------------------------*/\n\nstatic int checkStringLength(client *c, long long size, long long append) {\n    if (c->flags & CLIENT_MASTER)\n        return C_OK;\n    /* 'uint64_t' cast is there just to prevent undefined behavior on overflow */\n    long long total = (uint64_t)size + append;\n    /* Test configured max-bulk-len represending a limit of the biggest string object,\n     * and also test for overflow. */\n    if (total > g_pserver->proto_max_bulk_len || total < size || total < append) {\n        addReplyError(c,\"string exceeds maximum allowed size (proto-max-bulk-len)\");\n        return C_ERR;\n    }\n    return C_OK;\n}\n\n/* The setGenericCommand() function implements the SET operation with different\n * options and variants. This function is called in order to implement the\n * following commands: SET, SETEX, PSETEX, SETNX, GETSET.\n *\n * 'flags' changes the behavior of the command (NX, XX or GET, see below).\n *\n * 'expire' represents an expire to set in form of a Redis object as passed\n * by the user. It is interpreted according to the specified 'unit'.\n *\n * 'ok_reply' and 'abort_reply' is what the function will reply to the client\n * if the operation is performed, or when it is not because of NX or\n * XX flags.\n *\n * If ok_reply is NULL \"+OK\" is used.\n * If abort_reply is NULL, \"$-1\" is used. */\n\n#define OBJ_NO_FLAGS 0\n#define OBJ_SET_NX (1<<0)          /* Set if key not exists. */\n#define OBJ_SET_XX (1<<1)          /* Set if key exists. */\n#define OBJ_EX (1<<2)              /* Set if time in seconds is given */\n#define OBJ_PX (1<<3)              /* Set if time in ms in given */\n#define OBJ_KEEPTTL (1<<4)         /* Set and keep the ttl */\n#define OBJ_SET_GET (1<<5)         /* Set if want to get key before set */\n#define OBJ_EXAT (1<<6)            /* Set if timestamp in second is given */\n#define OBJ_PXAT (1<<7)            /* Set if timestamp in ms is given */\n#define OBJ_PERSIST (1<<8)         /* Set if we need to remove the ttl */\n\nvoid setGenericCommand(client *c, int flags, robj *key, robj *val, robj *expire, int unit, robj *ok_reply, robj *abort_reply) {\n    long long milliseconds = 0, when = 0; /* initialized to avoid any harmness warning */\n\n    if (expire) {\n        if (getLongLongFromObjectOrReply(c, expire, &milliseconds, NULL) != C_OK)\n            return;\n        if (milliseconds <= 0 || (unit == UNIT_SECONDS && milliseconds > LLONG_MAX / 1000)) {\n            /* Negative value provided or multiplication is gonna overflow. */\n            addReplyErrorFormat(c, \"invalid expire time in %s\", c->cmd->name);\n            return;\n        }\n        if (unit == UNIT_SECONDS) milliseconds *= 1000;\n        when = milliseconds;\n        if ((flags & OBJ_PX) || (flags & OBJ_EX))\n            when += mstime();\n        if (when <= 0) {\n            /* Overflow detected. */\n            addReplyErrorFormat(c, \"invalid expire time in %s\", c->cmd->name);\n            return;\n        }\n    }\n\n    if ((flags & OBJ_SET_NX && lookupKeyWrite(c->db,key) != NULL) ||\n        (flags & OBJ_SET_XX && lookupKeyWrite(c->db,key) == NULL))\n    {\n        addReply(c, abort_reply ? abort_reply : shared.null[c->resp]);\n        return;\n    }\n\n    if (flags & OBJ_SET_GET) {\n        if (getGenericCommand(c) == C_ERR) return;\n    }\n\n    genericSetKey(c,c->db,key, val,flags & OBJ_KEEPTTL,1);\n    g_pserver->dirty++;\n    notifyKeyspaceEvent(NOTIFY_STRING,\"set\",key,c->db->id);\n    if (expire) {\n        setExpire(c,c->db,key,nullptr,when);\n        notifyKeyspaceEvent(NOTIFY_GENERIC,\"expire\",key,c->db->id);\n\n        /* Propagate as SET Key Value PXAT millisecond-timestamp if there is EXAT/PXAT or\n         * propagate as SET Key Value PX millisecond if there is EX/PX flag.\n         *\n         * Additionally when we propagate the SET with PX (relative millisecond) we translate\n         * it again to SET with PXAT for the AOF.\n         *\n         * Additional care is required while modifying the argument order. AOF relies on the\n         * exp argument being at index 3. (see feedAppendOnlyFile)\n         * */\n        robj *exp = (flags & OBJ_PXAT) || (flags & OBJ_EXAT) ? shared.pxat : shared.px;\n        robj *millisecondObj = createStringObjectFromLongLong(milliseconds);\n        rewriteClientCommandVector(c,5,shared.set,key,val,exp,millisecondObj);\n        decrRefCount(millisecondObj);\n    }\n    if (!(flags & OBJ_SET_GET)) {\n        addReply(c, ok_reply ? ok_reply : shared.ok);\n    }\n\n    /* Propagate without the GET argument (Isn't needed if we had expire since in that case we completely re-written the command argv) */\n    if ((flags & OBJ_SET_GET) && !expire) {\n        int argc = 0;\n        int j;\n        robj **argv = (robj**)zmalloc((c->argc-1)*sizeof(robj*));\n        for (j=0; j < c->argc; j++) {\n            const char *a = szFromObj(c->argv[j]);\n            /* Skip GET which may be repeated multiple times. */\n            if (j >= 3 &&\n                (a[0] == 'g' || a[0] == 'G') &&\n                (a[1] == 'e' || a[1] == 'E') &&\n                (a[2] == 't' || a[2] == 'T') && a[3] == '\\0')\n                continue;\n            argv[argc++] = c->argv[j];\n            incrRefCount(c->argv[j]);\n        }\n        replaceClientCommandVector(c, argc, argv);\n    }\n}\n\n#define COMMAND_GET 0\n#define COMMAND_SET 1\n/*\n * The parseExtendedStringArgumentsOrReply() function performs the common validation for extended\n * string arguments used in SET and GET command.\n *\n * Get specific commands - PERSIST/DEL\n * Set specific commands - XX/NX/GET\n * Common commands - EX/EXAT/PX/PXAT/KEEPTTL\n *\n * Function takes pointers to client, flags, unit, pointer to pointer of expire obj if needed\n * to be determined and command_type which can be COMMAND_GET or COMMAND_SET.\n *\n * If there are any syntax violations C_ERR is returned else C_OK is returned.\n *\n * Input flags are updated upon parsing the arguments. Unit and expire are updated if there are any\n * EX/EXAT/PX/PXAT arguments. Unit is updated to millisecond if PX/PXAT is set.\n */\nint parseExtendedStringArgumentsOrReply(client *c, int *flags, int *unit, robj **expire, int command_type) {\n\n    int j = command_type == COMMAND_GET ? 2 : 3;\n    for (; j < c->argc; j++) {\n        const char *opt = szFromObj(c->argv[j]);\n        robj *next = (j == c->argc-1) ? NULL : c->argv[j+1];\n\n        if ((opt[0] == 'n' || opt[0] == 'N') &&\n            (opt[1] == 'x' || opt[1] == 'X') && opt[2] == '\\0' &&\n            !(*flags & OBJ_SET_XX) && !(*flags & OBJ_SET_GET) && (command_type == COMMAND_SET))\n        {\n            *flags |= OBJ_SET_NX;\n        } else if ((opt[0] == 'x' || opt[0] == 'X') &&\n                   (opt[1] == 'x' || opt[1] == 'X') && opt[2] == '\\0' &&\n                   !(*flags & OBJ_SET_NX) && (command_type == COMMAND_SET))\n        {\n            *flags |= OBJ_SET_XX;\n        } else if ((opt[0] == 'g' || opt[0] == 'G') &&\n                   (opt[1] == 'e' || opt[1] == 'E') &&\n                   (opt[2] == 't' || opt[2] == 'T') && opt[3] == '\\0' &&\n                   !(*flags & OBJ_SET_NX) && (command_type == COMMAND_SET))\n        {\n            *flags |= OBJ_SET_GET;\n        } else if (!strcasecmp(opt, \"KEEPTTL\") && !(*flags & OBJ_PERSIST) &&\n            !(*flags & OBJ_EX) && !(*flags & OBJ_EXAT) &&\n            !(*flags & OBJ_PX) && !(*flags & OBJ_PXAT) && (command_type == COMMAND_SET))\n        {\n            *flags |= OBJ_KEEPTTL;\n        } else if (!strcasecmp(opt,\"PERSIST\") && (command_type == COMMAND_GET) &&\n               !(*flags & OBJ_EX) && !(*flags & OBJ_EXAT) &&\n               !(*flags & OBJ_PX) && !(*flags & OBJ_PXAT) &&\n               !(*flags & OBJ_KEEPTTL))\n        {\n            *flags |= OBJ_PERSIST;\n        } else if ((opt[0] == 'e' || opt[0] == 'E') &&\n                   (opt[1] == 'x' || opt[1] == 'X') && opt[2] == '\\0' &&\n                   !(*flags & OBJ_KEEPTTL) && !(*flags & OBJ_PERSIST) &&\n                   !(*flags & OBJ_EXAT) && !(*flags & OBJ_PX) &&\n                   !(*flags & OBJ_PXAT) && next)\n        {\n            *flags |= OBJ_EX;\n            *expire = next;\n            j++;\n        } else if ((opt[0] == 'p' || opt[0] == 'P') &&\n                   (opt[1] == 'x' || opt[1] == 'X') && opt[2] == '\\0' &&\n                   !(*flags & OBJ_KEEPTTL) && !(*flags & OBJ_PERSIST) &&\n                   !(*flags & OBJ_EX) && !(*flags & OBJ_EXAT) &&\n                   !(*flags & OBJ_PXAT) && next)\n        {\n            *flags |= OBJ_PX;\n            *unit = UNIT_MILLISECONDS;\n            *expire = next;\n            j++;\n        } else if ((opt[0] == 'e' || opt[0] == 'E') &&\n                   (opt[1] == 'x' || opt[1] == 'X') &&\n                   (opt[2] == 'a' || opt[2] == 'A') &&\n                   (opt[3] == 't' || opt[3] == 'T') && opt[4] == '\\0' &&\n                   !(*flags & OBJ_KEEPTTL) && !(*flags & OBJ_PERSIST) &&\n                   !(*flags & OBJ_EX) && !(*flags & OBJ_PX) &&\n                   !(*flags & OBJ_PXAT) && next)\n        {\n            *flags |= OBJ_EXAT;\n            *expire = next;\n            j++;\n        } else if ((opt[0] == 'p' || opt[0] == 'P') &&\n                   (opt[1] == 'x' || opt[1] == 'X') &&\n                   (opt[2] == 'a' || opt[2] == 'A') &&\n                   (opt[3] == 't' || opt[3] == 'T') && opt[4] == '\\0' &&\n                   !(*flags & OBJ_KEEPTTL) && !(*flags & OBJ_PERSIST) &&\n                   !(*flags & OBJ_EX) && !(*flags & OBJ_EXAT) &&\n                   !(*flags & OBJ_PX) && next)\n        {\n            *flags |= OBJ_PXAT;\n            *unit = UNIT_MILLISECONDS;\n            *expire = next;\n            j++;\n        } else {\n            addReplyErrorObject(c,shared.syntaxerr);\n            return C_ERR;\n        }\n    }\n    return C_OK;\n}\n\n/* SET key value [NX] [XX] [KEEPTTL] [GET] [EX <seconds>] [PX <milliseconds>]\n *     [EXAT <seconds-timestamp>][PXAT <milliseconds-timestamp>] */\nvoid setCommand(client *c) {\n    robj *expire = NULL;\n    int unit = UNIT_SECONDS;\n    int flags = OBJ_NO_FLAGS;\n\n    if (parseExtendedStringArgumentsOrReply(c,&flags,&unit,&expire,COMMAND_SET) != C_OK) {\n        return;\n    }\n\n    c->argv[2] = tryObjectEncoding(c->argv[2]);\n    setGenericCommand(c,flags,c->argv[1],c->argv[2],expire,unit,NULL,NULL);\n}\n\nvoid setnxCommand(client *c) {\n    c->argv[2] = tryObjectEncoding(c->argv[2]);\n    setGenericCommand(c,OBJ_SET_NX,c->argv[1],c->argv[2],NULL,0,shared.cone,shared.czero);\n}\n\nvoid setexCommand(client *c) {\n    c->argv[3] = tryObjectEncoding(c->argv[3]);\n    setGenericCommand(c,OBJ_EX,c->argv[1],c->argv[3],c->argv[2],UNIT_SECONDS,NULL,NULL);\n}\n\nvoid psetexCommand(client *c) {\n    c->argv[3] = tryObjectEncoding(c->argv[3]);\n    setGenericCommand(c,OBJ_PX,c->argv[1],c->argv[3],c->argv[2],UNIT_MILLISECONDS,NULL,NULL);\n}\n\nint getGenericCommand(client *c) {\n    robj_roptr o;\n    AeLocker locker;\n\n    if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.null[c->resp],locker)) == nullptr)\n        return C_OK;\n\n    if (checkType(c,o,OBJ_STRING)) {\n        return C_ERR;\n    }\n\n    addReplyBulk(c,o);\n    return C_OK;\n}\n\nvoid getCommand(client *c) {\n    getGenericCommand(c);\n}\n\n/*\n * GETEX <key> [PERSIST][EX seconds][PX milliseconds][EXAT seconds-timestamp][PXAT milliseconds-timestamp]\n *\n * The getexCommand() function implements extended options and variants of the GET command. Unlike GET\n * command this command is not read-only.\n *\n * The default behavior when no options are specified is same as GET and does not alter any TTL.\n *\n * Only one of the below options can be used at a given time.\n *\n * 1. PERSIST removes any TTL associated with the key.\n * 2. EX Set expiry TTL in seconds.\n * 3. PX Set expiry TTL in milliseconds.\n * 4. EXAT Same like EX instead of specifying the number of seconds representing the TTL\n *      (time to live), it takes an absolute Unix timestamp\n * 5. PXAT Same like PX instead of specifying the number of milliseconds representing the TTL\n *      (time to live), it takes an absolute Unix timestamp\n *\n * Command would either return the bulk string, error or nil.\n */\nvoid getexCommand(client *c) {\n    robj *expire = NULL;\n    int unit = UNIT_SECONDS;\n    int flags = OBJ_NO_FLAGS;\n\n    if (parseExtendedStringArgumentsOrReply(c,&flags,&unit,&expire,COMMAND_GET) != C_OK) {\n        return;\n    }\n\n    robj_roptr o;\n\n    if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.null[c->resp])) == nullptr)\n        return;\n\n    if (checkType(c,o,OBJ_STRING)) {\n        return;\n    }\n\n    long long milliseconds = 0, when = 0;\n\n    /* Validate the expiration time value first */\n    if (expire) {\n        if (getLongLongFromObjectOrReply(c, expire, &milliseconds, NULL) != C_OK)\n            return;\n        if (milliseconds <= 0 || (unit == UNIT_SECONDS && milliseconds > LLONG_MAX / 1000)) {\n            /* Negative value provided or multiplication is gonna overflow. */\n            addReplyErrorFormat(c, \"invalid expire time in %s\", c->cmd->name);\n            return;\n        }\n        if (unit == UNIT_SECONDS) milliseconds *= 1000;\n        when = milliseconds;\n        if ((flags & OBJ_PX) || (flags & OBJ_EX))\n            when += mstime();\n        if (when <= 0) {\n            /* Overflow detected. */\n            addReplyErrorFormat(c, \"invalid expire time in %s\", c->cmd->name);\n            return;\n        }\n    }\n\n    /* We need to do this before we expire the key or delete it */\n    addReplyBulk(c,o);\n\n    /* This command is never propagated as is. It is either propagated as PEXPIRE[AT],DEL,UNLINK or PERSIST.\n     * This why it doesn't need special handling in feedAppendOnlyFile to convert relative expire time to absolute one. */\n    if (((flags & OBJ_PXAT) || (flags & OBJ_EXAT)) && checkAlreadyExpired(milliseconds)) {\n        /* When PXAT/EXAT absolute timestamp is specified, there can be a chance that timestamp\n         * has already elapsed so delete the key in that case. */\n        int deleted = g_pserver->lazyfree_lazy_expire ? dbAsyncDelete(c->db, c->argv[1]) :\n                      dbSyncDelete(c->db, c->argv[1]);\n        serverAssert(deleted);\n        robj *aux = g_pserver->lazyfree_lazy_expire ? shared.unlink : shared.del;\n        rewriteClientCommandVector(c,2,aux,c->argv[1]);\n        signalModifiedKey(c, c->db, c->argv[1]);\n        notifyKeyspaceEvent(NOTIFY_GENERIC, \"del\", c->argv[1], c->db->id);\n        g_pserver->dirty++;\n    } else if (expire) {\n        setExpire(c,c->db,c->argv[1],nullptr,when);\n        /* Propagate */\n        robj *exp = (flags & OBJ_PXAT) || (flags & OBJ_EXAT) ? shared.pexpireat : shared.pexpire;\n        robj* millisecondObj = createStringObjectFromLongLong(milliseconds);\n        rewriteClientCommandVector(c,3,exp,c->argv[1],millisecondObj);\n        decrRefCount(millisecondObj);\n        signalModifiedKey(c, c->db, c->argv[1]);\n        notifyKeyspaceEvent(NOTIFY_GENERIC,\"expire\",c->argv[1],c->db->id);\n        g_pserver->dirty++;\n    } else if (flags & OBJ_PERSIST) {\n        if (removeExpire(c->db, c->argv[1])) {\n            signalModifiedKey(c, c->db, c->argv[1]);\n            rewriteClientCommandVector(c, 2, shared.persist, c->argv[1]);\n            notifyKeyspaceEvent(NOTIFY_GENERIC,\"persist\",c->argv[1],c->db->id);\n            g_pserver->dirty++;\n        }\n    }\n}\n\nvoid getdelCommand(client *c) {\n    if (getGenericCommand(c) == C_ERR) return;\n    int deleted = g_pserver->lazyfree_lazy_user_del ? dbAsyncDelete(c->db, c->argv[1]) :\n                  dbSyncDelete(c->db, c->argv[1]);\n    if (deleted) {\n        /* Propagate as DEL/UNLINK command */\n        robj *aux = g_pserver->lazyfree_lazy_user_del ? shared.unlink : shared.del;\n        rewriteClientCommandVector(c,2,aux,c->argv[1]);\n        signalModifiedKey(c, c->db, c->argv[1]);\n        notifyKeyspaceEvent(NOTIFY_GENERIC, \"del\", c->argv[1], c->db->id);\n        g_pserver->dirty++;\n    }\n}\n\nvoid getsetCommand(client *c) {\n    if (getGenericCommand(c) == C_ERR) return;\n    c->argv[2] = tryObjectEncoding(c->argv[2]);\n    setKey(c,c->db,c->argv[1],c->argv[2]);\n    notifyKeyspaceEvent(NOTIFY_STRING,\"set\",c->argv[1],c->db->id);\n    g_pserver->dirty++;\n\n    /* Propagate as SET command */\n    rewriteClientCommandArgument(c,0,shared.set);\n}\n\nvoid setrangeCommand(client *c) {\n    robj *o;\n    long offset;\n    sds value = (sds)ptrFromObj(c->argv[3]);\n\n    if (getLongFromObjectOrReply(c,c->argv[2],&offset,NULL) != C_OK)\n        return;\n\n    if (offset < 0) {\n        addReplyError(c,\"offset is out of range\");\n        return;\n    }\n\n    o = lookupKeyWrite(c->db,c->argv[1]);\n    if (o == NULL) {\n        /* Return 0 when setting nothing on a non-existing string */\n        if (sdslen(value) == 0) {\n            addReply(c,shared.czero);\n            return;\n        }\n\n        /* Return when the resulting string exceeds allowed size */\n        if (checkStringLength(c,offset,sdslen(value)) != C_OK)\n            return;\n\n        o = createObject(OBJ_STRING,sdsnewlen(NULL, offset+sdslen(value)));\n        dbAdd(c->db,c->argv[1],o);\n    } else {\n        size_t olen;\n\n        /* Key exists, check type */\n        if (checkType(c,o,OBJ_STRING))\n            return;\n\n        /* Return existing string length when setting nothing */\n        olen = stringObjectLen(o);\n        if (sdslen(value) == 0) {\n            addReplyLongLong(c,olen);\n            return;\n        }\n\n        /* Return when the resulting string exceeds allowed size */\n        if (checkStringLength(c,offset,sdslen(value)) != C_OK)\n            return;\n\n        /* Create a copy when the object is shared or encoded. */\n        o = dbUnshareStringValue(c->db,c->argv[1],o);\n    }\n\n    if (sdslen(value) > 0) {\n        o->m_ptr = sdsgrowzero(szFromObj(o),offset+sdslen(value));\n        memcpy((char*)o->m_ptr+offset,value,sdslen(value));\n        signalModifiedKey(c,c->db,c->argv[1]);\n        notifyKeyspaceEvent(NOTIFY_STRING,\n            \"setrange\",c->argv[1],c->db->id);\n        g_pserver->dirty++;\n    }\n    addReplyLongLong(c,sdslen((sds)ptrFromObj(o)));\n}\n\nvoid getrangeCommand(client *c) {\n    robj_roptr o;\n    long long start, end;\n    const char *str;\n    char llbuf[32];\n    size_t strlen;\n\n    if (getLongLongFromObjectOrReply(c,c->argv[2],&start,NULL) != C_OK)\n        return;\n    if (getLongLongFromObjectOrReply(c,c->argv[3],&end,NULL) != C_OK)\n        return;\n    if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptybulk)) == nullptr ||\n        checkType(c,o,OBJ_STRING)) return;\n\n    if (o->encoding == OBJ_ENCODING_INT) {\n        str = llbuf;\n        strlen = ll2string(llbuf,sizeof(llbuf),(long)ptrFromObj(o));\n    } else {\n        str = szFromObj(o);\n        strlen = sdslen(str);\n    }\n\n    /* Convert negative indexes */\n    if (start < 0 && end < 0 && start > end) {\n        addReply(c,shared.emptybulk);\n        return;\n    }\n    if (start < 0) start = strlen+start;\n    if (end < 0) end = strlen+end;\n    if (start < 0) start = 0;\n    if (end < 0) end = 0;\n    if ((unsigned long long)end >= strlen) end = strlen-1;\n\n    /* Precondition: end >= 0 && end < strlen, so the only condition where\n     * nothing can be returned is: start > end. */\n    if (start > end || strlen == 0) {\n        addReply(c,shared.emptybulk);\n    } else {\n        addReplyBulkCBuffer(c,(char*)str+start,end-start+1);\n    }\n}\n\nvoid mgetCommand(client *c) {\n    AeLocker locker;\n    addReplyArrayLen(c,c->argc-1);\n    for (int i = 1; i < c->argc; i++) {\n        robj_roptr o = lookupKeyRead(c->db,c->argv[i],c->mvccCheckpoint,locker);\n        if (o == nullptr || o->type != OBJ_STRING) {\n            addReplyNull(c);\n        } else {\n            addReplyBulk(c,o);\n        }\n    }\n}\n\nvoid msetGenericCommand(client *c, int nx) {\n    int j;\n\n    if ((c->argc % 2) == 0) {\n        addReplyError(c,\"wrong number of arguments for MSET\");\n        return;\n    }\n\n    /* Handle the NX flag. The MSETNX semantic is to return zero and don't\n     * set anything if at least one key already exists. */\n    if (nx) {\n        for (j = 1; j < c->argc; j += 2) {\n            if (lookupKeyWrite(c->db,c->argv[j]) != NULL) {\n                addReply(c, shared.czero);\n                return;\n            }\n        }\n    }\n\n    for (j = 1; j < c->argc; j += 2) {\n        c->argv[j+1] = tryObjectEncoding(c->argv[j+1]);\n        setKey(c,c->db,c->argv[j],c->argv[j+1]);\n        notifyKeyspaceEvent(NOTIFY_STRING,\"set\",c->argv[j],c->db->id);\n    }\n    g_pserver->dirty += (c->argc-1)/2;\n    addReply(c, nx ? shared.cone : shared.ok);\n}\n\nvoid msetCommand(client *c) {\n    msetGenericCommand(c,0);\n}\n\nvoid msetnxCommand(client *c) {\n    msetGenericCommand(c,1);\n}\n\nvoid incrDecrCommand(client *c, long long incr) {\n    long long value, oldvalue;\n    robj *o, *newObj;\n\n    o = lookupKeyWrite(c->db,c->argv[1]);\n    if (checkType(c,o,OBJ_STRING)) return;\n    if (getLongLongFromObjectOrReply(c,o,&value,NULL) != C_OK) return;\n\n    oldvalue = value;\n    if ((incr < 0 && oldvalue < 0 && incr < (LLONG_MIN-oldvalue)) ||\n        (incr > 0 && oldvalue > 0 && incr > (LLONG_MAX-oldvalue))) {\n        addReplyError(c,\"increment or decrement would overflow\");\n        return;\n    }\n    value += incr;\n\n    if (o && o->getrefcount(std::memory_order_relaxed) == 1 && o->encoding == OBJ_ENCODING_INT &&\n        (value < 0 || value >= OBJ_SHARED_INTEGERS) &&\n        value >= LONG_MIN && value <= LONG_MAX)\n    {\n        newObj = o;\n        o->m_ptr = (void*)((long)value);\n    } else {\n        newObj = createStringObjectFromLongLongForValue(value);\n        if (o) {\n            dbOverwrite(c->db,c->argv[1],newObj);\n        } else {\n            dbAdd(c->db,c->argv[1],newObj);\n        }\n    }\n    signalModifiedKey(c,c->db,c->argv[1]);\n    notifyKeyspaceEvent(NOTIFY_STRING,\"incrby\",c->argv[1],c->db->id);\n    g_pserver->dirty++;\n    addReply(c,shared.colon);\n    addReply(c,newObj);\n    addReply(c,shared.crlf);\n}\n\nvoid incrCommand(client *c) {\n    incrDecrCommand(c,1);\n}\n\nvoid decrCommand(client *c) {\n    incrDecrCommand(c,-1);\n}\n\nvoid incrbyCommand(client *c) {\n    long long incr;\n\n    if (getLongLongFromObjectOrReply(c, c->argv[2], &incr, NULL) != C_OK) return;\n    incrDecrCommand(c,incr);\n}\n\nvoid decrbyCommand(client *c) {\n    long long incr;\n\n    if (getLongLongFromObjectOrReply(c, c->argv[2], &incr, NULL) != C_OK) return;\n    incrDecrCommand(c,-incr);\n}\n\nvoid incrbyfloatCommand(client *c) {\n    long double incr, value;\n    robj *o, *newObj;\n\n    o = lookupKeyWrite(c->db,c->argv[1]);\n    if (checkType(c,o,OBJ_STRING)) return;\n    if (getLongDoubleFromObjectOrReply(c,o,&value,NULL) != C_OK ||\n        getLongDoubleFromObjectOrReply(c,c->argv[2],&incr,NULL) != C_OK)\n        return;\n\n    value += incr;\n    if (std::isnan(value) || std::isinf(value)) {\n        addReplyError(c,\"increment would produce NaN or Infinity\");\n        return;\n    }\n    newObj = createStringObjectFromLongDouble(value,1);\n    if (o)\n        dbOverwrite(c->db,c->argv[1],newObj);\n    else\n        dbAdd(c->db,c->argv[1],newObj);\n    signalModifiedKey(c,c->db,c->argv[1]);\n    notifyKeyspaceEvent(NOTIFY_STRING,\"incrbyfloat\",c->argv[1],c->db->id);\n    g_pserver->dirty++;\n    addReplyBulk(c,newObj);\n\n    /* Always replicate INCRBYFLOAT as a SET command with the final value\n     * in order to make sure that differences in float precision or formatting\n     * will not create differences in replicas or after an AOF restart. */\n    rewriteClientCommandArgument(c,0,shared.set);\n    rewriteClientCommandArgument(c,2,newObj);\n    rewriteClientCommandArgument(c,3,shared.keepttl);\n}\n\nvoid appendCommand(client *c) {\n    size_t totlen;\n    robj *o, *append;\n\n    o = lookupKeyWrite(c->db,c->argv[1]);\n    if (o == NULL) {\n        /* Create the key */\n        c->argv[2] = tryObjectEncoding(c->argv[2]);\n        dbAdd(c->db,c->argv[1],c->argv[2]);\n        incrRefCount(c->argv[2]);\n        totlen = stringObjectLen(c->argv[2]);\n    } else {\n        /* Key exists, check type */\n        if (checkType(c,o,OBJ_STRING))\n            return;\n\n        /* \"append\" is an argument, so always an sds */\n        append = c->argv[2];\n        if (checkStringLength(c,stringObjectLen(o),sdslen((sds)ptrFromObj(append))) != C_OK)\n            return;\n\n        /* Append the value */\n        o = dbUnshareStringValue(c->db,c->argv[1],o);\n        o->m_ptr = sdscatlen((sds)ptrFromObj(o),ptrFromObj(append),sdslen((sds)ptrFromObj(append)));\n        totlen = sdslen((sds)ptrFromObj(o));\n    }\n    signalModifiedKey(c,c->db,c->argv[1]);\n    notifyKeyspaceEvent(NOTIFY_STRING,\"append\",c->argv[1],c->db->id);\n    g_pserver->dirty++;\n    addReplyLongLong(c,totlen);\n}\n\nvoid strlenCommand(client *c) {\n    robj_roptr o;\n    if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == nullptr ||\n        checkType(c,o,OBJ_STRING)) return;\n    addReplyLongLong(c,stringObjectLen(o));\n}\n\n\n/* STRALGO -- Implement complex algorithms on strings.\n *\n * STRALGO <algorithm> ... arguments ... */\nvoid stralgoLCS(client *c);     /* This implements the LCS algorithm. */\nvoid stralgoCommand(client *c) {\n    /* Select the algorithm. */\n    if (!strcasecmp(szFromObj(c->argv[1]),\"lcs\")) {\n        stralgoLCS(c);\n    } else {\n        addReplyErrorObject(c,shared.syntaxerr);\n    }\n}\n\n/* STRALGO <algo> [IDX] [LEN] [MINMATCHLEN <len>] [WITHMATCHLEN]\n *     STRINGS <string> <string> | KEYS <keya> <keyb>\n */\nvoid stralgoLCS(client *c) {\n    uint32_t i, j;\n    long long minmatchlen = 0;\n    const char *a = NULL, *b = NULL;\n    int getlen = 0, getidx = 0, withmatchlen = 0;\n    robj_roptr obja = nullptr, objb = nullptr;\n    uint32_t arraylen = 0;  /* Number of ranges emitted in the array. */\n    int computelcs;\n\n    for (j = 2; j < (uint32_t)c->argc; j++) {\n        char *opt = szFromObj(c->argv[j]);\n        int moreargs = (c->argc-1) - j;\n\n        if (!strcasecmp(opt,\"IDX\")) {\n            getidx = 1;\n        } else if (!strcasecmp(opt,\"LEN\")) {\n            getlen = 1;\n        } else if (!strcasecmp(opt,\"WITHMATCHLEN\")) {\n            withmatchlen = 1;\n        } else if (!strcasecmp(opt,\"MINMATCHLEN\") && moreargs) {\n            if (getLongLongFromObjectOrReply(c,c->argv[j+1],&minmatchlen,NULL)\n                != C_OK) goto cleanup;\n            if (minmatchlen < 0) minmatchlen = 0;\n            j++;\n        } else if (!strcasecmp(opt,\"STRINGS\") && moreargs > 1) {\n            if (a != NULL) {\n                addReplyError(c,\"Either use STRINGS or KEYS\");\n                goto cleanup;\n            }\n            a = szFromObj(c->argv[j+1]);\n            b = szFromObj(c->argv[j+2]);\n            j += 2;\n        } else if (!strcasecmp(opt,\"KEYS\") && moreargs > 1) {\n            if (a != NULL) {\n                addReplyError(c,\"Either use STRINGS or KEYS\");\n                goto cleanup;\n            }\n            obja = lookupKeyRead(c->db,c->argv[j+1]);\n            objb = lookupKeyRead(c->db,c->argv[j+2]);\n            if ((obja && obja->type != OBJ_STRING) ||\n                (objb && objb->type != OBJ_STRING))\n            {\n                addReplyError(c,\n                    \"The specified keys must contain string values\");\n                /* Don't cleanup the objects, we need to do that\n                 * only after calling getDecodedObject(). */\n                obja = NULL;\n                objb = NULL;\n                goto cleanup;\n            }\n            obja = obja ? getDecodedObject(obja) : createStringObject(\"\",0);\n            objb = objb ? getDecodedObject(objb) : createStringObject(\"\",0);\n            a = szFromObj(obja);\n            b = szFromObj(objb);\n            j += 2;\n        } else {\n            addReplyErrorObject(c,shared.syntaxerr);\n            goto cleanup;\n        }\n    }\n\n    /* Complain if the user passed ambiguous parameters. */\n    if (a == NULL) {\n        addReplyError(c,\"Please specify two strings: \"\n                        \"STRINGS or KEYS options are mandatory\");\n        goto cleanup;\n    } else if (getlen && getidx) {\n        addReplyError(c,\n            \"If you want both the length and indexes, please \"\n            \"just use IDX.\");\n        goto cleanup;\n    }\n\n    {   // Scope variables below for the goto\n    \n    /* Detect string truncation or later overflows. */\n    if (sdslen(a) >= UINT32_MAX-1 || sdslen(b) >= UINT32_MAX-1) {\n        addReplyError(c, \"String too long for LCS\");\n        goto cleanup;\n    }\n\n    /* Compute the LCS using the vanilla dynamic programming technique of\n     * building a table of LCS(x,y) substrings. */\n    uint32_t alen = sdslen(a);\n    uint32_t blen = sdslen(b);\n\n    /* Setup an uint32_t array to store at LCS[i,j] the length of the\n     * LCS A0..i-1, B0..j-1. Note that we have a linear array here, so\n     * we index it as LCS[j+(blen+1)*j] */\n    #define LCS(A,B) lcs[(B)+((A)*(blen+1))]\n\n    /* Try to allocate the LCS table, and abort on overflow or insufficient memory. */\n    unsigned long long lcssize = (unsigned long long)(alen+1)*(blen+1); /* Can't overflow due to the size limits above. */\n    unsigned long long lcsalloc = lcssize * sizeof(uint32_t);\n    uint32_t *lcs = NULL;\n    if (lcsalloc < SIZE_MAX && lcsalloc / lcssize == sizeof(uint32_t))\n        lcs = (uint32_t *)ztrymalloc(lcsalloc);\n    if (!lcs) {\n        addReplyError(c, \"Insufficient memory\");\n        goto cleanup;\n    }\n\n    /* Start building the LCS table. */\n    for (uint32_t i = 0; i <= alen; i++) {\n        for (uint32_t j = 0; j <= blen; j++) {\n            if (i == 0 || j == 0) {\n                /* If one substring has length of zero, the\n                 * LCS length is zero. */\n                LCS(i,j) = 0;\n            } else if (a[i-1] == b[j-1]) {\n                /* The len LCS (and the LCS itself) of two\n                 * sequences with the same final character, is the\n                 * LCS of the two sequences without the last char\n                 * plus that last char. */\n                LCS(i,j) = LCS(i-1,j-1)+1;\n            } else {\n                /* If the last character is different, take the longest\n                 * between the LCS of the first string and the second\n                 * minus the last char, and the reverse. */\n                uint32_t lcs1 = LCS(i-1,j);\n                uint32_t lcs2 = LCS(i,j-1);\n                LCS(i,j) = lcs1 > lcs2 ? lcs1 : lcs2;\n            }\n        }\n    }\n\n    /* Store the actual LCS string in \"result\" if needed. We create\n     * it backward, but the length is already known, we store it into idx. */\n    uint32_t idx = LCS(alen,blen);\n    sds result = NULL;        /* Resulting LCS string. */\n    void *arraylenptr = NULL; /* Deffered length of the array for IDX. */\n    uint32_t arange_start = alen, /* alen signals that values are not set. */\n             arange_end = 0,\n             brange_start = 0,\n             brange_end = 0;\n\n    /* Do we need to compute the actual LCS string? Allocate it in that case. */\n    computelcs = getidx || !getlen;\n    if (computelcs) result = sdsnewlen(SDS_NOINIT,idx);\n\n    /* Start with a deferred array if we have to emit the ranges. */\n    arraylen = 0;\n    if (getidx) {\n        addReplyMapLen(c,2);\n        addReplyBulkCString(c,\"matches\");\n        arraylenptr = addReplyDeferredLen(c);\n    }\n\n    i = alen, j = blen;\n    while (computelcs && i > 0 && j > 0) {\n        int emit_range = 0;\n        if (a[i-1] == b[j-1]) {\n            /* If there is a match, store the character and reduce\n             * the indexes to look for a new match. */\n            result[idx-1] = a[i-1];\n\n            /* Track the current range. */\n            if (arange_start == alen) {\n                arange_start = i-1;\n                arange_end = i-1;\n                brange_start = j-1;\n                brange_end = j-1;\n            } else {\n                /* Let's see if we can extend the range backward since\n                 * it is contiguous. */\n                if (arange_start == i && brange_start == j) {\n                    arange_start--;\n                    brange_start--;\n                } else {\n                    emit_range = 1;\n                }\n            }\n            /* Emit the range if we matched with the first byte of\n             * one of the two strings. We'll exit the loop ASAP. */\n            if (arange_start == 0 || brange_start == 0) emit_range = 1;\n            idx--; i--; j--;\n        } else {\n            /* Otherwise reduce i and j depending on the largest\n             * LCS between, to understand what direction we need to go. */\n            uint32_t lcs1 = LCS(i-1,j);\n            uint32_t lcs2 = LCS(i,j-1);\n            if (lcs1 > lcs2)\n                i--;\n            else\n                j--;\n            if (arange_start != alen) emit_range = 1;\n        }\n\n        /* Emit the current range if needed. */\n        uint32_t match_len = arange_end - arange_start + 1;\n        if (emit_range) {\n            if (minmatchlen == 0 || match_len >= minmatchlen) {\n                if (arraylenptr) {\n                    addReplyArrayLen(c,2+withmatchlen);\n                    addReplyArrayLen(c,2);\n                    addReplyLongLong(c,arange_start);\n                    addReplyLongLong(c,arange_end);\n                    addReplyArrayLen(c,2);\n                    addReplyLongLong(c,brange_start);\n                    addReplyLongLong(c,brange_end);\n                    if (withmatchlen) addReplyLongLong(c,match_len);\n                    arraylen++;\n                }\n            }\n            arange_start = alen; /* Restart at the next match. */\n        }\n    }\n\n    /* Signal modified key, increment dirty, ... */\n\n    /* Reply depending on the given options. */\n    if (arraylenptr) {\n        addReplyBulkCString(c,\"len\");\n        addReplyLongLong(c,LCS(alen,blen));\n        setDeferredArrayLen(c,arraylenptr,arraylen);\n    } else if (getlen) {\n        addReplyLongLong(c,LCS(alen,blen));\n    } else {\n        addReplyBulkSds(c,result);\n        result = NULL;\n    }\n\n    /* Cleanup. */\n    sdsfree(result);\n    zfree(lcs);\n    }   // END GOTO Scope\n\ncleanup:\n    if (obja) decrRefCount(obja);\n    if (objb) decrRefCount(objb);\n    return;\n}\n\n"
  },
  {
    "path": "src/t_zset.cpp",
    "content": "/*\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * Copyright (c) 2009-2012, Pieter Noordhuis <pcnoordhuis at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n/*-----------------------------------------------------------------------------\n * Sorted set API\n *----------------------------------------------------------------------------*/\n\n/* ZSETs are ordered sets using two data structures to hold the same elements\n * in order to get O(log(N)) INSERT and REMOVE operations into a sorted\n * data structure.\n *\n * The elements are added to a hash table mapping Redis objects to scores.\n * At the same time the elements are added to a skip list mapping scores\n * to Redis objects (so objects are sorted by scores in this \"view\").\n *\n * Note that the SDS string representing the element is the same in both\n * the hash table and skiplist in order to save memory. What we do in order\n * to manage the shared SDS string more easily is to free the SDS string\n * only in zslFreeNode(). The dictionary has no value free method set.\n * So we should always remove an element from the dictionary, and later from\n * the skiplist.\n *\n * This skiplist implementation is almost a C translation of the original\n * algorithm described by William Pugh in \"Skip Lists: A Probabilistic\n * Alternative to Balanced Trees\", modified in three ways:\n * a) this implementation allows for repeated scores.\n * b) the comparison is not just by key (our 'score') but by satellite data.\n * c) there is a back pointer, so it's a doubly linked list with the back\n * pointers being only at \"level 1\". This allows to traverse the list\n * from tail to head, useful for ZREVRANGE. */\n\n#include \"server.h\"\n#include <math.h>\n\n/*-----------------------------------------------------------------------------\n * Skiplist implementation of the low level API\n *----------------------------------------------------------------------------*/\n\nint zslLexValueGteMin(sds value, zlexrangespec *spec);\nint zslLexValueLteMax(sds value, zlexrangespec *spec);\n\n/* Create a skiplist node with the specified number of levels.\n * The SDS string 'ele' is referenced by the node after the call. */\nzskiplistNode *zslCreateNode(int level, double score, sds ele) {\n    zskiplistNode *zn = (zskiplistNode*)\n        zmalloc(sizeof(*zn)+level*sizeof(struct zskiplistLevel), MALLOC_SHARED);\n    zn->score = score;\n    zn->ele = ele;\n    return zn;\n}\n\n/* Create a new skiplist. */\nzskiplist *zslCreate(void) {\n    int j;\n    zskiplist *zsl;\n\n    zsl = (zskiplist*)zmalloc(sizeof(*zsl), MALLOC_SHARED);\n    zsl->level = 1;\n    zsl->length = 0;\n    zsl->header = zslCreateNode(ZSKIPLIST_MAXLEVEL,0,NULL);\n    for (j = 0; j < ZSKIPLIST_MAXLEVEL; j++) {\n        zsl->header->level(j)->forward = NULL;\n        zsl->header->level(j)->span = 0;\n    }\n    zsl->header->backward = NULL;\n    zsl->tail = NULL;\n    return zsl;\n}\n\n/* Free the specified skiplist node. The referenced SDS string representation\n * of the element is freed too, unless node->ele is set to NULL before calling\n * this function. */\nvoid zslFreeNode(zskiplistNode *node) {\n    sdsfree(node->ele);\n    zfree(node);\n}\n\n/* Free a whole skiplist. */\nvoid zslFree(zskiplist *zsl) {\n    zskiplistNode *node = zsl->header->level(0)->forward, *next;\n\n    zfree(zsl->header);\n    while(node) {\n        next = node->level(0)->forward;\n        zslFreeNode(node);\n        node = next;\n    }\n    zfree(zsl);\n}\n\n/* Returns a random level for the new skiplist node we are going to create.\n * The return value of this function is between 1 and ZSKIPLIST_MAXLEVEL\n * (both inclusive), with a powerlaw-alike distribution where higher\n * levels are less likely to be returned. */\nint zslRandomLevel(void) {\n    int level = 1;\n    while ((random()&0xFFFF) < (ZSKIPLIST_P * 0xFFFF))\n        level += 1;\n    return (level<ZSKIPLIST_MAXLEVEL) ? level : ZSKIPLIST_MAXLEVEL;\n}\n\n/* Insert a new node in the skiplist. Assumes the element does not already\n * exist (up to the caller to enforce that). The skiplist takes ownership\n * of the passed SDS string 'ele'. */\nzskiplistNode *zslInsert(zskiplist *zsl, double score, sds ele) {\n    zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;\n    unsigned int rank[ZSKIPLIST_MAXLEVEL];\n    int i, level;\n\n    serverAssert(!std::isnan(score));\n    x = zsl->header;\n    for (i = zsl->level-1; i >= 0; i--) {\n        /* store rank that is crossed to reach the insert position */\n        rank[i] = i == (zsl->level-1) ? 0 : rank[i+1];\n        while (x->level(i)->forward &&\n                (x->level(i)->forward->score < score ||\n                    (x->level(i)->forward->score == score &&\n                    sdscmp(x->level(i)->forward->ele,ele) < 0)))\n        {\n            rank[i] += x->level(i)->span;\n            x = x->level(i)->forward;\n        }\n        update[i] = x;\n    }\n    /* we assume the element is not already inside, since we allow duplicated\n     * scores, reinserting the same element should never happen since the\n     * caller of zslInsert() should test in the hash table if the element is\n     * already inside or not. */\n    level = zslRandomLevel();\n    if (level > zsl->level) {\n        for (i = zsl->level; i < level; i++) {\n            rank[i] = 0;\n            update[i] = zsl->header;\n            update[i]->level(i)->span = zsl->length;\n        }\n        zsl->level = level;\n    }\n    x = zslCreateNode(level,score,ele);\n    for (i = 0; i < level; i++) {\n        x->level(i)->forward = update[i]->level(i)->forward;\n        update[i]->level(i)->forward = x;\n\n        /* update span covered by update[i] as x is inserted here */\n        x->level(i)->span = update[i]->level(i)->span - (rank[0] - rank[i]);\n        update[i]->level(i)->span = (rank[0] - rank[i]) + 1;\n    }\n\n    /* increment span for untouched levels */\n    for (i = level; i < zsl->level; i++) {\n        update[i]->level(i)->span++;\n    }\n\n    x->backward = (update[0] == zsl->header) ? NULL : update[0];\n    if (x->level(0)->forward)\n        x->level(0)->forward->backward = x;\n    else\n        zsl->tail = x;\n    zsl->length++;\n    return x;\n}\n\n/* Internal function used by zslDelete, zslDeleteRangeByScore and\n * zslDeleteRangeByRank. */\nvoid zslDeleteNode(zskiplist *zsl, zskiplistNode *x, zskiplistNode **update) {\n    int i;\n    for (i = 0; i < zsl->level; i++) {\n        if (update[i]->level(i)->forward == x) {\n            update[i]->level(i)->span += x->level(i)->span - 1;\n            update[i]->level(i)->forward = x->level(i)->forward;\n        } else {\n            update[i]->level(i)->span -= 1;\n        }\n    }\n    if (x->level(0)->forward) {\n        x->level(0)->forward->backward = x->backward;\n    } else {\n        zsl->tail = x->backward;\n    }\n    while(zsl->level > 1 && zsl->header->level(zsl->level-1)->forward == NULL)\n        zsl->level--;\n    zsl->length--;\n}\n\n/* Delete an element with matching score/element from the skiplist.\n * The function returns 1 if the node was found and deleted, otherwise\n * 0 is returned.\n *\n * If 'node' is NULL the deleted node is freed by zslFreeNode(), otherwise\n * it is not freed (but just unlinked) and *node is set to the node pointer,\n * so that it is possible for the caller to reuse the node (including the\n * referenced SDS string at node->ele). */\nint zslDelete(zskiplist *zsl, double score, sds ele, zskiplistNode **node) {\n    zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;\n    int i;\n\n    x = zsl->header;\n    for (i = zsl->level-1; i >= 0; i--) {\n        while (x->level(i)->forward &&\n                (x->level(i)->forward->score < score ||\n                    (x->level(i)->forward->score == score &&\n                     sdscmp(x->level(i)->forward->ele,ele) < 0)))\n        {\n            x = x->level(i)->forward;\n        }\n        update[i] = x;\n    }\n    /* We may have multiple elements with the same score, what we need\n     * is to find the element with both the right score and object. */\n    x = x->level(0)->forward;\n    if (x && score == x->score && sdscmp(x->ele,ele) == 0) {\n        zslDeleteNode(zsl, x, update);\n        if (!node)\n            zslFreeNode(x);\n        else\n            *node = x;\n        return 1;\n    }\n    return 0; /* not found */\n}\n\n/* Update the score of an element inside the sorted set skiplist.\n * Note that the element must exist and must match 'score'.\n * This function does not update the score in the hash table side, the\n * caller should take care of it.\n *\n * Note that this function attempts to just update the node, in case after\n * the score update, the node would be exactly at the same position.\n * Otherwise the skiplist is modified by removing and re-adding a new\n * element, which is more costly.\n *\n * The function returns the updated element skiplist node pointer. */\nzskiplistNode *zslUpdateScore(zskiplist *zsl, double curscore, sds ele, double newscore) {\n    zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;\n    int i;\n\n    /* We need to seek to element to update to start: this is useful anyway,\n     * we'll have to update or remove it. */\n    x = zsl->header;\n    for (i = zsl->level-1; i >= 0; i--) {\n        while (x->level(i)->forward &&\n                (x->level(i)->forward->score < curscore ||\n                    (x->level(i)->forward->score == curscore &&\n                     sdscmp(x->level(i)->forward->ele,ele) < 0)))\n        {\n            x = x->level(i)->forward;\n        }\n        update[i] = x;\n    }\n\n    /* Jump to our element: note that this function assumes that the\n     * element with the matching score exists. */\n    x = x->level(0)->forward;\n    serverAssert(x && curscore == x->score && sdscmp(x->ele,ele) == 0);\n\n    /* If the node, after the score update, would be still exactly\n     * at the same position, we can just update the score without\n     * actually removing and re-inserting the element in the skiplist. */\n    if ((x->backward == NULL || x->backward->score < newscore) &&\n        (x->level(0)->forward == NULL || x->level(0)->forward->score > newscore))\n    {\n        x->score = newscore;\n        return x;\n    }\n\n    /* No way to reuse the old node: we need to remove and insert a new\n     * one at a different place. */\n    zslDeleteNode(zsl, x, update);\n    zskiplistNode *newnode = zslInsert(zsl,newscore,x->ele);\n    /* We reused the old node x->ele SDS string, free the node now\n     * since zslInsert created a new one. */\n    x->ele = NULL;\n    zslFreeNode(x);\n    return newnode;\n}\n\nint zslValueGteMin(double value, zrangespec *spec) {\n    return spec->minex ? (value > spec->min) : (value >= spec->min);\n}\n\nint zslValueLteMax(double value, zrangespec *spec) {\n    return spec->maxex ? (value < spec->max) : (value <= spec->max);\n}\n\n/* Returns if there is a part of the zset is in range. */\nint zslIsInRange(zskiplist *zsl, zrangespec *range) {\n    zskiplistNode *x;\n\n    /* Test for ranges that will always be empty. */\n    if (range->min > range->max ||\n            (range->min == range->max && (range->minex || range->maxex)))\n        return 0;\n    x = zsl->tail;\n    if (x == NULL || !zslValueGteMin(x->score,range))\n        return 0;\n    x = zsl->header->level(0)->forward;\n    if (x == NULL || !zslValueLteMax(x->score,range))\n        return 0;\n    return 1;\n}\n\n/* Find the first node that is contained in the specified range.\n * Returns NULL when no element is contained in the range. */\nzskiplistNode *zslFirstInRange(zskiplist *zsl, zrangespec *range) {\n    zskiplistNode *x;\n    int i;\n\n    /* If everything is out of range, return early. */\n    if (!zslIsInRange(zsl,range)) return NULL;\n\n    x = zsl->header;\n    for (i = zsl->level-1; i >= 0; i--) {\n        /* Go forward while *OUT* of range. */\n        while (x->level(i)->forward &&\n            !zslValueGteMin(x->level(i)->forward->score,range))\n                x = x->level(i)->forward;\n    }\n\n    /* This is an inner range, so the next node cannot be NULL. */\n    x = x->level(0)->forward;\n    serverAssert(x != NULL);\n\n    /* Check if score <= max. */\n    if (!zslValueLteMax(x->score,range)) return NULL;\n    return x;\n}\n\n/* Find the last node that is contained in the specified range.\n * Returns NULL when no element is contained in the range. */\nzskiplistNode *zslLastInRange(zskiplist *zsl, zrangespec *range) {\n    zskiplistNode *x;\n    int i;\n\n    /* If everything is out of range, return early. */\n    if (!zslIsInRange(zsl,range)) return NULL;\n\n    x = zsl->header;\n    for (i = zsl->level-1; i >= 0; i--) {\n        /* Go forward while *IN* range. */\n        while (x->level(i)->forward &&\n            zslValueLteMax(x->level(i)->forward->score,range))\n                x = x->level(i)->forward;\n    }\n\n    /* This is an inner range, so this node cannot be NULL. */\n    serverAssert(x != NULL);\n\n    /* Check if score >= min. */\n    if (!zslValueGteMin(x->score,range)) return NULL;\n    return x;\n}\n\n/* Delete all the elements with score between min and max from the skiplist.\n * Both min and max can be inclusive or exclusive (see range->minex and\n * range->maxex). When inclusive a score >= min && score <= max is deleted.\n * Note that this function takes the reference to the hash table view of the\n * sorted set, in order to remove the elements from the hash table too. */\nunsigned long zslDeleteRangeByScore(zskiplist *zsl, zrangespec *range, dict *dict) {\n    zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;\n    unsigned long removed = 0;\n    int i;\n\n    x = zsl->header;\n    for (i = zsl->level-1; i >= 0; i--) {\n        while (x->level(i)->forward &&\n            !zslValueGteMin(x->level(i)->forward->score, range))\n                x = x->level(i)->forward;\n        update[i] = x;\n    }\n\n    /* Current node is the last with score < or <= min. */\n    x = x->level(0)->forward;\n\n    /* Delete nodes while in range. */\n    while (x && zslValueLteMax(x->score, range)) {\n        zskiplistNode *next = x->level(0)->forward;\n        zslDeleteNode(zsl,x,update);\n        dictDelete(dict,x->ele);\n        zslFreeNode(x); /* Here is where x->ele is actually released. */\n        removed++;\n        x = next;\n    }\n    return removed;\n}\n\nunsigned long zslDeleteRangeByLex(zskiplist *zsl, zlexrangespec *range, dict *dict) {\n    zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;\n    unsigned long removed = 0;\n    int i;\n\n\n    x = zsl->header;\n    for (i = zsl->level-1; i >= 0; i--) {\n        while (x->level(i)->forward &&\n            !zslLexValueGteMin(x->level(i)->forward->ele,range))\n                x = x->level(i)->forward;\n        update[i] = x;\n    }\n\n    /* Current node is the last with score < or <= min. */\n    x = x->level(0)->forward;\n\n    /* Delete nodes while in range. */\n    while (x && zslLexValueLteMax(x->ele,range)) {\n        zskiplistNode *next = x->level(0)->forward;\n        zslDeleteNode(zsl,x,update);\n        dictDelete(dict,x->ele);\n        zslFreeNode(x); /* Here is where x->ele is actually released. */\n        removed++;\n        x = next;\n    }\n    return removed;\n}\n\n/* Delete all the elements with rank between start and end from the skiplist.\n * Start and end are inclusive. Note that start and end need to be 1-based */\nunsigned long zslDeleteRangeByRank(zskiplist *zsl, unsigned int start, unsigned int end, dict *dict) {\n    zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;\n    unsigned long traversed = 0, removed = 0;\n    int i;\n\n    x = zsl->header;\n    for (i = zsl->level-1; i >= 0; i--) {\n        while (x->level(i)->forward && (traversed + x->level(i)->span) < start) {\n            traversed += x->level(i)->span;\n            x = x->level(i)->forward;\n        }\n        update[i] = x;\n    }\n\n    traversed++;\n    x = x->level(0)->forward;\n    while (x && traversed <= end) {\n        zskiplistNode *next = x->level(0)->forward;\n        zslDeleteNode(zsl,x,update);\n        dictDelete(dict,x->ele);\n        zslFreeNode(x);\n        removed++;\n        traversed++;\n        x = next;\n    }\n    return removed;\n}\n\n/* Find the rank for an element by both score and key.\n * Returns 0 when the element cannot be found, rank otherwise.\n * Note that the rank is 1-based due to the span of zsl->header to the\n * first element. */\nunsigned long zslGetRank(zskiplist *zsl, double score, sds ele) {\n    zskiplistNode *x;\n    unsigned long rank = 0;\n    int i;\n\n    x = zsl->header;\n    for (i = zsl->level-1; i >= 0; i--) {\n        while (x->level(i)->forward &&\n            (x->level(i)->forward->score < score ||\n                (x->level(i)->forward->score == score &&\n                sdscmp(x->level(i)->forward->ele,ele) <= 0))) {\n            rank += x->level(i)->span;\n            x = x->level(i)->forward;\n        }\n\n        /* x might be equal to zsl->header, so test if obj is non-NULL */\n        if (x->ele && sdscmp(x->ele,ele) == 0) {\n            return rank;\n        }\n    }\n    return 0;\n}\n\n/* Finds an element by its rank. The rank argument needs to be 1-based. */\nzskiplistNode* zslGetElementByRank(zskiplist *zsl, unsigned long rank) {\n    zskiplistNode *x;\n    unsigned long traversed = 0;\n    int i;\n\n    x = zsl->header;\n    for (i = zsl->level-1; i >= 0; i--) {\n        while (x->level(i)->forward && (traversed + x->level(i)->span) <= rank)\n        {\n            traversed += x->level(i)->span;\n            x = x->level(i)->forward;\n        }\n        if (traversed == rank) {\n            return x;\n        }\n    }\n    return NULL;\n}\n\n/* Populate the rangespec according to the objects min and max. */\nstatic int zslParseRange(robj *min, robj *max, zrangespec *spec) {\n    char *eptr;\n    spec->minex = spec->maxex = 0;\n\n    /* Parse the min-max interval. If one of the values is prefixed\n     * by the \"(\" character, it's considered \"open\". For instance\n     * ZRANGEBYSCORE zset (1.5 (2.5 will match min < x < max\n     * ZRANGEBYSCORE zset 1.5 2.5 will instead match min <= x <= max */\n    if (min->encoding == OBJ_ENCODING_INT) {\n        spec->min = (long)min->m_ptr;\n    } else {\n        if (((char*)ptrFromObj(min))[0] == '(') {\n            spec->min = strtod((char*)ptrFromObj(min)+1,&eptr);\n            if (eptr[0] != '\\0' || std::isnan(spec->min)) return C_ERR;\n            spec->minex = 1;\n        } else {\n            spec->min = strtod((char*)ptrFromObj(min),&eptr);\n            if (eptr[0] != '\\0' || std::isnan(spec->min)) return C_ERR;\n        }\n    }\n    if (max->encoding == OBJ_ENCODING_INT) {\n        spec->max = (long)max->m_ptr;\n    } else {\n        if (((char*)ptrFromObj(max))[0] == '(') {\n            spec->max = strtod((char*)ptrFromObj(max)+1,&eptr);\n            if (eptr[0] != '\\0' || std::isnan(spec->max)) return C_ERR;\n            spec->maxex = 1;\n        } else {\n            spec->max = strtod((char*)ptrFromObj(max),&eptr);\n            if (eptr[0] != '\\0' || std::isnan(spec->max)) return C_ERR;\n        }\n    }\n\n    return C_OK;\n}\n\n/* ------------------------ Lexicographic ranges ---------------------------- */\n\n/* Parse max or min argument of ZRANGEBYLEX.\n  * (foo means foo (open interval)\n  * [foo means foo (closed interval)\n  * - means the min string possible\n  * + means the max string possible\n  *\n  * If the string is valid the *dest pointer is set to the redis object\n  * that will be used for the comparison, and ex will be set to 0 or 1\n  * respectively if the item is exclusive or inclusive. C_OK will be\n  * returned.\n  *\n  * If the string is not a valid range C_ERR is returned, and the value\n  * of *dest and *ex is undefined. */\nint zslParseLexRangeItem(robj *item, sds *dest, int *ex) {\n    char *c = szFromObj(item);\n\n    switch(c[0]) {\n    case '+':\n        if (c[1] != '\\0') return C_ERR;\n        *ex = 1;\n        *dest = shared.maxstring;\n        return C_OK;\n    case '-':\n        if (c[1] != '\\0') return C_ERR;\n        *ex = 1;\n        *dest = shared.minstring;\n        return C_OK;\n    case '(':\n        *ex = 1;\n        *dest = sdsnewlen(c+1,sdslen(c)-1);\n        return C_OK;\n    case '[':\n        *ex = 0;\n        *dest = sdsnewlen(c+1,sdslen(c)-1);\n        return C_OK;\n    default:\n        return C_ERR;\n    }\n}\n\n/* Free a lex range structure, must be called only after zelParseLexRange()\n * populated the structure with success (C_OK returned). */\nvoid zslFreeLexRange(zlexrangespec *spec) {\n    if (spec->min != shared.minstring &&\n        spec->min != shared.maxstring) sdsfree(spec->min);\n    if (spec->max != shared.minstring &&\n        spec->max != shared.maxstring) sdsfree(spec->max);\n}\n\n/* Populate the lex rangespec according to the objects min and max.\n *\n * Return C_OK on success. On error C_ERR is returned.\n * When OK is returned the structure must be freed with zslFreeLexRange(),\n * otherwise no release is needed. */\nint zslParseLexRange(robj *min, robj *max, zlexrangespec *spec) {\n    /* The range can't be valid if objects are integer encoded.\n     * Every item must start with ( or [. */\n    if (min->encoding == OBJ_ENCODING_INT ||\n        max->encoding == OBJ_ENCODING_INT) return C_ERR;\n\n    spec->min = spec->max = NULL;\n    if (zslParseLexRangeItem(min, &spec->min, &spec->minex) == C_ERR ||\n        zslParseLexRangeItem(max, &spec->max, &spec->maxex) == C_ERR) {\n        zslFreeLexRange(spec);\n        return C_ERR;\n    } else {\n        return C_OK;\n    }\n}\n\n/* This is just a wrapper to sdscmp() that is able to\n * handle shared.minstring and shared.maxstring as the equivalent of\n * -inf and +inf for strings */\nint sdscmplex(sds a, sds b) {\n    if (a == b) return 0;\n    if (a == shared.minstring || b == shared.maxstring) return -1;\n    if (a == shared.maxstring || b == shared.minstring) return 1;\n    return sdscmp(a,b);\n}\n\nint zslLexValueGteMin(sds value, zlexrangespec *spec) {\n    return spec->minex ?\n        (sdscmplex(value,spec->min) > 0) :\n        (sdscmplex(value,spec->min) >= 0);\n}\n\nint zslLexValueLteMax(sds value, zlexrangespec *spec) {\n    return spec->maxex ?\n        (sdscmplex(value,spec->max) < 0) :\n        (sdscmplex(value,spec->max) <= 0);\n}\n\n/* Returns if there is a part of the zset is in the lex range. */\nint zslIsInLexRange(zskiplist *zsl, zlexrangespec *range) {\n    zskiplistNode *x;\n\n    /* Test for ranges that will always be empty. */\n    int cmp = sdscmplex(range->min,range->max);\n    if (cmp > 0 || (cmp == 0 && (range->minex || range->maxex)))\n        return 0;\n    x = zsl->tail;\n    if (x == NULL || !zslLexValueGteMin(x->ele,range))\n        return 0;\n    x = zsl->header->level(0)->forward;\n    if (x == NULL || !zslLexValueLteMax(x->ele,range))\n        return 0;\n    return 1;\n}\n\n/* Find the first node that is contained in the specified lex range.\n * Returns NULL when no element is contained in the range. */\nzskiplistNode *zslFirstInLexRange(zskiplist *zsl, zlexrangespec *range) {\n    zskiplistNode *x;\n    int i;\n\n    /* If everything is out of range, return early. */\n    if (!zslIsInLexRange(zsl,range)) return NULL;\n\n    x = zsl->header;\n    for (i = zsl->level-1; i >= 0; i--) {\n        /* Go forward while *OUT* of range. */\n        while (x->level(i)->forward &&\n            !zslLexValueGteMin(x->level(i)->forward->ele,range))\n                x = x->level(i)->forward;\n    }\n\n    /* This is an inner range, so the next node cannot be NULL. */\n    x = x->level(0)->forward;\n    serverAssert(x != NULL);\n\n    /* Check if score <= max. */\n    if (!zslLexValueLteMax(x->ele,range)) return NULL;\n    return x;\n}\n\n/* Find the last node that is contained in the specified range.\n * Returns NULL when no element is contained in the range. */\nzskiplistNode *zslLastInLexRange(zskiplist *zsl, zlexrangespec *range) {\n    zskiplistNode *x;\n    int i;\n\n    /* If everything is out of range, return early. */\n    if (!zslIsInLexRange(zsl,range)) return NULL;\n\n    x = zsl->header;\n    for (i = zsl->level-1; i >= 0; i--) {\n        /* Go forward while *IN* range. */\n        while (x->level(i)->forward &&\n            zslLexValueLteMax(x->level(i)->forward->ele,range))\n                x = x->level(i)->forward;\n    }\n\n    /* This is an inner range, so this node cannot be NULL. */\n    serverAssert(x != NULL);\n\n    /* Check if score >= min. */\n    if (!zslLexValueGteMin(x->ele,range)) return NULL;\n    return x;\n}\n\n/*-----------------------------------------------------------------------------\n * Ziplist-backed sorted set API\n *----------------------------------------------------------------------------*/\n\ndouble zzlStrtod(unsigned char *vstr, unsigned int vlen) {\n    char buf[128];\n    if (vlen > sizeof(buf))\n        vlen = sizeof(buf);\n    memcpy(buf,vstr,vlen);\n    buf[vlen] = '\\0';\n    return strtod(buf,NULL);\n }\n\ndouble zzlGetScore(unsigned char *sptr) {\n    unsigned char *vstr;\n    unsigned int vlen;\n    long long vlong;\n    double score;\n\n    serverAssert(sptr != NULL);\n    serverAssert(ziplistGet(sptr,&vstr,&vlen,&vlong));\n\n    if (vstr) {\n        score = zzlStrtod(vstr,vlen);\n    } else {\n        score = vlong;\n    }\n\n    return score;\n}\n\n/* Return a ziplist element as an SDS string. */\nsds ziplistGetObject(unsigned char *sptr) {\n    unsigned char *vstr;\n    unsigned int vlen;\n    long long vlong;\n\n    serverAssert(sptr != NULL);\n    serverAssert(ziplistGet(sptr,&vstr,&vlen,&vlong));\n\n    if (vstr) {\n        return sdsnewlen((char*)vstr,vlen);\n    } else {\n        return sdsfromlonglong(vlong);\n    }\n}\n\n/* Compare element in sorted set with given element. */\nint zzlCompareElements(unsigned char *eptr, unsigned char *cstr, unsigned int clen) {\n    unsigned char *vstr;\n    unsigned int vlen;\n    long long vlong;\n    unsigned char vbuf[32];\n    int minlen, cmp;\n\n    serverAssert(ziplistGet(eptr,&vstr,&vlen,&vlong));\n    if (vstr == NULL) {\n        /* Store string representation of long long in buf. */\n        vlen = ll2string((char*)vbuf,sizeof(vbuf),vlong);\n        vstr = vbuf;\n    }\n\n    minlen = (vlen < clen) ? vlen : clen;\n    cmp = memcmp(vstr,cstr,minlen);\n    if (cmp == 0) return vlen-clen;\n    return cmp;\n}\n\nunsigned int zzlLength(const unsigned char *zl) {\n    return ziplistLen(zl)/2;\n}\n\n/* Move to next entry based on the values in eptr and sptr. Both are set to\n * NULL when there is no next entry. */\nvoid zzlNext(unsigned char *zl, unsigned char **eptr, unsigned char **sptr) {\n    unsigned char *_eptr, *_sptr;\n    serverAssert(*eptr != NULL && *sptr != NULL);\n\n    _eptr = ziplistNext(zl,*sptr);\n    if (_eptr != NULL) {\n        _sptr = ziplistNext(zl,_eptr);\n        serverAssert(_sptr != NULL);\n    } else {\n        /* No next entry. */\n        _sptr = NULL;\n    }\n\n    *eptr = _eptr;\n    *sptr = _sptr;\n}\n\n/* Move to the previous entry based on the values in eptr and sptr. Both are\n * set to NULL when there is no next entry. */\nvoid zzlPrev(unsigned char *zl, unsigned char **eptr, unsigned char **sptr) {\n    unsigned char *_eptr, *_sptr;\n    serverAssert(*eptr != NULL && *sptr != NULL);\n\n    _sptr = ziplistPrev(zl,*eptr);\n    if (_sptr != NULL) {\n        _eptr = ziplistPrev(zl,_sptr);\n        serverAssert(_eptr != NULL);\n    } else {\n        /* No previous entry. */\n        _eptr = NULL;\n    }\n\n    *eptr = _eptr;\n    *sptr = _sptr;\n}\n\n/* Returns if there is a part of the zset is in range. Should only be used\n * internally by zzlFirstInRange and zzlLastInRange. */\nint zzlIsInRange(unsigned char *zl, zrangespec *range) {\n    unsigned char *p;\n    double score;\n\n    /* Test for ranges that will always be empty. */\n    if (range->min > range->max ||\n            (range->min == range->max && (range->minex || range->maxex)))\n        return 0;\n\n    p = ziplistIndex(zl,-1); /* Last score. */\n    if (p == NULL) return 0; /* Empty sorted set */\n    score = zzlGetScore(p);\n    if (!zslValueGteMin(score,range))\n        return 0;\n\n    p = ziplistIndex(zl,1); /* First score. */\n    serverAssert(p != NULL);\n    score = zzlGetScore(p);\n    if (!zslValueLteMax(score,range))\n        return 0;\n\n    return 1;\n}\n\n/* Find pointer to the first element contained in the specified range.\n * Returns NULL when no element is contained in the range. */\nunsigned char *zzlFirstInRange(unsigned char *zl, zrangespec *range) {\n    unsigned char *eptr = ziplistIndex(zl,0), *sptr;\n    double score;\n\n    /* If everything is out of range, return early. */\n    if (!zzlIsInRange(zl,range)) return NULL;\n\n    while (eptr != NULL) {\n        sptr = ziplistNext(zl,eptr);\n        serverAssert(sptr != NULL);\n\n        score = zzlGetScore(sptr);\n        if (zslValueGteMin(score,range)) {\n            /* Check if score <= max. */\n            if (zslValueLteMax(score,range))\n                return eptr;\n            return NULL;\n        }\n\n        /* Move to next element. */\n        eptr = ziplistNext(zl,sptr);\n    }\n\n    return NULL;\n}\n\n/* Find pointer to the last element contained in the specified range.\n * Returns NULL when no element is contained in the range. */\nunsigned char *zzlLastInRange(unsigned char *zl, zrangespec *range) {\n    unsigned char *eptr = ziplistIndex(zl,-2), *sptr;\n    double score;\n\n    /* If everything is out of range, return early. */\n    if (!zzlIsInRange(zl,range)) return NULL;\n\n    while (eptr != NULL) {\n        sptr = ziplistNext(zl,eptr);\n        serverAssert(sptr != NULL);\n\n        score = zzlGetScore(sptr);\n        if (zslValueLteMax(score,range)) {\n            /* Check if score >= min. */\n            if (zslValueGteMin(score,range))\n                return eptr;\n            return NULL;\n        }\n\n        /* Move to previous element by moving to the score of previous element.\n         * When this returns NULL, we know there also is no element. */\n        sptr = ziplistPrev(zl,eptr);\n        if (sptr != NULL)\n            serverAssert((eptr = ziplistPrev(zl,sptr)) != NULL);\n        else\n            eptr = NULL;\n    }\n\n    return NULL;\n}\n\nint zzlLexValueGteMin(unsigned char *p, zlexrangespec *spec) {\n    sds value = ziplistGetObject(p);\n    int res = zslLexValueGteMin(value,spec);\n    sdsfree(value);\n    return res;\n}\n\nint zzlLexValueLteMax(unsigned char *p, zlexrangespec *spec) {\n    sds value = ziplistGetObject(p);\n    int res = zslLexValueLteMax(value,spec);\n    sdsfree(value);\n    return res;\n}\n\n/* Returns if there is a part of the zset is in range. Should only be used\n * internally by zzlFirstInRange and zzlLastInRange. */\nint zzlIsInLexRange(unsigned char *zl, zlexrangespec *range) {\n    unsigned char *p;\n\n    /* Test for ranges that will always be empty. */\n    int cmp = sdscmplex(range->min,range->max);\n    if (cmp > 0 || (cmp == 0 && (range->minex || range->maxex)))\n        return 0;\n\n    p = ziplistIndex(zl,-2); /* Last element. */\n    if (p == NULL) return 0;\n    if (!zzlLexValueGteMin(p,range))\n        return 0;\n\n    p = ziplistIndex(zl,0); /* First element. */\n    serverAssert(p != NULL);\n    if (!zzlLexValueLteMax(p,range))\n        return 0;\n\n    return 1;\n}\n\n/* Find pointer to the first element contained in the specified lex range.\n * Returns NULL when no element is contained in the range. */\nunsigned char *zzlFirstInLexRange(unsigned char *zl, zlexrangespec *range) {\n    unsigned char *eptr = ziplistIndex(zl,0), *sptr;\n\n    /* If everything is out of range, return early. */\n    if (!zzlIsInLexRange(zl,range)) return NULL;\n\n    while (eptr != NULL) {\n        if (zzlLexValueGteMin(eptr,range)) {\n            /* Check if score <= max. */\n            if (zzlLexValueLteMax(eptr,range))\n                return eptr;\n            return NULL;\n        }\n\n        /* Move to next element. */\n        sptr = ziplistNext(zl,eptr); /* This element score. Skip it. */\n        serverAssert(sptr != NULL);\n        eptr = ziplistNext(zl,sptr); /* Next element. */\n    }\n\n    return NULL;\n}\n\n/* Find pointer to the last element contained in the specified lex range.\n * Returns NULL when no element is contained in the range. */\nunsigned char *zzlLastInLexRange(unsigned char *zl, zlexrangespec *range) {\n    unsigned char *eptr = ziplistIndex(zl,-2), *sptr;\n\n    /* If everything is out of range, return early. */\n    if (!zzlIsInLexRange(zl,range)) return NULL;\n\n    while (eptr != NULL) {\n        if (zzlLexValueLteMax(eptr,range)) {\n            /* Check if score >= min. */\n            if (zzlLexValueGteMin(eptr,range))\n                return eptr;\n            return NULL;\n        }\n\n        /* Move to previous element by moving to the score of previous element.\n         * When this returns NULL, we know there also is no element. */\n        sptr = ziplistPrev(zl,eptr);\n        if (sptr != NULL)\n            serverAssert((eptr = ziplistPrev(zl,sptr)) != NULL);\n        else\n            eptr = NULL;\n    }\n\n    return NULL;\n}\n\nunsigned char *zzlFind(unsigned char *zl, sds ele, double *score) {\n    unsigned char *eptr = ziplistIndex(zl,0), *sptr;\n\n    while (eptr != NULL) {\n        sptr = ziplistNext(zl,eptr);\n        serverAssert(sptr != NULL);\n\n        if (ziplistCompare(eptr,(unsigned char*)ele,sdslen(ele))) {\n            /* Matching element, pull out score. */\n            if (score != NULL) *score = zzlGetScore(sptr);\n            return eptr;\n        }\n\n        /* Move to next element. */\n        eptr = ziplistNext(zl,sptr);\n    }\n    return NULL;\n}\n\n/* Delete (element,score) pair from ziplist. Use local copy of eptr because we\n * don't want to modify the one given as argument. */\nunsigned char *zzlDelete(unsigned char *zl, unsigned char *eptr) {\n    unsigned char *p = eptr;\n\n    /* TODO: add function to ziplist API to delete N elements from offset. */\n    zl = ziplistDelete(zl,&p);\n    zl = ziplistDelete(zl,&p);\n    return zl;\n}\n\nunsigned char *zzlInsertAt(unsigned char *zl, unsigned char *eptr, sds ele, double score) {\n    unsigned char *sptr;\n    char scorebuf[128];\n    int scorelen;\n    size_t offset;\n\n    scorelen = d2string(scorebuf,sizeof(scorebuf),score);\n    if (eptr == NULL) {\n        zl = ziplistPush(zl,(unsigned char*)ele,sdslen(ele),ZIPLIST_TAIL);\n        zl = ziplistPush(zl,(unsigned char*)scorebuf,scorelen,ZIPLIST_TAIL);\n    } else {\n        /* Keep offset relative to zl, as it might be re-allocated. */\n        offset = eptr-zl;\n        zl = ziplistInsert(zl,eptr,(unsigned char*)ele,sdslen(ele));\n        eptr = zl+offset;\n\n        /* Insert score after the element. */\n        serverAssert((sptr = ziplistNext(zl,eptr)) != NULL);\n        zl = ziplistInsert(zl,sptr,(unsigned char*)scorebuf,scorelen);\n    }\n    return zl;\n}\n\n/* Insert (element,score) pair in ziplist. This function assumes the element is\n * not yet present in the list. */\nunsigned char *zzlInsert(unsigned char *zl, sds ele, double score) {\n    unsigned char *eptr = ziplistIndex(zl,0), *sptr;\n    double s;\n\n    while (eptr != NULL) {\n        sptr = ziplistNext(zl,eptr);\n        serverAssert(sptr != NULL);\n        s = zzlGetScore(sptr);\n\n        if (s > score) {\n            /* First element with score larger than score for element to be\n             * inserted. This means we should take its spot in the list to\n             * maintain ordering. */\n            zl = zzlInsertAt(zl,eptr,ele,score);\n            break;\n        } else if (s == score) {\n            /* Ensure lexicographical ordering for elements. */\n            if (zzlCompareElements(eptr,(unsigned char*)ele,sdslen(ele)) > 0) {\n                zl = zzlInsertAt(zl,eptr,ele,score);\n                break;\n            }\n        }\n\n        /* Move to next element. */\n        eptr = ziplistNext(zl,sptr);\n    }\n\n    /* Push on tail of list when it was not yet inserted. */\n    if (eptr == NULL)\n        zl = zzlInsertAt(zl,NULL,ele,score);\n    return zl;\n}\n\nunsigned char *zzlDeleteRangeByScore(unsigned char *zl, zrangespec *range, unsigned long *deleted) {\n    unsigned char *eptr, *sptr;\n    double score;\n    unsigned long num = 0;\n\n    if (deleted != NULL) *deleted = 0;\n\n    eptr = zzlFirstInRange(zl,range);\n    if (eptr == NULL) return zl;\n\n    /* When the tail of the ziplist is deleted, eptr will point to the sentinel\n     * byte and ziplistNext will return NULL. */\n    while ((sptr = ziplistNext(zl,eptr)) != NULL) {\n        score = zzlGetScore(sptr);\n        if (zslValueLteMax(score,range)) {\n            /* Delete both the element and the score. */\n            zl = ziplistDelete(zl,&eptr);\n            zl = ziplistDelete(zl,&eptr);\n            num++;\n        } else {\n            /* No longer in range. */\n            break;\n        }\n    }\n\n    if (deleted != NULL) *deleted = num;\n    return zl;\n}\n\nunsigned char *zzlDeleteRangeByLex(unsigned char *zl, zlexrangespec *range, unsigned long *deleted) {\n    unsigned char *eptr, *sptr;\n    unsigned long num = 0;\n\n    if (deleted != NULL) *deleted = 0;\n\n    eptr = zzlFirstInLexRange(zl,range);\n    if (eptr == NULL) return zl;\n\n    /* When the tail of the ziplist is deleted, eptr will point to the sentinel\n     * byte and ziplistNext will return NULL. */\n    while ((sptr = ziplistNext(zl,eptr)) != NULL) {\n        if (zzlLexValueLteMax(eptr,range)) {\n            /* Delete both the element and the score. */\n            zl = ziplistDelete(zl,&eptr);\n            zl = ziplistDelete(zl,&eptr);\n            num++;\n        } else {\n            /* No longer in range. */\n            break;\n        }\n    }\n\n    if (deleted != NULL) *deleted = num;\n    return zl;\n}\n\n/* Delete all the elements with rank between start and end from the skiplist.\n * Start and end are inclusive. Note that start and end need to be 1-based */\nunsigned char *zzlDeleteRangeByRank(unsigned char *zl, unsigned int start, unsigned int end, unsigned long *deleted) {\n    unsigned int num = (end-start)+1;\n    if (deleted) *deleted = num;\n    zl = ziplistDeleteRange(zl,2*(start-1),2*num);\n    return zl;\n}\n\n/*-----------------------------------------------------------------------------\n * Common sorted set API\n *----------------------------------------------------------------------------*/\n\nunsigned long zsetLength(robj_roptr zobj) {\n    unsigned long length = 0;\n    if (zobj->encoding == OBJ_ENCODING_ZIPLIST) {\n        length = zzlLength((const unsigned char*)zobj->m_ptr);\n    } else if (zobj->encoding == OBJ_ENCODING_SKIPLIST) {\n        length = ((const zset*)zobj->m_ptr)->zsl->length;\n    } else {\n        serverPanic(\"Unknown sorted set encoding\");\n    }\n    return length;\n}\n\nvoid zsetConvert(robj *zobj, int encoding) {\n    zset *zs;\n    zskiplistNode *node, *next;\n    sds ele;\n    double score;\n\n    if (zobj->encoding == encoding) return;\n    if (zobj->encoding == OBJ_ENCODING_ZIPLIST) {\n        unsigned char *zl = (unsigned char*)zobj->m_ptr;\n        unsigned char *eptr, *sptr;\n        unsigned char *vstr;\n        unsigned int vlen;\n        long long vlong;\n\n        if (encoding != OBJ_ENCODING_SKIPLIST)\n            serverPanic(\"Unknown target encoding\");\n\n        zs = (zset*)zmalloc(sizeof(*zs), MALLOC_SHARED);\n        zs->dict = dictCreate(&zsetDictType,NULL);\n        zs->zsl = zslCreate();\n\n        if (ziplistLen(zl) > 0) {\n            eptr = ziplistIndex(zl,0);\n            serverAssertWithInfo(NULL,zobj,eptr != NULL);\n            sptr = ziplistNext(zl,eptr);\n            serverAssertWithInfo(NULL,zobj,sptr != NULL);\n\n            while (eptr != NULL) {\n                score = zzlGetScore(sptr);\n                serverAssertWithInfo(NULL,zobj,ziplistGet(eptr,&vstr,&vlen,&vlong));\n                if (vstr == NULL)\n                    ele = sdsfromlonglong(vlong);\n                else\n                    ele = sdsnewlen((char*)vstr,vlen);\n\n                node = zslInsert(zs->zsl,score,ele);\n                serverAssert(dictAdd(zs->dict,ele,&node->score) == DICT_OK);\n                zzlNext(zl,&eptr,&sptr);\n            }\n        }\n        \n        zfree(zobj->m_ptr);\n        zobj->m_ptr = zs;\n        zobj->encoding = OBJ_ENCODING_SKIPLIST;\n    } else if (zobj->encoding == OBJ_ENCODING_SKIPLIST) {\n        unsigned char *zl = ziplistNew();\n\n        if (encoding != OBJ_ENCODING_ZIPLIST)\n            serverPanic(\"Unknown target encoding\");\n\n        /* Approach similar to zslFree(), since we want to free the skiplist at\n         * the same time as creating the ziplist. */\n        zs = (zset*)zobj->m_ptr;\n        dictRelease(zs->dict);\n        node = zs->zsl->header->level(0)->forward;\n        zfree(zs->zsl->header);\n        zfree(zs->zsl);\n\n        while (node) {\n            zl = zzlInsertAt(zl,NULL,node->ele,node->score);\n            next = node->level(0)->forward;\n            zslFreeNode(node);\n            node = next;\n        }\n\n        zfree(zs);\n        zobj->m_ptr = zl;\n        zobj->encoding = OBJ_ENCODING_ZIPLIST;\n    } else {\n        serverPanic(\"Unknown sorted set encoding\");\n    }\n}\n\n/* Convert the sorted set object into a ziplist if it is not already a ziplist\n * and if the number of elements and the maximum element size and total elements size\n * are within the expected ranges. */\nvoid zsetConvertToZiplistIfNeeded(robj *zobj, size_t maxelelen, size_t totelelen) {\n    if (zobj->encoding == OBJ_ENCODING_ZIPLIST) return;\n    zset *set = (zset*)zobj->m_ptr;\n\n    if (set->zsl->length <= g_pserver->zset_max_ziplist_entries &&\n        maxelelen <= g_pserver->zset_max_ziplist_value &&\n        ziplistSafeToAdd(NULL, totelelen))\n            zsetConvert(zobj,OBJ_ENCODING_ZIPLIST);\n}\n\n/* Return (by reference) the score of the specified member of the sorted set\n * storing it into *score. If the element does not exist C_ERR is returned\n * otherwise C_OK is returned and *score is correctly populated.\n * If 'zobj' or 'member' is NULL, C_ERR is returned. */\nint zsetScore(robj_roptr zobj, sds member, double *score) {\n    if (!zobj || !member) return C_ERR;\n\n    if (zobj->encoding == OBJ_ENCODING_ZIPLIST) {\n        if (zzlFind((unsigned char*)zobj->m_ptr, member, score) == NULL) return C_ERR;\n    } else if (zobj->encoding == OBJ_ENCODING_SKIPLIST) {\n        zset *zs = (zset*)zobj->m_ptr;\n        dictEntry *de = dictFind(zs->dict, member);\n        if (de == NULL) return C_ERR;\n        *score = *(double*)dictGetVal(de);\n    } else {\n        serverPanic(\"Unknown sorted set encoding\");\n    }\n    return C_OK;\n}\n\n/* Add a new element or update the score of an existing element in a sorted\n * set, regardless of its encoding.\n *\n * The set of flags change the command behavior. \n *\n * The input flags are the following:\n *\n * ZADD_INCR: Increment the current element score by 'score' instead of updating\n *            the current element score. If the element does not exist, we\n *            assume 0 as previous score.\n * ZADD_NX:   Perform the operation only if the element does not exist.\n * ZADD_XX:   Perform the operation only if the element already exist.\n * ZADD_GT:   Perform the operation on existing elements only if the new score is \n *            greater than the current score.\n * ZADD_LT:   Perform the operation on existing elements only if the new score is \n *            less than the current score.\n *\n * When ZADD_INCR is used, the new score of the element is stored in\n * '*newscore' if 'newscore' is not NULL.\n *\n * The returned flags are the following:\n *\n * ZADD_NAN:     The resulting score is not a number.\n * ZADD_ADDED:   The element was added (not present before the call).\n * ZADD_UPDATED: The element score was updated.\n * ZADD_NOP:     No operation was performed because of NX or XX.\n *\n * Return value:\n *\n * The function returns 1 on success, and sets the appropriate flags\n * ADDED or UPDATED to signal what happened during the operation (note that\n * none could be set if we re-added an element using the same score it used\n * to have, or in the case a zero increment is used).\n *\n * The function returns 0 on error, currently only when the increment\n * produces a NAN condition, or when the 'score' value is NAN since the\n * start.\n *\n * The command as a side effect of adding a new element may convert the sorted\n * set internal encoding from ziplist to hashtable+skiplist.\n *\n * Memory management of 'ele':\n *\n * The function does not take ownership of the 'ele' SDS string, but copies\n * it if needed. */\nint zsetAdd(robj *zobj, double score, sds ele, int in_flags, int *out_flags, double *newscore) {\n    /* Turn options into simple to check vars. */\n    int incr = (in_flags & ZADD_IN_INCR) != 0;\n    int nx = (in_flags & ZADD_IN_NX) != 0;\n    int xx = (in_flags & ZADD_IN_XX) != 0;\n    int gt = (in_flags & ZADD_IN_GT) != 0;\n    int lt = (in_flags & ZADD_IN_LT) != 0;\n    *out_flags = 0; /* We'll return our response flags. */\n    double curscore;\n\n    /* NaN as input is an error regardless of all the other parameters. */\n    if (std::isnan(score)) {\n        *out_flags = ZADD_OUT_NAN;\n        return 0;\n    }\n\n    /* Update the sorted set according to its encoding. */\n    if (zobj->encoding == OBJ_ENCODING_ZIPLIST) {\n        unsigned char *eptr;\n\n        if ((eptr = zzlFind((unsigned char*)zobj->m_ptr,ele,&curscore)) != NULL) {\n            /* NX? Return, same element already exists. */\n            if (nx) {\n                *out_flags |= ZADD_OUT_NOP;\n                return 1;\n            }\n\n            /* Prepare the score for the increment if needed. */\n            if (incr) {\n                score += curscore;\n                if (std::isnan(score)) {\n                    *out_flags |= ZADD_OUT_NAN;\n                    return 0;\n                }\n            }\n\n            /* GT/LT? Only update if score is greater/less than current. */\n            if ((lt && score >= curscore) || (gt && score <= curscore)) {\n                *out_flags |= ZADD_OUT_NOP;\n                return 1;\n            }\n\n            if (newscore) *newscore = score;\n\n            /* Remove and re-insert when score changed. */\n            if (score != curscore) {\n                zobj->m_ptr = zzlDelete((unsigned char*)zobj->m_ptr,eptr);\n                zobj->m_ptr = zzlInsert((unsigned char*)zobj->m_ptr,ele,score);\n                *out_flags |= ZADD_OUT_UPDATED;\n            }\n            return 1;\n        } else if (!xx) {\n            /* check if the element is too large or the list\n             * becomes too long *before* executing zzlInsert. */\n            if (zzlLength((unsigned char*)ptrFromObj(zobj))+1 > g_pserver->zset_max_ziplist_entries ||\n                sdslen(ele) > g_pserver->zset_max_ziplist_value ||\n                !ziplistSafeToAdd((unsigned char *)ptrFromObj(zobj), sdslen(ele)))\n            {\n                zsetConvert(zobj,OBJ_ENCODING_SKIPLIST);\n            } else {\n                zobj->m_ptr = zzlInsert((unsigned char *)ptrFromObj(zobj),ele,score);\n                if (newscore) *newscore = score;\n                *out_flags |= ZADD_OUT_ADDED;\n                return 1;\n            }\n        } else {\n            *out_flags |= ZADD_OUT_NOP;\n            return 1;\n        }\n    }\n\n    /* Note that the above block handling ziplist would have either returned or\n     * converted the key to skiplist. */\n    if (zobj->encoding == OBJ_ENCODING_SKIPLIST) {\n        zset *zs = (zset*)zobj->m_ptr;\n        zskiplistNode *znode;\n        dictEntry *de;\n\n        de = dictFind(zs->dict,ele);\n        if (de != NULL) {\n            /* NX? Return, same element already exists. */\n            if (nx) {\n                *out_flags |= ZADD_OUT_NOP;\n                return 1;\n            }\n\n            curscore = *(double*)dictGetVal(de);\n\n            /* Prepare the score for the increment if needed. */\n            if (incr) {\n                score += curscore;\n                if (std::isnan(score)) {\n                    *out_flags |= ZADD_OUT_NAN;\n                    return 0;\n                }\n            }\n\n            /* GT/LT? Only update if score is greater/less than current. */\n            if ((lt && score >= curscore) || (gt && score <= curscore)) {\n                *out_flags |= ZADD_OUT_NOP;\n                return 1;\n            }\n\n            if (newscore) *newscore = score;\n\n            /* Remove and re-insert when score changes. */\n            if (score != curscore) {\n                znode = zslUpdateScore(zs->zsl,curscore,ele,score);\n                /* Note that we did not removed the original element from\n                 * the hash table representing the sorted set, so we just\n                 * update the score. */\n                dictGetVal(de) = &znode->score; /* Update score ptr. */\n                *out_flags |= ZADD_OUT_UPDATED;\n            }\n            return 1;\n        } else if (!xx) {\n            ele = sdsdup(ele);\n            znode = zslInsert(zs->zsl,score,ele);\n            serverAssert(dictAdd(zs->dict,ele,&znode->score) == DICT_OK);\n            *out_flags |= ZADD_OUT_ADDED;\n            if (newscore) *newscore = score;\n            return 1;\n        } else {\n            *out_flags |= ZADD_OUT_NOP;\n            return 1;\n        }\n    } else {\n        serverPanic(\"Unknown sorted set encoding\");\n    }\n    return 0; /* Never reached. */\n}\n\n/* Deletes the element 'ele' from the sorted set encoded as a skiplist+dict,\n * returning 1 if the element existed and was deleted, 0 otherwise (the\n * element was not there). It does not resize the dict after deleting the\n * element. */\nstatic int zsetRemoveFromSkiplist(zset *zs, sds ele) {\n    dictEntry *de;\n    double score;\n\n    de = dictUnlink(zs->dict,ele);\n    if (de != NULL) {\n        /* Get the score in order to delete from the skiplist later. */\n        score = *(double*)dictGetVal(de);\n\n        /* Delete from the hash table and later from the skiplist.\n         * Note that the order is important: deleting from the skiplist\n         * actually releases the SDS string representing the element,\n         * which is shared between the skiplist and the hash table, so\n         * we need to delete from the skiplist as the final step. */\n        dictFreeUnlinkedEntry(zs->dict,de);\n\n        /* Delete from skiplist. */\n        int retval = zslDelete(zs->zsl,score,ele,NULL);\n        serverAssert(retval);\n\n        return 1;\n    }\n\n    return 0;\n}\n\n/* Delete the element 'ele' from the sorted set, returning 1 if the element\n * existed and was deleted, 0 otherwise (the element was not there). */\nint zsetDel(robj *zobj, sds ele) {\n    if (zobj->encoding == OBJ_ENCODING_ZIPLIST) {\n        unsigned char *eptr;\n\n        if ((eptr = zzlFind((unsigned char*)zobj->m_ptr,ele,NULL)) != NULL) {\n            zobj->m_ptr = zzlDelete((unsigned char*)zobj->m_ptr,eptr);\n            return 1;\n        }\n    } else if (zobj->encoding == OBJ_ENCODING_SKIPLIST) {\n        zset *zs = (zset*)ptrFromObj(zobj);\n        if (zsetRemoveFromSkiplist(zs, ele)) {\n            if (htNeedsResize(zs->dict)) dictResize(zs->dict);\n            return 1;\n        }\n    } else {\n        serverPanic(\"Unknown sorted set encoding\");\n    }\n    return 0; /* No such element found. */\n}\n\n/* Given a sorted set object returns the 0-based rank of the object or\n * -1 if the object does not exist.\n *\n * For rank we mean the position of the element in the sorted collection\n * of elements. So the first element has rank 0, the second rank 1, and so\n * forth up to length-1 elements.\n *\n * If 'reverse' is false, the rank is returned considering as first element\n * the one with the lowest score. Otherwise if 'reverse' is non-zero\n * the rank is computed considering as element with rank 0 the one with\n * the highest score. */\nlong zsetRank(robj_roptr zobj, sds ele, int reverse) {\n    unsigned long llen;\n    unsigned long rank;\n\n    llen = zsetLength(zobj);\n\n    if (zobj->encoding == OBJ_ENCODING_ZIPLIST) {\n        unsigned char *zl = (unsigned char*)zobj->m_ptr;\n        unsigned char *eptr, *sptr;\n\n        eptr = ziplistIndex(zl,0);\n        serverAssert(eptr != NULL);\n        sptr = ziplistNext(zl,eptr);\n        serverAssert(sptr != NULL);\n\n        rank = 1;\n        while(eptr != NULL) {\n            if (ziplistCompare(eptr,(unsigned char*)ele,sdslen(ele)))\n                break;\n            rank++;\n            zzlNext(zl,&eptr,&sptr);\n        }\n\n        if (eptr != NULL) {\n            if (reverse)\n                return llen-rank;\n            else\n                return rank-1;\n        } else {\n            return -1;\n        }\n    } else if (zobj->encoding == OBJ_ENCODING_SKIPLIST) {\n        zset *zs = (zset*)zobj->m_ptr;\n        zskiplist *zsl = zs->zsl;\n        dictEntry *de;\n        double score;\n\n        de = dictFind(zs->dict,ele);\n        if (de != NULL) {\n            score = *(double*)dictGetVal(de);\n            rank = zslGetRank(zsl,score,ele);\n            /* Existing elements always have a rank. */\n            serverAssert(rank != 0);\n            if (reverse)\n                return llen-rank;\n            else\n                return rank-1;\n        } else {\n            return -1;\n        }\n    } else {\n        serverPanic(\"Unknown sorted set encoding\");\n    }\n}\n\n/* This is a helper function for the COPY command.\n * Duplicate a sorted set object, with the guarantee that the returned object\n * has the same encoding as the original one.\n *\n * The resulting object always has refcount set to 1 */\nrobj *zsetDup(robj *o) {\n    robj *zobj;\n    zset *zs;\n    zset *new_zs;\n\n    serverAssert(o->type == OBJ_ZSET);\n\n    /* Create a new sorted set object that have the same encoding as the original object's encoding */\n    if (o->encoding == OBJ_ENCODING_ZIPLIST) {\n        unsigned char *zl = (unsigned char*)ptrFromObj(o);\n        size_t sz = ziplistBlobLen(zl);\n        unsigned char *new_zl = (unsigned char*)zmalloc(sz);\n        memcpy(new_zl, zl, sz);\n        zobj = createObject(OBJ_ZSET, new_zl);\n        zobj->encoding = OBJ_ENCODING_ZIPLIST;\n    } else if (o->encoding == OBJ_ENCODING_SKIPLIST) {\n        zobj = createZsetObject();\n        zs = (zset*)ptrFromObj(o);\n        new_zs = (zset*)ptrFromObj(zobj);\n        dictExpand(new_zs->dict,dictSize(zs->dict));\n        zskiplist *zsl = zs->zsl;\n        zskiplistNode *ln;\n        sds ele;\n        long llen = zsetLength(o);\n\n        /* We copy the skiplist elements from the greatest to the\n         * smallest (that's trivial since the elements are already ordered in\n         * the skiplist): this improves the load process, since the next loaded\n         * element will always be the smaller, so adding to the skiplist\n         * will always immediately stop at the head, making the insertion\n         * O(1) instead of O(log(N)). */\n        ln = zsl->tail;\n        while (llen--) {\n            ele = ln->ele;\n            sds new_ele = sdsdup(ele);\n            zskiplistNode *znode = zslInsert(new_zs->zsl,ln->score,new_ele);\n            dictAdd(new_zs->dict,new_ele,&znode->score);\n            ln = ln->backward;\n        }\n    } else {\n        serverPanic(\"Unknown sorted set encoding\");\n    }\n    return zobj;\n}\n\nstruct zset_validate_data {\n    long count;\n    dict *fields;\n};\n\n/* callback for to check the ziplist doesn't have duplicate recoreds */\nstatic int _zsetZiplistValidateIntegrity(unsigned char *p, void *userdata) {\n    zset_validate_data *data = (zset_validate_data*)userdata;\n\n    /* Even records are field names, add to dict and check that's not a dup */\n    if (((data->count) & 1) == 0) {\n        unsigned char *str;\n        unsigned int slen;\n        long long vll;\n        if (!ziplistGet(p, &str, &slen, &vll))\n            return 0;\n        sds field = str? sdsnewlen(str, slen): sdsfromlonglong(vll);;\n        if (dictAdd(data->fields, field, NULL) != DICT_OK) {\n            /* Duplicate, return an error */\n            sdsfree(field);\n            return 0;\n        }\n    }\n\n    (data->count)++;\n    return 1;\n}\n\n/* Validate the integrity of the data structure.\n * when `deep` is 0, only the integrity of the header is validated.\n * when `deep` is 1, we scan all the entries one by one. */\nint zsetZiplistValidateIntegrity(unsigned char *zl, size_t size, int deep) {\n    if (!deep)\n        return ziplistValidateIntegrity(zl, size, 0, NULL, NULL);\n\n    /* Keep track of the field names to locate duplicate ones */\n    zset_validate_data data = {0, dictCreate(&hashDictType, NULL)};\n\n    int ret = ziplistValidateIntegrity(zl, size, 1, _zsetZiplistValidateIntegrity, &data);\n\n    /* make sure we have an even number of records. */\n    if (data.count & 1)\n        ret = 0;\n\n    dictRelease(data.fields);\n    return ret;\n}\n\n/* Create a new sds string from the ziplist entry. */\nsds zsetSdsFromZiplistEntry(ziplistEntry *e) {\n    return e->sval ? sdsnewlen(e->sval, e->slen) : sdsfromlonglong(e->lval);\n}\n\n/* Reply with bulk string from the ziplist entry. */\nvoid zsetReplyFromZiplistEntry(client *c, ziplistEntry *e) {\n    if (e->sval)\n        addReplyBulkCBuffer(c, e->sval, e->slen);\n    else\n        addReplyBulkLongLong(c, e->lval);\n}\n\n\n/* Return random element from a non empty zset.\n * 'key' and 'val' will be set to hold the element.\n * The memory in `key` is not to be freed or modified by the caller.\n * 'score' can be NULL in which case it's not extracted. */\nvoid zsetTypeRandomElement(robj_roptr zsetobj, unsigned long zsetsize, ziplistEntry *key, double *score) {\n    if (zsetobj->encoding == OBJ_ENCODING_SKIPLIST) {\n        zset *zs = (zset*)ptrFromObj(zsetobj);\n        dictEntry *de = dictGetFairRandomKey(zs->dict);\n        sds s = (sds)dictGetKey(de);\n        key->sval = (unsigned char*)s;\n        key->slen = sdslen(s);\n        if (score)\n            *score = *(double*)dictGetVal(de);\n    } else if (zsetobj->encoding == OBJ_ENCODING_ZIPLIST) {\n        ziplistEntry val;\n        ziplistRandomPair((unsigned char*)ptrFromObj(zsetobj), zsetsize, key, &val);\n        if (score) {\n            if (val.sval) {\n                *score = zzlStrtod(val.sval,val.slen);\n            } else {\n                *score = (double)val.lval;\n            }\n        }\n    } else {\n        serverPanic(\"Unknown zset encoding\");\n    }\n}\n\n/*-----------------------------------------------------------------------------\n * Sorted set commands\n *----------------------------------------------------------------------------*/\n\n/* This generic command implements both ZADD and ZINCRBY. */\nvoid zaddGenericCommand(client *c, int flags) {\n    static const char *nanerr = \"resulting score is not a number (NaN)\";\n    robj *key = c->argv[1];\n    robj *zobj;\n    sds ele;\n    double score = 0, *scores = NULL;\n    int j, elements, ch = 0;\n    int scoreidx = 0;\n    /* The following vars are used in order to track what the command actually\n     * did during the execution, to reply to the client and to trigger the\n     * notification of keyspace change. */\n    int added = 0;      /* Number of new elements added. */\n    int updated = 0;    /* Number of elements with updated score. */\n    int processed = 0;  /* Number of elements processed, may remain zero with\n                           options like XX. */\n\n    /* Parse options. At the end 'scoreidx' is set to the argument position\n     * of the score of the first score-element pair. */\n    scoreidx = 2;\n    while(scoreidx < c->argc) {\n        char *opt = szFromObj(c->argv[scoreidx]);\n        if (!strcasecmp(opt,\"nx\")) flags |= ZADD_IN_NX;\n        else if (!strcasecmp(opt,\"xx\")) flags |= ZADD_IN_XX;\n        else if (!strcasecmp(opt,\"ch\")) ch = 1; /* Return num of elements added or updated. */\n        else if (!strcasecmp(opt,\"incr\")) flags |= ZADD_IN_INCR;\n        else if (!strcasecmp(opt,\"gt\")) flags |= ZADD_IN_GT;\n        else if (!strcasecmp(opt,\"lt\")) flags |= ZADD_IN_LT;\n        else break;\n        scoreidx++;\n    }\n\n    /* Turn options into simple to check vars. */\n    int incr = (flags & ZADD_IN_INCR) != 0;\n    int nx = (flags & ZADD_IN_NX) != 0;\n    int xx = (flags & ZADD_IN_XX) != 0;\n    int gt = (flags & ZADD_IN_GT) != 0;\n    int lt = (flags & ZADD_IN_LT) != 0;\n\n    /* After the options, we expect to have an even number of args, since\n     * we expect any number of score-element pairs. */\n    elements = c->argc-scoreidx;\n    if (elements % 2 || !elements) {\n        addReplyErrorObject(c,shared.syntaxerr);\n        return;\n    }\n    elements /= 2; /* Now this holds the number of score-element pairs. */\n\n    /* Check for incompatible options. */\n    if (nx && xx) {\n        addReplyError(c,\n            \"XX and NX options at the same time are not compatible\");\n        return;\n    }\n    \n    if ((gt && nx) || (lt && nx) || (gt && lt)) {\n        addReplyError(c,\n            \"GT, LT, and/or NX options at the same time are not compatible\");\n        return;\n    }\n    /* Note that XX is compatible with either GT or LT */\n\n    if (incr && elements > 1) {\n        addReplyError(c,\n            \"INCR option supports a single increment-element pair\");\n        return;\n    }\n\n    /* Start parsing all the scores, we need to emit any syntax error\n     * before executing additions to the sorted set, as the command should\n     * either execute fully or nothing at all. */\n    scores = (double*)zmalloc(sizeof(double)*elements, MALLOC_SHARED);\n    for (j = 0; j < elements; j++) {\n        if (getDoubleFromObjectOrReply(c,c->argv[scoreidx+j*2],&scores[j],NULL)\n            != C_OK) goto cleanup;\n    }\n\n    /* Lookup the key and create the sorted set if does not exist. */\n    zobj = lookupKeyWrite(c->db,key);\n    if (checkType(c,zobj,OBJ_ZSET)) goto cleanup;\n    if (zobj == NULL) {\n        if (xx) goto reply_to_client; /* No key + XX option: nothing to do. */\n        if (g_pserver->zset_max_ziplist_entries == 0 ||\n            g_pserver->zset_max_ziplist_value < sdslen(szFromObj(c->argv[scoreidx+1])))\n        {\n            zobj = createZsetObject();\n        } else {\n            zobj = createZsetZiplistObject();\n        }\n        dbAdd(c->db,key,zobj);\n    }\n\n    for (j = 0; j < elements; j++) {\n        double newscore;\n        score = scores[j];\n        int retflags = 0;\n\n        ele = szFromObj(c->argv[scoreidx+1+j*2]);\n        int retval = zsetAdd(zobj, score, ele, flags, &retflags, &newscore);\n        if (retval == 0) {\n            addReplyError(c,nanerr);\n            goto cleanup;\n        }\n        if (retflags & ZADD_OUT_ADDED) added++;\n        if (retflags & ZADD_OUT_UPDATED) updated++;\n        if (!(retflags & ZADD_OUT_NOP)) processed++;\n        score = newscore;\n    }\n    g_pserver->dirty += (added+updated);\n\nreply_to_client:\n    if (incr) { /* ZINCRBY or INCR option. */\n        if (processed)\n            addReplyDouble(c,score);\n        else\n            addReplyNull(c);\n    } else { /* ZADD. */\n        addReplyLongLong(c,ch ? added+updated : added);\n    }\n\ncleanup:\n    zfree(scores);\n    if (added || updated) {\n        signalModifiedKey(c,c->db,key);\n        notifyKeyspaceEvent(NOTIFY_ZSET,\n            incr ? \"zincr\" : \"zadd\", key, c->db->id);\n    }\n}\n\nvoid zaddCommand(client *c) {\n    zaddGenericCommand(c,ZADD_IN_NONE);\n}\n\nvoid zincrbyCommand(client *c) {\n    zaddGenericCommand(c,ZADD_IN_INCR);\n}\n\nvoid zremCommand(client *c) {\n    robj *key = c->argv[1];\n    robj *zobj;\n    int deleted = 0, keyremoved = 0, j;\n\n    if ((zobj = lookupKeyWriteOrReply(c,key,shared.czero)) == NULL ||\n        checkType(c,zobj,OBJ_ZSET)) return;\n\n    for (j = 2; j < c->argc; j++) {\n        if (zsetDel(zobj,szFromObj(c->argv[j]))) deleted++;\n        if (zsetLength(zobj) == 0) {\n            dbDelete(c->db,key);\n            keyremoved = 1;\n            break;\n        }\n    }\n\n    if (deleted) {\n        notifyKeyspaceEvent(NOTIFY_ZSET,\"zrem\",key,c->db->id);\n        if (keyremoved)\n            notifyKeyspaceEvent(NOTIFY_GENERIC,\"del\",key,c->db->id);\n        signalModifiedKey(c,c->db,key);\n        g_pserver->dirty += deleted;\n    }\n    addReplyLongLong(c,deleted);\n}\n\ntypedef enum {\n    ZRANGE_AUTO = 0,\n    ZRANGE_RANK,\n    ZRANGE_SCORE,\n    ZRANGE_LEX,\n} zrange_type;\n\n/* Implements ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZREMRANGEBYLEX commands. */\nvoid zremrangeGenericCommand(client *c, zrange_type rangetype) {\n    robj *key = c->argv[1];\n    robj *zobj;\n    int keyremoved = 0;\n    unsigned long deleted = 0;\n    zrangespec range;\n    zlexrangespec lexrange;\n    long start, end, llen;\n    const char *notify_type = NULL;\n\n    /* Step 1: Parse the range. */\n    if (rangetype == ZRANGE_RANK) {\n        notify_type = \"zremrangebyrank\";\n        if ((getLongFromObjectOrReply(c,c->argv[2],&start,NULL) != C_OK) ||\n            (getLongFromObjectOrReply(c,c->argv[3],&end,NULL) != C_OK))\n            return;\n    } else if (rangetype == ZRANGE_SCORE) {\n        notify_type = \"zremrangebyscore\";\n        if (zslParseRange(c->argv[2],c->argv[3],&range) != C_OK) {\n            addReplyError(c,\"min or max is not a float\");\n            return;\n        }\n    } else if (rangetype == ZRANGE_LEX) {\n        notify_type = \"zremrangebylex\";\n        if (zslParseLexRange(c->argv[2],c->argv[3],&lexrange) != C_OK) {\n            addReplyError(c,\"min or max not valid string range item\");\n            return;\n        }\n    } else {\n        serverPanic(\"unknown rangetype %d\", (int)rangetype);\n    }\n\n    /* Step 2: Lookup & range sanity checks if needed. */\n    if ((zobj = lookupKeyWriteOrReply(c,key,shared.czero)) == NULL ||\n        checkType(c,zobj,OBJ_ZSET)) goto cleanup;\n\n    if (rangetype == ZRANGE_RANK) {\n        /* Sanitize indexes. */\n        llen = zsetLength(zobj);\n        if (start < 0) start = llen+start;\n        if (end < 0) end = llen+end;\n        if (start < 0) start = 0;\n\n        /* Invariant: start >= 0, so this test will be true when end < 0.\n         * The range is empty when start > end or start >= length. */\n        if (start > end || start >= llen) {\n            addReply(c,shared.czero);\n            goto cleanup;\n        }\n        if (end >= llen) end = llen-1;\n    }\n\n    /* Step 3: Perform the range deletion operation. */\n    if (zobj->encoding == OBJ_ENCODING_ZIPLIST) {\n        switch(rangetype) {\n        case ZRANGE_AUTO:\n        case ZRANGE_RANK:\n            zobj->m_ptr = zzlDeleteRangeByRank((unsigned char*)zobj->m_ptr,start+1,end+1,&deleted);\n            break;\n        case ZRANGE_SCORE:\n            zobj->m_ptr = zzlDeleteRangeByScore((unsigned char*)zobj->m_ptr,&range,&deleted);\n            break;\n        case ZRANGE_LEX:\n            zobj->m_ptr = zzlDeleteRangeByLex((unsigned char*)zobj->m_ptr,&lexrange,&deleted);\n            break;\n        }\n        if (zzlLength((unsigned char*)zobj->m_ptr) == 0) {\n            dbDelete(c->db,key);\n            keyremoved = 1;\n        }\n    } else if (zobj->encoding == OBJ_ENCODING_SKIPLIST) {\n        zset *zs = (zset*)zobj->m_ptr;\n        switch(rangetype) {\n        case ZRANGE_AUTO:\n        case ZRANGE_RANK:\n            deleted = zslDeleteRangeByRank(zs->zsl,start+1,end+1,zs->dict);\n            break;\n        case ZRANGE_SCORE:\n            deleted = zslDeleteRangeByScore(zs->zsl,&range,zs->dict);\n            break;\n        case ZRANGE_LEX:\n            deleted = zslDeleteRangeByLex(zs->zsl,&lexrange,zs->dict);\n            break;\n        }\n        if (htNeedsResize(zs->dict)) dictResize(zs->dict);\n        if (dictSize(zs->dict) == 0) {\n            dbDelete(c->db,key);\n            keyremoved = 1;\n        }\n    } else {\n        serverPanic(\"Unknown sorted set encoding\");\n    }\n\n    /* Step 4: Notifications and reply. */\n    if (deleted) {\n        signalModifiedKey(c,c->db,key);\n        notifyKeyspaceEvent(NOTIFY_ZSET,notify_type,key,c->db->id);\n        if (keyremoved)\n            notifyKeyspaceEvent(NOTIFY_GENERIC,\"del\",key,c->db->id);\n    }\n    g_pserver->dirty += deleted;\n    addReplyLongLong(c,deleted);\n\ncleanup:\n    if (rangetype == ZRANGE_LEX) zslFreeLexRange(&lexrange);\n}\n\nvoid zremrangebyrankCommand(client *c) {\n    zremrangeGenericCommand(c,ZRANGE_RANK);\n}\n\nvoid zremrangebyscoreCommand(client *c) {\n    zremrangeGenericCommand(c,ZRANGE_SCORE);\n}\n\nvoid zremrangebylexCommand(client *c) {\n    zremrangeGenericCommand(c,ZRANGE_LEX);\n}\n\nstruct zsetopsrc {\n    robj *subject;\n    int type; /* Set, sorted set */\n    int encoding;\n    double weight;\n\n    union _uT {\n        /* Set iterators. */\n        union _iterset {\n            struct {\n                intset *is;\n                int ii;\n            } is;\n            struct {\n                ::dict *dict;\n                dictIterator *di;\n                dictEntry *de;\n            } ht;\n        } set;\n\n        /* Sorted set iterators. */\n        union _iterzset {\n            struct {\n                unsigned char *zl;\n                unsigned char *eptr, *sptr;\n            } zl;\n            struct {\n                zset *zs;\n                zskiplistNode *node;\n            } sl;\n        } iterzset;\n    } iter;\n};\n\n\n/* Use dirty flags for pointers that need to be cleaned up in the next\n * iteration over the zsetopval. The dirty flag for the long long value is\n * special, since long long values don't need cleanup. Instead, it means that\n * we already checked that \"ell\" holds a long long, or tried to convert another\n * representation into a long long value. When this was successful,\n * OPVAL_VALID_LL is set as well. */\n#define OPVAL_DIRTY_SDS 1\n#define OPVAL_DIRTY_LL 2\n#define OPVAL_VALID_LL 4\n\n/* Store value retrieved from the iterator. */\ntypedef struct {\n    int flags;\n    unsigned char _buf[32]; /* Private buffer. */\n    sds ele;\n    unsigned char *estr;\n    unsigned int elen;\n    long long ell;\n    double score;\n} zsetopval;\n\ntypedef union zsetopsrc::_uT::_iterset iterset;\ntypedef union zsetopsrc::_uT::_iterzset iterzset;\n\nvoid zuiInitIterator(zsetopsrc *op) {\n    if (op->subject == NULL)\n        return;\n\n    if (op->type == OBJ_SET) {\n        iterset *it = (iterset*)&op->iter.set;\n        if (op->encoding == OBJ_ENCODING_INTSET) {\n            it->is.is = (intset*)op->subject->m_ptr;\n            it->is.ii = 0;\n        } else if (op->encoding == OBJ_ENCODING_HT) {\n            it->ht.dict = (dict*)op->subject->m_ptr;\n            it->ht.di = dictGetIterator((dict*)op->subject->m_ptr);\n            it->ht.de = dictNext(it->ht.di);\n        } else {\n            serverPanic(\"Unknown set encoding\");\n        }\n    } else if (op->type == OBJ_ZSET) {\n        /* Sorted sets are traversed in reverse order to optimize for\n         * the insertion of the elements in a new list as in\n         * ZDIFF/ZINTER/ZUNION */\n        iterzset *it = (iterzset*)&op->iter.iterzset;\n        if (op->encoding == OBJ_ENCODING_ZIPLIST) {\n            it->zl.zl = (unsigned char*)op->subject->m_ptr;\n            it->zl.eptr = ziplistIndex(it->zl.zl,-2);\n            if (it->zl.eptr != NULL) {\n                it->zl.sptr = ziplistNext(it->zl.zl,it->zl.eptr);\n                serverAssert(it->zl.sptr != NULL);\n            }\n        } else if (op->encoding == OBJ_ENCODING_SKIPLIST) {\n            it->sl.zs = (zset*)op->subject->m_ptr;\n            it->sl.node = it->sl.zs->zsl->tail;\n        } else {\n            serverPanic(\"Unknown sorted set encoding\");\n        }\n    } else {\n        serverPanic(\"Unsupported type\");\n    }\n}\n\nvoid zuiClearIterator(zsetopsrc *op) {\n    if (op->subject == NULL)\n        return;\n\n    if (op->type == OBJ_SET) {\n        iterset *it = &op->iter.set;\n        if (op->encoding == OBJ_ENCODING_INTSET) {\n            UNUSED(it); /* skip */\n        } else if (op->encoding == OBJ_ENCODING_HT) {\n            dictReleaseIterator(it->ht.di);\n        } else {\n            serverPanic(\"Unknown set encoding\");\n        }\n    } else if (op->type == OBJ_ZSET) {\n        iterzset *it = &op->iter.iterzset;\n        if (op->encoding == OBJ_ENCODING_ZIPLIST) {\n            UNUSED(it); /* skip */\n        } else if (op->encoding == OBJ_ENCODING_SKIPLIST) {\n            UNUSED(it); /* skip */\n        } else {\n            serverPanic(\"Unknown sorted set encoding\");\n        }\n    } else {\n        serverPanic(\"Unsupported type\");\n    }\n}\n\nunsigned long zuiLength(zsetopsrc *op) {\n    if (op->subject == NULL)\n        return 0;\n\n    if (op->type == OBJ_SET) {\n        if (op->encoding == OBJ_ENCODING_INTSET) {\n            return intsetLen((const intset*)op->subject->m_ptr);\n        } else if (op->encoding == OBJ_ENCODING_HT) {\n            dict *ht = (dict*)op->subject->m_ptr;\n            return dictSize(ht);\n        } else {\n            serverPanic(\"Unknown set encoding\");\n        }\n    } else if (op->type == OBJ_ZSET) {\n        if (op->encoding == OBJ_ENCODING_ZIPLIST) {\n            return zzlLength((unsigned char*)op->subject->m_ptr);\n        } else if (op->encoding == OBJ_ENCODING_SKIPLIST) {\n            zset *zs = (zset*)op->subject->m_ptr;\n            return zs->zsl->length;\n        } else {\n            serverPanic(\"Unknown sorted set encoding\");\n        }\n    } else {\n        serverPanic(\"Unsupported type\");\n    }\n}\n\n/* Check if the current value is valid. If so, store it in the passed structure\n * and move to the next element. If not valid, this means we have reached the\n * end of the structure and can abort. */\nint zuiNext(zsetopsrc *op, zsetopval *val) {\n    if (op->subject == NULL)\n        return 0;\n\n    if (val->flags & OPVAL_DIRTY_SDS)\n        sdsfree(val->ele);\n\n    memset(val,0,sizeof(zsetopval));\n\n    if (op->type == OBJ_SET) {\n        iterset *it = &op->iter.set;\n        if (op->encoding == OBJ_ENCODING_INTSET) {\n            int64_t ell;\n\n            if (!intsetGet(it->is.is,it->is.ii,&ell))\n                return 0;\n            val->ell = ell;\n            val->score = 1.0;\n\n            /* Move to next element. */\n            it->is.ii++;\n        } else if (op->encoding == OBJ_ENCODING_HT) {\n            if (it->ht.de == NULL)\n                return 0;\n            val->ele = (sds)dictGetKey(it->ht.de);\n            val->score = 1.0;\n\n            /* Move to next element. */\n            it->ht.de = dictNext(it->ht.di);\n        } else {\n            serverPanic(\"Unknown set encoding\");\n        }\n    } else if (op->type == OBJ_ZSET) {\n        iterzset *it = &op->iter.iterzset;\n        if (op->encoding == OBJ_ENCODING_ZIPLIST) {\n            /* No need to check both, but better be explicit. */\n            if (it->zl.eptr == NULL || it->zl.sptr == NULL)\n                return 0;\n            serverAssert(ziplistGet(it->zl.eptr,&val->estr,&val->elen,&val->ell));\n            val->score = zzlGetScore(it->zl.sptr);\n\n            /* Move to next element (going backwards, see zuiInitIterator). */\n            zzlPrev(it->zl.zl,&it->zl.eptr,&it->zl.sptr);\n        } else if (op->encoding == OBJ_ENCODING_SKIPLIST) {\n            if (it->sl.node == NULL)\n                return 0;\n            val->ele = it->sl.node->ele;\n            val->score = it->sl.node->score;\n\n            /* Move to next element. (going backwards, see zuiInitIterator) */\n            it->sl.node = it->sl.node->backward;\n        } else {\n            serverPanic(\"Unknown sorted set encoding\");\n        }\n    } else {\n        serverPanic(\"Unsupported type\");\n    }\n    return 1;\n}\n\nint zuiLongLongFromValue(zsetopval *val) {\n    if (!(val->flags & OPVAL_DIRTY_LL)) {\n        val->flags |= OPVAL_DIRTY_LL;\n\n        if (val->ele != NULL) {\n            if (string2ll(val->ele,sdslen(val->ele),&val->ell))\n                val->flags |= OPVAL_VALID_LL;\n        } else if (val->estr != NULL) {\n            if (string2ll((char*)val->estr,val->elen,&val->ell))\n                val->flags |= OPVAL_VALID_LL;\n        } else {\n            /* The long long was already set, flag as valid. */\n            val->flags |= OPVAL_VALID_LL;\n        }\n    }\n    return val->flags & OPVAL_VALID_LL;\n}\n\nsds zuiSdsFromValue(zsetopval *val) {\n    if (val->ele == NULL) {\n        if (val->estr != NULL) {\n            val->ele = sdsnewlen((char*)val->estr,val->elen);\n        } else {\n            val->ele = sdsfromlonglong(val->ell);\n        }\n        val->flags |= OPVAL_DIRTY_SDS;\n    }\n    return val->ele;\n}\n\n/* This is different from zuiSdsFromValue since returns a new SDS string\n * which is up to the caller to free. */\nsds zuiNewSdsFromValue(zsetopval *val) {\n    if (val->flags & OPVAL_DIRTY_SDS) {\n        /* We have already one to return! */\n        sds ele = val->ele;\n        val->flags &= ~OPVAL_DIRTY_SDS;\n        val->ele = NULL;\n        return ele;\n    } else if (val->ele) {\n        return sdsdup(val->ele);\n    } else if (val->estr) {\n        return sdsnewlen((char*)val->estr,val->elen);\n    } else {\n        return sdsfromlonglong(val->ell);\n    }\n}\n\nint zuiBufferFromValue(zsetopval *val) {\n    if (val->estr == NULL) {\n        if (val->ele != NULL) {\n            val->elen = sdslen(val->ele);\n            val->estr = (unsigned char*)val->ele;\n        } else {\n            val->elen = ll2string((char*)val->_buf,sizeof(val->_buf),val->ell);\n            val->estr = val->_buf;\n        }\n    }\n    return 1;\n}\n\n/* Find value pointed to by val in the source pointer to by op. When found,\n * return 1 and store its score in target. Return 0 otherwise. */\nint zuiFind(zsetopsrc *op, zsetopval *val, double *score) {\n    if (op->subject == NULL)\n        return 0;\n\n    if (op->type == OBJ_SET) {\n        if (op->encoding == OBJ_ENCODING_INTSET) {\n            if (zuiLongLongFromValue(val) &&\n                intsetFind((intset*)op->subject->m_ptr,val->ell))\n            {\n                *score = 1.0;\n                return 1;\n            } else {\n                return 0;\n            }\n        } else if (op->encoding == OBJ_ENCODING_HT) {\n            dict *ht = (dict*)op->subject->m_ptr;\n            zuiSdsFromValue(val);\n            if (dictFind(ht,val->ele) != NULL) {\n                *score = 1.0;\n                return 1;\n            } else {\n                return 0;\n            }\n        } else {\n            serverPanic(\"Unknown set encoding\");\n        }\n    } else if (op->type == OBJ_ZSET) {\n        zuiSdsFromValue(val);\n\n        if (op->encoding == OBJ_ENCODING_ZIPLIST) {\n            if (zzlFind((unsigned char*)op->subject->m_ptr,val->ele,score) != NULL) {\n                /* Score is already set by zzlFind. */\n                return 1;\n            } else {\n                return 0;\n            }\n        } else if (op->encoding == OBJ_ENCODING_SKIPLIST) {\n            zset *zs = (zset*)op->subject->m_ptr;\n            dictEntry *de;\n            if ((de = dictFind(zs->dict,val->ele)) != NULL) {\n                *score = *(double*)dictGetVal(de);\n                return 1;\n            } else {\n                return 0;\n            }\n        } else {\n            serverPanic(\"Unknown sorted set encoding\");\n        }\n    } else {\n        serverPanic(\"Unsupported type\");\n    }\n}\n\nint zuiCompareByCardinality(const void *s1, const void *s2) {\n    unsigned long first = zuiLength((zsetopsrc*)s1);\n    unsigned long second = zuiLength((zsetopsrc*)s2);\n    if (first > second) return 1;\n    if (first < second) return -1;\n    return 0;\n}\n\nstatic int zuiCompareByRevCardinality(const void *s1, const void *s2) {\n    return zuiCompareByCardinality(s1, s2) * -1;\n}\n\n#define REDIS_AGGR_SUM 1\n#define REDIS_AGGR_MIN 2\n#define REDIS_AGGR_MAX 3\n#define zunionInterDictValue(_e) (dictGetVal(_e) == NULL ? 1.0 : *(double*)dictGetVal(_e))\n\ninline static void zunionInterAggregate(double *target, double val, int aggregate) {\n    if (aggregate == REDIS_AGGR_SUM) {\n        *target = *target + val;\n        /* The result of adding two doubles is NaN when one variable\n         * is +inf and the other is -inf. When these numbers are added,\n         * we maintain the convention of the result being 0.0. */\n        if (std::isnan(*target)) *target = 0.0;\n    } else if (aggregate == REDIS_AGGR_MIN) {\n        *target = val < *target ? val : *target;\n    } else if (aggregate == REDIS_AGGR_MAX) {\n        *target = val > *target ? val : *target;\n    } else {\n        /* safety net */\n        serverPanic(\"Unknown ZUNION/INTER aggregate type\");\n    }\n}\n\nstatic size_t zsetDictGetMaxElementLength(dict *d, size_t *totallen) {\n    dictIterator *di;\n    dictEntry *de;\n    size_t maxelelen = 0;\n\n    di = dictGetIterator(d);\n\n    while((de = dictNext(di)) != NULL) {\n        sds ele = (sds)dictGetKey(de);\n        if (sdslen(ele) > maxelelen) maxelelen = sdslen(ele);\n        if (totallen)\n            (*totallen) += sdslen(ele);\n    }\n\n    dictReleaseIterator(di);\n\n    return maxelelen;\n}\n\nstatic void zdiffAlgorithm1(zsetopsrc *src, long setnum, zset *dstzset, size_t *maxelelen, size_t *totelelen) {\n    /* DIFF Algorithm 1:\n     *\n     * We perform the diff by iterating all the elements of the first set,\n     * and only adding it to the target set if the element does not exist\n     * into all the other sets.\n     *\n     * This way we perform at max N*M operations, where N is the size of\n     * the first set, and M the number of sets.\n     *\n     * There is also a O(K*log(K)) cost for adding the resulting elements\n     * to the target set, where K is the final size of the target set.\n     *\n     * The final complexity of this algorithm is O(N*M + K*log(K)). */\n    int j;\n    zsetopval zval;\n    zskiplistNode *znode;\n    sds tmp;\n\n    /* With algorithm 1 it is better to order the sets to subtract\n     * by decreasing size, so that we are more likely to find\n     * duplicated elements ASAP. */\n    qsort(src+1,setnum-1,sizeof(zsetopsrc),zuiCompareByRevCardinality);\n\n    memset(&zval, 0, sizeof(zval));\n    zuiInitIterator(&src[0]);\n    while (zuiNext(&src[0],&zval)) {\n        double value;\n        int exists = 0;\n\n        for (j = 1; j < setnum; j++) {\n            /* It is not safe to access the zset we are\n             * iterating, so explicitly check for equal object.\n             * This check isn't really needed anymore since we already\n             * check for a duplicate set in the zsetChooseDiffAlgorithm\n             * function, but we're leaving it for future-proofing. */\n            if (src[j].subject == src[0].subject ||\n                zuiFind(&src[j],&zval,&value)) {\n                exists = 1;\n                break;\n            }\n        }\n\n        if (!exists) {\n            tmp = zuiNewSdsFromValue(&zval);\n            znode = zslInsert(dstzset->zsl,zval.score,tmp);\n            dictAdd(dstzset->dict,tmp,&znode->score);\n            if (sdslen(tmp) > *maxelelen) *maxelelen = sdslen(tmp);\n            (*totelelen) += sdslen(tmp);\n        }\n    }\n    zuiClearIterator(&src[0]);\n}\n\n\nstatic void zdiffAlgorithm2(zsetopsrc *src, long setnum, zset *dstzset, size_t *maxelelen, size_t *totelelen) {\n    /* DIFF Algorithm 2:\n     *\n     * Add all the elements of the first set to the auxiliary set.\n     * Then remove all the elements of all the next sets from it.\n     *\n\n     * This is O(L + (N-K)log(N)) where L is the sum of all the elements in every\n     * set, N is the size of the first set, and K is the size of the result set.\n     *\n     * Note that from the (L-N) dict searches, (N-K) got to the zsetRemoveFromSkiplist\n     * which costs log(N)\n     *\n     * There is also a O(K) cost at the end for finding the largest element\n     * size, but this doesn't change the algorithm complexity since K < L, and\n     * O(2L) is the same as O(L). */\n    int j;\n    int cardinality = 0;\n    zsetopval zval;\n    zskiplistNode *znode;\n    sds tmp;\n\n    for (j = 0; j < setnum; j++) {\n        if (zuiLength(&src[j]) == 0) continue;\n\n        memset(&zval, 0, sizeof(zval));\n        zuiInitIterator(&src[j]);\n        while (zuiNext(&src[j],&zval)) {\n            if (j == 0) {\n                tmp = zuiNewSdsFromValue(&zval);\n                znode = zslInsert(dstzset->zsl,zval.score,tmp);\n                dictAdd(dstzset->dict,tmp,&znode->score);\n                cardinality++;\n            } else {\n                tmp = zuiSdsFromValue(&zval);\n                if (zsetRemoveFromSkiplist(dstzset, tmp)) {\n                    cardinality--;\n                }\n            }\n\n            /* Exit if result set is empty as any additional removal\n                * of elements will have no effect. */\n            if (cardinality == 0) break;\n        }\n        zuiClearIterator(&src[j]);\n\n        if (cardinality == 0) break;\n    }\n\n    /* Redize dict if needed after removing multiple elements */\n    if (htNeedsResize(dstzset->dict)) dictResize(dstzset->dict);\n\n    /* Using this algorithm, we can't calculate the max element as we go,\n     * we have to iterate through all elements to find the max one after. */\n    *maxelelen = zsetDictGetMaxElementLength(dstzset->dict, totelelen);\n}\n\nstatic int zsetChooseDiffAlgorithm(zsetopsrc *src, long setnum) {\n    int j;\n\n    /* Select what DIFF algorithm to use.\n     *\n     * Algorithm 1 is O(N*M + K*log(K)) where N is the size of the\n     * first set, M the total number of sets, and K is the size of the\n     * result set.\n     *\n     * Algorithm 2 is O(L + (N-K)log(N)) where L is the total number of elements\n     * in all the sets, N is the size of the first set, and K is the size of the\n     * result set.\n     *\n     * We compute what is the best bet with the current input here. */\n    long long algo_one_work = 0;\n    long long algo_two_work = 0;\n\n    for (j = 0; j < setnum; j++) {\n        /* If any other set is equal to the first set, there is nothing to be\n         * done, since we would remove all elements anyway. */\n        if (j > 0 && src[0].subject == src[j].subject) {\n            return 0;\n        }\n\n        algo_one_work += zuiLength(&src[0]);\n        algo_two_work += zuiLength(&src[j]);\n    }\n\n    /* Algorithm 1 has better constant times and performs less operations\n     * if there are elements in common. Give it some advantage. */\n    algo_one_work /= 2;\n    return (algo_one_work <= algo_two_work) ? 1 : 2;\n}\n\nstatic void zdiff(zsetopsrc *src, long setnum, zset *dstzset, size_t *maxelelen, size_t *totelelen) {\n    /* Skip everything if the smallest input is empty. */\n    if (zuiLength(&src[0]) > 0) {\n        int diff_algo = zsetChooseDiffAlgorithm(src, setnum);\n        if (diff_algo == 1) {\n            zdiffAlgorithm1(src, setnum, dstzset, maxelelen, totelelen);\n        } else if (diff_algo == 2) {\n            zdiffAlgorithm2(src, setnum, dstzset, maxelelen, totelelen);\n        } else if (diff_algo != 0) {\n            serverPanic(\"Unknown algorithm\");\n        }\n    }\n}\n\nuint64_t dictSdsHash(const void *key);\nint dictSdsKeyCompare(void *privdata, const void *key1, const void *key2);\n\ndictType setAccumulatorDictType = {\n    dictSdsHash,               /* hash function */\n    NULL,                      /* key dup */\n    NULL,                      /* val dup */\n    dictSdsKeyCompare,         /* key compare */\n    NULL,                      /* key destructor */\n    NULL,                      /* val destructor */\n    NULL                       /* allow to expand */\n};\n\n/* The zunionInterDiffGenericCommand() function is called in order to implement the\n * following commands: ZUNION, ZINTER, ZDIFF, ZUNIONSTORE, ZINTERSTORE, ZDIFFSTORE.\n *\n * 'numkeysIndex' parameter position of key number. for ZUNION/ZINTER/ZDIFF command,\n * this value is 1, for ZUNIONSTORE/ZINTERSTORE/ZDIFFSTORE command, this value is 2.\n *\n * 'op' SET_OP_INTER, SET_OP_UNION or SET_OP_DIFF.\n */\nvoid zunionInterDiffGenericCommand(client *c, robj *dstkey, int numkeysIndex, int op) {\n    int i, j;\n    long setnum;\n    int aggregate = REDIS_AGGR_SUM;\n    zsetopsrc *src;\n    zsetopval zval;\n    sds tmp;\n    size_t maxelelen = 0, totelelen = 0;\n    robj *dstobj;\n    zset *dstzset;\n    zskiplistNode *znode;\n    int withscores = 0;\n\n    /* expect setnum input keys to be given */\n    if ((getLongFromObjectOrReply(c, c->argv[numkeysIndex], &setnum, NULL) != C_OK))\n        return;\n\n    if (setnum < 1) {\n        addReplyErrorFormat(c,\n            \"at least 1 input key is needed for %s\", c->cmd->name);\n        return;\n    }\n\n    /* test if the expected number of keys would overflow */\n    if (setnum > (c->argc-(numkeysIndex+1))) {\n        addReplyErrorObject(c,shared.syntaxerr);\n        return;\n    }\n\n    /* read keys to be used for input */\n    src = (zsetopsrc*)zcalloc(sizeof(zsetopsrc) * setnum);\n    for (i = 0, j = numkeysIndex+1; i < setnum; i++, j++) {\n        robj_roptr obj = dstkey ?\n            lookupKeyWrite(c->db,c->argv[j]) :\n            lookupKeyRead(c->db,c->argv[j]);\n        if (obj != nullptr) {\n            if (obj->type != OBJ_ZSET && obj->type != OBJ_SET) {\n                zfree(src);\n                addReplyErrorObject(c,shared.wrongtypeerr);\n                return;\n            }\n\n            src[i].subject = obj.unsafe_robjcast();\n            src[i].type = obj->type;\n            src[i].encoding = obj->encoding;\n        } else {\n            src[i].subject = NULL;\n        }\n\n        /* Default all weights to 1. */\n        src[i].weight = 1.0;\n    }\n\n    /* parse optional extra arguments */\n    if (j < c->argc) {\n        int remaining = c->argc - j;\n\n        while (remaining) {\n            if (op != SET_OP_DIFF &&\n                remaining >= (setnum + 1) &&\n                !strcasecmp(szFromObj(c->argv[j]),\"weights\"))\n            {\n                j++; remaining--;\n                for (i = 0; i < setnum; i++, j++, remaining--) {\n                    if (getDoubleFromObjectOrReply(c,c->argv[j],&src[i].weight,\n                            \"weight value is not a float\") != C_OK)\n                    {\n                        zfree(src);\n                        return;\n                    }\n                }\n            } else if (op != SET_OP_DIFF &&\n                       remaining >= 2 &&\n                       !strcasecmp(szFromObj(c->argv[j]),\"aggregate\"))\n            {\n                j++; remaining--;\n                if (!strcasecmp(szFromObj(c->argv[j]),\"sum\")) {\n                    aggregate = REDIS_AGGR_SUM;\n                } else if (!strcasecmp(szFromObj(c->argv[j]),\"min\")) {\n                    aggregate = REDIS_AGGR_MIN;\n                } else if (!strcasecmp(szFromObj(c->argv[j]),\"max\")) {\n                    aggregate = REDIS_AGGR_MAX;\n                } else {\n                    zfree(src);\n                    addReplyErrorObject(c,shared.syntaxerr);\n                    return;\n                }\n                j++; remaining--;\n            } else if (remaining >= 1 &&\n                       !dstkey &&\n                       !strcasecmp(szFromObj(c->argv[j]),\"withscores\"))\n            {\n                j++; remaining--;\n                withscores = 1;\n            } else {\n                zfree(src);\n                addReplyErrorObject(c,shared.syntaxerr);\n                return;\n            }\n        }\n    }\n\n    if (op != SET_OP_DIFF) {\n        /* sort sets from the smallest to largest, this will improve our\n        * algorithm's performance */\n        qsort(src,setnum,sizeof(zsetopsrc),zuiCompareByCardinality);\n    }\n\n    dstobj = createZsetObject();\n    dstzset = (zset*)ptrFromObj(dstobj);\n    memset(&zval, 0, sizeof(zval));\n\n    if (op == SET_OP_INTER) {\n        /* Skip everything if the smallest input is empty. */\n        if (zuiLength(&src[0]) > 0) {\n            /* Precondition: as src[0] is non-empty and the inputs are ordered\n             * by size, all src[i > 0] are non-empty too. */\n            zuiInitIterator(&src[0]);\n            while (zuiNext(&src[0],&zval)) {\n                double score, value;\n\n                score = src[0].weight * zval.score;\n                if (std::isnan(score)) score = 0;\n\n                for (j = 1; j < setnum; j++) {\n                    /* It is not safe to access the zset we are\n                     * iterating, so explicitly check for equal object. */\n                    if (src[j].subject == src[0].subject) {\n                        value = zval.score*src[j].weight;\n                        zunionInterAggregate(&score,value,aggregate);\n                    } else if (zuiFind(&src[j],&zval,&value)) {\n                        value *= src[j].weight;\n                        zunionInterAggregate(&score,value,aggregate);\n                    } else {\n                        break;\n                    }\n                }\n\n                /* Only continue when present in every input. */\n                if (j == setnum) {\n                    tmp = zuiNewSdsFromValue(&zval);\n                    znode = zslInsert(dstzset->zsl,score,tmp);\n                    dictAdd(dstzset->dict,tmp,&znode->score);\n                    totelelen += sdslen(tmp);\n                    if (sdslen(tmp) > maxelelen) maxelelen = sdslen(tmp);\n                }\n            }\n            zuiClearIterator(&src[0]);\n        }\n    } else if (op == SET_OP_UNION) {\n        dict *accumulator = dictCreate(&setAccumulatorDictType,NULL);\n        dictIterator *di;\n        dictEntry *de, *existing;\n        double score;\n\n        if (setnum) {\n            /* Our union is at least as large as the largest set.\n             * Resize the dictionary ASAP to avoid useless rehashing. */\n            dictExpand(accumulator,zuiLength(&src[setnum-1]));\n        }\n\n        /* Step 1: Create a dictionary of elements -> aggregated-scores\n         * by iterating one sorted set after the other. */\n        for (i = 0; i < setnum; i++) {\n            if (zuiLength(&src[i]) == 0) continue;\n\n            zuiInitIterator(&src[i]);\n            while (zuiNext(&src[i],&zval)) {\n                /* Initialize value */\n                score = src[i].weight * zval.score;\n                if (std::isnan(score)) score = 0;\n\n                /* Search for this element in the accumulating dictionary. */\n                de = dictAddRaw(accumulator,zuiSdsFromValue(&zval),&existing);\n                /* If we don't have it, we need to create a new entry. */\n                if (!existing) {\n                    tmp = zuiNewSdsFromValue(&zval);\n                    /* Remember the longest single element encountered,\n                     * to understand if it's possible to convert to ziplist\n                     * at the end. */\n                     totelelen += sdslen(tmp);\n                     if (sdslen(tmp) > maxelelen) maxelelen = sdslen(tmp);\n                    /* Update the element with its initial score. */\n                    dictSetKey(accumulator, de, tmp);\n                    dictSetDoubleVal(de,score);\n                } else {\n                    /* Update the score with the score of the new instance\n                     * of the element found in the current sorted set.\n                     *\n                     * Here we access directly the dictEntry double\n                     * value inside the union as it is a big speedup\n                     * compared to using the getDouble/setDouble API. */\n                    zunionInterAggregate(&existing->v.d,score,aggregate);\n                }\n            }\n            zuiClearIterator(&src[i]);\n        }\n\n        /* Step 2: convert the dictionary into the final sorted set. */\n        di = dictGetIterator(accumulator);\n\n        /* We now are aware of the final size of the resulting sorted set,\n         * let's resize the dictionary embedded inside the sorted set to the\n         * right size, in order to save rehashing time. */\n        dictExpand(dstzset->dict,dictSize(accumulator));\n\n        while((de = dictNext(di)) != NULL) {\n            sds ele = (sds)dictGetKey(de);\n            score = dictGetDoubleVal(de);\n            znode = zslInsert(dstzset->zsl,score,ele);\n            dictAdd(dstzset->dict,ele,&znode->score);\n        }\n        dictReleaseIterator(di);\n        dictRelease(accumulator);\n    } else if (op == SET_OP_DIFF) {\n        zdiff(src, setnum, dstzset, &maxelelen, &totelelen);\n    } else {\n        serverPanic(\"Unknown operator\");\n    }\n\n    if (dstkey) {\n        if (dstzset->zsl->length) {\n            zsetConvertToZiplistIfNeeded(dstobj, maxelelen, totelelen);\n            setKey(c, c->db, dstkey, dstobj);\n            addReplyLongLong(c, zsetLength(dstobj));\n            notifyKeyspaceEvent(NOTIFY_ZSET,\n                                (op == SET_OP_UNION) ? \"zunionstore\" :\n                                    (op == SET_OP_INTER ? \"zinterstore\" : \"zdiffstore\"),\n                                dstkey, c->db->id);\n            g_pserver->dirty++;\n        } else {\n            addReply(c, shared.czero);\n            if (dbDelete(c->db, dstkey)) {\n                signalModifiedKey(c, c->db, dstkey);\n                notifyKeyspaceEvent(NOTIFY_GENERIC, \"del\", dstkey, c->db->id);\n                g_pserver->dirty++;\n            }\n        }\n    } else {\n        unsigned long length = dstzset->zsl->length;\n        zskiplist *zsl = dstzset->zsl;\n        zskiplistNode *zn = zsl->header->level(0)->forward;\n        /* In case of WITHSCORES, respond with a single array in RESP2, and\n         * nested arrays in RESP3. We can't use a map response type since the\n         * client library needs to know to respect the order. */\n        if (withscores && c->resp == 2)\n            addReplyArrayLen(c, length*2);\n        else\n            addReplyArrayLen(c, length);\n\n        while (zn != NULL) {\n            if (withscores && c->resp > 2) addReplyArrayLen(c,2);\n            addReplyBulkCBuffer(c,zn->ele,sdslen(zn->ele));\n            if (withscores) addReplyDouble(c,zn->score);\n            zn = zn->level(0)->forward;\n        }\n    }\n    decrRefCount(dstobj);\n    zfree(src);\n}\n\nvoid zunionstoreCommand(client *c) {\n    zunionInterDiffGenericCommand(c, c->argv[1], 2, SET_OP_UNION);\n}\n\nvoid zinterstoreCommand(client *c) {\n    zunionInterDiffGenericCommand(c, c->argv[1], 2, SET_OP_INTER);\n}\n\nvoid zdiffstoreCommand(client *c) {\n    zunionInterDiffGenericCommand(c, c->argv[1], 2, SET_OP_DIFF);\n}\n\nvoid zunionCommand(client *c) {\n    zunionInterDiffGenericCommand(c, NULL, 1, SET_OP_UNION);\n}\n\nvoid zinterCommand(client *c) {\n    zunionInterDiffGenericCommand(c, NULL, 1, SET_OP_INTER);\n}\n\nvoid zdiffCommand(client *c) {\n    zunionInterDiffGenericCommand(c, NULL, 1, SET_OP_DIFF);\n}\n\ntypedef enum {\n    ZRANGE_DIRECTION_AUTO = 0,\n    ZRANGE_DIRECTION_FORWARD,\n    ZRANGE_DIRECTION_REVERSE\n} zrange_direction;\n\ntypedef enum {\n    ZRANGE_CONSUMER_TYPE_CLIENT = 0,\n    ZRANGE_CONSUMER_TYPE_INTERNAL\n} zrange_consumer_type;\n\ntypedef struct zrange_result_handler zrange_result_handler;\n\ntypedef void (*zrangeResultBeginFunction)(zrange_result_handler *c);\ntypedef void (*zrangeResultFinalizeFunction)(\n    zrange_result_handler *c, size_t result_count);\ntypedef void (*zrangeResultEmitCBufferFunction)(\n    zrange_result_handler *c, const void *p, size_t len, double score);\ntypedef void (*zrangeResultEmitLongLongFunction)(\n    zrange_result_handler *c, long long ll, double score);\n\nvoid zrangeGenericCommand (zrange_result_handler *handler, int argc_start, int store,\n                           zrange_type rangetype, zrange_direction direction);\n\n/* Interface struct for ZRANGE/ZRANGESTORE generic implementation.\n * There is one implementation of this interface that sends a RESP reply to clients.\n * and one implementation that stores the range result into a zset object. */\nstruct zrange_result_handler {\n    zrange_consumer_type                 type;\n    ::client                            *client;\n    robj                                *dstkey;\n    robj                                *dstobj;\n    void                                *userdata;\n    int                                  withscores;\n    int                                  should_emit_array_length;\n    zrangeResultBeginFunction            beginResultEmission;\n    zrangeResultFinalizeFunction         finalizeResultEmission;\n    zrangeResultEmitCBufferFunction      emitResultFromCBuffer;\n    zrangeResultEmitLongLongFunction     emitResultFromLongLong;\n};\n\n/* Result handler methods for responding the ZRANGE to clients. */\nstatic void zrangeResultBeginClient(zrange_result_handler *handler) {\n    handler->userdata = addReplyDeferredLen(handler->client);\n}\n\nstatic void zrangeResultEmitCBufferToClient(zrange_result_handler *handler,\n    const void *value, size_t value_length_in_bytes, double score)\n{\n    if (handler->should_emit_array_length) {\n        addReplyArrayLen(handler->client, 2);\n    }\n\n    addReplyBulkCBuffer(handler->client, value, value_length_in_bytes);\n\n    if (handler->withscores) {\n        addReplyDouble(handler->client, score);\n    }\n}\n\nstatic void zrangeResultEmitLongLongToClient(zrange_result_handler *handler,\n    long long value, double score)\n{\n    if (handler->should_emit_array_length) {\n        addReplyArrayLen(handler->client, 2);\n    }\n\n    addReplyBulkLongLong(handler->client, value);\n\n    if (handler->withscores) {\n        addReplyDouble(handler->client, score);\n    }\n}\n\nstatic void zrangeResultFinalizeClient(zrange_result_handler *handler,\n    size_t result_count)\n{\n    /* In case of WITHSCORES, respond with a single array in RESP2, and\n     * nested arrays in RESP3. We can't use a map response type since the\n     * client library needs to know to respect the order. */\n    if (handler->withscores && (handler->client->resp == 2)) {\n        result_count *= 2;\n    }\n\n    setDeferredArrayLen(handler->client, handler->userdata, result_count);\n}\n\n/* Result handler methods for storing the ZRANGESTORE to a zset. */\nstatic void zrangeResultBeginStore(zrange_result_handler *handler)\n{\n    handler->dstobj = createZsetZiplistObject();\n}\n\nstatic void zrangeResultEmitCBufferForStore(zrange_result_handler *handler,\n    const void *value, size_t value_length_in_bytes, double score)\n{\n    double newscore;\n    int retflags = 0;\n    sds ele = sdsnewlen(value, value_length_in_bytes);\n    int retval = zsetAdd(handler->dstobj, score, ele, ZADD_IN_NONE, &retflags, &newscore);\n    sdsfree(ele);\n    serverAssert(retval);\n}\n\nstatic void zrangeResultEmitLongLongForStore(zrange_result_handler *handler,\n    long long value, double score)\n{\n    double newscore;\n    int retflags = 0;\n    sds ele = sdsfromlonglong(value);\n    int retval = zsetAdd(handler->dstobj, score, ele, ZADD_IN_NONE, &retflags, &newscore);\n    sdsfree(ele);\n    serverAssert(retval);\n}\n\nstatic void zrangeResultFinalizeStore(zrange_result_handler *handler, size_t result_count)\n{\n    if (result_count) {\n        setKey(handler->client, handler->client->db, handler->dstkey, handler->dstobj);\n        addReplyLongLong(handler->client, result_count);\n        notifyKeyspaceEvent(NOTIFY_ZSET, \"zrangestore\", handler->dstkey, handler->client->db->id);\n        g_pserver->dirty++;\n    } else {\n        addReply(handler->client, shared.czero);\n        if (dbDelete(handler->client->db, handler->dstkey)) {\n            signalModifiedKey(handler->client, handler->client->db, handler->dstkey);\n            notifyKeyspaceEvent(NOTIFY_GENERIC, \"del\", handler->dstkey, handler->client->db->id);\n            g_pserver->dirty++;\n        }\n    }\n    decrRefCount(handler->dstobj);\n}\n\n/* Initialize the consumer interface type with the requested type. */\nstatic void zrangeResultHandlerInit(zrange_result_handler *handler,\n    client *client, zrange_consumer_type type)\n{\n    memset(handler, 0, sizeof(*handler));\n\n    handler->client = client;\n\n    switch (type) {\n    case ZRANGE_CONSUMER_TYPE_CLIENT:\n        handler->beginResultEmission = zrangeResultBeginClient;\n        handler->finalizeResultEmission = zrangeResultFinalizeClient;\n        handler->emitResultFromCBuffer = zrangeResultEmitCBufferToClient;\n        handler->emitResultFromLongLong = zrangeResultEmitLongLongToClient;\n        break;\n\n    case ZRANGE_CONSUMER_TYPE_INTERNAL:\n        handler->beginResultEmission = zrangeResultBeginStore;\n        handler->finalizeResultEmission = zrangeResultFinalizeStore;\n        handler->emitResultFromCBuffer = zrangeResultEmitCBufferForStore;\n        handler->emitResultFromLongLong = zrangeResultEmitLongLongForStore;\n        break;\n    }\n}\n\nstatic void zrangeResultHandlerScoreEmissionEnable(zrange_result_handler *handler) {\n    handler->withscores = 1;\n    handler->should_emit_array_length = (handler->client->resp > 2);\n}\n\nstatic void zrangeResultHandlerDestinationKeySet (zrange_result_handler *handler,\n    robj *dstkey)\n{\n    handler->dstkey = dstkey;\n}\n\n/* This command implements ZRANGE, ZREVRANGE. */\nvoid genericZrangebyrankCommand(zrange_result_handler *handler,\n    robj_roptr zobj, long start, long end, int withscores, int reverse) {\n\n    client *c = handler->client;\n    long llen;\n    long rangelen;\n    size_t result_cardinality;\n\n    /* Sanitize indexes. */\n    llen = zsetLength(zobj);\n    if (start < 0) start = llen+start;\n    if (end < 0) end = llen+end;\n    if (start < 0) start = 0;\n\n    handler->beginResultEmission(handler);\n\n    /* Invariant: start >= 0, so this test will be true when end < 0.\n     * The range is empty when start > end or start >= length. */\n    if (start > end || start >= llen) {\n        handler->finalizeResultEmission(handler, 0);\n        return;\n    }\n    if (end >= llen) end = llen-1;\n    rangelen = (end-start)+1;\n    result_cardinality = rangelen;\n\n    if (zobj->encoding == OBJ_ENCODING_ZIPLIST) {\n        unsigned char *zl = (unsigned char*)zobj->m_ptr;\n        unsigned char *eptr, *sptr;\n        unsigned char *vstr;\n        unsigned int vlen;\n        long long vlong;\n        double score = 0.0;\n\n        if (reverse)\n            eptr = ziplistIndex(zl,-2-(2*start));\n        else\n            eptr = ziplistIndex(zl,2*start);\n\n        serverAssertWithInfo(c,zobj,eptr != NULL);\n        sptr = ziplistNext(zl,eptr);\n\n        while (rangelen--) {\n            serverAssertWithInfo(c,zobj,eptr != NULL && sptr != NULL);\n            serverAssertWithInfo(c,zobj,ziplistGet(eptr,&vstr,&vlen,&vlong));\n\n            if (withscores) /* don't bother to extract the score if it's gonna be ignored. */\n                score = zzlGetScore(sptr);\n\n            if (vstr == NULL) {\n                handler->emitResultFromLongLong(handler, vlong, score);\n            } else {\n                handler->emitResultFromCBuffer(handler, vstr, vlen, score);\n            }\n\n            if (reverse)\n                zzlPrev(zl,&eptr,&sptr);\n            else\n                zzlNext(zl,&eptr,&sptr);\n        }\n\n    } else if (zobj->encoding == OBJ_ENCODING_SKIPLIST) {\n        zset *zs = (zset*)zobj->m_ptr;\n        zskiplist *zsl = zs->zsl;\n        zskiplistNode *ln;\n\n        /* Check if starting point is trivial, before doing log(N) lookup. */\n        if (reverse) {\n            ln = zsl->tail;\n            if (start > 0)\n                ln = zslGetElementByRank(zsl,llen-start);\n        } else {\n            ln = zsl->header->level(0)->forward;\n            if (start > 0)\n                ln = zslGetElementByRank(zsl,start+1);\n        }\n\n        while(rangelen--) {\n            serverAssertWithInfo(c,zobj,ln != NULL);\n            sds ele = ln->ele;\n            handler->emitResultFromCBuffer(handler, ele, sdslen(ele), ln->score);\n            ln = reverse ? ln->backward : ln->level(0)->forward;\n        }\n    } else {\n        serverPanic(\"Unknown sorted set encoding\");\n    }\n\n    handler->finalizeResultEmission(handler, result_cardinality);\n}\n\n/* ZRANGESTORE <dst> <src> <min> <max> [BYSCORE | BYLEX] [REV] [LIMIT offset count] */\nvoid zrangestoreCommand (client *c) {\n    robj *dstkey = c->argv[1];\n    zrange_result_handler handler;\n    zrangeResultHandlerInit(&handler, c, ZRANGE_CONSUMER_TYPE_INTERNAL);\n    zrangeResultHandlerDestinationKeySet(&handler, dstkey);\n    zrangeGenericCommand(&handler, 2, 1, ZRANGE_AUTO, ZRANGE_DIRECTION_AUTO);\n}\n\n/* ZRANGE <key> <min> <max> [BYSCORE | BYLEX] [REV] [WITHSCORES] [LIMIT offset count] */\nvoid zrangeCommand(client *c) {\n    zrange_result_handler handler;\n    zrangeResultHandlerInit(&handler, c, ZRANGE_CONSUMER_TYPE_CLIENT);\n    zrangeGenericCommand(&handler, 1, 0, ZRANGE_AUTO, ZRANGE_DIRECTION_AUTO);\n}\n\n/* ZREVRANGE <key> <min> <max> [WITHSCORES] */\nvoid zrevrangeCommand(client *c) {\n    zrange_result_handler handler;\n    zrangeResultHandlerInit(&handler, c, ZRANGE_CONSUMER_TYPE_CLIENT);\n    zrangeGenericCommand(&handler, 1, 0, ZRANGE_RANK, ZRANGE_DIRECTION_REVERSE);\n}\n\n/* This command implements ZRANGEBYSCORE, ZREVRANGEBYSCORE. */\nvoid genericZrangebyscoreCommand(zrange_result_handler *handler,\n    zrangespec *range, robj_roptr zobj, long offset, long limit, \n    int reverse) {\n\n    client *c = handler->client;\n    unsigned long rangelen = 0;\n\n    handler->beginResultEmission(handler);\n\n    /* For invalid offset, return directly. */\n    if (offset > 0 && offset >= (long)zsetLength(zobj)) {\n        handler->finalizeResultEmission(handler, 0);\n        return;\n    }\n\n    if (zobj->encoding == OBJ_ENCODING_ZIPLIST) {\n        unsigned char *zl = (unsigned char*)zobj->m_ptr;\n        unsigned char *eptr, *sptr;\n        unsigned char *vstr;\n        unsigned int vlen;\n        long long vlong;\n\n        /* If reversed, get the last node in range as starting point. */\n        if (reverse) {\n            eptr = zzlLastInRange(zl,range);\n        } else {\n            eptr = zzlFirstInRange(zl,range);\n        }\n\n        /* Get score pointer for the first element. */\n        if (eptr)\n            sptr = ziplistNext(zl,eptr);\n\n        /* If there is an offset, just traverse the number of elements without\n         * checking the score because that is done in the next loop. */\n        while (eptr && offset--) {\n            if (reverse) {\n                zzlPrev(zl,&eptr,&sptr);\n            } else {\n                zzlNext(zl,&eptr,&sptr);\n            }\n        }\n\n        while (eptr && limit--) {\n            double score = zzlGetScore(sptr);\n\n            /* Abort when the node is no longer in range. */\n            if (reverse) {\n                if (!zslValueGteMin(score,range)) break;\n            } else {\n                if (!zslValueLteMax(score,range)) break;\n            }\n\n            /* We know the element exists, so ziplistGet should always\n             * succeed */\n            serverAssertWithInfo(c,zobj,ziplistGet(eptr,&vstr,&vlen,&vlong));\n\n            rangelen++;\n            if (vstr == NULL) {\n                handler->emitResultFromLongLong(handler, vlong, score);\n            } else {\n                handler->emitResultFromCBuffer(handler, vstr, vlen, score);\n            }\n\n            /* Move to next node */\n            if (reverse) {\n                zzlPrev(zl,&eptr,&sptr);\n            } else {\n                zzlNext(zl,&eptr,&sptr);\n            }\n        }\n    } else if (zobj->encoding == OBJ_ENCODING_SKIPLIST) {\n        zset *zs = (zset*)zobj->m_ptr;\n        zskiplist *zsl = zs->zsl;\n        zskiplistNode *ln;\n\n        /* If reversed, get the last node in range as starting point. */\n        if (reverse) {\n            ln = zslLastInRange(zsl,range);\n        } else {\n            ln = zslFirstInRange(zsl,range);\n        }\n\n        /* If there is an offset, just traverse the number of elements without\n         * checking the score because that is done in the next loop. */\n        while (ln && offset--) {\n            if (reverse) {\n                ln = ln->backward;\n            } else {\n                ln = ln->level(0)->forward;\n            }\n        }\n\n        while (ln && limit--) {\n            /* Abort when the node is no longer in range. */\n            if (reverse) {\n                if (!zslValueGteMin(ln->score,range)) break;\n            } else {\n                if (!zslValueLteMax(ln->score,range)) break;\n            }\n\n            rangelen++;\n            handler->emitResultFromCBuffer(handler, ln->ele, sdslen(ln->ele), ln->score);\n\n            /* Move to next node */\n            if (reverse) {\n                ln = ln->backward;\n            } else {\n                ln = ln->level(0)->forward;\n            }\n        }\n    } else {\n        serverPanic(\"Unknown sorted set encoding\");\n    }\n\n    handler->finalizeResultEmission(handler, rangelen);\n}\n\n/* ZRANGEBYSCORE <key> <min> <max> [WITHSCORES] [LIMIT offset count] */\nvoid zrangebyscoreCommand(client *c) {\n    zrange_result_handler handler;\n    zrangeResultHandlerInit(&handler, c, ZRANGE_CONSUMER_TYPE_CLIENT);\n    zrangeGenericCommand(&handler, 1, 0, ZRANGE_SCORE, ZRANGE_DIRECTION_FORWARD);\n}\n\n/* ZREVRANGEBYSCORE <key> <min> <max> [WITHSCORES] [LIMIT offset count] */\nvoid zrevrangebyscoreCommand(client *c) {\n    zrange_result_handler handler;\n    zrangeResultHandlerInit(&handler, c, ZRANGE_CONSUMER_TYPE_CLIENT);\n    zrangeGenericCommand(&handler, 1, 0, ZRANGE_SCORE, ZRANGE_DIRECTION_REVERSE);\n}\n\nvoid zcountCommand(client *c) {\n    robj *key = c->argv[1];\n    robj_roptr zobj;\n    zrangespec range;\n    unsigned long count = 0;\n\n    /* Parse the range arguments */\n    if (zslParseRange(c->argv[2],c->argv[3],&range) != C_OK) {\n        addReplyError(c,\"min or max is not a float\");\n        return;\n    }\n\n    /* Lookup the sorted set */\n    if ((zobj = lookupKeyReadOrReply(c, key, shared.czero)) == nullptr ||\n        checkType(c, zobj, OBJ_ZSET)) return;\n\n    if (zobj->encoding == OBJ_ENCODING_ZIPLIST) {\n        unsigned char *zl = (unsigned char*)zobj->m_ptr;\n        unsigned char *eptr, *sptr;\n        double score;\n\n        /* Use the first element in range as the starting point */\n        eptr = zzlFirstInRange(zl,&range);\n\n        /* No \"first\" element */\n        if (eptr == NULL) {\n            addReply(c, shared.czero);\n            return;\n        }\n\n        /* First element is in range */\n        sptr = ziplistNext(zl,eptr);\n        score = zzlGetScore(sptr);\n        serverAssertWithInfo(c,zobj,zslValueLteMax(score,&range));\n\n        /* Iterate over elements in range */\n        while (eptr) {\n            score = zzlGetScore(sptr);\n\n            /* Abort when the node is no longer in range. */\n            if (!zslValueLteMax(score,&range)) {\n                break;\n            } else {\n                count++;\n                zzlNext(zl,&eptr,&sptr);\n            }\n        }\n    } else if (zobj->encoding == OBJ_ENCODING_SKIPLIST) {\n        zset *zs = (zset*)zobj->m_ptr;\n        zskiplist *zsl = zs->zsl;\n        zskiplistNode *zn;\n        unsigned long rank;\n\n        /* Find first element in range */\n        zn = zslFirstInRange(zsl, &range);\n\n        /* Use rank of first element, if any, to determine preliminary count */\n        if (zn != NULL) {\n            rank = zslGetRank(zsl, zn->score, zn->ele);\n            count = (zsl->length - (rank - 1));\n\n            /* Find last element in range */\n            zn = zslLastInRange(zsl, &range);\n\n            /* Use rank of last element, if any, to determine the actual count */\n            if (zn != NULL) {\n                rank = zslGetRank(zsl, zn->score, zn->ele);\n                count -= (zsl->length - rank);\n            }\n        }\n    } else {\n        serverPanic(\"Unknown sorted set encoding\");\n    }\n\n    addReplyLongLong(c, count);\n}\n\nvoid zlexcountCommand(client *c) {\n    robj *key = c->argv[1];\n    robj_roptr zobj;\n    zlexrangespec range;\n    unsigned long count = 0;\n\n    /* Parse the range arguments */\n    if (zslParseLexRange(c->argv[2],c->argv[3],&range) != C_OK) {\n        addReplyError(c,\"min or max not valid string range item\");\n        return;\n    }\n\n    /* Lookup the sorted set */\n    if ((zobj = lookupKeyReadOrReply(c, key, shared.czero)) == nullptr ||\n        checkType(c, zobj, OBJ_ZSET))\n    {\n        zslFreeLexRange(&range);\n        return;\n    }\n\n    if (zobj->encoding == OBJ_ENCODING_ZIPLIST) {\n        unsigned char *zl = (unsigned char*)zobj->m_ptr;\n        unsigned char *eptr, *sptr;\n\n        /* Use the first element in range as the starting point */\n        eptr = zzlFirstInLexRange(zl,&range);\n\n        /* No \"first\" element */\n        if (eptr == NULL) {\n            zslFreeLexRange(&range);\n            addReply(c, shared.czero);\n            return;\n        }\n\n        /* First element is in range */\n        sptr = ziplistNext(zl,eptr);\n        serverAssertWithInfo(c,zobj,zzlLexValueLteMax(eptr,&range));\n\n        /* Iterate over elements in range */\n        while (eptr) {\n            /* Abort when the node is no longer in range. */\n            if (!zzlLexValueLteMax(eptr,&range)) {\n                break;\n            } else {\n                count++;\n                zzlNext(zl,&eptr,&sptr);\n            }\n        }\n    } else if (zobj->encoding == OBJ_ENCODING_SKIPLIST) {\n        zset *zs = (zset*)zobj->m_ptr;\n        zskiplist *zsl = zs->zsl;\n        zskiplistNode *zn;\n        unsigned long rank;\n\n        /* Find first element in range */\n        zn = zslFirstInLexRange(zsl, &range);\n\n        /* Use rank of first element, if any, to determine preliminary count */\n        if (zn != NULL) {\n            rank = zslGetRank(zsl, zn->score, zn->ele);\n            count = (zsl->length - (rank - 1));\n\n            /* Find last element in range */\n            zn = zslLastInLexRange(zsl, &range);\n\n            /* Use rank of last element, if any, to determine the actual count */\n            if (zn != NULL) {\n                rank = zslGetRank(zsl, zn->score, zn->ele);\n                count -= (zsl->length - rank);\n            }\n        }\n    } else {\n        serverPanic(\"Unknown sorted set encoding\");\n    }\n\n    zslFreeLexRange(&range);\n    addReplyLongLong(c, count);\n}\n\n/* This command implements ZRANGEBYLEX, ZREVRANGEBYLEX. */\nvoid genericZrangebylexCommand(zrange_result_handler *handler,\n    zlexrangespec *range, robj_roptr zobj, int withscores, long offset, long limit,\n    int reverse)\n{\n    client *c = handler->client;\n    unsigned long rangelen = 0;\n\n    handler->beginResultEmission(handler);\n\n    if (zobj->encoding == OBJ_ENCODING_ZIPLIST) {\n        unsigned char *zl = (unsigned char*)zobj->m_ptr;\n        unsigned char *eptr, *sptr;\n        unsigned char *vstr;\n        unsigned int vlen;\n        long long vlong;\n\n        /* If reversed, get the last node in range as starting point. */\n        if (reverse) {\n            eptr = zzlLastInLexRange(zl,range);\n        } else {\n            eptr = zzlFirstInLexRange(zl,range);\n        }\n\n        /* Get score pointer for the first element. */\n        if (eptr)\n            sptr = ziplistNext(zl,eptr);\n\n        /* If there is an offset, just traverse the number of elements without\n         * checking the score because that is done in the next loop. */\n        while (eptr && offset--) {\n            if (reverse) {\n                zzlPrev(zl,&eptr,&sptr);\n            } else {\n                zzlNext(zl,&eptr,&sptr);\n            }\n        }\n\n        while (eptr && limit--) {\n            double score = 0;\n            if (withscores) /* don't bother to extract the score if it's gonna be ignored. */\n                score = zzlGetScore(sptr);\n\n            /* Abort when the node is no longer in range. */\n            if (reverse) {\n                if (!zzlLexValueGteMin(eptr,range)) break;\n            } else {\n                if (!zzlLexValueLteMax(eptr,range)) break;\n            }\n\n            /* We know the element exists, so ziplistGet should always\n             * succeed. */\n            serverAssertWithInfo(c,zobj,ziplistGet(eptr,&vstr,&vlen,&vlong));\n\n            rangelen++;\n            if (vstr == NULL) {\n                handler->emitResultFromLongLong(handler, vlong, score);\n            } else {\n                handler->emitResultFromCBuffer(handler, vstr, vlen, score);\n            }\n\n            /* Move to next node */\n            if (reverse) {\n                zzlPrev(zl,&eptr,&sptr);\n            } else {\n                zzlNext(zl,&eptr,&sptr);\n            }\n        }\n    } else if (zobj->encoding == OBJ_ENCODING_SKIPLIST) {\n        zset *zs = (zset*)zobj->m_ptr;\n        zskiplist *zsl = zs->zsl;\n        zskiplistNode *ln;\n\n        /* If reversed, get the last node in range as starting point. */\n        if (reverse) {\n            ln = zslLastInLexRange(zsl,range);\n        } else {\n            ln = zslFirstInLexRange(zsl,range);\n        }\n\n        /* If there is an offset, just traverse the number of elements without\n         * checking the score because that is done in the next loop. */\n        while (ln && offset--) {\n            if (reverse) {\n                ln = ln->backward;\n            } else {\n                ln = ln->level(0)->forward;\n            }\n        }\n\n        while (ln && limit--) {\n            /* Abort when the node is no longer in range. */\n            if (reverse) {\n                if (!zslLexValueGteMin(ln->ele,range)) break;\n            } else {\n                if (!zslLexValueLteMax(ln->ele,range)) break;\n            }\n\n            rangelen++;\n            handler->emitResultFromCBuffer(handler, ln->ele, sdslen(ln->ele), ln->score);\n\n            /* Move to next node */\n            if (reverse) {\n                ln = ln->backward;\n            } else {\n                ln = ln->level(0)->forward;\n            }\n        }\n    } else {\n        serverPanic(\"Unknown sorted set encoding\");\n    }\n\n    handler->finalizeResultEmission(handler, rangelen);\n}\n\n/* ZRANGEBYLEX <key> <min> <max> [LIMIT offset count] */\nvoid zrangebylexCommand(client *c) {\n    zrange_result_handler handler;\n    zrangeResultHandlerInit(&handler, c, ZRANGE_CONSUMER_TYPE_CLIENT);\n    zrangeGenericCommand(&handler, 1, 0, ZRANGE_LEX, ZRANGE_DIRECTION_FORWARD);\n}\n\n/* ZREVRANGEBYLEX <key> <min> <max> [LIMIT offset count] */\nvoid zrevrangebylexCommand(client *c) {\n    zrange_result_handler handler;\n    zrangeResultHandlerInit(&handler, c, ZRANGE_CONSUMER_TYPE_CLIENT);\n    zrangeGenericCommand(&handler, 1, 0, ZRANGE_LEX, ZRANGE_DIRECTION_REVERSE);\n}\n\n/**\n * This function handles ZRANGE and ZRANGESTORE, and also the deprecated\n * Z[REV]RANGE[BYPOS|BYLEX] commands.\n *\n * The simple ZRANGE and ZRANGESTORE can take _AUTO in rangetype and direction,\n * other command pass explicit value.\n *\n * The argc_start points to the src key argument, so following syntax is like:\n * <src> <min> <max> [BYSCORE | BYLEX] [REV] [WITHSCORES] [LIMIT offset count]\n */\nvoid zrangeGenericCommand(zrange_result_handler *handler, int argc_start, int store,\n                          zrange_type rangetype, zrange_direction direction)\n{\n    client *c = handler->client;\n    robj *key = c->argv[argc_start];\n    robj_roptr zobj;\n    zrangespec range;\n    zlexrangespec lexrange;\n    int minidx = argc_start + 1;\n    int maxidx = argc_start + 2;\n\n    /* Options common to all */\n    long opt_start = 0;\n    long opt_end = 0;\n    int opt_withscores = 0;\n    long opt_offset = 0;\n    long opt_limit = -1;\n\n    /* Step 1: Skip the <src> <min> <max> args and parse remaining optional arguments. */\n    for (int j=argc_start + 3; j < c->argc; j++) {\n        int leftargs = c->argc-j-1;\n        if (!store && !strcasecmp(szFromObj(c->argv[j]),\"withscores\")) {\n            opt_withscores = 1;\n        } else if (!strcasecmp(szFromObj(c->argv[j]),\"limit\") && leftargs >= 2) {\n            if ((getLongFromObjectOrReply(c, c->argv[j+1], &opt_offset, NULL) != C_OK) ||\n                (getLongFromObjectOrReply(c, c->argv[j+2], &opt_limit, NULL) != C_OK))\n            {\n                return;\n            }\n            j += 2;\n        } else if (direction == ZRANGE_DIRECTION_AUTO &&\n                   !strcasecmp(szFromObj(c->argv[j]),\"rev\"))\n        {\n            direction = ZRANGE_DIRECTION_REVERSE;\n        } else if (rangetype == ZRANGE_AUTO &&\n                   !strcasecmp(szFromObj(c->argv[j]),\"bylex\"))\n        {\n            rangetype = ZRANGE_LEX;\n        } else if (rangetype == ZRANGE_AUTO &&\n                   !strcasecmp(szFromObj(c->argv[j]),\"byscore\"))\n        {\n            rangetype = ZRANGE_SCORE;\n        } else {\n            addReplyErrorObject(c,shared.syntaxerr);\n            return;\n        }\n    }\n\n    /* Use defaults if not overriden by arguments. */\n    if (direction == ZRANGE_DIRECTION_AUTO)\n        direction = ZRANGE_DIRECTION_FORWARD;\n    if (rangetype == ZRANGE_AUTO)\n        rangetype = ZRANGE_RANK;\n\n    /* Check for conflicting arguments. */\n    if (opt_limit != -1 && rangetype == ZRANGE_RANK) {\n        addReplyError(c,\"syntax error, LIMIT is only supported in combination with either BYSCORE or BYLEX\");\n        return;\n    }\n    if (opt_withscores && rangetype == ZRANGE_LEX) {\n        addReplyError(c,\"syntax error, WITHSCORES not supported in combination with BYLEX\");\n        return;\n    }\n\n    if (direction == ZRANGE_DIRECTION_REVERSE &&\n        ((ZRANGE_SCORE == rangetype) || (ZRANGE_LEX == rangetype)))\n    {\n        /* Range is given as [max,min] */\n        int tmp = maxidx;\n        maxidx = minidx;\n        minidx = tmp;\n    }\n\n    /* Step 2: Parse the range. */\n    switch (rangetype) {\n    case ZRANGE_AUTO:\n    case ZRANGE_RANK:\n        /* Z[REV]RANGE, ZRANGESTORE [REV]RANGE */\n        if ((getLongFromObjectOrReply(c, c->argv[minidx], &opt_start,NULL) != C_OK) ||\n            (getLongFromObjectOrReply(c, c->argv[maxidx], &opt_end,NULL) != C_OK))\n        {\n            return;\n        }\n        break;\n\n    case ZRANGE_SCORE:\n        /* Z[REV]RANGEBYSCORE, ZRANGESTORE [REV]RANGEBYSCORE */\n        if (zslParseRange(c->argv[minidx], c->argv[maxidx], &range) != C_OK) {\n            addReplyError(c, \"min or max is not a float\");\n            return;\n        }\n        break;\n\n    case ZRANGE_LEX:\n        /* Z[REV]RANGEBYLEX, ZRANGESTORE [REV]RANGEBYLEX */\n        if (zslParseLexRange(c->argv[minidx], c->argv[maxidx], &lexrange) != C_OK) {\n            addReplyError(c, \"min or max not valid string range item\");\n            return;\n        }\n        break;\n    }\n\n    if (opt_withscores || store) {\n        zrangeResultHandlerScoreEmissionEnable(handler);\n    }\n\n    /* Step 3: Lookup the key and get the range. */\n    zobj = handler->dstkey ?\n        lookupKeyWrite(c->db,key) :\n        lookupKeyRead(c->db,key);\n    if (zobj == nullptr) {\n        if (store) {\n            handler->beginResultEmission(handler);\n            handler->finalizeResultEmission(handler, 0);\n        } else {\n            addReply(c, shared.emptyarray);\n        }\n        goto cleanup;\n    }\n\n    if (checkType(c,zobj,OBJ_ZSET)) goto cleanup;\n\n    /* Step 4: Pass this to the command-specific handler. */\n    switch (rangetype) {\n    case ZRANGE_AUTO:\n    case ZRANGE_RANK:\n        genericZrangebyrankCommand(handler, zobj, opt_start, opt_end,\n            opt_withscores || store, direction == ZRANGE_DIRECTION_REVERSE);\n        break;\n\n    case ZRANGE_SCORE:\n        genericZrangebyscoreCommand(handler, &range, zobj, opt_offset,\n            opt_limit, direction == ZRANGE_DIRECTION_REVERSE);\n        break;\n\n    case ZRANGE_LEX:\n        genericZrangebylexCommand(handler, &lexrange, zobj, opt_withscores || store,\n            opt_offset, opt_limit, direction == ZRANGE_DIRECTION_REVERSE);\n        break;\n    }\n\n    /* Instead of returning here, we'll just fall-through the clean-up. */\n\ncleanup:\n\n    if (rangetype == ZRANGE_LEX) {\n        zslFreeLexRange(&lexrange);\n    }\n}\n\nvoid zcardCommand(client *c) {\n    robj *key = c->argv[1];\n    robj_roptr zobj;\n\n    if ((zobj = lookupKeyReadOrReply(c,key,shared.czero)) == nullptr ||\n        checkType(c,zobj,OBJ_ZSET)) return;\n\n    addReplyLongLong(c,zsetLength(zobj));\n}\n\nvoid zscoreCommand(client *c) {\n    robj *key = c->argv[1];\n    robj_roptr zobj;\n    double score;\n\n    if ((zobj = lookupKeyReadOrReply(c,key,shared.null[c->resp])) == nullptr ||\n        checkType(c,zobj,OBJ_ZSET)) return;\n\n    if (zsetScore(zobj,szFromObj(c->argv[2]),&score) == C_ERR) {\n        addReplyNull(c);\n    } else {\n        addReplyDouble(c,score);\n    }\n}\n\nvoid zmscoreCommand(client *c) {\n    robj *key = c->argv[1];\n    double score;\n    robj_roptr zobj = lookupKeyRead(c->db,key);\n    if (checkType(c,zobj,OBJ_ZSET)) return;\n\n    addReplyArrayLen(c,c->argc - 2);\n    for (int j = 2; j < c->argc; j++) {\n        /* Treat a missing set the same way as an empty set */\n        if (zobj == nullptr || zsetScore(zobj,(sds)ptrFromObj(c->argv[j]),&score) == C_ERR) {\n            addReplyNull(c);\n        } else {\n            addReplyDouble(c,score);\n        }\n    }\n}\n\nvoid zrankGenericCommand(client *c, int reverse) {\n    robj *key = c->argv[1];\n    robj *ele = c->argv[2];\n    robj_roptr zobj;\n    long rank;\n\n    if ((zobj = lookupKeyReadOrReply(c,key,shared.null[c->resp])) == nullptr ||\n        checkType(c,zobj,OBJ_ZSET)) return;\n\n    serverAssertWithInfo(c,ele,sdsEncodedObject(ele));\n    rank = zsetRank(zobj,szFromObj(ele),reverse);\n    if (rank >= 0) {\n        addReplyLongLong(c,rank);\n    } else {\n        addReplyNull(c);\n    }\n}\n\nvoid zrankCommand(client *c) {\n    zrankGenericCommand(c, 0);\n}\n\nvoid zrevrankCommand(client *c) {\n    zrankGenericCommand(c, 1);\n}\n\nvoid zscanCommand(client *c) {\n    robj_roptr o;\n    unsigned long cursor;\n\n    if (parseScanCursorOrReply(c,c->argv[2],&cursor) == C_ERR) return;\n    if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptyscan)) == nullptr ||\n        checkType(c,o,OBJ_ZSET)) return;\n    scanGenericCommand(c,o,cursor);\n}\n\n/* This command implements the generic zpop operation, used by:\n * ZPOPMIN, ZPOPMAX, BZPOPMIN and BZPOPMAX. This function is also used\n * inside blocked.c in the unblocking stage of BZPOPMIN and BZPOPMAX.\n *\n * If 'emitkey' is true also the key name is emitted, useful for the blocking\n * behavior of BZPOP[MIN|MAX], since we can block into multiple keys.\n *\n * The synchronous version instead does not need to emit the key, but may\n * use the 'count' argument to return multiple items if available. */\nvoid genericZpopCommand(client *c, robj **keyv, int keyc, int where, int emitkey, robj *countarg) {\n    int idx;\n    robj *key = NULL;\n    robj *zobj = NULL;\n    sds ele;\n    double score;\n    long count = 1;\n\n    /* If a count argument as passed, parse it or return an error. */\n    if (countarg) {\n        if (getLongFromObjectOrReply(c,countarg,&count,NULL) != C_OK)\n            return;\n        if (count <= 0) {\n            addReply(c,shared.emptyarray);\n            return;\n        }\n    }\n\n    /* Check type and break on the first error, otherwise identify candidate. */\n    idx = 0;\n    while (idx < keyc) {\n        key = keyv[idx++];\n        zobj = lookupKeyWrite(c->db,key);\n        if (!zobj) continue;\n        if (checkType(c,zobj,OBJ_ZSET)) return;\n        break;\n    }\n\n    /* No candidate for zpopping, return empty. */\n    if (!zobj) {\n        addReply(c,shared.emptyarray);\n        return;\n    }\n\n    void *arraylen_ptr = addReplyDeferredLen(c);\n    long result_count = 0;\n\n    /* We emit the key only for the blocking variant. */\n    if (emitkey) addReplyBulk(c,key);\n\n    /* Respond with a single (flat) array in RESP2 or if countarg is not\n     * provided (returning a single element). In RESP3, when countarg is\n     * provided, use nested array.  */\n    int use_nested_array = c->resp > 2 && countarg != NULL;\n\n    /* Remove the element. */\n    do {\n        if (zobj->encoding == OBJ_ENCODING_ZIPLIST) {\n            unsigned char *zl = (unsigned char*)zobj->m_ptr;\n            unsigned char *eptr, *sptr;\n            unsigned char *vstr;\n            unsigned int vlen;\n            long long vlong;\n\n            /* Get the first or last element in the sorted set. */\n            eptr = ziplistIndex(zl,where == ZSET_MAX ? -2 : 0);\n            serverAssertWithInfo(c,zobj,eptr != NULL);\n            serverAssertWithInfo(c,zobj,ziplistGet(eptr,&vstr,&vlen,&vlong));\n            if (vstr == NULL)\n                ele = sdsfromlonglong(vlong);\n            else\n                ele = sdsnewlen(vstr,vlen);\n\n            /* Get the score. */\n            sptr = ziplistNext(zl,eptr);\n            serverAssertWithInfo(c,zobj,sptr != NULL);\n            score = zzlGetScore(sptr);\n        } else if (zobj->encoding == OBJ_ENCODING_SKIPLIST) {\n            zset *zs = (zset*)zobj->m_ptr;\n            zskiplist *zsl = zs->zsl;\n            zskiplistNode *zln;\n\n            /* Get the first or last element in the sorted set. */\n            zln = (where == ZSET_MAX ? zsl->tail :\n                                       zsl->header->level(0)->forward);\n\n            /* There must be an element in the sorted set. */\n            serverAssertWithInfo(c,zobj,zln != NULL);\n            ele = sdsdup(zln->ele);\n            score = zln->score;\n        } else {\n            serverPanic(\"Unknown sorted set encoding\");\n        }\n\n        serverAssertWithInfo(c,zobj,zsetDel(zobj,ele));\n        g_pserver->dirty++;\n\n        if (result_count == 0) { /* Do this only for the first iteration. */\n            const char *events[2] = {\"zpopmin\",\"zpopmax\"};\n            notifyKeyspaceEvent(NOTIFY_ZSET,events[where],key,c->db->id);\n            signalModifiedKey(c,c->db,key);\n        }\n\n        if (use_nested_array) {\n            addReplyArrayLen(c,2);\n        }\n        addReplyBulkCBuffer(c,ele,sdslen(ele));\n        addReplyDouble(c,score);\n        sdsfree(ele);\n        ++result_count;\n\n        /* Remove the key, if indeed needed. */\n        if (zsetLength(zobj) == 0) {\n            dbDelete(c->db,key);\n            notifyKeyspaceEvent(NOTIFY_GENERIC,\"del\",key,c->db->id);\n            break;\n        }\n    } while(--count);\n\n    if (!use_nested_array) {\n        result_count *= 2;\n    }\n    setDeferredArrayLen(c,arraylen_ptr,result_count + (emitkey != 0));\n}\n\n/* ZPOPMIN key [<count>] */\nvoid zpopminCommand(client *c) {\n    if (c->argc > 3) {\n        addReplyErrorObject(c,shared.syntaxerr);\n        return;\n    }\n    genericZpopCommand(c,&c->argv[1],1,ZSET_MIN,0,\n        c->argc == 3 ? c->argv[2] : NULL);\n}\n\n/* ZMAXPOP key [<count>] */\nvoid zpopmaxCommand(client *c) {\n    if (c->argc > 3) {\n        addReplyErrorObject(c,shared.syntaxerr);\n        return;\n    }\n    genericZpopCommand(c,&c->argv[1],1,ZSET_MAX,0,\n        c->argc == 3 ? c->argv[2] : NULL);\n}\n\n/* BZPOPMIN / BZPOPMAX actual implementation. */\nvoid blockingGenericZpopCommand(client *c, int where) {\n    robj *o;\n    mstime_t timeout;\n    int j;\n\n    if (getTimeoutFromObjectOrReply(c,c->argv[c->argc-1],&timeout,UNIT_SECONDS)\n        != C_OK) return;\n\n    for (j = 1; j < c->argc-1; j++) {\n        o = lookupKeyWrite(c->db,c->argv[j]);\n        if (checkType(c,o,OBJ_ZSET)) return;\n        if (o != NULL) {\n            if (zsetLength(o) != 0) {\n                /* Non empty zset, this is like a normal ZPOP[MIN|MAX]. */\n                genericZpopCommand(c,&c->argv[j],1,where,1,NULL);\n                /* Replicate it as an ZPOP[MIN|MAX] instead of BZPOP[MIN|MAX]. */\n                rewriteClientCommandVector(c,2,\n                    where == ZSET_MAX ? shared.zpopmax : shared.zpopmin,\n                    c->argv[j]);\n                return;\n            }\n        }\n    }\n\n    /* If we are not allowed to block the client and the zset is empty the only thing\n     * we can do is treating it as a timeout (even with timeout 0). */\n    if (c->flags & CLIENT_DENY_BLOCKING) {\n        addReplyNullArray(c);\n        return;\n    }\n\n    /* If the keys do not exist we must block */\n    blockForKeys(c,BLOCKED_ZSET,c->argv + 1,c->argc - 2,timeout,NULL,NULL,NULL);\n}\n\n// BZPOPMIN key [key ...] timeout\nvoid bzpopminCommand(client *c) {\n    blockingGenericZpopCommand(c,ZSET_MIN);\n}\n\n// BZPOPMAX key [key ...] timeout\nvoid bzpopmaxCommand(client *c) {\n    blockingGenericZpopCommand(c,ZSET_MAX);\n}\n\nstatic void zarndmemberReplyWithZiplist(client *c, unsigned int count, ziplistEntry *keys, ziplistEntry *vals) {\n    for (unsigned long i = 0; i < count; i++) {\n        if (vals && c->resp > 2)\n            addReplyArrayLen(c,2);\n        if (keys[i].sval)\n            addReplyBulkCBuffer(c, keys[i].sval, keys[i].slen);\n        else\n            addReplyBulkLongLong(c, keys[i].lval);\n        if (vals) {\n            if (vals[i].sval) {\n                addReplyDouble(c, zzlStrtod(vals[i].sval,vals[i].slen));\n            } else\n                addReplyDouble(c, vals[i].lval);\n        }\n    }\n}\n\n/* How many times bigger should be the zset compared to the requested size\n * for us to not use the \"remove elements\" strategy? Read later in the\n * implementation for more info. */\n#define ZRANDMEMBER_SUB_STRATEGY_MUL 3\n\n/* If client is trying to ask for a very large number of random elements,\n * queuing may consume an unlimited amount of memory, so we want to limit\n * the number of randoms per time. */\n#define ZRANDMEMBER_RANDOM_SAMPLE_LIMIT 1000\n\nvoid zrandmemberWithCountCommand(client *c, long l, int withscores) {\n    unsigned long count, size;\n    int uniq = 1;\n    robj_roptr zsetobj;\n\n    if ((zsetobj = lookupKeyReadOrReply(c, c->argv[1], shared.emptyarray))\n        == nullptr || checkType(c, zsetobj, OBJ_ZSET)) return;\n    size = zsetLength(zsetobj);\n\n    if(l >= 0) {\n        count = (unsigned long) l;\n    } else {\n        count = -l;\n        uniq = 0;\n    }\n\n    /* If count is zero, serve it ASAP to avoid special cases later. */\n    if (count == 0) {\n        addReply(c,shared.emptyarray);\n        return;\n    }\n\n    /* CASE 1: The count was negative, so the extraction method is just:\n     * \"return N random elements\" sampling the whole set every time.\n     * This case is trivial and can be served without auxiliary data\n     * structures. This case is the only one that also needs to return the\n     * elements in random order. */\n    if (!uniq || count == 1) {\n        if (withscores && c->resp == 2)\n            addReplyArrayLen(c, count*2);\n        else\n            addReplyArrayLen(c, count);\n        if (zsetobj->encoding == OBJ_ENCODING_SKIPLIST) {\n            zset *zs = (zset*)ptrFromObj(zsetobj);\n            while (count--) {\n                dictEntry *de = dictGetFairRandomKey(zs->dict);\n                sds key = (sds)dictGetKey(de);\n                if (withscores && c->resp > 2)\n                    addReplyArrayLen(c,2);\n                addReplyBulkCBuffer(c, key, sdslen(key));\n                if (withscores)\n                    addReplyDouble(c, *(double*)dictGetVal(de));\n                if (c->flags & CLIENT_CLOSE_ASAP)\n                    break;\n            }\n        } else if (zsetobj->encoding == OBJ_ENCODING_ZIPLIST) {\n            ziplistEntry *keys, *vals = NULL;\n            unsigned long limit, sample_count;\n            limit = count > ZRANDMEMBER_RANDOM_SAMPLE_LIMIT ? ZRANDMEMBER_RANDOM_SAMPLE_LIMIT : count;\n            keys = (ziplistEntry*)zmalloc(sizeof(ziplistEntry)*limit);\n            if (withscores)\n                vals = (ziplistEntry*)zmalloc(sizeof(ziplistEntry)*limit);\n            while (count) {\n                sample_count = count > limit ? limit : count;\n                count -= sample_count;\n                ziplistRandomPairs((unsigned char*)ptrFromObj(zsetobj), sample_count, keys, vals);\n                zarndmemberReplyWithZiplist(c, sample_count, keys, vals);\n                if (c->flags & CLIENT_CLOSE_ASAP)\n                    break;\n            }\n            zfree(keys);\n            zfree(vals);\n        }\n        return;\n    }\n\n    zsetopsrc src;\n    zsetopval zval;\n    src.subject = zsetobj.unsafe_robjcast();\n    src.type = zsetobj->type;\n    src.encoding = zsetobj->encoding;\n    zuiInitIterator(&src);\n    memset(&zval, 0, sizeof(zval));\n\n    /* Initiate reply count, RESP3 responds with nested array, RESP2 with flat one. */\n    long reply_size = count < size ? count : size;\n    if (withscores && c->resp == 2)\n        addReplyArrayLen(c, reply_size*2);\n    else\n        addReplyArrayLen(c, reply_size);\n\n    /* CASE 2:\n    * The number of requested elements is greater than the number of\n    * elements inside the zset: simply return the whole zset. */\n    if (count >= size) {\n        while (zuiNext(&src, &zval)) {\n            if (withscores && c->resp > 2)\n                addReplyArrayLen(c,2);\n            addReplyBulkSds(c, zuiNewSdsFromValue(&zval));\n            if (withscores)\n                addReplyDouble(c, zval.score);\n        }\n        return;\n    }\n\n    /* CASE 3:\n     * The number of elements inside the zset is not greater than\n     * ZRANDMEMBER_SUB_STRATEGY_MUL times the number of requested elements.\n     * In this case we create a dict from scratch with all the elements, and\n     * subtract random elements to reach the requested number of elements.\n     *\n     * This is done because if the number of requested elements is just\n     * a bit less than the number of elements in the set, the natural approach\n     * used into CASE 4 is highly inefficient. */\n    if (count*ZRANDMEMBER_SUB_STRATEGY_MUL > size) {\n        dict *d = dictCreate(&sdsReplyDictType, NULL);\n        dictExpand(d, size);\n        /* Add all the elements into the temporary dictionary. */\n        while (zuiNext(&src, &zval)) {\n            sds key = zuiNewSdsFromValue(&zval);\n            dictEntry *de = dictAddRaw(d, key, NULL);\n            serverAssert(de);\n            if (withscores)\n                dictSetDoubleVal(de, zval.score);\n        }\n        serverAssert(dictSize(d) == size);\n\n        /* Remove random elements to reach the right count. */\n        while (size > count) {\n            dictEntry *de;\n            de = dictGetRandomKey(d);\n            dictUnlink(d,dictGetKey(de));\n            sdsfree((sds)dictGetKey(de));\n            dictFreeUnlinkedEntry(d,de);\n            size--;\n        }\n\n        /* Reply with what's in the dict and release memory */\n        dictIterator *di;\n        dictEntry *de;\n        di = dictGetIterator(d);\n        while ((de = dictNext(di)) != NULL) {\n            if (withscores && c->resp > 2)\n                addReplyArrayLen(c,2);\n            addReplyBulkSds(c, (sds)dictGetKey(de));\n            if (withscores)\n                addReplyDouble(c, dictGetDoubleVal(de));\n        }\n\n        dictReleaseIterator(di);\n        dictRelease(d);\n    }\n\n    /* CASE 4: We have a big zset compared to the requested number of elements.\n     * In this case we can simply get random elements from the zset and add\n     * to the temporary set, trying to eventually get enough unique elements\n     * to reach the specified count. */\n    else {\n        if (zsetobj->encoding == OBJ_ENCODING_ZIPLIST) {\n            /* it is inefficient to repeatedly pick one random element from a\n             * ziplist. so we use this instead: */\n            ziplistEntry *keys, *vals = NULL;\n            keys = (ziplistEntry*)zmalloc(sizeof(ziplistEntry)*count);\n            if (withscores)\n                vals = (ziplistEntry*)zmalloc(sizeof(ziplistEntry)*count);\n            serverAssert(ziplistRandomPairsUnique((unsigned char*)ptrFromObj(zsetobj), count, keys, vals) == count);\n            zarndmemberReplyWithZiplist(c, count, keys, vals);\n            zfree(keys);\n            zfree(vals);\n            return;\n        }\n\n        /* Hashtable encoding (generic implementation) */\n        unsigned long added = 0;\n        dict *d = dictCreate(&hashDictType, NULL);\n        dictExpand(d, count);\n\n        while (added < count) {\n            ziplistEntry key;\n            double score;\n            zsetTypeRandomElement(zsetobj, size, &key, withscores ? &score: NULL);\n\n            /* Try to add the object to the dictionary. If it already exists\n            * free it, otherwise increment the number of objects we have\n            * in the result dictionary. */\n            sds skey = zsetSdsFromZiplistEntry(&key);\n            if (dictAdd(d,skey,NULL) != DICT_OK) {\n                sdsfree(skey);\n                continue;\n            }\n            added++;\n\n            if (withscores && c->resp > 2)\n                addReplyArrayLen(c,2);\n            zsetReplyFromZiplistEntry(c, &key);\n            if (withscores)\n                addReplyDouble(c, score);\n        }\n\n        /* Release memory */\n        dictRelease(d);\n    }\n}\n\n/* ZRANDMEMBER key [<count> [WITHSCORES]] */\nvoid zrandmemberCommand(client *c) {\n    long l;\n    int withscores = 0;\n    robj_roptr zset;\n    ziplistEntry ele;\n\n    if (c->argc >= 3) {\n        if (getRangeLongFromObjectOrReply(c,c->argv[2],-LONG_MAX,LONG_MAX,&l,NULL) != C_OK) return;\n        if (c->argc > 4 || (c->argc == 4 && strcasecmp(szFromObj(c->argv[3]),\"withscores\"))) {\n            addReplyErrorObject(c,shared.syntaxerr);\n            return;\n        } else if (c->argc == 4) {\n            withscores = 1;\n            if (l < -g_pserver->rand_total_threshold || l > g_pserver->rand_total_threshold) {\n                addReplyError(c,\"value is out of range\");\n                return;\n            }\n        }\n        zrandmemberWithCountCommand(c, l, withscores);\n        return;\n    }\n\n    /* Handle variant without <count> argument. Reply with simple bulk string */\n    if ((zset = lookupKeyReadOrReply(c,c->argv[1],shared.null[c->resp])) == nullptr ||\n        checkType(c,zset,OBJ_ZSET)) {\n        return;\n    }\n\n    zsetTypeRandomElement(zset, zsetLength(zset), &ele,NULL);\n    zsetReplyFromZiplistEntry(c,&ele);\n}\n"
  },
  {
    "path": "src/testhelp.h",
    "content": "/* This is a really minimal testing framework for C.\n *\n * Example:\n *\n * test_cond(\"Check if 1 == 1\", 1==1)\n * test_cond(\"Check if 5 > 10\", 5 > 10)\n * test_report()\n *\n * ----------------------------------------------------------------------------\n *\n * Copyright (c) 2010-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __TESTHELP_H\n#define __TESTHELP_H\n\nint __failed_tests = 0;\nint __test_num = 0;\n#define test_cond(descr,_c) do { \\\n    __test_num++; printf(\"%d - %s: \", __test_num, descr); \\\n    if(_c) printf(\"PASSED\\n\"); else {printf(\"FAILED\\n\"); __failed_tests++;} \\\n} while(0)\n#define test_report() do { \\\n    printf(\"%d tests, %d passed, %d failed\\n\", __test_num, \\\n                    __test_num-__failed_tests, __failed_tests); \\\n    if (__failed_tests) { \\\n        printf(\"=== WARNING === We have failed tests here...\\n\"); \\\n        exit(1); \\\n    } \\\n} while(0)\n\n#endif\n"
  },
  {
    "path": "src/timeout.cpp",
    "content": "/* Copyright (c) 2009-2020, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n#include \"cluster.h\"\n#include <mutex>\n\n/* ========================== Clients timeouts ============================= */\n\n/* Check if this blocked client timedout (does nothing if the client is\n * not blocked right now). If so send a reply, unblock it, and return 1.\n * Otherwise 0 is returned and no operation is performed. */\nint checkBlockedClientTimeout(client *c, mstime_t now) {\n    if (c->flags & CLIENT_BLOCKED &&\n        c->bpop.timeout != 0\n        && c->bpop.timeout < now)\n    {\n        /* Handle blocking operation specific timeout. */\n        replyToBlockedClientTimedOut(c);\n        unblockClient(c);\n        return 1;\n    } else {\n        return 0;\n    }\n}\n\n/* Check for timeouts. Returns non-zero if the client was terminated.\n * The function gets the current time in milliseconds as argument since\n * it gets called multiple times in a loop, so calling gettimeofday() for\n * each iteration would be costly without any actual gain. */\nint clientsCronHandleTimeout(client *c, mstime_t now_ms) {\n    time_t now = now_ms/1000;\n\n    if (cserver.maxidletime &&\n        /* This handles the idle clients connection timeout if set. */\n        !(c->flags & CLIENT_SLAVE) &&   /* No timeout for slaves and monitors */\n        !(c->flags & CLIENT_MASTER) &&  /* No timeout for masters */\n        !(c->flags & CLIENT_BLOCKED) && /* No timeout for BLPOP */\n        !(c->flags & CLIENT_PUBSUB) &&  /* No timeout for Pub/Sub clients */\n        (now - c->lastinteraction > cserver.maxidletime))\n    {\n        serverLog(LL_VERBOSE,\"Closing idle client\");\n        freeClient(c);\n        return 1;\n    } else if (c->flags & CLIENT_BLOCKED) {\n        /* Cluster: handle unblock & redirect of clients blocked\n         * into keys no longer served by this server. */\n        if (g_pserver->cluster_enabled) {\n            if (clusterRedirectBlockedClientIfNeeded(c))\n                unblockClient(c);\n        }\n    }\n    return 0;\n}\n\n/* For blocked clients timeouts we populate a radix tree of 128 bit keys\n * composed as such:\n *\n *  [8 byte big endian expire time]+[8 byte client ID]\n *\n * We don't do any cleanup in the Radix tree: when we run the clients that\n * reached the timeout already, if they are no longer existing or no longer\n * blocked with such timeout, we just go forward.\n *\n * Every time a client blocks with a timeout, we add the client in\n * the tree. In beforeSleep() we call clientsHandleTimeout() to run\n * the tree and unblock the clients. */\n\n#define CLIENT_ST_KEYLEN 16    /* 8 bytes mstime + 8 bytes client ID. */\n\n/* Given client ID and timeout, write the resulting radix tree key in buf. */\nvoid encodeTimeoutKey(unsigned char *buf, uint64_t timeout, client *c) {\n    timeout = htonu64(timeout);\n    memcpy(buf,&timeout,sizeof(timeout));\n    memcpy(buf+8,&c,sizeof(c));\n    if (sizeof(c) == 4) memset(buf+12,0,4); /* Zero padding for 32bit target. */\n}\n\n/* Given a key encoded with encodeTimeoutKey(), resolve the fields and write\n * the timeout into *toptr and the client pointer into *cptr. */\nvoid decodeTimeoutKey(unsigned char *buf, uint64_t *toptr, client **cptr) {\n    memcpy(toptr,buf,sizeof(*toptr));\n    *toptr = ntohu64(*toptr);\n    memcpy(cptr,buf+8,sizeof(*cptr));\n}\n\n/* Add the specified client id / timeout as a key in the radix tree we use\n * to handle blocked clients timeouts. The client is not added to the list\n * if its timeout is zero (block forever). */\nvoid addClientToTimeoutTable(client *c) {\n    if (c->bpop.timeout == 0) return;\n    uint64_t timeout = c->bpop.timeout;\n    unsigned char buf[CLIENT_ST_KEYLEN];\n    encodeTimeoutKey(buf,timeout,c);\n    if (raxTryInsert(g_pserver->clients_timeout_table,buf,sizeof(buf),NULL,NULL))\n        c->flags |= CLIENT_IN_TO_TABLE;\n}\n\n/* Remove the client from the table when it is unblocked for reasons\n * different than timing out. */\nvoid removeClientFromTimeoutTable(client *c) {\n    if (!(c->flags & CLIENT_IN_TO_TABLE)) return;\n    c->flags &= ~CLIENT_IN_TO_TABLE;\n    uint64_t timeout = c->bpop.timeout;\n    unsigned char buf[CLIENT_ST_KEYLEN];\n    encodeTimeoutKey(buf,timeout,c);\n    raxRemove(g_pserver->clients_timeout_table,buf,sizeof(buf),NULL);\n}\n\n/* This function is called in beforeSleep() in order to unblock clients\n * that are waiting in blocking operations with a timeout set. */\nvoid handleBlockedClientsTimeout(void) {\n    if (raxSize(g_pserver->clients_timeout_table) == 0) return;\n    uint64_t now = mstime();\n    raxIterator ri;\n    raxStart(&ri,g_pserver->clients_timeout_table);\n    raxSeek(&ri,\"^\",NULL,0);\n\n    while(raxNext(&ri)) {\n        uint64_t timeout;\n        client *c;\n        decodeTimeoutKey(ri.key,&timeout,&c);\n        if (timeout >= now) break; /* All the timeouts are in the future. */\n        std::unique_lock<fastlock> lock(c->lock);\n        c->flags &= ~CLIENT_IN_TO_TABLE;\n        checkBlockedClientTimeout(c,now);\n        raxRemove(g_pserver->clients_timeout_table,ri.key,ri.key_len,NULL);\n        raxSeek(&ri,\"^\",NULL,0);\n    }\n    raxStop(&ri);\n}\n\n/* Get a timeout value from an object and store it into 'timeout'.\n * The final timeout is always stored as milliseconds as a time where the\n * timeout will expire, however the parsing is performed according to\n * the 'unit' that can be seconds or milliseconds.\n *\n * Note that if the timeout is zero (usually from the point of view of\n * commands API this means no timeout) the value stored into 'timeout'\n * is zero. */\nint getTimeoutFromObjectOrReply(client *c, robj *object, mstime_t *timeout, int unit) {\n    long long tval;\n    long double ftval;\n\n    if (unit == UNIT_SECONDS) {\n        if (getLongDoubleFromObjectOrReply(c,object,&ftval,\n            \"timeout is not a float or out of range\") != C_OK)\n            return C_ERR;\n        tval = (long long) (ftval * 1000.0);\n    } else {\n        if (getLongLongFromObjectOrReply(c,object,&tval,\n            \"timeout is not an integer or out of range\") != C_OK)\n            return C_ERR;\n    }\n\n    if (tval < 0) {\n        addReplyError(c,\"timeout is negative\");\n        return C_ERR;\n    }\n\n    if (tval > 0) {\n        tval += mstime();\n    }\n    *timeout = tval;\n\n    return C_OK;\n}\n"
  },
  {
    "path": "src/tls.cpp",
    "content": "/*\n * Copyright (c) 2019, Redis Labs\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n\n#include \"server.h\"\n#include \"connhelpers.h\"\n#include \"adlist.h\"\n#include \"aelocker.h\"\n#include <mutex>\n\n#ifdef USE_OPENSSL\n#include <openssl/conf.h>\n#include <openssl/ssl.h>\n#include <openssl/err.h>\n#include <openssl/rand.h>\n#include <openssl/pem.h>\n#include <openssl/x509.h>\n#include <openssl/x509v3.h>\n#include <cstring>\n\n#include <sys/stat.h>\n#include <arpa/inet.h>\n#if OPENSSL_VERSION_NUMBER >= 0x30000000L\n#include <openssl/decoder.h>\n#endif\n\n#define REDIS_TLS_PROTO_TLSv1       (1<<0)\n#define REDIS_TLS_PROTO_TLSv1_1     (1<<1)\n#define REDIS_TLS_PROTO_TLSv1_2     (1<<2)\n#define REDIS_TLS_PROTO_TLSv1_3     (1<<3)\n\n/* Use safe defaults */\n#ifdef TLS1_3_VERSION\n#define REDIS_TLS_PROTO_DEFAULT     (REDIS_TLS_PROTO_TLSv1_2|REDIS_TLS_PROTO_TLSv1_3)\n#else\n#define REDIS_TLS_PROTO_DEFAULT     (REDIS_TLS_PROTO_TLSv1_2)\n#endif\n\nextern ConnectionType CT_Socket;\n\nSSL_CTX *redis_tls_ctx = NULL;\nSSL_CTX *redis_tls_client_ctx = NULL;\nfastlock g_ctxtlock(\"SSL CTX\");\n\nstatic int parseProtocolsConfig(const char *str) {\n    int i, count = 0;\n    int protocols = 0;\n\n    if (!str) return REDIS_TLS_PROTO_DEFAULT;\n    sds *tokens = sdssplitlen(str, strlen(str), \" \", 1, &count);\n\n    if (!tokens) { \n        serverLog(LL_WARNING, \"Invalid tls-protocols configuration string\");\n        return -1;\n    }\n    for (i = 0; i < count; i++) {\n        if (!strcasecmp(tokens[i], \"tlsv1\")) protocols |= REDIS_TLS_PROTO_TLSv1;\n        else if (!strcasecmp(tokens[i], \"tlsv1.1\")) protocols |= REDIS_TLS_PROTO_TLSv1_1;\n        else if (!strcasecmp(tokens[i], \"tlsv1.2\")) protocols |= REDIS_TLS_PROTO_TLSv1_2;\n        else if (!strcasecmp(tokens[i], \"tlsv1.3\")) {\n#ifdef TLS1_3_VERSION\n            protocols |= REDIS_TLS_PROTO_TLSv1_3;\n#else\n            serverLog(LL_WARNING, \"TLSv1.3 is specified in tls-protocols but not supported by OpenSSL.\");\n            protocols = -1;\n            break;\n#endif\n        } else {\n            serverLog(LL_WARNING, \"Invalid tls-protocols specified. \"\n                    \"Use a combination of 'TLSv1', 'TLSv1.1', 'TLSv1.2' and 'TLSv1.3'.\");\n            protocols = -1;\n            break;\n        }\n    }\n    sdsfreesplitres(tokens, count);\n\n    return protocols;\n}\n\n/* list of connections with pending data already read from the socket, but not\n * served to the reader yet. */\nstatic thread_local list *pending_list = NULL;\n\n/**\n * OpenSSL global initialization and locking handling callbacks.\n * Note that this is only required for OpenSSL < 1.1.0.\n */\n\n#if OPENSSL_VERSION_NUMBER < 0x10100000L\n#define USE_CRYPTO_LOCKS\n#endif\n\n#ifdef USE_CRYPTO_LOCKS\n\nstatic pthread_mutex_t *openssl_locks;\n\nstatic void sslLockingCallback(int mode, int lock_id, const char *f, int line) {\n    pthread_mutex_t *mt = openssl_locks + lock_id;\n\n    if (mode & CRYPTO_LOCK) {\n        pthread_mutex_lock(mt);\n    } else {\n        pthread_mutex_unlock(mt);\n    }\n\n    (void)f;\n    (void)line;\n}\n\nstatic void initCryptoLocks(void) {\n    unsigned i, nlocks;\n    if (CRYPTO_get_locking_callback() != NULL) {\n        /* Someone already set the callback before us. Don't destroy it! */\n        return;\n    }\n    nlocks = CRYPTO_num_locks();\n    openssl_locks = (pthread_mutex_t*)zmalloc(sizeof(*openssl_locks) * nlocks);\n    for (i = 0; i < nlocks; i++) {\n        pthread_mutex_init(openssl_locks + i, NULL);\n    }\n    CRYPTO_set_locking_callback(sslLockingCallback);\n}\n#endif /* USE_CRYPTO_LOCKS */\n\nvoid tlsInit(void) {\n    /* Enable configuring OpenSSL using the standard openssl.cnf\n     * OPENSSL_config()/OPENSSL_init_crypto() should be the first \n     * call to the OpenSSL* library.\n     *  - OPENSSL_config() should be used for OpenSSL versions < 1.1.0\n     *  - OPENSSL_init_crypto() should be used for OpenSSL versions >= 1.1.0\n     */\n    #if OPENSSL_VERSION_NUMBER < 0x10100000L\n    OPENSSL_config(NULL);\n    SSL_load_error_strings();\n    SSL_library_init();\n    #elif OPENSSL_VERSION_NUMBER < 0x10101000L\n    OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);\n    #else\n    OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG|OPENSSL_INIT_ATFORK, NULL);\n    #endif\n\n#ifdef USE_CRYPTO_LOCKS\n    initCryptoLocks();\n#endif\n\n    if (!RAND_poll()) {\n        serverLog(LL_WARNING, \"OpenSSL: Failed to seed random number generator.\");\n    }\n\n    tlsInitThread();\n}\n\nvoid tlsInitThread(void)\n{\n    serverAssert(pending_list == nullptr);\n    pending_list = listCreate();\n}\n\nvoid tlsCleanupThread(void)\n{\n    if (pending_list)\n        listRelease(pending_list);\n}\n\nvoid tlsCleanup(void) {\n    if (redis_tls_ctx) {\n        SSL_CTX_free(redis_tls_ctx);\n        redis_tls_ctx = NULL;\n    }\n    if (redis_tls_client_ctx) {\n        SSL_CTX_free(redis_tls_client_ctx);\n        redis_tls_client_ctx = NULL;\n    }\n\n    #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)\n    // unavailable on LibreSSL\n    OPENSSL_cleanup();\n    #endif\n}\n\n/* Callback for passing a keyfile password stored as an sds to OpenSSL */\nstatic int tlsPasswordCallback(char *buf, int size, int rwflag, void *u) {\n    UNUSED(rwflag);\n\n    const char *pass = (const char*)u;\n    size_t pass_len;\n\n    if (!pass) return -1;\n    pass_len = strlen(pass);\n    if (pass_len > (size_t) size) return -1;\n    memcpy(buf, pass, pass_len);\n\n    return (int) pass_len;\n}\n\n/* Given a path to a file, return the last time it was accessed (in seconds) */\ntime_t getLastModifiedTime(const char* path){\n    struct stat path_stat;\n    stat(path, &path_stat);\n\n#ifdef __APPLE__\n    return path_stat.st_mtimespec.tv_sec;\n#else\n    return path_stat.st_mtime;\n#endif\n}\n\n/* Create a *base* SSL_CTX using the SSL configuration provided. The base context\n * includes everything that's common for both client-side and server-side connections.\n */\nstatic SSL_CTX *createSSLContext(redisTLSContextConfig *ctx_config, int protocols, int client) {\n    const char *cert_file = client ? ctx_config->client_cert_file : ctx_config->cert_file;\n    const char *key_file = client ? ctx_config->client_key_file : ctx_config->key_file;\n    const char *key_file_pass = client ? ctx_config->client_key_file_pass : ctx_config->key_file_pass;\n    char errbuf[256];\n    SSL_CTX *ctx = NULL;\n\n    ctx = SSL_CTX_new(SSLv23_method());\n\n    SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3);\n\n#ifdef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS\n    SSL_CTX_set_options(ctx, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);\n#endif\n\n    if (!(protocols & REDIS_TLS_PROTO_TLSv1))\n        SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1);\n    if (!(protocols & REDIS_TLS_PROTO_TLSv1_1))\n        SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_1);\n#ifdef SSL_OP_NO_TLSv1_2\n    if (!(protocols & REDIS_TLS_PROTO_TLSv1_2))\n        SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_2);\n#endif\n#ifdef SSL_OP_NO_TLSv1_3\n    if (!(protocols & REDIS_TLS_PROTO_TLSv1_3))\n        SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_3);\n#endif\n\n#ifdef SSL_OP_NO_COMPRESSION\n    SSL_CTX_set_options(ctx, SSL_OP_NO_COMPRESSION);\n#endif\n\n    SSL_CTX_set_mode(ctx, SSL_MODE_ENABLE_PARTIAL_WRITE|SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);\n    SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL);\n\n    SSL_CTX_set_default_passwd_cb(ctx, tlsPasswordCallback);\n    SSL_CTX_set_default_passwd_cb_userdata(ctx, (void *) key_file_pass);\n\n    if (SSL_CTX_use_certificate_chain_file(ctx, cert_file) <= 0) {\n        ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf));\n        serverLog(LL_WARNING, \"Failed to load certificate: %s: %s\", cert_file, errbuf);\n        goto error;\n    }\n\n    if (SSL_CTX_use_PrivateKey_file(ctx, key_file, SSL_FILETYPE_PEM) <= 0) {\n        ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf));\n        serverLog(LL_WARNING, \"Failed to load private key: %s: %s\", key_file, errbuf);\n        goto error;\n    }\n\n    if ((ctx_config->ca_cert_file || ctx_config->ca_cert_dir) &&\n        SSL_CTX_load_verify_locations(ctx, ctx_config->ca_cert_file, ctx_config->ca_cert_dir) <= 0) {\n        ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf));\n        serverLog(LL_WARNING, \"Failed to configure CA certificate(s) file/directory: %s\", errbuf);\n        goto error;\n    }\n\n    if (ctx_config->ciphers && !SSL_CTX_set_cipher_list(ctx, ctx_config->ciphers)) {\n        serverLog(LL_WARNING, \"Failed to configure ciphers: %s\", ctx_config->ciphers);\n        goto error;\n    }\n\n#ifdef TLS1_3_VERSION\n    if (ctx_config->ciphersuites && !SSL_CTX_set_ciphersuites(ctx, ctx_config->ciphersuites)) {\n        serverLog(LL_WARNING, \"Failed to configure ciphersuites: %s\", ctx_config->ciphersuites);\n        goto error;\n    }\n#endif\n\n    return ctx;\n\nerror:\n    if (ctx) SSL_CTX_free(ctx);\n    return NULL;\n}\n\n/* Attempt to configure/reconfigure TLS. This operation is atomic and will\n * leave the SSL_CTX unchanged if fails.\n */\nint tlsConfigure(redisTLSContextConfig *ctx_config) {\n    char errbuf[256];\n    SSL_CTX *ctx = NULL;\n    SSL_CTX *client_ctx = NULL;\n    int protocols;\n\n    if (!ctx_config->cert_file) {\n        serverLog(LL_WARNING, \"No tls-cert-file configured!\");\n        goto error;\n    }\n\n    if (!ctx_config->key_file) {\n        serverLog(LL_WARNING, \"No tls-key-file configured!\");\n        goto error;\n    }\n\n    if (((g_pserver->tls_auth_clients != TLS_CLIENT_AUTH_NO) || g_pserver->tls_cluster || g_pserver->tls_replication) &&\n            !ctx_config->ca_cert_file && !ctx_config->ca_cert_dir) {\n        serverLog(LL_WARNING, \"Either tls-ca-cert-file or tls-ca-cert-dir must be specified when tls-cluster, tls-replication or tls-auth-clients are enabled!\");\n        goto error;\n    }\n\n    /* Update the last modified times for the TLS elements */\n    ctx_config->key_file_last_modified = getLastModifiedTime(ctx_config->key_file);\n    ctx_config->cert_file_last_modified = getLastModifiedTime(ctx_config->cert_file);\n    ctx_config->client_cert_file_last_modified = getLastModifiedTime(ctx_config->client_cert_file);\n    ctx_config->client_key_file_last_modified = getLastModifiedTime(ctx_config->client_key_file);\n    ctx_config->ca_cert_dir_last_modified = getLastModifiedTime(ctx_config->ca_cert_dir);\n    ctx_config->ca_cert_file_last_modified = getLastModifiedTime(ctx_config->ca_cert_file);\n\n    protocols = parseProtocolsConfig(ctx_config->protocols);\n    if (protocols == -1) goto error;\n\n    /* Create server side/generla context */\n    ctx = createSSLContext(ctx_config, protocols, 0);\n    if (!ctx) goto error;\n\n    if (ctx_config->session_caching) {\n        SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER);\n        SSL_CTX_sess_set_cache_size(ctx, ctx_config->session_cache_size);\n        SSL_CTX_set_timeout(ctx, ctx_config->session_cache_timeout);\n        SSL_CTX_set_session_id_context(ctx, (const unsigned char *) \"KeyDB\", 5);\n    } else {\n        SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);\n    }\n\n#ifdef SSL_OP_NO_CLIENT_RENEGOTIATION\n    SSL_CTX_set_options(ctx, SSL_OP_NO_CLIENT_RENEGOTIATION);\n#endif\n\n    if (ctx_config->prefer_server_ciphers)\n        SSL_CTX_set_options(ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);\n\n#if ((OPENSSL_VERSION_NUMBER < 0x30000000L) && defined(SSL_CTX_set_ecdh_auto))\n    SSL_CTX_set_ecdh_auto(ctx, 1);\n#endif\n    SSL_CTX_set_options(ctx, SSL_OP_SINGLE_DH_USE);\n\n    if (ctx_config->dh_params_file) {\n        FILE *dhfile = fopen(ctx_config->dh_params_file, \"r\");\n        if (!dhfile) {\n            serverLog(LL_WARNING, \"Failed to load %s: %s\", ctx_config->dh_params_file, strerror(errno));\n            goto error;\n        }\n\n#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)\n        EVP_PKEY *pkey = NULL;\n        OSSL_DECODER_CTX *dctx = OSSL_DECODER_CTX_new_for_pkey(\n                &pkey, \"PEM\", NULL, \"DH\", OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, NULL, NULL);\n        if (!dctx) {\n            serverLog(LL_WARNING, \"No decoder for DH params.\");\n            fclose(dhfile);\n            goto error;\n        }\n        if (!OSSL_DECODER_from_fp(dctx, dhfile)) {\n            serverLog(LL_WARNING, \"%s: failed to read DH params.\", ctx_config->dh_params_file);\n            OSSL_DECODER_CTX_free(dctx);\n            fclose(dhfile);\n            goto error;\n        }\n\n        OSSL_DECODER_CTX_free(dctx);\n        fclose(dhfile);\n\n        if (SSL_CTX_set0_tmp_dh_pkey(ctx, pkey) <= 0) {\n            ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf));\n            serverLog(LL_WARNING, \"Failed to load DH params file: %s: %s\", ctx_config->dh_params_file, errbuf);\n            EVP_PKEY_free(pkey);\n            goto error;\n        }\n        /* Not freeing pkey, it is owned by OpenSSL now */\n#else\n        DH *dh = PEM_read_DHparams(dhfile, NULL, NULL, NULL);\n        fclose(dhfile);\n        if (!dh) {\n            serverLog(LL_WARNING, \"%s: failed to read DH params.\", ctx_config->dh_params_file);\n            goto error;\n        }\n\n        if (SSL_CTX_set_tmp_dh(ctx, dh) <= 0) {\n            ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf));\n            serverLog(LL_WARNING, \"Failed to load DH params file: %s: %s\", ctx_config->dh_params_file, errbuf);\n            DH_free(dh);\n            goto error;\n        }\n\n        DH_free(dh);\n#endif\n    } else {\n#if (OPENSSL_VERSION_NUMBER >= 0x30000000L)\n        SSL_CTX_set_dh_auto(ctx, 1);\n#endif\n    }\n\n    /* If a client-side certificate is configured, create an explicit client context */\n    if (ctx_config->client_cert_file && ctx_config->client_key_file) {\n        client_ctx = createSSLContext(ctx_config, protocols, 1);\n        if (!client_ctx) goto error;\n    }\n\n    {\n    std::unique_lock<fastlock> ul(g_ctxtlock);\n    SSL_CTX_free(redis_tls_ctx);\n    SSL_CTX_free(redis_tls_client_ctx);\n    redis_tls_ctx = ctx;\n    redis_tls_client_ctx = client_ctx;\n    }\n\n    return C_OK;\n\nerror:\n    if (ctx) SSL_CTX_free(ctx);\n    if (client_ctx) SSL_CTX_free(client_ctx);\n    return C_ERR;\n}\n\n/* Reload TLS certificate from disk, effectively rotating it */\nvoid tlsReload(void){\n    /* We will only bother checking keys and certs if TLS is enabled, otherwise we would be calling 'stat' for no reason */\n    if (g_pserver->tls_rotation && (g_pserver->tls_port || g_pserver->tls_replication || g_pserver->tls_cluster)){\n\n        bool cert_file_modified = getLastModifiedTime(g_pserver->tls_ctx_config.cert_file) != g_pserver->tls_ctx_config.cert_file_last_modified;\n        bool key_file_modified = getLastModifiedTime(g_pserver->tls_ctx_config.key_file) != g_pserver->tls_ctx_config.key_file_last_modified;\n\n        bool client_cert_file_modified = g_pserver->tls_ctx_config.client_cert_file != NULL && getLastModifiedTime(g_pserver->tls_ctx_config.client_cert_file) != g_pserver->tls_ctx_config.client_cert_file_last_modified;\n        bool client_key_file_modified = g_pserver->tls_ctx_config.client_key_file != NULL && getLastModifiedTime(g_pserver->tls_ctx_config.client_key_file) != g_pserver->tls_ctx_config.client_key_file_last_modified;\n\n        bool ca_cert_file_modified = g_pserver->tls_ctx_config.ca_cert_file != NULL && getLastModifiedTime(g_pserver->tls_ctx_config.ca_cert_file) != g_pserver->tls_ctx_config.ca_cert_file_last_modified;\n        bool ca_cert_dir_modified = g_pserver->tls_ctx_config.ca_cert_dir != NULL && getLastModifiedTime(g_pserver->tls_ctx_config.ca_cert_dir) != g_pserver->tls_ctx_config.ca_cert_dir_last_modified;\n\n        if (cert_file_modified || key_file_modified || ca_cert_file_modified || ca_cert_dir_modified || client_cert_file_modified || client_key_file_modified){\n            serverLog(LL_NOTICE, \"TLS certificates changed on disk, attempting to rotate.\");\n            if (tlsConfigure(&g_pserver->tls_ctx_config) == C_ERR) {\n                serverLog(LL_NOTICE, \"Error trying to rotate TLS certificates, TLS credentials remain unchanged.\");\n            } else {\n                serverLog(LL_NOTICE, \"TLS certificates rotated successfully.\");\n            }\n        }\n    }\n}\n\n#ifdef TLS_DEBUGGING\n#define TLSCONN_DEBUG(fmt, ...) \\\n    serverLog(LL_DEBUG, \"TLSCONN: \" fmt, __VA_ARGS__)\n#else\n#define TLSCONN_DEBUG(fmt, ...)\n#endif\n\nextern ConnectionType CT_TLS;\n\n/* Normal socket connections have a simple events/handler correlation.\n *\n * With TLS connections we need to handle cases where during a logical read\n * or write operation, the SSL library asks to block for the opposite\n * socket operation.\n *\n * When this happens, we need to do two things:\n * 1. Make sure we register for the event.\n * 2. Make sure we know which handler needs to execute when the\n *    event fires.  That is, if we notify the caller of a write operation\n *    that it blocks, and SSL asks for a read, we need to trigger the\n *    write handler again on the next read event.\n *\n */\n\ntypedef enum {\n    WANT_INVALID = 0,\n    WANT_READ = 1,\n    WANT_WRITE\n} WantIOType;\n\n#define TLS_CONN_FLAG_READ_WANT_WRITE   (1<<0)\n#define TLS_CONN_FLAG_WRITE_WANT_READ   (1<<1)\n#define TLS_CONN_FLAG_FD_SET            (1<<2)\n\ntypedef struct tls_connection {\n    connection c;\n    int flags;\n    SSL *ssl;\n    char *ssl_error;\n    listNode *pending_list_node;\n    aeEventLoop *el;\n} tls_connection;\n\n/* Check to see if a given client name is contained in the provided set (allowlist/blocklist)\n * Return true if it does */\nbool tlsCheckAgainstAllowlist(const char * client, std::set<sdsstring> set){\n    /* Because of wildcard matching, we need to iterate over the entire set.\n     * If we were doing simply straight matching, we could just directly \n     * check to see if the client name is in the set in O(1) time */\n    for (auto &client_pattern: set){\n        if (stringmatchlen(client_pattern.get(), client_pattern.size(), client, strlen(client), 1))\n            return true;\n    }\n    return false;\n}\n\n/* Sets the sha256 certificate fingerprint on the connection\n * Based on the example here https://fm4dd.com/openssl/certfprint.shtm */\nvoid tlsSetCertificateFingerprint(tls_connection* conn, X509 * cert) {\n    unsigned int fprint_size;\n    unsigned char fprint[EVP_MAX_MD_SIZE];\n    const EVP_MD *fprint_type = EVP_sha256();\n    X509_digest(cert, fprint_type, fprint, &fprint_size);\n\n    if (conn->c.fprint) zfree(conn->c.fprint);\n    conn->c.fprint = (char*)zcalloc(fprint_size*2+1);\n\n    /* Format fingerprint as hex string */\n    char tmp[3];\n    for (unsigned int i = 0; i < fprint_size; i++) {\n        snprintf(tmp, 2, \"%02x\", (unsigned int)fprint[i]);\n        strncat(conn->c.fprint, tmp, 2);\n    }\n}\n\n/* ASN1_STRING_get0_data was introduced in OPENSSL 1.1.1\n * use ASN1_STRING_data for older versions where it is not available */\n#if OPENSSL_VERSION_NUMBER < 0x10100000L\n#define ASN1_STRING_get0_data ASN1_STRING_data \n#endif \n\nclass TCleanup {\n    std::function<void()> fn;\n\npublic:\n    TCleanup(std::function<void()> fn)\n        : fn(fn)\n    {}\n\n    ~TCleanup() {\n        fn();\n    }\n};\n\nbool tlsCheckCertificateAgainstAllowlist(tls_connection* conn, std::set<sdsstring> allowlist, const char** commonName){\n    if (allowlist.empty()){\n        // An empty list implies acceptance of all\n        return true;\n    }\n\n    X509 * cert = SSL_get_peer_certificate(conn->ssl);\n    TCleanup certClen([cert]{X509_free(cert);});\n\n    /* Check the common name (CN) of the certificate first */\n    X509_NAME_ENTRY * ne = X509_NAME_get_entry(X509_get_subject_name(cert), X509_NAME_get_index_by_NID(X509_get_subject_name(cert), NID_commonName, -1));\n    *commonName = reinterpret_cast<const char*>(ASN1_STRING_get0_data(X509_NAME_ENTRY_get_data(ne)));\n\n    tlsSetCertificateFingerprint(conn, cert);\n\n    if (tlsCheckAgainstAllowlist(*commonName, allowlist)) {\n        return true;\n    }\n\n    /* If that fails, check through the subject alternative names (SANs) as well */\n    GENERAL_NAMES* subjectAltNames = (GENERAL_NAMES*)X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);\n    if (subjectAltNames != nullptr){\n        for (int i = 0; i < sk_GENERAL_NAME_num(subjectAltNames); i++)\n        {\n            GENERAL_NAME* generalName = sk_GENERAL_NAME_value(subjectAltNames, i);\n            /* Short circuit if one of the SANs match. \n             * We only support DNS, EMAIL, URI, and IP (specifically IPv4) */\n            switch (generalName->type)\n            {\n                case GEN_EMAIL:\n                    if (tlsCheckAgainstAllowlist(reinterpret_cast<const char*>(ASN1_STRING_get0_data(generalName->d.rfc822Name)), allowlist)){\n                        sk_GENERAL_NAME_pop_free(subjectAltNames, GENERAL_NAME_free);\n                        return true;\n                    }\n                    break;\n                case GEN_DNS:\n                    if (tlsCheckAgainstAllowlist(reinterpret_cast<const char*>(ASN1_STRING_get0_data(generalName->d.dNSName)), allowlist)){\n                        sk_GENERAL_NAME_pop_free(subjectAltNames, GENERAL_NAME_free);\n                        return true;\n                    }\n                    break;\n                case GEN_URI:\n                    if (tlsCheckAgainstAllowlist(reinterpret_cast<const char*>(ASN1_STRING_get0_data(generalName->d.uniformResourceIdentifier)), allowlist)){\n                        sk_GENERAL_NAME_pop_free(subjectAltNames, GENERAL_NAME_free);\n                        return true;\n                    }\n                    break;\n                case GEN_IPADD:\n                    {\n                        int ipLen = ASN1_STRING_length(generalName->d.iPAddress);\n                        if (ipLen == 4){ //IPv4 case\n                            char addr[INET_ADDRSTRLEN];\n                            inet_ntop(AF_INET, ASN1_STRING_get0_data(generalName->d.iPAddress), addr, INET_ADDRSTRLEN);\n                            if (tlsCheckAgainstAllowlist(addr, allowlist)){\n                                sk_GENERAL_NAME_pop_free(subjectAltNames, GENERAL_NAME_free);\n                                return true;\n                            }\n                        } else if (ipLen == 16) { // IPv6 case\n                            /* We don't support IPv6 at the moment */\n                        }\n                    }\n                    break;     \n                default:\n                    break;\n            }\n        }\n        sk_GENERAL_NAME_pop_free(subjectAltNames, GENERAL_NAME_free);\n    }\n\n    return false;\n}\n\nbool tlsCertificateRequiresAuditLogging(tls_connection* conn){\n    const char* cn = \"\";\n    if (tlsCheckCertificateAgainstAllowlist(conn, g_pserver->tls_auditlog_blocklist, &cn)) {\n        // Certificate is in exclusion list, no need to audit log\n        serverLog(LL_NOTICE, \"Audit Log: disabled for %s\", conn->c.fprint);\n        return false;\n    } else {\n        serverLog(LL_NOTICE, \"Audit Log: enabled for %s\", conn->c.fprint);\n        return true;\n    }\n}\n\nbool tlsValidateCertificateName(tls_connection* conn){\n    const char* cn = \"\";\n    if (tlsCheckCertificateAgainstAllowlist(conn, g_pserver->tls_allowlist, &cn)) {\n        return true;\n    } else {\n        /* If neither the CN nor the SANs match, update the SSL error and return false */\n        conn->c.last_errno = 0;\n        if (conn->ssl_error) zfree(conn->ssl_error);\n        size_t bufsize = 512;\n        conn->ssl_error = (char*)zmalloc(bufsize);\n        snprintf(conn->ssl_error, bufsize, \"Client CN (%s) and SANs not found in allowlist.\", cn);\n        return false;\n    }\n}\n\nstatic connection *createTLSConnection(int client_side) {\n    SSL_CTX *ctx = redis_tls_ctx;\n    if (client_side && redis_tls_client_ctx)\n        ctx = redis_tls_client_ctx;\n    tls_connection *conn = (tls_connection*)zcalloc(sizeof(tls_connection));\n    conn->c.type = &CT_TLS;\n    conn->c.fd = -1;\n    std::unique_lock<fastlock> ul(g_ctxtlock);\n    conn->ssl = SSL_new(ctx);\n    conn->el = serverTL->el;\n    return (connection *) conn;\n}\n\nvoid connTLSMarshalThread(connection *c) {\n    tls_connection *conn = (tls_connection*)c;\n    serverAssert(conn->pending_list_node == nullptr);\n    conn->el = serverTL->el;\n}\n\n/* Fetch the latest OpenSSL error and store it in the connection */\nstatic void updateTLSError(tls_connection *conn) {\n    conn->c.last_errno = 0;\n    if (conn->ssl_error) zfree(conn->ssl_error);\n    conn->ssl_error = (char*)zmalloc(512);\n    ERR_error_string_n(ERR_get_error(), conn->ssl_error, 512);\n}\n\nconnection *connCreateTLS(void) {\n    return createTLSConnection(1);\n}\n\n/* Create a new TLS connection that is already associated with\n * an accepted underlying file descriptor.\n *\n * The socket is not ready for I/O until connAccept() was called and\n * invoked the connection-level accept handler.\n *\n * Callers should use connGetState() and verify the created connection\n * is not in an error state.\n */\nconnection *connCreateAcceptedTLS(int fd, int require_auth) {\n    tls_connection *conn = (tls_connection *) createTLSConnection(0);\n    conn->c.fd = fd;\n    conn->c.state = CONN_STATE_ACCEPTING;\n\n    if (!conn->ssl) {\n        updateTLSError(conn);\n        conn->c.state = CONN_STATE_ERROR;\n        return (connection *) conn;\n    }\n\n    switch (require_auth) {\n        case TLS_CLIENT_AUTH_NO:\n            SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);\n            break;\n        case TLS_CLIENT_AUTH_OPTIONAL:\n            SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, NULL);\n            break;\n        default: /* TLS_CLIENT_AUTH_YES, also fall-secure */\n            SSL_set_verify(conn->ssl, SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL);\n            break;\n    }\n\n    SSL_set_fd(conn->ssl, conn->c.fd);\n    SSL_set_accept_state(conn->ssl);\n\n    return (connection *) conn;\n}\n\nstatic void tlsEventHandler(struct aeEventLoop *el, int fd, void *clientData, int mask);\n\n/* Process the return code received from OpenSSL>\n * Update the want parameter with expected I/O.\n * Update the connection's error state if a real error has occured.\n * Returns an SSL error code, or 0 if no further handling is required.\n */\nstatic int handleSSLReturnCode(tls_connection *conn, int ret_value, WantIOType *want) {\n    if (ret_value <= 0) {\n        int ssl_err = SSL_get_error(conn->ssl, ret_value);\n        switch (ssl_err) {\n            case SSL_ERROR_WANT_WRITE:\n                *want = WANT_WRITE;\n                return 0;\n            case SSL_ERROR_WANT_READ:\n                *want = WANT_READ;\n                return 0;\n            case SSL_ERROR_SYSCALL:\n                conn->c.last_errno = errno;\n                if (conn->ssl_error) zfree(conn->ssl_error);\n                conn->ssl_error = errno ? zstrdup(strerror(errno)) : NULL;\n                break;\n            default:\n                /* Error! */\n                updateTLSError(conn);\n                break;\n        }\n\n        return ssl_err;\n    }\n\n    return 0;\n}\n\nvoid registerSSLEvent(tls_connection *conn, WantIOType want) {\n    int mask = aeGetFileEvents(serverTL->el, conn->c.fd);\n\n    serverAssert(conn->el == serverTL->el);\n\n    switch (want) {\n        case WANT_READ:\n            if (mask & AE_WRITABLE) aeDeleteFileEvent(conn->el, conn->c.fd, AE_WRITABLE);\n            if (!(mask & AE_READABLE)) aeCreateFileEvent(conn->el, conn->c.fd, AE_READABLE|AE_READ_THREADSAFE,\n                        tlsEventHandler, conn);\n            break;\n        case WANT_WRITE:\n            if (mask & AE_READABLE) aeDeleteFileEvent(conn->el, conn->c.fd, AE_READABLE);\n            if (!(mask & AE_WRITABLE)) aeCreateFileEvent(conn->el, conn->c.fd, AE_WRITABLE|AE_WRITE_THREADSAFE,\n                        tlsEventHandler, conn);\n            break;\n        default:\n            serverAssert(0);\n            break;\n    }\n}\n\nvoid updateSSLEventCore(tls_connection *conn) {\n    int mask = aeGetFileEvents(serverTL->el, conn->c.fd);\n    int need_read = conn->c.read_handler || (conn->flags & TLS_CONN_FLAG_WRITE_WANT_READ);\n    int need_write = conn->c.write_handler || (conn->flags & TLS_CONN_FLAG_READ_WANT_WRITE);\n\n    serverAssert(conn->el == serverTL->el);\n\n    if (need_read && !(mask & AE_READABLE))\n        aeCreateFileEvent(serverTL->el, conn->c.fd, AE_READABLE|AE_READ_THREADSAFE, tlsEventHandler, conn);\n    if (!need_read && (mask & AE_READABLE))\n        aeDeleteFileEvent(serverTL->el, conn->c.fd, AE_READABLE);\n\n    if (need_write && !(mask & AE_WRITABLE))\n        aeCreateFileEvent(serverTL->el, conn->c.fd, AE_WRITABLE|AE_WRITE_THREADSAFE, tlsEventHandler, conn);\n    if (!need_write && (mask & AE_WRITABLE))\n        aeDeleteFileEvent(serverTL->el, conn->c.fd, AE_WRITABLE);\n}\n\nvoid updateSSLEvent(tls_connection *conn) {\n    if (conn->el != serverTL->el) {\n        aePostFunction(conn->el, [conn]{\n            updateSSLEventCore(conn);\n        });\n    } else {\n        updateSSLEventCore(conn);\n    }\n}\n\nvoid tlsHandleEvent(tls_connection *conn, int mask) {\n    int ret, conn_error;\n    serverAssert(conn->el == serverTL->el);\n\n    TLSCONN_DEBUG(\"tlsEventHandler(): fd=%d, state=%d, mask=%d, r=%d, w=%d, flags=%d\",\n            fd, conn->c.state, mask, conn->c.read_handler != NULL, conn->c.write_handler != NULL,\n            conn->flags);\n\n    ERR_clear_error();\n\n    switch (conn->c.state) {\n        case CONN_STATE_CONNECTING:\n            conn_error = connGetSocketError((connection *) conn);\n            if (conn_error) {\n                conn->c.last_errno = conn_error;\n                conn->c.state = CONN_STATE_ERROR;\n            } else {\n                if (!(conn->flags & TLS_CONN_FLAG_FD_SET)) {\n                    SSL_set_fd(conn->ssl, conn->c.fd);\n                    conn->flags |= TLS_CONN_FLAG_FD_SET;\n                }\n                ret = SSL_connect(conn->ssl);\n                if (ret <= 0) {\n                    WantIOType want = WANT_INVALID;\n                    if (!handleSSLReturnCode(conn, ret, &want)) {\n                        registerSSLEvent(conn, want);\n\n                        /* Avoid hitting UpdateSSLEvent, which knows nothing\n                         * of what SSL_connect() wants and instead looks at our\n                         * R/W handlers.\n                         */\n                        return;\n                    }\n\n                    /* If not handled, it's an error */\n                    conn->c.state = CONN_STATE_ERROR;\n                } else {\n                    conn->c.state = CONN_STATE_CONNECTED;\n                }\n            }\n\n            {\n            AeLocker locker;\n            locker.arm(nullptr);\n            if (!callHandler((connection *) conn, conn->c.conn_handler)) return;\n            }\n            conn->c.conn_handler = NULL;\n            break;\n        case CONN_STATE_ACCEPTING:\n            ret = SSL_accept(conn->ssl);\n            if (ret <= 0) {\n                WantIOType want = WANT_INVALID;\n                if (!handleSSLReturnCode(conn, ret, &want)) {\n                    /* Avoid hitting UpdateSSLEvent, which knows nothing\n                     * of what SSL_connect() wants and instead looks at our\n                     * R/W handlers.\n                     */\n                    registerSSLEvent(conn, want);\n                    return;\n                }\n\n                /* If not handled, it's an error */\n                conn->c.state = CONN_STATE_ERROR;\n            } else {\n                /* Validate name */\n                if (!tlsValidateCertificateName(conn)){\n                    conn->c.state = CONN_STATE_ERROR;\n                } else {\n                    conn->c.state = CONN_STATE_CONNECTED;\n                    if (tlsCertificateRequiresAuditLogging(conn)){\n                        conn->c.flags |= CONN_FLAG_AUDIT_LOGGING_REQUIRED;\n                    }\n                }\n            }\n\n            {\n            AeLocker locker;\n            locker.arm(nullptr);\n            if (!callHandler((connection *) conn, conn->c.conn_handler)) return;\n            }\n            conn->c.conn_handler = NULL;\n            break;\n        case CONN_STATE_CONNECTED:\n        {\n            int call_read = ((mask & AE_READABLE) && conn->c.read_handler) ||\n                ((mask & AE_WRITABLE) && (conn->flags & TLS_CONN_FLAG_READ_WANT_WRITE));\n            int call_write = ((mask & AE_WRITABLE) && conn->c.write_handler) ||\n                ((mask & AE_READABLE) && (conn->flags & TLS_CONN_FLAG_WRITE_WANT_READ));\n\n            /* Normally we execute the readable event first, and the writable\n             * event laster. This is useful as sometimes we may be able\n             * to serve the reply of a query immediately after processing the\n             * query.\n             *\n             * However if WRITE_BARRIER is set in the mask, our application is\n             * asking us to do the reverse: never fire the writable event\n             * after the readable. In such a case, we invert the calls.\n             * This is useful when, for instance, we want to do things\n             * in the beforeSleep() hook, like fsynching a file to disk,\n             * before replying to a client. */\n            int invert = conn->c.flags & CONN_FLAG_WRITE_BARRIER;\n\n            if (!invert && call_read) {\n                AeLocker lock;\n                if (!(conn->c.flags & CONN_FLAG_READ_THREADSAFE))\n                    lock.arm(nullptr);\n                conn->flags &= ~TLS_CONN_FLAG_READ_WANT_WRITE;\n                if (!callHandler((connection *) conn, conn->c.read_handler)) return;\n            }\n\n            /* Fire the writable event. */\n            if (call_write) {\n                AeLocker lock;\n                if (!(conn->c.flags & CONN_FLAG_WRITE_THREADSAFE))\n                    lock.arm(nullptr);\n                conn->flags &= ~TLS_CONN_FLAG_WRITE_WANT_READ;\n                if (!callHandler((connection *) conn, conn->c.write_handler)) return;\n            }\n\n            /* If we have to invert the call, fire the readable event now\n             * after the writable one. */\n            if (invert && call_read) {\n                AeLocker lock;\n                if (!(conn->c.flags & CONN_FLAG_READ_THREADSAFE))\n                    lock.arm(nullptr);\n                conn->flags &= ~TLS_CONN_FLAG_READ_WANT_WRITE;\n                if (!callHandler((connection *) conn, conn->c.read_handler)) return;\n            }\n\n            /* If SSL has pending that, already read from the socket, we're at\n             * risk of not calling the read handler again, make sure to add it\n             * to a list of pending connection that should be handled anyway. */\n            if ((mask & AE_READABLE)) {\n                if (SSL_pending(conn->ssl) > 0) {\n                    if (!conn->pending_list_node) {\n                        listAddNodeTail(pending_list, conn);\n                        conn->pending_list_node = listLast(pending_list);\n                    }\n                } else if (conn->pending_list_node) {\n                    listDelNode(pending_list, conn->pending_list_node);\n                    conn->pending_list_node = NULL;\n                }\n            }\n\n            break;\n        }\n        default:\n            break;\n    }\n\n    updateSSLEvent(conn);\n}\n\nstatic void tlsEventHandler(struct aeEventLoop *el, int fd, void *clientData, int mask) {\n    UNUSED(el);\n    UNUSED(fd);\n    tls_connection *conn = (tls_connection*)clientData;\n    tlsHandleEvent(conn, mask);\n}\n\nstatic void connTLSCloseCore(tls_connection *conn) {\n    serverAssert(conn->el == serverTL->el);\n\n    if (conn->ssl) {\n        SSL_free(conn->ssl);\n        conn->ssl = NULL;\n    }\n\n    if (conn->ssl_error) {\n        zfree(conn->ssl_error);\n        conn->ssl_error = NULL;\n    }\n\n    if (conn->pending_list_node) {\n        listDelNode(pending_list, conn->pending_list_node);\n        conn->pending_list_node = NULL;\n    }\n\n    CT_Socket.close(&conn->c);\n}\n\nstatic void connTLSClose(connection *conn_) {\n    tls_connection *conn = (tls_connection *) conn_;\n    if (conn->el != serverTL->el) {\n        aePostFunction(conn->el, [conn]{\n            connTLSCloseCore(conn);\n        });\n    } else {\n        connTLSCloseCore(conn);\n    }\n}\n\nstatic int connTLSAccept(connection *_conn, ConnectionCallbackFunc accept_handler) {\n    tls_connection *conn = (tls_connection *) _conn;\n    int ret;\n\n    serverAssert(conn->el == serverTL->el);\n\n    if (conn->c.state != CONN_STATE_ACCEPTING) return C_ERR;\n    ERR_clear_error();\n\n    /* Try to accept */\n    conn->c.conn_handler = accept_handler;\n    ret = SSL_accept(conn->ssl);\n\n    if (ret <= 0) {\n        WantIOType want = WANT_INVALID;\n        if (!handleSSLReturnCode(conn, ret, &want)) {\n            registerSSLEvent(conn, want);   /* We'll fire back */\n            return C_OK;\n        } else {\n            conn->c.state = CONN_STATE_ERROR;\n            return C_ERR;\n        }\n    }\n\n    conn->c.state = CONN_STATE_CONNECTED;\n    if (!callHandler((connection *) conn, conn->c.conn_handler)) return C_OK;\n    conn->c.conn_handler = NULL;\n\n    return C_OK;\n}\n\nstatic int connTLSConnect(connection *conn_, const char *addr, int port, const char *src_addr, ConnectionCallbackFunc connect_handler) {\n    tls_connection *conn = (tls_connection *) conn_;\n\n    serverAssert(conn->el == serverTL->el);\n\n    if (conn->c.state != CONN_STATE_NONE) return C_ERR;\n    ERR_clear_error();\n\n    /* Initiate Socket connection first */\n    if (CT_Socket.connect(conn_, addr, port, src_addr, connect_handler) == C_ERR) return C_ERR;\n\n    /* Return now, once the socket is connected we'll initiate\n     * TLS connection from the event handler.\n     */\n    return C_OK;\n}\n\nstatic int connTLSWrite(connection *conn_, const void *data, size_t data_len) {\n    tls_connection *conn = (tls_connection *) conn_;\n    int ret, ssl_err;\n\n    if (data_len == 0)\n        return 0;\n\n    if (conn->c.state != CONN_STATE_CONNECTED) return -1;\n    ERR_clear_error();\n    ret = SSL_write(conn->ssl, data, data_len);\n\n    if (ret <= 0) {\n        WantIOType want = WANT_INVALID;\n        if (!(ssl_err = handleSSLReturnCode(conn, ret, &want))) {\n            if (want == WANT_READ) conn->flags |= TLS_CONN_FLAG_WRITE_WANT_READ;\n            updateSSLEvent(conn);\n            errno = EAGAIN;\n            return -1;\n        } else {\n            if (ssl_err == SSL_ERROR_ZERO_RETURN ||\n                    ((ssl_err == SSL_ERROR_SYSCALL && !errno))) {\n                conn->c.state = CONN_STATE_CLOSED;\n                return 0;\n            } else {\n                conn->c.state = CONN_STATE_ERROR;\n                return -1;\n            }\n        }\n    }\n\n    return ret;\n}\n\nstatic int connTLSRead(connection *conn_, void *buf, size_t buf_len) {\n    tls_connection *conn = (tls_connection *) conn_;\n    int ret;\n    int ssl_err;\n\n    serverAssert(conn->el == serverTL->el);\n\n    if (conn->c.state != CONN_STATE_CONNECTED) return -1;\n    ERR_clear_error();\n    ret = SSL_read(conn->ssl, buf, buf_len);\n    if (ret <= 0) {\n        WantIOType want = WANT_INVALID;\n        if (!(ssl_err = handleSSLReturnCode(conn, ret, &want))) {\n            if (want == WANT_WRITE) conn->flags |= TLS_CONN_FLAG_READ_WANT_WRITE;\n            updateSSLEvent(conn);\n\n            errno = EAGAIN;\n            return -1;\n        } else {\n            if (ssl_err == SSL_ERROR_ZERO_RETURN ||\n                    ((ssl_err == SSL_ERROR_SYSCALL) && !errno)) {\n                conn->c.state = CONN_STATE_CLOSED;\n                return 0;\n            } else {\n                conn->c.state = CONN_STATE_ERROR;\n                return -1;\n            }\n        }\n    }\n\n    return ret;\n}\n\nstatic const char *connTLSGetLastError(connection *conn_) {\n    tls_connection *conn = (tls_connection *) conn_;\n\n    if (conn->ssl_error) return conn->ssl_error;\n    return NULL;\n}\n\nint connTLSSetWriteHandler(connection *conn, ConnectionCallbackFunc func, int barrier, bool fThreadSafe) {\n    serverAssert(((tls_connection*)conn)->el == serverTL->el);\n    conn->write_handler = func;\n    if (barrier)\n        conn->flags |= CONN_FLAG_WRITE_BARRIER;\n    else\n        conn->flags &= ~CONN_FLAG_WRITE_BARRIER;\n\n    if (fThreadSafe)\n        conn->flags |= CONN_FLAG_WRITE_THREADSAFE;\n    else\n        conn->flags &= ~CONN_FLAG_WRITE_THREADSAFE;\n\n    updateSSLEvent((tls_connection *) conn);\n    return C_OK;\n}\n\nint connTLSSetReadHandler(connection *conn, ConnectionCallbackFunc func, bool fThreadSafe) {\n    serverAssert(((tls_connection*)conn)->el == serverTL->el);\n    conn->read_handler = func;\n\n    if (fThreadSafe)\n        conn->flags |= CONN_FLAG_READ_THREADSAFE;\n    else\n        conn->flags &= ~CONN_FLAG_READ_THREADSAFE;\n\n    updateSSLEvent((tls_connection *) conn);\n    return C_OK;\n}\n\nstatic void setBlockingTimeout(tls_connection *conn, long long timeout) {\n    anetBlock(NULL, conn->c.fd);\n    anetSendTimeout(NULL, conn->c.fd, timeout);\n    anetRecvTimeout(NULL, conn->c.fd, timeout);\n}\n\nstatic void unsetBlockingTimeout(tls_connection *conn) {\n    anetNonBlock(NULL, conn->c.fd);\n    anetSendTimeout(NULL, conn->c.fd, 0);\n    anetRecvTimeout(NULL, conn->c.fd, 0);\n}\n\nstatic int connTLSBlockingConnect(connection *conn_, const char *addr, int port, long long timeout) {\n    tls_connection *conn = (tls_connection *) conn_;\n    int ret;\n\n    serverAssert(conn->el == serverTL->el);\n\n    if (conn->c.state != CONN_STATE_NONE) return C_ERR;\n\n    /* Initiate socket blocking connect first */\n    if (CT_Socket.blocking_connect(conn_, addr, port, timeout) == C_ERR) return C_ERR;\n\n    /* Initiate TLS connection now.  We set up a send/recv timeout on the socket,\n     * which means the specified timeout will not be enforced accurately. */\n    SSL_set_fd(conn->ssl, conn->c.fd);\n    setBlockingTimeout(conn, timeout);\n\n    if ((ret = SSL_connect(conn->ssl)) <= 0) {\n        conn->c.state = CONN_STATE_ERROR;\n        return C_ERR;\n    }\n    unsetBlockingTimeout(conn);\n\n    conn->c.state = CONN_STATE_CONNECTED;\n    return C_OK;\n}\n\nstatic ssize_t connTLSSyncWrite(connection *conn_, const char *ptr, ssize_t size, long long timeout) {\n    tls_connection *conn = (tls_connection *) conn_;\n\n    serverAssert(conn->el == serverTL->el);\n\n    setBlockingTimeout(conn, timeout);\n    SSL_clear_mode(conn->ssl, SSL_MODE_ENABLE_PARTIAL_WRITE);\n    int ret = SSL_write(conn->ssl, ptr, size);\n    SSL_set_mode(conn->ssl, SSL_MODE_ENABLE_PARTIAL_WRITE);\n    unsetBlockingTimeout(conn);\n\n    return ret;\n}\n\nstatic ssize_t connTLSSyncRead(connection *conn_, char *ptr, ssize_t size, long long timeout) {\n    tls_connection *conn = (tls_connection *) conn_;\n\n    serverAssert(conn->el == serverTL->el);\n\n    setBlockingTimeout(conn, timeout);\n    int ret = SSL_read(conn->ssl, ptr, size);\n    unsetBlockingTimeout(conn);\n\n    return ret;\n}\n\nstatic ssize_t connTLSSyncReadLine(connection *conn_, char *ptr, ssize_t size, long long timeout) {\n    tls_connection *conn = (tls_connection *) conn_;\n    ssize_t nread = 0;\n\n    serverAssert(conn->el == serverTL->el);\n\n    setBlockingTimeout(conn, timeout);\n\n    size--;\n    while(size) {\n        char c;\n\n        if (SSL_read(conn->ssl,&c,1) <= 0) {\n            nread = -1;\n            goto exit;\n        }\n        if (c == '\\n') {\n            *ptr = '\\0';\n            if (nread && *(ptr-1) == '\\r') *(ptr-1) = '\\0';\n            goto exit;\n        } else {\n            *ptr++ = c;\n            *ptr = '\\0';\n            nread++;\n        }\n        size--;\n    }\nexit:\n    unsetBlockingTimeout(conn);\n    return nread;\n}\n\nstatic int connTLSGetType(connection *conn_) {\n    (void) conn_;\n\n    return CONN_TYPE_TLS;\n}\n\nConnectionType CT_TLS = {\n    tlsEventHandler,\n    connTLSConnect,\n    connTLSWrite,\n    connTLSRead,\n    connTLSClose,\n    connTLSAccept,\n    connTLSSetWriteHandler,\n    connTLSSetReadHandler,\n    connTLSGetLastError,\n    connTLSBlockingConnect,\n    connTLSSyncWrite,\n    connTLSSyncRead,\n    connTLSSyncReadLine,\n    connTLSMarshalThread,\n    connTLSGetType\n};\n\nint tlsHasPendingData() {\n    if (!pending_list)\n        return 0;\n    return listLength(pending_list) > 0;\n}\n\nint tlsProcessPendingData() {\n    listIter li;\n    listNode *ln;\n    serverAssert(!GlobalLocksAcquired());\n\n    int processed = listLength(pending_list);\n    listRewind(pending_list,&li);\n    while((ln = listNext(&li))) {\n        tls_connection *conn = (tls_connection*)listNodeValue(ln);\n        tlsHandleEvent(conn, AE_READABLE);\n    }\n    return processed;\n}\n\n/* Fetch the peer certificate used for authentication on the specified\n * connection and return it as a PEM-encoded sds.\n */\nsds connTLSGetPeerCert(connection *conn_) {\n    tls_connection *conn = (tls_connection *) conn_;\n    if (conn_->type->get_type(conn_) != CONN_TYPE_TLS || !conn->ssl) return NULL;\n\n    X509 *cert = SSL_get_peer_certificate(conn->ssl);\n    if (!cert) return NULL;\n\n    BIO *bio = BIO_new(BIO_s_mem());\n    if (bio == NULL || !PEM_write_bio_X509(bio, cert)) {\n        if (bio != NULL) BIO_free(bio);\n        return NULL;\n    }\n\n    const char *bio_ptr;\n    long long bio_len = BIO_get_mem_data(bio, &bio_ptr);\n    sds cert_pem = sdsnewlen(bio_ptr, bio_len);\n    BIO_free(bio);\n\n    return cert_pem;\n}\n\n#else   /* USE_OPENSSL */\n\nvoid tlsInit(void) {\n}\n\nvoid tlsCleanup(void) {\n}\n\nint tlsConfigure(redisTLSContextConfig *ctx_config) {\n    UNUSED(ctx_config);\n    return C_OK;\n}\n\nvoid tlsReload(void){\n}\n\nconnection *connCreateTLS(void) { \n    return NULL;\n}\n\nconnection *connCreateAcceptedTLS(int fd, int require_auth) {\n    UNUSED(fd);\n    UNUSED(require_auth);\n\n    return NULL;\n}\n\nint tlsHasPendingData() {\n    return 0;\n}\n\nint tlsProcessPendingData() {\n    return 0;\n}\n\nvoid tlsInitThread() {}\nvoid tlsCleanupThread(void) {}\n\nsds connTLSGetPeerCert(connection *conn_) {\n    (void) conn_;\n    return NULL;\n}\n\n#endif\n"
  },
  {
    "path": "src/tracking.cpp",
    "content": "/* tracking.c - Client side caching: keys tracking and invalidation\n *\n * Copyright (c) 2019, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"server.h\"\n\n/* The tracking table is constituted by a radix tree of keys, each pointing\n * to a radix tree of client IDs, used to track the clients that may have\n * certain keys in their local, client side, cache.\n *\n * When a client enables tracking with \"CLIENT TRACKING on\", each key served to\n * the client is remembered in the table mapping the keys to the client IDs.\n * Later, when a key is modified, all the clients that may have local copy\n * of such key will receive an invalidation message.\n *\n * Clients will normally take frequently requested objects in memory, removing\n * them when invalidation messages are received. */\nrax *TrackingTable = NULL;\nrax *PrefixTable = NULL;\nuint64_t TrackingTableTotalItems = 0; /* Total number of IDs stored across\n                                         the whole tracking table. This gives\n                                         an hint about the total memory we\n                                         are using server side for CSC. */\nrobj *TrackingChannelName;\n\n/* This is the structure that we have as value of the PrefixTable, and\n * represents the list of keys modified, and the list of clients that need\n * to be notified, for a given prefix. */\ntypedef struct bcastState {\n    rax *keys;      /* Keys modified in the current event loop cycle. */\n    rax *clients;   /* Clients subscribed to the notification events for this\n                       prefix. */\n} bcastState;\n\n/* Remove the tracking state from the client 'c'. Note that there is not much\n * to do for us here, if not to decrement the counter of the clients in\n * tracking mode, because we just store the ID of the client in the tracking\n * table, so we'll remove the ID reference in a lazy way. Otherwise when a\n * client with many entries in the table is removed, it would cost a lot of\n * time to do the cleanup. */\nvoid disableTracking(client *c) {\n    /* If this client is in broadcasting mode, we need to unsubscribe it\n     * from all the prefixes it is registered to. */\n    if (c->flags & CLIENT_TRACKING_BCAST) {\n        raxIterator ri;\n        raxStart(&ri,c->client_tracking_prefixes);\n        raxSeek(&ri,\"^\",NULL,0);\n        while(raxNext(&ri)) {\n            bcastState *bs = (bcastState*)raxFind(PrefixTable,ri.key,ri.key_len);\n            serverAssert(bs != raxNotFound);\n            raxRemove(bs->clients,(unsigned char*)&c,sizeof(c),NULL);\n            /* Was it the last client? Remove the prefix from the\n             * table. */\n            if (raxSize(bs->clients) == 0) {\n                raxFree(bs->clients);\n                raxFree(bs->keys);\n                zfree(bs);\n                raxRemove(PrefixTable,ri.key,ri.key_len,NULL);\n            }\n        }\n        raxStop(&ri);\n        raxFree(c->client_tracking_prefixes);\n        c->client_tracking_prefixes = NULL;\n    }\n\n    /* Clear flags and adjust the count. */\n    if (c->flags & CLIENT_TRACKING) {\n        g_pserver->tracking_clients--;\n        c->flags &= ~(CLIENT_TRACKING|CLIENT_TRACKING_BROKEN_REDIR|\n                      CLIENT_TRACKING_BCAST|CLIENT_TRACKING_OPTIN|\n                      CLIENT_TRACKING_OPTOUT|CLIENT_TRACKING_CACHING|\n                      CLIENT_TRACKING_NOLOOP);\n    }\n}\n\nstatic int stringCheckPrefix(unsigned char *s1, size_t s1_len, unsigned char *s2, size_t s2_len) {\n    size_t min_length = s1_len < s2_len ? s1_len : s2_len;\n    return memcmp(s1,s2,min_length) == 0;   \n}\n\n/* Check if any of the provided prefixes collide with one another or\n * with an existing prefix for the client. A collision is defined as two \n * prefixes that will emit an invalidation for the same key. If no prefix \n * collision is found, 1 is return, otherwise 0 is returned and the client \n * has an error emitted describing the error. */\nint checkPrefixCollisionsOrReply(client *c, robj **prefixes, size_t numprefix) {\n    for (size_t i = 0; i < numprefix; i++) {\n        /* Check input list has no overlap with existing prefixes. */\n        if (c->client_tracking_prefixes) {\n            raxIterator ri;\n            raxStart(&ri,c->client_tracking_prefixes);\n            raxSeek(&ri,\"^\",NULL,0);\n            while(raxNext(&ri)) {\n                if (stringCheckPrefix(ri.key,ri.key_len,\n                    (unsigned char*)ptrFromObj(prefixes[i]),sdslen(szFromObj(prefixes[i])))) \n                {\n                    sds collision = sdsnewlen(ri.key,ri.key_len);\n                    addReplyErrorFormat(c,\n                        \"Prefix '%s' overlaps with an existing prefix '%s'. \"\n                        \"Prefixes for a single client must not overlap.\",\n                        (unsigned char *)ptrFromObj(prefixes[i]),\n                        (unsigned char *)collision);\n                    sdsfree(collision);\n                    raxStop(&ri);\n                    return 0;\n                }\n            }\n            raxStop(&ri);\n        }\n        /* Check input has no overlap with itself. */\n        for (size_t j = i + 1; j < numprefix; j++) {\n            if (stringCheckPrefix((unsigned char*)ptrFromObj(prefixes[i]),sdslen(szFromObj(prefixes[i])),\n                (unsigned char*)ptrFromObj(prefixes[j]),sdslen(szFromObj(prefixes[j]))))\n            {\n                addReplyErrorFormat(c,\n                    \"Prefix '%s' overlaps with another provided prefix '%s'. \"\n                    \"Prefixes for a single client must not overlap.\",\n                    (unsigned char *)ptrFromObj(prefixes[i]),\n                    (unsigned char *)ptrFromObj(prefixes[j]));\n                return i;\n            }\n        }\n    }\n    return 1;\n}\n\n/* Set the client 'c' to track the prefix 'prefix'. If the client 'c' is\n * already registered for the specified prefix, no operation is performed. */\nvoid enableBcastTrackingForPrefix(client *c, const char *prefix, size_t plen) {\n    bcastState *bs = (bcastState*)raxFind(PrefixTable,(unsigned char*)prefix,plen);\n    /* If this is the first client subscribing to such prefix, create\n     * the prefix in the table. */\n    if (bs == raxNotFound) {\n        bs = (bcastState*)zmalloc(sizeof(*bs));\n        bs->keys = raxNew();\n        bs->clients = raxNew();\n        raxInsert(PrefixTable,(unsigned char*)prefix,plen,bs,NULL);\n    }\n    if (raxTryInsert(bs->clients,(unsigned char*)&c,sizeof(c),NULL,NULL)) {\n        if (c->client_tracking_prefixes == NULL)\n            c->client_tracking_prefixes = raxNew();\n        raxInsert(c->client_tracking_prefixes,\n                  (unsigned char*)prefix,plen,NULL,NULL);\n    }\n}\n\n/* Enable the tracking state for the client 'c', and as a side effect allocates\n * the tracking table if needed. If the 'redirect_to' argument is non zero, the\n * invalidation messages for this client will be sent to the client ID\n * specified by the 'redirect_to' argument. Note that if such client will\n * eventually get freed, we'll send a message to the original client to\n * inform it of the condition. Multiple clients can redirect the invalidation\n * messages to the same client ID. */\nvoid enableTracking(client *c, uint64_t redirect_to, uint64_t options, robj **prefix, size_t numprefix) {\n    if (!(c->flags & CLIENT_TRACKING)) g_pserver->tracking_clients++;\n    c->flags |= CLIENT_TRACKING;\n    c->flags &= ~(CLIENT_TRACKING_BROKEN_REDIR|CLIENT_TRACKING_BCAST|\n                  CLIENT_TRACKING_OPTIN|CLIENT_TRACKING_OPTOUT|\n                  CLIENT_TRACKING_NOLOOP);\n    c->client_tracking_redirection = redirect_to;\n\n    /* This may be the first client we ever enable. Create the tracking\n     * table if it does not exist. */\n    if (TrackingTable == NULL) {\n        TrackingTable = raxNew();\n        PrefixTable = raxNew();\n        TrackingChannelName = createStringObject(\"__redis__:invalidate\",20);\n    }\n\n    /* For broadcasting, set the list of prefixes in the client. */\n    if (options & CLIENT_TRACKING_BCAST) {\n        c->flags |= CLIENT_TRACKING_BCAST;\n        if (numprefix == 0) enableBcastTrackingForPrefix(c,\"\",0);\n        for (size_t j = 0; j < numprefix; j++) {\n            sds sdsprefix = szFromObj(prefix[j]);\n            enableBcastTrackingForPrefix(c,sdsprefix,sdslen(sdsprefix));\n        }\n    }\n\n    /* Set the remaining flags that don't need any special handling. */\n    c->flags |= options & (CLIENT_TRACKING_OPTIN|CLIENT_TRACKING_OPTOUT|\n                           CLIENT_TRACKING_NOLOOP);\n}\n\n/* This function is called after the execution of a readonly command in the\n * case the client 'c' has keys tracking enabled and the tracking is not\n * in BCAST mode. It will populate the tracking invalidation table according\n * to the keys the user fetched, so that Redis will know what are the clients\n * that should receive an invalidation message with certain groups of keys\n * are modified. */\nvoid trackingRememberKeys(client *c) {\n    /* Return if we are in optin/out mode and the right CACHING command\n     * was/wasn't given in order to modify the default behavior. */\n    uint64_t optin = c->flags & CLIENT_TRACKING_OPTIN;\n    uint64_t optout = c->flags & CLIENT_TRACKING_OPTOUT;\n    uint64_t caching_given = c->flags & CLIENT_TRACKING_CACHING;\n    if ((optin && !caching_given) || (optout && caching_given)) return;\n\n    getKeysResult result = GETKEYS_RESULT_INIT;\n    int numkeys = getKeysFromCommand(c->cmd,c->argv,c->argc,&result);\n    if (!numkeys) {\n        getKeysFreeResult(&result);\n        return;\n    }\n\n    int *keys = result.keys;\n\n    for(int j = 0; j < numkeys; j++) {\n        int idx = keys[j];\n        sds sdskey = szFromObj(c->argv[idx]);\n        rax *ids = (rax*)raxFind(TrackingTable,(unsigned char*)sdskey,sdslen(sdskey));\n        if (ids == raxNotFound) {\n            ids = raxNew();\n            int inserted = raxTryInsert(TrackingTable,(unsigned char*)sdskey,\n                                        sdslen(sdskey),ids, NULL);\n            serverAssert(inserted == 1);\n        }\n        if (raxTryInsert(ids,(unsigned char*)&c->id,sizeof(c->id),NULL,NULL))\n            TrackingTableTotalItems++;\n    }\n    getKeysFreeResult(&result);\n}\n\n/* Given a key name, this function sends an invalidation message in the\n * proper channel (depending on RESP version: PubSub or Push message) and\n * to the proper client (in case fo redirection), in the context of the\n * client 'c' with tracking enabled.\n *\n * In case the 'proto' argument is non zero, the function will assume that\n * 'keyname' points to a buffer of 'keylen' bytes already expressed in the\n * form of Redis RESP protocol. This is used for:\n * - In BCAST mode, to send an array of invalidated keys to all\n *   applicable clients\n * - Following a flush command, to send a single RESP NULL to indicate\n *   that all keys are now invalid. */\nvoid sendTrackingMessage(client *c, char *keyname, size_t keylen, int proto) {\n    std::unique_lock<fastlock> ul(c->lock);\n\n    int using_redirection = 0;\n    if (c->client_tracking_redirection) {\n        client *redir = lookupClientByID(c->client_tracking_redirection);\n        if (!redir) {\n            c->flags |= CLIENT_TRACKING_BROKEN_REDIR;\n            /* We need to signal to the original connection that we\n             * are unable to send invalidation messages to the redirected\n             * connection, because the client no longer exist. */\n            if (c->resp > 2) {\n                addReplyPushLen(c,2);\n                addReplyBulkCBuffer(c,\"tracking-redir-broken\",21);\n                addReplyLongLong(c,c->client_tracking_redirection);\n            }\n            return;\n        }\n        ul.unlock();\n        ul = std::unique_lock<fastlock>(redir->lock);\n        c = redir;\n        using_redirection = 1;\n    }\n\n    /* Only send such info for clients in RESP version 3 or more. However\n     * if redirection is active, and the connection we redirect to is\n     * in Pub/Sub mode, we can support the feature with RESP 2 as well,\n     * by sending Pub/Sub messages in the __redis__:invalidate channel. */\n    if (c->resp > 2) {\n        addReplyPushLen(c,2);\n        addReplyBulkCBuffer(c,\"invalidate\",10);\n    } else if (using_redirection && c->flags & CLIENT_PUBSUB) {\n        /* We use a static object to speedup things, however we assume\n         * that addReplyPubsubMessage() will not take a reference. */\n        addReplyPubsubMessage(c,TrackingChannelName,NULL);\n    } else {\n        /* If are here, the client is not using RESP3, nor is\n         * redirecting to another client. We can't send anything to\n         * it since RESP2 does not support push messages in the same\n         * connection. */\n        return;\n    }\n\n    /* Send the \"value\" part, which is the array of keys. */\n    if (proto) {\n        addReplyProto(c,keyname,keylen);\n    } else {\n        addReplyArrayLen(c,1);\n        addReplyBulkCBuffer(c,keyname,keylen);\n    }\n}\n\n/* This function is called when a key is modified in Redis and in the case\n * we have at least one client with the BCAST mode enabled.\n * Its goal is to set the key in the right broadcast state if the key\n * matches one or more prefixes in the prefix table. Later when we\n * return to the event loop, we'll send invalidation messages to the\n * clients subscribed to each prefix. */\nvoid trackingRememberKeyToBroadcast(client *c, char *keyname, size_t keylen) {\n    raxIterator ri;\n    raxStart(&ri,PrefixTable);\n    raxSeek(&ri,\"^\",NULL,0);\n    while(raxNext(&ri)) {\n        if (ri.key_len > keylen) continue;\n        if (ri.key_len != 0 && memcmp(ri.key,keyname,ri.key_len) != 0)\n            continue;\n        bcastState *bs = (bcastState*)ri.data;\n        /* We insert the client pointer as associated value in the radix\n         * tree. This way we know who was the client that did the last\n         * change to the key, and can avoid sending the notification in the\n         * case the client is in NOLOOP mode. */\n        raxInsert(bs->keys,(unsigned char*)keyname,keylen,c,NULL);\n    }\n    raxStop(&ri);\n}\n\n/* This function is called from signalModifiedKey() or other places in Redis\n * when a key changes value. In the context of keys tracking, our task here is\n * to send a notification to every client that may have keys about such caching\n * slot.\n *\n * Note that 'c' may be NULL in case the operation was performed outside the\n * context of a client modifying the database (for instance when we delete a\n * key because of expire).\n *\n * The last argument 'bcast' tells the function if it should also schedule\n * the key for broadcasting to clients in BCAST mode. This is the case when\n * the function is called from the Redis core once a key is modified, however\n * we also call the function in order to evict keys in the key table in case\n * of memory pressure: in that case the key didn't really change, so we want\n * just to notify the clients that are in the table for this key, that would\n * otherwise miss the fact we are no longer tracking the key for them. */\nvoid trackingInvalidateKeyRaw(client *c, char *key, size_t keylen, int bcast) {\n    if (TrackingTable == NULL) return;\n\n    if (bcast && raxSize(PrefixTable) > 0)\n        trackingRememberKeyToBroadcast(c,key,keylen);\n\n    rax *ids = (rax*)raxFind(TrackingTable,(unsigned char*)key,keylen);\n    if (ids == raxNotFound) return;\n\n    raxIterator ri;\n    raxStart(&ri,ids);\n    raxSeek(&ri,\"^\",NULL,0);\n    while(raxNext(&ri)) {\n        uint64_t id;\n        memcpy(&id,ri.key,sizeof(id));\n        client *target = lookupClientByID(id);\n        /* Note that if the client is in BCAST mode, we don't want to\n         * send invalidation messages that were pending in the case\n         * previously the client was not in BCAST mode. This can happen if\n         * TRACKING is enabled normally, and then the client switches to\n         * BCAST mode. */\n        if (target == NULL ||\n            !(target->flags & CLIENT_TRACKING)||\n            target->flags & CLIENT_TRACKING_BCAST)\n        {\n            continue;\n        }\n\n        /* If the client enabled the NOLOOP mode, don't send notifications\n         * about keys changed by the client itself. */\n        if (target->flags & CLIENT_TRACKING_NOLOOP &&\n            target == c)\n        {\n            continue;\n        }\n\n        sendTrackingMessage(target,key,keylen,0);\n    }\n    raxStop(&ri);\n\n    /* Free the tracking table: we'll create the radix tree and populate it\n     * again if more keys will be modified in this caching slot. */\n    TrackingTableTotalItems -= raxSize(ids);\n    raxFree(ids);\n    raxRemove(TrackingTable,(unsigned char*)key,keylen,NULL);\n}\n\n/* Wrapper (the one actually called across the core) to pass the key\n * as object. */\nvoid trackingInvalidateKey(client *c, robj *keyobj) {\n    trackingInvalidateKeyRaw(c,szFromObj(keyobj),sdslen(szFromObj(keyobj)),1);\n}\n\n/* This function is called when one or all the Redis databases are\n * flushed. Caching keys are not specific for each DB but are global: \n * currently what we do is send a special notification to clients with \n * tracking enabled, sending a RESP NULL, which means, \"all the keys\", \n * in order to avoid flooding clients with many invalidation messages \n * for all the keys they may hold.\n */\nvoid freeTrackingRadixTreeCallback(void *rt) {\n    raxFree((rax*)rt);\n}\n\nvoid freeTrackingRadixTree(rax *rt) {\n    raxFreeWithCallback(rt,freeTrackingRadixTreeCallback);\n}\n\n/* A RESP NULL is sent to indicate that all keys are invalid */\nvoid trackingInvalidateKeysOnFlush(int async) {\n    if (g_pserver->tracking_clients) {\n        listNode *ln;\n        listIter li;\n        listRewind(g_pserver->clients,&li);\n        while ((ln = listNext(&li)) != NULL) {\n            client *c = (client*)listNodeValue(ln);\n            if (c->flags & CLIENT_TRACKING) {\n                sendTrackingMessage(c,szFromObj(shared.null[c->resp]),sdslen(szFromObj(shared.null[c->resp])),1);\n            }\n        }\n    }\n\n    /* In case of FLUSHALL, reclaim all the memory used by tracking. */\n    if (TrackingTable) {\n        if (async) {\n            freeTrackingRadixTreeAsync(TrackingTable);\n        } else {\n            freeTrackingRadixTree(TrackingTable);\n        }\n        TrackingTable = raxNew();\n        TrackingTableTotalItems = 0;\n    }\n}\n\n/* Tracking forces Redis to remember information about which client may have\n * certain keys. In workloads where there are a lot of reads, but keys are\n * hardly modified, the amount of information we have to remember server side\n * could be a lot, with the number of keys being totally not bound.\n *\n * So Redis allows the user to configure a maximum number of keys for the\n * invalidation table. This function makes sure that we don't go over the\n * specified fill rate: if we are over, we can just evict informations about\n * a random key, and send invalidation messages to clients like if the key was\n * modified. */\nvoid trackingLimitUsedSlots(void) {\n    static unsigned int timeout_counter = 0;\n    if (TrackingTable == NULL) return;\n    if (g_pserver->tracking_table_max_keys == 0) return; /* No limits set. */\n    size_t max_keys = g_pserver->tracking_table_max_keys;\n    if (raxSize(TrackingTable) <= max_keys) {\n        timeout_counter = 0;\n        return; /* Limit not reached. */\n    }\n\n    /* We have to invalidate a few keys to reach the limit again. The effort\n     * we do here is proportional to the number of times we entered this\n     * function and found that we are still over the limit. */\n    int effort = 100 * (timeout_counter+1);\n\n    /* We just remove one key after another by using a random walk. */\n    raxIterator ri;\n    raxStart(&ri,TrackingTable);\n    while(effort > 0) {\n        effort--;\n        raxSeek(&ri,\"^\",NULL,0);\n        raxRandomWalk(&ri,0);\n        if (raxEOF(&ri)) break;\n        trackingInvalidateKeyRaw(NULL,(char*)ri.key,ri.key_len,0);\n        if (raxSize(TrackingTable) <= max_keys) {\n            timeout_counter = 0;\n            raxStop(&ri);\n            return; /* Return ASAP: we are again under the limit. */\n        }\n    }\n\n    /* If we reach this point, we were not able to go under the configured\n     * limit using the maximum effort we had for this run. */\n    raxStop(&ri);\n    timeout_counter++;\n}\n\n/* Generate Redis protocol for an array containing all the key names\n * in the 'keys' radix tree. If the client is not NULL, the list will not\n * include keys that were modified the last time by this client, in order\n * to implement the NOLOOP option.\n *\n * If the resultin array would be empty, NULL is returned instead. */\nsds trackingBuildBroadcastReply(client *c, rax *keys) {\n    raxIterator ri;\n    uint64_t count;\n\n    if (c == NULL) {\n        count = raxSize(keys);\n    } else {\n        count = 0;\n        raxStart(&ri,keys);\n        raxSeek(&ri,\"^\",NULL,0);\n        while(raxNext(&ri)) {\n            if (ri.data != c) count++;\n        }\n        raxStop(&ri);\n\n        if (count == 0) return NULL;\n    }\n\n    /* Create the array reply with the list of keys once, then send\n    * it to all the clients subscribed to this prefix. */\n    char buf[32];\n    size_t len = ll2string(buf,sizeof(buf),count);\n    sds proto = sdsempty();\n    proto = sdsMakeRoomFor(proto,count*15);\n    proto = sdscatlen(proto,\"*\",1);\n    proto = sdscatlen(proto,buf,len);\n    proto = sdscatlen(proto,\"\\r\\n\",2);\n    raxStart(&ri,keys);\n    raxSeek(&ri,\"^\",NULL,0);\n    while(raxNext(&ri)) {\n        if (c && ri.data == c) continue;\n        len = ll2string(buf,sizeof(buf),ri.key_len);\n        proto = sdscatlen(proto,\"$\",1);\n        proto = sdscatlen(proto,buf,len);\n        proto = sdscatlen(proto,\"\\r\\n\",2);\n        proto = sdscatlen(proto,ri.key,ri.key_len);\n        proto = sdscatlen(proto,\"\\r\\n\",2);\n    }\n    raxStop(&ri);\n    return proto;\n}\n\n/* This function will run the prefixes of clients in BCAST mode and\n * keys that were modified about each prefix, and will send the\n * notifications to each client in each prefix. */\nvoid trackingBroadcastInvalidationMessages(void) {\n    raxIterator ri, ri2;\n\n    /* Return ASAP if there is nothing to do here. */\n    if (TrackingTable == NULL || !g_pserver->tracking_clients) return;\n\n    raxStart(&ri,PrefixTable);\n    raxSeek(&ri,\"^\",NULL,0);\n\n    /* For each prefix... */\n    while(raxNext(&ri)) {\n        bcastState *bs = (bcastState*)ri.data;\n        \n        if (raxSize(bs->keys)) {\n            /* Generate the common protocol for all the clients that are\n             * not using the NOLOOP option. */\n            sds proto = trackingBuildBroadcastReply(NULL,bs->keys);\n\n            /* Send this array of keys to every client in the list. */\n            raxStart(&ri2,bs->clients);\n            raxSeek(&ri2,\"^\",NULL,0);\n            while(raxNext(&ri2)) {\n                client *c;\n                memcpy(&c,ri2.key,sizeof(c));\n                if (c->flags & CLIENT_TRACKING_NOLOOP) {\n                    /* This client may have certain keys excluded. */\n                    sds adhoc = trackingBuildBroadcastReply(c,bs->keys);\n                    if (adhoc) {\n                        sendTrackingMessage(c,adhoc,sdslen(adhoc),1);\n                        sdsfree(adhoc);\n                    }\n                } else {\n                    sendTrackingMessage(c,proto,sdslen(proto),1);\n                }\n            }\n            raxStop(&ri2);\n\n            /* Clean up: we can remove everything from this state, because we\n             * want to only track the new keys that will be accumulated starting\n             * from now. */\n            sdsfree(proto);\n        }\n        raxFree(bs->keys);\n        bs->keys = raxNew();\n    }\n    raxStop(&ri);\n}\n\n/* This is just used in order to access the amount of used slots in the\n * tracking table. */\nuint64_t trackingGetTotalItems(void) {\n    return TrackingTableTotalItems;\n}\n\nuint64_t trackingGetTotalKeys(void) {\n    if (TrackingTable == NULL) return 0;\n    return raxSize(TrackingTable);\n}\n\nuint64_t trackingGetTotalPrefixes(void) {\n    if (PrefixTable == NULL) return 0;\n    return raxSize(PrefixTable);\n}\n"
  },
  {
    "path": "src/util.c",
    "content": "/*\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include \"fmacros.h\"\n#include <stdlib.h>\n#include <stdio.h>\n#include <string.h>\n#include <ctype.h>\n#include <limits.h>\n#include <math.h>\n#include <unistd.h>\n#include <sys/time.h>\n#include <float.h>\n#include <stdint.h>\n#include <errno.h>\n#include <time.h>\n\n#include \"util.h\"\n#include \"sha256.h\"\n\n/* Glob-style pattern matching. */\nint stringmatchlen(const char *pattern, int patternLen,\n        const char *string, int stringLen, int nocase)\n{\n    while(patternLen && stringLen) {\n        switch(pattern[0]) {\n        case '*':\n            while (patternLen && pattern[1] == '*') {\n                pattern++;\n                patternLen--;\n            }\n            if (patternLen == 1)\n                return 1; /* match */\n            while(stringLen) {\n                if (stringmatchlen(pattern+1, patternLen-1,\n                            string, stringLen, nocase))\n                    return 1; /* match */\n                string++;\n                stringLen--;\n            }\n            return 0; /* no match */\n            break;\n        case '?':\n            string++;\n            stringLen--;\n            break;\n        case '[':\n        {\n            int not, match;\n\n            pattern++;\n            patternLen--;\n            not = pattern[0] == '^';\n            if (not) {\n                pattern++;\n                patternLen--;\n            }\n            match = 0;\n            while(1) {\n                if (pattern[0] == '\\\\' && patternLen >= 2) {\n                    pattern++;\n                    patternLen--;\n                    if (pattern[0] == string[0])\n                        match = 1;\n                } else if (pattern[0] == ']') {\n                    break;\n                } else if (patternLen == 0) {\n                    pattern--;\n                    patternLen++;\n                    break;\n                } else if (patternLen >= 3 && pattern[1] == '-') {\n                    int start = pattern[0];\n                    int end = pattern[2];\n                    int c = string[0];\n                    if (start > end) {\n                        int t = start;\n                        start = end;\n                        end = t;\n                    }\n                    if (nocase) {\n                        start = tolower(start);\n                        end = tolower(end);\n                        c = tolower(c);\n                    }\n                    pattern += 2;\n                    patternLen -= 2;\n                    if (c >= start && c <= end)\n                        match = 1;\n                } else {\n                    if (!nocase) {\n                        if (pattern[0] == string[0])\n                            match = 1;\n                    } else {\n                        if (tolower((int)pattern[0]) == tolower((int)string[0]))\n                            match = 1;\n                    }\n                }\n                pattern++;\n                patternLen--;\n            }\n            if (not)\n                match = !match;\n            if (!match)\n                return 0; /* no match */\n            string++;\n            stringLen--;\n            break;\n        }\n        case '\\\\':\n            if (patternLen >= 2) {\n                pattern++;\n                patternLen--;\n            }\n            /* fall through */\n        default:\n            if (!nocase) {\n                if (pattern[0] != string[0])\n                    return 0; /* no match */\n            } else {\n                if (tolower((int)pattern[0]) != tolower((int)string[0]))\n                    return 0; /* no match */\n            }\n            string++;\n            stringLen--;\n            break;\n        }\n        pattern++;\n        patternLen--;\n        if (stringLen == 0) {\n            while(*pattern == '*') {\n                pattern++;\n                patternLen--;\n            }\n            break;\n        }\n    }\n    if (patternLen == 0 && stringLen == 0)\n        return 1;\n    return 0;\n}\n\nint stringmatch(const char *pattern, const char *string, int nocase) {\n    return stringmatchlen(pattern,strlen(pattern),string,strlen(string),nocase);\n}\n\n/* Fuzz stringmatchlen() trying to crash it with bad input. */\nint stringmatchlen_fuzz_test(void) {\n    char str[32];\n    char pat[32];\n    int cycles = 10000000;\n    int total_matches = 0;\n    while(cycles--) {\n        int strlen = rand() % sizeof(str);\n        int patlen = rand() % sizeof(pat);\n        for (int j = 0; j < strlen; j++) str[j] = rand() % 128;\n        for (int j = 0; j < patlen; j++) pat[j] = rand() % 128;\n        total_matches += stringmatchlen(pat, patlen, str, strlen, 0);\n    }\n    return total_matches;\n}\n\n/* Convert a string representing an amount of memory into the number of\n * bytes, so for instance memtoll(\"1Gb\") will return 1073741824 that is\n * (1024*1024*1024).\n *\n * On parsing error, if *err is not NULL, it's set to 1, otherwise it's\n * set to 0. On error the function return value is 0, regardless of the\n * fact 'err' is NULL or not. */\nlong long memtoll(const char *p, int *err) {\n    const char *u;\n    char buf[128];\n    long mul; /* unit multiplier */\n    long long val;\n    unsigned int digits;\n\n    if (err) *err = 0;\n\n    /* Search the first non digit character. */\n    u = p;\n    if (*u == '-') u++;\n    while(*u && isdigit(*u)) u++;\n    if (*u == '\\0' || !strcasecmp(u,\"b\")) {\n        mul = 1;\n    } else if (!strcasecmp(u,\"k\")) {\n        mul = 1000;\n    } else if (!strcasecmp(u,\"kb\")) {\n        mul = 1024;\n    } else if (!strcasecmp(u,\"m\")) {\n        mul = 1000*1000;\n    } else if (!strcasecmp(u,\"mb\")) {\n        mul = 1024*1024;\n    } else if (!strcasecmp(u,\"g\")) {\n        mul = 1000L*1000*1000;\n    } else if (!strcasecmp(u,\"gb\")) {\n        mul = 1024L*1024*1024;\n    } else {\n        if (err) *err = 1;\n        return 0;\n    }\n\n    /* Copy the digits into a buffer, we'll use strtoll() to convert\n     * the digit (without the unit) into a number. */\n    digits = u-p;\n    if (digits >= sizeof(buf)) {\n        if (err) *err = 1;\n        return 0;\n    }\n    memcpy(buf,p,digits);\n    buf[digits] = '\\0';\n\n    char *endptr;\n    errno = 0;\n    val = strtoll(buf,&endptr,10);\n    if ((val == 0 && errno == EINVAL) || *endptr != '\\0') {\n        if (err) *err = 1;\n        return 0;\n    }\n    return val*mul;\n}\n\n/* Search a memory buffer for any set of bytes, like strpbrk().\n * Returns pointer to first found char or NULL.\n */\nconst char *mempbrk(const char *s, size_t len, const char *chars, size_t charslen) {\n    for (size_t j = 0; j < len; j++) {\n        for (size_t n = 0; n < charslen; n++)\n            if (s[j] == chars[n]) return &s[j];\n    }\n\n    return NULL;\n}\n\n/* Modify the buffer replacing all occurrences of chars from the 'from'\n * set with the corresponding char in the 'to' set. Always returns s.\n */\nchar *memmapchars(char *s, size_t len, const char *from, const char *to, size_t setlen) {\n    for (size_t j = 0; j < len; j++) {\n        for (size_t i = 0; i < setlen; i++) {\n            if (s[j] == from[i]) {\n                s[j] = to[i];\n                break;\n            }\n        }\n    }\n    return s;\n}\n\n/* Return the number of digits of 'v' when converted to string in radix 10.\n * See ll2string() for more information. */\nuint32_t digits10(uint64_t v) {\n    if (v < 10) return 1;\n    if (v < 100) return 2;\n    if (v < 1000) return 3;\n    if (v < 1000000000000UL) {\n        if (v < 100000000UL) {\n            if (v < 1000000) {\n                if (v < 10000) return 4;\n                return 5 + (v >= 100000);\n            }\n            return 7 + (v >= 10000000UL);\n        }\n        if (v < 10000000000UL) {\n            return 9 + (v >= 1000000000UL);\n        }\n        return 11 + (v >= 100000000000UL);\n    }\n    return 12 + digits10(v / 1000000000000UL);\n}\n\n/* Like digits10() but for signed values. */\nuint32_t sdigits10(int64_t v) {\n    if (v < 0) {\n        /* Abs value of LLONG_MIN requires special handling. */\n        uint64_t uv = (v != LLONG_MIN) ?\n                      (uint64_t)-v : ((uint64_t) LLONG_MAX)+1;\n        return digits10(uv)+1; /* +1 for the minus. */\n    } else {\n        return digits10(v);\n    }\n}\n\n/* Convert a long long into a string. Returns the number of\n * characters needed to represent the number.\n * If the buffer is not big enough to store the string, 0 is returned.\n *\n * Based on the following article (that apparently does not provide a\n * novel approach but only publicizes an already used technique):\n *\n * https://www.facebook.com/notes/facebook-engineering/three-optimization-tips-for-c/10151361643253920\n *\n * Modified in order to handle signed integers since the original code was\n * designed for unsigned integers. */\nint ll2string(char *dst, size_t dstlen, long long svalue) {\n    static const char digits[201] =\n        \"0001020304050607080910111213141516171819\"\n        \"2021222324252627282930313233343536373839\"\n        \"4041424344454647484950515253545556575859\"\n        \"6061626364656667686970717273747576777879\"\n        \"8081828384858687888990919293949596979899\";\n    int negative;\n    unsigned long long value;\n\n    /* The main loop works with 64bit unsigned integers for simplicity, so\n     * we convert the number here and remember if it is negative. */\n    if (svalue < 0) {\n        if (svalue != LLONG_MIN) {\n            value = -svalue;\n        } else {\n            value = ((unsigned long long) LLONG_MAX)+1;\n        }\n        negative = 1;\n    } else {\n        value = svalue;\n        negative = 0;\n    }\n\n    /* Check length. */\n    uint32_t const length = digits10(value)+negative;\n    if (length >= dstlen) return 0;\n\n    /* Null term. */\n    uint32_t next = length;\n    dst[next] = '\\0';\n    next--;\n    while (value >= 100) {\n        int const i = (value % 100) * 2;\n        value /= 100;\n        dst[next] = digits[i + 1];\n        dst[next - 1] = digits[i];\n        next -= 2;\n    }\n\n    /* Handle last 1-2 digits. */\n    if (value < 10) {\n        dst[next] = '0' + (uint32_t) value;\n    } else {\n        int i = (uint32_t) value * 2;\n        dst[next] = digits[i + 1];\n        dst[next - 1] = digits[i];\n    }\n\n    /* Add sign. */\n    if (negative) dst[0] = '-';\n    return length;\n}\n\n/* Convert a string into a long long. Returns 1 if the string could be parsed\n * into a (non-overflowing) long long, 0 otherwise. The value will be set to\n * the parsed value when appropriate.\n *\n * Note that this function demands that the string strictly represents\n * a long long: no spaces or other characters before or after the string\n * representing the number are accepted, nor zeroes at the start if not\n * for the string \"0\" representing the zero number.\n *\n * Because of its strictness, it is safe to use this function to check if\n * you can convert a string into a long long, and obtain back the string\n * from the number without any loss in the string representation. */\nint string2ll(const char *s, size_t slen, long long *value) {\n    const char *p = s;\n    size_t plen = 0;\n    int negative = 0;\n    unsigned long long v;\n\n    /* A zero length string is not a valid number. */\n    if (plen == slen)\n        return 0;\n\n    /* Special case: first and only digit is 0. */\n    if (slen == 1 && p[0] == '0') {\n        if (value != NULL) *value = 0;\n        return 1;\n    }\n\n    /* Handle negative numbers: just set a flag and continue like if it\n     * was a positive number. Later convert into negative. */\n    if (p[0] == '-') {\n        negative = 1;\n        p++; plen++;\n\n        /* Abort on only a negative sign. */\n        if (plen == slen)\n            return 0;\n    }\n\n    /* First digit should be 1-9, otherwise the string should just be 0. */\n    if (p[0] >= '1' && p[0] <= '9') {\n        v = p[0]-'0';\n        p++; plen++;\n    } else {\n        return 0;\n    }\n\n    /* Parse all the other digits, checking for overflow at every step. */\n    while (plen < slen && p[0] >= '0' && p[0] <= '9') {\n        if (v > (ULLONG_MAX / 10)) /* Overflow. */\n            return 0;\n        v *= 10;\n\n        if (v > (ULLONG_MAX - (p[0]-'0'))) /* Overflow. */\n            return 0;\n        v += p[0]-'0';\n\n        p++; plen++;\n    }\n\n    /* Return if not all bytes were used. */\n    if (plen < slen)\n        return 0;\n\n    /* Convert to negative if needed, and do the final overflow check when\n     * converting from unsigned long long to long long. */\n    if (negative) {\n        if (v > ((unsigned long long)(-(LLONG_MIN+1))+1)) /* Overflow. */\n            return 0;\n        if (value != NULL) *value = -v;\n    } else {\n        if (v > LLONG_MAX) /* Overflow. */\n            return 0;\n        if (value != NULL) *value = v;\n    }\n    return 1;\n}\n\n/* Helper function to convert a string to an unsigned long long value.\n * The function attempts to use the faster string2ll() function inside\n * Redis: if it fails, strtoull() is used instead. The function returns\n * 1 if the conversion happened successfully or 0 if the number is\n * invalid or out of range. */\nint string2ull(const char *s, unsigned long long *value) {\n    long long ll;\n    if (string2ll(s,strlen(s),&ll)) {\n        if (ll < 0) return 0; /* Negative values are out of range. */\n        *value = ll;\n        return 1;\n    }\n    errno = 0;\n    char *endptr = NULL;\n    *value = strtoull(s,&endptr,10);\n    if (errno == EINVAL || errno == ERANGE || !(*s != '\\0' && *endptr == '\\0'))\n        return 0; /* strtoull() failed. */\n    return 1; /* Conversion done! */\n}\n\n/* Convert a string into a long. Returns 1 if the string could be parsed into a\n * (non-overflowing) long, 0 otherwise. The value will be set to the parsed\n * value when appropriate. */\nint string2l(const char *s, size_t slen, long *lval) {\n    long long llval;\n\n    if (!string2ll(s,slen,&llval))\n        return 0;\n\n    if (llval < LONG_MIN || llval > LONG_MAX)\n        return 0;\n\n    *lval = (long)llval;\n    return 1;\n}\n\n/* Convert a string into a double. Returns 1 if the string could be parsed\n * into a (non-overflowing) double, 0 otherwise. The value will be set to\n * the parsed value when appropriate.\n *\n * Note that this function demands that the string strictly represents\n * a double: no spaces or other characters before or after the string\n * representing the number are accepted. */\nint string2ld(const char *s, size_t slen, long double *dp) {\n    char buf[MAX_LONG_DOUBLE_CHARS];\n    long double value;\n    char *eptr;\n\n    if (slen == 0 || slen >= sizeof(buf)) return 0;\n    memcpy(buf,s,slen);\n    buf[slen] = '\\0';\n\n    errno = 0;\n    value = strtold(buf, &eptr);\n    if (isspace(buf[0]) || eptr[0] != '\\0' ||\n        (size_t)(eptr-buf) != slen ||\n        (errno == ERANGE &&\n            (value == HUGE_VAL || value == -HUGE_VAL || value == 0)) ||\n        errno == EINVAL ||\n        isnan(value))\n        return 0;\n\n    if (dp) *dp = value;\n    return 1;\n}\n\n/* Convert a string into a double. Returns 1 if the string could be parsed\n * into a (non-overflowing) double, 0 otherwise. The value will be set to\n * the parsed value when appropriate.\n *\n * Note that this function demands that the string strictly represents\n * a double: no spaces or other characters before or after the string\n * representing the number are accepted. */\nint string2d(const char *s, size_t slen, double *dp) {\n    errno = 0;\n    char *eptr;\n    *dp = strtod(s, &eptr);\n    if (slen == 0 ||\n        isspace(((const char*)s)[0]) ||\n        (size_t)(eptr-(char*)s) != slen ||\n        (errno == ERANGE &&\n            (*dp == HUGE_VAL || *dp == -HUGE_VAL || *dp == 0)) ||\n        isnan(*dp))\n        return 0;\n    return 1;\n}\n\n/* Convert a double to a string representation. Returns the number of bytes\n * required. The representation should always be parsable by strtod(3).\n * This function does not support human-friendly formatting like ld2string\n * does. It is intended mainly to be used inside t_zset.c when writing scores\n * into a ziplist representing a sorted set. */\nint d2string(char *buf, size_t len, double value) {\n    if (isnan(value)) {\n        len = snprintf(buf,len,\"nan\");\n    } else if (isinf(value)) {\n        if (value < 0)\n            len = snprintf(buf,len,\"-inf\");\n        else\n            len = snprintf(buf,len,\"inf\");\n    } else if (value == 0) {\n        /* See: http://en.wikipedia.org/wiki/Signed_zero, \"Comparisons\". */\n        if (1.0/value < 0)\n            len = snprintf(buf,len,\"-0\");\n        else\n            len = snprintf(buf,len,\"0\");\n    } else {\n#if (DBL_MANT_DIG >= 52) && (LLONG_MAX == 0x7fffffffffffffffLL)\n        /* Check if the float is in a safe range to be casted into a\n         * long long. We are assuming that long long is 64 bit here.\n         * Also we are assuming that there are no implementations around where\n         * double has precision < 52 bit.\n         *\n         * Under this assumptions we test if a double is inside an interval\n         * where casting to long long is safe. Then using two castings we\n         * make sure the decimal part is zero. If all this is true we use\n         * integer printing function that is much faster. */\n        double min = -4503599627370495; /* (2^52)-1 */\n        double max = 4503599627370496; /* -(2^52) */\n        if (value > min && value < max && value == ((double)((long long)value)))\n            len = ll2string(buf,len,(long long)value);\n        else\n#endif\n            len = snprintf(buf,len,\"%.17g\",value);\n    }\n\n    return len;\n}\n\n/* Create a string object from a long double.\n * If mode is humanfriendly it does not use exponential format and trims trailing\n * zeroes at the end (may result in loss of precision).\n * If mode is default exp format is used and the output of snprintf()\n * is not modified (may result in loss of precision).\n * If mode is hex hexadecimal format is used (no loss of precision)\n *\n * The function returns the length of the string or zero if there was not\n * enough buffer room to store it. */\nint ld2string(char *buf, size_t len, long double value, ld2string_mode mode) {\n    size_t l = 0;\n\n    if (isinf(value)) {\n        /* Libc in odd systems (Hi Solaris!) will format infinite in a\n         * different way, so better to handle it in an explicit way. */\n        if (len < 5) return 0; /* No room. 5 is \"-inf\\0\" */\n        if (value > 0) {\n            memcpy(buf,\"inf\",3);\n            l = 3;\n        } else {\n            memcpy(buf,\"-inf\",4);\n            l = 4;\n        }\n    } else {\n        switch (mode) {\n        case LD_STR_AUTO:\n            l = snprintf(buf,len,\"%.17Lg\",value);\n            if (l+1 > len) return 0; /* No room. */\n            break;\n        case LD_STR_HEX:\n            l = snprintf(buf,len,\"%La\",value);\n            if (l+1 > len) return 0; /* No room. */\n            break;\n        case LD_STR_HUMAN:\n            /* We use 17 digits precision since with 128 bit floats that precision\n             * after rounding is able to represent most small decimal numbers in a\n             * way that is \"non surprising\" for the user (that is, most small\n             * decimal numbers will be represented in a way that when converted\n             * back into a string are exactly the same as what the user typed.) */\n            l = snprintf(buf,len,\"%.17Lf\",value);\n            if (l+1 > len) return 0; /* No room. */\n            /* Now remove trailing zeroes after the '.' */\n            if (strchr(buf,'.') != NULL) {\n                char *p = buf+l-1;\n                while(*p == '0') {\n                    p--;\n                    l--;\n                }\n                if (*p == '.') l--;\n            }\n            if (l == 2 && buf[0] == '-' && buf[1] == '0') {\n                buf[0] = '0';\n                l = 1;\n            }\n            break;\n        default: return 0; /* Invalid mode. */\n        }\n    }\n    buf[l] = '\\0';\n    return l;\n}\n\n/* Get random bytes, attempts to get an initial seed from /dev/urandom and\n * the uses a one way hash function in counter mode to generate a random\n * stream. However if /dev/urandom is not available, a weaker seed is used.\n *\n * This function is not thread safe, since the state is global. */\nvoid getRandomBytes(unsigned char *p, size_t len) {\n    /* Global state. */\n    static int seed_initialized = 0;\n    static unsigned char seed[64]; /* 512 bit internal block size. */\n    static uint64_t counter = 0; /* The counter we hash with the seed. */\n\n    if (!seed_initialized) {\n        /* Initialize a seed and use SHA1 in counter mode, where we hash\n         * the same seed with a progressive counter. For the goals of this\n         * function we just need non-colliding strings, there are no\n         * cryptographic security needs. */\n        FILE *fp = fopen(\"/dev/urandom\",\"r\");\n        if (fp == NULL || fread(seed,sizeof(seed),1,fp) != 1) {\n            /* Revert to a weaker seed, and in this case reseed again\n             * at every call.*/\n            for (unsigned int j = 0; j < sizeof(seed); j++) {\n                struct timeval tv;\n                gettimeofday(&tv,NULL);\n                pid_t pid = getpid();\n                seed[j] = tv.tv_sec ^ tv.tv_usec ^ pid ^ (long)fp;\n            }\n        } else {\n            seed_initialized = 1;\n        }\n        if (fp) fclose(fp);\n    }\n\n    while(len) {\n        /* This implements SHA256-HMAC. */\n        unsigned char digest[SHA256_BLOCK_SIZE];\n        unsigned char kxor[64];\n        unsigned int copylen =\n            len > SHA256_BLOCK_SIZE ? SHA256_BLOCK_SIZE : len;\n\n        /* IKEY: key xored with 0x36. */\n        memcpy(kxor,seed,sizeof(kxor));\n        for (unsigned int i = 0; i < sizeof(kxor); i++) kxor[i] ^= 0x36;\n\n        /* Obtain HASH(IKEY||MESSAGE). */\n        SHA256_CTX ctx;\n        sha256_init(&ctx);\n        sha256_update(&ctx,kxor,sizeof(kxor));\n        sha256_update(&ctx,(unsigned char*)&counter,sizeof(counter));\n        sha256_final(&ctx,digest);\n\n        /* OKEY: key xored with 0x5c. */\n        memcpy(kxor,seed,sizeof(kxor));\n        for (unsigned int i = 0; i < sizeof(kxor); i++) kxor[i] ^= 0x5C;\n\n        /* Obtain HASH(OKEY || HASH(IKEY||MESSAGE)). */\n        sha256_init(&ctx);\n        sha256_update(&ctx,kxor,sizeof(kxor));\n        sha256_update(&ctx,digest,SHA256_BLOCK_SIZE);\n        sha256_final(&ctx,digest);\n\n        /* Increment the counter for the next iteration. */\n        counter++;\n\n        memcpy(p,digest,copylen);\n        len -= copylen;\n        p += copylen;\n    }\n}\n\n/* Generate the Redis \"Run ID\", a SHA1-sized random number that identifies a\n * given execution of Redis, so that if you are talking with an instance\n * having run_id == A, and you reconnect and it has run_id == B, you can be\n * sure that it is either a different instance or it was restarted. */\nvoid getRandomHexChars(char *p, size_t len) {\n    char *charset = \"0123456789abcdef\";\n    size_t j;\n\n    getRandomBytes((unsigned char*)p,len);\n    for (j = 0; j < len; j++) p[j] = charset[p[j] & 0x0F];\n}\n\n/* Given the filename, return the absolute path as an SDS string, or NULL\n * if it fails for some reason. Note that \"filename\" may be an absolute path\n * already, this will be detected and handled correctly.\n *\n * The function does not try to normalize everything, but only the obvious\n * case of one or more \"../\" appearing at the start of \"filename\"\n * relative path. */\nsds getAbsolutePath(char *filename) {\n    char cwd[1024];\n    sds abspath;\n    sds relpath = sdsnew(filename);\n\n    relpath = sdstrim(relpath,\" \\r\\n\\t\");\n    if (relpath[0] == '/') return relpath; /* Path is already absolute. */\n\n    /* If path is relative, join cwd and relative path. */\n    if (getcwd(cwd,sizeof(cwd)) == NULL) {\n        sdsfree(relpath);\n        return NULL;\n    }\n    abspath = sdsnew(cwd);\n    if (sdslen(abspath) && abspath[sdslen(abspath)-1] != '/')\n        abspath = sdscat(abspath,\"/\");\n\n    /* At this point we have the current path always ending with \"/\", and\n     * the trimmed relative path. Try to normalize the obvious case of\n     * trailing ../ elements at the start of the path.\n     *\n     * For every \"../\" we find in the filename, we remove it and also remove\n     * the last element of the cwd, unless the current cwd is \"/\". */\n    while (sdslen(relpath) >= 3 &&\n           relpath[0] == '.' && relpath[1] == '.' && relpath[2] == '/')\n    {\n        sdsrange(relpath,3,-1);\n        if (sdslen(abspath) > 1) {\n            char *p = abspath + sdslen(abspath)-2;\n            int trimlen = 1;\n\n            while(*p != '/') {\n                p--;\n                trimlen++;\n            }\n            sdsrange(abspath,0,-(trimlen+1));\n        }\n    }\n\n    /* Finally glue the two parts together. */\n    abspath = sdscatsds(abspath,relpath);\n    sdsfree(relpath);\n    return abspath;\n}\n\n/*\n * Gets the proper timezone in a more portable fashion\n * i.e timezone variables are linux specific.\n */\nlong getTimeZone(void) {\n#if defined(__linux__) || defined(__sun)\n    return timezone;\n#else\n    struct timeval tv;\n    struct timezone tz;\n\n    gettimeofday(&tv, &tz);\n\n    return tz.tz_minuteswest * 60L;\n#endif\n}\n\n/* Return true if the specified path is just a file basename without any\n * relative or absolute path. This function just checks that no / or \\\n * character exists inside the specified path, that's enough in the\n * environments where Redis runs. */\nint pathIsBaseName(char *path) {\n    return strchr(path,'/') == NULL && strchr(path,'\\\\') == NULL;\n}\n\n#ifdef REDIS_TEST\n#include <assert.h>\n\nstatic void test_string2ll(void) {\n    char buf[32];\n    long long v;\n\n    /* May not start with +. */\n    strcpy(buf,\"+1\");\n    assert(string2ll(buf,strlen(buf),&v) == 0);\n\n    /* Leading space. */\n    strcpy(buf,\" 1\");\n    assert(string2ll(buf,strlen(buf),&v) == 0);\n\n    /* Trailing space. */\n    strcpy(buf,\"1 \");\n    assert(string2ll(buf,strlen(buf),&v) == 0);\n\n    /* May not start with 0. */\n    strcpy(buf,\"01\");\n    assert(string2ll(buf,strlen(buf),&v) == 0);\n\n    strcpy(buf,\"-1\");\n    assert(string2ll(buf,strlen(buf),&v) == 1);\n    assert(v == -1);\n\n    strcpy(buf,\"0\");\n    assert(string2ll(buf,strlen(buf),&v) == 1);\n    assert(v == 0);\n\n    strcpy(buf,\"1\");\n    assert(string2ll(buf,strlen(buf),&v) == 1);\n    assert(v == 1);\n\n    strcpy(buf,\"99\");\n    assert(string2ll(buf,strlen(buf),&v) == 1);\n    assert(v == 99);\n\n    strcpy(buf,\"-99\");\n    assert(string2ll(buf,strlen(buf),&v) == 1);\n    assert(v == -99);\n\n    strcpy(buf,\"-9223372036854775808\");\n    assert(string2ll(buf,strlen(buf),&v) == 1);\n    assert(v == LLONG_MIN);\n\n    strcpy(buf,\"-9223372036854775809\"); /* overflow */\n    assert(string2ll(buf,strlen(buf),&v) == 0);\n\n    strcpy(buf,\"9223372036854775807\");\n    assert(string2ll(buf,strlen(buf),&v) == 1);\n    assert(v == LLONG_MAX);\n\n    strcpy(buf,\"9223372036854775808\"); /* overflow */\n    assert(string2ll(buf,strlen(buf),&v) == 0);\n}\n\nstatic void test_string2l(void) {\n    char buf[32];\n    long v;\n\n    /* May not start with +. */\n    strcpy(buf,\"+1\");\n    assert(string2l(buf,strlen(buf),&v) == 0);\n\n    /* May not start with 0. */\n    strcpy(buf,\"01\");\n    assert(string2l(buf,strlen(buf),&v) == 0);\n\n    strcpy(buf,\"-1\");\n    assert(string2l(buf,strlen(buf),&v) == 1);\n    assert(v == -1);\n\n    strcpy(buf,\"0\");\n    assert(string2l(buf,strlen(buf),&v) == 1);\n    assert(v == 0);\n\n    strcpy(buf,\"1\");\n    assert(string2l(buf,strlen(buf),&v) == 1);\n    assert(v == 1);\n\n    strcpy(buf,\"99\");\n    assert(string2l(buf,strlen(buf),&v) == 1);\n    assert(v == 99);\n\n    strcpy(buf,\"-99\");\n    assert(string2l(buf,strlen(buf),&v) == 1);\n    assert(v == -99);\n\n#if LONG_MAX != LLONG_MAX\n    strcpy(buf,\"-2147483648\");\n    assert(string2l(buf,strlen(buf),&v) == 1);\n    assert(v == LONG_MIN);\n\n    strcpy(buf,\"-2147483649\"); /* overflow */\n    assert(string2l(buf,strlen(buf),&v) == 0);\n\n    strcpy(buf,\"2147483647\");\n    assert(string2l(buf,strlen(buf),&v) == 1);\n    assert(v == LONG_MAX);\n\n    strcpy(buf,\"2147483648\"); /* overflow */\n    assert(string2l(buf,strlen(buf),&v) == 0);\n#endif\n}\n\nstatic void test_ll2string(void) {\n    char buf[32];\n    long long v;\n    int sz;\n\n    v = 0;\n    sz = ll2string(buf, sizeof buf, v);\n    assert(sz == 1);\n    assert(!strcmp(buf, \"0\"));\n\n    v = -1;\n    sz = ll2string(buf, sizeof buf, v);\n    assert(sz == 2);\n    assert(!strcmp(buf, \"-1\"));\n\n    v = 99;\n    sz = ll2string(buf, sizeof buf, v);\n    assert(sz == 2);\n    assert(!strcmp(buf, \"99\"));\n\n    v = -99;\n    sz = ll2string(buf, sizeof buf, v);\n    assert(sz == 3);\n    assert(!strcmp(buf, \"-99\"));\n\n    v = -2147483648;\n    sz = ll2string(buf, sizeof buf, v);\n    assert(sz == 11);\n    assert(!strcmp(buf, \"-2147483648\"));\n\n    v = LLONG_MIN;\n    sz = ll2string(buf, sizeof buf, v);\n    assert(sz == 20);\n    assert(!strcmp(buf, \"-9223372036854775808\"));\n\n    v = LLONG_MAX;\n    sz = ll2string(buf, sizeof buf, v);\n    assert(sz == 19);\n    assert(!strcmp(buf, \"9223372036854775807\"));\n}\n\n#define UNUSED(x) (void)(x)\nint utilTest(int argc, char **argv, int accurate) {\n    UNUSED(argc);\n    UNUSED(argv);\n    UNUSED(accurate);\n\n    test_string2ll();\n    test_string2l();\n    test_ll2string();\n    return 0;\n}\n#endif\n"
  },
  {
    "path": "src/util.h",
    "content": "/*\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __REDIS_UTIL_H\n#define __REDIS_UTIL_H\n\n#include <stdint.h>\n#include \"sds.h\"\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/* The maximum number of characters needed to represent a long double\n * as a string (long double has a huge range).\n * This should be the size of the buffer given to ld2string */\n#define MAX_LONG_DOUBLE_CHARS 5*1024\n\n/* long double to string convertion options */\ntypedef enum {\n    LD_STR_AUTO,     /* %.17Lg */\n    LD_STR_HUMAN,    /* %.17Lf + Trimming of trailing zeros */\n    LD_STR_HEX       /* %La */\n} ld2string_mode;\n\nint stringmatchlen(const char *p, int plen, const char *s, int slen, int nocase);\nint stringmatch(const char *p, const char *s, int nocase);\nint stringmatchlen_fuzz_test(void);\nlong long memtoll(const char *p, int *err);\nconst char *mempbrk(const char *s, size_t len, const char *chars, size_t charslen);\nchar *memmapchars(char *s, size_t len, const char *from, const char *to, size_t setlen);\nuint32_t digits10(uint64_t v);\nuint32_t sdigits10(int64_t v);\nint ll2string(char *s, size_t len, long long value);\nint string2ll(const char *s, size_t slen, long long *value);\nint string2ull(const char *s, unsigned long long *value);\nint string2l(const char *s, size_t slen, long *value);\nint string2ld(const char *s, size_t slen, long double *dp);\nint string2d(const char *s, size_t slen, double *dp);\nint d2string(char *buf, size_t len, double value);\nint ld2string(char *buf, size_t len, long double value, ld2string_mode mode);\nsds getAbsolutePath(char *filename);\nlong getTimeZone(void);\nint pathIsBaseName(char *path);\n\n#ifdef REDIS_TEST\nint utilTest(int argc, char **argv, int accurate);\n#endif\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n"
  },
  {
    "path": "src/uuid.h",
    "content": "#pragma once\n\n#define UUID_BINARY_LEN 16\n\nstatic inline int FUuidNil(unsigned char *uuid)\n{\n    unsigned char val = 0;\n    for (int i = 0; i < UUID_BINARY_LEN; ++i)\n        val |= uuid[i];\n    return (val == 0);\n}\n\nstatic inline int FUuidEqual(unsigned char *uuid1, unsigned char *uuid2)\n{\n    unsigned char val = 0;\n    for (int i = 0; i < UUID_BINARY_LEN; ++i)\n        val |= (uuid1[i] ^ uuid2[i]);\n    return (val == 0);\n}"
  },
  {
    "path": "src/valgrind.sup",
    "content": "{\n   <lzf_uninitialized_hash_table>\n   Memcheck:Cond\n   fun:lzf_compress\n}\n\n{\n   <lzf_uninitialized_hash_table>\n   Memcheck:Value4\n   fun:lzf_compress\n}\n\n{\n   <lzf_uninitialized_hash_table>\n   Memcheck:Value8\n   fun:lzf_compress\n}\n\n{\n   <negative size allocatoin, see integration/corrupt-dump>\n   Memcheck:FishyValue\n   malloc(size)\n   fun:malloc\n   fun:ztrymalloc_usable\n   fun:ztrymalloc\n}\n"
  },
  {
    "path": "src/version.h",
    "content": "#define KEYDB_REAL_VERSION \"255.255.255\"\n#define KEYDB_VERSION_NUM 0x00ffffff\nextern const char *KEYDB_SET_VERSION;   // Unlike real version, this can be overriden by the config\n\nenum VersionCompareResult\n{\n    EqualVersion,\n    OlderVersion,\n    NewerVersion,\n    IncompatibleVersion,\n};\n\nstruct SymVer\n{\n    long major;\n    long minor;\n    long build;\n};\n\n#ifdef __cplusplus\nextern \"C\"\n{\n#endif\n\nstruct SymVer parseVersion(const char *version);\nenum VersionCompareResult compareVersion(struct SymVer *pver);\n\n#ifdef __cplusplus\n}\n#endif\n"
  },
  {
    "path": "src/ziplist.c",
    "content": "/* The ziplist is a specially encoded dually linked list that is designed\n * to be very memory efficient. It stores both strings and integer values,\n * where integers are encoded as actual integers instead of a series of\n * characters. It allows push and pop operations on either side of the list\n * in O(1) time. However, because every operation requires a reallocation of\n * the memory used by the ziplist, the actual complexity is related to the\n * amount of memory used by the ziplist.\n *\n * ----------------------------------------------------------------------------\n *\n * ZIPLIST OVERALL LAYOUT\n * ======================\n *\n * The general layout of the ziplist is as follows:\n *\n * <zlbytes> <zltail> <zllen> <entry> <entry> ... <entry> <zlend>\n *\n * NOTE: all fields are stored in little endian, if not specified otherwise.\n *\n * <uint32_t zlbytes> is an unsigned integer to hold the number of bytes that\n * the ziplist occupies, including the four bytes of the zlbytes field itself.\n * This value needs to be stored to be able to resize the entire structure\n * without the need to traverse it first.\n *\n * <uint32_t zltail> is the offset to the last entry in the list. This allows\n * a pop operation on the far side of the list without the need for full\n * traversal.\n *\n * <uint16_t zllen> is the number of entries. When there are more than\n * 2^16-2 entries, this value is set to 2^16-1 and we need to traverse the\n * entire list to know how many items it holds.\n *\n * <uint8_t zlend> is a special entry representing the end of the ziplist.\n * Is encoded as a single byte equal to 255. No other normal entry starts\n * with a byte set to the value of 255.\n *\n * ZIPLIST ENTRIES\n * ===============\n *\n * Every entry in the ziplist is prefixed by metadata that contains two pieces\n * of information. First, the length of the previous entry is stored to be\n * able to traverse the list from back to front. Second, the entry encoding is\n * provided. It represents the entry type, integer or string, and in the case\n * of strings it also represents the length of the string payload.\n * So a complete entry is stored like this:\n *\n * <prevlen> <encoding> <entry-data>\n *\n * Sometimes the encoding represents the entry itself, like for small integers\n * as we'll see later. In such a case the <entry-data> part is missing, and we\n * could have just:\n *\n * <prevlen> <encoding>\n *\n * The length of the previous entry, <prevlen>, is encoded in the following way:\n * If this length is smaller than 254 bytes, it will only consume a single\n * byte representing the length as an unsinged 8 bit integer. When the length\n * is greater than or equal to 254, it will consume 5 bytes. The first byte is\n * set to 254 (FE) to indicate a larger value is following. The remaining 4\n * bytes take the length of the previous entry as value.\n *\n * So practically an entry is encoded in the following way:\n *\n * <prevlen from 0 to 253> <encoding> <entry>\n *\n * Or alternatively if the previous entry length is greater than 253 bytes\n * the following encoding is used:\n *\n * 0xFE <4 bytes unsigned little endian prevlen> <encoding> <entry>\n *\n * The encoding field of the entry depends on the content of the\n * entry. When the entry is a string, the first 2 bits of the encoding first\n * byte will hold the type of encoding used to store the length of the string,\n * followed by the actual length of the string. When the entry is an integer\n * the first 2 bits are both set to 1. The following 2 bits are used to specify\n * what kind of integer will be stored after this header. An overview of the\n * different types and encodings is as follows. The first byte is always enough\n * to determine the kind of entry.\n *\n * |00pppppp| - 1 byte\n *      String value with length less than or equal to 63 bytes (6 bits).\n *      \"pppppp\" represents the unsigned 6 bit length.\n * |01pppppp|qqqqqqqq| - 2 bytes\n *      String value with length less than or equal to 16383 bytes (14 bits).\n *      IMPORTANT: The 14 bit number is stored in big endian.\n * |10000000|qqqqqqqq|rrrrrrrr|ssssssss|tttttttt| - 5 bytes\n *      String value with length greater than or equal to 16384 bytes.\n *      Only the 4 bytes following the first byte represents the length\n *      up to 2^32-1. The 6 lower bits of the first byte are not used and\n *      are set to zero.\n *      IMPORTANT: The 32 bit number is stored in big endian.\n * |11000000| - 3 bytes\n *      Integer encoded as int16_t (2 bytes).\n * |11010000| - 5 bytes\n *      Integer encoded as int32_t (4 bytes).\n * |11100000| - 9 bytes\n *      Integer encoded as int64_t (8 bytes).\n * |11110000| - 4 bytes\n *      Integer encoded as 24 bit signed (3 bytes).\n * |11111110| - 2 bytes\n *      Integer encoded as 8 bit signed (1 byte).\n * |1111xxxx| - (with xxxx between 0001 and 1101) immediate 4 bit integer.\n *      Unsigned integer from 0 to 12. The encoded value is actually from\n *      1 to 13 because 0000 and 1111 can not be used, so 1 should be\n *      subtracted from the encoded 4 bit value to obtain the right value.\n * |11111111| - End of ziplist special entry.\n *\n * Like for the ziplist header, all the integers are represented in little\n * endian byte order, even when this code is compiled in big endian systems.\n *\n * EXAMPLES OF ACTUAL ZIPLISTS\n * ===========================\n *\n * The following is a ziplist containing the two elements representing\n * the strings \"2\" and \"5\". It is composed of 15 bytes, that we visually\n * split into sections:\n *\n *  [0f 00 00 00] [0c 00 00 00] [02 00] [00 f3] [02 f6] [ff]\n *        |             |          |       |       |     |\n *     zlbytes        zltail    entries   \"2\"     \"5\"   end\n *\n * The first 4 bytes represent the number 15, that is the number of bytes\n * the whole ziplist is composed of. The second 4 bytes are the offset\n * at which the last ziplist entry is found, that is 12, in fact the\n * last entry, that is \"5\", is at offset 12 inside the ziplist.\n * The next 16 bit integer represents the number of elements inside the\n * ziplist, its value is 2 since there are just two elements inside.\n * Finally \"00 f3\" is the first entry representing the number 2. It is\n * composed of the previous entry length, which is zero because this is\n * our first entry, and the byte F3 which corresponds to the encoding\n * |1111xxxx| with xxxx between 0001 and 1101. We need to remove the \"F\"\n * higher order bits 1111, and subtract 1 from the \"3\", so the entry value\n * is \"2\". The next entry has a prevlen of 02, since the first entry is\n * composed of exactly two bytes. The entry itself, F6, is encoded exactly\n * like the first entry, and 6-1 = 5, so the value of the entry is 5.\n * Finally the special entry FF signals the end of the ziplist.\n *\n * Adding another element to the above string with the value \"Hello World\"\n * allows us to show how the ziplist encodes small strings. We'll just show\n * the hex dump of the entry itself. Imagine the bytes as following the\n * entry that stores \"5\" in the ziplist above:\n *\n * [02] [0b] [48 65 6c 6c 6f 20 57 6f 72 6c 64]\n *\n * The first byte, 02, is the length of the previous entry. The next\n * byte represents the encoding in the pattern |00pppppp| that means\n * that the entry is a string of length <pppppp>, so 0B means that\n * an 11 bytes string follows. From the third byte (48) to the last (64)\n * there are just the ASCII characters for \"Hello World\".\n *\n * ----------------------------------------------------------------------------\n *\n * Copyright (c) 2009-2012, Pieter Noordhuis <pcnoordhuis at gmail dot com>\n * Copyright (c) 2009-2017, Salvatore Sanfilippo <antirez at gmail dot com>\n * Copyright (c) 2020, Redis Labs, Inc\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <stdint.h>\n#include <limits.h>\n#include \"zmalloc.h\"\n#include \"util.h\"\n#include \"ziplist.h\"\n#include \"config.h\"\n#include \"endianconv.h\"\n#include \"redisassert.h\"\n\n#define ZIP_END 255         /* Special \"end of ziplist\" entry. */\n#define ZIP_BIG_PREVLEN 254 /* ZIP_BIG_PREVLEN - 1 is the max number of bytes of\n                               the previous entry, for the \"prevlen\" field prefixing\n                               each entry, to be represented with just a single byte.\n                               Otherwise it is represented as FE AA BB CC DD, where\n                               AA BB CC DD are a 4 bytes unsigned integer\n                               representing the previous entry len. */\n\n/* Different encoding/length possibilities */\n#define ZIP_STR_MASK 0xc0\n#define ZIP_INT_MASK 0x30\n#define ZIP_STR_06B (0 << 6)\n#define ZIP_STR_14B (1 << 6)\n#define ZIP_STR_32B (2 << 6)\n#define ZIP_INT_16B (0xc0 | 0<<4)\n#define ZIP_INT_32B (0xc0 | 1<<4)\n#define ZIP_INT_64B (0xc0 | 2<<4)\n#define ZIP_INT_24B (0xc0 | 3<<4)\n#define ZIP_INT_8B 0xfe\n\n/* 4 bit integer immediate encoding |1111xxxx| with xxxx between\n * 0001 and 1101. */\n#define ZIP_INT_IMM_MASK 0x0f   /* Mask to extract the 4 bits value. To add\n                                   one is needed to reconstruct the value. */\n#define ZIP_INT_IMM_MIN 0xf1    /* 11110001 */\n#define ZIP_INT_IMM_MAX 0xfd    /* 11111101 */\n\n#define INT24_MAX 0x7fffff\n#define INT24_MIN (-INT24_MAX - 1)\n\n/* Macro to determine if the entry is a string. String entries never start\n * with \"11\" as most significant bits of the first byte. */\n#define ZIP_IS_STR(enc) (((enc) & ZIP_STR_MASK) < ZIP_STR_MASK)\n\n/* Utility macros.*/\n\n/* Return total bytes a ziplist is composed of. */\n#define ZIPLIST_BYTES(zl)       (*((uint32_t*)(zl)))\n\n/* Return the offset of the last item inside the ziplist. */\n#define ZIPLIST_TAIL_OFFSET(zl) (*((uint32_t*)((zl)+sizeof(uint32_t))))\n\n/* Return the length of a ziplist, or UINT16_MAX if the length cannot be\n * determined without scanning the whole ziplist. */\n#define ZIPLIST_LENGTH(zl)      (*((uint16_t*)((zl)+sizeof(uint32_t)*2)))\n\n/* The size of a ziplist header: two 32 bit integers for the total\n * bytes count and last item offset. One 16 bit integer for the number\n * of items field. */\n#define ZIPLIST_HEADER_SIZE     (sizeof(uint32_t)*2+sizeof(uint16_t))\n\n/* Size of the \"end of ziplist\" entry. Just one byte. */\n#define ZIPLIST_END_SIZE        (sizeof(uint8_t))\n\n/* Return the pointer to the first entry of a ziplist. */\n#define ZIPLIST_ENTRY_HEAD(zl)  ((zl)+ZIPLIST_HEADER_SIZE)\n\n/* Return the pointer to the last entry of a ziplist, using the\n * last entry offset inside the ziplist header. */\n#define ZIPLIST_ENTRY_TAIL(zl)  ((zl)+intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl)))\n\n/* Return the pointer to the last byte of a ziplist, which is, the\n * end of ziplist FF entry. */\n#define ZIPLIST_ENTRY_END(zl)   ((zl)+intrev32ifbe(ZIPLIST_BYTES(zl))-1)\n\n/* Increment the number of items field in the ziplist header. Note that this\n * macro should never overflow the unsigned 16 bit integer, since entries are\n * always pushed one at a time. When UINT16_MAX is reached we want the count\n * to stay there to signal that a full scan is needed to get the number of\n * items inside the ziplist. */\n#define ZIPLIST_INCR_LENGTH(zl,incr) { \\\n    if (intrev16ifbe(ZIPLIST_LENGTH(zl)) < UINT16_MAX) \\\n        ZIPLIST_LENGTH(zl) = intrev16ifbe(intrev16ifbe(ZIPLIST_LENGTH(zl))+incr); \\\n}\n\n/* Don't let ziplists grow over 1GB in any case, don't wanna risk overflow in\n * zlbytes*/\n#define ZIPLIST_MAX_SAFETY_SIZE (1<<30)\nint ziplistSafeToAdd(unsigned char* zl, size_t add) {\n    size_t len = zl? ziplistBlobLen(zl): 0;\n    if (len + add > ZIPLIST_MAX_SAFETY_SIZE)\n        return 0;\n    return 1;\n}\n\n\n/* We use this function to receive information about a ziplist entry.\n * Note that this is not how the data is actually encoded, is just what we\n * get filled by a function in order to operate more easily. */\ntypedef struct zlentry {\n    unsigned int prevrawlensize; /* Bytes used to encode the previous entry len*/\n    unsigned int prevrawlen;     /* Previous entry len. */\n    unsigned int lensize;        /* Bytes used to encode this entry type/len.\n                                    For example strings have a 1, 2 or 5 bytes\n                                    header. Integers always use a single byte.*/\n    unsigned int len;            /* Bytes used to represent the actual entry.\n                                    For strings this is just the string length\n                                    while for integers it is 1, 2, 3, 4, 8 or\n                                    0 (for 4 bit immediate) depending on the\n                                    number range. */\n    unsigned int headersize;     /* prevrawlensize + lensize. */\n    unsigned char encoding;      /* Set to ZIP_STR_* or ZIP_INT_* depending on\n                                    the entry encoding. However for 4 bits\n                                    immediate integers this can assume a range\n                                    of values and must be range-checked. */\n    unsigned char *p;            /* Pointer to the very start of the entry, that\n                                    is, this points to prev-entry-len field. */\n} zlentry;\n\n#define ZIPLIST_ENTRY_ZERO(zle) { \\\n    (zle)->prevrawlensize = (zle)->prevrawlen = 0; \\\n    (zle)->lensize = (zle)->len = (zle)->headersize = 0; \\\n    (zle)->encoding = 0; \\\n    (zle)->p = NULL; \\\n}\n\n/* Extract the encoding from the byte pointed by 'ptr' and set it into\n * 'encoding' field of the zlentry structure. */\n#define ZIP_ENTRY_ENCODING(ptr, encoding) do {  \\\n    (encoding) = ((ptr)[0]); \\\n    if ((encoding) < ZIP_STR_MASK) (encoding) &= ZIP_STR_MASK; \\\n} while(0)\n\n#define ZIP_ENCODING_SIZE_INVALID 0xff\n/* Return the number of bytes required to encode the entry type + length.\n * On error, return ZIP_ENCODING_SIZE_INVALID */\nstatic inline unsigned int zipEncodingLenSize(unsigned char encoding) {\n    if (encoding == ZIP_INT_16B || encoding == ZIP_INT_32B ||\n        encoding == ZIP_INT_24B || encoding == ZIP_INT_64B ||\n        encoding == ZIP_INT_8B)\n        return 1;\n    if (encoding >= ZIP_INT_IMM_MIN && encoding <= ZIP_INT_IMM_MAX)\n        return 1;\n    if (encoding == ZIP_STR_06B)\n        return 1;\n    if (encoding == ZIP_STR_14B)\n        return 2;\n    if (encoding == ZIP_STR_32B)\n        return 5;\n    return ZIP_ENCODING_SIZE_INVALID;\n}\n\n#define ZIP_ASSERT_ENCODING(encoding) do {                                     \\\n    assert(zipEncodingLenSize(encoding) != ZIP_ENCODING_SIZE_INVALID);         \\\n} while (0)\n\n/* Return bytes needed to store integer encoded by 'encoding' */\nstatic inline unsigned int zipIntSize(unsigned char encoding) {\n    switch(encoding) {\n    case ZIP_INT_8B:  return 1;\n    case ZIP_INT_16B: return 2;\n    case ZIP_INT_24B: return 3;\n    case ZIP_INT_32B: return 4;\n    case ZIP_INT_64B: return 8;\n    }\n    if (encoding >= ZIP_INT_IMM_MIN && encoding <= ZIP_INT_IMM_MAX)\n        return 0; /* 4 bit immediate */\n    /* bad encoding, covered by a previous call to ZIP_ASSERT_ENCODING */\n    redis_unreachable();\n    return 0;\n}\n\n/* Write the encoding header of the entry in 'p'. If p is NULL it just returns\n * the amount of bytes required to encode such a length. Arguments:\n *\n * 'encoding' is the encoding we are using for the entry. It could be\n * ZIP_INT_* or ZIP_STR_* or between ZIP_INT_IMM_MIN and ZIP_INT_IMM_MAX\n * for single-byte small immediate integers.\n *\n * 'rawlen' is only used for ZIP_STR_* encodings and is the length of the\n * string that this entry represents.\n *\n * The function returns the number of bytes used by the encoding/length\n * header stored in 'p'. */\nunsigned int zipStoreEntryEncoding(unsigned char *p, unsigned char encoding, unsigned int rawlen) {\n    unsigned char len = 1, buf[5];\n\n    if (ZIP_IS_STR(encoding)) {\n        /* Although encoding is given it may not be set for strings,\n         * so we determine it here using the raw length. */\n        if (rawlen <= 0x3f) {\n            if (!p) return len;\n            buf[0] = ZIP_STR_06B | rawlen;\n        } else if (rawlen <= 0x3fff) {\n            len += 1;\n            if (!p) return len;\n            buf[0] = ZIP_STR_14B | ((rawlen >> 8) & 0x3f);\n            buf[1] = rawlen & 0xff;\n        } else {\n            len += 4;\n            if (!p) return len;\n            buf[0] = ZIP_STR_32B;\n            buf[1] = (rawlen >> 24) & 0xff;\n            buf[2] = (rawlen >> 16) & 0xff;\n            buf[3] = (rawlen >> 8) & 0xff;\n            buf[4] = rawlen & 0xff;\n        }\n    } else {\n        /* Implies integer encoding, so length is always 1. */\n        if (!p) return len;\n        buf[0] = encoding;\n    }\n\n    /* Store this length at p. */\n    memcpy(p,buf,len);\n    return len;\n}\n\n/* Decode the entry encoding type and data length (string length for strings,\n * number of bytes used for the integer for integer entries) encoded in 'ptr'.\n * The 'encoding' variable is input, extracted by the caller, the 'lensize'\n * variable will hold the number of bytes required to encode the entry\n * length, and the 'len' variable will hold the entry length.\n * On invalid encoding error, lensize is set to 0. */\n#define ZIP_DECODE_LENGTH(ptr, encoding, lensize, len) do {                    \\\n    if ((encoding) < ZIP_STR_MASK) {                                           \\\n        if ((encoding) == ZIP_STR_06B) {                                       \\\n            (lensize) = 1;                                                     \\\n            (len) = (ptr)[0] & 0x3f;                                           \\\n        } else if ((encoding) == ZIP_STR_14B) {                                \\\n            (lensize) = 2;                                                     \\\n            (len) = (((ptr)[0] & 0x3f) << 8) | (ptr)[1];                       \\\n        } else if ((encoding) == ZIP_STR_32B) {                                \\\n            (lensize) = 5;                                                     \\\n            (len) = ((ptr)[1] << 24) |                                         \\\n                    ((ptr)[2] << 16) |                                         \\\n                    ((ptr)[3] <<  8) |                                         \\\n                    ((ptr)[4]);                                                \\\n        } else {                                                               \\\n            (lensize) = 0; /* bad encoding, should be covered by a previous */ \\\n            (len) = 0;     /* ZIP_ASSERT_ENCODING / zipEncodingLenSize, or  */ \\\n                           /* match the lensize after this macro with 0.    */ \\\n        }                                                                      \\\n    } else {                                                                   \\\n        (lensize) = 1;                                                         \\\n        if ((encoding) == ZIP_INT_8B)  (len) = 1;                              \\\n        else if ((encoding) == ZIP_INT_16B) (len) = 2;                         \\\n        else if ((encoding) == ZIP_INT_24B) (len) = 3;                         \\\n        else if ((encoding) == ZIP_INT_32B) (len) = 4;                         \\\n        else if ((encoding) == ZIP_INT_64B) (len) = 8;                         \\\n        else if (encoding >= ZIP_INT_IMM_MIN && encoding <= ZIP_INT_IMM_MAX)   \\\n            (len) = 0; /* 4 bit immediate */                                   \\\n        else                                                                   \\\n            (lensize) = (len) = 0; /* bad encoding */                          \\\n    }                                                                          \\\n} while(0)\n\n/* Encode the length of the previous entry and write it to \"p\". This only\n * uses the larger encoding (required in __ziplistCascadeUpdate). */\nint zipStorePrevEntryLengthLarge(unsigned char *p, unsigned int len) {\n    uint32_t u32;\n    if (p != NULL) {\n        p[0] = ZIP_BIG_PREVLEN;\n        u32 = len;\n        memcpy(p+1,&u32,sizeof(u32));\n        memrev32ifbe(p+1);\n    }\n    return 1 + sizeof(uint32_t);\n}\n\n/* Encode the length of the previous entry and write it to \"p\". Return the\n * number of bytes needed to encode this length if \"p\" is NULL. */\nunsigned int zipStorePrevEntryLength(unsigned char *p, unsigned int len) {\n    if (p == NULL) {\n        return (len < ZIP_BIG_PREVLEN) ? 1 : sizeof(uint32_t) + 1;\n    } else {\n        if (len < ZIP_BIG_PREVLEN) {\n            p[0] = len;\n            return 1;\n        } else {\n            return zipStorePrevEntryLengthLarge(p,len);\n        }\n    }\n}\n\n/* Return the number of bytes used to encode the length of the previous\n * entry. The length is returned by setting the var 'prevlensize'. */\n#define ZIP_DECODE_PREVLENSIZE(ptr, prevlensize) do {                          \\\n    if ((ptr)[0] < ZIP_BIG_PREVLEN) {                                          \\\n        (prevlensize) = 1;                                                     \\\n    } else {                                                                   \\\n        (prevlensize) = 5;                                                     \\\n    }                                                                          \\\n} while(0)\n\n/* Return the length of the previous element, and the number of bytes that\n * are used in order to encode the previous element length.\n * 'ptr' must point to the prevlen prefix of an entry (that encodes the\n * length of the previous entry in order to navigate the elements backward).\n * The length of the previous entry is stored in 'prevlen', the number of\n * bytes needed to encode the previous entry length are stored in\n * 'prevlensize'. */\n#define ZIP_DECODE_PREVLEN(ptr, prevlensize, prevlen) do {                     \\\n    ZIP_DECODE_PREVLENSIZE(ptr, prevlensize);                                  \\\n    if ((prevlensize) == 1) {                                                  \\\n        (prevlen) = (ptr)[0];                                                  \\\n    } else { /* prevlensize == 5 */                                            \\\n        (prevlen) = ((ptr)[4] << 24) |                                         \\\n                    ((ptr)[3] << 16) |                                         \\\n                    ((ptr)[2] <<  8) |                                         \\\n                    ((ptr)[1]);                                                \\\n    }                                                                          \\\n} while(0)\n\n/* Given a pointer 'p' to the prevlen info that prefixes an entry, this\n * function returns the difference in number of bytes needed to encode\n * the prevlen if the previous entry changes of size.\n *\n * So if A is the number of bytes used right now to encode the 'prevlen'\n * field.\n *\n * And B is the number of bytes that are needed in order to encode the\n * 'prevlen' if the previous element will be updated to one of size 'len'.\n *\n * Then the function returns B - A\n *\n * So the function returns a positive number if more space is needed,\n * a negative number if less space is needed, or zero if the same space\n * is needed. */\nint zipPrevLenByteDiff(unsigned char *p, unsigned int len) {\n    unsigned int prevlensize;\n    ZIP_DECODE_PREVLENSIZE(p, prevlensize);\n    return zipStorePrevEntryLength(NULL, len) - prevlensize;\n}\n\n/* Check if string pointed to by 'entry' can be encoded as an integer.\n * Stores the integer value in 'v' and its encoding in 'encoding'. */\nint zipTryEncoding(unsigned char *entry, unsigned int entrylen, long long *v, unsigned char *encoding) {\n    long long value;\n\n    if (entrylen >= 32 || entrylen == 0) return 0;\n    if (string2ll((char*)entry,entrylen,&value)) {\n        /* Great, the string can be encoded. Check what's the smallest\n         * of our encoding types that can hold this value. */\n        if (value >= 0 && value <= 12) {\n            *encoding = ZIP_INT_IMM_MIN+value;\n        } else if (value >= INT8_MIN && value <= INT8_MAX) {\n            *encoding = ZIP_INT_8B;\n        } else if (value >= INT16_MIN && value <= INT16_MAX) {\n            *encoding = ZIP_INT_16B;\n        } else if (value >= INT24_MIN && value <= INT24_MAX) {\n            *encoding = ZIP_INT_24B;\n        } else if (value >= INT32_MIN && value <= INT32_MAX) {\n            *encoding = ZIP_INT_32B;\n        } else {\n            *encoding = ZIP_INT_64B;\n        }\n        *v = value;\n        return 1;\n    }\n    return 0;\n}\n\n/* Store integer 'value' at 'p', encoded as 'encoding' */\nvoid zipSaveInteger(unsigned char *p, int64_t value, unsigned char encoding) {\n    int16_t i16;\n    int32_t i32;\n    int64_t i64;\n    if (encoding == ZIP_INT_8B) {\n        ((int8_t*)p)[0] = (int8_t)value;\n    } else if (encoding == ZIP_INT_16B) {\n        i16 = value;\n        memcpy(p,&i16,sizeof(i16));\n        memrev16ifbe(p);\n    } else if (encoding == ZIP_INT_24B) {\n        i32 = value<<8;\n        memrev32ifbe(&i32);\n        memcpy(p,((uint8_t*)&i32)+1,sizeof(i32)-sizeof(uint8_t));\n    } else if (encoding == ZIP_INT_32B) {\n        i32 = value;\n        memcpy(p,&i32,sizeof(i32));\n        memrev32ifbe(p);\n    } else if (encoding == ZIP_INT_64B) {\n        i64 = value;\n        memcpy(p,&i64,sizeof(i64));\n        memrev64ifbe(p);\n    } else if (encoding >= ZIP_INT_IMM_MIN && encoding <= ZIP_INT_IMM_MAX) {\n        /* Nothing to do, the value is stored in the encoding itself. */\n    } else {\n        assert(NULL);\n    }\n}\n\n/* Read integer encoded as 'encoding' from 'p' */\nint64_t zipLoadInteger(unsigned char *p, unsigned char encoding) {\n    int16_t i16;\n    int32_t i32;\n    int64_t i64, ret = 0;\n    if (encoding == ZIP_INT_8B) {\n        ret = ((int8_t*)p)[0];\n    } else if (encoding == ZIP_INT_16B) {\n        memcpy(&i16,p,sizeof(i16));\n        memrev16ifbe(&i16);\n        ret = i16;\n    } else if (encoding == ZIP_INT_32B) {\n        memcpy(&i32,p,sizeof(i32));\n        memrev32ifbe(&i32);\n        ret = i32;\n    } else if (encoding == ZIP_INT_24B) {\n        i32 = 0;\n        memcpy(((uint8_t*)&i32)+1,p,sizeof(i32)-sizeof(uint8_t));\n        memrev32ifbe(&i32);\n        ret = i32>>8;\n    } else if (encoding == ZIP_INT_64B) {\n        memcpy(&i64,p,sizeof(i64));\n        memrev64ifbe(&i64);\n        ret = i64;\n    } else if (encoding >= ZIP_INT_IMM_MIN && encoding <= ZIP_INT_IMM_MAX) {\n        ret = (encoding & ZIP_INT_IMM_MASK)-1;\n    } else {\n        assert(NULL);\n    }\n    return ret;\n}\n\n/* Fills a struct with all information about an entry.\n * This function is the \"unsafe\" alternative to the one blow.\n * Generally, all function that return a pointer to an element in the ziplist\n * will assert that this element is valid, so it can be freely used.\n * Generally functions such ziplistGet assume the input pointer is already\n * validated (since it's the return value of another function). */\nstatic inline void zipEntry(unsigned char *p, zlentry *e) {\n    ZIP_DECODE_PREVLEN(p, e->prevrawlensize, e->prevrawlen);\n    ZIP_ENTRY_ENCODING(p + e->prevrawlensize, e->encoding);\n    ZIP_DECODE_LENGTH(p + e->prevrawlensize, e->encoding, e->lensize, e->len);\n    assert(e->lensize != 0); /* check that encoding was valid. */\n    e->headersize = e->prevrawlensize + e->lensize;\n    e->p = p;\n}\n\n/* Fills a struct with all information about an entry.\n * This function is safe to use on untrusted pointers, it'll make sure not to\n * try to access memory outside the ziplist payload.\n * Returns 1 if the entry is valid, and 0 otherwise. */\nstatic inline int zipEntrySafe(unsigned char* zl, size_t zlbytes, unsigned char *p, zlentry *e, int validate_prevlen) {\n    unsigned char *zlfirst = zl + ZIPLIST_HEADER_SIZE;\n    unsigned char *zllast = zl + zlbytes - ZIPLIST_END_SIZE;\n#define OUT_OF_RANGE(p) (unlikely((p) < zlfirst || (p) > zllast))\n\n    /* If threre's no possibility for the header to reach outside the ziplist,\n     * take the fast path. (max lensize and prevrawlensize are both 5 bytes) */\n    if (p >= zlfirst && p + 10 < zllast) {\n        ZIP_DECODE_PREVLEN(p, e->prevrawlensize, e->prevrawlen);\n        ZIP_ENTRY_ENCODING(p + e->prevrawlensize, e->encoding);\n        ZIP_DECODE_LENGTH(p + e->prevrawlensize, e->encoding, e->lensize, e->len);\n        e->headersize = e->prevrawlensize + e->lensize;\n        e->p = p;\n        /* We didn't call ZIP_ASSERT_ENCODING, so we check lensize was set to 0. */\n        if (unlikely(e->lensize == 0))\n            return 0;\n        /* Make sure the entry doesn't rech outside the edge of the ziplist */\n        if (OUT_OF_RANGE(p + e->headersize + e->len))\n            return 0;\n        /* Make sure prevlen doesn't rech outside the edge of the ziplist */\n        if (validate_prevlen && OUT_OF_RANGE(p - e->prevrawlen))\n            return 0;\n        return 1;\n    }\n\n    /* Make sure the pointer doesn't rech outside the edge of the ziplist */\n    if (OUT_OF_RANGE(p))\n        return 0;\n\n    /* Make sure the encoded prevlen header doesn't reach outside the allocation */\n    ZIP_DECODE_PREVLENSIZE(p, e->prevrawlensize);\n    if (OUT_OF_RANGE(p + e->prevrawlensize))\n        return 0;\n\n    /* Make sure encoded entry header is valid. */\n    ZIP_ENTRY_ENCODING(p + e->prevrawlensize, e->encoding);\n    e->lensize = zipEncodingLenSize(e->encoding);\n    if (unlikely(e->lensize == ZIP_ENCODING_SIZE_INVALID))\n        return 0;\n\n    /* Make sure the encoded entry header doesn't reach outside the allocation */\n    if (OUT_OF_RANGE(p + e->prevrawlensize + e->lensize))\n        return 0;\n\n    /* Decode the prevlen and entry len headers. */\n    ZIP_DECODE_PREVLEN(p, e->prevrawlensize, e->prevrawlen);\n    ZIP_DECODE_LENGTH(p + e->prevrawlensize, e->encoding, e->lensize, e->len);\n    e->headersize = e->prevrawlensize + e->lensize;\n\n    /* Make sure the entry doesn't rech outside the edge of the ziplist */\n    if (OUT_OF_RANGE(p + e->headersize + e->len))\n        return 0;\n\n    /* Make sure prevlen doesn't rech outside the edge of the ziplist */\n    if (validate_prevlen && OUT_OF_RANGE(p - e->prevrawlen))\n        return 0;\n\n    e->p = p;\n    return 1;\n#undef OUT_OF_RANGE\n}\n\n/* Return the total number of bytes used by the entry pointed to by 'p'. */\nstatic inline unsigned int zipRawEntryLengthSafe(unsigned char* zl, size_t zlbytes, const unsigned char *p) {\n    zlentry e;\n    assert(zipEntrySafe(zl, zlbytes, (unsigned char*)p, &e, 0));\n    return e.headersize + e.len;\n}\n\n/* Return the total number of bytes used by the entry pointed to by 'p'. */\nstatic inline unsigned int zipRawEntryLength(const unsigned char *p) {\n    zlentry e;\n    zipEntry((unsigned char*)p, &e);\n    return e.headersize + e.len;\n}\n\n/* Validate that the entry doesn't reach outside the ziplist allocation. */\nstatic inline void zipAssertValidEntry(unsigned char* zl, size_t zlbytes, unsigned char *p) {\n    zlentry e;\n    assert(zipEntrySafe(zl, zlbytes, p, &e, 1));\n}\n\n/* Create a new empty ziplist. */\nunsigned char *ziplistNew(void) {\n    unsigned int bytes = ZIPLIST_HEADER_SIZE+ZIPLIST_END_SIZE;\n    unsigned char *zl = zmalloc(bytes, MALLOC_SHARED);\n    ZIPLIST_BYTES(zl) = intrev32ifbe(bytes);\n    ZIPLIST_TAIL_OFFSET(zl) = intrev32ifbe(ZIPLIST_HEADER_SIZE);\n    ZIPLIST_LENGTH(zl) = 0;\n    zl[bytes-1] = ZIP_END;\n    return zl;\n}\n\n/* Resize the ziplist. */\nunsigned char *ziplistResize(unsigned char *zl, size_t len) {\n    assert(len < UINT32_MAX);\n    zl = zrealloc(zl,len, MALLOC_SHARED);\n    ZIPLIST_BYTES(zl) = intrev32ifbe(len);\n    zl[len-1] = ZIP_END;\n    return zl;\n}\n\n/* When an entry is inserted, we need to set the prevlen field of the next\n * entry to equal the length of the inserted entry. It can occur that this\n * length cannot be encoded in 1 byte and the next entry needs to be grow\n * a bit larger to hold the 5-byte encoded prevlen. This can be done for free,\n * because this only happens when an entry is already being inserted (which\n * causes a realloc and memmove). However, encoding the prevlen may require\n * that this entry is grown as well. This effect may cascade throughout\n * the ziplist when there are consecutive entries with a size close to\n * ZIP_BIG_PREVLEN, so we need to check that the prevlen can be encoded in\n * every consecutive entry.\n *\n * Note that this effect can also happen in reverse, where the bytes required\n * to encode the prevlen field can shrink. This effect is deliberately ignored,\n * because it can cause a \"flapping\" effect where a chain prevlen fields is\n * first grown and then shrunk again after consecutive inserts. Rather, the\n * field is allowed to stay larger than necessary, because a large prevlen\n * field implies the ziplist is holding large entries anyway.\n *\n * The pointer \"p\" points to the first entry that does NOT need to be\n * updated, i.e. consecutive fields MAY need an update. */\nunsigned char *__ziplistCascadeUpdate(unsigned char *zl, unsigned char *p) {\n    zlentry cur;\n    size_t prevlen, prevlensize, prevoffset; /* Informat of the last changed entry. */\n    size_t firstentrylen; /* Used to handle insert at head. */\n    size_t rawlen, curlen = intrev32ifbe(ZIPLIST_BYTES(zl));\n    size_t extra = 0, cnt = 0, offset;\n    size_t delta = 4; /* Extra bytes needed to update a entry's prevlen (5-1). */\n    unsigned char *tail = zl + intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl));\n\n    /* Empty ziplist */\n    if (p[0] == ZIP_END) return zl;\n\n    zipEntry(p, &cur); /* no need for \"safe\" variant since the input pointer was validated by the function that returned it. */\n    firstentrylen = prevlen = cur.headersize + cur.len;\n    prevlensize = zipStorePrevEntryLength(NULL, prevlen);\n    prevoffset = p - zl;\n    p += prevlen;\n\n    /* Iterate ziplist to find out how many extra bytes do we need to update it. */\n    while (p[0] != ZIP_END) {\n        assert(zipEntrySafe(zl, curlen, p, &cur, 0));\n\n        /* Abort when \"prevlen\" has not changed. */\n        if (cur.prevrawlen == prevlen) break;\n\n        /* Abort when entry's \"prevlensize\" is big enough. */\n        if (cur.prevrawlensize >= prevlensize) {\n            if (cur.prevrawlensize == prevlensize) {\n                zipStorePrevEntryLength(p, prevlen);\n            } else {\n                /* This would result in shrinking, which we want to avoid.\n                 * So, set \"prevlen\" in the available bytes. */\n                zipStorePrevEntryLengthLarge(p, prevlen);\n            }\n            break;\n        }\n\n        /* cur.prevrawlen means cur is the former head entry. */\n        assert(cur.prevrawlen == 0 || cur.prevrawlen + delta == prevlen);\n\n        /* Update prev entry's info and advance the cursor. */\n        rawlen = cur.headersize + cur.len;\n        prevlen = rawlen + delta; \n        prevlensize = zipStorePrevEntryLength(NULL, prevlen);\n        prevoffset = p - zl;\n        p += rawlen;\n        extra += delta;\n        cnt++;\n    }\n\n    /* Extra bytes is zero all update has been done(or no need to update). */\n    if (extra == 0) return zl;\n\n    /* Update tail offset after loop. */\n    if (tail == zl + prevoffset) {\n        /* When the the last entry we need to update is also the tail, update tail offset\n         * unless this is the only entry that was updated (so the tail offset didn't change). */\n        if (extra - delta != 0) {\n            ZIPLIST_TAIL_OFFSET(zl) =\n                intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+extra-delta);\n        }\n    } else {\n        /* Update the tail offset in cases where the last entry we updated is not the tail. */\n        ZIPLIST_TAIL_OFFSET(zl) =\n            intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+extra);\n    }\n\n    /* Now \"p\" points at the first unchanged byte in original ziplist,\n     * move data after that to new ziplist. */\n    offset = p - zl;\n    zl = ziplistResize(zl, curlen + extra);\n    p = zl + offset;\n    memmove(p + extra, p, curlen - offset - 1);\n    p += extra;\n\n    /* Iterate all entries that need to be updated tail to head. */\n    while (cnt) {\n        zipEntry(zl + prevoffset, &cur); /* no need for \"safe\" variant since we already iterated on all these entries above. */\n        rawlen = cur.headersize + cur.len;\n        /* Move entry to tail and reset prevlen. */\n        memmove(p - (rawlen - cur.prevrawlensize), \n                zl + prevoffset + cur.prevrawlensize, \n                rawlen - cur.prevrawlensize);\n        p -= (rawlen + delta);\n        if (cur.prevrawlen == 0) {\n            /* \"cur\" is the previous head entry, update its prevlen with firstentrylen. */\n            zipStorePrevEntryLength(p, firstentrylen);\n        } else {\n            /* An entry's prevlen can only increment 4 bytes. */\n            zipStorePrevEntryLength(p, cur.prevrawlen+delta);\n        }\n        /* Foward to previous entry. */\n        prevoffset -= cur.prevrawlen;\n        cnt--;\n    }\n    return zl;\n}\n\n/* Delete \"num\" entries, starting at \"p\". Returns pointer to the ziplist. */\nunsigned char *__ziplistDelete(unsigned char *zl, unsigned char *p, unsigned int num) {\n    unsigned int i, totlen, deleted = 0;\n    size_t offset;\n    int nextdiff = 0;\n    zlentry first, tail;\n    size_t zlbytes = intrev32ifbe(ZIPLIST_BYTES(zl));\n\n    zipEntry(p, &first); /* no need for \"safe\" variant since the input pointer was validated by the function that returned it. */\n    for (i = 0; p[0] != ZIP_END && i < num; i++) {\n        p += zipRawEntryLengthSafe(zl, zlbytes, p);\n        deleted++;\n    }\n\n    assert(p >= first.p);\n    totlen = p-first.p; /* Bytes taken by the element(s) to delete. */\n    if (totlen > 0) {\n        uint32_t set_tail;\n        if (p[0] != ZIP_END) {\n            /* Storing `prevrawlen` in this entry may increase or decrease the\n             * number of bytes required compare to the current `prevrawlen`.\n             * There always is room to store this, because it was previously\n             * stored by an entry that is now being deleted. */\n            nextdiff = zipPrevLenByteDiff(p,first.prevrawlen);\n\n            /* Note that there is always space when p jumps backward: if\n             * the new previous entry is large, one of the deleted elements\n             * had a 5 bytes prevlen header, so there is for sure at least\n             * 5 bytes free and we need just 4. */\n            p -= nextdiff;\n            assert(p >= first.p && p<zl+zlbytes-1);\n            zipStorePrevEntryLength(p,first.prevrawlen);\n\n            /* Update offset for tail */\n            set_tail = intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))-totlen;\n\n            /* When the tail contains more than one entry, we need to take\n             * \"nextdiff\" in account as well. Otherwise, a change in the\n             * size of prevlen doesn't have an effect on the *tail* offset. */\n            assert(zipEntrySafe(zl, zlbytes, p, &tail, 1));\n            if (p[tail.headersize+tail.len] != ZIP_END) {\n                set_tail = set_tail + nextdiff;\n            }\n\n            /* Move tail to the front of the ziplist */\n            /* since we asserted that p >= first.p. we know totlen >= 0,\n             * so we know that p > first.p and this is guaranteed not to reach\n             * beyond the allocation, even if the entries lens are corrupted. */\n            size_t bytes_to_move = zlbytes-(p-zl)-1;\n            memmove(first.p,p,bytes_to_move);\n        } else {\n            /* The entire tail was deleted. No need to move memory. */\n            set_tail = (first.p-zl)-first.prevrawlen;\n        }\n\n        /* Resize the ziplist */\n        offset = first.p-zl;\n        zlbytes -= totlen - nextdiff;\n        zl = ziplistResize(zl, zlbytes);\n        p = zl+offset;\n\n        /* Update record count */\n        ZIPLIST_INCR_LENGTH(zl,-deleted);\n\n        /* Set the tail offset computed above */\n        assert(set_tail <= zlbytes - ZIPLIST_END_SIZE);\n        ZIPLIST_TAIL_OFFSET(zl) = intrev32ifbe(set_tail);\n\n        /* When nextdiff != 0, the raw length of the next entry has changed, so\n         * we need to cascade the update throughout the ziplist */\n        if (nextdiff != 0)\n            zl = __ziplistCascadeUpdate(zl,p);\n    }\n    return zl;\n}\n\n/* Insert item at \"p\". */\nunsigned char *__ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {\n    size_t curlen = intrev32ifbe(ZIPLIST_BYTES(zl)), reqlen, newlen;\n    unsigned int prevlensize, prevlen = 0;\n    size_t offset;\n    int nextdiff = 0;\n    unsigned char encoding = 0;\n    long long value = 123456789; /* initialized to avoid warning. Using a value\n                                    that is easy to see if for some reason\n                                    we use it uninitialized. */\n    zlentry tail;\n\n    /* Find out prevlen for the entry that is inserted. */\n    if (p[0] != ZIP_END) {\n        ZIP_DECODE_PREVLEN(p, prevlensize, prevlen);\n    } else {\n        unsigned char *ptail = ZIPLIST_ENTRY_TAIL(zl);\n        if (ptail[0] != ZIP_END) {\n            prevlen = zipRawEntryLengthSafe(zl, curlen, ptail);\n        }\n    }\n\n    /* See if the entry can be encoded */\n    if (zipTryEncoding(s,slen,&value,&encoding)) {\n        /* 'encoding' is set to the appropriate integer encoding */\n        reqlen = zipIntSize(encoding);\n    } else {\n        /* 'encoding' is untouched, however zipStoreEntryEncoding will use the\n         * string length to figure out how to encode it. */\n        reqlen = slen;\n    }\n    /* We need space for both the length of the previous entry and\n     * the length of the payload. */\n    reqlen += zipStorePrevEntryLength(NULL,prevlen);\n    reqlen += zipStoreEntryEncoding(NULL,encoding,slen);\n\n    /* When the insert position is not equal to the tail, we need to\n     * make sure that the next entry can hold this entry's length in\n     * its prevlen field. */\n    int forcelarge = 0;\n    nextdiff = (p[0] != ZIP_END) ? zipPrevLenByteDiff(p,reqlen) : 0;\n    if (nextdiff == -4 && reqlen < 4) {\n        nextdiff = 0;\n        forcelarge = 1;\n    }\n\n    /* Store offset because a realloc may change the address of zl. */\n    offset = p-zl;\n    newlen = curlen+reqlen+nextdiff;\n    zl = ziplistResize(zl,newlen);\n    p = zl+offset;\n\n    /* Apply memory move when necessary and update tail offset. */\n    if (p[0] != ZIP_END) {\n        /* Subtract one because of the ZIP_END bytes */\n        memmove(p+reqlen,p-nextdiff,curlen-offset-1+nextdiff);\n\n        /* Encode this entry's raw length in the next entry. */\n        if (forcelarge)\n            zipStorePrevEntryLengthLarge(p+reqlen,reqlen);\n        else\n            zipStorePrevEntryLength(p+reqlen,reqlen);\n\n        /* Update offset for tail */\n        ZIPLIST_TAIL_OFFSET(zl) =\n            intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+reqlen);\n\n        /* When the tail contains more than one entry, we need to take\n         * \"nextdiff\" in account as well. Otherwise, a change in the\n         * size of prevlen doesn't have an effect on the *tail* offset. */\n        assert(zipEntrySafe(zl, newlen, p+reqlen, &tail, 1));\n        if (p[reqlen+tail.headersize+tail.len] != ZIP_END) {\n            ZIPLIST_TAIL_OFFSET(zl) =\n                intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+nextdiff);\n        }\n    } else {\n        /* This element will be the new tail. */\n        ZIPLIST_TAIL_OFFSET(zl) = intrev32ifbe(p-zl);\n    }\n\n    /* When nextdiff != 0, the raw length of the next entry has changed, so\n     * we need to cascade the update throughout the ziplist */\n    if (nextdiff != 0) {\n        offset = p-zl;\n        zl = __ziplistCascadeUpdate(zl,p+reqlen);\n        p = zl+offset;\n    }\n\n    /* Write the entry */\n    p += zipStorePrevEntryLength(p,prevlen);\n    p += zipStoreEntryEncoding(p,encoding,slen);\n    if (ZIP_IS_STR(encoding)) {\n        memcpy(p,s,slen);\n    } else {\n        zipSaveInteger(p,value,encoding);\n    }\n    ZIPLIST_INCR_LENGTH(zl,1);\n    return zl;\n}\n\n/* Merge ziplists 'first' and 'second' by appending 'second' to 'first'.\n *\n * NOTE: The larger ziplist is reallocated to contain the new merged ziplist.\n * Either 'first' or 'second' can be used for the result.  The parameter not\n * used will be free'd and set to NULL.\n *\n * After calling this function, the input parameters are no longer valid since\n * they are changed and free'd in-place.\n *\n * The result ziplist is the contents of 'first' followed by 'second'.\n *\n * On failure: returns NULL if the merge is impossible.\n * On success: returns the merged ziplist (which is expanded version of either\n * 'first' or 'second', also frees the other unused input ziplist, and sets the\n * input ziplist argument equal to newly reallocated ziplist return value. */\nunsigned char *ziplistMerge(unsigned char **first, unsigned char **second) {\n    /* If any params are null, we can't merge, so NULL. */\n    if (first == NULL || *first == NULL || second == NULL || *second == NULL)\n        return NULL;\n\n    /* Can't merge same list into itself. */\n    if (*first == *second)\n        return NULL;\n\n    size_t first_bytes = intrev32ifbe(ZIPLIST_BYTES(*first));\n    size_t first_len = intrev16ifbe(ZIPLIST_LENGTH(*first));\n\n    size_t second_bytes = intrev32ifbe(ZIPLIST_BYTES(*second));\n    size_t second_len = intrev16ifbe(ZIPLIST_LENGTH(*second));\n\n    int append;\n    unsigned char *source, *target;\n    size_t target_bytes, source_bytes;\n    /* Pick the largest ziplist so we can resize easily in-place.\n     * We must also track if we are now appending or prepending to\n     * the target ziplist. */\n    if (first_len >= second_len) {\n        /* retain first, append second to first. */\n        target = *first;\n        target_bytes = first_bytes;\n        source = *second;\n        source_bytes = second_bytes;\n        append = 1;\n    } else {\n        /* else, retain second, prepend first to second. */\n        target = *second;\n        target_bytes = second_bytes;\n        source = *first;\n        source_bytes = first_bytes;\n        append = 0;\n    }\n\n    /* Calculate final bytes (subtract one pair of metadata) */\n    size_t zlbytes = first_bytes + second_bytes -\n                     ZIPLIST_HEADER_SIZE - ZIPLIST_END_SIZE;\n    size_t zllength = first_len + second_len;\n\n    /* Combined zl length should be limited within UINT16_MAX */\n    zllength = zllength < UINT16_MAX ? zllength : UINT16_MAX;\n\n    /* larger values can't be stored into ZIPLIST_BYTES */\n    assert(zlbytes < UINT32_MAX);\n\n    /* Save offset positions before we start ripping memory apart. */\n    size_t first_offset = intrev32ifbe(ZIPLIST_TAIL_OFFSET(*first));\n    size_t second_offset = intrev32ifbe(ZIPLIST_TAIL_OFFSET(*second));\n\n    /* Extend target to new zlbytes then append or prepend source. */\n    target = zrealloc(target, zlbytes, MALLOC_SHARED);\n    if (append) {\n        /* append == appending to target */\n        /* Copy source after target (copying over original [END]):\n         *   [TARGET - END, SOURCE - HEADER] */\n        memcpy(target + target_bytes - ZIPLIST_END_SIZE,\n               source + ZIPLIST_HEADER_SIZE,\n               source_bytes - ZIPLIST_HEADER_SIZE);\n    } else {\n        /* !append == prepending to target */\n        /* Move target *contents* exactly size of (source - [END]),\n         * then copy source into vacated space (source - [END]):\n         *   [SOURCE - END, TARGET - HEADER] */\n        memmove(target + source_bytes - ZIPLIST_END_SIZE,\n                target + ZIPLIST_HEADER_SIZE,\n                target_bytes - ZIPLIST_HEADER_SIZE);\n        memcpy(target, source, source_bytes - ZIPLIST_END_SIZE);\n    }\n\n    /* Update header metadata. */\n    ZIPLIST_BYTES(target) = intrev32ifbe(zlbytes);\n    ZIPLIST_LENGTH(target) = intrev16ifbe(zllength);\n    /* New tail offset is:\n     *   + N bytes of first ziplist\n     *   - 1 byte for [END] of first ziplist\n     *   + M bytes for the offset of the original tail of the second ziplist\n     *   - J bytes for HEADER because second_offset keeps no header. */\n    ZIPLIST_TAIL_OFFSET(target) = intrev32ifbe(\n                                   (first_bytes - ZIPLIST_END_SIZE) +\n                                   (second_offset - ZIPLIST_HEADER_SIZE));\n\n    /* __ziplistCascadeUpdate just fixes the prev length values until it finds a\n     * correct prev length value (then it assumes the rest of the list is okay).\n     * We tell CascadeUpdate to start at the first ziplist's tail element to fix\n     * the merge seam. */\n    target = __ziplistCascadeUpdate(target, target+first_offset);\n\n    /* Now free and NULL out what we didn't realloc */\n    if (append) {\n        zfree(*second);\n        *second = NULL;\n        *first = target;\n    } else {\n        zfree(*first);\n        *first = NULL;\n        *second = target;\n    }\n    return target;\n}\n\nunsigned char *ziplistPush(unsigned char *zl, unsigned char *s, unsigned int slen, int where) {\n    unsigned char *p;\n    p = (where == ZIPLIST_HEAD) ? ZIPLIST_ENTRY_HEAD(zl) : ZIPLIST_ENTRY_END(zl);\n    return __ziplistInsert(zl,p,s,slen);\n}\n\n/* Returns an offset to use for iterating with ziplistNext. When the given\n * index is negative, the list is traversed back to front. When the list\n * doesn't contain an element at the provided index, NULL is returned. */\nunsigned char *ziplistIndex(unsigned char *zl, int index) {\n    unsigned char *p;\n    unsigned int prevlensize, prevlen = 0;\n    size_t zlbytes = intrev32ifbe(ZIPLIST_BYTES(zl));\n    if (index < 0) {\n        index = (-index)-1;\n        p = ZIPLIST_ENTRY_TAIL(zl);\n        if (p[0] != ZIP_END) {\n            /* No need for \"safe\" check: when going backwards, we know the header\n             * we're parsing is in the range, we just need to assert (below) that\n             * the size we take doesn't cause p to go outside the allocation. */\n            ZIP_DECODE_PREVLEN(p, prevlensize, prevlen);\n            while (prevlen > 0 && index--) {\n                p -= prevlen;\n                assert(p >= zl + ZIPLIST_HEADER_SIZE && p < zl + zlbytes - ZIPLIST_END_SIZE);\n                ZIP_DECODE_PREVLEN(p, prevlensize, prevlen);\n            }\n        }\n    } else {\n        p = ZIPLIST_ENTRY_HEAD(zl);\n        while (index--) {\n            /* Use the \"safe\" length: When we go forward, we need to be careful\n             * not to decode an entry header if it's past the ziplist allocation. */\n            p += zipRawEntryLengthSafe(zl, zlbytes, p);\n            if (p[0] == ZIP_END)\n                break;\n        }\n    }\n    if (p[0] == ZIP_END || index > 0)\n        return NULL;\n    zipAssertValidEntry(zl, zlbytes, p);\n    return p;\n}\n\n/* Return pointer to next entry in ziplist.\n *\n * zl is the pointer to the ziplist\n * p is the pointer to the current element\n *\n * The element after 'p' is returned, otherwise NULL if we are at the end. */\nunsigned char *ziplistNext(unsigned char *zl, unsigned char *p) {\n    ((void) zl);\n    size_t zlbytes = intrev32ifbe(ZIPLIST_BYTES(zl));\n\n    /* \"p\" could be equal to ZIP_END, caused by ziplistDelete,\n     * and we should return NULL. Otherwise, we should return NULL\n     * when the *next* element is ZIP_END (there is no next entry). */\n    if (p[0] == ZIP_END) {\n        return NULL;\n    }\n\n    p += zipRawEntryLength(p);\n    if (p[0] == ZIP_END) {\n        return NULL;\n    }\n\n    zipAssertValidEntry(zl, zlbytes, p);\n    return p;\n}\n\n/* Return pointer to previous entry in ziplist. */\nunsigned char *ziplistPrev(unsigned char *zl, unsigned char *p) {\n    unsigned int prevlensize, prevlen = 0;\n\n    /* Iterating backwards from ZIP_END should return the tail. When \"p\" is\n     * equal to the first element of the list, we're already at the head,\n     * and should return NULL. */\n    if (p[0] == ZIP_END) {\n        p = ZIPLIST_ENTRY_TAIL(zl);\n        return (p[0] == ZIP_END) ? NULL : p;\n    } else if (p == ZIPLIST_ENTRY_HEAD(zl)) {\n        return NULL;\n    } else {\n        ZIP_DECODE_PREVLEN(p, prevlensize, prevlen);\n        assert(prevlen > 0);\n        p-=prevlen;\n        size_t zlbytes = intrev32ifbe(ZIPLIST_BYTES(zl));\n        zipAssertValidEntry(zl, zlbytes, p);\n        return p;\n    }\n}\n\n/* Get entry pointed to by 'p' and store in either '*sstr' or 'sval' depending\n * on the encoding of the entry. '*sstr' is always set to NULL to be able\n * to find out whether the string pointer or the integer value was set.\n * Return 0 if 'p' points to the end of the ziplist, 1 otherwise. */\nunsigned int ziplistGet(unsigned char *p, unsigned char **sstr, unsigned int *slen, long long *sval) {\n    zlentry entry;\n    if (p == NULL || p[0] == ZIP_END) return 0;\n    if (sstr) *sstr = NULL;\n\n    zipEntry(p, &entry); /* no need for \"safe\" variant since the input pointer was validated by the function that returned it. */\n    if (ZIP_IS_STR(entry.encoding)) {\n        if (sstr) {\n            *slen = entry.len;\n            *sstr = p+entry.headersize;\n        }\n    } else {\n        if (sval) {\n            *sval = zipLoadInteger(p+entry.headersize,entry.encoding);\n        }\n    }\n    return 1;\n}\n\n/* Insert an entry at \"p\". */\nunsigned char *ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {\n    return __ziplistInsert(zl,p,s,slen);\n}\n\n/* Delete a single entry from the ziplist, pointed to by *p.\n * Also update *p in place, to be able to iterate over the\n * ziplist, while deleting entries. */\nunsigned char *ziplistDelete(unsigned char *zl, unsigned char **p) {\n    size_t offset = *p-zl;\n    zl = __ziplistDelete(zl,*p,1);\n\n    /* Store pointer to current element in p, because ziplistDelete will\n     * do a realloc which might result in a different \"zl\"-pointer.\n     * When the delete direction is back to front, we might delete the last\n     * entry and end up with \"p\" pointing to ZIP_END, so check this. */\n    *p = zl+offset;\n    return zl;\n}\n\n/* Delete a range of entries from the ziplist. */\nunsigned char *ziplistDeleteRange(unsigned char *zl, int index, unsigned int num) {\n    unsigned char *p = ziplistIndex(zl,index);\n    return (p == NULL) ? zl : __ziplistDelete(zl,p,num);\n}\n\n/* Replaces the entry at p. This is equivalent to a delete and an insert,\n * but avoids some overhead when replacing a value of the same size. */\nunsigned char *ziplistReplace(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {\n\n    /* get metadata of the current entry */\n    zlentry entry;\n    zipEntry(p, &entry);\n\n    /* compute length of entry to store, excluding prevlen */\n    unsigned int reqlen;\n    unsigned char encoding = 0;\n    long long value = 123456789; /* initialized to avoid warning. */\n    if (zipTryEncoding(s,slen,&value,&encoding)) {\n        reqlen = zipIntSize(encoding); /* encoding is set */\n    } else {\n        reqlen = slen; /* encoding == 0 */\n    }\n    reqlen += zipStoreEntryEncoding(NULL,encoding,slen);\n\n    if (reqlen == entry.lensize + entry.len) {\n        /* Simply overwrite the element. */\n        p += entry.prevrawlensize;\n        p += zipStoreEntryEncoding(p,encoding,slen);\n        if (ZIP_IS_STR(encoding)) {\n            memcpy(p,s,slen);\n        } else {\n            zipSaveInteger(p,value,encoding);\n        }\n    } else {\n        /* Fallback. */\n        zl = ziplistDelete(zl,&p);\n        zl = ziplistInsert(zl,p,s,slen);\n    }\n    return zl;\n}\n\n/* Compare entry pointer to by 'p' with 'sstr' of length 'slen'. */\n/* Return 1 if equal. */\nunsigned int ziplistCompare(unsigned char *p, unsigned char *sstr, unsigned int slen) {\n    zlentry entry;\n    unsigned char sencoding;\n    long long zval, sval;\n    if (p[0] == ZIP_END) return 0;\n\n    zipEntry(p, &entry); /* no need for \"safe\" variant since the input pointer was validated by the function that returned it. */\n    if (ZIP_IS_STR(entry.encoding)) {\n        /* Raw compare */\n        if (entry.len == slen) {\n            return memcmp(p+entry.headersize,sstr,slen) == 0;\n        } else {\n            return 0;\n        }\n    } else {\n        /* Try to compare encoded values. Don't compare encoding because\n         * different implementations may encoded integers differently. */\n        if (zipTryEncoding(sstr,slen,&sval,&sencoding)) {\n          zval = zipLoadInteger(p+entry.headersize,entry.encoding);\n          return zval == sval;\n        }\n    }\n    return 0;\n}\n\n/* Find pointer to the entry equal to the specified entry. Skip 'skip' entries\n * between every comparison. Returns NULL when the field could not be found. */\nunsigned char *ziplistFind(unsigned char *zl, unsigned char *p, unsigned char *vstr, unsigned int vlen, unsigned int skip) {\n    int skipcnt = 0;\n    unsigned char vencoding = 0;\n    long long vll = 0;\n    size_t zlbytes = ziplistBlobLen(zl);\n\n    while (p[0] != ZIP_END) {\n        struct zlentry e;\n        unsigned char *q;\n\n        assert(zipEntrySafe(zl, zlbytes, p, &e, 1));\n        q = p + e.prevrawlensize + e.lensize;\n\n        if (skipcnt == 0) {\n            /* Compare current entry with specified entry */\n            if (ZIP_IS_STR(e.encoding)) {\n                if (e.len == vlen && memcmp(q, vstr, vlen) == 0) {\n                    return p;\n                }\n            } else {\n                /* Find out if the searched field can be encoded. Note that\n                 * we do it only the first time, once done vencoding is set\n                 * to non-zero and vll is set to the integer value. */\n                if (vencoding == 0) {\n                    if (!zipTryEncoding(vstr, vlen, &vll, &vencoding)) {\n                        /* If the entry can't be encoded we set it to\n                         * UCHAR_MAX so that we don't retry again the next\n                         * time. */\n                        vencoding = UCHAR_MAX;\n                    }\n                    /* Must be non-zero by now */\n                    assert(vencoding);\n                }\n\n                /* Compare current entry with specified entry, do it only\n                 * if vencoding != UCHAR_MAX because if there is no encoding\n                 * possible for the field it can't be a valid integer. */\n                if (vencoding != UCHAR_MAX) {\n                    long long ll = zipLoadInteger(q, e.encoding);\n                    if (ll == vll) {\n                        return p;\n                    }\n                }\n            }\n\n            /* Reset skip count */\n            skipcnt = skip;\n        } else {\n            /* Skip entry */\n            skipcnt--;\n        }\n\n        /* Move to next entry */\n        p = q + e.len;\n    }\n\n    return NULL;\n}\n\n/* Return length of ziplist. */\nunsigned int ziplistLen(const unsigned char *zl) {\n    unsigned int len = 0;\n    if (intrev16ifbe(ZIPLIST_LENGTH(zl)) < UINT16_MAX) {\n        len = intrev16ifbe(ZIPLIST_LENGTH(zl));\n    } else {\n        const unsigned char *p = zl+ZIPLIST_HEADER_SIZE;\n        size_t zlbytes = intrev32ifbe(ZIPLIST_BYTES(zl));\n        while (*p != ZIP_END) {\n            p += zipRawEntryLengthSafe((unsigned char*)zl, zlbytes, p);\n            len++;\n        }\n\n        /* Re-store length if small enough */\n        if (len < UINT16_MAX) ZIPLIST_LENGTH(zl) = intrev16ifbe(len);\n    }\n    return len;\n}\n\n/* Return ziplist blob size in bytes. */\nsize_t ziplistBlobLen(unsigned char *zl) {\n    return intrev32ifbe(ZIPLIST_BYTES(zl));\n}\n\nvoid ziplistRepr(unsigned char *zl) {\n    unsigned char *p;\n    int index = 0;\n    zlentry entry;\n    size_t zlbytes = ziplistBlobLen(zl);\n\n    printf(\n        \"{total bytes %u} \"\n        \"{num entries %u}\\n\"\n        \"{tail offset %u}\\n\",\n        intrev32ifbe(ZIPLIST_BYTES(zl)),\n        intrev16ifbe(ZIPLIST_LENGTH(zl)),\n        intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl)));\n    p = ZIPLIST_ENTRY_HEAD(zl);\n    while(*p != ZIP_END) {\n        assert(zipEntrySafe(zl, zlbytes, p, &entry, 1));\n        printf(\n            \"{\\n\"\n                \"\\taddr 0x%08lx,\\n\"\n                \"\\tindex %2d,\\n\"\n                \"\\toffset %5lu,\\n\"\n                \"\\thdr+entry len: %5u,\\n\"\n                \"\\thdr len%2u,\\n\"\n                \"\\tprevrawlen: %5u,\\n\"\n                \"\\tprevrawlensize: %2u,\\n\"\n                \"\\tpayload %5u\\n\",\n            (long unsigned)p,\n            index,\n            (unsigned long) (p-zl),\n            entry.headersize+entry.len,\n            entry.headersize,\n            entry.prevrawlen,\n            entry.prevrawlensize,\n            entry.len);\n        printf(\"\\tbytes: \");\n        for (unsigned int i = 0; i < entry.headersize+entry.len; i++) {\n            printf(\"%02x|\",p[i]);\n        }\n        printf(\"\\n\");\n        p += entry.headersize;\n        if (ZIP_IS_STR(entry.encoding)) {\n            printf(\"\\t[str]\");\n            if (entry.len > 40) {\n                if (fwrite(p,40,1,stdout) == 0) perror(\"fwrite\");\n                printf(\"...\");\n            } else {\n                if (entry.len &&\n                    fwrite(p,entry.len,1,stdout) == 0) perror(\"fwrite\");\n            }\n        } else {\n            printf(\"\\t[int]%lld\", (long long) zipLoadInteger(p,entry.encoding));\n        }\n        printf(\"\\n}\\n\");\n        p += entry.len;\n        index++;\n    }\n    printf(\"{end}\\n\\n\");\n}\n\n/* Validate the integrity of the data structure.\n * when `deep` is 0, only the integrity of the header is validated.\n * when `deep` is 1, we scan all the entries one by one. */\nint ziplistValidateIntegrity(unsigned char *zl, size_t size, int deep,\n    ziplistValidateEntryCB entry_cb, void *cb_userdata) {\n    /* check that we can actually read the header. (and ZIP_END) */\n    if (size < ZIPLIST_HEADER_SIZE + ZIPLIST_END_SIZE)\n        return 0;\n\n    /* check that the encoded size in the header must match the allocated size. */\n    size_t bytes = intrev32ifbe(ZIPLIST_BYTES(zl));\n    if (bytes != size)\n        return 0;\n\n    /* the last byte must be the terminator. */\n    if (zl[size - ZIPLIST_END_SIZE] != ZIP_END)\n        return 0;\n\n    /* make sure the tail offset isn't reaching outside the allocation. */\n    if (intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl)) > size - ZIPLIST_END_SIZE)\n        return 0;\n\n    if (!deep)\n        return 1;\n\n    unsigned int count = 0;\n    unsigned char *p = ZIPLIST_ENTRY_HEAD(zl);\n    unsigned char *prev = NULL;\n    size_t prev_raw_size = 0;\n    while(*p != ZIP_END) {\n        struct zlentry e;\n        /* Decode the entry headers and fail if invalid or reaches outside the allocation */\n        if (!zipEntrySafe(zl, size, p, &e, 1))\n            return 0;\n\n        /* Make sure the record stating the prev entry size is correct. */\n        if (e.prevrawlen != prev_raw_size)\n            return 0;\n\n        /* Optionally let the caller validate the entry too. */\n        if (entry_cb && !entry_cb(p, cb_userdata))\n            return 0;\n\n        /* Move to the next entry */\n        prev_raw_size = e.headersize + e.len;\n        prev = p;\n        p += e.headersize + e.len;\n        count++;\n    }\n\n    /* Make sure 'p' really does point to the end of the ziplist. */\n    if (p != zl + bytes - ZIPLIST_END_SIZE)\n        return 0;\n\n    /* Make sure the <zltail> entry really do point to the start of the last entry. */\n    if (prev != NULL && prev != ZIPLIST_ENTRY_TAIL(zl))\n        return 0;\n\n    /* Check that the count in the header is correct */\n    unsigned int header_count = intrev16ifbe(ZIPLIST_LENGTH(zl));\n    if (header_count != UINT16_MAX && count != header_count)\n        return 0;\n\n    return 1;\n}\n\n/* Randomly select a pair of key and value.\n * total_count is a pre-computed length/2 of the ziplist (to avoid calls to ziplistLen)\n * 'key' and 'val' are used to store the result key value pair.\n * 'val' can be NULL if the value is not needed. */\nvoid ziplistRandomPair(unsigned char *zl, unsigned long total_count, ziplistEntry *key, ziplistEntry *val) {\n    int ret;\n    unsigned char *p;\n\n    /* Avoid div by zero on corrupt ziplist */\n    assert(total_count);\n\n    /* Generate even numbers, because ziplist saved K-V pair */\n    int r = (rand() % total_count) * 2;\n    p = ziplistIndex(zl, r);\n    ret = ziplistGet(p, &key->sval, &key->slen, &key->lval);\n    assert(ret != 0);\n\n    if (!val)\n        return;\n    p = ziplistNext(zl, p);\n    ret = ziplistGet(p, &val->sval, &val->slen, &val->lval);\n    assert(ret != 0);\n}\n\n/* int compare for qsort */\nint uintCompare(const void *a, const void *b) {\n    return (*(unsigned int *) a - *(unsigned int *) b);\n}\n\n/* Helper method to store a string into from val or lval into dest */\nstatic inline void ziplistSaveValue(unsigned char *val, unsigned int len, long long lval, ziplistEntry *dest) {\n    dest->sval = val;\n    dest->slen = len;\n    dest->lval = lval;\n}\n\n/* Randomly select count of key value pairs and store into 'keys' and\n * 'vals' args. The order of the picked entries is random, and the selections\n * are non-unique (repetitions are possible).\n * The 'vals' arg can be NULL in which case we skip these. */\nvoid ziplistRandomPairs(unsigned char *zl, unsigned int count, ziplistEntry *keys, ziplistEntry *vals) {\n    unsigned char *p, *key, *value;\n    unsigned int klen = 0, vlen = 0;\n    long long klval = 0, vlval = 0;\n\n    /* Notice: the index member must be first due to the use in uintCompare */\n    typedef struct {\n        unsigned int index;\n        unsigned int order;\n    } rand_pick;\n    rand_pick *picks = zmalloc(sizeof(rand_pick)*count, MALLOC_LOCAL);\n    unsigned int total_size = ziplistLen(zl)/2;\n\n    /* Avoid div by zero on corrupt ziplist */\n    assert(total_size);\n\n    /* create a pool of random indexes (some may be duplicate). */\n    for (unsigned int i = 0; i < count; i++) {\n        picks[i].index = (rand() % total_size) * 2; /* Generate even indexes */\n        /* keep track of the order we picked them */\n        picks[i].order = i;\n    }\n\n    /* sort by indexes. */\n    qsort(picks, count, sizeof(rand_pick), uintCompare);\n\n    /* fetch the elements form the ziplist into a output array respecting the original order. */\n    unsigned int zipindex = 0, pickindex = 0;\n    p = ziplistIndex(zl, 0);\n    while (ziplistGet(p, &key, &klen, &klval) && pickindex < count) {\n        p = ziplistNext(zl, p);\n        assert(ziplistGet(p, &value, &vlen, &vlval));\n        while (pickindex < count && zipindex == picks[pickindex].index) {\n            int storeorder = picks[pickindex].order;\n            ziplistSaveValue(key, klen, klval, &keys[storeorder]);\n            if (vals)\n                ziplistSaveValue(value, vlen, vlval, &vals[storeorder]);\n             pickindex++;\n        }\n        zipindex += 2;\n        p = ziplistNext(zl, p);\n    }\n\n    zfree(picks);\n}\n\n/* Randomly select count of key value pairs and store into 'keys' and\n * 'vals' args. The selections are unique (no repetitions), and the order of\n * the picked entries is NOT-random.\n * The 'vals' arg can be NULL in which case we skip these.\n * The return value is the number of items picked which can be lower than the\n * requested count if the ziplist doesn't hold enough pairs. */\nunsigned int ziplistRandomPairsUnique(unsigned char *zl, unsigned int count, ziplistEntry *keys, ziplistEntry *vals) {\n    unsigned char *p, *key;\n    unsigned int klen = 0;\n    long long klval = 0;\n    unsigned int total_size = ziplistLen(zl)/2;\n    unsigned int index = 0;\n    if (count > total_size)\n        count = total_size;\n\n    /* To only iterate once, every time we try to pick a member, the probability\n     * we pick it is the quotient of the count left we want to pick and the\n     * count still we haven't visited in the dict, this way, we could make every\n     * member be equally picked.*/\n    p = ziplistIndex(zl, 0);\n    unsigned int picked = 0, remaining = count;\n    while (picked < count && p) {\n        double randomDouble = ((double)rand()) / RAND_MAX;\n        double threshold = ((double)remaining) / (total_size - index);\n        if (randomDouble <= threshold) {\n            assert(ziplistGet(p, &key, &klen, &klval));\n            ziplistSaveValue(key, klen, klval, &keys[picked]);\n            p = ziplistNext(zl, p);\n            assert(p);\n            if (vals) {\n                assert(ziplistGet(p, &key, &klen, &klval));\n                ziplistSaveValue(key, klen, klval, &vals[picked]);\n            }\n            remaining--;\n            picked++;\n        } else {\n            p = ziplistNext(zl, p);\n            assert(p);\n        }\n        p = ziplistNext(zl, p);\n        index++;\n    }\n    return picked;\n}\n\n#ifdef REDIS_TEST\n#include <sys/time.h>\n#include \"adlist.h\"\n#include \"sds.h\"\n\n#define debug(f, ...) { if (DEBUG) printf(f, __VA_ARGS__); }\n\nstatic unsigned char *createList() {\n    unsigned char *zl = ziplistNew();\n    zl = ziplistPush(zl, (unsigned char*)\"foo\", 3, ZIPLIST_TAIL);\n    zl = ziplistPush(zl, (unsigned char*)\"quux\", 4, ZIPLIST_TAIL);\n    zl = ziplistPush(zl, (unsigned char*)\"hello\", 5, ZIPLIST_HEAD);\n    zl = ziplistPush(zl, (unsigned char*)\"1024\", 4, ZIPLIST_TAIL);\n    return zl;\n}\n\nstatic unsigned char *createIntList() {\n    unsigned char *zl = ziplistNew();\n    char buf[32];\n\n    snprintf(buf, sizeof(buf), \"100\");\n    zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);\n    snprintf(buf, sizeof(buf), \"128000\");\n    zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);\n    snprintf(buf, sizeof(buf), \"-100\");\n    zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_HEAD);\n    snprintf(buf, sizeof(buf), \"4294967296\");\n    zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_HEAD);\n    snprintf(buf, sizeof(buf), \"non integer\");\n    zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);\n    snprintf(buf, sizeof(buf), \"much much longer non integer\");\n    zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);\n    return zl;\n}\n\nstatic long long usec(void) {\n    struct timeval tv;\n    gettimeofday(&tv,NULL);\n    return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;\n}\n\nstatic void stress(int pos, int num, int maxsize, int dnum) {\n    int i,j,k;\n    unsigned char *zl;\n    char posstr[2][5] = { \"HEAD\", \"TAIL\" };\n    long long start;\n    for (i = 0; i < maxsize; i+=dnum) {\n        zl = ziplistNew();\n        for (j = 0; j < i; j++) {\n            zl = ziplistPush(zl,(unsigned char*)\"quux\",4,ZIPLIST_TAIL);\n        }\n\n        /* Do num times a push+pop from pos */\n        start = usec();\n        for (k = 0; k < num; k++) {\n            zl = ziplistPush(zl,(unsigned char*)\"quux\",4,pos);\n            zl = ziplistDeleteRange(zl,0,1);\n        }\n        printf(\"List size: %8d, bytes: %8d, %dx push+pop (%s): %6lld usec\\n\",\n            i,intrev32ifbe(ZIPLIST_BYTES(zl)),num,posstr[pos],usec()-start);\n        zfree(zl);\n    }\n}\n\nstatic unsigned char *pop(unsigned char *zl, int where) {\n    unsigned char *p, *vstr;\n    unsigned int vlen;\n    long long vlong;\n\n    p = ziplistIndex(zl,where == ZIPLIST_HEAD ? 0 : -1);\n    if (ziplistGet(p,&vstr,&vlen,&vlong)) {\n        if (where == ZIPLIST_HEAD)\n            printf(\"Pop head: \");\n        else\n            printf(\"Pop tail: \");\n\n        if (vstr) {\n            if (vlen && fwrite(vstr,vlen,1,stdout) == 0) perror(\"fwrite\");\n        }\n        else {\n            printf(\"%lld\", vlong);\n        }\n\n        printf(\"\\n\");\n        return ziplistDelete(zl,&p);\n    } else {\n        printf(\"ERROR: Could not pop\\n\");\n        exit(1);\n    }\n}\n\nstatic int randstring(char *target, unsigned int min, unsigned int max) {\n    int p = 0;\n    int len = min+rand()%(max-min+1);\n    int minval, maxval;\n    switch(rand() % 3) {\n    case 0:\n        minval = 0;\n        maxval = 255;\n    break;\n    case 1:\n        minval = 48;\n        maxval = 122;\n    break;\n    case 2:\n        minval = 48;\n        maxval = 52;\n    break;\n    default:\n        assert(NULL);\n    }\n\n    while(p < len)\n        target[p++] = minval+rand()%(maxval-minval+1);\n    return len;\n}\n\nstatic void verify(unsigned char *zl, zlentry *e) {\n    int len = ziplistLen(zl);\n    zlentry _e;\n\n    ZIPLIST_ENTRY_ZERO(&_e);\n\n    for (int i = 0; i < len; i++) {\n        memset(&e[i], 0, sizeof(zlentry));\n        zipEntry(ziplistIndex(zl, i), &e[i]);\n\n        memset(&_e, 0, sizeof(zlentry));\n        zipEntry(ziplistIndex(zl, -len+i), &_e);\n\n        assert(memcmp(&e[i], &_e, sizeof(zlentry)) == 0);\n    }\n}\n\nstatic unsigned char *insertHelper(unsigned char *zl, char ch, size_t len, unsigned char *pos) {\n    assert(len <= ZIP_BIG_PREVLEN);\n    unsigned char data[ZIP_BIG_PREVLEN] = {0};\n    memset(data, ch, len);\n    return ziplistInsert(zl, pos, data, len);\n}\n\nstatic int compareHelper(unsigned char *zl, char ch, size_t len, int index) {\n    assert(len <= ZIP_BIG_PREVLEN);\n    unsigned char data[ZIP_BIG_PREVLEN] = {0};\n    memset(data, ch, len);\n    unsigned char *p = ziplistIndex(zl, index);\n    assert(p != NULL);\n    return ziplistCompare(p, data, len);\n}\n\nstatic size_t strEntryBytesSmall(size_t slen) {\n    return slen + zipStorePrevEntryLength(NULL, 0) + zipStoreEntryEncoding(NULL, 0, slen);\n}\n\nstatic size_t strEntryBytesLarge(size_t slen) {\n    return slen + zipStorePrevEntryLength(NULL, ZIP_BIG_PREVLEN) + zipStoreEntryEncoding(NULL, 0, slen);\n}\n\n/* ./redis-server test ziplist <randomseed> --accurate */\nint ziplistTest(int argc, char **argv, int accurate) {\n    unsigned char *zl, *p;\n    unsigned char *entry;\n    unsigned int elen;\n    long long value;\n    int iteration;\n\n    /* If an argument is given, use it as the random seed. */\n    if (argc >= 4)\n        srand(atoi(argv[3]));\n\n    zl = createIntList();\n    ziplistRepr(zl);\n\n    zfree(zl);\n\n    zl = createList();\n    ziplistRepr(zl);\n\n    zl = pop(zl,ZIPLIST_TAIL);\n    ziplistRepr(zl);\n\n    zl = pop(zl,ZIPLIST_HEAD);\n    ziplistRepr(zl);\n\n    zl = pop(zl,ZIPLIST_TAIL);\n    ziplistRepr(zl);\n\n    zl = pop(zl,ZIPLIST_TAIL);\n    ziplistRepr(zl);\n\n    zfree(zl);\n\n    printf(\"Get element at index 3:\\n\");\n    {\n        zl = createList();\n        p = ziplistIndex(zl, 3);\n        if (!ziplistGet(p, &entry, &elen, &value)) {\n            printf(\"ERROR: Could not access index 3\\n\");\n            return 1;\n        }\n        if (entry) {\n            if (elen && fwrite(entry,elen,1,stdout) == 0) perror(\"fwrite\");\n            printf(\"\\n\");\n        } else {\n            printf(\"%lld\\n\", value);\n        }\n        printf(\"\\n\");\n        zfree(zl);\n    }\n\n    printf(\"Get element at index 4 (out of range):\\n\");\n    {\n        zl = createList();\n        p = ziplistIndex(zl, 4);\n        if (p == NULL) {\n            printf(\"No entry\\n\");\n        } else {\n            printf(\"ERROR: Out of range index should return NULL, returned offset: %ld\\n\", (long)(p-zl));\n            return 1;\n        }\n        printf(\"\\n\");\n        zfree(zl);\n    }\n\n    printf(\"Get element at index -1 (last element):\\n\");\n    {\n        zl = createList();\n        p = ziplistIndex(zl, -1);\n        if (!ziplistGet(p, &entry, &elen, &value)) {\n            printf(\"ERROR: Could not access index -1\\n\");\n            return 1;\n        }\n        if (entry) {\n            if (elen && fwrite(entry,elen,1,stdout) == 0) perror(\"fwrite\");\n            printf(\"\\n\");\n        } else {\n            printf(\"%lld\\n\", value);\n        }\n        printf(\"\\n\");\n        zfree(zl);\n    }\n\n    printf(\"Get element at index -4 (first element):\\n\");\n    {\n        zl = createList();\n        p = ziplistIndex(zl, -4);\n        if (!ziplistGet(p, &entry, &elen, &value)) {\n            printf(\"ERROR: Could not access index -4\\n\");\n            return 1;\n        }\n        if (entry) {\n            if (elen && fwrite(entry,elen,1,stdout) == 0) perror(\"fwrite\");\n            printf(\"\\n\");\n        } else {\n            printf(\"%lld\\n\", value);\n        }\n        printf(\"\\n\");\n        zfree(zl);\n    }\n\n    printf(\"Get element at index -5 (reverse out of range):\\n\");\n    {\n        zl = createList();\n        p = ziplistIndex(zl, -5);\n        if (p == NULL) {\n            printf(\"No entry\\n\");\n        } else {\n            printf(\"ERROR: Out of range index should return NULL, returned offset: %ld\\n\", (long)(p-zl));\n            return 1;\n        }\n        printf(\"\\n\");\n        zfree(zl);\n    }\n\n    printf(\"Iterate list from 0 to end:\\n\");\n    {\n        zl = createList();\n        p = ziplistIndex(zl, 0);\n        while (ziplistGet(p, &entry, &elen, &value)) {\n            printf(\"Entry: \");\n            if (entry) {\n                if (elen && fwrite(entry,elen,1,stdout) == 0) perror(\"fwrite\");\n            } else {\n                printf(\"%lld\", value);\n            }\n            p = ziplistNext(zl,p);\n            printf(\"\\n\");\n        }\n        printf(\"\\n\");\n        zfree(zl);\n    }\n\n    printf(\"Iterate list from 1 to end:\\n\");\n    {\n        zl = createList();\n        p = ziplistIndex(zl, 1);\n        while (ziplistGet(p, &entry, &elen, &value)) {\n            printf(\"Entry: \");\n            if (entry) {\n                if (elen && fwrite(entry,elen,1,stdout) == 0) perror(\"fwrite\");\n            } else {\n                printf(\"%lld\", value);\n            }\n            p = ziplistNext(zl,p);\n            printf(\"\\n\");\n        }\n        printf(\"\\n\");\n        zfree(zl);\n    }\n\n    printf(\"Iterate list from 2 to end:\\n\");\n    {\n        zl = createList();\n        p = ziplistIndex(zl, 2);\n        while (ziplistGet(p, &entry, &elen, &value)) {\n            printf(\"Entry: \");\n            if (entry) {\n                if (elen && fwrite(entry,elen,1,stdout) == 0) perror(\"fwrite\");\n            } else {\n                printf(\"%lld\", value);\n            }\n            p = ziplistNext(zl,p);\n            printf(\"\\n\");\n        }\n        printf(\"\\n\");\n        zfree(zl);\n    }\n\n    printf(\"Iterate starting out of range:\\n\");\n    {\n        zl = createList();\n        p = ziplistIndex(zl, 4);\n        if (!ziplistGet(p, &entry, &elen, &value)) {\n            printf(\"No entry\\n\");\n        } else {\n            printf(\"ERROR\\n\");\n        }\n        printf(\"\\n\");\n        zfree(zl);\n    }\n\n    printf(\"Iterate from back to front:\\n\");\n    {\n        zl = createList();\n        p = ziplistIndex(zl, -1);\n        while (ziplistGet(p, &entry, &elen, &value)) {\n            printf(\"Entry: \");\n            if (entry) {\n                if (elen && fwrite(entry,elen,1,stdout) == 0) perror(\"fwrite\");\n            } else {\n                printf(\"%lld\", value);\n            }\n            p = ziplistPrev(zl,p);\n            printf(\"\\n\");\n        }\n        printf(\"\\n\");\n        zfree(zl);\n    }\n\n    printf(\"Iterate from back to front, deleting all items:\\n\");\n    {\n        zl = createList();\n        p = ziplistIndex(zl, -1);\n        while (ziplistGet(p, &entry, &elen, &value)) {\n            printf(\"Entry: \");\n            if (entry) {\n                if (elen && fwrite(entry,elen,1,stdout) == 0) perror(\"fwrite\");\n            } else {\n                printf(\"%lld\", value);\n            }\n            zl = ziplistDelete(zl,&p);\n            p = ziplistPrev(zl,p);\n            printf(\"\\n\");\n        }\n        printf(\"\\n\");\n        zfree(zl);\n    }\n\n    printf(\"Delete inclusive range 0,0:\\n\");\n    {\n        zl = createList();\n        zl = ziplistDeleteRange(zl, 0, 1);\n        ziplistRepr(zl);\n        zfree(zl);\n    }\n\n    printf(\"Delete inclusive range 0,1:\\n\");\n    {\n        zl = createList();\n        zl = ziplistDeleteRange(zl, 0, 2);\n        ziplistRepr(zl);\n        zfree(zl);\n    }\n\n    printf(\"Delete inclusive range 1,2:\\n\");\n    {\n        zl = createList();\n        zl = ziplistDeleteRange(zl, 1, 2);\n        ziplistRepr(zl);\n        zfree(zl);\n    }\n\n    printf(\"Delete with start index out of range:\\n\");\n    {\n        zl = createList();\n        zl = ziplistDeleteRange(zl, 5, 1);\n        ziplistRepr(zl);\n        zfree(zl);\n    }\n\n    printf(\"Delete with num overflow:\\n\");\n    {\n        zl = createList();\n        zl = ziplistDeleteRange(zl, 1, 5);\n        ziplistRepr(zl);\n        zfree(zl);\n    }\n\n    printf(\"Delete foo while iterating:\\n\");\n    {\n        zl = createList();\n        p = ziplistIndex(zl,0);\n        while (ziplistGet(p,&entry,&elen,&value)) {\n            if (entry && strncmp(\"foo\",(char*)entry,elen) == 0) {\n                printf(\"Delete foo\\n\");\n                zl = ziplistDelete(zl,&p);\n            } else {\n                printf(\"Entry: \");\n                if (entry) {\n                    if (elen && fwrite(entry,elen,1,stdout) == 0)\n                        perror(\"fwrite\");\n                } else {\n                    printf(\"%lld\",value);\n                }\n                p = ziplistNext(zl,p);\n                printf(\"\\n\");\n            }\n        }\n        printf(\"\\n\");\n        ziplistRepr(zl);\n        zfree(zl);\n    }\n\n    printf(\"Replace with same size:\\n\");\n    {\n        zl = createList(); /* \"hello\", \"foo\", \"quux\", \"1024\" */\n        unsigned char *orig_zl = zl;\n        p = ziplistIndex(zl, 0);\n        zl = ziplistReplace(zl, p, (unsigned char*)\"zoink\", 5);\n        p = ziplistIndex(zl, 3);\n        zl = ziplistReplace(zl, p, (unsigned char*)\"yy\", 2);\n        p = ziplistIndex(zl, 1);\n        zl = ziplistReplace(zl, p, (unsigned char*)\"65536\", 5);\n        p = ziplistIndex(zl, 0);\n        assert(!memcmp((char*)p,\n                       \"\\x00\\x05zoink\"\n                       \"\\x07\\xf0\\x00\\x00\\x01\" /* 65536 as int24 */\n                       \"\\x05\\x04quux\" \"\\x06\\x02yy\" \"\\xff\",\n                       23));\n        assert(zl == orig_zl); /* no reallocations have happened */\n        zfree(zl);\n        printf(\"SUCCESS\\n\\n\");\n    }\n\n    printf(\"Replace with different size:\\n\");\n    {\n        zl = createList(); /* \"hello\", \"foo\", \"quux\", \"1024\" */\n        p = ziplistIndex(zl, 1);\n        zl = ziplistReplace(zl, p, (unsigned char*)\"squirrel\", 8);\n        p = ziplistIndex(zl, 0);\n        assert(!strncmp((char*)p,\n                        \"\\x00\\x05hello\" \"\\x07\\x08squirrel\" \"\\x0a\\x04quux\"\n                        \"\\x06\\xc0\\x00\\x04\" \"\\xff\",\n                        28));\n        zfree(zl);\n        printf(\"SUCCESS\\n\\n\");\n    }\n\n    printf(\"Regression test for >255 byte strings:\\n\");\n    {\n        char v1[257] = {0}, v2[257] = {0};\n        memset(v1,'x',256);\n        memset(v2,'y',256);\n        zl = ziplistNew();\n        zl = ziplistPush(zl,(unsigned char*)v1,strlen(v1),ZIPLIST_TAIL);\n        zl = ziplistPush(zl,(unsigned char*)v2,strlen(v2),ZIPLIST_TAIL);\n\n        /* Pop values again and compare their value. */\n        p = ziplistIndex(zl,0);\n        assert(ziplistGet(p,&entry,&elen,&value));\n        assert(strncmp(v1,(char*)entry,elen) == 0);\n        p = ziplistIndex(zl,1);\n        assert(ziplistGet(p,&entry,&elen,&value));\n        assert(strncmp(v2,(char*)entry,elen) == 0);\n        printf(\"SUCCESS\\n\\n\");\n        zfree(zl);\n    }\n\n    printf(\"Regression test deleting next to last entries:\\n\");\n    {\n        char v[3][257] = {{0}};\n        zlentry e[3] = {{.prevrawlensize = 0, .prevrawlen = 0, .lensize = 0,\n                         .len = 0, .headersize = 0, .encoding = 0, .p = NULL}};\n        size_t i;\n\n        for (i = 0; i < (sizeof(v)/sizeof(v[0])); i++) {\n            memset(v[i], 'a' + i, sizeof(v[0]));\n        }\n\n        v[0][256] = '\\0';\n        v[1][  1] = '\\0';\n        v[2][256] = '\\0';\n\n        zl = ziplistNew();\n        for (i = 0; i < (sizeof(v)/sizeof(v[0])); i++) {\n            zl = ziplistPush(zl, (unsigned char *) v[i], strlen(v[i]), ZIPLIST_TAIL);\n        }\n\n        verify(zl, e);\n\n        assert(e[0].prevrawlensize == 1);\n        assert(e[1].prevrawlensize == 5);\n        assert(e[2].prevrawlensize == 1);\n\n        /* Deleting entry 1 will increase `prevrawlensize` for entry 2 */\n        unsigned char *p = e[1].p;\n        zl = ziplistDelete(zl, &p);\n\n        verify(zl, e);\n\n        assert(e[0].prevrawlensize == 1);\n        assert(e[1].prevrawlensize == 5);\n\n        printf(\"SUCCESS\\n\\n\");\n        zfree(zl);\n    }\n\n    printf(\"Create long list and check indices:\\n\");\n    {\n        unsigned long long start = usec();\n        zl = ziplistNew();\n        char buf[32];\n        int i,len;\n        for (i = 0; i < 1000; i++) {\n            len = snprintf(buf,sizeof(buf),\"%d\",i);\n            zl = ziplistPush(zl,(unsigned char*)buf,len,ZIPLIST_TAIL);\n        }\n        for (i = 0; i < 1000; i++) {\n            p = ziplistIndex(zl,i);\n            assert(ziplistGet(p,NULL,NULL,&value));\n            assert(i == value);\n\n            p = ziplistIndex(zl,-i-1);\n            assert(ziplistGet(p,NULL,NULL,&value));\n            assert(999-i == value);\n        }\n        printf(\"SUCCESS. usec=%lld\\n\\n\", usec()-start);\n        zfree(zl);\n    }\n\n    printf(\"Compare strings with ziplist entries:\\n\");\n    {\n        zl = createList();\n        p = ziplistIndex(zl,0);\n        if (!ziplistCompare(p,(unsigned char*)\"hello\",5)) {\n            printf(\"ERROR: not \\\"hello\\\"\\n\");\n            return 1;\n        }\n        if (ziplistCompare(p,(unsigned char*)\"hella\",5)) {\n            printf(\"ERROR: \\\"hella\\\"\\n\");\n            return 1;\n        }\n\n        p = ziplistIndex(zl,3);\n        if (!ziplistCompare(p,(unsigned char*)\"1024\",4)) {\n            printf(\"ERROR: not \\\"1024\\\"\\n\");\n            return 1;\n        }\n        if (ziplistCompare(p,(unsigned char*)\"1025\",4)) {\n            printf(\"ERROR: \\\"1025\\\"\\n\");\n            return 1;\n        }\n        printf(\"SUCCESS\\n\\n\");\n        zfree(zl);\n    }\n\n    printf(\"Merge test:\\n\");\n    {\n        /* create list gives us: [hello, foo, quux, 1024] */\n        zl = createList();\n        unsigned char *zl2 = createList();\n\n        unsigned char *zl3 = ziplistNew();\n        unsigned char *zl4 = ziplistNew();\n\n        if (ziplistMerge(&zl4, &zl4)) {\n            printf(\"ERROR: Allowed merging of one ziplist into itself.\\n\");\n            return 1;\n        }\n\n        /* Merge two empty ziplists, get empty result back. */\n        zl4 = ziplistMerge(&zl3, &zl4);\n        ziplistRepr(zl4);\n        if (ziplistLen(zl4)) {\n            printf(\"ERROR: Merging two empty ziplists created entries.\\n\");\n            return 1;\n        }\n        zfree(zl4);\n\n        zl2 = ziplistMerge(&zl, &zl2);\n        /* merge gives us: [hello, foo, quux, 1024, hello, foo, quux, 1024] */\n        ziplistRepr(zl2);\n\n        if (ziplistLen(zl2) != 8) {\n            printf(\"ERROR: Merged length not 8, but: %u\\n\", ziplistLen(zl2));\n            return 1;\n        }\n\n        p = ziplistIndex(zl2,0);\n        if (!ziplistCompare(p,(unsigned char*)\"hello\",5)) {\n            printf(\"ERROR: not \\\"hello\\\"\\n\");\n            return 1;\n        }\n        if (ziplistCompare(p,(unsigned char*)\"hella\",5)) {\n            printf(\"ERROR: \\\"hella\\\"\\n\");\n            return 1;\n        }\n\n        p = ziplistIndex(zl2,3);\n        if (!ziplistCompare(p,(unsigned char*)\"1024\",4)) {\n            printf(\"ERROR: not \\\"1024\\\"\\n\");\n            return 1;\n        }\n        if (ziplistCompare(p,(unsigned char*)\"1025\",4)) {\n            printf(\"ERROR: \\\"1025\\\"\\n\");\n            return 1;\n        }\n\n        p = ziplistIndex(zl2,4);\n        if (!ziplistCompare(p,(unsigned char*)\"hello\",5)) {\n            printf(\"ERROR: not \\\"hello\\\"\\n\");\n            return 1;\n        }\n        if (ziplistCompare(p,(unsigned char*)\"hella\",5)) {\n            printf(\"ERROR: \\\"hella\\\"\\n\");\n            return 1;\n        }\n\n        p = ziplistIndex(zl2,7);\n        if (!ziplistCompare(p,(unsigned char*)\"1024\",4)) {\n            printf(\"ERROR: not \\\"1024\\\"\\n\");\n            return 1;\n        }\n        if (ziplistCompare(p,(unsigned char*)\"1025\",4)) {\n            printf(\"ERROR: \\\"1025\\\"\\n\");\n            return 1;\n        }\n        printf(\"SUCCESS\\n\\n\");\n        zfree(zl);\n    }\n\n    printf(\"Stress with random payloads of different encoding:\\n\");\n    {\n        unsigned long long start = usec();\n        int i,j,len,where;\n        unsigned char *p;\n        char buf[1024];\n        int buflen;\n        list *ref;\n        listNode *refnode;\n\n        /* Hold temp vars from ziplist */\n        unsigned char *sstr;\n        unsigned int slen;\n        long long sval;\n\n        iteration = accurate ? 20000 : 20;\n        for (i = 0; i < iteration; i++) {\n            zl = ziplistNew();\n            ref = listCreate();\n            listSetFreeMethod(ref,(void (*)(void*))sdsfree);\n            len = rand() % 256;\n\n            /* Create lists */\n            for (j = 0; j < len; j++) {\n                where = (rand() & 1) ? ZIPLIST_HEAD : ZIPLIST_TAIL;\n                if (rand() % 2) {\n                    buflen = randstring(buf,1,sizeof(buf)-1);\n                } else {\n                    switch(rand() % 3) {\n                    case 0:\n                        buflen = snprintf(buf,sizeof(buf),\"%lld\",(0LL + rand()) >> 20);\n                        break;\n                    case 1:\n                        buflen = snprintf(buf,sizeof(buf),\"%lld\",(0LL + rand()));\n                        break;\n                    case 2:\n                        buflen = snprintf(buf,sizeof(buf),\"%lld\",(0LL + rand()) << 20);\n                        break;\n                    default:\n                        assert(NULL);\n                    }\n                }\n\n                /* Add to ziplist */\n                zl = ziplistPush(zl, (unsigned char*)buf, buflen, where);\n\n                /* Add to reference list */\n                if (where == ZIPLIST_HEAD) {\n                    listAddNodeHead(ref,sdsnewlen(buf, buflen));\n                } else if (where == ZIPLIST_TAIL) {\n                    listAddNodeTail(ref,sdsnewlen(buf, buflen));\n                } else {\n                    assert(NULL);\n                }\n            }\n\n            assert(listLength(ref) == ziplistLen(zl));\n            for (j = 0; j < len; j++) {\n                /* Naive way to get elements, but similar to the stresser\n                 * executed from the Tcl test suite. */\n                p = ziplistIndex(zl,j);\n                refnode = listIndex(ref,j);\n\n                assert(ziplistGet(p,&sstr,&slen,&sval));\n                if (sstr == NULL) {\n                    buflen = snprintf(buf,sizeof(buf),\"%lld\",sval);\n                } else {\n                    buflen = slen;\n                    memcpy(buf,sstr,buflen);\n                    buf[buflen] = '\\0';\n                }\n                assert(memcmp(buf,listNodeValue(refnode),buflen) == 0);\n            }\n            zfree(zl);\n            listRelease(ref);\n        }\n        printf(\"Done. usec=%lld\\n\\n\", usec()-start);\n    }\n\n    printf(\"Stress with variable ziplist size:\\n\");\n    {\n        unsigned long long start = usec();\n        int maxsize = accurate ? 16384 : 16;\n        stress(ZIPLIST_HEAD,100000,maxsize,256);\n        stress(ZIPLIST_TAIL,100000,maxsize,256);\n        printf(\"Done. usec=%lld\\n\\n\", usec()-start);\n    }\n\n    /* Benchmarks */\n    {\n        zl = ziplistNew();\n        iteration = accurate ? 100000 : 100;\n        for (int i=0; i<iteration; i++) {\n            char buf[4096] = \"asdf\";\n            zl = ziplistPush(zl, (unsigned char*)buf, 4, ZIPLIST_TAIL);\n            zl = ziplistPush(zl, (unsigned char*)buf, 40, ZIPLIST_TAIL);\n            zl = ziplistPush(zl, (unsigned char*)buf, 400, ZIPLIST_TAIL);\n            zl = ziplistPush(zl, (unsigned char*)buf, 4000, ZIPLIST_TAIL);\n            zl = ziplistPush(zl, (unsigned char*)\"1\", 1, ZIPLIST_TAIL);\n            zl = ziplistPush(zl, (unsigned char*)\"10\", 2, ZIPLIST_TAIL);\n            zl = ziplistPush(zl, (unsigned char*)\"100\", 3, ZIPLIST_TAIL);\n            zl = ziplistPush(zl, (unsigned char*)\"1000\", 4, ZIPLIST_TAIL);\n            zl = ziplistPush(zl, (unsigned char*)\"10000\", 5, ZIPLIST_TAIL);\n            zl = ziplistPush(zl, (unsigned char*)\"100000\", 6, ZIPLIST_TAIL);\n        }\n\n        printf(\"Benchmark ziplistFind:\\n\");\n        {\n            unsigned long long start = usec();\n            for (int i = 0; i < 2000; i++) {\n                unsigned char *fptr = ziplistIndex(zl, ZIPLIST_HEAD);\n                fptr = ziplistFind(zl, fptr, (unsigned char*)\"nothing\", 7, 1);\n            }\n            printf(\"%lld\\n\", usec()-start);\n        }\n\n        printf(\"Benchmark ziplistIndex:\\n\");\n        {\n            unsigned long long start = usec();\n            for (int i = 0; i < 2000; i++) {\n                ziplistIndex(zl, 99999);\n            }\n            printf(\"%lld\\n\", usec()-start);\n        }\n\n        printf(\"Benchmark ziplistValidateIntegrity:\\n\");\n        {\n            unsigned long long start = usec();\n            for (int i = 0; i < 2000; i++) {\n                ziplistValidateIntegrity(zl, ziplistBlobLen(zl), 1, NULL, NULL);\n            }\n            printf(\"%lld\\n\", usec()-start);\n        }\n\n        zfree(zl);\n    }\n\n    printf(\"Stress __ziplistCascadeUpdate:\\n\");\n    {\n        char data[ZIP_BIG_PREVLEN];\n        zl = ziplistNew();\n        iteration = accurate ? 100000 : 100;\n        for (int i = 0; i < iteration; i++) {\n            zl = ziplistPush(zl, (unsigned char*)data, ZIP_BIG_PREVLEN-4, ZIPLIST_TAIL);\n        }\n        unsigned long long start = usec();\n        zl = ziplistPush(zl, (unsigned char*)data, ZIP_BIG_PREVLEN-3, ZIPLIST_HEAD);\n        printf(\"Done. usec=%lld\\n\\n\", usec()-start);\n        zfree(zl);\n    }\n\n    printf(\"Edge cases of __ziplistCascadeUpdate:\\n\");\n    {\n        /* Inserting a entry with data length greater than ZIP_BIG_PREVLEN-4 \n         * will leads to cascade update. */\n        size_t s1 = ZIP_BIG_PREVLEN-4, s2 = ZIP_BIG_PREVLEN-3;\n        zl = ziplistNew();\n\n        zlentry e[4] = {{.prevrawlensize = 0, .prevrawlen = 0, .lensize = 0,\n                         .len = 0, .headersize = 0, .encoding = 0, .p = NULL}};\n\n        zl = insertHelper(zl, 'a', s1, ZIPLIST_ENTRY_HEAD(zl));\n        verify(zl, e);\n\n        assert(e[0].prevrawlensize == 1 && e[0].prevrawlen == 0);\n        assert(compareHelper(zl, 'a', s1, 0));\n        ziplistRepr(zl);\n\n        /* No expand. */\n        zl = insertHelper(zl, 'b', s1, ZIPLIST_ENTRY_HEAD(zl));\n        verify(zl, e);\n\n        assert(e[0].prevrawlensize == 1 && e[0].prevrawlen == 0);\n        assert(compareHelper(zl, 'b', s1, 0));\n\n        assert(e[1].prevrawlensize == 1 && e[1].prevrawlen == strEntryBytesSmall(s1));\n        assert(compareHelper(zl, 'a', s1, 1));\n\n        ziplistRepr(zl);\n\n        /* Expand(tail included). */\n        zl = insertHelper(zl, 'c', s2, ZIPLIST_ENTRY_HEAD(zl));\n        verify(zl, e);\n\n        assert(e[0].prevrawlensize == 1 && e[0].prevrawlen == 0);\n        assert(compareHelper(zl, 'c', s2, 0));\n\n        assert(e[1].prevrawlensize == 5 && e[1].prevrawlen == strEntryBytesSmall(s2));\n        assert(compareHelper(zl, 'b', s1, 1));\n\n        assert(e[2].prevrawlensize == 5 && e[2].prevrawlen == strEntryBytesLarge(s1));\n        assert(compareHelper(zl, 'a', s1, 2));\n\n        ziplistRepr(zl);\n\n        /* Expand(only previous head entry). */\n        zl = insertHelper(zl, 'd', s2, ZIPLIST_ENTRY_HEAD(zl));\n        verify(zl, e);\n\n        assert(e[0].prevrawlensize == 1 && e[0].prevrawlen == 0);\n        assert(compareHelper(zl, 'd', s2, 0));\n\n        assert(e[1].prevrawlensize == 5 && e[1].prevrawlen == strEntryBytesSmall(s2));\n        assert(compareHelper(zl, 'c', s2, 1));\n\n        assert(e[2].prevrawlensize == 5 && e[2].prevrawlen == strEntryBytesLarge(s2));\n        assert(compareHelper(zl, 'b', s1, 2));\n\n        assert(e[3].prevrawlensize == 5 && e[3].prevrawlen == strEntryBytesLarge(s1));\n        assert(compareHelper(zl, 'a', s1, 3));\n\n        ziplistRepr(zl);\n\n        /* Delete from mid. */\n        unsigned char *p = ziplistIndex(zl, 2);\n        zl = ziplistDelete(zl, &p);\n        verify(zl, e);\n\n        assert(e[0].prevrawlensize == 1 && e[0].prevrawlen == 0);\n        assert(compareHelper(zl, 'd', s2, 0));\n\n        assert(e[1].prevrawlensize == 5 && e[1].prevrawlen == strEntryBytesSmall(s2));\n        assert(compareHelper(zl, 'c', s2, 1));\n\n        assert(e[2].prevrawlensize == 5 && e[2].prevrawlen == strEntryBytesLarge(s2));\n        assert(compareHelper(zl, 'a', s1, 2));\n\n        ziplistRepr(zl);\n\n        zfree(zl);\n    }\n\n    return 0;\n}\n#endif\n"
  },
  {
    "path": "src/ziplist.h",
    "content": "/*\n * Copyright (c) 2009-2012, Pieter Noordhuis <pcnoordhuis at gmail dot com>\n * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef _ZIPLIST_H\n#define _ZIPLIST_H\n\n#define ZIPLIST_HEAD 0\n#define ZIPLIST_TAIL 1\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n/* Each entry in the ziplist is either a string or an integer. */\ntypedef struct {\n    /* When string is used, it is provided with the length (slen). */\n    unsigned char *sval;\n    unsigned int slen;\n    /* When integer is used, 'sval' is NULL, and lval holds the value. */\n    long long lval;\n} ziplistEntry;\n\nunsigned char *ziplistNew(void);\nunsigned char *ziplistMerge(unsigned char **first, unsigned char **second);\nunsigned char *ziplistPush(unsigned char *zl, unsigned char *s, unsigned int slen, int where);\nunsigned char *ziplistIndex(unsigned char *zl, int index);\nunsigned char *ziplistNext(unsigned char *zl, unsigned char *p);\nunsigned char *ziplistPrev(unsigned char *zl, unsigned char *p);\nunsigned int ziplistGet(unsigned char *p, unsigned char **sval, unsigned int *slen, long long *lval);\nunsigned char *ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen);\nunsigned char *ziplistDelete(unsigned char *zl, unsigned char **p);\nunsigned char *ziplistDeleteRange(unsigned char *zl, int index, unsigned int num);\nunsigned char *ziplistReplace(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen);\nunsigned int ziplistCompare(unsigned char *p, unsigned char *s, unsigned int slen);\nunsigned char *ziplistFind(unsigned char *zl, unsigned char *p, unsigned char *vstr, unsigned int vlen, unsigned int skip);\nunsigned int ziplistLen(const unsigned char *zl);\nsize_t ziplistBlobLen(unsigned char *zl);\nvoid ziplistRepr(unsigned char *zl);\ntypedef int (*ziplistValidateEntryCB)(unsigned char* p, void* userdata);\nint ziplistValidateIntegrity(unsigned char *zl, size_t size, int deep,\n                             ziplistValidateEntryCB entry_cb, void *cb_userdata);\nvoid ziplistRandomPair(unsigned char *zl, unsigned long total_count, ziplistEntry *key, ziplistEntry *val);\nvoid ziplistRandomPairs(unsigned char *zl, unsigned int count, ziplistEntry *keys, ziplistEntry *vals);\nunsigned int ziplistRandomPairsUnique(unsigned char *zl, unsigned int count, ziplistEntry *keys, ziplistEntry *vals);\nint ziplistSafeToAdd(unsigned char* zl, size_t add);\n\n#ifdef REDIS_TEST\nint ziplistTest(int argc, char *argv[], int accurate);\n#endif\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif /* _ZIPLIST_H */\n"
  },
  {
    "path": "src/zipmap.c",
    "content": "/* String -> String Map data structure optimized for size.\n * This file implements a data structure mapping strings to other strings\n * implementing an O(n) lookup data structure designed to be very memory\n * efficient.\n *\n * The Redis Hash type uses this data structure for hashes composed of a small\n * number of elements, to switch to a hash table once a given number of\n * elements is reached.\n *\n * Given that many times Redis Hashes are used to represent objects composed\n * of few fields, this is a very big win in terms of used memory.\n *\n * --------------------------------------------------------------------------\n *\n * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n/* Memory layout of a zipmap, for the map \"foo\" => \"bar\", \"hello\" => \"world\":\n *\n * <zmlen><len>\"foo\"<len><free>\"bar\"<len>\"hello\"<len><free>\"world\"\n *\n * <zmlen> is 1 byte length that holds the current size of the zipmap.\n * When the zipmap length is greater than or equal to 254, this value\n * is not used and the zipmap needs to be traversed to find out the length.\n *\n * <len> is the length of the following string (key or value).\n * <len> lengths are encoded in a single value or in a 5 bytes value.\n * If the first byte value (as an unsigned 8 bit value) is between 0 and\n * 253, it's a single-byte length. If it is 254 then a four bytes unsigned\n * integer follows (in the host byte ordering). A value of 255 is used to\n * signal the end of the hash.\n *\n * <free> is the number of free unused bytes after the string, resulting\n * from modification of values associated to a key. For instance if \"foo\"\n * is set to \"bar\", and later \"foo\" will be set to \"hi\", it will have a\n * free byte to use if the value will enlarge again later, or even in\n * order to add a key/value pair if it fits.\n *\n * <free> is always an unsigned 8 bit number, because if after an\n * update operation there are more than a few free bytes, the zipmap will be\n * reallocated to make sure it is as small as possible.\n *\n * The most compact representation of the above two elements hash is actually:\n *\n * \"\\x02\\x03foo\\x03\\x00bar\\x05hello\\x05\\x00world\\xff\"\n *\n * Note that because keys and values are prefixed length \"objects\",\n * the lookup will take O(N) where N is the number of elements\n * in the zipmap and *not* the number of bytes needed to represent the zipmap.\n * This lowers the constant times considerably.\n */\n\n#include <stdio.h>\n#include <string.h>\n#include \"zmalloc.h\"\n#include \"endianconv.h\"\n\n#define ZIPMAP_BIGLEN 254\n#define ZIPMAP_END 255\n\n/* The following defines the max value for the <free> field described in the\n * comments above, that is, the max number of trailing bytes in a value. */\n#define ZIPMAP_VALUE_MAX_FREE 4\n\n/* The following macro returns the number of bytes needed to encode the length\n * for the integer value _l, that is, 1 byte for lengths < ZIPMAP_BIGLEN and\n * 5 bytes for all the other lengths. */\n#define ZIPMAP_LEN_BYTES(_l) (((_l) < ZIPMAP_BIGLEN) ? 1 : sizeof(unsigned int)+1)\n\n/* Create a new empty zipmap. */\nunsigned char *zipmapNew(void) {\n    unsigned char *zm = zmalloc(2, MALLOC_SHARED);\n\n    zm[0] = 0; /* Length */\n    zm[1] = ZIPMAP_END;\n    return zm;\n}\n\n/* Decode the encoded length pointed by 'p' */\nstatic unsigned int zipmapDecodeLength(unsigned char *p) {\n    unsigned int len = *p;\n\n    if (len < ZIPMAP_BIGLEN) return len;\n    memcpy(&len,p+1,sizeof(unsigned int));\n    memrev32ifbe(&len);\n    return len;\n}\n\nstatic unsigned int zipmapGetEncodedLengthSize(unsigned char *p) {\n    return (*p < ZIPMAP_BIGLEN) ? 1: 5;\n}\n\n/* Encode the length 'l' writing it in 'p'. If p is NULL it just returns\n * the amount of bytes required to encode such a length. */\nstatic unsigned int zipmapEncodeLength(unsigned char *p, unsigned int len) {\n    if (p == NULL) {\n        return ZIPMAP_LEN_BYTES(len);\n    } else {\n        if (len < ZIPMAP_BIGLEN) {\n            p[0] = len;\n            return 1;\n        } else {\n            p[0] = ZIPMAP_BIGLEN;\n            memcpy(p+1,&len,sizeof(len));\n            memrev32ifbe(p+1);\n            return 1+sizeof(len);\n        }\n    }\n}\n\n/* Search for a matching key, returning a pointer to the entry inside the\n * zipmap. Returns NULL if the key is not found.\n *\n * If NULL is returned, and totlen is not NULL, it is set to the entire\n * size of the zipmap, so that the calling function will be able to\n * reallocate the original zipmap to make room for more entries. */\nstatic unsigned char *zipmapLookupRaw(unsigned char *zm, unsigned char *key, unsigned int klen, unsigned int *totlen) {\n    unsigned char *p = zm+1, *k = NULL;\n    unsigned int l,llen;\n\n    while(*p != ZIPMAP_END) {\n        unsigned char free;\n\n        /* Match or skip the key */\n        l = zipmapDecodeLength(p);\n        llen = zipmapEncodeLength(NULL,l);\n        if (key != NULL && k == NULL && l == klen && !memcmp(p+llen,key,l)) {\n            /* Only return when the user doesn't care\n             * for the total length of the zipmap. */\n            if (totlen != NULL) {\n                k = p;\n            } else {\n                return p;\n            }\n        }\n        p += llen+l;\n        /* Skip the value as well */\n        l = zipmapDecodeLength(p);\n        p += zipmapEncodeLength(NULL,l);\n        free = p[0];\n        p += l+1+free; /* +1 to skip the free byte */\n    }\n    if (totlen != NULL) *totlen = (unsigned int)(p-zm)+1;\n    return k;\n}\n\nstatic unsigned long zipmapRequiredLength(unsigned int klen, unsigned int vlen) {\n    unsigned int l;\n\n    l = klen+vlen+3;\n    if (klen >= ZIPMAP_BIGLEN) l += 4;\n    if (vlen >= ZIPMAP_BIGLEN) l += 4;\n    return l;\n}\n\n/* Return the total amount used by a key (encoded length + payload) */\nstatic unsigned int zipmapRawKeyLength(unsigned char *p) {\n    unsigned int l = zipmapDecodeLength(p);\n    return zipmapEncodeLength(NULL,l) + l;\n}\n\n/* Return the total amount used by a value\n * (encoded length + single byte free count + payload) */\nstatic unsigned int zipmapRawValueLength(unsigned char *p) {\n    unsigned int l = zipmapDecodeLength(p);\n    unsigned int used;\n\n    used = zipmapEncodeLength(NULL,l);\n    used += p[used] + 1 + l;\n    return used;\n}\n\n/* If 'p' points to a key, this function returns the total amount of\n * bytes used to store this entry (entry = key + associated value + trailing\n * free space if any). */\nstatic unsigned int zipmapRawEntryLength(unsigned char *p) {\n    unsigned int l = zipmapRawKeyLength(p);\n    return l + zipmapRawValueLength(p+l);\n}\n\nstatic inline unsigned char *zipmapResize(unsigned char *zm, unsigned int len) {\n    zm = zrealloc(zm, len, MALLOC_SHARED);\n    zm[len-1] = ZIPMAP_END;\n    return zm;\n}\n\n/* Set key to value, creating the key if it does not already exist.\n * If 'update' is not NULL, *update is set to 1 if the key was\n * already preset, otherwise to 0. */\nunsigned char *zipmapSet(unsigned char *zm, unsigned char *key, unsigned int klen, unsigned char *val, unsigned int vlen, int *update) {\n    unsigned int zmlen, offset;\n    unsigned int freelen, reqlen = zipmapRequiredLength(klen,vlen);\n    unsigned int empty, vempty;\n    unsigned char *p;\n\n    freelen = reqlen;\n    if (update) *update = 0;\n    p = zipmapLookupRaw(zm,key,klen,&zmlen);\n    if (p == NULL) {\n        /* Key not found: enlarge */\n        zm = zipmapResize(zm, zmlen+reqlen);\n        p = zm+zmlen-1;\n        zmlen = zmlen+reqlen;\n\n        /* Increase zipmap length (this is an insert) */\n        if (zm[0] < ZIPMAP_BIGLEN) zm[0]++;\n    } else {\n        /* Key found. Is there enough space for the new value? */\n        /* Compute the total length: */\n        if (update) *update = 1;\n        freelen = zipmapRawEntryLength(p);\n        if (freelen < reqlen) {\n            /* Store the offset of this key within the current zipmap, so\n             * it can be resized. Then, move the tail backwards so this\n             * pair fits at the current position. */\n            offset = p-zm;\n            zm = zipmapResize(zm, zmlen-freelen+reqlen);\n            p = zm+offset;\n\n            /* The +1 in the number of bytes to be moved is caused by the\n             * end-of-zipmap byte. Note: the *original* zmlen is used. */\n            memmove(p+reqlen, p+freelen, zmlen-(offset+freelen+1));\n            zmlen = zmlen-freelen+reqlen;\n            freelen = reqlen;\n        }\n    }\n\n    /* We now have a suitable block where the key/value entry can\n     * be written. If there is too much free space, move the tail\n     * of the zipmap a few bytes to the front and shrink the zipmap,\n     * as we want zipmaps to be very space efficient. */\n    empty = freelen-reqlen;\n    if (empty >= ZIPMAP_VALUE_MAX_FREE) {\n        /* First, move the tail <empty> bytes to the front, then resize\n         * the zipmap to be <empty> bytes smaller. */\n        offset = p-zm;\n        memmove(p+reqlen, p+freelen, zmlen-(offset+freelen+1));\n        zmlen -= empty;\n        zm = zipmapResize(zm, zmlen);\n        p = zm+offset;\n        vempty = 0;\n    } else {\n        vempty = empty;\n    }\n\n    /* Just write the key + value and we are done. */\n    /* Key: */\n    p += zipmapEncodeLength(p,klen);\n    memcpy(p,key,klen);\n    p += klen;\n    /* Value: */\n    p += zipmapEncodeLength(p,vlen);\n    *p++ = vempty;\n    memcpy(p,val,vlen);\n    return zm;\n}\n\n/* Remove the specified key. If 'deleted' is not NULL the pointed integer is\n * set to 0 if the key was not found, to 1 if it was found and deleted. */\nunsigned char *zipmapDel(unsigned char *zm, unsigned char *key, unsigned int klen, int *deleted) {\n    unsigned int zmlen, freelen;\n    unsigned char *p = zipmapLookupRaw(zm,key,klen,&zmlen);\n    if (p) {\n        freelen = zipmapRawEntryLength(p);\n        memmove(p, p+freelen, zmlen-((p-zm)+freelen+1));\n        zm = zipmapResize(zm, zmlen-freelen);\n\n        /* Decrease zipmap length */\n        if (zm[0] < ZIPMAP_BIGLEN) zm[0]--;\n\n        if (deleted) *deleted = 1;\n    } else {\n        if (deleted) *deleted = 0;\n    }\n    return zm;\n}\n\n/* Call before iterating through elements via zipmapNext() */\nunsigned char *zipmapRewind(unsigned char *zm) {\n    return zm+1;\n}\n\n/* This function is used to iterate through all the zipmap elements.\n * In the first call the first argument is the pointer to the zipmap + 1.\n * In the next calls what zipmapNext returns is used as first argument.\n * Example:\n *\n * unsigned char *i = zipmapRewind(my_zipmap);\n * while((i = zipmapNext(i,&key,&klen,&value,&vlen)) != NULL) {\n *     printf(\"%d bytes key at $p\\n\", klen, key);\n *     printf(\"%d bytes value at $p\\n\", vlen, value);\n * }\n */\nunsigned char *zipmapNext(unsigned char *zm, unsigned char **key, unsigned int *klen, unsigned char **value, unsigned int *vlen) {\n    if (zm[0] == ZIPMAP_END) return NULL;\n    if (key) {\n        *key = zm;\n        *klen = zipmapDecodeLength(zm);\n        *key += ZIPMAP_LEN_BYTES(*klen);\n    }\n    zm += zipmapRawKeyLength(zm);\n    if (value) {\n        *value = zm+1;\n        *vlen = zipmapDecodeLength(zm);\n        *value += ZIPMAP_LEN_BYTES(*vlen);\n    }\n    zm += zipmapRawValueLength(zm);\n    return zm;\n}\n\n/* Search a key and retrieve the pointer and len of the associated value.\n * If the key is found the function returns 1, otherwise 0. */\nint zipmapGet(unsigned char *zm, unsigned char *key, unsigned int klen, unsigned char **value, unsigned int *vlen) {\n    unsigned char *p;\n\n    if ((p = zipmapLookupRaw(zm,key,klen,NULL)) == NULL) return 0;\n    p += zipmapRawKeyLength(p);\n    *vlen = zipmapDecodeLength(p);\n    *value = p + ZIPMAP_LEN_BYTES(*vlen) + 1;\n    return 1;\n}\n\n/* Return 1 if the key exists, otherwise 0 is returned. */\nint zipmapExists(unsigned char *zm, unsigned char *key, unsigned int klen) {\n    return zipmapLookupRaw(zm,key,klen,NULL) != NULL;\n}\n\n/* Return the number of entries inside a zipmap */\nunsigned int zipmapLen(unsigned char *zm) {\n    unsigned int len = 0;\n    if (zm[0] < ZIPMAP_BIGLEN) {\n        len = zm[0];\n    } else {\n        unsigned char *p = zipmapRewind(zm);\n        while((p = zipmapNext(p,NULL,NULL,NULL,NULL)) != NULL) len++;\n\n        /* Re-store length if small enough */\n        if (len < ZIPMAP_BIGLEN) zm[0] = len;\n    }\n    return len;\n}\n\n/* Return the raw size in bytes of a zipmap, so that we can serialize\n * the zipmap on disk (or everywhere is needed) just writing the returned\n * amount of bytes of the C array starting at the zipmap pointer. */\nsize_t zipmapBlobLen(unsigned char *zm) {\n    unsigned int totlen;\n    zipmapLookupRaw(zm,NULL,0,&totlen);\n    return totlen;\n}\n\n/* Validate the integrity of the data structure.\n * when `deep` is 0, only the integrity of the header is validated.\n * when `deep` is 1, we scan all the entries one by one. */\nint zipmapValidateIntegrity(unsigned char *zm, size_t size, int deep) {\n#define OUT_OF_RANGE(p) ( \\\n        (p) < zm + 2 || \\\n        (p) > zm + size - 1)\n    unsigned int l, s, e;\n\n    /* check that we can actually read the header (or ZIPMAP_END). */\n    if (size < 2)\n        return 0;\n\n    /* the last byte must be the terminator. */\n    if (zm[size-1] != ZIPMAP_END)\n        return 0;\n\n    if (!deep)\n        return 1;\n\n    unsigned int count = 0;\n    unsigned char *p = zm + 1; /* skip the count */\n    while(*p != ZIPMAP_END) {\n        /* read the field name length encoding type */\n        s = zipmapGetEncodedLengthSize(p);\n        /* make sure the entry length doesn't rech outside the edge of the zipmap */\n        if (OUT_OF_RANGE(p+s))\n            return 0;\n\n        /* read the field name length */\n        l = zipmapDecodeLength(p);\n        p += s; /* skip the encoded field size */\n        p += l; /* skip the field */\n\n        /* make sure the entry doesn't rech outside the edge of the zipmap */\n        if (OUT_OF_RANGE(p))\n            return 0;\n\n        /* read the value length encoding type */\n        s = zipmapGetEncodedLengthSize(p);\n        /* make sure the entry length doesn't rech outside the edge of the zipmap */\n        if (OUT_OF_RANGE(p+s))\n            return 0;\n\n        /* read the value length */\n        l = zipmapDecodeLength(p);\n        p += s; /* skip the encoded value size*/\n        e = *p++; /* skip the encoded free space (always encoded in one byte) */\n        p += l+e; /* skip the value and free space */\n        count++;\n\n        /* make sure the entry doesn't rech outside the edge of the zipmap */\n        if (OUT_OF_RANGE(p))\n            return 0;\n    }\n\n    /* check that the zipmap is not empty. */\n    if (count == 0) return 0;\n\n    /* check that the count in the header is correct */\n    if (zm[0] != ZIPMAP_BIGLEN && zm[0] != count)\n        return 0;\n\n    return 1;\n#undef OUT_OF_RANGE\n}\n\n#ifdef REDIS_TEST\nstatic void zipmapRepr(unsigned char *p) {\n    unsigned int l;\n\n    printf(\"{status %u}\",*p++);\n    while(1) {\n        if (p[0] == ZIPMAP_END) {\n            printf(\"{end}\");\n            break;\n        } else {\n            unsigned char e;\n\n            l = zipmapDecodeLength(p);\n            printf(\"{key %u}\",l);\n            p += zipmapEncodeLength(NULL,l);\n            if (l != 0 && fwrite(p,l,1,stdout) == 0) perror(\"fwrite\");\n            p += l;\n\n            l = zipmapDecodeLength(p);\n            printf(\"{value %u}\",l);\n            p += zipmapEncodeLength(NULL,l);\n            e = *p++;\n            if (l != 0 && fwrite(p,l,1,stdout) == 0) perror(\"fwrite\");\n            p += l+e;\n            if (e) {\n                printf(\"[\");\n                while(e--) printf(\".\");\n                printf(\"]\");\n            }\n        }\n    }\n    printf(\"\\n\");\n}\n\n#define UNUSED(x) (void)(x)\nint zipmapTest(int argc, char *argv[], int accurate) {\n    unsigned char *zm;\n\n    UNUSED(argc);\n    UNUSED(argv);\n    UNUSED(accurate);\n\n    zm = zipmapNew();\n\n    zm = zipmapSet(zm,(unsigned char*) \"name\",4, (unsigned char*) \"foo\",3,NULL);\n    zm = zipmapSet(zm,(unsigned char*) \"surname\",7, (unsigned char*) \"foo\",3,NULL);\n    zm = zipmapSet(zm,(unsigned char*) \"age\",3, (unsigned char*) \"foo\",3,NULL);\n    zipmapRepr(zm);\n\n    zm = zipmapSet(zm,(unsigned char*) \"hello\",5, (unsigned char*) \"world!\",6,NULL);\n    zm = zipmapSet(zm,(unsigned char*) \"foo\",3, (unsigned char*) \"bar\",3,NULL);\n    zm = zipmapSet(zm,(unsigned char*) \"foo\",3, (unsigned char*) \"!\",1,NULL);\n    zipmapRepr(zm);\n    zm = zipmapSet(zm,(unsigned char*) \"foo\",3, (unsigned char*) \"12345\",5,NULL);\n    zipmapRepr(zm);\n    zm = zipmapSet(zm,(unsigned char*) \"new\",3, (unsigned char*) \"xx\",2,NULL);\n    zm = zipmapSet(zm,(unsigned char*) \"noval\",5, (unsigned char*) \"\",0,NULL);\n    zipmapRepr(zm);\n    zm = zipmapDel(zm,(unsigned char*) \"new\",3,NULL);\n    zipmapRepr(zm);\n\n    printf(\"\\nLook up large key:\\n\");\n    {\n        unsigned char buf[512];\n        unsigned char *value;\n        unsigned int vlen, i;\n        for (i = 0; i < 512; i++) buf[i] = 'a';\n\n        zm = zipmapSet(zm,buf,512,(unsigned char*) \"long\",4,NULL);\n        if (zipmapGet(zm,buf,512,&value,&vlen)) {\n            printf(\"  <long key> is associated to the %d bytes value: %.*s\\n\",\n                vlen, vlen, value);\n        }\n    }\n\n    printf(\"\\nPerform a direct lookup:\\n\");\n    {\n        unsigned char *value;\n        unsigned int vlen;\n\n        if (zipmapGet(zm,(unsigned char*) \"foo\",3,&value,&vlen)) {\n            printf(\"  foo is associated to the %d bytes value: %.*s\\n\",\n                vlen, vlen, value);\n        }\n    }\n    printf(\"\\nIterate through elements:\\n\");\n    {\n        unsigned char *i = zipmapRewind(zm);\n        unsigned char *key, *value;\n        unsigned int klen, vlen;\n\n        while((i = zipmapNext(i,&key,&klen,&value,&vlen)) != NULL) {\n            printf(\"  %d:%.*s => %d:%.*s\\n\", klen, klen, key, vlen, vlen, value);\n        }\n    }\n    zfree(zm);\n    return 0;\n}\n#endif\n"
  },
  {
    "path": "src/zipmap.h",
    "content": "/* String -> String Map data structure optimized for size.\n *\n * See zipmap.c for more info.\n *\n * --------------------------------------------------------------------------\n *\n * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef _ZIPMAP_H\n#define _ZIPMAP_H\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\nunsigned char *zipmapNew(void);\nunsigned char *zipmapSet(unsigned char *zm, unsigned char *key, unsigned int klen, unsigned char *val, unsigned int vlen, int *update);\nunsigned char *zipmapDel(unsigned char *zm, unsigned char *key, unsigned int klen, int *deleted);\nunsigned char *zipmapRewind(unsigned char *zm);\nunsigned char *zipmapNext(unsigned char *zm, unsigned char **key, unsigned int *klen, unsigned char **value, unsigned int *vlen);\nint zipmapGet(unsigned char *zm, unsigned char *key, unsigned int klen, unsigned char **value, unsigned int *vlen);\nint zipmapExists(unsigned char *zm, unsigned char *key, unsigned int klen);\nunsigned int zipmapLen(unsigned char *zm);\nsize_t zipmapBlobLen(unsigned char *zm);\nvoid zipmapRepr(unsigned char *p);\nint zipmapValidateIntegrity(unsigned char *zm, size_t size, int deep);\n\n#ifdef REDIS_TEST\nint zipmapTest(int argc, char *argv[], int accurate);\n#endif\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif\n"
  },
  {
    "path": "src/zmalloc.cpp",
    "content": "/* zmalloc - total amount of allocated memory aware version of malloc()\n *\n * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#include <stdio.h>\n#include <stdlib.h>\n#include <stdint.h>\n#include <unistd.h>\n#include <assert.h>\n\n/* This function provide us access to the original libc free(). This is useful\n * for instance to free results obtained by backtrace_symbols(). We need\n * to define this function before including zmalloc.h that may shadow the\n * free implementation if we use jemalloc or another non standard allocator. */\nextern \"C\" void zlibc_free(void *ptr) {\n    free(ptr);\n}\n\n#include <string.h>\n#include <pthread.h>\n#include \"config.h\"\n#include \"zmalloc.h\"\n#include \"atomicvar.h\"\n\n#ifdef HAVE_MALLOC_SIZE\n#define PREFIX_SIZE (0)\n#define ASSERT_NO_SIZE_OVERFLOW(sz)\n#else\n#define PREFIX_SIZE 16\n#if defined(__sun) || defined(__sparc) || defined(__sparc__)\nstatic_assert(PREFIX_SIZE >= (sizeof(long long)), \"\");\n#else\nstatic_assert(PREFIX_SIZE >= (sizeof(size_t)), \"\");\n#endif\n#define ASSERT_NO_SIZE_OVERFLOW(sz) assert((sz) + PREFIX_SIZE > (sz))\n#endif\n\nstatic_assert((PREFIX_SIZE % 16) == 0, \"Our prefix must be modulo 16-bytes or our pointers will not be aligned\");\n\n/* When using the libc allocator, use a minimum allocation size to match the\n * jemalloc behavior that doesn't return NULL in this case.\n */\n#define MALLOC_MIN_SIZE(x) ((x) > 0 ? (x) : sizeof(long))\n\n/* Explicitly override malloc/free etc when using tcmalloc. */\n#if defined(USE_MEMKIND)\n#define malloc(size, type) salloc(size, type)\n#define calloc(count, size, type) scalloc(count, size, type)\n#define realloc(ptr, size, type) srealloc(ptr, size, type)\n#define free(ptr) sfree(ptr)\n#elif defined(USE_TCMALLOC)\n#define malloc(size, type) tc_malloc(size)\n#define calloc(count,size, type) tc_calloc(count,size)\n#define realloc(ptr,size, type) tc_realloc(ptr,size)\n#define free(ptr) tc_free(ptr)\n#elif defined(USE_JEMALLOC)\n#define malloc(size, type) malloc(size)\n#define calloc(count,size,type) calloc(count,size)\n#define realloc(ptr,size,type) realloc(ptr,size)\n#define free(ptr) free(ptr)\n#define mallocx(size,flags) mallocx(size,flags)\n#define dallocx(ptr,flags) dallocx(ptr,flags)\n#else\n#define malloc(size, type) malloc(size)\n#define calloc(count,size,type) calloc(count,size)\n#define realloc(ptr,size,type) realloc(ptr,size)\n#endif\n\n#define update_zmalloc_stat_alloc(__n) atomicIncr(used_memory,(__n))\n#define update_zmalloc_stat_free(__n) atomicDecr(used_memory,(__n))\n\nstatic redisAtomic size_t used_memory = 0;\n\nstatic void zmalloc_default_oom(size_t size) {\n    fprintf(stderr, \"zmalloc: Out of memory trying to allocate %zu bytes\\n\",\n        size);\n    fflush(stderr);\n    abort();\n}\n\nstatic void (*zmalloc_oom_handler)(size_t) = zmalloc_default_oom;\n\n/* Try allocating memory, and return NULL if failed.\n * '*usable' is set to the usable size if non NULL. */\nvoid *ztrymalloc_usable(size_t size, size_t *usable) {\n    ASSERT_NO_SIZE_OVERFLOW(size);\n    void *ptr = malloc(MALLOC_MIN_SIZE(size)+PREFIX_SIZE, MALLOC_LOCAL);\n\n    if (!ptr) return NULL;\n#ifdef HAVE_MALLOC_SIZE\n    size = zmalloc_size(ptr);\n    update_zmalloc_stat_alloc(size);\n    if (usable) *usable = size;\n    return ptr;\n#else\n    *((size_t*)ptr) = size;\n    update_zmalloc_stat_alloc(size+PREFIX_SIZE);\n    if (usable) *usable = size;\n    return (char*)ptr+PREFIX_SIZE;\n#endif\n}\n\n/* Allocate memory or panic */\nvoid *zmalloc(size_t size, enum MALLOC_CLASS /*mclass*/) {\n    void *ptr = ztrymalloc_usable(size, NULL);\n    if (!ptr) zmalloc_oom_handler(size);\n    return ptr;\n}\n\n/* Try allocating memory, and return NULL if failed. */\nvoid *ztrymalloc(size_t size) {\n    void *ptr = ztrymalloc_usable(size, NULL);\n    return ptr;\n}\n\n/* Allocate memory or panic.\n * '*usable' is set to the usable size if non NULL. */\nvoid *zmalloc_usable(size_t size, size_t *usable) {\n    void *ptr = ztrymalloc_usable(size, usable);\n    if (!ptr) zmalloc_oom_handler(size);\n    return ptr;\n}\n\n/* Allocation and free functions that bypass the thread cache\n * and go straight to the allocator arena bins.\n * Currently implemented only for jemalloc. Used for online defragmentation. */\n#ifdef HAVE_DEFRAG\nvoid *zmalloc_no_tcache(size_t size) {\n    ASSERT_NO_SIZE_OVERFLOW(size);\n    void *ptr = mallocx(size+PREFIX_SIZE, MALLOCX_TCACHE_NONE);\n    if (!ptr) zmalloc_oom_handler(size);\n    update_zmalloc_stat_alloc(zmalloc_size(ptr));\n    return ptr;\n}\n\nvoid zfree_no_tcache(void *ptr) {\n    if (ptr == NULL) return;\n    update_zmalloc_stat_free(zmalloc_size(ptr));\n    dallocx(ptr, MALLOCX_TCACHE_NONE);\n}\n#endif\n\n/* Try allocating memory and zero it, and return NULL if failed.\n * '*usable' is set to the usable size if non NULL. */\nvoid *ztrycalloc_usable(size_t size, size_t *usable) {\n    ASSERT_NO_SIZE_OVERFLOW(size);\n    void *ptr = calloc(1, MALLOC_MIN_SIZE(size)+PREFIX_SIZE, MALLOC_LOCAL);\n    if (ptr == NULL) return NULL;\n\n#ifdef HAVE_MALLOC_SIZE\n    size = zmalloc_size(ptr);\n    update_zmalloc_stat_alloc(size);\n    if (usable) *usable = size;\n    return ptr;\n#else\n    *((size_t*)ptr) = size;\n    update_zmalloc_stat_alloc(size+PREFIX_SIZE);\n    if (usable) *usable = size;\n    return (char*)ptr+PREFIX_SIZE;\n#endif\n}\n\n/* Allocate memory and zero it or panic */\nvoid *zcalloc(size_t size, enum MALLOC_CLASS /*mclass*/) {\n    void *ptr = ztrycalloc_usable(size, NULL);\n    if (!ptr) zmalloc_oom_handler(size);\n    return ptr;\n}\n\n/* Try allocating memory, and return NULL if failed. */\nvoid *ztrycalloc(size_t size) {\n    void *ptr = ztrycalloc_usable(size, NULL);\n    return ptr;\n}\n\n/* Allocate memory or panic.\n * '*usable' is set to the usable size if non NULL. */\nvoid *zcalloc_usable(size_t size, size_t *usable) {\n    void *ptr = ztrycalloc_usable(size, usable);\n    if (!ptr) zmalloc_oom_handler(size);\n    return ptr;\n}\n\n/* Try reallocating memory, and return NULL if failed.\n * '*usable' is set to the usable size if non NULL. */\nvoid *ztryrealloc_usable(void *ptr, size_t size, size_t *usable) {\n    ASSERT_NO_SIZE_OVERFLOW(size);\n#ifndef HAVE_MALLOC_SIZE\n    void *realptr;\n#endif\n    size_t oldsize;\n    void *newptr;\n\n    /* not allocating anything, just redirect to free. */\n    if (size == 0 && ptr != NULL) {\n        zfree(ptr);\n        if (usable) *usable = 0;\n        return NULL;\n    }\n    /* Not freeing anything, just redirect to malloc. */\n    if (ptr == NULL)\n        return ztrymalloc_usable(size, usable);\n\n#ifdef HAVE_MALLOC_SIZE\n    oldsize = zmalloc_size(ptr);\n    newptr = realloc(ptr,size, MALLOC_LOCAL);\n    if (newptr == NULL) {\n        if (usable) *usable = 0;\n        return NULL;\n    }\n\n    update_zmalloc_stat_free(oldsize);\n    size = zmalloc_size(newptr);\n    update_zmalloc_stat_alloc(size);\n    if (usable) *usable = size;\n    return newptr;\n#else\n    realptr = (char*)ptr-PREFIX_SIZE;\n    oldsize = *((size_t*)realptr);\n    newptr = realloc(realptr,size+PREFIX_SIZE, MALLOC_LOCAL);\n    if (newptr == NULL) {\n        if (usable) *usable = 0;\n        return NULL;\n    }\n\n    *((size_t*)newptr) = size;\n    update_zmalloc_stat_free(oldsize);\n    update_zmalloc_stat_alloc(size);\n    if (usable) *usable = size;\n    return (char*)newptr+PREFIX_SIZE;\n#endif\n}\n\n/* Reallocate memory and zero it or panic */\nvoid *zrealloc(void *ptr, size_t size, enum MALLOC_CLASS /*mclass*/) {\n    ptr = ztryrealloc_usable(ptr, size, NULL);\n    if (!ptr && size != 0) zmalloc_oom_handler(size);\n    return ptr;\n}\n\n/* Try Reallocating memory, and return NULL if failed. */\nvoid *ztryrealloc(void *ptr, size_t size) {\n    ptr = ztryrealloc_usable(ptr, size, NULL);\n    return ptr;\n}\n\n/* Reallocate memory or panic.\n * '*usable' is set to the usable size if non NULL. */\nvoid *zrealloc_usable(void *ptr, size_t size, size_t *usable) {\n    ptr = ztryrealloc_usable(ptr, size, usable);\n    if (!ptr && size != 0) zmalloc_oom_handler(size);\n    return ptr;\n}\n\n/* Provide zmalloc_size() for systems where this function is not provided by\n * malloc itself, given that in that case we store a header with this\n * information as the first bytes of every allocation. */\n#ifndef HAVE_MALLOC_SIZE\nsize_t zmalloc_size(void *ptr) {\n    void *realptr = (char*)ptr-PREFIX_SIZE;\n    size_t size = *((size_t*)realptr);\n    return size+PREFIX_SIZE;\n}\nsize_t zmalloc_usable_size(void *ptr) {\n    return zmalloc_size(ptr)-PREFIX_SIZE;\n}\n#endif\n\nvoid zfree(const void *ptr) {\n#ifndef HAVE_MALLOC_SIZE\n    void *realptr;\n    size_t oldsize;\n#endif\n\n    if (ptr == NULL) return;\n#ifdef HAVE_MALLOC_SIZE\n    update_zmalloc_stat_free(zmalloc_size((void*)ptr));\n    free((void*)ptr);\n#else\n    realptr = (char*)ptr-PREFIX_SIZE;\n    oldsize = *((size_t*)realptr);\n    update_zmalloc_stat_free(oldsize+PREFIX_SIZE);\n    free(realptr);\n#endif\n}\n\n/* Similar to zfree, '*usable' is set to the usable size being freed. */\nvoid zfree_usable(void *ptr, size_t *usable) {\n#ifndef HAVE_MALLOC_SIZE\n    void *realptr;\n    size_t oldsize;\n#endif\n\n    if (ptr == NULL) return;\n#ifdef HAVE_MALLOC_SIZE\n    update_zmalloc_stat_free(*usable = zmalloc_size(ptr));\n    free(ptr);\n#else\n    realptr = (char*)ptr-PREFIX_SIZE;\n    *usable = oldsize = *((size_t*)realptr);\n    update_zmalloc_stat_free(oldsize+PREFIX_SIZE);\n    free(realptr);\n#endif\n}\n\nchar *zstrdup(const char *s) {\n    size_t l = strlen(s)+1;\n    char *p = (char*)zmalloc(l, MALLOC_SHARED);\n\n    memcpy(p,s,l);\n    return p;\n}\n\nsize_t zmalloc_used_memory(void) {\n    size_t um;\n    atomicGet(used_memory,um);\n    return um;\n}\n\nvoid zmalloc_set_oom_handler(void (*oom_handler)(size_t)) {\n    zmalloc_oom_handler = oom_handler;\n}\n\n/* Get the RSS information in an OS-specific way.\n *\n * WARNING: the function zmalloc_get_rss() is not designed to be fast\n * and may not be called in the busy loops where Redis tries to release\n * memory expiring or swapping out objects.\n *\n * For this kind of \"fast RSS reporting\" usages use instead the\n * function RedisEstimateRSS() that is a much faster (and less precise)\n * version of the function. */\n\n#if defined(HAVE_PROC_STAT)\n#include <sys/types.h>\n#include <sys/stat.h>\n#include <fcntl.h>\n\nsize_t zmalloc_get_rss(void) {\n    int page = sysconf(_SC_PAGESIZE);\n    size_t rss;\n    char buf[4096];\n    char filename[256];\n    int fd, count;\n    char *p, *x;\n\n    snprintf(filename,sizeof(filename),\"/proc/%ld/stat\",(long) getpid());\n    if ((fd = open(filename,O_RDONLY)) == -1) return 0;\n    if (read(fd,buf,4096) <= 0) {\n        close(fd);\n        return 0;\n    }\n    close(fd);\n\n    p = buf;\n    count = 23; /* RSS is the 24th field in /proc/<pid>/stat */\n    while(p && count--) {\n        p = strchr(p,' ');\n        if (p) p++;\n    }\n    if (!p) return 0;\n    x = strchr(p,' ');\n    if (!x) return 0;\n    *x = '\\0';\n\n    rss = strtoll(p,NULL,10);\n    rss *= page;\n    return rss;\n}\n#elif defined(HAVE_TASKINFO)\n#include <sys/types.h>\n#include <sys/sysctl.h>\n#include <mach/task.h>\n#include <mach/mach_init.h>\n\nsize_t zmalloc_get_rss(void) {\n    task_t task = MACH_PORT_NULL;\n    struct task_basic_info t_info;\n    mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;\n\n    if (task_for_pid(current_task(), getpid(), &task) != KERN_SUCCESS)\n        return 0;\n    task_info(task, TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count);\n\n    return t_info.resident_size;\n}\n#elif defined(__FreeBSD__) || defined(__DragonFly__)\n#include <sys/types.h>\n#include <sys/sysctl.h>\n#include <sys/user.h>\n\nsize_t zmalloc_get_rss(void) {\n    struct kinfo_proc info;\n    size_t infolen = sizeof(info);\n    int mib[4];\n    mib[0] = CTL_KERN;\n    mib[1] = KERN_PROC;\n    mib[2] = KERN_PROC_PID;\n    mib[3] = getpid();\n\n    if (sysctl(mib, 4, &info, &infolen, NULL, 0) == 0)\n#if defined(__FreeBSD__)\n        return (size_t)info.ki_rssize * getpagesize();\n#else\n        return (size_t)info.kp_vm_rssize * getpagesize();\n#endif\n\n    return 0L;\n}\n#elif defined(__NetBSD__)\n#include <sys/types.h>\n#include <sys/sysctl.h>\n\nsize_t zmalloc_get_rss(void) {\n    struct kinfo_proc2 info;\n    size_t infolen = sizeof(info);\n    int mib[6];\n    mib[0] = CTL_KERN;\n    mib[1] = KERN_PROC;\n    mib[2] = KERN_PROC_PID;\n    mib[3] = getpid();\n    mib[4] = sizeof(info);\n    mib[5] = 1;\n    if (sysctl(mib, 4, &info, &infolen, NULL, 0) == 0)\n        return (size_t)info.p_vm_rssize * getpagesize();\n\n    return 0L;\n}\n#elif defined(HAVE_PSINFO)\n#include <unistd.h>\n#include <sys/procfs.h>\n#include <fcntl.h>\n\nsize_t zmalloc_get_rss(void) {\n    struct prpsinfo info;\n    char filename[256];\n    int fd;\n\n    snprintf(filename,sizeof(filename),\"/proc/%ld/psinfo\",(long) getpid());\n\n    if ((fd = open(filename,O_RDONLY)) == -1) return 0;\n    if (ioctl(fd, PIOCPSINFO, &info) == -1) {\n        close(fd);\n\treturn 0;\n    }\n\n    close(fd);\n    return info.pr_rssize;\n}\n#else\nsize_t zmalloc_get_rss(void) {\n    /* If we can't get the RSS in an OS-specific way for this system just\n     * return the memory usage we estimated in zmalloc()..\n     *\n     * Fragmentation will appear to be always 1 (no fragmentation)\n     * of course... */\n    return zmalloc_used_memory();\n}\n#endif\n\n#if defined(USE_JEMALLOC)\n\nint zmalloc_get_allocator_info(size_t *allocated,\n                               size_t *active,\n                               size_t *resident) {\n    uint64_t epoch = 1;\n    size_t sz;\n    *allocated = *resident = *active = 0;\n    /* Update the statistics cached by mallctl. */\n    sz = sizeof(epoch);\n    mallctl(\"epoch\", &epoch, &sz, &epoch, sz);\n    sz = sizeof(size_t);\n    /* Unlike RSS, this does not include RSS from shared libraries and other non\n     * heap mappings. */\n    mallctl(\"stats.resident\", resident, &sz, NULL, 0);\n    /* Unlike resident, this doesn't not include the pages jemalloc reserves\n     * for re-use (purge will clean that). */\n    mallctl(\"stats.active\", active, &sz, NULL, 0);\n    /* Unlike zmalloc_used_memory, this matches the stats.resident by taking\n     * into account all allocations done by this process (not only zmalloc). */\n    mallctl(\"stats.allocated\", allocated, &sz, NULL, 0);\n    return 1;\n}\n\nvoid set_jemalloc_bg_thread(int enable) {\n    /* let jemalloc do purging asynchronously, required when there's no traffic \n     * after flushdb */\n    char val = !!enable;\n    mallctl(\"background_thread\", NULL, 0, &val, 1);\n}\n\nint jemalloc_purge() {\n    /* return all unused (reserved) pages to the OS */\n    char tmp[32];\n    unsigned narenas = 0;\n    size_t sz = sizeof(unsigned);\n    if (!mallctl(\"arenas.narenas\", &narenas, &sz, NULL, 0)) {\n        snprintf(tmp, sizeof(tmp), \"arena.%d.purge\", narenas);\n        if (!mallctl(tmp, NULL, 0, NULL, 0))\n            return 0;\n    }\n    return -1;\n}\n\n#else\n\nint zmalloc_get_allocator_info(size_t *allocated,\n                               size_t *active,\n                               size_t *resident) {\n    *allocated = *resident = *active = 0;\n    return 1;\n}\n\nvoid set_jemalloc_bg_thread(int enable) {\n    ((void)(enable));\n}\n\nint jemalloc_purge() {\n    return 0;\n}\n\n#endif\n\n#if defined(__APPLE__)\n/* For proc_pidinfo() used later in zmalloc_get_smap_bytes_by_field().\n * Note that this file cannot be included in zmalloc.h because it includes\n * a Darwin queue.h file where there is a \"LIST_HEAD\" macro (!) defined\n * conficting with Redis user code. */\n#include <libproc.h>\n#endif\n\n/* Get the sum of the specified field (converted form kb to bytes) in\n * /proc/self/smaps. The field must be specified with trailing \":\" as it\n * apperas in the smaps output.\n *\n * If a pid is specified, the information is extracted for such a pid,\n * otherwise if pid is -1 the information is reported is about the\n * current process.\n *\n * Example: zmalloc_get_smap_bytes_by_field(\"Rss:\",-1);\n */\n#if defined(HAVE_PROC_SMAPS)\nsize_t zmalloc_get_smap_bytes_by_field(const char *field, long pid) {\n    char line[1024];\n    size_t bytes = 0;\n    int flen = strlen(field);\n    FILE *fp;\n\n    if (pid == -1) {\n        fp = fopen(\"/proc/self/smaps\",\"r\");\n    } else {\n        char filename[128];\n        snprintf(filename,sizeof(filename),\"/proc/%ld/smaps\",pid);\n        fp = fopen(filename,\"r\");\n    }\n\n    if (!fp) return 0;\n    while(fgets(line,sizeof(line),fp) != NULL) {\n        if (strncmp(line,field,flen) == 0) {\n            char *p = strchr(line,'k');\n            if (p) {\n                *p = '\\0';\n                bytes += strtol(line+flen,NULL,10) * 1024;\n            }\n        }\n    }\n    fclose(fp);\n    return bytes;\n}\n#else\n/* Get sum of the specified field from libproc api call.\n * As there are per page value basis we need to convert\n * them accordingly.\n *\n * Note that AnonHugePages is a no-op as THP feature\n * is not supported in this platform\n */\nsize_t zmalloc_get_smap_bytes_by_field(const char *field, long pid) {\n#if defined(__APPLE__)\n    struct proc_regioninfo pri;\n    if (pid == -1) pid = getpid();\n    if (proc_pidinfo(pid, PROC_PIDREGIONINFO, 0, &pri,\n                     PROC_PIDREGIONINFO_SIZE) == PROC_PIDREGIONINFO_SIZE)\n    {\n        int pagesize = getpagesize();\n        if (!strcmp(field, \"Private_Dirty:\")) {\n            return (size_t)pri.pri_pages_dirtied * pagesize;\n        } else if (!strcmp(field, \"Rss:\")) {\n            return (size_t)pri.pri_pages_resident * pagesize;\n        } else if (!strcmp(field, \"AnonHugePages:\")) {\n            return 0;\n        }\n    }\n    return 0;\n#endif\n    ((void) field);\n    ((void) pid);\n    return 0;\n}\n#endif\n\n/* Return the total number bytes in pages marked as Private Dirty.\n *\n * Note: depending on the platform and memory footprint of the process, this\n * call can be slow, exceeding 1000ms!\n */\nsize_t zmalloc_get_private_dirty(long pid) {\n    return zmalloc_get_smap_bytes_by_field(\"Private_Dirty:\",pid);\n}\n\n/* Returns the size of physical memory (RAM) in bytes.\n * It looks ugly, but this is the cleanest way to achieve cross platform results.\n * Cleaned up from:\n *\n * http://nadeausoftware.com/articles/2012/09/c_c_tip_how_get_physical_memory_size_system\n *\n * Note that this function:\n * 1) Was released under the following CC attribution license:\n *    http://creativecommons.org/licenses/by/3.0/deed.en_US.\n * 2) Was originally implemented by David Robert Nadeau.\n * 3) Was modified for Redis by Matt Stancliff.\n * 4) This note exists in order to comply with the original license.\n */\nsize_t zmalloc_get_memory_size(void) {\n#if defined(__unix__) || defined(__unix) || defined(unix) || \\\n    (defined(__APPLE__) && defined(__MACH__))\n#if defined(CTL_HW) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM64))\n    int mib[2];\n    mib[0] = CTL_HW;\n#if defined(HW_MEMSIZE)\n    mib[1] = HW_MEMSIZE;            /* OSX. --------------------- */\n#elif defined(HW_PHYSMEM64)\n    mib[1] = HW_PHYSMEM64;          /* NetBSD, OpenBSD. --------- */\n#endif\n    int64_t size = 0;               /* 64-bit */\n    size_t len = sizeof(size);\n    if (sysctl( mib, 2, &size, &len, NULL, 0) == 0)\n        return (size_t)size;\n    return 0L;          /* Failed? */\n\n#elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)\n    /* FreeBSD, Linux, OpenBSD, and Solaris. -------------------- */\n    return (size_t)sysconf(_SC_PHYS_PAGES) * (size_t)sysconf(_SC_PAGESIZE);\n\n#elif defined(CTL_HW) && (defined(HW_PHYSMEM) || defined(HW_REALMEM))\n    /* DragonFly BSD, FreeBSD, NetBSD, OpenBSD, and OSX. -------- */\n    int mib[2];\n    mib[0] = CTL_HW;\n#if defined(HW_REALMEM)\n    mib[1] = HW_REALMEM;        /* FreeBSD. ----------------- */\n#elif defined(HW_PHYSMEM)\n    mib[1] = HW_PHYSMEM;        /* Others. ------------------ */\n#endif\n    unsigned int size = 0;      /* 32-bit */\n    size_t len = sizeof(size);\n    if (sysctl(mib, 2, &size, &len, NULL, 0) == 0)\n        return (size_t)size;\n    return 0L;          /* Failed? */\n#else\n    return 0L;          /* Unknown method to get the data. */\n#endif\n#else\n    return 0L;          /* Unknown OS. */\n#endif\n}\n\n#ifdef REDIS_TEST\n#define UNUSED(x) ((void)(x))\nint zmalloc_test(int argc, char **argv, int accurate) {\n    void *ptr;\n\n    UNUSED(argc);\n    UNUSED(argv);\n    UNUSED(accurate);\n    printf(\"Malloc prefix size: %d\\n\", (int) PREFIX_SIZE);\n    printf(\"Initial used memory: %zu\\n\", zmalloc_used_memory());\n    ptr = zmalloc(123);\n    printf(\"Allocated 123 bytes; used: %zu\\n\", zmalloc_used_memory());\n    ptr = zrealloc(ptr, 456);\n    printf(\"Reallocated to 456 bytes; used: %zu\\n\", zmalloc_used_memory());\n    zfree(ptr);\n    printf(\"Freed pointer; used: %zu\\n\", zmalloc_used_memory());\n    return 0;\n}\n#endif\n"
  },
  {
    "path": "src/zmalloc.h",
    "content": "/* zmalloc - total amount of allocated memory aware version of malloc()\n *\n * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>\n * All rights reserved.\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are met:\n *\n *   * Redistributions of source code must retain the above copyright notice,\n *     this list of conditions and the following disclaimer.\n *   * Redistributions in binary form must reproduce the above copyright\n *     notice, this list of conditions and the following disclaimer in the\n *     documentation and/or other materials provided with the distribution.\n *   * Neither the name of Redis nor the names of its contributors may be used\n *     to endorse or promote products derived from this software without\n *     specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n * POSSIBILITY OF SUCH DAMAGE.\n */\n\n#ifndef __ZMALLOC_H\n#define __ZMALLOC_H\n\n/* Double expansion needed for stringification of macro values. */\n#define __xstr(s) __str(s)\n#define __str(s) #s\n\n#include \"storage.h\"\n#if defined(USE_MEMKIND)\n    #define ZMALLOC_LIB (\"memkind\")\n    #undef USE_JEMALLOC\n    #define USE_MALLOC_CLASS 1\n#elif defined(USE_TCMALLOC)\n#define ZMALLOC_LIB (\"tcmalloc-\" __xstr(TC_VERSION_MAJOR) \".\" __xstr(TC_VERSION_MINOR))\n#include <gperftools/tcmalloc.h>\n#if (TC_VERSION_MAJOR == 1 && TC_VERSION_MINOR >= 6) || (TC_VERSION_MAJOR > 1)\n#define HAVE_MALLOC_SIZE 1\n#define zmalloc_size(p) tc_malloc_size(p)\n#else\n#error \"Newer version of tcmalloc required\"\n#endif\n\n#elif defined(USE_JEMALLOC)\n#define ZMALLOC_LIB (\"jemalloc-\" __xstr(JEMALLOC_VERSION_MAJOR) \".\" __xstr(JEMALLOC_VERSION_MINOR) \".\" __xstr(JEMALLOC_VERSION_BUGFIX))\n#include <jemalloc/jemalloc.h>\n#if (JEMALLOC_VERSION_MAJOR == 2 && JEMALLOC_VERSION_MINOR >= 1) || (JEMALLOC_VERSION_MAJOR > 2)\n#define HAVE_MALLOC_SIZE 1\n#define zmalloc_size(p) malloc_usable_size(p)\n#else\n#error \"Newer version of jemalloc required\"\n#endif\n\n#elif defined(__APPLE__)\n#include <malloc/malloc.h>\n#define HAVE_MALLOC_SIZE 1\n#define zmalloc_size(p) malloc_size(p)\n#endif\n\n/* On native libc implementations, we should still do our best to provide a\n * HAVE_MALLOC_SIZE capability. This can be set explicitly as well:\n *\n * NO_MALLOC_USABLE_SIZE disables it on all platforms, even if they are\n *      known to support it.\n * USE_MALLOC_USABLE_SIZE forces use of malloc_usable_size() regardless\n *      of platform.\n */\n#ifndef ZMALLOC_LIB\n#define ZMALLOC_LIB \"libc\"\n\n#if !defined(NO_MALLOC_USABLE_SIZE) && \\\n    (defined(__GLIBC__) || defined(__FreeBSD__) || \\\n     defined(USE_MALLOC_USABLE_SIZE))\n\n/* Includes for malloc_usable_size() */\n#ifdef __FreeBSD__\n#include <malloc_np.h>\n#else\n#include <malloc.h>\n#endif\n\n#define HAVE_MALLOC_SIZE 1\n#define zmalloc_size(p) malloc_usable_size(p)\n\n#endif\n#endif\n\n/* We can enable the Redis defrag capabilities only if we are using Jemalloc\n * and the version used is our special version modified for Redis having\n * the ability to return per-allocation fragmentation hints. */\n#if defined(USE_JEMALLOC) && defined(JEMALLOC_FRAG_HINT)\n#define HAVE_DEFRAG\n#endif\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#ifdef __cplusplus\nvoid *zmalloc(size_t size, enum MALLOC_CLASS mclass = MALLOC_LOCAL);\nvoid *zcalloc(size_t size, enum MALLOC_CLASS mclass = MALLOC_LOCAL);\nvoid *zrealloc(void *ptr, size_t size, enum MALLOC_CLASS mclass = MALLOC_LOCAL);\n#else\nvoid *zmalloc(size_t size, enum MALLOC_CLASS mclass);\nvoid *zcalloc(size_t size, enum MALLOC_CLASS mclass);\nvoid *zrealloc(void *ptr, size_t size, enum MALLOC_CLASS mclass);\n#endif\nvoid *ztrymalloc(size_t size);\nvoid *ztrycalloc(size_t size);\nvoid *ztryrealloc(void *ptr, size_t size);\nvoid zfree(const void *ptr);\nvoid *zmalloc_usable(size_t size, size_t *usable);\nvoid *zcalloc_usable(size_t size, size_t *usable);\nvoid *zrealloc_usable(void *ptr, size_t size, size_t *usable);\nvoid *ztrymalloc_usable(size_t size, size_t *usable);\nvoid *ztrycalloc_usable(size_t size, size_t *usable);\nvoid *ztryrealloc_usable(void *ptr, size_t size, size_t *usable);\nvoid zfree_usable(void *ptr, size_t *usable);\nchar *zstrdup(const char *s);\nsize_t zmalloc_used_memory(void);\nvoid zmalloc_set_oom_handler(void (*oom_handler)(size_t));\nsize_t zmalloc_get_rss(void);\nint zmalloc_get_allocator_info(size_t *allocated, size_t *active, size_t *resident);\nvoid set_jemalloc_bg_thread(int enable);\nint jemalloc_purge();\nsize_t zmalloc_get_private_dirty(long pid);\nsize_t zmalloc_get_smap_bytes_by_field(const char *field, long pid);\nsize_t zmalloc_get_memory_size(void);\nvoid zlibc_free(void *ptr);\n\n#ifdef HAVE_DEFRAG\nvoid zfree_no_tcache(void *ptr);\nvoid *zmalloc_no_tcache(size_t size);\n#endif\n\n#ifndef HAVE_MALLOC_SIZE\nsize_t zmalloc_size(void *ptr);\nsize_t zmalloc_usable_size(void *ptr);\n#else\n#define zmalloc_usable_size(p) zmalloc_size(p)\n#endif\n\n#ifdef REDIS_TEST\nint zmalloc_test(int argc, char **argv, int accurate);\n#endif\n\n#ifdef __cplusplus\n}\n#endif\n\n#ifdef __cplusplus\n#include \"new.h\"\n#endif\n\n#endif /* __ZMALLOC_H */\n"
  },
  {
    "path": "tests/assets/default.conf",
    "content": "# KeyDB configuration for testing.\n\nalways-show-logo yes\nnotify-keyspace-events KEA\ndaemonize no\npidfile /var/run/keydb.pid\nport 6379\ntimeout 0\nbind 127.0.0.1\nloglevel verbose\nlogfile ''\ndatabases 16\nlatency-monitor-threshold 1\n\nsave 900 1\nsave 300 10\nsave 60 10000\n\nrdbcompression yes\ndbfilename dump.rdb\ndir ./\n\nslave-serve-stale-data yes\nappendonly no\nappendfsync everysec\nno-appendfsync-on-rewrite no\nactiverehashing yes\n\n"
  },
  {
    "path": "tests/assets/minimal.conf",
    "content": "# Minimal configuration for testing.\nbind 127.0.0.1\nalways-show-logo yes\ndaemonize no\npidfile /var/run/keydb.pid\nloglevel verbose\n"
  },
  {
    "path": "tests/assets/nodefaultuser.acl",
    "content": "user alice on nopass ~* +@all\nuser bob on nopass ~* &* +@all"
  },
  {
    "path": "tests/assets/user.acl",
    "content": "user alice on allcommands allkeys >alice\nuser bob on -@all +@set +acl ~set* >bob\nuser default on nopass ~* +@all\n"
  },
  {
    "path": "tests/cluster/cluster.tcl",
    "content": "# Cluster-specific test functions.\n#\n# Copyright (C) 2014 Salvatore Sanfilippo antirez@gmail.com\n# This software is released under the BSD License. See the COPYING file for\n# more information.\n\n# Track cluster configuration as created by create_cluster below\nset ::cluster_master_nodes 0\nset ::cluster_replica_nodes 0\n\n# Returns a parsed CLUSTER NODES output as a list of dictionaries.\nproc get_cluster_nodes id {\n    set lines [split [R $id cluster nodes] \"\\r\\n\"]\n    set nodes {}\n    foreach l $lines {\n        set l [string trim $l]\n        if {$l eq {}} continue\n        set args [split $l]\n        set node [dict create \\\n            id [lindex $args 0] \\\n            addr [lindex $args 1] \\\n            flags [split [lindex $args 2] ,] \\\n            slaveof [lindex $args 3] \\\n            ping_sent [lindex $args 4] \\\n            pong_recv [lindex $args 5] \\\n            config_epoch [lindex $args 6] \\\n            linkstate [lindex $args 7] \\\n            slots [lrange $args 8 end] \\\n        ]\n        lappend nodes $node\n    }\n    return $nodes\n}\n\n# Test node for flag.\nproc has_flag {node flag} {\n    expr {[lsearch -exact [dict get $node flags] $flag] != -1}\n}\n\n# Returns the parsed myself node entry as a dictionary.\nproc get_myself id {\n    set nodes [get_cluster_nodes $id]\n    foreach n $nodes {\n        if {[has_flag $n myself]} {return $n}\n    }\n    return {}\n}\n\n# Get a specific node by ID by parsing the CLUSTER NODES output\n# of the instance Number 'instance_id'\nproc get_node_by_id {instance_id node_id} {\n    set nodes [get_cluster_nodes $instance_id]\n    foreach n $nodes {\n        if {[dict get $n id] eq $node_id} {return $n}\n    }\n    return {}\n}\n\n# Return the value of the specified CLUSTER INFO field.\nproc CI {n field} {\n    get_info_field [R $n cluster info] $field\n}\n\n# Return the value of the specified INFO field.\nproc s {n field} {\n    get_info_field [R $n info] $field\n}\n\n# Assuming nodes are reest, this function performs slots allocation.\n# Only the first 'n' nodes are used.\nproc cluster_allocate_slots {n} {\n    set slot 16383\n    while {$slot >= 0} {\n        # Allocate successive slots to random nodes.\n        set node [randomInt $n]\n        lappend slots_$node $slot\n        incr slot -1\n    }\n    for {set j 0} {$j < $n} {incr j} {\n        R $j cluster addslots {*}[set slots_${j}]\n    }\n}\n\n# Check that cluster nodes agree about \"state\", or raise an error.\nproc assert_cluster_state {state} {\n    foreach_redis_id id {\n        if {[instance_is_killed redis $id]} continue\n        wait_for_condition 1000 50 {\n            [CI $id cluster_state] eq $state\n        } else {\n            fail \"Cluster node $id cluster_state:[CI $id cluster_state]\"\n        }\n    }\n}\n\n# Search the first node starting from ID $first that is not\n# already configured as a slave.\nproc cluster_find_available_slave {first} {\n    foreach_redis_id id {\n        if {$id < $first} continue\n        if {[instance_is_killed redis $id]} continue\n        set me [get_myself $id]\n        if {[dict get $me slaveof] eq {-}} {return $id}\n    }\n    fail \"No available slaves\"\n}\n\n# Add 'slaves' slaves to a cluster composed of 'masters' masters.\n# It assumes that masters are allocated sequentially from instance ID 0\n# to N-1.\nproc cluster_allocate_slaves {masters slaves} {\n    for {set j 0} {$j < $slaves} {incr j} {\n        set master_id [expr {$j % $masters}]\n        set slave_id [cluster_find_available_slave $masters]\n        set master_myself [get_myself $master_id]\n        R $slave_id cluster replicate [dict get $master_myself id]\n    }\n}\n\n# Create a cluster composed of the specified number of masters and slaves.\nproc create_cluster {masters slaves} {\n    cluster_allocate_slots $masters\n    if {$slaves} {\n        cluster_allocate_slaves $masters $slaves\n    }\n    assert_cluster_state ok\n\n    set ::cluster_master_nodes $masters\n    set ::cluster_replica_nodes $slaves\n}\n\n# Set the cluster node-timeout to all the reachalbe nodes.\nproc set_cluster_node_timeout {to} {\n    foreach_redis_id id {\n        catch {R $id CONFIG SET cluster-node-timeout $to}\n    }\n}\n\n# Check if the cluster is writable and readable. Use node \"id\"\n# as a starting point to talk with the cluster.\nproc cluster_write_test {id} {\n    set prefix [randstring 20 20 alpha]\n    set port [get_instance_attrib redis $id port]\n    set cluster [redis_cluster 127.0.0.1:$port]\n    for {set j 0} {$j < 100} {incr j} {\n        $cluster set key.$j $prefix.$j\n    }\n    for {set j 0} {$j < 100} {incr j} {\n        assert {[$cluster get key.$j] eq \"$prefix.$j\"}\n    }\n    $cluster close\n}\n\n# Check if cluster configuration is consistent.\nproc cluster_config_consistent {} {\n    for {set j 0} {$j < $::cluster_master_nodes + $::cluster_replica_nodes} {incr j} {\n        if {$j == 0} {\n            set base_cfg [R $j cluster slots]\n        } else {\n            set cfg [R $j cluster slots]\n            if {$cfg != $base_cfg} {\n                return 0\n            }\n        }\n    }\n\n    return 1\n}\n\n# Wait for cluster configuration to propagate and be consistent across nodes.\nproc wait_for_cluster_propagation {} {\n    wait_for_condition 50 100 {\n        [cluster_config_consistent] eq 1\n    } else {\n        fail \"cluster config did not reach a consistent state\"\n    }\n}\n"
  },
  {
    "path": "tests/cluster/run.tcl",
    "content": "# Cluster test suite. Copyright (C) 2014 Salvatore Sanfilippo antirez@gmail.com\n# This software is released under the BSD License. See the COPYING file for\n# more information.\n\ncd tests/cluster\nsource cluster.tcl\nsource ../instances.tcl\nsource ../../support/cluster.tcl ; # Redis Cluster client.\n\nset ::instances_count 20 ; # How many instances we use at max.\nset ::tlsdir \"../../tls\"\n\nproc main {} {\n    parse_options\n    spawn_instance redis $::redis_base_port $::instances_count {\n\t\"bind 127.0.0.1\"\n\t\"cluster-enabled yes\"\n\t\"appendonly yes\"\n\t\"testmode yes\"\n\t\"server-threads 3\"\n    }\n    run_tests\n    cleanup\n    end_tests\n}\n\nif {[catch main e]} {\n    puts $::errorInfo\n    if {$::pause_on_error} pause_on_error\n    cleanup\n    exit 1\n}\n"
  },
  {
    "path": "tests/cluster/tests/00-base.tcl",
    "content": "# Check the basic monitoring and failover capabilities.\n\nsource \"../tests/includes/init-tests.tcl\"\n\nif {$::simulate_error} {\n    test \"This test will fail\" {\n        fail \"Simulated error\"\n    }\n}\n\ntest \"Different nodes have different IDs\" {\n    set ids {}\n    set numnodes 0\n    foreach_redis_id id {\n        incr numnodes\n        # Every node should just know itself.\n        set nodeid [dict get [get_myself $id] id]\n        assert {$nodeid ne {}}\n        lappend ids $nodeid\n    }\n    set numids [llength [lsort -unique $ids]]\n    assert {$numids == $numnodes}\n}\n\ntest \"It is possible to perform slot allocation\" {\n    cluster_allocate_slots 5\n}\n\ntest \"After the join, every node gets a different config epoch\" {\n    set trynum 60\n    while {[incr trynum -1] != 0} {\n        # We check that this condition is true for *all* the nodes.\n        set ok 1 ; # Will be set to 0 every time a node is not ok.\n        foreach_redis_id id {\n            set epochs {}\n            foreach n [get_cluster_nodes $id] {\n                lappend epochs [dict get $n config_epoch]\n            }\n            if {[lsort $epochs] != [lsort -unique $epochs]} {\n                set ok 0 ; # At least one collision!\n            }\n        }\n        if {$ok} break\n        after 1000\n        puts -nonewline .\n        flush stdout\n    }\n    if {$trynum == 0} {\n        fail \"Config epoch conflict resolution is not working.\"\n    }\n}\n\ntest \"Nodes should report cluster_state is ok now\" {\n    assert_cluster_state ok\n}\n\ntest \"It is possible to write and read from the cluster\" {\n    cluster_write_test 0\n}\n"
  },
  {
    "path": "tests/cluster/tests/01-faildet.tcl",
    "content": "# Check the basic monitoring and failover capabilities.\n\nsource \"../tests/includes/init-tests.tcl\"\n\ntest \"Create a 5 nodes cluster\" {\n    create_cluster 5 5\n}\n\ntest \"Cluster should start ok\" {\n    assert_cluster_state ok\n}\n\ntest \"Killing two slave nodes\" {\n    kill_instance redis 5\n    kill_instance redis 6\n}\n\ntest \"Cluster should be still up\" {\n    assert_cluster_state ok\n}\n\ntest \"Killing one master node\" {\n    kill_instance redis 0\n}\n\n# Note: the only slave of instance 0 is already down so no\n# failover is possible, that would change the state back to ok.\ntest \"Cluster should be down now\" {\n    assert_cluster_state fail\n}\n\ntest \"Restarting master node\" {\n    restart_instance redis 0\n}\n\ntest \"Cluster should be up again\" {\n    assert_cluster_state ok\n}\n"
  },
  {
    "path": "tests/cluster/tests/02-failover.tcl",
    "content": "# Check the basic monitoring and failover capabilities.\n\nsource \"../tests/includes/init-tests.tcl\"\n\ntest \"Create a 5 nodes cluster\" {\n    create_cluster 5 5\n}\n\ntest \"Cluster is up\" {\n    assert_cluster_state ok\n}\n\ntest \"Cluster is writable\" {\n    cluster_write_test 0\n}\n\ntest \"Instance #5 is a slave\" {\n    assert {[RI 5 role] eq {slave}}\n}\n\ntest \"Instance #5 synced with the master\" {\n    wait_for_condition 1000 50 {\n        [RI 5 master_link_status] eq {up}\n    } else {\n        fail \"Instance #5 master link status is not up\"\n    }\n}\n\nset current_epoch [CI 1 cluster_current_epoch]\n\ntest \"Killing one master node\" {\n    kill_instance redis 0\n}\n\ntest \"Wait for failover\" {\n    wait_for_condition 1000 50 {\n        [CI 1 cluster_current_epoch] > $current_epoch\n    } else {\n        fail \"No failover detected\"\n    }\n}\n\ntest \"Cluster should eventually be up again\" {\n    assert_cluster_state ok\n}\n\ntest \"Cluster is writable\" {\n    cluster_write_test 1\n}\n\ntest \"Instance #5 is now a master\" {\n    assert {[RI 5 role] eq {master}}\n}\n\ntest \"Restarting the previously killed master node\" {\n    restart_instance redis 0\n}\n\ntest \"Instance #0 gets converted into a slave\" {\n    wait_for_condition 1000 50 {\n        [RI 0 role] eq {slave}\n    } else {\n        fail \"Old master was not converted into slave\"\n    }\n}\n"
  },
  {
    "path": "tests/cluster/tests/03-failover-loop.tcl",
    "content": "# Failover stress test.\n# In this test a different node is killed in a loop for N\n# iterations. The test checks that certain properties\n# are preseved across iterations.\n\nsource \"../tests/includes/init-tests.tcl\"\n\ntest \"Create a 5 nodes cluster\" {\n    create_cluster 5 5\n}\n\ntest \"Cluster is up\" {\n    assert_cluster_state ok\n}\n\nset iterations 20\nset cluster [redis_cluster 127.0.0.1:[get_instance_attrib redis 0 port]]\n\nwhile {[incr iterations -1]} {\n    set tokill [randomInt 10]\n    set other [expr {($tokill+1)%10}] ; # Some other instance.\n    set key [randstring 20 20 alpha]\n    set val [randstring 20 20 alpha]\n    set role [RI $tokill role]\n    if {$role eq {master}} {\n        set slave {}\n        set myid [dict get [get_myself $tokill] id]\n        foreach_redis_id id {\n            if {$id == $tokill} continue\n            if {[dict get [get_myself $id] slaveof] eq $myid} {\n                set slave $id\n            }\n        }\n        if {$slave eq {}} {\n            fail \"Unable to retrieve slave's ID for master #$tokill\"\n        }\n    }\n\n    puts \"--- Iteration $iterations ---\"\n\n    if {$role eq {master}} {\n        test \"Wait for slave of #$tokill to sync\" {\n            wait_for_condition 1000 50 {\n                [string match {*state=online*} [RI $tokill slave0]]\n            } else {\n                fail \"Slave of node #$tokill is not ok\"\n            }\n        }\n        set slave_config_epoch [CI $slave cluster_my_epoch]\n    }\n\n    test \"Cluster is writable before failover\" {\n        for {set i 0} {$i < 100} {incr i} {\n            catch {$cluster set $key:$i $val:$i} err\n            assert {$err eq {OK}}\n        }\n        # Wait for the write to propagate to the slave if we\n        # are going to kill a master.\n        if {$role eq {master}} {\n            R $tokill wait 1 20000\n        }\n    }\n\n    test \"Killing node #$tokill\" {\n        kill_instance redis $tokill\n    }\n\n    if {$role eq {master}} {\n        test \"Wait failover by #$slave with old epoch $slave_config_epoch\" {\n            wait_for_condition 1000 50 {\n                [CI $slave cluster_my_epoch] > $slave_config_epoch\n            } else {\n                fail \"No failover detected, epoch is still [CI $slave cluster_my_epoch]\"\n            }\n        }\n    }\n\n    test \"Cluster should eventually be up again\" {\n        assert_cluster_state ok\n    }\n\n    test \"Cluster is writable again\" {\n        for {set i 0} {$i < 100} {incr i} {\n            catch {$cluster set $key:$i:2 $val:$i:2} err\n            assert {$err eq {OK}}\n        }\n    }\n\n    test \"Restarting node #$tokill\" {\n        restart_instance redis $tokill\n    }\n\n    test \"Instance #$tokill is now a slave\" {\n        wait_for_condition 1000 50 {\n            [RI $tokill role] eq {slave}\n        } else {\n            fail \"Restarted instance is not a slave\"\n        }\n    }\n\n    test \"We can read back the value we set before\" {\n        for {set i 0} {$i < 100} {incr i} {\n            catch {$cluster get $key:$i} err\n            assert {$err eq \"$val:$i\"}\n            catch {$cluster get $key:$i:2} err\n            assert {$err eq \"$val:$i:2\"}\n        }\n    }\n}\n\ntest \"Post condition: current_epoch >= my_epoch everywhere\" {\n    foreach_redis_id id {\n        assert {[CI $id cluster_current_epoch] >= [CI $id cluster_my_epoch]}\n    }\n}\n"
  },
  {
    "path": "tests/cluster/tests/04-resharding.tcl",
    "content": "# Failover stress test.\n# In this test a different node is killed in a loop for N\n# iterations. The test checks that certain properties\n# are preserved across iterations.\n\nsource \"../tests/includes/init-tests.tcl\"\nsource \"../../../tests/support/cli.tcl\"\n\ntest \"Create a 5 nodes cluster\" {\n    create_cluster 5 5\n}\n\ntest \"Cluster is up\" {\n    assert_cluster_state ok\n}\n\ntest \"Enable AOF in all the instances\" {\n    foreach_redis_id id {\n        R $id config set appendonly yes\n        # We use \"appendfsync no\" because it's fast but also guarantees that\n        # write(2) is performed before replying to client.\n        R $id config set appendfsync no\n    }\n\n    foreach_redis_id id {\n        wait_for_condition 1000 500 {\n            [RI $id aof_rewrite_in_progress] == 0 &&\n            [RI $id aof_enabled] == 1\n        } else {\n            fail \"Failed to enable AOF on instance #$id\"\n        }\n    }\n}\n\n# Return non-zero if the specified PID is about a process still in execution,\n# otherwise 0 is returned.\nproc process_is_running {pid} {\n    # PS should return with an error if PID is non existing,\n    # and catch will return non-zero. We want to return non-zero if\n    # the PID exists, so we invert the return value with expr not operator.\n    expr {![catch {exec ps -p $pid}]}\n}\n\n# Our resharding test performs the following actions:\n#\n# - N commands are sent to the cluster in the course of the test.\n# - Every command selects a random key from key:0 to key:MAX-1.\n# - The operation RPUSH key <randomvalue> is performed.\n# - Tcl remembers into an array all the values pushed to each list.\n# - After N/2 commands, the resharding process is started in background.\n# - The test continues while the resharding is in progress.\n# - At the end of the test, we wait for the resharding process to stop.\n# - Finally the keys are checked to see if they contain the value they should.\n\nset numkeys 50000\nset numops 200000\nset start_node_port [get_instance_attrib redis 0 port]\nset cluster [redis_cluster 127.0.0.1:$start_node_port]\nif {$::tls} {\n    # setup a non-TLS cluster client to the TLS cluster\n    set plaintext_port [get_instance_attrib redis 0 plaintext-port]\n    set cluster_plaintext [redis_cluster 127.0.0.1:$plaintext_port 0]\n    puts \"Testing TLS cluster on start node 127.0.0.1:$start_node_port, plaintext port $plaintext_port\"\n} else {\n    set cluster_plaintext $cluster\n    puts \"Testing using non-TLS cluster\"\n}\ncatch {unset content}\narray set content {}\nset tribpid {}\n\ntest \"Cluster consistency during live resharding\" {\n    set ele 0\n    for {set j 0} {$j < $numops} {incr j} {\n        # Trigger the resharding once we execute half the ops.\n        if {$tribpid ne {} &&\n            ($j % 10000) == 0 &&\n            ![process_is_running $tribpid]} {\n            set tribpid {}\n        }\n\n        if {$j >= $numops/2 && $tribpid eq {}} {\n            puts -nonewline \"...Starting resharding...\"\n            flush stdout\n            set target [dict get [get_myself [randomInt 5]] id]\n            set tribpid [lindex [exec \\\n                ../../../src/keydb-cli --cluster reshard \\\n                127.0.0.1:[get_instance_attrib redis 0 port] \\\n                --cluster-from all \\\n                --cluster-to $target \\\n                --cluster-slots 100 \\\n                --cluster-yes \\\n                {*}[rediscli_tls_config \"../../../tests\"] \\\n                | [info nameofexecutable] \\\n                ../tests/helpers/onlydots.tcl \\\n                &] 0]\n        }\n\n        # Write random data to random list.\n        set listid [randomInt $numkeys]\n        set key \"key:$listid\"\n        incr ele\n        # We write both with Lua scripts and with plain commands.\n        # This way we are able to stress Lua -> Redis command invocation\n        # as well, that has tests to prevent Lua to write into wrong\n        # hash slots.\n        # We also use both TLS and plaintext connections.\n        if {$listid % 3 == 0} {\n            $cluster rpush $key $ele\n        } elseif {$listid % 3 == 1} {\n            $cluster_plaintext rpush $key $ele\n        } else {\n            $cluster eval {redis.call(\"rpush\",KEYS[1],ARGV[1])} 1 $key $ele\n        }\n        lappend content($key) $ele\n\n        if {($j % 1000) == 0} {\n            puts -nonewline W; flush stdout\n        }\n    }\n\n    # Wait for the resharding process to end\n    wait_for_condition 1000 500 {\n        [process_is_running $tribpid] == 0\n    } else {\n        fail \"Resharding is not terminating after some time.\"\n    }\n\n}\n\ntest \"Verify $numkeys keys for consistency with logical content\" {\n    # Check that the Redis Cluster content matches our logical content.\n    foreach {key value} [array get content] {\n        if {[$cluster lrange $key 0 -1] ne $value} {\n            fail \"Key $key expected to hold '$value' but actual content is [$cluster lrange $key 0 -1]\"\n        }\n    }\n}\n\ntest \"Crash and restart all the instances\" {\n    foreach_redis_id id {\n        kill_instance redis $id\n        restart_instance redis $id\n    }\n}\n\ntest \"Cluster should eventually be up again\" {\n    assert_cluster_state ok\n}\n\ntest \"Verify $numkeys keys after the crash & restart\" {\n    # Check that the Redis Cluster content matches our logical content.\n    foreach {key value} [array get content] {\n        if {[$cluster lrange $key 0 -1] ne $value} {\n            fail \"Key $key expected to hold '$value' but actual content is [$cluster lrange $key 0 -1]\"\n        }\n    }\n}\n\ntest \"Disable AOF in all the instances\" {\n    foreach_redis_id id {\n        R $id config set appendonly no\n    }\n}\n\ntest \"Verify slaves consistency\" {\n    set verified_masters 0\n    foreach_redis_id id {\n        set role [R $id role]\n        lassign $role myrole myoffset slaves\n        if {$myrole eq {slave}} continue\n        set masterport [get_instance_attrib redis $id port]\n        set masterdigest [R $id debug digest]\n        foreach_redis_id sid {\n            set srole [R $sid role]\n            if {[lindex $srole 0] eq {master}} continue\n            if {[lindex $srole 2] != $masterport} continue\n            wait_for_condition 1000 500 {\n                [R $sid debug digest] eq $masterdigest\n            } else {\n                fail \"Master and slave data digest are different\"\n            }\n            incr verified_masters\n        }\n    }\n    assert {$verified_masters >= 5}\n}\n\ntest \"Dump sanitization was skipped for migrations\" {\n    set verified_masters 0\n    foreach_redis_id id {\n        assert {[RI $id dump_payload_sanitizations] == 0}\n    }\n}\n"
  },
  {
    "path": "tests/cluster/tests/05-slave-selection.tcl",
    "content": "# Slave selection test\n# Check the algorithm trying to pick the slave with the most complete history.\n\nsource \"../tests/includes/init-tests.tcl\"\n\n# Create a cluster with 5 master and 10 slaves, so that we have 2\n# slaves for each master.\ntest \"Create a 5 nodes cluster\" {\n    create_cluster 5 10\n}\n\ntest \"Cluster is up\" {\n    assert_cluster_state ok\n}\n\ntest \"The first master has actually two slaves\" {\n    assert {[llength [lindex [R 0 role] 2]] == 2}\n}\n\ntest {Slaves of #0 are instance #5 and #10 as expected} {\n    set port0 [get_instance_attrib redis 0 port]\n    assert {[lindex [R 5 role] 2] == $port0}\n    assert {[lindex [R 10 role] 2] == $port0}\n}\n\ntest \"Instance #5 and #10 synced with the master\" {\n    wait_for_condition 1000 50 {\n        [RI 5 master_link_status] eq {up} &&\n        [RI 10 master_link_status] eq {up}\n    } else {\n        fail \"Instance #5 or #10 master link status is not up\"\n    }\n}\n\nset cluster [redis_cluster 127.0.0.1:[get_instance_attrib redis 0 port]]\n\ntest \"Slaves are both able to receive and acknowledge writes\" {\n    for {set j 0} {$j < 100} {incr j} {\n        $cluster set $j $j\n    }\n    assert {[R 0 wait 2 60000] == 2}\n}\n\ntest \"Write data while slave #10 is paused and can't receive it\" {\n    # Stop the slave with a multi/exec transaction so that the master will\n    # be killed as soon as it can accept writes again.\n    R 10 multi\n    R 10 debug sleep 10\n    R 10 client kill 127.0.0.1:$port0\n    R 10 deferred 1\n    R 10 exec\n\n    # Write some data the slave can't receive.\n    for {set j 0} {$j < 100} {incr j} {\n        $cluster set $j $j\n    }\n\n    # Prevent the master from accepting new slaves.\n    # Use a large pause value since we'll kill it anyway.\n    R 0 CLIENT PAUSE 60000\n\n    # Wait for the slave to return available again\n    R 10 deferred 0\n    assert {[R 10 read] eq {OK OK}}\n\n    # Kill the master so that a reconnection will not be possible.\n    kill_instance redis 0\n}\n\ntest \"Wait for instance #5 (and not #10) to turn into a master\" {\n    wait_for_condition 1000 50 {\n        [RI 5 role] eq {master}\n    } else {\n        fail \"No failover detected\"\n    }\n}\n\ntest \"Wait for the node #10 to return alive before ending the test\" {\n    R 10 ping\n}\n\ntest \"Cluster should eventually be up again\" {\n    assert_cluster_state ok\n}\n\ntest \"Node #10 should eventually replicate node #5\" {\n    set port5 [get_instance_attrib redis 5 port]\n    wait_for_condition 1000 50 {\n        ([lindex [R 10 role] 2] == $port5) &&\n        ([lindex [R 10 role] 3] eq {connected})\n    } else {\n        fail \"#10 didn't became slave of #5\"\n    }\n}\n\nsource \"../tests/includes/init-tests.tcl\"\n\n# Create a cluster with 3 master and 15 slaves, so that we have 5\n# slaves for eatch master.\ntest \"Create a 3 nodes cluster\" {\n    create_cluster 3 15\n}\n\ntest \"Cluster is up\" {\n    assert_cluster_state ok\n}\n\ntest \"The first master has actually 5 slaves\" {\n    assert {[llength [lindex [R 0 role] 2]] == 5}\n}\n\ntest {Slaves of #0 are instance #3, #6, #9, #12 and #15 as expected} {\n    set port0 [get_instance_attrib redis 0 port]\n    assert {[lindex [R 3 role] 2] == $port0}\n    assert {[lindex [R 6 role] 2] == $port0}\n    assert {[lindex [R 9 role] 2] == $port0}\n    assert {[lindex [R 12 role] 2] == $port0}\n    assert {[lindex [R 15 role] 2] == $port0}\n}\n\ntest {Instance #3, #6, #9, #12 and #15 synced with the master} {\n    wait_for_condition 1000 50 {\n        [RI 3 master_link_status] eq {up} &&\n        [RI 6 master_link_status] eq {up} &&\n        [RI 9 master_link_status] eq {up} &&\n        [RI 12 master_link_status] eq {up} &&\n        [RI 15 master_link_status] eq {up}\n    } else {\n        fail \"Instance #3 or #6 or #9 or #12 or #15 master link status is not up\"\n    }\n}\n\nproc master_detected {instances} {\n    foreach instance [dict keys $instances] {\n        if {[RI $instance role] eq {master}} {\n            return true\n        }\n    }\n\n    return false\n}\n\ntest \"New Master down consecutively\" {\n    set instances [dict create 0 1 3 1 6 1 9 1 12 1 15 1]\n\n    set loops [expr {[dict size $instances]-1}]\n    for {set i 0} {$i < $loops} {incr i} {\n        set master_id -1\n        foreach instance [dict keys $instances] {\n            if {[RI $instance role] eq {master}} {\n                set master_id $instance\n                break;\n            }\n        }\n\n        if {$master_id eq -1} {\n            fail \"no master detected, #loop $i\"\n        }\n\n        set instances [dict remove $instances $master_id]\n\n        kill_instance redis $master_id\n        wait_for_condition 1000 50 {\n            [master_detected $instances]\n        } else {\n            fail \"No failover detected when master $master_id fails\"\n        }\n\n        assert_cluster_state ok\n    }\n}\n"
  },
  {
    "path": "tests/cluster/tests/06-slave-stop-cond.tcl",
    "content": "# Slave stop condition test\n# Check that if there is a disconnection time limit, the slave will not try\n# to failover its master.\n\nsource \"../tests/includes/init-tests.tcl\"\n\n# Create a cluster with 5 master and 5 slaves.\ntest \"Create a 5 nodes cluster\" {\n    create_cluster 5 5\n}\n\ntest \"Cluster is up\" {\n    assert_cluster_state ok\n}\n\ntest \"The first master has actually one slave\" {\n    assert {[llength [lindex [R 0 role] 2]] == 1}\n}\n\ntest {Slaves of #0 is instance #5 as expected} {\n    set port0 [get_instance_attrib redis 0 port]\n    assert {[lindex [R 5 role] 2] == $port0}\n}\n\ntest \"Instance #5 synced with the master\" {\n    wait_for_condition 1000 50 {\n        [RI 5 master_link_status] eq {up}\n    } else {\n        fail \"Instance #5 master link status is not up\"\n    }\n}\n\ntest \"Lower the slave validity factor of #5 to the value of 2\" {\n    assert {[R 5 config set cluster-slave-validity-factor 2] eq {OK}}\n}\n\ntest \"Break master-slave link and prevent further reconnections\" {\n    # Stop the slave with a multi/exec transaction so that the master will\n    # be killed as soon as it can accept writes again.\n    R 5 multi\n    R 5 client kill 127.0.0.1:$port0\n    # here we should sleep 6 or more seconds (node_timeout * slave_validity)\n    # but the actual validity time is actually incremented by the\n    # repl-ping-slave-period value which is 10 seconds by default. So we\n    # need to wait more than 16 seconds.\n    R 5 debug sleep 20\n    R 5 deferred 1\n    R 5 exec\n\n    # Prevent the master from accepting new slaves.\n    # Use a large pause value since we'll kill it anyway.\n    R 0 CLIENT PAUSE 60000\n\n    # Wait for the slave to return available again\n    R 5 deferred 0\n    assert {[R 5 read] eq {OK OK}}\n\n    # Kill the master so that a reconnection will not be possible.\n    kill_instance redis 0\n}\n\ntest \"Slave #5 is reachable and alive\" {\n    assert {[R 5 ping] eq {PONG}}\n}\n\ntest \"Slave #5 should not be able to failover\" {\n    after 10000\n    assert_equal {slave} [RI 5 role]\n}\n\ntest \"Cluster should be down\" {\n    assert_cluster_state fail\n}\n"
  },
  {
    "path": "tests/cluster/tests/07-replica-migration.tcl",
    "content": "# Replica migration test.\n# Check that orphaned masters are joined by replicas of masters having\n# multiple replicas attached, according to the migration barrier settings.\n\nsource \"../tests/includes/init-tests.tcl\"\n\n# Create a cluster with 5 master and 10 slaves, so that we have 2\n# slaves for each master.\ntest \"Create a 5 nodes cluster\" {\n    create_cluster 5 10\n}\n\ntest \"Cluster is up\" {\n    assert_cluster_state ok\n}\n\ntest \"Each master should have two replicas attached\" {\n    foreach_redis_id id {\n        if {$id < 5} {\n            wait_for_condition 1000 50 {\n                [llength [lindex [R 0 role] 2]] == 2\n            } else {\n                fail \"Master #$id does not have 2 slaves as expected\"\n            }\n        }\n    }\n}\n\ntest \"Killing all the slaves of master #0 and #1\" {\n    kill_instance redis 5\n    kill_instance redis 10\n    kill_instance redis 6\n    kill_instance redis 11\n    after 4000\n}\n\nforeach_redis_id id {\n    if {$id < 5} {\n        test \"Master #$id should have at least one replica\" {\n            wait_for_condition 1000 50 {\n                [llength [lindex [R $id role] 2]] >= 1\n            } else {\n                fail \"Master #$id has no replicas\"\n            }\n        }\n    }\n}\n\n# Now test the migration to a master which used to be a slave, after\n# a failver.\n\nsource \"../tests/includes/init-tests.tcl\"\n\n# Create a cluster with 5 master and 10 slaves, so that we have 2\n# slaves for each master.\ntest \"Create a 5 nodes cluster\" {\n    create_cluster 5 10\n}\n\ntest \"Cluster is up\" {\n    assert_cluster_state ok\n}\n\ntest \"Kill slave #7 of master #2. Only slave left is #12 now\" {\n    kill_instance redis 7\n}\n\nset current_epoch [CI 1 cluster_current_epoch]\n\ntest \"Killing master node #2, #12 should failover\" {\n    kill_instance redis 2\n}\n\ntest \"Wait for failover\" {\n    wait_for_condition 1000 50 {\n        [CI 1 cluster_current_epoch] > $current_epoch\n    } else {\n        fail \"No failover detected\"\n    }\n}\n\ntest \"Cluster should eventually be up again\" {\n    assert_cluster_state ok\n}\n\ntest \"Cluster is writable\" {\n    cluster_write_test 1\n}\n\ntest \"Instance 12 is now a master without slaves\" {\n    assert {[RI 12 role] eq {master}}\n}\n\n# The remaining instance is now without slaves. Some other slave\n# should migrate to it.\n\ntest \"Master #12 should get at least one migrated replica\" {\n    wait_for_condition 1000 50 {\n        [llength [lindex [R 12 role] 2]] >= 1\n    } else {\n        fail \"Master #12 has no replicas\"\n    }\n}\n"
  },
  {
    "path": "tests/cluster/tests/08-update-msg.tcl",
    "content": "# Test UPDATE messages sent by other nodes when the currently authorirative\n# master is unavaialble. The test is performed in the following steps:\n#\n# 1) Master goes down.\n# 2) Slave failover and becomes new master.\n# 3) New master is partitoned away.\n# 4) Old master returns.\n# 5) At this point we expect the old master to turn into a slave ASAP because\n#    of the UPDATE messages it will receive from the other nodes when its\n#    configuration will be found to be outdated.\n\nsource \"../tests/includes/init-tests.tcl\"\n\ntest \"Create a 5 nodes cluster\" {\n    create_cluster 5 5\n}\n\ntest \"Cluster is up\" {\n    assert_cluster_state ok\n}\n\ntest \"Cluster is writable\" {\n    cluster_write_test 0\n}\n\ntest \"Instance #5 is a slave\" {\n    assert {[RI 5 role] eq {slave}}\n}\n\ntest \"Instance #5 synced with the master\" {\n    wait_for_condition 1000 50 {\n        [RI 5 master_link_status] eq {up}\n    } else {\n        fail \"Instance #5 master link status is not up\"\n    }\n}\n\nset current_epoch [CI 1 cluster_current_epoch]\n\ntest \"Killing one master node\" {\n    kill_instance redis 0\n}\n\ntest \"Wait for failover\" {\n    wait_for_condition 1000 50 {\n        [CI 1 cluster_current_epoch] > $current_epoch\n    } else {\n        fail \"No failover detected\"\n    }\n}\n\ntest \"Cluster should eventually be up again\" {\n    assert_cluster_state ok\n}\n\ntest \"Cluster is writable\" {\n    cluster_write_test 1\n}\n\ntest \"Instance #5 is now a master\" {\n    assert {[RI 5 role] eq {master}}\n}\n\ntest \"Killing the new master #5\" {\n    kill_instance redis 5\n}\n\ntest \"Cluster should be down now\" {\n    assert_cluster_state fail\n}\n\ntest \"Restarting the old master node\" {\n    restart_instance redis 0\n}\n\ntest \"Instance #0 gets converted into a slave\" {\n    wait_for_condition 1000 50 {\n        [RI 0 role] eq {slave}\n    } else {\n        fail \"Old master was not converted into slave\"\n    }\n}\n\ntest \"Restarting the new master node\" {\n    restart_instance redis 5\n}\n\ntest \"Cluster is up again\" {\n    assert_cluster_state ok\n}\n"
  },
  {
    "path": "tests/cluster/tests/09-pubsub.tcl",
    "content": "# Test PUBLISH propagation across the cluster.\n\nsource \"../tests/includes/init-tests.tcl\"\n\ntest \"Create a 5 nodes cluster\" {\n    create_cluster 5 5\n}\n\nproc test_cluster_publish {instance instances} {\n    # Subscribe all the instances but the one we use to send.\n    for {set j 0} {$j < $instances} {incr j} {\n        if {$j != $instance} {\n            R $j deferred 1\n            R $j subscribe testchannel\n            R $j read; # Read the subscribe reply\n        }\n    }\n\n    set data [randomValue]\n    R $instance PUBLISH testchannel $data\n\n    # Read the message back from all the nodes.\n    for {set j 0} {$j < $instances} {incr j} {\n        if {$j != $instance} {\n            set msg [R $j read]\n            assert {$data eq [lindex $msg 2]}\n            R $j unsubscribe testchannel\n            R $j read; # Read the unsubscribe reply\n            R $j deferred 0\n        }\n    }\n}\n\ntest \"Test publishing to master\" {\n    test_cluster_publish 0 10\n}\n\ntest \"Test publishing to slave\" {\n    test_cluster_publish 5 10\n}\n"
  },
  {
    "path": "tests/cluster/tests/10-manual-failover.tcl",
    "content": "# Check the manual failover\n\nsource \"../tests/includes/init-tests.tcl\"\n\ntest \"Create a 5 nodes cluster\" {\n    create_cluster 5 5\n}\n\ntest \"Cluster is up\" {\n    assert_cluster_state ok\n}\n\ntest \"Cluster is writable\" {\n    cluster_write_test 0\n}\n\ntest \"Instance #5 is a slave\" {\n    assert {[RI 5 role] eq {slave}}\n}\n\ntest \"Instance #5 synced with the master\" {\n    wait_for_condition 1000 50 {\n        [RI 5 master_link_status] eq {up}\n    } else {\n        fail \"Instance #5 master link status is not up\"\n    }\n}\n\nset current_epoch [CI 1 cluster_current_epoch]\n\nset numkeys 50000\nset numops 10000\nset cluster [redis_cluster 127.0.0.1:[get_instance_attrib redis 0 port]]\ncatch {unset content}\narray set content {}\n\ntest \"Send CLUSTER FAILOVER to #5, during load\" {\n    for {set j 0} {$j < $numops} {incr j} {\n        # Write random data to random list.\n        set listid [randomInt $numkeys]\n        set key \"key:$listid\"\n        set ele [randomValue]\n        # We write both with Lua scripts and with plain commands.\n        # This way we are able to stress Lua -> Redis command invocation\n        # as well, that has tests to prevent Lua to write into wrong\n        # hash slots.\n        if {$listid % 2} {\n            $cluster rpush $key $ele\n        } else {\n           $cluster eval {redis.call(\"rpush\",KEYS[1],ARGV[1])} 1 $key $ele\n        }\n        lappend content($key) $ele\n\n        if {($j % 1000) == 0} {\n            puts -nonewline W; flush stdout\n        }\n\n        if {$j == $numops/2} {R 5 cluster failover}\n    }\n}\n\ntest \"Wait for failover\" {\n    wait_for_condition 1000 50 {\n        [CI 1 cluster_current_epoch] > $current_epoch\n    } else {\n        fail \"No failover detected\"\n    }\n}\n\ntest \"Cluster should eventually be up again\" {\n    assert_cluster_state ok\n}\n\ntest \"Cluster is writable\" {\n    cluster_write_test 1\n}\n\ntest \"Instance #5 is now a master\" {\n    assert {[RI 5 role] eq {master}}\n}\n\ntest \"Verify $numkeys keys for consistency with logical content\" {\n    # Check that the Redis Cluster content matches our logical content.\n    foreach {key value} [array get content] {\n        assert {[$cluster lrange $key 0 -1] eq $value}\n    }\n}\n\ntest \"Instance #0 gets converted into a slave\" {\n    wait_for_condition 1000 50 {\n        [RI 0 role] eq {slave}\n    } else {\n        fail \"Old master was not converted into slave\"\n    }\n}\n\n## Check that manual failover does not happen if we can't talk with the master.\n\nsource \"../tests/includes/init-tests.tcl\"\n\ntest \"Create a 5 nodes cluster\" {\n    create_cluster 5 5\n}\n\ntest \"Cluster is up\" {\n    assert_cluster_state ok\n}\n\ntest \"Cluster is writable\" {\n    cluster_write_test 0\n}\n\ntest \"Instance #5 is a slave\" {\n    assert {[RI 5 role] eq {slave}}\n}\n\ntest \"Instance #5 synced with the master\" {\n    wait_for_condition 1000 50 {\n        [RI 5 master_link_status] eq {up}\n    } else {\n        fail \"Instance #5 master link status is not up\"\n    }\n}\n\ntest \"Make instance #0 unreachable without killing it\" {\n    R 0 deferred 1\n    R 0 DEBUG SLEEP 10\n}\n\ntest \"Send CLUSTER FAILOVER to instance #5\" {\n    R 5 cluster failover\n}\n\ntest \"Instance #5 is still a slave after some time (no failover)\" {\n    after 5000\n    assert {[RI 5 role] eq {master}}\n}\n\ntest \"Wait for instance #0 to return back alive\" {\n    R 0 deferred 0\n    assert {[R 0 read] eq {OK}}\n}\n\n## Check with \"force\" failover happens anyway.\n\nsource \"../tests/includes/init-tests.tcl\"\n\ntest \"Create a 5 nodes cluster\" {\n    create_cluster 5 5\n}\n\ntest \"Cluster is up\" {\n    assert_cluster_state ok\n}\n\ntest \"Cluster is writable\" {\n    cluster_write_test 0\n}\n\ntest \"Instance #5 is a slave\" {\n    assert {[RI 5 role] eq {slave}}\n}\n\ntest \"Instance #5 synced with the master\" {\n    wait_for_condition 1000 50 {\n        [RI 5 master_link_status] eq {up}\n    } else {\n        fail \"Instance #5 master link status is not up\"\n    }\n}\n\ntest \"Make instance #0 unreachable without killing it\" {\n    R 0 deferred 1\n    R 0 DEBUG SLEEP 10\n}\n\ntest \"Send CLUSTER FAILOVER to instance #5\" {\n    R 5 cluster failover force\n}\n\ntest \"Instance #5 is a master after some time\" {\n    wait_for_condition 1000 50 {\n        [RI 5 role] eq {master}\n    } else {\n        fail \"Instance #5 is not a master after some time regardless of FORCE\"\n    }\n}\n\ntest \"Wait for instance #0 to return back alive\" {\n    R 0 deferred 0\n    assert {[R 0 read] eq {OK}}\n}\n"
  },
  {
    "path": "tests/cluster/tests/11-manual-takeover.tcl",
    "content": "# Manual takeover test\n\nsource \"../tests/includes/init-tests.tcl\"\n\ntest \"Create a 5 nodes cluster\" {\n    create_cluster 5 5\n}\n\ntest \"Cluster is up\" {\n    assert_cluster_state ok\n}\n\ntest \"Cluster is writable\" {\n    cluster_write_test 0\n}\n\ntest \"Killing majority of master nodes\" {\n    kill_instance redis 0\n    kill_instance redis 1\n    kill_instance redis 2\n}\n\ntest \"Cluster should eventually be down\" {\n    assert_cluster_state fail\n}\n\ntest \"Use takeover to bring slaves back\" {\n    R 5 cluster failover takeover\n    R 6 cluster failover takeover\n    R 7 cluster failover takeover\n}\n\ntest \"Cluster should eventually be up again\" {\n    assert_cluster_state ok\n}\n\ntest \"Cluster is writable\" {\n    cluster_write_test 4\n}\n\ntest \"Instance #5, #6, #7 are now masters\" {\n    assert {[RI 5 role] eq {master}}\n    assert {[RI 6 role] eq {master}}\n    assert {[RI 7 role] eq {master}}\n}\n\ntest \"Restarting the previously killed master nodes\" {\n    restart_instance redis 0\n    restart_instance redis 1\n    restart_instance redis 2\n}\n\ntest \"Instance #0, #1, #2 gets converted into a slaves\" {\n    wait_for_condition 1000 50 {\n        [RI 0 role] eq {slave} && [RI 1 role] eq {slave} && [RI 2 role] eq {slave}\n    } else {\n        fail \"Old masters not converted into slaves\"\n    }\n}\n"
  },
  {
    "path": "tests/cluster/tests/12-replica-migration-2.tcl",
    "content": "# Replica migration test #2.\n#\n# Check that the status of master that can be targeted by replica migration\n# is acquired again, after being getting slots again, in a cluster where the\n# other masters have slaves.\n\nsource \"../tests/includes/init-tests.tcl\"\nsource \"../../../tests/support/cli.tcl\"\n\n# Create a cluster with 5 master and 15 slaves, to make sure there are no\n# empty masters and make rebalancing simpler to handle during the test.\ntest \"Create a 5 nodes cluster\" {\n    create_cluster 5 15\n}\n\ntest \"Cluster is up\" {\n    assert_cluster_state ok\n}\n\ntest \"Each master should have at least two replicas attached\" {\n    foreach_redis_id id {\n        if {$id < 5} {\n            wait_for_condition 1000 50 {\n                [llength [lindex [R 0 role] 2]] >= 2\n            } else {\n                fail \"Master #$id does not have 2 slaves as expected\"\n            }\n        }\n    }\n}\n\ntest \"Set allow-replica-migration yes\" {\n    foreach_redis_id id {\n        R $id CONFIG SET cluster-allow-replica-migration yes\n    }\n}\n\nset master0_id [dict get [get_myself 0] id]\ntest \"Resharding all the master #0 slots away from it\" {\n    set output [exec \\\n        ../../../src/keydb-cli --cluster rebalance \\\n        127.0.0.1:[get_instance_attrib redis 0 port] \\\n        {*}[rediscli_tls_config \"../../../tests\"] \\\n        --cluster-weight ${master0_id}=0 >@ stdout ]\n\n}\n\ntest \"Master #0 should lose its replicas\" {\n    wait_for_condition 1000 50 {\n        [llength [lindex [R 0 role] 2]] == 0\n    } else {\n        fail \"Master #0 still has replicas\"\n    }\n}\n\ntest \"Resharding back some slot to master #0\" {\n    # Wait for the cluster config to propagate before attempting a\n    # new resharding.\n    after 10000\n    set output [exec \\\n        ../../../src/keydb-cli --cluster rebalance \\\n        127.0.0.1:[get_instance_attrib redis 0 port] \\\n        {*}[rediscli_tls_config \"../../../tests\"] \\\n        --cluster-weight ${master0_id}=.01 \\\n        --cluster-use-empty-masters  >@ stdout]\n}\n\ntest \"Master #0 should re-acquire one or more replicas\" {\n    wait_for_condition 1000 50 {\n        [llength [lindex [R 0 role] 2]] >= 1\n    } else {\n        fail \"Master #0 has no has replicas\"\n    }\n}\n"
  },
  {
    "path": "tests/cluster/tests/12.1-replica-migration-3.tcl",
    "content": "# Replica migration test #2.\n#\n# Check that if 'cluster-allow-replica-migration' is set to 'no', slaves do not\n# migrate when master becomes empty.\n\nsource \"../tests/includes/init-tests.tcl\"\n\n# Create a cluster with 5 master and 15 slaves, to make sure there are no\n# empty masters and make rebalancing simpler to handle during the test.\ntest \"Create a 5 nodes cluster\" {\n    create_cluster 5 15\n}\n\ntest \"Cluster is up\" {\n    assert_cluster_state ok\n}\n\ntest \"Each master should have at least two replicas attached\" {\n    foreach_redis_id id {\n        if {$id < 5} {\n            wait_for_condition 1000 50 {\n                [llength [lindex [R 0 role] 2]] >= 2\n            } else {\n                fail \"Master #$id does not have 2 slaves as expected\"\n            }\n        }\n    }\n}\n\ntest \"Set allow-replica-migration no\" {\n    foreach_redis_id id {\n        R $id CONFIG SET cluster-allow-replica-migration no\n    }\n}\n\nset master0_id [dict get [get_myself 0] id]\ntest \"Resharding all the master #0 slots away from it\" {\n    set output [exec \\\n        ../../../src/keydb-cli --cluster rebalance \\\n        127.0.0.1:[get_instance_attrib redis 0 port] \\\n        {*}[rediscli_tls_config \"../../../tests\"] \\\n        --cluster-weight ${master0_id}=0 >@ stdout ]\n}\n\ntest \"Wait cluster to be stable\" {\n    wait_for_condition 1000 50 {\n        [catch {exec ../../../src/keydb-cli --cluster \\\n            check 127.0.0.1:[get_instance_attrib redis 0 port] \\\n            {*}[rediscli_tls_config \"../../../tests\"] \\\n            }] == 0\n    } else {\n        fail \"Cluster doesn't stabilize\"\n    }\n}\n\ntest \"Master #0 stil should have its replicas\" {\n    assert { [llength [lindex [R 0 role] 2]] >= 2 }\n}\n\ntest \"Each master should have at least two replicas attached\" {\n    foreach_redis_id id {\n        if {$id < 5} {\n            wait_for_condition 1000 50 {\n                [llength [lindex [R 0 role] 2]] >= 2\n            } else {\n                fail \"Master #$id does not have 2 slaves as expected\"\n            }\n        }\n    }\n}\n\n"
  },
  {
    "path": "tests/cluster/tests/13-no-failover-option.tcl",
    "content": "# Check that the no-failover option works\n\nsource \"../tests/includes/init-tests.tcl\"\n\ntest \"Create a 5 nodes cluster\" {\n    create_cluster 5 5\n}\n\ntest \"Cluster is up\" {\n    assert_cluster_state ok\n}\n\ntest \"Cluster is writable\" {\n    cluster_write_test 0\n}\n\ntest \"Instance #5 is a slave\" {\n    assert {[RI 5 role] eq {slave}}\n\n    # Configure it to never failover the master\n    R 5 CONFIG SET cluster-slave-no-failover yes\n}\n\ntest \"Instance #5 synced with the master\" {\n    wait_for_condition 1000 50 {\n        [RI 5 master_link_status] eq {up}\n    } else {\n        fail \"Instance #5 master link status is not up\"\n    }\n}\n\ntest \"The nofailover flag is propagated\" {\n    set slave5_id [dict get [get_myself 5] id]\n\n    foreach_redis_id id {\n        wait_for_condition 1000 50 {\n            [has_flag [get_node_by_id $id $slave5_id] nofailover]\n        } else {\n            fail \"Instance $id can't see the nofailover flag of slave\"\n        }\n    }\n}\n\nset current_epoch [CI 1 cluster_current_epoch]\n\ntest \"Killing one master node\" {\n    kill_instance redis 0\n}\n\ntest \"Cluster should be still down after some time\" {\n    after 10000\n    assert_cluster_state fail\n}\n\ntest \"Instance #5 is still a slave\" {\n    assert {[RI 5 role] eq {slave}}\n}\n\ntest \"Restarting the previously killed master node\" {\n    restart_instance redis 0\n}\n"
  },
  {
    "path": "tests/cluster/tests/14-consistency-check.tcl",
    "content": "source \"../tests/includes/init-tests.tcl\"\nsource \"../../../tests/support/cli.tcl\"\n\ntest \"Create a 5 nodes cluster\" {\n    create_cluster 5 5\n}\n\ntest \"Cluster should start ok\" {\n    assert_cluster_state ok\n}\n\ntest \"Cluster is writable\" {\n    cluster_write_test 0\n}\n\nproc find_non_empty_master {} {\n    set master_id_no {}\n    foreach_redis_id id {\n        if {[RI $id role] eq {master} && [R $id dbsize] > 0} {\n            set master_id_no $id\n        }\n    }\n    return $master_id_no\n}\n\nproc get_one_of_my_replica {id} {\n    set replica_port [lindex [lindex [lindex [R $id role] 2] 0] 1]\n    set replica_id_num [get_instance_id_by_port redis $replica_port]\n    return $replica_id_num\n}\n\nproc cluster_write_keys_with_expire {id ttl} {\n    set prefix [randstring 20 20 alpha]\n    set port [get_instance_attrib redis $id port]\n    set cluster [redis_cluster 127.0.0.1:$port]\n    for {set j 100} {$j < 200} {incr j} {\n        $cluster setex key_expire.$j $ttl $prefix.$j\n    }\n    $cluster close\n}\n\n# make sure that replica who restarts from persistence will load keys\n# that have already expired, critical for correct execution of commands\n# that arrive from the master\nproc test_slave_load_expired_keys {aof} {\n    test \"Slave expired keys is loaded when restarted: appendonly=$aof\" {\n        set master_id [find_non_empty_master]\n        set replica_id [get_one_of_my_replica $master_id]\n\n        set master_dbsize_0 [R $master_id dbsize]\n        set replica_dbsize_0 [R $replica_id dbsize]\n        assert_equal $master_dbsize_0 $replica_dbsize_0\n\n        # config the replica persistency and rewrite the config file to survive restart\n        # note that this needs to be done before populating the volatile keys since\n        # that triggers and AOFRW, and we rather the AOF file to have SETEX commands\n        # rather than an RDB with volatile keys\n        R $replica_id config set appendonly $aof\n        R $replica_id config rewrite\n\n        # fill with 100 keys with 3 second TTL\n        set data_ttl 3\n        cluster_write_keys_with_expire $master_id $data_ttl\n\n        # wait for replica to be in sync with master\n        wait_for_condition 500 10 {\n            [R $replica_id dbsize] eq [R $master_id dbsize]\n        } else {\n            fail \"replica didn't sync\"\n        }\n        \n        set replica_dbsize_1 [R $replica_id dbsize]\n        assert {$replica_dbsize_1 > $replica_dbsize_0}\n\n        # make replica create persistence file\n        if {$aof == \"yes\"} {\n            # we need to wait for the initial AOFRW to be done, otherwise\n            # kill_instance (which now uses SIGTERM will fail (\"Writing initial AOF, can't exit\")\n            wait_for_condition 100 10 {\n                [RI $replica_id aof_rewrite_in_progress] eq 0\n            } else {\n                fail \"keys didn't expire\"\n            }\n        } else {\n            R $replica_id save\n        }\n\n        # kill the replica (would stay down until re-started)\n        kill_instance redis $replica_id\n\n        # Make sure the master doesn't do active expire (sending DELs to the replica)\n        R $master_id DEBUG SET-ACTIVE-EXPIRE 0\n\n        # wait for all the keys to get logically expired\n        after [expr $data_ttl*1000]\n\n        # start the replica again (loading an RDB or AOF file)\n        restart_instance redis $replica_id\n\n        # make sure the keys are still there\n        set replica_dbsize_3 [R $replica_id dbsize]\n        assert {$replica_dbsize_3 > $replica_dbsize_0}\n        \n        # restore settings\n        R $master_id DEBUG SET-ACTIVE-EXPIRE 1\n\n        # wait for the master to expire all keys and replica to get the DELs\n        wait_for_condition 500 10 {\n            [R $replica_id dbsize] eq $master_dbsize_0\n        } else {\n            fail \"keys didn't expire\"\n        }\n    }\n}\n\ntest_slave_load_expired_keys no\ntest_slave_load_expired_keys yes\n"
  },
  {
    "path": "tests/cluster/tests/15-cluster-slots.tcl",
    "content": "source \"../tests/includes/init-tests.tcl\"\n\nproc cluster_allocate_mixedSlots {n} {\n    set slot 16383\n    while {$slot >= 0} {\n        set node [expr {$slot % $n}]\n        lappend slots_$node $slot\n        incr slot -1\n    }\n    for {set j 0} {$j < $n} {incr j} {\n        R $j cluster addslots {*}[set slots_${j}]\n    }\n}\n\nproc create_cluster_with_mixedSlot {masters slaves} {\n    cluster_allocate_mixedSlots $masters\n    if {$slaves} {\n        cluster_allocate_slaves $masters $slaves\n    }\n    assert_cluster_state ok\n}\n\ntest \"Create a 5 nodes cluster\" {\n    create_cluster_with_mixedSlot 5 15\n}\n\ntest \"Cluster is up\" {\n    assert_cluster_state ok\n}\n\ntest \"Cluster is writable\" {\n    cluster_write_test 0\n}\n\ntest \"Instance #5 is a slave\" {\n    assert {[RI 5 role] eq {slave}}\n}\n\ntest \"client do not break when cluster slot\" {\n    R 0 config set client-output-buffer-limit \"normal 33554432 16777216 60\"\n    if { [catch {R 0 cluster slots}] } {\n        fail \"output overflow when cluster slots\"\n    }\n}\n\ntest \"client can handle keys with hash tag\" {\n    set cluster [redis_cluster 127.0.0.1:[get_instance_attrib redis 0 port]]\n    $cluster set foo{tag} bar\n    $cluster close\n}\n\nif {$::tls} {\n    test {CLUSTER SLOTS from non-TLS client in TLS cluster} {\n        set slots_tls [R 0 cluster slots]\n        set host [get_instance_attrib redis 0 host]\n        set plaintext_port [get_instance_attrib redis 0 plaintext-port]\n        set client_plain [redis $host $plaintext_port 0 0]\n        set slots_plain [$client_plain cluster slots]\n        $client_plain close\n        # Compare the ports in the first row\n        assert_no_match [lindex $slots_tls 0 3 1] [lindex $slots_plain 0 3 1]\n    }\n}\n"
  },
  {
    "path": "tests/cluster/tests/16-transactions-on-replica.tcl",
    "content": "# Check basic transactions on a replica.\n\nsource \"../tests/includes/init-tests.tcl\"\n\ntest \"Create a primary with a replica\" {\n    create_cluster 1 1\n}\n\ntest \"Cluster should start ok\" {\n    assert_cluster_state ok\n}\n\nset primary [Rn 0]\nset replica [Rn 1]\n\ntest \"Cant read from replica without READONLY\" {\n    $primary SET a 1\n    wait_for_ofs_sync $primary $replica\n    catch {$replica GET a} err\n    assert {[string range $err 0 4] eq {MOVED}}\n}\n\ntest \"Can read from replica after READONLY\" {\n    $replica READONLY\n    assert {[$replica GET a] eq {1}}\n}\n\ntest \"Can preform HSET primary and HGET from replica\" {\n    $primary HSET h a 1\n    $primary HSET h b 2\n    $primary HSET h c 3\n    wait_for_ofs_sync $primary $replica\n    assert {[$replica HGET h a] eq {1}}\n    assert {[$replica HGET h b] eq {2}}\n    assert {[$replica HGET h c] eq {3}}\n}\n\ntest \"Can MULTI-EXEC transaction of HGET operations from replica\" {\n    $replica MULTI\n    assert {[$replica HGET h a] eq {QUEUED}}\n    assert {[$replica HGET h b] eq {QUEUED}}\n    assert {[$replica HGET h c] eq {QUEUED}}\n    assert {[$replica EXEC] eq {1 2 3}}\n}\n\ntest \"MULTI-EXEC with write operations is MOVED\" {\n    $replica MULTI\n    catch {$replica HSET h b 4} err\n    assert {[string range $err 0 4] eq {MOVED}}\n    catch {$replica exec} err\n    assert {[string range $err 0 8] eq {EXECABORT}}\n}\n\ntest \"read-only blocking operations from replica\" {\n    set rd [redis_deferring_client redis 1]\n    $rd readonly\n    $rd read\n    $rd XREAD BLOCK 0 STREAMS k 0\n\n    wait_for_condition 1000 50 {\n        [RI 1 blocked_clients] eq {1}\n    } else {\n        fail \"client wasn't blocked\"\n    }\n\n    $primary XADD k * foo bar\n    set res [$rd read]\n    set res [lindex [lindex [lindex [lindex $res 0] 1] 0] 1]\n    assert {$res eq {foo bar}}\n    $rd close\n}\n"
  },
  {
    "path": "tests/cluster/tests/17-diskless-load-swapdb.tcl",
    "content": "# Check replica can restore database buckup correctly if fail to diskless load.\n\nsource \"../tests/includes/init-tests.tcl\"\n\ntest \"Create a primary with a replica\" {\n    create_cluster 1 1\n}\n\ntest \"Cluster should start ok\" {\n    assert_cluster_state ok\n}\n\ntest \"Cluster is writable\" {\n    cluster_write_test 0\n}\n\ntest \"Right to restore backups when fail to diskless load \" {\n    set master [Rn 0]\n    set replica [Rn 1]\n    set master_id 0\n    set replica_id 1\n\n    $replica READONLY\n    $replica config set repl-diskless-load swapdb\n    $replica config set appendonly no\n    $replica config set save \"\"\n    $replica config rewrite\n    $master config set repl-backlog-size 1024\n    $master config set repl-diskless-sync yes\n    $master config set repl-diskless-sync-delay 0\n    $master config set rdb-key-save-delay 10000\n    $master config set rdbcompression no\n    $master config set appendonly no\n    $master config set save \"\"\n\n    # Write a key that belongs to slot 0\n    set slot0_key \"06S\"\n    $master set $slot0_key 1\n    wait_for_ofs_sync $master $replica\n    assert_equal {1} [$replica get $slot0_key]\n    assert_equal $slot0_key [$replica CLUSTER GETKEYSINSLOT 0 1] \"THIS ONE\"\n\n    # Save an RDB and kill the replica\n    $replica save\n    kill_instance redis $replica_id\n\n    # Delete the key from master\n    $master del $slot0_key\n\n    # Replica must full sync with master when start because replication\n    # backlog size is very small, and dumping rdb will cost several seconds.\n    set num 10000\n    set value [string repeat A 1024]\n    set rd [redis_deferring_client redis $master_id]\n    for {set j 0} {$j < $num} {incr j} {\n        $rd set $j $value\n    }\n    for {set j 0} {$j < $num} {incr j} {\n        $rd read\n    }\n\n    # Start the replica again\n    restart_instance redis $replica_id\n    $replica READONLY\n\n    # Start full sync, wait till after db is flushed (backed up)\n    wait_for_condition 500 10 {\n        [s $replica_id loading] eq 1\n    } else {\n        fail \"Fail to full sync\"\n    }\n\n    # Kill master, abort full sync\n    kill_instance redis $master_id\n\n    # Start full sync, wait till the replica detects the disconnection\n    wait_for_condition 500 10 {\n        [s $replica_id loading] eq 0\n    } else {\n        fail \"Fail to full sync\"\n    }\n\n    # Replica keys and keys to slots map still both are right\n    assert_equal {1} [$replica get $slot0_key]\n    assert_equal $slot0_key [$replica CLUSTER GETKEYSINSLOT 0 1] \"POST RUN\"\n}\n"
  },
  {
    "path": "tests/cluster/tests/18-info.tcl",
    "content": "# Check cluster info stats\n\nsource \"../tests/includes/init-tests.tcl\"\n\ntest \"Create a primary with a replica\" {\n    create_cluster 2 0\n}\n\ntest \"Cluster should start ok\" {\n    assert_cluster_state ok\n}\n\nset primary1 [Rn 0]\nset primary2 [Rn 1]\n\nproc cmdstat {instace cmd} {\n    return [cmdrstat $cmd $instace]\n}\n\nproc errorstat {instace cmd} {\n    return [errorrstat $cmd $instace]\n}\n\ntest \"errorstats: rejected call due to MOVED Redirection\" {\n    $primary1 config resetstat\n    $primary2 config resetstat\n    assert_match {} [errorstat $primary1 MOVED]\n    assert_match {} [errorstat $primary2 MOVED]\n    # we know that one will have a MOVED reply and one will succeed\n    catch {$primary1 set key b} replyP1\n    catch {$primary2 set key b} replyP2\n    # sort servers so we know which one failed\n    if {$replyP1 eq {OK}} {\n        assert_match {MOVED*} $replyP2\n        set pok $primary1\n        set perr $primary2\n    } else {\n        assert_match {MOVED*} $replyP1\n        set pok $primary2\n        set perr $primary1\n    }\n    assert_match {} [errorstat $pok MOVED]\n    assert_match {*count=1*} [errorstat $perr MOVED]\n    assert_match {*calls=0,*,rejected_calls=1,failed_calls=0} [cmdstat $perr set]\n}\n"
  },
  {
    "path": "tests/cluster/tests/19-cluster-nodes-slots.tcl",
    "content": "# Optimize CLUSTER NODES command by generating all nodes slot topology firstly\n\nsource \"../tests/includes/init-tests.tcl\"\n\nproc cluster_allocate_with_continuous_slots {n} {\n    set slot 16383\n    set avg [expr ($slot+1) / $n]\n    while {$slot >= 0} {\n        set node [expr $slot/$avg >= $n ? $n-1 : $slot/$avg]\n        lappend slots_$node $slot\n        incr slot -1\n    }\n    for {set j 0} {$j < $n} {incr j} {\n        R $j cluster addslots {*}[set slots_${j}]\n    }\n}\n\nproc cluster_create_with_continuous_slots {masters slaves} {\n    cluster_allocate_with_continuous_slots $masters\n    if {$slaves} {\n        cluster_allocate_slaves $masters $slaves\n    }\n    assert_cluster_state ok\n}\n\ntest \"Create a 2 nodes cluster\" {\n    cluster_create_with_continuous_slots 2 2\n}\n\ntest \"Cluster should start ok\" {\n    assert_cluster_state ok\n}\n\nset master1 [Rn 0]\nset master2 [Rn 1]\n\ntest \"Continuous slots distribution\" {\n    assert_match \"* 0-8191*\" [$master1 CLUSTER NODES]\n    assert_match \"* 8192-16383*\" [$master2 CLUSTER NODES]\n    assert_match \"*0 8191*\" [$master1 CLUSTER SLOTS]\n    assert_match \"*8192 16383*\" [$master2 CLUSTER SLOTS]\n\n    $master1 CLUSTER DELSLOTS 4096\n    assert_match \"* 0-4095 4097-8191*\" [$master1 CLUSTER NODES]\n    assert_match \"*0 4095*4097 8191*\" [$master1 CLUSTER SLOTS]\n\n\n    $master2 CLUSTER DELSLOTS 12288\n    assert_match \"* 8192-12287 12289-16383*\" [$master2 CLUSTER NODES]\n    assert_match \"*8192 12287*12289 16383*\" [$master2 CLUSTER SLOTS]\n}\n\ntest \"Discontinuous slots distribution\" {\n    # Remove middle slots\n    $master1 CLUSTER DELSLOTS 4092 4094\n    assert_match \"* 0-4091 4093 4095 4097-8191*\" [$master1 CLUSTER NODES]\n    assert_match \"*0 4091*4093 4093*4095 4095*4097 8191*\" [$master1 CLUSTER SLOTS]\n    $master2 CLUSTER DELSLOTS 12284 12286\n    assert_match \"* 8192-12283 12285 12287 12289-16383*\" [$master2 CLUSTER NODES]\n    assert_match \"*8192 12283*12285 12285*12287 12287*12289 16383*\" [$master2 CLUSTER SLOTS]\n\n    # Remove head slots\n    $master1 CLUSTER DELSLOTS 0 2\n    assert_match \"* 1 3-4091 4093 4095 4097-8191*\" [$master1 CLUSTER NODES]\n    assert_match \"*1 1*3 4091*4093 4093*4095 4095*4097 8191*\" [$master1 CLUSTER SLOTS]\n\n    # Remove tail slots\n    $master2 CLUSTER DELSLOTS 16380 16382 16383\n    assert_match \"* 8192-12283 12285 12287 12289-16379 16381*\" [$master2 CLUSTER NODES]\n    assert_match \"*8192 12283*12285 12285*12287 12287*12289 16379*16381 16381*\" [$master2 CLUSTER SLOTS]\n}\n"
  },
  {
    "path": "tests/cluster/tests/20-half-migrated-slot.tcl",
    "content": "# Tests for fixing migrating slot at all stages:\n# 1. when migration is half inited on \"migrating\" node\n# 2. when migration is half inited on \"importing\" node\n# 3. migration inited, but not finished\n# 4. migration is half finished on \"migrating\" node\n# 5. migration is half finished on \"importing\" node\n\n# TODO: Test is currently disabled until it is stabilized (fixing the test\n# itself or real issues in Redis).\n\nif {false} {\nsource \"../tests/includes/init-tests.tcl\"\nsource \"../tests/includes/utils.tcl\"\n\ntest \"Create a 2 nodes cluster\" {\n    create_cluster 2 0\n    config_set_all_nodes cluster-allow-replica-migration no\n}\n\ntest \"Cluster is up\" {\n    assert_cluster_state ok\n}\n\nset cluster [redis_cluster 127.0.0.1:[get_instance_attrib redis 0 port]]\ncatch {unset nodefrom}\ncatch {unset nodeto}\n\nproc reset_cluster {} {\n    uplevel 1 {\n        $cluster refresh_nodes_map\n        array set nodefrom [$cluster masternode_for_slot 609]\n        array set nodeto [$cluster masternode_notfor_slot 609]\n    }\n}\n\nreset_cluster\n\n$cluster set aga xyz\n\ntest \"Half init migration in 'migrating' is fixable\" {\n    assert_equal {OK} [$nodefrom(link) cluster setslot 609 migrating $nodeto(id)]\n    fix_cluster $nodefrom(addr)\n    assert_equal \"xyz\" [$cluster get aga]\n}\n\ntest \"Half init migration in 'importing' is fixable\" {\n    assert_equal {OK} [$nodeto(link) cluster setslot 609 importing $nodefrom(id)]\n    fix_cluster $nodefrom(addr)\n    assert_equal \"xyz\" [$cluster get aga]\n}\n\ntest \"Init migration and move key\" {\n    assert_equal {OK} [$nodefrom(link) cluster setslot 609 migrating $nodeto(id)]\n    assert_equal {OK} [$nodeto(link) cluster setslot 609 importing $nodefrom(id)]\n    assert_equal {OK} [$nodefrom(link) migrate $nodeto(host) $nodeto(port) aga 0 10000]\n    wait_for_cluster_propagation\n    assert_equal \"xyz\" [$cluster get aga]\n    fix_cluster $nodefrom(addr)\n    assert_equal \"xyz\" [$cluster get aga]\n}\n\nreset_cluster\n\ntest \"Move key again\" {\n    wait_for_cluster_propagation\n    assert_equal {OK} [$nodefrom(link) cluster setslot 609 migrating $nodeto(id)]\n    assert_equal {OK} [$nodeto(link) cluster setslot 609 importing $nodefrom(id)]\n    assert_equal {OK} [$nodefrom(link) migrate $nodeto(host) $nodeto(port) aga 0 10000]\n    wait_for_cluster_propagation\n    assert_equal \"xyz\" [$cluster get aga]\n}\n\ntest \"Half-finish migration\" {\n    # half finish migration on 'migrating' node\n    assert_equal {OK} [$nodefrom(link) cluster setslot 609 node $nodeto(id)]\n    fix_cluster $nodefrom(addr)\n    assert_equal \"xyz\" [$cluster get aga]\n}\n\nreset_cluster\n\ntest \"Move key back\" {\n    # 'aga' key is in 609 slot\n    assert_equal {OK} [$nodefrom(link) cluster setslot 609 migrating $nodeto(id)]\n    assert_equal {OK} [$nodeto(link) cluster setslot 609 importing $nodefrom(id)]\n    assert_equal {OK} [$nodefrom(link) migrate $nodeto(host) $nodeto(port) aga 0 10000]\n    assert_equal \"xyz\" [$cluster get aga]\n}\n\ntest \"Half-finish importing\" {\n    # Now we half finish 'importing' node\n    assert_equal {OK} [$nodeto(link) cluster setslot 609 node $nodeto(id)]\n    fix_cluster $nodefrom(addr)\n    assert_equal \"xyz\" [$cluster get aga]\n}\n\nconfig_set_all_nodes cluster-allow-replica-migration yes\n}\n"
  },
  {
    "path": "tests/cluster/tests/21-many-slot-migration.tcl",
    "content": "# Tests for many simlutaneous migrations.\n\n# TODO: Test is currently disabled until it is stabilized (fixing the test\n# itself or real issues in Redis).\n\nif {false} {\n\nsource \"../tests/includes/init-tests.tcl\"\nsource \"../tests/includes/utils.tcl\"\n\n# TODO: This test currently runs without replicas, as failovers (which may\n# happen on lower-end CI platforms) are still not handled properly by the\n# cluster during slot migration (related to #6339).\n\ntest \"Create a 10 nodes cluster\" {\n    create_cluster 10 0\n    config_set_all_nodes cluster-allow-replica-migration no\n}\n\ntest \"Cluster is up\" {\n    assert_cluster_state ok\n}\n\nset cluster [redis_cluster 127.0.0.1:[get_instance_attrib redis 0 port]]\ncatch {unset nodefrom}\ncatch {unset nodeto}\n\n$cluster refresh_nodes_map\n\ntest \"Set many keys\" {\n    for {set i 0} {$i < 40000} {incr i} {\n        $cluster set key:$i val:$i\n    }\n}\n\ntest \"Keys are accessible\" {\n    for {set i 0} {$i < 40000} {incr i} {\n        assert { [$cluster get key:$i] eq \"val:$i\" }\n    }\n}\n\ntest \"Init migration of many slots\" {\n    for {set slot 0} {$slot < 1000} {incr slot} {\n        array set nodefrom [$cluster masternode_for_slot $slot]\n        array set nodeto [$cluster masternode_notfor_slot $slot]\n\n        $nodefrom(link) cluster setslot $slot migrating $nodeto(id)\n        $nodeto(link) cluster setslot $slot importing $nodefrom(id)\n    }\n}\n\ntest \"Fix cluster\" {\n    wait_for_cluster_propagation\n    fix_cluster $nodefrom(addr)\n}\n\ntest \"Keys are accessible\" {\n    for {set i 0} {$i < 40000} {incr i} {\n        assert { [$cluster get key:$i] eq \"val:$i\" }\n    }\n}\n\nconfig_set_all_nodes cluster-allow-replica-migration yes\n}\n"
  },
  {
    "path": "tests/cluster/tests/helpers/onlydots.tcl",
    "content": "# Read the standard input and only shows dots in the output, filtering out\n# all the other characters. Designed to avoid bufferization so that when\n# we get the output of keydb-trib and want to show just the dots, we'll see\n# the dots as soon as keydb-trib will output them.\n\nfconfigure stdin -buffering none\n\nwhile 1 {\n    set c [read stdin 1]\n    if {$c eq {}} {\n        exit 0; # EOF\n    } elseif {$c eq {.}} {\n        puts -nonewline .\n        flush stdout\n    }\n}\n"
  },
  {
    "path": "tests/cluster/tests/includes/init-tests.tcl",
    "content": "# Initialization tests -- most units will start including this.\n\ntest \"(init) Restart killed instances\" {\n    foreach type {redis} {\n        foreach_${type}_id id {\n            if {[get_instance_attrib $type $id pid] == -1} {\n                puts -nonewline \"$type/$id \"\n                flush stdout\n                restart_instance $type $id\n            }\n        }\n    }\n}\n\ntest \"Cluster nodes are reachable\" {\n    foreach_redis_id id {\n        # Every node should be reachable.\n        wait_for_condition 1000 50 {\n            ([catch {R $id ping} ping_reply] == 0) &&\n            ($ping_reply eq {PONG})\n        } else {\n            catch {R $id ping} err\n            fail \"Node #$id keeps replying '$err' to PING.\"\n        }\n    }\n}\n\ntest \"Cluster nodes hard reset\" {\n    foreach_redis_id id {\n        if {$::valgrind} {\n            set node_timeout 10000\n        } else {\n            set node_timeout 3000\n        }\n        catch {R $id flushall} ; # May fail for readonly slaves.\n        R $id MULTI\n        R $id cluster reset hard\n        R $id cluster set-config-epoch [expr {$id+1}]\n        R $id EXEC\n        R $id config set cluster-node-timeout $node_timeout\n        R $id config set cluster-slave-validity-factor 10\n        R $id config rewrite\n    }\n}\n\ntest \"Cluster Join and auto-discovery test\" {\n    # Join node 0 with 1, 1 with 2, ... and so forth.\n    # If auto-discovery works all nodes will know every other node\n    # eventually.\n    set ids {}\n    foreach_redis_id id {lappend ids $id}\n    for {set j 0} {$j < [expr [llength $ids]-1]} {incr j} {\n        set a [lindex $ids $j]\n        set b [lindex $ids [expr $j+1]]\n        set b_port [get_instance_attrib redis $b port]\n        R $a cluster meet 127.0.0.1 $b_port\n    }\n\n    foreach_redis_id id {\n        wait_for_condition 1000 50 {\n            [llength [get_cluster_nodes $id]] == [llength $ids]\n        } else {\n            fail \"Cluster failed to join into a full mesh.\"\n        }\n    }\n}\n\ntest \"Before slots allocation, all nodes report cluster failure\" {\n    assert_cluster_state fail\n}\n"
  },
  {
    "path": "tests/cluster/tests/includes/utils.tcl",
    "content": "source \"../../../tests/support/cli.tcl\"\n\nproc config_set_all_nodes {keyword value} {\n    foreach_redis_id id {\n        R $id config set $keyword $value\n    }\n}\n\nproc fix_cluster {addr} {\n    set code [catch {\n        exec ../../../src/keydb-cli {*}[rediscli_tls_config \"../../../tests\"] --cluster fix $addr << yes\n    } result]\n    if {$code != 0} {\n        puts \"keydb-cli --cluster fix returns non-zero exit code, output below:\\n$result\"\n    }\n    # Note: keydb-cli --cluster fix may return a non-zero exit code if nodes don't agree,\n    # but we can ignore that and rely on the check below.\n    assert_cluster_state ok\n    wait_for_condition 100 100 {\n        [catch {exec ../../../src/keydb-cli {*}[rediscli_tls_config \"../../../tests\"] --cluster check $addr} result] == 0\n    } else {\n        puts \"keydb-cli --cluster check returns non-zero exit code, output below:\\n$result\"\n        fail \"Cluster could not settle with configuration\"\n    }\n}\n"
  },
  {
    "path": "tests/cluster/tmp/.gitignore",
    "content": "redis_*\nsentinel_*\n"
  },
  {
    "path": "tests/helpers/bg_block_op.tcl",
    "content": "source tests/support/keydb.tcl\nsource tests/support/util.tcl\n\nset ::tlsdir \"tests/tls\"\n\n# This function sometimes writes sometimes blocking-reads from lists/sorted\n# sets. There are multiple processes like this executing at the same time\n# so that we have some chance to trap some corner condition if there is\n# a regression. For this to happen it is important that we narrow the key\n# space to just a few elements, and balance the operations so that it is\n# unlikely that lists and zsets just get more data without ever causing\n# blocking.\nproc bg_block_op {host port db ops tls} {\n    set r [redis $host $port 0 $tls]\n    $r client setname LOAD_HANDLER\n    $r select $db\n\n    for {set j 0} {$j < $ops} {incr j} {\n\n        # List side\n        set k list_[randomInt 10]\n        set k2 list_[randomInt 10]\n        set v [randomValue]\n\n        randpath {\n            randpath {\n                $r rpush $k $v\n            } {\n                $r lpush $k $v\n            }\n        } {\n            $r blpop $k 2\n        } {\n            $r blpop $k $k2 2\n        }\n\n        # Zset side\n        set k zset_[randomInt 10]\n        set k2 zset_[randomInt 10]\n        set v1 [randomValue]\n        set v2 [randomValue]\n\n        randpath {\n            $r zadd $k [randomInt 10000] $v\n        } {\n            $r zadd $k [randomInt 10000] $v [randomInt 10000] $v2\n        } {\n            $r bzpopmin $k 2\n        } {\n            $r bzpopmax $k 2\n        }\n    }\n}\n\nbg_block_op [lindex $argv 0] [lindex $argv 1] [lindex $argv 2] [lindex $argv 3] [lindex $argv 4]\n"
  },
  {
    "path": "tests/helpers/bg_complex_data.tcl",
    "content": "source tests/support/keydb.tcl\nsource tests/support/util.tcl\n\nset ::tlsdir \"tests/tls\"\n\nproc bg_complex_data {host port db ops tls} {\n    set r [redis $host $port 0 $tls]\n    $r client setname LOAD_HANDLER\n    $r select $db\n    createComplexDataset $r $ops\n}\n\nbg_complex_data [lindex $argv 0] [lindex $argv 1] [lindex $argv 2] [lindex $argv 3] [lindex $argv 4]\n"
  },
  {
    "path": "tests/helpers/fake_redis_node.tcl",
    "content": "# A fake Redis node for replaying predefined/expected traffic with a client.\n#\n# Usage: tclsh fake_redis_node.tcl PORT COMMAND REPLY [ COMMAND REPLY [ ... ] ]\n#\n# Commands are given as space-separated strings, e.g. \"GET foo\", and replies as\n# RESP-encoded replies minus the trailing \\r\\n, e.g. \"+OK\".\n\nset port [lindex $argv 0];\nset expected_traffic [lrange $argv 1 end];\n\n# Reads and parses a command from a socket and returns it as a space-separated\n# string, e.g. \"set foo bar\".\nproc read_command {sock} {\n    set char [read $sock 1]\n    switch $char {\n        * {\n            set numargs [gets $sock]\n            set result {}\n            for {set i 0} {$i<$numargs} {incr i} {\n                read $sock 1;       # dollar sign\n                set len [gets $sock]\n                set str [read $sock $len]\n                gets $sock;         # trailing \\r\\n\n                lappend result $str\n            }\n            return $result\n        }\n        {} {\n            # EOF\n            return {}\n        }\n        default {\n            # Non-RESP command\n            set rest [gets $sock]\n            return \"$char$rest\"\n        }\n    }\n}\n\nproc accept {sock host port} {\n    global expected_traffic\n    foreach {expect_cmd reply} $expected_traffic {\n        if {[eof $sock]} {break}\n        set cmd [read_command $sock]\n        if {[string equal -nocase $cmd $expect_cmd]} {\n            puts $sock $reply\n            flush $sock\n        } else {\n            puts $sock \"-ERR unexpected command $cmd\"\n            break\n        }\n    }\n    close $sock\n}\n\nsocket -server accept $port\nafter 5000 set done timeout\nvwait done\n"
  },
  {
    "path": "tests/helpers/gen_climbing_load.tcl",
    "content": "source tests/support/keydb.tcl\n\n\nset ::tlsdir \"tests/tls\"\n\nproc gen_climbing_load {host port db ops tls} {\n    set start_time [clock seconds]\n    set r [redis $host $port 1 $tls]\n    $r client setname LOAD_HANDLER\n    $r select $db\n    set x 0\n    while {$x < $ops} {\n        incr x\n        $r set [expr $x] [expr rand()]\n    }\n}\n\ngen_climbing_load [lindex $argv 0] [lindex $argv 1] [lindex $argv 2] [lindex $argv 3] [lindex $argv 4]\n"
  },
  {
    "path": "tests/helpers/gen_write_load.tcl",
    "content": "source tests/support/keydb.tcl\n\nset ::tlsdir \"tests/tls\"\n\nproc gen_write_load {host port seconds tls} {\n    set start_time [clock seconds]\n    set r [redis $host $port 1 $tls]\n    $r client setname LOAD_HANDLER\n    $r select 9\n    while 1 {\n        $r set [expr rand()] [expr rand()]\n        if {[clock seconds]-$start_time > $seconds} {\n            exit 0\n        }\n    }\n}\n\ngen_write_load [lindex $argv 0] [lindex $argv 1] [lindex $argv 2] [lindex $argv 3]\n"
  },
  {
    "path": "tests/instances.tcl",
    "content": "# Multi-instance test framework.\n# This is used in order to test Sentinel and Redis Cluster, and provides\n# basic capabilities for spawning and handling N parallel Redis / Sentinel\n# instances.\n#\n# Copyright (C) 2014 Salvatore Sanfilippo antirez@gmail.com\n# This software is released under the BSD License. See the COPYING file for\n# more information.\n\npackage require Tcl 8.5\n\nset tcl_precision 17\nsource ../support/keydb.tcl\nsource ../support/util.tcl\nsource ../support/server.tcl\nsource ../support/test.tcl\n\nset ::verbose 0\nset ::valgrind 0\nset ::tls 0\nset ::pause_on_error 0\nset ::dont_clean 0\nset ::simulate_error 0\nset ::flash 0\nset ::failed 0\nset ::sentinel_instances {}\nset ::redis_instances {}\nset ::global_config {}\nset ::sentinel_base_port 20000\nset ::redis_base_port 30000\nset ::redis_port_count 1024\nset ::host \"127.0.0.1\"\nset ::leaked_fds_file [file normalize \"tmp/leaked_fds.txt\"]\nset ::pids {} ; # We kill everything at exit\nset ::dirs {} ; # We remove all the temp dirs at exit\nset ::run_matching {} ; # If non empty, only tests matching pattern are run.\n\nif {[catch {cd tmp}]} {\n    puts \"tmp directory not found.\"\n    puts \"Please run this test from the KeyDB source root.\"\n    exit 1\n}\n\n# Execute the specified instance of the server specified by 'type', using\n# the provided configuration file. Returns the PID of the process.\nproc exec_instance {type dirname cfgfile} {\n    if {$type eq \"redis\"} {\n        set prgname keydb-server\n    } elseif {$type eq \"sentinel\"} {\n        set prgname keydb-sentinel\n    } else {\n        error \"Unknown instance type.\"\n    }\n\n    set errfile [file join $dirname err.txt]\n    if {$::valgrind} {\n        set pid [exec valgrind --track-origins=yes --suppressions=../../../src/valgrind.sup --show-reachable=no --show-possibly-lost=no --leak-check=full ../../../src/${prgname} $cfgfile 2>> $errfile &]\n    } else {\n        set pid [exec ../../../src/${prgname} $cfgfile 2>> $errfile &]\n    }\n    return $pid\n}\n\n# Spawn a redis or sentinel instance, depending on 'type'.\nproc spawn_instance {type base_port count {conf {}} {base_conf_file \"\"}} {\n    for {set j 0} {$j < $count} {incr j} {\n        set port [find_available_port $base_port $::redis_port_count]\n        # plaintext port (only used for TLS cluster)\n        set pport 0\n        # Create a directory for this instance.\n        set dirname \"${type}_${j}\"\n        lappend ::dirs $dirname\n        catch {exec rm -rf $dirname}\n        file mkdir $dirname\n\n        # Write the instance config file.\n        set cfgfile [file join $dirname $type.conf]\n        if {$base_conf_file ne \"\"} {\n            file copy -- $base_conf_file $cfgfile\n            set cfg [open $cfgfile a+]\n        } else {\n            set cfg [open $cfgfile w]\n        }\n\n        if {$::flash} {\n            puts $cfg \"storage-provider flash ./flash_$base_port\"\n        }\n\n        if {$::tls} {\n            puts $cfg \"tls-port $port\"\n            puts $cfg \"tls-replication yes\"\n            puts $cfg \"tls-cluster yes\"\n            # plaintext port, only used by plaintext clients in a TLS cluster\n            set pport [find_available_port $base_port $::redis_port_count]\n            puts $cfg \"port $pport\"\n            puts $cfg [format \"tls-cert-file %s/../../tls/server.crt\" [pwd]]\n            puts $cfg [format \"tls-key-file %s/../../tls/server.key\" [pwd]]\n            puts $cfg [format \"tls-client-cert-file %s/../../tls/client.crt\" [pwd]]\n            puts $cfg [format \"tls-client-key-file %s/../../tls/client.key\" [pwd]]\n            puts $cfg [format \"tls-dh-params-file %s/../../tls/keydb.dh\" [pwd]]\n            puts $cfg [format \"tls-ca-cert-file %s/../../tls/ca.crt\" [pwd]]\n            puts $cfg \"loglevel debug\"\n        } else {\n            puts $cfg \"port $port\"\n        }\n        puts $cfg \"dir ./$dirname\"\n        puts $cfg \"logfile log.txt\"\n        # Add additional config files\n        foreach directive $conf {\n            puts $cfg $directive\n        }\n        dict for {name val} $::global_config {\n            puts $cfg \"$name $val\"\n        }\n        close $cfg\n\n        # Finally exec it and remember the pid for later cleanup.\n        set retry 100\n        while {$retry} {\n            set pid [exec_instance $type $dirname $cfgfile]\n\n            # Check availability\n            if {[server_is_up 127.0.0.1 $port 100] == 0} {\n                puts \"Starting $type #$j at port $port failed, try another\"\n                incr retry -1\n                set port [find_available_port $base_port $::redis_port_count]\n                set cfg [open $cfgfile a+]\n                if {$::tls} {\n                    puts $cfg \"tls-port $port\"\n                    set pport [find_available_port $base_port $::redis_port_count]\n                    puts $cfg \"port $pport\"\n                } else {\n                    puts $cfg \"port $port\"\n                }\n                close $cfg\n            } else {\n                puts \"Starting $type #$j at port $port\"\n                lappend ::pids $pid\n                break\n            }\n        }\n\n        # Check availability finally\n        if {[server_is_up $::host $port 100] == 0} {\n            set logfile [file join $dirname log.txt]\n            puts [exec tail $logfile]\n            abort_sentinel_test \"Problems starting $type #$j: ping timeout, maybe server start failed, check $logfile\"\n        }\n\n        # Push the instance into the right list\n        set link [redis $::host $port 0 $::tls]\n        $link reconnect 1\n        lappend ::${type}_instances [list \\\n            pid $pid \\\n            host $::host \\\n            port $port \\\n            plaintext-port $pport \\\n            link $link \\\n        ]\n    }\n}\n\nproc log_crashes {} {\n    set start_pattern {*REDIS BUG REPORT START*}\n    set logs [glob */log.txt]\n    foreach log $logs {\n        set fd [open $log]\n        set found 0\n        while {[gets $fd line] >= 0} {\n            if {[string match $start_pattern $line]} {\n                puts \"\\n*** Crash report found in $log ***\"\n                set found 1\n            }\n            if {$found} {\n                puts $line\n                incr ::failed\n            }\n        }\n    }\n\n    set logs [glob */err.txt]\n    foreach log $logs {\n        set res [find_valgrind_errors $log true]\n        if {$res != \"\"} {\n            puts $res\n            incr ::failed\n        }\n    }\n}\n\nproc is_alive pid {\n    if {[catch {exec ps -p $pid} err]} {\n        return 0\n    } else {\n        return 1\n    }\n}\n\nproc stop_instance pid {\n    catch {exec kill $pid}\n    # Node might have been stopped in the test\n    catch {exec kill -SIGCONT $pid}\n    if {$::valgrind} {\n        set max_wait 60000\n    } else {\n        set max_wait 10000\n    }\n    while {[is_alive $pid]} {\n        incr wait 10\n\n        if {$wait >= $max_wait} {\n            puts \"Forcing process $pid to exit...\"\n            catch {exec kill -KILL $pid}\n        } elseif {$wait % 1000 == 0} {\n            puts \"Waiting for process $pid to exit...\"\n        }\n        after 10\n    }\n}\n\nproc cleanup {} {\n    puts \"Cleaning up...\"\n    foreach pid $::pids {\n        puts \"killing stale instance $pid\"\n        stop_instance $pid\n    }\n    log_crashes\n    if {$::dont_clean} {\n        return\n    }\n    foreach dir $::dirs {\n        catch {exec rm -rf $dir}\n    }\n}\n\nproc abort_sentinel_test msg {\n    incr ::failed\n    puts \"WARNING: Aborting the test.\"\n    puts \">>>>>>>> $msg\"\n    if {$::pause_on_error} pause_on_error\n    cleanup\n    exit 1\n}\n\nproc parse_options {} {\n    for {set j 0} {$j < [llength $::argv]} {incr j} {\n        set opt [lindex $::argv $j]\n        set val [lindex $::argv [expr $j+1]]\n        if {$opt eq \"--single\"} {\n            incr j\n            set ::run_matching \"*${val}*\"\n        } elseif {$opt eq \"--pause-on-error\"} {\n            set ::pause_on_error 1\n        } elseif {$opt eq {--dont-clean}} {\n            set ::dont_clean 1\n        } elseif {$opt eq \"--fail\"} {\n            set ::simulate_error 1\n        } elseif {$opt eq {--valgrind}} {\n            set ::valgrind 1\n        } elseif {$opt eq {--host}} {\n            incr j\n            set ::host ${val}\n        } elseif {$opt eq {--tls}} {\n            package require tls 1.6\n            ::tls::init \\\n                -cafile \"$::tlsdir/ca.crt\" \\\n                -certfile \"$::tlsdir/client.crt\" \\\n                -keyfile \"$::tlsdir/client.key\"\n            set ::tls 1\n        } elseif {$opt eq {--config}} {\n            set val2 [lindex $::argv [expr $j+2]]\n            dict set ::global_config $val $val2\n            incr j 2\n        } elseif {$opt eq {--flash}} {\n            set ::flash 1\n        } elseif {$opt eq \"--help\"} {\n            puts \"--single <pattern>      Only runs tests specified by pattern.\"\n            puts \"--dont-clean            Keep log files on exit.\"\n            puts \"--pause-on-error        Pause for manual inspection on error.\"\n            puts \"--fail                  Simulate a test failure.\"\n            puts \"--valgrind              Run with valgrind.\"\n            puts \"--tls                   Run tests in TLS mode.\"\n            puts \"--host <host>           Use hostname instead of 127.0.0.1.\"\n            puts \"--config <k> <v>        Extra config argument(s).\"\n            puts \"--flash                 Run the whole suite with flash enabled\"\n            puts \"--help                  Shows this help.\"\n            exit 0\n        } else {\n            puts \"Unknown option $opt\"\n            exit 1\n        }\n    }\n}\n\n# If --pause-on-error option was passed at startup this function is called\n# on error in order to give the developer a chance to understand more about\n# the error condition while the instances are still running.\nproc pause_on_error {} {\n    puts \"\"\n    puts [colorstr yellow \"*** Please inspect the error now ***\"]\n    puts \"\\nType \\\"continue\\\" to resume the test, \\\"help\\\" for help screen.\\n\"\n    while 1 {\n        puts -nonewline \"> \"\n        flush stdout\n        set line [gets stdin]\n        set argv [split $line \" \"]\n        set cmd [lindex $argv 0]\n        if {$cmd eq {continue}} {\n            break\n        } elseif {$cmd eq {show-keydb-logs}} {\n            set count 10\n            set instance {}\n            if {[lindex $argv 1] ne {}} {set count [lindex $argv 1]}\n            if {[lindex $argv 2] ne {}} {set instance [lindex $argv 2]}\n            foreach_redis_id id {\n                if {$instance eq $id || $instance eq {}} {\n                    puts \"=== KeyDB $id ====\"\n                    puts [exec tail -$count redis_$id/log.txt]\n                    puts \"---------------------\\n\"\n                }\n            }\n        } elseif {$cmd eq {show-sentinel-logs}} {\n            set count 10\n            if {[lindex $argv 1] ne {}} {set count [lindex $argv 1]}\n            foreach_sentinel_id id {\n                puts \"=== SENTINEL $id ====\"\n                puts [exec tail -$count sentinel_$id/log.txt]\n                puts \"---------------------\\n\"\n            }\n        } elseif {$cmd eq {ls}} {\n            foreach_redis_id id {\n                puts -nonewline \"KeyDB $id\"\n                set errcode [catch {\n                    set str {}\n                    append str \"@[RI $id tcp_port]: \"\n                    append str \"[RI $id role] \"\n                    if {[RI $id role] eq {slave}} {\n                        append str \"[RI $id master_host]:[RI $id master_port]\"\n                    }\n                    set str\n                } retval]\n                if {$errcode} {\n                    puts \" -- $retval\"\n                } else {\n                    puts $retval\n                }\n            }\n            foreach_sentinel_id id {\n                puts -nonewline \"Sentinel $id\"\n                set errcode [catch {\n                    set str {}\n                    append str \"@[SI $id tcp_port]: \"\n                    append str \"[join [S $id sentinel get-master-addr-by-name mymaster]]\"\n                    set str\n                } retval]\n                if {$errcode} {\n                    puts \" -- $retval\"\n                } else {\n                    puts $retval\n                }\n            }\n        } elseif {$cmd eq {help}} {\n            puts \"ls                     List Sentinel and KeyDB instances.\"\n            puts \"show-sentinel-logs \\[N\\] Show latest N lines of logs.\"\n            puts \"show-keydb-logs \\[N\\] \\[id\\]    Show latest N lines of logs of server id.\"\n            puts \"S <id> cmd ... arg     Call command in Sentinel <id>.\"\n            puts \"R <id> cmd ... arg     Call command in KeyDB <id>.\"\n            puts \"SI <id> <field>        Show Sentinel <id> INFO <field>.\"\n            puts \"RI <id> <field>        Show KeyDB <id> INFO <field>.\"\n            puts \"continue               Resume test.\"\n        } else {\n            set errcode [catch {eval $line} retval]\n            if {$retval ne {}} {puts \"$retval\"}\n        }\n    }\n}\n\n# We redefine 'test' as for Sentinel we don't use the server-client\n# architecture for the test, everything is sequential.\nproc test {descr code} {\n    set ts [clock format [clock seconds] -format %H:%M:%S]\n    puts -nonewline \"$ts> $descr: \"\n    flush stdout\n\n    if {[catch {set retval [uplevel 1 $code]} error]} {\n        incr ::failed\n        if {[string match \"assertion:*\" $error]} {\n            set msg [string range $error 10 end]\n            puts [colorstr red $msg]\n            if {$::pause_on_error} pause_on_error\n            puts \"(Jumping to next unit after error)\"\n            return -code continue\n        } else {\n            # Re-raise, let handler up the stack take care of this.\n            error $error $::errorInfo\n        }\n    } else {\n        puts [colorstr green OK]\n    }\n}\n\n# Check memory leaks when running on OSX using the \"leaks\" utility.\nproc check_leaks instance_types {\n    if {[string match {*Darwin*} [exec uname -a]]} {\n        puts -nonewline \"Testing for memory leaks...\"; flush stdout\n        foreach type $instance_types {\n            foreach_instance_id [set ::${type}_instances] id {\n                if {[instance_is_killed $type $id]} continue\n                set pid [get_instance_attrib $type $id pid]\n                set output {0 leaks}\n                catch {exec leaks $pid} output\n                if {[string match {*process does not exist*} $output] ||\n                    [string match {*cannot examine*} $output]} {\n                    # In a few tests we kill the server process.\n                    set output \"0 leaks\"\n                } else {\n                    puts -nonewline \"$type/$pid \"\n                    flush stdout\n                }\n                if {![string match {*0 leaks*} $output]} {\n                    puts [colorstr red \"=== MEMORY LEAK DETECTED ===\"]\n                    puts \"Instance type $type, ID $id:\"\n                    puts $output\n                    puts \"===\"\n                    incr ::failed\n                }\n            }\n        }\n        puts \"\"\n    }\n}\n\n# Execute all the units inside the 'tests' directory.\nproc run_tests {} {\n    set tests [lsort [glob ../tests/*]]\n    foreach test $tests {\n        # Remove leaked_fds file before starting\n        if {$::leaked_fds_file != \"\" && [file exists $::leaked_fds_file]} {\n            file delete $::leaked_fds_file\n        }\n\n        if {$::run_matching ne {} && [string match $::run_matching $test] == 0} {\n            continue\n        }\n        if {[file isdirectory $test]} continue\n        puts [colorstr yellow \"Testing unit: [lindex [file split $test] end]\"]\n        source $test\n        check_leaks {redis sentinel}\n\n        # Check if a leaked fds file was created and abort the test.\n        if {$::leaked_fds_file != \"\" && [file exists $::leaked_fds_file]} {\n            puts [colorstr red \"ERROR: Sentinel has leaked fds to scripts:\"]\n            puts [exec cat $::leaked_fds_file]\n            puts \"----\"\n            incr ::failed\n        }\n    }\n}\n\n# Print a message and exists with 0 / 1 according to zero or more failures.\nproc end_tests {} {\n    if {$::failed == 0 } {\n        puts \"GOOD! No errors.\"\n        exit 0\n    } else {\n        puts \"WARNING $::failed test(s) failed.\"\n        exit 1\n    }\n}\n\n# The \"S\" command is used to interact with the N-th Sentinel.\n# The general form is:\n#\n# S <sentinel-id> command arg arg arg ...\n#\n# Example to ping the Sentinel 0 (first instance): S 0 PING\nproc S {n args} {\n    set s [lindex $::sentinel_instances $n]\n    [dict get $s link] {*}$args\n}\n\n# Returns a Redis instance by index.\n# Example:\n#     [Rn 0] info\nproc Rn {n} {\n    return [dict get [lindex $::redis_instances $n] link]\n}\n\n# Like R but to chat with Redis instances.\nproc R {n args} {\n    [Rn $n] {*}$args\n}\n\nproc get_info_field {info field} {\n    set fl [string length $field]\n    append field :\n    foreach line [split $info \"\\n\"] {\n        set line [string trim $line \"\\r\\n \"]\n        if {[string range $line 0 $fl] eq $field} {\n            return [string range $line [expr {$fl+1}] end]\n        }\n    }\n    return {}\n}\n\nproc SI {n field} {\n    get_info_field [S $n info] $field\n}\n\nproc RI {n field} {\n    get_info_field [R $n info] $field\n}\n\nproc RPort {n} {\n    if {$::tls} {\n        return [lindex [R $n config get tls-port] 1]\n    } else {\n        return [lindex [R $n config get port] 1]\n    }\n}\n\n# Iterate over IDs of sentinel or redis instances.\nproc foreach_instance_id {instances idvar code} {\n    upvar 1 $idvar id\n    for {set id 0} {$id < [llength $instances]} {incr id} {\n        set errcode [catch {uplevel 1 $code} result]\n        if {$errcode == 1} {\n            error $result $::errorInfo $::errorCode\n        } elseif {$errcode == 4} {\n            continue\n        } elseif {$errcode == 3} {\n            break\n        } elseif {$errcode != 0} {\n            return -code $errcode $result\n        }\n    }\n}\n\nproc foreach_sentinel_id {idvar code} {\n    set errcode [catch {uplevel 1 [list foreach_instance_id $::sentinel_instances $idvar $code]} result]\n    return -code $errcode $result\n}\n\nproc foreach_redis_id {idvar code} {\n    set errcode [catch {uplevel 1 [list foreach_instance_id $::redis_instances $idvar $code]} result]\n    return -code $errcode $result\n}\n\n# Get the specific attribute of the specified instance type, id.\nproc get_instance_attrib {type id attrib} {\n    dict get [lindex [set ::${type}_instances] $id] $attrib\n}\n\n# Set the specific attribute of the specified instance type, id.\nproc set_instance_attrib {type id attrib newval} {\n    set d [lindex [set ::${type}_instances] $id]\n    dict set d $attrib $newval\n    lset ::${type}_instances $id $d\n}\n\n# Create a master-slave cluster of the given number of total instances.\n# The first instance \"0\" is the master, all others are configured as\n# slaves.\nproc create_redis_master_slave_cluster n {\n    foreach_redis_id id {\n        if {$id == 0} {\n            # Our master.\n            R $id slaveof no one\n            R $id flushall\n        } elseif {$id < $n} {\n            R $id slaveof [get_instance_attrib redis 0 host] \\\n                          [get_instance_attrib redis 0 port]\n        } else {\n            # Instances not part of the cluster.\n            R $id slaveof no one\n        }\n    }\n    # Wait for all the slaves to sync.\n    wait_for_condition 1000 50 {\n        [RI 0 connected_slaves] == ($n-1)\n    } else {\n        fail \"Unable to create a master-slaves cluster.\"\n    }\n}\n\nproc get_instance_id_by_port {type port} {\n    foreach_${type}_id id {\n        if {[get_instance_attrib $type $id port] == $port} {\n            return $id\n        }\n    }\n    fail \"Instance $type port $port not found.\"\n}\n\n# Kill an instance of the specified type/id with SIGKILL.\n# This function will mark the instance PID as -1 to remember that this instance\n# is no longer running and will remove its PID from the list of pids that\n# we kill at cleanup.\n#\n# The instance can be restarted with restart-instance.\nproc kill_instance {type id} {\n    set pid [get_instance_attrib $type $id pid]\n    set port [get_instance_attrib $type $id port]\n\n    if {$pid == -1} {\n        error \"You tried to kill $type $id twice.\"\n    }\n\n    stop_instance $pid\n    set_instance_attrib $type $id pid -1\n    set_instance_attrib $type $id link you_tried_to_talk_with_killed_instance\n\n    # Remove the PID from the list of pids to kill at exit.\n    set ::pids [lsearch -all -inline -not -exact $::pids $pid]\n\n    # Wait for the port it was using to be available again, so that's not\n    # an issue to start a new server ASAP with the same port.\n    set retry 100\n    while {[incr retry -1]} {\n        set port_is_free [catch {set s [socket 127.0.0.1 $port]}]\n        if {$port_is_free} break\n        catch {close $s}\n        after 100\n    }\n    if {$retry == 0} {\n        error \"Port $port does not return available after killing instance.\"\n    }\n}\n\n# Return true of the instance of the specified type/id is killed.\nproc instance_is_killed {type id} {\n    set pid [get_instance_attrib $type $id pid]\n    expr {$pid == -1}\n}\n\n# Restart an instance previously killed by kill_instance\nproc restart_instance {type id} {\n    set dirname \"${type}_${id}\"\n    set cfgfile [file join $dirname $type.conf]\n    set port [get_instance_attrib $type $id port]\n\n    # Execute the instance with its old setup and append the new pid\n    # file for cleanup.\n    set pid [exec_instance $type $dirname $cfgfile]\n    set_instance_attrib $type $id pid $pid\n    lappend ::pids $pid\n\n    # Check that the instance is running\n    if {[server_is_up 127.0.0.1 $port 100] == 0} {\n        set logfile [file join $dirname log.txt]\n        puts [exec tail $logfile]\n        abort_sentinel_test \"Problems starting $type #$id: ping timeout, maybe server start failed, check $logfile\"\n    }\n\n    # Connect with it with a fresh link\n    set link [redis 127.0.0.1 $port 0 $::tls]\n    $link reconnect 1\n    set_instance_attrib $type $id link $link\n\n    # Make sure the instance is not loading the dataset when this\n    # function returns.\n    while 1 {\n        catch {[$link ping]} retval\n        if {[string match {*LOADING*} $retval]} {\n            after 100\n            continue\n        } else {\n            break\n        }\n    }\n}\n\nproc redis_deferring_client {type id} {\n    set port [get_instance_attrib $type $id port]\n    set host [get_instance_attrib $type $id host]\n    set client [redis $host $port 1 $::tls]\n    return $client\n}\n\nproc redis_client {type id} {\n    set port [get_instance_attrib $type $id port]\n    set host [get_instance_attrib $type $id host]\n    set client [redis $host $port 0 $::tls]\n    return $client\n}\n"
  },
  {
    "path": "tests/integration/aof-race.tcl",
    "content": "set defaults { appendonly {yes} appendfilename {appendonly.aof} aof-use-rdb-preamble {no} }\nset server_path [tmpdir server.aof]\nset aof_path \"$server_path/appendonly.aof\"\n\nproc start_server_aof {overrides code} {\n    upvar defaults defaults srv srv server_path server_path\n    set config [concat $defaults $overrides]\n    start_server [list overrides $config] $code\n}\n\ntags {\"aof\"} {\n    # Specific test for a regression where internal buffers were not properly\n    # cleaned after a child responsible for an AOF rewrite exited. This buffer\n    # was subsequently appended to the new AOF, resulting in duplicate commands.\n    start_server_aof [list dir $server_path] {\n        set client [redis [srv host] [srv port] 0 $::tls]\n        set bench [open \"|src/keydb-benchmark -q -s [srv unixsocket] -c 20 -n 20000 incr foo\" \"r+\"]\n\n        after 100\n\n        # Benchmark should be running by now: start background rewrite\n        $client bgrewriteaof\n\n        # Read until benchmark pipe reaches EOF\n        while {[string length [read $bench]] > 0} {}\n\n        # Check contents of foo\n        assert_equal 20000 [$client get foo]\n    }\n\n    # Restart server to replay AOF\n    start_server_aof [list dir $server_path] {\n        set client [redis [srv host] [srv port] 0 $::tls]\n        assert_equal 20000 [$client get foo]\n    }\n}\n"
  },
  {
    "path": "tests/integration/aof.tcl",
    "content": "set defaults { appendonly {yes} appendfilename {appendonly.aof} }\nset server_path [tmpdir server.aof]\nset aof_path \"$server_path/appendonly.aof\"\n\nproc append_to_aof {str} {\n    upvar fp fp\n    puts -nonewline $fp $str\n}\n\nproc create_aof {code} {\n    upvar fp fp aof_path aof_path\n    set fp [open $aof_path w+]\n    uplevel 1 $code\n    close $fp\n}\n\nproc start_server_aof {overrides code} {\n    upvar defaults defaults srv srv server_path server_path\n    set config [concat $defaults $overrides]\n    set srv [start_server [list overrides $config]]\n    uplevel 1 $code\n    kill_server $srv\n}\n\ntags {\"aof\"} {\n    ## Server can start when aof-load-truncated is set to yes and AOF\n    ## is truncated, with an incomplete MULTI block.\n    create_aof {\n        append_to_aof [formatCommand set foo hello]\n        append_to_aof [formatCommand multi]\n        append_to_aof [formatCommand set bar world]\n    }\n\n    start_server_aof [list dir $server_path aof-load-truncated yes] {\n        test \"Unfinished MULTI: Server should start if load-truncated is yes\" {\n            assert_equal 1 [is_alive $srv]\n        }\n    }\n\n    ## Should also start with truncated AOF without incomplete MULTI block.\n    create_aof {\n        append_to_aof [formatCommand incr foo]\n        append_to_aof [formatCommand incr foo]\n        append_to_aof [formatCommand incr foo]\n        append_to_aof [formatCommand incr foo]\n        append_to_aof [formatCommand incr foo]\n        append_to_aof [string range [formatCommand incr foo] 0 end-1]\n    }\n\n    start_server_aof [list dir $server_path aof-load-truncated yes] {\n        test \"Short read: Server should start if load-truncated is yes\" {\n            assert_equal 1 [is_alive $srv]\n        }\n\n        test \"Truncated AOF loaded: we expect foo to be equal to 5\" {\n            set client [redis [dict get $srv host] [dict get $srv port] 0 $::tls]\n            wait_done_loading $client\n            assert {[$client get foo] eq \"5\"}\n        }\n\n        test \"Append a new command after loading an incomplete AOF\" {\n            $client incr foo\n        }\n    }\n\n    # Now the AOF file is expected to be correct\n    start_server_aof [list dir $server_path aof-load-truncated yes] {\n        test \"Short read + command: Server should start\" {\n            assert_equal 1 [is_alive $srv]\n        }\n\n        test \"Truncated AOF loaded: we expect foo to be equal to 6 now\" {\n            set client [redis [dict get $srv host] [dict get $srv port] 0 $::tls]\n            wait_done_loading $client\n            assert {[$client get foo] eq \"6\"}\n        }\n    }\n\n    ## Test that the server exits when the AOF contains a format error\n    create_aof {\n        append_to_aof [formatCommand set foo hello]\n        append_to_aof \"!!!\"\n        append_to_aof [formatCommand set foo hello]\n    }\n\n    start_server_aof [list dir $server_path aof-load-truncated yes] {\n        test \"Bad format: Server should have logged an error\" {\n            set pattern \"*Bad file format reading the append only file*\"\n            set retry 10\n            while {$retry} {\n                set result [exec tail -1 < [dict get $srv stdout]]\n                if {[string match $pattern $result]} {\n                    break\n                }\n                incr retry -1\n                after 1000\n            }\n            if {$retry == 0} {\n                error \"assertion:expected error not found on config file\"\n            }\n        }\n    }\n\n    ## Test the server doesn't start when the AOF contains an unfinished MULTI\n    create_aof {\n        append_to_aof [formatCommand set foo hello]\n        append_to_aof [formatCommand multi]\n        append_to_aof [formatCommand set bar world]\n    }\n\n    start_server_aof [list dir $server_path aof-load-truncated no] {\n        test \"Unfinished MULTI: Server should have logged an error\" {\n            set pattern \"*Unexpected end of file reading the append only file*\"\n            set retry 10\n            while {$retry} {\n                set result [exec tail -1 < [dict get $srv stdout]]\n                if {[string match $pattern $result]} {\n                    break\n                }\n                incr retry -1\n                after 1000\n            }\n            if {$retry == 0} {\n                error \"assertion:expected error not found on config file\"\n            }\n        }\n    }\n\n    ## Test that the server exits when the AOF contains a short read\n    create_aof {\n        append_to_aof [formatCommand set foo hello]\n        append_to_aof [string range [formatCommand set bar world] 0 end-1]\n    }\n\n    start_server_aof [list dir $server_path aof-load-truncated no] {\n        test \"Short read: Server should have logged an error\" {\n            set pattern \"*Unexpected end of file reading the append only file*\"\n            set retry 10\n            while {$retry} {\n                set result [exec tail -1 < [dict get $srv stdout]]\n                if {[string match $pattern $result]} {\n                    break\n                }\n                incr retry -1\n                after 1000\n            }\n            if {$retry == 0} {\n                error \"assertion:expected error not found on config file\"\n            }\n        }\n    }\n\n    ## Test that keydb-check-aof indeed sees this AOF is not valid\n    test \"Short read: Utility should confirm the AOF is not valid\" {\n        catch {\n            exec src/keydb-check-aof $aof_path\n        } result\n        assert_match \"*not valid*\" $result\n    }\n\n    test \"Short read: Utility should show the abnormal line num in AOF\" {\n        create_aof {\n            append_to_aof [formatCommand set foo hello]\n            append_to_aof \"!!!\"\n        }\n\n        catch {\n            exec src/keydb-check-aof $aof_path\n        } result\n        assert_match \"*ok_up_to_line=8*\" $result\n    }\n\n    test \"Short read: Utility should be able to fix the AOF\" {\n        set result [exec src/keydb-check-aof --fix $aof_path << \"y\\n\"]\n        assert_match \"*Successfully truncated AOF*\" $result\n    }\n\n    ## Test that the server can be started using the truncated AOF\n    start_server_aof [list dir $server_path aof-load-truncated no] {\n        test \"Fixed AOF: Server should have been started\" {\n            assert_equal 1 [is_alive $srv]\n        }\n\n        test \"Fixed AOF: Keyspace should contain values that were parseable\" {\n            set client [redis [dict get $srv host] [dict get $srv port] 0 $::tls]\n            wait_done_loading $client\n            assert_equal \"hello\" [$client get foo]\n            assert_equal \"\" [$client get bar]\n        }\n    }\n\n    ## Test that SPOP (that modifies the client's argc/argv) is correctly free'd\n    create_aof {\n        append_to_aof [formatCommand sadd set foo]\n        append_to_aof [formatCommand sadd set bar]\n        append_to_aof [formatCommand spop set]\n    }\n\n    start_server_aof [list dir $server_path aof-load-truncated no] {\n        test \"AOF+SPOP: Server should have been started\" {\n            assert_equal 1 [is_alive $srv]\n        }\n\n        test \"AOF+SPOP: Set should have 1 member\" {\n            set client [redis [dict get $srv host] [dict get $srv port] 0 $::tls]\n            wait_done_loading $client\n            assert_equal 1 [$client scard set]\n        }\n    }\n\n    ## Uses the alsoPropagate() API.\n    create_aof {\n        append_to_aof [formatCommand sadd set foo]\n        append_to_aof [formatCommand sadd set bar]\n        append_to_aof [formatCommand sadd set gah]\n        append_to_aof [formatCommand spop set 2]\n    }\n\n    start_server_aof [list dir $server_path] {\n        test \"AOF+SPOP: Server should have been started\" {\n            assert_equal 1 [is_alive $srv]\n        }\n\n        test \"AOF+SPOP: Set should have 1 member\" {\n            set client [redis [dict get $srv host] [dict get $srv port] 0 $::tls]\n            wait_done_loading $client\n            assert_equal 1 [$client scard set]\n        }\n    }\n\n    ## Test that EXPIREAT is loaded correctly\n    create_aof {\n        append_to_aof [formatCommand rpush list foo]\n        append_to_aof [formatCommand expireat list 1000]\n        append_to_aof [formatCommand rpush list bar]\n    }\n\n    start_server_aof [list dir $server_path aof-load-truncated no] {\n        test \"AOF+EXPIRE: Server should have been started\" {\n            assert_equal 1 [is_alive $srv]\n        }\n\n        test \"AOF+EXPIRE: List should be empty\" {\n            set client [redis [dict get $srv host] [dict get $srv port] 0 $::tls]\n            wait_done_loading $client\n            assert_equal 0 [$client llen list]\n        }\n    }\n\n    start_server {overrides {appendonly {yes} appendfilename {appendonly.aof}}} {\n        test {Redis should not try to convert DEL into EXPIREAT for EXPIRE -1} {\n            r set x 10\n            r expire x -1\n        }\n    }\n\n    # Because of how this test works its inherently unreliable with multithreading, so force threads 1\n    #   No real client should rely on this undocumented behavior\n    start_server {overrides {appendonly {yes} appendfilename {appendonly.aof} appendfsync always server-threads 1}} {\n        test {AOF fsync always barrier issue} {\n            set rd [redis_deferring_client]\n            # Set a sleep when aof is flushed, so that we have a chance to look\n            # at the aof size and detect if the response of an incr command\n            # arrives before the data was written (and hopefully fsynced)\n            # We create a big reply, which will hopefully not have room in the\n            # socket buffers, and will install a write handler, then we sleep\n            # a big and issue the incr command, hoping that the last portion of\n            # the output buffer write, and the processing of the incr will happen\n            # in the same event loop cycle.\n            # Since the socket buffers and timing are unpredictable, we fuzz this\n            # test with slightly different sizes and sleeps a few times.\n            for {set i 0} {$i < 10} {incr i} {\n                r debug aof-flush-sleep 0\n                r del x\n                r setrange x [expr {int(rand()*5000000)+10000000}] x\n                r debug aof-flush-sleep 500000\n                set aof [file join [lindex [r config get dir] 1] appendonly.aof]\n                set size1 [file size $aof]\n                $rd get x\n                after [expr {int(rand()*30)}]\n                $rd incr new_value\n                $rd read\n                $rd read\n                set size2 [file size $aof]\n                assert {$size1 != $size2}\n            }\n        }\n    }\n    \n    ## Test that PEXPIREMEMBERAT is loaded correctly\n    create_aof {\n        append_to_aof [formatCommand sadd testkey a b c d]\n        append_to_aof [formatCommand pexpirememberat testkey a 1000]\n    }\n\n    start_server_aof [list dir $server_path aof-load-truncated no] {\n        test \"AOF+EXPIREMEMBER: Server shuold have been started\" {\n            assert_equal 1 [is_alive $srv]\n        }\n\n        test \"AOF+PEXPIREMEMBERAT: set should have 3 values\" {\n            set client [redis [dict get $srv host] [dict get $srv port] 0 $::tls]\n            wait_for_condition 50 100 {\n                [catch {$client ping} e] == 0\n            } else {\n                fail \"Loading DB is taking too much time.\"\n            }\n            assert_equal 3 [$client scard testkey]\n        }\n    }\n\n    start_server {overrides {appendonly {yes} appendfilename {appendonly.aof}}} {\n        test {GETEX should not append to AOF} {\n            set aof [file join [lindex [r config get dir] 1] appendonly.aof]\n            r set foo bar\n            set before [file size $aof]\n            r getex foo\n            set after [file size $aof]\n            assert_equal $before $after\n        }\n    }\n}\n"
  },
  {
    "path": "tests/integration/block-repl.tcl",
    "content": "# Test replication of blocking lists and zset operations.\n# Unlike stream operations such operations are \"pop\" style, so they consume\n# the list or sorted set, and must be replicated correctly.\n\nproc start_bg_block_op {host port db ops tls} {\n    set tclsh [info nameofexecutable]\n    exec $tclsh tests/helpers/bg_block_op.tcl $host $port $db $ops $tls &\n}\n\nproc stop_bg_block_op {handle} {\n    catch {exec /bin/kill -9 $handle}\n}\n\nstart_server {tags {\"repl\"}} {\n    start_server {} {\n        set master [srv -1 client]\n        set master_host [srv -1 host]\n        set master_port [srv -1 port]\n        set slave [srv 0 client]\n\n        set load_handle0 [start_bg_block_op $master_host $master_port 9 100000 $::tls]\n        set load_handle1 [start_bg_block_op $master_host $master_port 9 100000 $::tls]\n        set load_handle2 [start_bg_block_op $master_host $master_port 9 100000 $::tls]\n\n        test {First server should have role slave after SLAVEOF} {\n            $slave slaveof $master_host $master_port\n            after 1000\n            s 0 role\n        } {slave}\n\n        test {Test replication with blocking lists and sorted sets operations} {\n            after 25000\n            stop_bg_block_op $load_handle0\n            stop_bg_block_op $load_handle1\n            stop_bg_block_op $load_handle2\n            wait_for_condition 100 100 {\n                [$master debug digest] == [$slave debug digest]\n            } else {\n                set csv1 [csvdump r]\n                set csv2 [csvdump {r -1}]\n                set fd [open /tmp/repldump1.txt w]\n                puts -nonewline $fd $csv1\n                close $fd\n                set fd [open /tmp/repldump2.txt w]\n                puts -nonewline $fd $csv2\n                close $fd\n                fail \"Master - Replica inconsistency, Run diff -u against /tmp/repldump*.txt for more info\"\n            }\n        }\n    }\n}\n"
  },
  {
    "path": "tests/integration/corrupt-dump-fuzzer.tcl",
    "content": "# tests of corrupt ziplist payload with valid CRC\n\ntags {\"dump\" \"corruption\"} {\n\nproc generate_collections {suffix elements} {\n    set rd [redis_deferring_client]\n    for {set j 0} {$j < $elements} {incr j} {\n        # add both string values and integers\n        if {$j % 2 == 0} {set val $j} else {set val \"_$j\"}\n        $rd hset hash$suffix $j $val\n        $rd lpush list$suffix $val\n        $rd zadd zset$suffix $j $val\n        $rd sadd set$suffix $val\n        $rd xadd stream$suffix * item 1 value $val\n    }\n    for {set j 0} {$j < $elements * 5} {incr j} {\n        $rd read ; # Discard replies\n    }\n    $rd close\n}\n\n# generate keys with various types and encodings\nproc generate_types {} {\n    r config set list-max-ziplist-size 5\n    r config set hash-max-ziplist-entries 5\n    r config set zset-max-ziplist-entries 5\n    r config set stream-node-max-entries 5\n\n    # create small (ziplist / listpack encoded) objects with 3 items\n    generate_collections \"\" 3\n\n    # add some metadata to the stream\n    r xgroup create stream mygroup 0\n    set records [r xreadgroup GROUP mygroup Alice COUNT 2 STREAMS stream >]\n    r xdel stream [lindex [lindex [lindex [lindex $records 0] 1] 1] 0]\n    r xack stream mygroup [lindex [lindex [lindex [lindex $records 0] 1] 0] 0]\n\n    # create other non-collection types\n    r incr int\n    r set string str\n\n    # create bigger objects with 10 items (more than a single ziplist / listpack)\n    generate_collections big 10\n\n    # make sure our big stream also has a listpack record that has different\n    # field names than the master recored\n    r xadd streambig * item 1 value 1\n    r xadd streambig * item 1 unique value\n}\n\nproc corrupt_payload {payload} {\n    set len [string length $payload]\n    set count 1 ;# usually corrupt only one byte\n    if {rand() > 0.9} { set count 2 }\n    while { $count > 0 } {\n        set idx [expr {int(rand() * $len)}]\n        set ch [binary format c [expr {int(rand()*255)}]]\n        set payload [string replace $payload $idx $idx $ch]\n        incr count -1\n    }\n    return $payload\n}\n\n# fuzzy tester for corrupt RESTORE payloads\n# valgrind will make sure there were no leaks in the rdb loader error handling code\nforeach sanitize_dump {no yes} {\n    if {$::accurate} {\n        set min_duration [expr {60 * 10}] ;# run at least 10 minutes\n        set min_cycles 1000 ;# run at least 1k cycles (max 16 minutes)\n    } else {\n        set min_duration 10 ; # run at least 10 seconds\n        set min_cycles 10 ; # run at least 10 cycles\n    }\n\n    # Don't execute this on FreeBSD due to a yet-undiscovered memory issue\n    # which causes tclsh to bloat.\n    if {[exec uname] == \"FreeBSD\"} {\n        set min_cycles 1\n        set min_duration 1\n    }\n\n    test \"Fuzzer corrupt restore payloads - sanitize_dump: $sanitize_dump\" {\n        if {$min_duration * 2 > $::timeout} {\n            fail \"insufficient timeout\"\n        }\n        # start a server, fill with data and save an RDB file once (avoid re-save)\n        start_server [list overrides [list \"save\" \"\" use-exit-on-panic yes crash-memcheck-enabled no loglevel verbose] ] {\n            set stdout [srv 0 stdout]\n            r config set sanitize-dump-payload $sanitize_dump\n            r debug set-skip-checksum-validation 1\n            set start_time [clock seconds]\n            generate_types\n            set dbsize [r dbsize]\n            r save\n            set cycle 0\n            set stat_terminated_in_restore 0\n            set stat_terminated_in_traffic 0\n            set stat_terminated_by_signal 0\n            set stat_successful_restore 0\n            set stat_rejected_restore 0\n            set stat_traffic_commands_sent 0\n            # repeatedly DUMP a random key, corrupt it and try RESTORE into a new key\n            while true {\n                set k [r randomkey]\n                set dump [r dump $k]\n                set dump [corrupt_payload $dump]\n                set printable_dump [string2printable $dump]\n                set restore_failed false\n                set report_and_restart false\n                set sent {}\n                # RESTORE can fail, but hopefully not terminate\n                if { [catch { r restore \"_$k\" 0 $dump REPLACE } err] } {\n                    set restore_failed true\n                    # skip if return failed with an error response.\n                    if {[string match \"ERR*\" $err]} {\n                        incr stat_rejected_restore\n                    } else {\n                        set report_and_restart true\n                        incr stat_terminated_in_restore\n                        write_log_line 0 \"corrupt payload: $printable_dump\"\n                        if {$sanitize_dump == yes} {\n                            puts \"Server crashed in RESTORE with payload: $printable_dump\"\n                        }\n                    }\n                } else {\n                    r ping ;# an attempt to check if the server didn't terminate (this will throw an error that will terminate the tests)\n                }\n\n                set print_commands false\n                if {!$restore_failed} {\n                    # if RESTORE didn't fail or terminate, run some random traffic on the new key\n                    incr stat_successful_restore\n                    if { [ catch {\n                        set sent [generate_fuzzy_traffic_on_key \"_$k\" 1] ;# traffic for 1 second\n                        incr stat_traffic_commands_sent [llength $sent]\n                        r del \"_$k\" ;# in case the server terminated, here's where we'll detect it.\n                        if {$dbsize != [r dbsize]} {\n                            puts \"unexpected keys\"\n                            puts \"keys: [r keys *]\"\n                            puts $sent\n                            exit 1\n                        }\n                    } err ] } {\n                        # if the server terminated update stats and restart it\n                        set report_and_restart true\n                        incr stat_terminated_in_traffic\n                        set by_signal [count_log_message 0 \"crashed by signal\"]\n                        incr stat_terminated_by_signal $by_signal\n\n                        if {$by_signal != 0 || $sanitize_dump == yes} {\n                            puts \"Server crashed (by signal: $by_signal), with payload: $printable_dump\"\n                            set print_commands true\n                        }\n                    }\n                }\n\n                # check valgrind report for invalid reads after each RESTORE\n                # payload so that we have a report that is easier to reproduce\n                set valgrind_errors [find_valgrind_errors [srv 0 stderr] false]\n                if {$valgrind_errors != \"\"} {\n                    puts \"valgrind found an issue for payload: $printable_dump\"\n                    set report_and_restart true\n                    set print_commands true\n                }\n\n                if {$report_and_restart} {\n                    if {$print_commands} {\n                        puts \"violating commands:\"\n                        foreach cmd $sent {\n                            foreach arg $cmd {\n                                puts -nonewline \"[string2printable $arg] \"\n                            }\n                            puts \"\"\n                        }\n                    }\n\n                    # restart the server and re-apply debug configuration\n                    write_log_line 0 \"corrupt payload: $printable_dump\"\n                    restart_server 0 true true\n                    r config set sanitize-dump-payload $sanitize_dump\n                    r debug set-skip-checksum-validation 1\n                }\n\n                incr cycle\n                if { ([clock seconds]-$start_time) >= $min_duration && $cycle >= $min_cycles} {\n                    break\n                }\n            }\n            if {$::verbose} {\n                puts \"Done $cycle cycles in [expr {[clock seconds]-$start_time}] seconds.\"\n                puts \"RESTORE: successful: $stat_successful_restore, rejected: $stat_rejected_restore\"\n                puts \"Total commands sent in traffic: $stat_traffic_commands_sent, crashes during traffic: $stat_terminated_in_traffic ($stat_terminated_by_signal by signal).\"\n            }\n        }\n        # if we run sanitization we never expect the server to crash at runtime\n        if {$sanitize_dump == yes} {\n            assert_equal $stat_terminated_in_restore 0\n            assert_equal $stat_terminated_in_traffic 0\n        }\n        # make sure all terminations where due to assertion and not a SIGSEGV\n        assert_equal $stat_terminated_by_signal 0\n    }\n}\n\n\n\n} ;# tags\n\n"
  },
  {
    "path": "tests/integration/corrupt-dump.tcl",
    "content": "# tests of corrupt ziplist payload with valid CRC\n# * setting crash-memcheck-enabled to no to avoid issues with valgrind\n# * setting use-exit-on-panic to yes so that valgrind can search for leaks\n# * settng debug set-skip-checksum-validation to 1 on some tests for which we\n#   didn't bother to fake a valid checksum\n# * some tests set sanitize-dump-payload to no and some to yet, depending on\n#   what we want to test\n\ntags {\"dump\" \"corruption\"} {\n\nset corrupt_payload_7445 \"\\x0E\\x01\\x1D\\x1D\\x00\\x00\\x00\\x16\\x00\\x00\\x00\\x03\\x00\\x00\\x04\\x43\\x43\\x43\\x43\\x06\\x04\\x42\\x42\\x42\\x42\\x06\\x3F\\x41\\x41\\x41\\x41\\xFF\\x09\\x00\\x88\\xA5\\xCA\\xA8\\xC5\\x41\\xF4\\x35\"\n\ntest {corrupt payload: #7445 - with sanitize} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload yes\n        catch {\n            r restore key 0 $corrupt_payload_7445\n        } err\n        assert_match \"*Bad data format*\" $err\n        verify_log_message 0 \"*integrity check failed*\" 0\n    }\n}\n\ntest {corrupt payload: #7445 - without sanitize - 1} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r restore key 0 $corrupt_payload_7445\n        catch {r lindex key 2}\n        assert_equal [count_log_message 0 \"crashed by signal\"] 0\n        assert_equal [count_log_message 0 \"ASSERTION FAILED\"] 1\n    }\n}\n\ntest {corrupt payload: #7445 - without sanitize - 2} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r restore key 0 $corrupt_payload_7445\n        catch {r lset key 2 \"BEEF\"}\n        assert_equal [count_log_message 0 \"crashed by signal\"] 0\n        assert_equal [count_log_message 0 \"ASSERTION FAILED\"] 1\n    }\n}\n\ntest {corrupt payload: hash with valid zip list header, invalid entry len} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r restore key 0 \"\\x0D\\x1B\\x1B\\x00\\x00\\x00\\x16\\x00\\x00\\x00\\x04\\x00\\x00\\x02\\x61\\x00\\x04\\x02\\x62\\x00\\x04\\x14\\x63\\x00\\x04\\x02\\x64\\x00\\xFF\\x09\\x00\\xD9\\x10\\x54\\x92\\x15\\xF5\\x5F\\x52\"\n        r config set hash-max-ziplist-entries 1\n        catch {r hset key b b}\n        verify_log_message 0 \"*zipEntrySafe*\" 0\n    }\n}\n\ntest {corrupt payload: invalid zlbytes header} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        catch {\n            r restore key 0 \"\\x0D\\x1B\\x25\\x00\\x00\\x00\\x16\\x00\\x00\\x00\\x04\\x00\\x00\\x02\\x61\\x00\\x04\\x02\\x62\\x00\\x04\\x02\\x63\\x00\\x04\\x02\\x64\\x00\\xFF\\x09\\x00\\xB7\\xF7\\x6E\\x9F\\x43\\x43\\x14\\xC6\"\n        } err\n        assert_match \"*Bad data format*\" $err\n    }\n}\n\ntest {corrupt payload: valid zipped hash header, dup records} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r restore key 0 \"\\x0D\\x1B\\x1B\\x00\\x00\\x00\\x16\\x00\\x00\\x00\\x04\\x00\\x00\\x02\\x61\\x00\\x04\\x02\\x62\\x00\\x04\\x02\\x61\\x00\\x04\\x02\\x64\\x00\\xFF\\x09\\x00\\xA1\\x98\\x36\\x78\\xCC\\x8E\\x93\\x2E\"\n        r config set hash-max-ziplist-entries 1\n        # cause an assertion when converting to hash table\n        catch {r hset key b b}\n        verify_log_message 0 \"*ziplist with dup elements dump*\" 0\n    }\n}\n\ntest {corrupt payload: quicklist big ziplist prev len} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r restore key 0 \"\\x0E\\x01\\x13\\x13\\x00\\x00\\x00\\x0E\\x00\\x00\\x00\\x02\\x00\\x00\\x02\\x61\\x00\\x0E\\x02\\x62\\x00\\xFF\\x09\\x00\\x49\\x97\\x30\\xB2\\x0D\\xA1\\xED\\xAA\"\n        catch {r lindex key -2}\n        assert_equal [count_log_message 0 \"crashed by signal\"] 0\n        assert_equal [count_log_message 0 \"ASSERTION FAILED\"] 1\n    }\n}\n\ntest {corrupt payload: quicklist small ziplist prev len} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload yes\n        catch {\n            r restore key 0 \"\\x0E\\x01\\x13\\x13\\x00\\x00\\x00\\x0E\\x00\\x00\\x00\\x02\\x00\\x00\\x02\\x61\\x00\\x02\\x02\\x62\\x00\\xFF\\x09\\x00\\xC7\\x71\\x03\\x97\\x07\\x75\\xB0\\x63\"\n        } err\n        assert_match \"*Bad data format*\" $err\n        verify_log_message 0 \"*integrity check failed*\" 0\n    }\n}\n\ntest {corrupt payload: quicklist ziplist wrong count} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r restore key 0 \"\\x0E\\x01\\x13\\x13\\x00\\x00\\x00\\x0E\\x00\\x00\\x00\\x03\\x00\\x00\\x02\\x61\\x00\\x04\\x02\\x62\\x00\\xFF\\x09\\x00\\x4D\\xE2\\x0A\\x2F\\x08\\x25\\xDF\\x91\"\n        # we'll be able to push, but iterating on the list will assert\n        r lpush key header\n        r rpush key footer\n        catch { [r lrange key -1 -1] }\n        assert_equal [count_log_message 0 \"crashed by signal\"] 0\n        assert_equal [count_log_message 0 \"ASSERTION FAILED\"] 1\n    }\n}\n\ntest {corrupt payload: #3080 - quicklist} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        catch {\n            r RESTORE key 0 \"\\x0E\\x01\\x80\\x00\\x00\\x00\\x10\\x41\\x41\\x41\\x41\\x41\\x41\\x41\\x41\\x02\\x00\\x00\\x80\\x41\\x41\\x41\\x41\\x07\\x00\\x03\\xC7\\x1D\\xEF\\x54\\x68\\xCC\\xF3\"\n            r DUMP key ;# DUMP was used in the original issue, but now even with shallow sanitization restore safely fails, so this is dead code\n        } err\n        assert_match \"*Bad data format*\" $err\n        verify_log_message 0 \"*integrity check failed*\" 0\n    }\n}\n\ntest {corrupt payload: quicklist with empty ziplist} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r debug set-skip-checksum-validation 1\n        catch {r restore key 0 \"\\x0E\\x01\\x0B\\x0B\\x00\\x00\\x00\\x0A\\x00\\x00\\x00\\x00\\x00\\xFF\\x09\\x00\\xC2\\x69\\x37\\x83\\x3C\\x7F\\xFE\\x6F\" replace} err\n        assert_match \"*Bad data format*\" $err\n        r ping\n    }\n}\n\ntest {corrupt payload: #3080 - ziplist} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        # shallow sanitization is enough for restore to safely reject the payload with wrong size\n        r config set sanitize-dump-payload no\n        catch {\n            r RESTORE key 0 \"\\x0A\\x80\\x00\\x00\\x00\\x10\\x41\\x41\\x41\\x41\\x41\\x41\\x41\\x41\\x02\\x00\\x00\\x80\\x41\\x41\\x41\\x41\\x07\\x00\\x39\\x5B\\x49\\xE0\\xC1\\xC6\\xDD\\x76\"\n        } err\n        assert_match \"*Bad data format*\" $err\n        verify_log_message 0 \"*integrity check failed*\" 0\n    }\n}\n\ntest {corrupt payload: load corrupted rdb with no CRC - #3505} {\n    set server_path [tmpdir \"server.rdb-corruption-test\"]\n    exec cp tests/assets/corrupt_ziplist.rdb $server_path\n    set srv [start_server [list overrides [list \"dir\" $server_path \"dbfilename\" \"corrupt_ziplist.rdb\" loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no sanitize-dump-payload no]]]\n\n    # wait for termination\n    wait_for_condition 100 50 {\n        ! [is_alive $srv]\n    } else {\n        fail \"rdb loading didn't fail\"\n    }\n\n    set stdout [dict get $srv stdout]\n    assert_equal [count_message_lines $stdout \"Terminating server after rdb file reading failure.\"]  1\n    assert_lessthan 1 [count_message_lines $stdout \"integrity check failed\"]\n    kill_server $srv ;# let valgrind look for issues\n}\n\nforeach sanitize_dump {no yes} {\n    test {corrupt payload: load corrupted rdb with empty keys} {\n        set server_path [tmpdir \"server.rdb-corruption-empty-keys-test\"]\n        exec cp tests/assets/corrupt_empty_keys.rdb $server_path\n        start_server [list overrides [list \"dir\" $server_path \"dbfilename\" \"corrupt_empty_keys.rdb\" \"sanitize-dump-payload\" $sanitize_dump]] {\n            r select 0\n            assert_equal [r dbsize] 0\n\n            verify_log_message 0 \"*skipping empty key: set*\" 0\n            verify_log_message 0 \"*skipping empty key: list_quicklist*\" 0\n            verify_log_message 0 \"*skipping empty key: list_quicklist_empty_ziplist*\" 0\n            verify_log_message 0 \"*skipping empty key: list_ziplist*\" 0\n            verify_log_message 0 \"*skipping empty key: hash*\" 0\n            verify_log_message 0 \"*skipping empty key: hash_ziplist*\" 0\n            verify_log_message 0 \"*skipping empty key: zset*\" 0\n            verify_log_message 0 \"*skipping empty key: zset_ziplist*\" 0\n            verify_log_message 0 \"*empty keys skipped: 8*\" 0\n        }\n    }\n}\n\ntest {corrupt payload: listpack invalid size header} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        catch {\n            r restore key 0 \"\\x0F\\x01\\x10\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x40\\x55\\x5F\\x00\\x00\\x00\\x0F\\x00\\x01\\x01\\x00\\x01\\x02\\x01\\x88\\x31\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x88\\x32\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x00\\x01\\x00\\x01\\x00\\x01\\x00\\x01\\x02\\x02\\x88\\x31\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x88\\x61\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x88\\x32\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x88\\x62\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x08\\x01\\xFF\\x0A\\x01\\x00\\x00\\x09\\x00\\x45\\x91\\x0A\\x87\\x2F\\xA5\\xF9\\x2E\"\n        } err\n        assert_match \"*Bad data format*\" $err\n        verify_log_message 0 \"*Stream listpack integrity check failed*\" 0\n    }\n}\n\ntest {corrupt payload: listpack too long entry len} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r restore key 0 \"\\x0F\\x01\\x10\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x40\\x55\\x55\\x00\\x00\\x00\\x0F\\x00\\x01\\x01\\x00\\x01\\x02\\x01\\x88\\x31\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x88\\x32\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x00\\x01\\x00\\x01\\x00\\x01\\x00\\x01\\x02\\x02\\x89\\x31\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x88\\x61\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x88\\x32\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x88\\x62\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x08\\x01\\xFF\\x0A\\x01\\x00\\x00\\x09\\x00\\x40\\x63\\xC9\\x37\\x03\\xA2\\xE5\\x68\"\n        catch {\n            r xinfo stream key full\n        } err\n        assert_equal [count_log_message 0 \"crashed by signal\"] 0\n        assert_equal [count_log_message 0 \"ASSERTION FAILED\"] 1\n    }\n}\n\ntest {corrupt payload: listpack very long entry len} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r restore key 0 \"\\x0F\\x01\\x10\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x40\\x55\\x55\\x00\\x00\\x00\\x0F\\x00\\x01\\x01\\x00\\x01\\x02\\x01\\x88\\x31\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x88\\x32\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x00\\x01\\x00\\x01\\x00\\x01\\x00\\x01\\x02\\x02\\x88\\x31\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x88\\x61\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x88\\x32\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x9C\\x62\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x08\\x01\\xFF\\x0A\\x01\\x00\\x00\\x09\\x00\\x63\\x6F\\x42\\x8E\\x7C\\xB5\\xA2\\x9D\"\n        catch {\n            r xinfo stream key full\n        } err\n        assert_equal [count_log_message 0 \"crashed by signal\"] 0\n        assert_equal [count_log_message 0 \"ASSERTION FAILED\"] 1\n    }\n}\n\ntest {corrupt payload: listpack too long entry prev len} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload yes\n        catch {\n            r restore key 0 \"\\x0F\\x01\\x10\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02\\x40\\x55\\x55\\x00\\x00\\x00\\x0F\\x00\\x01\\x01\\x00\\x15\\x02\\x01\\x88\\x31\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x88\\x32\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x00\\x01\\x00\\x01\\x00\\x01\\x00\\x01\\x02\\x02\\x88\\x31\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x88\\x61\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x88\\x32\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x88\\x62\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x08\\x01\\xFF\\x0A\\x01\\x00\\x00\\x09\\x00\\x06\\xFB\\x44\\x24\\x0A\\x8E\\x75\\xEA\"\n        } err\n        assert_match \"*Bad data format*\" $err\n        verify_log_message 0 \"*Stream listpack integrity check failed*\" 0\n    }\n}\n\ntest {corrupt payload: hash ziplist with duplicate records} {\n    # when we do perform full sanitization, we expect duplicate records to fail the restore\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload yes\n        r debug set-skip-checksum-validation 1\n        catch { r RESTORE _hash 0 \"\\x0D\\x3D\\x3D\\x00\\x00\\x00\\x3A\\x00\\x00\\x00\\x14\\x13\\x00\\xF5\\x02\\xF5\\x02\\xF2\\x02\\x53\\x5F\\x31\\x04\\xF3\\x02\\xF3\\x02\\xF7\\x02\\xF7\\x02\\xF8\\x02\\x02\\x5F\\x37\\x04\\xF1\\x02\\xF1\\x02\\xF6\\x02\\x02\\x5F\\x35\\x04\\xF4\\x02\\x02\\x5F\\x33\\x04\\xFA\\x02\\x02\\x5F\\x39\\x04\\xF9\\x02\\xF9\\xFF\\x09\\x00\\xB5\\x48\\xDE\\x62\\x31\\xD0\\xE5\\x63\" } err\n        assert_match \"*Bad data format*\" $err\n    }\n}\n\ntest {corrupt payload: hash ziplist uneven record count} {\n    # when we do perform full sanitization, we expect duplicate records to fail the restore\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload yes\n        r debug set-skip-checksum-validation 1\n        catch { r RESTORE _hash 0 \"\\r\\x1b\\x1b\\x00\\x00\\x00\\x16\\x00\\x00\\x00\\x04\\x00\\x00\\x02a\\x00\\x04\\x02b\\x00\\x04\\x02a\\x00\\x04\\x02d\\x00\\xff\\t\\x00\\xa1\\x98\\x36x\\xcc\\x8e\\x93\\x2e\" } err\n        assert_match \"*Bad data format*\" $err\n    }\n}\n\ntest {corrupt payload: hash dupliacte records} {\n    # when we do perform full sanitization, we expect duplicate records to fail the restore\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload yes\n        r debug set-skip-checksum-validation 1\n        catch { r RESTORE _hash 0 \"\\x04\\x02\\x01a\\x01b\\x01a\\x01d\\t\\x00\\xc6\\x9c\\xab\\xbc\\bk\\x0c\\x06\" } err\n        assert_match \"*Bad data format*\" $err\n    }\n}\n\ntest {corrupt payload: hash empty zipmap} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r debug set-skip-checksum-validation 1\n        catch { r RESTORE _hash 0 \"\\x09\\x02\\x00\\xFF\\x09\\x00\\xC0\\xF1\\xB8\\x67\\x4C\\x16\\xAC\\xE3\" } err\n        assert_match \"*Bad data format*\" $err\n        verify_log_message 0 \"*Zipmap integrity check failed*\" 0\n    }\n}\n\ntest {corrupt payload: fuzzer findings - NPD in streamIteratorGetID} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r debug set-skip-checksum-validation 1\n        catch {\n            r RESTORE key 0 \"\\x0F\\x01\\x10\\x00\\x00\\x01\\x73\\xBD\\x68\\x48\\x71\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x40\\x42\\x42\\x00\\x00\\x00\\x18\\x00\\x03\\x01\\x00\\x01\\x02\\x01\\x84\\x69\\x74\\x65\\x6D\\x05\\x85\\x76\\x61\\x6C\\x75\\x65\\x06\\x00\\x01\\x02\\x01\\x00\\x01\\x00\\x01\\x01\\x01\\x00\\x01\\x05\\x01\\x02\\x01\\x00\\x01\\x01\\x01\\x01\\x01\\x82\\x5F\\x31\\x03\\x05\\x01\\x02\\x01\\x00\\x01\\x02\\x01\\x01\\x01\\x02\\x01\\x48\\x01\\xFF\\x03\\x81\\x00\\x00\\x01\\x73\\xBD\\x68\\x48\\x71\\x02\\x01\\x07\\x6D\\x79\\x67\\x72\\x6F\\x75\\x70\\x81\\x00\\x00\\x01\\x73\\xBD\\x68\\x48\\x71\\x00\\x01\\x00\\x00\\x01\\x73\\xBD\\x68\\x48\\x71\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x72\\x48\\x68\\xBD\\x73\\x01\\x00\\x00\\x01\\x01\\x05\\x41\\x6C\\x69\\x63\\x65\\x72\\x48\\x68\\xBD\\x73\\x01\\x00\\x00\\x01\\x00\\x00\\x01\\x73\\xBD\\x68\\x48\\x71\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x00\\x80\\xCD\\xB0\\xD5\\x1A\\xCE\\xFF\\x10\"\n            r XREVRANGE key 725 233\n        }\n        assert_equal [count_log_message 0 \"crashed by signal\"] 0\n        assert_equal [count_log_message 0 \"ASSERTION FAILED\"] 1\n    }\n}\n\ntest {corrupt payload: fuzzer findings - listpack NPD on invalid stream} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r debug set-skip-checksum-validation 1\n        catch {\n            r RESTORE _stream 0 \"\\x0F\\x01\\x10\\x00\\x00\\x01\\x73\\xDC\\xB6\\x6B\\xF1\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x40\\x42\\x42\\x00\\x00\\x00\\x18\\x00\\x03\\x01\\x00\\x01\\x02\\x01\\x84\\x69\\x74\\x65\\x6D\\x05\\x85\\x76\\x61\\x6C\\x75\\x65\\x06\\x00\\x01\\x02\\x01\\x00\\x01\\x00\\x01\\x01\\x01\\x00\\x01\\x05\\x01\\x02\\x01\\x1F\\x01\\x00\\x01\\x01\\x01\\x6D\\x5F\\x31\\x03\\x05\\x01\\x02\\x01\\x29\\x01\\x00\\x01\\x01\\x01\\x02\\x01\\x05\\x01\\xFF\\x03\\x81\\x00\\x00\\x01\\x73\\xDC\\xB6\\x6C\\x1A\\x00\\x01\\x07\\x6D\\x79\\x67\\x72\\x6F\\x75\\x70\\x81\\x00\\x00\\x01\\x73\\xDC\\xB6\\x6B\\xF1\\x00\\x01\\x00\\x00\\x01\\x73\\xDC\\xB6\\x6B\\xF1\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x4B\\x6C\\xB6\\xDC\\x73\\x01\\x00\\x00\\x01\\x01\\x05\\x41\\x6C\\x69\\x63\\x65\\x3D\\x6C\\xB6\\xDC\\x73\\x01\\x00\\x00\\x01\\x00\\x00\\x01\\x73\\xDC\\xB6\\x6B\\xF1\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x00\\xC7\\x7D\\x1C\\xD7\\x04\\xFF\\xE6\\x9D\"\n            r XREAD STREAMS _stream 519389898758\n        }\n        assert_equal [count_log_message 0 \"crashed by signal\"] 0\n        assert_equal [count_log_message 0 \"ASSERTION FAILED\"] 1\n    }\n}\n\ntest {corrupt payload: fuzzer findings - NPD in quicklistIndex} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r debug set-skip-checksum-validation 1\n        catch {\n            r RESTORE key 0 \"\\x0E\\x01\\x13\\x13\\x00\\x00\\x00\\x10\\x00\\x00\\x00\\x03\\x12\\x00\\xF3\\x02\\x02\\x5F\\x31\\x04\\xF1\\xFF\\x09\\x00\\xC9\\x4B\\x31\\xFE\\x61\\xC0\\x96\\xFE\"\n            r LSET key 290 290\n        }\n        assert_equal [count_log_message 0 \"crashed by signal\"] 0\n        assert_equal [count_log_message 0 \"ASSERTION FAILED\"] 1\n    }\n}\n\ntest {corrupt payload: fuzzer findings - invalid read in ziplistFind} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r debug set-skip-checksum-validation 1\n        catch {\n            r RESTORE key 0 \"\\x0D\\x19\\x19\\x00\\x00\\x00\\x16\\x00\\x00\\x00\\x06\\x00\\x00\\xF1\\x02\\xF1\\x02\\xF2\\x02\\x02\\x5F\\x31\\x04\\x99\\x02\\xF3\\xFF\\x09\\x00\\xC5\\xB8\\x10\\xC0\\x8A\\xF9\\x16\\xDF\"\n            r HEXISTS key -688319650333\n        }\n        assert_equal [count_log_message 0 \"crashed by signal\"] 0\n        assert_equal [count_log_message 0 \"ASSERTION FAILED\"] 1\n    }\n}\n\n\ntest {corrupt payload: fuzzer findings - invalid ziplist encoding} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload yes\n        r debug set-skip-checksum-validation 1\n        catch {\n            r RESTORE _listbig 0 \"\\x0E\\x02\\x1B\\x1B\\x00\\x00\\x00\\x16\\x00\\x00\\x00\\x05\\x00\\x00\\x02\\x5F\\x39\\x04\\xF9\\x02\\x86\\x5F\\x37\\x04\\xF7\\x02\\x02\\x5F\\x35\\xFF\\x19\\x19\\x00\\x00\\x00\\x16\\x00\\x00\\x00\\x05\\x00\\x00\\xF5\\x02\\x02\\x5F\\x33\\x04\\xF3\\x02\\x02\\x5F\\x31\\x04\\xF1\\xFF\\x09\\x00\\x0C\\xFC\\x99\\x2C\\x23\\x45\\x15\\x60\"\n        } err\n        assert_match \"*Bad data format*\" $err\n        verify_log_message 0 \"*integrity check failed*\" 0\n    }\n}\n\ntest {corrupt payload: fuzzer findings - hash crash} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload yes\n        r debug set-skip-checksum-validation 1\n        r RESTORE _hash 0 \"\\x0D\\x19\\x19\\x00\\x00\\x00\\x16\\x00\\x00\\x00\\x06\\x00\\x00\\xF1\\x02\\xF1\\x02\\xF2\\x02\\x02\\x5F\\x31\\x04\\xF3\\x02\\xF3\\xFF\\x09\\x00\\x38\\xB8\\x10\\xC0\\x8A\\xF9\\x16\\xDF\"\n        r HSET _hash 394891450 1635910264\n        r HMGET _hash 887312884855\n    }\n}\n\ntest {corrupt payload: fuzzer findings - uneven entry count in hash} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r debug set-skip-checksum-validation 1\n        r RESTORE _hashbig 0 \"\\x0D\\x3D\\x3D\\x00\\x00\\x00\\x38\\x00\\x00\\x00\\x14\\x00\\x00\\xF2\\x02\\x02\\x5F\\x31\\x04\\x1C\\x02\\xF7\\x02\\xF1\\x02\\xF1\\x02\\xF5\\x02\\xF5\\x02\\xF4\\x02\\x02\\x5F\\x33\\x04\\xF6\\x02\\x02\\x5F\\x35\\x04\\xF8\\x02\\x02\\x5F\\x37\\x04\\xF9\\x02\\xF9\\x02\\xF3\\x02\\xF3\\x02\\xFA\\x02\\x02\\x5F\\x39\\xFF\\x09\\x00\\x73\\xB7\\x68\\xC8\\x97\\x24\\x8E\\x88\"\n        catch { r HSCAN _hashbig -250 }\n        assert_equal [count_log_message 0 \"crashed by signal\"] 0\n        assert_equal [count_log_message 0 \"ASSERTION FAILED\"] 1\n    }\n}\n\ntest {corrupt payload: fuzzer findings - invalid read in lzf_decompress} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r debug set-skip-checksum-validation 1\n        catch { r RESTORE _setbig 0 \"\\x02\\x03\\x02\\x5F\\x31\\xC0\\x02\\xC3\\x00\\x09\\x00\\xE6\\xDC\\x76\\x44\\xFF\\xEB\\x3D\\xFE\" } err\n        assert_match \"*Bad data format*\" $err\n    }\n}\n\ntest {corrupt payload: fuzzer findings - leak in rdbloading due to dup entry in set} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r debug set-skip-checksum-validation 1\n        catch { r RESTORE _setbig 0 \"\\x02\\x0A\\x02\\x5F\\x39\\xC0\\x06\\x02\\x5F\\x31\\xC0\\x00\\xC0\\x04\\x02\\x5F\\x35\\xC0\\x02\\xC0\\x08\\x02\\x5F\\x31\\x02\\x5F\\x33\\x09\\x00\\x7A\\x5A\\xFB\\x90\\x3A\\xE9\\x3C\\xBE\" } err\n        assert_match \"*Bad data format*\" $err\n    }\n}\n\ntest {corrupt payload: fuzzer findings - empty intset} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r debug set-skip-checksum-validation 1\n        catch {r RESTORE _setbig 0 \"\\x02\\xC0\\xC0\\x06\\x02\\x5F\\x39\\xC0\\x02\\x02\\x5F\\x33\\xC0\\x00\\x02\\x5F\\x31\\xC0\\x04\\xC0\\x08\\x02\\x5F\\x37\\x02\\x5F\\x35\\x09\\x00\\xC5\\xD4\\x6D\\xBA\\xAD\\x14\\xB7\\xE7\"} err\n        assert_match \"*Bad data format*\" $err\n        r ping\n    }\n}\n\ntest {corrupt payload: fuzzer findings - valgrind ziplist - crash report prints freed memory} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r debug set-skip-checksum-validation 1\n        r RESTORE _zsetbig 0 \"\\x0C\\x3D\\x3D\\x00\\x00\\x00\\x3A\\x00\\x00\\x00\\x14\\x00\\x00\\xF1\\x02\\xF1\\x02\\x02\\x5F\\x31\\x04\\xF2\\x02\\xF3\\x02\\xF3\\x02\\x02\\x5F\\x33\\x04\\xF4\\x02\\xEE\\x02\\xF5\\x02\\x02\\x5F\\x35\\x04\\xF6\\x02\\xF7\\x02\\xF7\\x02\\x02\\x5F\\x37\\x04\\xF8\\x02\\xF9\\x02\\xF9\\x02\\x02\\x5F\\x39\\x04\\xFA\\xFF\\x09\\x00\\xAE\\xF9\\x77\\x2A\\x47\\x24\\x33\\xF6\"\n        catch { r ZREMRANGEBYSCORE _zsetbig -1050966020 724 }\n        assert_equal [count_log_message 0 \"crashed by signal\"] 0\n        assert_equal [count_log_message 0 \"ASSERTION FAILED\"] 1\n    }\n}\n\ntest {corrupt payload: fuzzer findings - valgrind ziplist prevlen reaches outside the ziplist} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r debug set-skip-checksum-validation 1\n        r RESTORE _listbig 0 \"\\x0E\\x02\\x1B\\x1B\\x00\\x00\\x00\\x16\\x00\\x00\\x00\\x05\\x00\\x00\\x02\\x5F\\x39\\x04\\xF9\\x02\\x02\\x5F\\x37\\x04\\xF7\\x02\\x02\\x5F\\x35\\xFF\\x19\\x19\\x00\\x00\\x00\\x16\\x00\\x00\\x00\\x05\\x00\\x00\\xF5\\x02\\x02\\x5F\\x33\\x04\\xF3\\x95\\x02\\x5F\\x31\\x04\\xF1\\xFF\\x09\\x00\\x0C\\xFC\\x99\\x2C\\x23\\x45\\x15\\x60\"\n        catch { r RPOP _listbig }\n        catch { r RPOP _listbig }\n        catch { r RPUSH _listbig 949682325 }\n        assert_equal [count_log_message 0 \"crashed by signal\"] 0\n        assert_equal [count_log_message 0 \"ASSERTION FAILED\"] 1\n    }\n}\n\ntest {corrupt payload: fuzzer findings - valgrind - bad rdbLoadDoubleValue} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r debug set-skip-checksum-validation 1\n        catch { r RESTORE _list 0 \"\\x03\\x01\\x11\\x11\\x00\\x00\\x00\\x0A\\x00\\x00\\x00\\x01\\x00\\x00\\xD0\\x07\\x1A\\xE9\\x02\\xFF\\x09\\x00\\x1A\\x06\\x07\\x32\\x41\\x28\\x3A\\x46\" } err\n        assert_match \"*Bad data format*\" $err\n    }\n}\n\ntest {corrupt payload: fuzzer findings - valgrind ziplist prev too big} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r debug set-skip-checksum-validation 1\n        r RESTORE _list 0 \"\\x0E\\x01\\x13\\x13\\x00\\x00\\x00\\x10\\x00\\x00\\x00\\x03\\x00\\x00\\xF3\\x02\\x02\\x5F\\x31\\xC1\\xF1\\xFF\\x09\\x00\\xC9\\x4B\\x31\\xFE\\x61\\xC0\\x96\\xFE\"\n        catch { r RPUSHX _list -45 }\n        catch { r LREM _list -748 -840}\n        assert_equal [count_log_message 0 \"crashed by signal\"] 0\n        assert_equal [count_log_message 0 \"ASSERTION FAILED\"] 1\n    }\n}\n\ntest {corrupt payload: fuzzer findings - lzf decompression fails, avoid valgrind invalid read} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r debug set-skip-checksum-validation 1\n        catch {r RESTORE _stream 0 \"\\x0F\\x02\\x10\\x00\\x00\\x01\\x73\\xDD\\xAA\\x2A\\xB9\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\xC3\\x40\\x4B\\x40\\x5C\\x18\\x5C\\x00\\x00\\x00\\x24\\x00\\x05\\x01\\x00\\x01\\x02\\x01\\x84\\x69\\x74\\x65\\x6D\\x05\\x85\\x76\\x61\\x6C\\x75\\x65\\x06\\x40\\x10\\x00\\x00\\x20\\x01\\x00\\x01\\x20\\x03\\x00\\x05\\x20\\x1C\\x40\\x07\\x05\\x01\\x01\\x82\\x5F\\x31\\x03\\x80\\x0D\\x40\\x00\\x00\\x02\\x60\\x19\\x40\\x27\\x40\\x19\\x00\\x33\\x60\\x19\\x40\\x29\\x02\\x01\\x01\\x04\\x20\\x19\\x00\\xFF\\x10\\x00\\x00\\x01\\x73\\xDD\\xAA\\x2A\\xBC\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\xC3\\x40\\x4D\\x40\\x5E\\x18\\x5E\\x00\\x00\\x00\\x24\\x00\\x05\\x01\\x00\\x01\\x02\\x01\\x84\\x69\\x74\\x65\\x6D\\x05\\x85\\x76\\x61\\x6C\\x75\\x65\\x06\\x40\\x10\\x00\\x00\\x20\\x01\\x06\\x01\\x01\\x82\\x5F\\x35\\x03\\x05\\x20\\x1E\\x17\\x0B\\x03\\x01\\x01\\x06\\x01\\x40\\x0B\\x00\\x01\\x60\\x0D\\x02\\x82\\x5F\\x37\\x60\\x19\\x80\\x00\\x00\\x08\\x60\\x19\\x80\\x27\\x02\\x82\\x5F\\x39\\x20\\x19\\x00\\xFF\\x0A\\x81\\x00\\x00\\x01\\x73\\xDD\\xAA\\x2A\\xBE\\x00\\x00\\x09\\x00\\x21\\x85\\x77\\x43\\x71\\x7B\\x17\\x88\"} err\n        assert_match \"*Bad data format*\" $err\n    }\n}\n\ntest {corrupt payload: fuzzer findings - stream bad lp_count} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload yes\n        r debug set-skip-checksum-validation 1\n        catch { r RESTORE _stream 0 \"\\x0F\\x01\\x10\\x00\\x00\\x01\\x73\\xDE\\xDF\\x7D\\x9B\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x40\\x42\\x42\\x00\\x00\\x00\\x18\\x00\\x03\\x01\\x00\\x01\\x02\\x01\\x84\\x69\\x74\\x65\\x6D\\x05\\x85\\x76\\x61\\x6C\\x75\\x65\\x06\\x00\\x01\\x02\\x01\\x00\\x01\\x00\\x01\\x01\\x01\\x00\\x01\\x56\\x01\\x02\\x01\\x22\\x01\\x00\\x01\\x01\\x01\\x82\\x5F\\x31\\x03\\x05\\x01\\x02\\x01\\x2C\\x01\\x00\\x01\\x01\\x01\\x02\\x01\\x05\\x01\\xFF\\x03\\x81\\x00\\x00\\x01\\x73\\xDE\\xDF\\x7D\\xC7\\x00\\x01\\x07\\x6D\\x79\\x67\\x72\\x6F\\x75\\x70\\x81\\x00\\x00\\x01\\x73\\xDE\\xDF\\x7D\\x9B\\x00\\x01\\x00\\x00\\x01\\x73\\xDE\\xDF\\x7D\\x9B\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\xF9\\x7D\\xDF\\xDE\\x73\\x01\\x00\\x00\\x01\\x01\\x05\\x41\\x6C\\x69\\x63\\x65\\xEB\\x7D\\xDF\\xDE\\x73\\x01\\x00\\x00\\x01\\x00\\x00\\x01\\x73\\xDE\\xDF\\x7D\\x9B\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x00\\xB2\\xA8\\xA7\\x5F\\x1B\\x61\\x72\\xD5\"} err\n        assert_match \"*Bad data format*\" $err\n        r ping\n    }\n}\n\ntest {corrupt payload: fuzzer findings - stream bad lp_count - unsanitized} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r debug set-skip-checksum-validation 1\n        r RESTORE _stream 0 \"\\x0F\\x01\\x10\\x00\\x00\\x01\\x73\\xDE\\xDF\\x7D\\x9B\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x40\\x42\\x42\\x00\\x00\\x00\\x18\\x00\\x03\\x01\\x00\\x01\\x02\\x01\\x84\\x69\\x74\\x65\\x6D\\x05\\x85\\x76\\x61\\x6C\\x75\\x65\\x06\\x00\\x01\\x02\\x01\\x00\\x01\\x00\\x01\\x01\\x01\\x00\\x01\\x56\\x01\\x02\\x01\\x22\\x01\\x00\\x01\\x01\\x01\\x82\\x5F\\x31\\x03\\x05\\x01\\x02\\x01\\x2C\\x01\\x00\\x01\\x01\\x01\\x02\\x01\\x05\\x01\\xFF\\x03\\x81\\x00\\x00\\x01\\x73\\xDE\\xDF\\x7D\\xC7\\x00\\x01\\x07\\x6D\\x79\\x67\\x72\\x6F\\x75\\x70\\x81\\x00\\x00\\x01\\x73\\xDE\\xDF\\x7D\\x9B\\x00\\x01\\x00\\x00\\x01\\x73\\xDE\\xDF\\x7D\\x9B\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\xF9\\x7D\\xDF\\xDE\\x73\\x01\\x00\\x00\\x01\\x01\\x05\\x41\\x6C\\x69\\x63\\x65\\xEB\\x7D\\xDF\\xDE\\x73\\x01\\x00\\x00\\x01\\x00\\x00\\x01\\x73\\xDE\\xDF\\x7D\\x9B\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x00\\xB2\\xA8\\xA7\\x5F\\x1B\\x61\\x72\\xD5\"\n        catch { r XREVRANGE _stream 638932639 738}\n        assert_equal [count_log_message 0 \"crashed by signal\"] 0\n        assert_equal [count_log_message 0 \"ASSERTION FAILED\"] 1\n    }\n}\n\ntest {corrupt payload: fuzzer findings - stream integrity check issue} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload yes\n        r debug set-skip-checksum-validation 1\n        catch { r RESTORE _stream 0 \"\\x0F\\x02\\x10\\x00\\x00\\x01\\x75\\x2D\\xA2\\x90\\x67\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\xC3\\x40\\x4F\\x40\\x5C\\x18\\x5C\\x00\\x00\\x00\\x24\\x00\\x05\\x01\\x00\\x01\\x4A\\x01\\x84\\x69\\x74\\x65\\x6D\\x05\\x85\\x76\\x61\\x6C\\x75\\x65\\x06\\x40\\x10\\x00\\x00\\x20\\x01\\x00\\x01\\x20\\x03\\x00\\x05\\x20\\x1C\\x40\\x09\\x05\\x01\\x01\\x82\\x5F\\x31\\x03\\x80\\x0D\\x00\\x02\\x20\\x0D\\x00\\x02\\xA0\\x19\\x00\\x03\\x20\\x0B\\x02\\x82\\x5F\\x33\\xA0\\x19\\x00\\x04\\x20\\x0D\\x00\\x04\\x20\\x19\\x00\\xFF\\x10\\x00\\x00\\x01\\x75\\x2D\\xA2\\x90\\x67\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x05\\xC3\\x40\\x56\\x40\\x60\\x18\\x60\\x00\\x00\\x00\\x24\\x00\\x05\\x01\\x00\\x01\\x02\\x01\\x84\\x69\\x74\\x65\\x6D\\x05\\x85\\x76\\x61\\x6C\\x75\\x65\\x06\\x40\\x10\\x00\\x00\\x20\\x01\\x06\\x01\\x01\\x82\\x5F\\x35\\x03\\x05\\x20\\x1E\\x40\\x0B\\x03\\x01\\x01\\x06\\x01\\x80\\x0B\\x00\\x02\\x20\\x0B\\x02\\x82\\x5F\\x37\\x60\\x19\\x03\\x01\\x01\\xDF\\xFB\\x20\\x05\\x00\\x08\\x60\\x1A\\x20\\x0C\\x00\\xFC\\x20\\x05\\x02\\x82\\x5F\\x39\\x20\\x1B\\x00\\xFF\\x0A\\x81\\x00\\x00\\x01\\x75\\x2D\\xA2\\x90\\x68\\x01\\x00\\x09\\x00\\x1D\\x6F\\xC0\\x69\\x8A\\xDE\\xF7\\x92\" } err\n        assert_match \"*Bad data format*\" $err\n    }\n}\n\ntest {corrupt payload: fuzzer findings - infinite loop} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r debug set-skip-checksum-validation 1\n        r RESTORE _stream 0 \"\\x0F\\x01\\x10\\x00\\x00\\x01\\x75\\x3A\\xA6\\xD0\\x93\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x40\\x42\\x42\\x00\\x00\\x00\\x18\\x00\\x03\\x01\\x00\\x01\\x02\\x01\\x84\\x69\\x74\\x65\\x6D\\x05\\x85\\x76\\x61\\x6C\\x75\\x65\\x06\\x00\\x01\\x02\\x01\\x00\\x01\\x00\\x01\\x01\\x01\\x00\\x01\\x05\\x01\\x02\\x01\\x00\\x01\\x01\\x01\\x01\\x01\\x82\\x5F\\x31\\x03\\xFD\\x01\\x02\\x01\\x00\\x01\\x02\\x01\\x01\\x01\\x02\\x01\\x05\\x01\\xFF\\x03\\x81\\x00\\x00\\x01\\x75\\x3A\\xA6\\xD0\\x93\\x02\\x01\\x07\\x6D\\x79\\x67\\x72\\x6F\\x75\\x70\\x81\\x00\\x00\\x01\\x75\\x3A\\xA6\\xD0\\x93\\x00\\x01\\x00\\x00\\x01\\x75\\x3A\\xA6\\xD0\\x93\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x94\\xD0\\xA6\\x3A\\x75\\x01\\x00\\x00\\x01\\x01\\x05\\x41\\x6C\\x69\\x63\\x65\\x94\\xD0\\xA6\\x3A\\x75\\x01\\x00\\x00\\x01\\x00\\x00\\x01\\x75\\x3A\\xA6\\xD0\\x93\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x00\\xC4\\x09\\xAD\\x69\\x7E\\xEE\\xA6\\x2F\"\n        catch { r XREVRANGE _stream 288270516 971031845 }\n        assert_equal [count_log_message 0 \"crashed by signal\"] 0\n        assert_equal [count_log_message 0 \"ASSERTION FAILED\"] 1\n    }\n}\n\ntest {corrupt payload: fuzzer findings - hash convert asserts on RESTORE with shallow sanitization} {\n    # if we don't perform full sanitization, and the next command can assert on converting\n    # a ziplist to hash records, then we're ok with that happning in RESTORE too\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r debug set-skip-checksum-validation 1\n        catch { r RESTORE _hash 0 \"\\x0D\\x3D\\x3D\\x00\\x00\\x00\\x3A\\x00\\x00\\x00\\x14\\x13\\x00\\xF5\\x02\\xF5\\x02\\xF2\\x02\\x53\\x5F\\x31\\x04\\xF3\\x02\\xF3\\x02\\xF7\\x02\\xF7\\x02\\xF8\\x02\\x02\\x5F\\x37\\x04\\xF1\\x02\\xF1\\x02\\xF6\\x02\\x02\\x5F\\x35\\x04\\xF4\\x02\\x02\\x5F\\x33\\x04\\xFA\\x02\\x02\\x5F\\x39\\x04\\xF9\\x02\\xF9\\xFF\\x09\\x00\\xB5\\x48\\xDE\\x62\\x31\\xD0\\xE5\\x63\" }\n        assert_equal [count_log_message 0 \"crashed by signal\"] 0\n        assert_equal [count_log_message 0 \"ASSERTION FAILED\"] 1\n    }\n}\n\ntest {corrupt payload: OOM in rdbGenericLoadStringObject} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        catch { r RESTORE x 0 \"\\x0A\\x81\\x7F\\xFF\\xFF\\xFF\\xFF\\xFF\\xFF\\xFF\\x13\\x00\\x00\\x00\\x0E\\x00\\x00\\x00\\x02\\x00\\x00\\x02\\x61\\x00\\x04\\x02\\x62\\x00\\xFF\\x09\\x00\\x57\\x04\\xE5\\xCD\\xD4\\x37\\x6C\\x57\" } err\n        assert_match \"*Bad data format*\" $err\n        r ping\n    }\n}\n\ntest {corrupt payload: fuzzer findings - OOM in dictExpand} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r debug set-skip-checksum-validation 1\n        catch { r RESTORE x 0 \"\\x02\\x81\\x02\\x5F\\x31\\xC0\\x00\\xC0\\x02\\x09\\x00\\xCD\\x84\\x2C\\xB7\\xE8\\xA4\\x49\\x57\" } err\n        assert_match \"*Bad data format*\" $err\n        r ping\n    }\n}\n\ntest {corrupt payload: fuzzer findings - invalid tail offset after removal} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r debug set-skip-checksum-validation 1\n        r RESTORE _zset 0 \"\\x0C\\x19\\x19\\x00\\x00\\x00\\x02\\x00\\x00\\x00\\x06\\x00\\x00\\xF1\\x02\\xF1\\x02\\x02\\x5F\\x31\\x04\\xF2\\x02\\xF3\\x02\\xF3\\xFF\\x09\\x00\\x4D\\x72\\x7B\\x97\\xCD\\x9A\\x70\\xC1\"\n        catch {r ZPOPMIN _zset}\n        catch {r ZPOPMAX _zset}\n        assert_equal [count_log_message 0 \"crashed by signal\"] 0\n        assert_equal [count_log_message 0 \"ASSERTION FAILED\"] 1\n    }\n}\n\ntest {corrupt payload: fuzzer findings - negative reply length} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r debug set-skip-checksum-validation 1\n        r RESTORE _stream 0 \"\\x0F\\x01\\x10\\x00\\x00\\x01\\x75\\xCF\\xA1\\x16\\xA7\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x40\\x42\\x42\\x00\\x00\\x00\\x18\\x00\\x03\\x01\\x00\\x01\\x02\\x01\\x84\\x69\\x74\\x65\\x6D\\x05\\x85\\x76\\x61\\x6C\\x75\\x65\\x06\\x00\\x01\\x02\\x01\\x00\\x01\\x00\\x01\\x01\\x01\\x00\\x01\\x05\\x01\\x02\\x01\\x00\\x01\\x01\\x01\\x01\\x01\\x14\\x5F\\x31\\x03\\x05\\x01\\x02\\x01\\x00\\x01\\x02\\x01\\x01\\x01\\x02\\x01\\x05\\x01\\xFF\\x03\\x81\\x00\\x00\\x01\\x75\\xCF\\xA1\\x16\\xA7\\x02\\x01\\x07\\x6D\\x79\\x67\\x72\\x6F\\x75\\x70\\x81\\x00\\x00\\x01\\x75\\xCF\\xA1\\x16\\xA7\\x01\\x01\\x00\\x00\\x01\\x75\\xCF\\xA1\\x16\\xA7\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x01\\xA7\\x16\\xA1\\xCF\\x75\\x01\\x00\\x00\\x01\\x01\\x05\\x41\\x6C\\x69\\x63\\x65\\xA7\\x16\\xA1\\xCF\\x75\\x01\\x00\\x00\\x01\\x00\\x00\\x01\\x75\\xCF\\xA1\\x16\\xA7\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x01\\x09\\x00\\x1B\\x42\\x52\\xB8\\xDD\\x5C\\xE5\\x4E\"\n        catch {r XADD _stream * -956 -2601503852}\n        catch {r XINFO STREAM _stream FULL}\n        assert_equal [count_log_message 0 \"crashed by signal\"] 0\n        assert_equal [count_log_message 0 \"ASSERTION FAILED\"] 1\n    }\n}\n\ntest {corrupt payload: fuzzer findings - valgrind negative malloc} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload yes\n        r debug set-skip-checksum-validation 1\n        catch {r RESTORE _key 0 \"\\x0E\\x01\\x81\\xD6\\xD6\\x00\\x00\\x00\\x0A\\x00\\x00\\x00\\x01\\x00\\x00\\x40\\xC8\\x6F\\x2F\\x36\\xE2\\xDF\\xE3\\x2E\\x26\\x64\\x8B\\x87\\xD1\\x7A\\xBD\\xFF\\xEF\\xEF\\x63\\x65\\xF6\\xF8\\x8C\\x4E\\xEC\\x96\\x89\\x56\\x88\\xF8\\x3D\\x96\\x5A\\x32\\xBD\\xD1\\x36\\xD8\\x02\\xE6\\x66\\x37\\xCB\\x34\\x34\\xC4\\x52\\xA7\\x2A\\xD5\\x6F\\x2F\\x7E\\xEE\\xA2\\x94\\xD9\\xEB\\xA9\\x09\\x38\\x3B\\xE1\\xA9\\x60\\xB6\\x4E\\x09\\x44\\x1F\\x70\\x24\\xAA\\x47\\xA8\\x6E\\x30\\xE1\\x13\\x49\\x4E\\xA1\\x92\\xC4\\x6C\\xF0\\x35\\x83\\xD9\\x4F\\xD9\\x9C\\x0A\\x0D\\x7A\\xE7\\xB1\\x61\\xF5\\xC1\\x2D\\xDC\\xC3\\x0E\\x87\\xA6\\x80\\x15\\x18\\xBA\\x7F\\x72\\xDD\\x14\\x75\\x46\\x44\\x0B\\xCA\\x9C\\x8F\\x1C\\x3C\\xD7\\xDA\\x06\\x62\\x18\\x7E\\x15\\x17\\x24\\xAB\\x45\\x21\\x27\\xC2\\xBC\\xBB\\x86\\x6E\\xD8\\xBD\\x8E\\x50\\xE0\\xE0\\x88\\xA4\\x9B\\x9D\\x15\\x2A\\x98\\xFF\\x5E\\x78\\x6C\\x81\\xFC\\xA8\\xC9\\xC8\\xE6\\x61\\xC8\\xD1\\x4A\\x7F\\x81\\xD6\\xA6\\x1A\\xAD\\x4C\\xC1\\xA2\\x1C\\x90\\x68\\x15\\x2A\\x8A\\x36\\xC0\\x58\\xC3\\xCC\\xA6\\x54\\x19\\x12\\x0F\\xEB\\x46\\xFF\\x6E\\xE3\\xA7\\x92\\xF8\\xFF\\x09\\x00\\xD0\\x71\\xF7\\x9F\\xF7\\x6A\\xD6\\x2E\"} err\n        assert_match \"*Bad data format*\" $err\n        r ping\n    }\n}\n\ntest {corrupt payload: fuzzer findings - valgrind invalid read} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload yes\n        r debug set-skip-checksum-validation 1\n        catch {r RESTORE _key 0 \"\\x05\\x0A\\x02\\x5F\\x39\\x00\\x00\\x00\\x00\\x00\\x00\\x22\\x40\\xC0\\x08\\x00\\x00\\x00\\x00\\x00\\x00\\x20\\x40\\x02\\x5F\\x37\\x00\\x00\\x00\\x00\\x00\\x00\\x1C\\x40\\xC0\\x06\\x00\\x00\\x00\\x00\\x00\\x00\\x18\\x40\\x02\\x5F\\x33\\x00\\x00\\x00\\x00\\x00\\x00\\x14\\x40\\xC0\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x10\\x40\\x02\\x5F\\x33\\x00\\x00\\x00\\x00\\x00\\x00\\x08\\x40\\xC0\\x02\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x40\\x02\\x5F\\x31\\x00\\x00\\x00\\x00\\x00\\x00\\xF0\\x3F\\xC0\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x00\\x3C\\x66\\xD7\\x14\\xA9\\xDA\\x3C\\x69\"} err\n        assert_match \"*Bad data format*\" $err\n        r ping\n    }\n}\n\ntest {corrupt payload: fuzzer findings - empty hash ziplist} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload yes\n        r debug set-skip-checksum-validation 1\n        catch {r RESTORE _int 0 \"\\x04\\xC0\\x01\\x09\\x00\\xF6\\x8A\\xB6\\x7A\\x85\\x87\\x72\\x4D\"} err\n        assert_match \"*Bad data format*\" $err\n        r ping\n    }\n}\n\ntest {corrupt payload: fuzzer findings - stream with no records} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r debug set-skip-checksum-validation 1\n        r restore _stream 0 \"\\x0F\\x01\\x10\\x00\\x00\\x01\\x78\\x4D\\x55\\x68\\x09\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x40\\x42\\x42\\x00\\x00\\x00\\x18\\x00\\x02\\x01\\x01\\x01\\x02\\x01\\x84\\x69\\x74\\x65\\x6D\\x05\\x85\\x76\\x61\\x6C\\x75\\x65\\x06\\x00\\x01\\x02\\x01\\x00\\x01\\x00\\x01\\x01\\x01\\x00\\x01\\x05\\x01\\x03\\x01\\x3E\\x01\\x00\\x01\\x01\\x01\\x82\\x5F\\x31\\x03\\x05\\x01\\x02\\x01\\x50\\x01\\x00\\x01\\x01\\x01\\x02\\x01\\x05\\x23\\xFF\\x02\\x81\\x00\\x00\\x01\\x78\\x4D\\x55\\x68\\x59\\x00\\x01\\x07\\x6D\\x79\\x67\\x72\\x6F\\x75\\x70\\x81\\x00\\x00\\x01\\x78\\x4D\\x55\\x68\\x47\\x00\\x01\\x00\\x00\\x01\\x78\\x4D\\x55\\x68\\x47\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x9F\\x68\\x55\\x4D\\x78\\x01\\x00\\x00\\x01\\x01\\x05\\x41\\x6C\\x69\\x63\\x65\\x85\\x68\\x55\\x4D\\x78\\x01\\x00\\x00\\x01\\x00\\x00\\x01\\x78\\x4D\\x55\\x68\\x47\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x00\\xF1\\xC0\\x72\\x70\\x39\\x40\\x1E\\xA9\" replace\n        catch {r XREAD STREAMS _stream $}\n        assert_equal [count_log_message 0 \"crashed by signal\"] 0\n        assert_equal [count_log_message 0 \"Guru Meditation\"] 1\n    }\n}\n\ntest {corrupt payload: fuzzer findings - quicklist ziplist tail followed by extra data which start with 0xff} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload yes\n        r debug set-skip-checksum-validation 1\n        catch {\n            r restore key 0 \"\\x0E\\x01\\x11\\x11\\x00\\x00\\x00\\x0A\\x00\\x00\\x00\\x01\\x00\\x00\\xF6\\xFF\\xB0\\x6C\\x9C\\xFF\\x09\\x00\\x9C\\x37\\x47\\x49\\x4D\\xDE\\x94\\xF5\" replace\n        } err\n        assert_match \"*Bad data format*\" $err\n        verify_log_message 0 \"*integrity check failed*\" 0\n    }\n}\n\ntest {corrupt payload: fuzzer findings - dict init to huge size} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r debug set-skip-checksum-validation 1\n        catch {r restore key 0 \"\\x02\\x81\\xC0\\x00\\x02\\x5F\\x31\\xC0\\x02\\x09\\x00\\xB2\\x1B\\xE5\\x17\\x2E\\x15\\xF4\\x6C\" replace} err\n        assert_match \"*Bad data format*\" $err\n        r ping\n    }\n}\n\ntest {corrupt payload: fuzzer findings - huge string} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload yes\n        r debug set-skip-checksum-validation 1\n        catch {r restore key 0 \"\\x00\\x81\\x01\\x09\\x00\\xF6\\x2B\\xB6\\x7A\\x85\\x87\\x72\\x4D\"} err\n        assert_match \"*Bad data format*\" $err\n        r ping\n    }\n}\n\ntest {corrupt payload: fuzzer findings - stream PEL without consumer} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload yes\n        r debug set-skip-checksum-validation 1\n        catch {r restore _stream 0 \"\\x0F\\x01\\x10\\x00\\x00\\x01\\x7B\\x08\\xF0\\xB2\\x34\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\xC3\\x3B\\x40\\x42\\x19\\x42\\x00\\x00\\x00\\x18\\x00\\x02\\x01\\x01\\x01\\x02\\x01\\x84\\x69\\x74\\x65\\x6D\\x05\\x85\\x76\\x61\\x6C\\x75\\x65\\x06\\x00\\x20\\x10\\x00\\x00\\x20\\x01\\x00\\x01\\x20\\x03\\x02\\x05\\x01\\x03\\x20\\x05\\x40\\x00\\x04\\x82\\x5F\\x31\\x03\\x05\\x60\\x19\\x80\\x32\\x02\\x05\\x01\\xFF\\x02\\x81\\x00\\x00\\x01\\x7B\\x08\\xF0\\xB2\\x34\\x02\\x01\\x07\\x6D\\x79\\x67\\x72\\x6F\\x75\\x70\\x81\\x00\\x00\\x01\\x7B\\x08\\xF0\\xB2\\x34\\x01\\x01\\x00\\x00\\x01\\x7B\\x08\\xF0\\xB2\\x34\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x01\\x35\\xB2\\xF0\\x08\\x7B\\x01\\x00\\x00\\x01\\x01\\x13\\x41\\x6C\\x69\\x63\\x65\\x35\\xB2\\xF0\\x08\\x7B\\x01\\x00\\x00\\x01\\x00\\x00\\x01\\x7B\\x08\\xF0\\xB2\\x34\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x01\\x09\\x00\\x28\\x2F\\xE0\\xC5\\x04\\xBB\\xA7\\x31\"} err\n        assert_match \"*Bad data format*\" $err\n        #catch {r XINFO STREAM _stream FULL }\n        r ping\n    }\n}\n\ntest {corrupt payload: fuzzer findings - stream listpack valgrind issue} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r debug set-skip-checksum-validation 1\n        r restore _stream 0 \"\\x0F\\x01\\x10\\x00\\x00\\x01\\x7B\\x09\\x5E\\x94\\xFF\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x40\\x42\\x42\\x00\\x00\\x00\\x18\\x00\\x02\\x01\\x01\\x01\\x02\\x01\\x84\\x69\\x74\\x65\\x6D\\x05\\x85\\x76\\x61\\x6C\\x75\\x65\\x06\\x00\\x01\\x02\\x01\\x00\\x01\\x00\\x01\\x01\\x01\\x00\\x01\\x05\\x01\\x03\\x01\\x25\\x01\\x00\\x01\\x01\\x01\\x82\\x5F\\x31\\x03\\x05\\x01\\x02\\x01\\x32\\x01\\x00\\x01\\x01\\x01\\x02\\x01\\xF0\\x01\\xFF\\x02\\x81\\x00\\x00\\x01\\x7B\\x09\\x5E\\x95\\x31\\x00\\x01\\x07\\x6D\\x79\\x67\\x72\\x6F\\x75\\x70\\x81\\x00\\x00\\x01\\x7B\\x09\\x5E\\x95\\x24\\x00\\x01\\x00\\x00\\x01\\x7B\\x09\\x5E\\x95\\x24\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x5C\\x95\\x5E\\x09\\x7B\\x01\\x00\\x00\\x01\\x01\\x05\\x41\\x6C\\x69\\x63\\x65\\x4B\\x95\\x5E\\x09\\x7B\\x01\\x00\\x00\\x01\\x00\\x00\\x01\\x7B\\x09\\x5E\\x95\\x24\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x00\\x19\\x29\\x94\\xDF\\x76\\xF8\\x1A\\xC6\"\n        catch {r XINFO STREAM _stream FULL }\n        assert_equal [count_log_message 0 \"crashed by signal\"] 0\n        assert_equal [count_log_message 0 \"ASSERTION FAILED\"] 1\n    }\n}\n\ntest {corrupt payload: fuzzer findings - stream with bad lpFirst} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload yes\n        r debug set-skip-checksum-validation 1\n        catch {r restore _stream 0 \"\\x0F\\x01\\x10\\x00\\x00\\x01\\x7B\\x0E\\x52\\xD2\\xEC\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x40\\x42\\x42\\x00\\x00\\x00\\x18\\x00\\x02\\xF7\\x01\\x01\\x02\\x01\\x84\\x69\\x74\\x65\\x6D\\x05\\x85\\x76\\x61\\x6C\\x75\\x65\\x06\\x00\\x01\\x02\\x01\\x00\\x01\\x00\\x01\\x01\\x01\\x00\\x01\\x05\\x01\\x03\\x01\\x01\\x01\\x00\\x01\\x01\\x01\\x82\\x5F\\x31\\x03\\x05\\x01\\x02\\x01\\x01\\x01\\x01\\x01\\x01\\x01\\x02\\x01\\x05\\x01\\xFF\\x02\\x81\\x00\\x00\\x01\\x7B\\x0E\\x52\\xD2\\xED\\x01\\x01\\x07\\x6D\\x79\\x67\\x72\\x6F\\x75\\x70\\x81\\x00\\x00\\x01\\x7B\\x0E\\x52\\xD2\\xED\\x00\\x01\\x00\\x00\\x01\\x7B\\x0E\\x52\\xD2\\xED\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\xED\\xD2\\x52\\x0E\\x7B\\x01\\x00\\x00\\x01\\x01\\x05\\x41\\x6C\\x69\\x63\\x65\\xED\\xD2\\x52\\x0E\\x7B\\x01\\x00\\x00\\x01\\x00\\x00\\x01\\x7B\\x0E\\x52\\xD2\\xED\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x00\\xAC\\x05\\xC9\\x97\\x5D\\x45\\x80\\xB3\"} err\n        assert_match \"*Bad data format*\" $err\n        r ping\n    }\n}\n\ntest {corrupt payload: fuzzer findings - stream listpack lpPrev valgrind issue} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload no\n        r debug set-skip-checksum-validation 1\n        r restore _stream 0  \"\\x0F\\x01\\x10\\x00\\x00\\x01\\x7B\\x0E\\xAE\\x66\\x36\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x40\\x42\\x42\\x00\\x00\\x00\\x18\\x00\\x02\\x01\\x01\\x01\\x02\\x01\\x84\\x69\\x74\\x65\\x6D\\x05\\x85\\x76\\x61\\x6C\\x75\\x65\\x06\\x00\\x01\\x02\\x01\\x00\\x01\\x00\\x01\\x01\\x01\\x00\\x01\\x1D\\x01\\x03\\x01\\x24\\x01\\x00\\x01\\x01\\x69\\x82\\x5F\\x31\\x03\\x05\\x01\\x02\\x01\\x33\\x01\\x00\\x01\\x01\\x01\\x02\\x01\\x05\\x01\\xFF\\x02\\x81\\x00\\x00\\x01\\x7B\\x0E\\xAE\\x66\\x69\\x00\\x01\\x07\\x6D\\x79\\x67\\x72\\x6F\\x75\\x70\\x81\\x00\\x00\\x01\\x7B\\x0E\\xAE\\x66\\x5A\\x00\\x01\\x00\\x00\\x01\\x7B\\x0E\\xAE\\x66\\x5A\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x94\\x66\\xAE\\x0E\\x7B\\x01\\x00\\x00\\x01\\x01\\x05\\x41\\x6C\\x69\\x63\\x65\\x83\\x66\\xAE\\x0E\\x7B\\x01\\x00\\x00\\x01\\x00\\x00\\x01\\x7B\\x0E\\xAE\\x66\\x5A\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x09\\x00\\xD5\\xD7\\xA5\\x5C\\x63\\x1C\\x09\\x40\"\n        catch {r XREVRANGE _stream 1618622681 606195012389}\n        assert_equal [count_log_message 0 \"crashed by signal\"] 0\n        assert_equal [count_log_message 0 \"ASSERTION FAILED\"] 1\n    }\n}\n\ntest {corrupt payload: fuzzer findings - stream with non-integer entry id} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload yes\n        r debug set-skip-checksum-validation 1\n        catch {r restore _streambig 0 \"\\x0F\\x03\\x10\\x00\\x00\\x01\\x7B\\x13\\x34\\xC3\\xB2\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\xC3\\x40\\x4F\\x40\\x5C\\x18\\x5C\\x00\\x00\\x00\\x24\\x00\\x05\\x01\\x00\\x01\\x02\\x01\\x84\\x69\\x74\\x65\\x6D\\x05\\x85\\x76\\x61\\x6C\\x75\\x65\\x06\\x40\\x10\\x00\\x80\\x20\\x01\\x00\\x01\\x20\\x03\\x00\\x05\\x20\\x1C\\x40\\x09\\x05\\x01\\x01\\x82\\x5F\\x31\\x03\\x80\\x0D\\x00\\x02\\x20\\x0D\\x00\\x02\\xA0\\x19\\x00\\x03\\x20\\x0B\\x02\\x82\\x5F\\x33\\xA0\\x19\\x00\\x04\\x20\\x0D\\x00\\x04\\x20\\x19\\x00\\xFF\\x10\\x00\\x00\\x01\\x7B\\x13\\x34\\xC3\\xB2\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x05\\xC3\\x40\\x56\\x40\\x61\\x18\\x61\\x00\\x00\\x00\\x24\\x00\\x05\\x01\\x00\\x01\\x02\\x01\\x84\\x69\\x74\\x65\\x6D\\x05\\x85\\x76\\x61\\x6C\\x75\\x65\\x06\\x40\\x10\\x00\\x00\\x20\\x01\\x06\\x01\\x01\\x82\\x5F\\x35\\x03\\x05\\x20\\x1E\\x40\\x0B\\x03\\x01\\x01\\x06\\x01\\x40\\x0B\\x03\\x01\\x01\\xDF\\xFB\\x20\\x05\\x02\\x82\\x5F\\x37\\x60\\x1A\\x20\\x0E\\x00\\xFC\\x20\\x05\\x00\\x08\\xC0\\x1B\\x00\\xFD\\x20\\x0C\\x02\\x82\\x5F\\x39\\x20\\x1B\\x00\\xFF\\x10\\x00\\x00\\x01\\x7B\\x13\\x34\\xC3\\xB3\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x03\\xC3\\x3D\\x40\\x4A\\x18\\x4A\\x00\\x00\\x00\\x15\\x00\\x02\\x01\\x00\\x01\\x02\\x01\\x84\\x69\\x74\\x65\\x6D\\x05\\x85\\x76\\x61\\x6C\\x75\\x65\\x06\\x40\\x10\\x00\\x00\\x20\\x01\\x40\\x00\\x00\\x05\\x60\\x07\\x02\\xDF\\xFD\\x02\\xC0\\x23\\x09\\x01\\x01\\x86\\x75\\x6E\\x69\\x71\\x75\\x65\\x07\\xA0\\x2D\\x02\\x08\\x01\\xFF\\x0C\\x81\\x00\\x00\\x01\\x7B\\x13\\x34\\xC3\\xB4\\x00\\x00\\x09\\x00\\x9D\\xBD\\xD5\\xB9\\x33\\xC4\\xC5\\xFF\"} err\n        #catch {r XINFO STREAM _streambig FULL }\n        assert_match \"*Bad data format*\" $err\n        r ping\n    }\n}\n\ntest {corrupt payload: fuzzer findings - empty quicklist} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload yes\n        r debug set-skip-checksum-validation 1\n        catch {\n            r restore key 0 \"\\x0E\\xC0\\x2B\\x15\\x00\\x00\\x00\\x0A\\x00\\x00\\x00\\x01\\x00\\x00\\xE0\\x62\\x58\\xEA\\xDF\\x22\\x00\\x00\\x00\\xFF\\x09\\x00\\xDF\\x35\\xD2\\x67\\xDC\\x0E\\x89\\xAB\" replace\n        } err\n        assert_match \"*Bad data format*\" $err\n        r ping\n    }\n}\n\ntest {corrupt payload: fuzzer findings - empty zset} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload yes\n        r debug set-skip-checksum-validation 1\n        catch {r restore key 0 \"\\x05\\xC0\\x01\\x09\\x00\\xF6\\x8A\\xB6\\x7A\\x85\\x87\\x72\\x4D\"} err\n        assert_match \"*Bad data format*\" $err\n        r ping\n    }\n}\n\ntest {corrupt payload: fuzzer findings - hash with len of 0} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r config set sanitize-dump-payload yes\n        r debug set-skip-checksum-validation 1\n        catch {r restore key 0 \"\\x04\\xC0\\x21\\x09\\x00\\xF6\\x8A\\xB6\\x7A\\x85\\x87\\x72\\x4D\"} err\n        assert_match \"*Bad data format*\" $err\n        r ping\n    }\n}\n\ntest {corrupt payload: fuzzer findings - stream double free listpack when insert dup node to rax returns 0} {\n    start_server [list overrides [list loglevel verbose use-exit-on-panic yes crash-memcheck-enabled no] ] {\n        r debug set-skip-checksum-validation 1\n        r config set sanitize-dump-payload yes\n        catch { r restore _stream 0 \"\\x0F\\x03\\x10\\x00\\x00\\x01\\x7B\\x60\\x5A\\x23\\x79\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\xC3\\x40\\x4F\\x40\\x5C\\x18\\x5C\\x00\\x00\\x00\\x24\\x00\\x05\\x01\\x00\\x01\\x02\\x01\\x84\\x69\\x74\\x65\\x6D\\x05\\x85\\x76\\x61\\x6C\\x75\\x65\\x06\\x40\\x10\\x00\\x00\\x20\\x01\\x00\\x01\\x20\\x03\\x00\\x05\\x20\\x1C\\x40\\x09\\x05\\x01\\x01\\x82\\x5F\\x31\\x03\\x80\\x0D\\x00\\x02\\x20\\x0D\\x00\\x02\\xA0\\x19\\x00\\x03\\x20\\x0B\\x02\\x82\\x5F\\x33\\xA0\\x19\\x00\\x04\\x20\\x0D\\x00\\x04\\x20\\x19\\x00\\xFF\\x10\\x00\\x00\\x01\\x7B\\x60\\x5A\\x23\\x79\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x05\\xC3\\x40\\x51\\x40\\x5E\\x18\\x5E\\x00\\x00\\x00\\x24\\x00\\x05\\x01\\x00\\x01\\x02\\x01\\x84\\x69\\x74\\x65\\x6D\\x05\\x85\\x76\\x61\\x6C\\x75\\x65\\x06\\x40\\x10\\x00\\x00\\x20\\x01\\x06\\x01\\x01\\x82\\x5F\\x35\\x03\\x05\\x20\\x1E\\x40\\x0B\\x03\\x01\\x01\\x06\\x01\\x80\\x0B\\x00\\x02\\x20\\x0B\\x02\\x82\\x5F\\x37\\xA0\\x19\\x00\\x03\\x20\\x0D\\x00\\x08\\xA0\\x19\\x00\\x04\\x20\\x0B\\x02\\x82\\x5F\\x39\\x20\\x19\\x00\\xFF\\x10\\x00\\x00\\x01\\x7B\\x60\\x5A\\x23\\x79\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\xC3\\x3B\\x40\\x49\\x18\\x49\\x00\\x00\\x00\\x15\\x00\\x02\\x01\\x00\\x01\\x02\\x01\\x84\\x69\\x74\\x65\\x6D\\x05\\x85\\x76\\x61\\x6C\\x75\\x65\\x06\\x40\\x10\\x00\\x00\\x20\\x01\\x40\\x00\\x00\\x05\\x20\\x07\\x40\\x09\\xC0\\x22\\x09\\x01\\x01\\x86\\x75\\x6E\\x69\\x71\\x75\\x65\\x07\\xA0\\x2C\\x02\\x08\\x01\\xFF\\x0C\\x81\\x00\\x00\\x01\\x7B\\x60\\x5A\\x23\\x7A\\x01\\x00\\x09\\x00\\x9C\\x8F\\x1E\\xBF\\x2E\\x05\\x59\\x09\" replace } err\n        assert_match \"*Bad data format*\" $err\n        r ping\n    }\n}\n\n} ;# tags\n\n"
  },
  {
    "path": "tests/integration/failover.tcl",
    "content": "start_server {tags {\"failover\"}} {\nstart_server {} {\nstart_server {} {\n    set node_0 [srv 0 client]\n    set node_0_host [srv 0 host]\n    set node_0_port [srv 0 port]\n    set node_0_pid [srv 0 pid]\n\n    set node_1 [srv -1 client]\n    set node_1_host [srv -1 host]\n    set node_1_port [srv -1 port]\n    set node_1_pid [srv -1 pid]\n\n    set node_2 [srv -2 client]\n    set node_2_host [srv -2 host]\n    set node_2_port [srv -2 port]\n    set node_2_pid [srv -2 pid]\n\n    proc assert_digests_match {n1 n2 n3} {\n        assert_equal [$n1 debug digest] [$n2 debug digest]\n        assert_equal [$n2 debug digest] [$n3 debug digest]\n    }\n\n    test {failover command fails without connected replica} {\n        catch { $node_0 failover to $node_1_host $node_1_port } err\n        if {! [string match \"ERR*\" $err]} {\n            fail \"failover command succeeded when replica not connected\"\n        }\n    }\n\n    test {setup replication for following tests} {\n        $node_1 replicaof $node_0_host $node_0_port\n        $node_2 replicaof $node_0_host $node_0_port\n        wait_for_sync $node_1\n        wait_for_sync $node_2\n    }\n\n    test {failover command fails with invalid host} {\n        catch { $node_0 failover to invalidhost $node_1_port } err\n        assert_match \"ERR*\" $err\n    }\n\n    test {failover command fails with invalid port} {\n        catch { $node_0 failover to $node_1_host invalidport } err\n        assert_match \"ERR*\" $err\n    }\n\n    test {failover command fails with just force and timeout} {\n        catch { $node_0 FAILOVER FORCE TIMEOUT 100} err\n        assert_match \"ERR*\" $err\n    }\n\n    test {failover command fails when sent to a replica} {\n        catch { $node_1 failover to $node_1_host $node_1_port } err\n        assert_match \"ERR*\" $err\n    }\n\n    test {failover command fails with force without timeout} {\n        catch { $node_0 failover to $node_1_host $node_1_port FORCE } err\n        assert_match \"ERR*\" $err\n    }\n\n    test {failover command to specific replica works} {\n        set initial_psyncs [s -1 sync_partial_ok]\n        set initial_syncs [s -1 sync_full]\n\n        # Generate a delta between primary and replica\n        set load_handler [start_write_load $node_0_host $node_0_port 5]\n        exec kill -SIGSTOP [srv -1 pid]\n        wait_for_condition 50 100 {\n            [s 0 total_commands_processed] > 100\n        } else {\n            fail \"Node 0 did not accept writes\"\n        }\n        exec kill -SIGCONT [srv -1 pid]\n\n        # Execute the failover\n        $node_0 failover to $node_1_host $node_1_port\n\n        # Wait for failover to end\n        wait_for_condition 50 100 {\n            [s 0 master_failover_state] == \"no-failover\"\n        } else {\n            fail \"Failover from node 0 to node 1 did not finish\"\n        }\n\n        # stop the write load and make sure no more commands processed\n        stop_write_load $load_handler\n        wait_load_handlers_disconnected\n\n        $node_2 replicaof $node_1_host $node_1_port\n        wait_for_sync $node_0\n        wait_for_sync $node_2\n\n        assert_match *slave* [$node_0 role]\n        assert_match *master* [$node_1 role]\n        assert_match *slave* [$node_2 role]\n\n        # We should accept psyncs from both nodes\n        assert_equal [expr [s -1 sync_partial_ok] - $initial_psyncs] 2\n        assert_equal [expr [s -1 sync_full] - $initial_psyncs] 0\n        assert_digests_match $node_0 $node_1 $node_2\n    }\n\n    test {failover command to any replica works} {\n        set initial_psyncs [s -2 sync_partial_ok]\n        set initial_syncs [s -2 sync_full]\n\n        wait_for_ofs_sync $node_1 $node_2\n        # We stop node 0 to and make sure node 2 is selected\n        exec kill -SIGSTOP $node_0_pid\n        $node_1 set CASE 1\n        $node_1 FAILOVER\n\n        # Wait for failover to end\n        wait_for_condition 50 100 {\n            [s -1 master_failover_state] == \"no-failover\"\n        } else {\n            fail \"Failover from node 1 to node 2 did not finish\"\n        }\n        exec kill -SIGCONT $node_0_pid\n        $node_0 replicaof $node_2_host $node_2_port\n\n        wait_for_sync $node_0\n        wait_for_sync $node_1\n\n        assert_match *slave* [$node_0 role]\n        assert_match *slave* [$node_1 role]\n        assert_match *master* [$node_2 role]\n\n        # We should accept Psyncs from both nodes\n        assert_equal [expr [s -2 sync_partial_ok] - $initial_psyncs] 2\n        assert_equal [expr [s -1 sync_full] - $initial_psyncs] 0\n        assert_digests_match $node_0 $node_1 $node_2\n    }\n\n    test {failover to a replica with force works} {\n        set initial_psyncs [s 0 sync_partial_ok]\n        set initial_syncs [s 0 sync_full]\n\n        exec kill -SIGSTOP $node_0_pid\n        # node 0 will never acknowledge this write\n        $node_2 set case 2\n        $node_2 failover to $node_0_host $node_0_port TIMEOUT 100 FORCE\n\n        # Wait for node 0 to give up on sync attempt and start failover\n        wait_for_condition 50 100 {\n            [s -2 master_failover_state] == \"failover-in-progress\"\n        } else {\n            fail \"Failover from node 2 to node 0 did not timeout\"\n        }\n\n        # Quick check that everyone is a replica, we never want a \n        # state where there are two masters.\n        assert_match *slave* [$node_1 role]\n        assert_match *slave* [$node_2 role]\n\n        exec kill -SIGCONT $node_0_pid\n\n        # Wait for failover to end\n        wait_for_condition 50 100 {\n            [s -2 master_failover_state] == \"no-failover\"\n        } else {\n            fail \"Failover from node 2 to node 0 did not finish\"\n        }\n        $node_1 replicaof $node_0_host $node_0_port\n\n        wait_for_sync $node_1\n        wait_for_sync $node_2\n\n        assert_match *master* [$node_0 role]\n        assert_match *slave* [$node_1 role]\n        assert_match *slave* [$node_2 role]\n\n        assert_equal [count_log_message -2 \"time out exceeded, failing over.\"] 1\n\n        # We should accept both psyncs, although this is the condition we might not\n        # since we didn't catch up.\n        assert_equal [expr [s 0 sync_partial_ok] - $initial_psyncs] 2\n        assert_equal [expr [s 0 sync_full] - $initial_syncs] 0\n        assert_digests_match $node_0 $node_1 $node_2\n    }\n\n    test {failover with timeout aborts if replica never catches up} {\n        set initial_psyncs [s 0 sync_partial_ok]\n        set initial_syncs [s 0 sync_full]\n\n        # Stop replica so it never catches up\n        exec kill -SIGSTOP [srv -1 pid]\n        $node_0 SET CASE 1\n        \n        $node_0 failover to [srv -1 host] [srv -1 port] TIMEOUT 500\n        # Wait for failover to end\n        wait_for_condition 50 20 {\n            [s 0 master_failover_state] == \"no-failover\"\n        } else {\n            fail \"Failover from node_0 to replica did not finish\"\n        }\n\n        exec kill -SIGCONT [srv -1 pid]\n\n        # We need to make sure the nodes actually sync back up\n        wait_for_ofs_sync $node_0 $node_1\n        wait_for_ofs_sync $node_0 $node_2\n\n        assert_match *master* [$node_0 role]\n        assert_match *slave* [$node_1 role]\n        assert_match *slave* [$node_2 role]\n\n        # Since we never caught up, there should be no syncs\n        assert_equal [expr [s 0 sync_partial_ok] - $initial_psyncs] 0\n        assert_equal [expr [s 0 sync_full] - $initial_syncs] 0\n        assert_digests_match $node_0 $node_1 $node_2\n    }\n\n    test {failovers can be aborted} {\n        set initial_psyncs [s 0 sync_partial_ok]\n        set initial_syncs [s 0 sync_full]\n    \n        # Stop replica so it never catches up\n        exec kill -SIGSTOP [srv -1 pid]\n        $node_0 SET CASE 2\n        \n        $node_0 failover to [srv -1 host] [srv -1 port] TIMEOUT 60000\n        assert_match [s 0 master_failover_state] \"waiting-for-sync\"\n\n        # Sanity check that read commands are still accepted\n        $node_0 GET CASE\n\n        $node_0 failover abort\n        assert_match [s 0 master_failover_state] \"no-failover\"\n\n        exec kill -SIGCONT [srv -1 pid]\n\n        # Just make sure everything is still synced\n        wait_for_ofs_sync $node_0 $node_1\n        wait_for_ofs_sync $node_0 $node_2\n\n        assert_match *master* [$node_0 role]\n        assert_match *slave* [$node_1 role]\n        assert_match *slave* [$node_2 role]\n\n        # Since we never caught up, there should be no syncs\n        assert_equal [expr [s 0 sync_partial_ok] - $initial_psyncs] 0\n        assert_equal [expr [s 0 sync_full] - $initial_syncs] 0\n        assert_digests_match $node_0 $node_1 $node_2\n    }\n\n    test {failover aborts if target rejects sync request} {\n        set initial_psyncs [s 0 sync_partial_ok]\n        set initial_syncs [s 0 sync_full]\n\n        # We block psync, so the failover will fail\n        $node_1 acl setuser default -psync\n\n        # We pause the target long enough to send a write command\n        # during the pause. This write will not be interrupted.\n        exec kill -SIGSTOP [srv -1 pid]\n        set rd [redis_deferring_client]\n        $rd SET FOO BAR\n        $node_0 failover to $node_1_host $node_1_port\n        exec kill -SIGCONT [srv -1 pid]\n\n        # Wait for failover to end\n        wait_for_condition 50 100 {\n            [s 0 master_failover_state] == \"no-failover\"\n        } else {\n            fail \"Failover from node_0 to replica did not finish\"\n        }\n\n        assert_equal [$rd read] \"OK\"\n        $rd close\n\n        # restore access to psync\n        $node_1 acl setuser default +psync\n\n        # We need to make sure the nodes actually sync back up\n        wait_for_sync $node_1\n        wait_for_sync $node_2\n\n        assert_match *master* [$node_0 role]\n        assert_match *slave* [$node_1 role]\n        assert_match *slave* [$node_2 role]\n\n        # We will cycle all of our replicas here and force a psync.\n        after 100\n        assert_equal [expr [s 0 sync_partial_ok] - $initial_psyncs] 2\n        assert_equal [expr [s 0 sync_full] - $initial_syncs] 0\n\n        assert_equal [count_log_message 0 \"Failover target rejected psync request\"] 1\n        assert_digests_match $node_0 $node_1 $node_2\n    }\n}\n}\n}\n"
  },
  {
    "path": "tests/integration/keydb-benchmark.tcl",
    "content": "source tests/support/benchmark.tcl\n\n\nproc cmdstat {cmd} {\n    return [cmdrstat $cmd r]\n}\n\nstart_server {tags {\"benchmark network\"}} {\n    start_server {} {\n        set master_host [srv 0 host]\n        set master_port [srv 0 port]\n\n        test {benchmark: set,get} {\n            r config resetstat\n            r flushall\n            set cmd [redisbenchmark $master_host $master_port \"-c 5 -n 10 -e -t set,get\"]\n            if {[catch { exec {*}$cmd } error]} {\n                set first_line [lindex [split $error \"\\n\"] 0]\n                puts [colorstr red \"keydb-benchmark non zero code. first line: $first_line\"]\n                fail \"keydb-benchmark non zero code. first line: $first_line\"\n            }\n            assert_match  {*calls=10,*} [cmdstat set]\n            assert_match  {*calls=10,*} [cmdstat get]\n            # assert one of the non benchmarked commands is not present\n            assert_match  {} [cmdstat lrange]\n        }\n\n        test {benchmark: full test suite} {\n            r config resetstat\n            set cmd [redisbenchmark $master_host $master_port \"-c 10 -n 100 -e\"]\n            if {[catch { exec {*}$cmd } error]} {\n                set first_line [lindex [split $error \"\\n\"] 0]\n                puts [colorstr red \"keydb-benchmark non zero code. first line: $first_line\"]\n                fail \"keydb-benchmark non zero code. first line: $first_line\"\n            }\n            # ping total calls are 2*issued commands per test due to PING_INLINE and PING_MBULK\n            assert_match  {*calls=200,*} [cmdstat ping]\n            assert_match  {*calls=100,*} [cmdstat set]\n            assert_match  {*calls=100,*} [cmdstat get]\n            assert_match  {*calls=100,*} [cmdstat incr]\n            # lpush total calls are 2*issued commands per test due to the lrange tests\n            assert_match  {*calls=200,*} [cmdstat lpush]\n            assert_match  {*calls=100,*} [cmdstat rpush]\n            assert_match  {*calls=100,*} [cmdstat lpop]\n            assert_match  {*calls=100,*} [cmdstat rpop]\n            assert_match  {*calls=100,*} [cmdstat sadd]\n            assert_match  {*calls=100,*} [cmdstat hset]\n            assert_match  {*calls=100,*} [cmdstat spop]\n            assert_match  {*calls=100,*} [cmdstat zadd]\n            assert_match  {*calls=100,*} [cmdstat zpopmin]\n            assert_match  {*calls=400,*} [cmdstat lrange]\n            assert_match  {*calls=100,*} [cmdstat mset]\n            # assert one of the non benchmarked commands is not present\n            assert_match {} [cmdstat rpoplpush]\n        }\n\n        test {benchmark: multi-thread set,get} {\n            r config resetstat\n            r flushall\n            set cmd [redisbenchmark $master_host $master_port \"--threads 10 -c 5 -n 10 -e -t set,get\"]\n            if {[catch { exec {*}$cmd } error]} {\n                set first_line [lindex [split $error \"\\n\"] 0]\n                puts [colorstr red \"keydb-benchmark non zero code. first line: $first_line\"]\n                fail \"keydb-benchmark non zero code. first line: $first_line\"\n            }\n            assert_match  {*calls=10,*} [cmdstat set]\n            assert_match  {*calls=10,*} [cmdstat get]\n            # assert one of the non benchmarked commands is not present\n            assert_match  {} [cmdstat lrange]\n\n            # ensure only one key was populated\n            assert_match  {1} [scan [regexp -inline {keys\\=([\\d]*)} [r info keyspace]] keys=%d]\n        }\n\n        test {benchmark: pipelined full set,get} {\n            r config resetstat\n            r flushall\n            set cmd [redisbenchmark $master_host $master_port \"-P 5 -c 10 -n 10010 -e -t set,get\"]\n            if {[catch { exec {*}$cmd } error]} {\n                set first_line [lindex [split $error \"\\n\"] 0]\n                puts [colorstr red \"keydb-benchmark non zero code. first line: $first_line\"]\n                fail \"keydb-benchmark non zero code. first line: $first_line\"\n            }\n            assert_match  {*calls=10010,*} [cmdstat set]\n            assert_match  {*calls=10010,*} [cmdstat get]\n            # assert one of the non benchmarked commands is not present\n            assert_match  {} [cmdstat lrange]\n\n            # ensure only one key was populated\n            assert_match  {1} [scan [regexp -inline {keys\\=([\\d]*)} [r info keyspace]] keys=%d]\n        }\n\n        test {benchmark: arbitrary command} {\n            r config resetstat\n            r flushall\n            set cmd [redisbenchmark $master_host $master_port \"-c 5 -n 150 -e INCRBYFLOAT mykey 10.0\"]\n            if {[catch { exec {*}$cmd } error]} {\n                set first_line [lindex [split $error \"\\n\"] 0]\n                puts [colorstr red \"keydb-benchmark non zero code. first line: $first_line\"]\n                fail \"keydb-benchmark non zero code. first line: $first_line\"\n            }\n            assert_match  {*calls=150,*} [cmdstat incrbyfloat]\n            # assert one of the non benchmarked commands is not present\n            assert_match  {} [cmdstat get]\n\n            # ensure only one key was populated\n            assert_match  {1} [scan [regexp -inline {keys\\=([\\d]*)} [r info keyspace]] keys=%d]\n        }\n\n        test {benchmark: keyspace length} {\n            r flushall\n            r config resetstat\n            set cmd [redisbenchmark $master_host $master_port \"-r 50 -t set -n 1000\"]\n            if {[catch { exec {*}$cmd } error]} {\n                set first_line [lindex [split $error \"\\n\"] 0]\n                puts [colorstr red \"keydb-benchmark non zero code. first line: $first_line\"]\n                fail \"keydb-benchmark non zero code. first line: $first_line\"\n            }\n            assert_match  {*calls=1000,*} [cmdstat set]\n            # assert one of the non benchmarked commands is not present\n            assert_match  {} [cmdstat get]\n\n            # ensure the keyspace has the desired size\n            assert_match  {50} [scan [regexp -inline {keys\\=([\\d]*)} [r info keyspace]] keys=%d]\n        }\n\n        # tls specific tests\n        if {$::tls} {\n            test {benchmark: specific tls-ciphers} {\n                r flushall\n                r config resetstat\n                set cmd [redisbenchmark $master_host $master_port \"-r 50 -t set -n 1000 --tls-ciphers \\\"DEFAULT:-AES128-SHA256\\\"\"]\n                if {[catch { exec {*}$cmd } error]} {\n                    set first_line [lindex [split $error \"\\n\"] 0]\n                    puts [colorstr red \"keydb-benchmark non zero code. first line: $first_line\"]\n                    fail \"keydb-benchmark non zero code. first line: $first_line\"\n                }\n                assert_match  {*calls=1000,*} [cmdstat set]\n                # assert one of the non benchmarked commands is not present\n                assert_match  {} [cmdstat get]\n            }\n\n            test {benchmark: specific tls-ciphersuites} {\n                r flushall\n                r config resetstat\n                set ciphersuites_supported 1\n                set cmd [redisbenchmark $master_host $master_port \"-r 50 -t set -n 1000 --tls-ciphersuites \\\"TLS_AES_128_GCM_SHA256\\\"\"]\n                if {[catch { exec {*}$cmd } error]} {\n                    set first_line [lindex [split $error \"\\n\"] 0]\n                    if {[string match \"*Invalid option*\" $first_line]} {\n                        set ciphersuites_supported 0\n                        if {$::verbose} {\n                            puts \"Skipping test, TLSv1.3 not supported.\"\n                        }\n                    } else {\n                        puts [colorstr red \"keydb-benchmark non zero code. first line: $first_line\"]\n                        fail \"keydb-benchmark non zero code. first line: $first_line\"\n                    }\n                }\n                if {$ciphersuites_supported} {\n                    assert_match  {*calls=1000,*} [cmdstat set]\n                    # assert one of the non benchmarked commands is not present\n                    assert_match  {} [cmdstat get]\n                }\n            }\n        }\n    }\n}\n"
  },
  {
    "path": "tests/integration/keydb-cli.tcl",
    "content": "source tests/support/cli.tcl\n\nstart_server [list tags {\"cli\"} overrides {enable-async-commands no}] {\n    proc open_cli {{opts \"-n 9\"} {infile \"\"}} {\n        set ::env(TERM) dumb\n        set cmdline [rediscli [srv host] [srv port] $opts]\n        if {$infile ne \"\"} {\n            set cmdline \"$cmdline < $infile\"\n            set mode \"r\"\n        } else {\n            set mode \"r+\"\n        }\n        set fd [open \"|$cmdline\" $mode]\n        fconfigure $fd -buffering none\n        fconfigure $fd -blocking false\n        fconfigure $fd -translation binary\n        set _ $fd\n    }\n\n    proc close_cli {fd} {\n        close $fd\n    }\n\n    proc read_cli {fd} {\n        set buf [read $fd]\n        while {[string length $buf] == 0} {\n            # wait some time and try again\n            after 10\n            set buf [read $fd]\n        }\n        set _ $buf\n    }\n\n    proc write_cli {fd buf} {\n        puts $fd $buf\n        flush $fd\n    }\n\n    # Helpers to run tests in interactive mode\n\n    proc format_output {output} {\n        set _ [string trimright [regsub -all \"\\r\" $output \"\"] \"\\n\"]\n    }\n\n    proc run_command {fd cmd} {\n        write_cli $fd $cmd\n        set _ [format_output [read_cli $fd]]\n    }\n\n    proc test_interactive_cli {name code} {\n        set ::env(FAKETTY) 1\n        set fd [open_cli]\n        test \"Interactive CLI: $name\" $code\n        close_cli $fd\n        unset ::env(FAKETTY)\n    }\n\n    # Helpers to run tests where stdout is not a tty\n    proc write_tmpfile {contents} {\n        set tmp [tmpfile \"cli\"]\n        set tmpfd [open $tmp \"w\"]\n        puts -nonewline $tmpfd $contents\n        close $tmpfd\n        set _ $tmp\n    }\n\n    proc _run_cli {host port db opts args} {\n        set cmd [rediscli $host $port [list -n $db {*}$args]]\n        foreach {key value} $opts {\n            if {$key eq \"pipe\"} {\n                set cmd \"sh -c \\\"$value | $cmd\\\"\"\n            }\n            if {$key eq \"path\"} {\n                set cmd \"$cmd < $value\"\n            }\n        }\n\n        set fd [open \"|$cmd\" \"r\"]\n        fconfigure $fd -buffering none\n        fconfigure $fd -translation binary\n        set resp [read $fd 1048576]\n        close $fd\n        set _ [format_output $resp]\n    }\n\n    proc run_cli {args} {\n        _run_cli [srv host] [srv port] 9 {} {*}$args\n    }\n\n    proc run_cli_with_input_pipe {cmd args} {\n        _run_cli [srv host] [srv port] 9 [list pipe $cmd] -x {*}$args\n    }\n\n    proc run_cli_with_input_file {path args} {\n        _run_cli [srv host] [srv port] 9 [list path $path] -x {*}$args\n    }\n\n    proc run_cli_host_port_db {host port db args} {\n        _run_cli $host $port $db {} {*}$args\n    }\n\n    proc test_nontty_cli {name code} {\n        test \"Non-interactive non-TTY CLI: $name\" $code\n    }\n\n    # Helpers to run tests where stdout is a tty (fake it)\n    proc test_tty_cli {name code} {\n        set ::env(FAKETTY) 1\n        test \"Non-interactive TTY CLI: $name\" $code\n        unset ::env(FAKETTY)\n    }\n\n    test_interactive_cli \"INFO response should be printed raw\" {\n        set lines [split [run_command $fd info] \"\\n\"]\n        foreach line $lines {\n            assert [regexp {^$|^#|^[^#:]+:} $line]\n        }\n    }\n\n    test_interactive_cli \"Status reply\" {\n        assert_equal \"OK\" [run_command $fd \"set key foo\"]\n    }\n\n    test_interactive_cli \"Integer reply\" {\n        assert_equal \"(integer) 1\" [run_command $fd \"incr counter\"]\n    }\n\n    test_interactive_cli \"Bulk reply\" {\n        r set key foo\n        assert_equal \"\\\"foo\\\"\" [run_command $fd \"get key\"]\n    }\n\n    test_interactive_cli \"Multi-bulk reply\" {\n        r rpush list foo\n        r rpush list bar\n        assert_equal \"1) \\\"foo\\\"\\n2) \\\"bar\\\"\" [run_command $fd \"lrange list 0 -1\"]\n    }\n\n    test_interactive_cli \"Parsing quotes\" {\n        assert_equal \"OK\" [run_command $fd \"set key \\\"bar\\\"\"]\n        assert_equal \"bar\" [r get key]\n        assert_equal \"OK\" [run_command $fd \"set key \\\" bar \\\"\"]\n        assert_equal \" bar \" [r get key]\n        assert_equal \"OK\" [run_command $fd \"set key \\\"\\\\\\\"bar\\\\\\\"\\\"\"]\n        assert_equal \"\\\"bar\\\"\" [r get key]\n        assert_equal \"OK\" [run_command $fd \"set key \\\"\\tbar\\t\\\"\"]\n        assert_equal \"\\tbar\\t\" [r get key]\n\n        # invalid quotation\n        assert_equal \"Invalid argument(s)\" [run_command $fd \"get \\\"\\\"key\"]\n        assert_equal \"Invalid argument(s)\" [run_command $fd \"get \\\"key\\\"x\"]\n\n        # quotes after the argument are weird, but should be allowed\n        assert_equal \"OK\" [run_command $fd \"set key\\\"\\\" bar\"]\n        assert_equal \"bar\" [r get key]\n    }\n\n    test_tty_cli \"Status reply\" {\n        assert_equal \"OK\" [run_cli set key bar]\n        assert_equal \"bar\" [r get key]\n    }\n\n    test_tty_cli \"Integer reply\" {\n        r del counter\n        assert_equal \"(integer) 1\" [run_cli incr counter]\n    }\n\n    test_tty_cli \"Bulk reply\" {\n        r set key \"tab\\tnewline\\n\"\n        assert_equal \"\\\"tab\\\\tnewline\\\\n\\\"\" [run_cli get key]\n    }\n\n    test_tty_cli \"Multi-bulk reply\" {\n        r del list\n        r rpush list foo\n        r rpush list bar\n        assert_equal \"1) \\\"foo\\\"\\n2) \\\"bar\\\"\" [run_cli lrange list 0 -1]\n    }\n\n    test_tty_cli \"Read last argument from pipe\" {\n        assert_equal \"OK\" [run_cli_with_input_pipe \"echo foo\" set key]\n        assert_equal \"foo\\n\" [r get key]\n    }\n\n    test_tty_cli \"Read last argument from file\" {\n        set tmpfile [write_tmpfile \"from file\"]\n        assert_equal \"OK\" [run_cli_with_input_file $tmpfile set key]\n        assert_equal \"from file\" [r get key]\n        file delete $tmpfile\n    }\n\n    test_nontty_cli \"Status reply\" {\n        assert_equal \"OK\" [run_cli set key bar]\n        assert_equal \"bar\" [r get key]\n    }\n\n    test_nontty_cli \"Integer reply\" {\n        r del counter\n        assert_equal \"1\" [run_cli incr counter]\n    }\n\n    test_nontty_cli \"Bulk reply\" {\n        r set key \"tab\\tnewline\\n\"\n        assert_equal \"tab\\tnewline\" [run_cli get key]\n    }\n\n    test_nontty_cli \"Multi-bulk reply\" {\n        r del list\n        r rpush list foo\n        r rpush list bar\n        assert_equal \"foo\\nbar\" [run_cli lrange list 0 -1]\n    }\n\nif {!$::tls} { ;# fake_redis_node doesn't support TLS\n    test_nontty_cli \"ASK redirect test\" {\n        # Set up two fake Redis nodes.\n        set tclsh [info nameofexecutable]\n        set script \"tests/helpers/fake_redis_node.tcl\"\n        set port1 [find_available_port $::baseport $::portcount]\n        set port2 [find_available_port $::baseport $::portcount]\n        set p1 [exec $tclsh $script $port1 \\\n                \"SET foo bar\" \"-ASK 12182 127.0.0.1:$port2\" &]\n        set p2 [exec $tclsh $script $port2 \\\n                \"ASKING\" \"+OK\" \\\n                \"SET foo bar\" \"+OK\" &]\n        # Make sure both fake nodes have started listening\n        wait_for_condition 50 50 {\n            [catch {close [socket \"127.0.0.1\" $port1]}] == 0 && \\\n            [catch {close [socket \"127.0.0.1\" $port2]}] == 0\n        } else {\n            fail \"Failed to start fake Redis nodes\"\n        }\n        # Run the cli\n        assert_equal \"OK\" [run_cli_host_port_db \"127.0.0.1\" $port1 0 -c SET foo bar]\n    }\n}\n\n    test_nontty_cli \"Quoted input arguments\" {\n        r set \"\\x00\\x00\" \"value\"\n        assert_equal \"value\" [run_cli --quoted-input get {\"\\x00\\x00\"}]\n    }\n\n    test_nontty_cli \"No accidental unquoting of input arguments\" {\n        run_cli --quoted-input set {\"\\x41\\x41\"} quoted-val\n        run_cli set {\"\\x41\\x41\"} unquoted-val\n        assert_equal \"quoted-val\" [r get AA]\n        assert_equal \"unquoted-val\" [r get {\"\\x41\\x41\"}]\n    }\n\n    test_nontty_cli \"Invalid quoted input arguments\" {\n        catch {run_cli --quoted-input set {\"Unterminated}} err\n        assert_match {*exited abnormally*} $err\n\n        # A single arg that unquotes to two arguments is also not expected\n        catch {run_cli --quoted-input set {\"arg1\" \"arg2\"}} err\n        assert_match {*exited abnormally*} $err\n    }\n\n    test_nontty_cli \"Read last argument from pipe\" {\n        assert_equal \"OK\" [run_cli_with_input_pipe \"echo foo\" set key]\n        assert_equal \"foo\\n\" [r get key]\n    }\n\n    test_nontty_cli \"Read last argument from file\" {\n        set tmpfile [write_tmpfile \"from file\"]\n        assert_equal \"OK\" [run_cli_with_input_file $tmpfile set key]\n        assert_equal \"from file\" [r get key]\n        file delete $tmpfile\n    }\n\n    proc test_redis_cli_rdb_dump {} {\n        r flushdb\n\n        set dir [lindex [r config get dir] 1]\n\n        assert_equal \"OK\" [r debug populate 100000 key 1000]\n        catch {run_cli --rdb \"$dir/cli.rdb\"} output\n        assert_match {*Transfer finished with success*} $output\n\n        file delete \"$dir/dump.rdb\"\n        file rename \"$dir/cli.rdb\" \"$dir/dump.rdb\"\n\n        assert_equal \"OK\" [r set should-not-exist 1]\n        assert_equal \"OK\" [r debug reload nosave]\n        assert_equal {} [r get should-not-exist]\n    }\n\n    test \"Dumping an RDB\" {\n        # Disk-based master\n        assert_match \"OK\" [r config set repl-diskless-sync no]\n        test_redis_cli_rdb_dump\n\n        # Disk-less master\n        assert_match \"OK\" [r config set repl-diskless-sync yes]\n        assert_match \"OK\" [r config set repl-diskless-sync-delay 0]\n        test_redis_cli_rdb_dump\n    }\n\n    test \"Scan mode\" {\n        r flushdb\n        populate 1000 key: 1\n\n        # basic use\n        assert_equal 1000 [llength [split [run_cli --scan]]]\n\n        # pattern\n        assert_equal {key:2} [run_cli --scan --pattern \"*:2\"]\n\n        # pattern matching with a quoted string\n        assert_equal {key:2} [run_cli --scan --quoted-pattern {\"*:\\x32\"}]\n    }\n\n    proc test_redis_cli_repl {} {\n        set fd [open_cli \"--replica\"]\n        wait_for_condition 500 100 {\n            [string match {*slave0:*state=online*} [r info]]\n        } else {\n            fail \"redis-cli --replica did not connect\"\n        }\n\n        for {set i 0} {$i < 100} {incr i} {\n           r set test-key test-value-$i\n        }\n\n        wait_for_condition 500 100 {\n            [string match {*test-value-99*} [read_cli $fd]]\n        } else {\n            fail \"redis-cli --replica didn't read commands\"\n        }\n\n        fconfigure $fd -blocking true\n        r client kill type slave\n        catch { close_cli $fd } err\n        assert_match {*Server closed the connection*} $err\n    }\n\n    test \"Connecting as a replica\" {\n        # Disk-based master\n        assert_match \"OK\" [r config set repl-diskless-sync no]\n        test_redis_cli_repl\n\n        # Disk-less master\n        assert_match \"OK\" [r config set repl-diskless-sync yes]\n        assert_match \"OK\" [r config set repl-diskless-sync-delay 0]\n        test_redis_cli_repl\n    } {}\n\n    test \"Piping raw protocol\" {\n        set cmds [tmpfile \"cli_cmds\"]\n        set cmds_fd [open $cmds \"w\"]\n\n        puts $cmds_fd [formatCommand select 9]\n        puts $cmds_fd [formatCommand del test-counter]\n\n        for {set i 0} {$i < 1000} {incr i} {\n            puts $cmds_fd [formatCommand incr test-counter]\n            puts $cmds_fd [formatCommand set large-key [string repeat \"x\" 20000]]\n        }\n\n        for {set i 0} {$i < 100} {incr i} {\n            puts $cmds_fd [formatCommand set very-large-key [string repeat \"x\" 512000]]\n        }\n        close $cmds_fd\n\n        set cli_fd [open_cli \"--pipe\" $cmds]\n        fconfigure $cli_fd -blocking true\n        set output [read_cli $cli_fd]\n\n        assert_equal {1000} [r get test-counter]\n        assert_match {*All data transferred*errors: 0*replies: 2102*} $output\n\n        file delete $cmds\n    }\n}\n"
  },
  {
    "path": "tests/integration/multimaster-psync.tcl",
    "content": "start_server {tags {\"repl\"} overrides {active-replica {yes} multi-master {yes}}} {\n    start_server {overrides {active-replica {yes} multi-master {yes}}} {\n        test {2 node cluster heals after multimaster psync} {\n            set master [srv -1 client]\n            set master_host [srv -1 host]\n            set master_port [srv -1 port]\n            set replica [srv 0 client]\n            set replica_host [srv 0 host]\n            set replica_port [srv 0 port]\n\n            # connect two nodes in active-active\n            $replica replicaof $master_host $master_port\n            $master replicaof $replica_host $replica_port\n            after 1000\n\n            # write to db7 in the master\n            $master select 7\n            $master set x 1\n\n            # restart the replica to break the connection and force a psync\n            restart_server 0 true false\n            set replica [srv 0 client]\n\n            # write again to db7\n            $master set y 2\n\n            # uncommenting the following delay causes test to pass\n            # after 1000\n\n            # reconnect the replica to the master\n            $replica replicaof $master_host $master_port\n\n            # verify results\n            after 1000\n            for {set j 0} {$j < 16} {incr j} {\n                $master select $j\n                $replica select $j\n                assert_equal [$replica get x] [$master get x] \n                assert_equal [$replica get y] [$master get y]\n            }\n        }\n    }\n}"
  },
  {
    "path": "tests/integration/psync2-reg-multimaster.tcl",
    "content": "# Issue 3899 regression test.\n# We create a chain of three instances: master -> replica -> replica2\n# and continuously break the link while traffic is generated by\n# keydb-benchmark. At the end we check that the data is the same\n# everywhere.\n\nstart_server {tags {\"psync2\"} overrides {active-replica {yes} multi-master {yes} client-output-buffer-limit {replica 200mb 10mb 999999} } } {\nstart_server {overrides {active-replica {yes} multi-master {yes} client-output-buffer-limit {replica 200mb 10mb 999999} } } {\nstart_server {overrides {active-replica {yes} multi-master {yes} client-output-buffer-limit {replica 200mb 10mb 999999} } } {\n    # Config\n    set debug_msg 0                 ; # Enable additional debug messages\n\n    set no_exit 0                   ; # Do not exit at end of the test\n\n    set duration 20                 ; # Total test seconds\n\n    for {set j 0} {$j < 3} {incr j} {\n        set R($j) [srv [expr 0-$j] client]\n        set R_host($j) [srv [expr 0-$j] host]\n        set R_port($j) [srv [expr 0-$j] port]\n        set R_unixsocket($j) [srv [expr 0-$j] unixsocket]\n        if {$debug_msg} {puts \"Log file: [srv [expr 0-$j] stdout]\"}\n    }\n\n    # Setup the replication and backlog parameters\n    test \"PSYNC2 #3899 regression: setup\" {\n        $R(0) replicaof $R_host(1) $R_port(1)\n        $R(0) replicaof $R_host(2) $R_port(2)\n        $R(1) replicaof $R_host(0) $R_port(0)\n        $R(1) replicaof $R_host(2) $R_port(2)\n        $R(2) replicaof $R_host(0) $R_port(0)\n        $R(2) replicaof $R_host(1) $R_port(1)\n\n        $R(0) set foo bar\n        wait_for_condition 50 1000 {\n            [status $R(1) master_link_status] == \"up\" &&\n            [status $R(2) master_link_status] == \"up\" &&\n            [$R(1) dbsize] == 1 &&\n            [$R(2) dbsize] == 1\n        } else {\n            fail \"Replicas not replicating from master\"\n        }\n\n        $R(0) config set repl-backlog-size 200mb\n        $R(1) config set repl-backlog-size 200mb\n        $R(2) config set repl-backlog-size 200mb\n\n    }\n\n    set cycle_start_time [clock milliseconds]\n    set bench_pid [exec src/keydb-benchmark -s $R_unixsocket(0) -n 10000000 -r 1000 incr __rand_int__ > /dev/null &]\n    while 1 {\n        set elapsed [expr {[clock milliseconds]-$cycle_start_time}]\n        if {$elapsed > $duration*1000} break\n        if {rand() < .05} {\n            test \"PSYNC2 #3899 regression (multi-master): kill first replica\" {\n                $R(1) client kill type master\n            }\n        }\n        if {rand() < .05} {\n            test \"PSYNC2 #3899 regression (multi-master): kill chained replica\" {\n                $R(2) client kill type master\n            }\n        }\n        after 100\n    }\n    exec kill -9 $bench_pid\n\n    if {$debug_msg} {\n        for {set j 0} {$j < 100} {incr j} {\n            if {\n                [$R(0) debug digest] == [$R(1) debug digest] &&\n                [$R(1) debug digest] == [$R(2) debug digest]\n            } break\n            puts [$R(0) debug digest]\n            puts [$R(1) debug digest]\n            puts [$R(2) debug digest]\n            after 1000\n        }\n    }\n\n    test \"PSYNC2 #3899 regression: verify consistency\" {\n        wait_for_condition 50 1000 {\n            ([$R(0) debug digest] eq [$R(1) debug digest]) &&\n            ([$R(1) debug digest] eq [$R(2) debug digest])\n        } else {\n            set csv3 [csvdump {r -2}]\n            set csv2 [csvdump {r -1}]\n            set csv1 [csvdump r]\n            set fd [open /tmp/repldump1.txt w]\n            puts -nonewline $fd $csv1\n            close $fd\n            set fd [open /tmp/repldump2.txt w]\n            puts -nonewline $fd $csv2\n            close $fd\n            set fd [open /tmp/repldump3.txt w]\n            puts -nonewline $fd $csv3\n            close $fd\n            fail [format \"The three instances have different data sets:\\n\\tnode 1: %s\\n\\tnode 2: %s\\n\\tnode 3: %s\\nRun diff -u against /tmp/repldump*.txt for more info\" [$R(0) debug digest] [$R(1) debug digest] [$R(2) debug digest] ]\n        }\n    }\n\n    assert {[s -2 sync_partial_ok] > 0}\n    assert {[s -1 sync_partial_ok] > 0}\n    assert {[s 0 sync_partial_ok] > 0}\n}}}\n"
  },
  {
    "path": "tests/integration/psync2-reg.tcl",
    "content": "# Issue 3899 regression test.\n# We create a chain of three instances: master -> slave -> slave2\n# and continuously break the link while traffic is generated by\n# keydb-benchmark. At the end we check that the data is the same\n# everywhere.\n\nstart_server {tags {\"psync2\"}} {\nstart_server {} {\nstart_server {} {\n    # Config\n    set debug_msg 0                 ; # Enable additional debug messages\n\n    set no_exit 0                   ; # Do not exit at end of the test\n\n    set duration 20                 ; # Total test seconds\n\n    for {set j 0} {$j < 3} {incr j} {\n        set R($j) [srv [expr 0-$j] client]\n        set R_host($j) [srv [expr 0-$j] host]\n        set R_port($j) [srv [expr 0-$j] port]\n        set R_unixsocket($j) [srv [expr 0-$j] unixsocket]\n        if {$debug_msg} {puts \"Log file: [srv [expr 0-$j] stdout]\"}\n    }\n\n    # Setup the replication and backlog parameters\n    test \"PSYNC2 #3899 regression: setup\" {\n        $R(1) slaveof $R_host(0) $R_port(0)\n        $R(2) slaveof $R_host(0) $R_port(0)\n        $R(0) set foo bar\n        wait_for_condition 50 1000 {\n            [status $R(1) master_link_status] == \"up\" &&\n            [status $R(2) master_link_status] == \"up\" &&\n            [$R(1) dbsize] == 1 &&\n            [$R(2) dbsize] == 1\n        } else {\n            fail \"Replicas not replicating from master\"\n        }\n        $R(0) config set repl-backlog-size 10mb\n        $R(1) config set repl-backlog-size 10mb\n    }\n\n    set cycle_start_time [clock milliseconds]\n    set bench_pid [exec src/keydb-benchmark -s $R_unixsocket(0) -n 10000000 -r 1000 incr __rand_int__ > /dev/null &]\n    while 1 {\n        set elapsed [expr {[clock milliseconds]-$cycle_start_time}]\n        if {$elapsed > $duration*1000} break\n        if {rand() < .05} {\n            test \"PSYNC2 #3899 regression: kill first replica\" {\n                $R(1) client kill type master\n            }\n        }\n        if {rand() < .05} {\n            test \"PSYNC2 #3899 regression: kill chained replica\" {\n                $R(2) client kill type master\n            }\n        }\n        after 100\n    }\n    exec kill -9 $bench_pid\n\n    if {$debug_msg} {\n        for {set j 0} {$j < 100} {incr j} {\n            if {\n                [$R(0) debug digest] == [$R(1) debug digest] &&\n                [$R(1) debug digest] == [$R(2) debug digest]\n            } break\n            puts [$R(0) debug digest]\n            puts [$R(1) debug digest]\n            puts [$R(2) debug digest]\n            after 1000\n        }\n    }\n\n    test \"PSYNC2 #3899 regression: verify consistency\" {\n        wait_for_condition 50 1000 {\n            ([$R(0) debug digest] eq [$R(1) debug digest]) &&\n            ([$R(1) debug digest] eq [$R(2) debug digest])\n        } else {\n            fail \"The three instances have different data sets\"\n        }\n    }\n}}}\n"
  },
  {
    "path": "tests/integration/rdb.tcl",
    "content": "tags {\"rdb\"} {\n\nset server_path [tmpdir \"server.rdb-encoding-test\"]\n\n# Copy RDB with different encodings in server path\nexec cp tests/assets/encodings.rdb $server_path\n\nstart_server [list overrides [list \"dir\" $server_path \"dbfilename\" \"encodings.rdb\"]] {\n  test \"RDB encoding loading test\" {\n    r select 0\n    csvdump r\n  } {\"0\",\"compressible\",\"string\",\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n\"0\",\"hash\",\"hash\",\"a\",\"1\",\"aa\",\"10\",\"aaa\",\"100\",\"b\",\"2\",\"bb\",\"20\",\"bbb\",\"200\",\"c\",\"3\",\"cc\",\"30\",\"ccc\",\"300\",\"ddd\",\"400\",\"eee\",\"5000000000\",\n\"0\",\"hash_zipped\",\"hash\",\"a\",\"1\",\"b\",\"2\",\"c\",\"3\",\n\"0\",\"list\",\"list\",\"1\",\"2\",\"3\",\"a\",\"b\",\"c\",\"100000\",\"6000000000\",\"1\",\"2\",\"3\",\"a\",\"b\",\"c\",\"100000\",\"6000000000\",\"1\",\"2\",\"3\",\"a\",\"b\",\"c\",\"100000\",\"6000000000\",\n\"0\",\"list_zipped\",\"list\",\"1\",\"2\",\"3\",\"a\",\"b\",\"c\",\"100000\",\"6000000000\",\n\"0\",\"number\",\"string\",\"10\"\n\"0\",\"set\",\"set\",\"1\",\"100000\",\"2\",\"3\",\"6000000000\",\"a\",\"b\",\"c\",\n\"0\",\"set_zipped_1\",\"set\",\"1\",\"2\",\"3\",\"4\",\n\"0\",\"set_zipped_2\",\"set\",\"100000\",\"200000\",\"300000\",\"400000\",\n\"0\",\"set_zipped_3\",\"set\",\"1000000000\",\"2000000000\",\"3000000000\",\"4000000000\",\"5000000000\",\"6000000000\",\n\"0\",\"string\",\"string\",\"Hello World\"\n\"0\",\"zset\",\"zset\",\"a\",\"1\",\"b\",\"2\",\"c\",\"3\",\"aa\",\"10\",\"bb\",\"20\",\"cc\",\"30\",\"aaa\",\"100\",\"bbb\",\"200\",\"ccc\",\"300\",\"aaaa\",\"1000\",\"cccc\",\"123456789\",\"bbbb\",\"5000000000\",\n\"0\",\"zset_zipped\",\"zset\",\"a\",\"1\",\"b\",\"2\",\"c\",\"3\",\n}\n}\n\nset server_path [tmpdir \"server.rdb-startup-test\"]\n\nstart_server [list overrides [list \"dir\" $server_path] keep_persistence true] {\n    test {Server started empty with non-existing RDB file} {\n        r debug digest\n    } {0000000000000000000000000000000000000000}\n    # Save an RDB file, needed for the next test.\n    r save\n}\n\nstart_server [list overrides [list \"dir\" $server_path] keep_persistence true] {\n    test {Server started empty with empty RDB file} {\n        r debug digest\n    } {0000000000000000000000000000000000000000}\n}\n\nstart_server [list overrides [list \"dir\" $server_path] keep_persistence true] {\n    test {Test RDB stream encoding} {\n        for {set j 0} {$j < 1000} {incr j} {\n            if {rand() < 0.9} {\n                r xadd stream * foo abc\n            } else {\n                r xadd stream * bar $j\n            }\n        }\n        r xgroup create stream mygroup 0\n        set records [r xreadgroup GROUP mygroup Alice COUNT 2 STREAMS stream >]\n        r xdel stream [lindex [lindex [lindex [lindex $records 0] 1] 1] 0]\n        r xack stream mygroup [lindex [lindex [lindex [lindex $records 0] 1] 0] 0]\n        set digest [r debug digest]\n        r config set sanitize-dump-payload no\n        r debug reload\n        set newdigest [r debug digest]\n        assert {$digest eq $newdigest}\n    }\n    test {Test RDB stream encoding - sanitize dump} {\n        r config set sanitize-dump-payload yes\n        r debug reload\n        set newdigest [r debug digest]\n        assert {$digest eq $newdigest}\n    }\n    # delete the stream, maybe valgrind will find something\n    r del stream\n}\n\n# Helper function to start a server and kill it, just to check the error\n# logged.\nset defaults {}\nproc start_server_and_kill_it {overrides code} {\n    upvar defaults defaults srv srv server_path server_path\n    set config [concat $defaults $overrides]\n    set srv [start_server [list overrides $config keep_persistence true]]\n    uplevel 1 $code\n    kill_server $srv\n}\n\n# Make the RDB file unreadable\nfile attributes [file join $server_path dump.rdb] -permissions 0222\n\n# Detect root account (it is able to read the file even with 002 perm)\nset isroot 0\ncatch {\n    open [file join $server_path dump.rdb]\n    set isroot 1\n}\n\n# Now make sure the server aborted with an error\nif {!$isroot} {\n    start_server_and_kill_it [list \"dir\" $server_path] {\n        test {Server should not start if RDB file can't be open} {\n            wait_for_condition 50 100 {\n                [string match {*Fatal error loading*} \\\n                    [exec tail -1 < [dict get $srv stdout]]]\n            } else {\n                fail \"Server started even if RDB was unreadable!\"\n            }\n        }\n    }\n}\n\n# Fix permissions of the RDB file.\nfile attributes [file join $server_path dump.rdb] -permissions 0666\n\n# Corrupt its CRC64 checksum.\nset filesize [file size [file join $server_path dump.rdb]]\nset fd [open [file join $server_path dump.rdb] r+]\nfconfigure $fd -translation binary\nseek $fd -8 end\nputs -nonewline $fd \"foobar00\"; # Corrupt the checksum\nclose $fd\n\n# Now make sure the server aborted with an error\nstart_server_and_kill_it [list \"dir\" $server_path] {\n    test {Server should not start if RDB is corrupted} {\n        wait_for_condition 50 100 {\n            [string match {*CRC error*} \\\n                [exec tail -10 < [dict get $srv stdout]]]\n        } else {\n            fail \"Server started even if RDB was corrupted!\"\n        }\n    }\n}\n\nstart_server {} {\n    test {Test FLUSHALL aborts bgsave} {\n        # 1000 keys with 1ms sleep per key shuld take 1 second\n        r config set rdb-key-save-delay 1000\n        r debug populate 1000\n        r bgsave\n        assert_equal [s rdb_bgsave_in_progress] 1\n        r flushall\n        # wait half a second max\n        wait_for_condition 5 100 {\n            [s rdb_bgsave_in_progress] == 0\n        } else {\n            fail \"bgsave not aborted\"\n        }\n        # veirfy that bgsave failed, by checking that the change counter is still high\n        assert_lessthan 999 [s rdb_changes_since_last_save]\n        # make sure the server is still writable\n        r set x xx\n    }\n\n    test {bgsave resets the change counter} {\n        r config set rdb-key-save-delay 0\n        r bgsave\n        wait_for_condition 50 100 {\n            [s rdb_bgsave_in_progress] == 0\n        } else {\n            fail \"bgsave not done\"\n        }\n        assert_equal [s rdb_changes_since_last_save] 0\n    }\n}\n\ntest {client freed during loading} {\n    start_server [list overrides [list key-load-delay 10 rdbcompression no]] {\n        # create a big rdb that will take long to load. it is important\n        # for keys to be big since the server processes events only once in 2mb.\n        # 100mb of rdb, 100k keys will load in more than 1 second\n        r debug populate 100000 key 1000\n\n        restart_server 0 false false\n\n        # make sure it's still loading\n        assert_equal [s loading] 1\n\n        # connect and disconnect 10 clients\n        set clients {}\n        for {set j 0} {$j < 10} {incr j} {\n            lappend clients [redis_deferring_client]\n        }\n        foreach rd $clients {\n            $rd debug log bla\n        }\n        foreach rd $clients {\n            $rd read\n        }\n        foreach rd $clients {\n            $rd close\n        }\n\n        # make sure the server freed the clients\n        wait_for_condition 100 100 {\n            [s connected_clients] < 3\n        } else {\n            fail \"clients didn't disconnect\"\n        }\n\n        # make sure it's still loading\n        assert_equal [s loading] 1\n\n        # no need to keep waiting for loading to complete\n        exec kill [srv 0 pid]\n    }\n}\n\ntest {repeated load} {\n    start_server [list overrides [list server-threads 3 allow-rdb-resize-op no]] {\n        r debug populate 500000 key 1000\n\n        set digest [r debug digest]\n        for {set j 0} {$j < 10} {incr j} {\n            r debug reload\n            assert_equal $digest [r debug digest]\n        }\n    }\n}\n\n# Our COW metrics (Private_Dirty) work only on Linux\nset system_name [string tolower [exec uname -s]]\nif {$system_name eq {linux}} {\n\n# use-fork not stable\nif 0 {\nstart_server {overrides {save \"\" use-fork yes}} {\n    test {Test child sending info} {\n        # make sure that rdb_last_cow_size and current_cow_size are zero (the test using new server),\n        # so that the comparisons during the test will be valid\n        assert {[s current_cow_size] == 0}\n        assert {[s current_save_keys_processed] == 0}\n        assert {[s current_save_keys_total] == 0}\n\n        assert {[s rdb_last_cow_size] == 0}\n\n        # using a 200us delay, the bgsave is empirically taking about 10 seconds.\n        # we need it to take more than some 5 seconds, since redis only report COW once a second.\n        r config set rdb-key-save-delay 200\n        r config set loglevel debug\n\n        # populate the db with 10k keys of 4k each\n        set rd [redis_deferring_client 0]\n        set size 4096\n        set cmd_count 10000\n        for {set k 0} {$k < $cmd_count} {incr k} {\n            $rd set key$k [string repeat A $size]\n        }\n\n        for {set k 0} {$k < $cmd_count} {incr k} {\n            catch { $rd read }\n        }\n\n        $rd close\n\n        # start background rdb save\n        r bgsave\n\n        set current_save_keys_total [s current_save_keys_total]\n        if {$::verbose} {\n            puts \"Keys before bgsave start: current_save_keys_total\"\n        }\n\n        # on each iteration, we will write some key to the server to trigger copy-on-write, and\n        # wait to see that it reflected in INFO.\n        set iteration 1\n        while 1 {\n            # take samples before writing new data to the server\n            set cow_size [s current_cow_size]\n            if {$::verbose} {\n                puts \"COW info before copy-on-write: $cow_size\"\n            }\n\n            set keys_processed [s current_save_keys_processed]\n            if {$::verbose} {\n                puts \"current_save_keys_processed info : $keys_processed\"\n            }\n\n            # trigger copy-on-write\n            r setrange key$iteration 0 [string repeat B $size]\n\n            # wait to see that current_cow_size value updated (as long as the child is in progress)\n            wait_for_condition 80 100 {\n                [s rdb_bgsave_in_progress] == 0 ||\n                [s current_cow_size] >= $cow_size + $size && \n                [s current_save_keys_processed] > $keys_processed &&\n                [s current_fork_perc] > 0\n            } else {\n                if {$::verbose} {\n                    puts \"COW info on fail: [s current_cow_size]\"\n                    puts [exec tail -n 100 < [srv 0 stdout]]\n                }\n                fail \"COW info wasn't reported\"\n            }\n\n            # assert that $keys_processed is not greater than total keys.\n            assert_morethan_equal $current_save_keys_total $keys_processed\n\n            # for no accurate, stop after 2 iterations\n            if {!$::accurate && $iteration == 2} {\n                break\n            }\n\n            # stop iterating if the bgsave completed\n            if { [s rdb_bgsave_in_progress] == 0 } {\n                break\n            }\n\n            incr iteration 1\n        }\n\n        # make sure we saw report of current_cow_size\n        if {$iteration < 2 && $::verbose} {\n            puts [exec tail -n 100 < [srv 0 stdout]]\n        }\n        assert_morethan_equal $iteration 2\n\n        # if bgsave completed, check that rdb_last_cow_size (fork exit report)\n        # is at least 90% of last rdb_active_cow_size.\n        if { [s rdb_bgsave_in_progress] == 0 } {\n            set final_cow [s rdb_last_cow_size]\n            set cow_size [expr $cow_size * 0.9]\n            if {$final_cow < $cow_size && $::verbose} {\n                puts [exec tail -n 100 < [srv 0 stdout]]\n            }\n            assert_morethan_equal $final_cow $cow_size\n        }\n    }\n}\n} ;# system_name\n}\n\n} ;# tags\n"
  },
  {
    "path": "tests/integration/replication-2.tcl",
    "content": "start_server {tags {\"repl\"}} {\n    start_server {} {\n        test {First server should have role slave after SLAVEOF} {\n            r -1 slaveof [srv 0 host] [srv 0 port]\n            wait_for_condition 50 100 {\n                [s -1 master_link_status] eq {up}\n            } else {\n                fail \"Replication not started.\"\n            }\n        }\n\n        test {If min-slaves-to-write is honored, write is accepted} {\n            r config set min-slaves-to-write 1\n            r config set min-slaves-max-lag 10\n            r set foo 12345\n            wait_for_condition 50 100 {\n                [r -1 get foo] eq {12345}\n            } else {\n                fail \"Write did not reached replica\"\n            }\n        }\n\n        test {No write if min-slaves-to-write is < attached slaves} {\n            r config set min-slaves-to-write 2\n            r config set min-slaves-max-lag 10\n            catch {r set foo 12345} err\n            set err\n        } {NOREPLICAS*}\n\n        test {If min-slaves-to-write is honored, write is accepted (again)} {\n            r config set min-slaves-to-write 1\n            r config set min-slaves-max-lag 10\n            r set foo 12345\n            wait_for_condition 50 100 {\n                [r -1 get foo] eq {12345}\n            } else {\n                fail \"Write did not reached replica\"\n            }\n        }\n\n        test {No write if min-slaves-max-lag is > of the slave lag} {\n            r config set min-slaves-to-write 1\n            r config set min-slaves-max-lag 2\n            exec kill -SIGSTOP [srv -1 pid]\n            assert {[r set foo 12345] eq {OK}}\n            wait_for_condition 100 100 {\n                [catch {r set foo 12345}] != 0\n            } else {\n                fail \"Master didn't become readonly\"\n            }\n            catch {r set foo 12345} err\n            assert_match {NOREPLICAS*} $err\n        }\n        exec kill -SIGCONT [srv -1 pid]\n\n        test {min-slaves-to-write is ignored by slaves} {\n            r config set min-slaves-to-write 1\n            r config set min-slaves-max-lag 10\n            r -1 config set min-slaves-to-write 1\n            r -1 config set min-slaves-max-lag 10\n            r set foo aaabbb\n            wait_for_condition 50 100 {\n                [r -1 get foo] eq {aaabbb}\n            } else {\n                fail \"Write did not reached replica\"\n            }\n        }\n\n        # Fix parameters for the next test to work\n        r config set min-slaves-to-write 0\n        r -1 config set min-slaves-to-write 0\n        r flushall\n\n        test {MASTER and SLAVE dataset should be identical after complex ops} {\n            createComplexDataset r 10000\n            after 500\n            if {[r debug digest] ne [r -1 debug digest]} {\n                set csv1 [csvdump r]\n                set csv2 [csvdump {r -1}]\n                set fd [open /tmp/repldump1.txt w]\n                puts -nonewline $fd $csv1\n                close $fd\n                set fd [open /tmp/repldump2.txt w]\n                puts -nonewline $fd $csv2\n                close $fd\n                puts \"Master - Replica inconsistency\"\n                puts \"Run diff -u against /tmp/repldump*.txt for more info\"\n            }\n            assert_equal [r debug digest] [r -1 debug digest]\n        }\n\n        test {REPL Backlog handles large value} {\n            # initialize bigval to 64-bytes\n            r flushall\n            r config set repl-backlog-size 1K\n            r config set client-output-buffer-limit \"replica 1024 1024 0\"\n            set bigval \"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\"\n            for {set i 0} { $i < 20 } { incr i } {\n                append bigval $bigval\n            }\n            r set bigkey $bigval\n            # We expect the replication to be disconnected so wait a bit\n            wait_for_condition 50 100 {\n                [s -1 master_link_status] eq {down}\n            } else {\n                fail \"Memory limit exceeded but not detected\"\n            }\n            wait_for_condition 50 100 {\n                [r debug digest] eq [r -1 debug digest]\n            } else {\n                fail \"Replica did not reconnect\"\n            }\n        }\n    }\n}\n"
  },
  {
    "path": "tests/integration/replication-4.tcl",
    "content": "start_server {tags {\"repl network\"}} {\n    start_server {} {\n\n        set master [srv -1 client]\n        set master_host [srv -1 host]\n        set master_port [srv -1 port]\n        set slave [srv 0 client]\n\n        set load_handle0 [start_bg_complex_data $master_host $master_port 9 100000]\n        set load_handle1 [start_bg_complex_data $master_host $master_port 11 100000]\n        set load_handle2 [start_bg_complex_data $master_host $master_port 12 100000]\n\n        test {First server should have role slave after SLAVEOF} {\n            $slave slaveof $master_host $master_port\n            after 1000\n            s 0 role\n        } {slave}\n\n        test {Test replication with parallel clients writing in different DBs} {\n            after 5000\n            stop_bg_complex_data $load_handle0\n            stop_bg_complex_data $load_handle1\n            stop_bg_complex_data $load_handle2\n            wait_for_condition 100 100 {\n                [$master debug digest] == [$slave debug digest]\n            } else {\n                set csv1 [csvdump r]\n                set csv2 [csvdump {r -1}]\n                set fd [open /tmp/repldump1.txt w]\n                puts -nonewline $fd $csv1\n                close $fd\n                set fd [open /tmp/repldump2.txt w]\n                puts -nonewline $fd $csv2\n                close $fd\n                fail \"Master - Replica inconsistency, Run diff -u against /tmp/repldump*.txt for more info\"\n            }\n            assert {[$master dbsize] > 0}\n        }\n    }\n}\n\nstart_server {tags {\"repl\"}} {\n    start_server {} {\n        set master [srv -1 client]\n        set master_host [srv -1 host]\n        set master_port [srv -1 port]\n        set slave [srv 0 client]\n\n        test {First server should have role slave after SLAVEOF} {\n            $slave slaveof $master_host $master_port\n            wait_for_condition 50 100 {\n                [s 0 master_link_status] eq {up}\n            } else {\n                fail \"Replication not started.\"\n            }\n        }\n\n        test {With min-slaves-to-write (1,3): master should be writable} {\n            $master config set min-slaves-max-lag 3\n            $master config set min-slaves-to-write 1\n            $master set foo bar\n        } {OK}\n\n        test {With min-slaves-to-write (2,3): master should not be writable} {\n            $master config set min-slaves-max-lag 3\n            $master config set min-slaves-to-write 2\n            catch {$master set foo bar} e\n            set e\n        } {NOREPLICAS*}\n\n        test {With min-slaves-to-write: master not writable with lagged slave} {\n            $master config set min-slaves-max-lag 2\n            $master config set min-slaves-to-write 1\n            assert {[$master set foo bar] eq {OK}}\n            exec kill -SIGSTOP [srv 0 pid]\n            wait_for_condition 100 100 {\n                [catch {$master set foo bar}] != 0\n            } else {\n                fail \"Master didn't become readonly\"\n            }\n            catch {$master set foo bar} err\n            assert_match {NOREPLICAS*} $err\n            exec kill -SIGCONT [srv 0 pid]\n        }\n    }\n}\n\nstart_server {tags {\"repl\"}} {\n    start_server {} {\n        set master [srv -1 client]\n        set master_host [srv -1 host]\n        set master_port [srv -1 port]\n        set slave [srv 0 client]\n\n        test {First server should have role slave after SLAVEOF} {\n            $slave slaveof $master_host $master_port\n            wait_for_condition 50 100 {\n                [s 0 role] eq {slave}\n            } else {\n                fail \"Replication not started.\"\n            }\n        }\n\n        test {Replication: commands with many arguments (issue #1221)} {\n            # We now issue large MSET commands, that may trigger a specific\n            # class of bugs, see issue #1221.\n            for {set j 0} {$j < 100} {incr j} {\n                set cmd [list mset]\n                for {set x 0} {$x < 1000} {incr x} {\n                    lappend cmd [randomKey] [randomValue]\n                }\n                $master {*}$cmd\n            }\n\n            set retry 10\n            while {$retry && ([$master debug digest] ne [$slave debug digest])}\\\n            {\n                after 1000\n                incr retry -1\n            }\n            assert {[$master dbsize] > 0}\n        }\n\n        test {Replication of SPOP command -- alsoPropagate() API} {\n            $master del myset\n            set size [expr 1+[randomInt 100]]\n            set content {}\n            for {set j 0} {$j < $size} {incr j} {\n                lappend content [randomValue]\n            }\n            $master sadd myset {*}$content\n\n            set count [randomInt 100]\n            set result [$master spop myset $count]\n\n            wait_for_condition 50 100 {\n                [$master debug digest] eq [$slave debug digest]\n            } else {\n                fail \"SPOP replication inconsistency\"\n            }\n        }\n\n        test {Replication of EXPIREMEMBER (set) command} {\n            $master sadd testkey a b c d\n            wait_for_condition 50 100 {\n                [$master debug digest] eq [$slave debug digest]\n            } else {\n                fail \"Failed to replicate set\"\n            }\n            $master expiremember testkey a 1\n            after 1000\n            wait_for_condition 50 100 {\n                [$master scard testkey] eq 3\n            } else {\n                fail \"expiremember failed to work on master\"\n            }\n            wait_for_condition 50 100 {\n                [$slave scard testkey] eq 3\n            } else {\n                assert_equal [$slave scard testkey] 3\n            }\n\t\t\t$master del testkey\n        }\n\n\t\ttest {Replication of EXPIREMEMBER (hash) command} {\n            $master hset testkey a value\n\t\t\t$master hset testkey b value\n            wait_for_condition 50 100 {\n                [$master debug digest] eq [$slave debug digest]\n            } else {\n                fail \"Failed to replicate set\"\n            }\n            $master expiremember testkey a 1\n            after 1000\n            wait_for_condition 50 100 {\n                [$master hlen testkey] eq 1\n            } else {\n                fail \"expiremember failed to work on master\"\n            }\n            wait_for_condition 50 100 {\n                [$slave hlen testkey] eq 1\n            } else {\n                assert_equal [$slave hlen testkey] 1\n            }\n\t\t\t$master del testkey\n        }\n\n\t\ttest {Replication of EXPIREMEMBER (zset) command} {\n            $master zadd testkey 1 a\n\t\t\t$master zadd testkey 2 b\n            wait_for_condition 50 100 {\n                [$master debug digest] eq [$slave debug digest]\n            } else {\n                fail \"Failed to replicate set\"\n            }\n            $master expiremember testkey a 1\n            after 1000\n            wait_for_condition 50 100 {\n                [$master zcard testkey] eq 1\n            } else {\n                fail \"expiremember failed to work on master\"\n            }\n            wait_for_condition 50 100 {\n                [$slave zcard testkey] eq 1\n            } else {\n                assert_equal [$slave zcard testkey] 1\n            }\n        }\n\n\t\ttest {keydb.cron replicates} {\n            $master del testkey\n            $master keydb.cron testjob repeat 0 1000000 {redis.call(\"incr\", \"testkey\")} 1 testkey\n            after 300\n            assert_equal 1 [$master get testkey]\n            assert_equal 1 [$master exists testjob]\n\n            wait_for_condition 50 100 {\n                [$master debug digest] eq [$slave debug digest]\n            } else {\n                fail \"KEYDB.CRON failed to replicate\"\n            }\n            $master del testjob\n            $master del testkey\n            wait_for_condition 50 1000 {\n                [$master debug digest] eq [$slave debug digest]\n            } else {\n                fail \"cron delete failed to propogate\"\n            }\n        }\n    }\n}\n"
  },
  {
    "path": "tests/integration/replication-active.tcl",
    "content": "start_server {tags {\"active-repl\"} overrides {active-replica yes}} {\n    set slave [srv 0 client]\n    set slave_host [srv 0 host]\n    set slave_port [srv 0 port]\n    set slave_log [srv 0 stdout]\n    set slave_pid [s process_id]\n\n    start_server [list overrides [list active-replica yes replicaof [list $slave_host $slave_port]]] {\n        set master [srv 0 client]\n        set master_host [srv 0 host]\n        set master_port [srv 0 port]\n    \tset master_pid [s process_id]\n\n        # Use a short replication timeout on the slave, so that if there\n        # are no bugs the timeout is triggered in a reasonable amount\n        # of time.\n        $slave config set repl-timeout 5\n        $master config set repl-timeout 5\n\n        # Start the replication process...\n        $slave slaveof $master_host $master_port\n        #note the master is a replica via the config (see start_server above)\n\n        test {Active replicas report the correct role} {\n            wait_for_condition 50 100 {\n                [string match *active-replica* [$slave role]]\n            } else {\n                fail \"Replica0 does not report the correct role\"\n            }\n            wait_for_condition 50 100 {\n                [string match *active-replica* [$master role]]\n            } else {\n                fail \"Replica1 does not report the correct role\"\n            }\n        }\n\n        test {Active replicas propogate} {\n            $master set testkey foo\n            after 500\n            wait_for_condition 50 500 {\n                [string match *foo* [$slave get testkey]]\n            } else {\n                fail \"replication failed to propogate\"\n            }\n\n            $slave set testkey bar\n            wait_for_condition 50 500 {\n                [string match bar [$master get testkey]]\n            } else {\n                fail \"replication failed to propogate in the other direction\"\n            }\n        }\n\n        test {Active replicas propogate binary} {\n            $master set binkey \"\\u0000foo\"\n            wait_for_condition 50 500 {\n                [string match *foo* [$slave get binkey]]\n            } else {\n                fail \"replication failed to propogate binary data\"\n            }\n        }\n\n        test {Active replicas propogate transaction} {\n            $master set testkey 0\n            $master multi\n            $master incr testkey\n            $master incr testkey\n            after 5000\n            $master get testkey\n            $master exec\n            assert_equal 2 [$master get testkey]\n            after 500\n            wait_for_condition 50 500 {\n                [string match \"2\" [$slave get testkey]]\n            } else {\n                fail \"Transaction failed to replicate\"\n            }\n            $master flushall\n        }\n\n        test {Replication of EXPIREMEMBER (set) command (Active)} {\n            $master sadd testkey a b c d\n            wait_for_condition 50 200 {\n                [$master debug digest] eq [$slave debug digest]\n            } else {\n                fail \"Failed to replicate set\"\n            }\n            $master expiremember testkey a 1\n            after 1000\n            wait_for_condition 50 100 {\n                [$master scard testkey] eq 3\n            } else {\n                fail \"expiremember failed to work on master\"\n            }\n            wait_for_condition 50 100 {\n                [$slave scard testkey] eq 3\n            } else {\n                assert_equal [$slave scard testkey] 3\n            }\n            $master del testkey\n        }\n\n\t\ttest {Replication of EXPIREMEMBER (hash) command (Active)} {\n            $master hset testkey a value\n            $master hset testkey b value\n            wait_for_condition 50 100 {\n                [$master debug digest] eq [$slave debug digest]\n            } else {\n                fail \"Failed to replicate set\"\n            }\n            $master expiremember testkey a 1\n            after 1000\n            wait_for_condition 50 100 {\n                [$master hlen testkey] eq 1\n            } else {\n                fail \"expiremember failed to work on master\"\n            }\n            wait_for_condition 50 100 {\n                [$slave hlen testkey] eq 1\n            } else {\n                assert_equal [$slave hlen testkey] 1\n            }\n            $master del testkey\n        }\n\n        test {Replication of EXPIREMEMBER (zset) command (Active)} {\n            $master zadd testkey 1 a\n            $master zadd testkey 2 b\n            wait_for_condition 50 100 {\n                [$master debug digest] eq [$slave debug digest]\n            } else {\n                fail \"Failed to replicate set\"\n            }\n            $master expiremember testkey a 1\n            after 1000\n            wait_for_condition 50 100 {\n                [$master zcard testkey] eq 1\n            } else {\n                fail \"expiremember failed to work on master\"\n            }\n            wait_for_condition 50 100 {\n                [$slave zcard testkey] eq 1\n            } else {\n                assert_equal [$slave zcard testkey] 1\n            }\n        }\n\n        test {keydb.cron replicates (Active) } {\n            $master del testkey\n\t        $master keydb.cron testjob repeat 0 1000000 {redis.call(\"incr\", \"testkey\")} 1 testkey\n         \tafter 300\n    \t    assert_equal 1 [$master get testkey]\n\t        assert_equal 1 [$master exists testjob]\n\t\t\t\n\t\t\twait_for_condition 50 100 {\n\t\t\t\t[$master debug digest] eq [$slave debug digest]\n\t\t\t} else {\n                fail \"KEYDB.CRON failed to replicate\"\n            }\n        \t$master del testjob\n            $master del testkey\n            wait_for_condition 50 1000 {\n                [$master debug digest] eq [$slave debug digest]\n            } else {\n                fail \"cron delete failed to propogate\"\n            }\n    \t}\n\n        test {Active replicas WAIT} {\n            # Test that wait succeeds since replicas should be syncronized\n            $master set testkey foo\n            $slave set testkey2 test\n            assert_equal {1} [$master wait 1 1000] { \"value should propogate\n                within 1 second\" }\n            assert_equal {1} [$slave wait 1 1000] { \"value should propogate\n                within 1 second\" }\n\n            # Now setup a situation where wait should fail\n            exec kill -SIGSTOP $slave_pid\n            $master set testkey fee\n            assert_equal {0} [$master wait 1 1000] { \"slave shouldn't be\n                synchronized since its stopped\" }\n        }\n        # Resume the replica we paused in the prior test\n        exec kill -SIGCONT $slave_pid\n\n        test {Active replica expire propogates} {\n            $master set testkey1 foo\n            wait_for_condition 50 1000 {\n                [string match *foo* [$slave get testkey1]]\n            } else {\n                fail \"Replication failed to propogate\"\n            }\n            $master pexpire testkey1 200\n            after 1000\n            assert_equal {0} [$master del testkey1] {\"master expired\"}\n            assert_equal {0} [$slave del testkey1]  {\"slave expired\"}\n\n            $slave set testkey1 foo px 200\n            after 1000\n            assert_equal {0} [$master del testkey1]\n            assert_equal {0} [$slave del testkey1]\n        }\n\n    test {Active replica expire propogates when source is down} {\n        $slave flushall\n        $slave set testkey2 foo\n        $slave set testkey1 foo\n        wait_for_condition 50 1000 {\n            [string match *foo* [$master get testkey1]]\n        } else {\n            fail \"Replication failed to propogate\"\n        }\n        $slave expire testkey1 2\n        assert_equal {1} [$slave wait 1 500] { \"value should propogate\n                    within 0.5 seconds\" }\n        exec kill -SIGSTOP $slave_pid\n        after 3000\n        # Ensure testkey1 is gone.  Note, we can't do this directly as the normal commands lie to us\n        # about what is actually in the dict.  The only way to know is with a count from info\n        assert_equal {1} [expr [string first {keys=1} [$master info keyspace]] >= 0]  {\"slave expired\"}\n    }\n    \n    exec kill -SIGCONT $slave_pid\n\n    test {Active replica merge works when reconnecting} {\n        $slave flushall\n        $slave set testkey foo\n        wait_for_condition 50 1000 {\n            [string match *foo* [$master get testkey]]\n        } else {\n            fail \"Replication failed to propogate\"\n        }\n        $slave replicaof no one\n        $master replicaof no one\n        after 100\n        $master set testkey baz\n        after 200\n        $slave set testkey bar\n        after 100\n        $slave replicaof $master_host $master_port\n        after 1000\n        $master replicaof $slave_host $slave_port\n\n        wait_for_condition 50 100 {\n            [string match bar [$slave get testkey]]\n        } else {\n            fail \"Replica is not correct\"\n        }\n\n        wait_for_condition 50 100 {\n            [string match bar [$master get testkey]]\n        } else {\n            fail \"Master is not correct\"\n        }\n    }\n\n    test {Active replica merge works with client blocked} {\n        $slave flushall\n        $slave replicaof no one\n        $master replicaof no one\n        after 100\n        set rd [redis_deferring_client]\n        $rd blpop testlist 0\n        $slave lpush testlist foo\n        \n        #OK Now reconnect\n        $slave replicaof $master_host $master_port\n        $master replicaof $slave_host $slave_port\n        after 1000\n\n        $rd read\n    } {testlist foo}\n\n    test {Active replica different databases} {\n        $master select 3\n        $master set testkey abcd\n        $master select 2\n        $master del testkey\n        $slave select 3\n        wait_for_condition 50 1000 {\n            [string match abcd [$slave get testkey]]\n        } else {\n            fail \"Replication failed to propogate DB 3\"\n        }\n    }\n}\n\nforeach mdl {no yes} {\n    foreach sdl {disabled swapdb} {\n        start_server {tags {\"active-repl\"} overrides {active-replica yes}} {\n            set master [srv 0 client]\n            $master config set repl-diskless-sync $mdl\n            set master_host [srv 0 host]\n            set master_port [srv 0 port]\n            r set masterkey foo\n            start_server {overrides {active-replica yes}} {\n                test \"Active replication databases are merged, master diskless=$mdl, replica diskless=$sdl\" {\n                    r config set repl-diskless-load $sdl\n                    r set slavekey bar\n                    r replicaof $master_host $master_port\n                    \n                    wait_for_condition 50 400 {\n                        [string match *state=online* [$master info]]\n                    } else {\n                        fail \"Replica never came online\"\n                    }\n                    \n                    assert_equal bar [r get slavekey]\n                    assert_equal foo [r get masterkey]\n                }\n            }\n        }\n    }\n}\n}\n\nstart_server {tags {\"active-repl\"} overrides {active-replica yes}} {\n    set slave [srv 0 client]\n    set slave_host [srv 0 host]\n    set slave_port [srv 0 port]\n    start_server {tags {\"active-repl\"} overrides { active-replica yes}} {\n        r set testkeyB bar\n        test {Active Replica Merges Database On Sync} {\n            $slave set testkeyA foo\n            r replicaof $slave_host $slave_port\n\t    wait_for_condition 50 1000 {\n                [string match *active-replica* [r role]]\n            } else {\n                fail \"Replica did not connect\"\n            }\n\t    wait_for_condition 50 1000 {\n\t\t[string match \"2\" [r dbsize]]\n            } else {\n                fail \"key did not propogate\"\n            }\n\t}\n    }\n}\n\nstart_server {tags {\"active-repl\"} overrides {active-replica yes}} {\n    set master [srv 0 client]\n    set master_host [srv 0 host]\n    set master_port [srv 0 port]\n    test {REPLICAOF no one in config properly clears master list} {\n        start_server [list overrides [list \"replicaof\" \"$master_host $master_port\" \"replicaof\" \"no one\" \"replicaof\" \"$master_host $master_port\"]] {\n            wait_for_condition 50 100 {\n                [string match {*role:slave*} [[srv 0 client] info replication]] &&\n                [string match \"*master_host:$master_host*\" [[srv 0 client] info replication]] &&\n                [string match \"*master_port:$master_port*\" [[srv 0 client] info replication]]\n            } else {\n                fail \"Replica did not properly connect to master\"\n            }\n        }\n\t}\n}\n"
  },
  {
    "path": "tests/integration/replication-multimaster-connect.tcl",
    "content": "start_server {tags {\"multi-master\"} overrides {active-replica yes multi-master yes}} {\nstart_server {overrides {active-replica yes multi-master yes}} {\nstart_server {overrides {active-replica yes multi-master yes}} {\nstart_server {overrides {active-replica yes multi-master yes}} {\n    for {set j 0} {$j < 4} {incr j} {\n        set R($j) [srv [expr 0-$j] client]\n        set R_host($j) [srv [expr 0-$j] host]\n        set R_port($j) [srv [expr 0-$j] port]\n    }\n\n    set keysPerServer 100\n\n    # Initialize Dataset\n    for {set j 1} {$j < 4} {incr j} {\n        for {set key 0} { $key < $keysPerServer} { incr key } {\n            $R($j) set \"key_$j\\_$key\" asdjaoijioasdjiod ex 100000\n        }\n        set hash($j) [$R($j) debug digest]\n    }\n\n    $R(1) replicaof $R_host(0) $R_port(0)\n    $R(2) replicaof $R_host(0) $R_port(0)\n    $R(3) replicaof $R_host(0) $R_port(0)\n\n    test \"all nodes up\" {\n        for {set j 1} {$j < 4} {incr j} {\n            wait_for_condition 50 100 {\n                [string match {*master_global_link_status:up*} [$R($j) info replication]]\n            } else {\n                fail \"Multimaster group didn't connect up in a reasonable period of time\"\n            }\n        }\n    }\n\n    test \"nodes retain their data\" {\n        for {set j 1} { $j < 4 } { incr j } {\n            assert_equal [$R($j) debug digest] $hash($j) $j\n        }\n    }\n\n    # Set all servers with an overlapping key - the last one should win\n    $R(0) set isvalid no\n    $R(1) set isvalid no\n    $R(2) set isvalid no\n    # Note: Sleep is due to mvcc slip\n    after 2\n    $R(3) set isvalid yes\n\n    for {set n 1} {$n < 4} {incr n} {\n        test \"Node $n reciprocal rep works\" {\n            $R(0) replicaof $R_host($n) $R_port($n)\n            after 2000\n            for {set key 0} { $key < $keysPerServer } { incr key } {\n                assert_equal [$R(0) get \"key_$n\\_$key\"] asdjaoijioasdjiod $key\n            }\n        }\n    }\n\n    test \"All data transferred between nodes\" {\n        for {set server 0} {$server < 4} {incr server} {\n            set hash($j) [$R($server) debug digest]\n            for {set n 1} {$n < 4} {incr n} {\n                for {set key 0} {$key < $keysPerServer} {incr key} {\n                    assert_equal [$R($server) get \"key_$n\\_$key\"] asdjaoijioasdjiod \"server: $n key: $key\"\n                }\n            }\n        }\n    }\n\n    test \"MVCC Updates Correctly\" {\n        assert_equal [$R(0) get isvalid] yes\n        assert_equal [$R(1) get isvalid] yes\n        assert_equal [$R(2) get isvalid] yes\n        assert_equal [$R(3) get isvalid] yes\n    }\n\n    unset hash\n    test \"All servers same debug digest\" {\n        set hash [$R(0) debug digest]\n        for {set j 1} {$j < 4} {incr j} {\n            assert_equal $hash [$R($j) debug digest] $j\n        }\n    }\n}}}}\n\n# The tests below validate features replicated via RDB\nstart_server {tags {\"multi-master\"} overrides {active-replica yes multi-master yes}} {\nstart_server {overrides {active-replica yes multi-master yes}} {\nstart_server {overrides {active-replica yes multi-master yes}} {\n    for {set j 0} {$j < 3} {incr j} {\n        set R($j) [srv [expr 0-$j] client]\n        set R_host($j) [srv [expr 0-$j] host]\n        set R_port($j) [srv [expr 0-$j] port]\n    }\n\n    # Set replicated features here\n    $R(0) sadd testhash subkey\n    $R(0) expiremember testhash subkey 10000\n\n    \n    test \"node 2 up\" {\n        $R(2) replicaof $R_host(1) $R_port(1)\n        wait_for_condition 50 100 {\n            [string match {*master_global_link_status:up*} [$R(2) info replication]]\n        } else {\n            fail \"didn't connect up in a reasonable period of time\"\n        }\n     }\n\n    # While node 1 loads from 0, it will relay to 2\n    test \"node 1 up\" {\n        $R(1) replicaof $R_host(0) $R_port(0)\n        wait_for_condition 50 100 {\n            [string match {*master_global_link_status:up*} [$R(1) info replication]]\n        } else {\n            fail \"didn't connect up in a reasonable period of time\"\n        }\n     }\n\n     #Tests that validate replication made it to node 2\n     test \"subkey expire replicates via RDB\" {\n         assert [expr [$R(2) ttl testhash subkey] > 0]\n     }\n}}}\n"
  },
  {
    "path": "tests/integration/replication.tcl",
    "content": "proc log_file_matches {log pattern} {\n    set fp [open $log r]\n    set content [read $fp]\n    close $fp\n    string match $pattern $content\n}\n\nstart_server {tags {\"repl network\"}} {\n    set slave [srv 0 client]\n    set slave_host [srv 0 host]\n    set slave_port [srv 0 port]\n    set slave_log [srv 0 stdout]\n    start_server {} {\n        set master [srv 0 client]\n        set master_host [srv 0 host]\n        set master_port [srv 0 port]\n\n        # Configure the master in order to hang waiting for the BGSAVE\n        # operation, so that the slave remains in the handshake state.\n        $master config set repl-diskless-sync yes\n        $master config set repl-diskless-sync-delay 1000\n\n        # Use a short replication timeout on the slave, so that if there\n        # are no bugs the timeout is triggered in a reasonable amount\n        # of time.\n        $slave config set repl-timeout 5\n\n        # Start the replication process...\n        $slave slaveof $master_host $master_port\n\n        test {Slave enters handshake} {\n            wait_for_condition 50 1000 {\n                [string match *handshake* [$slave role]]\n            } else {\n                fail \"Replica does not enter handshake state\"\n            }\n        }\n\n        # But make the master unable to send\n        # the periodic newlines to refresh the connection. The slave\n        # should detect the timeout.\n        $master debug sleep 10\n\n        test {Slave is able to detect timeout during handshake} {\n            wait_for_condition 50 1000 {\n                [log_file_matches $slave_log \"*Timeout connecting to the MASTER*\"]\n            } else {\n                fail \"Replica is not able to detect timeout\"\n            }\n        }\n    }\n}\n\nstart_server {tags {\"repl\"}} {\n    set A [srv 0 client]\n    set A_host [srv 0 host]\n    set A_port [srv 0 port]\n    start_server {} {\n        set B [srv 0 client]\n        set B_host [srv 0 host]\n        set B_port [srv 0 port]\n\n        test {Set instance A as slave of B} {\n            $A slaveof $B_host $B_port\n            wait_for_condition 50 100 {\n                [lindex [$A role] 0] eq {slave} &&\n                [string match {*master_link_status:up*} [$A info replication]]\n            } else {\n                fail \"Can't turn the instance into a replica\"\n            }\n        }\n\n        test {INCRBYFLOAT replication, should not remove expire} {\n            r set test 1 EX 100\n            r incrbyfloat test 0.1\n            after 1000\n            assert_equal [$A debug digest] [$B debug digest]\n        }\n\n        test {GETSET replication} {\n            $A config resetstat\n            $A config set loglevel debug\n            $B config set loglevel debug\n            r set test foo\n            assert_equal [r getset test bar] foo\n            wait_for_condition 500 10 {\n                [$A get test] eq \"bar\"\n            } else {\n                fail \"getset wasn't propagated\"\n            }\n            assert_equal [r set test vaz get] bar\n            wait_for_condition 500 10 {\n                [$A get test] eq \"vaz\"\n            } else {\n                fail \"set get wasn't propagated\"\n            }\n            assert_match {*calls=3,*} [cmdrstat set $A]\n            assert_match {} [cmdrstat getset $A]\n        }\n\n        test {BRPOPLPUSH replication, when blocking against empty list} {\n            $A config resetstat\n            set rd [redis_deferring_client]\n            $rd brpoplpush a b 5\n            r lpush a foo\n            wait_for_condition 50 100 {\n                [$A debug digest] eq [$B debug digest]\n            } else {\n                fail \"Master and replica have different digest: [$A debug digest] VS [$B debug digest]\"\n            }\n            assert_match {*calls=1,*} [cmdrstat rpoplpush $A]\n            assert_match {} [cmdrstat lmove $A]\n        }\n\n        test {BRPOPLPUSH replication, list exists} {\n            $A config resetstat\n            set rd [redis_deferring_client]\n            r lpush c 1\n            r lpush c 2\n            r lpush c 3\n            $rd brpoplpush c d 5\n            after 1000\n            assert_equal [$A debug digest] [$B debug digest]\n            assert_match {*calls=1,*} [cmdrstat rpoplpush $A]\n            assert_match {} [cmdrstat lmove $A]\n        }\n\n        foreach wherefrom {left right} {\n            foreach whereto {left right} {\n                test \"BLMOVE ($wherefrom, $whereto) replication, when blocking against empty list\" {\n                    $A config resetstat\n                    set rd [redis_deferring_client]\n                    $rd blmove a b $wherefrom $whereto 5\n                    r lpush a foo\n                    wait_for_condition 50 100 {\n                        [$A debug digest] eq [$B debug digest]\n                    } else {\n                        fail \"Master and replica have different digest: [$A debug digest] VS [$B debug digest]\"\n                    }\n                    assert_match {*calls=1,*} [cmdrstat lmove $A]\n                    assert_match {} [cmdrstat rpoplpush $A]\n                }\n\n                test \"BLMOVE ($wherefrom, $whereto) replication, list exists\" {\n                    $A config resetstat\n                    set rd [redis_deferring_client]\n                    r lpush c 1\n                    r lpush c 2\n                    r lpush c 3\n                    $rd blmove c d $wherefrom $whereto 5\n                    after 1000\n                    assert_equal [$A debug digest] [$B debug digest]\n                    assert_match {*calls=1,*} [cmdrstat lmove $A]\n                    assert_match {} [cmdrstat rpoplpush $A]\n                }\n            }\n        }\n\n        test {BLPOP followed by role change, issue #2473} {\n            set rd [redis_deferring_client]\n            $rd blpop foo 0 ; # Block while B is a master\n\n            # Turn B into master of A\n            $A slaveof no one\n            $B slaveof $A_host $A_port\n            wait_for_condition 50 100 {\n                [lindex [$B role] 0] eq {slave} &&\n                [string match {*master_link_status:up*} [$B info replication]]\n            } else {\n                fail \"Can't turn the instance into a replica\"\n            }\n\n            # Push elements into the \"foo\" list of the new replica.\n            # If the client is still attached to the instance, we'll get\n            # a desync between the two instances.\n            $A rpush foo a b c\n            after 100\n\n            wait_for_condition 50 100 {\n                [$A debug digest] eq [$B debug digest] &&\n                [$A lrange foo 0 -1] eq {a b c} &&\n                [$B lrange foo 0 -1] eq {a b c}\n            } else {\n                fail \"Master and replica have different digest: [$A debug digest] VS [$B debug digest]\"\n            }\n        }\n    }\n}\n\nstart_server {tags {\"repl\"}} {\n    r set mykey foo\n\n    start_server {} {\n        test {Second server should have role master at first} {\n            s role\n        } {master}\n\n        test {SLAVEOF should start with link status \"down\"} {\n            r multi\n            r slaveof [srv -1 host] [srv -1 port]\n            r info replication\n            r exec\n        } {*master_link_status:down*}\n\n        test {The role should immediately be changed to \"replica\"} {\n            s role\n        } {slave}\n\n        wait_for_sync r\n        test {Sync should have transferred keys from master} {\n            r get mykey\n        } {foo}\n\n        test {The link status should be up} {\n            s master_link_status\n        } {up}\n\n        test {SET on the master should immediately propagate} {\n            r -1 set mykey bar\n\n            wait_for_condition 500 100 {\n                [r  0 get mykey] eq {bar}\n            } else {\n                fail \"SET on master did not propagated on replica\"\n            }\n        }\n\n        test {FLUSHALL should replicate} {\n            r -1 flushall\n            if {$::valgrind} {after 2000} else {after 500}\n            list [r -1 dbsize] [r 0 dbsize]\n        } {0 0}\n\n        test {ROLE in master reports master with a slave} {\n            set res [r -1 role]\n            lassign $res role offset slaves\n            assert {$role eq {master}}\n            assert {$offset > 0}\n            assert {[llength $slaves] == 1}\n            lassign [lindex $slaves 0] master_host master_port slave_offset\n            assert {$slave_offset <= $offset}\n        }\n\n        test {ROLE in slave reports slave in connected state} {\n            set res [r role]\n            lassign $res role master_host master_port slave_state slave_offset\n            assert {$role eq {slave}}\n            assert {$slave_state eq {connected}}\n        }\n    }\n}\n\nforeach mdl {no yes} {\n    foreach sdl {disabled swapdb} {\n        start_server {tags {\"repl\"}} {\n            set master [srv 0 client]\n            $master config set repl-diskless-sync $mdl\n            $master config set repl-diskless-sync-delay 1\n            set master_host [srv 0 host]\n            set master_port [srv 0 port]\n            set slaves {}\n            start_server {} {\n                lappend slaves [srv 0 client]\n                start_server {} {\n                    lappend slaves [srv 0 client]\n                    start_server {} {\n                        lappend slaves [srv 0 client]\n                        test \"Connect multiple replicas at the same time (issue #141), master diskless=$mdl, replica diskless=$sdl\" {\n                            # start load handles only inside the test, so that the test can be skipped\n                            set load_handle0 [start_bg_complex_data $master_host $master_port 9 100000000]\n                            set load_handle1 [start_bg_complex_data $master_host $master_port 11 100000000]\n                            set load_handle2 [start_bg_complex_data $master_host $master_port 12 100000000]\n                            set load_handle3 [start_write_load $master_host $master_port 8]\n                            set load_handle4 [start_write_load $master_host $master_port 4]\n                            after 5000 ;# wait for some data to accumulate so that we have RDB part for the fork\n\n                            # Send SLAVEOF commands to slaves\n                            [lindex $slaves 0] config set repl-diskless-load $sdl\n                            [lindex $slaves 1] config set repl-diskless-load $sdl\n                            [lindex $slaves 2] config set repl-diskless-load $sdl\n                            [lindex $slaves 0] slaveof $master_host $master_port\n                            [lindex $slaves 1] slaveof $master_host $master_port\n                            [lindex $slaves 2] slaveof $master_host $master_port\n\n                            # Wait for all the three slaves to reach the \"online\"\n                            # state from the POV of the master.\n                            set retry 500\n                            while {$retry} {\n                                set info [r -3 info]\n                                if {[string match {*slave0:*state=online*slave1:*state=online*slave2:*state=online*} $info]} {\n                                    break\n                                } else {\n                                    incr retry -1\n                                    after 100\n                                }\n                            }\n                            if {$retry == 0} {\n                                error \"assertion:Slaves not correctly synchronized\"\n                            }\n\n                            # Wait that slaves acknowledge they are online so\n                            # we are sure that DBSIZE and DEBUG DIGEST will not\n                            # fail because of timing issues.\n                            wait_for_condition 500 100 {\n                                [lindex [[lindex $slaves 0] role] 3] eq {connected} &&\n                                [lindex [[lindex $slaves 1] role] 3] eq {connected} &&\n                                [lindex [[lindex $slaves 2] role] 3] eq {connected}\n                            } else {\n                                fail \"Slaves still not connected after some time\"\n                            }\n\n                            # Stop the write load\n                            stop_bg_complex_data $load_handle0\n                            stop_bg_complex_data $load_handle1\n                            stop_bg_complex_data $load_handle2\n                            stop_write_load $load_handle3\n                            stop_write_load $load_handle4\n\n                            # Make sure no more commands processed\n                            wait_load_handlers_disconnected\n\n                            wait_for_ofs_sync $master [lindex $slaves 0]\n                            wait_for_ofs_sync $master [lindex $slaves 1]\n                            wait_for_ofs_sync $master [lindex $slaves 2]\n\n                            # Check digests\n                            set digest [$master debug digest]\n                            set digest0 [[lindex $slaves 0] debug digest]\n                            set digest1 [[lindex $slaves 1] debug digest]\n                            set digest2 [[lindex $slaves 2] debug digest]\n                            assert {$digest ne 0000000000000000000000000000000000000000}\n                            assert {$digest eq $digest0}\n                            assert {$digest eq $digest1}\n                            assert {$digest eq $digest2}\n                        }\n                   }\n                }\n            }\n        }\n    }\n}\n\nstart_server {tags {\"repl\"}} {\n    set master [srv 0 client]\n    set master_host [srv 0 host]\n    set master_port [srv 0 port]\n    start_server {} {\n        test \"Master stream is correctly processed while the replica has a script in -BUSY state\" {\n            set load_handle0 [start_write_load $master_host $master_port 3]\n            set slave [srv 0 client]\n            $slave config set lua-time-limit 500\n            $slave slaveof $master_host $master_port\n\n            # Wait for the slave to be online\n            wait_for_condition 500 100 {\n                [lindex [$slave role] 3] eq {connected}\n            } else {\n                fail \"Replica still not connected after some time\"\n            }\n\n            # Wait some time to make sure the master is sending data\n            # to the slave.\n            after 5000\n\n            # Stop the ability of the slave to process data by sendig\n            # a script that will put it in BUSY state.\n            $slave eval {for i=1,3000000000 do end} 0\n\n            # Wait some time again so that more master stream will\n            # be processed.\n            after 2000\n\n            # Stop the write load\n            stop_write_load $load_handle0\n\n            # number of keys\n            wait_for_condition 500 100 {\n                [$master debug digest] eq [$slave debug digest]\n            } else {\n                fail \"Different datasets between replica and master\"\n            }\n        }\n    }\n}\n\ntest {slave fails full sync and diskless load swapdb recovers it} {\n    start_server {tags {\"repl\"}} {\n        set slave [srv 0 client]\n        set slave_host [srv 0 host]\n        set slave_port [srv 0 port]\n        set slave_log [srv 0 stdout]\n        start_server {} {\n            set master [srv 0 client]\n            set master_host [srv 0 host]\n            set master_port [srv 0 port]\n\n            # Put different data sets on the master and slave\n            # we need to put large keys on the master since the slave replies to info only once in 2mb\n            $slave debug populate 2000 slave 10\n            $master debug populate 200 master 100000\n            $master config set rdbcompression no\n\n            # Set master and slave to use diskless replication\n            $master config set repl-diskless-sync yes\n            $master config set repl-diskless-sync-delay 0\n            $slave config set repl-diskless-load swapdb\n\n            # Set master with a slow rdb generation, so that we can easily disconnect it mid sync\n            # 10ms per key, with 200 keys is 2 seconds\n            $master config set rdb-key-save-delay 10000\n\n            # Start the replication process...\n            $slave slaveof $master_host $master_port\n\n            # wait for the slave to start reading the rdb\n            wait_for_condition 50 100 {\n                [s -1 loading] eq 1\n            } else {\n                fail \"Replica didn't get into loading mode\"\n            }\n\n            # make sure that next sync will not start immediately so that we can catch the slave in betweeen syncs\n            $master config set repl-diskless-sync-delay 5\n            # for faster server shutdown, make rdb saving fast again (the fork is already uses the slow one)\n            $master config set rdb-key-save-delay 0\n\n            # waiting slave to do flushdb (key count drop)\n            wait_for_condition 50 100 {\n                2000 != [scan [regexp -inline {keys\\=([\\d]*)} [$slave info keyspace]] keys=%d]\n            } else {\n                fail \"Replica didn't flush\"\n            }\n\n            # make sure we're still loading\n            assert_equal [s -1 loading] 1\n\n            # kill the slave connection on the master\n            set killed [$master client kill type slave]\n\n            # wait for loading to stop (fail)\n            wait_for_condition 50 100 {\n                [s -1 loading] eq 0\n            } else {\n                fail \"Replica didn't disconnect\"\n            }\n\n            # make sure the original keys were restored\n            assert_equal [$slave dbsize] 2000\n        }\n    }\n}\n\ntest {diskless loading short read} {\n    start_server {tags {\"repl\"}} {\n        set replica [srv 0 client]\n        set replica_host [srv 0 host]\n        set replica_port [srv 0 port]\n        start_server {} {\n            set master [srv 0 client]\n            set master_host [srv 0 host]\n            set master_port [srv 0 port]\n\n            # Set master and replica to use diskless replication\n            $master config set repl-diskless-sync yes\n            $master config set rdbcompression no\n            $replica config set repl-diskless-load swapdb\n            $master config set hz 500\n            $replica config set hz 500\n            $master config set dynamic-hz no\n            $replica config set dynamic-hz no\n            # Try to fill the master with all types of data types / encodings\n            set start [clock clicks -milliseconds]\n            for {set k 0} {$k < 3} {incr k} {\n                for {set i 0} {$i < 10} {incr i} {\n                    r set \"$k int_$i\" [expr {int(rand()*10000)}]\n                    r expire \"$k int_$i\" [expr {int(rand()*10000)}]\n                    r set \"$k string_$i\" [string repeat A [expr {int(rand()*1000000)}]]\n                    r hset \"$k hash_small\" [string repeat A [expr {int(rand()*10)}]]  0[string repeat A [expr {int(rand()*10)}]]\n                    r hset \"$k hash_large\" [string repeat A [expr {int(rand()*10000)}]] [string repeat A [expr {int(rand()*1000000)}]]\n                    r sadd \"$k set_small\" [string repeat A [expr {int(rand()*10)}]]\n                    r sadd \"$k set_large\" [string repeat A [expr {int(rand()*1000000)}]]\n                    r zadd \"$k zset_small\" [expr {rand()}] [string repeat A [expr {int(rand()*10)}]]\n                    r zadd \"$k zset_large\" [expr {rand()}] [string repeat A [expr {int(rand()*1000000)}]]\n                    r lpush \"$k list_small\" [string repeat A [expr {int(rand()*10)}]]\n                    r lpush \"$k list_large\" [string repeat A [expr {int(rand()*1000000)}]]\n                    for {set j 0} {$j < 10} {incr j} {\n                        r xadd \"$k stream\" * foo \"asdf\" bar \"1234\"\n                    }\n                    r xgroup create \"$k stream\" \"mygroup_$i\" 0\n                    r xreadgroup GROUP \"mygroup_$i\" Alice COUNT 1 STREAMS \"$k stream\" >\n                }\n            }\n\n            if {$::verbose} {\n                set end [clock clicks -milliseconds]\n                set duration [expr $end - $start]\n                puts \"filling took $duration ms (TODO: use pipeline)\"\n                set start [clock clicks -milliseconds]\n            }\n\n            # Start the replication process...\n            set loglines [count_log_lines -1]\n            $master config set repl-diskless-sync-delay 0\n            $replica replicaof $master_host $master_port\n\n            # kill the replication at various points\n            set attempts 100\n            if {$::accurate} { set attempts 500 }\n            for {set i 0} {$i < $attempts} {incr i} {\n                # wait for the replica to start reading the rdb\n                # using the log file since the replica only responds to INFO once in 2mb\n                set res [wait_for_log_messages -1 {\"*Loading DB in memory*\"} $loglines 2000 10]\n                set loglines [lindex $res 1]\n\n                # add some additional random sleep so that we kill the master on a different place each time\n                after [expr {int(rand()*50)}]\n\n                # kill the replica connection on the master\n                set killed [$master client kill type replica]\n\n                set res [wait_for_log_messages -1 {\"*Internal error in RDB*\" \"*Finished with success*\" \"*Successful partial resynchronization*\"} $loglines 1000 1]\n                if {$::verbose} { puts $res }\n                set log_text [lindex $res 0]\n                set loglines [lindex $res 1]\n                if {![string match \"*Internal error in RDB*\" $log_text]} {\n                    # force the replica to try another full sync\n                    $master multi\n                    $master client kill type replica\n                    $master set asdf asdf\n                    $master debug truncate-repl-backlog\n                    $master exec\n                }\n                # wait for loading to stop (fail)\n                wait_for_condition 1000 1 {\n                    [s -1 loading] eq 0\n                } else {\n                    fail \"Replica didn't disconnect\"\n                }\n            }\n            if {$::verbose} {\n                set end [clock clicks -milliseconds]\n                set duration [expr $end - $start]\n                puts \"test took $duration ms\"\n            }\n            # enable fast shutdown\n            $master config set rdb-key-save-delay 0\n        }\n    }\n}\n\n# get current stime and utime metrics for a thread (since it's creation)\nproc get_cpu_metrics { statfile } {\n    if { [ catch {\n        set fid   [ open $statfile r ]\n        set data  [ read $fid 1024 ]\n        ::close $fid\n        set data  [ split $data ]\n\n        ;## number of jiffies it has been scheduled...\n        set utime [ lindex $data 13 ]\n        set stime [ lindex $data 14 ]\n    } err ] } {\n        error \"assertion:can't parse /proc: $err\"\n    }\n    set mstime [clock milliseconds]\n    return [ list $mstime $utime $stime ]\n}\n\n# compute %utime and %stime of a thread between two measurements\nproc compute_cpu_usage {start end} {\n    set clock_ticks [exec getconf CLK_TCK]\n    # convert ms time to jiffies and calc delta\n    set dtime [ expr { ([lindex $end 0] - [lindex $start 0]) * double($clock_ticks) / 1000 } ]\n    set utime [ expr { [lindex $end 1] - [lindex $start 1] } ]\n    set stime [ expr { [lindex $end 2] - [lindex $start 2] } ]\n    set pucpu  [ expr { ($utime / $dtime) * 100 } ]\n    set pscpu  [ expr { ($stime / $dtime) * 100 } ]\n    return [ list $pucpu $pscpu ]\n}\n\n\n# test diskless rdb pipe with multiple replicas, which may drop half way\nstart_server {tags {\"repl\"}} {\n    set master [srv 0 client]\n    $master config set repl-diskless-sync yes\n    $master config set repl-diskless-sync-delay 1\n    $master config set enable-keydb-fastsync no\n    set master_host [srv 0 host]\n    set master_port [srv 0 port]\n    set master_pid [srv 0 pid]\n    # put enough data in the db that the rdb file will be bigger than the socket buffers\n    # and since we'll have key-load-delay of 100, 20000 keys will take at least 2 seconds\n    # we also need the replica to process requests during transfer (which it does only once in 2mb)\n    $master debug populate 20000 test 10000\n    $master config set rdbcompression no\n    # If running on Linux, we also measure utime/stime to detect possible I/O handling issues\n    set os [catch {exec uname}]\n    set measure_time [expr {$os == \"Linux\"} ? 1 : 0]\n    foreach all_drop {no slow fast all timeout} {\n        test \"diskless $all_drop replicas drop during rdb pipe\" {\n            set replicas {}\n            set replicas_alive {}\n            # start one replica that will read the rdb fast, and one that will be slow\n            start_server {} {\n                lappend replicas [srv 0 client]\n                lappend replicas_alive [srv 0 client]\n                start_server {} {\n                    lappend replicas [srv 0 client]\n                    lappend replicas_alive [srv 0 client]\n\n                    # start replication\n                    # it's enough for just one replica to be slow, and have it's write handler enabled\n                    # so that the whole rdb generation process is bound to that\n                    set loglines [count_log_lines -1]\n                    [lindex $replicas 0] config set repl-diskless-load swapdb\n                    [lindex $replicas 0] config set key-load-delay 100 ;# 20k keys and 100 microseconds sleep means at least 2 seconds\n                    [lindex $replicas 0] replicaof $master_host $master_port\n                    [lindex $replicas 1] replicaof $master_host $master_port\n\n                    # wait for the replicas to start reading the rdb\n                    # using the log file since the replica only responds to INFO once in 2mb\n                    wait_for_log_messages -1 {\"*Loading DB in memory*\"} $loglines 800 10\n\n                    if {$measure_time} {\n                        set master_statfile \"/proc/$master_pid/stat\"\n                        set master_start_metrics [get_cpu_metrics $master_statfile]\n                        set start_time [clock seconds]\n                    }\n\n                    # wait a while so that the pipe socket writer will be\n                    # blocked on write (since replica 0 is slow to read from the socket)\n                    after 500\n\n                    # add some command to be present in the command stream after the rdb.\n                    $master incr $all_drop\n\n                    # disconnect replicas depending on the current test\n                    set loglines [count_log_lines -2]\n                    if {$all_drop == \"all\" || $all_drop == \"fast\"} {\n                        exec kill [srv 0 pid]\n                        set replicas_alive [lreplace $replicas_alive 1 1]\n                    }\n                    if {$all_drop == \"all\" || $all_drop == \"slow\"} {\n                        exec kill [srv -1 pid]\n                        set replicas_alive [lreplace $replicas_alive 0 0]\n                    }\n                    if {$all_drop == \"timeout\"} {\n                        $master config set repl-timeout 2\n                        # we want the slow replica to hang on a key for very long so it'll reach repl-timeout\n                        exec kill -SIGSTOP [srv -1 pid]\n                        after 2000\n                    }\n\n                    # wait for rdb child to exit\n                    wait_for_condition 500 100 {\n                        [s -2 rdb_bgsave_in_progress] == 0\n                    } else {\n                        fail \"rdb child didn't terminate\"\n                    }\n\n                    # make sure we got what we were aiming for, by looking for the message in the log file\n                    if {$all_drop == \"all\"} {\n                        wait_for_log_messages -2 {\"*Diskless rdb transfer, last replica dropped, killing fork child*\"} $loglines 1 1\n                    }\n                    if {$all_drop == \"no\"} {\n                        wait_for_log_messages -2 {\"*Diskless rdb transfer, done reading from pipe, 2 replicas still up*\"} $loglines 1 1\n                    }\n                    if {$all_drop == \"slow\" || $all_drop == \"fast\"} {\n                        wait_for_log_messages -2 {\"*Diskless rdb transfer, done reading from pipe, 1 replicas still up*\"} $loglines 1 1\n                    }\n                    if {$all_drop == \"timeout\"} {\n                        wait_for_log_messages -2 {\"*Disconnecting timedout replica (full sync)*\"} $loglines 1 1\n                        wait_for_log_messages -2 {\"*Diskless rdb transfer, done reading from pipe, 1 replicas still up*\"} $loglines 1 1\n                        # master disconnected the slow replica, remove from array\n                        set replicas_alive [lreplace $replicas_alive 0 0]\n                        # release it\n                        exec kill -SIGCONT [srv -1 pid]\n                    }\n\n                    # make sure we don't have a busy loop going thought epoll_wait\n                    if {$measure_time} {\n                        set master_end_metrics [get_cpu_metrics $master_statfile]\n                        set time_elapsed [expr {[clock seconds]-$start_time}]\n                        set master_cpu [compute_cpu_usage $master_start_metrics $master_end_metrics]\n                        set master_utime [lindex $master_cpu 0]\n                        set master_stime [lindex $master_cpu 1]\n                        if {$::verbose} {\n                            puts \"elapsed: $time_elapsed\"\n                            puts \"master utime: $master_utime\"\n                            puts \"master stime: $master_stime\"\n                        }\n                        if {!$::no_latency && ($all_drop == \"all\" || $all_drop == \"slow\" || $all_drop == \"timeout\")} {\n                            assert {$master_utime < 70}\n                            assert {$master_stime < 70}\n                        }\n                        if {!$::no_latency && ($all_drop == \"none\" || $all_drop == \"fast\")} {\n                            assert {$master_utime < 15}\n                            assert {$master_stime < 15}\n                        }\n                    }\n\n                    # verify the data integrity\n                    foreach replica $replicas_alive {\n                        # Wait that replicas acknowledge they are online so\n                        # we are sure that DBSIZE and DEBUG DIGEST will not\n                        # fail because of timing issues.\n                        wait_for_condition 150 100 {\n                            [lindex [$replica role] 3] eq {connected}\n                        } else {\n                            fail \"replicas still not connected after some time\"\n                        }\n\n                        # Make sure that replicas and master have same\n                        # number of keys\n                        wait_for_condition 50 100 {\n                            [$master dbsize] == [$replica dbsize]\n                        } else {\n                            fail \"Different number of keys between master and replicas after too long time.\"\n                        }\n\n                        # Check digests\n                        set digest [$master debug digest]\n                        set digest0 [$replica debug digest]\n                        assert {$digest ne 0000000000000000000000000000000000000000}\n                        assert {$digest eq $digest0}\n                    }\n                }\n            }\n        }\n    }\n\n    $master config set enable-keydb-fastsync yes\n}\n\nif 0 {\n    # This test is not applicable to forkless bgsave\ntest \"diskless replication child being killed is collected\" {\n    # when diskless master is waiting for the replica to become writable\n    # it removes the read event from the rdb pipe so if the child gets killed\n    # the replica will hung. and the master may not collect the pid with waitpid\n    start_server {tags {\"repl\"}} {\n        set master [srv 0 client]\n        set master_host [srv 0 host]\n        set master_port [srv 0 port]\n        set master_pid [srv 0 pid]\n        $master config set repl-diskless-sync yes\n        $master config set repl-diskless-sync-delay 0\n        # put enough data in the db that the rdb file will be bigger than the socket buffers\n        $master debug populate 20000 test 10000\n        $master config set rdbcompression no\n        start_server {} {\n            set replica [srv 0 client]\n            set loglines [count_log_lines 0]\n            $replica config set repl-diskless-load swapdb\n            $replica config set key-load-delay 1000000\n            $replica replicaof $master_host $master_port\n\n            # wait for the replicas to start reading the rdb\n            wait_for_log_messages 0 {\"*Loading DB in memory*\"} $loglines 800 10\n\n            # wait to be sure the eplica is hung and the master is blocked on write\n            after 500\n\n            # simulate the OOM killer or anyone else kills the child\n            set fork_child_pid [get_child_pid -1]\n            exec kill -9 $fork_child_pid\n\n            # wait for the parent to notice the child have exited\n            wait_for_condition 50 100 {\n                [s -1 rdb_bgsave_in_progress] == 0\n            } else {\n                fail \"rdb child didn't terminate\"\n            }\n        }\n    }\n}\n    # Neither is this test\ntest \"diskless replication read pipe cleanup\" {\n    # In diskless replication, we create a read pipe for the RDB, between the child and the parent.\n    # When we close this pipe (fd), the read handler also needs to be removed from the event loop (if it still registered).\n    # Otherwise, next time we will use the same fd, the registration will be fail (panic), because\n    # we will use EPOLL_CTL_MOD (the fd still register in the event loop), on fd that already removed from epoll_ctl\n    start_server {tags {\"repl\"}} {\n        set master [srv 0 client]\n        set master_host [srv 0 host]\n        set master_port [srv 0 port]\n        set master_pid [srv 0 pid]\n        $master config set repl-diskless-sync yes\n        $master config set repl-diskless-sync-delay 0\n\n        # put enough data in the db, and slowdown the save, to keep the parent busy at the read process\n        $master config set rdb-key-save-delay 100000\n        $master debug populate 20000 test 10000\n        $master config set rdbcompression no\n        start_server {} {\n            set replica [srv 0 client]\n            set loglines [count_log_lines 0]\n            $replica config set repl-diskless-load swapdb\n            $replica replicaof $master_host $master_port\n\n            # wait for the replicas to start reading the rdb\n            wait_for_log_messages 0 {\"*Loading DB in memory*\"} $loglines 800 10\n\n            set loglines [count_log_lines 0]\n            # send FLUSHALL so the RDB child will be killed\n            $master flushall\n\n            # wait for another RDB child process to be started\n            wait_for_log_messages -1 {\"*Background RDB transfer started by pid*\"} $loglines 800 10\n\n            # make sure master is alive\n            $master ping\n        }\n    }\n}\n}\n\ntest {replicaof right after disconnection} {\n    # this is a rare race condition that was reproduced sporadically by the psync2 unit.\n    # see details in #7205\n    start_server {tags {\"repl\"}} {\n        set replica1 [srv 0 client]\n        set replica1_host [srv 0 host]\n        set replica1_port [srv 0 port]\n        set replica1_log [srv 0 stdout]\n        start_server {} {\n            set replica2 [srv 0 client]\n            set replica2_host [srv 0 host]\n            set replica2_port [srv 0 port]\n            set replica2_log [srv 0 stdout]\n            start_server {} {\n                set master [srv 0 client]\n                set master_host [srv 0 host]\n                set master_port [srv 0 port]\n                $replica1 replicaof $master_host $master_port\n                $replica2 replicaof $master_host $master_port\n\n                wait_for_condition 50 100 {\n                    [string match {*master_link_status:up*} [$replica1 info replication]] &&\n                    [string match {*master_link_status:up*} [$replica2 info replication]]\n                } else {\n                    fail \"Can't turn the instance into a replica\"\n                }\n\n                set rd [redis_deferring_client -1]\n                $rd debug sleep 1\n                after 100\n\n                # when replica2 will wake up from the sleep it will find both disconnection\n                # from it's master and also a replicaof command at the same event loop\n                $master client kill type replica\n                $replica2 replicaof $replica1_host $replica1_port\n                $rd read\n\n                wait_for_condition 50 100 {\n                    [string match {*master_link_status:up*} [$replica2 info replication]]\n                } else {\n                    fail \"role change failed.\"\n                }\n\n                # make sure psync succeeded, and there were no unexpected full syncs.\n                assert_equal [status $master sync_full] 2\n                assert_equal [status $replica1 sync_full] 0\n                assert_equal [status $replica2 sync_full] 0\n            }\n        }\n    }\n}\n\ntest {Kill rdb child process if its dumping RDB is not useful} {\n    start_server {tags {\"repl\"}} {\n        set slave1 [srv 0 client]\n        $slave1 config set enable-keydb-fastsync no\n        start_server {} {\n            set slave2 [srv 0 client]\n            $slave2 config set enable-keydb-fastsync no\n            start_server {} {\n                set master [srv 0 client]\n                set master_host [srv 0 host]\n                set master_port [srv 0 port]\n                for {set i 0} {$i < 10} {incr i} {\n                    $master set $i $i\n                }\n                # Generating RDB will cost 10s(10 * 1s)\n                $master config set rdb-key-save-delay 1000000\n                $master config set repl-diskless-sync no\n                $master config set save \"\"\n\n                $slave1 slaveof $master_host $master_port\n                $slave2 slaveof $master_host $master_port\n\n                # Wait for starting child\n                wait_for_condition 50 100 {\n                    ([s 0 rdb_bgsave_in_progress] == 1) &&\n                    ([string match \"*wait_bgsave*\" [s 0 slave0]]) &&\n                    ([string match \"*wait_bgsave*\" [s 0 slave1]])\n                } else {\n                    fail \"rdb child didn't start\"\n                }\n\n                # Slave1 disconnect with master\n                $slave1 slaveof no one\n                # Shouldn't kill child since another slave wait for rdb\n                after 100\n                assert {[s 0 rdb_bgsave_in_progress] == 1}\n\n                # Slave2 disconnect with master\n                $slave2 slaveof no one\n                # Should kill child\n                wait_for_condition 100 10 {\n                    [s 0 rdb_bgsave_in_progress] eq 0\n                } else {\n                    fail \"can't kill rdb child\"\n                }\n\n                # If have save parameters, won't kill child\n                $master config set save \"900 1\"\n                $slave1 slaveof $master_host $master_port\n                $slave2 slaveof $master_host $master_port\n                wait_for_condition 50 100 {\n                    ([s 0 rdb_bgsave_in_progress] == 1) &&\n                    ([string match \"*wait_bgsave*\" [s 0 slave0]]) &&\n                    ([string match \"*wait_bgsave*\" [s 0 slave1]])\n                } else {\n                    fail \"rdb child didn't start\"\n                }\n                $slave1 slaveof no one\n                $slave2 slaveof no one\n                after 200\n                assert {[s 0 rdb_bgsave_in_progress] == 1}\n                catch {$master shutdown nosave}\n            }\n        }\n    }\n}\n"
  }
]